Skip to content

Commit

Permalink
Merge pull request #48 from Glyphack/main
Browse files Browse the repository at this point in the history
Handle assigning 0 to pointers
  • Loading branch information
Glyphack authored Nov 9, 2024
2 parents 10c8369 + 076120c commit e271ca6
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 3 deletions.
11 changes: 11 additions & 0 deletions codegen/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,17 @@ general_type *new_struct_type(char *struct_name)

int types_equal(general_type *a, general_type *b, context *ctx)
{
// Special case for handling assigning NULL to pointers
if (a->kind == TYPE_POINTER)
{
if (b->kind == TYPE_PRIMITIVE)
{
char *type_name = ((primitive_type *)b->data)->type_name;
if (strcmp(type_name, "TKN_INT") == 0)
return 1;
}
}

if ((a->debug != b->debug) || (a->size != b->size))
{
return 0;
Expand Down
5 changes: 5 additions & 0 deletions examples/inp_pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ int main()
printf("arr[0][0] %d\n", arr[0][0]);
printf("arr[1][0] %d\n", *(arr+1)[0]);
printf("arr[1][1] %d\n", arr[1][1]);

// Can assign 0 or TODO: null to pointers
int *b = 0;
if (b == 0)
printf("b is null pointer\n");
}
3 changes: 2 additions & 1 deletion parser/expr/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ apply_result *binary_op_apply(parser_node *node, context *ctx)
}
}

