summaryrefslogtreecommitdiff
path: root/boot
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2026-03-13 10:15:07 +0100
committerEugen Wissner <belka@caraus.de>2026-03-13 18:14:35 +0100
commitf8ff357045c7126521f7ec8f8f03ac886634eb80 (patch)
tree53596862bbfd61705949b4bf2c93131431a45e7e /boot
parente1971b469e131083bfb11425f9ba5c4e9e650bc6 (diff)
downloadelna-f8ff357045c7126521f7ec8f8f03ac886634eb80.tar.gz
Align the stack upon function entry
Diffstat (limited to 'boot')
-rw-r--r--boot/stage19/cl.elna2
-rw-r--r--boot/stage20/cl.elna45
2 files changed, 45 insertions, 2 deletions
diff --git a/boot/stage19/cl.elna b/boot/stage19/cl.elna
index ed2937d..5a3916e 100644
--- a/boot/stage19/cl.elna
+++ b/boot/stage19/cl.elna
@@ -1516,6 +1516,8 @@ begin
elna_alloc_operation_target(instruction, variable_map)
elsif instruction^.operator = ElnaRtlOperator._xor then
elna_alloc_operation_target(instruction, variable_map)
+ elsif instruction^.operator = ElnaRtlOperator.div then
+ elna_alloc_operation_target(instruction, variable_map)
elsif instruction^.operator = ElnaRtlOperator.rem then
elna_alloc_operation_target(instruction, variable_map)
elsif instruction^.operator = ElnaRtlOperator.slt then
diff --git a/boot/stage20/cl.elna b/boot/stage20/cl.elna
index a4adfc5..267da48 100644
--- a/boot/stage20/cl.elna
+++ b/boot/stage20/cl.elna
@@ -1565,6 +1565,8 @@ begin
instruction := elna_alloc_binary(instructions, instruction, variable_map)
elsif instruction^.operator = ElnaRtlOperator._xor then
instruction := elna_alloc_binary(instructions, instruction, variable_map)
+ elsif instruction^.operator = ElnaRtlOperator.div then
+ instruction := elna_alloc_binary(instructions, instruction, variable_map)
elsif instruction^.operator = ElnaRtlOperator.rem then
instruction := elna_alloc_binary(instructions, instruction, variable_map)
elsif instruction^.operator = ElnaRtlOperator.slt then
@@ -1615,13 +1617,13 @@ begin
(* Write the prologue. *)
printf("\taddi sp, sp, -%i\n\tsw ra, %i(sp)\n\tsw s0, %i(sp)\n\taddi s0, sp, %i\n\0",
- operand_value + 8, operand_value + 4, operand_value, operand_value + 8);
+ operand_value, operand_value - 4, operand_value - 8, operand_value);
fflush(nil)
elsif instruction^.operator = ElnaRtlOperator.ret then
operand_value := instruction^.operands[1].value;
(* Write the epilogue. *)
- printf("\tlw ra, %i(sp)\n\tlw s0, %i(sp)\n\taddi sp, sp, %i\n\0", operand_value + 4, operand_value, operand_value + 8);
+ printf("\tlw ra, %i(sp)\n\tlw s0, %i(sp)\n\taddi sp, sp, %i\n\0", operand_value - 4, operand_value - 8, operand_value);
fflush(nil)
elsif instruction^.operator = ElnaRtlOperator.nop then
else
@@ -1750,6 +1752,44 @@ begin
elna_alloc_procedure(pair^.code)
end;
+proc elna_fixup_instructions(instructions: ^ElnaList);
+var
+ instruction: ^ElnaRtlInstruction;
+ stack_size: Word;
+begin
+ instruction := instructions^.first;
+ stack_size := instruction^.operands[1].value;
+
+ (* Align the stack. *)
+ stack_size := stack_size + 8;
+ if stack_size % 16 then
+ stack_size := stack_size + 16
+ end;
+ stack_size := stack_size / 16;
+ stack_size := stack_size * 16;
+
+ instruction^.operands[1].value := stack_size;
+ instruction := instructions^.last;
+ instruction^.operands[1].value := stack_size
+end;
+
+proc elna_fixup_procedure(rtl_declaration: ^ElnaRtlProcedure);
+begin
+ .elna_fixup_procedure_loop;
+
+ elna_fixup_instructions(@rtl_declaration^.body);
+
+ rtl_declaration := rtl_declaration^.next;
+ if rtl_declaration <> nil then
+ goto elna_fixup_procedure_loop
+ end
+end;
+
+proc elna_fixup_module(pair: ^ElnaInstructionModule);
+begin
+ elna_fixup_procedure(pair^.code)
+end;
+
proc elna_riscv_module(pair: ^ElnaInstructionModule);
var
compiler_strings_copy: Word;
@@ -4378,6 +4418,7 @@ begin
tac := elna_tac_module_declaration(parser_node);
rtl := elna_rtl_module_declaration(tac);
elna_alloc_module(rtl);
+ elna_fixup_module(rtl);
elna_riscv_module(rtl);
end;