Move memory allocator to own func and prefix internal functions to prevent collisions
This commit is contained in:
@@ -70,7 +70,24 @@ public class Generator
|
|||||||
|
|
||||||
_builder.AppendLine("""
|
_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
|
xor rdx, rdx
|
||||||
.loop:
|
.loop:
|
||||||
mov al, [rsi + rdx]
|
mov al, [rsi + rdx]
|
||||||
@@ -87,16 +104,8 @@ public class Generator
|
|||||||
.equal:
|
.equal:
|
||||||
mov rax, 1
|
mov rax, 1
|
||||||
ret
|
ret
|
||||||
""");
|
|
||||||
|
eb6e_oob_error:
|
||||||
_builder.AppendLine("""
|
|
||||||
|
|
||||||
alloc_error:
|
|
||||||
mov rax, 60
|
|
||||||
mov rdi, 1
|
|
||||||
syscall
|
|
||||||
|
|
||||||
oob_error:
|
|
||||||
mov rax, 60
|
mov rax, 60
|
||||||
mov rdi, 139
|
mov rdi, 139
|
||||||
syscall
|
syscall
|
||||||
@@ -419,7 +428,8 @@ public class Generator
|
|||||||
|
|
||||||
private void GenerateArrayInitializer(ArrayInitializerNode arrayInitializer)
|
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}");
|
_builder.AppendLine($" mov QWORD [rax], {arrayInitializer.Length}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -496,7 +506,7 @@ public class Generator
|
|||||||
case StringType:
|
case StringType:
|
||||||
_builder.AppendLine(" mov rdi, rax");
|
_builder.AppendLine(" mov rdi, rax");
|
||||||
_builder.AppendLine(" mov rsi, rcx");
|
_builder.AppendLine(" mov rsi, rcx");
|
||||||
_builder.AppendLine(" call str_cmp");
|
_builder.AppendLine(" call eb6e_str_cmp");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new ArgumentOutOfRangeException(nameof(type));
|
throw new ArgumentOutOfRangeException(nameof(type));
|
||||||
@@ -649,7 +659,8 @@ public class Generator
|
|||||||
throw new Exception($"Struct {structInitializer.StructType} is not defined");
|
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");
|
_builder.AppendLine(" mov rcx, rax");
|
||||||
|
|
||||||
foreach (var initializer in structInitializer.Initializers)
|
foreach (var initializer in structInitializer.Initializers)
|
||||||
@@ -739,32 +750,18 @@ public class Generator
|
|||||||
_builder.AppendLine(" cmp rdx, rcx");
|
_builder.AppendLine(" cmp rdx, rcx");
|
||||||
if (ZeroBasedIndexing)
|
if (ZeroBasedIndexing)
|
||||||
{
|
{
|
||||||
_builder.AppendLine(" jge oob_error");
|
_builder.AppendLine(" jge eb6e_oob_error");
|
||||||
_builder.AppendLine(" cmp rdx, 0");
|
_builder.AppendLine(" cmp rdx, 0");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_builder.AppendLine(" jg oob_error");
|
_builder.AppendLine(" jg eb6e_oob_error");
|
||||||
_builder.AppendLine(" cmp rdx, 1");
|
_builder.AppendLine(" cmp rdx, 1");
|
||||||
}
|
}
|
||||||
_builder.AppendLine(" jl oob_error");
|
_builder.AppendLine(" jl eb6e_oob_error");
|
||||||
|
|
||||||
_builder.AppendLine(" inc rdx");
|
_builder.AppendLine(" inc rdx");
|
||||||
_builder.AppendLine(" shl rdx, 3");
|
_builder.AppendLine(" shl rdx, 3");
|
||||||
_builder.AppendLine(" add rax, rdx");
|
_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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Nub.Core" Version="1.0.1" />
|
<PackageReference Include="Nub.Core" Version="1.0.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func main() {
|
|||||||
|
|
||||||
x[23] = "test";
|
x[23] = "test";
|
||||||
|
|
||||||
println(x[22]);
|
println(x[23]);
|
||||||
println(arr_size(x));
|
println(arr_size(x));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user