From: Peep P. <so...@us...> - 2004-03-28 18:05:51
|
Update of /cvsroot/agd/server/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31077 Modified Files: compile.c Log Message: Separated object loading and program compilation into load_object() and compile_prog() - allows for recompilation in situ; changed static string buffers to strings on the heap Index: compile.c =================================================================== RCS file: /cvsroot/agd/server/src/compile.c,v retrieving revision 1.17 retrieving revision 1.18 diff -u -d -r1.17 -r1.18 --- compile.c 21 Mar 2004 13:02:37 -0000 1.17 +++ compile.c 28 Mar 2004 17:54:29 -0000 1.18 @@ -3,9 +3,9 @@ #include "lex.h" /* for yylloc */ #include "lpc_incl.h" -static object_t *cob; -static function_t *curr_f; -static char *curr_fn; +static program_t *this_prog; +static function_t *curr_f; /* TODO: rename to curr_fun. */ +static char *this_file; int compile_errors, compile_warnings; @@ -15,7 +15,6 @@ numdfuns, numlfuns; array_t local_ids, global_ids; -/*static int locals_level;*/ static array_t *curr_block; static array_t block_history; @@ -221,7 +220,7 @@ case F_OR: SET_OPERATOR("||", 2, 0, 1, 1, 1); break; - case F_RANGE: + case F_INDEX: SET_OPERATOR("[]", 1, 0, 0, 1, 0); break; case F_POW: @@ -240,7 +239,8 @@ int check_operand(int operator, int opr1, int opr2) { int ret; - char buf[128]; + char buf[256]; /* Safe, since operator names + are set in the code. */ operator_t *op; if(opr1 == T_VOID) { @@ -251,6 +251,7 @@ if(!opr1) return; + /* Really necessary? */ memset(buf, 0, sizeof buf); op = &operator_table[operator-F_ADD]; @@ -258,15 +259,15 @@ (op->num_operand == 2 && opr2 && op->both_same_type && opr1 != opr2)) { switch(op->num_operand) { - case 1: - sprintf(buf, "wrong type argument to unary "); - break; - case 2: - sprintf(buf, "invalid operands to binary "); - break; - case 3: - sprintf(buf, "invalid operands to trinary "); - break; + case 1: + sprintf(buf, "wrong type argument to unary "); + break; + case 2: + sprintf(buf, "invalid operands to binary "); + break; + case 3: + sprintf(buf, "invalid operands to trinary "); + break; } sprintf(buf, "%s%s", buf, op->name); comp_error(buf); @@ -322,10 +323,10 @@ void add_function(def_id_t *idp, int lineno, array_t *code) { - if(idp->index < cob->prog->functions.length) { + if(idp->index < this_prog->functions.length) { function_t *f; /* Adding definition for a prototype. */ - f = cob->prog->functions.data[idp->index]; + f = this_prog->functions.data[idp->index]; f->type = idp->lpc_type; if(idp->args) f->args = *idp->args; @@ -340,23 +341,9 @@ if(code) curr_f->code = *code; curr_f->lineno = lineno; -#if 0 - if(idp->index == cob->prog->functions.length) { -#endif - array_push(&cob->prog->functions, curr_f); - /* This removes the need to generate a fun_table. */ - array_push(&cob->prog->fun_table, idp->name); -#if 0 - } else { /* index > length */ - cob->prog->functions.length = idp->index + 1; - alloc_array(&cob->prog->functions); - cob->prog->functions.data[idp->index] = curr_f; - - cob->prog->fun_table.length = idp->index + 1; - alloc_array(&cob->prog->fun_table); - cob->prog->fun_table.data[idp->index] = idp->name; - } -#endif + array_push(&this_prog->functions, curr_f); + /* This removes the need to generate a fun_table. */ + array_push(&this_prog->fun_table, idp->name); curr_f = NULL; } @@ -382,17 +369,25 @@ var = var->u.a->data[0]; } - init_var(var); + /*init_var(var);*/ if(type == ID_GVAR) { - array_push(&cob->variables, var); + /*array_push(&cob->variables, var);*/ + /* this_ob is set to the compiled object during compilation. */ + array_push(&this_ob->variables, var); } else /* LVAR */{ switch(var->type) { case T_INT: array_push(curr_block, (void *) F_PUSH_INT); + if(var->u.i) { + printf("add_varible(): var->u.i is not 0!\n"); + } array_push(curr_block, (void *) var->u.i); break; case T_STRING: array_push(curr_block, (void *) F_PUSH_STRING); + if(var->u.s) { + printf("add_variable(): var->u.s is not null!\n"); + } array_push(curr_block, (void *) var->u.s); break; case T_OBJECT: @@ -426,10 +421,6 @@ array_push(&block_history, curr_block); curr_block = type_xmalloc(array_t); init_array(curr_block); -/* if(!locals_level) { - free_array(&temp_variables, (void (*)(void*))free_var); - } - locals_level++;*/ } array_t add_block(array_t *stmt) @@ -448,11 +439,6 @@ curr_block = block_history.data[block_history.length]; /*block_history.data = realloc(block_history.data, block_history.length);*/ -/* if(locals_level > 0) - locals_level--; - if(!locals_level) { - free_array(&temp_nametable, (void (*)(void*))free_ntentry NULL); - }*/ } int compare_args(array_t *arg1, array_t *arg2) @@ -463,35 +449,38 @@ if(arg2->length < arg1->length) return -1; for(i=0;i<arg1->length;i++) { - if((int)arg1->data[i] != (int)arg2->data[i]) { +/* if(arg != (int)arg2->data[i] && (arg->type == T_INT && arg->u.i)) { return i; - } + }*/ } return 0; } void redeclaration_error(def_id_t *id, int new_type) { - char buf[256]; + char *buf, *buf2; + buf = xmalloc(strlen(id->name) + 16); sprintf(buf, "%s redeclared as ", id->name); switch(new_type) { case ID_ARG: - sprintf(buf, "%sfunction argument", buf); + buf2 = "function argument"; break; case ID_FUN: - sprintf(buf, "%slocal function", buf); + buf2 = "local function"; break; case ID_LVAR: - sprintf(buf, "%slocal variable", buf); + buf2 = "local variable"; break; case ID_GVAR: - sprintf(buf, "%sglobal variable", buf); + buf2 = "global variable"; break; default: - sprintf(buf, "%sidontknowwhat!", buf); + buf2 = "idontknowwhat!"; break; } + buf = strcat(buf, buf2); comp_error(buf); + xfree(buf); } /* This does the actual formatting. */ @@ -505,7 +494,7 @@ } /* buf = xmalloc(strlen(s) + 256); - s*/printf(/*buf, */"%s:%d:%d: %s\n", curr_fn, yylloc.line, yylloc.pos, s); + s*/printf(/*buf, */"%s:%d:%d: %s\n", this_file, yylloc.line, yylloc.pos, s); /* do_write(buf); xfree(buf);*/ } @@ -526,20 +515,16 @@ { compile_errors = 0; - init_array(&cob->prog->functions); - init_array(&cob->prog->fun_table); - init_array(&cob->variables); + init_array(&this_prog->functions); + init_array(&this_prog->fun_table); + init_array(&this_ob->variables); numlvars = numgvars = 0; numdfuns = numlfuns = 0; /*curr_f_check();*/ -/* init_array(&defined_names); - init_array(&temp_nametable); - init_array(&temp_variables);*/ free_array(&local_ids, free_id); - init_array(&block_history); yylloc.pos = 0; @@ -556,73 +541,54 @@ for(i=0;i<local_ids.length;i++) { def_id_t *id = local_ids.data[i]; if(id->type == ID_FUN_PROT && id->has_been_called) { - char buf[256]; - sprintf(buf, "Function %s has been called, but not defined", id->name); + char *buf; + buf = xmalloc(strlen(id->name) + 43); + sprintf(buf, "Function %s has been called, " + "but not defined", id->name); comp_error(buf); + xfree(buf); return 1; } } return 0; } -#define END_COMPILE() cob = NULL; start_with_newline = 0; actual_destruct(ob); xfree(file_name); return NULL -object_t *load_object(char *fn) +program_t *compile_prog(char *path) { - char *file_name, *our_fn; - object_t *ob; + char *real_path; + program_t *prog; + #ifdef DEBUG struct timeval tm, tm2; #endif - if(!legal_path(fn)) { - printf("load_object(): illegal path name \"%s\"!\n", fn); - return NULL; - } - - if(fn[strlen(fn)-2] != '.' && fn[strlen(fn)-1] != 'c') { - our_fn = xmalloc((strlen(fn) + 3)); - sprintf(our_fn, "%s.c", fn); - } else { - our_fn = stringdup(fn); - } + path = add_extension(path); #ifdef DEBUG if(conf.debuglevel > 1) gettimeofday(&tm, NULL); #endif - file_name = xmalloc(strlen(conf.lib_root) + strlen(our_fn) + 1); - sprintf(file_name, "%s%s", conf.lib_root, our_fn); - - ob = type_xmalloc(object_t); - memset(ob, 0, sizeof(object_t)); - ob->name = our_fn; - cob = ob; - - printf("compiling %s", ob->name); - if(conf.debuglevel) - printf(" (absolute path %s)", file_name); - printf("... "); - + real_path = absolute_path(path); + printf("compiling %s...", path); start_with_newline = 1; - if(access(file_name, R_OK)) { - printf("failed (file not accessable)\n"); - END_COMPILE(); - } - - yyin = fopen(file_name, "r"); + yyin = fopen(real_path, "r"); if(!yyin) { - printf("failed (can't open %s: %s)\n", file_name, strerror(errno)); - END_COMPILE(); + printf("failed (can't open %s: %s)\n", real_path, strerror(errno)); + this_prog = 0; + goto out; } - ob->prog = type_xmalloc(program_t); - memset(ob->prog, 0, sizeof(program_t)); + prog = type_xmalloc(program_t); + memset(prog, 0, sizeof(program_t)); + this_prog = prog; + parse_init(); - curr_fn = ob->name; + this_file = path; +/* curr_fn = ob->name;*/ -#if defined(DEBUG) && defined(YYDEBUG) +#if defined(DEBUG) && YYDEBUG if(conf.debuglevel > 4) { yydebug = 1; } @@ -637,37 +603,45 @@ compile_warnings == 1 ? "": "s"); } printf(" - failed\n"); - END_COMPILE(); + xfree(this_prog); + prog = NULL; + goto out; } if(check_fun_definitions()) { - END_COMPILE(); + xfree(this_prog); + prog = NULL; + goto out; } - ref_prog(ob->prog); - list_push(&all_objects, ob); + ref_prog(prog); #ifdef DEBUG printf("done"); if(conf.debuglevel > 1) { gettimeofday(&tm2, NULL); - printf(" in %d microseconds\n", tm2.tv_usec - tm.tv_usec); + printf(" in %d microseconds\n", abs(tm2.tv_usec - tm.tv_usec)); } else { putchar('\n'); } #else printf("done\n"); #endif - - if(conf.debuglevel > 1) - print_code(ob); - - apply(ob, "create", NULL); +#ifdef DEBUG + if(conf.debuglevel > 1) { + printf("Printing code for program %s:\n", path); + printf("Globals: \n"); + print_var_arr(&this_ob->variables); + printf("\n"); + print_code(prog); + } +#endif - curr_fn = NULL; - cob = NULL; - xfree(file_name); +out: + this_prog = NULL; + this_file = NULL; + xfree(real_path); start_with_newline = 0; - return ob; + return prog; } |