1 //===-- ExternalFunctions.cpp - Implement External Functions --------------===//
3 // This file contains both code to deal with invoking "external" functions, but
4 // also contains code that implements "exported" external functions.
6 // External functions in LLI are implemented by dlopen'ing the lli executable
7 // and using dlsym to look op the functions that we want to invoke. If a
8 // function is found, then the arguments are mangled and passed in to the
11 //===----------------------------------------------------------------------===//
13 #include "Interpreter.h"
14 #include "ExecutionAnnotations.h"
15 #include "llvm/Module.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/SymbolTable.h"
18 #include "llvm/Target/TargetData.h"
27 typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
28 static std::map<const Function *, ExFunc> Functions;
29 static std::map<std::string, ExFunc> FuncNames;
31 static Interpreter *TheInterpreter;
33 // getCurrentExecutablePath() - Return the directory that the lli executable
36 std::string Interpreter::getCurrentExecutablePath() const {
38 if (dladdr(&TheInterpreter, &Info) == 0) return "";
40 std::string LinkAddr(Info.dli_fname);
41 unsigned SlashPos = LinkAddr.rfind('/');
42 if (SlashPos != std::string::npos)
43 LinkAddr.resize(SlashPos); // Trim the executable name off...
49 static char getTypeID(const Type *Ty) {
50 switch (Ty->getPrimitiveID()) {
51 case Type::VoidTyID: return 'V';
52 case Type::BoolTyID: return 'o';
53 case Type::UByteTyID: return 'B';
54 case Type::SByteTyID: return 'b';
55 case Type::UShortTyID: return 'S';
56 case Type::ShortTyID: return 's';
57 case Type::UIntTyID: return 'I';
58 case Type::IntTyID: return 'i';
59 case Type::ULongTyID: return 'L';
60 case Type::LongTyID: return 'l';
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 static ExFunc lookupFunction(const Function *M) {
73 // Function not found, look it up... start by figuring out what the
74 // composite function name should be.
75 std::string ExtName = "lle_";
76 const FunctionType *MT = M->getFunctionType();
77 for (unsigned i = 0; const Type *Ty = MT->getContainedType(i); ++i)
78 ExtName += getTypeID(Ty);
79 ExtName += "_" + M->getName();
81 //cout << "Tried: '" << ExtName << "'\n";
82 ExFunc FnPtr = FuncNames[ExtName];
84 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ExtName.c_str());
86 FnPtr = FuncNames["lle_X_"+M->getName()];
87 if (FnPtr == 0) // Try calling a generic function... if it exists...
88 FnPtr = (ExFunc)dlsym(RTLD_DEFAULT, ("lle_X_"+M->getName()).c_str());
90 Functions.insert(std::make_pair(M, FnPtr)); // Cache for later
94 GenericValue Interpreter::callExternalMethod(Function *M,
95 const 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 // defered annotation!
100 std::map<const Function *, ExFunc>::iterator FI = Functions.find(M);
101 ExFunc Fn = (FI == Functions.end()) ? lookupFunction(M) : FI->second;
103 cout << "Tried to execute an unknown external function: "
104 << M->getType()->getDescription() << " " << M->getName() << "\n";
105 return GenericValue();
108 // TODO: FIXME when types are not const!
109 GenericValue Result = Fn(const_cast<FunctionType*>(M->getFunctionType()),
115 //===----------------------------------------------------------------------===//
116 // Functions "exported" to the running application...
118 extern "C" { // Don't add C++ manglings to llvm mangling :)
120 // Implement void printstr([ubyte {x N}] *)
121 GenericValue lle_VP_printstr(FunctionType *M,
122 const vector<GenericValue> &ArgVal){
123 assert(ArgVal.size() == 1 && "printstr only takes one argument!");
124 cout << (char*)GVTOP(ArgVal[0]);
125 return GenericValue();
128 // Implement 'void print(X)' for every type...
129 GenericValue lle_X_print(FunctionType *M, const vector<GenericValue> &ArgVals) {
130 assert(ArgVals.size() == 1 && "generic print only takes one argument!");
132 Interpreter::print(M->getParamTypes()[0], ArgVals[0]);
133 return GenericValue();
136 // Implement 'void printVal(X)' for every type...
137 GenericValue lle_X_printVal(FunctionType *M,
138 const vector<GenericValue> &ArgVal) {
139 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
141 // Specialize print([ubyte {x N} ] *) and print(sbyte *)
142 if (const PointerType *PTy =
143 dyn_cast<PointerType>(M->getParamTypes()[0].get()))
144 if (PTy->getElementType() == Type::SByteTy ||
145 isa<ArrayType>(PTy->getElementType())) {
146 return lle_VP_printstr(M, ArgVal);
149 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);
150 return GenericValue();
153 // Implement 'void printString(X)'
154 // Argument must be [ubyte {x N} ] * or sbyte *
155 GenericValue lle_X_printString(FunctionType *M,
156 const vector<GenericValue> &ArgVal) {
157 assert(ArgVal.size() == 1 && "generic print only takes one argument!");
158 return lle_VP_printstr(M, ArgVal);
161 // Implement 'void print<TYPE>(X)' for each primitive type or pointer type
162 #define PRINT_TYPE_FUNC(TYPENAME,TYPEID) \
163 GenericValue lle_X_print##TYPENAME(FunctionType *M,\
164 const vector<GenericValue> &ArgVal) {\
165 assert(ArgVal.size() == 1 && "generic print only takes one argument!");\
166 assert(M->getParamTypes()[0].get()->getPrimitiveID() == Type::TYPEID);\
167 Interpreter::printValue(M->getParamTypes()[0], ArgVal[0]);\
168 return GenericValue();\
171 PRINT_TYPE_FUNC(SByte, SByteTyID)
172 PRINT_TYPE_FUNC(UByte, UByteTyID)
173 PRINT_TYPE_FUNC(Short, ShortTyID)
174 PRINT_TYPE_FUNC(UShort, UShortTyID)
175 PRINT_TYPE_FUNC(Int, IntTyID)
176 PRINT_TYPE_FUNC(UInt, UIntTyID)
177 PRINT_TYPE_FUNC(Long, LongTyID)
178 PRINT_TYPE_FUNC(ULong, ULongTyID)
179 PRINT_TYPE_FUNC(Float, FloatTyID)
180 PRINT_TYPE_FUNC(Double, DoubleTyID)
181 PRINT_TYPE_FUNC(Pointer, PointerTyID)
184 // void putchar(sbyte)
185 GenericValue lle_Vb_putchar(FunctionType *M, const vector<GenericValue> &Args) {
186 cout << Args[0].SByteVal;
187 return GenericValue();
191 GenericValue lle_ii_putchar(FunctionType *M, const vector<GenericValue> &Args) {
192 cout << ((char)Args[0].IntVal) << std::flush;
196 // void putchar(ubyte)
197 GenericValue lle_VB_putchar(FunctionType *M, const vector<GenericValue> &Args) {
198 cout << Args[0].SByteVal << std::flush;
203 GenericValue lle_V___main(FunctionType *M, const vector<GenericValue> &Args) {
204 return GenericValue();
208 GenericValue lle_X_exit(FunctionType *M, const vector<GenericValue> &Args) {
209 TheInterpreter->exitCalled(Args[0]);
210 return GenericValue();
214 GenericValue lle_X_abort(FunctionType *M, const vector<GenericValue> &Args) {
215 std::cerr << "***PROGRAM ABORTED***!\n";
218 TheInterpreter->exitCalled(GV);
219 return GenericValue();
222 // void *malloc(uint)
223 GenericValue lle_X_malloc(FunctionType *M, const vector<GenericValue> &Args) {
224 assert(Args.size() == 1 && "Malloc expects one argument!");
225 return PTOGV(malloc(Args[0].UIntVal));
228 // void *calloc(uint, uint)
229 GenericValue lle_X_calloc(FunctionType *M, const vector<GenericValue> &Args) {
230 assert(Args.size() == 2 && "calloc expects two arguments!");
231 return PTOGV(calloc(Args[0].UIntVal, Args[1].UIntVal));
235 GenericValue lle_X_free(FunctionType *M, const vector<GenericValue> &Args) {
236 assert(Args.size() == 1);
237 free(GVTOP(Args[0]));
238 return GenericValue();
242 GenericValue lle_X_atoi(FunctionType *M, const vector<GenericValue> &Args) {
243 assert(Args.size() == 1);
245 GV.IntVal = atoi((char*)GVTOP(Args[0]));
249 // double pow(double, double)
250 GenericValue lle_X_pow(FunctionType *M, const vector<GenericValue> &Args) {
251 assert(Args.size() == 2);
253 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
257 // double exp(double)
258 GenericValue lle_X_exp(FunctionType *M, const vector<GenericValue> &Args) {
259 assert(Args.size() == 1);
261 GV.DoubleVal = exp(Args[0].DoubleVal);
265 // double sqrt(double)
266 GenericValue lle_X_sqrt(FunctionType *M, const vector<GenericValue> &Args) {
267 assert(Args.size() == 1);
269 GV.DoubleVal = sqrt(Args[0].DoubleVal);
273 // double log(double)
274 GenericValue lle_X_log(FunctionType *M, const vector<GenericValue> &Args) {
275 assert(Args.size() == 1);
277 GV.DoubleVal = log(Args[0].DoubleVal);
281 // int isnan(double value);
282 GenericValue lle_X_isnan(FunctionType *F, const vector<GenericValue> &Args) {
283 assert(Args.size() == 1);
285 GV.IntVal = isnan(Args[0].DoubleVal);
289 // double floor(double)
290 GenericValue lle_X_floor(FunctionType *M, const vector<GenericValue> &Args) {
291 assert(Args.size() == 1);
293 GV.DoubleVal = floor(Args[0].DoubleVal);
298 GenericValue lle_X_drand48(FunctionType *M, const vector<GenericValue> &Args) {
299 assert(Args.size() == 0);
301 GV.DoubleVal = drand48();
306 GenericValue lle_X_lrand48(FunctionType *M, const vector<GenericValue> &Args) {
307 assert(Args.size() == 0);
309 GV.IntVal = lrand48();
313 // void srand48(long)
314 GenericValue lle_X_srand48(FunctionType *M, const vector<GenericValue> &Args) {
315 assert(Args.size() == 1);
316 srand48(Args[0].IntVal);
317 return GenericValue();
321 GenericValue lle_X_srand(FunctionType *M, const vector<GenericValue> &Args) {
322 assert(Args.size() == 1);
323 srand(Args[0].UIntVal);
324 return GenericValue();
327 // int puts(const char*)
328 GenericValue lle_X_puts(FunctionType *M, const vector<GenericValue> &Args) {
329 assert(Args.size() == 1);
331 GV.IntVal = puts((char*)GVTOP(Args[0]));
335 // int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
337 GenericValue lle_X_sprintf(FunctionType *M, const vector<GenericValue> &Args) {
338 char *OutputBuffer = (char *)GVTOP(Args[0]);
339 const char *FmtStr = (const char *)GVTOP(Args[1]);
342 // printf should return # chars printed. This is completely incorrect, but
343 // close enough for now.
344 GenericValue GV; GV.IntVal = strlen(FmtStr);
347 case 0: return GV; // Null terminator...
348 default: // Normal nonspecial character
349 sprintf(OutputBuffer++, "%c", *FmtStr++);
351 case '\\': { // Handle escape codes
352 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
353 FmtStr += 2; OutputBuffer += 2;
356 case '%': { // Handle format specifiers
357 char FmtBuf[100] = "", Buffer[1000] = "";
360 char Last = *FB++ = *FmtStr++;
361 unsigned HowLong = 0;
362 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
363 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
364 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
365 Last != 'p' && Last != 's' && Last != '%') {
366 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
367 Last = *FB++ = *FmtStr++;
373 sprintf(Buffer, FmtBuf); break;
375 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
380 if (HowLong == 1 && TheInterpreter->getModule().has64BitPointers() &&
381 sizeof(long) < sizeof(long long)) {
382 // Make sure we use %lld with a 64 bit argument because we might be
383 // compiling LLI on a 32 bit compiler.
384 unsigned Size = strlen(FmtBuf);
385 FmtBuf[Size] = FmtBuf[Size-1];
387 FmtBuf[Size-1] = 'l';
389 sprintf(Buffer, FmtBuf, Args[ArgNo++].ULongVal);
391 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
392 case 'e': case 'E': case 'g': case 'G': case 'f':
393 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
395 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
397 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
398 default: cout << "<unknown printf code '" << *FmtStr << "'!>";
401 strcpy(OutputBuffer, Buffer);
402 OutputBuffer += strlen(Buffer);
409 // int printf(sbyte *, ...) - a very rough implementation to make output useful.
410 GenericValue lle_X_printf(FunctionType *M, const vector<GenericValue> &Args) {
412 vector<GenericValue> NewArgs;
413 NewArgs.push_back(PTOGV(Buffer));
414 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
415 GenericValue GV = lle_X_sprintf(M, NewArgs);
420 static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1,
421 void *Arg2, void *Arg3, void *Arg4, void *Arg5,
422 void *Arg6, void *Arg7, void *Arg8) {
423 void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 };
425 // Loop over the format string, munging read values as appropriate (performs
426 // byteswaps as neccesary).
430 // Read any flag characters that may be present...
431 bool Suppress = false;
434 bool LongLong = false; // long long or long double
438 case '*': Suppress = true; break;
439 case 'a': /*Allocate = true;*/ break; // We don't need to track this
440 case 'h': Half = true; break;
441 case 'l': Long = true; break;
443 case 'L': LongLong = true; break;
445 if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs
451 // Read the conversion character
452 if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
457 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
459 if (Long || LongLong) {
460 Size = 8; Ty = Type::ULongTy;
462 Size = 4; Ty = Type::UShortTy;
464 Size = 4; Ty = Type::UIntTy;
468 case 'e': case 'g': case 'E':
470 if (Long || LongLong) {
471 Size = 8; Ty = Type::DoubleTy;
473 Size = 4; Ty = Type::FloatTy;
477 case 's': case 'c': case '[': // No byteswap needed
487 void *Arg = Args[ArgNo++];
488 memcpy(&GV, Arg, Size);
489 TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
496 // int sscanf(const char *format, ...);
497 GenericValue lle_X_sscanf(FunctionType *M, const vector<GenericValue> &args) {
498 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
501 for (unsigned i = 0; i < args.size(); ++i)
502 Args[i] = (char*)GVTOP(args[i]);
505 GV.IntVal = sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
506 Args[5], Args[6], Args[7], Args[8], Args[9]);
507 ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4],
508 Args[5], Args[6], Args[7], Args[8], Args[9], 0);
512 // int scanf(const char *format, ...);
513 GenericValue lle_X_scanf(FunctionType *M, const vector<GenericValue> &args) {
514 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
517 for (unsigned i = 0; i < args.size(); ++i)
518 Args[i] = (char*)GVTOP(args[i]);
521 GV.IntVal = scanf(Args[0], Args[1], Args[2], Args[3], Args[4],
522 Args[5], Args[6], Args[7], Args[8], Args[9]);
523 ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4],
524 Args[5], Args[6], Args[7], Args[8], Args[9]);
529 // int clock(void) - Profiling implementation
530 GenericValue lle_i_clock(FunctionType *M, const vector<GenericValue> &Args) {
531 extern int clock(void);
532 GenericValue GV; GV.IntVal = clock();
537 //===----------------------------------------------------------------------===//
538 // String Functions...
539 //===----------------------------------------------------------------------===//
541 // int strcmp(const char *S1, const char *S2);
542 GenericValue lle_X_strcmp(FunctionType *M, const vector<GenericValue> &Args) {
543 assert(Args.size() == 2);
545 Ret.IntVal = strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]));
549 // char *strcat(char *Dest, const char *src);
550 GenericValue lle_X_strcat(FunctionType *M, const vector<GenericValue> &Args) {
551 assert(Args.size() == 2);
552 return PTOGV(strcat((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
555 // char *strcpy(char *Dest, const char *src);
556 GenericValue lle_X_strcpy(FunctionType *M, const vector<GenericValue> &Args) {
557 assert(Args.size() == 2);
558 return PTOGV(strcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
561 // long strlen(const char *src);
562 GenericValue lle_X_strlen(FunctionType *M, const vector<GenericValue> &Args) {
563 assert(Args.size() == 1);
565 Ret.LongVal = strlen((char*)GVTOP(Args[0]));
569 // char *__strdup(const char *src);
570 GenericValue lle_X___strdup(FunctionType *M, const vector<GenericValue> &Args) {
571 assert(Args.size() == 1);
572 return PTOGV(strdup((char*)GVTOP(Args[0])));
575 // void *memset(void *S, int C, size_t N)
576 GenericValue lle_X_memset(FunctionType *M, const vector<GenericValue> &Args) {
577 assert(Args.size() == 3);
578 return PTOGV(memset(GVTOP(Args[0]), Args[1].IntVal, Args[2].UIntVal));
581 // void *memcpy(void *Dest, void *src, size_t Size);
582 GenericValue lle_X_memcpy(FunctionType *M, const vector<GenericValue> &Args) {
583 assert(Args.size() == 3);
584 return PTOGV(memcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]),
588 //===----------------------------------------------------------------------===//
590 //===----------------------------------------------------------------------===//
592 // getFILE - Turn a pointer in the host address space into a legit pointer in
593 // the interpreter address space. For the most part, this is an identity
594 // transformation, but if the program refers to stdio, stderr, stdin then they
595 // have pointers that are relative to the __iob array. If this is the case,
596 // change the FILE into the REAL stdio stream.
598 static FILE *getFILE(void *Ptr) {
599 static Module *LastMod = 0;
600 static PointerTy IOBBase = 0;
601 static unsigned FILESize;
603 if (LastMod != &TheInterpreter->getModule()) { // Module change or initialize?
604 Module *M = LastMod = &TheInterpreter->getModule();
606 // Check to see if the currently loaded module contains an __iob symbol...
607 GlobalVariable *IOB = 0;
608 SymbolTable &ST = M->getSymbolTable();
609 for (SymbolTable::iterator I = ST.begin(), E = ST.end(); I != E; ++I) {
610 SymbolTable::VarMap &M = I->second;
611 for (SymbolTable::VarMap::iterator J = M.begin(), E = M.end();
613 if (J->first == "__iob")
614 if ((IOB = dyn_cast<GlobalVariable>(J->second)))
619 #if 0 /// FIXME! __iob support for LLI
620 // If we found an __iob symbol now, find out what the actual address it's
623 // Get the address the array lives in...
624 GlobalAddress *Address =
625 (GlobalAddress*)IOB->getOrCreateAnnotation(GlobalAddressAID);
626 IOBBase = (PointerTy)(GenericValue*)Address->Ptr;
628 // Figure out how big each element of the array is...
629 const ArrayType *AT =
630 dyn_cast<ArrayType>(IOB->getType()->getElementType());
632 FILESize = TD.getTypeSize(AT->getElementType());
634 FILESize = 16*8; // Default size
639 // Check to see if this is a reference to __iob...
641 unsigned FDNum = ((unsigned long)Ptr-IOBBase)/FILESize;
654 // FILE *fopen(const char *filename, const char *mode);
655 GenericValue lle_X_fopen(FunctionType *M, const vector<GenericValue> &Args) {
656 assert(Args.size() == 2);
657 return PTOGV(fopen((const char *)GVTOP(Args[0]),
658 (const char *)GVTOP(Args[1])));
661 // int fclose(FILE *F);
662 GenericValue lle_X_fclose(FunctionType *M, const vector<GenericValue> &Args) {
663 assert(Args.size() == 1);
665 GV.IntVal = fclose(getFILE(GVTOP(Args[0])));
669 // int feof(FILE *stream);
670 GenericValue lle_X_feof(FunctionType *M, const vector<GenericValue> &Args) {
671 assert(Args.size() == 1);
674 GV.IntVal = feof(getFILE(GVTOP(Args[0])));
678 // size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
679 GenericValue lle_X_fread(FunctionType *M, const vector<GenericValue> &Args) {
680 assert(Args.size() == 4);
683 GV.UIntVal = fread((void*)GVTOP(Args[0]), Args[1].UIntVal,
684 Args[2].UIntVal, getFILE(GVTOP(Args[3])));
688 // size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
689 GenericValue lle_X_fwrite(FunctionType *M, const vector<GenericValue> &Args) {
690 assert(Args.size() == 4);
693 GV.UIntVal = fwrite((void*)GVTOP(Args[0]), Args[1].UIntVal,
694 Args[2].UIntVal, getFILE(GVTOP(Args[3])));
698 // char *fgets(char *s, int n, FILE *stream);
699 GenericValue lle_X_fgets(FunctionType *M, const vector<GenericValue> &Args) {
700 assert(Args.size() == 3);
701 return GVTOP(fgets((char*)GVTOP(Args[0]), Args[1].IntVal,
702 getFILE(GVTOP(Args[2]))));
705 // FILE *freopen(const char *path, const char *mode, FILE *stream);
706 GenericValue lle_X_freopen(FunctionType *M, const vector<GenericValue> &Args) {
707 assert(Args.size() == 3);
708 return PTOGV(freopen((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]),
709 getFILE(GVTOP(Args[2]))));
712 // int fflush(FILE *stream);
713 GenericValue lle_X_fflush(FunctionType *M, const vector<GenericValue> &Args) {
714 assert(Args.size() == 1);
716 GV.IntVal = fflush(getFILE(GVTOP(Args[0])));
720 // int getc(FILE *stream);
721 GenericValue lle_X_getc(FunctionType *M, const vector<GenericValue> &Args) {
722 assert(Args.size() == 1);
724 GV.IntVal = getc(getFILE(GVTOP(Args[0])));
728 // int _IO_getc(FILE *stream);
729 GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) {
730 return lle_X_getc(F, Args);
733 // int fputc(int C, FILE *stream);
734 GenericValue lle_X_fputc(FunctionType *M, const vector<GenericValue> &Args) {
735 assert(Args.size() == 2);
737 GV.IntVal = fputc(Args[0].IntVal, getFILE(GVTOP(Args[1])));
741 // int ungetc(int C, FILE *stream);
742 GenericValue lle_X_ungetc(FunctionType *M, const vector<GenericValue> &Args) {
743 assert(Args.size() == 2);
745 GV.IntVal = ungetc(Args[0].IntVal, getFILE(GVTOP(Args[1])));
749 // int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
751 GenericValue lle_X_fprintf(FunctionType *M, const vector<GenericValue> &Args) {
752 assert(Args.size() >= 2);
754 vector<GenericValue> NewArgs;
755 NewArgs.push_back(PTOGV(Buffer));
756 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
757 GenericValue GV = lle_X_sprintf(M, NewArgs);
759 fputs(Buffer, getFILE(GVTOP(Args[0])));
766 void Interpreter::initializeExternalMethods() {
767 FuncNames["lle_VP_printstr"] = lle_VP_printstr;
768 FuncNames["lle_X_print"] = lle_X_print;
769 FuncNames["lle_X_printVal"] = lle_X_printVal;
770 FuncNames["lle_X_printString"] = lle_X_printString;
771 FuncNames["lle_X_printUByte"] = lle_X_printUByte;
772 FuncNames["lle_X_printSByte"] = lle_X_printSByte;
773 FuncNames["lle_X_printUShort"] = lle_X_printUShort;
774 FuncNames["lle_X_printShort"] = lle_X_printShort;
775 FuncNames["lle_X_printInt"] = lle_X_printInt;
776 FuncNames["lle_X_printUInt"] = lle_X_printUInt;
777 FuncNames["lle_X_printLong"] = lle_X_printLong;
778 FuncNames["lle_X_printULong"] = lle_X_printULong;
779 FuncNames["lle_X_printFloat"] = lle_X_printFloat;
780 FuncNames["lle_X_printDouble"] = lle_X_printDouble;
781 FuncNames["lle_X_printPointer"] = lle_X_printPointer;
782 FuncNames["lle_Vb_putchar"] = lle_Vb_putchar;
783 FuncNames["lle_ii_putchar"] = lle_ii_putchar;
784 FuncNames["lle_VB_putchar"] = lle_VB_putchar;
785 FuncNames["lle_V___main"] = lle_V___main;
786 FuncNames["lle_X_exit"] = lle_X_exit;
787 FuncNames["lle_X_abort"] = lle_X_abort;
788 FuncNames["lle_X_malloc"] = lle_X_malloc;
789 FuncNames["lle_X_calloc"] = lle_X_calloc;
790 FuncNames["lle_X_free"] = lle_X_free;
791 FuncNames["lle_X_atoi"] = lle_X_atoi;
792 FuncNames["lle_X_pow"] = lle_X_pow;
793 FuncNames["lle_X_exp"] = lle_X_exp;
794 FuncNames["lle_X_log"] = lle_X_log;
795 FuncNames["lle_X_isnan"] = lle_X_isnan;
796 FuncNames["lle_X_floor"] = lle_X_floor;
797 FuncNames["lle_X_srand"] = lle_X_srand;
798 FuncNames["lle_X_drand48"] = lle_X_drand48;
799 FuncNames["lle_X_srand48"] = lle_X_srand48;
800 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
801 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
802 FuncNames["lle_X_puts"] = lle_X_puts;
803 FuncNames["lle_X_printf"] = lle_X_printf;
804 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
805 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
806 FuncNames["lle_X_scanf"] = lle_X_scanf;
807 FuncNames["lle_i_clock"] = lle_i_clock;
809 FuncNames["lle_X_strcmp"] = lle_X_strcmp;
810 FuncNames["lle_X_strcat"] = lle_X_strcat;
811 FuncNames["lle_X_strcpy"] = lle_X_strcpy;
812 FuncNames["lle_X_strlen"] = lle_X_strlen;
813 FuncNames["lle_X___strdup"] = lle_X___strdup;
814 FuncNames["lle_X_memset"] = lle_X_memset;
815 FuncNames["lle_X_memcpy"] = lle_X_memcpy;
817 FuncNames["lle_X_fopen"] = lle_X_fopen;
818 FuncNames["lle_X_fclose"] = lle_X_fclose;
819 FuncNames["lle_X_feof"] = lle_X_feof;
820 FuncNames["lle_X_fread"] = lle_X_fread;
821 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
822 FuncNames["lle_X_fgets"] = lle_X_fgets;
823 FuncNames["lle_X_fflush"] = lle_X_fflush;
824 FuncNames["lle_X_fgetc"] = lle_X_getc;
825 FuncNames["lle_X_getc"] = lle_X_getc;
826 FuncNames["lle_X__IO_getc"] = lle_X__IO_getc;
827 FuncNames["lle_X_fputc"] = lle_X_fputc;
828 FuncNames["lle_X_ungetc"] = lle_X_ungetc;
829 FuncNames["lle_X_fprintf"] = lle_X_fprintf;
830 FuncNames["lle_X_freopen"] = lle_X_freopen;