Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1 | //===-- ExternalFunctions.cpp - Implement External Functions --------------===// |
Misha Brukman | 91fb9ab | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 91fb9ab | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | 91fb9ab | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 62b7fd1 | 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 | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 12 | // |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +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 | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "Interpreter.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/APInt.h" |
| 24 | #include "llvm/ADT/ArrayRef.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 25 | #include "llvm/Config/config.h" // Detect libffi |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 26 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/DataLayout.h" |
| 28 | #include "llvm/IR/DerivedTypes.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Function.h" |
| 30 | #include "llvm/IR/Type.h" |
| 31 | #include "llvm/Support/Casting.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 32 | #include "llvm/Support/DynamicLibrary.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/Support/ErrorHandling.h" |
Chuck Rose III | 1a39a2d1 | 2007-07-27 18:26:35 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ManagedStatic.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Mutex.h" |
Dylan Noblesmith | c4c5180 | 2014-08-23 23:07:14 +0000 | [diff] [blame] | 36 | #include "llvm/Support/UniqueLock.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 38 | #include <cassert> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 39 | #include <cmath> |
Brian Gaeke | 4e106f0 | 2003-11-05 01:18:49 +0000 | [diff] [blame] | 40 | #include <csignal> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 41 | #include <cstdint> |
Duncan Sands | 26ff6f9 | 2008-10-08 07:23:46 +0000 | [diff] [blame] | 42 | #include <cstdio> |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 43 | #include <cstring> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 44 | #include <map> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 45 | #include <string> |
| 46 | #include <utility> |
| 47 | #include <vector> |
Zhou Sheng | 3115ba3 | 2007-12-12 06:16:47 +0000 | [diff] [blame] | 48 | |
Nick Lewycky | e54da99 | 2009-04-13 04:26:06 +0000 | [diff] [blame] | 49 | #ifdef HAVE_FFI_CALL |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 50 | #ifdef HAVE_FFI_H |
| 51 | #include <ffi.h> |
Nick Lewycky | e54da99 | 2009-04-13 04:26:06 +0000 | [diff] [blame] | 52 | #define USE_LIBFFI |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 53 | #elif HAVE_FFI_FFI_H |
| 54 | #include <ffi/ffi.h> |
Nick Lewycky | e54da99 | 2009-04-13 04:26:06 +0000 | [diff] [blame] | 55 | #define USE_LIBFFI |
Zhou Sheng | 3115ba3 | 2007-12-12 06:16:47 +0000 | [diff] [blame] | 56 | #endif |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 57 | #endif |
Tanya Lattner | 9a8602c | 2009-01-22 20:09:20 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 1809966 | 2003-12-14 23:25:48 +0000 | [diff] [blame] | 59 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 60 | |
Owen Anderson | da19a13 | 2009-06-22 22:30:56 +0000 | [diff] [blame] | 61 | static ManagedStatic<sys::Mutex> FunctionsLock; |
| 62 | |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 63 | typedef GenericValue (*ExFunc)(FunctionType *, ArrayRef<GenericValue>); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 64 | static ManagedStatic<std::map<const Function *, ExFunc> > ExportedFunctions; |
Chris Bieneman | 3b1fd57 | 2014-09-19 21:07:01 +0000 | [diff] [blame] | 65 | static ManagedStatic<std::map<std::string, ExFunc> > FuncNames; |
Chris Lattner | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 66 | |
Nick Lewycky | e54da99 | 2009-04-13 04:26:06 +0000 | [diff] [blame] | 67 | #ifdef USE_LIBFFI |
Dan Gohman | 1432ef8 | 2009-08-12 22:10:57 +0000 | [diff] [blame] | 68 | typedef void (*RawFunc)(); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 69 | static ManagedStatic<std::map<const Function *, RawFunc> > RawFunctions; |
| 70 | #endif |
| 71 | |
Chris Lattner | 15157b8 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 72 | static Interpreter *TheInterpreter; |
| 73 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 74 | static char getTypeID(Type *Ty) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 75 | switch (Ty->getTypeID()) { |
Chris Lattner | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 76 | case Type::VoidTyID: return 'V'; |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 77 | case Type::IntegerTyID: |
| 78 | switch (cast<IntegerType>(Ty)->getBitWidth()) { |
| 79 | case 1: return 'o'; |
| 80 | case 8: return 'B'; |
| 81 | case 16: return 'S'; |
| 82 | case 32: return 'I'; |
| 83 | case 64: return 'L'; |
| 84 | default: return 'N'; |
| 85 | } |
Chris Lattner | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 86 | case Type::FloatTyID: return 'F'; |
| 87 | case Type::DoubleTyID: return 'D'; |
| 88 | case Type::PointerTyID: return 'P'; |
Reid Spencer | 0d54e78 | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 89 | case Type::FunctionTyID:return 'M'; |
Chris Lattner | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 90 | case Type::StructTyID: return 'T'; |
| 91 | case Type::ArrayTyID: return 'A'; |
Chris Lattner | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 92 | default: return 'U'; |
| 93 | } |
| 94 | } |
| 95 | |
Anton Korobeynikov | 187bf73 | 2007-07-30 23:03:25 +0000 | [diff] [blame] | 96 | // Try to find address of external function given a Function object. |
| 97 | // Please note, that interpreter doesn't know how to assemble a |
| 98 | // real call in general case (this is JIT job), that's why it assumes, |
| 99 | // that all external functions has the same (and pretty "general") signature. |
| 100 | // The typical example of such functions are "lle_X_" ones. |
Brian Gaeke | 6cc20de | 2003-10-10 17:03:10 +0000 | [diff] [blame] | 101 | static ExFunc lookupFunction(const Function *F) { |
Chris Lattner | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 102 | // Function not found, look it up... start by figuring out what the |
| 103 | // composite function name should be. |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 104 | std::string ExtName = "lle_"; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 105 | FunctionType *FT = F->getFunctionType(); |
Brian Gaeke | 6cc20de | 2003-10-10 17:03:10 +0000 | [diff] [blame] | 106 | for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i) |
| 107 | ExtName += getTypeID(FT->getContainedType(i)); |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 108 | ExtName += ("_" + F->getName()).str(); |
Chris Lattner | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 109 | |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 110 | sys::ScopedLock Writer(*FunctionsLock); |
Chris Bieneman | 3b1fd57 | 2014-09-19 21:07:01 +0000 | [diff] [blame] | 111 | ExFunc FnPtr = (*FuncNames)[ExtName]; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 112 | if (!FnPtr) |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 113 | FnPtr = (*FuncNames)[("lle_X_" + F->getName()).str()]; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 114 | if (!FnPtr) // Try calling a generic function... if it exists... |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 115 | FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol( |
| 116 | ("lle_X_" + F->getName()).str()); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 117 | if (FnPtr) |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 118 | ExportedFunctions->insert(std::make_pair(F, FnPtr)); // Cache for later |
Chris Lattner | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 119 | return FnPtr; |
| 120 | } |
| 121 | |
Nick Lewycky | e54da99 | 2009-04-13 04:26:06 +0000 | [diff] [blame] | 122 | #ifdef USE_LIBFFI |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 123 | static ffi_type *ffiTypeFor(Type *Ty) { |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 124 | switch (Ty->getTypeID()) { |
| 125 | case Type::VoidTyID: return &ffi_type_void; |
| 126 | case Type::IntegerTyID: |
| 127 | switch (cast<IntegerType>(Ty)->getBitWidth()) { |
| 128 | case 8: return &ffi_type_sint8; |
| 129 | case 16: return &ffi_type_sint16; |
| 130 | case 32: return &ffi_type_sint32; |
| 131 | case 64: return &ffi_type_sint64; |
| 132 | } |
| 133 | case Type::FloatTyID: return &ffi_type_float; |
| 134 | case Type::DoubleTyID: return &ffi_type_double; |
| 135 | case Type::PointerTyID: return &ffi_type_pointer; |
| 136 | default: break; |
| 137 | } |
| 138 | // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc. |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 139 | report_fatal_error("Type could not be mapped for use with libffi."); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 140 | return NULL; |
| 141 | } |
| 142 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 143 | static void *ffiValueFor(Type *Ty, const GenericValue &AV, |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 144 | void *ArgDataPtr) { |
| 145 | switch (Ty->getTypeID()) { |
| 146 | case Type::IntegerTyID: |
| 147 | switch (cast<IntegerType>(Ty)->getBitWidth()) { |
| 148 | case 8: { |
| 149 | int8_t *I8Ptr = (int8_t *) ArgDataPtr; |
| 150 | *I8Ptr = (int8_t) AV.IntVal.getZExtValue(); |
| 151 | return ArgDataPtr; |
| 152 | } |
| 153 | case 16: { |
| 154 | int16_t *I16Ptr = (int16_t *) ArgDataPtr; |
| 155 | *I16Ptr = (int16_t) AV.IntVal.getZExtValue(); |
| 156 | return ArgDataPtr; |
| 157 | } |
| 158 | case 32: { |
| 159 | int32_t *I32Ptr = (int32_t *) ArgDataPtr; |
| 160 | *I32Ptr = (int32_t) AV.IntVal.getZExtValue(); |
| 161 | return ArgDataPtr; |
| 162 | } |
| 163 | case 64: { |
| 164 | int64_t *I64Ptr = (int64_t *) ArgDataPtr; |
| 165 | *I64Ptr = (int64_t) AV.IntVal.getZExtValue(); |
| 166 | return ArgDataPtr; |
| 167 | } |
| 168 | } |
| 169 | case Type::FloatTyID: { |
| 170 | float *FloatPtr = (float *) ArgDataPtr; |
Nick Lewycky | 41ee792 | 2009-11-18 05:43:15 +0000 | [diff] [blame] | 171 | *FloatPtr = AV.FloatVal; |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 172 | return ArgDataPtr; |
| 173 | } |
| 174 | case Type::DoubleTyID: { |
| 175 | double *DoublePtr = (double *) ArgDataPtr; |
| 176 | *DoublePtr = AV.DoubleVal; |
| 177 | return ArgDataPtr; |
| 178 | } |
| 179 | case Type::PointerTyID: { |
| 180 | void **PtrPtr = (void **) ArgDataPtr; |
| 181 | *PtrPtr = GVTOP(AV); |
| 182 | return ArgDataPtr; |
| 183 | } |
| 184 | default: break; |
| 185 | } |
| 186 | // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc. |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 187 | report_fatal_error("Type value could not be mapped for use with libffi."); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 188 | return NULL; |
| 189 | } |
| 190 | |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 191 | static bool ffiInvoke(RawFunc Fn, Function *F, ArrayRef<GenericValue> ArgVals, |
Mehdi Amini | a3fcefb | 2015-07-16 16:34:23 +0000 | [diff] [blame] | 192 | const DataLayout &TD, GenericValue &Result) { |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 193 | ffi_cif cif; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 194 | FunctionType *FTy = F->getFunctionType(); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 195 | const unsigned NumArgs = F->arg_size(); |
| 196 | |
| 197 | // TODO: We don't have type information about the remaining arguments, because |
| 198 | // this information is never passed into ExecutionEngine::runFunction(). |
| 199 | if (ArgVals.size() > NumArgs && F->isVarArg()) { |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 200 | report_fatal_error("Calling external var arg function '" + F->getName() |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 201 | + "' is not supported by the Interpreter."); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | unsigned ArgBytes = 0; |
| 205 | |
| 206 | std::vector<ffi_type*> args(NumArgs); |
| 207 | for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end(); |
| 208 | A != E; ++A) { |
| 209 | const unsigned ArgNo = A->getArgNo(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 210 | Type *ArgTy = FTy->getParamType(ArgNo); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 211 | args[ArgNo] = ffiTypeFor(ArgTy); |
Mehdi Amini | 731ad62 | 2015-07-16 22:23:09 +0000 | [diff] [blame] | 212 | ArgBytes += TD.getTypeStoreSize(ArgTy); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Nick Lewycky | bf4c56d | 2009-09-18 16:46:16 +0000 | [diff] [blame] | 215 | SmallVector<uint8_t, 128> ArgData; |
| 216 | ArgData.resize(ArgBytes); |
| 217 | uint8_t *ArgDataPtr = ArgData.data(); |
| 218 | SmallVector<void*, 16> values(NumArgs); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 219 | for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end(); |
| 220 | A != E; ++A) { |
| 221 | const unsigned ArgNo = A->getArgNo(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 222 | Type *ArgTy = FTy->getParamType(ArgNo); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 223 | values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr); |
Mehdi Amini | 731ad62 | 2015-07-16 22:23:09 +0000 | [diff] [blame] | 224 | ArgDataPtr += TD.getTypeStoreSize(ArgTy); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 227 | Type *RetTy = FTy->getReturnType(); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 228 | ffi_type *rtype = ffiTypeFor(RetTy); |
| 229 | |
| 230 | if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, &args[0]) == FFI_OK) { |
Nick Lewycky | bf4c56d | 2009-09-18 16:46:16 +0000 | [diff] [blame] | 231 | SmallVector<uint8_t, 128> ret; |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 232 | if (RetTy->getTypeID() != Type::VoidTyID) |
Mehdi Amini | 731ad62 | 2015-07-16 22:23:09 +0000 | [diff] [blame] | 233 | ret.resize(TD.getTypeStoreSize(RetTy)); |
Nick Lewycky | bf4c56d | 2009-09-18 16:46:16 +0000 | [diff] [blame] | 234 | ffi_call(&cif, Fn, ret.data(), values.data()); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 235 | switch (RetTy->getTypeID()) { |
| 236 | case Type::IntegerTyID: |
| 237 | switch (cast<IntegerType>(RetTy)->getBitWidth()) { |
Nick Lewycky | bf4c56d | 2009-09-18 16:46:16 +0000 | [diff] [blame] | 238 | case 8: Result.IntVal = APInt(8 , *(int8_t *) ret.data()); break; |
| 239 | case 16: Result.IntVal = APInt(16, *(int16_t*) ret.data()); break; |
| 240 | case 32: Result.IntVal = APInt(32, *(int32_t*) ret.data()); break; |
| 241 | case 64: Result.IntVal = APInt(64, *(int64_t*) ret.data()); break; |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 242 | } |
| 243 | break; |
Nick Lewycky | bf4c56d | 2009-09-18 16:46:16 +0000 | [diff] [blame] | 244 | case Type::FloatTyID: Result.FloatVal = *(float *) ret.data(); break; |
| 245 | case Type::DoubleTyID: Result.DoubleVal = *(double*) ret.data(); break; |
| 246 | case Type::PointerTyID: Result.PointerVal = *(void **) ret.data(); break; |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 247 | default: break; |
| 248 | } |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | return false; |
| 253 | } |
Nick Lewycky | e54da99 | 2009-04-13 04:26:06 +0000 | [diff] [blame] | 254 | #endif // USE_LIBFFI |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 28edd69 | 2005-01-21 19:59:37 +0000 | [diff] [blame] | 256 | GenericValue Interpreter::callExternalFunction(Function *F, |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 257 | ArrayRef<GenericValue> ArgVals) { |
Chris Lattner | 15157b8 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 258 | TheInterpreter = this; |
| 259 | |
Dylan Noblesmith | c4c5180 | 2014-08-23 23:07:14 +0000 | [diff] [blame] | 260 | unique_lock<sys::Mutex> Guard(*FunctionsLock); |
Owen Anderson | da19a13 | 2009-06-22 22:30:56 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 262 | // Do a lookup to see if the function is in our cache... this should just be a |
Misha Brukman | aa7d26c | 2003-10-10 17:42:19 +0000 | [diff] [blame] | 263 | // deferred annotation! |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 264 | std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F); |
| 265 | if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F) |
Owen Anderson | da19a13 | 2009-06-22 22:30:56 +0000 | [diff] [blame] | 266 | : FI->second) { |
Dylan Noblesmith | c4c5180 | 2014-08-23 23:07:14 +0000 | [diff] [blame] | 267 | Guard.unlock(); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 268 | return Fn(F->getFunctionType(), ArgVals); |
Owen Anderson | da19a13 | 2009-06-22 22:30:56 +0000 | [diff] [blame] | 269 | } |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 270 | |
Nick Lewycky | e54da99 | 2009-04-13 04:26:06 +0000 | [diff] [blame] | 271 | #ifdef USE_LIBFFI |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 272 | std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F); |
| 273 | RawFunc RawFn; |
| 274 | if (RF == RawFunctions->end()) { |
| 275 | RawFn = (RawFunc)(intptr_t) |
| 276 | sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName()); |
Torok Edwin | 921e80b | 2010-03-30 20:15:13 +0000 | [diff] [blame] | 277 | if (!RawFn) |
Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 278 | RawFn = (RawFunc)(intptr_t)getPointerToGlobalIfAvailable(F); |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 279 | if (RawFn != 0) |
| 280 | RawFunctions->insert(std::make_pair(F, RawFn)); // Cache for later |
| 281 | } else { |
| 282 | RawFn = RF->second; |
Chris Lattner | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 283 | } |
Daniel Dunbar | c7012fa | 2009-09-17 00:14:44 +0000 | [diff] [blame] | 284 | |
Dylan Noblesmith | c4c5180 | 2014-08-23 23:07:14 +0000 | [diff] [blame] | 285 | Guard.unlock(); |
Chris Lattner | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 286 | |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 287 | GenericValue Result; |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 288 | if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals, getDataLayout(), Result)) |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 289 | return Result; |
Nick Lewycky | e54da99 | 2009-04-13 04:26:06 +0000 | [diff] [blame] | 290 | #endif // USE_LIBFFI |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 291 | |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 292 | if (F->getName() == "__main") |
David Greene | 1df14c4 | 2010-01-05 01:53:59 +0000 | [diff] [blame] | 293 | errs() << "Tried to execute an unknown external function: " |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 294 | << *F->getType() << " __main\n"; |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 295 | else |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 296 | report_fatal_error("Tried to execute an unknown external function: " + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 297 | F->getName()); |
Nick Lewycky | 7efd07f | 2009-11-17 07:52:09 +0000 | [diff] [blame] | 298 | #ifndef USE_LIBFFI |
David Greene | 1df14c4 | 2010-01-05 01:53:59 +0000 | [diff] [blame] | 299 | errs() << "Recompiling LLVM with --enable-libffi might help.\n"; |
Nick Lewycky | 7efd07f | 2009-11-17 07:52:09 +0000 | [diff] [blame] | 300 | #endif |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 301 | return GenericValue(); |
Chris Lattner | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Chris Lattner | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 304 | //===----------------------------------------------------------------------===// |
Chris Lattner | f94811a | 2002-03-29 03:57:15 +0000 | [diff] [blame] | 305 | // Functions "exported" to the running application... |
Chris Lattner | baf08eb | 2001-09-10 04:50:17 +0000 | [diff] [blame] | 306 | // |
Daniel Dunbar | 86ec7ba | 2009-08-07 20:50:09 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 4a5bb95 | 2003-05-14 14:21:30 +0000 | [diff] [blame] | 308 | // void atexit(Function*) |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 309 | static GenericValue lle_X_atexit(FunctionType *FT, |
| 310 | ArrayRef<GenericValue> Args) { |
Chris Lattner | 4a5bb95 | 2003-05-14 14:21:30 +0000 | [diff] [blame] | 311 | assert(Args.size() == 1); |
| 312 | TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0])); |
| 313 | GenericValue GV; |
Reid Spencer | c45e104 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 314 | GV.IntVal = 0; |
Chris Lattner | 4a5bb95 | 2003-05-14 14:21:30 +0000 | [diff] [blame] | 315 | return GV; |
Chris Lattner | d299dba | 2001-10-18 21:55:32 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Chris Lattner | 0313db6 | 2002-10-02 21:12:13 +0000 | [diff] [blame] | 318 | // void exit(int) |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 319 | static GenericValue lle_X_exit(FunctionType *FT, ArrayRef<GenericValue> Args) { |
Chris Lattner | 15157b8 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 320 | TheInterpreter->exitCalled(Args[0]); |
| 321 | return GenericValue(); |
| 322 | } |
| 323 | |
Chris Lattner | 0313db6 | 2002-10-02 21:12:13 +0000 | [diff] [blame] | 324 | // void abort(void) |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 325 | static GenericValue lle_X_abort(FunctionType *FT, ArrayRef<GenericValue> Args) { |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 326 | //FIXME: should we report or raise here? |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 327 | //report_fatal_error("Interpreted program raised SIGABRT"); |
Brian Gaeke | 4e106f0 | 2003-11-05 01:18:49 +0000 | [diff] [blame] | 328 | raise (SIGABRT); |
Chris Lattner | 1319429 | 2002-05-20 21:17:16 +0000 | [diff] [blame] | 329 | return GenericValue(); |
| 330 | } |
| 331 | |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 332 | // int sprintf(char *, const char *, ...) - a very rough implementation to make |
Chris Lattner | 60a9a23 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 333 | // output useful. |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 334 | static GenericValue lle_X_sprintf(FunctionType *FT, |
| 335 | ArrayRef<GenericValue> Args) { |
Chris Lattner | d49518c | 2003-01-13 00:59:47 +0000 | [diff] [blame] | 336 | char *OutputBuffer = (char *)GVTOP(Args[0]); |
| 337 | const char *FmtStr = (const char *)GVTOP(Args[1]); |
Chris Lattner | 60a9a23 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 338 | unsigned ArgNo = 2; |
Chris Lattner | 490d2a8 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 339 | |
| 340 | // printf should return # chars printed. This is completely incorrect, but |
| 341 | // close enough for now. |
Daniel Dunbar | c7012fa | 2009-09-17 00:14:44 +0000 | [diff] [blame] | 342 | GenericValue GV; |
Reid Spencer | c45e104 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 343 | GV.IntVal = APInt(32, strlen(FmtStr)); |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 344 | while (true) { |
Chris Lattner | 490d2a8 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 345 | switch (*FmtStr) { |
| 346 | case 0: return GV; // Null terminator... |
| 347 | default: // Normal nonspecial character |
Chris Lattner | 60a9a23 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 348 | sprintf(OutputBuffer++, "%c", *FmtStr++); |
Chris Lattner | 490d2a8 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 349 | break; |
| 350 | case '\\': { // Handle escape codes |
Chris Lattner | 60a9a23 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 351 | sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1)); |
| 352 | FmtStr += 2; OutputBuffer += 2; |
Chris Lattner | 490d2a8 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 353 | break; |
| 354 | } |
| 355 | case '%': { // Handle format specifiers |
Chris Lattner | 31e9e4d | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 356 | char FmtBuf[100] = "", Buffer[1000] = ""; |
| 357 | char *FB = FmtBuf; |
| 358 | *FB++ = *FmtStr++; |
| 359 | char Last = *FB++ = *FmtStr++; |
| 360 | unsigned HowLong = 0; |
| 361 | while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' && |
| 362 | Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' && |
| 363 | Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' && |
| 364 | Last != 'p' && Last != 's' && Last != '%') { |
| 365 | if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's |
| 366 | Last = *FB++ = *FmtStr++; |
Chris Lattner | 490d2a8 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | 31e9e4d | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 368 | *FB = 0; |
Misha Brukman | 91fb9ab | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 31e9e4d | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 370 | switch (Last) { |
| 371 | case '%': |
Benjamin Kramer | 29063ea | 2010-01-28 18:04:38 +0000 | [diff] [blame] | 372 | memcpy(Buffer, "%", 2); break; |
Chris Lattner | 31e9e4d | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 373 | case 'c': |
Reid Spencer | c45e104 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 374 | sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue())); |
| 375 | break; |
Chris Lattner | 31e9e4d | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 376 | case 'd': case 'i': |
| 377 | case 'u': case 'o': |
| 378 | case 'x': case 'X': |
Chris Lattner | 33b3b96 | 2002-08-02 23:08:32 +0000 | [diff] [blame] | 379 | if (HowLong >= 1) { |
Chris Lattner | 4798540 | 2003-08-24 14:02:47 +0000 | [diff] [blame] | 380 | if (HowLong == 1 && |
Mehdi Amini | a3fcefb | 2015-07-16 16:34:23 +0000 | [diff] [blame] | 381 | TheInterpreter->getDataLayout().getPointerSizeInBits() == 64 && |
Reid Spencer | 6e64180 | 2006-05-24 19:21:13 +0000 | [diff] [blame] | 382 | sizeof(long) < sizeof(int64_t)) { |
Chris Lattner | 33b3b96 | 2002-08-02 23:08:32 +0000 | [diff] [blame] | 383 | // Make sure we use %lld with a 64 bit argument because we might be |
| 384 | // compiling LLI on a 32 bit compiler. |
| 385 | unsigned Size = strlen(FmtBuf); |
| 386 | FmtBuf[Size] = FmtBuf[Size-1]; |
| 387 | FmtBuf[Size+1] = 0; |
| 388 | FmtBuf[Size-1] = 'l'; |
| 389 | } |
Reid Spencer | c45e104 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 390 | sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue()); |
Chris Lattner | 33b3b96 | 2002-08-02 23:08:32 +0000 | [diff] [blame] | 391 | } else |
Reid Spencer | c45e104 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 392 | sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue())); |
| 393 | break; |
Chris Lattner | 31e9e4d | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 394 | case 'e': case 'E': case 'g': case 'G': case 'f': |
| 395 | sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break; |
| 396 | case 'p': |
Chris Lattner | d49518c | 2003-01-13 00:59:47 +0000 | [diff] [blame] | 397 | sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break; |
Misha Brukman | 91fb9ab | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 398 | case 's': |
Chris Lattner | d49518c | 2003-01-13 00:59:47 +0000 | [diff] [blame] | 399 | sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break; |
Chris Lattner | 471ba48 | 2009-08-23 08:43:55 +0000 | [diff] [blame] | 400 | default: |
David Greene | 1df14c4 | 2010-01-05 01:53:59 +0000 | [diff] [blame] | 401 | errs() << "<unknown printf code '" << *FmtStr << "'!>"; |
Chris Lattner | 31e9e4d | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 402 | ArgNo++; break; |
Chris Lattner | 490d2a8 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 403 | } |
Benjamin Kramer | 29063ea | 2010-01-28 18:04:38 +0000 | [diff] [blame] | 404 | size_t Len = strlen(Buffer); |
| 405 | memcpy(OutputBuffer, Buffer, Len + 1); |
| 406 | OutputBuffer += Len; |
Chris Lattner | 31e9e4d | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 407 | } |
Chris Lattner | 490d2a8 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 408 | break; |
| 409 | } |
Chris Lattner | 490d2a8 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 410 | } |
Elena Demikhovsky | 843657c | 2013-09-02 06:40:09 +0000 | [diff] [blame] | 411 | return GV; |
Chris Lattner | 490d2a8 | 2001-10-29 20:27:45 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 414 | // int printf(const char *, ...) - a very rough implementation to make output |
| 415 | // useful. |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 416 | static GenericValue lle_X_printf(FunctionType *FT, |
| 417 | ArrayRef<GenericValue> Args) { |
Chris Lattner | 60a9a23 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 418 | char Buffer[10000]; |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 419 | std::vector<GenericValue> NewArgs; |
Reid Spencer | a68c374 | 2007-04-21 17:11:45 +0000 | [diff] [blame] | 420 | NewArgs.push_back(PTOGV((void*)&Buffer[0])); |
Chris Lattner | 60a9a23 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 421 | NewArgs.insert(NewArgs.end(), Args.begin(), Args.end()); |
Reid Spencer | 8c3d3dc | 2007-03-30 16:41:50 +0000 | [diff] [blame] | 422 | GenericValue GV = lle_X_sprintf(FT, NewArgs); |
Chris Lattner | 471ba48 | 2009-08-23 08:43:55 +0000 | [diff] [blame] | 423 | outs() << Buffer; |
Chris Lattner | 60a9a23 | 2001-12-13 00:43:47 +0000 | [diff] [blame] | 424 | return GV; |
| 425 | } |
| 426 | |
Chris Lattner | 5dec460 | 2002-03-08 22:51:07 +0000 | [diff] [blame] | 427 | // int sscanf(const char *format, ...); |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 428 | static GenericValue lle_X_sscanf(FunctionType *FT, |
| 429 | ArrayRef<GenericValue> args) { |
Chris Lattner | 5dec460 | 2002-03-08 22:51:07 +0000 | [diff] [blame] | 430 | assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!"); |
| 431 | |
Chris Lattner | d318371 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 432 | char *Args[10]; |
Chris Lattner | 5dec460 | 2002-03-08 22:51:07 +0000 | [diff] [blame] | 433 | for (unsigned i = 0; i < args.size(); ++i) |
Chris Lattner | d318371 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 434 | Args[i] = (char*)GVTOP(args[i]); |
Chris Lattner | 5dec460 | 2002-03-08 22:51:07 +0000 | [diff] [blame] | 435 | |
| 436 | GenericValue GV; |
Reid Spencer | c45e104 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 437 | GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4], |
Elena Demikhovsky | 843657c | 2013-09-02 06:40:09 +0000 | [diff] [blame] | 438 | Args[5], Args[6], Args[7], Args[8], Args[9])); |
Chris Lattner | d318371 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 439 | return GV; |
| 440 | } |
| 441 | |
| 442 | // int scanf(const char *format, ...); |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 443 | static GenericValue lle_X_scanf(FunctionType *FT, ArrayRef<GenericValue> args) { |
Chris Lattner | d318371 | 2003-03-31 22:12:37 +0000 | [diff] [blame] | 444 | assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!"); |
| 445 | |
| 446 | char *Args[10]; |
| 447 | for (unsigned i = 0; i < args.size(); ++i) |
| 448 | Args[i] = (char*)GVTOP(args[i]); |
| 449 | |
| 450 | GenericValue GV; |
Reid Spencer | c45e104 | 2007-03-06 03:08:12 +0000 | [diff] [blame] | 451 | GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4], |
Elena Demikhovsky | 843657c | 2013-09-02 06:40:09 +0000 | [diff] [blame] | 452 | Args[5], Args[6], Args[7], Args[8], Args[9])); |
Chris Lattner | 5dec460 | 2002-03-08 22:51:07 +0000 | [diff] [blame] | 453 | return GV; |
| 454 | } |
| 455 | |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 456 | // int fprintf(FILE *, const char *, ...) - a very rough implementation to make |
| 457 | // output useful. |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 458 | static GenericValue lle_X_fprintf(FunctionType *FT, |
| 459 | ArrayRef<GenericValue> Args) { |
Chris Lattner | 1610666 | 2003-04-21 22:43:20 +0000 | [diff] [blame] | 460 | assert(Args.size() >= 2); |
Chris Lattner | c3a8409 | 2002-11-06 23:05:03 +0000 | [diff] [blame] | 461 | char Buffer[10000]; |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 462 | std::vector<GenericValue> NewArgs; |
Chris Lattner | d49518c | 2003-01-13 00:59:47 +0000 | [diff] [blame] | 463 | NewArgs.push_back(PTOGV(Buffer)); |
Chris Lattner | c3a8409 | 2002-11-06 23:05:03 +0000 | [diff] [blame] | 464 | NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end()); |
Reid Spencer | 8c3d3dc | 2007-03-30 16:41:50 +0000 | [diff] [blame] | 465 | GenericValue GV = lle_X_sprintf(FT, NewArgs); |
Chris Lattner | c3a8409 | 2002-11-06 23:05:03 +0000 | [diff] [blame] | 466 | |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 467 | fputs(Buffer, (FILE *) GVTOP(Args[0])); |
Chris Lattner | c3a8409 | 2002-11-06 23:05:03 +0000 | [diff] [blame] | 468 | return GV; |
| 469 | } |
| 470 | |
Benjamin Kramer | fcb7166 | 2013-09-11 12:42:39 +0000 | [diff] [blame] | 471 | static GenericValue lle_X_memset(FunctionType *FT, |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 472 | ArrayRef<GenericValue> Args) { |
Elena Demikhovsky | 843657c | 2013-09-02 06:40:09 +0000 | [diff] [blame] | 473 | int val = (int)Args[1].IntVal.getSExtValue(); |
| 474 | size_t len = (size_t)Args[2].IntVal.getZExtValue(); |
Benjamin Kramer | fcb7166 | 2013-09-11 12:42:39 +0000 | [diff] [blame] | 475 | memset((void *)GVTOP(Args[0]), val, len); |
Elena Demikhovsky | 843657c | 2013-09-02 06:40:09 +0000 | [diff] [blame] | 476 | // llvm.memset.* returns void, lle_X_* returns GenericValue, |
| 477 | // so here we return GenericValue with IntVal set to zero |
| 478 | GenericValue GV; |
| 479 | GV.IntVal = 0; |
| 480 | return GV; |
| 481 | } |
| 482 | |
Benjamin Kramer | fcb7166 | 2013-09-11 12:42:39 +0000 | [diff] [blame] | 483 | static GenericValue lle_X_memcpy(FunctionType *FT, |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 484 | ArrayRef<GenericValue> Args) { |
Elena Demikhovsky | 843657c | 2013-09-02 06:40:09 +0000 | [diff] [blame] | 485 | memcpy(GVTOP(Args[0]), GVTOP(Args[1]), |
| 486 | (size_t)(Args[2].IntVal.getLimitedValue())); |
| 487 | |
Benjamin Kramer | fcb7166 | 2013-09-11 12:42:39 +0000 | [diff] [blame] | 488 | // llvm.memcpy* returns void, lle_X_* returns GenericValue, |
Elena Demikhovsky | 843657c | 2013-09-02 06:40:09 +0000 | [diff] [blame] | 489 | // so here we return GenericValue with IntVal set to zero |
| 490 | GenericValue GV; |
| 491 | GV.IntVal = 0; |
| 492 | return GV; |
| 493 | } |
| 494 | |
Chris Lattner | 470754e | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 495 | void Interpreter::initializeExternalFunctions() { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 496 | sys::ScopedLock Writer(*FunctionsLock); |
Chris Bieneman | 3b1fd57 | 2014-09-19 21:07:01 +0000 | [diff] [blame] | 497 | (*FuncNames)["lle_X_atexit"] = lle_X_atexit; |
| 498 | (*FuncNames)["lle_X_exit"] = lle_X_exit; |
| 499 | (*FuncNames)["lle_X_abort"] = lle_X_abort; |
Nick Lewycky | a89ec99 | 2009-02-04 06:26:47 +0000 | [diff] [blame] | 500 | |
Chris Bieneman | 3b1fd57 | 2014-09-19 21:07:01 +0000 | [diff] [blame] | 501 | (*FuncNames)["lle_X_printf"] = lle_X_printf; |
| 502 | (*FuncNames)["lle_X_sprintf"] = lle_X_sprintf; |
| 503 | (*FuncNames)["lle_X_sscanf"] = lle_X_sscanf; |
| 504 | (*FuncNames)["lle_X_scanf"] = lle_X_scanf; |
| 505 | (*FuncNames)["lle_X_fprintf"] = lle_X_fprintf; |
| 506 | (*FuncNames)["lle_X_memset"] = lle_X_memset; |
| 507 | (*FuncNames)["lle_X_memcpy"] = lle_X_memcpy; |
Chris Lattner | 7fd51b5 | 2001-10-30 20:28:00 +0000 | [diff] [blame] | 508 | } |