V2 funktioniert
This commit is contained in:
Binary file not shown.
Binary file not shown.
@ -13,10 +13,10 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("STCompiler.Common")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+bdee19419dc879274e82afdd621b54e1923e0940")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("STCompiler.Common")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("STCompiler.Common")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
// Generated by the MSBuild WriteCodeFragment class.
|
||||
// Von der MSBuild WriteCodeFragment-Klasse generiert.
|
||||
|
||||
|
||||
@ -1 +1 @@
|
||||
47f7f9783b0890c01839d6f882aac21cc3b1825ebf07d1532f3ffeb6d47e1764
|
||||
003d3c09b7e8f7918fbf4cdaa8090fdb095851afe383470e725a487d784f9416
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -622,12 +622,12 @@ public class BytecodeEmitter {
|
||||
syms[v.Name]=new Symbol{Name=v.Name,Type=v.Type,Index=idx++};
|
||||
|
||||
foreach(var v in p.Vars)
|
||||
if(v.Init!=null){EmitExpr(v.Init);EmitByte(0x03);EmitU16((ushort)syms[v.Name].Index);}
|
||||
if(v.Init!=null){EmitExpr(v.Init);EmitByte(Bytecode.OpCodes.STORE_VAR);EmitU16((ushort)syms[v.Name].Index);}
|
||||
|
||||
foreach(var s in p.Stmts)
|
||||
EmitStmt(s);
|
||||
|
||||
EmitByte(0xF0); // Program End
|
||||
EmitByte(Bytecode.OpCodes.HALT); // Program End
|
||||
}
|
||||
|
||||
// Operationscodes für verschiedene Datentypen
|
||||
@ -779,14 +779,14 @@ public class BytecodeEmitter {
|
||||
int ci = AddConst(ie.Value);
|
||||
EmitByte(Bytecode.OpCodes.PUSH_CONST); // PUSH_CONST
|
||||
EmitU16((ushort)ci);
|
||||
EmitByte((byte)ie.Type); // Typ der Konstante
|
||||
// removed emitting the type byte into code stream; type is stored with constant table
|
||||
break;
|
||||
|
||||
case RealExpr re:
|
||||
int cri = AddConst(re.Value);
|
||||
EmitByte(Bytecode.OpCodes.PUSH_REAL_CONST); // PUSH_REAL_CONST
|
||||
EmitU16((ushort)cri);
|
||||
EmitByte((byte)re.Type); // REAL oder LREAL
|
||||
// removed emitting the type byte into code stream; type is stored with constant table
|
||||
break;
|
||||
|
||||
case VarExpr ve:
|
||||
@ -808,10 +808,11 @@ public class BytecodeEmitter {
|
||||
}
|
||||
|
||||
// Wenn nötig, Typenkonvertierung vor der Operation
|
||||
if (be.L.Type != be.Type)
|
||||
EmitByte((byte)(0x50 + (int)be.Type)); // CONVERT_TO_*
|
||||
if (be.R.Type != be.Type)
|
||||
EmitByte((byte)(0x50 + (int)be.Type)); // CONVERT_TO_*
|
||||
// Conversion opcodes removed - runtime will handle type differences using constant type markers and operation opcodes
|
||||
// if (be.L.Type != be.Type)
|
||||
// EmitByte((byte)(0x50 + (int)be.Type)); // CONVERT_TO_*
|
||||
// if (be.R.Type != be.Type)
|
||||
// EmitByte((byte)(0x50 + (int)be.Type)); // CONVERT_TO_*
|
||||
|
||||
EmitByte(opCodes[key]);
|
||||
break;
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -13,10 +13,10 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("STCompiler.Compiler")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+bdee19419dc879274e82afdd621b54e1923e0940")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("STCompiler.Compiler")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("STCompiler.Compiler")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
// Generated by the MSBuild WriteCodeFragment class.
|
||||
// Von der MSBuild WriteCodeFragment-Klasse generiert.
|
||||
|
||||
|
||||
@ -1 +1 @@
|
||||
62e0a705d77d04812d8911688545c821288c02824251ca71c994335e8a831b77
|
||||
46a6144e75588781017434fbf53d108cb421bb266942aa9aa55b1dd432b3b750
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -13,10 +13,10 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("STCompiler.Disassembler")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+bdee19419dc879274e82afdd621b54e1923e0940")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("STCompiler.Disassembler")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("STCompiler.Disassembler")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
// Generated by the MSBuild WriteCodeFragment class.
|
||||
// Von der MSBuild WriteCodeFragment-Klasse generiert.
|
||||
|
||||
|
||||
@ -1 +1 @@
|
||||
dc637a389172b7c1d98556c8fdb0f7e6b7d0182cadc40b75d2a802cc3145ba46
|
||||
e512ea89251f2295a76007dc4f99ce92f19bc2813739fd32e9bf7233cc288310
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -23,86 +23,71 @@ class Program {
|
||||
string magic = System.Text.Encoding.ASCII.GetString(r.ReadBytes(4));
|
||||
if (magic != Bytecode.Magic) throw new Exception("Invalid magic");
|
||||
ushort ver = r.ReadUInt16();
|
||||
if (ver != Bytecode.Version) throw new Exception($"Unsupported version {ver}");
|
||||
Console.WriteLine($"Version: {ver}");
|
||||
bool oldFormat = ver < Bytecode.Version;
|
||||
|
||||
ushort nConsts = r.ReadUInt16();
|
||||
Console.WriteLine($"Consts: {nConsts}");
|
||||
var consts = new List<object>();
|
||||
if (oldFormat) {
|
||||
// old format: constants stored as 4-byte ints
|
||||
for (int i = 0; i < nConsts; i++) { int v = r.ReadInt32(); consts.Add(v); Console.WriteLine($" [{i}] = {v}"); }
|
||||
} else {
|
||||
for (int i = 0; i < nConsts; i++) {
|
||||
byte t = r.ReadByte();
|
||||
switch(t) {
|
||||
case 1: consts.Add(r.ReadInt64()); break;
|
||||
case 2: consts.Add(r.ReadDouble()); break;
|
||||
case 3: consts.Add(r.ReadSingle()); break;
|
||||
case 4: consts.Add(r.ReadInt32()); break;
|
||||
default: throw new Exception($"Unknown const type {t}");
|
||||
case 1: { long v = r.ReadInt64(); consts.Add(v); Console.WriteLine($" [{i}] (long) = {v}"); break; }
|
||||
case 2: { double v = r.ReadDouble(); consts.Add(v); Console.WriteLine($" [{i}] (double) = {v}"); break; }
|
||||
case 3: { float v = r.ReadSingle(); consts.Add(v); Console.WriteLine($" [{i}] (float) = {v}"); break; }
|
||||
case 4: { int v = r.ReadInt32(); consts.Add(v); Console.WriteLine($" [{i}] (int) = {v}"); break; }
|
||||
default: { Console.WriteLine($" [{i}] Unknown const type {t}"); break; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ushort nVars = r.ReadUInt16();
|
||||
var varTypes = new VarType[nVars];
|
||||
for (int i = 0; i < nVars; i++) varTypes[i] = (VarType)r.ReadByte();
|
||||
Console.WriteLine($"Vars: {nVars}");
|
||||
var varTypes = new byte[nVars];
|
||||
for (int i = 0; i < nVars; i++) { varTypes[i] = r.ReadByte(); Console.WriteLine($" Var[{i}] type = {varTypes[i]}"); }
|
||||
|
||||
ushort codeLen = r.ReadUInt16();
|
||||
Console.WriteLine($"CodeLen: {codeLen} bytes");
|
||||
var code = r.ReadBytes(codeLen);
|
||||
|
||||
var stack = new Stack<object>();
|
||||
var vars = new object[nVars];
|
||||
int ip = 0;
|
||||
Console.WriteLine("\n--- Disassembly / Simulation ---");
|
||||
while (ip < code.Length) {
|
||||
int addr = ip;
|
||||
byte op = code[ip++];
|
||||
switch(op) {
|
||||
case Bytecode.OpCodes.NOP: break;
|
||||
Console.Write($"{addr:0000}: 0x{op:X2} {Bytecode.OpName(op)} ");
|
||||
switch (op) {
|
||||
case Bytecode.OpCodes.NOP: Console.WriteLine("NOP"); break;
|
||||
case Bytecode.OpCodes.PUSH_CONST: {
|
||||
ushort ci = (ushort)(code[ip++] | (code[ip++] << 8));
|
||||
if (oldFormat) { byte typeMarker = code[ip++]; /* skip legacy type byte */ }
|
||||
Console.WriteLine($"PUSH_CONST {ci} ({consts[ci]})");
|
||||
stack.Push(consts[ci]);
|
||||
break;
|
||||
}
|
||||
break; }
|
||||
case Bytecode.OpCodes.PUSH_REAL_CONST: {
|
||||
ushort ci = (ushort)(code[ip++] | (code[ip++] << 8));
|
||||
if (oldFormat) { byte typeMarker = code[ip++]; /* skip legacy type byte */ }
|
||||
Console.WriteLine($"PUSH_REAL_CONST {ci} ({consts[ci]})");
|
||||
stack.Push(consts[ci]);
|
||||
break; }
|
||||
case Bytecode.OpCodes.LOAD_VAR: { ushort vi = (ushort)(code[ip++] | (code[ip++] << 8)); Console.WriteLine($"LOAD_VAR {vi}"); stack.Push(vars[vi]); break; }
|
||||
case Bytecode.OpCodes.STORE_VAR: { ushort vi = (ushort)(code[ip++] | (code[ip++] << 8)); Console.WriteLine($"STORE_VAR {vi}"); vars[vi] = stack.Pop(); break; }
|
||||
case Bytecode.OpCodes.JZ: { ushort target = (ushort)(code[ip++] | (code[ip++] << 8)); Console.WriteLine($"JZ addr={target}"); var cond = stack.Pop(); bool isFalse = cond is int ci ? ci == 0 : cond is long cl ? cl == 0L : cond is double cd ? cd == 0.0 : cond == null; if (isFalse) ip = target; break; }
|
||||
case Bytecode.OpCodes.JMP: { ushort target = (ushort)(code[ip++] | (code[ip++] << 8)); Console.WriteLine($"JMP addr={target}"); ip = target; break; }
|
||||
case Bytecode.OpCodes.HALT: Console.WriteLine("HALT"); ip = code.Length; break;
|
||||
default: Console.WriteLine();
|
||||
// fallback handlers
|
||||
if (Bytecode.OpName(op).StartsWith("ADD_") || Bytecode.OpName(op).StartsWith("SUB_") || Bytecode.OpName(op).StartsWith("MUL_") || Bytecode.OpName(op).StartsWith("DIV_")) {
|
||||
dynamic b = stack.Pop(); dynamic a = stack.Pop(); if (Bytecode.OpName(op).StartsWith("ADD_")) stack.Push(a + b); else if (Bytecode.OpName(op).StartsWith("SUB_")) stack.Push(a - b); else if (Bytecode.OpName(op).StartsWith("MUL_")) stack.Push(a * b); else stack.Push(a / b);
|
||||
break;
|
||||
}
|
||||
case Bytecode.OpCodes.LOAD_VAR: {
|
||||
ushort vi = (ushort)(code[ip++] | (code[ip++] << 8));
|
||||
stack.Push(vars[vi]);
|
||||
break;
|
||||
}
|
||||
case Bytecode.OpCodes.STORE_VAR: {
|
||||
ushort vi = (ushort)(code[ip++] | (code[ip++] << 8));
|
||||
vars[vi] = stack.Pop();
|
||||
break;
|
||||
}
|
||||
case Bytecode.OpCodes.JZ: {
|
||||
ushort target = (ushort)(code[ip++] | (code[ip++] << 8));
|
||||
var cond = stack.Pop();
|
||||
bool isFalse = cond is int ci ? ci == 0 : cond is long cl ? cl == 0L : cond is double cd ? cd == 0.0 : cond == null;
|
||||
if (isFalse) ip = target;
|
||||
break;
|
||||
}
|
||||
case Bytecode.OpCodes.JMP: {
|
||||
ushort target = (ushort)(code[ip++] | (code[ip++] << 8));
|
||||
ip = target;
|
||||
break;
|
||||
}
|
||||
case Bytecode.OpCodes.HALT:
|
||||
Console.WriteLine("HALT");
|
||||
return;
|
||||
default:
|
||||
// Simple arithmetic handlers for some opcodes
|
||||
if (Bytecode.OpName(op).StartsWith("ADD_")) {
|
||||
dynamic b = stack.Pop(); dynamic a = stack.Pop(); stack.Push(a + b); break;
|
||||
}
|
||||
if (Bytecode.OpName(op).StartsWith("SUB_")) {
|
||||
dynamic b = stack.Pop(); dynamic a = stack.Pop(); stack.Push(a - b); break;
|
||||
}
|
||||
if (Bytecode.OpName(op).StartsWith("MUL_")) {
|
||||
dynamic b = stack.Pop(); dynamic a = stack.Pop(); stack.Push(a * b); break;
|
||||
}
|
||||
if (Bytecode.OpName(op).StartsWith("DIV_")) {
|
||||
dynamic b = stack.Pop(); dynamic a = stack.Pop(); stack.Push(a / b); break;
|
||||
}
|
||||
if (Bytecode.OpName(op).StartsWith("LT_") || Bytecode.OpName(op).StartsWith("GT_") || Bytecode.OpName(op).StartsWith("LE_") || Bytecode.OpName(op).StartsWith("GE_") || Bytecode.OpName(op).StartsWith("EQ_") || Bytecode.OpName(op).StartsWith("NEQ_")) {
|
||||
// comparisons: pop r, pop l, push int 0/1
|
||||
dynamic rVal = stack.Pop(); dynamic lVal = stack.Pop();
|
||||
bool res = Bytecode.OpName(op).StartsWith("LT_") ? (lVal < rVal) :
|
||||
Bytecode.OpName(op).StartsWith("GT_") ? (lVal > rVal) :
|
||||
@ -117,7 +102,14 @@ class Program {
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("Execution finished");
|
||||
for (int i = 0; i < vars.Length; i++) Console.WriteLine($"Var[{i}] = {vars[i]}");
|
||||
Console.WriteLine("Execution finished\n");
|
||||
// Detailed variable summary
|
||||
Console.WriteLine("=== Variable summary ===");
|
||||
Console.WriteLine("Index\tType\t\tValue");
|
||||
for (int i = 0; i < vars.Length; i++) {
|
||||
string typeName = (i < varTypes.Length) ? ((VarType)varTypes[i]).ToString() : "Unknown";
|
||||
var value = vars[i] ?? "null";
|
||||
Console.WriteLine($"{i}\t{typeName.PadRight(8)}\t{value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -13,10 +13,10 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("STCompiler.Simulator")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+bdee19419dc879274e82afdd621b54e1923e0940")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("STCompiler.Simulator")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("STCompiler.Simulator")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
// Generated by the MSBuild WriteCodeFragment class.
|
||||
// Von der MSBuild WriteCodeFragment-Klasse generiert.
|
||||
|
||||
|
||||
@ -1 +1 @@
|
||||
e68c3397a6969111aabee6432e0447cb372a6ba9ca289ded0d01796be53dbb16
|
||||
d8b8419173d413df3b338b6f12782609e7ef0c8712b509b4a4bb6869e65f93af
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user