Skip to content

Commit

Permalink
Add work on IL.
Browse files Browse the repository at this point in the history
  • Loading branch information
atrexus committed Jan 24, 2024
1 parent eee591e commit b624809
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Unluau/Chunk/IChunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface IChunk
/// <summary>
/// Translates the current chunk to the IL.
/// </summary>
/// <returns></returns>
public Program Translate();
/// <returns>A block that wraps the IL instructions.</returns>
public BasicBlock Translate();
}
}
34 changes: 34 additions & 0 deletions src/Unluau/IL/BasicBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unluau.IL.Instructions;

namespace Unluau.IL
{
/// <summary>
/// Represents a basic block in the program.
/// </summary>
/// <param name="instructions">A list of instructions.</param>
public class BasicBlock(Instruction[] instructions) : Node
{
/// <summary>
/// The instructions within the block.
/// </summary>
public Instruction[] Instructions { get; set; } = instructions;

/// <summary>
/// Recursive visitor method.
/// </summary>
/// <param name="visitor">The visitor.</param>
public override void Visit(Visitor visitor)
{
if (visitor.Visit(this))
{
foreach (Instruction instruction in Instructions)
instruction.Visit(visitor);
}
}
}
}
43 changes: 43 additions & 0 deletions src/Unluau/IL/Closure.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Unluau.IL
{
/// <summary>
/// Provides information on parameters and contents of a closure.
/// </summary>
public struct ClosureContext
{
/// <summary>
/// Basic line and instruction information.
/// </summary>
public Context Context { get; set; }
}

/// <summary>
/// Represents a function (closure) within the code.
/// </summary>
public class Closure(ClosureContext context, BasicBlock[] blocks) : Node(context.Context)
{
/// <summary>
/// The children blocks of the closure. Each contain
/// </summary>
public BasicBlock[] Blocks { get; set; } = blocks;

/// <summary>
/// Visits the children of the closure.
/// </summary>
/// <param name="visitor">The visitor.</param>
public override void Visit(Visitor visitor)
{
if (visitor.Visit(this))
{
foreach (var block in Blocks)
block.Visit(visitor);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System.Text;
using System.Threading.Tasks;

namespace Unluau.IL
namespace Unluau.IL.Instructions
{
public class Program
public class Instruction : Node
{
}
}
45 changes: 45 additions & 0 deletions src/Unluau/IL/Node.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Unluau.IL
{
/// <summary>
/// Contains information about location.
/// </summary>
public struct Context
{
/// <summary>
/// The start and end instruction the node was translated from.
/// </summary>
public (int, int) PcScope { get; private set; }

/// <summary>
/// The line number from the original script.
/// </summary>
public int? Line { get; private set; }
}

/// <summary>
/// The base node for all IL components.
/// </summary>
/// <param name="context">Additional context on the node.</param>
public class Node(Context context)
{
/// <summary>
/// Provides additional context on the current node.
/// </summary>
public Context Context { get; private set; } = context;

/// <summary>
/// Recursive visitor.
/// </summary>
/// <param name="visitor">The visitor.</param>
public virtual void Visit(Visitor visitor)
{
return;
}
}
}
26 changes: 26 additions & 0 deletions src/Unluau/IL/Variable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Unluau.IL
{
public struct Variable
{
/// <summary>
/// The name of the variable.
/// </summary>
public string? Symbol { get; set; }

/// <summary>
/// The assigned register slot for the variable.
/// </summary>
public int Slot { get; set; }

/// <summary>
/// The
/// </summary>
public (int, int) PcScope { get; set; }
}
}
16 changes: 16 additions & 0 deletions src/Unluau/IL/Visitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unluau.IL.Instructions;

namespace Unluau.IL
{
public class Visitor
{
public virtual bool Visit(Node node) => true;

public virtual bool Visit(Instruction node) => Visit(node as Node);
}
}

0 comments on commit b624809

Please sign in to comment.