]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ir.c
ir_function_create_block now takes a lex_ctx instead of copying from the function...
[xonotic/gmqcc.git] / ir.c
diff --git a/ir.c b/ir.c
index 70004b87bd87b38c1d045af05f9f3bad4414048f..f0a8a84fbd6d51e618b21d0e8aac80f7bb998755 100644 (file)
--- a/ir.c
+++ b/ir.c
@@ -284,6 +284,9 @@ ir_builder* ir_builder_new(const char *modulename)
     self->extparams   = NULL;
     self->filenames   = NULL;
     self->filestrings = NULL;
+    self->htglobals   = util_htnew(IR_HT_SIZE);
+    self->htfields    = util_htnew(IR_HT_SIZE);
+    self->htfunctions = util_htnew(IR_HT_SIZE);
 
     self->str_immediate = 0;
     self->name = NULL;
@@ -298,6 +301,9 @@ ir_builder* ir_builder_new(const char *modulename)
 void ir_builder_delete(ir_builder* self)
 {
     size_t i;
+    util_htdel(self->htglobals);
+    util_htdel(self->htfields);
+    util_htdel(self->htfunctions);
     mem_d((void*)self->name);
     for (i = 0; i != vec_size(self->functions); ++i) {
         ir_function_delete_quick(self->functions[i]);
@@ -330,12 +336,7 @@ bool ir_builder_set_name(ir_builder *self, const char *name)
 
 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
 {
-    size_t i;
-    for (i = 0; i < vec_size(self->functions); ++i) {
-        if (!strcmp(name, self->functions[i]->name))
-            return self->functions[i];
-    }
-    return NULL;
+    return (ir_function*)util_htget(self->htfunctions, name);
 }
 
 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
@@ -352,6 +353,7 @@ ir_function* ir_builder_create_function(ir_builder *self, const char *name, int
         return NULL;
     }
     vec_push(self->functions, fn);
+    util_htset(self->htfunctions, name, fn);
 
     fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
     if (!fn->value) {
@@ -369,12 +371,7 @@ ir_function* ir_builder_create_function(ir_builder *self, const char *name, int
 
 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
 {
-    size_t i;
-    for (i = 0; i < vec_size(self->globals); ++i) {
-        if (!strcmp(self->globals[i]->name, name))
-            return self->globals[i];
-    }
-    return NULL;
+    return (ir_value*)util_htget(self->htglobals, name);
 }
 
 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
@@ -391,17 +388,13 @@ ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype
 
     ve = ir_value_var(name, store_global, vtype);
     vec_push(self->globals, ve);
+    util_htset(self->htglobals, name, ve);
     return ve;
 }
 
 ir_value* ir_builder_get_field(ir_builder *self, const char *name)
 {
-    size_t i;
-    for (i = 0; i < vec_size(self->fields); ++i) {
-        if (!strcmp(self->fields[i]->name, name))
-            return self->fields[i];
-    }
-    return NULL;
+    return (ir_value*)util_htget(self->htfields, name);
 }
 
 
@@ -415,6 +408,7 @@ ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
     ve = ir_value_var(name, store_global, TYPE_FIELD);
     ve->fieldtype = vtype;
     vec_push(self->fields, ve);
+    util_htset(self->htfields, name, ve);
     return ve;
 }
 
@@ -522,10 +516,10 @@ void ir_function_collect_value(ir_function *self, ir_value *v)
     vec_push(self->values, v);
 }
 
-ir_block* ir_function_create_block(ir_function *self, const char *label)
+ir_block* ir_function_create_block(lex_ctx ctx, ir_function *self, const char *label)
 {
     ir_block* bn = ir_block_new(self, label);
-    memcpy(&bn->context, &self->context, sizeof(self->context));
+    bn->context = ctx;
     vec_push(self->blocks, bn);
     return bn;
 }
@@ -547,25 +541,10 @@ bool ir_function_finalize(ir_function *self)
     return true;
 }
 
-ir_value* ir_function_get_local(ir_function *self, const char *name)
-{
-    size_t i;
-    for (i = 0; i < vec_size(self->locals); ++i) {
-        if (!strcmp(self->locals[i]->name, name))
-            return self->locals[i];
-    }
-    return NULL;
-}
-
 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
 {
     ir_value *ve;
 
-    /*
-    if (ir_function_get_local(self, name))
-        return NULL;
-    */
-
     if (param &&
         vec_size(self->locals) &&
         self->locals[vec_size(self->locals)-1]->store != store_param) {
@@ -2170,6 +2149,9 @@ static bool ir_block_living_add_instr(ir_block *self, size_t eid)
 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
 {
     size_t i;
+
+    (void)changed;
+
     /* values which have been read in a previous iteration are now
      * in the "living" array even if the previous block doesn't use them.
      * So we have to remove whatever does not exist in the previous block.
@@ -2871,6 +2853,8 @@ static bool gen_global_function_code(ir_builder *ir, ir_value *global)
     prog_section_function *fundef;
     ir_function           *irfun;
 
+    (void)ir;
+
     irfun = global->constval.vfunc;
     if (!irfun) {
         irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
@@ -3045,8 +3029,10 @@ static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
     prog_section_def def;
     prog_section_field fld;
 
-    def.type   = field->vtype;
-    def.offset = vec_size(code_globals);
+    (void)self;
+
+    def.type   = (uint16_t)field->vtype;
+    def.offset = (uint16_t)vec_size(code_globals);
 
     /* create a global named the same as the field */
     if (opts_standard == COMPILER_GMQCC) {
@@ -3134,6 +3120,11 @@ bool ir_builder_generate(ir_builder *self, const char *filename)
         }
     }
 
+    if (vec_size(code_globals) >= 65536) {
+        irerror(vec_last(self->globals)->context, "This progs file would require more globals than the metadata can handle. Bailing out.");
+        return false;
+    }
+
     /* DP errors if the last instruction is not an INSTR_DONE
      * and for debugging purposes we add an additional AINSTR_END
      * to the end of functions, so here it goes:
@@ -3162,7 +3153,7 @@ bool ir_builder_generate(ir_builder *self, const char *filename)
 const char *qc_opname(int op)
 {
     if (op < 0) return "<INVALID>";
-    if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
+    if (op < (int)( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
         return asm_instr[op].m;
     switch (op) {
         case VINSTR_PHI:  return "PHI";
@@ -3258,8 +3249,7 @@ void ir_block_dump(ir_block* b, char *ind,
     ind[strlen(ind)-1] = 0;
 }
 
-void dump_phi(ir_instr *in, char *ind,
-              int (*oprintf)(const char*, ...))
+void dump_phi(ir_instr *in, int (*oprintf)(const char*, ...))
 {
     size_t i;
     oprintf("%s <- phi ", in->_ops[0]->name);
@@ -3280,7 +3270,7 @@ void ir_instr_dump(ir_instr *in, char *ind,
     oprintf("%s (%i) ", ind, (int)in->eid);
 
     if (in->opcode == VINSTR_PHI) {
-        dump_phi(in, ind, oprintf);
+        dump_phi(in, oprintf);
         return;
     }