forked from wasm3/wasm3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm3_env.h
274 lines (189 loc) · 8.52 KB
/
m3_env.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//
// m3_env.h
//
// Created by Steven Massey on 4/19/19.
// Copyright © 2019 Steven Massey. All rights reserved.
//
#ifndef m3_env_h
#define m3_env_h
#include "wasm3.h"
#include "m3_code.h"
#include "m3_exec.h"
#include "m3_compile.h"
d_m3BeginExternC
typedef struct M3FuncType
{
struct M3FuncType * next;
u32 numArgs;
u8 returnType;
u8 argTypes [3]; // M3FuncType is a dynamically sized object; these are padding
}
M3FuncType;
typedef M3FuncType * IM3FuncType;
M3Result AllocFuncType (IM3FuncType * o_functionType, u32 i_numArgs);
bool AreFuncTypesEqual (const IM3FuncType i_typeA, const IM3FuncType i_typeB);
//---------------------------------------------------------------------------------------------------------------------------------
typedef struct M3Function
{
struct M3Module * module;
M3ImportInfo import;
bytes_t wasm;
bytes_t wasmEnd;
cstr_t name;
IM3FuncType funcType;
pc_t compiled;
# if (d_m3EnableCodePageRefCounting)
IM3CodePage * codePageRefs; // array of all pages used
u32 numCodePageRefs;
# endif
# if defined(DEBUG)
u32 hits;
# endif
u16 maxStackSlots;
u16 numArgSlots;
u16 numLocals; // not including args
u16 numLocalBytes;
void * constants;
u16 numConstantBytes;
bool ownsWasmCode;
}
M3Function;
void Function_Release (IM3Function i_function);
void Function_FreeCompiledCode (IM3Function i_function);
cstr_t GetFunctionImportModuleName (IM3Function i_function);
cstr_t GetFunctionName (IM3Function i_function);
u32 GetFunctionNumArgs (IM3Function i_function);
u32 GetFunctionNumReturns (IM3Function i_function);
u8 GetFunctionReturnType (IM3Function i_function);
u32 GetFunctionNumArgsAndLocals (IM3Function i_function);
cstr_t SPrintFunctionArgList (IM3Function i_function, m3stack_t i_sp);
//---------------------------------------------------------------------------------------------------------------------------------
typedef struct M3MemoryInfo
{
u32 initPages;
u32 maxPages;
}
M3MemoryInfo;
typedef struct M3Memory
{
M3MemoryHeader * mallocated;
u32 numPages;
u32 maxPages;
}
M3Memory;
typedef M3Memory * IM3Memory;
//---------------------------------------------------------------------------------------------------------------------------------
typedef struct M3DataSegment
{
const u8 * initExpr; // wasm code
const u8 * data;
u32 initExprSize;
u32 memoryRegion;
u32 size;
}
M3DataSegment;
void FreeImportInfo (M3ImportInfo * i_info);
//---------------------------------------------------------------------------------------------------------------------------------
typedef struct M3Global
{
M3ImportInfo import;
union
{
i64 intValue;
#if d_m3HasFloat
f64 f64Value;
f32 f32Value;
#endif
};
bytes_t initExpr; // wasm code
u32 initExprSize;
u8 type;
bool imported;
bool isMutable;
}
M3Global;
typedef M3Global * IM3Global;
//---------------------------------------------------------------------------------------------------------------------------------
typedef struct M3Module
{
struct M3Runtime * runtime;
struct M3Environment * environment;
cstr_t name;
u32 numFuncTypes;
IM3FuncType * funcTypes; // array of pointers to list of FuncTypes
u32 numImports;
IM3Function * imports; // notice: "I" prefix. imports are pointers to functions in another module.
u32 numFunctions;
M3Function * functions;
i32 startFunction;
u32 numDataSegments;
M3DataSegment * dataSegments;
u32 importedGlobals;
u32 numGlobals;
M3Global * globals;
u32 numElementSegments;
bytes_t elementSection;
bytes_t elementSectionEnd;
IM3Function * table0;
u32 table0Size;
M3MemoryInfo memoryInfo;
bool memoryImported;
bool hasWasmCodeCopy;
struct M3Module * next;
}
M3Module;
M3Result Module_AddGlobal (IM3Module io_module, IM3Global * o_global, u8 i_type, bool i_mutable, bool i_isImported);
M3Result Module_AddFunction (IM3Module io_module, u32 i_typeIndex, IM3ImportInfo i_importInfo /* can be null */);
IM3Function Module_GetFunction (IM3Module i_module, u32 i_functionIndex);
//---------------------------------------------------------------------------------------------------------------------------------
static const u32 c_m3NumTypesPerPage = 8;
//---------------------------------------------------------------------------------------------------------------------------------
typedef struct M3Environment
{
// struct M3Runtime * runtimes;
IM3FuncType funcTypes; // linked list
M3CodePage * pagesReleased;
}
M3Environment;
void Environment_Release (IM3Environment i_environment);
// takes ownership of io_funcType and returns a pointer to the persistent version (could be same or different)
void Environment_AddFuncType (IM3Environment i_environment, IM3FuncType * io_funcType);
//---------------------------------------------------------------------------------------------------------------------------------
// OPTZ: function types need to move to the runtime structure so that all modules can share types
// then type equality can be a simple pointer compare for indirect call checks
typedef struct M3Runtime
{
M3Compilation compilation;
IM3Environment environment;
M3CodePage * pagesOpen; // linked list of code pages with writable space on them
M3CodePage * pagesFull; // linked list of at-capacity pages
u32 numCodePages;
u32 numActiveCodePages;
IM3Module modules; // linked list of imported modules
void * stack;
u32 stackSize;
u32 numStackSlots;
u32 argc;
ccstr_t * argv;
M3Result runtimeError;
M3Memory memory;
u32 memoryLimit;
M3ErrorInfo error;
#if d_m3VerboseLogs
char error_message[256];
#endif
i32 exit_code;
}
M3Runtime;
void InitRuntime (IM3Runtime io_runtime, u32 i_stackSizeInBytes);
void Runtime_Release (IM3Runtime io_runtime);
M3Result ResizeMemory (IM3Runtime io_runtime, u32 i_numPages);
typedef void * (* ModuleVisitor) (IM3Module i_module, void * i_info);
void * ForEachModule (IM3Runtime i_runtime, ModuleVisitor i_visitor, void * i_info);
void * v_FindFunction (IM3Module i_module, const char * const i_name);
IM3CodePage AcquireCodePage (IM3Runtime io_runtime);
IM3CodePage AcquireCodePageWithCapacity (IM3Runtime io_runtime, u32 i_lineCount);
void ReleaseCodePage (IM3Runtime io_runtime, IM3CodePage i_codePage);
M3Result m3Error (M3Result i_result, IM3Runtime i_runtime, IM3Module i_module, IM3Function i_function, const char * const i_file, u32 i_lineNum, const char * const i_errorMessage, ...);
d_m3EndExternC
#endif // m3_env_h