1 /*===-- llvm_ocaml.h - LLVM Ocaml Glue --------------------------*- C++ -*-===*\
3 |* The LLVM Compiler Infrastructure *|
5 |* This file was developed by Gordon Henriksen and is distributed under the *|
6 |* University of Illinois Open Source License. See LICENSE.TXT for details. *|
8 |*===----------------------------------------------------------------------===*|
10 |* This file glues LLVM's ocaml interface to its C interface. These functions *|
11 |* are by and large transparent wrappers to the corresponding C functions. *|
13 |* Note that these functions intentionally take liberties with the CAMLparamX *|
14 |* macros, since most of the parameters are not GC heap objects. *|
16 \*===----------------------------------------------------------------------===*/
18 #include "llvm-c/Core.h"
19 #include "caml/alloc.h"
20 #include "caml/mlvalues.h"
21 #include "caml/memory.h"
22 #include "llvm/Config/config.h"
29 /*===-- Modules -----------------------------------------------------------===*/
31 /* string -> llmodule */
32 CAMLprim value llvm_create_module(value ModuleID) {
33 return (value) LLVMModuleCreateWithName(String_val(ModuleID));
36 /* llmodule -> unit */
37 CAMLprim value llvm_dispose_module(value M) {
38 LLVMDisposeModule((LLVMModuleRef) M);
42 /* string -> lltype -> llmodule -> bool */
43 CAMLprim value llvm_add_type_name(value Name, value Ty, value M) {
44 int res = LLVMAddTypeName((LLVMModuleRef) M,
45 String_val(Name), (LLVMTypeRef) Ty);
46 return Val_bool(res == 0);
50 /*===-- Types -------------------------------------------------------------===*/
52 /* lltype -> type_kind */
53 CAMLprim value llvm_classify_type(value Ty) {
54 return Val_int(LLVMGetTypeKind((LLVMTypeRef) Ty));
57 /* lltype -> lltype -> unit */
58 CAMLprim value llvm_refine_abstract_type(value ConcreteTy, value AbstractTy) {
59 LLVMRefineAbstractType((LLVMTypeRef) AbstractTy, (LLVMTypeRef) ConcreteTy);
60 return (value) Val_unit;
63 /*--... Operations on integer types ........................................--*/
66 CAMLprim value llvm_i1_type (value Unit) { return (value) LLVMInt1Type(); }
67 CAMLprim value llvm_i8_type (value Unit) { return (value) LLVMInt8Type(); }
68 CAMLprim value llvm_i16_type(value Unit) { return (value) LLVMInt16Type(); }
69 CAMLprim value llvm_i32_type(value Unit) { return (value) LLVMInt32Type(); }
70 CAMLprim value llvm_i64_type(value Unit) { return (value) LLVMInt64Type(); }
73 CAMLprim value llvm_make_integer_type(value Width) {
74 return (value) LLVMCreateIntegerType(Int_val(Width));
78 CAMLprim value llvm_integer_bitwidth(value IntegerTy) {
79 return Val_int(LLVMGetIntegerTypeWidth((LLVMTypeRef) IntegerTy));
82 /*--... Operations on real types ...........................................--*/
85 CAMLprim value llvm_float_type(value Unit) {
86 return (value) LLVMFloatType();
90 CAMLprim value llvm_double_type(value Unit) {
91 return (value) LLVMDoubleType();
95 CAMLprim value llvm_x86fp80_type(value Unit) {
96 return (value) LLVMX86FP80Type();
100 CAMLprim value llvm_fp128_type(value Unit) {
101 return (value) LLVMFP128Type();
105 CAMLprim value llvm_ppc_fp128_type(value Unit) {
106 return (value) LLVMPPCFP128Type();
109 /*--... Operations on function types .......................................--*/
111 /* lltype -> lltype array -> bool -> lltype */
112 CAMLprim value llvm_make_function_type(value RetTy, value ParamTys,
114 return (value) LLVMCreateFunctionType((LLVMTypeRef) RetTy,
115 (LLVMTypeRef *) ParamTys,
116 Wosize_val(ParamTys),
121 CAMLprim value llvm_is_var_arg(value FunTy) {
122 return Val_bool(LLVMIsFunctionVarArg((LLVMTypeRef) FunTy));
125 /* lltype -> lltype */
126 CAMLprim value llvm_return_type(value FunTy) {
127 return (value) LLVMGetFunctionReturnType((LLVMTypeRef) FunTy);
130 /* lltype -> lltype array */
131 CAMLprim value llvm_param_types(value FunTy) {
132 unsigned Count = LLVMGetFunctionParamCount((LLVMTypeRef) FunTy);
133 LLVMTypeRef *FunTys = alloca(Count * sizeof(LLVMTypeRef));
135 /* copy into an ocaml array */
137 value ParamTys = alloc(Count, 0);
139 LLVMGetFunctionParamTypes((LLVMTypeRef) FunTy, FunTys);
140 for (i = 0; i != Count; ++i)
141 Store_field(ParamTys, i, (value) FunTys[i]);
146 /*--... Operations on struct types .........................................--*/
148 /* lltype array -> bool -> lltype */
149 CAMLprim value llvm_make_struct_type(value ElementTypes, value Packed) {
150 return (value) LLVMCreateStructType((LLVMTypeRef *) ElementTypes,
151 Wosize_val(ElementTypes),
155 /* lltype -> lltype array */
156 CAMLprim value llvm_element_types(value StructTy) {
157 unsigned Count = LLVMGetStructElementCount((LLVMTypeRef) StructTy);
158 LLVMTypeRef *Tys = alloca(Count * sizeof(LLVMTypeRef));
160 /* copy into an ocaml array */
162 value ElementTys = alloc(Count, 0);
164 LLVMGetStructElementTypes((LLVMTypeRef) StructTy, Tys);
165 for (i = 0; i != Count; ++i)
166 Store_field(ElementTys, i, (value) Tys[i]);
171 CAMLprim value llvm_is_packed(value StructTy) {
172 return Val_bool(LLVMIsPackedStruct((LLVMTypeRef) StructTy));
175 /*--... Operations on array, pointer, and vector types .....................--*/
177 /* lltype -> int -> lltype */
178 CAMLprim value llvm_make_array_type(value ElementTy, value Count) {
179 return (value) LLVMCreateArrayType((LLVMTypeRef) ElementTy, Int_val(Count));
182 /* lltype -> lltype */
183 CAMLprim value llvm_make_pointer_type(value ElementTy) {
184 return (value) LLVMCreatePointerType((LLVMTypeRef) ElementTy);
187 /* lltype -> int -> lltype */
188 CAMLprim value llvm_make_vector_type(value ElementTy, value Count) {
189 return (value) LLVMCreateVectorType((LLVMTypeRef) ElementTy, Int_val(Count));
192 /* lltype -> lltype */
193 CAMLprim value llvm_element_type(value Ty) {
194 return (value) LLVMGetElementType((LLVMTypeRef) Ty);
198 CAMLprim value llvm_array_length(value ArrayTy) {
199 return Val_int(LLVMGetArrayLength((LLVMTypeRef) ArrayTy));
203 CAMLprim value llvm_vector_size(value VectorTy) {
204 return Val_int(LLVMGetVectorSize((LLVMTypeRef) VectorTy));
207 /*--... Operations on other types ..........................................--*/
210 CAMLprim value llvm_void_type (value Unit) { return (value) LLVMVoidType(); }
211 CAMLprim value llvm_label_type(value Unit) { return (value) LLVMLabelType(); }
214 CAMLprim value llvm_make_opaque_type(value Unit) {
215 return (value) LLVMCreateOpaqueType();
219 /*===-- VALUES ------------------------------------------------------------===*/
221 /* llvalue -> lltype */
222 CAMLprim value llvm_type_of(value Val) {
223 return (value) LLVMGetTypeOfValue((LLVMValueRef) Val);
226 /* llvalue -> string */
227 CAMLprim value llvm_value_name(value Val) {
228 return copy_string(LLVMGetValueName((LLVMValueRef) Val));
231 /* string -> llvalue -> unit */
232 CAMLprim value llvm_set_value_name(value Name, value Val) {
233 LLVMSetValueName((LLVMValueRef) Val, String_val(Name));
237 /*--... Operations on constants of (mostly) any type .......................--*/
239 /* lltype -> llvalue */
240 CAMLprim value llvm_make_null(value Ty) {
241 return (value) LLVMGetNull((LLVMTypeRef) Ty);
244 /* lltype -> llvalue */
245 CAMLprim value llvm_make_all_ones(value Ty) {
246 return (value) LLVMGetAllOnes((LLVMTypeRef) Ty);
249 /* lltype -> llvalue */
250 CAMLprim value llvm_make_undef(value Ty) {
251 return (value) LLVMGetUndef((LLVMTypeRef) Ty);
254 /* llvalue -> bool */
255 CAMLprim value llvm_is_constant(value Ty) {
256 return Val_bool(LLVMIsConstant((LLVMValueRef) Ty));
259 /* llvalue -> bool */
260 CAMLprim value llvm_is_null(value Val) {
261 return Val_bool(LLVMIsNull((LLVMValueRef) Val));
264 /* llvalue -> bool */
265 CAMLprim value llvm_is_undef(value Ty) {
266 return Val_bool(LLVMIsUndef((LLVMValueRef) Ty));
269 /*--... Operations on scalar constants .....................................--*/
271 /* lltype -> int -> bool -> llvalue */
272 CAMLprim value llvm_make_int_constant(value IntTy, value N, value SExt) {
273 /* GCC warns if we use the ternary operator. */
274 unsigned long long N2;
276 N2 = (value) Int_val(N);
278 N2 = (mlsize_t) Int_val(N);
280 return (value) LLVMGetIntConstant((LLVMTypeRef) IntTy, N2, Bool_val(SExt));
283 /* lltype -> Int64.t -> bool -> llvalue */
284 CAMLprim value llvm_make_int64_constant(value IntTy, value N, value SExt) {
285 return (value) LLVMGetIntConstant((LLVMTypeRef) IntTy, Int64_val(N),
289 /* lltype -> float -> llvalue */
290 CAMLprim value llvm_make_real_constant(value RealTy, value N) {
291 return (value) LLVMGetRealConstant((LLVMTypeRef) RealTy, Double_val(N));
294 /*--... Operations on composite constants ..................................--*/
296 /* string -> bool -> llvalue */
297 CAMLprim value llvm_make_string_constant(value Str, value NullTerminate) {
298 return (value) LLVMGetStringConstant(String_val(Str),
300 Bool_val(NullTerminate) == 0);
303 /* lltype -> llvalue array -> llvalue */
304 CAMLprim value llvm_make_array_constant(value ElementTy, value ElementVals) {
305 return (value) LLVMGetArrayConstant((LLVMTypeRef) ElementTy,
306 (LLVMValueRef*) Op_val(ElementVals),
307 Wosize_val(ElementVals));
310 /* llvalue array -> bool -> llvalue */
311 CAMLprim value llvm_make_struct_constant(value ElementVals, value Packed) {
312 return (value) LLVMGetStructConstant((LLVMValueRef*) Op_val(ElementVals),
313 Wosize_val(ElementVals),
317 /* llvalue array -> llvalue */
318 CAMLprim value llvm_make_vector_constant(value ElementVals) {
319 return (value) LLVMGetVectorConstant((LLVMValueRef*) Op_val(ElementVals),
320 Wosize_val(ElementVals));
323 /*--... Operations on global variables, functions, and aliases (globals) ...--*/
325 /* llvalue -> bool */
326 CAMLprim value llvm_is_declaration(value Global) {
327 return Val_bool(LLVMIsDeclaration((LLVMValueRef) Global));
330 /* llvalue -> linkage */
331 CAMLprim value llvm_linkage(value Global) {
332 return Val_int(LLVMGetLinkage((LLVMValueRef) Global));
335 /* linkage -> llvalue -> unit */
336 CAMLprim value llvm_set_linkage(value Linkage, value Global) {
337 LLVMSetLinkage((LLVMValueRef) Global, Int_val(Linkage));
341 /* llvalue -> string */
342 CAMLprim value llvm_section(value Global) {
343 return copy_string(LLVMGetSection((LLVMValueRef) Global));
346 /* string -> llvalue -> unit */
347 CAMLprim value llvm_set_section(value Section, value Global) {
348 LLVMSetSection((LLVMValueRef) Global, String_val(Section));
352 /* llvalue -> visibility */
353 CAMLprim value llvm_visibility(value Global) {
354 return Val_int(LLVMGetVisibility((LLVMValueRef) Global));
357 /* visibility -> llvalue -> unit */
358 CAMLprim value llvm_set_visibility(value Viz, value Global) {
359 LLVMSetVisibility((LLVMValueRef) Global, Int_val(Viz));
364 CAMLprim value llvm_alignment(value Global) {
365 return Val_int(LLVMGetAlignment((LLVMValueRef) Global));
368 /* int -> llvalue -> unit */
369 CAMLprim value llvm_set_alignment(value Bytes, value Global) {
370 LLVMSetAlignment((LLVMValueRef) Global, Int_val(Bytes));
374 /*--... Operations on global variables .....................................--*/
376 /* lltype -> string -> llmodule -> llvalue */
377 CAMLprim value llvm_add_global(value Ty, value Name, value M) {
378 return (value) LLVMAddGlobal((LLVMModuleRef) M,
379 (LLVMTypeRef) Ty, String_val(Name));
382 /* lltype -> string -> llmodule -> llvalue */
383 CAMLprim value llvm_declare_global(value Ty, value Name, value M) {
384 return (value) LLVMAddGlobal((LLVMModuleRef) M,
385 (LLVMTypeRef) Ty, String_val(Name));
388 /* string -> llvalue -> llmodule -> llvalue */
389 CAMLprim value llvm_define_global(value Name, value ConstantVal, value M) {
390 LLVMValueRef Initializer = (LLVMValueRef) ConstantVal;
391 LLVMValueRef GlobalVar = LLVMAddGlobal((LLVMModuleRef) M,
392 LLVMGetTypeOfValue(Initializer),
394 LLVMSetInitializer(GlobalVar, Initializer);
395 return (value) GlobalVar;
398 /* llvalue -> unit */
399 CAMLprim value llvm_delete_global(value GlobalVar) {
400 LLVMDeleteGlobal((LLVMValueRef) GlobalVar);
404 /* llvalue -> llvalue */
405 CAMLprim value llvm_global_initializer(value GlobalVar) {
406 return (value) LLVMGetInitializer((LLVMValueRef) GlobalVar);
409 /* llvalue -> llvalue -> unit */
410 CAMLprim value llvm_set_initializer(value ConstantVal, value GlobalVar) {
411 LLVMSetInitializer((LLVMValueRef) GlobalVar, (LLVMValueRef) ConstantVal);
415 /* llvalue -> unit */
416 CAMLprim value llvm_remove_initializer(value GlobalVar) {
417 LLVMSetInitializer((LLVMValueRef) GlobalVar, NULL);
421 /* llvalue -> bool */
422 CAMLprim value llvm_is_thread_local(value GlobalVar) {
423 return Val_bool(LLVMIsThreadLocal((LLVMValueRef) GlobalVar));
426 /* bool -> llvalue -> unit */
427 CAMLprim value llvm_set_thread_local(value IsThreadLocal, value GlobalVar) {
428 LLVMSetThreadLocal((LLVMValueRef) GlobalVar, Bool_val(IsThreadLocal));