1 //
2 //  m3_env.h
3 //
4 //  Created by Steven Massey on 4/19/19.
5 //  Copyright © 2019 Steven Massey. All rights reserved.
6 //
7 
8 #ifndef m3_env_h
9 #define m3_env_h
10 
11 #include "wasm3.h"
12 #include "m3_code.h"
13 #include "m3_compile.h"
14 
15 d_m3BeginExternC
16 
17 
18 //---------------------------------------------------------------------------------------------------------------------------------
19 
20 typedef struct M3MemoryInfo
21 {
22     u32     initPages;
23     u32     maxPages;
24 }
25 M3MemoryInfo;
26 
27 
28 typedef struct M3Memory
29 {
30     M3MemoryHeader *        mallocated;
31 
32     u32                     numPages;
33     u32                     maxPages;
34 }
35 M3Memory;
36 
37 typedef M3Memory *          IM3Memory;
38 
39 
40 //---------------------------------------------------------------------------------------------------------------------------------
41 
42 typedef struct M3DataSegment
43 {
44     const u8 *              initExpr;           // wasm code
45     const u8 *              data;
46 
47     u32                     initExprSize;
48     u32                     memoryRegion;
49     u32                     size;
50 }
51 M3DataSegment;
52 
53 //---------------------------------------------------------------------------------------------------------------------------------
54 
55 typedef struct M3Global
56 {
57     M3ImportInfo            import;
58 
59     union
60     {
61         i64 intValue;
62 #if d_m3HasFloat
63         f64 f64Value;
64         f32 f32Value;
65 #endif
66     };
67 
68     cstr_t                  name;
69     bytes_t                 initExpr;       // wasm code
70     u32                     initExprSize;
71     u8                      type;
72     bool                    imported;
73     bool                    isMutable;
74 }
75 M3Global;
76 
77 
78 //---------------------------------------------------------------------------------------------------------------------------------
79 typedef struct M3Module
80 {
81     struct M3Runtime *      runtime;
82     struct M3Environment *  environment;
83 
84     bytes_t                 wasmStart;
85     bytes_t                 wasmEnd;
86 
87     cstr_t                  name;
88 
89     u32                     numFuncTypes;
90     IM3FuncType *           funcTypes;              // array of pointers to list of FuncTypes
91 
92     u32                     numFuncImports;
93     u32                     numFunctions;
94     M3Function *            functions;
95 
96     i32                     startFunction;
97 
98     u32                     numDataSegments;
99     M3DataSegment *         dataSegments;
100 
101     //u32                     importedGlobals;
102     u32                     numGlobals;
103     M3Global *              globals;
104 
105     u32                     numElementSegments;
106     bytes_t                 elementSection;
107     bytes_t                 elementSectionEnd;
108 
109     IM3Function *           table0;
110     u32                     table0Size;
111 
112     M3MemoryInfo            memoryInfo;
113     bool                    memoryImported;
114 
115     //bool                    hasWasmCodeCopy;
116 
117     struct M3Module *       next;
118 }
119 M3Module;
120 
121 M3Result                    Module_AddGlobal            (IM3Module io_module, IM3Global * o_global, u8 i_type, bool i_mutable, bool i_isImported);
122 
123 M3Result                    Module_AddFunction          (IM3Module io_module, u32 i_typeIndex, IM3ImportInfo i_importInfo /* can be null */);
124 IM3Function                 Module_GetFunction          (IM3Module i_module, u32 i_functionIndex);
125 
126 void                        Module_GenerateNames        (IM3Module i_module);
127 
128 void                        FreeImportInfo              (M3ImportInfo * i_info);
129 
130 //---------------------------------------------------------------------------------------------------------------------------------
131 
132 typedef struct M3Environment
133 {
134 //    struct M3Runtime *      runtimes;
135 
136     IM3FuncType             funcTypes;                          // linked list of unique M3FuncType structs that can be compared using pointer-equivalence
137 
138     IM3FuncType             retFuncTypes [c_m3Type_unknown];    // these 'point' to elements in the linked list above.
139                                                                 // the number of elements must match the basic types as per M3ValueType
140     M3CodePage *            pagesReleased;
141 }
142 M3Environment;
143 
144 void                        Environment_Release         (IM3Environment i_environment);
145 
146 // takes ownership of io_funcType and returns a pointer to the persistent version (could be same or different)
147 void                        Environment_AddFuncType     (IM3Environment i_environment, IM3FuncType * io_funcType);
148 
149 //---------------------------------------------------------------------------------------------------------------------------------
150 
151 typedef struct M3Runtime
152 {
153     M3Compilation           compilation;
154 
155     IM3Environment          environment;
156 
157     M3CodePage *            pagesOpen;      // linked list of code pages with writable space on them
158     M3CodePage *            pagesFull;      // linked list of at-capacity pages
159 
160     u32                     numCodePages;
161     u32                     numActiveCodePages;
162 
163     IM3Module               modules;        // linked list of imported modules
164 
165     void *                  stack;
166     u32                     stackSize;
167     u32                     numStackSlots;
168     IM3Function             lastCalled;     // last function that successfully executed
169 
170     void *                  userdata;
171 
172     M3Memory                memory;
173     u32                     memoryLimit;
174 
175 #if d_m3EnableStrace >= 2
176     u32                     callDepth;
177 #endif
178 
179     M3ErrorInfo             error;
180 #if d_m3VerboseErrorMessages
181     char                    error_message[256]; // the actual buffer. M3ErrorInfo can point to this
182 #endif
183 
184 #if d_m3RecordBacktraces
185     M3BacktraceInfo         backtrace;
186 #endif
187 }
188 M3Runtime;
189 
190 void                        InitRuntime                 (IM3Runtime io_runtime, u32 i_stackSizeInBytes);
191 void                        Runtime_Release             (IM3Runtime io_runtime);
192 
193 M3Result                    ResizeMemory                (IM3Runtime io_runtime, u32 i_numPages);
194 
195 typedef void *              (* ModuleVisitor)           (IM3Module i_module, void * i_info);
196 void *                      ForEachModule               (IM3Runtime i_runtime, ModuleVisitor i_visitor, void * i_info);
197 
198 void *                      v_FindFunction              (IM3Module i_module, const char * const i_name);
199 
200 IM3CodePage                 AcquireCodePage             (IM3Runtime io_runtime);
201 IM3CodePage                 AcquireCodePageWithCapacity (IM3Runtime io_runtime, u32 i_lineCount);
202 void                        ReleaseCodePage             (IM3Runtime io_runtime, IM3CodePage i_codePage);
203 
204 d_m3EndExternC
205 
206 #endif // m3_env_h
207