-
Notifications
You must be signed in to change notification settings - Fork 0
/
llo-orc.c
159 lines (132 loc) · 4.98 KB
/
llo-orc.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "llvm-c/Core.h"
#include "llvm-c/LLJIT.h"
#include "llvm-c/OrcEE.h"
#include "llvm-c/Target.h"
#include "llvm-c/Transforms/Scalar.h"
#include "llvm-c/Transforms/Utils.h"
#include <assert.h>
#ifndef NDEBUG
# include "llvm-c/Analysis.h"
#endif
#include "orc.h"
#include <stdio.h> /* puts(), fopen() */
#include <stdlib.h> /* atoi() */
#define FALSE 0
#define TRUE 1
int main(int argc, char **argv)
{
LLVMInitializeNativeTarget();
LLVMInitializeNativeAsmPrinter();
LLVMOrcLLJITRef Jit = NULL;
LLVMOrcThreadSafeContextRef TSC = LLVMOrcCreateNewThreadSafeContext();
LLVMErrorRef Err;
LLVMOrcJITTargetMachineBuilderRef JTMB;
{
if ((Err = LLVMOrcJITTargetMachineBuilderDetectHost(&JTMB))) {
err_exit:
LLVMOrcDisposeThreadSafeContext(TSC);
LLVMOrcDisposeLLJIT(Jit);
char *ErrMsg = LLVMGetErrorMessage(Err);
fprintf(stderr, "Error: %s\n", ErrMsg);
LLVMDisposeErrorMessage(ErrMsg);
return 1;
}
LLVMOrcLLJITBuilderRef JB = LLVMOrcCreateLLJITBuilder();
LLVMOrcLLJITBuilderSetJITTargetMachineBuilder(JB, JTMB);
if ((Err = LLVMOrcCreateLLJIT(&Jit, JB)))
goto err_exit;
}
int count = argc > 1 ? atoi(argv[1]) : 1;
loop:
LLVMOrcJITDylibRef JD = LLVMOrcExecutionSessionCreateBareJITDylib
(LLVMOrcLLJITGetExecutionSession(Jit), "<main>");
{
LLVMOrcDefinitionGeneratorRef DG;
if ((Err = LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess
(&DG, LLVMOrcLLJITGetGlobalPrefix(Jit), NULL, NULL)))
goto err_exit;
LLVMOrcJITDylibAddGenerator(JD, DG);
}
{
LLVMOrcThreadSafeModuleRef TSM;
LLVMContextRef C = LLVMOrcThreadSafeContextGetContext(TSC);
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("heLLoVM-C", C);
LLVMTypeRef stringType = LLVMPointerType(LLVMInt8TypeInContext(C), 0);
LLVMTypeRef intType = LLVMInt32TypeInContext(C);
LLVMTypeRef PutsType = LLVMFunctionType(intType, &stringType, 1, FALSE);
LLVMTypeRef PutsArgs[3];
PutsArgs[0] = stringType;
PutsArgs[1] = LLVMPointerType(PutsType, 0);
PutsArgs[2] = intType;
LLVMTypeRef FT = LLVMFunctionType(intType, PutsArgs, 3, FALSE);
LLVMValueRef TheFunction = LLVMAddFunction(M, "boo", FT);
LLVMSetLinkage(TheFunction, LLVMExternalLinkage);
{
LLVMValueRef greets[2];
greets[0] = LLVMConstString("world", 6, TRUE);
greets[1] = LLVMConstString("all\0\0", 6, TRUE);
LLVMTypeRef greetType = LLVMArrayType(LLVMTypeOf(greets[0]), 2);
LLVMValueRef GV = LLVMAddGlobal(M, greetType, "greetings");
LLVMSetInitializer(GV, LLVMConstArray(greetType, greets, 2));
LLVMSetGlobalConstant(GV, TRUE);
LLVMSetUnnamedAddress(GV, LLVMLocalUnnamedAddr);
LLVMSetLinkage(GV, LLVMExternalLinkage);
LLVMSetAlignment(GV, 1);
LLVMSetSection(GV, ".text");
LLVMBuilderRef builder = LLVMCreateBuilder();
LLVMBasicBlockRef BB =
LLVMAppendBasicBlockInContext(C, TheFunction, "entry");
LLVMPositionBuilderAtEnd(builder, BB);
LLVMValueRef args[3];
assert(LLVMCountParams(TheFunction) == 3);
LLVMGetParams(TheFunction, args);
LLVMValueRef c1 = LLVMBuildCall2(builder, PutsType, args[1],
args, 1, "");
LLVMValueRef indices[2];
indices[0] = LLVMConstInt(intType, 0, FALSE);
indices[1] = args[2];
LLVMValueRef c2a =
LLVMBuildInBoundsGEP2(builder, LLVMGlobalGetValueType(GV), GV,
indices, 2, "m");
c2a = LLVMBuildBitCast(builder, c2a, stringType, "");
LLVMValueRef c2 = LLVMBuildCall2(builder, PutsType, args[1],
&c2a, 1, "");
LLVMBuildRet(builder, LLVMBuildAdd(builder, c1, c2, ""));
LLVMDisposeBuilder(builder);
}
/* LLVMDumpModule(M); */
assert(!LLVMVerifyFunction(TheFunction, LLVMPrintMessageAction));
TSM = LLVMOrcCreateNewThreadSafeModule(M, TSC);
if ((Err = LLVMOrcLLJITAddLLVMIRModule(Jit, JD, TSM)))
goto err_exit;
}
LLVMOrcJITTargetAddress booAddr, greetingsAddr;
if ((Err = LLVM_Lookup(Jit, JD, &booAddr, "boo")) ||
(Err = LLVM_Lookup(Jit, JD, &greetingsAddr, "greetings")))
goto err_exit;
printf("boo=%" PRIx64 ", greetings=%" PRIx64 "\n", booAddr, greetingsAddr);
#if 0 // TODO: How to determine the length of the code?
// For some reason, ORCv2 on LLVM later than LLVM 13 may emit
// "greetings" before "boo" while MCJIT always seems to follow the
// same order.
assert(gv > f);
assert(f);
FILE *file = fopen("c.bin", "wb");
if (file) {
fwrite((const void*) f, gv - f + 12, 1, file);
fclose(file);
}
#endif
typedef int (*callback)(const char*);
int (*boo) (const char *, callback, unsigned) =
((int (*)(const char *, callback, unsigned)) booAddr);
int ret = boo("hello", puts, 0) + boo("goodbye", puts, 1);
#if LLVM_VERSION_MAJOR >= 14
LLVM_Remove(LLVMOrcLLJITGetExecutionSession(Jit), JD);
#endif
if (--count > 0)
goto loop;
LLVMOrcDisposeThreadSafeContext(TSC);
LLVMOrcDisposeLLJIT(Jit);
return ret;
}