blob: 1a425c2bb62fec27483f352f142886ba44187367 [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
Vikram S. Adveeb1a8452002-05-19 15:59:25 +000024
Chris Lattnerb408b122002-03-29 03:57:15 +000025typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
26static std::map<const Function *, ExFunc> Functions;
Chris Lattner697954c2002-01-20 22:54:45 +000027static std::map<std::string, ExFunc> FuncNames;
Chris Lattner7720c8e2001-09-10 04:50:17 +000028
Chris Lattnere43db882001-10-27 04:15:57 +000029static Interpreter *TheInterpreter;
30
31// getCurrentExecutablePath() - Return the directory that the lli executable
32// lives in.
33//
Chris Lattner697954c2002-01-20 22:54:45 +000034std::string Interpreter::getCurrentExecutablePath() const {
Chris Lattnere43db882001-10-27 04:15:57 +000035 Dl_info Info;
36 if (dladdr(&TheInterpreter, &Info) == 0) return "";
37
Chris Lattner697954c2002-01-20 22:54:45 +000038 std::string LinkAddr(Info.dli_fname);
Chris Lattnere43db882001-10-27 04:15:57 +000039 unsigned SlashPos = LinkAddr.rfind('/');
Chris Lattner697954c2002-01-20 22:54:45 +000040 if (SlashPos != std::string::npos)
Chris Lattnere43db882001-10-27 04:15:57 +000041 LinkAddr.resize(SlashPos); // Trim the executable name off...
42
43 return LinkAddr;
44}
45
46
Chris Lattner7720c8e2001-09-10 04:50:17 +000047static char getTypeID(const Type *Ty) {
48 switch (Ty->getPrimitiveID()) {
49 case Type::VoidTyID: return 'V';
50 case Type::BoolTyID: return 'o';
51 case Type::UByteTyID: return 'B';
52 case Type::SByteTyID: return 'b';
53 case Type::UShortTyID: return 'S';
54 case Type::ShortTyID: return 's';
55 case Type::UIntTyID: return 'I';
56 case Type::IntTyID: return 'i';
57 case Type::ULongTyID: return 'L';
58 case Type::LongTyID: return 'l';
59 case Type::FloatTyID: return 'F';
60 case Type::DoubleTyID: return 'D';
61 case Type::PointerTyID: return 'P';
Chris Lattnerb408b122002-03-29 03:57:15 +000062 case Type::FunctionTyID: return 'M';
Chris Lattner7720c8e2001-09-10 04:50:17 +000063 case Type::StructTyID: return 'T';
64 case Type::ArrayTyID: return 'A';
65 case Type::OpaqueTyID: return 'O';
66 default: return 'U';
67 }
68}
69
Chris Lattnerb408b122002-03-29 03:57:15 +000070static ExFunc lookupFunction(const Function *M) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000071 // Function not found, look it up... start by figuring out what the
72 // composite function name should be.
Chris Lattner697954c2002-01-20 22:54:45 +000073 std::string ExtName = "lle_";
Chris Lattnerb408b122002-03-29 03:57:15 +000074 const FunctionType *MT = M->getFunctionType();
Chris Lattner7720c8e2001-09-10 04:50:17 +000075 for (unsigned i = 0; const Type *Ty = MT->getContainedType(i); ++i)
76 ExtName += getTypeID(Ty);
77 ExtName += "_" + M->getName();
78
79 //cout << "Tried: '" << ExtName << "'\n";
Chris Lattner4721f132001-10-30 20:28:00 +000080 ExFunc FnPtr = FuncNames[ExtName];
81 if (FnPtr == 0)
82 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ExtName.c_str());
83 if (FnPtr == 0)
84 FnPtr = FuncNames["lle_X_"+M->getName()];
Chris Lattner7720c8e2001-09-10 04:50:17 +000085 if (FnPtr == 0) // Try calling a generic function... if it exists...
86 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ("lle_X_"+M->getName()).c_str());
87 if (FnPtr != 0)
Chris Lattner697954c2002-01-20 22:54:45 +000088 Functions.insert(std::make_pair(M, FnPtr)); // Cache for later
Chris Lattner7720c8e2001-09-10 04:50:17 +000089 return FnPtr;
90}
91
Chris Lattnerb408b122002-03-29 03:57:15 +000092GenericValue Interpreter::callExternalMethod(Function *M,
Chris Lattner4721f132001-10-30 20:28:00 +000093 const vector<GenericValue> &ArgVals) {
Chris Lattnere43db882001-10-27 04:15:57 +000094 TheInterpreter = this;
95
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000096 // Do a lookup to see if the function is in our cache... this should just be a
Chris Lattner7720c8e2001-09-10 04:50:17 +000097 // defered annotation!
Chris Lattnerb408b122002-03-29 03:57:15 +000098 std::map<const Function *, ExFunc>::iterator FI = Functions.find(M);
99 ExFunc Fn = (FI == Functions.end()) ? lookupFunction(M) : FI->second;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000100 if (Fn == 0) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000101 cout << "Tried to execute an unknown external function: "
Chris Lattner697954c2002-01-20 22:54:45 +0000102 << M->getType()->getDescription() << " " << M->getName() << "\n";
Chris Lattner4721f132001-10-30 20:28:00 +0000103 return GenericValue();
Chris Lattner7720c8e2001-09-10 04:50:17 +0000104 }
105
106 // TODO: FIXME when types are not const!
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000107 GenericValue Result = Fn(const_cast<FunctionType*>(M->getFunctionType()),
108 ArgVals);
Chris Lattner4721f132001-10-30 20:28:00 +0000109 return Result;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000110}
111
112
113//===----------------------------------------------------------------------===//
Chris Lattnerb408b122002-03-29 03:57:15 +0000114// Functions "exported" to the running application...
Chris Lattner7720c8e2001-09-10 04:50:17 +0000115//
116extern "C" { // Don't add C++ manglings to llvm mangling :)
117
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000118// Implement void printstr([ubyte {x N}] *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000119GenericValue lle_VP_printstr(FunctionType *M, const vector<GenericValue> &ArgVal){
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000120 assert(ArgVal.size() == 1 && "printstr only takes one argument!");
121 cout << (char*)ArgVal[0].PointerVal;
122 return GenericValue();
123}
124
Chris Lattner7720c8e2001-09-10 04:50:17 +0000125// Implement 'void print(X)' for every type...
Chris Lattnerb408b122002-03-29 03:57:15 +0000126GenericValue lle_X_print(FunctionType *M, const vector<GenericValue> &ArgVals) {
Chris Lattner7720c8e2001-09-10 04:50:17 +0000127 assert(ArgVals.size() == 1 && "generic print only takes one argument!");
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000128
129 Interpreter::print(M->getParamTypes()[0], ArgVals[0]);
130 return GenericValue();
131}
132
133// Implement 'void printVal(X)' for every type...
Chris Lattnerb408b122002-03-29 03:57:15 +0000134GenericValue lle_X_printVal(FunctionType *M, const vector<GenericValue> &ArgVal) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000135 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
136
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000137 // Specialize print([ubyte {x N} ] *) and print(sbyte *)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000138 if (const PointerType *PTy =
139 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 Lattner1ee34a52002-05-20 21:17:16 +0000208// void "abort"(void)
209GenericValue lle_X_abort(FunctionType *M, const vector<GenericValue> &Args) {
210 std::cerr << "***PROGRAM ABORTED***!\n";
211 GenericValue GV;
212 GV.IntVal = 1;
213 TheInterpreter->exitCalled(GV);
214 return GenericValue();
215}
216
Chris Lattnerc2593162001-10-27 08:28:11 +0000217// void *malloc(uint)
Chris Lattnerb408b122002-03-29 03:57:15 +0000218GenericValue lle_X_malloc(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner4721f132001-10-30 20:28:00 +0000219 assert(Args.size() == 1 && "Malloc expects one argument!");
Chris Lattnerc2593162001-10-27 08:28:11 +0000220 GenericValue GV;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000221 GV.PointerVal = (PointerTy)malloc(Args[0].UIntVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000222 return GV;
223}
224
225// void free(void *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000226GenericValue lle_X_free(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000227 assert(Args.size() == 1);
Chris Lattner4721f132001-10-30 20:28:00 +0000228 free((void*)Args[0].PointerVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000229 return GenericValue();
230}
231
Chris Lattner782b9392001-11-26 18:18:18 +0000232// int atoi(char *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000233GenericValue lle_X_atoi(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000234 assert(Args.size() == 1);
235 GenericValue GV;
236 GV.IntVal = atoi((char*)Args[0].PointerVal);
237 return GV;
238}
239
Chris Lattnerc2593162001-10-27 08:28:11 +0000240// double pow(double, double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000241GenericValue lle_X_pow(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000242 assert(Args.size() == 2);
Chris Lattnerc2593162001-10-27 08:28:11 +0000243 GenericValue GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000244 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000245 return GV;
246}
247
Chris Lattner34dd24b2002-02-18 19:06:25 +0000248// double exp(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000249GenericValue lle_X_exp(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner34dd24b2002-02-18 19:06:25 +0000250 assert(Args.size() == 1);
251 GenericValue GV;
252 GV.DoubleVal = exp(Args[0].DoubleVal);
253 return GV;
254}
255
Chris Lattnerc063d382001-11-06 21:52:18 +0000256// double sqrt(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000257GenericValue lle_X_sqrt(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000258 assert(Args.size() == 1);
259 GenericValue GV;
260 GV.DoubleVal = sqrt(Args[0].DoubleVal);
261 return GV;
262}
Chris Lattnerc2593162001-10-27 08:28:11 +0000263
Chris Lattner86790052001-11-06 22:53:25 +0000264// double log(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000265GenericValue lle_X_log(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000266 assert(Args.size() == 1);
267 GenericValue GV;
268 GV.DoubleVal = log(Args[0].DoubleVal);
269 return GV;
270}
271
Chris Lattner782b9392001-11-26 18:18:18 +0000272// double floor(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000273GenericValue lle_X_floor(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000274 assert(Args.size() == 1);
275 GenericValue GV;
276 GV.DoubleVal = floor(Args[0].DoubleVal);
277 return GV;
278}
279
Chris Lattner86790052001-11-06 22:53:25 +0000280// double drand48()
Chris Lattnerb408b122002-03-29 03:57:15 +0000281GenericValue lle_X_drand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000282 assert(Args.size() == 0);
283 GenericValue GV;
284 GV.DoubleVal = drand48();
285 return GV;
286}
287
Chris Lattner1b600142001-11-13 05:46:08 +0000288// long lrand48()
Chris Lattnerb408b122002-03-29 03:57:15 +0000289GenericValue lle_X_lrand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000290 assert(Args.size() == 0);
291 GenericValue GV;
292 GV.IntVal = lrand48();
293 return GV;
294}
295
296// void srand48(long)
Chris Lattnerb408b122002-03-29 03:57:15 +0000297GenericValue lle_X_srand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000298 assert(Args.size() == 1);
299 srand48(Args[0].IntVal);
300 return GenericValue();
301}
302
Chris Lattner782b9392001-11-26 18:18:18 +0000303// void srand(uint)
Chris Lattnerb408b122002-03-29 03:57:15 +0000304GenericValue lle_X_srand(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000305 assert(Args.size() == 1);
306 srand(Args[0].UIntVal);
307 return GenericValue();
308}
Chris Lattner86790052001-11-06 22:53:25 +0000309
Chris Lattnere7c6f722001-12-13 00:43:47 +0000310// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
311// output useful.
Chris Lattnerb408b122002-03-29 03:57:15 +0000312GenericValue lle_X_sprintf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000313 char *OutputBuffer = (char *)Args[0].PointerVal;
314 const char *FmtStr = (const char *)Args[1].PointerVal;
315 unsigned ArgNo = 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000316
317 // printf should return # chars printed. This is completely incorrect, but
318 // close enough for now.
319 GenericValue GV; GV.IntVal = strlen(FmtStr);
320 while (1) {
321 switch (*FmtStr) {
322 case 0: return GV; // Null terminator...
323 default: // Normal nonspecial character
Chris Lattnere7c6f722001-12-13 00:43:47 +0000324 sprintf(OutputBuffer++, "%c", *FmtStr++);
Chris Lattner08845a22001-10-29 20:27:45 +0000325 break;
326 case '\\': { // Handle escape codes
Chris Lattnere7c6f722001-12-13 00:43:47 +0000327 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
328 FmtStr += 2; OutputBuffer += 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000329 break;
330 }
331 case '%': { // Handle format specifiers
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000332 char FmtBuf[100] = "", Buffer[1000] = "";
333 char *FB = FmtBuf;
334 *FB++ = *FmtStr++;
335 char Last = *FB++ = *FmtStr++;
336 unsigned HowLong = 0;
337 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
338 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
339 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
340 Last != 'p' && Last != 's' && Last != '%') {
341 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
342 Last = *FB++ = *FmtStr++;
Chris Lattner08845a22001-10-29 20:27:45 +0000343 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000344 *FB = 0;
345
346 switch (Last) {
347 case '%':
348 sprintf(Buffer, FmtBuf); break;
349 case 'c':
Chris Lattner2012d5e2002-04-17 17:43:01 +0000350 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000351 case 'd': case 'i':
352 case 'u': case 'o':
353 case 'x': case 'X':
Chris Lattner69ab7a82002-08-02 23:08:32 +0000354 if (HowLong >= 1) {
355 if (HowLong == 1) {
356 // Make sure we use %lld with a 64 bit argument because we might be
357 // compiling LLI on a 32 bit compiler.
358 unsigned Size = strlen(FmtBuf);
359 FmtBuf[Size] = FmtBuf[Size-1];
360 FmtBuf[Size+1] = 0;
361 FmtBuf[Size-1] = 'l';
362 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000363 sprintf(Buffer, FmtBuf, Args[ArgNo++].ULongVal);
Chris Lattner69ab7a82002-08-02 23:08:32 +0000364 } else
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000365 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
366 case 'e': case 'E': case 'g': case 'G': case 'f':
367 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
368 case 'p':
369 sprintf(Buffer, FmtBuf, (void*)Args[ArgNo++].PointerVal); break;
370 case 's':
371 sprintf(Buffer, FmtBuf, (char*)Args[ArgNo++].PointerVal); break;
372 default: cout << "<unknown printf code '" << *FmtStr << "'!>";
373 ArgNo++; break;
Chris Lattner08845a22001-10-29 20:27:45 +0000374 }
Chris Lattnere7c6f722001-12-13 00:43:47 +0000375 strcpy(OutputBuffer, Buffer);
376 OutputBuffer += strlen(Buffer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000377 }
Chris Lattner08845a22001-10-29 20:27:45 +0000378 break;
379 }
Chris Lattner08845a22001-10-29 20:27:45 +0000380 }
381}
382
Chris Lattnere7c6f722001-12-13 00:43:47 +0000383// int printf(sbyte *, ...) - a very rough implementation to make output useful.
Chris Lattnerb408b122002-03-29 03:57:15 +0000384GenericValue lle_X_printf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000385 char Buffer[10000];
386 vector<GenericValue> NewArgs;
387 GenericValue GV; GV.PointerVal = (PointerTy)Buffer;
388 NewArgs.push_back(GV);
389 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
390 GV = lle_X_sprintf(M, NewArgs);
391 cout << Buffer;
392 return GV;
393}
394
Chris Lattner665ee882002-03-08 22:51:07 +0000395// int sscanf(const char *format, ...);
Chris Lattnerb408b122002-03-29 03:57:15 +0000396GenericValue lle_X_sscanf(FunctionType *M, const vector<GenericValue> &args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000397 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
398
399 const char *Args[10];
400 for (unsigned i = 0; i < args.size(); ++i)
401 Args[i] = (const char*)args[i].PointerVal;
402
403 GenericValue GV;
404 GV.IntVal = sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
405 Args[5], Args[6], Args[7], Args[8], Args[9]);
406 return GV;
407}
408
409
Chris Lattner295fe672002-01-23 21:38:07 +0000410// int clock(void) - Profiling implementation
Chris Lattnerb408b122002-03-29 03:57:15 +0000411GenericValue lle_i_clock(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner295fe672002-01-23 21:38:07 +0000412 extern int clock(void);
413 GenericValue GV; GV.IntVal = clock();
414 return GV;
415}
Chris Lattnere7c6f722001-12-13 00:43:47 +0000416
Chris Lattner665ee882002-03-08 22:51:07 +0000417//===----------------------------------------------------------------------===//
418// IO Functions...
419//===----------------------------------------------------------------------===//
420
421// FILE *fopen(const char *filename, const char *mode);
Chris Lattnerb408b122002-03-29 03:57:15 +0000422GenericValue lle_X_fopen(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000423 assert(Args.size() == 2);
424 GenericValue GV;
425
426 GV.PointerVal = (PointerTy)fopen((const char *)Args[0].PointerVal,
427 (const char *)Args[1].PointerVal);
428 return GV;
429}
430
431// int fclose(FILE *F);
Chris Lattnerb408b122002-03-29 03:57:15 +0000432GenericValue lle_X_fclose(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000433 assert(Args.size() == 1);
434 GenericValue GV;
435
436 GV.IntVal = fclose((FILE *)Args[0].PointerVal);
437 return GV;
438}
439
440// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000441GenericValue lle_X_fread(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000442 assert(Args.size() == 4);
443 GenericValue GV;
444
445 GV.UIntVal = fread((void*)Args[0].PointerVal, Args[1].UIntVal,
446 Args[2].UIntVal, (FILE*)Args[3].PointerVal);
447 return GV;
448}
449
450// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000451GenericValue lle_X_fwrite(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000452 assert(Args.size() == 4);
453 GenericValue GV;
454
455 GV.UIntVal = fwrite((void*)Args[0].PointerVal, Args[1].UIntVal,
456 Args[2].UIntVal, (FILE*)Args[3].PointerVal);
457 return GV;
458}
459
460// char *fgets(char *s, int n, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000461GenericValue lle_X_fgets(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000462 assert(Args.size() == 3);
463 GenericValue GV;
464
465 GV.PointerVal = (PointerTy)fgets((char*)Args[0].PointerVal, Args[1].IntVal,
466 (FILE*)Args[2].PointerVal);
467 return GV;
468}
469
470// int fflush(FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000471GenericValue lle_X_fflush(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000472 assert(Args.size() == 1);
473 GenericValue GV;
474
475 GV.IntVal = fflush((FILE*)Args[0].PointerVal);
476 return GV;
477}
478
Chris Lattner7720c8e2001-09-10 04:50:17 +0000479} // End extern "C"
Chris Lattner4721f132001-10-30 20:28:00 +0000480
481
482void Interpreter::initializeExternalMethods() {
483 FuncNames["lle_VP_printstr"] = lle_VP_printstr;
484 FuncNames["lle_X_print"] = lle_X_print;
485 FuncNames["lle_X_printVal"] = lle_X_printVal;
486 FuncNames["lle_X_printString"] = lle_X_printString;
487 FuncNames["lle_X_printUByte"] = lle_X_printUByte;
488 FuncNames["lle_X_printSByte"] = lle_X_printSByte;
489 FuncNames["lle_X_printUShort"] = lle_X_printUShort;
490 FuncNames["lle_X_printShort"] = lle_X_printShort;
491 FuncNames["lle_X_printInt"] = lle_X_printInt;
492 FuncNames["lle_X_printUInt"] = lle_X_printUInt;
493 FuncNames["lle_X_printLong"] = lle_X_printLong;
494 FuncNames["lle_X_printULong"] = lle_X_printULong;
495 FuncNames["lle_X_printFloat"] = lle_X_printFloat;
496 FuncNames["lle_X_printDouble"] = lle_X_printDouble;
497 FuncNames["lle_X_printPointer"] = lle_X_printPointer;
Chris Lattner0f279b22001-11-03 10:15:32 +0000498 FuncNames["lle_Vb_putchar"] = lle_Vb_putchar;
499 FuncNames["lle_ii_putchar"] = lle_ii_putchar;
500 FuncNames["lle_VB_putchar"] = lle_VB_putchar;
501 FuncNames["lle_V___main"] = lle_V___main;
502 FuncNames["lle_X_exit"] = lle_X_exit;
Chris Lattner1ee34a52002-05-20 21:17:16 +0000503 FuncNames["lle_X_abort"] = lle_X_abort;
Chris Lattner0f279b22001-11-03 10:15:32 +0000504 FuncNames["lle_X_malloc"] = lle_X_malloc;
505 FuncNames["lle_X_free"] = lle_X_free;
Chris Lattner782b9392001-11-26 18:18:18 +0000506 FuncNames["lle_X_atoi"] = lle_X_atoi;
Chris Lattner0f279b22001-11-03 10:15:32 +0000507 FuncNames["lle_X_pow"] = lle_X_pow;
Chris Lattner34dd24b2002-02-18 19:06:25 +0000508 FuncNames["lle_X_exp"] = lle_X_exp;
Chris Lattner1b600142001-11-13 05:46:08 +0000509 FuncNames["lle_X_log"] = lle_X_log;
Chris Lattner782b9392001-11-26 18:18:18 +0000510 FuncNames["lle_X_floor"] = lle_X_floor;
511 FuncNames["lle_X_srand"] = lle_X_srand;
Chris Lattner1b600142001-11-13 05:46:08 +0000512 FuncNames["lle_X_drand48"] = lle_X_drand48;
513 FuncNames["lle_X_srand48"] = lle_X_srand48;
514 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
Chris Lattnerc063d382001-11-06 21:52:18 +0000515 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
Chris Lattner0f279b22001-11-03 10:15:32 +0000516 FuncNames["lle_X_printf"] = lle_X_printf;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000517 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
Chris Lattner665ee882002-03-08 22:51:07 +0000518 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
Chris Lattner295fe672002-01-23 21:38:07 +0000519 FuncNames["lle_i_clock"] = lle_i_clock;
Chris Lattner665ee882002-03-08 22:51:07 +0000520 FuncNames["lle_X_fopen"] = lle_X_fopen;
521 FuncNames["lle_X_fclose"] = lle_X_fclose;
522 FuncNames["lle_X_fread"] = lle_X_fread;
523 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
524 FuncNames["lle_X_fgets"] = lle_X_fgets;
525 FuncNames["lle_X_fflush"] = lle_X_fflush;
Chris Lattner4721f132001-10-30 20:28:00 +0000526}