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
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
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 "opcodes.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "options.h"
|
2002-06-07 17:29:30 +00:00
|
|
|
#include "qfcc.h"
|
2012-11-19 03:28:41 +00:00
|
|
|
#include "statements.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "type.h"
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2011-01-25 06:46:48 +00:00
|
|
|
hashtab_t *opcode_type_table;
|
2011-01-27 05:47:33 +00:00
|
|
|
hashtab_t *opcode_void_table;
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2011-01-26 23:58:50 +00:00
|
|
|
#define ROTL(x,n) ((((unsigned)(x))<<(n))|((unsigned)(x))>>(32-n))
|
2001-08-16 02:51:53 +00:00
|
|
|
|
2007-04-04 11:22:48 +00:00
|
|
|
static uintptr_t
|
2012-07-18 13:34:37 +00:00
|
|
|
get_hash (const 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;
|
2007-04-04 11:22:48 +00:00
|
|
|
uintptr_t hash;
|
2001-08-16 02:51:53 +00:00
|
|
|
|
2011-01-25 06:46:48 +00:00
|
|
|
hash = ROTL (~op->type_a, 8) + ROTL (~op->type_b, 16)
|
|
|
|
+ ROTL (~op->type_c, 24);
|
2002-11-20 21:44:04 +00:00
|
|
|
return hash + Hash_String (op->name);
|
2001-08-16 02:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-07-18 13:34:37 +00:00
|
|
|
compare (const void *_opa, const void *_opb, void *unused)
|
2001-08-16 02:51:53 +00:00
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
opcode_t *opa = (opcode_t *) _opa;
|
|
|
|
opcode_t *opb = (opcode_t *) _opb;
|
|
|
|
int cmp;
|
2001-06-04 02:41:45 +00:00
|
|
|
|
2011-01-25 06:46:48 +00:00
|
|
|
cmp = (opa->type_a == opb->type_a)
|
|
|
|
&& (opa->type_b == opb->type_b)
|
|
|
|
&& (opa->type_c == opb->type_c);
|
2001-08-16 02:51:53 +00:00
|
|
|
return cmp && !strcmp (opa->name, opb->name);
|
2001-06-04 02:41:45 +00:00
|
|
|
}
|
|
|
|
|
2011-01-27 05:47:33 +00:00
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
get_key (const void *op, void *unused)
|
2011-01-27 05:47:33 +00:00
|
|
|
{
|
|
|
|
return ((opcode_t *) op)->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_operand_type (etype_t ot1, etype_t ot2)
|
|
|
|
{
|
|
|
|
if ((ot1 == ev_void && ot2 != ev_invalid)
|
|
|
|
|| ot1 == ot2)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-06-04 02:41:45 +00:00
|
|
|
opcode_t *
|
2012-11-19 03:28:41 +00:00
|
|
|
opcode_find (const char *name, operand_t *op_a, operand_t *op_b,
|
|
|
|
operand_t *op_c)
|
2001-06-04 02:41:45 +00:00
|
|
|
{
|
2011-01-27 05:47:33 +00:00
|
|
|
opcode_t search_op;
|
|
|
|
opcode_t *op;
|
|
|
|
opcode_t *sop;
|
|
|
|
void **op_list;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
search_op.name = name;
|
2019-06-17 14:38:34 +00:00
|
|
|
search_op.type_a = op_a ? low_level_type (op_a->type) : ev_invalid;
|
|
|
|
search_op.type_b = op_b ? low_level_type (op_b->type) : ev_invalid;
|
|
|
|
search_op.type_c = op_c ? low_level_type (op_c->type) : ev_invalid;
|
2011-01-27 05:47:33 +00:00
|
|
|
op = Hash_FindElement (opcode_type_table, &search_op);
|
|
|
|
if (op)
|
|
|
|
return op;
|
|
|
|
op_list = Hash_FindList (opcode_void_table, name);
|
|
|
|
if (!op_list)
|
|
|
|
return op;
|
|
|
|
for (i = 0; !op && op_list[i]; i++) {
|
|
|
|
sop = op_list[i];
|
|
|
|
if (check_operand_type (sop->type_a, search_op.type_a)
|
|
|
|
&& check_operand_type (sop->type_b, search_op.type_b)
|
|
|
|
&& check_operand_type (sop->type_c, search_op.type_c))
|
|
|
|
op = sop;
|
|
|
|
}
|
|
|
|
free (op_list);
|
|
|
|
return op;
|
2001-06-04 02:41:45 +00:00
|
|
|
}
|
|
|
|
|
2012-11-22 11:42:00 +00:00
|
|
|
static void
|
|
|
|
opcode_free (void *_op, void *unused)
|
|
|
|
{
|
|
|
|
free (_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
|
|
|
{
|
2020-02-25 16:20:28 +00:00
|
|
|
const opcode_t *op;
|
|
|
|
opcode_t *mop;
|
2012-11-22 11:42:00 +00:00
|
|
|
|
|
|
|
if (opcode_type_table) {
|
|
|
|
Hash_FlushTable (opcode_void_table);
|
|
|
|
Hash_FlushTable (opcode_type_table);
|
|
|
|
} else {
|
|
|
|
PR_Opcode_Init ();
|
|
|
|
opcode_type_table = Hash_NewTable (1021, 0, opcode_free, 0);
|
|
|
|
Hash_SetHashCompare (opcode_type_table, get_hash, compare);
|
|
|
|
opcode_void_table = Hash_NewTable (1021, get_key, 0, 0);
|
|
|
|
}
|
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;
|
2012-11-22 11:42:00 +00:00
|
|
|
mop = malloc (sizeof (opcode_t));
|
|
|
|
*mop = *op;
|
2011-01-27 00:00:11 +00:00
|
|
|
if (options.code.progsversion == PROG_ID_VERSION) {
|
|
|
|
// v6 progs have no concept of integer, but the QF engine
|
|
|
|
// treats the operands of certain operands as integers
|
|
|
|
// irrespective the progs version, so convert the engine's
|
|
|
|
// view of the operands to the prog's view.
|
2012-11-22 11:42:00 +00:00
|
|
|
if (mop->type_a == ev_integer)
|
|
|
|
mop->type_a = ev_float;
|
|
|
|
if (mop->type_b == ev_integer)
|
|
|
|
mop->type_b = ev_float;
|
|
|
|
if (mop->type_c == ev_integer)
|
|
|
|
mop->type_c = ev_float;
|
2011-01-27 00:00:11 +00:00
|
|
|
}
|
2012-11-22 11:42:00 +00:00
|
|
|
Hash_AddElement (opcode_type_table, mop);
|
|
|
|
if (mop->type_a == ev_void || mop->type_b == ev_void
|
|
|
|
|| mop->type_c == ev_void)
|
|
|
|
Hash_Add (opcode_void_table, mop);
|
2001-06-04 02:41:45 +00:00
|
|
|
}
|
|
|
|
}
|