blob: 0d8cae2fea1c13019cb43dc464a6fc0e15a148a5 [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 Lattnerfe11a972002-12-23 23:59:41 +000015#include "llvm/Module.h"
Chris Lattner7720c8e2001-09-10 04:50:17 +000016#include "llvm/DerivedTypes.h"
Chris Lattner005cbce2002-10-02 21:12:13 +000017#include "llvm/SymbolTable.h"
18#include "llvm/Target/TargetData.h"
Chris Lattner7720c8e2001-09-10 04:50:17 +000019#include <map>
20#include <dlfcn.h>
21#include <link.h>
Chris Lattnerc2593162001-10-27 08:28:11 +000022#include <math.h>
Chris Lattnerc063d382001-11-06 21:52:18 +000023#include <stdio.h>
Chris Lattner697954c2002-01-20 22:54:45 +000024using std::vector;
25using std::cout;
Chris Lattner7720c8e2001-09-10 04:50:17 +000026
Chris Lattnerb408b122002-03-29 03:57:15 +000027typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
28static std::map<const Function *, ExFunc> Functions;
Chris Lattner697954c2002-01-20 22:54:45 +000029static std::map<std::string, ExFunc> FuncNames;
Chris Lattner7720c8e2001-09-10 04:50:17 +000030
Chris Lattnere43db882001-10-27 04:15:57 +000031static Interpreter *TheInterpreter;
32
33// getCurrentExecutablePath() - Return the directory that the lli executable
34// lives in.
35//
Chris Lattner697954c2002-01-20 22:54:45 +000036std::string Interpreter::getCurrentExecutablePath() const {
Chris Lattnere43db882001-10-27 04:15:57 +000037 Dl_info Info;
38 if (dladdr(&TheInterpreter, &Info) == 0) return "";
39
Chris Lattner697954c2002-01-20 22:54:45 +000040 std::string LinkAddr(Info.dli_fname);
Chris Lattnere43db882001-10-27 04:15:57 +000041 unsigned SlashPos = LinkAddr.rfind('/');
Chris Lattner697954c2002-01-20 22:54:45 +000042 if (SlashPos != std::string::npos)
Chris Lattnere43db882001-10-27 04:15:57 +000043 LinkAddr.resize(SlashPos); // Trim the executable name off...
44
45 return LinkAddr;
46}
47
48
Chris Lattner7720c8e2001-09-10 04:50:17 +000049static char getTypeID(const Type *Ty) {
50 switch (Ty->getPrimitiveID()) {
51 case Type::VoidTyID: return 'V';
52 case Type::BoolTyID: return 'o';
53 case Type::UByteTyID: return 'B';
54 case Type::SByteTyID: return 'b';
55 case Type::UShortTyID: return 'S';
56 case Type::ShortTyID: return 's';
57 case Type::UIntTyID: return 'I';
58 case Type::IntTyID: return 'i';
59 case Type::ULongTyID: return 'L';
60 case Type::LongTyID: return 'l';
61 case Type::FloatTyID: return 'F';
62 case Type::DoubleTyID: return 'D';
63 case Type::PointerTyID: return 'P';
Chris Lattnerb408b122002-03-29 03:57:15 +000064 case Type::FunctionTyID: return 'M';
Chris Lattner7720c8e2001-09-10 04:50:17 +000065 case Type::StructTyID: return 'T';
66 case Type::ArrayTyID: return 'A';
67 case Type::OpaqueTyID: return 'O';
68 default: return 'U';
69 }
70}
71
Chris Lattnerb408b122002-03-29 03:57:15 +000072static ExFunc lookupFunction(const Function *M) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000073 // Function not found, look it up... start by figuring out what the
74 // composite function name should be.
Chris Lattner697954c2002-01-20 22:54:45 +000075 std::string ExtName = "lle_";
Chris Lattnerb408b122002-03-29 03:57:15 +000076 const FunctionType *MT = M->getFunctionType();
Chris Lattner7720c8e2001-09-10 04:50:17 +000077 for (unsigned i = 0; const Type *Ty = MT->getContainedType(i); ++i)
78 ExtName += getTypeID(Ty);
79 ExtName += "_" + M->getName();
80
81 //cout << "Tried: '" << ExtName << "'\n";
Chris Lattner4721f132001-10-30 20:28:00 +000082 ExFunc FnPtr = FuncNames[ExtName];
83 if (FnPtr == 0)
84 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ExtName.c_str());
85 if (FnPtr == 0)
86 FnPtr = FuncNames["lle_X_"+M->getName()];
Chris Lattner7720c8e2001-09-10 04:50:17 +000087 if (FnPtr == 0) // Try calling a generic function... if it exists...
88 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ("lle_X_"+M->getName()).c_str());
89 if (FnPtr != 0)
Chris Lattner697954c2002-01-20 22:54:45 +000090 Functions.insert(std::make_pair(M, FnPtr)); // Cache for later
Chris Lattner7720c8e2001-09-10 04:50:17 +000091 return FnPtr;
92}
93
Chris Lattnerb408b122002-03-29 03:57:15 +000094GenericValue Interpreter::callExternalMethod(Function *M,
Chris Lattner4721f132001-10-30 20:28:00 +000095 const vector<GenericValue> &ArgVals) {
Chris Lattnere43db882001-10-27 04:15:57 +000096 TheInterpreter = this;
97
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000098 // Do a lookup to see if the function is in our cache... this should just be a
Chris Lattner7720c8e2001-09-10 04:50:17 +000099 // defered annotation!
Chris Lattnerb408b122002-03-29 03:57:15 +0000100 std::map<const Function *, ExFunc>::iterator FI = Functions.find(M);
101 ExFunc Fn = (FI == Functions.end()) ? lookupFunction(M) : FI->second;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000102 if (Fn == 0) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000103 cout << "Tried to execute an unknown external function: "
Chris Lattner697954c2002-01-20 22:54:45 +0000104 << M->getType()->getDescription() << " " << M->getName() << "\n";
Chris Lattner4721f132001-10-30 20:28:00 +0000105 return GenericValue();
Chris Lattner7720c8e2001-09-10 04:50:17 +0000106 }
107
108 // TODO: FIXME when types are not const!
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000109 GenericValue Result = Fn(const_cast<FunctionType*>(M->getFunctionType()),
110 ArgVals);
Chris Lattner4721f132001-10-30 20:28:00 +0000111 return Result;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000112}
113
114
115//===----------------------------------------------------------------------===//
Chris Lattnerb408b122002-03-29 03:57:15 +0000116// Functions "exported" to the running application...
Chris Lattner7720c8e2001-09-10 04:50:17 +0000117//
118extern "C" { // Don't add C++ manglings to llvm mangling :)
119
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000120// Implement void printstr([ubyte {x N}] *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000121GenericValue lle_VP_printstr(FunctionType *M, const vector<GenericValue> &ArgVal){
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000122 assert(ArgVal.size() == 1 && "printstr only takes one argument!");
123 cout << (char*)ArgVal[0].PointerVal;
124 return GenericValue();
125}
126
Chris Lattner7720c8e2001-09-10 04:50:17 +0000127// Implement 'void print(X)' for every type...
Chris Lattnerb408b122002-03-29 03:57:15 +0000128GenericValue lle_X_print(FunctionType *M, const vector<GenericValue> &ArgVals) {
Chris Lattner7720c8e2001-09-10 04:50:17 +0000129 assert(ArgVals.size() == 1 && "generic print only takes one argument!");
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000130
131 Interpreter::print(M->getParamTypes()[0], ArgVals[0]);
132 return GenericValue();
133}
134
135// Implement 'void printVal(X)' for every type...
Chris Lattnerb408b122002-03-29 03:57:15 +0000136GenericValue lle_X_printVal(FunctionType *M, const vector<GenericValue> &ArgVal) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000137 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
138
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000139 // Specialize print([ubyte {x N} ] *) and print(sbyte *)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000140 if (const PointerType *PTy =
141 dyn_cast<PointerType>(M->getParamTypes()[0].get()))
Chris Lattner7a176752001-12-04 00:03:30 +0000142 if (PTy->getElementType() == Type::SByteTy ||
143 isa<ArrayType>(PTy->getElementType())) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000144 return lle_VP_printstr(M, ArgVal);
145 }
146
147 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);
Chris Lattner7720c8e2001-09-10 04:50:17 +0000148 return GenericValue();
149}
150
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000151// Implement 'void printString(X)'
152// Argument must be [ubyte {x N} ] * or sbyte *
Chris Lattnerb408b122002-03-29 03:57:15 +0000153GenericValue lle_X_printString(FunctionType *M, const vector<GenericValue> &ArgVal) {
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000154 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
155 return lle_VP_printstr(M, ArgVal);
156}
157
158// Implement 'void print<TYPE>(X)' for each primitive type or pointer type
159#define PRINT_TYPE_FUNC(TYPENAME,TYPEID) \
Chris Lattnerb408b122002-03-29 03:57:15 +0000160 GenericValue lle_X_print##TYPENAME(FunctionType *M,\
Chris Lattner316a65b2001-10-28 22:38:22 +0000161 const vector<GenericValue> &ArgVal) {\
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000162 assert(ArgVal.size() == 1 && "generic print only takes one argument!");\
Chris Lattner69b5ce92001-11-26 19:19:27 +0000163 assert(M->getParamTypes()[0].get()->getPrimitiveID() == Type::TYPEID);\
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000164 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);\
165 return GenericValue();\
166 }
167
Chris Lattner4721f132001-10-30 20:28:00 +0000168PRINT_TYPE_FUNC(SByte, SByteTyID)
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000169PRINT_TYPE_FUNC(UByte, UByteTyID)
170PRINT_TYPE_FUNC(Short, ShortTyID)
171PRINT_TYPE_FUNC(UShort, UShortTyID)
172PRINT_TYPE_FUNC(Int, IntTyID)
173PRINT_TYPE_FUNC(UInt, UIntTyID)
174PRINT_TYPE_FUNC(Long, LongTyID)
175PRINT_TYPE_FUNC(ULong, ULongTyID)
176PRINT_TYPE_FUNC(Float, FloatTyID)
177PRINT_TYPE_FUNC(Double, DoubleTyID)
178PRINT_TYPE_FUNC(Pointer, PointerTyID)
179
180
Chris Lattner005cbce2002-10-02 21:12:13 +0000181// void putchar(sbyte)
Chris Lattnerb408b122002-03-29 03:57:15 +0000182GenericValue lle_Vb_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner7720c8e2001-09-10 04:50:17 +0000183 cout << Args[0].SByteVal;
184 return GenericValue();
185}
186
Chris Lattner005cbce2002-10-02 21:12:13 +0000187// int putchar(int)
Chris Lattnerb408b122002-03-29 03:57:15 +0000188GenericValue lle_ii_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner697954c2002-01-20 22:54:45 +0000189 cout << ((char)Args[0].IntVal) << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000190 return Args[0];
191}
192
Chris Lattner005cbce2002-10-02 21:12:13 +0000193// void putchar(ubyte)
Chris Lattnerb408b122002-03-29 03:57:15 +0000194GenericValue lle_VB_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner697954c2002-01-20 22:54:45 +0000195 cout << Args[0].SByteVal << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000196 return Args[0];
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000197}
198
Chris Lattner005cbce2002-10-02 21:12:13 +0000199// void __main()
Chris Lattnerb408b122002-03-29 03:57:15 +0000200GenericValue lle_V___main(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000201 return GenericValue();
202}
203
Chris Lattner005cbce2002-10-02 21:12:13 +0000204// void exit(int)
Chris Lattnerb408b122002-03-29 03:57:15 +0000205GenericValue lle_X_exit(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere43db882001-10-27 04:15:57 +0000206 TheInterpreter->exitCalled(Args[0]);
207 return GenericValue();
208}
209
Chris Lattner005cbce2002-10-02 21:12:13 +0000210// void abort(void)
Chris Lattner1ee34a52002-05-20 21:17:16 +0000211GenericValue lle_X_abort(FunctionType *M, const vector<GenericValue> &Args) {
212 std::cerr << "***PROGRAM ABORTED***!\n";
213 GenericValue GV;
214 GV.IntVal = 1;
215 TheInterpreter->exitCalled(GV);
216 return GenericValue();
217}
218
Chris Lattnerc2593162001-10-27 08:28:11 +0000219// void *malloc(uint)
Chris Lattnerb408b122002-03-29 03:57:15 +0000220GenericValue lle_X_malloc(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner4721f132001-10-30 20:28:00 +0000221 assert(Args.size() == 1 && "Malloc expects one argument!");
Chris Lattnerc2593162001-10-27 08:28:11 +0000222 GenericValue GV;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000223 GV.PointerVal = (PointerTy)malloc(Args[0].UIntVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000224 return GV;
225}
226
227// void free(void *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000228GenericValue lle_X_free(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000229 assert(Args.size() == 1);
Chris Lattner4721f132001-10-30 20:28:00 +0000230 free((void*)Args[0].PointerVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000231 return GenericValue();
232}
233
Chris Lattner782b9392001-11-26 18:18:18 +0000234// int atoi(char *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000235GenericValue lle_X_atoi(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000236 assert(Args.size() == 1);
237 GenericValue GV;
238 GV.IntVal = atoi((char*)Args[0].PointerVal);
239 return GV;
240}
241
Chris Lattnerc2593162001-10-27 08:28:11 +0000242// double pow(double, double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000243GenericValue lle_X_pow(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000244 assert(Args.size() == 2);
Chris Lattnerc2593162001-10-27 08:28:11 +0000245 GenericValue GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000246 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000247 return GV;
248}
249
Chris Lattner34dd24b2002-02-18 19:06:25 +0000250// double exp(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000251GenericValue lle_X_exp(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner34dd24b2002-02-18 19:06:25 +0000252 assert(Args.size() == 1);
253 GenericValue GV;
254 GV.DoubleVal = exp(Args[0].DoubleVal);
255 return GV;
256}
257
Chris Lattnerc063d382001-11-06 21:52:18 +0000258// double sqrt(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000259GenericValue lle_X_sqrt(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000260 assert(Args.size() == 1);
261 GenericValue GV;
262 GV.DoubleVal = sqrt(Args[0].DoubleVal);
263 return GV;
264}
Chris Lattnerc2593162001-10-27 08:28:11 +0000265
Chris Lattner86790052001-11-06 22:53:25 +0000266// double log(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000267GenericValue lle_X_log(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000268 assert(Args.size() == 1);
269 GenericValue GV;
270 GV.DoubleVal = log(Args[0].DoubleVal);
271 return GV;
272}
273
Chris Lattnerdbcda222002-12-20 04:18:13 +0000274// int isnan(double value);
275GenericValue lle_X_isnan(FunctionType *F, const vector<GenericValue> &Args) {
276 assert(Args.size() == 1);
277 GenericValue GV;
278 GV.IntVal = isnan(Args[0].DoubleVal);
279 return GV;
280}
281
Chris Lattner782b9392001-11-26 18:18:18 +0000282// double floor(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000283GenericValue lle_X_floor(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000284 assert(Args.size() == 1);
285 GenericValue GV;
286 GV.DoubleVal = floor(Args[0].DoubleVal);
287 return GV;
288}
289
Chris Lattner86790052001-11-06 22:53:25 +0000290// double drand48()
Chris Lattnerb408b122002-03-29 03:57:15 +0000291GenericValue lle_X_drand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000292 assert(Args.size() == 0);
293 GenericValue GV;
294 GV.DoubleVal = drand48();
295 return GV;
296}
297
Chris Lattner1b600142001-11-13 05:46:08 +0000298// long lrand48()
Chris Lattnerb408b122002-03-29 03:57:15 +0000299GenericValue lle_X_lrand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000300 assert(Args.size() == 0);
301 GenericValue GV;
302 GV.IntVal = lrand48();
303 return GV;
304}
305
306// void srand48(long)
Chris Lattnerb408b122002-03-29 03:57:15 +0000307GenericValue lle_X_srand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000308 assert(Args.size() == 1);
309 srand48(Args[0].IntVal);
310 return GenericValue();
311}
312
Chris Lattner782b9392001-11-26 18:18:18 +0000313// void srand(uint)
Chris Lattnerb408b122002-03-29 03:57:15 +0000314GenericValue lle_X_srand(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000315 assert(Args.size() == 1);
316 srand(Args[0].UIntVal);
317 return GenericValue();
318}
Chris Lattner86790052001-11-06 22:53:25 +0000319
Chris Lattnere7c6f722001-12-13 00:43:47 +0000320// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
321// output useful.
Chris Lattnerb408b122002-03-29 03:57:15 +0000322GenericValue lle_X_sprintf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000323 char *OutputBuffer = (char *)Args[0].PointerVal;
324 const char *FmtStr = (const char *)Args[1].PointerVal;
325 unsigned ArgNo = 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000326
327 // printf should return # chars printed. This is completely incorrect, but
328 // close enough for now.
329 GenericValue GV; GV.IntVal = strlen(FmtStr);
330 while (1) {
331 switch (*FmtStr) {
332 case 0: return GV; // Null terminator...
333 default: // Normal nonspecial character
Chris Lattnere7c6f722001-12-13 00:43:47 +0000334 sprintf(OutputBuffer++, "%c", *FmtStr++);
Chris Lattner08845a22001-10-29 20:27:45 +0000335 break;
336 case '\\': { // Handle escape codes
Chris Lattnere7c6f722001-12-13 00:43:47 +0000337 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
338 FmtStr += 2; OutputBuffer += 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000339 break;
340 }
341 case '%': { // Handle format specifiers
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000342 char FmtBuf[100] = "", Buffer[1000] = "";
343 char *FB = FmtBuf;
344 *FB++ = *FmtStr++;
345 char Last = *FB++ = *FmtStr++;
346 unsigned HowLong = 0;
347 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
348 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
349 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
350 Last != 'p' && Last != 's' && Last != '%') {
351 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
352 Last = *FB++ = *FmtStr++;
Chris Lattner08845a22001-10-29 20:27:45 +0000353 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000354 *FB = 0;
355
356 switch (Last) {
357 case '%':
358 sprintf(Buffer, FmtBuf); break;
359 case 'c':
Chris Lattner2012d5e2002-04-17 17:43:01 +0000360 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000361 case 'd': case 'i':
362 case 'u': case 'o':
363 case 'x': case 'X':
Chris Lattner69ab7a82002-08-02 23:08:32 +0000364 if (HowLong >= 1) {
365 if (HowLong == 1) {
366 // Make sure we use %lld with a 64 bit argument because we might be
367 // compiling LLI on a 32 bit compiler.
368 unsigned Size = strlen(FmtBuf);
369 FmtBuf[Size] = FmtBuf[Size-1];
370 FmtBuf[Size+1] = 0;
371 FmtBuf[Size-1] = 'l';
372 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000373 sprintf(Buffer, FmtBuf, Args[ArgNo++].ULongVal);
Chris Lattner69ab7a82002-08-02 23:08:32 +0000374 } else
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000375 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
376 case 'e': case 'E': case 'g': case 'G': case 'f':
377 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
378 case 'p':
379 sprintf(Buffer, FmtBuf, (void*)Args[ArgNo++].PointerVal); break;
380 case 's':
381 sprintf(Buffer, FmtBuf, (char*)Args[ArgNo++].PointerVal); break;
382 default: cout << "<unknown printf code '" << *FmtStr << "'!>";
383 ArgNo++; break;
Chris Lattner08845a22001-10-29 20:27:45 +0000384 }
Chris Lattnere7c6f722001-12-13 00:43:47 +0000385 strcpy(OutputBuffer, Buffer);
386 OutputBuffer += strlen(Buffer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000387 }
Chris Lattner08845a22001-10-29 20:27:45 +0000388 break;
389 }
Chris Lattner08845a22001-10-29 20:27:45 +0000390 }
391}
392
Chris Lattnere7c6f722001-12-13 00:43:47 +0000393// int printf(sbyte *, ...) - a very rough implementation to make output useful.
Chris Lattnerb408b122002-03-29 03:57:15 +0000394GenericValue lle_X_printf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000395 char Buffer[10000];
396 vector<GenericValue> NewArgs;
397 GenericValue GV; GV.PointerVal = (PointerTy)Buffer;
398 NewArgs.push_back(GV);
399 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
400 GV = lle_X_sprintf(M, NewArgs);
401 cout << Buffer;
402 return GV;
403}
404
Chris Lattner665ee882002-03-08 22:51:07 +0000405// int sscanf(const char *format, ...);
Chris Lattnerb408b122002-03-29 03:57:15 +0000406GenericValue lle_X_sscanf(FunctionType *M, const vector<GenericValue> &args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000407 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
408
409 const char *Args[10];
410 for (unsigned i = 0; i < args.size(); ++i)
411 Args[i] = (const char*)args[i].PointerVal;
412
413 GenericValue GV;
414 GV.IntVal = sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
415 Args[5], Args[6], Args[7], Args[8], Args[9]);
416 return GV;
417}
418
419
Chris Lattner295fe672002-01-23 21:38:07 +0000420// int clock(void) - Profiling implementation
Chris Lattnerb408b122002-03-29 03:57:15 +0000421GenericValue lle_i_clock(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner295fe672002-01-23 21:38:07 +0000422 extern int clock(void);
423 GenericValue GV; GV.IntVal = clock();
424 return GV;
425}
Chris Lattnere7c6f722001-12-13 00:43:47 +0000426
Chris Lattner665ee882002-03-08 22:51:07 +0000427//===----------------------------------------------------------------------===//
428// IO Functions...
429//===----------------------------------------------------------------------===//
430
Chris Lattner005cbce2002-10-02 21:12:13 +0000431// getFILE - Turn a pointer in the host address space into a legit pointer in
432// the interpreter address space. For the most part, this is an identity
433// transformation, but if the program refers to stdio, stderr, stdin then they
434// have pointers that are relative to the __iob array. If this is the case,
435// change the FILE into the REAL stdio stream.
436//
437static FILE *getFILE(PointerTy Ptr) {
438 static Module *LastMod = 0;
439 static PointerTy IOBBase = 0;
440 static unsigned FILESize;
441
Chris Lattnerfe11a972002-12-23 23:59:41 +0000442 if (LastMod != &TheInterpreter->getModule()) { // Module change or initialize?
443 Module *M = LastMod = &TheInterpreter->getModule();
Chris Lattner005cbce2002-10-02 21:12:13 +0000444
445 // Check to see if the currently loaded module contains an __iob symbol...
446 GlobalVariable *IOB = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000447 SymbolTable &ST = M->getSymbolTable();
448 for (SymbolTable::iterator I = ST.begin(), E = ST.end(); I != E; ++I) {
449 SymbolTable::VarMap &M = I->second;
450 for (SymbolTable::VarMap::iterator J = M.begin(), E = M.end();
451 J != E; ++J)
452 if (J->first == "__iob")
453 if ((IOB = dyn_cast<GlobalVariable>(J->second)))
454 break;
455 if (IOB) break;
Chris Lattner005cbce2002-10-02 21:12:13 +0000456 }
457
Chris Lattnerfe11a972002-12-23 23:59:41 +0000458#if 0 /// FIXME! __iob support for LLI
Chris Lattner005cbce2002-10-02 21:12:13 +0000459 // If we found an __iob symbol now, find out what the actual address it's
460 // held in is...
461 if (IOB) {
462 // Get the address the array lives in...
463 GlobalAddress *Address =
464 (GlobalAddress*)IOB->getOrCreateAnnotation(GlobalAddressAID);
465 IOBBase = (PointerTy)(GenericValue*)Address->Ptr;
466
467 // Figure out how big each element of the array is...
468 const ArrayType *AT =
469 dyn_cast<ArrayType>(IOB->getType()->getElementType());
470 if (AT)
471 FILESize = TD.getTypeSize(AT->getElementType());
472 else
473 FILESize = 16*8; // Default size
474 }
Chris Lattnerfe11a972002-12-23 23:59:41 +0000475#endif
Chris Lattner005cbce2002-10-02 21:12:13 +0000476 }
477
478 // Check to see if this is a reference to __iob...
479 if (IOBBase) {
480 unsigned FDNum = (Ptr-IOBBase)/FILESize;
481 if (FDNum == 0)
482 return stdin;
483 else if (FDNum == 1)
484 return stdout;
485 else if (FDNum == 2)
486 return stderr;
487 }
488
489 return (FILE*)Ptr;
490}
491
492
Chris Lattner665ee882002-03-08 22:51:07 +0000493// FILE *fopen(const char *filename, const char *mode);
Chris Lattnerb408b122002-03-29 03:57:15 +0000494GenericValue lle_X_fopen(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000495 assert(Args.size() == 2);
496 GenericValue GV;
497
498 GV.PointerVal = (PointerTy)fopen((const char *)Args[0].PointerVal,
499 (const char *)Args[1].PointerVal);
500 return GV;
501}
502
503// int fclose(FILE *F);
Chris Lattnerb408b122002-03-29 03:57:15 +0000504GenericValue lle_X_fclose(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000505 assert(Args.size() == 1);
506 GenericValue GV;
507
Chris Lattner005cbce2002-10-02 21:12:13 +0000508 GV.IntVal = fclose(getFILE(Args[0].PointerVal));
Chris Lattner665ee882002-03-08 22:51:07 +0000509 return GV;
510}
511
Chris Lattner25f6f372002-11-08 19:10:26 +0000512// int feof(FILE *stream);
513GenericValue lle_X_feof(FunctionType *M, const vector<GenericValue> &Args) {
514 assert(Args.size() == 1);
515 GenericValue GV;
516
517 GV.IntVal = feof(getFILE(Args[0].PointerVal));
518 return GV;
519}
520
Chris Lattner665ee882002-03-08 22:51:07 +0000521// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000522GenericValue lle_X_fread(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000523 assert(Args.size() == 4);
524 GenericValue GV;
525
526 GV.UIntVal = fread((void*)Args[0].PointerVal, Args[1].UIntVal,
Chris Lattner005cbce2002-10-02 21:12:13 +0000527 Args[2].UIntVal, getFILE(Args[3].PointerVal));
Chris Lattner665ee882002-03-08 22:51:07 +0000528 return GV;
529}
530
531// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000532GenericValue lle_X_fwrite(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000533 assert(Args.size() == 4);
534 GenericValue GV;
535
536 GV.UIntVal = fwrite((void*)Args[0].PointerVal, Args[1].UIntVal,
Chris Lattner005cbce2002-10-02 21:12:13 +0000537 Args[2].UIntVal, getFILE(Args[3].PointerVal));
Chris Lattner665ee882002-03-08 22:51:07 +0000538 return GV;
539}
540
541// char *fgets(char *s, int n, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000542GenericValue lle_X_fgets(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000543 assert(Args.size() == 3);
544 GenericValue GV;
545
546 GV.PointerVal = (PointerTy)fgets((char*)Args[0].PointerVal, Args[1].IntVal,
Chris Lattner005cbce2002-10-02 21:12:13 +0000547 getFILE(Args[2].PointerVal));
Chris Lattner665ee882002-03-08 22:51:07 +0000548 return GV;
549}
550
Chris Lattnera4479cd2002-11-07 19:33:50 +0000551// FILE *freopen(const char *path, const char *mode, FILE *stream);
552GenericValue lle_X_freopen(FunctionType *M, const vector<GenericValue> &Args) {
553 assert(Args.size() == 3);
554 GenericValue GV;
555 GV.PointerVal = (PointerTy)freopen((char*)Args[0].PointerVal,
556 (char*)Args[1].PointerVal,
557 getFILE(Args[2].PointerVal));
558 return GV;
559}
560
Chris Lattner665ee882002-03-08 22:51:07 +0000561// int fflush(FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000562GenericValue lle_X_fflush(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000563 assert(Args.size() == 1);
564 GenericValue GV;
Chris Lattner005cbce2002-10-02 21:12:13 +0000565 GV.IntVal = fflush(getFILE(Args[0].PointerVal));
566 return GV;
567}
568
569// int getc(FILE *stream);
570GenericValue lle_X_getc(FunctionType *M, const vector<GenericValue> &Args) {
571 assert(Args.size() == 1);
572 GenericValue GV;
Chris Lattner005cbce2002-10-02 21:12:13 +0000573 GV.IntVal = getc(getFILE(Args[0].PointerVal));
Chris Lattner665ee882002-03-08 22:51:07 +0000574 return GV;
575}
576
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000577// int fputc(int C, FILE *stream);
578GenericValue lle_X_fputc(FunctionType *M, const vector<GenericValue> &Args) {
579 assert(Args.size() == 2);
580 GenericValue GV;
581 GV.IntVal = fputc(Args[0].IntVal, getFILE(Args[1].PointerVal));
582 return GV;
583}
584
585// int ungetc(int C, FILE *stream);
586GenericValue lle_X_ungetc(FunctionType *M, const vector<GenericValue> &Args) {
587 assert(Args.size() == 2);
588 GenericValue GV;
589 GV.IntVal = ungetc(Args[0].IntVal, getFILE(Args[1].PointerVal));
590 return GV;
591}
592
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000593// int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
594// useful.
595GenericValue lle_X_fprintf(FunctionType *M, const vector<GenericValue> &Args) {
596 assert(Args.size() > 2);
597 char Buffer[10000];
598 vector<GenericValue> NewArgs;
599 GenericValue GV; GV.PointerVal = (PointerTy)Buffer;
600 NewArgs.push_back(GV);
601 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
602 GV = lle_X_sprintf(M, NewArgs);
603
604 fputs(Buffer, getFILE(Args[0].PointerVal));
605 return GV;
606}
607
Chris Lattner7720c8e2001-09-10 04:50:17 +0000608} // End extern "C"
Chris Lattner4721f132001-10-30 20:28:00 +0000609
610
611void Interpreter::initializeExternalMethods() {
612 FuncNames["lle_VP_printstr"] = lle_VP_printstr;
613 FuncNames["lle_X_print"] = lle_X_print;
614 FuncNames["lle_X_printVal"] = lle_X_printVal;
615 FuncNames["lle_X_printString"] = lle_X_printString;
616 FuncNames["lle_X_printUByte"] = lle_X_printUByte;
617 FuncNames["lle_X_printSByte"] = lle_X_printSByte;
618 FuncNames["lle_X_printUShort"] = lle_X_printUShort;
619 FuncNames["lle_X_printShort"] = lle_X_printShort;
620 FuncNames["lle_X_printInt"] = lle_X_printInt;
621 FuncNames["lle_X_printUInt"] = lle_X_printUInt;
622 FuncNames["lle_X_printLong"] = lle_X_printLong;
623 FuncNames["lle_X_printULong"] = lle_X_printULong;
624 FuncNames["lle_X_printFloat"] = lle_X_printFloat;
625 FuncNames["lle_X_printDouble"] = lle_X_printDouble;
626 FuncNames["lle_X_printPointer"] = lle_X_printPointer;
Chris Lattner0f279b22001-11-03 10:15:32 +0000627 FuncNames["lle_Vb_putchar"] = lle_Vb_putchar;
628 FuncNames["lle_ii_putchar"] = lle_ii_putchar;
629 FuncNames["lle_VB_putchar"] = lle_VB_putchar;
630 FuncNames["lle_V___main"] = lle_V___main;
631 FuncNames["lle_X_exit"] = lle_X_exit;
Chris Lattner1ee34a52002-05-20 21:17:16 +0000632 FuncNames["lle_X_abort"] = lle_X_abort;
Chris Lattner0f279b22001-11-03 10:15:32 +0000633 FuncNames["lle_X_malloc"] = lle_X_malloc;
634 FuncNames["lle_X_free"] = lle_X_free;
Chris Lattner782b9392001-11-26 18:18:18 +0000635 FuncNames["lle_X_atoi"] = lle_X_atoi;
Chris Lattner0f279b22001-11-03 10:15:32 +0000636 FuncNames["lle_X_pow"] = lle_X_pow;
Chris Lattner34dd24b2002-02-18 19:06:25 +0000637 FuncNames["lle_X_exp"] = lle_X_exp;
Chris Lattner1b600142001-11-13 05:46:08 +0000638 FuncNames["lle_X_log"] = lle_X_log;
Chris Lattnerdbcda222002-12-20 04:18:13 +0000639 FuncNames["lle_X_isnan"] = lle_X_isnan;
Chris Lattner782b9392001-11-26 18:18:18 +0000640 FuncNames["lle_X_floor"] = lle_X_floor;
641 FuncNames["lle_X_srand"] = lle_X_srand;
Chris Lattner1b600142001-11-13 05:46:08 +0000642 FuncNames["lle_X_drand48"] = lle_X_drand48;
643 FuncNames["lle_X_srand48"] = lle_X_srand48;
644 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
Chris Lattnerc063d382001-11-06 21:52:18 +0000645 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
Chris Lattner0f279b22001-11-03 10:15:32 +0000646 FuncNames["lle_X_printf"] = lle_X_printf;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000647 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
Chris Lattner665ee882002-03-08 22:51:07 +0000648 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
Chris Lattner295fe672002-01-23 21:38:07 +0000649 FuncNames["lle_i_clock"] = lle_i_clock;
Chris Lattner665ee882002-03-08 22:51:07 +0000650 FuncNames["lle_X_fopen"] = lle_X_fopen;
651 FuncNames["lle_X_fclose"] = lle_X_fclose;
Chris Lattner25f6f372002-11-08 19:10:26 +0000652 FuncNames["lle_X_feof"] = lle_X_feof;
Chris Lattner665ee882002-03-08 22:51:07 +0000653 FuncNames["lle_X_fread"] = lle_X_fread;
654 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
655 FuncNames["lle_X_fgets"] = lle_X_fgets;
656 FuncNames["lle_X_fflush"] = lle_X_fflush;
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000657 FuncNames["lle_X_fgetc"] = lle_X_getc;
Chris Lattner005cbce2002-10-02 21:12:13 +0000658 FuncNames["lle_X_getc"] = lle_X_getc;
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000659 FuncNames["lle_X_fputc"] = lle_X_fputc;
660 FuncNames["lle_X_ungetc"] = lle_X_ungetc;
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000661 FuncNames["lle_X_fprintf"] = lle_X_fprintf;
Chris Lattnera4479cd2002-11-07 19:33:50 +0000662 FuncNames["lle_X_freopen"] = lle_X_freopen;
Chris Lattner4721f132001-10-30 20:28:00 +0000663}