2001-04-01 06:01:02 +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.
|
|
|
|
*/
|
2001-09-28 07:09:38 +00:00
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
2001-04-01 06:01:02 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#include "QF/hash.h"
|
|
|
|
|
|
|
|
#include "qfcc.h"
|
2002-05-09 17:11:14 +00:00
|
|
|
#include "type.h"
|
2001-04-01 06:01:02 +00:00
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
static hashtab_t *string_imm_defs;
|
|
|
|
static hashtab_t *float_imm_defs;
|
|
|
|
static hashtab_t *vector_imm_defs;
|
|
|
|
static hashtab_t *entity_imm_defs;
|
|
|
|
static hashtab_t *field_imm_defs;
|
|
|
|
static hashtab_t *func_imm_defs;
|
|
|
|
static hashtab_t *pointer_imm_defs;
|
|
|
|
static hashtab_t *quaternion_imm_defs;
|
|
|
|
static hashtab_t *integer_imm_defs;
|
2001-04-01 06:01:02 +00:00
|
|
|
|
2001-06-04 04:52:14 +00:00
|
|
|
static const char *
|
2001-04-01 06:01:02 +00:00
|
|
|
string_imm_get_key (void *_def, void *unused)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
def_t *def = (def_t *) _def;
|
|
|
|
|
2001-04-01 06:01:02 +00:00
|
|
|
return G_STRING (def->ofs);
|
|
|
|
}
|
|
|
|
|
2001-06-04 04:52:14 +00:00
|
|
|
static const char *
|
2001-04-01 06:01:02 +00:00
|
|
|
float_imm_get_key (void *_def, void *unused)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
def_t *def = (def_t *) _def;
|
2001-04-01 06:01:02 +00:00
|
|
|
static char rep[20];
|
2001-12-06 19:49:40 +00:00
|
|
|
|
|
|
|
snprintf (rep, sizeof (rep), "\001float:%08X\001", G_INT (def->ofs));
|
2001-04-01 06:01:02 +00:00
|
|
|
return rep;
|
|
|
|
}
|
|
|
|
|
2001-06-04 04:52:14 +00:00
|
|
|
static const char *
|
2001-04-01 06:01:02 +00:00
|
|
|
vector_imm_get_key (void *_def, void *unused)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
def_t *def = (def_t *) _def;
|
2001-04-01 06:01:02 +00:00
|
|
|
static char rep[60];
|
2001-12-06 19:49:40 +00:00
|
|
|
|
2001-11-15 03:54:35 +00:00
|
|
|
snprintf (rep, sizeof (rep), "\001vector:%08X\001%08X\001%08X\001",
|
2001-12-06 19:49:40 +00:00
|
|
|
G_INT (def->ofs), G_INT (def->ofs + 1), G_INT (def->ofs + 2));
|
2001-04-01 06:01:02 +00:00
|
|
|
return rep;
|
|
|
|
}
|
|
|
|
|
2001-06-26 07:21:20 +00:00
|
|
|
static const char *
|
|
|
|
quaternion_imm_get_key (void *_def, void *unused)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
def_t *def = (def_t *) _def;
|
2001-06-26 07:21:20 +00:00
|
|
|
static char rep[60];
|
2001-12-06 19:49:40 +00:00
|
|
|
|
2001-11-15 03:54:35 +00:00
|
|
|
snprintf (rep, sizeof (rep),
|
2001-12-06 19:49:40 +00:00
|
|
|
"\001quaternion:%08X\001%08X\001%08X\001%08X\001",
|
|
|
|
G_INT (def->ofs), G_INT (def->ofs + 1),
|
|
|
|
G_INT (def->ofs + 2), G_INT (def->ofs + 3));
|
2001-06-26 07:21:20 +00:00
|
|
|
return rep;
|
|
|
|
}
|
|
|
|
|
2001-07-18 06:37:14 +00:00
|
|
|
static const char *
|
|
|
|
int_imm_get_key (void *_def, void *_str)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
def_t *def = (def_t *) _def;
|
2001-07-18 06:37:14 +00:00
|
|
|
static char rep[60];
|
2001-12-06 19:49:40 +00:00
|
|
|
char *str = (char *) _str;
|
|
|
|
|
|
|
|
snprintf (rep, sizeof (rep), "\001%s:%08X\001", str, G_INT (def->ofs));
|
2001-07-18 06:37:14 +00:00
|
|
|
return rep;
|
|
|
|
}
|
|
|
|
|
2001-06-26 07:21:20 +00:00
|
|
|
def_t *
|
2001-06-26 22:07:09 +00:00
|
|
|
PR_ReuseConstant (expr_t *expr, def_t *def)
|
2001-06-26 07:21:20 +00:00
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
def_t *cn = 0;
|
|
|
|
char rep[60];
|
2001-11-13 08:58:54 +00:00
|
|
|
const char *r = rep;
|
2001-12-06 19:49:40 +00:00
|
|
|
hashtab_t *tab = 0;
|
|
|
|
type_t *type;
|
|
|
|
expr_t e = *expr;
|
2001-06-26 07:21:20 +00:00
|
|
|
|
|
|
|
if (!string_imm_defs) {
|
|
|
|
string_imm_defs = Hash_NewTable (16381, string_imm_get_key, 0, 0);
|
|
|
|
float_imm_defs = Hash_NewTable (16381, float_imm_get_key, 0, 0);
|
|
|
|
vector_imm_defs = Hash_NewTable (16381, vector_imm_get_key, 0, 0);
|
2001-07-18 06:37:14 +00:00
|
|
|
entity_imm_defs = Hash_NewTable (16381, int_imm_get_key, 0, "entity");
|
|
|
|
field_imm_defs = Hash_NewTable (16381, int_imm_get_key, 0, "field");
|
|
|
|
func_imm_defs = Hash_NewTable (16381, int_imm_get_key, 0, "func");
|
|
|
|
pointer_imm_defs = Hash_NewTable (16381, int_imm_get_key, 0, "pointer");
|
2001-12-06 19:49:40 +00:00
|
|
|
quaternion_imm_defs =
|
|
|
|
Hash_NewTable (16381, quaternion_imm_get_key, 0, 0);
|
2001-07-23 01:31:22 +00:00
|
|
|
integer_imm_defs = Hash_NewTable (16381, int_imm_get_key, 0, "integer");
|
2001-07-24 22:30:31 +00:00
|
|
|
|
|
|
|
Hash_Add (string_imm_defs, PR_NewDef (&type_string, ".imm", 0));
|
|
|
|
Hash_Add (float_imm_defs, PR_NewDef (&type_float, ".imm", 0));
|
|
|
|
Hash_Add (entity_imm_defs, PR_NewDef (&type_entity, ".imm", 0));
|
|
|
|
Hash_Add (pointer_imm_defs, PR_NewDef (&type_pointer, ".imm", 0));
|
|
|
|
Hash_Add (integer_imm_defs, PR_NewDef (&type_integer, ".imm", 0));
|
2001-06-26 07:21:20 +00:00
|
|
|
}
|
2001-06-26 22:07:09 +00:00
|
|
|
switch (e.type) {
|
2001-07-18 06:37:14 +00:00
|
|
|
case ex_entity:
|
2001-12-06 19:49:40 +00:00
|
|
|
snprintf (rep, sizeof (rep), "\001entity:%08X\001",
|
|
|
|
e.e.integer_val);
|
2001-07-24 23:53:35 +00:00
|
|
|
tab = entity_imm_defs;
|
2001-07-18 06:37:14 +00:00
|
|
|
type = &type_entity;
|
|
|
|
break;
|
|
|
|
case ex_field:
|
2001-11-15 03:54:35 +00:00
|
|
|
snprintf (rep, sizeof (rep), "\001field:%08X\001", e.e.integer_val);
|
2001-07-24 23:53:35 +00:00
|
|
|
tab = field_imm_defs;
|
2001-07-18 06:37:14 +00:00
|
|
|
type = &type_field;
|
|
|
|
break;
|
|
|
|
case ex_func:
|
2001-11-15 03:54:35 +00:00
|
|
|
snprintf (rep, sizeof (rep), "\001func:%08X\001", e.e.integer_val);
|
2001-07-24 23:53:35 +00:00
|
|
|
tab = func_imm_defs;
|
2001-07-18 06:37:14 +00:00
|
|
|
type = &type_function;
|
|
|
|
break;
|
|
|
|
case ex_pointer:
|
2001-12-06 19:49:40 +00:00
|
|
|
snprintf (rep, sizeof (rep), "\001pointer:%08X\001",
|
2001-12-12 08:39:47 +00:00
|
|
|
e.e.pointer.val);
|
2001-07-18 06:37:14 +00:00
|
|
|
tab = pointer_imm_defs;
|
|
|
|
type = &type_pointer;
|
|
|
|
break;
|
2001-07-22 06:59:12 +00:00
|
|
|
case ex_integer:
|
2001-11-13 08:58:54 +00:00
|
|
|
case ex_uinteger:
|
2001-07-23 01:31:22 +00:00
|
|
|
if (!def || def->type != &type_float) {
|
2001-12-06 19:49:40 +00:00
|
|
|
snprintf (rep, sizeof (rep), "\001integer:%08X\001",
|
|
|
|
e.e.integer_val);
|
2001-07-23 01:31:22 +00:00
|
|
|
tab = integer_imm_defs;
|
2001-11-13 18:11:19 +00:00
|
|
|
if (e.type == ex_uinteger)
|
|
|
|
type = &type_uinteger;
|
|
|
|
else
|
|
|
|
type = &type_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-11-13 08:58:54 +00:00
|
|
|
if (e.type == ex_uinteger)
|
|
|
|
e.e.float_val = e.e.uinteger_val;
|
|
|
|
else
|
|
|
|
e.e.float_val = e.e.integer_val;
|
2001-06-26 07:21:20 +00:00
|
|
|
case ex_float:
|
2001-11-15 03:54:35 +00:00
|
|
|
snprintf (rep, sizeof (rep), "\001float:%08X\001", e.e.integer_val);
|
2001-06-26 07:21:20 +00:00
|
|
|
tab = float_imm_defs;
|
|
|
|
type = &type_float;
|
|
|
|
break;
|
|
|
|
case ex_string:
|
2001-07-24 22:30:31 +00:00
|
|
|
r = e.e.string_val ? e.e.string_val : "";
|
2001-06-26 07:21:20 +00:00
|
|
|
tab = string_imm_defs;
|
|
|
|
type = &type_string;
|
|
|
|
break;
|
|
|
|
case ex_vector:
|
2001-11-15 03:54:35 +00:00
|
|
|
snprintf (rep, sizeof (rep), "\001vector:%08X\001%08X\001%08X\001",
|
2001-12-06 19:49:40 +00:00
|
|
|
*(int *) &e.e.vector_val[0],
|
|
|
|
*(int *) &e.e.vector_val[1], *(int *) &e.e.vector_val[2]);
|
2001-06-26 07:21:20 +00:00
|
|
|
tab = vector_imm_defs;
|
|
|
|
type = &type_vector;
|
|
|
|
break;
|
|
|
|
case ex_quaternion:
|
2001-12-06 19:49:40 +00:00
|
|
|
snprintf (rep, sizeof (rep),
|
|
|
|
"\001quaternion:%08X\001%08X\001%08X\001%08X\001",
|
|
|
|
*(int *) &e.e.quaternion_val[0],
|
|
|
|
*(int *) &e.e.quaternion_val[1],
|
|
|
|
*(int *) &e.e.quaternion_val[2],
|
|
|
|
*(int *) &e.e.quaternion_val[3]);
|
2001-06-26 07:21:20 +00:00
|
|
|
tab = vector_imm_defs;
|
|
|
|
type = &type_quaternion;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort ();
|
|
|
|
}
|
2001-12-06 19:49:40 +00:00
|
|
|
cn = (def_t *) Hash_Find (tab, r);
|
2001-06-26 07:21:20 +00:00
|
|
|
if (cn) {
|
|
|
|
if (def) {
|
|
|
|
PR_FreeLocation (def);
|
|
|
|
def->ofs = cn->ofs;
|
2001-10-18 17:41:22 +00:00
|
|
|
def->initialized = def->constant = 1;
|
2001-06-26 07:21:20 +00:00
|
|
|
cn = def;
|
2001-11-14 05:08:37 +00:00
|
|
|
} else {
|
|
|
|
if (cn->type != type) {
|
|
|
|
def = PR_NewDef (type, ".imm", 0);
|
|
|
|
def->ofs = cn->ofs;
|
|
|
|
cn = def;
|
|
|
|
}
|
2001-06-26 07:21:20 +00:00
|
|
|
}
|
|
|
|
return cn;
|
|
|
|
}
|
|
|
|
// allocate a new one
|
|
|
|
// always share immediates
|
|
|
|
if (def) {
|
2001-11-13 18:11:19 +00:00
|
|
|
if (def->type != type) {
|
|
|
|
cn = PR_NewDef (type, ".imm", 0);
|
|
|
|
cn->ofs = def->ofs;
|
|
|
|
} else {
|
|
|
|
cn = def;
|
|
|
|
}
|
2001-06-26 07:21:20 +00:00
|
|
|
} else {
|
2001-07-15 01:50:10 +00:00
|
|
|
cn = PR_NewDef (type, ".imm", 0);
|
2001-06-26 17:34:21 +00:00
|
|
|
cn->ofs = PR_NewLocation (type);
|
2001-06-26 07:21:20 +00:00
|
|
|
pr_global_defs[cn->ofs] = cn;
|
2001-06-27 22:24:28 +00:00
|
|
|
if (type == &type_vector || type == &type_quaternion) {
|
2001-12-06 19:49:40 +00:00
|
|
|
int i;
|
2001-06-27 22:24:28 +00:00
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
for (i = 0; i < 3 + (type == &type_quaternion); i++)
|
2001-07-15 01:50:10 +00:00
|
|
|
PR_NewDef (&type_float, ".imm", 0);
|
2001-06-27 22:24:28 +00:00
|
|
|
}
|
2001-06-26 07:21:20 +00:00
|
|
|
}
|
2001-10-18 17:41:22 +00:00
|
|
|
cn->initialized = cn->constant = 1;
|
2001-06-26 07:21:20 +00:00
|
|
|
// copy the immediate to the global area
|
2001-06-26 22:07:09 +00:00
|
|
|
if (e.type == ex_string)
|
2001-07-24 22:30:31 +00:00
|
|
|
e.e.integer_val = CopyString (r);
|
2001-06-26 07:21:20 +00:00
|
|
|
|
2001-11-12 23:56:46 +00:00
|
|
|
memcpy (pr_globals + cn->ofs, &e.e, 4 * pr_type_size[type->type]);
|
2001-06-26 07:21:20 +00:00
|
|
|
|
|
|
|
Hash_Add (tab, cn);
|
|
|
|
|
|
|
|
return cn;
|
|
|
|
}
|