]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ir.c
fix an uninitialized value
[xonotic/gmqcc.git] / ir.c
diff --git a/ir.c b/ir.c
index 133aca5d66e179a60501fee268faa17365898fd1..7af6162d524876a290ee1ff6a1edd0cd279b8323 100644 (file)
--- a/ir.c
+++ b/ir.c
@@ -38,10 +38,11 @@ const char *type_name[TYPE_COUNT] = {
     "field",
     "function",
     "pointer",
-#if 0
     "integer",
-#endif
-    "variant"
+    "variant",
+    "struct",
+    "union",
+    "array"
 };
 
 size_t type_sizeof[TYPE_COUNT] = {
@@ -53,10 +54,11 @@ size_t type_sizeof[TYPE_COUNT] = {
     1, /* TYPE_FIELD    */
     1, /* TYPE_FUNCTION */
     1, /* TYPE_POINTER  */
-#if 0
     1, /* TYPE_INTEGER  */
-#endif
     3, /* TYPE_VARIANT  */
+    0, /* TYPE_STRUCT   */
+    0, /* TYPE_UNION    */
+    0, /* TYPE_ARRAY    */
 };
 
 uint16_t type_store_instr[TYPE_COUNT] = {
@@ -70,9 +72,37 @@ uint16_t type_store_instr[TYPE_COUNT] = {
     INSTR_STORE_ENT, /* should use I */
 #if 0
     INSTR_STORE_I, /* integer type */
+#else
+    INSTR_STORE_F,
 #endif
 
     INSTR_STORE_V, /* variant, should never be accessed */
+
+    AINSTR_END, /* struct */
+    AINSTR_END, /* union  */
+    AINSTR_END, /* array  */
+};
+
+uint16_t field_store_instr[TYPE_COUNT] = {
+    INSTR_STORE_FLD,
+    INSTR_STORE_FLD,
+    INSTR_STORE_FLD,
+    INSTR_STORE_V,
+    INSTR_STORE_FLD,
+    INSTR_STORE_FLD,
+    INSTR_STORE_FLD,
+    INSTR_STORE_FLD,
+#if 0
+    INSTR_STORE_FLD, /* integer type */
+#else
+    INSTR_STORE_FLD,
+#endif
+
+    INSTR_STORE_V, /* variant, should never be accessed */
+
+    AINSTR_END, /* struct */
+    AINSTR_END, /* union  */
+    AINSTR_END, /* array  */
 };
 
 uint16_t type_storep_instr[TYPE_COUNT] = {
@@ -86,9 +116,15 @@ uint16_t type_storep_instr[TYPE_COUNT] = {
     INSTR_STOREP_ENT, /* should use I */
 #if 0
     INSTR_STOREP_ENT, /* integer type */
+#else
+    INSTR_STOREP_F,
 #endif
 
     INSTR_STOREP_V, /* variant, should never be accessed */
+
+    AINSTR_END, /* struct */
+    AINSTR_END, /* union  */
+    AINSTR_END, /* array  */
 };
 
 uint16_t type_eq_instr[TYPE_COUNT] = {
@@ -102,9 +138,15 @@ uint16_t type_eq_instr[TYPE_COUNT] = {
     INSTR_EQ_E, /* should use I */
 #if 0
     INSTR_EQ_I,
+#else
+    INSTR_EQ_F,
 #endif
 
     INSTR_EQ_V, /* variant, should never be accessed */
+
+    AINSTR_END, /* struct */
+    AINSTR_END, /* union  */
+    AINSTR_END, /* array  */
 };
 
 uint16_t type_ne_instr[TYPE_COUNT] = {
@@ -118,9 +160,15 @@ uint16_t type_ne_instr[TYPE_COUNT] = {
     INSTR_NE_E, /* should use I */
 #if 0
     INSTR_NE_I,
+#else
+    INSTR_NE_F,
 #endif
 
     INSTR_NE_V, /* variant, should never be accessed */
+
+    AINSTR_END, /* struct */
+    AINSTR_END, /* union  */
+    AINSTR_END, /* array  */
 };
 
 MEM_VEC_FUNCTIONS(ir_value_vector, ir_value*, v)
@@ -155,6 +203,10 @@ static bool irwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
  *IR Builder
  */
 
+static void ir_block_delete_quick(ir_block* self);
+static void ir_instr_delete_quick(ir_instr *self);
+static void ir_function_delete_quick(ir_function *self);
+
 ir_builder* ir_builder_new(const char *modulename)
 {
     ir_builder* self;
@@ -189,7 +241,7 @@ void ir_builder_delete(ir_builder* self)
     size_t i;
     mem_d((void*)self->name);
     for (i = 0; i != self->functions_count; ++i) {
-        ir_function_delete(self->functions[i]);
+        ir_function_delete_quick(self->functions[i]);
     }
     MEM_VECTOR_CLEAR(self, functions);
     for (i = 0; i != self->globals_count; ++i) {
@@ -363,6 +415,30 @@ bool ir_function_set_name(ir_function *self, const char *name)
     return !!self->name;
 }
 
+static void ir_function_delete_quick(ir_function *self)
+{
+    size_t i;
+    mem_d((void*)self->name);
+
+    for (i = 0; i != self->blocks_count; ++i)
+        ir_block_delete_quick(self->blocks[i]);
+    MEM_VECTOR_CLEAR(self, blocks);
+
+    MEM_VECTOR_CLEAR(self, params);
+
+    for (i = 0; i != self->values_count; ++i)
+        ir_value_delete(self->values[i]);
+    MEM_VECTOR_CLEAR(self, values);
+
+    for (i = 0; i != self->locals_count; ++i)
+        ir_value_delete(self->locals[i]);
+    MEM_VECTOR_CLEAR(self, locals);
+
+    /* self->value is deleted by the builder */
+
+    mem_d(self);
+}
+
 void ir_function_delete(ir_function *self)
 {
     size_t i;
@@ -495,6 +571,19 @@ MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, entries)
 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, exits)
 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_value*, living)
 
+static void ir_block_delete_quick(ir_block* self)
+{
+    size_t i;
+    if (self->label) mem_d(self->label);
+    for (i = 0; i != self->instr_count; ++i)
+        ir_instr_delete_quick(self->instr[i]);
+    MEM_VECTOR_CLEAR(self, instr);
+    MEM_VECTOR_CLEAR(self, entries);
+    MEM_VECTOR_CLEAR(self, exits);
+    MEM_VECTOR_CLEAR(self, living);
+    mem_d(self);
+}
+
 void ir_block_delete(ir_block* self)
 {
     size_t i;
@@ -545,6 +634,13 @@ ir_instr* ir_instr_new(ir_block* owner, int op)
 MEM_VEC_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
 MEM_VEC_FUNCTIONS(ir_instr, ir_value*, params)
 
+static void ir_instr_delete_quick(ir_instr *self)
+{
+    MEM_VECTOR_CLEAR(self, phi);
+    MEM_VECTOR_CLEAR(self, params);
+    mem_d(self);
+}
+
 void ir_instr_delete(ir_instr *self)
 {
     size_t i;
@@ -1497,6 +1593,7 @@ ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_va
         case TYPE_INTEGER: op = INSTR_LOAD_I;   break;
 #endif
         default:
+            irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
             return NULL;
     }
 
@@ -1513,6 +1610,7 @@ ir_value* ir_block_create_add(ir_block *self,
     if (l == r) {
         switch (l) {
             default:
+                irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
                 return NULL;
             case TYPE_FLOAT:
                 op = INSTR_ADD_F;
@@ -1534,7 +1632,10 @@ ir_value* ir_block_create_add(ir_block *self,
             op = INSTR_ADD_IF;
         else
 #endif
+        {
+            irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
             return NULL;
+        }
     }
     return ir_block_create_binop(self, label, op, left, right);
 }
@@ -1550,6 +1651,7 @@ ir_value* ir_block_create_sub(ir_block *self,
 
         switch (l) {
             default:
+                irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
                 return NULL;
             case TYPE_FLOAT:
                 op = INSTR_SUB_F;
@@ -1571,7 +1673,10 @@ ir_value* ir_block_create_sub(ir_block *self,
             op = INSTR_SUB_IF;
         else
 #endif
+        {
+            irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
             return NULL;
+        }
     }
     return ir_block_create_binop(self, label, op, left, right);
 }
@@ -1587,6 +1692,7 @@ ir_value* ir_block_create_mul(ir_block *self,
 
         switch (l) {
             default:
+                irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
                 return NULL;
             case TYPE_FLOAT:
                 op = INSTR_MUL_F;
@@ -1615,8 +1721,10 @@ ir_value* ir_block_create_mul(ir_block *self,
         else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
             op = INSTR_MUL_IF;
 #endif
-        else
+        else {
+            irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
             return NULL;
+        }
     }
     return ir_block_create_binop(self, label, op, left, right);
 }
@@ -1632,6 +1740,7 @@ ir_value* ir_block_create_div(ir_block *self,
 
         switch (l) {
             default:
+                irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
                 return NULL;
             case TYPE_FLOAT:
                 op = INSTR_DIV_F;
@@ -1652,7 +1761,10 @@ ir_value* ir_block_create_div(ir_block *self,
             op = INSTR_DIV_IF;
         else
 #endif
+        {
+            irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
             return NULL;
+        }
     }
     return ir_block_create_binop(self, label, op, left, right);
 }
@@ -2454,7 +2566,10 @@ tailcall:
                 stmt.opcode = INSTR_STORE_F;
                 stmt.o3.u1 = 0;
 
-                stmt.opcode = type_store_instr[param->vtype];
+                if (param->vtype == TYPE_FIELD)
+                    stmt.opcode = field_store_instr[param->fieldtype];
+                else
+                    stmt.opcode = type_store_instr[param->vtype];
                 stmt.o1.u1 = ir_value_code_addr(param);
                 stmt.o2.u1 = OFS_PARM0 + 3 * p;
                 if (code_statements_add(stmt) < 0)
@@ -2473,7 +2588,10 @@ tailcall:
             if (retvalue && retvalue->store != store_return && retvalue->life_count)
             {
                 /* not to be kept in OFS_RETURN */
-                stmt.opcode = type_store_instr[retvalue->vtype];
+                if (retvalue->vtype == TYPE_FIELD)
+                    stmt.opcode = field_store_instr[retvalue->vtype];
+                else
+                    stmt.opcode = type_store_instr[retvalue->vtype];
                 stmt.o1.u1 = OFS_RETURN;
                 stmt.o2.u1 = ir_value_code_addr(retvalue);
                 stmt.o3.u1 = 0;
@@ -2731,7 +2849,7 @@ static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool isloc
     case TYPE_FLOAT:
     {
         if (global->isconst) {
-            iptr = (int32_t*)&global->constval.vfloat;
+            iptr = (int32_t*)&global->constval.ivec[0];
             ir_value_code_setaddr(global, code_globals_add(*iptr));
         } else {
             ir_value_code_setaddr(global, code_globals_add(0));
@@ -2760,7 +2878,7 @@ static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool isloc
     {
         size_t d;
         if (global->isconst) {
-            iptr = (int32_t*)&global->constval.vvec;
+            iptr = (int32_t*)&global->constval.ivec[0];
             ir_value_code_setaddr(global, code_globals_add(iptr[0]));
             if (global->code.globaladdr < 0)
                 return false;
@@ -2938,8 +3056,6 @@ bool ir_builder_generate(ir_builder *self, const char *filename)
 
 #ifdef WIN32
 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
-#else
-# define strncat strncat
 #endif
 
 const char *qc_opname(int op)