X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=exec.c;h=d5448153072039287445a609555ad11fed39bf84;hp=8fe29434107ef1b67bc3dfea2704e7a4b3079ea0;hb=e11a17b40805b93e1e2ae80129c9d235b6d6119b;hpb=890ca3c782c7c8dfc666e84b0898b8766de6f1dc diff --git a/exec.c b/exec.c index 8fe2943..d544815 100644 --- a/exec.c +++ b/exec.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012 + * Copyright (C) 2012, 2013 * Wolfgang Bumiller * Dale Weiler * @@ -51,7 +51,7 @@ static void qcvmerror(qc_program *prog, const char *fmt, ...) putchar('\n'); } -qc_program* prog_load(const char *filename) +qc_program* prog_load(const char *filename, bool skipversion) { qc_program *prog; prog_header header; @@ -66,7 +66,7 @@ qc_program* prog_load(const char *filename) return NULL; } - if (header.version != 6) { + if (!skipversion && header.version != 6) { loaderror("header says this is a version %i progs, we need version 6\n", header.version); file_close(file); return NULL; @@ -75,7 +75,7 @@ qc_program* prog_load(const char *filename) prog = (qc_program*)mem_a(sizeof(qc_program)); if (!prog) { file_close(file); - printf("failed to allocate program data\n"); + fprintf(stderr, "failed to allocate program data\n"); return NULL; } memset(prog, 0, sizeof(*prog)); @@ -199,7 +199,7 @@ qcany* prog_getedict(qc_program *prog, qcint e) { if (e >= (qcint)vec_size(prog->entitypool)) { prog->vmerror++; - printf("Accessing out of bounds edict %i\n", (int)e); + fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e); e = 0; } return (qcany*)(prog->entitydata + (prog->entityfields * e)); @@ -227,29 +227,24 @@ void prog_free_entity(qc_program *prog, qcint e) { if (!e) { prog->vmerror++; - printf("Trying to free world entity\n"); + fprintf(stderr, "Trying to free world entity\n"); return; } if (e >= (qcint)vec_size(prog->entitypool)) { prog->vmerror++; - printf("Trying to free out of bounds entity\n"); + fprintf(stderr, "Trying to free out of bounds entity\n"); return; } if (!prog->entitypool[e]) { prog->vmerror++; - printf("Double free on entity\n"); + fprintf(stderr, "Double free on entity\n"); return; } prog->entitypool[e] = false; } -qcint prog_tempstring(qc_program *prog, const char *_str) +qcint prog_tempstring(qc_program *prog, const char *str) { - /* we don't access it, but the macro-generated functions don't use - * const - */ - char *str = (char*)_str; - size_t len = strlen(str); size_t at = prog->tempstring_at; @@ -359,7 +354,7 @@ static void trace_print_global(qc_program *prog, unsigned int glob, int vtype) done: if (len < (int)sizeof(spaces)-1) { spaces[sizeof(spaces)-1-len] = 0; - printf(spaces); + file_puts(stdout, spaces); spaces[sizeof(spaces)-1-len] = ' '; } } @@ -475,7 +470,8 @@ static qcint prog_enterfunction(qc_program *prog, prog_section_function *func) st.function = func; if (prog->xflags & VMXF_TRACE) { - vec_push(prog->function_stack, prog_getstring(prog, func->name)); + const char *str = prog_getstring(prog, func->name); + vec_push(prog->function_stack, str); } #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS @@ -613,10 +609,16 @@ const char *type_name[TYPE_COUNT] = { "field", "function", "pointer", -#if 0 "integer", -#endif - "variant" + + "variant", + + "struct", + "union", + "array", + + "nil", + "noexpr" }; typedef struct { @@ -629,7 +631,7 @@ qcvm_parameter *main_params = NULL; #define CheckArgs(num) do { \ if (prog->argc != (num)) { \ prog->vmerror++; \ - printf("ERROR: invalid number of arguments for %s: %i, expected %i\n", \ + fprintf(stderr, "ERROR: invalid number of arguments for %s: %i, expected %i\n", \ __FUNCTION__, prog->argc, (num)); \ return -1; \ } \ @@ -658,7 +660,7 @@ static int qc_print(qc_program *prog) static int qc_error(qc_program *prog) { - printf("*** VM raised an error:\n"); + fprintf(stderr, "*** VM raised an error:\n"); qc_print(prog); prog->vmerror++; return -1; @@ -732,6 +734,16 @@ static int qc_kill(qc_program *prog) return 0; } +static int qc_sqrt(qc_program *prog) +{ + qcany *num, out; + CheckArgs(1); + num = GetArg(0); + out._float = sqrt(num->_float); + Return(out); + return 0; +} + static int qc_vlen(qc_program *prog) { qcany *vec, len; @@ -744,6 +756,27 @@ static int qc_vlen(qc_program *prog) return 0; } +static int qc_normalize(qc_program *prog) +{ + double len; + qcany *vec; + qcany out; + CheckArgs(1); + vec = GetArg(0); + len = sqrt(vec->vector[0] * vec->vector[0] + + vec->vector[1] * vec->vector[1] + + vec->vector[2] * vec->vector[2]); + if (len) + len = 1.0 / len; + else + len = 0; + out.vector[0] = len * vec->vector[0]; + out.vector[1] = len * vec->vector[1]; + out.vector[2] = len * vec->vector[2]; + Return(out); + return 0; +} + static int qc_strcat(qc_program *prog) { char *buffer; @@ -775,7 +808,7 @@ static int qc_strcmp(qc_program *prog) qcany out; if (prog->argc != 2 && prog->argc != 3) { - printf("ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n", + fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n", prog->argc); return -1; } @@ -794,17 +827,19 @@ static int qc_strcmp(qc_program *prog) static prog_builtin qc_builtins[] = { NULL, - &qc_print, /* 1 */ - &qc_ftos, /* 2 */ - &qc_spawn, /* 3 */ - &qc_kill, /* 4 */ - &qc_vtos, /* 5 */ - &qc_error, /* 6 */ - &qc_vlen, /* 7 */ - &qc_etos, /* 8 */ - &qc_stof, /* 9 */ - &qc_strcat, /* 10 */ - &qc_strcmp /* 11 */ + &qc_print, /* 1 */ + &qc_ftos, /* 2 */ + &qc_spawn, /* 3 */ + &qc_kill, /* 4 */ + &qc_vtos, /* 5 */ + &qc_error, /* 6 */ + &qc_vlen, /* 7 */ + &qc_etos, /* 8 */ + &qc_stof, /* 9 */ + &qc_strcat, /* 10 */ + &qc_strcmp, /* 11 */ + &qc_normalize, /* 12 */ + &qc_sqrt /* 13 */ }; static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]); @@ -832,7 +867,9 @@ static void usage() " -disasm-func func disassemble and exit\n" " -printdefs list the defs section\n" " -printfields list the field section\n" - " -printfuns list functions information\n"); + " -printfuns list functions information\n" + " -v be verbose\n" + " -vv be even more verbose\n"); printf("parameters:\n"); printf(" -vector pass a vector parameter to main()\n" " -float pass a float parameter to main()\n" @@ -870,7 +907,7 @@ static void prog_main_setparams(qc_program *prog) arg->string = prog_tempstring(prog, main_params[i].value); break; default: - printf("error: unhandled parameter type: %i\n", main_params[i].vtype); + fprintf(stderr, "error: unhandled parameter type: %i\n", main_params[i].vtype); break; } } @@ -891,6 +928,7 @@ int main(int argc, char **argv) bool noexec = false; const char *progsfile = NULL; const char **dis_list = NULL; + int opts_v = 0; arg0 = argv[0]; @@ -907,8 +945,25 @@ int main(int argc, char **argv) usage(); exit(0); } - else if (!strcmp(argv[1], "-v") || - !strcmp(argv[1], "-version") || + else if (!strcmp(argv[1], "-v")) { + ++opts_v; + --argc; + ++argv; + } + else if (!strncmp(argv[1], "-vv", 3)) { + const char *av = argv[1]+1; + for (; *av; ++av) { + if (*av == 'v') + ++opts_v; + else { + usage(); + exit(1); + } + } + --argc; + ++argv; + } + else if (!strcmp(argv[1], "-version") || !strcmp(argv[1], "--version")) { version(); @@ -980,7 +1035,7 @@ int main(int argc, char **argv) --argc; ++argv; - if (argc < 3) { + if (argc < 2) { usage(); exit(1); } @@ -997,7 +1052,7 @@ int main(int argc, char **argv) } else if (argv[1][0] != '-') { if (progsfile) { - printf("only 1 program file may be specified\n"); + fprintf(stderr, "only 1 program file may be specified\n"); usage(); exit(1); } @@ -1007,34 +1062,27 @@ int main(int argc, char **argv) } else { + fprintf(stderr, "unknown parameter: %s\n", argv[1]); usage(); exit(1); } } - if (argc > 2) { - usage(); - exit(1); - } - if (argc > 1) { - if (progsfile) { - printf("only 1 program file may be specified\n"); - usage(); - exit(1); - } + if (argc == 2 && !progsfile) { progsfile = argv[1]; --argc; ++argv; } if (!progsfile) { + fprintf(stderr, "must specify a program to execute\n"); usage(); exit(1); } - prog = prog_load(progsfile); + prog = prog_load(progsfile, noexec); if (!prog) { - printf("failed to load program '%s'\n", progsfile); + fprintf(stderr, "failed to load program '%s'\n", progsfile); exit(1); } @@ -1080,11 +1128,33 @@ int main(int argc, char **argv) } if (opts_printdefs) { for (i = 0; i < vec_size(prog->defs); ++i) { - printf("Global: %8s %-16s at %u%s\n", + 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]" : "")); + if (opts_v) { + switch (prog->defs[i].type & DEF_TYPEMASK) { + case TYPE_FLOAT: + printf(" [init: %g]", ((qcany*)(prog->globals + prog->defs[i].offset))->_float); + break; + case TYPE_INTEGER: + printf(" [init: %i]", (int)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int )); + break; + case TYPE_ENTITY: + case TYPE_FUNCTION: + case TYPE_FIELD: + case TYPE_POINTER: + printf(" [init: %u]", (unsigned)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int )); + break; + case TYPE_STRING: + printf(" [init: `%s`]", prog_getstring(prog, ((qcany*)(prog->globals + prog->defs[i].offset))->string )); + break; + default: + break; + } + } + printf("\n"); } } if (opts_printfields) { @@ -1105,9 +1175,32 @@ int main(int argc, char **argv) for (a = 0; a < prog->functions[i].nargs; ++a) { printf(" %i", prog->functions[i].argsize[a]); } - printf(") locals: %i + %i\n", - prog->functions[i].firstlocal, - prog->functions[i].locals); + if (opts_v > 1) { + int32_t start = prog->functions[i].entry; + if (start < 0) + printf(") builtin %i\n", (int)-start); + else { + size_t funsize = 0; + prog_section_statement *st = prog->code + start; + for (;st->opcode != INSTR_DONE; ++st) + ++funsize; + printf(") - %lu instructions", (unsigned long)funsize); + if (opts_v > 2) { + printf(" - locals: %i + %i\n", + prog->functions[i].firstlocal, + prog->functions[i].locals); + } + else + printf("\n"); + } + } + else if (opts_v) { + printf(") locals: %i + %i\n", + prog->functions[i].firstlocal, + prog->functions[i].locals); + } + else + printf(")\n"); } } if (!noexec) { @@ -1122,7 +1215,7 @@ int main(int argc, char **argv) prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT); } else - printf("No main function found\n"); + fprintf(stderr, "No main function found\n"); } prog_delete(prog); @@ -1211,15 +1304,21 @@ while (1) { OPA->vector[2]*OPB->vector[2]; break; case INSTR_MUL_FV: - OPC->vector[0] = OPA->_float * OPB->vector[0]; - OPC->vector[1] = OPA->_float * OPB->vector[1]; - OPC->vector[2] = OPA->_float * OPB->vector[2]; + { + qcfloat f = OPA->_float; + OPC->vector[0] = f * OPB->vector[0]; + OPC->vector[1] = f * OPB->vector[1]; + OPC->vector[2] = f * OPB->vector[2]; break; + } case INSTR_MUL_VF: - OPC->vector[0] = OPB->_float * OPA->vector[0]; - OPC->vector[1] = OPB->_float * OPA->vector[1]; - OPC->vector[2] = OPB->_float * OPA->vector[2]; + { + qcfloat f = OPB->_float; + OPC->vector[0] = f * OPA->vector[0]; + OPC->vector[1] = f * OPA->vector[1]; + OPC->vector[2] = f * OPA->vector[2]; break; + } case INSTR_DIV_F: if (OPB->_float != 0.0f) OPC->_float = OPA->_float / OPB->_float; @@ -1325,9 +1424,10 @@ while (1) { goto cleanup; } ed = prog_getedict(prog, OPA->edict); - OPC->ivector[0] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[0]; - OPC->ivector[1] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[1]; - OPC->ivector[2] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[2]; + ptr = (qcany*)( ((qcint*)ed) + OPB->_int ); + OPC->ivector[0] = ptr->ivector[0]; + OPC->ivector[1] = ptr->ivector[1]; + OPC->ivector[2] = ptr->ivector[2]; break; case INSTR_ADDRESS: