diff --git a/compiler/NubLang.CLI/Program.cs b/compiler/NubLang.CLI/Program.cs index ef86376..6d8e53f 100644 --- a/compiler/NubLang.CLI/Program.cs +++ b/compiler/NubLang.CLI/Program.cs @@ -91,10 +91,10 @@ if (moduleRepository.Modules().TryGetValue("main", out var mainModule)) .text .globl _start _start: - mov rdi, rsp # Pass stack pointer to main (argv-like) + mov rdi, [rsp + 8] # Pass stack pointer to main (argv-like) call {mainFunction.ExternSymbol} - mov rdi, rax # Move return value into rdi - mov rax, 60 # syscall: exit + mov rdi, rax # Move return value into rdi + mov rax, 60 # syscall: exit syscall """; @@ -102,8 +102,9 @@ if (moduleRepository.Modules().TryGetValue("main", out var mainModule)) var runtimePath = Path.Combine(".build", "runtime.s"); File.WriteAllText(runtimePath, runtime); - using var assembleProcess = Process.Start(new ProcessStartInfo("as", ["-c", runtimePath, "-o", Path.Combine(".build", "runtime.o")])); + using var assembleProcess = Process.Start(new ProcessStartInfo("as", ["-g", "-c", runtimePath, "-o", Path.Combine(".build", "runtime.o")])); if (assembleProcess == null) return 1; + assembleProcess.WaitForExit(); if (assembleProcess.ExitCode != 0) { diff --git a/compiler/NubLang/Generation/Generator.cs b/compiler/NubLang/Generation/Generator.cs index 6537a79..8529af3 100644 --- a/compiler/NubLang/Generation/Generator.cs +++ b/compiler/NubLang/Generation/Generator.cs @@ -166,7 +166,7 @@ public class Generator foreach (var funcNode in _definitions.OfType()) { - EmitLine(funcNode.Tokens); + EmitLine(funcNode.Tokens.FirstOrDefault()); appendNewLine = true; var parameters = funcNode.Signature.Parameters.Count != 0 ? string.Join(", ", funcNode.Signature.Parameters.Select(x => MapNameWithType(x.Type, x.Name))) @@ -185,7 +185,7 @@ public class Generator { foreach (var structFuncNode in structNode.Functions) { - EmitLine(structFuncNode.Tokens); + EmitLine(structFuncNode.Tokens.FirstOrDefault()); var parameters = structFuncNode.Signature.Parameters.Count != 0 ? string.Join(", ", structFuncNode.Signature.Parameters.Select(x => MapNameWithType(x.Type, x.Name))) : "void"; @@ -201,7 +201,7 @@ public class Generator { if (funcNode.Body == null) continue; - EmitLine(funcNode.Tokens); + EmitLine(funcNode.Tokens.FirstOrDefault()); var parameters = funcNode.Signature.Parameters.Count != 0 ? string.Join(", ", funcNode.Signature.Parameters.Select(x => MapNameWithType(x.Type, x.Name))) : "void"; @@ -236,7 +236,7 @@ public class Generator private void EmitStatement(StatementNode statementNode) { - EmitLine(statementNode.Tokens); + EmitLine(statementNode.Tokens.FirstOrDefault()); switch (statementNode) { case AssignmentNode assignmentNode: @@ -277,14 +277,12 @@ public class Generator } } - private void EmitLine(List tokens) + private void EmitLine(Token? token) { - if (tokens.Count >= 1) - { - var file = tokens[0].Span.FilePath; - var line = tokens[0].Span.Start.Line; - _writer.WriteLine($"#line {line} \"{file}\""); - } + if (token == null) return; + var file = token.Span.FilePath; + var line = token.Span.Start.Line; + _writer.WriteLine($"#line {line} \"{file}\""); } private void EmitAssignment(AssignmentNode assignmentNode) @@ -352,10 +350,12 @@ public class Generator EmitStatement(blockDefers[i].Statement); } + EmitLine(returnNode.Tokens.FirstOrDefault()); _writer.WriteLine($"return {tmp};"); } else { + EmitLine(returnNode.Tokens.FirstOrDefault()); _writer.WriteLine($"return {returnValue};"); } } @@ -670,6 +670,7 @@ public class Generator private void EmitBlock(BlockNode blockNode) { + EmitLine(blockNode.Tokens.FirstOrDefault()); _writer.WriteLine("{"); using (_writer.Indent()) { @@ -687,6 +688,7 @@ public class Generator } } + EmitLine(blockNode.Tokens.LastOrDefault()); _writer.WriteLine("}"); } } \ No newline at end of file diff --git a/examples/hello-world/main.nub b/examples/hello-world/main.nub index 1da5724..f63a21f 100644 --- a/examples/hello-world/main.nub +++ b/examples/hello-world/main.nub @@ -4,6 +4,7 @@ extern "puts" func puts(text: cstring) extern "main" func main(args: []cstring): i64 { + defer puts("Goodybye World") puts("Hello World") return 0 } \ No newline at end of file diff --git a/examples/raylib/makefile b/examples/raylib/makefile index d3b41fa..f86d350 100644 --- a/examples/raylib/makefile +++ b/examples/raylib/makefile @@ -1,6 +1,5 @@ -out: main.nub generated/raylib.nub +.build/out: main.nub generated/raylib.nub nubc main.nub generated/raylib.nub raylib-5.5_linux_amd64/lib/libraylib.a clean: @rm -r .build 2>/dev/null || true - @rm out 2>/dev/null || true diff --git a/examples/raylib/x86_64.s b/examples/raylib/x86_64.s deleted file mode 100644 index f55a1d0..0000000 --- a/examples/raylib/x86_64.s +++ /dev/null @@ -1,10 +0,0 @@ -.intel_syntax noprefix - -.text -.globl _start -_start: - mov rdi, rsp - call main - mov rdi, rax - mov rax, 60 - syscall