blob: 66a26cff3c63a1ffce7440ec59780536ff88155b [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//
Tanya Lattner32aaee62009-01-22 20:09:20 +000013// External functions in the interpreter are implemented by
14// 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>
Duncan Sands4520dd22008-10-08 07:23:46 +000030#include <cstdio>
Misha Brukmanb8d15b22003-10-14 21:42:11 +000031#include <map>
Jeff Cohen97af7512006-12-02 02:22:01 +000032#include <cmath>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000033#include <cstring>
Zhou Sheng621dead2007-12-12 06:16:47 +000034
Tanya Lattner32aaee62009-01-22 20:09:20 +000035#ifdef __linux__
36#include <cxxabi.h>
Zhou Sheng621dead2007-12-12 06:16:47 +000037#endif
38
Tanya Lattner32aaee62009-01-22 20:09:20 +000039using std::vector;
40
Chris Lattnerf7a743d2003-12-14 23:25:48 +000041using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000042
Tanya Lattner32aaee62009-01-22 20:09:20 +000043typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
44static ManagedStatic<std::map<const Function *, ExFunc> > Functions;
Chris Lattner697954c2002-01-20 22:54:45 +000045static std::map<std::string, ExFunc> FuncNames;
Chris Lattner7720c8e2001-09-10 04:50:17 +000046
Chris Lattnere43db882001-10-27 04:15:57 +000047static Interpreter *TheInterpreter;
48
Chris Lattner7720c8e2001-09-10 04:50:17 +000049static char getTypeID(const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000050 switch (Ty->getTypeID()) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000051 case Type::VoidTyID: return 'V';
Reid Spencera54b7cb2007-01-12 07:05:14 +000052 case Type::IntegerTyID:
53 switch (cast<IntegerType>(Ty)->getBitWidth()) {
54 case 1: return 'o';
55 case 8: return 'B';
56 case 16: return 'S';
57 case 32: return 'I';
58 case 64: return 'L';
59 default: return 'N';
60 }
Chris Lattner7720c8e2001-09-10 04:50:17 +000061 case Type::FloatTyID: return 'F';
62 case Type::DoubleTyID: return 'D';
63 case Type::PointerTyID: return 'P';
Reid Spencere49661b2006-12-31 05:51:36 +000064 case Type::FunctionTyID:return 'M';
Chris Lattner7720c8e2001-09-10 04:50:17 +000065 case Type::StructTyID: return 'T';
66 case Type::ArrayTyID: return 'A';
67 case Type::OpaqueTyID: return 'O';
68 default: return 'U';
69 }
70}
71
Anton Korobeynikov42346f52007-07-30 23:03:25 +000072// Try to find address of external function given a Function object.
73// Please note, that interpreter doesn't know how to assemble a
74// real call in general case (this is JIT job), that's why it assumes,
75// that all external functions has the same (and pretty "general") signature.
76// The typical example of such functions are "lle_X_" ones.
Brian Gaeke58a6faa2003-10-10 17:03:10 +000077static ExFunc lookupFunction(const Function *F) {
Chris Lattner7720c8e2001-09-10 04:50:17 +000078 // Function not found, look it up... start by figuring out what the
79 // composite function name should be.
Chris Lattner697954c2002-01-20 22:54:45 +000080 std::string ExtName = "lle_";
Brian Gaeke58a6faa2003-10-10 17:03:10 +000081 const FunctionType *FT = F->getFunctionType();
82 for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
83 ExtName += getTypeID(FT->getContainedType(i));
84 ExtName += "_" + F->getName();
Chris Lattner7720c8e2001-09-10 04:50:17 +000085
Chris Lattner4721f132001-10-30 20:28:00 +000086 ExFunc FnPtr = FuncNames[ExtName];
87 if (FnPtr == 0)
Brian Gaeke58a6faa2003-10-10 17:03:10 +000088 FnPtr = FuncNames["lle_X_"+F->getName()];
Chris Lattner7720c8e2001-09-10 04:50:17 +000089 if (FnPtr == 0) // Try calling a generic function... if it exists...
Chris Lattner26e6e102006-06-01 17:27:11 +000090 FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(
Reid Spencerdf5a37e2004-11-29 14:11:29 +000091 ("lle_X_"+F->getName()).c_str());
Tanya Lattner32aaee62009-01-22 20:09:20 +000092 if (FnPtr == 0)
93 FnPtr = (ExFunc)(intptr_t)
94 sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
Chris Lattner7720c8e2001-09-10 04:50:17 +000095 if (FnPtr != 0)
Tanya Lattner32aaee62009-01-22 20:09:20 +000096 Functions->insert(std::make_pair(F, FnPtr)); // Cache for later
Chris Lattner7720c8e2001-09-10 04:50:17 +000097 return FnPtr;
98}
99
Chris Lattner4e7dd8f2005-01-21 19:59:37 +0000100GenericValue Interpreter::callExternalFunction(Function *F,
Chris Lattner44edb6b2003-05-14 14:21:30 +0000101 const std::vector<GenericValue> &ArgVals) {
Chris Lattnere43db882001-10-27 04:15:57 +0000102 TheInterpreter = this;
103
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000104 // Do a lookup to see if the function is in our cache... this should just be a
Misha Brukmand5d96b92003-10-10 17:42:19 +0000105 // deferred annotation!
Tanya Lattner32aaee62009-01-22 20:09:20 +0000106 std::map<const Function *, ExFunc>::iterator FI = Functions->find(F);
107 ExFunc Fn = (FI == Functions->end()) ? lookupFunction(F) : FI->second;
108 if (Fn == 0) {
109 cerr << "Tried to execute an unknown external function: "
110 << F->getType()->getDescription() << " " << F->getName() << "\n";
111 if (F->getName() == "__main")
112 return GenericValue();
113 abort();
Chris Lattner7720c8e2001-09-10 04:50:17 +0000114 }
115
Tanya Lattner32aaee62009-01-22 20:09:20 +0000116 // TODO: FIXME when types are not const!
117 GenericValue Result = Fn(const_cast<FunctionType*>(F->getFunctionType()),
118 ArgVals);
119 return Result;
Chris Lattner7720c8e2001-09-10 04:50:17 +0000120}
121
122
123//===----------------------------------------------------------------------===//
Chris Lattnerb408b122002-03-29 03:57:15 +0000124// Functions "exported" to the running application...
Chris Lattner7720c8e2001-09-10 04:50:17 +0000125//
126extern "C" { // Don't add C++ manglings to llvm mangling :)
127
Tanya Lattner32aaee62009-01-22 20:09:20 +0000128// void putchar(ubyte)
129GenericValue lle_X_putchar(FunctionType *FT, const vector<GenericValue> &Args){
130 cout << ((char)Args[0].IntVal.getZExtValue()) << std::flush;
131 return Args[0];
132}
133
134// void _IO_putc(int c, FILE* fp)
135GenericValue lle_X__IO_putc(FunctionType *FT, const vector<GenericValue> &Args){
136#ifdef __linux__
137 _IO_putc((char)Args[0].IntVal.getZExtValue(), (FILE*) Args[1].PointerVal);
138#else
139 assert(0 && "Can't call _IO_putc on this platform");
140#endif
141 return Args[0];
142}
143
Chris Lattner44edb6b2003-05-14 14:21:30 +0000144// void atexit(Function*)
Tanya Lattner32aaee62009-01-22 20:09:20 +0000145GenericValue lle_X_atexit(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner44edb6b2003-05-14 14:21:30 +0000146 assert(Args.size() == 1);
147 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
148 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000149 GV.IntVal = 0;
Chris Lattner44edb6b2003-05-14 14:21:30 +0000150 return GV;
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000151}
152
Chris Lattner005cbce2002-10-02 21:12:13 +0000153// void exit(int)
Tanya Lattner32aaee62009-01-22 20:09:20 +0000154GenericValue lle_X_exit(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnere43db882001-10-27 04:15:57 +0000155 TheInterpreter->exitCalled(Args[0]);
156 return GenericValue();
157}
158
Chris Lattner005cbce2002-10-02 21:12:13 +0000159// void abort(void)
Tanya Lattner32aaee62009-01-22 20:09:20 +0000160GenericValue lle_X_abort(FunctionType *FT, const vector<GenericValue> &Args) {
Brian Gaekeb56a6bc2003-11-05 01:18:49 +0000161 raise (SIGABRT);
Chris Lattner1ee34a52002-05-20 21:17:16 +0000162 return GenericValue();
163}
164
Tanya Lattner32aaee62009-01-22 20:09:20 +0000165// void *malloc(uint)
166GenericValue lle_X_malloc(FunctionType *FT, const vector<GenericValue> &Args) {
167 assert(Args.size() == 1 && "Malloc expects one argument!");
168 assert(isa<PointerType>(FT->getReturnType()) && "malloc must return pointer");
169 return PTOGV(malloc(Args[0].IntVal.getZExtValue()));
170}
171
172// void *calloc(uint, uint)
173GenericValue lle_X_calloc(FunctionType *FT, const vector<GenericValue> &Args) {
174 assert(Args.size() == 2 && "calloc expects two arguments!");
175 assert(isa<PointerType>(FT->getReturnType()) && "calloc must return pointer");
176 return PTOGV(calloc(Args[0].IntVal.getZExtValue(),
177 Args[1].IntVal.getZExtValue()));
178}
179
180// void *calloc(uint, uint)
181GenericValue lle_X_realloc(FunctionType *FT, const vector<GenericValue> &Args) {
182 assert(Args.size() == 2 && "calloc expects two arguments!");
183 assert(isa<PointerType>(FT->getReturnType()) &&"realloc must return pointer");
184 return PTOGV(realloc(GVTOP(Args[0]), Args[1].IntVal.getZExtValue()));
185}
186
187// void free(void *)
188GenericValue lle_X_free(FunctionType *FT, const vector<GenericValue> &Args) {
189 assert(Args.size() == 1);
190 free(GVTOP(Args[0]));
191 return GenericValue();
192}
193
194// int atoi(char *)
195GenericValue lle_X_atoi(FunctionType *FT, const vector<GenericValue> &Args) {
196 assert(Args.size() == 1);
197 GenericValue GV;
198 GV.IntVal = APInt(32, atoi((char*)GVTOP(Args[0])));
199 return GV;
200}
201
202// double pow(double, double)
203GenericValue lle_X_pow(FunctionType *FT, const vector<GenericValue> &Args) {
204 assert(Args.size() == 2);
205 GenericValue GV;
206 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
207 return GV;
208}
209
210// double sin(double)
211GenericValue lle_X_sin(FunctionType *FT, const vector<GenericValue> &Args) {
212 assert(Args.size() == 1);
213 GenericValue GV;
214 GV.DoubleVal = sin(Args[0].DoubleVal);
215 return GV;
216}
217
218// double cos(double)
219GenericValue lle_X_cos(FunctionType *FT, const vector<GenericValue> &Args) {
220 assert(Args.size() == 1);
221 GenericValue GV;
222 GV.DoubleVal = cos(Args[0].DoubleVal);
223 return GV;
224}
225
226// double exp(double)
227GenericValue lle_X_exp(FunctionType *FT, const vector<GenericValue> &Args) {
228 assert(Args.size() == 1);
229 GenericValue GV;
230 GV.DoubleVal = exp(Args[0].DoubleVal);
231 return GV;
232}
233
234// double sqrt(double)
235GenericValue lle_X_sqrt(FunctionType *FT, const vector<GenericValue> &Args) {
236 assert(Args.size() == 1);
237 GenericValue GV;
238 GV.DoubleVal = sqrt(Args[0].DoubleVal);
239 return GV;
240}
241
242// double log(double)
243GenericValue lle_X_log(FunctionType *FT, const vector<GenericValue> &Args) {
244 assert(Args.size() == 1);
245 GenericValue GV;
246 GV.DoubleVal = log(Args[0].DoubleVal);
247 return GV;
248}
249
250// double floor(double)
251GenericValue lle_X_floor(FunctionType *FT, const vector<GenericValue> &Args) {
252 assert(Args.size() == 1);
253 GenericValue GV;
254 GV.DoubleVal = floor(Args[0].DoubleVal);
255 return GV;
256}
257
258#ifdef HAVE_RAND48
259
260// double drand48()
261GenericValue lle_X_drand48(FunctionType *FT, const vector<GenericValue> &Args) {
262 assert(Args.empty());
263 GenericValue GV;
264 GV.DoubleVal = drand48();
265 return GV;
266}
267
268// long lrand48()
269GenericValue lle_X_lrand48(FunctionType *FT, const vector<GenericValue> &Args) {
270 assert(Args.empty());
271 GenericValue GV;
272 GV.IntVal = APInt(32, lrand48());
273 return GV;
274}
275
276// void srand48(long)
277GenericValue lle_X_srand48(FunctionType *FT, const vector<GenericValue> &Args) {
278 assert(Args.size() == 1);
279 srand48(Args[0].IntVal.getZExtValue());
280 return GenericValue();
281}
282
283#endif
284
285// int rand()
286GenericValue lle_X_rand(FunctionType *FT, const vector<GenericValue> &Args) {
287 assert(Args.empty());
288 GenericValue GV;
289 GV.IntVal = APInt(32, rand());
290 return GV;
291}
292
293// void srand(uint)
294GenericValue lle_X_srand(FunctionType *FT, const vector<GenericValue> &Args) {
295 assert(Args.size() == 1);
296 srand(Args[0].IntVal.getZExtValue());
297 return GenericValue();
298}
299
300// int puts(const char*)
301GenericValue lle_X_puts(FunctionType *FT, const vector<GenericValue> &Args) {
302 assert(Args.size() == 1);
303 GenericValue GV;
304 GV.IntVal = APInt(32, puts((char*)GVTOP(Args[0])));
305 return GV;
306}
307
308// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
Chris Lattnere7c6f722001-12-13 00:43:47 +0000309// output useful.
Tanya Lattner32aaee62009-01-22 20:09:20 +0000310GenericValue lle_X_sprintf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnerb1118742003-01-13 00:59:47 +0000311 char *OutputBuffer = (char *)GVTOP(Args[0]);
312 const char *FmtStr = (const char *)GVTOP(Args[1]);
Chris Lattnere7c6f722001-12-13 00:43:47 +0000313 unsigned ArgNo = 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000314
315 // printf should return # chars printed. This is completely incorrect, but
316 // close enough for now.
Reid Spencerbfcd5992007-03-06 03:08:12 +0000317 GenericValue GV;
318 GV.IntVal = APInt(32, strlen(FmtStr));
Chris Lattner08845a22001-10-29 20:27:45 +0000319 while (1) {
320 switch (*FmtStr) {
321 case 0: return GV; // Null terminator...
322 default: // Normal nonspecial character
Chris Lattnere7c6f722001-12-13 00:43:47 +0000323 sprintf(OutputBuffer++, "%c", *FmtStr++);
Chris Lattner08845a22001-10-29 20:27:45 +0000324 break;
325 case '\\': { // Handle escape codes
Chris Lattnere7c6f722001-12-13 00:43:47 +0000326 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
327 FmtStr += 2; OutputBuffer += 2;
Chris Lattner08845a22001-10-29 20:27:45 +0000328 break;
329 }
330 case '%': { // Handle format specifiers
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000331 char FmtBuf[100] = "", Buffer[1000] = "";
332 char *FB = FmtBuf;
333 *FB++ = *FmtStr++;
334 char Last = *FB++ = *FmtStr++;
335 unsigned HowLong = 0;
336 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
337 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
338 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
339 Last != 'p' && Last != 's' && Last != '%') {
340 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
341 Last = *FB++ = *FmtStr++;
Chris Lattner08845a22001-10-29 20:27:45 +0000342 }
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000343 *FB = 0;
Misha Brukmand1c881a2005-04-21 22:43:08 +0000344
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000345 switch (Last) {
346 case '%':
Dan Gohman1eac4e02008-08-05 23:36:35 +0000347 strcpy(Buffer, "%"); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000348 case 'c':
Reid Spencerbfcd5992007-03-06 03:08:12 +0000349 sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
350 break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000351 case 'd': case 'i':
352 case 'u': case 'o':
353 case 'x': case 'X':
Chris Lattner69ab7a82002-08-02 23:08:32 +0000354 if (HowLong >= 1) {
Chris Lattner1543e402003-08-24 14:02:47 +0000355 if (HowLong == 1 &&
Chris Lattnerfe854032006-08-16 01:24:12 +0000356 TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 &&
Reid Spencer19b7e0e2006-05-24 19:21:13 +0000357 sizeof(long) < sizeof(int64_t)) {
Chris Lattner69ab7a82002-08-02 23:08:32 +0000358 // Make sure we use %lld with a 64 bit argument because we might be
359 // compiling LLI on a 32 bit compiler.
360 unsigned Size = strlen(FmtBuf);
361 FmtBuf[Size] = FmtBuf[Size-1];
362 FmtBuf[Size+1] = 0;
363 FmtBuf[Size-1] = 'l';
364 }
Reid Spencerbfcd5992007-03-06 03:08:12 +0000365 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
Chris Lattner69ab7a82002-08-02 23:08:32 +0000366 } else
Reid Spencerbfcd5992007-03-06 03:08:12 +0000367 sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
368 break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000369 case 'e': case 'E': case 'g': case 'G': case 'f':
370 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
371 case 'p':
Chris Lattnerb1118742003-01-13 00:59:47 +0000372 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
Misha Brukmand1c881a2005-04-21 22:43:08 +0000373 case 's':
Chris Lattnerb1118742003-01-13 00:59:47 +0000374 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
Bill Wendlinge8156192006-12-07 01:30:32 +0000375 default: cerr << "<unknown printf code '" << *FmtStr << "'!>";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000376 ArgNo++; break;
Chris Lattner08845a22001-10-29 20:27:45 +0000377 }
Chris Lattnere7c6f722001-12-13 00:43:47 +0000378 strcpy(OutputBuffer, Buffer);
379 OutputBuffer += strlen(Buffer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000380 }
Chris Lattner08845a22001-10-29 20:27:45 +0000381 break;
382 }
Chris Lattner08845a22001-10-29 20:27:45 +0000383 }
Reid Spencerb3b07272007-04-21 17:11:45 +0000384 return GV;
Chris Lattner08845a22001-10-29 20:27:45 +0000385}
386
Tanya Lattner32aaee62009-01-22 20:09:20 +0000387// int printf(sbyte *, ...) - a very rough implementation to make output useful.
388GenericValue lle_X_printf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattnere7c6f722001-12-13 00:43:47 +0000389 char Buffer[10000];
Tanya Lattner32aaee62009-01-22 20:09:20 +0000390 vector<GenericValue> NewArgs;
Reid Spencerb3b07272007-04-21 17:11:45 +0000391 NewArgs.push_back(PTOGV((void*)&Buffer[0]));
Chris Lattnere7c6f722001-12-13 00:43:47 +0000392 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
Reid Spencer97e0c222007-03-30 16:41:50 +0000393 GenericValue GV = lle_X_sprintf(FT, NewArgs);
Bill Wendlinge8156192006-12-07 01:30:32 +0000394 cout << Buffer;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000395 return GV;
396}
397
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000398static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1,
399 void *Arg2, void *Arg3, void *Arg4, void *Arg5,
400 void *Arg6, void *Arg7, void *Arg8) {
401 void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 };
402
403 // Loop over the format string, munging read values as appropriate (performs
Misha Brukman5560c9d2003-08-18 14:43:39 +0000404 // byteswaps as necessary).
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000405 unsigned ArgNo = 0;
406 while (*Fmt) {
407 if (*Fmt++ == '%') {
408 // Read any flag characters that may be present...
409 bool Suppress = false;
410 bool Half = false;
411 bool Long = false;
412 bool LongLong = false; // long long or long double
413
414 while (1) {
415 switch (*Fmt++) {
416 case '*': Suppress = true; break;
417 case 'a': /*Allocate = true;*/ break; // We don't need to track this
418 case 'h': Half = true; break;
419 case 'l': Long = true; break;
420 case 'q':
421 case 'L': LongLong = true; break;
422 default:
423 if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs
424 goto Out;
425 }
426 }
427 Out:
428
429 // Read the conversion character
430 if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
431 unsigned Size = 0;
432 const Type *Ty = 0;
433
434 switch (Fmt[-1]) {
435 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
436 case 'd':
437 if (Long || LongLong) {
Reid Spencere49661b2006-12-31 05:51:36 +0000438 Size = 8; Ty = Type::Int64Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000439 } else if (Half) {
Reid Spencere49661b2006-12-31 05:51:36 +0000440 Size = 4; Ty = Type::Int16Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000441 } else {
Reid Spencere49661b2006-12-31 05:51:36 +0000442 Size = 4; Ty = Type::Int32Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000443 }
444 break;
445
446 case 'e': case 'g': case 'E':
447 case 'f':
448 if (Long || LongLong) {
449 Size = 8; Ty = Type::DoubleTy;
450 } else {
451 Size = 4; Ty = Type::FloatTy;
452 }
453 break;
454
455 case 's': case 'c': case '[': // No byteswap needed
456 Size = 1;
Reid Spencere49661b2006-12-31 05:51:36 +0000457 Ty = Type::Int8Ty;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000458 break;
459
460 default: break;
461 }
462
463 if (Size) {
464 GenericValue GV;
465 void *Arg = Args[ArgNo++];
466 memcpy(&GV, Arg, Size);
467 TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
468 }
469 }
470 }
471 }
472}
473
Chris Lattner665ee882002-03-08 22:51:07 +0000474// int sscanf(const char *format, ...);
Tanya Lattner32aaee62009-01-22 20:09:20 +0000475GenericValue lle_X_sscanf(FunctionType *FT, const vector<GenericValue> &args) {
Chris Lattner665ee882002-03-08 22:51:07 +0000476 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
477
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000478 char *Args[10];
Chris Lattner665ee882002-03-08 22:51:07 +0000479 for (unsigned i = 0; i < args.size(); ++i)
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000480 Args[i] = (char*)GVTOP(args[i]);
Chris Lattner665ee882002-03-08 22:51:07 +0000481
482 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000483 GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
484 Args[5], Args[6], Args[7], Args[8], Args[9]));
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000485 ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4],
486 Args[5], Args[6], Args[7], Args[8], Args[9], 0);
487 return GV;
488}
489
490// int scanf(const char *format, ...);
Tanya Lattner32aaee62009-01-22 20:09:20 +0000491GenericValue lle_X_scanf(FunctionType *FT, const vector<GenericValue> &args) {
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000492 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
493
494 char *Args[10];
495 for (unsigned i = 0; i < args.size(); ++i)
496 Args[i] = (char*)GVTOP(args[i]);
497
498 GenericValue GV;
Reid Spencerbfcd5992007-03-06 03:08:12 +0000499 GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
500 Args[5], Args[6], Args[7], Args[8], Args[9]));
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000501 ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4],
502 Args[5], Args[6], Args[7], Args[8], Args[9]);
Chris Lattner665ee882002-03-08 22:51:07 +0000503 return GV;
504}
505
Tanya Lattner32aaee62009-01-22 20:09:20 +0000506
507// int clock(void) - Profiling implementation
508GenericValue lle_i_clock(FunctionType *FT, const vector<GenericValue> &Args) {
509 extern unsigned int clock(void);
510 GenericValue GV;
511 GV.IntVal = APInt(32, clock());
512 return GV;
513}
514
515
516//===----------------------------------------------------------------------===//
517// String Functions...
518//===----------------------------------------------------------------------===//
519
520// int strcmp(const char *S1, const char *S2);
521GenericValue lle_X_strcmp(FunctionType *FT, const vector<GenericValue> &Args) {
522 assert(Args.size() == 2);
523 GenericValue Ret;
524 Ret.IntVal = APInt(32, strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
525 return Ret;
526}
527
528// char *strcat(char *Dest, const char *src);
529GenericValue lle_X_strcat(FunctionType *FT, const vector<GenericValue> &Args) {
530 assert(Args.size() == 2);
531 assert(isa<PointerType>(FT->getReturnType()) &&"strcat must return pointer");
532 return PTOGV(strcat((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
533}
534
535// char *strcpy(char *Dest, const char *src);
536GenericValue lle_X_strcpy(FunctionType *FT, const vector<GenericValue> &Args) {
537 assert(Args.size() == 2);
538 assert(isa<PointerType>(FT->getReturnType()) &&"strcpy must return pointer");
539 return PTOGV(strcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
540}
541
542static GenericValue size_t_to_GV (size_t n) {
543 GenericValue Ret;
544 if (sizeof (size_t) == sizeof (uint64_t)) {
545 Ret.IntVal = APInt(64, n);
546 } else {
547 assert (sizeof (size_t) == sizeof (unsigned int));
548 Ret.IntVal = APInt(32, n);
549 }
550 return Ret;
551}
552
553static size_t GV_to_size_t (GenericValue GV) {
554 size_t count;
555 if (sizeof (size_t) == sizeof (uint64_t)) {
556 count = (size_t)GV.IntVal.getZExtValue();
557 } else {
558 assert (sizeof (size_t) == sizeof (unsigned int));
559 count = (size_t)GV.IntVal.getZExtValue();
560 }
561 return count;
562}
563
564// size_t strlen(const char *src);
565GenericValue lle_X_strlen(FunctionType *FT, const vector<GenericValue> &Args) {
566 assert(Args.size() == 1);
567 size_t strlenResult = strlen ((char *) GVTOP (Args[0]));
568 return size_t_to_GV (strlenResult);
569}
570
571// char *strdup(const char *src);
572GenericValue lle_X_strdup(FunctionType *FT, const vector<GenericValue> &Args) {
573 assert(Args.size() == 1);
574 assert(isa<PointerType>(FT->getReturnType()) && "strdup must return pointer");
575 return PTOGV(strdup((char*)GVTOP(Args[0])));
576}
577
578// char *__strdup(const char *src);
579GenericValue lle_X___strdup(FunctionType *FT, const vector<GenericValue> &Args) {
580 assert(Args.size() == 1);
581 assert(isa<PointerType>(FT->getReturnType()) &&"_strdup must return pointer");
582 return PTOGV(strdup((char*)GVTOP(Args[0])));
583}
584
585// void *memset(void *S, int C, size_t N)
586GenericValue lle_X_memset(FunctionType *FT, const vector<GenericValue> &Args) {
587 assert(Args.size() == 3);
588 size_t count = GV_to_size_t (Args[2]);
589 assert(isa<PointerType>(FT->getReturnType()) && "memset must return pointer");
590 return PTOGV(memset(GVTOP(Args[0]), uint32_t(Args[1].IntVal.getZExtValue()),
591 count));
592}
593
594// void *memcpy(void *Dest, void *src, size_t Size);
595GenericValue lle_X_memcpy(FunctionType *FT, const vector<GenericValue> &Args) {
596 assert(Args.size() == 3);
597 assert(isa<PointerType>(FT->getReturnType()) && "memcpy must return pointer");
598 size_t count = GV_to_size_t (Args[2]);
599 return PTOGV(memcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));
600}
601
602// void *memcpy(void *Dest, void *src, size_t Size);
603GenericValue lle_X_memmove(FunctionType *FT, const vector<GenericValue> &Args) {
604 assert(Args.size() == 3);
605 assert(isa<PointerType>(FT->getReturnType()) && "memmove must return pointer");
606 size_t count = GV_to_size_t (Args[2]);
607 return PTOGV(memmove((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));
608}
609
610//===----------------------------------------------------------------------===//
611// IO Functions...
612//===----------------------------------------------------------------------===//
613
614// getFILE - Turn a pointer in the host address space into a legit pointer in
615// the interpreter address space. This is an identity transformation.
616#define getFILE(ptr) ((FILE*)ptr)
617
618// FILE *fopen(const char *filename, const char *mode);
619GenericValue lle_X_fopen(FunctionType *FT, const vector<GenericValue> &Args) {
620 assert(Args.size() == 2);
621 assert(isa<PointerType>(FT->getReturnType()) && "fopen must return pointer");
622 return PTOGV(fopen((const char *)GVTOP(Args[0]),
623 (const char *)GVTOP(Args[1])));
624}
625
626// int fclose(FILE *F);
627GenericValue lle_X_fclose(FunctionType *FT, const vector<GenericValue> &Args) {
628 assert(Args.size() == 1);
629 GenericValue GV;
630 GV.IntVal = APInt(32, fclose(getFILE(GVTOP(Args[0]))));
631 return GV;
632}
633
634// int feof(FILE *stream);
635GenericValue lle_X_feof(FunctionType *FT, const vector<GenericValue> &Args) {
636 assert(Args.size() == 1);
637 GenericValue GV;
638
639 GV.IntVal = APInt(32, feof(getFILE(GVTOP(Args[0]))));
640 return GV;
641}
642
643// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
644GenericValue lle_X_fread(FunctionType *FT, const vector<GenericValue> &Args) {
645 assert(Args.size() == 4);
646 size_t result;
647
648 result = fread((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);
651}
652
653// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
654GenericValue lle_X_fwrite(FunctionType *FT, const vector<GenericValue> &Args) {
655 assert(Args.size() == 4);
656 size_t result;
657
658 result = fwrite((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
659 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
660 return size_t_to_GV (result);
661}
662
663// char *fgets(char *s, int n, FILE *stream);
664GenericValue lle_X_fgets(FunctionType *FT, const vector<GenericValue> &Args) {
665 assert(Args.size() == 3);
666 return PTOGV(fgets((char*)GVTOP(Args[0]), Args[1].IntVal.getZExtValue(),
667 getFILE(GVTOP(Args[2]))));
668}
669
670// FILE *freopen(const char *path, const char *mode, FILE *stream);
671GenericValue lle_X_freopen(FunctionType *FT, const vector<GenericValue> &Args) {
672 assert(Args.size() == 3);
673 assert(isa<PointerType>(FT->getReturnType()) &&"freopen must return pointer");
674 return PTOGV(freopen((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]),
675 getFILE(GVTOP(Args[2]))));
676}
677
678// int fflush(FILE *stream);
679GenericValue lle_X_fflush(FunctionType *FT, const vector<GenericValue> &Args) {
680 assert(Args.size() == 1);
681 GenericValue GV;
682 GV.IntVal = APInt(32, fflush(getFILE(GVTOP(Args[0]))));
683 return GV;
684}
685
686// int getc(FILE *stream);
687GenericValue lle_X_getc(FunctionType *FT, const vector<GenericValue> &Args) {
688 assert(Args.size() == 1);
689 GenericValue GV;
690 GV.IntVal = APInt(32, getc(getFILE(GVTOP(Args[0]))));
691 return GV;
692}
693
694// int _IO_getc(FILE *stream);
695GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) {
696 return lle_X_getc(F, Args);
697}
698
699// int fputc(int C, FILE *stream);
700GenericValue lle_X_fputc(FunctionType *FT, const vector<GenericValue> &Args) {
701 assert(Args.size() == 2);
702 GenericValue GV;
703 GV.IntVal = APInt(32, fputc(Args[0].IntVal.getZExtValue(),
704 getFILE(GVTOP(Args[1]))));
705 return GV;
706}
707
708// int ungetc(int C, FILE *stream);
709GenericValue lle_X_ungetc(FunctionType *FT, const vector<GenericValue> &Args) {
710 assert(Args.size() == 2);
711 GenericValue GV;
712 GV.IntVal = APInt(32, ungetc(Args[0].IntVal.getZExtValue(),
713 getFILE(GVTOP(Args[1]))));
714 return GV;
715}
716
717// int ferror (FILE *stream);
718GenericValue lle_X_ferror(FunctionType *FT, const vector<GenericValue> &Args) {
719 assert(Args.size() == 1);
720 GenericValue GV;
721 GV.IntVal = APInt(32, ferror (getFILE(GVTOP(Args[0]))));
722 return GV;
723}
724
725// int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
726// useful.
727GenericValue lle_X_fprintf(FunctionType *FT, const vector<GenericValue> &Args) {
Chris Lattner9dbf6dd2003-04-21 22:43:20 +0000728 assert(Args.size() >= 2);
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000729 char Buffer[10000];
Tanya Lattner32aaee62009-01-22 20:09:20 +0000730 vector<GenericValue> NewArgs;
Chris Lattnerb1118742003-01-13 00:59:47 +0000731 NewArgs.push_back(PTOGV(Buffer));
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000732 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
Reid Spencer97e0c222007-03-30 16:41:50 +0000733 GenericValue GV = lle_X_sprintf(FT, NewArgs);
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000734
Tanya Lattner32aaee62009-01-22 20:09:20 +0000735 fputs(Buffer, getFILE(GVTOP(Args[0])));
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000736 return GV;
737}
738
Tanya Lattner32aaee62009-01-22 20:09:20 +0000739// int __cxa_guard_acquire (__guard *g);
740GenericValue lle_X___cxa_guard_acquire(FunctionType *FT,
741 const vector<GenericValue> &Args) {
742 assert(Args.size() == 1);
743 GenericValue GV;
744#ifdef __linux__
745 GV.IntVal = APInt(32, __cxxabiv1::__cxa_guard_acquire (
746 (__cxxabiv1::__guard*)GVTOP(Args[0])));
747#else
748 assert(0 && "Can't call __cxa_guard_acquire on this platform");
749#endif
750 return GV;
751}
752
753// void __cxa_guard_release (__guard *g);
754GenericValue lle_X___cxa_guard_release(FunctionType *FT,
755 const vector<GenericValue> &Args) {
756 assert(Args.size() == 1);
757#ifdef __linux__
758 __cxxabiv1::__cxa_guard_release ((__cxxabiv1::__guard*)GVTOP(Args[0]));
759#else
760 assert(0 && "Can't call __cxa_guard_release on this platform");
761#endif
762 return GenericValue();
763}
764
Chris Lattner7720c8e2001-09-10 04:50:17 +0000765} // End extern "C"
Chris Lattner4721f132001-10-30 20:28:00 +0000766
767
Chris Lattnerda82ed52003-05-08 16:18:31 +0000768void Interpreter::initializeExternalFunctions() {
Tanya Lattner32aaee62009-01-22 20:09:20 +0000769 FuncNames["lle_X_putchar"] = lle_X_putchar;
770 FuncNames["lle_X__IO_putc"] = lle_X__IO_putc;
Chris Lattner0f279b22001-11-03 10:15:32 +0000771 FuncNames["lle_X_exit"] = lle_X_exit;
Chris Lattner1ee34a52002-05-20 21:17:16 +0000772 FuncNames["lle_X_abort"] = lle_X_abort;
Tanya Lattner32aaee62009-01-22 20:09:20 +0000773 FuncNames["lle_X_malloc"] = lle_X_malloc;
774 FuncNames["lle_X_calloc"] = lle_X_calloc;
775 FuncNames["lle_X_realloc"] = lle_X_realloc;
776 FuncNames["lle_X_free"] = lle_X_free;
777 FuncNames["lle_X_atoi"] = lle_X_atoi;
778 FuncNames["lle_X_pow"] = lle_X_pow;
779 FuncNames["lle_X_sin"] = lle_X_sin;
780 FuncNames["lle_X_cos"] = lle_X_cos;
781 FuncNames["lle_X_exp"] = lle_X_exp;
782 FuncNames["lle_X_log"] = lle_X_log;
783 FuncNames["lle_X_floor"] = lle_X_floor;
784 FuncNames["lle_X_srand"] = lle_X_srand;
785 FuncNames["lle_X_rand"] = lle_X_rand;
786#ifdef HAVE_RAND48
787 FuncNames["lle_X_drand48"] = lle_X_drand48;
788 FuncNames["lle_X_srand48"] = lle_X_srand48;
789 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
790#endif
791 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
792 FuncNames["lle_X_puts"] = lle_X_puts;
Chris Lattner0f279b22001-11-03 10:15:32 +0000793 FuncNames["lle_X_printf"] = lle_X_printf;
Chris Lattnere7c6f722001-12-13 00:43:47 +0000794 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
Chris Lattner665ee882002-03-08 22:51:07 +0000795 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
Chris Lattnerf9a88b62003-03-31 22:12:37 +0000796 FuncNames["lle_X_scanf"] = lle_X_scanf;
Tanya Lattner32aaee62009-01-22 20:09:20 +0000797 FuncNames["lle_i_clock"] = lle_i_clock;
798
799 FuncNames["lle_X_strcmp"] = lle_X_strcmp;
800 FuncNames["lle_X_strcat"] = lle_X_strcat;
801 FuncNames["lle_X_strcpy"] = lle_X_strcpy;
802 FuncNames["lle_X_strlen"] = lle_X_strlen;
803 FuncNames["lle_X___strdup"] = lle_X___strdup;
804 FuncNames["lle_X_memset"] = lle_X_memset;
805 FuncNames["lle_X_memcpy"] = lle_X_memcpy;
806 FuncNames["lle_X_memmove"] = lle_X_memmove;
807
808 FuncNames["lle_X_fopen"] = lle_X_fopen;
809 FuncNames["lle_X_fclose"] = lle_X_fclose;
810 FuncNames["lle_X_feof"] = lle_X_feof;
811 FuncNames["lle_X_fread"] = lle_X_fread;
812 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
813 FuncNames["lle_X_fgets"] = lle_X_fgets;
814 FuncNames["lle_X_fflush"] = lle_X_fflush;
815 FuncNames["lle_X_fgetc"] = lle_X_getc;
816 FuncNames["lle_X_getc"] = lle_X_getc;
817 FuncNames["lle_X__IO_getc"] = lle_X__IO_getc;
818 FuncNames["lle_X_fputc"] = lle_X_fputc;
819 FuncNames["lle_X_ungetc"] = lle_X_ungetc;
Chris Lattnercf9b4f02002-11-06 23:05:03 +0000820 FuncNames["lle_X_fprintf"] = lle_X_fprintf;
Tanya Lattner32aaee62009-01-22 20:09:20 +0000821 FuncNames["lle_X_freopen"] = lle_X_freopen;
822
823 FuncNames["lle_X___cxa_guard_acquire"] = lle_X___cxa_guard_acquire;
824 FuncNames["lle_X____cxa_guard_release"] = lle_X___cxa_guard_release;
Chris Lattner4721f132001-10-30 20:28:00 +0000825}
Brian Gaeked0fde302003-11-11 22:41:34 +0000826