2002-10-22 14:53:18 +00:00
|
|
|
/*
|
2011-03-22 03:24:39 +00:00
|
|
|
value.c
|
2001-04-01 06:01:02 +00:00
|
|
|
|
2011-03-22 03:24:39 +00:00
|
|
|
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
|
|
|
|
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>
|
|
|
|
|
2012-12-06 11:52:53 +00:00
|
|
|
#include "QF/alloc.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"
|
2011-01-17 13:33:33 +00:00
|
|
|
#include "defspace.h"
|
2011-03-22 03:24:39 +00:00
|
|
|
#include "diagnostic.h"
|
2002-07-14 03:41:13 +00:00
|
|
|
#include "emit.h"
|
2002-06-01 05:30:16 +00:00
|
|
|
#include "expr.h"
|
2002-07-11 22:38:23 +00:00
|
|
|
#include "reloc.h"
|
2002-07-05 20:02:10 +00:00
|
|
|
#include "strpool.h"
|
2011-01-25 00:08:11 +00:00
|
|
|
#include "symtab.h"
|
2002-05-09 17:11:14 +00:00
|
|
|
#include "type.h"
|
2011-03-22 03:24:39 +00:00
|
|
|
#include "value.h"
|
2001-04-01 06:01:02 +00:00
|
|
|
|
2002-10-20 03:31:49 +00:00
|
|
|
typedef struct {
|
|
|
|
def_t *def;
|
|
|
|
union {
|
2003-03-03 21:09:41 +00:00
|
|
|
string_t string_val;
|
2002-10-20 03:31:49 +00:00
|
|
|
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;
|
|
|
|
|
2012-07-18 14:01:09 +00:00
|
|
|
static hashtab_t *value_table;
|
2013-03-08 13:16:31 +00:00
|
|
|
static ex_value_t *values_freelist;
|
2012-07-18 14:01:09 +00:00
|
|
|
|
|
|
|
static uintptr_t
|
|
|
|
value_get_hash (const void *_val, void *unused)
|
|
|
|
{
|
|
|
|
const ex_value_t *val = (const ex_value_t *) _val;
|
|
|
|
return Hash_Buffer (&val->v, sizeof (val->v)) + val->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
value_compare (const void *_val1, const void *_val2, void *unused)
|
|
|
|
{
|
|
|
|
const ex_value_t *val1 = (const ex_value_t *) _val1;
|
|
|
|
const ex_value_t *val2 = (const ex_value_t *) _val2;
|
|
|
|
if (val1->type != val2->type)
|
|
|
|
return 0;
|
|
|
|
return memcmp (&val1->v, &val2->v, sizeof (val1->v)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ex_value_t *
|
|
|
|
new_value (void)
|
|
|
|
{
|
|
|
|
ex_value_t *value;
|
|
|
|
ALLOC (256, ex_value_t, values, value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ex_value_t *
|
|
|
|
find_value (const ex_value_t *val)
|
|
|
|
{
|
|
|
|
ex_value_t *value;
|
|
|
|
|
|
|
|
value = Hash_FindElement (value_table, val);
|
|
|
|
if (value)
|
|
|
|
return value;
|
|
|
|
value = new_value ();
|
|
|
|
*value = *val;
|
2012-11-20 04:33:57 +00:00
|
|
|
Hash_AddElement (value_table, value);
|
2012-07-18 14:01:09 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
ex_value_t *
|
|
|
|
new_string_val (const char *string_val)
|
|
|
|
{
|
|
|
|
ex_value_t val;
|
2012-07-19 01:10:30 +00:00
|
|
|
memset (&val, 0, sizeof (val));
|
2012-07-18 14:01:09 +00:00
|
|
|
val.type = ev_string;
|
|
|
|
if (string_val)
|
|
|
|
val.v.string_val = save_string (string_val);
|
|
|
|
return find_value (&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
ex_value_t *
|
|
|
|
new_float_val (float float_val)
|
|
|
|
{
|
|
|
|
ex_value_t val;
|
2012-07-19 01:10:30 +00:00
|
|
|
memset (&val, 0, sizeof (val));
|
2012-07-18 14:01:09 +00:00
|
|
|
val.type = ev_float;
|
|
|
|
val.v.float_val = float_val;
|
|
|
|
return find_value (&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
ex_value_t *
|
|
|
|
new_vector_val (const float *vector_val)
|
|
|
|
{
|
|
|
|
ex_value_t val;
|
2012-07-19 01:10:30 +00:00
|
|
|
memset (&val, 0, sizeof (val));
|
2012-07-18 14:01:09 +00:00
|
|
|
val.type = ev_vector;
|
|
|
|
VectorCopy (vector_val, val.v.vector_val);
|
|
|
|
return find_value (&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
ex_value_t *
|
|
|
|
new_entity_val (int entity_val)
|
|
|
|
{
|
|
|
|
ex_value_t val;
|
2012-07-19 01:10:30 +00:00
|
|
|
memset (&val, 0, sizeof (val));
|
2012-07-18 14:01:09 +00:00
|
|
|
val.type = ev_entity;
|
|
|
|
val.v.entity_val = entity_val;
|
|
|
|
return find_value (&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
ex_value_t *
|
|
|
|
new_field_val (int field_val, type_t *type, def_t *def)
|
|
|
|
{
|
|
|
|
ex_value_t val;
|
2012-07-19 01:10:30 +00:00
|
|
|
memset (&val, 0, sizeof (val));
|
2012-07-18 14:01:09 +00:00
|
|
|
val.type = ev_field;
|
|
|
|
val.v.pointer.val = field_val;
|
|
|
|
val.v.pointer.type = type;
|
|
|
|
val.v.pointer.def = def;
|
|
|
|
return find_value (&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
ex_value_t *
|
2012-11-21 01:06:15 +00:00
|
|
|
new_func_val (int func_val, type_t *type)
|
2012-07-18 14:01:09 +00:00
|
|
|
{
|
|
|
|
ex_value_t val;
|
2012-07-19 01:10:30 +00:00
|
|
|
memset (&val, 0, sizeof (val));
|
2012-07-18 14:01:09 +00:00
|
|
|
val.type = ev_func;
|
2012-11-21 01:06:15 +00:00
|
|
|
val.v.func_val.val = func_val;
|
|
|
|
val.v.func_val.type = type;
|
2012-07-18 14:01:09 +00:00
|
|
|
return find_value (&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
ex_value_t *
|
|
|
|
new_pointer_val (int pointer_val, type_t *type, def_t *def)
|
|
|
|
{
|
|
|
|
ex_value_t val;
|
2012-07-19 01:10:30 +00:00
|
|
|
memset (&val, 0, sizeof (val));
|
2012-07-18 14:01:09 +00:00
|
|
|
val.type = ev_pointer;
|
|
|
|
val.v.pointer.val = pointer_val;
|
|
|
|
val.v.pointer.type = type;
|
|
|
|
val.v.pointer.def = def;
|
|
|
|
return find_value (&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
ex_value_t *
|
|
|
|
new_quaternion_val (const float *quaternion_val)
|
|
|
|
{
|
|
|
|
ex_value_t val;
|
2012-07-19 01:10:30 +00:00
|
|
|
memset (&val, 0, sizeof (val));
|
2012-07-18 14:01:09 +00:00
|
|
|
val.type = ev_quat;
|
|
|
|
QuatCopy (quaternion_val, val.v.quaternion_val);
|
|
|
|
return find_value (&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
ex_value_t *
|
|
|
|
new_integer_val (int integer_val)
|
|
|
|
{
|
|
|
|
ex_value_t val;
|
2012-07-19 01:10:30 +00:00
|
|
|
memset (&val, 0, sizeof (val));
|
2012-07-18 14:01:09 +00:00
|
|
|
val.type = ev_integer;
|
|
|
|
val.v.integer_val = integer_val;
|
|
|
|
return find_value (&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
ex_value_t *
|
|
|
|
new_uinteger_val (int uinteger_val)
|
|
|
|
{
|
|
|
|
ex_value_t val;
|
2012-07-19 01:10:30 +00:00
|
|
|
memset (&val, 0, sizeof (val));
|
2012-07-18 14:01:09 +00:00
|
|
|
val.type = ev_uinteger;
|
|
|
|
val.v.uinteger_val = uinteger_val;
|
|
|
|
return find_value (&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
ex_value_t *
|
|
|
|
new_short_val (short short_val)
|
|
|
|
{
|
|
|
|
ex_value_t val;
|
2012-07-19 01:10:30 +00:00
|
|
|
memset (&val, 0, sizeof (val));
|
2012-07-18 14:01:09 +00:00
|
|
|
val.type = ev_short;
|
|
|
|
val.v.short_val = short_val;
|
|
|
|
return find_value (&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
ex_value_t *
|
|
|
|
new_nil_val (type_t *type)
|
|
|
|
{
|
|
|
|
ex_value_t val;
|
2012-07-19 01:10:30 +00:00
|
|
|
memset (&val, 0, sizeof (val));
|
2012-07-18 14:01:09 +00:00
|
|
|
val.type = low_level_type (type);
|
|
|
|
if (val.type == ev_pointer|| val.type == ev_field )
|
|
|
|
val.v.pointer.type = type->t.fldptr.type;
|
2012-11-21 01:06:15 +00:00
|
|
|
if (val.type == ev_func)
|
|
|
|
val.v.func_val.type = type;
|
2012-07-18 14:01:09 +00:00
|
|
|
return find_value (&val);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-12-01 11:53:58 +00:00
|
|
|
static void
|
|
|
|
imm_free (void *_imm, void *unused)
|
|
|
|
{
|
|
|
|
free (_imm);
|
|
|
|
}
|
|
|
|
|
2018-10-09 03:35:01 +00:00
|
|
|
static __attribute__((pure)) uintptr_t
|
2012-07-18 13:34:37 +00:00
|
|
|
imm_get_hash (const 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) {
|
2003-03-03 21:09:41 +00:00
|
|
|
const char *str = pr.strings->strings + imm->i.string_val;
|
|
|
|
return str ? Hash_String (str) : 0;
|
2002-10-20 03:31:49 +00:00
|
|
|
} 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) {
|
2011-03-21 09:10:58 +00:00
|
|
|
return Hash_Buffer (&imm->i.pointer, sizeof (&imm->i.pointer));
|
2002-10-20 03:31:49 +00:00
|
|
|
} 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 {
|
2012-10-26 10:02:02 +00:00
|
|
|
internal_error (0, 0);
|
2002-10-20 03:31:49 +00:00
|
|
|
}
|
2001-06-26 07:21:20 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 03:35:01 +00:00
|
|
|
static __attribute__((pure)) int
|
2012-07-18 13:34:37 +00:00
|
|
|
imm_compare (const void *_imm1, const 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) {
|
2003-03-03 21:09:41 +00:00
|
|
|
const char *str1 = pr.strings->strings + imm1->i.string_val;
|
|
|
|
const char *str2 = pr.strings->strings + imm2->i.string_val;
|
|
|
|
return (str1 == str2 || (str1 && str2 && !strcmp (str1, str2)));
|
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) {
|
2011-03-21 09:10:58 +00:00
|
|
|
return !memcmp (&imm1->i.pointer, &imm2->i.pointer,
|
|
|
|
sizeof (imm1->i.pointer));
|
2002-10-20 03:31:49 +00:00
|
|
|
} 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 {
|
2012-10-26 10:02:02 +00:00
|
|
|
internal_error (0, 0);
|
2002-10-20 03:31:49 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2011-03-22 03:24:39 +00:00
|
|
|
static float
|
|
|
|
value_as_float (ex_value_t *value)
|
|
|
|
{
|
2011-04-09 02:32:33 +00:00
|
|
|
if (value->type == ev_uinteger)
|
|
|
|
return value->v.uinteger_val;
|
2011-03-22 03:24:39 +00:00
|
|
|
if (value->type == ev_integer)
|
|
|
|
return value->v.integer_val;
|
|
|
|
if (value->type == ev_short)
|
|
|
|
return value->v.short_val;
|
|
|
|
if (value->type == ev_float)
|
|
|
|
return value->v.float_val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-09 02:32:33 +00:00
|
|
|
static int
|
2011-03-22 03:24:39 +00:00
|
|
|
value_as_int (ex_value_t *value)
|
|
|
|
{
|
2011-04-09 02:32:33 +00:00
|
|
|
if (value->type == ev_uinteger)
|
|
|
|
return value->v.uinteger_val;
|
|
|
|
if (value->type == ev_integer)
|
|
|
|
return value->v.integer_val;
|
|
|
|
if (value->type == ev_short)
|
|
|
|
return value->v.short_val;
|
|
|
|
if (value->type == ev_float)
|
|
|
|
return value->v.float_val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
|
|
|
value_as_uint (ex_value_t *value)
|
|
|
|
{
|
|
|
|
if (value->type == ev_uinteger)
|
|
|
|
return value->v.uinteger_val;
|
2011-03-22 03:24:39 +00:00
|
|
|
if (value->type == ev_integer)
|
|
|
|
return value->v.integer_val;
|
|
|
|
if (value->type == ev_short)
|
|
|
|
return value->v.short_val;
|
|
|
|
if (value->type == ev_float)
|
|
|
|
return value->v.float_val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-05 13:16:08 +00:00
|
|
|
ex_value_t *
|
2011-03-22 03:24:39 +00:00
|
|
|
convert_value (ex_value_t *value, type_t *type)
|
|
|
|
{
|
|
|
|
if (!is_scalar (type) || !is_scalar (ev_types[value->type])) {
|
|
|
|
error (0, "unable to convert non-scalar value");
|
2012-12-05 13:16:08 +00:00
|
|
|
return value;
|
2011-03-22 03:24:39 +00:00
|
|
|
}
|
|
|
|
if (is_float (type)) {
|
|
|
|
float val = value_as_float (value);
|
2012-12-05 13:16:08 +00:00
|
|
|
return new_float_val (val);
|
2011-03-22 03:24:39 +00:00
|
|
|
} else if (type->type == ev_short) {
|
|
|
|
int val = value_as_int (value);
|
2012-12-05 13:16:08 +00:00
|
|
|
return new_short_val (val);
|
2011-04-09 02:32:33 +00:00
|
|
|
} else if (type->type == ev_uinteger) {
|
|
|
|
unsigned val = value_as_uint (value);
|
2012-12-05 13:16:08 +00:00
|
|
|
return new_uinteger_val (val);
|
2011-03-22 03:24:39 +00:00
|
|
|
} else {
|
|
|
|
//FIXME handle enums separately?
|
|
|
|
int val = value_as_int (value);
|
2012-12-05 13:16:08 +00:00
|
|
|
return new_integer_val (val);
|
2011-03-22 03:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-23 10:25:35 +00:00
|
|
|
ex_value_t *
|
|
|
|
alias_value (ex_value_t *value, type_t *type)
|
|
|
|
{
|
|
|
|
ex_value_t new;
|
|
|
|
|
|
|
|
if (type_size (type) != type_size (ev_types[value->type])) {
|
|
|
|
error (0, "unable to alias different sized values");
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
new = *value;
|
|
|
|
new.type = type->type;
|
|
|
|
return find_value (&new);
|
|
|
|
}
|
|
|
|
|
2012-12-01 11:53:58 +00:00
|
|
|
static immediate_t *
|
2012-12-21 04:17:07 +00:00
|
|
|
make_def_imm (def_t *def, hashtab_t *tab, ex_value_t *val)
|
2012-12-01 11:53:58 +00:00
|
|
|
{
|
|
|
|
immediate_t *imm;
|
|
|
|
|
|
|
|
imm = calloc (1, sizeof (immediate_t));
|
|
|
|
imm->def = def;
|
2012-12-21 04:17:07 +00:00
|
|
|
memcpy (&imm->i, &val->v, sizeof (imm->i));
|
2012-12-01 11:53:58 +00:00
|
|
|
|
|
|
|
Hash_AddElement (tab, imm);
|
|
|
|
|
|
|
|
return imm;
|
|
|
|
}
|
|
|
|
|
2001-06-26 07:21:20 +00:00
|
|
|
def_t *
|
2011-02-13 01:09:42 +00:00
|
|
|
emit_value (ex_value_t *value, 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;
|
2011-02-13 01:09:42 +00:00
|
|
|
ex_value_t val = *value;
|
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;
|
2011-03-03 05:56:21 +00:00
|
|
|
if (val.type == ev_void)
|
|
|
|
val.type = type_nil->type;
|
2011-02-13 01:09:42 +00:00
|
|
|
switch (val.type) {
|
2011-01-18 23:41:24 +00:00
|
|
|
case ev_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;
|
2011-01-18 23:41:24 +00:00
|
|
|
case ev_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;
|
2011-01-18 23:41:24 +00:00
|
|
|
case ev_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;
|
2011-01-18 23:41:24 +00:00
|
|
|
case ev_pointer:
|
2001-07-18 06:37:14 +00:00
|
|
|
tab = pointer_imm_defs;
|
|
|
|
type = &type_pointer;
|
|
|
|
break;
|
2011-01-18 23:41:24 +00:00
|
|
|
case ev_integer:
|
2011-04-09 01:07:47 +00:00
|
|
|
case ev_uinteger:
|
2001-07-23 01:31:22 +00:00
|
|
|
if (!def || def->type != &type_float) {
|
|
|
|
tab = integer_imm_defs;
|
2011-01-18 23:41:24 +00:00
|
|
|
type = &type_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-02-13 01:09:42 +00:00
|
|
|
val.v.float_val = val.v.integer_val;
|
|
|
|
val.type = ev_float;
|
2011-01-18 23:41:24 +00:00
|
|
|
case ev_float:
|
2001-06-26 07:21:20 +00:00
|
|
|
tab = float_imm_defs;
|
|
|
|
type = &type_float;
|
|
|
|
break;
|
2011-01-18 23:41:24 +00:00
|
|
|
case ev_string:
|
2011-02-13 01:09:42 +00:00
|
|
|
val.v.integer_val = ReuseString (val.v.string_val);
|
2001-06-26 07:21:20 +00:00
|
|
|
tab = string_imm_defs;
|
|
|
|
type = &type_string;
|
|
|
|
break;
|
2011-01-18 23:41:24 +00:00
|
|
|
case ev_vector:
|
2001-06-26 07:21:20 +00:00
|
|
|
tab = vector_imm_defs;
|
|
|
|
type = &type_vector;
|
|
|
|
break;
|
2011-01-18 23:41:24 +00:00
|
|
|
case ev_quat:
|
2004-04-08 03:32:14 +00:00
|
|
|
tab = quaternion_imm_defs;
|
2001-06-26 07:21:20 +00:00
|
|
|
type = &type_quaternion;
|
|
|
|
break;
|
|
|
|
default:
|
2012-10-26 10:02:02 +00:00
|
|
|
internal_error (0, 0);
|
2001-06-26 07:21:20 +00:00
|
|
|
}
|
2011-02-13 01:09:42 +00:00
|
|
|
memcpy (&search.i, &val.v, sizeof (search.i));
|
2002-10-20 03:31:49 +00:00
|
|
|
imm = (immediate_t *) Hash_FindElement (tab, &search);
|
2003-04-22 15:29:32 +00:00
|
|
|
if (imm && strcmp (imm->def->name, ".zero") == 0) {
|
|
|
|
if (def) {
|
|
|
|
imm = 0; //FIXME do full def aliasing
|
|
|
|
} else {
|
2011-01-25 23:01:24 +00:00
|
|
|
symbol_t *sym;
|
2012-12-02 01:11:30 +00:00
|
|
|
sym = make_symbol (".zero", &type_zero, 0, sc_extern);
|
2011-01-25 23:01:24 +00:00
|
|
|
return sym->s.def;
|
2003-04-22 15:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
2002-10-20 03:31:49 +00:00
|
|
|
if (imm) {
|
|
|
|
cn = imm->def;
|
2001-06-26 07:21:20 +00:00
|
|
|
if (def) {
|
2011-01-24 06:41:43 +00:00
|
|
|
defspace_free_loc (def->space, def->offset, type_size (def->type));
|
|
|
|
def->offset = cn->offset;
|
2001-10-18 17:41:22 +00:00
|
|
|
def->initialized = def->constant = 1;
|
2003-04-25 17:00:22 +00:00
|
|
|
def->nosave = 1;
|
2007-09-15 07:58:39 +00:00
|
|
|
def->local = 0;
|
2001-06-26 07:21:20 +00:00
|
|
|
cn = def;
|
2001-11-14 05:08:37 +00:00
|
|
|
} else {
|
|
|
|
if (cn->type != type) {
|
2012-12-02 01:11:30 +00:00
|
|
|
def = new_def (".imm", type, pr.near_data, sc_static);
|
2011-01-24 06:41:43 +00:00
|
|
|
def->offset = cn->offset;
|
2001-11-14 05:08:37 +00:00
|
|
|
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) {
|
2012-12-02 01:11:30 +00:00
|
|
|
cn = new_def (".imm", type, pr.near_data, sc_static);
|
2011-01-24 06:41:43 +00:00
|
|
|
cn->offset = def->offset;
|
2001-11-13 18:11:19 +00:00
|
|
|
} else {
|
|
|
|
cn = def;
|
|
|
|
}
|
2001-06-26 07:21:20 +00:00
|
|
|
} else {
|
2012-12-02 01:11:30 +00:00
|
|
|
cn = new_def (".imm", type, pr.near_data, sc_static);
|
2001-06-26 07:21:20 +00:00
|
|
|
}
|
2001-10-18 17:41:22 +00:00
|
|
|
cn->initialized = cn->constant = 1;
|
2003-04-25 17:00:22 +00:00
|
|
|
cn->nosave = 1;
|
2001-06-26 07:21:20 +00:00
|
|
|
// copy the immediate to the global area
|
2011-02-13 01:09:42 +00:00
|
|
|
switch (val.type) {
|
2011-01-18 23:41:24 +00:00
|
|
|
case ev_string:
|
2011-02-12 13:34:38 +00:00
|
|
|
reloc_def_string (cn);
|
2002-07-11 22:38:23 +00:00
|
|
|
break;
|
2011-01-18 23:41:24 +00:00
|
|
|
case ev_func:
|
2012-11-21 01:06:15 +00:00
|
|
|
if (val.v.func_val.val) {
|
2011-02-12 13:34:38 +00:00
|
|
|
reloc_t *reloc;
|
|
|
|
reloc = new_reloc (cn->space, cn->offset, rel_def_func);
|
|
|
|
reloc->next = pr.relocs;
|
|
|
|
pr.relocs = reloc;
|
|
|
|
}
|
2002-07-11 22:38:23 +00:00
|
|
|
break;
|
2011-01-18 23:41:24 +00:00
|
|
|
case ev_field:
|
2011-02-13 01:09:42 +00:00
|
|
|
if (val.v.pointer.def)
|
|
|
|
reloc_def_field_ofs (val.v.pointer.def, cn);
|
2004-11-12 10:49:00 +00:00
|
|
|
break;
|
2011-01-18 23:41:24 +00:00
|
|
|
case ev_pointer:
|
2011-02-13 01:09:42 +00:00
|
|
|
if (val.v.pointer.def) {
|
2011-01-24 06:41:43 +00:00
|
|
|
EMIT_DEF_OFS (pr.near_data, D_INT (cn),
|
2011-02-13 01:09:42 +00:00
|
|
|
val.v.pointer.def);
|
2005-06-09 10:34:03 +00:00
|
|
|
}
|
2002-07-14 03:41:13 +00:00
|
|
|
break;
|
2002-07-11 22:38:23 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2001-06-26 07:21:20 +00:00
|
|
|
|
2011-02-13 01:09:42 +00:00
|
|
|
memcpy (D_POINTER (void, cn), &val.v, 4 * type_size (type));
|
2001-06-26 07:21:20 +00:00
|
|
|
|
2018-09-08 13:23:57 +00:00
|
|
|
make_def_imm (cn, tab, &val);
|
2002-10-20 03:31:49 +00:00
|
|
|
|
2001-06-26 07:21:20 +00:00
|
|
|
return cn;
|
|
|
|
}
|
2002-06-28 16:00:01 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
clear_immediates (void)
|
|
|
|
{
|
2012-12-01 11:53:58 +00:00
|
|
|
def_t *def;
|
2012-12-21 04:17:07 +00:00
|
|
|
ex_value_t zero_val;
|
2002-10-20 03:31:49 +00:00
|
|
|
|
2012-07-18 14:01:09 +00:00
|
|
|
if (value_table) {
|
|
|
|
Hash_FlushTable (value_table);
|
2002-06-28 16:00:01 +00:00
|
|
|
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 {
|
2012-07-18 14:01:09 +00:00
|
|
|
value_table = Hash_NewTable (16381, 0, 0, 0);
|
|
|
|
Hash_SetHashCompare (value_table, value_get_hash, value_compare);
|
|
|
|
|
2012-12-01 11:53:58 +00:00
|
|
|
string_imm_defs = Hash_NewTable (16381, 0, imm_free, &string_imm_defs);
|
2002-10-20 03:31:49 +00:00
|
|
|
Hash_SetHashCompare (string_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
2012-12-01 11:53:58 +00:00
|
|
|
float_imm_defs = Hash_NewTable (16381, 0, imm_free, &float_imm_defs);
|
2002-10-20 03:31:49 +00:00
|
|
|
Hash_SetHashCompare (float_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
2012-12-01 11:53:58 +00:00
|
|
|
vector_imm_defs = Hash_NewTable (16381, 0, imm_free, &vector_imm_defs);
|
2002-10-20 03:31:49 +00:00
|
|
|
Hash_SetHashCompare (vector_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
2012-12-01 11:53:58 +00:00
|
|
|
entity_imm_defs = Hash_NewTable (16381, 0, imm_free, &entity_imm_defs);
|
2002-10-20 03:31:49 +00:00
|
|
|
Hash_SetHashCompare (entity_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
2012-12-01 11:53:58 +00:00
|
|
|
field_imm_defs = Hash_NewTable (16381, 0, imm_free, &field_imm_defs);
|
2002-10-20 03:31:49 +00:00
|
|
|
Hash_SetHashCompare (field_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
2012-12-01 11:53:58 +00:00
|
|
|
func_imm_defs = Hash_NewTable (16381, 0, imm_free, &func_imm_defs);
|
2002-10-20 03:31:49 +00:00
|
|
|
Hash_SetHashCompare (func_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
2012-12-01 11:53:58 +00:00
|
|
|
pointer_imm_defs =
|
|
|
|
Hash_NewTable (16381, 0, imm_free, &pointer_imm_defs);
|
2002-10-20 03:31:49 +00:00
|
|
|
Hash_SetHashCompare (pointer_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
2002-06-28 16:00:01 +00:00
|
|
|
quaternion_imm_defs =
|
2012-12-01 11:53:58 +00:00
|
|
|
Hash_NewTable (16381, 0, imm_free, &quaternion_imm_defs);
|
2002-10-20 03:31:49 +00:00
|
|
|
Hash_SetHashCompare (quaternion_imm_defs, imm_get_hash, imm_compare);
|
|
|
|
|
2012-12-01 11:53:58 +00:00
|
|
|
integer_imm_defs =
|
|
|
|
Hash_NewTable (16381, 0, imm_free, &integer_imm_defs);
|
2002-10-20 03:31:49 +00:00
|
|
|
Hash_SetHashCompare (integer_imm_defs, imm_get_hash, imm_compare);
|
2002-06-28 16:00:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-02 01:11:30 +00:00
|
|
|
def = make_symbol (".zero", &type_zero, 0, sc_extern)->s.def;
|
2012-12-01 11:53:58 +00:00
|
|
|
|
2012-12-21 04:17:07 +00:00
|
|
|
memset (&zero_val, 0, sizeof (zero_val));
|
|
|
|
make_def_imm (def, string_imm_defs, &zero_val);
|
|
|
|
make_def_imm (def, float_imm_defs, &zero_val);
|
|
|
|
make_def_imm (def, entity_imm_defs, &zero_val);
|
|
|
|
make_def_imm (def, pointer_imm_defs, &zero_val);
|
|
|
|
make_def_imm (def, integer_imm_defs, &zero_val);
|
2002-06-28 16:00:01 +00:00
|
|
|
}
|