blob: efe2e8fe3284bf9685c1c6a1995f8dbc0cfcc12d [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"
Vikram S. Adveeb1a8452002-05-19 15:59:25 +000015#include "../test/Libraries/libinstr/tracelib.h"
Chris Lattner7720c8e2001-09-10 04:50:17 +000016#include <map>
17#include <dlfcn.h>
Chris Lattner697954c2002-01-20 22:54:45 +000018#include <iostream>
Chris Lattner7720c8e2001-09-10 04:50:17 +000019#include <link.h>
Chris Lattnerc2593162001-10-27 08:28:11 +000020#include <math.h>
Chris Lattnerc063d382001-11-06 21:52:18 +000021#include <stdio.h>
Chris Lattner697954c2002-01-20 22:54:45 +000022using std::vector;
23using std::cout;
Chris Lattner7720c8e2001-09-10 04:50:17 +000024
Vikram S. Adveeb1a8452002-05-19 15:59:25 +000025
Chris Lattnerb408b122002-03-29 03:57:15 +000026typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
27static std::map<const Function *, ExFunc> Functions;
Chris Lattner697954c2002-01-20 22:54:45 +000028static std::map<std::string, ExFunc> FuncNames;
Chris Lattner7720c8e2001-09-10 04:50:17 +000029
Chris Lattnere43db882001-10-27 04:15:57 +000030static Interpreter *TheInterpreter;
31
32// getCurrentExecutablePath() - Return the directory that the lli executable
33// lives in.
34//
Chris Lattner697954c2002-01-20 22:54:45 +000035std::string Interpreter::getCurrentExecutablePath() const {
Chris Lattnere43db882001-10-27 04:15:57 +000036 Dl_info Info;
37 if (dladdr(&TheInterpreter, &Info) == 0) return "";
38
Chris Lattner697954c2002-01-20 22:54:45 +000039 std::string LinkAddr(Info.dli_fname);
Chris Lattnere43db882001-10-27 04:15:57 +000040 unsigned SlashPos = LinkAddr.rfind('/');
Chris Lattner697954c2002-01-20 22:54:45 +000041 if (SlashPos != std::string::npos)
Chris Lattnere43db882001-10-27 04:15:57 +000042 LinkAddr.resize(SlashPos); // Trim the executable name off...
43
44 return LinkAddr;
45}
46
47
Chris Lattner7720c8e2001-09-10 04:50:17 +000048static char getTypeID(const Type *Ty) {
49 switch (Ty->getPrimitiveID()) {
50 case Type::VoidTyID: return 'V';
51 case Type::BoolTyID: return 'o';
52 case Type::UByteTyID: return 'B';
53 case Type::SByteTyID: return 'b';
54 case Type::UShortTyID: return 'S';
55 case Type::ShortTyID: return 's';
56 case Type::UIntTyID: return 'I';
57 case Type::IntTyID: return 'i';
58 case Type::ULongTyID: return 'L';
59 case Type::LongTyID: return 'l';
60 case Type::FloatTyID: return 'F';
61 case Type::DoubleTyID: return 'D';
62 case Type::PointerTyID: return 'P';
Chris Lattnerb408b122002-03-29 03:57:15 +000063 case Type::FunctionTyID: return 'M';
Chris Lattner7720c8e2001-09-10 04:50:17 +000064 case Type::StructTyID: return 'T';
65 case Type::ArrayTyID: return 'A';
66 case Type::OpaqueTyID: return 'O';
67 default: return 'U';
68 }
69}
70
Chris Lattnerb408b122002-03-29 03:57:15 +000071static ExFunc lookupFunction(const Function *M) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000072 // Function not found, look it up... start by figuring out what the
73 // composite function name should be.
Chris Lattner697954c2002-01-20 22:54:45 +000074 std::string ExtName = "lle_";
Chris Lattnerb408b122002-03-29 03:57:15 +000075 const FunctionType *MT = M->getFunctionType();
Chris Lattner7720c8e2001-09-10 04:50:17 +000076 for (unsigned i = 0; const Type *Ty = MT->getContainedType(i); ++i)
77 ExtName += getTypeID(Ty);
78 ExtName += "_" + M->getName();
79
80 //cout << "Tried: '" << ExtName << "'\n";
Chris Lattner4721f132001-10-30 20:28:00 +000081 ExFunc FnPtr = FuncNames[ExtName];
82 if (FnPtr == 0)
83 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ExtName.c_str());
84 if (FnPtr == 0)
85 FnPtr = FuncNames["lle_X_"+M->getName()];
Chris Lattner7720c8e2001-09-10 04:50:17 +000086 if (FnPtr == 0) // Try calling a generic function... if it exists...
87 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ("lle_X_"+M->getName()).c_str());
88 if (FnPtr != 0)
Chris Lattner697954c2002-01-20 22:54:45 +000089 Functions.insert(std::make_pair(M, FnPtr)); // Cache for later
Chris Lattner7720c8e2001-09-10 04:50:17 +000090 return FnPtr;
91}
92
Chris Lattnerb408b122002-03-29 03:57:15 +000093GenericValue Interpreter::callExternalMethod(Function *M,
Chris Lattner4721f132001-10-30 20:28:00 +000094 const vector<GenericValue> &ArgVals) {
Chris Lattnere43db882001-10-27 04:15:57 +000095 TheInterpreter = this;
96
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000097 // Do a lookup to see if the function is in our cache... this should just be a
Chris Lattner7720c8e2001-09-10 04:50:17 +000098 // defered annotation!
Chris Lattnerb408b122002-03-29 03:57:15 +000099 std::map<const Function *, ExFunc>::iterator FI = Functions.find(M);
100 ExFunc Fn = (FI == Functions.end()) ? lookupFunction(M) : FI->second;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000101 if (Fn == 0) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000102 cout << "Tried to execute an unknown external function: "
Chris Lattner697954c2002-01-20 22:54:45 +0000103 << M->getType()->getDescription() << " " << M->getName() << "\n";
Chris Lattner4721f132001-10-30 20:28:00 +0000104 return GenericValue();
Chris Lattner7720c8e2001-09-10 04:50:17 +0000105 }
106
107 // TODO: FIXME when types are not const!
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000108 GenericValue Result = Fn(const_cast<FunctionType*>(M->getFunctionType()),
109 ArgVals);
Chris Lattner4721f132001-10-30 20:28:00 +0000110 return Result;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000111}
112
113
114//===----------------------------------------------------------------------===//
Chris Lattnerb408b122002-03-29 03:57:15 +0000115// Functions "exported" to the running application...
Chris Lattner7720c8e2001-09-10 04:50:17 +0000116//
117extern "C" { // Don't add C++ manglings to llvm mangling :)
118
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000119// Implement void printstr([ubyte {x N}] *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000120GenericValue lle_VP_printstr(FunctionType *M, const vector<GenericValue> &ArgVal){
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000121 assert(ArgVal.size() == 1 && "printstr only takes one argument!");
122 cout << (char*)ArgVal[0].PointerVal;
123 return GenericValue();
124}
125
Chris Lattner7720c8e2001-09-10 04:50:17 +0000126// Implement 'void print(X)' for every type...
Chris Lattnerb408b122002-03-29 03:57:15 +0000127GenericValue lle_X_print(FunctionType *M, const vector<GenericValue> &ArgVals) {
Chris Lattner7720c8e2001-09-10 04:50:17 +0000128 assert(ArgVals.size() == 1 && "generic print only takes one argument!");
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000129
130 Interpreter::print(M->getParamTypes()[0], ArgVals[0]);
131 return GenericValue();
132}
133
134// Implement 'void printVal(X)' for every type...
Chris Lattnerb408b122002-03-29 03:57:15 +0000135GenericValue lle_X_printVal(FunctionType *M, const vector<GenericValue> &ArgVal) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000136 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
137
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000138 // Specialize print([ubyte {x N} ] *) and print(sbyte *)
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000139 if (PointerType *PTy = dyn_cast<PointerType>(M->getParamTypes()[0].get()))
Chris Lattner7a176752001-12-04 00:03:30 +0000140 if (PTy->getElementType() == Type::SByteTy ||
141 isa<ArrayType>(PTy->getElementType())) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000142 return lle_VP_printstr(M, ArgVal);
143 }
144
145 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);
Chris Lattner7720c8e2001-09-10 04:50:17 +0000146 return GenericValue();
147}
148
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000149// Implement 'void printString(X)'
150// Argument must be [ubyte {x N} ] * or sbyte *
Chris Lattnerb408b122002-03-29 03:57:15 +0000151GenericValue lle_X_printString(FunctionType *M, const vector<GenericValue> &ArgVal) {
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000152 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
153 return lle_VP_printstr(M, ArgVal);
154}
155
156// Implement 'void print<TYPE>(X)' for each primitive type or pointer type
157#define PRINT_TYPE_FUNC(TYPENAME,TYPEID) \
Chris Lattnerb408b122002-03-29 03:57:15 +0000158 GenericValue lle_X_print##TYPENAME(FunctionType *M,\
Chris Lattner316a65b2001-10-28 22:38:22 +0000159 const vector<GenericValue> &ArgVal) {\
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000160 assert(ArgVal.size() == 1 && "generic print only takes one argument!");\
Chris Lattner69b5ce92001-11-26 19:19:27 +0000161 assert(M->getParamTypes()[0].get()->getPrimitiveID() == Type::TYPEID);\
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000162 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);\
163 return GenericValue();\
164 }
165
Chris Lattner4721f132001-10-30 20:28:00 +0000166PRINT_TYPE_FUNC(SByte, SByteTyID)
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000167PRINT_TYPE_FUNC(UByte, UByteTyID)
168PRINT_TYPE_FUNC(Short, ShortTyID)
169PRINT_TYPE_FUNC(UShort, UShortTyID)
170PRINT_TYPE_FUNC(Int, IntTyID)
171PRINT_TYPE_FUNC(UInt, UIntTyID)
172PRINT_TYPE_FUNC(Long, LongTyID)
173PRINT_TYPE_FUNC(ULong, ULongTyID)
174PRINT_TYPE_FUNC(Float, FloatTyID)
175PRINT_TYPE_FUNC(Double, DoubleTyID)
176PRINT_TYPE_FUNC(Pointer, PointerTyID)
177
178
Chris Lattner7720c8e2001-09-10 04:50:17 +0000179// void "putchar"(sbyte)
Chris Lattnerb408b122002-03-29 03:57:15 +0000180GenericValue lle_Vb_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner7720c8e2001-09-10 04:50:17 +0000181 cout << Args[0].SByteVal;
182 return GenericValue();
183}
184
Chris Lattnere43db882001-10-27 04:15:57 +0000185// int "putchar"(int)
Chris Lattnerb408b122002-03-29 03:57:15 +0000186GenericValue lle_ii_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner697954c2002-01-20 22:54:45 +0000187 cout << ((char)Args[0].IntVal) << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000188 return Args[0];
189}
190
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000191// void "putchar"(ubyte)
Chris Lattnerb408b122002-03-29 03:57:15 +0000192GenericValue lle_VB_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner697954c2002-01-20 22:54:45 +0000193 cout << Args[0].SByteVal << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000194 return Args[0];
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000195}
196
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000197// void "__main"()
Chris Lattnerb408b122002-03-29 03:57:15 +0000198GenericValue lle_V___main(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000199 return GenericValue();
200}
201
Chris Lattnere43db882001-10-27 04:15:57 +0000202// void "exit"(int)
Chris Lattnerb408b122002-03-29 03:57:15 +0000203GenericValue lle_X_exit(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere43db882001-10-27 04:15:57 +0000204 TheInterpreter->exitCalled(Args[0]);
205 return GenericValue();
206}
207
Chris Lattnerc2593162001-10-27 08:28:11 +0000208// void *malloc(uint)
Chris Lattnerb408b122002-03-29 03:57:15 +0000209GenericValue lle_X_malloc(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner4721f132001-10-30 20:28:00 +0000210 assert(Args.size() == 1 && "Malloc expects one argument!");
Chris Lattnerc2593162001-10-27 08:28:11 +0000211 GenericValue GV;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000212 GV.PointerVal = (PointerTy)malloc(Args[0].UIntVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000213 return GV;
214}
215
216// void free(void *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000217GenericValue lle_X_free(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000218 assert(Args.size() == 1);
Chris Lattner4721f132001-10-30 20:28:00 +0000219 free((void*)Args[0].PointerVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000220 return GenericValue();
221}
222
Chris Lattner782b9392001-11-26 18:18:18 +0000223// int atoi(char *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000224GenericValue lle_X_atoi(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000225 assert(Args.size() == 1);
226 GenericValue GV;
227 GV.IntVal = atoi((char*)Args[0].PointerVal);
228 return GV;
229}
230
Chris Lattnerc2593162001-10-27 08:28:11 +0000231// double pow(double, double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000232GenericValue lle_X_pow(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000233 assert(Args.size() == 2);
Chris Lattnerc2593162001-10-27 08:28:11 +0000234 GenericValue GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000235 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000236 return GV;
237}
238
Chris Lattner34dd24b2002-02-18 19:06:25 +0000239// double exp(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000240GenericValue lle_X_exp(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner34dd24b2002-02-18 19:06:25 +0000241 assert(Args.size() == 1);
242 GenericValue GV;
243 GV.DoubleVal = exp(Args[0].DoubleVal);
244 return GV;
245}
246
Chris Lattnerc063d382001-11-06 21:52:18 +0000247// double sqrt(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000248GenericValue lle_X_sqrt(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000249 assert(Args.size() == 1);
250 GenericValue GV;
251 GV.DoubleVal = sqrt(Args[0].DoubleVal);
252 return GV;
253}
Chris Lattnerc2593162001-10-27 08:28:11 +0000254
Chris Lattner86790052001-11-06 22:53:25 +0000255// double log(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000256GenericValue lle_X_log(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000257 assert(Args.size() == 1);
258 GenericValue GV;
259 GV.DoubleVal = log(Args[0].DoubleVal);
260 return GV;
261}
262
Chris Lattner782b9392001-11-26 18:18:18 +0000263// double floor(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000264GenericValue lle_X_floor(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000265 assert(Args.size() == 1);
266 GenericValue GV;
267 GV.DoubleVal = floor(Args[0].DoubleVal);
268 return GV;
269}
270
Chris Lattner86790052001-11-06 22:53:25 +0000271// double drand48()
Chris Lattnerb408b122002-03-29 03:57:15 +0000272GenericValue lle_X_drand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000273 assert(Args.size() == 0);
274 GenericValue GV;
275 GV.DoubleVal = drand48();
276 return GV;
277}
278
Chris Lattner1b600142001-11-13 05:46:08 +0000279// long lrand48()
Chris Lattnerb408b122002-03-29 03:57:15 +0000280GenericValue lle_X_lrand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000281 assert(Args.size() == 0);
282 GenericValue GV;
283 GV.IntVal = lrand48();
284 return GV;
285}
286
287// void srand48(long)
Chris Lattnerb408b122002-03-29 03:57:15 +0000288GenericValue lle_X_srand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000289 assert(Args.size() == 1);
290 srand48(Args[0].IntVal);
291 return GenericValue();
292}
293
Chris Lattner782b9392001-11-26 18:18:18 +0000294// void srand(uint)
Chris Lattnerb408b122002-03-29 03:57:15 +0000295GenericValue lle_X_srand(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000296 assert(Args.size() == 1);
297 srand(Args[0].UIntVal);
298 return GenericValue();
299}
Chris Lattner86790052001-11-06 22:53:25 +0000300
Chris Lattnere7c6f722001-12-13 00:43:47 +0000301// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
302// output useful.
Chris Lattnerb408b122002-03-29 03:57:15 +0000303GenericValue lle_X_sprintf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000304 char *OutputBuffer = (char *)Args[0].PointerVal;
305 const char *FmtStr = (const char *)Args[1].PointerVal;
306 unsigned ArgNo = 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000307
308 // printf should return # chars printed. This is completely incorrect, but
309 // close enough for now.
310 GenericValue GV; GV.IntVal = strlen(FmtStr);
311 while (1) {
312 switch (*FmtStr) {
313 case 0: return GV; // Null terminator...
314 default: // Normal nonspecial character
Chris Lattnere7c6f722001-12-13 00:43:47 +0000315 sprintf(OutputBuffer++, "%c", *FmtStr++);
Chris Lattner08845a22001-10-29 20:27:45 +0000316 break;
317 case '\\': { // Handle escape codes
Chris Lattnere7c6f722001-12-13 00:43:47 +0000318 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
319 FmtStr += 2; OutputBuffer += 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000320 break;
321 }
322 case '%': { // Handle format specifiers
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000323 char FmtBuf[100] = "", Buffer[1000] = "";
324 char *FB = FmtBuf;
325 *FB++ = *FmtStr++;
326 char Last = *FB++ = *FmtStr++;
327 unsigned HowLong = 0;
328 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
329 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
330 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
331 Last != 'p' && Last != 's' && Last != '%') {
332 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
333 Last = *FB++ = *FmtStr++;
Chris Lattner08845a22001-10-29 20:27:45 +0000334 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000335 *FB = 0;
336
337 switch (Last) {
338 case '%':
339 sprintf(Buffer, FmtBuf); break;
340 case 'c':
Chris Lattner2012d5e2002-04-17 17:43:01 +0000341 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000342 case 'd': case 'i':
343 case 'u': case 'o':
344 case 'x': case 'X':
345 if (HowLong == 2)
346 sprintf(Buffer, FmtBuf, Args[ArgNo++].ULongVal);
347 else
348 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
349 case 'e': case 'E': case 'g': case 'G': case 'f':
350 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
351 case 'p':
352 sprintf(Buffer, FmtBuf, (void*)Args[ArgNo++].PointerVal); break;
353 case 's':
354 sprintf(Buffer, FmtBuf, (char*)Args[ArgNo++].PointerVal); break;
355 default: cout << "<unknown printf code '" << *FmtStr << "'!>";
356 ArgNo++; break;
Chris Lattner08845a22001-10-29 20:27:45 +0000357 }
Chris Lattnere7c6f722001-12-13 00:43:47 +0000358 strcpy(OutputBuffer, Buffer);
359 OutputBuffer += strlen(Buffer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000360 }
Chris Lattner08845a22001-10-29 20:27:45 +0000361 break;
362 }
Chris Lattner08845a22001-10-29 20:27:45 +0000363 }
364}
365
Chris Lattnere7c6f722001-12-13 00:43:47 +0000366// int printf(sbyte *, ...) - a very rough implementation to make output useful.
Chris Lattnerb408b122002-03-29 03:57:15 +0000367GenericValue lle_X_printf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000368 char Buffer[10000];
369 vector<GenericValue> NewArgs;
370 GenericValue GV; GV.PointerVal = (PointerTy)Buffer;
371 NewArgs.push_back(GV);
372 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
373 GV = lle_X_sprintf(M, NewArgs);
374 cout << Buffer;
375 return GV;
376}
377
Chris Lattner665ee882002-03-08 22:51:07 +0000378// int sscanf(const char *format, ...);
Chris Lattnerb408b122002-03-29 03:57:15 +0000379GenericValue lle_X_sscanf(FunctionType *M, const vector<GenericValue> &args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000380 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
381
382 const char *Args[10];
383 for (unsigned i = 0; i < args.size(); ++i)
384 Args[i] = (const char*)args[i].PointerVal;
385
386 GenericValue GV;
387 GV.IntVal = sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
388 Args[5], Args[6], Args[7], Args[8], Args[9]);
389 return GV;
390}
391
392
Chris Lattner295fe672002-01-23 21:38:07 +0000393// int clock(void) - Profiling implementation
Chris Lattnerb408b122002-03-29 03:57:15 +0000394GenericValue lle_i_clock(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner295fe672002-01-23 21:38:07 +0000395 extern int clock(void);
396 GenericValue GV; GV.IntVal = clock();
397 return GV;
398}
Chris Lattnere7c6f722001-12-13 00:43:47 +0000399
Chris Lattner665ee882002-03-08 22:51:07 +0000400//===----------------------------------------------------------------------===//
401// IO Functions...
402//===----------------------------------------------------------------------===//
403
404// FILE *fopen(const char *filename, const char *mode);
Chris Lattnerb408b122002-03-29 03:57:15 +0000405GenericValue lle_X_fopen(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000406 assert(Args.size() == 2);
407 GenericValue GV;
408
409 GV.PointerVal = (PointerTy)fopen((const char *)Args[0].PointerVal,
410 (const char *)Args[1].PointerVal);
411 return GV;
412}
413
414// int fclose(FILE *F);
Chris Lattnerb408b122002-03-29 03:57:15 +0000415GenericValue lle_X_fclose(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000416 assert(Args.size() == 1);
417 GenericValue GV;
418
419 GV.IntVal = fclose((FILE *)Args[0].PointerVal);
420 return GV;
421}
422
423// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000424GenericValue lle_X_fread(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000425 assert(Args.size() == 4);
426 GenericValue GV;
427
428 GV.UIntVal = fread((void*)Args[0].PointerVal, Args[1].UIntVal,
429 Args[2].UIntVal, (FILE*)Args[3].PointerVal);
430 return GV;
431}
432
433// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000434GenericValue lle_X_fwrite(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000435 assert(Args.size() == 4);
436 GenericValue GV;
437
438 GV.UIntVal = fwrite((void*)Args[0].PointerVal, Args[1].UIntVal,
439 Args[2].UIntVal, (FILE*)Args[3].PointerVal);
440 return GV;
441}
442
443// char *fgets(char *s, int n, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000444GenericValue lle_X_fgets(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000445 assert(Args.size() == 3);
446 GenericValue GV;
447
448 GV.PointerVal = (PointerTy)fgets((char*)Args[0].PointerVal, Args[1].IntVal,
449 (FILE*)Args[2].PointerVal);
450 return GV;
451}
452
453// int fflush(FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000454GenericValue lle_X_fflush(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000455 assert(Args.size() == 1);
456 GenericValue GV;
457
458 GV.IntVal = fflush((FILE*)Args[0].PointerVal);
459 return GV;
460}
461
Vikram S. Adveeb1a8452002-05-19 15:59:25 +0000462// unsigned int HashPointerToSeqNum(char* ptr)
463GenericValue lle_X_HashPointerToSeqNum(FunctionType *M, const vector<GenericValue> &Args) {
464 assert(Args.size() == 1);
465 GenericValue GV;
466
467 GV.UIntVal = HashPointerToSeqNum((char*) Args[0].PointerVal);
468 return GV;
469}
470
471// void ReleasePointerSeqNum(char* ptr);
472GenericValue lle_X_ReleasePointerSeqNum(FunctionType *M, const vector<GenericValue> &Args) {
473 assert(Args.size() == 1);
474 ReleasePointerSeqNum((char*) Args[0].PointerVal);
475 return GenericValue();
476}
477
478// void RecordPointer(char* ptr);
479GenericValue lle_X_RecordPointer(FunctionType *M, const vector<GenericValue> &Args) {
480 assert(Args.size() == 1);
481 RecordPointer((char*) Args[0].PointerVal);
482 return GenericValue();
483}
484
485// void PushPointerSet();
486GenericValue lle_X_PushPointerSet(FunctionType *M, const vector<GenericValue> &Args) {
487 assert(Args.size() == 0);
488 PushPointerSet();
489 return GenericValue();
490}
491
492// void ReleaseRecordedPointers();
493GenericValue lle_X_ReleasePointersPopSet(FunctionType *M, const vector<GenericValue> &Args) {
494 assert(Args.size() == 0);
495 ReleasePointersPopSet();
496 return GenericValue();
497}
498
Chris Lattner7720c8e2001-09-10 04:50:17 +0000499} // End extern "C"
Chris Lattner4721f132001-10-30 20:28:00 +0000500
501
502void Interpreter::initializeExternalMethods() {
503 FuncNames["lle_VP_printstr"] = lle_VP_printstr;
504 FuncNames["lle_X_print"] = lle_X_print;
505 FuncNames["lle_X_printVal"] = lle_X_printVal;
506 FuncNames["lle_X_printString"] = lle_X_printString;
507 FuncNames["lle_X_printUByte"] = lle_X_printUByte;
508 FuncNames["lle_X_printSByte"] = lle_X_printSByte;
509 FuncNames["lle_X_printUShort"] = lle_X_printUShort;
510 FuncNames["lle_X_printShort"] = lle_X_printShort;
511 FuncNames["lle_X_printInt"] = lle_X_printInt;
512 FuncNames["lle_X_printUInt"] = lle_X_printUInt;
513 FuncNames["lle_X_printLong"] = lle_X_printLong;
514 FuncNames["lle_X_printULong"] = lle_X_printULong;
515 FuncNames["lle_X_printFloat"] = lle_X_printFloat;
516 FuncNames["lle_X_printDouble"] = lle_X_printDouble;
517 FuncNames["lle_X_printPointer"] = lle_X_printPointer;
Chris Lattner0f279b22001-11-03 10:15:32 +0000518 FuncNames["lle_Vb_putchar"] = lle_Vb_putchar;
519 FuncNames["lle_ii_putchar"] = lle_ii_putchar;
520 FuncNames["lle_VB_putchar"] = lle_VB_putchar;
521 FuncNames["lle_V___main"] = lle_V___main;
522 FuncNames["lle_X_exit"] = lle_X_exit;
523 FuncNames["lle_X_malloc"] = lle_X_malloc;
524 FuncNames["lle_X_free"] = lle_X_free;
Chris Lattner782b9392001-11-26 18:18:18 +0000525 FuncNames["lle_X_atoi"] = lle_X_atoi;
Chris Lattner0f279b22001-11-03 10:15:32 +0000526 FuncNames["lle_X_pow"] = lle_X_pow;
Chris Lattner34dd24b2002-02-18 19:06:25 +0000527 FuncNames["lle_X_exp"] = lle_X_exp;
Chris Lattner1b600142001-11-13 05:46:08 +0000528 FuncNames["lle_X_log"] = lle_X_log;
Chris Lattner782b9392001-11-26 18:18:18 +0000529 FuncNames["lle_X_floor"] = lle_X_floor;
530 FuncNames["lle_X_srand"] = lle_X_srand;
Chris Lattner1b600142001-11-13 05:46:08 +0000531 FuncNames["lle_X_drand48"] = lle_X_drand48;
532 FuncNames["lle_X_srand48"] = lle_X_srand48;
533 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
Chris Lattnerc063d382001-11-06 21:52:18 +0000534 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
Chris Lattner0f279b22001-11-03 10:15:32 +0000535 FuncNames["lle_X_printf"] = lle_X_printf;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000536 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
Chris Lattner665ee882002-03-08 22:51:07 +0000537 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
Chris Lattner295fe672002-01-23 21:38:07 +0000538 FuncNames["lle_i_clock"] = lle_i_clock;
Chris Lattner665ee882002-03-08 22:51:07 +0000539 FuncNames["lle_X_fopen"] = lle_X_fopen;
540 FuncNames["lle_X_fclose"] = lle_X_fclose;
541 FuncNames["lle_X_fread"] = lle_X_fread;
542 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
543 FuncNames["lle_X_fgets"] = lle_X_fgets;
544 FuncNames["lle_X_fflush"] = lle_X_fflush;
Vikram S. Adveeb1a8452002-05-19 15:59:25 +0000545 FuncNames["lle_X_HashPointerToSeqNum"] = lle_X_HashPointerToSeqNum;
546 FuncNames["lle_X_ReleasePointerSeqNum"] = lle_X_ReleasePointerSeqNum;
547 FuncNames["lle_X_RecordPointer"] = lle_X_RecordPointer;
548 FuncNames["lle_X_PushPointerSet"] = lle_X_PushPointerSet;
549 FuncNames["lle_X_ReleasePointersPopSet"] = lle_X_ReleasePointersPopSet;
Chris Lattner4721f132001-10-30 20:28:00 +0000550}