+static bool ir_builder_gen_global(ir_builder *self, ir_value *global);
+
+static bool gen_global_field(ir_value *global)
+{
+ if (global->isconst)
+ {
+ ir_value *fld = global->constval.vpointer;
+ if (!fld) {
+ printf("Invalid field constant with no field: %s\n", global->name);
+ return false;
+ }
+
+ /* Now, in this case, a relocation would be impossible to code
+ * since it looks like this:
+ * .vector v = origin; <- parse error, wtf is 'origin'?
+ * .vector origin;
+ *
+ * But we will need a general relocation support later anyway
+ * for functions... might as well support that here.
+ */
+ if (!fld->code.globaladdr) {
+ printf("FIXME: Relocation support\n");
+ return false;
+ }
+
+ /* copy the field's value */
+ global->code.globaladdr = code_globals_add(code_globals_data[fld->code.globaladdr]);
+ }
+ else
+ {
+ prog_section_field fld;
+
+ fld.name = global->code.name;
+ fld.offset = code_fields_elements;
+ fld.type = global->fieldtype;
+
+ if (fld.type == TYPE_VOID) {
+ printf("Field is missing a type: %s\n", global->name);
+ return false;
+ }
+
+ if (code_fields_add(fld) < 0)
+ return false;
+
+ global->code.globaladdr = code_globals_add(fld.offset);
+ }
+ if (global->code.globaladdr < 0)
+ return false;
+ return true;
+}
+
+static bool gen_global_pointer(ir_value *global)
+{
+ if (global->isconst)
+ {
+ ir_value *target = global->constval.vpointer;
+ if (!target) {
+ printf("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.
+ */
+ return false;
+ }
+
+ /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
+ * void() foo; <- proto
+ * void() *fooptr = &foo;
+ * void() foo = { code }
+ */
+ if (!target->code.globaladdr) {
+ /* FIXME: Check for the constant nullptr ir_value!
+ * because then code.globaladdr being 0 is valid.
+ */
+ printf("FIXME: Relocation support\n");
+ return false;
+ }
+
+ global->code.globaladdr = code_globals_add(target->code.globaladdr);
+ }
+ else
+ {
+ global->code.globaladdr = code_globals_add(0);
+ }
+ if (global->code.globaladdr < 0)
+ return false;
+ return true;
+}
+
+static bool gen_function_code(ir_function *self)
+{
+ return false;
+}
+
+static bool gen_global_function(ir_builder *ir, ir_value *global)
+{
+ prog_section_function fun;
+ ir_function *irfun;
+
+ size_t i;
+
+ if (!global->isconst ||
+ !global->constval.vfunc)
+ {
+ printf("Invalid state of function-global: not constant: %s\n", global->name);
+ return false;
+ }
+
+ irfun = global->constval.vfunc;
+
+ fun.name = global->code.name;
+ fun.file = code_cachedstring(global->context.file);
+ fun.profile = 0; /* always 0 */
+ fun.nargs = irfun->params_count;
+
+ for (i = 0;i < 8; ++i) {
+ if (i >= fun.nargs)
+ fun.argsize[i] = 0;
+ else if (irfun->params[i] == TYPE_VECTOR)
+ fun.argsize[i] = 3;
+ else
+ fun.argsize[i] = 1;
+ }
+
+ fun.locals = irfun->locals_count;
+ fun.firstlocal = code_globals_elements;
+ for (i = 0; i < irfun->locals_count; ++i) {
+ if (!ir_builder_gen_global(ir, irfun->locals[i]))
+ return false;
+ }
+
+ fun.entry = code_statements_elements;
+ if (!gen_function_code(irfun))
+ return false;
+
+ return (code_functions_add(fun) >= 0);
+}