blob: b85f270f9e0a3663d89e5479dabd8e0596df76d8 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000032#include <cstring>
Zhou Sheng621dead2007-12-12 06:16:47 +000033
34#ifdef __linux__
Zhou Sheng6a7951c2007-12-12 04:55:43 +000035#include <cxxabi.h>
Zhou Sheng621dead2007-12-12 06:16:47 +000036#endif
37
Chris Lattner697954c2002-01-20 22:54:45 +000038using std::vector;
Chris Lattner7720c8e2001-09-10 04:50:17 +000039
Chris Lattnerf7a743d2003-12-14 23:25:48 +000040using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000041
Chris Lattnerb408b122002-03-29 03:57:15 +000042typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
Chuck Rose III936baaa2007-07-27 18:26:35 +000043static ManagedStatic<std::map<const Function *, ExFunc> > Functions;
Chris Lattner697954c2002-01-20 22:54:45 +000044static std::map<std::string, ExFunc> FuncNames;
Chris Lattner7720c8e2001-09-10 04:50:17 +000045
Chris Lattnere43db882001-10-27 04:15:57 +000046static Interpreter *TheInterpreter;
47
Chris Lattner7720c8e2001-09-10 04:50:17 +000048static char getTypeID(const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000049 switch (Ty->getTypeID()) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000050 case Type::VoidTyID: return 'V';
Reid Spencera54b7cb2007-01-12 07:05:14 +000051 case Type::IntegerTyID:
52 switch (cast<IntegerType>(Ty)->getBitWidth()) {
53 case 1: return 'o';
54 case 8: return 'B';
55 case 16: return 'S';
56 case 32: return 'I';
57 case 64: return 'L';
58 default: return 'N';
59 }
Chris Lattner7720c8e2001-09-10 04:50:17 +000060 case Type::FloatTyID: return 'F';
61 case Type::DoubleTyID: return 'D';
62 case Type::PointerTyID: return 'P';
Reid Spencere49661b2006-12-31 05:51:36 +000063 case Type::FunctionTyID:return 'M';
Chris Lattner7720c8e2001-09-10 04:50:17 +000064 case Type::StructTyID: return 'T';
65 case Type::ArrayTyID: return 'A';
66 case Type::OpaqueTyID: return 'O';
67 default: return 'U';
68 }
69}
70
Anton Korobeynikov42346f52007-07-30 23:03:25 +000071// Try to find address of external function given a Function object.
72// Please note, that interpreter doesn't know how to assemble a
73// real call in general case (this is JIT job), that's why it assumes,
74// that all external functions has the same (and pretty "general") signature.
75// The typical example of such functions are "lle_X_" ones.
Brian Gaeke58a6faa2003-10-10 17:03:10 +000076static ExFunc lookupFunction(const Function *F) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000077 // Function not found, look it up... start by figuring out what the
78 // composite function name should be.
Chris Lattner697954c2002-01-20 22:54:45 +000079 std::string ExtName = "lle_";
Brian Gaeke58a6faa2003-10-10 17:03:10 +000080 const FunctionType *FT = F->getFunctionType();
81 for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
82 ExtName += getTypeID(FT->getContainedType(i));
83 ExtName += "_" + F->getName();
Chris Lattner7720c8e2001-09-10 04:50:17 +000084
Chris Lattner4721f132001-10-30 20:28:00 +000085 ExFunc FnPtr = FuncNames[ExtName];
86 if (FnPtr == 0)
Brian Gaeke58a6faa2003-10-10 17:03:10 +000087 FnPtr = FuncNames["lle_X_"+F->getName()];
Chris Lattner7720c8e2001-09-10 04:50:17 +000088 if (FnPtr == 0) // Try calling a generic function... if it exists...
Chris Lattner26e6e102006-06-01 17:27:11 +000089 FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(
Reid Spencerdf5a37e2004-11-29 14:11:29 +000090 ("lle_X_"+F->getName()).c_str());
Reid Spencercc442ca2007-05-19 01:36:17 +000091 if (FnPtr == 0)
92 FnPtr = (ExFunc)(intptr_t)
93 sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
Chris Lattner7720c8e2001-09-10 04:50:17 +000094 if (FnPtr != 0)
Chuck Rose III936baaa2007-07-27 18:26:35 +000095 Functions->insert(std::make_pair(F, FnPtr)); // Cache for later
Chris Lattner7720c8e2001-09-10 04:50:17 +000096 return FnPtr;
97}
98
Chris Lattner4e7dd8f2005-01-21 19:59:37 +000099GenericValue Interpreter::callExternalFunction(Function *F,
Chris Lattner44edb6b2003-05-14 14:21:30 +0000100 const std::vector<GenericValue> &ArgVals) {
Chris Lattnere43db882001-10-27 04:15:57 +0000101 TheInterpreter = this;
102
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000103 // Do a lookup to see if the function is in our cache... this should just be a
Misha Brukmand5d96b92003-10-10 17:42:19 +0000104 // deferred annotation!
Chuck Rose III936baaa2007-07-27 18:26:35 +0000105 std::map<const Function *, ExFunc>::iterator FI = Functions->find(F);
106 ExFunc Fn = (FI == Functions->end()) ? lookupFunction(F) : FI->second;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000107 if (Fn == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000108 cerr << "Tried to execute an unknown external function: "
109 << F->getType()->getDescription() << " " << F->getName() << "\n";
Chris Lattner4e7dd8f2005-01-21 19:59:37 +0000110 if (F->getName() == "__main")
111 return GenericValue();
112 abort();
Chris Lattner7720c8e2001-09-10 04:50:17 +0000113 }
114
115 // TODO: FIXME when types are not const!
Chris Lattner4e7dd8f2005-01-21 19:59:37 +0000116 GenericValue Result = Fn(const_cast<FunctionType*>(F->getFunctionType()),
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000117 ArgVals);
Chris Lattner4721f132001-10-30 20:28:00 +0000118 return Result;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000119}
120
121
122//===----------------------------------------------------------------------===//
Chris Lattnerb408b122002-03-29 03:57:15 +0000123// Functions "exported" to the running application...
Chris Lattner7720c8e2001-09-10 04:50:17 +0000124//
125extern "C" { // Don't add C++ manglings to llvm mangling :)
126
Chris Lattner005cbce2002-10-02 21:12:13 +0000127// void putchar(ubyte)
Reid Spencerb3b07272007-04-21 17:11:45 +0000128GenericValue lle_X_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
Reid Spencercc442ca2007-05-19 01:36:17 +0000133// void _IO_putc(int c, FILE* fp)
134GenericValue lle_X__IO_putc(FunctionType *FT, const vector<GenericValue> &Args){
135#ifdef __linux__
136 _IO_putc((char)Args[0].IntVal.getZExtValue(), (FILE*) Args[1].PointerVal);
137#else
138 assert(0 && "Can't call _IO_putc on this platform");
139#endif
140 return Args[0];
141}
142
Chris Lattner44edb6b2003-05-14 14:21:30 +0000143// void atexit(Function*)
Reid Spencer97e0c222007-03-30 16:41:50 +0000144GenericValue lle_X_atexit(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner44edb6b2003-05-14 14:21:30 +0000145 assert(Args.size() == 1);
146 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
147 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000148 GV.IntVal = 0;
Chris Lattner44edb6b2003-05-14 14:21:30 +0000149 return GV;
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000150}
151
Chris Lattner005cbce2002-10-02 21:12:13 +0000152// void exit(int)
Reid Spencer97e0c222007-03-30 16:41:50 +0000153GenericValue lle_X_exit(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnere43db882001-10-27 04:15:57 +0000154 TheInterpreter->exitCalled(Args[0]);
155 return GenericValue();
156}
157
Chris Lattner005cbce2002-10-02 21:12:13 +0000158// void abort(void)
Reid Spencer97e0c222007-03-30 16:41:50 +0000159GenericValue lle_X_abort(FunctionType *FT, const vector<GenericValue> &Args) {
Brian Gaekeb56a6bc2003-11-05 01:18:49 +0000160 raise (SIGABRT);
Chris Lattner1ee34a52002-05-20 21:17:16 +0000161 return GenericValue();
162}
163
Chris Lattnerc2593162001-10-27 08:28:11 +0000164// void *malloc(uint)
Reid Spencer97e0c222007-03-30 16:41:50 +0000165GenericValue lle_X_malloc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner4721f132001-10-30 20:28:00 +0000166 assert(Args.size() == 1 && "Malloc expects one argument!");
Reid Spencer97e0c222007-03-30 16:41:50 +0000167 assert(isa<PointerType>(FT->getReturnType()) && "malloc must return pointer");
Reid Spencerbfcd5992007-03-06 03:08:12 +0000168 return PTOGV(malloc(Args[0].IntVal.getZExtValue()));
Chris Lattnerc2593162001-10-27 08:28:11 +0000169}
170
Chris Lattner957d62a2003-04-23 19:55:24 +0000171// void *calloc(uint, uint)
Reid Spencer97e0c222007-03-30 16:41:50 +0000172GenericValue lle_X_calloc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000173 assert(Args.size() == 2 && "calloc expects two arguments!");
Reid Spencer97e0c222007-03-30 16:41:50 +0000174 assert(isa<PointerType>(FT->getReturnType()) && "calloc must return pointer");
Reid Spencerbfcd5992007-03-06 03:08:12 +0000175 return PTOGV(calloc(Args[0].IntVal.getZExtValue(),
176 Args[1].IntVal.getZExtValue()));
Chris Lattner957d62a2003-04-23 19:55:24 +0000177}
178
Reid Spencer97e0c222007-03-30 16:41:50 +0000179// void *calloc(uint, uint)
180GenericValue lle_X_realloc(FunctionType *FT, const vector<GenericValue> &Args) {
181 assert(Args.size() == 2 && "calloc expects two arguments!");
182 assert(isa<PointerType>(FT->getReturnType()) &&"realloc must return pointer");
183 return PTOGV(realloc(GVTOP(Args[0]), Args[1].IntVal.getZExtValue()));
184}
185
Chris Lattnerc2593162001-10-27 08:28:11 +0000186// void free(void *)
Reid Spencer97e0c222007-03-30 16:41:50 +0000187GenericValue lle_X_free(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000188 assert(Args.size() == 1);
Chris Lattnerb1118742003-01-13 00:59:47 +0000189 free(GVTOP(Args[0]));
Chris Lattnerc2593162001-10-27 08:28:11 +0000190 return GenericValue();
191}
192
Chris Lattner782b9392001-11-26 18:18:18 +0000193// int atoi(char *)
Reid Spencer97e0c222007-03-30 16:41:50 +0000194GenericValue lle_X_atoi(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000195 assert(Args.size() == 1);
196 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000197 GV.IntVal = APInt(32, atoi((char*)GVTOP(Args[0])));
Chris Lattner782b9392001-11-26 18:18:18 +0000198 return GV;
199}
200
Chris Lattnerc2593162001-10-27 08:28:11 +0000201// double pow(double, double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000202GenericValue lle_X_pow(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000203 assert(Args.size() == 2);
Chris Lattnerc2593162001-10-27 08:28:11 +0000204 GenericValue GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000205 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000206 return GV;
207}
208
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000209// double sin(double)
210GenericValue lle_X_sin(FunctionType *FT, const vector<GenericValue> &Args) {
211 assert(Args.size() == 1);
212 GenericValue GV;
213 GV.DoubleVal = sin(Args[0].DoubleVal);
214 return GV;
215}
216
217// double cos(double)
218GenericValue lle_X_cos(FunctionType *FT, const vector<GenericValue> &Args) {
219 assert(Args.size() == 1);
220 GenericValue GV;
221 GV.DoubleVal = cos(Args[0].DoubleVal);
222 return GV;
223}
224
Chris Lattner34dd24b2002-02-18 19:06:25 +0000225// double exp(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000226GenericValue lle_X_exp(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner34dd24b2002-02-18 19:06:25 +0000227 assert(Args.size() == 1);
228 GenericValue GV;
229 GV.DoubleVal = exp(Args[0].DoubleVal);
230 return GV;
231}
232
Chris Lattnerc063d382001-11-06 21:52:18 +0000233// double sqrt(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000234GenericValue lle_X_sqrt(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000235 assert(Args.size() == 1);
236 GenericValue GV;
237 GV.DoubleVal = sqrt(Args[0].DoubleVal);
238 return GV;
239}
Chris Lattnerc2593162001-10-27 08:28:11 +0000240
Chris Lattner86790052001-11-06 22:53:25 +0000241// double log(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000242GenericValue lle_X_log(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000243 assert(Args.size() == 1);
244 GenericValue GV;
245 GV.DoubleVal = log(Args[0].DoubleVal);
246 return GV;
247}
248
Chris Lattner782b9392001-11-26 18:18:18 +0000249// double floor(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000250GenericValue lle_X_floor(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000251 assert(Args.size() == 1);
252 GenericValue GV;
253 GV.DoubleVal = floor(Args[0].DoubleVal);
254 return GV;
255}
256
Reid Spencerabec8f92004-10-27 23:03:44 +0000257#ifdef HAVE_RAND48
258
Chris Lattner86790052001-11-06 22:53:25 +0000259// double drand48()
Reid Spencer97e0c222007-03-30 16:41:50 +0000260GenericValue lle_X_drand48(FunctionType *FT, const vector<GenericValue> &Args) {
Dan Gohman30359592008-01-29 13:02:09 +0000261 assert(Args.empty());
Chris Lattner86790052001-11-06 22:53:25 +0000262 GenericValue GV;
263 GV.DoubleVal = drand48();
264 return GV;
265}
266
Chris Lattner1b600142001-11-13 05:46:08 +0000267// long lrand48()
Reid Spencer97e0c222007-03-30 16:41:50 +0000268GenericValue lle_X_lrand48(FunctionType *FT, const vector<GenericValue> &Args) {
Dan Gohman30359592008-01-29 13:02:09 +0000269 assert(Args.empty());
Chris Lattner1b600142001-11-13 05:46:08 +0000270 GenericValue GV;
Duncan Sands22ad1d72007-12-10 14:43:10 +0000271 GV.IntVal = APInt(32, lrand48());
Chris Lattner1b600142001-11-13 05:46:08 +0000272 return GV;
273}
274
275// void srand48(long)
Reid Spencer97e0c222007-03-30 16:41:50 +0000276GenericValue lle_X_srand48(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000277 assert(Args.size() == 1);
Duncan Sands22ad1d72007-12-10 14:43:10 +0000278 srand48(Args[0].IntVal.getZExtValue());
Chris Lattner1b600142001-11-13 05:46:08 +0000279 return GenericValue();
280}
281
Reid Spencerabec8f92004-10-27 23:03:44 +0000282#endif
283
284// int rand()
Reid Spencer97e0c222007-03-30 16:41:50 +0000285GenericValue lle_X_rand(FunctionType *FT, const vector<GenericValue> &Args) {
Dan Gohman30359592008-01-29 13:02:09 +0000286 assert(Args.empty());
Reid Spencerabec8f92004-10-27 23:03:44 +0000287 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000288 GV.IntVal = APInt(32, rand());
Reid Spencerabec8f92004-10-27 23:03:44 +0000289 return GV;
290}
291
Chris Lattner782b9392001-11-26 18:18:18 +0000292// void srand(uint)
Reid Spencer97e0c222007-03-30 16:41:50 +0000293GenericValue lle_X_srand(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000294 assert(Args.size() == 1);
Reid Spencerbfcd5992007-03-06 03:08:12 +0000295 srand(Args[0].IntVal.getZExtValue());
Chris Lattner782b9392001-11-26 18:18:18 +0000296 return GenericValue();
297}
Chris Lattner86790052001-11-06 22:53:25 +0000298
Chris Lattnerb1118742003-01-13 00:59:47 +0000299// int puts(const char*)
Reid Spencer97e0c222007-03-30 16:41:50 +0000300GenericValue lle_X_puts(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerb1118742003-01-13 00:59:47 +0000301 assert(Args.size() == 1);
302 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000303 GV.IntVal = APInt(32, puts((char*)GVTOP(Args[0])));
Chris Lattnerb1118742003-01-13 00:59:47 +0000304 return GV;
305}
306
Chris Lattnere7c6f722001-12-13 00:43:47 +0000307// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
308// output useful.
Reid Spencer97e0c222007-03-30 16:41:50 +0000309GenericValue lle_X_sprintf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerb1118742003-01-13 00:59:47 +0000310 char *OutputBuffer = (char *)GVTOP(Args[0]);
311 const char *FmtStr = (const char *)GVTOP(Args[1]);
Chris Lattnere7c6f722001-12-13 00:43:47 +0000312 unsigned ArgNo = 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000313
314 // printf should return # chars printed. This is completely incorrect, but
315 // close enough for now.
Reid Spencerbfcd5992007-03-06 03:08:12 +0000316 GenericValue GV;
317 GV.IntVal = APInt(32, strlen(FmtStr));
Chris Lattner08845a22001-10-29 20:27:45 +0000318 while (1) {
319 switch (*FmtStr) {
320 case 0: return GV; // Null terminator...
321 default: // Normal nonspecial character
Chris Lattnere7c6f722001-12-13 00:43:47 +0000322 sprintf(OutputBuffer++, "%c", *FmtStr++);
Chris Lattner08845a22001-10-29 20:27:45 +0000323 break;
324 case '\\': { // Handle escape codes
Chris Lattnere7c6f722001-12-13 00:43:47 +0000325 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
326 FmtStr += 2; OutputBuffer += 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000327 break;
328 }
329 case '%': { // Handle format specifiers
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000330 char FmtBuf[100] = "", Buffer[1000] = "";
331 char *FB = FmtBuf;
332 *FB++ = *FmtStr++;
333 char Last = *FB++ = *FmtStr++;
334 unsigned HowLong = 0;
335 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
336 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
337 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
338 Last != 'p' && Last != 's' && Last != '%') {
339 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
340 Last = *FB++ = *FmtStr++;
Chris Lattner08845a22001-10-29 20:27:45 +0000341 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000342 *FB = 0;
Misha Brukmand1c881a2005-04-21 22:43:08 +0000343
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000344 switch (Last) {
345 case '%':
346 sprintf(Buffer, FmtBuf); break;
347 case 'c':
Reid Spencerbfcd5992007-03-06 03:08:12 +0000348 sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
349 break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000350 case 'd': case 'i':
351 case 'u': case 'o':
352 case 'x': case 'X':
Chris Lattner69ab7a82002-08-02 23:08:32 +0000353 if (HowLong >= 1) {
Chris Lattner1543e402003-08-24 14:02:47 +0000354 if (HowLong == 1 &&
Chris Lattnerfe854032006-08-16 01:24:12 +0000355 TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 &&
Reid Spencer19b7e0e2006-05-24 19:21:13 +0000356 sizeof(long) < sizeof(int64_t)) {
Chris Lattner69ab7a82002-08-02 23:08:32 +0000357 // Make sure we use %lld with a 64 bit argument because we might be
358 // compiling LLI on a 32 bit compiler.
359 unsigned Size = strlen(FmtBuf);
360 FmtBuf[Size] = FmtBuf[Size-1];
361 FmtBuf[Size+1] = 0;
362 FmtBuf[Size-1] = 'l';
363 }
Reid Spencerbfcd5992007-03-06 03:08:12 +0000364 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
Chris Lattner69ab7a82002-08-02 23:08:32 +0000365 } else
Reid Spencerbfcd5992007-03-06 03:08:12 +0000366 sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
367 break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000368 case 'e': case 'E': case 'g': case 'G': case 'f':
369 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
370 case 'p':
Chris Lattnerb1118742003-01-13 00:59:47 +0000371 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
Misha Brukmand1c881a2005-04-21 22:43:08 +0000372 case 's':
Chris Lattnerb1118742003-01-13 00:59:47 +0000373 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
Bill Wendlinge8156192006-12-07 01:30:32 +0000374 default: cerr << "<unknown printf code '" << *FmtStr << "'!>";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000375 ArgNo++; break;
Chris Lattner08845a22001-10-29 20:27:45 +0000376 }
Chris Lattnere7c6f722001-12-13 00:43:47 +0000377 strcpy(OutputBuffer, Buffer);
378 OutputBuffer += strlen(Buffer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000379 }
Chris Lattner08845a22001-10-29 20:27:45 +0000380 break;
381 }
Chris Lattner08845a22001-10-29 20:27:45 +0000382 }
Reid Spencerb3b07272007-04-21 17:11:45 +0000383 return GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000384}
385
Chris Lattnere7c6f722001-12-13 00:43:47 +0000386// int printf(sbyte *, ...) - a very rough implementation to make output useful.
Reid Spencer97e0c222007-03-30 16:41:50 +0000387GenericValue lle_X_printf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000388 char Buffer[10000];
389 vector<GenericValue> NewArgs;
Reid Spencerb3b07272007-04-21 17:11:45 +0000390 NewArgs.push_back(PTOGV((void*)&Buffer[0]));
Chris Lattnere7c6f722001-12-13 00:43:47 +0000391 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
Reid Spencer97e0c222007-03-30 16:41:50 +0000392 GenericValue GV = lle_X_sprintf(FT, NewArgs);
Bill Wendlinge8156192006-12-07 01:30:32 +0000393 cout << Buffer;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000394 return GV;
395}
396
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000397static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1,
398 void *Arg2, void *Arg3, void *Arg4, void *Arg5,
399 void *Arg6, void *Arg7, void *Arg8) {
400 void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 };
401
402 // Loop over the format string, munging read values as appropriate (performs
Misha Brukman5560c9d2003-08-18 14:43:39 +0000403 // byteswaps as necessary).
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000404 unsigned ArgNo = 0;
405 while (*Fmt) {
406 if (*Fmt++ == '%') {
407 // Read any flag characters that may be present...
408 bool Suppress = false;
409 bool Half = false;
410 bool Long = false;
411 bool LongLong = false; // long long or long double
412
413 while (1) {
414 switch (*Fmt++) {
415 case '*': Suppress = true; break;
416 case 'a': /*Allocate = true;*/ break; // We don't need to track this
417 case 'h': Half = true; break;
418 case 'l': Long = true; break;
419 case 'q':
420 case 'L': LongLong = true; break;
421 default:
422 if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs
423 goto Out;
424 }
425 }
426 Out:
427
428 // Read the conversion character
429 if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
430 unsigned Size = 0;
431 const Type *Ty = 0;
432
433 switch (Fmt[-1]) {
434 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
435 case 'd':
436 if (Long || LongLong) {
Reid Spencere49661b2006-12-31 05:51:36 +0000437 Size = 8; Ty = Type::Int64Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000438 } else if (Half) {
Reid Spencere49661b2006-12-31 05:51:36 +0000439 Size = 4; Ty = Type::Int16Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000440 } else {
Reid Spencere49661b2006-12-31 05:51:36 +0000441 Size = 4; Ty = Type::Int32Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000442 }
443 break;
444
445 case 'e': case 'g': case 'E':
446 case 'f':
447 if (Long || LongLong) {
448 Size = 8; Ty = Type::DoubleTy;
449 } else {
450 Size = 4; Ty = Type::FloatTy;
451 }
452 break;
453
454 case 's': case 'c': case '[': // No byteswap needed
455 Size = 1;
Reid Spencere49661b2006-12-31 05:51:36 +0000456 Ty = Type::Int8Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000457 break;
458
459 default: break;
460 }
461
462 if (Size) {
463 GenericValue GV;
464 void *Arg = Args[ArgNo++];
465 memcpy(&GV, Arg, Size);
466 TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
467 }
468 }
469 }
470 }
471}
472
Chris Lattner665ee882002-03-08 22:51:07 +0000473// int sscanf(const char *format, ...);
Reid Spencer97e0c222007-03-30 16:41:50 +0000474GenericValue lle_X_sscanf(FunctionType *FT, const vector<GenericValue> &args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000475 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
476
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000477 char *Args[10];
Chris Lattner665ee882002-03-08 22:51:07 +0000478 for (unsigned i = 0; i < args.size(); ++i)
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000479 Args[i] = (char*)GVTOP(args[i]);
Chris Lattner665ee882002-03-08 22:51:07 +0000480
481 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000482 GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
483 Args[5], Args[6], Args[7], Args[8], Args[9]));
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000484 ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4],
485 Args[5], Args[6], Args[7], Args[8], Args[9], 0);
486 return GV;
487}
488
489// int scanf(const char *format, ...);
Reid Spencer97e0c222007-03-30 16:41:50 +0000490GenericValue lle_X_scanf(FunctionType *FT, const vector<GenericValue> &args) {
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000491 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
492
493 char *Args[10];
494 for (unsigned i = 0; i < args.size(); ++i)
495 Args[i] = (char*)GVTOP(args[i]);
496
497 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000498 GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
499 Args[5], Args[6], Args[7], Args[8], Args[9]));
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000500 ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4],
501 Args[5], Args[6], Args[7], Args[8], Args[9]);
Chris Lattner665ee882002-03-08 22:51:07 +0000502 return GV;
503}
504
505
Chris Lattner295fe672002-01-23 21:38:07 +0000506// int clock(void) - Profiling implementation
Reid Spencer97e0c222007-03-30 16:41:50 +0000507GenericValue lle_i_clock(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner638559a2005-01-16 01:31:31 +0000508 extern unsigned int clock(void);
Reid Spencerbfcd5992007-03-06 03:08:12 +0000509 GenericValue GV;
510 GV.IntVal = APInt(32, clock());
Chris Lattner295fe672002-01-23 21:38:07 +0000511 return GV;
512}
Chris Lattnere7c6f722001-12-13 00:43:47 +0000513
Chris Lattner957d62a2003-04-23 19:55:24 +0000514
515//===----------------------------------------------------------------------===//
516// String Functions...
517//===----------------------------------------------------------------------===//
518
519// int strcmp(const char *S1, const char *S2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000520GenericValue lle_X_strcmp(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000521 assert(Args.size() == 2);
522 GenericValue Ret;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000523 Ret.IntVal = APInt(32, strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
Chris Lattner957d62a2003-04-23 19:55:24 +0000524 return Ret;
525}
526
527// char *strcat(char *Dest, const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000528GenericValue lle_X_strcat(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000529 assert(Args.size() == 2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000530 assert(isa<PointerType>(FT->getReturnType()) &&"strcat must return pointer");
Chris Lattner957d62a2003-04-23 19:55:24 +0000531 return PTOGV(strcat((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
532}
533
534// char *strcpy(char *Dest, const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000535GenericValue lle_X_strcpy(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000536 assert(Args.size() == 2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000537 assert(isa<PointerType>(FT->getReturnType()) &&"strcpy must return pointer");
Chris Lattner957d62a2003-04-23 19:55:24 +0000538 return PTOGV(strcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
539}
540
Brian Gaeke3c66c352004-05-01 06:42:15 +0000541static GenericValue size_t_to_GV (size_t n) {
542 GenericValue Ret;
543 if (sizeof (size_t) == sizeof (uint64_t)) {
Reid Spencerbfcd5992007-03-06 03:08:12 +0000544 Ret.IntVal = APInt(64, n);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000545 } else {
546 assert (sizeof (size_t) == sizeof (unsigned int));
Reid Spencerbfcd5992007-03-06 03:08:12 +0000547 Ret.IntVal = APInt(32, n);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000548 }
549 return Ret;
550}
551
Misha Brukmand1c881a2005-04-21 22:43:08 +0000552static size_t GV_to_size_t (GenericValue GV) {
Brian Gaeke3c66c352004-05-01 06:42:15 +0000553 size_t count;
554 if (sizeof (size_t) == sizeof (uint64_t)) {
Reid Spencerbfcd5992007-03-06 03:08:12 +0000555 count = (size_t)GV.IntVal.getZExtValue();
Brian Gaeke3c66c352004-05-01 06:42:15 +0000556 } else {
557 assert (sizeof (size_t) == sizeof (unsigned int));
Reid Spencerbfcd5992007-03-06 03:08:12 +0000558 count = (size_t)GV.IntVal.getZExtValue();
Brian Gaeke3c66c352004-05-01 06:42:15 +0000559 }
560 return count;
561}
562
Brian Gaeke6c9d5822003-12-12 15:38:06 +0000563// size_t strlen(const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000564GenericValue lle_X_strlen(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000565 assert(Args.size() == 1);
Brian Gaeke6c9d5822003-12-12 15:38:06 +0000566 size_t strlenResult = strlen ((char *) GVTOP (Args[0]));
Brian Gaeke3c66c352004-05-01 06:42:15 +0000567 return size_t_to_GV (strlenResult);
Chris Lattner957d62a2003-04-23 19:55:24 +0000568}
569
Brian Gaeke70975ee2003-09-05 18:42:01 +0000570// char *strdup(const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000571GenericValue lle_X_strdup(FunctionType *FT, const vector<GenericValue> &Args) {
Brian Gaeke70975ee2003-09-05 18:42:01 +0000572 assert(Args.size() == 1);
Reid Spencer97e0c222007-03-30 16:41:50 +0000573 assert(isa<PointerType>(FT->getReturnType()) && "strdup must return pointer");
Brian Gaeke70975ee2003-09-05 18:42:01 +0000574 return PTOGV(strdup((char*)GVTOP(Args[0])));
575}
576
Chris Lattnerc8cff9e2003-04-25 18:23:38 +0000577// char *__strdup(const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000578GenericValue lle_X___strdup(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc8cff9e2003-04-25 18:23:38 +0000579 assert(Args.size() == 1);
Reid Spencer97e0c222007-03-30 16:41:50 +0000580 assert(isa<PointerType>(FT->getReturnType()) &&"_strdup must return pointer");
Chris Lattnerc8cff9e2003-04-25 18:23:38 +0000581 return PTOGV(strdup((char*)GVTOP(Args[0])));
582}
583
Chris Lattner957d62a2003-04-23 19:55:24 +0000584// void *memset(void *S, int C, size_t N)
Reid Spencer97e0c222007-03-30 16:41:50 +0000585GenericValue lle_X_memset(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000586 assert(Args.size() == 3);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000587 size_t count = GV_to_size_t (Args[2]);
Reid Spencer97e0c222007-03-30 16:41:50 +0000588 assert(isa<PointerType>(FT->getReturnType()) && "memset must return pointer");
Reid Spencerbfcd5992007-03-06 03:08:12 +0000589 return PTOGV(memset(GVTOP(Args[0]), uint32_t(Args[1].IntVal.getZExtValue()),
590 count));
Chris Lattner957d62a2003-04-23 19:55:24 +0000591}
592
Chris Lattner5f311a72003-04-23 20:23:16 +0000593// void *memcpy(void *Dest, void *src, size_t Size);
Reid Spencer97e0c222007-03-30 16:41:50 +0000594GenericValue lle_X_memcpy(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner5f311a72003-04-23 20:23:16 +0000595 assert(Args.size() == 3);
Reid Spencer97e0c222007-03-30 16:41:50 +0000596 assert(isa<PointerType>(FT->getReturnType()) && "memcpy must return pointer");
Brian Gaeke3c66c352004-05-01 06:42:15 +0000597 size_t count = GV_to_size_t (Args[2]);
598 return PTOGV(memcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));
Chris Lattner5f311a72003-04-23 20:23:16 +0000599}
Chris Lattner957d62a2003-04-23 19:55:24 +0000600
Evan Cheng8647bcd2008-02-20 07:55:26 +0000601// void *memcpy(void *Dest, void *src, size_t Size);
602GenericValue lle_X_memmove(FunctionType *FT, const vector<GenericValue> &Args) {
603 assert(Args.size() == 3);
604 assert(isa<PointerType>(FT->getReturnType()) && "memmove must return pointer");
605 size_t count = GV_to_size_t (Args[2]);
606 return PTOGV(memmove((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));
607}
608
Chris Lattner665ee882002-03-08 22:51:07 +0000609//===----------------------------------------------------------------------===//
610// IO Functions...
611//===----------------------------------------------------------------------===//
612
Chris Lattner005cbce2002-10-02 21:12:13 +0000613// getFILE - Turn a pointer in the host address space into a legit pointer in
Reid Spencer0098bdf2004-05-25 08:46:15 +0000614// the interpreter address space. This is an identity transformation.
615#define getFILE(ptr) ((FILE*)ptr)
Chris Lattner005cbce2002-10-02 21:12:13 +0000616
Chris Lattner665ee882002-03-08 22:51:07 +0000617// FILE *fopen(const char *filename, const char *mode);
Reid Spencer97e0c222007-03-30 16:41:50 +0000618GenericValue lle_X_fopen(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000619 assert(Args.size() == 2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000620 assert(isa<PointerType>(FT->getReturnType()) && "fopen must return pointer");
Chris Lattnerb1118742003-01-13 00:59:47 +0000621 return PTOGV(fopen((const char *)GVTOP(Args[0]),
Misha Brukman3c944972005-04-22 04:08:30 +0000622 (const char *)GVTOP(Args[1])));
Chris Lattner665ee882002-03-08 22:51:07 +0000623}
624
625// int fclose(FILE *F);
Reid Spencer97e0c222007-03-30 16:41:50 +0000626GenericValue lle_X_fclose(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000627 assert(Args.size() == 1);
628 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000629 GV.IntVal = APInt(32, fclose(getFILE(GVTOP(Args[0]))));
Chris Lattner665ee882002-03-08 22:51:07 +0000630 return GV;
631}
632
Chris Lattner25f6f372002-11-08 19:10:26 +0000633// int feof(FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000634GenericValue lle_X_feof(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner25f6f372002-11-08 19:10:26 +0000635 assert(Args.size() == 1);
636 GenericValue GV;
637
Reid Spencerbfcd5992007-03-06 03:08:12 +0000638 GV.IntVal = APInt(32, feof(getFILE(GVTOP(Args[0]))));
Chris Lattner25f6f372002-11-08 19:10:26 +0000639 return GV;
640}
641
Chris Lattner665ee882002-03-08 22:51:07 +0000642// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000643GenericValue lle_X_fread(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000644 assert(Args.size() == 4);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000645 size_t result;
Chris Lattner665ee882002-03-08 22:51:07 +0000646
Brian Gaeke3c66c352004-05-01 06:42:15 +0000647 result = fread((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
648 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
649 return size_t_to_GV (result);
Chris Lattner665ee882002-03-08 22:51:07 +0000650}
651
652// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000653GenericValue lle_X_fwrite(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000654 assert(Args.size() == 4);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000655 size_t result;
Chris Lattner665ee882002-03-08 22:51:07 +0000656
Brian Gaeke3c66c352004-05-01 06:42:15 +0000657 result = fwrite((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
658 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
659 return size_t_to_GV (result);
Chris Lattner665ee882002-03-08 22:51:07 +0000660}
661
662// char *fgets(char *s, int n, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000663GenericValue lle_X_fgets(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000664 assert(Args.size() == 3);
Dan Gohmanded2b0d2007-12-14 15:41:34 +0000665 return PTOGV(fgets((char*)GVTOP(Args[0]), Args[1].IntVal.getZExtValue(),
Misha Brukman3c944972005-04-22 04:08:30 +0000666 getFILE(GVTOP(Args[2]))));
Chris Lattner665ee882002-03-08 22:51:07 +0000667}
668
Chris Lattnera4479cd2002-11-07 19:33:50 +0000669// FILE *freopen(const char *path, const char *mode, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000670GenericValue lle_X_freopen(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnera4479cd2002-11-07 19:33:50 +0000671 assert(Args.size() == 3);
Reid Spencer97e0c222007-03-30 16:41:50 +0000672 assert(isa<PointerType>(FT->getReturnType()) &&"freopen must return pointer");
Chris Lattnerb1118742003-01-13 00:59:47 +0000673 return PTOGV(freopen((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]),
Misha Brukman3c944972005-04-22 04:08:30 +0000674 getFILE(GVTOP(Args[2]))));
Chris Lattnera4479cd2002-11-07 19:33:50 +0000675}
676
Chris Lattner665ee882002-03-08 22:51:07 +0000677// int fflush(FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000678GenericValue lle_X_fflush(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000679 assert(Args.size() == 1);
680 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000681 GV.IntVal = APInt(32, fflush(getFILE(GVTOP(Args[0]))));
Chris Lattner005cbce2002-10-02 21:12:13 +0000682 return GV;
683}
684
685// int getc(FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000686GenericValue lle_X_getc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner005cbce2002-10-02 21:12:13 +0000687 assert(Args.size() == 1);
688 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000689 GV.IntVal = APInt(32, getc(getFILE(GVTOP(Args[0]))));
Chris Lattner665ee882002-03-08 22:51:07 +0000690 return GV;
691}
692
Chris Lattnerf87a1982003-04-23 19:20:50 +0000693// int _IO_getc(FILE *stream);
694GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) {
695 return lle_X_getc(F, Args);
696}
697
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000698// int fputc(int C, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000699GenericValue lle_X_fputc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000700 assert(Args.size() == 2);
701 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000702 GV.IntVal = APInt(32, fputc(Args[0].IntVal.getZExtValue(),
703 getFILE(GVTOP(Args[1]))));
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000704 return GV;
705}
706
707// int ungetc(int C, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000708GenericValue lle_X_ungetc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000709 assert(Args.size() == 2);
710 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000711 GV.IntVal = APInt(32, ungetc(Args[0].IntVal.getZExtValue(),
712 getFILE(GVTOP(Args[1]))));
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000713 return GV;
714}
715
Brian Gaeke59108d32004-06-16 02:56:40 +0000716// int ferror (FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000717GenericValue lle_X_ferror(FunctionType *FT, const vector<GenericValue> &Args) {
Brian Gaeke59108d32004-06-16 02:56:40 +0000718 assert(Args.size() == 1);
719 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000720 GV.IntVal = APInt(32, ferror (getFILE(GVTOP(Args[0]))));
Brian Gaeke59108d32004-06-16 02:56:40 +0000721 return GV;
722}
723
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000724// int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
725// useful.
Reid Spencer97e0c222007-03-30 16:41:50 +0000726GenericValue lle_X_fprintf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner9dbf6dd2003-04-21 22:43:20 +0000727 assert(Args.size() >= 2);
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000728 char Buffer[10000];
729 vector<GenericValue> NewArgs;
Chris Lattnerb1118742003-01-13 00:59:47 +0000730 NewArgs.push_back(PTOGV(Buffer));
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000731 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
Reid Spencer97e0c222007-03-30 16:41:50 +0000732 GenericValue GV = lle_X_sprintf(FT, NewArgs);
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000733
Chris Lattnerb1118742003-01-13 00:59:47 +0000734 fputs(Buffer, getFILE(GVTOP(Args[0])));
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000735 return GV;
736}
737
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000738// int __cxa_guard_acquire (__guard *g);
739GenericValue lle_X___cxa_guard_acquire(FunctionType *FT,
740 const vector<GenericValue> &Args) {
741 assert(Args.size() == 1);
742 GenericValue GV;
Zhou Sheng621dead2007-12-12 06:16:47 +0000743#ifdef __linux__
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000744 GV.IntVal = APInt(32, __cxxabiv1::__cxa_guard_acquire (
745 (__cxxabiv1::__guard*)GVTOP(Args[0])));
Zhou Sheng621dead2007-12-12 06:16:47 +0000746#else
747 assert(0 && "Can't call __cxa_guard_acquire on this platform");
748#endif
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000749 return GV;
750}
751
752// void __cxa_guard_release (__guard *g);
753GenericValue lle_X___cxa_guard_release(FunctionType *FT,
754 const vector<GenericValue> &Args) {
755 assert(Args.size() == 1);
Zhou Sheng621dead2007-12-12 06:16:47 +0000756#ifdef __linux__
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000757 __cxxabiv1::__cxa_guard_release ((__cxxabiv1::__guard*)GVTOP(Args[0]));
Zhou Sheng621dead2007-12-12 06:16:47 +0000758#else
759 assert(0 && "Can't call __cxa_guard_release on this platform");
760#endif
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000761 return GenericValue();
762}
763
Chris Lattner7720c8e2001-09-10 04:50:17 +0000764} // End extern "C"
Chris Lattner4721f132001-10-30 20:28:00 +0000765
766
Chris Lattnerda82ed52003-05-08 16:18:31 +0000767void Interpreter::initializeExternalFunctions() {
Reid Spencerb3b07272007-04-21 17:11:45 +0000768 FuncNames["lle_X_putchar"] = lle_X_putchar;
Reid Spencercc442ca2007-05-19 01:36:17 +0000769 FuncNames["lle_X__IO_putc"] = lle_X__IO_putc;
Chris Lattner0f279b22001-11-03 10:15:32 +0000770 FuncNames["lle_X_exit"] = lle_X_exit;
Chris Lattner1ee34a52002-05-20 21:17:16 +0000771 FuncNames["lle_X_abort"] = lle_X_abort;
Chris Lattner0f279b22001-11-03 10:15:32 +0000772 FuncNames["lle_X_malloc"] = lle_X_malloc;
Chris Lattner957d62a2003-04-23 19:55:24 +0000773 FuncNames["lle_X_calloc"] = lle_X_calloc;
Reid Spencer97e0c222007-03-30 16:41:50 +0000774 FuncNames["lle_X_realloc"] = lle_X_realloc;
Chris Lattner0f279b22001-11-03 10:15:32 +0000775 FuncNames["lle_X_free"] = lle_X_free;
Chris Lattner782b9392001-11-26 18:18:18 +0000776 FuncNames["lle_X_atoi"] = lle_X_atoi;
Chris Lattner0f279b22001-11-03 10:15:32 +0000777 FuncNames["lle_X_pow"] = lle_X_pow;
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000778 FuncNames["lle_X_sin"] = lle_X_sin;
779 FuncNames["lle_X_cos"] = lle_X_cos;
Chris Lattner34dd24b2002-02-18 19:06:25 +0000780 FuncNames["lle_X_exp"] = lle_X_exp;
Chris Lattner1b600142001-11-13 05:46:08 +0000781 FuncNames["lle_X_log"] = lle_X_log;
Chris Lattner782b9392001-11-26 18:18:18 +0000782 FuncNames["lle_X_floor"] = lle_X_floor;
783 FuncNames["lle_X_srand"] = lle_X_srand;
Reid Spencerabec8f92004-10-27 23:03:44 +0000784 FuncNames["lle_X_rand"] = lle_X_rand;
785#ifdef HAVE_RAND48
Chris Lattner1b600142001-11-13 05:46:08 +0000786 FuncNames["lle_X_drand48"] = lle_X_drand48;
787 FuncNames["lle_X_srand48"] = lle_X_srand48;
788 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
Reid Spencerabec8f92004-10-27 23:03:44 +0000789#endif
Chris Lattnerc063d382001-11-06 21:52:18 +0000790 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
Chris Lattnerb1118742003-01-13 00:59:47 +0000791 FuncNames["lle_X_puts"] = lle_X_puts;
Chris Lattner0f279b22001-11-03 10:15:32 +0000792 FuncNames["lle_X_printf"] = lle_X_printf;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000793 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
Chris Lattner665ee882002-03-08 22:51:07 +0000794 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000795 FuncNames["lle_X_scanf"] = lle_X_scanf;
Chris Lattner295fe672002-01-23 21:38:07 +0000796 FuncNames["lle_i_clock"] = lle_i_clock;
Chris Lattner957d62a2003-04-23 19:55:24 +0000797
798 FuncNames["lle_X_strcmp"] = lle_X_strcmp;
799 FuncNames["lle_X_strcat"] = lle_X_strcat;
800 FuncNames["lle_X_strcpy"] = lle_X_strcpy;
801 FuncNames["lle_X_strlen"] = lle_X_strlen;
Chris Lattnerda82ed52003-05-08 16:18:31 +0000802 FuncNames["lle_X___strdup"] = lle_X___strdup;
Chris Lattner957d62a2003-04-23 19:55:24 +0000803 FuncNames["lle_X_memset"] = lle_X_memset;
Chris Lattner5f311a72003-04-23 20:23:16 +0000804 FuncNames["lle_X_memcpy"] = lle_X_memcpy;
Evan Cheng8647bcd2008-02-20 07:55:26 +0000805 FuncNames["lle_X_memmove"] = lle_X_memmove;
Chris Lattner957d62a2003-04-23 19:55:24 +0000806
Chris Lattner665ee882002-03-08 22:51:07 +0000807 FuncNames["lle_X_fopen"] = lle_X_fopen;
808 FuncNames["lle_X_fclose"] = lle_X_fclose;
Chris Lattner25f6f372002-11-08 19:10:26 +0000809 FuncNames["lle_X_feof"] = lle_X_feof;
Chris Lattner665ee882002-03-08 22:51:07 +0000810 FuncNames["lle_X_fread"] = lle_X_fread;
811 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
812 FuncNames["lle_X_fgets"] = lle_X_fgets;
813 FuncNames["lle_X_fflush"] = lle_X_fflush;
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000814 FuncNames["lle_X_fgetc"] = lle_X_getc;
Chris Lattner005cbce2002-10-02 21:12:13 +0000815 FuncNames["lle_X_getc"] = lle_X_getc;
Chris Lattnerf87a1982003-04-23 19:20:50 +0000816 FuncNames["lle_X__IO_getc"] = lle_X__IO_getc;
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000817 FuncNames["lle_X_fputc"] = lle_X_fputc;
818 FuncNames["lle_X_ungetc"] = lle_X_ungetc;
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000819 FuncNames["lle_X_fprintf"] = lle_X_fprintf;
Chris Lattnera4479cd2002-11-07 19:33:50 +0000820 FuncNames["lle_X_freopen"] = lle_X_freopen;
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000821
822 FuncNames["lle_X___cxa_guard_acquire"] = lle_X___cxa_guard_acquire;
823 FuncNames["lle_X____cxa_guard_release"] = lle_X___cxa_guard_release;
Chris Lattner4721f132001-10-30 20:28:00 +0000824}
Brian Gaeked0fde302003-11-11 22:41:34 +0000825