various memory saving tricks. seems to have not broken anything

This commit is contained in:
Bill Currie 2002-06-27 22:48:28 +00:00
parent 4e467ac8df
commit 001571e25a
12 changed files with 140 additions and 103 deletions

View file

@ -36,12 +36,12 @@
typedef struct function_s { typedef struct function_s {
struct function_s *next; struct function_s *next;
dfunction_t *dfunc;
pr_auxfunction_t *aux; // debug info; pr_auxfunction_t *aux; // debug info;
int builtin; // if non 0, call an internal function int builtin; // if non 0, call an internal function
int code; // first statement int code; // first statement
int function_num; int function_num;
const char *file; // source file with definition string_t s_file; // source file with definition
string_t s_name;
int file_line; int file_line;
struct def_s *def; struct def_s *def;
struct scope_s *scope; struct scope_s *scope;
@ -67,10 +67,11 @@ param_t *_reverse_params (param_t *params, param_t *next);
param_t *reverse_params (param_t *params); param_t *reverse_params (param_t *params);
struct type_s *parse_params (struct type_s *type, param_t *params); struct type_s *parse_params (struct type_s *type, param_t *params);
void build_scope (function_t *f, struct def_s *func, param_t *params); void build_scope (function_t *f, struct def_s *func, param_t *params);
function_t *new_function (void); function_t *new_function (const char *name);
function_t *build_builtin_function (struct def_s *def, struct expr_s *bi_val); function_t *build_builtin_function (struct def_s *def, struct expr_s *bi_val);
void build_function (function_t *f); void build_function (function_t *f);
void finish_function (function_t *f); void finish_function (function_t *f);
void emit_function (function_t *f, struct expr_s *e); void emit_function (function_t *f, struct expr_s *e);
int function_parms (function_t *f, byte *parm_size);
#endif//__function_h #endif//__function_h

View file

@ -92,6 +92,8 @@ extern string_t s_file; // filename for function definition
const char *strip_path (const char *filename); const char *strip_path (const char *filename);
const char *save_string (const char *str);
#define ALLOC(s, t, n, v) \ #define ALLOC(s, t, n, v) \
do { \ do { \
if (!free_##n) { \ if (!free_##n) { \

View file

@ -475,7 +475,7 @@ class_finish_module (void)
exec_class_def = get_def (&type_obj_exec_class, "__obj_exec_class", exec_class_def = get_def (&type_obj_exec_class, "__obj_exec_class",
pr.scope, 1); pr.scope, 1);
exec_class_func = new_function (); exec_class_func = new_function ("__obj_exec_class");
exec_class_func->builtin = 0; exec_class_func->builtin = 0;
exec_class_func->def = exec_class_def; exec_class_func->def = exec_class_def;
exec_class_func->refs = new_reloc (exec_class_def->ofs, rel_def_func); exec_class_func->refs = new_reloc (exec_class_def->ofs, rel_def_func);
@ -483,7 +483,7 @@ class_finish_module (void)
finish_function (exec_class_func); finish_function (exec_class_func);
init_def = get_def (&type_function, ".ctor", pr.scope, 1); init_def = get_def (&type_function, ".ctor", pr.scope, 1);
init_func = new_function (); init_func = new_function (".ctor");
init_func->def = init_def; init_func->def = init_def;
init_func->refs = new_reloc (init_def->ofs, rel_def_func); init_func->refs = new_reloc (init_def->ofs, rel_def_func);
init_func->code = pr.num_statements; init_func->code = pr.num_statements;

View file

@ -134,9 +134,10 @@ static void
vector_component (def_t *vec, int comp, scope_t *scope) vector_component (def_t *vec, int comp, scope_t *scope)
{ {
def_t *d; def_t *d;
const char *name;
d = new_def (&type_float, va (vector_component_names[comp], vec->name), name = save_string (va (vector_component_names[comp], vec->name));
scope); d = new_def (&type_float, name, scope);
d->used = 1; d->used = 1;
d->parent = vec; d->parent = vec;
d->ofs = vec->ofs + comp; d->ofs = vec->ofs + comp;
@ -147,9 +148,10 @@ static void
vector_field_component (def_t *vec, int comp, scope_t *scope) vector_field_component (def_t *vec, int comp, scope_t *scope)
{ {
def_t *d; def_t *d;
const char *name;
d = new_def (&type_floatfield, va (vector_component_names[comp], vec->name), name = save_string (va (vector_component_names[comp], vec->name));
scope); d = new_def (&type_floatfield, name, scope);
d->used = 1; // always `used' d->used = 1; // always `used'
d->parent = vec; d->parent = vec;
d->ofs = vec->ofs + comp; d->ofs = vec->ofs + comp;
@ -217,7 +219,7 @@ new_def (type_t *type, const char *name, scope_t *scope)
def->return_addr = __builtin_return_address (0); def->return_addr = __builtin_return_address (0);
def->name = name ? strdup (name) : 0; def->name = name;
def->type = type; def->type = type;
def->scope = scope; def->scope = scope;

View file

@ -669,21 +669,20 @@ print_expr (expr_t *e)
static expr_t * static expr_t *
do_op_string (int op, expr_t *e1, expr_t *e2) do_op_string (int op, expr_t *e1, expr_t *e2)
{ {
int len;
char *buf;
const char *s1, *s2; const char *s1, *s2;
static dstring_t *temp_str;
s1 = e1->e.string_val ? e1->e.string_val : ""; s1 = e1->e.string_val ? e1->e.string_val : "";
s2 = e2->e.string_val ? e2->e.string_val : ""; s2 = e2->e.string_val ? e2->e.string_val : "";
switch (op) { switch (op) {
case '+': case '+':
len = strlen (s1) + strlen (s2) + 1; if (!temp_str)
buf = malloc (len); temp_str = dstring_newstr ();
SYS_CHECKMEM (buf); dstring_clearstr (temp_str);
strcpy (buf, s1); dstring_appendstr (temp_str, s1);
strcat (buf, s2); dstring_appendstr (temp_str, s2);
e1->e.string_val = buf; e1->e.string_val = save_string (temp_str->str);
break; break;
case LT: case LT:
e1->type = ex_integer; e1->type = ex_integer;

View file

@ -54,11 +54,15 @@ static const char rcsid[] =
#include "reloc.h" #include "reloc.h"
#include "type.h" #include "type.h"
static param_t *free_params;
static function_t *free_functions;
param_t * param_t *
new_param (const char *selector, type_t *type, const char *name) new_param (const char *selector, type_t *type, const char *name)
{ {
param_t *param = malloc (sizeof (param_t)); param_t *param;
ALLOC (4096, param_t, params, param);
param->next = 0; param->next = 0;
param->selector = selector; param->selector = selector;
param->type = type; param->type = type;
@ -165,14 +169,17 @@ build_scope (function_t *f, def_t *func, param_t *params)
} }
function_t * function_t *
new_function (void) new_function (const char *name)
{ {
function_t *f; function_t *f;
f = calloc (1, sizeof (function_t)); ALLOC (1024, function_t, functions, f);
*pr.func_tail = f; *pr.func_tail = f;
pr.func_tail = &f->next; pr.func_tail = &f->next;
f->function_num = pr.num_functions++; f->function_num = pr.num_functions++;
f->s_name = ReuseString (name);
f->s_file = s_file;
return f; return f;
} }
@ -191,7 +198,7 @@ build_builtin_function (def_t *def, expr_t *bi_val)
return 0; return 0;
} }
f = new_function (); f = new_function (def->name);
f->builtin = bi_val->type == ex_integer ? bi_val->e.integer_val f->builtin = bi_val->type == ex_integer ? bi_val->e.integer_val
: (int)bi_val->e.float_val; : (int)bi_val->e.float_val;
@ -213,28 +220,6 @@ build_function (function_t *f)
void void
finish_function (function_t *f) finish_function (function_t *f)
{ {
dfunction_t *df;
int i, count;
df = calloc (1, sizeof (dfunction_t));
f->dfunc = df;
if (f->builtin)
df->first_statement = -f->builtin;
else
df->first_statement = f->code;
df->s_name = ReuseString (f->def->name);
df->s_file = s_file;
df->numparms = f->def->type->num_parms;
if (f->scope)
df->locals = f->scope->space->size;
df->parm_start = 0;
if ((count = df->numparms) < 0)
count = -count - 1;
for (i = 0; i < count; i++)
df->parm_size[i] = type_size (f->def->type->parm_types[i]);
if (f->aux) { if (f->aux) {
def_t *def; def_t *def;
f->aux->function = f->function_num; f->aux->function = f->function_num;
@ -275,3 +260,18 @@ emit_function (function_t *f, expr_t *e)
//puts (""); //puts ("");
} }
int
function_parms (function_t *f, byte *parm_size)
{
int count, i;
if (f->def->type->num_parms >= 0)
count = f->def->type->num_parms;
else
count = -f->def->type->num_parms - 1;
for (i = 0; i < count; i++)
parm_size[i] = type_size (f->def->type->parm_types[i]);
return f->def->type->num_parms;
}

View file

@ -166,8 +166,8 @@ setup_data (void)
for (d = pr.scope->head; d; d = d->def_next) for (d = pr.scope->head; d; d = d->def_next)
write_def (d, def++, &reloc); write_def (d, def++, &reloc);
for (f = pr.func_head; f; f = f->next) { for (f = pr.func_head; f; f = f->next) {
func->name = LittleLong (f->dfunc->s_name); func->name = LittleLong (f->s_name);
func->file = LittleLong (f->dfunc->s_file); func->file = LittleLong (f->s_file);
func->line = LittleLong (f->def->line); func->line = LittleLong (f->def->line);
func->builtin = LittleLong (f->builtin); func->builtin = LittleLong (f->builtin);
func->code = LittleLong (f->code); func->code = LittleLong (f->code);
@ -182,8 +182,7 @@ setup_data (void)
func->num_local_defs = LittleLong (f->scope->num_defs); func->num_local_defs = LittleLong (f->scope->num_defs);
if (f->aux) if (f->aux)
func->line_info = LittleLong (f->aux->line_info); func->line_info = LittleLong (f->aux->line_info);
func->num_parms = LittleLong (f->dfunc->numparms); func->num_parms = LittleLong (function_parms (f, func->parm_size));
memcpy (func->parm_size, f->dfunc->parm_size, MAX_PARMS);
func->relocs = LittleLong (reloc - relocs); func->relocs = LittleLong (reloc - relocs);
write_relocs (f->refs, &reloc); write_relocs (f->refs, &reloc);

View file

@ -43,8 +43,10 @@ static const char rcsid[] =
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <QF/dstring.h>
#include <QF/hash.h> #include <QF/hash.h>
#include <QF/sys.h> #include <QF/sys.h>
#include "qfcc.h" #include "qfcc.h"
#include "expr.h" #include "expr.h"
#include "class.h" #include "class.h"
@ -62,7 +64,7 @@ int do_grab (char *token);
void add_frame_macro (char *token); void add_frame_macro (char *token);
char *make_string (char *token); const char *make_string (char *token);
extern YYSTYPE yylval; extern YYSTYPE yylval;
extern int element_flag; extern int element_flag;
@ -136,18 +138,18 @@ m ([\-+]?)
} }
'(\\[^xX0-7\r\n]|[^'\r\n]|\\[xX][0-9A-Fa-f]+|\\[0-7]+)*' { '(\\[^xX0-7\r\n]|[^'\r\n]|\\[xX][0-9A-Fa-f]+|\\[0-7]+)*' {
char *str = make_string (yytext); const char *str = make_string (yytext);
if (str[1]) if (str[1])
warning (0, "multibyte char constant"); warning (0, "multibyte char constant");
yylval.integer_val = *str; yylval.integer_val = *str;
free (str);
return INT_VAL; return INT_VAL;
} }
^#{s}+{DIGIT}+{s}+\"(\.|[^"\n])*\".*$ { ^#{s}+{DIGIT}+{s}+\"(\.|[^"\n])*\".*$ {
char *p; char *p;
char *s; char *s;
const char *str;
int line; int line;
p = yytext + 1; p = yytext + 1;
@ -157,12 +159,11 @@ m ([\-+]?)
p++; p++;
if (!*p) if (!*p)
error (0, "Unexpected end of file"); error (0, "Unexpected end of file");
s = make_string (p); // grab the filename str = make_string (p); // grab the filename
while (*p && *p != '\n') // ignore flags while (*p && *p != '\n') // ignore flags
p++; p++;
pr_source_line = line - 1; pr_source_line = line - 1;
s_file = ReuseString (strip_path (s)); s_file = ReuseString (strip_path (str));
free (s);
} }
[+\-*/&|^%]= { [+\-*/&|^%]= {
@ -335,7 +336,7 @@ type_or_name (char *token)
yylval.type = type; yylval.type = type;
return TYPE; return TYPE;
} }
yylval.string_val = strdup (token); yylval.string_val = save_string (token);
return NAME; return NAME;
} }
@ -369,7 +370,6 @@ static void
frame_free (void *_f, void *unused) frame_free (void *_f, void *unused)
{ {
frame_t *f = (frame_t *)_f; frame_t *f = (frame_t *)_f;
free ((char*)f->name);
f->next = free_frames; f->next = free_frames;
free_frames = f; free_frames = f;
} }
@ -412,7 +412,7 @@ add_frame_macro (char *token)
frame_t *frame; frame_t *frame;
ALLOC (1024, frame_t, frames, frame); ALLOC (1024, frame_t, frames, frame);
frame->name = strdup (token); frame->name = save_string (token);
frame->num = frame_number++; frame->num = frame_number++;
Hash_Add (frame_tab, frame); Hash_Add (frame_tab, frame);
} }
@ -425,18 +425,22 @@ clear_frame_macros (void)
Hash_FlushTable (frame_tab); Hash_FlushTable (frame_tab);
} }
char * const char *
make_string (char *token) make_string (char *token)
{ {
char *str, *s; char s[2];
int c; int c;
int i; int i;
int mask; int mask;
int boldnext; int boldnext;
int quote; int quote;
static dstring_t *str;
s = str = malloc (strlen (token) + 1); if (!str)
SYS_CHECKMEM (str); str = dstring_newstr ();
dstring_clearstr (str);
s[1] = 0;
mask = 0x00; mask = 0x00;
boldnext = 0; boldnext = 0;
@ -574,17 +578,17 @@ make_string (char *token)
break; break;
} }
} else if (c == quote) { } else if (c == quote) {
*s++ = 0;
break;; break;;
} }
if (boldnext) if (boldnext)
c = c ^ 0x80; c = c ^ 0x80;
boldnext = 0; boldnext = 0;
c = c ^ mask; c = c ^ mask;
*s++ = c; s[0] = c;
dstring_appendstr (str, s);
} while (1); } while (1);
return str; return save_string (str->str);
} }
static void *(*const yy_flex_realloc_hack)(void *,yy_size_t) = yy_flex_realloc; static void *(*const yy_flex_realloc_hack)(void *,yy_size_t) = yy_flex_realloc;

