2001-06-04 02:41:45 +00:00
|
|
|
/* Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
See file, 'COPYING', for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <QF/hash.h>
|
|
|
|
|
|
|
|
#include "qfcc.h"
|
|
|
|
|
|
|
|
hashtab_t *opcode_table;
|
|
|
|
hashtab_t *opcode_priority_table;
|
|
|
|
hashtab_t *opcode_priority_type_table_ab;
|
|
|
|
hashtab_t *opcode_priority_type_table_abc;
|
|
|
|
|
|
|
|
opcode_t *op_done;
|
|
|
|
opcode_t *op_return;
|
|
|
|
opcode_t *op_if;
|
|
|
|
opcode_t *op_ifnot;
|
|
|
|
opcode_t *op_state;
|
|
|
|
opcode_t *op_goto;
|
|
|
|
|
|
|
|
opcode_t pr_opcodes[] = {
|
2001-07-14 01:52:05 +00:00
|
|
|
{"<DONE>", "done", OP_DONE, -1, false, ev_entity, ev_field, ev_void, PROG_ID_VERSION},
|
|
|
|
|
|
|
|
{"*", "mul.f", OP_MUL_F, 2, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
{"*", "mul.v", OP_MUL_V, 2, false, ev_vector, ev_vector, ev_float, PROG_ID_VERSION},
|
|
|
|
{"*", "mul.fv", OP_MUL_FV, 2, false, ev_float, ev_vector, ev_vector, PROG_ID_VERSION},
|
|
|
|
{"*", "mul.vf", OP_MUL_VF, 2, false, ev_vector, ev_float, ev_vector, PROG_ID_VERSION},
|
|
|
|
|
|
|
|
{"/", "div.f", OP_DIV_F, 2, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
|
|
|
|
{"+", "add.f", OP_ADD_F, 3, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
{"+", "add.v", OP_ADD_V, 3, false, ev_vector, ev_vector, ev_vector, PROG_ID_VERSION},
|
|
|
|
{"+", "add.s", OP_ADD_S, 3, false, ev_string, ev_string, ev_string, PROG_VERSION},
|
|
|
|
|
|
|
|
{"-", "sub.f", OP_SUB_F, 3, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
{"-", "sub.v", OP_SUB_V, 3, false, ev_vector, ev_vector, ev_vector, PROG_ID_VERSION},
|
|
|
|
|
|
|
|
{"==", "eq.f", OP_EQ_F, 4, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
{"==", "eq.v", OP_EQ_V, 4, false, ev_vector, ev_vector, ev_float, PROG_ID_VERSION},
|
|
|
|
{"==", "eq.s", OP_EQ_S, 4, false, ev_string, ev_string, ev_float, PROG_ID_VERSION},
|
|
|
|
{"==", "eq.e", OP_EQ_E, 4, false, ev_entity, ev_entity, ev_float, PROG_ID_VERSION},
|
|
|
|
{"==", "eq.fnc", OP_EQ_FNC, 4, false, ev_func, ev_func, ev_float, PROG_ID_VERSION},
|
|
|
|
|
|
|
|
{"!=", "ne.f", OP_NE_F, 4, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
{"!=", "ne.v", OP_NE_V, 4, false, ev_vector, ev_vector, ev_float, PROG_ID_VERSION},
|
|
|
|
{"!=", "ne.s", OP_NE_S, 4, false, ev_string, ev_string, ev_float, PROG_ID_VERSION},
|
|
|
|
{"!=", "ne.e", OP_NE_E, 4, false, ev_entity, ev_entity, ev_float, PROG_ID_VERSION},
|
|
|
|
{"!=", "ne.fnc", OP_NE_FNC, 4, false, ev_func, ev_func, ev_float, PROG_ID_VERSION},
|
|
|
|
|
|
|
|
{"<=", "le", OP_LE, 4, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
{">=", "ge", OP_GE, 4, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
{"<=", "le.s", OP_LE_S, 4, false, ev_string, ev_string, ev_float, PROG_VERSION},
|
|
|
|
{">=", "ge.s", OP_GE_S, 4, false, ev_string, ev_string, ev_float, PROG_VERSION},
|
|
|
|
{"<", "lt", OP_LT, 4, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
{">", "gt", OP_GT, 4, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
{"<", "lt.s", OP_LT_S, 4, false, ev_string, ev_string, ev_float, PROG_VERSION},
|
|
|
|
{">", "gt.s", OP_GT_S, 4, false, ev_string, ev_string, ev_float, PROG_VERSION},
|
|
|
|
|
|
|
|
{".", "load.f", OP_LOAD_F, 1, false, ev_entity, ev_field, ev_float, PROG_ID_VERSION},
|
|
|
|
{".", "load.v", OP_LOAD_V, 1, false, ev_entity, ev_field, ev_vector, PROG_ID_VERSION},
|
|
|
|
{".", "load.s", OP_LOAD_S, 1, false, ev_entity, ev_field, ev_string, PROG_ID_VERSION},
|
|
|
|
{".", "load.ent", OP_LOAD_ENT, 1, false, ev_entity, ev_field, ev_entity, PROG_ID_VERSION},
|
|
|
|
{".", "load.fld", OP_LOAD_FLD, 1, false, ev_entity, ev_field, ev_field, PROG_ID_VERSION},
|
|
|
|
{".", "load.fnc", OP_LOAD_FNC, 1, false, ev_entity, ev_field, ev_func, PROG_ID_VERSION},
|
|
|
|
|
|
|
|
{".", "address", OP_ADDRESS, 1, false, ev_entity, ev_field, ev_pointer, PROG_ID_VERSION},
|
|
|
|
|
|
|
|
{"=", "store.f", OP_STORE_F, 5, true, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
{"=", "store.v", OP_STORE_V, 5, true, ev_vector, ev_vector, ev_vector, PROG_ID_VERSION},
|
|
|
|
{"=", "store.s", OP_STORE_S, 5, true, ev_string, ev_string, ev_string, PROG_ID_VERSION},
|
|
|
|
{"=", "store.ent", OP_STORE_ENT, 5, true, ev_entity, ev_entity, ev_entity, PROG_ID_VERSION},
|
|
|
|
{"=", "store.fld", OP_STORE_FLD, 5, true, ev_field, ev_field, ev_field, PROG_ID_VERSION},
|
|
|
|
{"=", "store.fnc", OP_STORE_FNC, 5, true, ev_func, ev_func, ev_func, PROG_ID_VERSION},
|
|
|
|
|
|
|
|
{"=", "storep.f", OP_STOREP_F, 5, true, ev_pointer, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
{"=", "storep.v", OP_STOREP_V, 5, true, ev_pointer, ev_vector, ev_vector, PROG_ID_VERSION},
|
|
|
|
{"=", "storep.s", OP_STOREP_S, 5, true, ev_pointer, ev_string, ev_string, PROG_ID_VERSION},
|
|
|
|
{"=", "storep.ent", OP_STOREP_ENT, 5, true, ev_pointer, ev_entity, ev_entity, PROG_ID_VERSION},
|
|
|
|
{"=", "storep.fld", OP_STOREP_FLD, 5, true, ev_pointer, ev_field, ev_field, PROG_ID_VERSION},
|
|
|
|
{"=", "storep.fnc", OP_STOREP_FNC, 5, true, ev_pointer, ev_func, ev_func, PROG_ID_VERSION},
|
|
|
|
|
|
|
|
{"<RETURN>", "return", OP_RETURN, -1, false, ev_void, ev_void, ev_void, PROG_ID_VERSION},
|
|
|
|
|
|
|
|
{"!", "not.f", OP_NOT_F, -1, false, ev_float, ev_void, ev_float, PROG_ID_VERSION},
|
|
|
|
{"!", "not.v", OP_NOT_V, -1, false, ev_vector, ev_void, ev_float, PROG_ID_VERSION},
|
|
|
|
{"!", "not.s", OP_NOT_S, -1, false, ev_string, ev_void, ev_float, PROG_ID_VERSION},
|
|
|
|
{"!", "not.ent", OP_NOT_ENT, -1, false, ev_entity, ev_void, ev_float, PROG_ID_VERSION},
|
|
|
|
{"!", "not.fnc", OP_NOT_FNC, -1, false, ev_func, ev_void, ev_float, PROG_ID_VERSION},
|
|
|
|
|
|
|
|
{"<IF>", "if", OP_IF, -1, false, ev_float, ev_float, ev_void, PROG_ID_VERSION},
|
|
|
|
{"<IFNOT>", "ifnot", OP_IFNOT, -1, false, ev_float, ev_float, ev_void, PROG_ID_VERSION},
|
2001-06-04 02:41:45 +00:00
|
|
|
|
|
|
|
// calls returns REG_RETURN
|
2001-07-14 01:52:05 +00:00
|
|
|
{"<CALL0>", "call0", OP_CALL0, -1, false, ev_func, ev_void, ev_void, PROG_ID_VERSION},
|
|
|
|
{"<CALL1>", "call1", OP_CALL1, -1, false, ev_func, ev_void, ev_void, PROG_ID_VERSION},
|
|
|
|
{"<CALL2>", "call2", OP_CALL2, -1, false, ev_func, ev_void, ev_void, PROG_ID_VERSION},
|
|
|
|
{"<CALL3>", "call3", OP_CALL3, -1, false, ev_func, ev_void, ev_void, PROG_ID_VERSION},
|
|
|
|
{"<CALL4>", "call4", OP_CALL4, -1, false, ev_func, ev_void, ev_void, PROG_ID_VERSION},
|
|
|
|
{"<CALL5>", "call5", OP_CALL5, -1, false, ev_func, ev_void, ev_void, PROG_ID_VERSION},
|
|
|
|
{"<CALL6>", "call6", OP_CALL6, -1, false, ev_func, ev_void, ev_void, PROG_ID_VERSION},
|
|
|
|
{"<CALL7>", "call7", OP_CALL7, -1, false, ev_func, ev_void, ev_void, PROG_ID_VERSION},
|
|
|
|
{"<CALL8>", "call8", OP_CALL8, -1, false, ev_func, ev_void, ev_void, PROG_ID_VERSION},
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2001-07-14 01:52:05 +00:00
|
|
|
{"<STATE>", "state", OP_STATE, -1, false, ev_float, ev_float, ev_void, PROG_ID_VERSION},
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2001-07-14 01:52:05 +00:00
|
|
|
{"<GOTO>", "goto", OP_GOTO, -1, false, ev_float, ev_void, ev_void, PROG_ID_VERSION},
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2001-07-14 01:52:05 +00:00
|
|
|
{"&&", "and", OP_AND, 6, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
{"||", "or", OP_OR, 6, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2001-07-14 01:52:05 +00:00
|
|
|
{"&", "bitand", OP_BITAND, 2, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
{"|", "bitor", OP_BITOR, 2, false, ev_float, ev_float, ev_float, PROG_ID_VERSION},
|
|
|
|
};
|
|
|
|
|
|
|
|
static type_t *types[] = {
|
|
|
|
&type_void,
|
|
|
|
&type_string,
|
|
|
|
&type_float,
|
|
|
|
&type_vector,
|
|
|
|
&type_entity,
|
|
|
|
&type_field,
|
|
|
|
&type_function,
|
|
|
|
&type_pointer,
|
2001-06-04 02:41:45 +00:00
|
|
|
};
|
|
|
|
|
2001-06-26 07:21:20 +00:00
|
|
|
statref_t *
|
|
|
|
PR_NewStatref (dstatement_t *st, int field)
|
|
|
|
{
|
|
|
|
statref_t *ref = calloc (1, sizeof (statref_t));
|
|
|
|
ref->statement = st;
|
|
|
|
ref->field = field;
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
2001-06-05 08:09:12 +00:00
|
|
|
void
|
|
|
|
PR_AddStatementRef (def_t *def, dstatement_t *st, int field)
|
|
|
|
{
|
|
|
|
if (def) {
|
2001-06-26 07:21:20 +00:00
|
|
|
statref_t *ref = PR_NewStatref (st, field);
|
2001-06-05 08:09:12 +00:00
|
|
|
ref->next = def->refs;
|
|
|
|
def->refs = ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-04 02:41:45 +00:00
|
|
|
/*
|
|
|
|
PR_Statement
|
|
|
|
|
|
|
|
Emits a primitive statement, returning the var it places it's value in
|
|
|
|
*/
|
|
|
|
def_t *
|
|
|
|
PR_Statement (opcode_t * op, def_t * var_a, def_t * var_b)
|
|
|
|
{
|
|
|
|
dstatement_t *statement;
|
|
|
|
def_t *var_c;
|
|
|
|
|
|
|
|
statement = &statements[numstatements];
|
|
|
|
numstatements++;
|
|
|
|
|
|
|
|
statement_linenums[statement - statements] = pr_source_line;
|
|
|
|
statement->op = op->opcode;
|
|
|
|
statement->a = var_a ? var_a->ofs : 0;
|
|
|
|
statement->b = var_b ? var_b->ofs : 0;
|
2001-07-14 01:52:05 +00:00
|
|
|
if (op->type_c == ev_void || op->right_associative) {
|
2001-06-04 02:41:45 +00:00
|
|
|
// ifs, gotos, and assignments don't need vars allocated
|
|
|
|
var_c = NULL;
|
|
|
|
statement->c = 0;
|
|
|
|
} else { // allocate result space
|
2001-07-14 01:52:05 +00:00
|
|
|
var_c = PR_GetTempDef (types[op->type_c], pr_scope);
|
2001-06-04 17:52:50 +00:00
|
|
|
statement->c = var_c->ofs;
|
2001-06-04 02:41:45 +00:00
|
|
|
}
|
2001-06-05 08:09:12 +00:00
|
|
|
PR_AddStatementRef (var_a, statement, 0);
|
|
|
|
PR_AddStatementRef (var_b, statement, 1);
|
|
|
|
PR_AddStatementRef (var_c, statement, 2);
|
2001-06-04 02:41:45 +00:00
|
|
|
|
|
|
|
if (op->right_associative)
|
|
|
|
return var_a;
|
|
|
|
|
|
|
|
return var_c;
|
|
|
|
}
|
|
|
|
|
2001-06-04 04:52:14 +00:00
|
|
|
static const char *
|
2001-06-04 02:41:45 +00:00
|
|
|
get_key (void *_op, void *_tab)
|
|
|
|
{
|
|
|
|
opcode_t *op = (opcode_t *)_op;
|
|
|
|
hashtab_t **tab = (hashtab_t **)_tab;
|
|
|
|
static char rep[100];
|
|
|
|
char *r = rep;
|
|
|
|
const char *s = op->name;
|
|
|
|
|
|
|
|
if (tab == &opcode_priority_table) {
|
|
|
|
while (*s)
|
|
|
|
*r++ = *s++;
|
|
|
|
*r++ = op->priority + 2;
|
|
|
|
*r = 0;
|
|
|
|
} else if (tab == &opcode_priority_type_table_ab) {
|
|
|
|
while (*s)
|
|
|
|
*r++ = *s++;
|
|
|
|
*r++ = op->priority + 2;
|
2001-07-14 01:52:05 +00:00
|
|
|
*r++ = op->type_a + 2;
|
|
|
|
*r++ = op->type_b + 2;
|
2001-06-04 02:41:45 +00:00
|
|
|
*r = 0;
|
|
|
|
} else if (tab == &opcode_priority_type_table_abc) {
|
|
|
|
while (*s)
|
|
|
|
*r++ = *s++;
|
|
|
|
*r++ = op->priority + 2;
|
2001-07-14 01:52:05 +00:00
|
|
|
*r++ = op->type_a + 2;
|
|
|
|
*r++ = op->type_b + 2;
|
|
|
|
*r++ = op->type_c + 2;
|
2001-06-04 02:41:45 +00:00
|
|
|
*r = 0;
|
|
|
|
} else if (tab == &opcode_table) {
|
|
|
|
*r++ = (op->opcode & 0xff) + 2;
|
|
|
|
*r++ = ((op->opcode >> 8) & 0xff) + 2;
|
|
|
|
*r = 0;
|
|
|
|
} else {
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
return rep;
|
|
|
|
}
|
|
|
|
|
|
|
|
opcode_t *
|
|
|
|
PR_Opcode_Find (const char *name, int priority, def_t *var_a, def_t *var_b, def_t *var_c)
|
|
|
|
{
|
|
|
|
opcode_t op;
|
|
|
|
hashtab_t **tab;
|
|
|
|
char rep[100];
|
|
|
|
|
|
|
|
op.name = name;
|
|
|
|
op.priority = priority;
|
|
|
|
if (var_a && var_b && var_c) {
|
2001-07-14 01:52:05 +00:00
|
|
|
op.type_a = var_a->type->type;
|
|
|
|
op.type_b = var_b->type->type;
|
|
|
|
op.type_c = var_c->type->type;
|
|
|
|
if (op.type_c == ev_void)
|
2001-06-04 02:41:45 +00:00
|
|
|
tab = &opcode_priority_type_table_ab;
|
|
|
|
else
|
|
|
|
tab = &opcode_priority_type_table_abc;
|
|
|
|
strcpy (rep, get_key (&op, tab));
|
|
|
|
//printf ("%s\n", rep);
|
|
|
|
return Hash_Find (*tab, rep);
|
|
|
|
} else {
|
|
|
|
strcpy (rep, get_key (&op, &opcode_priority_table));
|
|
|
|
return Hash_Find (opcode_priority_table, rep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opcode_t *
|
|
|
|
PR_Opcode (short opcode)
|
|
|
|
{
|
|
|
|
opcode_t op;
|
|
|
|
char rep[100];
|
|
|
|
|
|
|
|
op.opcode = opcode;
|
|
|
|
strcpy (rep, get_key (&op, &opcode_table));
|
|
|
|
return Hash_Find (opcode_table, rep);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PR_Opcode_Init (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
opcode_table = Hash_NewTable (1021, get_key, 0, &opcode_table);
|
|
|
|
opcode_priority_table = Hash_NewTable (1021, get_key, 0,
|
|
|
|
&opcode_priority_table);
|
|
|
|
opcode_priority_type_table_ab = Hash_NewTable (1021, get_key, 0,
|
|
|
|
&opcode_priority_type_table_ab);
|
|
|
|
opcode_priority_type_table_abc = Hash_NewTable (1021, get_key, 0,
|
|
|
|
&opcode_priority_type_table_abc);
|
|
|
|
for (i = 0; i < sizeof (pr_opcodes) / sizeof (pr_opcodes[0]); i++) {
|
|
|
|
opcode_t *op = &pr_opcodes[i];
|
2001-06-09 06:25:33 +00:00
|
|
|
if (op->min_version > options.version)
|
|
|
|
continue;
|
2001-06-04 02:41:45 +00:00
|
|
|
Hash_Add (opcode_table, op);
|
|
|
|
Hash_Add (opcode_priority_table, op);
|
|
|
|
Hash_Add (opcode_priority_type_table_ab, op);
|
|
|
|
Hash_Add (opcode_priority_type_table_abc, op);
|
|
|
|
if (!strcmp (op->name, "<DONE>")) {
|
|
|
|
op_done = op;
|
|
|
|
} else if (!strcmp (op->name, "<RETURN>")) {
|
|
|
|
op_return = op;
|
|
|
|
} else if (!strcmp (op->name, "<IF>")) {
|
|
|
|
op_if = op;
|
|
|
|
} else if (!strcmp (op->name, "<IFNOT>")) {
|
|
|
|
op_ifnot = op;
|
|
|
|
} else if (!strcmp (op->name, "<STATE>")) {
|
|
|
|
op_state = op;
|
|
|
|
} else if (!strcmp (op->name, "<GOTO>")) {
|
|
|
|
op_goto = op;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|