From 43fd6c41e03f50901ab21157645f5bdd9b1b98a1 Mon Sep 17 00:00:00 2001 From: Chris Anders Date: Thu, 3 Oct 2024 19:34:32 +0200 Subject: [PATCH 1/3] feat: Define Label --- .../Core/Parsing/Emiting/ByteCodeEmitter.cs | 2 +- .../Parsers/Assembler/AssemblyVisitor.cs | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/MimaSim/MimaSim/Core/Parsing/Emiting/ByteCodeEmitter.cs b/src/MimaSim/MimaSim/Core/Parsing/Emiting/ByteCodeEmitter.cs index e41b221b..625e71f0 100644 --- a/src/MimaSim/MimaSim/Core/Parsing/Emiting/ByteCodeEmitter.cs +++ b/src/MimaSim/MimaSim/Core/Parsing/Emiting/ByteCodeEmitter.cs @@ -73,7 +73,7 @@ public void MarkLabel(Label label) public byte[] ToArray() => _builder.ToArray(); - internal void CreateLabel(string name) + public void CreateLabel(string name) { _labels.Add(name, (byte)_builder.Length); } diff --git a/src/MimaSim/MimaSim/MIMA/Parsing/Parsers/Assembler/AssemblyVisitor.cs b/src/MimaSim/MimaSim/MIMA/Parsing/Parsers/Assembler/AssemblyVisitor.cs index 37bfb8d5..496ed8ef 100644 --- a/src/MimaSim/MimaSim/MIMA/Parsing/Parsers/Assembler/AssemblyVisitor.cs +++ b/src/MimaSim/MimaSim/MIMA/Parsing/Parsers/Assembler/AssemblyVisitor.cs @@ -19,7 +19,25 @@ public AssemblyVisitor() For(Visit); For(VisitLiteral, node => node.Value is ulong); For(VisitRegister, node => node.Value is Registers); - For(VisitAddress, op => op.Operator == "&"); + For(VisitAddress, op => op.Tag == "address"); + For(VisitLabelRef, op => op.Tag == "labelref"); + For(VisitLabel, op => op.Tag == "label"); + } + + private void VisitLabel(PostfixOperatorNode labelDef) + { + if (labelDef.Expr is NameNode nameNode) + { + _emitter.CreateLabel(nameNode.Token.Text.ToString()); + } + } + + private void VisitLabelRef(PrefixOperatorNode labelRef) + { + if (labelRef.Expr is NameNode nameNode) + { + //todo: emit 0 address and adjust later in GetRaw() + } } private void VisitAddress(PrefixOperatorNode obj) From 06ecca0b8f162182ffb6f77be1d5ef6af790e764 Mon Sep 17 00:00:00 2001 From: Chris Anders Date: Thu, 3 Oct 2024 19:39:37 +0200 Subject: [PATCH 2/3] feat: Add Hexadecimal Dissasembling Of Numbers --- src/MimaSim/MimaSim/MIMA/Instructions/LoadInstruction.cs | 2 +- .../MimaSim/MIMA/Instructions/Move/MovMemMemInstruction.cs | 2 +- .../MimaSim/MIMA/Instructions/Move/MovMemRegInstruction.cs | 2 +- .../MimaSim/MIMA/Instructions/Move/MovRegMemInstruction.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/MimaSim/MimaSim/MIMA/Instructions/LoadInstruction.cs b/src/MimaSim/MimaSim/MIMA/Instructions/LoadInstruction.cs index d6414a75..93919597 100644 --- a/src/MimaSim/MimaSim/MIMA/Instructions/LoadInstruction.cs +++ b/src/MimaSim/MimaSim/MIMA/Instructions/LoadInstruction.cs @@ -18,6 +18,6 @@ public bool Invoke(CPU cpu) public void Dissassemble(StringBuilder builder, Disassembler disassembler) { - builder.AppendLine($"load {disassembler.Fetch16()}"); + builder.AppendLine($"load 0x{disassembler.Fetch16():X2}"); } } \ No newline at end of file diff --git a/src/MimaSim/MimaSim/MIMA/Instructions/Move/MovMemMemInstruction.cs b/src/MimaSim/MimaSim/MIMA/Instructions/Move/MovMemMemInstruction.cs index 95f771a8..6c61d77c 100644 --- a/src/MimaSim/MimaSim/MIMA/Instructions/Move/MovMemMemInstruction.cs +++ b/src/MimaSim/MimaSim/MIMA/Instructions/Move/MovMemMemInstruction.cs @@ -25,6 +25,6 @@ public bool Invoke(CPU cpu) public void Dissassemble(StringBuilder builder, Disassembler disassembler) { - builder.AppendLine($"mov {disassembler.Fetch16()}, {disassembler.Fetch16()}"); + builder.AppendLine($"mov 0x{disassembler.Fetch16():X2}, 0x{disassembler.Fetch16():X2}"); } } \ No newline at end of file diff --git a/src/MimaSim/MimaSim/MIMA/Instructions/Move/MovMemRegInstruction.cs b/src/MimaSim/MimaSim/MIMA/Instructions/Move/MovMemRegInstruction.cs index 4bcb3040..11d83a7d 100644 --- a/src/MimaSim/MimaSim/MIMA/Instructions/Move/MovMemRegInstruction.cs +++ b/src/MimaSim/MimaSim/MIMA/Instructions/Move/MovMemRegInstruction.cs @@ -24,6 +24,6 @@ public bool Invoke(CPU cpu) public void Dissassemble(StringBuilder builder, Disassembler disassembler) { - builder.AppendLine($"mov {disassembler.Fetch16()}, {disassembler.FetchRegister()}"); + builder.AppendLine($"mov 0x{disassembler.Fetch16():X2}, {disassembler.FetchRegister()}"); } } \ No newline at end of file diff --git a/src/MimaSim/MimaSim/MIMA/Instructions/Move/MovRegMemInstruction.cs b/src/MimaSim/MimaSim/MIMA/Instructions/Move/MovRegMemInstruction.cs index 8f56b181..c1a4114a 100644 --- a/src/MimaSim/MimaSim/MIMA/Instructions/Move/MovRegMemInstruction.cs +++ b/src/MimaSim/MimaSim/MIMA/Instructions/Move/MovRegMemInstruction.cs @@ -23,6 +23,6 @@ public bool Invoke(CPU cpu) public void Dissassemble(StringBuilder builder, Disassembler disassembler) { - builder.AppendLine($"mov {disassembler.FetchRegister()}, {disassembler.Fetch16()}"); + builder.AppendLine($"mov {disassembler.FetchRegister()}, 0x{disassembler.Fetch16():X2}"); } } \ No newline at end of file From c907e14bbc56cd568fb0dca87497eb3324d741f9 Mon Sep 17 00:00:00 2001 From: Chris Anders Date: Sun, 6 Oct 2024 12:42:17 +0200 Subject: [PATCH 3/3] feat: Add Label Habdling In Emitter --- .../Core/Parsing/Emiting/ByteArrayBuilder.cs | 46 +++++++++++++++++++ .../Core/Parsing/Emiting/ByteCodeEmitter.cs | 35 ++++++++++---- .../MIMA/Visitors/HighParserVisitor.cs | 2 +- 3 files changed, 73 insertions(+), 10 deletions(-) diff --git a/src/MimaSim/MimaSim/Core/Parsing/Emiting/ByteArrayBuilder.cs b/src/MimaSim/MimaSim/Core/Parsing/Emiting/ByteArrayBuilder.cs index 30b37285..d78693ce 100644 --- a/src/MimaSim/MimaSim/Core/Parsing/Emiting/ByteArrayBuilder.cs +++ b/src/MimaSim/MimaSim/Core/Parsing/Emiting/ByteArrayBuilder.cs @@ -137,6 +137,52 @@ public void Dispose() _store.Dispose(); } + public void ReplaceAt(int index, byte value) + { + ValidateIndex(index); + var buffer = _store.GetBuffer(); + buffer[index] = value; + } + + public void ReplaceAt(int index, bool value) + { + ValidateIndex(index); + ReplaceAt(index, value ? streamTrue : streamFalse); + } + + public void ReplaceAt(int index, char value) + { + ValidateIndex(index); + ReplaceAt(index, (byte)value); + } + + public void ReplaceAt(int index, short value) + { + ValidateIndex(index); + var bytes = BitConverter.GetBytes(value); + Array.Copy(bytes, 0, _store.GetBuffer(), index, bytes.Length); + } + + public void ReplaceAt(int index, int value) + { + ValidateIndex(index); + var bytes = BitConverter.GetBytes(value); + Array.Copy(bytes, 0, _store.GetBuffer(), index, bytes.Length); + } + + public void ReplaceAt(int index, long value) + { + ValidateIndex(index); + var bytes = BitConverter.GetBytes(value); + Array.Copy(bytes, 0, _store.GetBuffer(), index, bytes.Length); + } + + private void ValidateIndex(int index) + { + if (index < 0 || index >= Length) + throw new ArgumentOutOfRangeException(nameof(index), "Index is out of range."); + } + public bool GetBool() { return _store.ReadByte() == streamTrue; diff --git a/src/MimaSim/MimaSim/Core/Parsing/Emiting/ByteCodeEmitter.cs b/src/MimaSim/MimaSim/Core/Parsing/Emiting/ByteCodeEmitter.cs index 625e71f0..1ffdbbdf 100644 --- a/src/MimaSim/MimaSim/Core/Parsing/Emiting/ByteCodeEmitter.cs +++ b/src/MimaSim/MimaSim/Core/Parsing/Emiting/ByteCodeEmitter.cs @@ -6,9 +6,8 @@ namespace MimaSim.Core.Parsing.Emiting; public class ByteCodeEmitter { private readonly ByteArrayBuilder _builder = new(); - - private readonly Dictionary _labels = new(); - private int _labelCount = 0; + private readonly Dictionary _labels = new(); + private readonly List _labelReferences = new(); public int Position => _builder.Length; public void Append(byte[] raw) @@ -18,7 +17,7 @@ public void Append(byte[] raw) public Label DefineLabel() { - return new Label(_labelCount++); + return new Label(_labels.Count); } public void EmitInstruction(OpCodes opcode) @@ -61,17 +60,35 @@ public void EmitRegister(Registers reg) _builder.Append((byte)reg); } - public byte GetLabel(string name) + public void AddLabelReference() { - return _labels[name]; + _labelReferences.Add((short)_builder.Length); + _builder.Append((short)0); } - public void MarkLabel(Label label) + private void ReplaceLabelReferences() { - _labels.Add("L" + label.LabelNum, (byte)_builder.Length); + foreach (var kvp in _labels) + { + int index = 0; + while (index < _labelReferences.Count) + { + if (_labelReferences[index] == kvp.Value) + { + _builder.ReplaceAt(index, kvp.Value); + } + index++; + } + } + + _labelReferences.Clear(); } - public byte[] ToArray() => _builder.ToArray(); + public byte[] ToArray() + { + ReplaceLabelReferences(); // Ensure labels are replaced before converting to array + return _builder.ToArray(); + } public void CreateLabel(string name) { diff --git a/src/MimaSim/MimaSim/MIMA/Visitors/HighParserVisitor.cs b/src/MimaSim/MimaSim/MIMA/Visitors/HighParserVisitor.cs index 6ade6ad9..6c3b997a 100644 --- a/src/MimaSim/MimaSim/MIMA/Visitors/HighParserVisitor.cs +++ b/src/MimaSim/MimaSim/MIMA/Visitors/HighParserVisitor.cs @@ -261,7 +261,7 @@ private void VisitIfStatement(CallNode call) private void VisitLoopStatement(CallNode call) { var label = _emitter.DefineLabel(); - _emitter.MarkLabel(label); + // _emitter.MarkLabel(label); Visit((CallNode)call.Args.First());