7 MEM_VEC_FUNCTIONS(qc_program, prog_section_statement, code)
8 MEM_VEC_FUNCTIONS(qc_program, prog_section_def, defs)
9 MEM_VEC_FUNCTIONS(qc_program, prog_section_def, fields)
10 MEM_VEC_FUNCTIONS(qc_program, prog_section_function, functions)
11 MEM_VEC_FUNCTIONS(qc_program, char, strings)
12 MEM_VEC_FUN_APPEND(qc_program, char, strings)
13 MEM_VEC_FUN_RESIZE(qc_program, char, strings)
14 MEM_VEC_FUNCTIONS(qc_program, qcint, globals)
15 MEM_VEC_FUNCTIONS(qc_program, qcint, entitydata)
16 MEM_VEC_FUNCTIONS(qc_program, bool, entitypool)
18 MEM_VEC_FUNCTIONS(qc_program, qcint, localstack)
19 MEM_VEC_FUN_APPEND(qc_program, qcint, localstack)
20 MEM_VEC_FUN_RESIZE(qc_program, qcint, localstack)
21 MEM_VEC_FUNCTIONS(qc_program, qc_exec_stack, stack)
23 MEM_VEC_FUNCTIONS(qc_program, size_t, profile)
24 MEM_VEC_FUN_RESIZE(qc_program, size_t, profile)
26 MEM_VEC_FUNCTIONS(qc_program, prog_builtin, builtins)
28 static void loaderror(const char *fmt, ...)
35 printf(": %s\n", strerror(err));
38 static void printvmerr(const char *fmt, ...)
47 qc_program* prog_load(const char *filename)
54 file = fopen(filename, "rb");
58 if (fread(&header, sizeof(header), 1, file) != 1) {
59 loaderror("failed to read header from '%s'", filename);
64 if (header.version != 6) {
65 loaderror("header says this is a version %i progs, we need version 6\n", header.version);
70 prog = (qc_program*)mem_a(sizeof(qc_program));
73 printf("failed to allocate program data\n");
76 memset(prog, 0, sizeof(*prog));
78 prog->entityfields = header.entfield;
80 prog->filename = util_strdup(filename);
81 if (!prog->filename) {
82 loaderror("failed to store program name");
86 #define read_data(hdrvar, progvar, type) \
87 if (fseek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
88 loaderror("seek failed"); \
91 prog->progvar##_alloc = header.hdrvar.length; \
92 prog->progvar##_count = header.hdrvar.length; \
93 prog->progvar = (type*)mem_a(header.hdrvar.length * sizeof(*prog->progvar)); \
96 if (fread(prog->progvar, sizeof(*prog->progvar), header.hdrvar.length, file) \
97 != header.hdrvar.length) { \
98 loaderror("read failed"); \
101 #define read_data1(x, y) read_data(x, x, y)
103 read_data (statements, code, prog_section_statement);
104 read_data1(defs, prog_section_def);
105 read_data1(fields, prog_section_def);
106 read_data1(functions, prog_section_function);
107 read_data1(strings, char);
108 read_data1(globals, qcint);
112 /* profile counters */
113 if (!qc_program_profile_resize(prog, prog->code_count))
116 /* Add tempstring area */
117 prog->tempstring_start = prog->strings_count;
118 prog->tempstring_at = prog->strings_count;
119 if (!qc_program_strings_resize(prog, prog->strings_count + 16*1024))
122 /* spawn the world entity */
123 if (!qc_program_entitypool_add(prog, true)) {
124 loaderror("failed to allocate world entity\n");
127 for (i = 0; i < prog->entityfields; ++i) {
128 if (!qc_program_entitydata_add(prog, 0)) {
129 loaderror("failed to allocate world data\n");
137 if (prog->filename) mem_d(prog->filename);
138 if (prog->code) mem_d(prog->code);
139 if (prog->defs) mem_d(prog->defs);
140 if (prog->fields) mem_d(prog->fields);
141 if (prog->functions) mem_d(prog->functions);
142 if (prog->strings) mem_d(prog->strings);
143 if (prog->globals) mem_d(prog->globals);
144 if (prog->entitydata) mem_d(prog->entitydata);
145 if (prog->entitypool) mem_d(prog->entitypool);
150 void prog_delete(qc_program *prog)
152 if (prog->filename) mem_d(prog->filename);
153 MEM_VECTOR_CLEAR(prog, code);
154 MEM_VECTOR_CLEAR(prog, defs);
155 MEM_VECTOR_CLEAR(prog, fields);
156 MEM_VECTOR_CLEAR(prog, functions);
157 MEM_VECTOR_CLEAR(prog, strings);
158 MEM_VECTOR_CLEAR(prog, globals);
159 MEM_VECTOR_CLEAR(prog, entitydata);
160 MEM_VECTOR_CLEAR(prog, entitypool);
161 MEM_VECTOR_CLEAR(prog, localstack);
162 MEM_VECTOR_CLEAR(prog, stack);
163 MEM_VECTOR_CLEAR(prog, profile);
165 if (prog->builtins_alloc) {
166 MEM_VECTOR_CLEAR(prog, builtins);
168 /* otherwise the builtins were statically allocated */
172 /***********************************************************************
176 char* prog_getstring(qc_program *prog, qcint str)
178 if (str < 0 || str >= prog->strings_count)
179 return "<<<invalid string>>>";
180 return prog->strings + str;
183 prog_section_def* prog_entfield(qc_program *prog, qcint off)
186 for (i = 0; i < prog->fields_count; ++i) {
187 if (prog->fields[i].offset == off)
188 return (prog->fields + i);
193 prog_section_def* prog_getdef(qc_program *prog, qcint off)
196 for (i = 0; i < prog->defs_count; ++i) {
197 if (prog->defs[i].offset == off)
198 return (prog->defs + i);
203 qcany* prog_getedict(qc_program *prog, qcint e)
205 if (e >= prog->entitypool_count) {
207 printf("Accessing out of bounds edict %i\n", (int)e);
210 return (qcany*)(prog->entitydata + (prog->entityfields * e));
213 qcint prog_spawn_entity(qc_program *prog)
217 for (e = 0; e < (qcint)prog->entitypool_count; ++e) {
218 if (!prog->entitypool[e]) {
219 char *data = (char*)(prog->entitydata + (prog->entityfields * e));
220 memset(data, 0, prog->entityfields * sizeof(qcint));
224 if (!qc_program_entitypool_add(prog, true)) {
226 printf("Failed to allocate entity\n");
229 for (i = 0; i < prog->entityfields; ++i) {
230 if (!qc_program_entitydata_add(prog, 0)) {
231 printf("Failed to allocate entity\n");
238 void prog_free_entity(qc_program *prog, qcint e)
242 printf("Trying to free world entity\n");
245 if (e >= prog->entitypool_count) {
247 printf("Trying to free out of bounds entity\n");
250 if (!prog->entitypool[e]) {
252 printf("Double free on entity\n");
255 prog->entitypool[e] = false;
258 qcint prog_tempstring(qc_program *prog, const char *_str)
260 /* we don't access it, but the macro-generated functions don't use
263 char *str = (char*)_str;
265 size_t len = strlen(str);
266 size_t at = prog->tempstring_at;
268 /* when we reach the end we start over */
269 if (at + len >= prog->strings_count)
270 at = prog->tempstring_start;
272 /* when it doesn't fit, reallocate */
273 if (at + len >= prog->strings_count)
275 prog->strings_count = at;
276 if (!qc_program_strings_append(prog, str, len+1)) {
277 prog->vmerror = VMERR_TEMPSTRING_ALLOC;
283 /* when it fits, just copy */
284 memcpy(prog->strings + at, str, len+1);
285 prog->tempstring_at += len+1;
289 static int print_escaped_string(const char *str)
295 case '\a': len += 2; putchar('\\'); putchar('a'); break;
296 case '\b': len += 2; putchar('\\'); putchar('b'); break;
297 case '\r': len += 2; putchar('\\'); putchar('r'); break;
298 case '\n': len += 2; putchar('\\'); putchar('n'); break;
299 case '\t': len += 2; putchar('\\'); putchar('t'); break;
300 case '\f': len += 2; putchar('\\'); putchar('f'); break;
301 case '\v': len += 2; putchar('\\'); putchar('v'); break;
302 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
303 case '"': len += 2; putchar('\\'); putchar('"'); break;
315 static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
317 static char spaces[16+1] = " ";
318 prog_section_def *def;
325 def = prog_getdef(prog, glob);
326 value = (qcany*)(&prog->globals[glob]);
329 len = printf("[%s] ", prog_getstring(prog, def->name));
333 len = printf("[@%u] ", glob);
341 len += printf("%i,", value->_int);
344 len += printf("'%g %g %g',", value->vector[0],
349 len += print_escaped_string(prog_getstring(prog, value->string));
350 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
354 len += printf("%g,", value->_float);
360 spaces[16-len] = ' ';
364 static void prog_print_statement(qc_program *prog, prog_section_statement *st)
366 if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
367 printf("<illegal instruction %d>\n", st->opcode);
370 printf(" <> %-12s", asm_instr[st->opcode].m);
371 if (st->opcode >= INSTR_IF &&
372 st->opcode <= INSTR_IFNOT)
374 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
375 printf("%d\n", st->o2.s1);
377 else if (st->opcode >= INSTR_CALL0 &&
378 st->opcode <= INSTR_CALL8)
382 else if (st->opcode == INSTR_GOTO)
384 printf("%i\n", st->o1.s1);
388 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
392 t[1] = t[2] = TYPE_VECTOR;
395 t[0] = t[2] = TYPE_VECTOR;
398 t[0] = t[1] = TYPE_VECTOR;
404 t[0] = t[1] = t[2] = TYPE_VECTOR;
408 t[0] = t[1] = TYPE_STRING;
411 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
414 t[0] = t[1] = TYPE_STRING; t[2] = -1;
417 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
418 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
419 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
424 static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
430 st.localsp = prog->localstack_count;
431 st.stmt = prog->statement;
434 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
435 if (prog->stack_count)
437 prog_section_function *cur;
438 cur = prog->stack[prog->stack_count-1].function;
441 qcint *globals = prog->globals + cur->firstlocal;
442 if (!qc_program_localstack_append(prog, globals, cur->locals))
444 printf("out of memory\n");
451 qcint *globals = prog->globals + func->firstlocal;
452 if (!qc_program_localstack_append(prog, globals, func->locals))
454 printf("out of memory\n");
460 /* copy parameters */
461 parampos = func->firstlocal;
462 for (p = 0; p < func->nargs; ++p)
465 for (s = 0; s < func->argsize[p]; ++s) {
466 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
471 if (!qc_program_stack_add(prog, st)) {
472 printf("out of memory\n");
479 static qcint prog_leavefunction(qc_program *prog)
481 prog_section_function *prev = NULL;
484 qc_exec_stack st = prog->stack[prog->stack_count-1];
486 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
487 if (prog->stack_count > 1) {
488 prev = prog->stack[prog->stack_count-2].function;
489 oldsp = prog->stack[prog->stack_count-2].localsp;
492 prev = prog->stack[prog->stack_count-1].function;
493 oldsp = prog->stack[prog->stack_count-1].localsp;
496 qcint *globals = prog->globals + prev->firstlocal;
497 memcpy(globals, prog->localstack + oldsp, prev->locals);
498 if (!qc_program_localstack_resize(prog, oldsp)) {
499 printf("out of memory\n");
504 if (!qc_program_stack_remove(prog, prog->stack_count-1)) {
505 printf("out of memory\n");
512 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps)
515 prog_section_statement *st;
519 st = prog->code + prog_enterfunction(prog, func);
526 #define QCVM_PROFILE 0
528 # include "execloop.h"
533 #define QCVM_PROFILE 0
535 # include "execloop.h"
540 #define QCVM_PROFILE 1
542 # include "execloop.h"
545 case (VMXF_TRACE|VMXF_PROFILE):
547 #define QCVM_PROFILE 1
549 # include "execloop.h"
555 prog->localstack_count = 0;
556 prog->stack_count = 0;
562 /***********************************************************************
563 * main for when building the standalone executor
566 #if defined(QCVM_EXECUTOR)
567 bool opts_debug = false;
568 bool opts_memchk = false;
570 #define CheckArgs(num) do { \
571 if (prog->argc != (num)) { \
573 printf("ERROR: invalid number of arguments for %s: %i, expected %i\n", \
574 __FUNCTION__, prog->argc, (num)); \
579 #define GetGlobal(idx) ((qcany*)(prog->globals + (idx)))
580 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
581 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
583 static int qc_print(qc_program *prog)
585 qcany *str = (qcany*)(prog->globals + OFS_PARM0);
586 printf("%s", prog_getstring(prog, str->string));
590 static int qc_ftos(qc_program *prog)
597 snprintf(buffer, sizeof(buffer), "%g", num->_float);
598 str.string = prog_tempstring(prog, buffer);
603 static int qc_spawn(qc_program *prog)
607 ent.edict = prog_spawn_entity(prog);
609 return (ent.edict ? 0 : -1);
612 static int qc_kill(qc_program *prog)
617 prog_free_entity(prog, ent->edict);
621 static prog_builtin qc_builtins[] = {
628 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
630 int main(int argc, char **argv)
637 printf("usage: %s file\n", argv[0]);
641 prog = prog_load(argv[1]);
643 printf("failed to load program '%s'\n", argv[1]);
647 prog->builtins = qc_builtins;
648 prog->builtins_count = qc_builtins_count;
649 prog->builtins_alloc = 0;
651 for (i = 1; i < prog->functions_count; ++i) {
652 const char *name = prog_getstring(prog, prog->functions[i].name);
653 printf("Found function: %s\n", name);
654 if (!strcmp(name, "main"))
659 prog_exec(prog, &prog->functions[fnmain], VMXF_TRACE, VM_JUMPS_DEFAULT);
662 printf("No main function found\n");