@@ -127,7 +127,12 @@ public void PushOrMergeInstruction(int lastLocal,in Instruction instruction, in
127127 switch ( opcode )
128128 {
129129 case OpCode . Move :
130- if ( lastInstruction . A != lastLocal && lastInstruction . A == instruction . B )
130+ //last A is not local variable
131+ if ( lastInstruction . A != lastLocal &&
132+ //available to merge
133+ lastInstruction . A == instruction . B &&
134+ //not already merged
135+ lastInstruction . A != lastInstruction . B )
131136 {
132137 switch ( lastInstruction . OpCode )
133138 {
@@ -150,15 +155,14 @@ public void PushOrMergeInstruction(int lastLocal,in Instruction instruction, in
150155 case OpCode . GetTable :
151156 {
152157 //Merge MOVE GetTable
153- if ( lastInstruction . OpCode == OpCode . Move && lastLocal != lastInstruction . A )
158+ if ( lastInstruction . OpCode == OpCode . Move && lastLocal != lastInstruction . A )
154159 {
155- var b = instruction . B ;
156- if ( instruction . A == b && lastInstruction . A == b )
157- {
158- lastInstruction = Instruction . GetTable ( instruction . A , lastInstruction . B , instruction . C ) ;
159- incrementStackPosition = false ;
160- return ;
161- }
160+ if ( lastInstruction . A == instruction . B )
161+ {
162+ lastInstruction = Instruction . GetTable ( instruction . A , lastInstruction . B , instruction . C ) ;
163+ incrementStackPosition = false ;
164+ return ;
165+ }
162166
163167 }
164168 break ;
@@ -181,6 +185,7 @@ public void PushOrMergeInstruction(int lastLocal,in Instruction instruction, in
181185 {
182186 last2Instruction = Instruction . SetTable ( ( byte ) ( lastB ) , instruction . B , last2Instruction . B ) ;
183187 instructions . RemoveAtSwapback ( instructions . Length - 1 ) ;
188+ instructionPositions . RemoveAtSwapback ( instructionPositions . Length - 1 ) ;
184189 incrementStackPosition = false ;
185190 return ;
186191 }
@@ -197,6 +202,24 @@ public void PushOrMergeInstruction(int lastLocal,in Instruction instruction, in
197202 return ;
198203 }
199204 }
205+ else if ( lastInstruction . OpCode == OpCode . GetTabUp && instructions . Length >= 2 )
206+ {
207+ ref var last2Instruction = ref instructions [ ^ 2 ] ;
208+ var last2OpCode = last2Instruction . OpCode ;
209+ if ( last2OpCode is OpCode . LoadK or OpCode . Move )
210+ {
211+
212+ var last2A = last2Instruction . A ;
213+ if ( last2A != lastLocal && instruction . C == last2A )
214+ {
215+ var c = last2OpCode == OpCode . LoadK ? last2Instruction . Bx + 256 : last2Instruction . B ;
216+ last2Instruction = lastInstruction ;
217+ lastInstruction = instruction with { C = ( ushort ) c } ;
218+ incrementStackPosition = false ;
219+ return ;
220+ }
221+ }
222+ }
200223 break ;
201224 }
202225 case OpCode . Unm :
@@ -209,6 +232,14 @@ public void PushOrMergeInstruction(int lastLocal,in Instruction instruction, in
209232 return ;
210233 }
211234 break ;
235+ case OpCode . Return :
236+ if ( lastInstruction . OpCode == OpCode . Move && instruction . B == 2 && lastInstruction . B < 256 )
237+ {
238+ lastInstruction = instruction with { A = ( byte ) lastInstruction . B } ;
239+ incrementStackPosition = false ;
240+ return ;
241+ }
242+ break ;
212243 }
213244
214245 instructions . Add ( instruction ) ;
0 commit comments