codegen remainder, fix addition

This commit is contained in:
pommicket 2022-02-12 18:47:03 -05:00
parent d44625524a
commit 31aff32423
2 changed files with 145 additions and 114 deletions

View file

@ -876,11 +876,11 @@ function generate_stack_add
argument type1 ; type of 1st operand argument type1 ; type of 1st operand
argument type2 ; type of 2nd operand argument type2 ; type of 2nd operand
argument out_type argument out_type
local p
p = types + out_type
out_type += types if *1p == TYPE_FLOAT goto generate_add_floats
if *1out_type == TYPE_FLOAT goto generate_add_floats if *1p == TYPE_DOUBLE goto generate_add_doubles
if *1out_type == TYPE_DOUBLE goto generate_add_doubles
emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp] (second operand) emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp] (second operand)
scale_rax_for_addition_with(type1) ; in case this is a pointer addition scale_rax_for_addition_with(type1) ; in case this is a pointer addition
@ -1060,14 +1060,12 @@ function generate_stack_div
emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8] (first operand) emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8] (first operand)
c = *1p & 1 c = *1p & 1
if c == 1 goto generate_div_signed if c == 1 goto generate_div_signed
:generate_div_unsigned
emit_zero_rdx() ; xor edx, edx emit_zero_rdx() ; xor edx, edx
emit_div_rbx() ; div rbx emit_div_rbx() ; div rbx
goto generate_div_cont goto generate_div_cont
:generate_div_signed :generate_div_signed
emit_cqo() ; cqo emit_cqo() ; cqo
emit_idiv_rbx() ; idiv rbx emit_idiv_rbx() ; idiv rbx
goto generate_div_cont
:generate_div_cont :generate_div_cont
emit_add_rsp_imm32(8) ; add rsp, 8 emit_add_rsp_imm32(8) ; add rsp, 8
emit_mov_qword_rsp_rax() ; mov [rsp], rax emit_mov_qword_rsp_rax() ; mov [rsp], rax
@ -1100,6 +1098,32 @@ function generate_stack_div
emit_mov_qword_rsp_rax() ; mov [rsp], rax emit_mov_qword_rsp_rax() ; mov [rsp], rax
return return
; pop the top two things off of the stack, and push their remainder
function generate_stack_remainder
argument statement ; for errors
argument type
local p
local c
p = types + type
emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp] (second operand)
emit_mov_reg(REG_RBX, REG_RAX) ; mov rbx, rax
emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8] (first operand)
c = *1p & 1
if c == 1 goto generate_remainder_signed
emit_zero_rdx() ; xor edx, edx
emit_div_rbx() ; div rbx
emit_mov_reg(REG_RAX, REG_RDX) ; mov rax, rdx
goto generate_remainder_cont
:generate_remainder_signed
emit_cqo() ; cqo
emit_idiv_rbx() ; idiv rbx
emit_mov_reg(REG_RAX, REG_RDX) ; mov rax, rdx
:generate_remainder_cont
emit_add_rsp_imm32(8) ; add rsp, 8
emit_mov_qword_rsp_rax() ; mov [rsp], rax
generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, type)
return
; pop a pointer off of the stack, then push the dereferenced value according to `type` ; pop a pointer off of the stack, then push the dereferenced value according to `type`
function generate_stack_dereference function generate_stack_dereference
argument statement ; for errors argument statement ; for errors
@ -1293,6 +1317,7 @@ function generate_push_expression
if c == EXPRESSION_SUB goto generate_sub if c == EXPRESSION_SUB goto generate_sub
if c == EXPRESSION_MUL goto generate_mul if c == EXPRESSION_MUL goto generate_mul
if c == EXPRESSION_DIV goto generate_div if c == EXPRESSION_DIV goto generate_div
if c == EXPRESSION_REMAINDER goto generate_remainder
if c == EXPRESSION_GLOBAL_VARIABLE goto generate_global_variable if c == EXPRESSION_GLOBAL_VARIABLE goto generate_global_variable
if c == EXPRESSION_LOCAL_VARIABLE goto generate_local_variable if c == EXPRESSION_LOCAL_VARIABLE goto generate_local_variable
if c == EXPRESSION_DEREFERENCE goto generate_dereference if c == EXPRESSION_DEREFERENCE goto generate_dereference
@ -1403,6 +1428,12 @@ function generate_push_expression
expr = generate_push_expression_casted(statement, expr, type) expr = generate_push_expression_casted(statement, expr, type)
generate_stack_div(statement, type) generate_stack_div(statement, type)
return expr return expr
:generate_remainder
expr += 8
expr = generate_push_expression_casted(statement, expr, type)
expr = generate_push_expression_casted(statement, expr, type)
generate_stack_remainder(statement, type)
return expr
:generate_unary_logical_not :generate_unary_logical_not
expr += 8 expr += 8
p = expr + 4 p = expr + 4

View file

@ -1,4 +1,4 @@
long main(int argc, char **argv) { long main(int argc, char **argv) {
return ((float)1 / (float)3) * (float)3; return argc + argv;
} }