1 /*
2  * Copyright (c) 1994-2019, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 /**
19  *  \file
20  *  \brief ast.h - AST definitions for Fortran
21  */
22 
23 /* clang-format off */
24 .ST
25 
26 .SE
27 
28 /* overloaded macros accessing shared fields */
29 
30 #define A_STDG(s)   A_HSHLKG(s)
31 #define A_STDP(s,v) A_HSHLKP(s,v)
32 #define A_OPTYPEG(s)   A_hw21G(s)
33 #define A_OPTYPEP(s,v) A_hw21P(s,v)
34 #define A_TKNG(s)   A_hw21G(s)
35 #define A_TKNP(s,v) A_hw21P(s,v)
36 #define A_NTRIPLEG(s)   A_hw21G(s)
37 #define A_NTRIPLEP(s,v) A_hw21P(s,v)
38 #define A_SCHED_TYPEG(s)   A_hw21G(s)
39 #define A_SCHED_TYPEP(s,v) A_hw21P(s,v)
40 #define A_CANCELKINDG(s)   A_hw21G(s)
41 #define A_CANCELKINDP(s,v) A_hw21P(s,v)
42 #define A_ARGCNTG(s)   A_hw22G(s)
43 #define A_ARGCNTP(s,v) A_hw22P(s,v)
44 #define A_NCOUNTG(s)   A_hw22G(s)
45 #define A_NCOUNTP(s,v) A_hw22P(s,v)
46 #define A_ORDEREDG(s)   A_hw22G(s)
47 #define A_ORDEREDP(s,v) A_hw22P(s,v)
48 #define A_MEM_ORDERG(s)   A_hw22G(s)
49 #define A_MEM_ORDERP(s,v) A_hw22P(s,v)
50 
51 /* A_BINOP, A_UNOP OP_ macros; also used by semant */
52 
53 #define OP_NEG 0
54 #define OP_ADD 1
55 #define OP_SUB 2
56 #define OP_MUL 3
57 #define OP_DIV 4
58 #define OP_XTOI 5
59 #define OP_XTOX 6
60 #define OP_CMP 7
61 #define OP_AIF 8
62 #define OP_LD 9
63 #define OP_ST 10
64 #define OP_FUNC 11
65 #define OP_CON 12
66 #define OP_CAT 13
67 #define OP_LOG 14
68 #define OP_LEQV 15
69 #define OP_LNEQV 16
70 #define OP_LOR 17
71 #define OP_LAND 18
72 #define OP_EQ 19
73 #define OP_GE 20
74 #define OP_GT 21
75 #define OP_LE 22
76 #define OP_LT 23
77 #define OP_NE 24
78 #define OP_LNOT 25
79 /* remaining OP_ defines are not used by semant */
80 #define OP_LOC 26
81 #define OP_REF 27
82 #define OP_VAL 28
83 #define OP_SCAND 29
84 #define OP_SCALAR 30
85 #define OP_ARRAY 31
86 #define OP_DERIVED 32
87 #define OP_BYVAL 33
88 
89 /* AST attributes: for fast AST checking -- astb.attr is a table indexed
90  * by A_<type>
91  */
92 .TA
93 #define A_ISLVAL(a) (astb.attr[a]&__A_LVAL)
94 #define A_ISEXPR(a) (astb.attr[a]&__A_EXPR)
95 
96 /* miscellaneous macros */
97 
98 #define COMSTR(s) (astb.comstr.stg_base + A_COMPTRG(s))
99 
100 /*=================================================================*/
101 
102 /* ----- Auxiliary Structures ----- */
103 
104 /* AST List Item (ASTLI) */
105 
106 typedef struct {
107     int      h1;
108     int      h2;
109     int      flags;
110     int      next;
111 } ASTLI;
112 
113 #define ASTLI_HEAD astb.astli.stg_base[0].next
114 
115 #define ASTLI_SPTR(i) astb.astli.stg_base[i].h1
116 #define ASTLI_AST(i) astb.astli.stg_base[i].h1
117 
118 #define ASTLI_PT(i) astb.astli.stg_base[i].h2
119 #define ASTLI_TRIPLE(i) astb.astli.stg_base[i].h2
120 
121 #define ASTLI_FLAGS(i) astb.astli.stg_base[i].flags
122 #define ASTLI_NEXT(i) astb.astli.stg_base[i].next
123 
124 /* ARG Table */
125 
126 #define ARGT_CNT(i)  astb.argt.stg_base[i]
127 #define ARGT_ARG(i,j) astb.argt.stg_base[(i)+((j)+1)]
128 
129 /* Array Subscript Descriptor (ASD) */
130 
131 typedef struct {
132     int    ndim;	/* number of dimensions for this descriptor */
133     int    next;	/* next ASD with the same number of subscripts */
134     int    subs[1];	/* 1 <= size <= 7; 0 <= index <= 6 */
135 } ASD;
136 
137 #define ASD_NDIM(i) ((ASD *)&astb.asd.stg_base[i])->ndim
138 #define ASD_NEXT(i) ((ASD *)&astb.asd.stg_base[i])->next
139 #define ASD_SUBS(i,j) ((ASD *)&astb.asd.stg_base[i])->subs[j]
140 
141 /* Shape Descriptor (SHD) */
142 
143 typedef struct {
144     /* A shape descscriptor is composed of 'n+1' elements to represent
145      * a shape of rank 'n'.  The first element of a SHD is:
146      *     lwb    -> ndim, rank of the shape descriptor
147      *     upb    -> next, next SHD with the same number of subscripts
148      *     stride -> not used
149      * The ensuing 'n' elements describe the lower bound, upper bound, and
150      * stride for each dimension.
151      */
152     int    lwb;	/* ast of lower bound */
153     int    upb;	/* ast of upper bound */
154     int    stride;	/* ast of stride */
155 } SHD;
156 
157 #define SHD_NDIM(i) astb.shd.stg_base[i].lwb
158 #define SHD_NEXT(i) astb.shd.stg_base[i].upb
159 #define SHD_FILL(i) astb.shd.stg_base[i].stride
160 #define SHD_LWB(i,j) astb.shd.stg_base[i+j+1].lwb
161 #define SHD_UPB(i,j) astb.shd.stg_base[i+j+1].upb
162 #define SHD_STRIDE(i,j) astb.shd.stg_base[i+j+1].stride
163 
164 
165 /* Statement Descriptor (STD) */
166 
167 typedef struct {
168     int    ast;
169     int    next;
170     int    prev;
171     int    label;
172     int    lineno;
173     int    findex;
174     int    fg;		/* defined & referenced only by comm_post() */
175     SPTR   blksym;      /* do concurrent body block sym */
176 #ifdef PGF90
177     int    tag;		/* used for PFO */
178     int    pta;		/* pointer target information */
179     int    ptasgn;	/* pointer target pseudo-assignments */
180     int    astd;	/* std of paired set (e.g. allocate/deallocate) */
181     int    visit;	/* mark std after it is hoisted */
182 #endif
183     union {
184 	int  all;
185 	struct {
186 	    unsigned  ex:1;
187 	    unsigned  st:1;
188 	    unsigned  br:1;
189 	    unsigned  delete:1;
190 	    unsigned  ignore:1;	 /* used by hl vectorizer */
191 	    unsigned  split:1;	 /* split the loop here */
192 	    unsigned  minfo:1;	 /* stmt has 'minfo' */
193 	    unsigned  local:1;	 /* stmt will not cause communication */
194 	    unsigned  pure:1;	 /* stmt references a PURE subprogram */
195 	    unsigned  par:1;	 /* stmt belongs to a parallel region */
196 	    unsigned  cs:1;	 /* stmt belongs to a critical section */
197 	    unsigned  parsect:1; /* stmt belongs to a parallel section */
198 	    unsigned  orig:1;	 /* stmt was original user statement */
199 	    unsigned  mark:1;
200 	    unsigned  task:1;    /* stmt belongs to a task */
201 	    unsigned  accel:1;   /* stmt belongs to an accelerator region */
202 	    unsigned  kernel:1;  /* stmt belongs to an cuda kernel */
203 	    unsigned  atomic:1;  /* stmt belongs to an atomic */
204 	    unsigned  ztrip:1;   /* stmt marked for array assignment */
205 	    unsigned  rescope:1; /* stmt marked for kernels rescope */
206 	    unsigned  indivisible:1; /* stmt is in indivisible structure(s) */
207 	}  bits;
208     }  flags;
209 } STD;
210 
211 #define STD_AST(i)     astb.std.stg_base[i].ast
212 #define STD_NEXT(i)    astb.std.stg_base[i].next
213 #define STD_PREV(i)    astb.std.stg_base[i].prev
214 #define STD_LABEL(i)   astb.std.stg_base[i].label
215 #define STD_LINENO(i)  astb.std.stg_base[i].lineno
216 #define STD_FINDEX(i)  astb.std.stg_base[i].findex
217 #define STD_FG(i)      astb.std.stg_base[i].fg
218 #define STD_BLKSYM(i)  astb.std.stg_base[i].blksym
219 #define STD_FIRST      astb.std.stg_base[0].next
220 #define STD_LAST       astb.std.stg_base[0].prev
221 #ifdef PGF90
222 #define STD_TAG(i)     astb.std.stg_base[i].tag
223 #define STD_PTA(i)     astb.std.stg_base[i].pta
224 #define STD_PTASGN(i)  astb.std.stg_base[i].ptasgn
225 #define STD_HSTBLE(i)  astb.std.stg_base[i].astd
226 #define STD_VISIT(i)   astb.std.stg_base[i].visit
227 #endif
228 #define STD_FLAGS(i)   astb.std.stg_base[i].flags.all
229 #define STD_EX(i)      astb.std.stg_base[i].flags.bits.ex
230 #define STD_ST(i)      astb.std.stg_base[i].flags.bits.st
231 #define STD_BR(i)      astb.std.stg_base[i].flags.bits.br
232 #define STD_DELETE(i)  astb.std.stg_base[i].flags.bits.delete
233 #define STD_IGNORE(i)  astb.std.stg_base[i].flags.bits.ignore
234 #define STD_SPLIT(i)   astb.std.stg_base[i].flags.bits.split
235 #define STD_MOVED(i)   STD_SPLIT(i)
236 #define STD_MINFO(i)   astb.std.stg_base[i].flags.bits.minfo
237 #define STD_LOCAL(i)   astb.std.stg_base[i].flags.bits.local
238 #define STD_PURE(i)    astb.std.stg_base[i].flags.bits.pure
239 #define STD_PAR(i)     astb.std.stg_base[i].flags.bits.par
240 #define STD_CS(i)      astb.std.stg_base[i].flags.bits.cs
241 #define STD_PARSECT(i) astb.std.stg_base[i].flags.bits.parsect
242 #define STD_ORIG(i)    astb.std.stg_base[i].flags.bits.orig
243 #define STD_MARK(i)    astb.std.stg_base[i].flags.bits.mark
244 #define STD_TASK(i)    astb.std.stg_base[i].flags.bits.task
245 #define STD_ACCEL(i)   astb.std.stg_base[i].flags.bits.accel
246 #define STD_KERNEL(i)  astb.std.stg_base[i].flags.bits.kernel
247 #define STD_ATOMIC(i)  astb.std.stg_base[i].flags.bits.atomic
248 #define STD_ZTRIP(i)   astb.std.stg_base[i].flags.bits.ztrip
249 #define STD_RESCOPE(i)   astb.std.stg_base[i].flags.bits.rescope
250 #define STD_INDIVISIBLE(i)   astb.std.stg_base[i].flags.bits.indivisible
251 
252 
253 /*=================================================================*/
254 
255 /* hash table stuff */
256 #define HSHSZ 503
257 
258 /* limits */
259 #define MAXAST   67108864
260 
261 
262 /*  AST typedef declarations:  */
263 
264 typedef struct {
265     int16_t type;
266     uint8_t f1:1, f2:1, f3:1, f4:1, f5:1, f6:1, f7:1, f8:1;
267     uint8_t hw1:8;
268     int   hshlk;
269     int   w3;
270     int   w4;
271     int   w5;
272     int   w6;
273     int   w7;
274     int   w8;
275     int   w9;
276     int   w10;
277     uint16_t hw21;
278     uint16_t hw22;
279     int   w12;
280     int   opt1;
281     int   opt2;
282     int   repl;
283     int   visit;
284     int   shape;
285     int   w18;
286     int   w19;
287 }               AST;
288 
289 /*   AST data declaration:  */
290 
291 typedef struct {
292     char   *atypes[AST_MAX + 1];
293     int     attr[AST_MAX + 1];
294     int     hshtb[HSHSZ + 1];
295     STG_MEMBERS(AST);
296     int     firstuast;
297     int     i0;		/* 'predefined' ast for integer 0 */
298     int     i1;		/* 'predefined' ast for integer 1 */
299     int     k0;		/* 'predefined' ast for INT8 0 */
300     int     k1;		/* 'predefined' ast for INT8 1 */
301     int     ptr0;	/* 'predefined' ast for (void *)0 */
302     int     ptr1;	/* 'predefined' ast for (void *)1 */
303     int     ptr0c;	/* 'predefined' ast for non-present character I/O spec*/
304     struct {
305 	STG_MEMBERS(int);
306 	int     hash[7];  /* max # of dimensions */
307     } asd;
308     STG_DECLARE(std, STD);
309     STG_DECLARE(astli, ASTLI);
310     STG_DECLARE(argt, int);
311     struct {
312 	STG_MEMBERS(SHD);
313 	int     hash[7];  /* max # of dimensions */
314     } shd;
315     STG_DECLARE(comstr, char);
316     UINT16      implicit[55];  /* implicit dtypes:
317 				* [ 0-25] a-z    [26-51] A-Z
318 				*    [52] $         [53] _     [55] none
319 				*/
320     FILE   *astfil;	/* file pointer for (temporary) ast source file */
321     FILE   *df;		/* temp file for saving data initializations */
322     /*
323      * the following members are initialized to values which reflect the
324      * default type for the extents and subscripts of arrays.  The type could
325      * either be 32-int or 64-bit (BIGOBJects & -Mlarge_arrays).
326      *
327      */
328     struct {
329         int     zero;	/* 'predefined' ast for ISZ_T 0 */
330 	int     one;	/* 'predefined' ast for ISZ_T 1 */
331 	int     dtype;	/* dtype used for the bound temps */
332     } bnd;
333 }  ASTB;
334 
335 extern ASTB  astb;
336 extern int   intast_sym[];
337 
338 /** \brief Type of function passed to ast_traverse() and ast_traverse_all() */
339 typedef LOGICAL (*ast_preorder_fn) ();
340 
341 /** \brief Type of function passed to ast_visit(), ast_traverse() and ast_traverse_all() */
342 typedef void (*ast_visit_fn) ();
343 
344 /*   declare external functions from ast.c:  */
345 
346 void ast_init (void);
347 void ast_fini (void);
348 int new_node (int);
349 int mk_id (int);
350 int mk_id_noshape (int);
351 int mk_init (int, DTYPE);
352 int mk_cnst (int);
353 int mk_cval (INT, DTYPE);
354 int mk_isz_cval (ISZ_T, DTYPE);
355 int mk_cval1 (INT, DTYPE);
356 void mk_alias (int, int);
357 int mk_label (int);
358 int mk_binop (int, int, int, DTYPE);
359 int mk_unop (int, int, DTYPE);
360 int mk_cmplxc (int, int, DTYPE);
361 int mk_paren (int, DTYPE);
362 int mk_convert (int, DTYPE);
363 int convert_int (int, DTYPE);
364 int mk_promote_scalar (int, DTYPE, int);
365 int mk_subscr (int, int *, int, DTYPE);
366 int mk_subscr_copy (int, int, DTYPE);
367 int mk_asd(int *, int);
368 int mk_triple (int, int, int);
369 int mk_substr (int, int, int, DTYPE);
370 int complex_alias(int);
371 int mk_member (int, int, DTYPE);
372 int mkshape (DTYPE);
373 int mk_mem_ptr_shape (int, int, DTYPE);
374 int mk_shape (void);
375 int mk_atomic(int, int, int, DTYPE);
376 int reduc_shape (int, int, int);
377 int increase_shape (int, int, int, int);
378 void add_shape_rank (int);
379 void add_shape_spec (int, int, int);
380 LOGICAL conform_shape (int, int);
381 int extent_of_shape (int, int);
382 int lbound_of_shape (int, int);
383 int ubound_of_shape (int, int);
384 int rank_of_ast (int);
385 int mk_zbase_expr (ADSC *);
386 int mk_mlpyr_expr (int, int, int);
387 int mk_extent_expr (int, int);
388 int mk_extent (int, int, int);
389 int mk_offset (int, int);
390 int mk_shared_extent (int, int, int);
391 int sym_of_ast (int);
392 int memsym_of_ast (int);
393 int procsym_of_ast (int);
394 LOGICAL pure_func_call(int);
395 LOGICAL elemental_func_call(int);
396 int sptr_of_subscript (int);
397 int left_array_symbol (int);
398 int left_subscript_ast (int);
399 int left_nonscalar_subscript_ast (int);
400 int dist_symbol (int);
401 int dist_ast (int);
402 LOGICAL is_whole_dim(int arr_ast, int i);
403 LOGICAL is_whole_array(int arr_ast);
404 LOGICAL simply_contiguous(int arr_ast);
405 LOGICAL bnds_remap_list(int subscr_ast);
406 int replace_ast_subtree (int, int, int);
407 int elem_size_of_ast (int);
408 int size_of_ast (int);
409 int mk_bnd_ast (void);
410 int mk_shared_bnd_ast (int);
411 int mk_stmt (int, DTYPE);
412 int mk_std (int);
413 int add_stmt (int);
414 int add_stmt_after (int, int);
415 int add_stmt_before (int, int);
416 void insert_stmt_after(int std, int stdafter);
417 void insert_stmt_before(int std, int stdbefore);
418 void remove_stmt(int std);
419 void move_stmt_before(int std, int stdbefore);
420 void move_stmt_after(int std, int stdafter);
421 void move_stmts_before(int std, int stdbefore);
422 void move_stmts_after(int std, int stdafter);
423 void ast_to_comment (int);
424 int mk_comstr (char *str);
425 int mk_argt (int cnt);
426 void unmk_argt (int cnt);
427 void start_astli (void);
428 int add_astli (void);
429 void ast_implicit (int, int, DTYPE);
430 int begin_call (int, int, int);
431 void add_arg (int);
432 void finish_args (DTYPE, LOGICAL);
433 int mk_func_node (int, int, int, int);
434 int mk_assn_stmt (int, int, DTYPE);
435 LOGICAL contains_ast (int, int);
436 void ast_visit (int, int);
437 void ast_replace (int, int);
438 void ast_unvisit (void);
439 void ast_unvisit_norepl (void);
440 void ast_revisit (ast_visit_fn, int *);
441 int ast_rewrite (int);
442 void ast_clear_repl (int);
443 void ast_traverse (int, ast_preorder_fn, ast_visit_fn, int *);
444 void ast_traverse_all (int, ast_preorder_fn, ast_visit_fn, int *);
445 void ast_traverse_more (int, int *);
446 void ast_trav_recurse (int, int *);
447 void dump_one_ast (int);
448 void _dump_one_ast(int, FILE *);
449 void dump_ast_tree (int);
450 void dump_ast (void);
451 void dump_astli(int astli);
452 void dump_std (void);
453 void _dump_std (int, FILE *);
454 void dump_shape(int);
455 void _dump_shape(int, FILE *);
456 void dump_stg_stat (char *);
457 void dbg_print_ast(int ast, FILE *fil); /* astout.c */
458 
459 int ast_intr(int i_intr, DTYPE dtype, int cnt, ...);
460 int mk_default_int (int);
461 int mk_bnd_int (int);
462 int mk_smallest_val (DTYPE);
463 int mk_largest_val (DTYPE);
464 int mk_merge(int, int, int, DTYPE);
465 int get_atemp (void);
466 void set_atemp (int);
467 void delete_stmt (int);
468 int add_nullify_ast(int);
469 void printast(int);
470 int pass_sym_of_ast(int);
471 void end_param(void); /* astout.c */
472 void add_param(int); /*astout.c */
473 void astout_init(void); /* astout.c */
474 void put_memsym_of_ast(int ast, int sptr);
475 int replace_memsym_of_ast(int ast, SPTR sptr);
476 int has_assumshp_expr(int ast);
477 int has_adjustable_expr(int ast);
478 int has_pointer_expr(int ast);
479 int has_allocatable_expr(int ast);
480 int is_iso_cloc(int ast);
481 int is_iso_c_loc(int ast);
482 int is_iso_c_funloc(int ast);
483 int find_pointer_variable(int ast);
484 void find_pointer_target(int ast, int *pbase, int *psym);
485 void holtonum(char *cp, INT *num, int bc);
486 INT cngcon(INT oldval, int oldtyp, int newtyp);
487 INT negate_const(INT conval, DTYPE dtype);
488 INT const_fold(int opr, INT conval1, INT conval2, DTYPE dtype);
489 int resolve_ast_alias(int ast);
490 LOGICAL is_data_ast(int ast);
491 LOGICAL is_variable_ast(int ast);
492 LOGICAL is_array_ast(int ast);
493 LOGICAL has_vector_subscript_ast(int ast);
494 int get_ast_sptr(int ast);
495 int get_ast_asd(int ast);
496 DTYPE get_ast_dtype(int ast);
497 int get_ast_rank(int ast);
498 int rewrite_ast_with_new_dtype(int ast, DTYPE dtype);
499 int mk_duplicate_ast(int ast);
500 int get_ast_extents(int extent_asts[], int from_ast, DTYPE arr_dtype);
501 int get_ast_bounds(int lower_bound_asts[], int upper_bound_asts[],
502                    int from_ast, DTYPE arr_dtype);
503 int add_extent_subscripts(int to_ast, int rank, const int extent_asts[], DTYPE elt_dtype);
504 int add_bounds_subscripts(int to_ast, int rank, const int lower_bound_asts[],
505                           const int upper_bound_asts[], DTYPE elt_dtype);
506 int add_shapely_subscripts(int to_ast, int from_ast, DTYPE arr_dtype, DTYPE elt_dtype);
507 LOGICAL ast_is_sym(int ast);
508