diff --git a/example/src/main.nub b/example/src/main.nub index 8bf7d2e..d063ee0 100644 --- a/example/src/main.nub +++ b/example/src/main.nub @@ -1,8 +1,19 @@ // c extern func puts(text: cstring) +struct Human { + name: cstring + age: u32 +} + func main(args: []cstring): i64 { - puts("Hello, World") + let x: Human = struct { + name = "Oliver" + age = 23 + } + + puts(x.name) + return 0 } diff --git a/src/compiler/NubLang.CLI/NubLang.CLI.csproj b/src/compiler/NubLang.CLI/NubLang.CLI.csproj index a686430..2f25112 100644 --- a/src/compiler/NubLang.CLI/NubLang.CLI.csproj +++ b/src/compiler/NubLang.CLI/NubLang.CLI.csproj @@ -14,8 +14,10 @@ - - + + + + diff --git a/src/compiler/NubLang.CLI/Program.cs b/src/compiler/NubLang.CLI/Program.cs index 2b9f706..3f4f122 100644 --- a/src/compiler/NubLang.CLI/Program.cs +++ b/src/compiler/NubLang.CLI/Program.cs @@ -124,32 +124,38 @@ for (var i = 0; i < typedSyntaxTrees.Count; i++) objectFiles.Add(objFilePath); } -const string runtimeName = "libruntime_x64.a"; - var resources = Assembly.GetExecutingAssembly().GetManifestResourceNames(); -var runtime = resources.First(r => r.EndsWith(runtimeName)); -await using var reader = Assembly.GetExecutingAssembly().GetManifestResourceStream(runtime); +string[] runtimeObjects = ["runtime.o", "x64.o"]; -if (reader == null) +foreach (var runtimeObject in runtimeObjects) { - Console.Error.WriteLine($"Cannot open stream to '{runtimeName}'"); - return 1; + var runtime = resources.First(r => r.EndsWith(runtimeObject)); + + await using var reader = Assembly + .GetExecutingAssembly() + .GetManifestResourceStream(runtime); + + if (reader == null) + { + Console.Error.WriteLine($"Cannot open read stream to '{runtimeObject}'"); + return 1; + } + + var runtimePath = Path.Combine("build", "runtime", runtimeObject); + var runtimeDir = Path.GetDirectoryName(runtimePath); + if (!string.IsNullOrEmpty(runtimeDir)) + { + Directory.CreateDirectory(runtimeDir); + } + + await using var writer = new FileStream(runtimePath, FileMode.Create); + + reader.CopyTo(writer); + + objectFiles.Add(runtimePath); } -var runtimePath = Path.Combine("build", "runtime", runtimeName); -var runtimeDir = Path.GetDirectoryName(runtimePath); -if (!string.IsNullOrEmpty(runtimeDir)) -{ - Directory.CreateDirectory(runtimeDir); -} - -await using var writer = new FileStream(runtimePath, FileMode.Create); - -reader.CopyTo(writer); - -objectFiles.Add(runtimePath); - var outPath = options.OutputPath ?? Path.Combine("build", "out.a"); var outDir = Path.GetDirectoryName(outPath); if (!string.IsNullOrEmpty(outDir)) @@ -163,4 +169,5 @@ if (!archiveResult) return 1; } + return 0; \ No newline at end of file diff --git a/src/compiler/NubLang.CLI/assets/libruntime_x64.a b/src/compiler/NubLang.CLI/assets/runtime.o similarity index 55% rename from src/compiler/NubLang.CLI/assets/libruntime_x64.a rename to src/compiler/NubLang.CLI/assets/runtime.o index d43a568..26e4678 100644 Binary files a/src/compiler/NubLang.CLI/assets/libruntime_x64.a and b/src/compiler/NubLang.CLI/assets/runtime.o differ diff --git a/src/compiler/NubLang.CLI/assets/x64.o b/src/compiler/NubLang.CLI/assets/x64.o new file mode 100644 index 0000000..f4f53cd Binary files /dev/null and b/src/compiler/NubLang.CLI/assets/x64.o differ diff --git a/src/compiler/NubLang/Generation/QBE/QBEGenerator.cs b/src/compiler/NubLang/Generation/QBE/QBEGenerator.cs index af0acbd..4734cdc 100644 --- a/src/compiler/NubLang/Generation/QBE/QBEGenerator.cs +++ b/src/compiler/NubLang/Generation/QBE/QBEGenerator.cs @@ -939,7 +939,7 @@ public class QBEGenerator { destination = TmpName(); var size = SizeOf(structInitializer.StructType); - _writer.Indented($"{destination} =l alloc8 {size}"); + _writer.Indented($"{destination} =l call $malloc(l {size})"); } foreach (var field in structDef.Fields) diff --git a/src/runtime/.gitignore b/src/runtime/.gitignore index 4c66b30..378eac2 100644 --- a/src/runtime/.gitignore +++ b/src/runtime/.gitignore @@ -1,2 +1 @@ -bin -bin-int \ No newline at end of file +build diff --git a/src/runtime/makefile b/src/runtime/makefile index d868586..ef774d8 100644 --- a/src/runtime/makefile +++ b/src/runtime/makefile @@ -3,14 +3,12 @@ TARGET ?= x64 CFLAGS = -Wall -Werror -Wextra -g -libruntime: bin-int/runtime.o - $(CC) $(CFLAGS) -c targets/$(TARGET).s -o bin-int/$(TARGET).o - mkdir -p bin - ar rcs bin/libruntime_$(TARGET).a bin-int/runtime.o bin-int/$(TARGET).o +libruntime: build/runtime.o + $(CC) $(CFLAGS) -c targets/$(TARGET).s -o build/$(TARGET).o -bin-int/runtime.o: runtime/runtime.c - mkdir -p bin-int - $(CC) $(CFLAGS) -c runtime/runtime.c -o bin-int/runtime.o +build/runtime.o: runtime/runtime.c + mkdir -p build + $(CC) $(CFLAGS) -c runtime/runtime.c -o build/runtime.o clean: - rm -r bin bin-int + rm -r build