2002-10-22 14:53:18 +00:00
|
|
|
/*
|
|
|
|
immediate.c
|
2001-04-01 06:01:02 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
shared immediate value handling
|
2001-04-01 06:01:02 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
|
2001-04-01 06:01:02 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2002/06/04
|
|
|
|
|
|
|
|
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-04-01 06:01:02 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
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>
|
|
|
|
|
2002-06-02 01:10:31 +00:00
|
|
|
#include "QF/dstring.h"
|
2001-04-01 06:01:02 +00:00
|
|
|
#include "QF/hash.h"
|
2002-10-20 03:31:49 +00:00
|
|
|
#include "QF/mathlib.h"
|
2002-06-01 05:06:19 +00:00
|
|
|
#include "QF/va.h"
|
2001-04-01 06:01:02 +00:00
|
|
|
|
|
|
|
#include "qfcc.h"
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "def.h"
|
2002-07-14 03:41:13 +00:00
|
|
|
#include "emit.h"
|
2002-06-01 05:30:16 +00:00
|
|
|
#include "expr.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "immediate.h"
|
2002-07-11 22:38:23 +00:00
|
|
|
#include "reloc.h"
|
2002-07-05 20:02:10 +00:00
|
|
|
#include "strpool.h"
|
2002-05-09 17:11:14 +00:00
|
|
|
#include "type.h"
|
2001-04-01 06:01:02 +00:00
|
|
|
|
2002-10-20 03:31:49 +00:00
|
|
|
typedef struct {
|
|
|
|
def_t *def;
|
|
|
|
union {
|
|
|
|
const char *string_val;
|
|
|
|
float float_val;
|
|
|
|
float vector_val[3];
|
|
|
|
int entity_val;
|
|
|
|
int field_val;
|
|
|
|
int func_val;
|
|
|
|
ex_pointer_t pointer;
|
|
|
|
float quaternion_val[4];
|
|
|
|
int integer_val;
|
|
|
|
} i;
|
|
|
|
} immediate_t;
|
|
|
|
|
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
|
|
|
|
2002-10-20 03:31:49 +00:00
|
|
|
static unsigned long
|
|
|
|
imm_get_hash (void *_imm, void *_tab)
|
2001-04-01 06:01:02 +00:00
|
|
|
{
|
2002-10-20 03:31:49 +00:00
|
|
|
immediate_t *imm = (immediate_t *) _imm;
|
|
|
|
hashtab_t **tab = (hashtab_t **) _tab;
|
2001-12-06 19:49:40 +00:00
|
|
|
|
2002-10-20 03:31:49 +00:00
|
|
|
if (tab == &string_imm_defs) {
|
|
|
|
return imm->i.string_val ? Hash_String (imm->i.string_val) : 0;
|
|
|
|
} else if (tab == &float_imm_defs) {
|
|
|
|
return imm->i.integer_val;
|
|
|
|
} else if (tab == &vector_imm_defs) {
|
|
|
|
return Hash_Buffer (&imm->i.vector_val, sizeof (&imm->i.vector_val));
|
|
|
|
} else if (tab == &entity_imm_defs) {
|
|
|
|
return imm->i.integer_val;
|
|
|
|
} else if (tab == &field_imm_defs) {
|
|
|
|
return imm->i.integer_val;
|
|
|
|
} else if (tab == &func_imm_defs) {
|
|
|
|
return imm->i.integer_val;
|
|
|
|
} else if (tab == &pointer_imm_defs) {
|
|
|
|
return Hash_Buffer (&imm->i.pointer, sizeof (&imm->i.pointer));
|
|
|
|
} else if (tab == &quaternion_imm_defs) {
|
|
|
|
return Hash_Buffer (&imm->i.quaternion_val,
|
|
|
|
sizeof (&imm->i.quaternion_val));
|
|
|
|
} else if (tab == &integer_imm_defs) {
|
|
|
|
return imm->i.integer_val;
|
|
|
|
} else {
|
|
|
|
abort ();
|
|
|
|
}
|
2001-06-26 07:21:20 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 03:31:49 +00:00
|
|
|
static int
|
|
|
|
imm_compare (void *_imm1, void *_imm2, void *_tab)
|
2001-07-18 06:37:14 +00:00
|
|
|
{
|
2002-10-20 03:31:49 +00:00
|
|
|
immediate_t *imm1 = (immediate_t *) _imm1;
|
|
|
|
immediate_t *imm2 = (immediate_t *) _imm2;
|
|
|
|
hashtab_t **tab = (hashtab_t **) _tab;
|
2001-12-06 19:49:40 +00:00
|
|
|
|
2002-10-20 03:31:49 +00:00
|
|
|
if (tab == &string_imm_defs) {
|
2002-10-20 03:43:14 +00:00
|
|
|
return (imm1->i.string_val == imm2->i.string_val
|
|
|
|
|| (imm1->i.string_val && imm2->i.string_val
|
|
|
|
&& !strcmp (imm1->i.string_val, imm2->i.string_val)));
|
2002-10-20 03:31:49 +00:00
|
|
|
} else if (tab == &float_imm_defs) {
|
|
|
|
return imm1->i.float_val == imm2->i.float_val;
|
|
|
|
} else if (tab == &vector_imm_defs) {
|
|
|
|
return VectorCompare (imm1->i.vector_val, imm2->i.vector_val);
|
|
|
|
} else if (tab == &entity_imm_defs) {
|
|
|
|
return imm1->i.entity_val == imm2->i.entity_val;
|
|
|
|
} else if (tab == &field_imm_defs) {
|
|
|
|
return imm1->i.field_val == imm2->i.field_val;
|
|
|
|
} else if (tab == &func_imm_defs) {
|
|
|
|
return imm1->i.func_val == imm2->i.func_val;
|
|
|
|
} else if (tab == &pointer_imm_defs) {
|
|
|
|
return !memcmp (&imm1->i.pointer, &imm2->i.pointer,
|
|
|
|
sizeof (imm1->i.pointer));
|
|
|
|
} else if (tab == &quaternion_imm_defs) {
|
|
|
|
return (VectorCompare (imm1->i.quaternion_val,
|
|
|
|
imm2->i.quaternion_val)
|
|
|
|
&& imm1->i.quaternion_val[3] == imm2->i.quaternion_val[3]);
|
|
|
|
} else if (tab == &integer_imm_defs) {
|
|
|
|
return imm1->i.integer_val == imm2->i.integer_val;
|
|
|
|
} else {
|
|
|
|
abort ();
|
|
|
|
}
|
2001-07-18 06:37:14 +00:00
|
|
|
}
|
|
|
|
|
2002-06-04 18:44:03 +00:00
|
|
|
int
|
|
|
|
ReuseString (const char *str)
|
|
|
|
{
|
2002-07-05 20:02:10 +00:00
|
|
|
return strpool_addstr (pr.strings, str);
|
2002-06-04 18:44:03 +00:00
|
|
|
}
|
|
|
|
|
2001-06-26 07:21:20 +00:00
|
|
|
def_t *
|
2002-06-04 18:44:03 +00:00
|
|
|
ReuseConstant (expr_t *expr, def_t *def)
|
2001-06-26 07:21:20 +00:00
|
|
|
{
|
2002-05-16 21:57:03 +00:00
|
|
|
def_t *cn;
|
2001-12-06 19:49:40 +00:00
|
|
|
hashtab_t *tab = 0;
|
|
|
|
type_t *type;
|
|
|
|
expr_t e = *expr;
|
2002-07-11 22:38:23 +00:00
|
|
|
reloc_t *reloc = 0;
|
2002-10-20 03:31:49 +00:00
|
|
|
immediate_t *imm, search;
|
2001-06-26 07:21:20 +00:00
|
|
|
|
|
|
|
if (!string_imm_defs) {
|
2002-06-28 16:00:01 +00:00
|
|
|
clear_immediates ();
|
2001-06-26 07:21:20 +00:00
|
|
|
}
|
2002-05-16 21:57:03 +00:00
|
|
|
cn = 0;
|
2002-10-20 03:31:49 +00:00
|
|
|
memcpy (&search.i, &e.e, sizeof (search.i));
|
2001-06-26 22:07:09 +00:00
|
|
|
switch (e.type) {
|
2001-07-18 06:37:14 +00:00
|
|
|
case ex_entity:
|
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-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-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:
|
|
|
|
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) {
|
|
|
|
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:
|
|
|
|
tab = float_imm_defs;
|
|
|
|
type = &type_float;
|
|
|
|
break;
|
|
|
|
case ex_string:
|
|
|
|
tab = string_imm_defs;
|
|
|
|
type = &type_string;
|
|
|
|
break;
|
|
|
|
case ex_vector:
|
|
|
|
tab = vector_imm_defs;
|
|
|
|
type = &type_vector;
|
|
|
|
break;
|
|
|
|
case ex_quaternion:
|
|
|
|
tab = vector_imm_defs;
|
|
|
|
type = &type_quaternion;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort ();
|
|
|
|
}
|
2002-10-20 03:31:49 +00:00
|
|
|
imm = (immediate_t *) Hash_FindElement (tab, &search);
|
|
|
|
if (imm) {
|
|
|
|
cn = imm->def;
|
2001-06-26 07:21:20 +00:00
|
|
|
if (def) {
|
2002-06-09 04:30:02 +00:00
|
|
|
free_location (def);
|
2001-06-26 07:21:20 +00:00
|
|
|
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) {
|
2002-06-09 04:30:02 +00:00
|
|
|
def = new_def (type, ".imm", pr.scope);
|
2001-11-14 05:08:37 +00:00
|
|
|
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) {
|
2002-06-09 04:30:02 +00:00
|
|
|
cn = new_def (type, ".imm", pr.scope);
|
2001-11-13 18:11:19 +00:00
|
|
|
cn->ofs = def->ofs;
|
|
|
|
} else {
|
|
|
|
cn = def;
|
|
|
|
}
|
2001-06-26 07:21:20 +00:00
|
|
|
} else {
|
2002-06-09 04:30:02 +00:00
|
|
|
cn = new_def (type, ".imm", pr.scope);
|
2002-06-10 20:54:22 +00:00
|
|
|
cn->ofs = new_location (type, pr.near_data);
|
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++)
|
2002-06-09 04:30:02 +00:00
|
|
|
new_def (&type_float, ".imm", pr.scope);
|
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
|
2002-07-11 22:38:23 +00:00
|
|
|
switch (e.type) {
|
|
|
|
case ex_string:
|
2002-10-20 03:31:49 +00:00
|
|
|
e.e.integer_val = ReuseString (e.e.string_val);
|
2002-07-11 22:38:23 +00:00
|
|
|
reloc = new_reloc (cn->ofs, rel_def_string);
|
|
|
|
break;
|
|
|
|
case ex_func:
|
|
|
|
reloc = new_reloc (cn->ofs, rel_def_func);
|
|
|
|
break;
|
2002-07-14 03:41:13 +00:00
|
|
|
case ex_pointer:
|
|
|
|
if (e.e.pointer.def)
|
|
|
|
EMIT_DEF (G_INT (cn->ofs), e.e.pointer.def);
|
|
|
|
break;
|
2002-07-11 22:38:23 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (reloc) {
|
|
|
|
reloc->next = pr.relocs;
|
|
|
|
pr.relocs = reloc;
|
|
|
|
}
|
2001-06-26 07:21:20 +00:00
|
|
|
|
2002-06-10 20:54:22 +00:00
|
|
|
memcpy (G_POINTER (void, cn->ofs), &e.e, 4 * type_size (type));
|
2001-06-26 07:21:20 +00:00
|
|
|
|
2002-10-20 03:31:49 +00:00
|
|
|
imm = malloc (sizeof (immediate_t));
|
|
|
|
imm->def = cn;
|
|
|
|
memcpy (&imm->i, &expr->e, sizeof (imm->i));
|
|
|
|
|
|
|
|
Hash_AddElement (tab, imm);
|
2001-06-26 07:21:20 +00:00
|
|
|
|
|
|
|
return cn;
|
|
|
|
}
|
2002-06-28 16:00:01 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
clear_immediates (void)
|
|
|
|
{
|
2002-10-20 03:31:49 +00:00
|
|
|
immediate_t *imm;
|
|
|
|
|
2002-06-28 16:00:01 +00:00
|
|
|
if (string_imm_defs) {
|
|
|
|
Hash_FlushTable (string_imm_defs);
|
|
|
|
Hash_FlushTable (float_imm_defs);
|
|
|
|
Hash_FlushTable (vector_imm_defs);
|
|
|
|
Hash_FlushTable (entity_imm_defs);
|
|
|
|
Hash_FlushTable (field_imm_defs);
|
|
|
|
Hash_FlushTable (func_imm_defs);
|
|
|
|
Hash_FlushTable (pointer_imm_defs);
|
|
|
|
Hash_FlushTable (quaternion_imm_defs);
|
2002-10-20 03:31:49 +00:00
|
|
|
Hash_FlushTable (integer_imm_defs);
|
2002-06-28 16:00:01 +00:00
|
|
|
} else {
|
2002-10-20 03:31:49 +00:00
|
|
|
string_imm_defs = Hash_NewTable (16381, 0, 0, &string_imm_defs);
|
|
|
|
Hash_SetHashCompare (string_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
|
|
|
float_imm_defs = Hash_NewTable (16381, 0, 0, &float_imm_defs);
|
|
|
|
Hash_SetHashCompare (float_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
|
|
|
vector_imm_defs = Hash_NewTable (16381, 0, 0, &vector_imm_defs);
|
|
|
|
Hash_SetHashCompare (vector_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
|
|
|
entity_imm_defs = Hash_NewTable (16381, 0, 0, &entity_imm_defs);
|
|
|
|
Hash_SetHashCompare (entity_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
|
|
|
field_imm_defs = Hash_NewTable (16381, 0, 0, &field_imm_defs);
|
|
|
|
Hash_SetHashCompare (field_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
|
|
|
func_imm_defs = Hash_NewTable (16381, 0, 0, &func_imm_defs);
|
|
|
|
Hash_SetHashCompare (func_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
|
|
|
pointer_imm_defs = Hash_NewTable (16381, 0, 0, &pointer_imm_defs);
|
|
|
|
Hash_SetHashCompare (pointer_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
2002-06-28 16:00:01 +00:00
|
|
|
quaternion_imm_defs =
|
2002-10-20 03:31:49 +00:00
|
|
|
Hash_NewTable (16381, 0, 0, &quaternion_imm_defs);
|
|
|
|
Hash_SetHashCompare (quaternion_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
|
|
|
integer_imm_defs = Hash_NewTable (16381, 0, 0, &integer_imm_defs);
|
|
|
|
Hash_SetHashCompare (integer_imm_defs, imm_get_hash, imm_compare);
|
2002-06-28 16:00:01 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 03:31:49 +00:00
|
|
|
imm = calloc (1, sizeof (immediate_t));
|
|
|
|
imm->def = new_def (&type_void, ".imm", pr.scope);
|
|
|
|
imm->def->initialized = imm->def->constant = 1;
|
|
|
|
|
|
|
|
Hash_AddElement (string_imm_defs, imm);
|
|
|
|
Hash_AddElement (float_imm_defs, imm);
|
|
|
|
Hash_AddElement (entity_imm_defs, imm);
|
|
|
|
Hash_AddElement (pointer_imm_defs, imm);
|
|
|
|
Hash_AddElement (integer_imm_defs, imm);
|
2002-06-28 16:00:01 +00:00
|
|
|
}
|