JIT.cpp revision 66941988de6a295649b33f4c2b0f36a094b2244d
14d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
2f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
9bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner//
104d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner// This tool implements a just-in-time compiler for LLVM, allowing direct
11a99be51bf5cdac1438069d4b01766c47704961c8Gabor Greif// execution of LLVM bitcode in an efficient manner.
12bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner//
13bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner//===----------------------------------------------------------------------===//
14bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
154d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner#include "JIT.h"
16cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner#include "llvm/Constants.h"
17c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner#include "llvm/DerivedTypes.h"
184d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner#include "llvm/Function.h"
19c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner#include "llvm/GlobalVariable.h"
20cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner#include "llvm/Instructions.h"
210f4f7d9eb72dec2a72ec596fb3d611a241893bb7Misha Brukman#include "llvm/ModuleProvider.h"
224d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner#include "llvm/CodeGen/MachineCodeEmitter.h"
239722294d30fff2a432d8e171eb904f33956353e2Brian Gaeke#include "llvm/ExecutionEngine/GenericValue.h"
2407000c6f01d8f57170f2d4c77a86d934bdc5c696Owen Anderson#include "llvm/Target/TargetData.h"
25bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner#include "llvm/Target/TargetMachine.h"
264d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner#include "llvm/Target/TargetJITInfo.h"
2744e3dd1672572df28e79649c86f2d44bf3730062Chris Lattner#include "llvm/Support/Dwarf.h"
2844e3dd1672572df28e79649c86f2d44bf3730062Chris Lattner#include "llvm/Support/MutexGuard.h"
2944e3dd1672572df28e79649c86f2d44bf3730062Chris Lattner#include "llvm/System/DynamicLibrary.h"
303b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov#include "llvm/Config/config.h"
313b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov
32c19aadee66b744311afe6e420847e80822a765f2Chris Lattnerusing namespace llvm;
33abb027cf412944db4d27579ba3ae00717d23c25eMisha Brukman
34b76ea74845849e7a499603bb04bfe1614101e910Nate Begeman#ifdef __APPLE__
353b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov// Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
363b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov// of atexit). It passes the address of linker generated symbol __dso_handle
373b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov// to the function.
383b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov// This configuration change happened at version 5330.
393b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov# include <AvailabilityMacros.h>
403b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov# if defined(MAC_OS_X_VERSION_10_4) && \
413b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov     ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
423b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov      (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
433b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov       __APPLE_CC__ >= 5330))
443b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov#  ifndef HAVE___DSO_HANDLE
453b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov#   define HAVE___DSO_HANDLE 1
463b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov#  endif
473b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov# endif
485f42c550305814f916207b9604b92fbd258e8eafEvan Cheng#endif
493b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov
503b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov#if HAVE___DSO_HANDLE
513b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikovextern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
52b76ea74845849e7a499603bb04bfe1614101e910Nate Begeman#endif
535f42c550305814f916207b9604b92fbd258e8eafEvan Cheng
54844731a7f1909f55935e3514c9e713a62d67662eDan Gohmannamespace {
55844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
562fe4bb06c6c40d16b7a5ae9cdf6bb6fe94d51be0Chris Lattnerstatic struct RegisterJIT {
572fe4bb06c6c40d16b7a5ae9cdf6bb6fe94d51be0Chris Lattner  RegisterJIT() { JIT::Register(); }
582fe4bb06c6c40d16b7a5ae9cdf6bb6fe94d51be0Chris Lattner} JITRegistrator;
592fe4bb06c6c40d16b7a5ae9cdf6bb6fe94d51be0Chris Lattner
60844731a7f1909f55935e3514c9e713a62d67662eDan Gohman}
61844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
622f51914d828b462b054195e73c75448f24e01979Jeff Cohennamespace llvm {
632f51914d828b462b054195e73c75448f24e01979Jeff Cohen  void LinkInJIT() {
642f51914d828b462b054195e73c75448f24e01979Jeff Cohen  }
652f51914d828b462b054195e73c75448f24e01979Jeff Cohen}
662f51914d828b462b054195e73c75448f24e01979Jeff Cohen
67d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
682d5424d76add9714d95239f183f7f88263f44360Evan Cheng#if defined(__GNUC__) && !defined(__ARM__EABI__)
69d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
70d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// libgcc defines the __register_frame function to dynamically register new
71d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// dwarf frames for exception handling. This functionality is not portable
72d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// across compilers and is only provided by GCC. We use the __register_frame
73d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// function here so that code generated by the JIT cooperates with the unwinding
74d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// runtime of libgcc. When JITting with exception handling enable, LLVM
75d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// generates dwarf frames and registers it to libgcc with __register_frame.
76d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray//
77d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// The __register_frame function works with Linux.
78d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray//
79d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// Unfortunately, this functionality seems to be in libgcc after the unwinding
80d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// library of libgcc for darwin was written. The code for darwin overwrites the
81d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// value updated by __register_frame with a value fetched with "keymgr".
82d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// "keymgr" is an obsolete functionality, which should be rewritten some day.
83d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// In the meantime, since "keymgr" is on all libgccs shipped with apple-gcc, we
84d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// need a workaround in LLVM which uses the "keymgr" to dynamically modify the
85d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// values of an opaque key, used by libgcc to find dwarf tables.
86d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
87299d9d74e9b010c499557380fb6467f5361e141aAnton Korobeynikovextern "C" void __register_frame(void*);
88d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
892d5424d76add9714d95239f183f7f88263f44360Evan Cheng#if defined(__APPLE__)
90d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
91d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffraynamespace {
92d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
93d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// LibgccObject - This is the structure defined in libgcc. There is no #include
94d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// provided for this structure, so we also define it here. libgcc calls it
95d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// "struct object". The structure is undocumented in libgcc.
96d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffraystruct LibgccObject {
97d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  void *unused1;
98d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  void *unused2;
99d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  void *unused3;
100d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
101d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  /// frame - Pointer to the exception table.
102d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  void *frame;
103d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
104d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  /// encoding -  The encoding of the object?
105d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  union {
106d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray    struct {
107d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray      unsigned long sorted : 1;
108d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray      unsigned long from_array : 1;
109d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray      unsigned long mixed_encoding : 1;
110d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray      unsigned long encoding : 8;
111d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray      unsigned long count : 21;
112d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray    } b;
113d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray    size_t i;
114d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  } encoding;
115d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
116d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  /// fde_end - libgcc defines this field only if some macro is defined. We
117d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  /// include this field even if it may not there, to make libgcc happy.
118d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  char *fde_end;
119d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
120d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  /// next - At least we know it's a chained list!
121d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  struct LibgccObject *next;
122d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray};
123d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
124d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray// "kemgr" stuff. Apparently, all frame tables are stored there.
125d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffrayextern "C" void _keymgr_set_and_unlock_processwide_ptr(int, void *);
126d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffrayextern "C" void *_keymgr_get_and_lock_processwide_ptr(int);
127d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray#define KEYMGR_GCC3_DW2_OBJ_LIST        302     /* Dwarf2 object list  */
128d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
129d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray/// LibgccObjectInfo - libgcc defines this struct as km_object_info. It
130d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray/// probably contains all dwarf tables that are loaded.
131d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffraystruct LibgccObjectInfo {
132d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
133d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  /// seenObjects - LibgccObjects already parsed by the unwinding runtime.
134d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  ///
135d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  struct LibgccObject* seenObjects;
136d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
137d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  /// unseenObjects - LibgccObjects not parsed yet by the unwinding runtime.
138d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  ///
139d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  struct LibgccObject* unseenObjects;
140d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
141d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  unsigned unused[2];
142d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray};
143d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
144d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray/// darwin_register_frame - Since __register_frame does not work with darwin's
145d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray/// libgcc,we provide our own function, which "tricks" libgcc by modifying the
146d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray/// "Dwarf2 object list" key.
147d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffrayvoid DarwinRegisterFrame(void* FrameBegin) {
148d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // Get the key.
14944e3dd1672572df28e79649c86f2d44bf3730062Chris Lattner  LibgccObjectInfo* LOI = (struct LibgccObjectInfo*)
150d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray    _keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST);
15144e3dd1672572df28e79649c86f2d44bf3730062Chris Lattner  assert(LOI && "This should be preallocated by the runtime");
152d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
153d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // Allocate a new LibgccObject to represent this frame. Deallocation of this
154d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // object may be impossible: since darwin code in libgcc was written after
155d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // the ability to dynamically register frames, things may crash if we
156d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // deallocate it.
157d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  struct LibgccObject* ob = (struct LibgccObject*)
158d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray    malloc(sizeof(struct LibgccObject));
159d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
160d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // Do like libgcc for the values of the field.
161d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  ob->unused1 = (void *)-1;
162d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  ob->unused2 = 0;
163d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  ob->unused3 = 0;
164d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  ob->frame = FrameBegin;
165d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  ob->encoding.i = 0;
166d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  ob->encoding.b.encoding = llvm::dwarf::DW_EH_PE_omit;
167d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
168d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // Put the info on both places, as libgcc uses the first or the the second
169d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // field. Note that we rely on having two pointers here. If fde_end was a
170d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // char, things would get complicated.
171d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  ob->fde_end = (char*)LOI->unseenObjects;
172d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  ob->next = LOI->unseenObjects;
173d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
174d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // Update the key's unseenObjects list.
175d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  LOI->unseenObjects = ob;
176d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
177d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // Finally update the "key". Apparently, libgcc requires it.
178d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  _keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST,
179d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray                                         LOI);
180d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
181d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray}
182d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
183d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray}
184d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray#endif // __APPLE__
185d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray#endif // __GNUC__
186299d9d74e9b010c499557380fb6467f5361e141aAnton Korobeynikov
18734c9433004cabd4760987dce4804a91c84908219Chris Lattner/// createJIT - This is the factory method for creating a JIT for the current
18834c9433004cabd4760987dce4804a91c84908219Chris Lattner/// machine, it does not fall back to the interpreter.  This takes ownership
18934c9433004cabd4760987dce4804a91c84908219Chris Lattner/// of the module provider.
19034c9433004cabd4760987dce4804a91c84908219Chris LattnerExecutionEngine *ExecutionEngine::createJIT(ModuleProvider *MP,
19134c9433004cabd4760987dce4804a91c84908219Chris Lattner                                            std::string *ErrorStr,
192502f20b17ede40de84503010b7699b328a4f2867Evan Cheng                                            JITMemoryManager *JMM,
193502f20b17ede40de84503010b7699b328a4f2867Evan Cheng                                            bool Fast) {
194502f20b17ede40de84503010b7699b328a4f2867Evan Cheng  ExecutionEngine *EE = JIT::createJIT(MP, ErrorStr, JMM, Fast);
19534c9433004cabd4760987dce4804a91c84908219Chris Lattner  if (!EE) return 0;
19634c9433004cabd4760987dce4804a91c84908219Chris Lattner
19734c9433004cabd4760987dce4804a91c84908219Chris Lattner  // Make sure we can resolve symbols in the program as well. The zero arg
19834c9433004cabd4760987dce4804a91c84908219Chris Lattner  // to the function tells DynamicLibrary to load the program, not a library.
19934c9433004cabd4760987dce4804a91c84908219Chris Lattner  sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr);
20034c9433004cabd4760987dce4804a91c84908219Chris Lattner  return EE;
20134c9433004cabd4760987dce4804a91c84908219Chris Lattner}
20234c9433004cabd4760987dce4804a91c84908219Chris Lattner
20334c9433004cabd4760987dce4804a91c84908219Chris LattnerJIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
204502f20b17ede40de84503010b7699b328a4f2867Evan Cheng         JITMemoryManager *JMM, bool Fast)
205f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  : ExecutionEngine(MP), TM(tm), TJI(tji) {
206bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  setTargetData(TM.getTargetData());
207abb027cf412944db4d27579ba3ae00717d23c25eMisha Brukman
208f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  jitstate = new JITState(MP);
209f049e07eb8930214941c72f8e4409df394de1567Nate Begeman
210abb027cf412944db4d27579ba3ae00717d23c25eMisha Brukman  // Initialize MCE
21134c9433004cabd4760987dce4804a91c84908219Chris Lattner  MCE = createEmitter(*this, JMM);
212f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman
21350872d5385463cf1d2e23a758516e7f818e1af24Brian Gaeke  // Add target data
214ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  MutexGuard locked(lock);
215f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  FunctionPassManager &PM = jitstate->getPM(locked);
216b93b0347d8715e9f2ded1dc56a086f92aeedfb86Chris Lattner  PM.add(new TargetData(*TM.getTargetData()));
21750872d5385463cf1d2e23a758516e7f818e1af24Brian Gaeke
2184d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  // Turn the machine code intermediate representation into bytes in memory that
2194d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  // may be executed.
220502f20b17ede40de84503010b7699b328a4f2867Evan Cheng  if (TM.addPassesToEmitMachineCode(PM, *MCE, Fast)) {
221832171cb9724d2d31c8dfb73172e2be8f6dd13eeBill Wendling    cerr << "Target does not support machine code emission!\n";
2224d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner    abort();
2234d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  }
2241911fd4f85aebcd4d7b8f27313c5a363eebf49cbChris Lattner
225a7ec87cd0793c463d792323087b2fb3a4871efe0Nicolas Geoffray  // Register routine for informing unwinding runtime about new EH frames
2262d5424d76add9714d95239f183f7f88263f44360Evan Cheng#if defined(__GNUC__) && !defined(__ARM_EABI__)
227d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray#if defined(__APPLE__)
228d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  struct LibgccObjectInfo* LOI = (struct LibgccObjectInfo*)
229d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray    _keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST);
230d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray
231d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // The key is created on demand, and libgcc creates it the first time an
232d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // exception occurs. Since we need the key to register frames, we create
233d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  // it now.
234d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  if (!LOI) {
235d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray    LOI = (LibgccObjectInfo*)malloc(sizeof(struct LibgccObjectInfo));
236d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray    _keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST,
237d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray                                           LOI);
238d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  }
239d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray  InstallExceptionTableRegister(DarwinRegisterFrame);
240d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray#else
241a7ec87cd0793c463d792323087b2fb3a4871efe0Nicolas Geoffray  InstallExceptionTableRegister(__register_frame);
242d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray#endif // __APPLE__
243d046fc61ac28f65b209185a12a5e83716941d59bNicolas Geoffray#endif // __GNUC__
244a7ec87cd0793c463d792323087b2fb3a4871efe0Nicolas Geoffray
2451911fd4f85aebcd4d7b8f27313c5a363eebf49cbChris Lattner  // Initialize passes.
2461911fd4f85aebcd4d7b8f27313c5a363eebf49cbChris Lattner  PM.doInitialization();
247bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner}
248bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
2494d326fa9bea5b80147edf14d1521fc41ce315275Chris LattnerJIT::~JIT() {
250f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  delete jitstate;
2514d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  delete MCE;
2524d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  delete &TM;
2534d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner}
2544d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner
255f049e07eb8930214941c72f8e4409df394de1567Nate Begeman/// addModuleProvider - Add a new ModuleProvider to the JIT.  If we previously
256f049e07eb8930214941c72f8e4409df394de1567Nate Begeman/// removed the last ModuleProvider, we need re-initialize jitstate with a valid
257f049e07eb8930214941c72f8e4409df394de1567Nate Begeman/// ModuleProvider.
258f049e07eb8930214941c72f8e4409df394de1567Nate Begemanvoid JIT::addModuleProvider(ModuleProvider *MP) {
259f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  MutexGuard locked(lock);
260f049e07eb8930214941c72f8e4409df394de1567Nate Begeman
261f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  if (Modules.empty()) {
262f049e07eb8930214941c72f8e4409df394de1567Nate Begeman    assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
263f049e07eb8930214941c72f8e4409df394de1567Nate Begeman
264f049e07eb8930214941c72f8e4409df394de1567Nate Begeman    jitstate = new JITState(MP);
265f049e07eb8930214941c72f8e4409df394de1567Nate Begeman
266f049e07eb8930214941c72f8e4409df394de1567Nate Begeman    FunctionPassManager &PM = jitstate->getPM(locked);
267f049e07eb8930214941c72f8e4409df394de1567Nate Begeman    PM.add(new TargetData(*TM.getTargetData()));
268f049e07eb8930214941c72f8e4409df394de1567Nate Begeman
269f049e07eb8930214941c72f8e4409df394de1567Nate Begeman    // Turn the machine code intermediate representation into bytes in memory
270f049e07eb8930214941c72f8e4409df394de1567Nate Begeman    // that may be executed.
271f049e07eb8930214941c72f8e4409df394de1567Nate Begeman    if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
272f049e07eb8930214941c72f8e4409df394de1567Nate Begeman      cerr << "Target does not support machine code emission!\n";
273f049e07eb8930214941c72f8e4409df394de1567Nate Begeman      abort();
274f049e07eb8930214941c72f8e4409df394de1567Nate Begeman    }
275f049e07eb8930214941c72f8e4409df394de1567Nate Begeman
276f049e07eb8930214941c72f8e4409df394de1567Nate Begeman    // Initialize passes.
277f049e07eb8930214941c72f8e4409df394de1567Nate Begeman    PM.doInitialization();
278f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  }
279f049e07eb8930214941c72f8e4409df394de1567Nate Begeman
280f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  ExecutionEngine::addModuleProvider(MP);
281f049e07eb8930214941c72f8e4409df394de1567Nate Begeman}
282f049e07eb8930214941c72f8e4409df394de1567Nate Begeman
283f049e07eb8930214941c72f8e4409df394de1567Nate Begeman/// removeModuleProvider - If we are removing the last ModuleProvider,
284f049e07eb8930214941c72f8e4409df394de1567Nate Begeman/// invalidate the jitstate since the PassManager it contains references a
285f049e07eb8930214941c72f8e4409df394de1567Nate Begeman/// released ModuleProvider.
286f049e07eb8930214941c72f8e4409df394de1567Nate BegemanModule *JIT::removeModuleProvider(ModuleProvider *MP, std::string *E) {
287f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  Module *result = ExecutionEngine::removeModuleProvider(MP, E);
288f049e07eb8930214941c72f8e4409df394de1567Nate Begeman
289f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  MutexGuard locked(lock);
290d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman
291d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  if (jitstate->getMP() == MP) {
292f049e07eb8930214941c72f8e4409df394de1567Nate Begeman    delete jitstate;
293f049e07eb8930214941c72f8e4409df394de1567Nate Begeman    jitstate = 0;
294f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  }
295f049e07eb8930214941c72f8e4409df394de1567Nate Begeman
296d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  if (!jitstate && !Modules.empty()) {
297d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    jitstate = new JITState(Modules[0]);
298d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman
299d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    FunctionPassManager &PM = jitstate->getPM(locked);
300d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    PM.add(new TargetData(*TM.getTargetData()));
301d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman
302d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    // Turn the machine code intermediate representation into bytes in memory
303d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    // that may be executed.
304d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
305d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman      cerr << "Target does not support machine code emission!\n";
306d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman      abort();
307d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    }
308d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman
309d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    // Initialize passes.
310d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    PM.doInitialization();
311d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  }
312f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  return result;
313f049e07eb8930214941c72f8e4409df394de1567Nate Begeman}
314f049e07eb8930214941c72f8e4409df394de1567Nate Begeman
31560789e419e04c260e36af9a1add5ad316313e490Nate Begeman/// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
31660789e419e04c260e36af9a1add5ad316313e490Nate Begeman/// and deletes the ModuleProvider and owned Module.  Avoids materializing
31760789e419e04c260e36af9a1add5ad316313e490Nate Begeman/// the underlying module.
31860789e419e04c260e36af9a1add5ad316313e490Nate Begemanvoid JIT::deleteModuleProvider(ModuleProvider *MP, std::string *E) {
31960789e419e04c260e36af9a1add5ad316313e490Nate Begeman  ExecutionEngine::deleteModuleProvider(MP, E);
32060789e419e04c260e36af9a1add5ad316313e490Nate Begeman
32160789e419e04c260e36af9a1add5ad316313e490Nate Begeman  MutexGuard locked(lock);
322d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman
323d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  if (jitstate->getMP() == MP) {
32460789e419e04c260e36af9a1add5ad316313e490Nate Begeman    delete jitstate;
32560789e419e04c260e36af9a1add5ad316313e490Nate Begeman    jitstate = 0;
32660789e419e04c260e36af9a1add5ad316313e490Nate Begeman  }
327d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman
328d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  if (!jitstate && !Modules.empty()) {
329d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    jitstate = new JITState(Modules[0]);
330d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman
331d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    FunctionPassManager &PM = jitstate->getPM(locked);
332d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    PM.add(new TargetData(*TM.getTargetData()));
333d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman
334d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    // Turn the machine code intermediate representation into bytes in memory
335d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    // that may be executed.
336d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
337d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman      cerr << "Target does not support machine code emission!\n";
338d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman      abort();
339d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    }
340d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman
341d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    // Initialize passes.
342d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    PM.doInitialization();
343d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  }
34460789e419e04c260e36af9a1add5ad316313e490Nate Begeman}
34560789e419e04c260e36af9a1add5ad316313e490Nate Begeman
34670975eef572b9e132bbaade16ba9edb76f15f287Brian Gaeke/// run - Start execution with the specified function and arguments.
34705a1a306bcfba2b533dc1210d24924e5f3e9ed0eChris Lattner///
348ff0f1bb32a439cf82cb09ee29544c894a2bfe877Chris LattnerGenericValue JIT::runFunction(Function *F,
349ff0f1bb32a439cf82cb09ee29544c894a2bfe877Chris Lattner                              const std::vector<GenericValue> &ArgValues) {
350b47130c580385a5a2c922c480a85438b2bb80293Chris Lattner  assert(F && "Function *F was null at entry to run()");
351b47130c580385a5a2c922c480a85438b2bb80293Chris Lattner
352b47130c580385a5a2c922c480a85438b2bb80293Chris Lattner  void *FPtr = getPointerToFunction(F);
3537c45d7898bdfdc29a61a58b678e13ed41333ed51Chris Lattner  assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
354f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner  const FunctionType *FTy = F->getFunctionType();
355e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner  const Type *RetTy = FTy->getReturnType();
356bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
35707ab52b6459f65b3e1c94d52321165688a6096aaDan Gohman  assert((FTy->getNumParams() == ArgValues.size() ||
35807ab52b6459f65b3e1c94d52321165688a6096aaDan Gohman          (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
35907ab52b6459f65b3e1c94d52321165688a6096aaDan Gohman         "Wrong number of arguments passed into function!");
360e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner  assert(FTy->getNumParams() == ArgValues.size() &&
361e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner         "This doesn't support passing arguments through varargs (yet)!");
362e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner
363ec8430270a9f1adbf9b790319a1d4781658359fdMisha Brukman  // Handle some common cases first.  These cases correspond to common `main'
364e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner  // prototypes.
36579e038a16d644868bd8d6e230b451c4cb8f8ff93Chris Lattner  if (RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
366f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner    switch (ArgValues.size()) {
367f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner    case 3:
36879e038a16d644868bd8d6e230b451c4cb8f8ff93Chris Lattner      if (FTy->getParamType(0) == Type::Int32Ty &&
369f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner          isa<PointerType>(FTy->getParamType(1)) &&
370f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner          isa<PointerType>(FTy->getParamType(2))) {
371f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner        int (*PF)(int, char **, const char **) =
372870286aa33290c00e55ba479a60251c79f3a7911Chris Lattner          (int(*)(int, char **, const char **))(intptr_t)FPtr;
373ec8430270a9f1adbf9b790319a1d4781658359fdMisha Brukman
374f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner        // Call the function.
375e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner        GenericValue rv;
37638f6a15df69c271c3bebf648642041b31380e31aReid Spencer        rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
37738f6a15df69c271c3bebf648642041b31380e31aReid Spencer                                 (char **)GVTOP(ArgValues[1]),
37838f6a15df69c271c3bebf648642041b31380e31aReid Spencer                                 (const char **)GVTOP(ArgValues[2])));
379f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner        return rv;
380f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner      }
381f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner      break;
382174f2264649d9ae062bdb0a038131c2836596be5Chris Lattner    case 2:
38379e038a16d644868bd8d6e230b451c4cb8f8ff93Chris Lattner      if (FTy->getParamType(0) == Type::Int32Ty &&
384174f2264649d9ae062bdb0a038131c2836596be5Chris Lattner          isa<PointerType>(FTy->getParamType(1))) {
385870286aa33290c00e55ba479a60251c79f3a7911Chris Lattner        int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
386ec8430270a9f1adbf9b790319a1d4781658359fdMisha Brukman
387174f2264649d9ae062bdb0a038131c2836596be5Chris Lattner        // Call the function.
388174f2264649d9ae062bdb0a038131c2836596be5Chris Lattner        GenericValue rv;
38938f6a15df69c271c3bebf648642041b31380e31aReid Spencer        rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
39038f6a15df69c271c3bebf648642041b31380e31aReid Spencer                                 (char **)GVTOP(ArgValues[1])));
391174f2264649d9ae062bdb0a038131c2836596be5Chris Lattner        return rv;
392174f2264649d9ae062bdb0a038131c2836596be5Chris Lattner      }
393174f2264649d9ae062bdb0a038131c2836596be5Chris Lattner      break;
394f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner    case 1:
395f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner      if (FTy->getNumParams() == 1 &&
39679e038a16d644868bd8d6e230b451c4cb8f8ff93Chris Lattner          FTy->getParamType(0) == Type::Int32Ty) {
397e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner        GenericValue rv;
398870286aa33290c00e55ba479a60251c79f3a7911Chris Lattner        int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
39938f6a15df69c271c3bebf648642041b31380e31aReid Spencer        rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
400f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner        return rv;
401f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner      }
402f7bedf447e080cb57c64d6f0cbf6fbeb9f4c596eChris Lattner      break;
403e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner    }
404e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner  }
405e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner
406e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner  // Handle cases where no arguments are passed first.
407e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner  if (ArgValues.empty()) {
408e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner    GenericValue rv;
409e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner    switch (RetTy->getTypeID()) {
410e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner    default: assert(0 && "Unknown return type for function call!");
411a54b7cbd452b3adb2f51346140d996b29c2cdb30Reid Spencer    case Type::IntegerTyID: {
412a54b7cbd452b3adb2f51346140d996b29c2cdb30Reid Spencer      unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
413a54b7cbd452b3adb2f51346140d996b29c2cdb30Reid Spencer      if (BitWidth == 1)
41438f6a15df69c271c3bebf648642041b31380e31aReid Spencer        rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
415a54b7cbd452b3adb2f51346140d996b29c2cdb30Reid Spencer      else if (BitWidth <= 8)
41638f6a15df69c271c3bebf648642041b31380e31aReid Spencer        rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
417a54b7cbd452b3adb2f51346140d996b29c2cdb30Reid Spencer      else if (BitWidth <= 16)
41838f6a15df69c271c3bebf648642041b31380e31aReid Spencer        rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
419a54b7cbd452b3adb2f51346140d996b29c2cdb30Reid Spencer      else if (BitWidth <= 32)
42038f6a15df69c271c3bebf648642041b31380e31aReid Spencer        rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
421a54b7cbd452b3adb2f51346140d996b29c2cdb30Reid Spencer      else if (BitWidth <= 64)
42238f6a15df69c271c3bebf648642041b31380e31aReid Spencer        rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
423a54b7cbd452b3adb2f51346140d996b29c2cdb30Reid Spencer      else
424a54b7cbd452b3adb2f51346140d996b29c2cdb30Reid Spencer        assert(0 && "Integer types > 64 bits not supported");
425e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner      return rv;
426a54b7cbd452b3adb2f51346140d996b29c2cdb30Reid Spencer    }
427e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner    case Type::VoidTyID:
42838f6a15df69c271c3bebf648642041b31380e31aReid Spencer      rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
429e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner      return rv;
430e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner    case Type::FloatTyID:
431870286aa33290c00e55ba479a60251c79f3a7911Chris Lattner      rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
432e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner      return rv;
433e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner    case Type::DoubleTyID:
434870286aa33290c00e55ba479a60251c79f3a7911Chris Lattner      rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
435d297aea5f24f2ad369162b8c1084cda4dcc839f0Chris Lattner      return rv;
4361abac0d725374cb6e527d2a735a5272a4f7913faDale Johannesen    case Type::X86_FP80TyID:
4371abac0d725374cb6e527d2a735a5272a4f7913faDale Johannesen    case Type::FP128TyID:
4381abac0d725374cb6e527d2a735a5272a4f7913faDale Johannesen    case Type::PPC_FP128TyID:
4391abac0d725374cb6e527d2a735a5272a4f7913faDale Johannesen      assert(0 && "long double not supported yet");
4401abac0d725374cb6e527d2a735a5272a4f7913faDale Johannesen      return rv;
441e5eab142a51373dd3bee758ea958575afbfce259Chris Lattner    case Type::PointerTyID:
442870286aa33290c00e55ba479a60251c79f3a7911Chris Lattner      return PTOGV(((void*(*)())(intptr_t)FPtr)());
443d297aea5f24f2ad369162b8c1084cda4dcc839f0Chris Lattner    }
444ff0f1bb32a439cf82cb09ee29544c894a2bfe877Chris Lattner  }
44570975eef572b9e132bbaade16ba9edb76f15f287Brian Gaeke
446cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  // Okay, this is not one of our quick and easy cases.  Because we don't have a
447cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  // full FFI, we have to codegen a nullary stub function that just calls the
448cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  // function we are interested in, passing in constants for all of the
449cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  // arguments.  Make this function and return.
450cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner
451cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  // First, create the function.
452cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
453051a950000e21935165db56695e35bade668193bGabor Greif  Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
454051a950000e21935165db56695e35bade668193bGabor Greif                                    F->getParent());
455cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner
456cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  // Insert a basic block.
457051a950000e21935165db56695e35bade668193bGabor Greif  BasicBlock *StubBB = BasicBlock::Create("", Stub);
458cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner
459cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  // Convert all of the GenericValue arguments over to constants.  Note that we
460cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  // currently don't support varargs.
461990b849abc9481b8c7a482019cd0d95fe2f2a3eaChris Lattner  SmallVector<Value*, 8> Args;
462cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
463cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner    Constant *C = 0;
464cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner    const Type *ArgTy = FTy->getParamType(i);
465cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner    const GenericValue &AV = ArgValues[i];
466cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner    switch (ArgTy->getTypeID()) {
467cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner    default: assert(0 && "Unknown argument type for function call!");
46802a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner    case Type::IntegerTyID:
46902a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner        C = ConstantInt::get(AV.IntVal);
47002a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner        break;
47102a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner    case Type::FloatTyID:
47202a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner        C = ConstantFP::get(APFloat(AV.FloatVal));
47302a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner        break;
47402a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner    case Type::DoubleTyID:
47502a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner        C = ConstantFP::get(APFloat(AV.DoubleVal));
47602a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner        break;
4771abac0d725374cb6e527d2a735a5272a4f7913faDale Johannesen    case Type::PPC_FP128TyID:
4781abac0d725374cb6e527d2a735a5272a4f7913faDale Johannesen    case Type::X86_FP80TyID:
47902a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner    case Type::FP128TyID:
48002a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner        C = ConstantFP::get(APFloat(AV.IntVal));
48102a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner        break;
482cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner    case Type::PointerTyID:
483cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner      void *ArgPtr = GVTOP(AV);
48402a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner      if (sizeof(void*) == 4)
485e49661bdf5b7a913d4e368cf511381e524ae403aReid Spencer        C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
48602a260aa11a2e1b2c14335274d3c42ca3f3eabc0Chris Lattner      else
487e49661bdf5b7a913d4e368cf511381e524ae403aReid Spencer        C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
48815f46d6c280cc91be70c60ed181931fbe0088652Reid Spencer      C = ConstantExpr::getIntToPtr(C, ArgTy);  // Cast the integer to pointer
489cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner      break;
490cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner    }
491cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner    Args.push_back(C);
492cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  }
493cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner
494b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif  CallInst *TheCall = CallInst::Create(F, Args.begin(), Args.end(),
495b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif                                       "", StubBB);
496cdfc51ffe3eda17a611fe2aaed1a2e6650d3d203Chris Lattner  TheCall->setCallingConv(F->getCallingConv());
497a471e045b7cef5da428bdada4a362a4be671ef0bChris Lattner  TheCall->setTailCall();
498cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  if (TheCall->getType() != Type::VoidTy)
499b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif    ReturnInst::Create(TheCall, StubBB);    // Return result of the call.
500cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  else
501b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif    ReturnInst::Create(StubBB);             // Just return void.
502cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner
503cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  // Finally, return the value returned by our nullary stub function.
504cc22e9f406cf21e3a2036bf6628ee7a5c850d42cChris Lattner  return runFunction(Stub, std::vector<GenericValue>());
505bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner}
5064d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner
5074d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner/// runJITOnFunction - Run the FunctionPassManager full of
5084d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner/// just-in-time compilation passes on F, hopefully filling in
5094d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner/// GlobalAddress[F] with the address of F's machine code.
5104d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner///
5114d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattnervoid JIT::runJITOnFunction(Function *F) {
512ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  MutexGuard locked(lock);
51321afcda54479602c2936946731a3f1ee8e5d2322Dan Gohman  runJITOnFunctionUnlocked(F, locked);
51421afcda54479602c2936946731a3f1ee8e5d2322Dan Gohman}
51521afcda54479602c2936946731a3f1ee8e5d2322Dan Gohman
51621afcda54479602c2936946731a3f1ee8e5d2322Dan Gohmanvoid JIT::runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked) {
51721afcda54479602c2936946731a3f1ee8e5d2322Dan Gohman  static bool isAlreadyCodeGenerating = false;
51817f218e001862785c5ba5974a6374f2f7bc29ac0Chris Lattner  assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
5194d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner
5204d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  // JIT the function
5214d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  isAlreadyCodeGenerating = true;
522f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  jitstate->getPM(locked).run(*F);
5234d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  isAlreadyCodeGenerating = false;
524c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner
525d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  // If the function referred to another function that had not yet been
526d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  // read from bitcode, but we are jitting non-lazily, emit it now.
527d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  while (!jitstate->getPendingFunctions(locked).empty()) {
528d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    Function *PF = jitstate->getPendingFunctions(locked).back();
529d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    jitstate->getPendingFunctions(locked).pop_back();
530d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman
531d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    // JIT the function
532d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    isAlreadyCodeGenerating = true;
533d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    jitstate->getPM(locked).run(*PF);
534d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    isAlreadyCodeGenerating = false;
535d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman
536d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    // Now that the function has been jitted, ask the JITEmitter to rewrite
537d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    // the stub with real address of the function.
538d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    updateFunctionStub(PF);
539c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  }
540d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman
541d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  // If the JIT is configured to emit info so that dlsym can be used to
542d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  // rewrite stubs to external globals, do so now.
543d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  if (areDlsymStubsEnabled() && isLazyCompilationDisabled())
544d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    updateDlsymStubTable();
5454d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner}
5464d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner
5474d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner/// getPointerToFunction - This method is used to get the address of the
5484d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner/// specified function, compiling it if neccesary.
5494d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner///
5504d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattnervoid *JIT::getPointerToFunction(Function *F) {
551ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
552c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  if (void *Addr = getPointerToGlobalIfAvailable(F))
553c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner    return Addr;   // Check if function already code gen'd
5544d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner
555a3ac0c105d26f8535d03031db7e97274b3ff8159Dan Gohman  MutexGuard locked(lock);
556a3ac0c105d26f8535d03031db7e97274b3ff8159Dan Gohman
557fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  // Make sure we read in the function if it exists in this Module.
558a99be51bf5cdac1438069d4b01766c47704961c8Gabor Greif  if (F->hasNotBeenReadFromBitcode()) {
559fe854034677f59baca1e38075e71f6efca247a03Chris Lattner    // Determine the module provider this function is provided by.
560fe854034677f59baca1e38075e71f6efca247a03Chris Lattner    Module *M = F->getParent();
561fe854034677f59baca1e38075e71f6efca247a03Chris Lattner    ModuleProvider *MP = 0;
562fe854034677f59baca1e38075e71f6efca247a03Chris Lattner    for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
563fe854034677f59baca1e38075e71f6efca247a03Chris Lattner      if (Modules[i]->getModule() == M) {
564fe854034677f59baca1e38075e71f6efca247a03Chris Lattner        MP = Modules[i];
565fe854034677f59baca1e38075e71f6efca247a03Chris Lattner        break;
566fe854034677f59baca1e38075e71f6efca247a03Chris Lattner      }
567fe854034677f59baca1e38075e71f6efca247a03Chris Lattner    }
568fe854034677f59baca1e38075e71f6efca247a03Chris Lattner    assert(MP && "Function isn't in a module we know about!");
569fe854034677f59baca1e38075e71f6efca247a03Chris Lattner
5705c72a3ae106d8d1dd6aa7e573948dac4102aca7bChris Lattner    std::string ErrorMsg;
5715c72a3ae106d8d1dd6aa7e573948dac4102aca7bChris Lattner    if (MP->materializeFunction(F, &ErrorMsg)) {
572832171cb9724d2d31c8dfb73172e2be8f6dd13eeBill Wendling      cerr << "Error reading function '" << F->getName()
573a99be51bf5cdac1438069d4b01766c47704961c8Gabor Greif           << "' from bitcode file: " << ErrorMsg << "\n";
5740050ef831912eea2377c815bc59c09ed828f2684Chris Lattner      abort();
5750050ef831912eea2377c815bc59c09ed828f2684Chris Lattner    }
5761c341c8462a208ccc5ba4b4e030cdc989ce38818Mon P Wang
57769f9378675b23135043d93aa58300fed3ec41cbfDan Gohman    // Now retry to get the address.
57869f9378675b23135043d93aa58300fed3ec41cbfDan Gohman    if (void *Addr = getPointerToGlobalIfAvailable(F))
57969f9378675b23135043d93aa58300fed3ec41cbfDan Gohman      return Addr;
580fd7d99120b394e7aa4cf0b670f7b783a61dec3a1Nicolas Geoffray  }
5814d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner
5825cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer  if (F->isDeclaration()) {
58369f9378675b23135043d93aa58300fed3ec41cbfDan Gohman    bool AbortOnFailure = F->getLinkage() != GlobalValue::ExternalWeakLinkage;
58469f9378675b23135043d93aa58300fed3ec41cbfDan Gohman    void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
585c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner    addGlobalMapping(F, Addr);
586c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner    return Addr;
587c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  }
5884d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner
58921afcda54479602c2936946731a3f1ee8e5d2322Dan Gohman  runJITOnFunctionUnlocked(F, locked);
590c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner
591c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  void *Addr = getPointerToGlobalIfAvailable(F);
5924d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  assert(Addr && "Code generation didn't add function to GlobalAddress table!");
5934d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  return Addr;
5944d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner}
5954d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner
596c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner/// getOrEmitGlobalVariable - Return the address of the specified global
597c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner/// variable, possibly emitting it to memory if needed.  This is used by the
598c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner/// Emitter.
599c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattnervoid *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
600ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  MutexGuard locked(lock);
601ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
602c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  void *Ptr = getPointerToGlobalIfAvailable(GV);
603c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  if (Ptr) return Ptr;
604c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner
605c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  // If the global is external, just remember the address.
6065cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer  if (GV->isDeclaration()) {
6073b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov#if HAVE___DSO_HANDLE
6085f42c550305814f916207b9604b92fbd258e8eafEvan Cheng    if (GV->getName() == "__dso_handle")
6095f42c550305814f916207b9604b92fbd258e8eafEvan Cheng      return (void*)&__dso_handle;
610b82ab94e20bd48ac149746864e0d5b1653bdcf3eEvan Cheng#endif
611df5a37efc997288da520ff4889443e3560d95387Reid Spencer    Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
61266941988de6a295649b33f4c2b0f36a094b2244dNate Begeman    if (Ptr == 0 && !areDlsymStubsEnabled()) {
613832171cb9724d2d31c8dfb73172e2be8f6dd13eeBill Wendling      cerr << "Could not resolve external global address: "
614832171cb9724d2d31c8dfb73172e2be8f6dd13eeBill Wendling           << GV->getName() << "\n";
615c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner      abort();
616c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner    }
61766941988de6a295649b33f4c2b0f36a094b2244dNate Begeman    addGlobalMapping(GV, Ptr);
618c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  } else {
6194d544a2c807756af6bffc71f1967f22e8045d02eEvan Cheng    // GlobalVariable's which are not "constant" will cause trouble in a server
6204d544a2c807756af6bffc71f1967f22e8045d02eEvan Cheng    // situation. It's returned in the same block of memory as code which may
6214d544a2c807756af6bffc71f1967f22e8045d02eEvan Cheng    // not be writable.
6224d544a2c807756af6bffc71f1967f22e8045d02eEvan Cheng    if (isGVCompilationDisabled() && !GV->isConstant()) {
62377f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng      cerr << "Compilation of non-internal GlobalValue is disabled!\n";
624446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng      abort();
625446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng    }
626dd947ea3c5e020c33c58a31939561265b980a3adDale Johannesen    // If the global hasn't been emitted to memory yet, allocate space and
627dd947ea3c5e020c33c58a31939561265b980a3adDale Johannesen    // emit it into memory.  It goes in the same array as the generated
628dd947ea3c5e020c33c58a31939561265b980a3adDale Johannesen    // code, jump tables, etc.
629f5d438c1f0e0c0ed44475ab69f03d86a3c818c60Chris Lattner    const Type *GlobalType = GV->getType()->getElementType();
630ceb4d1aecb9deffe59b3dcdc9a783ffde8477be9Duncan Sands    size_t S = getTargetData()->getTypePaddedSize(GlobalType);
631d102593b425da27fa96359300ff0e3d547d0ac8dDuncan Sands    size_t A = getTargetData()->getPreferredAlignment(GV);
63246fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray    if (GV->isThreadLocal()) {
63346fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray      MutexGuard locked(lock);
63446fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray      Ptr = TJI.allocateThreadLocalMemory(S);
635b0b53491ef32b85bd90c8590faeb8a3fb4b17a95Evan Cheng    } else if (TJI.allocateSeparateGVMemory()) {
636b0b53491ef32b85bd90c8590faeb8a3fb4b17a95Evan Cheng      if (A <= 8) {
637b0b53491ef32b85bd90c8590faeb8a3fb4b17a95Evan Cheng        Ptr = malloc(S);
638b0b53491ef32b85bd90c8590faeb8a3fb4b17a95Evan Cheng      } else {
639b0b53491ef32b85bd90c8590faeb8a3fb4b17a95Evan Cheng        // Allocate S+A bytes of memory, then use an aligned pointer within that
640b0b53491ef32b85bd90c8590faeb8a3fb4b17a95Evan Cheng        // space.
641b0b53491ef32b85bd90c8590faeb8a3fb4b17a95Evan Cheng        Ptr = malloc(S+A);
642b0b53491ef32b85bd90c8590faeb8a3fb4b17a95Evan Cheng        unsigned MisAligned = ((intptr_t)Ptr & (A-1));
643b0b53491ef32b85bd90c8590faeb8a3fb4b17a95Evan Cheng        Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
644b0b53491ef32b85bd90c8590faeb8a3fb4b17a95Evan Cheng      }
64546fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray    } else {
64646fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray      Ptr = MCE->allocateSpace(S, A);
64746fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray    }
648dd947ea3c5e020c33c58a31939561265b980a3adDale Johannesen    addGlobalMapping(GV, Ptr);
649dd947ea3c5e020c33c58a31939561265b980a3adDale Johannesen    EmitGlobalVariable(GV);
650c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  }
651c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  return Ptr;
652c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner}
653c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner
6544d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner/// recompileAndRelinkFunction - This method is used to force a function
6554d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner/// which has already been compiled, to be compiled again, possibly
6564d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner/// after it has been modified. Then the entry to the old copy is overwritten
6574d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner/// with a branch to the new copy. If there was no old copy, this acts
6584d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner/// just like JIT::getPointerToFunction().
6594d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner///
6604d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattnervoid *JIT::recompileAndRelinkFunction(Function *F) {
661c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  void *OldAddr = getPointerToGlobalIfAvailable(F);
6624d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner
663c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  // If it's not already compiled there is no reason to patch it up.
664c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  if (OldAddr == 0) { return getPointerToFunction(F); }
6654d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner
666c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  // Delete the old function mapping.
667c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  addGlobalMapping(F, 0);
668c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner
669c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  // Recodegen the function
6704d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  runJITOnFunction(F);
671c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner
672c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  // Update state, forward the old function to the new function.
673c07ed1387503d25c0b93fcf617f69329d73fc589Chris Lattner  void *Addr = getPointerToGlobalIfAvailable(F);
6744d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  assert(Addr && "Code generation didn't add function to GlobalAddress table!");
6754d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  TJI.replaceMachineCodeForFunction(OldAddr, Addr);
6764d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner  return Addr;
6774d326fa9bea5b80147edf14d1521fc41ce315275Chris Lattner}
678895eddfad43f848d5accce1789aa80be0db459d3Misha Brukman
67946fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray/// getMemoryForGV - This method abstracts memory allocation of global
68046fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray/// variable so that the JIT can allocate thread local variables depending
68146fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray/// on the target.
68246fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray///
68346fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffraychar* JIT::getMemoryForGV(const GlobalVariable* GV) {
68446fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray  const Type *ElTy = GV->getType()->getElementType();
685ceb4d1aecb9deffe59b3dcdc9a783ffde8477be9Duncan Sands  size_t GVSize = (size_t)getTargetData()->getTypePaddedSize(ElTy);
68646fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray  if (GV->isThreadLocal()) {
68746fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray    MutexGuard locked(lock);
68846fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray    return TJI.allocateThreadLocalMemory(GVSize);
68946fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray  } else {
69046fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray    return new char[GVSize];
69146fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray  }
69246fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray}
693d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman
694d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begemanvoid JIT::addPendingFunction(Function *F) {
695d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  MutexGuard locked(lock);
696d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  jitstate->getPendingFunctions(locked).push_back(F);
697d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman}
698