Skip to content

Commit

Permalink
feat: Implement sizeof operator #11
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Oct 21, 2024
1 parent d16b035 commit c5d1dc7
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 109 deletions.
7 changes: 4 additions & 3 deletions examples/inp5.c
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
void printf(char *, ...);
void *malloc(int sz);

int main(int argc, char **argv)
{
for (int i = 0; i < argc; i += 1)
{
printf("Arg %u: %s\n", i, argv[i]);
}

int *a = (int *)malloc(10 * 8);
int *a = (int *)malloc(10 * sizeof(int));
for (int i = 0; i < 10; i = i + 1)
a[i] = 2 * i;
for (int i = 0; i < 10; i += 1)
printf("%u\n", a[i]);

int **b = (int **)malloc(1024 * 8);
int **b = (int **)malloc(1024 * sizeof(int));
int c = 123;
b[0] = (&c);
*(b[0]) = *(b[0]) * 2;
printf("%u\n", c);

char *str = (char *)malloc(20);
char *str = (char *)malloc(20 * sizeof(char));
str[0] = 'h';
str[1] = 'i';
str[2] = '!';
Expand Down
2 changes: 1 addition & 1 deletion examples/inp6.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct MyStruct

int main()
{
struct MyStruct *m = (struct MyStruct *)malloc(16);
struct MyStruct *m = (struct MyStruct *)malloc(sizeof(struct MyStruct));
m->a=10;
m->b=20;
printf("%u %u\n", m->a, m->b);
Expand Down
6 changes: 3 additions & 3 deletions examples/inp_linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ struct linked_list

struct linked_list *new_list()
{
struct linked_list *ret = (struct linked_list *)malloc(8);
struct linked_list *ret = (struct linked_list *)malloc(sizeof(struct linked_list));
return ret;
}

void add_val(struct linked_list *lst, int val)
{
struct node *new = (struct node *)malloc(16);
*((int *)(&new->next)) = new;
struct node *new = (struct node *)malloc(sizeof(struct node));
*((int *)(&new->next)) = 0;
new->val = val;
if (lst->first)
{
Expand Down
2 changes: 2 additions & 0 deletions lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ typed_token *next_keyword_or_identifier(char **inp_ptr)
return new_simp_tkn(TKN_WHILE);
else if (strcmp(val, "struct") == 0)
return new_simp_tkn(TKN_STRUCT);
else if (strcmp(val, "sizeof") == 0)
return new_simp_tkn(TKN_SIZEOF);
else
return new_tkn(TKN_ID, val, ident_tkn_debug);
}
Expand Down
1 change: 1 addition & 0 deletions lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typed_token *tokenize(char *inp);
#define TKN_WHILE 8
#define TKN_STRUCT 9
#define TKN_ELSE 10
#define TKN_SIZEOF 11

// Single letter symbols
#define TKN_L_PAREN 16
Expand Down
48 changes: 48 additions & 0 deletions parser/expr/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ apply_result *cast_apply(parser_node *node, context *ctx)
return new_result(cast->val->apply(cast->val, ctx)->code, cast_type);
}

void sizeof_debug(int depth, parser_node *node)
{
node_sizeof *sz = (node_sizeof *)node->data;
printtabs(depth);
printf("SizeOf:\n");
sz->type->debug(depth + 1, sz->type);
}

apply_result *sizeof_apply(parser_node *node, context *ctx)
{
node_sizeof *size_of = (node_sizeof *)node->data;
general_type *type = ((node_type *)size_of->type->data)->type;
int sz = type->size(type, ctx);
return new_result(cc_asprintf("%u",sz), new_primitive_type("TKN_INT"));
}

parser_node *parse_paren(typed_token **tkns_ptr)
{
typed_token *tkn = *tkns_ptr;
Expand All @@ -259,6 +275,36 @@ parser_node *parse_paren(typed_token **tkns_ptr)
return NULL;
}

parser_node *parse_sizeof(typed_token **tkns_ptr)
{
typed_token *tkn = *tkns_ptr;
if (tkn->type_id == TKN_SIZEOF)
{
tkn = tkn->next;
if (tkn->type_id == TKN_L_PAREN)
{
tkn = tkn->next;
parser_node *type = parse_type(&tkn);
if (type)
{
if (tkn->type_id == TKN_R_PAREN)
{
tkn = tkn->next;
*tkns_ptr = tkn;
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_sizeof));
node->debug = sizeof_debug;
node->apply = sizeof_apply;
node_sizeof *cast = (node_sizeof *)node->data;
cast->type = type;
return node;
}
}
}
}
return NULL;
}

