Skip to content

Commit

Permalink
fix postfix unary operators (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
amiremohamadi authored Oct 27, 2024
1 parent f6ce664 commit 8fa60ba
Show file tree
Hide file tree
Showing 6 changed files with 513 additions and 1 deletion.
4 changes: 4 additions & 0 deletions examples/inp_unary_op.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ int main() {
printf("%d\n", ++a);
printf("%d\n", a++);
printf("%d\n", a--);

for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
}
4 changes: 3 additions & 1 deletion parser/expr/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,20 @@ apply_result *postfix_op_apply(parser_node *node, context *ctx) {
apply_result *operand = postfix_op->exp->apply(postfix_op->exp, ctx);
add_text(ctx, "mov rax, %s", operand->code);
symbol *tmp = new_temp_symbol(ctx, operand->type);
add_text(ctx, "mov %s, rax", tmp->repl);

int op = postfix_op->op;

if (op == TKN_PLUSPLUS) {
add_text(ctx, "add rax, 1");
move_reg_to_var(ctx, operand, "rax");
} else if (op == TKN_MINMIN) {
add_text(ctx, "sub rax, 1");
move_reg_to_var(ctx, operand, "rax");
} else {
exit(1);
}

add_text(ctx, "mov %s, rax", tmp->repl);
add_text(ctx, "; postfix op finish");
return new_result(tmp->repl, tmp->type);
}
Expand Down
1 change: 1 addition & 0 deletions scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"./examples/inp6.c",
"./examples/inp_bin_op.c",
"./examples/inp_linked_list.c",
"./examples/inp_unary_op.c",
]
C_PROGRAM_NAME = "./a.out"
OUTPUT_FOLDER = "tests/output"
Expand Down
262 changes: 262 additions & 0 deletions tests/output/inp_unary_op.c_asm_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
section .data
__printf_size: equ 16
__temp_str_1 db `%d\n`, 0
__temp_str_2 db `%d\n`, 0
__temp_str_3 db `%d\n`, 0
__temp_str_4 db `%d\n`, 0
__temp_str_5 db `%d\n`, 0
__temp_str_6 db `%d\n`, 0
__temp_str_7 db `%d\n`, 0
__temp_str_8 db `%d\n`, 0
__temp_str_9 db `%d\n`, 0
__main_size: equ 432
section .text
extern printf
global main
main:
push rbp
mov rbp, rsp
sub rsp, __main_size
mov rax, 2
mov [rsp+0], rax
mov rax, __temp_str_1
mov [rsp+8], rax
; unary op apply
mov rax, rsp
add rax, 0
mov [rsp+16], rax
; operand code: [rsp+0]
mov rax, [rsp+0]
neg rax
mov [rsp+24], rax
; unary op finish
mov rax, [rsp+24]
mov [rsp+32], rax
mov rdi, [rsp+8]
mov rsi, [rsp+32]
mov rax, rsp
add rax, 0
mov [rsp+40], rax
call printf
mov [rsp+48], rax
mov rax, __temp_str_2
mov [rsp+48], rax
; unary op apply
mov rax, rsp
add rax, 0
mov [rsp+56], rax
; operand code: [rsp+0]
mov rax, [rsp+0]
sub rax, 1
mov rbx, [rsp+56]
mov [rbx], rax
mov [rsp+64], rax
; unary op finish
mov rax, [rsp+64]
mov [rsp+72], rax
mov rdi, [rsp+48]
mov rsi, [rsp+72]
mov rax, rsp
add rax, 0
mov [rsp+80], rax
call printf
mov [rsp+88], rax
mov rax, __temp_str_3
mov [rsp+88], rax
; unary op apply
mov rax, rsp
add rax, 0
mov [rsp+96], rax
; operand code: [rsp+0]
mov rax, [rsp+0]
cmp rax, 0
sete al
movzx rax, al
mov [rsp+104], rax
; unary op finish
mov rax, [rsp+104]
mov [rsp+112], rax
mov rdi, [rsp+88]
mov rsi, [rsp+112]
mov rax, rsp
add rax, 0
mov [rsp+120], rax
call printf
mov [rsp+128], rax
mov rax, __temp_str_4
mov [rsp+128], rax
; unary op apply
; unary op apply
mov rax, rsp
add rax, 0
mov [rsp+136], rax
; operand code: [rsp+0]
mov rax, [rsp+0]
cmp rax, 0
sete al
movzx rax, al
mov [rsp+144], rax
; unary op finish
; operand code: [rsp+144]
mov rax, [rsp+144]
cmp rax, 0
sete al
movzx rax, al
mov [rsp+152], rax
; unary op finish
mov rax, [rsp+152]
mov [rsp+160], rax
mov rdi, [rsp+128]
mov rsi, [rsp+160]
mov rax, rsp
add rax, 0
mov [rsp+168], rax
call printf
mov [rsp+176], rax
mov rax, __temp_str_5
mov [rsp+176], rax
; unary op apply
; unary op apply
mov rax, rsp
add rax, 0
mov [rsp+184], rax
; operand code: [rsp+0]
mov rax, [rsp+0]
neg rax
mov [rsp+192], rax
; unary op finish
; operand code: [rsp+192]
mov rax, [rsp+192]
mov [rsp+200], rax
; unary op finish
mov rax, [rsp+200]
mov [rsp+208], rax
mov rdi, [rsp+176]
mov rsi, [rsp+208]
mov rax, rsp
add rax, 0
mov [rsp+216], rax
call printf
mov [rsp+224], rax
mov rax, __temp_str_6
mov [rsp+224], rax
; unary op apply
mov rax, rsp
add rax, 0
mov [rsp+232], rax
; operand code: [rsp+0]
mov rax, [rsp+0]
add rax, 1
mov rbx, [rsp+232]
mov [rbx], rax
mov [rsp+240], rax
; unary op finish
mov rax, [rsp+240]
mov [rsp+248], rax
mov rdi, [rsp+224]
mov rsi, [rsp+248]
mov rax, rsp
add rax, 0
mov [rsp+256], rax
call printf
mov [rsp+264], rax
mov rax, __temp_str_7
mov [rsp+264], rax
; postfix op apply
mov rax, rsp
add rax, 0
mov [rsp+272], rax
mov rax, [rsp+0]
mov [rsp+280], rax
add rax, 1
mov rbx, [rsp+272]
mov [rbx], rax
; postfix op finish
mov rax, [rsp+280]
mov [rsp+288], rax
mov rdi, [rsp+264]
mov rsi, [rsp+288]
mov rax, rsp
add rax, 0
mov [rsp+296], rax
call printf
mov [rsp+304], rax
mov rax, __temp_str_8
mov [rsp+304], rax
; postfix op apply
mov rax, rsp
add rax, 0
mov [rsp+312], rax
mov rax, [rsp+0]
mov [rsp+320], rax
sub rax, 1
mov rbx, [rsp+312]
mov [rbx], rax
; postfix op finish
mov rax, [rsp+320]
mov [rsp+328], rax
mov rdi, [rsp+304]
mov rsi, [rsp+328]
mov rax, rsp
add rax, 0
mov [rsp+336], rax
call printf
mov [rsp+344], rax
mov rax, 0
mov [rsp+344], rax
__tmp_label_0:
mov rax, rsp
add rax, 344
mov [rsp+352], rax
mov rax, [rsp+344]
mov rbx, 5
cmp rax, rbx
jl __tmp_label_2
mov rax, 0
jmp __tmp_label_3
__tmp_label_2:
mov rax, 1
__tmp_label_3:
mov [rsp+360], rax
mov rax, [rsp+360]
cmp rax, 0
je __tmp_label_1
mov rax, __temp_str_9
mov [rsp+368], rax
mov rax, rsp
add rax, 344
mov [rsp+376], rax
mov rax, [rsp+344]
mov [rsp+384], rax
mov rdi, [rsp+368]
mov rsi, [rsp+384]
mov rax, rsp
add rax, 0
mov [rsp+392], rax
call printf
mov [rsp+400], rax
; postfix op apply
mov rax, rsp
add rax, 344
mov [rsp+368], rax
mov rax, [rsp+344]
mov [rsp+376], rax
add rax, 1
mov rbx, [rsp+368]
mov [rbx], rax
; postfix op finish
jmp __tmp_label_0
__tmp_label_1:
mov rsp, rbp
pop rbp
ret
extern exit
global _start
_start:
; Pass argc and argv
mov rdi, [rsp]
mov rsi, rsp
add rsi, 8
call main
mov rdi, 0
call exit
Loading

0 comments on commit 8fa60ba

Please sign in to comment.