View file

@ -96,7 +96,7 @@ void free_local_inits (hashtab_t *def_list);
expr_t *expr; expr_t *expr;
int integer_val; int integer_val;
float float_val; float float_val;
char *string_val; const char *string_val;
float vector_val[3]; float vector_val[3];
float quaternion_val[4]; float quaternion_val[4];
struct function_s *function; struct function_s *function;
@ -479,7 +479,7 @@ opt_comma
begin_function begin_function
: /*empty*/ : /*empty*/
{ {
$$ = current_func = new_function (); $$ = current_func = new_function (current_def->name);
$$->def = current_def; $$->def = current_def;
$$->refs = new_reloc ($$->def->ofs, rel_def_func); $$->refs = new_reloc ($$->def->ofs, rel_def_func);
$$->code = pr.num_statements; $$->code = pr.num_statements;
@ -1204,29 +1204,29 @@ keywordselector
selector selector
: NAME : NAME
| TYPE { $$ = strdup (yytext); } | TYPE { $$ = save_string (yytext); }
| reserved_word | reserved_word
; ;
reserved_word reserved_word
: LOCAL { $$ = strdup (yytext); } : LOCAL { $$ = save_string (yytext); }
| RETURN { $$ = strdup (yytext); } | RETURN { $$ = save_string (yytext); }
| WHILE { $$ = strdup (yytext); } | WHILE { $$ = save_string (yytext); }
| DO { $$ = strdup (yytext); } | DO { $$ = save_string (yytext); }
| IF { $$ = strdup (yytext); } | IF { $$ = save_string (yytext); }
| ELSE { $$ = strdup (yytext); } | ELSE { $$ = save_string (yytext); }
| FOR { $$ = strdup (yytext); } | FOR { $$ = save_string (yytext); }
| BREAK { $$ = strdup (yytext); } | BREAK { $$ = save_string (yytext); }
| CONTINUE { $$ = strdup (yytext); } | CONTINUE { $$ = save_string (yytext); }
| SWITCH { $$ = strdup (yytext); } | SWITCH { $$ = save_string (yytext); }
| CASE { $$ = strdup (yytext); } | CASE { $$ = save_string (yytext); }
| DEFAULT { $$ = strdup (yytext); } | DEFAULT { $$ = save_string (yytext); }
| NIL { $$ = strdup (yytext); } | NIL { $$ = save_string (yytext); }
| STRUCT { $$ = strdup (yytext); } | STRUCT { $$ = save_string (yytext); }
| UNION { $$ = strdup (yytext); } | UNION { $$ = save_string (yytext); }
| ENUM { $$ = strdup (yytext); } | ENUM { $$ = save_string (yytext); }
| TYPEDEF { $$ = strdup (yytext); } | TYPEDEF { $$ = save_string (yytext); }
| SUPER { $$ = strdup (yytext); } | SUPER { $$ = save_string (yytext); }
; ;
keyworddecl keyworddecl

View file

@ -98,6 +98,27 @@ const char *big_function = 0;
ddef_t *fields; ddef_t *fields;
int numfielddefs; int numfielddefs;
hashtab_t *saved_strings;
static const char *
ss_get_key (void *s, void *unused)
{
return (const char *)s;
}
const char *
save_string (const char *str)
{
char *s;
if (!saved_strings)
saved_strings = Hash_NewTable (16381, ss_get_key, 0, 0);
s = Hash_Find (saved_strings, str);
if (s)
return s;
s = strdup (str);
Hash_Add (saved_strings, s);
return s;
}
void void
InitData (void) InitData (void)
@ -134,8 +155,8 @@ WriteData (int crc)
FILE *h; FILE *h;
int i; int i;
globals = calloc (pr.scope->num_defs, sizeof (ddef_t)); globals = calloc (pr.scope->num_defs + 1, sizeof (ddef_t));
fields = calloc (pr.scope->num_defs, sizeof (ddef_t)); fields = calloc (pr.scope->num_defs + 1, sizeof (ddef_t));
for (def = pr.scope->head; def; def = def->def_next) { for (def = pr.scope->head; def; def = def->def_next) {
if (!def->global || !def->name) if (!def->global || !def->name)
@ -192,20 +213,17 @@ WriteData (int crc)
SafeWrite (h, pr.statements, pr.num_statements * sizeof (dstatement_t)); SafeWrite (h, pr.statements, pr.num_statements * sizeof (dstatement_t));
{ {
function_t *f; dfunction_t *df;
progs.ofs_functions = ftell (h); progs.ofs_functions = ftell (h);
progs.numfunctions = pr.num_functions; progs.numfunctions = pr.num_functions;
pr.functions = malloc (pr.num_functions * sizeof (dfunction_t)); for (i = 0, df = pr.functions + 1; i < pr.num_functions; i++, df++) {
for (i = 1, f = pr.func_head; f; i++, f = f->next) { df->first_statement = LittleLong (df->first_statement);
pr.functions[i].first_statement = df->parm_start = LittleLong (df->parm_start);
LittleLong (f->dfunc->first_statement); df->s_name = LittleLong (df->s_name);
pr.functions[i].parm_start = LittleLong (f->dfunc->parm_start); df->s_file = LittleLong (df->s_file);
pr.functions[i].s_name = LittleLong (f->dfunc->s_name); df->numparms = LittleLong (df->numparms);
pr.functions[i].s_file = LittleLong (f->dfunc->s_file); df->locals = LittleLong (df->locals);
pr.functions[i].numparms = LittleLong (f->dfunc->numparms);
pr.functions[i].locals = LittleLong (f->dfunc->locals);
memcpy (pr.functions[i].parm_size, f->dfunc->parm_size, MAX_PARMS);
} }
SafeWrite (h, pr.functions, pr.num_functions * sizeof (dfunction_t)); SafeWrite (h, pr.functions, pr.num_functions * sizeof (dfunction_t));
} }
@ -320,6 +338,7 @@ finish_compilation (void)
def_t *def; def_t *def;
expr_t e; expr_t e;
ex_label_t *l; ex_label_t *l;
dfunction_t *df;
class_finish_module (); class_finish_module ();
// check to make sure all functions prototyped have code // check to make sure all functions prototyped have code
@ -351,14 +370,25 @@ finish_compilation (void)
relocate_refs (def->refs, def->ofs); relocate_refs (def->refs, def->ofs);
} }
for (f = pr.func_head; f; f = f->next) { pr.functions = calloc (pr.num_functions + 1, sizeof (dfunction_t));
if (f->builtin || !f->code) for (df = pr.functions + 1, f = pr.func_head; f; df++, f = f->next) {
df->s_name = f->s_name;
df->s_file = f->s_file;
df->numparms = function_parms (f, df->parm_size);
if (f->scope)
df->locals = f->scope->space->size;
if (f->builtin) {
df->first_statement = -f->builtin;
continue; continue;
}
if (!f->code)
continue;
df->first_statement = f->code;
if (f->scope->space->size > num_localdefs) { if (f->scope->space->size > num_localdefs) {
num_localdefs = f->scope->space->size; num_localdefs = f->scope->space->size;
big_function = f->def->name; big_function = f->def->name;
} }
f->dfunc->parm_start = pr.near_data->size; df->parm_start = pr.near_data->size;
for (def = f->scope->head; def; def = def->def_next) { for (def = f->scope->head; def; def = def->def_next) {
if (def->absolute) if (def->absolute)
continue; continue;

View file

@ -116,7 +116,7 @@ new_reloc (int ofs, reloc_type type)
{ {
reloc_t *ref; reloc_t *ref;
ALLOC (1024, reloc_t, refs, ref); ALLOC (16384, reloc_t, refs, ref);
ref->ofs = ofs; ref->ofs = ofs;
ref->type = type; ref->type = type;
return ref; return ref;

View file

@ -146,7 +146,7 @@ new_struct (const char *name)
strct->type->class = (struct class_s *)strct; strct->type->class = (struct class_s *)strct;
strct->is_union = 0; strct->is_union = 0;
if (name) { if (name) {
strct->type->name = strdup (name); strct->type->name = save_string (name);
Hash_Add (structs, strct); Hash_Add (structs, strct);
} }
return strct->type; return strct->type;