Working on the assembler.

This commit is contained in:
2025-07-19 21:41:04 +08:00
parent d5b2e93117
commit 41bf6f1b25
3 changed files with 66 additions and 17 deletions

View File

@@ -1,5 +1,8 @@
using LibCLCC.NET.Lexer; using LibCLCC.NET.Lexer;
using LibCLCC.NET.Operations;
using SVM.Core;
using System; using System;
using System.Collections.Generic;
namespace SVM.Assembler.Core namespace SVM.Assembler.Core
{ {
@@ -19,7 +22,10 @@ InstSD sd
Register \${D}+ Register \${D}+
LabelCode \.code\: LabelCode \.code\:
LabelData \.data\: LabelData \.data\:
\.const\:
string "".*"" string "".*""
word \w*
GenericLabel {word}\:
LineEnd \n LineEnd \n
OpAdd add OpAdd add
@@ -42,11 +48,54 @@ OpSub BOp
OpMul BOp OpMul BOp
OpDiv BOp OpDiv BOp
LineEnd LE LineEnd LE
GenericLabel LblG
LabelCode InternalLbl
LabelData InternalLbl
LabelConstant InternalLbl
"; ";
LexerDefinition? definition; LexerDefinition? definition;
public Assembler() public Assembler()
{ {
LexerDefinition.TryParse(LexDefinition, out definition); if (LexerDefinition.TryParse(LexDefinition, out definition))
{
definition.Substitute();
}
}
public OperationResult<IntermediateObject> Assemble(string input, string ID = "main.asm")
{
StringLexer lexer = new StringLexer();
lexer.SetDefinition(definition);
lexer.Content = input;
lexer.SourceID = ID;
OperationResult<IntermediateObject> operationResult = new OperationResult<IntermediateObject>();
while (true)
{
var lexResult = lexer.Lex();
if (lexResult.Result == null) break;
var r = lexResult.Result;
switch (r.LexSegmentId)
{
case "InternalLbl":
break;
default:
break;
}
}
return operationResult;
} }
} }
[Serializable]
public class IntermediateObject
{
public Dictionary<string, string> data = new Dictionary<string, string>();
public List<IntermediateInstruction> instructions = new List<IntermediateInstruction>();
}
[Serializable]
public class IntermediateInstruction
{
public string? Label = null;
public PrimaryInstruction inst;
public List<LexSegment> Parameters = new List<LexSegment>();
}
} }

View File

@@ -1,6 +1,6 @@
namespace SVM.Core namespace SVM.Core
{ {
public enum SVMInstDef : byte public enum PrimaryInstruction : byte
{ {
// 0 1 2 3 4 5 6 // 0 1 2 3 4 5 6
// Math [I]Op [I]Type [R]L [R]R [R]T [I]CheckOF // Math [I]Op [I]Type [R]L [R]R [R]T [I]CheckOF

View File

@@ -72,7 +72,7 @@ namespace SVM.Core
switch (def) switch (def)
{ {
case SVMInstDef.BMath: case PrimaryInstruction.BMath:
{ {
var Op = Instruction.GetData<BMathOp>(1); var Op = Instruction.GetData<BMathOp>(1);
var NativeType = Instruction.GetData<SVMNativeTypes>(2); var NativeType = Instruction.GetData<SVMNativeTypes>(2);
@@ -102,16 +102,16 @@ namespace SVM.Core
} }
} }
break; break;
case SVMInstDef.UMath: case PrimaryInstruction.UMath:
break; break;
case SVMInstDef.Cvt: case PrimaryInstruction.Cvt:
{ {
Convert(Instruction); Convert(Instruction);
} }
break; break;
case SVMInstDef.Cmp: case PrimaryInstruction.Cmp:
break; break;
case SVMInstDef.SD: case PrimaryInstruction.SD:
{ {
var Reg = Instruction.GetData<byte>(1); var Reg = Instruction.GetData<byte>(1);
PC++; PC++;
@@ -120,19 +120,19 @@ namespace SVM.Core
registers.SetData(Reg, data); registers.SetData(Reg, data);
} }
break; break;
case SVMInstDef.JAL: case PrimaryInstruction.JAL:
break; break;
case SVMInstDef.JALF: case PrimaryInstruction.JALF:
break; break;
case SVMInstDef.Load: case PrimaryInstruction.Load:
break; break;
case SVMInstDef.Save: case PrimaryInstruction.Save:
break; break;
case SVMInstDef.Call: case PrimaryInstruction.Call:
break; break;
case SVMInstDef.Return: case PrimaryInstruction.Return:
break; break;
case SVMInstDef.System: case PrimaryInstruction.System:
if (Config != null) if (Config != null)
{ {
var target = Instruction.GetData<uint>(1); var target = Instruction.GetData<uint>(1);
@@ -142,7 +142,7 @@ namespace SVM.Core
} }
} }
break; break;
case SVMInstDef.SIMD: case PrimaryInstruction.SIMD:
break; break;
default: default:
break; break;
@@ -351,11 +351,11 @@ namespace SVM.Core
return ((T*)(((byte*)dataPtr) + offset))[0]; return ((T*)(((byte*)dataPtr) + offset))[0];
} }
} }
public SVMInstDef GetDef() public PrimaryInstruction GetDef()
{ {
fixed (ulong* dataPtr = &data) fixed (ulong* dataPtr = &data)
{ {
return ((SVMInstDef*)dataPtr)[0]; return ((PrimaryInstruction*)dataPtr)[0];
} }
} }
} }