2002-05-08 05:15:19 +00:00
|
|
|
/*
|
2002-10-22 14:53:18 +00:00
|
|
|
function.h
|
2002-05-08 05:15:19 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
QC function support code
|
2002-05-08 05:15:19 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
|
2002-05-08 05:15:19 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2002/05/08
|
2002-05-08 05:15:19 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __function_h
|
|
|
|
#define __function_h
|
|
|
|
|
2012-10-30 08:37:02 +00:00
|
|
|
/** \defgroup qfcc_function Internal function structures.
|
2012-12-04 03:48:52 +00:00
|
|
|
\ingroup qfcc
|
2012-10-30 08:37:02 +00:00
|
|
|
*/
|
2020-02-11 06:20:49 +00:00
|
|
|
///@{
|
2012-10-30 08:37:02 +00:00
|
|
|
|
2022-01-08 15:26:52 +00:00
|
|
|
#include "QF/progs/pr_comp.h"
|
|
|
|
#include "QF/progs/pr_debug.h"
|
2011-01-06 07:31:44 +00:00
|
|
|
|
|
|
|
#include "def.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
|
2022-02-14 23:39:20 +00:00
|
|
|
// The maximum size of a temp def, return value, or parameter value
|
|
|
|
#define MAX_DEF_SIZE 32
|
[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
|
|
|
|
2024-09-06 17:36:54 +00:00
|
|
|
typedef enum param_qual_e {
|
|
|
|
pq_const,
|
|
|
|
pq_in,
|
|
|
|
pq_out,
|
|
|
|
pq_inout,
|
|
|
|
} param_qual_t;
|
2024-07-07 12:36:40 +00:00
|
|
|
|
2024-09-06 17:36:54 +00:00
|
|
|
typedef struct function_s function_t;
|
|
|
|
typedef struct typeeval_s typeeval_t;
|
2024-07-07 12:36:40 +00:00
|
|
|
|
|
|
|
typedef struct gentype_s {
|
|
|
|
const char *name;
|
|
|
|
// earlier types have priority over later types. null if compute valid
|
|
|
|
const type_t **valid_types;
|
|
|
|
} gentype_t;
|
|
|
|
|
|
|
|
typedef struct genparam_s {
|
|
|
|
const char *name;
|
|
|
|
const type_t *fixed_type;
|
2024-09-06 17:36:54 +00:00
|
|
|
typeeval_t *compute;
|
2024-07-07 12:36:40 +00:00
|
|
|
int gentype; // index into function's list of types
|
2024-09-06 17:36:54 +00:00
|
|
|
param_qual_t qual;
|
2024-07-07 12:36:40 +00:00
|
|
|
} genparam_t;
|
|
|
|
|
|
|
|
typedef struct genfunc_s {
|
|
|
|
struct genfunc_s *next;
|
|
|
|
const char *name;
|
|
|
|
gentype_t *types;
|
|
|
|
int num_types;
|
|
|
|
int num_params;
|
|
|
|
genparam_t *params;
|
|
|
|
genparam_t *ret_type;
|
|
|
|
// only automatic (#0) builtins supported for now
|
|
|
|
} genfunc_t;
|
|
|
|
|
2024-08-19 09:29:44 +00:00
|
|
|
typedef enum {
|
|
|
|
mf_simple,
|
|
|
|
mf_overload,
|
|
|
|
mf_generic,
|
|
|
|
} mf_type_e;
|
|
|
|
|
2012-10-30 08:37:02 +00:00
|
|
|
/** Internal representation of a function.
|
|
|
|
*/
|
2002-06-04 18:44:03 +00:00
|
|
|
typedef struct function_s {
|
2002-06-21 20:46:56 +00:00
|
|
|
struct function_s *next;
|
2024-09-17 07:30:45 +00:00
|
|
|
const char *o_name;
|
2011-01-17 13:33:33 +00:00
|
|
|
int builtin; ///< if non 0, call an internal function
|
|
|
|
int code; ///< first statement
|
2024-11-12 09:24:40 +00:00
|
|
|
int id;
|
2002-06-21 20:46:56 +00:00
|
|
|
int function_num;
|
2011-03-07 04:33:02 +00:00
|
|
|
int line_info;
|
2022-01-27 02:25:50 +00:00
|
|
|
int params_start;///< relative to locals space. 0 for v6p
|
2022-01-18 03:11:14 +00:00
|
|
|
pr_string_t s_file; ///< source file with definition
|
|
|
|
pr_string_t s_name; ///< name of function in output
|
2024-05-12 03:48:50 +00:00
|
|
|
const type_t *type; ///< function's type without aliases
|
2022-01-21 11:34:43 +00:00
|
|
|
int temp_reg; ///< base register to use for temp defs
|
2011-02-09 01:04:55 +00:00
|
|
|
int temp_num; ///< number for next temp var
|
[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
|
|
|
struct def_s *temp_defs[MAX_DEF_SIZE];///< freed temp vars (by size)
|
2012-10-30 08:37:02 +00:00
|
|
|
struct def_s *def; ///< output def holding function number
|
2024-05-12 03:48:50 +00:00
|
|
|
symbol_t *sym; ///< internal symbol for this function
|
2022-01-21 01:20:02 +00:00
|
|
|
/** \name Local data space
|
|
|
|
|
|
|
|
The function parameters form the root scope for the function. Its
|
|
|
|
defspace is separate from the locals defspace so that it can be moved
|
|
|
|
to the beginning of locals space for v6 progs, and too the end (just
|
|
|
|
above the stack pointer on entry to the function) for Ruamoko progs.
|
|
|
|
|
|
|
|
The locals scope is a direct child of the parameters scope, and any
|
|
|
|
sub-scope symbol tables are not directly accessible, but all defs
|
|
|
|
other than function call arugments created in the function's local
|
|
|
|
data space are recorded in the root local scope symbol table's
|
|
|
|
defspace.
|
|
|
|
|
|
|
|
The arguments defspace is not used for v6 progs. It is used as a
|
|
|
|
highwater allocator for the arguments to all calls made by the
|
|
|
|
funciton, with the arguments to separate functions overlapping each
|
|
|
|
other.
|
|
|
|
|
|
|
|
Afther the function has been emitted, locals, arguments and possibly
|
|
|
|
parameters will be merged into the one defspace.
|
2012-10-30 08:37:02 +00:00
|
|
|
*/
|
2022-01-21 01:20:02 +00:00
|
|
|
///@{
|
2024-05-12 03:48:50 +00:00
|
|
|
symtab_t *parameters; ///< Root scope symbol table
|
|
|
|
symtab_t *locals; ///< Actual local variables
|
2022-01-21 01:20:02 +00:00
|
|
|
struct defspace_s *arguments; ///< Space for called function arguments
|
|
|
|
///@}
|
2024-05-12 03:48:50 +00:00
|
|
|
symtab_t *label_scope;
|
2012-10-30 08:37:02 +00:00
|
|
|
struct reloc_s *refs; ///< relocation targets for this function
|
2024-05-12 03:48:50 +00:00
|
|
|
expr_t *var_init;
|
2011-01-17 13:33:33 +00:00
|
|
|
const char *name; ///< nice name for __PRETTY_FUNCTION__
|
2012-11-02 08:30:29 +00:00
|
|
|
struct sblock_s *sblock; ///< initial node of function's code
|
|
|
|
struct flowgraph_s *graph; ///< the function's flow graph
|
2012-10-30 08:41:40 +00:00
|
|
|
/** Array of pointers to all variables referenced by the function's code.
|
|
|
|
|
|
|
|
This permits ready mapping of (function specific) variable number to
|
|
|
|
variable in the flow analyzer.
|
|
|
|
*/
|
2012-11-05 10:00:57 +00:00
|
|
|
struct flowvar_s **vars;
|
2012-10-30 08:41:40 +00:00
|
|
|
int num_vars; ///< total number of variables referenced
|
2012-11-19 08:21:35 +00:00
|
|
|
struct set_s *global_vars;///< set indicating which vars are global
|
2023-05-14 04:40:40 +00:00
|
|
|
struct set_s *param_vars; ///< set indicating which vars are params
|
2023-05-19 08:50:19 +00:00
|
|
|
struct set_s *real_statements;///< actual statements for ud-chaining
|
2012-11-05 06:21:40 +00:00
|
|
|
struct statement_s **statements;
|
|
|
|
int num_statements;
|
2023-05-19 15:59:23 +00:00
|
|
|
int num_ud_chains;
|
|
|
|
struct udchain_s *ud_chains;
|
2023-06-04 02:24:52 +00:00
|
|
|
struct udchain_s *du_chains;
|
2021-12-25 03:21:59 +00:00
|
|
|
int pseudo_addr;///< pseudo address space for flow analysis
|
2021-12-25 03:42:15 +00:00
|
|
|
struct pseudoop_s *pseudo_ops;///< pseudo operands used by this function
|
2024-11-03 14:45:54 +00:00
|
|
|
const expr_t *exprs;
|
2002-06-04 18:44:03 +00:00
|
|
|
} function_t;
|
|
|
|
|
2024-08-26 06:50:27 +00:00
|
|
|
/** Represent an overloading of a function.
|
|
|
|
|
|
|
|
Every function, whether overloaded or not, has a meta-function
|
|
|
|
*/
|
|
|
|
typedef struct metafunc_s {
|
|
|
|
struct metafunc_s *next;
|
|
|
|
const char *name; ///< source level name of function
|
|
|
|
const char *full_name; ///< progs name of function, with type
|
|
|
|
///< encoding
|
|
|
|
const type_t *type; ///< type of this function
|
|
|
|
rua_loc_t loc; ///< source location of the function
|
|
|
|
mf_type_e meta_type; ///< is this function overloaded
|
|
|
|
function_t *func;
|
|
|
|
} metafunc_t;
|
|
|
|
|
2002-06-04 18:44:03 +00:00
|
|
|
extern function_t *current_func;
|
|
|
|
|
2012-10-30 08:37:02 +00:00
|
|
|
/** Representation of a function parameter.
|
|
|
|
\note The first two fields match the first two fields of keywordarg_t
|
|
|
|
in method.h
|
|
|
|
*/
|
2002-05-08 05:15:19 +00:00
|
|
|
typedef struct param_s {
|
|
|
|
struct param_s *next;
|
|
|
|
const char *selector;
|
2024-05-01 01:58:01 +00:00
|
|
|
const type_t *type;
|
|
|
|
const expr_t *type_expr;
|
2019-06-08 10:22:29 +00:00
|
|
|
const char *name;
|
2024-09-03 08:59:56 +00:00
|
|
|
param_qual_t qual;
|
2002-05-08 05:15:19 +00:00
|
|
|
} param_t;
|
|
|
|
|
2024-05-01 01:58:01 +00:00
|
|
|
typedef struct expr_s expr_t;
|
|
|
|
typedef struct symbol_s symbol_t;
|
|
|
|
typedef struct symtab_s symtab_t;
|
2002-05-17 18:35:54 +00:00
|
|
|
|
2024-08-10 05:36:52 +00:00
|
|
|
void add_generic_function (genfunc_t *genfunc);
|
|
|
|
|
2024-05-12 03:48:50 +00:00
|
|
|
param_t *new_param (const char *selector, const type_t *type, const char *name);
|
2024-05-01 01:58:01 +00:00
|
|
|
param_t *new_generic_param (const expr_t *type_expr, const char *name);
|
2011-01-13 05:45:53 +00:00
|
|
|
param_t *param_append_identifiers (param_t *params, struct symbol_s *idents,
|
2024-05-12 03:48:50 +00:00
|
|
|
const type_t *type);
|
2002-05-08 17:33:28 +00:00
|
|
|
param_t *reverse_params (param_t *params);
|
2020-03-06 08:14:14 +00:00
|
|
|
param_t *append_params (param_t *params, param_t *more_params);
|
2002-08-20 02:09:34 +00:00
|
|
|
param_t *copy_params (param_t *params);
|
2024-05-12 03:48:50 +00:00
|
|
|
const type_t *parse_params (const type_t *return_type, param_t *params);
|
2023-02-11 11:23:23 +00:00
|
|
|
|
2011-02-01 12:18:08 +00:00
|
|
|
param_t *check_params (param_t *params);
|
2011-01-26 05:48:22 +00:00
|
|
|
|
2012-05-21 23:23:22 +00:00
|
|
|
enum storage_class_e;
|
|
|
|
struct defspace_s;
|
2024-05-12 03:48:50 +00:00
|
|
|
int value_too_large (const type_t *val_type) __attribute__((pure));
|
2024-08-26 06:50:27 +00:00
|
|
|
function_t *make_function (symbol_t *sym, const char *nice_name,
|
|
|
|
struct defspace_s *space,
|
|
|
|
enum storage_class_e storage);
|
2024-08-26 01:35:33 +00:00
|
|
|
symbol_t *function_symbol (specifier_t spec);
|
2024-05-12 03:48:50 +00:00
|
|
|
const expr_t *find_function (const expr_t *fexpr, const expr_t *params);
|
|
|
|
function_t *begin_function (symbol_t *sym, const char *nicename,
|
|
|
|
symtab_t *parent, int far,
|
2021-09-24 10:49:55 +00:00
|
|
|
enum storage_class_e storage);
|
2024-05-12 03:48:50 +00:00
|
|
|
function_t *build_code_function (symbol_t *fsym,
|
|
|
|
const expr_t *state_expr, expr_t *statements);
|
|
|
|
function_t *build_builtin_function (symbol_t *sym,
|
|
|
|
const expr_t *bi_val, int far,
|
2021-09-24 10:49:55 +00:00
|
|
|
enum storage_class_e storage);
|
2024-05-12 03:48:50 +00:00
|
|
|
void emit_function (function_t *f, expr_t *e);
|
2012-11-01 11:52:22 +00:00
|
|
|
void clear_functions (void);
|
2002-05-08 05:15:19 +00:00
|
|
|
|
2024-10-01 07:51:49 +00:00
|
|
|
void add_ctor_expr (const expr_t *expr);
|
|
|
|
void emit_ctor (void);
|
|
|
|
|
2020-02-11 06:20:49 +00:00
|
|
|
///@}
|
2012-12-05 07:01:28 +00:00
|
|
|
|
2002-05-08 05:15:19 +00:00
|
|
|
#endif//__function_h
|