@@ -42,6 +42,11 @@ bool IsFpRegisterLocation (i16 i_location) { return (i_location == d_m3
4242bool IsIntRegisterLocation (i16 i_location ) { return (i_location == d_m3Reg0SlotAlias ); }
4343
4444
45+ u32 GetTypeNumSlots (u8 i_type )
46+ {
47+ return Is64BitType (i_type ) ? 1 : 1 ;
48+ }
49+
4550i16 GetStackTopIndex (IM3Compilation o )
4651{
4752 return o -> stackIndex - 1 ;
@@ -77,7 +82,6 @@ u8 GetStackBottomType (IM3Compilation o, u16 i_offset)
7782}
7883
7984
80-
8185u8 GetBlockType (IM3Compilation o )
8286{
8387 return o -> block .type ;
@@ -154,10 +158,10 @@ void MarkSlotAllocated (IM3Compilation o, u16 i_slot)
154158}
155159
156160
157- bool AllocateSlot (IM3Compilation o , u16 * o_execSlot )
161+ bool AllocateSlots (IM3Compilation o , u16 * o_execSlot , u8 i_type )
158162{
159163 bool found = false;
160-
164+
161165 // search for empty slot in the execution stack
162166 i16 i = o -> firstSlotIndex ;
163167 while (i < d_m3MaxFunctionStackHeight )
@@ -194,11 +198,15 @@ M3Result IncrementSlotUsageCount (IM3Compilation o, u16 i_slot)
194198}
195199
196200
197- void DeallocateSlot (IM3Compilation o , i16 i_slotIndex )
201+ void DeallocateSlot (IM3Compilation o , i16 i_slotIndex , u8 i_type )
198202{ d_m3Assert (i_slotIndex >= o -> firstSlotIndex );
199203 d_m3Assert (o -> m3Slots [i_slotIndex ]);
200- if (-- o -> m3Slots [i_slotIndex ] == 0 )
201- o -> numAllocatedExecSlots -- ;
204+ for (u32 i = 0 ; i < GetTypeNumSlots (i_type ); ++ i , ++ i_slotIndex )
205+ {
206+ if (-- o -> m3Slots [i_slotIndex ] == 0 )
207+ o -> numAllocatedExecSlots -- ;
208+ }
209+
202210}
203211
204212
@@ -268,15 +276,15 @@ M3Result PreserveRegisterIfOccupied (IM3Compilation o, u8 i_registerType)
268276 {
269277 u16 stackIndex = GetRegisterStackIndex (o , regSelect );
270278 DeallocateRegister (o , regSelect );
279+
280+ u8 type = GetStackBottomType (o , stackIndex );
271281
272282 // and point to a exec slot
273283 u16 slot ;
274- if (AllocateSlot (o , & slot ))
284+ if (AllocateSlots (o , & slot , type ))
275285 {
276286 o -> wasmStack [stackIndex ] = slot ;
277287
278- u8 type = o -> typeStack [stackIndex ];
279-
280288_ (EmitOp (o , c_setSetOps [type ]));
281289 EmitSlotOffset (o , slot );
282290 }
@@ -328,7 +336,7 @@ _ (PreserveRegisterIfOccupied (o, c_m3Type_f64));
328336//----------------------------------------------------------------------------------------------------------------------
329337
330338
331- M3Result Push (IM3Compilation o , u8 i_m3Type , i16 i_location )
339+ M3Result Push (IM3Compilation o , u8 i_type , i16 i_location )
332340{
333341 M3Result result = m3Err_none ;
334342
@@ -344,7 +352,7 @@ M3Result Push (IM3Compilation o, u8 i_m3Type, i16 i_location)
344352 }
345353
346354 o -> wasmStack [stackIndex ] = i_location ;
347- o -> typeStack [stackIndex ] = i_m3Type ;
355+ o -> typeStack [stackIndex ] = i_type ;
348356
349357 if (IsRegisterLocation (i_location ))
350358 {
@@ -358,10 +366,10 @@ M3Result Push (IM3Compilation o, u8 i_m3Type, i16 i_location)
358366}
359367
360368
361- M3Result PushRegister (IM3Compilation o , u8 i_m3Type )
369+ M3Result PushRegister (IM3Compilation o , u8 i_type )
362370{
363- i16 location = IsFpType (i_m3Type ) ? d_m3Fp0SlotAlias : d_m3Reg0SlotAlias ; d_m3Assert (i_m3Type or IsStackPolymorphic (o ));
364- return Push (o , i_m3Type , location );
371+ i16 location = IsFpType (i_type ) ? d_m3Fp0SlotAlias : d_m3Reg0SlotAlias ; d_m3Assert (i_type or IsStackPolymorphic (o ));
372+ return Push (o , i_type , location );
365373}
366374
367375
@@ -374,6 +382,7 @@ M3Result Pop (IM3Compilation o)
374382 o -> stackIndex -- ; // printf ("pop: %d\n", (i32) o->stackIndex);
375383
376384 i16 location = o -> wasmStack [o -> stackIndex ];
385+ u8 type = o -> typeStack [o -> stackIndex ];
377386
378387 if (IsRegisterLocation (location ))
379388 {
@@ -382,7 +391,7 @@ M3Result Pop (IM3Compilation o)
382391 }
383392 else if (location >= o -> firstSlotIndex )
384393 {
385- DeallocateSlot (o , location );
394+ DeallocateSlot (o , location , type );
386395 }
387396
388397 m3logif (stack , dump_type_stack (o ))
@@ -414,15 +423,15 @@ _ (Pop (o));
414423}
415424
416425
417- M3Result _PushAllocatedSlotAndEmit (IM3Compilation o , u8 i_m3Type , bool i_doEmit )
426+ M3Result _PushAllocatedSlotAndEmit (IM3Compilation o , u8 i_type , bool i_doEmit )
418427{
419428 M3Result result = m3Err_none ;
420429
421430 u16 slot ;
422431
423- if (AllocateSlot (o , & slot ))
432+ if (AllocateSlots (o , & slot , i_type ))
424433 {
425- _ (Push (o , i_m3Type , slot ));
434+ _ (Push (o , i_type , slot ));
426435
427436 if (i_doEmit )
428437 EmitSlotOffset (o , slot );
@@ -433,19 +442,19 @@ _ (Push (o, i_m3Type, slot));
433442}
434443
435444
436- M3Result PushAllocatedSlotAndEmit (IM3Compilation o , u8 i_m3Type )
445+ M3Result PushAllocatedSlotAndEmit (IM3Compilation o , u8 i_type )
437446{
438- return _PushAllocatedSlotAndEmit (o , i_m3Type , true);
447+ return _PushAllocatedSlotAndEmit (o , i_type , true);
439448}
440449
441450
442- M3Result PushAllocatedSlot (IM3Compilation o , u8 i_m3Type )
451+ M3Result PushAllocatedSlot (IM3Compilation o , u8 i_type )
443452{
444- return _PushAllocatedSlotAndEmit (o , i_m3Type , false);
453+ return _PushAllocatedSlotAndEmit (o , i_type , false);
445454}
446455
447456
448- M3Result PushConst (IM3Compilation o , u64 i_word , u8 i_m3Type )
457+ M3Result PushConst (IM3Compilation o , u64 i_word , u8 i_type )
449458{
450459 M3Result result = m3Err_none ;
451460
@@ -459,7 +468,7 @@ M3Result PushConst (IM3Compilation o, u64 i_word, u8 i_m3Type)
459468 if (o -> constants [i ] == i_word )
460469 {
461470 location = o -> firstConstSlotIndex + i ;
462- _ (Push (o , i_m3Type , location ));
471+ _ (Push (o , i_type , location ));
463472 break ;
464473 }
465474 }
@@ -471,13 +480,13 @@ _ (Push (o, i_m3Type, location));
471480 o -> constants [numConstants ] = i_word ;
472481 location = o -> constSlotIndex ++ ;
473482
474- _ (Push (o , i_m3Type , location ));
483+ _ (Push (o , i_type , location ));
475484 }
476485 else
477486 {
478487_ (EmitOp (o , op_Const ));
479488 EmitConstant64 (o , i_word );
480- _ (PushAllocatedSlotAndEmit (o , i_m3Type ));
489+ _ (PushAllocatedSlotAndEmit (o , i_type ));
481490 }
482491 }
483492
@@ -686,7 +695,9 @@ M3Result FindReferencedLocalWithinCurrentBlock (IM3Compilation o, u16 * o_pres
686695 {
687696 if (* o_preservedSlotIndex == i_localIndex )
688697 {
689- if (not AllocateSlot (o , o_preservedSlotIndex ))
698+ u8 localType = GetStackBottomType (o , i_localIndex );
699+
700+ if (not AllocateSlots (o , o_preservedSlotIndex , localType ))
690701 _throw (m3Err_functionStackOverflow );
691702 }
692703 else
@@ -1239,7 +1250,7 @@ _ (ReadLEB_i7 (& reserved, & o->wasm, o->wasmEnd));
12391250
12401251_ (EmitOp (o , op_MemCurrent ));
12411252
1242- _ (PushRegister (o , c_m3Type_i32 )); // i32?
1253+ _ (PushRegister (o , c_m3Type_i32 ));
12431254
12441255 _catch : return result ;
12451256}
@@ -1257,7 +1268,7 @@ _ (Pop (o));
12571268
12581269_ (EmitOp (o , op_MemGrow ));
12591270
1260- _ (PushRegister (o , c_m3Type_i32 )); // i32?
1271+ _ (PushRegister (o , c_m3Type_i32 ));
12611272
12621273 _catch : return result ;
12631274}
@@ -1760,9 +1771,9 @@ const M3OpInfo c_operations [] =
17601771 M3OP ( "i32.div_u" , -1 , i_32 , d_binOpList (u32 , Divide ) ), // 0x6e
17611772 M3OP ( "i32.rem_s" , -1 , i_32 , d_binOpList (i32 , Remainder ) ), // 0x6f
17621773 M3OP ( "i32.rem_u" , -1 , i_32 , d_binOpList (u32 , Remainder ) ), // 0x70
1763- M3OP ( "i32.and" , -1 , i_32 , d_commutativeBinOpList (u64 , And ) ), // 0x71
1764- M3OP ( "i32.or" , -1 , i_32 , d_commutativeBinOpList (u64 , Or ) ), // 0x72
1765- M3OP ( "i32.xor" , -1 , i_32 , d_commutativeBinOpList (u64 , Xor ) ), // 0x73
1774+ M3OP ( "i32.and" , -1 , i_32 , d_commutativeBinOpList (u32 , And ) ), // 0x71
1775+ M3OP ( "i32.or" , -1 , i_32 , d_commutativeBinOpList (u32 , Or ) ), // 0x72
1776+ M3OP ( "i32.xor" , -1 , i_32 , d_commutativeBinOpList (u32 , Xor ) ), // 0x73
17661777 M3OP ( "i32.shl" , -1 , i_32 , d_binOpList (u32 , ShiftLeft ) ), // 0x74
17671778 M3OP ( "i32.shr_s" , -1 , i_32 , d_binOpList (i32 , ShiftRight ) ), // 0x75
17681779 M3OP ( "i32.shr_u" , -1 , i_32 , d_binOpList (u32 , ShiftRight ) ), // 0x76
0 commit comments