Skip to content

Commit 8feb3c6

Browse files
committed
Processor: Don't re-fetch currentFrame for instructions guaranteed not to modify
1 parent b8ccc19 commit 8feb3c6

1 file changed

Lines changed: 31 additions & 30 deletions

File tree

src/WattleScript.Interpreter/Execution/VM/Processor/Processor_InstructionLoop.cs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ private DynValue Processing_Loop(int instructionPtr, bool canAwait = false)
3434

3535
try
3636
{
37+
new_frame:
38+
ref var currentFrame = ref m_ExecutionStack.Peek();
39+
3740
while (true)
3841
{
3942
//TODO: Hoist outside of while loop, update any instruction that
4043
//can change frame to jump to before loop when changing frame
41-
ref var currentFrame = ref m_ExecutionStack.Peek();
42-
4344
Instruction i = currentFrame.Function.code[instructionPtr];
4445
int currentPtr = instructionPtr;
4546
if (m_Debug.DebuggerAttached != null)
@@ -121,69 +122,69 @@ private DynValue Processing_Loop(int instructionPtr, bool canAwait = false)
121122
case OpCode.Add:
122123
instructionPtr = ExecAdd(instructionPtr);
123124
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
124-
break;
125+
goto new_frame;
125126
case OpCode.AddStr:
126127
instructionPtr = ExecAddStr(instructionPtr);
127128
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
128-
break;
129+
goto new_frame;
129130
case OpCode.Concat:
130131
instructionPtr = ExecConcat(instructionPtr);
131132
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
132-
break;
133+
goto new_frame;
133134
case OpCode.Neg:
134135
instructionPtr = ExecNeg(instructionPtr);
135136
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
136-
break;
137+
goto new_frame;
137138
case OpCode.Sub:
138139
instructionPtr = ExecSub(instructionPtr);
139140
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
140-
break;
141+
goto new_frame;
141142
case OpCode.Mul:
142143
instructionPtr = ExecMul(instructionPtr);
143144
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
144-
break;
145+
goto new_frame;
145146
case OpCode.Div:
146147
instructionPtr = ExecDiv(instructionPtr);
147148
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
148-
break;
149+
goto new_frame;
149150
case OpCode.Mod:
150151
instructionPtr = ExecMod(instructionPtr);
151152
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
152-
break;
153+
goto new_frame;
153154
case OpCode.Power:
154155
instructionPtr = ExecPower(instructionPtr);
155156
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
156-
break;
157+
goto new_frame;
157158
case OpCode.Eq:
158159
instructionPtr = ExecEq(instructionPtr, i);
159160
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
160-
break;
161+
goto new_frame;
161162
case OpCode.LessEq:
162163
instructionPtr = ExecLessEq(instructionPtr, i);
163164
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
164-
break;
165+
goto new_frame;
165166
case OpCode.Less:
166167
instructionPtr = ExecLess(instructionPtr, i);
167168
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
168-
break;
169+
goto new_frame;
169170
case OpCode.Len:
170171
instructionPtr = ExecLen(instructionPtr);
171172
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
172-
break;
173+
goto new_frame;
173174
case OpCode.Switch:
174175
instructionPtr = ExecSwitch(currentPtr, i, ref currentFrame);
175-
break;
176+
goto new_frame;
176177
case OpCode.Call:
177178
case OpCode.ThisCall:
178179
instructionPtr = Internal_ExecCall(canAwait, i.NumVal, instructionPtr, null, null, i.OpCode == OpCode.ThisCall, GetString(i.NumVal2));
179180
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
180181
if (instructionPtr == YIELD_SPECIAL_AWAIT) goto yield_to_await;
181-
break;
182+
goto new_frame;
182183
case OpCode.NewCall:
183184
instructionPtr = ExecNewCall(i, instructionPtr, canAwait);
184185
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
185186
if (instructionPtr == YIELD_SPECIAL_AWAIT) goto yield_to_await;
186-
break;
187+
goto new_frame;
187188
case OpCode.Scalar:
188189
m_ValueStack.Push(m_ValueStack.Pop().ToScalar());
189190
break;
@@ -209,7 +210,7 @@ private DynValue Processing_Loop(int instructionPtr, bool canAwait = false)
209210
case OpCode.JtOrPop:
210211
instructionPtr = ExecShortCircuitingOperator(i, instructionPtr);
211212
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
212-
break;
213+
goto new_frame;
213214
case OpCode.JNil:
214215
{
215216
DynValue v = m_ValueStack.Pop().ToScalar();
@@ -218,25 +219,25 @@ private DynValue Processing_Loop(int instructionPtr, bool canAwait = false)
218219
instructionPtr = i.NumVal;
219220
}
220221
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
221-
break;
222+
goto new_frame;
222223
case OpCode.JNilChk:
223224
{
224225
if(m_ValueStack.Peek().IsNil())
225226
instructionPtr = i.NumVal;
226227
}
227-
break;
228+
goto new_frame;
228229
case OpCode.Jf:
229230
instructionPtr = JumpBool(i, false, instructionPtr);
230231
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
231-
break;
232+
goto new_frame;
232233
case OpCode.Jt:
233234
instructionPtr = JumpBool(i, true, instructionPtr);
234235
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
235-
break;
236+
goto new_frame;
236237
case OpCode.Jump:
237238
instructionPtr = i.NumVal;
238239
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
239-
break;
240+
goto new_frame;
240241
case OpCode.MkTuple:
241242
ExecMkTuple(i);
242243
break;
@@ -263,14 +264,14 @@ private DynValue Processing_Loop(int instructionPtr, bool canAwait = false)
263264
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
264265
if (instructionPtr < 0)
265266
goto return_to_native_code;
266-
break;
267+
goto new_frame;
267268
case OpCode.Incr:
268269
ExecIncr(i);
269270
break;
270271
case OpCode.JFor:
271272
instructionPtr = ExecJFor(i, instructionPtr);
272273
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
273-
break;
274+
goto new_frame;
274275
case OpCode.TabProps:
275276
{
276277
ref var top = ref m_ValueStack.Peek();
@@ -368,21 +369,21 @@ private DynValue Processing_Loop(int instructionPtr, bool canAwait = false)
368369
case OpCode.IndexL:
369370
instructionPtr = ExecIndex(i, instructionPtr);
370371
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
371-
break;
372+
goto new_frame;
372373
case OpCode.IndexSet:
373374
case OpCode.IndexSetN:
374375
case OpCode.IndexSetL:
375376
instructionPtr = ExecIndexSet(i, instructionPtr);
376377
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
377-
break;
378+
goto new_frame;
378379
case OpCode.NilCoalescing:
379380
instructionPtr = ExecNilCoalescingAssignment( instructionPtr);
380381
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
381-
break;
382+
goto new_frame;
382383
case OpCode.NilCoalescingInverse:
383384
instructionPtr = ExecNilCoalescingAssignmentInverse(instructionPtr);
384385
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
385-
break;
386+
goto new_frame;
386387
case OpCode.JLclInit:
387388
if(m_ValueStack[m_ExecutionStack.Peek().BasePointer + i.NumVal2].IsNotNil())
388389
instructionPtr = i.NumVal;

0 commit comments

Comments
 (0)