blob: 4c2039ded4c9cf04e479b6ed8cfb214c2db677bc [file] [log] [blame]
Chris Lattner7720c8e2001-09-10 04:50:17 +00001//===-- ExternalMethods.cpp - Implement External Methods ------------------===//
2//
3// This file contains both code to deal with invoking "external" methods, but
4// also contains code that implements "exported" external methods.
5//
6// External methods in LLI are implemented by dlopen'ing the lli executable and
7// using dlsym to look op the methods that we want to invoke. If a method is
8// found, then the arguments are mangled and passed in to the function call.
9//
10//===----------------------------------------------------------------------===//
11
12#include "Interpreter.h"
13#include "llvm/DerivedTypes.h"
14#include <map>
15#include <dlfcn.h>
16#include <link.h>
17
18typedef GenericValue (*ExFunc)(MethodType *, const vector<GenericValue> &);
19static map<const Method *, ExFunc> Functions;
20
21static char getTypeID(const Type *Ty) {
22 switch (Ty->getPrimitiveID()) {
23 case Type::VoidTyID: return 'V';
24 case Type::BoolTyID: return 'o';
25 case Type::UByteTyID: return 'B';
26 case Type::SByteTyID: return 'b';
27 case Type::UShortTyID: return 'S';
28 case Type::ShortTyID: return 's';
29 case Type::UIntTyID: return 'I';
30 case Type::IntTyID: return 'i';
31 case Type::ULongTyID: return 'L';
32 case Type::LongTyID: return 'l';
33 case Type::FloatTyID: return 'F';
34 case Type::DoubleTyID: return 'D';
35 case Type::PointerTyID: return 'P';
36 case Type::MethodTyID: return 'M';
37 case Type::StructTyID: return 'T';
38 case Type::ArrayTyID: return 'A';
39 case Type::OpaqueTyID: return 'O';
40 default: return 'U';
41 }
42}
43
44static ExFunc lookupMethod(const Method *M) {
45 // Function not found, look it up... start by figuring out what the
46 // composite function name should be.
47 string ExtName = "lle_";
Chris Lattneref9c23f2001-10-03 14:53:21 +000048 const MethodType *MT = M->getMethodType();
Chris Lattner7720c8e2001-09-10 04:50:17 +000049 for (unsigned i = 0; const Type *Ty = MT->getContainedType(i); ++i)
50 ExtName += getTypeID(Ty);
51 ExtName += "_" + M->getName();
52
53 //cout << "Tried: '" << ExtName << "'\n";
54 ExFunc FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ExtName.c_str());
55 if (FnPtr == 0) // Try calling a generic function... if it exists...
56 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ("lle_X_"+M->getName()).c_str());
57 if (FnPtr != 0)
58 Functions.insert(make_pair(M, FnPtr)); // Cache for later
59 return FnPtr;
60}
61
62void Interpreter::callExternalMethod(Method *M,
63 const vector<GenericValue> &ArgVals) {
64 // Do a lookup to see if the method is in our cache... this should just be a
65 // defered annotation!
66 map<const Method *, ExFunc>::iterator FI = Functions.find(M);
67 ExFunc Fn = (FI == Functions.end()) ? lookupMethod(M) : FI->second;
68 if (Fn == 0) {
69 cout << "Tried to execute an unknown external method: "
70 << M->getType()->getDescription() << " " << M->getName() << endl;
71 return;
72 }
73
74 // TODO: FIXME when types are not const!
Chris Lattneref9c23f2001-10-03 14:53:21 +000075 GenericValue Result = Fn(const_cast<MethodType*>(M->getMethodType()),ArgVals);
Chris Lattner7720c8e2001-09-10 04:50:17 +000076
77 // Copy the result back into the result variable if we are not returning void.
78 if (M->getReturnType() != Type::VoidTy) {
79 CallInst *Caller = ECStack.back().Caller;
80 if (Caller) {
81
82 } else {
83 // print it.
84 }
85 }
86}
87
88
89//===----------------------------------------------------------------------===//
90// Methods "exported" to the running application...
91//
92extern "C" { // Don't add C++ manglings to llvm mangling :)
93
94// Implement 'void print(X)' for every type...
95GenericValue lle_X_print(MethodType *M, const vector<GenericValue> &ArgVals) {
96 assert(ArgVals.size() == 1 && "generic print only takes one argument!");
97 Interpreter::printValue(M->getParamTypes()[0], ArgVals[0]);
98 return GenericValue();
99}
100
101// void "putchar"(sbyte)
102GenericValue lle_Vb_putchar(MethodType *M, const vector<GenericValue> &Args) {
103 cout << Args[0].SByteVal;
104 return GenericValue();
105}
106
107} // End extern "C"