For PR950:
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / ExternalFunctions.cpp
1 //===-- ExternalFunctions.cpp - Implement External Functions --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
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"
28 #include <csignal>
29 #include <map>
30 #include <cmath>
31 using std::vector;
32
33 using namespace llvm;
34
35 typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
36 static std::map<const Function *, ExFunc> Functions;
37 static std::map<std::string, ExFunc> FuncNames;
38
39 static Interpreter *TheInterpreter;
40
41 static char getTypeID(const Type *Ty) {
42   switch (Ty->getTypeID()) {
43   case Type::VoidTyID:    return 'V';
44   case Type::BoolTyID:    return 'o';
45   case Type::Int8TyID:    return 'B';
46   case Type::Int16TyID:   return 'S';
47   case Type::Int32TyID:   return 'I';
48   case Type::Int64TyID:   return 'L';
49   case Type::FloatTyID:   return 'F';
50   case Type::DoubleTyID:  return 'D';
51   case Type::PointerTyID: return 'P';
52   case Type::FunctionTyID:return 'M';
53   case Type::StructTyID:  return 'T';
54   case Type::ArrayTyID:   return 'A';
55   case Type::OpaqueTyID:  return 'O';
56   default: return 'U';
57   }
58 }
59
60 static ExFunc lookupFunction(const Function *F) {
61   // Function not found, look it up... start by figuring out what the
62   // composite function name should be.
63   std::string ExtName = "lle_";
64   const FunctionType *FT = F->getFunctionType();
65   for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
66     ExtName += getTypeID(FT->getContainedType(i));
67   ExtName += "_" + F->getName();
68
69   ExFunc FnPtr = FuncNames[ExtName];
70   if (FnPtr == 0)
71     FnPtr = 
72       (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(ExtName);
73   if (FnPtr == 0)
74     FnPtr = FuncNames["lle_X_"+F->getName()];
75   if (FnPtr == 0)  // Try calling a generic function... if it exists...
76     FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(
77             ("lle_X_"+F->getName()).c_str());
78   if (FnPtr != 0)
79     Functions.insert(std::make_pair(F, FnPtr));  // Cache for later
80   return FnPtr;
81 }
82
83 GenericValue Interpreter::callExternalFunction(Function *F,
84                                      const std::vector<GenericValue> &ArgVals) {
85   TheInterpreter = this;
86
87   // Do a lookup to see if the function is in our cache... this should just be a
88   // deferred annotation!
89   std::map<const Function *, ExFunc>::iterator FI = Functions.find(F);
90   ExFunc Fn = (FI == Functions.end()) ? lookupFunction(F) : FI->second;
91   if (Fn == 0) {
92     cerr << "Tried to execute an unknown external function: "
93          << F->getType()->getDescription() << " " << F->getName() << "\n";
94     if (F->getName() == "__main")
95       return GenericValue();
96     abort();
97   }
98
99   // TODO: FIXME when types are not const!
100   GenericValue Result = Fn(const_cast<FunctionType*>(F->getFunctionType()),
101                            ArgVals);
102   return Result;
103 }
104
105
106 //===----------------------------------------------------------------------===//
107 //  Functions "exported" to the running application...
108 //
109 extern "C" {  // Don't add C++ manglings to llvm mangling :)
110
111 // void putchar(sbyte)
112 GenericValue lle_VB_putchar(FunctionType *M, const vector<GenericValue> &Args) {
113   cout << Args[0].Int8Val;
114   return GenericValue();
115 }
116
117 // int putchar(int)
118 GenericValue lle_ii_putchar(FunctionType *M, const vector<GenericValue> &Args) {
119   cout << ((char)Args[0].Int32Val) << std::flush;
120   return Args[0];
121 }
122
123 // void putchar(ubyte)
124 GenericValue lle_Vb_putchar(FunctionType *M, const vector<GenericValue> &Args) {
125   cout << Args[0].Int8Val << std::flush;
126   return Args[0];
127 }
128
129 // void atexit(Function*)
130 GenericValue lle_X_atexit(FunctionType *M, const vector<GenericValue> &Args) {
131   assert(Args.size() == 1);
132   TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
133   GenericValue GV;
134   GV.Int32Val = 0;
135   return GV;
136 }
137
138 // void exit(int)
139 GenericValue lle_X_exit(FunctionType *M, const vector<GenericValue> &Args) {
140   TheInterpreter->exitCalled(Args[0]);
141   return GenericValue();
142 }
143
144 // void abort(void)
145 GenericValue lle_X_abort(FunctionType *M, const vector<GenericValue> &Args) {
146   raise (SIGABRT);
147   return GenericValue();
148 }
149
150 // void *malloc(uint)
151 GenericValue lle_X_malloc(FunctionType *M, const vector<GenericValue> &Args) {
152   assert(Args.size() == 1 && "Malloc expects one argument!");
153   return PTOGV(malloc(Args[0].Int32Val));
154 }
155
156 // void *calloc(uint, uint)
157 GenericValue lle_X_calloc(FunctionType *M, const vector<GenericValue> &Args) {
158   assert(Args.size() == 2 && "calloc expects two arguments!");
159   return PTOGV(calloc(Args[0].Int32Val, Args[1].Int32Val));
160 }
161
162 // void free(void *)
163 GenericValue lle_X_free(FunctionType *M, const vector<GenericValue> &Args) {
164   assert(Args.size() == 1);
165   free(GVTOP(Args[0]));
166   return GenericValue();
167 }
168
169 // int atoi(char *)
170 GenericValue lle_X_atoi(FunctionType *M, const vector<GenericValue> &Args) {
171   assert(Args.size() == 1);
172   GenericValue GV;
173   GV.Int32Val = atoi((char*)GVTOP(Args[0]));
174   return GV;
175 }
176
177 // double pow(double, double)
178 GenericValue lle_X_pow(FunctionType *M, const vector<GenericValue> &Args) {
179   assert(Args.size() == 2);
180   GenericValue GV;
181   GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
182   return GV;
183 }
184
185 // double exp(double)
186 GenericValue lle_X_exp(FunctionType *M, const vector<GenericValue> &Args) {
187   assert(Args.size() == 1);
188   GenericValue GV;
189   GV.DoubleVal = exp(Args[0].DoubleVal);
190   return GV;
191 }
192
193 // double sqrt(double)
194 GenericValue lle_X_sqrt(FunctionType *M, const vector<GenericValue> &Args) {
195   assert(Args.size() == 1);
196   GenericValue GV;
197   GV.DoubleVal = sqrt(Args[0].DoubleVal);
198   return GV;
199 }
200
201 // double log(double)
202 GenericValue lle_X_log(FunctionType *M, const vector<GenericValue> &Args) {
203   assert(Args.size() == 1);
204   GenericValue GV;
205   GV.DoubleVal = log(Args[0].DoubleVal);
206   return GV;
207 }
208
209 // double floor(double)
210 GenericValue lle_X_floor(FunctionType *M, const vector<GenericValue> &Args) {
211   assert(Args.size() == 1);
212   GenericValue GV;
213   GV.DoubleVal = floor(Args[0].DoubleVal);
214   return GV;
215 }
216
217 #ifdef HAVE_RAND48
218
219 // double drand48()
220 GenericValue lle_X_drand48(FunctionType *M, const vector<GenericValue> &Args) {
221   assert(Args.size() == 0);
222   GenericValue GV;
223   GV.DoubleVal = drand48();
224   return GV;
225 }
226
227 // long lrand48()
228 GenericValue lle_X_lrand48(FunctionType *M, const vector<GenericValue> &Args) {
229   assert(Args.size() == 0);
230   GenericValue GV;
231   GV.Int32Val = lrand48();
232   return GV;
233 }
234
235 // void srand48(long)
236 GenericValue lle_X_srand48(FunctionType *M, const vector<GenericValue> &Args) {
237   assert(Args.size() == 1);
238   srand48(Args[0].Int32Val);
239   return GenericValue();
240 }
241
242 #endif
243
244 // int rand()
245 GenericValue lle_X_rand(FunctionType *M, const vector<GenericValue> &Args) {
246   assert(Args.size() == 0);
247   GenericValue GV;
248   GV.Int32Val = rand();
249   return GV;
250 }
251
252 // void srand(uint)
253 GenericValue lle_X_srand(FunctionType *M, const vector<GenericValue> &Args) {
254   assert(Args.size() == 1);
255   srand(Args[0].Int32Val);
256   return GenericValue();
257 }
258
259 // int puts(const char*)
260 GenericValue lle_X_puts(FunctionType *M, const vector<GenericValue> &Args) {
261   assert(Args.size() == 1);
262   GenericValue GV;
263   GV.Int32Val = puts((char*)GVTOP(Args[0]));
264   return GV;
265 }
266
267 // int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
268 // output useful.
269 GenericValue lle_X_sprintf(FunctionType *M, const vector<GenericValue> &Args) {
270   char *OutputBuffer = (char *)GVTOP(Args[0]);
271   const char *FmtStr = (const char *)GVTOP(Args[1]);
272   unsigned ArgNo = 2;
273
274   // printf should return # chars printed.  This is completely incorrect, but
275   // close enough for now.
276   GenericValue GV; GV.Int32Val = strlen(FmtStr);
277   while (1) {
278     switch (*FmtStr) {
279     case 0: return GV;             // Null terminator...
280     default:                       // Normal nonspecial character
281       sprintf(OutputBuffer++, "%c", *FmtStr++);
282       break;
283     case '\\': {                   // Handle escape codes
284       sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
285       FmtStr += 2; OutputBuffer += 2;
286       break;
287     }
288     case '%': {                    // Handle format specifiers
289       char FmtBuf[100] = "", Buffer[1000] = "";
290       char *FB = FmtBuf;
291       *FB++ = *FmtStr++;
292       char Last = *FB++ = *FmtStr++;
293       unsigned HowLong = 0;
294       while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
295              Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
296              Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
297              Last != 'p' && Last != 's' && Last != '%') {
298         if (Last == 'l' || Last == 'L') HowLong++;  // Keep track of l's
299         Last = *FB++ = *FmtStr++;
300       }
301       *FB = 0;
302
303       switch (Last) {
304       case '%':
305         sprintf(Buffer, FmtBuf); break;
306       case 'c':
307         sprintf(Buffer, FmtBuf, Args[ArgNo++].Int32Val); break;
308       case 'd': case 'i':
309       case 'u': case 'o':
310       case 'x': case 'X':
311         if (HowLong >= 1) {
312           if (HowLong == 1 &&
313               TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 &&
314               sizeof(long) < sizeof(int64_t)) {
315             // Make sure we use %lld with a 64 bit argument because we might be
316             // compiling LLI on a 32 bit compiler.
317             unsigned Size = strlen(FmtBuf);
318             FmtBuf[Size] = FmtBuf[Size-1];
319             FmtBuf[Size+1] = 0;
320             FmtBuf[Size-1] = 'l';
321           }
322           sprintf(Buffer, FmtBuf, Args[ArgNo++].Int64Val);
323         } else
324           sprintf(Buffer, FmtBuf, Args[ArgNo++].Int32Val); break;
325       case 'e': case 'E': case 'g': case 'G': case 'f':
326         sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
327       case 'p':
328         sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
329       case 's':
330         sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
331       default:  cerr << "<unknown printf code '" << *FmtStr << "'!>";
332         ArgNo++; break;
333       }
334       strcpy(OutputBuffer, Buffer);
335       OutputBuffer += strlen(Buffer);
336       }
337       break;
338     }
339   }
340 }
341
342 // int printf(sbyte *, ...) - a very rough implementation to make output useful.
343 GenericValue lle_X_printf(FunctionType *M, const vector<GenericValue> &Args) {
344   char Buffer[10000];
345   vector<GenericValue> NewArgs;
346   NewArgs.push_back(PTOGV(Buffer));
347   NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
348   GenericValue GV = lle_X_sprintf(M, NewArgs);
349   cout << Buffer;
350   return GV;
351 }
352
353 static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1,
354                                  void *Arg2, void *Arg3, void *Arg4, void *Arg5,
355                                  void *Arg6, void *Arg7, void *Arg8) {
356   void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 };
357
358   // Loop over the format string, munging read values as appropriate (performs
359   // byteswaps as necessary).
360   unsigned ArgNo = 0;
361   while (*Fmt) {
362     if (*Fmt++ == '%') {
363       // Read any flag characters that may be present...
364       bool Suppress = false;
365       bool Half = false;
366       bool Long = false;
367       bool LongLong = false;  // long long or long double
368
369       while (1) {
370         switch (*Fmt++) {
371         case '*': Suppress = true; break;
372         case 'a': /*Allocate = true;*/ break;  // We don't need to track this
373         case 'h': Half = true; break;
374         case 'l': Long = true; break;
375         case 'q':
376         case 'L': LongLong = true; break;
377         default:
378           if (Fmt[-1] > '9' || Fmt[-1] < '0')   // Ignore field width specs
379             goto Out;
380         }
381       }
382     Out:
383
384       // Read the conversion character
385       if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
386         unsigned Size = 0;
387         const Type *Ty = 0;
388
389         switch (Fmt[-1]) {
390         case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
391         case 'd':
392           if (Long || LongLong) {
393             Size = 8; Ty = Type::Int64Ty;
394           } else if (Half) {
395             Size = 4; Ty = Type::Int16Ty;
396           } else {
397             Size = 4; Ty = Type::Int32Ty;
398           }
399           break;
400
401         case 'e': case 'g': case 'E':
402         case 'f':
403           if (Long || LongLong) {
404             Size = 8; Ty = Type::DoubleTy;
405           } else {
406             Size = 4; Ty = Type::FloatTy;
407           }
408           break;
409
410         case 's': case 'c': case '[':  // No byteswap needed
411           Size = 1;
412           Ty = Type::Int8Ty;
413           break;
414
415         default: break;
416         }
417
418         if (Size) {
419           GenericValue GV;
420           void *Arg = Args[ArgNo++];
421           memcpy(&GV, Arg, Size);
422           TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
423         }
424       }
425     }
426   }
427 }
428
429 // int sscanf(const char *format, ...);
430 GenericValue lle_X_sscanf(FunctionType *M, const vector<GenericValue> &args) {
431   assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
432
433   char *Args[10];
434   for (unsigned i = 0; i < args.size(); ++i)
435     Args[i] = (char*)GVTOP(args[i]);
436
437   GenericValue GV;
438   GV.Int32Val = sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
439                        Args[5], Args[6], Args[7], Args[8], Args[9]);
440   ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4],
441                        Args[5], Args[6], Args[7], Args[8], Args[9], 0);
442   return GV;
443 }
444
445 // int scanf(const char *format, ...);
446 GenericValue lle_X_scanf(FunctionType *M, const vector<GenericValue> &args) {
447   assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
448
449   char *Args[10];
450   for (unsigned i = 0; i < args.size(); ++i)
451     Args[i] = (char*)GVTOP(args[i]);
452
453   GenericValue GV;
454   GV.Int32Val = scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
455                        Args[5], Args[6], Args[7], Args[8], Args[9]);
456   ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4],
457                        Args[5], Args[6], Args[7], Args[8], Args[9]);
458   return GV;
459 }
460
461
462 // int clock(void) - Profiling implementation
463 GenericValue lle_i_clock(FunctionType *M, const vector<GenericValue> &Args) {
464   extern unsigned int clock(void);
465   GenericValue GV; GV.Int32Val = clock();
466   return GV;
467 }
468
469
470 //===----------------------------------------------------------------------===//
471 // String Functions...
472 //===----------------------------------------------------------------------===//
473
474 // int strcmp(const char *S1, const char *S2);
475 GenericValue lle_X_strcmp(FunctionType *M, const vector<GenericValue> &Args) {
476   assert(Args.size() == 2);
477   GenericValue Ret;
478   Ret.Int32Val = strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]));
479   return Ret;
480 }
481
482 // char *strcat(char *Dest, const char *src);
483 GenericValue lle_X_strcat(FunctionType *M, const vector<GenericValue> &Args) {
484   assert(Args.size() == 2);
485   return PTOGV(strcat((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
486 }
487
488 // char *strcpy(char *Dest, const char *src);
489 GenericValue lle_X_strcpy(FunctionType *M, const vector<GenericValue> &Args) {
490   assert(Args.size() == 2);
491   return PTOGV(strcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
492 }
493
494 static GenericValue size_t_to_GV (size_t n) {
495   GenericValue Ret;
496   if (sizeof (size_t) == sizeof (uint64_t)) {
497     Ret.Int64Val = n;
498   } else {
499     assert (sizeof (size_t) == sizeof (unsigned int));
500     Ret.Int32Val = n;
501   }
502   return Ret;
503 }
504
505 static size_t GV_to_size_t (GenericValue GV) {
506   size_t count;
507   if (sizeof (size_t) == sizeof (uint64_t)) {
508     count = (size_t)GV.Int64Val;
509   } else {
510     assert (sizeof (size_t) == sizeof (unsigned int));
511     count = (size_t)GV.Int32Val;
512   }
513   return count;
514 }
515
516 // size_t strlen(const char *src);
517 GenericValue lle_X_strlen(FunctionType *M, const vector<GenericValue> &Args) {
518   assert(Args.size() == 1);
519   size_t strlenResult = strlen ((char *) GVTOP (Args[0]));
520   return size_t_to_GV (strlenResult);
521 }
522
523 // char *strdup(const char *src);
524 GenericValue lle_X_strdup(FunctionType *M, const vector<GenericValue> &Args) {
525   assert(Args.size() == 1);
526   return PTOGV(strdup((char*)GVTOP(Args[0])));
527 }
528
529 // char *__strdup(const char *src);
530 GenericValue lle_X___strdup(FunctionType *M, const vector<GenericValue> &Args) {
531   assert(Args.size() == 1);
532   return PTOGV(strdup((char*)GVTOP(Args[0])));
533 }
534
535 // void *memset(void *S, int C, size_t N)
536 GenericValue lle_X_memset(FunctionType *M, const vector<GenericValue> &Args) {
537   assert(Args.size() == 3);
538   size_t count = GV_to_size_t (Args[2]);
539   return PTOGV(memset(GVTOP(Args[0]), Args[1].Int32Val, count));
540 }
541
542 // void *memcpy(void *Dest, void *src, size_t Size);
543 GenericValue lle_X_memcpy(FunctionType *M, const vector<GenericValue> &Args) {
544   assert(Args.size() == 3);
545   size_t count = GV_to_size_t (Args[2]);
546   return PTOGV(memcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));
547 }
548
549 //===----------------------------------------------------------------------===//
550 // IO Functions...
551 //===----------------------------------------------------------------------===//
552
553 // getFILE - Turn a pointer in the host address space into a legit pointer in
554 // the interpreter address space.  This is an identity transformation.
555 #define getFILE(ptr) ((FILE*)ptr)
556
557 // FILE *fopen(const char *filename, const char *mode);
558 GenericValue lle_X_fopen(FunctionType *M, const vector<GenericValue> &Args) {
559   assert(Args.size() == 2);
560   return PTOGV(fopen((const char *)GVTOP(Args[0]),
561                      (const char *)GVTOP(Args[1])));
562 }
563
564 // int fclose(FILE *F);
565 GenericValue lle_X_fclose(FunctionType *M, const vector<GenericValue> &Args) {
566   assert(Args.size() == 1);
567   GenericValue GV;
568   GV.Int32Val = fclose(getFILE(GVTOP(Args[0])));
569   return GV;
570 }
571
572 // int feof(FILE *stream);
573 GenericValue lle_X_feof(FunctionType *M, const vector<GenericValue> &Args) {
574   assert(Args.size() == 1);
575   GenericValue GV;
576
577   GV.Int32Val = feof(getFILE(GVTOP(Args[0])));
578   return GV;
579 }
580
581 // size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
582 GenericValue lle_X_fread(FunctionType *M, const vector<GenericValue> &Args) {
583   assert(Args.size() == 4);
584   size_t result;
585
586   result = fread((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
587                  GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
588   return size_t_to_GV (result);
589 }
590
591 // size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
592 GenericValue lle_X_fwrite(FunctionType *M, const vector<GenericValue> &Args) {
593   assert(Args.size() == 4);
594   size_t result;
595
596   result = fwrite((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
597                   GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
598   return size_t_to_GV (result);
599 }
600
601 // char *fgets(char *s, int n, FILE *stream);
602 GenericValue lle_X_fgets(FunctionType *M, const vector<GenericValue> &Args) {
603   assert(Args.size() == 3);
604   return GVTOP(fgets((char*)GVTOP(Args[0]), Args[1].Int32Val,
605                      getFILE(GVTOP(Args[2]))));
606 }
607
608 // FILE *freopen(const char *path, const char *mode, FILE *stream);
609 GenericValue lle_X_freopen(FunctionType *M, const vector<GenericValue> &Args) {
610   assert(Args.size() == 3);
611   return PTOGV(freopen((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]),
612                        getFILE(GVTOP(Args[2]))));
613 }
614
615 // int fflush(FILE *stream);
616 GenericValue lle_X_fflush(FunctionType *M, const vector<GenericValue> &Args) {
617   assert(Args.size() == 1);
618   GenericValue GV;
619   GV.Int32Val = fflush(getFILE(GVTOP(Args[0])));
620   return GV;
621 }
622
623 // int getc(FILE *stream);
624 GenericValue lle_X_getc(FunctionType *M, const vector<GenericValue> &Args) {
625   assert(Args.size() == 1);
626   GenericValue GV;
627   GV.Int32Val = getc(getFILE(GVTOP(Args[0])));
628   return GV;
629 }
630
631 // int _IO_getc(FILE *stream);
632 GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) {
633   return lle_X_getc(F, Args);
634 }
635
636 // int fputc(int C, FILE *stream);
637 GenericValue lle_X_fputc(FunctionType *M, const vector<GenericValue> &Args) {
638   assert(Args.size() == 2);
639   GenericValue GV;
640   GV.Int32Val = fputc(Args[0].Int32Val, getFILE(GVTOP(Args[1])));
641   return GV;
642 }
643
644 // int ungetc(int C, FILE *stream);
645 GenericValue lle_X_ungetc(FunctionType *M, const vector<GenericValue> &Args) {
646   assert(Args.size() == 2);
647   GenericValue GV;
648   GV.Int32Val = ungetc(Args[0].Int32Val, getFILE(GVTOP(Args[1])));
649   return GV;
650 }
651
652 // int ferror (FILE *stream);
653 GenericValue lle_X_ferror(FunctionType *M, const vector<GenericValue> &Args) {
654   assert(Args.size() == 1);
655   GenericValue GV;
656   GV.Int32Val = ferror (getFILE(GVTOP(Args[0])));
657   return GV;
658 }
659
660 // int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
661 // useful.
662 GenericValue lle_X_fprintf(FunctionType *M, const vector<GenericValue> &Args) {
663   assert(Args.size() >= 2);
664   char Buffer[10000];
665   vector<GenericValue> NewArgs;
666   NewArgs.push_back(PTOGV(Buffer));
667   NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
668   GenericValue GV = lle_X_sprintf(M, NewArgs);
669
670   fputs(Buffer, getFILE(GVTOP(Args[0])));
671   return GV;
672 }
673
674 } // End extern "C"
675
676
677 void Interpreter::initializeExternalFunctions() {
678   FuncNames["lle_Vb_putchar"]     = lle_Vb_putchar;
679   FuncNames["lle_ii_putchar"]     = lle_ii_putchar;
680   FuncNames["lle_VB_putchar"]     = lle_VB_putchar;
681   FuncNames["lle_X_exit"]         = lle_X_exit;
682   FuncNames["lle_X_abort"]        = lle_X_abort;
683   FuncNames["lle_X_malloc"]       = lle_X_malloc;
684   FuncNames["lle_X_calloc"]       = lle_X_calloc;
685   FuncNames["lle_X_free"]         = lle_X_free;
686   FuncNames["lle_X_atoi"]         = lle_X_atoi;
687   FuncNames["lle_X_pow"]          = lle_X_pow;
688   FuncNames["lle_X_exp"]          = lle_X_exp;
689   FuncNames["lle_X_log"]          = lle_X_log;
690   FuncNames["lle_X_floor"]        = lle_X_floor;
691   FuncNames["lle_X_srand"]        = lle_X_srand;
692   FuncNames["lle_X_rand"]         = lle_X_rand;
693 #ifdef HAVE_RAND48
694   FuncNames["lle_X_drand48"]      = lle_X_drand48;
695   FuncNames["lle_X_srand48"]      = lle_X_srand48;
696   FuncNames["lle_X_lrand48"]      = lle_X_lrand48;
697 #endif
698   FuncNames["lle_X_sqrt"]         = lle_X_sqrt;
699   FuncNames["lle_X_puts"]         = lle_X_puts;
700   FuncNames["lle_X_printf"]       = lle_X_printf;
701   FuncNames["lle_X_sprintf"]      = lle_X_sprintf;
702   FuncNames["lle_X_sscanf"]       = lle_X_sscanf;
703   FuncNames["lle_X_scanf"]        = lle_X_scanf;
704   FuncNames["lle_i_clock"]        = lle_i_clock;
705
706   FuncNames["lle_X_strcmp"]       = lle_X_strcmp;
707   FuncNames["lle_X_strcat"]       = lle_X_strcat;
708   FuncNames["lle_X_strcpy"]       = lle_X_strcpy;
709   FuncNames["lle_X_strlen"]       = lle_X_strlen;
710   FuncNames["lle_X___strdup"]     = lle_X___strdup;
711   FuncNames["lle_X_memset"]       = lle_X_memset;
712   FuncNames["lle_X_memcpy"]       = lle_X_memcpy;
713
714   FuncNames["lle_X_fopen"]        = lle_X_fopen;
715   FuncNames["lle_X_fclose"]       = lle_X_fclose;
716   FuncNames["lle_X_feof"]         = lle_X_feof;
717   FuncNames["lle_X_fread"]        = lle_X_fread;
718   FuncNames["lle_X_fwrite"]       = lle_X_fwrite;
719   FuncNames["lle_X_fgets"]        = lle_X_fgets;
720   FuncNames["lle_X_fflush"]       = lle_X_fflush;
721   FuncNames["lle_X_fgetc"]        = lle_X_getc;
722   FuncNames["lle_X_getc"]         = lle_X_getc;
723   FuncNames["lle_X__IO_getc"]     = lle_X__IO_getc;
724   FuncNames["lle_X_fputc"]        = lle_X_fputc;
725   FuncNames["lle_X_ungetc"]       = lle_X_ungetc;
726   FuncNames["lle_X_fprintf"]      = lle_X_fprintf;
727   FuncNames["lle_X_freopen"]      = lle_X_freopen;
728 }
729