2012-11-10 20:53:37 +00:00
|
|
|
/*
|
2013-01-01 05:08:55 +00:00
|
|
|
* Copyright (C) 2012, 2013
|
2012-11-10 20:53:37 +00:00
|
|
|
* Wolfgang Bumiller
|
|
|
|
* Dale Weiler
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
* so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef QCVM_LOOP
|
2012-06-27 13:34:26 +00:00
|
|
|
#include <errno.h>
|
2012-08-16 09:36:28 +00:00
|
|
|
#include <stdio.h>
|
2012-06-27 13:34:26 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2013-06-04 02:47:07 +00:00
|
|
|
#include <stdlib.h>
|
2012-06-27 13:34:26 +00:00
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
#include "gmqcc.h"
|
|
|
|
|
2013-08-16 09:03:36 +00:00
|
|
|
opts_cmd_t opts; /* command line options */
|
2012-06-27 13:34:26 +00:00
|
|
|
static void loaderror(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
int err = errno;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
2013-04-24 01:43:53 +00:00
|
|
|
printf(": %s\n", util_strerror(err));
|
2012-06-27 13:34:26 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static void qcvmerror(qc_program_t *prog, const char *fmt, ...)
|
2012-08-11 15:31:08 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
2012-08-16 10:10:24 +00:00
|
|
|
|
|
|
|
prog->vmerror++;
|
|
|
|
|
2012-08-11 15:31:08 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
qc_program_t* prog_load(const char *filename, bool skipversion)
|
2012-06-25 21:58:47 +00:00
|
|
|
{
|
2013-07-30 16:00:51 +00:00
|
|
|
qc_program_t *prog;
|
|
|
|
prog_header_t header;
|
2013-06-21 23:16:00 +00:00
|
|
|
FILE *file = fs_file_open(filename, "rb");
|
2012-06-25 21:58:47 +00:00
|
|
|
|
|
|
|
if (!file)
|
|
|
|
return NULL;
|
|
|
|
|
2013-02-08 12:06:59 +00:00
|
|
|
if (fs_file_read(&header, sizeof(header), 1, file) != 1) {
|
2012-06-27 13:34:26 +00:00
|
|
|
loaderror("failed to read header from '%s'", filename);
|
2013-02-08 12:06:59 +00:00
|
|
|
fs_file_close(file);
|
2012-06-25 21:58:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-17 16:36:06 +00:00
|
|
|
if (!skipversion && header.version != 6) {
|
2012-06-27 13:34:26 +00:00
|
|
|
loaderror("header says this is a version %i progs, we need version 6\n", header.version);
|
2013-02-08 12:06:59 +00:00
|
|
|
fs_file_close(file);
|
2012-06-25 21:58:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
prog = (qc_program_t*)mem_a(sizeof(qc_program_t));
|
2012-06-25 21:58:47 +00:00
|
|
|
if (!prog) {
|
2013-02-08 12:06:59 +00:00
|
|
|
fs_file_close(file);
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "failed to allocate program data\n");
|
2012-06-25 21:58:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memset(prog, 0, sizeof(*prog));
|
|
|
|
|
2012-06-27 11:21:37 +00:00
|
|
|
prog->entityfields = header.entfield;
|
2012-08-22 12:00:57 +00:00
|
|
|
prog->crc16 = header.crc16;
|
2012-06-27 11:21:37 +00:00
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
prog->filename = util_strdup(filename);
|
2012-06-27 13:34:26 +00:00
|
|
|
if (!prog->filename) {
|
|
|
|
loaderror("failed to store program name");
|
2012-06-25 21:58:47 +00:00
|
|
|
goto error;
|
2012-06-27 13:34:26 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 17:32:03 +00:00
|
|
|
#define read_data(hdrvar, progvar, reserved) \
|
2013-02-08 12:06:59 +00:00
|
|
|
if (fs_file_seek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
|
2012-11-15 17:32:03 +00:00
|
|
|
loaderror("seek failed"); \
|
|
|
|
goto error; \
|
|
|
|
} \
|
2013-02-08 12:06:59 +00:00
|
|
|
if (fs_file_read ( \
|
2012-12-23 06:05:22 +00:00
|
|
|
vec_add(prog->progvar, header.hdrvar.length + reserved), \
|
|
|
|
sizeof(*prog->progvar), \
|
|
|
|
header.hdrvar.length, \
|
|
|
|
file \
|
|
|
|
)!= header.hdrvar.length \
|
|
|
|
) { \
|
2012-11-15 17:32:03 +00:00
|
|
|
loaderror("read failed"); \
|
|
|
|
goto error; \
|
2012-06-25 21:58:47 +00:00
|
|
|
}
|
2012-11-15 17:32:03 +00:00
|
|
|
#define read_data1(x) read_data(x, x, 0)
|
|
|
|
#define read_data2(x, y) read_data(x, x, y)
|
2012-06-25 21:58:47 +00:00
|
|
|
|
2012-11-15 17:32:03 +00:00
|
|
|
read_data (statements, code, 0);
|
|
|
|
read_data1(defs);
|
|
|
|
read_data1(fields);
|
|
|
|
read_data1(functions);
|
|
|
|
read_data1(strings);
|
|
|
|
read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */
|
2012-06-25 21:58:47 +00:00
|
|
|
|
2013-02-08 12:06:59 +00:00
|
|
|
fs_file_close(file);
|
2012-06-25 21:58:47 +00:00
|
|
|
|
2012-06-27 11:21:37 +00:00
|
|
|
/* profile counters */
|
2012-11-15 17:32:03 +00:00
|
|
|
memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
|
2012-06-27 11:21:37 +00:00
|
|
|
|
2012-06-26 11:10:10 +00:00
|
|
|
/* Add tempstring area */
|
2012-11-15 17:32:03 +00:00
|
|
|
prog->tempstring_start = vec_size(prog->strings);
|
|
|
|
prog->tempstring_at = vec_size(prog->strings);
|
|
|
|
memset(vec_add(prog->strings, 16*1024), 0, 16*1024);
|
2012-06-26 11:10:10 +00:00
|
|
|
|
2012-08-11 15:31:08 +00:00
|
|
|
/* spawn the world entity */
|
2012-11-15 17:32:03 +00:00
|
|
|
vec_push(prog->entitypool, true);
|
|
|
|
memset(vec_add(prog->entitydata, prog->entityfields), 0, prog->entityfields * sizeof(prog->entitydata[0]));
|
2012-08-11 15:35:55 +00:00
|
|
|
prog->entities = 1;
|
2012-08-11 15:31:08 +00:00
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
return prog;
|
|
|
|
|
|
|
|
error:
|
2012-11-15 17:32:03 +00:00
|
|
|
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);
|
2012-06-25 21:58:47 +00:00
|
|
|
mem_d(prog);
|
2013-06-21 23:16:00 +00:00
|
|
|
|
|
|
|
fs_file_close(file);
|
2012-06-25 21:58:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
void prog_delete(qc_program_t *prog)
|
2012-06-25 21:58:47 +00:00
|
|
|
{
|
2012-06-26 11:10:10 +00:00
|
|
|
if (prog->filename) mem_d(prog->filename);
|
2012-11-15 17:32:03 +00:00
|
|
|
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);
|
|
|
|
vec_free(prog->stack);
|
|
|
|
vec_free(prog->profile);
|
2012-06-25 21:58:47 +00:00
|
|
|
mem_d(prog);
|
|
|
|
}
|
|
|
|
|
2012-06-26 11:10:10 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* VM code
|
|
|
|
*/
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
const char* prog_getstring(qc_program_t *prog, qcint_t str) {
|
2012-12-23 07:51:19 +00:00
|
|
|
/* cast for return required for C++ */
|
2013-07-30 16:00:51 +00:00
|
|
|
if (str < 0 || str >= (qcint_t)vec_size(prog->strings))
|
2013-05-29 02:16:50 +00:00
|
|
|
return "<<<invalid string>>>";
|
2013-06-14 21:36:16 +00:00
|
|
|
|
2012-06-26 11:10:10 +00:00
|
|
|
return prog->strings + str;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
prog_section_def_t* prog_entfield(qc_program_t *prog, qcint_t off) {
|
2012-06-27 11:21:37 +00:00
|
|
|
size_t i;
|
2012-11-15 17:32:03 +00:00
|
|
|
for (i = 0; i < vec_size(prog->fields); ++i) {
|
2012-06-27 11:21:37 +00:00
|
|
|
if (prog->fields[i].offset == off)
|
|
|
|
return (prog->fields + i);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
prog_section_def_t* prog_getdef(qc_program_t *prog, qcint_t off)
|
2012-06-27 11:21:37 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
2012-11-15 17:32:03 +00:00
|
|
|
for (i = 0; i < vec_size(prog->defs); ++i) {
|
2012-06-27 11:21:37 +00:00
|
|
|
if (prog->defs[i].offset == off)
|
|
|
|
return (prog->defs + i);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
qcany_t* prog_getedict(qc_program_t *prog, qcint_t e) {
|
|
|
|
if (e >= (qcint_t)vec_size(prog->entitypool)) {
|
2012-08-11 15:31:08 +00:00
|
|
|
prog->vmerror++;
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
|
2012-08-11 15:31:08 +00:00
|
|
|
e = 0;
|
|
|
|
}
|
2013-07-30 16:00:51 +00:00
|
|
|
return (qcany_t*)(prog->entitydata + (prog->entityfields * e));
|
2012-08-11 15:31:08 +00:00
|
|
|
}
|
|
|
|
|
2013-08-16 03:28:02 +00:00
|
|
|
static qcint_t prog_spawn_entity(qc_program_t *prog) {
|
2012-08-22 13:35:28 +00:00
|
|
|
char *data;
|
2013-07-30 16:00:51 +00:00
|
|
|
qcint_t e;
|
|
|
|
for (e = 0; e < (qcint_t)vec_size(prog->entitypool); ++e) {
|
2012-08-11 15:31:08 +00:00
|
|
|
if (!prog->entitypool[e]) {
|
2012-08-22 13:35:28 +00:00
|
|
|
data = (char*)(prog->entitydata + (prog->entityfields * e));
|
2013-07-30 16:00:51 +00:00
|
|
|
memset(data, 0, prog->entityfields * sizeof(qcint_t));
|
2012-08-11 15:31:08 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
2012-11-15 17:32:03 +00:00
|
|
|
vec_push(prog->entitypool, true);
|
2012-08-11 15:35:55 +00:00
|
|
|
prog->entities++;
|
2012-11-15 17:32:03 +00:00
|
|
|
data = (char*)vec_add(prog->entitydata, prog->entityfields);
|
2013-07-30 16:00:51 +00:00
|
|
|
memset(data, 0, prog->entityfields * sizeof(qcint_t));
|
2012-08-11 15:31:08 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2013-08-16 03:28:02 +00:00
|
|
|
static void prog_free_entity(qc_program_t *prog, qcint_t e) {
|
2012-08-11 15:31:08 +00:00
|
|
|
if (!e) {
|
|
|
|
prog->vmerror++;
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "Trying to free world entity\n");
|
2012-08-11 15:31:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-07-30 16:00:51 +00:00
|
|
|
if (e >= (qcint_t)vec_size(prog->entitypool)) {
|
2012-08-11 15:31:08 +00:00
|
|
|
prog->vmerror++;
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "Trying to free out of bounds entity\n");
|
2012-08-11 15:31:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!prog->entitypool[e]) {
|
|
|
|
prog->vmerror++;
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "Double free on entity\n");
|
2012-08-11 15:31:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
prog->entitypool[e] = false;
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
qcint_t prog_tempstring(qc_program_t *prog, const char *str) {
|
2012-06-26 11:10:10 +00:00
|
|
|
size_t len = strlen(str);
|
|
|
|
size_t at = prog->tempstring_at;
|
|
|
|
|
|
|
|
/* when we reach the end we start over */
|
2012-11-15 17:32:03 +00:00
|
|
|
if (at + len >= vec_size(prog->strings))
|
2012-06-26 11:10:10 +00:00
|
|
|
at = prog->tempstring_start;
|
|
|
|
|
|
|
|
/* when it doesn't fit, reallocate */
|
2012-11-15 17:32:03 +00:00
|
|
|
if (at + len >= vec_size(prog->strings))
|
2012-06-26 11:10:10 +00:00
|
|
|
{
|
2012-11-15 17:32:03 +00:00
|
|
|
(void)vec_add(prog->strings, len+1);
|
|
|
|
memcpy(prog->strings + at, str, len+1);
|
2012-06-26 11:10:10 +00:00
|
|
|
return at;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* when it fits, just copy */
|
|
|
|
memcpy(prog->strings + at, str, len+1);
|
|
|
|
prog->tempstring_at += len+1;
|
|
|
|
return at;
|
|
|
|
}
|
|
|
|
|
2013-05-29 02:16:50 +00:00
|
|
|
static size_t print_escaped_string(const char *str, size_t maxlen) {
|
2012-11-23 06:23:20 +00:00
|
|
|
size_t len = 2;
|
2012-08-11 09:31:50 +00:00
|
|
|
putchar('"');
|
2012-08-22 15:41:24 +00:00
|
|
|
--maxlen; /* because we're lazy and have escape sequences */
|
2012-08-11 09:31:50 +00:00
|
|
|
while (*str) {
|
2012-08-22 15:41:24 +00:00
|
|
|
if (len >= maxlen) {
|
|
|
|
putchar('.');
|
|
|
|
putchar('.');
|
|
|
|
putchar('.');
|
|
|
|
len += 3;
|
|
|
|
break;
|
|
|
|
}
|
2012-08-11 09:31:50 +00:00
|
|
|
switch (*str) {
|
|
|
|
case '\a': len += 2; putchar('\\'); putchar('a'); break;
|
|
|
|
case '\b': len += 2; putchar('\\'); putchar('b'); break;
|
|
|
|
case '\r': len += 2; putchar('\\'); putchar('r'); break;
|
|
|
|
case '\n': len += 2; putchar('\\'); putchar('n'); break;
|
|
|
|
case '\t': len += 2; putchar('\\'); putchar('t'); break;
|
|
|
|
case '\f': len += 2; putchar('\\'); putchar('f'); break;
|
|
|
|
case '\v': len += 2; putchar('\\'); putchar('v'); break;
|
|
|
|
case '\\': len += 2; putchar('\\'); putchar('\\'); break;
|
|
|
|
case '"': len += 2; putchar('\\'); putchar('"'); break;
|
|
|
|
default:
|
|
|
|
++len;
|
|
|
|
putchar(*str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
putchar('"');
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static void trace_print_global(qc_program_t *prog, unsigned int glob, int vtype) {
|
2012-08-22 15:41:24 +00:00
|
|
|
static char spaces[28+1] = " ";
|
2013-07-30 16:00:51 +00:00
|
|
|
prog_section_def_t *def;
|
|
|
|
qcany_t *value;
|
2012-06-27 11:21:37 +00:00
|
|
|
int len;
|
|
|
|
|
2012-08-19 14:42:12 +00:00
|
|
|
if (!glob) {
|
2013-06-22 00:15:25 +00:00
|
|
|
if ((len = printf("<null>,")) == -1)
|
|
|
|
len = 0;
|
|
|
|
|
2012-08-19 14:42:12 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2012-06-27 11:21:37 +00:00
|
|
|
|
|
|
|
def = prog_getdef(prog, glob);
|
2013-07-30 16:00:51 +00:00
|
|
|
value = (qcany_t*)(&prog->globals[glob]);
|
2012-06-27 11:21:37 +00:00
|
|
|
|
2012-11-18 19:32:22 +00:00
|
|
|
len = printf("[@%u] ", glob);
|
2012-06-27 11:21:37 +00:00
|
|
|
if (def) {
|
2012-08-22 15:32:57 +00:00
|
|
|
const char *name = prog_getstring(prog, def->name);
|
|
|
|
if (name[0] == '#')
|
2012-11-18 19:32:22 +00:00
|
|
|
len += printf("$");
|
2012-08-22 15:32:57 +00:00
|
|
|
else
|
2012-11-18 19:32:22 +00:00
|
|
|
len += printf("%s ", name);
|
2012-11-11 22:39:40 +00:00
|
|
|
vtype = def->type & DEF_TYPEMASK;
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (vtype) {
|
|
|
|
case TYPE_VOID:
|
|
|
|
case TYPE_ENTITY:
|
|
|
|
case TYPE_FIELD:
|
|
|
|
case TYPE_FUNCTION:
|
|
|
|
case TYPE_POINTER:
|
2012-08-22 15:32:57 +00:00
|
|
|
len += printf("(%i),", value->_int);
|
2012-06-27 11:21:37 +00:00
|
|
|
break;
|
|
|
|
case TYPE_VECTOR:
|
|
|
|
len += printf("'%g %g %g',", value->vector[0],
|
|
|
|
value->vector[1],
|
|
|
|
value->vector[2]);
|
|
|
|
break;
|
|
|
|
case TYPE_STRING:
|
2012-12-23 16:32:14 +00:00
|
|
|
if (value->string)
|
|
|
|
len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
|
|
|
|
else
|
|
|
|
len += printf("(null)");
|
2012-08-19 14:42:12 +00:00
|
|
|
len += printf(",");
|
2012-08-11 09:31:50 +00:00
|
|
|
/* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
|
2012-06-27 11:21:37 +00:00
|
|
|
break;
|
|
|
|
case TYPE_FLOAT:
|
|
|
|
default:
|
|
|
|
len += printf("%g,", value->_float);
|
|
|
|
break;
|
|
|
|
}
|
2012-08-19 14:42:12 +00:00
|
|
|
done:
|
2012-11-23 06:23:20 +00:00
|
|
|
if (len < (int)sizeof(spaces)-1) {
|
2012-08-19 14:42:12 +00:00
|
|
|
spaces[sizeof(spaces)-1-len] = 0;
|
2013-02-08 12:06:59 +00:00
|
|
|
fs_file_puts(stdout, spaces);
|
2012-08-19 14:42:12 +00:00
|
|
|
spaces[sizeof(spaces)-1-len] = ' ';
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static void prog_print_statement(qc_program_t *prog, prog_section_statement_t *st) {
|
2013-07-28 00:23:15 +00:00
|
|
|
if (st->opcode >= VINSTR_END) {
|
2012-06-27 11:21:37 +00:00
|
|
|
printf("<illegal instruction %d>\n", st->opcode);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-15 17:32:03 +00:00
|
|
|
if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
|
2012-11-11 22:57:42 +00:00
|
|
|
size_t i;
|
2012-11-15 17:32:03 +00:00
|
|
|
for (i = 0; i < vec_size(prog->function_stack); ++i)
|
2012-11-11 22:57:42 +00:00
|
|
|
printf("->");
|
2012-11-15 17:32:03 +00:00
|
|
|
printf("%s:", vec_last(prog->function_stack));
|
2012-11-11 22:57:42 +00:00
|
|
|
}
|
2013-07-28 00:23:15 +00:00
|
|
|
printf(" <> %-12s", util_instr_str[st->opcode]);
|
2012-06-27 11:21:37 +00:00
|
|
|
if (st->opcode >= INSTR_IF &&
|
|
|
|
st->opcode <= INSTR_IFNOT)
|
|
|
|
{
|
|
|
|
trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
|
|
|
|
printf("%d\n", st->o2.s1);
|
|
|
|
}
|
|
|
|
else if (st->opcode >= INSTR_CALL0 &&
|
|
|
|
st->opcode <= INSTR_CALL8)
|
|
|
|
{
|
2012-11-11 22:36:03 +00:00
|
|
|
trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
|
2012-06-27 11:21:37 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
else if (st->opcode == INSTR_GOTO)
|
|
|
|
{
|
|
|
|
printf("%i\n", st->o1.s1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
|
|
|
|
switch (st->opcode)
|
|
|
|
{
|
|
|
|
case INSTR_MUL_FV:
|
|
|
|
t[1] = t[2] = TYPE_VECTOR;
|
|
|
|
break;
|
|
|
|
case INSTR_MUL_VF:
|
|
|
|
t[0] = t[2] = TYPE_VECTOR;
|
|
|
|
break;
|
|
|
|
case INSTR_MUL_V:
|
|
|
|
t[0] = t[1] = TYPE_VECTOR;
|
|
|
|
break;
|
|
|
|
case INSTR_ADD_V:
|
|
|
|
case INSTR_SUB_V:
|
|
|
|
case INSTR_EQ_V:
|
|
|
|
case INSTR_NE_V:
|
|
|
|
t[0] = t[1] = t[2] = TYPE_VECTOR;
|
|
|
|
break;
|
|
|
|
case INSTR_EQ_S:
|
|
|
|
case INSTR_NE_S:
|
|
|
|
t[0] = t[1] = TYPE_STRING;
|
|
|
|
break;
|
2012-08-19 14:42:12 +00:00
|
|
|
case INSTR_STORE_F:
|
|
|
|
case INSTR_STOREP_F:
|
|
|
|
t[2] = -1;
|
|
|
|
break;
|
2012-06-27 11:21:37 +00:00
|
|
|
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;
|
2012-08-19 14:42:12 +00:00
|
|
|
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;
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
|
2012-08-19 14:42:12 +00:00
|
|
|
else printf("(none), ");
|
2012-06-27 11:21:37 +00:00
|
|
|
if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
|
2012-08-19 14:42:12 +00:00
|
|
|
else printf("(none), ");
|
2012-06-27 11:21:37 +00:00
|
|
|
if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
|
2012-08-19 14:42:12 +00:00
|
|
|
else printf("(none)");
|
2012-06-27 11:21:37 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static qcint_t prog_enterfunction(qc_program_t *prog, prog_section_function_t *func) {
|
|
|
|
qc_exec_stack_t st;
|
2012-11-30 19:20:13 +00:00
|
|
|
size_t parampos;
|
|
|
|
int32_t p;
|
2012-06-27 11:21:37 +00:00
|
|
|
|
|
|
|
/* back up locals */
|
2012-11-15 17:32:03 +00:00
|
|
|
st.localsp = vec_size(prog->localstack);
|
2012-06-27 11:21:37 +00:00
|
|
|
st.stmt = prog->statement;
|
|
|
|
st.function = func;
|
|
|
|
|
2012-11-11 22:57:42 +00:00
|
|
|
if (prog->xflags & VMXF_TRACE) {
|
2012-12-30 11:01:45 +00:00
|
|
|
const char *str = prog_getstring(prog, func->name);
|
|
|
|
vec_push(prog->function_stack, str);
|
2012-11-11 22:57:42 +00:00
|
|
|
}
|
|
|
|
|
2012-06-29 14:04:24 +00:00
|
|
|
#ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
|
2012-11-15 17:32:03 +00:00
|
|
|
if (vec_size(prog->stack))
|
2012-06-29 14:04:24 +00:00
|
|
|
{
|
2013-07-30 16:00:51 +00:00
|
|
|
prog_section_function_t *cur;
|
2012-11-15 17:32:03 +00:00
|
|
|
cur = prog->stack[vec_size(prog->stack)-1].function;
|
2012-06-29 14:04:24 +00:00
|
|
|
if (cur)
|
|
|
|
{
|
2013-07-30 16:00:51 +00:00
|
|
|
qcint_t *globals = prog->globals + cur->firstlocal;
|
2012-11-15 17:32:03 +00:00
|
|
|
vec_append(prog->localstack, cur->locals, globals);
|
2012-06-29 14:04:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2012-06-27 11:21:37 +00:00
|
|
|
{
|
2013-07-30 16:00:51 +00:00
|
|
|
qcint_t *globals = prog->globals + func->firstlocal;
|
2012-11-15 17:32:03 +00:00
|
|
|
vec_append(prog->localstack, func->locals, globals);
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
2012-06-29 14:04:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* copy parameters */
|
|
|
|
parampos = func->firstlocal;
|
|
|
|
for (p = 0; p < func->nargs; ++p)
|
|
|
|
{
|
|
|
|
size_t s;
|
|
|
|
for (s = 0; s < func->argsize[p]; ++s) {
|
|
|
|
prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
|
|
|
|
++parampos;
|
|
|
|
}
|
|
|
|
}
|
2012-06-27 11:21:37 +00:00
|
|
|
|
2012-11-15 17:32:03 +00:00
|
|
|
vec_push(prog->stack, st);
|
2012-06-27 11:21:37 +00:00
|
|
|
|
|
|
|
return func->entry;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static qcint_t prog_leavefunction(qc_program_t *prog) {
|
|
|
|
prog_section_function_t *prev = NULL;
|
2012-06-29 14:04:24 +00:00
|
|
|
size_t oldsp;
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
qc_exec_stack_t st = vec_last(prog->stack);
|
2012-06-27 11:21:37 +00:00
|
|
|
|
2012-11-11 22:57:42 +00:00
|
|
|
if (prog->xflags & VMXF_TRACE) {
|
2012-11-15 17:32:03 +00:00
|
|
|
if (vec_size(prog->function_stack))
|
|
|
|
vec_pop(prog->function_stack);
|
2012-11-11 22:57:42 +00:00
|
|
|
}
|
|
|
|
|
2012-06-29 14:04:24 +00:00
|
|
|
#ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
|
2012-11-15 17:32:03 +00:00
|
|
|
if (vec_size(prog->stack) > 1) {
|
|
|
|
prev = prog->stack[vec_size(prog->stack)-2].function;
|
|
|
|
oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
|
2012-06-29 14:04:24 +00:00
|
|
|
}
|
|
|
|
#else
|
2012-11-15 17:32:03 +00:00
|
|
|
prev = prog->stack[vec_size(prog->stack)-1].function;
|
|
|
|
oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
|
2012-06-29 14:04:24 +00:00
|
|
|
#endif
|
|
|
|
if (prev) {
|
2013-07-30 16:00:51 +00:00
|
|
|
qcint_t *globals = prog->globals + prev->firstlocal;
|
2012-12-23 16:32:14 +00:00
|
|
|
memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
|
2012-11-15 17:32:03 +00:00
|
|
|
/* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
|
|
|
|
vec_shrinkto(prog->localstack, oldsp);
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 17:32:03 +00:00
|
|
|
vec_pop(prog->stack);
|
2012-06-29 14:04:24 +00:00
|
|
|
|
2012-08-16 14:28:59 +00:00
|
|
|
return st.stmt - 1; /* offset the ++st */
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
bool prog_exec(qc_program_t *prog, prog_section_function_t *func, size_t flags, long maxjumps) {
|
2012-06-27 11:21:37 +00:00
|
|
|
long jumpcount = 0;
|
2012-08-22 15:37:22 +00:00
|
|
|
size_t oldxflags = prog->xflags;
|
2013-07-30 16:00:51 +00:00
|
|
|
prog_section_statement_t *st;
|
2012-06-27 11:21:37 +00:00
|
|
|
|
2012-08-11 15:31:08 +00:00
|
|
|
prog->vmerror = 0;
|
2012-08-22 15:37:22 +00:00
|
|
|
prog->xflags = flags;
|
2012-08-11 15:31:08 +00:00
|
|
|
|
2012-06-27 11:21:37 +00:00
|
|
|
st = prog->code + prog_enterfunction(prog, func);
|
|
|
|
--st;
|
|
|
|
switch (flags)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case 0:
|
|
|
|
{
|
2012-11-10 20:53:37 +00:00
|
|
|
#define QCVM_LOOP 1
|
2012-06-27 11:21:37 +00:00
|
|
|
#define QCVM_PROFILE 0
|
|
|
|
#define QCVM_TRACE 0
|
2012-11-10 20:53:37 +00:00
|
|
|
# include __FILE__
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
case (VMXF_TRACE):
|
|
|
|
{
|
|
|
|
#define QCVM_PROFILE 0
|
|
|
|
#define QCVM_TRACE 1
|
2012-11-10 20:53:37 +00:00
|
|
|
# include __FILE__
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
case (VMXF_PROFILE):
|
|
|
|
{
|
|
|
|
#define QCVM_PROFILE 1
|
|
|
|
#define QCVM_TRACE 0
|
2012-11-10 20:53:37 +00:00
|
|
|
# include __FILE__
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
case (VMXF_TRACE|VMXF_PROFILE):
|
|
|
|
{
|
|
|
|
#define QCVM_PROFILE 1
|
|
|
|
#define QCVM_TRACE 1
|
2012-11-10 20:53:37 +00:00
|
|
|
# include __FILE__
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
cleanup:
|
2012-08-22 15:37:22 +00:00
|
|
|
prog->xflags = oldxflags;
|
2012-11-15 17:32:03 +00:00
|
|
|
vec_free(prog->localstack);
|
|
|
|
vec_free(prog->stack);
|
2012-08-11 15:31:08 +00:00
|
|
|
if (prog->vmerror)
|
|
|
|
return false;
|
2012-06-27 11:21:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-26 11:10:10 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* main for when building the standalone executor
|
|
|
|
*/
|
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
#if defined(QCVM_EXECUTOR)
|
2012-08-22 13:39:15 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2012-08-22 12:00:57 +00:00
|
|
|
const char *type_name[TYPE_COUNT] = {
|
|
|
|
"void",
|
|
|
|
"string",
|
|
|
|
"float",
|
|
|
|
"vector",
|
|
|
|
"entity",
|
|
|
|
"field",
|
|
|
|
"function",
|
|
|
|
"pointer",
|
|
|
|
"integer",
|
2013-01-11 22:15:16 +00:00
|
|
|
|
|
|
|
"variant",
|
|
|
|
|
|
|
|
"struct",
|
|
|
|
"union",
|
|
|
|
"array",
|
|
|
|
|
|
|
|
"nil",
|
|
|
|
"noexpr"
|
2012-08-22 12:00:57 +00:00
|
|
|
};
|
|
|
|
|
2012-08-23 08:35:03 +00:00
|
|
|
typedef struct {
|
|
|
|
int vtype;
|
|
|
|
const char *value;
|
|
|
|
} qcvm_parameter;
|
|
|
|
|
2013-04-25 03:34:42 +00:00
|
|
|
static qcvm_parameter *main_params = NULL;
|
2012-08-23 08:35:03 +00:00
|
|
|
|
2012-08-11 15:31:08 +00:00
|
|
|
#define CheckArgs(num) do { \
|
|
|
|
if (prog->argc != (num)) { \
|
|
|
|
prog->vmerror++; \
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "ERROR: invalid number of arguments for %s: %i, expected %i\n", \
|
2012-08-11 15:31:08 +00:00
|
|
|
__FUNCTION__, prog->argc, (num)); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
#define GetGlobal(idx) ((qcany_t*)(prog->globals + (idx)))
|
2012-08-11 15:31:08 +00:00
|
|
|
#define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
|
|
|
|
#define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_print(qc_program_t *prog) {
|
2012-08-11 19:18:15 +00:00
|
|
|
size_t i;
|
2012-08-22 15:37:22 +00:00
|
|
|
const char *laststr = NULL;
|
2012-11-23 06:23:20 +00:00
|
|
|
for (i = 0; i < (size_t)prog->argc; ++i) {
|
2013-07-30 16:00:51 +00:00
|
|
|
qcany_t *str = (qcany_t*)(prog->globals + OFS_PARM0 + 3*i);
|
2012-12-22 08:07:54 +00:00
|
|
|
laststr = prog_getstring(prog, str->string);
|
|
|
|
printf("%s", laststr);
|
2012-08-22 15:37:22 +00:00
|
|
|
}
|
|
|
|
if (laststr && (prog->xflags & VMXF_TRACE)) {
|
|
|
|
size_t len = strlen(laststr);
|
|
|
|
if (!len || laststr[len-1] != '\n')
|
|
|
|
printf("\n");
|
2012-08-11 19:18:15 +00:00
|
|
|
}
|
2012-07-27 17:27:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_error(qc_program_t *prog) {
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "*** VM raised an error:\n");
|
2012-08-22 13:35:28 +00:00
|
|
|
qc_print(prog);
|
|
|
|
prog->vmerror++;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_ftos(qc_program_t *prog) {
|
2012-08-11 15:31:08 +00:00
|
|
|
char buffer[512];
|
2013-07-30 16:00:51 +00:00
|
|
|
qcany_t *num;
|
|
|
|
qcany_t str;
|
2012-08-11 15:31:08 +00:00
|
|
|
CheckArgs(1);
|
|
|
|
num = GetArg(0);
|
2013-04-24 01:43:53 +00:00
|
|
|
util_snprintf(buffer, sizeof(buffer), "%g", num->_float);
|
2012-08-11 15:31:08 +00:00
|
|
|
str.string = prog_tempstring(prog, buffer);
|
|
|
|
Return(str);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_stof(qc_program_t *prog) {
|
|
|
|
qcany_t *str;
|
|
|
|
qcany_t num;
|
2012-12-01 14:54:13 +00:00
|
|
|
CheckArgs(1);
|
|
|
|
str = GetArg(0);
|
2013-04-24 01:43:53 +00:00
|
|
|
num._float = (float)strtod(prog_getstring(prog, str->string), NULL);
|
2012-12-01 14:54:13 +00:00
|
|
|
Return(num);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_vtos(qc_program_t *prog) {
|
2012-08-22 13:03:49 +00:00
|
|
|
char buffer[512];
|
2013-07-30 16:00:51 +00:00
|
|
|
qcany_t *num;
|
|
|
|
qcany_t str;
|
2012-08-22 13:03:49 +00:00
|
|
|
CheckArgs(1);
|
|
|
|
num = GetArg(0);
|
2013-04-24 01:43:53 +00:00
|
|
|
util_snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
|
2012-08-22 13:03:49 +00:00
|
|
|
str.string = prog_tempstring(prog, buffer);
|
|
|
|
Return(str);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_etos(qc_program_t *prog) {
|
2012-08-24 20:58:07 +00:00
|
|
|
char buffer[512];
|
2013-07-30 16:00:51 +00:00
|
|
|
qcany_t *num;
|
|
|
|
qcany_t str;
|
2012-08-24 20:58:07 +00:00
|
|
|
CheckArgs(1);
|
|
|
|
num = GetArg(0);
|
2013-04-24 01:43:53 +00:00
|
|
|
util_snprintf(buffer, sizeof(buffer), "%i", num->_int);
|
2012-08-24 20:58:07 +00:00
|
|
|
str.string = prog_tempstring(prog, buffer);
|
|
|
|
Return(str);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_spawn(qc_program_t *prog) {
|
|
|
|
qcany_t ent;
|
2012-08-11 15:31:08 +00:00
|
|
|
CheckArgs(0);
|
|
|
|
ent.edict = prog_spawn_entity(prog);
|
|
|
|
Return(ent);
|
|
|
|
return (ent.edict ? 0 : -1);
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_kill(qc_program_t *prog) {
|
|
|
|
qcany_t *ent;
|
2012-08-11 15:31:08 +00:00
|
|
|
CheckArgs(1);
|
|
|
|
ent = GetArg(0);
|
|
|
|
prog_free_entity(prog, ent->edict);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_sqrt(qc_program_t *prog) {
|
|
|
|
qcany_t *num, out;
|
2013-01-08 18:21:24 +00:00
|
|
|
CheckArgs(1);
|
|
|
|
num = GetArg(0);
|
|
|
|
out._float = sqrt(num->_float);
|
|
|
|
Return(out);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_vlen(qc_program_t *prog) {
|
|
|
|
qcany_t *vec, len;
|
2012-08-22 13:39:15 +00:00
|
|
|
CheckArgs(1);
|
|
|
|
vec = GetArg(0);
|
2012-11-26 11:12:06 +00:00
|
|
|
len._float = sqrt(vec->vector[0] * vec->vector[0] +
|
2012-08-22 13:39:15 +00:00
|
|
|
vec->vector[1] * vec->vector[1] +
|
|
|
|
vec->vector[2] * vec->vector[2]);
|
|
|
|
Return(len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_normalize(qc_program_t *prog) {
|
2013-01-08 18:21:24 +00:00
|
|
|
double len;
|
2013-07-30 16:00:51 +00:00
|
|
|
qcany_t *vec;
|
|
|
|
qcany_t out;
|
2013-01-08 18:21:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_strcat(qc_program_t *prog) {
|
2012-12-23 18:05:39 +00:00
|
|
|
char *buffer;
|
|
|
|
size_t len1, len2;
|
2013-07-30 16:00:51 +00:00
|
|
|
qcany_t *str1, *str2;
|
|
|
|
qcany_t out;
|
2013-06-14 21:36:16 +00:00
|
|
|
|
2013-05-29 02:16:50 +00:00
|
|
|
const char *cstr1;
|
|
|
|
const char *cstr2;
|
2012-12-23 18:05:39 +00:00
|
|
|
|
|
|
|
CheckArgs(2);
|
|
|
|
str1 = GetArg(0);
|
|
|
|
str2 = GetArg(1);
|
|
|
|
cstr1 = prog_getstring(prog, str1->string);
|
|
|
|
cstr2 = prog_getstring(prog, str2->string);
|
|
|
|
len1 = strlen(cstr1);
|
|
|
|
len2 = strlen(cstr2);
|
|
|
|
buffer = (char*)mem_a(len1 + len2 + 1);
|
|
|
|
memcpy(buffer, cstr1, len1);
|
|
|
|
memcpy(buffer+len1, cstr2, len2+1);
|
|
|
|
out.string = prog_tempstring(prog, buffer);
|
|
|
|
mem_d(buffer);
|
|
|
|
Return(out);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_strcmp(qc_program_t *prog) {
|
|
|
|
qcany_t *str1, *str2;
|
|
|
|
qcany_t out;
|
2012-12-23 18:22:38 +00:00
|
|
|
|
2013-05-29 02:16:50 +00:00
|
|
|
const char *cstr1;
|
|
|
|
const char *cstr2;
|
2013-06-14 21:36:16 +00:00
|
|
|
|
2012-12-23 18:22:38 +00:00
|
|
|
if (prog->argc != 2 && prog->argc != 3) {
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
|
2012-12-23 18:22:38 +00:00
|
|
|
prog->argc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
str1 = GetArg(0);
|
|
|
|
str2 = GetArg(1);
|
|
|
|
cstr1 = prog_getstring(prog, str1->string);
|
|
|
|
cstr2 = prog_getstring(prog, str2->string);
|
|
|
|
if (prog->argc == 3)
|
|
|
|
out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
|
|
|
|
else
|
|
|
|
out._float = strcmp(cstr1, cstr2);
|
|
|
|
Return(out);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static int qc_floor(qc_program_t *prog) {
|
|
|
|
qcany_t *num, out;
|
2013-03-08 09:17:54 +00:00
|
|
|
CheckArgs(1);
|
|
|
|
num = GetArg(0);
|
|
|
|
out._float = floor(num->_float);
|
|
|
|
Return(out);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static prog_builtin_t qc_builtins[] = {
|
2012-07-27 17:27:56 +00:00
|
|
|
NULL,
|
2013-01-08 18:21:24 +00:00
|
|
|
&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 */
|
2013-03-08 09:17:54 +00:00
|
|
|
&qc_sqrt, /* 13 */
|
|
|
|
&qc_floor /* 14 */
|
2012-07-27 17:27:56 +00:00
|
|
|
};
|
|
|
|
|
2012-08-19 13:19:43 +00:00
|
|
|
static const char *arg0 = NULL;
|
|
|
|
|
2013-06-06 02:51:13 +00:00
|
|
|
static void version(void) {
|
2012-12-18 14:46:26 +00:00
|
|
|
printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
|
|
|
|
GMQCC_VERSION_MAJOR,
|
|
|
|
GMQCC_VERSION_MINOR,
|
|
|
|
GMQCC_VERSION_PATCH,
|
|
|
|
__DATE__,
|
|
|
|
__TIME__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-06 02:51:13 +00:00
|
|
|
static void usage(void) {
|
2012-12-18 14:39:20 +00:00
|
|
|
printf("usage: %s [options] [parameters] file\n", arg0);
|
|
|
|
printf("options:\n");
|
2012-12-23 19:45:43 +00:00
|
|
|
printf(" -h, --help print this message\n"
|
|
|
|
" -trace trace the execution\n"
|
|
|
|
" -profile perform profiling during execution\n"
|
|
|
|
" -info print information from the prog's header\n"
|
|
|
|
" -disasm disassemble and exit\n"
|
|
|
|
" -disasm-func func disassemble and exit\n"
|
|
|
|
" -printdefs list the defs section\n"
|
|
|
|
" -printfields list the field section\n"
|
2012-12-26 09:23:45 +00:00
|
|
|
" -printfuns list functions information\n"
|
|
|
|
" -v be verbose\n"
|
|
|
|
" -vv be even more verbose\n");
|
2012-12-18 14:39:20 +00:00
|
|
|
printf("parameters:\n");
|
|
|
|
printf(" -vector <V> pass a vector parameter to main()\n"
|
|
|
|
" -float <f> pass a float parameter to main()\n"
|
|
|
|
" -string <s> pass a string parameter to main() \n");
|
2012-08-19 13:19:43 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
static void prog_main_setparams(qc_program_t *prog) {
|
2012-08-23 08:35:03 +00:00
|
|
|
size_t i;
|
2013-07-30 16:00:51 +00:00
|
|
|
qcany_t *arg;
|
2012-08-23 08:35:03 +00:00
|
|
|
|
2012-11-15 17:32:03 +00:00
|
|
|
for (i = 0; i < vec_size(main_params); ++i) {
|
2012-08-23 08:35:03 +00:00
|
|
|
arg = GetGlobal(OFS_PARM0 + 3*i);
|
|
|
|
arg->vector[0] = 0;
|
|
|
|
arg->vector[1] = 0;
|
|
|
|
arg->vector[2] = 0;
|
2012-11-15 17:32:03 +00:00
|
|
|
switch (main_params[i].vtype) {
|
2012-08-23 08:35:03 +00:00
|
|
|
case TYPE_VECTOR:
|
2012-12-20 14:46:31 +00:00
|
|
|
#ifdef _MSC_VER
|
2012-11-15 17:32:03 +00:00
|
|
|
(void)sscanf_s(main_params[i].value, " %f %f %f ",
|
2012-08-23 08:35:03 +00:00
|
|
|
&arg->vector[0],
|
|
|
|
&arg->vector[1],
|
|
|
|
&arg->vector[2]);
|
|
|
|
#else
|
2012-11-15 17:32:03 +00:00
|
|
|
(void)sscanf(main_params[i].value, " %f %f %f ",
|
2012-08-23 08:35:03 +00:00
|
|
|
&arg->vector[0],
|
|
|
|
&arg->vector[1],
|
|
|
|
&arg->vector[2]);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case TYPE_FLOAT:
|
2012-11-15 17:32:03 +00:00
|
|
|
arg->_float = atof(main_params[i].value);
|
2012-08-23 08:35:03 +00:00
|
|
|
break;
|
|
|
|
case TYPE_STRING:
|
2012-11-15 17:32:03 +00:00
|
|
|
arg->string = prog_tempstring(prog, main_params[i].value);
|
2012-08-23 08:35:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "error: unhandled parameter type: %i\n", main_params[i].vtype);
|
2012-08-23 08:35:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
void prog_disasm_function(qc_program_t *prog, size_t id);
|
2013-05-29 02:16:50 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2012-06-26 11:10:10 +00:00
|
|
|
size_t i;
|
2013-07-30 16:00:51 +00:00
|
|
|
qcint_t fnmain = -1;
|
|
|
|
qc_program_t *prog;
|
2012-08-19 13:19:43 +00:00
|
|
|
size_t xflags = VMXF_DEFAULT;
|
2012-08-22 12:00:57 +00:00
|
|
|
bool opts_printfields = false;
|
2012-08-22 12:15:35 +00:00
|
|
|
bool opts_printdefs = false;
|
2012-11-30 19:20:13 +00:00
|
|
|
bool opts_printfuns = false;
|
2012-11-02 11:23:55 +00:00
|
|
|
bool opts_disasm = false;
|
2012-12-18 14:39:20 +00:00
|
|
|
bool opts_info = false;
|
|
|
|
bool noexec = false;
|
2012-12-18 14:46:26 +00:00
|
|
|
const char *progsfile = NULL;
|
2012-12-23 19:45:43 +00:00
|
|
|
const char **dis_list = NULL;
|
2012-12-26 09:23:45 +00:00
|
|
|
int opts_v = 0;
|
2012-06-25 21:58:47 +00:00
|
|
|
|
2012-08-19 13:19:43 +00:00
|
|
|
arg0 = argv[0];
|
|
|
|
|
2012-12-18 14:39:20 +00:00
|
|
|
if (argc < 2) {
|
2012-08-19 13:19:43 +00:00
|
|
|
usage();
|
2012-12-18 14:39:20 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-08-19 13:19:43 +00:00
|
|
|
|
2012-12-18 14:46:26 +00:00
|
|
|
while (argc > 1) {
|
2012-12-18 14:39:20 +00:00
|
|
|
if (!strcmp(argv[1], "-h") ||
|
|
|
|
!strcmp(argv[1], "-help") ||
|
|
|
|
!strcmp(argv[1], "--help"))
|
|
|
|
{
|
|
|
|
usage();
|
|
|
|
exit(0);
|
|
|
|
}
|
2012-12-26 09:23:45 +00:00
|
|
|
else if (!strcmp(argv[1], "-v")) {
|
|
|
|
++opts_v;
|
2012-12-26 21:49:50 +00:00
|
|
|
--argc;
|
|
|
|
++argv;
|
2012-12-26 09:23:45 +00:00
|
|
|
}
|
|
|
|
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") ||
|
2012-12-18 14:46:26 +00:00
|
|
|
!strcmp(argv[1], "--version"))
|
|
|
|
{
|
|
|
|
version();
|
|
|
|
exit(0);
|
|
|
|
}
|
2012-12-18 14:39:20 +00:00
|
|
|
else if (!strcmp(argv[1], "-trace")) {
|
2012-08-19 13:19:43 +00:00
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
xflags |= VMXF_TRACE;
|
|
|
|
}
|
|
|
|
else if (!strcmp(argv[1], "-profile")) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
xflags |= VMXF_PROFILE;
|
|
|
|
}
|
2012-08-22 12:00:57 +00:00
|
|
|
else if (!strcmp(argv[1], "-info")) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
opts_info = true;
|
2012-12-18 14:39:20 +00:00
|
|
|
noexec = true;
|
2012-08-22 12:00:57 +00:00
|
|
|
}
|
2012-11-02 11:23:55 +00:00
|
|
|
else if (!strcmp(argv[1], "-disasm")) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
opts_disasm = true;
|
2012-12-18 14:39:20 +00:00
|
|
|
noexec = true;
|
2012-11-02 11:23:55 +00:00
|
|
|
}
|
2012-12-23 19:45:43 +00:00
|
|
|
else if (!strcmp(argv[1], "-disasm-func")) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
if (argc <= 1) {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
vec_push(dis_list, argv[1]);
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
noexec = true;
|
|
|
|
}
|
2012-08-22 12:15:35 +00:00
|
|
|
else if (!strcmp(argv[1], "-printdefs")) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
opts_printdefs = true;
|
2012-12-18 14:39:20 +00:00
|
|
|
noexec = true;
|
2012-08-22 12:15:35 +00:00
|
|
|
}
|
2012-11-30 19:20:13 +00:00
|
|
|
else if (!strcmp(argv[1], "-printfuns")) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
opts_printfuns = true;
|
2012-12-18 14:39:20 +00:00
|
|
|
noexec = true;
|
2012-11-30 19:20:13 +00:00
|
|
|
}
|
2012-08-22 12:00:57 +00:00
|
|
|
else if (!strcmp(argv[1], "-printfields")) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
opts_printfields = true;
|
2012-12-18 14:39:20 +00:00
|
|
|
noexec = true;
|
2012-08-22 12:00:57 +00:00
|
|
|
}
|
2012-08-23 08:35:03 +00:00
|
|
|
else if (!strcmp(argv[1], "-vector") ||
|
|
|
|
!strcmp(argv[1], "-string") ||
|
|
|
|
!strcmp(argv[1], "-float") )
|
|
|
|
{
|
|
|
|
qcvm_parameter p;
|
|
|
|
if (argv[1][1] == 'f')
|
|
|
|
p.vtype = TYPE_FLOAT;
|
|
|
|
else if (argv[1][1] == 's')
|
|
|
|
p.vtype = TYPE_STRING;
|
|
|
|
else if (argv[1][1] == 'v')
|
|
|
|
p.vtype = TYPE_VECTOR;
|
2013-07-21 21:56:03 +00:00
|
|
|
else
|
|
|
|
p.vtype = TYPE_VOID;
|
2012-08-23 08:35:03 +00:00
|
|
|
|
|
|
|
--argc;
|
|
|
|
++argv;
|
2013-01-06 15:37:02 +00:00
|
|
|
if (argc < 2) {
|
2012-08-23 08:35:03 +00:00
|
|
|
usage();
|
2012-12-18 14:39:20 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-08-23 08:35:03 +00:00
|
|
|
p.value = argv[1];
|
|
|
|
|
2012-11-15 17:32:03 +00:00
|
|
|
vec_push(main_params, p);
|
2012-08-23 08:35:03 +00:00
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
}
|
2012-12-18 14:46:26 +00:00
|
|
|
else if (!strcmp(argv[1], "--")) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (argv[1][0] != '-') {
|
|
|
|
if (progsfile) {
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "only 1 program file may be specified\n");
|
2012-12-18 14:46:26 +00:00
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
progsfile = argv[1];
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
}
|
2012-08-19 13:19:43 +00:00
|
|
|
else
|
2012-12-18 14:39:20 +00:00
|
|
|
{
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "unknown parameter: %s\n", argv[1]);
|
2012-08-19 13:19:43 +00:00
|
|
|
usage();
|
2012-12-18 14:39:20 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-06-25 21:58:47 +00:00
|
|
|
}
|
|
|
|
|
2013-01-06 15:37:02 +00:00
|
|
|
if (argc == 2 && !progsfile) {
|
2012-12-18 14:46:26 +00:00
|
|
|
progsfile = argv[1];
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!progsfile) {
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "must specify a program to execute\n");
|
2012-12-18 14:46:26 +00:00
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-08-19 13:19:43 +00:00
|
|
|
|
2013-01-17 16:36:06 +00:00
|
|
|
prog = prog_load(progsfile, noexec);
|
2012-06-25 21:58:47 +00:00
|
|
|
if (!prog) {
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "failed to load program '%s'\n", progsfile);
|
2012-06-25 21:58:47 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-07-27 17:27:56 +00:00
|
|
|
prog->builtins = qc_builtins;
|
2013-08-17 23:39:06 +00:00
|
|
|
prog->builtins_count = GMQCC_ARRAY_COUNT(qc_builtins);
|
2012-07-27 17:27:56 +00:00
|
|
|
|
2012-08-23 10:42:41 +00:00
|
|
|
if (opts_info) {
|
2012-11-28 18:16:35 +00:00
|
|
|
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));
|
2012-12-22 22:30:52 +00:00
|
|
|
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));
|
2012-08-23 10:42:41 +00:00
|
|
|
}
|
2012-08-22 12:00:57 +00:00
|
|
|
|
|
|
|
if (opts_info) {
|
|
|
|
prog_delete(prog);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-23 19:45:43 +00:00
|
|
|
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) {
|
|
|
|
const char *name = prog_getstring(prog, prog->functions[k].name);
|
|
|
|
if (!strcmp(name, dis_list[i])) {
|
|
|
|
prog_disasm_function(prog, k);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-02 11:23:55 +00:00
|
|
|
if (opts_disasm) {
|
2012-11-15 17:32:03 +00:00
|
|
|
for (i = 1; i < vec_size(prog->functions); ++i)
|
2012-11-02 11:23:55 +00:00
|
|
|
prog_disasm_function(prog, i);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-22 12:15:35 +00:00
|
|
|
if (opts_printdefs) {
|
2013-05-02 19:18:59 +00:00
|
|
|
const char *getstring = NULL;
|
2012-11-15 17:32:03 +00:00
|
|
|
for (i = 0; i < vec_size(prog->defs); ++i) {
|
2013-01-25 18:17:31 +00:00
|
|
|
printf("Global: %8s %-16s at %u%s",
|
2012-08-23 16:28:05 +00:00
|
|
|
type_name[prog->defs[i].type & DEF_TYPEMASK],
|
2012-08-22 12:15:35 +00:00
|
|
|
prog_getstring(prog, prog->defs[i].name),
|
2012-12-18 11:41:29 +00:00
|
|
|
(unsigned int)prog->defs[i].offset,
|
|
|
|
((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
|
2013-01-25 18:17:31 +00:00
|
|
|
if (opts_v) {
|
|
|
|
switch (prog->defs[i].type & DEF_TYPEMASK) {
|
|
|
|
case TYPE_FLOAT:
|
2013-07-30 16:00:51 +00:00
|
|
|
printf(" [init: %g]", ((qcany_t*)(prog->globals + prog->defs[i].offset))->_float);
|
2013-01-25 18:17:31 +00:00
|
|
|
break;
|
|
|
|
case TYPE_INTEGER:
|
2013-07-30 16:00:51 +00:00
|
|
|
printf(" [init: %i]", (int)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
|
2013-01-25 18:17:31 +00:00
|
|
|
break;
|
|
|
|
case TYPE_ENTITY:
|
|
|
|
case TYPE_FUNCTION:
|
|
|
|
case TYPE_FIELD:
|
|
|
|
case TYPE_POINTER:
|
2013-07-30 16:00:51 +00:00
|
|
|
printf(" [init: %u]", (unsigned)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
|
2013-01-25 18:17:31 +00:00
|
|
|
break;
|
|
|
|
case TYPE_STRING:
|
2013-07-30 16:00:51 +00:00
|
|
|
getstring = prog_getstring(prog, ((qcany_t*)(prog->globals + prog->defs[i].offset))->string);
|
2013-08-17 22:16:40 +00:00
|
|
|
printf(" [init: `");
|
|
|
|
print_escaped_string(getstring, strlen(getstring));
|
|
|
|
printf("`]\n");
|
2013-01-25 18:17:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
2012-08-22 12:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-18 14:39:20 +00:00
|
|
|
if (opts_printfields) {
|
2012-11-15 17:32:03 +00:00
|
|
|
for (i = 0; i < vec_size(prog->fields); ++i) {
|
2012-12-18 11:41:29 +00:00
|
|
|
printf("Field: %8s %-16s at %u%s\n",
|
2012-08-22 12:00:57 +00:00
|
|
|
type_name[prog->fields[i].type],
|
|
|
|
prog_getstring(prog, prog->fields[i].name),
|
2012-12-18 11:41:29 +00:00
|
|
|
(unsigned int)prog->fields[i].offset,
|
|
|
|
((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
|
2012-08-22 12:00:57 +00:00
|
|
|
}
|
2012-06-26 11:22:37 +00:00
|
|
|
}
|
2012-12-18 14:39:20 +00:00
|
|
|
if (opts_printfuns) {
|
2012-11-30 19:20:13 +00:00
|
|
|
for (i = 0; i < vec_size(prog->functions); ++i) {
|
|
|
|
int32_t a;
|
2012-12-05 13:22:24 +00:00
|
|
|
printf("Function: %-16s taking %i parameters:(",
|
2012-11-30 19:20:13 +00:00
|
|
|
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]);
|
|
|
|
}
|
2012-12-26 09:23:45 +00:00
|
|
|
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;
|
2013-07-30 16:00:51 +00:00
|
|
|
prog_section_statement_t *st = prog->code + start;
|
2012-12-26 09:23:45 +00:00
|
|
|
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");
|
2012-11-30 19:20:13 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-18 14:39:20 +00:00
|
|
|
if (!noexec) {
|
|
|
|
for (i = 1; i < vec_size(prog->functions); ++i) {
|
|
|
|
const char *name = prog_getstring(prog, prog->functions[i].name);
|
|
|
|
if (!strcmp(name, "main"))
|
2013-07-30 16:00:51 +00:00
|
|
|
fnmain = (qcint_t)i;
|
2012-12-18 14:39:20 +00:00
|
|
|
}
|
2012-08-22 12:00:57 +00:00
|
|
|
if (fnmain > 0)
|
|
|
|
{
|
2012-08-23 08:35:03 +00:00
|
|
|
prog_main_setparams(prog);
|
2012-08-22 12:00:57 +00:00
|
|
|
prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
|
|
|
|
}
|
|
|
|
else
|
2013-01-25 17:41:17 +00:00
|
|
|
fprintf(stderr, "No main function found\n");
|
2012-08-22 12:00:57 +00:00
|
|
|
}
|
2012-06-26 11:10:10 +00:00
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
prog_delete(prog);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-02 11:23:55 +00:00
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
void prog_disasm_function(qc_program_t *prog, size_t id) {
|
|
|
|
prog_section_function_t *fdef = prog->functions + id;
|
|
|
|
prog_section_statement_t *st;
|
2012-11-02 11:23:55 +00:00
|
|
|
|
2012-11-02 11:32:13 +00:00
|
|
|
if (fdef->entry < 0) {
|
|
|
|
printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
|
2012-11-02 11:23:55 +00:00
|
|
|
|
|
|
|
st = prog->code + fdef->entry;
|
2012-12-18 10:46:26 +00:00
|
|
|
while (st->opcode != INSTR_DONE) {
|
2012-11-02 11:23:55 +00:00
|
|
|
prog_print_statement(prog, st);
|
|
|
|
++st;
|
|
|
|
}
|
|
|
|
}
|
2012-06-25 21:58:47 +00:00
|
|
|
#endif
|
2012-11-10 20:53:37 +00:00
|
|
|
#else /* !QCVM_LOOP */
|
|
|
|
/*
|
|
|
|
* Everything from here on is not including into the compilation of the
|
|
|
|
* executor. This is simply code that is #included via #include __FILE__
|
|
|
|
* see when QCVM_LOOP is defined, the rest of the code above do not get
|
|
|
|
* re-included. So this really just acts like one large macro, but it
|
|
|
|
* sort of isn't, which makes it nicer looking.
|
|
|
|
*/
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
#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) )
|
2012-11-10 20:53:37 +00:00
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
#define GLOBAL(x) ( (qcany_t*) (prog->globals + (x)) )
|
2012-11-10 20:53:37 +00:00
|
|
|
|
|
|
|
/* to be consistent with current darkplaces behaviour */
|
|
|
|
#if !defined(FLOAT_IS_TRUE_FOR_INT)
|
|
|
|
# define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (1) {
|
2013-07-30 16:00:51 +00:00
|
|
|
prog_section_function_t *newf;
|
|
|
|
qcany_t *ed;
|
|
|
|
qcany_t *ptr;
|
2012-11-10 20:53:37 +00:00
|
|
|
|
|
|
|
++st;
|
|
|
|
|
|
|
|
#if QCVM_PROFILE
|
|
|
|
prog->profile[st - prog->code]++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if QCVM_TRACE
|
|
|
|
prog_print_statement(prog, st);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (st->opcode)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
|
|
|
|
goto cleanup;
|
|
|
|
|
2012-11-11 09:40:31 +00:00
|
|
|
case INSTR_DONE:
|
|
|
|
case INSTR_RETURN:
|
|
|
|
/* TODO: add instruction count to function profile count */
|
|
|
|
GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
|
|
|
|
GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
|
|
|
|
GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
|
2012-11-10 20:53:37 +00:00
|
|
|
|
|
|
|
st = prog->code + prog_leavefunction(prog);
|
2012-11-15 17:32:03 +00:00
|
|
|
if (!vec_size(prog->stack))
|
2012-11-10 20:53:37 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2012-11-11 09:40:31 +00:00
|
|
|
case INSTR_MUL_F:
|
|
|
|
OPC->_float = OPA->_float * OPB->_float;
|
|
|
|
break;
|
|
|
|
case INSTR_MUL_V:
|
|
|
|
OPC->_float = OPA->vector[0]*OPB->vector[0] +
|
|
|
|
OPA->vector[1]*OPB->vector[1] +
|
|
|
|
OPA->vector[2]*OPB->vector[2];
|
|
|
|
break;
|
|
|
|
case INSTR_MUL_FV:
|
2013-01-14 13:22:02 +00:00
|
|
|
{
|
2013-07-30 16:00:51 +00:00
|
|
|
qcfloat_t f = OPA->_float;
|
2013-01-14 13:22:02 +00:00
|
|
|
OPC->vector[0] = f * OPB->vector[0];
|
|
|
|
OPC->vector[1] = f * OPB->vector[1];
|
|
|
|
OPC->vector[2] = f * OPB->vector[2];
|
2012-11-11 09:40:31 +00:00
|
|
|
break;
|
2013-01-14 13:22:02 +00:00
|
|
|
}
|
2012-11-11 09:40:31 +00:00
|
|
|
case INSTR_MUL_VF:
|
2013-01-14 13:22:02 +00:00
|
|
|
{
|
2013-07-30 16:00:51 +00:00
|
|
|
qcfloat_t f = OPB->_float;
|
2013-01-14 13:22:02 +00:00
|
|
|
OPC->vector[0] = f * OPA->vector[0];
|
|
|
|
OPC->vector[1] = f * OPA->vector[1];
|
|
|
|
OPC->vector[2] = f * OPA->vector[2];
|
2012-11-11 09:40:31 +00:00
|
|
|
break;
|
2013-01-14 13:22:02 +00:00
|
|
|
}
|
2012-11-11 09:40:31 +00:00
|
|
|
case INSTR_DIV_F:
|
|
|
|
if (OPB->_float != 0.0f)
|
|
|
|
OPC->_float = OPA->_float / OPB->_float;
|
|
|
|
else
|
|
|
|
OPC->_float = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_ADD_F:
|
|
|
|
OPC->_float = OPA->_float + OPB->_float;
|
|
|
|
break;
|
|
|
|
case INSTR_ADD_V:
|
|
|
|
OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
|
|
|
|
OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
|
|
|
|
OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
|
|
|
|
break;
|
|
|
|
case INSTR_SUB_F:
|
|
|
|
OPC->_float = OPA->_float - OPB->_float;
|
|
|
|
break;
|
|
|
|
case INSTR_SUB_V:
|
|
|
|
OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
|
|
|
|
OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
|
|
|
|
OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_EQ_F:
|
|
|
|
OPC->_float = (OPA->_float == OPB->_float);
|
|
|
|
break;
|
|
|
|
case INSTR_EQ_V:
|
|
|
|
OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
|
|
|
|
(OPA->vector[1] == OPB->vector[1]) &&
|
|
|
|
(OPA->vector[2] == OPB->vector[2]) );
|
|
|
|
break;
|
|
|
|
case INSTR_EQ_S:
|
|
|
|
OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
|
|
|
|
prog_getstring(prog, OPB->string));
|
|
|
|
break;
|
|
|
|
case INSTR_EQ_E:
|
|
|
|
OPC->_float = (OPA->_int == OPB->_int);
|
|
|
|
break;
|
|
|
|
case INSTR_EQ_FNC:
|
|
|
|
OPC->_float = (OPA->function == OPB->function);
|
|
|
|
break;
|
|
|
|
case INSTR_NE_F:
|
|
|
|
OPC->_float = (OPA->_float != OPB->_float);
|
|
|
|
break;
|
|
|
|
case INSTR_NE_V:
|
|
|
|
OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
|
|
|
|
(OPA->vector[1] != OPB->vector[1]) ||
|
|
|
|
(OPA->vector[2] != OPB->vector[2]) );
|
|
|
|
break;
|
|
|
|
case INSTR_NE_S:
|
|
|
|
OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
|
|
|
|
prog_getstring(prog, OPB->string));
|
|
|
|
break;
|
|
|
|
case INSTR_NE_E:
|
|
|
|
OPC->_float = (OPA->_int != OPB->_int);
|
|
|
|
break;
|
|
|
|
case INSTR_NE_FNC:
|
|
|
|
OPC->_float = (OPA->function != OPB->function);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_LE:
|
|
|
|
OPC->_float = (OPA->_float <= OPB->_float);
|
|
|
|
break;
|
|
|
|
case INSTR_GE:
|
|
|
|
OPC->_float = (OPA->_float >= OPB->_float);
|
|
|
|
break;
|
|
|
|
case INSTR_LT:
|
|
|
|
OPC->_float = (OPA->_float < OPB->_float);
|
|
|
|
break;
|
|
|
|
case INSTR_GT:
|
|
|
|
OPC->_float = (OPA->_float > OPB->_float);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_LOAD_F:
|
|
|
|
case INSTR_LOAD_S:
|
|
|
|
case INSTR_LOAD_FLD:
|
|
|
|
case INSTR_LOAD_ENT:
|
|
|
|
case INSTR_LOAD_FNC:
|
|
|
|
if (OPA->edict < 0 || OPA->edict >= prog->entities) {
|
|
|
|
qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
|
|
|
|
qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
|
|
|
|
prog->filename,
|
|
|
|
OPB->_int);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
ed = prog_getedict(prog, OPA->edict);
|
2013-07-30 16:00:51 +00:00
|
|
|
OPC->_int = ((qcany_t*)( ((qcint_t*)ed) + OPB->_int ))->_int;
|
2012-11-11 09:40:31 +00:00
|
|
|
break;
|
|
|
|
case INSTR_LOAD_V:
|
|
|
|
if (OPA->edict < 0 || OPA->edict >= prog->entities) {
|
|
|
|
qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-07-30 16:00:51 +00:00
|
|
|
if (OPB->_int < 0 || OPB->_int + 3 > (qcint_t)prog->entityfields)
|
2012-11-11 09:40:31 +00:00
|
|
|
{
|
|
|
|
qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
|
|
|
|
prog->filename,
|
|
|
|
OPB->_int + 2);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
ed = prog_getedict(prog, OPA->edict);
|
2013-07-30 16:00:51 +00:00
|
|
|
ptr = (qcany_t*)( ((qcint_t*)ed) + OPB->_int );
|
2013-01-14 13:22:02 +00:00
|
|
|
OPC->ivector[0] = ptr->ivector[0];
|
|
|
|
OPC->ivector[1] = ptr->ivector[1];
|
|
|
|
OPC->ivector[2] = ptr->ivector[2];
|
2012-11-11 09:40:31 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_ADDRESS:
|
|
|
|
if (OPA->edict < 0 || OPA->edict >= prog->entities) {
|
|
|
|
qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
|
|
|
|
{
|
|
|
|
qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
|
|
|
|
prog->filename,
|
|
|
|
OPB->_int);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ed = prog_getedict(prog, OPA->edict);
|
2013-07-30 16:00:51 +00:00
|
|
|
OPC->_int = ((qcint_t*)ed) - prog->entitydata + OPB->_int;
|
2012-11-11 09:40:31 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_STORE_F:
|
|
|
|
case INSTR_STORE_S:
|
|
|
|
case INSTR_STORE_ENT:
|
|
|
|
case INSTR_STORE_FLD:
|
|
|
|
case INSTR_STORE_FNC:
|
|
|
|
OPB->_int = OPA->_int;
|
|
|
|
break;
|
|
|
|
case INSTR_STORE_V:
|
|
|
|
OPB->ivector[0] = OPA->ivector[0];
|
|
|
|
OPB->ivector[1] = OPA->ivector[1];
|
|
|
|
OPB->ivector[2] = OPA->ivector[2];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_STOREP_F:
|
|
|
|
case INSTR_STOREP_S:
|
|
|
|
case INSTR_STOREP_ENT:
|
|
|
|
case INSTR_STOREP_FLD:
|
|
|
|
case INSTR_STOREP_FNC:
|
2013-07-30 16:00:51 +00:00
|
|
|
if (OPB->_int < 0 || OPB->_int >= (qcint_t)vec_size(prog->entitydata)) {
|
2012-11-11 09:40:31 +00:00
|
|
|
qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-07-30 16:00:51 +00:00
|
|
|
if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
|
2012-11-11 09:40:31 +00:00
|
|
|
qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
|
|
|
|
prog->filename,
|
|
|
|
prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
|
|
|
|
OPB->_int);
|
2013-07-30 16:00:51 +00:00
|
|
|
ptr = (qcany_t*)(prog->entitydata + OPB->_int);
|
2012-11-11 09:40:31 +00:00
|
|
|
ptr->_int = OPA->_int;
|
|
|
|
break;
|
|
|
|
case INSTR_STOREP_V:
|
2013-07-30 16:00:51 +00:00
|
|
|
if (OPB->_int < 0 || OPB->_int + 2 >= (qcint_t)vec_size(prog->entitydata)) {
|
2012-11-11 09:40:31 +00:00
|
|
|
qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-07-30 16:00:51 +00:00
|
|
|
if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
|
2012-11-11 09:40:31 +00:00
|
|
|
qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
|
|
|
|
prog->filename,
|
|
|
|
prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
|
|
|
|
OPB->_int);
|
2013-07-30 16:00:51 +00:00
|
|
|
ptr = (qcany_t*)(prog->entitydata + OPB->_int);
|
2012-11-11 09:40:31 +00:00
|
|
|
ptr->ivector[0] = OPA->ivector[0];
|
|
|
|
ptr->ivector[1] = OPA->ivector[1];
|
|
|
|
ptr->ivector[2] = OPA->ivector[2];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_NOT_F:
|
|
|
|
OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
|
|
|
|
break;
|
|
|
|
case INSTR_NOT_V:
|
|
|
|
OPC->_float = !OPA->vector[0] &&
|
|
|
|
!OPA->vector[1] &&
|
|
|
|
!OPA->vector[2];
|
|
|
|
break;
|
|
|
|
case INSTR_NOT_S:
|
|
|
|
OPC->_float = !OPA->string ||
|
|
|
|
!*prog_getstring(prog, OPA->string);
|
|
|
|
break;
|
|
|
|
case INSTR_NOT_ENT:
|
|
|
|
OPC->_float = (OPA->edict == 0);
|
|
|
|
break;
|
|
|
|
case INSTR_NOT_FNC:
|
|
|
|
OPC->_float = !OPA->function;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_IF:
|
|
|
|
/* this is consistent with darkplaces' behaviour */
|
|
|
|
if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
|
|
|
|
{
|
|
|
|
st += st->o2.s1 - 1; /* offset the s++ */
|
|
|
|
if (++jumpcount >= maxjumps)
|
|
|
|
qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case INSTR_IFNOT:
|
|
|
|
if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
|
|
|
|
{
|
|
|
|
st += st->o2.s1 - 1; /* offset the s++ */
|
|
|
|
if (++jumpcount >= maxjumps)
|
|
|
|
qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_CALL0:
|
|
|
|
case INSTR_CALL1:
|
|
|
|
case INSTR_CALL2:
|
|
|
|
case INSTR_CALL3:
|
|
|
|
case INSTR_CALL4:
|
|
|
|
case INSTR_CALL5:
|
|
|
|
case INSTR_CALL6:
|
|
|
|
case INSTR_CALL7:
|
|
|
|
case INSTR_CALL8:
|
|
|
|
prog->argc = st->opcode - INSTR_CALL0;
|
|
|
|
if (!OPA->function)
|
|
|
|
qcvmerror(prog, "NULL function in `%s`", prog->filename);
|
|
|
|
|
2013-07-30 16:00:51 +00:00
|
|
|
if(!OPA->function || OPA->function >= (qcint_t)vec_size(prog->functions))
|
2012-11-11 09:40:31 +00:00
|
|
|
{
|
|
|
|
qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
newf = &prog->functions[OPA->function];
|
|
|
|
newf->profile++;
|
|
|
|
|
|
|
|
prog->statement = (st - prog->code) + 1;
|
|
|
|
|
|
|
|
if (newf->entry < 0)
|
|
|
|
{
|
|
|
|
/* negative statements are built in functions */
|
2013-07-30 16:00:51 +00:00
|
|
|
qcint_t builtinnumber = -newf->entry;
|
|
|
|
if (builtinnumber < (qcint_t)prog->builtins_count && prog->builtins[builtinnumber])
|
2012-11-11 09:40:31 +00:00
|
|
|
prog->builtins[builtinnumber](prog);
|
|
|
|
else
|
|
|
|
qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
|
|
|
|
builtinnumber, prog->filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
|
|
|
|
if (prog->vmerror)
|
|
|
|
goto cleanup;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_STATE:
|
|
|
|
qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_GOTO:
|
|
|
|
st += st->o1.s1 - 1; /* offset the s++ */
|
|
|
|
if (++jumpcount == 10000000)
|
|
|
|
qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_AND:
|
|
|
|
OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
|
|
|
|
FLOAT_IS_TRUE_FOR_INT(OPB->_int);
|
|
|
|
break;
|
|
|
|
case INSTR_OR:
|
|
|
|
OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
|
|
|
|
FLOAT_IS_TRUE_FOR_INT(OPB->_int);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTR_BITAND:
|
|
|
|
OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
|
|
|
|
break;
|
|
|
|
case INSTR_BITOR:
|
|
|
|
OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
|
|
|
|
break;
|
2012-11-10 20:53:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef QCVM_PROFILE
|
|
|
|
#undef QCVM_TRACE
|
|
|
|
#endif /* !QCVM_LOOP */
|