blob: 66a26cff3c63a1ffce7440ec59780536ff88155b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ExternalFunctions.cpp - Implement External Functions --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains both code to deal with invoking "external" functions, but
11// also contains code that implements "exported" external functions.
12//
13// 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.
19//
20//===----------------------------------------------------------------------===//
21
22#include "Interpreter.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Module.h"
25#include "llvm/Support/Streams.h"
26#include "llvm/System/DynamicLibrary.h"
27#include "llvm/Target/TargetData.h"
Chuck Rose III9a2da442007-07-27 18:26:35 +000028#include "llvm/Support/ManagedStatic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include <csignal>
Duncan Sands05f68372008-10-08 07:23:46 +000030#include <cstdio>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include <map>
32#include <cmath>
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000033#include <cstring>
Zhou Shenga80aaba2007-12-12 06:16:47 +000034
35#ifdef __linux__
Zhou Shenga0a1c2b2007-12-12 04:55:43 +000036#include <cxxabi.h>
Zhou Shenga80aaba2007-12-12 06:16:47 +000037#endif
38
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039using std::vector;
40
41using namespace llvm;
42
43typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
Chuck Rose III9a2da442007-07-27 18:26:35 +000044static ManagedStatic<std::map<const Function *, ExFunc> > Functions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045static std::map<std::string, ExFunc> FuncNames;
46
47static Interpreter *TheInterpreter;
48
49static char getTypeID(const Type *Ty) {
50 switch (Ty->getTypeID()) {
51 case Type::VoidTyID: return 'V';
52 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 }
61 case Type::FloatTyID: return 'F';
62 case Type::DoubleTyID: return 'D';
63 case Type::PointerTyID: return 'P';
64 case Type::FunctionTyID:return 'M';
65 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 Korobeynikov85a94032007-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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077static ExFunc lookupFunction(const Function *F) {
78 // Function not found, look it up... start by figuring out what the
79 // composite function name should be.
80 std::string ExtName = "lle_";
81 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();
85
86 ExFunc FnPtr = FuncNames[ExtName];
87 if (FnPtr == 0)
88 FnPtr = FuncNames["lle_X_"+F->getName()];
89 if (FnPtr == 0) // Try calling a generic function... if it exists...
90 FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(
91 ("lle_X_"+F->getName()).c_str());
92 if (FnPtr == 0)
93 FnPtr = (ExFunc)(intptr_t)
94 sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
95 if (FnPtr != 0)
Chuck Rose III9a2da442007-07-27 18:26:35 +000096 Functions->insert(std::make_pair(F, FnPtr)); // Cache for later
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 return FnPtr;
98}
99
100GenericValue Interpreter::callExternalFunction(Function *F,
101 const std::vector<GenericValue> &ArgVals) {
102 TheInterpreter = this;
103
104 // Do a lookup to see if the function is in our cache... this should just be a
105 // deferred annotation!
Chuck Rose III9a2da442007-07-27 18:26:35 +0000106 std::map<const Function *, ExFunc>::iterator FI = Functions->find(F);
107 ExFunc Fn = (FI == Functions->end()) ? lookupFunction(F) : FI->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 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();
114 }
115
116 // TODO: FIXME when types are not const!
117 GenericValue Result = Fn(const_cast<FunctionType*>(F->getFunctionType()),
118 ArgVals);
119 return Result;
120}
121
122
123//===----------------------------------------------------------------------===//
124// Functions "exported" to the running application...
125//
126extern "C" { // Don't add C++ manglings to llvm mangling :)
127
128// 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
144// void atexit(Function*)
145GenericValue lle_X_atexit(FunctionType *FT, const vector<GenericValue> &Args) {
146 assert(Args.size() == 1);
147 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
148 GenericValue GV;
149 GV.IntVal = 0;
150 return GV;
151}
152
153// void exit(int)
154GenericValue lle_X_exit(FunctionType *FT, const vector<GenericValue> &Args) {
155 TheInterpreter->exitCalled(Args[0]);
156 return GenericValue();
157}
158
159// void abort(void)
160GenericValue lle_X_abort(FunctionType *FT, const vector<GenericValue> &Args) {
161 raise (SIGABRT);
162 return GenericValue();
163}
164
165// 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
Zhou Shenga0a1c2b2007-12-12 04:55:43 +0000210// 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226// 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) {
Dan Gohman301f4052008-01-29 13:02:09 +0000262 assert(Args.empty());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 GenericValue GV;
264 GV.DoubleVal = drand48();
265 return GV;
266}
267
268// long lrand48()
269GenericValue lle_X_lrand48(FunctionType *FT, const vector<GenericValue> &Args) {
Dan Gohman301f4052008-01-29 13:02:09 +0000270 assert(Args.empty());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 GenericValue GV;
Duncan Sands4c8ee7f2007-12-10 14:43:10 +0000272 GV.IntVal = APInt(32, lrand48());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 return GV;
274}
275
276// void srand48(long)
277GenericValue lle_X_srand48(FunctionType *FT, const vector<GenericValue> &Args) {
278 assert(Args.size() == 1);
Duncan Sands4c8ee7f2007-12-10 14:43:10 +0000279 srand48(Args[0].IntVal.getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 return GenericValue();
281}
282
283#endif
284
285// int rand()
286GenericValue lle_X_rand(FunctionType *FT, const vector<GenericValue> &Args) {
Dan Gohman301f4052008-01-29 13:02:09 +0000287 assert(Args.empty());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 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
309// output useful.
310GenericValue lle_X_sprintf(FunctionType *FT, const vector<GenericValue> &Args) {
311 char *OutputBuffer = (char *)GVTOP(Args[0]);
312 const char *FmtStr = (const char *)GVTOP(Args[1]);
313 unsigned ArgNo = 2;
314
315 // printf should return # chars printed. This is completely incorrect, but
316 // close enough for now.
317 GenericValue GV;
318 GV.IntVal = APInt(32, strlen(FmtStr));
319 while (1) {
320 switch (*FmtStr) {
321 case 0: return GV; // Null terminator...
322 default: // Normal nonspecial character
323 sprintf(OutputBuffer++, "%c", *FmtStr++);
324 break;
325 case '\\': { // Handle escape codes
326 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
327 FmtStr += 2; OutputBuffer += 2;
328 break;
329 }
330 case '%': { // Handle format specifiers
331 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++;
342 }
343 *FB = 0;
344
345 switch (Last) {
346 case '%':
Dan Gohmane5dce982008-08-05 23:36:35 +0000347 strcpy(Buffer, "%"); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 case 'c':
349 sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
350 break;
351 case 'd': case 'i':
352 case 'u': case 'o':
353 case 'x': case 'X':
354 if (HowLong >= 1) {
355 if (HowLong == 1 &&
356 TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 &&
357 sizeof(long) < sizeof(int64_t)) {
358 // 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 }
365 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
366 } else
367 sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
368 break;
369 case 'e': case 'E': case 'g': case 'G': case 'f':
370 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
371 case 'p':
372 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
373 case 's':
374 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
375 default: cerr << "<unknown printf code '" << *FmtStr << "'!>";
376 ArgNo++; break;
377 }
378 strcpy(OutputBuffer, Buffer);
379 OutputBuffer += strlen(Buffer);
380 }
381 break;
382 }
383 }
384 return GV;
385}
386
387// int printf(sbyte *, ...) - a very rough implementation to make output useful.
388GenericValue lle_X_printf(FunctionType *FT, const vector<GenericValue> &Args) {
389 char Buffer[10000];
390 vector<GenericValue> NewArgs;
391 NewArgs.push_back(PTOGV((void*)&Buffer[0]));
392 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
393 GenericValue GV = lle_X_sprintf(FT, NewArgs);
394 cout << Buffer;
395 return GV;
396}
397
398static 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
404 // byteswaps as necessary).
405 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) {
438 Size = 8; Ty = Type::Int64Ty;
439 } else if (Half) {
440 Size = 4; Ty = Type::Int16Ty;
441 } else {
442 Size = 4; Ty = Type::Int32Ty;
443 }
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;
457 Ty = Type::Int8Ty;
458 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
474// int sscanf(const char *format, ...);
475GenericValue lle_X_sscanf(FunctionType *FT, const vector<GenericValue> &args) {
476 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
477
478 char *Args[10];
479 for (unsigned i = 0; i < args.size(); ++i)
480 Args[i] = (char*)GVTOP(args[i]);
481
482 GenericValue GV;
483 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]));
485 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, ...);
491GenericValue lle_X_scanf(FunctionType *FT, const vector<GenericValue> &args) {
492 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;
499 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]));
501 ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4],
502 Args[5], Args[6], Args[7], Args[8], Args[9]);
503 return GV;
504}
505
506
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
Evan Chengf4d008f2008-02-20 07:55:26 +0000602// 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610//===----------------------------------------------------------------------===//
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);
Dan Gohmanc43c7f42007-12-14 15:41:34 +0000666 return PTOGV(fgets((char*)GVTOP(Args[0]), Args[1].IntVal.getZExtValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667 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) {
728 assert(Args.size() >= 2);
729 char Buffer[10000];
730 vector<GenericValue> NewArgs;
731 NewArgs.push_back(PTOGV(Buffer));
732 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
733 GenericValue GV = lle_X_sprintf(FT, NewArgs);
734
735 fputs(Buffer, getFILE(GVTOP(Args[0])));
736 return GV;
737}
738
Zhou Shenga0a1c2b2007-12-12 04:55:43 +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;
Zhou Shenga80aaba2007-12-12 06:16:47 +0000744#ifdef __linux__
Zhou Shenga0a1c2b2007-12-12 04:55:43 +0000745 GV.IntVal = APInt(32, __cxxabiv1::__cxa_guard_acquire (
746 (__cxxabiv1::__guard*)GVTOP(Args[0])));
Zhou Shenga80aaba2007-12-12 06:16:47 +0000747#else
748 assert(0 && "Can't call __cxa_guard_acquire on this platform");
749#endif
Zhou Shenga0a1c2b2007-12-12 04:55:43 +0000750 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);
Zhou Shenga80aaba2007-12-12 06:16:47 +0000757#ifdef __linux__
Zhou Shenga0a1c2b2007-12-12 04:55:43 +0000758 __cxxabiv1::__cxa_guard_release ((__cxxabiv1::__guard*)GVTOP(Args[0]));
Zhou Shenga80aaba2007-12-12 06:16:47 +0000759#else
760 assert(0 && "Can't call __cxa_guard_release on this platform");
761#endif
Zhou Shenga0a1c2b2007-12-12 04:55:43 +0000762 return GenericValue();
763}
764
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765} // End extern "C"
766
767
768void Interpreter::initializeExternalFunctions() {
769 FuncNames["lle_X_putchar"] = lle_X_putchar;
770 FuncNames["lle_X__IO_putc"] = lle_X__IO_putc;
771 FuncNames["lle_X_exit"] = lle_X_exit;
772 FuncNames["lle_X_abort"] = lle_X_abort;
773 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;
Zhou Shenga0a1c2b2007-12-12 04:55:43 +0000779 FuncNames["lle_X_sin"] = lle_X_sin;
780 FuncNames["lle_X_cos"] = lle_X_cos;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000781 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;
793 FuncNames["lle_X_printf"] = lle_X_printf;
794 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
795 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
796 FuncNames["lle_X_scanf"] = lle_X_scanf;
797 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;
Evan Chengf4d008f2008-02-20 07:55:26 +0000806 FuncNames["lle_X_memmove"] = lle_X_memmove;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807
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;
820 FuncNames["lle_X_fprintf"] = lle_X_fprintf;
821 FuncNames["lle_X_freopen"] = lle_X_freopen;
Zhou Shenga0a1c2b2007-12-12 04:55:43 +0000822
823 FuncNames["lle_X___cxa_guard_acquire"] = lle_X___cxa_guard_acquire;
824 FuncNames["lle_X____cxa_guard_release"] = lle_X___cxa_guard_release;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825}
826