forked from WAVM/WAVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntrinsics.cpp
More file actions
154 lines (132 loc) · 3.94 KB
/
Copy pathIntrinsics.cpp
File metadata and controls
154 lines (132 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "Inline/BasicTypes.h"
#include "Intrinsics.h"
#include "Runtime.h"
#include "RuntimePrivate.h"
#include <string>
namespace Intrinsics
{
struct ModuleImpl
{
std::map<std::string,Intrinsics::Function*> functionMap;
std::map<std::string,Intrinsics::Global*> globalMap;
std::map<std::string,Intrinsics::Memory*> memoryMap;
std::map<std::string,Intrinsics::Table*> tableMap;
};
Module::~Module()
{
if(impl)
{
delete impl;
}
}
static void initializeModule(Intrinsics::Module& moduleRef)
{
if(!moduleRef.impl) { moduleRef.impl = new Intrinsics::ModuleImpl; }
}
Function::Function(
Intrinsics::Module& moduleRef,
const char* inName,
void* inNativeFunction,
const IR::FunctionType* inType,
Runtime::CallingConvention inCallingConvention)
: name(inName)
, type(inType)
, nativeFunction(inNativeFunction)
, callingConvention(inCallingConvention)
{
initializeModule(moduleRef);
if(moduleRef.impl->functionMap.count(name))
{
Errors::fatalf("Intrinsic function already registered: %s",name);
}
moduleRef.impl->functionMap[name] = this;
}
Runtime::FunctionInstance* Function::instantiate(Runtime::Compartment* compartment)
{
return new Runtime::FunctionInstance(
nullptr,
IR::FunctionType::get(type->ret,type->parameters),
nativeFunction,
callingConvention);
}
Global::Global(Intrinsics::Module& moduleRef,const char* inName,IR::ValueType inType,Runtime::Value inValue)
: name(inName)
, type(inType)
, value(inValue)
{
initializeModule(moduleRef);
if(moduleRef.impl->globalMap.count(name))
{
Errors::fatalf("Intrinsic global already registered: %s",name);
}
moduleRef.impl->globalMap[name] = this;
}
Runtime::GlobalInstance* Global::instantiate(Runtime::Compartment* compartment)
{
return Runtime::createGlobal(compartment,IR::GlobalType(type,false),value);
}
Table::Table(Intrinsics::Module& moduleRef,const char* inName,const IR::TableType& inType)
: name(inName)
, type(inType)
{
initializeModule(moduleRef);
if(moduleRef.impl->tableMap.count(name))
{
Errors::fatalf("Intrinsic table already registered: %s",name);
}
moduleRef.impl->tableMap[name] = this;
}
Runtime::TableInstance* Table::instantiate(Runtime::Compartment* compartment)
{
return Runtime::createTable(compartment,type);
}
Memory::Memory(Intrinsics::Module& moduleRef,const char* inName,const IR::MemoryType& inType)
: name(inName)
, type(inType)
{
initializeModule(moduleRef);
if(moduleRef.impl->memoryMap.count(name))
{
Errors::fatalf("Intrinsic memory already registered: %s",name);
}
moduleRef.impl->memoryMap[name] = this;
}
Runtime::MemoryInstance* Memory::instantiate(Runtime::Compartment* compartment)
{
return Runtime::createMemory(compartment,type);
}
Runtime::ModuleInstance* instantiateModule(
Runtime::Compartment* compartment,
const Intrinsics::Module& moduleRef)
{
auto moduleInstance = new Runtime::ModuleInstance(compartment,{},{},{},{},{});
if(moduleRef.impl)
{
for(const auto& pair : moduleRef.impl->functionMap)
{
auto functionInstance = pair.second->instantiate(compartment);
moduleInstance->functions.push_back(functionInstance);
moduleInstance->exportMap[pair.first] = functionInstance;
}
for(const auto& pair : moduleRef.impl->tableMap)
{
auto tableInstance = pair.second->instantiate(compartment);
moduleInstance->tables.push_back(tableInstance);
moduleInstance->exportMap[pair.first] = tableInstance;
}
for(const auto& pair : moduleRef.impl->memoryMap)
{
auto memoryInstance = pair.second->instantiate(compartment);
moduleInstance->memories.push_back(memoryInstance);
moduleInstance->exportMap[pair.first] = memoryInstance;
}
for(const auto& pair : moduleRef.impl->globalMap)
{
auto globalInstance = pair.second->instantiate(compartment);
moduleInstance->globals.push_back(globalInstance);
moduleInstance->exportMap[pair.first] = globalInstance;
}
}
return moduleInstance;
}
}