2001-12-08 20:40:50 +00:00
|
|
|
/*
|
2002-10-22 14:53:18 +00:00
|
|
|
struct.c
|
2001-12-08 20:40:50 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
structure support
|
2001-12-08 20:40:50 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Copyright (C) 2001 Bill Currie <bill@taniwha.org>
|
2011-01-24 23:54:02 +00:00
|
|
|
Copyright (C) 2011 Bill Currie <bill@taniwha.org>
|
2001-12-08 20:40:50 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2001/12/08
|
2011-01-24 23:54:02 +00:00
|
|
|
Date: 2011/01/17
|
2001-12-08 20:40:50 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
2002-06-01 04:41:25 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-01-15 15:31:36 +00:00
|
|
|
"$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-05-15 23:24:19 +00:00
|
|
|
#include <QF/dstring.h>
|
2001-12-08 00:09:11 +00:00
|
|
|
#include <QF/hash.h>
|
2002-05-15 23:24:19 +00:00
|
|
|
#include <QF/pr_obj.h>
|
2002-01-21 19:03:29 +00:00
|
|
|
#include <QF/sys.h>
|
2002-05-15 23:24:19 +00:00
|
|
|
#include <QF/va.h>
|
2001-12-08 00:09:11 +00:00
|
|
|
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "def.h"
|
2011-01-24 06:41:43 +00:00
|
|
|
#include "defspace.h"
|
2011-01-24 12:54:57 +00:00
|
|
|
#include "diagnostic.h"
|
2002-07-13 06:09:03 +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-13 06:09:03 +00:00
|
|
|
#include "qfcc.h"
|
|
|
|
#include "reloc.h"
|
2011-01-17 13:33:33 +00:00
|
|
|
#include "strpool.h"
|
2001-12-08 00:09:11 +00:00
|
|
|
#include "struct.h"
|
2011-01-17 13:33:33 +00:00
|
|
|
#include "symtab.h"
|
2002-05-15 23:24:19 +00:00
|
|
|
#include "type.h"
|
2001-12-08 00:09:11 +00:00
|
|
|
|
2011-01-17 13:33:33 +00:00
|
|
|
static symbol_t *
|
|
|
|
find_tag (ty_type_e ty, symbol_t *tag, type_t *type)
|
|
|
|
{
|
|
|
|
const char *tag_name;
|
|
|
|
symbol_t *sym;
|
|
|
|
static int tag_num;
|
|
|
|
|
|
|
|
if (tag)
|
|
|
|
tag_name = va ("tag %s", tag->name);
|
|
|
|
else
|
|
|
|
tag_name = va ("tag .%s.%d.%d",
|
2011-01-24 06:41:43 +00:00
|
|
|
GETSTR (pr.source_file), pr.source_line, tag_num++);
|
2011-01-17 13:33:33 +00:00
|
|
|
sym = symtab_lookup (current_symtab, tag_name);
|
|
|
|
if (sym) {
|
|
|
|
if (sym->table == current_symtab && sym->type->ty != ty)
|
|
|
|
error (0, "%s defined as wrong kind of tag", tag->name);
|
|
|
|
if (sym->type->ty == ty)
|
|
|
|
return sym;
|
2002-06-20 20:28:01 +00:00
|
|
|
}
|
2011-01-17 13:33:33 +00:00
|
|
|
sym = new_symbol (tag_name);
|
2011-02-07 11:48:01 +00:00
|
|
|
if (!type)
|
2011-02-03 01:52:12 +00:00
|
|
|
type = new_type ();
|
2011-02-07 11:48:01 +00:00
|
|
|
if (!type->name)
|
|
|
|
type->name = sym->name;
|
2011-01-17 13:33:33 +00:00
|
|
|
sym->type = type;
|
|
|
|
sym->type->type = ev_invalid;
|
|
|
|
sym->type->ty = ty;
|
2011-02-07 06:30:49 +00:00
|
|
|
sym->sy_type = sy_type;
|
2011-01-17 13:33:33 +00:00
|
|
|
return sym;
|
2001-12-08 00:09:11 +00:00
|
|
|
}
|
|
|
|
|
2011-01-17 13:33:33 +00:00
|
|
|
symbol_t *
|
|
|
|
find_struct (int su, symbol_t *tag, type_t *type)
|
2002-10-16 02:05:39 +00:00
|
|
|
{
|
2011-01-17 13:33:33 +00:00
|
|
|
ty_type_e ty = ty_struct;
|
2002-10-16 02:05:39 +00:00
|
|
|
|
2011-01-17 13:33:33 +00:00
|
|
|
if (su == 'u')
|
|
|
|
ty = ty_union;
|
2001-12-08 00:09:11 +00:00
|
|
|
|
2011-01-17 13:33:33 +00:00
|
|
|
return find_tag (ty, tag, type);
|
2001-12-08 00:09:11 +00:00
|
|
|
}
|
2002-01-21 19:03:29 +00:00
|
|
|
|
2011-01-17 13:33:33 +00:00
|
|
|
symbol_t *
|
|
|
|
build_struct (int su, symbol_t *tag, symtab_t *symtab, type_t *type)
|
2002-05-15 19:10:23 +00:00
|
|
|
{
|
2011-01-17 13:33:33 +00:00
|
|
|
symbol_t *sym = find_struct (su, tag, type);
|
2011-01-17 23:59:44 +00:00
|
|
|
symbol_t *s;
|
2002-05-15 19:10:23 +00:00
|
|
|
|
2011-01-17 13:33:33 +00:00
|
|
|
symtab->parent = 0; // disconnect struct's symtab from parent scope
|
2002-05-15 19:10:23 +00:00
|
|
|
|
2011-01-17 13:33:33 +00:00
|
|
|
if (sym->table == current_symtab && sym->type->t.symtab) {
|
|
|
|
error (0, "%s defined as wrong kind of tag", tag->name);
|
|
|
|
return sym;
|
2003-07-30 04:11:45 +00:00
|
|
|
}
|
2011-01-17 23:59:44 +00:00
|
|
|
for (s = symtab->symbols; s; s = s->next) {
|
|
|
|
if (s->sy_type != sy_var)
|
2011-01-17 13:33:33 +00:00
|
|
|
continue;
|
|
|
|
if (su == 's') {
|
2011-01-19 02:09:54 +00:00
|
|
|
s->s.offset = symtab->size;
|
2011-01-17 23:59:44 +00:00
|
|
|
symtab->size += type_size (s->type);
|
2011-01-17 13:33:33 +00:00
|
|
|
} else {
|
2011-01-17 23:59:44 +00:00
|
|
|
int size = type_size (s->type);
|
2011-01-17 13:33:33 +00:00
|
|
|
if (size > symtab->size)
|
|
|
|
symtab->size = size;
|
|
|
|
}
|
2003-07-30 04:11:45 +00:00
|
|
|
}
|
2011-01-17 13:33:33 +00:00
|
|
|
sym->type->t.symtab = symtab;
|
2011-02-06 11:07:19 +00:00
|
|
|
sym->type = find_type (sym->type);
|
2011-01-17 13:33:33 +00:00
|
|
|
return sym;
|
2003-07-30 04:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-01-17 13:33:33 +00:00
|
|
|
symbol_t *
|
|
|
|
find_enum (symbol_t *tag)
|
2003-07-30 04:11:45 +00:00
|
|
|
{
|
2011-01-17 13:33:33 +00:00
|
|
|
return find_tag (ty_enum, tag, 0);
|
2003-07-30 04:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-01-17 13:33:33 +00:00
|
|
|
symtab_t *
|
|
|
|
start_enum (symbol_t *sym)
|
2002-05-15 23:24:19 +00:00
|
|
|
{
|
2011-01-17 13:33:33 +00:00
|
|
|
if (sym->table == current_symtab && sym->type->t.symtab) {
|
|
|
|
error (0, "%s defined as wrong kind of tag", sym->name);
|
|
|
|
sym = find_enum (0);
|
2002-05-15 23:24:19 +00:00
|
|
|
}
|
2011-01-17 13:33:33 +00:00
|
|
|
sym->type->t.symtab = new_symtab (current_symtab, stab_local);
|
2011-02-06 11:07:19 +00:00
|
|
|
sym->type = find_type (sym->type);
|
2011-01-17 13:33:33 +00:00
|
|
|
return sym->type->t.symtab;
|
2002-05-15 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
2002-06-28 16:00:01 +00:00
|
|
|
void
|
2011-01-17 13:33:33 +00:00
|
|
|
add_enum (symbol_t *enm, symbol_t *name, expr_t *val)
|
|
|
|
{
|
2011-02-06 11:05:56 +00:00
|
|
|
symbol_t *sym;
|
2011-01-17 13:33:33 +00:00
|
|
|
type_t *enum_type = enm->type;
|
|
|
|
symtab_t *enum_tab;
|
|
|
|
int value;
|
|
|
|
|
|
|
|
if (name->table == current_symtab)
|
|
|
|
error (0, "%s redefined", name->name);
|
|
|
|
if (name->table)
|
|
|
|
name = new_symbol (name->name);
|
|
|
|
name->sy_type = sy_const;
|
|
|
|
name->type = enum_type;
|
|
|
|
enum_tab = enum_type->t.symtab;
|
|
|
|
value = 0;
|
|
|
|
if (*enum_tab->symtail)
|
2011-01-19 02:09:54 +00:00
|
|
|
value = ((symbol_t *)(*enum_tab->symtail))->s.value.v.integer_val + 1;
|
2011-01-17 13:33:33 +00:00
|
|
|
if (val) {
|
2011-01-18 23:41:24 +00:00
|
|
|
if (!is_constant (val))
|
2011-01-17 13:33:33 +00:00
|
|
|
error (val, "non-constant initializer");
|
2011-01-18 23:41:24 +00:00
|
|
|
else if (!is_integer_val (val))
|
2011-01-17 13:33:33 +00:00
|
|
|
error (val, "invalid initializer type");
|
|
|
|
else
|
2011-01-18 23:41:24 +00:00
|
|
|
value = expr_integer (val);
|
2002-01-21 19:03:29 +00:00
|
|
|
}
|
2011-02-06 11:05:56 +00:00
|
|
|
name->s.value.type = ev_integer;
|
2011-01-19 02:09:54 +00:00
|
|
|
name->s.value.v.integer_val = value;
|
2011-01-17 13:33:33 +00:00
|
|
|
symtab_addsymbol (enum_tab, name);
|
2011-02-06 11:05:56 +00:00
|
|
|
sym = new_symbol_type (name->name, name->type);
|
|
|
|
sym->sy_type = sy_const;
|
|
|
|
sym->s.value = name->s.value;
|
|
|
|
symtab_addsymbol (enum_tab->parent, sym);
|
2011-01-17 13:33:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
symbol_t *
|
|
|
|
make_structure (const char *name, int su, struct_def_t *defs, type_t *type)
|
|
|
|
{
|
|
|
|
symtab_t *strct;
|
|
|
|
symbol_t *field;
|
2011-01-17 23:56:47 +00:00
|
|
|
symbol_t *sym = 0;
|
2011-01-17 13:33:33 +00:00
|
|
|
|
2011-01-24 06:41:43 +00:00
|
|
|
if (name)
|
|
|
|
sym = new_symbol (name);
|
2011-01-17 13:33:33 +00:00
|
|
|
if (su == 'u')
|
|
|
|
strct = new_symtab (0, stab_union);
|
|
|
|
else
|
|
|
|
strct = new_symtab (0, stab_struct);
|
|
|
|
while (defs->name) {
|
|
|
|
field = new_symbol_type (defs->name, defs->type);
|
|
|
|
if (!symtab_addsymbol (strct, field))
|
|
|
|
internal_error (0, "duplicate symbol: %s", defs->name);
|
|
|
|
defs++;
|
2002-01-21 19:03:29 +00:00
|
|
|
}
|
2011-01-17 23:56:47 +00:00
|
|
|
sym = build_struct (su, sym, strct, type);
|
2011-01-17 13:33:33 +00:00
|
|
|
return sym;
|
2002-06-28 16:00:01 +00:00
|
|
|
}
|
2011-01-24 06:41:43 +00:00
|
|
|
|
|
|
|
def_t *
|
|
|
|
emit_structure (const char *name, int su, struct_def_t *defs, type_t *type,
|
|
|
|
void *data, storage_class_t storage)
|
|
|
|
{
|
|
|
|
type_t new;
|
|
|
|
int i, j;
|
|
|
|
int saw_null = 0;
|
|
|
|
int saw_func = 0;
|
|
|
|
symbol_t *struct_sym;
|
|
|
|
symbol_t *field_sym;
|
|
|
|
def_t *struct_def;
|
|
|
|
def_t field_def;
|
|
|
|
|
2011-02-09 08:12:47 +00:00
|
|
|
name = save_string (name);
|
2011-01-24 06:41:43 +00:00
|
|
|
if (!type) {
|
2011-02-07 11:48:01 +00:00
|
|
|
memset (&new, 0, sizeof (new));
|
2011-01-24 06:41:43 +00:00
|
|
|
type = &new;
|
2011-02-09 08:15:29 +00:00
|
|
|
type = make_structure (0, su, defs, type)->type;
|
2011-01-24 06:41:43 +00:00
|
|
|
}
|
|
|
|
if (!is_struct (type) || (su == 's' && type->ty != ty_struct)
|
|
|
|
|| (su == 'u' && type->ty != ty_union))
|
|
|
|
internal_error (0, "structure %s type mismatch", name);
|
|
|
|
for (i = 0, field_sym = type->t.symtab->symbols; field_sym;
|
|
|
|
i++, field_sym = field_sym->next) {
|
|
|
|
if (!defs[i].name)
|
|
|
|
internal_error (0, "structure %s unexpected end of defs", name);
|
|
|
|
if (field_sym->type != defs[i].type)
|
|
|
|
internal_error (0, "structure %s.%s field type mismatch", name,
|
|
|
|
defs[i].name);
|
|
|
|
if ((!defs[i].emit && saw_func) || (defs[i].emit && saw_null))
|
|
|
|
internal_error (0, "structure %s mixed emit/copy", name);
|
|
|
|
if (!defs[i].emit)
|
|
|
|
saw_null = 1;
|
|
|
|
if (defs[i].emit)
|
|
|
|
saw_func = 1;
|
|
|
|
}
|
|
|
|
if (defs[i].name)
|
|
|
|
internal_error (0, "structure %s too many defs", name);
|
|
|
|
if (storage != st_global && storage != st_static)
|
|
|
|
internal_error (0, "structure %s must be global or static", name);
|
|
|
|
|
|
|
|
struct_sym = make_symbol (name, type, pr.far_data, storage);
|
|
|
|
|
|
|
|
struct_def = struct_sym->s.def;
|
|
|
|
if (struct_def->initialized)
|
|
|
|
internal_error (0, "structure %s must be global or static", name);
|
|
|
|
struct_def->initialized = struct_def->constant = 1;
|
|
|
|
struct_def->nosave = 1;
|
|
|
|
|
|
|
|
for (i = 0, field_sym = type->t.symtab->symbols; field_sym;
|
|
|
|
i++, field_sym = field_sym->next) {
|
|
|
|
field_def.type = field_sym->type;
|
|
|
|
field_def.name = save_string (va ("%s.%s", name, field_sym->name));
|
|
|
|
field_def.space = struct_def->space;
|
|
|
|
field_def.offset = struct_def->offset + field_sym->s.offset;
|
|
|
|
if (!defs[i].emit) {
|
|
|
|
//FIXME relocs? arrays? structs?
|
|
|
|
pr_type_t *val = (pr_type_t *) data;
|
|
|
|
memcpy (D_POINTER (void, &field_def), val,
|
|
|
|
type_size (field_def.type) * sizeof (pr_type_t));
|
|
|
|
data = &val[type_size (field_def.type)];
|
|
|
|
} else {
|
|
|
|
if (is_array (field_def.type)) {
|
|
|
|
for (j = 0; j < field_def.type->t.array.size; j++) {
|
2011-02-03 23:25:27 +00:00
|
|
|
defs[i].emit (&field_def, data, j);
|
2011-01-24 06:41:43 +00:00
|
|
|
field_def.offset+=type_size (field_def.type->t.array.type);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
defs[i].emit (&field_def, data, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return struct_def;
|
|
|
|
}
|