Add initial pass at constants and operands that reference labels

This commit is contained in:
Yoshi Askharoun
2024-02-11 11:51:59 -06:00
parent a0d5661a99
commit 03f8974ac1
7 changed files with 70 additions and 33 deletions
+1 -2
View File
@@ -314,8 +314,7 @@ internal class AsmMarkupLoader
ErrorManager.ReportError(line, column, error);
}
public void ReportError(string error, IAsmItem item)
=> ReportError(error, item.Line, item.Column);
public void ReportError(string error, IAsmItem item) => ReportError(error, item.Line, item.Column);
public void TrackImportedLoadResult(LoadResult loadResult)
{
+10 -10
View File
@@ -106,11 +106,11 @@ internal static class InstructionSet
("DBG", OpCode.EnterDebugState),
];
private static readonly OperandDataType[] Inst_UInt16 = [OperandDataType.UInt16];
private static readonly OperandDataType[] Inst_UInt32 = [OperandDataType.UInt32];
private static readonly OperandDataType[] Inst_UInt16x2 = [OperandDataType.UInt16, OperandDataType.UInt16];
private static readonly LiteralDataType[] Inst_UInt16 = [LiteralDataType.UInt16];
private static readonly LiteralDataType[] Inst_UInt32 = [LiteralDataType.UInt32];
private static readonly LiteralDataType[] Inst_UInt16x2 = [LiteralDataType.UInt16, LiteralDataType.UInt16];
public static readonly Dictionary<OpCode, OperandDataType[]> InstructionSchema = new()
public static readonly Dictionary<OpCode, LiteralDataType[]> InstructionSchema = new()
{
[OpCode.InitializeInstanceIndirect] = [],
[OpCode.PushNull] = [],
@@ -152,20 +152,20 @@ internal static class InstructionSet
[OpCode.JumpIfNullPeek] = Inst_UInt32,
[OpCode.Jump] = Inst_UInt32,
[OpCode.EnterDebugState] = [OperandDataType.Int32],
[OpCode.EnterDebugState] = [LiteralDataType.Int32],
[OpCode.ConstructObjectParam] = Inst_UInt16x2,
[OpCode.ConstructFromString] = Inst_UInt16x2,
[OpCode.PropertyDictionaryAdd] = Inst_UInt16x2,
[OpCode.ConvertType] = Inst_UInt16x2,
[OpCode.JumpIfDictionaryContains] = [OperandDataType.UInt16, OperandDataType.UInt16, OperandDataType.UInt32],
[OpCode.JumpIfDictionaryContains] = [LiteralDataType.UInt16, LiteralDataType.UInt16, LiteralDataType.UInt32],
[OpCode.ConstructFromBinary] = [OperandDataType.UInt16, OperandDataType.Bytes],
[OpCode.ConstructFromBinary] = [LiteralDataType.UInt16, LiteralDataType.Bytes],
[OpCode.Operation] = [OperandDataType.UInt16, OperandDataType.Byte],
[OpCode.Operation] = [LiteralDataType.UInt16, LiteralDataType.Byte],
[OpCode.Listen] = [OperandDataType.UInt16, OperandDataType.Byte, OperandDataType.UInt16, OperandDataType.UInt32],
[OpCode.DestructiveListen] = [OperandDataType.UInt16, OperandDataType.Byte, OperandDataType.UInt16, OperandDataType.UInt32, OperandDataType.UInt32],
[OpCode.Listen] = [LiteralDataType.UInt16, LiteralDataType.Byte, LiteralDataType.UInt16, LiteralDataType.UInt32],
[OpCode.DestructiveListen] = [LiteralDataType.UInt16, LiteralDataType.Byte, LiteralDataType.UInt16, LiteralDataType.UInt32, LiteralDataType.UInt32],
};
}
+6 -6
View File
@@ -44,7 +44,7 @@ partial class Lexer
}
else
{
List<Operand> operands = new();
List<OperandLiteral> operands = new();
var endOfInstructionResult = StatementEnd(input);
input = endOfInstructionResult.Remainder;
@@ -65,11 +65,11 @@ partial class Lexer
object operandValue = operandType switch
{
OperandDataType.Byte => byte.Parse(operandContent),
OperandDataType.UInt16 => ushort.Parse(operandContent),
OperandDataType.UInt32 => uint.Parse(operandContent),
OperandDataType.Int32 => int.Parse(operandContent),
OperandDataType.Bytes or _ => operandContent,
LiteralDataType.Byte => byte.Parse(operandContent),
LiteralDataType.UInt16 => ushort.Parse(operandContent),
LiteralDataType.UInt32 => uint.Parse(operandContent),
LiteralDataType.Int32 => int.Parse(operandContent),
LiteralDataType.Bytes or _ => operandContent,
};
operands.Add(new(operandValue, operandType, operandContent)
+14
View File
@@ -12,6 +12,20 @@ public record SectionDirective : Directive
public override string ToString() => $"{base.ToString()} {Name}";
}
public record ConstantDirective : Directive
{
public ConstantDirective(string name, string typeName, string content) : base("constant")
{
Name = name;
TypeName = typeName;
ValueString = content;
}
public string Name { get; }
public string TypeName { get; }
public string ValueString { get; }
}
public record ExportDirective : Directive
{
public ExportDirective(string labelPrefix, uint listenerCount, string baseTypeName) : base("export")
+1 -1
View File
@@ -37,7 +37,7 @@ public record Instruction(string Mnemonic, IEnumerable<Operand> Operands) : Code
if (operands.Length != schema.Length)
throw new ArgumentException($"{opCode} requires {schema.Length} operands, got {operands.Length}");
var operandModels = new Operand[operands.Length];
var operandModels = new OperandLiteral[operands.Length];
for (int i = 0; i < schema.Length; i++)
{
var operandValue = operands[i];
+38
View File
@@ -0,0 +1,38 @@
namespace Microsoft.Iris.Asm.Models;
public enum LiteralDataType : byte
{
Byte,
UInt16,
UInt32,
Int32,
Bytes,
}
public abstract record Operand(object Value, string Content = null) : AsmItem
{
public override string ToString() => Content ?? Value.ToString();
}
public record OperandLiteral : Operand
{
public OperandLiteral(object value, LiteralDataType dataType, string content = null) : base(value, content)
{
DataType = dataType;
}
public LiteralDataType DataType { get; init; }
public override string ToString() => base.ToString();
}
public record OperandReference : Operand
{
public OperandReference(string labelName) : base(labelName, labelName)
{
}
public string LabelName => Content;
public override string ToString() => base.ToString();
}
-14
View File
@@ -11,20 +11,6 @@ public record Label(string Name) : CodeItem
public override string ToString() => $"{Name}:";
}
public enum OperandDataType : byte
{
Byte,
UInt16,
UInt32,
Int32,
Bytes,
}
public record Operand(object Value, OperandDataType DataType, string Content = null) : AsmItem
{
public override string ToString() => Content ?? Value.ToString();
}
public record Program
{
public Program(IEnumerable<IBodyItem> body)