Skip to content

Commit

Permalink
Compiler: Apply module rewind index to all leaves of top frame
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenkai committed Jun 12, 2023
1 parent 7d1e007 commit 5e23624
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions GTAdhocToolchain.Compiler/AdhocScriptCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ public void CompileReturnStatement(AdhocCodeFrame frame, ReturnStatement retStat
InsertSetState(frame, AdhocRunState.RETURN);

// Top level of frame?
if (frame.IsTopLevel)
if (frame.IsCurrentScopeTopScope)
frame.HasTopLevelReturnValue = true;
}

Expand Down Expand Up @@ -2981,10 +2981,14 @@ private void LeaveLoop(AdhocCodeFrame frame)
{
InsLeaveScope leave = new InsLeaveScope();

if (isModuleLeave)
{
// Top level is a special "module" frame where it can rewind some variables based on depth
// GT5 uses this and every leave in the top level requires this, GT6 and above ignores this altogether.

if (frame.ParentFrame is null) // Short for "do we have a parent? if not, we're most likely the top level"
leave.ModuleOrClassDepthRewindIndex = ModuleStack.Count - 1;

if (isModuleLeave)
{
if (isModuleExitFromSubroutine)
leave.VariableStorageRewindIndex = frame.Stack.GetLastLocalVariableIndex();
else
Expand Down
2 changes: 1 addition & 1 deletion GTAdhocToolchain.Core/AdhocCodeFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class AdhocCodeFrame
/// <summary>
/// Returns whether the current scope is the top block level.
/// </summary>
public bool IsTopLevel => CurrentScopes.Count == 1;
public bool IsCurrentScopeTopScope => CurrentScopes.Count == 1;

/// <summary>
/// Whether the current block has a return statement, to manually add the return instructions if false.
Expand Down

0 comments on commit 5e23624

Please sign in to comment.