xref: /llvm-project/llvm/bindings/ocaml/bitwriter/bitwriter_ocaml.c (revision 30f423a543641816e367cd17dad3d4808cba8a79)
1 /*===-- bitwriter_ocaml.c - 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 #include "caml/alloc.h"
19 #include "caml/memory.h"
20 #include "caml/mlvalues.h"
21 #include "llvm_ocaml.h"
22 #include "llvm-c/BitWriter.h"
23 #include "llvm-c/Core.h"
24 
25 /* Llvm.llmodule -> string -> bool */
llvm_write_bitcode_file(value M,value Path)26 value llvm_write_bitcode_file(value M, value Path) {
27   int Result = LLVMWriteBitcodeToFile(Module_val(M), String_val(Path));
28   return Val_bool(Result == 0);
29 }
30 
31 /* ?unbuffered:bool -> Llvm.llmodule -> Unix.file_descr -> bool */
llvm_write_bitcode_to_fd(value U,value M,value FD)32 value llvm_write_bitcode_to_fd(value U, value M, value FD) {
33   int Unbuffered;
34   int Result;
35 
36   if (U == Val_int(0)) {
37     Unbuffered = 0;
38   } else {
39     Unbuffered = Bool_val(Field(U, 0));
40   }
41 
42   Result = LLVMWriteBitcodeToFD(Module_val(M), Int_val(FD), 0, Unbuffered);
43   return Val_bool(Result == 0);
44 }
45 
46 /* Llvm.llmodule -> Llvm.llmemorybuffer */
llvm_write_bitcode_to_memory_buffer(value M)47 value llvm_write_bitcode_to_memory_buffer(value M) {
48   return to_val(LLVMWriteBitcodeToMemoryBuffer(Module_val(M)));
49 }
50