JITCall.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (C) 2008, 2013 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. #include "config.h"
  26. #if ENABLE(JIT)
  27. #if USE(JSVALUE64)
  28. #include "JIT.h"
  29. #include "Arguments.h"
  30. #include "CodeBlock.h"
  31. #include "JITInlines.h"
  32. #include "JITStubCall.h"
  33. #include "JSArray.h"
  34. #include "JSFunction.h"
  35. #include "Interpreter.h"
  36. #include "Operations.h"
  37. #include "RepatchBuffer.h"
  38. #include "ResultType.h"
  39. #include "SamplingTool.h"
  40. #include "ThunkGenerators.h"
  41. #include <wtf/StringPrintStream.h>
  42. #ifndef NDEBUG
  43. #include <stdio.h>
  44. #endif
  45. using namespace std;
  46. namespace JSC {
  47. void JIT::emit_op_call_put_result(Instruction* instruction)
  48. {
  49. int dst = instruction[1].u.operand;
  50. emitValueProfilingSite();
  51. emitPutVirtualRegister(dst);
  52. if (canBeOptimizedOrInlined())
  53. killLastResultRegister(); // Make lastResultRegister tracking simpler in the DFG.
  54. }
  55. void JIT::compileLoadVarargs(Instruction* instruction)
  56. {
  57. int thisValue = instruction[2].u.operand;
  58. int arguments = instruction[3].u.operand;
  59. int firstFreeRegister = instruction[4].u.operand;
  60. killLastResultRegister();
  61. JumpList slowCase;
  62. JumpList end;
  63. bool canOptimize = m_codeBlock->usesArguments()
  64. && arguments == m_codeBlock->argumentsRegister()
  65. && !m_codeBlock->symbolTable()->slowArguments();
  66. if (canOptimize) {
  67. emitGetVirtualRegister(arguments, regT0);
  68. slowCase.append(branch64(NotEqual, regT0, TrustedImm64(JSValue::encode(JSValue()))));
  69. emitGetFromCallFrameHeader32(JSStack::ArgumentCount, regT0);
  70. slowCase.append(branch32(Above, regT0, TrustedImm32(Arguments::MaxArguments + 1)));
  71. // regT0: argumentCountIncludingThis
  72. move(regT0, regT1);
  73. add32(TrustedImm32(firstFreeRegister + JSStack::CallFrameHeaderSize), regT1);
  74. lshift32(TrustedImm32(3), regT1);
  75. addPtr(callFrameRegister, regT1);
  76. // regT1: newCallFrame
  77. slowCase.append(branchPtr(Below, AbsoluteAddress(m_vm->interpreter->stack().addressOfEnd()), regT1));
  78. // Initialize ArgumentCount.
  79. store32(regT0, Address(regT1, JSStack::ArgumentCount * static_cast<int>(sizeof(Register)) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload)));
  80. // Initialize 'this'.
  81. emitGetVirtualRegister(thisValue, regT2);
  82. store64(regT2, Address(regT1, CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register))));
  83. // Copy arguments.
  84. neg32(regT0);
  85. signExtend32ToPtr(regT0, regT0);
  86. end.append(branchAdd64(Zero, TrustedImm32(1), regT0));
  87. // regT0: -argumentCount
  88. Label copyLoop = label();
  89. load64(BaseIndex(callFrameRegister, regT0, TimesEight, CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register))), regT2);
  90. store64(regT2, BaseIndex(regT1, regT0, TimesEight, CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register))));
  91. branchAdd64(NonZero, TrustedImm32(1), regT0).linkTo(copyLoop, this);
  92. end.append(jump());
  93. }
  94. if (canOptimize)
  95. slowCase.link(this);
  96. JITStubCall stubCall(this, cti_op_load_varargs);
  97. stubCall.addArgument(thisValue, regT0);
  98. stubCall.addArgument(arguments, regT0);
  99. stubCall.addArgument(Imm32(firstFreeRegister));
  100. stubCall.call(regT1);
  101. if (canOptimize)
  102. end.link(this);
  103. }
  104. void JIT::compileCallEval()
  105. {
  106. JITStubCall stubCall(this, cti_op_call_eval); // Initializes ScopeChain; ReturnPC; CodeBlock.
  107. stubCall.call();
  108. addSlowCase(branch64(Equal, regT0, TrustedImm64(JSValue::encode(JSValue()))));
  109. emitGetFromCallFrameHeaderPtr(JSStack::CallerFrame, callFrameRegister);
  110. sampleCodeBlock(m_codeBlock);
  111. }
  112. void JIT::compileCallEvalSlowCase(Vector<SlowCaseEntry>::iterator& iter)
  113. {
  114. linkSlowCase(iter);
  115. emitGetFromCallFrameHeader64(JSStack::Callee, regT0);
  116. emitNakedCall(m_vm->getCTIStub(virtualCallGenerator).code());
  117. sampleCodeBlock(m_codeBlock);
  118. }
  119. void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex)
  120. {
  121. int callee = instruction[1].u.operand;
  122. /* Caller always:
  123. - Updates callFrameRegister to callee callFrame.
  124. - Initializes ArgumentCount; CallerFrame; Callee.
  125. For a JS call:
  126. - Caller initializes ScopeChain.
  127. - Callee initializes ReturnPC; CodeBlock.
  128. - Callee restores callFrameRegister before return.
  129. For a non-JS call:
  130. - Caller initializes ScopeChain; ReturnPC; CodeBlock.
  131. - Caller restores callFrameRegister after return.
  132. */
  133. if (opcodeID == op_call_varargs)
  134. compileLoadVarargs(instruction);
  135. else {
  136. int argCount = instruction[2].u.operand;
  137. int registerOffset = instruction[3].u.operand;
  138. if (opcodeID == op_call && shouldEmitProfiling()) {
  139. emitGetVirtualRegister(registerOffset + CallFrame::argumentOffsetIncludingThis(0), regT0);
  140. Jump done = emitJumpIfNotJSCell(regT0);
  141. loadPtr(Address(regT0, JSCell::structureOffset()), regT0);
  142. storePtr(regT0, instruction[5].u.arrayProfile->addressOfLastSeenStructure());
  143. done.link(this);
  144. }
  145. addPtr(TrustedImm32(registerOffset * sizeof(Register)), callFrameRegister, regT1);
  146. store32(TrustedImm32(argCount), Address(regT1, JSStack::ArgumentCount * static_cast<int>(sizeof(Register)) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload)));
  147. } // regT1 holds newCallFrame with ArgumentCount initialized.
  148. store32(TrustedImm32(instruction - m_codeBlock->instructions().begin()), Address(callFrameRegister, JSStack::ArgumentCount * static_cast<int>(sizeof(Register)) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag)));
  149. emitGetVirtualRegister(callee, regT0); // regT0 holds callee.
  150. store64(callFrameRegister, Address(regT1, JSStack::CallerFrame * static_cast<int>(sizeof(Register))));
  151. store64(regT0, Address(regT1, JSStack::Callee * static_cast<int>(sizeof(Register))));
  152. move(regT1, callFrameRegister);
  153. if (opcodeID == op_call_eval) {
  154. compileCallEval();
  155. return;
  156. }
  157. DataLabelPtr addressOfLinkedFunctionCheck;
  158. BEGIN_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
  159. Jump slowCase = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(0));
  160. END_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
  161. addSlowCase(slowCase);
  162. ASSERT(m_callStructureStubCompilationInfo.size() == callLinkInfoIndex);
  163. m_callStructureStubCompilationInfo.append(StructureStubCompilationInfo());
  164. m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck;
  165. m_callStructureStubCompilationInfo[callLinkInfoIndex].callType = CallLinkInfo::callTypeFor(opcodeID);
  166. m_callStructureStubCompilationInfo[callLinkInfoIndex].bytecodeIndex = m_bytecodeOffset;
  167. loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scope)), regT1);
  168. emitPutToCallFrameHeader(regT1, JSStack::ScopeChain);
  169. m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall();
  170. sampleCodeBlock(m_codeBlock);
  171. }
  172. void JIT::compileOpCallSlowCase(OpcodeID opcodeID, Instruction*, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex)
  173. {
  174. if (opcodeID == op_call_eval) {
  175. compileCallEvalSlowCase(iter);
  176. return;
  177. }
  178. linkSlowCase(iter);
  179. m_callStructureStubCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(opcodeID == op_construct ? m_vm->getCTIStub(linkConstructGenerator).code() : m_vm->getCTIStub(linkCallGenerator).code());
  180. sampleCodeBlock(m_codeBlock);
  181. }
  182. void JIT::privateCompileClosureCall(CallLinkInfo* callLinkInfo, CodeBlock* calleeCodeBlock, Structure* expectedStructure, ExecutableBase* expectedExecutable, MacroAssemblerCodePtr codePtr)
  183. {
  184. JumpList slowCases;
  185. slowCases.append(branchTestPtr(NonZero, regT0, tagMaskRegister));
  186. slowCases.append(branchPtr(NotEqual, Address(regT0, JSCell::structureOffset()), TrustedImmPtr(expectedStructure)));
  187. slowCases.append(branchPtr(NotEqual, Address(regT0, JSFunction::offsetOfExecutable()), TrustedImmPtr(expectedExecutable)));
  188. loadPtr(Address(regT0, JSFunction::offsetOfScopeChain()), regT1);
  189. emitPutToCallFrameHeader(regT1, JSStack::ScopeChain);
  190. Call call = nearCall();
  191. Jump done = jump();
  192. slowCases.link(this);
  193. move(TrustedImmPtr(callLinkInfo->callReturnLocation.executableAddress()), regT2);
  194. restoreReturnAddressBeforeReturn(regT2);
  195. Jump slow = jump();
  196. LinkBuffer patchBuffer(*m_vm, this, m_codeBlock);
  197. patchBuffer.link(call, FunctionPtr(codePtr.executableAddress()));
  198. patchBuffer.link(done, callLinkInfo->hotPathOther.labelAtOffset(0));
  199. patchBuffer.link(slow, CodeLocationLabel(m_vm->getCTIStub(virtualCallGenerator).code()));
  200. RefPtr<ClosureCallStubRoutine> stubRoutine = adoptRef(new ClosureCallStubRoutine(
  201. FINALIZE_CODE(
  202. patchBuffer,
  203. ("Baseline closure call stub for %s, return point %p, target %p (%s)",
  204. toCString(*m_codeBlock).data(),
  205. callLinkInfo->hotPathOther.labelAtOffset(0).executableAddress(),
  206. codePtr.executableAddress(),
  207. toCString(pointerDump(calleeCodeBlock)).data())),
  208. *m_vm, m_codeBlock->ownerExecutable(), expectedStructure, expectedExecutable,
  209. callLinkInfo->codeOrigin));
  210. RepatchBuffer repatchBuffer(m_codeBlock);
  211. repatchBuffer.replaceWithJump(
  212. RepatchBuffer::startOfBranchPtrWithPatchOnRegister(callLinkInfo->hotPathBegin),
  213. CodeLocationLabel(stubRoutine->code().code()));
  214. repatchBuffer.relink(callLinkInfo->callReturnLocation, m_vm->getCTIStub(virtualCallGenerator).code());
  215. callLinkInfo->stub = stubRoutine.release();
  216. }
  217. } // namespace JSC
  218. #endif // USE(JSVALUE64)
  219. #endif // ENABLE(JIT)