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