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