From 896d5a5e138b760f4ca51e677a571a6193a44217 Mon Sep 17 00:00:00 2001 From: nub31 Date: Sat, 1 Feb 2025 17:24:51 +0100 Subject: [PATCH] Move memory allocator to own func and prefix internal functions to prevent collisions --- Nub.Lang/Nub.Lang/Backend/Custom/Generator.cs | 59 +++++++++---------- Nub.Lang/Nub.Lang/Nub.Lang.csproj | 2 +- input/program.nub | 2 +- 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/Nub.Lang/Nub.Lang/Backend/Custom/Generator.cs b/Nub.Lang/Nub.Lang/Backend/Custom/Generator.cs index 8d52fbf..052b7f4 100644 --- a/Nub.Lang/Nub.Lang/Backend/Custom/Generator.cs +++ b/Nub.Lang/Nub.Lang/Backend/Custom/Generator.cs @@ -70,7 +70,24 @@ public class Generator _builder.AppendLine(""" - str_cmp: + eb6e_alloc: + mov rax, 9 + mov rsi, rdi + mov rdi, 0 + mov rdx, 3 + mov r10, 34 + mov r8, -1 + mov r9, 0 + syscall + cmp rax, -1 + je .error + ret + .error: + mov rax, 60 + mov rdi, 1 + syscall + + eb6e_str_cmp: xor rdx, rdx .loop: mov al, [rsi + rdx] @@ -87,16 +104,8 @@ public class Generator .equal: mov rax, 1 ret - """); - - _builder.AppendLine(""" - - alloc_error: - mov rax, 60 - mov rdi, 1 - syscall - - oob_error: + + eb6e_oob_error: mov rax, 60 mov rdi, 139 syscall @@ -419,7 +428,8 @@ public class Generator private void GenerateArrayInitializer(ArrayInitializerNode arrayInitializer) { - Alloc(8 + arrayInitializer.Length * 8); + _builder.AppendLine($" mov rdi, {8 + arrayInitializer.Length * 8}"); + _builder.AppendLine(" call eb6e_alloc"); _builder.AppendLine($" mov QWORD [rax], {arrayInitializer.Length}"); } @@ -496,7 +506,7 @@ public class Generator case StringType: _builder.AppendLine(" mov rdi, rax"); _builder.AppendLine(" mov rsi, rcx"); - _builder.AppendLine(" call str_cmp"); + _builder.AppendLine(" call eb6e_str_cmp"); break; default: throw new ArgumentOutOfRangeException(nameof(type)); @@ -649,7 +659,8 @@ public class Generator throw new Exception($"Struct {structInitializer.StructType} is not defined"); } - Alloc(structDefinition.Members.Count * 8); + _builder.AppendLine($" mov rdi, {structDefinition.Members.Count * 8}"); + _builder.AppendLine(" call eb6e_alloc"); _builder.AppendLine(" mov rcx, rax"); foreach (var initializer in structInitializer.Initializers) @@ -739,32 +750,18 @@ public class Generator _builder.AppendLine(" cmp rdx, rcx"); if (ZeroBasedIndexing) { - _builder.AppendLine(" jge oob_error"); + _builder.AppendLine(" jge eb6e_oob_error"); _builder.AppendLine(" cmp rdx, 0"); } else { - _builder.AppendLine(" jg oob_error"); + _builder.AppendLine(" jg eb6e_oob_error"); _builder.AppendLine(" cmp rdx, 1"); } - _builder.AppendLine(" jl oob_error"); + _builder.AppendLine(" jl eb6e_oob_error"); _builder.AppendLine(" inc rdx"); _builder.AppendLine(" shl rdx, 3"); _builder.AppendLine(" add rax, rdx"); } - - private void Alloc(long bytes) - { - _builder.AppendLine(" mov rax, 9"); - _builder.AppendLine(" mov rdi, 0"); - _builder.AppendLine($" mov rsi, {bytes}"); - _builder.AppendLine(" mov rdx, 3"); - _builder.AppendLine(" mov r10, 34"); - _builder.AppendLine(" mov r8, -1"); - _builder.AppendLine(" mov r9, 0"); - _builder.AppendLine(" syscall"); - _builder.AppendLine(" cmp rax, 0"); - _builder.AppendLine(" je alloc_error"); - } } \ No newline at end of file diff --git a/Nub.Lang/Nub.Lang/Nub.Lang.csproj b/Nub.Lang/Nub.Lang/Nub.Lang.csproj index 4832d7a..46ae5e6 100644 --- a/Nub.Lang/Nub.Lang/Nub.Lang.csproj +++ b/Nub.Lang/Nub.Lang/Nub.Lang.csproj @@ -8,7 +8,7 @@ - + diff --git a/input/program.nub b/input/program.nub index f494d73..18343e0 100644 --- a/input/program.nub +++ b/input/program.nub @@ -23,7 +23,7 @@ func main() { x[23] = "test"; - println(x[22]); + println(x[23]); println(arr_size(x)); }