-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcompiler-internals.h
351 lines (296 loc) · 10.4 KB
/
compiler-internals.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* compiler-internals.h
*
* MathMap
*
* Copyright (C) 2002-2009 Mark Probst
*
* 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 the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __COMPILER_INTERNALS_H__
#define __COMPILER_INTERNALS_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "mathmap.h"
#include "vars.h"
#include "compiler.h"
#include "bitvector.h"
#include "opdefs.h"
#define CLOSURE_ARG(x) ((long)(x))
#define CLOSURE_GET(n,t) (t)(((long*)info)[(n)])
#define CLOSURE_VAR(t,name,n) t name = CLOSURE_GET((n),t)
struct _value_t;
typedef struct
{
int number;
int last_index;
} temporary_t;
#define MAX_PROMOTABLE_TYPE TYPE_COMPLEX
#define CONST_MAX (CONST_Y | CONST_X | CONST_T)
#define CONST_IGNORE 0x1000
typedef int type_t;
typedef struct _compvar_t
{
int index;
variable_t *var; /* 0 if compvar is a temporary */
temporary_t *temp; /* 0 if compvar is a variable */
int n; /* n/a if compvar is a temporary */
type_t type;
struct _value_t *current;
struct _value_t *values;
} compvar_t;
struct _statement_list_t;
struct _statement_t;
typedef struct _value_t
{
compvar_t *compvar;
int global_index;
int index; /* SSA index */
struct _statement_t *def;
struct _statement_list_t *uses;
unsigned int const_type : 3; /* defined in internals.h */
unsigned int least_const_type_directly_used_in : 3;
unsigned int least_const_type_multiply_used_in : 3;
unsigned int have_defined : 1; /* used in c code output */
struct _value_t *next; /* next value for same compvar */
} value_t;
/* is_rhs_const_primary() assumes that PRIMARY_VALUE is the only non-const
* primary type. */
#define PRIMARY_VALUE 1
#define PRIMARY_CONST 2
typedef struct
{
int kind;
int const_type; /* different meanings for PRIMARY_VALUE vs PRIMARY_CONST! */
union
{
value_t *value;
runtime_value_t constant;
} v;
} primary_t;
#define MAKE_CONST_PRIMARY(name, c_type, type_name) \
runtime_value_t \
make_ ## name ## _runtime_value (c_type name ## _value) \
{ \
runtime_value_t value; \
value.name ## _value = name ## _value; \
return value; \
} \
primary_t \
make_ ## name ## _const_primary (c_type name ## _const) \
{ \
primary_t primary; \
primary.kind = PRIMARY_CONST; \
primary.const_type = type_name; \
primary.v.constant.name ## _value = name ## _const; \
return primary; \
}
#define TYPE_PROP_CONST 1
#define TYPE_PROP_MAX 2
#define TYPE_PROP_MAX_FLOAT 3
typedef int type_prop_t;
typedef struct _operation_t
{
int index;
char *name;
int num_args;
type_prop_t type_prop;
int is_pure;
int is_foldable;
type_t const_type; /* used only if type_prop == TYPE_PROP_CONST */
type_t arg_types[MAX_OP_ARGS]; /* used only if type_prop == TYPE_PROP_CONST */
} operation_t;
typedef struct _inlining_history_t
{
filter_t *filter;
struct _inlining_history_t *next;
} inlining_history_t;
#define RHS_PRIMARY 1
#define RHS_INTERNAL 2
#define RHS_OP 3
#define RHS_FILTER 4
#define RHS_CLOSURE 5
#define RHS_TUPLE 6
#define RHS_TREE_VECTOR 7
typedef struct
{
int kind;
union
{
primary_t primary;
internal_t *internal;
struct
{
operation_t *op;
primary_t args[MAX_OP_ARGS];
} op;
struct
{
filter_t *filter;
primary_t *args;
inlining_history_t *history;
} filter;
struct
{
filter_t *filter;
primary_t *args;
inlining_history_t *history;
} closure;
struct
{
int length;
primary_t *args;
} tuple; /* also for tree vectors */
} v;
} rhs_t;
#define STMT_NIL 0
#define STMT_ASSIGN 1
#define STMT_PHI_ASSIGN 2
#define STMT_IF_COND 3
#define STMT_WHILE_LOOP 4
#define SLICE_XY_CONST 1
#define SLICE_X_CONST 2
#define SLICE_Y_CONST 4
#define SLICE_NO_CONST 8
#define SLICE_IGNORE 0x1000
typedef struct _statement_t
{
int kind;
union
{
struct
{
value_t *lhs;
rhs_t *rhs;
rhs_t *rhs2; /* only valid for STMT_PHI_ASSIGN */
value_t *old_value; /* only valid for STMT_PHI_ASSIGN */
} assign;
struct
{
rhs_t *condition;
struct _statement_t *consequent;
struct _statement_t *alternative;
struct _statement_t *exit;
} if_cond;
struct
{
struct _statement_t *entry;
rhs_t *invariant;
struct _statement_t *body;
} while_loop;
} v;
struct _statement_t *parent;
unsigned int slice_flags;
struct _statement_t *next;
} statement_t;
typedef struct _statement_list_t
{
statement_t *stmt;
struct _statement_list_t *next;
} statement_list_t;
typedef struct _filter_code_t
{
filter_t *filter;
statement_t *first_stmt;
} filter_code_t;
typedef struct
{
int userval_type;
int var_type;
int num_vars;
int getter_op;
} userval_representation_t;
#define BINDING_USERVAL 1
#define BINDING_INTERNAL 2
typedef struct _binding_values_t
{
int kind;
gpointer key;
struct _binding_values_t *next;
value_t *values[];
} binding_values_t;
typedef bit_vector_t value_set_t;
extern int compiler_op_index (operation_t *op);
extern compvar_t* make_temporary (type_t type);
#define compiler_make_temporary make_temporary
extern statement_t* make_assign (value_t *lhs, rhs_t *rhs);
#define compiler_make_assign make_assign
extern value_t* make_lhs (compvar_t *compvar);
#define compiler_make_lhs make_lhs
extern rhs_t* make_op_rhs (int op_index, ...);
#define compiler_make_op_rhs make_op_rhs
extern primary_t make_compvar_primary (compvar_t *compvar);
#define compiler_make_compvar_primary make_compvar_primary
extern rhs_t* make_primary_rhs (primary_t primary);
#define compiler_make_primary_rhs make_primary_rhs
extern rhs_t* make_value_rhs (value_t *val);
#define compiler_make_value_rhs make_value_rhs
rhs_t* compiler_make_internal_rhs (internal_t *internal);
extern void compiler_reset_have_defined (statement_t *stmt);
extern gboolean compiler_is_permanent_const_value (value_t *value);
extern gboolean compiler_is_temporary_const_value (value_t *value);
extern gboolean compiler_is_const_type_within (int const_type, int lower_bound, int upper_bound);
extern gboolean compiler_is_value_needed_for_const (value_t *value, int const_type);
extern char* compiler_get_value_name (value_t *val);
extern void compiler_print_value (value_t *val);
extern void compiler_print_assign_statement (statement_t *stmt);
extern int compiler_num_filter_args (filter_t *filter);
extern char* compiler_function_name_for_op_rhs (rhs_t *rhs, type_t *promotion_type);
extern statement_t** compiler_emit_stmt_before (statement_t *stmt, statement_t **loc, statement_t *parent);
extern gboolean compiler_rhs_is_pure (rhs_t *rhs);
extern gboolean compiler_stmt_is_assign_with_rhs (statement_t *stmt, int rhs_kind);
extern gboolean compiler_stmt_is_assign_with_op (statement_t *stmt, int op_index);
extern primary_t compiler_stmt_op_assign_arg (statement_t *stmt, int arg_index);
extern void compiler_remove_uses_in_rhs (rhs_t *rhs, statement_t *stmt);
extern void compiler_replace_rhs (rhs_t **rhs, rhs_t *replacement, statement_t *stmt);
extern void compiler_replace_op_rhs_arg (statement_t *stmt, int arg_num, primary_t replacement);
extern value_set_t* compiler_new_value_set (void);
extern void compiler_value_set_add (value_set_t *set, value_t *val);
extern void compiler_value_set_add_set (value_set_t *set, value_set_t *addee);
extern gboolean compiler_value_set_contains (value_set_t *set, value_t *val);
extern value_set_t* compiler_value_set_copy (value_set_t *set);
extern void compiler_free_value_set (value_set_t *set);
extern statement_t* compiler_stmt_unlink (statement_t **stmtp);
extern statement_t** compiler_stmt_insert_before (statement_t *stmt, statement_t **insertion_point);
extern void compiler_for_each_value_in_rhs (rhs_t *rhs, void (*func) (value_t *value, void *info),
void *info);
extern void compiler_for_each_value_in_statements (statement_t *stmt,
void (*func) (value_t *value, statement_t *stmt, void *info),
void *info);
extern void compiler_for_each_value_in_statement (statement_t *stmt,
void (*func) (value_t *value, statement_t *stmt, void *info),
void *info);
extern int compiler_slice_code (statement_t *stmt, unsigned int slice_flag,
int (*predicate) (statement_t *stmt, void *info), void *info);
extern filter_code_t* compiler_generate_ir_code (filter_t *filter, int constant_analysis,
int convert_types, int timeout, gboolean debug_output);
extern filter_code_t** compiler_compile_filters (mathmap_t *mathmap, int timeout);
extern void compiler_free_pools (mathmap_t *mathmap);
extern unsigned int compiler_slice_flag_for_const_type (int const_type);
extern void compiler_slice_code_for_const (statement_t *stmt, int const_type);
extern gboolean compiler_opt_remove_dead_assignments (statement_t *first_stmt);
extern gboolean compiler_opt_orig_val_resize (statement_t **first_stmt);
extern gboolean compiler_opt_strip_resize (statement_t **first_stmt);
extern gboolean compiler_opt_loop_invariant_code_motion (statement_t **first_stmt);
extern gboolean compiler_opt_simplify (filter_t *filter, statement_t *first_stmt);
#define COMPILER_FOR_EACH_VALUE_IN_RHS(rhs,func,...) do { long __clos[] = { __VA_ARGS__ }; compiler_for_each_value_in_rhs((rhs),(func),__clos); } while (0)
#define COMPILER_FOR_EACH_VALUE_IN_STATEMENTS(stmt,func,...) do { long __clos[] = { __VA_ARGS__ }; compiler_for_each_value_in_statements((stmt),(func),__clos); } while (0)
#define COMPILER_FOR_EACH_VALUE_IN_STATEMENT(stmt,func,...) do { long __clos[] = { __VA_ARGS__ }; compiler_for_each_value_in_statement((stmt),(func),__clos); } while (0)
#define COMPILER_SLICE_CODE(stmt,flag,func,...) do { long __clos[] = { __VA_ARGS__ }; compiler_slice_code((stmt),(flag),(func),__clos); } while (0)
#ifdef __cplusplus
}
#endif
#endif