X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=ir.c;h=3132685eae51c6bfa1c7550f4ca0aa6eac63c29d;hp=3a5cac7de0ade3a5cb7bd8281b69bf5a6357a847;hb=e606913187a6601a7183784c91322344d6a4206d;hpb=e3b3c2efe3a52dd22229927df319d80262bed2f1 diff --git a/ir.c b/ir.c index 3a5cac7..3132685 100644 --- a/ir.c +++ b/ir.c @@ -125,6 +125,14 @@ uint16_t type_ne_instr[TYPE_COUNT] = { MEM_VEC_FUNCTIONS(ir_value_vector, ir_value*, v) +static void irerror(lex_ctx ctx, const char *msg, ...) +{ + va_list ap; + va_start(ap, msg); + cvprintmsg(ctx, LVL_ERROR, "internal error", msg, ap); + va_end(ap); +} + /*********************************************************************** *IR Builder */ @@ -169,7 +177,7 @@ void ir_builder_delete(ir_builder* self) for (i = 0; i != self->globals_count; ++i) { ir_value_delete(self->globals[i]); } - MEM_VECTOR_CLEAR(self, fields); + MEM_VECTOR_CLEAR(self, globals); for (i = 0; i != self->fields_count; ++i) { ir_value_delete(self->fields[i]); } @@ -314,6 +322,8 @@ ir_function* ir_function_new(ir_builder* owner, int outtype) MEM_VECTOR_INIT(self, values); MEM_VECTOR_INIT(self, locals); + self->code_function_def = -1; + self->run_id = 0; return self; } @@ -408,7 +418,7 @@ ir_value* ir_function_create_local(ir_function *self, const char *name, int vtyp if (param && self->locals_count && self->locals[self->locals_count-1]->store != store_param) { - printf("cannot add parameters after adding locals\n"); + irerror(self->context, "cannot add parameters after adding locals\n"); return NULL; } @@ -649,7 +659,7 @@ ir_value* ir_value_vector_member(ir_value *self, unsigned int member) } else { - printf("invalid member access on %s\n", self->name); + irerror(self->context, "invalid member access on %s\n", self->name); return NULL; } @@ -969,7 +979,7 @@ bool ir_values_overlap(const ir_value *a, const ir_value *b) if (++la == enda) break; } - else if (lb->start < la->start) + else /* if (lb->start < la->start) actually <= */ { /* order: B A, move B forward * check if we hit the end with B @@ -994,9 +1004,9 @@ bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value if (target->store == store_value && (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC)) { - fprintf(stderr, "cannot store to an SSA value\n"); - fprintf(stderr, "trying to store: %s <- %s\n", target->name, what->name); - fprintf(stderr, "instruction: %s\n", asm_instr[op].m); + irerror(self->context, "cannot store to an SSA value\n"); + irerror(self->context, "trying to store: %s <- %s\n", target->name, what->name); + irerror(self->context, "instruction: %s\n", asm_instr[op].m); return false; } @@ -1060,7 +1070,7 @@ bool ir_block_create_return(ir_block *self, ir_value *v) { ir_instr *in; if (self->final) { - fprintf(stderr, "block already ended (%s)\n", self->label); + irerror(self->context, "block already ended (%s)\n", self->label); return false; } self->final = true; @@ -1069,11 +1079,11 @@ bool ir_block_create_return(ir_block *self, ir_value *v) if (!in) return false; - if (!ir_instr_op(in, 0, v, false) || - !ir_block_instr_add(self, in) ) - { + if (v && !ir_instr_op(in, 0, v, false)) + return false; + + if (!ir_block_instr_add(self, in)) return false; - } return true; } @@ -1082,7 +1092,7 @@ bool ir_block_create_if(ir_block *self, ir_value *v, { ir_instr *in; if (self->final) { - fprintf(stderr, "block already ended (%s)\n", self->label); + irerror(self->context, "block already ended (%s)\n", self->label); return false; } self->final = true; @@ -1116,7 +1126,7 @@ bool ir_block_create_jump(ir_block *self, ir_block *to) { ir_instr *in; if (self->final) { - fprintf(stderr, "block already ended (%s)\n", self->label); + irerror(self->context, "block already ended (%s)\n", self->label); return false; } self->final = true; @@ -1140,7 +1150,7 @@ bool ir_block_create_goto(ir_block *self, ir_block *to) { ir_instr *in; if (self->final) { - fprintf(stderr, "block already ended (%s)\n", self->label); + irerror(self->context, "block already ended (%s)\n", self->label); return false; } self->final = true; @@ -1198,7 +1208,7 @@ bool ir_phi_add(ir_instr* self, ir_block *b, ir_value *v) /* Must not be possible to cause this, otherwise the AST * is doing something wrong. */ - fprintf(stderr, "Invalid entry block for PHI\n"); + irerror(self->context, "Invalid entry block for PHI\n"); abort(); } @@ -1217,7 +1227,7 @@ ir_instr* ir_block_create_call(ir_block *self, const char *label, ir_value *func in = ir_instr_new(self, INSTR_CALL0); if (!in) return NULL; - out = ir_value_out(self->owner, label, store_return, func->outtype); + out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype); if (!out) { ir_instr_delete(in); return NULL; @@ -1438,11 +1448,12 @@ ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_va switch (outype) { - case TYPE_FLOAT: op = INSTR_LOAD_F; break; - case TYPE_VECTOR: op = INSTR_LOAD_V; break; - case TYPE_STRING: op = INSTR_LOAD_S; break; - case TYPE_FIELD: op = INSTR_LOAD_FLD; break; - case TYPE_ENTITY: op = INSTR_LOAD_ENT; break; + case TYPE_FLOAT: op = INSTR_LOAD_F; break; + case TYPE_VECTOR: op = INSTR_LOAD_V; break; + case TYPE_STRING: op = INSTR_LOAD_S; break; + case TYPE_FIELD: op = INSTR_LOAD_FLD; break; + case TYPE_ENTITY: op = INSTR_LOAD_ENT; break; + case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break; #if 0 case TYPE_POINTER: op = INSTR_LOAD_I; break; case TYPE_INTEGER: op = INSTR_LOAD_I; break; @@ -1835,7 +1846,7 @@ bool ir_function_allocate_locals(ir_function *self) function_allocator alloc; - if (!self->locals_count) + if (!self->locals_count && !self->values_count) return true; MEM_VECTOR_INIT(&alloc, locals); @@ -1898,8 +1909,9 @@ bool ir_function_allocate_locals(ir_function *self) self->allocated_locals = pos + alloc.sizes[alloc.sizes_count-1]; /* Take over the actual slot positions */ - for (i = 0; i < self->values_count; ++i) + for (i = 0; i < self->values_count; ++i) { self->values[i]->code.local = alloc.positions[self->values[i]->code.local]; + } goto cleanup; @@ -1937,6 +1949,15 @@ static void ir_op_read_write(int op, size_t *read, size_t *write) *write = 0; *read = 1; break; + case INSTR_STOREP_F: + case INSTR_STOREP_V: + case INSTR_STOREP_S: + case INSTR_STOREP_ENT: + case INSTR_STOREP_FLD: + case INSTR_STOREP_FNC: + *write = 0; + *read = 7; + break; default: *write = 1; *read = 6; @@ -1954,7 +1975,7 @@ static bool ir_block_living_add_instr(ir_block *self, size_t eid) tempbool = ir_value_life_merge(self->living[i], eid); /* debug if (tempbool) - fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid); + irerror(self->context, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid); */ changed = changed || tempbool; } @@ -1989,7 +2010,7 @@ static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *ch if (!ir_block_living_add(self, prev->living[i])) return false; /* - printf("%s got from prev: %s\n", self->label, prev->living[i]->_name); + irerror(self->contextt from prev: %s\n", self->label, prev->living[i]->_name); */ } return true; @@ -2044,6 +2065,25 @@ static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *change #endif } + /* call params are read operands too */ + for (p = 0; p < instr->params_count; ++p) + { + value = instr->params[p]; +#if ! defined(LIFE_RANGE_WITHOUT_LAST_READ) + if (!ir_block_living_find(self, value, NULL) && + !ir_block_living_add(self, value)) + { + goto on_error; + } +#else + if (!new_reads_t_v_find(&new_reads, value, NULL)) + { + if (!new_reads_t_v_add(&new_reads, value)) + goto on_error; + } +#endif + } + /* See which operands are read and write operands */ ir_op_read_write(instr->opcode, &read, &write); @@ -2107,7 +2147,7 @@ static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *change * since this function is run multiple times. */ /* For now: debug info: */ - fprintf(stderr, "Value only written %s\n", value->name); + /* fprintf(stderr, "Value only written %s\n", value->name); */ tempbool = ir_value_life_merge(value, instr->eid); *changed = *changed || tempbool; /* @@ -2203,7 +2243,7 @@ static bool gen_global_field(ir_value *global) { ir_value *fld = global->constval.vpointer; if (!fld) { - printf("Invalid field constant with no field: %s\n", global->name); + irerror(global->context, "Invalid field constant with no field: %s\n", global->name); return false; } @@ -2216,7 +2256,7 @@ static bool gen_global_field(ir_value *global) * for functions... might as well support that here. */ if (!fld->code.globaladdr) { - printf("FIXME: Relocation support\n"); + irerror(global->context, "FIXME: Relocation support\n"); return false; } @@ -2246,7 +2286,7 @@ static bool gen_global_pointer(ir_value *global) { ir_value *target = global->constval.vpointer; if (!target) { - printf("Invalid pointer constant: %s\n", global->name); + irerror(global->context, "Invalid pointer constant: %s\n", global->name); /* NULL pointers are pointing to the NULL constant, which also * sits at address 0, but still has an ir_value for itself. */ @@ -2262,7 +2302,7 @@ static bool gen_global_pointer(ir_value *global) /* FIXME: Check for the constant nullptr ir_value! * because then code.globaladdr being 0 is valid. */ - printf("FIXME: Relocation support\n"); + irerror(global->context, "FIXME: Relocation support\n"); return false; } @@ -2295,7 +2335,7 @@ tailcall: instr = block->instr[i]; if (instr->opcode == VINSTR_PHI) { - printf("cannot generate virtual instruction (phi)\n"); + irerror(block->context, "cannot generate virtual instruction (phi)\n"); return false; } @@ -2334,13 +2374,13 @@ tailcall: if (ontrue->generated) { stmt.opcode = INSTR_IF; - stmt.o2.s1 = (ontrue->code_start-1) - code_statements_elements; + stmt.o2.s1 = (ontrue->code_start) - code_statements_elements; if (code_statements_add(stmt) < 0) return false; } if (onfalse->generated) { stmt.opcode = INSTR_IFNOT; - stmt.o2.s1 = (onfalse->code_start-1) - code_statements_elements; + stmt.o2.s1 = (onfalse->code_start) - code_statements_elements; if (code_statements_add(stmt) < 0) return false; } @@ -2436,7 +2476,7 @@ tailcall: } if (instr->opcode == INSTR_STATE) { - printf("TODO: state instruction\n"); + irerror(block->context, "TODO: state instruction\n"); return false; } @@ -2485,7 +2525,7 @@ static bool gen_function_code(ir_function *self) * for now. Dead blocks will not be translated obviously. */ if (!self->blocks_count) { - printf("Function '%s' declared without body.\n", self->name); + irerror(self->context, "Function '%s' declared without body.\n", self->name); return false; } @@ -2494,7 +2534,7 @@ static bool gen_function_code(ir_function *self) return true; if (!gen_blocks_recursive(self, block)) { - printf("failed to generate blocks for '%s'\n", self->name); + irerror(self->context, "failed to generate blocks for '%s'\n", self->name); return false; } @@ -2518,7 +2558,7 @@ static bool gen_global_function(ir_builder *ir, ir_value *global) if (!global->isconst || (!global->constval.vfunc)) { - printf("Invalid state of function-global: not constant: %s\n", global->name); + irerror(global->context, "Invalid state of function-global: not constant: %s\n", global->name); return false; } @@ -2539,10 +2579,10 @@ static bool gen_global_function(ir_builder *ir, ir_value *global) fun.firstlocal = code_globals_elements; fun.locals = irfun->allocated_locals + irfun->locals_count; - local_var_end = 0; + local_var_end = fun.firstlocal; for (i = 0; i < irfun->locals_count; ++i) { if (!ir_builder_gen_global(ir, irfun->locals[i])) { - printf("Failed to generate global %s\n", irfun->locals[i]->name); + irerror(irfun->locals[i]->context, "Failed to generate local %s", irfun->locals[i]->name); return false; } } @@ -2557,7 +2597,7 @@ static bool gen_global_function(ir_builder *ir, ir_value *global) ir_value *v = irfun->values[i]; ir_value_code_setaddr(v, local_var_end + v->code.local); } - for (i = 0; i < irfun->locals_count; ++i) { + for (i = 0; i < irfun->allocated_locals; ++i) { /* fill the locals with zeros */ code_globals_add(0); } @@ -2565,16 +2605,42 @@ static bool gen_global_function(ir_builder *ir, ir_value *global) if (irfun->builtin) fun.entry = irfun->builtin; else { + irfun->code_function_def = code_functions_elements; fun.entry = code_statements_elements; + /* done in second pass: gen_global_function_code! if (!gen_function_code(irfun)) { - printf("Failed to generate code for function %s\n", irfun->name); + irerror(irfun->context, "Failed to generate code for function %s", irfun->name); return false; } + */ } return (code_functions_add(fun) >= 0); } +static bool gen_global_function_code(ir_builder *ir, ir_value *global) +{ + prog_section_function *fundef; + ir_function *irfun; + + irfun = global->constval.vfunc; + if (irfun->builtin) + return true; + + if (irfun->code_function_def < 0) { + irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name); + return false; + } + fundef = &code_functions_data[irfun->code_function_def]; + + fundef->entry = code_statements_elements; + if (!gen_function_code(irfun)) { + irerror(irfun->context, "Failed to generate code for function %s", irfun->name); + return false; + } + return true; +} + static bool ir_builder_gen_global(ir_builder *self, ir_value *global) { size_t i; @@ -2662,7 +2728,7 @@ static bool ir_builder_gen_global(ir_builder *self, ir_value *global) return true; default: /* refuse to create 'void' type or any other fancy business. */ - printf("Invalid type for global variable %s\n", global->name); + irerror(global->context, "Invalid type for global variable %s\n", global->name); return false; } } @@ -2685,12 +2751,12 @@ static bool ir_builder_gen_field(ir_builder *self, ir_value *field) * bytes is more than enough for a variable/field name */ if (len+2 >= sizeof(name)) { - printf("invalid field name size: %u\n", (unsigned int)len); + irerror(field->context, "invalid field name size: %u\n", (unsigned int)len); return false; } name[0] = '.'; - strcpy(name+1, field->name); /* no strncpy - we used strlen above */ + memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */ name[len+1] = 0; def.name = code_genstring(name); @@ -2713,7 +2779,7 @@ static bool ir_builder_gen_field(ir_builder *self, ir_value *field) fld.type = field->fieldtype; if (fld.type == TYPE_VOID) { - printf("field is missing a type: %s - don't know its size\n", field->name); + irerror(field->context, "field is missing a type: %s - don't know its size\n", field->name); return false; } @@ -2755,6 +2821,16 @@ bool ir_builder_generate(ir_builder *self, const char *filename) } } + /* generate function code */ + for (i = 0; i < self->globals_count; ++i) + { + if (self->globals[i]->vtype == TYPE_FUNCTION) { + if (!gen_global_function_code(self, self->globals[i])) { + return false; + } + } + } + printf("writing '%s'...\n", filename); return code_write(filename); } @@ -2765,6 +2841,12 @@ bool ir_builder_generate(ir_builder *self, const char *filename) #define IND_BUFSZ 1024 +#ifdef WIN32 +# define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE) +#else +# define strncat strncat +#endif + const char *qc_opname(int op) { if (op < 0) return "";