blob: bebbf8364ee5894fb3845e2010ff9a1aa75c28f8 [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>
Zhou Sheng621dead2007-12-12 06:16:47 +000032
33#ifdef __linux__
Zhou Sheng6a7951c2007-12-12 04:55:43 +000034#include <cxxabi.h>
Zhou Sheng621dead2007-12-12 06:16:47 +000035#endif
36
Chris Lattner697954c2002-01-20 22:54:45 +000037using std::vector;
Chris Lattner7720c8e2001-09-10 04:50:17 +000038
Chris Lattnerf7a743d2003-12-14 23:25:48 +000039using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000040
Chris Lattnerb408b122002-03-29 03:57:15 +000041typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
Chuck Rose III936baaa2007-07-27 18:26:35 +000042static ManagedStatic<std::map<const Function *, ExFunc> > Functions;
Chris Lattner697954c2002-01-20 22:54:45 +000043static std::map<std::string, ExFunc> FuncNames;
Chris Lattner7720c8e2001-09-10 04:50:17 +000044
Chris Lattnere43db882001-10-27 04:15:57 +000045static Interpreter *TheInterpreter;
46
Chris Lattner7720c8e2001-09-10 04:50:17 +000047static char getTypeID(const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000048 switch (Ty->getTypeID()) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000049 case Type::VoidTyID: return 'V';
Reid Spencera54b7cb2007-01-12 07:05:14 +000050 case Type::IntegerTyID:
51 switch (cast<IntegerType>(Ty)->getBitWidth()) {
52 case 1: return 'o';
53 case 8: return 'B';
54 case 16: return 'S';
55 case 32: return 'I';
56 case 64: return 'L';
57 default: return 'N';
58 }
Chris Lattner7720c8e2001-09-10 04:50:17 +000059 case Type::FloatTyID: return 'F';
60 case Type::DoubleTyID: return 'D';
61 case Type::PointerTyID: return 'P';
Reid Spencere49661b2006-12-31 05:51:36 +000062 case Type::FunctionTyID:return 'M';
Chris Lattner7720c8e2001-09-10 04:50:17 +000063 case Type::StructTyID: return 'T';
64 case Type::ArrayTyID: return 'A';
65 case Type::OpaqueTyID: return 'O';
66 default: return 'U';
67 }
68}
69
Anton Korobeynikov42346f52007-07-30 23:03:25 +000070// Try to find address of external function given a Function object.
71// Please note, that interpreter doesn't know how to assemble a
72// real call in general case (this is JIT job), that's why it assumes,
73// that all external functions has the same (and pretty "general") signature.
74// The typical example of such functions are "lle_X_" ones.
Brian Gaeke58a6faa2003-10-10 17:03:10 +000075static ExFunc lookupFunction(const Function *F) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000076 // Function not found, look it up... start by figuring out what the
77 // composite function name should be.
Chris Lattner697954c2002-01-20 22:54:45 +000078 std::string ExtName = "lle_";
Brian Gaeke58a6faa2003-10-10 17:03:10 +000079 const FunctionType *FT = F->getFunctionType();
80 for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
81 ExtName += getTypeID(FT->getContainedType(i));
82 ExtName += "_" + F->getName();
Chris Lattner7720c8e2001-09-10 04:50:17 +000083
Chris Lattner4721f132001-10-30 20:28:00 +000084 ExFunc FnPtr = FuncNames[ExtName];
85 if (FnPtr == 0)
Brian Gaeke58a6faa2003-10-10 17:03:10 +000086 FnPtr = FuncNames["lle_X_"+F->getName()];
Chris Lattner7720c8e2001-09-10 04:50:17 +000087 if (FnPtr == 0) // Try calling a generic function... if it exists...
Chris Lattner26e6e102006-06-01 17:27:11 +000088 FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(
Reid Spencerdf5a37e2004-11-29 14:11:29 +000089 ("lle_X_"+F->getName()).c_str());
Reid Spencercc442ca2007-05-19 01:36:17 +000090 if (FnPtr == 0)
91 FnPtr = (ExFunc)(intptr_t)
92 sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
Chris Lattner7720c8e2001-09-10 04:50:17 +000093 if (FnPtr != 0)
Chuck Rose III936baaa2007-07-27 18:26:35 +000094 Functions->insert(std::make_pair(F, FnPtr)); // Cache for later
Chris Lattner7720c8e2001-09-10 04:50:17 +000095 return FnPtr;
96}
97
Chris Lattner4e7dd8f2005-01-21 19:59:37 +000098GenericValue Interpreter::callExternalFunction(Function *F,
Chris Lattner44edb6b2003-05-14 14:21:30 +000099 const std::vector<GenericValue> &ArgVals) {
Chris Lattnere43db882001-10-27 04:15:57 +0000100 TheInterpreter = this;
101
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000102 // Do a lookup to see if the function is in our cache... this should just be a
Misha Brukmand5d96b92003-10-10 17:42:19 +0000103 // deferred annotation!
Chuck Rose III936baaa2007-07-27 18:26:35 +0000104 std::map<const Function *, ExFunc>::iterator FI = Functions->find(F);
105 ExFunc Fn = (FI == Functions->end()) ? lookupFunction(F) : FI->second;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000106 if (Fn == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000107 cerr << "Tried to execute an unknown external function: "
108 << F->getType()->getDescription() << " " << F->getName() << "\n";
Chris Lattner4e7dd8f2005-01-21 19:59:37 +0000109 if (F->getName() == "__main")
110 return GenericValue();
111 abort();
Chris Lattner7720c8e2001-09-10 04:50:17 +0000112 }
113
114 // TODO: FIXME when types are not const!
Chris Lattner4e7dd8f2005-01-21 19:59:37 +0000115 GenericValue Result = Fn(const_cast<FunctionType*>(F->getFunctionType()),
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000116 ArgVals);
Chris Lattner4721f132001-10-30 20:28:00 +0000117 return Result;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000118}
119
120
121//===----------------------------------------------------------------------===//
Chris Lattnerb408b122002-03-29 03:57:15 +0000122// Functions "exported" to the running application...
Chris Lattner7720c8e2001-09-10 04:50:17 +0000123//
124extern "C" { // Don't add C++ manglings to llvm mangling :)
125
Chris Lattner005cbce2002-10-02 21:12:13 +0000126// void putchar(ubyte)
Reid Spencerb3b07272007-04-21 17:11:45 +0000127GenericValue lle_X_putchar(FunctionType *FT, const vector<GenericValue> &Args){
Reid Spencerbfcd5992007-03-06 03:08:12 +0000128 cout << ((char)Args[0].IntVal.getZExtValue()) << std::flush;
Chris Lattnere43db882001-10-27 04:15:57 +0000129 return Args[0];
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000130}
131
Reid Spencercc442ca2007-05-19 01:36:17 +0000132// void _IO_putc(int c, FILE* fp)
133GenericValue lle_X__IO_putc(FunctionType *FT, const vector<GenericValue> &Args){
134#ifdef __linux__
135 _IO_putc((char)Args[0].IntVal.getZExtValue(), (FILE*) Args[1].PointerVal);
136#else
137 assert(0 && "Can't call _IO_putc on this platform");
138#endif
139 return Args[0];
140}
141
Chris Lattner44edb6b2003-05-14 14:21:30 +0000142// void atexit(Function*)
Reid Spencer97e0c222007-03-30 16:41:50 +0000143GenericValue lle_X_atexit(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner44edb6b2003-05-14 14:21:30 +0000144 assert(Args.size() == 1);
145 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
146 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000147 GV.IntVal = 0;
Chris Lattner44edb6b2003-05-14 14:21:30 +0000148 return GV;
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000149}
150
Chris Lattner005cbce2002-10-02 21:12:13 +0000151// void exit(int)
Reid Spencer97e0c222007-03-30 16:41:50 +0000152GenericValue lle_X_exit(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnere43db882001-10-27 04:15:57 +0000153 TheInterpreter->exitCalled(Args[0]);
154 return GenericValue();
155}
156
Chris Lattner005cbce2002-10-02 21:12:13 +0000157// void abort(void)
Reid Spencer97e0c222007-03-30 16:41:50 +0000158GenericValue lle_X_abort(FunctionType *FT, const vector<GenericValue> &Args) {
Brian Gaekeb56a6bc2003-11-05 01:18:49 +0000159 raise (SIGABRT);
Chris Lattner1ee34a52002-05-20 21:17:16 +0000160 return GenericValue();
161}
162
Chris Lattnerc2593162001-10-27 08:28:11 +0000163// void *malloc(uint)
Reid Spencer97e0c222007-03-30 16:41:50 +0000164GenericValue lle_X_malloc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner4721f132001-10-30 20:28:00 +0000165 assert(Args.size() == 1 && "Malloc expects one argument!");
Reid Spencer97e0c222007-03-30 16:41:50 +0000166 assert(isa<PointerType>(FT->getReturnType()) && "malloc must return pointer");
Reid Spencerbfcd5992007-03-06 03:08:12 +0000167 return PTOGV(malloc(Args[0].IntVal.getZExtValue()));
Chris Lattnerc2593162001-10-27 08:28:11 +0000168}
169
Chris Lattner957d62a2003-04-23 19:55:24 +0000170// void *calloc(uint, uint)
Reid Spencer97e0c222007-03-30 16:41:50 +0000171GenericValue lle_X_calloc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000172 assert(Args.size() == 2 && "calloc expects two arguments!");
Reid Spencer97e0c222007-03-30 16:41:50 +0000173 assert(isa<PointerType>(FT->getReturnType()) && "calloc must return pointer");
Reid Spencerbfcd5992007-03-06 03:08:12 +0000174 return PTOGV(calloc(Args[0].IntVal.getZExtValue(),
175 Args[1].IntVal.getZExtValue()));
Chris Lattner957d62a2003-04-23 19:55:24 +0000176}
177
Reid Spencer97e0c222007-03-30 16:41:50 +0000178// void *calloc(uint, uint)
179GenericValue lle_X_realloc(FunctionType *FT, const vector<GenericValue> &Args) {
180 assert(Args.size() == 2 && "calloc expects two arguments!");
181 assert(isa<PointerType>(FT->getReturnType()) &&"realloc must return pointer");
182 return PTOGV(realloc(GVTOP(Args[0]), Args[1].IntVal.getZExtValue()));
183}
184
Chris Lattnerc2593162001-10-27 08:28:11 +0000185// void free(void *)
Reid Spencer97e0c222007-03-30 16:41:50 +0000186GenericValue lle_X_free(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000187 assert(Args.size() == 1);
Chris Lattnerb1118742003-01-13 00:59:47 +0000188 free(GVTOP(Args[0]));
Chris Lattnerc2593162001-10-27 08:28:11 +0000189 return GenericValue();
190}
191
Chris Lattner782b9392001-11-26 18:18:18 +0000192// int atoi(char *)
Reid Spencer97e0c222007-03-30 16:41:50 +0000193GenericValue lle_X_atoi(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000194 assert(Args.size() == 1);
195 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000196 GV.IntVal = APInt(32, atoi((char*)GVTOP(Args[0])));
Chris Lattner782b9392001-11-26 18:18:18 +0000197 return GV;
198}
199
Chris Lattnerc2593162001-10-27 08:28:11 +0000200// double pow(double, double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000201GenericValue lle_X_pow(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000202 assert(Args.size() == 2);
Chris Lattnerc2593162001-10-27 08:28:11 +0000203 GenericValue GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000204 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
Chris Lattnerc2593162001-10-27 08:28:11 +0000205 return GV;
206}
207
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000208// double sin(double)
209GenericValue lle_X_sin(FunctionType *FT, const vector<GenericValue> &Args) {
210 assert(Args.size() == 1);
211 GenericValue GV;
212 GV.DoubleVal = sin(Args[0].DoubleVal);
213 return GV;
214}
215
216// double cos(double)
217GenericValue lle_X_cos(FunctionType *FT, const vector<GenericValue> &Args) {
218 assert(Args.size() == 1);
219 GenericValue GV;
220 GV.DoubleVal = cos(Args[0].DoubleVal);
221 return GV;
222}
223
Chris Lattner34dd24b2002-02-18 19:06:25 +0000224// double exp(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000225GenericValue lle_X_exp(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner34dd24b2002-02-18 19:06:25 +0000226 assert(Args.size() == 1);
227 GenericValue GV;
228 GV.DoubleVal = exp(Args[0].DoubleVal);
229 return GV;
230}
231
Chris Lattnerc063d382001-11-06 21:52:18 +0000232// double sqrt(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000233GenericValue lle_X_sqrt(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc063d382001-11-06 21:52:18 +0000234 assert(Args.size() == 1);
235 GenericValue GV;
236 GV.DoubleVal = sqrt(Args[0].DoubleVal);
237 return GV;
238}
Chris Lattnerc2593162001-10-27 08:28:11 +0000239
Chris Lattner86790052001-11-06 22:53:25 +0000240// double log(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000241GenericValue lle_X_log(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000242 assert(Args.size() == 1);
243 GenericValue GV;
244 GV.DoubleVal = log(Args[0].DoubleVal);
245 return GV;
246}
247
Chris Lattner782b9392001-11-26 18:18:18 +0000248// double floor(double)
Reid Spencer97e0c222007-03-30 16:41:50 +0000249GenericValue lle_X_floor(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000250 assert(Args.size() == 1);
251 GenericValue GV;
252 GV.DoubleVal = floor(Args[0].DoubleVal);
253 return GV;
254}
255
Reid Spencerabec8f92004-10-27 23:03:44 +0000256#ifdef HAVE_RAND48
257
Chris Lattner86790052001-11-06 22:53:25 +0000258// double drand48()
Reid Spencer97e0c222007-03-30 16:41:50 +0000259GenericValue lle_X_drand48(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner86790052001-11-06 22:53:25 +0000260 assert(Args.size() == 0);
261 GenericValue GV;
262 GV.DoubleVal = drand48();
263 return GV;
264}
265
Chris Lattner1b600142001-11-13 05:46:08 +0000266// long lrand48()
Reid Spencer97e0c222007-03-30 16:41:50 +0000267GenericValue lle_X_lrand48(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000268 assert(Args.size() == 0);
269 GenericValue GV;
Duncan Sands22ad1d72007-12-10 14:43:10 +0000270 GV.IntVal = APInt(32, lrand48());
Chris Lattner1b600142001-11-13 05:46:08 +0000271 return GV;
272}
273
274// void srand48(long)
Reid Spencer97e0c222007-03-30 16:41:50 +0000275GenericValue lle_X_srand48(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner1b600142001-11-13 05:46:08 +0000276 assert(Args.size() == 1);
Duncan Sands22ad1d72007-12-10 14:43:10 +0000277 srand48(Args[0].IntVal.getZExtValue());
Chris Lattner1b600142001-11-13 05:46:08 +0000278 return GenericValue();
279}
280
Reid Spencerabec8f92004-10-27 23:03:44 +0000281#endif
282
283// int rand()
Reid Spencer97e0c222007-03-30 16:41:50 +0000284GenericValue lle_X_rand(FunctionType *FT, const vector<GenericValue> &Args) {
Reid Spencerabec8f92004-10-27 23:03:44 +0000285 assert(Args.size() == 0);
286 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000287 GV.IntVal = APInt(32, rand());
Reid Spencerabec8f92004-10-27 23:03:44 +0000288 return GV;
289}
290
Chris Lattner782b9392001-11-26 18:18:18 +0000291// void srand(uint)
Reid Spencer97e0c222007-03-30 16:41:50 +0000292GenericValue lle_X_srand(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner782b9392001-11-26 18:18:18 +0000293 assert(Args.size() == 1);
Reid Spencerbfcd5992007-03-06 03:08:12 +0000294 srand(Args[0].IntVal.getZExtValue());
Chris Lattner782b9392001-11-26 18:18:18 +0000295 return GenericValue();
296}
Chris Lattner86790052001-11-06 22:53:25 +0000297
Chris Lattnerb1118742003-01-13 00:59:47 +0000298// int puts(const char*)
Reid Spencer97e0c222007-03-30 16:41:50 +0000299GenericValue lle_X_puts(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerb1118742003-01-13 00:59:47 +0000300 assert(Args.size() == 1);
301 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000302 GV.IntVal = APInt(32, puts((char*)GVTOP(Args[0])));
Chris Lattnerb1118742003-01-13 00:59:47 +0000303 return GV;
304}
305
Chris Lattnere7c6f722001-12-13 00:43:47 +0000306// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
307// output useful.
Reid Spencer97e0c222007-03-30 16:41:50 +0000308GenericValue lle_X_sprintf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerb1118742003-01-13 00:59:47 +0000309 char *OutputBuffer = (char *)GVTOP(Args[0]);
310 const char *FmtStr = (const char *)GVTOP(Args[1]);
Chris Lattnere7c6f722001-12-13 00:43:47 +0000311 unsigned ArgNo = 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000312
313 // printf should return # chars printed. This is completely incorrect, but
314 // close enough for now.
Reid Spencerbfcd5992007-03-06 03:08:12 +0000315 GenericValue GV;
316 GV.IntVal = APInt(32, strlen(FmtStr));
Chris Lattner08845a22001-10-29 20:27:45 +0000317 while (1) {
318 switch (*FmtStr) {
319 case 0: return GV; // Null terminator...
320 default: // Normal nonspecial character
Chris Lattnere7c6f722001-12-13 00:43:47 +0000321 sprintf(OutputBuffer++, "%c", *FmtStr++);
Chris Lattner08845a22001-10-29 20:27:45 +0000322 break;
323 case '\\': { // Handle escape codes
Chris Lattnere7c6f722001-12-13 00:43:47 +0000324 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
325 FmtStr += 2; OutputBuffer += 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000326 break;
327 }
328 case '%': { // Handle format specifiers
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000329 char FmtBuf[100] = "", Buffer[1000] = "";
330 char *FB = FmtBuf;
331 *FB++ = *FmtStr++;
332 char Last = *FB++ = *FmtStr++;
333 unsigned HowLong = 0;
334 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
335 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
336 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
337 Last != 'p' && Last != 's' && Last != '%') {
338 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
339 Last = *FB++ = *FmtStr++;
Chris Lattner08845a22001-10-29 20:27:45 +0000340 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000341 *FB = 0;
Misha Brukmand1c881a2005-04-21 22:43:08 +0000342
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000343 switch (Last) {
344 case '%':
345 sprintf(Buffer, FmtBuf); break;
346 case 'c':
Reid Spencerbfcd5992007-03-06 03:08:12 +0000347 sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
348 break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000349 case 'd': case 'i':
350 case 'u': case 'o':
351 case 'x': case 'X':
Chris Lattner69ab7a82002-08-02 23:08:32 +0000352 if (HowLong >= 1) {
Chris Lattner1543e402003-08-24 14:02:47 +0000353 if (HowLong == 1 &&
Chris Lattnerfe854032006-08-16 01:24:12 +0000354 TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 &&
Reid Spencer19b7e0e2006-05-24 19:21:13 +0000355 sizeof(long) < sizeof(int64_t)) {
Chris Lattner69ab7a82002-08-02 23:08:32 +0000356 // Make sure we use %lld with a 64 bit argument because we might be
357 // compiling LLI on a 32 bit compiler.
358 unsigned Size = strlen(FmtBuf);
359 FmtBuf[Size] = FmtBuf[Size-1];
360 FmtBuf[Size+1] = 0;
361 FmtBuf[Size-1] = 'l';
362 }
Reid Spencerbfcd5992007-03-06 03:08:12 +0000363 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
Chris Lattner69ab7a82002-08-02 23:08:32 +0000364 } else
Reid Spencerbfcd5992007-03-06 03:08:12 +0000365 sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
366 break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000367 case 'e': case 'E': case 'g': case 'G': case 'f':
368 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
369 case 'p':
Chris Lattnerb1118742003-01-13 00:59:47 +0000370 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
Misha Brukmand1c881a2005-04-21 22:43:08 +0000371 case 's':
Chris Lattnerb1118742003-01-13 00:59:47 +0000372 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
Bill Wendlinge8156192006-12-07 01:30:32 +0000373 default: cerr << "<unknown printf code '" << *FmtStr << "'!>";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000374 ArgNo++; break;
Chris Lattner08845a22001-10-29 20:27:45 +0000375 }
Chris Lattnere7c6f722001-12-13 00:43:47 +0000376 strcpy(OutputBuffer, Buffer);
377 OutputBuffer += strlen(Buffer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000378 }
Chris Lattner08845a22001-10-29 20:27:45 +0000379 break;
380 }
Chris Lattner08845a22001-10-29 20:27:45 +0000381 }
Reid Spencerb3b07272007-04-21 17:11:45 +0000382 return GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000383}
384
Chris Lattnere7c6f722001-12-13 00:43:47 +0000385// int printf(sbyte *, ...) - a very rough implementation to make output useful.
Reid Spencer97e0c222007-03-30 16:41:50 +0000386GenericValue lle_X_printf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000387 char Buffer[10000];
388 vector<GenericValue> NewArgs;
Reid Spencerb3b07272007-04-21 17:11:45 +0000389 NewArgs.push_back(PTOGV((void*)&Buffer[0]));
Chris Lattnere7c6f722001-12-13 00:43:47 +0000390 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
Reid Spencer97e0c222007-03-30 16:41:50 +0000391 GenericValue GV = lle_X_sprintf(FT, NewArgs);
Bill Wendlinge8156192006-12-07 01:30:32 +0000392 cout << Buffer;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000393 return GV;
394}
395
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000396static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1,
397 void *Arg2, void *Arg3, void *Arg4, void *Arg5,
398 void *Arg6, void *Arg7, void *Arg8) {
399 void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 };
400
401 // Loop over the format string, munging read values as appropriate (performs
Misha Brukman5560c9d2003-08-18 14:43:39 +0000402 // byteswaps as necessary).
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000403 unsigned ArgNo = 0;
404 while (*Fmt) {
405 if (*Fmt++ == '%') {
406 // Read any flag characters that may be present...
407 bool Suppress = false;
408 bool Half = false;
409 bool Long = false;
410 bool LongLong = false; // long long or long double
411
412 while (1) {
413 switch (*Fmt++) {
414 case '*': Suppress = true; break;
415 case 'a': /*Allocate = true;*/ break; // We don't need to track this
416 case 'h': Half = true; break;
417 case 'l': Long = true; break;
418 case 'q':
419 case 'L': LongLong = true; break;
420 default:
421 if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs
422 goto Out;
423 }
424 }
425 Out:
426
427 // Read the conversion character
428 if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
429 unsigned Size = 0;
430 const Type *Ty = 0;
431
432 switch (Fmt[-1]) {
433 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
434 case 'd':
435 if (Long || LongLong) {
Reid Spencere49661b2006-12-31 05:51:36 +0000436 Size = 8; Ty = Type::Int64Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000437 } else if (Half) {
Reid Spencere49661b2006-12-31 05:51:36 +0000438 Size = 4; Ty = Type::Int16Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000439 } else {
Reid Spencere49661b2006-12-31 05:51:36 +0000440 Size = 4; Ty = Type::Int32Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000441 }
442 break;
443
444 case 'e': case 'g': case 'E':
445 case 'f':
446 if (Long || LongLong) {
447 Size = 8; Ty = Type::DoubleTy;
448 } else {
449 Size = 4; Ty = Type::FloatTy;
450 }
451 break;
452
453 case 's': case 'c': case '[': // No byteswap needed
454 Size = 1;
Reid Spencere49661b2006-12-31 05:51:36 +0000455 Ty = Type::Int8Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000456 break;
457
458 default: break;
459 }
460
461 if (Size) {
462 GenericValue GV;
463 void *Arg = Args[ArgNo++];
464 memcpy(&GV, Arg, Size);
465 TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
466 }
467 }
468 }
469 }
470}
471
Chris Lattner665ee882002-03-08 22:51:07 +0000472// int sscanf(const char *format, ...);
Reid Spencer97e0c222007-03-30 16:41:50 +0000473GenericValue lle_X_sscanf(FunctionType *FT, const vector<GenericValue> &args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000474 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
475
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000476 char *Args[10];
Chris Lattner665ee882002-03-08 22:51:07 +0000477 for (unsigned i = 0; i < args.size(); ++i)
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000478 Args[i] = (char*)GVTOP(args[i]);
Chris Lattner665ee882002-03-08 22:51:07 +0000479
480 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000481 GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
482 Args[5], Args[6], Args[7], Args[8], Args[9]));
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000483 ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4],
484 Args[5], Args[6], Args[7], Args[8], Args[9], 0);
485 return GV;
486}
487
488// int scanf(const char *format, ...);
Reid Spencer97e0c222007-03-30 16:41:50 +0000489GenericValue lle_X_scanf(FunctionType *FT, const vector<GenericValue> &args) {
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000490 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
491
492 char *Args[10];
493 for (unsigned i = 0; i < args.size(); ++i)
494 Args[i] = (char*)GVTOP(args[i]);
495
496 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000497 GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
498 Args[5], Args[6], Args[7], Args[8], Args[9]));
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000499 ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4],
500 Args[5], Args[6], Args[7], Args[8], Args[9]);
Chris Lattner665ee882002-03-08 22:51:07 +0000501 return GV;
502}
503
504
Chris Lattner295fe672002-01-23 21:38:07 +0000505// int clock(void) - Profiling implementation
Reid Spencer97e0c222007-03-30 16:41:50 +0000506GenericValue lle_i_clock(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner638559a2005-01-16 01:31:31 +0000507 extern unsigned int clock(void);
Reid Spencerbfcd5992007-03-06 03:08:12 +0000508 GenericValue GV;
509 GV.IntVal = APInt(32, clock());
Chris Lattner295fe672002-01-23 21:38:07 +0000510 return GV;
511}
Chris Lattnere7c6f722001-12-13 00:43:47 +0000512
Chris Lattner957d62a2003-04-23 19:55:24 +0000513
514//===----------------------------------------------------------------------===//
515// String Functions...
516//===----------------------------------------------------------------------===//
517
518// int strcmp(const char *S1, const char *S2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000519GenericValue lle_X_strcmp(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000520 assert(Args.size() == 2);
521 GenericValue Ret;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000522 Ret.IntVal = APInt(32, strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
Chris Lattner957d62a2003-04-23 19:55:24 +0000523 return Ret;
524}
525
526// char *strcat(char *Dest, const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000527GenericValue lle_X_strcat(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000528 assert(Args.size() == 2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000529 assert(isa<PointerType>(FT->getReturnType()) &&"strcat must return pointer");
Chris Lattner957d62a2003-04-23 19:55:24 +0000530 return PTOGV(strcat((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
531}
532
533// char *strcpy(char *Dest, const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000534GenericValue lle_X_strcpy(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000535 assert(Args.size() == 2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000536 assert(isa<PointerType>(FT->getReturnType()) &&"strcpy must return pointer");
Chris Lattner957d62a2003-04-23 19:55:24 +0000537 return PTOGV(strcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
538}
539
Brian Gaeke3c66c352004-05-01 06:42:15 +0000540static GenericValue size_t_to_GV (size_t n) {
541 GenericValue Ret;
542 if (sizeof (size_t) == sizeof (uint64_t)) {
Reid Spencerbfcd5992007-03-06 03:08:12 +0000543 Ret.IntVal = APInt(64, n);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000544 } else {
545 assert (sizeof (size_t) == sizeof (unsigned int));
Reid Spencerbfcd5992007-03-06 03:08:12 +0000546 Ret.IntVal = APInt(32, n);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000547 }
548 return Ret;
549}
550
Misha Brukmand1c881a2005-04-21 22:43:08 +0000551static size_t GV_to_size_t (GenericValue GV) {
Brian Gaeke3c66c352004-05-01 06:42:15 +0000552 size_t count;
553 if (sizeof (size_t) == sizeof (uint64_t)) {
Reid Spencerbfcd5992007-03-06 03:08:12 +0000554 count = (size_t)GV.IntVal.getZExtValue();
Brian Gaeke3c66c352004-05-01 06:42:15 +0000555 } else {
556 assert (sizeof (size_t) == sizeof (unsigned int));
Reid Spencerbfcd5992007-03-06 03:08:12 +0000557 count = (size_t)GV.IntVal.getZExtValue();
Brian Gaeke3c66c352004-05-01 06:42:15 +0000558 }
559 return count;
560}
561
Brian Gaeke6c9d5822003-12-12 15:38:06 +0000562// size_t strlen(const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000563GenericValue lle_X_strlen(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000564 assert(Args.size() == 1);
Brian Gaeke6c9d5822003-12-12 15:38:06 +0000565 size_t strlenResult = strlen ((char *) GVTOP (Args[0]));
Brian Gaeke3c66c352004-05-01 06:42:15 +0000566 return size_t_to_GV (strlenResult);
Chris Lattner957d62a2003-04-23 19:55:24 +0000567}
568
Brian Gaeke70975ee2003-09-05 18:42:01 +0000569// char *strdup(const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000570GenericValue lle_X_strdup(FunctionType *FT, const vector<GenericValue> &Args) {
Brian Gaeke70975ee2003-09-05 18:42:01 +0000571 assert(Args.size() == 1);
Reid Spencer97e0c222007-03-30 16:41:50 +0000572 assert(isa<PointerType>(FT->getReturnType()) && "strdup must return pointer");
Brian Gaeke70975ee2003-09-05 18:42:01 +0000573 return PTOGV(strdup((char*)GVTOP(Args[0])));
574}
575
Chris Lattnerc8cff9e2003-04-25 18:23:38 +0000576// char *__strdup(const char *src);
Reid Spencer97e0c222007-03-30 16:41:50 +0000577GenericValue lle_X___strdup(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerc8cff9e2003-04-25 18:23:38 +0000578 assert(Args.size() == 1);
Reid Spencer97e0c222007-03-30 16:41:50 +0000579 assert(isa<PointerType>(FT->getReturnType()) &&"_strdup must return pointer");
Chris Lattnerc8cff9e2003-04-25 18:23:38 +0000580 return PTOGV(strdup((char*)GVTOP(Args[0])));
581}
582
Chris Lattner957d62a2003-04-23 19:55:24 +0000583// void *memset(void *S, int C, size_t N)
Reid Spencer97e0c222007-03-30 16:41:50 +0000584GenericValue lle_X_memset(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner957d62a2003-04-23 19:55:24 +0000585 assert(Args.size() == 3);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000586 size_t count = GV_to_size_t (Args[2]);
Reid Spencer97e0c222007-03-30 16:41:50 +0000587 assert(isa<PointerType>(FT->getReturnType()) && "memset must return pointer");
Reid Spencerbfcd5992007-03-06 03:08:12 +0000588 return PTOGV(memset(GVTOP(Args[0]), uint32_t(Args[1].IntVal.getZExtValue()),
589 count));
Chris Lattner957d62a2003-04-23 19:55:24 +0000590}
591
Chris Lattner5f311a72003-04-23 20:23:16 +0000592// void *memcpy(void *Dest, void *src, size_t Size);
Reid Spencer97e0c222007-03-30 16:41:50 +0000593GenericValue lle_X_memcpy(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner5f311a72003-04-23 20:23:16 +0000594 assert(Args.size() == 3);
Reid Spencer97e0c222007-03-30 16:41:50 +0000595 assert(isa<PointerType>(FT->getReturnType()) && "memcpy must return pointer");
Brian Gaeke3c66c352004-05-01 06:42:15 +0000596 size_t count = GV_to_size_t (Args[2]);
597 return PTOGV(memcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));
Chris Lattner5f311a72003-04-23 20:23:16 +0000598}
Chris Lattner957d62a2003-04-23 19:55:24 +0000599
Chris Lattner665ee882002-03-08 22:51:07 +0000600//===----------------------------------------------------------------------===//
601// IO Functions...
602//===----------------------------------------------------------------------===//
603
Chris Lattner005cbce2002-10-02 21:12:13 +0000604// getFILE - Turn a pointer in the host address space into a legit pointer in
Reid Spencer0098bdf2004-05-25 08:46:15 +0000605// the interpreter address space. This is an identity transformation.
606#define getFILE(ptr) ((FILE*)ptr)
Chris Lattner005cbce2002-10-02 21:12:13 +0000607
Chris Lattner665ee882002-03-08 22:51:07 +0000608// FILE *fopen(const char *filename, const char *mode);
Reid Spencer97e0c222007-03-30 16:41:50 +0000609GenericValue lle_X_fopen(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000610 assert(Args.size() == 2);
Reid Spencer97e0c222007-03-30 16:41:50 +0000611 assert(isa<PointerType>(FT->getReturnType()) && "fopen must return pointer");
Chris Lattnerb1118742003-01-13 00:59:47 +0000612 return PTOGV(fopen((const char *)GVTOP(Args[0]),
Misha Brukman3c944972005-04-22 04:08:30 +0000613 (const char *)GVTOP(Args[1])));
Chris Lattner665ee882002-03-08 22:51:07 +0000614}
615
616// int fclose(FILE *F);
Reid Spencer97e0c222007-03-30 16:41:50 +0000617GenericValue lle_X_fclose(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000618 assert(Args.size() == 1);
619 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000620 GV.IntVal = APInt(32, fclose(getFILE(GVTOP(Args[0]))));
Chris Lattner665ee882002-03-08 22:51:07 +0000621 return GV;
622}
623
Chris Lattner25f6f372002-11-08 19:10:26 +0000624// int feof(FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000625GenericValue lle_X_feof(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner25f6f372002-11-08 19:10:26 +0000626 assert(Args.size() == 1);
627 GenericValue GV;
628
Reid Spencerbfcd5992007-03-06 03:08:12 +0000629 GV.IntVal = APInt(32, feof(getFILE(GVTOP(Args[0]))));
Chris Lattner25f6f372002-11-08 19:10:26 +0000630 return GV;
631}
632
Chris Lattner665ee882002-03-08 22:51:07 +0000633// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000634GenericValue lle_X_fread(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000635 assert(Args.size() == 4);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000636 size_t result;
Chris Lattner665ee882002-03-08 22:51:07 +0000637
Brian Gaeke3c66c352004-05-01 06:42:15 +0000638 result = fread((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
639 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
640 return size_t_to_GV (result);
Chris Lattner665ee882002-03-08 22:51:07 +0000641}
642
643// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000644GenericValue lle_X_fwrite(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000645 assert(Args.size() == 4);
Brian Gaeke3c66c352004-05-01 06:42:15 +0000646 size_t result;
Chris Lattner665ee882002-03-08 22:51:07 +0000647
Brian Gaeke3c66c352004-05-01 06:42:15 +0000648 result = fwrite((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
649 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
650 return size_t_to_GV (result);
Chris Lattner665ee882002-03-08 22:51:07 +0000651}
652
653// char *fgets(char *s, int n, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000654GenericValue lle_X_fgets(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000655 assert(Args.size() == 3);
Dan Gohmanded2b0d2007-12-14 15:41:34 +0000656 return PTOGV(fgets((char*)GVTOP(Args[0]), Args[1].IntVal.getZExtValue(),
Misha Brukman3c944972005-04-22 04:08:30 +0000657 getFILE(GVTOP(Args[2]))));
Chris Lattner665ee882002-03-08 22:51:07 +0000658}
659
Chris Lattnera4479cd2002-11-07 19:33:50 +0000660// FILE *freopen(const char *path, const char *mode, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000661GenericValue lle_X_freopen(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnera4479cd2002-11-07 19:33:50 +0000662 assert(Args.size() == 3);
Reid Spencer97e0c222007-03-30 16:41:50 +0000663 assert(isa<PointerType>(FT->getReturnType()) &&"freopen must return pointer");
Chris Lattnerb1118742003-01-13 00:59:47 +0000664 return PTOGV(freopen((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]),
Misha Brukman3c944972005-04-22 04:08:30 +0000665 getFILE(GVTOP(Args[2]))));
Chris Lattnera4479cd2002-11-07 19:33:50 +0000666}
667
Chris Lattner665ee882002-03-08 22:51:07 +0000668// int fflush(FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000669GenericValue lle_X_fflush(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000670 assert(Args.size() == 1);
671 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000672 GV.IntVal = APInt(32, fflush(getFILE(GVTOP(Args[0]))));
Chris Lattner005cbce2002-10-02 21:12:13 +0000673 return GV;
674}
675
676// int getc(FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000677GenericValue lle_X_getc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner005cbce2002-10-02 21:12:13 +0000678 assert(Args.size() == 1);
679 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000680 GV.IntVal = APInt(32, getc(getFILE(GVTOP(Args[0]))));
Chris Lattner665ee882002-03-08 22:51:07 +0000681 return GV;
682}
683
Chris Lattnerf87a1982003-04-23 19:20:50 +0000684// int _IO_getc(FILE *stream);
685GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) {
686 return lle_X_getc(F, Args);
687}
688
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000689// int fputc(int C, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000690GenericValue lle_X_fputc(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000691 assert(Args.size() == 2);
692 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000693 GV.IntVal = APInt(32, fputc(Args[0].IntVal.getZExtValue(),
694 getFILE(GVTOP(Args[1]))));
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000695 return GV;
696}
697
698// int ungetc(int C, FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000699GenericValue lle_X_ungetc(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, ungetc(Args[0].IntVal.getZExtValue(),
703 getFILE(GVTOP(Args[1]))));
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000704 return GV;
705}
706
Brian Gaeke59108d32004-06-16 02:56:40 +0000707// int ferror (FILE *stream);
Reid Spencer97e0c222007-03-30 16:41:50 +0000708GenericValue lle_X_ferror(FunctionType *FT, const vector<GenericValue> &Args) {
Brian Gaeke59108d32004-06-16 02:56:40 +0000709 assert(Args.size() == 1);
710 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000711 GV.IntVal = APInt(32, ferror (getFILE(GVTOP(Args[0]))));
Brian Gaeke59108d32004-06-16 02:56:40 +0000712 return GV;
713}
714
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000715// int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
716// useful.
Reid Spencer97e0c222007-03-30 16:41:50 +0000717GenericValue lle_X_fprintf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner9dbf6dd2003-04-21 22:43:20 +0000718 assert(Args.size() >= 2);
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000719 char Buffer[10000];
720 vector<GenericValue> NewArgs;
Chris Lattnerb1118742003-01-13 00:59:47 +0000721 NewArgs.push_back(PTOGV(Buffer));
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000722 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
Reid Spencer97e0c222007-03-30 16:41:50 +0000723 GenericValue GV = lle_X_sprintf(FT, NewArgs);
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000724
Chris Lattnerb1118742003-01-13 00:59:47 +0000725 fputs(Buffer, getFILE(GVTOP(Args[0])));
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000726 return GV;
727}
728
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000729// int __cxa_guard_acquire (__guard *g);
730GenericValue lle_X___cxa_guard_acquire(FunctionType *FT,
731 const vector<GenericValue> &Args) {
732 assert(Args.size() == 1);
733 GenericValue GV;
Zhou Sheng621dead2007-12-12 06:16:47 +0000734#ifdef __linux__
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000735 GV.IntVal = APInt(32, __cxxabiv1::__cxa_guard_acquire (
736 (__cxxabiv1::__guard*)GVTOP(Args[0])));
Zhou Sheng621dead2007-12-12 06:16:47 +0000737#else
738 assert(0 && "Can't call __cxa_guard_acquire on this platform");
739#endif
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000740 return GV;
741}
742
743// void __cxa_guard_release (__guard *g);
744GenericValue lle_X___cxa_guard_release(FunctionType *FT,
745 const vector<GenericValue> &Args) {
746 assert(Args.size() == 1);
Zhou Sheng621dead2007-12-12 06:16:47 +0000747#ifdef __linux__
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000748 __cxxabiv1::__cxa_guard_release ((__cxxabiv1::__guard*)GVTOP(Args[0]));
Zhou Sheng621dead2007-12-12 06:16:47 +0000749#else
750 assert(0 && "Can't call __cxa_guard_release on this platform");
751#endif
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000752 return GenericValue();
753}
754
Chris Lattner7720c8e2001-09-10 04:50:17 +0000755} // End extern "C"
Chris Lattner4721f132001-10-30 20:28:00 +0000756
757
Chris Lattnerda82ed52003-05-08 16:18:31 +0000758void Interpreter::initializeExternalFunctions() {
Reid Spencerb3b07272007-04-21 17:11:45 +0000759 FuncNames["lle_X_putchar"] = lle_X_putchar;
Reid Spencercc442ca2007-05-19 01:36:17 +0000760 FuncNames["lle_X__IO_putc"] = lle_X__IO_putc;
Chris Lattner0f279b22001-11-03 10:15:32 +0000761 FuncNames["lle_X_exit"] = lle_X_exit;
Chris Lattner1ee34a52002-05-20 21:17:16 +0000762 FuncNames["lle_X_abort"] = lle_X_abort;
Chris Lattner0f279b22001-11-03 10:15:32 +0000763 FuncNames["lle_X_malloc"] = lle_X_malloc;
Chris Lattner957d62a2003-04-23 19:55:24 +0000764 FuncNames["lle_X_calloc"] = lle_X_calloc;
Reid Spencer97e0c222007-03-30 16:41:50 +0000765 FuncNames["lle_X_realloc"] = lle_X_realloc;
Chris Lattner0f279b22001-11-03 10:15:32 +0000766 FuncNames["lle_X_free"] = lle_X_free;
Chris Lattner782b9392001-11-26 18:18:18 +0000767 FuncNames["lle_X_atoi"] = lle_X_atoi;
Chris Lattner0f279b22001-11-03 10:15:32 +0000768 FuncNames["lle_X_pow"] = lle_X_pow;
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000769 FuncNames["lle_X_sin"] = lle_X_sin;
770 FuncNames["lle_X_cos"] = lle_X_cos;
Chris Lattner34dd24b2002-02-18 19:06:25 +0000771 FuncNames["lle_X_exp"] = lle_X_exp;
Chris Lattner1b600142001-11-13 05:46:08 +0000772 FuncNames["lle_X_log"] = lle_X_log;
Chris Lattner782b9392001-11-26 18:18:18 +0000773 FuncNames["lle_X_floor"] = lle_X_floor;
774 FuncNames["lle_X_srand"] = lle_X_srand;
Reid Spencerabec8f92004-10-27 23:03:44 +0000775 FuncNames["lle_X_rand"] = lle_X_rand;
776#ifdef HAVE_RAND48
Chris Lattner1b600142001-11-13 05:46:08 +0000777 FuncNames["lle_X_drand48"] = lle_X_drand48;
778 FuncNames["lle_X_srand48"] = lle_X_srand48;
779 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
Reid Spencerabec8f92004-10-27 23:03:44 +0000780#endif
Chris Lattnerc063d382001-11-06 21:52:18 +0000781 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
Chris Lattnerb1118742003-01-13 00:59:47 +0000782 FuncNames["lle_X_puts"] = lle_X_puts;
Chris Lattner0f279b22001-11-03 10:15:32 +0000783 FuncNames["lle_X_printf"] = lle_X_printf;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000784 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
Chris Lattner665ee882002-03-08 22:51:07 +0000785 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000786 FuncNames["lle_X_scanf"] = lle_X_scanf;
Chris Lattner295fe672002-01-23 21:38:07 +0000787 FuncNames["lle_i_clock"] = lle_i_clock;
Chris Lattner957d62a2003-04-23 19:55:24 +0000788
789 FuncNames["lle_X_strcmp"] = lle_X_strcmp;
790 FuncNames["lle_X_strcat"] = lle_X_strcat;
791 FuncNames["lle_X_strcpy"] = lle_X_strcpy;
792 FuncNames["lle_X_strlen"] = lle_X_strlen;
Chris Lattnerda82ed52003-05-08 16:18:31 +0000793 FuncNames["lle_X___strdup"] = lle_X___strdup;
Chris Lattner957d62a2003-04-23 19:55:24 +0000794 FuncNames["lle_X_memset"] = lle_X_memset;
Chris Lattner5f311a72003-04-23 20:23:16 +0000795 FuncNames["lle_X_memcpy"] = lle_X_memcpy;
Chris Lattner957d62a2003-04-23 19:55:24 +0000796
Chris Lattner665ee882002-03-08 22:51:07 +0000797 FuncNames["lle_X_fopen"] = lle_X_fopen;
798 FuncNames["lle_X_fclose"] = lle_X_fclose;
Chris Lattner25f6f372002-11-08 19:10:26 +0000799 FuncNames["lle_X_feof"] = lle_X_feof;
Chris Lattner665ee882002-03-08 22:51:07 +0000800 FuncNames["lle_X_fread"] = lle_X_fread;
801 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
802 FuncNames["lle_X_fgets"] = lle_X_fgets;
803 FuncNames["lle_X_fflush"] = lle_X_fflush;
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000804 FuncNames["lle_X_fgetc"] = lle_X_getc;
Chris Lattner005cbce2002-10-02 21:12:13 +0000805 FuncNames["lle_X_getc"] = lle_X_getc;
Chris Lattnerf87a1982003-04-23 19:20:50 +0000806 FuncNames["lle_X__IO_getc"] = lle_X__IO_getc;
Chris Lattnera5c0bfe2002-11-06 22:59:28 +0000807 FuncNames["lle_X_fputc"] = lle_X_fputc;
808 FuncNames["lle_X_ungetc"] = lle_X_ungetc;
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000809 FuncNames["lle_X_fprintf"] = lle_X_fprintf;
Chris Lattnera4479cd2002-11-07 19:33:50 +0000810 FuncNames["lle_X_freopen"] = lle_X_freopen;
Zhou Sheng6a7951c2007-12-12 04:55:43 +0000811
812 FuncNames["lle_X___cxa_guard_acquire"] = lle_X___cxa_guard_acquire;
813 FuncNames["lle_X____cxa_guard_release"] = lle_X___cxa_guard_release;
Chris Lattner4721f132001-10-30 20:28:00 +0000814}
Brian Gaeked0fde302003-11-11 22:41:34 +0000815