llvm.org GIT mirror llvm / da1435f
Adding bindings for memory buffers and module providers. Switching to exceptions rather than variants for error handling in Ocaml. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45226 91177308-0d34-0410-b5e6-96231b3b80d8 Gordon Henriksen 11 years ago
16 changed file(s) with 355 addition(s) and 159 deletion(s). Raw diff Collapse all Expand all
9797 9F68EB130C77AD2C004AA152 /* BitcodeWriterPass.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = BitcodeWriterPass.cpp; sourceTree = ""; };
9898 9F68EB250C77AD2C004AA152 /* ValueEnumerator.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = ValueEnumerator.cpp; sourceTree = ""; };
9999 9F68EB260C77AD2C004AA152 /* ValueEnumerator.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = ValueEnumerator.h; sourceTree = ""; };
100 9F6B2CC00D0F6E56000F00FD /* bitreader.ml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = bitreader.ml; sourceTree = ""; };
100101 9F7793460C73BC2000551F9C /* CodeGenPrepare.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CodeGenPrepare.cpp; sourceTree = ""; };
101102 9F7793470C73BC2000551F9C /* GVN.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GVN.cpp; sourceTree = ""; };
102103 9F7793480C73BC2000551F9C /* GVNPRE.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GVNPRE.cpp; sourceTree = ""; };
12401241 isa = PBXGroup;
12411242 children = (
12421243 9F7C2C4F0CB9496C00498408 /* analysis.ml */,
1244 9F6B2CC00D0F6E56000F00FD /* bitreader.ml */,
12431245 9F7C2C520CB9496C00498408 /* bitwriter.ml */,
12441246 9F7C2C5D0CB9496C00498408 /* vmcore.ml */,
12451247 );
3636 Store_field(Option, 0, String);
3737 }
3838
39 LLVMDisposeVerifierMessage(Message);
39 LLVMDisposeMessage(Message);
4040
4141 CAMLreturn(Option);
4242 }
1515 #include "caml/alloc.h"
1616 #include "caml/mlvalues.h"
1717 #include "caml/memory.h"
18 #include
19
20
21 /* Can't use the recommended caml_named_value mechanism for backwards
22 compatibility reasons. This is largely equivalent. */
23 static value llvm_bitreader_error_exn;
24
25 CAMLprim value llvm_register_bitreader_exns(value Error) {
26 llvm_bitreader_error_exn = Field(Error, 0);
27 register_global_root(&llvm_bitreader_error_exn);
28 return Val_unit;
29 }
30
31 void llvm_raise(value Prototype, char *Message);
32
1833
1934 /*===-- Modules -----------------------------------------------------------===*/
2035
21 /* string -> bitreader_result
36 /* Llvm.llmemorybuffer -> Llvm.module */
37 CAMLprim value llvm_get_module_provider(LLVMMemoryBufferRef MemBuf) {
38 CAMLparam0();
39 CAMLlocal2(Variant, MessageVal);
40 char *Message;
41
42 LLVMModuleProviderRef MP;
43 if (LLVMGetBitcodeModuleProvider(MemBuf, &MP, &Message))
44 llvm_raise(llvm_bitreader_error_exn, Message);
45
46 CAMLreturn((value) MemBuf);
47 }
2248
23 type bitreader_result =
24 | Bitreader_success of Llvm.llmodule
25 | Bitreader_failure of string
26 */
27 CAMLprim value llvm_read_bitcode_file(value Path) {
49 /* Llvm.llmemorybuffer -> Llvm.llmodule */
50 CAMLprim value llvm_parse_bitcode(LLVMMemoryBufferRef MemBuf) {
51 CAMLparam0();
52 CAMLlocal2(Variant, MessageVal);
2853 LLVMModuleRef M;
2954 char *Message;
30 CAMLparam1(Path);
31 CAMLlocal2(Variant, MessageVal);
3255
33 if (LLVMReadBitcodeFromFile(String_val(Path), &M, &Message)) {
34 MessageVal = copy_string(Message);
35 LLVMDisposeBitcodeReaderMessage(Message);
36
37 Variant = alloc(1, 1);
38 Field(Variant, 0) = MessageVal;
39 } else {
40 Variant = alloc(1, 0);
41 Field(Variant, 0) = Val_op(M);
42 }
56 if (LLVMParseBitcode(MemBuf, &M, &Message))
57 llvm_raise(llvm_bitreader_error_exn, Message);
4358
44 CAMLreturn(Variant);
59 CAMLreturn((value) M);
4560 }
77 *===----------------------------------------------------------------------===*)
88
99
10 type bitreader_result =
11 | Bitreader_success of Llvm.llmodule
12 | Bitreader_failure of string
10 exception Error of string
1311
12 external register_exns : exn -> unit = "llvm_register_bitreader_exns"
13 let _ = register_exns (Error "")
1414
15 external read_bitcode_file : string -> bitreader_result
16 = "llvm_read_bitcode_file"
15 external get_module_provider : Llvm.llmemorybuffer -> Llvm.llmoduleprovider
16 = "llvm_get_module_provider"
17 external parse_bitcode : Llvm.llmemorybuffer -> Llvm.llmodule
18 = "llvm_parse_bitcode"
1212 *===----------------------------------------------------------------------===*)
1313
1414
15 type bitreader_result =
16 | Bitreader_success of Llvm.llmodule
17 | Bitreader_failure of string
15 exception Error of string
1816
17 (** [read_bitcode_file path] reads the bitcode for a new module [m] from the
18 file at [path]. Returns [Success m] if successful, and [Failure msg]
19 otherwise, where [msg] is a description of the error encountered.
20 See the function [llvm::getBitcodeModuleProvider]. **)
21 external get_module_provider : Llvm.llmemorybuffer -> Llvm.llmoduleprovider
22 = "llvm_get_module_provider"
1923
20 (** [read_bitcode_file path] reads the bitcode for module [m] from the file at
21 [path]. Returns [Reader_success m] if successful, and [Reader_failure msg]
22 otherwise, where [msg] is a description of the error encountered. **)
23 external read_bitcode_file : string -> bitreader_result
24 = "llvm_read_bitcode_file"
24 (** [parse_bitcode mb] parses the bitcode for a new module [m] from the memory
25 buffer [mb]. Returns [Success m] if successful, and [Failure msg] otherwise,
26 where [msg] is a description of the error encountered.
27 See the function [llvm::ParseBitcodeFile]. **)
28 external parse_bitcode : Llvm.llmemorybuffer -> Llvm.llmodule
29 = "llvm_parse_bitcode"
1414 type llbasicblock
1515 type llbuilder
1616 type llmoduleprovider
17 type llmemorybuffer
1718
1819 type type_kind =
1920 Void_type
8283 | Fcmp_ule
8384 | Fcmp_une
8485 | Fcmp_true
86
87 exception IoError of string
88
89 external register_exns : exn -> unit = "llvm_register_core_exns"
90 let _ = register_exns (IoError "")
8591
8692
8793 (*===-- Modules -----------------------------------------------------------===*)
431437
432438
433439 (*===-- Module providers --------------------------------------------------===*)
434 external create_module_provider : llmodule -> llmoduleprovider
435 = "LLVMCreateModuleProviderForExistingModule"
436 external dispose_module_provider : llmoduleprovider -> unit
437 = "llvm_dispose_module_provider"
440
441 module ModuleProvider = struct
442 external create : llmodule -> llmoduleprovider
443 = "LLVMCreateModuleProviderForExistingModule"
444 external dispose : llmoduleprovider -> unit = "llvm_dispose_module_provider"
445 end
446
447
448 (*===-- Memory buffers ----------------------------------------------------===*)
449
450 module MemoryBuffer = struct
451 external of_file : string -> llmemorybuffer = "llvm_memorybuffer_of_file"
452 external of_stdin : unit -> llmemorybuffer = "llvm_memorybuffer_of_stdin"
453 external dispose : llmemorybuffer -> unit = "llvm_memorybuffer_dispose"
454 end
438455
439456
440457 (*===-- Non-Externs -------------------------------------------------------===*)
3939 class. **)
4040 type llbuilder
4141
42 (** Used to provide a module to JIT or interpreter. **)
42 (** Used to provide a module to JIT or interpreter.
43 See the [llvm::ModuleProvider] class. **)
4344 type llmoduleprovider
45
46 (** Used to efficiently handle large buffers of read-only binary data.
47 See the [llvm::MemoryBuffer] class. **)
48 type llmemorybuffer
4449
4550 (** The kind of an [lltype], the result of [classify_type ty]. See the
4651 [llvm::Type::TypeID] enumeration. **)
128133 | Fcmp_une
129134 | Fcmp_true
130135
136 exception IoError of string
137
131138
132139 (*===-- Modules -----------------------------------------------------------===*)
133140
12341241
12351242 (*===-- Module providers --------------------------------------------------===*)
12361243
1237 (** [create_module_provider m] encapsulates [m] in a module provider and takes
1238 ownership of the module. See the constructor
1239 [llvm::ExistingModuleProvider::ExistingModuleProvider]. **)
1240 external create_module_provider : llmodule -> llmoduleprovider
1241 = "LLVMCreateModuleProviderForExistingModule"
1242
1243 (** [dispose_module_provider mp] destroys the module provider [mp] as well as
1244 the contained module. **)
1245 external dispose_module_provider : llmoduleprovider -> unit
1246 = "llvm_dispose_module_provider"
1244 module ModuleProvider : sig
1245 (** [create_module_provider m] encapsulates [m] in a module provider and takes
1246 ownership of the module. See the constructor
1247 [llvm::ExistingModuleProvider::ExistingModuleProvider]. **)
1248 external create : llmodule -> llmoduleprovider
1249 = "LLVMCreateModuleProviderForExistingModule"
1250
1251 (** [dispose_module_provider mp] destroys the module provider [mp] as well as
1252 the contained module. **)
1253 external dispose : llmoduleprovider -> unit = "llvm_dispose_module_provider"
1254 end
1255
1256
1257 (*===-- Memory buffers ----------------------------------------------------===*)
1258
1259 module MemoryBuffer : sig
1260 (** [of_file p] is the memory buffer containing the contents of the file at
1261 path [p]. If the file could not be read, then [IoError msg] is raised. **)
1262 external of_file : string -> llmemorybuffer = "llvm_memorybuffer_of_file"
1263
1264 (** [stdin ()] is the memory buffer containing the contents of standard input.
1265 If standard input is empty, then [IoError msg] is raised. **)
1266 external of_stdin : unit -> llmemorybuffer = "llvm_memorybuffer_of_stdin"
1267
1268 (** Disposes of a memory buffer. **)
1269 external dispose : llmemorybuffer -> unit = "llvm_memorybuffer_dispose"
1270 end
1919 #include "caml/custom.h"
2020 #include "caml/mlvalues.h"
2121 #include "caml/memory.h"
22 #include "caml/fail.h"
23 #include "caml/callback.h"
2224 #include "llvm/Config/config.h"
2325 #include
26 #include
27
28
29 /* Can't use the recommended caml_named_value mechanism for backwards
30 compatibility reasons. This is largely equivalent. */
31 static value llvm_ioerror_exn;
32
33 CAMLprim value llvm_register_core_exns(value IoError) {
34 llvm_ioerror_exn = Field(IoError, 0);
35 register_global_root(&llvm_ioerror_exn);
36 return Val_unit;
37 }
38
39 void llvm_raise(value Prototype, char *Message) {
40 CAMLparam1(Prototype);
41 CAMLlocal1(CamlMessage);
42
43 CamlMessage = copy_string(Message);
44 LLVMDisposeMessage(Message);
45
46 raise_with_arg(Prototype, CamlMessage);
47 CAMLnoreturn;
48 }
2449
2550
2651 /*===-- Modules -----------------------------------------------------------===*/
10701095 LLVMDisposeModuleProvider(MP);
10711096 return Val_unit;
10721097 }
1098
1099
1100 /*===-- Memory buffers ----------------------------------------------------===*/
1101
1102 /* string -> llmemorybuffer
1103 raises IoError msg on error */
1104 CAMLprim value llvm_memorybuffer_of_file(value Path) {
1105 CAMLparam1(Path);
1106 char *Message;
1107 LLVMMemoryBufferRef MemBuf;
1108
1109 if (LLVMCreateMemoryBufferWithContentsOfFile(String_val(Path),
1110 &MemBuf, &Message))
1111 llvm_raise(llvm_ioerror_exn, Message);
1112
1113 CAMLreturn((value) MemBuf);
1114 }
1115
1116 /* unit -> llmemorybuffer
1117 raises IoError msg on error */
1118 CAMLprim LLVMMemoryBufferRef llvm_memorybuffer_of_stdin(value Unit) {
1119 char *Message;
1120 LLVMMemoryBufferRef MemBuf;
1121
1122 if (LLVMCreateMemoryBufferWithSTDIN(&MemBuf, &Message))
1123 llvm_raise(llvm_ioerror_exn, Message);
1124
1125 return MemBuf;
1126 }
1127
1128 /* llmemorybuffer -> unit */
1129 CAMLprim value llvm_memorybuffer_dispose(LLVMMemoryBufferRef MemBuf) {
1130 LLVMDisposeMemoryBuffer(MemBuf);
1131 return Val_unit;
1132 }
1133
3333
3434
3535 /* Verifies that a module is valid, taking the specified action if not.
36 Optionally returns a human-readable description of any invalid constructs. */
36 Optionally returns a human-readable description of any invalid constructs.
37 OutMessage must be disposed with LLVMDisposeMessage. */
3738 int LLVMVerifyModule(LLVMModuleRef M, LLVMVerifierFailureAction Action,
3839 char **OutMessage);
39
40 /* Disposes of the message allocated by the verifier, if any. */
41 void LLVMDisposeVerifierMessage(char *Message);
4240
4341 /* Verifies that a single function is valid, taking the specified action. Useful
4442 for debugging. */
2525 #endif
2626
2727
28 /* Reads a module from the specified path, returning a reference to the module
29 via the OutModule parameter. Returns 0 on success. Optionally returns a
30 human-readable error message. */
31 int LLVMReadBitcodeFromFile(const char *Path, LLVMModuleRef *OutModule,
32 char **OutMessage);
28 /* Builds a module from the bitcode in the specified memory buffer, returning a
29 reference to the module via the OutModule parameter. Returns 0 on success.
30 Optionally returns a human-readable error message via OutMessage. */
31 int LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
32 LLVMModuleRef *OutModule, char **OutMessage);
3333
34 /* Reads a module from the specified path, returning a reference to a lazy
35 module provider via the OutModule parameter. Returns 0 on success. Optionally
36 returns a human-readable error message. */
37 int LLVMCreateModuleProviderFromFile(const char *Path,
38 LLVMModuleProviderRef *OutMP,
39 char **OutMessage);
40
41 /* Disposes of the message allocated by the bitcode reader, if any. */
42 void LLVMDisposeBitcodeReaderMessage(char *Message);
34 /* Reads a module from the specified path, returning via the OutMP parameter
35 a module provider which performs lazy deserialization. Returns 0 on success.
36 Optionally returns a human-readable error message via OutMessage. */
37 int LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
38 LLVMModuleProviderRef *OutMP,
39 char **OutMessage);
4340
4441
4542 #ifdef __cplusplus
5050 typedef struct LLVMOpaqueValue *LLVMValueRef;
5151 typedef struct LLVMOpaqueBasicBlock *LLVMBasicBlockRef;
5252 typedef struct LLVMOpaqueBuilder *LLVMBuilderRef;
53
54 /* Used to provide a module to JIT or interpreter.
55 * See the llvm::ModuleProvider class.
56 */
5357 typedef struct LLVMOpaqueModuleProvider *LLVMModuleProviderRef;
58
59 /* Used to provide a module to JIT or interpreter.
60 * See the llvm::MemoryBuffer class.
61 */
62 typedef struct LLVMOpaqueMemoryBuffer *LLVMMemoryBufferRef;
5463
5564 typedef enum {
5665 LLVMVoidTypeKind, /* type with no size */
126135 LLVMRealUNE, /* True if unordered or not equal */
127136 LLVMRealPredicateTrue /* Always true (always folded) */
128137 } LLVMRealPredicate;
138
139
140 /*===-- Error handling ----------------------------------------------------===*/
141
142 void LLVMDisposeMessage(char *Message);
129143
130144
131145 /*===-- Modules -----------------------------------------------------------===*/
490504 LLVMValueRef V2, LLVMValueRef Mask,
491505 const char *Name);
492506
507
493508 /*===-- Module providers --------------------------------------------------===*/
494509
495510 /* Encapsulates the module M in a module provider, taking ownership of the
504519 */
505520 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP);
506521
522
523 /*===-- Memory buffers ----------------------------------------------------===*/
524
525 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
526 LLVMMemoryBufferRef *OutMemBuf,
527 char **OutMessage);
528 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
529 char **OutMessage);
530 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf);
531
507532 #ifdef __cplusplus
508533 }
509534
510535 namespace llvm {
511536 class ModuleProvider;
512
513 /* Opaque module conversions
514 */
515 inline Module *unwrap(LLVMModuleRef M) {
516 return reinterpret_cast(M);
517 }
518
519 inline LLVMModuleRef wrap(Module *M) {
520 return reinterpret_cast(M);
521 }
522
523 /* Opaque type conversions
524 */
525 inline Type *unwrap(LLVMTypeRef Ty) {
526 return reinterpret_cast(Ty);
527 }
528
537 class MemoryBuffer;
538
539 #define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref) \
540 inline ty *unwrap(ref P) { \
541 return reinterpret_cast(P); \
542 } \
543 \
544 inline ref wrap(const ty *P) { \
545 return reinterpret_cast(const_cast(P)); \
546 }
547
548 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Type, LLVMTypeRef )
549 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Value, LLVMValueRef )
550 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module, LLVMModuleRef )
551 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef )
552 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMBuilder, LLVMBuilderRef )
553 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PATypeHolder, LLVMTypeHandleRef )
554 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ModuleProvider, LLVMModuleProviderRef)
555 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef )
556
557 #undef DEFINE_SIMPLE_CONVERSION_FUNCTIONS
558
559 /* Specialized opaque type conversions.
560 */
529561 template
530562 inline T *unwrap(LLVMTypeRef Ty) {
531563 return cast(unwrap(Ty));
535567 return reinterpret_cast(Tys);
536568 }
537569
538 inline LLVMTypeRef wrap(const Type *Ty) {
539 return reinterpret_cast(const_cast(Ty));
540 }
541
542570 inline LLVMTypeRef *wrap(const Type **Tys) {
543571 return reinterpret_cast(const_cast(Tys));
544572 }
545573
546 /* Opaque value conversions
574 /* Specialized opaque value conversions.
547575 */
548 inline Value *unwrap(LLVMValueRef Val) {
549 return reinterpret_cast(Val);
550 }
551
552576 template
553577 inline T *unwrap(LLVMValueRef Val) {
554578 return cast(unwrap(Val));
567591 return reinterpret_cast(Vals);
568592 }
569593
570 inline LLVMValueRef wrap(const Value *Val) {
571 return reinterpret_cast(const_cast(Val));
572 }
573
574594 inline LLVMValueRef *wrap(const Value **Vals) {
575595 return reinterpret_cast(const_cast(Vals));
576596 }
577
578 /* Basic block conversions
579 */
580 inline BasicBlock *unwrap(LLVMBasicBlockRef BBRef) {
581 return reinterpret_cast(BBRef);
582 }
583
584 inline LLVMBasicBlockRef wrap(const BasicBlock *BB) {
585 return reinterpret_cast(const_cast(BB));
586 }
587
588 /* Opaque builder conversions.
589 */
590 inline LLVMBuilder *unwrap(LLVMBuilderRef B) {
591 return reinterpret_cast(B);
592 }
593
594 inline LLVMBuilderRef wrap(LLVMBuilder *B) {
595 return reinterpret_cast(B);
596 }
597
598 /* Opaque type handle conversions.
599 */
600 inline PATypeHolder *unwrap(LLVMTypeHandleRef B) {
601 return reinterpret_cast(B);
602 }
603
604 inline LLVMTypeHandleRef wrap(PATypeHolder *B) {
605 return reinterpret_cast(B);
606 }
607
608 /* Opaque module provider conversions.
609 */
610 inline ModuleProvider *unwrap(LLVMModuleProviderRef P) {
611 return reinterpret_cast(P);
612 }
613
614 inline LLVMModuleProviderRef wrap(ModuleProvider *P) {
615 return reinterpret_cast(P);
616 }
617597 }
618598
619599 #endif /* !defined(__cplusplus) */
2626 return Result;
2727 }
2828
29 void LLVMDisposeVerifierMessage(char *Message) {
30 free(Message);
31 }
32
3329 int LLVMVerifyFunction(LLVMValueRef Fn, LLVMVerifierFailureAction Action) {
3430 return verifyFunction(*unwrap(Fn),
3531 static_cast(Action));
1313
1414 using namespace llvm;
1515
16
17 int LLVMReadBitcodeFromFile(const char *Path, LLVMModuleRef *OutModule,
18 char **OutMessage) {
16 /* Builds a module from the bitcode in the specified memory buffer, returning a
17 reference to the module via the OutModule parameter. Returns 0 on success.
18 Optionally returns a human-readable error message via OutMessage. */
19 int LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
20 LLVMModuleRef *OutModule, char **OutMessage) {
1921 std::string Message;
2022
21 MemoryBuffer *buf = MemoryBuffer::getFile(Path, strlen(Path), &Message);
22 if (!buf) {
23 if (!OutMessage)
24 *OutMessage = strdup(Message.c_str());
25 return 1;
26 }
27
28 *OutModule = wrap(ParseBitcodeFile(buf, &Message));
23 *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), &Message));
2924 if (!*OutModule) {
3025 if (OutMessage)
3126 *OutMessage = strdup(Message.c_str());
3530 return 0;
3631 }
3732
38 void LLVMDisposeBitcodeReaderMessage(char *Message) {
39 if (Message)
40 free(Message);
33 /* Reads a module from the specified path, returning via the OutModule parameter
34 a module provider which performs lazy deserialization. Returns 0 on success.
35 Optionally returns a human-readable error message via OutMessage. */
36 int LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
37 LLVMModuleProviderRef *OutMP,
38 char **OutMessage) {
39 std::string Message;
40
41 *OutMP = wrap(getBitcodeModuleProvider(unwrap(MemBuf), &Message));
42 if (!*OutMP) {
43 if (OutMessage)
44 *OutMessage = strdup(Message.c_str());
45 return 1;
46 }
47
48 return 0;
4149 }
1818 #include "llvm/GlobalVariable.h"
1919 #include "llvm/TypeSymbolTable.h"
2020 #include "llvm/ModuleProvider.h"
21 #include "llvm/Support/MemoryBuffer.h"
2122 #include
23 #include
2224
2325 using namespace llvm;
26
27
28 /*===-- Error handling ----------------------------------------------------===*/
29
30 void LLVMDisposeMessage(char *Message) {
31 free(Message);
32 }
2433
2534
2635 /*===-- Operations on modules ---------------------------------------------===*/
10471056 delete unwrap(MP);
10481057 }
10491058
1059
1060 /*===-- Memory buffers ----------------------------------------------------===*/
1061
1062 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1063 LLVMMemoryBufferRef *OutMemBuf,
1064 char **OutMessage) {
1065 std::string Error;
1066 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, strlen(Path), &Error)) {
1067 *OutMemBuf = wrap(MB);
1068 return 0;
1069 }
1070
1071 *OutMessage = strdup(Error.c_str());
1072 return 1;
1073 }
1074
1075 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1076 char **OutMessage) {
1077 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1078 *OutMemBuf = wrap(MB);
1079 return 0;
1080 }
1081
1082 *OutMessage = strdup("stdin is empty.");
1083 return 1;
1084 }
1085
1086 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1087 delete unwrap(MemBuf);
1088 }
1717
1818 Llvm.dispose_module m;
1919
20 test (match Llvm_bitreader.read_bitcode_file fn with
21 | Llvm_bitreader.Bitreader_success m -> Llvm.dispose_module m; true
22 | Llvm_bitreader.Bitreader_failure _ -> false)
20 (* parse_bitcode *)
21 begin
22 let mb = Llvm.MemoryBuffer.of_file fn in
23 begin try
24 let m = Llvm_bitreader.parse_bitcode mb in
25 Llvm.dispose_module m
26 with x ->
27 Llvm.MemoryBuffer.dispose;
28 raise x
29 end
30 end;
31
32 (* MemoryBuffer.of_file *)
33 test begin try
34 let mb = Llvm.MemoryBuffer.of_file (fn ^ ".bogus") in
35 Llvm.MemoryBuffer.dispose mb;
36 false
37 with Llvm.IoError _ ->
38 true
39 end;
40
41 (* get_module_provider *)
42 begin
43 let mb = Llvm.MemoryBuffer.of_file fn in
44 let mp = begin try
45 Llvm_bitreader.get_module_provider mb
46 with x ->
47 Llvm.MemoryBuffer.dispose mb;
48 raise x
49 end in
50 Llvm.ModuleProvider.dispose mp
51 end;
52
53 (* corrupt the bitcode *)
54 let fn = fn ^ ".txt" in
55 begin let oc = open_out fn in
56 output_string oc "not a bitcode file\n";
57 close_out oc
58 end;
59
60 (* test get_module_provider exceptions *)
61 test begin
62 try
63 let mb = Llvm.MemoryBuffer.of_file fn in
64 let mp = begin try
65 Llvm_bitreader.get_module_provider mb
66 with x ->
67 Llvm.MemoryBuffer.dispose mb;
68 raise x
69 end in
70 Llvm.ModuleProvider.dispose mp;
71 false
72 with Llvm_bitreader.Error _ ->
73 true
74 end
804804
805805 let test_module_provider () =
806806 let m = create_module "test" in
807 let mp = create_module_provider m in
808 dispose_module_provider mp
807 let mp = ModuleProvider.create m in
808 ModuleProvider.dispose mp
809809
810810
811811 (*===-- Writer ------------------------------------------------------------===*)