diff options
author | Teddy <[email protected]> | 2014-05-02 01:28:21 +0800 |
---|---|---|
committer | Teddy <[email protected]> | 2014-05-02 01:28:21 +0800 |
commit | 51e150b8240e77120b82c7f6815e9a784b64ceed (patch) | |
tree | f4aa6834f30923921499ff44f3e724207f0721c9 | |
parent | dc0b69f1d2c44f5a45e1ce9eb7f2885ab33cb335 (diff) |
complex eight-queen puzzle now works
-rw-r--r-- | main.c | 12 | ||||
-rw-r--r-- | semantics.c | 19 |
2 files changed, 28 insertions, 3 deletions
@@ -52,11 +52,23 @@ void print_sem() { /* cnode_debug_print(ast_root, 1); */ } +void lib_generate() { + FILE *f = fopen("lib.s", "r"); + static char buff[1024]; + if (f) + { + size_t size; + while ((size = fread(buff, 1, 1024, f))) + fwrite(buff, 1, size, stdout); + } +} + void print_ssa() { cibic_init(); yyparse(); semantics_check(ast_root); ssa_generate(); + lib_generate(); } void print_help() { diff --git a/semantics.c b/semantics.c index b694a8f..014f7e5 100644 --- a/semantics.c +++ b/semantics.c @@ -59,6 +59,9 @@ static CType_t basic_type_void; static CType_t builtin_printf; static CType_t builtin_scanf; static CType_t builtin_malloc; +static CType_t builtin_print_int; +static CType_t builtin_print_char; +static CType_t builtin_print_string; static int scnt = 0; CTList_t funcs; @@ -1523,7 +1526,7 @@ CVar_t semantics_comp(CNode *p, CScope_t scope) { CType_t semantics_func(CNode *p, CScope_t scope) { CHECK_TYPE(p, FUNC_DEF); CVar_t head; - CType_t func, efunc, rt; + CType_t func, efunc = NULL, rt; cscope_enter(scope); /* enter function local scope */ head = semantics_declr(p->chd->next, semantics_typespec(p->chd, scope), @@ -1558,7 +1561,6 @@ CType_t semantics_func(CNode *p, CScope_t scope) { else if (!is_same_type(efunc, func)) ERROR((func->ast, "function defintion does not match the prototype")); type_merge(efunc, func); - free(func); } scope->top = ntop; @@ -1573,7 +1575,12 @@ CType_t semantics_func(CNode *p, CScope_t scope) { } func->rec.func.local = semantics_comp(p->chd->next->next, scope); /* check comp */ cscope_exit(scope); /* exit from local scope */ - + if (efunc) + { + type_merge(efunc, func); + free(func); + func = efunc; + } return func; } @@ -1832,6 +1839,9 @@ void semantics_check(CNode *p) { basic_type_void = ctype_create("void", CVOID, NULL); builtin_printf = make_builtin_func("printf", basic_type_int); builtin_scanf = make_builtin_func("scanf", basic_type_int); + builtin_print_int = make_builtin_func("__print_int", basic_type_int); + builtin_print_char = make_builtin_func("__print_char", basic_type_int); + builtin_print_string = make_builtin_func("__print_string", basic_type_int); { CType_t vstar = ctype_create("", CPTR, NULL); vstar->rec.ref = basic_type_void; @@ -1844,6 +1854,9 @@ void semantics_check(CNode *p) { cscope_push_type(scope, builtin_printf, NS_ID); cscope_push_type(scope, builtin_scanf, NS_ID); cscope_push_type(scope, builtin_malloc, NS_ID); + cscope_push_type(scope, builtin_print_int, NS_ID); + cscope_push_type(scope, builtin_print_char, NS_ID); + cscope_push_type(scope, builtin_print_string, NS_ID); /* const string counter */ scnt = 0; /* check all definitions and declarations */ |