LLVM API Documentation
00001 //===-- ExternalFunctions.cpp - Implement External Functions --------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains both code to deal with invoking "external" functions, but 00011 // also contains code that implements "exported" external functions. 00012 // 00013 // External functions in the interpreter are implemented by 00014 // using the system's dynamic loader to look up the address of the function 00015 // we want to invoke. If a function is found, then one of the 00016 // many lle_* wrapper functions in this file will translate its arguments from 00017 // GenericValues to the types the function is actually expecting, before the 00018 // function is called. 00019 // 00020 //===----------------------------------------------------------------------===// 00021 00022 #include "Interpreter.h" 00023 #include "llvm/DerivedTypes.h" 00024 #include "llvm/Module.h" 00025 #include "llvm/System/DynamicLibrary.h" 00026 #include "llvm/Target/TargetData.h" 00027 #include <cmath> 00028 #include <csignal> 00029 #include <map> 00030 using std::vector; 00031 00032 using namespace llvm; 00033 00034 typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &); 00035 static std::map<const Function *, ExFunc> Functions; 00036 static std::map<std::string, ExFunc> FuncNames; 00037 00038 static Interpreter *TheInterpreter; 00039 00040 static char getTypeID(const Type *Ty) { 00041 switch (Ty->getTypeID()) { 00042 case Type::VoidTyID: return 'V'; 00043 case Type::BoolTyID: return 'o'; 00044 case Type::UByteTyID: return 'B'; 00045 case Type::SByteTyID: return 'b'; 00046 case Type::UShortTyID: return 'S'; 00047 case Type::ShortTyID: return 's'; 00048 case Type::UIntTyID: return 'I'; 00049 case Type::IntTyID: return 'i'; 00050 case Type::ULongTyID: return 'L'; 00051 case Type::LongTyID: return 'l'; 00052 case Type::FloatTyID: return 'F'; 00053 case Type::DoubleTyID: return 'D'; 00054 case Type::PointerTyID: return 'P'; 00055 case Type::FunctionTyID: return 'M'; 00056 case Type::StructTyID: return 'T'; 00057 case Type::ArrayTyID: return 'A'; 00058 case Type::OpaqueTyID: return 'O'; 00059 default: return 'U'; 00060 } 00061 } 00062 00063 static ExFunc lookupFunction(const Function *F) { 00064 // Function not found, look it up... start by figuring out what the 00065 // composite function name should be. 00066 std::string ExtName = "lle_"; 00067 const FunctionType *FT = F->getFunctionType(); 00068 for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i) 00069 ExtName += getTypeID(FT->getContainedType(i)); 00070 ExtName += "_" + F->getName(); 00071 00072 ExFunc FnPtr = FuncNames[ExtName]; 00073 if (FnPtr == 0) 00074 FnPtr = 00075 (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(ExtName); 00076 if (FnPtr == 0) 00077 FnPtr = FuncNames["lle_X_"+F->getName()]; 00078 if (FnPtr == 0) // Try calling a generic function... if it exists... 00079 FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol( 00080 ("lle_X_"+F->getName()).c_str()); 00081 if (FnPtr != 0) 00082 Functions.insert(std::make_pair(F, FnPtr)); // Cache for later 00083 return FnPtr; 00084 } 00085 00086 GenericValue Interpreter::callExternalFunction(Function *F, 00087 const std::vector<GenericValue> &ArgVals) { 00088 TheInterpreter = this; 00089 00090 // Do a lookup to see if the function is in our cache... this should just be a 00091 // deferred annotation! 00092 std::map<const Function *, ExFunc>::iterator FI = Functions.find(F); 00093 ExFunc Fn = (FI == Functions.end()) ? lookupFunction(F) : FI->second; 00094 if (Fn == 0) { 00095 std::cout << "Tried to execute an unknown external function: " 00096 << F->getType()->getDescription() << " " << F->getName() << "\n"; 00097 if (F->getName() == "__main") 00098 return GenericValue(); 00099 abort(); 00100 } 00101 00102 // TODO: FIXME when types are not const! 00103 GenericValue Result = Fn(const_cast<FunctionType*>(F->getFunctionType()), 00104 ArgVals); 00105 return Result; 00106 } 00107 00108 00109 //===----------------------------------------------------------------------===// 00110 // Functions "exported" to the running application... 00111 // 00112 extern "C" { // Don't add C++ manglings to llvm mangling :) 00113 00114 // void putchar(sbyte) 00115 GenericValue lle_Vb_putchar(FunctionType *M, const vector<GenericValue> &Args) { 00116 std::cout << Args[0].SByteVal; 00117 return GenericValue(); 00118 } 00119 00120 // int putchar(int) 00121 GenericValue lle_ii_putchar(FunctionType *M, const vector<GenericValue> &Args) { 00122 std::cout << ((char)Args[0].IntVal) << std::flush; 00123 return Args[0]; 00124 } 00125 00126 // void putchar(ubyte) 00127 GenericValue lle_VB_putchar(FunctionType *M, const vector<GenericValue> &Args) { 00128 std::cout << Args[0].SByteVal << std::flush; 00129 return Args[0]; 00130 } 00131 00132 // void atexit(Function*) 00133 GenericValue lle_X_atexit(FunctionType *M, const vector<GenericValue> &Args) { 00134 assert(Args.size() == 1); 00135 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0])); 00136 GenericValue GV; 00137 GV.IntVal = 0; 00138 return GV; 00139 } 00140 00141 // void exit(int) 00142 GenericValue lle_X_exit(FunctionType *M, const vector<GenericValue> &Args) { 00143 TheInterpreter->exitCalled(Args[0]); 00144 return GenericValue(); 00145 } 00146 00147 // void abort(void) 00148 GenericValue lle_X_abort(FunctionType *M, const vector<GenericValue> &Args) { 00149 raise (SIGABRT); 00150 return GenericValue(); 00151 } 00152 00153 // void *malloc(uint) 00154 GenericValue lle_X_malloc(FunctionType *M, const vector<GenericValue> &Args) { 00155 assert(Args.size() == 1 && "Malloc expects one argument!"); 00156 return PTOGV(malloc(Args[0].UIntVal)); 00157 } 00158 00159 // void *calloc(uint, uint) 00160 GenericValue lle_X_calloc(FunctionType *M, const vector<GenericValue> &Args) { 00161 assert(Args.size() == 2 && "calloc expects two arguments!"); 00162 return PTOGV(calloc(Args[0].UIntVal, Args[1].UIntVal)); 00163 } 00164 00165 // void free(void *) 00166 GenericValue lle_X_free(FunctionType *M, const vector<GenericValue> &Args) { 00167 assert(Args.size() == 1); 00168 free(GVTOP(Args[0])); 00169 return GenericValue(); 00170 } 00171 00172 // int atoi(char *) 00173 GenericValue lle_X_atoi(FunctionType *M, const vector<GenericValue> &Args) { 00174 assert(Args.size() == 1); 00175 GenericValue GV; 00176 GV.IntVal = atoi((char*)GVTOP(Args[0])); 00177 return GV; 00178 } 00179 00180 // double pow(double, double) 00181 GenericValue lle_X_pow(FunctionType *M, const vector<GenericValue> &Args) { 00182 assert(Args.size() == 2); 00183 GenericValue GV; 00184 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal); 00185 return GV; 00186 } 00187 00188 // double exp(double) 00189 GenericValue lle_X_exp(FunctionType *M, const vector<GenericValue> &Args) { 00190 assert(Args.size() == 1); 00191 GenericValue GV; 00192 GV.DoubleVal = exp(Args[0].DoubleVal); 00193 return GV; 00194 } 00195 00196 // double sqrt(double) 00197 GenericValue lle_X_sqrt(FunctionType *M, const vector<GenericValue> &Args) { 00198 assert(Args.size() == 1); 00199 GenericValue GV; 00200 GV.DoubleVal = sqrt(Args[0].DoubleVal); 00201 return GV; 00202 } 00203 00204 // double log(double) 00205 GenericValue lle_X_log(FunctionType *M, const vector<GenericValue> &Args) { 00206 assert(Args.size() == 1); 00207 GenericValue GV; 00208 GV.DoubleVal = log(Args[0].DoubleVal); 00209 return GV; 00210 } 00211 00212 // double floor(double) 00213 GenericValue lle_X_floor(FunctionType *M, const vector<GenericValue> &Args) { 00214 assert(Args.size() == 1); 00215 GenericValue GV; 00216 GV.DoubleVal = floor(Args[0].DoubleVal); 00217 return GV; 00218 } 00219 00220 #ifdef HAVE_RAND48 00221 00222 // double drand48() 00223 GenericValue lle_X_drand48(FunctionType *M, const vector<GenericValue> &Args) { 00224 assert(Args.size() == 0); 00225 GenericValue GV; 00226 GV.DoubleVal = drand48(); 00227 return GV; 00228 } 00229 00230 // long lrand48() 00231 GenericValue lle_X_lrand48(FunctionType *M, const vector<GenericValue> &Args) { 00232 assert(Args.size() == 0); 00233 GenericValue GV; 00234 GV.IntVal = lrand48(); 00235 return GV; 00236 } 00237 00238 // void srand48(long) 00239 GenericValue lle_X_srand48(FunctionType *M, const vector<GenericValue> &Args) { 00240 assert(Args.size() == 1); 00241 srand48(Args[0].IntVal); 00242 return GenericValue(); 00243 } 00244 00245 #endif 00246 00247 // int rand() 00248 GenericValue lle_X_rand(FunctionType *M, const vector<GenericValue> &Args) { 00249 assert(Args.size() == 0); 00250 GenericValue GV; 00251 GV.IntVal = rand(); 00252 return GV; 00253 } 00254 00255 // void srand(uint) 00256 GenericValue lle_X_srand(FunctionType *M, const vector<GenericValue> &Args) { 00257 assert(Args.size() == 1); 00258 srand(Args[0].UIntVal); 00259 return GenericValue(); 00260 } 00261 00262 // int puts(const char*) 00263 GenericValue lle_X_puts(FunctionType *M, const vector<GenericValue> &Args) { 00264 assert(Args.size() == 1); 00265 GenericValue GV; 00266 GV.IntVal = puts((char*)GVTOP(Args[0])); 00267 return GV; 00268 } 00269 00270 // int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make 00271 // output useful. 00272 GenericValue lle_X_sprintf(FunctionType *M, const vector<GenericValue> &Args) { 00273 char *OutputBuffer = (char *)GVTOP(Args[0]); 00274 const char *FmtStr = (const char *)GVTOP(Args[1]); 00275 unsigned ArgNo = 2; 00276 00277 // printf should return # chars printed. This is completely incorrect, but 00278 // close enough for now. 00279 GenericValue GV; GV.IntVal = strlen(FmtStr); 00280 while (1) { 00281 switch (*FmtStr) { 00282 case 0: return GV; // Null terminator... 00283 default: // Normal nonspecial character 00284 sprintf(OutputBuffer++, "%c", *FmtStr++); 00285 break; 00286 case '\\': { // Handle escape codes 00287 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1)); 00288 FmtStr += 2; OutputBuffer += 2; 00289 break; 00290 } 00291 case '%': { // Handle format specifiers 00292 char FmtBuf[100] = "", Buffer[1000] = ""; 00293 char *FB = FmtBuf; 00294 *FB++ = *FmtStr++; 00295 char Last = *FB++ = *FmtStr++; 00296 unsigned HowLong = 0; 00297 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' && 00298 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' && 00299 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' && 00300 Last != 'p' && Last != 's' && Last != '%') { 00301 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's 00302 Last = *FB++ = *FmtStr++; 00303 } 00304 *FB = 0; 00305 00306 switch (Last) { 00307 case '%': 00308 sprintf(Buffer, FmtBuf); break; 00309 case 'c': 00310 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break; 00311 case 'd': case 'i': 00312 case 'u': case 'o': 00313 case 'x': case 'X': 00314 if (HowLong >= 1) { 00315 if (HowLong == 1 && 00316 TheInterpreter->getModule().getPointerSize()==Module::Pointer64 && 00317 sizeof(long) < sizeof(int64_t)) { 00318 // Make sure we use %lld with a 64 bit argument because we might be 00319 // compiling LLI on a 32 bit compiler. 00320 unsigned Size = strlen(FmtBuf); 00321 FmtBuf[Size] = FmtBuf[Size-1]; 00322 FmtBuf[Size+1] = 0; 00323 FmtBuf[Size-1] = 'l'; 00324 } 00325 sprintf(Buffer, FmtBuf, Args[ArgNo++].ULongVal); 00326 } else 00327 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break; 00328 case 'e': case 'E': case 'g': case 'G': case 'f': 00329 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break; 00330 case 'p': 00331 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break; 00332 case 's': 00333 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break; 00334 default: std::cout << "<unknown printf code '" << *FmtStr << "'!>"; 00335 ArgNo++; break; 00336 } 00337 strcpy(OutputBuffer, Buffer); 00338 OutputBuffer += strlen(Buffer); 00339 } 00340 break; 00341 } 00342 } 00343 } 00344 00345 // int printf(sbyte *, ...) - a very rough implementation to make output useful. 00346 GenericValue lle_X_printf(FunctionType *M, const vector<GenericValue> &Args) { 00347 char Buffer[10000]; 00348 vector<GenericValue> NewArgs; 00349 NewArgs.push_back(PTOGV(Buffer)); 00350 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end()); 00351 GenericValue GV = lle_X_sprintf(M, NewArgs); 00352 std::cout << Buffer; 00353 return GV; 00354 } 00355 00356 static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1, 00357 void *Arg2, void *Arg3, void *Arg4, void *Arg5, 00358 void *Arg6, void *Arg7, void *Arg8) { 00359 void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 }; 00360 00361 // Loop over the format string, munging read values as appropriate (performs 00362 // byteswaps as necessary). 00363 unsigned ArgNo = 0; 00364 while (*Fmt) { 00365 if (*Fmt++ == '%') { 00366 // Read any flag characters that may be present... 00367 bool Suppress = false; 00368 bool Half = false; 00369 bool Long = false; 00370 bool LongLong = false; // long long or long double 00371 00372 while (1) { 00373 switch (*Fmt++) { 00374 case '*': Suppress = true; break; 00375 case 'a': /*Allocate = true;*/ break; // We don't need to track this 00376 case 'h': Half = true; break; 00377 case 'l': Long = true; break; 00378 case 'q': 00379 case 'L': LongLong = true; break; 00380 default: 00381 if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs 00382 goto Out; 00383 } 00384 } 00385 Out: 00386 00387 // Read the conversion character 00388 if (!Suppress && Fmt[-1] != '%') { // Nothing to do? 00389 unsigned Size = 0; 00390 const Type *Ty = 0; 00391 00392 switch (Fmt[-1]) { 00393 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p': 00394 case 'd': 00395 if (Long || LongLong) { 00396 Size = 8; Ty = Type::ULongTy; 00397 } else if (Half) { 00398 Size = 4; Ty = Type::UShortTy; 00399 } else { 00400 Size = 4; Ty = Type::UIntTy; 00401 } 00402 break; 00403 00404 case 'e': case 'g': case 'E': 00405 case 'f': 00406 if (Long || LongLong) { 00407 Size = 8; Ty = Type::DoubleTy; 00408 } else { 00409 Size = 4; Ty = Type::FloatTy; 00410 } 00411 break; 00412 00413 case 's': case 'c': case '[': // No byteswap needed 00414 Size = 1; 00415 Ty = Type::SByteTy; 00416 break; 00417 00418 default: break; 00419 } 00420 00421 if (Size) { 00422 GenericValue GV; 00423 void *Arg = Args[ArgNo++]; 00424 memcpy(&GV, Arg, Size); 00425 TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty); 00426 } 00427 } 00428 } 00429 } 00430 } 00431 00432 // int sscanf(const char *format, ...); 00433 GenericValue lle_X_sscanf(FunctionType *M, const vector<GenericValue> &args) { 00434 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!"); 00435 00436 char *Args[10]; 00437 for (unsigned i = 0; i < args.size(); ++i) 00438 Args[i] = (char*)GVTOP(args[i]); 00439 00440 GenericValue GV; 00441 GV.IntVal = sscanf(Args[0], Args[1], Args[2], Args[3], Args[4], 00442 Args[5], Args[6], Args[7], Args[8], Args[9]); 00443 ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4], 00444 Args[5], Args[6], Args[7], Args[8], Args[9], 0); 00445 return GV; 00446 } 00447 00448 // int scanf(const char *format, ...); 00449 GenericValue lle_X_scanf(FunctionType *M, const vector<GenericValue> &args) { 00450 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!"); 00451 00452 char *Args[10]; 00453 for (unsigned i = 0; i < args.size(); ++i) 00454 Args[i] = (char*)GVTOP(args[i]); 00455 00456 GenericValue GV; 00457 GV.IntVal = scanf(Args[0], Args[1], Args[2], Args[3], Args[4], 00458 Args[5], Args[6], Args[7], Args[8], Args[9]); 00459 ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4], 00460 Args[5], Args[6], Args[7], Args[8], Args[9]); 00461 return GV; 00462 } 00463 00464 00465 // int clock(void) - Profiling implementation 00466 GenericValue lle_i_clock(FunctionType *M, const vector<GenericValue> &Args) { 00467 extern unsigned int clock(void); 00468 GenericValue GV; GV.IntVal = clock(); 00469 return GV; 00470 } 00471 00472 00473 //===----------------------------------------------------------------------===// 00474 // String Functions... 00475 //===----------------------------------------------------------------------===// 00476 00477 // int strcmp(const char *S1, const char *S2); 00478 GenericValue lle_X_strcmp(FunctionType *M, const vector<GenericValue> &Args) { 00479 assert(Args.size() == 2); 00480 GenericValue Ret; 00481 Ret.IntVal = strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])); 00482 return Ret; 00483 } 00484 00485 // char *strcat(char *Dest, const char *src); 00486 GenericValue lle_X_strcat(FunctionType *M, const vector<GenericValue> &Args) { 00487 assert(Args.size() == 2); 00488 return PTOGV(strcat((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]))); 00489 } 00490 00491 // char *strcpy(char *Dest, const char *src); 00492 GenericValue lle_X_strcpy(FunctionType *M, const vector<GenericValue> &Args) { 00493 assert(Args.size() == 2); 00494 return PTOGV(strcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]))); 00495 } 00496 00497 static GenericValue size_t_to_GV (size_t n) { 00498 GenericValue Ret; 00499 if (sizeof (size_t) == sizeof (uint64_t)) { 00500 Ret.ULongVal = n; 00501 } else { 00502 assert (sizeof (size_t) == sizeof (unsigned int)); 00503 Ret.UIntVal = n; 00504 } 00505 return Ret; 00506 } 00507 00508 static size_t GV_to_size_t (GenericValue GV) { 00509 size_t count; 00510 if (sizeof (size_t) == sizeof (uint64_t)) { 00511 count = (size_t)GV.ULongVal; 00512 } else { 00513 assert (sizeof (size_t) == sizeof (unsigned int)); 00514 count = (size_t)GV.UIntVal; 00515 } 00516 return count; 00517 } 00518 00519 // size_t strlen(const char *src); 00520 GenericValue lle_X_strlen(FunctionType *M, const vector<GenericValue> &Args) { 00521 assert(Args.size() == 1); 00522 size_t strlenResult = strlen ((char *) GVTOP (Args[0])); 00523 return size_t_to_GV (strlenResult); 00524 } 00525 00526 // char *strdup(const char *src); 00527 GenericValue lle_X_strdup(FunctionType *M, const vector<GenericValue> &Args) { 00528 assert(Args.size() == 1); 00529 return PTOGV(strdup((char*)GVTOP(Args[0]))); 00530 } 00531 00532 // char *__strdup(const char *src); 00533 GenericValue lle_X___strdup(FunctionType *M, const vector<GenericValue> &Args) { 00534 assert(Args.size() == 1); 00535 return PTOGV(strdup((char*)GVTOP(Args[0]))); 00536 } 00537 00538 // void *memset(void *S, int C, size_t N) 00539 GenericValue lle_X_memset(FunctionType *M, const vector<GenericValue> &Args) { 00540 assert(Args.size() == 3); 00541 size_t count = GV_to_size_t (Args[2]); 00542 return PTOGV(memset(GVTOP(Args[0]), Args[1].IntVal, count)); 00543 } 00544 00545 // void *memcpy(void *Dest, void *src, size_t Size); 00546 GenericValue lle_X_memcpy(FunctionType *M, const vector<GenericValue> &Args) { 00547 assert(Args.size() == 3); 00548 size_t count = GV_to_size_t (Args[2]); 00549 return PTOGV(memcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count)); 00550 } 00551 00552 //===----------------------------------------------------------------------===// 00553 // IO Functions... 00554 //===----------------------------------------------------------------------===// 00555 00556 // getFILE - Turn a pointer in the host address space into a legit pointer in 00557 // the interpreter address space. This is an identity transformation. 00558 #define getFILE(ptr) ((FILE*)ptr) 00559 00560 // FILE *fopen(const char *filename, const char *mode); 00561 GenericValue lle_X_fopen(FunctionType *M, const vector<GenericValue> &Args) { 00562 assert(Args.size() == 2); 00563 return PTOGV(fopen((const char *)GVTOP(Args[0]), 00564 (const char *)GVTOP(Args[1]))); 00565 } 00566 00567 // int fclose(FILE *F); 00568 GenericValue lle_X_fclose(FunctionType *M, const vector<GenericValue> &Args) { 00569 assert(Args.size() == 1); 00570 GenericValue GV; 00571 GV.IntVal = fclose(getFILE(GVTOP(Args[0]))); 00572 return GV; 00573 } 00574 00575 // int feof(FILE *stream); 00576 GenericValue lle_X_feof(FunctionType *M, const vector<GenericValue> &Args) { 00577 assert(Args.size() == 1); 00578 GenericValue GV; 00579 00580 GV.IntVal = feof(getFILE(GVTOP(Args[0]))); 00581 return GV; 00582 } 00583 00584 // size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream); 00585 GenericValue lle_X_fread(FunctionType *M, const vector<GenericValue> &Args) { 00586 assert(Args.size() == 4); 00587 size_t result; 00588 00589 result = fread((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]), 00590 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3]))); 00591 return size_t_to_GV (result); 00592 } 00593 00594 // size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream); 00595 GenericValue lle_X_fwrite(FunctionType *M, const vector<GenericValue> &Args) { 00596 assert(Args.size() == 4); 00597 size_t result; 00598 00599 result = fwrite((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]), 00600 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3]))); 00601 return size_t_to_GV (result); 00602 } 00603 00604 // char *fgets(char *s, int n, FILE *stream); 00605 GenericValue lle_X_fgets(FunctionType *M, const vector<GenericValue> &Args) { 00606 assert(Args.size() == 3); 00607 return GVTOP(fgets((char*)GVTOP(Args[0]), Args[1].IntVal, 00608 getFILE(GVTOP(Args[2])))); 00609 } 00610 00611 // FILE *freopen(const char *path, const char *mode, FILE *stream); 00612 GenericValue lle_X_freopen(FunctionType *M, const vector<GenericValue> &Args) { 00613 assert(Args.size() == 3); 00614 return PTOGV(freopen((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), 00615 getFILE(GVTOP(Args[2])))); 00616 } 00617 00618 // int fflush(FILE *stream); 00619 GenericValue lle_X_fflush(FunctionType *M, const vector<GenericValue> &Args) { 00620 assert(Args.size() == 1); 00621 GenericValue GV; 00622 GV.IntVal = fflush(getFILE(GVTOP(Args[0]))); 00623 return GV; 00624 } 00625 00626 // int getc(FILE *stream); 00627 GenericValue lle_X_getc(FunctionType *M, const vector<GenericValue> &Args) { 00628 assert(Args.size() == 1); 00629 GenericValue GV; 00630 GV.IntVal = getc(getFILE(GVTOP(Args[0]))); 00631 return GV; 00632 } 00633 00634 // int _IO_getc(FILE *stream); 00635 GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) { 00636 return lle_X_getc(F, Args); 00637 } 00638 00639 // int fputc(int C, FILE *stream); 00640 GenericValue lle_X_fputc(FunctionType *M, const vector<GenericValue> &Args) { 00641 assert(Args.size() == 2); 00642 GenericValue GV; 00643 GV.IntVal = fputc(Args[0].IntVal, getFILE(GVTOP(Args[1]))); 00644 return GV; 00645 } 00646 00647 // int ungetc(int C, FILE *stream); 00648 GenericValue lle_X_ungetc(FunctionType *M, const vector<GenericValue> &Args) { 00649 assert(Args.size() == 2); 00650 GenericValue GV; 00651 GV.IntVal = ungetc(Args[0].IntVal, getFILE(GVTOP(Args[1]))); 00652 return GV; 00653 } 00654 00655 // int ferror (FILE *stream); 00656 GenericValue lle_X_ferror(FunctionType *M, const vector<GenericValue> &Args) { 00657 assert(Args.size() == 1); 00658 GenericValue GV; 00659 GV.IntVal = ferror (getFILE(GVTOP(Args[0]))); 00660 return GV; 00661 } 00662 00663 // int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output 00664 // useful. 00665 GenericValue lle_X_fprintf(FunctionType *M, const vector<GenericValue> &Args) { 00666 assert(Args.size() >= 2); 00667 char Buffer[10000]; 00668 vector<GenericValue> NewArgs; 00669 NewArgs.push_back(PTOGV(Buffer)); 00670 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end()); 00671 GenericValue GV = lle_X_sprintf(M, NewArgs); 00672 00673 fputs(Buffer, getFILE(GVTOP(Args[0]))); 00674 return GV; 00675 } 00676 00677 } // End extern "C" 00678 00679 00680 void Interpreter::initializeExternalFunctions() { 00681 FuncNames["lle_Vb_putchar"] = lle_Vb_putchar; 00682 FuncNames["lle_ii_putchar"] = lle_ii_putchar; 00683 FuncNames["lle_VB_putchar"] = lle_VB_putchar; 00684 FuncNames["lle_X_exit"] = lle_X_exit; 00685 FuncNames["lle_X_abort"] = lle_X_abort; 00686 FuncNames["lle_X_malloc"] = lle_X_malloc; 00687 FuncNames["lle_X_calloc"] = lle_X_calloc; 00688 FuncNames["lle_X_free"] = lle_X_free; 00689 FuncNames["lle_X_atoi"] = lle_X_atoi; 00690 FuncNames["lle_X_pow"] = lle_X_pow; 00691 FuncNames["lle_X_exp"] = lle_X_exp; 00692 FuncNames["lle_X_log"] = lle_X_log; 00693 FuncNames["lle_X_floor"] = lle_X_floor; 00694 FuncNames["lle_X_srand"] = lle_X_srand; 00695 FuncNames["lle_X_rand"] = lle_X_rand; 00696 #ifdef HAVE_RAND48 00697 FuncNames["lle_X_drand48"] = lle_X_drand48; 00698 FuncNames["lle_X_srand48"] = lle_X_srand48; 00699 FuncNames["lle_X_lrand48"] = lle_X_lrand48; 00700 #endif 00701 FuncNames["lle_X_sqrt"] = lle_X_sqrt; 00702 FuncNames["lle_X_puts"] = lle_X_puts; 00703 FuncNames["lle_X_printf"] = lle_X_printf; 00704 FuncNames["lle_X_sprintf"] = lle_X_sprintf; 00705 FuncNames["lle_X_sscanf"] = lle_X_sscanf; 00706 FuncNames["lle_X_scanf"] = lle_X_scanf; 00707 FuncNames["lle_i_clock"] = lle_i_clock; 00708 00709 FuncNames["lle_X_strcmp"] = lle_X_strcmp; 00710 FuncNames["lle_X_strcat"] = lle_X_strcat; 00711 FuncNames["lle_X_strcpy"] = lle_X_strcpy; 00712 FuncNames["lle_X_strlen"] = lle_X_strlen; 00713 FuncNames["lle_X___strdup"] = lle_X___strdup; 00714 FuncNames["lle_X_memset"] = lle_X_memset; 00715 FuncNames["lle_X_memcpy"] = lle_X_memcpy; 00716 00717 FuncNames["lle_X_fopen"] = lle_X_fopen; 00718 FuncNames["lle_X_fclose"] = lle_X_fclose; 00719 FuncNames["lle_X_feof"] = lle_X_feof; 00720 FuncNames["lle_X_fread"] = lle_X_fread; 00721 FuncNames["lle_X_fwrite"] = lle_X_fwrite; 00722 FuncNames["lle_X_fgets"] = lle_X_fgets; 00723 FuncNames["lle_X_fflush"] = lle_X_fflush; 00724 FuncNames["lle_X_fgetc"] = lle_X_getc; 00725 FuncNames["lle_X_getc"] = lle_X_getc; 00726 FuncNames["lle_X__IO_getc"] = lle_X__IO_getc; 00727 FuncNames["lle_X_fputc"] = lle_X_fputc; 00728 FuncNames["lle_X_ungetc"] = lle_X_ungetc; 00729 FuncNames["lle_X_fprintf"] = lle_X_fprintf; 00730 FuncNames["lle_X_freopen"] = lle_X_freopen; 00731 } 00732