2002-10-22 14:53:18 +00:00
|
|
|
/*
|
|
|
|
opcodes.c
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
opcode searching
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2002/06/01
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
2001-06-04 02:41:45 +00:00
|
|
|
|
|
|
|
*/
|
2001-09-28 07:09:38 +00:00
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
2001-06-04 02:41:45 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2002-06-01 04:41:25 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
2001-06-04 02:41:45 +00:00
|
|
|
|
|
|
|
#include <QF/hash.h>
|
|
|
|
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "def.h"
|
|
|
|
#include "opcodes.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "options.h"
|
2002-06-07 17:29:30 +00:00
|
|
|
#include "qfcc.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "type.h"
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
hashtab_t *opcode_type_table_ab;
|
|
|
|
hashtab_t *opcode_type_table_abc;
|
|
|
|
|
|
|
|
opcode_t *op_done;
|
|
|
|
opcode_t *op_return;
|
|
|
|
opcode_t *op_if;
|
|
|
|
opcode_t *op_ifnot;
|
|
|
|
opcode_t *op_ifbe;
|
|
|
|
opcode_t *op_ifb;
|
|
|
|
opcode_t *op_ifae;
|
|
|
|
opcode_t *op_ifa;
|
|
|
|
opcode_t *op_state;
|
|
|
|
opcode_t *op_goto;
|
|
|
|
opcode_t *op_jump;
|
|
|
|
opcode_t *op_jumpb;
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2001-08-16 02:51:53 +00:00
|
|
|
#define ROTL(x,n) (((x)<<(n))|(x)>>(32-n))
|
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
get_hash (void *_op, void *_tab)
|
2001-06-04 02:41:45 +00:00
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
opcode_t *op = (opcode_t *) _op;
|
|
|
|
hashtab_t **tab = (hashtab_t **) _tab;
|
2001-08-16 02:51:53 +00:00
|
|
|
unsigned long hash;
|
|
|
|
|
2001-10-25 17:48:35 +00:00
|
|
|
if (tab == &opcode_type_table_ab) {
|
2001-12-06 19:49:40 +00:00
|
|
|
hash = ROTL (~op->type_a, 8) + ROTL (~op->type_b, 16);
|
2001-10-25 17:48:35 +00:00
|
|
|
} else if (tab == &opcode_type_table_abc) {
|
2001-12-06 19:49:40 +00:00
|
|
|
hash = ROTL (~op->type_a, 8) + ROTL (~op->type_b, 16)
|
|
|
|
+ ROTL (~op->type_c, 24);
|
2001-08-16 02:51:53 +00:00
|
|
|
} else {
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
return hash + Hash_String (op->name);;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
compare (void *_opa, void *_opb, void *_tab)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
opcode_t *opa = (opcode_t *) _opa;
|
|
|
|
opcode_t *opb = (opcode_t *) _opb;
|
|
|
|
hashtab_t **tab = (hashtab_t **) _tab;
|
|
|
|
int cmp;
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2001-10-25 17:48:35 +00:00
|
|
|
if (tab == &opcode_type_table_ab) {
|
|
|
|
cmp = (opa->type_a == opb->type_a)
|
2001-08-16 02:51:53 +00:00
|
|
|
&& (opa->type_b == opb->type_b);
|
2001-10-25 17:48:35 +00:00
|
|
|
} else if (tab == &opcode_type_table_abc) {
|
|
|
|
cmp = (opa->type_a == opb->type_a)
|
2001-08-16 02:51:53 +00:00
|
|
|
&& (opa->type_b == opb->type_b)
|
|
|
|
&& (opa->type_c == opb->type_c);
|
2001-06-04 02:41:45 +00:00
|
|
|
} else {
|
|
|
|
abort ();
|
|
|
|
}
|
2001-08-16 02:51:53 +00:00
|
|
|
return cmp && !strcmp (opa->name, opb->name);
|
2001-06-04 02:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opcode_t *
|
2002-06-07 17:29:30 +00:00
|
|
|
opcode_find (const char *name, def_t *var_a, def_t *var_b, def_t *var_c)
|
2001-06-04 02:41:45 +00:00
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
opcode_t op;
|
2001-06-04 02:41:45 +00:00
|
|
|
hashtab_t **tab;
|
|
|
|
|
|
|
|
op.name = name;
|
|
|
|
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;
|
2001-11-15 04:32:50 +00:00
|
|
|
tab = &opcode_type_table_abc;
|
2001-10-02 18:55:52 +00:00
|
|
|
} else if (var_a && var_b) {
|
|
|
|
op.type_a = var_a->type->type;
|
|
|
|
op.type_b = var_b->type->type;
|
2001-10-25 17:48:35 +00:00
|
|
|
tab = &opcode_type_table_ab;
|
2001-06-04 02:41:45 +00:00
|
|
|
} else {
|
2001-10-25 17:48:35 +00:00
|
|
|
tab = 0;
|
2001-06-04 02:41:45 +00:00
|
|
|
}
|
2001-10-02 18:55:52 +00:00
|
|
|
return Hash_FindElement (*tab, &op);
|
2001-06-04 02:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-06-07 17:29:30 +00:00
|
|
|
opcode_init (void)
|
2001-06-04 02:41:45 +00:00
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
opcode_t *op;
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2001-07-14 02:34:16 +00:00
|
|
|
PR_Opcode_Init ();
|
2001-10-25 17:48:35 +00:00
|
|
|
opcode_type_table_ab = Hash_NewTable (1021, 0, 0, &opcode_type_table_ab);
|
|
|
|
opcode_type_table_abc = Hash_NewTable (1021, 0, 0, &opcode_type_table_abc);
|
|
|
|
Hash_SetHashCompare (opcode_type_table_ab, get_hash, compare);
|
|
|
|
Hash_SetHashCompare (opcode_type_table_abc, get_hash, compare);
|
2001-07-14 02:34:16 +00:00
|
|
|
for (op = pr_opcodes; op->name; op++) {
|
2001-10-26 06:43:56 +00:00
|
|
|
if (op->min_version > options.code.progsversion)
|
2001-06-09 06:25:33 +00:00
|
|
|
continue;
|
2001-10-26 06:43:56 +00:00
|
|
|
if (options.code.progsversion == PROG_ID_VERSION
|
2001-08-03 07:47:15 +00:00
|
|
|
&& op->type_c == ev_integer)
|
|
|
|
op->type_c = ev_float;
|
2001-10-25 17:48:35 +00:00
|
|
|
Hash_AddElement (opcode_type_table_ab, op);
|
|
|
|
Hash_AddElement (opcode_type_table_abc, op);
|
2001-06-04 02:41:45 +00:00
|
|
|
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;
|
pr_comp.h:
o add ev_uniteger to the types enum
o add opcodes for ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
progs.h:
o add uinteger accessors
pr_exec.c:
o implement ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
pr_opcode.c:
o add opcodes for ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
expr.h:
o prototype inc_users
qfcc.h:
o add externs for op_ifbe, op_ifb, op_ifae and op_ifa
emit.c:
o don't bother emiting an assignment to a temp def that's only used once
(ie, it's never read, only written to)
o support the new if* instructions
expr.c:
o support the new if* insructions
o dectect expression loops in append_expr
o support unsigned integers
o re-work temp def usage counting
pr_def.c
o debugging for temp def usage counts
pr_opcode.c:
o support the new if* instructions
qc-parse.y:
o provide defines for IFBE IFB IFAE IFA
switch.c:
o do binary searches for strings, floats and ints if there are more than
8 cases in a switch. Strings need more testing.
2001-11-09 00:58:16 +00:00
|
|
|
} else if (!strcmp (op->name, "<IFBE>")) {
|
|
|
|
op_ifbe = op;
|
|
|
|
} else if (!strcmp (op->name, "<IFB>")) {
|
|
|
|
op_ifb = op;
|
|
|
|
} else if (!strcmp (op->name, "<IFAE>")) {
|
|
|
|
op_ifae = op;
|
|
|
|
} else if (!strcmp (op->name, "<IFA>")) {
|
|
|
|
op_ifa = op;
|
2001-06-04 02:41:45 +00:00
|
|
|
} else if (!strcmp (op->name, "<STATE>")) {
|
|
|
|
op_state = op;
|
|
|
|
} else if (!strcmp (op->name, "<GOTO>")) {
|
|
|
|
op_goto = op;
|
2001-11-13 08:58:54 +00:00
|
|
|
} else if (!strcmp (op->name, "<JUMP>")) {
|
|
|
|
op_jump = op;
|
|
|
|
} else if (!strcmp (op->name, "<JUMPB>")) {
|
|
|
|
op_jumpb = op;
|
2001-06-04 02:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|