blob: b8f7786f6a8c9d3b0dc37d33f9f4b7fd8e6799f6 [file] [log] [blame]
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001//===-- ExternalFunctions.cpp - Implement External Functions --------------===//
Chris Lattner7720c8e2001-09-10 04:50:17 +00002//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00003// This file contains both code to deal with invoking "external" functions, but
4// also contains code that implements "exported" external functions.
Chris Lattner7720c8e2001-09-10 04:50:17 +00005//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00006// External functions in LLI are implemented by dlopen'ing the lli executable
7// and using dlsym to look op the functions that we want to invoke. If a
8// function is found, then the arguments are mangled and passed in to the
9// function call.
Chris Lattner7720c8e2001-09-10 04:50:17 +000010//
11//===----------------------------------------------------------------------===//
12
13#include "Interpreter.h"
Chris Lattner005cbce2002-10-02 21:12:13 +000014#include "ExecutionAnnotations.h"
Chris Lattner7720c8e2001-09-10 04:50:17 +000015#include "llvm/DerivedTypes.h"
Chris Lattner005cbce2002-10-02 21:12:13 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Target/TargetData.h"
Chris Lattner7720c8e2001-09-10 04:50:17 +000018#include <map>
19#include <dlfcn.h>
20#include <link.h>
Chris Lattnerc2593162001-10-27 08:28:11 +000021#include <math.h>
Chris Lattnerc063d382001-11-06 21:52:18 +000022#include <stdio.h>
Chris Lattner697954c2002-01-20 22:54:45 +000023using std::vector;
24using std::cout;
Chris Lattner7720c8e2001-09-10 04:50:17 +000025
Chris Lattner005cbce2002-10-02 21:12:13 +000026extern TargetData TD;
Vikram S. Adveeb1a8452002-05-19 15:59:25 +000027
Chris Lattnerb408b122002-03-29 03:57:15 +000028typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
29static std::map<const Function *, ExFunc> Functions;
Chris Lattner697954c2002-01-20 22:54:45 +000030static std::map<std::string, ExFunc> FuncNames;
Chris Lattner7720c8e2001-09-10 04:50:17 +000031
Chris Lattnere43db882001-10-27 04:15:57 +000032static Interpreter *TheInterpreter;
33
34// getCurrentExecutablePath() - Return the directory that the lli executable
35// lives in.
36//
Chris Lattner697954c2002-01-20 22:54:45 +000037std::string Interpreter::getCurrentExecutablePath() const {
Chris Lattnere43db882001-10-27 04:15:57 +000038 Dl_info Info;
39 if (dladdr(&TheInterpreter, &Info) == 0) return "";
40
Chris Lattner697954c2002-01-20 22:54:45 +000041 std::string LinkAddr(Info.dli_fname);
Chris Lattnere43db882001-10-27 04:15:57 +000042 unsigned SlashPos = LinkAddr.rfind('/');
Chris Lattner697954c2002-01-20 22:54:45 +000043 if (SlashPos != std::string::npos)
Chris Lattnere43db882001-10-27 04:15:57 +000044 LinkAddr.resize(SlashPos); // Trim the executable name off...
45
46 return LinkAddr;
47}
48
49
Chris Lattner7720c8e2001-09-10 04:50:17 +000050static char getTypeID(const Type *Ty) {
51 switch (Ty->getPrimitiveID()) {
52 case Type::VoidTyID: return 'V';
53 case Type::BoolTyID: return 'o';
54 case Type::UByteTyID: return 'B';
55 case Type::SByteTyID: return 'b';
56 case Type::UShortTyID: return 'S';
57 case Type::ShortTyID: return 's';
58 case Type::UIntTyID: return 'I';
59 case Type::IntTyID: return 'i';
60 case Type::ULongTyID: return 'L';
61 case Type::LongTyID: return 'l';
62 case Type::FloatTyID: return 'F';
63 case Type::DoubleTyID: return 'D';
64 case Type::PointerTyID: return 'P';
Chris Lattnerb408b122002-03-29 03:57:15 +000065 case Type::FunctionTyID: return 'M';
Chris Lattner7720c8e2001-09-10 04:50:17 +000066 case Type::StructTyID: return 'T';
67 case Type::ArrayTyID: return 'A';
68 case Type::OpaqueTyID: return 'O';
69 default: return 'U';
70 }
71}
72
Chris Lattnerb408b122002-03-29 03:57:15 +000073static ExFunc lookupFunction(const Function *M) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000074 // Function not found, look it up... start by figuring out what the
75 // composite function name should be.
Chris Lattner697954c2002-01-20 22:54:45 +000076 std::string ExtName = "lle_";
Chris Lattnerb408b122002-03-29 03:57:15 +000077 const FunctionType *MT = M->getFunctionType();
Chris Lattner7720c8e2001-09-10 04:50:17 +000078 for (unsigned i = 0; const Type *Ty = MT->getContainedType(i); ++i)
79 ExtName += getTypeID(Ty);
80 ExtName += "_" + M->getName();
81
82 //cout << "Tried: '" << ExtName << "'\n";
Chris Lattner4721f132001-10-30 20:28:00 +000083 ExFunc FnPtr = FuncNames[ExtName];
84 if (FnPtr == 0)
85 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ExtName.c_str());
86 if (FnPtr == 0)
87 FnPtr = FuncNames["lle_X_"+M->getName()];
Chris Lattner7720c8e2001-09-10 04:50:17 +000088 if (FnPtr == 0) // Try calling a generic function... if it exists...
89 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ("lle_X_"+M->getName()).c_str());
90 if (FnPtr != 0)
Chris Lattner697954c2002-01-20 22:54:45 +000091 Functions.insert(std::make_pair(M, FnPtr)); // Cache for later
Chris Lattner7720c8e2001-09-10 04:50:17 +000092 return FnPtr;
93}
94
Chris Lattnerb408b122002-03-29 03:57:15 +000095GenericValue Interpreter::callExternalMethod(Function *M,
Chris Lattner4721f132001-10-30 20:28:00 +000096 const vector<GenericValue> &ArgVals) {
Chris Lattnere43db882001-10-27 04:15:57 +000097 TheInterpreter = this;
98
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000099 // Do a lookup to see if the function is in our cache... this should just be a
Chris Lattner7720c8e2001-09-10 04:50:17 +0000100 // defered annotation!
Chris Lattnerb408b122002-03-29 03:57:15 +0000101 std::map<const Function *, ExFunc>::iterator FI = Functions.find(M);
102 ExFunc Fn = (FI == Functions.end()) ? lookupFunction(M) : FI->second;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000103 if (Fn == 0) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000104 cout << "Tried to execute an unknown external function: "
Chris Lattner697954c2002-01-20 22:54:45 +0000105 << M->getType()->getDescription() << " " << M->getName() << "\n";
Chris Lattner4721f132001-10-30 20:28:00 +0000106 return GenericValue();
Chris Lattner7720c8e2001-09-10 04:50:17 +0000107 }
108
109 // TODO: FIXME when types are not const!
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000110 GenericValue Result = Fn(const_cast<FunctionType*>(M->getFunctionType()),
111 ArgVals);
Chris Lattner4721f132001-10-30 20:28:00 +0000112 return Result;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000113}
114
115
116//===----------------------------------------------------------------------===//
Chris Lattnerb408b122002-03-29 03:57:15 +0000117// Functions "exported" to the running application...
Chris Lattner7720c8e2001-09-10 04:50:17 +0000118//
119extern "C" { // Don't add C++ manglings to llvm mangling :)
120
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000121// Implement void printstr([ubyte {x N}] *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000122GenericValue lle_VP_printstr(FunctionType *M, const vector<GenericValue> &ArgVal){
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000123 assert(ArgVal.size() == 1 && "printstr only takes one argument!");
124 cout << (char*)ArgVal[0].PointerVal;
125 return GenericValue();
126}
127
Chris Lattner7720c8e2001-09-10 04:50:17 +0000128// Implement 'void print(X)' for every type...
Chris Lattnerb408b122002-03-29 03:57:15 +0000129GenericValue lle_X_print(FunctionType *M, const vector<GenericValue> &ArgVals) {
Chris Lattner7720c8e2001-09-10 04:50:17 +0000130 assert(ArgVals.size() == 1 && "generic print only takes one argument!");
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000131
132 Interpreter::print(M->getParamTypes()[0], ArgVals[0]);
133 return GenericValue();
134}
135
136// Implement 'void printVal(X)' for every type...
Chris Lattnerb408b122002-03-29 03:57:15 +0000137GenericValue lle_X_printVal(FunctionType *M, const vector<GenericValue> &ArgVal) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000138 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
139
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000140 // Specialize print([ubyte {x N} ] *) and print(sbyte *)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000141 if (const PointerType *PTy =
142 dyn_cast<PointerType>(M->getParamTypes()[0].get()))
Chris Lattner7a176752001-12-04 00:03:30 +0000143 if (PTy->getElementType() == Type::SByteTy ||
144 isa<ArrayType>(PTy->getElementType())) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000145 return lle_VP_printstr(M, ArgVal);
146 }
147
148 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);
Chris Lattner7720c8e2001-09-10 04:50:17 +0000149 return GenericValue();
150}
151
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000152// Implement 'void printString(X)'
153// Argument must be [ubyte {x N} ] * or sbyte *
Chris Lattnerb408b122002-03-29 03:57:15 +0000154GenericValue lle_X_printString(FunctionType *M, const vector<GenericValue> &ArgVal) {
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000155 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
156 return lle_VP_printstr(M, ArgVal);
157}
158
159// Implement 'void print<TYPE>(X)' for each primitive type or pointer type
160#define PRINT_TYPE_FUNC(TYPENAME,TYPEID) \
Chris Lattnerb408b122002-03-29 03:57:15 +0000161 GenericValue lle_X_print##TYPENAME(FunctionType *M,\
Chris Lattner316a65b2001-10-28 22:38:22 +0000162 const vector<GenericValue> &ArgVal) {\
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000163 assert(ArgVal.size() == 1 && "generic print only takes one argument!");\
Chris Lattner69b5ce92001-11-26 19:19:27 +0000164 assert(M->getParamTypes()[0].get()->getPrimitiveID() == Type::TYPEID);\
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000165 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);\
166 return GenericValue();\
167 }
168
Chris Lattner4721f132001-10-30 20:28:00 +0000169PRINT_TYPE_FUNC(SByte, SByteTyID)
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000170PRINT_TYPE_FUNC(UByte, UByteTyID)
171PRINT_TYPE_FUNC(Short, ShortTyID)
172PRINT_TYPE_FUNC(UShort, UShortTyID)
173PRINT_TYPE_FUNC(Int, IntTyID)
174PRINT_TYPE_FUNC(UInt, UIntTyID)
175PRINT_TYPE_FUNC(Long, LongTyID)
176PRINT_TYPE_FUNC(ULong, ULongTyID)
177PRINT_TYPE_FUNC(Float, FloatTyID)
178PRINT_TYPE_FUNC(Double, DoubleTyID)
179PRINT_TYPE_FUNC(Pointer, PointerTyID)
180
181
Chris Lattner005cbce2002-10-02 21:12:13 +0000182// void putchar(sbyte)
Chris Lattnerb408b122002-03-29 03:57:15 +0000183GenericValue lle_Vb_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner7720c8e2001-09-10 04:50:17 +0000184 cout << Args[0].SByteVal;
185 return GenericValue();
186}
187
Chris Lattner005cbce2002-10-02 21:12:13 +0000188// int putchar(int)
Chris Lattnerb408b122002-03-29 03:57:15 +0000189GenericValue lle_ii_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner697954c2002-01-20 22:54:45 +0000190 cout << ((char)Args[0].IntVal) << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000191 return Args[0];
192}
193
Chris Lattner005cbce2002-10-02 21:12:13 +0000194// void putchar(ubyte)
Chris Lattnerb408b122002-03-29 03:57:15 +0000195GenericValue lle_VB_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner697954c2002-01-20 22:54:45 +0000196 cout << Args[0].SByteVal << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000197 return Args[0];
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000198}
199
Chris Lattner005cbce2002-10-02 21:12:13 +0000200// void __main()
Chris Lattnerb408b122002-03-29 03:57:15 +0000201GenericValue lle_V___main(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000202 return GenericValue();
203}
204
Chris Lattner005cbce2002-10-02 21:12:13 +0000205// void exit(int)
Chris Lattnerb408b122002-03-29 03:57:15 +0000206GenericValue lle_X_exit(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere43db882001-10-27 04:15:57 +0000207 TheInterpreter->exitCalled(Args[0]);
208 return GenericValue();
209}
210
Chris Lattner005cbce2002-10-02 21:12:13 +0000211// void abort(void)
Chris Lattner1ee34a52002-05-20 21:17:16 +0000212GenericValue lle_X_abort(FunctionType *M, const vector<GenericValue> &Args) {
213 std::cerr << "***PROGRAM ABORTED***!\n";
214 GenericValue GV;
215 GV.IntVal = 1;
216 TheInterpreter->exitCalled(GV);
217 return GenericValue();
218}
219
Chris Lattnerc2593162001-10-27 08:28:11 +0000220// void *malloc(uint)
Chris Lattnerb408b122002-03-29 03:57:15 +0000221GenericValue lle_X_malloc(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner4721f132001-10-30 20:28:00 +0000222 assert(Args.size() == 1 && "Malloc expects one argument!");
Chris Lattnerc2593162001-10-27 08:28:11 +0000223 GenericValue GV;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000224 GV.PointerVal = (PointerTy)malloc(Args[0].UIntVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000225 return GV;
226}
227
228// void free(void *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000229GenericValue lle_X_free(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000230 assert(Args.size() == 1);
Chris Lattner4721f132001-10-30 20:28:00 +0000231 free((void*)Args[0].PointerVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000232 return GenericValue();
233}
234
Chris Lattner782b9392001-11-26 18:18:18 +0000235// int atoi(char *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000236GenericValue lle_X_atoi(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000237 assert(Args.size() == 1);
238 GenericValue GV;
239 GV.IntVal = atoi((char*)Args[0].PointerVal);
240 return GV;
241}
242
Chris Lattnerc2593162001-10-27 08:28:11 +0000243// double pow(double, double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000244GenericValue lle_X_pow(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000245 assert(Args.size() == 2);
Chris Lattnerc2593162001-10-27 08:28:11 +0000246 GenericValue GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000247 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000248 return GV;
249}
250
Chris Lattner34dd24b2002-02-18 19:06:25 +0000251// double exp(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000252GenericValue lle_X_exp(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner34dd24b2002-02-18 19:06:25 +0000253 assert(Args.size() == 1);
254 GenericValue GV;
255 GV.DoubleVal = exp(Args[0].DoubleVal);
256 return GV;
257}
258
Chris Lattnerc063d382001-11-06 21:52:18 +0000259// double sqrt(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000260GenericValue lle_X_sqrt(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000261 assert(Args.size() == 1);
262 GenericValue GV;
263 GV.DoubleVal = sqrt(Args[0].DoubleVal);
264 return GV;
265}
Chris Lattnerc2593162001-10-27 08:28:11 +0000266
Chris Lattner86790052001-11-06 22:53:25 +0000267// double log(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000268GenericValue lle_X_log(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000269 assert(Args.size() == 1);
270 GenericValue GV;
271 GV.DoubleVal = log(Args[0].DoubleVal);
272 return GV;
273}
274
Chris Lattner782b9392001-11-26 18:18:18 +0000275// double floor(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000276GenericValue lle_X_floor(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000277 assert(Args.size() == 1);
278 GenericValue GV;
279 GV.DoubleVal = floor(Args[0].DoubleVal);
280 return GV;
281}
282
Chris Lattner86790052001-11-06 22:53:25 +0000283// double drand48()
Chris Lattnerb408b122002-03-29 03:57:15 +0000284GenericValue lle_X_drand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000285 assert(Args.size() == 0);
286 GenericValue GV;
287 GV.DoubleVal = drand48();
288 return GV;
289}
290
Chris Lattner1b600142001-11-13 05:46:08 +0000291// long lrand48()
Chris Lattnerb408b122002-03-29 03:57:15 +0000292GenericValue lle_X_lrand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000293 assert(Args.size() == 0);
294 GenericValue GV;
295 GV.IntVal = lrand48();
296 return GV;
297}
298
299// void srand48(long)
Chris Lattnerb408b122002-03-29 03:57:15 +0000300GenericValue lle_X_srand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000301 assert(Args.size() == 1);
302 srand48(Args[0].IntVal);
303 return GenericValue();
304}
305
Chris Lattner782b9392001-11-26 18:18:18 +0000306// void srand(uint)
Chris Lattnerb408b122002-03-29 03:57:15 +0000307GenericValue lle_X_srand(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000308 assert(Args.size() == 1);
309 srand(Args[0].UIntVal);
310 return GenericValue();
311}
Chris Lattner86790052001-11-06 22:53:25 +0000312
Chris Lattnere7c6f722001-12-13 00:43:47 +0000313// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
314// output useful.
Chris Lattnerb408b122002-03-29 03:57:15 +0000315GenericValue lle_X_sprintf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000316 char *OutputBuffer = (char *)Args[0].PointerVal;
317 const char *FmtStr = (const char *)Args[1].PointerVal;
318 unsigned ArgNo = 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000319
320 // printf should return # chars printed. This is completely incorrect, but
321 // close enough for now.
322 GenericValue GV; GV.IntVal = strlen(FmtStr);
323 while (1) {
324 switch (*FmtStr) {
325 case 0: return GV; // Null terminator...
326 default: // Normal nonspecial character
Chris Lattnere7c6f722001-12-13 00:43:47 +0000327 sprintf(OutputBuffer++, "%c", *FmtStr++);
Chris Lattner08845a22001-10-29 20:27:45 +0000328 break;
329 case '\\': { // Handle escape codes
Chris Lattnere7c6f722001-12-13 00:43:47 +0000330 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
331 FmtStr += 2; OutputBuffer += 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000332 break;
333 }
334 case '%': { // Handle format specifiers
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000335 char FmtBuf[100] = "", Buffer[1000] = "";
336 char *FB = FmtBuf;
337 *FB++ = *FmtStr++;
338 char Last = *FB++ = *FmtStr++;
339 unsigned HowLong = 0;
340 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
341 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
342 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
343 Last != 'p' && Last != 's' && Last != '%') {
344 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
345 Last = *FB++ = *FmtStr++;
Chris Lattner08845a22001-10-29 20:27:45 +0000346 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000347 *FB = 0;
348
349 switch (Last) {
350 case '%':
351 sprintf(Buffer, FmtBuf); break;
352 case 'c':
Chris Lattner2012d5e2002-04-17 17:43:01 +0000353 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000354 case 'd': case 'i':
355 case 'u': case 'o':
356 case 'x': case 'X':
Chris Lattner69ab7a82002-08-02 23:08:32 +0000357 if (HowLong >= 1) {
358 if (HowLong == 1) {
359 // Make sure we use %lld with a 64 bit argument because we might be
360 // compiling LLI on a 32 bit compiler.
361 unsigned Size = strlen(FmtBuf);
362 FmtBuf[Size] = FmtBuf[Size-1];
363 FmtBuf[Size+1] = 0;
364 FmtBuf[Size-1] = 'l';
365 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000366 sprintf(Buffer, FmtBuf, Args[ArgNo++].ULongVal);
Chris Lattner69ab7a82002-08-02 23:08:32 +0000367 } else
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000368 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
369 case 'e': case 'E': case 'g': case 'G': case 'f':
370 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
371 case 'p':
372 sprintf(Buffer, FmtBuf, (void*)Args[ArgNo++].PointerVal); break;
373 case 's':
374 sprintf(Buffer, FmtBuf, (char*)Args[ArgNo++].PointerVal); break;
375 default: cout << "<unknown printf code '" << *FmtStr << "'!>";
376 ArgNo++; break;
Chris Lattner08845a22001-10-29 20:27:45 +0000377 }
Chris Lattnere7c6f722001-12-13 00:43:47 +0000378 strcpy(OutputBuffer, Buffer);
379 OutputBuffer += strlen(Buffer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000380 }
Chris Lattner08845a22001-10-29 20:27:45 +0000381 break;
382 }
Chris Lattner08845a22001-10-29 20:27:45 +0000383 }
384}
385
Chris Lattnere7c6f722001-12-13 00:43:47 +0000386// int printf(sbyte *, ...) - a very rough implementation to make output useful.
Chris Lattnerb408b122002-03-29 03:57:15 +0000387GenericValue lle_X_printf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000388 char Buffer[10000];
389 vector<GenericValue> NewArgs;
390 GenericValue GV; GV.PointerVal = (PointerTy)Buffer;
391 NewArgs.push_back(GV);
392 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
393 GV = lle_X_sprintf(M, NewArgs);
394 cout << Buffer;
395 return GV;
396}
397
Chris Lattner665ee882002-03-08 22:51:07 +0000398// int sscanf(const char *format, ...);
Chris Lattnerb408b122002-03-29 03:57:15 +0000399GenericValue lle_X_sscanf(FunctionType *M, const vector<GenericValue> &args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000400 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
401
402 const char *Args[10];
403 for (unsigned i = 0; i < args.size(); ++i)
404 Args[i] = (const char*)args[i].PointerVal;
405
406 GenericValue GV;
407 GV.IntVal = sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
408 Args[5], Args[6], Args[7], Args[8], Args[9]);
409 return GV;
410}
411
412
Chris Lattner295fe672002-01-23 21:38:07 +0000413// int clock(void) - Profiling implementation
Chris Lattnerb408b122002-03-29 03:57:15 +0000414GenericValue lle_i_clock(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner295fe672002-01-23 21:38:07 +0000415 extern int clock(void);
416 GenericValue GV; GV.IntVal = clock();
417 return GV;
418}
Chris Lattnere7c6f722001-12-13 00:43:47 +0000419
Chris Lattner665ee882002-03-08 22:51:07 +0000420//===----------------------------------------------------------------------===//
421// IO Functions...
422//===----------------------------------------------------------------------===//
423
Chris Lattner005cbce2002-10-02 21:12:13 +0000424// getFILE - Turn a pointer in the host address space into a legit pointer in
425// the interpreter address space. For the most part, this is an identity
426// transformation, but if the program refers to stdio, stderr, stdin then they
427// have pointers that are relative to the __iob array. If this is the case,
428// change the FILE into the REAL stdio stream.
429//
430static FILE *getFILE(PointerTy Ptr) {
431 static Module *LastMod = 0;
432 static PointerTy IOBBase = 0;
433 static unsigned FILESize;
434
435 if (LastMod != TheInterpreter->getModule()) { // Module change or initialize?
436 Module *M = LastMod = TheInterpreter->getModule();
437
438 // Check to see if the currently loaded module contains an __iob symbol...
439 GlobalVariable *IOB = 0;
440 if (SymbolTable *ST = M->getSymbolTable()) {
441 for (SymbolTable::iterator I = ST->begin(), E = ST->end(); I != E; ++I) {
442 SymbolTable::VarMap &M = I->second;
443 for (SymbolTable::VarMap::iterator J = M.begin(), E = M.end();
444 J != E; ++J)
445 if (J->first == "__iob")
446 if ((IOB = dyn_cast<GlobalVariable>(J->second)))
447 break;
448 if (IOB) break;
449 }
450 }
451
452 // If we found an __iob symbol now, find out what the actual address it's
453 // held in is...
454 if (IOB) {
455 // Get the address the array lives in...
456 GlobalAddress *Address =
457 (GlobalAddress*)IOB->getOrCreateAnnotation(GlobalAddressAID);
458 IOBBase = (PointerTy)(GenericValue*)Address->Ptr;
459
460 // Figure out how big each element of the array is...
461 const ArrayType *AT =
462 dyn_cast<ArrayType>(IOB->getType()->getElementType());
463 if (AT)
464 FILESize = TD.getTypeSize(AT->getElementType());
465 else
466 FILESize = 16*8; // Default size
467 }
468 }
469
470 // Check to see if this is a reference to __iob...
471 if (IOBBase) {
472 unsigned FDNum = (Ptr-IOBBase)/FILESize;
473 if (FDNum == 0)
474 return stdin;
475 else if (FDNum == 1)
476 return stdout;
477 else if (FDNum == 2)
478 return stderr;
479 }
480
481 return (FILE*)Ptr;
482}
483
484
Chris Lattner665ee882002-03-08 22:51:07 +0000485// FILE *fopen(const char *filename, const char *mode);
Chris Lattnerb408b122002-03-29 03:57:15 +0000486GenericValue lle_X_fopen(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000487 assert(Args.size() == 2);
488 GenericValue GV;
489
490 GV.PointerVal = (PointerTy)fopen((const char *)Args[0].PointerVal,
491 (const char *)Args[1].PointerVal);
492 return GV;
493}
494
495// int fclose(FILE *F);
Chris Lattnerb408b122002-03-29 03:57:15 +0000496GenericValue lle_X_fclose(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000497 assert(Args.size() == 1);
498 GenericValue GV;
499
Chris Lattner005cbce2002-10-02 21:12:13 +0000500 GV.IntVal = fclose(getFILE(Args[0].PointerVal));
Chris Lattner665ee882002-03-08 22:51:07 +0000501 return GV;
502}
503
504// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000505GenericValue lle_X_fread(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000506 assert(Args.size() == 4);
507 GenericValue GV;
508
509 GV.UIntVal = fread((void*)Args[0].PointerVal, Args[1].UIntVal,
Chris Lattner005cbce2002-10-02 21:12:13 +0000510 Args[2].UIntVal, getFILE(Args[3].PointerVal));
Chris Lattner665ee882002-03-08 22:51:07 +0000511 return GV;
512}
513
514// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000515GenericValue lle_X_fwrite(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000516 assert(Args.size() == 4);
517 GenericValue GV;
518
519 GV.UIntVal = fwrite((void*)Args[0].PointerVal, Args[1].UIntVal,
Chris Lattner005cbce2002-10-02 21:12:13 +0000520 Args[2].UIntVal, getFILE(Args[3].PointerVal));
Chris Lattner665ee882002-03-08 22:51:07 +0000521 return GV;
522}
523
524// char *fgets(char *s, int n, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000525GenericValue lle_X_fgets(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000526 assert(Args.size() == 3);
527 GenericValue GV;
528
529 GV.PointerVal = (PointerTy)fgets((char*)Args[0].PointerVal, Args[1].IntVal,
Chris Lattner005cbce2002-10-02 21:12:13 +0000530 getFILE(Args[2].PointerVal));
Chris Lattner665ee882002-03-08 22:51:07 +0000531 return GV;
532}
533
534// int fflush(FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000535GenericValue lle_X_fflush(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000536 assert(Args.size() == 1);
537 GenericValue GV;
538
Chris Lattner005cbce2002-10-02 21:12:13 +0000539 GV.IntVal = fflush(getFILE(Args[0].PointerVal));
540 return GV;
541}
542
543// int getc(FILE *stream);
544GenericValue lle_X_getc(FunctionType *M, const vector<GenericValue> &Args) {
545 assert(Args.size() == 1);
546 GenericValue GV;
547
548 GV.IntVal = getc(getFILE(Args[0].PointerVal));
Chris Lattner665ee882002-03-08 22:51:07 +0000549 return GV;
550}
551
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000552// int fputc(int C, FILE *stream);
553GenericValue lle_X_fputc(FunctionType *M, const vector<GenericValue> &Args) {
554 assert(Args.size() == 2);
555 GenericValue GV;
556 GV.IntVal = fputc(Args[0].IntVal, getFILE(Args[1].PointerVal));
557 return GV;
558}
559
560// int ungetc(int C, FILE *stream);
561GenericValue lle_X_ungetc(FunctionType *M, const vector<GenericValue> &Args) {
562 assert(Args.size() == 2);
563 GenericValue GV;
564 GV.IntVal = ungetc(Args[0].IntVal, getFILE(Args[1].PointerVal));
565 return GV;
566}
567
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000568// int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
569// useful.
570GenericValue lle_X_fprintf(FunctionType *M, const vector<GenericValue> &Args) {
571 assert(Args.size() > 2);
572 char Buffer[10000];
573 vector<GenericValue> NewArgs;
574 GenericValue GV; GV.PointerVal = (PointerTy)Buffer;
575 NewArgs.push_back(GV);
576 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
577 GV = lle_X_sprintf(M, NewArgs);
578
579 fputs(Buffer, getFILE(Args[0].PointerVal));
580 return GV;
581}
582
Chris Lattner7720c8e2001-09-10 04:50:17 +0000583} // End extern "C"
Chris Lattner4721f132001-10-30 20:28:00 +0000584
585
586void Interpreter::initializeExternalMethods() {
587 FuncNames["lle_VP_printstr"] = lle_VP_printstr;
588 FuncNames["lle_X_print"] = lle_X_print;
589 FuncNames["lle_X_printVal"] = lle_X_printVal;
590 FuncNames["lle_X_printString"] = lle_X_printString;
591 FuncNames["lle_X_printUByte"] = lle_X_printUByte;
592 FuncNames["lle_X_printSByte"] = lle_X_printSByte;
593 FuncNames["lle_X_printUShort"] = lle_X_printUShort;
594 FuncNames["lle_X_printShort"] = lle_X_printShort;
595 FuncNames["lle_X_printInt"] = lle_X_printInt;
596 FuncNames["lle_X_printUInt"] = lle_X_printUInt;
597 FuncNames["lle_X_printLong"] = lle_X_printLong;
598 FuncNames["lle_X_printULong"] = lle_X_printULong;
599 FuncNames["lle_X_printFloat"] = lle_X_printFloat;
600 FuncNames["lle_X_printDouble"] = lle_X_printDouble;
601 FuncNames["lle_X_printPointer"] = lle_X_printPointer;
Chris Lattner0f279b22001-11-03 10:15:32 +0000602 FuncNames["lle_Vb_putchar"] = lle_Vb_putchar;
603 FuncNames["lle_ii_putchar"] = lle_ii_putchar;
604 FuncNames["lle_VB_putchar"] = lle_VB_putchar;
605 FuncNames["lle_V___main"] = lle_V___main;
606 FuncNames["lle_X_exit"] = lle_X_exit;
Chris Lattner1ee34a52002-05-20 21:17:16 +0000607 FuncNames["lle_X_abort"] = lle_X_abort;
Chris Lattner0f279b22001-11-03 10:15:32 +0000608 FuncNames["lle_X_malloc"] = lle_X_malloc;
609 FuncNames["lle_X_free"] = lle_X_free;
Chris Lattner782b9392001-11-26 18:18:18 +0000610 FuncNames["lle_X_atoi"] = lle_X_atoi;
Chris Lattner0f279b22001-11-03 10:15:32 +0000611 FuncNames["lle_X_pow"] = lle_X_pow;
Chris Lattner34dd24b2002-02-18 19:06:25 +0000612 FuncNames["lle_X_exp"] = lle_X_exp;
Chris Lattner1b600142001-11-13 05:46:08 +0000613 FuncNames["lle_X_log"] = lle_X_log;
Chris Lattner782b9392001-11-26 18:18:18 +0000614 FuncNames["lle_X_floor"] = lle_X_floor;
615 FuncNames["lle_X_srand"] = lle_X_srand;
Chris Lattner1b600142001-11-13 05:46:08 +0000616 FuncNames["lle_X_drand48"] = lle_X_drand48;
617 FuncNames["lle_X_srand48"] = lle_X_srand48;
618 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
Chris Lattnerc063d382001-11-06 21:52:18 +0000619 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
Chris Lattner0f279b22001-11-03 10:15:32 +0000620 FuncNames["lle_X_printf"] = lle_X_printf;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000621 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
Chris Lattner665ee882002-03-08 22:51:07 +0000622 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
Chris Lattner295fe672002-01-23 21:38:07 +0000623 FuncNames["lle_i_clock"] = lle_i_clock;
Chris Lattner665ee882002-03-08 22:51:07 +0000624 FuncNames["lle_X_fopen"] = lle_X_fopen;
625 FuncNames["lle_X_fclose"] = lle_X_fclose;
626 FuncNames["lle_X_fread"] = lle_X_fread;
627 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
628 FuncNames["lle_X_fgets"] = lle_X_fgets;
629 FuncNames["lle_X_fflush"] = lle_X_fflush;
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000630 FuncNames["lle_X_fgetc"] = lle_X_getc;
Chris Lattner005cbce2002-10-02 21:12:13 +0000631 FuncNames["lle_X_getc"] = lle_X_getc;
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000632 FuncNames["lle_X_fputc"] = lle_X_fputc;
633 FuncNames["lle_X_ungetc"] = lle_X_ungetc;
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000634 FuncNames["lle_X_fprintf"] = lle_X_fprintf;
Chris Lattner4721f132001-10-30 20:28:00 +0000635}