better global (and other space) def re-use

This commit is contained in:
Bill Currie 2002-06-10 23:14:32 +00:00
parent bc4c4588cc
commit f7d8c2968f
5 changed files with 65 additions and 65 deletions

View file

@ -60,6 +60,7 @@ typedef struct def_s {
struct def_s *def_next; // next def in scope struct def_s *def_next; // next def in scope
struct def_s *next; // general purpose linking struct def_s *next; // general purpose linking
struct scope_s *scope; // scope the var was defined in struct scope_s *scope; // scope the var was defined in
struct defspace_s *space;
struct def_s *parent; // vector/quaternion member struct def_s *parent; // vector/quaternion member
void *return_addr; // who allocated this void *return_addr; // who allocated this
@ -67,6 +68,7 @@ typedef struct def_s {
typedef struct defspace_s { typedef struct defspace_s {
struct defspace_s *next; struct defspace_s *next;
struct locref_s *free_locs;
pr_type_t *data; pr_type_t *data;
int size; int size;
int max_size; int max_size;

View file

@ -63,9 +63,8 @@ typedef struct {
struct defspace_s *near_data; struct defspace_s *near_data;
struct defspace_s *far_data; struct defspace_s *far_data;
struct defspace_s *entity_data;
struct scope_s *scope; struct scope_s *scope;
int size_fields;
} pr_info_t; } pr_info_t;
extern pr_info_t pr; extern pr_info_t pr;

View file

@ -43,6 +43,7 @@ static const char rcsid[] =
typedef struct locref_s { typedef struct locref_s {
struct locref_s *next; struct locref_s *next;
int ofs; int ofs;
int size;
} locref_t; } locref_t;
def_t def_void = { &type_void, "temp" }; def_t def_void = { &type_void, "temp" };
@ -55,7 +56,6 @@ static def_t temp_scope;
static def_t *free_defs; static def_t *free_defs;
static defspace_t *free_spaces; static defspace_t *free_spaces;
static scope_t *free_scopes; static scope_t *free_scopes;
static locref_t *free_locs[4]; // indexted by type size
static locref_t *free_free_locs; static locref_t *free_free_locs;
static hashtab_t *defs_by_name; static hashtab_t *defs_by_name;
@ -138,7 +138,6 @@ def_t *
get_def (type_t *type, const char *name, scope_t *scope, int allocate) get_def (type_t *type, const char *name, scope_t *scope, int allocate)
{ {
def_t *def = check_for_name (type, name, scope, allocate); def_t *def = check_for_name (type, name, scope, allocate);
int size;
if (def || !allocate) if (def || !allocate)
return def; return def;
@ -148,8 +147,7 @@ get_def (type_t *type, const char *name, scope_t *scope, int allocate)
if (name) if (name)
Hash_Add (defs_by_name, def); Hash_Add (defs_by_name, def);
// FIXME: need to sort out location re-use def->ofs = new_location (type, scope->space);
def->ofs = scope->space->size;
/* /*
make automatic defs for the vectors elements .origin can be accessed make automatic defs for the vectors elements .origin can be accessed
@ -158,73 +156,49 @@ get_def (type_t *type, const char *name, scope_t *scope, int allocate)
if (type->type == ev_vector && name) { if (type->type == ev_vector && name) {
def_t *d; def_t *d;
d = get_def (&type_float, va ("%s_x", name), scope, allocate); d = new_def (&type_float, va ("%s_x", name), scope);
d->used = 1; d->used = 1;
d->parent = def; d->parent = def;
d->ofs = def->ofs + 0;
Hash_Add (defs_by_name, d);
d = get_def (&type_float, va ("%s_y", name), scope, allocate); d = new_def (&type_float, va ("%s_y", name), scope);
d->used = 1; d->used = 1;
d->parent = def; d->parent = def;
d->ofs = def->ofs + 3;
Hash_Add (defs_by_name, d);
d = get_def (&type_float, va ("%s_z", name), scope, allocate); d = new_def (&type_float, va ("%s_z", name), scope);
d->used = 1; d->used = 1;
d->parent = def; d->parent = def;
} else { d->ofs = def->ofs + 2;
scope->space->size += type_size (type); Hash_Add (defs_by_name, d);
} }
if (type->type == ev_field) { if (type->type == ev_field) {
G_INT (def->ofs) = pr.size_fields; G_INT (def->ofs) = new_location (type->aux_type, pr.entity_data);
if (type->aux_type->type == ev_vector) { if (type->aux_type->type == ev_vector) {
def_t *d; def_t *d;
d = get_def (&type_floatfield, d = new_def (&type_floatfield, va ("%s_x", name), scope);
va ("%s_x", name), scope, allocate);
d->used = 1; // always `used' d->used = 1; // always `used'
d->parent = def; d->parent = def;
d->ofs = def->ofs + 0;
Hash_Add (defs_by_name, d);
d = get_def (&type_floatfield, d = new_def (&type_floatfield, va ("%s_y", name), scope);
va ("%s_y", name), scope, allocate);
d->used = 1; // always `used' d->used = 1; // always `used'
d->parent = def; d->parent = def;
d->ofs = def->ofs + 1;
Hash_Add (defs_by_name, d);
d = get_def (&type_floatfield, d = new_def (&type_floatfield, va ("%s_z", name), scope);
va ("%s_z", name), scope, allocate);
d->used = 1; // always `used' d->used = 1; // always `used'
d->parent = def; d->parent = def;
} else if (type->aux_type->type == ev_pointer) { d->ofs = def->ofs + 2;
//FIXME I don't think this is right for a field pointer Hash_Add (defs_by_name, d);
size = type_size (type->aux_type->aux_type);
pr.size_fields += type->aux_type->num_parms * size;
} else {
size = type_size (type->aux_type);
pr.size_fields += size;
} }
} else if (type->type == ev_pointer && type->num_parms) {
int ofs = scope->space->size;
size = type_size (type->aux_type);
scope->space->size += type->num_parms * size;
if (scope->type >= sc_params) {
expr_t *e1 = new_expr ();
expr_t *e2 = new_expr ();
e1->type = ex_def;
e1->e.def = def;
e2->type = ex_def;
e2->e.def = new_def (type->aux_type, 0, scope);
e2->e.def->ofs = ofs;
append_expr (local_expr,
new_binary_expr ('=', e1, address_expr (e2, 0, 0)));
} else {
G_INT (def->ofs) = ofs;
def->constant = 1;
}
def->initialized = 1;
} }
return def; return def;
@ -247,6 +221,7 @@ new_def (type_t *type, const char *name, scope_t *scope)
def->type = type; def->type = type;
def->scope = scope; def->scope = scope;
def->space = scope->space;
def->global = scope->type == sc_global; def->global = scope->type == sc_global;
def->file = s_file; def->file = s_file;
@ -259,19 +234,27 @@ int
new_location (type_t *type, defspace_t *space) new_location (type_t *type, defspace_t *space)
{ {
int size = type_size (type); int size = type_size (type);
int ofs;
locref_t *loc; locref_t *loc;
locref_t **l = &space->free_locs;
if (free_locs[size]) { while (*l && (*l)->size < size)
loc = free_locs[size]; l = &(*l)->next;
free_locs[size] = loc->next; if ((loc = *l)) {
ofs = loc->ofs;
loc->next = free_free_locs; if (loc->size == size) {
free_free_locs = loc; *l = loc->next;
} else {
return loc->ofs; loc->ofs += size;
loc->size -= size;
}
return ofs;
} }
ofs = space->size;
space->size += size; space->size += size;
return space->size - size; if (space->size > space->max_size)
space->grow (space);
return ofs;
} }
void void
@ -280,10 +263,25 @@ free_location (def_t *def)
int size = type_size (def->type); int size = type_size (def->type);
locref_t *loc; 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;
}
}
ALLOC (1024, locref_t, free_locs, loc); ALLOC (1024, locref_t, free_locs, loc);
loc->ofs = def->ofs; loc->ofs = def->ofs;
loc->next = free_locs[size]; loc->size = size;
free_locs[size] = loc; loc->next = def->space->free_locs;
def->space->free_locs = loc;
} }
def_t * def_t *
@ -298,8 +296,7 @@ get_tempdef (type_t *type, scope_t *scope)
def->type = type; def->type = type;
} else { } else {
def = new_def (type, 0, scope); def = new_def (type, 0, scope);
def->ofs = scope->space->size; def->ofs = new_location (type, scope->space);
scope->space->size += size;
} }
def->freed = def->removed = def->users = 0; def->freed = def->removed = def->users = 0;
def->next = temp_scope.next; def->next = temp_scope.next;

View file

@ -274,7 +274,7 @@ emit_bind_expr (expr_t *e1, expr_t *e2)
} }
def = emit_sub_expr (e1, e2->e.temp.def); def = emit_sub_expr (e1, e2->e.temp.def);
if (t1 != t2) { if (t1 != t2) {
def_t *tmp = new_def (t2, 0, def->scope); def_t *tmp = new_def (t2, 0, current_scope);
tmp->ofs = def->ofs; tmp->ofs = def->ofs;
tmp->users = e2->e.temp.users; tmp->users = e2->e.temp.users;

View file

@ -113,6 +113,8 @@ InitData (void)
pr.scope = new_scope (sc_global, pr.near_data, 0); pr.scope = new_scope (sc_global, pr.near_data, 0);
current_scope = pr.scope; current_scope = pr.scope;
pr.entity_data = new_defspace ();
numglobaldefs = 1; numglobaldefs = 1;
numfielddefs = 1; numfielddefs = 1;
@ -169,7 +171,7 @@ WriteData (int crc)
printf ("%6i locals size (%s)\n", num_localdefs, big_function); printf ("%6i locals size (%s)\n", num_localdefs, big_function);
printf ("%6i fielddefs\n", numfielddefs); printf ("%6i fielddefs\n", numfielddefs);
printf ("%6i globals\n", pr.near_data->size); printf ("%6i globals\n", pr.near_data->size);
printf ("%6i entity fields\n", pr.size_fields); printf ("%6i entity fields\n", pr.entity_data->size);
} }
h = SafeOpenWrite (destfile); h = SafeOpenWrite (destfile);
@ -235,7 +237,7 @@ WriteData (int crc)
if (options.verbosity >= -1) if (options.verbosity >= -1)
printf ("%6i TOTAL SIZE\n", (int) ftell (h)); printf ("%6i TOTAL SIZE\n", (int) ftell (h));
progs.entityfields = pr.size_fields; progs.entityfields = pr.entity_data->size;
progs.version = options.code.progsversion; progs.version = options.code.progsversion;
progs.crc = crc; progs.crc = crc;