2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
31 #include "CodeBlock.h"
32 #include "Interpreter.h"
33 #include "JITInlineMethods.h"
34 #include "JITStubCall.h"
36 #include "JSFunction.h"
37 #include "ResultType.h"
38 #include "SamplingTool.h"
48 void ctiPatchNearCallByReturnAddress(ReturnAddressPtr returnAddress
, MacroAssemblerCodePtr newCalleeFunction
)
50 MacroAssembler::RepatchBuffer repatchBuffer
;
51 repatchBuffer
.relinkNearCallerToTrampoline(returnAddress
, newCalleeFunction
);
54 void ctiPatchCallByReturnAddress(ReturnAddressPtr returnAddress
, MacroAssemblerCodePtr newCalleeFunction
)
56 MacroAssembler::RepatchBuffer repatchBuffer
;
57 repatchBuffer
.relinkCallerToTrampoline(returnAddress
, newCalleeFunction
);
60 void ctiPatchCallByReturnAddress(ReturnAddressPtr returnAddress
, FunctionPtr newCalleeFunction
)
62 MacroAssembler::RepatchBuffer repatchBuffer
;
63 repatchBuffer
.relinkCallerToFunction(returnAddress
, newCalleeFunction
);
66 JIT::JIT(JSGlobalData
* globalData
, CodeBlock
* codeBlock
)
67 : m_interpreter(globalData
->interpreter
)
68 , m_globalData(globalData
)
69 , m_codeBlock(codeBlock
)
70 , m_labels(codeBlock
? codeBlock
->instructions().size() : 0)
71 , m_propertyAccessCompilationInfo(codeBlock
? codeBlock
->numberOfStructureStubInfos() : 0)
72 , m_callStructureStubCompilationInfo(codeBlock
? codeBlock
->numberOfCallLinkInfos() : 0)
73 , m_bytecodeIndex((unsigned)-1)
74 , m_lastResultBytecodeRegister(std::numeric_limits
<int>::max())
75 , m_jumpTargetsPosition(0)
79 void JIT::compileOpStrictEq(Instruction
* currentInstruction
, CompileOpStrictEqType type
)
81 unsigned dst
= currentInstruction
[1].u
.operand
;
82 unsigned src1
= currentInstruction
[2].u
.operand
;
83 unsigned src2
= currentInstruction
[3].u
.operand
;
85 emitGetVirtualRegisters(src1
, regT0
, src2
, regT1
);
87 // Jump to a slow case if either operand is a number, or if both are JSCell*s.
90 addSlowCase(emitJumpIfJSCell(regT2
));
91 addSlowCase(emitJumpIfImmediateNumber(regT2
));
93 if (type
== OpStrictEq
)
94 set32(Equal
, regT1
, regT0
, regT0
);
96 set32(NotEqual
, regT1
, regT0
, regT0
);
97 emitTagAsBoolImmediate(regT0
);
99 emitPutVirtualRegister(dst
);
102 void JIT::emitTimeoutCheck()
104 Jump skipTimeout
= branchSub32(NonZero
, Imm32(1), timeoutCheckRegister
);
105 JITStubCall(this, JITStubs::cti_timeout_check
).call(timeoutCheckRegister
);
106 skipTimeout
.link(this);
108 killLastResultRegister();
112 #define NEXT_OPCODE(name) \
113 m_bytecodeIndex += OPCODE_LENGTH(name); \
116 #define DEFINE_BINARY_OP(name) \
118 JITStubCall stubCall(this, JITStubs::cti_##name); \
119 stubCall.addArgument(currentInstruction[2].u.operand, regT2); \
120 stubCall.addArgument(currentInstruction[3].u.operand, regT2); \
121 stubCall.call(currentInstruction[1].u.operand); \
125 #define DEFINE_UNARY_OP(name) \
127 JITStubCall stubCall(this, JITStubs::cti_##name); \
128 stubCall.addArgument(currentInstruction[2].u.operand, regT2); \
129 stubCall.call(currentInstruction[1].u.operand); \
133 #define DEFINE_OP(name) \
135 emit_##name(currentInstruction); \
139 #define DEFINE_SLOWCASE_OP(name) \
141 emitSlow_##name(currentInstruction, iter); \
145 void JIT::privateCompileMainPass()
147 Instruction
* instructionsBegin
= m_codeBlock
->instructions().begin();
148 unsigned instructionCount
= m_codeBlock
->instructions().size();
150 m_propertyAccessInstructionIndex
= 0;
151 m_globalResolveInfoIndex
= 0;
152 m_callLinkInfoIndex
= 0;
154 for (m_bytecodeIndex
= 0; m_bytecodeIndex
< instructionCount
; ) {
155 Instruction
* currentInstruction
= instructionsBegin
+ m_bytecodeIndex
;
156 ASSERT_WITH_MESSAGE(m_interpreter
->isOpcode(currentInstruction
->u
.opcode
), "privateCompileMainPass gone bad @ %d", m_bytecodeIndex
);
158 #if ENABLE(OPCODE_SAMPLING)
159 if (m_bytecodeIndex
> 0) // Avoid the overhead of sampling op_enter twice.
160 sampleInstruction(currentInstruction
);
163 if (m_labels
[m_bytecodeIndex
].isUsed())
164 killLastResultRegister();
166 m_labels
[m_bytecodeIndex
] = label();
168 switch (m_interpreter
->getOpcodeID(currentInstruction
->u
.opcode
)) {
169 DEFINE_BINARY_OP(op_del_by_val
)
170 DEFINE_BINARY_OP(op_div
)
171 DEFINE_BINARY_OP(op_in
)
172 DEFINE_BINARY_OP(op_less
)
173 DEFINE_BINARY_OP(op_lesseq
)
174 DEFINE_BINARY_OP(op_urshift
)
175 DEFINE_UNARY_OP(op_get_pnames
)
176 DEFINE_UNARY_OP(op_is_boolean
)
177 DEFINE_UNARY_OP(op_is_function
)
178 DEFINE_UNARY_OP(op_is_number
)
179 DEFINE_UNARY_OP(op_is_object
)
180 DEFINE_UNARY_OP(op_is_string
)
181 DEFINE_UNARY_OP(op_is_undefined
)
182 DEFINE_UNARY_OP(op_negate
)
183 DEFINE_UNARY_OP(op_typeof
)
191 DEFINE_OP(op_call_eval
)
192 DEFINE_OP(op_call_varargs
)
194 DEFINE_OP(op_construct
)
195 DEFINE_OP(op_construct_verify
)
196 DEFINE_OP(op_convert_this
)
197 DEFINE_OP(op_init_arguments
)
198 DEFINE_OP(op_create_arguments
)
200 DEFINE_OP(op_del_by_id
)
203 DEFINE_OP(op_enter_with_activation
)
205 DEFINE_OP(op_eq_null
)
206 DEFINE_OP(op_get_by_id
)
207 DEFINE_OP(op_get_by_val
)
208 DEFINE_OP(op_get_global_var
)
209 DEFINE_OP(op_get_scoped_var
)
210 DEFINE_OP(op_instanceof
)
211 DEFINE_OP(op_jeq_null
)
214 DEFINE_OP(op_jmp_scopes
)
215 DEFINE_OP(op_jneq_null
)
216 DEFINE_OP(op_jneq_ptr
)
218 DEFINE_OP(op_jnlesseq
)
221 DEFINE_OP(op_load_varargs
)
223 DEFINE_OP(op_loop_if_less
)
224 DEFINE_OP(op_loop_if_lesseq
)
225 DEFINE_OP(op_loop_if_true
)
227 DEFINE_OP(op_method_check
)
232 DEFINE_OP(op_neq_null
)
233 DEFINE_OP(op_new_array
)
234 DEFINE_OP(op_new_error
)
235 DEFINE_OP(op_new_func
)
236 DEFINE_OP(op_new_func_exp
)
237 DEFINE_OP(op_new_object
)
238 DEFINE_OP(op_new_regexp
)
239 DEFINE_OP(op_next_pname
)
241 DEFINE_OP(op_nstricteq
)
242 DEFINE_OP(op_pop_scope
)
243 DEFINE_OP(op_post_dec
)
244 DEFINE_OP(op_post_inc
)
245 DEFINE_OP(op_pre_dec
)
246 DEFINE_OP(op_pre_inc
)
247 DEFINE_OP(op_profile_did_call
)
248 DEFINE_OP(op_profile_will_call
)
249 DEFINE_OP(op_push_new_scope
)
250 DEFINE_OP(op_push_scope
)
251 DEFINE_OP(op_put_by_id
)
252 DEFINE_OP(op_put_by_index
)
253 DEFINE_OP(op_put_by_val
)
254 DEFINE_OP(op_put_getter
)
255 DEFINE_OP(op_put_global_var
)
256 DEFINE_OP(op_put_scoped_var
)
257 DEFINE_OP(op_put_setter
)
258 DEFINE_OP(op_resolve
)
259 DEFINE_OP(op_resolve_base
)
260 DEFINE_OP(op_resolve_func
)
261 DEFINE_OP(op_resolve_global
)
262 DEFINE_OP(op_resolve_skip
)
263 DEFINE_OP(op_resolve_with_base
)
268 DEFINE_OP(op_stricteq
)
270 DEFINE_OP(op_switch_char
)
271 DEFINE_OP(op_switch_imm
)
272 DEFINE_OP(op_switch_string
)
273 DEFINE_OP(op_tear_off_activation
)
274 DEFINE_OP(op_tear_off_arguments
)
276 DEFINE_OP(op_to_jsnumber
)
277 DEFINE_OP(op_to_primitive
)
278 DEFINE_OP(op_unexpected_load
)
280 case op_get_array_length
:
281 case op_get_by_id_chain
:
282 case op_get_by_id_generic
:
283 case op_get_by_id_proto
:
284 case op_get_by_id_proto_list
:
285 case op_get_by_id_self
:
286 case op_get_by_id_self_list
:
287 case op_get_string_length
:
288 case op_put_by_id_generic
:
289 case op_put_by_id_replace
:
290 case op_put_by_id_transition
:
291 ASSERT_NOT_REACHED();
295 ASSERT(m_propertyAccessInstructionIndex
== m_codeBlock
->numberOfStructureStubInfos());
296 ASSERT(m_callLinkInfoIndex
== m_codeBlock
->numberOfCallLinkInfos());
299 // Reset this, in order to guard its use with ASSERTs.
300 m_bytecodeIndex
= (unsigned)-1;
305 void JIT::privateCompileLinkPass()
307 unsigned jmpTableCount
= m_jmpTable
.size();
308 for (unsigned i
= 0; i
< jmpTableCount
; ++i
)
309 m_jmpTable
[i
].from
.linkTo(m_labels
[m_jmpTable
[i
].toBytecodeIndex
], this);
313 void JIT::privateCompileSlowCases()
315 Instruction
* instructionsBegin
= m_codeBlock
->instructions().begin();
317 m_propertyAccessInstructionIndex
= 0;
318 m_callLinkInfoIndex
= 0;
320 for (Vector
<SlowCaseEntry
>::iterator iter
= m_slowCases
.begin(); iter
!= m_slowCases
.end();) {
321 // FIXME: enable peephole optimizations for slow cases when applicable
322 killLastResultRegister();
324 m_bytecodeIndex
= iter
->to
;
326 unsigned firstTo
= m_bytecodeIndex
;
328 Instruction
* currentInstruction
= instructionsBegin
+ m_bytecodeIndex
;
330 switch (m_interpreter
->getOpcodeID(currentInstruction
->u
.opcode
)) {
331 DEFINE_SLOWCASE_OP(op_add
)
332 DEFINE_SLOWCASE_OP(op_bitand
)
333 DEFINE_SLOWCASE_OP(op_bitnot
)
334 DEFINE_SLOWCASE_OP(op_bitor
)
335 DEFINE_SLOWCASE_OP(op_bitxor
)
336 DEFINE_SLOWCASE_OP(op_call
)
337 DEFINE_SLOWCASE_OP(op_call_eval
)
338 DEFINE_SLOWCASE_OP(op_call_varargs
)
339 DEFINE_SLOWCASE_OP(op_construct
)
340 DEFINE_SLOWCASE_OP(op_construct_verify
)
341 DEFINE_SLOWCASE_OP(op_convert_this
)
342 DEFINE_SLOWCASE_OP(op_eq
)
343 DEFINE_SLOWCASE_OP(op_get_by_id
)
344 DEFINE_SLOWCASE_OP(op_get_by_val
)
345 DEFINE_SLOWCASE_OP(op_instanceof
)
346 DEFINE_SLOWCASE_OP(op_jfalse
)
347 DEFINE_SLOWCASE_OP(op_jnless
)
348 DEFINE_SLOWCASE_OP(op_jnlesseq
)
349 DEFINE_SLOWCASE_OP(op_jtrue
)
350 DEFINE_SLOWCASE_OP(op_loop_if_less
)
351 DEFINE_SLOWCASE_OP(op_loop_if_lesseq
)
352 DEFINE_SLOWCASE_OP(op_loop_if_true
)
353 DEFINE_SLOWCASE_OP(op_lshift
)
354 DEFINE_SLOWCASE_OP(op_mod
)
355 DEFINE_SLOWCASE_OP(op_mul
)
356 DEFINE_SLOWCASE_OP(op_method_check
)
357 DEFINE_SLOWCASE_OP(op_neq
)
358 DEFINE_SLOWCASE_OP(op_not
)
359 DEFINE_SLOWCASE_OP(op_nstricteq
)
360 DEFINE_SLOWCASE_OP(op_post_dec
)
361 DEFINE_SLOWCASE_OP(op_post_inc
)
362 DEFINE_SLOWCASE_OP(op_pre_dec
)
363 DEFINE_SLOWCASE_OP(op_pre_inc
)
364 DEFINE_SLOWCASE_OP(op_put_by_id
)
365 DEFINE_SLOWCASE_OP(op_put_by_val
)
366 DEFINE_SLOWCASE_OP(op_rshift
)
367 DEFINE_SLOWCASE_OP(op_stricteq
)
368 DEFINE_SLOWCASE_OP(op_sub
)
369 DEFINE_SLOWCASE_OP(op_to_jsnumber
)
370 DEFINE_SLOWCASE_OP(op_to_primitive
)
372 ASSERT_NOT_REACHED();
375 ASSERT_WITH_MESSAGE(iter
== m_slowCases
.end() || firstTo
!= iter
->to
,"Not enough jumps linked in slow case codegen.");
376 ASSERT_WITH_MESSAGE(firstTo
== (iter
- 1)->to
, "Too many jumps linked in slow case codegen.");
378 emitJumpSlowToHot(jump(), 0);
381 #if ENABLE(JIT_OPTIMIZE_PROPERTY_ACCESS)
382 ASSERT(m_propertyAccessInstructionIndex
== m_codeBlock
->numberOfStructureStubInfos());
384 ASSERT(m_callLinkInfoIndex
== m_codeBlock
->numberOfCallLinkInfos());
387 // Reset this, in order to guard its use with ASSERTs.
388 m_bytecodeIndex
= (unsigned)-1;
392 void JIT::privateCompile()
394 sampleCodeBlock(m_codeBlock
);
395 #if ENABLE(OPCODE_SAMPLING)
396 sampleInstruction(m_codeBlock
->instructions().begin());
399 // Could use a pop_m, but would need to offset the following instruction if so.
400 preverveReturnAddressAfterCall(regT2
);
401 emitPutToCallFrameHeader(regT2
, RegisterFile::ReturnPC
);
403 Jump slowRegisterFileCheck
;
404 Label afterRegisterFileCheck
;
405 if (m_codeBlock
->codeType() == FunctionCode
) {
406 // In the case of a fast linked call, we do not set this up in the caller.
407 emitPutImmediateToCallFrameHeader(m_codeBlock
, RegisterFile::CodeBlock
);
409 peek(regT0
, OBJECT_OFFSETOF(JITStackFrame
, registerFile
) / sizeof (void*));
410 addPtr(Imm32(m_codeBlock
->m_numCalleeRegisters
* sizeof(Register
)), callFrameRegister
, regT1
);
412 slowRegisterFileCheck
= branchPtr(Above
, regT1
, Address(regT0
, OBJECT_OFFSETOF(RegisterFile
, m_end
)));
413 afterRegisterFileCheck
= label();
416 privateCompileMainPass();
417 privateCompileLinkPass();
418 privateCompileSlowCases();
420 if (m_codeBlock
->codeType() == FunctionCode
) {
421 slowRegisterFileCheck
.link(this);
423 JITStubCall(this, JITStubs::cti_register_file_check
).call();
425 m_bytecodeIndex
= (unsigned)-1; // Reset this, in order to guard its use with ASSERTs.
427 jump(afterRegisterFileCheck
);
430 ASSERT(m_jmpTable
.isEmpty());
432 LinkBuffer
patchBuffer(this, m_globalData
->executableAllocator
.poolForSize(m_assembler
.size()));
434 // Translate vPC offsets into addresses in JIT generated code, for switch tables.
435 for (unsigned i
= 0; i
< m_switches
.size(); ++i
) {
436 SwitchRecord record
= m_switches
[i
];
437 unsigned bytecodeIndex
= record
.bytecodeIndex
;
439 if (record
.type
!= SwitchRecord::String
) {
440 ASSERT(record
.type
== SwitchRecord::Immediate
|| record
.type
== SwitchRecord::Character
);
441 ASSERT(record
.jumpTable
.simpleJumpTable
->branchOffsets
.size() == record
.jumpTable
.simpleJumpTable
->ctiOffsets
.size());
443 record
.jumpTable
.simpleJumpTable
->ctiDefault
= patchBuffer
.locationOf(m_labels
[bytecodeIndex
+ 3 + record
.defaultOffset
]);
445 for (unsigned j
= 0; j
< record
.jumpTable
.simpleJumpTable
->branchOffsets
.size(); ++j
) {
446 unsigned offset
= record
.jumpTable
.simpleJumpTable
->branchOffsets
[j
];
447 record
.jumpTable
.simpleJumpTable
->ctiOffsets
[j
] = offset
? patchBuffer
.locationOf(m_labels
[bytecodeIndex
+ 3 + offset
]) : record
.jumpTable
.simpleJumpTable
->ctiDefault
;
450 ASSERT(record
.type
== SwitchRecord::String
);
452 record
.jumpTable
.stringJumpTable
->ctiDefault
= patchBuffer
.locationOf(m_labels
[bytecodeIndex
+ 3 + record
.defaultOffset
]);
454 StringJumpTable::StringOffsetTable::iterator end
= record
.jumpTable
.stringJumpTable
->offsetTable
.end();
455 for (StringJumpTable::StringOffsetTable::iterator it
= record
.jumpTable
.stringJumpTable
->offsetTable
.begin(); it
!= end
; ++it
) {
456 unsigned offset
= it
->second
.branchOffset
;
457 it
->second
.ctiOffset
= offset
? patchBuffer
.locationOf(m_labels
[bytecodeIndex
+ 3 + offset
]) : record
.jumpTable
.stringJumpTable
->ctiDefault
;
462 for (size_t i
= 0; i
< m_codeBlock
->numberOfExceptionHandlers(); ++i
) {
463 HandlerInfo
& handler
= m_codeBlock
->exceptionHandler(i
);
464 handler
.nativeCode
= patchBuffer
.locationOf(m_labels
[handler
.target
]);
467 for (Vector
<CallRecord
>::iterator iter
= m_calls
.begin(); iter
!= m_calls
.end(); ++iter
) {
469 patchBuffer
.link(iter
->from
, FunctionPtr(iter
->to
));
472 if (m_codeBlock
->hasExceptionInfo()) {
473 m_codeBlock
->callReturnIndexVector().reserveCapacity(m_calls
.size());
474 for (Vector
<CallRecord
>::iterator iter
= m_calls
.begin(); iter
!= m_calls
.end(); ++iter
)
475 m_codeBlock
->callReturnIndexVector().append(CallReturnOffsetToBytecodeIndex(patchBuffer
.returnAddressOffset(iter
->from
), iter
->bytecodeIndex
));
478 // Link absolute addresses for jsr
479 for (Vector
<JSRInfo
>::iterator iter
= m_jsrSites
.begin(); iter
!= m_jsrSites
.end(); ++iter
)
480 patchBuffer
.patch(iter
->storeLocation
, patchBuffer
.locationOf(iter
->target
).executableAddress());
482 #if ENABLE(JIT_OPTIMIZE_PROPERTY_ACCESS)
483 for (unsigned i
= 0; i
< m_codeBlock
->numberOfStructureStubInfos(); ++i
) {
484 StructureStubInfo
& info
= m_codeBlock
->structureStubInfo(i
);
485 info
.callReturnLocation
= patchBuffer
.locationOf(m_propertyAccessCompilationInfo
[i
].callReturnLocation
);
486 info
.hotPathBegin
= patchBuffer
.locationOf(m_propertyAccessCompilationInfo
[i
].hotPathBegin
);
489 #if ENABLE(JIT_OPTIMIZE_CALL)
490 for (unsigned i
= 0; i
< m_codeBlock
->numberOfCallLinkInfos(); ++i
) {
491 CallLinkInfo
& info
= m_codeBlock
->callLinkInfo(i
);
492 info
.callReturnLocation
= patchBuffer
.locationOfNearCall(m_callStructureStubCompilationInfo
[i
].callReturnLocation
);
493 info
.hotPathBegin
= patchBuffer
.locationOf(m_callStructureStubCompilationInfo
[i
].hotPathBegin
);
494 info
.hotPathOther
= patchBuffer
.locationOfNearCall(m_callStructureStubCompilationInfo
[i
].hotPathOther
);
497 unsigned methodCallCount
= m_methodCallCompilationInfo
.size();
498 m_codeBlock
->addMethodCallLinkInfos(methodCallCount
);
499 for (unsigned i
= 0; i
< methodCallCount
; ++i
) {
500 MethodCallLinkInfo
& info
= m_codeBlock
->methodCallLinkInfo(i
);
501 info
.structureLabel
= patchBuffer
.locationOf(m_methodCallCompilationInfo
[i
].structureToCompare
);
502 info
.callReturnLocation
= m_codeBlock
->structureStubInfo(m_methodCallCompilationInfo
[i
].propertyAccessIndex
).callReturnLocation
;
505 m_codeBlock
->setJITCode(patchBuffer
.finalizeCode());
508 void JIT::privateCompileCTIMachineTrampolines(RefPtr
<ExecutablePool
>* executablePool
, JSGlobalData
* globalData
, CodePtr
* ctiArrayLengthTrampoline
, CodePtr
* ctiStringLengthTrampoline
, CodePtr
* ctiVirtualCallPreLink
, CodePtr
* ctiVirtualCallLink
, CodePtr
* ctiVirtualCall
, CodePtr
* ctiNativeCallThunk
)
510 #if ENABLE(JIT_OPTIMIZE_PROPERTY_ACCESS)
511 // (1) The first function provides fast property access for array length
512 Label arrayLengthBegin
= align();
514 // Check eax is an array
515 Jump array_failureCases1
= emitJumpIfNotJSCell(regT0
);
516 Jump array_failureCases2
= branchPtr(NotEqual
, Address(regT0
), ImmPtr(m_globalData
->jsArrayVPtr
));
518 // Checks out okay! - get the length from the storage
519 loadPtr(Address(regT0
, OBJECT_OFFSETOF(JSArray
, m_storage
)), regT0
);
520 load32(Address(regT0
, OBJECT_OFFSETOF(ArrayStorage
, m_length
)), regT0
);
522 Jump array_failureCases3
= branch32(Above
, regT0
, Imm32(JSImmediate::maxImmediateInt
));
524 // regT0 contains a 64 bit value (is positive, is zero extended) so we don't need sign extend here.
525 emitFastArithIntToImmNoCheck(regT0
, regT0
);
529 // (2) The second function provides fast property access for string length
530 Label stringLengthBegin
= align();
532 // Check eax is a string
533 Jump string_failureCases1
= emitJumpIfNotJSCell(regT0
);
534 Jump string_failureCases2
= branchPtr(NotEqual
, Address(regT0
), ImmPtr(m_globalData
->jsStringVPtr
));
536 // Checks out okay! - get the length from the Ustring.
537 loadPtr(Address(regT0
, OBJECT_OFFSETOF(JSString
, m_value
) + OBJECT_OFFSETOF(UString
, m_rep
)), regT0
);
538 load32(Address(regT0
, OBJECT_OFFSETOF(UString::Rep
, len
)), regT0
);
540 Jump string_failureCases3
= branch32(Above
, regT0
, Imm32(JSImmediate::maxImmediateInt
));
542 // regT0 contains a 64 bit value (is positive, is zero extended) so we don't need sign extend here.
543 emitFastArithIntToImmNoCheck(regT0
, regT0
);
548 // (3) Trampolines for the slow cases of op_call / op_call_eval / op_construct.
549 COMPILE_ASSERT(sizeof(CodeType
) == 4, CodeTypeEnumMustBe32Bit
);
551 Label virtualCallPreLinkBegin
= align();
553 // Load the callee CodeBlock* into eax
554 loadPtr(Address(regT2
, OBJECT_OFFSETOF(JSFunction
, m_body
)), regT3
);
555 loadPtr(Address(regT3
, OBJECT_OFFSETOF(FunctionBodyNode
, m_code
)), regT0
);
556 Jump hasCodeBlock1
= branchTestPtr(NonZero
, regT0
);
557 preverveReturnAddressAfterCall(regT3
);
558 restoreArgumentReference();
559 Call callJSFunction1
= call();
560 emitGetJITStubArg(1, regT2
);
561 emitGetJITStubArg(3, regT1
);
562 restoreReturnAddressBeforeReturn(regT3
);
563 hasCodeBlock1
.link(this);
565 Jump isNativeFunc1
= branch32(Equal
, Address(regT0
, OBJECT_OFFSETOF(CodeBlock
, m_codeType
)), Imm32(NativeCode
));
567 // Check argCount matches callee arity.
568 Jump arityCheckOkay1
= branch32(Equal
, Address(regT0
, OBJECT_OFFSETOF(CodeBlock
, m_numParameters
)), regT1
);
569 preverveReturnAddressAfterCall(regT3
);
570 emitPutJITStubArg(regT3
, 2);
571 emitPutJITStubArg(regT0
, 4);
572 restoreArgumentReference();
573 Call callArityCheck1
= call();
574 move(regT1
, callFrameRegister
);
575 emitGetJITStubArg(1, regT2
);
576 emitGetJITStubArg(3, regT1
);
577 restoreReturnAddressBeforeReturn(regT3
);
578 arityCheckOkay1
.link(this);
579 isNativeFunc1
.link(this);
581 compileOpCallInitializeCallFrame();
583 preverveReturnAddressAfterCall(regT3
);
584 emitPutJITStubArg(regT3
, 2);
585 restoreArgumentReference();
586 Call callDontLazyLinkCall
= call();
587 emitGetJITStubArg(1, regT2
);
588 restoreReturnAddressBeforeReturn(regT3
);
592 Label virtualCallLinkBegin
= align();
594 // Load the callee CodeBlock* into eax
595 loadPtr(Address(regT2
, OBJECT_OFFSETOF(JSFunction
, m_body
)), regT3
);
596 loadPtr(Address(regT3
, OBJECT_OFFSETOF(FunctionBodyNode
, m_code
)), regT0
);
597 Jump hasCodeBlock2
= branchTestPtr(NonZero
, regT0
);
598 preverveReturnAddressAfterCall(regT3
);
599 restoreArgumentReference();
600 Call callJSFunction2
= call();
601 emitGetJITStubArg(1, regT2
);
602 emitGetJITStubArg(3, regT1
);
603 restoreReturnAddressBeforeReturn(regT3
);
604 hasCodeBlock2
.link(this);
606 Jump isNativeFunc2
= branch32(Equal
, Address(regT0
, OBJECT_OFFSETOF(CodeBlock
, m_codeType
)), Imm32(NativeCode
));
608 // Check argCount matches callee arity.
609 Jump arityCheckOkay2
= branch32(Equal
, Address(regT0
, OBJECT_OFFSETOF(CodeBlock
, m_numParameters
)), regT1
);
610 preverveReturnAddressAfterCall(regT3
);
611 emitPutJITStubArg(regT3
, 2);
612 emitPutJITStubArg(regT0
, 4);
613 restoreArgumentReference();
614 Call callArityCheck2
= call();
615 move(regT1
, callFrameRegister
);
616 emitGetJITStubArg(1, regT2
);
617 emitGetJITStubArg(3, regT1
);
618 restoreReturnAddressBeforeReturn(regT3
);
619 arityCheckOkay2
.link(this);
620 isNativeFunc2
.link(this);
622 compileOpCallInitializeCallFrame();
624 preverveReturnAddressAfterCall(regT3
);
625 emitPutJITStubArg(regT3
, 2);
626 restoreArgumentReference();
627 Call callLazyLinkCall
= call();
628 restoreReturnAddressBeforeReturn(regT3
);
632 Label virtualCallBegin
= align();
634 // Load the callee CodeBlock* into eax
635 loadPtr(Address(regT2
, OBJECT_OFFSETOF(JSFunction
, m_body
)), regT3
);
636 loadPtr(Address(regT3
, OBJECT_OFFSETOF(FunctionBodyNode
, m_code
)), regT0
);
637 Jump hasCodeBlock3
= branchTestPtr(NonZero
, regT0
);
638 preverveReturnAddressAfterCall(regT3
);
639 restoreArgumentReference();
640 Call callJSFunction3
= call();
641 emitGetJITStubArg(1, regT2
);
642 emitGetJITStubArg(3, regT1
);
643 restoreReturnAddressBeforeReturn(regT3
);
644 loadPtr(Address(regT2
, OBJECT_OFFSETOF(JSFunction
, m_body
)), regT3
); // reload the function body nody, so we can reload the code pointer.
645 hasCodeBlock3
.link(this);
647 Jump isNativeFunc3
= branch32(Equal
, Address(regT0
, OBJECT_OFFSETOF(CodeBlock
, m_codeType
)), Imm32(NativeCode
));
649 // Check argCount matches callee arity.
650 Jump arityCheckOkay3
= branch32(Equal
, Address(regT0
, OBJECT_OFFSETOF(CodeBlock
, m_numParameters
)), regT1
);
651 preverveReturnAddressAfterCall(regT3
);
652 emitPutJITStubArg(regT3
, 2);
653 emitPutJITStubArg(regT0
, 4);
654 restoreArgumentReference();
655 Call callArityCheck3
= call();
656 move(regT1
, callFrameRegister
);
657 emitGetJITStubArg(1, regT2
);
658 emitGetJITStubArg(3, regT1
);
659 restoreReturnAddressBeforeReturn(regT3
);
660 loadPtr(Address(regT2
, OBJECT_OFFSETOF(JSFunction
, m_body
)), regT3
); // reload the function body nody, so we can reload the code pointer.
661 arityCheckOkay3
.link(this);
662 isNativeFunc3
.link(this);
664 // load ctiCode from the new codeBlock.
665 loadPtr(Address(regT3
, OBJECT_OFFSETOF(FunctionBodyNode
, m_jitCode
)), regT0
);
667 compileOpCallInitializeCallFrame();
671 Label nativeCallThunk
= align();
672 preverveReturnAddressAfterCall(regT0
);
673 emitPutToCallFrameHeader(regT0
, RegisterFile::ReturnPC
); // Push return address
675 // Load caller frame's scope chain into this callframe so that whatever we call can
676 // get to its global data.
677 emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame
, regT1
);
678 emitGetFromCallFrameHeaderPtr(RegisterFile::ScopeChain
, regT1
, regT1
);
679 emitPutToCallFrameHeader(regT1
, RegisterFile::ScopeChain
);
683 emitGetFromCallFrameHeader32(RegisterFile::ArgumentCount
, X86::ecx
);
685 // Allocate stack space for our arglist
686 subPtr(Imm32(sizeof(ArgList
)), stackPointerRegister
);
687 COMPILE_ASSERT((sizeof(ArgList
) & 0xf) == 0, ArgList_should_by_16byte_aligned
);
690 subPtr(Imm32(1), X86::ecx
); // Don't include 'this' in argcount
693 storePtr(X86::ecx
, Address(stackPointerRegister
, OBJECT_OFFSETOF(ArgList
, m_argCount
)));
695 // Calculate the start of the callframe header, and store in edx
696 addPtr(Imm32(-RegisterFile::CallFrameHeaderSize
* (int32_t)sizeof(Register
)), callFrameRegister
, X86::edx
);
698 // Calculate start of arguments as callframe header - sizeof(Register) * argcount (ecx)
699 mul32(Imm32(sizeof(Register
)), X86::ecx
, X86::ecx
);
700 subPtr(X86::ecx
, X86::edx
);
702 // push pointer to arguments
703 storePtr(X86::edx
, Address(stackPointerRegister
, OBJECT_OFFSETOF(ArgList
, m_args
)));
705 // ArgList is passed by reference so is stackPointerRegister
706 move(stackPointerRegister
, X86::ecx
);
708 // edx currently points to the first argument, edx-sizeof(Register) points to 'this'
709 loadPtr(Address(X86::edx
, -(int32_t)sizeof(Register
)), X86::edx
);
711 emitGetFromCallFrameHeaderPtr(RegisterFile::Callee
, X86::esi
);
713 move(callFrameRegister
, X86::edi
);
715 call(Address(X86::esi
, OBJECT_OFFSETOF(JSFunction
, m_data
)));
717 addPtr(Imm32(sizeof(ArgList
)), stackPointerRegister
);
719 emitGetFromCallFrameHeader32(RegisterFile::ArgumentCount
, regT0
);
721 /* We have two structs that we use to describe the stackframe we set up for our
722 * call to native code. NativeCallFrameStructure describes the how we set up the stack
723 * in advance of the call. NativeFunctionCalleeSignature describes the callframe
724 * as the native code expects it. We do this as we are using the fastcall calling
725 * convention which results in the callee popping its arguments off the stack, but
726 * not the rest of the callframe so we need a nice way to ensure we increment the
727 * stack pointer by the right amount after the call.
729 #if COMPILER(MSVC) || PLATFORM(LINUX)
730 struct NativeCallFrameStructure
{
731 // CallFrame* callFrame; // passed in EDX
738 struct NativeFunctionCalleeSignature
{
744 struct NativeCallFrameStructure
{
745 // CallFrame* callFrame; // passed in ECX
746 // JSObject* callee; // passed in EDX
751 struct NativeFunctionCalleeSignature
{
756 const int NativeCallFrameSize
= (sizeof(NativeCallFrameStructure
) + 15) & ~15;
757 // Allocate system stack frame
758 subPtr(Imm32(NativeCallFrameSize
), stackPointerRegister
);
761 subPtr(Imm32(1), regT0
); // Don't include 'this' in argcount
764 storePtr(regT0
, Address(stackPointerRegister
, OBJECT_OFFSETOF(NativeCallFrameStructure
, args
) + OBJECT_OFFSETOF(ArgList
, m_argCount
)));
766 // Calculate the start of the callframe header, and store in regT1
767 addPtr(Imm32(-RegisterFile::CallFrameHeaderSize
* (int)sizeof(Register
)), callFrameRegister
, regT1
);
769 // Calculate start of arguments as callframe header - sizeof(Register) * argcount (regT0)
770 mul32(Imm32(sizeof(Register
)), regT0
, regT0
);
771 subPtr(regT0
, regT1
);
772 storePtr(regT1
, Address(stackPointerRegister
, OBJECT_OFFSETOF(NativeCallFrameStructure
, args
) + OBJECT_OFFSETOF(ArgList
, m_args
)));
774 // ArgList is passed by reference so is stackPointerRegister + 4 * sizeof(Register)
775 addPtr(Imm32(OBJECT_OFFSETOF(NativeCallFrameStructure
, args
)), stackPointerRegister
, regT0
);
776 storePtr(regT0
, Address(stackPointerRegister
, OBJECT_OFFSETOF(NativeCallFrameStructure
, argPointer
)));
778 // regT1 currently points to the first argument, regT1 - sizeof(Register) points to 'this'
779 loadPtr(Address(regT1
, -(int)sizeof(Register
)), regT1
);
780 storePtr(regT1
, Address(stackPointerRegister
, OBJECT_OFFSETOF(NativeCallFrameStructure
, thisValue
)));
782 #if COMPILER(MSVC) || PLATFORM(LINUX)
783 // ArgList is passed by reference so is stackPointerRegister + 4 * sizeof(Register)
784 addPtr(Imm32(OBJECT_OFFSETOF(NativeCallFrameStructure
, result
)), stackPointerRegister
, X86::ecx
);
787 emitGetFromCallFrameHeaderPtr(RegisterFile::Callee
, X86::eax
);
788 storePtr(X86::eax
, Address(stackPointerRegister
, OBJECT_OFFSETOF(NativeCallFrameStructure
, callee
)));
791 move(callFrameRegister
, X86::edx
);
793 call(Address(X86::eax
, OBJECT_OFFSETOF(JSFunction
, m_data
)));
795 // JSValue is a non-POD type
796 loadPtr(Address(X86::eax
), X86::eax
);
799 emitGetFromCallFrameHeaderPtr(RegisterFile::Callee
, X86::edx
);
802 move(callFrameRegister
, X86::ecx
);
803 call(Address(X86::edx
, OBJECT_OFFSETOF(JSFunction
, m_data
)));
806 // We've put a few temporaries on the stack in addition to the actual arguments
807 // so pull them off now
808 addPtr(Imm32(NativeCallFrameSize
- sizeof(NativeFunctionCalleeSignature
)), stackPointerRegister
);
810 #elif ENABLE(JIT_OPTIMIZE_NATIVE_CALL)
811 #error "JIT_OPTIMIZE_NATIVE_CALL not yet supported on this platform."
816 // Check for an exception
817 loadPtr(&(globalData
->exception
), regT2
);
818 Jump exceptionHandler
= branchTestPtr(NonZero
, regT2
);
820 // Grab the return address.
821 emitGetFromCallFrameHeaderPtr(RegisterFile::ReturnPC
, regT1
);
823 // Restore our caller's "r".
824 emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame
, callFrameRegister
);
827 restoreReturnAddressBeforeReturn(regT1
);
830 // Handle an exception
831 exceptionHandler
.link(this);
832 // Grab the return address.
833 emitGetFromCallFrameHeaderPtr(RegisterFile::ReturnPC
, regT1
);
834 move(ImmPtr(&globalData
->exceptionLocation
), regT2
);
835 storePtr(regT1
, regT2
);
836 move(ImmPtr(reinterpret_cast<void*>(ctiVMThrowTrampoline
)), regT2
);
837 emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame
, callFrameRegister
);
838 poke(callFrameRegister
, OBJECT_OFFSETOF(struct JITStackFrame
, callFrame
) / sizeof (void*));
839 restoreReturnAddressBeforeReturn(regT2
);
843 #if ENABLE(JIT_OPTIMIZE_PROPERTY_ACCESS)
844 Call array_failureCases1Call
= makeTailRecursiveCall(array_failureCases1
);
845 Call array_failureCases2Call
= makeTailRecursiveCall(array_failureCases2
);
846 Call array_failureCases3Call
= makeTailRecursiveCall(array_failureCases3
);
847 Call string_failureCases1Call
= makeTailRecursiveCall(string_failureCases1
);
848 Call string_failureCases2Call
= makeTailRecursiveCall(string_failureCases2
);
849 Call string_failureCases3Call
= makeTailRecursiveCall(string_failureCases3
);
852 // All trampolines constructed! copy the code, link up calls, and set the pointers on the Machine object.
853 LinkBuffer
patchBuffer(this, m_globalData
->executableAllocator
.poolForSize(m_assembler
.size()));
855 #if ENABLE(JIT_OPTIMIZE_PROPERTY_ACCESS)
856 patchBuffer
.link(array_failureCases1Call
, FunctionPtr(JITStubs::cti_op_get_by_id_array_fail
));
857 patchBuffer
.link(array_failureCases2Call
, FunctionPtr(JITStubs::cti_op_get_by_id_array_fail
));
858 patchBuffer
.link(array_failureCases3Call
, FunctionPtr(JITStubs::cti_op_get_by_id_array_fail
));
859 patchBuffer
.link(string_failureCases1Call
, FunctionPtr(JITStubs::cti_op_get_by_id_string_fail
));
860 patchBuffer
.link(string_failureCases2Call
, FunctionPtr(JITStubs::cti_op_get_by_id_string_fail
));
861 patchBuffer
.link(string_failureCases3Call
, FunctionPtr(JITStubs::cti_op_get_by_id_string_fail
));
863 patchBuffer
.link(callArityCheck1
, FunctionPtr(JITStubs::cti_op_call_arityCheck
));
864 patchBuffer
.link(callArityCheck2
, FunctionPtr(JITStubs::cti_op_call_arityCheck
));
865 patchBuffer
.link(callArityCheck3
, FunctionPtr(JITStubs::cti_op_call_arityCheck
));
866 patchBuffer
.link(callJSFunction1
, FunctionPtr(JITStubs::cti_op_call_JSFunction
));
867 patchBuffer
.link(callJSFunction2
, FunctionPtr(JITStubs::cti_op_call_JSFunction
));
868 patchBuffer
.link(callJSFunction3
, FunctionPtr(JITStubs::cti_op_call_JSFunction
));
869 patchBuffer
.link(callDontLazyLinkCall
, FunctionPtr(JITStubs::cti_vm_dontLazyLinkCall
));
870 patchBuffer
.link(callLazyLinkCall
, FunctionPtr(JITStubs::cti_vm_lazyLinkCall
));
872 CodeRef finalCode
= patchBuffer
.finalizeCode();
873 *executablePool
= finalCode
.m_executablePool
;
875 *ctiVirtualCallPreLink
= trampolineAt(finalCode
, virtualCallPreLinkBegin
);
876 *ctiVirtualCallLink
= trampolineAt(finalCode
, virtualCallLinkBegin
);
877 *ctiVirtualCall
= trampolineAt(finalCode
, virtualCallBegin
);
878 *ctiNativeCallThunk
= trampolineAt(finalCode
, nativeCallThunk
);
879 #if ENABLE(JIT_OPTIMIZE_PROPERTY_ACCESS)
880 *ctiArrayLengthTrampoline
= trampolineAt(finalCode
, arrayLengthBegin
);
881 *ctiStringLengthTrampoline
= trampolineAt(finalCode
, stringLengthBegin
);
883 UNUSED_PARAM(ctiArrayLengthTrampoline
);
884 UNUSED_PARAM(ctiStringLengthTrampoline
);
888 void JIT::emitGetVariableObjectRegister(RegisterID variableObject
, int index
, RegisterID dst
)
890 loadPtr(Address(variableObject
, OBJECT_OFFSETOF(JSVariableObject
, d
)), dst
);
891 loadPtr(Address(dst
, OBJECT_OFFSETOF(JSVariableObject::JSVariableObjectData
, registers
)), dst
);
892 loadPtr(Address(dst
, index
* sizeof(Register
)), dst
);
895 void JIT::emitPutVariableObjectRegister(RegisterID src
, RegisterID variableObject
, int index
)
897 loadPtr(Address(variableObject
, OBJECT_OFFSETOF(JSVariableObject
, d
)), variableObject
);
898 loadPtr(Address(variableObject
, OBJECT_OFFSETOF(JSVariableObject::JSVariableObjectData
, registers
)), variableObject
);
899 storePtr(src
, Address(variableObject
, index
* sizeof(Register
)));
902 void JIT::unlinkCall(CallLinkInfo
* callLinkInfo
)
904 // When the JSFunction is deleted the pointer embedded in the instruction stream will no longer be valid
905 // (and, if a new JSFunction happened to be constructed at the same location, we could get a false positive
906 // match). Reset the check so it no longer matches.
907 RepatchBuffer repatchBuffer
;
908 repatchBuffer
.repatch(callLinkInfo
->hotPathBegin
, JSValue::encode(JSValue()));
911 void JIT::linkCall(JSFunction
* callee
, CodeBlock
* calleeCodeBlock
, JITCode
& code
, CallLinkInfo
* callLinkInfo
, int callerArgCount
, JSGlobalData
* globalData
)
913 ASSERT(calleeCodeBlock
);
914 RepatchBuffer repatchBuffer
;
916 // Currently we only link calls with the exact number of arguments.
917 // If this is a native call calleeCodeBlock is null so the number of parameters is unimportant
918 if (callerArgCount
== calleeCodeBlock
->m_numParameters
|| calleeCodeBlock
->codeType() == NativeCode
) {
919 ASSERT(!callLinkInfo
->isLinked());
922 calleeCodeBlock
->addCaller(callLinkInfo
);
924 repatchBuffer
.repatch(callLinkInfo
->hotPathBegin
, callee
);
925 repatchBuffer
.relink(callLinkInfo
->hotPathOther
, code
.addressForCall());
928 // patch the call so we do not continue to try to link.
929 repatchBuffer
.relink(callLinkInfo
->callReturnLocation
, globalData
->jitStubs
.ctiVirtualCall());
934 #endif // ENABLE(JIT)
936 // This probably does not belong here; adding here for now as a quick Windows build fix.
937 #if ENABLE(ASSEMBLER)
939 #if PLATFORM(X86) && !PLATFORM(MAC)
940 JSC::MacroAssemblerX86Common::SSE2CheckState
JSC::MacroAssemblerX86Common::s_sse2CheckState
= NotCheckedSSE2
;