xref: /llvm-project/llvm/bindings/ocaml/llvm/llvm_ocaml.h (revision 2a2fd488b6bc1f3df7a8c103f53fec8bf849da4a)
1 /*===-- llvm_ocaml.h - LLVM OCaml Glue --------------------------*- C++ -*-===*\
2 |*                                                                            *|
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM          *|
4 |* Exceptions.                                                                *|
5 |* See https://llvm.org/LICENSE.txt for license information.                  *|
6 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception                    *|
7 |*                                                                            *|
8 |*===----------------------------------------------------------------------===*|
9 |*                                                                            *|
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.    *|
12 |*                                                                            *|
13 |* Note that these functions intentionally take liberties with the CAMLparamX *|
14 |* macros, since most of the parameters are not GC heap objects.              *|
15 |*                                                                            *|
16 \*===----------------------------------------------------------------------===*/
17 
18 #ifndef LLVM_LLVM_OCAML_H
19 #define LLVM_LLVM_OCAML_H
20 
21 #include "caml/alloc.h"
22 #include "caml/custom.h"
23 #include "caml/version.h"
24 
25 #if OCAML_VERSION < 41200
26 /* operations on OCaml option values, defined by OCaml 4.12 */
27 #define Val_none Val_int(0)
28 #define Some_val(v) Field(v, 0)
29 #define Tag_some 0
30 #define Is_none(v) ((v) == Val_none)
31 #define Is_some(v) Is_block(v)
32 value caml_alloc_some(value);
33 #endif
34 
35 /* to_val and from_val convert between an OCaml value and a pointer from LLVM,
36    which points outside the OCaml heap. They assume that all pointers from LLVM
37    are 2-byte aligned: to_val sets the low bit so that OCaml treats the value
38    as an integer, and from_val clears the low bit. */
39 value to_val(void *ptr);
40 
41 void *from_val(value v);
42 
43 /* from_val_array takes an OCaml array value of LLVM references encoded with
44    the representation described above and returns a malloc'd array
45    of decoded LLVM references. The returned array must be deallocated using
46    free. */
47 void *from_val_array(value Elements);
48 
49 #define DiagnosticInfo_val(v) ((LLVMDiagnosticInfoRef)from_val(v))
50 #define Context_val(v) ((LLVMContextRef)from_val(v))
51 #define Attribute_val(v) ((LLVMAttributeRef)from_val(v))
52 #define Module_val(v) ((LLVMModuleRef)from_val(v))
53 #define Metadata_val(v) ((LLVMMetadataRef)from_val(v))
54 #define Type_val(v) ((LLVMTypeRef)from_val(v))
55 #define Value_val(v) ((LLVMValueRef)from_val(v))
56 #define DbgRecord_val(v) ((LLVMDbgRecordRef)from_val(v))
57 #define Use_val(v) ((LLVMUseRef)from_val(v))
58 #define BasicBlock_val(v) ((LLVMBasicBlockRef)from_val(v))
59 #define MemoryBuffer_val(v) ((LLVMMemoryBufferRef)from_val(v))
60 
61 /* Convert a C pointer to an OCaml option */
62 value ptr_to_option(void *Ptr);
63 
64 /* Convert a C string into an OCaml string */
65 value cstr_to_string(const char *Str, mlsize_t Len);
66 
67 #endif // LLVM_LLVM_OCAML_H
68