Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
atrexus committed May 7, 2024
1 parent d7beb40 commit f56939c
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Unluau.Common/IR/ProtoTypes/Instructions/GetTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Represents a get table instruction.
/// </summary>
public class GetTable(uint value) : Instruction(value)
public class GetTable(uint value) : InstructionABC(value)
{
/// <inheritdoc/>
public override void Accept(Visitor visitor)
Expand Down
14 changes: 14 additions & 0 deletions src/Unluau.Common/IR/ProtoTypes/Instructions/GetTableKS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Unluau.Common.IR.ProtoTypes.Instructions
{
/// <summary>
/// Represents a get table instruction with a constant key.
/// </summary>
public class GetTableKS(ulong value) : InstructionABC(value)
{
/// <inheritdoc/>
public override void Accept(Visitor visitor)
{
visitor.Visit(this);
}
}
}
11 changes: 11 additions & 0 deletions src/Unluau.Common/IR/ProtoTypes/Instructions/GetTableN.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Unluau.Common.IR.ProtoTypes.Instructions
{
public class GetTableN(uint value) : InstructionABC(value)
{
/// <inheritdoc/>
public override void Accept(Visitor visitor)
{
visitor.Visit(this);
}
}
}
98 changes: 98 additions & 0 deletions src/Unluau.Common/IR/ProtoTypes/Instructions/Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,104 @@ public enum OpCode
/// C: index register.
/// </remarks>
GetTable,

/// <summary>
/// Store source register into table using key from register
/// </summary>
/// <remarks>
/// A: source register.
/// B: table register.
/// C: index register.
/// </remarks>
SetTable,

/// <summary>
/// Load value from table into target register using constant string as a key
/// </summary>
/// <remarks>
/// A: target register.
/// B: table register.
/// C: predicted slot index (based on hash).
/// AUX: constant table index.
/// </remarks>
GetTableKS,

/// <summary>
/// Store source register into table using constant string as a key
/// </summary>
/// <remarks>
/// A: source register.
/// B: table register.
/// C: predicted slot index (based on hash).
/// AUX: constant table index.
/// </remarks>
SetTableKS,

/// <summary>
/// Load value from table into target register using small integer index as a key
/// </summary>
/// <remarks>
/// A: target register.
/// B: table register.
/// C: index-1 (index is 1..256).
/// </remarks>
GetTableN,

NewClosure,
NameCall,
Call,
Return,
Jump,
JumpBack,
JumpIf,
JumpIfNot,
JumpIfEq,
JumpIfLe,
JumpIfLt,
JumpIfNotEq,
JumpIfNotLe,
JumpIfNotLt,

Add,
Sub,
Mul,
Div,
Mod,
Pow,

AddK,
SubK,
MulK,
DivK,
ModK,
PowK,

And,
Or,

AndK,
OrK,

Concat,

Not,
Minus,
Len,

NewTable,
DupTable,
SetList,
ForNPrep,
ForNLoop,
ForGPrep,
ForGLoop,

ForGPrepINext,

// Deprecated, removed in v3
_ForGLoopINext,

ForGPrepNext,
}

/// <summary>
Expand Down
14 changes: 14 additions & 0 deletions src/Unluau.Common/IR/ProtoTypes/Instructions/SetTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Unluau.Common.IR.ProtoTypes.Instructions
{
/// <summary>
/// Represents the `settable` instruction.
/// </summary>
public class SetTable(uint value) : InstructionABC(value)
{
/// <inheritdoc/>
public override void Accept(Visitor visitor)
{
visitor.Visit(this);
}
}
}
14 changes: 14 additions & 0 deletions src/Unluau.Common/IR/ProtoTypes/Instructions/SetTableKS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Unluau.Common.IR.ProtoTypes.Instructions
{
/// <summary>
/// Represents a set table instruction with a constant key.
/// </summary>
public class SetTableKS(ulong value) : InstructionABC(value)
{
/// <inheritdoc/>
public override void Accept(Visitor visitor)
{
visitor.Visit(this);
}
}
}
4 changes: 4 additions & 0 deletions src/Unluau.Common/IR/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ public Instruction ReadInstruction()
OpCode.CloseUpvalues => new CloseUpvalues(ReadUInt32()),
OpCode.GetImport => new GetImport(ReadUInt64()),
OpCode.GetTable => new GetTable(ReadUInt32()),
OpCode.SetTable => new SetTable(ReadUInt32()),
OpCode.GetTableKS => new GetTableKS(ReadUInt64()),
OpCode.SetTableKS => new SetTableKS(ReadUInt64()),
OpCode.GetTableN => new GetTableN(ReadUInt32()),
_ => throw new Exception($"invalid opcode {code}")
};
}
Expand Down
25 changes: 25 additions & 0 deletions src/Unluau.Common/IR/Visitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,30 @@ public abstract class Visitor
/// The visitor for the <see cref="GetImport"/> class.
/// </summary>
public virtual bool Visit(GetImport instruction) => Visit(instruction as InstructionAD);

/// <summary>
/// The visitor for the <see cref="GetTable"/> class.
/// </summary>
public virtual bool Visit(GetTable instruction) => Visit(instruction as InstructionABC);

/// <summary>
/// The visitor for the <see cref="SetTable"/> class.
/// </summary>
public virtual bool Visit(SetTable instruction) => Visit(instruction as InstructionABC);

/// <summary>
/// The visitor for the <see cref="GetTableKS"/> class.
/// </summary>
public virtual bool Visit(GetTableKS instruction) => Visit(instruction as InstructionABC);

/// <summary>
/// The visitor for the <see cref="SetTableKS"/> class.
/// </summary>
public virtual bool Visit(SetTableKS instruction) => Visit(instruction as InstructionABC);

/// <summary>
/// The visitor for the <see cref="GetTableN"/> class.
/// </summary>
public virtual bool Visit(GetTableN instruction) => Visit(instruction as InstructionABC);
}
}

0 comments on commit f56939c

Please sign in to comment.