1 /*
2  * Copyright (c) 2010-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
19  * \brief Include file for ILI to LLVM translation
20  */
21 
22 #ifndef CGLLVM_H__
23 #define CGLLVM_H__
24 
25 #include "llutil.h"
26 
27 void cprintf(char *s, const char *format, INT *val);
28 
29 #define SNAME(sptr) (sptrinfo.array.stg_base[sptr])
30 #define LLTYPE(sptr) (sptrinfo.type_array.stg_base[sptr])
31 #define LLTYPE_kind(sptr) (sptrinfo.type_array.stg_base[sptr]->kind)
32 #define LLTYPE_size(sptr) (sptrinfo.type_array.stg_base[sptr]->size)
33 
34 #define AGGREGATE_STYPE(s) \
35   ((s) == ST_STRUCT || (s) == ST_UNION || (s) == ST_ARRAY)
36 #define AGGREGATE_DTYPE(d) \
37   ((DTY(d)) == TY_STRUCT || (DTY(d)) == TY_UNION || (DTY(d)) == TY_ARRAY)
38 #define COMPLEX_DTYPE(d) ((DTY(d)) == TY_CMPLX || (DTY(d)) == TY_DCMPLX)
39 #define VECTOR_DTYPE(d) ((DTY(d)) == TY_VECT)
40 
41 #define LLCCF_NEG                                                         \
42   {                                                                       \
43     LLCCF_NONE, LLCCF_TRUE, LLCCF_UNE, LLCCF_ULE, LLCCF_ULT, LLCCF_UGE,   \
44         LLCCF_UGT, LLCCF_UNE, LLCCF_UNO, LLCCF_ONE, LLCCF_OLE, LLCCF_OLT, \
45         LLCCF_OGE, LLCCF_OGT, LLCCF_OEQ, LLCCF_ORD, LLCCF_FALSE           \
46   }
47 
48 /*  functions defined in cgmain.c file:  */
49 
50 void schedule(void);
51 void process_global_lifetime_debug(void);
52 OPERAND *gen_llvm_expr(int ilix, LL_Type *expected_type);
53 void clear_deletable_flags(int ilix);
54 INSTR_LIST *llvm_info_last_instr(void);
55 /* Use MSZ_TO_BYTES to detect presence of MSZ */
56 #ifdef MSZ_TO_BYTES
57 OPERAND *gen_address_operand(int, int, bool, LL_Type *, MSZ);
58 DTYPE msz_dtype(MSZ msz);
59 #endif
60 void update_external_function_declarations(const char *, char *, unsigned);
61 void cg_fetch_clen_parampos(SPTR *len, int *param, SPTR sptr);
62 
63 extern LL_Module *cpu_llvm_module;
64 #ifdef OMP_OFFLOAD_LLVM
65 extern LL_Module *gpu_llvm_module;
66 #endif
67 typedef enum STMT_Type {
68   STMT_NONE = 0,
69   STMT_RET = 1,
70   STMT_EXPR = 2,
71   STMT_LABEL = 3,
72   STMT_BR = 4,
73   STMT_ST = 5,
74   STMT_CALL = 6,
75   STMT_SMOVE = 7,
76   STMT_SZERO = 8,
77   STMT_DECL = 9,
78   STMT_LAST = 10
79 } STMT_Type;
80 
81 #define BITOP(i) ((i) >= I_SHL && (i) <= I_XOR)
82 #define BINOP(i) ((i) >= I_ADD && (i) <= I_FREM)
83 #define CONVERT(i) ((i) >= I_TRUNC && (i) <= I_BITCAST)
84 #define PICALL(i) ((i) == I_PICALL)
85 
86 #define CMP_FLT 0
87 #define CMP_INT 1
88 #define CMP_USG 2
89 
90 typedef enum {
91   MATCH_NO = -1,
92   MATCH_OK = 0,
93   MATCH_MEM = 1,
94   MATCH_LAST = 2
95 } MATCH_Kind;
96 
97 /* TMP flags */
98 #define CARRAY_TMP 1
99 
100 /* external declaration flags */
101 #define EXF_INTRINSIC 1
102 #define EXF_STRUCT_RETURN 2
103 #define EXF_VARARG 4
104 
105 #define IS_OLD_STYLE_CAND(s) (DEFDG(sptr) || CCSYMG(sptr))
106 
107 typedef struct{
108     STG_DECLARE(array, char*);
109     STG_DECLARE(type_array, LL_Type*);
110 }SPTRINFO_T;
111 
112 extern SPTRINFO_T sptrinfo;
113 
114 void cg_llvm_init(void);
115 void cg_llvm_end(void);
116 void cg_llvm_fnend(void);
117 void llvm_ctor_add(const char *);
118 void llvm_ctor_add_with_priority(const char *name, int priority);
119 void llvm_dtor_add(const char *);
120 void llvm_dtor_add_with_priority(const char *name, int priority);
121 void llvm_write_ctors(void);
122 
123 extern FILE *par_file1;
124 extern FILE *par_file2;
125 
126 void build_routine_and_parameter_entries(SPTR func_sptr, LL_ABI_Info *abi,
127                                          LL_Module *module);
128 bool strict_match(LL_Type *, LL_Type *);
129 bool is_cg_llvm_init(void);
130 void process_formal_arguments(LL_ABI_Info *);
131 void write_external_function_declarations(int);
132 
133 OPERAND *mk_alloca_instr(LL_Type *ptrTy);
134 INSTR_LIST *mk_store_instr(OPERAND *val, OPERAND *addr);
135 
136 #ifdef TARGET_LLVM_X8664
137 LL_Type *maybe_fixup_x86_abi_return(LL_Type *sig);
138 #endif
139 
140 #include "ll_ftn.h"
141 
142 #endif /* CGLLVM_H__ */
143