blob: 8f486e84cebcbfdb447a7851432d7d6e834805d2 [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"
14#include "llvm/DerivedTypes.h"
15#include <map>
16#include <dlfcn.h>
Chris Lattner697954c2002-01-20 22:54:45 +000017#include <iostream>
Chris Lattner7720c8e2001-09-10 04:50:17 +000018#include <link.h>
Chris Lattnerc2593162001-10-27 08:28:11 +000019#include <math.h>
Chris Lattnerc063d382001-11-06 21:52:18 +000020#include <stdio.h>
Chris Lattner697954c2002-01-20 22:54:45 +000021using std::vector;
22using std::cout;
Chris Lattner7720c8e2001-09-10 04:50:17 +000023
Chris Lattnerb408b122002-03-29 03:57:15 +000024typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
25static std::map<const Function *, ExFunc> Functions;
Chris Lattner697954c2002-01-20 22:54:45 +000026static std::map<std::string, ExFunc> FuncNames;
Chris Lattner7720c8e2001-09-10 04:50:17 +000027
Chris Lattnere43db882001-10-27 04:15:57 +000028static Interpreter *TheInterpreter;
29
30// getCurrentExecutablePath() - Return the directory that the lli executable
31// lives in.
32//
Chris Lattner697954c2002-01-20 22:54:45 +000033std::string Interpreter::getCurrentExecutablePath() const {
Chris Lattnere43db882001-10-27 04:15:57 +000034 Dl_info Info;
35 if (dladdr(&TheInterpreter, &Info) == 0) return "";
36
Chris Lattner697954c2002-01-20 22:54:45 +000037 std::string LinkAddr(Info.dli_fname);
Chris Lattnere43db882001-10-27 04:15:57 +000038 unsigned SlashPos = LinkAddr.rfind('/');
Chris Lattner697954c2002-01-20 22:54:45 +000039 if (SlashPos != std::string::npos)
Chris Lattnere43db882001-10-27 04:15:57 +000040 LinkAddr.resize(SlashPos); // Trim the executable name off...
41
42 return LinkAddr;
43}
44
45
Chris Lattner7720c8e2001-09-10 04:50:17 +000046static char getTypeID(const Type *Ty) {
47 switch (Ty->getPrimitiveID()) {
48 case Type::VoidTyID: return 'V';
49 case Type::BoolTyID: return 'o';
50 case Type::UByteTyID: return 'B';
51 case Type::SByteTyID: return 'b';
52 case Type::UShortTyID: return 'S';
53 case Type::ShortTyID: return 's';
54 case Type::UIntTyID: return 'I';
55 case Type::IntTyID: return 'i';
56 case Type::ULongTyID: return 'L';
57 case Type::LongTyID: return 'l';
58 case Type::FloatTyID: return 'F';
59 case Type::DoubleTyID: return 'D';
60 case Type::PointerTyID: return 'P';
Chris Lattnerb408b122002-03-29 03:57:15 +000061 case Type::FunctionTyID: return 'M';
Chris Lattner7720c8e2001-09-10 04:50:17 +000062 case Type::StructTyID: return 'T';
63 case Type::ArrayTyID: return 'A';
64 case Type::OpaqueTyID: return 'O';
65 default: return 'U';
66 }
67}
68
Chris Lattnerb408b122002-03-29 03:57:15 +000069static ExFunc lookupFunction(const Function *M) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000070 // Function not found, look it up... start by figuring out what the
71 // composite function name should be.
Chris Lattner697954c2002-01-20 22:54:45 +000072 std::string ExtName = "lle_";
Chris Lattnerb408b122002-03-29 03:57:15 +000073 const FunctionType *MT = M->getFunctionType();
Chris Lattner7720c8e2001-09-10 04:50:17 +000074 for (unsigned i = 0; const Type *Ty = MT->getContainedType(i); ++i)
75 ExtName += getTypeID(Ty);
76 ExtName += "_" + M->getName();
77
78 //cout << "Tried: '" << ExtName << "'\n";
Chris Lattner4721f132001-10-30 20:28:00 +000079 ExFunc FnPtr = FuncNames[ExtName];
80 if (FnPtr == 0)
81 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ExtName.c_str());
82 if (FnPtr == 0)
83 FnPtr = FuncNames["lle_X_"+M->getName()];
Chris Lattner7720c8e2001-09-10 04:50:17 +000084 if (FnPtr == 0) // Try calling a generic function... if it exists...
85 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ("lle_X_"+M->getName()).c_str());
86 if (FnPtr != 0)
Chris Lattner697954c2002-01-20 22:54:45 +000087 Functions.insert(std::make_pair(M, FnPtr)); // Cache for later
Chris Lattner7720c8e2001-09-10 04:50:17 +000088 return FnPtr;
89}
90
Chris Lattnerb408b122002-03-29 03:57:15 +000091GenericValue Interpreter::callExternalMethod(Function *M,
Chris Lattner4721f132001-10-30 20:28:00 +000092 const vector<GenericValue> &ArgVals) {
Chris Lattnere43db882001-10-27 04:15:57 +000093 TheInterpreter = this;
94
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000095 // Do a lookup to see if the function is in our cache... this should just be a
Chris Lattner7720c8e2001-09-10 04:50:17 +000096 // defered annotation!
Chris Lattnerb408b122002-03-29 03:57:15 +000097 std::map<const Function *, ExFunc>::iterator FI = Functions.find(M);
98 ExFunc Fn = (FI == Functions.end()) ? lookupFunction(M) : FI->second;
Chris Lattner7720c8e2001-09-10 04:50:17 +000099 if (Fn == 0) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000100 cout << "Tried to execute an unknown external function: "
Chris Lattner697954c2002-01-20 22:54:45 +0000101 << M->getType()->getDescription() << " " << M->getName() << "\n";
Chris Lattner4721f132001-10-30 20:28:00 +0000102 return GenericValue();
Chris Lattner7720c8e2001-09-10 04:50:17 +0000103 }
104
105 // TODO: FIXME when types are not const!
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000106 GenericValue Result = Fn(const_cast<FunctionType*>(M->getFunctionType()),
107 ArgVals);
Chris Lattner4721f132001-10-30 20:28:00 +0000108 return Result;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000109}
110
111
112//===----------------------------------------------------------------------===//
Chris Lattnerb408b122002-03-29 03:57:15 +0000113// Functions "exported" to the running application...
Chris Lattner7720c8e2001-09-10 04:50:17 +0000114//
115extern "C" { // Don't add C++ manglings to llvm mangling :)
116
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000117// Implement void printstr([ubyte {x N}] *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000118GenericValue lle_VP_printstr(FunctionType *M, const vector<GenericValue> &ArgVal){
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000119 assert(ArgVal.size() == 1 && "printstr only takes one argument!");
120 cout << (char*)ArgVal[0].PointerVal;
121 return GenericValue();
122}
123
Chris Lattner7720c8e2001-09-10 04:50:17 +0000124// Implement 'void print(X)' for every type...
Chris Lattnerb408b122002-03-29 03:57:15 +0000125GenericValue lle_X_print(FunctionType *M, const vector<GenericValue> &ArgVals) {
Chris Lattner7720c8e2001-09-10 04:50:17 +0000126 assert(ArgVals.size() == 1 && "generic print only takes one argument!");
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000127
128 Interpreter::print(M->getParamTypes()[0], ArgVals[0]);
129 return GenericValue();
130}
131
132// Implement 'void printVal(X)' for every type...
Chris Lattnerb408b122002-03-29 03:57:15 +0000133GenericValue lle_X_printVal(FunctionType *M, const vector<GenericValue> &ArgVal) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000134 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
135
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000136 // Specialize print([ubyte {x N} ] *) and print(sbyte *)
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000137 if (PointerType *PTy = dyn_cast<PointerType>(M->getParamTypes()[0].get()))
Chris Lattner7a176752001-12-04 00:03:30 +0000138 if (PTy->getElementType() == Type::SByteTy ||
139 isa<ArrayType>(PTy->getElementType())) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000140 return lle_VP_printstr(M, ArgVal);
141 }
142
143 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);
Chris Lattner7720c8e2001-09-10 04:50:17 +0000144 return GenericValue();
145}
146
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000147// Implement 'void printString(X)'
148// Argument must be [ubyte {x N} ] * or sbyte *
Chris Lattnerb408b122002-03-29 03:57:15 +0000149GenericValue lle_X_printString(FunctionType *M, const vector<GenericValue> &ArgVal) {
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000150 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
151 return lle_VP_printstr(M, ArgVal);
152}
153
154// Implement 'void print<TYPE>(X)' for each primitive type or pointer type
155#define PRINT_TYPE_FUNC(TYPENAME,TYPEID) \
Chris Lattnerb408b122002-03-29 03:57:15 +0000156 GenericValue lle_X_print##TYPENAME(FunctionType *M,\
Chris Lattner316a65b2001-10-28 22:38:22 +0000157 const vector<GenericValue> &ArgVal) {\
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000158 assert(ArgVal.size() == 1 && "generic print only takes one argument!");\
Chris Lattner69b5ce92001-11-26 19:19:27 +0000159 assert(M->getParamTypes()[0].get()->getPrimitiveID() == Type::TYPEID);\
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000160 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);\
161 return GenericValue();\
162 }
163
Chris Lattner4721f132001-10-30 20:28:00 +0000164PRINT_TYPE_FUNC(SByte, SByteTyID)
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000165PRINT_TYPE_FUNC(UByte, UByteTyID)
166PRINT_TYPE_FUNC(Short, ShortTyID)
167PRINT_TYPE_FUNC(UShort, UShortTyID)
168PRINT_TYPE_FUNC(Int, IntTyID)
169PRINT_TYPE_FUNC(UInt, UIntTyID)
170PRINT_TYPE_FUNC(Long, LongTyID)
171PRINT_TYPE_FUNC(ULong, ULongTyID)
172PRINT_TYPE_FUNC(Float, FloatTyID)
173PRINT_TYPE_FUNC(Double, DoubleTyID)
174PRINT_TYPE_FUNC(Pointer, PointerTyID)
175
176
Chris Lattner7720c8e2001-09-10 04:50:17 +0000177// void "putchar"(sbyte)
Chris Lattnerb408b122002-03-29 03:57:15 +0000178GenericValue lle_Vb_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner7720c8e2001-09-10 04:50:17 +0000179 cout << Args[0].SByteVal;
180 return GenericValue();
181}
182
Chris Lattnere43db882001-10-27 04:15:57 +0000183// int "putchar"(int)
Chris Lattnerb408b122002-03-29 03:57:15 +0000184GenericValue lle_ii_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner697954c2002-01-20 22:54:45 +0000185 cout << ((char)Args[0].IntVal) << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000186 return Args[0];
187}
188
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000189// void "putchar"(ubyte)
Chris Lattnerb408b122002-03-29 03:57:15 +0000190GenericValue lle_VB_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner697954c2002-01-20 22:54:45 +0000191 cout << Args[0].SByteVal << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000192 return Args[0];
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000193}
194
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000195// void "__main"()
Chris Lattnerb408b122002-03-29 03:57:15 +0000196GenericValue lle_V___main(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000197 return GenericValue();
198}
199
Chris Lattnere43db882001-10-27 04:15:57 +0000200// void "exit"(int)
Chris Lattnerb408b122002-03-29 03:57:15 +0000201GenericValue lle_X_exit(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere43db882001-10-27 04:15:57 +0000202 TheInterpreter->exitCalled(Args[0]);
203 return GenericValue();
204}
205
Chris Lattnerc2593162001-10-27 08:28:11 +0000206// void *malloc(uint)
Chris Lattnerb408b122002-03-29 03:57:15 +0000207GenericValue lle_X_malloc(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner4721f132001-10-30 20:28:00 +0000208 assert(Args.size() == 1 && "Malloc expects one argument!");
Chris Lattnerc2593162001-10-27 08:28:11 +0000209 GenericValue GV;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000210 GV.PointerVal = (PointerTy)malloc(Args[0].UIntVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000211 return GV;
212}
213
214// void free(void *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000215GenericValue lle_X_free(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000216 assert(Args.size() == 1);
Chris Lattner4721f132001-10-30 20:28:00 +0000217 free((void*)Args[0].PointerVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000218 return GenericValue();
219}
220
Chris Lattner782b9392001-11-26 18:18:18 +0000221// int atoi(char *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000222GenericValue lle_X_atoi(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000223 assert(Args.size() == 1);
224 GenericValue GV;
225 GV.IntVal = atoi((char*)Args[0].PointerVal);
226 return GV;
227}
228
Chris Lattnerc2593162001-10-27 08:28:11 +0000229// double pow(double, double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000230GenericValue lle_X_pow(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000231 assert(Args.size() == 2);
Chris Lattnerc2593162001-10-27 08:28:11 +0000232 GenericValue GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000233 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000234 return GV;
235}
236
Chris Lattner34dd24b2002-02-18 19:06:25 +0000237// double exp(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000238GenericValue lle_X_exp(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner34dd24b2002-02-18 19:06:25 +0000239 assert(Args.size() == 1);
240 GenericValue GV;
241 GV.DoubleVal = exp(Args[0].DoubleVal);
242 return GV;
243}
244
Chris Lattnerc063d382001-11-06 21:52:18 +0000245// double sqrt(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000246GenericValue lle_X_sqrt(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000247 assert(Args.size() == 1);
248 GenericValue GV;
249 GV.DoubleVal = sqrt(Args[0].DoubleVal);
250 return GV;
251}
Chris Lattnerc2593162001-10-27 08:28:11 +0000252
Chris Lattner86790052001-11-06 22:53:25 +0000253// double log(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000254GenericValue lle_X_log(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000255 assert(Args.size() == 1);
256 GenericValue GV;
257 GV.DoubleVal = log(Args[0].DoubleVal);
258 return GV;
259}
260
Chris Lattner782b9392001-11-26 18:18:18 +0000261// double floor(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000262GenericValue lle_X_floor(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000263 assert(Args.size() == 1);
264 GenericValue GV;
265 GV.DoubleVal = floor(Args[0].DoubleVal);
266 return GV;
267}
268
Chris Lattner86790052001-11-06 22:53:25 +0000269// double drand48()
Chris Lattnerb408b122002-03-29 03:57:15 +0000270GenericValue lle_X_drand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000271 assert(Args.size() == 0);
272 GenericValue GV;
273 GV.DoubleVal = drand48();
274 return GV;
275}
276
Chris Lattner1b600142001-11-13 05:46:08 +0000277// long lrand48()
Chris Lattnerb408b122002-03-29 03:57:15 +0000278GenericValue lle_X_lrand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000279 assert(Args.size() == 0);
280 GenericValue GV;
281 GV.IntVal = lrand48();
282 return GV;
283}
284
285// void srand48(long)
Chris Lattnerb408b122002-03-29 03:57:15 +0000286GenericValue lle_X_srand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000287 assert(Args.size() == 1);
288 srand48(Args[0].IntVal);
289 return GenericValue();
290}
291
Chris Lattner782b9392001-11-26 18:18:18 +0000292// void srand(uint)
Chris Lattnerb408b122002-03-29 03:57:15 +0000293GenericValue lle_X_srand(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000294 assert(Args.size() == 1);
295 srand(Args[0].UIntVal);
296 return GenericValue();
297}
Chris Lattner86790052001-11-06 22:53:25 +0000298
Chris Lattnere7c6f722001-12-13 00:43:47 +0000299// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
300// output useful.
Chris Lattnerb408b122002-03-29 03:57:15 +0000301GenericValue lle_X_sprintf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000302 char *OutputBuffer = (char *)Args[0].PointerVal;
303 const char *FmtStr = (const char *)Args[1].PointerVal;
304 unsigned ArgNo = 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000305
306 // printf should return # chars printed. This is completely incorrect, but
307 // close enough for now.
308 GenericValue GV; GV.IntVal = strlen(FmtStr);
309 while (1) {
310 switch (*FmtStr) {
311 case 0: return GV; // Null terminator...
312 default: // Normal nonspecial character
Chris Lattnere7c6f722001-12-13 00:43:47 +0000313 sprintf(OutputBuffer++, "%c", *FmtStr++);
Chris Lattner08845a22001-10-29 20:27:45 +0000314 break;
315 case '\\': { // Handle escape codes
Chris Lattnere7c6f722001-12-13 00:43:47 +0000316 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
317 FmtStr += 2; OutputBuffer += 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000318 break;
319 }
320 case '%': { // Handle format specifiers
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000321 char FmtBuf[100] = "", Buffer[1000] = "";
322 char *FB = FmtBuf;
323 *FB++ = *FmtStr++;
324 char Last = *FB++ = *FmtStr++;
325 unsigned HowLong = 0;
326 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
327 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
328 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
329 Last != 'p' && Last != 's' && Last != '%') {
330 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
331 Last = *FB++ = *FmtStr++;
Chris Lattner08845a22001-10-29 20:27:45 +0000332 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000333 *FB = 0;
334
335 switch (Last) {
336 case '%':
337 sprintf(Buffer, FmtBuf); break;
338 case 'c':
339 sprintf(Buffer, FmtBuf, Args[ArgNo++].SByteVal); break;
340 case 'd': case 'i':
341 case 'u': case 'o':
342 case 'x': case 'X':
343 if (HowLong == 2)
344 sprintf(Buffer, FmtBuf, Args[ArgNo++].ULongVal);
345 else
346 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
347 case 'e': case 'E': case 'g': case 'G': case 'f':
348 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
349 case 'p':
350 sprintf(Buffer, FmtBuf, (void*)Args[ArgNo++].PointerVal); break;
351 case 's':
352 sprintf(Buffer, FmtBuf, (char*)Args[ArgNo++].PointerVal); break;
353 default: cout << "<unknown printf code '" << *FmtStr << "'!>";
354 ArgNo++; break;
Chris Lattner08845a22001-10-29 20:27:45 +0000355 }
Chris Lattnere7c6f722001-12-13 00:43:47 +0000356 strcpy(OutputBuffer, Buffer);
357 OutputBuffer += strlen(Buffer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000358 }
Chris Lattner08845a22001-10-29 20:27:45 +0000359 break;
360 }
Chris Lattner08845a22001-10-29 20:27:45 +0000361 }
362}
363
Chris Lattnere7c6f722001-12-13 00:43:47 +0000364// int printf(sbyte *, ...) - a very rough implementation to make output useful.
Chris Lattnerb408b122002-03-29 03:57:15 +0000365GenericValue lle_X_printf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000366 char Buffer[10000];
367 vector<GenericValue> NewArgs;
368 GenericValue GV; GV.PointerVal = (PointerTy)Buffer;
369 NewArgs.push_back(GV);
370 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
371 GV = lle_X_sprintf(M, NewArgs);
372 cout << Buffer;
373 return GV;
374}
375
Chris Lattner665ee882002-03-08 22:51:07 +0000376// int sscanf(const char *format, ...);
Chris Lattnerb408b122002-03-29 03:57:15 +0000377GenericValue lle_X_sscanf(FunctionType *M, const vector<GenericValue> &args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000378 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
379
380 const char *Args[10];
381 for (unsigned i = 0; i < args.size(); ++i)
382 Args[i] = (const char*)args[i].PointerVal;
383
384 GenericValue GV;
385 GV.IntVal = sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
386 Args[5], Args[6], Args[7], Args[8], Args[9]);
387 return GV;
388}
389
390
Chris Lattner295fe672002-01-23 21:38:07 +0000391// int clock(void) - Profiling implementation
Chris Lattnerb408b122002-03-29 03:57:15 +0000392GenericValue lle_i_clock(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner295fe672002-01-23 21:38:07 +0000393 extern int clock(void);
394 GenericValue GV; GV.IntVal = clock();
395 return GV;
396}
Chris Lattnere7c6f722001-12-13 00:43:47 +0000397
Chris Lattner665ee882002-03-08 22:51:07 +0000398//===----------------------------------------------------------------------===//
399// IO Functions...
400//===----------------------------------------------------------------------===//
401
402// FILE *fopen(const char *filename, const char *mode);
Chris Lattnerb408b122002-03-29 03:57:15 +0000403GenericValue lle_X_fopen(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000404 assert(Args.size() == 2);
405 GenericValue GV;
406
407 GV.PointerVal = (PointerTy)fopen((const char *)Args[0].PointerVal,
408 (const char *)Args[1].PointerVal);
409 return GV;
410}
411
412// int fclose(FILE *F);
Chris Lattnerb408b122002-03-29 03:57:15 +0000413GenericValue lle_X_fclose(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000414 assert(Args.size() == 1);
415 GenericValue GV;
416
417 GV.IntVal = fclose((FILE *)Args[0].PointerVal);
418 return GV;
419}
420
421// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000422GenericValue lle_X_fread(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000423 assert(Args.size() == 4);
424 GenericValue GV;
425
426 GV.UIntVal = fread((void*)Args[0].PointerVal, Args[1].UIntVal,
427 Args[2].UIntVal, (FILE*)Args[3].PointerVal);
428 return GV;
429}
430
431// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000432GenericValue lle_X_fwrite(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000433 assert(Args.size() == 4);
434 GenericValue GV;
435
436 GV.UIntVal = fwrite((void*)Args[0].PointerVal, Args[1].UIntVal,
437 Args[2].UIntVal, (FILE*)Args[3].PointerVal);
438 return GV;
439}
440
441// char *fgets(char *s, int n, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000442GenericValue lle_X_fgets(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000443 assert(Args.size() == 3);
444 GenericValue GV;
445
446 GV.PointerVal = (PointerTy)fgets((char*)Args[0].PointerVal, Args[1].IntVal,
447 (FILE*)Args[2].PointerVal);
448 return GV;
449}
450
451// int fflush(FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000452GenericValue lle_X_fflush(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000453 assert(Args.size() == 1);
454 GenericValue GV;
455
456 GV.IntVal = fflush((FILE*)Args[0].PointerVal);
457 return GV;
458}
459
Chris Lattner7720c8e2001-09-10 04:50:17 +0000460} // End extern "C"
Chris Lattner4721f132001-10-30 20:28:00 +0000461
462
463void Interpreter::initializeExternalMethods() {
464 FuncNames["lle_VP_printstr"] = lle_VP_printstr;
465 FuncNames["lle_X_print"] = lle_X_print;
466 FuncNames["lle_X_printVal"] = lle_X_printVal;
467 FuncNames["lle_X_printString"] = lle_X_printString;
468 FuncNames["lle_X_printUByte"] = lle_X_printUByte;
469 FuncNames["lle_X_printSByte"] = lle_X_printSByte;
470 FuncNames["lle_X_printUShort"] = lle_X_printUShort;
471 FuncNames["lle_X_printShort"] = lle_X_printShort;
472 FuncNames["lle_X_printInt"] = lle_X_printInt;
473 FuncNames["lle_X_printUInt"] = lle_X_printUInt;
474 FuncNames["lle_X_printLong"] = lle_X_printLong;
475 FuncNames["lle_X_printULong"] = lle_X_printULong;
476 FuncNames["lle_X_printFloat"] = lle_X_printFloat;
477 FuncNames["lle_X_printDouble"] = lle_X_printDouble;
478 FuncNames["lle_X_printPointer"] = lle_X_printPointer;
Chris Lattner0f279b22001-11-03 10:15:32 +0000479 FuncNames["lle_Vb_putchar"] = lle_Vb_putchar;
480 FuncNames["lle_ii_putchar"] = lle_ii_putchar;
481 FuncNames["lle_VB_putchar"] = lle_VB_putchar;
482 FuncNames["lle_V___main"] = lle_V___main;
483 FuncNames["lle_X_exit"] = lle_X_exit;
484 FuncNames["lle_X_malloc"] = lle_X_malloc;
485 FuncNames["lle_X_free"] = lle_X_free;
Chris Lattner782b9392001-11-26 18:18:18 +0000486 FuncNames["lle_X_atoi"] = lle_X_atoi;
Chris Lattner0f279b22001-11-03 10:15:32 +0000487 FuncNames["lle_X_pow"] = lle_X_pow;
Chris Lattner34dd24b2002-02-18 19:06:25 +0000488 FuncNames["lle_X_exp"] = lle_X_exp;
Chris Lattner1b600142001-11-13 05:46:08 +0000489 FuncNames["lle_X_log"] = lle_X_log;
Chris Lattner782b9392001-11-26 18:18:18 +0000490 FuncNames["lle_X_floor"] = lle_X_floor;
491 FuncNames["lle_X_srand"] = lle_X_srand;
Chris Lattner1b600142001-11-13 05:46:08 +0000492 FuncNames["lle_X_drand48"] = lle_X_drand48;
493 FuncNames["lle_X_srand48"] = lle_X_srand48;
494 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
Chris Lattnerc063d382001-11-06 21:52:18 +0000495 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
Chris Lattner0f279b22001-11-03 10:15:32 +0000496 FuncNames["lle_X_printf"] = lle_X_printf;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000497 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
Chris Lattner665ee882002-03-08 22:51:07 +0000498 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
Chris Lattner295fe672002-01-23 21:38:07 +0000499 FuncNames["lle_i_clock"] = lle_i_clock;
Chris Lattner665ee882002-03-08 22:51:07 +0000500 FuncNames["lle_X_fopen"] = lle_X_fopen;
501 FuncNames["lle_X_fclose"] = lle_X_fclose;
502 FuncNames["lle_X_fread"] = lle_X_fread;
503 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
504 FuncNames["lle_X_fgets"] = lle_X_fgets;
505 FuncNames["lle_X_fflush"] = lle_X_fflush;
Chris Lattner4721f132001-10-30 20:28:00 +0000506}