diff --git a/examples/inp5.c b/examples/inp5.c index 4c0df98..bba1bec 100644 --- a/examples/inp5.c +++ b/examples/inp5.c @@ -1,5 +1,6 @@ void printf(char *, ...); void *malloc(int sz); + int main(int argc, char **argv) { for (int i = 0; i < argc; i += 1) @@ -7,19 +8,19 @@ int main(int argc, char **argv) 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] = '!'; diff --git a/examples/inp6.c b/examples/inp6.c index 9a15113..1ce677c 100644 --- a/examples/inp6.c +++ b/examples/inp6.c @@ -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); diff --git a/examples/inp_linked_list.c b/examples/inp_linked_list.c index 2c4644a..1f59559 100644 --- a/examples/inp_linked_list.c +++ b/examples/inp_linked_list.c @@ -14,13 +14,13 @@ 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); + struct node *new = (struct node *)malloc(sizeof(struct node)); *((int *)(&new->next)) = 0; new->val = val; if (lst->first) diff --git a/lexer.c b/lexer.c index f608cb0..03103d1 100644 --- a/lexer.c +++ b/lexer.c @@ -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); } diff --git a/lexer.h b/lexer.h index 351c13a..d53a756 100644 --- a/lexer.h +++ b/lexer.h @@ -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 diff --git a/parser/expr/expr.c b/parser/expr/expr.c index b98fbf1..c89e28a 100644 --- a/parser/expr/expr.c +++ b/parser/expr/expr.c @@ -320,6 +320,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; @@ -346,6 +362,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; @@ -383,6 +429,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) diff --git a/parser/expr/expr.h b/parser/expr/expr.h index 44a7f4b..7a50709 100644 --- a/parser/expr/expr.h +++ b/parser/expr/expr.h @@ -14,6 +14,11 @@ typedef struct parser_node *right; } node_binary_op; +typedef struct +{ + parser_node *type; +} node_sizeof; + typedef struct { int op; diff --git a/tests/output/inp5.c_asm_output.txt b/tests/output/inp5.c_asm_output.txt index 8ae97a7..ac64581 100644 --- a/tests/output/inp5.c_asm_output.txt +++ b/tests/output/inp5.c_asm_output.txt @@ -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 @@ -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 diff --git a/tests/output/inp5.c_lex_output.txt b/tests/output/inp5.c_lex_output.txt index 26f8b2e..5b78706 100644 Binary files a/tests/output/inp5.c_lex_output.txt and b/tests/output/inp5.c_lex_output.txt differ diff --git a/tests/output/inp5.c_tree_output.txt b/tests/output/inp5.c_tree_output.txt index b015d26..deb55d5 100644 --- a/tests/output/inp5.c_tree_output.txt +++ b/tests/output/inp5.c_tree_output.txt @@ -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 @@ -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: @@ -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 diff --git a/tests/output/inp6.c_lex_output.txt b/tests/output/inp6.c_lex_output.txt index 5106b76..ab52e7c 100644 --- a/tests/output/inp6.c_lex_output.txt +++ b/tests/output/inp6.c_lex_output.txt @@ -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) diff --git a/tests/output/inp6.c_tree_output.txt b/tests/output/inp6.c_tree_output.txt index 63a7811..a8f66ad 100644 --- a/tests/output/inp6.c_tree_output.txt +++ b/tests/output/inp6.c_tree_output.txt @@ -34,7 +34,8 @@ Program( Function: Variable(malloc) Args: - Literal(Type: 34, Value: 16) + SizeOf: + struct MyStruct Type: Pointer of: struct MyStruct diff --git a/tests/output/inp_linked_list.c_lex_output.txt b/tests/output/inp_linked_list.c_lex_output.txt index 0c6151c..6d6e4e8 100644 --- a/tests/output/inp_linked_list.c_lex_output.txt +++ b/tests/output/inp_linked_list.c_lex_output.txt @@ -57,7 +57,11 @@ TKN_STAR TKN_R_PAREN TKN_ID(malloc) TKN_L_PAREN -TKN_LIT_INT(8) +TKN_SIZEOF +TKN_L_PAREN +TKN_STRUCT +TKN_ID(linked_list) +TKN_R_PAREN TKN_R_PAREN TKN_SEMICOLON TKN_RETURN @@ -88,7 +92,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(node) +TKN_R_PAREN TKN_R_PAREN TKN_SEMICOLON TKN_STAR diff --git a/tests/output/inp_linked_list.c_tree_output.txt b/tests/output/inp_linked_list.c_tree_output.txt index 9ed5c53..420a0b9 100644 --- a/tests/output/inp_linked_list.c_tree_output.txt +++ b/tests/output/inp_linked_list.c_tree_output.txt @@ -35,7 +35,8 @@ Program( Function: Variable(malloc) Args: - Literal(Type: 34, Value: 8) + SizeOf: + struct linked_list Type: Pointer of: struct linked_list @@ -64,7 +65,8 @@ Program( Function: Variable(malloc) Args: - Literal(Type: 34, Value: 16) + SizeOf: + struct node Type: Pointer of: struct node