@@ -292,7 +292,8 @@ M3Result AllocateSlots (IM3Compilation o, u16 * o_slot, u8 i_type)
292292
293293M3Result AllocateConstantSlots (IM3Compilation o , u16 * o_slot , u8 i_type )
294294{
295- return AllocateSlotsWithinRange (o , o_slot , i_type , o -> slotFirstConstIndex , o -> slotFirstDynamicIndex );
295+ u16 maxTableIndex = o -> slotFirstConstIndex + d_m3MaxConstantTableSize ;
296+ return AllocateSlotsWithinRange (o , o_slot , i_type , o -> slotFirstConstIndex , M3_MIN (o -> slotFirstDynamicIndex , maxTableIndex ));
296297}
297298
298299
@@ -1116,10 +1117,10 @@ _ (Read_u8 (& opcode, & o->wasm, o->wasmEnd)); m3log (compile, d_i
11161117
11171118 //printf("Extended opcode: 0x%x\n", i_opcode);
11181119
1119- const M3OpInfo * opinfo = GetOpInfo (i_opcode );
1120- _throwif (m3Err_unknownOpcode , not opinfo );
1120+ IM3OpInfo opInfo = GetOpInfo (i_opcode );
1121+ _throwif (m3Err_unknownOpcode , not opInfo );
11211122
1122- M3Compiler compiler = opinfo -> compiler ;
1123+ M3Compiler compiler = opInfo -> compiler ;
11231124 _throwif (m3Err_noCompiler , not compiler );
11241125
11251126_ ((* compiler ) (o , i_opcode ));
@@ -2011,74 +2012,75 @@ M3Result Compile_Operator (IM3Compilation o, m3opcode_t i_opcode)
20112012{
20122013 M3Result result ;
20132014
2014- const M3OpInfo * op = GetOpInfo (i_opcode );
2015+ IM3OpInfo opInfo = GetOpInfo (i_opcode );
2016+ _throwif (m3Err_unknownOpcode , not opInfo );
20152017
2016- IM3Operation operation ;
2018+ IM3Operation op ;
20172019
20182020 // This preserve is for for FP compare operations.
20192021 // either need additional slot destination operations or the
20202022 // easy fix, move _r0 out of the way.
20212023 // moving out the way might be the optimal solution most often?
20222024 // otherwise, the _r0 reg can get buried down in the stack
20232025 // and be idle & wasted for a moment.
2024- if (IsFpType (GetStackTopType (o )) and IsIntType (op -> type ))
2026+ if (IsFpType (GetStackTopType (o )) and IsIntType (opInfo -> type ))
20252027 {
2026- _ (PreserveRegisterIfOccupied (o , op -> type ));
2028+ _ (PreserveRegisterIfOccupied (o , opInfo -> type ));
20272029 }
20282030
2029- if (op -> stackOffset == 0 )
2031+ if (opInfo -> stackOffset == 0 )
20302032 {
20312033 if (IsStackTopInRegister (o ))
20322034 {
2033- operation = op -> operations [0 ]; // _s
2035+ op = opInfo -> operations [0 ]; // _s
20342036 }
20352037 else
20362038 {
2037- _ (PreserveRegisterIfOccupied (o , op -> type ));
2038- operation = op -> operations [1 ]; // _r
2039+ _ (PreserveRegisterIfOccupied (o , opInfo -> type ));
2040+ op = opInfo -> operations [1 ]; // _r
20392041 }
20402042 }
20412043 else
20422044 {
20432045 if (IsStackTopInRegister (o ))
20442046 {
2045- operation = op -> operations [0 ]; // _rs
2047+ op = opInfo -> operations [0 ]; // _rs
20462048
20472049 if (IsStackTopMinus1InRegister (o ))
20482050 { d_m3Assert (i_opcode == 0x38 or i_opcode == 0x39 );
2049- operation = op -> operations [3 ]; // _rr for fp.store
2051+ op = opInfo -> operations [3 ]; // _rr for fp.store
20502052 }
20512053 }
20522054 else if (IsStackTopMinus1InRegister (o ))
20532055 {
2054- operation = op -> operations [1 ]; // _sr
2056+ op = opInfo -> operations [1 ]; // _sr
20552057
2056- if (not operation ) // must be commutative, then
2057- operation = op -> operations [0 ];
2058+ if (not op ) // must be commutative, then
2059+ op = opInfo -> operations [0 ];
20582060 }
20592061 else
20602062 {
2061- _ (PreserveRegisterIfOccupied (o , op -> type )); // _ss
2062- operation = op -> operations [2 ];
2063+ _ (PreserveRegisterIfOccupied (o , opInfo -> type )); // _ss
2064+ op = opInfo -> operations [2 ];
20632065 }
20642066 }
20652067
2066- if (operation )
2068+ if (op )
20672069 {
2068- _ (EmitOp (o , operation ));
2070+ _ (EmitOp (o , op ));
20692071
20702072_ (EmitSlotNumOfStackTopAndPop (o ));
20712073
2072- if (op -> stackOffset < 0 )
2074+ if (opInfo -> stackOffset < 0 )
20732075_ (EmitSlotNumOfStackTopAndPop (o ));
20742076
2075- if (op -> type != c_m3Type_none )
2076- _ (PushRegister (o , op -> type ));
2077+ if (opInfo -> type != c_m3Type_none )
2078+ _ (PushRegister (o , opInfo -> type ));
20772079 }
20782080 else
20792081 {
20802082# ifdef DEBUG
2081- result = ErrorCompile ("no operation found for opcode" , o , "'%s'" , op -> name );
2083+ result = ErrorCompile ("no operation found for opcode" , o , "'%s'" , opInfo -> name );
20822084# else
20832085 result = ErrorCompile ("no operation found for opcode" , o , "%x" , i_opcode );
20842086# endif
@@ -2093,7 +2095,9 @@ M3Result Compile_Convert (IM3Compilation o, m3opcode_t i_opcode)
20932095{
20942096 M3Result result = m3Err_none ;
20952097
2096- const M3OpInfo * opInfo = GetOpInfo (i_opcode );
2098+ _try {
2099+ IM3OpInfo opInfo = GetOpInfo (i_opcode );
2100+ _throwif (m3Err_unknownOpcode , not opInfo );
20972101
20982102 bool destInSlot = IsRegisterTypeAllocated (o , opInfo -> type );
20992103 bool sourceInSlot = IsStackTopInSlot (o );
@@ -2108,6 +2112,7 @@ _ (PushAllocatedSlotAndEmit (o, opInfo->type))
21082112 else
21092113_ (PushRegister (o , opInfo -> type ))
21102114
2115+ }
21112116 _catch : return result ;
21122117}
21132118
@@ -2122,9 +2127,10 @@ _try {
21222127_ (ReadLEB_u32 (& alignHint , & o -> wasm , o -> wasmEnd ));
21232128_ (ReadLEB_u32 (& memoryOffset , & o -> wasm , o -> wasmEnd ));
21242129 m3log (compile , d_indent " (offset = %d)" , get_indention_string (o ), memoryOffset );
2125- const M3OpInfo * op = GetOpInfo (i_opcode );
2130+ IM3OpInfo opInfo = GetOpInfo (i_opcode );
2131+ _throwif (m3Err_unknownOpcode , not opInfo );
21262132
2127- if (IsFpType (op -> type ))
2133+ if (IsFpType (opInfo -> type ))
21282134_ (PreserveRegisterIfOccupied (o , c_m3Type_f64 ));
21292135
21302136_ (Compile_Operator (o , i_opcode ));
@@ -2439,7 +2445,8 @@ const M3OpInfo c_operationsFC [] =
24392445# endif
24402446};
24412447
2442- const M3OpInfo * GetOpInfo (m3opcode_t opcode )
2448+
2449+ IM3OpInfo GetOpInfo (m3opcode_t opcode )
24432450{
24442451 switch (opcode >> 8 ) {
24452452 case 0x00 :
@@ -2480,7 +2487,7 @@ _ (Read_opcode (& opcode, & o->wasm, o->wasmEnd)); log_opco
24802487 }
24812488 }
24822489
2483- IM3OpInfo opinfo = GetOpInfo (opcode );
2490+ IM3OpInfo opinfo = GetOpInfo (opcode );
24842491
24852492 if (opinfo == NULL )
24862493 _throw (ErrorCompile (m3Err_unknownOpcode , o , "opcode '%x' not available" , opcode ));
@@ -2648,6 +2655,7 @@ M3Result CompileLocals (IM3Compilation o)
26482655{
26492656 M3Result result ;
26502657
2658+ u32 numLocals = 0 ;
26512659 u32 numLocalBlocks ;
26522660_ (ReadLEB_u32 (& numLocalBlocks , & o -> wasm , o -> wasmEnd ));
26532661
@@ -2660,11 +2668,14 @@ _ (ReadLEB_u32 (& numLocalBlocks, & o->wasm, o->wasmEnd));
26602668_ (ReadLEB_u32 (& varCount , & o -> wasm , o -> wasmEnd ));
26612669_ (ReadLEB_i7 (& waType , & o -> wasm , o -> wasmEnd ));
26622670_ (NormalizeType (& localType , waType ));
2663- m3log (compile , "pushing locals. count: %d; type: %s" , varCount , c_waTypes [localType ]);
2671+ numLocals += varCount ; m3log (compile , "pushing locals. count: %d; type: %s" , varCount , c_waTypes [localType ]);
26642672 while (varCount -- )
26652673_ (PushAllocatedSlot (o , localType ));
26662674 }
26672675
2676+ if (o -> function )
2677+ o -> function -> numLocals = numLocals ;
2678+
26682679 _catch : return result ;
26692680}
26702681
0 commit comments