Skip to content

Commit

Permalink
lingo: store the immediates
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulusParssinen committed Feb 23, 2024
1 parent d469e12 commit d150001
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
7 changes: 4 additions & 3 deletions Shockky.SourceGeneration.Tests/InstructionGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ namespace Shockky.Lingo.Instructions;
[global::System.CodeDom.Compiler.GeneratedCode("InstructionGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public sealed class PushInt : IInstruction
public sealed class PushInt(int immediate) : IInstruction
{
public OPCode OP => OPCode.PushInt;

public int Immediate { get; set; }
public int Immediate { get; set; } = immediate;

public int GetSize()
{
Expand Down Expand Up @@ -92,10 +92,11 @@ public static IInstruction Read(ref global::Shockky.IO.ShockwaveReader input)
_ => 0
};

if (op >= 0x80) op = (byte)(op & 0x3F | 0x40);
return (OPCode)op switch
{
OPCode.Return => new Return(),
OPCode.PushInt => new PushInt(),
OPCode.PushInt => new PushInt(immediate),
_ => throw null
};
}
Expand Down
19 changes: 15 additions & 4 deletions Shockky.SourceGeneration/InstructionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
""", true);
writer.WriteLine();
writer.WriteLine("if (op >= 0x80) op = (byte)(op & 0x3F | 0x40);");
using (writer.WriteBlock("return (OPCode)op switch", isExpression: true))
{
foreach (var instruction in item.AsSpan())
{
writer.WriteLine($"OPCode.{instruction.Name} => new {instruction.Name}(),");
bool hasImmediate = instruction.ImmediateKind is not ImmediateKind.None;
writer.Write($"OPCode.{instruction.Name} => new {instruction.Name}(");
writer.WriteIf(hasImmediate, "immediate");
writer.WriteLine("),");
}
writer.WriteLine("_ => throw null");
}
Expand Down Expand Up @@ -156,12 +161,18 @@ internal static bool TryGetInfo(
internal static void WriteInstructionSyntax(IndentedTextWriter writer,
InstructionInfo instruction)
{
bool hasImmediate = instruction.ImmediateKind is not ImmediateKind.None;

writer.WriteGeneratedAttributes(nameof(InstructionGenerator));

using (writer.WriteBlock($"public sealed class {instruction.Name} : IInstruction"))
if (hasImmediate)
{
bool hasImmediate = instruction.ImmediateKind is not ImmediateKind.None;
writer.WriteLine($"public sealed class {instruction.Name}(int immediate) : IInstruction");
}
else writer.WriteLine($"public sealed class {instruction.Name} : IInstruction");

using (writer.WriteBlock())
{
// Offer cached instance for instruction with no immediate
writer.WriteLineIf(!hasImmediate, $"public static readonly {instruction.Name} Default = new();");
writer.WriteLineIf(!hasImmediate);
Expand All @@ -174,7 +185,7 @@ internal static void WriteInstructionSyntax(IndentedTextWriter writer,
{
writer.WriteLine("int IInstruction.Immediate { get; set; }");
}
else writer.WriteLine("public int Immediate { get; set; }");
else writer.WriteLine("public int Immediate { get; set; } = immediate;");
writer.WriteLine();

if (hasImmediate)
Expand Down

0 comments on commit d150001

Please sign in to comment.