Neue Architektur

This commit is contained in:
2025-10-12 19:06:28 +02:00
commit bdee19419d
120 changed files with 2706 additions and 0 deletions

View File

@ -0,0 +1,123 @@
using System;
using System.IO;
using STCompiler.Common;
using System.Collections.Generic;
class Program {
static int Main(string[] args) {
if (args.Length < 1) {
Console.WriteLine("Usage: StSim <file.stbc>");
return 1;
}
var path = args[0];
if (!File.Exists(path)) { Console.WriteLine("File not found: " + path); return 2; }
var data = File.ReadAllBytes(path);
try { Simulate(data); } catch(Exception ex) { Console.WriteLine("Error: " + ex.Message); return 3; }
return 0;
}
static void Simulate(byte[] data) {
using var ms = new MemoryStream(data);
using var r = new BinaryReader(ms);
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}");
ushort nConsts = r.ReadUInt16();
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}");
}
}
ushort nVars = r.ReadUInt16();
var varTypes = new VarType[nVars];
for (int i = 0; i < nVars; i++) varTypes[i] = (VarType)r.ReadByte();
ushort codeLen = r.ReadUInt16();
var code = r.ReadBytes(codeLen);
var stack = new Stack<object>();
var vars = new object[nVars];
int ip = 0;
while (ip < code.Length) {
byte op = code[ip++];
switch(op) {
case Bytecode.OpCodes.NOP: break;
case Bytecode.OpCodes.PUSH_CONST: {
ushort ci = (ushort)(code[ip++] | (code[ip++] << 8));
stack.Push(consts[ci]);
break;
}
case Bytecode.OpCodes.PUSH_REAL_CONST: {
ushort ci = (ushort)(code[ip++] | (code[ip++] << 8));
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;
}
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) :
Bytecode.OpName(op).StartsWith("LE_") ? (lVal <= rVal) :
Bytecode.OpName(op).StartsWith("GE_") ? (lVal >= rVal) :
Bytecode.OpName(op).StartsWith("EQ_") ? (lVal == rVal) :
(lVal != rVal);
stack.Push(res ? 1 : 0);
break;
}
throw new Exception($"Unknown opcode 0x{op:X2}");
}
}
Console.WriteLine("Execution finished");
for (int i = 0; i < vars.Length; i++) Console.WriteLine($"Var[{i}] = {vars[i]}");
}
}

View File

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\STCompiler.Common\STCompiler.Common.csproj" />
</ItemGroup>
</Project>

Binary file not shown.

View File

@ -0,0 +1,36 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v8.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v8.0": {
"STCompiler.Simulator/1.0.0": {
"dependencies": {
"STCompiler.Common": "1.0.0"
},
"runtime": {
"STCompiler.Simulator.dll": {}
}
},
"STCompiler.Common/1.0.0": {
"runtime": {
"STCompiler.Common.dll": {}
}
}
}
},
"libraries": {
"STCompiler.Simulator/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
},
"STCompiler.Common/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}

View File

