[qfcc] Make the value union anonymous

Another little tag bites the dust.
This commit is contained in:
Bill Currie 2024-08-16 17:12:26 +09:00
parent ce8c72c323
commit e5bd2591c2
13 changed files with 132 additions and 128 deletions

View file

@ -218,6 +218,7 @@ typedef struct ex_value_s {
const type_t *type; const type_t *type;
etype_t lltype; etype_t lltype;
union { union {
uint8_t raw_value; ///< for memcpy
const char *string_val; ///< string constant const char *string_val; ///< string constant
double double_val; ///< double constant double double_val; ///< double constant
int64_t long_val; ///< signed 64-bit constant int64_t long_val; ///< signed 64-bit constant
@ -234,8 +235,9 @@ typedef struct ex_value_s {
uint16_t ushort_val; ///< unsigned short constant uint16_t ushort_val; ///< unsigned short constant
#define VEC_TYPE(type_name, base_type) pr_##type_name##_t type_name##_val; #define VEC_TYPE(type_name, base_type) pr_##type_name##_t type_name##_val;
#include "tools/qfcc/include/vec_types.h" #include "tools/qfcc/include/vec_types.h"
} v; };
} ex_value_t; } ex_value_t;
#define value_size (sizeof(ex_value_t) - field_offset(ex_value_t, raw_value))
typedef struct { typedef struct {
const type_t *type; ///< type to view the expression const type_t *type; ///< type to view the expression

View file

@ -577,8 +577,8 @@ dagnode_set_edges (dag_t *dag, dagnode_t *n, statement_t *s)
} }
if (op->op_type == op_value if (op->op_type == op_value
&& op->value->lltype == ev_ptr && op->value->lltype == ev_ptr
&& op->value->v.pointer.def) { && op->value->pointer.def) {
def_visit_all (op->value->v.pointer.def, 1, def_visit_all (op->value->pointer.def, 1,
dagnode_def_set_edges_visit, n); dagnode_def_set_edges_visit, n);
} }
if (op->op_type == op_def if (op->op_type == op_def

View file

@ -424,9 +424,9 @@ init_elements (struct def_s *def, const expr_t *eles)
} }
if (c->value->lltype == ev_string) { if (c->value->lltype == ev_string) {
EMIT_STRING (def->space, *(pr_string_t *) g, EMIT_STRING (def->space, *(pr_string_t *) g,
c->value->v.string_val); c->value->string_val);
} else { } else {
memcpy (g, &c->value->v, type_size (get_type (c)) * 4); memcpy (g, &c->value->raw_value, type_size (get_type (c)) * 4);
} }
} }
} }
@ -462,8 +462,8 @@ init_vector_components (symbol_t *vector_sym, int is_field, symtab_t *symtab)
error (0, "%s redefined", name); error (0, "%s redefined", name);
sym = 0; sym = 0;
} else { } else {
expr->value->v.pointer.def = vector_sym->def; expr->value->pointer.def = vector_sym->def;
expr->value->v.pointer.val = i; expr->value->pointer.val = i;
} }
} }
} }
@ -657,9 +657,9 @@ initialize_def (symbol_t *sym, const expr_t *init, defspace_t *space,
if (init->value->lltype == ev_ptr if (init->value->lltype == ev_ptr
|| init->value->lltype == ev_field) { || init->value->lltype == ev_field) {
// FIXME offset pointers // FIXME offset pointers
D_INT (sym->def) = init->value->v.pointer.val; D_INT (sym->def) = init->value->pointer.val;
if (init->value->v.pointer.def) if (init->value->pointer.def)
reloc_def_field (init->value->v.pointer.def, sym->def); reloc_def_field (init->value->pointer.def, sym->def);
} else { } else {
ex_value_t *v = init->value; ex_value_t *v = init->value;
if (!init->implicit if (!init->implicit
@ -672,9 +672,9 @@ initialize_def (symbol_t *sym, const expr_t *init, defspace_t *space,
v = convert_value (v, sym->type); v = convert_value (v, sym->type);
if (v->lltype == ev_string) { if (v->lltype == ev_string) {
EMIT_STRING (sym->def->space, D_STRING (sym->def), EMIT_STRING (sym->def->space, D_STRING (sym->def),
v->v.string_val); v->string_val);
} else { } else {
memcpy (D_POINTER (pr_type_t, sym->def), &v->v, memcpy (D_POINTER (pr_type_t, sym->def), &v->raw_value,
type_size (sym->type) * sizeof (pr_type_t)); type_size (sym->type) * sizeof (pr_type_t));
} }
} }

View file

@ -169,7 +169,7 @@ print_struct (dstring_t *dstr, const type_t *t, int level, int id)
int val; int val;
const char *port = ""; const char *port = "";
if (sym->sy_type == sy_const) { if (sym->sy_type == sy_const) {
val = sym->value->v.int_val; val = sym->value->int_val;
} else { } else {
if (sym->sy_type != sy_var) { if (sym->sy_type != sy_var) {
continue; continue;

View file

@ -87,12 +87,12 @@ get_value_def (const expr_t *expr, ex_value_t *value, const type_t *type)
if (is_short (type)) { if (is_short (type)) {
def = new_def (0, &type_short, 0, sc_extern); def = new_def (0, &type_short, 0, sc_extern);
def->offset = value->v.short_val; def->offset = value->short_val;
return def; return def;
} }
if (is_ptr (type) && value->v.pointer.tempop && !value->v.pointer.def) { if (is_ptr (type) && value->pointer.tempop && !value->pointer.def) {
value->v.pointer.def = get_tempop_def (expr, value->v.pointer.tempop, value->pointer.def = get_tempop_def (expr, value->pointer.tempop,
type->t.fldptr.type); type->t.fldptr.type);
} }
def = emit_value (value, 0); def = emit_value (value, 0);
if (type != def->type) if (type != def->type)

View file

@ -172,7 +172,7 @@ convert_value (ex_value_t *value, const type_t *type)
int addr = value_functions[vf_convert].first_statement; int addr = value_functions[vf_convert].first_statement;
value_statements[addr + 0].b = conv; value_statements[addr + 0].b = conv;
value_statements[addr + 1].c = type_size (type) - 1; value_statements[addr + 1].c = type_size (type) - 1;
memcpy (value_globals, &value->v, memcpy (value_globals, &value->raw_value,
type_size (value->type) * sizeof (pr_type_t)); type_size (value->type) * sizeof (pr_type_t));
value_pr.pr_trace = options.verbosity > 1; value_pr.pr_trace = options.verbosity > 1;
PR_ExecuteProgram (&value_pr, vf_convert); PR_ExecuteProgram (&value_pr, vf_convert);
@ -204,7 +204,7 @@ get_def (operand_t *op)
{ {
if (is_short (op->type)) { if (is_short (op->type)) {
auto def = new_def (0, &type_short, 0, sc_extern); auto def = new_def (0, &type_short, 0, sc_extern);
def->offset = op->value->v.short_val; def->offset = op->value->short_val;
return def; return def;
} }
switch (op->op_type) { switch (op->op_type) {

View file

@ -844,7 +844,7 @@ expr_long (const expr_t *e)
return 0; return 0;
} }
if (e->type == ex_value && e->value->lltype == ev_long) { if (e->type == ex_value && e->value->lltype == ev_long) {
return e->value->v.long_val; return e->value->long_val;
} }
internal_error (e, "not a long constant"); internal_error (e, "not a long constant");
} }
@ -954,7 +954,7 @@ expr_string (const expr_t *e)
if (e->type == ex_nil) if (e->type == ex_nil)
return 0; return 0;
if (e->type == ex_value && e->value->lltype == ev_string) if (e->type == ex_value && e->value->lltype == ev_string)
return e->value->v.string_val; return e->value->string_val;
internal_error (e, "not a string constant"); internal_error (e, "not a string constant");
} }
@ -977,10 +977,10 @@ expr_double (const expr_t *e)
if (e->type == ex_nil) if (e->type == ex_nil)
return 0; return 0;
if (e->type == ex_value && e->value->lltype == ev_double) if (e->type == ex_value && e->value->lltype == ev_double)
return e->value->v.double_val; return e->value->double_val;
if (e->type == ex_symbol && e->symbol->sy_type == sy_const if (e->type == ex_symbol && e->symbol->sy_type == sy_const
&& e->symbol->type->type == ev_double) && e->symbol->type->type == ev_double)
return e->symbol->value->v.double_val; return e->symbol->value->double_val;
if (e->type == ex_symbol && e->symbol->sy_type == sy_var if (e->type == ex_symbol && e->symbol->sy_type == sy_var
&& e->symbol->def->constant && e->symbol->def->constant
&& is_double (e->symbol->def->type)) && is_double (e->symbol->def->type))
@ -994,10 +994,10 @@ expr_float (const expr_t *e)
if (e->type == ex_nil) if (e->type == ex_nil)
return 0; return 0;
if (e->type == ex_value && e->value->lltype == ev_float) if (e->type == ex_value && e->value->lltype == ev_float)
return e->value->v.float_val; return e->value->float_val;
if (e->type == ex_symbol && e->symbol->sy_type == sy_const if (e->type == ex_symbol && e->symbol->sy_type == sy_const
&& e->symbol->type->type == ev_float) && e->symbol->type->type == ev_float)
return e->symbol->value->v.float_val; return e->symbol->value->float_val;
if (e->type == ex_symbol && e->symbol->sy_type == sy_var if (e->type == ex_symbol && e->symbol->sy_type == sy_var
&& e->symbol->def->constant && e->symbol->def->constant
&& is_float (e->symbol->def->type)) && is_float (e->symbol->def->type))
@ -1024,10 +1024,10 @@ expr_vector (const expr_t *e)
if (e->type == ex_nil) if (e->type == ex_nil)
return vec3_origin; return vec3_origin;
if (e->type == ex_value && e->value->lltype == ev_vector) if (e->type == ex_value && e->value->lltype == ev_vector)
return e->value->v.vector_val; return e->value->vector_val;
if (e->type == ex_symbol && e->symbol->sy_type == sy_const if (e->type == ex_symbol && e->symbol->sy_type == sy_const
&& e->symbol->type->type == ev_vector) && e->symbol->type->type == ev_vector)
return e->symbol->value->v.vector_val; return e->symbol->value->vector_val;
if (e->type == ex_symbol && e->symbol->sy_type == sy_var if (e->type == ex_symbol && e->symbol->sy_type == sy_var
&& e->symbol->def->constant && e->symbol->def->constant
&& e->symbol->def->type->type == ev_vector) && e->symbol->def->type->type == ev_vector)
@ -1054,10 +1054,10 @@ expr_quaternion (const expr_t *e)
if (e->type == ex_nil) if (e->type == ex_nil)
return quat_origin; return quat_origin;
if (e->type == ex_value && e->value->lltype == ev_quaternion) if (e->type == ex_value && e->value->lltype == ev_quaternion)
return e->value->v.quaternion_val; return e->value->quaternion_val;
if (e->type == ex_symbol && e->symbol->sy_type == sy_const if (e->type == ex_symbol && e->symbol->sy_type == sy_const
&& e->symbol->type->type == ev_quaternion) && e->symbol->type->type == ev_quaternion)
return e->symbol->value->v.quaternion_val; return e->symbol->value->quaternion_val;
if (e->type == ex_symbol && e->symbol->sy_type == sy_var if (e->type == ex_symbol && e->symbol->sy_type == sy_var
&& e->symbol->def->constant && e->symbol->def->constant
&& e->symbol->def->type->type == ev_quaternion) && e->symbol->def->type->type == ev_quaternion)
@ -1092,15 +1092,15 @@ expr_int (const expr_t *e)
return 0; return 0;
} }
if (e->type == ex_value && e->value->lltype == ev_int) { if (e->type == ex_value && e->value->lltype == ev_int) {
return e->value->v.int_val; return e->value->int_val;
} }
if (e->type == ex_value && e->value->lltype == ev_short) { if (e->type == ex_value && e->value->lltype == ev_short) {
return e->value->v.short_val; return e->value->short_val;
} }
if (e->type == ex_symbol && e->symbol->sy_type == sy_const if (e->type == ex_symbol && e->symbol->sy_type == sy_const
&& (e->symbol->type->type == ev_int && (e->symbol->type->type == ev_int
|| is_enum (e->symbol->type))) { || is_enum (e->symbol->type))) {
return e->symbol->value->v.int_val; return e->symbol->value->int_val;
} }
if (e->type == ex_symbol && e->symbol->sy_type == sy_var if (e->type == ex_symbol && e->symbol->sy_type == sy_var
&& e->symbol->def->constant && e->symbol->def->constant
@ -1141,11 +1141,11 @@ expr_uint (const expr_t *e)
return 0; return 0;
} }
if (e->type == ex_value && e->value->lltype == ev_uint) { if (e->type == ex_value && e->value->lltype == ev_uint) {
return e->value->v.uint_val; return e->value->uint_val;
} }
if (e->type == ex_symbol && e->symbol->sy_type == sy_const if (e->type == ex_symbol && e->symbol->sy_type == sy_const
&& e->symbol->type->type == ev_uint) { && e->symbol->type->type == ev_uint) {
return e->symbol->value->v.uint_val; return e->symbol->value->uint_val;
} }
if (e->type == ex_symbol && e->symbol->sy_type == sy_var if (e->type == ex_symbol && e->symbol->sy_type == sy_var
&& e->symbol->def->constant && e->symbol->def->constant
@ -1182,11 +1182,11 @@ expr_short (const expr_t *e)
return 0; return 0;
} }
if (e->type == ex_value && e->value->lltype == ev_short) { if (e->type == ex_value && e->value->lltype == ev_short) {
return e->value->v.short_val; return e->value->short_val;
} }
if (e->type == ex_symbol && e->symbol->sy_type == sy_const if (e->type == ex_symbol && e->symbol->sy_type == sy_const
&& e->symbol->type->type == ev_short) { && e->symbol->type->type == ev_short) {
return e->symbol->value->v.short_val; return e->symbol->value->short_val;
} }
internal_error (e, "not a short constant"); internal_error (e, "not a short constant");
} }
@ -1198,11 +1198,11 @@ expr_ushort (const expr_t *e)
return 0; return 0;
} }
if (e->type == ex_value && e->value->lltype == ev_ushort) { if (e->type == ex_value && e->value->lltype == ev_ushort) {
return e->value->v.ushort_val; return e->value->ushort_val;
} }
if (e->type == ex_symbol && e->symbol->sy_type == sy_const if (e->type == ex_symbol && e->symbol->sy_type == sy_const
&& e->symbol->type->type == ev_ushort) { && e->symbol->type->type == ev_ushort) {
return e->symbol->value->v.ushort_val; return e->symbol->value->ushort_val;
} }
internal_error (e, "not a ushort constant"); internal_error (e, "not a ushort constant");
} }
@ -1596,7 +1596,7 @@ field_expr (const expr_t *e1, const expr_t *e2)
|| e2->value->lltype != ev_field) { || e2->value->lltype != ev_field) {
internal_error (e2, "unexpected field exression"); internal_error (e2, "unexpected field exression");
} }
auto fv = new_field_val (e2->value->v.pointer.val + field->offset, field->type, e2->value->v.pointer.def); auto fv = new_field_val (e2->value->pointer.val + field->offset, field->type, e2->value->pointer.def);
scoped_src_loc (e2); scoped_src_loc (e2);
e2 = new_value_expr (fv, false); e2 = new_value_expr (fv, false);
// create a new . expression // create a new . expression

