redo def allocation to come from configurable `spaces' (locals, globals, far

globals, etc).
This commit is contained in:
Bill Currie 2002-06-09 03:57:20 +00:00
parent 426dc07e53
commit 943c2eefb3
16 changed files with 238 additions and 193 deletions

View file

@ -42,43 +42,62 @@ typedef struct def_s {
struct reloc_s *refs; // for relocations struct reloc_s *refs; // for relocations
int initialized:1; // for uninit var detection unsigned initialized:1; // for uninit var detection
int constant:1; // 1 when a declaration included "= immediate" unsigned constant:1; // 1 when a declaration included "= immediate"
unsigned freed:1; // already freed from the scope unsigned freed:1; // already freed from the scope
unsigned removed:1; // already removed from the symbol table unsigned removed:1; // already removed from the symbol table
unsigned used:1; // unused local detection unsigned used:1; // unused local detection
unsigned absolute:1; // don't relocate (for temps for shorts) unsigned absolute:1; // don't relocate (for temps for shorts)
unsigned managed:1; // managed temp unsigned managed:1; // managed temp
string_t file; // source file string_t file; // source file
int line; // source line int line; // source line
int users; // ref counted temps int users; // ref counted temps
struct expr_s *expr; // temp expr using this def struct expr_s *expr; // temp expr using this def
int locals; struct def_s *def_next; // next def in scope
int *alloc;
struct def_s *def_next; // for writing out the global defs list
struct def_s *next; // general purpose linking struct def_s *next; // general purpose linking
struct def_s *scope_next; // to facilitate hash table removal struct scope_s *scope; // scope the var was defined in
struct def_s *scope; // function the var was defined in, or NULL
struct def_s *parent; // vector/quaternion member struct def_s *parent; // vector/quaternion member
void *return_addr; // who allocated this
} def_t; } def_t;
typedef struct defspace_s {
struct defspace_s *next;
pr_type_t *data;
int size;
int max_size;
int (*grow) (struct defspace_s *space);
} defspace_t;
typedef struct scope_s {
struct scope_s *next;
defspace_t *space;
def_t *head;
def_t **tail;
int num_defs;
struct scope_s *parent;
} scope_t;
extern def_t def_ret, def_parms[MAX_PARMS]; extern def_t def_ret, def_parms[MAX_PARMS];
extern def_t def_void; extern def_t def_void;
extern def_t def_function; extern def_t def_function;
struct def_s *PR_GetDef (struct type_s *type, const char *name, scope_t *new_scope (defspace_t *space, scope_t *parent);
struct def_s *scope, int *allocate); defspace_t *new_defspace (void);
struct def_s *PR_NewDef (struct type_s *type, const char *name,
struct def_s *scope); def_t *PR_GetDef (struct type_s *type, const char *name, scope_t *scope,
int PR_NewLocation (struct type_s *type); int allocate);
void PR_FreeLocation (struct def_s *def); def_t *PR_NewDef (struct type_s *type, const char *name, scope_t *scope);
struct def_s *PR_GetTempDef (struct type_s *type, struct def_s *scope); int PR_NewLocation (struct type_s *type, defspace_t *space);
void PR_FreeLocation (def_t *def);
def_t *PR_GetTempDef (struct type_s *type, scope_t *scope);
void PR_FreeTempDefs (); void PR_FreeTempDefs ();
void PR_ResetTempDefs (); void PR_ResetTempDefs ();
void PR_FlushScope (struct def_s *scope, int force_used); void PR_FlushScope (scope_t *scope, int force_used);
void PR_DefInitialized (struct def_s *d); void PR_DefInitialized (def_t *d);
#endif//__def_h #endif//__def_h

View file

@ -43,6 +43,7 @@ typedef struct function_s {
const char *file; // source file with definition const char *file; // source file with definition
int file_line; int file_line;
struct def_s *def; struct def_s *def;
struct scope_s *scope;
int parm_ofs[MAX_PARMS]; // allways contiguous, right? int parm_ofs[MAX_PARMS]; // allways contiguous, right?
} function_t; } function_t;

View file

@ -47,9 +47,6 @@ typedef struct {
struct type_s *types; struct type_s *types;
struct ex_label_s *labels; struct ex_label_s *labels;
struct def_s *def_head; // unused head of linked list
struct def_s **def_tail; // add new defs after this and move it
char *strings; char *strings;
int strofs; int strofs;
int strings_size; int strings_size;
@ -64,9 +61,8 @@ typedef struct {
dfunction_t *functions; dfunction_t *functions;
int num_functions; int num_functions;
float *globals; struct defspace_s *globals;
int num_globals; struct scope_s *scope;
int globals_size;
int size_fields; int size_fields;
} pr_info_t; } pr_info_t;
@ -78,15 +74,16 @@ extern pr_info_t pr;
extern char destfile[]; extern char destfile[];
extern int pr_source_line; extern int pr_source_line;
extern struct def_s *pr_scope; extern struct scope_s *current_scope;
extern int pr_error_count; extern int pr_error_count;
#define G_FLOAT(o) (pr.globals[o]) #define G_var(t, o) (pr.globals->data[o].t##_var)
#define G_INT(o) (*(int *)&pr.globals[o]) #define G_FLOAT(o) G_var (float, o)
#define G_VECTOR(o) (&pr.globals[o]) #define G_INT(o) G_var (integer, o)
#define G_STRING(o) (pr.strings + *(string_t *)&pr.globals[o]) #define G_VECTOR(o) G_var (vector, o)
#define G_FUNCTION(o) (*(func_t *)&pr.globals[o]) #define G_STRING(o) (pr.strings + G_var (string, o))
#define G_STRUCT(t,o) (*(t *)&pr.globals[o]) #define G_FUNCTION(o) G_var (func, o)
#define G_STRUCT(t,o) (*(t *)&pr.globals->data[o])
extern string_t s_file; // filename for function definition extern string_t s_file; // filename for function definition

View file

@ -159,7 +159,7 @@ class_begin (class_t *class)
va ("_OBJ_CATEGORY_%s_%s", va ("_OBJ_CATEGORY_%s_%s",
class->class_name, class->class_name,
class->category_name), class->category_name),
0, &pr.num_globals); pr.scope, 1);
class->def->initialized = class->def->constant = 1; class->def->initialized = class->def->constant = 1;
category = &G_STRUCT (pr_category_t, class->def->ofs); category = &G_STRUCT (pr_category_t, class->def->ofs);
category->category_name = ReuseString (class->category_name); category->category_name = ReuseString (class->category_name);
@ -175,7 +175,7 @@ class_begin (class_t *class)
meta_def = PR_GetDef (type_Class.aux_type, meta_def = PR_GetDef (type_Class.aux_type,
va ("_OBJ_METACLASS_%s", class->class_name), va ("_OBJ_METACLASS_%s", class->class_name),
0, &pr.num_globals); pr.scope, 1);
meta_def->initialized = meta_def->constant = 1; meta_def->initialized = meta_def->constant = 1;
meta = &G_STRUCT (pr_class_t, meta_def->ofs); meta = &G_STRUCT (pr_class_t, meta_def->ofs);
meta->class_pointer = ReuseString (class->class_name); meta->class_pointer = ReuseString (class->class_name);
@ -190,7 +190,7 @@ class_begin (class_t *class)
class->def = PR_GetDef (type_Class.aux_type, class->def = PR_GetDef (type_Class.aux_type,
va ("_OBJ_CLASS_%s", class->class_name), va ("_OBJ_CLASS_%s", class->class_name),
0, &pr.num_globals); pr.scope, 1);
class->def->initialized = class->def->constant = 1; class->def->initialized = class->def->constant = 1;
cls = &G_STRUCT (pr_class_t, class->def->ofs); cls = &G_STRUCT (pr_class_t, class->def->ofs);
cls->class_pointer = meta_def->ofs; cls->class_pointer = meta_def->ofs;
@ -398,7 +398,7 @@ class_def (class_t *class)
def = PR_GetDef (class->type, def = PR_GetDef (class->type,
va ("_OBJ_CLASS_POINTER_%s", class->class_name), va ("_OBJ_CLASS_POINTER_%s", class->class_name),
0, &pr.num_globals); pr.scope, 1);
if (def->initialized) if (def->initialized)
return def; return def;
if (class->def) { //FIXME need externals? if (class->def) { //FIXME need externals?
@ -451,7 +451,7 @@ class_finish_module (void)
new_struct_field (symtab_type, &type_integer, "cat_def_cnt", vis_public); new_struct_field (symtab_type, &type_integer, "cat_def_cnt", vis_public);
for (i = 0; i < num_classes + num_categories; i++) for (i = 0; i < num_classes + num_categories; i++)
new_struct_field (symtab_type, &type_pointer, 0, vis_public); new_struct_field (symtab_type, &type_pointer, 0, vis_public);
symtab_def = PR_GetDef (symtab_type, "_OBJ_SYMTAB", 0, &pr.num_globals); symtab_def = PR_GetDef (symtab_type, "_OBJ_SYMTAB", pr.scope, 1);
symtab_def->initialized = symtab_def->constant = 1; symtab_def->initialized = symtab_def->constant = 1;
symtab = &G_STRUCT (pr_symtab_t, symtab_def->ofs); symtab = &G_STRUCT (pr_symtab_t, symtab_def->ofs);
symtab->cls_def_cnt = num_classes; symtab->cls_def_cnt = num_classes;
@ -464,7 +464,7 @@ class_finish_module (void)
if ((*t)->def) if ((*t)->def)
*def_ptr++ = (*t)->def->ofs; *def_ptr++ = (*t)->def->ofs;
module_def = PR_GetDef (type_module, "_OBJ_MODULE", 0, &pr.num_globals); module_def = PR_GetDef (type_module, "_OBJ_MODULE", pr.scope, 1);
module_def->initialized = module_def->constant = 1; module_def->initialized = module_def->constant = 1;
module = &G_STRUCT (pr_module_t, module_def->ofs); module = &G_STRUCT (pr_module_t, module_def->ofs);
module->size = type_size (type_module); module->size = type_size (type_module);
@ -472,17 +472,18 @@ class_finish_module (void)
module->symtab = symtab_def->ofs; module->symtab = symtab_def->ofs;
exec_class_def = PR_GetDef (&type_obj_exec_class, "__obj_exec_class", exec_class_def = PR_GetDef (&type_obj_exec_class, "__obj_exec_class",
0, &pr.num_globals); pr.scope, 1);
exec_class_func = new_function (); exec_class_func = new_function ();
exec_class_func->builtin = 0; exec_class_func->builtin = 0;
exec_class_func->def = exec_class_def; exec_class_func->def = exec_class_def;
build_function (exec_class_func); build_function (exec_class_func);
finish_function (exec_class_func); finish_function (exec_class_func);
init_def = PR_GetDef (&type_function, ".ctor", 0, &pr.num_globals); init_def = PR_GetDef (&type_function, ".ctor", pr.scope, 1);
init_func = new_function (); init_func = new_function ();
init_func->def = init_def; init_func->def = init_def;
init_func->code = pr.num_statements; init_func->code = pr.num_statements;
build_scope (init_func, init_def, 0);
build_function (init_func); build_function (init_func);
init_expr = new_block_expr (); init_expr = new_block_expr ();
append_expr (init_expr, append_expr (init_expr,
@ -543,7 +544,7 @@ protocol_add_protocol_methods (protocol_t *protocol, expr_t *protocols)
def_t * def_t *
protocol_def (protocol_t *protocol) protocol_def (protocol_t *protocol)
{ {
return PR_GetDef (&type_Protocol, protocol->name, 0, &pr.num_globals); return PR_GetDef (&type_Protocol, protocol->name, pr.scope, 1);
} }
protocollist_t * protocollist_t *
@ -573,7 +574,7 @@ emit_protocol (protocol_t *protocol)
proto_def = PR_GetDef (type_Protocol.aux_type, proto_def = PR_GetDef (type_Protocol.aux_type,
va ("_OBJ_PROTOCOL_%s", protocol->name), va ("_OBJ_PROTOCOL_%s", protocol->name),
0, &pr.num_globals); pr.scope, 1);
proto_def->initialized = proto_def->constant = 1; proto_def->initialized = proto_def->constant = 1;
proto = &G_STRUCT (pr_protocol_t, proto_def->ofs); proto = &G_STRUCT (pr_protocol_t, proto_def->ofs);
proto->class_pointer = 0; proto->class_pointer = 0;
@ -604,7 +605,7 @@ emit_protocol_list (protocollist_t *protocols, const char *name)
new_struct_field (protocol_list, &type_pointer, 0, vis_public); new_struct_field (protocol_list, &type_pointer, 0, vis_public);
proto_list_def = PR_GetDef (type_Protocol.aux_type, proto_list_def = PR_GetDef (type_Protocol.aux_type,
va ("_OBJ_PROTOCOLS_%s", name), va ("_OBJ_PROTOCOLS_%s", name),
0, &pr.num_globals); pr.scope, 1);
proto_list_def->initialized = proto_list_def->constant = 1; proto_list_def->initialized = proto_list_def->constant = 1;
proto_list = &G_STRUCT (pr_protocol_list_t, proto_list_def->ofs); proto_list = &G_STRUCT (pr_protocol_list_t, proto_list_def->ofs);
proto_list->next = 0; proto_list->next = 0;

View file

@ -98,6 +98,8 @@ parse_cpp_name ()
{ {
char *n; char *n;
if (!cpp_name)
return;
n = strdup (cpp_name); n = strdup (cpp_name);
while (*n) { while (*n) {
while (*n && *n == ' ') while (*n && *n == ' ')

View file

@ -120,7 +120,7 @@ emit_statement (int sline, opcode_t *op, def_t *var_a, def_t *var_b,
ret = var_a; ret = var_a;
} else { // allocate result space } else { // allocate result space
if (!var_c) { if (!var_c) {
var_c = PR_GetTempDef (types[op->type_c], pr_scope); var_c = PR_GetTempDef (types[op->type_c], current_scope);
var_c->users += 2; var_c->users += 2;
} }
statement->c = var_c->ofs; statement->c = var_c->ofs;
@ -221,9 +221,10 @@ emit_assign_expr (int oper, expr_t *e)
if (def_a->constant) { if (def_a->constant) {
if (options.code.cow) { if (options.code.cow) {
int size = type_size (def_a->type); int size = type_size (def_a->type);
int ofs = PR_NewLocation (def_a->type); int ofs = PR_NewLocation (def_a->type, pr.globals);
memcpy (pr.globals + ofs, pr.globals + def_a->ofs, size); memcpy (pr.globals->data + ofs, pr.globals->data + def_a->ofs,
size);
def_a->ofs = ofs; def_a->ofs = ofs;
def_a->constant = 0; def_a->constant = 0;
if (options.warnings.cow) if (options.warnings.cow)
@ -328,7 +329,7 @@ emit_sub_expr (expr_t *e, def_t *dest)
} }
operator = get_op_string (e->e.expr.op); operator = get_op_string (e->e.expr.op);
if (!dest) { if (!dest) {
dest = PR_GetTempDef (e->e.expr.type, pr_scope); dest = PR_GetTempDef (e->e.expr.type, current_scope);
dest->users += 2; dest->users += 2;
} }
op = opcode_find (operator, def_a, def_b, dest); op = opcode_find (operator, def_a, def_b, dest);
@ -353,7 +354,7 @@ emit_sub_expr (expr_t *e, def_t *dest)
def_a = ReuseConstant (&zero, 0); def_a = ReuseConstant (&zero, 0);
def_b = emit_sub_expr (e->e.expr.e1, 0); def_b = emit_sub_expr (e->e.expr.e1, 0);
if (!dest) { if (!dest) {
dest = PR_GetTempDef (e->e.expr.type, pr_scope); dest = PR_GetTempDef (e->e.expr.type, current_scope);
dest->users += 2; dest->users += 2;
} }
break; break;
@ -363,7 +364,7 @@ emit_sub_expr (expr_t *e, def_t *dest)
operator = "&"; operator = "&";
if (e->e.expr.e1->type == ex_expr if (e->e.expr.e1->type == ex_expr
&& e->e.expr.e1->e.expr.op == '.') { && e->e.expr.e1->e.expr.op == '.') {
tmp = PR_GetTempDef (e->e.expr.type, pr_scope); tmp = PR_GetTempDef (e->e.expr.type, current_scope);
tmp->users += 2; tmp->users += 2;
def_b = emit_sub_expr (&zero, 0); def_b = emit_sub_expr (&zero, 0);
} else { } else {
@ -371,7 +372,7 @@ emit_sub_expr (expr_t *e, def_t *dest)
} }
def_a = emit_sub_expr (e->e.expr.e1, tmp); def_a = emit_sub_expr (e->e.expr.e1, tmp);
if (!dest) { if (!dest) {
dest = PR_GetTempDef (e->e.expr.type, pr_scope); dest = PR_GetTempDef (e->e.expr.type, current_scope);
dest->users += 2; dest->users += 2;
} }
break; break;
@ -380,7 +381,7 @@ emit_sub_expr (expr_t *e, def_t *dest)
&& (e->e.expr.e1->type != ex_pointer && (e->e.expr.e1->type != ex_pointer
|| !(e->e.expr.e1->e.pointer.val > 0 || !(e->e.expr.e1->e.pointer.val > 0
&& e->e.expr.e1->e.pointer.val < 65536))) { && e->e.expr.e1->e.pointer.val < 65536))) {
dest = PR_GetTempDef (e->e.expr.type, pr_scope); dest = PR_GetTempDef (e->e.expr.type, current_scope);
dest->users += 2; dest->users += 2;
} }
if (e->e.expr.e1->type == ex_expr if (e->e.expr.e1->type == ex_expr
@ -398,7 +399,7 @@ emit_sub_expr (expr_t *e, def_t *dest)
} }
def_b = &def_void; def_b = &def_void;
if (!dest) { if (!dest) {
dest = PR_GetTempDef (e->e.expr.type, pr_scope); dest = PR_GetTempDef (e->e.expr.type, current_scope);
dest->users = 2; dest->users = 2;
} }
operator = "="; operator = "=";
@ -417,7 +418,7 @@ emit_sub_expr (expr_t *e, def_t *dest)
if (dest) if (dest)
e->e.temp.def = dest; e->e.temp.def = dest;
else else
e->e.temp.def = PR_GetTempDef (e->e.temp.type, pr_scope); e->e.temp.def = PR_GetTempDef (e->e.temp.type, current_scope);
e->e.temp.def->users = e->e.temp.users; e->e.temp.def->users = e->e.temp.users;
e->e.temp.def->expr = e; e->e.temp.def->expr = e;
e->e.temp.def->managed = 1; e->e.temp.def->managed = 1;
@ -427,7 +428,7 @@ emit_sub_expr (expr_t *e, def_t *dest)
case ex_pointer: case ex_pointer:
if (e->e.pointer.val > 0 && e->e.pointer.val < 65536 if (e->e.pointer.val > 0 && e->e.pointer.val < 65536
&& e->e.pointer.type->type != ev_struct) { && e->e.pointer.type->type != ev_struct) {
d = PR_NewDef (e->e.pointer.type, 0, pr_scope); d = PR_NewDef (e->e.pointer.type, 0, current_scope);
d->ofs = e->e.short_val; d->ofs = e->e.short_val;
d->absolute = e->e.pointer.abs; d->absolute = e->e.pointer.abs;
d->users = 1; d->users = 1;
@ -446,7 +447,7 @@ emit_sub_expr (expr_t *e, def_t *dest)
d = ReuseConstant (e, 0); d = ReuseConstant (e, 0);
break; break;
case ex_short: case ex_short:
d = PR_NewDef (&type_short, 0, pr_scope); d = PR_NewDef (&type_short, 0, current_scope);
d->ofs = e->e.short_val; d->ofs = e->e.short_val;
d->absolute = 1; d->absolute = 1;
d->users = 1; d->users = 1;

View file

@ -139,7 +139,7 @@ convert_name (expr_t *e)
e->e.def = class_def (class); e->e.def = class_def (class);
return; return;
} }
d = PR_GetDef (NULL, name, pr_scope, 0); d = PR_GetDef (NULL, name, current_scope, 0);
if (d) { if (d) {
if (!d->scope) { if (!d->scope) {
new = class_ivar_expr (current_class, name); new = class_ivar_expr (current_class, name);
@ -326,7 +326,7 @@ check_initialized (expr_t *e)
{ {
if (options.warnings.uninited_variable) { if (options.warnings.uninited_variable) {
if (e->type == ex_def if (e->type == ex_def
&& !(e->e.def->type->type == ev_func && !e->e.def->scope) && !(e->e.def->type->type == ev_func && !e->e.def->scope->parent)
&& !e->e.def->initialized) { && !e->e.def->initialized) {
warning (e, "%s may be used uninitialized", e->e.def->name); warning (e, "%s may be used uninitialized", e->e.def->name);
e->e.def->initialized = 1; // only warn once e->e.def->initialized = 1; // only warn once
@ -476,7 +476,7 @@ new_def_expr (def_t *def)
expr_t * expr_t *
new_self_expr (void) new_self_expr (void)
{ {
def_t *def = PR_GetDef (&type_entity, ".self", 0, &pr.num_globals); def_t *def = PR_GetDef (&type_entity, ".self", pr.scope, 1);
PR_DefInitialized (def); PR_DefInitialized (def);
return new_def_expr (def); return new_def_expr (def);
@ -486,7 +486,7 @@ expr_t *
new_this_expr (void) new_this_expr (void)
{ {
type_t *type = field_type (&type_id); type_t *type = field_type (&type_id);
def_t *def = PR_GetDef (type, ".this", 0, &pr.num_globals); def_t *def = PR_GetDef (type, ".this", pr.scope, 1);
PR_DefInitialized (def); PR_DefInitialized (def);
return new_def_expr (def); return new_def_expr (def);
@ -1629,10 +1629,9 @@ function_expr (expr_t *e1, expr_t *e2)
expr_t *ret = new_expr (); expr_t *ret = new_expr ();
ret->type = ex_def; ret->type = ex_def;
ret->e.def = PR_NewDef (0, 0, 0); ret->e.def = PR_NewDef (ftype->aux_type, 0, pr.scope);
*ret->e.def = def_ret; ret->e.def->ofs = def_ret.ofs;
ret->e.def->type = ftype->aux_type;
call->e.block.result = ret; call->e.block.result = ret;
} }
return call; return call;

View file

@ -123,13 +123,13 @@ build_scope (function_t *f, def_t *func, param_t *params)
param_t *p; param_t *p;
def_t *argv = 0; def_t *argv = 0;
func->alloc = &func->locals; f->scope = new_scope (new_defspace (), pr.scope);
if (func->type->num_parms < 0) { if (func->type->num_parms < 0) {
def = PR_GetDef (&type_integer, ".argc", func, func->alloc); def = PR_GetDef (&type_integer, ".argc", f->scope, 1);
def->used = 1; def->used = 1;
PR_DefInitialized (def); PR_DefInitialized (def);
argv = PR_GetDef (&type_pointer, ".argv", func, func->alloc); argv = PR_GetDef (&type_pointer, ".argv", f->scope, 1);
argv->used = 1; argv->used = 1;
PR_DefInitialized (argv); PR_DefInitialized (argv);
} }
@ -139,7 +139,7 @@ build_scope (function_t *f, def_t *func, param_t *params)
continue; // ellipsis marker continue; // ellipsis marker
if (!p->type) if (!p->type)
continue; // non-param selector continue; // non-param selector
def = PR_GetDef (p->type, p->name, func, func->alloc); def = PR_GetDef (p->type, p->name, f->scope, 1);
f->parm_ofs[i] = def->ofs; f->parm_ofs[i] = def->ofs;
if (i > 0 && f->parm_ofs[i] < f->parm_ofs[i - 1]) { if (i > 0 && f->parm_ofs[i] < f->parm_ofs[i - 1]) {
error (0, "bad parm order"); error (0, "bad parm order");
@ -153,7 +153,7 @@ build_scope (function_t *f, def_t *func, param_t *params)
if (argv) { if (argv) {
while (i < MAX_PARMS) { while (i < MAX_PARMS) {
def = PR_GetDef (&type_vector, 0, func, func->alloc); def = PR_GetDef (&type_vector, 0, f->scope, 1);
def->used = 1; def->used = 1;
if (argv->type == &type_pointer) if (argv->type == &type_pointer)
argv->type = array_type (&type_vector, MAX_PARMS - i); argv->type = array_type (&type_vector, MAX_PARMS - i);
@ -224,7 +224,8 @@ finish_function (function_t *f)
df->s_name = ReuseString (f->def->name); df->s_name = ReuseString (f->def->name);
df->s_file = s_file; df->s_file = s_file;
df->numparms = f->def->type->num_parms; df->numparms = f->def->type->num_parms;
df->locals = f->def->locals; if (f->scope)
df->locals = f->scope->space->size;
df->parm_start = 0; df->parm_start = 0;
if ((count = df->numparms) < 0) if ((count = df->numparms) < 0)
count = -count - 1; count = -count - 1;
@ -234,7 +235,7 @@ finish_function (function_t *f)
if (f->aux) { if (f->aux) {
def_t *def; def_t *def;
f->aux->function = df - pr.functions; f->aux->function = df - pr.functions;
for (def = f->def->scope_next; def; def = def->scope_next) { for (def = f->scope->head; def; def = def->def_next) {
if (def->name) { if (def->name) {
ddef_t *d = new_local (); ddef_t *d = new_local ();
d->type = def->type->type; d->type = def->type->type;
@ -255,7 +256,7 @@ emit_function (function_t *f, expr_t *e)
if (f->aux) if (f->aux)
lineno_base = f->aux->source_line; lineno_base = f->aux->source_line;
pr_scope = f->def; current_scope = f->scope;
while (e) { while (e) {
//printf ("%d ", pr_source_line); //printf ("%d ", pr_source_line);
//print_expr (e); //print_expr (e);
@ -265,8 +266,8 @@ emit_function (function_t *f, expr_t *e)
e = e->next; e = e->next;
} }
emit_statement (pr_source_line, op_done, 0, 0, 0); emit_statement (pr_source_line, op_done, 0, 0, 0);
PR_FlushScope (pr_scope, 0); PR_FlushScope (current_scope, 0);
pr_scope = 0; current_scope = pr.scope;
PR_ResetTempDefs (); PR_ResetTempDefs ();
//puts (""); //puts ("");

View file

@ -207,7 +207,7 @@ WriteProgdefs (char *filename)
"\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{\tint\tpad[%i];\n", "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{\tint\tpad[%i];\n",
RESERVED_OFS); RESERVED_OFS);
for (d = pr.def_head; d; d = d->def_next) { for (d = pr.scope->head; d; d = d->def_next) {
if (!strcmp (d->name, "end_sys_globals")) if (!strcmp (d->name, "end_sys_globals"))
break; break;
@ -237,7 +237,7 @@ WriteProgdefs (char *filename)
// print all fields // print all fields
fprintf (f, "typedef struct\n{\n"); fprintf (f, "typedef struct\n{\n");
for (d = pr.def_head; d; d = d->def_next) { for (d = pr.scope->head; d; d = d->def_next) {
if (!strcmp (d->name, "end_sys_fields")) if (!strcmp (d->name, "end_sys_fields"))
break; break;

View file

@ -167,15 +167,20 @@ ReuseConstant (expr_t *expr, def_t *def)
Hash_NewTable (16381, quaternion_imm_get_key, 0, 0); Hash_NewTable (16381, quaternion_imm_get_key, 0, 0);
integer_imm_defs = Hash_NewTable (16381, int_imm_get_key, 0, "integer"); integer_imm_defs = Hash_NewTable (16381, int_imm_get_key, 0, "integer");
Hash_Add (string_imm_defs, cn = PR_NewDef (&type_string, ".imm", 0)); Hash_Add (string_imm_defs, cn = PR_NewDef (&type_string, ".imm",
pr.scope));
cn->initialized = cn->constant = 1; cn->initialized = cn->constant = 1;
Hash_Add (float_imm_defs, cn = PR_NewDef (&type_float, ".imm", 0)); Hash_Add (float_imm_defs, cn = PR_NewDef (&type_float, ".imm",
pr.scope));
cn->initialized = cn->constant = 1; cn->initialized = cn->constant = 1;
Hash_Add (entity_imm_defs, cn = PR_NewDef (&type_entity, ".imm", 0)); Hash_Add (entity_imm_defs, cn = PR_NewDef (&type_entity, ".imm",
pr.scope));
cn->initialized = cn->constant = 1; cn->initialized = cn->constant = 1;
Hash_Add (pointer_imm_defs, cn = PR_NewDef (&type_pointer, ".imm", 0)); Hash_Add (pointer_imm_defs, cn = PR_NewDef (&type_pointer, ".imm",
pr.scope));
cn->initialized = cn->constant = 1; cn->initialized = cn->constant = 1;
Hash_Add (integer_imm_defs, cn = PR_NewDef (&type_integer, ".imm", 0)); Hash_Add (integer_imm_defs, cn = PR_NewDef (&type_integer, ".imm",
pr.scope));
cn->initialized = cn->constant = 1; cn->initialized = cn->constant = 1;
} }
cn = 0; cn = 0;
@ -253,7 +258,7 @@ ReuseConstant (expr_t *expr, def_t *def)
cn = def; cn = def;
} else { } else {
if (cn->type != type) { if (cn->type != type) {
def = PR_NewDef (type, ".imm", 0); def = PR_NewDef (type, ".imm", pr.scope);
def->ofs = cn->ofs; def->ofs = cn->ofs;
cn = def; cn = def;
} }
@ -264,19 +269,19 @@ ReuseConstant (expr_t *expr, def_t *def)
// always share immediates // always share immediates
if (def) { if (def) {
if (def->type != type) { if (def->type != type) {
cn = PR_NewDef (type, ".imm", 0); cn = PR_NewDef (type, ".imm", pr.scope);
cn->ofs = def->ofs; cn->ofs = def->ofs;
} else { } else {
cn = def; cn = def;
} }
} else { } else {
cn = PR_NewDef (type, ".imm", 0); cn = PR_NewDef (type, ".imm", pr.scope);
cn->ofs = PR_NewLocation (type); cn->ofs = PR_NewLocation (type, pr.globals);
if (type == &type_vector || type == &type_quaternion) { if (type == &type_vector || type == &type_quaternion) {
int i; int i;
for (i = 0; i < 3 + (type == &type_quaternion); i++) for (i = 0; i < 3 + (type == &type_quaternion); i++)
PR_NewDef (&type_float, ".imm", 0); PR_NewDef (&type_float, ".imm", pr.scope);
} }
} }
cn->initialized = cn->constant = 1; cn->initialized = cn->constant = 1;
@ -284,7 +289,7 @@ ReuseConstant (expr_t *expr, def_t *def)
if (e.type == ex_string) if (e.type == ex_string)
e.e.integer_val = ReuseString (rep->str); e.e.integer_val = ReuseString (rep->str);
memcpy (pr.globals + cn->ofs, &e.e, 4 * type_size (type)); memcpy (pr.globals->data + cn->ofs, &e.e, 4 * type_size (type));
Hash_Add (tab, cn); Hash_Add (tab, cn);

View file

@ -57,9 +57,7 @@ static const char rcsid[] =
#include "type.h" #include "type.h"
static def_t *send_message_def; static def_t *send_message_def;
static function_t *send_message_func;
static def_t *send_message_super_def; static def_t *send_message_super_def;
static function_t *send_message_super_func;
method_t * method_t *
new_method (type_t *ret_type, param_t *selector, param_t *opt_parms) new_method (type_t *ret_type, param_t *selector, param_t *opt_parms)
@ -123,7 +121,7 @@ method_def (class_t *class, method_t *method)
*s = '_'; *s = '_';
//printf ("%s %s %s %ld\n", method->name, method->types, str->str, str->size); //printf ("%s %s %s %ld\n", method->name, method->types, str->str, str->size);
// FIXME need a file scope // FIXME need a file scope
def = PR_GetDef (method->type, str->str, 0, &pr.num_globals); def = PR_GetDef (method->type, str->str, pr.scope, 1);
dstring_delete (str); dstring_delete (str);
return def; return def;
} }
@ -171,14 +169,13 @@ new_keywordarg (const char *selector, struct expr_s *expr)
} }
static void static void
make_message_def (const char *name, def_t **def, function_t **func) make_message_def (const char *name, def_t **def)
{ {
*def = PR_GetDef (&type_IMP, name, 0, &pr.num_globals); expr_t *zero = new_expr ();
*func = new_function ();
(*func)->builtin = 0; zero->type = ex_integer;
(*func)->def = *def; *def = PR_GetDef (&type_IMP, name, pr.scope, 1);
build_function (*func); build_builtin_function (*def, zero);
finish_function (*func);
} }
expr_t * expr_t *
@ -187,10 +184,8 @@ send_message (int super)
expr_t *e; expr_t *e;
if (!send_message_def) { if (!send_message_def) {
make_message_def ("obj_msgSend", make_message_def ("obj_msgSend", &send_message_def);
&send_message_def, &send_message_func); make_message_def ("obj_msgSend_super", &send_message_super_def);
make_message_def ("obj_msgSend_super",
&send_message_super_def, &send_message_super_func);
} }
e = new_expr (); e = new_expr ();
e->type = ex_def; e->type = ex_def;
@ -266,9 +261,9 @@ selector_def (const char *_sel_id, const char *_sel_types)
sel_def = malloc (sizeof (sel_def_t)); sel_def = malloc (sizeof (sel_def_t));
sel_def->sel_id = sel_id; sel_def->sel_id = sel_id;
sel_def->sel_types = sel_types; sel_def->sel_types = sel_types;
sel_def->def = PR_NewDef (type_SEL.aux_type, ".imm", 0); sel_def->def = PR_NewDef (type_SEL.aux_type, ".imm", pr.scope);
sel_def->def->initialized = sel_def->def->constant = 1; sel_def->def->initialized = sel_def->def->constant = 1;
sel_def->def->ofs = PR_NewLocation (type_SEL.aux_type); sel_def->def->ofs = PR_NewLocation (type_SEL.aux_type, pr.globals);
G_INT (sel_def->def->ofs) = sel_id; G_INT (sel_def->def->ofs) = sel_id;
G_INT (sel_def->def->ofs + 1) = sel_types; G_INT (sel_def->def->ofs + 1) = sel_types;
Hash_AddElement (sel_def_hash, sel_def); Hash_AddElement (sel_def_hash, sel_def);
@ -302,7 +297,7 @@ emit_methods (methodlist_t *_methods, const char *name, int instance)
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
new_struct_field (method_list, type_Method.aux_type, 0, vis_public); new_struct_field (method_list, type_Method.aux_type, 0, vis_public);
methods_def = PR_GetDef (method_list, va ("_OBJ_%s_METHODS_%s", type, name), methods_def = PR_GetDef (method_list, va ("_OBJ_%s_METHODS_%s", type, name),
0, &pr.num_globals); pr.scope, 1);
methods_def->initialized = methods_def->constant = 1; methods_def->initialized = methods_def->constant = 1;
methods = &G_STRUCT (pr_method_list_t, methods_def->ofs); methods = &G_STRUCT (pr_method_list_t, methods_def->ofs);
methods->method_next = 0; methods->method_next = 0;

View file

@ -53,6 +53,8 @@ def_t def_ret, def_parms[MAX_PARMS];
static def_t *free_temps[4]; // indexted by type size static def_t *free_temps[4]; // indexted by type size
static def_t temp_scope; static def_t temp_scope;
static def_t *free_defs; static def_t *free_defs;
static defspace_t *free_spaces;
static scope_t *free_scopes;
static locref_t *free_locs[4]; // indexted by type size static locref_t *free_locs[4]; // indexted by type size
static locref_t *free_free_locs; static locref_t *free_free_locs;
@ -71,7 +73,7 @@ defs_get_key (void *_def, void *_tab)
} }
static def_t * static def_t *
check_for_name (type_t *type, const char *name, def_t *scope, int *allocate) check_for_name (type_t *type, const char *name, scope_t *scope, int allocate)
{ {
def_t *def; def_t *def;
@ -96,6 +98,35 @@ check_for_name (type_t *type, const char *name, def_t *scope, int *allocate)
return 0; 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;
}
scope_t *
new_scope (defspace_t *space, scope_t *parent)
{
scope_t *scope;
ALLOC (1024, scope_t, scopes, scope);
scope->space = space;
scope->parent = parent;
scope->tail = &scope->head;
return scope;
}
/* /*
PR_GetDef PR_GetDef
@ -103,7 +134,7 @@ check_for_name (type_t *type, const char *name, def_t *scope, int *allocate)
If allocate is true, a new def will be allocated if it can't be found If allocate is true, a new def will be allocated if it can't be found
*/ */
def_t * def_t *
PR_GetDef (type_t *type, const char *name, def_t *scope, int *allocate) PR_GetDef (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; int size;
@ -117,7 +148,7 @@ PR_GetDef (type_t *type, const char *name, def_t *scope, int *allocate)
Hash_Add (defs_by_name, def); Hash_Add (defs_by_name, def);
// FIXME: need to sort out location re-use // FIXME: need to sort out location re-use
def->ofs = *allocate; 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
@ -138,7 +169,7 @@ PR_GetDef (type_t *type, const char *name, def_t *scope, int *allocate)
d->used = 1; d->used = 1;
d->parent = def; d->parent = def;
} else { } else {
*allocate += type_size (type); scope->space->size += type_size (type);
} }
if (type->type == ev_field) { if (type->type == ev_field) {
@ -170,12 +201,12 @@ PR_GetDef (type_t *type, const char *name, def_t *scope, int *allocate)
pr.size_fields += size; pr.size_fields += size;
} }
} else if (type->type == ev_pointer && type->num_parms) { } else if (type->type == ev_pointer && type->num_parms) {
int ofs = *allocate; int ofs = scope->space->size;
size = type_size (type->aux_type); size = type_size (type->aux_type);
*allocate += type->num_parms * size; scope->space->size += type->num_parms * size;
if (pr_scope) { if (scope->parent) {
expr_t *e1 = new_expr (); expr_t *e1 = new_expr ();
expr_t *e2 = new_expr (); expr_t *e2 = new_expr ();
@ -183,10 +214,11 @@ PR_GetDef (type_t *type, const char *name, def_t *scope, int *allocate)
e1->e.def = def; e1->e.def = def;
e2->type = ex_def; e2->type = ex_def;
e2->e.def = PR_NewDef (type->aux_type, 0, pr_scope); e2->e.def = PR_NewDef (type->aux_type, 0, scope);
e2->e.def->ofs = ofs; e2->e.def->ofs = ofs;
append_expr (local_expr, new_binary_expr ('=', e1, address_expr (e2, 0, 0))); append_expr (local_expr,
new_binary_expr ('=', e1, address_expr (e2, 0, 0)));
} else { } else {
G_INT (def->ofs) = ofs; G_INT (def->ofs) = ofs;
def->constant = 1; def->constant = 1;
@ -198,21 +230,17 @@ PR_GetDef (type_t *type, const char *name, def_t *scope, int *allocate)
} }
def_t * def_t *
PR_NewDef (type_t *type, const char *name, def_t *scope) PR_NewDef (type_t *type, const char *name, scope_t *scope)
{ {
def_t *def; def_t *def;
ALLOC (16384, def_t, defs, def); ALLOC (16384, def_t, defs, def);
if (name) { *scope->tail = def;
*pr.def_tail = def; scope->tail = &def->def_next;
pr.def_tail = &def->def_next; scope->num_defs++;
}
if (scope) { def->return_addr = __builtin_return_address (0);
def->scope_next = scope->scope_next;
scope->scope_next = def;
}
def->name = name ? strdup (name) : 0; def->name = name ? strdup (name) : 0;
def->type = type; def->type = type;
@ -226,7 +254,7 @@ PR_NewDef (type_t *type, const char *name, def_t *scope)
} }
int int
PR_NewLocation (type_t *type) PR_NewLocation (type_t *type, defspace_t *space)
{ {
int size = type_size (type); int size = type_size (type);
locref_t *loc; locref_t *loc;
@ -240,8 +268,8 @@ PR_NewLocation (type_t *type)
return loc->ofs; return loc->ofs;
} }
pr.num_globals += size; space->size += size;
return pr.num_globals - size; return space->size - size;
} }
void void
@ -257,7 +285,7 @@ PR_FreeLocation (def_t *def)
} }
def_t * def_t *
PR_GetTempDef (type_t *type, def_t *scope) PR_GetTempDef (type_t *type, scope_t *scope)
{ {
int size = type_size (type); int size = type_size (type);
def_t *def; def_t *def;
@ -268,8 +296,8 @@ PR_GetTempDef (type_t *type, def_t *scope)
def->type = type; def->type = type;
} else { } else {
def = PR_NewDef (type, 0, scope); def = PR_NewDef (type, 0, scope);
def->ofs = *scope->alloc; def->ofs = scope->space->size;
*scope->alloc += size; 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;
@ -325,11 +353,11 @@ PR_ResetTempDefs (void)
} }
void void
PR_FlushScope (def_t *scope, int force_used) PR_FlushScope (scope_t *scope, int force_used)
{ {
def_t *def; def_t *def;
for (def = scope->scope_next; def; def = def->scope_next) { for (def = scope->head; def; def = def->def_next) {
if (def->name) { if (def->name) {
if (!force_used && !def->used) { if (!force_used && !def->used) {
expr_t e; expr_t e;

View file

@ -80,7 +80,7 @@ parse_error (void)
int yylex (void); int yylex (void);
hashtab_t *save_local_inits (def_t *scope); hashtab_t *save_local_inits (scope_t *scope);
hashtab_t *merge_local_inits (hashtab_t *dl_1, hashtab_t *dl_2); hashtab_t *merge_local_inits (hashtab_t *dl_1, hashtab_t *dl_2);
void restore_local_inits (hashtab_t *def_list); void restore_local_inits (hashtab_t *def_list);
void free_local_inits (hashtab_t *def_list); void free_local_inits (hashtab_t *def_list);
@ -178,8 +178,8 @@ switch_block_t *switch_block;
type_t *struct_type; type_t *struct_type;
visibility_t current_visibility; visibility_t current_visibility;
type_t *current_ivars; type_t *current_ivars;
scope_t *current_scope;
def_t *pr_scope; // the function being parsed, or NULL
string_t s_file; // filename for function definition string_t s_file; // filename for function definition
int element_flag; int element_flag;
@ -340,7 +340,7 @@ def_item
: def_name opt_initializer : def_name opt_initializer
{ {
$$ = $1; $$ = $1;
if ($$ && !$$->scope && $$->type->type != ev_func) if ($$ && !$$->scope->parent && $$->type->type != ev_func)
PR_DefInitialized ($$); PR_DefInitialized ($$);
} }
; ;
@ -348,17 +348,20 @@ def_item
def_name def_name
: NAME : NAME
{ {
int *alloc = &pr.num_globals; if (current_scope->parent) {
scope_t *scope = current_scope->parent;
if (pr_scope) { if (!scope->parent && scope->parent->parent) {
alloc = pr_scope->alloc; def_t *def = PR_GetDef (0, $1, scope, 0);
if (pr_scope->scope && !pr_scope->scope->scope) { if (def) {
def_t *def = PR_GetDef (0, $1, pr_scope->scope, 0); scope = def->scope;
if (def && def->scope && !def->scope->scope) if (scope->parent && !scope->parent->parent) {
warning (0, "local %s shadows param %s", $1, def->name); warning (0, "local %s shadows param %s", $1,
def->name);
}
}
} }
} }
$$ = PR_GetDef (current_type, $1, pr_scope, alloc); $$ = PR_GetDef (current_type, $1, current_scope, 1);
current_def = $$; current_def = $$;
} }
; ;
@ -372,7 +375,7 @@ opt_initializer
var_initializer var_initializer
: '=' expr : '=' expr
{ {
if (pr_scope) { if (current_scope->parent) {
append_expr (local_expr, append_expr (local_expr,
assign_expr (new_def_expr (current_def), $2)); assign_expr (new_def_expr (current_def), $2));
PR_DefInitialized (current_def); PR_DefInitialized (current_def);
@ -487,40 +490,36 @@ begin_function
lineno->fa.func = $$->aux - auxfunctions; lineno->fa.func = $$->aux - auxfunctions;
} }
pr_scope = current_def;
build_scope ($$, current_def, current_params); build_scope ($$, current_def, current_params);
current_scope = $$->scope;
} }
; ;
end_function end_function
: /*empty*/ : /*empty*/
{ {
pr_scope = 0; current_scope = current_scope->parent;
} }
; ;
statement_block statement_block
: '{' : '{'
{ {
def_t *scope = PR_NewDef (&type_void, ".scope", pr_scope); scope_t *scope = new_scope (current_scope->space, current_scope);
scope->alloc = pr_scope->alloc; current_scope = scope;
scope->used = 1;
pr_scope->scope_next = scope->scope_next;
scope->scope_next = 0;
pr_scope = scope;
} }
statements '}' statements '}'
{ {
def_t *scope = pr_scope; def_t *defs = current_scope->head;
PR_FlushScope (pr_scope, 1); PR_FlushScope (current_scope, 1);
while (scope->scope_next) current_scope = current_scope->parent;
scope = scope->scope_next; *current_scope->tail = defs;
while (*current_scope->tail) {
scope->scope_next = pr_scope->scope->scope_next; current_scope->tail = &(*current_scope->tail)->next;
pr_scope->scope->scope_next = pr_scope; current_scope->num_defs++;
pr_scope = pr_scope->scope; }
$$ = $3; $$ = $3;
} }
; ;
@ -652,7 +651,7 @@ statement
} }
| IF '(' expr ')' save_inits statement ELSE | IF '(' expr ')' save_inits statement ELSE
{ {
$<def_list>$ = save_local_inits (pr_scope); $<def_list>$ = save_local_inits (current_scope);
restore_local_inits ($5); restore_local_inits ($5);
} }
statement statement
@ -665,7 +664,7 @@ statement
$$ = new_block_expr (); $$ = new_block_expr ();
else_ini = save_local_inits (pr_scope); else_ini = save_local_inits (current_scope);
restore_local_inits ($5); restore_local_inits ($5);
free_local_inits ($5); free_local_inits ($5);
@ -746,7 +745,7 @@ switch_block
save_inits save_inits
: /* empty */ : /* empty */
{ {
$$ = save_local_inits (pr_scope); $$ = save_local_inits (current_scope);
} }
; ;
@ -1330,12 +1329,12 @@ free_key (void *_d, void *unused)
} }
static void static void
scan_scope (hashtab_t *tab, def_t *scope) scan_scope (hashtab_t *tab, scope_t *scope)
{ {
def_t *def; def_t *def;
if (scope->scope) if (scope->parent && scope->parent->parent)
scan_scope (tab, scope->scope); scan_scope (tab, scope->parent);
for (def = scope->scope_next; def; def = def->scope_next) { for (def = scope->head; def; def = def->def_next) {
if (def->name && !def->removed) { if (def->name && !def->removed) {
def_state_t *ds; def_state_t *ds;
ALLOC (1024, def_state_t, def_states, ds); ALLOC (1024, def_state_t, def_states, ds);
@ -1347,7 +1346,7 @@ scan_scope (hashtab_t *tab, def_t *scope)
} }
hashtab_t * hashtab_t *
save_local_inits (def_t *scope) save_local_inits (scope_t *scope)
{ {
hashtab_t *tab = Hash_NewTable (61, get_key, free_key, 0); hashtab_t *tab = Hash_NewTable (61, get_key, free_key, 0);
scan_scope (tab, scope); scan_scope (tab, scope);

View file

@ -108,8 +108,10 @@ InitData (void)
pr.strofs = 1; pr.strofs = 1;
pr.num_functions = 1; pr.num_functions = 1;
pr.globals = calloc (65536, 4); //FIXME pr.globals = new_defspace ();
pr.globals_size = 65536; pr.globals->data = calloc (65536, sizeof (pr_type_t));
pr.scope = new_scope (pr.globals, 0);
current_scope = pr.scope;
numglobaldefs = 1; numglobaldefs = 1;
numfielddefs = 1; numfielddefs = 1;
@ -130,13 +132,11 @@ WriteData (int crc)
FILE *h; FILE *h;
int i; int i;
for (i = 0, def = pr.def_head; def; def = def->def_next) globals = calloc (pr.scope->num_defs, sizeof (ddef_t));
i++; fields = calloc (pr.scope->num_defs, sizeof (ddef_t));
globals = calloc (i, sizeof (ddef_t));
fields = calloc (i, sizeof (ddef_t));
for (def = pr.def_head; def; def = def->def_next) { for (def = pr.scope->head; def; def = def->def_next) {
if (def->scope) if (def->scope->parent)
continue; continue;
if (def->type->type == ev_func) { if (def->type->type == ev_func) {
} else if (def->type->type == ev_field) { } else if (def->type->type == ev_field) {
@ -165,10 +165,10 @@ WriteData (int crc)
printf ("%6i strofs\n", pr.strofs); printf ("%6i strofs\n", pr.strofs);
printf ("%6i statements\n", pr.num_statements); printf ("%6i statements\n", pr.num_statements);
printf ("%6i functions\n", pr.num_functions); printf ("%6i functions\n", pr.num_functions);
printf ("%6i global defs\n", numglobaldefs); printf ("%6i global defs\n", pr.scope->num_defs);
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.num_globals); printf ("%6i globals\n", pr.globals->size);
printf ("%6i entity fields\n", pr.size_fields); printf ("%6i entity fields\n", pr.size_fields);
} }
@ -227,10 +227,10 @@ WriteData (int crc)
SafeWrite (h, fields, numfielddefs * sizeof (ddef_t)); SafeWrite (h, fields, numfielddefs * sizeof (ddef_t));
progs.ofs_globals = ftell (h); progs.ofs_globals = ftell (h);
progs.numglobals = pr.num_globals; progs.numglobals = pr.globals->size;
for (i = 0; i < pr.num_globals; i++) for (i = 0; i < pr.globals->size; i++)
G_INT (i) = LittleLong (G_INT (i)); G_INT (i) = LittleLong (G_INT (i));
SafeWrite (h, pr.globals, pr.num_globals * 4); SafeWrite (h, pr.globals->data, pr.globals->size * 4);
if (options.verbosity >= -1) if (options.verbosity >= -1)
printf ("%6i TOTAL SIZE\n", (int) ftell (h)); printf ("%6i TOTAL SIZE\n", (int) ftell (h));
@ -302,8 +302,7 @@ WriteData (int crc)
void void
begin_compilation (void) begin_compilation (void)
{ {
pr.num_globals = RESERVED_OFS; pr.globals->size = RESERVED_OFS;
pr.def_tail = &pr.def_head;
pr.func_tail = &pr.func_head; pr.func_tail = &pr.func_head;
pr_error_count = 0; pr_error_count = 0;
@ -322,7 +321,7 @@ finish_compilation (void)
class_finish_module (); class_finish_module ();
// check to make sure all functions prototyped have code // check to make sure all functions prototyped have code
if (options.warnings.undefined_function) if (options.warnings.undefined_function)
for (d = pr.def_head; d; d = d->def_next) { for (d = pr.scope->head; d; d = d->def_next) {
if (d->type->type == ev_func && !d->scope) { // function args ok if (d->type->type == ev_func && !d->scope) { // function args ok
if (d->used) { if (d->used) {
if (!d->initialized) { if (!d->initialized) {
@ -339,32 +338,31 @@ finish_compilation (void)
if (options.code.debug) { if (options.code.debug) {
e.type = ex_string; e.type = ex_string;
e.e.string_val = debugfile; e.e.string_val = debugfile;
ReuseConstant (&e, PR_GetDef (&type_string, ".debug_file", 0, ReuseConstant (&e, PR_GetDef (&type_string, ".debug_file", pr.scope, 1));
&pr.num_globals));
} }
for (def = pr.def_head; def; def = def->def_next) { for (def = pr.scope->head; def; def = def->def_next) {
if (def->scope || def->absolute) if (def->scope || def->absolute)
continue; continue;
relocate_refs (def->refs, def->ofs); relocate_refs (def->refs, def->ofs);
} }
for (f = pr.func_head; f; f = f->next) { for (f = pr.func_head; f; f = f->next) {
if (f->builtin) if (f->builtin || !f->code)
continue; continue;
if (f->def->locals > num_localdefs) { if (f->scope->space->size > num_localdefs) {
num_localdefs = f->def->locals; num_localdefs = f->scope->space->size;
big_function = f->def->name; big_function = f->def->name;
} }
f->dfunc->parm_start = pr.num_globals; f->dfunc->parm_start = pr.globals->size;
for (def = f->def->scope_next; def; def = def->scope_next) { for (def = f->scope->head; def; def = def->def_next) {
if (def->absolute) if (def->absolute)
continue; continue;
def->ofs += pr.num_globals; def->ofs += pr.globals->size;
relocate_refs (def->refs, def->ofs); relocate_refs (def->refs, def->ofs);
} }
} }
pr.num_globals += num_localdefs; pr.globals->size += num_localdefs;
for (l = pr.labels; l; l = l->next) for (l = pr.labels; l; l = l->next)
relocate_refs (l->refs, l->ofs); relocate_refs (l->refs, l->ofs);

View file

@ -202,10 +202,10 @@ emit_struct(type_t *strct, const char *name)
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
new_struct_field (ivar_list, type_ivar, 0, vis_public); new_struct_field (ivar_list, type_ivar, 0, vis_public);
dsprintf (ivars_name, "_OBJ_INSTANCE_VARIABLES_%s", name); dsprintf (ivars_name, "_OBJ_INSTANCE_VARIABLES_%s", name);
ivars_def = PR_GetDef (ivar_list, ivars_name->str, 0, 0); ivars_def = PR_GetDef (ivar_list, ivars_name->str, pr.scope, 0);
if (ivars_def) if (ivars_def)
goto done; goto done;
ivars_def = PR_GetDef (ivar_list, ivars_name->str, 0, &pr.num_globals); ivars_def = PR_GetDef (ivar_list, ivars_name->str, pr.scope, 1);
ivars_def->initialized = ivars_def->constant = 1; ivars_def->initialized = ivars_def->constant = 1;
ivars = &G_STRUCT (pr_ivar_list_t, ivars_def->ofs); ivars = &G_STRUCT (pr_ivar_list_t, ivars_def->ofs);
ivars->ivar_count = count; ivars->ivar_count = count;
@ -252,7 +252,7 @@ process_enum (expr_t *enm)
} }
if ((structs && find_struct (name->e.string_val)) if ((structs && find_struct (name->e.string_val))
|| get_enum (name->e.string_val) || get_enum (name->e.string_val)
|| PR_GetDef (NULL, name->e.string_val, 0, 0)) { || PR_GetDef (NULL, name->e.string_val, pr.scope, 0)) {
error (name, "%s redeclared", name->e.string_val); error (name, "%s redeclared", name->e.string_val);
continue; continue;
} }

View file

@ -298,8 +298,7 @@ build_switch (expr_t *sw, case_node_t *tree, int op, expr_t *sw_val,
range->type = ex_uinteger; range->type = ex_uinteger;
def = PR_GetDef (array_type (&type_uinteger, high - low + 1), name, 0, def = PR_GetDef (array_type (&type_uinteger, high - low + 1), name, pr.scope, 1);
&pr.num_globals);
table = new_def_expr (def); table = new_def_expr (def);
if (tree->left) { if (tree->left) {