/js/src/methodjit/TrampolineCompiler.cpp

http://github.com/zpao/v8monkey · C++ · 158 lines · 86 code · 19 blank · 53 comment · 9 complexity · d875fd01be47497e735997b4f0b1fd87 MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=4 sw=4 et tw=99:
  3. *
  4. * ***** BEGIN LICENSE BLOCK *****
  5. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is Mozilla Jaegermonkey.
  18. *
  19. * The Initial Developer of the Original Code is the Mozilla Foundation.
  20. *
  21. * Portions created by the Initial Developer are Copyright (C) 2010
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. * Andrew Drake <drakedevel@gmail.com>
  26. *
  27. * Alternatively, the contents of this file may be used under the terms of
  28. * either the GNU General Public License Version 2 or later (the "GPL"), or
  29. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30. * in which case the provisions of the GPL or the LGPL are applicable instead
  31. * of those above. If you wish to allow use of your version of this file only
  32. * under the terms of either the GPL or the LGPL, and not to allow others to
  33. * use your version of this file under the terms of the MPL, indicate your
  34. * decision by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL or the LGPL. If you do not delete
  36. * the provisions above, a recipient may use your version of this file under
  37. * the terms of any one of the MPL, the GPL or the LGPL.
  38. *
  39. * ***** END LICENSE BLOCK ***** */
  40. #include "TrampolineCompiler.h"
  41. #include "StubCalls.h"
  42. #include "assembler/assembler/LinkBuffer.h"
  43. #include "assembler/jit/ExecutableAllocator.h"
  44. namespace js {
  45. namespace mjit {
  46. #define CHECK_RESULT(x) if (!(x)) return false
  47. #define COMPILE(which, pool, how) CHECK_RESULT(compileTrampoline(&(which), &pool, how))
  48. #define RELEASE(which, pool) JS_BEGIN_MACRO \
  49. which = NULL; \
  50. if (pool) \
  51. pool->release(); \
  52. pool = NULL; \
  53. JS_END_MACRO
  54. typedef JSC::MacroAssembler::Address Address;
  55. typedef JSC::MacroAssembler::Label Label;
  56. typedef JSC::MacroAssembler::Jump Jump;
  57. typedef JSC::MacroAssembler::ImmPtr ImmPtr;
  58. typedef JSC::MacroAssembler::Imm32 Imm32;
  59. typedef JSC::MacroAssembler::Address Address;
  60. bool
  61. TrampolineCompiler::compile()
  62. {
  63. #ifdef JS_METHODJIT_SPEW
  64. JMCheckLogging();
  65. #endif
  66. COMPILE(trampolines->forceReturn, trampolines->forceReturnPool, generateForceReturn);
  67. #if (defined(JS_NO_FASTCALL) && defined(JS_CPU_X86)) || defined(_WIN64)
  68. COMPILE(trampolines->forceReturnFast, trampolines->forceReturnFastPool, generateForceReturnFast);
  69. #endif
  70. return true;
  71. }
  72. void
  73. TrampolineCompiler::release(Trampolines *tramps)
  74. {
  75. RELEASE(tramps->forceReturn, tramps->forceReturnPool);
  76. #if (defined(JS_NO_FASTCALL) && defined(JS_CPU_X86)) || defined(_WIN64)
  77. RELEASE(tramps->forceReturnFast, tramps->forceReturnFastPool);
  78. #endif
  79. }
  80. bool
  81. TrampolineCompiler::compileTrampoline(Trampolines::TrampolinePtr *where,
  82. JSC::ExecutablePool **poolp, TrampolineGenerator generator)
  83. {
  84. Assembler masm;
  85. Label entry = masm.label();
  86. CHECK_RESULT(generator(masm));
  87. JS_ASSERT(entry.isSet());
  88. bool ok;
  89. JSC::LinkBuffer buffer(&masm, execAlloc, poolp, &ok, JSC::METHOD_CODE);
  90. if (!ok)
  91. return false;
  92. masm.finalize(buffer);
  93. uint8_t *result = (uint8_t*)buffer.finalizeCodeAddendum().dataLocation();
  94. *where = JS_DATA_TO_FUNC_PTR(Trampolines::TrampolinePtr, result + masm.distanceOf(entry));
  95. return true;
  96. }
  97. /*
  98. * This is shamelessly copied from emitReturn, but with several changes:
  99. * - There was always at least one inline call.
  100. * - We don't know if there are activation objects or a script with nesting
  101. * state whose active frames need adjustment, so we always stub the epilogue.
  102. * - We don't know where we came from, so we don't know frame depth or PC.
  103. * - There is no stub buffer.
  104. */
  105. bool
  106. TrampolineCompiler::generateForceReturn(Assembler &masm)
  107. {
  108. /* The JSStackFrame register may have been clobbered while returning, reload it. */
  109. masm.loadPtr(FrameAddress(VMFrame::offsetOfFp), JSFrameReg);
  110. /* Perform the frame epilogue. */
  111. masm.fallibleVMCall(true, JS_FUNC_TO_DATA_PTR(void *, stubs::AnyFrameEpilogue), NULL, NULL, 0);
  112. /* Store any known return value */
  113. masm.loadValueAsComponents(UndefinedValue(), JSReturnReg_Type, JSReturnReg_Data);
  114. Jump rvalClear = masm.branchTest32(Assembler::Zero,
  115. FrameFlagsAddress(), Imm32(StackFrame::HAS_RVAL));
  116. Address rvalAddress(JSFrameReg, StackFrame::offsetOfReturnValue());
  117. masm.loadValueAsComponents(rvalAddress, JSReturnReg_Type, JSReturnReg_Data);
  118. rvalClear.linkTo(masm.label(), &masm);
  119. /* Return to the caller */
  120. masm.loadPtr(Address(JSFrameReg, StackFrame::offsetOfNcode()), Registers::ReturnReg);
  121. masm.jump(Registers::ReturnReg);
  122. return true;
  123. }
  124. #if (defined(JS_NO_FASTCALL) && defined(JS_CPU_X86)) || defined(_WIN64)
  125. bool
  126. TrampolineCompiler::generateForceReturnFast(Assembler &masm)
  127. {
  128. #ifdef _WIN64
  129. masm.addPtr(Imm32(32), Registers::StackPointer);
  130. #else
  131. // In case of no fast call, when we change the return address,
  132. // we need to make sure add esp by 8.
  133. masm.addPtr(Imm32(16), Registers::StackPointer);
  134. #endif
  135. return generateForceReturn(masm);
  136. }
  137. #endif
  138. } /* namespace mjit */
  139. } /* namespace js */