blob: 12bfb8e252c7726713be2a13f14b85680f4958f0 [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"
Chuck Rose III936baaa2007-07-27 18:26:35 +000028#include "llvm/Support/ManagedStatic.h"
Brian Gaekeb56a6bc2003-11-05 01:18:49 +000029#include <csignal>
Misha Brukmanb8d15b22003-10-14 21:42:11 +000030#include <map>
Jeff Cohen97af7512006-12-02 02:22:01 +000031#include <cmath>
Zhou Sheng6a7951c2007-12-12 04:55:43 +000032#include <cxxabi.h>
Chris Lattner697954c2002-01-20 22:54:45 +000033using std::vector;
Chris Lattner7720c8e2001-09-10 04:50:17 +000034
Chris Lattnerf7a743d2003-12-14 23:25:48 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattnerb408b122002-03-29 03:57:15 +000037typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
Chuck Rose III936baaa2007-07-27 18:26:35 +000038static ManagedStatic<std::map<const Function *, ExFunc> > Functions;
Chris Lattner697954c2002-01-20 22:54:45 +000039static std::map<std::string, ExFunc> FuncNames;
Chris Lattner7720c8e2001-09-10 04:50:17 +000040
Chris Lattnere43db882001-10-27 04:15:57 +000041static Interpreter *TheInterpreter;
42
Chris Lattner7720c8e2001-09-10 04:50:17 +000043static char getTypeID(const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000044 switch (Ty->getTypeID()) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000045 case Type::VoidTyID: return 'V';
Reid Spencera54b7cb2007-01-12 07:05:14 +000046 case Type::IntegerTyID:
47 switch (cast<IntegerType>(Ty)->getBitWidth()) {
48 case 1: return 'o';
49 case 8: return 'B';
50 case 16: return 'S';
51 case 32: return 'I';
52 case 64: return 'L';
53 default: return 'N';
54 }
Chris Lattner7720c8e2001-09-10 04:50:17 +000055 case Type::FloatTyID: return 'F';
56 case Type::DoubleTyID: return 'D';
57 case Type::PointerTyID: return 'P';
Reid Spencere49661b2006-12-31 05:51:36 +000058 case Type::FunctionTyID:return 'M';
Chris Lattner7720c8e2001-09-10 04:50:17 +000059 case Type::StructTyID: return 'T';
60 case Type::ArrayTyID: return 'A';
61 case Type::OpaqueTyID: return 'O';
62 default: return 'U';
63 }
64}
65
Anton Korobeynikov42346f52007-07-30 23:03:25 +000066// Try to find address of external function given a Function object.
67// Please note, that interpreter doesn't know how to assemble a
68// real call in general case (this is JIT job), that's why it assumes,
69// that all external functions has the same (and pretty "general") signature.
70// The typical example of such functions are "lle_X_" ones.
Brian Gaeke58a6faa2003-10-10 17:03:10 +000071static ExFunc lookupFunction(const Function *F) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000072 // Function not found, look it up... start by figuring out what the
73 // composite function name should be.
Chris Lattner697954c2002-01-20 22:54:45 +000074 std::string ExtName = "lle_";
Brian Gaeke58a6faa2003-10-10 17:03:10 +000075 const FunctionType *FT = F->getFunctionType();
76 for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
77 ExtName += getTypeID(FT->getContainedType(i));
78 ExtName += "_" + F->getName();
Chris Lattner7720c8e2001-09-10 04:50:17 +000079
Chris Lattner4721f132001-10-30 20:28:00 +000080 ExFunc FnPtr = FuncNames[ExtName];
81 if (FnPtr == 0)
Brian Gaeke58a6faa2003-10-10 17:03:10 +000082 FnPtr = FuncNames["lle_X_"+F->getName()];
Chris Lattner7720c8e2001-09-10 04:50:17 +000083 if (FnPtr == 0) // Try calling a generic function... if it exists...
Chris Lattner26e6e102006-06-01 17:27:11 +000084 FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(
Reid Spencerdf5a37e2004-11-29 14:11:29 +000085 ("lle_X_"+F->getName()).c_str());
Reid Spencercc442ca2007-05-19 01:36:17 +000086 if (FnPtr == 0)
87 FnPtr = (ExFunc)(intptr_t)
88 sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
Chris Lattner7720c8e2001-09-10 04:50:17 +000089 if (FnPtr != 0)
Chuck Rose III936baaa2007-07-27 18:26:35 +000090 Functions->insert(std::make_pair(F, FnPtr)); // Cache for later
Chris Lattner7720c8e2001-09-10 04:50:17 +000091 return FnPtr;
92}
93
Chris Lattner4e7dd8f2005-01-21 19:59:37 +000094GenericValue Interpreter::callExternalFunction(Function *F,
Chris Lattner44edb6b2003-05-14 14:21:30 +000095 const std::vector<GenericValue> &ArgVals) {
Chris Lattnere43db882001-10-27 04:15:57 +000096 TheInterpreter = this;
97
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000098 // Do a lookup to see if the function is in our cache... this should just be a
Misha Brukmand5d96b92003-10-10 17:42:19 +000099 // deferred annotation!
Chuck Rose III936baaa2007-07-27 18:26:35 +0000100 std::map<const Function *, ExFunc>::iterator FI = Functions->find(F);
101 ExFunc Fn = (FI == Functions->end()) ? lookupFunction(F) : FI->second;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000102 if (Fn == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000103 cerr << "Tried to execute an unknown external function: "
104 << F->getType()->getDescription() << " " << F->getName() << "\n";
Chris Lattner4e7dd8f2005-01-21 19:59:37 +0000105 if (F->getName() == "__main")
106 return GenericValue();
107 abort();
Chris Lattner7720c8e2001-09-10 04:50:17 +0000108 }
109
110 // TODO: FIXME when types are not const!
Chris Lattner4e7dd8f2005-01-21 19:59:37 +0000111 GenericValue Result = Fn(const_cast<FunctionType*>(F->getFunctionType()),
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000112 ArgVals);
Chris Lattner4721f132001-10-30 20:28:00 +0000113 return Result;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000114}
115
116
117//===----------------------------------------------------------------------===//
Chris Lattnerb408b122002-03-29 03:57:15 +0000118// Functions "exported" to the running application...
Chris Lattner7720c8e2001-09-10 04:50:17 +0000119//
120extern "C" { // Don't add C++ manglings to llvm mangling :)
121
Chris Lattner005cbce2002-10-02 21:12:13 +0000122// void putchar(ubyte)
Reid Spencerb3b07272007-04-21 17:11:45 +0000123GenericValue lle_X_putchar(FunctionType *FT, const vector<GenericValue> &Args){
Reid Spencerbfcd5992007-03-06 03:08:12 +0000124 cout << ((char)Args[0].IntVal.getZExtValue()) << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000125 return Args[0];
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000126}
127
Reid Spencercc442ca2007-05-19 01:36:17 +0000128// void _IO_putc(int c, FILE* fp)
129GenericValue lle_X__IO_putc(FunctionType *FT, const vector<GenericValue> &Args){
130#ifdef __linux__
131 _IO_putc((char)Args[0].IntVal.getZExtValue(), (FILE*) Args[1].PointerVal);
132#else
133 assert(0 && "Can't call _IO_putc on this platform");
134#endif
135 return Args[0];
136}
137
Chris Lattner44edb6b2003-05-14 14:21:30 +0000138// void atexit(Function*)
Reid Spencer97e0c222007-03-30 16:41:50 +0000139GenericValue lle_X_atexit(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner44edb6b2003-05-14 14:21:30 +0000140 assert(Args.size() == 1);
141 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
142 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000143 GV.IntVal = 0;
Chris Lattner44edb6b2003-05-14 14:21:30 +0000144 return GV;
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000145}
146
Chris Lattner005cbce2002-10-02 21:12:13 +0000147// void exit(int)
Reid Spencer97e0c222007-03-30 16:41:50 +0000148GenericValue lle_X_exit(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnere43db882001-10-27 04:15:57 +0000149 TheInterpreter->exitCalled(Args[0]);
150 return GenericValue();
151}
152
Chris Lattner005cbce2002-10-02 21:12:13 +0000153// void abort(void)
Reid Spencer97e0c222007-03-30 16:41:50 +0000154GenericValue lle_X_abort(FunctionType *FT, const vector<GenericValue> &Args) {
Brian Gaekeb56a6bc2003-11-05 01:18:49 +0000155 raise (SIGABRT);
Chris Lattner1ee34a52002-05-20 21:17:16 +0000156 return GenericValue();
157}
158
Chris Lattnerc2593162001-10-27 08:28:11 +0000159// void *malloc(uint)
Reid Spencer97e0c222007-03-30 16:41:50 +0000160GenericValue lle_X_malloc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner4721f132001-10-30 20:28:00 +0000161 assert(Args.size() == 1 && "Malloc expects one argument!");
Reid Spencer97e0c222007-03-30 16:41:50 +0000162 assert(isa<PointerType>(FT->getReturnType()) && "malloc must return pointer");
Reid Spencerbfcd5992007-03-06 03:08:12 +0000163 return PTOGV(malloc(Args[0].IntVal.getZExtValue()));
Chris Lattnerc2593162001-10-27 08:28:11 +0000164}
165
Chris Lattner957d62a2003-04-23 19:55:24 +0000166// void *calloc(uint, uint)
Reid Spencer97e0c222007-03-30 16:41:50 +0000167GenericValue lle_X_calloc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000168 assert(Args.size() == 2 && "calloc expects two arguments!");
Reid Spencer97e0c222007-03-30 16:41:50 +0000169 assert(isa<PointerType>(FT->getReturnType()) && "calloc must return pointer");
Reid Spencerbfcd5992007-03-06 03:08:12 +0000170 return PTOGV(calloc(Args[0].IntVal.getZExtValue(),
171 Args[1].IntVal.getZExtValue()));
Chris Lattner957d62a2003-04-23 19:55:24 +0000172}
173
Reid Spencer97e0c222007-03-30 16:41:50 +0000174// void *calloc(uint, uint)
175GenericValue lle_X_realloc(FunctionType *FT, const vector<GenericValue> &Args) {
176 assert(Args.size() == 2 && "calloc expects two arguments!");
177 assert(isa<PointerType>(FT->getReturnType()) &&"realloc must return pointer");
178 return PTOGV(realloc(GVTOP(Args[0]), Args[1].IntVal.getZExtValue()));
179}
180
Chris Lattnerc2593162001-10-27 08:28:11 +0000181// void free(void *)
Reid Spencer97e0c222007-03-30 16:41:50 +0000182GenericValue lle_X_free(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000183 assert(Args.size() == 1);
Chris Lattnerb1118742003-01-13 00:59:47 +0000184 free(GVTOP(Args[0]));
Chris Lattnerc2593162001-10-27 08:28:11 +0000185 return GenericValue();
186}
187
Chris Lattner782b9392001-11-26 18:18:18 +0000188// int atoi(char *)
Reid Spencer97e0c222007-03-30 16:41:50 +0000189GenericValue lle_X_atoi(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000190 assert(Args.size() == 1);
191 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000192 GV.IntVal = APInt(32, atoi((char*)GVTOP(Args[0])));
Chris Lattner782b9392001-11-26 18:18:18 +0000193 return GV;
194}
195
Chris Lattnerc2593162001-10-27 08:28:11 +0000196// double pow(double, double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000197GenericValue lle_X_pow(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000198 assert(Args.size() == 2);
Chris Lattnerc2593162001-10-27 08:28:11 +0000199 GenericValue GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000200 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000201 return GV;
202}
203
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000204// double sin(double)
205GenericValue lle_X_sin(FunctionType *FT, const vector<GenericValue> &Args) {
206 assert(Args.size() == 1);
207 GenericValue GV;
208 GV.DoubleVal = sin(Args[0].DoubleVal);
209 return GV;
210}
211
212// double cos(double)
213GenericValue lle_X_cos(FunctionType *FT, const vector<GenericValue> &Args) {
214 assert(Args.size() == 1);
215 GenericValue GV;
216 GV.DoubleVal = cos(Args[0].DoubleVal);
217 return GV;
218}
219
Chris Lattner34dd24b2002-02-18 19:06:25 +0000220// double exp(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000221GenericValue lle_X_exp(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner34dd24b2002-02-18 19:06:25 +0000222 assert(Args.size() == 1);
223 GenericValue GV;
224 GV.DoubleVal = exp(Args[0].DoubleVal);
225 return GV;
226}
227
Chris Lattnerc063d382001-11-06 21:52:18 +0000228// double sqrt(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000229GenericValue lle_X_sqrt(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000230 assert(Args.size() == 1);
231 GenericValue GV;
232 GV.DoubleVal = sqrt(Args[0].DoubleVal);
233 return GV;
234}
Chris Lattnerc2593162001-10-27 08:28:11 +0000235
Chris Lattner86790052001-11-06 22:53:25 +0000236// double log(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000237GenericValue lle_X_log(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000238 assert(Args.size() == 1);
239 GenericValue GV;
240 GV.DoubleVal = log(Args[0].DoubleVal);
241 return GV;
242}
243
Chris Lattner782b9392001-11-26 18:18:18 +0000244// double floor(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000245GenericValue lle_X_floor(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000246 assert(Args.size() == 1);
247 GenericValue GV;
248 GV.DoubleVal = floor(Args[0].DoubleVal);
249 return GV;
250}
251
Reid Spencerabec8f92004-10-27 23:03:44 +0000252#ifdef HAVE_RAND48
253
Chris Lattner86790052001-11-06 22:53:25 +0000254// double drand48()
Reid Spencer97e0c222007-03-30 16:41:50 +0000255GenericValue lle_X_drand48(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000256 assert(Args.size() == 0);
257 GenericValue GV;
258 GV.DoubleVal = drand48();
259 return GV;
260}
261
Chris Lattner1b600142001-11-13 05:46:08 +0000262// long lrand48()
Reid Spencer97e0c222007-03-30 16:41:50 +0000263GenericValue lle_X_lrand48(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000264 assert(Args.size() == 0);
265 GenericValue GV;
Duncan Sands22ad1d72007-12-10 14:43:10 +0000266 GV.IntVal = APInt(32, lrand48());
Chris Lattner1b600142001-11-13 05:46:08 +0000267 return GV;
268}
269
270// void srand48(long)
Reid Spencer97e0c222007-03-30 16:41:50 +0000271GenericValue lle_X_srand48(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000272 assert(Args.size() == 1);
Duncan Sands22ad1d72007-12-10 14:43:10 +0000273 srand48(Args[0].IntVal.getZExtValue());
Chris Lattner1b600142001-11-13 05:46:08 +0000274 return GenericValue();
275}
276
Reid Spencerabec8f92004-10-27 23:03:44 +0000277#endif
278
279// int rand()
Reid Spencer97e0c222007-03-30 16:41:50 +0000280GenericValue lle_X_rand(FunctionType *FT, const vector<GenericValue> &Args) {
Reid Spencerabec8f92004-10-27 23:03:44 +0000281 assert(Args.size() == 0);
282 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000283 GV.IntVal = APInt(32, rand());
Reid Spencerabec8f92004-10-27 23:03:44 +0000284 return GV;
285}
286
Chris Lattner782b9392001-11-26 18:18:18 +0000287// void srand(uint)
Reid Spencer97e0c222007-03-30 16:41:50 +0000288GenericValue lle_X_srand(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000289 assert(Args.size() == 1);
Reid Spencerbfcd5992007-03-06 03:08:12 +0000290 srand(Args[0].IntVal.getZExtValue());
Chris Lattner782b9392001-11-26 18:18:18 +0000291 return GenericValue();
292}
Chris Lattner86790052001-11-06 22:53:25 +0000293
Chris Lattnerb1118742003-01-13 00:59:47 +0000294// int puts(const char*)
Reid Spencer97e0c222007-03-30 16:41:50 +0000295GenericValue lle_X_puts(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerb1118742003-01-13 00:59:47 +0000296 assert(Args.size() == 1);
297 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000298 GV.IntVal = APInt(32, puts((char*)GVTOP(Args[0])));
Chris Lattnerb1118742003-01-13 00:59:47 +0000299 return GV;
300}
301
Chris Lattnere7c6f722001-12-13 00:43:47 +0000302// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
303// output useful.
Reid Spencer97e0c222007-03-30 16:41:50 +0000304GenericValue lle_X_sprintf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerb1118742003-01-13 00:59:47 +0000305 char *OutputBuffer = (char *)GVTOP(Args[0]);
306 const char *FmtStr = (const char *)GVTOP(Args[1]);
Chris Lattnere7c6f722001-12-13 00:43:47 +0000307 unsigned ArgNo = 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000308
309 // printf should return # chars printed. This is completely incorrect, but
310 // close enough for now.
Reid Spencerbfcd5992007-03-06 03:08:12 +0000311 GenericValue GV;
312 GV.IntVal = APInt(32, strlen(FmtStr));
Chris Lattner08845a22001-10-29 20:27:45 +0000313 while (1) {
314 switch (*FmtStr) {
315 case 0: return GV; // Null terminator...
316 default: // Normal nonspecial character
Chris Lattnere7c6f722001-12-13 00:43:47 +0000317 sprintf(OutputBuffer++, "%c", *FmtStr++);
Chris Lattner08845a22001-10-29 20:27:45 +0000318 break;
319 case '\\': { // Handle escape codes
Chris Lattnere7c6f722001-12-13 00:43:47 +0000320 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
321 FmtStr += 2; OutputBuffer += 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000322 break;
323 }
324 case '%': { // Handle format specifiers
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000325 char FmtBuf[100] = "", Buffer[1000] = "";
326 char *FB = FmtBuf;
327 *FB++ = *FmtStr++;
328 char Last = *FB++ = *FmtStr++;
329 unsigned HowLong = 0;
330 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
331 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
332 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
333 Last != 'p' && Last != 's' && Last != '%') {
334 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
335 Last = *FB++ = *FmtStr++;
Chris Lattner08845a22001-10-29 20:27:45 +0000336 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000337 *FB = 0;
Misha Brukmand1c881a2005-04-21 22:43:08 +0000338
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000339 switch (Last) {
340 case '%':
341 sprintf(Buffer, FmtBuf); break;
342 case 'c':
Reid Spencerbfcd5992007-03-06 03:08:12 +0000343 sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
344 break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000345 case 'd': case 'i':
346 case 'u': case 'o':
347 case 'x': case 'X':
Chris Lattner69ab7a82002-08-02 23:08:32 +0000348 if (HowLong >= 1) {
Chris Lattner1543e402003-08-24 14:02:47 +0000349 if (HowLong == 1 &&
Chris Lattnerfe854032006-08-16 01:24:12 +0000350 TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 &&
Reid Spencer19b7e0e2006-05-24 19:21:13 +0000351 sizeof(long) < sizeof(int64_t)) {
Chris Lattner69ab7a82002-08-02 23:08:32 +0000352 // Make sure we use %lld with a 64 bit argument because we might be
353 // compiling LLI on a 32 bit compiler.
354 unsigned Size = strlen(FmtBuf);
355 FmtBuf[Size] = FmtBuf[Size-1];
356 FmtBuf[Size+1] = 0;
357 FmtBuf[Size-1] = 'l';
358 }
Reid Spencerbfcd5992007-03-06 03:08:12 +0000359 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
Chris Lattner69ab7a82002-08-02 23:08:32 +0000360 } else
Reid Spencerbfcd5992007-03-06 03:08:12 +0000361 sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
362 break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000363 case 'e': case 'E': case 'g': case 'G': case 'f':
364 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
365 case 'p':
Chris Lattnerb1118742003-01-13 00:59:47 +0000366 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
Misha Brukmand1c881a2005-04-21 22:43:08 +0000367 case 's':
Chris Lattnerb1118742003-01-13 00:59:47 +0000368 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
Bill Wendlinge8156192006-12-07 01:30:32 +0000369 default: cerr << "<unknown printf code '" << *FmtStr << "'!>";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000370 ArgNo++; break;
Chris Lattner08845a22001-10-29 20:27:45 +0000371 }
Chris Lattnere7c6f722001-12-13 00:43:47 +0000372 strcpy(OutputBuffer, Buffer);
373 OutputBuffer += strlen(Buffer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000374 }
Chris Lattner08845a22001-10-29 20:27:45 +0000375 break;
376 }
Chris Lattner08845a22001-10-29 20:27:45 +0000377 }
Reid Spencerb3b07272007-04-21 17:11:45 +0000378 return GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000379}
380
Chris Lattnere7c6f722001-12-13 00:43:47 +0000381// int printf(sbyte *, ...) - a very rough implementation to make output useful.
Reid Spencer97e0c222007-03-30 16:41:50 +0000382GenericValue lle_X_printf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000383 char Buffer[10000];
384 vector<GenericValue> NewArgs;
Reid Spencerb3b07272007-04-21 17:11:45 +0000385 NewArgs.push_back(PTOGV((void*)&Buffer[0]));
Chris Lattnere7c6f722001-12-13 00:43:47 +0000386 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
Reid Spencer97e0c222007-03-30 16:41:50 +0000387 GenericValue GV = lle_X_sprintf(FT, NewArgs);
Bill Wendlinge8156192006-12-07 01:30:32 +0000388 cout << Buffer;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000389 return GV;
390}
391
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000392static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1,
393 void *Arg2, void *Arg3, void *Arg4, void *Arg5,
394 void *Arg6, void *Arg7, void *Arg8) {
395 void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 };
396
397 // Loop over the format string, munging read values as appropriate (performs
Misha Brukman5560c9d2003-08-18 14:43:39 +0000398 // byteswaps as necessary).
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000399 unsigned ArgNo = 0;
400 while (*Fmt) {
401 if (*Fmt++ == '%') {
402 // Read any flag characters that may be present...
403 bool Suppress = false;
404 bool Half = false;
405 bool Long = false;
406 bool LongLong = false; // long long or long double
407
408 while (1) {
409 switch (*Fmt++) {
410 case '*': Suppress = true; break;
411 case 'a': /*Allocate = true;*/ break; // We don't need to track this
412 case 'h': Half = true; break;
413 case 'l': Long = true; break;
414 case 'q':
415 case 'L': LongLong = true; break;
416 default:
417 if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs
418 goto Out;
419 }
420 }
421 Out:
422
423 // Read the conversion character
424 if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
425 unsigned Size = 0;
426 const Type *Ty = 0;
427
428 switch (Fmt[-1]) {
429 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
430 case 'd':
431 if (Long || LongLong) {
Reid Spencere49661b2006-12-31 05:51:36 +0000432 Size = 8; Ty = Type::Int64Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000433 } else if (Half) {
Reid Spencere49661b2006-12-31 05:51:36 +0000434 Size = 4; Ty = Type::Int16Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000435 } else {
Reid Spencere49661b2006-12-31 05:51:36 +0000436 Size = 4; Ty = Type::Int32Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000437 }
438 break;
439
440 case 'e': case 'g': case 'E':
441 case 'f':
442 if (Long || LongLong) {
443 Size = 8; Ty = Type::DoubleTy;
444 } else {
445 Size = 4; Ty = Type::FloatTy;
446 }
447 break;
448
449 case 's': case 'c': case '[': // No byteswap needed
450 Size = 1;
Reid Spencere49661b2006-12-31 05:51:36 +0000451 Ty = Type::Int8Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000452 break;
453
454 default: break;
455 }
456
457 if (Size) {
458 GenericValue GV;
459 void *Arg = Args[ArgNo++];
460 memcpy(&GV, Arg, Size);
461 TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
462 }
463 }
464 }
465 }
466}
467
Chris Lattner665ee882002-03-08 22:51:07 +0000468// int sscanf(const char *format, ...);
Reid Spencer97e0c222007-03-30 16:41:50 +0000469GenericValue lle_X_sscanf(FunctionType *FT, const vector<GenericValue> &args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000470 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
471
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000472 char *Args[10];
Chris Lattner665ee882002-03-08 22:51:07 +0000473 for (unsigned i = 0; i < args.size(); ++i)
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000474 Args[i] = (char*)GVTOP(args[i]);
Chris Lattner665ee882002-03-08 22:51:07 +0000475
476 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000477 GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
478 Args[5], Args[6], Args[7], Args[8], Args[9]));
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000479 ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4],
480 Args[5], Args[6], Args[7], Args[8], Args[9], 0);
481 return GV;
482}
483
484// int scanf(const char *format, ...);
Reid Spencer97e0c222007-03-30 16:41:50 +0000485GenericValue lle_X_scanf(FunctionType *FT, const vector<GenericValue> &args) {
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000486 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
487
488 char *Args[10];
489 for (unsigned i = 0; i < args.size(); ++i)
490 Args[i] = (char*)GVTOP(args[i]);
491
492 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000493 GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
494 Args[5], Args[6], Args[7], Args[8], Args[9]));
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000495 ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4],
496 Args[5], Args[6], Args[7], Args[8], Args[9]);
Chris Lattner665ee882002-03-08 22:51:07 +0000497 return GV;
498}
499
500
Chris Lattner295fe672002-01-23 21:38:07 +0000501// int clock(void) - Profiling implementation
Reid Spencer97e0c222007-03-30 16:41:50 +0000502GenericValue lle_i_clock(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner638559a2005-01-16 01:31:31 +0000503 extern unsigned int clock(void);
Reid Spencerbfcd5992007-03-06 03:08:12 +0000504 GenericValue GV;
505 GV.IntVal = APInt(32, clock());
Chris Lattner295fe672002-01-23 21:38:07 +0000506 return GV;
507}
Chris Lattnere7c6f722001-12-13 00:43:47 +0000508
Chris Lattner957d62a2003-04-23 19:55:24 +0000509
510//===----------------------------------------------------------------------===//
511// String Functions...
512//===----------------------------------------------------------------------===//
513
514// int strcmp(const char *S1, const char *S2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000515GenericValue lle_X_strcmp(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000516 assert(Args.size() == 2);
517 GenericValue Ret;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000518 Ret.IntVal = APInt(32, strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
Chris Lattner957d62a2003-04-23 19:55:24 +0000519 return Ret;
520}
521
522// char *strcat(char *Dest, const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000523GenericValue lle_X_strcat(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000524 assert(Args.size() == 2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000525 assert(isa<PointerType>(FT->getReturnType()) &&"strcat must return pointer");
Chris Lattner957d62a2003-04-23 19:55:24 +0000526 return PTOGV(strcat((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
527}
528
529// char *strcpy(char *Dest, const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000530GenericValue lle_X_strcpy(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000531 assert(Args.size() == 2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000532 assert(isa<PointerType>(FT->getReturnType()) &&"strcpy must return pointer");
Chris Lattner957d62a2003-04-23 19:55:24 +0000533 return PTOGV(strcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
534}
535
Brian Gaeke3c66c352004-05-01 06:42:15 +0000536static GenericValue size_t_to_GV (size_t n) {
537 GenericValue Ret;
538 if (sizeof (size_t) == sizeof (uint64_t)) {
Reid Spencerbfcd5992007-03-06 03:08:12 +0000539 Ret.IntVal = APInt(64, n);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000540 } else {
541 assert (sizeof (size_t) == sizeof (unsigned int));
Reid Spencerbfcd5992007-03-06 03:08:12 +0000542 Ret.IntVal = APInt(32, n);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000543 }
544 return Ret;
545}
546
Misha Brukmand1c881a2005-04-21 22:43:08 +0000547static size_t GV_to_size_t (GenericValue GV) {
Brian Gaeke3c66c352004-05-01 06:42:15 +0000548 size_t count;
549 if (sizeof (size_t) == sizeof (uint64_t)) {
Reid Spencerbfcd5992007-03-06 03:08:12 +0000550 count = (size_t)GV.IntVal.getZExtValue();
Brian Gaeke3c66c352004-05-01 06:42:15 +0000551 } else {
552 assert (sizeof (size_t) == sizeof (unsigned int));
Reid Spencerbfcd5992007-03-06 03:08:12 +0000553 count = (size_t)GV.IntVal.getZExtValue();
Brian Gaeke3c66c352004-05-01 06:42:15 +0000554 }
555 return count;
556}
557
Brian Gaeke6c9d5822003-12-12 15:38:06 +0000558// size_t strlen(const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000559GenericValue lle_X_strlen(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000560 assert(Args.size() == 1);
Brian Gaeke6c9d5822003-12-12 15:38:06 +0000561 size_t strlenResult = strlen ((char *) GVTOP (Args[0]));
Brian Gaeke3c66c352004-05-01 06:42:15 +0000562 return size_t_to_GV (strlenResult);
Chris Lattner957d62a2003-04-23 19:55:24 +0000563}
564
Brian Gaeke70975ee2003-09-05 18:42:01 +0000565// char *strdup(const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000566GenericValue lle_X_strdup(FunctionType *FT, const vector<GenericValue> &Args) {
Brian Gaeke70975ee2003-09-05 18:42:01 +0000567 assert(Args.size() == 1);
Reid Spencer97e0c222007-03-30 16:41:50 +0000568 assert(isa<PointerType>(FT->getReturnType()) && "strdup must return pointer");
Brian Gaeke70975ee2003-09-05 18:42:01 +0000569 return PTOGV(strdup((char*)GVTOP(Args[0])));
570}
571
Chris Lattnerc8cff9e2003-04-25 18:23:38 +0000572// char *__strdup(const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000573GenericValue lle_X___strdup(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc8cff9e2003-04-25 18:23:38 +0000574 assert(Args.size() == 1);
Reid Spencer97e0c222007-03-30 16:41:50 +0000575 assert(isa<PointerType>(FT->getReturnType()) &&"_strdup must return pointer");
Chris Lattnerc8cff9e2003-04-25 18:23:38 +0000576 return PTOGV(strdup((char*)GVTOP(Args[0])));
577}
578
Chris Lattner957d62a2003-04-23 19:55:24 +0000579// void *memset(void *S, int C, size_t N)
Reid Spencer97e0c222007-03-30 16:41:50 +0000580GenericValue lle_X_memset(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000581 assert(Args.size() == 3);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000582 size_t count = GV_to_size_t (Args[2]);
Reid Spencer97e0c222007-03-30 16:41:50 +0000583 assert(isa<PointerType>(FT->getReturnType()) && "memset must return pointer");
Reid Spencerbfcd5992007-03-06 03:08:12 +0000584 return PTOGV(memset(GVTOP(Args[0]), uint32_t(Args[1].IntVal.getZExtValue()),
585 count));
Chris Lattner957d62a2003-04-23 19:55:24 +0000586}
587
Chris Lattner5f311a72003-04-23 20:23:16 +0000588// void *memcpy(void *Dest, void *src, size_t Size);
Reid Spencer97e0c222007-03-30 16:41:50 +0000589GenericValue lle_X_memcpy(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner5f311a72003-04-23 20:23:16 +0000590 assert(Args.size() == 3);
Reid Spencer97e0c222007-03-30 16:41:50 +0000591 assert(isa<PointerType>(FT->getReturnType()) && "memcpy must return pointer");
Brian Gaeke3c66c352004-05-01 06:42:15 +0000592 size_t count = GV_to_size_t (Args[2]);
593 return PTOGV(memcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));
Chris Lattner5f311a72003-04-23 20:23:16 +0000594}
Chris Lattner957d62a2003-04-23 19:55:24 +0000595
Chris Lattner665ee882002-03-08 22:51:07 +0000596//===----------------------------------------------------------------------===//
597// IO Functions...
598//===----------------------------------------------------------------------===//
599
Chris Lattner005cbce2002-10-02 21:12:13 +0000600// getFILE - Turn a pointer in the host address space into a legit pointer in
Reid Spencer0098bdf2004-05-25 08:46:15 +0000601// the interpreter address space. This is an identity transformation.
602#define getFILE(ptr) ((FILE*)ptr)
Chris Lattner005cbce2002-10-02 21:12:13 +0000603
Chris Lattner665ee882002-03-08 22:51:07 +0000604// FILE *fopen(const char *filename, const char *mode);
Reid Spencer97e0c222007-03-30 16:41:50 +0000605GenericValue lle_X_fopen(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000606 assert(Args.size() == 2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000607 assert(isa<PointerType>(FT->getReturnType()) && "fopen must return pointer");
Chris Lattnerb1118742003-01-13 00:59:47 +0000608 return PTOGV(fopen((const char *)GVTOP(Args[0]),
Misha Brukman3c944972005-04-22 04:08:30 +0000609 (const char *)GVTOP(Args[1])));
Chris Lattner665ee882002-03-08 22:51:07 +0000610}
611
612// int fclose(FILE *F);
Reid Spencer97e0c222007-03-30 16:41:50 +0000613GenericValue lle_X_fclose(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000614 assert(Args.size() == 1);
615 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000616 GV.IntVal = APInt(32, fclose(getFILE(GVTOP(Args[0]))));
Chris Lattner665ee882002-03-08 22:51:07 +0000617 return GV;
618}
619
Chris Lattner25f6f372002-11-08 19:10:26 +0000620// int feof(FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000621GenericValue lle_X_feof(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner25f6f372002-11-08 19:10:26 +0000622 assert(Args.size() == 1);
623 GenericValue GV;
624
Reid Spencerbfcd5992007-03-06 03:08:12 +0000625 GV.IntVal = APInt(32, feof(getFILE(GVTOP(Args[0]))));
Chris Lattner25f6f372002-11-08 19:10:26 +0000626 return GV;
627}
628
Chris Lattner665ee882002-03-08 22:51:07 +0000629// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000630GenericValue lle_X_fread(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000631 assert(Args.size() == 4);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000632 size_t result;
Chris Lattner665ee882002-03-08 22:51:07 +0000633
Brian Gaeke3c66c352004-05-01 06:42:15 +0000634 result = fread((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
635 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
636 return size_t_to_GV (result);
Chris Lattner665ee882002-03-08 22:51:07 +0000637}
638
639// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000640GenericValue lle_X_fwrite(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000641 assert(Args.size() == 4);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000642 size_t result;
Chris Lattner665ee882002-03-08 22:51:07 +0000643
Brian Gaeke3c66c352004-05-01 06:42:15 +0000644 result = fwrite((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
645 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
646 return size_t_to_GV (result);
Chris Lattner665ee882002-03-08 22:51:07 +0000647}
648
649// char *fgets(char *s, int n, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000650GenericValue lle_X_fgets(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000651 assert(Args.size() == 3);
Reid Spencerbfcd5992007-03-06 03:08:12 +0000652 return GVTOP(fgets((char*)GVTOP(Args[0]), Args[1].IntVal.getZExtValue(),
Misha Brukman3c944972005-04-22 04:08:30 +0000653 getFILE(GVTOP(Args[2]))));
Chris Lattner665ee882002-03-08 22:51:07 +0000654}
655
Chris Lattnera4479cd2002-11-07 19:33:50 +0000656// FILE *freopen(const char *path, const char *mode, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000657GenericValue lle_X_freopen(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnera4479cd2002-11-07 19:33:50 +0000658 assert(Args.size() == 3);
Reid Spencer97e0c222007-03-30 16:41:50 +0000659 assert(isa<PointerType>(FT->getReturnType()) &&"freopen must return pointer");
Chris Lattnerb1118742003-01-13 00:59:47 +0000660 return PTOGV(freopen((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]),
Misha Brukman3c944972005-04-22 04:08:30 +0000661 getFILE(GVTOP(Args[2]))));
Chris Lattnera4479cd2002-11-07 19:33:50 +0000662}
663
Chris Lattner665ee882002-03-08 22:51:07 +0000664// int fflush(FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000665GenericValue lle_X_fflush(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000666 assert(Args.size() == 1);
667 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000668 GV.IntVal = APInt(32, fflush(getFILE(GVTOP(Args[0]))));
Chris Lattner005cbce2002-10-02 21:12:13 +0000669 return GV;
670}
671
672// int getc(FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000673GenericValue lle_X_getc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner005cbce2002-10-02 21:12:13 +0000674 assert(Args.size() == 1);
675 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000676 GV.IntVal = APInt(32, getc(getFILE(GVTOP(Args[0]))));
Chris Lattner665ee882002-03-08 22:51:07 +0000677 return GV;
678}
679
Chris Lattnerf87a1982003-04-23 19:20:50 +0000680// int _IO_getc(FILE *stream);
681GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) {
682 return lle_X_getc(F, Args);
683}
684
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000685// int fputc(int C, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000686GenericValue lle_X_fputc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000687 assert(Args.size() == 2);
688 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000689 GV.IntVal = APInt(32, fputc(Args[0].IntVal.getZExtValue(),
690 getFILE(GVTOP(Args[1]))));
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000691 return GV;
692}
693
694// int ungetc(int C, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000695GenericValue lle_X_ungetc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000696 assert(Args.size() == 2);
697 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000698 GV.IntVal = APInt(32, ungetc(Args[0].IntVal.getZExtValue(),
699 getFILE(GVTOP(Args[1]))));
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000700 return GV;
701}
702
Brian Gaeke59108d32004-06-16 02:56:40 +0000703// int ferror (FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000704GenericValue lle_X_ferror(FunctionType *FT, const vector<GenericValue> &Args) {
Brian Gaeke59108d32004-06-16 02:56:40 +0000705 assert(Args.size() == 1);
706 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000707 GV.IntVal = APInt(32, ferror (getFILE(GVTOP(Args[0]))));
Brian Gaeke59108d32004-06-16 02:56:40 +0000708 return GV;
709}
710
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000711// int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
712// useful.
Reid Spencer97e0c222007-03-30 16:41:50 +0000713GenericValue lle_X_fprintf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner9dbf6dd2003-04-21 22:43:20 +0000714 assert(Args.size() >= 2);
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000715 char Buffer[10000];
716 vector<GenericValue> NewArgs;
Chris Lattnerb1118742003-01-13 00:59:47 +0000717 NewArgs.push_back(PTOGV(Buffer));
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000718 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
Reid Spencer97e0c222007-03-30 16:41:50 +0000719 GenericValue GV = lle_X_sprintf(FT, NewArgs);
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000720
Chris Lattnerb1118742003-01-13 00:59:47 +0000721 fputs(Buffer, getFILE(GVTOP(Args[0])));
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000722 return GV;
723}
724
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000725// int __cxa_guard_acquire (__guard *g);
726GenericValue lle_X___cxa_guard_acquire(FunctionType *FT,
727 const vector<GenericValue> &Args) {
728 assert(Args.size() == 1);
729 GenericValue GV;
730 GV.IntVal = APInt(32, __cxxabiv1::__cxa_guard_acquire (
731 (__cxxabiv1::__guard*)GVTOP(Args[0])));
732 return GV;
733}
734
735// void __cxa_guard_release (__guard *g);
736GenericValue lle_X___cxa_guard_release(FunctionType *FT,
737 const vector<GenericValue> &Args) {
738 assert(Args.size() == 1);
739 __cxxabiv1::__cxa_guard_release ((__cxxabiv1::__guard*)GVTOP(Args[0]));
740 return GenericValue();
741}
742
Chris Lattner7720c8e2001-09-10 04:50:17 +0000743} // End extern "C"
Chris Lattner4721f132001-10-30 20:28:00 +0000744
745
Chris Lattnerda82ed52003-05-08 16:18:31 +0000746void Interpreter::initializeExternalFunctions() {
Reid Spencerb3b07272007-04-21 17:11:45 +0000747 FuncNames["lle_X_putchar"] = lle_X_putchar;
Reid Spencercc442ca2007-05-19 01:36:17 +0000748 FuncNames["lle_X__IO_putc"] = lle_X__IO_putc;
Chris Lattner0f279b22001-11-03 10:15:32 +0000749 FuncNames["lle_X_exit"] = lle_X_exit;
Chris Lattner1ee34a52002-05-20 21:17:16 +0000750 FuncNames["lle_X_abort"] = lle_X_abort;
Chris Lattner0f279b22001-11-03 10:15:32 +0000751 FuncNames["lle_X_malloc"] = lle_X_malloc;
Chris Lattner957d62a2003-04-23 19:55:24 +0000752 FuncNames["lle_X_calloc"] = lle_X_calloc;
Reid Spencer97e0c222007-03-30 16:41:50 +0000753 FuncNames["lle_X_realloc"] = lle_X_realloc;
Chris Lattner0f279b22001-11-03 10:15:32 +0000754 FuncNames["lle_X_free"] = lle_X_free;
Chris Lattner782b9392001-11-26 18:18:18 +0000755 FuncNames["lle_X_atoi"] = lle_X_atoi;
Chris Lattner0f279b22001-11-03 10:15:32 +0000756 FuncNames["lle_X_pow"] = lle_X_pow;
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000757 FuncNames["lle_X_sin"] = lle_X_sin;
758 FuncNames["lle_X_cos"] = lle_X_cos;
Chris Lattner34dd24b2002-02-18 19:06:25 +0000759 FuncNames["lle_X_exp"] = lle_X_exp;
Chris Lattner1b600142001-11-13 05:46:08 +0000760 FuncNames["lle_X_log"] = lle_X_log;
Chris Lattner782b9392001-11-26 18:18:18 +0000761 FuncNames["lle_X_floor"] = lle_X_floor;
762 FuncNames["lle_X_srand"] = lle_X_srand;
Reid Spencerabec8f92004-10-27 23:03:44 +0000763 FuncNames["lle_X_rand"] = lle_X_rand;
764#ifdef HAVE_RAND48
Chris Lattner1b600142001-11-13 05:46:08 +0000765 FuncNames["lle_X_drand48"] = lle_X_drand48;
766 FuncNames["lle_X_srand48"] = lle_X_srand48;
767 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
Reid Spencerabec8f92004-10-27 23:03:44 +0000768#endif
Chris Lattnerc063d382001-11-06 21:52:18 +0000769 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
Chris Lattnerb1118742003-01-13 00:59:47 +0000770 FuncNames["lle_X_puts"] = lle_X_puts;
Chris Lattner0f279b22001-11-03 10:15:32 +0000771 FuncNames["lle_X_printf"] = lle_X_printf;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000772 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
Chris Lattner665ee882002-03-08 22:51:07 +0000773 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000774 FuncNames["lle_X_scanf"] = lle_X_scanf;
Chris Lattner295fe672002-01-23 21:38:07 +0000775 FuncNames["lle_i_clock"] = lle_i_clock;
Chris Lattner957d62a2003-04-23 19:55:24 +0000776
777 FuncNames["lle_X_strcmp"] = lle_X_strcmp;
778 FuncNames["lle_X_strcat"] = lle_X_strcat;
779 FuncNames["lle_X_strcpy"] = lle_X_strcpy;
780 FuncNames["lle_X_strlen"] = lle_X_strlen;
Chris Lattnerda82ed52003-05-08 16:18:31 +0000781 FuncNames["lle_X___strdup"] = lle_X___strdup;
Chris Lattner957d62a2003-04-23 19:55:24 +0000782 FuncNames["lle_X_memset"] = lle_X_memset;
Chris Lattner5f311a72003-04-23 20:23:16 +0000783 FuncNames["lle_X_memcpy"] = lle_X_memcpy;
Chris Lattner957d62a2003-04-23 19:55:24 +0000784
Chris Lattner665ee882002-03-08 22:51:07 +0000785 FuncNames["lle_X_fopen"] = lle_X_fopen;
786 FuncNames["lle_X_fclose"] = lle_X_fclose;
Chris Lattner25f6f372002-11-08 19:10:26 +0000787 FuncNames["lle_X_feof"] = lle_X_feof;
Chris Lattner665ee882002-03-08 22:51:07 +0000788 FuncNames["lle_X_fread"] = lle_X_fread;
789 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
790 FuncNames["lle_X_fgets"] = lle_X_fgets;
791 FuncNames["lle_X_fflush"] = lle_X_fflush;
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000792 FuncNames["lle_X_fgetc"] = lle_X_getc;
Chris Lattner005cbce2002-10-02 21:12:13 +0000793 FuncNames["lle_X_getc"] = lle_X_getc;
Chris Lattnerf87a1982003-04-23 19:20:50 +0000794 FuncNames["lle_X__IO_getc"] = lle_X__IO_getc;
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000795 FuncNames["lle_X_fputc"] = lle_X_fputc;
796 FuncNames["lle_X_ungetc"] = lle_X_ungetc;
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000797 FuncNames["lle_X_fprintf"] = lle_X_fprintf;
Chris Lattnera4479cd2002-11-07 19:33:50 +0000798 FuncNames["lle_X_freopen"] = lle_X_freopen;
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000799
800 FuncNames["lle_X___cxa_guard_acquire"] = lle_X___cxa_guard_acquire;
801 FuncNames["lle_X____cxa_guard_release"] = lle_X___cxa_guard_release;
Chris Lattner4721f132001-10-30 20:28:00 +0000802}
Brian Gaeked0fde302003-11-11 22:41:34 +0000803