/src/wasm3/source/m3_compile.c
Line | Count | Source |
1 | | // |
2 | | // m3_compile.c |
3 | | // |
4 | | // Created by Steven Massey on 4/17/19. |
5 | | // Copyright © 2019 Steven Massey. All rights reserved. |
6 | | // |
7 | | |
8 | | // Allow using opcodes for compilation process |
9 | | #define M3_COMPILE_OPCODES |
10 | | |
11 | | #include "m3_env.h" |
12 | | #include "m3_compile.h" |
13 | | #include "m3_exec.h" |
14 | | #include "m3_exception.h" |
15 | | #include "m3_info.h" |
16 | | #include "m3_validate.h" |
17 | | |
18 | | // Resolves M3OpInfo.compiler, an index, to the function it names. Defined down |
19 | | // with c_compilers [], which has to follow every compiler it points at. |
20 | | static |
21 | | M3Compiler GetCompiler (IM3OpInfo i_opInfo); |
22 | | |
23 | | //----- EMIT -------------------------------------------------------------------------------------------------------------- |
24 | | |
25 | | static inline |
26 | | pc_t GetPC (IM3Compilation o) |
27 | 121k | { |
28 | 121k | return GetPagePC(o->page); |
29 | 121k | } |
30 | | |
31 | | #if d_m3HasSnapshots |
32 | | |
33 | | # define d_m3GrowSnapshotArray(TYPE, ARRAY, COUNT, CAPACITY) \ |
34 | 0 | if ((COUNT) == (CAPACITY)) { \ |
35 | 0 | u32 newCapacity = (CAPACITY) ? (CAPACITY) * 2 : 8; \ |
36 | 0 | TYPE* grown = m3_ReallocArray(TYPE, (ARRAY), newCapacity, (CAPACITY)); \ |
37 | 0 | if (not grown) { \ |
38 | 0 | return m3Err_mallocFailed; \ |
39 | 0 | } \ |
40 | 0 | (ARRAY) = grown; \ |
41 | 0 | (CAPACITY) = newCapacity; \ |
42 | 0 | } |
43 | | |
44 | | // Where the instruction being compiled starts, counted from the start of the |
45 | | // function's body: the one position every build of every revision agrees on |
46 | | static inline |
47 | | u32 GetWasmOffset (IM3Compilation o) |
48 | 0 | { |
49 | 0 | return (u32)(o->lastOpcodeStart - o->bodyStart); |
50 | 0 | } |
51 | | |
52 | | // Whether the operand stack entry at i_index is a value the frame holds. Every |
53 | | // block keeps records of its parameter and result slots on the stack, between |
54 | | // the height it was entered at and where its own values begin (see |
55 | | // CompileBlock). Those are landing pads rather than values: nothing need have |
56 | | // written them yet, and the slots they name may be in use for something else. |
57 | | // The function's own scope has none - that stretch is its arguments and locals. |
58 | | static |
59 | | bool IsLiveStackEntry (IM3Compilation o, u16 i_index) |
60 | 0 | { |
61 | 0 | for (IM3CompilationScope scope = &o->block; scope->outer; scope = scope->outer) { |
62 | 0 | if (i_index >= scope->exitStackIndex and i_index < scope->blockStackIndex) { |
63 | 0 | return false; |
64 | 0 | } |
65 | 0 | } |
66 | | |
67 | 0 | return true; |
68 | 0 | } |
69 | | |
70 | | static |
71 | | M3Result AddSafePointValue (IM3Compilation o, M3SafePoint* io_point, u16 i_index) |
72 | 0 | { |
73 | 0 | M3SnapshotMap* map = o->snapshotMap; |
74 | |
|
75 | 0 | d_m3GrowSnapshotArray(M3SlotValue, map->values, map->numValues, o->valuesCapacity); |
76 | |
|
77 | 0 | map->values[map->numValues].slot = o->wasmStack[i_index]; |
78 | 0 | map->values[map->numValues].type = o->typeStack[i_index]; |
79 | 0 | map->numValues++; |
80 | 0 | io_point->numValues++; |
81 | |
|
82 | 0 | return m3Err_none; |
83 | 0 | } |
84 | | |
85 | | // Records the pc i_wordsBack words before the current one as a safepoint, with |
86 | | // where every value on the live operand stack is kept there. |
87 | | // |
88 | | // i_numResults are the operation's results, on top of the stack. A call's or a |
89 | | // resume's are not written until it returns, so they are no part of the frame; |
90 | | // a suspend's are, since whatever resumes it writes them - and cont.bind can |
91 | | // write them while it waits. i_aux is what the kind keeps there (see |
92 | | // M3SafePoint). |
93 | | // |
94 | | // A back edge to i_loop leaves the rest of the loop's body behind: what the |
95 | | // frame holds is what was live before the loop, and the loop's parameters, |
96 | | // which the branch has just written into its landing pads for the next lap. |
97 | | static |
98 | | M3Result RecordSafePoint (IM3Compilation o, M3SafePointKind i_kind, u32 i_wordsBack, u16 i_numResults, |
99 | | u16 i_aux, IM3CompilationScope i_loop) |
100 | 12.3k | { |
101 | 12.3k | _try { |
102 | 12.3k | M3SnapshotMap* map = o->snapshotMap; |
103 | | |
104 | 12.3k | if (not map or not o->page) { |
105 | 12.3k | return m3Err_none; |
106 | 12.3k | } |
107 | | |
108 | 0 | d_m3GrowSnapshotArray(M3SafePoint, map->safePoints, map->numSafePoints, o->safePointsCapacity); |
109 | | |
110 | | // grown before it is pointed at: adding values never moves the safepoints |
111 | 0 | M3SafePoint* point = &map->safePoints[map->numSafePoints]; |
112 | 0 | point->pc = GetPC(o) - i_wordsBack; |
113 | 0 | point->wasmOffset = GetWasmOffset(o); |
114 | 0 | point->firstValue = map->numValues; |
115 | 0 | point->firstLocalValue = UINT32_MAX; |
116 | 0 | point->numValues = 0; |
117 | 0 | point->numResults = i_numResults; |
118 | 0 | point->aux = i_aux; |
119 | 0 | point->resumeType = c_m3Type_none; |
120 | 0 | point->kind = (u8)i_kind; |
121 | 0 | point->flags = 0; |
122 | | |
123 | | // Non-defaultable locals can still be uninitialized at a safepoint. Keep |
124 | | // their heap types, but allow the null placeholder until local.set makes |
125 | | // them definitely initialized. Branches to a loop use its entry context. |
126 | 0 | if (o->localInitDepth) { |
127 | 0 | point->firstLocalValue = map->numValues; |
128 | 0 | for (u32 i = 0; i < map->numLocals; ++i) { |
129 | 0 | d_m3GrowSnapshotArray(M3SlotValue, map->values, map->numValues, o->valuesCapacity); |
130 | 0 | M3SlotValue value = map->locals[i]; |
131 | 0 | if (o->localInitDepth[i] == UINT32_MAX or |
132 | 0 | (i_loop and o->localInitDepth[i] >= (u32)i_loop->depth)) { |
133 | 0 | value.type &= ~d_m3Type_refNonNull; |
134 | 0 | } |
135 | 0 | map->values[map->numValues++] = value; |
136 | 0 | } |
137 | 0 | point->firstValue = map->numValues; |
138 | 0 | } |
139 | | |
140 | 0 | u16 top = o->stackIndex; |
141 | |
|
142 | 0 | if (i_kind != safepoint_suspend) { |
143 | 0 | top = (top > i_numResults) ? (u16)(top - i_numResults) : 0; |
144 | 0 | } |
145 | 0 | if (i_loop) { |
146 | 0 | top = M3_MIN(top, i_loop->exitStackIndex); |
147 | 0 | } |
148 | |
|
149 | 0 | for (u16 i = o->stackFirstDynamicIndex; i < top; ++i) { |
150 | 0 | if (IsLiveStackEntry(o, i)) { |
151 | 0 | _ (AddSafePointValue(o, point, i)); |
152 | 0 | } |
153 | 0 | } |
154 | | |
155 | 0 | if (i_loop) { |
156 | 0 | u16 numParams = GetFuncTypeNumParams(i_loop->type); |
157 | |
|
158 | 0 | for (u16 i = 0; i < numParams; ++i) { |
159 | 0 | _ (AddSafePointValue(o, point, (u16)(i_loop->exitStackIndex + i))); |
160 | 0 | } |
161 | 0 | } |
162 | | |
163 | 0 | map->numSafePoints++; |
164 | |
|
165 | 0 | } _catch: return result; |
166 | 0 | } |
167 | | |
168 | | // A back edge: op_ContinueLoop, or op_ContinueLoopIf when i_isConditional, and |
169 | | // the loop it goes back to. The conditional one suspends before it has taken |
170 | | // the branch, so it has to be resumed with the condition that took it. |
171 | | static |
172 | | M3Result RecordBackEdge (IM3Compilation o, IM3CompilationScope i_loop, bool i_isConditional) |
173 | 1.05k | { |
174 | 1.05k | M3Result result = m3Err_none; |
175 | | |
176 | | // the loop is named by its record in the map, which has to fit the field |
177 | 1.05k | _throwif("a function has too many loops for its snapshot map", |
178 | 1.05k | o->snapshotMap and o->page and (i_loop->snapshotBlock == 0 or i_loop->snapshotBlock > UINT16_MAX + 1u)); |
179 | | |
180 | 1.05k | _ (RecordSafePoint(o, safepoint_op, 2, 0, (u16)(i_loop->snapshotBlock - 1), i_loop)); |
181 | | |
182 | 1.05k | if (i_isConditional and o->snapshotMap and o->page) { |
183 | 0 | o->snapshotMap->safePoints[o->snapshotMap->numSafePoints - 1].flags |= d_m3SafePointTakenBranch; |
184 | 0 | } |
185 | | |
186 | 1.05k | _catch: return result; |
187 | 1.05k | } |
188 | | |
189 | | // A call, waiting on its callee with i_numResults not yet written; the |
190 | | // callee's frame starts i_calleeSlot slots above this one. A return_call that |
191 | | // is compiled as a call is one too, since its frame stands while the callee |
192 | | // runs, but it is marked: in Wasm the callee has replaced it, so a snapshot |
193 | | // walks past it rather than writing it. |
194 | | static |
195 | | M3Result RecordCall (IM3Compilation o, u16 i_numResults, u16 i_calleeSlot, bool i_isReturnCall) |
196 | 11.1k | { |
197 | 11.1k | M3Result result = RecordSafePoint(o, safepoint_call, 0, i_numResults, i_calleeSlot, NULL); |
198 | | |
199 | 11.1k | if (not result and i_isReturnCall and o->snapshotMap and o->page) { |
200 | 0 | o->snapshotMap->safePoints[o->snapshotMap->numSafePoints - 1].flags |= d_m3SafePointTailCall; |
201 | 0 | } |
202 | | |
203 | 11.1k | return result; |
204 | 11.1k | } |
205 | | |
206 | | // Where the function's arguments and locals are kept. Unlike the operand |
207 | | // stack, that never changes, so it is recorded once rather than at every |
208 | | // safepoint. |
209 | | static |
210 | | M3Result RecordLocals (IM3Compilation o) |
211 | 31.0k | { |
212 | 31.0k | M3SnapshotMap* map = o->snapshotMap; |
213 | | |
214 | 31.0k | if (map and o->stackIndex) { |
215 | 0 | map->locals = m3_AllocArray(M3SlotValue, o->stackIndex); |
216 | |
|
217 | 0 | if (not map->locals) { |
218 | 0 | return m3Err_mallocFailed; |
219 | 0 | } |
220 | | |
221 | 0 | for (u16 i = 0; i < o->stackIndex; ++i) { |
222 | 0 | map->locals[i].slot = o->wasmStack[i]; |
223 | 0 | map->locals[i].type = o->typeStack[i]; |
224 | 0 | } |
225 | |
|
226 | 0 | map->numLocals = o->stackIndex; |
227 | 0 | for (u32 i = GetFunctionNumArgs(o->function); i < map->numLocals; ++i) { |
228 | 0 | if (IsRefType(map->locals[i].type) and not IsNullableRef(map->locals[i].type)) { |
229 | 0 | o->localInitDepth = m3_AllocArray(u32, map->numLocals); |
230 | 0 | if (not o->localInitDepth) { |
231 | 0 | return m3Err_mallocFailed; |
232 | 0 | } |
233 | 0 | for (u32 j = i; j < map->numLocals; ++j) { |
234 | 0 | if (IsRefType(map->locals[j].type) and not IsNullableRef(map->locals[j].type)) { |
235 | 0 | o->localInitDepth[j] = UINT32_MAX; |
236 | 0 | } |
237 | 0 | } |
238 | 0 | break; |
239 | 0 | } |
240 | 0 | } |
241 | 0 | } |
242 | | |
243 | 31.0k | return m3Err_none; |
244 | 31.0k | } |
245 | | |
246 | | // A loop or a try_table, whose native frame a snapshot names by the |
247 | | // instruction's offset. i_pc is what that frame records. The scope being |
248 | | // compiled keeps the record, so that RecordBlockEnd can close it. |
249 | | static |
250 | | M3Result RecordBlockStart (IM3Compilation o, m3opcode_t i_opcode, pc_t i_pc, u32 i_numClauses) |
251 | 1.94k | { |
252 | 1.94k | M3SnapshotMap* map = o->snapshotMap; |
253 | | |
254 | 1.94k | if (map and o->page) { |
255 | 0 | d_m3GrowSnapshotArray(M3BlockStart, map->blocks, map->numBlocks, o->blocksCapacity); |
256 | |
|
257 | 0 | M3BlockStart* block = &map->blocks[map->numBlocks++]; |
258 | 0 | block->pc = i_pc; |
259 | 0 | block->wasmOffset = GetWasmOffset(o); |
260 | 0 | block->wasmEnd = UINT32_MAX; |
261 | 0 | block->numClauses = (u16)i_numClauses; |
262 | 0 | block->opcode = (u8)i_opcode; |
263 | |
|
264 | 0 | o->block.snapshotBlock = map->numBlocks; |
265 | 0 | } |
266 | | |
267 | 1.94k | return m3Err_none; |
268 | 1.94k | } |
269 | | |
270 | | // Closes the scope's region, while the instruction just read is its end |
271 | | static |
272 | | void RecordBlockEnd (IM3Compilation o) |
273 | 7.70k | { |
274 | 7.70k | if (o->snapshotMap and o->block.snapshotBlock) { |
275 | 0 | o->snapshotMap->blocks[o->block.snapshotBlock - 1].wasmEnd = GetWasmOffset(o); |
276 | 0 | } |
277 | 7.70k | } |
278 | | |
279 | | # undef d_m3GrowSnapshotArray |
280 | | |
281 | | #else |
282 | | |
283 | | static inline |
284 | | M3Result RecordSafePoint (IM3Compilation o, M3SafePointKind i_kind, u32 i_wordsBack, u16 i_numResults, |
285 | | u16 i_aux, IM3CompilationScope i_loop) |
286 | | { |
287 | | (void)o; |
288 | | (void)i_kind; |
289 | | (void)i_wordsBack; |
290 | | (void)i_numResults; |
291 | | (void)i_aux; |
292 | | (void)i_loop; |
293 | | return m3Err_none; |
294 | | } |
295 | | |
296 | | static inline |
297 | | M3Result RecordBackEdge (IM3Compilation o, IM3CompilationScope i_loop, bool i_isConditional) |
298 | | { |
299 | | (void)o; |
300 | | (void)i_loop; |
301 | | (void)i_isConditional; |
302 | | return m3Err_none; |
303 | | } |
304 | | |
305 | | static inline |
306 | | M3Result RecordCall (IM3Compilation o, u16 i_numResults, u16 i_calleeSlot, bool i_isReturnCall) |
307 | | { |
308 | | (void)o; |
309 | | (void)i_numResults; |
310 | | (void)i_calleeSlot; |
311 | | (void)i_isReturnCall; |
312 | | return m3Err_none; |
313 | | } |
314 | | |
315 | | #endif // d_m3HasSnapshots |
316 | | |
317 | | static M3_NOINLINE |
318 | | M3Result EnsureCodePageNumLines (IM3Compilation o, u32 i_numLines) |
319 | 439k | { |
320 | 439k | M3Result result = m3Err_none; |
321 | | |
322 | 439k | i_numLines += 2; // room for Bridge |
323 | | |
324 | 439k | if (M3_UNLIKELY(NumFreeLines(o->page) < i_numLines)) { |
325 | 6 | IM3CodePage page = AcquireCodePageWithCapacity(o->runtime, i_numLines); |
326 | | |
327 | 6 | if (page) { |
328 | 6 | m3log(emit, "bridging new code page from: %d %p (free slots: %d) to: %d", o->page->info.sequence, GetPC(o), NumFreeLines(o->page), page->info.sequence); |
329 | 6 | d_m3Assert(NumFreeLines(o->page) >= 2); |
330 | | |
331 | 6 | EmitWord(o->page, op_Branch); |
332 | 6 | EmitWord(o->page, GetPagePC(page)); |
333 | | |
334 | 6 | ReleaseCodePage(o->runtime, o->page); |
335 | | |
336 | 6 | o->page = page; |
337 | 6 | } else { |
338 | 0 | result = m3Err_mallocFailedCodePage; |
339 | 0 | } |
340 | 6 | } |
341 | | |
342 | 439k | return result; |
343 | 439k | } |
344 | | |
345 | | // invalidate the pending local.set fold candidate; called wherever code is emitted or a |
346 | | // branch target captures the current pc (a fold appends an immediate, moving that position) |
347 | | static inline |
348 | | void InvalidateFold (IM3Compilation o) |
349 | 483k | { |
350 | 483k | #if d_m3FoldSetLocal |
351 | 483k | o->foldPatchPC = NULL; |
352 | | #else |
353 | | (void)o; |
354 | | #endif |
355 | 483k | } |
356 | | |
357 | | static M3_NOINLINE |
358 | | M3Result EmitOp (IM3Compilation o, IM3Operation i_operation) |
359 | 457k | { |
360 | 457k | M3Result result = m3Err_none; d_m3Assert (i_operation or IsStackPolymorphic (o)); |
361 | | |
362 | 457k | InvalidateFold(o); |
363 | | |
364 | | // it's OK for page to be null; when compile-walking the bytecode without emitting |
365 | 457k | if (o->page) { |
366 | | #if d_m3EnableOpTracing |
367 | | if (i_operation != op_DumpStack) { |
368 | | o->numEmits++; |
369 | | } |
370 | | #endif |
371 | | |
372 | | // have execution jump to a new page if slots are critically low |
373 | 437k | result = EnsureCodePageNumLines(o, d_m3CodePageFreeLinesThreshold); |
374 | | |
375 | 437k | if (M3_LIKELY(not result)) { |
376 | 437k | if (d_m3LogEmit) { |
377 | 0 | log_emit(o, i_operation); |
378 | 0 | } |
379 | | #if d_m3RecordBacktraces |
380 | | EmitMappingEntry(o->page, (u32)(o->lastOpcodeStart - o->module->wasmStart)); |
381 | | #endif // d_m3RecordBacktraces |
382 | 437k | EmitWord(o->page, i_operation); |
383 | 437k | } |
384 | 437k | } |
385 | | |
386 | 457k | return result; |
387 | 457k | } |
388 | | |
389 | | // Push an immediate constant into the M3 codestream |
390 | | static M3_NOINLINE |
391 | | void EmitConstant32 (IM3Compilation o, const u32 i_immediate) |
392 | 96.9k | { |
393 | 96.9k | if (o->page) { m3log (emit, "const32: %ud", i_immediate); |
394 | 96.9k | EmitWord32(o->page, i_immediate); |
395 | 96.9k | } |
396 | 96.9k | } |
397 | | |
398 | | #if d_m3HasMemory64 |
399 | | // Takes two lines of the code page where a pointer is 32 bits, so whatever |
400 | | // reads it back has to step _pc by the same amount - see op_Const64. |
401 | | static M3_NOINLINE |
402 | | void EmitConstant64 (IM3Compilation o, const u64 i_immediate) |
403 | 1.65k | { |
404 | 1.65k | if (o->page) { |
405 | 1.65k | EmitWord64(o->page, i_immediate); |
406 | 1.65k | } |
407 | 1.65k | } |
408 | | #endif |
409 | | |
410 | | static M3_NOINLINE |
411 | | void EmitSlotOffset (IM3Compilation o, const i32 i_offset) |
412 | 326k | { |
413 | 326k | if (o->page) { m3log (emit, "slot: [%d]", i_offset); |
414 | 304k | EmitWord32(o->page, i_offset); |
415 | 304k | } |
416 | 326k | } |
417 | | |
418 | | static M3_NOINLINE |
419 | | pc_t EmitPointer (IM3Compilation o, const void* const i_pointer) |
420 | 70.9k | { |
421 | 70.9k | pc_t ptr = GetPagePC(o->page); |
422 | | |
423 | 70.9k | if (o->page) { m3log (emit, "ptr: %p", i_pointer); |
424 | 70.6k | EmitWord(o->page, i_pointer); |
425 | 70.6k | } |
426 | | |
427 | 70.9k | return ptr; |
428 | 70.9k | } |
429 | | |
430 | | static M3_NOINLINE |
431 | | void* ReservePointer (IM3Compilation o) |
432 | 6.50k | { |
433 | 6.50k | pc_t ptr = GetPagePC(o->page); |
434 | 6.50k | EmitPointer(o, NULL); |
435 | 6.50k | return (void*)ptr; |
436 | 6.50k | } |
437 | | |
438 | | |
439 | | //------------------------------------------------------------------------------------------------------------------------- |
440 | | |
441 | | #define d_indent " | %s" |
442 | | |
443 | | // just want less letters and numbers to stare at down the way in the compiler table |
444 | | #define i_32 c_m3Type_i32 |
445 | | #define i_64 c_m3Type_i64 |
446 | | #define f_32 c_m3Type_f32 |
447 | | #define f_64 c_m3Type_f64 |
448 | | #define none c_m3Type_none |
449 | | #define any (u8)-1 |
450 | | |
451 | | #if d_m3HasFloat |
452 | | # define FPOP(x) x |
453 | | #else |
454 | | # define FPOP(x) NULL |
455 | | #endif |
456 | | |
457 | | // These are indexed by M3ValueType, so every type up to c_m3Type_externref needs |
458 | | // an entry. A reference is one pointer-sized word, so it moves with the integer |
459 | | // operation of that width; v128 has no operations at all. |
460 | | #if M3_SIZEOF_PTR == 8 |
461 | | # define REFOP(NAME) op_##NAME##_i64 |
462 | | #else |
463 | | # define REFOP(NAME) op_##NAME##_i32 |
464 | | #endif |
465 | | |
466 | | // clang-format off |
467 | | |
468 | | static const IM3Operation c_preserveSetSlot[] = { NULL, op_PreserveSetSlot_i32, op_PreserveSetSlot_i64, |
469 | | FPOP(op_PreserveSetSlot_f32), FPOP(op_PreserveSetSlot_f64), |
470 | | NULL, REFOP(PreserveSetSlot), REFOP(PreserveSetSlot), |
471 | | REFOP(PreserveSetSlot), REFOP(PreserveSetSlot) }; |
472 | | static const IM3Operation c_setSetOps[] = { NULL, op_SetSlot_i32, op_SetSlot_i64, |
473 | | FPOP(op_SetSlot_f32), FPOP(op_SetSlot_f64), |
474 | | NULL, REFOP(SetSlot), REFOP(SetSlot), |
475 | | REFOP(SetSlot), REFOP(SetSlot) }; |
476 | | static const IM3Operation c_setGlobalOps[] = { NULL, op_SetGlobal_i32, op_SetGlobal_i64, |
477 | | FPOP(op_SetGlobal_f32), FPOP(op_SetGlobal_f64), |
478 | | NULL, REFOP(SetGlobal), REFOP(SetGlobal), |
479 | | REFOP(SetGlobal), REFOP(SetGlobal) }; |
480 | | static const IM3Operation c_setRegisterOps[] = { NULL, op_SetRegister_i32, op_SetRegister_i64, |
481 | | FPOP(op_SetRegister_f32), FPOP(op_SetRegister_f64), |
482 | | NULL, REFOP(SetRegister), REFOP(SetRegister), |
483 | | REFOP(SetRegister), REFOP(SetRegister) }; |
484 | | |
485 | | // A table shorter than the enum reads out of bounds, so tie the two together at |
486 | | // build time: adding a value type must not silently outgrow these. |
487 | | #define d_m3CheckTypeTable(TABLE) \ |
488 | | M3_STATIC_ASSERT (sizeof (TABLE) / sizeof (*(TABLE)) == c_m3Type_count, TABLE##_needs_one_entry_per_type) |
489 | | |
490 | | d_m3CheckTypeTable(c_preserveSetSlot); |
491 | | d_m3CheckTypeTable(c_setSetOps); |
492 | | d_m3CheckTypeTable(c_setGlobalOps); |
493 | | d_m3CheckTypeTable(c_setRegisterOps); |
494 | | |
495 | | #if d_m3FoldSetLocal |
496 | | |
497 | | // destination-folded variants of the hot binops, indexed by the operand-form index |
498 | | // recorded at emission: [0]=_rs [1]=_sr [2]=_ss [3]=(fp _rr, never folded) |
499 | | #define d_foldOpList(TYPE, NAME) { op_##TYPE##_##NAME##_rs_f, op_##TYPE##_##NAME##_sr_f, op_##TYPE##_##NAME##_ss_f, NULL } |
500 | | #define d_foldCommutativeOpList(TYPE, NAME) { op_##TYPE##_##NAME##_rs_f, NULL, op_##TYPE##_##NAME##_ss_f, NULL } |
501 | | |
502 | | static const IM3Operation c_fold_i32_Add[] = d_foldCommutativeOpList (i32, Add); |
503 | | static const IM3Operation c_fold_i32_Subtract[] = d_foldOpList (i32, Subtract); |
504 | | static const IM3Operation c_fold_i32_Multiply[] = d_foldCommutativeOpList (i32, Multiply); |
505 | | static const IM3Operation c_fold_u32_And[] = d_foldCommutativeOpList (u32, And); |
506 | | static const IM3Operation c_fold_u32_Or[] = d_foldCommutativeOpList (u32, Or); |
507 | | static const IM3Operation c_fold_u32_Xor[] = d_foldCommutativeOpList (u32, Xor); |
508 | | static const IM3Operation c_fold_u32_ShiftLeft[] = d_foldOpList (u32, ShiftLeft); |
509 | | static const IM3Operation c_fold_i32_ShiftRight[] = d_foldOpList (i32, ShiftRight); |
510 | | static const IM3Operation c_fold_u32_ShiftRight[] = d_foldOpList (u32, ShiftRight); |
511 | | |
512 | | static const IM3Operation c_fold_i64_Add[] = d_foldCommutativeOpList (i64, Add); |
513 | | static const IM3Operation c_fold_i64_Subtract[] = d_foldOpList (i64, Subtract); |
514 | | static const IM3Operation c_fold_i64_Multiply[] = d_foldCommutativeOpList (i64, Multiply); |
515 | | static const IM3Operation c_fold_u64_And[] = d_foldCommutativeOpList (u64, And); |
516 | | static const IM3Operation c_fold_u64_Or[] = d_foldCommutativeOpList (u64, Or); |
517 | | static const IM3Operation c_fold_u64_Xor[] = d_foldCommutativeOpList (u64, Xor); |
518 | | static const IM3Operation c_fold_u64_ShiftLeft[] = d_foldOpList (u64, ShiftLeft); |
519 | | static const IM3Operation c_fold_i64_ShiftRight[] = d_foldOpList (i64, ShiftRight); |
520 | | static const IM3Operation c_fold_u64_ShiftRight[] = d_foldOpList (u64, ShiftRight); |
521 | | |
522 | | # if d_m3HasFloat |
523 | | static const IM3Operation c_fold_f32_Add[] = d_foldCommutativeOpList (f32, Add); |
524 | | static const IM3Operation c_fold_f32_Subtract[] = d_foldOpList (f32, Subtract); |
525 | | static const IM3Operation c_fold_f32_Multiply[] = d_foldCommutativeOpList (f32, Multiply); |
526 | | static const IM3Operation c_fold_f32_Divide[] = d_foldOpList (f32, Divide); |
527 | | static const IM3Operation c_fold_f64_Add[] = d_foldCommutativeOpList (f64, Add); |
528 | | static const IM3Operation c_fold_f64_Subtract[] = d_foldOpList (f64, Subtract); |
529 | | static const IM3Operation c_fold_f64_Multiply[] = d_foldCommutativeOpList (f64, Multiply); |
530 | | static const IM3Operation c_fold_f64_Divide[] = d_foldOpList (f64, Divide); |
531 | | # endif |
532 | | |
533 | | // loads are unary: [0]= address in _r0, [1]= address in a slot |
534 | | #define d_foldLoadList(DEST, SRC) { op_##DEST##_Load_##SRC##_r_f, op_##DEST##_Load_##SRC##_s_f, NULL, NULL } |
535 | | |
536 | | static const IM3Operation c_fold_i32_Load_i32[] = d_foldLoadList(i32, i32); |
537 | | static const IM3Operation c_fold_i32_Load_i8[] = d_foldLoadList(i32, i8); |
538 | | static const IM3Operation c_fold_i32_Load_u8[] = d_foldLoadList(i32, u8); |
539 | | static const IM3Operation c_fold_i32_Load_i16[] = d_foldLoadList(i32, i16); |
540 | | static const IM3Operation c_fold_i32_Load_u16[] = d_foldLoadList(i32, u16); |
541 | | |
542 | | static const IM3Operation c_fold_i64_Load_i64[] = d_foldLoadList(i64, i64); |
543 | | static const IM3Operation c_fold_i64_Load_i8[] = d_foldLoadList(i64, i8); |
544 | | static const IM3Operation c_fold_i64_Load_u8[] = d_foldLoadList(i64, u8); |
545 | | static const IM3Operation c_fold_i64_Load_i16[] = d_foldLoadList(i64, i16); |
546 | | static const IM3Operation c_fold_i64_Load_u16[] = d_foldLoadList(i64, u16); |
547 | | static const IM3Operation c_fold_i64_Load_i32[] = d_foldLoadList(i64, i32); |
548 | | static const IM3Operation c_fold_i64_Load_u32[] = d_foldLoadList(i64, u32); |
549 | | # if d_m3HasFloat |
550 | | static const IM3Operation c_fold_f32_Load_f32[] = d_foldLoadList(f32, f32); |
551 | | static const IM3Operation c_fold_f64_Load_f64[] = d_foldLoadList(f64, f64); |
552 | | # endif |
553 | | |
554 | | static |
555 | | IM3Operation GetFoldOp (m3opcode_t i_opcode, u8 i_form) |
556 | 7.06k | { |
557 | 7.06k | if (i_form >= 4) { |
558 | 0 | return NULL; |
559 | 0 | } |
560 | | |
561 | 7.06k | switch (i_opcode) { |
562 | 118 | case 0x6a: return c_fold_i32_Add [i_form]; // i32.add |
563 | 186 | case 0x6b: return c_fold_i32_Subtract [i_form]; // i32.sub |
564 | 148 | case 0x6c: return c_fold_i32_Multiply [i_form]; // i32.mul |
565 | 139 | case 0x71: return c_fold_u32_And [i_form]; // i32.and |
566 | 184 | case 0x72: return c_fold_u32_Or [i_form]; // i32.or |
567 | 195 | case 0x73: return c_fold_u32_Xor [i_form]; // i32.xor |
568 | 284 | case 0x74: return c_fold_u32_ShiftLeft [i_form]; // i32.shl |
569 | 223 | case 0x75: return c_fold_i32_ShiftRight [i_form]; // i32.shr_s |
570 | 211 | case 0x76: return c_fold_u32_ShiftRight [i_form]; // i32.shr_u |
571 | | |
572 | 85 | case 0x7c: return c_fold_i64_Add [i_form]; // i64.add |
573 | 239 | case 0x7d: return c_fold_i64_Subtract [i_form]; // i64.sub |
574 | 88 | case 0x7e: return c_fold_i64_Multiply [i_form]; // i64.mul |
575 | 161 | case 0x83: return c_fold_u64_And [i_form]; // i64.and |
576 | 166 | case 0x84: return c_fold_u64_Or [i_form]; // i64.or |
577 | 75 | case 0x85: return c_fold_u64_Xor [i_form]; // i64.xor |
578 | 133 | case 0x86: return c_fold_u64_ShiftLeft [i_form]; // i64.shl |
579 | 107 | case 0x87: return c_fold_i64_ShiftRight [i_form]; // i64.shr_s |
580 | 130 | case 0x88: return c_fold_u64_ShiftRight [i_form]; // i64.shr_u |
581 | | |
582 | 0 | # if d_m3HasFloat |
583 | 93 | case 0x92: return c_fold_f32_Add [i_form]; // f32.add |
584 | 156 | case 0x93: return c_fold_f32_Subtract [i_form]; // f32.sub |
585 | 184 | case 0x94: return c_fold_f32_Multiply [i_form]; // f32.mul |
586 | 163 | case 0x95: return c_fold_f32_Divide [i_form]; // f32.div |
587 | 101 | case 0xa0: return c_fold_f64_Add [i_form]; // f64.add |
588 | 142 | case 0xa1: return c_fold_f64_Subtract [i_form]; // f64.sub |
589 | 105 | case 0xa2: return c_fold_f64_Multiply [i_form]; // f64.mul |
590 | 152 | case 0xa3: return c_fold_f64_Divide [i_form]; // f64.div |
591 | 0 | # endif |
592 | | |
593 | 127 | case 0x28: return c_fold_i32_Load_i32 [i_form]; // i32.load |
594 | 141 | case 0x2c: return c_fold_i32_Load_i8 [i_form]; // i32.load8_s |
595 | 167 | case 0x2d: return c_fold_i32_Load_u8 [i_form]; // i32.load8_u |
596 | 162 | case 0x2e: return c_fold_i32_Load_i16 [i_form]; // i32.load16_s |
597 | 135 | case 0x2f: return c_fold_i32_Load_u16 [i_form]; // i32.load16_u |
598 | | |
599 | 121 | case 0x29: return c_fold_i64_Load_i64 [i_form]; // i64.load |
600 | 173 | case 0x30: return c_fold_i64_Load_i8 [i_form]; // i64.load8_s |
601 | 182 | case 0x31: return c_fold_i64_Load_u8 [i_form]; // i64.load8_u |
602 | 173 | case 0x32: return c_fold_i64_Load_i16 [i_form]; // i64.load16_s |
603 | 204 | case 0x33: return c_fold_i64_Load_u16 [i_form]; // i64.load16_u |
604 | 284 | case 0x34: return c_fold_i64_Load_i32 [i_form]; // i64.load32_s |
605 | 192 | case 0x35: return c_fold_i64_Load_u32 [i_form]; // i64.load32_u |
606 | | |
607 | 0 | # if d_m3HasFloat |
608 | 94 | case 0x2a: return c_fold_f32_Load_f32 [i_form]; // f32.load |
609 | 62 | case 0x2b: return c_fold_f64_Load_f64 [i_form]; // f64.load |
610 | 0 | # endif |
611 | | |
612 | 875 | default: return NULL; |
613 | 7.06k | } |
614 | 7.06k | } |
615 | | |
616 | | #endif // d_m3FoldSetLocal |
617 | | |
618 | | // clang-format on |
619 | | |
620 | | #if d_m3FuseBranch |
621 | | |
622 | | // fused compare+branch/if variants, indexed like the fold lists: [0]=_rs [1]=_sr [2]=_ss [3]=unused. |
623 | | // the [1] entries are NULL for commutative compares: the compiler never records the _sr form for them |
624 | | # define d_fuseCmpList(TYPE, NAME, KIND) { op_##TYPE##_##KIND##_##NAME##_rs, op_##TYPE##_##KIND##_##NAME##_sr, op_##TYPE##_##KIND##_##NAME##_ss, NULL } |
625 | | # define d_fuseCommutativeCmpList(TYPE, NAME, KIND) { op_##TYPE##_##KIND##_##NAME##_rs, NULL, op_##TYPE##_##KIND##_##NAME##_ss, NULL } |
626 | | |
627 | | typedef struct M3FusedCmpOps { |
628 | | IM3Operation branchIf[4]; |
629 | | IM3Operation ifOp[4]; |
630 | | } M3FusedCmpOps; |
631 | | |
632 | | # define d_fuseCmp(TYPE, NAME) { d_fuseCmpList (TYPE, NAME, BranchIf), d_fuseCmpList (TYPE, NAME, If) } |
633 | | # define d_fuseCommutativeCmp(TYPE, NAME) { d_fuseCommutativeCmpList (TYPE, NAME, BranchIf), d_fuseCommutativeCmpList (TYPE, NAME, If) } |
634 | | |
635 | | // clang-format off |
636 | | static const M3FusedCmpOps c_fuse_i32_Equal = d_fuseCommutativeCmp (i32, Equal); |
637 | | static const M3FusedCmpOps c_fuse_i32_NotEqual = d_fuseCommutativeCmp (i32, NotEqual); |
638 | | static const M3FusedCmpOps c_fuse_i32_LessThan = d_fuseCmp (i32, LessThan); |
639 | | static const M3FusedCmpOps c_fuse_u32_LessThan = d_fuseCmp (u32, LessThan); |
640 | | static const M3FusedCmpOps c_fuse_i32_GreaterThan = d_fuseCmp (i32, GreaterThan); |
641 | | static const M3FusedCmpOps c_fuse_u32_GreaterThan = d_fuseCmp (u32, GreaterThan); |
642 | | static const M3FusedCmpOps c_fuse_i32_LessThanOrEqual = d_fuseCmp (i32, LessThanOrEqual); |
643 | | static const M3FusedCmpOps c_fuse_u32_LessThanOrEqual = d_fuseCmp (u32, LessThanOrEqual); |
644 | | static const M3FusedCmpOps c_fuse_i32_GreaterThanOrEqual = d_fuseCmp (i32, GreaterThanOrEqual); |
645 | | static const M3FusedCmpOps c_fuse_u32_GreaterThanOrEqual = d_fuseCmp (u32, GreaterThanOrEqual); |
646 | | // clang-format on |
647 | | |
648 | | // i32.eqz is unary: [0]= operand in _r0, [1]= operand in a slot |
649 | | static const M3FusedCmpOps c_fuse_i32_Eqz = { |
650 | | { op_i32_BranchIfEqz_r, op_i32_BranchIfEqz_s, NULL, NULL }, |
651 | | { op_i32_IfEqz_r, op_i32_IfEqz_s, NULL, NULL } |
652 | | }; |
653 | | |
654 | | static |
655 | | const M3FusedCmpOps* GetFusedCmpOps (m3opcode_t i_opcode) |
656 | 4.28k | { |
657 | 4.28k | switch (i_opcode) { |
658 | 244 | case 0x45: return &c_fuse_i32_Eqz; // i32.eqz |
659 | 232 | case 0x46: return &c_fuse_i32_Equal; // i32.eq |
660 | 281 | case 0x47: return &c_fuse_i32_NotEqual; // i32.ne |
661 | 350 | case 0x48: return &c_fuse_i32_LessThan; // i32.lt_s |
662 | 392 | case 0x49: return &c_fuse_u32_LessThan; // i32.lt_u |
663 | 335 | case 0x4a: return &c_fuse_i32_GreaterThan; // i32.gt_s |
664 | 381 | case 0x4b: return &c_fuse_u32_GreaterThan; // i32.gt_u |
665 | 374 | case 0x4c: return &c_fuse_i32_LessThanOrEqual; // i32.le_s |
666 | 386 | case 0x4d: return &c_fuse_u32_LessThanOrEqual; // i32.le_u |
667 | 413 | case 0x4e: return &c_fuse_i32_GreaterThanOrEqual; // i32.ge_s |
668 | 341 | case 0x4f: return &c_fuse_u32_GreaterThanOrEqual; // i32.ge_u |
669 | 560 | default: return NULL; |
670 | 4.28k | } |
671 | 4.28k | } |
672 | | |
673 | | #endif // d_m3FuseBranch |
674 | | |
675 | | static const IM3Operation c_intSelectOps[2][4] = { |
676 | | { op_Select_i32_rss, op_Select_i32_srs, op_Select_i32_ssr, op_Select_i32_sss }, |
677 | | { op_Select_i64_rss, op_Select_i64_srs, op_Select_i64_ssr, op_Select_i64_sss } |
678 | | }; |
679 | | #if d_m3HasFloat |
680 | | static const IM3Operation c_fpSelectOps[2][2][3] = { |
681 | | { { op_Select_f32_sss, op_Select_f32_srs, op_Select_f32_ssr }, // selector in slot |
682 | | { op_Select_f32_rss, op_Select_f32_rrs, op_Select_f32_rsr } }, // selector in reg |
683 | | { { op_Select_f64_sss, op_Select_f64_srs, op_Select_f64_ssr }, // selector in slot |
684 | | { op_Select_f64_rss, op_Select_f64_rrs, op_Select_f64_rsr } } |
685 | | }; // selector in reg |
686 | | #endif |
687 | | |
688 | | // all args & returns are 64-bit aligned, so use 2 slots for a d_m3Use32BitSlots=1 build |
689 | | |
690 | | static |
691 | | M3Result AcquireCompilationCodePage (IM3Compilation o, IM3CodePage* o_codePage) |
692 | 32.4k | { |
693 | 32.4k | M3Result result = m3Err_none; |
694 | | |
695 | 32.4k | IM3CodePage page = AcquireCodePage(o->runtime); |
696 | | |
697 | 32.4k | if (page) { |
698 | | #if (d_m3EnableCodePageRefCounting) |
699 | | { |
700 | | if (o->function) { |
701 | | IM3Function func = o->function; |
702 | | page->info.usageCount++; |
703 | | |
704 | | u32 index = func->numCodePageRefs++; |
705 | | _ (m3ReallocArray(&func->codePageRefs, IM3CodePage, func->numCodePageRefs, index)); |
706 | | func->codePageRefs[index] = page; |
707 | | } |
708 | | } |
709 | | #endif |
710 | 32.4k | } else { |
711 | 0 | _throw(m3Err_mallocFailedCodePage); |
712 | 0 | } |
713 | | |
714 | 32.4k | _catch: |
715 | | |
716 | 32.4k | *o_codePage = page; |
717 | | |
718 | 32.4k | return result; |
719 | 32.4k | } |
720 | | |
721 | | static inline |
722 | | void ReleaseCompilationCodePage (IM3Compilation o) |
723 | 32.4k | { |
724 | 32.4k | ReleaseCodePage(o->runtime, o->page); |
725 | 32.4k | } |
726 | | |
727 | | static inline |
728 | | u16 GetTypeNumSlots (m3type_t i_type) |
729 | 12.0M | { |
730 | 12.0M | i_type = BaseTypeOf(i_type); |
731 | | |
732 | | // v128 is 16 bytes - 4 slots in 32-bit-slot mode, 2 in 64-bit. |
733 | | // (Slot-allocator only; no v128 ops execute.) |
734 | 12.0M | if (i_type == c_m3Type_v128) |
735 | 705k | #if d_m3Use32BitSlots |
736 | 705k | return 4; |
737 | | #else |
738 | | return 2; |
739 | | #endif |
740 | 11.3M | #if d_m3Use32BitSlots |
741 | 11.3M | return Is64BitType(i_type) ? 2 : 1; |
742 | | #else |
743 | | return 1; |
744 | | #endif |
745 | 12.0M | } |
746 | | |
747 | | static inline |
748 | | void AlignSlotToType (u16* io_slot, m3type_t i_type) |
749 | 5.80M | { |
750 | | // align 64-bit words to even slots (if d_m3Use32BitSlots) |
751 | 5.80M | u16 numSlots = GetTypeNumSlots(i_type); |
752 | | |
753 | 5.80M | u16 mask = numSlots - 1; |
754 | 5.80M | *io_slot = (*io_slot + mask) & ~mask; |
755 | 5.80M | } |
756 | | |
757 | | // Deliberately unchecked. Validation rejects an empty operand stack before compilation. |
758 | | static inline |
759 | | i16 GetStackTopIndex (IM3Compilation o) |
760 | 824k | { |
761 | 824k | return o->stackIndex - 1; |
762 | 824k | } |
763 | | |
764 | | static inline |
765 | | M3Result GetStackTopIndexThrows (IM3Compilation o, i16* o_stackIndex) |
766 | 9.14k | { |
767 | 9.14k | *o_stackIndex = o->stackIndex - 1; |
768 | | |
769 | 9.14k | if (M3_LIKELY(o->stackIndex > o->stackFirstDynamicIndex or IsStackPolymorphic(o))) { |
770 | 9.14k | return m3Err_none; |
771 | 9.14k | } else { |
772 | 0 | return m3Err_functionStackUnderrun; |
773 | 0 | } |
774 | 9.14k | } |
775 | | |
776 | | |
777 | | // Items in the static portion of the stack (args/locals) are hidden from GetStackTypeFromTop () |
778 | | // In other words, only "real" Wasm stack items can be inspected. This is important when |
779 | | // returning values, etc. and you need an accurate wasm-view of the stack. |
780 | | static |
781 | | m3type_t GetStackTypeFromTop (IM3Compilation o, u16 i_offset) |
782 | 210k | { |
783 | 210k | m3type_t type = c_m3Type_none; |
784 | | |
785 | 210k | ++i_offset; |
786 | 210k | if (o->stackIndex >= i_offset) { |
787 | 203k | u16 index = o->stackIndex - i_offset; |
788 | | |
789 | 203k | if (index >= o->stackFirstDynamicIndex) { |
790 | 194k | type = o->typeStack[index]; |
791 | 194k | } |
792 | 203k | } |
793 | | |
794 | 210k | return type; |
795 | 210k | } |
796 | | |
797 | | static inline |
798 | | m3type_t GetStackTopType (IM3Compilation o) |
799 | 184k | { |
800 | 184k | return GetStackTypeFromTop(o, 0); |
801 | 184k | } |
802 | | |
803 | | static inline |
804 | | m3type_t GetStackTypeFromBottom (IM3Compilation o, u16 i_offset) |
805 | 124k | { |
806 | 124k | m3type_t type = c_m3Type_none; |
807 | | |
808 | 124k | if (i_offset < o->stackIndex) { |
809 | 123k | type = o->typeStack[i_offset]; |
810 | 123k | } |
811 | | |
812 | 124k | return type; |
813 | 124k | } |
814 | | |
815 | | |
816 | | static inline |
817 | | bool IsConstantSlot (IM3Compilation o, u16 i_slot) |
818 | 1.10k | { |
819 | 1.10k | return (i_slot >= o->slotFirstConstIndex and i_slot < o->slotMaxConstIndex); |
820 | 1.10k | } |
821 | | static inline |
822 | | bool IsSlotAllocated (IM3Compilation o, u16 i_slot) |
823 | 439k | { |
824 | 439k | return o->m3Slots[i_slot]; |
825 | 439k | } |
826 | | |
827 | | static inline |
828 | | bool IsStackIndexInRegister (IM3Compilation o, i32 i_stackIndex) |
829 | 530k | { d_m3Assert (i_stackIndex < o->stackIndex or IsStackPolymorphic (o)); |
830 | 530k | if (i_stackIndex >= 0 and i_stackIndex < o->stackIndex) { |
831 | 511k | return (o->wasmStack[i_stackIndex] >= d_m3Reg0SlotAlias); |
832 | 511k | } else { |
833 | 19.5k | return false; |
834 | 19.5k | } |
835 | 530k | } |
836 | | |
837 | | static inline |
838 | | u16 GetNumBlockValuesOnStack (IM3Compilation o) |
839 | 81.2k | { |
840 | 81.2k | return o->stackIndex - o->block.blockStackIndex; |
841 | 81.2k | } |
842 | | |
843 | | static inline |
844 | | bool IsStackTopInRegister (IM3Compilation o) |
845 | 426k | { |
846 | 426k | return IsStackIndexInRegister(o, (i32)GetStackTopIndex(o)); |
847 | 426k | } |
848 | | static inline |
849 | | bool IsStackTopMinus1InRegister (IM3Compilation o) |
850 | 84.5k | { |
851 | 84.5k | return IsStackIndexInRegister(o, (i32)GetStackTopIndex(o) - 1); |
852 | 84.5k | } |
853 | | static inline |
854 | | bool IsStackTopMinus2InRegister (IM3Compilation o) |
855 | 2.23k | { |
856 | 2.23k | return IsStackIndexInRegister(o, (i32)GetStackTopIndex(o) - 2); |
857 | 2.23k | } |
858 | | |
859 | | static inline |
860 | | bool IsStackTopInSlot (IM3Compilation o) |
861 | 270k | { |
862 | 270k | return not IsStackTopInRegister(o); |
863 | 270k | } |
864 | | |
865 | | static inline |
866 | | bool IsValidSlot (u16 i_slot) |
867 | 23.1k | { |
868 | 23.1k | return (i_slot < d_m3MaxFunctionSlots); |
869 | 23.1k | } |
870 | | |
871 | | static inline |
872 | | u16 GetStackTopSlotNumber (IM3Compilation o) |
873 | 186k | { |
874 | 186k | i16 i = GetStackTopIndex(o); |
875 | | |
876 | 186k | u16 slot = c_slotUnused; |
877 | | |
878 | 186k | if (i >= 0) { |
879 | 175k | slot = o->wasmStack[i]; |
880 | 175k | } |
881 | | |
882 | 186k | return slot; |
883 | 186k | } |
884 | | |
885 | | |
886 | | // from bottom |
887 | | static inline |
888 | | u16 GetSlotForStackIndex (IM3Compilation o, u16 i_stackIndex) |
889 | 2.77M | { d_m3Assert (i_stackIndex < o->stackIndex or IsStackPolymorphic (o)); |
890 | 2.77M | u16 slot = c_slotUnused; |
891 | | |
892 | 2.77M | if (i_stackIndex < o->stackIndex) { |
893 | 2.77M | slot = o->wasmStack[i_stackIndex]; |
894 | 2.77M | } |
895 | | |
896 | 2.77M | return slot; |
897 | 2.77M | } |
898 | | |
899 | | static inline |
900 | | u16 GetExtraSlotForStackIndex (IM3Compilation o, u16 i_stackIndex) |
901 | 40.4k | { |
902 | 40.4k | u16 baseSlot = GetSlotForStackIndex(o, i_stackIndex); |
903 | | |
904 | 40.4k | if (baseSlot != c_slotUnused) { |
905 | 40.4k | u16 extraSlot = GetTypeNumSlots(GetStackTypeFromBottom(o, i_stackIndex)) - 1; |
906 | 40.4k | baseSlot += extraSlot; |
907 | 40.4k | } |
908 | | |
909 | 40.4k | return baseSlot; |
910 | 40.4k | } |
911 | | |
912 | | |
913 | | static inline |
914 | | void TouchSlot (IM3Compilation o, u16 i_slot) |
915 | 10.1M | { |
916 | | // op_Entry uses this value to track and detect stack overflow |
917 | 10.1M | o->maxStackSlots = M3_MAX(o->maxStackSlots, i_slot + 1); |
918 | 10.1M | } |
919 | | |
920 | | static inline |
921 | | void MarkSlotAllocated (IM3Compilation o, u16 i_slot) |
922 | 10.1M | { d_m3Assert (o->m3Slots [i_slot] == 0); // shouldn't be already allocated |
923 | 10.1M | o->m3Slots[i_slot] = 1; |
924 | | |
925 | 10.1M | o->slotMaxAllocatedIndexPlusOne = M3_MAX(o->slotMaxAllocatedIndexPlusOne, i_slot + 1); |
926 | | |
927 | 10.1M | TouchSlot(o, i_slot); |
928 | 10.1M | } |
929 | | |
930 | | static inline |
931 | | M3Result MarkSlotsAllocated (IM3Compilation o, u16 i_slot, u16 i_numSlots) |
932 | 5.71M | { |
933 | 5.71M | if (M3_UNLIKELY(i_slot + i_numSlots > d_m3MaxFunctionSlots)) { |
934 | 3 | return m3Err_functionStackOverflow; |
935 | 3 | } |
936 | | |
937 | 15.8M | while (i_numSlots--) { |
938 | 10.1M | MarkSlotAllocated(o, i_slot++); |
939 | 10.1M | } |
940 | | |
941 | 5.71M | return m3Err_none; |
942 | 5.71M | } |
943 | | |
944 | | static inline |
945 | | M3Result MarkSlotsAllocatedByType (IM3Compilation o, u16 i_slot, m3type_t i_type) |
946 | 26.2k | { |
947 | 26.2k | u16 numSlots = GetTypeNumSlots(i_type); |
948 | 26.2k | return MarkSlotsAllocated(o, i_slot, numSlots); |
949 | 26.2k | } |
950 | | |
951 | | |
952 | | static |
953 | | M3Result AllocateSlotsWithinRange (IM3Compilation o, u16* o_slot, m3type_t i_type, u16 i_startSlot, u16 i_endSlot) |
954 | 5.70M | { |
955 | 5.70M | M3Result result = m3Err_functionStackOverflow; |
956 | | |
957 | 5.70M | u16 numSlots = GetTypeNumSlots(i_type); |
958 | 5.70M | u16 searchOffset = numSlots - 1; |
959 | | |
960 | 5.70M | AlignSlotToType(&i_startSlot, i_type); |
961 | | |
962 | | // search for 1 or 2 consecutive slots in the execution stack |
963 | 5.70M | u16 i = i_startSlot; |
964 | 12.6G | while (i + searchOffset < i_endSlot) { |
965 | 12.6G | if (i + searchOffset < d_m3MaxFunctionSlots and o->m3Slots[i] == 0 and o->m3Slots[i + searchOffset] == 0) { |
966 | 5.68M | MarkSlotsAllocated(o, i, numSlots); |
967 | | |
968 | 5.68M | *o_slot = i; |
969 | 5.68M | result = m3Err_none; |
970 | 5.68M | break; |
971 | 5.68M | } |
972 | | |
973 | | // keep 2-slot allocations even-aligned |
974 | 12.6G | i += numSlots; |
975 | 12.6G | } |
976 | | |
977 | 5.70M | return result; |
978 | 5.70M | } |
979 | | |
980 | | static inline |
981 | | M3Result AllocateSlots (IM3Compilation o, u16* o_slot, m3type_t i_type) |
982 | 5.57M | { |
983 | 5.57M | return AllocateSlotsWithinRange(o, o_slot, i_type, o->slotFirstDynamicIndex, d_m3MaxFunctionSlots); |
984 | 5.57M | } |
985 | | |
986 | | static inline |
987 | | M3Result AllocateConstantSlots (IM3Compilation o, u16* o_slot, m3type_t i_type) |
988 | 132k | { |
989 | 132k | u16 maxTableIndex = o->slotFirstConstIndex + d_m3MaxConstantTableSize; |
990 | 132k | return AllocateSlotsWithinRange(o, o_slot, i_type, o->slotFirstConstIndex, M3_MIN(o->slotFirstDynamicIndex, maxTableIndex)); |
991 | 132k | } |
992 | | |
993 | | |
994 | | // TOQUE: this usage count system could be eliminated. real world code doesn't frequently trigger it. just copy to multiple |
995 | | // unique slots. |
996 | | static inline |
997 | | M3Result IncrementSlotUsageCount (IM3Compilation o, u16 i_slot) |
998 | 1.21k | { d_m3Assert (i_slot < d_m3MaxFunctionSlots); |
999 | 1.21k | M3Result result = m3Err_none; d_m3Assert (o->m3Slots [i_slot] > 0); |
1000 | | |
1001 | | // OPTZ (memory): 'm3Slots' could still be fused with 'typeStack' if 4 bits were used to indicate: [0,1,2,many]. The many-case |
1002 | | // would scan 'wasmStack' to determine the actual usage count |
1003 | 1.21k | if (M3_LIKELY(o->m3Slots[i_slot] < 0xFF)) { |
1004 | 1.20k | o->m3Slots[i_slot]++; |
1005 | 1.20k | } else { |
1006 | 4 | result = "slot usage count overflow"; |
1007 | 4 | } |
1008 | | |
1009 | 1.21k | return result; |
1010 | 1.21k | } |
1011 | | |
1012 | | static inline |
1013 | | void DeallocateSlot (IM3Compilation o, i16 i_slot, m3type_t i_type) |
1014 | 99.3k | { d_m3Assert (i_slot >= o->slotFirstDynamicIndex); |
1015 | 99.3k | d_m3Assert (i_slot < o->slotMaxAllocatedIndexPlusOne); |
1016 | 278k | for (u16 i = 0; i < GetTypeNumSlots(i_type); ++i, ++i_slot) { d_m3Assert (o->m3Slots [i_slot]); |
1017 | 178k | --o->m3Slots[i_slot]; |
1018 | 178k | } |
1019 | 99.3k | } |
1020 | | |
1021 | | |
1022 | | static inline |
1023 | | bool IsRegisterTypeAllocated (IM3Compilation o, u8 i_type) |
1024 | 25.5k | { |
1025 | 25.5k | return IsRegisterAllocated(o, IsFpType(i_type)); |
1026 | 25.5k | } |
1027 | | |
1028 | | static inline |
1029 | | void AllocateRegister (IM3Compilation o, u32 i_register, u16 i_stackIndex) |
1030 | 152k | { d_m3Assert (not IsRegisterAllocated (o, i_register)); |
1031 | 152k | o->regStackIndexPlusOne[i_register] = i_stackIndex + 1; |
1032 | 152k | } |
1033 | | |
1034 | | static inline |
1035 | | void DeallocateRegister (IM3Compilation o, u32 i_register) |
1036 | 151k | { d_m3Assert (IsRegisterAllocated (o, i_register)); |
1037 | 151k | o->regStackIndexPlusOne[i_register] = c_m3RegisterUnallocated; |
1038 | 151k | } |
1039 | | |
1040 | | static inline |
1041 | | u16 GetRegisterStackIndex (IM3Compilation o, u32 i_register) |
1042 | 28.7k | { d_m3Assert (IsRegisterAllocated (o, i_register)); |
1043 | 28.7k | return o->regStackIndexPlusOne[i_register] - 1; |
1044 | 28.7k | } |
1045 | | |
1046 | | u16 GetMaxUsedSlotPlusOne (IM3Compilation o) |
1047 | 55.1k | { |
1048 | 77.5k | while (o->slotMaxAllocatedIndexPlusOne > o->slotFirstDynamicIndex) { |
1049 | 55.5k | if (IsSlotAllocated(o, o->slotMaxAllocatedIndexPlusOne - 1)) { |
1050 | 33.1k | break; |
1051 | 33.1k | } |
1052 | | |
1053 | 22.4k | o->slotMaxAllocatedIndexPlusOne--; |
1054 | 22.4k | } |
1055 | | |
1056 | | #ifdef DEBUG |
1057 | | u16 maxSlot = o->slotMaxAllocatedIndexPlusOne; |
1058 | | while (maxSlot < d_m3MaxFunctionSlots) { |
1059 | | d_m3Assert(o->m3Slots[maxSlot] == 0); |
1060 | | maxSlot++; |
1061 | | } |
1062 | | #endif |
1063 | | |
1064 | 55.1k | return o->slotMaxAllocatedIndexPlusOne; |
1065 | 55.1k | } |
1066 | | |
1067 | | static |
1068 | | M3Result PreserveRegisterIfOccupied (IM3Compilation o, m3type_t i_registerType) |
1069 | 85.5k | { |
1070 | 85.5k | M3Result result = m3Err_none; |
1071 | | |
1072 | 85.5k | u32 regSelect = IsFpType(i_registerType); |
1073 | | |
1074 | 85.5k | if (IsRegisterAllocated(o, regSelect)) { |
1075 | 24.8k | u16 stackIndex = GetRegisterStackIndex(o, regSelect); |
1076 | 24.8k | DeallocateRegister(o, regSelect); |
1077 | | |
1078 | 24.8k | m3type_t type = GetStackTypeFromBottom(o, stackIndex); |
1079 | | |
1080 | | // and point to a exec slot |
1081 | 24.8k | u16 slot = c_slotUnused; |
1082 | 24.8k | _ (AllocateSlots(o, &slot, type)); |
1083 | 24.8k | o->wasmStack[stackIndex] = slot; |
1084 | | |
1085 | | // Ensure type is within the valid range |
1086 | 24.8k | if (M3_LIKELY(BaseTypeOf(type) < c_m3Type_count)) { |
1087 | 24.8k | _ (EmitOp(o, c_setSetOps[BaseTypeOf(type)])); |
1088 | 24.8k | } else { |
1089 | 0 | _throw(m3Err_unknownType); |
1090 | 0 | } |
1091 | | |
1092 | 24.8k | EmitSlotOffset(o, slot); |
1093 | 24.8k | } |
1094 | | |
1095 | 85.5k | _catch: return result; |
1096 | 85.5k | } |
1097 | | |
1098 | | |
1099 | | // all values must be in slots before entering loop, if, and else blocks |
1100 | | // otherwise they'd end up preserve-copied in the block to probably different locations (if/else) |
1101 | | static inline |
1102 | | M3Result PreserveRegisters (IM3Compilation o) |
1103 | 3.38k | { |
1104 | 3.38k | M3Result result; |
1105 | | |
1106 | 3.38k | _ (PreserveRegisterIfOccupied(o, c_m3Type_f64)); |
1107 | 3.38k | _ (PreserveRegisterIfOccupied(o, c_m3Type_i64)); |
1108 | | |
1109 | 3.38k | _catch: return result; |
1110 | 3.37k | } |
1111 | | |
1112 | | static |
1113 | | M3Result PreserveNonTopRegisters (IM3Compilation o) |
1114 | 5.11k | { |
1115 | 5.11k | M3Result result = m3Err_none; |
1116 | | |
1117 | 5.11k | i16 stackTop = GetStackTopIndex(o); |
1118 | | |
1119 | 5.11k | if (stackTop >= 0) { |
1120 | 4.95k | if (IsRegisterAllocated(o, 0)) // r0 |
1121 | 3.65k | { |
1122 | 3.65k | if (GetRegisterStackIndex(o, 0) != stackTop) { |
1123 | 159 | _ (PreserveRegisterIfOccupied(o, c_m3Type_i64)); |
1124 | 158 | } |
1125 | 3.65k | } |
1126 | | |
1127 | 4.95k | if (IsRegisterAllocated(o, 1)) // fp0 |
1128 | 262 | { |
1129 | 262 | if (GetRegisterStackIndex(o, 1) != stackTop) { |
1130 | 252 | _ (PreserveRegisterIfOccupied(o, c_m3Type_f64)); |
1131 | 251 | } |
1132 | 262 | } |
1133 | 4.95k | } |
1134 | | |
1135 | 5.11k | _catch: return result; |
1136 | 5.11k | } |
1137 | | |
1138 | | |
1139 | | //---------------------------------------------------------------------------------------------------------------------- |
1140 | | |
1141 | | static |
1142 | | M3Result Push (IM3Compilation o, m3type_t i_type, u16 i_slot) |
1143 | 5.86M | { |
1144 | 5.86M | M3Result result = m3Err_none; |
1145 | | |
1146 | | #if !d_m3HasFloat |
1147 | | if (i_type == c_m3Type_f32 || i_type == c_m3Type_f64) { |
1148 | | return m3Err_unknownOpcode; |
1149 | | } |
1150 | | #endif |
1151 | | |
1152 | 5.86M | if (M3_UNLIKELY(o->stackIndex >= d_m3MaxFunctionStackHeight)) { |
1153 | 559 | return m3Err_functionStackOverflow; |
1154 | 559 | } |
1155 | | |
1156 | 5.86M | u16 stackIndex = o->stackIndex++; // printf ("push: %d\n", (i32) i); |
1157 | | |
1158 | 5.86M | o->wasmStack[stackIndex] = i_slot; |
1159 | 5.86M | o->typeStack[stackIndex] = i_type; |
1160 | | |
1161 | 5.86M | if (IsRegisterSlotAlias(i_slot)) { |
1162 | 152k | u32 regSelect = IsFpRegisterSlotAlias(i_slot); |
1163 | 152k | AllocateRegister(o, regSelect, stackIndex); |
1164 | 152k | } |
1165 | | |
1166 | 5.86M | if (d_m3LogWasmStack) { |
1167 | 0 | dump_type_stack(o); |
1168 | 0 | } |
1169 | | |
1170 | 5.86M | return result; |
1171 | 5.86M | } |
1172 | | |
1173 | | static inline |
1174 | | M3Result PushRegister (IM3Compilation o, m3type_t i_type) |
1175 | 152k | { |
1176 | 152k | M3Result result = m3Err_none; d_m3Assert ((u16) d_m3Reg0SlotAlias > (u16) d_m3MaxFunctionSlots); |
1177 | 152k | u16 slot = IsFpType(i_type) ? d_m3Fp0SlotAlias : d_m3Reg0SlotAlias; d_m3Assert (i_type or IsStackPolymorphic (o)); |
1178 | | |
1179 | 152k | _ (Push(o, i_type, slot)); |
1180 | | |
1181 | 152k | _catch: return result; |
1182 | 152k | } |
1183 | | |
1184 | | static |
1185 | | M3Result Pop (IM3Compilation o) |
1186 | 403k | { |
1187 | 403k | M3Result result = m3Err_none; |
1188 | | |
1189 | 403k | if (M3_LIKELY(o->stackIndex > o->block.blockStackIndex)) { |
1190 | 363k | o->stackIndex--; // printf ("pop: %d\n", (i32) o->stackIndex); |
1191 | | |
1192 | 363k | u16 slot = o->wasmStack[o->stackIndex]; |
1193 | 363k | m3type_t type = o->typeStack[o->stackIndex]; |
1194 | | |
1195 | 363k | if (IsRegisterSlotAlias(slot)) { |
1196 | 126k | u32 regSelect = IsFpRegisterSlotAlias(slot); |
1197 | 126k | DeallocateRegister(o, regSelect); |
1198 | 236k | } else if (M3_UNLIKELY(slot >= o->slotMaxAllocatedIndexPlusOne)) { |
1199 | 117 | return m3Err_functionStackUnderrun; // Return error for invalid slot indices |
1200 | 236k | } else if (slot >= o->slotFirstDynamicIndex) { |
1201 | 98.3k | DeallocateSlot(o, slot, type); |
1202 | 98.3k | } |
1203 | 363k | } else if (not IsStackPolymorphic(o)) { |
1204 | 39 | result = m3Err_functionStackUnderrun; |
1205 | 39 | } |
1206 | | |
1207 | 403k | return result; |
1208 | 403k | } |
1209 | | |
1210 | | static |
1211 | | M3Result PopType (IM3Compilation o, m3type_t i_type) |
1212 | 11.1k | { |
1213 | 11.1k | M3Result result = m3Err_none; |
1214 | | |
1215 | 11.1k | m3type_t topType = GetStackTopType(o); |
1216 | | |
1217 | 11.1k | if (M3_LIKELY(IsSubTypeOf(topType, i_type) or o->block.isPolymorphic)) { |
1218 | 11.1k | _ (Pop(o)); |
1219 | 11.1k | } else { |
1220 | 1 | _throw(m3Err_typeMismatch); |
1221 | 0 | } |
1222 | | |
1223 | 11.1k | _catch: |
1224 | 11.1k | return result; |
1225 | 11.1k | } |
1226 | | |
1227 | | static |
1228 | | M3Result _PushAllocatedSlotAndEmit (IM3Compilation o, m3type_t i_type, bool i_doEmit) |
1229 | 5.54M | { |
1230 | 5.54M | M3Result result = m3Err_none; |
1231 | | |
1232 | 5.54M | u16 slot = c_slotUnused; |
1233 | | |
1234 | 5.54M | _ (AllocateSlots(o, &slot, i_type)); |
1235 | 5.54M | _ (Push(o, i_type, slot)); |
1236 | | |
1237 | 5.54M | if (i_doEmit) { |
1238 | 31.4k | EmitSlotOffset(o, slot); |
1239 | 31.4k | } |
1240 | | |
1241 | | // printf ("push: %d\n", (u32) slot); |
1242 | | |
1243 | 5.54M | _catch: return result; |
1244 | 5.54M | } |
1245 | | |
1246 | | static inline |
1247 | | M3Result PushAllocatedSlotAndEmit (IM3Compilation o, m3type_t i_type) |
1248 | 31.4k | { |
1249 | 31.4k | return _PushAllocatedSlotAndEmit(o, i_type, true); |
1250 | 31.4k | } |
1251 | | |
1252 | | static inline |
1253 | | M3Result PushAllocatedSlot (IM3Compilation o, m3type_t i_type) |
1254 | 5.51M | { |
1255 | 5.51M | return _PushAllocatedSlotAndEmit(o, i_type, false); |
1256 | 5.51M | } |
1257 | | |
1258 | | static |
1259 | | M3Result PushConst (IM3Compilation o, u64 i_word, m3type_t i_type) |
1260 | 184k | { |
1261 | 184k | M3Result result = m3Err_none; |
1262 | | |
1263 | | // When compile-walking a constant expression without emitting there is no |
1264 | | // constant table to place the value in, but the type still has to land on |
1265 | | // the stack: extended-const arithmetic downstream consumes it. |
1266 | 184k | if (M3_UNLIKELY(!o->page)) { |
1267 | 45.5k | return PushAllocatedSlot(o, i_type); |
1268 | 45.5k | } |
1269 | | |
1270 | 184k | bool matchFound = false; |
1271 | 139k | bool is64BitType = Is64BitType(i_type); |
1272 | | |
1273 | 139k | u16 numRequiredSlots = GetTypeNumSlots(i_type); |
1274 | 139k | u16 numUsedConstSlots = o->slotMaxConstIndex - o->slotFirstConstIndex; |
1275 | | |
1276 | | // search for duplicate matching constant slot to reuse |
1277 | 139k | if (numRequiredSlots == 2 and numUsedConstSlots >= 2) { |
1278 | 38.9k | u16 firstConstSlot = o->slotFirstConstIndex; |
1279 | 38.9k | AlignSlotToType(&firstConstSlot, c_m3Type_i64); |
1280 | | |
1281 | 127k | for (u16 slot = firstConstSlot; slot < o->slotMaxConstIndex - 1; slot += 2) { |
1282 | 92.3k | if (IsSlotAllocated(o, slot) and IsSlotAllocated(o, slot + 1)) { |
1283 | 89.6k | u64 constant; |
1284 | 89.6k | memcpy(&constant, &o->constants[slot - o->slotFirstConstIndex], sizeof(constant)); |
1285 | | |
1286 | 89.6k | if (constant == i_word) { |
1287 | 3.50k | matchFound = true; |
1288 | 3.50k | _ (Push(o, i_type, slot)); |
1289 | 3.50k | break; |
1290 | 3.50k | } |
1291 | 89.6k | } |
1292 | 92.3k | } |
1293 | 100k | } else if (numRequiredSlots == 1) { |
1294 | 269k | for (u16 i = 0; i < numUsedConstSlots; ++i) { |
1295 | 198k | u16 slot = o->slotFirstConstIndex + i; |
1296 | | |
1297 | 198k | if (IsSlotAllocated(o, slot)) { |
1298 | 196k | bool matches; |
1299 | 196k | if (is64BitType) { |
1300 | 0 | u64 constant; |
1301 | 0 | memcpy(&constant, &o->constants[i], sizeof(constant)); |
1302 | 0 | matches = (constant == i_word); |
1303 | 196k | } else { |
1304 | 196k | u32 constant; |
1305 | 196k | memcpy(&constant, &o->constants[i], sizeof(constant)); |
1306 | 196k | matches = (constant == i_word); |
1307 | 196k | } |
1308 | 196k | if (matches) { |
1309 | 2.70k | matchFound = true; |
1310 | 2.70k | _ (Push(o, i_type, slot)); |
1311 | 2.70k | break; |
1312 | 2.70k | } |
1313 | 196k | } |
1314 | 198k | } |
1315 | 73.7k | } |
1316 | | |
1317 | 139k | if (not matchFound) { |
1318 | 132k | u16 slot = c_slotUnused; |
1319 | 132k | result = AllocateConstantSlots(o, &slot, i_type); |
1320 | | |
1321 | 132k | if (result || slot == c_slotUnused) // no more constant table space; use inline constants |
1322 | 20.0k | { |
1323 | 20.0k | result = m3Err_none; |
1324 | | |
1325 | 20.0k | if (is64BitType) { |
1326 | 12.9k | _ (EmitOp(o, op_Const64)); |
1327 | 12.9k | EmitWord64(o->page, i_word); |
1328 | 12.9k | } else { |
1329 | 7.11k | _ (EmitOp(o, op_Const32)); |
1330 | 7.11k | EmitWord32(o->page, (u32)i_word); |
1331 | 7.11k | } |
1332 | | |
1333 | 20.0k | _ (PushAllocatedSlotAndEmit(o, i_type)); |
1334 | 112k | } else { |
1335 | 112k | u16 constTableIndex = slot - o->slotFirstConstIndex; |
1336 | | |
1337 | 112k | d_m3Assert(constTableIndex < d_m3MaxConstantTableSize); |
1338 | | |
1339 | 112k | if (is64BitType) { |
1340 | 49.0k | memcpy(&o->constants[constTableIndex], &i_word, sizeof(i_word)); |
1341 | 63.8k | } else { |
1342 | 63.8k | u32 word32 = (u32)i_word; |
1343 | 63.8k | memcpy(&o->constants[constTableIndex], &word32, sizeof(word32)); |
1344 | 63.8k | } |
1345 | | |
1346 | 112k | _ (Push(o, i_type, slot)); |
1347 | | |
1348 | 112k | o->slotMaxConstIndex = M3_MAX(slot + numRequiredSlots, o->slotMaxConstIndex); |
1349 | 112k | } |
1350 | 132k | } |
1351 | | |
1352 | 139k | _catch: return result; |
1353 | 139k | } |
1354 | | |
1355 | | static inline |
1356 | | M3Result EmitSlotNumOfStackTopAndPop (IM3Compilation o) |
1357 | 239k | { |
1358 | | // no emit if value is in register |
1359 | 239k | if (IsStackTopInSlot(o)) { |
1360 | 168k | EmitSlotOffset(o, GetStackTopSlotNumber(o)); |
1361 | 168k | } |
1362 | | |
1363 | 239k | return Pop(o); |
1364 | 239k | } |
1365 | | |
1366 | | |
1367 | | // Or, maybe: EmitTrappingOp |
1368 | | M3Result AddTrapRecord (IM3Compilation o) |
1369 | 58.9k | { |
1370 | 58.9k | M3Result result = m3Err_none; |
1371 | | |
1372 | 58.9k | if (o->function) { |
1373 | 58.9k | } |
1374 | | |
1375 | 58.9k | return result; |
1376 | 58.9k | } |
1377 | | |
1378 | | static |
1379 | | M3Result UnwindBlockStack (IM3Compilation o) |
1380 | 83.4k | { |
1381 | 83.4k | M3Result result = m3Err_none; |
1382 | | |
1383 | 83.4k | u32 popCount = 0; |
1384 | 182k | while (o->stackIndex > o->block.blockStackIndex) { |
1385 | 99.3k | _ (Pop(o)); |
1386 | 99.2k | ++popCount; |
1387 | 99.2k | } |
1388 | | |
1389 | 83.4k | if (popCount) { |
1390 | 37.5k | m3log(compile, "unwound stack top: %d", popCount); |
1391 | 37.5k | } |
1392 | | |
1393 | 83.4k | _catch: return result; |
1394 | 83.4k | } |
1395 | | |
1396 | | static inline |
1397 | | M3Result SetStackPolymorphic (IM3Compilation o) |
1398 | 75.7k | { |
1399 | 75.7k | o->block.isPolymorphic = true; m3log (compile, "stack set polymorphic"); |
1400 | 75.7k | return UnwindBlockStack(o); |
1401 | 75.7k | } |
1402 | | |
1403 | | static |
1404 | | void PatchBranches (IM3Compilation o) |
1405 | 7.70k | { |
1406 | 7.70k | InvalidateFold(o); // patched branches land at the current pc |
1407 | | |
1408 | 7.70k | pc_t pc = GetPC(o); |
1409 | | |
1410 | 7.70k | pc_t patches = o->block.patches; |
1411 | 7.70k | o->block.patches = NULL; |
1412 | | |
1413 | 9.52k | while (patches) { m3log (compile, "patching location: %p to pc: %p", patches, pc); |
1414 | 1.82k | pc_t next = *(pc_t*)patches; |
1415 | 1.82k | *(pc_t*)patches = pc; |
1416 | 1.82k | patches = next; |
1417 | 1.82k | } |
1418 | 7.70k | } |
1419 | | |
1420 | | //------------------------------------------------------------------------------------------------------------------------- |
1421 | | |
1422 | | static |
1423 | | M3Result CopyStackIndexToSlot (IM3Compilation o, u16 i_destSlot, u16 i_stackIndex) // NoPushPop |
1424 | 17.6k | { |
1425 | 17.6k | M3Result result = m3Err_none; |
1426 | | |
1427 | 17.6k | IM3Operation op; |
1428 | | |
1429 | 17.6k | m3type_t type = GetStackTypeFromBottom(o, i_stackIndex); |
1430 | 17.6k | bool inRegister = IsStackIndexInRegister(o, i_stackIndex); |
1431 | | |
1432 | 17.6k | if (inRegister) { |
1433 | 2.10k | op = c_setSetOps[BaseTypeOf(type)]; |
1434 | 15.5k | } else { |
1435 | 15.5k | op = Is64BitType(type) ? op_CopySlot_64 : op_CopySlot_32; |
1436 | 15.5k | } |
1437 | | |
1438 | 17.6k | _ (EmitOp(o, op)); |
1439 | 17.6k | EmitSlotOffset(o, i_destSlot); |
1440 | | |
1441 | 17.6k | if (not inRegister) { |
1442 | 15.5k | u16 srcSlot = GetSlotForStackIndex(o, i_stackIndex); |
1443 | 15.5k | EmitSlotOffset(o, srcSlot); |
1444 | 15.5k | } |
1445 | | |
1446 | 17.6k | _catch: return result; |
1447 | 17.6k | } |
1448 | | |
1449 | | static |
1450 | | M3Result CopyStackTopToSlot (IM3Compilation o, u16 i_destSlot) // NoPushPop |
1451 | 9.14k | { |
1452 | 9.14k | M3Result result; |
1453 | | |
1454 | 9.14k | i16 stackTop; |
1455 | 9.14k | _ (GetStackTopIndexThrows(o, &stackTop)); |
1456 | 9.14k | _ (CopyStackIndexToSlot(o, i_destSlot, (u16)stackTop)); |
1457 | | |
1458 | 9.14k | _catch: return result; |
1459 | 9.14k | } |
1460 | | |
1461 | | |
1462 | | // a copy-on-write strategy is used with locals. when a get local occurs, it's not copied anywhere. the stack |
1463 | | // entry just has a index pointer to that local memory slot. |
1464 | | // then, when a previously referenced local is set, the current value needs to be preserved for those references |
1465 | | |
1466 | | // TODO: consider getting rid of these specialized operations: PreserveSetSlot & PreserveCopySlot. |
1467 | | // They likely just take up space (which seems to reduce performance) without improving performance. |
1468 | | // => unconfirmed in preliminary testing. |
1469 | | static |
1470 | | M3Result PreservedCopyTopSlot (IM3Compilation o, u16 i_destSlot, u16 i_preserveSlot) |
1471 | 1.17k | { |
1472 | 1.17k | M3Result result = m3Err_none; d_m3Assert (i_destSlot != i_preserveSlot); |
1473 | | |
1474 | 1.17k | IM3Operation op; |
1475 | | |
1476 | 1.17k | m3type_t type = GetStackTopType(o); |
1477 | | |
1478 | 1.17k | if (IsStackTopInRegister(o)) { |
1479 | 442 | op = c_preserveSetSlot[BaseTypeOf(type)]; |
1480 | 730 | } else { |
1481 | 730 | op = Is64BitType(type) ? op_PreserveCopySlot_64 : op_PreserveCopySlot_32; |
1482 | 730 | } |
1483 | | |
1484 | 1.17k | _ (EmitOp(o, op)); |
1485 | 1.17k | EmitSlotOffset(o, i_destSlot); |
1486 | | |
1487 | 1.17k | if (IsStackTopInSlot(o)) { |
1488 | 730 | EmitSlotOffset(o, GetStackTopSlotNumber(o)); |
1489 | 730 | } |
1490 | | |
1491 | 1.17k | EmitSlotOffset(o, i_preserveSlot); |
1492 | | |
1493 | 1.17k | _catch: return result; |
1494 | 1.17k | } |
1495 | | |
1496 | | static |
1497 | | M3Result CopyStackTopToRegister (IM3Compilation o, bool i_updateStack) |
1498 | 4.06k | { |
1499 | 4.06k | M3Result result = m3Err_none; |
1500 | | |
1501 | 4.06k | if (IsStackTopInSlot(o)) { |
1502 | 2.80k | m3type_t type = GetStackTopType(o); |
1503 | | |
1504 | 2.80k | _ (PreserveRegisterIfOccupied(o, type)); |
1505 | | |
1506 | 2.80k | IM3Operation op = c_setRegisterOps[BaseTypeOf(type)]; |
1507 | | |
1508 | 2.80k | _ (EmitOp(o, op)); |
1509 | 2.80k | EmitSlotOffset(o, GetStackTopSlotNumber(o)); |
1510 | | |
1511 | 2.80k | if (i_updateStack) { |
1512 | 0 | _ (PopType(o, type)); |
1513 | 0 | _ (PushRegister(o, type)); |
1514 | 0 | } |
1515 | 2.80k | } |
1516 | | |
1517 | 4.06k | _catch: return result; |
1518 | 4.06k | } |
1519 | | |
1520 | | |
1521 | | // if local is unreferenced, o_preservedSlotNumber will be equal to localIndex on return |
1522 | | static |
1523 | | M3Result FindReferencedLocalWithinCurrentBlock (IM3Compilation o, u16* o_preservedSlotNumber, u32 i_localSlot) |
1524 | 2.63M | { |
1525 | 2.63M | M3Result result = m3Err_none; |
1526 | | |
1527 | 2.63M | IM3CompilationScope scope = &o->block; |
1528 | | |
1529 | 2.63M | u16 startIndex = scope->blockStackIndex; |
1530 | | |
1531 | 2.85M | while (scope->opcode == c_waOp_block) { |
1532 | 215k | scope = scope->outer; |
1533 | 215k | if (not scope) { |
1534 | 0 | break; |
1535 | 0 | } |
1536 | | |
1537 | 215k | startIndex = scope->blockStackIndex; |
1538 | 215k | } |
1539 | | |
1540 | 2.63M | *o_preservedSlotNumber = (u16)i_localSlot; |
1541 | | |
1542 | 8.43M | for (u32 i = startIndex; i < o->stackIndex; ++i) { |
1543 | 5.80M | if (o->wasmStack[i] == i_localSlot) { |
1544 | 2.08k | m3type_t type = GetStackTypeFromBottom(o, i); d_m3Assert (type != c_m3Type_none) |
1545 | | |
1546 | 2.08k | if (*o_preservedSlotNumber == i_localSlot) { |
1547 | 1.54k | _ (AllocateSlots(o, o_preservedSlotNumber, type)); |
1548 | 1.54k | } else { |
1549 | | // A 64-bit value spans two slots, and allocating or releasing it |
1550 | | // counts both, so a second reference has to count both as well. |
1551 | 1.75k | for (u16 s = 0; s < GetTypeNumSlots(type); ++s) { |
1552 | 1.21k | _ (IncrementSlotUsageCount(o, (u16)(*o_preservedSlotNumber + s))); |
1553 | 1.20k | } |
1554 | 541 | } |
1555 | | |
1556 | 2.08k | o->wasmStack[i] = *o_preservedSlotNumber; |
1557 | 2.08k | } |
1558 | 5.80M | } |
1559 | | |
1560 | 2.63M | _catch: return result; |
1561 | 2.63M | } |
1562 | | |
1563 | | static |
1564 | | M3Result GetBlockScope (IM3Compilation o, IM3CompilationScope* o_scope, u32 i_depth) |
1565 | 6.70k | { |
1566 | 6.70k | M3Result result = m3Err_none; |
1567 | | |
1568 | 6.70k | IM3CompilationScope scope = &o->block; |
1569 | | |
1570 | 9.02k | while (i_depth--) { |
1571 | 2.32k | scope = scope->outer; |
1572 | 2.32k | _throwif("invalid block depth", not scope); |
1573 | 2.32k | } |
1574 | | |
1575 | 6.70k | *o_scope = scope; |
1576 | | |
1577 | 6.70k | _catch: |
1578 | 6.70k | return result; |
1579 | 6.70k | } |
1580 | | |
1581 | | static |
1582 | | M3Result CopyStackSlotsR (IM3Compilation o, u16 i_targetSlotStackIndex, u16 i_stackIndex, u16 i_endStackIndex, u16 i_tempSlot) |
1583 | 5.65k | { |
1584 | 5.65k | M3Result result = m3Err_none; |
1585 | | |
1586 | 5.65k | if (i_stackIndex < i_endStackIndex) { |
1587 | 5.16k | u16 srcSlot = GetSlotForStackIndex(o, i_stackIndex); |
1588 | | |
1589 | 5.16k | m3type_t type = GetStackTypeFromBottom(o, i_stackIndex); |
1590 | 5.16k | u16 numSlots = GetTypeNumSlots(type); |
1591 | 5.16k | u16 extraSlot = numSlots - 1; |
1592 | | |
1593 | 5.16k | u16 targetSlot = GetSlotForStackIndex(o, i_targetSlotStackIndex); |
1594 | | |
1595 | 5.16k | u16 preserveIndex = i_stackIndex; |
1596 | 5.16k | u16 collisionSlot = srcSlot; |
1597 | | |
1598 | 5.16k | if (targetSlot != srcSlot) { |
1599 | | // search for collisions |
1600 | 4.49k | u16 checkIndex = i_stackIndex + 1; |
1601 | 43.8k | while (checkIndex < i_endStackIndex) { |
1602 | 40.4k | u16 otherSlot1 = GetSlotForStackIndex(o, checkIndex); |
1603 | 40.4k | u16 otherSlot2 = GetExtraSlotForStackIndex(o, checkIndex); |
1604 | | |
1605 | 40.4k | if (targetSlot == otherSlot1 or |
1606 | 39.5k | targetSlot == otherSlot2 or |
1607 | 39.4k | targetSlot + extraSlot == otherSlot1) { |
1608 | 1.13k | _throwif(m3Err_functionStackOverflow, i_tempSlot >= d_m3MaxFunctionSlots); |
1609 | | |
1610 | 1.12k | _ (CopyStackIndexToSlot(o, i_tempSlot, checkIndex)); |
1611 | 1.12k | o->wasmStack[checkIndex] = i_tempSlot; |
1612 | 1.12k | i_tempSlot += GetTypeNumSlots(c_m3Type_i64); |
1613 | 1.12k | TouchSlot(o, i_tempSlot - 1); |
1614 | | |
1615 | | // restore this on the way back down |
1616 | 1.12k | preserveIndex = checkIndex; |
1617 | 1.12k | collisionSlot = otherSlot1; |
1618 | | |
1619 | 1.12k | break; |
1620 | 1.12k | } |
1621 | | |
1622 | 39.3k | ++checkIndex; |
1623 | 39.3k | } |
1624 | | |
1625 | 4.48k | _ (CopyStackIndexToSlot(o, targetSlot, i_stackIndex)); m3log (compile, " copying slot: %d to slot: %d", srcSlot, targetSlot); |
1626 | 4.48k | o->wasmStack[i_stackIndex] = targetSlot; |
1627 | 4.48k | } |
1628 | | |
1629 | 5.16k | _ (CopyStackSlotsR(o, i_targetSlotStackIndex + 1, i_stackIndex + 1, i_endStackIndex, i_tempSlot)); |
1630 | | |
1631 | | // restore the stack state |
1632 | 5.11k | o->wasmStack[i_stackIndex] = srcSlot; |
1633 | 5.11k | o->wasmStack[preserveIndex] = collisionSlot; |
1634 | 5.11k | } |
1635 | | |
1636 | 5.65k | _catch: |
1637 | 5.65k | return result; |
1638 | 5.65k | } |
1639 | | |
1640 | | static |
1641 | | M3Result ResolveBlockResults (IM3Compilation o, IM3CompilationScope i_targetBlock, bool i_isBranch) |
1642 | 5.55k | { |
1643 | 5.55k | M3Result result = m3Err_none; |
1644 | 5.55k | if (d_m3LogWasmStack) { |
1645 | 0 | dump_type_stack(o); |
1646 | 0 | } |
1647 | | |
1648 | 5.55k | bool isLoop = (i_targetBlock->opcode == c_waOp_loop and i_isBranch); |
1649 | | |
1650 | 5.55k | u16 numParams = GetFuncTypeNumParams(i_targetBlock->type); |
1651 | 5.55k | u16 numResults = GetFuncTypeNumResults(i_targetBlock->type); |
1652 | | |
1653 | 5.55k | u16 slotRecords = i_targetBlock->exitStackIndex; |
1654 | | |
1655 | 5.55k | u16 numValues; |
1656 | | |
1657 | 5.55k | if (not isLoop) { |
1658 | 5.28k | numValues = numResults; |
1659 | 5.28k | slotRecords += numParams; |
1660 | 5.28k | } else { |
1661 | 274 | numValues = numParams; |
1662 | 274 | } |
1663 | | |
1664 | 5.55k | u16 blockHeight = GetNumBlockValuesOnStack(o); |
1665 | | |
1666 | 5.55k | _throwif(m3Err_typeCountMismatch, i_isBranch ? (blockHeight < numValues) : (blockHeight != numValues)); |
1667 | | |
1668 | 5.55k | if (numValues) { |
1669 | 490 | u16 endIndex = GetStackTopIndex(o) + 1; |
1670 | 490 | u16 numRemValues = numValues; |
1671 | | |
1672 | | // The last result is taken from _fp0. See PushBlockResults. |
1673 | 490 | if (not isLoop and IsFpType(GetStackTopType(o))) { |
1674 | 108 | _ (CopyStackTopToRegister(o, false)); |
1675 | 108 | --endIndex; |
1676 | 108 | --numRemValues; |
1677 | 108 | } |
1678 | | |
1679 | | // tempslot affects maxStackSlots, so can grow unnecess each time. |
1680 | | // in practice, this is hardly ever hit. |
1681 | 490 | u16 tempSlot = o->maxStackSlots;// GetMaxUsedSlotPlusOne (o); doesn't work cause can collide with slotRecords |
1682 | 490 | AlignSlotToType(&tempSlot, c_m3Type_i64); |
1683 | | |
1684 | 490 | _ (CopyStackSlotsR(o, slotRecords, endIndex - numRemValues, endIndex, tempSlot)); |
1685 | | |
1686 | 483 | if (d_m3LogWasmStack) { |
1687 | 0 | dump_type_stack(o); |
1688 | 0 | } |
1689 | 483 | } |
1690 | | |
1691 | 5.55k | _catch: return result; |
1692 | 5.55k | } |
1693 | | |
1694 | | |
1695 | | static |
1696 | | M3Result ReturnValues (IM3Compilation o, IM3CompilationScope i_functionBlock, bool i_isBranch) |
1697 | 33.3k | { |
1698 | 33.3k | M3Result result = m3Err_none; |
1699 | 33.3k | if (d_m3LogWasmStack) { |
1700 | 0 | dump_type_stack(o); |
1701 | 0 | } |
1702 | | |
1703 | 33.3k | u16 numReturns = GetFuncTypeNumResults(i_functionBlock->type); // could just o->function too... |
1704 | 33.3k | u16 blockHeight = GetNumBlockValuesOnStack(o); |
1705 | | |
1706 | 33.3k | if (not IsStackPolymorphic(o)) { |
1707 | 3.99k | _throwif(m3Err_typeCountMismatch, i_isBranch ? (blockHeight < numReturns) : (blockHeight != numReturns)); |
1708 | 3.86k | } |
1709 | | |
1710 | 33.2k | if (numReturns) { |
1711 | | // return slots like args are 64-bit aligned |
1712 | 1.96k | u16 returnSlot = numReturns * c_ioSlotCount; |
1713 | 1.96k | u16 stackTop = GetStackTopIndex(o); |
1714 | | |
1715 | 7.91k | for (u16 i = 0; i < numReturns; ++i) { |
1716 | 5.98k | m3type_t returnType = GetFuncTypeResultType(i_functionBlock->type, numReturns - 1 - i); |
1717 | | |
1718 | 5.98k | m3type_t stackType = GetStackTypeFromTop(o, i); // using FromTop so that only dynamic items are checked |
1719 | | |
1720 | 5.98k | if (IsStackPolymorphic(o) and stackType == c_m3Type_none) { |
1721 | 2.60k | stackType = returnType; |
1722 | 2.60k | } |
1723 | | |
1724 | 5.98k | _throwif(m3Err_typeMismatch, not IsSubTypeOf(stackType, returnType)); |
1725 | | |
1726 | 5.94k | if (not IsStackPolymorphic(o)) { |
1727 | 2.67k | returnSlot -= c_ioSlotCount; |
1728 | 2.67k | _ (CopyStackIndexToSlot(o, returnSlot, stackTop--)); |
1729 | 2.67k | } |
1730 | 5.94k | } |
1731 | | |
1732 | 1.92k | if (not i_isBranch) { |
1733 | 6.46k | while (numReturns--) { |
1734 | 4.65k | _ (Pop(o)); |
1735 | 4.65k | } |
1736 | 1.81k | } |
1737 | 1.92k | } |
1738 | | |
1739 | 33.3k | _catch: return result; |
1740 | 33.2k | } |
1741 | | |
1742 | | |
1743 | | //------------------------------------------------------------------------------------------------------------------------- |
1744 | | |
1745 | | static |
1746 | | M3Result Compile_Const_i32 (IM3Compilation o, m3opcode_t i_opcode) |
1747 | 74.3k | { |
1748 | 74.3k | M3Result result; |
1749 | | |
1750 | 74.3k | i32 value; |
1751 | 74.3k | _ (ReadLEB_i32(&value, &o->wasm, o->wasmEnd)); |
1752 | 74.3k | _ (PushConst(o, value, c_m3Type_i32)); m3log (compile, d_indent " (const i32 = %" PRIi32 ")", get_indention_string (o), value); |
1753 | 74.3k | _catch: return result; |
1754 | 74.2k | } |
1755 | | |
1756 | | static |
1757 | | M3Result Compile_Const_i64 (IM3Compilation o, m3opcode_t i_opcode) |
1758 | 94.4k | { |
1759 | 94.4k | M3Result result; |
1760 | | |
1761 | 94.4k | i64 value; |
1762 | 94.4k | _ (ReadLEB_i64(&value, &o->wasm, o->wasmEnd)); |
1763 | 94.4k | _ (PushConst(o, value, c_m3Type_i64)); m3log (compile, d_indent " (const i64 = %" PRIi64 ")", get_indention_string (o), value); |
1764 | 94.4k | _catch: return result; |
1765 | 94.4k | } |
1766 | | |
1767 | | |
1768 | | #if d_m3ImplementFloat |
1769 | | static |
1770 | | M3Result Compile_Const_f32 (IM3Compilation o, m3opcode_t i_opcode) |
1771 | 9.59k | { |
1772 | 9.59k | M3Result result; |
1773 | | |
1774 | 9.59k | union { |
1775 | 9.59k | u32 u; |
1776 | 9.59k | f32 f; |
1777 | 9.59k | } value = { 0 }; |
1778 | | |
1779 | 9.59k | _ (Read_f32(&value.f, &o->wasm, o->wasmEnd)); m3log (compile, d_indent " (const f32 = %" PRIf32 ")", get_indention_string (o), value.f); |
1780 | 9.58k | _ (PushConst(o, value.u, c_m3Type_f32)); |
1781 | | |
1782 | 9.59k | _catch: return result; |
1783 | 9.58k | } |
1784 | | |
1785 | | static |
1786 | | M3Result Compile_Const_f64 (IM3Compilation o, m3opcode_t i_opcode) |
1787 | 1.99k | { |
1788 | 1.99k | M3Result result; |
1789 | | |
1790 | 1.99k | union { |
1791 | 1.99k | u64 u; |
1792 | 1.99k | f64 f; |
1793 | 1.99k | } value = { 0 }; |
1794 | | |
1795 | 1.99k | _ (Read_f64(&value.f, &o->wasm, o->wasmEnd)); m3log (compile, d_indent " (const f64 = %" PRIf64 ")", get_indention_string (o), value.f); |
1796 | 1.99k | _ (PushConst(o, value.u, c_m3Type_f64)); |
1797 | | |
1798 | 1.99k | _catch: return result; |
1799 | 1.99k | } |
1800 | | #endif |
1801 | | |
1802 | | static |
1803 | | M3Result Compile_ExtendedOpcode (IM3Compilation o, m3opcode_t i_opcode) |
1804 | 9.07k | { |
1805 | 9.07k | _try { |
1806 | 9.07k | u32 opcode; |
1807 | 9.07k | _ (ReadLEB_u32(&opcode, &o->wasm, o->wasmEnd)); m3log (compile, d_indent " (%s: %" PRIu32 ")", get_indention_string (o), (i_opcode == c_waOp_atomic) ? "FE" : "FC", opcode); |
1808 | | |
1809 | | // m3opcode_t carries only the low byte, so a wider sub-opcode is unknown |
1810 | | // here rather than something to truncate into range |
1811 | 9.07k | _throwif(m3Err_unknownOpcode, opcode > (u32)((i_opcode == c_waOp_atomic) ? c_waOp_lastAtomic : c_waOp_lastExtended)); |
1812 | | |
1813 | 8.99k | i_opcode = (m3opcode_t)((i_opcode << 8) | opcode); |
1814 | | |
1815 | | //printf("Extended opcode: 0x%x\n", i_opcode); |
1816 | | |
1817 | 8.99k | IM3OpInfo opInfo = GetOpInfo(i_opcode); |
1818 | 8.99k | _throwif(m3Err_unknownOpcode, not opInfo); |
1819 | | |
1820 | 8.99k | M3Compiler compiler = GetCompiler(opInfo); |
1821 | 8.99k | _throwif(m3Err_noCompiler, not compiler); |
1822 | | |
1823 | 8.99k | _ ((*compiler)(o, i_opcode)); |
1824 | | |
1825 | 8.96k | o->previousOpcode = i_opcode; |
1826 | | |
1827 | 9.07k | } _catch: return result; |
1828 | 8.96k | } |
1829 | | |
1830 | | static |
1831 | | M3Result Compile_Return (IM3Compilation o, m3opcode_t i_opcode) |
1832 | 2.95k | { |
1833 | 2.95k | M3Result result = m3Err_none; |
1834 | | |
1835 | 2.95k | if (not IsStackPolymorphic(o)) { |
1836 | 1.15k | IM3CompilationScope functionScope; |
1837 | 1.15k | _ (GetBlockScope(o, &functionScope, o->block.depth)); |
1838 | | |
1839 | 1.15k | _ (ReturnValues(o, functionScope, true)); |
1840 | | |
1841 | 1.15k | _ (EmitOp(o, op_Return)); |
1842 | | |
1843 | 1.15k | _ (SetStackPolymorphic(o)); |
1844 | 1.15k | } |
1845 | | |
1846 | 2.95k | _catch: return result; |
1847 | 2.95k | } |
1848 | | |
1849 | | static |
1850 | | M3Result ValidateBlockEnd (IM3Compilation o) |
1851 | 41.8k | { |
1852 | 41.8k | M3Result result = m3Err_none; |
1853 | | |
1854 | 41.8k | u16 numResults = GetFuncTypeNumResults(o->block.type); |
1855 | 41.8k | u16 blockHeight = GetNumBlockValuesOnStack(o); |
1856 | | |
1857 | 41.8k | if (not IsStackPolymorphic(o)) { |
1858 | | // Spec: at block end, stack height must match the number of results |
1859 | 9.83k | _throwif(m3Err_typeCountMismatch, blockHeight != numResults); |
1860 | | |
1861 | | // Spec: result types must match expected types |
1862 | 11.4k | for (u16 i = 0; i < numResults; ++i) { |
1863 | 4.47k | m3type_t expectedType = GetFuncTypeResultType(o->block.type, numResults - 1 - i); |
1864 | 4.47k | m3type_t actualType = GetStackTypeFromTop(o, i); |
1865 | 4.47k | _throwif(m3Err_typeMismatch, not IsSubTypeOf(actualType, expectedType)); |
1866 | 4.44k | } |
1867 | 7.03k | } |
1868 | | |
1869 | 41.8k | _catch: return result; |
1870 | 41.8k | } |
1871 | | |
1872 | | static |
1873 | | M3Result Compile_End (IM3Compilation o, m3opcode_t i_opcode) |
1874 | 41.7k | { |
1875 | 41.7k | M3Result result = m3Err_none; //dump_type_stack (o); |
1876 | | |
1877 | | // function end: |
1878 | 41.7k | if (o->block.depth == 0) { |
1879 | 34.1k | ValidateBlockEnd(o); |
1880 | | |
1881 | | // if (not IsStackPolymorphic (o)) |
1882 | 34.1k | { |
1883 | | // A constant expression has no function frame, but its value still |
1884 | | // has to be copied down to slot 0, where EvaluateExpression reads it |
1885 | | // back. The non-emitting init-expr walk is skipped: it has no block |
1886 | | // type to return against. |
1887 | 34.1k | if (o->function or o->page) { |
1888 | 31.2k | _ (ReturnValues(o, &o->block, false)); |
1889 | 31.0k | } |
1890 | | |
1891 | 33.9k | _ (EmitOp(o, op_Return)); |
1892 | 33.9k | } |
1893 | 33.9k | } |
1894 | | |
1895 | 41.7k | _catch: return result; |
1896 | 41.7k | } |
1897 | | |
1898 | | |
1899 | | static |
1900 | | M3Result Compile_SetLocal (IM3Compilation o, m3opcode_t i_opcode) |
1901 | 11.3k | { |
1902 | 11.3k | M3Result result; |
1903 | | |
1904 | 11.3k | u32 localIndex; |
1905 | 11.3k | _ (ReadLEB_u32(&localIndex, &o->wasm, o->wasmEnd)); m3log (compile, d_indent " (index = %u)", get_indention_string (o), localIndex); |
1906 | | |
1907 | 11.3k | if (localIndex < GetFunctionNumArgsAndLocals(o->function)) { |
1908 | | // Spec: value type must match local type |
1909 | 11.3k | if (not IsStackPolymorphic(o)) { |
1910 | 9.71k | m3type_t localType = GetStackTypeFromBottom(o, localIndex); |
1911 | 9.71k | m3type_t stackTopType = GetStackTopType(o); |
1912 | 9.71k | _throwif(m3Err_typeMismatch, stackTopType != c_m3Type_none and localType != c_m3Type_none and |
1913 | 9.71k | not IsSubTypeOf(stackTopType, localType)); |
1914 | 9.71k | #if d_m3HasSnapshots |
1915 | 9.71k | if (o->localInitDepth and o->localInitDepth[localIndex] == UINT32_MAX) { |
1916 | 0 | o->localInitDepth[localIndex] = (u32)o->block.depth; |
1917 | 0 | } |
1918 | 9.71k | #endif |
1919 | 9.71k | } |
1920 | | |
1921 | 11.3k | u16 localSlot = GetSlotForStackIndex(o, localIndex); |
1922 | | |
1923 | 11.3k | u16 preserveSlot; |
1924 | 11.3k | _ (FindReferencedLocalWithinCurrentBlock(o, &preserveSlot, localSlot)); // preserve will be different than local, if referenced |
1925 | | |
1926 | 11.3k | bool folded = false; |
1927 | | |
1928 | 11.3k | #if d_m3FoldSetLocal |
1929 | | // destination folding: when the value was produced by the immediately preceding op (nothing |
1930 | | // emitted since, result on top in _r0, no copy-on-write preservation needed), retro-patch the |
1931 | | // producer to a variant that writes straight to the local's slot and skip the SetSlot op |
1932 | 11.3k | if (o->foldPatchPC and preserveSlot == localSlot and IsStackTopInRegister(o) and GetStackTopIndex(o) == (i16)o->foldStackIndex) { |
1933 | 7.06k | IM3Operation foldOp = GetFoldOp(o->foldOpcode, o->foldForm); |
1934 | | |
1935 | 7.06k | if (foldOp) { |
1936 | 6.18k | *(IM3Operation*)o->foldPatchPC = foldOp; // retro-patch the producer |
1937 | 6.18k | EmitSlotOffset(o, localSlot); // append its destination |
1938 | 6.18k | InvalidateFold(o); |
1939 | 6.18k | folded = true; |
1940 | 6.18k | } |
1941 | 7.06k | } |
1942 | 11.3k | #endif |
1943 | | |
1944 | 11.3k | if (folded) { |
1945 | 6.18k | m3type_t type = GetStackTopType(o); |
1946 | 6.18k | _ (Pop(o)); // the value now lives in the local's slot |
1947 | | |
1948 | | // A folded op writes its result through to the register as well, so a tee |
1949 | | // can keep it there. Pushing the slot instead would make the next use that |
1950 | | // wants a register reload the slot this op just stored to. |
1951 | 6.18k | if (i_opcode == c_waOp_teeLocal) { |
1952 | 3.90k | _ (PushRegister(o, type)); |
1953 | 3.90k | } |
1954 | 6.18k | } else { |
1955 | 5.14k | if (preserveSlot == localSlot) { |
1956 | 3.97k | _ (CopyStackTopToSlot(o, localSlot)) |
1957 | 3.97k | } else { |
1958 | 1.17k | _ (PreservedCopyTopSlot(o, localSlot, preserveSlot)) |
1959 | 1.17k | } |
1960 | | |
1961 | 5.14k | if (i_opcode != c_waOp_teeLocal) { |
1962 | 1.84k | _ (Pop(o)); |
1963 | 1.84k | } |
1964 | 5.14k | } |
1965 | 11.3k | } else { |
1966 | 0 | _throw("local index out of bounds"); |
1967 | 0 | } |
1968 | | |
1969 | 11.3k | _catch: return result; |
1970 | 11.3k | } |
1971 | | |
1972 | | static |
1973 | | M3Result Compile_GetLocal (IM3Compilation o, m3opcode_t i_opcode) |
1974 | 21.1k | { |
1975 | 21.1k | _try { |
1976 | | |
1977 | 21.1k | u32 localIndex; |
1978 | 21.1k | _ (ReadLEB_u32(&localIndex, &o->wasm, o->wasmEnd)); m3log (compile, d_indent " (index = %u)", get_indention_string (o), localIndex); |
1979 | | |
1980 | 21.1k | if (localIndex >= GetFunctionNumArgsAndLocals(o->function)) { |
1981 | 0 | _throw("local index out of bounds"); |
1982 | 0 | } |
1983 | | |
1984 | 21.1k | m3type_t type = GetStackTypeFromBottom(o, localIndex); |
1985 | 21.1k | u16 slot = GetSlotForStackIndex(o, localIndex); |
1986 | | |
1987 | 21.1k | _ (Push(o, type, slot)); |
1988 | | |
1989 | 21.1k | } _catch: return result; |
1990 | 21.1k | } |
1991 | | |
1992 | | static |
1993 | | M3Result Compile_GetGlobal (IM3Compilation o, M3Global* i_global) |
1994 | 833 | { |
1995 | 833 | M3Result result; |
1996 | | |
1997 | 833 | IM3Operation op = Is64BitType(i_global->type) ? op_GetGlobal_s64 : op_GetGlobal_s32; |
1998 | 833 | _ (EmitOp(o, op)); |
1999 | 833 | EmitPointer(o, &i_global->i64Value); |
2000 | 833 | _ (PushAllocatedSlotAndEmit(o, i_global->type)); |
2001 | | |
2002 | 833 | _catch: return result; |
2003 | 832 | } |
2004 | | |
2005 | | static |
2006 | | M3Result Compile_SetGlobal (IM3Compilation o, M3Global* i_global) |
2007 | 356 | { |
2008 | 356 | M3Result result = m3Err_none; |
2009 | | |
2010 | 356 | if (i_global->isMutable) { |
2011 | | // Spec: value type must match global type |
2012 | 354 | if (not IsStackPolymorphic(o)) { |
2013 | 230 | m3type_t stackTopType = GetStackTopType(o); |
2014 | 230 | _throwif(m3Err_typeMismatch, stackTopType != c_m3Type_none and |
2015 | 230 | not IsSubTypeOf(stackTopType, i_global->type)); |
2016 | 230 | } |
2017 | | |
2018 | 354 | IM3Operation op; |
2019 | 354 | m3type_t type = GetStackTopType(o); |
2020 | | |
2021 | 354 | if (IsStackTopInRegister(o)) { |
2022 | 125 | op = c_setGlobalOps[BaseTypeOf(type)]; |
2023 | 229 | } else { |
2024 | 229 | op = Is64BitType(type) ? op_SetGlobal_s64 : op_SetGlobal_s32; |
2025 | 229 | } |
2026 | | |
2027 | 354 | _ (EmitOp(o, op)); |
2028 | 354 | EmitPointer(o, &i_global->i64Value); |
2029 | | |
2030 | 354 | if (IsStackTopInSlot(o)) { |
2031 | 229 | EmitSlotOffset(o, GetStackTopSlotNumber(o)); |
2032 | 229 | } |
2033 | | |
2034 | 354 | _ (Pop(o)); |
2035 | 353 | } else { |
2036 | 2 | _throw(m3Err_settingImmutableGlobal); |
2037 | 0 | } |
2038 | | |
2039 | 356 | _catch: return result; |
2040 | 356 | } |
2041 | | |
2042 | | static |
2043 | | M3Result Compile_GetSetGlobal (IM3Compilation o, m3opcode_t i_opcode) |
2044 | 1.24k | { |
2045 | 1.24k | M3Result result = m3Err_none; |
2046 | | |
2047 | 1.24k | u32 globalIndex; |
2048 | 1.24k | _ (ReadLEB_u32(&globalIndex, &o->wasm, o->wasmEnd)); |
2049 | | |
2050 | 1.24k | if (globalIndex < o->module->numGlobals) { |
2051 | 1.19k | if (o->module->globals) { |
2052 | 1.19k | M3Global* global = &o->module->globals[globalIndex]; |
2053 | | |
2054 | | // Spec: a constant expression may only read an imported immutable |
2055 | | // global. The module's own globals are counted before their |
2056 | | // initializer is walked, so a bare index check would let one |
2057 | | // reference itself. These describe how the module declared the |
2058 | | // global, so they are checked before following any link. |
2059 | 1.19k | _throwif(m3Err_globaIndexOutOfBounds, o->isInitExpr and not global->imported); |
2060 | 1.19k | _throwif(m3Err_wasmMalformed, o->isInitExpr and global->isMutable); |
2061 | | |
2062 | | // an import linked to another module reads and writes that module's |
2063 | | // cell, not the placeholder standing in for it here |
2064 | 1.18k | if (global->resolved) { |
2065 | 0 | global = global->resolved; |
2066 | 0 | } |
2067 | | |
2068 | 1.18k | _ ((i_opcode == c_waOp_getGlobal) ? Compile_GetGlobal(o, global) : Compile_SetGlobal(o, global)); |
2069 | 1.18k | } else { |
2070 | 0 | _throw(ErrorCompile(m3Err_globalMemoryNotAllocated, o, "module '%s' is missing global memory", o->module->name)); |
2071 | 0 | } |
2072 | 1.19k | } else { |
2073 | 50 | _throw(m3Err_globaIndexOutOfBounds); |
2074 | 0 | } |
2075 | | |
2076 | 1.24k | _catch: return result; |
2077 | 1.24k | } |
2078 | | |
2079 | | static |
2080 | | void EmitPatchingBranchPointer (IM3Compilation o, IM3CompilationScope i_scope) |
2081 | 1.96k | { |
2082 | 1.96k | pc_t patch = EmitPointer(o, i_scope->patches); m3log (compile, "branch patch required at: %p", patch); |
2083 | 1.96k | i_scope->patches = patch; |
2084 | 1.96k | } |
2085 | | |
2086 | | static |
2087 | | M3Result EmitPatchingBranch (IM3Compilation o, IM3CompilationScope i_scope) |
2088 | 265 | { |
2089 | 265 | M3Result result = m3Err_none; |
2090 | | |
2091 | 265 | _ (EmitOp(o, op_Branch)); |
2092 | 265 | EmitPatchingBranchPointer(o, i_scope); |
2093 | | |
2094 | 265 | _catch: return result; |
2095 | 265 | } |
2096 | | |
2097 | | #if d_m3HasExceptionHandling |
2098 | | |
2099 | | // How many try_table handler records an exit from i_from out to i_target takes |
2100 | | // down with it. Walking outward from the block the branch sits in, every |
2101 | | // try_table scope up to and including the target is left behind - the target |
2102 | | // too, because landing on a block's label means that block is finished. |
2103 | | // |
2104 | | // A loop label is the exception, but a loop is never a try_table, so the count |
2105 | | // is the same either way: the loop itself is not on the list. |
2106 | | static |
2107 | | u32 CountTryFramesToExit (IM3CompilationScope i_from, IM3CompilationScope i_target) |
2108 | 3.36k | { |
2109 | 3.36k | u32 count = 0; |
2110 | | |
2111 | 4.78k | for (IM3CompilationScope scope = i_from; scope; scope = scope->outer) { |
2112 | 4.78k | if (scope->opcode == c_waOp_tryTable) { |
2113 | 525 | ++count; |
2114 | 525 | } |
2115 | | |
2116 | 4.78k | if (scope == i_target) { |
2117 | 3.36k | break; |
2118 | 3.36k | } |
2119 | 4.78k | } |
2120 | | |
2121 | 3.36k | return count; |
2122 | 3.36k | } |
2123 | | |
2124 | | |
2125 | | // The handler records a branch out of i_from to i_target has to drop. |
2126 | | // |
2127 | | // A branch to the function's own label compiles to op_Return, and returning |
2128 | | // unwinds op_TryTable's native frame, which drops that record on its own, so |
2129 | | // those need no pop at the branch site. |
2130 | | // |
2131 | | // i_from is where the walk starts, which is not always the block the branch is |
2132 | | // written in: a catch stub passes the scope outside its try, because |
2133 | | // op_TryTable takes its own record off before jumping to the stub. |
2134 | | static |
2135 | | u32 NumTryFramesToPop (IM3CompilationScope i_from, IM3CompilationScope i_target) |
2136 | 5.32k | { |
2137 | 5.32k | if (i_target->depth == 0) { |
2138 | 1.96k | return 0; |
2139 | 1.96k | } |
2140 | | |
2141 | | // the walk only ever runs inward-to-outward; a target that is already |
2142 | | // outside i_from has nothing between them |
2143 | 3.36k | if (not i_from or i_from->depth < i_target->depth) { |
2144 | 0 | return 0; |
2145 | 0 | } |
2146 | | |
2147 | 3.36k | return CountTryFramesToExit(i_from, i_target); |
2148 | 3.36k | } |
2149 | | |
2150 | | |
2151 | | static |
2152 | | M3Result EmitPopTryFrames (IM3Compilation o, u32 i_numFrames) |
2153 | 13.8k | { |
2154 | 13.8k | M3Result result = m3Err_none; |
2155 | | |
2156 | 13.8k | if (i_numFrames) { |
2157 | 469 | _ (EmitOp(o, op_PopHandlers)); |
2158 | 469 | EmitConstant32(o, i_numFrames); |
2159 | 469 | } |
2160 | | |
2161 | 13.8k | _catch: return result; |
2162 | 13.8k | } |
2163 | | |
2164 | | |
2165 | | // A return_call leaves the enclosing function before the callee runs, so no |
2166 | | // try_table in this function may catch what the callee throws. Neither shape of |
2167 | | // the instruction unwinds the native stack in time to arrange that on its own: |
2168 | | // op_ReturnCall tail-jumps with op_TryTable's frames still standing, and the |
2169 | | // fallback shape is an ordinary call sitting inside them. Either way the |
2170 | | // handler records have to come off first. |
2171 | | static |
2172 | | M3Result EmitPopTryFramesForReturnCall (IM3Compilation o) |
2173 | 12.8k | { |
2174 | 12.8k | u32 count = 0; |
2175 | | |
2176 | 26.6k | for (IM3CompilationScope scope = &o->block; scope; scope = scope->outer) { |
2177 | 13.7k | if (scope->opcode == c_waOp_tryTable) { |
2178 | 245 | ++count; |
2179 | 245 | } |
2180 | 13.7k | } |
2181 | | |
2182 | 12.8k | return EmitPopTryFrames(o, count); |
2183 | 12.8k | } |
2184 | | |
2185 | | #else |
2186 | | |
2187 | | static |
2188 | | M3Result EmitPopTryFrames (IM3Compilation o, u32 i_numFrames) |
2189 | | { |
2190 | | (void)o; |
2191 | | (void)i_numFrames; |
2192 | | return m3Err_none; |
2193 | | } |
2194 | | |
2195 | | #endif // d_m3HasExceptionHandling |
2196 | | |
2197 | | #if d_m3FuseBranch |
2198 | | // when the pending fold candidate is a compare whose result sits on top in _r0, return the fused |
2199 | | // variant that absorbs an immediately following br_if (i_isIf false) or if (i_isIf true) |
2200 | | static |
2201 | | IM3Operation GetBranchFusionOp (IM3Compilation o, bool i_isIf) |
2202 | 6.53k | { |
2203 | 6.53k | if (o->foldPatchPC and o->foldForm < 4 and IsStackTopInRegister(o) and GetStackTopIndex(o) == (i16)o->foldStackIndex) { |
2204 | 4.28k | const M3FusedCmpOps* ops = GetFusedCmpOps(o->foldOpcode); |
2205 | | |
2206 | 4.28k | if (ops) { |
2207 | 3.72k | return i_isIf ? ops->ifOp[o->foldForm] : ops->branchIf[o->foldForm]; |
2208 | 3.72k | } |
2209 | 4.28k | } |
2210 | | |
2211 | 2.80k | return NULL; |
2212 | 6.53k | } |
2213 | | #endif // d_m3FuseBranch |
2214 | | |
2215 | | static inline |
2216 | | IM3Operation Op_ContinueLoop (IM3Compilation o) |
2217 | 610 | { |
2218 | 610 | #if d_m3HasStackSwitching |
2219 | 610 | if (M3_UNLIKELY(o->module->runtime and o->module->runtime->isSuspendable)) { |
2220 | 0 | return op_ContinueLoop_Suspendable; |
2221 | 0 | } |
2222 | 610 | #endif |
2223 | 610 | return op_ContinueLoop; |
2224 | 610 | } |
2225 | | |
2226 | | static inline |
2227 | | IM3Operation Op_ContinueLoopIf (IM3Compilation o) |
2228 | 449 | { |
2229 | 449 | #if d_m3HasStackSwitching |
2230 | 449 | if (M3_UNLIKELY(o->module->runtime and o->module->runtime->isSuspendable)) { |
2231 | 0 | return op_ContinueLoopIf_Suspendable; |
2232 | 0 | } |
2233 | 449 | #endif |
2234 | 449 | return op_ContinueLoopIf; |
2235 | 449 | } |
2236 | | |
2237 | | |
2238 | | static |
2239 | | M3Result Compile_Branch (IM3Compilation o, m3opcode_t i_opcode) |
2240 | 4.01k | { |
2241 | 4.01k | M3Result result; |
2242 | 4.01k | u32 numTryFrames = 0; |
2243 | | |
2244 | 4.01k | u32 depth; |
2245 | 4.01k | _ (ReadLEB_u32(&depth, &o->wasm, o->wasmEnd)); |
2246 | | |
2247 | | // Spec: br_if condition must be i32 |
2248 | 4.01k | if (i_opcode == c_waOp_branchIf and not IsStackPolymorphic(o)) { |
2249 | 1.60k | m3type_t condType = GetStackTopType(o); |
2250 | 1.60k | _throwif(m3Err_typeMismatch, condType != c_m3Type_none and condType != c_m3Type_i32); |
2251 | 1.60k | } |
2252 | | |
2253 | 4.01k | IM3CompilationScope scope; |
2254 | 4.01k | _ (GetBlockScope(o, &scope, depth)); |
2255 | | |
2256 | 4.01k | #if d_m3HasExceptionHandling |
2257 | 4.01k | numTryFrames = NumTryFramesToPop(&o->block, scope); |
2258 | 4.01k | #endif |
2259 | | |
2260 | | // branch target is a loop (continue) |
2261 | 4.01k | if (scope->opcode == c_waOp_loop) { |
2262 | 753 | if (i_opcode == c_waOp_branchIf) { |
2263 | | // a handler pop belongs on the taken path only, so it forces the |
2264 | | // jump-over form even where the target takes no parameters |
2265 | 563 | if (GetFuncTypeNumParams(scope->type) or numTryFrames) { |
2266 | 113 | IM3Operation op = IsStackTopInRegister(o) ? op_BranchIfPrologue_r : op_BranchIfPrologue_s; |
2267 | | |
2268 | 113 | _ (EmitOp(o, op)); |
2269 | 113 | _ (EmitSlotNumOfStackTopAndPop(o)); |
2270 | | |
2271 | 112 | pc_t* jumpTo = (pc_t*)ReservePointer(o); |
2272 | | |
2273 | 112 | _ (EmitPopTryFrames(o, numTryFrames)); |
2274 | | |
2275 | 112 | if (not IsStackPolymorphic(o)) { |
2276 | 13 | _ (ResolveBlockResults(o, scope, /* isBranch: */ true)); |
2277 | 13 | } |
2278 | | |
2279 | 112 | _ (EmitOp(o, Op_ContinueLoop(o))); |
2280 | 112 | EmitPointer(o, scope->pc); |
2281 | 112 | _ (RecordBackEdge(o, scope, false)); |
2282 | | |
2283 | 112 | *jumpTo = GetPC(o); |
2284 | 450 | } else { |
2285 | | // move the condition to a register |
2286 | 450 | _ (CopyStackTopToRegister(o, false)); |
2287 | 450 | _ (PopType(o, c_m3Type_i32)); |
2288 | | |
2289 | 449 | _ (EmitOp(o, Op_ContinueLoopIf(o))); |
2290 | 449 | EmitPointer(o, scope->pc); |
2291 | 449 | _ (RecordBackEdge(o, scope, true)); |
2292 | 449 | } |
2293 | | |
2294 | | // dump_type_stack(o); |
2295 | 563 | } else // is c_waOp_branch |
2296 | 190 | { |
2297 | 190 | _ (EmitPopTryFrames(o, numTryFrames)); |
2298 | | |
2299 | | // the branch operands become the loop's parameters on the next iteration |
2300 | 190 | if (not IsStackPolymorphic(o)) { |
2301 | 69 | _ (ResolveBlockResults(o, scope, /* isBranch: */ true)); |
2302 | 68 | } |
2303 | | |
2304 | 189 | _ (EmitOp(o, Op_ContinueLoop(o))); |
2305 | 189 | EmitPointer(o, scope->pc); |
2306 | 189 | _ (RecordBackEdge(o, scope, false)); |
2307 | 189 | o->block.isPolymorphic = true; |
2308 | 189 | } |
2309 | 753 | } else // forward branch |
2310 | 3.25k | { |
2311 | 3.25k | pc_t* jumpTo = NULL; |
2312 | | |
2313 | 3.25k | bool isReturn = (scope->depth == 0); |
2314 | 3.25k | bool targetHasResults = GetFuncTypeNumResults(scope->type); |
2315 | | |
2316 | 3.25k | if (i_opcode == c_waOp_branchIf) { |
2317 | 2.40k | if (targetHasResults or isReturn or numTryFrames) { |
2318 | 981 | IM3Operation op = IsStackTopInRegister(o) ? op_BranchIfPrologue_r : op_BranchIfPrologue_s; |
2319 | | |
2320 | 981 | _ (EmitOp(o, op)); |
2321 | 981 | _ (EmitSlotNumOfStackTopAndPop(o)); // condition |
2322 | | |
2323 | | // this is continuation point, if the branch isn't taken |
2324 | 980 | jumpTo = (pc_t*)ReservePointer(o); |
2325 | 1.42k | } else { |
2326 | 1.42k | #if d_m3FuseBranch |
2327 | 1.42k | IM3Operation fuseOp = GetBranchFusionOp(o, false); |
2328 | | |
2329 | 1.42k | if (fuseOp) { |
2330 | | // absorb the branch into the compare that produced the condition |
2331 | 1.01k | *(IM3Operation*)o->foldPatchPC = fuseOp; |
2332 | 1.01k | InvalidateFold(o); |
2333 | | |
2334 | 1.01k | _ (Pop(o)); // the condition is consumed inside the fused op |
2335 | | |
2336 | 1.01k | EmitPatchingBranchPointer(o, scope); |
2337 | 1.01k | goto _catch; |
2338 | 1.01k | } |
2339 | 408 | #endif |
2340 | 408 | IM3Operation op = IsStackTopInRegister(o) ? op_BranchIf_r : op_BranchIf_s; |
2341 | | |
2342 | 408 | _ (EmitOp(o, op)); |
2343 | 408 | _ (EmitSlotNumOfStackTopAndPop(o)); // condition |
2344 | | |
2345 | 407 | EmitPatchingBranchPointer(o, scope); |
2346 | 407 | goto _catch; |
2347 | 408 | } |
2348 | 2.40k | } |
2349 | | |
2350 | 1.83k | if (not IsStackPolymorphic(o)) { |
2351 | 924 | if (isReturn) { |
2352 | 746 | _ (ReturnValues(o, scope, true)); |
2353 | 745 | _ (EmitOp(o, op_Return)); |
2354 | 745 | } else { |
2355 | 178 | _ (EmitPopTryFrames(o, numTryFrames)); |
2356 | | |
2357 | 178 | _ (ResolveBlockResults(o, scope, true)); |
2358 | 173 | _ (EmitPatchingBranch(o, scope)); |
2359 | 173 | } |
2360 | 924 | } |
2361 | | |
2362 | 1.82k | if (jumpTo) { |
2363 | 976 | *jumpTo = GetPC(o); |
2364 | 976 | } |
2365 | | |
2366 | 1.82k | if (i_opcode == c_waOp_branch) { |
2367 | 852 | _ (SetStackPolymorphic(o)); |
2368 | 848 | } |
2369 | 1.82k | } |
2370 | | |
2371 | 4.01k | _catch: return result; |
2372 | 4.01k | } |
2373 | | |
2374 | | static |
2375 | | M3Result Compile_BranchTable (IM3Compilation o, m3opcode_t i_opcode) |
2376 | 715 | { |
2377 | | // the page a continue-op page is standing in for; non-NULL only while that |
2378 | | // page is installed in o->page, so the catch below knows to put it back |
2379 | 715 | IM3CodePage displacedPage = NULL; |
2380 | | // indexed by label depth: the continuation stub already emitted for that target |
2381 | | // within this table, or NULL if this table has not reached it yet |
2382 | 715 | pc_t* targetStubs = NULL; |
2383 | 715 | _try { |
2384 | 715 | u32 targetCount; |
2385 | 715 | _ (ReadLEB_u32(&targetCount, &o->wasm, o->wasmEnd)); |
2386 | | |
2387 | | // Spec: validate that the branch index operand is i32 |
2388 | 715 | if (not IsStackPolymorphic(o)) { |
2389 | 216 | m3type_t indexType = GetStackTopType(o); |
2390 | 216 | _throwif(m3Err_typeMismatch, indexType != c_m3Type_none and indexType != c_m3Type_i32); |
2391 | 216 | } |
2392 | | |
2393 | 715 | _ (PreserveRegisterIfOccupied(o, c_m3Type_i64)); // move branch operand to a slot |
2394 | 714 | u16 slot = GetStackTopSlotNumber(o); |
2395 | 714 | _ (Pop(o)); |
2396 | | |
2397 | | // OPTZ: according to spec: "forward branches that target a control instruction with a non-empty |
2398 | | // result type consume matching operands first and push them back on the operand stack after unwinding" |
2399 | | // So, this move-to-reg is only necessary if the target scopes have a type. |
2400 | | |
2401 | 712 | u32 numCodeLines = targetCount + 4; // 3 => IM3Operation + slot + target_count + default_target |
2402 | 712 | _ (EnsureCodePageNumLines(o, numCodeLines)); |
2403 | | |
2404 | 712 | _ (EmitOp(o, op_BranchTable)); |
2405 | 712 | EmitSlotOffset(o, slot); |
2406 | 712 | EmitConstant32(o, targetCount); |
2407 | | |
2408 | 712 | IM3CodePage continueOpPage = NULL; |
2409 | | |
2410 | | // The label types are checked by ValidateFunction, which runs under the same |
2411 | | // d_m3EnableValidation guard just before this compilation pass. Comparing each |
2412 | | // target against the default here would be wrong anyway: the spec matches every |
2413 | | // target against the operand stack, so in unreachable code the operands are the |
2414 | | // bottom type and the targets may legitimately differ (unreached-valid.wast). |
2415 | | |
2416 | 712 | ++targetCount; // include default |
2417 | | |
2418 | | // Every entry of one br_table is resolved from the same compilation state - each |
2419 | | // stub restores whatever it touched - so two entries naming the same label emit |
2420 | | // byte-identical epilogues. Emit one and point both table entries at it. A label |
2421 | | // deeper than the current block is rejected by GetBlockScope below, which is what |
2422 | | // bounds this array. |
2423 | 712 | targetStubs = m3_AllocArray(pc_t, (size_t)o->block.depth + 1); |
2424 | 712 | _throwifnull(targetStubs); |
2425 | | |
2426 | 1.94k | for (u32 i = 0; i < targetCount; ++i) { |
2427 | 1.23k | u32 target; |
2428 | 1.23k | _ (ReadLEB_u32(&target, &o->wasm, o->wasmEnd)); |
2429 | | |
2430 | 1.23k | IM3CompilationScope scope; |
2431 | 1.23k | _ (GetBlockScope(o, &scope, target)); |
2432 | | |
2433 | 1.23k | if (targetStubs[target]) { |
2434 | 216 | EmitPointer(o, targetStubs[target]); |
2435 | 216 | continue; |
2436 | 216 | } |
2437 | | |
2438 | 1.01k | u32 numTryFrames = 0; |
2439 | 1.01k | #if d_m3HasExceptionHandling |
2440 | 1.01k | numTryFrames = NumTryFramesToPop(&o->block, scope); |
2441 | 1.01k | #endif |
2442 | | |
2443 | | // A forward branch to a block that carries no results and leaves no try |
2444 | | // frames behind has nothing to do on the way out but arrive. There is no |
2445 | | // epilogue to hold, so the table entry becomes the patch site itself and the |
2446 | | // block's address is written into it when the block ends - no stub, and no |
2447 | | // code page to put one on. In unreachable code the same is true whatever the |
2448 | | // block's type, since nothing is emitted for that case either way. |
2449 | | // |
2450 | | // Each such entry is patched on its own, so none is recorded in targetStubs: |
2451 | | // there is no shared stub for a later entry naming this label to point at. |
2452 | 1.01k | if (scope->opcode != c_waOp_loop and scope->depth > 0 and numTryFrames == 0 and |
2453 | 290 | (IsStackPolymorphic(o) or GetFuncTypeNumResults(scope->type) == 0)) { |
2454 | 276 | EmitPatchingBranchPointer(o, scope); |
2455 | 276 | continue; |
2456 | 276 | } |
2457 | | |
2458 | 741 | _ (AcquireCompilationCodePage(o, &continueOpPage)); |
2459 | | |
2460 | 741 | pc_t startPC = GetPagePC(continueOpPage); |
2461 | 741 | displacedPage = o->page; |
2462 | 741 | o->page = continueOpPage; |
2463 | | |
2464 | 741 | if (scope->opcode == c_waOp_loop) { |
2465 | 141 | _ (EmitPopTryFrames(o, numTryFrames)); |
2466 | | |
2467 | 141 | if (not IsStackPolymorphic(o)) { |
2468 | 24 | _ (ResolveBlockResults(o, scope, true)); |
2469 | 24 | } |
2470 | | |
2471 | 141 | _ (EmitOp(o, Op_ContinueLoop(o))); |
2472 | 141 | EmitPointer(o, scope->pc); |
2473 | 141 | _ (RecordBackEdge(o, scope, false)); |
2474 | 600 | } else { |
2475 | 600 | if (not IsStackPolymorphic(o)) { |
2476 | 198 | if (scope->depth == 0) { |
2477 | 175 | _ (ReturnValues(o, scope, true)); |
2478 | 174 | _ (EmitOp(o, op_Return)); |
2479 | 174 | } else { |
2480 | 23 | _ (EmitPopTryFrames(o, numTryFrames)); |
2481 | | |
2482 | 23 | _ (ResolveBlockResults(o, scope, true)); |
2483 | | |
2484 | 20 | _ (EmitPatchingBranch(o, scope)); |
2485 | 20 | } |
2486 | 198 | } |
2487 | 600 | } |
2488 | | |
2489 | 737 | IM3CodePage returnTo = displacedPage; |
2490 | 737 | displacedPage = NULL; |
2491 | | |
2492 | 737 | ReleaseCompilationCodePage(o); |
2493 | 737 | o->page = returnTo; |
2494 | | |
2495 | 737 | EmitPointer(o, startPC); |
2496 | 737 | targetStubs[target] = startPC; |
2497 | 737 | } |
2498 | | |
2499 | 708 | _ (SetStackPolymorphic(o)); |
2500 | 715 | } _catch: |
2501 | | |
2502 | | // thrown out of the loop above with a continue-op page installed: release it |
2503 | | // and restore the one it displaced, which the caller's catch then releases. |
2504 | | // Otherwise the displaced page is on no list and simply leaks. |
2505 | 715 | if (displacedPage) { |
2506 | 4 | ReleaseCompilationCodePage(o); |
2507 | 4 | o->page = displacedPage; |
2508 | 4 | } |
2509 | | |
2510 | 715 | m3_Free(targetStubs); |
2511 | | |
2512 | 715 | return result; |
2513 | 707 | } |
2514 | | |
2515 | | static |
2516 | | M3Result CompileCallArgsAndReturn (IM3Compilation o, u16* o_stackOffset, IM3FuncType i_type, bool i_isIndirect) |
2517 | 11.1k | { |
2518 | 11.1k | _try { |
2519 | | |
2520 | 11.1k | u16 topSlot = GetMaxUsedSlotPlusOne(o); |
2521 | | |
2522 | | // force use of at least one stack slot; this is to help ensure |
2523 | | // the m3 stack overflows (and traps) before the native stack can overflow. |
2524 | | // e.g. see Wasm spec test 'runaway' in call.wast |
2525 | 11.1k | topSlot = M3_MAX(1, topSlot); |
2526 | | |
2527 | | // stack frame is 64-bit aligned |
2528 | 11.1k | AlignSlotToType(&topSlot, c_m3Type_i64); |
2529 | | |
2530 | 11.1k | *o_stackOffset = topSlot; |
2531 | | |
2532 | | // wait to pop this here so that topSlot search is correct |
2533 | 11.1k | if (i_isIndirect) { |
2534 | 922 | _ (Pop(o)); |
2535 | 920 | } |
2536 | | |
2537 | 11.1k | u16 numArgs = GetFuncTypeNumParams(i_type); |
2538 | 11.1k | u16 numRets = GetFuncTypeNumResults(i_type); |
2539 | | |
2540 | 11.1k | u16 argTop = topSlot + (numArgs + numRets) * c_ioSlotCount; |
2541 | | |
2542 | 11.1k | TouchSlot(o, argTop - 1); |
2543 | | |
2544 | 13.8k | while (numArgs--) { |
2545 | 2.71k | _ (CopyStackTopToSlot(o, argTop -= c_ioSlotCount)); |
2546 | 2.71k | _ (Pop(o)); |
2547 | 2.70k | } |
2548 | | |
2549 | 11.1k | u16 i = 0; |
2550 | 32.3k | while (numRets--) { |
2551 | 21.2k | m3type_t type = GetFuncTypeResultType(i_type, i++); |
2552 | | |
2553 | 21.2k | _ (Push(o, type, topSlot)); |
2554 | 21.2k | _ (MarkSlotsAllocatedByType(o, topSlot, type)); |
2555 | | |
2556 | 21.2k | topSlot += c_ioSlotCount; |
2557 | 21.2k | } |
2558 | | |
2559 | 11.1k | } _catch: return result; |
2560 | 11.1k | } |
2561 | | |
2562 | | // A tail call hands the current frame over to the callee instead of stacking a new one. |
2563 | | // The callee's results have to be the enclosing function's results, so both use the same |
2564 | | // return slots and only the arguments move: they're staged above the caller's stack here |
2565 | | // (the argument expressions can still be reading the args/locals they're about to land on) |
2566 | | // and slid down onto the frame base at runtime, by op_ReturnCall[Indirect]. |
2567 | | static |
2568 | | M3Result CompileTailCallArgs (IM3Compilation o, u16* o_stackOffset, u16* o_numArgSlots, IM3FuncType i_type, bool i_isIndirect) |
2569 | 12.9k | { |
2570 | 12.9k | _try { |
2571 | 12.9k | IM3FuncType funcType = o->function->funcType; |
2572 | | |
2573 | 12.9k | u16 numRets = GetFuncTypeNumResults(i_type); |
2574 | 12.9k | _throwif(m3Err_typeMismatch, numRets != GetFuncTypeNumResults(funcType)); |
2575 | | |
2576 | 15.0k | for (u16 i = 0; i < numRets; ++i) { |
2577 | 2.18k | _throwif(m3Err_typeMismatch, GetFuncTypeResultType(i_type, i) != GetFuncTypeResultType(funcType, i)); |
2578 | 2.18k | } |
2579 | | |
2580 | 12.9k | u16 topSlot = GetMaxUsedSlotPlusOne(o); |
2581 | | |
2582 | 12.9k | topSlot = M3_MAX(1, topSlot); |
2583 | | |
2584 | | // stack frame is 64-bit aligned |
2585 | 12.9k | AlignSlotToType(&topSlot, c_m3Type_i64); |
2586 | | |
2587 | 12.9k | *o_stackOffset = topSlot; |
2588 | | |
2589 | | // wait to pop this here so that topSlot search is correct |
2590 | 12.9k | if (i_isIndirect) { |
2591 | 843 | _ (Pop(o)); |
2592 | 841 | } |
2593 | | |
2594 | 12.9k | u16 numArgs = GetFuncTypeNumParams(i_type); |
2595 | 12.9k | u16 numArgSlots = numArgs * c_ioSlotCount; |
2596 | | |
2597 | 12.9k | *o_numArgSlots = numArgSlots; |
2598 | | |
2599 | 12.9k | u16 argTop = topSlot + numArgSlots; |
2600 | 12.9k | _throwif(m3Err_functionStackOverflow, argTop > d_m3MaxFunctionSlots); |
2601 | | |
2602 | | // the staging area is written by the caller, so op_Entry has to account for it |
2603 | 12.9k | TouchSlot(o, argTop - 1); |
2604 | | |
2605 | 15.3k | while (numArgs--) { |
2606 | 2.45k | _ (CopyStackTopToSlot(o, argTop -= c_ioSlotCount)); |
2607 | 2.45k | _ (Pop(o)); |
2608 | 2.44k | } |
2609 | | |
2610 | 12.9k | } _catch: return result; |
2611 | 12.9k | } |
2612 | | |
2613 | | |
2614 | | #if d_m3HasTypedRefs |
2615 | | |
2616 | | // call_ref $t: the callee is the reference on top of the stack, so the operand |
2617 | | // order is the same as call_indirect's, minus the table. |
2618 | | static |
2619 | | M3Result Compile_CallRef (IM3Compilation o, m3opcode_t i_opcode) |
2620 | 1.37k | { |
2621 | 1.37k | _try { |
2622 | 1.37k | u32 typeIndex; |
2623 | 1.37k | _ (ReadLEB_u32(&typeIndex, &o->wasm, o->wasmEnd)); |
2624 | | |
2625 | 1.37k | _throwif("function call type index out of range", typeIndex >= o->module->numFuncTypes); |
2626 | | |
2627 | 1.37k | IM3FuncType type = o->module->funcTypes[typeIndex]; |
2628 | | |
2629 | 1.37k | if (not IsStackPolymorphic(o)) { |
2630 | | // whatever shape the reference has, it must be a function of this type |
2631 | 109 | m3type_t refType = GetStackTopType(o); |
2632 | 109 | _throwif(m3Err_typeMismatch, not IsSubTypeOf(refType, RefTypeOfFuncType(type, false))); |
2633 | 104 | } |
2634 | | |
2635 | 1.37k | if (IsStackTopInRegister(o)) { |
2636 | 323 | _ (PreserveRegisterIfOccupied(o, c_m3Type_funcref)); |
2637 | 322 | } |
2638 | | |
2639 | 1.37k | u16 functionSlot = GetStackTopSlotNumber(o); |
2640 | | |
2641 | 1.37k | bool isReturnCall = (i_opcode == c_waOp_returnCallRef); |
2642 | 1.37k | bool useTailCall = isReturnCall and d_m3CanTailCall; |
2643 | | |
2644 | 1.37k | u16 execTop, numArgSlots = 0; |
2645 | | |
2646 | 1.37k | if (useTailCall) { |
2647 | 648 | _ (CompileTailCallArgs(o, &execTop, &numArgSlots, type, true)); |
2648 | 724 | } else { |
2649 | 724 | _ (CompileCallArgsAndReturn(o, &execTop, type, true)); |
2650 | 723 | } |
2651 | | |
2652 | 1.37k | # if d_m3HasExceptionHandling |
2653 | 1.37k | if (isReturnCall) { |
2654 | 647 | _ (EmitPopTryFramesForReturnCall(o)); |
2655 | 647 | } |
2656 | 1.37k | # endif |
2657 | | |
2658 | 1.37k | _ (EmitOp(o, useTailCall ? op_ReturnCallRef : op_CallRef)); |
2659 | 1.37k | EmitSlotOffset(o, functionSlot); |
2660 | 1.37k | EmitPointer(o, type); |
2661 | 1.37k | EmitSlotOffset(o, execTop); |
2662 | | |
2663 | 1.37k | if (not useTailCall) { |
2664 | 723 | _ (RecordCall(o, GetFuncTypeNumResults(type), execTop, isReturnCall)); |
2665 | 723 | } |
2666 | | |
2667 | 1.37k | if (useTailCall) { |
2668 | 647 | EmitSlotOffset(o, o->function->numRetSlots); |
2669 | 647 | EmitConstant32(o, numArgSlots); |
2670 | | |
2671 | 647 | _ (SetStackPolymorphic(o)); |
2672 | 723 | } else if (isReturnCall) { |
2673 | 0 | _ (Compile_Return(o, i_opcode)); |
2674 | 0 | } |
2675 | | |
2676 | 1.37k | } _catch: |
2677 | 1.37k | return result; |
2678 | 1.37k | } |
2679 | | |
2680 | | |
2681 | | // ref.as_non_null only sharpens the type; the value is unchanged, so the stack |
2682 | | // entry is re-pushed with the null stripped out of its type. |
2683 | | static |
2684 | | M3Result Compile_Ref_AsNonNull (IM3Compilation o, m3opcode_t i_opcode) |
2685 | 415 | { |
2686 | 415 | M3Result result = m3Err_none; |
2687 | | |
2688 | 415 | if (not IsStackPolymorphic(o)) { |
2689 | 241 | m3type_t type = GetStackTopType(o); |
2690 | 241 | _throwif(m3Err_typeMismatch, not IsRefType(type)); |
2691 | | |
2692 | | // op_RefAsNonNull only knows how to read its operand out of a slot; a |
2693 | | // value still sitting in the register has to be spilled first, the way |
2694 | | // every other single-op reader of the stack top does |
2695 | 241 | if (IsStackTopInRegister(o)) { |
2696 | 12 | _ (PreserveRegisterIfOccupied(o, type)); |
2697 | 12 | } |
2698 | | |
2699 | 241 | u16 slot = GetStackTopSlotNumber(o); |
2700 | | |
2701 | 241 | _ (EmitOp(o, op_RefAsNonNull)); |
2702 | 241 | EmitSlotOffset(o, slot); |
2703 | | |
2704 | 241 | m3type_t nonNull = IsSpelledRefType(type) |
2705 | 241 | ? (m3type_t)(type | d_m3Type_refNonNull) |
2706 | 241 | : (m3type_t)(d_m3Type_ref | d_m3Type_refNonNull | d_m3Type_heapAbstract | |
2707 | 39 | ((BaseTypeOf(type) == c_m3Type_externref) ? d_m3Type_refExtern : 0)); |
2708 | | |
2709 | | // the slot keeps the one reference it already had; a Pop()/Push() of |
2710 | | // the same slot would drop that reference's count without anything |
2711 | | // to put it back, so the type above it is corrected in place instead |
2712 | 241 | o->typeStack[GetStackTopIndex(o)] = nonNull; |
2713 | 241 | } |
2714 | | |
2715 | 415 | _catch: return result; |
2716 | 415 | } |
2717 | | |
2718 | | #endif // d_m3HasTypedRefs |
2719 | | |
2720 | | |
2721 | | // How wide a table operand is read at comes from the table it names, so a slot |
2722 | | // holding a narrower type would be read past what was allocated for it. The |
2723 | | // validator refuses that already; this keeps a build without one from reaching |
2724 | | // the interpreter with it. c_m3Type_none means the operand is not on the |
2725 | | // dynamic stack to inspect, which is not this check's business. |
2726 | | static |
2727 | | M3Result CheckOperandType (IM3Compilation o, u16 i_depthFromTop, m3type_t i_type) |
2728 | 2.46k | { |
2729 | 2.46k | m3type_t actual; |
2730 | | |
2731 | 2.46k | if (IsStackPolymorphic(o)) { |
2732 | 630 | return m3Err_none; |
2733 | 630 | } |
2734 | | |
2735 | 1.83k | actual = GetStackTypeFromTop(o, i_depthFromTop); |
2736 | | |
2737 | 1.83k | return (actual == c_m3Type_none or BaseTypeOf(actual) == i_type) ? m3Err_none |
2738 | 1.83k | : m3Err_typeMismatch; |
2739 | 2.46k | } |
2740 | | |
2741 | | |
2742 | | // Swaps the _mem register onto a given memory. Only ever emitted in pairs -- |
2743 | | // see op_SetMemory. |
2744 | | static |
2745 | | M3Result EmitSetMemoryPtr (IM3Compilation o, IM3Memory i_memory) |
2746 | 354 | { |
2747 | 354 | M3Result result = m3Err_none; |
2748 | | |
2749 | 354 | _ (EmitOp(o, op_SetMemory)); |
2750 | 354 | EmitPointer(o, i_memory); |
2751 | | |
2752 | 354 | _catch: return result; |
2753 | 354 | } |
2754 | | |
2755 | | |
2756 | | // ...onto one of the compiling module's own memories, by index. Only reached |
2757 | | // with a non-zero index under multi-memory, but the index has already been |
2758 | | // bounds-checked by ReadMemoryIndex either way. |
2759 | | static inline |
2760 | | M3Result EmitSetMemory (IM3Compilation o, u32 i_memoryIdx) |
2761 | 354 | { |
2762 | 354 | #if d_m3HasMultiMemory |
2763 | 354 | return EmitSetMemoryPtr(o, o->module->memories[i_memoryIdx]); |
2764 | | #else |
2765 | | (void)o; |
2766 | | (void)i_memoryIdx; |
2767 | | return m3Err_unknownMemory; |
2768 | | #endif |
2769 | 354 | } |
2770 | | |
2771 | | static |
2772 | | M3Result Compile_Call (IM3Compilation o, m3opcode_t i_opcode) |
2773 | 22.2k | { |
2774 | 22.2k | _try { |
2775 | 22.2k | u32 functionIndex; |
2776 | 22.2k | _ (ReadLEB_u32(&functionIndex, &o->wasm, o->wasmEnd)); |
2777 | | |
2778 | 22.2k | IM3Function function = Module_GetFunction(o->module, functionIndex); |
2779 | | |
2780 | 22.2k | if (function) { m3log (compile, d_indent " (func= [%d] '%s'; args= %d)", |
2781 | 22.2k | get_indention_string (o), functionIndex, m3_GetFunctionName (function), function->funcType->numArgs); |
2782 | | // an import linked to another module's export runs that module's body |
2783 | 22.2k | IM3Function target = Function_Implementation(function); |
2784 | | |
2785 | 22.2k | if (target->module) { |
2786 | 22.2k | bool isReturnCall = (i_opcode == c_waOp_returnCall); |
2787 | | |
2788 | | // The callee's body runs against its own module's memory, so a call |
2789 | | // that crosses modules is bracketed by a pair of op_SetMemory. A |
2790 | | // tail call never returns to run the second one, so those compile |
2791 | | // as a plain call followed by a return instead. |
2792 | 22.2k | bool crossModule = (target->module != o->module); |
2793 | 22.2k | bool useTailCall = isReturnCall and d_m3CanTailCall and not crossModule; |
2794 | | |
2795 | 22.2k | u16 slotTop, numArgSlots = 0; |
2796 | | |
2797 | 22.2k | if (useTailCall) { |
2798 | 12.0k | _ (CompileTailCallArgs(o, &slotTop, &numArgSlots, target->funcType, false)); |
2799 | 12.0k | } else { |
2800 | 10.2k | _ (CompileCallArgsAndReturn(o, &slotTop, target->funcType, false)); |
2801 | 10.1k | } |
2802 | | |
2803 | 22.2k | IM3Operation op; |
2804 | 22.2k | const void* operand; |
2805 | | |
2806 | 22.2k | if (target->compiled) { |
2807 | 83 | op = useTailCall ? op_ReturnCall : op_Call; |
2808 | 83 | operand = target->compiled; |
2809 | 22.1k | } else { |
2810 | 22.1k | op = useTailCall ? op_CompileReturnCall : op_Compile; |
2811 | 22.1k | operand = target; |
2812 | 22.1k | } |
2813 | | |
2814 | 22.2k | #if d_m3HasExceptionHandling |
2815 | 22.2k | if (isReturnCall) { |
2816 | 12.0k | _ (EmitPopTryFramesForReturnCall(o)); |
2817 | 12.0k | } |
2818 | 22.2k | #endif |
2819 | | |
2820 | 22.2k | if (crossModule) { |
2821 | 0 | _ (EmitSetMemoryPtr(o, Module_Memory0(target->module))); |
2822 | 0 | } |
2823 | | |
2824 | 22.2k | _ (EmitOp(o, op)); |
2825 | 22.2k | EmitPointer(o, operand); |
2826 | 22.2k | EmitSlotOffset(o, slotTop); |
2827 | | |
2828 | 22.2k | if (not useTailCall) { |
2829 | | // a suspend inside the callee leaves this frame waiting here, its |
2830 | | // results not yet written |
2831 | 10.1k | _ (RecordCall(o, GetFuncTypeNumResults(target->funcType), slotTop, isReturnCall)); |
2832 | 10.1k | } |
2833 | | |
2834 | 22.2k | if (useTailCall) { |
2835 | 12.0k | EmitSlotOffset(o, o->function->numRetSlots); |
2836 | 12.0k | EmitConstant32(o, numArgSlots); |
2837 | | |
2838 | 12.0k | _ (SetStackPolymorphic(o)); |
2839 | 12.0k | } else { |
2840 | 10.1k | if (crossModule) { |
2841 | 0 | _ (EmitSetMemoryPtr(o, Module_Memory0(o->module))); |
2842 | 0 | } |
2843 | | |
2844 | 10.1k | if (isReturnCall) { |
2845 | 0 | _ (Compile_Return(o, i_opcode)); |
2846 | 0 | } |
2847 | 10.1k | } |
2848 | 22.2k | } else { |
2849 | 0 | _throw(ErrorCompile(m3Err_functionImportMissing, o, "'%s.%s'", GetFunctionImportModuleName(function), m3_GetFunctionName(function))); |
2850 | 0 | } |
2851 | 22.2k | } else { |
2852 | 0 | _throw(ErrorCompile(m3Err_functionLookupFailed, o, "index: %d", functionIndex)); |
2853 | 0 | } |
2854 | | |
2855 | 22.2k | } _catch: return result; |
2856 | 22.2k | } |
2857 | | |
2858 | | static |
2859 | | M3Result Compile_CallIndirect (IM3Compilation o, m3opcode_t i_opcode) |
2860 | 393 | { |
2861 | 393 | _try { |
2862 | 393 | u32 typeIndex; |
2863 | 393 | _ (ReadLEB_u32(&typeIndex, &o->wasm, o->wasmEnd)); |
2864 | | |
2865 | 393 | u32 tableIndex; |
2866 | 393 | _ (ReadLEB_u32(&tableIndex, &o->wasm, o->wasmEnd)); |
2867 | | |
2868 | 393 | _throwif("function call type index out of range", typeIndex >= o->module->numFuncTypes); |
2869 | 393 | _throwif("table index out of range", tableIndex >= o->module->numTables); |
2870 | 393 | _throwif(m3Err_typeMismatch, BaseTypeOf(o->module->tables[tableIndex]->type) != c_m3Type_funcref); |
2871 | | |
2872 | 393 | _ (CheckOperandType(o, 0, Table_AddrType(o->module->tables[tableIndex]))); |
2873 | | |
2874 | 393 | if (IsStackTopInRegister(o)) { |
2875 | 106 | _ (PreserveRegisterIfOccupied(o, c_m3Type_i32)); |
2876 | 106 | } |
2877 | | |
2878 | 393 | u16 tableIndexSlot = GetStackTopSlotNumber(o); |
2879 | | |
2880 | 393 | bool isReturnCall = (i_opcode == c_waOp_returnCallIndirect); |
2881 | 393 | bool useTailCall = isReturnCall and d_m3CanTailCall; |
2882 | | |
2883 | 393 | u16 execTop, numArgSlots = 0; |
2884 | 393 | IM3FuncType type = o->module->funcTypes[typeIndex]; |
2885 | | |
2886 | 393 | if (useTailCall) { |
2887 | 195 | _ (CompileTailCallArgs(o, &execTop, &numArgSlots, type, true)); |
2888 | 198 | } else { |
2889 | 198 | _ (CompileCallArgsAndReturn(o, &execTop, type, true)); |
2890 | 197 | } |
2891 | | |
2892 | 391 | #if d_m3HasExceptionHandling |
2893 | 391 | if (isReturnCall) { |
2894 | 194 | _ (EmitPopTryFramesForReturnCall(o)); |
2895 | 194 | } |
2896 | 391 | #endif |
2897 | | |
2898 | | // the table comes first: how wide its indexes are is what says how much of |
2899 | | // the slot below holds one |
2900 | 391 | _ (EmitOp(o, useTailCall ? op_ReturnCallIndirect : op_CallIndirect)); |
2901 | 391 | EmitPointer(o, o->module->tables[tableIndex]); |
2902 | 391 | EmitSlotOffset(o, tableIndexSlot); |
2903 | 391 | EmitPointer(o, type); |
2904 | 391 | EmitSlotOffset(o, execTop); |
2905 | | |
2906 | 391 | if (not useTailCall) { |
2907 | 197 | _ (RecordCall(o, GetFuncTypeNumResults(type), execTop, isReturnCall)); |
2908 | 197 | } |
2909 | | |
2910 | 391 | if (useTailCall) { |
2911 | 194 | EmitSlotOffset(o, o->function->numRetSlots); |
2912 | 194 | EmitConstant32(o, numArgSlots); |
2913 | | |
2914 | 194 | _ (SetStackPolymorphic(o)); |
2915 | 197 | } else if (isReturnCall) { |
2916 | 0 | _ (Compile_Return(o, i_opcode)); |
2917 | 0 | } |
2918 | | |
2919 | 393 | } _catch: |
2920 | 393 | return result; |
2921 | 391 | } |
2922 | | |
2923 | | // Reads the memory index a memory instruction names, and checks it against the |
2924 | | // module's index space. Without multi-memory the immediate is a reserved byte |
2925 | | // that has to be zero, which is the same check. |
2926 | | static |
2927 | | M3Result ReadMemoryIndex (IM3Compilation o, u32* o_memoryIdx) |
2928 | 4.92k | { |
2929 | 4.92k | M3Result result; |
2930 | | |
2931 | 4.92k | _ (ReadLEB_u32(o_memoryIdx, &o->wasm, o->wasmEnd)); |
2932 | | |
2933 | 4.92k | _throwif(m3Err_unknownMemory, *o_memoryIdx >= o->module->numMemories); |
2934 | | |
2935 | 4.92k | _catch: return result; |
2936 | 4.92k | } |
2937 | | |
2938 | | static |
2939 | | M3Result Compile_Memory_Size (IM3Compilation o, m3opcode_t i_opcode) |
2940 | 1.90k | { |
2941 | 1.90k | M3Result result; |
2942 | | |
2943 | 1.90k | u32 memoryIdx; |
2944 | | |
2945 | | // A page count is given in the memory's own address type. Declared up here |
2946 | | // so the throws below don't jump over its initialization. |
2947 | 1.90k | m3type_t addrType = c_m3Type_i32; |
2948 | | |
2949 | 1.90k | _ (ReadMemoryIndex(o, &memoryIdx)); |
2950 | | |
2951 | 1.90k | addrType = Memory_AddrType(o->module->memories[memoryIdx]); |
2952 | | |
2953 | 1.90k | _ (PreserveRegisterIfOccupied(o, addrType)); |
2954 | | |
2955 | 1.90k | if (memoryIdx) { |
2956 | 44 | _ (EmitSetMemory(o, memoryIdx)); |
2957 | 44 | } |
2958 | | |
2959 | | // op_MemSize writes the whole register either way; the slot it is pushed |
2960 | | // to is what decides how much of it the module gets to see |
2961 | 1.90k | _ (EmitOp(o, op_MemSize)); |
2962 | | |
2963 | 1.90k | if (memoryIdx) { |
2964 | 44 | _ (EmitSetMemory(o, 0)); |
2965 | 44 | } |
2966 | | |
2967 | 1.90k | _ (PushRegister(o, addrType)); |
2968 | | |
2969 | 1.90k | _catch: return result; |
2970 | 1.90k | } |
2971 | | |
2972 | | static |
2973 | | M3Result Compile_Memory_Grow (IM3Compilation o, m3opcode_t i_opcode) |
2974 | 1.63k | { |
2975 | 1.63k | M3Result result; |
2976 | | |
2977 | 1.63k | u32 memoryIdx; |
2978 | | |
2979 | | // see Compile_Memory_Size |
2980 | 1.63k | m3type_t addrType = c_m3Type_i32; |
2981 | | |
2982 | 1.63k | _ (ReadMemoryIndex(o, &memoryIdx)); |
2983 | | |
2984 | 1.63k | addrType = Memory_AddrType(o->module->memories[memoryIdx]); |
2985 | | |
2986 | 1.63k | _ (CopyStackTopToRegister(o, false)); |
2987 | 1.63k | _ (PopType(o, addrType)); |
2988 | | |
2989 | 1.62k | if (memoryIdx) { |
2990 | 44 | _ (EmitSetMemory(o, memoryIdx)); |
2991 | 44 | } |
2992 | | |
2993 | 1.62k | #if d_m3HasMemory64 |
2994 | 1.62k | _ (EmitOp(o, (addrType == c_m3Type_i64) ? op_MemGrow64 : op_MemGrow)); |
2995 | | #else |
2996 | | _ (EmitOp(o, op_MemGrow)); |
2997 | | #endif |
2998 | | |
2999 | 1.62k | if (memoryIdx) { |
3000 | 44 | _ (EmitSetMemory(o, 0)); |
3001 | 44 | } |
3002 | | |
3003 | 1.62k | _ (PushRegister(o, addrType)); |
3004 | | |
3005 | 1.63k | _catch: return result; |
3006 | 1.62k | } |
3007 | | |
3008 | | static |
3009 | | M3Result Compile_Memory_CopyFill (IM3Compilation o, m3opcode_t i_opcode) |
3010 | 819 | { |
3011 | 819 | M3Result result = m3Err_none; |
3012 | | |
3013 | | // memory.copy names the destination first and then the source; memory.fill |
3014 | | // names only the one it writes. |
3015 | 819 | u32 sourceMemoryIdx = 0, targetMemoryIdx = 0; |
3016 | 819 | bool isCopy = (i_opcode == c_waOp_memoryCopy); |
3017 | | |
3018 | | // Each address is typed by the memory it belongs to. The length follows |
3019 | | // the narrower of the two, so it is 64-bit only when both are - which for |
3020 | | // memory.fill, naming one memory twice over, means whenever that one is. |
3021 | | // Declared up here so the throws below don't jump over them. |
3022 | 819 | m3type_t targetType = c_m3Type_i32; |
3023 | 819 | m3type_t sourceType = c_m3Type_i32; |
3024 | 819 | m3type_t lengthType = c_m3Type_i32; |
3025 | | |
3026 | 819 | _throwif(m3Err_wasmMalformed, o->module->numMemories == 0); |
3027 | | |
3028 | 819 | _ (ReadMemoryIndex(o, &targetMemoryIdx)); |
3029 | | |
3030 | 819 | if (isCopy) { |
3031 | 566 | _ (ReadMemoryIndex(o, &sourceMemoryIdx)); |
3032 | 566 | } |
3033 | | |
3034 | 819 | targetType = Memory_AddrType(o->module->memories[targetMemoryIdx]); |
3035 | 819 | sourceType = isCopy ? Memory_AddrType(o->module->memories[sourceMemoryIdx]) |
3036 | 819 | : targetType; |
3037 | 819 | lengthType = (targetType == c_m3Type_i64 and sourceType == c_m3Type_i64) |
3038 | 819 | ? c_m3Type_i64 |
3039 | 819 | : c_m3Type_i32; |
3040 | | |
3041 | 819 | _ (CopyStackTopToRegister(o, false)); |
3042 | | |
3043 | 818 | #if d_m3HasMultiMemory |
3044 | 818 | if (isCopy and sourceMemoryIdx != targetMemoryIdx) { |
3045 | | // two memories at once: _mem can only name one, so the op takes both |
3046 | 174 | _ (EmitOp(o, op_MemCopy_x)); |
3047 | 174 | EmitPointer(o, o->module->memories[targetMemoryIdx]); |
3048 | 174 | EmitPointer(o, o->module->memories[sourceMemoryIdx]); |
3049 | | |
3050 | | // ...and, since they need not be addressed alike, how wide to read |
3051 | | // each of the operands: bit 0 the destination, bit 1 the source |
3052 | 174 | EmitConstant32(o, ((targetType == c_m3Type_i64) ? 0x1u : 0u) | |
3053 | 174 | ((sourceType == c_m3Type_i64) ? 0x2u : 0u)); |
3054 | 174 | } else |
3055 | 644 | #endif |
3056 | 644 | { |
3057 | 644 | IM3Operation op = isCopy ? op_MemCopy : op_MemFill; |
3058 | | |
3059 | 644 | #if d_m3HasMemory64 |
3060 | 644 | if (targetType == c_m3Type_i64) { |
3061 | 466 | op = isCopy ? op_MemCopy64 : op_MemFill64; |
3062 | 466 | } |
3063 | 644 | #endif |
3064 | | |
3065 | 644 | if (targetMemoryIdx) { |
3066 | 55 | _ (EmitSetMemory(o, targetMemoryIdx)); |
3067 | 55 | } |
3068 | | |
3069 | 644 | _ (EmitOp(o, op)); |
3070 | 644 | } |
3071 | | |
3072 | | // the length is in the register; the other two are named by slot, at |
3073 | | // whatever width their stack entries were pushed with |
3074 | 818 | _ (PopType(o, lengthType)); |
3075 | 817 | _ (EmitSlotNumOfStackTopAndPop(o)); // the source, or the byte to fill with |
3076 | 816 | _ (EmitSlotNumOfStackTopAndPop(o)); // the destination |
3077 | | |
3078 | 815 | if (targetMemoryIdx and not (isCopy and sourceMemoryIdx != targetMemoryIdx)) { |
3079 | 55 | _ (EmitSetMemory(o, 0)); |
3080 | 55 | } |
3081 | | |
3082 | 819 | _catch: return result; |
3083 | 815 | } |
3084 | | |
3085 | | |
3086 | | // memory.init and data.drop address a data segment by index. Both are only valid |
3087 | | // when a data count section declared the segments up front. |
3088 | | static |
3089 | | M3Result ReadDataSegment (IM3Compilation o, M3DataSegment** o_segment) |
3090 | 0 | { |
3091 | 0 | M3Result result = m3Err_none; |
3092 | |
|
3093 | 0 | u32 index; |
3094 | 0 | _ (ReadLEB_u32(&index, &o->wasm, o->wasmEnd)); |
3095 | |
|
3096 | 0 | _throwif("data count section required", not o->module->hasDataCount); |
3097 | 0 | _throwif(m3Err_wasmMalformed, index >= o->module->numDataSegments); |
3098 | |
|
3099 | 0 | *o_segment = &o->module->dataSegments[index]; |
3100 | |
|
3101 | 0 | _catch: return result; |
3102 | 0 | } |
3103 | | |
3104 | | |
3105 | | M3Result Compile_Memory_Init (IM3Compilation o, m3opcode_t i_opcode) |
3106 | 0 | { |
3107 | 0 | M3Result result = m3Err_none; |
3108 | |
|
3109 | 0 | M3DataSegment* segment = NULL; |
3110 | 0 | u32 memoryIdx; |
3111 | |
|
3112 | 0 | _throwif(m3Err_wasmMalformed, o->module->numMemories == 0); |
3113 | |
|
3114 | 0 | _ (ReadDataSegment(o, &segment)); |
3115 | 0 | _ (ReadMemoryIndex(o, &memoryIdx)); |
3116 | |
|
3117 | 0 | _ (CopyStackTopToRegister(o, false)); |
3118 | |
|
3119 | 0 | if (memoryIdx) { |
3120 | 0 | _ (EmitSetMemory(o, memoryIdx)); |
3121 | 0 | } |
3122 | | |
3123 | 0 | #if d_m3HasMemory64 |
3124 | | // only the destination follows the memory's address type: a data segment |
3125 | | // is indexed as an i32 however the memory it is copied into is addressed |
3126 | 0 | _ (EmitOp(o, o->module->memories[memoryIdx]->isMemory64 ? op_MemInit64 : op_MemInit)); |
3127 | | #else |
3128 | | _ (EmitOp(o, op_MemInit)); |
3129 | | #endif |
3130 | 0 | EmitPointer(o, segment); |
3131 | 0 | _ (PopType(o, c_m3Type_i32)); |
3132 | 0 | _ (EmitSlotNumOfStackTopAndPop(o)); |
3133 | 0 | _ (EmitSlotNumOfStackTopAndPop(o)); |
3134 | |
|
3135 | 0 | if (memoryIdx) { |
3136 | 0 | _ (EmitSetMemory(o, 0)); |
3137 | 0 | } |
3138 | | |
3139 | 0 | _catch: return result; |
3140 | 0 | } |
3141 | | |
3142 | | |
3143 | | #if d_m3HasRefTypes |
3144 | | |
3145 | | static |
3146 | | M3Result Compile_Select (IM3Compilation o, m3opcode_t i_opcode); |
3147 | | |
3148 | | // select with an explicit result type vector. The types only matter to the |
3149 | | // validator; the operands are laid out exactly as for the untyped select. |
3150 | | M3Result Compile_Select_Typed (IM3Compilation o, m3opcode_t i_opcode) |
3151 | 25 | { |
3152 | 25 | M3Result result = m3Err_none; |
3153 | | |
3154 | 25 | u32 numTypes; |
3155 | 25 | _ (ReadLEB_u32(&numTypes, &o->wasm, o->wasmEnd)); |
3156 | 25 | _throwif(m3Err_wasmMalformed, numTypes != 1); |
3157 | | |
3158 | 25 | i8 waType; |
3159 | 25 | u8 type; |
3160 | 25 | _ (ReadLEB_i7(&waType, &o->wasm, o->wasmEnd)); |
3161 | 25 | _ (NormalizeType(&type, waType)); |
3162 | | |
3163 | 22 | _ (Compile_Select(o, i_opcode)); |
3164 | | |
3165 | 25 | _catch: return result; |
3166 | 22 | } |
3167 | | |
3168 | | |
3169 | | // A reference is one pointer-sized word and null is 0, so null-testing it is |
3170 | | // just an integer eqz of the matching width. |
3171 | | # if M3_SIZEOF_PTR == 8 |
3172 | 36 | # define d_m3RefIsNull_r op_i64_EqualToZero_r |
3173 | 274 | # define d_m3RefIsNull_s op_i64_EqualToZero_s |
3174 | | # else |
3175 | | # define d_m3RefIsNull_r op_i32_EqualToZero_r |
3176 | | # define d_m3RefIsNull_s op_i32_EqualToZero_s |
3177 | | # endif |
3178 | | |
3179 | | static |
3180 | | M3Result ReadRefType (IM3Compilation o, m3type_t* o_type) |
3181 | 3.98k | { |
3182 | 3.98k | M3Result result = m3Err_none; |
3183 | | |
3184 | 3.98k | # if d_m3HasTypedRefs |
3185 | | // ref.null names a heap type: func, extern, or a function type index |
3186 | 3.98k | m3type_t heapBits; |
3187 | 3.98k | _ (ParseHeapType(o->module, &heapBits, &o->wasm, o->wasmEnd)); |
3188 | | |
3189 | 3.83k | *o_type = d_m3Type_ref | heapBits; |
3190 | | # else |
3191 | | i8 waType; |
3192 | | u8 plainType; |
3193 | | _ (ReadLEB_i7(&waType, &o->wasm, o->wasmEnd)); |
3194 | | _ (NormalizeType(&plainType, waType)); |
3195 | | *o_type = plainType; |
3196 | | _throwif(m3Err_wasmMalformed, not IsRefType(*o_type)); |
3197 | | # endif |
3198 | | |
3199 | 3.98k | _catch: return result; |
3200 | 3.83k | } |
3201 | | |
3202 | | |
3203 | | M3Result Compile_Ref_Null (IM3Compilation o, m3opcode_t i_opcode) |
3204 | 3.98k | { |
3205 | 3.98k | M3Result result = m3Err_none; |
3206 | | |
3207 | 3.98k | m3type_t type; |
3208 | 3.98k | _ (ReadRefType(o, &type)); |
3209 | 3.83k | _ (PushConst(o, 0, type)); |
3210 | | |
3211 | 3.98k | _catch: return result; |
3212 | 3.83k | } |
3213 | | |
3214 | | |
3215 | | M3Result Compile_Ref_IsNull (IM3Compilation o, m3opcode_t i_opcode) |
3216 | 312 | { |
3217 | 312 | M3Result result = m3Err_none; |
3218 | | |
3219 | 312 | IM3Operation op; |
3220 | | |
3221 | 312 | if (not IsStackPolymorphic(o)) { |
3222 | 56 | _throwif(m3Err_typeMismatch, not IsRefType(GetStackTopType(o))); |
3223 | 55 | } |
3224 | | |
3225 | 311 | if (IsStackTopInRegister(o)) { |
3226 | 36 | op = d_m3RefIsNull_r; |
3227 | 275 | } else { |
3228 | 275 | _ (PreserveRegisterIfOccupied(o, c_m3Type_i32)); |
3229 | 274 | op = d_m3RefIsNull_s; |
3230 | 274 | } |
3231 | | |
3232 | 310 | _ (EmitOp(o, op)); |
3233 | 310 | _ (EmitSlotNumOfStackTopAndPop(o)); |
3234 | 309 | _ (PushRegister(o, c_m3Type_i32)); |
3235 | | |
3236 | 312 | _catch: return result; |
3237 | 308 | } |
3238 | | |
3239 | | |
3240 | | M3Result Compile_Ref_Func (IM3Compilation o, m3opcode_t i_opcode) |
3241 | 695 | { |
3242 | 695 | M3Result result = m3Err_none; |
3243 | | |
3244 | 695 | u32 funcIndex; |
3245 | | |
3246 | | // declared before the throws below, which jump past this point to _catch |
3247 | 695 | m3type_t refType = c_m3Type_funcref; |
3248 | 695 | IM3Function reference = NULL; |
3249 | | |
3250 | 695 | _ (ReadLEB_u32(&funcIndex, &o->wasm, o->wasmEnd)); |
3251 | 691 | _throwif("function index out of range", funcIndex >= o->module->numFunctions); |
3252 | | |
3253 | | // Inside a constant expression ref.func is itself a declaration; inside a |
3254 | | // function body the function must already have been declared elsewhere. |
3255 | 647 | if (o->function) { |
3256 | 111 | _throwif("undeclared function reference", not Module_IsFunctionDeclared(o->module, funcIndex)); |
3257 | 536 | } else { |
3258 | 536 | _ (Module_DeclareFunction(o->module, funcIndex)); |
3259 | 536 | } |
3260 | | |
3261 | 647 | # if d_m3HasTypedRefs |
3262 | 647 | refType = RefTypeOfFuncType(o->module->functions[funcIndex].funcType, true); |
3263 | 647 | # endif |
3264 | | |
3265 | | // A reference denotes the function that actually runs, so a ref.func naming |
3266 | | // an import linked to another module is the same reference that module's |
3267 | | // own ref.func would produce - and carries the defining module with it. |
3268 | 647 | reference = Function_Implementation(&o->module->functions[funcIndex]); |
3269 | | |
3270 | 647 | _ (PushConst(o, (u64)(uintptr_t)reference, refType)); |
3271 | | |
3272 | 695 | _catch: return result; |
3273 | 647 | } |
3274 | | |
3275 | | |
3276 | | // table.get/set/size/grow/fill all name a table by index; the table struct goes |
3277 | | // into the codestream so the operation doesn't have to walk the module. |
3278 | | static |
3279 | | M3Result ReadTable (IM3Compilation o, M3Table** o_table) |
3280 | 1.86k | { |
3281 | 1.86k | M3Result result = m3Err_none; |
3282 | | |
3283 | 1.86k | u32 index; |
3284 | 1.86k | _ (ReadLEB_u32(&index, &o->wasm, o->wasmEnd)); |
3285 | 1.86k | _throwif("table index out of range", index >= o->module->numTables); |
3286 | | |
3287 | 1.86k | *o_table = o->module->tables[index]; |
3288 | | |
3289 | 1.86k | _catch: return result; |
3290 | 1.86k | } |
3291 | | |
3292 | | |
3293 | | // The table operations take every operand from a slot, so spill the integer |
3294 | | // register first: after this each EmitSlotNumOfStackTopAndPop () emits a real |
3295 | | // slot offset, and the register is free for the result. Operands are emitted |
3296 | | // from the top of the stack downwards, which is the order the ops read them. |
3297 | | static |
3298 | | M3Result Compile_Table_Op (IM3Compilation o, IM3Operation i_op, M3Table* i_table, u32 i_numOperands, m3type_t i_retType) |
3299 | 1.16k | { |
3300 | 1.16k | M3Result result = m3Err_none; |
3301 | | |
3302 | 1.16k | _ (PreserveRegisterIfOccupied(o, c_m3Type_i64)); |
3303 | | |
3304 | 1.16k | _ (EmitOp(o, i_op)); |
3305 | 1.16k | EmitPointer(o, i_table); |
3306 | | |
3307 | 2.68k | for (u32 i = 0; i < i_numOperands; ++i) { |
3308 | 1.51k | _ (EmitSlotNumOfStackTopAndPop(o)); |
3309 | 1.51k | } |
3310 | | |
3311 | 1.16k | if (i_retType != c_m3Type_none) { |
3312 | 926 | _ (PushRegister(o, i_retType)); |
3313 | 926 | } |
3314 | | |
3315 | 1.16k | _catch: return result; |
3316 | 1.16k | } |
3317 | | |
3318 | | |
3319 | | M3Result Compile_Table_GetSet (IM3Compilation o, m3opcode_t i_opcode) |
3320 | 590 | { |
3321 | 590 | M3Result result = m3Err_none; |
3322 | | |
3323 | 590 | M3Table* table; |
3324 | 590 | _ (ReadTable(o, &table)); |
3325 | | |
3326 | 590 | if (i_opcode == c_waOp_tableGet) { |
3327 | 494 | _ (CheckOperandType(o, 0, Table_AddrType(table))); |
3328 | 494 | _ (Compile_Table_Op(o, op_TableGet, table, 1, table->type)); |
3329 | 494 | } else { |
3330 | 96 | _ (CheckOperandType(o, 1, Table_AddrType(table))); |
3331 | 96 | _ (Compile_Table_Op(o, op_TableSet, table, 2, c_m3Type_none)); |
3332 | 94 | } |
3333 | | |
3334 | 590 | _catch: return result; |
3335 | 590 | } |
3336 | | |
3337 | | |
3338 | | M3Result Compile_Table_Init (IM3Compilation o, m3opcode_t i_opcode) |
3339 | 120 | { |
3340 | 120 | M3Result result = m3Err_none; |
3341 | | |
3342 | 120 | u32 elemIndex; |
3343 | 120 | M3Table* table; |
3344 | | |
3345 | 120 | _ (ReadLEB_u32(&elemIndex, &o->wasm, o->wasmEnd)); |
3346 | 120 | _throwif("element segment index out of range", elemIndex >= o->module->numElementSegments); |
3347 | 120 | _ (ReadTable(o, &table)); |
3348 | | |
3349 | 120 | _ (CheckOperandType(o, 2, Table_AddrType(table))); |
3350 | | |
3351 | 120 | _ (PreserveRegisterIfOccupied(o, c_m3Type_i64)); |
3352 | 120 | _ (EmitOp(o, op_TableInit)); |
3353 | 120 | EmitPointer(o, table); |
3354 | 120 | EmitPointer(o, &o->module->elementSegments[elemIndex]); |
3355 | | |
3356 | 480 | for (u32 i = 0; i < 3; ++i) { |
3357 | 360 | _ (EmitSlotNumOfStackTopAndPop(o)); |
3358 | 360 | } |
3359 | | |
3360 | 120 | _catch: return result; |
3361 | 120 | } |
3362 | | |
3363 | | |
3364 | | M3Result Compile_Elem_Drop (IM3Compilation o, m3opcode_t i_opcode) |
3365 | 35 | { |
3366 | 35 | M3Result result = m3Err_none; |
3367 | | |
3368 | 35 | u32 elemIndex; |
3369 | 35 | _ (ReadLEB_u32(&elemIndex, &o->wasm, o->wasmEnd)); |
3370 | 35 | _throwif("element segment index out of range", elemIndex >= o->module->numElementSegments); |
3371 | | |
3372 | 35 | _ (EmitOp(o, op_ElemDrop)); |
3373 | 35 | EmitPointer(o, &o->module->elementSegments[elemIndex]); |
3374 | | |
3375 | 35 | _catch: return result; |
3376 | 35 | } |
3377 | | |
3378 | | |
3379 | | M3Result Compile_Table_Copy (IM3Compilation o, m3opcode_t i_opcode) |
3380 | 290 | { |
3381 | 290 | M3Result result = m3Err_none; |
3382 | | |
3383 | 290 | M3Table* dst; |
3384 | 290 | M3Table* src; |
3385 | | |
3386 | 290 | _ (ReadTable(o, &dst)); |
3387 | 290 | _ (ReadTable(o, &src)); |
3388 | 290 | _throwif(m3Err_typeMismatch, not IsSubTypeOf(src->type, dst->type)); |
3389 | | |
3390 | 290 | _ (CheckOperandType(o, 2, Table_AddrType(dst))); |
3391 | 290 | _ (CheckOperandType(o, 1, Table_AddrType(src))); |
3392 | 290 | _ (CheckOperandType(o, 0, (dst->isTable64 and src->isTable64) ? c_m3Type_i64 : c_m3Type_i32)); |
3393 | | |
3394 | 290 | _ (PreserveRegisterIfOccupied(o, c_m3Type_i64)); |
3395 | 290 | _ (EmitOp(o, op_TableCopy)); |
3396 | 290 | EmitPointer(o, dst); |
3397 | 290 | EmitPointer(o, src); |
3398 | | |
3399 | 1.15k | for (u32 i = 0; i < 3; ++i) { |
3400 | 867 | _ (EmitSlotNumOfStackTopAndPop(o)); |
3401 | 864 | } |
3402 | | |
3403 | 290 | _catch: return result; |
3404 | 290 | } |
3405 | | |
3406 | | |
3407 | | M3Result Compile_Table_Size (IM3Compilation o, m3opcode_t i_opcode) |
3408 | 233 | { |
3409 | 233 | M3Result result = m3Err_none; |
3410 | | |
3411 | 233 | M3Table* table; |
3412 | 233 | _ (ReadTable(o, &table)); |
3413 | | |
3414 | | // a table size is given in the table's own index type |
3415 | 233 | _ (Compile_Table_Op(o, op_TableSize, table, 0, Table_AddrType(table))); |
3416 | | |
3417 | 233 | _catch: return result; |
3418 | 233 | } |
3419 | | |
3420 | | |
3421 | | M3Result Compile_Table_GrowFill (IM3Compilation o, m3opcode_t i_opcode) |
3422 | 346 | { |
3423 | 346 | M3Result result = m3Err_none; |
3424 | | |
3425 | 346 | M3Table* table; |
3426 | 346 | _ (ReadTable(o, &table)); |
3427 | | |
3428 | 346 | if (i_opcode == c_waOp_tableGrow) { |
3429 | 200 | _ (CheckOperandType(o, 0, Table_AddrType(table))); |
3430 | 200 | _ (Compile_Table_Op(o, op_TableGrow, table, 2, Table_AddrType(table))); |
3431 | 199 | } else { |
3432 | 146 | _ (CheckOperandType(o, 0, Table_AddrType(table))); |
3433 | 146 | _ (CheckOperandType(o, 2, Table_AddrType(table))); |
3434 | 146 | _ (Compile_Table_Op(o, op_TableFill, table, 3, c_m3Type_none)); |
3435 | 143 | } |
3436 | | |
3437 | 346 | _catch: return result; |
3438 | 346 | } |
3439 | | |
3440 | | #endif // d_m3HasRefTypes |
3441 | | |
3442 | | |
3443 | | M3Result Compile_Data_Drop (IM3Compilation o, m3opcode_t i_opcode) |
3444 | 0 | { |
3445 | 0 | M3Result result = m3Err_none; |
3446 | |
|
3447 | 0 | M3DataSegment* segment = NULL; |
3448 | 0 | _ (ReadDataSegment(o, &segment)); |
3449 | |
|
3450 | 0 | _ (EmitOp(o, op_DataDrop)); |
3451 | 0 | EmitPointer(o, segment); |
3452 | |
|
3453 | 0 | _catch: return result; |
3454 | 0 | } |
3455 | | |
3456 | | |
3457 | | static |
3458 | | M3Result ReadBlockType (IM3Compilation o, IM3FuncType* o_blockType) |
3459 | 8.02k | { |
3460 | 8.02k | M3Result result = m3Err_none; |
3461 | | |
3462 | 8.02k | #if d_m3HasTypedRefs |
3463 | | // a spelled-out reference type is two bytes, so it cannot be told apart |
3464 | | // from a type index by the s33 that block types otherwise use |
3465 | 8.02k | if (o->wasm < o->wasmEnd and (*o->wasm == d_waEncode_ref or *o->wasm == d_waEncode_refNull)) { |
3466 | 69 | m3type_t refType; |
3467 | 69 | _ (ParseValueType(o->module, &refType, &o->wasm, o->wasmEnd)); |
3468 | 69 | IM3FuncType ftype = NULL; |
3469 | 69 | _ (AllocFuncType(&ftype, 1)); |
3470 | 69 | ftype->numRets = 1; |
3471 | 69 | ftype->types[0] = refType; |
3472 | 69 | _ (Environment_AddFuncType(o->module->environment, &ftype)); |
3473 | 69 | *o_blockType = ftype; |
3474 | 69 | return result; |
3475 | 69 | } |
3476 | 7.95k | #endif |
3477 | | |
3478 | 7.95k | i64 type; |
3479 | 7.95k | _ (ReadLebSigned(&type, 33, &o->wasm, o->wasmEnd)); |
3480 | | |
3481 | 7.95k | if (type < 0) { |
3482 | 3.81k | _throwif(m3Err_invalidTypeId, type < -64); |
3483 | 3.81k | u8 valueType; |
3484 | 3.81k | _ (NormalizeType(&valueType, (i8)type)); m3log (compile, d_indent " (type: %s)", get_indention_string (o), c_waTypes [valueType]); |
3485 | 3.81k | *o_blockType = o->module->environment->retFuncTypes[valueType]; |
3486 | 4.14k | } else { |
3487 | 4.14k | _throwif("func type out of bounds", type >= o->module->numFuncTypes); |
3488 | 4.14k | *o_blockType = o->module->funcTypes[type]; m3log (compile, d_indent " (type: %s)", get_indention_string (o), SPrintFuncTypeSignature (*o_blockType)); |
3489 | 4.14k | } |
3490 | 7.95k | _catch: return result; |
3491 | 7.95k | } |
3492 | | |
3493 | | static |
3494 | | M3Result PreserveArgsAndLocals (IM3Compilation o) |
3495 | 8.03k | { |
3496 | 8.03k | M3Result result = m3Err_none; |
3497 | | |
3498 | 8.03k | if (o->stackIndex > o->stackFirstDynamicIndex) { |
3499 | 6.75k | u32 numArgsAndLocals = GetFunctionNumArgsAndLocals(o->function); |
3500 | | |
3501 | 2.62M | for (u32 i = 0; i < numArgsAndLocals; ++i) { |
3502 | 2.62M | u16 slot = GetSlotForStackIndex(o, i); |
3503 | | |
3504 | 2.62M | u16 preservedSlotNumber; |
3505 | 2.62M | _ (FindReferencedLocalWithinCurrentBlock(o, &preservedSlotNumber, slot)); |
3506 | | |
3507 | 2.62M | if (preservedSlotNumber != slot) { |
3508 | 362 | m3type_t type = GetStackTypeFromBottom(o, i); d_m3Assert (type != c_m3Type_none) |
3509 | 362 | IM3Operation op = Is64BitType(type) ? op_CopySlot_64 : op_CopySlot_32; |
3510 | | |
3511 | 362 | EmitOp(o, op); |
3512 | 362 | EmitSlotOffset(o, preservedSlotNumber); |
3513 | 362 | EmitSlotOffset(o, slot); |
3514 | 362 | } |
3515 | 2.62M | } |
3516 | 6.75k | } |
3517 | | |
3518 | 8.03k | _catch: |
3519 | 8.03k | return result; |
3520 | 8.03k | } |
3521 | | |
3522 | | static |
3523 | | M3Result Compile_LoopOrBlock (IM3Compilation o, m3opcode_t i_opcode) |
3524 | 2.51k | { |
3525 | 2.51k | M3Result result; |
3526 | | |
3527 | 2.51k | IM3FuncType blockType = NULL; |
3528 | | |
3529 | 2.51k | _ (PreserveRegisters(o)); |
3530 | 2.51k | _ (PreserveArgsAndLocals(o)); |
3531 | | |
3532 | 2.50k | _ (ReadBlockType(o, &blockType)); |
3533 | | |
3534 | 2.50k | if (i_opcode == c_waOp_loop) { |
3535 | 1.53k | u16 numParams = GetFuncTypeNumParams(blockType); |
3536 | 1.53k | if (numParams) { |
3537 | | // instantiate constants |
3538 | 341 | u16 numValues = GetNumBlockValuesOnStack(o); |
3539 | | |
3540 | 341 | if (numValues >= numParams) { |
3541 | 143 | u16 stackTop = GetStackTopIndex(o) + 1; |
3542 | | |
3543 | 1.24k | for (u16 i = stackTop - numParams; i < stackTop; ++i) { |
3544 | 1.10k | u16 slot = GetSlotForStackIndex(o, i); |
3545 | 1.10k | m3type_t type = GetStackTypeFromBottom(o, i); |
3546 | | |
3547 | 1.10k | if (IsConstantSlot(o, slot)) { |
3548 | 183 | u16 newSlot = c_slotUnused; |
3549 | 183 | _ (AllocateSlots(o, &newSlot, type)); |
3550 | 182 | _ (CopyStackIndexToSlot(o, newSlot, i)); |
3551 | 182 | o->wasmStack[i] = newSlot; |
3552 | 182 | } |
3553 | 1.10k | } |
3554 | 143 | } |
3555 | 341 | } |
3556 | | |
3557 | 1.53k | _ (EmitOp(o, op_Loop)); |
3558 | 1.53k | } else { |
3559 | 969 | } |
3560 | | |
3561 | 2.50k | _ (CompileBlock(o, blockType, i_opcode)); |
3562 | | |
3563 | 2.51k | _catch: return result; |
3564 | 2.11k | } |
3565 | | |
3566 | | #if d_m3HasExceptionHandling |
3567 | | |
3568 | | // Emits one out-of-line stub per catch clause, and writes each stub's address |
3569 | | // into the slot op_TryTable reserved for it. |
3570 | | // |
3571 | | // Called from CompileBlock with the try block's scope already on the stack, so |
3572 | | // the clause labels - which count from outside the try - are one deeper here. |
3573 | | // The payload values are pushed on top of whatever the block entry left there: |
3574 | | // only their position relative to the stack top matters, since |
3575 | | // ResolveBlockResults copies the topmost values into the label's landing pads. |
3576 | | // Popping them again afterwards puts the compiler's stack back where the body |
3577 | | // expects to find it. |
3578 | | static |
3579 | | M3Result EmitCatchStubs (IM3Compilation o) |
3580 | 405 | { |
3581 | 405 | IM3CodePage displacedPage = NULL; |
3582 | 405 | _try { |
3583 | 405 | M3CatchClause* clauses = o->tryClauses; |
3584 | 405 | u32 numClauses = o->numTryClauses; |
3585 | | |
3586 | | // the body must not see these: a nested try_table sets up its own |
3587 | 405 | o->tryClauses = NULL; |
3588 | 405 | o->numTryClauses = 0; |
3589 | | |
3590 | 704 | for (u32 i = 0; i < numClauses; ++i) { |
3591 | 299 | M3CatchClause* clause = &clauses[i]; |
3592 | | |
3593 | 299 | IM3CompilationScope scope; |
3594 | 299 | _ (GetBlockScope(o, &scope, clause->labelDepth + 1)); |
3595 | | |
3596 | 299 | IM3FuncType payload = clause->tag ? clause->tag->type : NULL; |
3597 | 299 | u16 numArgs = GetFuncTypeNumParams(payload); |
3598 | | |
3599 | | // What the stub hands the label - the payload, then the exnref for a |
3600 | | // _ref clause - has to be what the label takes. ResolveBlockResults |
3601 | | // only counts the values, and the validator compares base types, so a |
3602 | | // typed reference is only checked here: (ref null $t) reaching a label |
3603 | | // that takes (ref $t) has to be rejected. |
3604 | 299 | { |
3605 | 299 | bool isLoop = (scope->opcode == c_waOp_loop); |
3606 | 299 | u16 numLabelValues = isLoop ? GetFuncTypeNumParams(scope->type) |
3607 | 299 | : GetFuncTypeNumResults(scope->type); |
3608 | | |
3609 | 299 | _throwif(m3Err_typeCountMismatch, numLabelValues != numArgs + (clause->hasRef ? 1 : 0)); |
3610 | | |
3611 | 1.82k | for (u16 a = 0; a < numLabelValues; ++a) { |
3612 | 1.52k | m3type_t given = (a < numArgs) ? GetFuncTypeParamType(payload, a) |
3613 | 1.52k | : (m3type_t)c_m3Type_exnref; |
3614 | 1.52k | m3type_t wanted = isLoop ? GetFuncTypeParamType(scope->type, a) |
3615 | 1.52k | : GetFuncTypeResultType(scope->type, a); |
3616 | | |
3617 | 1.52k | _throwif(m3Err_typeMismatch, not IsSubTypeOf(given, wanted)); |
3618 | 1.52k | } |
3619 | 299 | } |
3620 | | |
3621 | 299 | IM3CodePage stubPage; |
3622 | 299 | _ (AcquireCompilationCodePage(o, &stubPage)); |
3623 | | |
3624 | 299 | pc_t startPC = GetPagePC(stubPage); |
3625 | 299 | displacedPage = o->page; |
3626 | 299 | o->page = stubPage; |
3627 | | |
3628 | 299 | u16 firstIndex = o->stackIndex; |
3629 | | |
3630 | 1.79k | for (u16 a = 0; a < numArgs; ++a) { |
3631 | 1.49k | _ (PushAllocatedSlot(o, GetFuncTypeParamType(payload, a))); |
3632 | 1.49k | } |
3633 | | |
3634 | 299 | if (clause->hasRef) { |
3635 | 29 | _ (PushAllocatedSlot(o, c_m3Type_exnref)); |
3636 | 29 | } |
3637 | | |
3638 | | // emitted even with nothing to move: this is also where an exception |
3639 | | // no clause will reify gets released |
3640 | 299 | _ (EnsureCodePageNumLines(o, 2 * numArgs + 3 + d_m3CodePageFreeLinesThreshold)); |
3641 | | |
3642 | 299 | _ (EmitOp(o, op_CatchPayload)); |
3643 | 299 | EmitConstant32(o, numArgs); |
3644 | | |
3645 | 1.79k | for (u16 a = 0; a < numArgs; ++a) { |
3646 | 1.49k | u16 index = firstIndex + a; |
3647 | 1.49k | EmitSlotOffset(o, GetSlotForStackIndex(o, index)); |
3648 | 1.49k | EmitConstant32(o, Is64BitType(GetStackTypeFromBottom(o, index))); |
3649 | 1.49k | } |
3650 | | |
3651 | 299 | EmitSlotOffset(o, clause->hasRef ? (i32)GetSlotForStackIndex(o, firstIndex + numArgs) : -1); |
3652 | | |
3653 | 299 | _ (EmitPopTryFrames(o, NumTryFramesToPop(o->block.outer, scope))); |
3654 | | |
3655 | 299 | if (scope->opcode == c_waOp_loop) { |
3656 | 168 | _ (ResolveBlockResults(o, scope, /* isBranch: */ true)); |
3657 | | |
3658 | 168 | _ (EmitOp(o, Op_ContinueLoop(o))); |
3659 | 168 | EmitPointer(o, scope->pc); |
3660 | 168 | _ (RecordBackEdge(o, scope, false)); |
3661 | 168 | } else if (scope->depth == 0) { |
3662 | 59 | _ (ReturnValues(o, scope, /* isBranch: */ true)); |
3663 | 59 | _ (EmitOp(o, op_Return)); |
3664 | 72 | } else { |
3665 | 72 | _ (ResolveBlockResults(o, scope, /* isBranch: */ true)); |
3666 | 72 | _ (EmitPatchingBranch(o, scope)); |
3667 | 72 | } |
3668 | | |
3669 | 299 | IM3CodePage returnTo = displacedPage; |
3670 | 299 | displacedPage = NULL; |
3671 | | |
3672 | 299 | ReleaseCompilationCodePage(o); |
3673 | 299 | o->page = returnTo; |
3674 | | |
3675 | 299 | *(pc_t*)clause->stubSlot = startPC; |
3676 | | |
3677 | 1.82k | while (o->stackIndex > firstIndex) { |
3678 | 1.52k | _ (Pop(o)); |
3679 | 1.52k | } |
3680 | 299 | } |
3681 | | |
3682 | 405 | } _catch: |
3683 | | |
3684 | | // thrown with a stub page installed: release it and put back the one it |
3685 | | // displaced, which the caller's catch then releases |
3686 | 405 | if (displacedPage) { |
3687 | 0 | ReleaseCompilationCodePage(o); |
3688 | 0 | o->page = displacedPage; |
3689 | 0 | } |
3690 | | |
3691 | 405 | return result; |
3692 | 405 | } |
3693 | | |
3694 | | |
3695 | | static |
3696 | | M3Result Compile_TryTable (IM3Compilation o, m3opcode_t i_opcode) |
3697 | 408 | { |
3698 | 408 | M3CatchClause* clauses = NULL; |
3699 | 408 | _try { |
3700 | | // a catch entry has to be able to assume the operand stack is entirely in |
3701 | | // slots: an unwind leaves nothing in the registers |
3702 | 408 | _ (PreserveRegisters(o)); |
3703 | 406 | _ (PreserveArgsAndLocals(o)); |
3704 | | |
3705 | 405 | IM3FuncType blockType = NULL; |
3706 | 405 | _ (ReadBlockType(o, &blockType)); |
3707 | | |
3708 | 405 | u32 numClauses; |
3709 | 405 | _ (ReadLEB_u32(&numClauses, &o->wasm, o->wasmEnd)); |
3710 | | |
3711 | 405 | _throwif("too many catch clauses", numClauses > d_m3MaxSaneTagsCount); |
3712 | | |
3713 | 405 | if (numClauses) { |
3714 | 123 | clauses = m3_AllocArray(M3CatchClause, numClauses); |
3715 | 123 | _throwifnull(clauses); |
3716 | 123 | } |
3717 | | |
3718 | 704 | for (u32 i = 0; i < numClauses; ++i) { |
3719 | 299 | u8 kind; |
3720 | 299 | _ (Read_u8(&kind, &o->wasm, o->wasmEnd)); |
3721 | 299 | _throwif(m3Err_wasmMalformed, kind > 0x03); |
3722 | | |
3723 | 299 | clauses[i].tag = NULL; |
3724 | 299 | clauses[i].hasRef = (kind == 0x01 or kind == 0x03); |
3725 | | |
3726 | 299 | if (kind == 0x00 or kind == 0x01) { |
3727 | 159 | u32 tagIndex; |
3728 | 159 | _ (ReadLEB_u32(&tagIndex, &o->wasm, o->wasmEnd)); |
3729 | 159 | _throwif(m3Err_unknownTag, tagIndex >= o->module->numTags); |
3730 | | |
3731 | 159 | IM3Tag tag = &o->module->tags[tagIndex]; |
3732 | 159 | clauses[i].tag = tag->resolved ? tag->resolved : tag; |
3733 | 159 | } |
3734 | | |
3735 | 299 | _ (ReadLEB_u32(&clauses[i].labelDepth, &o->wasm, o->wasmEnd)); |
3736 | 299 | } |
3737 | | |
3738 | | // op_TryTable, the clause count, and two words per clause, all of which have |
3739 | | // to land on one page: the op reads the table by offset from its own pc |
3740 | 405 | _ (EnsureCodePageNumLines(o, 2 * numClauses + 2 + d_m3CodePageFreeLinesThreshold)); |
3741 | | |
3742 | 405 | _ (EmitOp(o, op_TryTable)); |
3743 | 405 | EmitConstant32(o, numClauses); |
3744 | | |
3745 | 704 | for (u32 i = 0; i < numClauses; ++i) { |
3746 | 299 | EmitPointer(o, clauses[i].tag); |
3747 | 299 | clauses[i].stubSlot = ReservePointer(o); |
3748 | 299 | } |
3749 | | |
3750 | 405 | o->tryClauses = clauses; |
3751 | 405 | o->numTryClauses = numClauses; |
3752 | | |
3753 | 405 | _ (CompileBlock(o, blockType, c_waOp_tryTable)); |
3754 | | |
3755 | 408 | } _catch: |
3756 | | |
3757 | 408 | o->tryClauses = NULL; |
3758 | 408 | o->numTryClauses = 0; |
3759 | | |
3760 | 408 | m3_Free(clauses); |
3761 | | |
3762 | 408 | return result; |
3763 | 298 | } |
3764 | | |
3765 | | |
3766 | | static |
3767 | | M3Result Compile_Throw (IM3Compilation o, m3opcode_t i_opcode) |
3768 | 275 | { |
3769 | 275 | _try { |
3770 | 275 | u32 tagIndex; |
3771 | 275 | _ (ReadLEB_u32(&tagIndex, &o->wasm, o->wasmEnd)); |
3772 | 275 | _throwif(m3Err_unknownTag, tagIndex >= o->module->numTags); |
3773 | | |
3774 | 275 | IM3Tag tag = &o->module->tags[tagIndex]; |
3775 | 275 | if (tag->resolved) { |
3776 | 0 | tag = tag->resolved; |
3777 | 0 | } |
3778 | | |
3779 | 275 | u16 numArgs = GetFuncTypeNumParams(tag->type); |
3780 | | |
3781 | | // the payload is read out of slots, so nothing may still be in a register |
3782 | 275 | _ (PreserveRegisters(o)); |
3783 | | |
3784 | | // check the payload before emitting anything, so a mistyped throw does not |
3785 | | // leave half an operation behind |
3786 | 275 | bool isPolymorphic = IsStackPolymorphic(o); |
3787 | | |
3788 | 275 | if (not isPolymorphic) { |
3789 | 81 | _throwif(m3Err_typeCountMismatch, GetNumBlockValuesOnStack(o) < numArgs); |
3790 | | |
3791 | 113 | for (u16 i = 0; i < numArgs; ++i) { |
3792 | 32 | u16 index = (u16)(o->stackIndex - numArgs + i); |
3793 | | |
3794 | 32 | _throwif(m3Err_typeMismatch, not IsSubTypeOf(GetStackTypeFromBottom(o, index), |
3795 | 32 | GetFuncTypeParamType(tag->type, i))); |
3796 | 32 | } |
3797 | 81 | } |
3798 | | |
3799 | 275 | _ (EnsureCodePageNumLines(o, 2 * numArgs + 3 + d_m3CodePageFreeLinesThreshold)); |
3800 | | |
3801 | 275 | _ (EmitOp(o, op_Throw)); |
3802 | 275 | EmitPointer(o, tag); |
3803 | 275 | EmitConstant32(o, numArgs); |
3804 | | |
3805 | | // the payload is named in the order the tag declares it, so the deepest of |
3806 | | // the arguments on the stack comes first. In unreachable code there is |
3807 | | // nothing on the stack to name and nothing will run this, so slot 0 stands |
3808 | | // in for an operand that isn't there. |
3809 | 1.03k | for (u16 i = 0; i < numArgs; ++i) { |
3810 | 757 | m3type_t type = GetFuncTypeParamType(tag->type, i); |
3811 | | |
3812 | 757 | EmitSlotOffset(o, isPolymorphic ? 0 : GetSlotForStackIndex(o, (u16)(o->stackIndex - numArgs + i))); |
3813 | 757 | EmitConstant32(o, Is64BitType(type)); |
3814 | 757 | } |
3815 | | |
3816 | 275 | if (not isPolymorphic) { |
3817 | 112 | for (u16 i = 0; i < numArgs; ++i) { |
3818 | 32 | _ (Pop(o)); |
3819 | 31 | } |
3820 | 81 | } |
3821 | | |
3822 | 274 | _ (SetStackPolymorphic(o)); |
3823 | | |
3824 | 275 | } _catch: return result; |
3825 | 273 | } |
3826 | | |
3827 | | |
3828 | | static |
3829 | | M3Result Compile_ThrowRef (IM3Compilation o, m3opcode_t i_opcode) |
3830 | 980 | { |
3831 | 980 | _try { |
3832 | 980 | if (not IsStackPolymorphic(o)) { |
3833 | 73 | _ (PreserveRegisterIfOccupied(o, c_m3Type_i64)); |
3834 | | |
3835 | 73 | _throwif(m3Err_typeMismatch, BaseTypeOf(GetStackTopType(o)) != c_m3Type_exnref); |
3836 | | |
3837 | 73 | _ (EmitOp(o, op_ThrowRef)); |
3838 | 73 | EmitSlotOffset(o, GetStackTopSlotNumber(o)); |
3839 | | |
3840 | 73 | _ (Pop(o)); |
3841 | 73 | } |
3842 | | |
3843 | 980 | _ (SetStackPolymorphic(o)); |
3844 | | |
3845 | 980 | } _catch: return result; |
3846 | 979 | } |
3847 | | |
3848 | | #endif // d_m3HasExceptionHandling |
3849 | | |
3850 | | #if d_m3HasStackSwitching |
3851 | | |
3852 | | static |
3853 | | M3Result Compile_ContNew (IM3Compilation o, m3opcode_t i_opcode) |
3854 | 0 | { |
3855 | 0 | _try { |
3856 | 0 | u32 typeIndex; |
3857 | 0 | _ (ReadLEB_u32(&typeIndex, &o->wasm, o->wasmEnd)); |
3858 | 0 | _throwif(m3Err_typeCountMismatch, typeIndex >= o->module->numFuncTypes); |
3859 | |
|
3860 | 0 | IM3FuncType funcType = o->module->funcTypes[typeIndex]; |
3861 | 0 | _throwif("type is not a continuation", not funcType->isContinuation); |
3862 | | |
3863 | | // the operands are read out of slots, so nothing may still be in a register |
3864 | 0 | _ (PreserveRegisters(o)); |
3865 | | |
3866 | | // cont.new $ct : [(ref null $ft)] -> [(ref $ct)], where $ct ~~ cont $ft: |
3867 | | // the function has to be one this continuation type can run |
3868 | 0 | m3type_t topType = GetStackTopType(o); |
3869 | 0 | _throwif(m3Err_typeMismatch, |
3870 | 0 | not IsSubTypeOf(topType, RefTypeOfFuncType(funcType->contFuncType, false))); |
3871 | |
|
3872 | 0 | u16 funcSlot = GetStackTopSlotNumber(o); |
3873 | 0 | _ (Pop(o)); |
3874 | |
|
3875 | 0 | u16 dstSlot; |
3876 | 0 | _ (PushAllocatedSlot(o, RefTypeOfFuncType(funcType, true))); |
3877 | 0 | dstSlot = GetStackTopSlotNumber(o); |
3878 | |
|
3879 | 0 | _ (EnsureCodePageNumLines(o, 4 + d_m3CodePageFreeLinesThreshold)); |
3880 | 0 | _ (EmitOp(o, op_ContNew)); |
3881 | 0 | EmitPointer(o, funcType); |
3882 | 0 | EmitSlotOffset(o, dstSlot); |
3883 | 0 | EmitSlotOffset(o, funcSlot); |
3884 | |
|
3885 | 0 | } _catch: return result; |
3886 | 0 | } |
3887 | | |
3888 | | static |
3889 | | M3Result Compile_ContBind (IM3Compilation o, m3opcode_t i_opcode) |
3890 | 0 | { |
3891 | 0 | _try { |
3892 | 0 | u32 typeIndex1, typeIndex2; |
3893 | 0 | _ (ReadLEB_u32(&typeIndex1, &o->wasm, o->wasmEnd)); |
3894 | 0 | _ (ReadLEB_u32(&typeIndex2, &o->wasm, o->wasmEnd)); |
3895 | 0 | _throwif(m3Err_typeCountMismatch, typeIndex1 >= o->module->numFuncTypes); |
3896 | 0 | _throwif(m3Err_typeCountMismatch, typeIndex2 >= o->module->numFuncTypes); |
3897 | |
|
3898 | 0 | IM3FuncType ct1 = o->module->funcTypes[typeIndex1]; |
3899 | 0 | IM3FuncType ct2 = o->module->funcTypes[typeIndex2]; |
3900 | 0 | _throwif("type is not a continuation", not ct1->isContinuation || not ct2->isContinuation); |
3901 | | |
3902 | | // the operands are read out of slots, so nothing may still be in a register |
3903 | 0 | _ (PreserveRegisters(o)); |
3904 | |
|
3905 | 0 | m3type_t contType = GetStackTopType(o); |
3906 | 0 | _throwif(m3Err_typeMismatch, not IsSubTypeOf(contType, RefTypeOfFuncType(ct1, false))); |
3907 | 0 | u16 contSlot = GetStackTopSlotNumber(o); |
3908 | 0 | _ (Pop(o)); |
3909 | |
|
3910 | 0 | IM3FuncType ft1 = ct1->contFuncType ? ct1->contFuncType : ct1; |
3911 | 0 | IM3FuncType ft2 = ct2->contFuncType ? ct2->contFuncType : ct2; |
3912 | |
|
3913 | 0 | u16 numParams1 = GetFuncTypeNumParams(ft1); |
3914 | 0 | u16 numParams2 = GetFuncTypeNumParams(ft2); |
3915 | 0 | _throwif(m3Err_typeMismatch, numParams1 < numParams2); |
3916 | 0 | u32 numBound = numParams1 - numParams2; |
3917 | |
|
3918 | 0 | u16 numResults1 = GetFuncTypeNumResults(ft1); |
3919 | 0 | u16 numResults2 = GetFuncTypeNumResults(ft2); |
3920 | 0 | _throwif(m3Err_typeMismatch, numResults1 != numResults2); |
3921 | 0 | for (u16 i = 0; i < numResults1; ++i) { |
3922 | 0 | _throwif(m3Err_typeMismatch, GetFuncTypeResultType(ft1, i) != GetFuncTypeResultType(ft2, i)); |
3923 | 0 | } |
3924 | 0 | for (u16 i = 0; i < numParams2; ++i) { |
3925 | 0 | _throwif(m3Err_typeMismatch, GetFuncTypeParamType(ft1, numBound + i) != GetFuncTypeParamType(ft2, i)); |
3926 | 0 | } |
3927 | | |
3928 | 0 | u16 argSlots[d_m3MaxContinuationPayload]; |
3929 | 0 | _throwif("too many bound args", numBound > d_m3MaxContinuationPayload); |
3930 | |
|
3931 | 0 | for (u32 i = 0; i < numBound; ++i) { |
3932 | 0 | u32 paramIdx = numBound - 1 - i; |
3933 | 0 | m3type_t expectedType = GetFuncTypeParamType(ft1, paramIdx); |
3934 | 0 | m3type_t actualType = GetStackTopType(o); |
3935 | 0 | _throwif(m3Err_typeMismatch, not IsSubTypeOf(actualType, expectedType)); |
3936 | 0 | argSlots[paramIdx] = GetStackTopSlotNumber(o); |
3937 | 0 | _ (Pop(o)); |
3938 | 0 | } |
3939 | | |
3940 | 0 | u16 dstSlot; |
3941 | 0 | _ (PushAllocatedSlot(o, RefTypeOfFuncType(ct2, true))); |
3942 | 0 | dstSlot = GetStackTopSlotNumber(o); |
3943 | |
|
3944 | 0 | _ (EnsureCodePageNumLines(o, 6 + numBound * 2 + d_m3CodePageFreeLinesThreshold)); |
3945 | 0 | _ (EmitOp(o, op_ContBind)); |
3946 | 0 | EmitPointer(o, ct1); |
3947 | 0 | EmitPointer(o, ct2); |
3948 | 0 | EmitSlotOffset(o, dstSlot); |
3949 | 0 | EmitSlotOffset(o, contSlot); |
3950 | 0 | EmitConstant32(o, numBound); |
3951 | 0 | for (u32 i = 0; i < numBound; ++i) { |
3952 | 0 | EmitSlotOffset(o, argSlots[i]); |
3953 | 0 | EmitConstant32(o, Is64BitType(GetFuncTypeParamType(ft1, i))); |
3954 | 0 | } |
3955 | |
|
3956 | 0 | } _catch: return result; |
3957 | 0 | } |
3958 | | |
3959 | | static |
3960 | | M3Result Compile_Suspend (IM3Compilation o, m3opcode_t i_opcode) |
3961 | 186 | { |
3962 | 186 | _try { |
3963 | 186 | u32 tagIndex; |
3964 | 186 | _ (ReadLEB_u32(&tagIndex, &o->wasm, o->wasmEnd)); |
3965 | 186 | _throwif(m3Err_unknownTag, tagIndex >= o->module->numTags); |
3966 | | |
3967 | 186 | IM3Tag tag = &o->module->tags[tagIndex]; |
3968 | 186 | if (tag->resolved) { |
3969 | 0 | tag = tag->resolved; |
3970 | 0 | } |
3971 | 186 | u16 numParams = GetFuncTypeNumParams(tag->type); |
3972 | 186 | u16 numResults = GetFuncTypeNumResults(tag->type); |
3973 | | |
3974 | 186 | _ (PreserveRegisters(o)); |
3975 | | |
3976 | 186 | u16 paramSlots[d_m3MaxContinuationPayload]; |
3977 | 186 | _throwif("too many tag params", numParams > d_m3MaxContinuationPayload); |
3978 | 368 | for (u16 i = 0; i < numParams; ++i) { |
3979 | 196 | u16 pIdx = numParams - 1 - i; |
3980 | 196 | m3type_t expectedType = GetFuncTypeParamType(tag->type, pIdx); |
3981 | 196 | m3type_t actualType = GetStackTopType(o); |
3982 | 196 | _throwif(m3Err_typeMismatch, not IsSubTypeOf(actualType, expectedType)); |
3983 | 186 | paramSlots[pIdx] = GetStackTopSlotNumber(o); |
3984 | 186 | _ (Pop(o)); |
3985 | 184 | } |
3986 | | |
3987 | 172 | u16 resultSlots[d_m3MaxContinuationPayload]; |
3988 | 172 | _throwif("too many tag results", numResults > d_m3MaxContinuationPayload); |
3989 | 372 | for (u16 i = 0; i < numResults; ++i) { |
3990 | 202 | m3type_t rtype = GetFuncTypeResultType(tag->type, i); |
3991 | 202 | _ (PushAllocatedSlot(o, rtype)); |
3992 | 202 | resultSlots[i] = GetStackTopSlotNumber(o); |
3993 | 202 | } |
3994 | | |
3995 | 170 | _ (EnsureCodePageNumLines(o, 4 + numParams * 2 + numResults * 2 + d_m3CodePageFreeLinesThreshold)); |
3996 | 170 | _ (EmitOp(o, op_Suspend)); |
3997 | 170 | EmitPointer(o, tag); |
3998 | 170 | EmitConstant32(o, numParams); |
3999 | 346 | for (u16 i = 0; i < numParams; ++i) { |
4000 | 176 | EmitSlotOffset(o, paramSlots[i]); |
4001 | 176 | EmitConstant32(o, Is64BitType(GetFuncTypeParamType(tag->type, i))); |
4002 | 176 | } |
4003 | 170 | EmitConstant32(o, numResults); |
4004 | 372 | for (u16 i = 0; i < numResults; ++i) { |
4005 | 202 | EmitSlotOffset(o, resultSlots[i]); |
4006 | 202 | EmitConstant32(o, Is64BitType(GetFuncTypeResultType(tag->type, i))); |
4007 | 202 | } |
4008 | | |
4009 | | // the results are part of the frame here: op_Suspend clears them, and |
4010 | | // whatever resumes this writes them before anything reads them |
4011 | 170 | _ (RecordSafePoint(o, safepoint_suspend, 0, numResults, 0, NULL)); |
4012 | | |
4013 | 186 | } _catch: return result; |
4014 | 170 | } |
4015 | | |
4016 | | // resume, resume_throw and resume_throw_ref. All three run a continuation |
4017 | | // under a handler table and take its results; they differ only in what they |
4018 | | // hand it to start with - arguments, a fresh exception, or one off the stack. |
4019 | | // |
4020 | | // Emitted as: the continuation type, the slot holding it, the operands that |
4021 | | // belong to this form, the handler table, then the result slots. Keeping the |
4022 | | // tail identical is what lets one runtime path finish all three. |
4023 | | static |
4024 | | M3Result Compile_Resume (IM3Compilation o, m3opcode_t i_opcode) |
4025 | 0 | { |
4026 | 0 | M3ResumeHandler* handlers = NULL; |
4027 | 0 | IM3CodePage displacedPage = NULL; |
4028 | 0 | _try { |
4029 | 0 | bool isThrow = (i_opcode == c_waOp_resumeThrow); |
4030 | 0 | bool isThrowRef = (i_opcode == c_waOp_resumeThrowRef); |
4031 | |
|
4032 | 0 | u32 typeIndex; |
4033 | 0 | _ (ReadLEB_u32(&typeIndex, &o->wasm, o->wasmEnd)); |
4034 | 0 | _throwif(m3Err_typeCountMismatch, typeIndex >= o->module->numFuncTypes); |
4035 | |
|
4036 | 0 | IM3FuncType contFuncType = o->module->funcTypes[typeIndex]; |
4037 | 0 | _throwif("type is not a continuation", not contFuncType->isContinuation); |
4038 | |
|
4039 | 0 | IM3FuncType innerType = contFuncType->contFuncType; |
4040 | 0 | u16 numContResults = GetFuncTypeNumResults(innerType); |
4041 | |
|
4042 | 0 | IM3Tag exnTag = NULL; |
4043 | 0 | u16 numOperands = 0; |
4044 | |
|
4045 | 0 | if (isThrow) { |
4046 | 0 | u32 tagIndex; |
4047 | 0 | _ (ReadLEB_u32(&tagIndex, &o->wasm, o->wasmEnd)); |
4048 | 0 | _throwif(m3Err_unknownTag, tagIndex >= o->module->numTags); |
4049 | |
|
4050 | 0 | exnTag = &o->module->tags[tagIndex]; |
4051 | | |
4052 | | // it is an exception that gets raised, so the tag must be one throw |
4053 | | // could have raised: no results |
4054 | 0 | _throwif(m3Err_typeMismatch, GetFuncTypeNumResults(exnTag->type) != 0); |
4055 | |
|
4056 | 0 | numOperands = GetFuncTypeNumParams(exnTag->type); |
4057 | 0 | } else if (not isThrowRef) { |
4058 | | // a plain resume hands over the continuation's own parameters. An |
4059 | | // aborted one never receives them, so they are unconstrained there. |
4060 | 0 | numOperands = GetFuncTypeNumParams(innerType); |
4061 | 0 | } |
4062 | | |
4063 | 0 | u32 numClauses; |
4064 | 0 | _ (ReadLEB_u32(&numClauses, &o->wasm, o->wasmEnd)); |
4065 | |
|
4066 | 0 | u32 clauseLabelDepths[16]; |
4067 | 0 | _throwif("too many resume clauses", numClauses > 16); |
4068 | |
|
4069 | 0 | if (numClauses) { |
4070 | 0 | handlers = m3_AllocArray(M3ResumeHandler, numClauses); |
4071 | 0 | _throwifnull(handlers); |
4072 | 0 | } |
4073 | | |
4074 | 0 | for (u32 i = 0; i < numClauses; ++i) { |
4075 | 0 | u8 kind; |
4076 | 0 | _ (Read_u8(&kind, &o->wasm, o->wasmEnd)); |
4077 | 0 | _throwif(m3Err_wasmMalformed, kind > 0x01); |
4078 | |
|
4079 | 0 | handlers[i].kind = kind; |
4080 | |
|
4081 | 0 | u32 tagIndex; |
4082 | 0 | _ (ReadLEB_u32(&tagIndex, &o->wasm, o->wasmEnd)); |
4083 | 0 | _throwif(m3Err_unknownTag, tagIndex >= o->module->numTags); |
4084 | |
|
4085 | 0 | IM3Tag tag = &o->module->tags[tagIndex]; |
4086 | 0 | handlers[i].tag = tag->resolved ? tag->resolved : tag; |
4087 | 0 | handlers[i].stubPC = NULL; |
4088 | |
|
4089 | 0 | if (kind == 0x00) { |
4090 | 0 | _ (ReadLEB_u32(&clauseLabelDepths[i], &o->wasm, o->wasmEnd)); |
4091 | 0 | } else { |
4092 | | // (on $e switch): the tag carries no payload, and what the switch |
4093 | | // site yields is what this resume yields |
4094 | 0 | _throwif(m3Err_typeMismatch, GetFuncTypeNumParams(handlers[i].tag->type) != 0); |
4095 | 0 | clauseLabelDepths[i] = 0; |
4096 | 0 | } |
4097 | 0 | } |
4098 | | |
4099 | | // the operands are read out of slots, so nothing may still be in a register |
4100 | 0 | _ (PreserveRegisters(o)); |
4101 | |
|
4102 | 0 | m3type_t contTopType = GetStackTopType(o); |
4103 | 0 | _throwif(m3Err_typeMismatch, not IsSubTypeOf(contTopType, RefTypeOfFuncType(contFuncType, false))); |
4104 | 0 | u16 contSlot = GetStackTopSlotNumber(o); |
4105 | 0 | _ (Pop(o)); |
4106 | |
|
4107 | 0 | u16 exnSlot = 0; |
4108 | |
|
4109 | 0 | if (isThrowRef) { |
4110 | 0 | m3type_t exnType = GetStackTopType(o); |
4111 | 0 | _throwif(m3Err_typeMismatch, BaseTypeOf(exnType) != c_m3Type_exnref); |
4112 | 0 | exnSlot = GetStackTopSlotNumber(o); |
4113 | 0 | _ (Pop(o)); |
4114 | 0 | } |
4115 | | |
4116 | 0 | u16 operandSlots[d_m3MaxContinuationPayload]; |
4117 | 0 | _throwif("too many resume operands", numOperands > d_m3MaxContinuationPayload); |
4118 | |
|
4119 | 0 | IM3FuncType operandType = isThrow ? exnTag->type : innerType; |
4120 | |
|
4121 | 0 | for (u16 i = 0; i < numOperands; ++i) { |
4122 | 0 | u16 pIdx = (u16)(numOperands - 1 - i); |
4123 | 0 | m3type_t expectedType = GetFuncTypeParamType(operandType, pIdx); |
4124 | 0 | m3type_t actualType = GetStackTopType(o); |
4125 | 0 | _throwif(m3Err_typeMismatch, not IsSubTypeOf(actualType, expectedType)); |
4126 | 0 | operandSlots[pIdx] = GetStackTopSlotNumber(o); |
4127 | 0 | _ (Pop(o)); |
4128 | 0 | } |
4129 | | |
4130 | 0 | for (u32 i = 0; i < numClauses; ++i) { |
4131 | 0 | if (handlers[i].kind != 0x00) { |
4132 | 0 | continue; |
4133 | 0 | } |
4134 | | |
4135 | 0 | IM3CompilationScope scope; |
4136 | 0 | _ (GetBlockScope(o, &scope, clauseLabelDepths[i])); |
4137 | |
|
4138 | 0 | IM3Tag tag = handlers[i].tag; |
4139 | 0 | u16 numPayload = GetFuncTypeNumParams(tag->type); |
4140 | |
|
4141 | 0 | m3type_t labelContType = (scope->opcode == c_waOp_loop) |
4142 | 0 | ? GetFuncTypeParamType(scope->type, numPayload) |
4143 | 0 | : GetFuncTypeResultType(scope->type, numPayload); |
4144 | |
|
4145 | 0 | IM3CodePage stubPage; |
4146 | 0 | _ (AcquireCompilationCodePage(o, &stubPage)); |
4147 | 0 | pc_t startPC = GetPagePC(stubPage); |
4148 | 0 | displacedPage = o->page; |
4149 | 0 | o->page = stubPage; |
4150 | |
|
4151 | 0 | u16 firstIndex = o->stackIndex; |
4152 | 0 | for (u16 a = 0; a < numPayload; ++a) { |
4153 | 0 | _ (PushAllocatedSlot(o, GetFuncTypeParamType(tag->type, a))); |
4154 | 0 | } |
4155 | | |
4156 | | // The handler is handed the suspended continuation. |
4157 | 0 | _ (PushAllocatedSlot(o, labelContType)); |
4158 | |
|
4159 | 0 | _ (EnsureCodePageNumLines(o, 2 * numPayload + 5 + d_m3CodePageFreeLinesThreshold)); |
4160 | 0 | _ (EmitOp(o, op_ResumePayload)); |
4161 | 0 | EmitPointer(o, Module_ContTypeOfRef(o->module, labelContType)); |
4162 | 0 | EmitConstant32(o, numPayload); |
4163 | 0 | for (u16 a = 0; a < numPayload; ++a) { |
4164 | 0 | u16 index = firstIndex + a; |
4165 | 0 | EmitSlotOffset(o, GetSlotForStackIndex(o, index)); |
4166 | 0 | EmitConstant32(o, Is64BitType(GetStackTypeFromBottom(o, index))); |
4167 | 0 | } |
4168 | 0 | EmitSlotOffset(o, GetSlotForStackIndex(o, firstIndex + numPayload)); |
4169 | |
|
4170 | 0 | # if d_m3HasExceptionHandling |
4171 | | // the clause leaves every try region between the resume and its label, |
4172 | | // and leaving one retires its handlers, the way a branch does |
4173 | 0 | _ (EmitPopTryFrames(o, NumTryFramesToPop(&o->block, scope))); |
4174 | 0 | # endif |
4175 | |
|
4176 | 0 | if (scope->opcode == c_waOp_loop) { |
4177 | 0 | _ (ResolveBlockResults(o, scope, true)); |
4178 | 0 | _ (EmitOp(o, Op_ContinueLoop(o))); |
4179 | 0 | EmitPointer(o, scope->pc); |
4180 | 0 | _ (RecordBackEdge(o, scope, false)); |
4181 | 0 | } else if (scope->depth == 0) { |
4182 | 0 | _ (ReturnValues(o, scope, true)); |
4183 | 0 | _ (EmitOp(o, op_Return)); |
4184 | 0 | } else { |
4185 | 0 | _ (ResolveBlockResults(o, scope, true)); |
4186 | 0 | _ (EmitPatchingBranch(o, scope)); |
4187 | 0 | } |
4188 | | |
4189 | 0 | IM3CodePage returnTo = displacedPage; |
4190 | 0 | displacedPage = NULL; |
4191 | |
|
4192 | 0 | ReleaseCompilationCodePage(o); |
4193 | 0 | o->page = returnTo; |
4194 | |
|
4195 | 0 | handlers[i].stubPC = startPC; |
4196 | |
|
4197 | 0 | while (o->stackIndex > firstIndex) { |
4198 | 0 | _ (Pop(o)); |
4199 | 0 | } |
4200 | 0 | } |
4201 | | |
4202 | 0 | u16 resSlots[d_m3MaxContinuationPayload]; |
4203 | 0 | _throwif("too many cont results", numContResults > d_m3MaxContinuationPayload); |
4204 | 0 | for (u16 i = 0; i < numContResults; ++i) { |
4205 | 0 | m3type_t rtype = GetFuncTypeResultType(innerType, i); |
4206 | 0 | _ (PushAllocatedSlot(o, rtype)); |
4207 | 0 | resSlots[i] = GetStackTopSlotNumber(o); |
4208 | 0 | } |
4209 | | |
4210 | 0 | _ (EnsureCodePageNumLines(o, 8 + numOperands * 2 + numClauses * 3 + numContResults * 2 + |
4211 | 0 | d_m3CodePageFreeLinesThreshold)); |
4212 | |
|
4213 | 0 | _ (EmitOp(o, op_Resume)); |
4214 | 0 | EmitPointer(o, contFuncType); |
4215 | 0 | EmitSlotOffset(o, contSlot); |
4216 | 0 | EmitConstant32(o, isThrow ? d_m3ResumeThrow : (isThrowRef ? d_m3ResumeThrowRef : d_m3ResumeNormal)); |
4217 | |
|
4218 | 0 | if (isThrowRef) { |
4219 | 0 | EmitSlotOffset(o, exnSlot); |
4220 | 0 | } else { |
4221 | 0 | if (isThrow) { |
4222 | 0 | EmitPointer(o, exnTag); |
4223 | 0 | } |
4224 | |
|
4225 | 0 | EmitConstant32(o, numOperands); |
4226 | 0 | for (u16 i = 0; i < numOperands; ++i) { |
4227 | 0 | EmitSlotOffset(o, operandSlots[i]); |
4228 | 0 | EmitConstant32(o, Is64BitType(GetFuncTypeParamType(operandType, i))); |
4229 | 0 | } |
4230 | 0 | } |
4231 | |
|
4232 | 0 | EmitConstant32(o, numClauses); |
4233 | 0 | for (u32 i = 0; i < numClauses; ++i) { |
4234 | 0 | EmitConstant32(o, handlers[i].kind); |
4235 | 0 | EmitPointer(o, handlers[i].tag); |
4236 | 0 | EmitPointer(o, handlers[i].stubPC); |
4237 | 0 | } |
4238 | |
|
4239 | 0 | EmitConstant32(o, numContResults); |
4240 | 0 | for (u16 i = 0; i < numContResults; ++i) { |
4241 | 0 | EmitSlotOffset(o, resSlots[i]); |
4242 | 0 | EmitConstant32(o, Is64BitType(GetFuncTypeResultType(innerType, i))); |
4243 | 0 | } |
4244 | | |
4245 | | // a suspend passing through the resumed continuation leaves this frame |
4246 | | // waiting here, with the continuation's results not yet taken |
4247 | 0 | _ (RecordSafePoint(o, safepoint_resume, 0, numContResults, (u16)numClauses, NULL)); |
4248 | 0 | # if d_m3HasSnapshots |
4249 | 0 | if (o->snapshotMap and o->page) { |
4250 | 0 | o->snapshotMap->safePoints[o->snapshotMap->numSafePoints - 1].resumeType = RefTypeOfFuncType(contFuncType, true); |
4251 | 0 | } |
4252 | 0 | # endif |
4253 | |
|
4254 | 0 | } _catch: |
4255 | 0 | if (displacedPage) { |
4256 | 0 | ReleaseCompilationCodePage(o); |
4257 | 0 | o->page = displacedPage; |
4258 | 0 | } |
4259 | 0 | m3_Free(handlers); |
4260 | 0 | return result; |
4261 | 0 | } |
4262 | | |
4263 | | |
4264 | | static |
4265 | | M3Result Compile_Switch (IM3Compilation o, m3opcode_t i_opcode) |
4266 | 0 | { |
4267 | 0 | _try { |
4268 | 0 | u32 typeIndex, tagIndex; |
4269 | 0 | _ (ReadLEB_u32(&typeIndex, &o->wasm, o->wasmEnd)); |
4270 | 0 | _ (ReadLEB_u32(&tagIndex, &o->wasm, o->wasmEnd)); |
4271 | 0 | _throwif(m3Err_typeCountMismatch, typeIndex >= o->module->numFuncTypes); |
4272 | 0 | _throwif(m3Err_unknownTag, tagIndex >= o->module->numTags); |
4273 | |
|
4274 | 0 | IM3FuncType targetContType = o->module->funcTypes[typeIndex]; |
4275 | 0 | IM3Tag tag = &o->module->tags[tagIndex]; |
4276 | 0 | if (tag->resolved) { |
4277 | 0 | tag = tag->resolved; |
4278 | 0 | } |
4279 | |
|
4280 | 0 | _throwif("type is not a continuation", not targetContType->isContinuation); |
4281 | | |
4282 | | // a switch tag carries no payload of its own: the values travel as the |
4283 | | // peer's arguments, and the tag's results are what the switch site yields |
4284 | 0 | _throwif(m3Err_typeMismatch, GetFuncTypeNumParams(tag->type) != 0); |
4285 | | |
4286 | | // switch $ct1 : [t1* (ref null $ct1)] -> [t2*] |
4287 | | // where $ct1 = cont [t1* (ref null? $ct2)] -> [..] |
4288 | | // and $ct2 = cont [t2*] -> [..] |
4289 | | // |
4290 | | // The trailing parameter of $ct1 is not an operand: the peer receives the |
4291 | | // continuation this instruction suspends. And what this instruction yields |
4292 | | // is not $ct1's results but $ct2's parameters - the values whoever comes |
4293 | | // back this way will hand over. |
4294 | 0 | IM3FuncType targetType = targetContType->contFuncType; |
4295 | 0 | u16 numParams = GetFuncTypeNumParams(targetType); |
4296 | |
|
4297 | 0 | _throwif("switch target takes no continuation", numParams == 0); |
4298 | |
|
4299 | 0 | m3type_t peerRefType = GetFuncTypeParamType(targetType, (u16)(numParams - 1)); |
4300 | 0 | IM3FuncType selfContType = Module_ContTypeOfRef(o->module, peerRefType); |
4301 | |
|
4302 | 0 | _throwif(m3Err_typeMismatch, not selfContType); |
4303 | |
|
4304 | 0 | IM3FuncType selfType = selfContType->contFuncType; |
4305 | 0 | u16 numArgs = (u16)(numParams - 1); |
4306 | 0 | u16 numResults = GetFuncTypeNumParams(selfType); |
4307 | | |
4308 | | // the operands are read out of slots, so nothing may still be in a register |
4309 | 0 | _ (PreserveRegisters(o)); |
4310 | |
|
4311 | 0 | m3type_t contTopType = GetStackTopType(o); |
4312 | 0 | _throwif(m3Err_typeMismatch, not IsSubTypeOf(contTopType, RefTypeOfFuncType(targetContType, false))); |
4313 | 0 | u16 targetContSlot = GetStackTopSlotNumber(o); |
4314 | 0 | _ (Pop(o)); |
4315 | |
|
4316 | 0 | u16 argSlots[d_m3MaxContinuationPayload]; |
4317 | 0 | _throwif("too many switch params", numArgs > d_m3MaxContinuationPayload); |
4318 | |
|
4319 | 0 | for (u16 i = 0; i < numArgs; ++i) { |
4320 | 0 | u16 pIdx = (u16)(numArgs - 1 - i); |
4321 | 0 | m3type_t expectedType = GetFuncTypeParamType(targetType, pIdx); |
4322 | 0 | m3type_t actualType = GetStackTopType(o); |
4323 | 0 | _throwif(m3Err_typeMismatch, not IsSubTypeOf(actualType, expectedType)); |
4324 | 0 | argSlots[pIdx] = GetStackTopSlotNumber(o); |
4325 | 0 | _ (Pop(o)); |
4326 | 0 | } |
4327 | | |
4328 | 0 | u16 resultSlots[d_m3MaxContinuationPayload]; |
4329 | 0 | _throwif("too many switch results", numResults > d_m3MaxContinuationPayload); |
4330 | |
|
4331 | 0 | for (u16 i = 0; i < numResults; ++i) { |
4332 | 0 | _ (PushAllocatedSlot(o, GetFuncTypeParamType(selfType, i))); |
4333 | 0 | resultSlots[i] = GetStackTopSlotNumber(o); |
4334 | 0 | } |
4335 | | |
4336 | 0 | _ (EnsureCodePageNumLines(o, 6 + numArgs * 2 + numResults * 2 + d_m3CodePageFreeLinesThreshold)); |
4337 | 0 | _ (EmitOp(o, op_Switch)); |
4338 | 0 | EmitPointer(o, targetContType); |
4339 | 0 | EmitPointer(o, tag); |
4340 | 0 | EmitSlotOffset(o, targetContSlot); |
4341 | 0 | EmitConstant32(o, numArgs); |
4342 | 0 | for (u16 i = 0; i < numArgs; ++i) { |
4343 | 0 | EmitSlotOffset(o, argSlots[i]); |
4344 | 0 | EmitConstant32(o, Is64BitType(GetFuncTypeParamType(targetType, i))); |
4345 | 0 | } |
4346 | 0 | EmitConstant32(o, numResults); |
4347 | 0 | for (u16 i = 0; i < numResults; ++i) { |
4348 | 0 | EmitSlotOffset(o, resultSlots[i]); |
4349 | 0 | EmitConstant32(o, Is64BitType(GetFuncTypeParamType(selfType, i))); |
4350 | 0 | } |
4351 | | |
4352 | | // as with suspend: op_Switch clears the results before it leaves |
4353 | 0 | _ (RecordSafePoint(o, safepoint_suspend, 0, numResults, 0, NULL)); |
4354 | |
|
4355 | 0 | } _catch: return result; |
4356 | 0 | } |
4357 | | |
4358 | | #endif // d_m3HasStackSwitching |
4359 | | |
4360 | | |
4361 | | static |
4362 | | M3Result CompileElseBlock (IM3Compilation o, pc_t* o_startPC, IM3FuncType i_blockType) |
4363 | 329 | { |
4364 | 329 | IM3CodePage savedPage = o->page; |
4365 | 329 | _try { |
4366 | | |
4367 | 329 | IM3CodePage elsePage; |
4368 | 329 | _ (AcquireCompilationCodePage(o, &elsePage)); |
4369 | | |
4370 | 329 | *o_startPC = GetPagePC(elsePage); |
4371 | | |
4372 | 329 | o->page = elsePage; |
4373 | | |
4374 | 329 | _ (CompileBlock(o, i_blockType, c_waOp_else)); |
4375 | | |
4376 | 300 | _ (EmitOp(o, op_Branch)); |
4377 | 300 | EmitPointer(o, GetPagePC(savedPage)); |
4378 | 329 | } _catch: |
4379 | | |
4380 | 329 | if (o->page != savedPage) { |
4381 | 329 | ReleaseCompilationCodePage(o); |
4382 | 329 | } |
4383 | | |
4384 | 329 | o->page = savedPage; |
4385 | | |
4386 | 329 | return result; |
4387 | 300 | } |
4388 | | |
4389 | | static |
4390 | | M3Result Compile_If (IM3Compilation o, m3opcode_t i_opcode) |
4391 | 5.11k | { |
4392 | | /* [ op_If ] |
4393 | | [ <else-pc> ] ----> [ ..else.. ] |
4394 | | [ ..if.. ] [ ..block.. ] |
4395 | | [ ..block.. ] [ op_Branch ] |
4396 | | [ end ] <----- [ <end-pc> ] */ |
4397 | | |
4398 | 5.11k | _try { |
4399 | | |
4400 | | // Spec: if condition must be i32 |
4401 | 5.11k | if (not IsStackPolymorphic(o)) { |
4402 | 4.19k | m3type_t condType = GetStackTopType(o); |
4403 | 4.19k | _throwif(m3Err_typeMismatch, condType != c_m3Type_none and condType != c_m3Type_i32); |
4404 | 4.19k | } |
4405 | | |
4406 | 5.11k | _ (PreserveNonTopRegisters(o)); |
4407 | 5.11k | _ (PreserveArgsAndLocals(o)); |
4408 | | |
4409 | 5.11k | #if d_m3FuseBranch |
4410 | 5.11k | IM3Operation fuseOp = GetBranchFusionOp(o, true); |
4411 | | |
4412 | 5.11k | if (fuseOp) { |
4413 | | // absorb the if into the compare that produced the condition |
4414 | 2.71k | *(IM3Operation*)o->foldPatchPC = fuseOp; |
4415 | 2.71k | InvalidateFold(o); |
4416 | | |
4417 | 2.71k | _ (Pop(o)); // the condition is consumed inside the fused op |
4418 | 2.71k | } else |
4419 | 2.39k | #endif |
4420 | 2.39k | { |
4421 | 2.39k | IM3Operation op = IsStackTopInRegister(o) ? op_If_r : op_If_s; |
4422 | | |
4423 | 2.39k | _ (EmitOp(o, op)); |
4424 | 2.39k | _ (EmitSlotNumOfStackTopAndPop(o)); |
4425 | 2.39k | } |
4426 | | |
4427 | 5.11k | pc_t* pc = (pc_t*)ReservePointer(o); |
4428 | | |
4429 | 5.11k | IM3FuncType blockType = NULL; |
4430 | 5.11k | _ (ReadBlockType(o, &blockType)); // dump_type_stack (o); |
4431 | | |
4432 | 5.11k | u16 stackIndex = o->stackIndex; |
4433 | | |
4434 | 5.11k | _ (CompileBlock(o, blockType, i_opcode)); |
4435 | | |
4436 | 4.99k | if (o->previousOpcode == c_waOp_else) { |
4437 | 171 | o->stackIndex = stackIndex; |
4438 | 171 | _ (CompileElseBlock(o, pc, blockType)); |
4439 | 4.82k | } else { |
4440 | | // if block produces values and there isn't a defined else |
4441 | | // case, then we need to make one up so that the pass-through |
4442 | | // results end up in the right place |
4443 | 4.82k | if (GetFuncTypeNumResults(blockType)) { |
4444 | | // rewind to the if's end to create a fake else block |
4445 | 158 | o->wasm--; |
4446 | 158 | o->stackIndex = stackIndex; // dump_type_stack (o); |
4447 | | |
4448 | 158 | _ (CompileElseBlock(o, pc, blockType)); |
4449 | 4.66k | } else { |
4450 | 4.66k | *pc = GetPC(o); |
4451 | 4.66k | } |
4452 | 4.82k | } |
4453 | | |
4454 | 5.11k | } _catch: return result; |
4455 | 4.99k | } |
4456 | | |
4457 | | static |
4458 | | M3Result Compile_Select (IM3Compilation o, m3opcode_t i_opcode) |
4459 | 7.73k | { |
4460 | 7.73k | M3Result result = m3Err_none; |
4461 | | |
4462 | 7.73k | u16 slots[3] = { c_slotUnused, c_slotUnused, c_slotUnused }; |
4463 | | |
4464 | 7.73k | IM3Operation op = NULL; |
4465 | | |
4466 | 7.73k | m3type_t type = GetStackTypeFromTop(o, 1); // get type of selection |
4467 | | |
4468 | 7.73k | if (not IsStackPolymorphic(o)) { |
4469 | | // Spec: the condition operand (top) must be i32 |
4470 | 1.96k | m3type_t condType = GetStackTypeFromTop(o, 0); |
4471 | 1.96k | _throwif(m3Err_typeMismatch, condType != c_m3Type_none and condType != c_m3Type_i32); |
4472 | | |
4473 | | // Spec: the two value operands (below condition) must have matching types |
4474 | 1.96k | m3type_t type2 = GetStackTypeFromTop(o, 2); |
4475 | 1.96k | _throwif(m3Err_typeMismatch, type != c_m3Type_none and type2 != c_m3Type_none and |
4476 | 1.96k | not (IsSubTypeOf(type, type2) or IsSubTypeOf(type2, type))); |
4477 | 1.96k | } |
4478 | | |
4479 | 7.73k | if (IsFpType(type)) { |
4480 | 1.86k | #if d_m3HasFloat |
4481 | | // not consuming a fp reg, so preserve |
4482 | 1.86k | if (not IsStackTopMinus1InRegister(o) and |
4483 | 1.35k | not IsStackTopMinus2InRegister(o)) { |
4484 | 850 | _ (PreserveRegisterIfOccupied(o, type)); |
4485 | 850 | } |
4486 | | |
4487 | 1.86k | bool selectorInReg = IsStackTopInRegister(o); |
4488 | 1.86k | slots[0] = GetStackTopSlotNumber(o); |
4489 | 1.86k | _ (Pop(o)); |
4490 | | |
4491 | 1.86k | u32 opIndex = 0; |
4492 | | |
4493 | 5.58k | for (u32 i = 1; i <= 2; ++i) { |
4494 | 3.72k | if (IsStackTopInRegister(o)) { |
4495 | 1.02k | opIndex = i; |
4496 | 2.69k | } else { |
4497 | 2.69k | slots[i] = GetStackTopSlotNumber(o); |
4498 | 2.69k | } |
4499 | | |
4500 | 3.72k | _ (Pop(o)); |
4501 | 3.72k | } |
4502 | | |
4503 | 1.86k | op = c_fpSelectOps[type - c_m3Type_f32][selectorInReg][opIndex]; |
4504 | | #else |
4505 | | _throw(m3Err_unknownOpcode); |
4506 | | #endif |
4507 | 5.87k | } else if (IsIntType(type) or IsRefType(type)) { |
4508 | | // 'sss' operation doesn't consume a register, so might have to protected its contents |
4509 | 2.72k | if (not IsStackTopInRegister(o) and |
4510 | 1.22k | not IsStackTopMinus1InRegister(o) and |
4511 | 873 | not IsStackTopMinus2InRegister(o)) { |
4512 | 599 | _ (PreserveRegisterIfOccupied(o, type)); |
4513 | 598 | } |
4514 | | |
4515 | 2.72k | u32 opIndex = 3; // op_Select_*_sss |
4516 | | |
4517 | 10.9k | for (u32 i = 0; i < 3; ++i) { |
4518 | 8.17k | if (IsStackTopInRegister(o)) { |
4519 | 2.48k | opIndex = i; |
4520 | 5.69k | } else { |
4521 | 5.69k | slots[i] = GetStackTopSlotNumber(o); |
4522 | 5.69k | } |
4523 | | |
4524 | 8.17k | _ (Pop(o)); |
4525 | 8.17k | } |
4526 | | |
4527 | | // a reference is a pointer-sized integer as far as select is concerned |
4528 | 2.72k | u32 typeIndex = IsRefType(type) ? ((M3_SIZEOF_PTR == 8) ? 1 : 0) |
4529 | 2.72k | : (u32)(type - c_m3Type_i32); |
4530 | | |
4531 | 2.72k | op = c_intSelectOps[typeIndex][opIndex]; |
4532 | 3.14k | } else if (not IsStackPolymorphic(o)) { |
4533 | 1 | _throw(m3Err_functionStackUnderrun); |
4534 | 0 | } |
4535 | | |
4536 | 7.72k | EmitOp(o, op); |
4537 | 30.9k | for (u32 i = 0; i < 3; i++) { |
4538 | 23.1k | if (IsValidSlot(slots[i])) { |
4539 | 8.91k | EmitSlotOffset(o, slots[i]); |
4540 | 8.91k | } |
4541 | 23.1k | } |
4542 | 7.72k | _ (PushRegister(o, type)); |
4543 | | |
4544 | 7.73k | _catch: return result; |
4545 | 7.72k | } |
4546 | | |
4547 | | static |
4548 | | M3Result Compile_Drop (IM3Compilation o, m3opcode_t i_opcode) |
4549 | 7.05k | { |
4550 | 7.05k | M3Result result = Pop(o); |
4551 | 7.05k | if (d_m3LogWasmStack) { |
4552 | 0 | dump_type_stack(o); |
4553 | 0 | } |
4554 | 7.05k | return result; |
4555 | 7.05k | } |
4556 | | |
4557 | | static |
4558 | | M3Result Compile_Nop (IM3Compilation o, m3opcode_t i_opcode) |
4559 | 18.6k | { |
4560 | 18.6k | return m3Err_none; |
4561 | 18.6k | } |
4562 | | |
4563 | | static |
4564 | | M3Result Compile_Unreachable (IM3Compilation o, m3opcode_t i_opcode) |
4565 | 58.9k | { |
4566 | 58.9k | M3Result result; |
4567 | | |
4568 | 58.9k | _ (AddTrapRecord(o)); |
4569 | | |
4570 | 58.9k | _ (EmitOp(o, op_Unreachable)); |
4571 | 58.9k | _ (SetStackPolymorphic(o)); |
4572 | | |
4573 | 58.9k | _catch: |
4574 | 58.9k | return result; |
4575 | 58.8k | } |
4576 | | |
4577 | | |
4578 | | // OPTZ: currently all stack slot indices take up a full word, but |
4579 | | // dual stack source operands could be packed together |
4580 | | static |
4581 | | M3Result Compile_Operator (IM3Compilation o, m3opcode_t i_opcode) |
4582 | 119k | { |
4583 | 119k | M3Result result; |
4584 | | |
4585 | | // Declared before the first throw below, which jumps past this point to |
4586 | | // _catch and so must not skip an initialization. |
4587 | 119k | IM3Operation op = NULL; |
4588 | | |
4589 | 119k | IM3OpInfo opInfo = GetOpInfo(i_opcode); |
4590 | 119k | _throwif(m3Err_unknownOpcode, not opInfo); |
4591 | | |
4592 | | // Spec: validate operand types for load/store operations |
4593 | 119k | if (not IsStackPolymorphic(o)) { |
4594 | | // For load ops (stackOffset == 0, unary), the operand is always i32 (address) |
4595 | 105k | if (i_opcode >= 0x28 and i_opcode <= 0x35) { |
4596 | 7.77k | m3type_t topType = GetStackTopType(o); |
4597 | 7.77k | _throwif(m3Err_typeMismatch, topType != c_m3Type_none and topType != c_m3Type_i32); |
4598 | 7.77k | } |
4599 | | |
4600 | | // For store ops (stackOffset == -2), the address operand must be i32 |
4601 | 105k | if (i_opcode >= 0x36 and i_opcode <= 0x3e) { |
4602 | 1.79k | m3type_t addrType = GetStackTypeFromTop(o, 1); |
4603 | 1.79k | _throwif(m3Err_typeMismatch, addrType != c_m3Type_none and addrType != c_m3Type_i32); |
4604 | 1.79k | } |
4605 | 105k | } |
4606 | | |
4607 | | // This preserve is for for FP compare operations. |
4608 | | // either need additional slot destination operations or the |
4609 | | // easy fix, move _r0 out of the way. |
4610 | | // moving out the way might be the optimal solution most often? |
4611 | | // otherwise, the _r0 reg can get buried down in the stack |
4612 | | // and be idle & wasted for a moment. |
4613 | 119k | if (M3_UNLIKELY(IsFpType(GetStackTopType(o)) and IsIntType(opInfo->type))) { |
4614 | 4.56k | _ (PreserveRegisterIfOccupied(o, opInfo->type)); |
4615 | 4.56k | } |
4616 | | |
4617 | 119k | if (opInfo->stackOffset == 0) { |
4618 | 38.2k | if (IsStackTopInRegister(o)) { |
4619 | 14.0k | op = opInfo->operations[0]; // _s |
4620 | 24.2k | } else { |
4621 | 24.2k | _ (PreserveRegisterIfOccupied(o, opInfo->type)); |
4622 | 24.2k | op = opInfo->operations[1]; // _r |
4623 | 24.2k | } |
4624 | 81.4k | } else { |
4625 | 81.4k | if (IsStackTopInRegister(o)) { |
4626 | 24.8k | op = opInfo->operations[0]; // _rs |
4627 | | |
4628 | 24.8k | if (IsStackTopMinus1InRegister(o)) { d_m3Assert (i_opcode == c_waOp_store_f32 or i_opcode == c_waOp_store_f64); |
4629 | 74 | op = opInfo->operations[3]; // _rr for fp.store |
4630 | 74 | } |
4631 | 56.6k | } else if (IsStackTopMinus1InRegister(o)) { |
4632 | 19.9k | op = opInfo->operations[1]; // _sr |
4633 | | |
4634 | 19.9k | if (not op) { // must be commutative, then |
4635 | 6.27k | op = opInfo->operations[0]; |
4636 | 6.27k | } |
4637 | 36.6k | } else { |
4638 | 36.6k | _ (PreserveRegisterIfOccupied(o, opInfo->type)); // _ss |
4639 | 36.6k | op = opInfo->operations[2]; |
4640 | 36.6k | } |
4641 | 81.4k | } |
4642 | | |
4643 | 119k | if (M3_LIKELY(op)) { |
4644 | 119k | _ (EmitOp(o, op)); |
4645 | | |
4646 | 119k | #if d_m3FoldSetLocal |
4647 | | // the op word just written (EmitOp may have bridged pages); NULL in the page-less compile-walk mode |
4648 | 119k | pc_t patchPC = o->page ? GetPC(o) - 1 : NULL; |
4649 | 119k | #endif |
4650 | | |
4651 | 119k | _ (EmitSlotNumOfStackTopAndPop(o)); |
4652 | | |
4653 | 119k | if (opInfo->stackOffset < 0) { |
4654 | 81.4k | _ (EmitSlotNumOfStackTopAndPop(o)); |
4655 | 81.4k | } |
4656 | | |
4657 | 119k | if (opInfo->type != c_m3Type_none) { |
4658 | 117k | _ (PushRegister(o, opInfo->type)); |
4659 | | |
4660 | 117k | #if d_m3FoldSetLocal |
4661 | | // record this op as a fold candidate for an immediately following local.set; |
4662 | | // the operand-form index is recovered by matching against the variant table |
4663 | 117k | if (patchPC) { |
4664 | 203k | for (u8 form = 0; form < 4; ++form) { |
4665 | 203k | if (opInfo->operations[form] == op) { |
4666 | 105k | o->foldPatchPC = patchPC; |
4667 | 105k | o->foldOpcode = i_opcode; |
4668 | 105k | o->foldForm = form; |
4669 | 105k | o->foldStackIndex = (u16)GetStackTopIndex(o); |
4670 | 105k | break; |
4671 | 105k | } |
4672 | 203k | } |
4673 | 105k | } |
4674 | 117k | #endif |
4675 | 117k | } |
4676 | 119k | } else { |
4677 | | #ifdef DEBUG |
4678 | | result = ErrorCompile("no operation found for opcode", o, "'%s'", opInfo->name); |
4679 | | #else |
4680 | 20 | result = ErrorCompile("no operation found for opcode", o, "%x", i_opcode); |
4681 | 20 | #endif |
4682 | 20 | _throw(result); |
4683 | 0 | } |
4684 | | |
4685 | 119k | _catch: return result; |
4686 | 119k | } |
4687 | | |
4688 | | static |
4689 | | M3Result Compile_Convert (IM3Compilation o, m3opcode_t i_opcode) |
4690 | 25.5k | { |
4691 | 25.5k | _try { |
4692 | 25.5k | IM3OpInfo opInfo = GetOpInfo(i_opcode); |
4693 | 25.5k | _throwif(m3Err_unknownOpcode, not opInfo); |
4694 | | |
4695 | | // Spec: validate source operand type for conversion instructions |
4696 | 25.5k | if (not IsStackPolymorphic(o)) { |
4697 | 23.4k | u8 sourceType = c_m3Type_none; |
4698 | 23.4k | switch (i_opcode) |
4699 | | // clang-format off |
4700 | 23.4k | { |
4701 | 0 | case 0xa7: // i32.wrap/i64 |
4702 | 0 | sourceType = c_m3Type_i64; break; |
4703 | 858 | case 0xa8: case 0xa9: // i32.trunc_s/f32, i32.trunc_u/f32 |
4704 | 1.84k | case 0xae: case 0xaf: // i64.trunc_s/f32, i64.trunc_u/f32 |
4705 | 1.84k | case 0xbb: // f64.promote/f32 |
4706 | 2.35k | case 0xbc: // i32.reinterpret/f32 |
4707 | 2.35k | sourceType = c_m3Type_f32; break; |
4708 | 821 | case 0xaa: case 0xab: // i32.trunc_s/f64, i32.trunc_u/f64 |
4709 | 1.77k | case 0xb0: case 0xb1: // i64.trunc_s/f64, i64.trunc_u/f64 |
4710 | 1.77k | case 0xb6: // f32.demote/f64 |
4711 | 2.39k | case 0xbd: // i64.reinterpret/f64 |
4712 | 2.39k | sourceType = c_m3Type_f64; break; |
4713 | 0 | case 0xac: case 0xad: // i64.extend_s/i32, i64.extend_u/i32 |
4714 | 2.02k | case 0xb2: case 0xb3: // f32.convert_s/i32, f32.convert_u/i32 |
4715 | 4.55k | case 0xb7: case 0xb8: // f64.convert_s/i32, f64.convert_u/i32 |
4716 | 5.85k | case 0xbe: // f32.reinterpret/i32 |
4717 | 5.85k | sourceType = c_m3Type_i32; break; |
4718 | 2.36k | case 0xb4: case 0xb5: // f32.convert_s/i64, f32.convert_u/i64 |
4719 | 5.94k | case 0xb9: case 0xba: // f64.convert_s/i64, f64.convert_u/i64 |
4720 | 8.01k | case 0xbf: // f64.reinterpret/i64 |
4721 | 8.01k | sourceType = c_m3Type_i64; break; |
4722 | 4.84k | default: break; |
4723 | 23.4k | } |
4724 | | // clang-format on |
4725 | | |
4726 | 23.4k | if (sourceType != c_m3Type_none) { |
4727 | 18.6k | m3type_t topType = GetStackTopType(o); |
4728 | 18.6k | _throwif(m3Err_typeMismatch, topType != c_m3Type_none and not IsSubTypeOf(topType, sourceType)); |
4729 | 18.6k | } |
4730 | 23.4k | } |
4731 | | |
4732 | 25.5k | bool destInSlot = IsRegisterTypeAllocated(o, opInfo->type); |
4733 | 25.5k | bool sourceInSlot = IsStackTopInSlot(o); |
4734 | | |
4735 | 25.5k | IM3Operation op = opInfo->operations[destInSlot * 2 + sourceInSlot]; |
4736 | | |
4737 | 25.5k | _ (EmitOp(o, op)); |
4738 | 25.5k | _ (EmitSlotNumOfStackTopAndPop(o)); |
4739 | | |
4740 | 25.5k | if (destInSlot) { |
4741 | 9.55k | _ (PushAllocatedSlotAndEmit(o, opInfo->type)) |
4742 | 16.0k | } else { |
4743 | 16.0k | _ (PushRegister(o, opInfo->type)) |
4744 | 16.0k | } |
4745 | 25.5k | } |
4746 | 25.5k | _catch: return result; |
4747 | 25.5k | } |
4748 | | |
4749 | | #if d_m3HasWideArithmetic |
4750 | | |
4751 | | // The wide arithmetic proposal: i64.add128 and i64.sub128 over two 128-bit values |
4752 | | // given as i64 halves, and i64.mul_wide_s/u giving the full product of two i64s. |
4753 | | // Each gives back a 128-bit result the same way, low half first. The operands are |
4754 | | // all read from slots, and of the two results the high half, which ends up on top, |
4755 | | // goes to _r0 and the low half to a newly allocated slot - so every operation has |
4756 | | // the one form, whatever shape the stack was in. |
4757 | | static |
4758 | | M3Result Compile_WideArithmetic (IM3Compilation o, m3opcode_t i_opcode) |
4759 | 932 | { |
4760 | 932 | M3Result result = m3Err_none; |
4761 | | |
4762 | 932 | IM3Operation op = op_i64_Add128; |
4763 | 932 | u32 numOperands = 4; |
4764 | | |
4765 | 932 | switch (i_opcode) |
4766 | | // clang-format off |
4767 | 932 | { |
4768 | 252 | case c_waOp_i64Sub128: op = op_i64_Sub128; break; |
4769 | 440 | case c_waOp_i64MulWideS: op = op_i64_MultiplyWide; numOperands = 2; break; |
4770 | 77 | case c_waOp_i64MulWideU: op = op_u64_MultiplyWide; numOperands = 2; break; |
4771 | 163 | default: break; |
4772 | 932 | } |
4773 | | // clang-format on |
4774 | | |
4775 | 932 | _ (PreserveRegisterIfOccupied(o, c_m3Type_i64)); |
4776 | | |
4777 | 931 | _ (EmitOp(o, op)); |
4778 | | |
4779 | | // the top of the stack first: the high half of the second operand, for the |
4780 | | // 128-bit ones |
4781 | 3.62k | for (u32 i = 0; i < numOperands; ++i) { |
4782 | 2.69k | _ (EmitSlotNumOfStackTopAndPop(o)); |
4783 | 2.68k | } |
4784 | | |
4785 | 930 | _ (PushAllocatedSlotAndEmit(o, c_m3Type_i64)); // the low half |
4786 | 928 | _ (PushRegister(o, c_m3Type_i64)); // the high half |
4787 | | |
4788 | 932 | _catch: return result; |
4789 | 927 | } |
4790 | | |
4791 | | #endif // d_m3HasWideArithmetic |
4792 | | |
4793 | | #if d_m3HasMemory64 |
4794 | | |
4795 | | // Replaces the 64-bit address operand of a load or store with the checked |
4796 | | // 32-bit effective address op_CheckAddr64 leaves behind, so that the access |
4797 | | // itself compiles exactly as it would against a 32-bit memory: one slot to read |
4798 | | // the address from, and an offset of zero folded in already. |
4799 | | // |
4800 | | // Where the address sits under other operands - the value of a store, the |
4801 | | // operands of an atomic - it is rewritten where it stands rather than popped: |
4802 | | // its stack entry is repointed at the scratch slot and retyped, and the slot it |
4803 | | // used to occupy released. i_numOperands counts the address and all above it. |
4804 | | static |
4805 | | M3Result EmitCheckAddr64 (IM3Compilation o, u64 i_offset, u16 i_numOperands) |
4806 | 2.02k | { |
4807 | 2.02k | M3Result result = m3Err_none; |
4808 | | |
4809 | | // The address has to be in a slot for op_CheckAddr64 to read it. Spilling |
4810 | | // r0 wholesale also keeps the value of a store out of it, which is what |
4811 | | // leaves the address as the one operand still in a register to worry about. |
4812 | 2.02k | _ (PreserveRegisterIfOccupied(o, c_m3Type_i64)); |
4813 | | |
4814 | | // unreachable code: the operands the instruction names may not be there |
4815 | 2.02k | if (o->stackIndex < o->block.blockStackIndex + i_numOperands) { |
4816 | 367 | return result; |
4817 | 367 | } |
4818 | | |
4819 | 1.65k | { |
4820 | 1.65k | u16 stackIndex = (u16)(o->stackIndex - i_numOperands); |
4821 | 1.65k | u16 srcSlot = o->wasmStack[stackIndex]; |
4822 | 1.65k | m3type_t srcType = o->typeStack[stackIndex]; |
4823 | | |
4824 | 1.65k | u16 dstSlot = c_slotUnused; |
4825 | | |
4826 | | // Checked here rather than left to the validator, which a build may |
4827 | | // have switched off: reading a slot as an i64 that only holds an i32 |
4828 | | // would reach past what was allocated for it. |
4829 | 1.65k | _throwif(m3Err_typeMismatch, BaseTypeOf(srcType) != c_m3Type_i64); |
4830 | | |
4831 | | // an address is an integer, so nothing should be left in a register |
4832 | | // once r0 has been preserved |
4833 | 1.65k | _throwif(m3Err_typeMismatch, IsRegisterSlotAlias(srcSlot)); |
4834 | 1.65k | _throwif(m3Err_functionStackUnderrun, srcSlot >= o->slotMaxAllocatedIndexPlusOne); |
4835 | | |
4836 | | // allocated before the source is released, so the two cannot be the |
4837 | | // same slot - op_CheckAddr64 reads one and writes the other |
4838 | 1.65k | _ (AllocateSlots(o, &dstSlot, c_m3Type_i32)); |
4839 | | |
4840 | 1.65k | _ (EmitOp(o, op_CheckAddr64)); |
4841 | 1.65k | EmitSlotOffset(o, srcSlot); |
4842 | 1.65k | EmitSlotOffset(o, dstSlot); |
4843 | 1.65k | EmitConstant64(o, i_offset); |
4844 | | |
4845 | 1.65k | o->wasmStack[stackIndex] = dstSlot; |
4846 | 1.65k | o->typeStack[stackIndex] = c_m3Type_i32; |
4847 | | |
4848 | | // locals and the constant table outlive the instruction; a dynamic |
4849 | | // slot the address was computed into does not |
4850 | 1.65k | if (srcSlot >= o->slotFirstDynamicIndex) { |
4851 | 928 | DeallocateSlot(o, srcSlot, srcType); |
4852 | 928 | } |
4853 | 1.65k | } |
4854 | | |
4855 | 1.66k | _catch: return result; |
4856 | 1.65k | } |
4857 | | |
4858 | | #endif // d_m3HasMemory64 |
4859 | | |
4860 | | |
4861 | | static |
4862 | | M3Result Compile_Load_Store (IM3Compilation o, m3opcode_t i_opcode) |
4863 | 10.7k | { |
4864 | 10.7k | _try { |
4865 | 10.7k | u32 alignHint, memoryIdx; |
4866 | 10.7k | u64 memoryOffset; |
4867 | | |
4868 | | // alignHint is checked by the validator |
4869 | 10.7k | _ (ReadMemoryArg(&alignHint, &memoryIdx, &memoryOffset, &o->wasm, o->wasmEnd)); |
4870 | 10.7k | m3log (compile, d_indent " (memory = %d; offset = %llu)", get_indention_string (o), memoryIdx, (unsigned long long) memoryOffset); |
4871 | 10.7k | _throwif(m3Err_unknownMemory, memoryIdx >= o->module->numMemories); |
4872 | | |
4873 | 10.7k | IM3OpInfo opInfo = GetOpInfo(i_opcode); |
4874 | 10.7k | _throwif(m3Err_unknownOpcode, not opInfo); |
4875 | | |
4876 | 10.7k | bool isMemory64 = o->module->memories[memoryIdx]->isMemory64; |
4877 | | |
4878 | | // Spec: the static offset has to be in range of the address type. Checked |
4879 | | // here as well as in the validator, so that a build without one still |
4880 | | // refuses the module rather than silently truncating the offset. |
4881 | 10.7k | _throwif(m3Err_wasmMalformed, not isMemory64 and memoryOffset > 0xFFFFFFFFull); |
4882 | | |
4883 | 10.7k | if (IsFpType(opInfo->type)) { |
4884 | 753 | _ (PreserveRegisterIfOccupied(o, c_m3Type_f64)); |
4885 | 752 | } |
4886 | | |
4887 | 10.7k | if (memoryIdx) { |
4888 | 22 | _ (EmitSetMemory(o, memoryIdx)); |
4889 | 22 | } |
4890 | | |
4891 | 10.7k | #if d_m3HasMemory64 |
4892 | 10.7k | if (isMemory64) { |
4893 | | // An offset this far out puts every address out of bounds, so rather |
4894 | | // than carrying it, clamp it to the limit: op_CheckAddr64 then traps |
4895 | | // on whatever address it is given, which is the outcome either way. |
4896 | 1.60k | if (memoryOffset > d_m3AddressLimit) { |
4897 | 19 | memoryOffset = d_m3AddressLimit; |
4898 | 19 | } |
4899 | | |
4900 | 1.60k | _ (EmitCheckAddr64(o, memoryOffset, opInfo->stackOffset < 0 ? 2 : 1)); |
4901 | | |
4902 | | // op_CheckAddr64 has folded the offset in already |
4903 | 1.60k | memoryOffset = 0; |
4904 | 1.60k | } |
4905 | 10.7k | #endif |
4906 | | |
4907 | 10.7k | _ (Compile_Operator(o, i_opcode)); |
4908 | | |
4909 | 10.7k | EmitConstant32(o, (u32)memoryOffset); |
4910 | | |
4911 | 10.7k | if (memoryIdx) { |
4912 | 22 | _ (EmitSetMemory(o, 0)); |
4913 | 22 | } |
4914 | 10.7k | } |
4915 | 10.7k | _catch: return result; |
4916 | 10.7k | } |
4917 | | |
4918 | | |
4919 | | #if d_m3HasAtomics |
4920 | | |
4921 | | // What an atomic instruction takes and gives back, from its sub-opcode. The |
4922 | | // accesses come in runs of seven, one per combination of value type and cell width: |
4923 | | // i32/32, i64/64, i32/8, i32/16, i64/8, i64/16, i64/32. Those runs are told apart by |
4924 | | // where they start. |
4925 | | // |
4926 | | // The operand count includes the address. The top operand is the one that ends up |
4927 | | // in a register, so its type is the one to check on the way out; a store has no |
4928 | | // result, which is c_m3Type_none. |
4929 | | static |
4930 | | void GetAtomicShape (u32 i_sub, u16* o_numOperands, m3type_t* o_topType, m3type_t* o_resultType) |
4931 | 1.06k | { |
4932 | | // clang-format off |
4933 | 1.06k | static const m3type_t c_valueTypes [7] = { |
4934 | 1.06k | c_m3Type_i32, c_m3Type_i64, |
4935 | 1.06k | c_m3Type_i32, c_m3Type_i32, |
4936 | 1.06k | c_m3Type_i64, c_m3Type_i64, c_m3Type_i64 |
4937 | 1.06k | }; |
4938 | | // clang-format on |
4939 | | |
4940 | 1.06k | if (i_sub == 0x00) { // memory.atomic.notify: address, count |
4941 | 107 | *o_numOperands = 2; |
4942 | 107 | *o_topType = c_m3Type_i32; |
4943 | 107 | *o_resultType = c_m3Type_i32; |
4944 | 954 | } else if (i_sub <= 0x02) { // memory.atomic.wait32/64: address, expected, timeout |
4945 | 99 | *o_numOperands = 3; |
4946 | 99 | *o_topType = c_m3Type_i64; |
4947 | 99 | *o_resultType = c_m3Type_i32; |
4948 | 855 | } else if (i_sub <= 0x16) { // loads: address |
4949 | 143 | *o_numOperands = 1; |
4950 | 143 | *o_topType = c_m3Type_i32; |
4951 | 143 | *o_resultType = c_valueTypes[i_sub - 0x10]; |
4952 | 712 | } else if (i_sub <= 0x1d) { // stores: address, value |
4953 | 119 | *o_numOperands = 2; |
4954 | 119 | *o_topType = c_valueTypes[i_sub - 0x17]; |
4955 | 119 | *o_resultType = c_m3Type_none; |
4956 | 593 | } else if (i_sub <= 0x47) { // read-modify-write: address, value |
4957 | 436 | *o_numOperands = 2; |
4958 | 436 | *o_topType = c_valueTypes[(i_sub - 0x1e) % 7]; |
4959 | 436 | *o_resultType = *o_topType; |
4960 | 436 | } else { // cmpxchg: address, expected, replacement |
4961 | 157 | *o_numOperands = 3; |
4962 | 157 | *o_topType = c_valueTypes[i_sub - 0x48]; |
4963 | 157 | *o_resultType = *o_topType; |
4964 | 157 | } |
4965 | 1.06k | } |
4966 | | |
4967 | | |
4968 | | // The description an access takes as its last immediate: see m3_exec_atomic.h |
4969 | | static |
4970 | | u32 GetAtomicDesc (u32 i_sub) |
4971 | 854 | { |
4972 | | // per variant, the log2 of the width of the cell |
4973 | 854 | static const u8 c_log2Widths[7] = { 2, 3, 0, 1, 0, 1, 2 }; |
4974 | | |
4975 | 854 | u32 variant, kind = 0; |
4976 | | |
4977 | 854 | if (i_sub <= 0x16) { |
4978 | 143 | variant = i_sub - 0x10; |
4979 | 711 | } else if (i_sub <= 0x1d) { |
4980 | 119 | variant = i_sub - 0x17; |
4981 | 592 | } else if (i_sub <= 0x47) { |
4982 | 435 | variant = (i_sub - 0x1e) % 7; |
4983 | 435 | kind = (i_sub - 0x1e) / 7; // add, sub, and, or, xor, xchg |
4984 | 435 | } else { |
4985 | 157 | variant = i_sub - 0x48; |
4986 | 157 | } |
4987 | | |
4988 | | // variants 1 and 4..6 are the i64 ones |
4989 | 854 | u32 is64 = (variant == 1 or variant >= 4) ? 1u : 0u; |
4990 | | |
4991 | 854 | return c_log2Widths[variant] | (is64 << 2) | (kind << 3); |
4992 | 854 | } |
4993 | | |
4994 | | |
4995 | | // Every atomic access is one op, whose operands are the topmost in _r0 and the rest |
4996 | | // in slots, topmost first. The address is always the deepest, so it is the last slot |
4997 | | // (or, for a load, the register), followed by the memarg offset. The ops check the |
4998 | | // address themselves: for an atomic that includes its alignment, which only the |
4999 | | // effective address can be asked. |
5000 | | static |
5001 | | M3Result Compile_Atomic (IM3Compilation o, m3opcode_t i_opcode) |
5002 | 1.08k | { |
5003 | 1.08k | _try { |
5004 | 1.08k | u32 alignHint, memoryIdx; |
5005 | 1.08k | u64 memoryOffset; |
5006 | | |
5007 | 1.08k | u16 numOperands = 0; |
5008 | 1.08k | m3type_t topType = c_m3Type_none; |
5009 | 1.08k | m3type_t resultType = c_m3Type_none; |
5010 | | |
5011 | 1.08k | if (i_opcode == c_waOp_atomicFence) { |
5012 | | // the reserved byte, which the validator has checked to be zero. There |
5013 | | // is nothing to order between accesses that one thread makes in sequence. |
5014 | 28 | u8 reserved; |
5015 | 28 | _ (Read_u8(&reserved, &o->wasm, o->wasmEnd)); |
5016 | 1.06k | } else { |
5017 | | // alignHint is checked by the validator |
5018 | 1.06k | _ (ReadMemoryArg(&alignHint, &memoryIdx, &memoryOffset, &o->wasm, o->wasmEnd)); |
5019 | 1.06k | m3log (compile, d_indent " (memory = %d; offset = %llu)", get_indention_string (o), memoryIdx, (unsigned long long) memoryOffset); |
5020 | 1.06k | _throwif(m3Err_unknownMemory, memoryIdx >= o->module->numMemories); |
5021 | | |
5022 | 1.06k | IM3OpInfo opInfo = GetOpInfo(i_opcode); |
5023 | 1.06k | _throwif(m3Err_unknownOpcode, not opInfo); |
5024 | | |
5025 | 1.06k | bool isMemory64 = o->module->memories[memoryIdx]->isMemory64; |
5026 | | |
5027 | | // see Compile_Load_Store |
5028 | 1.06k | _throwif(m3Err_wasmMalformed, not isMemory64 and memoryOffset > 0xFFFFFFFFull); |
5029 | | |
5030 | 1.06k | GetAtomicShape(i_opcode & 0xFF, &numOperands, &topType, &resultType); |
5031 | | |
5032 | 1.06k | if (memoryIdx) { |
5033 | 12 | _ (EmitSetMemory(o, memoryIdx)); |
5034 | 12 | } |
5035 | | |
5036 | 1.06k | # if d_m3HasMemory64 |
5037 | 1.06k | if (isMemory64) { |
5038 | | // see Compile_Load_Store |
5039 | 423 | if (memoryOffset > d_m3AddressLimit) { |
5040 | 20 | memoryOffset = d_m3AddressLimit; |
5041 | 20 | } |
5042 | | |
5043 | 423 | _ (EmitCheckAddr64(o, memoryOffset, numOperands)); |
5044 | 422 | memoryOffset = 0; |
5045 | 422 | } |
5046 | 1.06k | # endif |
5047 | | |
5048 | 1.06k | _ (CopyStackTopToRegister(o, false)); |
5049 | 1.06k | _ (EmitOp(o, opInfo->operations[0])); |
5050 | 1.06k | _ (PopType(o, topType)); |
5051 | | |
5052 | 2.22k | for (u16 i = 1; i < numOperands; ++i) { |
5053 | 1.17k | _ (EmitSlotNumOfStackTopAndPop(o)); |
5054 | 1.16k | } |
5055 | | |
5056 | 1.05k | EmitConstant32(o, (u32)memoryOffset); |
5057 | 1.05k | if (i_opcode >= c_waOp_atomicFirstAccess) { |
5058 | 854 | EmitConstant32(o, GetAtomicDesc(i_opcode & 0xFF)); |
5059 | 854 | } |
5060 | | |
5061 | 1.05k | if (memoryIdx) { |
5062 | 12 | _ (EmitSetMemory(o, 0)); |
5063 | 12 | } |
5064 | | |
5065 | 1.05k | if (resultType != c_m3Type_none) { |
5066 | 939 | _ (PushRegister(o, resultType)); |
5067 | 938 | } |
5068 | 1.05k | } |
5069 | 1.08k | } |
5070 | 1.08k | _catch: return result; |
5071 | 1.08k | } |
5072 | | |
5073 | | #endif // d_m3HasAtomics |
5074 | | |
5075 | | |
5076 | | M3Result CompileRawFunction (IM3Module io_module, IM3Function io_function, const void* i_function, const void* i_userdata) |
5077 | 0 | { |
5078 | 0 | d_m3Assert(io_module->runtime); |
5079 | |
|
5080 | 0 | IM3CodePage page = AcquireCodePageWithCapacity(io_module->runtime, 4); |
5081 | |
|
5082 | 0 | if (page) { |
5083 | 0 | io_function->compiled = GetPagePC(page); |
5084 | 0 | io_function->module = io_module; |
5085 | | |
5086 | | // Unless a host module's ABI names a memory (m3_BindImportMemory), a |
5087 | | // host function addresses memory 0 - what a bare i32 guest pointer means |
5088 | 0 | io_function->hostMemory = io_module->memory0; |
5089 | |
|
5090 | 0 | EmitWord(page, op_CallRawFunction); |
5091 | 0 | EmitWord(page, i_function); |
5092 | 0 | EmitWord(page, io_function); |
5093 | 0 | EmitWord(page, i_userdata); |
5094 | |
|
5095 | 0 | ReleaseCodePage(io_module->runtime, page); |
5096 | 0 | return m3Err_none; |
5097 | 0 | } else { |
5098 | 0 | return m3Err_mallocFailedCodePage; |
5099 | 0 | } |
5100 | 0 | } |
5101 | | |
5102 | | |
5103 | | // A table entry names every field it sets, so the struct can order them for |
5104 | | // packing; the columns follow that order, since M3OP names them positionally. |
5105 | | // |
5106 | | // d_logOp, d_logOp2 macros aren't actually used by the compiler, just codepage decoding (d_m3LogCodePages = 1) |
5107 | | #define d_ops(...) .operations = { __VA_ARGS__ } |
5108 | | |
5109 | | #define d_logOp(OP) d_ops( op_##OP, NULL, NULL, NULL ) |
5110 | | #define d_logOp2(OP1,OP2) d_ops( op_##OP1, op_##OP2, NULL, NULL ) |
5111 | | |
5112 | | #define d_emptyOpList d_ops( NULL, NULL, NULL, NULL ) |
5113 | | #define d_unaryOpList(TYPE, NAME) d_ops( op_##TYPE##_##NAME##_r, op_##TYPE##_##NAME##_s, NULL, NULL ) |
5114 | | #define d_binOpList(TYPE, NAME) d_ops( op_##TYPE##_##NAME##_rs, op_##TYPE##_##NAME##_sr, op_##TYPE##_##NAME##_ss, NULL ) |
5115 | | #define d_storeFpOpList(TYPE, NAME) d_ops( op_##TYPE##_##NAME##_rs, op_##TYPE##_##NAME##_sr, op_##TYPE##_##NAME##_ss, op_##TYPE##_##NAME##_rr ) |
5116 | | #define d_commutativeBinOpList(TYPE, NAME) d_ops( op_##TYPE##_##NAME##_rs, NULL, op_##TYPE##_##NAME##_ss, NULL ) |
5117 | | |
5118 | | #define d_convertOpList(OP) d_ops( op_##OP##_r_r, op_##OP##_r_s, op_##OP##_s_r, op_##OP##_s_s ) |
5119 | | |
5120 | | |
5121 | | // Every M3Compiler the tables below name, as one list, so that the index enum and |
5122 | | // the array of pointers can never drift apart. Each entry must carry the same |
5123 | | // config guard as the function's definition. |
5124 | | #define d_m3CompilerList(_) \ |
5125 | | _( Compile_Unreachable ) \ |
5126 | | _( Compile_Nop ) \ |
5127 | | _( Compile_LoopOrBlock ) \ |
5128 | | _( Compile_If ) \ |
5129 | | _( Compile_End ) \ |
5130 | | _( Compile_Branch ) \ |
5131 | | _( Compile_BranchTable ) \ |
5132 | | _( Compile_Return ) \ |
5133 | | _( Compile_Call ) \ |
5134 | | _( Compile_CallIndirect ) \ |
5135 | | _( Compile_Drop ) \ |
5136 | | _( Compile_Select ) \ |
5137 | | _( Compile_GetLocal ) \ |
5138 | | _( Compile_SetLocal ) \ |
5139 | | _( Compile_GetSetGlobal ) \ |
5140 | | _( Compile_Load_Store ) \ |
5141 | | _( Compile_Memory_Size ) \ |
5142 | | _( Compile_Memory_Grow ) \ |
5143 | | _( Compile_Memory_Init ) \ |
5144 | | _( Compile_Memory_CopyFill ) \ |
5145 | | _( Compile_Data_Drop ) \ |
5146 | | _( Compile_Const_i32 ) \ |
5147 | | _( Compile_Const_i64 ) \ |
5148 | | _( Compile_Convert ) \ |
5149 | | _( Compile_ExtendedOpcode ) \ |
5150 | | d_m3CompilerList_atomics( _ ) \ |
5151 | | d_m3CompilerList_f( _ ) \ |
5152 | | d_m3CompilerList_refTypes( _ ) \ |
5153 | | d_m3CompilerList_typedRefs( _ ) \ |
5154 | | d_m3CompilerList_eh( _ ) \ |
5155 | | d_m3CompilerList_wide( _ ) \ |
5156 | | d_m3CompilerList_stackSwitching( _ ) |
5157 | | |
5158 | | #if d_m3ImplementFloat |
5159 | | # define d_m3CompilerList_f(_) \ |
5160 | | _( Compile_Const_f32 ) \ |
5161 | | _( Compile_Const_f64 ) |
5162 | | #else |
5163 | | # define d_m3CompilerList_f(_) |
5164 | | #endif |
5165 | | |
5166 | | #if d_m3HasRefTypes |
5167 | | # define d_m3CompilerList_refTypes(_) \ |
5168 | | _( Compile_Select_Typed ) \ |
5169 | | _( Compile_Ref_Null ) \ |
5170 | | _( Compile_Ref_IsNull ) \ |
5171 | | _( Compile_Ref_Func ) \ |
5172 | | _( Compile_Table_GetSet ) \ |
5173 | | _( Compile_Table_Init ) \ |
5174 | | _( Compile_Table_Copy ) \ |
5175 | | _( Compile_Table_Size ) \ |
5176 | | _( Compile_Table_GrowFill ) \ |
5177 | | _( Compile_Elem_Drop ) |
5178 | | #else |
5179 | | # define d_m3CompilerList_refTypes(_) |
5180 | | #endif |
5181 | | |
5182 | | #if d_m3HasTypedRefs |
5183 | | # define d_m3CompilerList_typedRefs(_) \ |
5184 | | _( Compile_CallRef ) \ |
5185 | | _( Compile_Ref_AsNonNull ) |
5186 | | #else |
5187 | | # define d_m3CompilerList_typedRefs(_) |
5188 | | #endif |
5189 | | |
5190 | | #if d_m3HasExceptionHandling |
5191 | | # define d_m3CompilerList_eh(_) \ |
5192 | | _( Compile_Throw ) \ |
5193 | | _( Compile_ThrowRef ) \ |
5194 | | _( Compile_TryTable ) |
5195 | | #else |
5196 | | # define d_m3CompilerList_eh(_) |
5197 | | #endif |
5198 | | |
5199 | | #if d_m3HasAtomics |
5200 | | # define d_m3CompilerList_atomics(_) \ |
5201 | | _( Compile_Atomic ) |
5202 | | #else |
5203 | | # define d_m3CompilerList_atomics(_) |
5204 | | #endif |
5205 | | |
5206 | | #if d_m3HasWideArithmetic |
5207 | | # define d_m3CompilerList_wide(_) \ |
5208 | | _( Compile_WideArithmetic ) |
5209 | | #else |
5210 | | # define d_m3CompilerList_wide(_) |
5211 | | #endif |
5212 | | |
5213 | | #if d_m3HasStackSwitching |
5214 | | # define d_m3CompilerList_stackSwitching(_) \ |
5215 | | _( Compile_ContNew ) \ |
5216 | | _( Compile_ContBind ) \ |
5217 | | _( Compile_Suspend ) \ |
5218 | | _( Compile_Resume ) \ |
5219 | | _( Compile_Switch ) |
5220 | | #else |
5221 | | # define d_m3CompilerList_stackSwitching(_) |
5222 | | #endif |
5223 | | |
5224 | | // Index 0 is "this opcode has no compiler", so that a zeroed entry - which is |
5225 | | // what M3OP_RESERVED and the reserved holes in the tables are - reads as one. |
5226 | | enum { |
5227 | | c_cc_NULL = 0, |
5228 | | #define d_m3CompilerEnum(NAME) c_cc_##NAME, |
5229 | | d_m3CompilerList(d_m3CompilerEnum) |
5230 | | #undef d_m3CompilerEnum |
5231 | | }; |
5232 | | |
5233 | | static const M3Compiler c_compilers[] = { |
5234 | | NULL, |
5235 | | #define d_m3CompilerPtr(NAME) NAME, |
5236 | | d_m3CompilerList(d_m3CompilerPtr) |
5237 | | #undef d_m3CompilerPtr |
5238 | | }; |
5239 | | |
5240 | | // Names a table entry's compiler by index. d_cc (NULL) is the no-compiler entry. |
5241 | | #define d_cc(NAME) .compiler = c_cc_##NAME |
5242 | | |
5243 | | static |
5244 | | M3Compiler GetCompiler (IM3OpInfo i_opInfo) |
5245 | 448k | { |
5246 | 448k | d_m3Assert(i_opInfo->compiler < M3_COUNT_OF(c_compilers)); |
5247 | 448k | return c_compilers[i_opInfo->compiler]; |
5248 | 448k | } |
5249 | | |
5250 | | // clang-format off |
5251 | | const M3OpInfo c_operations[] = |
5252 | | { |
5253 | | M3OP( "unreachable", 0, none, d_cc(Compile_Unreachable), d_logOp (Unreachable) ), // 0x00 |
5254 | | M3OP( "nop", 0, none, d_cc(Compile_Nop), d_emptyOpList ), // 0x01 . |
5255 | | M3OP( "block", 0, none, d_cc(Compile_LoopOrBlock), d_emptyOpList ), // 0x02 |
5256 | | M3OP( "loop", 0, none, d_cc(Compile_LoopOrBlock), d_logOp (Loop) ), // 0x03 |
5257 | | M3OP( "if", -1, none, d_cc(Compile_If), d_emptyOpList ), // 0x04 |
5258 | | M3OP( "else", 0, none, d_cc(Compile_Nop), d_emptyOpList ), // 0x05 |
5259 | | |
5260 | | M3OP_RESERVED, M3OP_RESERVED, // 0x06...0x07 |
5261 | | |
5262 | | #if d_m3HasExceptionHandling |
5263 | | M3OP( "throw", 0, none, d_cc(Compile_Throw), d_logOp (Throw) ), // 0x08 |
5264 | | M3OP_RESERVED, // 0x09 |
5265 | | M3OP( "throw_ref", 0, none, d_cc(Compile_ThrowRef), d_logOp (ThrowRef) ), // 0x0a |
5266 | | #else |
5267 | | M3OP_RESERVED, M3OP_RESERVED, M3OP_RESERVED, // 0x08...0x0a |
5268 | | #endif |
5269 | | |
5270 | | M3OP( "end", 0, none, d_cc(Compile_End), d_emptyOpList ), // 0x0b |
5271 | | M3OP( "br", 0, none, d_cc(Compile_Branch), d_logOp (Branch) ), // 0x0c |
5272 | | M3OP( "br_if", -1, none, d_cc(Compile_Branch), d_logOp2 (BranchIf_r, BranchIf_s) ), // 0x0d |
5273 | | M3OP( "br_table", -1, none, d_cc(Compile_BranchTable), d_logOp (BranchTable) ), // 0x0e |
5274 | | M3OP( "return", 0, any, d_cc(Compile_Return), d_logOp (Return) ), // 0x0f |
5275 | | M3OP( "call", 0, any, d_cc(Compile_Call), d_logOp (Call) ), // 0x10 |
5276 | | M3OP( "call_indirect", 0, any, d_cc(Compile_CallIndirect), d_logOp (CallIndirect) ), // 0x11 |
5277 | | M3OP( "return_call", 0, any, d_cc(Compile_Call), d_logOp (ReturnCall) ), // 0x12 |
5278 | | M3OP( "return_call_indirect", 0, any, d_cc(Compile_CallIndirect), d_logOp (ReturnCallIndirect) ), // 0x13 |
5279 | | |
5280 | | #if d_m3HasTypedRefs |
5281 | | M3OP( "call_ref", 0, any, d_cc(Compile_CallRef), d_logOp (CallRef) ), // 0x14 |
5282 | | M3OP( "return_call_ref", 0, any, d_cc(Compile_CallRef), d_logOp (ReturnCallRef) ), // 0x15 |
5283 | | #else |
5284 | | M3OP_RESERVED, M3OP_RESERVED, // 0x14... |
5285 | | #endif |
5286 | | M3OP_RESERVED, M3OP_RESERVED, M3OP_RESERVED, M3OP_RESERVED, // ...0x19 |
5287 | | |
5288 | | M3OP( "drop", -1, none, d_cc(Compile_Drop), d_emptyOpList ), // 0x1a |
5289 | | M3OP( "select", -2, any, d_cc(Compile_Select), d_emptyOpList ), // 0x1b |
5290 | | |
5291 | | #if d_m3HasRefTypes |
5292 | | M3OP( "select.t", -2, any, d_cc(Compile_Select_Typed), d_emptyOpList ), // 0x1c |
5293 | | #else |
5294 | | M3OP_RESERVED, // 0x1c |
5295 | | #endif |
5296 | | M3OP_RESERVED, M3OP_RESERVED, // 0x1d...0x1e |
5297 | | |
5298 | | #if d_m3HasExceptionHandling |
5299 | | M3OP( "try_table", 0, none, d_cc(Compile_TryTable), d_logOp (TryTable) ), // 0x1f |
5300 | | #else |
5301 | | M3OP_RESERVED, // 0x1f |
5302 | | #endif |
5303 | | |
5304 | | M3OP( "local.get", 1, any, d_cc(Compile_GetLocal), d_emptyOpList ), // 0x20 |
5305 | | M3OP( "local.set", 1, none, d_cc(Compile_SetLocal), d_emptyOpList ), // 0x21 |
5306 | | M3OP( "local.tee", 0, any, d_cc(Compile_SetLocal), d_emptyOpList ), // 0x22 |
5307 | | M3OP( "global.get", 1, none, d_cc(Compile_GetSetGlobal), d_emptyOpList ), // 0x23 |
5308 | | M3OP( "global.set", 1, none, d_cc(Compile_GetSetGlobal), d_emptyOpList ), // 0x24 |
5309 | | |
5310 | | #if d_m3HasRefTypes |
5311 | | M3OP( "table.get", 0, any, d_cc(Compile_Table_GetSet), d_emptyOpList ), // 0x25 |
5312 | | M3OP( "table.set", -2, none, d_cc(Compile_Table_GetSet), d_emptyOpList ), // 0x26 |
5313 | | #else |
5314 | | M3OP_RESERVED, M3OP_RESERVED, // 0x25...0x26 |
5315 | | #endif |
5316 | | M3OP_RESERVED, // 0x27 |
5317 | | |
5318 | | M3OP( "i32.load", 0, i_32, d_cc(Compile_Load_Store), d_unaryOpList (i32, Load_i32) ), // 0x28 |
5319 | | M3OP( "i64.load", 0, i_64, d_cc(Compile_Load_Store), d_unaryOpList (i64, Load_i64) ), // 0x29 |
5320 | | M3OP_F( "f32.load", 0, f_32, d_cc(Compile_Load_Store), d_unaryOpList (f32, Load_f32) ), // 0x2a |
5321 | | M3OP_F( "f64.load", 0, f_64, d_cc(Compile_Load_Store), d_unaryOpList (f64, Load_f64) ), // 0x2b |
5322 | | |
5323 | | M3OP( "i32.load8_s", 0, i_32, d_cc(Compile_Load_Store), d_unaryOpList (i32, Load_i8) ), // 0x2c |
5324 | | M3OP( "i32.load8_u", 0, i_32, d_cc(Compile_Load_Store), d_unaryOpList (i32, Load_u8) ), // 0x2d |
5325 | | M3OP( "i32.load16_s", 0, i_32, d_cc(Compile_Load_Store), d_unaryOpList (i32, Load_i16) ), // 0x2e |
5326 | | M3OP( "i32.load16_u", 0, i_32, d_cc(Compile_Load_Store), d_unaryOpList (i32, Load_u16) ), // 0x2f |
5327 | | |
5328 | | M3OP( "i64.load8_s", 0, i_64, d_cc(Compile_Load_Store), d_unaryOpList (i64, Load_i8) ), // 0x30 |
5329 | | M3OP( "i64.load8_u", 0, i_64, d_cc(Compile_Load_Store), d_unaryOpList (i64, Load_u8) ), // 0x31 |
5330 | | M3OP( "i64.load16_s", 0, i_64, d_cc(Compile_Load_Store), d_unaryOpList (i64, Load_i16) ), // 0x32 |
5331 | | M3OP( "i64.load16_u", 0, i_64, d_cc(Compile_Load_Store), d_unaryOpList (i64, Load_u16) ), // 0x33 |
5332 | | M3OP( "i64.load32_s", 0, i_64, d_cc(Compile_Load_Store), d_unaryOpList (i64, Load_i32) ), // 0x34 |
5333 | | M3OP( "i64.load32_u", 0, i_64, d_cc(Compile_Load_Store), d_unaryOpList (i64, Load_u32) ), // 0x35 |
5334 | | |
5335 | | M3OP( "i32.store", -2, none, d_cc(Compile_Load_Store), d_binOpList (i32, Store_i32) ), // 0x36 |
5336 | | M3OP( "i64.store", -2, none, d_cc(Compile_Load_Store), d_binOpList (i64, Store_i64) ), // 0x37 |
5337 | | M3OP_F( "f32.store", -2, none, d_cc(Compile_Load_Store), d_storeFpOpList (f32, Store_f32) ), // 0x38 |
5338 | | M3OP_F( "f64.store", -2, none, d_cc(Compile_Load_Store), d_storeFpOpList (f64, Store_f64) ), // 0x39 |
5339 | | |
5340 | | M3OP( "i32.store8", -2, none, d_cc(Compile_Load_Store), d_binOpList (i32, Store_u8) ), // 0x3a |
5341 | | M3OP( "i32.store16", -2, none, d_cc(Compile_Load_Store), d_binOpList (i32, Store_i16) ), // 0x3b |
5342 | | |
5343 | | M3OP( "i64.store8", -2, none, d_cc(Compile_Load_Store), d_binOpList (i64, Store_u8) ), // 0x3c |
5344 | | M3OP( "i64.store16", -2, none, d_cc(Compile_Load_Store), d_binOpList (i64, Store_i16) ), // 0x3d |
5345 | | M3OP( "i64.store32", -2, none, d_cc(Compile_Load_Store), d_binOpList (i64, Store_i32) ), // 0x3e |
5346 | | |
5347 | | M3OP( "memory.size", 1, i_32, d_cc(Compile_Memory_Size), d_logOp (MemSize) ), // 0x3f |
5348 | | M3OP( "memory.grow", 1, i_32, d_cc(Compile_Memory_Grow), d_logOp (MemGrow) ), // 0x40 |
5349 | | |
5350 | | M3OP( "i32.const", 1, i_32, d_cc(Compile_Const_i32), d_logOp (Const32) ), // 0x41 |
5351 | | M3OP( "i64.const", 1, i_64, d_cc(Compile_Const_i64), d_logOp (Const64) ), // 0x42 |
5352 | | M3OP_F( "f32.const", 1, f_32, d_cc(Compile_Const_f32), d_emptyOpList ), // 0x43 |
5353 | | M3OP_F( "f64.const", 1, f_64, d_cc(Compile_Const_f64), d_emptyOpList ), // 0x44 |
5354 | | |
5355 | | M3OP( "i32.eqz", 0, i_32, d_cc(NULL), d_unaryOpList (i32, EqualToZero) ), // 0x45 |
5356 | | M3OP( "i32.eq", -1, i_32, d_cc(NULL), d_commutativeBinOpList (i32, Equal) ), // 0x46 |
5357 | | M3OP( "i32.ne", -1, i_32, d_cc(NULL), d_commutativeBinOpList (i32, NotEqual) ), // 0x47 |
5358 | | M3OP( "i32.lt_s", -1, i_32, d_cc(NULL), d_binOpList (i32, LessThan) ), // 0x48 |
5359 | | M3OP( "i32.lt_u", -1, i_32, d_cc(NULL), d_binOpList (u32, LessThan) ), // 0x49 |
5360 | | M3OP( "i32.gt_s", -1, i_32, d_cc(NULL), d_binOpList (i32, GreaterThan) ), // 0x4a |
5361 | | M3OP( "i32.gt_u", -1, i_32, d_cc(NULL), d_binOpList (u32, GreaterThan) ), // 0x4b |
5362 | | M3OP( "i32.le_s", -1, i_32, d_cc(NULL), d_binOpList (i32, LessThanOrEqual) ), // 0x4c |
5363 | | M3OP( "i32.le_u", -1, i_32, d_cc(NULL), d_binOpList (u32, LessThanOrEqual) ), // 0x4d |
5364 | | M3OP( "i32.ge_s", -1, i_32, d_cc(NULL), d_binOpList (i32, GreaterThanOrEqual) ), // 0x4e |
5365 | | M3OP( "i32.ge_u", -1, i_32, d_cc(NULL), d_binOpList (u32, GreaterThanOrEqual) ), // 0x4f |
5366 | | |
5367 | | M3OP( "i64.eqz", 0, i_32, d_cc(NULL), d_unaryOpList (i64, EqualToZero) ), // 0x50 |
5368 | | M3OP( "i64.eq", -1, i_32, d_cc(NULL), d_commutativeBinOpList (i64, Equal) ), // 0x51 |
5369 | | M3OP( "i64.ne", -1, i_32, d_cc(NULL), d_commutativeBinOpList (i64, NotEqual) ), // 0x52 |
5370 | | M3OP( "i64.lt_s", -1, i_32, d_cc(NULL), d_binOpList (i64, LessThan) ), // 0x53 |
5371 | | M3OP( "i64.lt_u", -1, i_32, d_cc(NULL), d_binOpList (u64, LessThan) ), // 0x54 |
5372 | | M3OP( "i64.gt_s", -1, i_32, d_cc(NULL), d_binOpList (i64, GreaterThan) ), // 0x55 |
5373 | | M3OP( "i64.gt_u", -1, i_32, d_cc(NULL), d_binOpList (u64, GreaterThan) ), // 0x56 |
5374 | | M3OP( "i64.le_s", -1, i_32, d_cc(NULL), d_binOpList (i64, LessThanOrEqual) ), // 0x57 |
5375 | | M3OP( "i64.le_u", -1, i_32, d_cc(NULL), d_binOpList (u64, LessThanOrEqual) ), // 0x58 |
5376 | | M3OP( "i64.ge_s", -1, i_32, d_cc(NULL), d_binOpList (i64, GreaterThanOrEqual) ), // 0x59 |
5377 | | M3OP( "i64.ge_u", -1, i_32, d_cc(NULL), d_binOpList (u64, GreaterThanOrEqual) ), // 0x5a |
5378 | | |
5379 | | M3OP_F( "f32.eq", -1, i_32, d_cc(NULL), d_commutativeBinOpList (f32, Equal) ), // 0x5b |
5380 | | M3OP_F( "f32.ne", -1, i_32, d_cc(NULL), d_commutativeBinOpList (f32, NotEqual) ), // 0x5c |
5381 | | M3OP_F( "f32.lt", -1, i_32, d_cc(NULL), d_binOpList (f32, LessThan) ), // 0x5d |
5382 | | M3OP_F( "f32.gt", -1, i_32, d_cc(NULL), d_binOpList (f32, GreaterThan) ), // 0x5e |
5383 | | M3OP_F( "f32.le", -1, i_32, d_cc(NULL), d_binOpList (f32, LessThanOrEqual) ), // 0x5f |
5384 | | M3OP_F( "f32.ge", -1, i_32, d_cc(NULL), d_binOpList (f32, GreaterThanOrEqual) ), // 0x60 |
5385 | | |
5386 | | M3OP_F( "f64.eq", -1, i_32, d_cc(NULL), d_commutativeBinOpList (f64, Equal) ), // 0x61 |
5387 | | M3OP_F( "f64.ne", -1, i_32, d_cc(NULL), d_commutativeBinOpList (f64, NotEqual) ), // 0x62 |
5388 | | M3OP_F( "f64.lt", -1, i_32, d_cc(NULL), d_binOpList (f64, LessThan) ), // 0x63 |
5389 | | M3OP_F( "f64.gt", -1, i_32, d_cc(NULL), d_binOpList (f64, GreaterThan) ), // 0x64 |
5390 | | M3OP_F( "f64.le", -1, i_32, d_cc(NULL), d_binOpList (f64, LessThanOrEqual) ), // 0x65 |
5391 | | M3OP_F( "f64.ge", -1, i_32, d_cc(NULL), d_binOpList (f64, GreaterThanOrEqual) ), // 0x66 |
5392 | | |
5393 | | M3OP( "i32.clz", 0, i_32, d_cc(NULL), d_unaryOpList (u32, Clz) ), // 0x67 |
5394 | | M3OP( "i32.ctz", 0, i_32, d_cc(NULL), d_unaryOpList (u32, Ctz) ), // 0x68 |
5395 | | M3OP( "i32.popcnt", 0, i_32, d_cc(NULL), d_unaryOpList (u32, Popcnt) ), // 0x69 |
5396 | | |
5397 | | M3OP( "i32.add", -1, i_32, d_cc(NULL), d_commutativeBinOpList (i32, Add) ), // 0x6a |
5398 | | M3OP( "i32.sub", -1, i_32, d_cc(NULL), d_binOpList (i32, Subtract) ), // 0x6b |
5399 | | M3OP( "i32.mul", -1, i_32, d_cc(NULL), d_commutativeBinOpList (i32, Multiply) ), // 0x6c |
5400 | | M3OP( "i32.div_s", -1, i_32, d_cc(NULL), d_binOpList (i32, Divide) ), // 0x6d |
5401 | | M3OP( "i32.div_u", -1, i_32, d_cc(NULL), d_binOpList (u32, Divide) ), // 0x6e |
5402 | | M3OP( "i32.rem_s", -1, i_32, d_cc(NULL), d_binOpList (i32, Remainder) ), // 0x6f |
5403 | | M3OP( "i32.rem_u", -1, i_32, d_cc(NULL), d_binOpList (u32, Remainder) ), // 0x70 |
5404 | | M3OP( "i32.and", -1, i_32, d_cc(NULL), d_commutativeBinOpList (u32, And) ), // 0x71 |
5405 | | M3OP( "i32.or", -1, i_32, d_cc(NULL), d_commutativeBinOpList (u32, Or) ), // 0x72 |
5406 | | M3OP( "i32.xor", -1, i_32, d_cc(NULL), d_commutativeBinOpList (u32, Xor) ), // 0x73 |
5407 | | M3OP( "i32.shl", -1, i_32, d_cc(NULL), d_binOpList (u32, ShiftLeft) ), // 0x74 |
5408 | | M3OP( "i32.shr_s", -1, i_32, d_cc(NULL), d_binOpList (i32, ShiftRight) ), // 0x75 |
5409 | | M3OP( "i32.shr_u", -1, i_32, d_cc(NULL), d_binOpList (u32, ShiftRight) ), // 0x76 |
5410 | | M3OP( "i32.rotl", -1, i_32, d_cc(NULL), d_binOpList (u32, Rotl) ), // 0x77 |
5411 | | M3OP( "i32.rotr", -1, i_32, d_cc(NULL), d_binOpList (u32, Rotr) ), // 0x78 |
5412 | | |
5413 | | M3OP( "i64.clz", 0, i_64, d_cc(NULL), d_unaryOpList (u64, Clz) ), // 0x79 |
5414 | | M3OP( "i64.ctz", 0, i_64, d_cc(NULL), d_unaryOpList (u64, Ctz) ), // 0x7a |
5415 | | M3OP( "i64.popcnt", 0, i_64, d_cc(NULL), d_unaryOpList (u64, Popcnt) ), // 0x7b |
5416 | | |
5417 | | M3OP( "i64.add", -1, i_64, d_cc(NULL), d_commutativeBinOpList (i64, Add) ), // 0x7c |
5418 | | M3OP( "i64.sub", -1, i_64, d_cc(NULL), d_binOpList (i64, Subtract) ), // 0x7d |
5419 | | M3OP( "i64.mul", -1, i_64, d_cc(NULL), d_commutativeBinOpList (i64, Multiply) ), // 0x7e |
5420 | | M3OP( "i64.div_s", -1, i_64, d_cc(NULL), d_binOpList (i64, Divide) ), // 0x7f |
5421 | | M3OP( "i64.div_u", -1, i_64, d_cc(NULL), d_binOpList (u64, Divide) ), // 0x80 |
5422 | | M3OP( "i64.rem_s", -1, i_64, d_cc(NULL), d_binOpList (i64, Remainder) ), // 0x81 |
5423 | | M3OP( "i64.rem_u", -1, i_64, d_cc(NULL), d_binOpList (u64, Remainder) ), // 0x82 |
5424 | | M3OP( "i64.and", -1, i_64, d_cc(NULL), d_commutativeBinOpList (u64, And) ), // 0x83 |
5425 | | M3OP( "i64.or", -1, i_64, d_cc(NULL), d_commutativeBinOpList (u64, Or) ), // 0x84 |
5426 | | M3OP( "i64.xor", -1, i_64, d_cc(NULL), d_commutativeBinOpList (u64, Xor) ), // 0x85 |
5427 | | M3OP( "i64.shl", -1, i_64, d_cc(NULL), d_binOpList (u64, ShiftLeft) ), // 0x86 |
5428 | | M3OP( "i64.shr_s", -1, i_64, d_cc(NULL), d_binOpList (i64, ShiftRight) ), // 0x87 |
5429 | | M3OP( "i64.shr_u", -1, i_64, d_cc(NULL), d_binOpList (u64, ShiftRight) ), // 0x88 |
5430 | | M3OP( "i64.rotl", -1, i_64, d_cc(NULL), d_binOpList (u64, Rotl) ), // 0x89 |
5431 | | M3OP( "i64.rotr", -1, i_64, d_cc(NULL), d_binOpList (u64, Rotr) ), // 0x8a |
5432 | | |
5433 | | M3OP_F( "f32.abs", 0, f_32, d_cc(NULL), d_unaryOpList(f32, Abs) ), // 0x8b |
5434 | | M3OP_F( "f32.neg", 0, f_32, d_cc(NULL), d_unaryOpList(f32, Negate) ), // 0x8c |
5435 | | M3OP_F( "f32.ceil", 0, f_32, d_cc(NULL), d_unaryOpList(f32, Ceil) ), // 0x8d |
5436 | | M3OP_F( "f32.floor", 0, f_32, d_cc(NULL), d_unaryOpList(f32, Floor) ), // 0x8e |
5437 | | M3OP_F( "f32.trunc", 0, f_32, d_cc(NULL), d_unaryOpList(f32, Trunc) ), // 0x8f |
5438 | | M3OP_F( "f32.nearest", 0, f_32, d_cc(NULL), d_unaryOpList(f32, Nearest) ), // 0x90 |
5439 | | M3OP_F( "f32.sqrt", 0, f_32, d_cc(NULL), d_unaryOpList(f32, Sqrt) ), // 0x91 |
5440 | | |
5441 | | M3OP_F( "f32.add", -1, f_32, d_cc(NULL), d_commutativeBinOpList (f32, Add) ), // 0x92 |
5442 | | M3OP_F( "f32.sub", -1, f_32, d_cc(NULL), d_binOpList (f32, Subtract) ), // 0x93 |
5443 | | M3OP_F( "f32.mul", -1, f_32, d_cc(NULL), d_commutativeBinOpList (f32, Multiply) ), // 0x94 |
5444 | | M3OP_F( "f32.div", -1, f_32, d_cc(NULL), d_binOpList (f32, Divide) ), // 0x95 |
5445 | | M3OP_F( "f32.min", -1, f_32, d_cc(NULL), d_commutativeBinOpList (f32, Min) ), // 0x96 |
5446 | | M3OP_F( "f32.max", -1, f_32, d_cc(NULL), d_commutativeBinOpList (f32, Max) ), // 0x97 |
5447 | | M3OP_F( "f32.copysign", -1, f_32, d_cc(NULL), d_binOpList (f32, CopySign) ), // 0x98 |
5448 | | |
5449 | | M3OP_F( "f64.abs", 0, f_64, d_cc(NULL), d_unaryOpList(f64, Abs) ), // 0x99 |
5450 | | M3OP_F( "f64.neg", 0, f_64, d_cc(NULL), d_unaryOpList(f64, Negate) ), // 0x9a |
5451 | | M3OP_F( "f64.ceil", 0, f_64, d_cc(NULL), d_unaryOpList(f64, Ceil) ), // 0x9b |
5452 | | M3OP_F( "f64.floor", 0, f_64, d_cc(NULL), d_unaryOpList(f64, Floor) ), // 0x9c |
5453 | | M3OP_F( "f64.trunc", 0, f_64, d_cc(NULL), d_unaryOpList(f64, Trunc) ), // 0x9d |
5454 | | M3OP_F( "f64.nearest", 0, f_64, d_cc(NULL), d_unaryOpList(f64, Nearest) ), // 0x9e |
5455 | | M3OP_F( "f64.sqrt", 0, f_64, d_cc(NULL), d_unaryOpList(f64, Sqrt) ), // 0x9f |
5456 | | |
5457 | | M3OP_F( "f64.add", -1, f_64, d_cc(NULL), d_commutativeBinOpList (f64, Add) ), // 0xa0 |
5458 | | M3OP_F( "f64.sub", -1, f_64, d_cc(NULL), d_binOpList (f64, Subtract) ), // 0xa1 |
5459 | | M3OP_F( "f64.mul", -1, f_64, d_cc(NULL), d_commutativeBinOpList (f64, Multiply) ), // 0xa2 |
5460 | | M3OP_F( "f64.div", -1, f_64, d_cc(NULL), d_binOpList (f64, Divide) ), // 0xa3 |
5461 | | M3OP_F( "f64.min", -1, f_64, d_cc(NULL), d_commutativeBinOpList (f64, Min) ), // 0xa4 |
5462 | | M3OP_F( "f64.max", -1, f_64, d_cc(NULL), d_commutativeBinOpList (f64, Max) ), // 0xa5 |
5463 | | M3OP_F( "f64.copysign", -1, f_64, d_cc(NULL), d_binOpList (f64, CopySign) ), // 0xa6 |
5464 | | |
5465 | | M3OP( "i32.wrap/i64", 0, i_32, d_cc(NULL), d_unaryOpList (i32, Wrap_i64) ), // 0xa7 |
5466 | | M3OP_F( "i32.trunc_s/f32", 0, i_32, d_cc(Compile_Convert), d_convertOpList (i32_Trunc_f32) ), // 0xa8 |
5467 | | M3OP_F( "i32.trunc_u/f32", 0, i_32, d_cc(Compile_Convert), d_convertOpList (u32_Trunc_f32) ), // 0xa9 |
5468 | | M3OP_F( "i32.trunc_s/f64", 0, i_32, d_cc(Compile_Convert), d_convertOpList (i32_Trunc_f64) ), // 0xaa |
5469 | | M3OP_F( "i32.trunc_u/f64", 0, i_32, d_cc(Compile_Convert), d_convertOpList (u32_Trunc_f64) ), // 0xab |
5470 | | |
5471 | | M3OP( "i64.extend_s/i32", 0, i_64, d_cc(NULL), d_unaryOpList (i64, Extend_i32) ), // 0xac |
5472 | | M3OP( "i64.extend_u/i32", 0, i_64, d_cc(NULL), d_unaryOpList (i64, Extend_u32) ), // 0xad |
5473 | | |
5474 | | M3OP_F( "i64.trunc_s/f32", 0, i_64, d_cc(Compile_Convert), d_convertOpList (i64_Trunc_f32) ), // 0xae |
5475 | | M3OP_F( "i64.trunc_u/f32", 0, i_64, d_cc(Compile_Convert), d_convertOpList (u64_Trunc_f32) ), // 0xaf |
5476 | | M3OP_F( "i64.trunc_s/f64", 0, i_64, d_cc(Compile_Convert), d_convertOpList (i64_Trunc_f64) ), // 0xb0 |
5477 | | M3OP_F( "i64.trunc_u/f64", 0, i_64, d_cc(Compile_Convert), d_convertOpList (u64_Trunc_f64) ), // 0xb1 |
5478 | | |
5479 | | M3OP_F( "f32.convert_s/i32", 0, f_32, d_cc(Compile_Convert), d_convertOpList (f32_Convert_i32) ), // 0xb2 |
5480 | | M3OP_F( "f32.convert_u/i32", 0, f_32, d_cc(Compile_Convert), d_convertOpList (f32_Convert_u32) ), // 0xb3 |
5481 | | M3OP_F( "f32.convert_s/i64", 0, f_32, d_cc(Compile_Convert), d_convertOpList (f32_Convert_i64) ), // 0xb4 |
5482 | | M3OP_F( "f32.convert_u/i64", 0, f_32, d_cc(Compile_Convert), d_convertOpList (f32_Convert_u64) ), // 0xb5 |
5483 | | |
5484 | | M3OP_F( "f32.demote/f64", 0, f_32, d_cc(NULL), d_unaryOpList (f32, Demote_f64) ), // 0xb6 |
5485 | | |
5486 | | M3OP_F( "f64.convert_s/i32", 0, f_64, d_cc(Compile_Convert), d_convertOpList (f64_Convert_i32) ), // 0xb7 |
5487 | | M3OP_F( "f64.convert_u/i32", 0, f_64, d_cc(Compile_Convert), d_convertOpList (f64_Convert_u32) ), // 0xb8 |
5488 | | M3OP_F( "f64.convert_s/i64", 0, f_64, d_cc(Compile_Convert), d_convertOpList (f64_Convert_i64) ), // 0xb9 |
5489 | | M3OP_F( "f64.convert_u/i64", 0, f_64, d_cc(Compile_Convert), d_convertOpList (f64_Convert_u64) ), // 0xba |
5490 | | |
5491 | | M3OP_F( "f64.promote/f32", 0, f_64, d_cc(NULL), d_unaryOpList (f64, Promote_f32) ), // 0xbb |
5492 | | |
5493 | | M3OP_F( "i32.reinterpret/f32", 0, i_32, d_cc(Compile_Convert), d_convertOpList (i32_Reinterpret_f32) ), // 0xbc |
5494 | | M3OP_F( "i64.reinterpret/f64", 0, i_64, d_cc(Compile_Convert), d_convertOpList (i64_Reinterpret_f64) ), // 0xbd |
5495 | | M3OP_F( "f32.reinterpret/i32", 0, f_32, d_cc(Compile_Convert), d_convertOpList (f32_Reinterpret_i32) ), // 0xbe |
5496 | | M3OP_F( "f64.reinterpret/i64", 0, f_64, d_cc(Compile_Convert), d_convertOpList (f64_Reinterpret_i64) ), // 0xbf |
5497 | | |
5498 | | M3OP( "i32.extend8_s", 0, i_32, d_cc(NULL), d_unaryOpList (i32, Extend8_s) ), // 0xc0 |
5499 | | M3OP( "i32.extend16_s", 0, i_32, d_cc(NULL), d_unaryOpList (i32, Extend16_s) ), // 0xc1 |
5500 | | M3OP( "i64.extend8_s", 0, i_64, d_cc(NULL), d_unaryOpList (i64, Extend8_s) ), // 0xc2 |
5501 | | M3OP( "i64.extend16_s", 0, i_64, d_cc(NULL), d_unaryOpList (i64, Extend16_s) ), // 0xc3 |
5502 | | M3OP( "i64.extend32_s", 0, i_64, d_cc(NULL), d_unaryOpList (i64, Extend32_s) ), // 0xc4 |
5503 | | |
5504 | | // Internal operations, for codepage logging only. They sit past c_waOp_lastCore, |
5505 | | // the last opcode this table covers, so GetOpInfo () can never reach them. |
5506 | | # ifdef DEBUG // for codepage logging. the order doesn't matter: |
5507 | | #define d_m3DebugOp(OP) M3OP (#OP, 0, none, d_ops (op_##OP)) |
5508 | | |
5509 | | # if d_m3HasFloat |
5510 | | #define d_m3DebugTypedOp(OP) M3OP (#OP, 0, none, d_ops (op_##OP##_i32, op_##OP##_i64, op_##OP##_f32, op_##OP##_f64)) |
5511 | | # else |
5512 | | #define d_m3DebugTypedOp(OP) M3OP (#OP, 0, none, d_ops (op_##OP##_i32, op_##OP##_i64)) |
5513 | | # endif |
5514 | | |
5515 | | d_m3DebugOp (Compile), d_m3DebugOp (Entry), d_m3DebugOp (End), |
5516 | | d_m3DebugOp (Unsupported), d_m3DebugOp (CallRawFunction), |
5517 | | |
5518 | | d_m3DebugOp (GetGlobal_s32), d_m3DebugOp (GetGlobal_s64), d_m3DebugOp (ContinueLoop), d_m3DebugOp (ContinueLoopIf), |
5519 | | # if d_m3HasStackSwitching |
5520 | | d_m3DebugOp (ContinueLoop_Suspendable), d_m3DebugOp (ContinueLoopIf_Suspendable), d_m3DebugOp (EntryCheck), |
5521 | | # if !d_m3EntryKeepsFrame |
5522 | | d_m3DebugOp (Entry_Suspendable), |
5523 | | # endif |
5524 | | # endif |
5525 | | |
5526 | | d_m3DebugOp (CopySlot_32), d_m3DebugOp (PreserveCopySlot_32), d_m3DebugOp (If_s), d_m3DebugOp (BranchIfPrologue_s), |
5527 | | d_m3DebugOp (CopySlot_64), d_m3DebugOp (PreserveCopySlot_64), d_m3DebugOp (If_r), d_m3DebugOp (BranchIfPrologue_r), |
5528 | | |
5529 | | d_m3DebugOp (Select_i32_rss), d_m3DebugOp (Select_i32_srs), d_m3DebugOp (Select_i32_ssr), d_m3DebugOp (Select_i32_sss), |
5530 | | d_m3DebugOp (Select_i64_rss), d_m3DebugOp (Select_i64_srs), d_m3DebugOp (Select_i64_ssr), d_m3DebugOp (Select_i64_sss), |
5531 | | |
5532 | | # if d_m3HasFloat |
5533 | | d_m3DebugOp (Select_f32_sss), d_m3DebugOp (Select_f32_srs), d_m3DebugOp (Select_f32_ssr), |
5534 | | d_m3DebugOp (Select_f32_rss), d_m3DebugOp (Select_f32_rrs), d_m3DebugOp (Select_f32_rsr), |
5535 | | |
5536 | | d_m3DebugOp (Select_f64_sss), d_m3DebugOp (Select_f64_srs), d_m3DebugOp (Select_f64_ssr), |
5537 | | d_m3DebugOp (Select_f64_rss), d_m3DebugOp (Select_f64_rrs), d_m3DebugOp (Select_f64_rsr), |
5538 | | # endif |
5539 | | |
5540 | | d_m3DebugOp (MemFill), d_m3DebugOp (MemCopy), d_m3DebugOp (MemInit), d_m3DebugOp (DataDrop), |
5541 | | |
5542 | | # if d_m3HasGasMetering |
5543 | | d_m3DebugOp (UseGas), |
5544 | | # endif |
5545 | | |
5546 | | # if d_m3HasRefTypes |
5547 | | d_m3DebugOp (TableGet), d_m3DebugOp (TableSet), d_m3DebugOp (TableSize), |
5548 | | d_m3DebugOp (TableGrow), d_m3DebugOp (TableFill), d_m3DebugOp (TableInit), |
5549 | | d_m3DebugOp (ElemDrop), d_m3DebugOp (TableCopy), |
5550 | | # endif |
5551 | | |
5552 | | d_m3DebugTypedOp (SetGlobal), d_m3DebugOp (SetGlobal_s32), d_m3DebugOp (SetGlobal_s64), |
5553 | | |
5554 | | d_m3DebugTypedOp (SetRegister), d_m3DebugTypedOp (SetSlot), d_m3DebugTypedOp (PreserveSetSlot), |
5555 | | # endif |
5556 | | |
5557 | | # ifdef DEBUG |
5558 | | M3OP( "termination", 0, c_m3Type_unknown, d_emptyOpList ) // for find_operation_info |
5559 | | # endif |
5560 | | }; |
5561 | | |
5562 | | const M3OpInfo c_operationsFC[] = |
5563 | | { |
5564 | | M3OP_F( "i32.trunc_s:sat/f32", 0, i_32, d_cc(Compile_Convert), d_convertOpList (i32_TruncSat_f32) ), // 0x00 |
5565 | | M3OP_F( "i32.trunc_u:sat/f32", 0, i_32, d_cc(Compile_Convert), d_convertOpList (u32_TruncSat_f32) ), // 0x01 |
5566 | | M3OP_F( "i32.trunc_s:sat/f64", 0, i_32, d_cc(Compile_Convert), d_convertOpList (i32_TruncSat_f64) ), // 0x02 |
5567 | | M3OP_F( "i32.trunc_u:sat/f64", 0, i_32, d_cc(Compile_Convert), d_convertOpList (u32_TruncSat_f64) ), // 0x03 |
5568 | | M3OP_F( "i64.trunc_s:sat/f32", 0, i_64, d_cc(Compile_Convert), d_convertOpList (i64_TruncSat_f32) ), // 0x04 |
5569 | | M3OP_F( "i64.trunc_u:sat/f32", 0, i_64, d_cc(Compile_Convert), d_convertOpList (u64_TruncSat_f32) ), // 0x05 |
5570 | | M3OP_F( "i64.trunc_s:sat/f64", 0, i_64, d_cc(Compile_Convert), d_convertOpList (i64_TruncSat_f64) ), // 0x06 |
5571 | | M3OP_F( "i64.trunc_u:sat/f64", 0, i_64, d_cc(Compile_Convert), d_convertOpList (u64_TruncSat_f64) ), // 0x07 |
5572 | | |
5573 | | M3OP( "memory.init", 0, none, d_cc(Compile_Memory_Init), d_emptyOpList ), // 0x08 |
5574 | | M3OP( "data.drop", 0, none, d_cc(Compile_Data_Drop), d_emptyOpList ), // 0x09 |
5575 | | |
5576 | | M3OP( "memory.copy", 0, none, d_cc(Compile_Memory_CopyFill), d_emptyOpList ), // 0x0a |
5577 | | M3OP( "memory.fill", 0, none, d_cc(Compile_Memory_CopyFill), d_emptyOpList ), // 0x0b |
5578 | | |
5579 | | #if d_m3HasRefTypes |
5580 | | M3OP( "table.init", 0, none, d_cc(Compile_Table_Init), d_emptyOpList ), // 0x0c |
5581 | | M3OP( "elem.drop", 0, none, d_cc(Compile_Elem_Drop), d_emptyOpList ), // 0x0d |
5582 | | M3OP( "table.copy", 0, none, d_cc(Compile_Table_Copy), d_emptyOpList ), // 0x0e |
5583 | | #else |
5584 | | M3OP_RESERVED, M3OP_RESERVED, M3OP_RESERVED, // 0x0c...0x0e |
5585 | | #endif |
5586 | | |
5587 | | #if d_m3HasRefTypes |
5588 | | M3OP( "table.grow", 0, i_32, d_cc(Compile_Table_GrowFill), d_emptyOpList ), // 0x0f |
5589 | | M3OP( "table.size", 1, i_32, d_cc(Compile_Table_Size), d_emptyOpList ), // 0x10 |
5590 | | M3OP( "table.fill", 0, none, d_cc(Compile_Table_GrowFill), d_emptyOpList ), // 0x11 |
5591 | | #else |
5592 | | M3OP_RESERVED, M3OP_RESERVED, M3OP_RESERVED, // 0x0f...0x11 |
5593 | | #endif |
5594 | | |
5595 | | #if d_m3HasWideArithmetic |
5596 | | M3OP_RESERVED, // 0x12 |
5597 | | |
5598 | | M3OP( "i64.add128", -2, i_64, d_cc(Compile_WideArithmetic), d_logOp (i64_Add128) ), // 0x13 |
5599 | | M3OP( "i64.sub128", -2, i_64, d_cc(Compile_WideArithmetic), d_logOp (i64_Sub128) ), // 0x14 |
5600 | | M3OP( "i64.mul_wide_s", 0, i_64, d_cc(Compile_WideArithmetic), d_logOp (i64_MultiplyWide) ), // 0x15 |
5601 | | M3OP( "i64.mul_wide_u", 0, i_64, d_cc(Compile_WideArithmetic), d_logOp (u64_MultiplyWide) ), // 0x16 |
5602 | | #endif |
5603 | | |
5604 | | |
5605 | | # ifdef DEBUG |
5606 | | M3OP( "termination", 0, c_m3Type_unknown, d_emptyOpList ) // for find_operation_info |
5607 | | # endif |
5608 | | }; |
5609 | | |
5610 | | #if d_m3HasAtomics |
5611 | | |
5612 | | // The 0xFE instructions share one entry per kind rather than each having its own: what |
5613 | | // tells them apart, the width of the cell and the operation, is worked out from the |
5614 | | // sub-opcode by Compile_Atomic. GetAtomicInfoIndex () maps a sub-opcode onto this table. |
5615 | | enum { |
5616 | | c_opAtomic_notify, |
5617 | | c_opAtomic_wait32, |
5618 | | c_opAtomic_wait64, |
5619 | | c_opAtomic_fence, |
5620 | | c_opAtomic_load, |
5621 | | c_opAtomic_store, |
5622 | | c_opAtomic_rmw, |
5623 | | c_opAtomic_cmpxchg |
5624 | | }; |
5625 | | |
5626 | | const M3OpInfo c_operationsFE[] = |
5627 | | { |
5628 | | M3OP( "memory.atomic.notify", -1, i_32, d_cc(Compile_Atomic), d_logOp (AtomicNotify) ), // c_opAtomic_notify |
5629 | | M3OP( "memory.atomic.wait32", -2, i_32, d_cc(Compile_Atomic), d_logOp (AtomicWait32) ), // c_opAtomic_wait32 |
5630 | | M3OP( "memory.atomic.wait64", -2, i_32, d_cc(Compile_Atomic), d_logOp (AtomicWait64) ), // c_opAtomic_wait64 |
5631 | | M3OP( "atomic.fence", 0, none, d_cc(Compile_Atomic), d_emptyOpList ), // c_opAtomic_fence |
5632 | | M3OP( "atomic.load", 0, any, d_cc(Compile_Atomic), d_logOp (AtomicLoad) ), // c_opAtomic_load |
5633 | | M3OP( "atomic.store", -2, none, d_cc(Compile_Atomic), d_logOp (AtomicStore) ), // c_opAtomic_store |
5634 | | M3OP( "atomic.rmw", -1, any, d_cc(Compile_Atomic), d_logOp (AtomicRmw) ), // c_opAtomic_rmw |
5635 | | M3OP( "atomic.rmw.cmpxchg", -2, any, d_cc(Compile_Atomic), d_logOp (AtomicCmpxchg) ), // c_opAtomic_cmpxchg |
5636 | | }; |
5637 | | |
5638 | | const u32 c_numOperationsFE = M3_COUNT_OF(c_operationsFE); |
5639 | | |
5640 | | // The entry a 0xFE sub-opcode uses, or -1 for one that names nothing: the spec leaves |
5641 | | // 0x04..0x0f and everything past cmpxchg unassigned. The accesses come in runs of seven, |
5642 | | // one per pairing of value type and cell width. |
5643 | | static inline |
5644 | | i32 GetAtomicInfoIndex (u32 i_sub) |
5645 | 2.15k | { |
5646 | 2.15k | if (i_sub <= 0x03) return (i32)i_sub; |
5647 | 1.71k | if (i_sub < 0x10) return -1; |
5648 | 1.71k | if (i_sub <= 0x16) return c_opAtomic_load; |
5649 | 1.42k | if (i_sub <= 0x1d) return c_opAtomic_store; |
5650 | 1.18k | if (i_sub <= 0x47) return c_opAtomic_rmw; |
5651 | 314 | if (i_sub <= c_waOp_lastAtomic) return c_opAtomic_cmpxchg; |
5652 | 0 | return -1; |
5653 | 314 | } |
5654 | | |
5655 | | #endif // d_m3HasAtomics |
5656 | | |
5657 | | // The opcodes above c_waOp_lastCore are sparse: the reference instructions at |
5658 | | // 0xd0..0xd2 and 0xd4, and the 0xfc extended-opcode prefix. Indexing c_operations out to |
5659 | | // 0xfc to reach them would leave ~50 empty entries there, so they get their own |
5660 | | // table, which GetHighOpIndex () maps the opcode onto. |
5661 | | enum { |
5662 | | #if d_m3HasRefTypes |
5663 | | c_opHigh_refNull, |
5664 | | c_opHigh_refIsNull, |
5665 | | c_opHigh_refFunc, |
5666 | | # if d_m3HasTypedRefs |
5667 | | c_opHigh_refAsNonNull, |
5668 | | # endif |
5669 | | #endif |
5670 | | #if d_m3HasStackSwitching |
5671 | | c_opHigh_contNew, |
5672 | | c_opHigh_contBind, |
5673 | | c_opHigh_suspend, |
5674 | | c_opHigh_resume, |
5675 | | c_opHigh_resumeThrow, |
5676 | | c_opHigh_resumeThrowRef, |
5677 | | c_opHigh_switch, |
5678 | | #endif |
5679 | | #if d_m3HasAtomics |
5680 | | c_opHigh_atomic, |
5681 | | #endif |
5682 | | c_opHigh_extended // always present, so the enum is never empty |
5683 | | }; |
5684 | | |
5685 | | // Positional, so the table must stay in c_opHigh_* order: array designators |
5686 | | // are not valid C++, which the maintenance builds compile this as. |
5687 | | static const M3OpInfo c_operationsHigh[] = |
5688 | | { |
5689 | | #if d_m3HasRefTypes |
5690 | | M3OP( "ref.null", 1, any, d_cc(Compile_Ref_Null), d_emptyOpList ), // 0xd0 c_opHigh_refNull |
5691 | | M3OP( "ref.is_null", 0, i_32, d_cc(Compile_Ref_IsNull), d_emptyOpList ), // 0xd1 c_opHigh_refIsNull |
5692 | | M3OP( "ref.func", 1, any, d_cc(Compile_Ref_Func), d_emptyOpList ), // 0xd2 c_opHigh_refFunc |
5693 | | # if d_m3HasTypedRefs |
5694 | | M3OP( "ref.as_non_null", 0, any, d_cc(Compile_Ref_AsNonNull), d_emptyOpList ), // 0xd4 c_opHigh_refAsNonNull |
5695 | | # endif |
5696 | | #endif |
5697 | | #if d_m3HasStackSwitching |
5698 | | M3OP( "cont.new", 1, any, d_cc(Compile_ContNew), d_emptyOpList ), // 0xe0 c_opHigh_contNew |
5699 | | M3OP( "cont.bind", 2, any, d_cc(Compile_ContBind), d_emptyOpList ), // 0xe1 c_opHigh_contBind |
5700 | | M3OP( "suspend", 1, any, d_cc(Compile_Suspend), d_emptyOpList ), // 0xe2 c_opHigh_suspend |
5701 | | M3OP( "resume", 1, any, d_cc(Compile_Resume), d_emptyOpList ), // 0xe3 c_opHigh_resume |
5702 | | M3OP( "resume_throw", 2, any, d_cc(Compile_Resume) , d_emptyOpList ), // 0xe4 c_opHigh_resumeThrow |
5703 | | M3OP( "resume_throw_ref", 1, any, d_cc(Compile_Resume) , d_emptyOpList ), // 0xe5 c_opHigh_resumeThrowRef |
5704 | | M3OP( "switch", 2, any, d_cc(Compile_Switch), d_emptyOpList ), // 0xe6 c_opHigh_switch |
5705 | | #endif |
5706 | | |
5707 | | #if d_m3HasAtomics |
5708 | | M3OP( "0xFE", 0, c_m3Type_unknown, d_cc(Compile_ExtendedOpcode), d_emptyOpList ), // 0xfe c_opHigh_atomic |
5709 | | #endif |
5710 | | M3OP( "0xFC", 0, c_m3Type_unknown, d_cc(Compile_ExtendedOpcode), d_emptyOpList ), // 0xfc c_opHigh_extended |
5711 | | }; |
5712 | | M3_STATIC_ASSERT (M3_COUNT_OF (c_operationsHigh) == c_opHigh_extended + 1, c_operationsHigh_needs_one_entry_per_c_opHigh); |
5713 | | |
5714 | | // clang-format on |
5715 | | |
5716 | | |
5717 | | // Opcodes the spec reserves leave zeroed holes in the tables above: no compiler |
5718 | | // and no operations. Every implemented op has at least one of the two. |
5719 | | static inline |
5720 | | bool IsImplementedOp (IM3OpInfo i_info) |
5721 | 714k | { |
5722 | 714k | return (i_info->compiler != c_cc_NULL or i_info->operations[0] != NULL); |
5723 | 714k | } |
5724 | | |
5725 | | const u32 c_numOperations = M3_COUNT_OF(c_operations); |
5726 | | const u32 c_numOperationsFC = M3_COUNT_OF(c_operationsFC); |
5727 | | |
5728 | | // Maps one of the sparse opcodes above c_waOp_lastCore onto c_operationsHigh, |
5729 | | // or -1 for anything else in that range. |
5730 | | static inline |
5731 | | i32 GetHighOpIndex (m3opcode_t opcode) |
5732 | 15.1k | { |
5733 | 15.1k | switch (opcode) { |
5734 | 0 | #if d_m3HasRefTypes |
5735 | 3.98k | case c_waOp_refNull: return c_opHigh_refNull; |
5736 | 312 | case c_waOp_refIsNull: return c_opHigh_refIsNull; |
5737 | 695 | case c_waOp_refFunc: return c_opHigh_refFunc; |
5738 | 0 | # if d_m3HasTypedRefs |
5739 | 415 | case c_waOp_refAsNonNull: return c_opHigh_refAsNonNull; |
5740 | 0 | # endif |
5741 | 0 | #endif |
5742 | 0 | #if d_m3HasStackSwitching |
5743 | 0 | case c_waOp_contNew: return c_opHigh_contNew; |
5744 | 0 | case c_waOp_contBind: return c_opHigh_contBind; |
5745 | 186 | case c_waOp_suspend: return c_opHigh_suspend; |
5746 | 0 | case c_waOp_resume: return c_opHigh_resume; |
5747 | 0 | case c_waOp_resumeThrow: return c_opHigh_resumeThrow; |
5748 | 0 | case c_waOp_resumeThrowRef: return c_opHigh_resumeThrowRef; |
5749 | 0 | case c_waOp_switch: return c_opHigh_switch; |
5750 | 0 | #endif |
5751 | 7.98k | case c_waOp_extended: return c_opHigh_extended; |
5752 | 0 | #if d_m3HasAtomics |
5753 | 1.08k | case c_waOp_atomic: return c_opHigh_atomic; |
5754 | 0 | #endif |
5755 | 526 | default: return -1; |
5756 | 15.1k | } |
5757 | 15.1k | } |
5758 | | |
5759 | | IM3OpInfo GetOpInfo (m3opcode_t opcode) |
5760 | 715k | { |
5761 | 715k | IM3OpInfo info = NULL; |
5762 | | |
5763 | 715k | switch (opcode >> 8) { |
5764 | 699k | case 0x00: |
5765 | | // c_operations is indexed by opcode only up to c_waOp_lastCore; past that |
5766 | | // it holds internal operations (DEBUG builds), unreachable by opcode. |
5767 | 699k | if (M3_LIKELY(opcode <= c_waOp_lastCore)) { |
5768 | 684k | info = &c_operations[opcode]; |
5769 | 684k | } else { |
5770 | 15.1k | i32 high = GetHighOpIndex(opcode); |
5771 | 15.1k | if (high >= 0) { |
5772 | 14.6k | info = &c_operationsHigh[high]; |
5773 | 14.6k | } |
5774 | 15.1k | } |
5775 | 699k | break; |
5776 | 13.0k | case c_waOp_extended: |
5777 | 13.0k | opcode &= 0xFF; |
5778 | 13.0k | if (M3_LIKELY(opcode <= c_waOp_lastExtended)) { |
5779 | 13.0k | info = &c_operationsFC[opcode]; |
5780 | 13.0k | } |
5781 | 13.0k | break; |
5782 | 0 | #if d_m3HasAtomics |
5783 | 2.15k | case c_waOp_atomic: { |
5784 | 2.15k | i32 index = GetAtomicInfoIndex(opcode & 0xFF); |
5785 | 2.15k | if (index >= 0) { |
5786 | 2.15k | info = &c_operationsFE[index]; |
5787 | 2.15k | } |
5788 | 2.15k | } break; |
5789 | 715k | #endif |
5790 | 715k | } |
5791 | | |
5792 | 715k | return (info and IsImplementedOp(info)) ? info : NULL; |
5793 | 715k | } |
5794 | | |
5795 | | #if d_m3HasGasMetering |
5796 | | |
5797 | | //----- GAS METERING ------------------------------------------------------------------------------------------------------ |
5798 | | // |
5799 | | // Instrumentation the engine applies to a function body as it compiles it, |
5800 | | // following ewasm's metering design, which does the same thing to the Wasm ahead |
5801 | | // of time: https://github.com/ewasm/design/blob/master/metering.md |
5802 | | // |
5803 | | // A suspendable runtime that is not metering still cuts the body into the same |
5804 | | // segments, and records a safepoint wherever a segment would be charged, without |
5805 | | // emitting the charge. A runtime that was metering can stop at any of those when |
5806 | | // its gas runs out, and a snapshot taken there has to be able to go on in one that |
5807 | | // is not metering, or in a build that cannot meter at all. |
5808 | | // |
5809 | | // The body is cut into segments at the instructions that can transfer control, |
5810 | | // and each segment is charged in full, before any of it runs, by an op_UseGas |
5811 | | // carrying the total as an immediate. Prepaying is what makes the accounting |
5812 | | // hold: a call, or a loop iteration that never comes back round, has already |
5813 | | // been paid for by the time it leaves. |
5814 | | |
5815 | | // The cost table's unit is a ten-thousandth of a gas, which is why these numbers |
5816 | | // are as large as they are. Two runs of instructions it does not price had to be |
5817 | | // filled in. It has no floating point at all, ewasm having banned it, so float |
5818 | | // instructions are priced as their integer counterparts - division and square |
5819 | | // root as an integer divide, the rest as integer arithmetic. And it predates the |
5820 | | // reference, bulk memory and exception instructions, which are priced as the |
5821 | | // nearest thing it does list: a table access as a memory access, an operation |
5822 | | // over a whole region as memory.grow, a throw as a branch. |
5823 | | enum { |
5824 | | c_gasLocal = 1, // charged once per declared local, at function entry |
5825 | | c_gasNominal = 1, // nop, block, loop, if, const |
5826 | | c_gasArith = 45, |
5827 | | c_gasShift = 67, |
5828 | | c_gasBranch = 90, |
5829 | | c_gasMemSize = 100, |
5830 | | c_gasAccess = 120, // a local, global, table or memory access |
5831 | | c_gasHeavy = 10000, // an indirect call, or anything that works over a whole region |
5832 | | c_gasDivide = 36000 |
5833 | | }; |
5834 | | |
5835 | | // The most one op_UseGas can carry. A segment that reaches it is split into |
5836 | | // consecutive charges, which prepays exactly the same amount. |
5837 | 393k | # define d_m3MaxSegmentGas 0xFFFFFFFF |
5838 | | |
5839 | | static |
5840 | | u32 GetGasCost (m3opcode_t i_opcode) |
5841 | 459k | { |
5842 | 459k | switch (i_opcode) { |
5843 | 58.9k | case 0x00: // unreachable |
5844 | 77.3k | case 0x01: // nop |
5845 | 78.3k | case c_waOp_block: |
5846 | 79.8k | case c_waOp_loop: |
5847 | 84.9k | case c_waOp_if: |
5848 | 85.3k | case c_waOp_tryTable: // a block that also installs handlers |
5849 | 86.1k | case c_waOp_refNull: // a constant, by another name |
5850 | 86.2k | case c_waOp_refFunc: |
5851 | 86.2k | return c_gasNominal; |
5852 | | |
5853 | 37.5k | case c_waOp_end: |
5854 | 37.5k | return 0; // the one control instruction the table prices at nothing |
5855 | | |
5856 | 171 | case c_waOp_else: |
5857 | 1.21k | case c_waOp_branch: |
5858 | 4.18k | case c_waOp_branchIf: |
5859 | 7.13k | case c_waOp_return: |
5860 | 17.3k | case c_waOp_call: |
5861 | 29.4k | case c_waOp_returnCall: |
5862 | 29.6k | case c_waOp_throw: |
5863 | 30.6k | case c_waOp_throwRef: |
5864 | 30.6k | return c_gasBranch; |
5865 | | |
5866 | 715 | case c_waOp_branchTable: |
5867 | 7.77k | case 0x1a: // drop |
5868 | 15.4k | case 0x1b: // select |
5869 | 15.5k | case c_waOp_selectTyped: |
5870 | 15.5k | return c_gasAccess; |
5871 | | |
5872 | 198 | case c_waOp_callIndirect: |
5873 | 393 | case c_waOp_returnCallIndirect: |
5874 | 1.11k | case c_waOp_callRef: |
5875 | 1.77k | case c_waOp_returnCallRef: |
5876 | 1.77k | case c_waOp_memoryInit: |
5877 | 2.33k | case c_waOp_memoryCopy: |
5878 | 2.59k | case c_waOp_memoryFill: |
5879 | 2.71k | case c_waOp_tableInit: |
5880 | 3.00k | case c_waOp_tableCopy: |
5881 | 3.20k | case c_waOp_tableGrow: |
5882 | 3.34k | case c_waOp_tableFill: |
5883 | 4.97k | case 0x40: // memory.grow |
5884 | 4.97k | return c_gasHeavy; |
5885 | | |
5886 | 0 | case c_waOp_dataDrop: |
5887 | 35 | case c_waOp_elemDrop: |
5888 | 268 | case c_waOp_tableSize: |
5889 | 2.17k | case 0x3f: // memory.size |
5890 | 2.17k | return c_gasMemSize; |
5891 | | |
5892 | 312 | case c_waOp_refIsNull: |
5893 | 727 | case c_waOp_refAsNonNull: |
5894 | 727 | return c_gasArith; |
5895 | | |
5896 | 281k | default: |
5897 | 281k | break; |
5898 | 459k | } |
5899 | | |
5900 | | // The two integer arithmetic runs are priced by position, i32 at 0x6a and |
5901 | | // i64 at 0x7c, the table giving both the same shape |
5902 | 281k | if ((i_opcode >= 0x6a and i_opcode <= 0x78) or (i_opcode >= 0x7c and i_opcode <= 0x8a)) { |
5903 | | // clang-format off |
5904 | 28.6k | static const u16 c_arithmeticCosts [] = { |
5905 | 28.6k | c_gasArith, c_gasArith, c_gasArith, // add, sub, mul |
5906 | 28.6k | c_gasDivide, c_gasDivide, c_gasDivide, c_gasDivide, // div_s, div_u, rem_s, rem_u |
5907 | 28.6k | c_gasArith, c_gasArith, c_gasArith, // and, or, xor |
5908 | 28.6k | c_gasShift, c_gasShift, c_gasShift, // shl, shr_s, shr_u |
5909 | 28.6k | c_gasBranch, c_gasBranch // rotl, rotr |
5910 | 28.6k | }; |
5911 | | // clang-format on |
5912 | | |
5913 | 28.6k | return c_arithmeticCosts[i_opcode - (i_opcode <= 0x78 ? 0x6a : 0x7c)]; |
5914 | 28.6k | } |
5915 | | |
5916 | | // the atomic accesses are memory accesses; what waits, wakes or orders is worth |
5917 | | // as much as an operation over a whole region |
5918 | 253k | if (i_opcode >= (c_waOp_atomic << 8) and i_opcode <= ((c_waOp_atomic << 8) | c_waOp_lastAtomic)) { |
5919 | 1.08k | return (i_opcode <= c_waOp_atomicFence) ? c_gasHeavy : c_gasAccess; |
5920 | 1.08k | } |
5921 | 251k | if (i_opcode >= 0x20 and i_opcode <= 0x3e) { |
5922 | 44.4k | return c_gasAccess; // locals, globals, table accesses, loads and stores |
5923 | 44.4k | } |
5924 | 207k | if (i_opcode >= 0x41 and i_opcode <= 0x44) { |
5925 | 120k | return c_gasNominal; // i32/i64/f32/f64.const |
5926 | 120k | } |
5927 | 87.2k | if (i_opcode >= 0x8b and i_opcode <= 0xa6) { |
5928 | | // the float run, priced as integer arithmetic except where it divides |
5929 | 13.0k | bool divides = (i_opcode == 0x91 or i_opcode == 0x95 or // f32.sqrt, f32.div |
5930 | 11.8k | i_opcode == 0x9f or i_opcode == 0xa3); // f64.sqrt, f64.div |
5931 | | |
5932 | 13.0k | return divides ? c_gasDivide : c_gasArith; |
5933 | 13.0k | } |
5934 | | |
5935 | | // comparisons and clz/ctz/popcnt (0x45..0x69, 0x79..0x7b), the conversions |
5936 | | // and sign extensions (0xa7..0xc4), the saturating truncations, the wide |
5937 | | // arithmetic, and anything else that reaches here: plain arithmetic |
5938 | 74.1k | return c_gasArith; |
5939 | 87.2k | } |
5940 | | |
5941 | | // The instructions ewasm's design segments a body at. A segment runs up to and |
5942 | | // including the next one of these, so every cycle a body can execute - a loop's |
5943 | | // back edge, a branch out of a block, falling off the end of a function - passes |
5944 | | // through at least one, and no path can run unmetered however it is entered. |
5945 | | static |
5946 | | bool IsGasSegmentEnd (m3opcode_t i_opcode) |
5947 | 459k | { |
5948 | 459k | switch (i_opcode) { |
5949 | 1.54k | case c_waOp_loop: |
5950 | 6.65k | case c_waOp_if: |
5951 | 6.83k | case c_waOp_else: |
5952 | 44.3k | case c_waOp_end: |
5953 | 45.4k | case c_waOp_branch: |
5954 | 48.3k | case c_waOp_branchIf: |
5955 | 49.1k | case c_waOp_branchTable: |
5956 | 52.0k | case c_waOp_return: |
5957 | 64.1k | case c_waOp_returnCall: |
5958 | 64.3k | case c_waOp_returnCallIndirect: |
5959 | 64.9k | case c_waOp_returnCallRef: |
5960 | 64.9k | return true; |
5961 | 394k | default: |
5962 | 394k | return false; |
5963 | 459k | } |
5964 | 459k | } |
5965 | | |
5966 | | // Ends the open segment by writing what it accumulated into the immediate its |
5967 | | // op_UseGas is still waiting on. Emitting onto a later code page does not move |
5968 | | // an earlier one, so the reservation is still where it was left. |
5969 | | static |
5970 | | void CloseGasSegment (IM3Compilation o) |
5971 | 64.9k | { |
5972 | 64.9k | if (o->isGasSegmentOpen) { |
5973 | 64.9k | if (o->gasPatch) { |
5974 | 64.9k | u32 cost = o->gasCost; |
5975 | 64.9k | memcpy(o->gasPatch, &cost, sizeof(cost)); |
5976 | | |
5977 | 64.9k | o->gasPatch = NULL; |
5978 | 64.9k | } |
5979 | 64.9k | o->gasCost = 0; |
5980 | 64.9k | o->isGasSegmentOpen = false; |
5981 | 64.9k | } |
5982 | 64.9k | } |
5983 | | |
5984 | | // Whether this compilation emits the charges |
5985 | | static inline |
5986 | | bool IsMetering (IM3Compilation o) |
5987 | 459k | { |
5988 | 459k | return o->runtime->gasLimit != 0; |
5989 | 459k | } |
5990 | | |
5991 | | // Charges one instruction to the segment being compiled, opening a segment at |
5992 | | // this point in the code stream if none is open, and settling the segment when |
5993 | | // the instruction is one that ends it. Called before the instruction is |
5994 | | // compiled, which is what both halves need: the op_UseGas lands ahead of what it |
5995 | | // pays for, and the cost is final before a block-structured instruction - loop, |
5996 | | // if, else - compiles its body from inside its own compiler. The body then opens |
5997 | | // a segment of its own, inside the loop or the arm, where it is charged on every |
5998 | | // pass rather than once on the way in. |
5999 | | static |
6000 | | M3Result MeterOpcode (IM3Compilation o, m3opcode_t i_opcode) |
6001 | 548k | { |
6002 | 548k | M3Result result = m3Err_none; |
6003 | | |
6004 | | // constant expressions are not metered: they run once at instantiation, |
6005 | | // before the module is anything a gas budget was handed out for |
6006 | 548k | if (o->function and o->page and IsMetering(o)) { |
6007 | | // only the 0xFC or 0xFE prefix has been read so far; the instruction it names is |
6008 | | // the LEB128 u32 the compiler is about to take. Peek it without consuming. |
6009 | | // Anything the tables do not cover leaves the prefix priced on its own, |
6010 | | // which is moot - the compile fails on it a moment later. |
6011 | 459k | if (i_opcode == c_waOp_extended or i_opcode == c_waOp_atomic) { |
6012 | 9.07k | bytes_t peek = o->wasm; |
6013 | 9.07k | u32 sub; |
6014 | 9.07k | u32 last = (i_opcode == c_waOp_atomic) ? c_waOp_lastAtomic : c_waOp_lastExtended; |
6015 | | |
6016 | 9.07k | if (not ReadLEB_u32(&sub, &peek, o->wasmEnd) and sub <= last) { |
6017 | 8.99k | i_opcode = (m3opcode_t)((i_opcode << 8) | sub); |
6018 | 8.99k | } |
6019 | 9.07k | } |
6020 | | |
6021 | 459k | u32 cost = GetGasCost(i_opcode); |
6022 | | |
6023 | 459k | if (o->isGasSegmentOpen and o->gasCost > d_m3MaxSegmentGas - cost) { |
6024 | 0 | CloseGasSegment(o); |
6025 | 0 | } |
6026 | | |
6027 | 459k | if (not o->isGasSegmentOpen) { |
6028 | 65.8k | o->isGasSegmentOpen = true; |
6029 | | |
6030 | 65.8k | _ (EmitOp(o, op_UseGas)); |
6031 | 65.8k | o->gasPatch = (void*)GetPagePC(o->page); |
6032 | 65.8k | EmitConstant32(o, 0); |
6033 | 65.8k | } |
6034 | | |
6035 | 459k | o->gasCost += cost; |
6036 | | |
6037 | 459k | if (IsGasSegmentEnd(i_opcode)) { |
6038 | 64.9k | CloseGasSegment(o); |
6039 | 64.9k | } |
6040 | 459k | } |
6041 | | |
6042 | 548k | _catch: return result; |
6043 | 548k | } |
6044 | | |
6045 | | #endif // d_m3HasGasMetering |
6046 | | |
6047 | | |
6048 | | M3Result CompileBlockStatements (IM3Compilation o) |
6049 | 44.1k | { |
6050 | 44.1k | M3Result result = m3Err_none; |
6051 | 44.1k | bool validEnd = false; |
6052 | | |
6053 | 549k | while (o->wasm < o->wasmEnd) { |
6054 | | #if d_m3EnableOpTracing |
6055 | | if (o->numEmits) { |
6056 | | EmitOp(o, op_DumpStack); |
6057 | | EmitConstant32(o, o->numOpcodes); |
6058 | | EmitConstant32(o, GetMaxUsedSlotPlusOne(o)); |
6059 | | EmitPointer(o, o->function); |
6060 | | |
6061 | | o->numEmits = 0; |
6062 | | } |
6063 | | #endif |
6064 | | |
6065 | 549k | m3opcode_t opcode; |
6066 | 549k | o->lastOpcodeStart = o->wasm; |
6067 | 549k | _ (Read_opcode(&opcode, &o->wasm, o->wasmEnd)); |
6068 | 549k | log_opcode(o, opcode); |
6069 | | |
6070 | | // Restrict opcodes when evaluating expressions |
6071 | 549k | if (M3_UNLIKELY(not o->function)) { |
6072 | | // clang-format off |
6073 | 88.6k | switch (opcode) { |
6074 | 57.2k | case c_waOp_i32_const: case c_waOp_i64_const: |
6075 | 60.1k | case c_waOp_f32_const: case c_waOp_f64_const: |
6076 | 64.9k | case c_waOp_getGlobal: case c_waOp_end: |
6077 | 64.9k | #if d_m3HasRefTypes |
6078 | 68.7k | case c_waOp_refNull: case c_waOp_refFunc: |
6079 | 68.7k | #endif |
6080 | 68.7k | #if d_m3HasExtendedConst |
6081 | 78.4k | case c_waOp_i32_add: case c_waOp_i32_sub: case c_waOp_i32_mul: |
6082 | 88.6k | case c_waOp_i64_add: case c_waOp_i64_sub: case c_waOp_i64_mul: |
6083 | 88.6k | #endif |
6084 | 88.6k | break; |
6085 | 59 | default: |
6086 | 59 | _throw(m3Err_restrictedOpcode); |
6087 | 88.6k | } |
6088 | | // clang-format on |
6089 | 88.6k | } |
6090 | | |
6091 | 548k | IM3OpInfo opinfo = GetOpInfo(opcode); |
6092 | | |
6093 | 548k | if (M3_UNLIKELY(opinfo == NULL)) { |
6094 | 755 | _throw(ErrorCompile(m3Err_unknownOpcode, o, "opcode '%x' not available", opcode)); |
6095 | 0 | } |
6096 | | |
6097 | 548k | #if d_m3HasGasMetering |
6098 | 548k | _ (MeterOpcode(o, opcode)); |
6099 | 548k | #endif |
6100 | | |
6101 | 548k | if (opinfo->compiler != c_cc_NULL) { |
6102 | 439k | _ ((*GetCompiler(opinfo))(o, opcode)) |
6103 | 439k | } else { |
6104 | 109k | _ (Compile_Operator(o, opcode)); |
6105 | 108k | } |
6106 | | |
6107 | 546k | o->previousOpcode = opcode; |
6108 | | |
6109 | 546k | if (opcode == c_waOp_else) { |
6110 | 171 | _throwif(m3Err_wasmMalformed, o->block.opcode != c_waOp_if); |
6111 | 171 | validEnd = true; |
6112 | 171 | break; |
6113 | 546k | } else if (opcode == c_waOp_end) { |
6114 | 41.5k | validEnd = true; |
6115 | 41.5k | break; |
6116 | 41.5k | } |
6117 | 546k | } |
6118 | 41.8k | _throwif(m3Err_wasmMalformed, not validEnd); |
6119 | | |
6120 | 44.1k | _catch: |
6121 | 44.1k | return result; |
6122 | 41.7k | } |
6123 | | |
6124 | | static |
6125 | | M3Result PushBlockResults (IM3Compilation o) |
6126 | 11.0k | { |
6127 | 11.0k | M3Result result = m3Err_none; |
6128 | | |
6129 | 11.0k | u16 numResults = GetFuncTypeNumResults(o->block.type); |
6130 | | |
6131 | 22.9k | for (u16 i = 0; i < numResults; ++i) { |
6132 | 11.8k | m3type_t type = GetFuncTypeResultType(o->block.type, i); |
6133 | | |
6134 | 11.8k | if (i == numResults - 1 and IsFpType(type)) { |
6135 | 427 | _ (PushRegister(o, type)); |
6136 | 11.4k | } else { |
6137 | 11.4k | _ (PushAllocatedSlot(o, type)); |
6138 | 11.4k | } |
6139 | 11.8k | } |
6140 | | |
6141 | 11.0k | _catch: return result; |
6142 | 11.0k | } |
6143 | | |
6144 | | |
6145 | | M3Result CompileBlock (IM3Compilation o, IM3FuncType i_blockType, m3opcode_t i_blockOpcode) |
6146 | 8.35k | { |
6147 | 8.35k | d_m3Assert (not IsRegisterAllocated (o, 0)); |
6148 | 8.35k | d_m3Assert (not IsRegisterAllocated (o, 1)); |
6149 | 8.35k | InvalidateFold(o); |
6150 | | |
6151 | 8.35k | M3CompilationScope outerScope = o->block; |
6152 | 8.35k | M3CompilationScope* block = &o->block; |
6153 | | |
6154 | 8.35k | block->outer = &outerScope; |
6155 | 8.35k | block->pc = GetPagePC(o->page); |
6156 | 8.35k | block->patches = NULL; |
6157 | 8.35k | block->type = i_blockType; |
6158 | 8.35k | block->opcode = i_blockOpcode; |
6159 | 8.35k | block->depth++; |
6160 | 8.35k | block->snapshotBlock = 0; |
6161 | | |
6162 | | /* |
6163 | | The block stack frame is a little strange but for good reasons. Because blocks need to be restarted to |
6164 | | compile different pathways (if/else), the incoming params must be saved. The parameters are popped |
6165 | | and validated. But, then the stack top is readjusted so they aren't subsequently overwritten. |
6166 | | Next, the result are preallocated to find destination slots. But again these are immediately popped |
6167 | | (deallocated) and the stack top is readjusted to keep these records in pace. This allows branch instructions |
6168 | | to find their result landing pads. Finally, the params are copied from the "dead" records and pushed back |
6169 | | onto the stack as active stack items for the CompileBlockStatements () call. |
6170 | | |
6171 | | [ block ] |
6172 | | [ params ] |
6173 | | ------------------ |
6174 | | [ result ] <---- blockStackIndex |
6175 | | [ slots ] |
6176 | | ------------------ |
6177 | | [ saved param ] |
6178 | | [ records ] |
6179 | | <----- exitStackIndex |
6180 | | */ |
6181 | | |
6182 | 8.35k | _try { |
6183 | 8.35k | #if d_m3HasSnapshots |
6184 | | // op_Loop's frame records the body, which is also the loop's identity; |
6185 | | // op_TryTable's records its clause table, two words a clause back from it |
6186 | 8.35k | if (i_blockOpcode == c_waOp_loop) { |
6187 | 1.53k | _ (RecordBlockStart(o, i_blockOpcode, block->pc, 0)); |
6188 | 1.53k | } |
6189 | 6.81k | # if d_m3HasExceptionHandling |
6190 | 6.81k | else if (i_blockOpcode == c_waOp_tryTable) { |
6191 | 405 | _ (RecordBlockStart(o, i_blockOpcode, block->pc - 2 * o->numTryClauses, o->numTryClauses)); |
6192 | 405 | } |
6193 | 8.35k | # endif |
6194 | 8.35k | #endif |
6195 | | |
6196 | | // validate and dealloc params ---------------------------- |
6197 | | |
6198 | 8.35k | u16 stackIndex = o->stackIndex; |
6199 | | |
6200 | 8.35k | u16 numParams = GetFuncTypeNumParams(i_blockType); |
6201 | | |
6202 | 8.35k | if (i_blockOpcode != c_waOp_else) { |
6203 | 15.1k | for (u16 i = 0; i < numParams; ++i) { |
6204 | 7.16k | m3type_t type = GetFuncTypeParamType(i_blockType, numParams - 1 - i); |
6205 | 7.16k | _ (PopType(o, type)); |
6206 | 7.15k | } |
6207 | 8.02k | } else { |
6208 | 329 | if (IsStackPolymorphic(o) && o->block.blockStackIndex + numParams > o->stackIndex) { |
6209 | 77 | o->stackIndex = o->block.blockStackIndex; |
6210 | 252 | } else { |
6211 | 252 | o->stackIndex -= numParams; |
6212 | 252 | } |
6213 | 329 | } |
6214 | | |
6215 | 8.34k | u16 paramIndex = o->stackIndex; |
6216 | 8.34k | block->exitStackIndex = paramIndex; // consume the params at block exit |
6217 | | |
6218 | | // keep copies of param slots in the stack |
6219 | 8.34k | o->stackIndex = stackIndex; |
6220 | | |
6221 | | // find slots for the results ---------------------------- |
6222 | 8.34k | _ (PushBlockResults(o)); |
6223 | | |
6224 | 8.33k | stackIndex = o->stackIndex; |
6225 | | |
6226 | | // dealloc but keep record of the result slots in the stack |
6227 | 8.33k | u16 numResults = GetFuncTypeNumResults(i_blockType); |
6228 | 14.9k | while (numResults--) { |
6229 | 6.65k | Pop(o); |
6230 | 6.65k | } |
6231 | | |
6232 | 8.33k | block->blockStackIndex = o->stackIndex = stackIndex; |
6233 | | |
6234 | | // push the params back onto the stack ------------------- |
6235 | 15.6k | for (u16 i = 0; i < numParams; ++i) { |
6236 | 7.29k | m3type_t type = GetFuncTypeParamType(i_blockType, i); |
6237 | | |
6238 | 7.29k | u16 slot = GetSlotForStackIndex(o, paramIndex + i); |
6239 | 7.29k | Push(o, type, slot); |
6240 | | |
6241 | 7.29k | if (slot >= o->slotFirstDynamicIndex && slot != c_slotUnused) { |
6242 | 5.00k | _ (MarkSlotsAllocatedByType(o, slot, type)); |
6243 | 5.00k | } |
6244 | 7.29k | } |
6245 | | |
6246 | | //-------------------------------------------------------- |
6247 | | |
6248 | 8.33k | #if d_m3HasExceptionHandling |
6249 | | // with the scope pushed and the params back on, a catch label of 0 names |
6250 | | // this block - which is what the proposal says it should |
6251 | 8.33k | if (i_blockOpcode == c_waOp_tryTable) { |
6252 | 405 | _ (EmitCatchStubs(o)); |
6253 | 405 | } |
6254 | 8.33k | #endif |
6255 | | |
6256 | 8.33k | _ (CompileBlockStatements(o)); |
6257 | | |
6258 | 7.72k | _ (ValidateBlockEnd(o)); |
6259 | | |
6260 | 7.71k | if (o->function) // skip for expressions |
6261 | 7.71k | { |
6262 | 7.71k | if (not IsStackPolymorphic(o)) { |
6263 | 5.01k | _ (ResolveBlockResults(o, &o->block, /* isBranch: */ false)); |
6264 | 5.00k | } |
6265 | | |
6266 | 7.70k | #if d_m3HasExceptionHandling |
6267 | | // Falling out of the end of a try region retires its handler. This sits |
6268 | | // ahead of PatchBranches so branches into the block's exit land past it: |
6269 | | // a branch out of the try popped its own handler at the branch site. |
6270 | 7.70k | if (i_blockOpcode == c_waOp_tryTable) { |
6271 | 298 | _ (EmitOp(o, op_PopHandlers)); |
6272 | 298 | EmitConstant32(o, 1); |
6273 | 298 | } |
6274 | 7.70k | #endif |
6275 | | |
6276 | 7.70k | _ (UnwindBlockStack(o)) |
6277 | | |
6278 | 7.70k | if (not ((i_blockOpcode == c_waOp_if and numResults) or o->previousOpcode == c_waOp_else)) { |
6279 | 2.71k | o->stackIndex = o->block.exitStackIndex; |
6280 | 2.71k | _ (PushBlockResults(o)); |
6281 | 2.70k | } |
6282 | 7.70k | } |
6283 | | |
6284 | 7.70k | PatchBranches(o); |
6285 | | |
6286 | 7.70k | #if d_m3HasSnapshots |
6287 | 7.70k | RecordBlockEnd(o); |
6288 | | |
6289 | 7.70k | if (o->localInitDepth) { |
6290 | 0 | for (u32 i = 0; i < o->snapshotMap->numLocals; ++i) { |
6291 | 0 | if (o->localInitDepth[i] >= (u32)o->block.depth) { |
6292 | 0 | o->localInitDepth[i] = UINT32_MAX; |
6293 | 0 | } |
6294 | 0 | } |
6295 | 0 | } |
6296 | 7.70k | #endif |
6297 | 7.70k | o->block = outerScope; |
6298 | | |
6299 | 8.35k | } _catch: return result; |
6300 | 7.70k | } |
6301 | | |
6302 | | static |
6303 | | M3Result CompileLocals (IM3Compilation o) |
6304 | 31.1k | { |
6305 | 31.1k | M3Result result; |
6306 | | |
6307 | 31.1k | u32 numLocals = 0; |
6308 | 31.1k | u32 numLocalBlocks; |
6309 | 31.1k | _ (ReadLEB_u32(&numLocalBlocks, &o->wasm, o->wasmEnd)); |
6310 | | |
6311 | 57.2k | for (u32 l = 0; l < numLocalBlocks; ++l) { |
6312 | 26.1k | u32 varCount; |
6313 | 26.1k | m3type_t localType; |
6314 | | |
6315 | 26.1k | _ (ReadLEB_u32(&varCount, &o->wasm, o->wasmEnd)); |
6316 | 26.1k | _ (ParseValueType(o->module, &localType, &o->wasm, o->wasmEnd)); |
6317 | 26.1k | numLocals += varCount; m3log (compile, "pushing locals. count: %d; type: %s", varCount, c_waTypes [BaseTypeOf(localType)]); |
6318 | 5.48M | while (varCount--) { |
6319 | 5.45M | _ (PushAllocatedSlot(o, localType)); |
6320 | 5.45M | } |
6321 | 26.1k | } |
6322 | | |
6323 | 31.0k | if (o->function) { |
6324 | 31.0k | o->function->numLocals = numLocals; |
6325 | 31.0k | } |
6326 | | |
6327 | 31.1k | _catch: return result; |
6328 | 31.0k | } |
6329 | | |
6330 | | static |
6331 | | M3Result ReserveConstants (IM3Compilation o) |
6332 | 31.0k | { |
6333 | 31.0k | M3Result result = m3Err_none; |
6334 | | |
6335 | | // in the interest of speed, this blindly scans the Wasm code looking for any byte |
6336 | | // that looks like an const opcode. |
6337 | 31.0k | u16 numConstantSlots = 0; |
6338 | | |
6339 | 31.0k | bytes_t wa = o->wasm; |
6340 | 864k | while (wa < o->wasmEnd) { |
6341 | 833k | u8 code = *wa++; |
6342 | 833k | u16 addSlots = 0; |
6343 | | |
6344 | 833k | if (code == c_waOp_i32_const or code == c_waOp_f32_const) { |
6345 | 77.0k | addSlots = 1; |
6346 | 756k | } else if (code == c_waOp_i64_const or code == c_waOp_f64_const) { |
6347 | 58.9k | addSlots = GetTypeNumSlots(c_m3Type_i64); |
6348 | 58.9k | } |
6349 | | |
6350 | 833k | if (numConstantSlots + addSlots >= d_m3MaxConstantTableSize) { |
6351 | 15 | break; |
6352 | 15 | } |
6353 | | |
6354 | 833k | numConstantSlots += addSlots; |
6355 | 833k | } |
6356 | | |
6357 | | // if constants overflow their reserved stack space, the compiler simply emits op_Const |
6358 | | // operations as needed. Compiled expressions (global inits) don't pass through this |
6359 | | // ReserveConstants function and thus always produce inline constants. |
6360 | | |
6361 | 31.0k | AlignSlotToType(&numConstantSlots, c_m3Type_i64); m3log (compile, "reserved constant slots: %d", numConstantSlots); |
6362 | | |
6363 | 31.0k | o->slotFirstDynamicIndex = o->slotFirstConstIndex + numConstantSlots; |
6364 | | |
6365 | 31.0k | if (o->slotFirstDynamicIndex >= d_m3MaxFunctionSlots) { |
6366 | 1 | _throw(m3Err_functionStackOverflow); |
6367 | 0 | } |
6368 | | |
6369 | 31.0k | _catch: |
6370 | 31.0k | return result; |
6371 | 31.0k | } |
6372 | | |
6373 | | |
6374 | | // A constant expression compiles like a miniature function root block: no args, |
6375 | | // no locals and a single result, which Compile_End copies down into slot 0 for |
6376 | | // EvaluateExpression to read back. |
6377 | | M3Result CompileExpression (IM3Compilation o, IM3FuncType i_resultType) |
6378 | 1.26k | { |
6379 | 1.26k | M3Result result = m3Err_none; |
6380 | | |
6381 | 1.26k | o->block.type = i_resultType; |
6382 | | |
6383 | 1.26k | u16 numRetSlots = GetFuncTypeNumResults(i_resultType) * c_ioSlotCount; |
6384 | | |
6385 | 3.73k | for (u16 i = 0; i < numRetSlots; ++i) { |
6386 | 2.46k | MarkSlotAllocated(o, i); |
6387 | 2.46k | } |
6388 | | |
6389 | 1.26k | o->maxStackSlots = o->slotMaxAllocatedIndexPlusOne = o->slotFirstDynamicIndex = numRetSlots; |
6390 | | |
6391 | 1.26k | _ (CompileBlockStatements(o)); |
6392 | | |
6393 | 1.26k | _catch: return result; |
6394 | 1.10k | } |
6395 | | |
6396 | | |
6397 | | M3Result CompileFunction (IM3Function io_function) |
6398 | 34.5k | { |
6399 | 34.5k | if (!io_function->wasm) { |
6400 | | // An import. Either linking pointed it at another module's function - |
6401 | | // in which case that one carries the body - or a host function was |
6402 | | // bound to it, or it is simply unsatisfied. |
6403 | 2 | IM3Function impl = Function_Implementation(io_function); |
6404 | | |
6405 | 2 | if (impl != io_function) { |
6406 | 0 | if (not impl->compiled) { |
6407 | 0 | M3Result r = CompileFunction(impl); |
6408 | 0 | if (r) { |
6409 | 0 | return r; |
6410 | 0 | } |
6411 | 0 | } |
6412 | | |
6413 | | // keep the placeholder callable too, for anything still holding it |
6414 | 0 | io_function->compiled = impl->compiled; |
6415 | 0 | return m3Err_none; |
6416 | 0 | } |
6417 | | |
6418 | 2 | if (io_function->compiled) { |
6419 | 0 | return m3Err_none; |
6420 | 0 | } |
6421 | | |
6422 | 2 | return ErrorModule(m3Err_functionImportMissing, io_function->module, "'%s.%s'", |
6423 | 2 | GetFunctionImportModuleName(io_function), |
6424 | 2 | m3_GetFunctionName(io_function)); |
6425 | 2 | } |
6426 | | |
6427 | 34.5k | #if d_m3EnableValidation |
6428 | 34.5k | if (not io_function->module->runtime->skipValidation) { |
6429 | 34.5k | M3Result vr = ValidateFunction(io_function); |
6430 | 34.5k | if (vr) { |
6431 | 3.43k | return vr; |
6432 | 3.43k | } |
6433 | 34.5k | } |
6434 | 31.1k | #endif |
6435 | | |
6436 | 31.1k | IM3FuncType funcType = io_function->funcType; m3log (compile, "compiling: [%d] %s %s; wasm-size: %d", |
6437 | 31.1k | io_function->index, m3_GetFunctionName (io_function), SPrintFuncTypeSignature (funcType), (u32) (io_function->wasmEnd - io_function->wasm)); |
6438 | 31.1k | IM3Runtime runtime = io_function->module->runtime; |
6439 | | |
6440 | 31.1k | IM3Compilation o = &runtime->compilation; d_m3Assert (d_m3MaxFunctionSlots >= d_m3MaxFunctionStackHeight * (d_m3Use32BitSlots + 1)) // need twice as many slots in 32-bit mode |
6441 | 31.1k | memset(o, 0x0, sizeof(M3Compilation)); |
6442 | | |
6443 | 31.1k | o->runtime = runtime; |
6444 | 31.1k | o->module = io_function->module; |
6445 | 31.1k | o->function = io_function; |
6446 | 31.1k | o->wasm = io_function->wasm; |
6447 | 31.1k | o->wasmEnd = io_function->wasmEnd; |
6448 | 31.1k | o->block.type = funcType; |
6449 | | |
6450 | 31.1k | _try { |
6451 | | // skip over code size. the end was already calculated during parse phase |
6452 | 31.1k | u32 size; |
6453 | 31.1k | _ (ReadLEB_u32(&size, &o->wasm, o->wasmEnd)); d_m3Assert (size == (o->wasmEnd - o->wasm)) |
6454 | | |
6455 | 31.1k | #if d_m3HasSnapshots |
6456 | 31.1k | o->bodyStart = o->wasm; |
6457 | 31.1k | #endif |
6458 | | |
6459 | 31.1k | #if d_m3HasSnapshots |
6460 | | // only code that can be suspended can end up in a snapshot |
6461 | 31.1k | if (runtime->isSuspendable) { |
6462 | 0 | o->snapshotMap = m3_AllocStruct(M3SnapshotMap); |
6463 | 0 | _throwifnull(o->snapshotMap); |
6464 | 0 | } |
6465 | 31.1k | #endif |
6466 | | |
6467 | 31.1k | _ (AcquireCompilationCodePage(o, &o->page)); |
6468 | | |
6469 | 31.1k | pc_t pc = GetPagePC(o->page); |
6470 | | |
6471 | 31.1k | u16 numRetSlots = GetFunctionNumReturns(o->function) * c_ioSlotCount; |
6472 | | |
6473 | 42.1k | for (u16 i = 0; i < numRetSlots; ++i) { |
6474 | 11.0k | MarkSlotAllocated(o, i); |
6475 | 11.0k | } |
6476 | | |
6477 | 31.1k | o->function->numRetSlots = o->slotFirstDynamicIndex = numRetSlots; |
6478 | | |
6479 | 31.1k | u16 numArgs = GetFunctionNumArgs(o->function); |
6480 | | |
6481 | | // push the arg types to the type stack |
6482 | 34.7k | for (u16 i = 0; i < numArgs; ++i) { |
6483 | 3.66k | m3type_t type = GetFunctionArgType(o->function, i); |
6484 | 3.66k | _ (PushAllocatedSlot(o, type)); |
6485 | | |
6486 | | // prevent allocator fill-in |
6487 | 3.66k | o->slotFirstDynamicIndex += c_ioSlotCount; |
6488 | 3.66k | } |
6489 | | |
6490 | 31.1k | o->slotMaxAllocatedIndexPlusOne = o->function->numRetAndArgSlots = o->slotFirstLocalIndex = o->slotFirstDynamicIndex; |
6491 | | |
6492 | 31.1k | _ (CompileLocals(o)); |
6493 | | |
6494 | 31.0k | #if d_m3HasSnapshots |
6495 | 31.0k | _ (RecordLocals(o)); |
6496 | 31.0k | #endif |
6497 | | |
6498 | 31.0k | #if d_m3HasGasMetering |
6499 | | // the table charges for a function's locals as well as for its instructions. |
6500 | | // op_Entry is what sets them up, so the charge rides on the body's first |
6501 | | // segment, which is emitted right after it |
6502 | 31.0k | o->gasCost = o->function->numLocals * c_gasLocal; |
6503 | 31.0k | #endif |
6504 | | |
6505 | 31.0k | u16 maxSlot = GetMaxUsedSlotPlusOne(o); |
6506 | | |
6507 | 31.0k | o->function->numLocalBytes = (maxSlot - o->slotFirstLocalIndex) * sizeof(m3slot_t); |
6508 | | |
6509 | 31.0k | o->slotFirstConstIndex = o->slotMaxConstIndex = maxSlot; |
6510 | | |
6511 | | // ReserveConstants initializes o->firstDynamicSlotNumber |
6512 | 31.0k | _ (ReserveConstants(o)); |
6513 | | |
6514 | | // start tracking the max stack used (Push() also updates this value) so that op_Entry can precisely detect stack overflow |
6515 | 31.0k | o->maxStackSlots = o->slotMaxAllocatedIndexPlusOne = o->slotFirstDynamicIndex; |
6516 | | |
6517 | 31.0k | o->block.blockStackIndex = o->stackFirstDynamicIndex = o->stackIndex; m3log (compile, "start stack index: %u; max stack slots: %u", |
6518 | 31.0k | (u32) o->stackFirstDynamicIndex, (u32) o->maxStackSlots); |
6519 | 31.0k | #if d_m3HasStackSwitching |
6520 | | // A suspendable body opens by checking for a pause request, once its frame |
6521 | | // is set up. The pause point's offset is the first instruction's, just past |
6522 | | // the local declarations - the body's end, when it has no other. |
6523 | 31.0k | if (runtime->isSuspendable) { |
6524 | 0 | o->lastOpcodeStart = o->wasm; |
6525 | | # if d_m3EntryKeepsFrame |
6526 | | _ (EmitOp(o, op_Entry)); |
6527 | | EmitPointer(o, io_function); |
6528 | | _ (EmitOp(o, op_EntryCheck)); |
6529 | | _ (RecordSafePoint(o, safepoint_entry, 1, 0, 0, NULL)); |
6530 | | # else |
6531 | 0 | _ (EmitOp(o, op_Entry_Suspendable)); |
6532 | 0 | EmitPointer(o, io_function); |
6533 | 0 | _ (RecordSafePoint(o, safepoint_entry, 0, 0, 0, NULL)); |
6534 | 0 | # endif |
6535 | 0 | } else |
6536 | 31.0k | #endif |
6537 | 31.0k | { |
6538 | 31.0k | _ (EmitOp(o, op_Entry)); |
6539 | 31.0k | EmitPointer(o, io_function); |
6540 | 31.0k | } |
6541 | | |
6542 | 31.0k | _ (CompileBlockStatements(o)); |
6543 | | |
6544 | 29.9k | _throwif(m3Err_wasmMalformed, o->previousOpcode != c_waOp_end); |
6545 | | |
6546 | 29.9k | io_function->compiled = pc; |
6547 | 29.9k | io_function->maxStackSlots = o->maxStackSlots; |
6548 | | |
6549 | 29.9k | u16 numConstantSlots = o->slotMaxConstIndex - o->slotFirstConstIndex; m3log (compile, "unique constant slots: %u; unused slots: %u", |
6550 | 29.9k | numConstantSlots, o->slotFirstDynamicIndex - o->slotMaxConstIndex); |
6551 | 29.9k | io_function->numConstantBytes = numConstantSlots * sizeof(m3slot_t); |
6552 | | |
6553 | 29.9k | if (numConstantSlots) { |
6554 | 27.5k | io_function->constants = m3_CopyMem((cbytes_t)o->constants, io_function->numConstantBytes); |
6555 | 27.5k | _throwifnull(io_function->constants); |
6556 | 27.5k | } |
6557 | | |
6558 | 29.9k | #if d_m3HasSnapshots |
6559 | 29.9k | io_function->snapshotMap = o->snapshotMap; |
6560 | 29.9k | o->snapshotMap = NULL; |
6561 | 29.9k | #endif |
6562 | | |
6563 | 31.1k | } _catch: |
6564 | | |
6565 | 31.1k | ReleaseCompilationCodePage(o); |
6566 | | |
6567 | 31.1k | #if d_m3HasSnapshots |
6568 | 31.1k | SnapshotMap_Free(o->snapshotMap); |
6569 | 31.1k | o->snapshotMap = NULL; |
6570 | 31.1k | m3_Free(o->localInitDepth); |
6571 | 31.1k | #endif |
6572 | | |
6573 | 31.1k | return result; |
6574 | 29.9k | } |
6575 | | |
6576 | | #if d_m3HasStackSwitching |
6577 | | m3ret_t ResumeContinuation (IM3Runtime i_runtime, IM3Continuation i_cont) |
6578 | 0 | { |
6579 | 0 | if (M3_UNLIKELY(!i_cont || i_cont->state != cont_suspended)) { |
6580 | 0 | return m3Err_none; |
6581 | 0 | } |
6582 | | |
6583 | 0 | M3MemoryHeader* _mem = i_cont->entryFunction ? Module_MemoryHeader(i_cont->entryFunction->module) : NULL; |
6584 | |
|
6585 | 0 | i_runtime->activeContinuation = i_cont; |
6586 | 0 | i_cont->state = cont_running; |
6587 | |
|
6588 | 0 | i32 outermost = (i32)i_cont->numFrames - 1; |
6589 | 0 | i_cont->numFrames = 0; |
6590 | | |
6591 | | // with no frames standing, this lands straight on the suspension point |
6592 | 0 | m3ret_t r = ReplayFrames(i_cont, outermost, _mem); |
6593 | |
|
6594 | 0 | if (r == m3Err_continuationSuspended) { |
6595 | 0 | i_cont->state = cont_suspended; |
6596 | 0 | } else { |
6597 | 0 | i_cont->state = cont_returned; |
6598 | 0 | } |
6599 | 0 | i_runtime->activeContinuation = NULL; |
6600 | |
|
6601 | 0 | return r; |
6602 | 0 | } |
6603 | | #endif |