Fixed linker.

This commit is contained in:
2025-08-02 06:31:00 +10:00
parent 6eb87e433e
commit ec6e7e1351
4 changed files with 52 additions and 8 deletions

View File

@@ -4,6 +4,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Text;
namespace SVM.Advanced.BSDStyleVM namespace SVM.Advanced.BSDStyleVM
{ {
@@ -19,7 +20,15 @@ namespace SVM.Advanced.BSDStyleVM
FDs.Add(0, new FileDescripter(Console.OpenStandardInput())); FDs.Add(0, new FileDescripter(Console.OpenStandardInput()));
FDs.Add(1, new FileDescripter(Console.OpenStandardOutput())); FDs.Add(1, new FileDescripter(Console.OpenStandardOutput()));
} }
public int FindAvailableFDID()
{
for (int i = 0; i < int.MaxValue; i++)
{
if (FDs.ContainsKey(i)) continue;
return i;
}
return -1;
}
} }
public class BSDConfig public class BSDConfig
{ {
@@ -38,6 +47,13 @@ namespace SVM.Advanced.BSDStyleVM
config.FuncCalls.Add(4, BSDStyleFunctions0.__write); config.FuncCalls.Add(4, BSDStyleFunctions0.__write);
} }
} }
public static class BSDFcntl
{
public const int O_RDONLY = 0x0;
public const int O_WRONLY = 0x01;
public const int O_RDWR = 0x02;
public const int O_ACCMODE = 0x03;
}
public static class BSDStyleFunctions0 public static class BSDStyleFunctions0
{ {
public static void __exit(SimpleVirtualMachine machine) public static void __exit(SimpleVirtualMachine machine)
@@ -45,7 +61,32 @@ namespace SVM.Advanced.BSDStyleVM
var status = machine.registers.GetData<int>(10); var status = machine.registers.GetData<int>(10);
Environment.Exit(status); Environment.Exit(status);
} }
public static void __write(SimpleVirtualMachine machine) public unsafe static void __open(SimpleVirtualMachine machine)
{
if (machine.AdditionalData is BSDLikeWrapper w)
{
var ptr = machine.registers.GetData<SVMPointer>(10);
var size = machine.registers.GetData<ulong>(11);
var flag = machine.registers.GetData<int>(12);
var fn = Encoding.UTF8.GetString((byte*)machine.GetPointer(ptr), (int)size);
FileMode fm = default;
FileAccess fa = default;
if ((flag & BSDFcntl.O_WRONLY) == BSDFcntl.O_WRONLY)
{
fa = FileAccess.Write;
}
var fdID = w.FindAvailableFDID();
if (fdID == -1) return;
var stream = File.Open(fn, fm, fa);
FileDescripter fd = new FileDescripter(stream);
w.FDs.Add(fdID, fd);
}
else
{
Console.WriteLine("Incorrectly set wrapper!");
}
}
public unsafe static void __write(SimpleVirtualMachine machine)
{ {
if (machine.AdditionalData is BSDLikeWrapper w) if (machine.AdditionalData is BSDLikeWrapper w)
{ {

View File

@@ -3,6 +3,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -390,6 +390,7 @@ namespace SVM.Assembler.Core
} }
Buffer.BlockCopy(data, 0, data2, sizeof(int), data.Length); Buffer.BlockCopy(data, 0, data2, sizeof(int), data.Length);
context.DataOffsets.Add(item.Key, offset); context.DataOffsets.Add(item.Key, offset);
Data.Add(data2);
} }
else if (item.Value.StartsWith("file:")) else if (item.Value.StartsWith("file:"))
{ {
@@ -404,12 +405,12 @@ namespace SVM.Assembler.Core
Buffer.BlockCopy(data, 0, data2, sizeof(int), data.Length); Buffer.BlockCopy(data, 0, data2, sizeof(int), data.Length);
context.DataOffsets.Add(item.Key, offset); context.DataOffsets.Add(item.Key, offset);
offset += (uint)data2.Length; offset += (uint)data2.Length;
Data.Add(data); Data.Add(data2);
} }
else else
{ {
byte[] data; byte[] data;
string str=Regex.Unescape(item.Value); string str = Regex.Unescape(item.Value);
if (str[0] == '\"' && str[^1] == '\"') if (str[0] == '\"' && str[^1] == '\"')
{ {
data = Encoding.UTF8.GetBytes(str, 1, str.Length - 2); data = Encoding.UTF8.GetBytes(str, 1, str.Length - 2);
@@ -417,16 +418,16 @@ namespace SVM.Assembler.Core
} }
else else
data = Encoding.UTF8.GetBytes(str); data = Encoding.UTF8.GetBytes(str);
byte[] data2 = new byte[data.Length + sizeof(int)]; byte[] data2 = new byte[data.Length + sizeof(int) + sizeof(char)];
fixed (byte* ptr = data2) fixed (byte* ptr = data2)
{ {
int len = data.Length; int len = data.Length + sizeof(char);
((IntPtr)ptr).SetData(len); ((IntPtr)ptr).SetData(len);
} }
Buffer.BlockCopy(data, 0, data2, sizeof(int), data.Length); Buffer.BlockCopy(data, 0, data2, sizeof(int), data.Length);
context.DataOffsets.Add(item.Key, offset); context.DataOffsets.Add(item.Key, offset);
offset += (uint)data2.Length; offset += (uint)data2.Length;
Data.Add(data); Data.Add(data2);
} }
} }
foreach (var item in Obj.instructions) foreach (var item in Obj.instructions)

View File

@@ -20,7 +20,8 @@ namespace SVM.Assembler.Core
{ {
if (DataOffsets.TryGetValue(label, out offset)) if (DataOffsets.TryGetValue(label, out offset))
{ {
offset += (uint)IntermediateObject.DetermineFinalInstructionCount(this) * (uint)sizeof(SVMInstruction); //Don't directly give the length there.
offset += (uint)IntermediateObject.DetermineFinalInstructionCount(this) * (uint)sizeof(SVMInstruction)+sizeof(int);
return true; return true;
} }
return false; return false;