blob: 7be3336c5865ca29be2ab4dc3cf77ad71203f0ab [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
Chris Lattner2e42d3a2001-10-15 05:51:48 +000094// Implement void printstr([ubyte {x N}] *)
95GenericValue lle_VP_printstr(MethodType *M, const vector<GenericValue> &ArgVal){
96 assert(ArgVal.size() == 1 && "printstr only takes one argument!");
97 cout << (char*)ArgVal[0].PointerVal;
98 return GenericValue();
99}
100
Chris Lattner7720c8e2001-09-10 04:50:17 +0000101// Implement 'void print(X)' for every type...
102GenericValue lle_X_print(MethodType *M, const vector<GenericValue> &ArgVals) {
103 assert(ArgVals.size() == 1 && "generic print only takes one argument!");
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000104
105 Interpreter::print(M->getParamTypes()[0], ArgVals[0]);
106 return GenericValue();
107}
108
109// Implement 'void printVal(X)' for every type...
110GenericValue lle_X_printVal(MethodType *M, const vector<GenericValue> &ArgVal) {
111 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
112
113 // Specialize print([ubyte {x N} ] *)
114 if (PointerType *PTy = dyn_cast<PointerType>(M->getParamTypes()[0].get()))
115 if (const ArrayType *ATy = dyn_cast<ArrayType>(PTy->getValueType())) {
116 return lle_VP_printstr(M, ArgVal);
117 }
118
119 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);
Chris Lattner7720c8e2001-09-10 04:50:17 +0000120 return GenericValue();
121}
122
123// void "putchar"(sbyte)
124GenericValue lle_Vb_putchar(MethodType *M, const vector<GenericValue> &Args) {
125 cout << Args[0].SByteVal;
126 return GenericValue();
127}
128
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000129// void "putchar"(ubyte)
130GenericValue lle_VB_putchar(MethodType *M, const vector<GenericValue> &Args) {
131 cout << Args[0].UByteVal;
132 return GenericValue();
133}
134
Chris Lattner7720c8e2001-09-10 04:50:17 +0000135} // End extern "C"