Skip to content

Commit

Permalink
tables
Browse files Browse the repository at this point in the history
  • Loading branch information
atrexus committed Feb 12, 2024
1 parent 555653d commit 5e341c2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
22 changes: 17 additions & 5 deletions src/Unluau/Chunk/Luau/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public BasicBlock LiftBasicBlock(Stack stack, int startPc, int? endPc = null)
case OpCode.GETIMPORT:
case OpCode.NEWTABLE:
{
int aux = Instructions[++pc].Value;
int aux = Instructions[pc + 1].Value;

var value = instruction.Code switch
{
Expand All @@ -362,7 +362,7 @@ OpCode.LOADKX or

// The size of the table varies. If the auxiliary instruction contains a value that is not
// zero, then we use it. Otherwise we switch to the B operand.
OpCode.NEWTABLE => new Table(context, aux > 0 ? aux : (instruction.B > 0 ? (1 << (instruction.B - 1)) : 0)),
OpCode.NEWTABLE => new Table(context, aux > 0 ? Instructions[++pc].Value : (instruction.B > 0 ? (1 << (instruction.B - 1)) : 0)),
OpCode.DUPTABLE => ConstantToBasicValue(context, Constants[instruction.D]),

// We know this won't ever happen, but the C# compiler will cry if I don't add this.
Expand Down Expand Up @@ -531,9 +531,22 @@ OpCode.LOADKX or
}
case OpCode.SETLIST:
{
var table = (Table)stack.Get(instruction.D)!.Value;
var table = (Table)stack.Get(instruction.A)!.Value;

for (int i = 0; i < instruction.C - 1; ++i)
{
var entry = stack.Get(instruction.B + i)!;

table.Entries.Add(new()
{
Context = entry.Value.Context,
Value = new Reference(context, entry)
});
}

// Skip the auxiliary instruction
++pc;
break;
}
}
}
Expand Down Expand Up @@ -575,11 +588,10 @@ private BasicValue ConstantToBasicValue(Context context, Constant constant)
entries[i] = new()
{
Context = context,
Key = null,
Value = ConstantToBasicValue(context, tableConstant.Value[i])
};

return new Table(context, entries);
return new Table(context, entries);
}

throw new NotImplementedException();
Expand Down
2 changes: 1 addition & 1 deletion src/Unluau/IL/Statements/Instructions/LoadValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class LoadValue(Context context, Slot slot, BasicValue value) : Instructi
/// <summary>
/// The value to load into the provided register.
/// </summary>
public BasicValue Value { get; private set; } = value;
public BasicValue Value { get; set; } = value;

/// <summary>
/// Implements the recursive visitor.
Expand Down
6 changes: 3 additions & 3 deletions src/Unluau/IL/Values/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed record TableEntry
/// <summary>
/// Key to the entry.
/// </summary>
public required BasicValue? Key { get; set; }
public BasicValue? Key { get; set; }

/// <summary>
/// Value for the entry.
Expand All @@ -26,7 +26,7 @@ public sealed record TableEntry
/// Returns a string representation of the entry.
/// </summary>
/// <returns>String representation.</returns>
public override string ToString() => $"Entry({Key}, {Value})";
public override string ToString() => Key is null ? $"Entry({Value})" : $"Entry({Key}, {Value})";
}

/// <summary>
Expand Down Expand Up @@ -74,7 +74,7 @@ public override void Visit(Visitor visitor)
foreach (var entry in Entries)
{
entry.Value.Visit(visitor);
entry.Key.Visit(visitor);
entry.Key?.Visit(visitor);
}
}
}
Expand Down
25 changes: 22 additions & 3 deletions src/Unluau/IL/Visitors/ValueVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public override bool Visit(LoadValue node)
{
TryDelete(node, node.Slot);

node.Value = ResolveValue(node.Value);

return true;
}

Expand Down Expand Up @@ -138,8 +140,12 @@ private static BasicValue ResolveValue(BasicValue value)
{
if (value is Reference reference)
return ResolveReference(reference);

if (value is Values.Index index)
return ResolveIndex(index);

if (value is Table table)
ResolveTable(table);

return value;
}
Expand Down Expand Up @@ -168,7 +174,7 @@ private static BasicValue ResolveReference(Reference reference)
/// <summary>
/// Resolves an index operation.
/// </summary>
/// <param name="reference">The reference.</param>
/// <param name="index">The reference.</param>
/// <returns>The resolved index.</returns>
private static Values.Index ResolveIndex(Values.Index index)
{
Expand All @@ -178,6 +184,19 @@ private static Values.Index ResolveIndex(Values.Index index)
return index;
}

/// <summary>
/// Resolves a table reference.
/// </summary>
/// <param name="table">The reference.</param>
/// <returns>The resolved table value.</returns>
private static Table ResolveTable(Table table)
{
foreach (var entry in table.Entries)
entry.Value = ResolveValue(entry.Value);

return table;
}

/// <summary>
/// Resolves a call operation.
/// </summary>
Expand Down Expand Up @@ -220,8 +239,8 @@ private void VisitBlockChildren(BasicBlock block)

// We call .ToList() to copy the list so that there are no concurrency issues. In the end
// we do end up adding/removing items in the statements list.
foreach (var statement in block.Statements.ToList())
statement.Visit(this);
foreach (var statement in block.Statements.ToList())
statement.Visit(this);

_lastBlock = previousBlock;
}
Expand Down

0 comments on commit 5e341c2

Please sign in to comment.