blob: a16f31718e63c4d611ea3a842971110f65801739 [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 Lattner2e42d3a2001-10-15 05:51:48 +0000138 if (PointerType *PTy = dyn_cast<PointerType>(M->getParamTypes()[0].get()))
Chris Lattner7a176752001-12-04 00:03:30 +0000139 if (PTy->getElementType() == Type::SByteTy ||
140 isa<ArrayType>(PTy->getElementType())) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000141 return lle_VP_printstr(M, ArgVal);
142 }
143
144 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);
Chris Lattner7720c8e2001-09-10 04:50:17 +0000145 return GenericValue();
146}
147
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000148// Implement 'void printString(X)'
149// Argument must be [ubyte {x N} ] * or sbyte *
Chris Lattnerb408b122002-03-29 03:57:15 +0000150GenericValue lle_X_printString(FunctionType *M, const vector<GenericValue> &ArgVal) {
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000151 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
152 return lle_VP_printstr(M, ArgVal);
153}
154
155// Implement 'void print<TYPE>(X)' for each primitive type or pointer type
156#define PRINT_TYPE_FUNC(TYPENAME,TYPEID) \
Chris Lattnerb408b122002-03-29 03:57:15 +0000157 GenericValue lle_X_print##TYPENAME(FunctionType *M,\
Chris Lattner316a65b2001-10-28 22:38:22 +0000158 const vector<GenericValue> &ArgVal) {\
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000159 assert(ArgVal.size() == 1 && "generic print only takes one argument!");\
Chris Lattner69b5ce92001-11-26 19:19:27 +0000160 assert(M->getParamTypes()[0].get()->getPrimitiveID() == Type::TYPEID);\
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000161 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);\
162 return GenericValue();\
163 }
164
Chris Lattner4721f132001-10-30 20:28:00 +0000165PRINT_TYPE_FUNC(SByte, SByteTyID)
Vikram S. Adve3eb60f82001-10-28 20:52:27 +0000166PRINT_TYPE_FUNC(UByte, UByteTyID)
167PRINT_TYPE_FUNC(Short, ShortTyID)
168PRINT_TYPE_FUNC(UShort, UShortTyID)
169PRINT_TYPE_FUNC(Int, IntTyID)
170PRINT_TYPE_FUNC(UInt, UIntTyID)
171PRINT_TYPE_FUNC(Long, LongTyID)
172PRINT_TYPE_FUNC(ULong, ULongTyID)
173PRINT_TYPE_FUNC(Float, FloatTyID)
174PRINT_TYPE_FUNC(Double, DoubleTyID)
175PRINT_TYPE_FUNC(Pointer, PointerTyID)
176
177
Chris Lattner7720c8e2001-09-10 04:50:17 +0000178// void "putchar"(sbyte)
Chris Lattnerb408b122002-03-29 03:57:15 +0000179GenericValue lle_Vb_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner7720c8e2001-09-10 04:50:17 +0000180 cout << Args[0].SByteVal;
181 return GenericValue();
182}
183
Chris Lattnere43db882001-10-27 04:15:57 +0000184// int "putchar"(int)
Chris Lattnerb408b122002-03-29 03:57:15 +0000185GenericValue lle_ii_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner697954c2002-01-20 22:54:45 +0000186 cout << ((char)Args[0].IntVal) << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000187 return Args[0];
188}
189
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000190// void "putchar"(ubyte)
Chris Lattnerb408b122002-03-29 03:57:15 +0000191GenericValue lle_VB_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner697954c2002-01-20 22:54:45 +0000192 cout << Args[0].SByteVal << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000193 return Args[0];
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000194}
195
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000196// void "__main"()
Chris Lattnerb408b122002-03-29 03:57:15 +0000197GenericValue lle_V___main(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000198 return GenericValue();
199}
200
Chris Lattnere43db882001-10-27 04:15:57 +0000201// void "exit"(int)
Chris Lattnerb408b122002-03-29 03:57:15 +0000202GenericValue lle_X_exit(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere43db882001-10-27 04:15:57 +0000203 TheInterpreter->exitCalled(Args[0]);
204 return GenericValue();
205}
206
Chris Lattner1ee34a52002-05-20 21:17:16 +0000207// void "abort"(void)
208GenericValue lle_X_abort(FunctionType *M, const vector<GenericValue> &Args) {
209 std::cerr << "***PROGRAM ABORTED***!\n";
210 GenericValue GV;
211 GV.IntVal = 1;
212 TheInterpreter->exitCalled(GV);
213 return GenericValue();
214}
215
Chris Lattnerc2593162001-10-27 08:28:11 +0000216// void *malloc(uint)
Chris Lattnerb408b122002-03-29 03:57:15 +0000217GenericValue lle_X_malloc(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner4721f132001-10-30 20:28:00 +0000218 assert(Args.size() == 1 && "Malloc expects one argument!");
Chris Lattnerc2593162001-10-27 08:28:11 +0000219 GenericValue GV;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000220 GV.PointerVal = (PointerTy)malloc(Args[0].UIntVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000221 return GV;
222}
223
224// void free(void *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000225GenericValue lle_X_free(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000226 assert(Args.size() == 1);
Chris Lattner4721f132001-10-30 20:28:00 +0000227 free((void*)Args[0].PointerVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000228 return GenericValue();
229}
230
Chris Lattner782b9392001-11-26 18:18:18 +0000231// int atoi(char *)
Chris Lattnerb408b122002-03-29 03:57:15 +0000232GenericValue lle_X_atoi(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000233 assert(Args.size() == 1);
234 GenericValue GV;
235 GV.IntVal = atoi((char*)Args[0].PointerVal);
236 return GV;
237}
238
Chris Lattnerc2593162001-10-27 08:28:11 +0000239// double pow(double, double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000240GenericValue lle_X_pow(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000241 assert(Args.size() == 2);
Chris Lattnerc2593162001-10-27 08:28:11 +0000242 GenericValue GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000243 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000244 return GV;
245}
246
Chris Lattner34dd24b2002-02-18 19:06:25 +0000247// double exp(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000248GenericValue lle_X_exp(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner34dd24b2002-02-18 19:06:25 +0000249 assert(Args.size() == 1);
250 GenericValue GV;
251 GV.DoubleVal = exp(Args[0].DoubleVal);
252 return GV;
253}
254
Chris Lattnerc063d382001-11-06 21:52:18 +0000255// double sqrt(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000256GenericValue lle_X_sqrt(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000257 assert(Args.size() == 1);
258 GenericValue GV;
259 GV.DoubleVal = sqrt(Args[0].DoubleVal);
260 return GV;
261}
Chris Lattnerc2593162001-10-27 08:28:11 +0000262
Chris Lattner86790052001-11-06 22:53:25 +0000263// double log(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000264GenericValue lle_X_log(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000265 assert(Args.size() == 1);
266 GenericValue GV;
267 GV.DoubleVal = log(Args[0].DoubleVal);
268 return GV;
269}
270
Chris Lattner782b9392001-11-26 18:18:18 +0000271// double floor(double)
Chris Lattnerb408b122002-03-29 03:57:15 +0000272GenericValue lle_X_floor(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000273 assert(Args.size() == 1);
274 GenericValue GV;
275 GV.DoubleVal = floor(Args[0].DoubleVal);
276 return GV;
277}
278
Chris Lattner86790052001-11-06 22:53:25 +0000279// double drand48()
Chris Lattnerb408b122002-03-29 03:57:15 +0000280GenericValue lle_X_drand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000281 assert(Args.size() == 0);
282 GenericValue GV;
283 GV.DoubleVal = drand48();
284 return GV;
285}
286
Chris Lattner1b600142001-11-13 05:46:08 +0000287// long lrand48()
Chris Lattnerb408b122002-03-29 03:57:15 +0000288GenericValue lle_X_lrand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000289 assert(Args.size() == 0);
290 GenericValue GV;
291 GV.IntVal = lrand48();
292 return GV;
293}
294
295// void srand48(long)
Chris Lattnerb408b122002-03-29 03:57:15 +0000296GenericValue lle_X_srand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000297 assert(Args.size() == 1);
298 srand48(Args[0].IntVal);
299 return GenericValue();
300}
301
Chris Lattner782b9392001-11-26 18:18:18 +0000302// void srand(uint)
Chris Lattnerb408b122002-03-29 03:57:15 +0000303GenericValue lle_X_srand(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000304 assert(Args.size() == 1);
305 srand(Args[0].UIntVal);
306 return GenericValue();
307}
Chris Lattner86790052001-11-06 22:53:25 +0000308
Chris Lattnere7c6f722001-12-13 00:43:47 +0000309// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
310// output useful.
Chris Lattnerb408b122002-03-29 03:57:15 +0000311GenericValue lle_X_sprintf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000312 char *OutputBuffer = (char *)Args[0].PointerVal;
313 const char *FmtStr = (const char *)Args[1].PointerVal;
314 unsigned ArgNo = 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000315
316 // printf should return # chars printed. This is completely incorrect, but
317 // close enough for now.
318 GenericValue GV; GV.IntVal = strlen(FmtStr);
319 while (1) {
320 switch (*FmtStr) {
321 case 0: return GV; // Null terminator...
322 default: // Normal nonspecial character
Chris Lattnere7c6f722001-12-13 00:43:47 +0000323 sprintf(OutputBuffer++, "%c", *FmtStr++);
Chris Lattner08845a22001-10-29 20:27:45 +0000324 break;
325 case '\\': { // Handle escape codes
Chris Lattnere7c6f722001-12-13 00:43:47 +0000326 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
327 FmtStr += 2; OutputBuffer += 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000328 break;
329 }
330 case '%': { // Handle format specifiers
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000331 char FmtBuf[100] = "", Buffer[1000] = "";
332 char *FB = FmtBuf;
333 *FB++ = *FmtStr++;
334 char Last = *FB++ = *FmtStr++;
335 unsigned HowLong = 0;
336 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
337 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
338 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
339 Last != 'p' && Last != 's' && Last != '%') {
340 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
341 Last = *FB++ = *FmtStr++;
Chris Lattner08845a22001-10-29 20:27:45 +0000342 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000343 *FB = 0;
344
345 switch (Last) {
346 case '%':
347 sprintf(Buffer, FmtBuf); break;
348 case 'c':
Chris Lattner2012d5e2002-04-17 17:43:01 +0000349 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000350 case 'd': case 'i':
351 case 'u': case 'o':
352 case 'x': case 'X':
353 if (HowLong == 2)
354 sprintf(Buffer, FmtBuf, Args[ArgNo++].ULongVal);
355 else
356 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
357 case 'e': case 'E': case 'g': case 'G': case 'f':
358 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
359 case 'p':
360 sprintf(Buffer, FmtBuf, (void*)Args[ArgNo++].PointerVal); break;
361 case 's':
362 sprintf(Buffer, FmtBuf, (char*)Args[ArgNo++].PointerVal); break;
363 default: cout << "<unknown printf code '" << *FmtStr << "'!>";
364 ArgNo++; break;
Chris Lattner08845a22001-10-29 20:27:45 +0000365 }
Chris Lattnere7c6f722001-12-13 00:43:47 +0000366 strcpy(OutputBuffer, Buffer);
367 OutputBuffer += strlen(Buffer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000368 }
Chris Lattner08845a22001-10-29 20:27:45 +0000369 break;
370 }
Chris Lattner08845a22001-10-29 20:27:45 +0000371 }
372}
373
Chris Lattnere7c6f722001-12-13 00:43:47 +0000374// int printf(sbyte *, ...) - a very rough implementation to make output useful.
Chris Lattnerb408b122002-03-29 03:57:15 +0000375GenericValue lle_X_printf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000376 char Buffer[10000];
377 vector<GenericValue> NewArgs;
378 GenericValue GV; GV.PointerVal = (PointerTy)Buffer;
379 NewArgs.push_back(GV);
380 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
381 GV = lle_X_sprintf(M, NewArgs);
382 cout << Buffer;
383 return GV;
384}
385
Chris Lattner665ee882002-03-08 22:51:07 +0000386// int sscanf(const char *format, ...);
Chris Lattnerb408b122002-03-29 03:57:15 +0000387GenericValue lle_X_sscanf(FunctionType *M, const vector<GenericValue> &args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000388 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
389
390 const char *Args[10];
391 for (unsigned i = 0; i < args.size(); ++i)
392 Args[i] = (const char*)args[i].PointerVal;
393
394 GenericValue GV;
395 GV.IntVal = sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
396 Args[5], Args[6], Args[7], Args[8], Args[9]);
397 return GV;
398}
399
400
Chris Lattner295fe672002-01-23 21:38:07 +0000401// int clock(void) - Profiling implementation
Chris Lattnerb408b122002-03-29 03:57:15 +0000402GenericValue lle_i_clock(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner295fe672002-01-23 21:38:07 +0000403 extern int clock(void);
404 GenericValue GV; GV.IntVal = clock();
405 return GV;
406}
Chris Lattnere7c6f722001-12-13 00:43:47 +0000407
Chris Lattner665ee882002-03-08 22:51:07 +0000408//===----------------------------------------------------------------------===//
409// IO Functions...
410//===----------------------------------------------------------------------===//
411
412// FILE *fopen(const char *filename, const char *mode);
Chris Lattnerb408b122002-03-29 03:57:15 +0000413GenericValue lle_X_fopen(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000414 assert(Args.size() == 2);
415 GenericValue GV;
416
417 GV.PointerVal = (PointerTy)fopen((const char *)Args[0].PointerVal,
418 (const char *)Args[1].PointerVal);
419 return GV;
420}
421
422// int fclose(FILE *F);
Chris Lattnerb408b122002-03-29 03:57:15 +0000423GenericValue lle_X_fclose(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000424 assert(Args.size() == 1);
425 GenericValue GV;
426
427 GV.IntVal = fclose((FILE *)Args[0].PointerVal);
428 return GV;
429}
430
431// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000432GenericValue lle_X_fread(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 = fread((void*)Args[0].PointerVal, Args[1].UIntVal,
437 Args[2].UIntVal, (FILE*)Args[3].PointerVal);
438 return GV;
439}
440
441// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000442GenericValue lle_X_fwrite(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000443 assert(Args.size() == 4);
444 GenericValue GV;
445
446 GV.UIntVal = fwrite((void*)Args[0].PointerVal, Args[1].UIntVal,
447 Args[2].UIntVal, (FILE*)Args[3].PointerVal);
448 return GV;
449}
450
451// char *fgets(char *s, int n, FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000452GenericValue lle_X_fgets(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000453 assert(Args.size() == 3);
454 GenericValue GV;
455
456 GV.PointerVal = (PointerTy)fgets((char*)Args[0].PointerVal, Args[1].IntVal,
457 (FILE*)Args[2].PointerVal);
458 return GV;
459}
460
461// int fflush(FILE *stream);
Chris Lattnerb408b122002-03-29 03:57:15 +0000462GenericValue lle_X_fflush(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000463 assert(Args.size() == 1);
464 GenericValue GV;
465
466 GV.IntVal = fflush((FILE*)Args[0].PointerVal);
467 return GV;
468}
469
Chris Lattner7720c8e2001-09-10 04:50:17 +0000470} // End extern "C"
Chris Lattner4721f132001-10-30 20:28:00 +0000471
472
473void Interpreter::initializeExternalMethods() {
474 FuncNames["lle_VP_printstr"] = lle_VP_printstr;
475 FuncNames["lle_X_print"] = lle_X_print;
476 FuncNames["lle_X_printVal"] = lle_X_printVal;
477 FuncNames["lle_X_printString"] = lle_X_printString;
478 FuncNames["lle_X_printUByte"] = lle_X_printUByte;
479 FuncNames["lle_X_printSByte"] = lle_X_printSByte;
480 FuncNames["lle_X_printUShort"] = lle_X_printUShort;
481 FuncNames["lle_X_printShort"] = lle_X_printShort;
482 FuncNames["lle_X_printInt"] = lle_X_printInt;
483 FuncNames["lle_X_printUInt"] = lle_X_printUInt;
484 FuncNames["lle_X_printLong"] = lle_X_printLong;
485 FuncNames["lle_X_printULong"] = lle_X_printULong;
486 FuncNames["lle_X_printFloat"] = lle_X_printFloat;
487 FuncNames["lle_X_printDouble"] = lle_X_printDouble;
488 FuncNames["lle_X_printPointer"] = lle_X_printPointer;
Chris Lattner0f279b22001-11-03 10:15:32 +0000489 FuncNames["lle_Vb_putchar"] = lle_Vb_putchar;
490 FuncNames["lle_ii_putchar"] = lle_ii_putchar;
491 FuncNames["lle_VB_putchar"] = lle_VB_putchar;
492 FuncNames["lle_V___main"] = lle_V___main;
493 FuncNames["lle_X_exit"] = lle_X_exit;
Chris Lattner1ee34a52002-05-20 21:17:16 +0000494 FuncNames["lle_X_abort"] = lle_X_abort;
Chris Lattner0f279b22001-11-03 10:15:32 +0000495 FuncNames["lle_X_malloc"] = lle_X_malloc;
496 FuncNames["lle_X_free"] = lle_X_free;
Chris Lattner782b9392001-11-26 18:18:18 +0000497 FuncNames["lle_X_atoi"] = lle_X_atoi;
Chris Lattner0f279b22001-11-03 10:15:32 +0000498 FuncNames["lle_X_pow"] = lle_X_pow;
Chris Lattner34dd24b2002-02-18 19:06:25 +0000499 FuncNames["lle_X_exp"] = lle_X_exp;
Chris Lattner1b600142001-11-13 05:46:08 +0000500 FuncNames["lle_X_log"] = lle_X_log;
Chris Lattner782b9392001-11-26 18:18:18 +0000501 FuncNames["lle_X_floor"] = lle_X_floor;
502 FuncNames["lle_X_srand"] = lle_X_srand;
Chris Lattner1b600142001-11-13 05:46:08 +0000503 FuncNames["lle_X_drand48"] = lle_X_drand48;
504 FuncNames["lle_X_srand48"] = lle_X_srand48;
505 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
Chris Lattnerc063d382001-11-06 21:52:18 +0000506 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
Chris Lattner0f279b22001-11-03 10:15:32 +0000507 FuncNames["lle_X_printf"] = lle_X_printf;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000508 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
Chris Lattner665ee882002-03-08 22:51:07 +0000509 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
Chris Lattner295fe672002-01-23 21:38:07 +0000510 FuncNames["lle_i_clock"] = lle_i_clock;
Chris Lattner665ee882002-03-08 22:51:07 +0000511 FuncNames["lle_X_fopen"] = lle_X_fopen;
512 FuncNames["lle_X_fclose"] = lle_X_fclose;
513 FuncNames["lle_X_fread"] = lle_X_fread;
514 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
515 FuncNames["lle_X_fgets"] = lle_X_fgets;
516 FuncNames["lle_X_fflush"] = lle_X_fflush;
Chris Lattner4721f132001-10-30 20:28:00 +0000517}