Skip to content

Commit

Permalink
feat: Add mod operator
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Dec 3, 2024
1 parent fdd6b78 commit 68b8b0f
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 1 deletion.
6 changes: 6 additions & 0 deletions examples/inp_bin_op.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
void printf(char *, ...);

int main() {
int a = 2;
int b = 3;
a += b;
a -= b;
a *= b + 1;

printf("%u\n", 23 % 16);
printf("%u\n", 19 % 16);
printf("%u\n", 32 % 16);
return 0;
}
5 changes: 5 additions & 0 deletions parser/expr/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ apply_result *binary_op_apply(parser_node *node, context *ctx)
add_text(ctx, "sub %s, %s", rega, regb);
move_reg_to_var(ctx, left, rega);
break;
case TKN_MOD:
add_text(ctx, "mov rdx, 0");
add_text(ctx, "div %s", regb);
add_text(ctx, "mov %s, %s", rega, reg_typed("rdx", left->type, ctx));
break;
case TKN_STAR:
// TODO: check sign for mul/imul
add_text(ctx, "mul %s", regb);
Expand Down
54 changes: 53 additions & 1 deletion tests/output/inp_bin_op.c_asm_output.asm
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
section .data
__main_size: equ 112
__temp_str_0 db `%u\n`, 0
__temp_str_1 db `%u\n`, 0
__temp_str_2 db `%u\n`, 0
__main_size: equ 208
section .text
extern printf
global main
main:
push rbp
Expand Down Expand Up @@ -54,6 +58,54 @@ mul rbx
mov rbx, [rbp-72]
mov [rbx], rax
mov [rbp-96], rax
mov rax, __temp_str_0
mov [rbp-104], rax
mov rax, 23
mov rbx, 16
mov rdx, 0
div rbx
mov rax, rdx
mov [rbp-112], rax
mov rax, [rbp-112]
mov [rbp-120], rax
mov rdi, [rbp-104]
mov rsi, [rbp-120]
mov rax, rbp
sub rax, 8
mov [rbp-128], rax
call printf
mov rax, __temp_str_1
mov [rbp-136], rax
mov rax, 19
mov rbx, 16
mov rdx, 0
div rbx
mov rax, rdx
mov [rbp-144], rax
mov rax, [rbp-144]
mov [rbp-152], rax
mov rdi, [rbp-136]
mov rsi, [rbp-152]
mov rax, rbp
sub rax, 8
mov [rbp-160], rax
call printf
mov rax, __temp_str_2
mov [rbp-168], rax
mov rax, 32
mov rbx, 16
mov rdx, 0
div rbx
mov rax, rdx
mov [rbp-176], rax
mov rax, [rbp-176]
mov [rbp-184], rax
mov rdi, [rbp-168]
mov rsi, [rbp-184]
mov rax, rbp
sub rax, 8
mov [rbp-192], rax
call printf
mov rax, 0
mov rsp, rbp
pop rbp
Expand Down
39 changes: 39 additions & 0 deletions tests/output/inp_bin_op.c_lex_output.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
TKN_VOID
TKN_ID(printf)
TKN_L_PAREN
TKN_CHAR
TKN_STAR
TKN_COMMA
TKN_DOTS
TKN_R_PAREN
TKN_SEMICOLON
TKN_INT
TKN_ID(main)
TKN_L_PAREN
Expand Down Expand Up @@ -27,6 +36,36 @@ TKN_ID(b)
TKN_PLUS
TKN_LIT_INT(1)
TKN_SEMICOLON
TKN_ID(printf)
TKN_L_PAREN
TKN_LIT_STR(%u
)
TKN_COMMA
TKN_LIT_INT(23)
TKN_MOD
TKN_LIT_INT(16)
TKN_R_PAREN
TKN_SEMICOLON
TKN_ID(printf)
TKN_L_PAREN
TKN_LIT_STR(%u
)
TKN_COMMA
TKN_LIT_INT(19)
TKN_MOD
TKN_LIT_INT(16)
TKN_R_PAREN
TKN_SEMICOLON
TKN_ID(printf)
TKN_L_PAREN
TKN_LIT_STR(%u
)
TKN_COMMA
TKN_LIT_INT(32)
TKN_MOD
TKN_LIT_INT(16)
TKN_R_PAREN
TKN_SEMICOLON
TKN_RETURN
TKN_LIT_INT(0)
TKN_SEMICOLON
Expand Down
39 changes: 39 additions & 0 deletions tests/output/inp_bin_op.c_prep_output.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
TKN_VOID
TKN_ID(printf)
TKN_L_PAREN
TKN_CHAR
TKN_STAR
TKN_COMMA
TKN_DOTS
TKN_R_PAREN
TKN_SEMICOLON
TKN_INT
TKN_ID(main)
TKN_L_PAREN
Expand Down Expand Up @@ -27,6 +36,36 @@ TKN_ID(b)
TKN_PLUS
TKN_LIT_INT(1)
TKN_SEMICOLON
TKN_ID(printf)
TKN_L_PAREN
TKN_LIT_STR(%u
)
TKN_COMMA
TKN_LIT_INT(23)
TKN_MOD
TKN_LIT_INT(16)
TKN_R_PAREN
TKN_SEMICOLON
TKN_ID(printf)
TKN_L_PAREN
TKN_LIT_STR(%u
)
TKN_COMMA
TKN_LIT_INT(19)
TKN_MOD
TKN_LIT_INT(16)
TKN_R_PAREN
TKN_SEMICOLON
TKN_ID(printf)
TKN_L_PAREN
TKN_LIT_STR(%u
)
TKN_COMMA
TKN_LIT_INT(32)
TKN_MOD
TKN_LIT_INT(16)
TKN_R_PAREN
TKN_SEMICOLON
TKN_RETURN
TKN_LIT_INT(0)
TKN_SEMICOLON
Expand Down
44 changes: 44 additions & 0 deletions tests/output/inp_bin_op.c_tree_output.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
Program(
FunctionDecl(
Name:
printf
Returns:
Type(Name: (null)):
TKN_VOID
Params:
Type(Name: (null)):
Pointer of:
TKN_CHAR
)
Function(
Name:
main
Expand Down Expand Up @@ -36,6 +47,39 @@ Program(
Variable(b)
Right:
Literal(Type: 34, Value: 1)
FunctionCall:
Function:
Variable(printf)
Args:
Literal(Type: 33, Value: %u
)
BinaryOp(Op: 91)
Left:
Literal(Type: 34, Value: 23)
Right:
Literal(Type: 34, Value: 16)
FunctionCall:
Function:
Variable(printf)
Args:
Literal(Type: 33, Value: %u
)
BinaryOp(Op: 91)
Left:
Literal(Type: 34, Value: 19)
Right:
Literal(Type: 34, Value: 16)
FunctionCall:
Function:
Variable(printf)
Args:
Literal(Type: 33, Value: %u
)
BinaryOp(Op: 91)
Left:
Literal(Type: 34, Value: 32)
Right:
Literal(Type: 34, Value: 16)
Return:
Literal(Type: 34, Value: 0)
)
Expand Down

0 comments on commit 68b8b0f

Please sign in to comment.