Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
Something is LYING TO ME
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarWitch7 committed Dec 13, 2023
1 parent 9e9bad6 commit a3622ca
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 262 deletions.
2 changes: 1 addition & 1 deletion Moth.CLI/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"MothCLI": {
"commandName": "Project",
"commandLineArgs": "-v --no-advanced-ir-opt -t exe -o test --moth-libs /home/aurora/RiderProjects/moth-lang/run/mothcore-arch-x86-64.mothlib -i /home/aurora/RiderProjects/moth-lang/TestScripts/moth/Program.moth /home/aurora/RiderProjects/moth-lang/TestScripts/moth/Foreign.moth /home/aurora/RiderProjects/moth-lang/TestScripts/moth/System.moth",
"commandLineArgs": "-v --no-advanced-ir-opt -t lib -o mothcore-arch-x86-64 -i /home/aurora/RiderProjects/moth-lang/TestScripts/moth/Program.moth /home/aurora/RiderProjects/moth-lang/TestScripts/moth/Foreign.moth /home/aurora/RiderProjects/moth-lang/TestScripts/moth/System.moth",
"workingDirectory": "/home/aurora/RiderProjects/moth-lang/run",
"executablePath": "/home/aurora/RiderProjects/moth-lang/Moth.CLI/bin/Debug/net7.0/MothCLI.exe",
"externalTerminal": true,
Expand Down
85 changes: 82 additions & 3 deletions Moth/LLVM/MetadataDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,90 @@ public void Process()
var size = sizeof(Reflection.Header);
_bytes.ReadExactly(new Span<byte>(&header, sizeof(Reflection.Header)));

var types = new Type[(int)(header.field_table_offset - header.type_table_offset)];
var types = new Reflection.Type[(int)((header.field_table_offset
- header.type_table_offset)
/ (uint)sizeof(Reflection.Type))];

fixed (Type* ptr = types)
fixed (Reflection.Type* ptr = types)
{
_bytes.ReadExactly(new Span<byte>((byte*)ptr, sizeof(Type) * types.Length));
_bytes.ReadExactly(new Span<byte>((byte*)ptr, sizeof(Reflection.Type) * types.Length));
}

var fields = new Reflection.Field[(int)((header.function_table_offset
- header.field_table_offset)
/ (uint)sizeof(Reflection.Type))];

fixed (Reflection.Field* ptr = fields)
{
_bytes.ReadExactly(new Span<byte>((byte*)ptr, sizeof(Reflection.Field) * fields.Length));
}

var functions = new Reflection.Function[(int)((header.method_table_offset
- header.function_table_offset)
/ (uint)sizeof(Reflection.Function))];

fixed (Reflection.Function* ptr = functions)
{
_bytes.ReadExactly(new Span<byte>((byte*)ptr, sizeof(Reflection.Function) * functions.Length));
}

var globals = new Reflection.Global[(int)((header.functype_table_offset
- header.global_variable_table_offset)
/ (uint)sizeof(Reflection.Global))];

fixed (Reflection.Global* ptr = globals)
{
_bytes.ReadExactly(new Span<byte>((byte*)ptr, sizeof(Reflection.Global) * globals.Length));
}

var funcTypes = new Reflection.FuncType[(int)((header.param_table_offset
- header.functype_table_offset)
/ (uint)sizeof(Reflection.FuncType))];

fixed (Reflection.FuncType* ptr = funcTypes)
{
_bytes.ReadExactly(new Span<byte>((byte*)ptr, sizeof(Reflection.FuncType) * funcTypes.Length));
}

var parameters = new Reflection.Parameter[(int)((header.paramtype_table_offset
- header.param_table_offset)
/ (uint)sizeof(Reflection.Parameter))];

fixed (Reflection.Parameter* ptr = parameters)
{
_bytes.ReadExactly(new Span<byte>((byte*)ptr, sizeof(Reflection.Parameter) * parameters.Length));
}

var paramTypes = new Reflection.ParamType[(int)((header.typeref_table_offset
- header.paramtype_table_offset)
/ (uint)sizeof(Reflection.ParamType))];

fixed (Reflection.ParamType* ptr = paramTypes)
{
_bytes.ReadExactly(new Span<byte>((byte*)ptr, sizeof(Reflection.ParamType) * paramTypes.Length));
}

var typeRefs = new byte[(int)((header.name_table_offset
- header.typeref_table_offset)
/ (uint)sizeof(byte))];

fixed (byte* ptr = typeRefs)
{
_bytes.ReadExactly(new Span<byte>((byte*)ptr, sizeof(byte) * typeRefs.Length));
}

var names = new byte[(int)((header.size
- header.name_table_offset)
/ (uint)sizeof(byte))];

fixed (byte* ptr = names)
{
_bytes.ReadExactly(new Span<byte>((byte*)ptr, sizeof(byte) * names.Length));
}

if (_bytes.ReadByte() != -1)
{
throw new Exception("Failed to read the entirety of the metadata.");
}

throw new NotImplementedException();
Expand Down
62 changes: 33 additions & 29 deletions Moth/LLVM/MetadataSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ public unsafe class MetadataSerializer
private List<Reflection.Type> _types = new List<Reflection.Type>();
private List<Reflection.Field> _fields = new List<Reflection.Field>();
private List<Reflection.Function> _functions = new List<Reflection.Function>();
private List<Reflection.Function> _methods = new List<Reflection.Function>();
private List<Reflection.Function> _staticMethods = new List<Reflection.Function>();
private List<Reflection.Global> _globals = new List<Reflection.Global>();
private List<Reflection.FuncType> _funcTypes = new List<Reflection.FuncType>();
private List<Reflection.Parameter> _params = new List<Reflection.Parameter>();
private List<Reflection.ParamType> _paramTypes = new List<Reflection.ParamType>();
private List<Reflection.FuncType> _funcTypes = new List<Reflection.FuncType>();
private List<string> _names = new List<string>();
private List<byte> _typeRefs = new List<byte>();
private List<string> _names = new List<string>();
private Dictionary<Data.Type, ulong> _typeIndexes = new Dictionary<Data.Type, ulong>();
private Dictionary<Data.FuncType, ulong> _functypeIndexes = new Dictionary<Data.FuncType, ulong>();
private uint _position = 0;
private uint _typeTablePosition = 0;
private uint _fieldTablePosition = 0;
private uint _functionTablePosition = 0;
Expand All @@ -39,9 +40,6 @@ public MemoryStream Process()
{
MemoryStream bytes = new MemoryStream();
Reflection.Header header = new Reflection.Header();
_position += (uint)sizeof(Reflection.Header);

header.type_table_offset = _position;

foreach (var @struct in _compiler.Types)
{
Expand All @@ -53,8 +51,6 @@ public MemoryStream Process()
AddName(@struct.FullName);
AddType(@struct, newType);
}

header.field_table_offset = _position;

foreach (var kv in _typeIndexes)
{
Expand All @@ -78,8 +74,6 @@ public MemoryStream Process()
}
}

header.function_table_offset = _position;

foreach (var func in _compiler.Functions)
{
var newFunc = new Reflection.Function();
Expand All @@ -101,11 +95,6 @@ public MemoryStream Process()

AddFunction(newFunc);
}

// header.method_table_offset = _position;
// header.static_method_table_offset = _position;

header.global_variable_table_offset = _position;

foreach (var global in _compiler.Globals)
{
Expand All @@ -119,11 +108,35 @@ public MemoryStream Process()
AddGlobal(newGlobal);
}

header.functype_table_offset = _position;
header.param_table_offset = header.functype_table_offset + _functypeTablePosition;
header.paramtype_table_offset = header.param_table_offset + _paramTablePosition;
header.typeref_table_offset = header.param_table_offset + _paramTablePosition;
header.name_table_offset = header.typeref_table_offset + _typeRefTablePosition;
header.type_table_offset
= (ulong)sizeof(Reflection.Header);
header.field_table_offset
= header.type_table_offset + (ulong)(sizeof(Reflection.Type) * _types.Count);
header.function_table_offset
= header.field_table_offset + (ulong)(sizeof(Reflection.Field) * _fields.Count);
header.method_table_offset
= header.function_table_offset + (ulong)(sizeof(Reflection.Function) * _functions.Count);
header.static_method_table_offset
= header.method_table_offset + (ulong)(sizeof(Reflection.Function) * _methods.Count);
header.global_variable_table_offset
= header.static_method_table_offset + (ulong)(sizeof(Reflection.Function) * _staticMethods.Count);
header.functype_table_offset
= header.global_variable_table_offset + (ulong)(sizeof(Reflection.Global) * _globals.Count);
header.param_table_offset
= header.functype_table_offset + (ulong)(sizeof(Reflection.FuncType) * _funcTypes.Count);
header.paramtype_table_offset
= header.param_table_offset + (ulong)(sizeof(Reflection.Parameter) * _params.Count);
header.typeref_table_offset
= header.paramtype_table_offset + (ulong)(sizeof(Reflection.ParamType) * _paramTypes.Count);
header.name_table_offset
= header.typeref_table_offset + (ulong)(sizeof(byte) * _typeRefs.Count);
header.size
= header.name_table_offset;

foreach (var name in _names)
{
header.size += (ulong)(sizeof(char) * name.Length); // only works with ASCII
}

// write the result
bytes.Write(System.Text.Encoding.UTF8.GetBytes("<metadata>"));
Expand Down Expand Up @@ -182,57 +195,49 @@ public void AddType(Struct @struct, Reflection.Type type)
{
_typeIndexes.Add(@struct, _typeTablePosition);
_types.Add(type);
_position += (uint)sizeof(Reflection.Type);
_typeTablePosition++;
}

public void AddFuncType(Data.FuncType originalType, Reflection.FuncType type)
{
_functypeIndexes.Add(originalType, _functypeTablePosition);
_funcTypes.Add(type);
_position += (uint)sizeof(Reflection.FuncType);
_functypeTablePosition++;
}

public void AddField(Reflection.Field field)
{
_fields.Add(field);
_position += (uint)sizeof(Reflection.Field);
_fieldTablePosition++;
}

public void AddFunction(Reflection.Function func)
{
_functions.Add(func);
_position += (uint)sizeof(Reflection.Function);
_functionTablePosition++;
}

public void AddGlobal(Reflection.Global global)
{
_globals.Add(global);
_position += (uint)sizeof(Reflection.Global);
_globalTablePosition++;
}

public void AddParam(Reflection.Parameter param)
{
_params.Add(param);
_position += (uint)sizeof(Reflection.Parameter);
_paramTablePosition++;
}

public void AddParamType(Reflection.ParamType paramType)
{
_paramTypes.Add(paramType);
_position += (uint)sizeof(Reflection.ParamType);
_paramTypeTablePosition++;
}

public void AddName(string name)
{
_names.Add(name);
_position += (uint)(sizeof(char) * name.Length);
_nameTablePosition += (uint)name.Length;
}

Expand Down Expand Up @@ -350,7 +355,6 @@ public ulong AddTypeRef(Reflection.Header header, Type type)
}

