quakeforge/tools/qfcc/source/def.c

496 lines
10 KiB
C
Raw Normal View History

/* Copyright (C) 1996-1997 Id Software, Inc.
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 the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
See file, 'COPYING', for details.
*/
2001-09-28 07:09:38 +00:00
static const char rcsid[] =
"$Id$";
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
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>
#include <QF/hash.h>
#include <QF/sys.h>
2002-06-01 05:06:19 +00:00
#include <QF/va.h>
#include "qfcc.h"
2002-06-04 21:23:39 +00:00
#include "def.h"
#include "expr.h"
2002-07-17 15:40:08 +00:00
#include "immediate.h"
#include "reloc.h"
#include "strpool.h"
2002-01-21 19:03:29 +00:00
#include "struct.h"
#include "type.h"
typedef struct locref_s {
struct locref_s *next;
int ofs;
int size;
} locref_t;
2002-06-07 21:17:51 +00:00
def_t def_void = { &type_void, "temp" };
def_t def_function = { &type_function, "temp" };
def_t def_ret, def_parms[MAX_PARMS];
static def_t *free_temps[4]; // indexted by type size
static def_t temp_scope;
2002-06-05 19:03:51 +00:00
static def_t *free_defs;
static defspace_t *free_spaces;
static scope_t *free_scopes;
static locref_t *free_free_locs;
static hashtab_t *defs_by_name;
static const char *
defs_get_key (void *_def, void *_tab)
{
def_t *def = (def_t *) _def;
hashtab_t **tab = (hashtab_t **) _tab;
if (tab == &defs_by_name) {
return def->name;
}
return "";
}
static def_t *
check_for_name (type_t *type, const char *name, scope_t *scope,
storage_class_t storage)
{
def_t *def;
if (!defs_by_name) {
defs_by_name = Hash_NewTable (16381, defs_get_key, 0, &defs_by_name);
}
if (!name)
return 0;
if (scope->type == sc_global && (find_struct (name) || get_enum (name))) {
2002-01-21 19:03:29 +00:00
error (0, "%s redeclared", name);
return 0;
}
// see if the name is already in use
def = (def_t *) Hash_Find (defs_by_name, name);
if (def) {
if (storage != st_none && scope == def->scope)
if (type && def->type != type)
2001-08-10 18:35:55 +00:00
error (0, "Type mismatch on redeclaration of %s", name);
if (storage == st_none || def->scope == scope)
return def;
}
return 0;
}
static int
grow_space (defspace_t *space)
{
space->max_size += 1024;
return 1;
}
defspace_t *
new_defspace (void)
{
defspace_t *space;
ALLOC (1024, defspace_t, spaces, space);
space->grow = grow_space;
return space;
}
2002-07-11 15:42:20 +00:00
void
defspace_adddata (defspace_t *space, pr_type_t *data, int size)
{
if (space->size + size > space->max_size) {
space->max_size = (space->size + size + 1023) & ~1023;
space->data = realloc (space->data,
space->max_size * sizeof (pr_type_t));
}
if (data)
memcpy (space->data + space->size, data, size * sizeof (pr_type_t));
space->size += size;
2002-07-11 15:42:20 +00:00
}
scope_t *
new_scope (scope_type type, defspace_t *space, scope_t *parent)
{
scope_t *scope;
ALLOC (1024, scope_t, scopes, scope);
scope->type = type;
scope->space = space;
scope->parent = parent;
scope->tail = &scope->head;
return scope;
}
2002-06-17 02:43:38 +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) {
case st_none:
break;
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
}
2002-07-02 21:34:40 +00:00
static const char *vector_component_names[] = {"%s_x", "%s_y", "%s_z"};
2002-06-17 02:43:38 +00:00
static void
2002-07-02 21:34:40 +00:00
vector_component (int is_field, def_t *vec, int comp, scope_t *scope,
storage_class_t storage)
2002-06-17 02:43:38 +00:00
{
def_t *d;
const char *name;
2002-06-17 02:43:38 +00:00
name = save_string (va (vector_component_names[comp], vec->name));
2002-07-02 21:34:40 +00:00
if (vec->external) {
d = get_def (is_field ? &type_floatfield : &type_float, name, scope,
st_none);
if (d) {
2002-07-02 21:34:40 +00:00
error (0, "internal error");
abort ();
}
}
d = new_def (is_field ? &type_floatfield : &type_float, name, scope);
2002-07-02 21:34:40 +00:00
d->used = 1;
2002-06-17 02:43:38 +00:00
d->parent = vec;
d->ofs = vec->ofs + comp;
2002-07-02 21:34:40 +00:00
set_storage_bits (d, storage);
if (is_field && (storage == st_global || storage == st_static)) {
2002-07-02 21:34:40 +00:00
G_INT (d->ofs) = G_INT (vec->ofs) + comp;
reloc_def_field (d, d->ofs);
}
2002-06-17 02:43:38 +00:00
Hash_Add (defs_by_name, d);
}
/*
get_def
If type is NULL, it will match any type
If allocate is true, a new def will be allocated if it can't be found
*/
def_t *
get_def (type_t *type, const char *name, scope_t *scope,
storage_class_t storage)
{
def_t *def = check_for_name (type, name, scope, storage);
defspace_t *space = NULL;
2002-07-02 21:34:40 +00:00
if (storage == st_none)
return def;
2002-07-02 21:34:40 +00:00
if (def) {
if (!def->external || storage == st_extern)
2002-07-02 21:34:40 +00:00
return def;
} else {
// allocate a new def
def = new_def (type, name, scope);
if (name)
Hash_Add (defs_by_name, def);
}
2002-07-02 21:34:40 +00:00
switch (storage) {
case st_none:
case st_global:
case st_local:
space = scope->space;
break;
case st_extern:
space = 0;
break;
case st_static:
space = pr.near_data;
break;
}
if (space) {
if (type->type == ev_field && type->aux_type == &type_vector)
def->ofs = new_location (type->aux_type, space);
else
def->ofs = new_location (type, space);
}
set_storage_bits (def, storage);
// make automatic defs for the vectors elements .origin can be accessed
// as .origin_x, .origin_y, and .origin_z
if (type->type == ev_vector && name) {
2002-07-02 21:34:40 +00:00
vector_component (0, def, 0, scope, storage);
vector_component (0, def, 1, scope, storage);
vector_component (0, def, 2, scope, storage);
}
if (type->type == ev_field) {
if (storage == st_global || storage == st_static) {
2002-07-02 21:34:40 +00:00
G_INT (def->ofs) = new_location (type->aux_type, pr.entity_data);
reloc_def_field (def, def->ofs);
}
if (type->aux_type->type == ev_vector) {
2002-07-02 21:34:40 +00:00
vector_component (1, def, 0, scope, storage);
vector_component (1, def, 1, scope, storage);
vector_component (1, def, 2, scope, storage);
}
}
if (storage == st_extern)
2002-07-02 23:08:44 +00:00
def_initialized (def);
return def;
}
def_t *
new_def (type_t *type, const char *name, scope_t *scope)
{
def_t *def;
2002-06-05 21:13:29 +00:00
ALLOC (16384, def_t, defs, def);
if (scope) {
*scope->tail = def;
scope->tail = &def->def_next;
scope->num_defs++;
}
def->return_addr = __builtin_return_address (0);
def->name = name ? save_string (name) : 0;
def->type = type;
if (scope) {
def->scope = scope;
def->space = scope->space;
}
def->file = pr.source_file;
def->line = pr.source_line;
2001-09-22 23:41:47 +00:00
return def;
}
int
new_location (type_t *type, defspace_t *space)
{
int size = type_size (type);
int ofs;
locref_t *loc;
locref_t **l = &space->free_locs;
while (*l && (*l)->size < size)
l = &(*l)->next;
if ((loc = *l)) {
ofs = loc->ofs;
if (loc->size == size) {
*l = loc->next;
} else {
loc->ofs += size;
loc->size -= size;
}
return ofs;
}
ofs = space->size;
space->size += size;
if (space->size > space->max_size)
space->grow (space);
return ofs;
}
void
free_location (def_t *def)
{
2002-06-05 19:31:43 +00:00
int size = type_size (def->type);
locref_t *loc;
for (loc = def->space->free_locs; loc; loc = loc->next) {
if (def->ofs + size == loc->ofs) {
loc->size += size;
loc->ofs = def->ofs;
return;
} else if (loc->ofs + loc->size == def->ofs) {
loc->size += size;
if (loc->next && loc->next->ofs == loc->ofs + loc->size) {
loc->size += loc->next->size;
loc->next = loc->next->next;
}
return;
}
}
2002-06-05 21:13:29 +00:00
ALLOC (1024, locref_t, free_locs, loc);
loc->ofs = def->ofs;
loc->size = size;
loc->next = def->space->free_locs;
def->space->free_locs = loc;
}
def_t *
get_tempdef (type_t *type, scope_t *scope)
{
2002-06-05 19:31:43 +00:00
int size = type_size (type);
def_t *def;
if (free_temps[size]) {
def = free_temps[size];
free_temps[size] = def->next;
def->type = type;
} else {
def = new_def (type, 0, scope);
def->ofs = new_location (type, scope->space);
}
def->freed = def->removed = def->users = 0;
def->next = temp_scope.next;
2002-07-12 06:17:24 +00:00
set_storage_bits (def, st_local);
temp_scope.next = def;
return def;
}
void
free_tempdefs (void)
{
def_t **def, *d;
int size;
def = &temp_scope.next;
while (*def) {
if ((*def)->users <= 0) {
d = *def;
*def = d->next;
if (d->users < 0)
printf ("%s:%d: warning: %s %3d %3d %d\n", G_GETSTR (d->file),
d->line, pr_type_name[d->type->type], d->ofs, d->users,
d->managed);
2002-06-05 19:31:43 +00:00
size = type_size (d->type);
if (d->expr)
d->expr->e.temp.def = 0;
if (!d->freed) {
d->next = free_temps[size];
free_temps[size] = d;
d->freed = 1;
}
} else {
def = &(*def)->next;
}
}
}
void
reset_tempdefs (void)
{
int i;
def_t *d;
for (i = 0; i < sizeof (free_temps) / sizeof (free_temps[0]); i++) {
free_temps[i] = 0;
}
for (d = temp_scope.next; d; d = d->next)
printf ("%s:%d: warning: %s %3d %3d %d\n", G_GETSTR (d->file), d->line,
pr_type_name[d->type->type], d->ofs, d->users, d->managed);
temp_scope.next = 0;
}
void
flush_scope (scope_t *scope, int force_used)
{
def_t *def;
for (def = scope->head; def; def = def->def_next) {
2001-09-22 23:41:47 +00:00
if (def->name) {
if (!force_used && !def->used) {
expr_t e;
2001-09-22 23:41:47 +00:00
e.line = def->line;
e.file = def->file;
2001-09-24 17:51:15 +00:00
warning (&e, "unused variable `%s'", def->name);
2001-09-22 23:41:47 +00:00
}
2001-10-24 06:39:49 +00:00
if (!def->removed) {
Hash_Del (defs_by_name, def->name);
def->removed = 1;
}
2001-09-22 23:41:47 +00:00
}
}
}
void
def_initialized (def_t *d)
{
d->initialized = 1;
if (d->type == &type_vector
|| (d->type->type == ev_field && d->type->aux_type == &type_vector)) {
d = d->def_next;
d->initialized = 1;
d = d->def_next;
d->initialized = 1;
d = d->def_next;
d->initialized = 1;
}
if (d->parent) {
d = d->parent;
if (d->type == &type_vector
&& d->def_next->initialized
&& d->def_next->def_next->initialized
&& d->def_next->def_next->def_next->initialized)
d->initialized = 1;
}
}
void
clear_defs (void)
{
if (defs_by_name)
Hash_FlushTable (defs_by_name);
}
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)
type = type->aux_type;
ddef->type = type->type;
ddef->ofs = def->ofs;
ddef->s_name = ReuseString (def->name);
}