1 /*
2  * Copyright (c) 2015-2018, 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 #ifndef LL_BUILDER_H_
19 #define LL_BUILDER_H_
20 
21 /**
22    \file
23    \brief Convenience functions for constructing LLVM IR.
24  */
25 
26 #include "ll_structure.h"
27 #include "llutil.h"
28 
29 /*
30  * LLMD_Builder -- Builder for metadata nodes.
31  *
32  * AN LLMD_Builder can be used to construct a single LLVM metadata node by
33  * adding one element at a time. A set of llmd_add_* functions are provided for
34  * adding different types of elements to the node.
35  *
36  * The LLMD_Builder struct is opaque. Allocate one with llmd_init() and destroy
37  * it with llmd_finish().
38  */
39 typedef struct LLMD_Builder_ *LLMD_Builder;
40 
41 #define LL_MD_IS_NULL(MD) (!(MD))
42 
43 /* Create a builder for constructing a metadata node in module. The allocated
44  * memory must be freed by passing the returned builder to llmd_finish(). */
45 LLMD_Builder llmd_init(LL_Module *module);
46 
47 void llmd_add_null(LLMD_Builder);
48 void llmd_add_i1(LLMD_Builder, int value);
49 void llmd_add_i32(LLMD_Builder, int value);
50 void llmd_add_i64(LLMD_Builder, long long value);
51 
52 /* Add an i64 represented by two 32-bit numbers containing the least
53  * significant bits and the most significant bits. */
54 void llmd_add_i64_lsb_msb(LLMD_Builder, unsigned lsb, unsigned msb);
55 
56 /* Add an i64 represented as an DBLINT64 array */
57 void llmd_add_INT64(LLMD_Builder, DBLINT64 value);
58 
59 /* Add a nul-terminated metadata string. The string may contain any non-nul
60  * characters, escapes will be added. */
61 void llmd_add_string(LLMD_Builder, const char *value);
62 
63 /* Add a reference to another metadata item. */
64 void llmd_add_md(LLMD_Builder, LL_MDRef value);
65 
66 /* Add a general LL_Value reference. Note that the value must not be
67  * function-local. */
68 void llmd_add_value(LLMD_Builder, LL_Value *value);
69 
70 /* Reverse the list of node elements added so far. */
71 void llmd_reverse(LLMD_Builder);
72 
73 /* Get the number of elements added so far. */
74 unsigned llmd_get_nelems(LLMD_Builder);
75 
76 /* Request that a distinct metadata node is created so that it can be updated
77  * later with ll_update_md_node(). */
78 void llmd_set_distinct(LLMD_Builder);
79 
80 /* Set a metadata node class for the node being built. */
81 void llmd_set_class(LLMD_Builder, enum LL_MDClass);
82 
83 /* Return the built metadata node after freeing any resources used by the
84  * builder. */
85 LL_MDRef llmd_finish(LLMD_Builder);
86 
87 LL_MDRef ll_finish_variable(LLMD_Builder, LL_MDRef);
88 
89 /**
90    \brief ...
91  */
92 LLMD_Builder llmd_init(LL_Module *module);
93 
94 /**
95    \brief ...
96  */
97 LL_MDRef ll_finish_variable(LLMD_Builder mdb, LL_MDRef fwd);
98 
99 /**
100    \brief ...
101  */
102 LL_MDRef llmd_finish(LLMD_Builder mdb);
103 
104 /**
105    \brief ...
106  */
107 unsigned llmd_get_nelems(LLMD_Builder mdb);
108 
109 /**
110    \brief ...
111  */
112 void llmd_add_i1(LLMD_Builder mdb, int value);
113 
114 /**
115    \brief ...
116  */
117 void llmd_add_i32(LLMD_Builder mdb, int value);
118 
119 /**
120    \brief ...
121  */
122 void llmd_add_i64(LLMD_Builder mdb, long long value);
123 
124 /**
125    \brief ...
126  */
127 void llmd_add_i64_lsb_msb(LLMD_Builder mdb, unsigned lsb, unsigned msb);
128 
129 /**
130    \brief ...
131  */
132 void llmd_add_INT64(LLMD_Builder mdb, DBLINT64 value);
133 
134 /**
135    \brief ...
136  */
137 void llmd_add_md(LLMD_Builder mdb, LL_MDRef mdnode);
138 
139 /**
140    \brief ...
141  */
142 void llmd_add_null(LLMD_Builder mdb);
143 
144 /**
145    \brief ...
146  */
147 void llmd_add_string(LLMD_Builder mdb, const char *value);
148 
149 /**
150    \brief ...
151  */
152 void llmd_add_value(LLMD_Builder mdb, LL_Value *value);
153 
154 /**
155    \brief ...
156  */
157 void llmd_reverse(LLMD_Builder mdb);
158 
159 /**
160    \brief ...
161  */
162 void llmd_set_class(LLMD_Builder mdb, enum LL_MDClass mdclass);
163 
164 /**
165    \brief ...
166  */
167 void llmd_set_distinct(LLMD_Builder mdb);
168 
169 
170 #endif /* LL_BUILDER_H_ */
171