JITCode.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) 2008, 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef JITCode_h
  26. #define JITCode_h
  27. #if ENABLE(JIT) || ENABLE(LLINT)
  28. #include "CallFrame.h"
  29. #include "Disassembler.h"
  30. #include "JITStubs.h"
  31. #include "JSCJSValue.h"
  32. #include "LegacyProfiler.h"
  33. #include "MacroAssemblerCodeRef.h"
  34. #endif
  35. namespace JSC {
  36. #if ENABLE(JIT)
  37. class VM;
  38. class JSStack;
  39. #endif
  40. class JITCode {
  41. #if ENABLE(DETACHED_JIT)
  42. DETACHED_JIT_MAKE_SHARED_DATA_ALLOCATED;
  43. #endif
  44. #if ENABLE(JIT) || ENABLE(LLINT)
  45. typedef MacroAssemblerCodeRef CodeRef;
  46. typedef MacroAssemblerCodePtr CodePtr;
  47. #else
  48. JITCode() { }
  49. #endif
  50. public:
  51. enum JITType { None, HostCallThunk, InterpreterThunk, BaselineJIT, DFGJIT };
  52. static JITType bottomTierJIT()
  53. {
  54. return BaselineJIT;
  55. }
  56. static JITType topTierJIT()
  57. {
  58. return DFGJIT;
  59. }
  60. static JITType nextTierJIT(JITType jitType)
  61. {
  62. ASSERT_UNUSED(jitType, jitType == BaselineJIT || jitType == DFGJIT);
  63. return DFGJIT;
  64. }
  65. static bool isOptimizingJIT(JITType jitType)
  66. {
  67. return jitType == DFGJIT;
  68. }
  69. static bool isBaselineCode(JITType jitType)
  70. {
  71. return jitType == InterpreterThunk || jitType == BaselineJIT;
  72. }
  73. #if ENABLE(JIT) || ENABLE(LLINT)
  74. JITCode()
  75. : m_jitType(None)
  76. {
  77. }
  78. JITCode(const CodeRef ref, JITType jitType)
  79. : m_ref(ref)
  80. , m_jitType(jitType)
  81. {
  82. ASSERT(jitType != None);
  83. }
  84. bool operator !() const
  85. {
  86. return !m_ref;
  87. }
  88. CodePtr addressForCall()
  89. {
  90. return m_ref.code();
  91. }
  92. void* executableAddressAtOffset(size_t offset) const
  93. {
  94. ASSERT(offset < size());
  95. return reinterpret_cast<char*>(m_ref.code().executableAddress()) + offset;
  96. }
  97. void* executableAddress() const
  98. {
  99. return executableAddressAtOffset(0);
  100. }
  101. void* dataAddressAtOffset(size_t offset) const
  102. {
  103. ASSERT(offset <= size()); // use <= instead of < because it is valid to ask for an address at the exclusive end of the code.
  104. return reinterpret_cast<char*>(m_ref.code().dataLocation()) + offset;
  105. }
  106. // This function returns the offset in bytes of 'pointerIntoCode' into
  107. // this block of code. The pointer provided must be a pointer into this
  108. // block of code. It is ASSERTed that no codeblock >4gb in size.
  109. unsigned offsetOf(void* pointerIntoCode)
  110. {
  111. intptr_t result = reinterpret_cast<intptr_t>(pointerIntoCode) - reinterpret_cast<intptr_t>(m_ref.code().executableAddress());
  112. ASSERT(static_cast<intptr_t>(static_cast<unsigned>(result)) == result);
  113. return static_cast<unsigned>(result);
  114. }
  115. // Execute the code!
  116. inline JSValue execute(JSStack* stack, CallFrame* callFrame, VM* vm)
  117. {
  118. #if ENABLE(JIT) && !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  119. JSValue result = JSValue::decode(ctiTrampoline(m_ref.code().executableAddress(), stack, callFrame, 0, 0, vm));
  120. return vm->exception ? jsNull() : result;
  121. #else
  122. ASSERT_NOT_REACHED();
  123. return jsNull();
  124. #endif
  125. }
  126. void* start() const
  127. {
  128. return m_ref.code().dataLocation();
  129. }
  130. size_t size() const
  131. {
  132. ASSERT(m_ref.code().executableAddress());
  133. return m_ref.size();
  134. }
  135. bool tryToDisassemble(const char* prefix) const
  136. {
  137. return m_ref.tryToDisassemble(prefix);
  138. }
  139. ExecutableMemoryHandle* getExecutableMemory()
  140. {
  141. return m_ref.executableMemory();
  142. }
  143. JITType jitType() const
  144. {
  145. return m_jitType;
  146. }
  147. // Host functions are a bit special; they have a m_code pointer but they
  148. // do not individully ref the executable pool containing the trampoline.
  149. static JITCode HostFunction(CodeRef code)
  150. {
  151. return JITCode(code, HostCallThunk);
  152. }
  153. void clear()
  154. {
  155. m_ref.~CodeRef();
  156. new (NotNull, &m_ref) CodeRef();
  157. }
  158. private:
  159. JITCode(PassRefPtr<ExecutableMemoryHandle> executableMemory, JITType jitType)
  160. : m_ref(executableMemory)
  161. , m_jitType(jitType)
  162. {
  163. }
  164. CodeRef m_ref;
  165. JITType m_jitType;
  166. #endif // ENABLE(JIT) || ENABLE(LLINT)
  167. };
  168. } // namespace JSC
  169. namespace WTF {
  170. class PrintStream;
  171. void printInternal(PrintStream&, JSC::JITCode::JITType);
  172. } // namespace WTF
  173. #endif