/src/wasm3/source/m3_function.h
Line | Count | Source |
1 | | // |
2 | | // m3_function.h |
3 | | // |
4 | | // Created by Steven Massey on 4/7/21. |
5 | | // Copyright © 2021 Steven Massey. All rights reserved. |
6 | | // |
7 | | |
8 | | #ifndef m3_function_h |
9 | | #define m3_function_h |
10 | | |
11 | | #include "m3_core.h" |
12 | | |
13 | | d_m3BeginExternC |
14 | | |
15 | | //--------------------------------------------------------------------------------------------------------------------------------- |
16 | | |
17 | | typedef struct M3FuncType { |
18 | | struct M3FuncType* next; |
19 | | |
20 | | u16 numRets; |
21 | | u16 numArgs; |
22 | | |
23 | | // Position in the environment's list of distinct function types. Equal |
24 | | // types share one M3FuncType, so this doubles as the canonical heap type |
25 | | // index a (ref $t) carries, and comparing two of them is exactly the |
26 | | // structural equivalence the spec asks for. |
27 | | u16 canonicalIndex; |
28 | | |
29 | | m3type_t types[]; // returns, then args |
30 | | } M3FuncType; |
31 | | |
32 | | typedef M3FuncType* IM3FuncType; |
33 | | |
34 | | |
35 | | M3Result AllocFuncType (IM3FuncType* o_functionType, u32 i_numTypes); |
36 | | bool AreFuncTypesEqual (const IM3FuncType i_typeA, const IM3FuncType i_typeB); |
37 | | |
38 | | u16 GetFuncTypeNumParams (const IM3FuncType i_funcType); |
39 | | m3type_t GetFuncTypeParamType (const IM3FuncType i_funcType, u16 i_index); |
40 | | |
41 | | u16 GetFuncTypeNumResults (const IM3FuncType i_funcType); |
42 | | m3type_t GetFuncTypeResultType (const IM3FuncType i_funcType, u16 i_index); |
43 | | |
44 | | #if d_m3HasTypedRefs |
45 | | // The type a (ref $t) / (ref null $t) naming this function type is spelled as |
46 | | m3type_t RefTypeOfFuncType (const IM3FuncType i_funcType, bool i_nonNull); |
47 | | #endif |
48 | | |
49 | | //--------------------------------------------------------------------------------------------------------------------------------- |
50 | | |
51 | | typedef struct M3Function { |
52 | | struct M3Module* module; |
53 | | |
54 | | M3ImportInfo import; |
55 | | |
56 | | // The linear memory a host function bound to this import addresses. Set to |
57 | | // the module's memory 0 when the host function is bound - what a bare i32 |
58 | | // guest pointer means when nothing says otherwise - and repointed by |
59 | | // m3_BindImportMemory for a host module whose ABI names a memory instead. |
60 | | // Holds the memory rather than its index: a slot can alias another module's |
61 | | // memory, and growing one reallocates behind it. NULL for anything that is |
62 | | // not an import bound to a host function. |
63 | | struct M3Memory* hostMemory; |
64 | | |
65 | | // An import linked to another module's export: the function that actually |
66 | | // implements it. Everything that calls through an import resolves to this |
67 | | // first, so the call carries the defining module with it - which is what |
68 | | // says whose linear memory the body runs against. NULL for a function with |
69 | | // a body of its own, and for an import bound to a host function (that one |
70 | | // runs against the importing module's memory). |
71 | | struct M3Function* resolved; |
72 | | |
73 | | bytes_t wasm; |
74 | | bytes_t wasmEnd; |
75 | | |
76 | | cstr_t names[d_m3MaxDuplicateFunctionImpl]; |
77 | | cstr_t export_name; // should be a part of "names" |
78 | | u16 numNames; // maximum of d_m3MaxDuplicateFunctionImpl |
79 | | |
80 | | IM3FuncType funcType; |
81 | | |
82 | | pc_t compiled; |
83 | | |
84 | | #if (d_m3EnableCodePageRefCounting) |
85 | | IM3CodePage* codePageRefs; // array of all pages used |
86 | | u32 numCodePageRefs; |
87 | | #endif |
88 | | |
89 | | #if defined(DEBUG) |
90 | | u32 hits; |
91 | | u32 index; |
92 | | #endif |
93 | | |
94 | | u16 maxStackSlots; |
95 | | |
96 | | u16 numRetSlots; |
97 | | u16 numRetAndArgSlots; |
98 | | |
99 | | u16 numLocals; // not including args |
100 | | u32 numLocalBytes; |
101 | | |
102 | | bool ownsWasmCode; |
103 | | |
104 | | u16 numConstantBytes; |
105 | | void* constants; |
106 | | } M3Function; |
107 | | |
108 | | |
109 | | // The function that actually runs when this one is called: an import linked to |
110 | | // another module resolves to that module's function, everything else to itself. |
111 | | static inline |
112 | | IM3Function Function_Implementation (IM3Function i_function) |
113 | 105 | { |
114 | 105 | return i_function->resolved ? i_function->resolved : i_function; |
115 | 105 | } Unexecuted instantiation: fuzzer.c:Function_Implementation m3_env.c:Function_Implementation Line | Count | Source | 113 | 1 | { | 114 | 1 | return i_function->resolved ? i_function->resolved : i_function; | 115 | 1 | } |
Unexecuted instantiation: m3_function.c:Function_Implementation Unexecuted instantiation: m3_info.c:Function_Implementation Unexecuted instantiation: m3_module.c:Function_Implementation Unexecuted instantiation: m3_parse.c:Function_Implementation Unexecuted instantiation: m3_code.c:Function_Implementation m3_compile.c:Function_Implementation Line | Count | Source | 113 | 104 | { | 114 | 104 | return i_function->resolved ? i_function->resolved : i_function; | 115 | 104 | } |
Unexecuted instantiation: m3_core.c:Function_Implementation Unexecuted instantiation: m3_validate.c:Function_Implementation |
116 | | |
117 | | void Function_Release (IM3Function i_function); |
118 | | void Function_FreeCompiledCode (IM3Function i_function); |
119 | | |
120 | | cstr_t GetFunctionImportModuleName (IM3Function i_function); |
121 | | cstr_t* GetFunctionNames (IM3Function i_function, u16* o_numNames); |
122 | | u16 GetFunctionNumArgs (IM3Function i_function); |
123 | | m3type_t GetFunctionArgType (IM3Function i_function, u32 i_index); |
124 | | |
125 | | u16 GetFunctionNumReturns (IM3Function i_function); |
126 | | u8 GetFunctionReturnType (const IM3Function i_function, u16 i_index); |
127 | | |
128 | | u32 GetFunctionNumArgsAndLocals (IM3Function i_function); |
129 | | |
130 | | cstr_t SPrintFunctionArgList (IM3Function i_function, m3stack_t i_sp); |
131 | | cstr_t SPrintFunctionRetList (IM3FuncType i_funcType, m3stack_t i_sp); |
132 | | |
133 | | //--------------------------------------------------------------------------------------------------------------------------------- |
134 | | |
135 | | |
136 | | d_m3EndExternC |
137 | | |
138 | | #endif /* m3_function_h */ |