From ec27ebeb4449bf721871da54305a1bb029ac1e63 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Sun, 30 Apr 2023 03:11:01 -0300 Subject: gas: Optimize the stack utilization Until now, every computation was pushed onto stack witch creates unnecessary stack manipulation and makes the generated code hard to read and understand. Now, the latest computation is stored and could be either a literal or a value on a register. When it is a register we may need to push the value to stack to avoid data loss. Now if it is a literal, hence, we can just set the value onto a register. example/main.pipa before this commit: .global _start .text _start: push %rbp mov %rsp, %rbp mov $69, %rax ; <- There is no reason to store data in rax mov %rax, %rdi mov $60, %rax syscall pop %rbp example/main.pipa after this commit: .global _start .text _start: push %rbp mov %rsp, %rbp mov $69, %rdi ; <- Fixed! mov $60, %rax syscall pop %rbp example/variables.pipa before this commit: .global _start .text _start: push %rbp mov %rsp, %rbp mov $12, %rax mov %rax, -8(%rbp) mov $32, %rax mov %rax, -16(%rbp) mov -8(%rbp), %rax mov %rax, -32(%rbp) mov -16(%rbp), %rax mov -32(%rbp), %rcx add %rcx, %rax mov %rax, -32(%rbp) mov $2, %rax mov -32(%rbp), %rcx mul %rcx mov %rax, -24(%rbp) mov $1, %rax mov %rax, -40(%rbp) mov $33, %rax mov %rax, -48(%rbp) mov -24(%rbp), %rax mov -48(%rbp), %rcx sub %rcx, %rax mov -40(%rbp), %rcx add %rcx, %rax mov %rax, -32(%rbp) mov $2, %rax mov %rax, -40(%rbp) mov -32(%rbp), %rax mov -40(%rbp), %rcx xor %rdx, %rdx div %rcx mov %rax, %rdi mov $60, %rax syscall pop %rbp example/variables.pipa after this commit: .global _start .text _start: push %rbp mov %rsp, %rbp mov $12, -8(%rbp) mov $32, -16(%rbp) mov -8(%rbp), %rax mov %rax, -32(%rbp) mov -16(%rbp), %rax mov -32(%rbp), %rcx add %rcx, %rax mov %rax, -32(%rbp) mov -32(%rbp), %rcx mov $2, %rax mul %rcx mov %rax, -24(%rbp) mov -24(%rbp), %rax mov $33, %rcx sub %rcx, %rax mov $1, %rcx add %rcx, %rax mov %rax, -32(%rbp) mov -32(%rbp), %rax mov $2, %rcx xor %rdx, %rdx div %rcx mov %rax, %rdi mov $60, %rax syscall pop %rbp Less 8 instructions! Signed-off-by: Carlos Maniero Reviewed-by: Johnny Richard --- src/gas_assembly_generator.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/gas_assembly_generator.h') diff --git a/src/gas_assembly_generator.h b/src/gas_assembly_generator.h index 044e8ed..d7d2e1e 100644 --- a/src/gas_assembly_generator.h +++ b/src/gas_assembly_generator.h @@ -21,11 +21,30 @@ #include "vector.h" #include +typedef enum evaluation_result_kind_t +{ + EVALUATION_RESULT_VOID, + EVALUATION_RESULT_REGISTER, + EVALUATION_RESULT_LITERAL_INTEGER +} evaluation_result_kind_t; + +typedef union evaluation_result_data_t +{ + int64_t literal_int; +} evaluation_result_data_t; + +typedef struct evaluation_result_t +{ + evaluation_result_kind_t kind; + evaluation_result_data_t data; +} evaluation_result_t; + typedef struct gas_assembly_generator_t { FILE *stream; vector_t *refs; int stack_offset; + evaluation_result_t latest_evaluation; } gas_assembly_generator_t; void -- cgit v1.2.3