X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=exec.c;h=2e1f58d476baf9f99c622153dcfaa5093b884505;hb=fe3b1b2e8d5e8672e6a15ccb476a1fecc6f30ff7;hp=363fb44049bb229279a0befb754bd00306f5ad52;hpb=919d73b0a0167b53875d39eead37222e32aebbed;p=xonotic%2Fgmqcc.git diff --git a/exec.c b/exec.c index 363fb44..2e1f58d 100644 --- a/exec.c +++ b/exec.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -35,9 +36,12 @@ static void loaderror(const char *fmt, ...) printf(": %s\n", strerror(err)); } -static void printvmerr(const char *fmt, ...) +static void qcvmerror(qc_program *prog, const char *fmt, ...) { va_list ap; + + prog->vmerror++; + va_start(ap, fmt); vprintf(fmt, ap); va_end(ap); @@ -51,7 +55,7 @@ qc_program* prog_load(const char *filename) size_t i; FILE *file; - file = fopen(filename, "rb"); + file = util_fopen(filename, "rb"); if (!file) return NULL; @@ -130,6 +134,7 @@ qc_program* prog_load(const char *filename) goto error; } } + prog->entities = 1; return prog; @@ -226,6 +231,7 @@ qcint prog_spawn_entity(qc_program *prog) printf("Failed to allocate entity\n"); return 0; } + prog->entities++; for (i = 0; i < prog->entityfields; ++i) { if (!qc_program_entitydata_add(prog, 0)) { printf("Failed to allocate entity\n"); @@ -314,13 +320,15 @@ static int print_escaped_string(const char *str) static void trace_print_global(qc_program *prog, unsigned int glob, int vtype) { - static char spaces[16+1] = " "; + static char spaces[40+1] = " "; prog_section_def *def; qcany *value; int len; - if (!glob) - return; + if (!glob) { + len = printf(","); + goto done; + } def = prog_getdef(prog, glob); value = (qcany*)(&prog->globals[glob]); @@ -347,6 +355,7 @@ static void trace_print_global(qc_program *prog, unsigned int glob, int vtype) break; case TYPE_STRING: len += print_escaped_string(prog_getstring(prog, value->string)); + len += printf(","); /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */ break; case TYPE_FLOAT: @@ -354,10 +363,11 @@ static void trace_print_global(qc_program *prog, unsigned int glob, int vtype) len += printf("%g,", value->_float); break; } - if (len < 16) { - spaces[16-len] = 0; +done: + if (len < sizeof(spaces)-1) { + spaces[sizeof(spaces)-1-len] = 0; printf(spaces); - spaces[16-len] = ' '; + spaces[sizeof(spaces)-1-len] = ' '; } } @@ -407,18 +417,50 @@ static void prog_print_statement(qc_program *prog, prog_section_statement *st) case INSTR_NE_S: t[0] = t[1] = TYPE_STRING; break; + case INSTR_STORE_F: + case INSTR_STOREP_F: + t[2] = -1; + break; case INSTR_STORE_V: t[0] = t[1] = TYPE_VECTOR; t[2] = -1; break; case INSTR_STORE_S: t[0] = t[1] = TYPE_STRING; t[2] = -1; break; + case INSTR_STORE_ENT: + t[0] = t[1] = TYPE_ENTITY; t[2] = -1; + break; + case INSTR_STORE_FLD: + t[0] = t[1] = TYPE_FIELD; t[2] = -1; + break; + case INSTR_STORE_FNC: + t[0] = t[1] = TYPE_FUNCTION; t[2] = -1; + break; + case INSTR_STOREP_V: + t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1; + break; + case INSTR_STOREP_S: + t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1; + break; + case INSTR_STOREP_ENT: + t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1; + break; + case INSTR_STOREP_FLD: + t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1; + break; + case INSTR_STOREP_FNC: + t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1; + break; } if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]); + else printf("(none), "); if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]); + else printf("(none), "); if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]); + else printf("(none)"); printf("\n"); } + fflush(stdout); } static qcint prog_enterfunction(qc_program *prog, prog_section_function *func) @@ -506,7 +548,7 @@ static qcint prog_leavefunction(qc_program *prog) exit(1); } - return st.stmt; + return st.stmt - 1; /* offset the ++st */ } bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps) @@ -582,8 +624,11 @@ bool opts_memchk = false; static int qc_print(qc_program *prog) { - qcany *str = (qcany*)(prog->globals + OFS_PARM0); - printf("%s", prog_getstring(prog, str->string)); + size_t i; + for (i = 0; i < prog->argc; ++i) { + qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i); + printf("%s", prog_getstring(prog, str->string)); + } return 0; } @@ -627,17 +672,42 @@ static prog_builtin qc_builtins[] = { }; static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]); +static const char *arg0 = NULL; + +void usage() +{ + printf("usage: [-debug] %s file\n", arg0); + exit(1); +} + int main(int argc, char **argv) { size_t i; qcint fnmain = -1; qc_program *prog; + size_t xflags = VMXF_DEFAULT; - if (argc != 2) { - printf("usage: %s file\n", argv[0]); - exit(1); + arg0 = argv[0]; + + if (argc < 2) + usage(); + + while (argc > 2) { + if (!strcmp(argv[1], "-trace")) { + --argc; + ++argv; + xflags |= VMXF_TRACE; + } + else if (!strcmp(argv[1], "-profile")) { + --argc; + ++argv; + xflags |= VMXF_PROFILE; + } + else + usage(); } + prog = prog_load(argv[1]); if (!prog) { printf("failed to load program '%s'\n", argv[1]); @@ -650,13 +720,14 @@ int main(int argc, char **argv) for (i = 1; i < prog->functions_count; ++i) { const char *name = prog_getstring(prog, prog->functions[i].name); - printf("Found function: %s\n", name); + /* printf("Found function: %s\n", name); */ if (!strcmp(name, "main")) fnmain = (qcint)i; } + printf("Entity field space: %i\n", (int)prog->entityfields); if (fnmain > 0) { - prog_exec(prog, &prog->functions[fnmain], VMXF_TRACE, VM_JUMPS_DEFAULT); + prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT); } else printf("No main function found\n");