1 /*
2  * Copyright (c) 1997-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 /** \file lower.h
19     \brief definitions for Fortran front-end's lower module
20 */
21 
22 /*
23  * Compatibility History:
24  * before 6.2  -- 1.9
25  * 6.2         -- 1.10
26  *                Includes all of 1.9 + PASSBYVAL & PASSBYREF
27  * 7.0         -- 1.11
28  *                Includes all of 1.10 + CFUNC for variables
29  * 7.1         -- 1.12
30  *                Includes all of 1.11 + DECORATE
31  * 7.2         -- 1.13
32  *                Includes all of 1.12 + CREF & NOMIXEDSTRLEN
33  * 8.0         -- 1.14
34  *                Includes all of 1.13 + FILE INDEX ENTRIES
35  * 8.1         -- 1.15
36  *                Includes all of 1.14 + new types + cuda flags
37  * 9.0-3       -- 1.16
38  *                Includes all of 1.15 + cudaemu value
39  * 10.6        -- 1.17
40  *                Includes all of 1.16 + sptr for Constant ID data init + denorm
41  * 10.9        -- 1.18
42  *                Includes all of 1.17 + reflected/mirrored/devcopy flags and
43  * devcopy field
44  * 11.0        -- 1.19
45  *                Includes all of 1.18 + mscall & cref for vars & members
46  * 11.4        -- 1.20
47  *                Includes all of 1.18 + libm & libc for functions
48  * 12.7        -- 1.21
49  *                Includes all of 1.20 + TASK for variables
50  * 12.7        -- 1.22
51  *                Includes all of 1.21 + cuda texture flag
52  * 12.7        -- 1.23
53  *                Includes all of 1.22 + INTENTIN flag
54  * 13.0        -- 1.24
55  *                Includes all of 1.23 + DATACNST flag
56  * 13.5        -- 1.25
57  *                Includes all of 1.24 + MODCMN flag
58  * 13.8        -- 1.26
59  *                Includes all of 1.25 + DOBEGNZ & DOENDNZ
60  * 13.9        -- 1.27
61  *                Includes all of 1.26 + symbol ACCCREATE and ACCRESIDENT
62  * 14.0        -- 1.28
63  *                Includes all of 1.27 + ACCROUT
64  * 14.4        -- 1.29
65  *                Includes all of 1.28 + CUDAMODULE
66  * 14.4        -- 1.30
67  *                Includes all of 1.29 + MANAGED + additionsl ILM operand
68  *                    for the call ILMs via a procedure ptr, e.g., CALLA,
69  *                    CDFUNCA, etc.
70  * 14.7        -- 1.31
71  *                All of 1.30 + ACCCREATE/ACCRESIDENT for common blocks,
72  *                    +ACCLINK+ACCCOPYIN flags
73  * 15.0        -- 1.32
74  *                All of 1.31 + new FARGF ILM
75  * 15.3        -- 1.33
76  *                All of 1.32 + FWDREF flag + INTERNREF flag + AGOTO field
77  * 15.4        -- 1.34
78  *                All of 1.33 + ST_MEMBER IFACE field
79  * 15.7        -- 1.35
80  *                All of 1.34 + ST_ENTRY/ST_PROC ARET field
81  * 15.9        -- 1.36
82  *                All of 1.35 + PARREF, PARSYMS, and PARSYMSCT field
83  * 15.10       -- 1.37
84  *                All of 1.36 + IM_BMPSCOPE/IM_EMPSCOPE
85  * 16.0        -- 1.38
86  *                All of 1.37 + IM_MPSCHED/IM_MPLOOP and
87  *                   IM_MMBORDERED/IM_MPEORDERED + TPALLOC + IM_FLUSH flag
88  * 16.4        -- 1.39
89  *                All of 1.38 + IM_ETASK and IM_TASKFIRSPRIV
90  * 16.5        -- 1.40
91  *                All of 1.39 + ISOTYPE flag + update IM_MPSCHED and IM_MPLOOP
92  * 16.6        -- 1.41
93  *                All of 1.40 + IM_LSECTION
94  * 16.6        -- 1.42
95  *                All of 1.41 + VARARG
96  * 16.8        -- 1.43
97  *                All of 1.42 + ALLOCATTR + F90POINTER
98  * 16.10       -- 1.44
99  *                All of 1.43 +
100  *IM_TASKGROUP/ETASKGROUP/TARGET/TARGETDATA/TARGETUPDATE/
101  *                TARGETEXITDATA/TARGETENTERDATA/DISTRIBUTE/TEAMS and their
102  *combinations
103  *		  TARGET/TEAMS/DISTRIBUTE/PARALLEL DO/CANCEL/CANCELLATIONPOINT
104  *                constructs
105  * 17.0        -- 1.45
106  *                All of 1.44 + INVOBJINC + PARREF for ST_PROC
107  * 17.2        -- 1.46
108  *                All of 1.45 + etls + tls, irrspective of target
109  * 17.7        -- 1.47
110  *                All of 1.46 + BPARA + PROC_BIND + MP_ATOMIC...
111  * 17.10        -- 1.48
112  *                All of 1.47 + ETASKFIRSTPRIV, MP_[E]TASKLOOP,
113  *                MP_[E]TASKLOOPREG
114  * 18.1         -- 1.49
115  *                All of 1.48 , MP_TASKLOOPVARS, [B/E]TASKDUP
116  * 18.4
117  *              -- 1.50
118  *                All of 1.49 +
119  *                Internal procedures passed as arguments and pointer targets
120  * 18.7         -- 1.51
121  *                All of 1.50 +
122  *                remove parsyms field and add parent for ST_BLOCK,
123  *                pass "has_opts" (no optional arguments) flag for ST_ENTRY and
124  *                ST_PROC symbols to back-end.
125  * 18.10        -- 1.52
126  *                All of 1.51 +
127  *                add IS_INTERFACE flag for ST_PROC, and for ST_MODULE when emitting
128  *                as ST_PROC
129  * 19.3         -- 1.53
130  *                All of 1.52 +
131  *                Add has_alias bit, and length and name of the alias for Fortran
132  *                module variable when it is on the ONLY list of a USE statement.
133  *                This is for Fortran LLVM compiler only.
134  */
135 #define VersionMajor 1
136 #define VersionMinor 53
137 
138 void lower(int);
139 void lower_end_contains(void);
140 void create_static_base(int blockname);
141 
142 #ifdef INSIDE_LOWER
143 #include <stdarg.h>
144 
145 #if DEBUG
146 #define Trace(a) LowerTraceOutput a
147 /* print a message, continue */
148 void LowerTraceOutput(const char *fmt, ...);
149 #else
150 /* eliminate the trace output */
151 #define Trace(a)
152 #endif
153 
154 void lerror(const char *fmt, ...);
155 void lower_visit_symbol(int sptr);
156 void lower_finish_sym(void);
157 void lower_use_datatype(int dtype, int usage);
158 void lower_data_types(void);
159 void lower_namelist_plists(void);
160 void lower_pointer_init(void);
161 void lower_push(int value);
162 void lower_check_stack(int);
163 void lower_linearized(void);
164 void lower_common_sizes(void);
165 int lower_pop(void);
166 int lower_getintcon(int val);
167 int lower_getiszcon(ISZ_T val);
168 int lower_getrealcon(int val);
169 int lower_getlogcon(int val);
170 int lower_newfunc(char *name, int stype, int dtype, int sclass);
171 void lower_add_pghpf_commons(void);
172 void lower_symbols(void);
173 void lower_clear_visit_fields(void);
174 void lower_set_symbols(void);
175 void lower_init_sym(void);
176 void lower_fill_member_parent(void);
177 void lower_sym_header(void);
178 void lower_fileinfo(void);
179 void lower_mark_entries(void);
180 int lower_makefunc(char *name, int dtype, LOGICAL isDscSafe);
181 int lower_lab(void);
182 void lower_check_generics(void);
183 
184 struct lower_syms {
185   int license, localmode, ptr0, ptr0c;
186   int intzero, intone, realzero, dblezero;
187   /* pointers for functions: loc, exit, allocate */
188   int loc, exit, alloc, alloc_chk, ptr_alloc, dealloc, dealloc_mbr, lmalloc,
189       lfree;
190   int calloc, ptr_calloc;
191   int auto_alloc, auto_calloc, auto_dealloc;
192   int oldsymavl, outersub, outerentries;
193   int ptrnull;
194 
195   int docount, labelcount, first_outer_sym, last_outer_sym_orig, last_outer_sym,
196       acount, Ccount;
197   int sym_lineno, last_lineno, sym_line_entry, sym_local, sym_save_local,
198       sym_function_entry, sym_function_exit, sym_exit, sym_function_name,
199       sym_file_name;
200   int sym_subchk, sym_ptrchk, sym_chkfile, intmax;
201   int sched_dtype;
202   int scheds_dtype;
203   int parallel_depth, task_depth, sc;
204   FILE *lowerfile;
205   /*
206    * The following members are initialized to values which reflect the
207    * default type for the extents and subscripts of arrays.  The type could
208    * either be 32-int or 64-bit (BIGOBJects & -Mlarge_arrays).
209    */
210   struct {
211     int zero;  /* Predefined sym for ISZ_T 0 (stb.i0 or stb.k0) */
212     int one;   /* Predefined sym for ISZ_T 1 (stb.i1 or stb.k1) */
213     int max;   /* Predefined sym for ISZ_T MAX */
214     int dtype; /* Type used for extents and subscripts. */
215     /* ilms for subscript operations (e.g., "ILD" or "KLD"): */
216     char *load;
217     char *store;
218     char *con;
219     char *add;
220     char *sub;
221     char *mul;
222     char *div;
223   } bnd;
224   struct {
225     int dtype;
226     int kput;
227     char *alloc;
228     char *calloc;
229     char *ptr_alloc;
230     char *ptr_calloc;
231   } allo;
232 };
233 extern struct lower_syms lowersym;
234 
235 extern struct ref_symbol dbgref_symbol;
236 
237 typedef struct {
238   int member_parent;   /* pointer from each 'member' to the 'parent' structure type symbol */
239   int symbol_replace;  /* When one symbol must be replaced by another, set its value here */
240   int pointer_list;    /* linked list of pointer or allocatable variables whose
241                         * pointer/offset/descriptors need to be initialized */
242   int refd_list;       /* linked list of pointer/offset/section descriptors in the order they
243                         * need to be given addresses */
244 } lower_symbol_lists;
245 STG_DECLARE(lsymlists, lower_symbol_lists);
246 #define LOWER_MEMBER_PARENT(x) lsymlists.stg_base[x].member_parent
247 #define LOWER_SYMBOL_REPLACE(x) lsymlists.stg_base[x].symbol_replace
248 #define LOWER_POINTER_LIST(x) lsymlists.stg_base[x].pointer_list
249 #define LOWER_REFD_LIST(x) lsymlists.stg_base[x].refd_list
250 
251 int *lower_argument;
252 int lower_argument_size;
253 int lower_line;
254 
255 /* only one of thenlabel and elselabel should be nonzero;
256  * the other is the 'fall through' case */
257 typedef struct {
258   int thenlabel, elselabel, endlabel;
259 } iflabeltype;
260 
261 int lower_disable_ptr_chk;
262 int lower_disable_subscr_chk;
263 
264 /* types of entries pushed onto the stack */
265 #define STKDO 1
266 #define STKIF 2
267 #define STKSINGLE 3
268 #define STKSECTION 4
269 #define STKMASTER 5
270 #define STKTASK 6
271 #define STKCANCEL 7
272 #define STKDDO 8
273 
274 void lower_ilm_header(void);
275 int plower(char *fmt, ...);
276 int plower_arg(char *, int, int, int);
277 void lower_start_stmt(int lineno, int label, LOGICAL exec, int std);
278 void lower_end_stmt(int std);
279 void lower_stmt(int std, int ast, int lineno, int label);
280 int lower_base(int ast);
281 int lower_base_sptr(int sptr);
282 int lower_address(int ast);
283 int lower_target(int ast);
284 int lower_ilm(int ast);
285 void lower_expression(int ast);
286 void lower_reinit(void);
287 void lower_exp_finish(void);
288 void lower_data_stmts(void);
289 void lower_debug_label(void);
290 void lower_ilm_finish(void);
291 
292 /* manage lower-created temporaries */
293 void lower_reset_temps(void);
294 int lower_scalar_temp(int);
295 
296 /* save spaces for ILM and BASE ILM info */
297 #define A_ILMP(ast, ilm) A_OPT1P(ast, ilm)
298 #define A_ILMG(ast) A_OPT1G(ast)
299 #define A_BASEP(ast, ext) A_OPT2P(ast, ext)
300 #define A_BASEG(ast) A_OPT2G(ast)
301 #if DEBUG
302 int lower_ndtypeg(int);
303 #define NDTYPE_IS_SET(ast) (astb.stg_base[ast].w19 > 0)
304 #undef A_NDTYPEG
305 #define A_NDTYPEG(ast) lower_ndtypeg(ast)
306 #else
307 #define NDTYPE_IS_SET(ast) (A_NDTYPEG(ast) > 0)
308 #endif
309 
310 int lower_conv(int ast, int dtype);
311 int lower_conv_ilm(int ast, int ilm, int fromdtype, int todtype);
312 int lower_null(void);
313 int lower_null_arg(void);
314 int lower_nullc_arg(void);
315 void lower_logical(int, iflabeltype *);
316 char *ltyped(char *opname, int dtype);
317 void ast_error(char *s, int ast);
318 void lower_clear_opt(int ast, int *unused);
319 int lower_parenthesize_expression(int ast);
320 int lower_typestore(int dtype, int lilm, int rilm);
321 int lower_typeload(int dtype, int ilm);
322 void lower_check_pointer(int ast, int ilm);
323 void lower_check_subscript(int sym, int ast, int ndim, int *ilm, int *lower,
324                            int *upper);
325 
326 void ccff_lower(FILE *lfile); /* ccffinfo.c */
327 
328 void uncouple_callee_args(void); /* lowersym.c */
329 void lower_unset_symbols(void);
330 void lower_outer_symbols(void);
331 void lower_set_craypointer(void);
332 void stb_fixup_llvmiface(void);
333 
334 void fill_entry_bounds(int sptr, int lineno); /* lowerilm.c */
335 int lower_replacement(int ast, int sym);
336 int lowersym_pghpf_cmem(int *whichmem);
337 
338 /*
339  *  The following are used to determine how to return bind(C) function retvals
340  * according to the ABI
341  */
342 /* Classes of by value arguments and C bind retvals */
343 #define CLASS_NONE 0
344 #define CLASS_INT1 1
345 #define CLASS_INT2 2
346 #define CLASS_INT3 3
347 #define CLASS_INT4 4
348 #define CLASS_INT5 5
349 #define CLASS_INT6 6
350 #define CLASS_INT7 7
351 #define CLASS_INT8 8
352 #define CLASS_SSESP4 9
353 #define CLASS_SSESP8 10
354 #define CLASS_SSEDP 11
355 #define CLASS_SSEQ 12
356 #define CLASS_MEM 13
357 #define CLASS_FSTK 14 // TODO: UNUSEDS delete
358 #define CLASS_PTR 15
359 
360 /* mostly used for small structs passed in regs stuff.*/
361 /* These values must be kept insync with the values in the BE file exp_rte.c */
362 #if defined(TARGET_WIN_X8664)
363 #define MAX_PASS_STRUCT_SIZE 8
364 #else
365 #define MAX_PASS_STRUCT_SIZE 16
366 #endif
367 
368 int check_return(int retdtype);
369 
370 #endif
371