From 35a8c3c9af67542d50d6a07a00501f6fe3767365 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Thu, 15 Jan 2015 01:22:21 -0500 Subject: [PATCH] More std::vector migration --- code.cpp | 127 ++++++++++++++------------------ exec.cpp | 217 ++++++++++++++++++++++++++----------------------------- gmqcc.h | 65 +++++++++-------- ir.cpp | 216 +++++++++++++++++++++++++++--------------------------- util.cpp | 82 ++++++++++----------- 5 files changed, 336 insertions(+), 371 deletions(-) diff --git a/code.cpp b/code.cpp index 314d11b..861d552 100644 --- a/code.cpp +++ b/code.cpp @@ -94,16 +94,16 @@ void code_push_statement(code_t *code, prog_section_statement_t *stmt_in, lex_ct } } - vec_push(code->statements, stmt); - vec_push(code->linenums, (int)ctx.line); - vec_push(code->columnnums, (int)ctx.column); + code->statements.push_back(stmt); + code->linenums.push_back(ctx.line); + code->columnnums.push_back(ctx.column); } void code_pop_statement(code_t *code) { - vec_pop(code->statements); - vec_pop(code->linenums); - vec_pop(code->columnnums); + code->statements.pop_back(); + code->linenums.pop_back(); + code->columnnums.pop_back(); } code_t *code_init() { @@ -124,15 +124,15 @@ code_t *code_init() { * some null (empty) statements, functions, and 28 globals */ for(; i < 28; i++) - vec_push(code->globals, 0); + code->globals.push_back(0); - vec_push(code->chars, '\0'); - vec_push(code->functions, empty_function); + code->chars.push_back('\0'); + code->functions.push_back(empty_function); code_push_statement(code, &empty_statement, empty_ctx); - vec_push(code->defs, empty_def); - vec_push(code->fields, empty_def); + code->defs.push_back(empty_def); + code->fields.push_back(empty_def); return code; } @@ -148,8 +148,8 @@ uint32_t code_genstring(code_t *code, const char *str) { if (!*str) { if (!code->string_cached_empty) { - code->string_cached_empty = vec_size(code->chars); - vec_push(code->chars, 0); + code->string_cached_empty = code->chars.size(); + code->chars.push_back(0); } return code->string_cached_empty; } @@ -165,8 +165,9 @@ uint32_t code_genstring(code_t *code, const char *str) { if (CODE_HASH_ENTER(existing)) return CODE_HASH_LEAVE(existing); - CODE_HASH_LEAVE(existing) = vec_size(code->chars); - vec_append(code->chars, strlen(str)+1, str); + CODE_HASH_LEAVE(existing) = code->chars.size(); + code->chars.insert(code->chars.end(), str, str + strlen(str) + 1); + //vec_append(code->chars, strlen(str)+1, str); util_htseth(code->string_cache, str, hash, CODE_HASH_ENTER(existing)); return CODE_HASH_LEAVE(existing); @@ -188,16 +189,16 @@ static size_t code_size_generic(code_t *code, prog_header_t *code_header, bool l size += sizeof(code_header->globals.length); size += sizeof(code_header->fields.length); size += sizeof(code_header->statements.length); - size += sizeof(code->linenums[0]) * vec_size(code->linenums); - size += sizeof(code->columnnums[0]) * vec_size(code->columnnums); + size += sizeof(code->linenums[0]) * code->linenums.size(); + size += sizeof(code->columnnums[0]) * code->columnnums.size(); } else { size += sizeof(prog_header_t); - size += sizeof(prog_section_statement_t) * vec_size(code->statements); - size += sizeof(prog_section_def_t) * vec_size(code->defs); - size += sizeof(prog_section_field_t) * vec_size(code->fields); - size += sizeof(prog_section_function_t) * vec_size(code->functions); - size += sizeof(int32_t) * vec_size(code->globals); - size += 1 * vec_size(code->chars); + size += sizeof(prog_section_statement_t) * code->statements.size(); + size += sizeof(prog_section_def_t) * code->defs.size(); + size += sizeof(prog_section_field_t) * code->fields.size(); + size += sizeof(prog_section_function_t) * code->functions.size(); + size += sizeof(int32_t) * code->globals.size(); + size += 1 * code->chars.size(); } return size; } @@ -209,17 +210,17 @@ static void code_create_header(code_t *code, prog_header_t *code_header, const c size_t i; code_header->statements.offset = sizeof(prog_header_t); - code_header->statements.length = vec_size(code->statements); - code_header->defs.offset = code_header->statements.offset + (sizeof(prog_section_statement_t) * vec_size(code->statements)); - code_header->defs.length = vec_size(code->defs); - code_header->fields.offset = code_header->defs.offset + (sizeof(prog_section_def_t) * vec_size(code->defs)); - code_header->fields.length = vec_size(code->fields); - code_header->functions.offset = code_header->fields.offset + (sizeof(prog_section_field_t) * vec_size(code->fields)); - code_header->functions.length = vec_size(code->functions); - code_header->globals.offset = code_header->functions.offset + (sizeof(prog_section_function_t) * vec_size(code->functions)); - code_header->globals.length = vec_size(code->globals); - code_header->strings.offset = code_header->globals.offset + (sizeof(int32_t) * vec_size(code->globals)); - code_header->strings.length = vec_size(code->chars); + code_header->statements.length = code->statements.size(); + code_header->defs.offset = code_header->statements.offset + (sizeof(prog_section_statement_t) * code->statements.size()); + code_header->defs.length = code->defs.size(); + code_header->fields.offset = code_header->defs.offset + (sizeof(prog_section_def_t) * code->defs.size()); + code_header->fields.length = code->fields.size(); + code_header->functions.offset = code_header->fields.offset + (sizeof(prog_section_field_t) * code->fields.size()); + code_header->functions.length = code->functions.size(); + code_header->globals.offset = code_header->functions.offset + (sizeof(prog_section_function_t) * code->functions.size()); + code_header->globals.length = code->globals.size(); + code_header->strings.offset = code_header->globals.offset + (sizeof(int32_t) * code->globals.size()); + code_header->strings.length = code->chars.size(); code_header->version = 6; code_header->skip = 0; @@ -231,24 +232,18 @@ static void code_create_header(code_t *code, prog_header_t *code_header, const c if (OPTS_FLAG(DARKPLACES_STRING_TABLE_BUG)) { /* >= + P */ - vec_push(code->chars, '\0'); /* > */ - vec_push(code->chars, '\0'); /* = */ - vec_push(code->chars, '\0'); /* P */ + code->chars.push_back('\0'); /* > */ + code->chars.push_back('\0'); /* = */ + code->chars.push_back('\0'); /* P */ } /* ensure all data is in LE format */ - util_swap_header(code_header); - - /* - * These are not part of the header but we ensure LE format here to save on duplicated - * code. - */ - - util_swap_statements (code->statements); + util_swap_header(*code_header); + util_swap_statements(code->statements); util_swap_defs_fields(code->defs); util_swap_defs_fields(code->fields); - util_swap_functions (code->functions); - util_swap_globals (code->globals); + util_swap_functions(code->functions); + util_swap_globals(code->globals); if (!OPTS_OPTION_BOOL(OPTION_QUIET)) { if (lnofile) @@ -296,8 +291,8 @@ static void code_stats(const char *filename, const char *lnofile, code_t *code, } bool code_write(code_t *code, const char *filename, const char *lnofile) { - prog_header_t code_header; - FILE *fp = NULL; + prog_header_t code_header; + FILE *fp = NULL; code_create_header(code, &code_header, filename, lnofile); @@ -308,9 +303,9 @@ bool code_write(code_t *code, const char *filename, const char *lnofile) { if (!fp) return false; - util_endianswap(&version, 1, sizeof(version)); - util_endianswap(code->linenums, vec_size(code->linenums), sizeof(code->linenums[0])); - util_endianswap(code->columnnums, vec_size(code->columnnums), sizeof(code->columnnums[0])); + util_endianswap(&version, 1, sizeof(version)); + util_endianswap(&code->linenums[0], code->linenums.size(), sizeof(code->linenums[0])); + util_endianswap(&code->columnnums[0], code->columnnums.size(), sizeof(code->columnnums[0])); if (fwrite("LNOF", 4, 1, fp) != 1 || fwrite(&version, sizeof(version), 1, fp) != 1 || @@ -318,8 +313,8 @@ bool code_write(code_t *code, const char *filename, const char *lnofile) { fwrite(&code_header.globals.length, sizeof(code_header.globals.length), 1, fp) != 1 || fwrite(&code_header.fields.length, sizeof(code_header.fields.length), 1, fp) != 1 || fwrite(&code_header.statements.length, sizeof(code_header.statements.length), 1, fp) != 1 || - fwrite(code->linenums, sizeof(code->linenums[0]), vec_size(code->linenums), fp) != vec_size(code->linenums) || - fwrite(code->columnnums, sizeof(code->columnnums[0]), vec_size(code->columnnums), fp) != vec_size(code->columnnums)) + fwrite(&code->linenums[0], sizeof(code->linenums[0]), code->linenums.size(), fp) != code->linenums.size() || + fwrite(&code->columnnums[0], sizeof(code->columnnums[0]), code->columnnums.size(), fp) != code->columnnums.size()) { con_err("failed to write lno file\n"); } @@ -332,13 +327,13 @@ bool code_write(code_t *code, const char *filename, const char *lnofile) { if (!fp) return false; - if (1 != fwrite(&code_header, sizeof(prog_header_t) , 1 , fp) || - vec_size(code->statements) != fwrite(code->statements, sizeof(prog_section_statement_t), vec_size(code->statements), fp) || - vec_size(code->defs) != fwrite(code->defs, sizeof(prog_section_def_t) , vec_size(code->defs) , fp) || - vec_size(code->fields) != fwrite(code->fields, sizeof(prog_section_field_t) , vec_size(code->fields) , fp) || - vec_size(code->functions) != fwrite(code->functions, sizeof(prog_section_function_t) , vec_size(code->functions) , fp) || - vec_size(code->globals) != fwrite(code->globals, sizeof(int32_t) , vec_size(code->globals) , fp) || - vec_size(code->chars) != fwrite(code->chars, 1 , vec_size(code->chars) , fp)) + if (1 != fwrite(&code_header, sizeof(prog_header_t) , 1 , fp) || + code->statements.size() != fwrite(&code->statements[0], sizeof(prog_section_statement_t), code->statements.size(), fp) || + code->defs.size() != fwrite(&code->defs[0], sizeof(prog_section_def_t) , code->defs.size() , fp) || + code->fields.size() != fwrite(&code->fields[0], sizeof(prog_section_field_t) , code->fields.size() , fp) || + code->functions.size() != fwrite(&code->functions[0], sizeof(prog_section_function_t) , code->functions.size() , fp) || + code->globals.size() != fwrite(&code->globals[0], sizeof(int32_t) , code->globals.size() , fp) || + code->chars.size() != fwrite(&code->chars[0], 1 , code->chars.size() , fp)) { fclose(fp); return false; @@ -350,16 +345,6 @@ bool code_write(code_t *code, const char *filename, const char *lnofile) { } void code_cleanup(code_t *code) { - vec_free(code->statements); - vec_free(code->linenums); - vec_free(code->columnnums); - vec_free(code->defs); - vec_free(code->fields); - vec_free(code->functions); - vec_free(code->globals); - vec_free(code->chars); - util_htdel(code->string_cache); - mem_d(code); } diff --git a/exec.cpp b/exec.cpp index 5b1fe23..4fb1cda 100644 --- a/exec.cpp +++ b/exec.cpp @@ -30,10 +30,9 @@ static void qcvmerror(qc_program_t *prog, const char *fmt, ...) qc_program_t* prog_load(const char *filename, bool skipversion) { - prog_header_t header; - qc_program_t *prog; - size_t i; - FILE *file = fopen(filename, "rb"); + prog_header_t header; + qc_program_t *prog; + FILE *file = fopen(filename, "rb"); /* we need all those in order to support INSTR_STATE: */ bool has_self = false, @@ -51,7 +50,7 @@ qc_program_t* prog_load(const char *filename, bool skipversion) return NULL; } - util_swap_header(&header); + util_swap_header(header); if (!skipversion && header.version != 6) { loaderror("header says this is a version %i progs, we need version 6\n", header.version); @@ -81,9 +80,10 @@ qc_program_t* prog_load(const char *filename, bool skipversion) loaderror("seek failed"); \ goto error; \ } \ + prog->progvar.resize(header.hdrvar.length + reserved); \ if (fread( \ - vec_add(prog->progvar, header.hdrvar.length + reserved), \ - sizeof(*prog->progvar), \ + &prog->progvar[0], \ + sizeof(prog->progvar[0]), \ header.hdrvar.length, \ file \ )!= header.hdrvar.length \ @@ -101,21 +101,22 @@ qc_program_t* prog_load(const char *filename, bool skipversion) read_data1(strings); read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */ - util_swap_statements (prog->code); + util_swap_statements(prog->code); util_swap_defs_fields(prog->defs); util_swap_defs_fields(prog->fields); - util_swap_functions (prog->functions); - util_swap_globals (prog->globals); + util_swap_functions(prog->functions); + util_swap_globals(prog->globals); fclose(file); /* profile counters */ - memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code)); + memset(vec_add(prog->profile, prog->code.size()), 0, sizeof(prog->profile[0]) * prog->code.size()); /* Add tempstring area */ - prog->tempstring_start = vec_size(prog->strings); - prog->tempstring_at = vec_size(prog->strings); - memset(vec_add(prog->strings, 16*1024), 0, 16*1024); + prog->tempstring_start = prog->strings.size(); + prog->tempstring_at = prog->strings.size(); + + prog->strings.resize(prog->strings.size() + 16*1024, '\0'); /* spawn the world entity */ vec_push(prog->entitypool, true); @@ -123,29 +124,29 @@ qc_program_t* prog_load(const char *filename, bool skipversion) prog->entities = 1; /* cache some globals and fields from names */ - for (i = 0; i < vec_size(prog->defs); ++i) { - const char *name = prog_getstring(prog, prog->defs[i].name); - if (!strcmp(name, "self")) { - prog->cached_globals.self = prog->defs[i].offset; + for (auto &it : prog->defs) { + const char *name = prog_getstring(prog, it.name); + if (!strcmp(name, "self")) { + prog->cached_globals.self = it.offset; has_self = true; } else if (!strcmp(name, "time")) { - prog->cached_globals.time = prog->defs[i].offset; + prog->cached_globals.time = it.offset; has_time = true; } } - for (i = 0; i < vec_size(prog->fields); ++i) { - const char *name = prog_getstring(prog, prog->fields[i].name); - if (!strcmp(name, "think")) { - prog->cached_fields.think = prog->fields[i].offset; + for (auto &it : prog->fields) { + const char *name = prog_getstring(prog, it.name); + if (!strcmp(name, "think")) { + prog->cached_fields.think = it.offset; has_think = true; } else if (!strcmp(name, "nextthink")) { - prog->cached_fields.nextthink = prog->fields[i].offset; + prog->cached_fields.nextthink = it.offset; has_nextthink = true; } else if (!strcmp(name, "frame")) { - prog->cached_fields.frame = prog->fields[i].offset; + prog->cached_fields.frame = it.offset; has_frame = true; } } @@ -157,12 +158,6 @@ qc_program_t* prog_load(const char *filename, bool skipversion) error: if (prog->filename) mem_d(prog->filename); - vec_free(prog->code); - vec_free(prog->defs); - vec_free(prog->fields); - vec_free(prog->functions); - vec_free(prog->strings); - vec_free(prog->globals); vec_free(prog->entitydata); vec_free(prog->entitypool); mem_d(prog); @@ -174,12 +169,6 @@ error: void prog_delete(qc_program_t *prog) { if (prog->filename) mem_d(prog->filename); - vec_free(prog->code); - vec_free(prog->defs); - vec_free(prog->fields); - vec_free(prog->functions); - vec_free(prog->strings); - vec_free(prog->globals); vec_free(prog->entitydata); vec_free(prog->entitypool); vec_free(prog->localstack); @@ -194,29 +183,25 @@ void prog_delete(qc_program_t *prog) const char* prog_getstring(qc_program_t *prog, qcint_t str) { /* cast for return required for C++ */ - if (str < 0 || str >= (qcint_t)vec_size(prog->strings)) + if (str < 0 || str >= (qcint_t)prog->strings.size()) return "<<>>"; - return prog->strings + str; + return &prog->strings[0] + str; } prog_section_def_t* prog_entfield(qc_program_t *prog, qcint_t off) { - size_t i; - for (i = 0; i < vec_size(prog->fields); ++i) { - if (prog->fields[i].offset == off) - return (prog->fields + i); - } - return NULL; + for (auto &it : prog->fields) + if (it.offset == off) + return ⁢ + return nullptr; } prog_section_def_t* prog_getdef(qc_program_t *prog, qcint_t off) { - size_t i; - for (i = 0; i < vec_size(prog->defs); ++i) { - if (prog->defs[i].offset == off) - return (prog->defs + i); - } - return NULL; + for (auto &it : prog->defs) + if (it.offset == off) + return ⁢ + return nullptr; } qcany_t* prog_getedict(qc_program_t *prog, qcint_t e) { @@ -269,19 +254,19 @@ qcint_t prog_tempstring(qc_program_t *prog, const char *str) { size_t at = prog->tempstring_at; /* when we reach the end we start over */ - if (at + len >= vec_size(prog->strings)) + if (at + len >= prog->strings.size()) at = prog->tempstring_start; /* when it doesn't fit, reallocate */ - if (at + len >= vec_size(prog->strings)) + if (at + len >= prog->strings.size()) { - (void)vec_add(prog->strings, len+1); - memcpy(prog->strings + at, str, len+1); + prog->strings.resize(prog->strings.size() + len+1); + memcpy(&prog->strings[0] + at, str, len+1); return at; } /* when it fits, just copy */ - memcpy(prog->strings + at, str, len+1); + memcpy(&prog->strings[0] + at, str, len+1); prog->tempstring_at += len+1; return at; } @@ -498,13 +483,13 @@ static qcint_t prog_enterfunction(qc_program_t *prog, prog_section_function_t *f cur = prog->stack[vec_size(prog->stack)-1].function; if (cur) { - qcint_t *globals = prog->globals + cur->firstlocal; + qcint_t *globals = &prog->globals[0] + cur->firstlocal; vec_append(prog->localstack, cur->locals, globals); } } #else { - qcint_t *globals = prog->globals + func->firstlocal; + qcint_t *globals = &prog->globals[0] + func->firstlocal; vec_append(prog->localstack, func->locals, globals); } #endif @@ -546,7 +531,7 @@ static qcint_t prog_leavefunction(qc_program_t *prog) { oldsp = prog->stack[vec_size(prog->stack)-1].localsp; #endif if (prev) { - qcint_t *globals = prog->globals + prev->firstlocal; + qcint_t *globals = &prog->globals[0] + prev->firstlocal; memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0])); /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */ vec_shrinkto(prog->localstack, oldsp); @@ -565,7 +550,7 @@ bool prog_exec(qc_program_t *prog, prog_section_function_t *func, size_t flags, prog->vmerror = 0; prog->xflags = flags; - st = prog->code + prog_enterfunction(prog, func); + st = &prog->code[0] + prog_enterfunction(prog, func); --st; switch (flags) { @@ -649,7 +634,7 @@ static qcvm_parameter *main_params = NULL; } \ } while (0) -#define GetGlobal(idx) ((qcany_t*)(prog->globals + (idx))) +#define GetGlobal(idx) ((qcany_t*)(&prog->globals[0] + (idx))) #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num)) #define Return(any) *(GetGlobal(OFS_RETURN)) = (any) @@ -657,7 +642,7 @@ static int qc_print(qc_program_t *prog) { size_t i; const char *laststr = NULL; for (i = 0; i < (size_t)prog->argc; ++i) { - qcany_t *str = (qcany_t*)(prog->globals + OFS_PARM0 + 3*i); + qcany_t *str = (qcany_t*)(&prog->globals[0] + OFS_PARM0 + 3*i); laststr = prog_getstring(prog, str->string); printf("%s", laststr); } @@ -1108,18 +1093,18 @@ int main(int argc, char **argv) { if (opts_info) { printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16); printf("Entity field space: %u\n", (unsigned int)prog->entityfields); - printf("Globals: %u\n", (unsigned int)vec_size(prog->globals)); + printf("Globals: %zu\n", prog->globals.size()); printf("Counts:\n" - " code: %lu\n" - " defs: %lu\n" - " fields: %lu\n" - " functions: %lu\n" - " strings: %lu\n", - (unsigned long)vec_size(prog->code), - (unsigned long)vec_size(prog->defs), - (unsigned long)vec_size(prog->fields), - (unsigned long)vec_size(prog->functions), - (unsigned long)vec_size(prog->strings)); + " code: %zu\n" + " defs: %zu\n" + " fields: %zu\n" + " functions: %zu\n" + " strings: %zu\n", + prog->code.size(), + prog->defs.size(), + prog->fields.size(), + prog->functions.size(), + prog->strings.size()); } if (opts_info) { @@ -1129,7 +1114,7 @@ int main(int argc, char **argv) { for (i = 0; i < vec_size(dis_list); ++i) { size_t k; printf("Looking for `%s`\n", dis_list[i]); - for (k = 1; k < vec_size(prog->functions); ++k) { + for (k = 1; k < prog->functions.size(); ++k) { const char *name = prog_getstring(prog, prog->functions[k].name); if (!strcmp(name, dis_list[i])) { prog_disasm_function(prog, k); @@ -1138,34 +1123,34 @@ int main(int argc, char **argv) { } } if (opts_disasm) { - for (i = 1; i < vec_size(prog->functions); ++i) + for (i = 1; i < prog->functions.size(); ++i) prog_disasm_function(prog, i); return 0; } if (opts_printdefs) { const char *getstring = NULL; - for (i = 0; i < vec_size(prog->defs); ++i) { + for (auto &it : prog->defs) { printf("Global: %8s %-16s at %u%s", - type_name[prog->defs[i].type & DEF_TYPEMASK], - prog_getstring(prog, prog->defs[i].name), - (unsigned int)prog->defs[i].offset, - ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : "")); + type_name[it.type & DEF_TYPEMASK], + prog_getstring(prog, it.name), + (unsigned int)it.offset, + ((it.type & DEF_SAVEGLOBAL) ? " [SAVE]" : "")); if (opts_v) { - switch (prog->defs[i].type & DEF_TYPEMASK) { + switch (it.type & DEF_TYPEMASK) { case TYPE_FLOAT: - printf(" [init: %g]", ((qcany_t*)(prog->globals + prog->defs[i].offset))->_float); + printf(" [init: %g]", ((qcany_t*)(&prog->globals[0] + it.offset))->_float); break; case TYPE_INTEGER: - printf(" [init: %i]", (int)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int )); + printf(" [init: %i]", (int)( ((qcany_t*)(&prog->globals[0] + it.offset))->_int )); break; case TYPE_ENTITY: case TYPE_FUNCTION: case TYPE_FIELD: case TYPE_POINTER: - printf(" [init: %u]", (unsigned)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int )); + printf(" [init: %u]", (unsigned)( ((qcany_t*)(&prog->globals[0] + it.offset))->_int )); break; case TYPE_STRING: - getstring = prog_getstring(prog, ((qcany_t*)(prog->globals + prog->defs[i].offset))->string); + getstring = prog_getstring(prog, ((qcany_t*)(&prog->globals[0] + it.offset))->string); printf(" [init: `"); print_escaped_string(getstring, strlen(getstring)); printf("`]\n"); @@ -1178,37 +1163,37 @@ int main(int argc, char **argv) { } } if (opts_printfields) { - for (i = 0; i < vec_size(prog->fields); ++i) { - printf("Field: %8s %-16s at %u%s\n", - type_name[prog->fields[i].type], - prog_getstring(prog, prog->fields[i].name), - (unsigned int)prog->fields[i].offset, - ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : "")); + for (auto &it : prog->fields) { + printf("Field: %8s %-16s at %d%s\n", + type_name[it.type], + prog_getstring(prog, it.name), + it.offset, + ((it.type & DEF_SAVEGLOBAL) ? " [SAVE]" : "")); } } if (opts_printfuns) { - for (i = 0; i < vec_size(prog->functions); ++i) { + for (auto &it : prog->functions) { int32_t a; printf("Function: %-16s taking %u parameters:(", - prog_getstring(prog, prog->functions[i].name), - (unsigned int)prog->functions[i].nargs); - for (a = 0; a < prog->functions[i].nargs; ++a) { - printf(" %i", prog->functions[i].argsize[a]); + prog_getstring(prog, it.name), + (unsigned int)it.nargs); + for (a = 0; a < it.nargs; ++a) { + printf(" %i", it.argsize[a]); } if (opts_v > 1) { - int32_t start = prog->functions[i].entry; + int32_t start = it.entry; if (start < 0) printf(") builtin %i\n", (int)-start); else { size_t funsize = 0; - prog_section_statement_t *st = prog->code + start; + prog_section_statement_t *st = &prog->code[0] + start; for (;st->opcode != INSTR_DONE; ++st) ++funsize; - printf(") - %lu instructions", (unsigned long)funsize); + printf(") - %zu instructions", funsize); if (opts_v > 2) { printf(" - locals: %i + %i\n", - prog->functions[i].firstlocal, - prog->functions[i].locals); + it.firstlocal, + it.locals); } else printf("\n"); @@ -1216,15 +1201,15 @@ int main(int argc, char **argv) { } else if (opts_v) { printf(") locals: %i + %i\n", - prog->functions[i].firstlocal, - prog->functions[i].locals); + it.firstlocal, + it.locals); } else printf(")\n"); } } if (!noexec) { - for (i = 1; i < vec_size(prog->functions); ++i) { + for (i = 1; i < prog->functions.size(); ++i) { const char *name = prog_getstring(prog, prog->functions[i].name); if (!strcmp(name, "main")) fnmain = (qcint_t)i; @@ -1243,7 +1228,7 @@ int main(int argc, char **argv) { } static void prog_disasm_function(qc_program_t *prog, size_t id) { - prog_section_function_t *fdef = prog->functions + id; + prog_section_function_t *fdef = &prog->functions[0] + id; prog_section_statement_t *st; if (fdef->entry < 0) { @@ -1253,7 +1238,7 @@ static void prog_disasm_function(qc_program_t *prog, size_t id) { else printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name)); - st = prog->code + fdef->entry; + st = &prog->code[0] + fdef->entry; while (st->opcode != INSTR_DONE) { prog_print_statement(prog, st); ++st; @@ -1268,11 +1253,11 @@ static void prog_disasm_function(qc_program_t *prog, size_t id) { * sort of isn't, which makes it nicer looking. */ -#define OPA ( (qcany_t*) (prog->globals + st->o1.u1) ) -#define OPB ( (qcany_t*) (prog->globals + st->o2.u1) ) -#define OPC ( (qcany_t*) (prog->globals + st->o3.u1) ) +#define OPA ( (qcany_t*) (&prog->globals[0] + st->o1.u1) ) +#define OPB ( (qcany_t*) (&prog->globals[0] + st->o2.u1) ) +#define OPC ( (qcany_t*) (&prog->globals[0] + st->o3.u1) ) -#define GLOBAL(x) ( (qcany_t*) (prog->globals + (x)) ) +#define GLOBAL(x) ( (qcany_t*) (&prog->globals[0] + (x)) ) /* to be consistent with current darkplaces behaviour */ #if !defined(FLOAT_IS_TRUE_FOR_INT) @@ -1287,7 +1272,7 @@ while (prog->vmerror == 0) { ++st; #if QCVM_PROFILE - prog->profile[st - prog->code]++; + prog->profile[st - &prog->code[0]]++; #endif #if QCVM_TRACE @@ -1307,7 +1292,7 @@ while (prog->vmerror == 0) { GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1]; GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2]; - st = prog->code + prog_leavefunction(prog); + st = &prog->code[0] + prog_leavefunction(prog); if (!vec_size(prog->stack)) goto cleanup; @@ -1561,7 +1546,7 @@ while (prog->vmerror == 0) { if (!OPA->function) qcvmerror(prog, "NULL function in `%s`", prog->filename); - if(!OPA->function || OPA->function >= (qcint_t)vec_size(prog->functions)) + if(!OPA->function || OPA->function >= (qcint_t)prog->functions.size()) { qcvmerror(prog, "CALL outside the program in `%s`", prog->filename); goto cleanup; @@ -1570,7 +1555,7 @@ while (prog->vmerror == 0) { newf = &prog->functions[OPA->function]; newf->profile++; - prog->statement = (st - prog->code) + 1; + prog->statement = (st - &prog->code[0]) + 1; if (newf->entry < 0) { @@ -1583,7 +1568,7 @@ while (prog->vmerror == 0) { builtinnumber, prog->filename); } else - st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */ + st = &prog->code[0] + prog_enterfunction(prog, newf) - 1; /* offset st++ */ if (prog->vmerror) goto cleanup; break; @@ -1603,7 +1588,7 @@ while (prog->vmerror == 0) { frame = (qcfloat_t*)&((qcint_t*)ed)[prog->cached_fields.frame]; *frame = OPA->_float; nextthink = (qcfloat_t*)&((qcint_t*)ed)[prog->cached_fields.nextthink]; - time = (qcfloat_t*)(prog->globals + prog->cached_globals.time); + time = (qcfloat_t*)(&prog->globals[0] + prog->cached_globals.time); *nextthink = *time + 0.1; break; } diff --git a/gmqcc.h b/gmqcc.h index 14a9b7c..34fefaa 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -1,5 +1,6 @@ #ifndef GMQCC_HDR #define GMQCC_HDR +#include #include #include #include @@ -486,29 +487,29 @@ enum { /* TODO: elide */ extern const char *util_instr_str[VINSTR_END]; -void util_swap_header (prog_header_t *code_header); -void util_swap_statements (prog_section_statement_t *statements); -void util_swap_defs_fields(prog_section_both_t *section); -void util_swap_functions (prog_section_function_t *functions); -void util_swap_globals (int32_t *globals); +void util_swap_header(prog_header_t &code_header); +void util_swap_statements(std::vector &statements); +void util_swap_defs_fields(std::vector §ion); +void util_swap_functions(std::vector &functions); +void util_swap_globals(std::vector &globals); -typedef float qcfloat_t; -typedef int32_t qcint_t; +typedef float qcfloat_t; +typedef int32_t qcint_t; typedef uint32_t qcuint_t; struct code_t { - prog_section_statement_t *statements; - int *linenums; - int *columnnums; - prog_section_def_t *defs; - prog_section_field_t *fields; - prog_section_function_t *functions; - int *globals; - char *chars; - uint16_t crc; - uint32_t entfields; - ht string_cache; - qcint_t string_cached_empty; + std::vector statements; + std::vector linenums; + std::vector columnnums; + std::vector defs; + std::vector fields; + std::vector functions; + std::vector globals; + std::vector chars; + uint16_t crc; + uint32_t entfields; + ht string_cache; + qcint_t string_cached_empty; }; /* @@ -517,8 +518,8 @@ struct code_t { */ struct lex_ctx_t { const char *file; - size_t line; - size_t column; + size_t line; + size_t column; }; /* @@ -645,17 +646,17 @@ struct qc_exec_stack_t { }; struct qc_program_t { - char *filename; - prog_section_statement_t *code; - prog_section_def_t *defs; - prog_section_def_t *fields; - prog_section_function_t *functions; - char *strings; - qcint_t *globals; - qcint_t *entitydata; - bool *entitypool; - - const char* *function_stack; + char *filename; + std::vector code; + std::vector defs; + std::vector fields; + std::vector functions; + std::vector strings; + std::vector globals; + qcint_t *entitydata; + bool *entitypool; + + const char* *function_stack; uint16_t crc16; diff --git a/ir.cpp b/ir.cpp index 01aa8f2..509fc3b 100644 --- a/ir.cpp +++ b/ir.cpp @@ -2747,20 +2747,20 @@ static bool gen_global_field(code_t *code, ir_value *global) } /* copy the field's value */ - ir_value_code_setaddr(global, vec_size(code->globals)); - vec_push(code->globals, fld->code.fieldaddr); + ir_value_code_setaddr(global, code->globals.size()); + code->globals.push_back(fld->code.fieldaddr); if (global->fieldtype == TYPE_VECTOR) { - vec_push(code->globals, fld->code.fieldaddr+1); - vec_push(code->globals, fld->code.fieldaddr+2); + code->globals.push_back(fld->code.fieldaddr+1); + code->globals.push_back(fld->code.fieldaddr+2); } } else { - ir_value_code_setaddr(global, vec_size(code->globals)); - vec_push(code->globals, 0); + ir_value_code_setaddr(global, code->globals.size()); + code->globals.push_back(0); if (global->fieldtype == TYPE_VECTOR) { - vec_push(code->globals, 0); - vec_push(code->globals, 0); + code->globals.push_back(0); + code->globals.push_back(0); } } if (global->code.globaladdr < 0) @@ -2794,13 +2794,13 @@ static bool gen_global_pointer(code_t *code, ir_value *global) return false; } - ir_value_code_setaddr(global, vec_size(code->globals)); - vec_push(code->globals, target->code.globaladdr); + ir_value_code_setaddr(global, code->globals.size()); + code->globals.push_back(target->code.globaladdr); } else { - ir_value_code_setaddr(global, vec_size(code->globals)); - vec_push(code->globals, 0); + ir_value_code_setaddr(global, code->globals.size()); + code->globals.push_back(0); } if (global->code.globaladdr < 0) return false; @@ -2819,7 +2819,7 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc int j; block->generated = true; - block->code_start = vec_size(code->statements); + block->code_start = code->statements.size(); for (i = 0; i < vec_size(block->instr); ++i) { instr = block->instr[i]; @@ -2839,7 +2839,7 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc /* otherwise we generate a jump instruction */ stmt.opcode = INSTR_GOTO; - stmt.o1.s1 = (target->code_start) - vec_size(code->statements); + stmt.o1.s1 = target->code_start - code->statements.size(); stmt.o2.s1 = 0; stmt.o3.s1 = 0; if (stmt.o1.s1 != 1) @@ -3023,13 +3023,13 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc if (ontrue->generated) { stmt.opcode = INSTR_IF; - stmt.o2.s1 = (ontrue->code_start) - vec_size(code->statements); + stmt.o2.s1 = ontrue->code_start - code->statements.size(); if (stmt.o2.s1 != 1) code_push_statement(code, &stmt, instr->context); } if (onfalse->generated) { stmt.opcode = INSTR_IFNOT; - stmt.o2.s1 = (onfalse->code_start) - vec_size(code->statements); + stmt.o2.s1 = onfalse->code_start - code->statements.size(); if (stmt.o2.s1 != 1) code_push_statement(code, &stmt, instr->context); } @@ -3050,24 +3050,24 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc onfalse = ontrue; ontrue = tmp; } - stidx = vec_size(code->statements); + stidx = code->statements.size(); code_push_statement(code, &stmt, instr->context); /* on false we jump, so add ontrue-path */ if (!gen_blocks_recursive(code, func, ontrue)) return false; /* fixup the jump address */ - code->statements[stidx].o2.s1 = vec_size(code->statements) - stidx; + code->statements[stidx].o2.s1 = code->statements.size() - stidx; /* generate onfalse path */ if (onfalse->generated) { /* fixup the jump address */ - code->statements[stidx].o2.s1 = (onfalse->code_start) - (stidx); - if (stidx+2 == vec_size(code->statements) && code->statements[stidx].o2.s1 == 1) { + code->statements[stidx].o2.s1 = onfalse->code_start - stidx; + if (stidx+2 == code->statements.size() && code->statements[stidx].o2.s1 == 1) { code->statements[stidx] = code->statements[stidx+1]; if (code->statements[stidx].o1.s1 < 0) code->statements[stidx].o1.s1++; code_pop_statement(code); } - stmt.opcode = vec_last(code->statements).opcode; + stmt.opcode = code->statements.back().opcode; if (stmt.opcode == INSTR_GOTO || stmt.opcode == INSTR_IF || stmt.opcode == INSTR_IFNOT || @@ -3079,14 +3079,14 @@ static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *bloc } /* may have been generated in the previous recursive call */ stmt.opcode = INSTR_GOTO; - stmt.o1.s1 = (onfalse->code_start) - vec_size(code->statements); + stmt.o1.s1 = onfalse->code_start - code->statements.size(); stmt.o2.s1 = 0; stmt.o3.s1 = 0; if (stmt.o1.s1 != 1) code_push_statement(code, &stmt, instr->context); return true; } - else if (stidx+2 == vec_size(code->statements) && code->statements[stidx].o2.s1 == 1) { + else if (stidx+2 == code->statements.size() && code->statements[stidx].o2.s1 == 1) { code->statements[stidx] = code->statements[stidx+1]; if (code->statements[stidx].o1.s1 < 0) code->statements[stidx].o1.s1++; @@ -3284,7 +3284,7 @@ static bool gen_function_code(code_t *code, ir_function *self) } /* code_write and qcvm -disasm need to know that the function ends here */ - retst = &vec_last(code->statements); + retst = &code->statements.back(); if (OPTS_OPTIMIZATION(OPTIM_VOID_RETURN) && self->outtype == TYPE_VOID && retst->opcode == INSTR_RETURN && @@ -3299,8 +3299,8 @@ static bool gen_function_code(code_t *code, ir_function *self) stmt.o1.u1 = 0; stmt.o2.u1 = 0; stmt.o3.u1 = 0; - last.line = vec_last(code->linenums); - last.column = vec_last(code->columnnums); + last.line = code->linenums.back(); + last.column = code->columnnums.back(); code_push_statement(code, &stmt, last); } @@ -3333,22 +3333,20 @@ static bool gen_global_function(ir_builder *ir, ir_value *global) size_t i; - if (!global->hasvalue || (!global->constval.vfunc)) - { + if (!global->hasvalue || (!global->constval.vfunc)) { irerror(global->context, "Invalid state of function-global: not constant: %s", global->name); return false; } irfun = global->constval.vfunc; - - fun.name = global->code.name; - fun.file = ir_builder_filestring(ir, global->context.file); + fun.name = global->code.name; + fun.file = ir_builder_filestring(ir, global->context.file); fun.profile = 0; /* always 0 */ - fun.nargs = vec_size(irfun->params); + fun.nargs = vec_size(irfun->params); if (fun.nargs > 8) fun.nargs = 8; - for (i = 0;i < 8; ++i) { + for (i = 0; i < 8; ++i) { if ((int32_t)i >= fun.nargs) fun.argsize[i] = 0; else @@ -3356,16 +3354,16 @@ static bool gen_global_function(ir_builder *ir, ir_value *global) } fun.firstlocal = 0; - fun.locals = irfun->allocated_locals; + fun.locals = irfun->allocated_locals; if (irfun->builtin) fun.entry = irfun->builtin+1; else { - irfun->code_function_def = vec_size(ir->code->functions); - fun.entry = vec_size(ir->code->statements); + irfun->code_function_def = ir->code->functions.size(); + fun.entry = ir->code->statements.size(); } - vec_push(ir->code->functions, fun); + ir->code->functions.push_back(fun); return true; } @@ -3391,17 +3389,17 @@ static void ir_gen_extparam(ir_builder *ir) else global = ir->extparam_protos[vec_size(ir->extparams)]; - def.name = code_genstring(ir->code, global->name); - def.type = TYPE_VECTOR; - def.offset = vec_size(ir->code->globals); + def.name = code_genstring(ir->code, global->name); + def.type = TYPE_VECTOR; + def.offset = ir->code->globals.size(); - vec_push(ir->code->defs, def); + ir->code->defs.push_back(def); ir_value_code_setaddr(global, def.offset); - vec_push(ir->code->globals, 0); - vec_push(ir->code->globals, 0); - vec_push(ir->code->globals, 0); + ir->code->globals.push_back(0); + ir->code->globals.push_back(0); + ir->code->globals.push_back(0); vec_push(ir->extparams, global); } @@ -3485,13 +3483,13 @@ static bool gen_function_locals(ir_builder *ir, ir_value *global) uint32_t firstlocal, firstglobal; irfun = global->constval.vfunc; - def = ir->code->functions + irfun->code_function_def; + def = &ir->code->functions[0] + irfun->code_function_def; if (OPTS_OPTION_BOOL(OPTION_G) || !OPTS_OPTIMIZATION(OPTIM_OVERLAP_LOCALS) || (irfun->flags & IR_FLAG_MASK_NO_OVERLAP)) { - firstlocal = def->firstlocal = vec_size(ir->code->globals); + firstlocal = def->firstlocal = ir->code->globals.size(); } else { firstlocal = def->firstlocal = ir->first_common_local; ++opts_optimizationcount[OPTIM_OVERLAP_LOCALS]; @@ -3499,8 +3497,8 @@ static bool gen_function_locals(ir_builder *ir, ir_value *global) firstglobal = (OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS) ? ir->first_common_globaltemp : firstlocal); - for (i = vec_size(ir->code->globals); i < firstlocal + irfun->allocated_locals; ++i) - vec_push(ir->code->globals, 0); + for (i = ir->code->globals.size(); i < firstlocal + irfun->allocated_locals; ++i) + ir->code->globals.push_back(0); for (i = 0; i < vec_size(irfun->locals); ++i) { ir_value *v = irfun->locals[i]; if (v->locked || !OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS)) { @@ -3568,7 +3566,7 @@ static bool gen_global_function_code(ir_builder *ir, ir_value *global) } fundef = &ir->code->functions[irfun->code_function_def]; - fundef->entry = vec_size(ir->code->statements); + fundef->entry = ir->code->statements.size(); if (!gen_function_locals(ir, global)) { irerror(irfun->context, "Failed to generate locals for function %s", irfun->name); return false; @@ -3610,7 +3608,7 @@ static void gen_vector_defs(code_t *code, prog_section_def_t def, const char *na for (i = 0; i < 3; ++i) { def.name = code_genstring(code, component); - vec_push(code->defs, def); + code->defs.push_back(def); def.offset++; component[len-1]++; } @@ -3640,7 +3638,7 @@ static void gen_vector_fields(code_t *code, prog_section_field_t fld, const char for (i = 0; i < 3; ++i) { fld.name = code_genstring(code, component); - vec_push(code->fields, fld); + code->fields.push_back(fld); fld.offset++; component[len-1]++; } @@ -3659,9 +3657,9 @@ static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool isloc if (global->vtype == TYPE_VECTOR && (global->flags & IR_FLAG_SPLIT_VECTOR)) return true; - def.type = global->vtype; - def.offset = vec_size(self->code->globals); - def.name = 0; + def.type = global->vtype; + def.offset = self->code->globals.size(); + def.name = 0; if (OPTS_OPTION_BOOL(OPTION_G) || !islocal) { pushdef = true; @@ -3694,7 +3692,7 @@ static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool isloc def.name = 0; if (islocal) { def.offset = ir_value_code_addr(global); - vec_push(self->code->defs, def); + self->code->defs.push_back(def); if (global->vtype == TYPE_VECTOR) gen_vector_defs(self->code, def, global->name); else if (global->vtype == TYPE_FIELD && global->fieldtype == TYPE_VECTOR) @@ -3728,17 +3726,17 @@ static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool isloc * Maybe this could be an -foption * fteqcc creates data for end_sys_* - of size 1, so let's do the same */ - ir_value_code_setaddr(global, vec_size(self->code->globals)); - vec_push(self->code->globals, 0); + ir_value_code_setaddr(global, self->code->globals.size()); + self->code->globals.push_back(0); /* Add the def */ - if (pushdef) vec_push(self->code->defs, def); + if (pushdef) self->code->defs.push_back(def); return true; case TYPE_POINTER: - if (pushdef) vec_push(self->code->defs, def); + if (pushdef) self->code->defs.push_back(def); return gen_global_pointer(self->code, global); case TYPE_FIELD: if (pushdef) { - vec_push(self->code->defs, def); + self->code->defs.push_back(def); if (global->fieldtype == TYPE_VECTOR) gen_vector_defs(self->code, def, global->name); } @@ -3747,84 +3745,84 @@ static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool isloc /* fall through */ case TYPE_FLOAT: { - ir_value_code_setaddr(global, vec_size(self->code->globals)); + ir_value_code_setaddr(global, self->code->globals.size()); if (global->hasvalue) { iptr = (int32_t*)&global->constval.ivec[0]; - vec_push(self->code->globals, *iptr); + self->code->globals.push_back(*iptr); } else { - vec_push(self->code->globals, 0); + self->code->globals.push_back(0); } if (!islocal && global->cvq != CV_CONST) def.type |= DEF_SAVEGLOBAL; - if (pushdef) vec_push(self->code->defs, def); + if (pushdef) self->code->defs.push_back(def); return global->code.globaladdr >= 0; } case TYPE_STRING: { - ir_value_code_setaddr(global, vec_size(self->code->globals)); + ir_value_code_setaddr(global, self->code->globals.size()); if (global->hasvalue) { uint32_t load = code_genstring(self->code, global->constval.vstring); - vec_push(self->code->globals, load); + self->code->globals.push_back(load); } else { - vec_push(self->code->globals, 0); + self->code->globals.push_back(0); } if (!islocal && global->cvq != CV_CONST) def.type |= DEF_SAVEGLOBAL; - if (pushdef) vec_push(self->code->defs, def); + if (pushdef) self->code->defs.push_back(def); return global->code.globaladdr >= 0; } case TYPE_VECTOR: { size_t d; - ir_value_code_setaddr(global, vec_size(self->code->globals)); + ir_value_code_setaddr(global, self->code->globals.size()); if (global->hasvalue) { iptr = (int32_t*)&global->constval.ivec[0]; - vec_push(self->code->globals, iptr[0]); + self->code->globals.push_back(iptr[0]); if (global->code.globaladdr < 0) return false; for (d = 1; d < type_sizeof_[global->vtype]; ++d) { - vec_push(self->code->globals, iptr[d]); + self->code->globals.push_back(iptr[d]); } } else { - vec_push(self->code->globals, 0); + self->code->globals.push_back(0); if (global->code.globaladdr < 0) return false; for (d = 1; d < type_sizeof_[global->vtype]; ++d) { - vec_push(self->code->globals, 0); + self->code->globals.push_back(0); } } if (!islocal && global->cvq != CV_CONST) def.type |= DEF_SAVEGLOBAL; if (pushdef) { - vec_push(self->code->defs, def); + self->code->defs.push_back(def); def.type &= ~DEF_SAVEGLOBAL; gen_vector_defs(self->code, def, global->name); } return global->code.globaladdr >= 0; } case TYPE_FUNCTION: - ir_value_code_setaddr(global, vec_size(self->code->globals)); + ir_value_code_setaddr(global, self->code->globals.size()); if (!global->hasvalue) { - vec_push(self->code->globals, 0); + self->code->globals.push_back(0); if (global->code.globaladdr < 0) return false; } else { - vec_push(self->code->globals, vec_size(self->code->functions)); + self->code->globals.push_back(self->code->functions.size()); if (!gen_global_function(self, global)) return false; } if (!islocal && global->cvq != CV_CONST) def.type |= DEF_SAVEGLOBAL; - if (pushdef) vec_push(self->code->defs, def); + if (pushdef) self->code->defs.push_back(def); return true; case TYPE_VARIANT: /* assume biggest type */ - ir_value_code_setaddr(global, vec_size(self->code->globals)); - vec_push(self->code->globals, 0); + ir_value_code_setaddr(global, self->code->globals.size()); + self->code->globals.push_back(0); for (i = 1; i < type_sizeof_[TYPE_VARIANT]; ++i) - vec_push(self->code->globals, 0); + self->code->globals.push_back(0); return true; default: /* refuse to create 'void' type or any other fancy business. */ @@ -3847,7 +3845,7 @@ static bool ir_builder_gen_field(ir_builder *self, ir_value *field) (void)self; def.type = (uint16_t)field->vtype; - def.offset = (uint16_t)vec_size(self->code->globals); + def.offset = (uint16_t)self->code->globals.size(); /* create a global named the same as the field */ if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) { @@ -3881,7 +3879,7 @@ static bool ir_builder_gen_field(ir_builder *self, ir_value *field) field->code.name = def.name; - vec_push(self->code->defs, def); + self->code->defs.push_back(def); fld.type = field->fieldtype; @@ -3892,13 +3890,13 @@ static bool ir_builder_gen_field(ir_builder *self, ir_value *field) fld.offset = field->code.fieldaddr; - vec_push(self->code->fields, fld); + self->code->fields.push_back(fld); - ir_value_code_setaddr(field, vec_size(self->code->globals)); - vec_push(self->code->globals, fld.offset); + ir_value_code_setaddr(field, self->code->globals.size()); + self->code->globals.push_back(fld.offset); if (fld.type == TYPE_VECTOR) { - vec_push(self->code->globals, fld.offset+1); - vec_push(self->code->globals, fld.offset+2); + self->code->globals.push_back(fld.offset+1); + self->code->globals.push_back(fld.offset+2); } if (field->fieldtype == TYPE_VECTOR) { @@ -4046,28 +4044,28 @@ bool ir_builder_generate(ir_builder *self, const char *filename) } /* generate nil */ - ir_value_code_setaddr(self->nil, vec_size(self->code->globals)); - vec_push(self->code->globals, 0); - vec_push(self->code->globals, 0); - vec_push(self->code->globals, 0); + ir_value_code_setaddr(self->nil, self->code->globals.size()); + self->code->globals.push_back(0); + self->code->globals.push_back(0); + self->code->globals.push_back(0); /* generate virtual-instruction temps */ for (i = 0; i < IR_MAX_VINSTR_TEMPS; ++i) { - ir_value_code_setaddr(self->vinstr_temp[i], vec_size(self->code->globals)); - vec_push(self->code->globals, 0); - vec_push(self->code->globals, 0); - vec_push(self->code->globals, 0); + ir_value_code_setaddr(self->vinstr_temp[i], self->code->globals.size()); + self->code->globals.push_back(0); + self->code->globals.push_back(0); + self->code->globals.push_back(0); } /* generate global temps */ - self->first_common_globaltemp = vec_size(self->code->globals); + self->first_common_globaltemp = self->code->globals.size(); for (i = 0; i < self->max_globaltemps; ++i) { - vec_push(self->code->globals, 0); + self->code->globals.push_back(0); } /* generate common locals */ - self->first_common_local = vec_size(self->code->globals); + self->first_common_local = self->code->globals.size(); for (i = 0; i < self->max_locals; ++i) { - vec_push(self->code->globals, 0); + self->code->globals.push_back(0); } /* generate function code */ @@ -4080,13 +4078,15 @@ bool ir_builder_generate(ir_builder *self, const char *filename) } } - if (vec_size(self->code->globals) >= 65536) { - irerror(vec_last(self->globals)->context, "This progs file would require more globals than the metadata can handle (%u). Bailing out.", (unsigned int)vec_size(self->code->globals)); + if (self->code->globals.size() >= 65536) { + irerror(vec_last(self->globals)->context, + "This progs file would require more globals than the metadata can handle (%zu). Bailing out.", + self->code->globals.size()); return false; } /* DP errors if the last instruction is not an INSTR_DONE. */ - if (vec_last(self->code->statements).opcode != INSTR_DONE) + if (self->code->statements.back().opcode != INSTR_DONE) { lex_ctx_t last; @@ -4094,8 +4094,8 @@ bool ir_builder_generate(ir_builder *self, const char *filename) stmt.o1.u1 = 0; stmt.o2.u1 = 0; stmt.o3.u1 = 0; - last.line = vec_last(self->code->linenums); - last.column = vec_last(self->code->columnnums); + last.line = self->code->linenums.back(); + last.column = self->code->columnnums.back(); code_push_statement(self->code, &stmt, last); } @@ -4103,10 +4103,10 @@ bool ir_builder_generate(ir_builder *self, const char *filename) if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) return true; - if (vec_size(self->code->statements) != vec_size(self->code->linenums)) { + if (self->code->statements.size() != self->code->linenums.size()) { con_err("Linecounter wrong: %lu != %lu\n", - (unsigned long)vec_size(self->code->statements), - (unsigned long)vec_size(self->code->linenums)); + self->code->statements.size(), + self->code->linenums.size()); } else if (OPTS_FLAG(LNO)) { char *dot; size_t filelen = strlen(filename); diff --git a/util.cpp b/util.cpp index ceed449..9c1ef6a 100644 --- a/util.cpp +++ b/util.cpp @@ -87,62 +87,56 @@ void util_endianswap(void *_data, size_t count, unsigned int typesize) { #endif } -void util_swap_header(prog_header_t *code_header) { - util_endianswap(&code_header->version, 1, sizeof(code_header->version)); - util_endianswap(&code_header->crc16, 1, sizeof(code_header->crc16)); - util_endianswap(&code_header->statements.offset, 1, sizeof(code_header->statements.offset)); - util_endianswap(&code_header->statements.length, 1, sizeof(code_header->statements.length)); - util_endianswap(&code_header->defs.offset, 1, sizeof(code_header->defs.offset)); - util_endianswap(&code_header->defs.length, 1, sizeof(code_header->defs.length)); - util_endianswap(&code_header->fields.offset, 1, sizeof(code_header->fields.offset)); - util_endianswap(&code_header->fields.length, 1, sizeof(code_header->fields.length)); - util_endianswap(&code_header->functions.offset, 1, sizeof(code_header->functions.offset)); - util_endianswap(&code_header->functions.length, 1, sizeof(code_header->functions.length)); - util_endianswap(&code_header->strings.offset, 1, sizeof(code_header->strings.offset)); - util_endianswap(&code_header->strings.length, 1, sizeof(code_header->strings.length)); - util_endianswap(&code_header->globals.offset, 1, sizeof(code_header->globals.offset)); - util_endianswap(&code_header->globals.length, 1, sizeof(code_header->globals.length)); - util_endianswap(&code_header->entfield, 1, sizeof(code_header->entfield)); +void util_swap_header(prog_header_t &code_header) { + util_endianswap(&code_header.version, 1, sizeof(code_header.version)); + util_endianswap(&code_header.crc16, 1, sizeof(code_header.crc16)); + util_endianswap(&code_header.statements.offset, 1, sizeof(code_header.statements.offset)); + util_endianswap(&code_header.statements.length, 1, sizeof(code_header.statements.length)); + util_endianswap(&code_header.defs.offset, 1, sizeof(code_header.defs.offset)); + util_endianswap(&code_header.defs.length, 1, sizeof(code_header.defs.length)); + util_endianswap(&code_header.fields.offset, 1, sizeof(code_header.fields.offset)); + util_endianswap(&code_header.fields.length, 1, sizeof(code_header.fields.length)); + util_endianswap(&code_header.functions.offset, 1, sizeof(code_header.functions.offset)); + util_endianswap(&code_header.functions.length, 1, sizeof(code_header.functions.length)); + util_endianswap(&code_header.strings.offset, 1, sizeof(code_header.strings.offset)); + util_endianswap(&code_header.strings.length, 1, sizeof(code_header.strings.length)); + util_endianswap(&code_header.globals.offset, 1, sizeof(code_header.globals.offset)); + util_endianswap(&code_header.globals.length, 1, sizeof(code_header.globals.length)); + util_endianswap(&code_header.entfield, 1, sizeof(code_header.entfield)); } -void util_swap_statements(prog_section_statement_t *statements) { - size_t i; - - for (i = 0; i < vec_size(statements); ++i) { - util_endianswap(&statements[i].opcode, 1, sizeof(statements[i].opcode)); - util_endianswap(&statements[i].o1, 1, sizeof(statements[i].o1)); - util_endianswap(&statements[i].o2, 1, sizeof(statements[i].o2)); - util_endianswap(&statements[i].o3, 1, sizeof(statements[i].o3)); +void util_swap_statements(std::vector &statements) { + for (auto &it : statements) { + util_endianswap(&it.opcode, 1, sizeof(it.opcode)); + util_endianswap(&it.o1, 1, sizeof(it.o1)); + util_endianswap(&it.o2, 1, sizeof(it.o2)); + util_endianswap(&it.o3, 1, sizeof(it.o3)); } } -void util_swap_defs_fields(prog_section_both_t *section) { - size_t i; - - for (i = 0; i < vec_size(section); ++i) { - util_endianswap(§ion[i].type, 1, sizeof(section[i].type)); - util_endianswap(§ion[i].offset, 1, sizeof(section[i].offset)); - util_endianswap(§ion[i].name, 1, sizeof(section[i].name)); +void util_swap_defs_fields(std::vector §ion) { + for (auto &it : section) { + util_endianswap(&it.type, 1, sizeof(it.type)); + util_endianswap(&it.offset, 1, sizeof(it.offset)); + util_endianswap(&it.name, 1, sizeof(it.name)); } } -void util_swap_functions(prog_section_function_t *functions) { - size_t i; - - for (i = 0; i < vec_size(functions); ++i) { - util_endianswap(&functions[i].entry, 1, sizeof(functions[i].entry)); - util_endianswap(&functions[i].firstlocal, 1, sizeof(functions[i].firstlocal)); - util_endianswap(&functions[i].locals, 1, sizeof(functions[i].locals)); - util_endianswap(&functions[i].profile, 1, sizeof(functions[i].profile)); - util_endianswap(&functions[i].name, 1, sizeof(functions[i].name)); - util_endianswap(&functions[i].file, 1, sizeof(functions[i].file)); - util_endianswap(&functions[i].nargs, 1, sizeof(functions[i].nargs)); +void util_swap_functions(std::vector &functions) { + for (auto &it : functions) { + util_endianswap(&it.entry, 1, sizeof(it.entry)); + util_endianswap(&it.firstlocal, 1, sizeof(it.firstlocal)); + util_endianswap(&it.locals, 1, sizeof(it.locals)); + util_endianswap(&it.profile, 1, sizeof(it.profile)); + util_endianswap(&it.name, 1, sizeof(it.name)); + util_endianswap(&it.file, 1, sizeof(it.file)); + util_endianswap(&it.nargs, 1, sizeof(it.nargs)); /* Don't swap argsize[] - it's just a byte array, which Quake uses only as such. */ } } -void util_swap_globals(int32_t *globals) { - util_endianswap(globals, vec_size(globals), sizeof(int32_t)); +void util_swap_globals(std::vector &globals) { + util_endianswap(&globals[0], globals.size(), sizeof(int32_t)); } /* -- 2.39.2