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