V2 funktioniert

This commit is contained in:
2025-10-12 19:21:32 +02:00
parent bdee19419d
commit d6c0c1ee3d
44 changed files with 71 additions and 78 deletions

View File

@ -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>();
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}");
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: { 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));
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;
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;
}
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}");
}
}
}

View File

@ -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.

View File

@ -1 +1 @@
e68c3397a6969111aabee6432e0447cb372a6ba9ca289ded0d01796be53dbb16
d8b8419173d413df3b338b6f12782609e7ef0c8712b509b4a4bb6869e65f93af