2002-10-22 14:53:18 +00:00
|
|
|
/*
|
|
|
|
def.c
|
2001-04-01 02:12:57 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
def management and symbol tables
|
2001-04-01 02:12:57 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
2001-04-01 02:12:57 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2002/06/09
|
|
|
|
|
|
|
|
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 02:12:57 +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-04-01 02:12:57 +00:00
|
|
|
#include <QF/hash.h>
|
2001-10-24 22:50:06 +00:00
|
|
|
#include <QF/sys.h>
|
2002-06-01 05:06:19 +00:00
|
|
|
#include <QF/va.h>
|
2001-04-01 02:12:57 +00:00
|
|
|
|
|
|
|
#include "qfcc.h"
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "def.h"
|
2011-01-17 03:06:15 +00:00
|
|
|
#include "defspace.h"
|
2011-01-24 12:54:57 +00:00
|
|
|
#include "diagnostic.h"
|
2011-02-15 00:30:37 +00:00
|
|
|
#include "emit.h"
|
2002-03-18 07:30:21 +00:00
|
|
|
#include "expr.h"
|
2011-03-23 12:32:14 +00:00
|
|
|
#include "function.h"
|
2004-02-17 00:39:21 +00:00
|
|
|
#include "options.h"
|
2002-07-16 21:59:47 +00:00
|
|
|
#include "reloc.h"
|
2002-07-05 20:02:10 +00:00
|
|
|
#include "strpool.h"
|
2002-01-21 19:03:29 +00:00
|
|
|
#include "struct.h"
|
2011-01-25 03:07:02 +00:00
|
|
|
#include "symtab.h"
|
2002-05-09 06:34:48 +00:00
|
|
|
#include "type.h"
|
2011-03-22 03:24:39 +00:00
|
|
|
#include "value.h"
|
2001-04-01 02:12:57 +00:00
|
|
|
|
2002-06-05 19:03:51 +00:00
|
|
|
static def_t *free_defs;
|
2002-06-09 03:57:20 +00:00
|
|
|
|
2011-01-24 06:41:43 +00:00
|
|
|
static void
|
2002-07-02 21:34:40 +00:00
|
|
|
set_storage_bits (def_t *def, storage_class_t storage)
|
2002-06-17 02:43:38 +00:00
|
|
|
{
|
2002-07-02 21:34:40 +00:00
|
|
|
switch (storage) {
|
2002-09-16 15:42:11 +00:00
|
|
|
case st_system:
|
|
|
|
def->system = 1;
|
|
|
|
// fall through
|
2002-07-02 21:34:40 +00:00
|
|
|
case st_global:
|
|
|
|
def->global = 1;
|
|
|
|
def->external = 0;
|
2002-07-12 06:17:24 +00:00
|
|
|
def->local = 0;
|
2002-07-02 21:34:40 +00:00
|
|
|
break;
|
|
|
|
case st_extern:
|
|
|
|
def->global = 1;
|
|
|
|
def->external = 1;
|
2002-07-12 06:17:24 +00:00
|
|
|
def->local = 0;
|
2002-07-02 21:34:40 +00:00
|
|
|
break;
|
|
|
|
case st_static:
|
|
|
|
def->external = 0;
|
|
|
|
def->global = 0;
|
2002-07-12 06:17:24 +00:00
|
|
|
def->local = 0;
|
2002-07-02 21:34:40 +00:00
|
|
|
break;
|
|
|
|
case st_local:
|
2002-07-12 06:17:24 +00:00
|
|
|
def->external = 0;
|
|
|
|
def->global = 0;
|
|
|
|
def->local = 1;
|
2002-07-02 21:34:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-07-02 23:08:44 +00:00
|
|
|
def->initialized = 0;
|
2002-06-17 02:43:38 +00:00
|
|
|
}
|
|
|
|
|
2003-07-23 18:28:31 +00:00
|
|
|
def_t *
|
2011-01-24 06:41:43 +00:00
|
|
|
new_def (const char *name, type_t *type, defspace_t *space,
|
2002-06-28 17:59:32 +00:00
|
|
|
storage_class_t storage)
|
2001-04-01 02:12:57 +00:00
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
def_t *def;
|
2001-04-01 02:12:57 +00:00
|
|
|
|
2002-06-05 21:13:29 +00:00
|
|
|
ALLOC (16384, def_t, defs, def);
|
2001-06-04 05:45:51 +00:00
|
|
|
|
2002-06-09 03:57:20 +00:00
|
|
|
def->return_addr = __builtin_return_address (0);
|
2001-04-01 02:12:57 +00:00
|
|
|
|
2002-07-03 19:12:29 +00:00
|
|
|
def->name = name ? save_string (name) : 0;
|
2001-04-01 02:12:57 +00:00
|
|
|
def->type = type;
|
|
|
|
|
2011-02-18 00:04:33 +00:00
|
|
|
def->file = pr.source_file;
|
|
|
|
def->line = pr.source_line;
|
|
|
|
|
|
|
|
set_storage_bits (def, storage);
|
|
|
|
|
|
|
|
if (space) {
|
|
|
|
def->space = space;
|
|
|
|
*space->def_tail = def;
|
|
|
|
space->def_tail = &def->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!type)
|
|
|
|
return def;
|
|
|
|
|
|
|
|
if (!space && storage != st_extern)
|
|
|
|
internal_error (0, "non-external def with no storage space");
|
|
|
|
|
2011-01-24 06:41:43 +00:00
|
|
|
if (storage != st_extern) {
|
2011-02-13 10:26:20 +00:00
|
|
|
int size = type_size (type);
|
|
|
|
if (!size) {
|
|
|
|
error (0, "%s has incomplete type", name);
|
|
|
|
size = 1;
|
|
|
|
}
|
2011-02-24 04:07:36 +00:00
|
|
|
def->offset = defspace_alloc_loc (space, size);
|
2011-02-09 15:15:19 +00:00
|
|
|
}
|
2001-12-06 19:49:40 +00:00
|
|
|
|
2001-06-05 05:22:11 +00:00
|
|
|
return def;
|
2001-06-04 17:52:50 +00:00
|
|
|
}
|
|
|
|
|
2011-01-25 06:42:26 +00:00
|
|
|
def_t *
|
|
|
|
alias_def (def_t *def, type_t *type)
|
|
|
|
{
|
|
|
|
def_t *alias;
|
|
|
|
|
2012-05-03 04:27:30 +00:00
|
|
|
|
|
|
|
if (def->alias) {
|
|
|
|
expr_t e;
|
|
|
|
e.file = def->file;
|
|
|
|
e.line = def->line;
|
|
|
|
bug (&e, "aliasing an alias def");
|
|
|
|
}
|
2011-01-25 06:42:26 +00:00
|
|
|
ALLOC (16384, def_t, defs, alias);
|
2011-03-23 12:32:14 +00:00
|
|
|
alias->return_addr = __builtin_return_address (0);
|
2011-01-25 06:42:26 +00:00
|
|
|
alias->offset = def->offset;
|
|
|
|
alias->type = type;
|
|
|
|
alias->alias = def;
|
2012-05-03 04:27:30 +00:00
|
|
|
alias->line = pr.source_line;
|
|
|
|
alias->file = pr.source_file;
|
2011-01-25 06:42:26 +00:00
|
|
|
return alias;
|
|
|
|
}
|
|
|
|
|
2011-03-23 12:32:14 +00:00
|
|
|
def_t *
|
|
|
|
temp_def (etype_t type, int size)
|
|
|
|
{
|
|
|
|
def_t *temp;
|
|
|
|
defspace_t *space = current_func->symtab->space;
|
|
|
|
|
|
|
|
ALLOC (16384, def_t, defs, temp);
|
|
|
|
temp->return_addr = __builtin_return_address (0);
|
|
|
|
temp->name = save_string (va (".tmp%d", current_func->temp_num++));
|
|
|
|
temp->type = ev_types[type];
|
|
|
|
temp->file = pr.source_file;
|
|
|
|
temp->line = pr.source_line;
|
|
|
|
set_storage_bits (temp, st_local);
|
|
|
|
temp->offset = defspace_alloc_loc (space, size);
|
|
|
|
temp->space = space;
|
|
|
|
*space->def_tail = temp;
|
|
|
|
space->def_tail = &temp->next;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2001-06-04 17:52:50 +00:00
|
|
|
void
|
2011-01-24 06:41:43 +00:00
|
|
|
free_def (def_t *def)
|
2001-06-04 22:35:54 +00:00
|
|
|
{
|
2011-01-25 06:42:26 +00:00
|
|
|
if (!def->alias && def->space) {
|
2011-01-24 06:41:43 +00:00
|
|
|
def_t **d;
|
2001-08-22 21:55:01 +00:00
|
|
|
|
2011-01-24 06:41:43 +00:00
|
|
|
for (d = &def->space->defs; *d && *d != def; d = &(*d)->next)
|
|
|
|
;
|
2011-02-14 01:35:54 +00:00
|
|
|
if (!*d)
|
|
|
|
internal_error (0, "freeing unlinked def %s", def->name);
|
2011-01-24 06:41:43 +00:00
|
|
|
*d = def->next;
|
2011-02-14 01:35:54 +00:00
|
|
|
if (&def->next == def->space->def_tail)
|
|
|
|
def->space->def_tail = d;
|
2011-02-11 09:27:07 +00:00
|
|
|
if (!def->external)
|
|
|
|
defspace_free_loc (def->space, def->offset, type_size (def->type));
|
2002-08-20 06:11:10 +00:00
|
|
|
}
|
2011-01-24 06:41:43 +00:00
|
|
|
def->next = free_defs;
|
|
|
|
free_defs = def;
|
2002-06-28 16:00:01 +00:00
|
|
|
}
|
2002-07-17 15:40:08 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
def_to_ddef (def_t *def, ddef_t *ddef, int aux)
|
|
|
|
{
|
|
|
|
type_t *type = def->type;
|
|
|
|
|
|
|
|
if (aux)
|
2011-01-09 10:41:24 +00:00
|
|
|
type = type->t.fldptr.type; // aux is true only for fields
|
2002-07-17 15:40:08 +00:00
|
|
|
ddef->type = type->type;
|
2011-01-24 06:41:43 +00:00
|
|
|
ddef->ofs = def->offset;
|
2002-07-17 15:40:08 +00:00
|
|
|
ddef->s_name = ReuseString (def->name);
|
|
|
|
}
|
2011-01-25 03:07:02 +00:00
|
|
|
|
2011-02-15 00:30:37 +00:00
|
|
|
static void
|
|
|
|
init_elements (struct def_s *def, expr_t *eles)
|
|
|
|
{
|
|
|
|
expr_t *e, *c;
|
2011-02-22 00:22:04 +00:00
|
|
|
int count, i, num_elements, base_offset;
|
2011-02-15 00:30:37 +00:00
|
|
|
pr_type_t *g;
|
|
|
|
def_t *elements;
|
|
|
|
|
|
|
|
base_offset = def->offset;
|
|
|
|
if (def->local && local_expr)
|
|
|
|
base_offset = 0;
|
|
|
|
if (is_array (def->type)) {
|
|
|
|
type_t *array_type = def->type->t.array.type;
|
|
|
|
int array_size = def->type->t.array.size;
|
|
|
|
elements = calloc (array_size, sizeof (def_t));
|
|
|
|
for (i = 0; i < array_size; i++) {
|
|
|
|
elements[i].type = array_type;
|
|
|
|
elements[i].space = def->space;
|
|
|
|
elements[i].offset = base_offset + i * type_size (array_type);
|
|
|
|
}
|
2011-02-22 00:22:04 +00:00
|
|
|
num_elements = i;
|
2011-02-15 00:30:37 +00:00
|
|
|
} else if (is_struct (def->type)) {
|
|
|
|
symtab_t *symtab = def->type->t.symtab;
|
|
|
|
symbol_t *field;
|
|
|
|
|
|
|
|
for (i = 0, field = symtab->symbols; field; field = field->next) {
|
|
|
|
if (field->sy_type != sy_var)
|
|
|
|
continue;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
elements = calloc (i, sizeof (def_t));
|
|
|
|
for (i = 0, field = symtab->symbols; field; field = field->next) {
|
|
|
|
if (field->sy_type != sy_var)
|
|
|
|
continue;
|
|
|
|
elements[i].type = field->type;
|
|
|
|
elements[i].space = def->space;
|
|
|
|
elements[i].offset = base_offset + field->s.offset;
|
|
|
|
i++;
|
|
|
|
}
|
2011-02-22 00:22:04 +00:00
|
|
|
num_elements = i;
|
2011-02-15 00:30:37 +00:00
|
|
|
} else {
|
|
|
|
error (eles, "invalid initializer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (count = 0, e = eles->e.block.head; e; count++, e = e->next) {
|
|
|
|
convert_name (e);
|
2011-02-22 00:22:04 +00:00
|
|
|
if (e->type == ex_nil && count < num_elements)
|
2011-02-15 00:30:37 +00:00
|
|
|
convert_nil (e, elements[count].type);
|
|
|
|
if (e->type == ex_error) {
|
|
|
|
free (elements);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-02-22 00:22:04 +00:00
|
|
|
if (count > num_elements) {
|
2011-02-15 00:30:37 +00:00
|
|
|
if (options.warnings.initializer)
|
|
|
|
warning (eles, "excessive elements in initializer");
|
2011-02-22 00:22:04 +00:00
|
|
|
count = num_elements;
|
2011-02-15 00:30:37 +00:00
|
|
|
}
|
|
|
|
for (i = 0, e = eles->e.block.head; i < count; i++, e = e->next) {
|
|
|
|
g = D_POINTER (pr_type_t, &elements[i]);
|
|
|
|
c = constant_expr (e);
|
|
|
|
if (c->type == ex_block) {
|
|
|
|
if (!is_array (elements[i].type)
|
|
|
|
&& !is_struct (elements[i].type)) {
|
|
|
|
error (e, "type mismatch in initializer");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
init_elements (&elements[i], c);
|
2011-03-05 00:21:12 +00:00
|
|
|
continue;
|
2011-02-15 00:30:37 +00:00
|
|
|
} else if (c->type == ex_value) {
|
|
|
|
if (c->e.value.type == ev_integer
|
|
|
|
&& elements[i].type->type == ev_float)
|
|
|
|
convert_int (c);
|
|
|
|
if (get_type (c) != elements[i].type) {
|
|
|
|
error (e, "type mismatch in initializer");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!def->local || !local_expr) {
|
|
|
|
error (e, "non-constant initializer");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (def->local && local_expr) {
|
|
|
|
int offset = elements[i].offset;
|
|
|
|
type_t *type = elements[i].type;
|
|
|
|
expr_t *ptr = new_pointer_expr (offset, type, def);
|
|
|
|
|
|
|
|
append_expr (local_expr, assign_expr (unary_expr ('.', ptr), c));
|
|
|
|
} else {
|
2011-03-05 00:21:12 +00:00
|
|
|
if (c->type != ex_value)
|
|
|
|
internal_error (c, "bogus expression type in init_elements()");
|
2011-02-15 00:30:37 +00:00
|
|
|
if (c->e.value.type == ev_string) {
|
|
|
|
EMIT_STRING (def->space, g->string_var,
|
|
|
|
c->e.value.v.string_val);
|
|
|
|
} else {
|
2011-03-22 06:10:06 +00:00
|
|
|
memcpy (g, &c->e.value.v, type_size (get_type (c)) * 4);
|
2011-02-15 00:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (elements);
|
|
|
|
}
|
|
|
|
|
2011-03-07 01:21:40 +00:00
|
|
|
static void
|
|
|
|
init_vector_components (symbol_t *vector_sym, int is_field)
|
|
|
|
{
|
|
|
|
expr_t *vector_expr;
|
|
|
|
int i;
|
|
|
|
static const char *fields[] = { "x", "y", "z" };
|
|
|
|
|
|
|
|
vector_expr = new_symbol_expr (vector_sym);
|
|
|
|
for (i = 0; i < 3; i++) {
|
2011-03-22 04:05:42 +00:00
|
|
|
expr_t *expr = 0;
|
|
|
|
symbol_t *sym;
|
|
|
|
const char *name;
|
|
|
|
|
2011-03-07 08:23:52 +00:00
|
|
|
name = va ("%s_%s", vector_sym->name, fields[i]);
|
|
|
|
sym = symtab_lookup (current_symtab, name);
|
|
|
|
if (sym) {
|
|
|
|
if (sym->table == current_symtab) {
|
|
|
|
if (sym->sy_type != sy_expr) {
|
|
|
|
error (0, "%s redefined", name);
|
|
|
|
sym = 0;
|
|
|
|
} else {
|
|
|
|
expr = sym->s.expr;
|
|
|
|
if (is_field) {
|
|
|
|
if (expr->type != ex_value
|
|
|
|
|| expr->e.value.type != ev_field) {
|
|
|
|
error (0, "%s redefined", name);
|
|
|
|
sym = 0;
|
|
|
|
} else {
|
|
|
|
expr->e.value.v.pointer.def = vector_sym->s.def;
|
2011-03-22 04:05:42 +00:00
|
|
|
expr->e.value.v.pointer.val = i;
|
2011-03-07 08:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sym = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!sym)
|
|
|
|
sym = new_symbol (name);
|
|
|
|
if (!expr) {
|
|
|
|
if (is_field) {
|
|
|
|
expr = new_field_expr (i, &type_float, vector_sym->s.def);
|
|
|
|
} else {
|
|
|
|
expr = binary_expr ('.', vector_expr,
|
|
|
|
new_symbol_expr (new_symbol (fields[i])));
|
|
|
|
}
|
2011-03-07 01:21:40 +00:00
|
|
|
}
|
|
|
|
sym->sy_type = sy_expr;
|
|
|
|
sym->s.expr = expr;
|
2011-03-07 08:23:52 +00:00
|
|
|
if (!sym->table)
|
|
|
|
symtab_addsymbol (current_symtab, sym);
|
2011-03-07 01:21:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-22 01:43:35 +00:00
|
|
|
static void
|
2011-03-07 08:23:52 +00:00
|
|
|
init_field_def (def_t *def, expr_t *init, storage_class_t storage)
|
2011-02-22 01:43:35 +00:00
|
|
|
{
|
|
|
|
type_t *type = def->type->t.fldptr.type;
|
2011-02-22 06:43:15 +00:00
|
|
|
def_t *field_def;
|
2011-03-06 06:38:19 +00:00
|
|
|
symbol_t *field_sym;
|
2011-03-09 01:52:29 +00:00
|
|
|
reloc_t *relocs = 0;
|
2011-02-22 06:43:15 +00:00
|
|
|
|
2011-03-07 08:23:52 +00:00
|
|
|
if (!init) {
|
2011-03-30 00:33:47 +00:00
|
|
|
field_sym = symtab_lookup (pr.entity_fields, def->name);
|
|
|
|
if (!field_sym)
|
|
|
|
field_sym = new_symbol_type (def->name, type);
|
|
|
|
if (field_sym->s.def && field_sym->s.def->external) {
|
|
|
|
//FIXME this really is not the right way
|
|
|
|
relocs = field_sym->s.def->relocs;
|
|
|
|
free_def (field_sym->s.def);
|
|
|
|
field_sym->s.def = 0;
|
|
|
|
}
|
|
|
|
if (!field_sym->s.def) {
|
|
|
|
field_sym->s.def = new_def (def->name, type, pr.entity_data, storage);
|
|
|
|
field_sym->s.def->relocs = relocs;
|
|
|
|
field_sym->s.def->nosave = 1;
|
|
|
|
}
|
|
|
|
field_def = field_sym->s.def;
|
|
|
|
if (!field_sym->table)
|
|
|
|
symtab_addsymbol (pr.entity_fields, field_sym);
|
2011-03-07 08:23:52 +00:00
|
|
|
if (storage != st_extern) {
|
|
|
|
D_INT (def) = field_def->offset;
|
2011-03-30 00:33:47 +00:00
|
|
|
reloc_def_field (field_def, def);
|
2011-03-07 08:23:52 +00:00
|
|
|
def->constant = 1;
|
|
|
|
def->nosave = 1;
|
|
|
|
}
|
|
|
|
// no support for initialized field vector componets (yet?)
|
|
|
|
if (type == &type_vector && options.code.vector_components)
|
|
|
|
init_vector_components (field_sym, 1);
|
2011-03-30 00:33:47 +00:00
|
|
|
} else if (init->type == ex_symbol) {
|
|
|
|
symbol_t *sym = init->e.symbol;
|
|
|
|
symbol_t *field = symtab_lookup (pr.entity_fields, sym->name);
|
|
|
|
if (field) {
|
|
|
|
expr_t *new = new_field_expr (0, field->type, field->s.def);
|
|
|
|
init->type = new->type;
|
|
|
|
init->e = new->e;
|
|
|
|
}
|
2011-03-07 08:23:52 +00:00
|
|
|
}
|
2011-02-22 01:43:35 +00:00
|
|
|
}
|
|
|
|
|
2011-01-25 03:07:02 +00:00
|
|
|
void
|
|
|
|
initialize_def (symbol_t *sym, type_t *type, expr_t *init, defspace_t *space,
|
|
|
|
storage_class_t storage)
|
|
|
|
{
|
2011-02-14 12:25:08 +00:00
|
|
|
symbol_t *check = symtab_lookup (current_symtab, sym->name);
|
2011-03-09 01:52:29 +00:00
|
|
|
reloc_t *relocs = 0;
|
|
|
|
|
2011-02-14 06:44:01 +00:00
|
|
|
if (!type) {
|
|
|
|
warning (0, "type for %s defaults to %s", sym->name,
|
|
|
|
type_default->name);
|
|
|
|
type = type_default;
|
|
|
|
}
|
2011-02-14 12:25:08 +00:00
|
|
|
if (check && check->table == current_symtab) {
|
|
|
|
if (check->sy_type != sy_var || check->type != type) {
|
2011-01-25 03:07:02 +00:00
|
|
|
error (0, "%s redefined", sym->name);
|
|
|
|
} else {
|
|
|
|
// is var and same type
|
2011-02-14 12:25:08 +00:00
|
|
|
if (!check->s.def)
|
2011-01-25 03:07:02 +00:00
|
|
|
internal_error (0, "half defined var");
|
|
|
|
if (storage == st_extern) {
|
|
|
|
if (init)
|
|
|
|
warning (0, "initializing external variable");
|
|
|
|
return;
|
|
|
|
}
|
2011-02-14 12:25:08 +00:00
|
|
|
if (init && check->s.def->initialized) {
|
2011-01-25 03:07:02 +00:00
|
|
|
error (0, "%s redefined", sym->name);
|
|
|
|
return;
|
|
|
|
}
|
2011-02-14 12:25:08 +00:00
|
|
|
sym = check;
|
2011-01-25 03:07:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sym->type = type;
|
2011-02-14 12:25:08 +00:00
|
|
|
if (!sym->table)
|
|
|
|
symtab_addsymbol (current_symtab, sym);
|
2011-03-30 10:58:09 +00:00
|
|
|
// if (storage == st_global && init && is_scalar (type)) {
|
|
|
|
// sym->sy_type = sy_const;
|
|
|
|
// memset (&sym->s.value, 0, sizeof (&sym->s.value));
|
|
|
|
// if (init->type != ex_value) { //FIXME arrays/structs
|
|
|
|
// error (0, "non-constant initializier");
|
|
|
|
// } else {
|
|
|
|
// sym->s.value = init->e.value;
|
|
|
|
// convert_value (&sym->s.value, sym->type);
|
|
|
|
// }
|
|
|
|
// return;
|
|
|
|
// }
|
2011-01-25 03:07:02 +00:00
|
|
|
if (sym->s.def && sym->s.def->external) {
|
2011-03-09 01:52:29 +00:00
|
|
|
//FIXME this really is not the right way
|
|
|
|
relocs = sym->s.def->relocs;
|
2011-01-25 03:07:02 +00:00
|
|
|
free_def (sym->s.def);
|
|
|
|
sym->s.def = 0;
|
|
|
|
}
|
2011-03-09 01:52:29 +00:00
|
|
|
if (!sym->s.def) {
|
2011-01-25 03:07:02 +00:00
|
|
|
sym->s.def = new_def (sym->name, type, space, storage);
|
2011-03-09 01:52:29 +00:00
|
|
|
sym->s.def->relocs = relocs;
|
|
|
|
}
|
2011-03-07 01:21:40 +00:00
|
|
|
if (type == &type_vector && options.code.vector_components)
|
|
|
|
init_vector_components (sym, 0);
|
2011-03-08 10:56:02 +00:00
|
|
|
if (type->type == ev_field && storage != st_local)
|
2011-03-07 08:23:52 +00:00
|
|
|
init_field_def (sym->s.def, init, storage);
|
2011-01-25 03:07:02 +00:00
|
|
|
if (storage == st_extern) {
|
|
|
|
if (init)
|
|
|
|
warning (0, "initializing external variable");
|
|
|
|
return;
|
|
|
|
}
|
2011-03-07 08:23:52 +00:00
|
|
|
if (!init)
|
2011-01-25 03:07:02 +00:00
|
|
|
return;
|
2011-02-14 06:45:31 +00:00
|
|
|
convert_name (init);
|
|
|
|
if (init->type == ex_error)
|
|
|
|
return;
|
2011-02-10 05:51:52 +00:00
|
|
|
if (init->type == ex_nil)
|
|
|
|
convert_nil (init, type);
|
2011-03-02 13:28:17 +00:00
|
|
|
if ((is_array (type) || is_struct (type))
|
|
|
|
&& init->type == ex_block && !init->e.block.result) {
|
2011-02-15 00:30:37 +00:00
|
|
|
init_elements (sym->s.def, init);
|
2011-01-25 03:07:02 +00:00
|
|
|
} else {
|
2011-02-15 00:30:37 +00:00
|
|
|
if (!type_assignable (type, get_type (init))) {
|
|
|
|
error (init, "type mismatch in initializer");
|
2011-01-25 03:07:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-02-15 00:30:37 +00:00
|
|
|
if (local_expr) {
|
2011-03-05 08:23:22 +00:00
|
|
|
init = assign_expr (new_symbol_expr (sym), init);
|
|
|
|
// fold_constants takes care of int/float conversions
|
|
|
|
append_expr (local_expr, fold_constants (init));
|
2011-02-15 00:30:37 +00:00
|
|
|
} else {
|
|
|
|
if (init->type != ex_value) { //FIXME enum etc
|
|
|
|
error (0, "non-constant initializier");
|
|
|
|
return;
|
|
|
|
}
|
2011-02-22 01:43:35 +00:00
|
|
|
if (init->e.value.type == ev_pointer
|
|
|
|
|| init->e.value.type == ev_field) {
|
|
|
|
// FIXME offset pointers
|
|
|
|
D_INT (sym->s.def) = init->e.value.v.pointer.val;
|
2011-03-30 00:33:47 +00:00
|
|
|
if (init->e.value.v.pointer.def)
|
|
|
|
reloc_def_field (init->e.value.v.pointer.def, sym->s.def);
|
2011-02-22 01:43:35 +00:00
|
|
|
} else {
|
2011-03-30 12:21:38 +00:00
|
|
|
ex_value_t v = init->e.value;
|
|
|
|
if (is_scalar (sym->type))
|
|
|
|
convert_value (&v, sym->type);
|
|
|
|
memcpy (D_POINTER (void, sym->s.def), &v.v,
|
2011-02-22 01:43:35 +00:00
|
|
|
type_size (type) * sizeof (pr_type_t));
|
|
|
|
}
|
2011-03-22 06:10:06 +00:00
|
|
|
sym->s.def->initialized = sym->s.def->constant = 1;
|
|
|
|
sym->s.def->nosave = 1;
|
2011-02-15 00:30:37 +00:00
|
|
|
}
|
2011-01-25 03:07:02 +00:00
|
|
|
}
|
|
|
|
}
|