diff --git a/CHANGELOG.org b/CHANGELOG.org index 77b9a14..acf86d8 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -16,12 +16,22 @@ * Current ** Added +Added more generic logic to lookup_type +Added prim_types to simply the task of reducing search space to primitives ** Changed +Reorganized Primitive expression evaluation +Reorganized Collect_local to better match implementation +Broke out process_break +Changed ordering in declare_function to reduce stack operations +Converted weird_string collection into a simpler form ** Fixed +Fixed detection of locals to screen out all non-primitive name space collisions ** Removed +Removed redundent steps in Recursive statement +Removed several redundent steps in Collect_arguments * 0.3 - 2018-08-12 ** Added diff --git a/cc.h b/cc.h index c419a52..9e74146 100644 --- a/cc.h +++ b/cc.h @@ -67,6 +67,7 @@ struct token_list /* What types we have */ struct type* global_types; +struct type* prim_types; /* What we are currently working on */ struct token_list* global_token; diff --git a/cc_core.c b/cc_core.c index 7aaef88..b98aa56 100644 --- a/cc_core.c +++ b/cc_core.c @@ -551,21 +551,6 @@ struct token_list* bitwise_expr(struct token_list* out, struct token_list* funct { out = relational_expr(out, function); out = bitwise_expr_stub(out, function); - if(match("=", global_token->s)) - { - char* store; - if(match("]", global_token->prev->s) && match("char*", current_target->name)) - { - store = "STORE_CHAR\n"; - } - else - { - store = "STORE_INTEGER\n"; - } - - out = common_recursion(out, function, expression); - out = emit(store, out); - } return out; } @@ -597,6 +582,21 @@ struct token_list* primary_expr(struct token_list* out, struct token_list* funct struct token_list* expression(struct token_list* out, struct token_list* function) { out = bitwise_expr(out, function); + if(match("=", global_token->s)) + { + char* store; + if(match("]", global_token->prev->s) && match("char*", current_target->name)) + { + store = "STORE_CHAR\n"; + } + else + { + store = "STORE_INTEGER\n"; + } + + out = common_recursion(out, function, expression); + out = emit(store, out); + } return out; } @@ -605,10 +605,6 @@ struct token_list* expression(struct token_list* out, struct token_list* functio struct token_list* collect_local(struct token_list* out, struct token_list* function) { struct type* type_size = type_name(); - out = emit("# Defining local ", out); - out = emit(global_token->s, out); - out = emit("\n", out); - struct token_list* a = sym_declare(global_token->s, type_size, function->locals); if(match("main", function->s) && (NULL == function->locals)) { @@ -628,6 +624,11 @@ struct token_list* collect_local(struct token_list* out, struct token_list* func } function->locals = a; + + out = emit("# Defining local ", out); + out = emit(global_token->s, out); + out = emit("\n", out); + global_token = global_token->next; if(match("=", global_token->s)) @@ -857,6 +858,32 @@ struct token_list* return_result(struct token_list* out, struct token_list* func return out; } +struct token_list* process_break(struct token_list* out, struct token_list* function) +{ + if(NULL == break_target_head) + { + file_print("Not inside of a loop or case statement", stderr); + line_error(); + exit(EXIT_FAILURE); + } + struct token_list* i = function->locals; + while(i != break_frame) + { + if(NULL == i) break; + out = emit("POP_ebx\t# break_cleanup_locals\n", out); + i = i->next; + } + global_token = global_token->next; + out = emit("JUMP %", out); + out = emit(break_target_head, out); + out = emit(break_target_func, out); + out = emit("_", out); + out = emit(break_target_num, out); + out = emit("\n", out); + require_match("ERROR in statement\nMissing ;\n", ";"); + return out; +} + struct token_list* recursive_statement(struct token_list* out, struct token_list* function) { global_token = global_token->next; @@ -869,16 +896,15 @@ struct token_list* recursive_statement(struct token_list* out, struct token_list global_token = global_token->next; /* Clean up any locals added */ - struct token_list* i; - for(i = function->locals; frame != i; i = i->next) + if(!match("RETURN\n", out->s)) { - if(NULL == function->locals) return out; - if(!match("RETURN\n", out->s)) + struct token_list* i; + for(i = function->locals; frame != i; i = i->next) { out = emit( "POP_ebx\t# _recursive_statement_locals\n", out); } - function->locals = function->locals->next; } + function->locals = frame; return out; } @@ -900,7 +926,7 @@ struct token_list* recursive_statement(struct token_list* out, struct token_list * expr ; */ -struct type* lookup_type(char* s); +struct type* lookup_type(char* s, struct type* start); struct token_list* statement(struct token_list* out, struct token_list* function) { if(global_token->s[0] == '{') @@ -913,9 +939,7 @@ struct token_list* statement(struct token_list* out, struct token_list* function out = emit("\t#C goto label\n", out); global_token = global_token->next; } - else if(((NULL == sym_lookup(global_token->s, function->locals)) && - (NULL == sym_lookup(global_token->s, function->arguments)) && - (NULL != lookup_type(global_token->s))) || + else if((NULL != lookup_type(global_token->s, prim_types)) || match("struct", global_token->s)) { out = collect_local(out, function); @@ -955,27 +979,7 @@ struct token_list* statement(struct token_list* out, struct token_list* function } else if(match("break", global_token->s)) { - if(NULL == break_target_head) - { - file_print("Not inside of a loop or case statement", stderr); - line_error(); - exit(EXIT_FAILURE); - } - struct token_list* i = function->locals; - while(i != break_frame) - { - if(NULL == i) break; - out = emit("POP_ebx\t# break_cleanup_locals\n", out); - i = i->next; - } - global_token = global_token->next; - out = emit("JUMP %", out); - out = emit(break_target_head, out); - out = emit(break_target_func, out); - out = emit("_", out); - out = emit(break_target_num, out); - out = emit("\n", out); - require_match("ERROR in statement\nMissing ;\n", ";"); + out = process_break(out, function); } else if(match("continue", global_token->s)) { @@ -1001,8 +1005,8 @@ void collect_arguments(struct token_list* function) struct type* type_size = type_name(); if(global_token->s[0] == ')') { - /* deal with foo(int|char|void) */ - global_token = global_token->prev; + /* foo(int,char,void) doesn't need anything done */ + continue; } else if(global_token->s[0] != ',') { @@ -1022,12 +1026,10 @@ void collect_arguments(struct token_list* function) a->depth = function->arguments->depth - 4; } + global_token = global_token->next; function->arguments = a; } - /* foo(int,char,void) doesn't need anything done */ - global_token = global_token->next; - /* ignore trailing comma (needed for foo(bar(), 1); expressions*/ if(global_token->s[0] == ',') global_token = global_token->next; } @@ -1037,11 +1039,11 @@ void collect_arguments(struct token_list* function) struct token_list* declare_function(struct token_list* out) { current_count = 0; - struct token_list* func = sym_declare(global_token->prev->s, calloc(1, sizeof(struct type)), global_function_list); - collect_arguments(func); + struct token_list* func = sym_declare(global_token->prev->s, NULL, global_function_list); /* allow previously defined functions to be looked up */ global_function_list = func; + collect_arguments(func); /* If just a prototype don't waste time */ if(global_token->s[0] == ';') global_token = global_token->next; diff --git a/cc_strings.c b/cc_strings.c index bee4eff..6d01330 100644 --- a/cc_strings.c +++ b/cc_strings.c @@ -125,27 +125,26 @@ collect_regular_string_reset: /* Deal with non-human strings */ char* collect_weird_string(char* string) { - int i = 1; string_index = 1; int temp; char* table = "0123456789ABCDEF"; hold_string[0] = '\''; -collect_weird_string: +collect_weird_string_reset: + string = string + 1; hold_string[string_index] = ' '; - temp = escape_lookup(string + i); + temp = escape_lookup(string); hold_string[string_index + 1] = table[(temp >> 4)]; hold_string[string_index + 2] = table[(temp & 15)]; - if(string[i] == '\\') + if(string[0] == '\\') { - if(string[i + 1] == 'x') i = i + 2; - i = i + 1; + if(string[1] == 'x') string = string + 2; + string = string + 1; } - i = i + 1; string_index = string_index + 3; - if(string[i] != 0) goto collect_weird_string; + if(string[1] != 0) goto collect_weird_string_reset; hold_string[string_index] = ' '; hold_string[string_index + 1] = '0'; diff --git a/cc_types.c b/cc_types.c index 13cd172..71fb961 100644 --- a/cc_types.c +++ b/cc_types.c @@ -93,12 +93,13 @@ void initialize_types() c->next = e; a->next = c; global_types->next = a; + prim_types = global_types; } -struct type* lookup_type(char* s) +struct type* lookup_type(char* s, struct type* start) { struct type* i; - for(i = global_types; NULL != i; i = i->next) + for(i = start; NULL != i; i = i->next) { if(match(i->name, s)) { @@ -213,7 +214,7 @@ struct type* type_name() global_token = global_token->next; } - struct type* ret = lookup_type(global_token->s); + struct type* ret = lookup_type(global_token->s, global_types); if(NULL == ret && !structure) { diff --git a/test/test.answers b/test/test.answers index 407c9dd..c5d3e11 100644 --- a/test/test.answers +++ b/test/test.answers @@ -9,7 +9,7 @@ b45fae655b7f848b28ebdb8eb2e30ae789fbcf7920bc315395d53986bb1adae4 test/results/t d511db73158a9544a5b5f828a79751e3de8a04b81c143fd0c146fc22c938aa9f test/results/test08-binary 907e1808f2e2b15ac72ebf13898b15c678e68ebd43d673dcd0f408d907e7962f test/results/test09-binary ef179cd359ba1d61d45089e314cd4ac2069c8dc4dd7494d7c766344ea3c8cf88 test/results/test10-binary -f6b6383edcec936cb5e94f5311995750d4f55b8c0f37e57c97cb45e8a21a6a59 test/results/test100-binary +fb2de146153e048c787807b58938a5e8cce689fa14961012eb5d60e48d3b221f test/results/test100-binary 5aaf399fe706d4a8c85c121c75ada29a65c293b57c98e8999961a2ef0bab0d62 test/results/test11-binary 4f8111e73e07255ae203963438c82ea8bcff7474e1594b52b426c58a03cb30eb test/results/test12-binary dd74dabfdce8657ff440c1eef531cbf67a64854f2020d4d6bcb65c9cc2d199cb test/results/test13-binary diff --git a/test/test100/proof.answer b/test/test100/proof.answer index 43d45e0..4417380 100644 --- a/test/test100/proof.answer +++ b/test/test100/proof.answer @@ -1 +1 @@ -3b81051a91a25db55946dfc322a1531d9b862f8c7d72a1b2fb6d122d4b8ba92e test/test100/proof +c8256bddd114b551cdc173b7b46bc043bb85cbd48d8505a79d11cd3d2d7ae3b2 test/test100/proof