blob: 973cca68422b5f693fc3c844f51d515c1bf81ad0 [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
Chris Lattnere43db882001-10-27 04:15:57 +000021static Interpreter *TheInterpreter;
22
23// getCurrentExecutablePath() - Return the directory that the lli executable
24// lives in.
25//
26string Interpreter::getCurrentExecutablePath() const {
27 Dl_info Info;
28 if (dladdr(&TheInterpreter, &Info) == 0) return "";
29
30 string LinkAddr(Info.dli_fname);
31 unsigned SlashPos = LinkAddr.rfind('/');
32 if (SlashPos != string::npos)
33 LinkAddr.resize(SlashPos); // Trim the executable name off...
34
35 return LinkAddr;
36}
37
38
Chris Lattner7720c8e2001-09-10 04:50:17 +000039static char getTypeID(const Type *Ty) {
40 switch (Ty->getPrimitiveID()) {
41 case Type::VoidTyID: return 'V';
42 case Type::BoolTyID: return 'o';
43 case Type::UByteTyID: return 'B';
44 case Type::SByteTyID: return 'b';
45 case Type::UShortTyID: return 'S';
46 case Type::ShortTyID: return 's';
47 case Type::UIntTyID: return 'I';
48 case Type::IntTyID: return 'i';
49 case Type::ULongTyID: return 'L';
50 case Type::LongTyID: return 'l';
51 case Type::FloatTyID: return 'F';
52 case Type::DoubleTyID: return 'D';
53 case Type::PointerTyID: return 'P';
54 case Type::MethodTyID: return 'M';
55 case Type::StructTyID: return 'T';
56 case Type::ArrayTyID: return 'A';
57 case Type::OpaqueTyID: return 'O';
58 default: return 'U';
59 }
60}
61
62static ExFunc lookupMethod(const Method *M) {
63 // Function not found, look it up... start by figuring out what the
64 // composite function name should be.
65 string ExtName = "lle_";
Chris Lattneref9c23f2001-10-03 14:53:21 +000066 const MethodType *MT = M->getMethodType();
Chris Lattner7720c8e2001-09-10 04:50:17 +000067 for (unsigned i = 0; const Type *Ty = MT->getContainedType(i); ++i)
68 ExtName += getTypeID(Ty);
69 ExtName += "_" + M->getName();
70
71 //cout << "Tried: '" << ExtName << "'\n";
72 ExFunc FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ExtName.c_str());
73 if (FnPtr == 0) // Try calling a generic function... if it exists...
74 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ("lle_X_"+M->getName()).c_str());
75 if (FnPtr != 0)
76 Functions.insert(make_pair(M, FnPtr)); // Cache for later
77 return FnPtr;
78}
79
80void Interpreter::callExternalMethod(Method *M,
81 const vector<GenericValue> &ArgVals) {
Chris Lattnere43db882001-10-27 04:15:57 +000082 TheInterpreter = this;
83
Chris Lattner7720c8e2001-09-10 04:50:17 +000084 // Do a lookup to see if the method is in our cache... this should just be a
85 // defered annotation!
86 map<const Method *, ExFunc>::iterator FI = Functions.find(M);
87 ExFunc Fn = (FI == Functions.end()) ? lookupMethod(M) : FI->second;
88 if (Fn == 0) {
89 cout << "Tried to execute an unknown external method: "
90 << M->getType()->getDescription() << " " << M->getName() << endl;
91 return;
92 }
93
94 // TODO: FIXME when types are not const!
Chris Lattneref9c23f2001-10-03 14:53:21 +000095 GenericValue Result = Fn(const_cast<MethodType*>(M->getMethodType()),ArgVals);
Chris Lattner7720c8e2001-09-10 04:50:17 +000096
97 // Copy the result back into the result variable if we are not returning void.
98 if (M->getReturnType() != Type::VoidTy) {
99 CallInst *Caller = ECStack.back().Caller;
100 if (Caller) {
101
102 } else {
103 // print it.
104 }
105 }
106}
107
108
109//===----------------------------------------------------------------------===//
110// Methods "exported" to the running application...
111//
112extern "C" { // Don't add C++ manglings to llvm mangling :)
113
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000114// Implement void printstr([ubyte {x N}] *)
115GenericValue lle_VP_printstr(MethodType *M, const vector<GenericValue> &ArgVal){
116 assert(ArgVal.size() == 1 && "printstr only takes one argument!");
117 cout << (char*)ArgVal[0].PointerVal;
118 return GenericValue();
119}
120
Chris Lattner7720c8e2001-09-10 04:50:17 +0000121// Implement 'void print(X)' for every type...
122GenericValue lle_X_print(MethodType *M, const vector<GenericValue> &ArgVals) {
123 assert(ArgVals.size() == 1 && "generic print only takes one argument!");
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000124
125 Interpreter::print(M->getParamTypes()[0], ArgVals[0]);
126 return GenericValue();
127}
128
129// Implement 'void printVal(X)' for every type...
130GenericValue lle_X_printVal(MethodType *M, const vector<GenericValue> &ArgVal) {
131 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
132
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000133 // Specialize print([ubyte {x N} ] *) and print(sbyte *)
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000134 if (PointerType *PTy = dyn_cast<PointerType>(M->getParamTypes()[0].get()))
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000135 if (PTy->getValueType() == Type::SByteTy ||
136 isa<ArrayType>(PTy->getValueType())) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000137 return lle_VP_printstr(M, ArgVal);
138 }
139
140 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);
Chris Lattner7720c8e2001-09-10 04:50:17 +0000141 return GenericValue();
142}
143
144// void "putchar"(sbyte)
145GenericValue lle_Vb_putchar(MethodType *M, const vector<GenericValue> &Args) {
146 cout << Args[0].SByteVal;
147 return GenericValue();
148}
149
Chris Lattnere43db882001-10-27 04:15:57 +0000150// int "putchar"(int)
151GenericValue lle_ii_putchar(MethodType *M, const vector<GenericValue> &Args) {
152 cout << ((char)Args[0].IntVal) << flush;
153 return Args[0];
154}
155
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000156// void "putchar"(ubyte)
157GenericValue lle_VB_putchar(MethodType *M, const vector<GenericValue> &Args) {
Chris Lattnere43db882001-10-27 04:15:57 +0000158 cout << Args[0].SByteVal << flush;
159 return Args[0];
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000160}
161
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000162// void "__main"()
163GenericValue lle_V___main(MethodType *M, const vector<GenericValue> &Args) {
164 return GenericValue();
165}
166
Chris Lattnere43db882001-10-27 04:15:57 +0000167// void "exit"(int)
168GenericValue lle_Vi_exit(MethodType *M, const vector<GenericValue> &Args) {
169 TheInterpreter->exitCalled(Args[0]);
170 return GenericValue();
171}
172
Chris Lattner7720c8e2001-09-10 04:50:17 +0000173} // End extern "C"