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
|
|
|
|
|
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>
|
|
|
|
|
|
2012-12-06 11:52:53 +00:00
|
|
|
|
#include "QF/alloc.h"
|
2002-05-08 21:24:24 +00:00
|
|
|
|
#include "QF/dstring.h"
|
2004-11-13 11:50:00 +00:00
|
|
|
|
#include "QF/hash.h"
|
|
|
|
|
#include "QF/va.h"
|
2002-05-08 21:24:24 +00:00
|
|
|
|
|
2002-05-08 05:15:19 +00:00
|
|
|
|
#include "qfcc.h"
|
|
|
|
|
|
2011-01-09 10:41:24 +00:00
|
|
|
|
#include "codespace.h"
|
2002-06-04 21:23:39 +00:00
|
|
|
|
#include "debug.h"
|
|
|
|
|
#include "def.h"
|
2011-01-17 13:33:33 +00:00
|
|
|
|
#include "defspace.h"
|
2011-01-24 12:54:57 +00:00
|
|
|
|
#include "diagnostic.h"
|
2002-07-08 18:53:07 +00:00
|
|
|
|
#include "emit.h"
|
2002-06-01 05:30:16 +00:00
|
|
|
|
#include "expr.h"
|
2012-10-30 08:41:40 +00:00
|
|
|
|
#include "flow.h"
|
2002-05-08 05:15:19 +00:00
|
|
|
|
#include "function.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"
|
2012-12-02 10:53:03 +00:00
|
|
|
|
#include "shared.h"
|
2011-01-19 06:47:45 +00:00
|
|
|
|
#include "statements.h"
|
2011-01-24 12:31:32 +00:00
|
|
|
|
#include "strpool.h"
|
2011-01-13 05:45:53 +00:00
|
|
|
|
#include "symtab.h"
|
2002-05-08 05:15:19 +00:00
|
|
|
|
#include "type.h"
|
2011-03-22 03:24:39 +00:00
|
|
|
|
#include "value.h"
|
2002-05-08 05:15:19 +00:00
|
|
|
|
|
2013-03-08 13:16:31 +00:00
|
|
|
|
static param_t *params_freelist;
|
|
|
|
|
static function_t *functions_freelist;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
static hashtab_t *overloaded_functions;
|
|
|
|
|
static hashtab_t *function_map;
|
|
|
|
|
|
|
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
|
ol_func_get_key (const void *_f, void *unused)
|
2004-11-13 11:50:00 +00:00
|
|
|
|
{
|
|
|
|
|
overloaded_function_t *f = (overloaded_function_t *) _f;
|
|
|
|
|
return f->full_name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
|
func_map_get_key (const void *_f, void *unused)
|
2004-11-13 11:50:00 +00:00
|
|
|
|
{
|
|
|
|
|
overloaded_function_t *f = (overloaded_function_t *) _f;
|
|
|
|
|
return f->name;
|
|
|
|
|
}
|
2002-06-27 22:48:28 +00:00
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-13 05:45:53 +00:00
|
|
|
|
param_t *
|
|
|
|
|
param_append_identifiers (param_t *params, symbol_t *idents, type_t *type)
|
|
|
|
|
{
|
|
|
|
|
param_t **p = ¶ms;
|
|
|
|
|
|
|
|
|
|
while (*p)
|
|
|
|
|
p = &(*p)->next;
|
|
|
|
|
if (!idents) {
|
|
|
|
|
*p = new_param (0, 0, 0);
|
|
|
|
|
p = &(*p)->next;
|
|
|
|
|
}
|
|
|
|
|
while (idents) {
|
|
|
|
|
idents->type = type;
|
|
|
|
|
*p = new_param (0, type, idents->name);
|
|
|
|
|
(*p)->symbol = idents;
|
|
|
|
|
p = &(*p)->next;
|
|
|
|
|
idents = idents->next;
|
|
|
|
|
}
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2011-02-03 12:32:58 +00:00
|
|
|
|
type_t *new;
|
2002-05-08 05:15:19 +00:00
|
|
|
|
|
2011-02-03 12:32:58 +00:00
|
|
|
|
new = new_type ();
|
|
|
|
|
new->type = ev_func;
|
|
|
|
|
new->t.func.type = type;
|
|
|
|
|
new->t.func.num_params = 0;
|
2002-05-08 05:15:19 +00:00
|
|
|
|
|
2002-05-09 20:12:28 +00:00
|
|
|
|
for (p = parms; p; p = p->next) {
|
2011-02-03 12:32:58 +00:00
|
|
|
|
if (new->t.func.num_params > MAX_PARMS) {
|
2002-05-08 05:15:19 +00:00
|
|
|
|
error (0, "too many params");
|
|
|
|
|
return type;
|
|
|
|
|
}
|
2002-05-08 17:33:28 +00:00
|
|
|
|
if (!p->selector && !p->type && !p->name) {
|
2012-10-26 10:02:02 +00:00
|
|
|
|
if (p->next)
|
2012-10-26 10:38:59 +00:00
|
|
|
|
internal_error (0, 0);
|
2011-02-03 12:32:58 +00:00
|
|
|
|
new->t.func.num_params = -(new->t.func.num_params + 1);
|
2002-05-09 20:12:28 +00:00
|
|
|
|
} else if (p->type) {
|
2011-02-03 12:32:58 +00:00
|
|
|
|
new->t.func.param_types[new->t.func.num_params] = p->type;
|
|
|
|
|
new->t.func.num_params++;
|
2002-05-08 17:33:28 +00:00
|
|
|
|
}
|
2002-05-08 05:15:19 +00:00
|
|
|
|
}
|
2011-02-03 12:32:58 +00:00
|
|
|
|
return new;
|
2002-05-08 05:15:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-01 12:18:08 +00:00
|
|
|
|
param_t *
|
|
|
|
|
check_params (param_t *params)
|
|
|
|
|
{
|
|
|
|
|
int num = 1;
|
|
|
|
|
param_t *p = params;
|
|
|
|
|
if (!params)
|
|
|
|
|
return 0;
|
|
|
|
|
while (p) {
|
|
|
|
|
if (p->type == &type_void) {
|
|
|
|
|
if (p->name) {
|
|
|
|
|
error (0, "parameter %d ('%s') has incomplete type", num,
|
|
|
|
|
p->name);
|
|
|
|
|
p->type = type_default;
|
|
|
|
|
} else if (num > 1 || p->next) {
|
|
|
|
|
error (0, "'void' must be the only parameter");
|
|
|
|
|
p->name = "void";
|
|
|
|
|
} else {
|
|
|
|
|
// this is a void function
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
p = p->next;
|
|
|
|
|
}
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
static overloaded_function_t *
|
2004-11-13 11:50:00 +00:00
|
|
|
|
get_function (const char *name, type_t *type, int overload, int create)
|
|
|
|
|
{
|
|
|
|
|
const char *full_name;
|
|
|
|
|
overloaded_function_t *func;
|
|
|
|
|
|
|
|
|
|
if (!overloaded_functions) {
|
|
|
|
|
overloaded_functions = Hash_NewTable (1021, ol_func_get_key, 0, 0);
|
|
|
|
|
function_map = Hash_NewTable (1021, func_map_get_key, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name = save_string (name);
|
|
|
|
|
|
|
|
|
|
full_name = save_string (va ("%s|%s", name, encode_params (type)));
|
|
|
|
|
|
|
|
|
|
func = Hash_Find (overloaded_functions, full_name);
|
|
|
|
|
if (func) {
|
|
|
|
|
if (func->type != type) {
|
|
|
|
|
error (0, "can't overload on return types");
|
|
|
|
|
return func;
|
|
|
|
|
}
|
|
|
|
|
return func;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!create)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
func = Hash_Find (function_map, name);
|
|
|
|
|
if (func) {
|
|
|
|
|
if (!overload && !func->overloaded) {
|
2007-04-06 12:02:34 +00:00
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
|
e->line = func->line;
|
|
|
|
|
e->file = func->file;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
warning (0, "creating overloaded function %s without @overload",
|
|
|
|
|
full_name);
|
2007-05-13 09:02:51 +00:00
|
|
|
|
warning (e, "(previous function is %s)", func->full_name);
|
2004-11-13 11:50:00 +00:00
|
|
|
|
}
|
|
|
|
|
overload = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func = calloc (1, sizeof (overloaded_function_t));
|
|
|
|
|
func->name = name;
|
|
|
|
|
func->full_name = full_name;
|
|
|
|
|
func->type = type;
|
|
|
|
|
func->overloaded = overload;
|
2007-04-06 12:02:34 +00:00
|
|
|
|
func->file = pr.source_file;
|
|
|
|
|
func->line = pr.source_line;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
|
|
|
|
|
Hash_Add (overloaded_functions, func);
|
|
|
|
|
Hash_Add (function_map, func);
|
|
|
|
|
return func;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
symbol_t *
|
|
|
|
|
function_symbol (symbol_t *sym, int overload, int create)
|
|
|
|
|
{
|
|
|
|
|
const char *name = sym->name;
|
|
|
|
|
overloaded_function_t *func;
|
|
|
|
|
symbol_t *s;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
func = get_function (name, sym->type, overload, create);
|
|
|
|
|
|
|
|
|
|
if (func && func->overloaded)
|
|
|
|
|
name = func->full_name;
|
|
|
|
|
s = symtab_lookup (current_symtab, name);
|
|
|
|
|
if ((!s || s->table != current_symtab) && create) {
|
|
|
|
|
s = new_symbol (name);
|
|
|
|
|
s->sy_type = sy_func;
|
|
|
|
|
s->type = sym->type;
|
|
|
|
|
s->params = sym->params;
|
|
|
|
|
s->s.func = 0; // function not yet defined
|
|
|
|
|
symtab_addsymbol (current_symtab, s);
|
|
|
|
|
}
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-13 11:50:00 +00:00
|
|
|
|
// NOTE sorts the list in /reverse/ order
|
|
|
|
|
static int
|
|
|
|
|
func_compare (const void *a, const void *b)
|
|
|
|
|
{
|
|
|
|
|
overloaded_function_t *fa = *(overloaded_function_t **) a;
|
|
|
|
|
overloaded_function_t *fb = *(overloaded_function_t **) b;
|
|
|
|
|
type_t *ta = fa->type;
|
|
|
|
|
type_t *tb = fb->type;
|
2011-01-09 10:41:24 +00:00
|
|
|
|
int na = ta->t.func.num_params;
|
|
|
|
|
int nb = tb->t.func.num_params;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
|
|
if (na < 0)
|
|
|
|
|
na = ~na;
|
|
|
|
|
if (nb < 0)
|
|
|
|
|
nb = ~nb;
|
|
|
|
|
if (na != nb)
|
|
|
|
|
return nb - na;
|
2011-01-09 10:41:24 +00:00
|
|
|
|
if ((ret = (fb->type->t.func.num_params - fa->type->t.func.num_params)))
|
2004-11-13 11:50:00 +00:00
|
|
|
|
return ret;
|
|
|
|
|
for (i = 0; i < na && i < nb; i++)
|
2011-01-09 10:41:24 +00:00
|
|
|
|
if (ta->t.func.param_types[i] != tb->t.func.param_types[i])
|
|
|
|
|
return (long)(tb->t.func.param_types[i] - ta->t.func.param_types[i]);
|
2004-11-13 11:50:00 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
|
find_function (expr_t *fexpr, expr_t *params)
|
|
|
|
|
{
|
|
|
|
|
expr_t *e;
|
|
|
|
|
int i, j, func_count, parm_count, reported = 0;
|
|
|
|
|
overloaded_function_t *f, dummy, *best = 0;
|
|
|
|
|
type_t type;
|
|
|
|
|
void **funcs, *dummy_p = &dummy;
|
|
|
|
|
|
2011-01-17 13:33:33 +00:00
|
|
|
|
if (fexpr->type != ex_symbol)
|
2004-11-13 11:50:00 +00:00
|
|
|
|
return fexpr;
|
|
|
|
|
|
|
|
|
|
memset (&type, 0, sizeof (type));
|
2013-01-07 04:52:48 +00:00
|
|
|
|
type.type = ev_func;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
|
|
|
|
|
for (e = params; e; e = e->next) {
|
|
|
|
|
if (e->type == ex_error)
|
|
|
|
|
return e;
|
2011-01-09 10:41:24 +00:00
|
|
|
|
type.t.func.num_params++;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
}
|
2011-01-09 10:41:24 +00:00
|
|
|
|
if (type.t.func.num_params > MAX_PARMS)
|
2004-11-13 11:50:00 +00:00
|
|
|
|
return fexpr;
|
|
|
|
|
for (i = 0, e = params; e; i++, e = e->next) {
|
2011-01-09 10:41:24 +00:00
|
|
|
|
type.t.func.param_types[type.t.func.num_params - 1 - i] = get_type (e);
|
2004-11-13 11:50:00 +00:00
|
|
|
|
if (e->type == ex_error)
|
|
|
|
|
return e;
|
|
|
|
|
}
|
2011-01-17 13:33:33 +00:00
|
|
|
|
funcs = Hash_FindList (function_map, fexpr->e.symbol->name);
|
2004-11-13 11:50:00 +00:00
|
|
|
|
if (!funcs)
|
|
|
|
|
return fexpr;
|
|
|
|
|
for (func_count = 0; funcs[func_count]; func_count++)
|
|
|
|
|
;
|
|
|
|
|
if (func_count < 2) {
|
|
|
|
|
free (funcs);
|
|
|
|
|
return fexpr;
|
|
|
|
|
}
|
2011-01-09 10:41:24 +00:00
|
|
|
|
type.t.func.type = ((overloaded_function_t *) funcs[0])->type->t.func.type;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
dummy.type = find_type (&type);
|
|
|
|
|
|
|
|
|
|
qsort (funcs, func_count, sizeof (void *), func_compare);
|
2011-01-17 13:33:33 +00:00
|
|
|
|
dummy.full_name = save_string (va ("%s|%s", fexpr->e.symbol->name,
|
2004-11-13 11:50:00 +00:00
|
|
|
|
encode_params (&type)));
|
|
|
|
|
dummy_p = bsearch (&dummy_p, funcs, func_count, sizeof (void *),
|
|
|
|
|
func_compare);
|
|
|
|
|
if (dummy_p) {
|
|
|
|
|
f = (overloaded_function_t *) *(void **) dummy_p;
|
2013-01-07 04:51:01 +00:00
|
|
|
|
if (f->overloaded) {
|
|
|
|
|
fexpr->e.symbol = symtab_lookup (current_symtab, f->full_name);
|
|
|
|
|
if (!fexpr->e.symbol)
|
|
|
|
|
internal_error (fexpr, "overloaded function %s not found",
|
|
|
|
|
best->full_name);
|
|
|
|
|
}
|
2004-11-13 11:50:00 +00:00
|
|
|
|
free (funcs);
|
|
|
|
|
return fexpr;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < func_count; i++) {
|
|
|
|
|
f = (overloaded_function_t *) funcs[i];
|
2011-01-09 10:41:24 +00:00
|
|
|
|
parm_count = f->type->t.func.num_params;
|
|
|
|
|
if ((parm_count >= 0 && parm_count != type.t.func.num_params)
|
|
|
|
|
|| (parm_count < 0 && ~parm_count > type.t.func.num_params)) {
|
2004-11-13 11:50:00 +00:00
|
|
|
|
funcs[i] = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (parm_count < 0)
|
|
|
|
|
parm_count = ~parm_count;
|
|
|
|
|
for (j = 0; j < parm_count; j++) {
|
2011-01-09 10:41:24 +00:00
|
|
|
|
if (!type_assignable (f->type->t.func.param_types[j],
|
|
|
|
|
type.t.func.param_types[j])) {
|
2004-11-13 11:50:00 +00:00
|
|
|
|
funcs[i] = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (j < parm_count)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < func_count; i++) {
|
|
|
|
|
f = (overloaded_function_t *) funcs[i];
|
|
|
|
|
if (f) {
|
|
|
|
|
if (!best) {
|
|
|
|
|
best = f;
|
|
|
|
|
} else {
|
|
|
|
|
if (!reported) {
|
|
|
|
|
reported = 1;
|
|
|
|
|
error (fexpr, "unable to disambiguate %s",
|
|
|
|
|
dummy.full_name);
|
|
|
|
|
error (fexpr, "possible match: %s", best->full_name);
|
|
|
|
|
}
|
|
|
|
|
error (fexpr, "possible match: %s", f->full_name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (reported)
|
|
|
|
|
return fexpr;
|
|
|
|
|
if (best) {
|
2011-03-03 05:44:48 +00:00
|
|
|
|
if (best->overloaded) {
|
|
|
|
|
fexpr->e.symbol = symtab_lookup (current_symtab,
|
|
|
|
|
best->full_name);
|
|
|
|
|
if (!fexpr->e.symbol)
|
|
|
|
|
internal_error (fexpr, "overloaded function %s not found",
|
|
|
|
|
best->full_name);
|
|
|
|
|
}
|
2004-11-13 11:50:00 +00:00
|
|
|
|
free (funcs);
|
|
|
|
|
return fexpr;
|
|
|
|
|
}
|
|
|
|
|
error (fexpr, "unable to find function matching %s", dummy.full_name);
|
|
|
|
|
free (funcs);
|
|
|
|
|
return fexpr;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-17 05:45:35 +00:00
|
|
|
|
static void
|
2011-01-18 03:37:12 +00:00
|
|
|
|
check_function (symbol_t *fsym)
|
2010-11-17 05:45:35 +00:00
|
|
|
|
{
|
2011-01-18 03:37:12 +00:00
|
|
|
|
param_t *params = fsym->params;
|
2010-11-17 05:45:35 +00:00
|
|
|
|
param_t *p;
|
|
|
|
|
int i;
|
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
if (!type_size (fsym->type->t.func.type)) {
|
2010-11-17 05:45:35 +00:00
|
|
|
|
error (0, "return type is an incomplete type");
|
2011-01-24 12:13:37 +00:00
|
|
|
|
fsym->type->t.func.type = &type_void;//FIXME better type?
|
2010-11-17 05:45:35 +00:00
|
|
|
|
}
|
2011-01-18 03:37:12 +00:00
|
|
|
|
if (type_size (fsym->type->t.func.type) > type_size (&type_param)) {
|
2010-11-17 05:45:35 +00:00
|
|
|
|
error (0, "return value too large to be passed by value");
|
2011-01-24 12:13:37 +00:00
|
|
|
|
fsym->type->t.func.type = &type_void;//FIXME better type?
|
2010-11-17 05:45:35 +00:00
|
|
|
|
}
|
|
|
|
|
for (p = params, i = 0; p; p = p->next, i++) {
|
|
|
|
|
if (!p->selector && !p->type && !p->name)
|
|
|
|
|
continue; // ellipsis marker
|
|
|
|
|
if (!p->type)
|
|
|
|
|
continue; // non-param selector
|
|
|
|
|
if (!type_size (p->type))
|
|
|
|
|
error (0, "parameter %d (‘%s’) has incomplete type",
|
|
|
|
|
i + 1, p->name);
|
|
|
|
|
if (type_size (p->type) > type_size (&type_param))
|
|
|
|
|
error (0, "param %d (‘%s’) is too large to be passed by value",
|
|
|
|
|
i + 1, p->name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
static void
|
|
|
|
|
build_scope (symbol_t *fsym, symtab_t *parent)
|
2002-05-08 05:15:19 +00:00
|
|
|
|
{
|
2002-05-22 20:43:29 +00:00
|
|
|
|
int i;
|
2002-05-08 17:33:28 +00:00
|
|
|
|
param_t *p;
|
2011-01-18 03:37:12 +00:00
|
|
|
|
symbol_t *args = 0;
|
|
|
|
|
symbol_t *param;
|
|
|
|
|
symtab_t *symtab;
|
2011-02-06 07:56:19 +00:00
|
|
|
|
symtab_t *cs = current_symtab;
|
2002-05-08 05:15:19 +00:00
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
check_function (fsym);
|
2010-11-17 05:45:35 +00:00
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
symtab = new_symtab (parent, stab_local);
|
|
|
|
|
fsym->s.func->symtab = symtab;
|
2012-12-04 05:08:51 +00:00
|
|
|
|
symtab->space = defspace_new (ds_virtual);
|
2011-02-06 07:56:19 +00:00
|
|
|
|
current_symtab = symtab;
|
2002-05-08 05:15:19 +00:00
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
if (fsym->type->t.func.num_params < 0) {
|
|
|
|
|
args = new_symbol_type (".args", &type_va_list);
|
2012-12-02 01:11:30 +00:00
|
|
|
|
initialize_def (args, args->type, 0, symtab->space, sc_param);
|
2002-05-22 20:43:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
for (p = fsym->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
|
2012-12-18 04:52:02 +00:00
|
|
|
|
if (!p->name) {
|
|
|
|
|
error (0, "parameter name omitted");
|
|
|
|
|
p->name = save_string ("");
|
|
|
|
|
}
|
2011-01-18 03:37:12 +00:00
|
|
|
|
param = new_symbol_type (p->name, p->type);
|
2012-12-02 01:11:30 +00:00
|
|
|
|
initialize_def (param, param->type, 0, symtab->space, sc_param);
|
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) {
|
2011-01-18 03:37:12 +00:00
|
|
|
|
param = new_symbol_type (va (".par%d", i), &type_param);
|
2012-12-02 01:11:30 +00:00
|
|
|
|
initialize_def (param, &type_param, 0, symtab->space, sc_param);
|
2002-05-22 20:43:29 +00:00
|
|
|
|
i++;
|
|
|
|
|
}
|
2002-05-08 05:15:19 +00:00
|
|
|
|
}
|
2011-02-06 07:56:19 +00:00
|
|
|
|
current_symtab = cs;
|
2002-05-08 05:15:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function_t *
|
2011-01-18 03:37:12 +00:00
|
|
|
|
new_function (const char *name, const char *nice_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);
|
2011-01-18 03:37:12 +00:00
|
|
|
|
f->s_name = ReuseString (name);
|
2008-08-01 13:54:24 +00:00
|
|
|
|
f->s_file = pr.source_file;
|
2010-12-31 09:26:42 +00:00
|
|
|
|
if (!(f->name = nice_name))
|
2011-01-18 03:37:12 +00:00
|
|
|
|
f->name = name;
|
2008-08-01 13:54:24 +00:00
|
|
|
|
return f;
|
|
|
|
|
}
|
2002-06-27 22:48:28 +00:00
|
|
|
|
|
2011-01-26 05:48:22 +00:00
|
|
|
|
void
|
2011-02-15 03:38:29 +00:00
|
|
|
|
make_function (symbol_t *sym, const char *nice_name, defspace_t *space,
|
|
|
|
|
storage_class_t storage)
|
2011-01-26 05:48:22 +00:00
|
|
|
|
{
|
2011-03-09 01:52:29 +00:00
|
|
|
|
reloc_t *relocs = 0;
|
2011-01-26 05:48:22 +00:00
|
|
|
|
if (sym->sy_type != sy_func)
|
|
|
|
|
internal_error (0, "%s is not a function", sym->name);
|
2012-12-02 01:11:30 +00:00
|
|
|
|
if (storage == sc_extern && sym->s.func)
|
2011-01-26 05:48:22 +00:00
|
|
|
|
return;
|
|
|
|
|
if (!sym->s.func) {
|
|
|
|
|
sym->s.func = new_function (sym->name, nice_name);
|
|
|
|
|
sym->s.func->sym = sym;
|
|
|
|
|
}
|
|
|
|
|
if (sym->s.func->def && sym->s.func->def->external
|
2012-12-02 01:11:30 +00:00
|
|
|
|
&& storage != sc_extern) {
|
2011-03-09 01:52:29 +00:00
|
|
|
|
//FIXME this really is not the right way
|
|
|
|
|
relocs = sym->s.func->def->relocs;
|
2011-01-26 05:48:22 +00:00
|
|
|
|
free_def (sym->s.func->def);
|
|
|
|
|
sym->s.func->def = 0;
|
|
|
|
|
}
|
2011-03-09 01:52:29 +00:00
|
|
|
|
if (!sym->s.func->def) {
|
2011-02-15 03:38:29 +00:00
|
|
|
|
sym->s.func->def = new_def (sym->name, sym->type, space, storage);
|
2012-11-12 07:14:09 +00:00
|
|
|
|
reloc_attach_relocs (relocs, &sym->s.func->def->relocs);
|
2011-03-09 01:52:29 +00:00
|
|
|
|
}
|
2011-01-26 05:48:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-08-01 13:54:24 +00:00
|
|
|
|
void
|
|
|
|
|
add_function (function_t *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-05-08 05:15:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-01-06 11:22:30 +00:00
|
|
|
|
function_t *
|
2011-02-15 03:38:29 +00:00
|
|
|
|
begin_function (symbol_t *sym, const char *nicename, symtab_t *parent,
|
|
|
|
|
int far)
|
2011-01-06 11:22:30 +00:00
|
|
|
|
{
|
2011-02-15 03:38:29 +00:00
|
|
|
|
defspace_t *space;
|
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
if (sym->sy_type != sy_func) {
|
|
|
|
|
error (0, "%s is not a function", sym->name);
|
2011-02-01 12:18:39 +00:00
|
|
|
|
sym = new_symbol_type (sym->name, &type_function);
|
|
|
|
|
sym = function_symbol (sym, 1, 1);
|
2011-01-06 11:22:30 +00:00
|
|
|
|
}
|
2011-01-26 05:48:22 +00:00
|
|
|
|
if (sym->s.func && sym->s.func->def && sym->s.func->def->initialized) {
|
2011-01-18 03:37:12 +00:00
|
|
|
|
error (0, "%s redefined", sym->name);
|
2011-02-14 12:25:55 +00:00
|
|
|
|
sym = new_symbol_type (sym->name, sym->type);
|
|
|
|
|
sym = function_symbol (sym, 1, 1);
|
2011-01-18 03:37:12 +00:00
|
|
|
|
}
|
2011-02-15 03:38:29 +00:00
|
|
|
|
space = sym->table->space;
|
|
|
|
|
if (far)
|
|
|
|
|
space = pr.far_data;
|
|
|
|
|
make_function (sym, nicename, space, current_storage);
|
2011-01-26 05:48:22 +00:00
|
|
|
|
if (!sym->s.func->def->external) {
|
|
|
|
|
sym->s.func->def->initialized = 1;
|
|
|
|
|
sym->s.func->def->constant = 1;
|
|
|
|
|
sym->s.func->def->nosave = 1;
|
2011-01-18 03:37:12 +00:00
|
|
|
|
add_function (sym->s.func);
|
2011-02-12 13:34:38 +00:00
|
|
|
|
reloc_def_func (sym->s.func, sym->s.func->def);
|
2011-01-26 05:48:22 +00:00
|
|
|
|
}
|
2011-01-18 03:37:12 +00:00
|
|
|
|
sym->s.func->code = pr.code->size;
|
2011-01-26 05:48:22 +00:00
|
|
|
|
|
2011-03-22 04:45:44 +00:00
|
|
|
|
sym->s.func->s_file = pr.source_file;
|
2011-03-07 04:33:02 +00:00
|
|
|
|
if (options.code.debug) {
|
2011-01-06 11:22:30 +00:00
|
|
|
|
pr_lineno_t *lineno = new_lineno ();
|
2011-03-07 04:33:02 +00:00
|
|
|
|
sym->s.func->line_info = lineno - pr.linenos;
|
2011-01-06 11:22:30 +00:00
|
|
|
|
}
|
2011-01-26 05:48:22 +00:00
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
build_scope (sym, parent);
|
|
|
|
|
return sym->s.func;
|
2011-01-06 11:22:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-02-11 00:36:34 +00:00
|
|
|
|
function_t *
|
2011-01-18 23:38:09 +00:00
|
|
|
|
build_code_function (symbol_t *fsym, expr_t *state_expr, expr_t *statements)
|
2004-02-11 00:36:34 +00:00
|
|
|
|
{
|
2011-02-01 12:18:39 +00:00
|
|
|
|
if (fsym->sy_type != sy_func) // probably in error recovery
|
|
|
|
|
return 0;
|
2011-01-18 23:38:09 +00:00
|
|
|
|
build_function (fsym->s.func);
|
2004-02-11 00:36:34 +00:00
|
|
|
|
if (state_expr) {
|
|
|
|
|
state_expr->next = statements;
|
2011-01-18 23:38:09 +00:00
|
|
|
|
statements = state_expr;
|
2004-02-11 00:36:34 +00:00
|
|
|
|
}
|
2011-01-18 23:38:09 +00:00
|
|
|
|
emit_function (fsym->s.func, statements);
|
|
|
|
|
finish_function (fsym->s.func);
|
|
|
|
|
return fsym->s.func;
|
2004-02-11 00:36:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-21 20:46:56 +00:00
|
|
|
|
function_t *
|
2011-02-15 03:38:29 +00:00
|
|
|
|
build_builtin_function (symbol_t *sym, expr_t *bi_val, int far)
|
2002-05-17 18:35:54 +00:00
|
|
|
|
{
|
2011-01-18 03:37:12 +00:00
|
|
|
|
int bi;
|
2011-02-15 03:38:29 +00:00
|
|
|
|
defspace_t *space;
|
2002-05-17 18:35:54 +00:00
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
if (sym->sy_type != sy_func) {
|
|
|
|
|
error (bi_val, "%s is not a function", sym->name);
|
2002-06-21 20:46:56 +00:00
|
|
|
|
return 0;
|
2002-05-17 18:35:54 +00:00
|
|
|
|
}
|
2011-01-26 05:48:22 +00:00
|
|
|
|
if (sym->s.func && sym->s.func->def && sym->s.func->def->initialized) {
|
2011-01-18 03:37:12 +00:00
|
|
|
|
error (bi_val, "%s redefined", sym->name);
|
2004-02-11 01:53:17 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2011-01-18 23:41:24 +00:00
|
|
|
|
if (!is_integer_val (bi_val) && !is_float_val (bi_val)) {
|
2002-05-17 18:35:54 +00:00
|
|
|
|
error (bi_val, "invalid constant for = #");
|
2002-06-21 20:46:56 +00:00
|
|
|
|
return 0;
|
2002-05-17 18:35:54 +00:00
|
|
|
|
}
|
2011-02-15 03:38:29 +00:00
|
|
|
|
space = sym->table->space;
|
|
|
|
|
if (far)
|
|
|
|
|
space = pr.far_data;
|
|
|
|
|
make_function (sym, 0, space, current_storage);
|
2011-01-26 05:48:22 +00:00
|
|
|
|
if (sym->s.func->def->external)
|
|
|
|
|
return 0;
|
2002-05-17 18:35:54 +00:00
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
add_function (sym->s.func);
|
2002-05-17 18:35:54 +00:00
|
|
|
|
|
2011-01-18 23:41:24 +00:00
|
|
|
|
if (is_integer_val (bi_val))
|
|
|
|
|
bi = expr_integer (bi_val);
|
|
|
|
|
else
|
|
|
|
|
bi = expr_float (bi_val);
|
2011-01-18 03:37:12 +00:00
|
|
|
|
sym->s.func->builtin = bi;
|
2011-02-12 13:34:38 +00:00
|
|
|
|
reloc_def_func (sym->s.func, sym->s.func->def);
|
2011-01-18 03:37:12 +00:00
|
|
|
|
build_function (sym->s.func);
|
|
|
|
|
finish_function (sym->s.func);
|
2011-01-17 13:33:33 +00:00
|
|
|
|
|
|
|
|
|
// for debug info
|
2011-03-10 11:16:35 +00:00
|
|
|
|
build_scope (sym, current_symtab);
|
2012-12-15 04:13:40 +00:00
|
|
|
|
sym->s.func->symtab->space->size = 0;
|
2011-01-18 03:37:12 +00:00
|
|
|
|
return sym->s.func;
|
2002-05-17 18:35:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-05-08 05:15:19 +00:00
|
|
|
|
void
|
|
|
|
|
build_function (function_t *f)
|
|
|
|
|
{
|
2011-01-18 03:37:12 +00:00
|
|
|
|
// FIXME
|
|
|
|
|
// f->def->constant = 1;
|
|
|
|
|
// f->def->nosave = 1;
|
|
|
|
|
// f->def->initialized = 1;
|
|
|
|
|
// G_FUNCTION (f->def->ofs) = f->function_num;
|
2002-05-08 05:15:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
finish_function (function_t *f)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
emit_function (function_t *f, expr_t *e)
|
|
|
|
|
{
|
2012-12-22 10:22:08 +00:00
|
|
|
|
if (pr.error_count)
|
|
|
|
|
return;
|
2011-01-25 06:46:48 +00:00
|
|
|
|
f->code = pr.code->size;
|
2011-03-07 04:33:02 +00:00
|
|
|
|
lineno_base = f->def->line;
|
2012-10-30 08:41:40 +00:00
|
|
|
|
f->sblock = make_statements (e);
|
2012-12-01 02:13:45 +00:00
|
|
|
|
if (options.code.optimize) {
|
2012-12-09 11:50:53 +00:00
|
|
|
|
flow_data_flow (f);
|
2012-12-01 02:13:45 +00:00
|
|
|
|
} else {
|
|
|
|
|
statements_count_temps (f->sblock);
|
|
|
|
|
}
|
2012-10-30 08:41:40 +00:00
|
|
|
|
emit_statements (f->sblock);
|
2002-05-08 05:15:19 +00:00
|
|
|
|
}
|
2002-06-27 22:48:28 +00:00
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
function_parms (function_t *f, byte *parm_size)
|
|
|
|
|
{
|
|
|
|
|
int count, i;
|
2012-11-07 00:38:08 +00:00
|
|
|
|
ty_func_t *func = &f->sym->type->t.func;
|
2002-06-27 22:48:28 +00:00
|
|
|
|
|
2012-11-07 00:38:08 +00:00
|
|
|
|
if (func->num_params >= 0)
|
|
|
|
|
count = func->num_params;
|
2002-06-27 22:48:28 +00:00
|
|
|
|
else
|
2012-11-07 00:38:08 +00:00
|
|
|
|
count = -func->num_params - 1;
|
2002-06-27 22:48:28 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
2012-11-07 00:38:08 +00:00
|
|
|
|
parm_size[i] = type_size (func->param_types[i]);
|
|
|
|
|
return func->num_params;
|
2002-06-27 22:48:28 +00:00
|
|
|
|
}
|
2012-11-01 11:52:22 +00:00
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
clear_functions (void)
|
|
|
|
|
{
|
|
|
|
|
if (overloaded_functions)
|
|
|
|
|
Hash_FlushTable (overloaded_functions);
|
|
|
|
|
if (function_map)
|
|
|
|
|
Hash_FlushTable (function_map);
|
|
|
|
|
}
|