Update so we dont use callee saved registers

This commit is contained in:
nub31
2025-01-30 18:22:53 +01:00
parent 89ed0e58fd
commit 9625c21148
2 changed files with 21 additions and 27 deletions

View File

@@ -368,7 +368,7 @@ public class Generator
GenerateExpression(binaryExpression.Left, func); GenerateExpression(binaryExpression.Left, func);
_builder.AppendLine(" push rax"); _builder.AppendLine(" push rax");
GenerateExpression(binaryExpression.Right, func); GenerateExpression(binaryExpression.Right, func);
_builder.AppendLine(" mov rbx, rax"); _builder.AppendLine(" mov rcx, rax");
_builder.AppendLine(" pop rax"); _builder.AppendLine(" pop rax");
switch (binaryExpression.Operator) switch (binaryExpression.Operator)
@@ -428,14 +428,14 @@ public class Generator
throw new InvalidOperationException($"Cannot compare type {type}"); throw new InvalidOperationException($"Cannot compare type {type}");
case ArrayType: case ArrayType:
// compare pointers // compare pointers
_builder.AppendLine(" cmp rax, rbx"); _builder.AppendLine(" cmp rax, rcx");
break; break;
case PrimitiveType: case PrimitiveType:
_builder.AppendLine(" cmp rax, rbx"); _builder.AppendLine(" cmp rax, rcx");
break; break;
case StringType: case StringType:
_builder.AppendLine(" mov rdi, rax"); _builder.AppendLine(" mov rdi, rax");
_builder.AppendLine(" mov rsi, rbx"); _builder.AppendLine(" mov rsi, rcx");
_builder.AppendLine(" call str_cmp"); _builder.AppendLine(" call str_cmp");
break; break;
default: default:
@@ -453,10 +453,10 @@ public class Generator
switch (primitiveType.Kind) switch (primitiveType.Kind)
{ {
case PrimitiveTypeKind.Int64: case PrimitiveTypeKind.Int64:
_builder.AppendLine(" add rax, rbx"); _builder.AppendLine(" add rax, rcx");
break; break;
case PrimitiveTypeKind.Int32: case PrimitiveTypeKind.Int32:
_builder.AppendLine(" add eax, ebx"); _builder.AppendLine(" add eax, ecx");
break; break;
default: default:
throw new InvalidOperationException($"Invalid type {primitiveType.Kind}"); throw new InvalidOperationException($"Invalid type {primitiveType.Kind}");
@@ -473,10 +473,10 @@ public class Generator
switch (primitiveType.Kind) switch (primitiveType.Kind)
{ {
case PrimitiveTypeKind.Int64: case PrimitiveTypeKind.Int64:
_builder.AppendLine(" sub rax, rbx"); _builder.AppendLine(" sub rax, rcx");
break; break;
case PrimitiveTypeKind.Int32: case PrimitiveTypeKind.Int32:
_builder.AppendLine(" sub eax, ebx"); _builder.AppendLine(" sub eax, ecx");
break; break;
default: default:
throw new InvalidOperationException($"Invalid type {primitiveType.Kind}"); throw new InvalidOperationException($"Invalid type {primitiveType.Kind}");
@@ -493,10 +493,10 @@ public class Generator
switch (primitiveType.Kind) switch (primitiveType.Kind)
{ {
case PrimitiveTypeKind.Int64: case PrimitiveTypeKind.Int64:
_builder.AppendLine(" imul rbx"); _builder.AppendLine(" imul rcx");
break; break;
case PrimitiveTypeKind.Int32: case PrimitiveTypeKind.Int32:
_builder.AppendLine(" imul ebx"); _builder.AppendLine(" imul ecx");
break; break;
default: default:
throw new InvalidOperationException($"Invalid type {primitiveType.Kind}"); throw new InvalidOperationException($"Invalid type {primitiveType.Kind}");
@@ -514,11 +514,11 @@ public class Generator
{ {
case PrimitiveTypeKind.Int64: case PrimitiveTypeKind.Int64:
_builder.AppendLine(" cqo"); _builder.AppendLine(" cqo");
_builder.AppendLine(" idiv rbx"); _builder.AppendLine(" idiv rcx");
break; break;
case PrimitiveTypeKind.Int32: case PrimitiveTypeKind.Int32:
_builder.AppendLine(" cdq"); _builder.AppendLine(" cdq");
_builder.AppendLine(" idiv ebx"); _builder.AppendLine(" idiv ecx");
break; break;
default: default:
throw new InvalidOperationException($"Invalid type {primitiveType.Kind}"); throw new InvalidOperationException($"Invalid type {primitiveType.Kind}");
@@ -627,25 +627,25 @@ public class Generator
GenerateExpression(index, func); GenerateExpression(index, func);
_builder.AppendLine(" push rax"); _builder.AppendLine(" push rax");
GenerateIdentifier(identifier, func); GenerateIdentifier(identifier, func);
_builder.AppendLine(" pop rbx"); _builder.AppendLine(" pop rcx");
// rcx now holds the length of the array which we can use to check bounds // rcx now holds the length of the array which we can use to check bounds
_builder.AppendLine(" mov rcx, [rax]"); _builder.AppendLine(" mov rcx, [rax]");
_builder.AppendLine(" cmp rbx, rcx"); _builder.AppendLine(" cmp rcx, rcx");
if (ZeroBasedIndexing) if (ZeroBasedIndexing)
{ {
_builder.AppendLine(" jge array_out_of_bounds"); _builder.AppendLine(" jge array_out_of_bounds");
_builder.AppendLine(" cmp rbx, 0"); _builder.AppendLine(" cmp rcx, 0");
} }
else else
{ {
_builder.AppendLine(" jg array_out_of_bounds"); _builder.AppendLine(" jg array_out_of_bounds");
_builder.AppendLine(" cmp rbx, 1"); _builder.AppendLine(" cmp rcx, 1");
} }
_builder.AppendLine(" jl array_out_of_bounds"); _builder.AppendLine(" jl array_out_of_bounds");
_builder.AppendLine(" inc rbx"); _builder.AppendLine(" inc rcx");
_builder.AppendLine(" shl rbx, 3"); _builder.AppendLine(" shl rcx, 3");
_builder.AppendLine(" add rax, rbx"); _builder.AppendLine(" add rax, rcx");
} }
} }

View File

@@ -5,17 +5,14 @@ section .text
global itoa global itoa
itoa: itoa:
push rbx
push rsi
push rdx
mov rax, rdi mov rax, rdi
mov rsi, buffer + 19 mov rsi, buffer + 19
mov byte [rsi], 0 mov byte [rsi], 0
dec rsi dec rsi
.loop: .loop:
xor rdx, rdx xor rdx, rdx
mov rbx, 10 mov rcx, 10
div rbx div rcx
add dl, '0' add dl, '0'
mov [rsi], dl mov [rsi], dl
dec rsi dec rsi
@@ -23,7 +20,4 @@ itoa:
jnz .loop jnz .loop
inc rsi inc rsi
mov rax, rsi mov rax, rsi
pop rdx
pop rsi
pop rbx
ret ret