From faa98d81985773fa86f4508e6b8875b5c45990da Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Wed, 26 Jan 2022 19:30:25 +0900 Subject: [PATCH] [gamecode] Use a struct for offset/count pairs This cleans up dprograms_t, making it easier to read and see what chunks are in it (I was surprised to see only 6, the explicit pairs made it seem to have more). --- include/QF/progs/pr_comp.h | 30 +++++------ libs/gamecode/pr_builtins.c | 4 +- libs/gamecode/pr_debug.c | 24 ++++----- libs/gamecode/pr_exec.c | 6 +-- libs/gamecode/pr_load.c | 38 ++++++------- libs/gamecode/pr_parse.c | 4 +- libs/gamecode/pr_resolve.c | 4 +- libs/gamecode/pr_strings.c | 2 +- libs/gamecode/pr_v6p_opcode.c | 12 ++--- libs/gamecode/test/main.c | 2 +- ruamoko/qwaq/builtins/debug.c | 8 +-- tools/qfcc/source/disassemble.c | 2 +- tools/qfcc/source/dump_globals.c | 18 +++---- tools/qfcc/source/dump_lines.c | 2 +- tools/qfcc/source/dump_modules.c | 2 +- tools/qfcc/source/dump_strings.c | 2 +- tools/qfcc/source/idstuff.c | 12 ++--- tools/qfcc/source/obj_file.c | 92 ++++++++++++++++---------------- tools/qfcc/source/qfcc.c | 20 +++---- tools/qfcc/source/qfprogs.c | 2 +- 20 files changed, 140 insertions(+), 146 deletions(-) diff --git a/include/QF/progs/pr_comp.h b/include/QF/progs/pr_comp.h index 88a644f62..4ae88e18e 100644 --- a/include/QF/progs/pr_comp.h +++ b/include/QF/progs/pr_comp.h @@ -552,27 +552,21 @@ typedef struct pr_va_list_s { #define PROG_V6P_VERSION PROG_VERSION_ENCODE(0,fff,00a) #define PROG_VERSION PROG_VERSION_ENCODE(0,fff,010) +typedef struct pr_chunk_s { + pr_uint_t offset; + pr_uint_t count; +} pr_chunk_t; + typedef struct dprograms_s { pr_uint_t version; - pr_uint_t crc; // check of header file + pr_uint_t crc; // checksum of header file - pr_uint_t ofs_statements; - pr_uint_t numstatements; // statement 0 is an error - - pr_uint_t ofs_globaldefs; - pr_uint_t numglobaldefs; - - pr_uint_t ofs_fielddefs; - pr_uint_t numfielddefs; - - pr_uint_t ofs_functions; - pr_uint_t numfunctions; // function 0 is an empty - - pr_uint_t ofs_strings; - pr_uint_t numstrings; // first string is a null string - - pr_uint_t ofs_globals; - pr_uint_t numglobals; + pr_chunk_t statements; // statement 0 is an error + pr_chunk_t globaldefs; + pr_chunk_t fielddefs; + pr_chunk_t functions; // function 0 is an empty + pr_chunk_t strings; // first string is a null string, count is bytes + pr_chunk_t globals; pr_uint_t entityfields; } dprograms_t; diff --git a/libs/gamecode/pr_builtins.c b/libs/gamecode/pr_builtins.c index ab5088b9b..fcccb57a4 100644 --- a/libs/gamecode/pr_builtins.c +++ b/libs/gamecode/pr_builtins.c @@ -188,10 +188,10 @@ PR_RelocateBuiltins (progs_t *pr) if (pr->function_table) free (pr->function_table); - pr->function_table = calloc (pr->progs->numfunctions, + pr->function_table = calloc (pr->progs->functions.count, sizeof (bfunction_t)); - for (i = 1; i < pr->progs->numfunctions; i++) { + for (i = 1; i < pr->progs->functions.count; i++) { desc = pr->pr_functions + i; func = pr->function_table + i; diff --git a/libs/gamecode/pr_debug.c b/libs/gamecode/pr_debug.c index a30aa8c66..9eff304dd 100644 --- a/libs/gamecode/pr_debug.c +++ b/libs/gamecode/pr_debug.c @@ -549,12 +549,12 @@ PR_DebugSetSym (progs_t *pr, pr_debug_header_t *debug) res->debug_data = (pr_type_t*)((char*)debug + debug->debug_data); size_t size; - size = pr->progs->numfunctions * sizeof (pr_auxfunction_t *); + size = pr->progs->functions.count * sizeof (pr_auxfunction_t *); res->auxfunction_map = pr->allocate_progs_mem (pr, size); - size = pr->progs->numfunctions * sizeof (pr_func_t); + size = pr->progs->functions.count * sizeof (pr_func_t); res->sorted_functions = pr->allocate_progs_mem (pr, size); - for (pr_uint_t i = 0; i < pr->progs->numfunctions; i++) { + for (pr_uint_t i = 0; i < pr->progs->functions.count; i++) { res->auxfunction_map[i] = 0; res->sorted_functions[i] = i; } @@ -575,7 +575,7 @@ PR_DebugSetSym (progs_t *pr, pr_debug_header_t *debug) res->auxfunction_map[res->auxfunctions[i].function] = &res->auxfunctions[i]; } - qsort_r (res->sorted_functions, pr->progs->numfunctions, + qsort_r (res->sorted_functions, pr->progs->functions.count, sizeof (pr_func_t), func_compare_sort, res); for (pr_uint_t i = 0; i < debug->num_locals; i++) { @@ -733,7 +733,7 @@ VISIBLE pr_auxfunction_t * PR_Debug_MappedAuxFunction (progs_t *pr, pr_uint_t func) { prdeb_resources_t *res = pr->pr_debug_resources; - if (!res->debug || func >= pr->progs->numfunctions) { + if (!res->debug || func >= pr->progs->functions.count) { return 0; } return res->auxfunction_map[func]; @@ -841,7 +841,7 @@ PR_FindSourceLineAddr (progs_t *pr, const char *file, pr_uint_t line) prdeb_resources_t *res = pr->pr_debug_resources; func_key_t key = { file, line }; pr_func_t *f = fbsearch_r (&key, res->sorted_functions, - pr->progs->numfunctions, sizeof (pr_func_t), + pr->progs->functions.count, sizeof (pr_func_t), func_compare_search, res); if (!f) { return 0; @@ -878,7 +878,7 @@ PR_Get_Source_File (progs_t *pr, pr_lineno_t *lineno) pr_auxfunction_t *f; f = PR_Get_Lineno_Func (pr, lineno); - if (f->function >= (unsigned) pr->progs->numfunctions) + if (f->function >= (unsigned) pr->progs->functions.count) return 0; return PR_GetString(pr, pr->pr_functions[f->function].file); } @@ -1113,7 +1113,7 @@ pr_debug_find_def (progs_t *pr, pr_ptr_t *ofs) prdeb_resources_t *res = pr->pr_debug_resources; pr_def_t *def = 0; - if (*ofs >= pr->progs->numglobals) { + if (*ofs >= pr->progs->globals.count) { return 0; } if (pr_debug->int_val && res->debug) { @@ -1306,7 +1306,7 @@ pr_debug_func_view (qfot_type_t *type, pr_type_t *value, void *_data) progs_t *pr = data->pr; dstring_t *dstr = data->dstr; - if (value->func_var >= pr->progs->numfunctions) { + if (value->func_var >= pr->progs->functions.count) { dasprintf (dstr, "INVALID:%d", value->func_var); } else if (!value->func_var) { dstring_appendstr (dstr, "NULL"); @@ -1652,7 +1652,7 @@ PR_PrintStatement (progs_t *pr, dstatement_t *s, int contents) str = global_string (&data, opval, optype, contents & 1); func = G_FUNCTION (pr, opval); - if (func < pr->progs->numfunctions) { + if (func < pr->progs->functions.count) { call_func = pr->pr_functions + func; } break; @@ -1790,7 +1790,7 @@ PR_Profile (progs_t * pr) do { max = 0; best = NULL; - for (i = 0; i < pr->progs->numfunctions; i++) { + for (i = 0; i < pr->progs->functions.count; i++) { bf = &pr->function_table[i]; if (bf->profile > max) { max = bf->profile; @@ -1862,7 +1862,7 @@ ED_Print (progs_t *pr, edict_t *ed, const char *fieldname) } return; } - for (i = 0; i < pr->progs->numfielddefs; i++) { + for (i = 0; i < pr->progs->fielddefs.count; i++) { d = &pr->pr_fielddefs[i]; if (!d->name) // null field def (probably 1st) continue; diff --git a/libs/gamecode/pr_exec.c b/libs/gamecode/pr_exec.c index b0a81c376..3c0734617 100644 --- a/libs/gamecode/pr_exec.c +++ b/libs/gamecode/pr_exec.c @@ -1459,7 +1459,7 @@ pr_exec_quakec (progs_t *pr, int exitdepth) break; case OP_JUMP_v6p: if (pr_boundscheck->int_val - && (OPA(uint) >= pr->progs->numstatements)) { + && (OPA(uint) >= pr->progs->statements.count)) { PR_RunError (pr, "Invalid jump destination"); } pr->pr_xstatement = OPA(uint) - 1; // offset the st++ @@ -1473,7 +1473,7 @@ pr_exec_quakec (progs_t *pr, int exitdepth) ptr = pr->pr_globals + pointer; pointer = ptr->int_var; if (pr_boundscheck->int_val - && (pointer >= pr->progs->numstatements)) { + && (pointer >= pr->progs->statements.count)) { PR_RunError (pr, "Invalid jump destination"); } pr->pr_xstatement = pointer - 1; // offset the st++ @@ -1874,7 +1874,7 @@ pr_jump_mode (progs_t *pr, const dstatement_t *st, int jump_ind) jump_offs = OPA(ptr) + OPB(int); break; } - if (pr_boundscheck->int_val && jump_offs >= pr->progs->numstatements) { + if (pr_boundscheck->int_val && jump_offs >= pr->progs->statements.count) { PR_RunError (pr, "out of bounds: %x", jump_offs); } return jump_offs - 1; // for st++ diff --git a/libs/gamecode/pr_load.c b/libs/gamecode/pr_load.c index 6d7116856..165ca789b 100644 --- a/libs/gamecode/pr_load.c +++ b/libs/gamecode/pr_load.c @@ -171,7 +171,7 @@ PR_LoadProgsFile (progs_t *pr, QFile *file, int size) // Some compilers (eg, FTE) put extra data between the header and the // strings section. What's worse, they de-align the data. - offset_tweak = progs.ofs_strings % sizeof (pr_int_t); + offset_tweak = progs.strings.offset % sizeof (pr_int_t); offset_tweak = (sizeof (pr_int_t) - offset_tweak) % sizeof (pr_int_t); // size of progs themselves @@ -216,14 +216,14 @@ PR_LoadProgsFile (progs_t *pr, QFile *file, int size) pr->zone = (memzone_t *) heap; } - pr->pr_functions = (dfunction_t *) (base + pr->progs->ofs_functions); - pr->pr_strings = (char *) base + pr->progs->ofs_strings; + pr->pr_functions = (dfunction_t *) (base + pr->progs->functions.offset); + pr->pr_strings = (char *) base + pr->progs->strings.offset; pr->pr_stringsize = (heap - base) + pr->zone_size; - global_ddefs = (ddef_t *) (base + pr->progs->ofs_globaldefs); - field_ddefs = (ddef_t *) (base + pr->progs->ofs_fielddefs); - pr->pr_statements = (dstatement_t *) (base + pr->progs->ofs_statements); + global_ddefs = (ddef_t *) (base + pr->progs->globaldefs.offset); + field_ddefs = (ddef_t *) (base + pr->progs->fielddefs.offset); + pr->pr_statements = (dstatement_t *) (base + pr->progs->statements.offset); - pr->pr_globals = (pr_type_t *) (base + pr->progs->ofs_globals); + pr->pr_globals = (pr_type_t *) (base + pr->progs->globals.offset); pr->stack = (pr_type_t *) ((byte *) pr->zone + pr->zone_size); pr->stack_bottom = pr->stack - pr->pr_globals; pr->globals_size = (pr_type_t *) ((byte *) pr->stack + pr->stack_size) @@ -241,14 +241,14 @@ PR_LoadProgsFile (progs_t *pr, QFile *file, int size) Hash_FlushTable (pr->field_hash); // byte swap the lumps - for (i = 0; i < pr->progs->numstatements; i++) { + for (i = 0; i < pr->progs->statements.count; i++) { pr->pr_statements[i].op = LittleShort (pr->pr_statements[i].op); pr->pr_statements[i].a = LittleShort (pr->pr_statements[i].a); pr->pr_statements[i].b = LittleShort (pr->pr_statements[i].b); pr->pr_statements[i].c = LittleShort (pr->pr_statements[i].c); } - for (i = 0; i < pr->progs->numfunctions; i++) { + for (i = 0; i < pr->progs->functions.count; i++) { pr->pr_functions[i].first_statement = LittleLong (pr->pr_functions[i].first_statement); pr->pr_functions[i].parm_start = @@ -265,9 +265,9 @@ PR_LoadProgsFile (progs_t *pr, QFile *file, int size) if (pr->pr_globaldefs) { free (pr->pr_globaldefs); } - pr->pr_globaldefs = calloc (pr->progs->numglobaldefs, sizeof (pr_def_t)); + pr->pr_globaldefs = calloc (pr->progs->globaldefs.count, sizeof (pr_def_t)); - for (i = 0; i < pr->progs->numglobaldefs; i++) { + for (i = 0; i < pr->progs->globaldefs.count; i++) { pr_ushort_t safe_type = global_ddefs[i].type & ~DEF_SAVEGLOBAL; global_ddefs[i].type = LittleShort (global_ddefs[i].type); global_ddefs[i].ofs = LittleShort (global_ddefs[i].ofs); @@ -283,8 +283,8 @@ PR_LoadProgsFile (progs_t *pr, QFile *file, int size) if (pr->pr_fielddefs) { free (pr->pr_fielddefs); } - pr->pr_fielddefs = calloc (pr->progs->numfielddefs, sizeof (pr_def_t)); - for (i = 0; i < pr->progs->numfielddefs; i++) { + pr->pr_fielddefs = calloc (pr->progs->fielddefs.count, sizeof (pr_def_t)); + for (i = 0; i < pr->progs->fielddefs.count; i++) { field_ddefs[i].type = LittleShort (field_ddefs[i].type); if (field_ddefs[i].type & DEF_SAVEGLOBAL) PR_Error (pr, "PR_LoadProgs: DEF_SAVEGLOBAL on field def %zd", i); @@ -297,7 +297,7 @@ PR_LoadProgsFile (progs_t *pr, QFile *file, int size) Hash_Add (pr->field_hash, &pr->pr_fielddefs[i]); } - for (i = 0; i < pr->progs->numglobals; i++) + for (i = 0; i < pr->progs->globals.count; i++) ((int *) pr->pr_globals)[i] = LittleLong (((int *) pr->pr_globals)[i]); xdefs_def = PR_FindGlobal (pr, ".xdefs"); @@ -305,23 +305,23 @@ PR_LoadProgsFile (progs_t *pr, QFile *file, int size) pr_xdefs_t *xdefs = &G_STRUCT (pr, pr_xdefs_t, xdefs_def->ofs); xdef_t *xdef = &G_STRUCT (pr, xdef_t, xdefs->xdefs); pr_def_t *def; - for (def = pr->pr_globaldefs, i = 0; i < pr->progs->numglobaldefs; + for (def = pr->pr_globaldefs, i = 0; i < pr->progs->globaldefs.count; i++, xdef++, def++) { def->ofs = xdef->ofs; def->type_encoding = xdef->type; } - for (def = pr->pr_fielddefs, i = 0; i < pr->progs->numfielddefs; + for (def = pr->pr_fielddefs, i = 0; i < pr->progs->fielddefs.count; i++, xdef++, def++) { def->ofs = xdef->ofs; def->type_encoding = xdef->type; } } else { pr_def_t *def; - for (def = pr->pr_globaldefs, i = 0; i < pr->progs->numglobaldefs; + for (def = pr->pr_globaldefs, i = 0; i < pr->progs->globaldefs.count; i++, def++) { def->size = pr_type_size[def->type & ~DEF_SAVEGLOBAL]; } - for (def = pr->pr_fielddefs, i = 0; i < pr->progs->numfielddefs; + for (def = pr->pr_fielddefs, i = 0; i < pr->progs->fielddefs.count; i++, def++) { def->size = pr_type_size[def->type & ~DEF_SAVEGLOBAL]; } @@ -369,7 +369,7 @@ pr_run_ctors (progs_t *pr) pr_uint_t fnum; dfunction_t *func; - for (fnum = 0; fnum < pr->progs->numfunctions; fnum++) { + for (fnum = 0; fnum < pr->progs->functions.count; fnum++) { func = pr->pr_functions + fnum; if (strequal (PR_GetString (pr, func->name), ".ctor")) PR_ExecuteProgram (pr, fnum); diff --git a/libs/gamecode/pr_parse.c b/libs/gamecode/pr_parse.c index 5f360c054..182146261 100644 --- a/libs/gamecode/pr_parse.c +++ b/libs/gamecode/pr_parse.c @@ -118,7 +118,7 @@ ED_EntityDict (progs_t *pr, edict_t *ed) pr_type_t *v; if (!ed->free) { - for (i = 0; i < pr->progs->numfielddefs; i++) { + for (i = 0; i < pr->progs->fielddefs.count; i++) { pr_def_t *d = &pr->pr_fielddefs[i]; name = PR_GetString (pr, d->name); @@ -162,7 +162,7 @@ ED_GlobalsDict (progs_t *pr) pr_def_t *def; int type; - for (i = 0; i < pr->progs->numglobaldefs; i++) { + for (i = 0; i < pr->progs->globaldefs.count; i++) { def = &pr->pr_globaldefs[i]; type = def->type; if (!(def->type & DEF_SAVEGLOBAL)) diff --git a/libs/gamecode/pr_resolve.c b/libs/gamecode/pr_resolve.c index 992aff74e..864baa443 100644 --- a/libs/gamecode/pr_resolve.c +++ b/libs/gamecode/pr_resolve.c @@ -71,13 +71,13 @@ PR_SearchDefs (pr_def_t *defs, unsigned num_defs, pr_ptr_t offset) pr_def_t * PR_GlobalAtOfs (progs_t * pr, pr_ptr_t ofs) { - return PR_SearchDefs (pr->pr_globaldefs, pr->progs->numglobaldefs, ofs); + return PR_SearchDefs (pr->pr_globaldefs, pr->progs->globaldefs.count, ofs); } VISIBLE pr_def_t * PR_FieldAtOfs (progs_t * pr, pr_ptr_t ofs) { - return PR_SearchDefs (pr->pr_fielddefs, pr->progs->numfielddefs, ofs); + return PR_SearchDefs (pr->pr_fielddefs, pr->progs->fielddefs.count, ofs); } VISIBLE pr_def_t * diff --git a/libs/gamecode/pr_strings.c b/libs/gamecode/pr_strings.c index 3c75984d2..b8364d89c 100644 --- a/libs/gamecode/pr_strings.c +++ b/libs/gamecode/pr_strings.c @@ -234,7 +234,7 @@ PR_LoadStrings (progs_t *pr) { prstr_resources_t *res = PR_Resources_Find (pr, "Strings"); - char *end = pr->pr_strings + pr->progs->numstrings; + char *end = pr->pr_strings + pr->progs->strings.count; char *str = pr->pr_strings; int count = 0; diff --git a/libs/gamecode/pr_v6p_opcode.c b/libs/gamecode/pr_v6p_opcode.c index 94da01a4c..13f129553 100644 --- a/libs/gamecode/pr_v6p_opcode.c +++ b/libs/gamecode/pr_v6p_opcode.c @@ -1493,7 +1493,7 @@ check_branch (progs_t *pr, dstatement_t *st, const v6p_opcode_t *op, short offse pr_int_t address = st - pr->pr_statements; address += offset; - if (address < 0 || (pr_uint_t) address >= pr->progs->numstatements) + if (address < 0 || (pr_uint_t) address >= pr->progs->statements.count) PR_Error (pr, "PR_Check_Opcodes: invalid branch (statement %ld: %s)", (long)(st - pr->pr_statements), op->opname); } @@ -1534,8 +1534,8 @@ check_global (progs_t *pr, dstatement_t *st, const v6p_opcode_t *op, etype_t typ break; default: if (operand + (unsigned) pr_type_size[type] - > pr->progs->numglobals) { - if (operand >= pr->progs->numglobals + > pr->progs->globals.count) { + if (operand >= pr->progs->globals.count || !is_vector_parameter_store (pr, st, operand)) { msg = "out of bounds global index"; goto error; @@ -1579,7 +1579,7 @@ check_global_size (progs_t *pr, dstatement_t *st, const v6p_opcode_t *op, unsigned short size, unsigned short operand) { const char *msg; - if (operand + size > pr->progs->numglobals) { + if (operand + size > pr->progs->globals.count) { msg = "out of bounds global index"; goto error; } @@ -1611,7 +1611,7 @@ PR_Check_v6p_Opcodes (progs_t *pr) // the only problem is that it slows progs load a little, but it's the only // way to check for qccx' evil if (0 && !pr_boundscheck->int_val) { - for (i = 0, st = pr->pr_statements; i < pr->progs->numstatements; + for (i = 0, st = pr->pr_statements; i < pr->progs->statements.count; st++, i++) { pr_opcode_v6p_e st_op = st->op; op = PR_v6p_Opcode (st_op); @@ -1632,7 +1632,7 @@ PR_Check_v6p_Opcodes (progs_t *pr) } } } else { - for (i = 0, st = pr->pr_statements; i < pr->progs->numstatements; + for (i = 0, st = pr->pr_statements; i < pr->progs->statements.count; st++, i++) { pr_opcode_v6p_e st_op = st->op; op = PR_v6p_Opcode (st_op); diff --git a/libs/gamecode/test/main.c b/libs/gamecode/test/main.c index 9f988fd50..bb0efb2fd 100644 --- a/libs/gamecode/test/main.c +++ b/libs/gamecode/test/main.c @@ -132,7 +132,7 @@ setup_test (test_t *test) } } - test_progs.numstatements = test->num_statements + 1; + test_progs.statements.count = test->num_statements + 1; test_pr.pr_statements = malloc ((test->num_statements + 1) * sizeof (dstatement_t)); memcpy (test_pr.pr_statements, test->statements, diff --git a/ruamoko/qwaq/builtins/debug.c b/ruamoko/qwaq/builtins/debug.c index c9da1e923..10c09c136 100644 --- a/ruamoko/qwaq/builtins/debug.c +++ b/ruamoko/qwaq/builtins/debug.c @@ -221,7 +221,7 @@ qdb_set_breakpoint (progs_t *pr) qwaq_target_t *target = get_target (debug, __FUNCTION__, handle); progs_t *tpr = target->pr; - if (staddr >= tpr->progs->numstatements) { + if (staddr >= tpr->progs->statements.count) { R_INT (pr) = -1; return; } @@ -239,7 +239,7 @@ qdb_clear_breakpoint (progs_t *pr) qwaq_target_t *target = get_target (debug, __FUNCTION__, handle); progs_t *tpr = target->pr; - if (staddr >= tpr->progs->numstatements) { + if (staddr >= tpr->progs->statements.count) { R_INT (pr) = -1; return; } @@ -555,7 +555,7 @@ qdb_get_function (progs_t *pr) pr_uint_t fnum = P_INT (pr, 1); dfunction_t *func = tpr->pr_functions + fnum; - if (fnum >= tpr->progs->numfunctions) { + if (fnum >= tpr->progs->functions.count) { func = 0; } return_function (pr, func); @@ -591,7 +591,7 @@ qdb_find_auxfunction (progs_t *pr) pr_uint_t fnum = func - tpr->pr_functions; pr_auxfunction_t *auxfunc = PR_Debug_MappedAuxFunction (tpr, fnum); - if (fnum >= tpr->progs->numfunctions) { + if (fnum >= tpr->progs->functions.count) { func = 0; } return_auxfunction (pr, auxfunc); diff --git a/tools/qfcc/source/disassemble.c b/tools/qfcc/source/disassemble.c index 60e4765bc..953ab8143 100644 --- a/tools/qfcc/source/disassemble.c +++ b/tools/qfcc/source/disassemble.c @@ -63,7 +63,7 @@ disassemble_progs (progs_t *pr) { unsigned int i; - for (i = 0; i < pr->progs->numstatements; i++) { + for (i = 0; i < pr->progs->statements.count; i++) { dfunction_t *desc = func_find (i); if (desc) { diff --git a/tools/qfcc/source/dump_globals.c b/tools/qfcc/source/dump_globals.c index 69cc1e61a..256c5e9d6 100644 --- a/tools/qfcc/source/dump_globals.c +++ b/tools/qfcc/source/dump_globals.c @@ -79,7 +79,7 @@ dump_def (progs_t *pr, pr_def_t *def, int indent) comment = " invalid offset"; - if (offset < pr->progs->numglobals) { + if (offset < pr->progs->globals.count) { comment = ""; switch (def->type & ~DEF_SAVEGLOBAL) { case ev_void: @@ -91,7 +91,7 @@ dump_def (progs_t *pr, pr_def_t *def, int indent) // dynamically allocated, thus a negative string index should // never appear in compiled code if (string < 0 - || (pr_uint_t) string >= pr->progs->numstrings) { + || (pr_uint_t) string >= pr->progs->strings.count) { str = "invalid string offset"; comment = va (0, " %d %s", string, str); } else { @@ -120,7 +120,7 @@ dump_def (progs_t *pr, pr_def_t *def, int indent) { pr_func_t func = G_FUNCTION (pr, offset); int start; - if (func < pr->progs->numfunctions) { + if (func < pr->progs->functions.count) { start = pr->pr_functions[func].first_statement; if (start > 0) comment = va (0, " %d @ %x", func, start); @@ -165,12 +165,12 @@ dump_globals (progs_t *pr) pr_def_t *global_defs = pr->pr_globaldefs; if (sorted) { - global_defs = malloc (pr->progs->numglobaldefs * sizeof (ddef_t)); + global_defs = malloc (pr->progs->globaldefs.count * sizeof (ddef_t)); memcpy (global_defs, pr->pr_globaldefs, - pr->progs->numglobaldefs * sizeof (ddef_t)); - qsort (global_defs, pr->progs->numglobaldefs, sizeof (ddef_t), cmp); + pr->progs->globaldefs.count * sizeof (ddef_t)); + qsort (global_defs, pr->progs->globaldefs.count, sizeof (ddef_t), cmp); } - for (i = 0; i < pr->progs->numglobaldefs; i++) { + for (i = 0; i < pr->progs->globaldefs.count; i++) { pr_def_t *def = &global_defs[i]; dump_def (pr, def, 0); } @@ -185,7 +185,7 @@ dump_fields (progs_t *pr) int offset; const char *comment; - for (i = 0; i < pr->progs->numfielddefs; i++) { + for (i = 0; i < pr->progs->fielddefs.count; i++) { pr_def_t *def = &pr->pr_fielddefs[i]; name = PR_GetString (pr, def->name); @@ -248,7 +248,7 @@ dump_functions (progs_t *pr) type_encodings = encodings_def->ofs; } - for (i = 0; i < pr->progs->numfunctions; i++) { + for (i = 0; i < pr->progs->functions.count; i++) { dfunction_t *func = &pr->pr_functions[i]; name = PR_GetString (pr, func->name); diff --git a/tools/qfcc/source/dump_lines.c b/tools/qfcc/source/dump_lines.c index f021e48a3..4b95b3ad3 100644 --- a/tools/qfcc/source/dump_lines.c +++ b/tools/qfcc/source/dump_lines.c @@ -78,7 +78,7 @@ progs_get_func_data (unsigned func_index, void *data) func_data.local_defs = aux_func->local_defs; func_data.line_info = aux_func->line_info; func_data.function = aux_func->function; - if (aux_func->function < (unsigned int) pr->progs->numfunctions) { + if (aux_func->function < (unsigned int) pr->progs->functions.count) { func = pr->pr_functions + aux_func->function; func_data.source_file = pr->pr_strings + func->file; func_data.source_name = pr->pr_strings + func->name; diff --git a/tools/qfcc/source/dump_modules.c b/tools/qfcc/source/dump_modules.c index 5f0f590f2..70dfe35c0 100644 --- a/tools/qfcc/source/dump_modules.c +++ b/tools/qfcc/source/dump_modules.c @@ -277,7 +277,7 @@ dump_modules (progs_t *pr) { unsigned int i; - for (i = 0; i < pr->progs->numglobaldefs; i++) { + for (i = 0; i < pr->progs->globaldefs.count; i++) { pr_def_t *def = &pr->pr_globaldefs[i]; const char *name = ""; diff --git a/tools/qfcc/source/dump_strings.c b/tools/qfcc/source/dump_strings.c index c8507011e..df4ca1b04 100644 --- a/tools/qfcc/source/dump_strings.c +++ b/tools/qfcc/source/dump_strings.c @@ -78,7 +78,7 @@ dump_string_block (const char *strblock, size_t size) void dump_strings (progs_t *pr) { - dump_string_block (pr->pr_strings, pr->progs->numstrings); + dump_string_block (pr->pr_strings, pr->progs->strings.count); } void diff --git a/tools/qfcc/source/idstuff.c b/tools/qfcc/source/idstuff.c index 9fb7f5003..34e9c4d7b 100644 --- a/tools/qfcc/source/idstuff.c +++ b/tools/qfcc/source/idstuff.c @@ -212,9 +212,9 @@ WriteProgdefs (dprograms_t *progs, const char *filename) "\n\ntypedef struct\n{\tint\tpad[%i];\n", RESERVED_OFS); - strings = (char *) progs + progs->ofs_strings; - for (i = 0; i < progs->numglobaldefs; i++) { - def = (ddef_t *) ((char *) progs + progs->ofs_globaldefs) + i; + strings = (char *) progs + progs->strings.offset; + for (i = 0; i < progs->globaldefs.count; i++) { + def = (ddef_t *) ((char *) progs + progs->globaldefs.offset) + i; name = strings + def->name; if (!strcmp (name, "end_sys_globals")) break; @@ -251,8 +251,8 @@ WriteProgdefs (dprograms_t *progs, const char *filename) // print all fields dasprintf (dstr, "typedef struct\n{\n"); - for (i = 0, j = 0; i < progs->numglobaldefs; i++) { - def = (ddef_t *) ((char *) progs + progs->ofs_globaldefs) + i; + for (i = 0, j = 0; i < progs->globaldefs.count; i++) { + def = (ddef_t *) ((char *) progs + progs->globaldefs.offset) + i; name = strings + def->name; if (!strcmp (name, "end_sys_fields")) break; @@ -264,7 +264,7 @@ WriteProgdefs (dprograms_t *progs, const char *filename) if (!strcmp (name, ".imm")) continue; - fdef = (ddef_t *) ((char *) progs + progs->ofs_fielddefs) + j++; + fdef = (ddef_t *) ((char *) progs + progs->fielddefs.offset) + j++; if (fdef->name != def->name) internal_error (0, "def and field order messup"); diff --git a/tools/qfcc/source/obj_file.c b/tools/qfcc/source/obj_file.c index 6a53b32ab..fa650a822 100644 --- a/tools/qfcc/source/obj_file.c +++ b/tools/qfcc/source/obj_file.c @@ -970,18 +970,18 @@ qfo_to_progs (qfo_t *in_qfo, int *size) *size = RUP (sizeof (dprograms_t), 16); progs = calloc (1, *size); progs->version = options.code.progsversion; - progs->numstatements = qfo->spaces[qfo_code_space].data_size; - progs->numglobaldefs = qfo->spaces[qfo_near_data_space].num_defs; + progs->statements.count = qfo->spaces[qfo_code_space].data_size; + progs->globaldefs.count = qfo->spaces[qfo_near_data_space].num_defs; //ddef offsets are 16 bits so the ddef ofs will likely be invalid //thus it will be forced invalid and the true offset written to the //.xdefs array in the progs file - progs->numglobaldefs += qfo->spaces[qfo_far_data_space].num_defs; - progs->numfielddefs = qfo->spaces[qfo_entity_space].num_defs; - progs->numfunctions = qfo->num_funcs + 1; - progs->numstrings = qfo->spaces[qfo_strings_space].data_size; - progs->numglobals = qfo->spaces[qfo_near_data_space].data_size; - progs->numglobals = align_globals_size (progs->numglobals); - locals_start = progs->numglobals; + progs->globaldefs.count += qfo->spaces[qfo_far_data_space].num_defs; + progs->fielddefs.count = qfo->spaces[qfo_entity_space].num_defs; + progs->functions.count = qfo->num_funcs + 1; + progs->strings.count = qfo->spaces[qfo_strings_space].data_size; + progs->globals.count = qfo->spaces[qfo_near_data_space].data_size; + progs->globals.count = align_globals_size (progs->globals.count); + locals_start = progs->globals.count; if (options.code.progsversion < PROG_VERSION) { for (i = qfo_num_spaces; i < qfo->num_spaces; i++) { if (options.code.local_merging) { @@ -994,34 +994,34 @@ qfo_to_progs (qfo_t *in_qfo, int *size) } } } - progs->numglobals += locals_size; - near_data_size = progs->numglobals; - progs->numglobals = RUP (progs->numglobals, 16 / sizeof (pr_type_t)); - progs->numglobals += qfo->spaces[qfo_far_data_space].data_size; - type_encodings_start = progs->numglobals; - progs->numglobals += qfo->spaces[qfo_type_space].data_size; - progs->numglobals = RUP (progs->numglobals, type_xdef.alignment); - xdefs_start = progs->numglobals; - progs->numglobals += progs->numglobaldefs * type_size (&type_xdef); - progs->numglobals += progs->numfielddefs * type_size (&type_xdef); + progs->globals.count += locals_size; + near_data_size = progs->globals.count; + progs->globals.count = RUP (progs->globals.count, 16 / sizeof (pr_type_t)); + progs->globals.count += qfo->spaces[qfo_far_data_space].data_size; + type_encodings_start = progs->globals.count; + progs->globals.count += qfo->spaces[qfo_type_space].data_size; + progs->globals.count = RUP (progs->globals.count, type_xdef.alignment); + xdefs_start = progs->globals.count; + progs->globals.count += progs->globaldefs.count * type_size (&type_xdef); + progs->globals.count += progs->fielddefs.count * type_size (&type_xdef); progs->entityfields = qfo->spaces[qfo_entity_space].data_size; // qfo_debug_space does not go in the progs file - *size += progs->numstatements * sizeof (dstatement_t); - *size += progs->numglobaldefs * sizeof (ddef_t); - *size += progs->numfielddefs * sizeof (ddef_t); - *size += progs->numfunctions * sizeof (dfunction_t); - *size += RUP (progs->numstrings * sizeof (char), 16); - *size += progs->numglobals * sizeof (pr_type_t); + *size += progs->statements.count * sizeof (dstatement_t); + *size += progs->globaldefs.count * sizeof (ddef_t); + *size += progs->fielddefs.count * sizeof (ddef_t); + *size += progs->functions.count * sizeof (dfunction_t); + *size += RUP (progs->strings.count * sizeof (char), 16); + *size += progs->globals.count * sizeof (pr_type_t); progs = realloc (progs, *size); data = (byte *) progs; memset (progs + 1, 0, *size - sizeof (dprograms_t)); data += RUP (sizeof (dprograms_t), 16); - def_indices = alloca ((progs->numglobaldefs + progs->numfielddefs) + def_indices = alloca ((progs->globaldefs.count + progs->fielddefs.count) * sizeof (*def_indices)); far_def_indices = def_indices + qfo->spaces[qfo_near_data_space].num_defs; - field_def_indices = def_indices + progs->numglobaldefs; + field_def_indices = def_indices + progs->globaldefs.count; for (unsigned i = 0; i < qfo->spaces[qfo_near_data_space].num_defs; i++) { def_indices[i] = i; } @@ -1041,28 +1041,28 @@ qfo_to_progs (qfo_t *in_qfo, int *size) sizeof (unsigned), qfo_def_compare, qfo->spaces[qfo_entity_space].defs); - progs->ofs_strings = data - (byte *) progs; + progs->strings.offset = data - (byte *) progs; strings = (char *) data; - data += RUP (progs->numstrings * sizeof (char), 16); + data += RUP (progs->strings.count * sizeof (char), 16); - progs->ofs_statements = data - (byte *) progs; + progs->statements.offset = data - (byte *) progs; statements = (dstatement_t *) data; - data += progs->numstatements * sizeof (dstatement_t); + data += progs->statements.count * sizeof (dstatement_t); - progs->ofs_functions = data - (byte *) progs; + progs->functions.offset = data - (byte *) progs; functions = (dfunction_t *) data; functions++; // skip over null function - data += progs->numfunctions * sizeof (dfunction_t); + data += progs->functions.count * sizeof (dfunction_t); - progs->ofs_globaldefs = data - (byte *) progs; + progs->globaldefs.offset = data - (byte *) progs; globaldefs = (ddef_t *) data; - data += progs->numglobaldefs * sizeof (ddef_t); + data += progs->globaldefs.count * sizeof (ddef_t); - progs->ofs_fielddefs = data - (byte *) progs; - fielddefs = (ddef_t *) (globaldefs + progs->numglobaldefs); - data += progs->numfielddefs * sizeof (ddef_t); + progs->fielddefs.offset = data - (byte *) progs; + fielddefs = (ddef_t *) (globaldefs + progs->globaldefs.count); + data += progs->fielddefs.count * sizeof (ddef_t); - progs->ofs_globals = data - (byte *) progs; + progs->globals.offset = data - (byte *) progs; globals = (pr_type_t*) data; locals = globals + locals_start; far_data = globals + near_data_size; @@ -1155,7 +1155,7 @@ qfo_to_progs (qfo_t *in_qfo, int *size) xdefs = (pr_xdefs_t *) &globals[xdefs_def->offset]; xdef = (xdef_t *) xdef_data; xdefs->xdefs = xdefs_start; - xdefs->num_xdefs = progs->numglobaldefs + progs->numfielddefs; + xdefs->num_xdefs = progs->globaldefs.count + progs->fielddefs.count; for (i = 0; i < qfo->spaces[qfo_near_data_space].num_defs; i++, xdef++) { qfo_def_t *def = qfo->spaces[qfo_near_data_space].defs + i; @@ -1193,12 +1193,12 @@ qfo_to_progs (qfo_t *in_qfo, int *size) const char *big_function = ""; if (big_func) big_function = va (0, " (%s)", strings + qfo->funcs[big_func].name); - printf ("%6i strofs\n", progs->numstrings); - printf ("%6i statements\n", progs->numstatements); - printf ("%6i functions\n", progs->numfunctions); - printf ("%6i global defs\n", progs->numglobaldefs); - printf ("%6i field defs\n", progs->numfielddefs); - printf ("%6i globals\n", progs->numglobals); + printf ("%6i strofs\n", progs->strings.count); + printf ("%6i statements\n", progs->statements.count); + printf ("%6i functions\n", progs->functions.count); + printf ("%6i global defs\n", progs->globaldefs.count); + printf ("%6i field defs\n", progs->fielddefs.count); + printf ("%6i globals\n", progs->globals.count); printf (" %6i near globals\n", near_data_size); printf (" %6i locals size%s\n", locals_size, big_function); printf (" %6i far globals\n", diff --git a/tools/qfcc/source/qfcc.c b/tools/qfcc/source/qfcc.c index 134890dea..ae256c8cf 100644 --- a/tools/qfcc/source/qfcc.c +++ b/tools/qfcc/source/qfcc.c @@ -212,20 +212,20 @@ WriteProgs (dprograms_t *progs, int size) pr_type_t *globals; #define P(t,o) ((t *)((char *)progs + progs->o)) - statements = P (dstatement_t, ofs_statements); - functions = P (dfunction_t, ofs_functions); - globaldefs = P (ddef_t, ofs_globaldefs); - fielddefs = P (ddef_t, ofs_fielddefs); - globals = P (pr_type_t, ofs_globals); + statements = P (dstatement_t, statements.offset); + functions = P (dfunction_t, functions.offset); + globaldefs = P (ddef_t, globaldefs.offset); + fielddefs = P (ddef_t, fielddefs.offset); + globals = P (pr_type_t, globals.offset); #undef P - for (i = 0; i < progs->numstatements; i++) { + for (i = 0; i < progs->statements.count; i++) { statements[i].op = LittleShort (statements[i].op); statements[i].a = LittleShort (statements[i].a); statements[i].b = LittleShort (statements[i].b); statements[i].c = LittleShort (statements[i].c); } - for (i = 0; i < (unsigned) progs->numfunctions; i++) { + for (i = 0; i < (unsigned) progs->functions.count; i++) { dfunction_t *func = functions + i; func->first_statement = LittleLong (func->first_statement); func->parm_start = LittleLong (func->parm_start); @@ -235,17 +235,17 @@ WriteProgs (dprograms_t *progs, int size) func->file = LittleLong (func->file); func->numparms = LittleLong (func->numparms); } - for (i = 0; i < progs->numglobaldefs; i++) { + for (i = 0; i < progs->globaldefs.count; i++) { globaldefs[i].type = LittleShort (globaldefs[i].type); globaldefs[i].ofs = LittleShort (globaldefs[i].ofs); globaldefs[i].name = LittleLong (globaldefs[i].name); } - for (i = 0; i < progs->numfielddefs; i++) { + for (i = 0; i < progs->fielddefs.count; i++) { fielddefs[i].type = LittleShort (fielddefs[i].type); fielddefs[i].ofs = LittleShort (fielddefs[i].ofs); fielddefs[i].name = LittleLong (fielddefs[i].name); } - for (i = 0; i < progs->numglobals; i++) + for (i = 0; i < progs->globals.count; i++) globals[i].int_var = LittleLong (globals[i].int_var); if (!(h = Qopen (options.output_file, "wb"))) diff --git a/tools/qfcc/source/qfprogs.c b/tools/qfcc/source/qfprogs.c index 1d60336a4..b468cc5b3 100644 --- a/tools/qfcc/source/qfprogs.c +++ b/tools/qfcc/source/qfprogs.c @@ -293,7 +293,7 @@ load_progs (const char *name) PR_LoadStrings (&pr); PR_LoadDebug (&pr); } - for (i = 0; i < pr.progs->numfunctions; i++) { + for (i = 0; i < pr.progs->functions.count; i++) { // don't bother with builtins if (pr.pr_functions[i].first_statement > 0) Hash_AddElement (func_tab, &pr.pr_functions[i]);