_typeRefs.AddRange(result);
_position += (uint)(result.Count * sizeof(byte));
_typeRefTablePosition += (uint)result.Count;
return (ulong)result.Count;
}
Expand Down
3 changes: 2 additions & 1 deletion Moth/LLVM/Reflection/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ public struct Header
public ulong static_method_table_offset;
public ulong global_variable_table_offset;
public ulong functype_table_offset;
public ulong paramtype_table_offset;
public ulong param_table_offset;
public ulong paramtype_table_offset;
public ulong typeref_table_offset;
public ulong name_table_offset;
public ulong size;
}
44 changes: 22 additions & 22 deletions TestScripts/moth/Foreign.moth
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
namespace test;
namespace core;

// private foreign func malloc(size #u64) #void*;
// private foreign func realloc(ptr #void*, size #u64) #void*;
// private foreign func free(pointer #void*) #void;
private foreign func malloc(size #u64) #void*;
private foreign func realloc(ptr #void*, size #u64) #void*;
private foreign func free(pointer #void*) #void;

// private foreign func printf(text #char*, ~~) #i32;
// private foreign func sprintf(destination #char*, format #char*, ~~) #i32;
// private foreign func fflush(stream #FILE*) #i32;
// private foreign func fputs(string #char*, stream #FILE*) #i32;
// private foreign func fgets(str #char*, n #i32, stream #FILE*) #char*;
// private foreign func fgetc(file #FILE*) #i32;
// private foreign func fopen(path #char*, mode #char*) #FILE*;
// private foreign func strcpy(destination #char*, source #char*) #char*;
private foreign func printf(text #char*, ~~) #i32;
private foreign func sprintf(destination #char*, format #char*, ~~) #i32;
private foreign func fflush(stream #FILE*) #i32;
private foreign func fputs(string #char*, stream #FILE*) #i32;
private foreign func fgets(str #char*, n #i32, stream #FILE*) #char*;
private foreign func fgetc(file #FILE*) #i32;
private foreign func fopen(path #char*, mode #char*) #FILE*;
private foreign func strcpy(destination #char*, source #char*) #char*;

// private foreign struct FILE;
private foreign struct FILE;

// // @TargetOS("windows")
// // @CallingConvention("cdecl")
// // private foreign func __acrt_iob_func(n #u32) #FILE*;
// @TargetOS("windows")
// @CallingConvention("cdecl")
// private foreign func __acrt_iob_func(n #u32) #FILE*;

// @TargetOS("linux")
// private foreign stdin #FILE*;
@TargetOS("linux")
private foreign stdin #FILE*;

// @TargetOS("linux")
// private foreign stdout #FILE*;
@TargetOS("linux")
private foreign stdout #FILE*;

// @TargetOS("linux")
// private foreign stderr #FILE*;
@TargetOS("linux")
private foreign stderr #FILE*;
Loading

0 comments on commit a3622ca

Please sign in to comment.