View file

@ -1552,14 +1552,14 @@ flow_analyze_pointer_operand (operand_t *ptrop, set_t *def)
operand_t *op = 0; operand_t *op = 0;
if (ptrop->op_type == op_value && ptrop->value->lltype == ev_ptr) { if (ptrop->op_type == op_value && ptrop->value->lltype == ev_ptr) {
ex_pointer_t *ptr = &ptrop->value->v.pointer; ex_pointer_t *ptr = &ptrop->value->pointer;
if (ptrop->value->v.pointer.def) { if (ptrop->value->pointer.def) {
def_t *alias; def_t *alias;
alias = alias_def (ptr->def, ptr->type, ptr->val); alias = alias_def (ptr->def, ptr->type, ptr->val);
op = def_operand (alias, ptr->type, ptrop->expr); op = def_operand (alias, ptr->type, ptrop->expr);
} }
if (ptrop->value->v.pointer.tempop) { if (ptrop->value->pointer.tempop) {
op = ptrop->value->v.pointer.tempop; op = ptrop->value->pointer.tempop;
} }
if (op) { if (op) {
flow_add_op_var (def, op, 6); flow_add_op_var (def, op, 6);
@ -1648,9 +1648,9 @@ flow_analyze_statement (statement_t *s, set_t *use, set_t *def, set_t *kill,
aux_op1 = s->opb; aux_op1 = s->opb;
if (s->type != st_ptrassign && s->opb->op_type == op_value) { if (s->type != st_ptrassign && s->opb->op_type == op_value) {
if (is_short (s->opb->type)) { if (is_short (s->opb->type)) {
size = s->opb->value->v.short_val; size = s->opb->value->short_val;
} else if (is_int (s->opb->type)) { } else if (is_int (s->opb->type)) {
size = s->opb->value->v.int_val; size = s->opb->value->int_val;
} else { } else {
print_type (s->opb->type); print_type (s->opb->type);
internal_error (s->expr, "unexpected type for memset/move"); internal_error (s->expr, "unexpected type for memset/move");
@ -1717,7 +1717,7 @@ flow_analyze_statement (statement_t *s, set_t *use, set_t *def, set_t *kill,
if (s->opc) { if (s->opc) {
// ruamoko // ruamoko
// opc always short // opc always short
short ret_mode = s->opc->value->v.short_val; short ret_mode = s->opc->value->short_val;
// -1 is void // -1 is void
// FIXME size and addressing // FIXME size and addressing
if (ret_mode >= 0) { if (ret_mode >= 0) {

View file

@ -217,7 +217,7 @@ qfo_encode_struct (const type_t *type, defspace_t *space)
if (i == num_fields) if (i == num_fields)
internal_error (0, "whoa, what happened?"); internal_error (0, "whoa, what happened?");
if (sym->sy_type == sy_const) if (sym->sy_type == sy_const)
offset = sym->value->v.int_val; offset = sym->value->int_val;
else else
offset = sym->offset; offset = sym->offset;
ENC_DEF (strct->fields[i].type, field_types[i]); ENC_DEF (strct->fields[i].type, field_types[i]);

View file

@ -1057,7 +1057,7 @@ vector_call (sblock_t *sblock, const expr_t *earg, const expr_t *param, int ind,
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
n = new_name_expr (names[i]); n = new_name_expr (names[i]);
v = new_float_expr (earg->value->v.vector_val[i]); v = new_float_expr (earg->value->vector_val[i]);
a = assign_expr (field_expr (param, n), v); a = assign_expr (field_expr (param, n), v);
param = new_param_expr (get_type (earg), ind); param = new_param_expr (get_type (earg), ind);
//a->line = earg->line; //a->line = earg->line;

View file

@ -286,7 +286,7 @@ add_enum (symbol_t *enm, symbol_t *name, const expr_t *val)
name->type = enum_type; name->type = enum_type;
value = 0; value = 0;
if (enum_tab->symbols) if (enum_tab->symbols)
value = ((symbol_t *)(enum_tab->symtail))->value->v.uint_val + 1; value = ((symbol_t *)(enum_tab->symtail))->value->uint_val + 1;
if (val) { if (val) {
val = convert_name (val); val = convert_name (val);
if (!is_constant (val)) if (!is_constant (val))
@ -314,12 +314,12 @@ enum_as_bool (const type_t *enm, expr_t **zero, expr_t **one)
for (sym = symtab->symbols; sym; sym = sym->next) { for (sym = symtab->symbols; sym; sym = sym->next) {
if (sym->sy_type != sy_const) if (sym->sy_type != sy_const)
continue; continue;
val = sym->value->v.int_val; val = sym->value->int_val;
if (!val) { if (!val) {
zero_sym = sym; zero_sym = sym;
} else { } else {
if (one_sym) { if (one_sym) {
v = one_sym->value->v.int_val; v = one_sym->value->int_val;
if (val * val > v * v) if (val * val > v * v)
continue; continue;
} }

View file

@ -82,7 +82,7 @@ get_hash (const void *_cl, void *unused)
if (!cl->value) if (!cl->value)
return 0; return 0;
val = get_value (cl->value); val = get_value (cl->value);
return Hash_Buffer (&val->v, sizeof (val->v)) + val->lltype; return Hash_Buffer (&val->raw_value, value_size) + val->lltype;
} }
static int static int
@ -104,7 +104,7 @@ compare (const void *_cla, const void *_clb, void *unused)
val2 = get_value (v2); val2 = get_value (v2);
if (val1->type != val2->type) if (val1->type != val2->type)
return 0; return 0;
return memcmp (&val1->v, &val2->v, sizeof (val1->v)) == 0; return memcmp (&val1->raw_value, &val2->raw_value, value_size) == 0;
} }
const expr_t * const expr_t *
@ -383,7 +383,7 @@ check_enum_switch (switch_block_t *switch_block)
for (enum_val = type->t.symtab->symbols; enum_val; for (enum_val = type->t.symtab->symbols; enum_val;
enum_val = enum_val->next) { enum_val = enum_val->next) {
cl.value = new_int_expr (enum_val->value->v.int_val, false); cl.value = new_int_expr (enum_val->value->int_val, false);
if (!Hash_FindElement (switch_block->labels, &cl)) { if (!Hash_FindElement (switch_block->labels, &cl)) {
warning (switch_block->test, warning (switch_block->test,
"enumeration value `%s' not handled in switch", "enumeration value `%s' not handled in switch",

View file

@ -79,7 +79,7 @@ static uintptr_t
value_get_hash (const void *_val, void *unused) value_get_hash (const void *_val, void *unused)
{ {
const ex_value_t *val = (const ex_value_t *) _val; const ex_value_t *val = (const ex_value_t *) _val;
return Hash_Buffer (&val->v, sizeof (val->v)) ^ (uintptr_t) val->type; return Hash_Buffer (&val->raw_value, value_size) ^ (uintptr_t) val->type;
} }
static int static int
@ -89,7 +89,7 @@ value_compare (const void *_val1, const void *_val2, void *unused)
const ex_value_t *val2 = (const ex_value_t *) _val2; const ex_value_t *val2 = (const ex_value_t *) _val2;
if (val1->type != val2->type) if (val1->type != val2->type)
return 0; return 0;
return memcmp (&val1->v, &val2->v, sizeof (val1->v)) == 0; return memcmp (&val1->raw_value, &val2->raw_value, value_size) == 0;
} }
static ex_value_t * static ex_value_t *
@ -130,7 +130,7 @@ new_string_val (const char *string_val)
memset (&val, 0, sizeof (val)); memset (&val, 0, sizeof (val));
set_val_type (&val, &type_string); set_val_type (&val, &type_string);
if (string_val) if (string_val)
val.v.string_val = save_string (string_val); val.string_val = save_string (string_val);
return find_value (&val); return find_value (&val);
} }
@ -140,7 +140,7 @@ new_double_val (double double_val)
ex_value_t val; ex_value_t val;
memset (&val, 0, sizeof (val)); memset (&val, 0, sizeof (val));
set_val_type (&val, &type_double); set_val_type (&val, &type_double);
val.v.double_val = double_val; val.double_val = double_val;
return find_value (&val); return find_value (&val);
} }
@ -150,7 +150,7 @@ new_float_val (float float_val)
ex_value_t val; ex_value_t val;
memset (&val, 0, sizeof (val)); memset (&val, 0, sizeof (val));
set_val_type (&val, &type_float); set_val_type (&val, &type_float);
val.v.float_val = float_val; val.float_val = float_val;
return find_value (&val); return find_value (&val);
} }
@ -160,7 +160,7 @@ new_vector_val (const float *vector_val)
ex_value_t val; ex_value_t val;
memset (&val, 0, sizeof (val)); memset (&val, 0, sizeof (val));
set_val_type (&val, &type_vector); set_val_type (&val, &type_vector);
VectorCopy (vector_val, val.v.vector_val); VectorCopy (vector_val, val.vector_val);
return find_value (&val); return find_value (&val);
} }
@ -170,7 +170,7 @@ new_entity_val (int entity_val)
ex_value_t val; ex_value_t val;
memset (&val, 0, sizeof (val)); memset (&val, 0, sizeof (val));
set_val_type (&val, &type_entity); set_val_type (&val, &type_entity);
val.v.entity_val = entity_val; val.entity_val = entity_val;
return find_value (&val); return find_value (&val);
} }
@ -180,9 +180,9 @@ new_field_val (int field_val, const type_t *type, def_t *def)
ex_value_t val; ex_value_t val;
memset (&val, 0, sizeof (val)); memset (&val, 0, sizeof (val));
set_val_type (&val, field_type (type)); set_val_type (&val, field_type (type));
val.v.pointer.val = field_val; val.pointer.val = field_val;
val.v.pointer.type = type; val.pointer.type = type;
val.v.pointer.def = def; val.pointer.def = def;
return find_value (&val); return find_value (&val);
} }
@ -192,8 +192,8 @@ new_func_val (int func_val, const type_t *type)
ex_value_t val; ex_value_t val;
memset (&val, 0, sizeof (val)); memset (&val, 0, sizeof (val));
set_val_type (&val, type); set_val_type (&val, type);
val.v.func_val.val = func_val; val.func_val.val = func_val;
val.v.func_val.type = type; val.func_val.type = type;
return find_value (&val); return find_value (&val);
} }
@ -207,10 +207,10 @@ new_pointer_val (int pointer_val, const type_t *type, def_t *def,
} }
memset (&val, 0, sizeof (val)); memset (&val, 0, sizeof (val));
set_val_type (&val, pointer_type (type)); set_val_type (&val, pointer_type (type));
val.v.pointer.val = pointer_val; val.pointer.val = pointer_val;
val.v.pointer.type = type; val.pointer.type = type;
val.v.pointer.def = def; val.pointer.def = def;
val.v.pointer.tempop = tempop; val.pointer.tempop = tempop;
return find_value (&val); return find_value (&val);
} }
@ -220,7 +220,7 @@ new_quaternion_val (const float *quaternion_val)
ex_value_t val; ex_value_t val;
memset (&val, 0, sizeof (val)); memset (&val, 0, sizeof (val));
set_val_type (&val, &type_quaternion); set_val_type (&val, &type_quaternion);
QuatCopy (quaternion_val, val.v.quaternion_val); QuatCopy (quaternion_val, val.quaternion_val);
return find_value (&val); return find_value (&val);
} }
@ -230,7 +230,7 @@ new_int_val (int int_val)
ex_value_t val; ex_value_t val;
memset (&val, 0, sizeof (val)); memset (&val, 0, sizeof (val));
set_val_type (&val, &type_int); set_val_type (&val, &type_int);
val.v.int_val = int_val; val.int_val = int_val;
return find_value (&val); return find_value (&val);
} }
@ -240,14 +240,14 @@ new_uint_val (int uint_val)
ex_value_t val; ex_value_t val;
memset (&val, 0, sizeof (val)); memset (&val, 0, sizeof (val));
set_val_type (&val, &type_uint); set_val_type (&val, &type_uint);
val.v.uint_val = uint_val; val.uint_val = uint_val;
return find_value (&val); return find_value (&val);
} }
ex_value_t * ex_value_t *
new_long_val (pr_long_t long_val) new_long_val (pr_long_t long_val)
{ {
ex_value_t val = { .v = { .long_val = long_val } }; ex_value_t val = { .long_val = long_val };
set_val_type (&val, &type_long); set_val_type (&val, &type_long);
return find_value (&val); return find_value (&val);
} }
@ -255,7 +255,7 @@ new_long_val (pr_long_t long_val)
ex_value_t * ex_value_t *
new_ulong_val (pr_ulong_t ulong_val) new_ulong_val (pr_ulong_t ulong_val)
{ {
ex_value_t val = { .v = { .ulong_val = ulong_val } }; ex_value_t val = { .ulong_val = ulong_val };
set_val_type (&val, &type_ulong); set_val_type (&val, &type_ulong);
return find_value (&val); return find_value (&val);
} }
@ -266,7 +266,7 @@ new_short_val (short short_val)
ex_value_t val; ex_value_t val;
memset (&val, 0, sizeof (val)); memset (&val, 0, sizeof (val));
set_val_type (&val, &type_short); set_val_type (&val, &type_short);
val.v.short_val = short_val; val.short_val = short_val;
return find_value (&val); return find_value (&val);
} }
@ -280,9 +280,9 @@ new_nil_val (const type_t *type)
val.lltype = type_nil->type; val.lltype = type_nil->type;
} }
if (val.lltype == ev_ptr || val.lltype == ev_field ) if (val.lltype == ev_ptr || val.lltype == ev_field )
val.v.pointer.type = type->t.fldptr.type; val.pointer.type = type->t.fldptr.type;
if (val.lltype == ev_func) if (val.lltype == ev_func)
val.v.func_val.type = type; val.func_val.type = type;
return find_value (&val); return find_value (&val);
} }
@ -291,8 +291,8 @@ new_type_value (const type_t *type, const pr_type_t *data)
{ {
size_t typeSize = type_size (type) * sizeof (pr_type_t); size_t typeSize = type_size (type) * sizeof (pr_type_t);
ex_value_t val = {}; ex_value_t val = {};
set_val_type (&val, (type_t *) type);//FIXME cast set_val_type (&val, type);
memcpy (&val.v, data, typeSize); memcpy (&val.raw_value, data, typeSize);
return find_value (&val); return find_value (&val);
} }
@ -322,7 +322,7 @@ value_store (pr_type_t *dst, const type_t *dstType, const expr_t *src)
if (!val) { if (!val) {
internal_error (src, "unexpected constant expression type"); internal_error (src, "unexpected constant expression type");
} }
memcpy (dst, &val->v, dstSize); memcpy (dst, &val->raw_value, dstSize);
} }
const char * const char *
@ -332,135 +332,135 @@ get_value_string (const ex_value_t *value)
const char *str = ""; const char *str = "";
switch (type->type) { switch (type->type) {
case ev_string: case ev_string:
return va (0, "\"%s\"", quote_string (value->v.string_val)); return va (0, "\"%s\"", quote_string (value->string_val));
case ev_vector: case ev_vector:
case ev_quaternion: case ev_quaternion:
case ev_float: case ev_float:
switch (type_width (type)) { switch (type_width (type)) {
case 1: case 1:
str = va (0, "%.9g", value->v.float_val); str = va (0, "%.9g", value->float_val);
break; break;
case 2: case 2:
str = va (0, VEC2F_FMT, VEC2_EXP (value->v.vec2_val)); str = va (0, VEC2F_FMT, VEC2_EXP (value->vec2_val));
break; break;
case 3: case 3:
str = va (0, "[%.9g, %.9g, %.9g]", str = va (0, "[%.9g, %.9g, %.9g]",
VectorExpand (value->v.vec3_val)); VectorExpand (value->vec3_val));
break; break;
case 4: case 4:
str = va (0, VEC4F_FMT, VEC4_EXP (value->v.vec4_val)); str = va (0, VEC4F_FMT, VEC4_EXP (value->vec4_val));
break; break;
} }
return va (0, "%s %s", type->name, str); return va (0, "%s %s", type->name, str);
case ev_entity: case ev_entity:
case ev_func: case ev_func:
return va (0, "%s %d", type->name, value->v.int_val); return va (0, "%s %d", type->name, value->int_val);
case ev_field: case ev_field:
if (value->v.pointer.def) { if (value->pointer.def) {
int offset = value->v.pointer.val; int offset = value->pointer.val;
offset += value->v.pointer.def->offset; offset += value->pointer.def->offset;
return va (0, "field %d", offset); return va (0, "field %d", offset);
} else { } else {
return va (0, "field %d", value->v.pointer.val); return va (0, "field %d", value->pointer.val);
} }
case ev_ptr: case ev_ptr:
if (value->v.pointer.def) { if (value->pointer.def) {
str = va (0, "<%s>", value->v.pointer.def->name); str = va (0, "<%s>", value->pointer.def->name);
} }
return va (0, "(* %s)[%d]%s", return va (0, "(* %s)[%d]%s",
value->v.pointer.type value->pointer.type
? get_type_string (value->v.pointer.type) : "???", ? get_type_string (value->pointer.type) : "???",
value->v.pointer.val, str); value->pointer.val, str);
case ev_int: case ev_int:
switch (type_width (type)) { switch (type_width (type)) {
case 1: case 1:
str = va (0, "%"PRIi32, value->v.int_val); str = va (0, "%"PRIi32, value->int_val);
break; break;
case 2: case 2:
str = va (0, VEC2I_FMT, VEC2_EXP (value->v.ivec2_val)); str = va (0, VEC2I_FMT, VEC2_EXP (value->ivec2_val));
break; break;
case 3: case 3:
str = va (0, "[%"PRIi32", %"PRIi32", %"PRIi32"]", str = va (0, "[%"PRIi32", %"PRIi32", %"PRIi32"]",
VectorExpand (value->v.ivec3_val)); VectorExpand (value->ivec3_val));
break; break;
case 4: case 4:
str = va (0, VEC4I_FMT, VEC4_EXP (value->v.ivec4_val)); str = va (0, VEC4I_FMT, VEC4_EXP (value->ivec4_val));
break; break;
} }
return va (0, "%s %s", type->name, str); return va (0, "%s %s", type->name, str);
case ev_uint: case ev_uint:
switch (type_width (type)) { switch (type_width (type)) {
case 1: case 1:
str = va (0, "%"PRIu32, value->v.uint_val); str = va (0, "%"PRIu32, value->uint_val);
break; break;
case 2: case 2:
str = va (0, "[%"PRIu32", %"PRIi32"]", str = va (0, "[%"PRIu32", %"PRIi32"]",
VEC2_EXP (value->v.uivec2_val)); VEC2_EXP (value->uivec2_val));
break; break;
case 3: case 3:
str = va (0, "[%"PRIu32", %"PRIi32", %"PRIi32"]", str = va (0, "[%"PRIu32", %"PRIi32", %"PRIi32"]",
VectorExpand (value->v.uivec3_val)); VectorExpand (value->uivec3_val));
break; break;
case 4: case 4:
str = va (0, "[%"PRIu32", %"PRIi32", %"PRIi32", %"PRIi32"]", str = va (0, "[%"PRIu32", %"PRIi32", %"PRIi32", %"PRIi32"]",
VEC4_EXP (value->v.uivec4_val)); VEC4_EXP (value->uivec4_val));
break; break;
} }
return va (0, "%s %s", type->name, str); return va (0, "%s %s", type->name, str);
case ev_short: case ev_short:
return va (0, "%s %"PRIi16, type->name, value->v.short_val); return va (0, "%s %"PRIi16, type->name, value->short_val);
case ev_ushort: case ev_ushort:
return va (0, "%s %"PRIu16, type->name, value->v.ushort_val); return va (0, "%s %"PRIu16, type->name, value->ushort_val);
case ev_double: case ev_double:
switch (type_width (type)) { switch (type_width (type)) {
case 1: case 1:
str = va (0, "%.17g", value->v.double_val); str = va (0, "%.17g", value->double_val);
break; break;
case 2: case 2:
str = va (0, VEC2D_FMT, VEC2_EXP (value->v.dvec2_val)); str = va (0, VEC2D_FMT, VEC2_EXP (value->dvec2_val));
break; break;
case 3: case 3:
str = va (0, "[%.17g, %.17g, %.17g]", str = va (0, "[%.17g, %.17g, %.17g]",
VectorExpand (value->v.dvec3_val)); VectorExpand (value->dvec3_val));
break; break;
case 4: case 4:
str = va (0, VEC4D_FMT, VEC4_EXP (value->v.dvec4_val)); str = va (0, VEC4D_FMT, VEC4_EXP (value->dvec4_val));
break; break;
} }
return va (0, "%s %s", type->name, str); return va (0, "%s %s", type->name, str);
case ev_long: case ev_long:
switch (type_width (type)) { switch (type_width (type)) {
case 1: case 1:
str = va (0, "%"PRIi64, value->v.long_val); str = va (0, "%"PRIi64, value->long_val);
break; break;
case 2: case 2:
str = va (0, VEC2L_FMT, VEC2_EXP (value->v.lvec2_val)); str = va (0, VEC2L_FMT, VEC2_EXP (value->lvec2_val));
break; break;
case 3: case 3:
str = va (0, "[%"PRIi64", %"PRIi64", %"PRIi64"]", str = va (0, "[%"PRIi64", %"PRIi64", %"PRIi64"]",
VectorExpand (value->v.lvec3_val)); VectorExpand (value->lvec3_val));
break; break;
case 4: case 4:
str = va (0, VEC4L_FMT, VEC4_EXP (value->v.lvec4_val)); str = va (0, VEC4L_FMT, VEC4_EXP (value->lvec4_val));
break; break;
} }
return va (0, "%s %s", type->name, str); return va (0, "%s %s", type->name, str);
case ev_ulong: case ev_ulong:
switch (type_width (type)) { switch (type_width (type)) {
case 1: case 1:
str = va (0, "%"PRIu64, value->v.ulong_val); str = va (0, "%"PRIu64, value->ulong_val);
break; break;
case 2: case 2:
str = va (0, "[%"PRIu64", %"PRIi64"]", str = va (0, "[%"PRIu64", %"PRIi64"]",
VEC2_EXP (value->v.ulvec2_val)); VEC2_EXP (value->ulvec2_val));
break; break;
case 3: case 3:
str = va (0, "[%"PRIu64", %"PRIi64", %"PRIi64"]", str = va (0, "[%"PRIu64", %"PRIi64", %"PRIi64"]",
VectorExpand (value->v.ulvec3_val)); VectorExpand (value->ulvec3_val));
break; break;
case 4: case 4:
str = va (0, "[%"PRIu64", %"PRIi64", %"PRIi64", %"PRIi64"]", str = va (0, "[%"PRIu64", %"PRIi64", %"PRIi64", %"PRIi64"]",
VEC4_EXP (value->v.ulvec4_val)); VEC4_EXP (value->ulvec4_val));
break; break;
} }
return va (0, "%s %s", type->name, str); return va (0, "%s %s", type->name, str);
@ -546,7 +546,8 @@ offset_alias_value (ex_value_t *value, const type_t *type, int offset)
return value; return value;
} }
pr_type_t data[type_size (value->type)]; pr_type_t data[type_size (value->type)];
memcpy (data, &value->v, sizeof (pr_type_t) * type_size (value->type)); size_t data_size = sizeof (pr_type_t) * type_size (value->type);
memcpy (data, &value->raw_value, data_size);
return new_type_value (type, data + offset); return new_type_value (type, data + offset);
} }
@ -572,7 +573,7 @@ make_def_imm (def_t *def, hashtab_t *tab, ex_value_t *val)
imm = aligned_alloc (__alignof__(immediate_t), sizeof (immediate_t)); imm = aligned_alloc (__alignof__(immediate_t), sizeof (immediate_t));
memset (imm, 0, sizeof (immediate_t)); memset (imm, 0, sizeof (immediate_t));
imm->def = def; imm->def = def;
memcpy (&imm->i, &val->v, sizeof (imm->i)); memcpy (&imm->i, &val->raw_value, sizeof (imm->i));
Hash_AddElement (tab, imm); Hash_AddElement (tab, imm);
@ -593,7 +594,7 @@ emit_value_core (ex_value_t *val, def_t *def, defspace_t *data)
reloc_def_string (def); reloc_def_string (def);
break; break;
case ev_func: case ev_func:
if (val->v.func_val.val) { if (val->func_val.val) {
reloc_t *reloc; reloc_t *reloc;
reloc = new_reloc (def->space, def->offset, rel_def_func); reloc = new_reloc (def->space, def->offset, rel_def_func);
reloc->next = pr.relocs; reloc->next = pr.relocs;
@ -601,19 +602,20 @@ emit_value_core (ex_value_t *val, def_t *def, defspace_t *data)
} }
break; break;
case ev_field: case ev_field:
if (val->v.pointer.def) if (val->pointer.def)
reloc_def_field_ofs (val->v.pointer.def, def); reloc_def_field_ofs (val->pointer.def, def);
break; break;
case ev_ptr: case ev_ptr:
if (val->v.pointer.def) { if (val->pointer.def) {
EMIT_DEF_OFS (data, D_INT (def), val->v.pointer.def); EMIT_DEF_OFS (data, D_INT (def), val->pointer.def);
} }
break; break;
default: default:
break; break;
} }
memcpy (D_POINTER (pr_type_t, def), &val->v, 4 * type_size (val->type)); size_t data_size = sizeof (pr_type_t) * type_size (val->type);
memcpy (D_POINTER (pr_type_t, def), &val->raw_value, data_size);
return def; return def;
} }
@ -651,7 +653,7 @@ emit_value (ex_value_t *value, def_t *def)
type = ev_types[val.lltype]; type = ev_types[val.lltype];
break; break;
case ev_string: case ev_string:
val.v.int_val = ReuseString (val.v.string_val); val.int_val = ReuseString (val.string_val);
tab = string_imm_defs; tab = string_imm_defs;
type = &type_string; type = &type_string;
break; break;
@ -663,7 +665,7 @@ emit_value (ex_value_t *value, def_t *def)
} }
def_t search_def = { .type = type }; def_t search_def = { .type = type };
immediate_t search = { .def = &search_def }; immediate_t search = { .def = &search_def };
memcpy (&search.i, &val.v, sizeof (search.i)); memcpy (&search.i, &val.raw_value, sizeof (search.i));
immediate_t *imm = Hash_FindElement (tab, &search); immediate_t *imm = Hash_FindElement (tab, &search);
if (imm && strcmp (imm->def->name, ".zero") == 0) { if (imm && strcmp (imm->def->name, ".zero") == 0) {
if (def) { if (def) {