llvm.org GIT mirror llvm / 46abf91
Added C and Ocaml bindings for functions, basic blocks, and instruction creation. No support yet for instruction introspection. Also eliminated allocas from the Ocaml bindings for portability, and avoided unnecessary casts. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42367 91177308-0d34-0410-b5e6-96231b3b80d8 Gordon Henriksen 11 years ago
11 changed file(s) with 2096 addition(s) and 242 deletion(s). Raw diff Collapse all Expand all
152152 9FA638EA0C77B252007F12AE /* InlinerPass.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = InlinerPass.h; sourceTree = ""; };
153153 9FA638EB0C77B26B007F12AE /* BasicInliner.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = BasicInliner.h; sourceTree = ""; };
154154 9FA638EC0C77B26B007F12AE /* InlineCost.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = InlineCost.h; sourceTree = ""; };
155 9FD3E5710CA0116100E54D15 /* bitwriter_ocaml.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; path = bitwriter_ocaml.c; sourceTree = ""; };
156 9FD3E5720CA0116100E54D15 /* llvm_bitwriter.ml */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = llvm_bitwriter.ml; sourceTree = ""; };
157 9FD3E5730CA0116100E54D15 /* llvm_bitwriter.mli */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = llvm_bitwriter.mli; sourceTree = ""; };
158 9FD3E57B0CA0116100E54D15 /* llvm.ml */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = llvm.ml; sourceTree = ""; };
159 9FD3E57C0CA0116100E54D15 /* llvm.mli */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = llvm.mli; sourceTree = ""; };
160 9FD3E57D0CA0116100E54D15 /* llvm_ocaml.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; path = llvm_ocaml.c; sourceTree = ""; };
161 9FD3E58D0CA0125F00E54D15 /* BitWriter.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = BitWriter.h; sourceTree = ""; };
162 9FD3E58E0CA0125F00E54D15 /* Core.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = Core.h; sourceTree = ""; };
163 9FD3E5900CA0129D00E54D15 /* Core.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = Core.cpp; path = ../lib/VMCore/Core.cpp; sourceTree = SOURCE_ROOT; };
164 9FD3E5920CA012B300E54D15 /* BitWriter.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = BitWriter.cpp; sourceTree = ""; };
155165 9FE4508B0C77A77000C4FEA4 /* ARMCodeEmitter.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = ARMCodeEmitter.cpp; sourceTree = ""; };
156166 9FE4508C0C77A77000C4FEA4 /* ARMGenAsmWriter.inc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.pascal; path = ARMGenAsmWriter.inc; sourceTree = ""; };
157167 9FE4508D0C77A77000C4FEA4 /* ARMGenDAGISel.inc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.pascal; path = ARMGenDAGISel.inc; sourceTree = ""; };
10481058 DE66F2BD08ABF14400323D32 /* tools */,
10491059 DE816FAC08CFB44C0093BDEF /* utils */,
10501060 DE66F38D08ABF35C00323D32 /* docs */,
1061 9FD3E56D0CA0116100E54D15 /* bindings */,
10511062 DE66F3FD08ABF37000323D32 /* examples */,
10521063 DE66F38C08ABF35300323D32 /* CREDITS.TXT */,
10531064 CFD99AA80AFE827B0068D19C /* LICENSE.TXT */,
10781089 9F68EB110C77AD2C004AA152 /* Writer */ = {
10791090 isa = PBXGroup;
10801091 children = (
1092 9FD3E5920CA012B300E54D15 /* BitWriter.cpp */,
10811093 9F68EB120C77AD2C004AA152 /* BitcodeWriter.cpp */,
10821094 9F68EB130C77AD2C004AA152 /* BitcodeWriterPass.cpp */,
10831095 9F68EB250C77AD2C004AA152 /* ValueEnumerator.cpp */,
11441156 path = IPO;
11451157 sourceTree = "";
11461158 };
1159 9FD3E56D0CA0116100E54D15 /* bindings */ = {
1160 isa = PBXGroup;
1161 children = (
1162 9FD3E56F0CA0116100E54D15 /* ocaml */,
1163 );
1164 name = bindings;
1165 path = ../bindings;
1166 sourceTree = SOURCE_ROOT;
1167 };
1168 9FD3E56F0CA0116100E54D15 /* ocaml */ = {
1169 isa = PBXGroup;
1170 children = (
1171 9FD3E5700CA0116100E54D15 /* bitwriter */,
1172 9FD3E57A0CA0116100E54D15 /* llvm */,
1173 );
1174 path = ocaml;
1175 sourceTree = "";
1176 };
1177 9FD3E5700CA0116100E54D15 /* bitwriter */ = {
1178 isa = PBXGroup;
1179 children = (
1180 9FD3E5710CA0116100E54D15 /* bitwriter_ocaml.c */,
1181 9FD3E5720CA0116100E54D15 /* llvm_bitwriter.ml */,
1182 9FD3E5730CA0116100E54D15 /* llvm_bitwriter.mli */,
1183 );
1184 path = bitwriter;
1185 sourceTree = "";
1186 };
1187 9FD3E57A0CA0116100E54D15 /* llvm */ = {
1188 isa = PBXGroup;
1189 children = (
1190 9FD3E57B0CA0116100E54D15 /* llvm.ml */,
1191 9FD3E57C0CA0116100E54D15 /* llvm.mli */,
1192 9FD3E57D0CA0116100E54D15 /* llvm_ocaml.c */,
1193 );
1194 path = llvm;
1195 sourceTree = "";
1196 };
11471197 9FE450DE0C77ABE400C4FEA4 /* lib/Archive */ = {
11481198 isa = PBXGroup;
11491199 children = (
11591209 CF8F1B480B64F7AB00BB4199 /* llvm-c */ = {
11601210 isa = PBXGroup;
11611211 children = (
1212 9FD3E58D0CA0125F00E54D15 /* BitWriter.h */,
1213 9FD3E58E0CA0125F00E54D15 /* Core.h */,
11621214 CF8F1B490B64F7AB00BB4199 /* LinkTimeOptimizer.h */,
11631215 );
11641216 name = "llvm-c";
12821334 9F77937C0C73C4F400551F9C /* ConstantFold.cpp */,
12831335 9F77937D0C73C4F400551F9C /* ConstantFold.h */,
12841336 DE66EC6008ABE86A00323D32 /* Constants.cpp */,
1337 9FD3E5900CA0129D00E54D15 /* Core.cpp */,
12851338 DE66EC6108ABE86A00323D32 /* Dominators.cpp */,
12861339 DE66EC6208ABE86A00323D32 /* Function.cpp */,
12871340 DE66EC6308ABE86A00323D32 /* Globals.cpp */,
9090 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
9191 $(OcamlDir)/.dir $(ObjDir)/.dir
9292 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
93
94 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
9395
9496 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
9597
1818 LIBRARYNAME := llvm_bitwriter
1919 DONT_BUILD_RELINKED := 1
2020 UsedComponents := bitwriter
21 UsedOcamlInterfaces := llvm
2122
2223 include ../Makefile.ocaml
1616 type llmodule
1717 type lltype
1818 type llvalue
19 type llbasicblock (* These are actually values, but
20 benefit from type checking. *)
21 type llbuilder
1922
2023 type type_kind =
2124 Void_type
4952 | Hidden_visibility
5053 | Protected_visibility
5154
55 let ccc = 0
56 let fastcc = 8
57 let coldcc = 9
58 let x86_stdcallcc = 64
59 let x86_fastcallcc = 65
60
61 type int_predicate =
62 Icmp_eq
63 | Icmp_ne
64 | Icmp_ugt
65 | Icmp_uge
66 | Icmp_ult
67 | Icmp_ule
68 | Icmp_sgt
69 | Icmp_sge
70 | Icmp_slt
71 | Icmp_sle
72
73 type real_predicate =
74 Fcmp_false
75 | Fcmp_oeq
76 | Fcmp_ogt
77 | Fcmp_oge
78 | Fcmp_olt
79 | Fcmp_ole
80 | Fcmp_one
81 | Fcmp_ord
82 | Fcmp_uno
83 | Fcmp_ueq
84 | Fcmp_ugt
85 | Fcmp_uge
86 | Fcmp_ult
87 | Fcmp_ule
88 | Fcmp_une
89 | Fcmp_true
90
5291
5392 (*===-- Modules -----------------------------------------------------------===*)
5493
62101
63102 (* Adds a named type to the module's symbol table. Returns true if successful.
64103 If such a name already exists, then no entry is added and returns false. *)
65 external add_type_name : string -> lltype -> llmodule -> bool
66 = "llvm_add_type_name"
104 external define_type_name : string -> lltype -> llmodule -> bool
105 = "llvm_add_type_name"
106
107 external delete_type_name : string -> llmodule -> unit
108 = "llvm_delete_type_name"
67109
68110
69111 (*===-- Types -------------------------------------------------------------===*)
141183
142184 (*--... Operations on constants of (mostly) any type .......................--*)
143185 external is_constant : llvalue -> bool = "llvm_is_constant"
144 external make_null : lltype -> llvalue = "llvm_make_null"
145 external make_all_ones : (*int|vec*)lltype -> llvalue = "llvm_make_all_ones"
146 external make_undef : lltype -> llvalue = "llvm_make_undef"
186 external make_null : lltype -> llvalue = "LLVMGetNull"
187 external make_all_ones : (*int|vec*)lltype -> llvalue = "LLVMGetAllOnes"
188 external make_undef : lltype -> llvalue = "LLVMGetUndef"
147189 external is_null : llvalue -> bool = "llvm_is_null"
148190 external is_undef : llvalue -> bool = "llvm_is_undef"
149191
182224 external define_global : string -> llvalue -> llmodule -> llvalue
183225 = "llvm_define_global"
184226 external delete_global : llvalue -> unit = "llvm_delete_global"
185 external global_initializer : llvalue -> llvalue = "llvm_global_initializer"
227 external global_initializer : llvalue -> llvalue = "LLVMGetInitializer"
186228 external set_initializer : llvalue -> llvalue -> unit = "llvm_set_initializer"
187229 external remove_initializer : llvalue -> unit = "llvm_remove_initializer"
188230 external is_thread_local : llvalue -> bool = "llvm_is_thread_local"
189231 external set_thread_local : bool -> llvalue -> unit = "llvm_set_thread_local"
232
233 (*--... Operations on functions ............................................--*)
234 external declare_function : string -> lltype -> llmodule -> llvalue
235 = "llvm_declare_function"
236 external define_function : string -> lltype -> llmodule -> llvalue
237 = "llvm_define_function"
238 external delete_function : llvalue -> unit = "llvm_delete_function"
239 external params : llvalue -> llvalue array = "llvm_params"
240 external param : llvalue -> int -> llvalue = "llvm_param"
241 external is_intrinsic : llvalue -> bool = "llvm_is_intrinsic"
242 external function_call_conv : llvalue -> int = "llvm_function_call_conv"
243 external set_function_call_conv : int -> llvalue -> unit
244 = "llvm_set_function_call_conv"
245
246 (* TODO: param attrs *)
247
248 (*--... Operations on basic blocks .........................................--*)
249 external basic_blocks : llvalue -> llbasicblock array = "llvm_basic_blocks"
250 external entry_block : llvalue -> llbasicblock = "LLVMGetEntryBasicBlock"
251 external delete_block : llbasicblock -> unit = "llvm_delete_block"
252 external append_block : string -> llvalue -> llbasicblock = "llvm_append_block"
253 external insert_block : string -> llbasicblock -> llbasicblock
254 = "llvm_insert_block"
255 external value_of_block : llbasicblock -> llvalue = "LLVMBasicBlockAsValue"
256 external value_is_block : llvalue -> bool = "llvm_value_is_block"
257 external block_of_value : llvalue -> llbasicblock = "LLVMValueAsBasicBlock"
258
259
260 (*===-- Instruction builders ----------------------------------------------===*)
261 external builder_before : llvalue -> llbuilder = "llvm_builder_before"
262 external builder_at_end : llbasicblock -> llbuilder = "llvm_builder_at_end"
263 external position_before : llvalue -> llbuilder -> unit = "llvm_position_before"
264 external position_at_end : llbasicblock -> llbuilder -> unit
265 = "llvm_position_at_end"
266
267 (*--... Terminators ........................................................--*)
268 external build_ret_void : llbuilder -> llvalue = "llvm_build_ret_void"
269 external build_ret : llvalue -> llbuilder -> llvalue = "llvm_build_ret"
270 external build_br : llbasicblock -> llbuilder -> llvalue = "llvm_build_br"
271 external build_cond_br : llvalue -> llbasicblock -> llbasicblock -> llbuilder ->
272 llvalue = "llvm_build_cond_br"
273 external build_switch : llvalue -> llbasicblock -> int -> llbuilder -> llvalue
274 = "llvm_build_switch"
275 external build_invoke : llvalue -> llvalue array -> llbasicblock ->
276 llbasicblock -> string -> llbuilder -> llvalue
277 = "llvm_build_invoke_bc" "llvm_build_invoke_nat"
278 external build_unwind : llbuilder -> llvalue = "llvm_build_unwind"
279 external build_unreachable : llbuilder -> llvalue = "llvm_build_unreachable"
280
281 (*--... Arithmetic .........................................................--*)
282 external build_add : llvalue -> llvalue -> string -> llbuilder -> llvalue
283 = "llvm_build_add"
284 external build_sub : llvalue -> llvalue -> string -> llbuilder -> llvalue
285 = "llvm_build_sub"
286 external build_mul : llvalue -> llvalue -> string -> llbuilder -> llvalue
287 = "llvm_build_mul"
288 external build_udiv : llvalue -> llvalue -> string -> llbuilder -> llvalue
289 = "llvm_build_udiv"
290 external build_sdiv : llvalue -> llvalue -> string -> llbuilder -> llvalue
291 = "llvm_build_sdiv"
292 external build_fdiv : llvalue -> llvalue -> string -> llbuilder -> llvalue
293 = "llvm_build_fdiv"
294 external build_urem : llvalue -> llvalue -> string -> llbuilder -> llvalue
295 = "llvm_build_urem"
296 external build_srem : llvalue -> llvalue -> string -> llbuilder -> llvalue
297 = "llvm_build_srem"
298 external build_frem : llvalue -> llvalue -> string -> llbuilder -> llvalue
299 = "llvm_build_frem"
300 external build_shl : llvalue -> llvalue -> string -> llbuilder -> llvalue
301 = "llvm_build_shl"
302 external build_lshr : llvalue -> llvalue -> string -> llbuilder -> llvalue
303 = "llvm_build_lshr"
304 external build_ashr : llvalue -> llvalue -> string -> llbuilder -> llvalue
305 = "llvm_build_ashr"
306 external build_and : llvalue -> llvalue -> string -> llbuilder -> llvalue
307 = "llvm_build_and"
308 external build_or : llvalue -> llvalue -> string -> llbuilder -> llvalue
309 = "llvm_build_or"
310 external build_xor : llvalue -> llvalue -> string -> llbuilder -> llvalue
311 = "llvm_build_xor"
312 external build_neg : llvalue -> string -> llbuilder -> llvalue
313 = "llvm_build_neg"
314 external build_not : llvalue -> string -> llbuilder -> llvalue
315 = "llvm_build_not"
316
317 (*--... Memory .............................................................--*)
318 external build_malloc : lltype -> string -> llbuilder -> llvalue
319 = "llvm_build_malloc"
320 external build_array_malloc : lltype -> llvalue -> string -> llbuilder ->
321 llvalue = "llvm_build_array_malloc"
322 external build_alloca : lltype -> string -> llbuilder -> llvalue
323 = "llvm_build_alloca"
324 external build_array_alloca : lltype -> llvalue -> string -> llbuilder ->
325 llvalue = "llvm_build_array_alloca"
326 external build_free : llvalue -> llbuilder -> llvalue = "llvm_build_free"
327 external build_load : llvalue -> string -> llbuilder -> llvalue
328 = "llvm_build_load"
329 external build_store : llvalue -> llvalue -> llbuilder -> llvalue
330 = "llvm_build_store"
331 external build_gep : llvalue -> llvalue array -> string -> llbuilder -> llvalue
332 = "llvm_build_gep"
333
334 (*--... Casts ..............................................................--*)
335 external build_trunc : llvalue -> lltype -> string -> llbuilder -> llvalue
336 = "llvm_build_trunc"
337 external build_zext : llvalue -> lltype -> string -> llbuilder -> llvalue
338 = "llvm_build_zext"
339 external build_sext : llvalue -> lltype -> string -> llbuilder -> llvalue
340 = "llvm_build_sext"
341 external build_fptoui : llvalue -> lltype -> string -> llbuilder -> llvalue
342 = "llvm_build_fptoui"
343 external build_fptosi : llvalue -> lltype -> string -> llbuilder -> llvalue
344 = "llvm_build_fptosi"
345 external build_uitofp : llvalue -> lltype -> string -> llbuilder -> llvalue
346 = "llvm_build_uitofp"
347 external build_sitofp : llvalue -> lltype -> string -> llbuilder -> llvalue
348 = "llvm_build_sitofp"
349 external build_fptrunc : llvalue -> lltype -> string -> llbuilder -> llvalue
350 = "llvm_build_fptrunc"
351 external build_fpext : llvalue -> lltype -> string -> llbuilder -> llvalue
352 = "llvm_build_fpext"
353 external build_ptrtoint : llvalue -> lltype -> string -> llbuilder -> llvalue
354 = "llvm_build_prttoint"
355 external build_inttoptr : llvalue -> lltype -> string -> llbuilder -> llvalue
356 = "llvm_build_inttoptr"
357 external build_bitcast : llvalue -> lltype -> string -> llbuilder -> llvalue
358 = "llvm_build_bitcast"
359
360 (*--... Comparisons ........................................................--*)
361 external build_icmp : int_predicate -> llvalue -> llvalue -> string ->
362 llbuilder -> llvalue = "llvm_build_icmp"
363 external build_fcmp : real_predicate -> llvalue -> llvalue -> string ->
364 llbuilder -> llvalue = "llvm_build_fcmp"
365
366 (*--... Miscellaneous instructions .........................................--*)
367 external build_phi : lltype -> string -> llbuilder -> llvalue = "llvm_build_phi"
368 external build_call : llvalue -> llvalue array -> string -> llbuilder -> llvalue
369 = "llvm_build_call"
370 external build_select : llvalue -> llvalue -> llvalue -> string -> llbuilder ->
371 llvalue = "llvm_build_select"
372 external build_va_arg : llvalue -> lltype -> string -> llbuilder -> llvalue
373 = "llvm_build_va_arg"
374 external build_extractelement : llvalue -> llvalue -> string -> llbuilder ->
375 llvalue = "llvm_build_extractelement"
376 external build_insertelement : llvalue -> llvalue -> llvalue -> string ->
377 llbuilder -> llvalue = "llvm_build_insertelement"
378 external build_shufflevector : llvalue -> llvalue -> llvalue -> string ->
379 llbuilder -> llvalue = "llvm_build_shufflevector"
190380
191381
192382 (*===-- Non-Externs -------------------------------------------------------===*)
1616 type llmodule
1717 type lltype
1818 type llvalue
19 type llbasicblock (* These are actually values, but
20 benefit from type checking. *)
21 type llbuilder
1922
2023 type type_kind =
2124 Void_type
4952 | Hidden_visibility
5053 | Protected_visibility
5154
55 val ccc : int
56 val fastcc : int
57 val coldcc : int
58 val x86_stdcallcc : int
59 val x86_fastcallcc : int
60
61 type int_predicate =
62 Icmp_eq
63 | Icmp_ne
64 | Icmp_ugt
65 | Icmp_uge
66 | Icmp_ult
67 | Icmp_ule
68 | Icmp_sgt
69 | Icmp_sge
70 | Icmp_slt
71 | Icmp_sle
72
73 type real_predicate =
74 Fcmp_false
75 | Fcmp_oeq
76 | Fcmp_ogt
77 | Fcmp_oge
78 | Fcmp_olt
79 | Fcmp_ole
80 | Fcmp_one
81 | Fcmp_ord
82 | Fcmp_uno
83 | Fcmp_ueq
84 | Fcmp_ugt
85 | Fcmp_uge
86 | Fcmp_ult
87 | Fcmp_ule
88 | Fcmp_une
89 | Fcmp_true
90
5291
5392 (*===-- Modules -----------------------------------------------------------===*)
5493
62101
63102 (* Adds a named type to the module's symbol table. Returns true if successful.
64103 If such a name already exists, then no entry is added and returns false. *)
65 external add_type_name : string -> lltype -> llmodule -> bool
66 = "llvm_add_type_name"
104 external define_type_name : string -> lltype -> llmodule -> bool
105 = "llvm_add_type_name"
106
107 external delete_type_name : string -> llmodule -> unit
108 = "llvm_delete_type_name"
67109
68110
69111 (*===-- Types -------------------------------------------------------------===*)
124166
125167 (*--... Operations on constants of (mostly) any type .......................--*)
126168 external is_constant : llvalue -> bool = "llvm_is_constant"
127 external make_null : lltype -> llvalue = "llvm_make_null"
128 external make_all_ones : (*int|vec*)lltype -> llvalue = "llvm_make_all_ones"
129 external make_undef : lltype -> llvalue = "llvm_make_undef"
169 external make_null : lltype -> llvalue = "LLVMGetNull"
170 external make_all_ones : (*int|vec*)lltype -> llvalue = "LLVMGetAllOnes"
171 external make_undef : lltype -> llvalue = "LLVMGetUndef"
130172 external is_null : llvalue -> bool = "llvm_is_null"
131173 external is_undef : llvalue -> bool = "llvm_is_undef"
132174
165207 external define_global : string -> llvalue -> llmodule -> llvalue
166208 = "llvm_define_global"
167209 external delete_global : llvalue -> unit = "llvm_delete_global"
168 external global_initializer : llvalue -> llvalue = "llvm_global_initializer"
210 external global_initializer : llvalue -> llvalue = "LLVMGetInitializer"
169211 external set_initializer : llvalue -> llvalue -> unit = "llvm_set_initializer"
170212 external remove_initializer : llvalue -> unit = "llvm_remove_initializer"
171213 external is_thread_local : llvalue -> bool = "llvm_is_thread_local"
172214 external set_thread_local : bool -> llvalue -> unit = "llvm_set_thread_local"
173215
216 (*--... Operations on functions ............................................--*)
217 external declare_function : string -> lltype -> llmodule -> llvalue
218 = "llvm_declare_function"
219 external define_function : string -> lltype -> llmodule -> llvalue
220 = "llvm_define_function"
221 external delete_function : llvalue -> unit = "llvm_delete_function"
222 external params : llvalue -> llvalue array = "llvm_params"
223 external param : llvalue -> int -> llvalue = "llvm_param"
224 external is_intrinsic : llvalue -> bool = "llvm_is_intrinsic"
225 external function_call_conv : llvalue -> int = "llvm_function_call_conv"
226 external set_function_call_conv : int -> llvalue -> unit
227 = "llvm_set_function_call_conv"
228
229 (*--... Operations on basic blocks .........................................--*)
230 external basic_blocks : llvalue -> llbasicblock array = "llvm_basic_blocks"
231 external entry_block : llvalue -> llbasicblock = "LLVMGetEntryBasicBlock"
232 external delete_block : llbasicblock -> unit = "llvm_delete_block"
233 external append_block : string -> llvalue -> llbasicblock = "llvm_append_block"
234 external insert_block : string -> llbasicblock -> llbasicblock
235 = "llvm_insert_block"
236 external value_of_block : llbasicblock -> llvalue = "LLVMBasicBlockAsValue"
237 external value_is_block : llvalue -> bool = "llvm_value_is_block"
238 external block_of_value : llvalue -> llbasicblock = "LLVMValueAsBasicBlock"
239
240
241 (*===-- Instruction builders ----------------------------------------------===*)
242 external builder_before : llvalue -> llbuilder = "llvm_builder_before"
243 external builder_at_end : llbasicblock -> llbuilder = "llvm_builder_at_end"
244 external position_before : llvalue -> llbuilder -> unit = "llvm_position_before"
245 external position_at_end : llbasicblock -> llbuilder -> unit
246 = "llvm_position_at_end"
247
248 (*--... Terminators ........................................................--*)
249 external build_ret_void : llbuilder -> llvalue = "llvm_build_ret_void"
250 external build_ret : llvalue -> llbuilder -> llvalue = "llvm_build_ret"
251 external build_br : llbasicblock -> llbuilder -> llvalue = "llvm_build_br"
252 external build_cond_br : llvalue -> llbasicblock -> llbasicblock -> llbuilder ->
253 llvalue = "llvm_build_cond_br"
254 external build_switch : llvalue -> llbasicblock -> int -> llbuilder -> llvalue
255 = "llvm_build_switch"
256 external build_invoke : llvalue -> llvalue array -> llbasicblock ->
257 llbasicblock -> string -> llbuilder -> llvalue
258 = "llvm_build_invoke_bc" "llvm_build_invoke_nat"
259 external build_unwind : llbuilder -> llvalue = "llvm_build_unwind"
260 external build_unreachable : llbuilder -> llvalue = "llvm_build_unreachable"
261
262 (*--... Arithmetic .........................................................--*)
263 external build_add : llvalue -> llvalue -> string -> llbuilder -> llvalue
264 = "llvm_build_add"
265 external build_sub : llvalue -> llvalue -> string -> llbuilder -> llvalue
266 = "llvm_build_sub"
267 external build_mul : llvalue -> llvalue -> string -> llbuilder -> llvalue
268 = "llvm_build_mul"
269 external build_udiv : llvalue -> llvalue -> string -> llbuilder -> llvalue
270 = "llvm_build_udiv"
271 external build_sdiv : llvalue -> llvalue -> string -> llbuilder -> llvalue
272 = "llvm_build_sdiv"
273 external build_fdiv : llvalue -> llvalue -> string -> llbuilder -> llvalue
274 = "llvm_build_fdiv"
275 external build_urem : llvalue -> llvalue -> string -> llbuilder -> llvalue
276 = "llvm_build_urem"
277 external build_srem : llvalue -> llvalue -> string -> llbuilder -> llvalue
278 = "llvm_build_srem"
279 external build_frem : llvalue -> llvalue -> string -> llbuilder -> llvalue
280 = "llvm_build_frem"
281 external build_shl : llvalue -> llvalue -> string -> llbuilder -> llvalue
282 = "llvm_build_shl"
283 external build_lshr : llvalue -> llvalue -> string -> llbuilder -> llvalue
284 = "llvm_build_lshr"
285 external build_ashr : llvalue -> llvalue -> string -> llbuilder -> llvalue
286 = "llvm_build_ashr"
287 external build_and : llvalue -> llvalue -> string -> llbuilder -> llvalue
288 = "llvm_build_and"
289 external build_or : llvalue -> llvalue -> string -> llbuilder -> llvalue
290 = "llvm_build_or"
291 external build_xor : llvalue -> llvalue -> string -> llbuilder -> llvalue
292 = "llvm_build_xor"
293 external build_neg : llvalue -> string -> llbuilder -> llvalue
294 = "llvm_build_neg"
295 external build_not : llvalue -> string -> llbuilder -> llvalue
296 = "llvm_build_not"
297
298 (*--... Memory .............................................................--*)
299 external build_malloc : lltype -> string -> llbuilder -> llvalue
300 = "llvm_build_malloc"
301 external build_array_malloc : lltype -> llvalue -> string -> llbuilder ->
302 llvalue = "llvm_build_array_malloc"
303 external build_alloca : lltype -> string -> llbuilder -> llvalue
304 = "llvm_build_alloca"
305 external build_array_alloca : lltype -> llvalue -> string -> llbuilder ->
306 llvalue = "llvm_build_array_alloca"
307 external build_free : llvalue -> llbuilder -> llvalue = "llvm_build_free"
308 external build_load : llvalue -> string -> llbuilder -> llvalue
309 = "llvm_build_load"
310 external build_store : llvalue -> llvalue -> llbuilder -> llvalue
311 = "llvm_build_store"
312 external build_gep : llvalue -> llvalue array -> string -> llbuilder -> llvalue
313 = "llvm_build_gep"
314
315 (*--... Casts ..............................................................--*)
316 external build_trunc : llvalue -> lltype -> string -> llbuilder -> llvalue
317 = "llvm_build_trunc"
318 external build_zext : llvalue -> lltype -> string -> llbuilder -> llvalue
319 = "llvm_build_zext"
320 external build_sext : llvalue -> lltype -> string -> llbuilder -> llvalue
321 = "llvm_build_sext"
322 external build_fptoui : llvalue -> lltype -> string -> llbuilder -> llvalue
323 = "llvm_build_fptoui"
324 external build_fptosi : llvalue -> lltype -> string -> llbuilder -> llvalue
325 = "llvm_build_fptosi"
326 external build_uitofp : llvalue -> lltype -> string -> llbuilder -> llvalue
327 = "llvm_build_uitofp"
328 external build_sitofp : llvalue -> lltype -> string -> llbuilder -> llvalue
329 = "llvm_build_sitofp"
330 external build_fptrunc : llvalue -> lltype -> string -> llbuilder -> llvalue
331 = "llvm_build_fptrunc"
332 external build_fpext : llvalue -> lltype -> string -> llbuilder -> llvalue
333 = "llvm_build_fpext"
334 external build_ptrtoint : llvalue -> lltype -> string -> llbuilder -> llvalue
335 = "llvm_build_prttoint"
336 external build_inttoptr : llvalue -> lltype -> string -> llbuilder -> llvalue
337 = "llvm_build_inttoptr"
338 external build_bitcast : llvalue -> lltype -> string -> llbuilder -> llvalue
339 = "llvm_build_bitcast"
340
341 (*--... Comparisons ........................................................--*)
342 external build_icmp : int_predicate -> llvalue -> llvalue -> string ->
343 llbuilder -> llvalue = "llvm_build_icmp"
344 external build_fcmp : real_predicate -> llvalue -> llvalue -> string ->
345 llbuilder -> llvalue = "llvm_build_fcmp"
346
347 (*--... Miscellaneous instructions .........................................--*)
348 external build_phi : lltype -> string -> llbuilder -> llvalue = "llvm_build_phi"
349 external build_call : llvalue -> llvalue array -> string -> llbuilder -> llvalue
350 = "llvm_build_call"
351 external build_select : llvalue -> llvalue -> llvalue -> string -> llbuilder ->
352 llvalue = "llvm_build_select"
353 external build_va_arg : llvalue -> lltype -> string -> llbuilder -> llvalue
354 = "llvm_build_va_arg"
355 external build_extractelement : llvalue -> llvalue -> string -> llbuilder ->
356 llvalue = "llvm_build_extractelement"
357 external build_insertelement : llvalue -> llvalue -> llvalue -> string ->
358 llbuilder -> llvalue = "llvm_build_insertelement"
359 external build_shufflevector : llvalue -> llvalue -> llvalue -> string ->
360 llbuilder -> llvalue = "llvm_build_shufflevector"
1616
1717 #include "llvm-c/Core.h"
1818 #include "caml/alloc.h"
19 #include "caml/custom.h"
1920 #include "caml/mlvalues.h"
2021 #include "caml/memory.h"
2122 #include "llvm/Config/config.h"
2223 #include
23 #ifdef HAVE_ALLOCA_H
24 #include
25 #endif
24 #include
2625
2726
2827 /*===-- Modules -----------------------------------------------------------===*/
2928
3029 /* string -> llmodule */
31 CAMLprim value llvm_create_module(value ModuleID) {
32 return (value) LLVMModuleCreateWithName(String_val(ModuleID));
30 CAMLprim LLVMModuleRef llvm_create_module(value ModuleID) {
31 return LLVMModuleCreateWithName(String_val(ModuleID));
3332 }
3433
3534 /* llmodule -> unit */
36 CAMLprim value llvm_dispose_module(value M) {
37 LLVMDisposeModule((LLVMModuleRef) M);
35 CAMLprim value llvm_dispose_module(LLVMModuleRef M) {
36 LLVMDisposeModule(M);
3837 return Val_unit;
3938 }
4039
4140 /* string -> lltype -> llmodule -> bool */
42 CAMLprim value llvm_add_type_name(value Name, value Ty, value M) {
43 int res = LLVMAddTypeName((LLVMModuleRef) M,
44 String_val(Name), (LLVMTypeRef) Ty);
41 CAMLprim value llvm_add_type_name(value Name, LLVMTypeRef Ty, LLVMModuleRef M) {
42 int res = LLVMAddTypeName(M, String_val(Name), Ty);
4543 return Val_bool(res == 0);
4644 }
4745
46 /* string -> llmodule -> unit */
47 CAMLprim value llvm_delete_type_name(value Name, LLVMModuleRef M) {
48 LLVMDeleteTypeName(M, String_val(Name));
49 return Val_unit;
50 }
51
4852
4953 /*===-- Types -------------------------------------------------------------===*/
5054
5155 /* lltype -> type_kind */
52 CAMLprim value llvm_classify_type(value Ty) {
53 return Val_int(LLVMGetTypeKind((LLVMTypeRef) Ty));
56 CAMLprim value llvm_classify_type(LLVMTypeRef Ty) {
57 return Val_int(LLVMGetTypeKind(Ty));
5458 }
5559
5660 /* lltype -> lltype -> unit */
57 CAMLprim value llvm_refine_abstract_type(value ConcreteTy, value AbstractTy) {
58 LLVMRefineAbstractType((LLVMTypeRef) AbstractTy, (LLVMTypeRef) ConcreteTy);
59 return (value) Val_unit;
61 CAMLprim value llvm_refine_abstract_type(LLVMTypeRef ConcreteTy,
62 LLVMTypeRef AbstractTy) {
63 LLVMRefineAbstractType(AbstractTy, ConcreteTy);
64 return Val_unit;
6065 }
6166
6267 /*--... Operations on integer types ........................................--*/
6368
6469 /* unit -> lltype */
65 CAMLprim value llvm_i1_type (value Unit) { return (value) LLVMInt1Type(); }
66 CAMLprim value llvm_i8_type (value Unit) { return (value) LLVMInt8Type(); }
67 CAMLprim value llvm_i16_type(value Unit) { return (value) LLVMInt16Type(); }
68 CAMLprim value llvm_i32_type(value Unit) { return (value) LLVMInt32Type(); }
69 CAMLprim value llvm_i64_type(value Unit) { return (value) LLVMInt64Type(); }
70 CAMLprim LLVMTypeRef llvm_i1_type (value Unit) { return LLVMInt1Type(); }
71 CAMLprim LLVMTypeRef llvm_i8_type (value Unit) { return LLVMInt8Type(); }
72 CAMLprim LLVMTypeRef llvm_i16_type(value Unit) { return LLVMInt16Type(); }
73 CAMLprim LLVMTypeRef llvm_i32_type(value Unit) { return LLVMInt32Type(); }
74 CAMLprim LLVMTypeRef llvm_i64_type(value Unit) { return LLVMInt64Type(); }
7075
7176 /* int -> lltype */
72 CAMLprim value llvm_make_integer_type(value Width) {
73 return (value) LLVMCreateIntegerType(Int_val(Width));
77 CAMLprim LLVMTypeRef llvm_make_integer_type(value Width) {
78 return LLVMCreateIntType(Int_val(Width));
7479 }
7580
7681 /* lltype -> int */
77 CAMLprim value llvm_integer_bitwidth(value IntegerTy) {
78 return Val_int(LLVMGetIntegerTypeWidth((LLVMTypeRef) IntegerTy));
82 CAMLprim value llvm_integer_bitwidth(LLVMTypeRef IntegerTy) {
83 return Val_int(LLVMGetIntTypeWidth(IntegerTy));
7984 }
8085
8186 /*--... Operations on real types ...........................................--*/
8287
8388 /* unit -> lltype */
84 CAMLprim value llvm_float_type(value Unit) {
85 return (value) LLVMFloatType();
89 CAMLprim LLVMTypeRef llvm_float_type(value Unit) {
90 return LLVMFloatType();
8691 }
8792
8893 /* unit -> lltype */
89 CAMLprim value llvm_double_type(value Unit) {
90 return (value) LLVMDoubleType();
94 CAMLprim LLVMTypeRef llvm_double_type(value Unit) {
95 return LLVMDoubleType();
9196 }
9297
9398 /* unit -> lltype */
94 CAMLprim value llvm_x86fp80_type(value Unit) {
95 return (value) LLVMX86FP80Type();
99 CAMLprim LLVMTypeRef llvm_x86fp80_type(value Unit) {
100 return LLVMX86FP80Type();
96101 }
97102
98103 /* unit -> lltype */
99 CAMLprim value llvm_fp128_type(value Unit) {
100 return (value) LLVMFP128Type();
104 CAMLprim LLVMTypeRef llvm_fp128_type(value Unit) {
105 return LLVMFP128Type();
101106 }
102107
103108 /* unit -> lltype */
104 CAMLprim value llvm_ppc_fp128_type(value Unit) {
105 return (value) LLVMPPCFP128Type();
109 CAMLprim LLVMTypeRef llvm_ppc_fp128_type(value Unit) {
110 return LLVMPPCFP128Type();
106111 }
107112
108113 /*--... Operations on function types .......................................--*/
109114
110115 /* lltype -> lltype array -> bool -> lltype */
111 CAMLprim value llvm_make_function_type(value RetTy, value ParamTys,
112 value IsVarArg) {
113 return (value) LLVMCreateFunctionType((LLVMTypeRef) RetTy,
114 (LLVMTypeRef *) ParamTys,
115 Wosize_val(ParamTys),
116 Bool_val(IsVarArg));
116 CAMLprim LLVMTypeRef llvm_make_function_type(LLVMTypeRef RetTy, value ParamTys,
117 value IsVarArg) {
118 return LLVMCreateFunctionType(RetTy, (LLVMTypeRef *) ParamTys,
119 Wosize_val(ParamTys),
120 Bool_val(IsVarArg));
117121 }
118122
119123 /* lltype -> bool */
120 CAMLprim value llvm_is_var_arg(value FunTy) {
121 return Val_bool(LLVMIsFunctionVarArg((LLVMTypeRef) FunTy));
124 CAMLprim value llvm_is_var_arg(LLVMTypeRef FunTy) {
125 return Val_bool(LLVMIsFunctionVarArg(FunTy));
122126 }
123127
124128 /* lltype -> lltype */
125 CAMLprim value llvm_return_type(value FunTy) {
126 return (value) LLVMGetFunctionReturnType((LLVMTypeRef) FunTy);
129 CAMLprim LLVMTypeRef llvm_return_type(LLVMTypeRef FunTy) {
130 return LLVMGetReturnType(FunTy);
127131 }
128132
129133 /* lltype -> lltype array */
130 CAMLprim value llvm_param_types(value FunTy) {
131 unsigned Count = LLVMGetFunctionParamCount((LLVMTypeRef) FunTy);
132 LLVMTypeRef *FunTys = alloca(Count * sizeof(LLVMTypeRef));
133
134 /* copy into an ocaml array */
135 unsigned i;
136 value ParamTys = alloc(Count, 0);
137
138 LLVMGetFunctionParamTypes((LLVMTypeRef) FunTy, FunTys);
139 for (i = 0; i != Count; ++i)
140 Store_field(ParamTys, i, (value) FunTys[i]);
141
142 return ParamTys;
134 CAMLprim value llvm_param_types(LLVMTypeRef FunTy) {
135 value Tys = alloc(LLVMCountParamTypes(FunTy), 0);
136 LLVMGetParamTypes(FunTy, (LLVMTypeRef *) Tys);
137 return Tys;
143138 }
144139
145140 /*--... Operations on struct types .........................................--*/
152147 }
153148
154149 /* lltype -> lltype array */
155 CAMLprim value llvm_element_types(value StructTy) {
156 unsigned Count = LLVMGetStructElementCount((LLVMTypeRef) StructTy);
157 LLVMTypeRef *Tys = alloca(Count * sizeof(LLVMTypeRef));
158
159 /* copy into an ocaml array */
160 unsigned i;
161 value ElementTys = alloc(Count, 0);
162
163 LLVMGetStructElementTypes((LLVMTypeRef) StructTy, Tys);
164 for (i = 0; i != Count; ++i)
165 Store_field(ElementTys, i, (value) Tys[i]);
166
167 return ElementTys;
168 }
169
170 CAMLprim value llvm_is_packed(value StructTy) {
171 return Val_bool(LLVMIsPackedStruct((LLVMTypeRef) StructTy));
150 CAMLprim value llvm_element_types(LLVMTypeRef StructTy) {
151 value Tys = alloc(LLVMCountStructElementTypes(StructTy), 0);
152 LLVMGetStructElementTypes(StructTy, (LLVMTypeRef *) Tys);
153 return Tys;
154 }
155
156 /* lltype -> bool */
157 CAMLprim value llvm_is_packed(LLVMTypeRef StructTy) {
158 return Val_bool(LLVMIsPackedStruct(StructTy));
172159 }
173160
174161 /*--... Operations on array, pointer, and vector types .....................--*/
175162
176163 /* lltype -> int -> lltype */
177 CAMLprim value llvm_make_array_type(value ElementTy, value Count) {
178 return (value) LLVMCreateArrayType((LLVMTypeRef) ElementTy, Int_val(Count));
164 CAMLprim value llvm_make_array_type(LLVMTypeRef ElementTy, value Count) {
165 return (value) LLVMCreateArrayType(ElementTy, Int_val(Count));
179166 }
180167
181168 /* lltype -> lltype */
182 CAMLprim value llvm_make_pointer_type(value ElementTy) {
183 return (value) LLVMCreatePointerType((LLVMTypeRef) ElementTy);
169 CAMLprim LLVMTypeRef llvm_make_pointer_type(LLVMTypeRef ElementTy) {
170 return LLVMCreatePointerType(ElementTy);
184171 }
185172
186173 /* lltype -> int -> lltype */
187 CAMLprim value llvm_make_vector_type(value ElementTy, value Count) {
188 return (value) LLVMCreateVectorType((LLVMTypeRef) ElementTy, Int_val(Count));
174 CAMLprim LLVMTypeRef llvm_make_vector_type(LLVMTypeRef ElementTy, value Count) {
175 return LLVMCreateVectorType(ElementTy, Int_val(Count));
189176 }
190177
191178 /* lltype -> lltype */
192 CAMLprim value llvm_element_type(value Ty) {
193 return (value) LLVMGetElementType((LLVMTypeRef) Ty);
179 CAMLprim LLVMTypeRef llvm_element_type(LLVMTypeRef Ty) {
180 return LLVMGetElementType(Ty);
194181 }
195182
196183 /* lltype -> int */
197 CAMLprim value llvm_array_length(value ArrayTy) {
198 return Val_int(LLVMGetArrayLength((LLVMTypeRef) ArrayTy));
184 CAMLprim value llvm_array_length(LLVMTypeRef ArrayTy) {
185 return Val_int(LLVMGetArrayLength(ArrayTy));
199186 }
200187
201188 /* lltype -> int */
202 CAMLprim value llvm_vector_size(value VectorTy) {
203 return Val_int(LLVMGetVectorSize((LLVMTypeRef) VectorTy));
189 CAMLprim value llvm_vector_size(LLVMTypeRef VectorTy) {
190 return Val_int(LLVMGetVectorSize(VectorTy));
204191 }
205192
206193 /*--... Operations on other types ..........................................--*/
207194
208195 /* unit -> lltype */
209 CAMLprim value llvm_void_type (value Unit) { return (value) LLVMVoidType(); }
210 CAMLprim value llvm_label_type(value Unit) { return (value) LLVMLabelType(); }
196 CAMLprim LLVMTypeRef llvm_void_type (value Unit) { return LLVMVoidType(); }
197 CAMLprim LLVMTypeRef llvm_label_type(value Unit) { return LLVMLabelType(); }
211198
212199 /* unit -> lltype */
213 CAMLprim value llvm_make_opaque_type(value Unit) {
214 return (value) LLVMCreateOpaqueType();
200 CAMLprim LLVMTypeRef llvm_make_opaque_type(value Unit) {
201 return LLVMCreateOpaqueType();
215202 }
216203
217204
218205 /*===-- VALUES ------------------------------------------------------------===*/
219206
220207 /* llvalue -> lltype */
221 CAMLprim value llvm_type_of(value Val) {
222 return (value) LLVMGetTypeOfValue((LLVMValueRef) Val);
208 CAMLprim LLVMTypeRef llvm_type_of(LLVMValueRef Val) {
209 return LLVMTypeOf(Val);
223210 }
224211
225212 /* llvalue -> string */
226 CAMLprim value llvm_value_name(value Val) {
227 return copy_string(LLVMGetValueName((LLVMValueRef) Val));
213 CAMLprim value llvm_value_name(LLVMValueRef Val) {
214 return copy_string(LLVMGetValueName(Val));
228215 }
229216
230217 /* string -> llvalue -> unit */
231 CAMLprim value llvm_set_value_name(value Name, value Val) {
232 LLVMSetValueName((LLVMValueRef) Val, String_val(Name));
218 CAMLprim value llvm_set_value_name(value Name, LLVMValueRef Val) {
219 LLVMSetValueName(Val, String_val(Name));
233220 return Val_unit;
234221 }
235222
236223 /*--... Operations on constants of (mostly) any type .......................--*/
237224
238 /* lltype -> llvalue */
239 CAMLprim value llvm_make_null(value Ty) {
240 return (value) LLVMGetNull((LLVMTypeRef) Ty);
241 }
242
243 /* lltype -> llvalue */
244 CAMLprim value llvm_make_all_ones(value Ty) {
245 return (value) LLVMGetAllOnes((LLVMTypeRef) Ty);
246 }
247
248 /* lltype -> llvalue */
249 CAMLprim value llvm_make_undef(value Ty) {
250 return (value) LLVMGetUndef((LLVMTypeRef) Ty);
251 }
252
253225 /* llvalue -> bool */
254 CAMLprim value llvm_is_constant(value Ty) {
255 return Val_bool(LLVMIsConstant((LLVMValueRef) Ty));
226 CAMLprim value llvm_is_constant(LLVMValueRef Val) {
227 return Val_bool(LLVMIsConstant(Val));
256228 }
257229
258230 /* llvalue -> bool */
259 CAMLprim value llvm_is_null(value Val) {
260 return Val_bool(LLVMIsNull((LLVMValueRef) Val));
231 CAMLprim value llvm_is_null(LLVMValueRef Val) {
232 return Val_bool(LLVMIsNull(Val));
261233 }
262234
263235 /* llvalue -> bool */
264 CAMLprim value llvm_is_undef(value Ty) {
265 return Val_bool(LLVMIsUndef((LLVMValueRef) Ty));
236 CAMLprim value llvm_is_undef(LLVMValueRef Val) {
237 return Val_bool(LLVMIsUndef(Val));
266238 }
267239
268240 /*--... Operations on scalar constants .....................................--*/
269241
270242 /* lltype -> int -> bool -> llvalue */
271 CAMLprim value llvm_make_int_constant(value IntTy, value N, value SExt) {
243 CAMLprim LLVMValueRef llvm_make_int_constant(LLVMTypeRef IntTy, value N,
244 value SExt) {
272245 /* GCC warns if we use the ternary operator. */
273246 unsigned long long N2;
274247 if (Bool_val(SExt))
276249 else
277250 N2 = (mlsize_t) Int_val(N);
278251
279 return (value) LLVMGetIntConstant((LLVMTypeRef) IntTy, N2, Bool_val(SExt));
252 return LLVMGetIntConstant(IntTy, N2, Bool_val(SExt));
280253 }
281254
282255 /* lltype -> Int64.t -> bool -> llvalue */
283 CAMLprim value llvm_make_int64_constant(value IntTy, value N, value SExt) {
284 return (value) LLVMGetIntConstant((LLVMTypeRef) IntTy, Int64_val(N),
285 Bool_val(SExt));
256 CAMLprim LLVMValueRef llvm_make_int64_constant(LLVMTypeRef IntTy, value N,
257 value SExt) {
258 return LLVMGetIntConstant(IntTy, Int64_val(N), Bool_val(SExt));
286259 }
287260
288261 /* lltype -> float -> llvalue */
289 CAMLprim value llvm_make_real_constant(value RealTy, value N) {
290 return (value) LLVMGetRealConstant((LLVMTypeRef) RealTy, Double_val(N));
262 CAMLprim LLVMValueRef llvm_make_real_constant(LLVMTypeRef RealTy, value N) {
263 return LLVMGetRealConstant(RealTy, Double_val(N));
291264 }
292265
293266 /*--... Operations on composite constants ..................................--*/
294267
295268 /* string -> bool -> llvalue */
296 CAMLprim value llvm_make_string_constant(value Str, value NullTerminate) {
297 return (value) LLVMGetStringConstant(String_val(Str),
298 string_length(Str),
299 Bool_val(NullTerminate) == 0);
269 CAMLprim LLVMValueRef llvm_make_string_constant(value Str, value NullTerminate) {
270 return LLVMGetStringConstant(String_val(Str), string_length(Str),
271 Bool_val(NullTerminate) == 0);
300272 }
301273
302274 /* lltype -> llvalue array -> llvalue */
303 CAMLprim value llvm_make_array_constant(value ElementTy, value ElementVals) {
304 return (value) LLVMGetArrayConstant((LLVMTypeRef) ElementTy,
305 (LLVMValueRef*) Op_val(ElementVals),
306 Wosize_val(ElementVals));
275 CAMLprim LLVMValueRef llvm_make_array_constant(LLVMTypeRef ElementTy,
276 value ElementVals) {
277 return LLVMGetArrayConstant(ElementTy, (LLVMValueRef*) Op_val(ElementVals),
278 Wosize_val(ElementVals));
307279 }
308280
309281 /* llvalue array -> bool -> llvalue */
310 CAMLprim value llvm_make_struct_constant(value ElementVals, value Packed) {
311 return (value) LLVMGetStructConstant((LLVMValueRef*) Op_val(ElementVals),
312 Wosize_val(ElementVals),
313 Bool_val(Packed));
282 CAMLprim LLVMValueRef llvm_make_struct_constant(value ElementVals,
283 value Packed) {
284 return LLVMGetStructConstant((LLVMValueRef *) Op_val(ElementVals),
285 Wosize_val(ElementVals), Bool_val(Packed));
314286 }
315287
316288 /* llvalue array -> llvalue */
322294 /*--... Operations on global variables, functions, and aliases (globals) ...--*/
323295
324296 /* llvalue -> bool */
325 CAMLprim value llvm_is_declaration(value Global) {
326 return Val_bool(LLVMIsDeclaration((LLVMValueRef) Global));
297 CAMLprim value llvm_is_declaration(LLVMValueRef Global) {
298 return Val_bool(LLVMIsDeclaration(Global));
327299 }
328300
329301 /* llvalue -> linkage */
330 CAMLprim value llvm_linkage(value Global) {
331 return Val_int(LLVMGetLinkage((LLVMValueRef) Global));
302 CAMLprim value llvm_linkage(LLVMValueRef Global) {
303 return Val_int(LLVMGetLinkage(Global));
332304 }
333305
334306 /* linkage -> llvalue -> unit */
335 CAMLprim value llvm_set_linkage(value Linkage, value Global) {
336 LLVMSetLinkage((LLVMValueRef) Global, Int_val(Linkage));
307 CAMLprim value llvm_set_linkage(value Linkage, LLVMValueRef Global) {
308 LLVMSetLinkage(Global, Int_val(Linkage));
337309 return Val_unit;
338310 }
339311
340312 /* llvalue -> string */
341 CAMLprim value llvm_section(value Global) {
342 return copy_string(LLVMGetSection((LLVMValueRef) Global));
313 CAMLprim value llvm_section(LLVMValueRef Global) {
314 return copy_string(LLVMGetSection(Global));
343315 }
344316
345317 /* string -> llvalue -> unit */
346 CAMLprim value llvm_set_section(value Section, value Global) {
347 LLVMSetSection((LLVMValueRef) Global, String_val(Section));
318 CAMLprim value llvm_set_section(value Section, LLVMValueRef Global) {
319 LLVMSetSection(Global, String_val(Section));
348320 return Val_unit;
349321 }
350322
351323 /* llvalue -> visibility */
352 CAMLprim value llvm_visibility(value Global) {
353 return Val_int(LLVMGetVisibility((LLVMValueRef) Global));
324 CAMLprim value llvm_visibility(LLVMValueRef Global) {
325 return Val_int(LLVMGetVisibility(Global));
354326 }
355327
356328 /* visibility -> llvalue -> unit */
357 CAMLprim value llvm_set_visibility(value Viz, value Global) {
358 LLVMSetVisibility((LLVMValueRef) Global, Int_val(Viz));
329 CAMLprim value llvm_set_visibility(value Viz, LLVMValueRef Global) {
330 LLVMSetVisibility(Global, Int_val(Viz));
359331 return Val_unit;
360332 }
361333
362334 /* llvalue -> int */
363 CAMLprim value llvm_alignment(value Global) {
364 return Val_int(LLVMGetAlignment((LLVMValueRef) Global));
335 CAMLprim value llvm_alignment(LLVMValueRef Global) {
336 return Val_int(LLVMGetAlignment(Global));
365337 }
366338
367339 /* int -> llvalue -> unit */
368 CAMLprim value llvm_set_alignment(value Bytes, value Global) {
369 LLVMSetAlignment((LLVMValueRef) Global, Int_val(Bytes));
340 CAMLprim value llvm_set_alignment(value Bytes, LLVMValueRef Global) {
341 LLVMSetAlignment(Global, Int_val(Bytes));
370342 return Val_unit;
371343 }
372344
373345 /*--... Operations on global variables .....................................--*/
374346
375347 /* lltype -> string -> llmodule -> llvalue */
376 CAMLprim value llvm_add_global(value Ty, value Name, value M) {
377 return (value) LLVMAddGlobal((LLVMModuleRef) M,
378 (LLVMTypeRef) Ty, String_val(Name));
379 }
380
381 /* lltype -> string -> llmodule -> llvalue */
382 CAMLprim value llvm_declare_global(value Ty, value Name, value M) {
383 return (value) LLVMAddGlobal((LLVMModuleRef) M,
384 (LLVMTypeRef) Ty, String_val(Name));
348 CAMLprim LLVMValueRef llvm_declare_global(LLVMTypeRef Ty, value Name,
349 LLVMModuleRef M) {
350 return LLVMAddGlobal(M, Ty, String_val(Name));
385351 }
386352
387353 /* string -> llvalue -> llmodule -> llvalue */
388 CAMLprim value llvm_define_global(value Name, value ConstantVal, value M) {
389 LLVMValueRef Initializer = (LLVMValueRef) ConstantVal;
390 LLVMValueRef GlobalVar = LLVMAddGlobal((LLVMModuleRef) M,
391 LLVMGetTypeOfValue(Initializer),
354 CAMLprim LLVMValueRef llvm_define_global(value Name, LLVMValueRef Initializer,
355 LLVMModuleRef M) {
356 LLVMValueRef GlobalVar = LLVMAddGlobal(M, LLVMTypeOf(Initializer),
392357 String_val(Name));
393358 LLVMSetInitializer(GlobalVar, Initializer);
394 return (value) GlobalVar;
359 return GlobalVar;
395360 }
396361
397362 /* llvalue -> unit */
398 CAMLprim value llvm_delete_global(value GlobalVar) {
399 LLVMDeleteGlobal((LLVMValueRef) GlobalVar);
400 return Val_unit;
401 }
402
403 /* llvalue -> llvalue */
404 CAMLprim value llvm_global_initializer(value GlobalVar) {
405 return (value) LLVMGetInitializer((LLVMValueRef) GlobalVar);
363 CAMLprim value llvm_delete_global(LLVMValueRef GlobalVar) {
364 LLVMDeleteGlobal(GlobalVar);
365 return Val_unit;
406366 }
407367
408368 /* llvalue -> llvalue -> unit */
409 CAMLprim value llvm_set_initializer(value ConstantVal, value GlobalVar) {
410 LLVMSetInitializer((LLVMValueRef) GlobalVar, (LLVMValueRef) ConstantVal);
369 CAMLprim value llvm_set_initializer(LLVMValueRef ConstantVal,
370 LLVMValueRef GlobalVar) {
371 LLVMSetInitializer(GlobalVar, ConstantVal);
411372 return Val_unit;
412373 }
413374
414375 /* llvalue -> unit */
415 CAMLprim value llvm_remove_initializer(value GlobalVar) {
416 LLVMSetInitializer((LLVMValueRef) GlobalVar, NULL);
376 CAMLprim value llvm_remove_initializer(LLVMValueRef GlobalVar) {
377 LLVMSetInitializer(GlobalVar, NULL);
417378 return Val_unit;
418379 }
419380
420381 /* llvalue -> bool */
421 CAMLprim value llvm_is_thread_local(value GlobalVar) {
422 return Val_bool(LLVMIsThreadLocal((LLVMValueRef) GlobalVar));
382 CAMLprim value llvm_is_thread_local(LLVMValueRef GlobalVar) {
383 return Val_bool(LLVMIsThreadLocal(GlobalVar));
423384 }
424385
425386 /* bool -> llvalue -> unit */
426 CAMLprim value llvm_set_thread_local(value IsThreadLocal, value GlobalVar) {
427 LLVMSetThreadLocal((LLVMValueRef) GlobalVar, Bool_val(IsThreadLocal));
428 return Val_unit;
429 }
387 CAMLprim value llvm_set_thread_local(value IsThreadLocal,
388 LLVMValueRef GlobalVar) {
389 LLVMSetThreadLocal(GlobalVar, Bool_val(IsThreadLocal));
390 return Val_unit;
391 }
392
393 /*--... Operations on functions ............................................--*/
394
395 /* string -> lltype -> llmodule -> llvalue */
396 CAMLprim LLVMValueRef llvm_declare_function(value Name, LLVMTypeRef Ty,
397 LLVMModuleRef M) {
398 return LLVMAddFunction(M, String_val(Name), Ty);
399 }
400
401 /* string -> lltype -> llmodule -> llvalue */
402 CAMLprim LLVMValueRef llvm_define_function(value Name, LLVMTypeRef Ty,
403 LLVMModuleRef M) {
404 LLVMValueRef Fn = LLVMAddFunction(M, String_val(Name), Ty);
405 LLVMAppendBasicBlock(Fn, "entry");
406 return Fn;
407 }
408
409 /* llvalue -> unit */
410 CAMLprim value llvm_delete_function(LLVMValueRef Fn) {
411 LLVMDeleteFunction(Fn);
412 return Val_unit;
413 }
414
415 /* llvalue -> int -> llvalue */
416 CAMLprim LLVMValueRef llvm_param(LLVMValueRef Fn, value Index) {
417 return LLVMGetParam(Fn, Int_val(Index));
418 }
419
420 /* llvalue -> int -> llvalue */
421 CAMLprim value llvm_params(LLVMValueRef Fn, value Index) {
422 value Params = alloc(LLVMCountParams(Fn), 0);
423 LLVMGetParams(Fn, (LLVMValueRef *) Op_val(Params));
424 return Params;
425 }
426
427 /* llvalue -> bool */
428 CAMLprim value llvm_is_intrinsic(LLVMValueRef Fn) {
429 return Val_bool(LLVMGetIntrinsicID(Fn));
430 }
431
432 /* llvalue -> int */
433 CAMLprim value llvm_function_call_conv(LLVMValueRef Fn) {
434 return Val_int(LLVMGetFunctionCallConv(Fn));
435 }
436
437 /* int -> llvalue -> unit */
438 CAMLprim value llvm_set_function_call_conv(value Id, LLVMValueRef Fn) {
439 LLVMSetFunctionCallConv(Fn, Int_val(Id));
440 return Val_unit;
441 }
442
443 /*--... Operations on basic blocks .........................................--*/
444
445 /* llvalue -> llbasicblock array */
446 CAMLprim value llvm_basic_blocks(LLVMValueRef Fn) {
447 value MLArray = alloc(LLVMCountBasicBlocks(Fn), 0);
448 LLVMGetBasicBlocks(Fn, (LLVMBasicBlockRef *) Op_val(MLArray));
449 return MLArray;
450 }
451
452 /* llbasicblock -> unit */
453 CAMLprim value llvm_delete_block(LLVMBasicBlockRef BB) {
454 LLVMDeleteBasicBlock(BB);
455 return Val_unit;
456 }
457
458 /* string -> llvalue -> llbasicblock */
459 CAMLprim LLVMBasicBlockRef llvm_append_block(value Name, LLVMValueRef Fn) {
460 return LLVMAppendBasicBlock(Fn, String_val(Name));
461 }
462
463 /* string -> llbasicblock -> llbasicblock */
464 CAMLprim LLVMBasicBlockRef llvm_insert_block(value Name, LLVMBasicBlockRef BB) {
465 return LLVMInsertBasicBlock(BB, String_val(Name));
466 }
467
468 /* llvalue -> bool */
469 CAMLprim value llvm_value_is_block(LLVMValueRef Val) {
470 return Val_bool(LLVMValueIsBasicBlock(Val));
471 }
472
473
474 /*===-- Instruction builders ----------------------------------------------===*/
475
476 #define Builder_val(v) (*(LLVMBuilderRef *)(Data_custom_val(v)))
477
478 void llvm_finalize_builder(value B) {
479 fprintf(stderr, "disposing builder = 0x%08x\n", (int) Builder_val(B));
480 LLVMDisposeBuilder(Builder_val(B));
481 }
482
483 static struct custom_operations builder_ops = {
484 (char *) "LLVMBuilder",
485 llvm_finalize_builder,
486 custom_compare_default,
487 custom_hash_default,
488 custom_serialize_default,
489 custom_deserialize_default
490 };
491
492 /* llvalue -> llbuilder */
493 CAMLprim value llvm_builder_before(LLVMValueRef Inst) {
494 value V;
495 LLVMBuilderRef B = LLVMCreateBuilder();
496 LLVMPositionBuilderBefore(B, Inst);
497 V = alloc_custom(&builder_ops, sizeof(LLVMBuilderRef), 0, 1);
498 Builder_val(V) = B;
499 return V;
500 }
501
502 /* llbasicblock -> llbuilder */
503 CAMLprim value llvm_builder_at_end(LLVMBasicBlockRef BB) {
504 value V;
505 LLVMBuilderRef B = LLVMCreateBuilder();
506 LLVMPositionBuilderAtEnd(B, BB);
507 fprintf(stderr, "returning builder = 0x%08x\n", (int) B);
508 V = alloc_custom(&builder_ops, sizeof(LLVMBuilderRef), 0, 1);
509 Builder_val(V) = B;
510 return V;
511 }
512
513 /* llvalue -> llbuilder -> unit */
514 CAMLprim value llvm_position_before(LLVMValueRef Inst, value B) {
515 LLVMPositionBuilderBefore(Builder_val(B), Inst);
516 return Val_unit;
517 }
518
519 /* llbasicblock -> llbuilder -> unit */
520 CAMLprim value llvm_position_at_end(LLVMBasicBlockRef BB, value B) {
521 LLVMPositionBuilderAtEnd(Builder_val(B), BB);
522 return Val_unit;
523 }
524
525 /*--... Terminators ........................................................--*/
526
527 /* llbuilder -> llvalue */
528 CAMLprim LLVMValueRef llvm_build_ret_void(value B) {
529 return LLVMBuildRetVoid(Builder_val(B));
530 }
531
532 /* llvalue -> llbuilder -> llvalue */
533 CAMLprim LLVMValueRef llvm_build_ret(LLVMValueRef Val, value B) {
534 return LLVMBuildRet(Builder_val(B), Val);
535 }
536
537 /* llbasicblock -> llbuilder -> llvalue */
538 CAMLprim LLVMValueRef llvm_build_br(LLVMBasicBlockRef BB, value B) {
539 return LLVMBuildBr(Builder_val(B), BB);
540 }
541
542 /* llvalue -> llbasicblock -> llbasicblock -> llbuilder -> llvalue */
543 CAMLprim LLVMValueRef llvm_build_cond_br(LLVMValueRef If,
544 LLVMBasicBlockRef Then,
545 LLVMBasicBlockRef Else,
546 value B) {
547 return LLVMBuildCondBr(Builder_val(B), If, Then, Else);
548 }
549
550 /* llvalue -> llbasicblock -> int -> llbuilder -> llvalue */
551 CAMLprim LLVMValueRef llvm_build_switch(LLVMValueRef Of,
552 LLVMBasicBlockRef Else,
553 value EstimatedCount,
554 value B) {
555 return LLVMBuildSwitch(Builder_val(B), Of, Else, Int_val(EstimatedCount));
556 }
557
558 /* llvalue -> llvalue array -> llbasicblock -> llbasicblock -> string ->
559 llbuilder -> llvalue */
560 CAMLprim LLVMValueRef llvm_build_invoke_nat(LLVMValueRef Fn, value Args,
561 LLVMBasicBlockRef Then,
562 LLVMBasicBlockRef Catch,
563 value Name, value B) {
564 return LLVMBuildInvoke(Builder_val(B), Fn, (LLVMValueRef *) Op_val(Args),
565 Wosize_val(Args), Then, Catch, String_val(Name));
566 }
567
568 /* llvalue -> llvalue array -> llbasicblock -> llbasicblock -> string ->
569 llbuilder -> llvalue */
570 CAMLprim LLVMValueRef llvm_build_invoke_bc(value Args[], int NumArgs) {
571 return llvm_build_invoke_nat((LLVMValueRef) Args[0], Args[1],
572 (LLVMBasicBlockRef) Args[2],
573 (LLVMBasicBlockRef) Args[3],
574 Args[4], Args[5]);
575 }
576
577 /* llbuilder -> llvalue */
578 CAMLprim LLVMValueRef llvm_build_unwind(value B) {
579 return LLVMBuildUnwind(Builder_val(B));
580 }
581
582 /* llbuilder -> llvalue */
583 CAMLprim LLVMValueRef llvm_build_unreachable(value B) {
584 return LLVMBuildUnreachable(Builder_val(B));
585 }
586
587 /*--... Arithmetic .........................................................--*/
588
589 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
590 CAMLprim LLVMValueRef llvm_build_add(LLVMValueRef LHS, LLVMValueRef RHS,
591 value Name, value B) {
592 return LLVMBuildAdd(Builder_val(B), LHS, RHS, String_val(Name));
593 }
594
595 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
596 CAMLprim LLVMValueRef llvm_build_sub(LLVMValueRef LHS, LLVMValueRef RHS,
597 value Name, value B) {
598 return LLVMBuildSub(Builder_val(B), LHS, RHS, String_val(Name));
599 }
600
601 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
602 CAMLprim LLVMValueRef llvm_build_mul(LLVMValueRef LHS, LLVMValueRef RHS,
603 value Name, value B) {
604 return LLVMBuildMul(Builder_val(B), LHS, RHS, String_val(Name));
605 }
606
607 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
608 CAMLprim LLVMValueRef llvm_build_udiv(LLVMValueRef LHS, LLVMValueRef RHS,
609 value Name, value B) {
610 return LLVMBuildUDiv(Builder_val(B), LHS, RHS, String_val(Name));
611 }
612
613 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
614 CAMLprim LLVMValueRef llvm_build_sdiv(LLVMValueRef LHS, LLVMValueRef RHS,
615 value Name, value B) {
616 return LLVMBuildSDiv(Builder_val(B), LHS, RHS, String_val(Name));
617 }
618
619 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
620 CAMLprim LLVMValueRef llvm_build_fdiv(LLVMValueRef LHS, LLVMValueRef RHS,
621 value Name, value B) {
622 return LLVMBuildFDiv(Builder_val(B), LHS, RHS, String_val(Name));
623 }
624
625 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
626 CAMLprim LLVMValueRef llvm_build_urem(LLVMValueRef LHS, LLVMValueRef RHS,
627 value Name, value B) {
628 return LLVMBuildURem(Builder_val(B), LHS, RHS, String_val(Name));
629 }
630
631 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
632 CAMLprim LLVMValueRef llvm_build_srem(LLVMValueRef LHS, LLVMValueRef RHS,
633 value Name, value B) {
634 return LLVMBuildSRem(Builder_val(B), LHS, RHS, String_val(Name));
635 }
636
637 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
638 CAMLprim LLVMValueRef llvm_build_frem(LLVMValueRef LHS, LLVMValueRef RHS,
639 value Name, value B) {
640 return LLVMBuildFRem(Builder_val(B), LHS, RHS, String_val(Name));
641 }
642
643 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
644 CAMLprim LLVMValueRef llvm_build_shl(LLVMValueRef LHS, LLVMValueRef RHS,
645 value Name, value B) {
646 return LLVMBuildShl(Builder_val(B), LHS, RHS, String_val(Name));
647 }
648
649 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
650 CAMLprim LLVMValueRef llvm_build_lshr(LLVMValueRef LHS, LLVMValueRef RHS,
651 value Name, value B) {
652 return LLVMBuildLShr(Builder_val(B), LHS, RHS, String_val(Name));
653 }
654
655 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
656 CAMLprim LLVMValueRef llvm_build_ashr(LLVMValueRef LHS, LLVMValueRef RHS,
657 value Name, value B) {
658 return LLVMBuildAShr(Builder_val(B), LHS, RHS, String_val(Name));
659 }
660
661 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
662 CAMLprim LLVMValueRef llvm_build_and(LLVMValueRef LHS, LLVMValueRef RHS,
663 value Name, value B) {
664 return LLVMBuildAnd(Builder_val(B), LHS, RHS, String_val(Name));
665 }
666
667 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
668 CAMLprim LLVMValueRef llvm_build_or(LLVMValueRef LHS, LLVMValueRef RHS,
669 value Name, value B) {
670 return LLVMBuildOr(Builder_val(B), LHS, RHS, String_val(Name));
671 }
672
673 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
674 CAMLprim LLVMValueRef llvm_build_xor(LLVMValueRef LHS, LLVMValueRef RHS,
675 value Name, value B) {
676 return LLVMBuildXor(Builder_val(B), LHS, RHS, String_val(Name));
677 }
678
679 /* llvalue -> string -> llbuilder -> llvalue */
680 CAMLprim LLVMValueRef llvm_build_neg(LLVMValueRef X,
681 value Name, value B) {
682 return LLVMBuildNeg(Builder_val(B), X, String_val(Name));
683 }
684
685 /* llvalue -> string -> llbuilder -> llvalue */
686 CAMLprim LLVMValueRef llvm_build_not(LLVMValueRef X,
687 value Name, value B) {
688 return LLVMBuildNot(Builder_val(B), X, String_val(Name));
689 }
690
691 /*--... Memory .............................................................--*/
692
693 /* lltype -> string -> llbuilder -> llvalue */
694 CAMLprim LLVMValueRef llvm_build_malloc(LLVMTypeRef Ty,
695 value Name, value B) {
696 return LLVMBuildMalloc(Builder_val(B), Ty, String_val(Name));
697 }
698
699 /* lltype -> llvalue -> string -> llbuilder -> llvalue */
700 CAMLprim LLVMValueRef llvm_build_array_malloc(LLVMTypeRef Ty, LLVMValueRef Size,
701 value Name, value B) {
702 return LLVMBuildArrayMalloc(Builder_val(B), Ty, Size, String_val(Name));
703 }
704
705 /* lltype -> string -> llbuilder -> llvalue */
706 CAMLprim LLVMValueRef llvm_build_alloca(LLVMTypeRef Ty,
707 value Name, value B) {
708 return LLVMBuildAlloca(Builder_val(B), Ty, String_val(Name));
709 }
710
711 /* lltype -> llvalue -> string -> llbuilder -> llvalue */
712 CAMLprim LLVMValueRef llvm_build_array_alloca(LLVMTypeRef Ty, LLVMValueRef Size,
713 value Name, value B) {
714 return LLVMBuildArrayAlloca(Builder_val(B), Ty, Size, String_val(Name));
715 }
716
717 /* llvalue -> llbuilder -> llvalue */
718 CAMLprim LLVMValueRef llvm_build_free(LLVMValueRef Pointer, value B) {
719 return LLVMBuildFree(Builder_val(B), Pointer);
720 }
721
722 /* llvalue -> string -> llbuilder -> llvalue */
723 CAMLprim LLVMValueRef llvm_build_load(LLVMValueRef Pointer,
724 value Name, value B) {
725 return LLVMBuildLoad(Builder_val(B), Pointer, String_val(Name));
726 }
727
728 /* llvalue -> llvalue -> llbuilder -> llvalue */
729 CAMLprim LLVMValueRef llvm_build_store(LLVMValueRef Value, LLVMValueRef Pointer,
730 value B) {
731 return LLVMBuildStore(Builder_val(B), Value, Pointer);
732 }
733
734 /* llvalue -> llvalue array -> string -> llbuilder -> llvalue */
735 CAMLprim LLVMValueRef llvm_build_gep(LLVMValueRef Pointer, value Indices,
736 value Name, value B) {
737 return LLVMBuildGEP(Builder_val(B), Pointer,
738 (LLVMValueRef *) Op_val(Indices), Wosize_val(Indices),
739 String_val(Name));
740 }
741
742 /*--... Casts ..............................................................--*/
743
744 /* llvalue -> lltype -> string -> llbuilder -> llvalue */
745 CAMLprim LLVMValueRef llvm_build_trunc(LLVMValueRef X, LLVMTypeRef Ty,
746 value Name, value B) {
747 return LLVMBuildTrunc(Builder_val(B), X, Ty, String_val(Name));
748 }
749
750 /* llvalue -> lltype -> string -> llbuilder -> llvalue */
751 CAMLprim LLVMValueRef llvm_build_zext(LLVMValueRef X, LLVMTypeRef Ty,
752 value Name, value B) {
753 return LLVMBuildZExt(Builder_val(B), X, Ty, String_val(Name));
754 }
755
756 /* llvalue -> lltype -> string -> llbuilder -> llvalue */
757 CAMLprim LLVMValueRef llvm_build_sext(LLVMValueRef X, LLVMTypeRef Ty,
758 value Name, value B) {
759 return LLVMBuildSExt(Builder_val(B), X, Ty, String_val(Name));
760 }
761
762 /* llvalue -> lltype -> string -> llbuilder -> llvalue */
763 CAMLprim LLVMValueRef llvm_build_fptoui(LLVMValueRef X, LLVMTypeRef Ty,
764 value Name, value B) {
765 return LLVMBuildFPToUI(Builder_val(B), X, Ty, String_val(Name));
766 }
767
768 /* llvalue -> lltype -> string -> llbuilder -> llvalue */
769 CAMLprim LLVMValueRef llvm_build_fptosi(LLVMValueRef X, LLVMTypeRef Ty,
770 value Name, value B) {
771 return LLVMBuildFPToSI(Builder_val(B), X, Ty, String_val(Name));
772 }
773
774 /* llvalue -> lltype -> string -> llbuilder -> llvalue */
775 CAMLprim LLVMValueRef llvm_build_uitofp(LLVMValueRef X, LLVMTypeRef Ty,
776 value Name, value B) {
777 return LLVMBuildUIToFP(Builder_val(B), X, Ty, String_val(Name));
778 }
779
780 /* llvalue -> lltype -> string -> llbuilder -> llvalue */
781 CAMLprim LLVMValueRef llvm_build_sitofp(LLVMValueRef X, LLVMTypeRef Ty,
782 value Name, value B) {
783 return LLVMBuildSIToFP(Builder_val(B), X, Ty, String_val(Name));
784 }
785
786 /* llvalue -> lltype -> string -> llbuilder -> llvalue */
787 CAMLprim LLVMValueRef llvm_build_fptrunc(LLVMValueRef X, LLVMTypeRef Ty,
788 value Name, value B) {
789 return LLVMBuildFPTrunc(Builder_val(B), X, Ty, String_val(Name));
790 }
791
792 /* llvalue -> lltype -> string -> llbuilder -> llvalue */
793 CAMLprim LLVMValueRef llvm_build_fpext(LLVMValueRef X, LLVMTypeRef Ty,
794 value Name, value B) {
795 return LLVMBuildFPExt(Builder_val(B), X, Ty, String_val(Name));
796 }
797
798 /* llvalue -> lltype -> string -> llbuilder -> llvalue */
799 CAMLprim LLVMValueRef llvm_build_prttoint(LLVMValueRef X, LLVMTypeRef Ty,
800 value Name, value B) {
801 return LLVMBuildPtrToInt(Builder_val(B), X, Ty, String_val(Name));
802 }
803
804 /* llvalue -> lltype -> string -> llbuilder -> llvalue */
805 CAMLprim LLVMValueRef llvm_build_inttoptr(LLVMValueRef X, LLVMTypeRef Ty,
806 value Name, value B) {
807 return LLVMBuildIntToPtr(Builder_val(B), X, Ty, String_val(Name));
808 }
809
810 /* llvalue -> lltype -> string -> llbuilder -> llvalue */
811 CAMLprim LLVMValueRef llvm_build_bitcast(LLVMValueRef X, LLVMTypeRef Ty,
812 value Name, value B) {
813 return LLVMBuildBitCast(Builder_val(B), X, Ty, String_val(Name));
814 }
815
816 /*--... Comparisons ........................................................--*/
817
818 /* int_predicate -> llvalue -> llvalue -> string -> llbuilder -> llvalue */
819 CAMLprim LLVMValueRef llvm_build_icmp(value Pred,
820 LLVMValueRef LHS, LLVMValueRef RHS,
821 value Name, value B) {
822 return LLVMBuildICmp(Builder_val(B), Int_val(Pred) + LLVMIntEQ, LHS, RHS,
823 String_val(Name));
824 }
825
826 /* real_predicate -> llvalue -> llvalue -> string -> llbuilder -> llvalue */
827 CAMLprim LLVMValueRef llvm_build_fcmp(value Pred,
828 LLVMValueRef LHS, LLVMValueRef RHS,
829 value Name, value B) {
830 return LLVMBuildFCmp(Builder_val(B), Int_val(Pred), LHS, RHS,
831 String_val(Name));
832 }
833
834 /*--... Miscellaneous instructions .........................................--*/
835
836 /* lltype -> string -> llbuilder -> llvalue */
837 CAMLprim LLVMValueRef llvm_build_phi(LLVMTypeRef Ty,
838 value Name, value B) {
839 return LLVMBuildPhi(Builder_val(B), Ty, String_val(Name));
840 }
841
842 /* llvalue -> llvalue array -> string -> llbuilder -> llvalue */
843 CAMLprim LLVMValueRef llvm_build_call(LLVMValueRef Fn, value Params,
844 value Name, value B) {
845 return LLVMBuildCall(Builder_val(B), Fn, (LLVMValueRef *) Op_val(Params),
846 Wosize_val(Params), String_val(Name));
847 }
848
849 /* llvalue -> llvalue -> llvalue -> string -> llbuilder -> llvalue */
850 CAMLprim LLVMValueRef llvm_build_select(LLVMValueRef If,
851 LLVMValueRef Then, LLVMValueRef Else,
852 value Name, value B) {
853 return LLVMBuildSelect(Builder_val(B), If, Then, Else, String_val(Name));
854 }
855
856 /* llvalue -> lltype -> string -> llbuilder -> llvalue */
857 CAMLprim LLVMValueRef llvm_build_va_arg(LLVMValueRef List, LLVMTypeRef Ty,
858 value Name, value B) {
859 return LLVMBuildVAArg(Builder_val(B), List, Ty, String_val(Name));
860 }
861
862 /* llvalue -> llvalue -> string -> llbuilder -> llvalue */
863 CAMLprim LLVMValueRef llvm_build_extractelement(LLVMValueRef Vec,
864 LLVMValueRef Idx,
865 value Name, value B) {
866 return LLVMBuildExtractElement(Builder_val(B), Vec, Idx, String_val(Name));
867 }
868
869 /* llvalue -> llvalue -> llvalue -> string -> llbuilder -> llvalue */
870 CAMLprim LLVMValueRef llvm_build_insertelement(LLVMValueRef Vec,
871 LLVMValueRef Element,
872 LLVMValueRef Idx,
873 value Name, value B) {
874 return LLVMBuildInsertElement(Builder_val(B), Vec, Element, Idx,
875 String_val(Name));
876 }
877
878 /* llvalue -> llvalue -> llvalue -> string -> llbuilder -> llvalue */
879 CAMLprim LLVMValueRef llvm_build_shufflevector(LLVMValueRef V1, LLVMValueRef V2,
880 LLVMValueRef Mask,
881 value Name, value B) {
882 return LLVMBuildShuffleVector(Builder_val(B), V1, V2, Mask, String_val(Name));
883 }
884
8888 inline LLVMValueRef *wrap(const Value **Vals) {
8989 return reinterpret_cast(const_cast(Vals));
9090 }
91
92 /// Basic block conversions
93 ///
94 inline BasicBlock *unwrap(LLVMBasicBlockRef BBRef) {
95 return reinterpret_cast(BBRef);
96 }
97
98 inline LLVMBasicBlockRef wrap(const BasicBlock *BB) {
99 return reinterpret_cast(const_cast(BB));
100 }
91101 }
92102
93103 #endif
3636 typedef struct LLVMOpaqueModule *LLVMModuleRef;
3737 typedef struct LLVMOpaqueType *LLVMTypeRef;
3838 typedef struct LLVMOpaqueValue *LLVMValueRef;
39
40 typedef enum {
41 LLVMVoidTypeKind = 0, /* type with no size */
39 typedef struct LLVMOpaqueBasicBlock *LLVMBasicBlockRef;
40 typedef struct LLVMOpaqueBuilder *LLVMBuilderRef;
41
42 typedef enum {
43 LLVMVoidTypeKind, /* type with no size */
4244 LLVMFloatTypeKind, /* 32 bit floating point type */
4345 LLVMDoubleTypeKind, /* 64 bit floating point type */
4446 LLVMX86_FP80TypeKind, /* 80 bit floating point type (X87) */
5557 } LLVMTypeKind;
5658
5759 typedef enum {
58 LLVMExternalLinkage = 0,/* Externally visible function */
60 LLVMExternalLinkage, /* Externally visible function */
5961 LLVMLinkOnceLinkage, /* Keep one copy of function when linking (inline) */
6062 LLVMWeakLinkage, /* Keep one copy of function when linking (weak) */
6163 LLVMAppendingLinkage, /* Special purpose, only applies to global arrays */
6769 } LLVMLinkage;
6870
6971 typedef enum {
70 LLVMDefaultVisibility = 0, /* The GV is visible */
71 LLVMHiddenVisibility, /* The GV is hidden */
72 LLVMProtectedVisibility /* The GV is protected */
72 LLVMDefaultVisibility, /* The GV is visible */
73 LLVMHiddenVisibility, /* The GV is hidden */
74 LLVMProtectedVisibility /* The GV is protected */
7375 } LLVMVisibility;
76
77 typedef enum {
78 LLVMCCallConv = 0,
79 LLVMFastCallConv = 8,
80 LLVMColdCallConv = 9,
81 LLVMX86StdcallCallConv = 64,
82 LLVMX86FastcallCallConv = 65
83 } LLVMCallConv;
84
85 typedef enum {
86 LLVMIntEQ = 32, /* equal */
87 LLVMIntNE, /* not equal */
88 LLVMIntUGT, /* unsigned greater than */
89 LLVMIntUGE, /* unsigned greater or equal */
90 LLVMIntULT, /* unsigned less than */
91 LLVMIntULE, /* unsigned less or equal */
92 LLVMIntSGT, /* signed greater than */
93 LLVMIntSGE, /* signed greater or equal */
94 LLVMIntSLT, /* signed less than */
95 LLVMIntSLE /* signed less or equal */
96 } LLVMIntPredicate;
97
98 typedef enum {
99 LLVMRealPredicateFalse, /* Always false (always folded) */
100 LLVMRealOEQ, /* True if ordered and equal */
101 LLVMRealOGT, /* True if ordered and greater than */
102 LLVMRealOGE, /* True if ordered and greater than or equal */
103 LLVMRealOLT, /* True if ordered and less than */
104 LLVMRealOLE, /* True if ordered and less than or equal */
105 LLVMRealONE, /* True if ordered and operands are unequal */
106 LLVMRealORD, /* True if ordered (no nans) */
107 LLVMRealUNO, /* True if unordered: isnan(X) | isnan(Y) */
108 LLVMRealUEQ, /* True if unordered or equal */
109 LLVMRealUGT, /* True if unordered or greater than */
110 LLVMRealUGE, /* True if unordered, greater than, or equal */
111 LLVMRealULT, /* True if unordered or less than */
112 LLVMRealULE, /* True if unordered, less than, or equal */
113 LLVMRealUNE, /* True if unordered or not equal */
114 LLVMRealPredicateTrue /* Always true (always folded) */
115 } LLVMRealPredicate;
74116
75117
76118 /*===-- Modules -----------------------------------------------------------===*/
81123
82124 /* Same as Module::addTypeName. */
83125 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty);
84 int LLVMDeleteTypeName(LLVMModuleRef M, const char *Name);
85
86
87 /*===-- Types --------------------------------------------------------------===*/
126 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name);
127
128
129 /*===-- Types -------------------------------------------------------------===*/
88130
89131 /* LLVM types conform to the following hierarchy:
90132 *
110152 LLVMTypeRef LLVMInt16Type();
111153 LLVMTypeRef LLVMInt32Type();
112154 LLVMTypeRef LLVMInt64Type();
113 LLVMTypeRef LLVMCreateIntegerType(unsigned NumBits);
114 unsigned LLVMGetIntegerTypeWidth(LLVMTypeRef IntegerTy);
155 LLVMTypeRef LLVMCreateIntType(unsigned NumBits);
156 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy);
115157
116158 /* Operations on real types */
117159 LLVMTypeRef LLVMFloatType();
125167 LLVMTypeRef *ParamTypes, unsigned ParamCount,
126168 int IsVarArg);
127169 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy);
128 LLVMTypeRef LLVMGetFunctionReturnType(LLVMTypeRef FunctionTy);
129 unsigned LLVMGetFunctionParamCount(LLVMTypeRef FunctionTy);
130 void LLVMGetFunctionParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest);
170 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy);
171 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy);
172 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest);
131173
132174 /* Operations on struct types */
133175 LLVMTypeRef LLVMCreateStructType(LLVMTypeRef *ElementTypes,
134176 unsigned ElementCount, int Packed);
135 unsigned LLVMGetStructElementCount(LLVMTypeRef StructTy);
177 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy);
136178 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest);
137179 int LLVMIsPackedStruct(LLVMTypeRef StructTy);
138180
164206 * global variable
165207 * function
166208 * alias
209 * basic blocks
167210 */
168211
169212 /* Operations on all values */
170 LLVMTypeRef LLVMGetTypeOfValue(LLVMValueRef Val);
213 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val);
171214 const char *LLVMGetValueName(LLVMValueRef Val);
172215 void LLVMSetValueName(LLVMValueRef Val, const char *Name);
173216
214257 int LLVMIsThreadLocal(LLVMValueRef GlobalVar);
215258 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal);
216259
260 /* Operations on functions */
261 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
262 LLVMTypeRef FunctionTy);
263 void LLVMDeleteFunction(LLVMValueRef Fn);
264 unsigned LLVMCountParams(LLVMValueRef Fn);
265 void LLVMGetParams(LLVMValueRef Fn, LLVMValueRef *Params);
266 LLVMValueRef LLVMGetParam(LLVMValueRef Fn, unsigned Index);
267 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn);
268 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn);
269 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC);
270
271 /* Operations on basic blocks */
272 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb);
273 int LLVMValueIsBasicBlock(LLVMValueRef Val);
274 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val);
275 unsigned LLVMCountBasicBlocks(LLVMValueRef Fn);
276 void LLVMGetBasicBlocks(LLVMValueRef Fn, LLVMBasicBlockRef *BasicBlocks);
277 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn);
278 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef Fn, const char *Name);
279 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBB,
280 const char *Name);
281 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BB);
282
283
284 /*===-- Instruction builders ----------------------------------------------===*/
285
286 /* An instruction builder represents a point within a basic block, and is the
287 * exclusive means of building instructions using the C interface.
288 */
289
290 LLVMBuilderRef LLVMCreateBuilder();
291 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr);
292 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block);
293 void LLVMDisposeBuilder(LLVMBuilderRef Builder);
294
295 /* Terminators */
296 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef);
297 LLVMValueRef LLVMBuildRet(LLVMBuilderRef, LLVMValueRef V);
298 LLVMValueRef LLVMBuildBr(LLVMBuilderRef, LLVMBasicBlockRef Dest);
299 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef, LLVMValueRef If,
300 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else);
301 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef, LLVMValueRef V,
302 LLVMBasicBlockRef Else, unsigned NumCases);
303 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef, LLVMValueRef Fn,
304 LLVMValueRef *Args, unsigned NumArgs,
305 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
306 const char *Name);
307 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef);
308 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef);
309
310 /* Arithmetic */
311 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
312 const char *Name);
313 LLVMValueRef LLVMBuildSub(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
314 const char *Name);
315 LLVMValueRef LLVMBuildMul(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
316 const char *Name);
317 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
318 const char *Name);
319 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
320 const char *Name);
321 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
322 const char *Name);
323 LLVMValueRef LLVMBuildURem(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
324 const char *Name);
325 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
326 const char *Name);
327 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
328 const char *Name);
329 LLVMValueRef LLVMBuildShl(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
330 const char *Name);
331 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
332 const char *Name);
333 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
334 const char *Name);
335 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
336 const char *Name);
337 LLVMValueRef LLVMBuildOr(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
338 const char *Name);
339 LLVMValueRef LLVMBuildXor(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
340 const char *Name);
341 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef, LLVMValueRef V, const char *Name);
342 LLVMValueRef LLVMBuildNot(LLVMBuilderRef, LLVMValueRef V, const char *Name);
343
344 /* Memory */
345 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
346 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef, LLVMTypeRef Ty,
347 LLVMValueRef Val, const char *Name);
348 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
349 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef, LLVMTypeRef Ty,
350 LLVMValueRef Val, const char *Name);
351 LLVMValueRef LLVMBuildFree(LLVMBuilderRef, LLVMValueRef PointerVal);
352 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef, LLVMValueRef PointerVal,
353 const char *Name);
354 LLVMValueRef LLVMBuildStore(LLVMBuilderRef, LLVMValueRef Val, LLVMValueRef Ptr);
355 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
356 LLVMValueRef *Indices, unsigned NumIndices,
357 const char *Name);
358
359 /* Casts */
360 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef, LLVMValueRef Val,
361 LLVMTypeRef DestTy, const char *Name);
362 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef, LLVMValueRef Val,
363 LLVMTypeRef DestTy, const char *Name);
364 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef, LLVMValueRef Val,
365 LLVMTypeRef DestTy, const char *Name);
366 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef, LLVMValueRef Val,
367 LLVMTypeRef DestTy, const char *Name);
368 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef, LLVMValueRef Val,
369 LLVMTypeRef DestTy, const char *Name);
370 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef, LLVMValueRef Val,
371 LLVMTypeRef DestTy, const char *Name);
372 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef, LLVMValueRef Val,
373 LLVMTypeRef DestTy, const char *Name);
374 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef, LLVMValueRef Val,
375 LLVMTypeRef DestTy, const char *Name);
376 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef, LLVMValueRef Val,
377 LLVMTypeRef DestTy, const char *Name);
378 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef, LLVMValueRef Val,
379 LLVMTypeRef DestTy, const char *Name);
380 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef, LLVMValueRef Val,
381 LLVMTypeRef DestTy, const char *Name);
382 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef, LLVMValueRef Val,
383 LLVMTypeRef DestTy, const char *Name);
384
385 /* Comparisons */
386 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef, LLVMIntPredicate Op,
387 LLVMValueRef LHS, LLVMValueRef RHS,
388 const char *Name);
389 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef, LLVMRealPredicate Op,
390 LLVMValueRef LHS, LLVMValueRef RHS,
391 const char *Name);
392
393 /* Miscellaneous instructions */
394 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
395 LLVMValueRef LLVMBuildCall(LLVMBuilderRef, LLVMValueRef Fn,
396 LLVMValueRef *Args, unsigned NumArgs,
397 const char *Name);
398 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef, LLVMValueRef If,
399 LLVMValueRef Then, LLVMValueRef Else,
400 const char *Name);
401 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef, LLVMValueRef List, LLVMTypeRef Ty,
402 const char *Name);
403 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef, LLVMValueRef VecVal,
404 LLVMValueRef Index, const char *Name);
405 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef, LLVMValueRef VecVal,
406 LLVMValueRef EltVal, LLVMValueRef Index,
407 const char *Name);
408 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef, LLVMValueRef V1,
409 LLVMValueRef V2, LLVMValueRef Mask,
410 const char *Name);
217411
218412 #ifdef __cplusplus
219413 }
1717 #include "llvm/Constants.h"
1818 #include "llvm/DerivedTypes.h"
1919 #include "llvm/GlobalVariable.h"
20 #include
21 #include
20 #include "llvm/Support/LLVMBuilder.h"
21 #include "llvm/TypeSymbolTable.h"
2222 #include
2323
2424 using namespace llvm;
25
26 namespace {
27 /// Opaque builder conversions.
28 ///
29 inline LLVMBuilder *unwrap(LLVMBuilderRef B) {
30 return reinterpret_cast(B);
31 }
32
33 inline LLVMBuilderRef wrap(LLVMBuilder *B) {
34 return reinterpret_cast(B);
35 }
36 }
2537
2638
2739 /*===-- Operations on modules ---------------------------------------------===*/
3648
3749 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
3850 return unwrap(M)->addTypeName(Name, unwrap(Ty));
51 }
52
53 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
54 std::string N(Name);
55
56 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
57 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
58 if (I->first == N)
59 TST.remove(I);
3960 }
4061
4162
6081 LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
6182 LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
6283
63 LLVMTypeRef LLVMCreateIntegerType(unsigned NumBits) {
84 LLVMTypeRef LLVMCreateIntType(unsigned NumBits) {
6485 return wrap(IntegerType::get(NumBits));
6586 }
6687
67 unsigned LLVMGetIntegerTypeWidth(LLVMTypeRef IntegerTy) {
88 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
6889 return unwrap(IntegerTy)->getBitWidth();
6990 }
7091
92113 return unwrap(FunctionTy)->isVarArg();
93114 }
94115
95 LLVMTypeRef LLVMGetFunctionReturnType(LLVMTypeRef FunctionTy) {
116 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
96117 return wrap(unwrap(FunctionTy)->getReturnType());
97118 }
98119
99 unsigned LLVMGetFunctionParamCount(LLVMTypeRef FunctionTy) {
120 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
100121 return unwrap(FunctionTy)->getNumParams();
101122 }
102123
103 void LLVMGetFunctionParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
124 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
104125 FunctionType *Ty = unwrap(FunctionTy);
105126 for (FunctionType::param_iterator I = Ty->param_begin(),
106127 E = Ty->param_end(); I != E; ++I)
119140 return wrap(StructType::get(Tys, Packed != 0));
120141 }
121142
122 unsigned LLVMGetStructElementCount(LLVMTypeRef StructTy) {
143 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
123144 return unwrap(StructTy)->getNumElements();
124145 }
125146
174195
175196 /*--.. Operations on all values ............................................--*/
176197
177 LLVMTypeRef LLVMGetTypeOfValue(LLVMValueRef Val) {
198 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
178199 return wrap(unwrap(Val)->getType());
179200 }
180201
327348 unwrap(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
328349 }
329350
351 /*--.. Operations on functions .............................................--*/
352
353 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
354 LLVMTypeRef FunctionTy) {
355 return wrap(new Function(unwrap(FunctionTy),
356 GlobalValue::ExternalLinkage, Name, unwrap(M)));
357 }
358
359 void LLVMDeleteFunction(LLVMValueRef Fn) {
360 unwrap(Fn)->eraseFromParent();
361 }
362
363 unsigned LLVMCountParams(LLVMValueRef FnRef) {
364 // This function is strictly redundant to
365 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
366 return unwrap(FnRef)->getArgumentList().size();
367 }
368
369 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
370 Function::arg_iterator AI = unwrap(FnRef)->arg_begin();
371 while (index --> 0)
372 AI++;
373 return wrap(AI);
374 }
375
376 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
377 Function *Fn = unwrap(FnRef);
378 for (Function::arg_iterator I = Fn->arg_begin(),
379 E = Fn->arg_end(); I != E; I++)
380 *ParamRefs++ = wrap(I);
381 }
382
383 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
384 if (Function *F = dyn_cast(unwrap(Fn)))
385 return F->getIntrinsicID();
386 return 0;
387 }
388
389 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
390 return unwrap(Fn)->getCallingConv();
391 }
392
393 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
394 return unwrap(Fn)->setCallingConv(CC);
395 }
396
397 /*--.. Operations on basic blocks ..........................................--*/
398
399 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
400 return wrap(static_cast(unwrap(Bb)));
401 }
402
403 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
404 return isa(unwrap(Val));
405 }
406
407 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
408 return wrap(unwrap(Val));
409 }
410
411 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
412 return unwrap(FnRef)->getBasicBlockList().size();
413 }
414
415 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
416 Function *Fn = unwrap(FnRef);
417 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
418 *BasicBlocksRefs++ = wrap(I);
419 }
420
421 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
422 return wrap(&unwrap(Fn)->getEntryBlock());
423 }
424
425 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
426 return wrap(new BasicBlock(Name, unwrap(FnRef)));
427 }
428
429 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
430 const char *Name) {
431 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
432 return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
433 InsertBeforeBB));
434 }
435
436 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
437 unwrap(BBRef)->eraseFromParent();
438 }
439
440 /*--.. Call and invoke instructions ........................................--*/
441
442 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
443 Value *V = unwrap(Instr);
444 if (CallInst *CI = dyn_cast(V))
445 return CI->getCallingConv();
446 else if (InvokeInst *II = dyn_cast(V))
447 return II->getCallingConv();
448 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
449 return 0;
450 }
451
452 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
453 Value *V = unwrap(Instr);
454 if (CallInst *CI = dyn_cast(V))
455 return CI->setCallingConv(CC);
456 else if (InvokeInst *II = dyn_cast(V))
457 return II->setCallingConv(CC);
458 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
459 }
460
461
462 /*===-- Instruction builders ----------------------------------------------===*/
463
464 LLVMBuilderRef LLVMCreateBuilder() {
465 return wrap(new LLVMBuilder());
466 }
467
468 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
469 Instruction *I = unwrap(Instr);
470 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
471 }
472
473 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
474 BasicBlock *BB = unwrap(Block);
475 unwrap(Builder)->SetInsertPoint(BB);
476 }
477
478 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
479 delete unwrap(Builder);
480 }
481
482 /*--.. Instruction builders ................................................--*/
483
484 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
485 return wrap(unwrap(B)->CreateRetVoid());
486 }
487
488 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
489 return wrap(unwrap(B)->CreateRet(unwrap(V)));
490 }
491
492 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
493 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
494 }
495
496 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
497 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
498 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
499 }
500
501 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
502 LLVMBasicBlockRef Else, unsigned NumCases) {
503 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
504 }
505
506 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
507 LLVMValueRef *Args, unsigned NumArgs,
508 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
509 const char *Name) {
510 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
511 unwrap(Args), unwrap(Args) + NumArgs,
512 Name));
513 }
514
515 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
516 return wrap(unwrap(B)->CreateUnwind());
517 }
518
519 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
520 return wrap(unwrap(B)->CreateUnreachable());
521 }
522
523 /*--.. Arithmetic ..........................................................--*/
524
525 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
526 const char *Name) {
527 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
528 }
529
530 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
531 const char *Name) {
532 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
533 }
534
535 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
536 const char *Name) {
537 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
538 }
539
540 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
541 const char *Name) {
542 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
543 }
544
545 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
546 const char *Name) {
547 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
548 }
549
550 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
551 const char *Name) {
552 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
553 }
554
555 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
556 const char *Name) {
557 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
558 }
559
560 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
561 const char *Name) {
562 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
563 }
564
565 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
566 const char *Name) {
567 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
568 }
569
570 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
571 const char *Name) {
572 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
573 }
574
575 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
576 const char *Name) {
577 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
578 }
579
580 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
581 const char *Name) {
582 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
583 }
584
585 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
586 const char *Name) {
587 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
588 }
589
590 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
591 const char *Name) {
592 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
593 }
594
595 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
596 const char *Name) {
597 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
598 }
599
600 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
601 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
602 }
603
604 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
605 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
606 }
607
608 /*--.. Memory ..............................................................--*/
609
610 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
611 const char *Name) {
612 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
613 }
614
615 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
616 LLVMValueRef Val, const char *Name) {
617 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
618 }
619
620 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
621 const char *Name) {
622 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
623 }
624
625 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
626 LLVMValueRef Val, const char *Name) {
627 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
628 }
629
630 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
631 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
632 }
633
634
635 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
636 const char *Name) {
637 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
638 }
639
640 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
641 LLVMValueRef PointerVal) {
642 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
643 }
644
645 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
646 LLVMValueRef *Indices, unsigned NumIndices,
647 const char *Name) {
648 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
649 unwrap(Indices) + NumIndices, Name));
650 }
651
652 /*--.. Casts ...............................................................--*/
653
654 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
655 LLVMTypeRef DestTy, const char *Name) {
656 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
657 }
658
659 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
660 LLVMTypeRef DestTy, const char *Name) {
661 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
662 }
663
664 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
665 LLVMTypeRef DestTy, const char *Name) {
666 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
667 }
668
669 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
670 LLVMTypeRef DestTy, const char *Name) {
671 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
672 }
673
674 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
675 LLVMTypeRef DestTy, const char *Name) {
676 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
677 }
678
679 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
680 LLVMTypeRef DestTy, const char *Name) {
681 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
682 }
683
684 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
685 LLVMTypeRef DestTy, const char *Name) {
686 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
687 }
688
689 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
690 LLVMTypeRef DestTy, const char *Name) {
691 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
692 }
693
694 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
695 LLVMTypeRef DestTy, const char *Name) {
696 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
697 }
698
699 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
700 LLVMTypeRef DestTy, const char *Name) {
701 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
702 }
703
704 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
705 LLVMTypeRef DestTy, const char *Name) {
706 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
707 }
708
709 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
710 LLVMTypeRef DestTy, const char *Name) {
711 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
712 }
713
714 /*--.. Comparisons .........................................................--*/
715
716 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
717 LLVMValueRef LHS, LLVMValueRef RHS,
718 const char *Name) {
719 return wrap(unwrap(B)->CreateICmp(static_cast(Op),
720 unwrap(LHS), unwrap(RHS), Name));
721 }
722
723 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
724 LLVMValueRef LHS, LLVMValueRef RHS,
725 const char *Name) {
726 return wrap(unwrap(B)->CreateFCmp(static_cast(Op),
727 unwrap(LHS), unwrap(RHS), Name));
728 }
729
730 /*--.. Miscellaneous instructions ..........................................--*/
731
732 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
733 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
734 }
735
736 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
737 LLVMValueRef *Args, unsigned NumArgs,
738 const char *Name) {
739 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
740 unwrap(Args) + NumArgs, Name));
741 }
742
743 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
744 LLVMValueRef Then, LLVMValueRef Else,
745 const char *Name) {
746 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
747 Name));
748 }
749
750 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
751 LLVMTypeRef Ty, const char *Name) {
752 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
753 }
754
755 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
756 LLVMValueRef Index, const char *Name) {
757 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
758 Name));
759 }
760
761 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
762 LLVMValueRef EltVal, LLVMValueRef Index,
763 const char *Name) {
764 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
765 unwrap(Index), Name));
766 }
767
768 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
769 LLVMValueRef V2, LLVMValueRef Mask,
770 const char *Name) {
771 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
772 unwrap(Mask), Name));
773 }
1010 let _ =
1111 let m = Llvm.create_module "ocaml_test_module" in
1212
13 ignore (Llvm.add_type_name "caml_int_ty" Llvm.i32_type m);
13 ignore (Llvm.define_type_name "caml_int_ty" Llvm.i32_type m);
1414
1515 test (Llvm_bitwriter.write_bitcode_file m Sys.argv.(1))
4343 (* RUN: grep {Ty01.*void} < %t.ll
4444 *)
4545 group "void";
46 insist (add_type_name "Ty01" void_type m);
46 insist (define_type_name "Ty01" void_type m);
4747 insist (Void_type == classify_type void_type);
4848
4949 (* RUN: grep {Ty02.*i1} < %t.ll
5050 *)
5151 group "i1";
52 insist (add_type_name "Ty02" i1_type m);
52 insist (define_type_name "Ty02" i1_type m);
5353 insist (Integer_type == classify_type i1_type);
5454
5555 (* RUN: grep {Ty03.*i32} < %t.ll
5656 *)
5757 group "i32";
58 insist (add_type_name "Ty03" i32_type m);
58 insist (define_type_name "Ty03" i32_type m);
5959
6060 (* RUN: grep {Ty04.*i42} < %t.ll
6161 *)
6262 group "i42";
6363 let ty = make_integer_type 42 in
64 insist (add_type_name "Ty04" ty m);
64 insist (define_type_name "Ty04" ty m);
6565
6666 (* RUN: grep {Ty05.*float} < %t.ll
6767 *)
6868 group "float";
69 insist (add_type_name "Ty05" float_type m);
69 insist (define_type_name "Ty05" float_type m);
7070 insist (Float_type == classify_type float_type);
7171
7272 (* RUN: grep {Ty06.*double} < %t.ll
7373 *)
7474 group "double";
75 insist (add_type_name "Ty06" double_type m);
75 insist (define_type_name "Ty06" double_type m);
7676 insist (Double_type == classify_type double_type);
7777
7878 (* RUN: grep {Ty07.*i32.*i1, double} < %t.ll
7979 *)
8080 group "function";
8181 let ty = make_function_type i32_type [| i1_type; double_type |] false in
82 insist (add_type_name "Ty07" ty m);
82 insist (define_type_name "Ty07" ty m);
8383 insist (Function_type = classify_type ty);
8484 insist (not (is_var_arg ty));
8585 insist (i32_type == return_type ty);
8989 *)
9090 group "vararg";
9191 let ty = make_function_type void_type [| i32_type |] true in
92 insist (add_type_name "Ty08" ty m);
92 insist (define_type_name "Ty08" ty m);
9393 insist (is_var_arg ty);
9494
9595 (* RUN: grep {Ty09.*\\\[7 x i8\\\]} < %t.ll
9696 *)
9797 group "array";
9898 let ty = make_array_type i8_type 7 in
99 insist (add_type_name "Ty09" ty m);
99 insist (define_type_name "Ty09" ty m);
100100 insist (7 = array_length ty);
101101 insist (i8_type == element_type ty);
102102 insist (Array_type == classify_type ty);
105105 *)
106106 group "pointer";
107107 let ty = make_pointer_type float_type in
108 insist (add_type_name "Ty10" ty m);
108 insist (define_type_name "Ty10" ty m);
109109 insist (float_type == element_type ty);
110110 insist (Pointer_type == classify_type ty);
111111
113113 *)
114114 group "vector";
115115 let ty = make_vector_type i16_type 4 in
116 insist (add_type_name "Ty11" ty m);
116 insist (define_type_name "Ty11" ty m);
117117 insist (i16_type == element_type ty);
118118 insist (4 = vector_size ty);
119119
121121 *)
122122 group "opaque";
123123 let ty = make_opaque_type () in
124 insist (add_type_name "Ty12" ty m);
124 insist (define_type_name "Ty12" ty m);
125125 insist (ty == ty);
126 insist (ty <> make_opaque_type ())
126 insist (ty <> make_opaque_type ());
127
128 (* RUN: grep -v {Ty13} < %t.ll
129 *)
130 group "delete";
131 let ty = make_opaque_type () in
132 insist (define_type_name "Ty13" ty m);
133 delete_type_name "Ty13" m
127134
128135
129136 (*===-- Constants ---------------------------------------------------------===*)
162169 *)
163170 group "string w/ null";
164171 let c = make_string_constant "hi\000again" true in
165 prerr_string "====> ";
166 prerr_int (array_length (type_of c));
167 prerr_endline " <====";
168172 ignore (define_global "Const05" c m);
169173 insist ((make_array_type i8_type 9) = type_of c);
170174
305309 delete_global g
306310
307311
312 (*===-- Functions ---------------------------------------------------------===*)
313
314 let test_functions () =
315 let ty = make_function_type i32_type [| i32_type; i64_type |] false in
316 let pty = make_pointer_type ty in
317
318 (* RUN: grep {declare i32 @Fn1\(i32, i64\)} < %t.ll
319 *)
320 group "declare";
321 let fn = declare_function "Fn1" ty m in
322 insist (pty = type_of fn);
323 insist (is_declaration fn);
324 insist (0 = Array.length (basic_blocks fn));
325
326 (* RUN: grep -v {Fn2} < %t.ll
327 *)
328 group "delete";
329 let fn = declare_function "Fn2" ty m in
330 delete_function fn;
331
332 (* RUN: grep {define.*Fn3} < %t.ll
333 *)
334 group "define";
335 let fn = define_function "Fn3" ty m in
336 insist (not (is_declaration fn));
337 insist (1 = Array.length (basic_blocks fn));
338 (* this function is not valid because init bb lacks a terminator *)
339
340 (* RUN: grep {define.*Fn4.*Param1.*Param2} < %t.ll
341 *)
342 group "params";
343 let fn = define_function "Fn4" ty m in
344 let params = params fn in
345 insist (2 = Array.length params);
346 insist (params.(0) = param fn 0);
347 insist (params.(1) = param fn 1);
348 insist (i32_type = type_of params.(0));
349 insist (i64_type = type_of params.(1));
350 set_value_name "Param1" params.(0);
351 set_value_name "Param2" params.(1);
352 (* this function is not valid because init bb lacks a terminator *)
353
354 (* RUN: grep {fastcc.*Fn5} < %t.ll
355 *)
356 group "callconv";
357 let fn = define_function "Fn5" ty m in
358 insist (ccc = function_call_conv fn);
359 set_function_call_conv fastcc fn;
360 insist (fastcc = function_call_conv fn)
361
362
363 (*===-- Basic Blocks ------------------------------------------------------===*)
364
365 let test_basic_blocks () =
366 let ty = make_function_type void_type [| |] false in
367
368 (* RUN: grep {Bb1} < %t.ll
369 *)
370 group "entry";
371 let fn = declare_function "X" ty m in
372 let bb = append_block "Bb1" fn in
373 insist (bb = entry_block fn);
374
375 (* RUN: grep -v Bb2 < %t.ll
376 *)
377 group "delete";
378 let fn = declare_function "X2" ty m in
379 let bb = append_block "Bb2" fn in
380 delete_block bb;
381
382 group "insert";
383 let fn = declare_function "X3" ty m in
384 let bbb = append_block "" fn in
385 let bba = insert_block "" bbb in
386 insist ([| bba; bbb |] = basic_blocks fn);
387
388 (* RUN: grep Bb3 < %t.ll
389 *)
390 group "name/value";
391 let fn = define_function "X4" ty m in
392 let bb = entry_block fn in
393 let bbv = value_of_block bb in
394 set_value_name "Bb3" bbv;
395 insist ("Bb3" = value_name bbv);
396
397 group "casts";
398 let fn = define_function "X5" ty m in
399 let bb = entry_block fn in
400 insist (bb = block_of_value (value_of_block bb));
401 insist (value_is_block (value_of_block bb));
402 insist (not (value_is_block (make_null i32_type)))
403
404
405 (*===-- Builder -----------------------------------------------------------===*)
406
407 let test_builder () =
408 let (++) x f = f x; x in
409
410 group "ret void";
411 begin
412 (* RUN: grep {ret void} < %t.ll
413 *)
414 let fty = make_function_type void_type [| |] false in
415 let fn = declare_function "X6" fty m in
416 let b = builder_at_end (append_block "Bb01" fn) in
417 ignore (build_ret_void b)
418 end;
419
420 (* The rest of the tests will use one big function. *)
421 let fty = make_function_type i32_type [| i32_type; i32_type |] false in
422 let fn = define_function "X7" fty m in
423 let atentry = builder_at_end (entry_block fn) in
424 let p1 = param fn 0 ++ set_value_name "P1" in
425 let p2 = param fn 1 ++ set_value_name "P2" in
426 let f1 = build_uitofp p1 float_type "F1" atentry in
427 let f2 = build_uitofp p2 float_type "F2" atentry in
428
429 let bb00 = append_block "Bb00" fn in
430 ignore (build_unreachable (builder_at_end bb00));
431
432 group "ret"; begin
433 (* RUN: grep {ret.*P1} < %t.ll
434 *)
435 let ret = build_ret p1 atentry in
436 position_before ret atentry
437 end;
438
439 group "br"; begin
440 (* RUN: grep {br.*Bb02} < %t.ll
441 *)
442 let bb02 = append_block "Bb02" fn in
443 let b = builder_at_end bb02 in
444 ignore (build_br bb02 b)
445 end;
446
447 group "cond_br"; begin
448 (* RUN: grep {br.*Inst01.*Bb03.*Bb00} < %t.ll
449 *)
450 let bb03 = append_block "Bb03" fn in
451 let b = builder_at_end bb03 in
452 let cond = build_trunc p1 i1_type "Inst01" b in
453 ignore (build_cond_br cond bb03 bb00 b)
454 end;
455
456 (* TODO: Switch *)
457
458 group "invoke"; begin
459 (* RUN: grep {Inst02.*invoke.*P1.*P2} < %t.ll
460 * RUN: grep {to.*Bb04.*unwind.*Bb00} < %t.ll
461 *)
462 let bb04 = append_block "Bb04" fn in
463 let b = builder_at_end bb04 in
464 ignore (build_invoke fn [| p1; p2 |] bb04 bb00 "Inst02" b)
465 end;
466
467 group "unwind"; begin
468 (* RUN: grep {unwind} < %t.ll
469 *)
470 let bb05 = append_block "Bb05" fn in
471 let b = builder_at_end bb05 in
472 ignore (build_unwind b)
473 end;
474
475 group "unreachable"; begin
476 (* RUN: grep {unreachable} < %t.ll
477 *)
478 let bb06 = append_block "Bb06" fn in
479 let b = builder_at_end bb06 in
480 ignore (build_unreachable b)
481 end;
482
483 group "arithmetic"; begin
484 let bb07 = append_block "Bb07" fn in
485 let b = builder_at_end bb07 in
486
487 (* RUN: grep {Inst03.*add.*P1.*P2} < %t.ll
488 * RUN: grep {Inst04.*sub.*P1.*Inst03} < %t.ll
489 * RUN: grep {Inst05.*mul.*P1.*Inst04} < %t.ll
490 * RUN: grep {Inst06.*udiv.*P1.*Inst05} < %t.ll
491 * RUN: grep {Inst07.*sdiv.*P1.*Inst06} < %t.ll
492 * RUN: grep {Inst08.*fdiv.*F1.*F2} < %t.ll
493 * RUN: grep {Inst09.*urem.*P1.*Inst07} < %t.ll
494 * RUN: grep {Inst10.*srem.*P1.*Inst09} < %t.ll
495 * RUN: grep {Inst11.*frem.*F1.*Inst08} < %t.ll
496 * RUN: grep {Inst12.*shl.*P1.*Inst10} < %t.ll
497 * RUN: grep {Inst13.*lshr.*P1.*Inst12} < %t.ll
498 * RUN: grep {Inst14.*ashr.*P1.*Inst13} < %t.ll
499 * RUN: grep {Inst15.*and.*P1.*Inst14} < %t.ll
500 * RUN: grep {Inst16.*or.*P1.*Inst15} < %t.ll
501 * RUN: grep {Inst17.*xor.*P1.*Inst16} < %t.ll
502 * RUN: grep {Inst18.*sub.*0.*Inst17} < %t.ll
503 * RUN: grep {Inst19.*xor.*Inst18.*-1} < %t.ll
504 *)
505 let inst03 = build_add p1 p2 "Inst03" b in
506 let inst04 = build_sub p1 inst03 "Inst04" b in
507 let inst05 = build_mul p1 inst04 "Inst05" b in
508 let inst06 = build_udiv p1 inst05 "Inst06" b in
509 let inst07 = build_sdiv p1 inst06 "Inst07" b in
510 let inst08 = build_fdiv f1 f2 "Inst08" b in
511 let inst09 = build_urem p1 inst07 "Inst09" b in
512 let inst10 = build_srem p1 inst09 "Inst10" b in
513 ignore(build_frem f1 inst08 "Inst11" b);
514 let inst12 = build_shl p1 inst10 "Inst12" b in
515 let inst13 = build_lshr p1 inst12 "Inst13" b in
516 let inst14 = build_ashr p1 inst13 "Inst14" b in
517 let inst15 = build_and p1 inst14 "Inst15" b in
518 let inst16 = build_or p1 inst15 "Inst16" b in
519 let inst17 = build_xor p1 inst16 "Inst17" b in
520 let inst18 = build_neg inst17 "Inst18" b in
521 ignore (build_not inst18 "Inst19" b)
522 end;
523
524 group "memory"; begin
525 let bb08 = append_block "Bb08" fn in
526 let b = builder_at_end bb08 in
527
528 (* RUN: grep {Inst20.*malloc.*i8 } < %t.ll
529 * RUN: grep {Inst21.*malloc.*i8.*P1} < %t.ll
530 * RUN: grep {Inst22.*alloca.*i32 } < %t.ll
531 * RUN: grep {Inst23.*alloca.*i32.*P2} < %t.ll
532 * RUN: grep {free.*Inst20} < %t.ll
533 * RUN: grep {Inst25.*load.*Inst21} < %t.ll
534 * RUN: grep {store.*P2.*Inst22} < %t.ll
535 * RUN: grep {Inst27.*getelementptr.*Inst23.*P2} < %t.ll
536 *)
537 let inst20 = build_malloc i8_type "Inst20" b in
538 let inst21 = build_array_malloc i8_type p1 "Inst21" b in
539 let inst22 = build_alloca i32_type "Inst22" b in
540 let inst23 = build_array_alloca i32_type p2 "Inst23" b in
541 ignore(build_free inst20 b);
542 ignore(build_load inst21 "Inst25" b);
543 ignore(build_store p2 inst22 b);
544 ignore(build_gep inst23 [| p2 |] "Inst27" b)
545 end;
546
547 group "casts"; begin
548 let void_ptr = make_pointer_type i8_type in
549
550 (* RUN: grep {Inst28.*trunc.*P1.*i8} < %t.ll
551 * RUN: grep {Inst29.*zext.*Inst28.*i32} < %t.ll
552 * RUN: grep {Inst30.*sext.*Inst29.*i64} < %t.ll
553 * RUN: grep {Inst31.*uitofp.*Inst30.*float} < %t.ll
554 * RUN: grep {Inst32.*sitofp.*Inst29.*double} < %t.ll
555 * RUN: grep {Inst33.*fptoui.*Inst31.*i32} < %t.ll
556 * RUN: grep {Inst34.*fptosi.*Inst32.*i64} < %t.ll
557 * RUN: grep {Inst35.*fptrunc.*Inst32.*float} < %t.ll
558 * RUN: grep {Inst36.*fpext.*Inst35.*double} < %t.ll
559 * RUN: grep {Inst37.*inttoptr.*P1.*i8\*} < %t.ll
560 * RUN: grep {Inst38.*ptrtoint.*Inst37.*i64} < %t.ll
561 * RUN: grep {Inst39.*bitcast.*Inst38.*double} < %t.ll
562 *)
563 let inst28 = build_trunc p1 i8_type "Inst28" atentry in
564 let inst29 = build_zext inst28 i32_type "Inst29" atentry in
565 let inst30 = build_sext inst29 i64_type "Inst30" atentry in
566 let inst31 = build_uitofp inst30 float_type "Inst31" atentry in
567 let inst32 = build_sitofp inst29 double_type "Inst32" atentry in
568 ignore(build_fptoui inst31 i32_type "Inst33" atentry);
569 ignore(build_fptosi inst32 i64_type "Inst34" atentry);
570 let inst35 = build_fptrunc inst32 float_type "Inst35" atentry in
571 ignore(build_fpext inst35 double_type "Inst36" atentry);
572 let inst37 = build_inttoptr p1 void_ptr "Inst37" atentry in
573 let inst38 = build_ptrtoint inst37 i64_type "Inst38" atentry in
574 ignore(build_bitcast inst38 double_type "Inst39" atentry)
575 end;
576
577 group "comparisons"; begin
578 (* RUN: grep {Inst40.*icmp.*ne.*P1.*P2} < %t.ll
579 * RUN: grep {Inst41.*icmp.*sle.*P2.*P1} < %t.ll
580 * RUN: grep {Inst42.*fcmp.*false.*F1.*F2} < %t.ll
581 * RUN: grep {Inst43.*fcmp.*true.*F2.*F1} < %t.ll
582 *)
583 ignore (build_icmp Icmp_ne p1 p2 "Inst40" atentry);
584 ignore (build_icmp Icmp_sle p2 p1 "Inst41" atentry);
585 ignore (build_fcmp Fcmp_false f1 f2 "Inst42" atentry);
586 ignore (build_fcmp Fcmp_true f2 f1 "Inst43" atentry)
587 end;
588
589 group "miscellaneous"; begin
590 (* RUN: grep {Inst45.*call.*P2.*P1} < %t.ll
591 * RUN: grep {Inst47.*select.*Inst46.*P1.*P2} < %t.ll
592 * RUN: grep {Inst48.*va_arg.*null.*i32} < %t.ll
593 * RUN: grep {Inst49.*extractelement.*Vec1.*P2} < %t.ll
594 * RUN: grep {Inst50.*insertelement.*Vec1.*P1.*P2} < %t.ll
595 * RUN: grep {Inst51.*shufflevector.*Vec1.*Vec2.*Vec3} < %t.ll
596 *)
597
598 (* TODO: %Inst44 = Phi *)
599
600 ignore (build_call fn [| p2; p1 |] "Inst45" atentry);
601 let inst46 = build_icmp Icmp_eq p1 p2 "Inst46" atentry in
602 ignore (build_select inst46 p1 p2 "Inst47" atentry);
603 ignore (build_va_arg
604 (make_null (make_pointer_type (make_pointer_type i8_type)))
605 i32_type "Inst48" atentry);
606
607 (* Set up some vector vregs. *)
608 let one = make_int_constant i32_type (-1) true in
609 let zero = make_int_constant i32_type 1 true in
610 let t1 = make_vector_constant [| one; zero; one; zero |] in
611 let t2 = make_vector_constant [| zero; one; zero; one |] in
612 let t3 = make_vector_constant [| one; one; zero; zero |] in
613 let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
614 let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
615 let vec3 = build_insertelement t3 p1 p2 "Vec3" atentry in
616
617 ignore (build_extractelement vec1 p2 "Inst49" atentry);
618 ignore (build_insertelement vec1 p1 p2 "Inst50" atentry);
619 ignore (build_shufflevector vec1 vec2 vec3 "Inst51" atentry);
620 end
621
622
308623 (*===-- Writer ------------------------------------------------------------===*)
309624
310625 let test_writer () =
321636 suite "constants" test_constants;
322637 suite "global values" test_global_values;
323638 suite "global variables" test_global_variables;
639 suite "functions" test_functions;
640 suite "basic blocks" test_basic_blocks;
641 suite "builder" test_builder;
324642 suite "writer" test_writer;
325643 exit !exit_status