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>
|
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
|
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
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2002-06-01 04:41:25 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2002-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"
|
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"
|
2001-12-08 00:09:11 +00:00
|
|
|
#include "struct.h"
|
2002-05-15 23:24:19 +00:00
|
|
|
#include "type.h"
|
2001-12-08 00:09:11 +00:00
|
|
|
|
|
|
|
static hashtab_t *structs;
|
2002-01-21 19:03:29 +00:00
|
|
|
static hashtab_t *enums;
|
2001-12-08 00:09:11 +00:00
|
|
|
|
|
|
|
static const char *
|
|
|
|
structs_get_key (void *s, void *unused)
|
|
|
|
{
|
|
|
|
return ((struct_t *) s)->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
struct_field_get_key (void *f, void *unused)
|
|
|
|
{
|
|
|
|
return ((struct_field_t *) f)->name;
|
|
|
|
}
|
|
|
|
|
2002-01-21 19:03:29 +00:00
|
|
|
static const char *
|
|
|
|
enums_get_key (void *e, void *unused)
|
|
|
|
{
|
|
|
|
return ((enum_t *) e)->name;
|
|
|
|
}
|
|
|
|
|
2001-12-08 00:09:11 +00:00
|
|
|
struct_field_t *
|
2003-07-30 04:11:45 +00:00
|
|
|
new_struct_field (struct_t *strct, type_t *type, const char *name,
|
|
|
|
visibility_type visibility)
|
2001-12-08 00:09:11 +00:00
|
|
|
{
|
|
|
|
struct_field_t *field;
|
|
|
|
|
|
|
|
if (!strct)
|
|
|
|
return 0;
|
2003-08-01 02:43:51 +00:00
|
|
|
field = calloc (sizeof (struct_field_t), 1);
|
2002-05-10 00:00:23 +00:00
|
|
|
field->visibility = visibility;
|
2003-07-31 06:14:26 +00:00
|
|
|
if (name)
|
|
|
|
field->name = save_string (name);
|
2001-12-08 00:09:11 +00:00
|
|
|
field->type = type;
|
2003-07-30 04:11:45 +00:00
|
|
|
if (strct->stype == str_union) {
|
2002-06-20 20:28:01 +00:00
|
|
|
int size = type_size (type);
|
|
|
|
field->offset = 0;
|
2003-07-30 04:11:45 +00:00
|
|
|
if (size > strct->size)
|
|
|
|
strct->size = size;
|
2002-06-20 20:28:01 +00:00
|
|
|
} else {
|
2003-07-30 04:11:45 +00:00
|
|
|
field->offset = strct->size;;
|
|
|
|
strct->size += type_size (type);
|
2002-06-20 20:28:01 +00:00
|
|
|
}
|
2001-12-08 00:09:11 +00:00
|
|
|
field->next = 0;
|
|
|
|
*strct->struct_tail = field;
|
|
|
|
strct->struct_tail = &field->next;
|
2002-05-15 23:24:19 +00:00
|
|
|
if (name)
|
|
|
|
Hash_Add (strct->struct_fields, field);
|
2001-12-08 08:19:48 +00:00
|
|
|
return field;
|
2001-12-08 00:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct_field_t *
|
2003-07-30 04:11:45 +00:00
|
|
|
struct_find_field (struct_t *strct, const char *name)
|
2001-12-08 00:09:11 +00:00
|
|
|
{
|
2002-05-17 19:34:40 +00:00
|
|
|
if (!structs || !strct)
|
2001-12-08 00:09:11 +00:00
|
|
|
return 0;
|
|
|
|
return Hash_Find (strct->struct_fields, name);
|
|
|
|
}
|
|
|
|
|
2002-10-16 02:05:39 +00:00
|
|
|
type_t *
|
2003-07-30 04:11:45 +00:00
|
|
|
init_struct (struct_t *strct, type_t *type, struct_type stype,
|
|
|
|
const char *name)
|
2002-10-16 02:05:39 +00:00
|
|
|
{
|
2003-07-30 04:11:45 +00:00
|
|
|
if (name)
|
|
|
|
strct->name = save_string (name);
|
2002-10-16 02:05:39 +00:00
|
|
|
strct->type = type;
|
|
|
|
strct->type->type = ev_struct;
|
2003-07-30 04:11:45 +00:00
|
|
|
strct->struct_tail = &strct->struct_head;
|
|
|
|
strct->struct_fields = Hash_NewTable (61, struct_field_get_key, 0, 0);
|
|
|
|
strct->type->s.strct = strct;
|
|
|
|
strct->stype = stype;
|
2002-10-16 02:05:39 +00:00
|
|
|
if (name) {
|
2003-07-30 04:11:45 +00:00
|
|
|
strct->type->name = strct->name;
|
2002-10-16 02:05:39 +00:00
|
|
|
Hash_Add (structs, strct);
|
|
|
|
}
|
|
|
|
return strct->type;
|
|
|
|
}
|
|
|
|
|
2003-07-30 04:11:45 +00:00
|
|
|
struct_t *
|
|
|
|
get_struct (const char *name, int create)
|
2001-12-08 00:09:11 +00:00
|
|
|
{
|
2003-07-30 04:11:45 +00:00
|
|
|
struct_t *s;
|
2001-12-08 00:09:11 +00:00
|
|
|
|
2003-07-30 04:11:45 +00:00
|
|
|
if (!structs)
|
2001-12-08 00:09:11 +00:00
|
|
|
structs = Hash_NewTable (16381, structs_get_key, 0, 0);
|
2002-05-10 00:00:23 +00:00
|
|
|
if (name) {
|
2003-07-30 04:11:45 +00:00
|
|
|
s = Hash_Find (structs, name);
|
|
|
|
if (s || !create)
|
|
|
|
return s;
|
2001-12-08 00:09:11 +00:00
|
|
|
}
|
2003-07-30 04:11:45 +00:00
|
|
|
s = calloc (sizeof (struct_t), 1);
|
2003-07-31 06:14:26 +00:00
|
|
|
if (name)
|
|
|
|
s->name = save_string (name);
|
2003-07-30 04:11:45 +00:00
|
|
|
s->return_addr = __builtin_return_address (0);
|
|
|
|
if (name)
|
|
|
|
Hash_Add (structs, s);
|
|
|
|
return s;
|
2001-12-08 00:09:11 +00:00
|
|
|
}
|
2002-01-21 19:03:29 +00:00
|
|
|
|
2002-05-15 19:10:23 +00:00
|
|
|
int
|
2003-07-30 04:11:45 +00:00
|
|
|
struct_compare_fields (struct_t *s1, struct_t *s2)
|
2002-05-15 19:10:23 +00:00
|
|
|
{
|
|
|
|
struct_field_t *f1 = s1->struct_head;
|
|
|
|
struct_field_t *f2 = s2->struct_head;
|
|
|
|
|
|
|
|
while (f1 && f2) {
|
|
|
|
if (strcmp (f1->name, f2->name)
|
|
|
|
|| f1->type != f2->type)
|
|
|
|
return 0;
|
|
|
|
f1 = f1->next;
|
|
|
|
f2 = f2->next;
|
|
|
|
}
|
|
|
|
return !((!f1) ^ !(f2));
|
|
|
|
}
|
|
|
|
|
2003-07-30 04:11:45 +00:00
|
|
|
static struct_t *
|
|
|
|
start_struct (const char *name, struct_type stype)
|
|
|
|
{
|
|
|
|
struct_t *strct = get_struct (name, 1);
|
|
|
|
if (strct->struct_head) {
|
|
|
|
error (0, "%s redeclared", name);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (strct->stype != str_none && strct->stype != stype) {
|
|
|
|
error (0, "%s defined as wrong kind of tag", name);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!strct->type)
|
|
|
|
init_struct (strct, new_type (), stype, 0);
|
|
|
|
return strct;
|
|
|
|
err:
|
|
|
|
strct = get_struct (0, 0);
|
|
|
|
init_struct (strct, new_type (), stype, 0);
|
|
|
|
return strct;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct_t *
|
|
|
|
new_struct (const char *name)
|
|
|
|
{
|
|
|
|
return start_struct (name, str_struct);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct_t *
|
|
|
|
new_union (const char *name)
|
|
|
|
{
|
|
|
|
return start_struct (name, str_union);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct_t *
|
|
|
|
check_struct (const char *name, struct_type stype)
|
|
|
|
{
|
|
|
|
struct_t *strct = get_struct (name, 0);
|
|
|
|
|
|
|
|
if (!strct)
|
|
|
|
return start_struct (name, stype);
|
|
|
|
if (strct->stype != stype) {
|
|
|
|
error (0, "%s defined as wrong kind of tag", name);
|
|
|
|
strct = get_struct (0, 0);
|
|
|
|
init_struct (strct, new_type (), stype, 0);
|
|
|
|
}
|
|
|
|
return strct;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct_t *
|
|
|
|
decl_struct (const char *name)
|
|
|
|
{
|
|
|
|
|
|
|
|
return check_struct (name, str_struct);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct_t *
|
|
|
|
decl_union (const char *name)
|
|
|
|
{
|
|
|
|
return check_struct (name, str_union);
|
|
|
|
}
|
|
|
|
|
2002-07-13 06:09:03 +00:00
|
|
|
def_t *
|
2003-07-30 04:11:45 +00:00
|
|
|
emit_struct(struct_t *strct, const char *name)
|
2002-05-15 23:24:19 +00:00
|
|
|
{
|
|
|
|
struct_field_t *field;
|
|
|
|
int i, count;
|
|
|
|
def_t *ivars_def;
|
|
|
|
pr_ivar_list_t *ivars;
|
2003-07-30 04:11:45 +00:00
|
|
|
struct_t *ivar_list;
|
2002-05-15 23:24:19 +00:00
|
|
|
dstring_t *encoding = dstring_newstr ();
|
2002-05-16 20:20:23 +00:00
|
|
|
dstring_t *ivars_name = dstring_newstr ();
|
2002-05-15 23:24:19 +00:00
|
|
|
|
2002-05-16 20:20:23 +00:00
|
|
|
if (!strct)
|
|
|
|
return 0;
|
2002-05-15 23:24:19 +00:00
|
|
|
for (count = 0, field = strct->struct_head; field; field = field->next)
|
2002-05-21 21:28:40 +00:00
|
|
|
if (field->name)
|
|
|
|
count++;
|
2002-05-15 23:24:19 +00:00
|
|
|
ivar_list = new_struct (0);
|
|
|
|
new_struct_field (ivar_list, &type_integer, "ivar_count", vis_public);
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
new_struct_field (ivar_list, type_ivar, 0, vis_public);
|
2002-05-16 20:20:23 +00:00
|
|
|
dsprintf (ivars_name, "_OBJ_INSTANCE_VARIABLES_%s", name);
|
2003-07-30 04:11:45 +00:00
|
|
|
ivars_def = get_def (ivar_list->type, ivars_name->str, pr.scope, st_none);
|
2002-05-16 20:20:23 +00:00
|
|
|
if (ivars_def)
|
|
|
|
goto done;
|
2003-07-30 04:11:45 +00:00
|
|
|
ivars_def = get_def (ivar_list->type, ivars_name->str, pr.scope,
|
|
|
|
st_static);
|
2002-05-16 21:57:03 +00:00
|
|
|
ivars_def->initialized = ivars_def->constant = 1;
|
2003-04-25 17:00:22 +00:00
|
|
|
ivars_def->nosave = 1;
|
2002-05-15 23:24:19 +00:00
|
|
|
ivars = &G_STRUCT (pr_ivar_list_t, ivars_def->ofs);
|
|
|
|
ivars->ivar_count = count;
|
2002-05-21 21:28:40 +00:00
|
|
|
for (i = 0, field = strct->struct_head; field; field = field->next) {
|
|
|
|
if (!field->name)
|
|
|
|
continue;
|
2002-05-15 23:24:19 +00:00
|
|
|
encode_type (encoding, field->type);
|
2002-07-13 06:09:03 +00:00
|
|
|
EMIT_STRING (ivars->ivar_list[i].ivar_name, field->name);
|
|
|
|
EMIT_STRING (ivars->ivar_list[i].ivar_type, encoding->str);
|
2002-05-15 23:24:19 +00:00
|
|
|
ivars->ivar_list[i].ivar_offset = field->offset;
|
|
|
|
dstring_clearstr (encoding);
|
2002-05-21 21:28:40 +00:00
|
|
|
i++;
|
2002-05-15 23:24:19 +00:00
|
|
|
}
|
2002-05-16 20:20:23 +00:00
|
|
|
done:
|
2002-05-15 23:24:19 +00:00
|
|
|
dstring_delete (encoding);
|
2002-05-16 20:20:23 +00:00
|
|
|
dstring_delete (ivars_name);
|
2002-07-13 06:09:03 +00:00
|
|
|
return ivars_def;
|
2002-05-15 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
2002-06-28 16:00:01 +00:00
|
|
|
void
|
|
|
|
clear_structs (void)
|
|
|
|
{
|
|
|
|
if (structs)
|
|
|
|
Hash_FlushTable (structs);
|
|
|
|
}
|
|
|
|
|
2002-01-21 19:03:29 +00:00
|
|
|
void
|
|
|
|
process_enum (expr_t *enm)
|
|
|
|
{
|
|
|
|
expr_t *e = enm;
|
|
|
|
expr_t *c_enum = 0;
|
|
|
|
int enum_val = 0;
|
|
|
|
|
|
|
|
if (!enums) {
|
|
|
|
enums = Hash_NewTable (16381, enums_get_key, 0, 0);
|
|
|
|
}
|
|
|
|
while (e) {
|
|
|
|
expr_t *t = e->next;
|
|
|
|
e->next = c_enum;
|
|
|
|
c_enum = e;
|
|
|
|
e = t;
|
|
|
|
}
|
|
|
|
for (e = c_enum; e; e = e->next) {
|
|
|
|
expr_t *name = e;
|
|
|
|
expr_t *val = 0;
|
|
|
|
enum_t *new_enum;
|
|
|
|
|
|
|
|
if (name->type == ex_expr) {
|
|
|
|
val = name->e.expr.e2;
|
|
|
|
name = name->e.expr.e1;
|
|
|
|
}
|
2003-07-30 04:11:45 +00:00
|
|
|
if (get_enum (name->e.string_val)
|
2002-06-28 17:59:32 +00:00
|
|
|
|| get_def (NULL, name->e.string_val, pr.scope, st_none)) {
|
2002-01-21 19:03:29 +00:00
|
|
|
error (name, "%s redeclared", name->e.string_val);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (val)
|
|
|
|
enum_val = val->e.integer_val;
|
|
|
|
new_enum = malloc (sizeof (enum_t));
|
|
|
|
if (!new_enum)
|
|
|
|
Sys_Error ("out of memory");
|
|
|
|
new_enum->name = name->e.string_val;
|
2002-10-16 02:05:39 +00:00
|
|
|
new_enum->value = enum_val++;
|
2002-01-21 19:03:29 +00:00
|
|
|
Hash_Add (enums, new_enum);
|
|
|
|
//printf ("%s = %d\n", new_enum->name, new_enum->value.e.integer_val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
get_enum (const char *name)
|
|
|
|
{
|
|
|
|
enum_t *e;
|
|
|
|
|
|
|
|
if (!enums)
|
|
|
|
return 0;
|
|
|
|
e = (enum_t *) Hash_Find (enums, name);
|
|
|
|
if (!e)
|
|
|
|
return 0;
|
2002-10-16 02:05:39 +00:00
|
|
|
return new_integer_expr (e->value);
|
2002-01-21 19:03:29 +00:00
|
|
|
}
|
2002-06-28 16:00:01 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
clear_enums (void)
|
|
|
|
{
|
|
|
|
if (enums)
|
|
|
|
Hash_FlushTable (enums);
|
|
|
|
}
|