blob: 21a6f761db53f57a2279cddfd5efcf635db66ed6 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ExternalFunctions.cpp - Implement External Functions --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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//
Nick Lewycky63254ee2009-02-04 06:26:47 +000013// There are currently two mechanisms for handling external functions in the
14// Interpreter. The first is to implement lle_* wrapper functions that are
15// specific to well-known library functions which manually translate the
16// arguments from GenericValues and make the call. If such a wrapper does
17// not exist, and libffi is available, then the Interpreter will attempt to
18// invoke the function using libffi, after finding its address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "Interpreter.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Module.h"
Nick Lewycky63254ee2009-02-04 06:26:47 +000025#include "llvm/Config/config.h" // Detect libffi
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Support/Streams.h"
27#include "llvm/System/DynamicLibrary.h"
28#include "llvm/Target/TargetData.h"
Chuck Rose III9a2da442007-07-27 18:26:35 +000029#include "llvm/Support/ManagedStatic.h"
Owen Anderson7c9c0682009-06-22 22:30:56 +000030#include "llvm/System/Mutex.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include <csignal>
Duncan Sands05f68372008-10-08 07:23:46 +000032#include <cstdio>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include <map>
34#include <cmath>
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000035#include <cstring>
Zhou Shenga80aaba2007-12-12 06:16:47 +000036
Nick Lewyckyc0b55e62009-04-13 04:26:06 +000037#ifdef HAVE_FFI_CALL
Nick Lewycky63254ee2009-02-04 06:26:47 +000038#ifdef HAVE_FFI_H
39#include <ffi.h>
Nick Lewyckyc0b55e62009-04-13 04:26:06 +000040#define USE_LIBFFI
Nick Lewycky63254ee2009-02-04 06:26:47 +000041#elif HAVE_FFI_FFI_H
42#include <ffi/ffi.h>
Nick Lewyckyc0b55e62009-04-13 04:26:06 +000043#define USE_LIBFFI
Zhou Shenga80aaba2007-12-12 06:16:47 +000044#endif
Nick Lewycky63254ee2009-02-04 06:26:47 +000045#endif
Tanya Lattner295e3772009-01-22 20:09:20 +000046
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047using namespace llvm;
48
Owen Anderson7c9c0682009-06-22 22:30:56 +000049static ManagedStatic<sys::Mutex> FunctionsLock;
50
Nick Lewycky63254ee2009-02-04 06:26:47 +000051typedef GenericValue (*ExFunc)(const FunctionType *,
52 const std::vector<GenericValue> &);
53static ManagedStatic<std::map<const Function *, ExFunc> > ExportedFunctions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054static std::map<std::string, ExFunc> FuncNames;
55
Nick Lewyckyc0b55e62009-04-13 04:26:06 +000056#ifdef USE_LIBFFI
Nick Lewycky63254ee2009-02-04 06:26:47 +000057typedef void (*RawFunc)(void);
58static ManagedStatic<std::map<const Function *, RawFunc> > RawFunctions;
59#endif
60
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061static Interpreter *TheInterpreter;
62
63static char getTypeID(const Type *Ty) {
64 switch (Ty->getTypeID()) {
65 case Type::VoidTyID: return 'V';
66 case Type::IntegerTyID:
67 switch (cast<IntegerType>(Ty)->getBitWidth()) {
68 case 1: return 'o';
69 case 8: return 'B';
70 case 16: return 'S';
71 case 32: return 'I';
72 case 64: return 'L';
73 default: return 'N';
74 }
75 case Type::FloatTyID: return 'F';
76 case Type::DoubleTyID: return 'D';
77 case Type::PointerTyID: return 'P';
78 case Type::FunctionTyID:return 'M';
79 case Type::StructTyID: return 'T';
80 case Type::ArrayTyID: return 'A';
81 case Type::OpaqueTyID: return 'O';
82 default: return 'U';
83 }
84}
85
Anton Korobeynikov85a94032007-07-30 23:03:25 +000086// Try to find address of external function given a Function object.
87// Please note, that interpreter doesn't know how to assemble a
88// real call in general case (this is JIT job), that's why it assumes,
89// that all external functions has the same (and pretty "general") signature.
90// The typical example of such functions are "lle_X_" ones.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091static ExFunc lookupFunction(const Function *F) {
92 // Function not found, look it up... start by figuring out what the
93 // composite function name should be.
94 std::string ExtName = "lle_";
95 const FunctionType *FT = F->getFunctionType();
96 for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
97 ExtName += getTypeID(FT->getContainedType(i));
98 ExtName += "_" + F->getName();
99
Owen Andersonbe44bed2009-07-07 18:33:04 +0000100 sys::ScopedLock Writer(*FunctionsLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 ExFunc FnPtr = FuncNames[ExtName];
102 if (FnPtr == 0)
103 FnPtr = FuncNames["lle_X_"+F->getName()];
104 if (FnPtr == 0) // Try calling a generic function... if it exists...
105 FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(
106 ("lle_X_"+F->getName()).c_str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 if (FnPtr != 0)
Nick Lewycky63254ee2009-02-04 06:26:47 +0000108 ExportedFunctions->insert(std::make_pair(F, FnPtr)); // Cache for later
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 return FnPtr;
110}
111
Nick Lewyckyc0b55e62009-04-13 04:26:06 +0000112#ifdef USE_LIBFFI
Nick Lewycky63254ee2009-02-04 06:26:47 +0000113static ffi_type *ffiTypeFor(const Type *Ty) {
114 switch (Ty->getTypeID()) {
115 case Type::VoidTyID: return &ffi_type_void;
116 case Type::IntegerTyID:
117 switch (cast<IntegerType>(Ty)->getBitWidth()) {
118 case 8: return &ffi_type_sint8;
119 case 16: return &ffi_type_sint16;
120 case 32: return &ffi_type_sint32;
121 case 64: return &ffi_type_sint64;
122 }
123 case Type::FloatTyID: return &ffi_type_float;
124 case Type::DoubleTyID: return &ffi_type_double;
125 case Type::PointerTyID: return &ffi_type_pointer;
126 default: break;
127 }
128 // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
129 cerr << "Type could not be mapped for use with libffi.\n";
130 abort();
131 return NULL;
132}
133
134static void *ffiValueFor(const Type *Ty, const GenericValue &AV,
135 void *ArgDataPtr) {
136 switch (Ty->getTypeID()) {
137 case Type::IntegerTyID:
138 switch (cast<IntegerType>(Ty)->getBitWidth()) {
139 case 8: {
140 int8_t *I8Ptr = (int8_t *) ArgDataPtr;
141 *I8Ptr = (int8_t) AV.IntVal.getZExtValue();
142 return ArgDataPtr;
143 }
144 case 16: {
145 int16_t *I16Ptr = (int16_t *) ArgDataPtr;
146 *I16Ptr = (int16_t) AV.IntVal.getZExtValue();
147 return ArgDataPtr;
148 }
149 case 32: {
150 int32_t *I32Ptr = (int32_t *) ArgDataPtr;
151 *I32Ptr = (int32_t) AV.IntVal.getZExtValue();
152 return ArgDataPtr;
153 }
154 case 64: {
155 int64_t *I64Ptr = (int64_t *) ArgDataPtr;
156 *I64Ptr = (int64_t) AV.IntVal.getZExtValue();
157 return ArgDataPtr;
158 }
159 }
160 case Type::FloatTyID: {
161 float *FloatPtr = (float *) ArgDataPtr;
162 *FloatPtr = AV.DoubleVal;
163 return ArgDataPtr;
164 }
165 case Type::DoubleTyID: {
166 double *DoublePtr = (double *) ArgDataPtr;
167 *DoublePtr = AV.DoubleVal;
168 return ArgDataPtr;
169 }
170 case Type::PointerTyID: {
171 void **PtrPtr = (void **) ArgDataPtr;
172 *PtrPtr = GVTOP(AV);
173 return ArgDataPtr;
174 }
175 default: break;
176 }
177 // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
178 cerr << "Type value could not be mapped for use with libffi.\n";
179 abort();
180 return NULL;
181}
182
183static bool ffiInvoke(RawFunc Fn, Function *F,
184 const std::vector<GenericValue> &ArgVals,
185 const TargetData *TD, GenericValue &Result) {
186 ffi_cif cif;
187 const FunctionType *FTy = F->getFunctionType();
188 const unsigned NumArgs = F->arg_size();
189
190 // TODO: We don't have type information about the remaining arguments, because
191 // this information is never passed into ExecutionEngine::runFunction().
192 if (ArgVals.size() > NumArgs && F->isVarArg()) {
193 cerr << "Calling external var arg function '" << F->getName()
194 << "' is not supported by the Interpreter.\n";
195 abort();
196 }
197
198 unsigned ArgBytes = 0;
199
200 std::vector<ffi_type*> args(NumArgs);
201 for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();
202 A != E; ++A) {
203 const unsigned ArgNo = A->getArgNo();
204 const Type *ArgTy = FTy->getParamType(ArgNo);
205 args[ArgNo] = ffiTypeFor(ArgTy);
206 ArgBytes += TD->getTypeStoreSize(ArgTy);
207 }
208
209 uint8_t *ArgData = (uint8_t*) alloca(ArgBytes);
210 uint8_t *ArgDataPtr = ArgData;
211 std::vector<void*> values(NumArgs);
212 for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();
213 A != E; ++A) {
214 const unsigned ArgNo = A->getArgNo();
215 const Type *ArgTy = FTy->getParamType(ArgNo);
216 values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);
217 ArgDataPtr += TD->getTypeStoreSize(ArgTy);
218 }
219
220 const Type *RetTy = FTy->getReturnType();
221 ffi_type *rtype = ffiTypeFor(RetTy);
222
223 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, &args[0]) == FFI_OK) {
224 void *ret = NULL;
225 if (RetTy->getTypeID() != Type::VoidTyID)
226 ret = alloca(TD->getTypeStoreSize(RetTy));
227 ffi_call(&cif, Fn, ret, &values[0]);
228 switch (RetTy->getTypeID()) {
229 case Type::IntegerTyID:
230 switch (cast<IntegerType>(RetTy)->getBitWidth()) {
231 case 8: Result.IntVal = APInt(8 , *(int8_t *) ret); break;
232 case 16: Result.IntVal = APInt(16, *(int16_t*) ret); break;
233 case 32: Result.IntVal = APInt(32, *(int32_t*) ret); break;
234 case 64: Result.IntVal = APInt(64, *(int64_t*) ret); break;
235 }
236 break;
237 case Type::FloatTyID: Result.FloatVal = *(float *) ret; break;
238 case Type::DoubleTyID: Result.DoubleVal = *(double*) ret; break;
239 case Type::PointerTyID: Result.PointerVal = *(void **) ret; break;
240 default: break;
241 }
242 return true;
243 }
244
245 return false;
246}
Nick Lewyckyc0b55e62009-04-13 04:26:06 +0000247#endif // USE_LIBFFI
Nick Lewycky63254ee2009-02-04 06:26:47 +0000248
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249GenericValue Interpreter::callExternalFunction(Function *F,
250 const std::vector<GenericValue> &ArgVals) {
251 TheInterpreter = this;
252
Owen Anderson7c9c0682009-06-22 22:30:56 +0000253 FunctionsLock->acquire();
254
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 // Do a lookup to see if the function is in our cache... this should just be a
256 // deferred annotation!
Nick Lewycky63254ee2009-02-04 06:26:47 +0000257 std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F);
258 if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F)
Owen Anderson7c9c0682009-06-22 22:30:56 +0000259 : FI->second) {
260 FunctionsLock->release();
Nick Lewycky63254ee2009-02-04 06:26:47 +0000261 return Fn(F->getFunctionType(), ArgVals);
Owen Anderson7c9c0682009-06-22 22:30:56 +0000262 }
Nick Lewycky63254ee2009-02-04 06:26:47 +0000263
Nick Lewyckyc0b55e62009-04-13 04:26:06 +0000264#ifdef USE_LIBFFI
Nick Lewycky63254ee2009-02-04 06:26:47 +0000265 std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F);
266 RawFunc RawFn;
267 if (RF == RawFunctions->end()) {
268 RawFn = (RawFunc)(intptr_t)
269 sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
270 if (RawFn != 0)
271 RawFunctions->insert(std::make_pair(F, RawFn)); // Cache for later
272 } else {
273 RawFn = RF->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 }
Owen Anderson7c9c0682009-06-22 22:30:56 +0000275
276 FunctionsLock->release();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277
Nick Lewycky63254ee2009-02-04 06:26:47 +0000278 GenericValue Result;
279 if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals, getTargetData(), Result))
280 return Result;
Nick Lewyckyc0b55e62009-04-13 04:26:06 +0000281#endif // USE_LIBFFI
Nick Lewycky63254ee2009-02-04 06:26:47 +0000282
283 cerr << "Tried to execute an unknown external function: "
284 << F->getType()->getDescription() << " " << F->getName() << "\n";
285 if (F->getName() != "__main")
286 abort();
287 return GenericValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288}
289
290
291//===----------------------------------------------------------------------===//
292// Functions "exported" to the running application...
293//
294extern "C" { // Don't add C++ manglings to llvm mangling :)
295
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296// void atexit(Function*)
Nick Lewycky63254ee2009-02-04 06:26:47 +0000297GenericValue lle_X_atexit(const FunctionType *FT,
298 const std::vector<GenericValue> &Args) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 assert(Args.size() == 1);
300 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
301 GenericValue GV;
302 GV.IntVal = 0;
303 return GV;
304}
305
306// void exit(int)
Nick Lewycky63254ee2009-02-04 06:26:47 +0000307GenericValue lle_X_exit(const FunctionType *FT,
308 const std::vector<GenericValue> &Args) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 TheInterpreter->exitCalled(Args[0]);
310 return GenericValue();
311}
312
313// void abort(void)
Nick Lewycky63254ee2009-02-04 06:26:47 +0000314GenericValue lle_X_abort(const FunctionType *FT,
315 const std::vector<GenericValue> &Args) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 raise (SIGABRT);
317 return GenericValue();
318}
319
Nick Lewycky63254ee2009-02-04 06:26:47 +0000320// int sprintf(char *, const char *, ...) - a very rough implementation to make
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321// output useful.
Nick Lewycky63254ee2009-02-04 06:26:47 +0000322GenericValue lle_X_sprintf(const FunctionType *FT,
323 const std::vector<GenericValue> &Args) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 char *OutputBuffer = (char *)GVTOP(Args[0]);
325 const char *FmtStr = (const char *)GVTOP(Args[1]);
326 unsigned ArgNo = 2;
327
328 // printf should return # chars printed. This is completely incorrect, but
329 // close enough for now.
330 GenericValue GV;
331 GV.IntVal = APInt(32, strlen(FmtStr));
332 while (1) {
333 switch (*FmtStr) {
334 case 0: return GV; // Null terminator...
335 default: // Normal nonspecial character
336 sprintf(OutputBuffer++, "%c", *FmtStr++);
337 break;
338 case '\\': { // Handle escape codes
339 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
340 FmtStr += 2; OutputBuffer += 2;
341 break;
342 }
343 case '%': { // Handle format specifiers
344 char FmtBuf[100] = "", Buffer[1000] = "";
345 char *FB = FmtBuf;
346 *FB++ = *FmtStr++;
347 char Last = *FB++ = *FmtStr++;
348 unsigned HowLong = 0;
349 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
350 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
351 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
352 Last != 'p' && Last != 's' && Last != '%') {
353 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
354 Last = *FB++ = *FmtStr++;
355 }
356 *FB = 0;
357
358 switch (Last) {
359 case '%':
Dan Gohmane5dce982008-08-05 23:36:35 +0000360 strcpy(Buffer, "%"); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 case 'c':
362 sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
363 break;
364 case 'd': case 'i':
365 case 'u': case 'o':
366 case 'x': case 'X':
367 if (HowLong >= 1) {
368 if (HowLong == 1 &&
369 TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 &&
370 sizeof(long) < sizeof(int64_t)) {
371 // Make sure we use %lld with a 64 bit argument because we might be
372 // compiling LLI on a 32 bit compiler.
373 unsigned Size = strlen(FmtBuf);
374 FmtBuf[Size] = FmtBuf[Size-1];
375 FmtBuf[Size+1] = 0;
376 FmtBuf[Size-1] = 'l';
377 }
378 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
379 } else
380 sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
381 break;
382 case 'e': case 'E': case 'g': case 'G': case 'f':
383 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
384 case 'p':
385 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
386 case 's':
387 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
388 default: cerr << "<unknown printf code '" << *FmtStr << "'!>";
389 ArgNo++; break;
390 }
391 strcpy(OutputBuffer, Buffer);
392 OutputBuffer += strlen(Buffer);
393 }
394 break;
395 }
396 }
397 return GV;
398}
399
Nick Lewycky63254ee2009-02-04 06:26:47 +0000400// int printf(const char *, ...) - a very rough implementation to make output
401// useful.
402GenericValue lle_X_printf(const FunctionType *FT,
403 const std::vector<GenericValue> &Args) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 char Buffer[10000];
Nick Lewycky63254ee2009-02-04 06:26:47 +0000405 std::vector<GenericValue> NewArgs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 NewArgs.push_back(PTOGV((void*)&Buffer[0]));
407 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
408 GenericValue GV = lle_X_sprintf(FT, NewArgs);
409 cout << Buffer;
410 return GV;
411}
412
413static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1,
414 void *Arg2, void *Arg3, void *Arg4, void *Arg5,
415 void *Arg6, void *Arg7, void *Arg8) {
416 void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 };
417
418 // Loop over the format string, munging read values as appropriate (performs
419 // byteswaps as necessary).
420 unsigned ArgNo = 0;
421 while (*Fmt) {
422 if (*Fmt++ == '%') {
423 // Read any flag characters that may be present...
424 bool Suppress = false;
425 bool Half = false;
426 bool Long = false;
427 bool LongLong = false; // long long or long double
428
429 while (1) {
430 switch (*Fmt++) {
431 case '*': Suppress = true; break;
432 case 'a': /*Allocate = true;*/ break; // We don't need to track this
433 case 'h': Half = true; break;
434 case 'l': Long = true; break;
435 case 'q':
436 case 'L': LongLong = true; break;
437 default:
438 if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs
439 goto Out;
440 }
441 }
442 Out:
443
444 // Read the conversion character
445 if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
446 unsigned Size = 0;
447 const Type *Ty = 0;
448
449 switch (Fmt[-1]) {
450 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
451 case 'd':
452 if (Long || LongLong) {
453 Size = 8; Ty = Type::Int64Ty;
454 } else if (Half) {
455 Size = 4; Ty = Type::Int16Ty;
456 } else {
457 Size = 4; Ty = Type::Int32Ty;
458 }
459 break;
460
461 case 'e': case 'g': case 'E':
462 case 'f':
463 if (Long || LongLong) {
464 Size = 8; Ty = Type::DoubleTy;
465 } else {
466 Size = 4; Ty = Type::FloatTy;
467 }
468 break;
469
470 case 's': case 'c': case '[': // No byteswap needed
471 Size = 1;
472 Ty = Type::Int8Ty;
473 break;
474
475 default: break;
476 }
477
478 if (Size) {
479 GenericValue GV;
480 void *Arg = Args[ArgNo++];
481 memcpy(&GV, Arg, Size);
482 TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
483 }
484 }
485 }
486 }
487}
488
489// int sscanf(const char *format, ...);
Nick Lewycky63254ee2009-02-04 06:26:47 +0000490GenericValue lle_X_sscanf(const FunctionType *FT,
491 const std::vector<GenericValue> &args) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
493
494 char *Args[10];
495 for (unsigned i = 0; i < args.size(); ++i)
496 Args[i] = (char*)GVTOP(args[i]);
497
498 GenericValue GV;
499 GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
500 Args[5], Args[6], Args[7], Args[8], Args[9]));
501 ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4],
502 Args[5], Args[6], Args[7], Args[8], Args[9], 0);
503 return GV;
504}
505
506// int scanf(const char *format, ...);
Nick Lewycky63254ee2009-02-04 06:26:47 +0000507GenericValue lle_X_scanf(const FunctionType *FT,
508 const std::vector<GenericValue> &args) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
510
511 char *Args[10];
512 for (unsigned i = 0; i < args.size(); ++i)
513 Args[i] = (char*)GVTOP(args[i]);
514
515 GenericValue GV;
516 GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
517 Args[5], Args[6], Args[7], Args[8], Args[9]));
518 ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4],
519 Args[5], Args[6], Args[7], Args[8], Args[9]);
520 return GV;
521}
522
Nick Lewycky63254ee2009-02-04 06:26:47 +0000523// int fprintf(FILE *, const char *, ...) - a very rough implementation to make
524// output useful.
525GenericValue lle_X_fprintf(const FunctionType *FT,
526 const std::vector<GenericValue> &Args) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 assert(Args.size() >= 2);
528 char Buffer[10000];
Nick Lewycky63254ee2009-02-04 06:26:47 +0000529 std::vector<GenericValue> NewArgs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 NewArgs.push_back(PTOGV(Buffer));
531 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
532 GenericValue GV = lle_X_sprintf(FT, NewArgs);
533
Nick Lewycky63254ee2009-02-04 06:26:47 +0000534 fputs(Buffer, (FILE *) GVTOP(Args[0]));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 return GV;
536}
537
538} // End extern "C"
539
540
541void Interpreter::initializeExternalFunctions() {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000542 sys::ScopedLock Writer(*FunctionsLock);
Nick Lewycky63254ee2009-02-04 06:26:47 +0000543 FuncNames["lle_X_atexit"] = lle_X_atexit;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 FuncNames["lle_X_exit"] = lle_X_exit;
545 FuncNames["lle_X_abort"] = lle_X_abort;
Nick Lewycky63254ee2009-02-04 06:26:47 +0000546
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 FuncNames["lle_X_printf"] = lle_X_printf;
548 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
549 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
550 FuncNames["lle_X_scanf"] = lle_X_scanf;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551 FuncNames["lle_X_fprintf"] = lle_X_fprintf;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552}
553