1 //===-- ExternalFunctions.cpp - Implement External Functions --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains both code to deal with invoking "external" functions, but
11 // also contains code that implements "exported" external functions.
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.
20 //===----------------------------------------------------------------------===//
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"
28 #include "llvm/Support/ManagedStatic.h"
43 typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
44 static ManagedStatic<std::map<const Function *, ExFunc> > Functions;
45 static std::map<std::string, ExFunc> FuncNames;
47 static Interpreter *TheInterpreter;
49 static 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()) {
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';
72 // 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.
77 static 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();
86 ExFunc FnPtr = FuncNames[ExtName];
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());
93 FnPtr = (ExFunc)(intptr_t)
94 sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
96 Functions->insert(std::make_pair(F, FnPtr)); // Cache for later
100 GenericValue Interpreter::callExternalFunction(Function *F,
101 const std::vector<GenericValue> &ArgVals) {
102 TheInterpreter = this;
104 // Do a lookup to see if the function is in our cache... this should just be a
105 // deferred annotation!
106 std::map<const Function *, ExFunc>::iterator FI = Functions->find(F);
107 ExFunc Fn = (FI == Functions->end()) ? lookupFunction(F) : FI->second;
109 cerr << "Tried to execute an unknown external function: "
110 << F->getType()->getDescription() << " " << F->getName() << "\n";
111 if (F->getName() == "__main")
112 return GenericValue();
116 // TODO: FIXME when types are not const!
117 GenericValue Result = Fn(const_cast<FunctionType*>(F->getFunctionType()),
123 //===----------------------------------------------------------------------===//
124 // Functions "exported" to the running application...
126 extern "C" { // Don't add C++ manglings to llvm mangling :)
128 // void putchar(ubyte)
129 GenericValue lle_X_putchar(FunctionType *FT, const vector<GenericValue> &Args){
130 cout << ((char)Args[0].IntVal.getZExtValue()) << std::flush;
134 // void _IO_putc(int c, FILE* fp)
135 GenericValue lle_X__IO_putc(FunctionType *FT, const vector<GenericValue> &Args){
137 _IO_putc((char)Args[0].IntVal.getZExtValue(), (FILE*) Args[1].PointerVal);
139 assert(0 && "Can't call _IO_putc on this platform");
144 // void atexit(Function*)
145 GenericValue lle_X_atexit(FunctionType *FT, const vector<GenericValue> &Args) {
146 assert(Args.size() == 1);
147 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
154 GenericValue lle_X_exit(FunctionType *FT, const vector<GenericValue> &Args) {
155 TheInterpreter->exitCalled(Args[0]);
156 return GenericValue();
160 GenericValue lle_X_abort(FunctionType *FT, const vector<GenericValue> &Args) {
162 return GenericValue();
165 // void *malloc(uint)
166 GenericValue 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()));
172 // void *calloc(uint, uint)
173 GenericValue 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()));
180 // void *calloc(uint, uint)
181 GenericValue 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()));
188 GenericValue lle_X_free(FunctionType *FT, const vector<GenericValue> &Args) {
189 assert(Args.size() == 1);
190 free(GVTOP(Args[0]));
191 return GenericValue();
195 GenericValue lle_X_atoi(FunctionType *FT, const vector<GenericValue> &Args) {
196 assert(Args.size() == 1);
198 GV.IntVal = APInt(32, atoi((char*)GVTOP(Args[0])));
202 // double pow(double, double)
203 GenericValue lle_X_pow(FunctionType *FT, const vector<GenericValue> &Args) {
204 assert(Args.size() == 2);
206 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
210 // double sin(double)
211 GenericValue lle_X_sin(FunctionType *FT, const vector<GenericValue> &Args) {
212 assert(Args.size() == 1);
214 GV.DoubleVal = sin(Args[0].DoubleVal);
218 // double cos(double)
219 GenericValue lle_X_cos(FunctionType *FT, const vector<GenericValue> &Args) {
220 assert(Args.size() == 1);
222 GV.DoubleVal = cos(Args[0].DoubleVal);
226 // double exp(double)
227 GenericValue lle_X_exp(FunctionType *FT, const vector<GenericValue> &Args) {
228 assert(Args.size() == 1);
230 GV.DoubleVal = exp(Args[0].DoubleVal);
234 // double sqrt(double)
235 GenericValue lle_X_sqrt(FunctionType *FT, const vector<GenericValue> &Args) {
236 assert(Args.size() == 1);
238 GV.DoubleVal = sqrt(Args[0].DoubleVal);
242 // double log(double)
243 GenericValue lle_X_log(FunctionType *FT, const vector<GenericValue> &Args) {
244 assert(Args.size() == 1);
246 GV.DoubleVal = log(Args[0].DoubleVal);
250 // double floor(double)
251 GenericValue lle_X_floor(FunctionType *FT, const vector<GenericValue> &Args) {
252 assert(Args.size() == 1);
254 GV.DoubleVal = floor(Args[0].DoubleVal);
261 GenericValue lle_X_drand48(FunctionType *FT, const vector<GenericValue> &Args) {
262 assert(Args.empty());
264 GV.DoubleVal = drand48();
269 GenericValue lle_X_lrand48(FunctionType *FT, const vector<GenericValue> &Args) {
270 assert(Args.empty());
272 GV.IntVal = APInt(32, lrand48());
276 // void srand48(long)
277 GenericValue lle_X_srand48(FunctionType *FT, const vector<GenericValue> &Args) {
278 assert(Args.size() == 1);
279 srand48(Args[0].IntVal.getZExtValue());
280 return GenericValue();
286 GenericValue lle_X_rand(FunctionType *FT, const vector<GenericValue> &Args) {
287 assert(Args.empty());
289 GV.IntVal = APInt(32, rand());
294 GenericValue lle_X_srand(FunctionType *FT, const vector<GenericValue> &Args) {
295 assert(Args.size() == 1);
296 srand(Args[0].IntVal.getZExtValue());
297 return GenericValue();
300 // int puts(const char*)
301 GenericValue lle_X_puts(FunctionType *FT, const vector<GenericValue> &Args) {
302 assert(Args.size() == 1);
304 GV.IntVal = APInt(32, puts((char*)GVTOP(Args[0])));
308 // int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
310 GenericValue 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]);
315 // printf should return # chars printed. This is completely incorrect, but
316 // close enough for now.
318 GV.IntVal = APInt(32, strlen(FmtStr));
321 case 0: return GV; // Null terminator...
322 default: // Normal nonspecial character
323 sprintf(OutputBuffer++, "%c", *FmtStr++);
325 case '\\': { // Handle escape codes
326 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
327 FmtStr += 2; OutputBuffer += 2;
330 case '%': { // Handle format specifiers
331 char FmtBuf[100] = "", Buffer[1000] = "";
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++;
347 strcpy(Buffer, "%"); break;
349 sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
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];
363 FmtBuf[Size-1] = 'l';
365 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
367 sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
369 case 'e': case 'E': case 'g': case 'G': case 'f':
370 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
372 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
374 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
375 default: cerr << "<unknown printf code '" << *FmtStr << "'!>";
378 strcpy(OutputBuffer, Buffer);
379 OutputBuffer += strlen(Buffer);
387 // int printf(sbyte *, ...) - a very rough implementation to make output useful.
388 GenericValue lle_X_printf(FunctionType *FT, const vector<GenericValue> &Args) {
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);
398 static 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 };
403 // Loop over the format string, munging read values as appropriate (performs
404 // byteswaps as necessary).
408 // Read any flag characters that may be present...
409 bool Suppress = false;
412 bool LongLong = false; // long long or long double
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;
421 case 'L': LongLong = true; break;
423 if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs
429 // Read the conversion character
430 if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
435 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
437 if (Long || LongLong) {
438 Size = 8; Ty = Type::Int64Ty;
440 Size = 4; Ty = Type::Int16Ty;
442 Size = 4; Ty = Type::Int32Ty;
446 case 'e': case 'g': case 'E':
448 if (Long || LongLong) {
449 Size = 8; Ty = Type::DoubleTy;
451 Size = 4; Ty = Type::FloatTy;
455 case 's': case 'c': case '[': // No byteswap needed
465 void *Arg = Args[ArgNo++];
466 memcpy(&GV, Arg, Size);
467 TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
474 // int sscanf(const char *format, ...);
475 GenericValue lle_X_sscanf(FunctionType *FT, const vector<GenericValue> &args) {
476 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
479 for (unsigned i = 0; i < args.size(); ++i)
480 Args[i] = (char*)GVTOP(args[i]);
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);
490 // int scanf(const char *format, ...);
491 GenericValue lle_X_scanf(FunctionType *FT, const vector<GenericValue> &args) {
492 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
495 for (unsigned i = 0; i < args.size(); ++i)
496 Args[i] = (char*)GVTOP(args[i]);
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]);
507 // int clock(void) - Profiling implementation
508 GenericValue lle_i_clock(FunctionType *FT, const vector<GenericValue> &Args) {
509 extern unsigned int clock(void);
511 GV.IntVal = APInt(32, clock());
516 //===----------------------------------------------------------------------===//
517 // String Functions...
518 //===----------------------------------------------------------------------===//
520 // int strcmp(const char *S1, const char *S2);
521 GenericValue lle_X_strcmp(FunctionType *FT, const vector<GenericValue> &Args) {
522 assert(Args.size() == 2);
524 Ret.IntVal = APInt(32, strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
528 // char *strcat(char *Dest, const char *src);
529 GenericValue 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])));
535 // char *strcpy(char *Dest, const char *src);
536 GenericValue 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])));
542 static GenericValue size_t_to_GV (size_t n) {
544 if (sizeof (size_t) == sizeof (uint64_t)) {
545 Ret.IntVal = APInt(64, n);
547 assert (sizeof (size_t) == sizeof (unsigned int));
548 Ret.IntVal = APInt(32, n);
553 static size_t GV_to_size_t (GenericValue GV) {
555 if (sizeof (size_t) == sizeof (uint64_t)) {
556 count = (size_t)GV.IntVal.getZExtValue();
558 assert (sizeof (size_t) == sizeof (unsigned int));
559 count = (size_t)GV.IntVal.getZExtValue();
564 // size_t strlen(const char *src);
565 GenericValue 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);
571 // char *strdup(const char *src);
572 GenericValue 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])));
578 // char *__strdup(const char *src);
579 GenericValue 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])));
585 // void *memset(void *S, int C, size_t N)
586 GenericValue 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()),
594 // void *memcpy(void *Dest, void *src, size_t Size);
595 GenericValue 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));
602 // void *memcpy(void *Dest, void *src, size_t Size);
603 GenericValue 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));
610 //===----------------------------------------------------------------------===//
612 //===----------------------------------------------------------------------===//
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)
618 // FILE *fopen(const char *filename, const char *mode);
619 GenericValue 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])));
626 // int fclose(FILE *F);
627 GenericValue lle_X_fclose(FunctionType *FT, const vector<GenericValue> &Args) {
628 assert(Args.size() == 1);
630 GV.IntVal = APInt(32, fclose(getFILE(GVTOP(Args[0]))));
634 // int feof(FILE *stream);
635 GenericValue lle_X_feof(FunctionType *FT, const vector<GenericValue> &Args) {
636 assert(Args.size() == 1);
639 GV.IntVal = APInt(32, feof(getFILE(GVTOP(Args[0]))));
643 // size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
644 GenericValue lle_X_fread(FunctionType *FT, const vector<GenericValue> &Args) {
645 assert(Args.size() == 4);
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);
653 // size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
654 GenericValue lle_X_fwrite(FunctionType *FT, const vector<GenericValue> &Args) {
655 assert(Args.size() == 4);
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);
663 // char *fgets(char *s, int n, FILE *stream);
664 GenericValue 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]))));
670 // FILE *freopen(const char *path, const char *mode, FILE *stream);
671 GenericValue 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]))));
678 // int fflush(FILE *stream);
679 GenericValue lle_X_fflush(FunctionType *FT, const vector<GenericValue> &Args) {
680 assert(Args.size() == 1);
682 GV.IntVal = APInt(32, fflush(getFILE(GVTOP(Args[0]))));
686 // int getc(FILE *stream);
687 GenericValue lle_X_getc(FunctionType *FT, const vector<GenericValue> &Args) {
688 assert(Args.size() == 1);
690 GV.IntVal = APInt(32, getc(getFILE(GVTOP(Args[0]))));
694 // int _IO_getc(FILE *stream);
695 GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) {
696 return lle_X_getc(F, Args);
699 // int fputc(int C, FILE *stream);
700 GenericValue lle_X_fputc(FunctionType *FT, const vector<GenericValue> &Args) {
701 assert(Args.size() == 2);
703 GV.IntVal = APInt(32, fputc(Args[0].IntVal.getZExtValue(),
704 getFILE(GVTOP(Args[1]))));
708 // int ungetc(int C, FILE *stream);
709 GenericValue lle_X_ungetc(FunctionType *FT, const vector<GenericValue> &Args) {
710 assert(Args.size() == 2);
712 GV.IntVal = APInt(32, ungetc(Args[0].IntVal.getZExtValue(),
713 getFILE(GVTOP(Args[1]))));
717 // int ferror (FILE *stream);
718 GenericValue lle_X_ferror(FunctionType *FT, const vector<GenericValue> &Args) {
719 assert(Args.size() == 1);
721 GV.IntVal = APInt(32, ferror (getFILE(GVTOP(Args[0]))));
725 // int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
727 GenericValue lle_X_fprintf(FunctionType *FT, const vector<GenericValue> &Args) {
728 assert(Args.size() >= 2);
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);
735 fputs(Buffer, getFILE(GVTOP(Args[0])));
739 // int __cxa_guard_acquire (__guard *g);
740 GenericValue lle_X___cxa_guard_acquire(FunctionType *FT,
741 const vector<GenericValue> &Args) {
742 assert(Args.size() == 1);
745 GV.IntVal = APInt(32, __cxxabiv1::__cxa_guard_acquire (
746 (__cxxabiv1::__guard*)GVTOP(Args[0])));
748 assert(0 && "Can't call __cxa_guard_acquire on this platform");
753 // void __cxa_guard_release (__guard *g);
754 GenericValue lle_X___cxa_guard_release(FunctionType *FT,
755 const vector<GenericValue> &Args) {
756 assert(Args.size() == 1);
758 __cxxabiv1::__cxa_guard_release ((__cxxabiv1::__guard*)GVTOP(Args[0]));
760 assert(0 && "Can't call __cxa_guard_release on this platform");
762 return GenericValue();
768 void 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;
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;
787 FuncNames["lle_X_drand48"] = lle_X_drand48;
788 FuncNames["lle_X_srand48"] = lle_X_srand48;
789 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
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;
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;
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;
823 FuncNames["lle_X___cxa_guard_acquire"] = lle_X___cxa_guard_acquire;
824 FuncNames["lle_X____cxa_guard_release"] = lle_X___cxa_guard_release;