Skip to content

Commit

Permalink
Compiler: Always allocate 1 stack variable for the top level (<=V10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenkai committed May 7, 2023
1 parent 30156c8 commit 5c57574
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion GTAdhocToolchain.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private static void BuildScript(string inputPath, string output, int version = 1
var compiler = new AdhocScriptCompiler();
compiler.SetSourcePath(compiler.SymbolMap, inputPath);
compiler.SetVersion(version);
compiler.SetupStack();
compiler.CreateStack();

if (debugExceptions)
compiler.BuildTryCatchDebugStatements();
Expand Down
8 changes: 6 additions & 2 deletions GTAdhocToolchain.Compiler/AdhocScriptCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public void CompileScript(Script script)
{
Logger.Info("Started script compilation.");

// Always an empty one in old versions (same in subroutines)
if (Version <= 10)
Stack.AddLocalVariable(null);

EnterScope(this, script);
CompileScriptBody(this, script);
LeaveScope(this);
Expand Down Expand Up @@ -951,7 +955,7 @@ public void CompileSubroutine(AdhocCodeFrame frame, Node parentNode, Node body,
subroutine.CodeFrame.ParentFrame = this;
subroutine.CodeFrame.CurrentModule = frame.CurrentModule;
subroutine.CodeFrame.Version = this.Version;
subroutine.CodeFrame.SetupStack();
subroutine.CodeFrame.CreateStack();

if (isMethod)
{
Expand Down Expand Up @@ -1550,7 +1554,7 @@ private void CompileAnonymousSubroutine(AdhocCodeFrame frame, Node parentNode, N
subroutine.CodeFrame.SourceFilePath = frame.SourceFilePath;
subroutine.CodeFrame.CurrentModule = frame.CurrentModule;
subroutine.CodeFrame.Version = this.Version;
subroutine.CodeFrame.SetupStack();
subroutine.CodeFrame.CreateStack();

/* Unlike JS, adhoc can capture variables from the parent frame
* Example:
Expand Down
5 changes: 4 additions & 1 deletion GTAdhocToolchain.Core/AdhocCodeFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ public class AdhocCodeFrame
/// </summary>
public bool HasRestElement { get; set; }

public void SetupStack()
/// <summary>
/// Creates the stack (depending on the adhoc version).
/// </summary>
public void CreateStack()
{
if (Version >= 11)
Stack = new AdhocStack();
Expand Down
2 changes: 1 addition & 1 deletion GTAdhocToolchain.Core/Instructions/InsFunctionConst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override void Serialize(AdhocStream stream)
public override void Deserialize(AdhocStream stream)
{
CodeFrame.Version = stream.Version;
CodeFrame.SetupStack();
CodeFrame.CreateStack();
CodeFrame.Read(stream);
}

Expand Down
2 changes: 1 addition & 1 deletion GTAdhocToolchain.Core/Instructions/InsFunctionDefine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override void Deserialize(AdhocStream stream)
Name = stream.ReadSymbol();

CodeFrame.Version = stream.Version;
CodeFrame.SetupStack();
CodeFrame.CreateStack();
CodeFrame.Read(stream);
}

Expand Down
2 changes: 1 addition & 1 deletion GTAdhocToolchain.Core/Instructions/InsMethodConst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override void Serialize(AdhocStream stream)
public override void Deserialize(AdhocStream stream)
{
CodeFrame.Version = stream.Version;
CodeFrame.SetupStack();
CodeFrame.CreateStack();
CodeFrame.Read(stream);
}

Expand Down
2 changes: 1 addition & 1 deletion GTAdhocToolchain.Core/Instructions/InsMethodDefine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override void Deserialize(AdhocStream stream)
Name = stream.ReadSymbol();

CodeFrame.Version = stream.Version;
CodeFrame.SetupStack();
CodeFrame.CreateStack();
CodeFrame.Read(stream);
}

Expand Down
2 changes: 1 addition & 1 deletion GTAdhocToolchain.Disasm/AdhocFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static List<AdhocFile> ReadFromFile(string path)

adhoc.TopLevelFrame = new AdhocCodeFrame();
adhoc.TopLevelFrame.Version = version;
adhoc.TopLevelFrame.SetupStack();
adhoc.TopLevelFrame.CreateStack();
adhoc.TopLevelFrame.Read(stream);

scripts.Add(adhoc);
Expand Down
4 changes: 2 additions & 2 deletions GTAdhocToolchain.Project/AdhocProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public bool Build(bool debug = false)
compiler.SetProjectDirectory(ProjectDir);
compiler.SetSourcePath(compiler.SymbolMap, ProjectFolder + "/" + tmpFileName);
compiler.SetVersion(Version);
compiler.SetupStack();
compiler.CreateStack();

if (debug)
compiler.BuildTryCatchDebugStatements();
Expand Down Expand Up @@ -262,7 +262,7 @@ private void BuildPackageFile()
compiler.SetProjectDirectory(ProjectDir);
compiler.SetSourcePath(compiler.SymbolMap, srcFile.SourcePath);
compiler.SetVersion(Version);
compiler.SetupStack();
compiler.CreateStack();
compiler.CompileScript(program);

AdhocCodeGen codeGen = new AdhocCodeGen(compiler, compiler.SymbolMap);
Expand Down

0 comments on commit 5c57574

Please sign in to comment.