2002-05-08 05:15:19 +00:00
|
|
|
/*
|
|
|
|
function.c
|
|
|
|
|
|
|
|
QC function support code
|
|
|
|
|
2002-07-05 20:02:10 +00:00
|
|
|
Copyright (C) 2002 Bill Currie
|
2002-05-08 05:15:19 +00:00
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2002/5/7
|
|
|
|
|
|
|
|
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-08 21:24:24 +00:00
|
|
|
#include "QF/dstring.h"
|
|
|
|
|
2002-05-08 05:15:19 +00:00
|
|
|
#include "qfcc.h"
|
|
|
|
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "def.h"
|
2002-07-08 18:53:07 +00:00
|
|
|
#include "emit.h"
|
2002-06-01 05:30:16 +00:00
|
|
|
#include "expr.h"
|
2002-05-08 05:15:19 +00:00
|
|
|
#include "function.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "immediate.h"
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "opcodes.h"
|
2002-07-17 14:19:30 +00:00
|
|
|
#include "options.h"
|
2002-06-21 20:46:56 +00:00
|
|
|
#include "reloc.h"
|
2002-05-08 05:15:19 +00:00
|
|
|
#include "type.h"
|
|
|
|
|
2002-06-27 22:48:28 +00:00
|
|
|
static param_t *free_params;
|
|
|
|
static function_t *free_functions;
|
|
|
|
|
2002-05-08 05:15:19 +00:00
|
|
|
param_t *
|
|
|
|
new_param (const char *selector, type_t *type, const char *name)
|
|
|
|
{
|
2002-06-27 22:48:28 +00:00
|
|
|
param_t *param;
|
2002-05-08 05:15:19 +00:00
|
|
|
|
2002-06-27 22:48:28 +00:00
|
|
|
ALLOC (4096, param_t, params, param);
|
2002-05-08 05:15:19 +00:00
|
|
|
param->next = 0;
|
|
|
|
param->selector = selector;
|
|
|
|
param->type = type;
|
|
|
|
param->name = name;
|
|
|
|
|
|
|
|
return param;
|
|
|
|
}
|
|
|
|
|
2002-05-08 21:24:24 +00:00
|
|
|
param_t *
|
2002-05-08 17:33:28 +00:00
|
|
|
_reverse_params (param_t *params, param_t *next)
|
|
|
|
{
|
|
|
|
param_t *p = params;
|
|
|
|
if (params->next)
|
|
|
|
p = _reverse_params (params->next, params);
|
|
|
|
params->next = next;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
param_t *
|
|
|
|
reverse_params (param_t *params)
|
|
|
|
{
|
|
|
|
if (!params)
|
|
|
|
return 0;
|
|
|
|
return _reverse_params (params, 0);
|
|
|
|
}
|
|
|
|
|
2002-08-20 02:09:34 +00:00
|
|
|
param_t *
|
|
|
|
copy_params (param_t *params)
|
|
|
|
{
|
|
|
|
param_t *n_parms = 0, **p = &n_parms;
|
|
|
|
|
|
|
|
while (params) {
|
|
|
|
*p = new_param (params->selector, params->type, params->name);
|
|
|
|
params = params->next;
|
|
|
|
p = &(*p)->next;
|
|
|
|
}
|
|
|
|
return n_parms;
|
|
|
|
}
|
|
|
|
|
2002-05-08 05:15:19 +00:00
|
|
|
type_t *
|
|
|
|
parse_params (type_t *type, param_t *parms)
|
|
|
|
{
|
|
|
|
param_t *p;
|
|
|
|
type_t new;
|
|
|
|
|
|
|
|
memset (&new, 0, sizeof (new));
|
|
|
|
new.type = ev_func;
|
|
|
|
new.aux_type = type;
|
|
|
|
new.num_parms = 0;
|
|
|
|
|
2002-05-09 20:12:28 +00:00
|
|
|
for (p = parms; p; p = p->next) {
|
2002-05-08 05:15:19 +00:00
|
|
|
if (new.num_parms > MAX_PARMS) {
|
|
|
|
error (0, "too many params");
|
|
|
|
return type;
|
|
|
|
}
|
2002-05-08 17:33:28 +00:00
|
|
|
if (!p->selector && !p->type && !p->name) {
|
|
|
|
if (p->next) {
|
|
|
|
error (0, "internal error");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
new.num_parms = -(new.num_parms + 1);
|
2002-05-09 20:12:28 +00:00
|
|
|
} else if (p->type) {
|
2002-05-08 17:33:28 +00:00
|
|
|
new.parm_types[new.num_parms] = p->type;
|
2002-05-09 20:12:28 +00:00
|
|
|
new.num_parms++;
|
2002-05-08 17:33:28 +00:00
|
|
|
}
|
2002-05-08 05:15:19 +00:00
|
|
|
}
|
|
|
|
//print_type (&new); puts("");
|
2002-05-09 06:34:48 +00:00
|
|
|
return find_type (&new);
|
2002-05-08 05:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
build_scope (function_t *f, def_t *func, param_t *params)
|
|
|
|
{
|
2002-05-22 20:43:29 +00:00
|
|
|
int i;
|
|
|
|
def_t *def;
|
2002-05-08 17:33:28 +00:00
|
|
|
param_t *p;
|
2002-10-16 02:28:08 +00:00
|
|
|
def_t *args = 0;
|
2002-06-17 05:28:43 +00:00
|
|
|
int parm_ofs[MAX_PARMS];
|
2002-05-08 05:15:19 +00:00
|
|
|
|
2002-06-09 05:19:13 +00:00
|
|
|
f->scope = new_scope (sc_params, new_defspace (), pr.scope);
|
2002-05-08 05:15:19 +00:00
|
|
|
|
2002-05-22 20:43:29 +00:00
|
|
|
if (func->type->num_parms < 0) {
|
2002-10-16 02:28:08 +00:00
|
|
|
args = get_def (&type_va_list, ".args", f->scope, st_local);
|
|
|
|
args->used = 1;
|
|
|
|
def_initialized (args);
|
2002-05-22 20:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (p = params, i = 0; p; p = p->next) {
|
2002-05-08 17:33:28 +00:00
|
|
|
if (!p->selector && !p->type && !p->name)
|
2002-05-08 05:15:19 +00:00
|
|
|
continue; // ellipsis marker
|
2002-05-09 20:12:28 +00:00
|
|
|
if (!p->type)
|
|
|
|
continue; // non-param selector
|
2002-06-28 17:59:32 +00:00
|
|
|
def = get_def (p->type, p->name, f->scope, st_local);
|
2002-06-17 05:28:43 +00:00
|
|
|
parm_ofs[i] = def->ofs;
|
|
|
|
if (i > 0 && parm_ofs[i] < parm_ofs[i - 1]) {
|
2002-06-01 04:41:25 +00:00
|
|
|
error (0, "bad parm order");
|
|
|
|
abort ();
|
|
|
|
}
|
2002-05-08 17:33:28 +00:00
|
|
|
//printf ("%s%s %d\n", p == params ? "" : " ", p->name, def->ofs);
|
2002-05-08 05:15:19 +00:00
|
|
|
def->used = 1; // don't warn for unused params
|
2002-06-09 04:30:02 +00:00
|
|
|
def_initialized (def); // params are assumed to be initialized
|
2002-05-22 20:43:29 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2002-10-16 02:28:08 +00:00
|
|
|
if (args) {
|
2002-05-22 20:43:29 +00:00
|
|
|
while (i < MAX_PARMS) {
|
2002-06-28 17:59:32 +00:00
|
|
|
def = get_def (&type_vector, 0, f->scope, st_local);
|
2002-05-22 20:43:29 +00:00
|
|
|
def->used = 1;
|
|
|
|
i++;
|
|
|
|
}
|
2002-05-08 05:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function_t *
|
2002-06-27 22:48:28 +00:00
|
|
|
new_function (const char *name)
|
2002-05-08 05:15:19 +00:00
|
|
|
{
|
|
|
|
function_t *f;
|
|
|
|
|
2002-06-27 22:48:28 +00:00
|
|
|
ALLOC (1024, function_t, functions, f);
|
|
|
|
|
2002-06-06 21:51:47 +00:00
|
|
|
*pr.func_tail = f;
|
|
|
|
pr.func_tail = &f->next;
|
2002-06-11 06:54:32 +00:00
|
|
|
f->function_num = pr.num_functions++;
|
2002-06-27 22:48:28 +00:00
|
|
|
f->s_name = ReuseString (name);
|
2002-07-03 21:32:03 +00:00
|
|
|
f->s_file = pr.source_file;
|
2002-07-17 14:19:30 +00:00
|
|
|
if (options.code.debug)
|
|
|
|
f->aux = new_auxfunction ();
|
2002-05-08 05:15:19 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2004-02-11 00:36:34 +00:00
|
|
|
function_t *
|
|
|
|
build_code_function (function_t *f, expr_t *state_expr, expr_t *statements)
|
|
|
|
{
|
|
|
|
build_function (f);
|
|
|
|
if (state_expr) {
|
|
|
|
state_expr->next = statements;
|
|
|
|
emit_function (f, state_expr);
|
|
|
|
} else {
|
|
|
|
emit_function (f, statements);
|
|
|
|
}
|
|
|
|
finish_function (f);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2002-06-21 20:46:56 +00:00
|
|
|
function_t *
|
2002-05-17 18:35:54 +00:00
|
|
|
build_builtin_function (def_t *def, expr_t *bi_val)
|
|
|
|
{
|
|
|
|
function_t *f;
|
|
|
|
|
|
|
|
if (def->type->type != ev_func) {
|
|
|
|
error (bi_val, "%s is not a function", def->name);
|
2002-06-21 20:46:56 +00:00
|
|
|
return 0;
|
2002-05-17 18:35:54 +00:00
|
|
|
}
|
2004-02-11 01:53:17 +00:00
|
|
|
if (def->constant) {
|
|
|
|
error (bi_val, "%s redefined", def->name);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-01-25 08:55:03 +00:00
|
|
|
|
2002-05-17 18:35:54 +00:00
|
|
|
if (bi_val->type != ex_integer && bi_val->type != ex_float) {
|
|
|
|
error (bi_val, "invalid constant for = #");
|
2002-06-21 20:46:56 +00:00
|
|
|
return 0;
|
2002-05-17 18:35:54 +00:00
|
|
|
}
|
|
|
|
|
2002-06-27 22:48:28 +00:00
|
|
|
f = new_function (def->name);
|
2002-05-17 18:35:54 +00:00
|
|
|
|
|
|
|
f->builtin = bi_val->type == ex_integer ? bi_val->e.integer_val
|
|
|
|
: (int)bi_val->e.float_val;
|
|
|
|
f->def = def;
|
2002-07-13 06:09:03 +00:00
|
|
|
reloc_def_func (f, def->ofs);
|
2002-05-17 18:35:54 +00:00
|
|
|
build_function (f);
|
|
|
|
finish_function (f);
|
2002-06-21 20:46:56 +00:00
|
|
|
return f;
|
2002-05-17 18:35:54 +00:00
|
|
|
}
|
|
|
|
|
2002-05-08 05:15:19 +00:00
|
|
|
void
|
|
|
|
build_function (function_t *f)
|
|
|
|
{
|
|
|
|
f->def->constant = 1;
|
2003-04-25 17:00:22 +00:00
|
|
|
f->def->nosave = 1;
|
2002-05-08 05:15:19 +00:00
|
|
|
f->def->initialized = 1;
|
2002-06-11 06:54:32 +00:00
|
|
|
G_FUNCTION (f->def->ofs) = f->function_num;
|
2002-05-08 05:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
finish_function (function_t *f)
|
|
|
|
{
|
|
|
|
if (f->aux) {
|
|
|
|
def_t *def;
|
2002-06-11 06:54:32 +00:00
|
|
|
f->aux->function = f->function_num;
|
2002-07-17 14:19:30 +00:00
|
|
|
if (f->scope) {
|
|
|
|
for (def = f->scope->head; def; def = def->def_next) {
|
|
|
|
if (def->name) {
|
2002-07-17 15:40:08 +00:00
|
|
|
def_to_ddef (def, new_local (), 0);
|
2002-07-17 14:19:30 +00:00
|
|
|
f->aux->num_locals++;
|
|
|
|
}
|
2002-05-08 05:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
emit_function (function_t *f, expr_t *e)
|
|
|
|
{
|
|
|
|
//printf (" %s =\n", f->def->name);
|
|
|
|
|
|
|
|
if (f->aux)
|
|
|
|
lineno_base = f->aux->source_line;
|
|
|
|
|
2002-06-09 03:57:20 +00:00
|
|
|
current_scope = f->scope;
|
2002-05-08 05:15:19 +00:00
|
|
|
while (e) {
|
2002-07-03 21:32:03 +00:00
|
|
|
//printf ("%d ", pr.source_line);
|
2002-05-08 05:15:19 +00:00
|
|
|
//print_expr (e);
|
|
|
|
//puts("");
|
|
|
|
|
|
|
|
emit_expr (e);
|
|
|
|
e = e->next;
|
|
|
|
}
|
2002-07-08 18:53:07 +00:00
|
|
|
emit_statement (0, op_done, 0, 0, 0);
|
2002-06-09 04:30:02 +00:00
|
|
|
flush_scope (current_scope, 0);
|
2002-06-09 03:57:20 +00:00
|
|
|
current_scope = pr.scope;
|
2002-06-09 04:30:02 +00:00
|
|
|
reset_tempdefs ();
|
2002-05-08 05:15:19 +00:00
|
|
|
|
|
|
|
//puts ("");
|
|
|
|
}
|
2002-06-27 22:48:28 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|