1 //===-- ExternalFunctions.cpp - Implement External Functions --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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"
37 typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
38 static ManagedStatic<std::map<const Function *, ExFunc> > Functions;
39 static std::map<std::string, ExFunc> FuncNames;
41 static Interpreter *TheInterpreter;
43 static char getTypeID(const Type *Ty) {
44 switch (Ty->getTypeID()) {
45 case Type::VoidTyID: return 'V';
46 case Type::IntegerTyID:
47 switch (cast<IntegerType>(Ty)->getBitWidth()) {
55 case Type::FloatTyID: return 'F';
56 case Type::DoubleTyID: return 'D';
57 case Type::PointerTyID: return 'P';
58 case Type::FunctionTyID:return 'M';
59 case Type::StructTyID: return 'T';
60 case Type::ArrayTyID: return 'A';
61 case Type::OpaqueTyID: return 'O';
66 // Try to find address of external function given a Function object.
67 // Please note, that interpreter doesn't know how to assemble a
68 // real call in general case (this is JIT job), that's why it assumes,
69 // that all external functions has the same (and pretty "general") signature.
70 // The typical example of such functions are "lle_X_" ones.
71 static ExFunc lookupFunction(const Function *F) {
72 // Function not found, look it up... start by figuring out what the
73 // composite function name should be.
74 std::string ExtName = "lle_";
75 const FunctionType *FT = F->getFunctionType();
76 for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
77 ExtName += getTypeID(FT->getContainedType(i));
78 ExtName += "_" + F->getName();
80 ExFunc FnPtr = FuncNames[ExtName];
82 FnPtr = FuncNames["lle_X_"+F->getName()];
83 if (FnPtr == 0) // Try calling a generic function... if it exists...
84 FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(
85 ("lle_X_"+F->getName()).c_str());
87 FnPtr = (ExFunc)(intptr_t)
88 sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
90 Functions->insert(std::make_pair(F, FnPtr)); // Cache for later
94 GenericValue Interpreter::callExternalFunction(Function *F,
95 const std::vector<GenericValue> &ArgVals) {
96 TheInterpreter = this;
98 // Do a lookup to see if the function is in our cache... this should just be a
99 // deferred annotation!
100 std::map<const Function *, ExFunc>::iterator FI = Functions->find(F);
101 ExFunc Fn = (FI == Functions->end()) ? lookupFunction(F) : FI->second;
103 cerr << "Tried to execute an unknown external function: "
104 << F->getType()->getDescription() << " " << F->getName() << "\n";
105 if (F->getName() == "__main")
106 return GenericValue();
110 // TODO: FIXME when types are not const!
111 GenericValue Result = Fn(const_cast<FunctionType*>(F->getFunctionType()),
117 //===----------------------------------------------------------------------===//
118 // Functions "exported" to the running application...
120 extern "C" { // Don't add C++ manglings to llvm mangling :)
122 // void putchar(ubyte)
123 GenericValue lle_X_putchar(FunctionType *FT, const vector<GenericValue> &Args){
124 cout << ((char)Args[0].IntVal.getZExtValue()) << std::flush;
128 // void _IO_putc(int c, FILE* fp)
129 GenericValue lle_X__IO_putc(FunctionType *FT, const vector<GenericValue> &Args){
131 _IO_putc((char)Args[0].IntVal.getZExtValue(), (FILE*) Args[1].PointerVal);
133 assert(0 && "Can't call _IO_putc on this platform");
138 // void atexit(Function*)
139 GenericValue lle_X_atexit(FunctionType *FT, const vector<GenericValue> &Args) {
140 assert(Args.size() == 1);
141 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
148 GenericValue lle_X_exit(FunctionType *FT, const vector<GenericValue> &Args) {
149 TheInterpreter->exitCalled(Args[0]);
150 return GenericValue();
154 GenericValue lle_X_abort(FunctionType *FT, const vector<GenericValue> &Args) {
156 return GenericValue();
159 // void *malloc(uint)
160 GenericValue lle_X_malloc(FunctionType *FT, const vector<GenericValue> &Args) {
161 assert(Args.size() == 1 && "Malloc expects one argument!");
162 assert(isa<PointerType>(FT->getReturnType()) && "malloc must return pointer");
163 return PTOGV(malloc(Args[0].IntVal.getZExtValue()));
166 // void *calloc(uint, uint)
167 GenericValue lle_X_calloc(FunctionType *FT, const vector<GenericValue> &Args) {
168 assert(Args.size() == 2 && "calloc expects two arguments!");
169 assert(isa<PointerType>(FT->getReturnType()) && "calloc must return pointer");
170 return PTOGV(calloc(Args[0].IntVal.getZExtValue(),
171 Args[1].IntVal.getZExtValue()));
174 // void *calloc(uint, uint)
175 GenericValue lle_X_realloc(FunctionType *FT, const vector<GenericValue> &Args) {
176 assert(Args.size() == 2 && "calloc expects two arguments!");
177 assert(isa<PointerType>(FT->getReturnType()) &&"realloc must return pointer");
178 return PTOGV(realloc(GVTOP(Args[0]), Args[1].IntVal.getZExtValue()));
182 GenericValue lle_X_free(FunctionType *FT, const vector<GenericValue> &Args) {
183 assert(Args.size() == 1);
184 free(GVTOP(Args[0]));
185 return GenericValue();
189 GenericValue lle_X_atoi(FunctionType *FT, const vector<GenericValue> &Args) {
190 assert(Args.size() == 1);
192 GV.IntVal = APInt(32, atoi((char*)GVTOP(Args[0])));
196 // double pow(double, double)
197 GenericValue lle_X_pow(FunctionType *FT, const vector<GenericValue> &Args) {
198 assert(Args.size() == 2);
200 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
204 // double sin(double)
205 GenericValue lle_X_sin(FunctionType *FT, const vector<GenericValue> &Args) {
206 assert(Args.size() == 1);
208 GV.DoubleVal = sin(Args[0].DoubleVal);
212 // double cos(double)
213 GenericValue lle_X_cos(FunctionType *FT, const vector<GenericValue> &Args) {
214 assert(Args.size() == 1);
216 GV.DoubleVal = cos(Args[0].DoubleVal);
220 // double exp(double)
221 GenericValue lle_X_exp(FunctionType *FT, const vector<GenericValue> &Args) {
222 assert(Args.size() == 1);
224 GV.DoubleVal = exp(Args[0].DoubleVal);
228 // double sqrt(double)
229 GenericValue lle_X_sqrt(FunctionType *FT, const vector<GenericValue> &Args) {
230 assert(Args.size() == 1);
232 GV.DoubleVal = sqrt(Args[0].DoubleVal);
236 // double log(double)
237 GenericValue lle_X_log(FunctionType *FT, const vector<GenericValue> &Args) {
238 assert(Args.size() == 1);
240 GV.DoubleVal = log(Args[0].DoubleVal);
244 // double floor(double)
245 GenericValue lle_X_floor(FunctionType *FT, const vector<GenericValue> &Args) {
246 assert(Args.size() == 1);
248 GV.DoubleVal = floor(Args[0].DoubleVal);
255 GenericValue lle_X_drand48(FunctionType *FT, const vector<GenericValue> &Args) {
256 assert(Args.size() == 0);
258 GV.DoubleVal = drand48();
263 GenericValue lle_X_lrand48(FunctionType *FT, const vector<GenericValue> &Args) {
264 assert(Args.size() == 0);
266 GV.IntVal = APInt(32, lrand48());
270 // void srand48(long)
271 GenericValue lle_X_srand48(FunctionType *FT, const vector<GenericValue> &Args) {
272 assert(Args.size() == 1);
273 srand48(Args[0].IntVal.getZExtValue());
274 return GenericValue();
280 GenericValue lle_X_rand(FunctionType *FT, const vector<GenericValue> &Args) {
281 assert(Args.size() == 0);
283 GV.IntVal = APInt(32, rand());
288 GenericValue lle_X_srand(FunctionType *FT, const vector<GenericValue> &Args) {
289 assert(Args.size() == 1);
290 srand(Args[0].IntVal.getZExtValue());
291 return GenericValue();
294 // int puts(const char*)
295 GenericValue lle_X_puts(FunctionType *FT, const vector<GenericValue> &Args) {
296 assert(Args.size() == 1);
298 GV.IntVal = APInt(32, puts((char*)GVTOP(Args[0])));
302 // int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
304 GenericValue lle_X_sprintf(FunctionType *FT, const vector<GenericValue> &Args) {
305 char *OutputBuffer = (char *)GVTOP(Args[0]);
306 const char *FmtStr = (const char *)GVTOP(Args[1]);
309 // printf should return # chars printed. This is completely incorrect, but
310 // close enough for now.
312 GV.IntVal = APInt(32, strlen(FmtStr));
315 case 0: return GV; // Null terminator...
316 default: // Normal nonspecial character
317 sprintf(OutputBuffer++, "%c", *FmtStr++);
319 case '\\': { // Handle escape codes
320 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
321 FmtStr += 2; OutputBuffer += 2;
324 case '%': { // Handle format specifiers
325 char FmtBuf[100] = "", Buffer[1000] = "";
328 char Last = *FB++ = *FmtStr++;
329 unsigned HowLong = 0;
330 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
331 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
332 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
333 Last != 'p' && Last != 's' && Last != '%') {
334 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
335 Last = *FB++ = *FmtStr++;
341 sprintf(Buffer, FmtBuf); break;
343 sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
350 TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 &&
351 sizeof(long) < sizeof(int64_t)) {
352 // Make sure we use %lld with a 64 bit argument because we might be
353 // compiling LLI on a 32 bit compiler.
354 unsigned Size = strlen(FmtBuf);
355 FmtBuf[Size] = FmtBuf[Size-1];
357 FmtBuf[Size-1] = 'l';
359 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
361 sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
363 case 'e': case 'E': case 'g': case 'G': case 'f':
364 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
366 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
368 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
369 default: cerr << "<unknown printf code '" << *FmtStr << "'!>";
372 strcpy(OutputBuffer, Buffer);
373 OutputBuffer += strlen(Buffer);
381 // int printf(sbyte *, ...) - a very rough implementation to make output useful.
382 GenericValue lle_X_printf(FunctionType *FT, const vector<GenericValue> &Args) {
384 vector<GenericValue> NewArgs;
385 NewArgs.push_back(PTOGV((void*)&Buffer[0]));
386 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
387 GenericValue GV = lle_X_sprintf(FT, NewArgs);
392 static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1,
393 void *Arg2, void *Arg3, void *Arg4, void *Arg5,
394 void *Arg6, void *Arg7, void *Arg8) {
395 void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 };
397 // Loop over the format string, munging read values as appropriate (performs
398 // byteswaps as necessary).
402 // Read any flag characters that may be present...
403 bool Suppress = false;
406 bool LongLong = false; // long long or long double
410 case '*': Suppress = true; break;
411 case 'a': /*Allocate = true;*/ break; // We don't need to track this
412 case 'h': Half = true; break;
413 case 'l': Long = true; break;
415 case 'L': LongLong = true; break;
417 if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs
423 // Read the conversion character
424 if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
429 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
431 if (Long || LongLong) {
432 Size = 8; Ty = Type::Int64Ty;
434 Size = 4; Ty = Type::Int16Ty;
436 Size = 4; Ty = Type::Int32Ty;
440 case 'e': case 'g': case 'E':
442 if (Long || LongLong) {
443 Size = 8; Ty = Type::DoubleTy;
445 Size = 4; Ty = Type::FloatTy;
449 case 's': case 'c': case '[': // No byteswap needed
459 void *Arg = Args[ArgNo++];
460 memcpy(&GV, Arg, Size);
461 TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
468 // int sscanf(const char *format, ...);
469 GenericValue lle_X_sscanf(FunctionType *FT, const vector<GenericValue> &args) {
470 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
473 for (unsigned i = 0; i < args.size(); ++i)
474 Args[i] = (char*)GVTOP(args[i]);
477 GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
478 Args[5], Args[6], Args[7], Args[8], Args[9]));
479 ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4],
480 Args[5], Args[6], Args[7], Args[8], Args[9], 0);
484 // int scanf(const char *format, ...);
485 GenericValue lle_X_scanf(FunctionType *FT, const vector<GenericValue> &args) {
486 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
489 for (unsigned i = 0; i < args.size(); ++i)
490 Args[i] = (char*)GVTOP(args[i]);
493 GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
494 Args[5], Args[6], Args[7], Args[8], Args[9]));
495 ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4],
496 Args[5], Args[6], Args[7], Args[8], Args[9]);
501 // int clock(void) - Profiling implementation
502 GenericValue lle_i_clock(FunctionType *FT, const vector<GenericValue> &Args) {
503 extern unsigned int clock(void);
505 GV.IntVal = APInt(32, clock());
510 //===----------------------------------------------------------------------===//
511 // String Functions...
512 //===----------------------------------------------------------------------===//
514 // int strcmp(const char *S1, const char *S2);
515 GenericValue lle_X_strcmp(FunctionType *FT, const vector<GenericValue> &Args) {
516 assert(Args.size() == 2);
518 Ret.IntVal = APInt(32, strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
522 // char *strcat(char *Dest, const char *src);
523 GenericValue lle_X_strcat(FunctionType *FT, const vector<GenericValue> &Args) {
524 assert(Args.size() == 2);
525 assert(isa<PointerType>(FT->getReturnType()) &&"strcat must return pointer");
526 return PTOGV(strcat((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
529 // char *strcpy(char *Dest, const char *src);
530 GenericValue lle_X_strcpy(FunctionType *FT, const vector<GenericValue> &Args) {
531 assert(Args.size() == 2);
532 assert(isa<PointerType>(FT->getReturnType()) &&"strcpy must return pointer");
533 return PTOGV(strcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
536 static GenericValue size_t_to_GV (size_t n) {
538 if (sizeof (size_t) == sizeof (uint64_t)) {
539 Ret.IntVal = APInt(64, n);
541 assert (sizeof (size_t) == sizeof (unsigned int));
542 Ret.IntVal = APInt(32, n);
547 static size_t GV_to_size_t (GenericValue GV) {
549 if (sizeof (size_t) == sizeof (uint64_t)) {
550 count = (size_t)GV.IntVal.getZExtValue();
552 assert (sizeof (size_t) == sizeof (unsigned int));
553 count = (size_t)GV.IntVal.getZExtValue();
558 // size_t strlen(const char *src);
559 GenericValue lle_X_strlen(FunctionType *FT, const vector<GenericValue> &Args) {
560 assert(Args.size() == 1);
561 size_t strlenResult = strlen ((char *) GVTOP (Args[0]));
562 return size_t_to_GV (strlenResult);
565 // char *strdup(const char *src);
566 GenericValue lle_X_strdup(FunctionType *FT, const vector<GenericValue> &Args) {
567 assert(Args.size() == 1);
568 assert(isa<PointerType>(FT->getReturnType()) && "strdup must return pointer");
569 return PTOGV(strdup((char*)GVTOP(Args[0])));
572 // char *__strdup(const char *src);
573 GenericValue lle_X___strdup(FunctionType *FT, const vector<GenericValue> &Args) {
574 assert(Args.size() == 1);
575 assert(isa<PointerType>(FT->getReturnType()) &&"_strdup must return pointer");
576 return PTOGV(strdup((char*)GVTOP(Args[0])));
579 // void *memset(void *S, int C, size_t N)
580 GenericValue lle_X_memset(FunctionType *FT, const vector<GenericValue> &Args) {
581 assert(Args.size() == 3);
582 size_t count = GV_to_size_t (Args[2]);
583 assert(isa<PointerType>(FT->getReturnType()) && "memset must return pointer");
584 return PTOGV(memset(GVTOP(Args[0]), uint32_t(Args[1].IntVal.getZExtValue()),
588 // void *memcpy(void *Dest, void *src, size_t Size);
589 GenericValue lle_X_memcpy(FunctionType *FT, const vector<GenericValue> &Args) {
590 assert(Args.size() == 3);
591 assert(isa<PointerType>(FT->getReturnType()) && "memcpy must return pointer");
592 size_t count = GV_to_size_t (Args[2]);
593 return PTOGV(memcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));
596 //===----------------------------------------------------------------------===//
598 //===----------------------------------------------------------------------===//
600 // getFILE - Turn a pointer in the host address space into a legit pointer in
601 // the interpreter address space. This is an identity transformation.
602 #define getFILE(ptr) ((FILE*)ptr)
604 // FILE *fopen(const char *filename, const char *mode);
605 GenericValue lle_X_fopen(FunctionType *FT, const vector<GenericValue> &Args) {
606 assert(Args.size() == 2);
607 assert(isa<PointerType>(FT->getReturnType()) && "fopen must return pointer");
608 return PTOGV(fopen((const char *)GVTOP(Args[0]),
609 (const char *)GVTOP(Args[1])));
612 // int fclose(FILE *F);
613 GenericValue lle_X_fclose(FunctionType *FT, const vector<GenericValue> &Args) {
614 assert(Args.size() == 1);
616 GV.IntVal = APInt(32, fclose(getFILE(GVTOP(Args[0]))));
620 // int feof(FILE *stream);
621 GenericValue lle_X_feof(FunctionType *FT, const vector<GenericValue> &Args) {
622 assert(Args.size() == 1);
625 GV.IntVal = APInt(32, feof(getFILE(GVTOP(Args[0]))));
629 // size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
630 GenericValue lle_X_fread(FunctionType *FT, const vector<GenericValue> &Args) {
631 assert(Args.size() == 4);
634 result = fread((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
635 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
636 return size_t_to_GV (result);
639 // size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
640 GenericValue lle_X_fwrite(FunctionType *FT, const vector<GenericValue> &Args) {
641 assert(Args.size() == 4);
644 result = fwrite((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
645 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
646 return size_t_to_GV (result);
649 // char *fgets(char *s, int n, FILE *stream);
650 GenericValue lle_X_fgets(FunctionType *FT, const vector<GenericValue> &Args) {
651 assert(Args.size() == 3);
652 return GVTOP(fgets((char*)GVTOP(Args[0]), Args[1].IntVal.getZExtValue(),
653 getFILE(GVTOP(Args[2]))));
656 // FILE *freopen(const char *path, const char *mode, FILE *stream);
657 GenericValue lle_X_freopen(FunctionType *FT, const vector<GenericValue> &Args) {
658 assert(Args.size() == 3);
659 assert(isa<PointerType>(FT->getReturnType()) &&"freopen must return pointer");
660 return PTOGV(freopen((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]),
661 getFILE(GVTOP(Args[2]))));
664 // int fflush(FILE *stream);
665 GenericValue lle_X_fflush(FunctionType *FT, const vector<GenericValue> &Args) {
666 assert(Args.size() == 1);
668 GV.IntVal = APInt(32, fflush(getFILE(GVTOP(Args[0]))));
672 // int getc(FILE *stream);
673 GenericValue lle_X_getc(FunctionType *FT, const vector<GenericValue> &Args) {
674 assert(Args.size() == 1);
676 GV.IntVal = APInt(32, getc(getFILE(GVTOP(Args[0]))));
680 // int _IO_getc(FILE *stream);
681 GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) {
682 return lle_X_getc(F, Args);
685 // int fputc(int C, FILE *stream);
686 GenericValue lle_X_fputc(FunctionType *FT, const vector<GenericValue> &Args) {
687 assert(Args.size() == 2);
689 GV.IntVal = APInt(32, fputc(Args[0].IntVal.getZExtValue(),
690 getFILE(GVTOP(Args[1]))));
694 // int ungetc(int C, FILE *stream);
695 GenericValue lle_X_ungetc(FunctionType *FT, const vector<GenericValue> &Args) {
696 assert(Args.size() == 2);
698 GV.IntVal = APInt(32, ungetc(Args[0].IntVal.getZExtValue(),
699 getFILE(GVTOP(Args[1]))));
703 // int ferror (FILE *stream);
704 GenericValue lle_X_ferror(FunctionType *FT, const vector<GenericValue> &Args) {
705 assert(Args.size() == 1);
707 GV.IntVal = APInt(32, ferror (getFILE(GVTOP(Args[0]))));
711 // int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
713 GenericValue lle_X_fprintf(FunctionType *FT, const vector<GenericValue> &Args) {
714 assert(Args.size() >= 2);
716 vector<GenericValue> NewArgs;
717 NewArgs.push_back(PTOGV(Buffer));
718 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
719 GenericValue GV = lle_X_sprintf(FT, NewArgs);
721 fputs(Buffer, getFILE(GVTOP(Args[0])));
725 // int __cxa_guard_acquire (__guard *g);
726 GenericValue lle_X___cxa_guard_acquire(FunctionType *FT,
727 const vector<GenericValue> &Args) {
728 assert(Args.size() == 1);
730 GV.IntVal = APInt(32, __cxxabiv1::__cxa_guard_acquire (
731 (__cxxabiv1::__guard*)GVTOP(Args[0])));
735 // void __cxa_guard_release (__guard *g);
736 GenericValue lle_X___cxa_guard_release(FunctionType *FT,
737 const vector<GenericValue> &Args) {
738 assert(Args.size() == 1);
739 __cxxabiv1::__cxa_guard_release ((__cxxabiv1::__guard*)GVTOP(Args[0]));
740 return GenericValue();
746 void Interpreter::initializeExternalFunctions() {
747 FuncNames["lle_X_putchar"] = lle_X_putchar;
748 FuncNames["lle_X__IO_putc"] = lle_X__IO_putc;
749 FuncNames["lle_X_exit"] = lle_X_exit;
750 FuncNames["lle_X_abort"] = lle_X_abort;
751 FuncNames["lle_X_malloc"] = lle_X_malloc;
752 FuncNames["lle_X_calloc"] = lle_X_calloc;
753 FuncNames["lle_X_realloc"] = lle_X_realloc;
754 FuncNames["lle_X_free"] = lle_X_free;
755 FuncNames["lle_X_atoi"] = lle_X_atoi;
756 FuncNames["lle_X_pow"] = lle_X_pow;
757 FuncNames["lle_X_sin"] = lle_X_sin;
758 FuncNames["lle_X_cos"] = lle_X_cos;
759 FuncNames["lle_X_exp"] = lle_X_exp;
760 FuncNames["lle_X_log"] = lle_X_log;
761 FuncNames["lle_X_floor"] = lle_X_floor;
762 FuncNames["lle_X_srand"] = lle_X_srand;
763 FuncNames["lle_X_rand"] = lle_X_rand;
765 FuncNames["lle_X_drand48"] = lle_X_drand48;
766 FuncNames["lle_X_srand48"] = lle_X_srand48;
767 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
769 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
770 FuncNames["lle_X_puts"] = lle_X_puts;
771 FuncNames["lle_X_printf"] = lle_X_printf;
772 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
773 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
774 FuncNames["lle_X_scanf"] = lle_X_scanf;
775 FuncNames["lle_i_clock"] = lle_i_clock;
777 FuncNames["lle_X_strcmp"] = lle_X_strcmp;
778 FuncNames["lle_X_strcat"] = lle_X_strcat;
779 FuncNames["lle_X_strcpy"] = lle_X_strcpy;
780 FuncNames["lle_X_strlen"] = lle_X_strlen;
781 FuncNames["lle_X___strdup"] = lle_X___strdup;
782 FuncNames["lle_X_memset"] = lle_X_memset;
783 FuncNames["lle_X_memcpy"] = lle_X_memcpy;
785 FuncNames["lle_X_fopen"] = lle_X_fopen;
786 FuncNames["lle_X_fclose"] = lle_X_fclose;
787 FuncNames["lle_X_feof"] = lle_X_feof;
788 FuncNames["lle_X_fread"] = lle_X_fread;
789 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
790 FuncNames["lle_X_fgets"] = lle_X_fgets;
791 FuncNames["lle_X_fflush"] = lle_X_fflush;
792 FuncNames["lle_X_fgetc"] = lle_X_getc;
793 FuncNames["lle_X_getc"] = lle_X_getc;
794 FuncNames["lle_X__IO_getc"] = lle_X__IO_getc;
795 FuncNames["lle_X_fputc"] = lle_X_fputc;
796 FuncNames["lle_X_ungetc"] = lle_X_ungetc;
797 FuncNames["lle_X_fprintf"] = lle_X_fprintf;
798 FuncNames["lle_X_freopen"] = lle_X_freopen;
800 FuncNames["lle_X___cxa_guard_acquire"] = lle_X___cxa_guard_acquire;
801 FuncNames["lle_X____cxa_guard_release"] = lle_X___cxa_guard_release;