1 | // |
2 | // m3_function.c |
3 | // |
4 | // Created by Steven Massey on 4/7/21. |
5 | // Copyright © 2021 Steven Massey. All rights reserved. |
6 | // |
7 | |
8 | #include "m3_function.h" |
9 | #include "m3_env.h" |
10 | |
11 | |
12 | M3Result AllocFuncType (IM3FuncType * o_functionType, u32 i_numTypes) |
13 | { |
14 | *o_functionType = (IM3FuncType) m3_Malloc ("M3FuncType" , sizeof (M3FuncType) + i_numTypes); |
15 | return (*o_functionType) ? m3Err_none : m3Err_mallocFailed; |
16 | } |
17 | |
18 | |
19 | bool AreFuncTypesEqual (const IM3FuncType i_typeA, const IM3FuncType i_typeB) |
20 | { |
21 | if (i_typeA->numRets == i_typeB->numRets && i_typeA->numArgs == i_typeB->numArgs) |
22 | { |
23 | return (memcmp (i_typeA->types, i_typeB->types, i_typeA->numRets + i_typeA->numArgs) == 0); |
24 | } |
25 | |
26 | return false; |
27 | } |
28 | |
29 | u16 GetFuncTypeNumParams (const IM3FuncType i_funcType) |
30 | { |
31 | return i_funcType ? i_funcType->numArgs : 0; |
32 | } |
33 | |
34 | |
35 | u8 GetFuncTypeParamType (const IM3FuncType i_funcType, u16 i_index) |
36 | { |
37 | u8 type = c_m3Type_unknown; |
38 | |
39 | if (i_funcType) |
40 | { |
41 | if (i_index < i_funcType->numArgs) |
42 | { |
43 | type = i_funcType->types [i_funcType->numRets + i_index]; |
44 | } |
45 | } |
46 | |
47 | return type; |
48 | } |
49 | |
50 | |
51 | |
52 | u16 GetFuncTypeNumResults (const IM3FuncType i_funcType) |
53 | { |
54 | return i_funcType ? i_funcType->numRets : 0; |
55 | } |
56 | |
57 | |
58 | u8 GetFuncTypeResultType (const IM3FuncType i_funcType, u16 i_index) |
59 | { |
60 | u8 type = c_m3Type_unknown; |
61 | |
62 | if (i_funcType) |
63 | { |
64 | if (i_index < i_funcType->numRets) |
65 | { |
66 | type = i_funcType->types [i_index]; |
67 | } |
68 | } |
69 | |
70 | return type; |
71 | } |
72 | |
73 | |
74 | //--------------------------------------------------------------------------------------------------------------- |
75 | |
76 | |
77 | void FreeImportInfo (M3ImportInfo * i_info) |
78 | { |
79 | m3_Free (i_info->moduleUtf8); |
80 | m3_Free (i_info->fieldUtf8); |
81 | } |
82 | |
83 | |
84 | void Function_Release (IM3Function i_function) |
85 | { |
86 | m3_Free (i_function->constants); |
87 | |
88 | for (int i = 0; i < i_function->numNames; i++) |
89 | { |
90 | // name can be an alias of fieldUtf8 |
91 | if (i_function->names[i] != i_function->import.fieldUtf8) |
92 | { |
93 | m3_Free (i_function->names[i]); |
94 | } |
95 | } |
96 | |
97 | FreeImportInfo (& i_function->import); |
98 | |
99 | if (i_function->ownsWasmCode) |
100 | m3_Free (i_function->wasm); |
101 | |
102 | // Function_FreeCompiledCode (func); |
103 | |
104 | # if (d_m3EnableCodePageRefCounting) |
105 | { |
106 | m3_Free (i_function->codePageRefs); |
107 | i_function->numCodePageRefs = 0; |
108 | } |
109 | # endif |
110 | } |
111 | |
112 | |
113 | void Function_FreeCompiledCode (IM3Function i_function) |
114 | { |
115 | # if (d_m3EnableCodePageRefCounting) |
116 | { |
117 | i_function->compiled = NULL; |
118 | |
119 | while (i_function->numCodePageRefs--) |
120 | { |
121 | IM3CodePage page = i_function->codePageRefs [i_function->numCodePageRefs]; |
122 | |
123 | if (--(page->info.usageCount) == 0) |
124 | { |
125 | // printf ("free %p\n", page); |
126 | } |
127 | } |
128 | |
129 | m3_Free (i_function->codePageRefs); |
130 | |
131 | Runtime_ReleaseCodePages (i_function->module->runtime); |
132 | } |
133 | # endif |
134 | } |
135 | |
136 | |
137 | cstr_t m3_GetFunctionName (IM3Function i_function) |
138 | { |
139 | u16 numNames = 0; |
140 | cstr_t *names = GetFunctionNames(i_function, &numNames); |
141 | if (numNames > 0) |
142 | return names[0]; |
143 | else |
144 | return "<unnamed>" ; |
145 | } |
146 | |
147 | |
148 | IM3Module m3_GetFunctionModule (IM3Function i_function) |
149 | { |
150 | return i_function ? i_function->module : NULL; |
151 | } |
152 | |
153 | |
154 | cstr_t * GetFunctionNames (IM3Function i_function, u16 * o_numNames) |
155 | { |
156 | if (!i_function || !o_numNames) |
157 | return NULL; |
158 | |
159 | if (i_function->import.fieldUtf8) |
160 | { |
161 | *o_numNames = 1; |
162 | return &i_function->import.fieldUtf8; |
163 | } |
164 | else |
165 | { |
166 | *o_numNames = i_function->numNames; |
167 | return i_function->names; |
168 | } |
169 | } |
170 | |
171 | |
172 | cstr_t GetFunctionImportModuleName (IM3Function i_function) |
173 | { |
174 | return (i_function->import.moduleUtf8) ? i_function->import.moduleUtf8 : "" ; |
175 | } |
176 | |
177 | |
178 | u16 GetFunctionNumArgs (IM3Function i_function) |
179 | { |
180 | u16 numArgs = 0; |
181 | |
182 | if (i_function) |
183 | { |
184 | if (i_function->funcType) |
185 | numArgs = i_function->funcType->numArgs; |
186 | } |
187 | |
188 | return numArgs; |
189 | } |
190 | |
191 | u8 GetFunctionArgType (IM3Function i_function, u32 i_index) |
192 | { |
193 | u8 type = c_m3Type_none; |
194 | |
195 | if (i_index < GetFunctionNumArgs (i_function)) |
196 | { |
197 | u32 numReturns = i_function->funcType->numRets; |
198 | |
199 | type = i_function->funcType->types [numReturns + i_index]; |
200 | } |
201 | |
202 | return type; |
203 | } |
204 | |
205 | |
206 | u16 GetFunctionNumReturns (IM3Function i_function) |
207 | { |
208 | u16 numReturns = 0; |
209 | |
210 | if (i_function) |
211 | { |
212 | if (i_function->funcType) |
213 | numReturns = i_function->funcType->numRets; |
214 | } |
215 | |
216 | return numReturns; |
217 | } |
218 | |
219 | |
220 | u8 GetFunctionReturnType (const IM3Function i_function, u16 i_index) |
221 | { |
222 | return i_function ? GetFuncTypeResultType (i_function->funcType, i_index) : c_m3Type_unknown; |
223 | } |
224 | |
225 | |
226 | u32 GetFunctionNumArgsAndLocals (IM3Function i_function) |
227 | { |
228 | if (i_function) |
229 | return i_function->numLocals + GetFunctionNumArgs (i_function); |
230 | else |
231 | return 0; |
232 | } |
233 | |
234 | |