From 03f8974ac1d249b37d67ce6d1bf92f95964be77c Mon Sep 17 00:00:00 2001 From: Yoshi Askharoun Date: Sun, 11 Feb 2024 11:51:59 -0600 Subject: [PATCH] Add initial pass at constants and operands that reference labels --- libs/UIX.Asm/AsmMarkupLoader.cs | 3 +-- libs/UIX.Asm/InstructionSet.cs | 20 +++++++-------- libs/UIX.Asm/Lexer.Body.cs | 12 ++++----- libs/UIX.Asm/Models/Directives.cs | 14 +++++++++++ libs/UIX.Asm/Models/Instructions.cs | 2 +- libs/UIX.Asm/Models/Operands.cs | 38 +++++++++++++++++++++++++++++ libs/UIX.Asm/Models/SyntaxModels.cs | 14 ----------- 7 files changed, 70 insertions(+), 33 deletions(-) create mode 100644 libs/UIX.Asm/Models/Operands.cs diff --git a/libs/UIX.Asm/AsmMarkupLoader.cs b/libs/UIX.Asm/AsmMarkupLoader.cs index 76e9e68..13599be 100644 --- a/libs/UIX.Asm/AsmMarkupLoader.cs +++ b/libs/UIX.Asm/AsmMarkupLoader.cs @@ -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) { diff --git a/libs/UIX.Asm/InstructionSet.cs b/libs/UIX.Asm/InstructionSet.cs index 1caca91..1c61db3 100644 --- a/libs/UIX.Asm/InstructionSet.cs +++ b/libs/UIX.Asm/InstructionSet.cs @@ -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 InstructionSchema = new() + public static readonly Dictionary 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], }; } \ No newline at end of file diff --git a/libs/UIX.Asm/Lexer.Body.cs b/libs/UIX.Asm/Lexer.Body.cs index 074ec43..cdce252 100644 --- a/libs/UIX.Asm/Lexer.Body.cs +++ b/libs/UIX.Asm/Lexer.Body.cs @@ -44,7 +44,7 @@ partial class Lexer } else { - List operands = new(); + List 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) diff --git a/libs/UIX.Asm/Models/Directives.cs b/libs/UIX.Asm/Models/Directives.cs index dc58657..06b4ff4 100644 --- a/libs/UIX.Asm/Models/Directives.cs +++ b/libs/UIX.Asm/Models/Directives.cs @@ -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") diff --git a/libs/UIX.Asm/Models/Instructions.cs b/libs/UIX.Asm/Models/Instructions.cs index 0a1c7f2..bac2204 100644 --- a/libs/UIX.Asm/Models/Instructions.cs +++ b/libs/UIX.Asm/Models/Instructions.cs @@ -37,7 +37,7 @@ public record Instruction(string Mnemonic, IEnumerable 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]; diff --git a/libs/UIX.Asm/Models/Operands.cs b/libs/UIX.Asm/Models/Operands.cs new file mode 100644 index 0000000..7de78a5 --- /dev/null +++ b/libs/UIX.Asm/Models/Operands.cs @@ -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(); +} diff --git a/libs/UIX.Asm/Models/SyntaxModels.cs b/libs/UIX.Asm/Models/SyntaxModels.cs index 3ec8859..545da4c 100644 --- a/libs/UIX.Asm/Models/SyntaxModels.cs +++ b/libs/UIX.Asm/Models/SyntaxModels.cs @@ -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 body)