1 /*
2  * Copyright (c) 2015-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 Debug info generation for LLVM IR.
21  */
22 
23 #ifndef LLDEBUG_H_
24 #define LLDEBUG_H_
25 
26 #include "gbldefs.h"
27 #include "ll_structure.h"
28 #include "llutil.h"
29 
30 /**
31    \brief Create a metadata node for the current subprogram
32    \param db
33    \param sptr
34    \param ret_dtype
35    \param findex
36 
37    Side-effect: stores the metadata node in the LL_DebugInfo struct.
38 
39    A function pointer to the corresponding LLVM function must be set later by
40    lldbg_set_func_ptr().
41  */
42 void lldbg_emit_subprogram(LL_DebugInfo *db, SPTR sptr, DTYPE ret_dtype,
43                            int findex, bool targetNVVM);
44 
45 /**
46    \brief Create a metadata node for the outlined subprogram \p sptr
47    \param db
48    \param sptr
49    \param findex
50    \param func_name
51    \param startlineno
52 
53    Side-effect: store the metadata in the LL_DebugInfo struct.
54 
55 
56    A function pointer to the corresponding LLVM function must be set later by
57    lldbg_set_func_ptr().
58  */
59 void lldbg_emit_outlined_subprogram(LL_DebugInfo *db, int sptr, int findex,
60                                     const char *func_name, int startlineno,
61                                     bool targetNVVM);
62 
63 void lldbg_emit_cmblk_variables(LL_DebugInfo *, int, int, char *, int);
64 
65 struct INSTR_TAG;
66 
67 /// \brief Write out metadata definitions to the current LLVM file
68 void write_metadata_defs(LL_DebugInfo *db);
69 
70 /**
71    \brief ...
72  */
73 char *lldbg_alloc(INT size);
74 
75 /// \brief Encode an argument to lldbg_emit_expression_mdnode()
76 int lldbg_encode_expression_arg(LL_DW_OP_t op, int value);
77 
78 /**
79    \brief Always produce \c !dbg metadata for current location
80 
81    This produces location info even when none exists.
82  */
83 LL_MDRef lldbg_cons_line(LL_DebugInfo *db);
84 
85 /**
86    \brief Create a metadata node for the current compile unit
87    \param db
88 
89    This function is idempotent.
90  */
91 LL_MDRef lldbg_emit_compile_unit(LL_DebugInfo *db);
92 
93 /**
94    \brief Emit empty expression mdnode
95 
96    Metadata for \code{!DIExpression()}.
97  */
98 LL_MDRef lldbg_emit_empty_expression_mdnode(LL_DebugInfo *db);
99 
100 /**
101    \brief Emit expression mdnode
102    \param db   pointer to the debug info
103    \param cnt  the number of arguments to the expression
104 
105    Each argument to the DIExpression needs to be encoded using the function
106    lldbg_encode_expression_arg().
107 
108    Metadata for \code{!DIExpression(} \e op [, \e op ]* \code{)}
109  */
110 LL_MDRef lldbg_emit_expression_mdnode(LL_DebugInfo *db, unsigned cnt, ...);
111 
112 /**
113    \brief Emit a metadata node for a local variable in the current function
114    \return a reference to the variable
115 
116    The returned reference can be used as the last argument to \c
117    llvm.dbg.declare or \c llvm.dbg.value.
118  */
119 LL_MDRef lldbg_emit_local_variable(LL_DebugInfo *db, SPTR sptr, int findex,
120                                    int emit_dummy_as_local);
121 
122 /**
123    \brief ...
124  */
125 LL_MDRef lldbg_emit_module_mdnode(LL_DebugInfo *db, int sptr);
126 
127 /**
128    \brief Emit DILocalVariable for \p sptr parameter
129 
130    Emits a metadata node for a formal parameter to the current function.  The
131    returned reference can be used as the last argument to \c llvm.dbg.declare
132    or \c llvm.dbg.value.
133  */
134 LL_MDRef lldbg_emit_param_variable(LL_DebugInfo *db, SPTR sptr, int findex,
135                                    int parnum, bool unnamed);
136 
137 /**
138    \brief ...
139  */
140 LL_MDRef lldbg_emit_ptr_param_variable(LL_DebugInfo *db, SPTR sptr, int findex,
141                                        int parnum);
142 
143 /// \brief Get metadata node representing the current line for \c !dbg
144 LL_MDRef lldbg_get_line(LL_DebugInfo *db);
145 
146 /**
147    \brief Get the metadata node representing the line for a var definition
148    \param sptr  The variable to lookup
149  */
150 LL_MDRef lldbg_get_var_line(LL_DebugInfo *db, int sptr);
151 
152 /**
153    \brief Get the \c DISubprogram for the current procedure
154    \param db  the debug info object
155 
156    Note this has a side-effect: it clears the cached metadata.  This is to
157    prevent the next function from re-using this one's DISubprogram.
158  */
159 LL_MDRef lldbg_subprogram(LL_DebugInfo *db);
160 
161 /**
162    \brief The types of entity for debug metadata !DIImportedEntity
163  */
164 typedef enum import_entity_type {
165   IMPORTED_DECLARATION = 0,
166   IMPORTED_MODULE = 1,
167   IMPORTED_UNIT = 2
168 } IMPORT_TYPE;
169 
170 /**
171    \brief Emit DICommonBlock mdnode
172  */
173 LL_MDRef lldbg_emit_common_block_mdnode(LL_DebugInfo *db, SPTR sptr);
174 
175 /**
176    \brief ...
177  */
178 void lldbg_create_cmblk_mem_mdnode_list(SPTR sptr, SPTR gblsym);
179 
180 /**
181    \brief Add one symbol to the list of pending import entities.
182    \param db      the debug info object
183    \param entity  the symbol of module or variable to be imported
184    \param entity_type  0 indicates DECLARATION, 1 indicates MODULE,
185    2 indicates UNIT.
186  */
187 void lldbg_add_pending_import_entity(LL_DebugInfo *db, SPTR entity,
188                                      IMPORT_TYPE entity_type);
189 
190 /**
191    \brief Create a temporary debug name.
192  */
193 const char *new_debug_name(const char *str1, const char *str2, const char *str3);
194 
195 /**
196    \brief ...
197  */
198 void lldbg_cleanup_missing_bounds(LL_DebugInfo *db, int findex);
199 
200 /**
201    \brief ...
202  */
203 void lldbg_emit_accel_global_variable(LL_DebugInfo *db, SPTR sptr, int findex,
204                                       LL_Value *var_ptr, int addrspace,
205                                       int is_local);
206 
207 /**
208    \brief Emit a metadata node for a global variable.
209 
210    Note that all LLVM globals are referenced as pointers, so \p value should
211    have a pointer type.
212  */
213 void lldbg_emit_global_variable(LL_DebugInfo *db, SPTR sptr, BIGINT off,
214                                 int findex, LL_Value *value);
215 
216 /**
217    \brief ...
218  */
219 void lldbg_emit_line(LL_DebugInfo *db, int lineno);
220 
221 /**
222    \brief ...
223  */
224 void lldbg_emit_lv_list(LL_DebugInfo *db);
225 
226 /**
227    \brief ...
228  */
229 void lldbg_emit_outlined_parameter_list(LL_DebugInfo *db, int findex,
230                                         DTYPE *param_dtypes, int num_args);
231 
232 /**
233    \brief Free all memory used by \p db
234    \param db
235 
236    Don't call this directly, it is called from ll_destroy_module.
237  */
238 void lldbg_free(LL_DebugInfo *db);
239 
240 /**
241    \brief Construct debug information at end of routine
242    \param db    debug info instance
243    \param func  current function symbol
244  */
245 void lldbg_function_end(LL_DebugInfo *db, int func);
246 
247 /**
248    \brief Initialize dtype arrays
249    \param db
250  */
251 void lldbg_init_arrays(LL_DebugInfo *db);
252 
253 /**
254    \brief Allocate and initialize debug info generation for module
255    \param module
256  */
257 void lldbg_init(LL_Module *module);
258 
259 /**
260    \brief ...
261  */
262 void lldbg_register_value_call(LL_DebugInfo *db, INSTR_LIST *instr, int sptr);
263 
264 /**
265    \brief ...
266  */
267 void lldbg_reset_dtype_array(LL_DebugInfo *db, const int off);
268 
269 /// \brief Provide a function pointer to the curent subprogram
270 void lldbg_set_func_ptr(LL_DebugInfo *db, LL_Value *func_ptr);
271 
272 /**
273    \brief Make room for new dtypes
274    \param db         The debug info
275    \param lastDType  dtype from which to bzero when extended
276    \param newSz      the new size of dtype_array
277  */
278 void lldbg_update_arrays(LL_DebugInfo *db, int lastDType, int newSz);
279 
280 /// \brief Initialize the DIFLAG values
281 /// The values may vary depending on the LLVM branch being used
282 void InitializeDIFlags(const LL_IRFeatures *feature);
283 
284 void lldbg_reset_module(LL_DebugInfo *db);
285 
286 #endif /* LLDEBUG_H_ */
287