| 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 | u32                     allFunctions;           // allocated functions count | 
|---|
| 95 | M3Function *            functions; | 
|---|
| 96 |  | 
|---|
| 97 | i32                     startFunction; | 
|---|
| 98 |  | 
|---|
| 99 | u32                     numDataSegments; | 
|---|
| 100 | M3DataSegment *         dataSegments; | 
|---|
| 101 |  | 
|---|
| 102 | //u32                     importedGlobals; | 
|---|
| 103 | u32                     numGlobals; | 
|---|
| 104 | M3Global *              globals; | 
|---|
| 105 |  | 
|---|
| 106 | u32                     numElementSegments; | 
|---|
| 107 | bytes_t                 elementSection; | 
|---|
| 108 | bytes_t                 elementSectionEnd; | 
|---|
| 109 |  | 
|---|
| 110 | IM3Function *           table0; | 
|---|
| 111 | u32                     table0Size; | 
|---|
| 112 |  | 
|---|
| 113 | M3MemoryInfo            memoryInfo; | 
|---|
| 114 | bool                    memoryImported; | 
|---|
| 115 |  | 
|---|
| 116 | //bool                    hasWasmCodeCopy; | 
|---|
| 117 |  | 
|---|
| 118 | struct M3Module *       next; | 
|---|
| 119 | } | 
|---|
| 120 | M3Module; | 
|---|
| 121 |  | 
|---|
| 122 | M3Result                    Module_AddGlobal            (IM3Module io_module, IM3Global * o_global, u8 i_type, bool i_mutable, bool i_isImported); | 
|---|
| 123 |  | 
|---|
| 124 | M3Result                    Module_PreallocFunctions    (IM3Module io_module, u32 i_totalFunctions); | 
|---|
| 125 | M3Result                    Module_AddFunction          (IM3Module io_module, u32 i_typeIndex, IM3ImportInfo i_importInfo /* can be null */); | 
|---|
| 126 | IM3Function                 Module_GetFunction          (IM3Module i_module, u32 i_functionIndex); | 
|---|
| 127 |  | 
|---|
| 128 | void                        Module_GenerateNames        (IM3Module i_module); | 
|---|
| 129 |  | 
|---|
| 130 | void                        FreeImportInfo              (M3ImportInfo * i_info); | 
|---|
| 131 |  | 
|---|
| 132 | //--------------------------------------------------------------------------------------------------------------------------------- | 
|---|
| 133 |  | 
|---|
| 134 | typedef struct M3Environment | 
|---|
| 135 | { | 
|---|
| 136 | //    struct M3Runtime *      runtimes; | 
|---|
| 137 |  | 
|---|
| 138 | IM3FuncType             funcTypes;                          // linked list of unique M3FuncType structs that can be compared using pointer-equivalence | 
|---|
| 139 |  | 
|---|
| 140 | IM3FuncType             retFuncTypes [c_m3Type_unknown];    // these 'point' to elements in the linked list above. | 
|---|
| 141 | // the number of elements must match the basic types as per M3ValueType | 
|---|
| 142 | M3CodePage *            pagesReleased; | 
|---|
| 143 |  | 
|---|
| 144 | M3SectionHandler        customSectionHandler; | 
|---|
| 145 | } | 
|---|
| 146 | M3Environment; | 
|---|
| 147 |  | 
|---|
| 148 | void                        Environment_Release         (IM3Environment i_environment); | 
|---|
| 149 |  | 
|---|
| 150 | // takes ownership of io_funcType and returns a pointer to the persistent version (could be same or different) | 
|---|
| 151 | void                        Environment_AddFuncType     (IM3Environment i_environment, IM3FuncType * io_funcType); | 
|---|
| 152 |  | 
|---|
| 153 | //--------------------------------------------------------------------------------------------------------------------------------- | 
|---|
| 154 |  | 
|---|
| 155 | typedef struct M3Runtime | 
|---|
| 156 | { | 
|---|
| 157 | M3Compilation           compilation; | 
|---|
| 158 |  | 
|---|
| 159 | IM3Environment          environment; | 
|---|
| 160 |  | 
|---|
| 161 | M3CodePage *            pagesOpen;      // linked list of code pages with writable space on them | 
|---|
| 162 | M3CodePage *            pagesFull;      // linked list of at-capacity pages | 
|---|
| 163 |  | 
|---|
| 164 | u32                     numCodePages; | 
|---|
| 165 | u32                     numActiveCodePages; | 
|---|
| 166 |  | 
|---|
| 167 | IM3Module               modules;        // linked list of imported modules | 
|---|
| 168 |  | 
|---|
| 169 | void *                  stack; | 
|---|
| 170 | u32                     stackSize; | 
|---|
| 171 | u32                     numStackSlots; | 
|---|
| 172 | IM3Function             lastCalled;     // last function that successfully executed | 
|---|
| 173 |  | 
|---|
| 174 | void *                  userdata; | 
|---|
| 175 |  | 
|---|
| 176 | M3Memory                memory; | 
|---|
| 177 | u32                     memoryLimit; | 
|---|
| 178 |  | 
|---|
| 179 | #if d_m3EnableStrace >= 2 | 
|---|
| 180 | u32                     callDepth; | 
|---|
| 181 | #endif | 
|---|
| 182 |  | 
|---|
| 183 | M3ErrorInfo             error; | 
|---|
| 184 | #if d_m3VerboseErrorMessages | 
|---|
| 185 | char                    error_message[256]; // the actual buffer. M3ErrorInfo can point to this | 
|---|
| 186 | #endif | 
|---|
| 187 |  | 
|---|
| 188 | #if d_m3RecordBacktraces | 
|---|
| 189 | M3BacktraceInfo         backtrace; | 
|---|
| 190 | #endif | 
|---|
| 191 |  | 
|---|
| 192 | u32						newCodePageSequence; | 
|---|
| 193 | } | 
|---|
| 194 | M3Runtime; | 
|---|
| 195 |  | 
|---|
| 196 | void                        InitRuntime                 (IM3Runtime io_runtime, u32 i_stackSizeInBytes); | 
|---|
| 197 | void                        Runtime_Release             (IM3Runtime io_runtime); | 
|---|
| 198 |  | 
|---|
| 199 | M3Result                    ResizeMemory                (IM3Runtime io_runtime, u32 i_numPages); | 
|---|
| 200 |  | 
|---|
| 201 | typedef void *              (* ModuleVisitor)           (IM3Module i_module, void * i_info); | 
|---|
| 202 | void *                      ForEachModule               (IM3Runtime i_runtime, ModuleVisitor i_visitor, void * i_info); | 
|---|
| 203 |  | 
|---|
| 204 | void *                      v_FindFunction              (IM3Module i_module, const char * const i_name); | 
|---|
| 205 |  | 
|---|
| 206 | IM3CodePage                 AcquireCodePage             (IM3Runtime io_runtime); | 
|---|
| 207 | IM3CodePage                 AcquireCodePageWithCapacity (IM3Runtime io_runtime, u32 i_lineCount); | 
|---|
| 208 | void                        ReleaseCodePage             (IM3Runtime io_runtime, IM3CodePage i_codePage); | 
|---|
| 209 |  | 
|---|
| 210 | d_m3EndExternC | 
|---|
| 211 |  | 
|---|
| 212 | #endif // m3_env_h | 
|---|
| 213 |  | 
|---|