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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
//===-- ExternalMethods.cpp - Implement External Methods ------------------===//
//
// This file contains both code to deal with invoking "external" methods, but
// also contains code that implements "exported" external methods.
//
// External methods in LLI are implemented by dlopen'ing the lli executable and
// using dlsym to look op the methods that we want to invoke. If a method is
// found, then the arguments are mangled and passed in to the function call.
//
//===----------------------------------------------------------------------===//
#include "Interpreter.h"
#include "llvm/DerivedTypes.h"
#include <map>
#include <dlfcn.h>
#include <link.h>
#include <math.h>
#include <stdio.h>
typedef GenericValue (*ExFunc)(MethodType *, const vector<GenericValue> &);
static map<const Method *, ExFunc> Functions;
static map<string, ExFunc> FuncNames;
static Interpreter *TheInterpreter;
// getCurrentExecutablePath() - Return the directory that the lli executable
// lives in.
//
string Interpreter::getCurrentExecutablePath() const {
Dl_info Info;
if (dladdr(&TheInterpreter, &Info) == 0) return "";
string LinkAddr(Info.dli_fname);
unsigned SlashPos = LinkAddr.rfind('/');
if (SlashPos != string::npos)
LinkAddr.resize(SlashPos); // Trim the executable name off...
return LinkAddr;
}
static char getTypeID(const Type *Ty) {
switch (Ty->getPrimitiveID()) {
case Type::VoidTyID: return 'V';
case Type::BoolTyID: return 'o';
case Type::UByteTyID: return 'B';
case Type::SByteTyID: return 'b';
case Type::UShortTyID: return 'S';
case Type::ShortTyID: return 's';
case Type::UIntTyID: return 'I';
case Type::IntTyID: return 'i';
case Type::ULongTyID: return 'L';
case Type::LongTyID: return 'l';
case Type::FloatTyID: return 'F';
case Type::DoubleTyID: return 'D';
case Type::PointerTyID: return 'P';
case Type::MethodTyID: return 'M';
case Type::StructTyID: return 'T';
case Type::ArrayTyID: return 'A';
case Type::OpaqueTyID: return 'O';
default: return 'U';
}
}
static ExFunc lookupMethod(const Method *M) {
// Function not found, look it up... start by figuring out what the
// composite function name should be.
string ExtName = "lle_";
const MethodType *MT = M->getMethodType();
for (unsigned i = 0; const Type *Ty = MT->getContainedType(i); ++i)
ExtName += getTypeID(Ty);
ExtName += "_" + M->getName();
//cout << "Tried: '" << ExtName << "'\n";
ExFunc FnPtr = FuncNames[ExtName];
if (FnPtr == 0)
FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ExtName.c_str());
if (FnPtr == 0)
FnPtr = FuncNames["lle_X_"+M->getName()];
if (FnPtr == 0) // Try calling a generic function... if it exists...
FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ("lle_X_"+M->getName()).c_str());
if (FnPtr != 0)
Functions.insert(make_pair(M, FnPtr)); // Cache for later
return FnPtr;
}
GenericValue Interpreter::callExternalMethod(Method *M,
const vector<GenericValue> &ArgVals) {
TheInterpreter = this;
// Do a lookup to see if the method is in our cache... this should just be a
// defered annotation!
map<const Method *, ExFunc>::iterator FI = Functions.find(M);
ExFunc Fn = (FI == Functions.end()) ? lookupMethod(M) : FI->second;
if (Fn == 0) {
cout << "Tried to execute an unknown external method: "
<< M->getType()->getDescription() << " " << M->getName() << endl;
return GenericValue();
}
// TODO: FIXME when types are not const!
GenericValue Result = Fn(const_cast<MethodType*>(M->getMethodType()),ArgVals);
return Result;
}
//===----------------------------------------------------------------------===//
// Methods "exported" to the running application...
//
extern "C" { // Don't add C++ manglings to llvm mangling :)
// Implement void printstr([ubyte {x N}] *)
GenericValue lle_VP_printstr(MethodType *M, const vector<GenericValue> &ArgVal){
assert(ArgVal.size() == 1 && "printstr only takes one argument!");
cout << (char*)ArgVal[0].PointerVal;
return GenericValue();
}
// Implement 'void print(X)' for every type...
GenericValue lle_X_print(MethodType *M, const vector<GenericValue> &ArgVals) {
assert(ArgVals.size() == 1 && "generic print only takes one argument!");
Interpreter::print(M->getParamTypes()[0], ArgVals[0]);
return GenericValue();
}
// Implement 'void printVal(X)' for every type...
GenericValue lle_X_printVal(MethodType *M, const vector<GenericValue> &ArgVal) {
assert(ArgVal.size() == 1 && "generic print only takes one argument!");
// Specialize print([ubyte {x N} ] *) and print(sbyte *)
if (PointerType *PTy = dyn_cast<PointerType>(M->getParamTypes()[0].get()))
if (PTy->getValueType() == Type::SByteTy ||
isa<ArrayType>(PTy->getValueType())) {
return lle_VP_printstr(M, ArgVal);
}
Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);
return GenericValue();
}
// Implement 'void printString(X)'
// Argument must be [ubyte {x N} ] * or sbyte *
GenericValue lle_X_printString(MethodType *M, const vector<GenericValue> &ArgVal) {
assert(ArgVal.size() == 1 && "generic print only takes one argument!");
return lle_VP_printstr(M, ArgVal);
}
// Implement 'void print<TYPE>(X)' for each primitive type or pointer type
#define PRINT_TYPE_FUNC(TYPENAME,TYPEID) \
GenericValue lle_X_print##TYPENAME(MethodType *M,\
const vector<GenericValue> &ArgVal) {\
assert(ArgVal.size() == 1 && "generic print only takes one argument!");\
assert(M->getParamTypes()[0].get()->getPrimitiveID() == Type::##TYPEID);\
Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);\
return GenericValue();\
}
PRINT_TYPE_FUNC(SByte, SByteTyID)
PRINT_TYPE_FUNC(UByte, UByteTyID)
PRINT_TYPE_FUNC(Short, ShortTyID)
PRINT_TYPE_FUNC(UShort, UShortTyID)
PRINT_TYPE_FUNC(Int, IntTyID)
PRINT_TYPE_FUNC(UInt, UIntTyID)
PRINT_TYPE_FUNC(Long, LongTyID)
PRINT_TYPE_FUNC(ULong, ULongTyID)
PRINT_TYPE_FUNC(Float, FloatTyID)
PRINT_TYPE_FUNC(Double, DoubleTyID)
PRINT_TYPE_FUNC(Pointer, PointerTyID)
// void "putchar"(sbyte)
GenericValue lle_Vb_putchar(MethodType *M, const vector<GenericValue> &Args) {
cout << Args[0].SByteVal;
return GenericValue();
}
// int "putchar"(int)
GenericValue lle_ii_putchar(MethodType *M, const vector<GenericValue> &Args) {
cout << ((char)Args[0].IntVal) << flush;
return Args[0];
}
// void "putchar"(ubyte)
GenericValue lle_VB_putchar(MethodType *M, const vector<GenericValue> &Args) {
cout << Args[0].SByteVal << flush;
return Args[0];
}
// void "__main"()
GenericValue lle_V___main(MethodType *M, const vector<GenericValue> &Args) {
return GenericValue();
}
// void "exit"(int)
GenericValue lle_X_exit(MethodType *M, const vector<GenericValue> &Args) {
TheInterpreter->exitCalled(Args[0]);
return GenericValue();
}
// void *malloc(uint)
GenericValue lle_X_malloc(MethodType *M, const vector<GenericValue> &Args) {
assert(Args.size() == 1 && "Malloc expects one argument!");
GenericValue GV;
GV.PointerVal = (PointerTy)malloc(Args[0].UIntVal);
return GV;
}
// void free(void *)
GenericValue lle_X_free(MethodType *M, const vector<GenericValue> &Args) {
assert(Args.size() == 1);
free((void*)Args[0].PointerVal);
return GenericValue();
}
// double pow(double, double)
GenericValue lle_X_po
|