if (binop->op != TKN_ASSIGN &&
// TODO: clag gives warning when comparing int* and int
if (binop->op != TKN_ASSIGN && binop->op != TKN_EQ &&
(left->type->kind == TYPE_POINTER || right->type->kind == TYPE_POINTER))
{
switch (binop->op)
Expand Down
3 changes: 2 additions & 1 deletion parser/var_decl.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "type.h"
#include "expr/expr.h"
#include "../codegen/codegen.h"
#include "../linked_list.h"

apply_result *var_decl_apply(parser_node *node, context *ctx)
{
Expand All @@ -26,8 +25,10 @@ apply_result *var_decl_apply(parser_node *node, context *ctx)
val->type->debug(val->type, 0);
exit(1);
}
add_text(ctx, ";define variable %s", decl->identity);
add_text(ctx, "mov rax, %s", val->code);
add_text(ctx, "mov %s, rax", sym->repl);
add_text(ctx, ";end define variable %s", decl->identity);
}
return NULL;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/output/inp.c_asm_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ main:
push rbp
mov rbp, rsp
sub rsp, __main_size
;define variable i
mov rax, 10
mov [rsp+0], rax
;end define variable i
;define variable i
mov rax, 0
mov [rsp+8], rax
;end define variable i
; enter loop
__tmp_label_0:
mov rax, rsp
Expand All @@ -32,8 +36,10 @@ mov [rsp+24], rax
mov rax, [rsp+24]
cmp rax, 0
je __tmp_label_1
;define variable i
mov rax, 2
mov [rsp+32], rax
;end define variable i
mov rax, __temp_str_1
mov [rsp+40], rax
mov rax, rsp
Expand Down
14 changes: 14 additions & 0 deletions tests/output/inp3.c_asm_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,10 @@ main:
push rbp
mov rbp, rsp
sub rsp, __main_size
;define variable i2
mov rax, 0
mov [rsp+0], rax
;end define variable i2
; enter loop
__tmp_label_10:
mov rax, rsp
Expand Down Expand Up @@ -290,10 +292,14 @@ add rax, 0
mov [rsp+58], rax
call printf
mov [rsp+66], rax
;define variable a
mov rax, 10
mov [rsp+66], rax
;end define variable a
;define variable b
mov rax, 20
mov [rsp+74], rax
;end define variable b
mov rax, rsp
add rax, 66
mov [rsp+90], rax
Expand All @@ -304,8 +310,10 @@ mov rax, [rsp+66]
mov rbx, [rsp+74]
mul rbx
mov [rsp+106], rax
;define variable c
mov rax, [rsp+106]
mov [rsp+82], rax
;end define variable c
mov rax, rsp
add rax, 82
mov [rsp+122], rax
Expand All @@ -323,8 +331,10 @@ mov rax, [rsp+82]
mov rbx, [rsp+146]
mul rbx
mov [rsp+154], rax
;define variable d
mov rax, [rsp+154]
mov [rsp+114], rax
;end define variable d
mov rax, __temp_str_8
mov [rsp+162], rax
mov rax, rsp
Expand Down Expand Up @@ -379,8 +389,10 @@ add rax, 82
mov [rsp+282], rax
mov rax, [rsp+282]
mov [rsp+290], rax
;define variable cc
mov rax, [rsp+290]
mov [rsp+274], rax
;end define variable cc
mov rax, __temp_str_9
mov [rsp+298], rax
mov rax, rsp
Expand Down Expand Up @@ -713,8 +725,10 @@ add rax, 0
mov [rsp+994], rax
call printf
mov [rsp+1002], rax
;define variable i
mov rax, 0
mov [rsp+1002], rax
;end define variable i
; enter loop
__tmp_label_20:
mov rax, rsp
Expand Down
4 changes: 4 additions & 0 deletions tests/output/inp4.c_asm_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,10 @@ add rax, 0
mov [rsp+1008], rax
call malloc
mov [rsp+1016], rax
;define variable salam
mov rax, [rsp+1016]
mov [rsp+984], rax
;end define variable salam
mov rax, rsp
add rax, 984
mov [rsp+1024], rax
Expand Down Expand Up @@ -272,8 +274,10 @@ mov rax, [rbx]
mov [rsp+1080], rax
mov rax, [rsp+1072]
mov [rsp+1088], rax
;define variable s5
mov rax, [rsp+1088]
mov [rsp+1056], rax
;end define variable s5
mov rax, __temp_str_3
mov [rsp+1096], rax
mov rax, rsp
Expand Down
14 changes: 14 additions & 0 deletions tests/output/inp5.c_asm_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ mov rbp, rsp
sub rsp, __main_size
mov [rsp+0], rdi
mov [rsp+8], rsi
;define variable i
mov rax, 0
mov [rsp+16], rax
;end define variable i
; enter loop
__tmp_label_0:
mov rax, rsp
Expand Down Expand Up @@ -94,10 +96,14 @@ add rax, 0
mov [rsp+40], rax
call malloc
mov [rsp+48], rax
;define variable a
mov rax, [rsp+48]
mov [rsp+16], rax
;end define variable a
;define variable i
mov rax, 0
mov [rsp+56], rax
;end define variable i
; enter loop
__tmp_label_4:
mov rax, rsp
Expand Down Expand Up @@ -162,8 +168,10 @@ mov [rsp+160], rax
jmp __tmp_label_4
__tmp_label_5:
; exit loop
;define variable i
mov rax, 0
mov [rsp+56], rax
;end define variable i
; enter loop
__tmp_label_8:
mov rax, rsp
Expand Down Expand Up @@ -231,10 +239,14 @@ add rax, 0
mov [rsp+80], rax
call malloc
mov [rsp+88], rax
;define variable b
mov rax, [rsp+88]
mov [rsp+56], rax
;end define variable b
;define variable c
mov rax, 123
mov [rsp+96], rax
;end define variable c
mov rax, rsp
add rax, 56
mov [rsp+104], rax
Expand Down Expand Up @@ -323,8 +335,10 @@ add rax, 0
mov [rsp+304], rax
call malloc
mov [rsp+312], rax
;define variable str
mov rax, [rsp+312]
mov [rsp+280], rax
;end define variable str
mov rax, rsp
add rax, 280
mov [rsp+320], rax
Expand Down
2 changes: 2 additions & 0 deletions tests/output/inp6.c_asm_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ add rax, 0
mov [rsp+16], rax
call malloc
mov [rsp+24], rax
;define variable m
mov rax, [rsp+24]
mov [rsp+0], rax
;end define variable m
mov rax, rsp
add rax, 0
mov [rsp+32], rax
Expand Down
4 changes: 4 additions & 0 deletions tests/output/inp_bin_op.c_asm_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ main:
push rbp
mov rbp, rsp
sub rsp, __main_size
;define variable a
mov rax, 2
mov [rsp+0], rax
;end define variable a
;define variable b
mov rax, 3
mov [rsp+8], rax
;end define variable b
mov rax, rsp
add rax, 0
mov [rsp+16], rax
Expand Down
8 changes: 8 additions & 0 deletions tests/output/inp_break.c_asm_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ main:
push rbp
mov rbp, rsp
sub rsp, __main_size
;define variable i
mov rax, 0
mov [rsp+0], rax
;end define variable i
; enter loop
__tmp_label_0:
mov rax, 1
Expand Down Expand Up @@ -49,8 +51,10 @@ jmp __tmp_label_1
jmp __tmp_label_0
__tmp_label_1:
; exit loop
;define variable i
mov rax, 0
mov [rsp+8], rax
;end define variable i
; enter loop
__tmp_label_5:
mov rax, rsp
Expand Down Expand Up @@ -132,8 +136,10 @@ mov [rsp+56], rax
jmp __tmp_label_5
__tmp_label_6:
; exit loop
;define variable i
mov rax, 0
mov [rsp+8], rax
;end define variable i
; enter loop
__tmp_label_15:
mov rax, rsp
Expand Down Expand Up @@ -166,8 +172,10 @@ add rax, 0
mov [rsp+56], rax
call printf
mov [rsp+64], rax
;define variable j
mov rax, 0
mov [rsp+64], rax
;end define variable j
; enter loop
__tmp_label_19:
mov rax, rsp
Expand Down
6 changes: 6 additions & 0 deletions tests/output/inp_func_ptrs.c_asm_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ add rax, 0
mov [rsp+24], rax
call malloc
mov [rsp+32], rax
;define variable funcs
mov rax, [rsp+32]
mov [rsp+0], rax
;end define variable funcs
mov rax, rsp
add rax, 0
mov [rsp+40], rax
Expand Down Expand Up @@ -148,8 +150,10 @@ mov rax, mul
mov rbx, [rsp+88]
mov [rbx], rax
mov [rsp+112], rax
;define variable i
mov rax, 0
mov [rsp+120], rax
;end define variable i
; enter loop
__tmp_label_0:
mov rax, rsp
Expand Down Expand Up @@ -196,8 +200,10 @@ add rax, 0
mov [rsp+208], rax
call handle
mov [rsp+216], rax
;define variable res
mov rax, [rsp+216]
mov [rsp+144], rax
;end define variable res
mov rax, __temp_str_5
mov [rsp+224], rax
mov rax, rsp
Expand Down
2 changes: 2 additions & 0 deletions tests/output/inp_goto.c_asm_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ main:
push rbp
mov rbp, rsp
sub rsp, __main_size
;define variable a
mov rax, 2
mov [rsp+0], rax
;end define variable a
mov rax, __temp_str_1
mov [rsp+8], rax
mov rdi, [rsp+8]
Expand Down
Loading

0 comments on commit e271ca6

Please sign in to comment.