JITCall32_64.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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(JSVALUE32_64)
  28. #include "JIT.h"
  29. #include "Arguments.h"
  30. #include "CodeBlock.h"
  31. #include "Interpreter.h"
  32. #include "JITInlines.h"
  33. #include "JITStubCall.h"
  34. #include "JSArray.h"
  35. #include "JSFunction.h"
  36. #include "Operations.h"
  37. #include "RepatchBuffer.h"
  38. #include "ResultType.h"
  39. #include "SamplingTool.h"
  40. #include <wtf/StringPrintStream.h>
  41. #ifndef NDEBUG
  42. #include <stdio.h>
  43. #endif
  44. using namespace std;
  45. namespace JSC {
  46. void JIT::emit_op_call_put_result(Instruction* instruction)
  47. {
  48. int dst = instruction[1].u.operand;
  49. emitValueProfilingSite();
  50. emitStore(dst, regT1, regT0);
  51. }
  52. void JIT::emit_op_ret(Instruction* currentInstruction)
  53. {
  54. unsigned dst = currentInstruction[1].u.operand;
  55. emitLoad(dst, regT1, regT0);
  56. emitGetFromCallFrameHeaderPtr(JSStack::ReturnPC, regT2);
  57. emitGetFromCallFrameHeaderPtr(JSStack::CallerFrame, callFrameRegister);
  58. restoreReturnAddressBeforeReturn(regT2);
  59. ret();
  60. }
  61. void JIT::emit_op_ret_object_or_this(Instruction* currentInstruction)
  62. {
  63. unsigned result = currentInstruction[1].u.operand;
  64. unsigned thisReg = currentInstruction[2].u.operand;
  65. emitLoad(result, regT1, regT0);
  66. Jump notJSCell = branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag));
  67. loadPtr(Address(regT0, JSCell::structureOffset()), regT2);
  68. Jump notObject = emitJumpIfNotObject(regT2);
  69. emitGetFromCallFrameHeaderPtr(JSStack::ReturnPC, regT2);
  70. emitGetFromCallFrameHeaderPtr(JSStack::CallerFrame, callFrameRegister);
  71. restoreReturnAddressBeforeReturn(regT2);
  72. ret();
  73. notJSCell.link(this);
  74. notObject.link(this);
  75. emitLoad(thisReg, regT1, regT0);
  76. emitGetFromCallFrameHeaderPtr(JSStack::ReturnPC, regT2);
  77. emitGetFromCallFrameHeaderPtr(JSStack::CallerFrame, callFrameRegister);
  78. restoreReturnAddressBeforeReturn(regT2);
  79. ret();
  80. }
  81. void JIT::emitSlow_op_call(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  82. {
  83. compileOpCallSlowCase(op_call, currentInstruction, iter, m_callLinkInfoIndex++);
  84. }
  85. void JIT::emitSlow_op_call_eval(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  86. {
  87. compileOpCallSlowCase(op_call_eval, currentInstruction, iter, m_callLinkInfoIndex);
  88. }
  89. void JIT::emitSlow_op_call_varargs(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  90. {
  91. compileOpCallSlowCase(op_call_varargs, currentInstruction, iter, m_callLinkInfoIndex++);
  92. }
  93. void JIT::emitSlow_op_construct(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  94. {
  95. compileOpCallSlowCase(op_construct, currentInstruction, iter, m_callLinkInfoIndex++);
  96. }
  97. void JIT::emit_op_call(Instruction* currentInstruction)
  98. {
  99. compileOpCall(op_call, currentInstruction, m_callLinkInfoIndex++);
  100. }
  101. void JIT::emit_op_call_eval(Instruction* currentInstruction)
  102. {
  103. compileOpCall(op_call_eval, currentInstruction, m_callLinkInfoIndex);
  104. }
  105. void JIT::emit_op_call_varargs(Instruction* currentInstruction)
  106. {
  107. compileOpCall(op_call_varargs, currentInstruction, m_callLinkInfoIndex++);
  108. }
  109. void JIT::emit_op_construct(Instruction* currentInstruction)
  110. {
  111. compileOpCall(op_construct, currentInstruction, m_callLinkInfoIndex++);
  112. }
  113. void JIT::compileLoadVarargs(Instruction* instruction)
  114. {
  115. int thisValue = instruction[2].u.operand;
  116. int arguments = instruction[3].u.operand;
  117. int firstFreeRegister = instruction[4].u.operand;
  118. JumpList slowCase;
  119. JumpList end;
  120. bool canOptimize = m_codeBlock->usesArguments()
  121. && arguments == m_codeBlock->argumentsRegister()
  122. && !m_codeBlock->symbolTable()->slowArguments();
  123. if (canOptimize) {
  124. emitLoadTag(arguments, regT1);
  125. slowCase.append(branch32(NotEqual, regT1, TrustedImm32(JSValue::EmptyValueTag)));
  126. load32(payloadFor(JSStack::ArgumentCount), regT2);
  127. slowCase.append(branch32(Above, regT2, TrustedImm32(Arguments::MaxArguments + 1)));
  128. // regT2: argumentCountIncludingThis
  129. move(regT2, regT3);
  130. add32(TrustedImm32(firstFreeRegister + JSStack::CallFrameHeaderSize), regT3);
  131. lshift32(TrustedImm32(3), regT3);
  132. addPtr(callFrameRegister, regT3);
  133. // regT3: newCallFrame
  134. slowCase.append(branchPtr(Below, AbsoluteAddress(m_vm->interpreter->stack().addressOfEnd()), regT3));
  135. // Initialize ArgumentCount.
  136. store32(regT2, payloadFor(JSStack::ArgumentCount, regT3));
  137. // Initialize 'this'.
  138. emitLoad(thisValue, regT1, regT0);
  139. store32(regT0, Address(regT3, OBJECT_OFFSETOF(JSValue, u.asBits.payload) + (CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
  140. store32(regT1, Address(regT3, OBJECT_OFFSETOF(JSValue, u.asBits.tag) + (CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
  141. // Copy arguments.
  142. neg32(regT2);
  143. end.append(branchAdd32(Zero, TrustedImm32(1), regT2));
  144. // regT2: -argumentCount;
  145. Label copyLoop = label();
  146. load32(BaseIndex(callFrameRegister, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.payload) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))), regT0);
  147. load32(BaseIndex(callFrameRegister, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.tag) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))), regT1);
  148. store32(regT0, BaseIndex(regT3, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.payload) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
  149. store32(regT1, BaseIndex(regT3, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.tag) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
  150. branchAdd32(NonZero, TrustedImm32(1), regT2).linkTo(copyLoop, this);
  151. end.append(jump());
  152. }
  153. if (canOptimize)
  154. slowCase.link(this);
  155. JITStubCall stubCall(this, cti_op_load_varargs);
  156. stubCall.addArgument(thisValue);
  157. stubCall.addArgument(arguments);
  158. stubCall.addArgument(Imm32(firstFreeRegister));
  159. stubCall.call(regT3);
  160. if (canOptimize)
  161. end.link(this);
  162. }
  163. void JIT::compileCallEval()
  164. {
  165. JITStubCall stubCall(this, cti_op_call_eval); // Initializes ScopeChain; ReturnPC; CodeBlock.
  166. stubCall.call();
  167. addSlowCase(branch32(Equal, regT1, TrustedImm32(JSValue::EmptyValueTag)));
  168. emitGetFromCallFrameHeaderPtr(JSStack::CallerFrame, callFrameRegister);
  169. sampleCodeBlock(m_codeBlock);
  170. }
  171. void JIT::compileCallEvalSlowCase(Vector<SlowCaseEntry>::iterator& iter)
  172. {
  173. linkSlowCase(iter);
  174. emitLoad(JSStack::Callee, regT1, regT0);
  175. emitNakedCall(m_vm->getCTIStub(virtualCallGenerator).code());
  176. sampleCodeBlock(m_codeBlock);
  177. }
  178. void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex)
  179. {
  180. int callee = instruction[1].u.operand;
  181. /* Caller always:
  182. - Updates callFrameRegister to callee callFrame.
  183. - Initializes ArgumentCount; CallerFrame; Callee.
  184. For a JS call:
  185. - Caller initializes ScopeChain.
  186. - Callee initializes ReturnPC; CodeBlock.
  187. - Callee restores callFrameRegister before return.
  188. For a non-JS call:
  189. - Caller initializes ScopeChain; ReturnPC; CodeBlock.
  190. - Caller restores callFrameRegister after return.
  191. */
  192. if (opcodeID == op_call_varargs)
  193. compileLoadVarargs(instruction);
  194. else {
  195. int argCount = instruction[2].u.operand;
  196. int registerOffset = instruction[3].u.operand;
  197. if (opcodeID == op_call && shouldEmitProfiling()) {
  198. emitLoad(registerOffset + CallFrame::argumentOffsetIncludingThis(0), regT0, regT1);
  199. Jump done = branch32(NotEqual, regT0, TrustedImm32(JSValue::CellTag));
  200. loadPtr(Address(regT1, JSCell::structureOffset()), regT1);
  201. storePtr(regT1, instruction[5].u.arrayProfile->addressOfLastSeenStructure());
  202. done.link(this);
  203. }
  204. addPtr(TrustedImm32(registerOffset * sizeof(Register)), callFrameRegister, regT3);
  205. store32(TrustedImm32(argCount), payloadFor(JSStack::ArgumentCount, regT3));
  206. } // regT3 holds newCallFrame with ArgumentCount initialized.
  207. storePtr(TrustedImmPtr(instruction), tagFor(JSStack::ArgumentCount, callFrameRegister));
  208. emitLoad(callee, regT1, regT0); // regT1, regT0 holds callee.
  209. storePtr(callFrameRegister, Address(regT3, JSStack::CallerFrame * static_cast<int>(sizeof(Register))));
  210. emitStore(JSStack::Callee, regT1, regT0, regT3);
  211. move(regT3, callFrameRegister);
  212. if (opcodeID == op_call_eval) {
  213. compileCallEval();
  214. return;
  215. }
  216. addSlowCase(branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag)));
  217. DataLabelPtr addressOfLinkedFunctionCheck;
  218. BEGIN_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
  219. Jump slowCase = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(0));
  220. END_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
  221. addSlowCase(slowCase);
  222. ASSERT(m_callStructureStubCompilationInfo.size() == callLinkInfoIndex);
  223. m_callStructureStubCompilationInfo.append(StructureStubCompilationInfo());
  224. m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck;
  225. m_callStructureStubCompilationInfo[callLinkInfoIndex].callType = CallLinkInfo::callTypeFor(opcodeID);
  226. m_callStructureStubCompilationInfo[callLinkInfoIndex].bytecodeIndex = m_bytecodeOffset;
  227. loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scope)), regT1);
  228. emitPutCellToCallFrameHeader(regT1, JSStack::ScopeChain);
  229. m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall();
  230. sampleCodeBlock(m_codeBlock);
  231. }
  232. void JIT::compileOpCallSlowCase(OpcodeID opcodeID, Instruction*, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex)
  233. {
  234. if (opcodeID == op_call_eval) {
  235. compileCallEvalSlowCase(iter);
  236. return;
  237. }
  238. linkSlowCase(iter);
  239. linkSlowCase(iter);
  240. m_callStructureStubCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(opcodeID == op_construct ? m_vm->getCTIStub(linkConstructGenerator).code() : m_vm->getCTIStub(linkCallGenerator).code());
  241. sampleCodeBlock(m_codeBlock);
  242. }
  243. void JIT::privateCompileClosureCall(CallLinkInfo* callLinkInfo, CodeBlock* calleeCodeBlock, Structure* expectedStructure, ExecutableBase* expectedExecutable, MacroAssemblerCodePtr codePtr)
  244. {
  245. JumpList slowCases;
  246. slowCases.append(branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag)));
  247. slowCases.append(branchPtr(NotEqual, Address(regT0, JSCell::structureOffset()), TrustedImmPtr(expectedStructure)));
  248. slowCases.append(branchPtr(NotEqual, Address(regT0, JSFunction::offsetOfExecutable()), TrustedImmPtr(expectedExecutable)));
  249. loadPtr(Address(regT0, JSFunction::offsetOfScopeChain()), regT1);
  250. emitPutCellToCallFrameHeader(regT1, JSStack::ScopeChain);
  251. Call call = nearCall();
  252. Jump done = jump();
  253. slowCases.link(this);
  254. move(TrustedImmPtr(callLinkInfo->callReturnLocation.executableAddress()), regT2);
  255. restoreReturnAddressBeforeReturn(regT2);
  256. Jump slow = jump();
  257. LinkBuffer patchBuffer(*m_vm, this, m_codeBlock);
  258. patchBuffer.link(call, FunctionPtr(codePtr.executableAddress()));
  259. patchBuffer.link(done, callLinkInfo->hotPathOther.labelAtOffset(0));
  260. patchBuffer.link(slow, CodeLocationLabel(m_vm->getCTIStub(virtualCallGenerator).code()));
  261. RefPtr<ClosureCallStubRoutine> stubRoutine = adoptRef(new ClosureCallStubRoutine(
  262. FINALIZE_CODE(
  263. patchBuffer,
  264. ("Baseline closure call stub for %s, return point %p, target %p (%s)",
  265. toCString(*m_codeBlock).data(),
  266. callLinkInfo->hotPathOther.labelAtOffset(0).executableAddress(),
  267. codePtr.executableAddress(),
  268. toCString(pointerDump(calleeCodeBlock)).data())),
  269. *m_vm, m_codeBlock->ownerExecutable(), expectedStructure, expectedExecutable,
  270. callLinkInfo->codeOrigin));
  271. RepatchBuffer repatchBuffer(m_codeBlock);
  272. repatchBuffer.replaceWithJump(
  273. RepatchBuffer::startOfBranchPtrWithPatchOnRegister(callLinkInfo->hotPathBegin),
  274. CodeLocationLabel(stubRoutine->code().code()));
  275. repatchBuffer.relink(callLinkInfo->callReturnLocation, m_vm->getCTIStub(virtualCallGenerator).code());
  276. callLinkInfo->stub = stubRoutine.release();
  277. }
  278. } // namespace JSC
  279. #endif // USE(JSVALUE32_64)
  280. #endif // ENABLE(JIT)