parser_node *parse_cast(typed_token **tkns_ptr)
{
typed_token *tkn = *tkns_ptr;
Expand Down Expand Up @@ -296,6 +342,8 @@ parser_node *parse_terminal(typed_token **tkns_ptr)

parser_node *curr = NULL;

if (!curr)
curr = parse_sizeof(&tkn);
if (!curr)
curr = parse_cast(&tkn);
if (!curr)
Expand Down
5 changes: 5 additions & 0 deletions parser/expr/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ typedef struct
parser_node *right;
} node_binary_op;

typedef struct
{
parser_node *type;
} node_sizeof;

typedef struct
{
int op;
Expand Down
88 changes: 46 additions & 42 deletions tests/output/inp5.c_asm_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ __temp_str_2 db `Arg %u: %s\n`, 0
__temp_str_3 db `%u\n`, 0
__temp_str_4 db `%u\n`, 0
__temp_str_5 db `%s`, 0
__main_size: equ 768
__main_size: equ 784
section .text
extern printf
extern malloc
Expand Down Expand Up @@ -306,114 +306,118 @@ mov [rsp+272], rax
call printf
mov [rsp+280], rax
mov rax, 20
mov rbx, 1
mul rbx
mov [rsp+288], rax
mov rdi, [rsp+288]
mov rax, [rsp+288]
mov [rsp+296], rax
mov rdi, [rsp+296]
mov rax, rsp
add rax, 0
mov [rsp+296], rax
call malloc
mov [rsp+304], rax
mov rax, [rsp+304]
call malloc
mov [rsp+312], rax
mov rax, [rsp+312]
mov [rsp+280], rax
mov rax, rsp
add rax, 280
mov [rsp+312], rax
mov [rsp+320], rax
mov rax, 0
mov rbx, 1
mul rbx
mov rbx, [rsp+280]
add rbx, rax
mov [rsp+320], rbx
mov [rsp+328], rbx
mov rax, [rbx]
mov [rsp+328], rax
mov rax, [rsp+328]
mov [rsp+336], rax
mov rax, [rsp+336]
mov rbx, 104
mov rax, 104
mov rbx, [rsp+320]
mov rbx, [rsp+328]
mov [rbx], rax
mov [rsp+329], rax
mov [rsp+337], rax
mov rax, rsp
add rax, 280
mov [rsp+330], rax
mov [rsp+338], rax
mov rax, 1
mov rbx, 1
mul rbx
mov rbx, [rsp+280]
add rbx, rax
mov [rsp+338], rbx
mov [rsp+346], rbx
mov rax, [rbx]
mov [rsp+346], rax
mov rax, [rsp+346]
mov [rsp+354], rax
mov rax, [rsp+354]
mov rbx, 105
mov rax, 105
mov rbx, [rsp+338]
mov rbx, [rsp+346]
mov [rbx], rax
mov [rsp+347], rax
mov [rsp+355], rax
mov rax, rsp
add rax, 280
mov [rsp+348], rax
mov [rsp+356], rax
mov rax, 2
mov rbx, 1
mul rbx
mov rbx, [rsp+280]
add rbx, rax
mov [rsp+356], rbx
mov [rsp+364], rbx
mov rax, [rbx]
mov [rsp+364], rax
mov rax, [rsp+364]
mov [rsp+372], rax
mov rax, [rsp+372]
mov rbx, 33
mov rax, 33
mov rbx, [rsp+356]
mov rbx, [rsp+364]
mov [rbx], rax
mov [rsp+365], rax
mov [rsp+373], rax
mov rax, rsp
add rax, 280
mov [rsp+366], rax
mov [rsp+374], rax
mov rax, 3
mov rbx, 1
mul rbx
mov rbx, [rsp+280]
add rbx, rax
mov [rsp+374], rbx
mov [rsp+382], rbx
mov rax, [rbx]
mov [rsp+382], rax
mov rax, [rsp+382]
mov [rsp+390], rax
mov rax, [rsp+390]
mov rbx, 10
mov rax, 10
mov rbx, [rsp+374]
mov rbx, [rsp+382]
mov [rbx], rax
mov [rsp+383], rax
mov [rsp+391], rax
mov rax, rsp
add rax, 280
mov [rsp+384], rax
mov [rsp+392], rax
mov rax, 4
mov rbx, 1
mul rbx
mov rbx, [rsp+280]
add rbx, rax
mov [rsp+392], rbx
mov [rsp+400], rbx
mov rax, [rbx]
mov [rsp+400], rax
mov rax, [rsp+400]
mov [rsp+408], rax
mov rax, [rsp+408]
mov rbx, 0
mov rax, 0
mov rbx, [rsp+392]
mov rbx, [rsp+400]
mov [rbx], rax
mov [rsp+401], rax
mov [rsp+409], rax
mov rax, __temp_str_5
mov [rsp+402], rax
mov [rsp+410], rax
mov rax, rsp
add rax, 280
mov [rsp+410], rax
mov rax, [rsp+280]
mov [rsp+418], rax
mov rdi, [rsp+402]
mov rsi, [rsp+418]
mov rax, [rsp+280]
mov [rsp+426], rax
mov rdi, [rsp+410]
mov rsi, [rsp+426]
mov rax, rsp
add rax, 0
mov [rsp+426], rax
call printf
mov [rsp+434], rax
call printf
mov [rsp+442], rax
mov rsp, rbp
pop rbp
ret
Expand Down
Binary file modified tests/output/inp5.c_lex_output.txt
Binary file not shown.
13 changes: 10 additions & 3 deletions tests/output/inp5.c_tree_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ Program(
Left:
Literal(Type: 34, Value: 10)
Right:
Literal(Type: 34, Value: 8)
SizeOf:
TKN_INT
Type:
Pointer of:
TKN_INT
Expand Down Expand Up @@ -161,7 +162,8 @@ Program(
Left:
Literal(Type: 34, Value: 1024)
Right:
Literal(Type: 34, Value: 8)
SizeOf:
TKN_INT
Type:
Pointer of:
Pointer of:
Expand Down Expand Up @@ -216,7 +218,12 @@ Program(
Function:
Variable(malloc)
Args:
Literal(Type: 34, Value: 20)
BinaryOp(Op: 78)
Left:
Literal(Type: 34, Value: 20)
Right:
SizeOf:
TKN_CHAR
Type:
Pointer of:
TKN_CHAR
Expand Down
6 changes: 5 additions & 1 deletion tests/output/inp6.c_lex_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ TKN_STAR
TKN_R_PAREN
TKN_ID(malloc)
TKN_L_PAREN
TKN_LIT_INT(16)
TKN_SIZEOF
TKN_L_PAREN
TKN_STRUCT
TKN_ID(MyStruct)
TKN_R_PAREN
TKN_R_PAREN
TKN_SEMICOLON
TKN_ID(m)
Expand Down
3 changes: 2 additions & 1 deletion tests/output/inp6.c_tree_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Program(
Function:
Variable(malloc)
Args:
Literal(Type: 34, Value: 16)
SizeOf:
struct MyStruct
Type:
Pointer of:
struct MyStruct
Expand Down
Loading

0 comments on commit c5d1dc7

Please sign in to comment.