Add offset and index to instructions and operands

This commit is contained in:
Yoshi Askharoun
2025-07-15 21:56:44 -05:00
parent 8b2a88a77c
commit 9495785efb
5 changed files with 61 additions and 73 deletions
+2 -45
View File
@@ -267,51 +267,8 @@ public class Disassembler
// Insert a label to mark the start of the object section.
yield return new SectionDirective("object");
while (reader.CurrentOffset < reader.Size)
{
if (_offsetLabelMap.TryGetValue(reader.CurrentOffset, out var labels))
foreach (var label in labels)
yield return label;
var opCode = (OpCode)reader.ReadByte();
var instSchema = InstructionSet.InstructionSchema[opCode];
Operand[] operands = new Operand[instSchema.Length];
for (int i = 0; i < instSchema.Length; i++)
{
var operandDataType = instSchema[i];
Operand operand;
if (operandDataType == LiteralDataType.ConstantIndex)
{
// Refer to the constant by name rather than index
var constantIndex = reader.ReadUInt16();
// TODO: Only generate name if not using a shared binary table
operand = new OperandReference($"const{constantIndex}");
}
else
{
object literalValue = OperandLiteral.ReduceDataType(operandDataType) switch
{
LiteralDataType.Byte => reader.ReadByte(),
LiteralDataType.UInt16 => reader.ReadUInt16(),
LiteralDataType.UInt32 => reader.ReadUInt32(),
LiteralDataType.Int32 => reader.ReadInt32(),
_ => throw new InvalidOperationException($"Unexpected operand data type '{operandDataType}'")
};
operand = new OperandLiteral(literalValue, operandDataType);
}
operands[i] = operand;
}
yield return new Instruction(opCode, operands);
}
yield break;
foreach (var codeItem in ObjectSection.Decode(reader, _offsetLabelMap))
yield return codeItem;
}
private QualifiedTypeName GetQualifiedName(TypeSchema schema)