*/
#include "gmqcc.h"
-/* This is outrageous! */
-#define QCINT_ENTRY void*
-#define QCINT_TO_HASH_ENTRY(q) ((void*)(uintptr_t)(q))
-#define HASH_ENTRY_TO_QCINT(h) ((qcint)(uintptr_t)(h))
+/*
+ * We could use the old method of casting to uintptr_t then to void*
+ * or qcint; however, it's incredibly unsafe for two reasons.
+ * 1) The compilers aliasing optimization can legally make it unstable
+ * (it's undefined behaviour).
+ *
+ * 2) The cast itself depends on fresh storage (newly allocated in which
+ * ever function is using the cast macros), the contents of which are
+ * transferred in a way that the obligation to release storage is not
+ * propagated.
+ */
+typedef union {
+ void *enter;
+ qcint leave;
+} code_hash_entry_t;
+
+/* Some sanity macros */
+#define CODE_HASH_ENTER(ENTRY) ((ENTRY).enter)
+#define CODE_HASH_LEAVE(ENTRY) ((ENTRY).leave)
void code_push_statement(code_t *code, prog_section_statement *stmt, int linenum)
{
void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin);
-uint32_t code_genstring(code_t *code, const char *str)
-{
- uint32_t off;
- size_t hash;
- QCINT_ENTRY existing;
+uint32_t code_genstring(code_t *code, const char *str) {
+ size_t hash;
+ code_hash_entry_t existing;
if (!str)
return 0;
}
if (OPTS_OPTIMIZATION(OPTIM_OVERLAP_STRINGS)) {
- hash = ((unsigned char*)str)[strlen(str)-1];
- existing = code_util_str_htgeth(code->string_cache, str, hash);
+ hash = ((unsigned char*)str)[strlen(str)-1];
+ CODE_HASH_ENTER(existing) = code_util_str_htgeth(code->string_cache, str, hash);
} else {
- hash = util_hthash(code->string_cache, str);
- existing = util_htgeth(code->string_cache, str, hash);
+ hash = util_hthash(code->string_cache, str);
+ CODE_HASH_ENTER(existing) = util_htgeth(code->string_cache, str, hash);
}
- if (existing)
- return HASH_ENTRY_TO_QCINT(existing);
+ if (CODE_HASH_ENTER(existing))
+ return CODE_HASH_LEAVE(existing);
- off = vec_size(code->chars);
+ CODE_HASH_LEAVE(existing) = vec_size(code->chars);
vec_upload(code->chars, str, strlen(str)+1);
- util_htseth(code->string_cache, str, hash, QCINT_TO_HASH_ENTRY(off));
- return off;
+ util_htseth(code->string_cache, str, hash, CODE_HASH_ENTER(existing));
+ return CODE_HASH_LEAVE(existing);
}
qcint code_alloc_field (code_t *code, size_t qcsize)
* VM code
*/
-char* prog_getstring(qc_program *prog, qcint str)
-{
+const char* prog_getstring(qc_program *prog, qcint str) {
/* cast for return required for C++ */
if (str < 0 || str >= (qcint)vec_size(prog->strings))
- return (char*)"<<<invalid string>>>";
+ return "<<<invalid string>>>";
+
return prog->strings + str;
}
-prog_section_def* prog_entfield(qc_program *prog, qcint off)
-{
+prog_section_def* prog_entfield(qc_program *prog, qcint off) {
size_t i;
for (i = 0; i < vec_size(prog->fields); ++i) {
if (prog->fields[i].offset == off)
return NULL;
}
-qcany* prog_getedict(qc_program *prog, qcint e)
-{
+qcany* prog_getedict(qc_program *prog, qcint e) {
if (e >= (qcint)vec_size(prog->entitypool)) {
prog->vmerror++;
fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
return (qcany*)(prog->entitydata + (prog->entityfields * e));
}
-qcint prog_spawn_entity(qc_program *prog)
-{
+qcint prog_spawn_entity(qc_program *prog) {
char *data;
qcint e;
for (e = 0; e < (qcint)vec_size(prog->entitypool); ++e) {
return e;
}
-void prog_free_entity(qc_program *prog, qcint e)
-{
+void prog_free_entity(qc_program *prog, qcint e) {
if (!e) {
prog->vmerror++;
fprintf(stderr, "Trying to free world entity\n");
prog->entitypool[e] = false;
}
-qcint prog_tempstring(qc_program *prog, const char *str)
-{
+qcint prog_tempstring(qc_program *prog, const char *str) {
size_t len = strlen(str);
size_t at = prog->tempstring_at;
return at;
}
-static size_t print_escaped_string(const char *str, size_t maxlen)
-{
+static size_t print_escaped_string(const char *str, size_t maxlen) {
size_t len = 2;
putchar('"');
--maxlen; /* because we're lazy and have escape sequences */
return len;
}
-static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
-{
+static void trace_print_global(qc_program *prog, unsigned int glob, int vtype) {
static char spaces[28+1] = " ";
prog_section_def *def;
qcany *value;
}
}
-static void prog_print_statement(qc_program *prog, prog_section_statement *st)
-{
+static void prog_print_statement(qc_program *prog, prog_section_statement *st) {
if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
printf("<illegal instruction %d>\n", st->opcode);
return;
}
}
-static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
-{
+static qcint prog_enterfunction(qc_program *prog, prog_section_function *func) {
qc_exec_stack st;
size_t parampos;
int32_t p;
return func->entry;
}
-static qcint prog_leavefunction(qc_program *prog)
-{
+static qcint prog_leavefunction(qc_program *prog) {
prog_section_function *prev = NULL;
size_t oldsp;
return st.stmt - 1; /* offset the ++st */
}
-bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps)
-{
+bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps) {
long jumpcount = 0;
size_t oldxflags = prog->xflags;
prog_section_statement *st;
#define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
#define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
-static int qc_print(qc_program *prog)
-{
+static int qc_print(qc_program *prog) {
size_t i;
const char *laststr = NULL;
for (i = 0; i < (size_t)prog->argc; ++i) {
return 0;
}
-static int qc_error(qc_program *prog)
-{
+static int qc_error(qc_program *prog) {
fprintf(stderr, "*** VM raised an error:\n");
qc_print(prog);
prog->vmerror++;
return -1;
}
-static int qc_ftos(qc_program *prog)
-{
+static int qc_ftos(qc_program *prog) {
char buffer[512];
qcany *num;
qcany str;
return 0;
}
-static int qc_stof(qc_program *prog)
-{
+static int qc_stof(qc_program *prog) {
qcany *str;
qcany num;
CheckArgs(1);
return 0;
}
-static int qc_vtos(qc_program *prog)
-{
+static int qc_vtos(qc_program *prog) {
char buffer[512];
qcany *num;
qcany str;
return 0;
}
-static int qc_etos(qc_program *prog)
-{
+static int qc_etos(qc_program *prog) {
char buffer[512];
qcany *num;
qcany str;
return 0;
}
-static int qc_spawn(qc_program *prog)
-{
+static int qc_spawn(qc_program *prog) {
qcany ent;
CheckArgs(0);
ent.edict = prog_spawn_entity(prog);
return (ent.edict ? 0 : -1);
}
-static int qc_kill(qc_program *prog)
-{
+static int qc_kill(qc_program *prog) {
qcany *ent;
CheckArgs(1);
ent = GetArg(0);
return 0;
}
-static int qc_sqrt(qc_program *prog)
-{
+static int qc_sqrt(qc_program *prog) {
qcany *num, out;
CheckArgs(1);
num = GetArg(0);
return 0;
}
-static int qc_vlen(qc_program *prog)
-{
+static int qc_vlen(qc_program *prog) {
qcany *vec, len;
CheckArgs(1);
vec = GetArg(0);
return 0;
}
-static int qc_normalize(qc_program *prog)
-{
+static int qc_normalize(qc_program *prog) {
double len;
qcany *vec;
qcany out;
return 0;
}
-static int qc_strcat(qc_program *prog)
-{
+static int qc_strcat(qc_program *prog) {
char *buffer;
size_t len1, len2;
- char *cstr1, *cstr2;
qcany *str1, *str2;
qcany out;
+
+ const char *cstr1;
+ const char *cstr2;
CheckArgs(2);
str1 = GetArg(0);
return 0;
}
-static int qc_strcmp(qc_program *prog)
-{
- char *cstr1, *cstr2;
+static int qc_strcmp(qc_program *prog) {
qcany *str1, *str2;
qcany out;
+ const char *cstr1;
+ const char *cstr2;
+
if (prog->argc != 2 && prog->argc != 3) {
fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
prog->argc);
return 0;
}
-static int qc_floor(qc_program *prog)
-{
+static int qc_floor(qc_program *prog) {
qcany *num, out;
CheckArgs(1);
num = GetArg(0);
);
}
-static void usage()
-{
+static void usage() {
printf("usage: %s [options] [parameters] file\n", arg0);
printf("options:\n");
printf(" -h, --help print this message\n"
" -string <s> pass a string parameter to main() \n");
}
-static void prog_main_setparams(qc_program *prog)
-{
+static void prog_main_setparams(qc_program *prog) {
size_t i;
qcany *arg;
}
void prog_disasm_function(qc_program *prog, size_t id);
-int main(int argc, char **argv)
-{
+
+int main(int argc, char **argv) {
size_t i;
qcint fnmain = -1;
qc_program *prog;
return 0;
}
-void prog_disasm_function(qc_program *prog, size_t id)
-{
+void prog_disasm_function(qc_program *prog, size_t id) {
prog_section_function *fdef = prog->functions + id;
prog_section_statement *st;