blob: 99b3fd6a61a6bd8af21a7d0656ca8b29e68793a3 [file] [log] [blame]
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001//===-- ExternalFunctions.cpp - Implement External Functions --------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmand1c881a2005-04-21 22:43:08 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00009//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000010// This file contains both code to deal with invoking "external" functions, but
11// also contains code that implements "exported" external functions.
Chris Lattner7720c8e2001-09-10 04:50:17 +000012//
Misha Brukmand1c881a2005-04-21 22:43:08 +000013// External functions in the interpreter are implemented by
Brian Gaeke58a6faa2003-10-10 17:03:10 +000014// using the system's dynamic loader to look up the address of the function
15// we want to invoke. If a function is found, then one of the
16// many lle_* wrapper functions in this file will translate its arguments from
17// GenericValues to the types the function is actually expecting, before the
18// function is called.
Chris Lattner7720c8e2001-09-10 04:50:17 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "Interpreter.h"
23#include "llvm/DerivedTypes.h"
Misha Brukmanb8d15b22003-10-14 21:42:11 +000024#include "llvm/Module.h"
Bill Wendling480f0932006-11-27 23:54:50 +000025#include "llvm/Support/Streams.h"
Reid Spencerdf5a37e2004-11-29 14:11:29 +000026#include "llvm/System/DynamicLibrary.h"
Chris Lattner005cbce2002-10-02 21:12:13 +000027#include "llvm/Target/TargetData.h"
Brian Gaekeb56a6bc2003-11-05 01:18:49 +000028#include <csignal>
Misha Brukmanb8d15b22003-10-14 21:42:11 +000029#include <map>
Jeff Cohen97af7512006-12-02 02:22:01 +000030#include <cmath>
Chris Lattner697954c2002-01-20 22:54:45 +000031using std::vector;
Chris Lattner7720c8e2001-09-10 04:50:17 +000032
Chris Lattnerf7a743d2003-12-14 23:25:48 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattnerb408b122002-03-29 03:57:15 +000035typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
36static std::map<const Function *, ExFunc> Functions;
Chris Lattner697954c2002-01-20 22:54:45 +000037static std::map<std::string, ExFunc> FuncNames;
Chris Lattner7720c8e2001-09-10 04:50:17 +000038
Chris Lattnere43db882001-10-27 04:15:57 +000039static Interpreter *TheInterpreter;
40
Chris Lattner7720c8e2001-09-10 04:50:17 +000041static char getTypeID(const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000042 switch (Ty->getTypeID()) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000043 case Type::VoidTyID: return 'V';
Reid Spencera54b7cb2007-01-12 07:05:14 +000044 case Type::IntegerTyID:
45 switch (cast<IntegerType>(Ty)->getBitWidth()) {
46 case 1: return 'o';
47 case 8: return 'B';
48 case 16: return 'S';
49 case 32: return 'I';
50 case 64: return 'L';
51 default: return 'N';
52 }
Chris Lattner7720c8e2001-09-10 04:50:17 +000053 case Type::FloatTyID: return 'F';
54 case Type::DoubleTyID: return 'D';
55 case Type::PointerTyID: return 'P';
Reid Spencere49661b2006-12-31 05:51:36 +000056 case Type::FunctionTyID:return 'M';
Chris Lattner7720c8e2001-09-10 04:50:17 +000057 case Type::StructTyID: return 'T';
58 case Type::ArrayTyID: return 'A';
59 case Type::OpaqueTyID: return 'O';
60 default: return 'U';
61 }
62}
63
Brian Gaeke58a6faa2003-10-10 17:03:10 +000064static ExFunc lookupFunction(const Function *F) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000065 // Function not found, look it up... start by figuring out what the
66 // composite function name should be.
Chris Lattner697954c2002-01-20 22:54:45 +000067 std::string ExtName = "lle_";
Brian Gaeke58a6faa2003-10-10 17:03:10 +000068 const FunctionType *FT = F->getFunctionType();
69 for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
70 ExtName += getTypeID(FT->getContainedType(i));
71 ExtName += "_" + F->getName();
Chris Lattner7720c8e2001-09-10 04:50:17 +000072
Chris Lattner4721f132001-10-30 20:28:00 +000073 ExFunc FnPtr = FuncNames[ExtName];
74 if (FnPtr == 0)
Chris Lattner26e6e102006-06-01 17:27:11 +000075 FnPtr =
76 (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(ExtName);
Chris Lattner4721f132001-10-30 20:28:00 +000077 if (FnPtr == 0)
Brian Gaeke58a6faa2003-10-10 17:03:10 +000078 FnPtr = FuncNames["lle_X_"+F->getName()];
Chris Lattner7720c8e2001-09-10 04:50:17 +000079 if (FnPtr == 0) // Try calling a generic function... if it exists...
Chris Lattner26e6e102006-06-01 17:27:11 +000080 FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(
Reid Spencerdf5a37e2004-11-29 14:11:29 +000081 ("lle_X_"+F->getName()).c_str());
Chris Lattner7720c8e2001-09-10 04:50:17 +000082 if (FnPtr != 0)
Brian Gaeke58a6faa2003-10-10 17:03:10 +000083 Functions.insert(std::make_pair(F, FnPtr)); // Cache for later
Chris Lattner7720c8e2001-09-10 04:50:17 +000084 return FnPtr;
85}
86
Chris Lattner4e7dd8f2005-01-21 19:59:37 +000087GenericValue Interpreter::callExternalFunction(Function *F,
Chris Lattner44edb6b2003-05-14 14:21:30 +000088 const std::vector<GenericValue> &ArgVals) {
Chris Lattnere43db882001-10-27 04:15:57 +000089 TheInterpreter = this;
90
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000091 // Do a lookup to see if the function is in our cache... this should just be a
Misha Brukmand5d96b92003-10-10 17:42:19 +000092 // deferred annotation!
Chris Lattner4e7dd8f2005-01-21 19:59:37 +000093 std::map<const Function *, ExFunc>::iterator FI = Functions.find(F);
94 ExFunc Fn = (FI == Functions.end()) ? lookupFunction(F) : FI->second;
Chris Lattner7720c8e2001-09-10 04:50:17 +000095 if (Fn == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000096 cerr << "Tried to execute an unknown external function: "
97 << F->getType()->getDescription() << " " << F->getName() << "\n";
Chris Lattner4e7dd8f2005-01-21 19:59:37 +000098 if (F->getName() == "__main")
99 return GenericValue();
100 abort();
Chris Lattner7720c8e2001-09-10 04:50:17 +0000101 }
102
103 // TODO: FIXME when types are not const!
Chris Lattner4e7dd8f2005-01-21 19:59:37 +0000104 GenericValue Result = Fn(const_cast<FunctionType*>(F->getFunctionType()),
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000105 ArgVals);
Chris Lattner4721f132001-10-30 20:28:00 +0000106 return Result;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000107}
108
109
110//===----------------------------------------------------------------------===//
Chris Lattnerb408b122002-03-29 03:57:15 +0000111// Functions "exported" to the running application...
Chris Lattner7720c8e2001-09-10 04:50:17 +0000112//
113extern "C" { // Don't add C++ manglings to llvm mangling :)
114
Chris Lattner005cbce2002-10-02 21:12:13 +0000115// void putchar(sbyte)
Reid Spencer97e0c222007-03-30 16:41:50 +0000116GenericValue lle_VB_putchar(FunctionType *FT, const vector<GenericValue> &Args){
Reid Spencerbfcd5992007-03-06 03:08:12 +0000117 cout << ((char)Args[0].IntVal.getZExtValue());
Chris Lattner7720c8e2001-09-10 04:50:17 +0000118 return GenericValue();
119}
120
Chris Lattner005cbce2002-10-02 21:12:13 +0000121// int putchar(int)
Reid Spencer97e0c222007-03-30 16:41:50 +0000122GenericValue lle_ii_putchar(FunctionType *FT, const vector<GenericValue> &Args){
Reid Spencerbfcd5992007-03-06 03:08:12 +0000123 cout << ((char)Args[0].IntVal.getZExtValue()) << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000124 return Args[0];
125}
126
Chris Lattner005cbce2002-10-02 21:12:13 +0000127// void putchar(ubyte)
Reid Spencer97e0c222007-03-30 16:41:50 +0000128GenericValue lle_Vb_putchar(FunctionType *FT, const vector<GenericValue> &Args){
Reid Spencerbfcd5992007-03-06 03:08:12 +0000129 cout << ((char)Args[0].IntVal.getZExtValue()) << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000130 return Args[0];
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000131}
132
Chris Lattner44edb6b2003-05-14 14:21:30 +0000133// void atexit(Function*)
Reid Spencer97e0c222007-03-30 16:41:50 +0000134GenericValue lle_X_atexit(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner44edb6b2003-05-14 14:21:30 +0000135 assert(Args.size() == 1);
136 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
137 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000138 GV.IntVal = 0;
Chris Lattner44edb6b2003-05-14 14:21:30 +0000139 return GV;
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000140}
141
Chris Lattner005cbce2002-10-02 21:12:13 +0000142// void exit(int)
Reid Spencer97e0c222007-03-30 16:41:50 +0000143GenericValue lle_X_exit(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnere43db882001-10-27 04:15:57 +0000144 TheInterpreter->exitCalled(Args[0]);
145 return GenericValue();
146}
147
Chris Lattner005cbce2002-10-02 21:12:13 +0000148// void abort(void)
Reid Spencer97e0c222007-03-30 16:41:50 +0000149GenericValue lle_X_abort(FunctionType *FT, const vector<GenericValue> &Args) {
Brian Gaekeb56a6bc2003-11-05 01:18:49 +0000150 raise (SIGABRT);
Chris Lattner1ee34a52002-05-20 21:17:16 +0000151 return GenericValue();
152}
153
Chris Lattnerc2593162001-10-27 08:28:11 +0000154// void *malloc(uint)
Reid Spencer97e0c222007-03-30 16:41:50 +0000155GenericValue lle_X_malloc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner4721f132001-10-30 20:28:00 +0000156 assert(Args.size() == 1 && "Malloc expects one argument!");
Reid Spencer97e0c222007-03-30 16:41:50 +0000157 assert(isa<PointerType>(FT->getReturnType()) && "malloc must return pointer");
Reid Spencerbfcd5992007-03-06 03:08:12 +0000158 return PTOGV(malloc(Args[0].IntVal.getZExtValue()));
Chris Lattnerc2593162001-10-27 08:28:11 +0000159}
160
Chris Lattner957d62a2003-04-23 19:55:24 +0000161// void *calloc(uint, uint)
Reid Spencer97e0c222007-03-30 16:41:50 +0000162GenericValue lle_X_calloc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000163 assert(Args.size() == 2 && "calloc expects two arguments!");
Reid Spencer97e0c222007-03-30 16:41:50 +0000164 assert(isa<PointerType>(FT->getReturnType()) && "calloc must return pointer");
Reid Spencerbfcd5992007-03-06 03:08:12 +0000165 return PTOGV(calloc(Args[0].IntVal.getZExtValue(),
166 Args[1].IntVal.getZExtValue()));
Chris Lattner957d62a2003-04-23 19:55:24 +0000167}
168
Reid Spencer97e0c222007-03-30 16:41:50 +0000169// void *calloc(uint, uint)
170GenericValue lle_X_realloc(FunctionType *FT, const vector<GenericValue> &Args) {
171 assert(Args.size() == 2 && "calloc expects two arguments!");
172 assert(isa<PointerType>(FT->getReturnType()) &&"realloc must return pointer");
173 return PTOGV(realloc(GVTOP(Args[0]), Args[1].IntVal.getZExtValue()));
174}
175
Chris Lattnerc2593162001-10-27 08:28:11 +0000176// void free(void *)
Reid Spencer97e0c222007-03-30 16:41:50 +0000177GenericValue lle_X_free(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000178 assert(Args.size() == 1);
Chris Lattnerb1118742003-01-13 00:59:47 +0000179 free(GVTOP(Args[0]));
Chris Lattnerc2593162001-10-27 08:28:11 +0000180 return GenericValue();
181}
182
Chris Lattner782b9392001-11-26 18:18:18 +0000183// int atoi(char *)
Reid Spencer97e0c222007-03-30 16:41:50 +0000184GenericValue lle_X_atoi(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000185 assert(Args.size() == 1);
186 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000187 GV.IntVal = APInt(32, atoi((char*)GVTOP(Args[0])));
Chris Lattner782b9392001-11-26 18:18:18 +0000188 return GV;
189}
190
Chris Lattnerc2593162001-10-27 08:28:11 +0000191// double pow(double, double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000192GenericValue lle_X_pow(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000193 assert(Args.size() == 2);
Chris Lattnerc2593162001-10-27 08:28:11 +0000194 GenericValue GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000195 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000196 return GV;
197}
198
Chris Lattner34dd24b2002-02-18 19:06:25 +0000199// double exp(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000200GenericValue lle_X_exp(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner34dd24b2002-02-18 19:06:25 +0000201 assert(Args.size() == 1);
202 GenericValue GV;
203 GV.DoubleVal = exp(Args[0].DoubleVal);
204 return GV;
205}
206
Chris Lattnerc063d382001-11-06 21:52:18 +0000207// double sqrt(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000208GenericValue lle_X_sqrt(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000209 assert(Args.size() == 1);
210 GenericValue GV;
211 GV.DoubleVal = sqrt(Args[0].DoubleVal);
212 return GV;
213}
Chris Lattnerc2593162001-10-27 08:28:11 +0000214
Chris Lattner86790052001-11-06 22:53:25 +0000215// double log(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000216GenericValue lle_X_log(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000217 assert(Args.size() == 1);
218 GenericValue GV;
219 GV.DoubleVal = log(Args[0].DoubleVal);
220 return GV;
221}
222
Chris Lattner782b9392001-11-26 18:18:18 +0000223// double floor(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000224GenericValue lle_X_floor(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000225 assert(Args.size() == 1);
226 GenericValue GV;
227 GV.DoubleVal = floor(Args[0].DoubleVal);
228 return GV;
229}
230
Reid Spencerabec8f92004-10-27 23:03:44 +0000231#ifdef HAVE_RAND48
232
Chris Lattner86790052001-11-06 22:53:25 +0000233// double drand48()
Reid Spencer97e0c222007-03-30 16:41:50 +0000234GenericValue lle_X_drand48(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000235 assert(Args.size() == 0);
236 GenericValue GV;
237 GV.DoubleVal = drand48();
238 return GV;
239}
240
Chris Lattner1b600142001-11-13 05:46:08 +0000241// long lrand48()
Reid Spencer97e0c222007-03-30 16:41:50 +0000242GenericValue lle_X_lrand48(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000243 assert(Args.size() == 0);
244 GenericValue GV;
Reid Spencere49661b2006-12-31 05:51:36 +0000245 GV.Int32Val = lrand48();
Chris Lattner1b600142001-11-13 05:46:08 +0000246 return GV;
247}
248
249// void srand48(long)
Reid Spencer97e0c222007-03-30 16:41:50 +0000250GenericValue lle_X_srand48(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000251 assert(Args.size() == 1);
Reid Spencere49661b2006-12-31 05:51:36 +0000252 srand48(Args[0].Int32Val);
Chris Lattner1b600142001-11-13 05:46:08 +0000253 return GenericValue();
254}
255
Reid Spencerabec8f92004-10-27 23:03:44 +0000256#endif
257
258// int rand()
Reid Spencer97e0c222007-03-30 16:41:50 +0000259GenericValue lle_X_rand(FunctionType *FT, const vector<GenericValue> &Args) {
Reid Spencerabec8f92004-10-27 23:03:44 +0000260 assert(Args.size() == 0);
261 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000262 GV.IntVal = APInt(32, rand());
Reid Spencerabec8f92004-10-27 23:03:44 +0000263 return GV;
264}
265
Chris Lattner782b9392001-11-26 18:18:18 +0000266// void srand(uint)
Reid Spencer97e0c222007-03-30 16:41:50 +0000267GenericValue lle_X_srand(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000268 assert(Args.size() == 1);
Reid Spencerbfcd5992007-03-06 03:08:12 +0000269 srand(Args[0].IntVal.getZExtValue());
Chris Lattner782b9392001-11-26 18:18:18 +0000270 return GenericValue();
271}
Chris Lattner86790052001-11-06 22:53:25 +0000272
Chris Lattnerb1118742003-01-13 00:59:47 +0000273// int puts(const char*)
Reid Spencer97e0c222007-03-30 16:41:50 +0000274GenericValue lle_X_puts(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerb1118742003-01-13 00:59:47 +0000275 assert(Args.size() == 1);
276 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000277 GV.IntVal = APInt(32, puts((char*)GVTOP(Args[0])));
Chris Lattnerb1118742003-01-13 00:59:47 +0000278 return GV;
279}
280
Chris Lattnere7c6f722001-12-13 00:43:47 +0000281// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
282// output useful.
Reid Spencer97e0c222007-03-30 16:41:50 +0000283GenericValue lle_X_sprintf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerb1118742003-01-13 00:59:47 +0000284 char *OutputBuffer = (char *)GVTOP(Args[0]);
285 const char *FmtStr = (const char *)GVTOP(Args[1]);
Chris Lattnere7c6f722001-12-13 00:43:47 +0000286 unsigned ArgNo = 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000287
288 // printf should return # chars printed. This is completely incorrect, but
289 // close enough for now.
Reid Spencerbfcd5992007-03-06 03:08:12 +0000290 GenericValue GV;
291 GV.IntVal = APInt(32, strlen(FmtStr));
Chris Lattner08845a22001-10-29 20:27:45 +0000292 while (1) {
293 switch (*FmtStr) {
294 case 0: return GV; // Null terminator...
295 default: // Normal nonspecial character
Chris Lattnere7c6f722001-12-13 00:43:47 +0000296 sprintf(OutputBuffer++, "%c", *FmtStr++);
Chris Lattner08845a22001-10-29 20:27:45 +0000297 break;
298 case '\\': { // Handle escape codes
Chris Lattnere7c6f722001-12-13 00:43:47 +0000299 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
300 FmtStr += 2; OutputBuffer += 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000301 break;
302 }
303 case '%': { // Handle format specifiers
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000304 char FmtBuf[100] = "", Buffer[1000] = "";
305 char *FB = FmtBuf;
306 *FB++ = *FmtStr++;
307 char Last = *FB++ = *FmtStr++;
308 unsigned HowLong = 0;
309 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
310 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
311 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
312 Last != 'p' && Last != 's' && Last != '%') {
313 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
314 Last = *FB++ = *FmtStr++;
Chris Lattner08845a22001-10-29 20:27:45 +0000315 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000316 *FB = 0;
Misha Brukmand1c881a2005-04-21 22:43:08 +0000317
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000318 switch (Last) {
319 case '%':
320 sprintf(Buffer, FmtBuf); break;
321 case 'c':
Reid Spencerbfcd5992007-03-06 03:08:12 +0000322 sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
323 break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000324 case 'd': case 'i':
325 case 'u': case 'o':
326 case 'x': case 'X':
Chris Lattner69ab7a82002-08-02 23:08:32 +0000327 if (HowLong >= 1) {
Chris Lattner1543e402003-08-24 14:02:47 +0000328 if (HowLong == 1 &&
Chris Lattnerfe854032006-08-16 01:24:12 +0000329 TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 &&
Reid Spencer19b7e0e2006-05-24 19:21:13 +0000330 sizeof(long) < sizeof(int64_t)) {
Chris Lattner69ab7a82002-08-02 23:08:32 +0000331 // Make sure we use %lld with a 64 bit argument because we might be
332 // compiling LLI on a 32 bit compiler.
333 unsigned Size = strlen(FmtBuf);
334 FmtBuf[Size] = FmtBuf[Size-1];
335 FmtBuf[Size+1] = 0;
336 FmtBuf[Size-1] = 'l';
337 }
Reid Spencerbfcd5992007-03-06 03:08:12 +0000338 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
Chris Lattner69ab7a82002-08-02 23:08:32 +0000339 } else
Reid Spencerbfcd5992007-03-06 03:08:12 +0000340 sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
341 break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000342 case 'e': case 'E': case 'g': case 'G': case 'f':
343 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
344 case 'p':
Chris Lattnerb1118742003-01-13 00:59:47 +0000345 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
Misha Brukmand1c881a2005-04-21 22:43:08 +0000346 case 's':
Chris Lattnerb1118742003-01-13 00:59:47 +0000347 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
Bill Wendlinge8156192006-12-07 01:30:32 +0000348 default: cerr << "<unknown printf code '" << *FmtStr << "'!>";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000349 ArgNo++; break;
Chris Lattner08845a22001-10-29 20:27:45 +0000350 }
Chris Lattnere7c6f722001-12-13 00:43:47 +0000351 strcpy(OutputBuffer, Buffer);
352 OutputBuffer += strlen(Buffer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000353 }
Chris Lattner08845a22001-10-29 20:27:45 +0000354 break;
355 }
Chris Lattner08845a22001-10-29 20:27:45 +0000356 }
357}
358
Chris Lattnere7c6f722001-12-13 00:43:47 +0000359// int printf(sbyte *, ...) - a very rough implementation to make output useful.
Reid Spencer97e0c222007-03-30 16:41:50 +0000360GenericValue lle_X_printf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000361 char Buffer[10000];
362 vector<GenericValue> NewArgs;
Chris Lattnerb1118742003-01-13 00:59:47 +0000363 NewArgs.push_back(PTOGV(Buffer));
Chris Lattnere7c6f722001-12-13 00:43:47 +0000364 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
Reid Spencer97e0c222007-03-30 16:41:50 +0000365 GenericValue GV = lle_X_sprintf(FT, NewArgs);
Bill Wendlinge8156192006-12-07 01:30:32 +0000366 cout << Buffer;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000367 return GV;
368}
369
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000370static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1,
371 void *Arg2, void *Arg3, void *Arg4, void *Arg5,
372 void *Arg6, void *Arg7, void *Arg8) {
373 void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 };
374
375 // Loop over the format string, munging read values as appropriate (performs
Misha Brukman5560c9d2003-08-18 14:43:39 +0000376 // byteswaps as necessary).
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000377 unsigned ArgNo = 0;
378 while (*Fmt) {
379 if (*Fmt++ == '%') {
380 // Read any flag characters that may be present...
381 bool Suppress = false;
382 bool Half = false;
383 bool Long = false;
384 bool LongLong = false; // long long or long double
385
386 while (1) {
387 switch (*Fmt++) {
388 case '*': Suppress = true; break;
389 case 'a': /*Allocate = true;*/ break; // We don't need to track this
390 case 'h': Half = true; break;
391 case 'l': Long = true; break;
392 case 'q':
393 case 'L': LongLong = true; break;
394 default:
395 if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs
396 goto Out;
397 }
398 }
399 Out:
400
401 // Read the conversion character
402 if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
403 unsigned Size = 0;
404 const Type *Ty = 0;
405
406 switch (Fmt[-1]) {
407 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
408 case 'd':
409 if (Long || LongLong) {
Reid Spencere49661b2006-12-31 05:51:36 +0000410 Size = 8; Ty = Type::Int64Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000411 } else if (Half) {
Reid Spencere49661b2006-12-31 05:51:36 +0000412 Size = 4; Ty = Type::Int16Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000413 } else {
Reid Spencere49661b2006-12-31 05:51:36 +0000414 Size = 4; Ty = Type::Int32Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000415 }
416 break;
417
418 case 'e': case 'g': case 'E':
419 case 'f':
420 if (Long || LongLong) {
421 Size = 8; Ty = Type::DoubleTy;
422 } else {
423 Size = 4; Ty = Type::FloatTy;
424 }
425 break;
426
427 case 's': case 'c': case '[': // No byteswap needed
428 Size = 1;
Reid Spencere49661b2006-12-31 05:51:36 +0000429 Ty = Type::Int8Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000430 break;
431
432 default: break;
433 }
434
435 if (Size) {
436 GenericValue GV;
437 void *Arg = Args[ArgNo++];
438 memcpy(&GV, Arg, Size);
439 TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
440 }
441 }
442 }
443 }
444}
445
Chris Lattner665ee882002-03-08 22:51:07 +0000446// int sscanf(const char *format, ...);
Reid Spencer97e0c222007-03-30 16:41:50 +0000447GenericValue lle_X_sscanf(FunctionType *FT, const vector<GenericValue> &args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000448 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
449
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000450 char *Args[10];
Chris Lattner665ee882002-03-08 22:51:07 +0000451 for (unsigned i = 0; i < args.size(); ++i)
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000452 Args[i] = (char*)GVTOP(args[i]);
Chris Lattner665ee882002-03-08 22:51:07 +0000453
454 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000455 GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
456 Args[5], Args[6], Args[7], Args[8], Args[9]));
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000457 ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4],
458 Args[5], Args[6], Args[7], Args[8], Args[9], 0);
459 return GV;
460}
461
462// int scanf(const char *format, ...);
Reid Spencer97e0c222007-03-30 16:41:50 +0000463GenericValue lle_X_scanf(FunctionType *FT, const vector<GenericValue> &args) {
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000464 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
465
466 char *Args[10];
467 for (unsigned i = 0; i < args.size(); ++i)
468 Args[i] = (char*)GVTOP(args[i]);
469
470 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000471 GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
472 Args[5], Args[6], Args[7], Args[8], Args[9]));
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000473 ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4],
474 Args[5], Args[6], Args[7], Args[8], Args[9]);
Chris Lattner665ee882002-03-08 22:51:07 +0000475 return GV;
476}
477
478
Chris Lattner295fe672002-01-23 21:38:07 +0000479// int clock(void) - Profiling implementation
Reid Spencer97e0c222007-03-30 16:41:50 +0000480GenericValue lle_i_clock(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner638559a2005-01-16 01:31:31 +0000481 extern unsigned int clock(void);
Reid Spencerbfcd5992007-03-06 03:08:12 +0000482 GenericValue GV;
483 GV.IntVal = APInt(32, clock());
Chris Lattner295fe672002-01-23 21:38:07 +0000484 return GV;
485}
Chris Lattnere7c6f722001-12-13 00:43:47 +0000486
Chris Lattner957d62a2003-04-23 19:55:24 +0000487
488//===----------------------------------------------------------------------===//
489// String Functions...
490//===----------------------------------------------------------------------===//
491
492// int strcmp(const char *S1, const char *S2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000493GenericValue lle_X_strcmp(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000494 assert(Args.size() == 2);
495 GenericValue Ret;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000496 Ret.IntVal = APInt(32, strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
Chris Lattner957d62a2003-04-23 19:55:24 +0000497 return Ret;
498}
499
500// char *strcat(char *Dest, const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000501GenericValue lle_X_strcat(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000502 assert(Args.size() == 2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000503 assert(isa<PointerType>(FT->getReturnType()) &&"strcat must return pointer");
Chris Lattner957d62a2003-04-23 19:55:24 +0000504 return PTOGV(strcat((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
505}
506
507// char *strcpy(char *Dest, const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000508GenericValue lle_X_strcpy(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000509 assert(Args.size() == 2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000510 assert(isa<PointerType>(FT->getReturnType()) &&"strcpy must return pointer");
Chris Lattner957d62a2003-04-23 19:55:24 +0000511 return PTOGV(strcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
512}
513
Brian Gaeke3c66c352004-05-01 06:42:15 +0000514static GenericValue size_t_to_GV (size_t n) {
515 GenericValue Ret;
516 if (sizeof (size_t) == sizeof (uint64_t)) {
Reid Spencerbfcd5992007-03-06 03:08:12 +0000517 Ret.IntVal = APInt(64, n);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000518 } else {
519 assert (sizeof (size_t) == sizeof (unsigned int));
Reid Spencerbfcd5992007-03-06 03:08:12 +0000520 Ret.IntVal = APInt(32, n);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000521 }
522 return Ret;
523}
524
Misha Brukmand1c881a2005-04-21 22:43:08 +0000525static size_t GV_to_size_t (GenericValue GV) {
Brian Gaeke3c66c352004-05-01 06:42:15 +0000526 size_t count;
527 if (sizeof (size_t) == sizeof (uint64_t)) {
Reid Spencerbfcd5992007-03-06 03:08:12 +0000528 count = (size_t)GV.IntVal.getZExtValue();
Brian Gaeke3c66c352004-05-01 06:42:15 +0000529 } else {
530 assert (sizeof (size_t) == sizeof (unsigned int));
Reid Spencerbfcd5992007-03-06 03:08:12 +0000531 count = (size_t)GV.IntVal.getZExtValue();
Brian Gaeke3c66c352004-05-01 06:42:15 +0000532 }
533 return count;
534}
535
Brian Gaeke6c9d5822003-12-12 15:38:06 +0000536// size_t strlen(const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000537GenericValue lle_X_strlen(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000538 assert(Args.size() == 1);
Brian Gaeke6c9d5822003-12-12 15:38:06 +0000539 size_t strlenResult = strlen ((char *) GVTOP (Args[0]));
Brian Gaeke3c66c352004-05-01 06:42:15 +0000540 return size_t_to_GV (strlenResult);
Chris Lattner957d62a2003-04-23 19:55:24 +0000541}
542
Brian Gaeke70975ee2003-09-05 18:42:01 +0000543// char *strdup(const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000544GenericValue lle_X_strdup(FunctionType *FT, const vector<GenericValue> &Args) {
Brian Gaeke70975ee2003-09-05 18:42:01 +0000545 assert(Args.size() == 1);
Reid Spencer97e0c222007-03-30 16:41:50 +0000546 assert(isa<PointerType>(FT->getReturnType()) && "strdup must return pointer");
Brian Gaeke70975ee2003-09-05 18:42:01 +0000547 return PTOGV(strdup((char*)GVTOP(Args[0])));
548}
549
Chris Lattnerc8cff9e2003-04-25 18:23:38 +0000550// char *__strdup(const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000551GenericValue lle_X___strdup(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc8cff9e2003-04-25 18:23:38 +0000552 assert(Args.size() == 1);
Reid Spencer97e0c222007-03-30 16:41:50 +0000553 assert(isa<PointerType>(FT->getReturnType()) &&"_strdup must return pointer");
Chris Lattnerc8cff9e2003-04-25 18:23:38 +0000554 return PTOGV(strdup((char*)GVTOP(Args[0])));
555}
556
Chris Lattner957d62a2003-04-23 19:55:24 +0000557// void *memset(void *S, int C, size_t N)
Reid Spencer97e0c222007-03-30 16:41:50 +0000558GenericValue lle_X_memset(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000559 assert(Args.size() == 3);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000560 size_t count = GV_to_size_t (Args[2]);
Reid Spencer97e0c222007-03-30 16:41:50 +0000561 assert(isa<PointerType>(FT->getReturnType()) && "memset must return pointer");
Reid Spencerbfcd5992007-03-06 03:08:12 +0000562 return PTOGV(memset(GVTOP(Args[0]), uint32_t(Args[1].IntVal.getZExtValue()),
563 count));
Chris Lattner957d62a2003-04-23 19:55:24 +0000564}
565
Chris Lattner5f311a72003-04-23 20:23:16 +0000566// void *memcpy(void *Dest, void *src, size_t Size);
Reid Spencer97e0c222007-03-30 16:41:50 +0000567GenericValue lle_X_memcpy(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner5f311a72003-04-23 20:23:16 +0000568 assert(Args.size() == 3);
Reid Spencer97e0c222007-03-30 16:41:50 +0000569 assert(isa<PointerType>(FT->getReturnType()) && "memcpy must return pointer");
Brian Gaeke3c66c352004-05-01 06:42:15 +0000570 size_t count = GV_to_size_t (Args[2]);
571 return PTOGV(memcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));
Chris Lattner5f311a72003-04-23 20:23:16 +0000572}
Chris Lattner957d62a2003-04-23 19:55:24 +0000573
Chris Lattner665ee882002-03-08 22:51:07 +0000574//===----------------------------------------------------------------------===//
575// IO Functions...
576//===----------------------------------------------------------------------===//
577
Chris Lattner005cbce2002-10-02 21:12:13 +0000578// getFILE - Turn a pointer in the host address space into a legit pointer in
Reid Spencer0098bdf2004-05-25 08:46:15 +0000579// the interpreter address space. This is an identity transformation.
580#define getFILE(ptr) ((FILE*)ptr)
Chris Lattner005cbce2002-10-02 21:12:13 +0000581
Chris Lattner665ee882002-03-08 22:51:07 +0000582// FILE *fopen(const char *filename, const char *mode);
Reid Spencer97e0c222007-03-30 16:41:50 +0000583GenericValue lle_X_fopen(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000584 assert(Args.size() == 2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000585 assert(isa<PointerType>(FT->getReturnType()) && "fopen must return pointer");
Chris Lattnerb1118742003-01-13 00:59:47 +0000586 return PTOGV(fopen((const char *)GVTOP(Args[0]),
Misha Brukman3c944972005-04-22 04:08:30 +0000587 (const char *)GVTOP(Args[1])));
Chris Lattner665ee882002-03-08 22:51:07 +0000588}
589
590// int fclose(FILE *F);
Reid Spencer97e0c222007-03-30 16:41:50 +0000591GenericValue lle_X_fclose(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000592 assert(Args.size() == 1);
593 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000594 GV.IntVal = APInt(32, fclose(getFILE(GVTOP(Args[0]))));
Chris Lattner665ee882002-03-08 22:51:07 +0000595 return GV;
596}
597
Chris Lattner25f6f372002-11-08 19:10:26 +0000598// int feof(FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000599GenericValue lle_X_feof(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner25f6f372002-11-08 19:10:26 +0000600 assert(Args.size() == 1);
601 GenericValue GV;
602
Reid Spencerbfcd5992007-03-06 03:08:12 +0000603 GV.IntVal = APInt(32, feof(getFILE(GVTOP(Args[0]))));
Chris Lattner25f6f372002-11-08 19:10:26 +0000604 return GV;
605}
606
Chris Lattner665ee882002-03-08 22:51:07 +0000607// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000608GenericValue lle_X_fread(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000609 assert(Args.size() == 4);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000610 size_t result;
Chris Lattner665ee882002-03-08 22:51:07 +0000611
Brian Gaeke3c66c352004-05-01 06:42:15 +0000612 result = fread((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
613 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
614 return size_t_to_GV (result);
Chris Lattner665ee882002-03-08 22:51:07 +0000615}
616
617// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000618GenericValue lle_X_fwrite(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000619 assert(Args.size() == 4);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000620 size_t result;
Chris Lattner665ee882002-03-08 22:51:07 +0000621
Brian Gaeke3c66c352004-05-01 06:42:15 +0000622 result = fwrite((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
623 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
624 return size_t_to_GV (result);
Chris Lattner665ee882002-03-08 22:51:07 +0000625}
626
627// char *fgets(char *s, int n, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000628GenericValue lle_X_fgets(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000629 assert(Args.size() == 3);
Reid Spencerbfcd5992007-03-06 03:08:12 +0000630 return GVTOP(fgets((char*)GVTOP(Args[0]), Args[1].IntVal.getZExtValue(),
Misha Brukman3c944972005-04-22 04:08:30 +0000631 getFILE(GVTOP(Args[2]))));
Chris Lattner665ee882002-03-08 22:51:07 +0000632}
633
Chris Lattnera4479cd2002-11-07 19:33:50 +0000634// FILE *freopen(const char *path, const char *mode, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000635GenericValue lle_X_freopen(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnera4479cd2002-11-07 19:33:50 +0000636 assert(Args.size() == 3);
Reid Spencer97e0c222007-03-30 16:41:50 +0000637 assert(isa<PointerType>(FT->getReturnType()) &&"freopen must return pointer");
Chris Lattnerb1118742003-01-13 00:59:47 +0000638 return PTOGV(freopen((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]),
Misha Brukman3c944972005-04-22 04:08:30 +0000639 getFILE(GVTOP(Args[2]))));
Chris Lattnera4479cd2002-11-07 19:33:50 +0000640}
641
Chris Lattner665ee882002-03-08 22:51:07 +0000642// int fflush(FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000643GenericValue lle_X_fflush(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000644 assert(Args.size() == 1);
645 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000646 GV.IntVal = APInt(32, fflush(getFILE(GVTOP(Args[0]))));
Chris Lattner005cbce2002-10-02 21:12:13 +0000647 return GV;
648}
649
650// int getc(FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000651GenericValue lle_X_getc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner005cbce2002-10-02 21:12:13 +0000652 assert(Args.size() == 1);
653 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000654 GV.IntVal = APInt(32, getc(getFILE(GVTOP(Args[0]))));
Chris Lattner665ee882002-03-08 22:51:07 +0000655 return GV;
656}
657
Chris Lattnerf87a1982003-04-23 19:20:50 +0000658// int _IO_getc(FILE *stream);
659GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) {
660 return lle_X_getc(F, Args);
661}
662
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000663// int fputc(int C, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000664GenericValue lle_X_fputc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000665 assert(Args.size() == 2);
666 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000667 GV.IntVal = APInt(32, fputc(Args[0].IntVal.getZExtValue(),
668 getFILE(GVTOP(Args[1]))));
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000669 return GV;
670}
671
672// int ungetc(int C, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000673GenericValue lle_X_ungetc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000674 assert(Args.size() == 2);
675 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000676 GV.IntVal = APInt(32, ungetc(Args[0].IntVal.getZExtValue(),
677 getFILE(GVTOP(Args[1]))));
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000678 return GV;
679}
680
Brian Gaeke59108d32004-06-16 02:56:40 +0000681// int ferror (FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000682GenericValue lle_X_ferror(FunctionType *FT, const vector<GenericValue> &Args) {
Brian Gaeke59108d32004-06-16 02:56:40 +0000683 assert(Args.size() == 1);
684 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000685 GV.IntVal = APInt(32, ferror (getFILE(GVTOP(Args[0]))));
Brian Gaeke59108d32004-06-16 02:56:40 +0000686 return GV;
687}
688
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000689// int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
690// useful.
Reid Spencer97e0c222007-03-30 16:41:50 +0000691GenericValue lle_X_fprintf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner9dbf6dd2003-04-21 22:43:20 +0000692 assert(Args.size() >= 2);
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000693 char Buffer[10000];
694 vector<GenericValue> NewArgs;
Chris Lattnerb1118742003-01-13 00:59:47 +0000695 NewArgs.push_back(PTOGV(Buffer));
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000696 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
Reid Spencer97e0c222007-03-30 16:41:50 +0000697 GenericValue GV = lle_X_sprintf(FT, NewArgs);
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000698
Chris Lattnerb1118742003-01-13 00:59:47 +0000699 fputs(Buffer, getFILE(GVTOP(Args[0])));
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000700 return GV;
701}
702
Chris Lattner7720c8e2001-09-10 04:50:17 +0000703} // End extern "C"
Chris Lattner4721f132001-10-30 20:28:00 +0000704
705
Chris Lattnerda82ed52003-05-08 16:18:31 +0000706void Interpreter::initializeExternalFunctions() {
Chris Lattner0f279b22001-11-03 10:15:32 +0000707 FuncNames["lle_Vb_putchar"] = lle_Vb_putchar;
708 FuncNames["lle_ii_putchar"] = lle_ii_putchar;
709 FuncNames["lle_VB_putchar"] = lle_VB_putchar;
Chris Lattner0f279b22001-11-03 10:15:32 +0000710 FuncNames["lle_X_exit"] = lle_X_exit;
Chris Lattner1ee34a52002-05-20 21:17:16 +0000711 FuncNames["lle_X_abort"] = lle_X_abort;
Chris Lattner0f279b22001-11-03 10:15:32 +0000712 FuncNames["lle_X_malloc"] = lle_X_malloc;
Chris Lattner957d62a2003-04-23 19:55:24 +0000713 FuncNames["lle_X_calloc"] = lle_X_calloc;
Reid Spencer97e0c222007-03-30 16:41:50 +0000714 FuncNames["lle_X_realloc"] = lle_X_realloc;
Chris Lattner0f279b22001-11-03 10:15:32 +0000715 FuncNames["lle_X_free"] = lle_X_free;
Chris Lattner782b9392001-11-26 18:18:18 +0000716 FuncNames["lle_X_atoi"] = lle_X_atoi;
Chris Lattner0f279b22001-11-03 10:15:32 +0000717 FuncNames["lle_X_pow"] = lle_X_pow;
Chris Lattner34dd24b2002-02-18 19:06:25 +0000718 FuncNames["lle_X_exp"] = lle_X_exp;
Chris Lattner1b600142001-11-13 05:46:08 +0000719 FuncNames["lle_X_log"] = lle_X_log;
Chris Lattner782b9392001-11-26 18:18:18 +0000720 FuncNames["lle_X_floor"] = lle_X_floor;
721 FuncNames["lle_X_srand"] = lle_X_srand;
Reid Spencerabec8f92004-10-27 23:03:44 +0000722 FuncNames["lle_X_rand"] = lle_X_rand;
723#ifdef HAVE_RAND48
Chris Lattner1b600142001-11-13 05:46:08 +0000724 FuncNames["lle_X_drand48"] = lle_X_drand48;
725 FuncNames["lle_X_srand48"] = lle_X_srand48;
726 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
Reid Spencerabec8f92004-10-27 23:03:44 +0000727#endif
Chris Lattnerc063d382001-11-06 21:52:18 +0000728 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
Chris Lattnerb1118742003-01-13 00:59:47 +0000729 FuncNames["lle_X_puts"] = lle_X_puts;
Chris Lattner0f279b22001-11-03 10:15:32 +0000730 FuncNames["lle_X_printf"] = lle_X_printf;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000731 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
Chris Lattner665ee882002-03-08 22:51:07 +0000732 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000733 FuncNames["lle_X_scanf"] = lle_X_scanf;
Chris Lattner295fe672002-01-23 21:38:07 +0000734 FuncNames["lle_i_clock"] = lle_i_clock;
Chris Lattner957d62a2003-04-23 19:55:24 +0000735
736 FuncNames["lle_X_strcmp"] = lle_X_strcmp;
737 FuncNames["lle_X_strcat"] = lle_X_strcat;
738 FuncNames["lle_X_strcpy"] = lle_X_strcpy;
739 FuncNames["lle_X_strlen"] = lle_X_strlen;
Chris Lattnerda82ed52003-05-08 16:18:31 +0000740 FuncNames["lle_X___strdup"] = lle_X___strdup;
Chris Lattner957d62a2003-04-23 19:55:24 +0000741 FuncNames["lle_X_memset"] = lle_X_memset;
Chris Lattner5f311a72003-04-23 20:23:16 +0000742 FuncNames["lle_X_memcpy"] = lle_X_memcpy;
Chris Lattner957d62a2003-04-23 19:55:24 +0000743
Chris Lattner665ee882002-03-08 22:51:07 +0000744 FuncNames["lle_X_fopen"] = lle_X_fopen;
745 FuncNames["lle_X_fclose"] = lle_X_fclose;
Chris Lattner25f6f372002-11-08 19:10:26 +0000746 FuncNames["lle_X_feof"] = lle_X_feof;
Chris Lattner665ee882002-03-08 22:51:07 +0000747 FuncNames["lle_X_fread"] = lle_X_fread;
748 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
749 FuncNames["lle_X_fgets"] = lle_X_fgets;
750 FuncNames["lle_X_fflush"] = lle_X_fflush;
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000751 FuncNames["lle_X_fgetc"] = lle_X_getc;
Chris Lattner005cbce2002-10-02 21:12:13 +0000752 FuncNames["lle_X_getc"] = lle_X_getc;
Chris Lattnerf87a1982003-04-23 19:20:50 +0000753 FuncNames["lle_X__IO_getc"] = lle_X__IO_getc;
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000754 FuncNames["lle_X_fputc"] = lle_X_fputc;
755 FuncNames["lle_X_ungetc"] = lle_X_ungetc;
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000756 FuncNames["lle_X_fprintf"] = lle_X_fprintf;
Chris Lattnera4479cd2002-11-07 19:33:50 +0000757 FuncNames["lle_X_freopen"] = lle_X_freopen;
Chris Lattner4721f132001-10-30 20:28:00 +0000758}
Brian Gaeked0fde302003-11-11 22:41:34 +0000759