Skip to content

Commit

Permalink
Debugging info, misc fixes & more program args
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenkai committed Feb 1, 2024
1 parent 032735f commit 0f07977
Show file tree
Hide file tree
Showing 11 changed files with 543 additions and 149 deletions.
29 changes: 25 additions & 4 deletions XenoTools.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ internal class Program
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

public const string Version = "0.2.0";

static void Main(string[] args)
{
Console.WriteLine("-----------------------------------------");
Console.WriteLine($"- XenoTools by Nenkai");
Console.WriteLine($"- XenoTools {Version} by Nenkai");
Console.WriteLine("-----------------------------------------");
Console.WriteLine("- https://github.com/Nenkai");
Console.WriteLine("-----------------------------------------");
Expand Down Expand Up @@ -122,15 +124,26 @@ public static void CompileScript(CompileScriptVerbs compileScriptVerbs)
return;
}

var compiler = new ScriptCompiler(compileScriptVerbs.InputPath);
if (compileScriptVerbs.Release)
Logger.Info("Compiling as release - debug information will be removed from the script");

if (compileScriptVerbs.DebugPrintFunctions)
Logger.Info("NOTE: Functions will have deb::put inserted");

var compiler = new ScriptCompiler(compileScriptVerbs.InputPath,
withDebugInformation: !compileScriptVerbs.Release,
debugPrintFunctions: compileScriptVerbs.DebugPrintFunctions);

var state = compiler.Compile(program);
PrintCompiledState(state);

Logger.Info("Compiled, generating script file..");
using (var outp = new FileStream(compileScriptVerbs.OutputPath, FileMode.Create))
{
var gen = new ScriptCodeGen(state);
gen.Write(outp);

Logger.Info($"Generated. File size: {outp.Position} bytes.");
}

Logger.Info($"Script build successful.");
Expand All @@ -157,7 +170,6 @@ public static void CompileScript(CompileScriptVerbs compileScriptVerbs)

Logger.Error("Script build failed.");
Environment.ExitCode = -1;

}

public static void Disassemble(DisassembleScriptVerbs verbs)
Expand All @@ -172,7 +184,7 @@ public static void Disassemble(DisassembleScriptVerbs verbs)
var ogFile = File.ReadAllBytes(verbs.InputPath);
var ogScript = new ScriptFile();
ogScript.Read(ogFile);
ogScript.Disassemble(verbs.OutputPath);
ogScript.Disassemble(verbs.OutputPath, verbs.CompareMode);

Environment.ExitCode = 0;
}
Expand Down Expand Up @@ -216,6 +228,12 @@ public class CompileScriptVerbs

[Option('b', "base-include-folder", Required = false, HelpText = "Set the root path for #include statements.")]
public string BaseIncludeFolder { get; set; }

[Option("release", Required = false, HelpText = "Compiles as release - no debug information will be emitted to the script file.")]
public bool Release { get; set; }

[Option("debug-print-functions", Required = false, HelpText = "Inserts deb::put(<function>) name at the start of every function for debugging.")]
public bool DebugPrintFunctions { get; set; }
}

[Verb("script-disassemble", HelpText = "Disassembles a script file (.sb).")]
Expand All @@ -226,6 +244,9 @@ public class DisassembleScriptVerbs

[Option('o', "output", Required = true, HelpText = "Output dissasembled file.")]
public string OutputPath { get; set; }

[Option("compare-mode", HelpText = "Whether to use compare mode - removes offsets/source files from disassembly, useful for matching instructions.")]
public bool CompareMode { get; set; }
}

[Verb("bdat-to-sqlite", HelpText = "Export bdats to a SQLite files.")]
Expand Down
2 changes: 2 additions & 0 deletions XenoTools.CLI/XenoTools.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyVersion>0.2.0.0</AssemblyVersion>
<FileVersion>0.2.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions XenoTools.Script/Compiler/CompiledScriptState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using XenoTools.Script.Entities;
using XenoTools.Script.Entities.Debugging;

namespace XenoTools.Script.Compiler;

Expand All @@ -22,4 +23,5 @@ public class CompiledScriptState
public Dictionary<string, ObjectConstructor> OCPool { get; init; }
public List<object> FuncImports { get; init; }
public List<SystemAttribute> SystemAttributes { get; init; }
public DebugInfo DebugInfo { get; init; }
}
30 changes: 28 additions & 2 deletions XenoTools.Script/Compiler/ScriptCodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public void Write(Stream stream)
WriteStaticPool(bs);
WriteLocalPool(bs);
WriteSystemAttributes(bs);

if (_compiledState.DebugInfo is not null)
WriteDebugInfo(bs);
}

private void WriteCode(BinaryStream bs)
Expand Down Expand Up @@ -402,6 +405,12 @@ private void WriteStaticPool(BinaryStream bs)
{
bs.WriteSingle((float)var.Value);
}
else if (var.Type == LocalType.True || var.Type == LocalType.False || var.Type == LocalType.Nil)
{
bs.WriteUInt32(0);
}
else
throw new NotSupportedException();

// This is directly copied to the stack so there's an extra 4 bytes (64 bit ptr on switch)
bs.WriteUInt32(0);
Expand Down Expand Up @@ -443,9 +452,9 @@ private void WriteLocalPool(BinaryStream bs)
StackLocals stackLocals = _compiledState.LocalPool[i];
bs.Position = lastOffset;
bs.WriteUInt32(0x08);
bs.WriteUInt32((uint)stackLocals.NamedLocals.Count);
bs.WriteUInt32((uint)stackLocals.Locals.Count);

foreach (VmVariable var in stackLocals.NamedLocals.Values)
foreach (VmVariable var in stackLocals.Locals)
{
bs.WriteByte((byte)var.Type);
bs.WriteByte(0);
Expand Down Expand Up @@ -499,4 +508,21 @@ private void WriteSystemAttributes(BinaryStream bs)
bs.Position = lastOffset;
bs.Align(0x04, grow: true);
}

private void WriteDebugInfo(BinaryStream bs)
{
long tableOffset = bs.Position;

_compiledState.DebugInfo.Write(bs);
int lastOffset = (int)bs.Position;

// Fill header
bs.Position = 0x3C;
bs.WriteUInt32((uint)tableOffset);

// Move to end.
bs.Position = lastOffset;
bs.Align(0x04, grow: true);
}

}
Loading

0 comments on commit 0f07977

Please sign in to comment.