From 8800d0429f55d11deb0f5894784f58dc21fe664d Mon Sep 17 00:00:00 2001 From: nub31 Date: Sat, 14 Jun 2025 19:00:45 +0200 Subject: [PATCH] ... --- example/main.nub | 1 - src/CLI/GCC.cs | 4 ++-- src/CLI/Runtime/entry.s | 5 ++++- src/CLI/Runtime/nub_memcpy.s | 2 +- src/CLI/Runtime/nub_memset.s | 2 +- src/CLI/Runtime/nub_panic.s | 27 ++++++++++++++++----------- src/CLI/Runtime/nub_strcmp.s | 2 +- src/Generation/QBE/QBEGenerator.cs | 10 ++-------- 8 files changed, 27 insertions(+), 26 deletions(-) diff --git a/example/main.nub b/example/main.nub index 942e600..331e130 100644 --- a/example/main.nub +++ b/example/main.nub @@ -38,6 +38,5 @@ export func main(args: []^string): i64 { i = i + 1 } - return 0 } diff --git a/src/CLI/GCC.cs b/src/CLI/GCC.cs index 354f030..445814c 100644 --- a/src/CLI/GCC.cs +++ b/src/CLI/GCC.cs @@ -7,7 +7,7 @@ public static class GCC public static async Task Assemble(string asmPath, string objPath) { using var gccProcess = new Process(); - gccProcess.StartInfo = new ProcessStartInfo("gcc", ["-c", asmPath, "-o", objPath]) + gccProcess.StartInfo = new ProcessStartInfo("gcc", ["-g", "-c", asmPath, "-o", objPath]) { UseShellExecute = false, RedirectStandardOutput = true, @@ -30,7 +30,7 @@ public static class GCC public static async Task Link(List objectFiles, string outputPath) { using var gccProcess = new Process(); - gccProcess.StartInfo = new ProcessStartInfo("gcc", ["-nostartfiles", "-o", outputPath, ..objectFiles]) + gccProcess.StartInfo = new ProcessStartInfo("gcc", ["-g", "-nostartfiles", "-o", outputPath, ..objectFiles]) { UseShellExecute = false, RedirectStandardOutput = true, diff --git a/src/CLI/Runtime/entry.s b/src/CLI/Runtime/entry.s index c46e228..2da9aa7 100644 --- a/src/CLI/Runtime/entry.s +++ b/src/CLI/Runtime/entry.s @@ -1,9 +1,12 @@ -.intel_Syntax noprefix +.intel_syntax noprefix .extern main .section .text .global _start _start: + mov rdi, rsp + sub rsp, 8 + mov [rsp], rdi mov rdi, rsp # func main(args: []^string): i64 call main diff --git a/src/CLI/Runtime/nub_memcpy.s b/src/CLI/Runtime/nub_memcpy.s index e7ce785..9fdf4b6 100644 --- a/src/CLI/Runtime/nub_memcpy.s +++ b/src/CLI/Runtime/nub_memcpy.s @@ -1,4 +1,4 @@ -.intel_Syntax noprefix +.intel_syntax noprefix .section .text # func nub_memcpy(destination: ^u8, source: ^u8, count: u64): ^u8 diff --git a/src/CLI/Runtime/nub_memset.s b/src/CLI/Runtime/nub_memset.s index 63c3757..4c29263 100644 --- a/src/CLI/Runtime/nub_memset.s +++ b/src/CLI/Runtime/nub_memset.s @@ -1,4 +1,4 @@ -.intel_Syntax noprefix +.intel_syntax noprefix .section .text # func nub_memset(destination: ^u8, value: i8, count: u64): ^u8 diff --git a/src/CLI/Runtime/nub_panic.s b/src/CLI/Runtime/nub_panic.s index 3b65f41..f7a7c73 100644 --- a/src/CLI/Runtime/nub_panic.s +++ b/src/CLI/Runtime/nub_panic.s @@ -1,14 +1,19 @@ -.intel_Syntax noprefix -.section .text +.intel_syntax noprefix -# func nub_panic(message: ^u8, message_length: u64): void -.global nub_panic -nub_panic: - mov rdx, rsi - mov rsi, rdi - mov rax, 1 - mov rdi, 2 +.section .data +.align 8 +array_out_of_bounds: + .ascii "Index is out of bounds of array\n" + +.section .text +.global nub_panic_array_oob +nub_panic_array_oob: + mov rax, 1 # syscall = sys_write + mov rdi, 2 # fd = stderr + lea rsi, [rip + array_out_of_bounds] # message + mov rdx, 32 # message length syscall - mov rax, 60 - mov rdi, 101 + + mov rax, 60 # sys_exit + mov rdi, 101 # exit code syscall diff --git a/src/CLI/Runtime/nub_strcmp.s b/src/CLI/Runtime/nub_strcmp.s index f1d278a..da502ec 100644 --- a/src/CLI/Runtime/nub_strcmp.s +++ b/src/CLI/Runtime/nub_strcmp.s @@ -1,4 +1,4 @@ -.intel_Syntax noprefix +.intel_syntax noprefix .section .text # func nub_strcmp(lhs: ^u8, rhs: ^u8): bool diff --git a/src/Generation/QBE/QBEGenerator.cs b/src/Generation/QBE/QBEGenerator.cs index 47ced57..e5c5edf 100644 --- a/src/Generation/QBE/QBEGenerator.cs +++ b/src/Generation/QBE/QBEGenerator.cs @@ -13,8 +13,6 @@ namespace Generation.QBE; public static class QBEGenerator { - private const string OutOfBoundsMessage = "Index is out of bounds\\n"; - private static CompilationUnit _compilationUnit = null!; private static DefinitionTable _definitionTable = null!; @@ -65,12 +63,9 @@ public static class QBEGenerator _builder.AppendLine(); } - _builder.AppendLine($"data $oob_message = {{ b \"{OutOfBoundsMessage}\" }}"); - for (var i = 0; i < _strings.Count; i++) { - var str = _strings[i]; - _builder.AppendLine($"data $string{i + 1} = {{ b \"{str}\", b 0 }}"); + _builder.AppendLine($"data $string{i + 1} = {{ b \"{_strings[i]}\", b 0 }}"); } return _builder.ToString(); @@ -806,7 +801,7 @@ public static class QBEGenerator _builder.AppendLine($" jnz {anyOobName}, {oobLabel}, {notOobLabel}"); _builder.AppendLine(oobLabel); - _builder.AppendLine($" call $nub_panic(l $oob_message, l {OutOfBoundsMessage.Length})"); + _builder.AppendLine($" call $nub_panic_array_oob()"); _builder.AppendLine(notOobLabel); } @@ -1146,7 +1141,6 @@ public static class QBEGenerator } } - private static string GenerateStructInitializer(StructInitializerNode structInitializer) { var structDefinition = _definitionTable.LookupStruct(structInitializer.StructType.Namespace, structInitializer.StructType.Name).GetValue();