Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1 | //===-- ExternalFunctions.cpp - Implement External Functions --------------===// |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 10 | // This file contains both code to deal with invoking "external" functions, but |
| 11 | // also contains code that implements "exported" external functions. |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 12 | // |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 13 | // 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. |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "Interpreter.h" |
| 23 | #include "llvm/DerivedTypes.h" |
Misha Brukman | b8d15b2 | 2003-10-14 21:42:11 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 25 | #include "llvm/Config/config.h" // Detect libffi |
Bill Wendling | 480f093 | 2006-11-27 23:54:50 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Streams.h" |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 27 | #include "llvm/System/DynamicLibrary.h" |
Chris Lattner | 005cbce | 2002-10-02 21:12:13 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Chuck Rose III | 936baaa | 2007-07-27 18:26:35 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ManagedStatic.h" |
Brian Gaeke | b56a6bc | 2003-11-05 01:18:49 +0000 | [diff] [blame] | 30 | #include <csignal> |
Duncan Sands | 4520dd2 | 2008-10-08 07:23:46 +0000 | [diff] [blame] | 31 | #include <cstdio> |
Misha Brukman | b8d15b2 | 2003-10-14 21:42:11 +0000 | [diff] [blame] | 32 | #include <map> |
Jeff Cohen | 97af751 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 33 | #include <cmath> |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 34 | #include <cstring> |
Zhou Sheng | 621dead | 2007-12-12 06:16:47 +0000 | [diff] [blame] | 35 | |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 36 | #ifdef HAVE_LIBFFI |
| 37 | #include FFI_HEADER |
Zhou Sheng | 621dead | 2007-12-12 06:16:47 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
Chris Lattner | f7a743d | 2003-12-14 23:25:48 +0000 | [diff] [blame] | 40 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 41 | |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 42 | typedef GenericValue (*ExFunc)(const FunctionType *, |
| 43 | const std::vector<GenericValue> &); |
| 44 | static ManagedStatic<std::map<const Function *, ExFunc> > ExportedFunctions; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 45 | static std::map<std::string, ExFunc> FuncNames; |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 46 | |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 47 | #ifdef HAVE_LIBFFI |
| 48 | typedef void (*RawFunc)(void); |
| 49 | static ManagedStatic<std::map<const Function *, RawFunc> > RawFunctions; |
| 50 | #endif // HAVE_LIBFFI |
| 51 | |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 52 | static Interpreter *TheInterpreter; |
| 53 | |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 54 | static char getTypeID(const Type *Ty) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 55 | switch (Ty->getTypeID()) { |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 56 | case Type::VoidTyID: return 'V'; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 57 | case Type::IntegerTyID: |
| 58 | switch (cast<IntegerType>(Ty)->getBitWidth()) { |
| 59 | case 1: return 'o'; |
| 60 | case 8: return 'B'; |
| 61 | case 16: return 'S'; |
| 62 | case 32: return 'I'; |
| 63 | case 64: return 'L'; |
| 64 | default: return 'N'; |
| 65 | } |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 66 | case Type::FloatTyID: return 'F'; |
| 67 | case Type::DoubleTyID: return 'D'; |
| 68 | case Type::PointerTyID: return 'P'; |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 69 | case Type::FunctionTyID:return 'M'; |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 70 | case Type::StructTyID: return 'T'; |
| 71 | case Type::ArrayTyID: return 'A'; |
| 72 | case Type::OpaqueTyID: return 'O'; |
| 73 | default: return 'U'; |
| 74 | } |
| 75 | } |
| 76 | |
Anton Korobeynikov | 42346f5 | 2007-07-30 23:03:25 +0000 | [diff] [blame] | 77 | // Try to find address of external function given a Function object. |
| 78 | // Please note, that interpreter doesn't know how to assemble a |
| 79 | // real call in general case (this is JIT job), that's why it assumes, |
| 80 | // that all external functions has the same (and pretty "general") signature. |
| 81 | // The typical example of such functions are "lle_X_" ones. |
Brian Gaeke | 58a6faa | 2003-10-10 17:03:10 +0000 | [diff] [blame] | 82 | static ExFunc lookupFunction(const Function *F) { |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 83 | // Function not found, look it up... start by figuring out what the |
| 84 | // composite function name should be. |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 85 | std::string ExtName = "lle_"; |
Brian Gaeke | 58a6faa | 2003-10-10 17:03:10 +0000 | [diff] [blame] | 86 | const FunctionType *FT = F->getFunctionType(); |
| 87 | for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i) |
| 88 | ExtName += getTypeID(FT->getContainedType(i)); |
| 89 | ExtName += "_" + F->getName(); |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 4721f13 | 2001-10-30 20:28:00 +0000 | [diff] [blame] | 91 | ExFunc FnPtr = FuncNames[ExtName]; |
| 92 | if (FnPtr == 0) |
Brian Gaeke | 58a6faa | 2003-10-10 17:03:10 +0000 | [diff] [blame] | 93 | FnPtr = FuncNames["lle_X_"+F->getName()]; |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 94 | if (FnPtr == 0) // Try calling a generic function... if it exists... |
Chris Lattner | 26e6e10 | 2006-06-01 17:27:11 +0000 | [diff] [blame] | 95 | FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol( |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 96 | ("lle_X_"+F->getName()).c_str()); |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 97 | if (FnPtr != 0) |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 98 | ExportedFunctions->insert(std::make_pair(F, FnPtr)); // Cache for later |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 99 | return FnPtr; |
| 100 | } |
| 101 | |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 102 | #ifdef HAVE_LIBFFI |
| 103 | static ffi_type *ffiTypeFor(const Type *Ty) { |
| 104 | switch (Ty->getTypeID()) { |
| 105 | case Type::VoidTyID: return &ffi_type_void; |
| 106 | case Type::IntegerTyID: |
| 107 | switch (cast<IntegerType>(Ty)->getBitWidth()) { |
| 108 | case 8: return &ffi_type_sint8; |
| 109 | case 16: return &ffi_type_sint16; |
| 110 | case 32: return &ffi_type_sint32; |
| 111 | case 64: return &ffi_type_sint64; |
| 112 | } |
| 113 | case Type::FloatTyID: return &ffi_type_float; |
| 114 | case Type::DoubleTyID: return &ffi_type_double; |
| 115 | case Type::PointerTyID: return &ffi_type_pointer; |
| 116 | default: break; |
| 117 | } |
| 118 | // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc. |
| 119 | cerr << "Type could not be mapped for use with libffi.\n"; |
| 120 | abort(); |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | static void *ffiValueFor(const Type *Ty, const GenericValue &AV, |
| 125 | void *ArgDataPtr) { |
| 126 | switch (Ty->getTypeID()) { |
| 127 | case Type::IntegerTyID: |
| 128 | switch (cast<IntegerType>(Ty)->getBitWidth()) { |
| 129 | case 8: { |
| 130 | int8_t *I8Ptr = (int8_t *) ArgDataPtr; |
| 131 | *I8Ptr = (int8_t) AV.IntVal.getZExtValue(); |
| 132 | return ArgDataPtr; |
| 133 | } |
| 134 | case 16: { |
| 135 | int16_t *I16Ptr = (int16_t *) ArgDataPtr; |
| 136 | *I16Ptr = (int16_t) AV.IntVal.getZExtValue(); |
| 137 | return ArgDataPtr; |
| 138 | } |
| 139 | case 32: { |
| 140 | int32_t *I32Ptr = (int32_t *) ArgDataPtr; |
| 141 | *I32Ptr = (int32_t) AV.IntVal.getZExtValue(); |
| 142 | return ArgDataPtr; |
| 143 | } |
| 144 | case 64: { |
| 145 | int64_t *I64Ptr = (int64_t *) ArgDataPtr; |
| 146 | *I64Ptr = (int64_t) AV.IntVal.getZExtValue(); |
| 147 | return ArgDataPtr; |
| 148 | } |
| 149 | } |
| 150 | case Type::FloatTyID: { |
| 151 | float *FloatPtr = (float *) ArgDataPtr; |
| 152 | *FloatPtr = AV.DoubleVal; |
| 153 | return ArgDataPtr; |
| 154 | } |
| 155 | case Type::DoubleTyID: { |
| 156 | double *DoublePtr = (double *) ArgDataPtr; |
| 157 | *DoublePtr = AV.DoubleVal; |
| 158 | return ArgDataPtr; |
| 159 | } |
| 160 | case Type::PointerTyID: { |
| 161 | void **PtrPtr = (void **) ArgDataPtr; |
| 162 | *PtrPtr = GVTOP(AV); |
| 163 | return ArgDataPtr; |
| 164 | } |
| 165 | default: break; |
| 166 | } |
| 167 | // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc. |
| 168 | cerr << "Type value could not be mapped for use with libffi.\n"; |
| 169 | abort(); |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | static bool ffiInvoke(RawFunc Fn, Function *F, |
| 174 | const std::vector<GenericValue> &ArgVals, |
| 175 | const TargetData *TD, GenericValue &Result) { |
| 176 | ffi_cif cif; |
| 177 | const FunctionType *FTy = F->getFunctionType(); |
| 178 | const unsigned NumArgs = F->arg_size(); |
| 179 | |
| 180 | // TODO: We don't have type information about the remaining arguments, because |
| 181 | // this information is never passed into ExecutionEngine::runFunction(). |
| 182 | if (ArgVals.size() > NumArgs && F->isVarArg()) { |
| 183 | cerr << "Calling external var arg function '" << F->getName() |
| 184 | << "' is not supported by the Interpreter.\n"; |
| 185 | abort(); |
| 186 | } |
| 187 | |
| 188 | unsigned ArgBytes = 0; |
| 189 | |
| 190 | std::vector<ffi_type*> args(NumArgs); |
| 191 | for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end(); |
| 192 | A != E; ++A) { |
| 193 | const unsigned ArgNo = A->getArgNo(); |
| 194 | const Type *ArgTy = FTy->getParamType(ArgNo); |
| 195 | args[ArgNo] = ffiTypeFor(ArgTy); |
| 196 | ArgBytes += TD->getTypeStoreSize(ArgTy); |
| 197 | } |
| 198 | |
| 199 | uint8_t *ArgData = (uint8_t*) alloca(ArgBytes); |
| 200 | uint8_t *ArgDataPtr = ArgData; |
| 201 | std::vector<void*> values(NumArgs); |
| 202 | for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end(); |
| 203 | A != E; ++A) { |
| 204 | const unsigned ArgNo = A->getArgNo(); |
| 205 | const Type *ArgTy = FTy->getParamType(ArgNo); |
| 206 | values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr); |
| 207 | ArgDataPtr += TD->getTypeStoreSize(ArgTy); |
| 208 | } |
| 209 | |
| 210 | const Type *RetTy = FTy->getReturnType(); |
| 211 | ffi_type *rtype = ffiTypeFor(RetTy); |
| 212 | |
| 213 | if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, &args[0]) == FFI_OK) { |
| 214 | void *ret = NULL; |
| 215 | if (RetTy->getTypeID() != Type::VoidTyID) |
| 216 | ret = alloca(TD->getTypeStoreSize(RetTy)); |
| 217 | ffi_call(&cif, Fn, ret, &values[0]); |
| 218 | switch (RetTy->getTypeID()) { |
| 219 | case Type::IntegerTyID: |
| 220 | switch (cast<IntegerType>(RetTy)->getBitWidth()) { |
| 221 | case 8: Result.IntVal = APInt(8 , *(int8_t *) ret); break; |
| 222 | case 16: Result.IntVal = APInt(16, *(int16_t*) ret); break; |
| 223 | case 32: Result.IntVal = APInt(32, *(int32_t*) ret); break; |
| 224 | case 64: Result.IntVal = APInt(64, *(int64_t*) ret); break; |
| 225 | } |
| 226 | break; |
| 227 | case Type::FloatTyID: Result.FloatVal = *(float *) ret; break; |
| 228 | case Type::DoubleTyID: Result.DoubleVal = *(double*) ret; break; |
| 229 | case Type::PointerTyID: Result.PointerVal = *(void **) ret; break; |
| 230 | default: break; |
| 231 | } |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | return false; |
| 236 | } |
| 237 | #endif // HAVE_LIBFFI |
| 238 | |
Chris Lattner | 4e7dd8f | 2005-01-21 19:59:37 +0000 | [diff] [blame] | 239 | GenericValue Interpreter::callExternalFunction(Function *F, |
Chris Lattner | 44edb6b | 2003-05-14 14:21:30 +0000 | [diff] [blame] | 240 | const std::vector<GenericValue> &ArgVals) { |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 241 | TheInterpreter = this; |
| 242 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 243 | // Do a lookup to see if the function is in our cache... this should just be a |
Misha Brukman | d5d96b9 | 2003-10-10 17:42:19 +0000 | [diff] [blame] | 244 | // deferred annotation! |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 245 | std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F); |
| 246 | if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F) |
| 247 | : FI->second) |
| 248 | return Fn(F->getFunctionType(), ArgVals); |
| 249 | |
| 250 | #ifdef HAVE_LIBFFI |
| 251 | std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F); |
| 252 | RawFunc RawFn; |
| 253 | if (RF == RawFunctions->end()) { |
| 254 | RawFn = (RawFunc)(intptr_t) |
| 255 | sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName()); |
| 256 | if (RawFn != 0) |
| 257 | RawFunctions->insert(std::make_pair(F, RawFn)); // Cache for later |
| 258 | } else { |
| 259 | RawFn = RF->second; |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 262 | GenericValue Result; |
| 263 | if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals, getTargetData(), Result)) |
| 264 | return Result; |
| 265 | #endif // HAVE_LIBFFI |
| 266 | |
| 267 | cerr << "Tried to execute an unknown external function: " |
| 268 | << F->getType()->getDescription() << " " << F->getName() << "\n"; |
| 269 | if (F->getName() != "__main") |
| 270 | abort(); |
| 271 | return GenericValue(); |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | //===----------------------------------------------------------------------===// |
Chris Lattner | b408b12 | 2002-03-29 03:57:15 +0000 | [diff] [blame] | 276 | // Functions "exported" to the running application... |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 277 | // |
| 278 | extern "C" { // Don't add C++ manglings to llvm mangling :) |
| 279 | |
Chris Lattner | 44edb6b | 2003-05-14 14:21:30 +0000 | [diff] [blame] | 280 | // void atexit(Function*) |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 281 | GenericValue lle_X_atexit(const FunctionType *FT, |
| 282 | const std::vector<GenericValue> &Args) { |
Chris Lattner | 44edb6b | 2003-05-14 14:21:30 +0000 | [diff] [blame] | 283 | assert(Args.size() == 1); |
| 284 | TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0])); |
| 285 | GenericValue GV; |
Reid Spencer | bfcd599 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 286 | GV.IntVal = 0; |
Chris Lattner | 44edb6b | 2003-05-14 14:21:30 +0000 | [diff] [blame] | 287 | return GV; |
Chris Lattner | f8f2afb | 2001-10-18 21:55:32 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Chris Lattner | 005cbce | 2002-10-02 21:12:13 +0000 | [diff] [blame] | 290 | // void exit(int) |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 291 | GenericValue lle_X_exit(const FunctionType *FT, |
| 292 | const std::vector<GenericValue> &Args) { |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 293 | TheInterpreter->exitCalled(Args[0]); |
| 294 | return GenericValue(); |
| 295 | } |
| 296 | |
Chris Lattner | 005cbce | 2002-10-02 21:12:13 +0000 | [diff] [blame] | 297 | // void abort(void) |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 298 | GenericValue lle_X_abort(const FunctionType *FT, |
| 299 | const std::vector<GenericValue> &Args) { |
Brian Gaeke | b56a6bc | 2003-11-05 01:18:49 +0000 | [diff] [blame] | 300 | raise (SIGABRT); |
Chris Lattner | 1ee34a5 | 2002-05-20 21:17:16 +0000 | [diff] [blame] | 301 | return GenericValue(); |
| 302 | } |
| 303 | |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 304 | // int sprintf(char *, const char *, ...) - a very rough implementation to make |
Chris Lattner | e7c6f72 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 305 | // output useful. |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 306 | GenericValue lle_X_sprintf(const FunctionType *FT, |
| 307 | const std::vector<GenericValue> &Args) { |
Chris Lattner | b111874 | 2003-01-13 00:59:47 +0000 | [diff] [blame] | 308 | char *OutputBuffer = (char *)GVTOP(Args[0]); |
| 309 | const char *FmtStr = (const char *)GVTOP(Args[1]); |
Chris Lattner | e7c6f72 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 310 | unsigned ArgNo = 2; |
Chris Lattner | 08845a2 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 311 | |
| 312 | // printf should return # chars printed. This is completely incorrect, but |
| 313 | // close enough for now. |
Reid Spencer | bfcd599 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 314 | GenericValue GV; |
| 315 | GV.IntVal = APInt(32, strlen(FmtStr)); |
Chris Lattner | 08845a2 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 316 | while (1) { |
| 317 | switch (*FmtStr) { |
| 318 | case 0: return GV; // Null terminator... |
| 319 | default: // Normal nonspecial character |
Chris Lattner | e7c6f72 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 320 | sprintf(OutputBuffer++, "%c", *FmtStr++); |
Chris Lattner | 08845a2 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 321 | break; |
| 322 | case '\\': { // Handle escape codes |
Chris Lattner | e7c6f72 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 323 | sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1)); |
| 324 | FmtStr += 2; OutputBuffer += 2; |
Chris Lattner | 08845a2 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 325 | break; |
| 326 | } |
| 327 | case '%': { // Handle format specifiers |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 328 | char FmtBuf[100] = "", Buffer[1000] = ""; |
| 329 | char *FB = FmtBuf; |
| 330 | *FB++ = *FmtStr++; |
| 331 | char Last = *FB++ = *FmtStr++; |
| 332 | unsigned HowLong = 0; |
| 333 | while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' && |
| 334 | Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' && |
| 335 | Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' && |
| 336 | Last != 'p' && Last != 's' && Last != '%') { |
| 337 | if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's |
| 338 | Last = *FB++ = *FmtStr++; |
Chris Lattner | 08845a2 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 339 | } |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 340 | *FB = 0; |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 341 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 342 | switch (Last) { |
| 343 | case '%': |
Dan Gohman | 1eac4e0 | 2008-08-05 23:36:35 +0000 | [diff] [blame] | 344 | strcpy(Buffer, "%"); break; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 345 | case 'c': |
Reid Spencer | bfcd599 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 346 | sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue())); |
| 347 | break; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 348 | case 'd': case 'i': |
| 349 | case 'u': case 'o': |
| 350 | case 'x': case 'X': |
Chris Lattner | 69ab7a8 | 2002-08-02 23:08:32 +0000 | [diff] [blame] | 351 | if (HowLong >= 1) { |
Chris Lattner | 1543e40 | 2003-08-24 14:02:47 +0000 | [diff] [blame] | 352 | if (HowLong == 1 && |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 353 | TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 && |
Reid Spencer | 19b7e0e | 2006-05-24 19:21:13 +0000 | [diff] [blame] | 354 | sizeof(long) < sizeof(int64_t)) { |
Chris Lattner | 69ab7a8 | 2002-08-02 23:08:32 +0000 | [diff] [blame] | 355 | // Make sure we use %lld with a 64 bit argument because we might be |
| 356 | // compiling LLI on a 32 bit compiler. |
| 357 | unsigned Size = strlen(FmtBuf); |
| 358 | FmtBuf[Size] = FmtBuf[Size-1]; |
| 359 | FmtBuf[Size+1] = 0; |
| 360 | FmtBuf[Size-1] = 'l'; |
| 361 | } |
Reid Spencer | bfcd599 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 362 | sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue()); |
Chris Lattner | 69ab7a8 | 2002-08-02 23:08:32 +0000 | [diff] [blame] | 363 | } else |
Reid Spencer | bfcd599 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 364 | sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue())); |
| 365 | break; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 366 | case 'e': case 'E': case 'g': case 'G': case 'f': |
| 367 | sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break; |
| 368 | case 'p': |
Chris Lattner | b111874 | 2003-01-13 00:59:47 +0000 | [diff] [blame] | 369 | sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break; |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 370 | case 's': |
Chris Lattner | b111874 | 2003-01-13 00:59:47 +0000 | [diff] [blame] | 371 | sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break; |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 372 | default: cerr << "<unknown printf code '" << *FmtStr << "'!>"; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 373 | ArgNo++; break; |
Chris Lattner | 08845a2 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 374 | } |
Chris Lattner | e7c6f72 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 375 | strcpy(OutputBuffer, Buffer); |
| 376 | OutputBuffer += strlen(Buffer); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 377 | } |
Chris Lattner | 08845a2 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 378 | break; |
| 379 | } |
Chris Lattner | 08845a2 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 380 | } |
Reid Spencer | b3b0727 | 2007-04-21 17:11:45 +0000 | [diff] [blame] | 381 | return GV; |
Chris Lattner | 08845a2 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 384 | // int printf(const char *, ...) - a very rough implementation to make output |
| 385 | // useful. |
| 386 | GenericValue lle_X_printf(const FunctionType *FT, |
| 387 | const std::vector<GenericValue> &Args) { |
Chris Lattner | e7c6f72 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 388 | char Buffer[10000]; |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 389 | std::vector<GenericValue> NewArgs; |
Reid Spencer | b3b0727 | 2007-04-21 17:11:45 +0000 | [diff] [blame] | 390 | NewArgs.push_back(PTOGV((void*)&Buffer[0])); |
Chris Lattner | e7c6f72 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 391 | NewArgs.insert(NewArgs.end(), Args.begin(), Args.end()); |
Reid Spencer | 97e0c22 | 2007-03-30 16:41:50 +0000 | [diff] [blame] | 392 | GenericValue GV = lle_X_sprintf(FT, NewArgs); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 393 | cout << Buffer; |
Chris Lattner | e7c6f72 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 394 | return GV; |
| 395 | } |
| 396 | |
Chris Lattner | f9a88b6 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 397 | static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1, |
| 398 | void *Arg2, void *Arg3, void *Arg4, void *Arg5, |
| 399 | void *Arg6, void *Arg7, void *Arg8) { |
| 400 | void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 }; |
| 401 | |
| 402 | // Loop over the format string, munging read values as appropriate (performs |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 403 | // byteswaps as necessary). |
Chris Lattner | f9a88b6 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 404 | unsigned ArgNo = 0; |
| 405 | while (*Fmt) { |
| 406 | if (*Fmt++ == '%') { |
| 407 | // Read any flag characters that may be present... |
| 408 | bool Suppress = false; |
| 409 | bool Half = false; |
| 410 | bool Long = false; |
| 411 | bool LongLong = false; // long long or long double |
| 412 | |
| 413 | while (1) { |
| 414 | switch (*Fmt++) { |
| 415 | case '*': Suppress = true; break; |
| 416 | case 'a': /*Allocate = true;*/ break; // We don't need to track this |
| 417 | case 'h': Half = true; break; |
| 418 | case 'l': Long = true; break; |
| 419 | case 'q': |
| 420 | case 'L': LongLong = true; break; |
| 421 | default: |
| 422 | if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs |
| 423 | goto Out; |
| 424 | } |
| 425 | } |
| 426 | Out: |
| 427 | |
| 428 | // Read the conversion character |
| 429 | if (!Suppress && Fmt[-1] != '%') { // Nothing to do? |
| 430 | unsigned Size = 0; |
| 431 | const Type *Ty = 0; |
| 432 | |
| 433 | switch (Fmt[-1]) { |
| 434 | case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p': |
| 435 | case 'd': |
| 436 | if (Long || LongLong) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 437 | Size = 8; Ty = Type::Int64Ty; |
Chris Lattner | f9a88b6 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 438 | } else if (Half) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 439 | Size = 4; Ty = Type::Int16Ty; |
Chris Lattner | f9a88b6 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 440 | } else { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 441 | Size = 4; Ty = Type::Int32Ty; |
Chris Lattner | f9a88b6 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 442 | } |
| 443 | break; |
| 444 | |
| 445 | case 'e': case 'g': case 'E': |
| 446 | case 'f': |
| 447 | if (Long || LongLong) { |
| 448 | Size = 8; Ty = Type::DoubleTy; |
| 449 | } else { |
| 450 | Size = 4; Ty = Type::FloatTy; |
| 451 | } |
| 452 | break; |
| 453 | |
| 454 | case 's': case 'c': case '[': // No byteswap needed |
| 455 | Size = 1; |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 456 | Ty = Type::Int8Ty; |
Chris Lattner | f9a88b6 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 457 | break; |
| 458 | |
| 459 | default: break; |
| 460 | } |
| 461 | |
| 462 | if (Size) { |
| 463 | GenericValue GV; |
| 464 | void *Arg = Args[ArgNo++]; |
| 465 | memcpy(&GV, Arg, Size); |
| 466 | TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
Chris Lattner | 665ee88 | 2002-03-08 22:51:07 +0000 | [diff] [blame] | 473 | // int sscanf(const char *format, ...); |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 474 | GenericValue lle_X_sscanf(const FunctionType *FT, |
| 475 | const std::vector<GenericValue> &args) { |
Chris Lattner | 665ee88 | 2002-03-08 22:51:07 +0000 | [diff] [blame] | 476 | assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!"); |
| 477 | |
Chris Lattner | f9a88b6 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 478 | char *Args[10]; |
Chris Lattner | 665ee88 | 2002-03-08 22:51:07 +0000 | [diff] [blame] | 479 | for (unsigned i = 0; i < args.size(); ++i) |
Chris Lattner | f9a88b6 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 480 | Args[i] = (char*)GVTOP(args[i]); |
Chris Lattner | 665ee88 | 2002-03-08 22:51:07 +0000 | [diff] [blame] | 481 | |
| 482 | GenericValue GV; |
Reid Spencer | bfcd599 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 483 | GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4], |
| 484 | Args[5], Args[6], Args[7], Args[8], Args[9])); |
Chris Lattner | f9a88b6 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 485 | ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4], |
| 486 | Args[5], Args[6], Args[7], Args[8], Args[9], 0); |
| 487 | return GV; |
| 488 | } |
| 489 | |
| 490 | // int scanf(const char *format, ...); |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 491 | GenericValue lle_X_scanf(const FunctionType *FT, |
| 492 | const std::vector<GenericValue> &args) { |
Chris Lattner | f9a88b6 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 493 | assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!"); |
| 494 | |
| 495 | char *Args[10]; |
| 496 | for (unsigned i = 0; i < args.size(); ++i) |
| 497 | Args[i] = (char*)GVTOP(args[i]); |
| 498 | |
| 499 | GenericValue GV; |
Reid Spencer | bfcd599 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 500 | GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4], |
| 501 | Args[5], Args[6], Args[7], Args[8], Args[9])); |
Chris Lattner | f9a88b6 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 502 | ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4], |
| 503 | Args[5], Args[6], Args[7], Args[8], Args[9]); |
Chris Lattner | 665ee88 | 2002-03-08 22:51:07 +0000 | [diff] [blame] | 504 | return GV; |
| 505 | } |
| 506 | |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 507 | // int fprintf(FILE *, const char *, ...) - a very rough implementation to make |
| 508 | // output useful. |
| 509 | GenericValue lle_X_fprintf(const FunctionType *FT, |
| 510 | const std::vector<GenericValue> &Args) { |
Chris Lattner | 9dbf6dd | 2003-04-21 22:43:20 +0000 | [diff] [blame] | 511 | assert(Args.size() >= 2); |
Chris Lattner | cf9b4f0 | 2002-11-06 23:05:03 +0000 | [diff] [blame] | 512 | char Buffer[10000]; |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 513 | std::vector<GenericValue> NewArgs; |
Chris Lattner | b111874 | 2003-01-13 00:59:47 +0000 | [diff] [blame] | 514 | NewArgs.push_back(PTOGV(Buffer)); |
Chris Lattner | cf9b4f0 | 2002-11-06 23:05:03 +0000 | [diff] [blame] | 515 | NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end()); |
Reid Spencer | 97e0c22 | 2007-03-30 16:41:50 +0000 | [diff] [blame] | 516 | GenericValue GV = lle_X_sprintf(FT, NewArgs); |
Chris Lattner | cf9b4f0 | 2002-11-06 23:05:03 +0000 | [diff] [blame] | 517 | |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 518 | fputs(Buffer, (FILE *) GVTOP(Args[0])); |
Chris Lattner | cf9b4f0 | 2002-11-06 23:05:03 +0000 | [diff] [blame] | 519 | return GV; |
| 520 | } |
| 521 | |
Chris Lattner | 7720c8e | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 522 | } // End extern "C" |
Chris Lattner | 4721f13 | 2001-10-30 20:28:00 +0000 | [diff] [blame] | 523 | |
| 524 | |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 525 | void Interpreter::initializeExternalFunctions() { |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 526 | FuncNames["lle_X_atexit"] = lle_X_atexit; |
Chris Lattner | 0f279b2 | 2001-11-03 10:15:32 +0000 | [diff] [blame] | 527 | FuncNames["lle_X_exit"] = lle_X_exit; |
Chris Lattner | 1ee34a5 | 2002-05-20 21:17:16 +0000 | [diff] [blame] | 528 | FuncNames["lle_X_abort"] = lle_X_abort; |
Nick Lewycky | f514e2d | 2009-01-20 00:51:40 +0000 | [diff] [blame^] | 529 | |
Chris Lattner | 0f279b2 | 2001-11-03 10:15:32 +0000 | [diff] [blame] | 530 | FuncNames["lle_X_printf"] = lle_X_printf; |
Chris Lattner | e7c6f72 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 531 | FuncNames["lle_X_sprintf"] = lle_X_sprintf; |
Chris Lattner | 665ee88 | 2002-03-08 22:51:07 +0000 | [diff] [blame] | 532 | FuncNames["lle_X_sscanf"] = lle_X_sscanf; |
Chris Lattner | f9a88b6 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 533 | FuncNames["lle_X_scanf"] = lle_X_scanf; |
Chris Lattner | cf9b4f0 | 2002-11-06 23:05:03 +0000 | [diff] [blame] | 534 | FuncNames["lle_X_fprintf"] = lle_X_fprintf; |
Chris Lattner | 4721f13 | 2001-10-30 20:28:00 +0000 | [diff] [blame] | 535 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 536 | |