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
|
|
|
|
|
2020-06-21 14:15:17 +00:00
|
|
|
|
#include "tools/qfcc/include/qfcc.h"
|
|
|
|
|
|
|
|
|
|
#include "tools/qfcc/include/class.h"
|
|
|
|
|
#include "tools/qfcc/include/codespace.h"
|
|
|
|
|
#include "tools/qfcc/include/debug.h"
|
|
|
|
|
#include "tools/qfcc/include/def.h"
|
|
|
|
|
#include "tools/qfcc/include/defspace.h"
|
|
|
|
|
#include "tools/qfcc/include/diagnostic.h"
|
2024-09-05 16:00:25 +00:00
|
|
|
|
#include "tools/qfcc/include/evaluate_type.h"
|
2020-06-21 14:15:17 +00:00
|
|
|
|
#include "tools/qfcc/include/emit.h"
|
|
|
|
|
#include "tools/qfcc/include/expr.h"
|
|
|
|
|
#include "tools/qfcc/include/flow.h"
|
|
|
|
|
#include "tools/qfcc/include/function.h"
|
|
|
|
|
#include "tools/qfcc/include/opcodes.h"
|
|
|
|
|
#include "tools/qfcc/include/options.h"
|
|
|
|
|
#include "tools/qfcc/include/reloc.h"
|
|
|
|
|
#include "tools/qfcc/include/shared.h"
|
|
|
|
|
#include "tools/qfcc/include/statements.h"
|
|
|
|
|
#include "tools/qfcc/include/strpool.h"
|
|
|
|
|
#include "tools/qfcc/include/symtab.h"
|
|
|
|
|
#include "tools/qfcc/include/type.h"
|
|
|
|
|
#include "tools/qfcc/include/value.h"
|
2002-05-08 05:15:19 +00:00
|
|
|
|
|
2023-03-05 07:26:37 +00:00
|
|
|
|
ALLOC_STATE (param_t, params);
|
|
|
|
|
ALLOC_STATE (function_t, functions);
|
2024-08-19 09:29:44 +00:00
|
|
|
|
ALLOC_STATE (metafunc_t, metafuncs);
|
2024-07-07 12:36:40 +00:00
|
|
|
|
ALLOC_STATE (genfunc_t, genfuncs);
|
2024-08-10 05:36:52 +00:00
|
|
|
|
static hashtab_t *generic_functions;
|
2024-08-19 09:29:44 +00:00
|
|
|
|
static hashtab_t *metafuncs;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
static hashtab_t *function_map;
|
|
|
|
|
|
2022-01-21 11:34:43 +00:00
|
|
|
|
// standardized base register to use for all locals (arguments, local defs,
|
|
|
|
|
// params)
|
|
|
|
|
#define LOCALS_REG 1
|
[qfcc] Update sizes and alignments for dvec4 and friends
dvec4, lvec4 and ulvec4 need to be aligned to 8 words (32 bytes) in
order to avoid hardware exceptions. Rather than dealing with possibly
mixed alignment when a function has 8-word aligned locals but only
4-word aligned parameters, simply keep the stack frame 8-word aligned at
all times.
As for sizes, the temp def recycler was written before the Ruamoko ISA
was even a pipe dream and thus never expected temp def sizes over 4. At
least now any future adjustments can be done in one place.
My quick and dirty test program works :)
dvec4 xy = {1d, 2d, 0d, 0.5};
void printf(string fmt, ...) = #0;
int main()
{
dvec4 u = {3, 4, 3.14};
dvec4 v = {3, 4, 0, 1};
dvec4 w = v * xy + u;
printf ("[%g, %g, %g, %g]\n", w[0], w[1], w[2], w[3]);
return 0;
}
2022-02-03 23:46:58 +00:00
|
|
|
|
// keep the stack aligned to 8 words (32 bytes) so lvec etc can be used without
|
|
|
|
|
// having to do shenanigans with mixed-alignment stack frames
|
|
|
|
|
#define STACK_ALIGN 8
|
2022-01-21 11:34:43 +00:00
|
|
|
|
|
2024-08-10 05:36:52 +00:00
|
|
|
|
static const char *
|
|
|
|
|
gen_func_get_key (const void *_f, void *unused)
|
|
|
|
|
{
|
|
|
|
|
auto f = (genfunc_t *) _f;
|
|
|
|
|
return f->name;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-13 11:50:00 +00:00
|
|
|
|
static const char *
|
2024-08-20 03:43:17 +00:00
|
|
|
|
metafunc_get_full_name (const void *_f, void *unused)
|
2004-11-13 11:50:00 +00:00
|
|
|
|
{
|
2024-08-19 09:29:44 +00:00
|
|
|
|
metafunc_t *f = (metafunc_t *) _f;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
return f->full_name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const char *
|
2024-08-20 03:43:17 +00:00
|
|
|
|
metafunc_get_name (const void *_f, void *unused)
|
2004-11-13 11:50:00 +00:00
|
|
|
|
{
|
2024-08-19 09:29:44 +00:00
|
|
|
|
metafunc_t *f = (metafunc_t *) _f;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
return f->name;
|
|
|
|
|
}
|
2002-06-27 22:48:28 +00:00
|
|
|
|
|
2024-08-10 05:36:52 +00:00
|
|
|
|
static void
|
|
|
|
|
check_generic_param (genparam_t *param, genfunc_t *genfunc)
|
|
|
|
|
{
|
2024-09-04 00:58:14 +00:00
|
|
|
|
if (param->fixed_type) {
|
|
|
|
|
if (param->gentype != -1) {
|
|
|
|
|
internal_error (0, "invalid type index %d on %s for %s",
|
|
|
|
|
param->gentype, param->name, genfunc->name);
|
|
|
|
|
}
|
2024-09-06 17:36:54 +00:00
|
|
|
|
if (param->compute) {
|
|
|
|
|
internal_error (0, "fixed and computed types on %s for %s",
|
|
|
|
|
param->name, genfunc->name);
|
|
|
|
|
}
|
|
|
|
|
} else if (param->compute) {
|
|
|
|
|
if (param->gentype != -1) {
|
|
|
|
|
internal_error (0, "invalid type index %d on %s for %s",
|
|
|
|
|
param->gentype, param->name, genfunc->name);
|
|
|
|
|
}
|
2024-09-04 00:58:14 +00:00
|
|
|
|
} else if (param->gentype < 0 || param->gentype >= genfunc->num_types) {
|
2024-08-10 11:04:58 +00:00
|
|
|
|
internal_error (0, "invalid type index %d on %s for %s",
|
|
|
|
|
param->gentype, param->name, genfunc->name);
|
2024-08-10 05:36:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool __attribute__((pure))
|
|
|
|
|
cmp_genparams (genfunc_t *g1, genparam_t *p1, genfunc_t *g2, genparam_t *p2)
|
|
|
|
|
{
|
|
|
|
|
if (p1->fixed_type || p2->fixed_type) {
|
|
|
|
|
return p1->fixed_type == p2->fixed_type;
|
|
|
|
|
}
|
|
|
|
|
// fixed_type for both p1 and p2 is null
|
|
|
|
|
auto t1 = g1->types[p1->gentype];
|
|
|
|
|
auto t2 = g2->types[p2->gentype];
|
|
|
|
|
auto vt1 = t1.valid_types;
|
|
|
|
|
auto vt2 = t2.valid_types;
|
|
|
|
|
for (; *vt1 && *vt2 && *vt1 == *vt2; vt1++, vt2++) continue;
|
|
|
|
|
return *vt1 == *vt2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
add_generic_function (genfunc_t *genfunc)
|
|
|
|
|
{
|
2024-08-27 05:41:38 +00:00
|
|
|
|
auto name = genfunc->name;
|
|
|
|
|
|
2024-08-10 05:36:52 +00:00
|
|
|
|
for (int i = 0; i < genfunc->num_types; i++) {
|
|
|
|
|
auto gentype = &genfunc->types[i];
|
2024-09-06 17:36:54 +00:00
|
|
|
|
if (!gentype->valid_types) {
|
|
|
|
|
internal_error (0, "no valid_types set in generic type");
|
2024-08-10 05:36:52 +00:00
|
|
|
|
}
|
|
|
|
|
for (auto type = gentype->valid_types; type && *type; type++) {
|
|
|
|
|
if (is_void (*type)) {
|
|
|
|
|
internal_error (0, "void in list of valid types");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int gen_params = 0;
|
|
|
|
|
for (int i = 0; i < genfunc->num_params; i++) {
|
|
|
|
|
auto param = &genfunc->params[i];
|
|
|
|
|
if (!param->fixed_type) {
|
|
|
|
|
gen_params++;
|
|
|
|
|
check_generic_param (param, genfunc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!gen_params) {
|
2024-08-27 05:41:38 +00:00
|
|
|
|
internal_error (0, "%s has no generic parameters", name);
|
2024-08-10 05:36:52 +00:00
|
|
|
|
}
|
|
|
|
|
if (!genfunc->ret_type) {
|
2024-08-27 05:41:38 +00:00
|
|
|
|
internal_error (0, "%s has no return type", name);
|
2024-08-10 05:36:52 +00:00
|
|
|
|
}
|
|
|
|
|
check_generic_param (genfunc->ret_type, genfunc);
|
|
|
|
|
|
|
|
|
|
bool is_new = true;
|
2024-08-27 05:41:38 +00:00
|
|
|
|
auto old_list = (genfunc_t **) Hash_FindList (generic_functions, name);
|
|
|
|
|
for (auto o = old_list; is_new && o && *o; o++) {
|
|
|
|
|
auto old = *o;
|
|
|
|
|
if (old->num_params == genfunc->num_params) {
|
|
|
|
|
is_new = false;
|
|
|
|
|
for (int i = 0; i < genfunc->num_params; i++) {
|
|
|
|
|
if (!cmp_genparams (genfunc, &genfunc->params[i],
|
|
|
|
|
old, &old->params[i])) {
|
|
|
|
|
is_new = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!is_new && !cmp_genparams (genfunc, genfunc->ret_type,
|
|
|
|
|
old, old->ret_type)) {
|
|
|
|
|
error (0, "can't overload on return types");
|
|
|
|
|
return;
|
2024-08-10 05:36:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_new) {
|
|
|
|
|
Hash_Add (generic_functions, genfunc);
|
|
|
|
|
} else {
|
|
|
|
|
for (int i = 0; i < genfunc->num_types; i++) {
|
|
|
|
|
auto gentype = &genfunc->types[i];
|
|
|
|
|
free (gentype->valid_types);
|
|
|
|
|
}
|
|
|
|
|
free (genfunc->types);
|
|
|
|
|
FREE (genfuncs, genfunc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-07 12:36:40 +00:00
|
|
|
|
static const type_t **
|
|
|
|
|
valid_type_list (const expr_t *expr)
|
|
|
|
|
{
|
|
|
|
|
if (expr->type != ex_list) {
|
2024-08-10 11:59:02 +00:00
|
|
|
|
return expand_type (expr);
|
2024-07-07 12:36:40 +00:00
|
|
|
|
}
|
|
|
|
|
int count = list_count (&expr->list);
|
|
|
|
|
const expr_t *type_refs[count];
|
|
|
|
|
list_scatter (&expr->list, type_refs);
|
|
|
|
|
const type_t **types = malloc (sizeof (type_t *[count + 1]));
|
|
|
|
|
types[count] = nullptr;
|
|
|
|
|
bool err = false;
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
if (!(types[i] = resolve_type (type_refs[i]))) {
|
|
|
|
|
error (type_refs[i], "not a constant type ref");
|
|
|
|
|
err = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (err) {
|
|
|
|
|
free (types);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return types;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gentype_t
|
|
|
|
|
make_gentype (const expr_t *expr)
|
|
|
|
|
{
|
|
|
|
|
if (expr->type != ex_symbol || expr->symbol->sy_type != sy_type_param) {
|
|
|
|
|
internal_error (expr, "expected generic type name");
|
|
|
|
|
}
|
|
|
|
|
auto sym = expr->symbol;
|
|
|
|
|
gentype_t gentype = {
|
|
|
|
|
.name = save_string (sym->name),
|
2024-08-16 07:48:11 +00:00
|
|
|
|
.valid_types = valid_type_list (sym->expr),
|
2024-07-07 12:36:40 +00:00
|
|
|
|
};
|
2024-09-06 17:36:54 +00:00
|
|
|
|
if (!gentype.valid_types) {
|
2024-07-07 12:36:40 +00:00
|
|
|
|
internal_error (expr, "empty generic type");
|
|
|
|
|
}
|
|
|
|
|
return gentype;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
find_gentype (const expr_t *expr, genfunc_t *genfunc)
|
|
|
|
|
{
|
2024-09-01 12:25:42 +00:00
|
|
|
|
if (!expr || expr->type != ex_symbol) {
|
2024-07-07 12:36:40 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
const char *name = expr->symbol->name;
|
|
|
|
|
for (int i = 0; i < genfunc->num_types; i++) {
|
|
|
|
|
auto t = &genfunc->types[i];
|
|
|
|
|
if (strcmp (name, t->name) == 0) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static genparam_t
|
|
|
|
|
make_genparam (param_t *param, genfunc_t *genfunc)
|
|
|
|
|
{
|
2024-09-06 17:36:54 +00:00
|
|
|
|
int gentype = find_gentype (param->type_expr, genfunc);
|
|
|
|
|
typeeval_t *compute = nullptr;
|
|
|
|
|
if (gentype < 0 && param->type_expr) {
|
|
|
|
|
compute = build_type_function (param->type_expr,
|
|
|
|
|
genfunc->num_types, genfunc->types);
|
|
|
|
|
}
|
2024-07-07 12:36:40 +00:00
|
|
|
|
genparam_t genparam = {
|
|
|
|
|
.name = save_string (param->name),
|
|
|
|
|
.fixed_type = param->type,
|
2024-09-06 17:36:54 +00:00
|
|
|
|
.compute = compute,
|
|
|
|
|
.gentype = gentype,
|
|
|
|
|
.qual = param->qual,
|
2024-07-07 12:36:40 +00:00
|
|
|
|
};
|
|
|
|
|
return genparam;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static genfunc_t *
|
|
|
|
|
parse_generic_function (const char *name, specifier_t spec)
|
|
|
|
|
{
|
|
|
|
|
if (!spec.is_generic) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
// fake parameter for the return type
|
|
|
|
|
param_t ret_param = {
|
|
|
|
|
.next = spec.sym->params,
|
2024-09-04 00:58:14 +00:00
|
|
|
|
.type = spec.type,
|
2024-07-07 12:36:40 +00:00
|
|
|
|
.type_expr = spec.type_expr,
|
|
|
|
|
};
|
|
|
|
|
int num_params = 0;
|
|
|
|
|
int num_gentype = 0;
|
|
|
|
|
for (auto p = &ret_param; p; p = p->next) {
|
|
|
|
|
num_params++;
|
|
|
|
|
}
|
2024-08-10 11:04:58 +00:00
|
|
|
|
auto generic_tab = spec.symtab;
|
2024-07-07 12:36:40 +00:00
|
|
|
|
for (auto s = generic_tab->symbols; s; s = s->next) {
|
|
|
|
|
bool found = false;
|
|
|
|
|
for (auto q = &ret_param; q; q = q->next) {
|
2024-09-06 17:36:54 +00:00
|
|
|
|
// skip complex expressions because they will be either fixed
|
|
|
|
|
// or rely on earlier parameters
|
2024-07-07 12:36:40 +00:00
|
|
|
|
if (!q->type_expr || q->type_expr->type != ex_symbol) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (strcmp (q->type_expr->symbol->name, s->name) == 0) {
|
|
|
|
|
num_gentype++;
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-10 11:04:58 +00:00
|
|
|
|
if (!spec.is_generic_block && !found) {
|
2024-07-07 12:36:40 +00:00
|
|
|
|
warning (0, "generic parameter %s not used", s->name);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-27 05:37:18 +00:00
|
|
|
|
if (!num_gentype) {
|
|
|
|
|
if (!spec.is_generic_block) {
|
|
|
|
|
warning (0, "no generic parameters for %s", name);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2024-07-07 12:36:40 +00:00
|
|
|
|
|
|
|
|
|
genfunc_t *genfunc;
|
|
|
|
|
ALLOC (4096, genfunc_t, genfuncs, genfunc);
|
|
|
|
|
*genfunc = (genfunc_t) {
|
|
|
|
|
.name = save_string (name),
|
|
|
|
|
.types = malloc (sizeof (gentype_t[num_gentype])
|
|
|
|
|
+ sizeof (genparam_t[num_params])),
|
|
|
|
|
.num_types = num_gentype,
|
|
|
|
|
.num_params = num_params - 1, // don't count return type
|
|
|
|
|
};
|
|
|
|
|
genfunc->params = (genparam_t *) &genfunc->types[num_gentype];
|
|
|
|
|
genfunc->ret_type = &genfunc->params[num_params - 1];
|
|
|
|
|
|
|
|
|
|
num_gentype = 0;
|
|
|
|
|
for (auto s = generic_tab->symbols; s; s = s->next) {
|
|
|
|
|
for (auto q = &ret_param; q; q = q->next) {
|
2024-09-06 17:36:54 +00:00
|
|
|
|
// see complex expressions comment above
|
2024-07-07 12:36:40 +00:00
|
|
|
|
if (!q->type_expr || q->type_expr->type != ex_symbol) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (strcmp (q->type_expr->symbol->name, s->name) == 0) {
|
|
|
|
|
genfunc->types[num_gentype++] = make_gentype (q->type_expr);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
num_params = 0;
|
2024-09-06 17:36:54 +00:00
|
|
|
|
// skip return type so it can be done last to support complex expressions
|
2024-07-07 12:36:40 +00:00
|
|
|
|
for (auto p = ret_param.next; p; p = p->next) {
|
|
|
|
|
genfunc->params[num_params++] = make_genparam (p, genfunc);
|
|
|
|
|
}
|
|
|
|
|
*genfunc->ret_type = make_genparam (&ret_param, genfunc);
|
|
|
|
|
return genfunc;
|
|
|
|
|
}
|
2024-08-11 02:54:39 +00:00
|
|
|
|
|
2002-05-08 05:15:19 +00:00
|
|
|
|
param_t *
|
2024-02-13 14:09:28 +00:00
|
|
|
|
new_param (const char *selector, const type_t *type, const char *name)
|
2002-05-08 05:15:19 +00:00
|
|
|
|
{
|
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);
|
2024-05-01 01:58:01 +00:00
|
|
|
|
*param = (param_t) {
|
|
|
|
|
.selector = selector,
|
|
|
|
|
.type = find_type (type),
|
|
|
|
|
.name = name,
|
2024-09-03 08:59:56 +00:00
|
|
|
|
.qual = pq_in,
|
2024-05-01 01:58:01 +00:00
|
|
|
|
};
|
|
|
|
|
return param;
|
|
|
|
|
}
|
2002-05-08 05:15:19 +00:00
|
|
|
|
|
2024-05-01 01:58:01 +00:00
|
|
|
|
param_t *
|
|
|
|
|
new_generic_param (const expr_t *type_expr, const char *name)
|
|
|
|
|
{
|
|
|
|
|
param_t *param;
|
|
|
|
|
|
|
|
|
|
ALLOC (4096, param_t, params, param);
|
|
|
|
|
*param = (param_t) {
|
|
|
|
|
.type_expr = type_expr,
|
|
|
|
|
.name = name,
|
2024-09-03 08:59:56 +00:00
|
|
|
|
.qual = pq_in,
|
2024-05-01 01:58:01 +00:00
|
|
|
|
};
|
2002-05-08 05:15:19 +00:00
|
|
|
|
return param;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-13 05:45:53 +00:00
|
|
|
|
param_t *
|
2024-02-13 14:09:28 +00:00
|
|
|
|
param_append_identifiers (param_t *params, symbol_t *idents, const type_t *type)
|
2011-01-13 05:45:53 +00:00
|
|
|
|
{
|
|
|
|
|
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 = &(*p)->next;
|
|
|
|
|
idents = idents->next;
|
|
|
|
|
}
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 08:14:14 +00:00
|
|
|
|
static 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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 08:14:14 +00:00
|
|
|
|
param_t *
|
|
|
|
|
append_params (param_t *params, param_t *more_params)
|
|
|
|
|
{
|
|
|
|
|
if (params) {
|
|
|
|
|
param_t *p;
|
|
|
|
|
for (p = params; p->next; ) {
|
|
|
|
|
p = p->next;
|
|
|
|
|
}
|
|
|
|
|
p->next = more_params;
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
return more_params;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-13 14:09:28 +00:00
|
|
|
|
const type_t *
|
|
|
|
|
parse_params (const type_t *return_type, param_t *parms)
|
2002-05-08 05:15:19 +00:00
|
|
|
|
{
|
|
|
|
|
param_t *p;
|
2011-02-03 12:32:58 +00:00
|
|
|
|
type_t *new;
|
2020-02-18 17:43:27 +00:00
|
|
|
|
int count = 0;
|
2002-05-08 05:15:19 +00:00
|
|
|
|
|
2023-02-08 02:18:42 +00:00
|
|
|
|
if (return_type && is_class (return_type)) {
|
2020-03-01 08:44:13 +00:00
|
|
|
|
error (0, "cannot return an object (forgot *?)");
|
2023-02-08 02:18:42 +00:00
|
|
|
|
return_type = &type_id;
|
2020-03-01 08:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-03 12:32:58 +00:00
|
|
|
|
new = new_type ();
|
|
|
|
|
new->type = ev_func;
|
2020-02-14 17:21:21 +00:00
|
|
|
|
new->alignment = 1;
|
2022-01-30 01:54:07 +00:00
|
|
|
|
new->width = 1;
|
2024-04-25 02:22:35 +00:00
|
|
|
|
new->columns = 1;
|
2024-08-16 08:23:29 +00:00
|
|
|
|
new->func.ret_type = return_type;
|
|
|
|
|
new->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) {
|
2020-02-18 16:51:24 +00:00
|
|
|
|
if (p->type) {
|
|
|
|
|
count++;
|
2002-05-08 05:15:19 +00:00
|
|
|
|
}
|
2020-02-18 16:51:24 +00:00
|
|
|
|
}
|
2020-02-18 17:43:27 +00:00
|
|
|
|
if (count) {
|
2024-09-03 08:59:56 +00:00
|
|
|
|
new->func.param_types = malloc (count * sizeof (type_t *));
|
|
|
|
|
new->func.param_quals = malloc (count * sizeof (param_qual_t));
|
2020-02-18 17:43:27 +00:00
|
|
|
|
}
|
2020-02-18 16:51:24 +00:00
|
|
|
|
for (p = parms; p; p = p->next) {
|
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);
|
2024-08-16 08:23:29 +00:00
|
|
|
|
new->func.num_params = -(new->func.num_params + 1);
|
2002-05-09 20:12:28 +00:00
|
|
|
|
} else if (p->type) {
|
2020-03-27 06:16:41 +00:00
|
|
|
|
if (is_class (p->type)) {
|
2020-03-01 08:44:13 +00:00
|
|
|
|
error (0, "cannot use an object as a parameter (forgot *?)");
|
|
|
|
|
p->type = &type_id;
|
|
|
|
|
}
|
2024-05-12 03:48:50 +00:00
|
|
|
|
auto ptype = unalias_type (p->type);
|
2024-08-16 08:23:29 +00:00
|
|
|
|
new->func.param_types[new->func.num_params] = ptype;
|
2024-09-03 08:59:56 +00:00
|
|
|
|
new->func.param_quals[new->func.num_params] = p->qual;
|
2024-08-16 08:23:29 +00:00
|
|
|
|
new->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) {
|
2020-03-27 06:16:41 +00:00
|
|
|
|
if (p->type && is_void(p->type)) {
|
2011-02-01 12:18:08 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 00:00:58 +00:00
|
|
|
|
static metafunc_t *
|
|
|
|
|
new_metafunc (void)
|
|
|
|
|
{
|
|
|
|
|
metafunc_t *metafunc;
|
|
|
|
|
ALLOC (1024, metafunc_t, metafuncs, metafunc);
|
|
|
|
|
return metafunc;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 09:29:44 +00:00
|
|
|
|
static metafunc_t *
|
2024-07-07 12:36:40 +00:00
|
|
|
|
get_function (const char *name, const type_t *type, specifier_t spec)
|
2004-11-13 11:50:00 +00:00
|
|
|
|
{
|
2024-08-20 03:43:17 +00:00
|
|
|
|
metafunc_t *func = Hash_Find (function_map, name);
|
|
|
|
|
if (func && func->meta_type == mf_generic) {
|
|
|
|
|
error (0, "can't mix generic and simple or overload");
|
2024-08-10 05:36:52 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-07 12:36:40 +00:00
|
|
|
|
bool overload = spec.is_overload;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
const char *full_name;
|
|
|
|
|
|
2021-01-31 07:01:20 +00:00
|
|
|
|
full_name = save_string (va (0, "%s|%s", name, encode_params (type)));
|
2004-11-13 11:50:00 +00:00
|
|
|
|
|
2024-08-10 05:36:52 +00:00
|
|
|
|
// check if the exact function signature already exists, in which case
|
|
|
|
|
// simply return it.
|
2024-08-19 09:29:44 +00:00
|
|
|
|
func = Hash_Find (metafuncs, full_name);
|
2004-11-13 11:50:00 +00:00
|
|
|
|
if (func) {
|
2020-03-28 04:38:26 +00:00
|
|
|
|
if (func->type != type) {
|
2004-11-13 11:50:00 +00:00
|
|
|
|
error (0, "can't overload on return types");
|
|
|
|
|
return func;
|
|
|
|
|
}
|
|
|
|
|
return func;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func = Hash_Find (function_map, name);
|
|
|
|
|
if (func) {
|
2024-08-19 09:29:44 +00:00
|
|
|
|
if (!overload && func->meta_type != mf_overload) {
|
2004-11-13 11:50:00 +00:00
|
|
|
|
warning (0, "creating overloaded function %s without @overload",
|
|
|
|
|
full_name);
|
2024-08-10 05:36:52 +00:00
|
|
|
|
warning (&(expr_t) { .loc = func->loc },
|
|
|
|
|
"(previous function is %s)", func->full_name);
|
2004-11-13 11:50:00 +00:00
|
|
|
|
}
|
2024-07-07 12:36:40 +00:00
|
|
|
|
overload = true;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 00:00:58 +00:00
|
|
|
|
func = new_metafunc ();
|
2024-08-19 09:29:44 +00:00
|
|
|
|
*func = (metafunc_t) {
|
2024-08-10 05:36:52 +00:00
|
|
|
|
.name = save_string (name),
|
|
|
|
|
.full_name = full_name,
|
|
|
|
|
.type = type,
|
|
|
|
|
.loc = pr.loc,
|
2024-08-19 09:29:44 +00:00
|
|
|
|
.meta_type = overload ? mf_overload : mf_simple,
|
2024-08-10 05:36:52 +00:00
|
|
|
|
};
|
2004-11-13 11:50:00 +00:00
|
|
|
|
|
2024-08-19 09:29:44 +00:00
|
|
|
|
Hash_Add (metafuncs, func);
|
2004-11-13 11:50:00 +00:00
|
|
|
|
Hash_Add (function_map, func);
|
|
|
|
|
return func;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
symbol_t *
|
2024-08-26 01:35:33 +00:00
|
|
|
|
function_symbol (specifier_t spec)
|
2011-01-18 03:37:12 +00:00
|
|
|
|
{
|
2024-08-26 01:35:33 +00:00
|
|
|
|
symbol_t *sym = spec.sym;
|
2011-01-18 03:37:12 +00:00
|
|
|
|
const char *name = sym->name;
|
2024-08-27 05:37:18 +00:00
|
|
|
|
metafunc_t *func = Hash_Find (function_map, name);
|
2012-05-21 23:23:22 +00:00
|
|
|
|
|
2024-08-27 05:37:18 +00:00
|
|
|
|
auto genfunc = parse_generic_function (name, spec);
|
|
|
|
|
//FIXME want to be able to provide specific overloads for generic functions
|
|
|
|
|
//but need to figure out details, so disallow for now.
|
|
|
|
|
if (genfunc) {
|
|
|
|
|
if (func && func->meta_type != mf_generic) {
|
|
|
|
|
error (0, "can't mix generic and simple or overload");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
add_generic_function (genfunc);
|
|
|
|
|
|
|
|
|
|
ALLOC (1024, metafunc_t, metafuncs, func);
|
|
|
|
|
*func = (metafunc_t) {
|
|
|
|
|
.name = save_string (name),
|
|
|
|
|
.full_name = name,
|
|
|
|
|
.loc = pr.loc,
|
|
|
|
|
.meta_type = mf_generic,
|
|
|
|
|
};
|
|
|
|
|
Hash_Add (metafuncs, func);
|
|
|
|
|
Hash_Add (function_map, func);
|
|
|
|
|
} else {
|
|
|
|
|
if (!spec.sym->type || !spec.sym->type->encoding) {
|
|
|
|
|
spec = default_type (spec, spec.sym);
|
|
|
|
|
spec.sym->type = append_type (spec.sym->type, spec.type);
|
|
|
|
|
set_func_type_attrs (spec.sym->type, spec);
|
|
|
|
|
spec.sym->type = find_type (spec.sym->type);
|
|
|
|
|
}
|
|
|
|
|
func = get_function (name, unalias_type (sym->type), spec);
|
|
|
|
|
}
|
2011-01-18 03:37:12 +00:00
|
|
|
|
|
2024-08-19 09:29:44 +00:00
|
|
|
|
if (func && func->meta_type == mf_overload)
|
2011-01-18 03:37:12 +00:00
|
|
|
|
name = func->full_name;
|
2024-08-27 05:37:18 +00:00
|
|
|
|
symbol_t *s = symtab_lookup (current_symtab, name);
|
2024-05-12 03:05:43 +00:00
|
|
|
|
if (!s || s->table != current_symtab) {
|
2011-01-18 03:37:12 +00:00
|
|
|
|
s = new_symbol (name);
|
|
|
|
|
s->sy_type = sy_func;
|
2024-05-12 03:05:43 +00:00
|
|
|
|
s->type = unalias_type (sym->type);
|
2011-01-18 03:37:12 +00:00
|
|
|
|
s->params = sym->params;
|
2024-08-26 06:50:27 +00:00
|
|
|
|
s->metafunc = func;
|
2011-01-18 03:37:12 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-08-19 09:29:44 +00:00
|
|
|
|
metafunc_t *fa = *(metafunc_t **) a;
|
|
|
|
|
metafunc_t *fb = *(metafunc_t **) b;
|
2020-03-28 04:38:26 +00:00
|
|
|
|
const type_t *ta = fa->type;
|
|
|
|
|
const type_t *tb = fb->type;
|
2024-08-16 08:23:29 +00:00
|
|
|
|
int na = ta->func.num_params;
|
|
|
|
|
int nb = tb->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;
|
2024-08-16 08:23:29 +00:00
|
|
|
|
if ((ret = (tb->func.num_params - ta->func.num_params)))
|
2004-11-13 11:50:00 +00:00
|
|
|
|
return ret;
|
2024-08-11 02:54:39 +00:00
|
|
|
|
for (i = 0; i < na && i < nb; i++) {
|
2024-08-16 08:23:29 +00:00
|
|
|
|
auto diff = tb->func.param_types[i] - ta->func.param_types[i];
|
2024-08-11 02:54:39 +00:00
|
|
|
|
if (diff) {
|
|
|
|
|
return diff < 0 ? -1 : 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-11-13 11:50:00 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 03:41:31 +00:00
|
|
|
|
static const expr_t *
|
2024-08-19 09:29:44 +00:00
|
|
|
|
set_func_symbol (const expr_t *fexpr, metafunc_t *f)
|
2023-09-27 03:41:31 +00:00
|
|
|
|
{
|
|
|
|
|
auto sym = symtab_lookup (current_symtab, f->full_name);
|
|
|
|
|
if (!sym) {
|
|
|
|
|
internal_error (fexpr, "overloaded function %s not found",
|
|
|
|
|
f->full_name);
|
|
|
|
|
}
|
|
|
|
|
auto nf = new_expr ();
|
|
|
|
|
*nf = *fexpr;
|
|
|
|
|
nf->symbol = sym;
|
|
|
|
|
return nf;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-11 06:18:20 +00:00
|
|
|
|
static const type_t * __attribute__((pure))
|
|
|
|
|
select_type (gentype_t *gentype, const type_t *param_type)
|
|
|
|
|
{
|
|
|
|
|
for (auto t = gentype->valid_types; t && *t; t++) {
|
|
|
|
|
if (*t == param_type || type_promotes (*t, param_type)) {
|
|
|
|
|
return *t;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
check_type (const type_t *type, const type_t *param_type, unsigned *cost)
|
|
|
|
|
{
|
|
|
|
|
if (type != param_type) {
|
|
|
|
|
if (type_promotes (type, param_type)) {
|
|
|
|
|
*cost += 1;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const expr_t *
|
2024-08-11 13:40:21 +00:00
|
|
|
|
find_generic_function (const expr_t *fexpr, genfunc_t **genfuncs,
|
|
|
|
|
const type_t *call_type)
|
2024-08-11 06:18:20 +00:00
|
|
|
|
{
|
|
|
|
|
int num_funcs = 0;
|
|
|
|
|
for (auto gf = genfuncs; *gf; gf++, num_funcs++) continue;
|
|
|
|
|
unsigned costs[num_funcs] = {};
|
|
|
|
|
|
2024-08-16 08:23:29 +00:00
|
|
|
|
int num_params = call_type->func.num_params;
|
|
|
|
|
auto call_params = call_type->func.param_types;
|
2024-08-11 06:18:20 +00:00
|
|
|
|
for (int j = 0; j < num_funcs; j++) {
|
|
|
|
|
auto g = genfuncs[j];
|
|
|
|
|
if (g->num_params != num_params) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const type_t *types[g->num_types] = {};
|
|
|
|
|
bool ok = true;
|
|
|
|
|
for (int i = 0; ok && i < num_params; i++) {
|
|
|
|
|
auto p = &g->params[i];
|
|
|
|
|
if (!p->fixed_type) {
|
|
|
|
|
int ind = p->gentype;
|
|
|
|
|
if (!types[ind]) {
|
|
|
|
|
types[ind] = select_type (&g->types[ind], call_params[i]);
|
|
|
|
|
}
|
|
|
|
|
ok &= check_type (types[ind], call_params[i], costs + j);
|
|
|
|
|
} else {
|
|
|
|
|
ok &= check_type (p->fixed_type, call_params[i], costs + j);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!ok) {
|
|
|
|
|
costs[j] = ~0u;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-11 13:40:21 +00:00
|
|
|
|
|
|
|
|
|
auto fsym = fexpr->symbol;
|
2024-08-11 06:18:20 +00:00
|
|
|
|
unsigned best_cost = ~0u;
|
|
|
|
|
int best_ind = -1;
|
|
|
|
|
for (int i = 0; i < num_funcs; i++) {
|
|
|
|
|
if (best_ind >= 0 && costs[i] == best_cost) {
|
2024-08-11 13:40:21 +00:00
|
|
|
|
return error (fexpr, "unable to disambiguate %s", fsym->name);
|
2024-08-11 06:18:20 +00:00
|
|
|
|
}
|
|
|
|
|
if (costs[i] < best_cost) {
|
|
|
|
|
best_ind = i;
|
|
|
|
|
best_cost = costs[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (best_ind < 0) {
|
2024-08-11 13:40:21 +00:00
|
|
|
|
return error (fexpr, "unable to find generic function matching %s",
|
|
|
|
|
fsym->name);
|
2024-08-11 06:18:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto g = genfuncs[best_ind];
|
|
|
|
|
const type_t *types[g->num_types] = {};
|
|
|
|
|
const type_t *param_types[num_params];
|
2024-09-06 17:36:54 +00:00
|
|
|
|
param_qual_t param_quals[num_params];
|
2024-08-11 06:18:20 +00:00
|
|
|
|
const type_t *return_type;
|
|
|
|
|
for (int i = 0; i < num_params; i++) {
|
|
|
|
|
auto p = &g->params[i];
|
|
|
|
|
if (!p->fixed_type) {
|
|
|
|
|
int ind = p->gentype;
|
|
|
|
|
if (!types[ind]) {
|
|
|
|
|
types[ind] = select_type (&g->types[ind], call_params[i]);
|
|
|
|
|
}
|
|
|
|
|
param_types[i] = types[ind];
|
|
|
|
|
} else {
|
|
|
|
|
param_types[i] = p->fixed_type;
|
|
|
|
|
}
|
2024-09-06 17:36:54 +00:00
|
|
|
|
param_quals[i] = p->qual;
|
2024-08-11 06:18:20 +00:00
|
|
|
|
}
|
|
|
|
|
if (!g->ret_type->fixed_type) {
|
|
|
|
|
int ind = g->ret_type->gentype;
|
|
|
|
|
if (!types[ind]) {
|
|
|
|
|
internal_error (0, "return type not determined");
|
|
|
|
|
}
|
|
|
|
|
return_type = types[ind];
|
|
|
|
|
} else {
|
|
|
|
|
return_type = g->ret_type->fixed_type;
|
|
|
|
|
}
|
2024-08-11 13:40:21 +00:00
|
|
|
|
param_t *params = nullptr;
|
2024-08-11 06:18:20 +00:00
|
|
|
|
for (int i = 0; i < num_params; i++) {
|
|
|
|
|
param_types[i] = unalias_type (param_types[i]);
|
2024-08-11 13:40:21 +00:00
|
|
|
|
params = append_params (params, new_param (nullptr, param_types[i],
|
|
|
|
|
g->params[i].name));
|
2024-08-11 06:18:20 +00:00
|
|
|
|
}
|
|
|
|
|
return_type = unalias_type (return_type);
|
|
|
|
|
|
|
|
|
|
type_t ftype = {
|
|
|
|
|
.type = ev_func,
|
|
|
|
|
|
2024-08-16 08:23:29 +00:00
|
|
|
|
.func = {
|
2024-08-11 06:18:20 +00:00
|
|
|
|
.ret_type = return_type,
|
|
|
|
|
.num_params = num_params,
|
|
|
|
|
.param_types = param_types,
|
2024-09-06 17:36:54 +00:00
|
|
|
|
.param_quals = param_quals,
|
2024-08-11 06:18:20 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
auto type = find_type (&ftype);
|
|
|
|
|
auto name = g->name;
|
|
|
|
|
auto full_name = save_string (va (0, "%s|%s", name, encode_params (type)));
|
2024-08-11 13:40:21 +00:00
|
|
|
|
|
|
|
|
|
auto sym = symtab_lookup (fsym->table, full_name);
|
|
|
|
|
if (!sym || sym->table != fsym->table) {
|
|
|
|
|
sym = new_symbol (full_name);
|
|
|
|
|
sym->sy_type = sy_func;
|
|
|
|
|
sym->type = type;
|
|
|
|
|
sym->params = params;
|
2024-08-28 00:00:58 +00:00
|
|
|
|
sym->metafunc = new_metafunc ();
|
|
|
|
|
*sym->metafunc = *fsym->metafunc;
|
2024-08-11 13:40:21 +00:00
|
|
|
|
symtab_addsymbol (fsym->table, sym);
|
|
|
|
|
}
|
|
|
|
|
return new_symbol_expr (sym);
|
2024-08-11 06:18:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 03:41:31 +00:00
|
|
|
|
const expr_t *
|
|
|
|
|
find_function (const expr_t *fexpr, const expr_t *params)
|
2004-11-13 11:50:00 +00:00
|
|
|
|
{
|
2024-05-12 03:48:50 +00:00
|
|
|
|
int func_count, parm_count, reported = 0;
|
2024-08-19 09:29:44 +00:00
|
|
|
|
metafunc_t dummy, *best = 0;
|
|
|
|
|
void *dummy_p = &dummy;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
|
2024-08-11 02:54:39 +00:00
|
|
|
|
if (fexpr->type != ex_symbol) {
|
2004-11-13 11:50:00 +00:00
|
|
|
|
return fexpr;
|
2024-08-11 02:54:39 +00:00
|
|
|
|
}
|
2004-11-13 11:50:00 +00:00
|
|
|
|
|
2024-08-11 02:54:39 +00:00
|
|
|
|
int num_params = params ? list_count (¶ms->list) : 0;
|
|
|
|
|
const type_t *arg_types[num_params + 1];
|
2024-09-06 17:36:54 +00:00
|
|
|
|
param_qual_t arg_quals[num_params + 1];
|
2024-08-11 02:54:39 +00:00
|
|
|
|
const expr_t *args[num_params + 1];
|
2023-09-24 11:44:50 +00:00
|
|
|
|
if (params) {
|
|
|
|
|
list_scatter_rev (¶ms->list, args);
|
|
|
|
|
}
|
2024-08-11 02:54:39 +00:00
|
|
|
|
for (int i = 0; i < num_params; i++) {
|
2023-09-24 11:44:50 +00:00
|
|
|
|
auto e = args[i];
|
|
|
|
|
if (e->type == ex_error) {
|
2004-11-13 11:50:00 +00:00
|
|
|
|
return e;
|
2023-09-24 11:44:50 +00:00
|
|
|
|
}
|
2024-08-11 02:54:39 +00:00
|
|
|
|
arg_types[i] = get_type (e);
|
2024-09-06 17:36:54 +00:00
|
|
|
|
arg_quals[i] = pq_in;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
}
|
2024-08-11 02:54:39 +00:00
|
|
|
|
|
|
|
|
|
type_t call_type = {
|
|
|
|
|
.type = ev_func,
|
2024-08-16 08:23:29 +00:00
|
|
|
|
.func = {
|
2024-08-11 02:54:39 +00:00
|
|
|
|
.num_params = num_params,
|
|
|
|
|
.param_types = arg_types,
|
2024-09-06 17:36:54 +00:00
|
|
|
|
.param_quals = arg_quals,
|
2024-08-11 02:54:39 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const char *fname = fexpr->symbol->name;
|
2024-08-11 06:18:20 +00:00
|
|
|
|
auto genfuncs = (genfunc_t **) Hash_FindList (generic_functions, fname);
|
|
|
|
|
if (genfuncs) {
|
2024-08-11 13:40:21 +00:00
|
|
|
|
return find_generic_function (fexpr, genfuncs, &call_type);
|
2024-08-11 06:18:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 09:29:44 +00:00
|
|
|
|
auto funcs = (metafunc_t **) Hash_FindList (function_map, fname);
|
2004-11-13 11:50:00 +00:00
|
|
|
|
if (!funcs)
|
|
|
|
|
return fexpr;
|
2024-08-11 02:54:39 +00:00
|
|
|
|
for (func_count = 0; funcs[func_count]; func_count++) continue;
|
2020-02-14 11:39:12 +00:00
|
|
|
|
if (func_count < 2) {
|
2024-08-19 09:29:44 +00:00
|
|
|
|
if (func_count && funcs[0]->meta_type != mf_overload) {
|
2020-02-14 11:39:12 +00:00
|
|
|
|
free (funcs);
|
|
|
|
|
return fexpr;
|
|
|
|
|
}
|
2004-11-13 11:50:00 +00:00
|
|
|
|
}
|
2024-08-16 08:23:29 +00:00
|
|
|
|
call_type.func.ret_type = funcs[0]->type->func.ret_type;
|
2024-08-11 02:54:39 +00:00
|
|
|
|
dummy.type = find_type (&call_type);
|
2004-11-13 11:50:00 +00:00
|
|
|
|
|
|
|
|
|
qsort (funcs, func_count, sizeof (void *), func_compare);
|
2023-09-23 09:01:49 +00:00
|
|
|
|
dummy.full_name = save_string (va (0, "%s|%s", fexpr->symbol->name,
|
2024-08-11 02:54:39 +00:00
|
|
|
|
encode_params (&call_type)));
|
2004-11-13 11:50:00 +00:00
|
|
|
|
dummy_p = bsearch (&dummy_p, funcs, func_count, sizeof (void *),
|
|
|
|
|
func_compare);
|
|
|
|
|
if (dummy_p) {
|
2024-08-19 09:29:44 +00:00
|
|
|
|
auto f = (metafunc_t *) *(void **) dummy_p;
|
|
|
|
|
if (f->meta_type == mf_overload) {
|
2023-09-27 03:41:31 +00:00
|
|
|
|
fexpr = set_func_symbol (fexpr, f);
|
2013-01-07 04:51:01 +00:00
|
|
|
|
}
|
2004-11-13 11:50:00 +00:00
|
|
|
|
free (funcs);
|
|
|
|
|
return fexpr;
|
|
|
|
|
}
|
2024-05-12 03:48:50 +00:00
|
|
|
|
for (int i = 0; i < func_count; i++) {
|
2024-08-19 09:29:44 +00:00
|
|
|
|
auto f = (metafunc_t *) funcs[i];
|
2024-08-16 08:23:29 +00:00
|
|
|
|
parm_count = f->type->func.num_params;
|
|
|
|
|
if ((parm_count >= 0 && parm_count != call_type.func.num_params)
|
|
|
|
|
|| (parm_count < 0 && ~parm_count > call_type.func.num_params)) {
|
2004-11-13 11:50:00 +00:00
|
|
|
|
funcs[i] = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (parm_count < 0)
|
|
|
|
|
parm_count = ~parm_count;
|
2024-05-12 03:48:50 +00:00
|
|
|
|
int j;
|
2004-11-13 11:50:00 +00:00
|
|
|
|
for (j = 0; j < parm_count; j++) {
|
2024-08-16 08:23:29 +00:00
|
|
|
|
if (!type_assignable (f->type->func.param_types[j],
|
|
|
|
|
call_type.func.param_types[j])) {
|
2004-11-13 11:50:00 +00:00
|
|
|
|
funcs[i] = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (j < parm_count)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-05-12 03:48:50 +00:00
|
|
|
|
for (int i = 0; i < func_count; i++) {
|
2024-08-19 09:29:44 +00:00
|
|
|
|
auto f = (metafunc_t *) funcs[i];
|
2004-11-13 11:50:00 +00:00
|
|
|
|
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) {
|
2024-08-19 09:29:44 +00:00
|
|
|
|
if (best->meta_type == mf_overload) {
|
2023-09-27 03:41:31 +00:00
|
|
|
|
fexpr = set_func_symbol (fexpr, best);
|
2011-03-03 05:44:48 +00:00
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 23:39:20 +00:00
|
|
|
|
int
|
2024-02-13 14:09:28 +00:00
|
|
|
|
value_too_large (const type_t *val_type)
|
2022-02-14 23:39:20 +00:00
|
|
|
|
{
|
|
|
|
|
if ((options.code.progsversion < PROG_VERSION
|
|
|
|
|
&& type_size (val_type) > type_size (&type_param))
|
|
|
|
|
|| (options.code.progsversion == PROG_VERSION
|
|
|
|
|
&& type_size (val_type) > MAX_DEF_SIZE)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2024-08-20 03:43:17 +00:00
|
|
|
|
auto ret_type = fsym->type->func.ret_type;
|
2010-11-17 05:45:35 +00:00
|
|
|
|
|
2024-08-20 03:43:17 +00:00
|
|
|
|
if (!ret_type || !type_size (ret_type)) {
|
2010-11-17 05:45:35 +00:00
|
|
|
|
error (0, "return type is an incomplete type");
|
2024-08-20 03:43:17 +00:00
|
|
|
|
return;
|
|
|
|
|
//fsym->type->t.func.type = &type_void;//FIXME better type?
|
2010-11-17 05:45:35 +00:00
|
|
|
|
}
|
2024-08-20 03:43:17 +00:00
|
|
|
|
if (value_too_large (ret_type)) {
|
2019-06-05 21:46:38 +00:00
|
|
|
|
error (0, "return value too large to be passed by value (%d)",
|
|
|
|
|
type_size (&type_param));
|
2024-08-16 08:23:29 +00:00
|
|
|
|
//fsym->type->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
|
2022-02-14 23:39:20 +00:00
|
|
|
|
if (!type_size (p->type)) {
|
2010-11-17 05:45:35 +00:00
|
|
|
|
error (0, "parameter %d (‘%s’) has incomplete type",
|
|
|
|
|
i + 1, p->name);
|
2022-02-14 23:39:20 +00:00
|
|
|
|
}
|
|
|
|
|
if (value_too_large (p->type)) {
|
2010-11-17 05:45:35 +00:00
|
|
|
|
error (0, "param %d (‘%s’) is too large to be passed by value",
|
|
|
|
|
i + 1, p->name);
|
2022-02-14 23:39:20 +00:00
|
|
|
|
}
|
2010-11-17 05:45:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-18 03:37:12 +00:00
|
|
|
|
static void
|
2022-01-24 09:35:16 +00:00
|
|
|
|
build_v6p_scope (symbol_t *fsym)
|
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;
|
2024-08-26 06:50:27 +00:00
|
|
|
|
function_t *func = fsym->metafunc->func;
|
|
|
|
|
symtab_t *parameters = func->parameters;
|
|
|
|
|
symtab_t *locals = func->locals;
|
2022-01-21 01:20:02 +00:00
|
|
|
|
|
2024-08-26 06:50:27 +00:00
|
|
|
|
if (func->type->func.num_params < 0) {
|
2011-01-18 03:37:12 +00:00
|
|
|
|
args = new_symbol_type (".args", &type_va_list);
|
2022-01-24 03:48:02 +00:00
|
|
|
|
initialize_def (args, 0, parameters->space, sc_param, locals);
|
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);
|
2022-01-24 03:48:02 +00:00
|
|
|
|
initialize_def (param, 0, parameters->space, sc_param, locals);
|
2024-09-03 08:59:56 +00:00
|
|
|
|
if (p->qual == pq_out) {
|
|
|
|
|
param->def->param = false;
|
|
|
|
|
param->def->out_param = true;
|
|
|
|
|
} else if (p->qual == pq_inout) {
|
|
|
|
|
param->def->out_param = true;
|
|
|
|
|
} else if (p->qual == pq_const) {
|
|
|
|
|
param->def->readonly = true;
|
|
|
|
|
}
|
2002-05-22 20:43:29 +00:00
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-16 02:28:08 +00:00
|
|
|
|
if (args) {
|
2022-01-23 05:17:25 +00:00
|
|
|
|
while (i < PR_MAX_PARAMS) {
|
2021-01-31 07:01:20 +00:00
|
|
|
|
param = new_symbol_type (va (0, ".par%d", i), &type_param);
|
2022-01-24 03:48:02 +00:00
|
|
|
|
initialize_def (param, 0, parameters->space, sc_param, locals);
|
2002-05-22 20:43:29 +00:00
|
|
|
|
i++;
|
|
|
|
|
}
|
2002-05-08 05:15:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 09:35:16 +00:00
|
|
|
|
static void
|
|
|
|
|
create_param (symtab_t *parameters, symbol_t *param)
|
|
|
|
|
{
|
|
|
|
|
defspace_t *space = parameters->space;
|
|
|
|
|
def_t *def = new_def (param->name, 0, space, sc_param);
|
|
|
|
|
int size = type_size (param->type);
|
|
|
|
|
int alignment = param->type->alignment;
|
|
|
|
|
if (alignment < 4) {
|
|
|
|
|
alignment = 4;
|
|
|
|
|
}
|
|
|
|
|
def->offset = defspace_alloc_aligned_highwater (space, size, alignment);
|
|
|
|
|
def->type = param->type;
|
2024-08-16 07:48:11 +00:00
|
|
|
|
param->def = def;
|
2024-09-20 02:27:15 +00:00
|
|
|
|
param->sy_type = sy_def;
|
2024-09-21 13:08:51 +00:00
|
|
|
|
param->lvalue = !def->readonly;
|
2022-01-24 09:35:16 +00:00
|
|
|
|
symtab_addsymbol (parameters, param);
|
2022-02-06 11:13:59 +00:00
|
|
|
|
if (is_vector(param->type) && options.code.vector_components)
|
|
|
|
|
init_vector_components (param, 0, parameters);
|
2022-01-24 09:35:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
build_rua_scope (symbol_t *fsym)
|
|
|
|
|
{
|
2024-08-26 06:50:27 +00:00
|
|
|
|
function_t *func = fsym->metafunc->func;
|
|
|
|
|
|
2022-01-24 09:35:16 +00:00
|
|
|
|
for (param_t *p = fsym->params; p; p = p->next) {
|
|
|
|
|
symbol_t *param;
|
|
|
|
|
if (!p->selector && !p->type && !p->name) {
|
|
|
|
|
// ellipsis marker
|
|
|
|
|
param = new_symbol_type (".args", &type_va_list);
|
|
|
|
|
} else {
|
|
|
|
|
if (!p->type) {
|
|
|
|
|
continue; // non-param selector
|
|
|
|
|
}
|
2024-08-30 04:32:32 +00:00
|
|
|
|
if (is_void (p->type)) {
|
|
|
|
|
if (p->name) {
|
|
|
|
|
error (0, "invalid parameter type for %s", p->name);
|
|
|
|
|
} else if (p != fsym->params || p->next) {
|
|
|
|
|
error (0, "void must be the only parameter");
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-24 09:35:16 +00:00
|
|
|
|
if (!p->name) {
|
|
|
|
|
error (0, "parameter name omitted");
|
|
|
|
|
p->name = save_string ("");
|
|
|
|
|
}
|
|
|
|
|
param = new_symbol_type (p->name, p->type);
|
|
|
|
|
}
|
2024-08-26 06:50:27 +00:00
|
|
|
|
create_param (func->parameters, param);
|
2024-09-03 08:59:56 +00:00
|
|
|
|
if (p->qual == pq_out) {
|
|
|
|
|
param->def->param = false;
|
|
|
|
|
param->def->out_param = true;
|
|
|
|
|
} else if (p->qual == pq_inout) {
|
|
|
|
|
param->def->out_param = true;
|
|
|
|
|
} else if (p->qual == pq_const) {
|
|
|
|
|
param->def->readonly = true;
|
|
|
|
|
}
|
2024-08-26 06:50:27 +00:00
|
|
|
|
param->def->reg = func->temp_reg;
|
2022-01-24 09:35:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
build_scope (symbol_t *fsym, symtab_t *parent)
|
|
|
|
|
{
|
2024-08-26 06:50:27 +00:00
|
|
|
|
function_t *func = fsym->metafunc->func;
|
2022-01-24 09:35:16 +00:00
|
|
|
|
symtab_t *parameters;
|
|
|
|
|
symtab_t *locals;
|
|
|
|
|
|
2024-08-26 06:50:27 +00:00
|
|
|
|
if (!func) {
|
2022-01-24 09:35:16 +00:00
|
|
|
|
internal_error (0, "function %s not defined", fsym->name);
|
|
|
|
|
}
|
2024-08-26 06:50:27 +00:00
|
|
|
|
if (!is_func (func->type)) {
|
2022-01-24 09:35:16 +00:00
|
|
|
|
internal_error (0, "function type %s not a funciton", fsym->name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
check_function (fsym);
|
|
|
|
|
|
2024-08-26 06:50:27 +00:00
|
|
|
|
func->label_scope = new_symtab (0, stab_label);
|
2022-01-24 09:35:16 +00:00
|
|
|
|
|
2023-02-11 05:38:37 +00:00
|
|
|
|
parameters = new_symtab (parent, stab_param);
|
2022-01-24 09:35:16 +00:00
|
|
|
|
parameters->space = defspace_new (ds_virtual);
|
2024-08-26 06:50:27 +00:00
|
|
|
|
func->parameters = parameters;
|
2022-01-24 09:35:16 +00:00
|
|
|
|
|
|
|
|
|
locals = new_symtab (parameters, stab_local);
|
|
|
|
|
locals->space = defspace_new (ds_virtual);
|
2024-08-26 06:50:27 +00:00
|
|
|
|
func->locals = locals;
|
2022-01-24 09:35:16 +00:00
|
|
|
|
|
|
|
|
|
if (options.code.progsversion == PROG_VERSION) {
|
|
|
|
|
build_rua_scope (fsym);
|
|
|
|
|
} else {
|
|
|
|
|
build_v6p_scope (fsym);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 00:26:24 +00:00
|
|
|
|
static 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);
|
2024-09-17 07:30:45 +00:00
|
|
|
|
f->o_name = save_string (name);
|
2011-01-18 03:37:12 +00:00
|
|
|
|
f->s_name = ReuseString (name);
|
2023-11-06 04:41:17 +00:00
|
|
|
|
f->s_file = pr.loc.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
|
|
|
|
|
2024-08-26 06:50:27 +00:00
|
|
|
|
function_t *
|
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);
|
2024-08-26 06:50:27 +00:00
|
|
|
|
if (storage == sc_extern && sym->metafunc->func)
|
|
|
|
|
return sym->metafunc->func;
|
|
|
|
|
function_t *func = sym->metafunc->func;
|
|
|
|
|
if (!func) {
|
|
|
|
|
func = new_function (sym->name, nice_name);
|
|
|
|
|
func->sym = sym;
|
|
|
|
|
func->type = unalias_type (sym->type);
|
|
|
|
|
sym->metafunc->func = func;
|
|
|
|
|
}
|
|
|
|
|
if (func->def && func->def->external && storage != sc_extern) {
|
2011-03-09 01:52:29 +00:00
|
|
|
|
//FIXME this really is not the right way
|
2024-08-26 06:50:27 +00:00
|
|
|
|
relocs = func->def->relocs;
|
|
|
|
|
free_def (func->def);
|
|
|
|
|
func->def = 0;
|
2011-01-26 05:48:22 +00:00
|
|
|
|
}
|
2024-08-26 06:50:27 +00:00
|
|
|
|
if (!func->def) {
|
|
|
|
|
func->def = new_def (sym->name, sym->type, space, storage);
|
|
|
|
|
reloc_attach_relocs (relocs, &func->def->relocs);
|
2011-03-09 01:52:29 +00:00
|
|
|
|
}
|
2024-08-26 06:50:27 +00:00
|
|
|
|
return func;
|
2011-01-26 05:48:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 00:26:24 +00:00
|
|
|
|
static void
|
2008-08-01 13:54:24 +00:00
|
|
|
|
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,
|
2021-09-24 10:49:55 +00:00
|
|
|
|
int far, storage_class_t storage)
|
2011-01-06 11:22:30 +00:00
|
|
|
|
{
|
2011-01-18 03:37:12 +00:00
|
|
|
|
if (sym->sy_type != sy_func) {
|
|
|
|
|
error (0, "%s is not a function", sym->name);
|
2022-01-18 13:34:52 +00:00
|
|
|
|
sym = new_symbol_type (sym->name, &type_func);
|
2024-08-26 01:35:33 +00:00
|
|
|
|
sym = function_symbol ((specifier_t) {
|
|
|
|
|
.sym = sym,
|
|
|
|
|
.is_overload = true
|
|
|
|
|
});
|
2011-01-06 11:22:30 +00:00
|
|
|
|
}
|
2024-08-26 06:50:27 +00:00
|
|
|
|
function_t *func = sym->metafunc->func;
|
|
|
|
|
if (func && func->def && 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);
|
2024-08-26 01:35:33 +00:00
|
|
|
|
sym = function_symbol ((specifier_t) {
|
|
|
|
|
.sym = sym,
|
|
|
|
|
.is_overload = true
|
|
|
|
|
});
|
2011-01-18 03:37:12 +00:00
|
|
|
|
}
|
2024-08-20 03:43:17 +00:00
|
|
|
|
|
|
|
|
|
defspace_t *space = far ? pr.far_data : sym->table->space;
|
|
|
|
|
|
2024-08-26 06:50:27 +00:00
|
|
|
|
func = make_function (sym, nicename, space, storage);
|
|
|
|
|
if (!func->def->external) {
|
|
|
|
|
func->def->initialized = 1;
|
|
|
|
|
func->def->constant = 1;
|
|
|
|
|
func->def->nosave = 1;
|
|
|
|
|
add_function (func);
|
|
|
|
|
reloc_def_func (func, func->def);
|
2022-02-06 11:14:52 +00:00
|
|
|
|
|
2024-08-26 06:50:27 +00:00
|
|
|
|
func->def->loc = pr.loc;
|
2011-01-26 05:48:22 +00:00
|
|
|
|
}
|
2024-08-26 06:50:27 +00:00
|
|
|
|
func->code = pr.code->size;
|
2011-01-26 05:48:22 +00:00
|
|
|
|
|
2024-08-26 06:50:27 +00:00
|
|
|
|
func->s_file = pr.loc.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 ();
|
2024-08-26 06:50:27 +00:00
|
|
|
|
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);
|
2024-08-26 06:50:27 +00:00
|
|
|
|
return func;
|
2011-01-06 11:22:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 16:51:24 +00:00
|
|
|
|
static void
|
|
|
|
|
build_function (symbol_t *fsym)
|
|
|
|
|
{
|
2024-08-26 06:50:27 +00:00
|
|
|
|
const type_t *func_type = fsym->metafunc->func->type;
|
2024-08-16 08:23:29 +00:00
|
|
|
|
if (func_type->func.num_params > PR_MAX_PARAMS) {
|
2020-02-18 16:51:24 +00:00
|
|
|
|
error (0, "too many params");
|
|
|
|
|
}
|
2022-01-21 01:20:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
merge_spaces (defspace_t *dst, defspace_t *src, int alignment)
|
|
|
|
|
{
|
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
|
|
for (def_t *def = src->defs; def; def = def->next) {
|
|
|
|
|
if (def->type->alignment > alignment) {
|
|
|
|
|
alignment = def->type->alignment;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
offset = defspace_alloc_aligned_highwater (dst, src->size, alignment);
|
|
|
|
|
for (def_t *def = src->defs; def; def = def->next) {
|
|
|
|
|
def->offset += offset;
|
|
|
|
|
def->space = dst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (src->defs) {
|
|
|
|
|
*dst->def_tail = src->defs;
|
|
|
|
|
dst->def_tail = src->def_tail;
|
|
|
|
|
src->def_tail = &src->defs;
|
|
|
|
|
*src->def_tail = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defspace_delete (src);
|
2020-02-18 16:51:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-02-11 00:36:34 +00:00
|
|
|
|
function_t *
|
2023-09-27 03:41:31 +00:00
|
|
|
|
build_code_function (symbol_t *fsym, const 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;
|
2020-02-18 16:51:24 +00:00
|
|
|
|
build_function (fsym);
|
2004-02-11 00:36:34 +00:00
|
|
|
|
if (state_expr) {
|
2022-01-29 09:57:48 +00:00
|
|
|
|
prepend_expr (statements, state_expr);
|
2004-02-11 00:36:34 +00:00
|
|
|
|
}
|
2024-08-26 06:50:27 +00:00
|
|
|
|
function_t *func = fsym->metafunc->func;
|
2022-01-21 01:20:02 +00:00
|
|
|
|
if (options.code.progsversion == PROG_VERSION) {
|
2022-01-26 03:56:15 +00:00
|
|
|
|
/* Create a function entry block to set up the stack frame and add the
|
|
|
|
|
* actual function code to that block. This ensure that the adjstk and
|
|
|
|
|
* with statements always come first, regardless of what ideas the
|
|
|
|
|
* optimizer gets.
|
|
|
|
|
*/
|
2022-01-21 09:47:12 +00:00
|
|
|
|
expr_t *e;
|
2023-09-27 03:41:31 +00:00
|
|
|
|
expr_t *entry = new_block_expr (0);
|
2023-11-06 04:41:17 +00:00
|
|
|
|
entry->loc = func->def->loc;
|
2022-01-26 03:56:15 +00:00
|
|
|
|
|
|
|
|
|
e = new_adjstk_expr (0, 0);
|
2023-11-06 04:41:17 +00:00
|
|
|
|
e->loc = entry->loc;
|
2022-01-26 03:56:15 +00:00
|
|
|
|
append_expr (entry, e);
|
2022-01-21 09:47:12 +00:00
|
|
|
|
|
2022-01-26 03:56:15 +00:00
|
|
|
|
e = new_with_expr (2, LOCALS_REG, new_short_expr (0));
|
2023-11-06 04:41:17 +00:00
|
|
|
|
e->loc = entry->loc;
|
2022-01-26 03:56:15 +00:00
|
|
|
|
append_expr (entry, e);
|
|
|
|
|
|
|
|
|
|
append_expr (entry, statements);
|
|
|
|
|
statements = entry;
|
2022-01-21 11:34:43 +00:00
|
|
|
|
|
2022-01-26 03:56:15 +00:00
|
|
|
|
/* Mark all local defs as using the base register used for stack
|
|
|
|
|
* references.
|
|
|
|
|
*/
|
2022-01-21 11:34:43 +00:00
|
|
|
|
func->temp_reg = LOCALS_REG;
|
|
|
|
|
for (def_t *def = func->locals->space->defs; def; def = def->next) {
|
|
|
|
|
if (def->local || def->param) {
|
|
|
|
|
def->reg = LOCALS_REG;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (def_t *def = func->parameters->space->defs; def; def = def->next) {
|
|
|
|
|
if (def->local || def->param) {
|
|
|
|
|
def->reg = LOCALS_REG;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-21 01:20:02 +00:00
|
|
|
|
}
|
|
|
|
|
emit_function (func, statements);
|
2023-09-12 13:26:30 +00:00
|
|
|
|
defspace_sort_defs (func->parameters->space);
|
|
|
|
|
defspace_sort_defs (func->locals->space);
|
2022-01-21 01:20:02 +00:00
|
|
|
|
if (options.code.progsversion < PROG_VERSION) {
|
|
|
|
|
// stitch parameter and locals data together with parameters coming
|
|
|
|
|
// first
|
|
|
|
|
defspace_t *space = defspace_new (ds_virtual);
|
|
|
|
|
|
2022-01-27 02:25:50 +00:00
|
|
|
|
func->params_start = 0;
|
|
|
|
|
|
2022-01-21 01:20:02 +00:00
|
|
|
|
merge_spaces (space, func->parameters->space, 1);
|
|
|
|
|
func->parameters->space = space;
|
|
|
|
|
|
|
|
|
|
merge_spaces (space, func->locals->space, 1);
|
|
|
|
|
func->locals->space = space;
|
|
|
|
|
} else {
|
|
|
|
|
defspace_t *space = defspace_new (ds_virtual);
|
|
|
|
|
|
|
|
|
|
if (func->arguments) {
|
|
|
|
|
func->arguments->size = func->arguments->max_size;
|
[qfcc] Update sizes and alignments for dvec4 and friends
dvec4, lvec4 and ulvec4 need to be aligned to 8 words (32 bytes) in
order to avoid hardware exceptions. Rather than dealing with possibly
mixed alignment when a function has 8-word aligned locals but only
4-word aligned parameters, simply keep the stack frame 8-word aligned at
all times.
As for sizes, the temp def recycler was written before the Ruamoko ISA
was even a pipe dream and thus never expected temp def sizes over 4. At
least now any future adjustments can be done in one place.
My quick and dirty test program works :)
dvec4 xy = {1d, 2d, 0d, 0.5};
void printf(string fmt, ...) = #0;
int main()
{
dvec4 u = {3, 4, 3.14};
dvec4 v = {3, 4, 0, 1};
dvec4 w = v * xy + u;
printf ("[%g, %g, %g, %g]\n", w[0], w[1], w[2], w[3]);
return 0;
}
2022-02-03 23:46:58 +00:00
|
|
|
|
merge_spaces (space, func->arguments, STACK_ALIGN);
|
2022-01-21 01:20:02 +00:00
|
|
|
|
func->arguments = 0;
|
|
|
|
|
}
|
|
|
|
|
|
[qfcc] Update sizes and alignments for dvec4 and friends
dvec4, lvec4 and ulvec4 need to be aligned to 8 words (32 bytes) in
order to avoid hardware exceptions. Rather than dealing with possibly
mixed alignment when a function has 8-word aligned locals but only
4-word aligned parameters, simply keep the stack frame 8-word aligned at
all times.
As for sizes, the temp def recycler was written before the Ruamoko ISA
was even a pipe dream and thus never expected temp def sizes over 4. At
least now any future adjustments can be done in one place.
My quick and dirty test program works :)
dvec4 xy = {1d, 2d, 0d, 0.5};
void printf(string fmt, ...) = #0;
int main()
{
dvec4 u = {3, 4, 3.14};
dvec4 v = {3, 4, 0, 1};
dvec4 w = v * xy + u;
printf ("[%g, %g, %g, %g]\n", w[0], w[1], w[2], w[3]);
return 0;
}
2022-02-03 23:46:58 +00:00
|
|
|
|
merge_spaces (space, func->locals->space, STACK_ALIGN);
|
2022-01-21 01:20:02 +00:00
|
|
|
|
func->locals->space = space;
|
|
|
|
|
|
2022-01-27 02:25:50 +00:00
|
|
|
|
// allocate 0 words to force alignment and get the address
|
[qfcc] Update sizes and alignments for dvec4 and friends
dvec4, lvec4 and ulvec4 need to be aligned to 8 words (32 bytes) in
order to avoid hardware exceptions. Rather than dealing with possibly
mixed alignment when a function has 8-word aligned locals but only
4-word aligned parameters, simply keep the stack frame 8-word aligned at
all times.
As for sizes, the temp def recycler was written before the Ruamoko ISA
was even a pipe dream and thus never expected temp def sizes over 4. At
least now any future adjustments can be done in one place.
My quick and dirty test program works :)
dvec4 xy = {1d, 2d, 0d, 0.5};
void printf(string fmt, ...) = #0;
int main()
{
dvec4 u = {3, 4, 3.14};
dvec4 v = {3, 4, 0, 1};
dvec4 w = v * xy + u;
printf ("[%g, %g, %g, %g]\n", w[0], w[1], w[2], w[3]);
return 0;
}
2022-02-03 23:46:58 +00:00
|
|
|
|
func->params_start = defspace_alloc_aligned_highwater (space, 0,
|
|
|
|
|
STACK_ALIGN);
|
2022-01-21 09:47:12 +00:00
|
|
|
|
|
|
|
|
|
dstatement_t *st = &pr.code->code[func->code];
|
2023-09-10 15:32:01 +00:00
|
|
|
|
if (pr.code->size > func->code && st->op == OP_ADJSTK) {
|
2022-02-05 11:36:38 +00:00
|
|
|
|
if (func->params_start) {
|
|
|
|
|
st->b = -func->params_start;
|
|
|
|
|
} else {
|
|
|
|
|
// skip over adjstk so a zero adjustment doesn't get executed
|
|
|
|
|
func->code += 1;
|
|
|
|
|
}
|
2022-01-21 09:47:12 +00:00
|
|
|
|
}
|
[qfcc] Update sizes and alignments for dvec4 and friends
dvec4, lvec4 and ulvec4 need to be aligned to 8 words (32 bytes) in
order to avoid hardware exceptions. Rather than dealing with possibly
mixed alignment when a function has 8-word aligned locals but only
4-word aligned parameters, simply keep the stack frame 8-word aligned at
all times.
As for sizes, the temp def recycler was written before the Ruamoko ISA
was even a pipe dream and thus never expected temp def sizes over 4. At
least now any future adjustments can be done in one place.
My quick and dirty test program works :)
dvec4 xy = {1d, 2d, 0d, 0.5};
void printf(string fmt, ...) = #0;
int main()
{
dvec4 u = {3, 4, 3.14};
dvec4 v = {3, 4, 0, 1};
dvec4 w = v * xy + u;
printf ("[%g, %g, %g, %g]\n", w[0], w[1], w[2], w[3]);
return 0;
}
2022-02-03 23:46:58 +00:00
|
|
|
|
merge_spaces (space, func->parameters->space, STACK_ALIGN);
|
2022-01-21 01:20:02 +00:00
|
|
|
|
func->parameters->space = space;
|
2022-01-27 02:25:50 +00:00
|
|
|
|
|
|
|
|
|
// force the alignment again so the full stack slot is counted when
|
[qfcc] Update sizes and alignments for dvec4 and friends
dvec4, lvec4 and ulvec4 need to be aligned to 8 words (32 bytes) in
order to avoid hardware exceptions. Rather than dealing with possibly
mixed alignment when a function has 8-word aligned locals but only
4-word aligned parameters, simply keep the stack frame 8-word aligned at
all times.
As for sizes, the temp def recycler was written before the Ruamoko ISA
was even a pipe dream and thus never expected temp def sizes over 4. At
least now any future adjustments can be done in one place.
My quick and dirty test program works :)
dvec4 xy = {1d, 2d, 0d, 0.5};
void printf(string fmt, ...) = #0;
int main()
{
dvec4 u = {3, 4, 3.14};
dvec4 v = {3, 4, 0, 1};
dvec4 w = v * xy + u;
printf ("[%g, %g, %g, %g]\n", w[0], w[1], w[2], w[3]);
return 0;
}
2022-02-03 23:46:58 +00:00
|
|
|
|
// the final parameter is smaller than STACK_ALIGN words
|
|
|
|
|
defspace_alloc_aligned_highwater (space, 0, STACK_ALIGN);
|
2022-01-21 01:20:02 +00:00
|
|
|
|
}
|
2024-08-26 06:50:27 +00:00
|
|
|
|
return fsym->metafunc->func;
|
2004-02-11 00:36:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-21 20:46:56 +00:00
|
|
|
|
function_t *
|
2023-09-27 03:41:31 +00:00
|
|
|
|
build_builtin_function (symbol_t *sym, const expr_t *bi_val, int far,
|
2021-09-24 10:49:55 +00:00
|
|
|
|
storage_class_t storage)
|
2002-05-17 18:35:54 +00:00
|
|
|
|
{
|
2011-01-18 03:37:12 +00:00
|
|
|
|
int bi;
|
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
|
|
|
|
}
|
2024-08-20 06:13:57 +00:00
|
|
|
|
if (!is_int_val (bi_val)
|
|
|
|
|
&& !(type_default != &type_int && 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
|
|
|
|
}
|
2024-08-27 05:37:18 +00:00
|
|
|
|
if (sym->metafunc->meta_type == mf_generic) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function_t *func = sym->metafunc->func;
|
|
|
|
|
if (func && func->def && func->def->initialized) {
|
|
|
|
|
error (bi_val, "%s redefined", sym->name);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-08-20 03:43:17 +00:00
|
|
|
|
|
|
|
|
|
defspace_t *space = far ? pr.far_data : sym->table->space;
|
2024-08-26 06:50:27 +00:00
|
|
|
|
func = make_function (sym, 0, space, storage);
|
2024-08-20 03:43:17 +00:00
|
|
|
|
|
2024-08-26 06:50:27 +00:00
|
|
|
|
if (func->def->external)
|
2011-01-26 05:48:22 +00:00
|
|
|
|
return 0;
|
2002-05-17 18:35:54 +00:00
|
|
|
|
|
2024-08-26 06:50:27 +00:00
|
|
|
|
func->def->initialized = 1;
|
|
|
|
|
func->def->constant = 1;
|
|
|
|
|
func->def->nosave = 1;
|
|
|
|
|
add_function (func);
|
2002-05-17 18:35:54 +00:00
|
|
|
|
|
2024-08-20 06:13:57 +00:00
|
|
|
|
if (is_int_val (bi_val)) {
|
2022-01-18 04:21:06 +00:00
|
|
|
|
bi = expr_int (bi_val);
|
2024-08-20 06:13:57 +00:00
|
|
|
|
} else {
|
2011-01-18 23:41:24 +00:00
|
|
|
|
bi = expr_float (bi_val);
|
2024-08-20 06:13:57 +00:00
|
|
|
|
if (bi != expr_float (bi_val)) {
|
|
|
|
|
error (bi_val, "invalid constant for = #");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-02 04:32:58 +00:00
|
|
|
|
if (bi < 0) {
|
|
|
|
|
error (bi_val, "builtin functions must be positive or 0");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-08-26 06:50:27 +00:00
|
|
|
|
func->builtin = bi;
|
|
|
|
|
reloc_def_func (func, func->def);
|
2020-02-18 16:51:24 +00:00
|
|
|
|
build_function (sym);
|
2011-01-17 13:33:33 +00:00
|
|
|
|
|
|
|
|
|
// for debug info
|
2011-03-10 11:16:35 +00:00
|
|
|
|
build_scope (sym, current_symtab);
|
2024-08-26 06:50:27 +00:00
|
|
|
|
func->parameters->space->size = 0;
|
|
|
|
|
func->locals->space = func->parameters->space;
|
|
|
|
|
return func;
|
2002-05-17 18:35:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-05-08 05:15:19 +00:00
|
|
|
|
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;
|
2023-11-06 04:41:17 +00:00
|
|
|
|
lineno_base = f->def->loc.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
|
|
|
|
|
2012-11-01 11:52:22 +00:00
|
|
|
|
void
|
|
|
|
|
clear_functions (void)
|
|
|
|
|
{
|
2024-08-19 09:29:44 +00:00
|
|
|
|
if (metafuncs) {
|
2024-08-10 05:36:52 +00:00
|
|
|
|
Hash_FlushTable (generic_functions);
|
2024-08-19 09:29:44 +00:00
|
|
|
|
Hash_FlushTable (metafuncs);
|
2012-11-01 11:52:22 +00:00
|
|
|
|
Hash_FlushTable (function_map);
|
2023-11-15 16:18:10 +00:00
|
|
|
|
} else {
|
2024-09-05 16:00:25 +00:00
|
|
|
|
setup_type_progs ();
|
2024-08-10 05:36:52 +00:00
|
|
|
|
generic_functions = Hash_NewTable (1021, gen_func_get_key, 0, 0, 0);
|
2024-08-20 03:43:17 +00:00
|
|
|
|
metafuncs = Hash_NewTable (1021, metafunc_get_full_name, 0, 0, 0);
|
|
|
|
|
function_map = Hash_NewTable (1021, metafunc_get_name, 0, 0, 0);
|
2023-11-15 16:18:10 +00:00
|
|
|
|
}
|
2012-11-01 11:52:22 +00:00
|
|
|
|
}
|
2024-10-01 07:51:49 +00:00
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
add_ctor_expr (const expr_t *expr)
|
|
|
|
|
{
|
|
|
|
|
if (!pr.ctor_exprs) {
|
|
|
|
|
pr.ctor_exprs = new_block_expr (nullptr);
|
|
|
|
|
}
|
|
|
|
|
append_expr (pr.ctor_exprs, expr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
emit_ctor (void)
|
|
|
|
|
{
|
|
|
|
|
if (!pr.ctor_exprs) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto ctor_sym = new_symbol_type (".ctor", &type_func);
|
|
|
|
|
ctor_sym = function_symbol ((specifier_t) { .sym = ctor_sym });
|
|
|
|
|
current_func = begin_function (ctor_sym, 0, current_symtab, 1, sc_static);
|
|
|
|
|
build_code_function (ctor_sym, 0, pr.ctor_exprs);
|
|
|
|
|
}
|