@ -0,0 +1,12 @@
{
"runtimeOptions": {
"tfm": "net8.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "8.0.0"
},
"configProperties": {
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}

View File

@ -0,0 +1,4 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v8.0", FrameworkDisplayName = ".NET 8.0")]

View File

@ -0,0 +1,22 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
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.AssemblyProductAttribute("STCompiler.Simulator")]
[assembly: System.Reflection.AssemblyTitleAttribute("STCompiler.Simulator")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.

View File

@ -0,0 +1 @@
e68c3397a6969111aabee6432e0447cb372a6ba9ca289ded0d01796be53dbb16

View File

@ -0,0 +1,13 @@
is_global = true
build_property.TargetFramework = net8.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = STCompiler.Simulator
build_property.ProjectDir = /home/martin/Projects/STCompiler/STCompiler.Simulator/
build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop =

View File

@ -0,0 +1,8 @@
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;

View File

@ -0,0 +1 @@
29797b73d70b263ab36f05c4b9e83be4bb9ce6dda606ecefd27f7cb05260cfd1

View File

@ -0,0 +1,18 @@
/home/martin/Projects/STCompiler/STCompiler.Simulator/obj/Debug/net8.0/STCompiler.Simulator.csproj.AssemblyReference.cache
/home/martin/Projects/STCompiler/STCompiler.Simulator/obj/Debug/net8.0/STCompiler.Simulator.GeneratedMSBuildEditorConfig.editorconfig
/home/martin/Projects/STCompiler/STCompiler.Simulator/obj/Debug/net8.0/STCompiler.Simulator.AssemblyInfoInputs.cache
/home/martin/Projects/STCompiler/STCompiler.Simulator/obj/Debug/net8.0/STCompiler.Simulator.AssemblyInfo.cs
/home/martin/Projects/STCompiler/STCompiler.Simulator/obj/Debug/net8.0/STCompiler.Simulator.csproj.CoreCompileInputs.cache
/home/martin/Projects/STCompiler/STCompiler.Simulator/bin/Debug/net8.0/STCompiler.Simulator
/home/martin/Projects/STCompiler/STCompiler.Simulator/bin/Debug/net8.0/STCompiler.Simulator.deps.json
/home/martin/Projects/STCompiler/STCompiler.Simulator/bin/Debug/net8.0/STCompiler.Simulator.runtimeconfig.json
/home/martin/Projects/STCompiler/STCompiler.Simulator/bin/Debug/net8.0/STCompiler.Simulator.dll
/home/martin/Projects/STCompiler/STCompiler.Simulator/bin/Debug/net8.0/STCompiler.Simulator.pdb
/home/martin/Projects/STCompiler/STCompiler.Simulator/bin/Debug/net8.0/STCompiler.Common.dll
/home/martin/Projects/STCompiler/STCompiler.Simulator/bin/Debug/net8.0/STCompiler.Common.pdb
/home/martin/Projects/STCompiler/STCompiler.Simulator/obj/Debug/net8.0/STCompiler.Simulator.csproj.CopyComplete
/home/martin/Projects/STCompiler/STCompiler.Simulator/obj/Debug/net8.0/STCompiler.Simulator.dll
/home/martin/Projects/STCompiler/STCompiler.Simulator/obj/Debug/net8.0/refint/STCompiler.Simulator.dll
/home/martin/Projects/STCompiler/STCompiler.Simulator/obj/Debug/net8.0/STCompiler.Simulator.pdb
/home/martin/Projects/STCompiler/STCompiler.Simulator/obj/Debug/net8.0/STCompiler.Simulator.genruntimeconfig.cache
/home/martin/Projects/STCompiler/STCompiler.Simulator/obj/Debug/net8.0/ref/STCompiler.Simulator.dll

View File

@ -0,0 +1 @@
5510516bb78bb880923f2d6c201171d2931e70c43ec955a279187d3e3671e44f

Binary file not shown.

View File

@ -0,0 +1,130 @@
{
"format": 1,
"restore": {
"/home/martin/Projects/STCompiler/STCompiler.Simulator/STCompiler.Simulator.csproj": {}
},
"projects": {
"/home/martin/Projects/STCompiler/STCompiler.Common/STCompiler.Common.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/home/martin/Projects/STCompiler/STCompiler.Common/STCompiler.Common.csproj",
"projectName": "STCompiler.Common",
"projectPath": "/home/martin/Projects/STCompiler/STCompiler.Common/STCompiler.Common.csproj",
"packagesPath": "/home/martin/.nuget/packages/",
"outputPath": "/home/martin/Projects/STCompiler/STCompiler.Common/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/home/martin/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net8.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Ref",
"version": "[8.0.20, 8.0.20]"
}
],
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.120/PortableRuntimeIdentifierGraph.json"
}
}
},
"/home/martin/Projects/STCompiler/STCompiler.Simulator/STCompiler.Simulator.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/home/martin/Projects/STCompiler/STCompiler.Simulator/STCompiler.Simulator.csproj",
"projectName": "STCompiler.Simulator",
"projectPath": "/home/martin/Projects/STCompiler/STCompiler.Simulator/STCompiler.Simulator.csproj",
"packagesPath": "/home/martin/.nuget/packages/",
"outputPath": "/home/martin/Projects/STCompiler/STCompiler.Simulator/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/home/martin/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net8.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"projectReferences": {
"/home/martin/Projects/STCompiler/STCompiler.Common/STCompiler.Common.csproj": {
"projectPath": "/home/martin/Projects/STCompiler/STCompiler.Common/STCompiler.Common.csproj"
}
}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Ref",
"version": "[8.0.20, 8.0.20]"
}
],
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.120/PortableRuntimeIdentifierGraph.json"
}
}
}
}
}

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/home/martin/.nuget/packages/</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/home/martin/.nuget/packages/</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.8.1</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="/home/martin/.nuget/packages/" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />

View File

@ -0,0 +1,95 @@
{
"version": 3,
"targets": {
"net8.0": {
"STCompiler.Common/1.0.0": {
"type": "project",
"framework": ".NETCoreApp,Version=v8.0",
"compile": {
"bin/placeholder/STCompiler.Common.dll": {}
},
"runtime": {
"bin/placeholder/STCompiler.Common.dll": {}
}
}
}
},
"libraries": {
"STCompiler.Common/1.0.0": {
"type": "project",
"path": "../STCompiler.Common/STCompiler.Common.csproj",
"msbuildProject": "../STCompiler.Common/STCompiler.Common.csproj"
}
},
"projectFileDependencyGroups": {
"net8.0": [
"STCompiler.Common >= 1.0.0"
]
},
"packageFolders": {
"/home/martin/.nuget/packages/": {}
},
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/home/martin/Projects/STCompiler/STCompiler.Simulator/STCompiler.Simulator.csproj",
"projectName": "STCompiler.Simulator",
"projectPath": "/home/martin/Projects/STCompiler/STCompiler.Simulator/STCompiler.Simulator.csproj",
"packagesPath": "/home/martin/.nuget/packages/",
"outputPath": "/home/martin/Projects/STCompiler/STCompiler.Simulator/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/home/martin/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net8.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"projectReferences": {
"/home/martin/Projects/STCompiler/STCompiler.Common/STCompiler.Common.csproj": {
"projectPath": "/home/martin/Projects/STCompiler/STCompiler.Common/STCompiler.Common.csproj"
}
}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Ref",
"version": "[8.0.20, 8.0.20]"
}
],
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.120/PortableRuntimeIdentifierGraph.json"
}
}
}
}

View File

@ -0,0 +1,10 @@
{
"version": 2,
"dgSpecHash": "Gj7wIZR6uqBd3t5lgWyZTGma/nOTViXRKY2cmywPzQECPkQPmMsSjUVC+FzVzUiNwmOddPHfs6Qrh+783H0YpQ==",
"success": true,
"projectFilePath": "/home/martin/Projects/STCompiler/STCompiler.Simulator/STCompiler.Simulator.csproj",
"expectedPackageFiles": [
"/home/martin/.nuget/packages/microsoft.aspnetcore.app.ref/8.0.20/microsoft.aspnetcore.app.ref.8.0.20.nupkg.sha512"
],
"logs": []
}