-
Notifications
You must be signed in to change notification settings - Fork 4
/
module-load.c
33 lines (28 loc) · 969 Bytes
/
module-load.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "zero-to-rust-jit.h"
static noreturn void DiagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) {
char *CErr = LLVMGetDiagInfoDescription(DI);
fprintf(stderr, "Error in bitcode parser: %s\n", CErr);
LLVMDisposeMessage(CErr);
shutdown(EXIT_FAILURE);
}
LLVMModuleRef loadModule(const char *FileName,
LLVMOrcThreadSafeContextRef Ctx) {
char *Err;
LLVMMemoryBufferRef MemBuf;
LLVMBool EC =
LLVMCreateMemoryBufferWithContentsOfFile(FileName, &MemBuf, &Err);
if (EC != 0) {
fprintf(stderr, "Error reading file %s: %s\n", FileName, Err);
shutdown(EC);
}
LLVMContextRef BareCtx = LLVMOrcThreadSafeContextGetContext(Ctx);
LLVMContextSetDiagnosticHandler(BareCtx, DiagnosticHandler, NULL);
LLVMModuleRef Mod;
EC = LLVMParseBitcode2(MemBuf, &Mod);
if (EC != 0) {
fprintf(stderr, "Error parsing bitcode: %s\n", FileName);
LLVMDisposeMemoryBuffer(MemBuf);
shutdown(EC);
}
return Mod;
}