source: webkit/trunk/JavaScriptCore/jit/JIT.cpp@ 44843

Last change on this file since 44843 was 44745, checked in by [email protected], 16 years ago

2009-06-16 Sam Weinig <[email protected]>

Fix windows build.

  • jit/JIT.cpp: (JSC::JIT::JIT):
File size: 37.4 KB
Line 
1/*
2 * Copyright (C) 2008, 2009 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
26#include "config.h"
27#include "JIT.h"
28
29#if ENABLE(JIT)
30
31#include "CodeBlock.h"
32#include "Interpreter.h"
33#include "JITInlineMethods.h"
34#include "JITStubCall.h"
35#include "JSArray.h"
36#include "JSFunction.h"
37#include "ResultType.h"
38#include "SamplingTool.h"
39
40#ifndef NDEBUG
41#include <stdio.h>
42#endif
43
44using namespace std;
45
46namespace JSC {
47
48void ctiPatchNearCallByReturnAddress(ReturnAddressPtr returnAddress, MacroAssemblerCodePtr newCalleeFunction)
49{
50 MacroAssembler::RepatchBuffer repatchBuffer;
51 repatchBuffer.relinkNearCallerToTrampoline(returnAddress, newCalleeFunction);
52}
53
54void ctiPatchCallByReturnAddress(ReturnAddressPtr returnAddress, MacroAssemblerCodePtr newCalleeFunction)
55{
56 MacroAssembler::RepatchBuffer repatchBuffer;
57 repatchBuffer.relinkCallerToTrampoline(returnAddress, newCalleeFunction);
58}
59
60void ctiPatchCallByReturnAddress(ReturnAddressPtr returnAddress, FunctionPtr newCalleeFunction)
61{
62 MacroAssembler::RepatchBuffer repatchBuffer;
63 repatchBuffer.relinkCallerToFunction(returnAddress, newCalleeFunction);
64}
65
66JIT::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)
76{
77}
78
79void JIT::compileOpStrictEq(Instruction* currentInstruction, CompileOpStrictEqType type)
80{
81 unsigned dst = currentInstruction[1].u.operand;
82 unsigned src1 = currentInstruction[2].u.operand;
83 unsigned src2 = currentInstruction[3].u.operand;
84
85 emitGetVirtualRegisters(src1, regT0, src2, regT1);
86
87 // Jump to a slow case if either operand is a number, or if both are JSCell*s.
88 move(regT0, regT2);
89 orPtr(regT1, regT2);
90 addSlowCase(emitJumpIfJSCell(regT2));
91 addSlowCase(emitJumpIfImmediateNumber(regT2));
92
93 if (type == OpStrictEq)
94 set32(Equal, regT1, regT0, regT0);
95 else
96 set32(NotEqual, regT1, regT0, regT0);
97 emitTagAsBoolImmediate(regT0);
98
99 emitPutVirtualRegister(dst);
100}
101
102void JIT::emitTimeoutCheck()
103{
104 Jump skipTimeout = branchSub32(NonZero, Imm32(1), timeoutCheckRegister);
105 JITStubCall(this, JITStubs::cti_timeout_check).call(timeoutCheckRegister);
106 skipTimeout.link(this);
107
108 killLastResultRegister();
109}
110
111
112#define NEXT_OPCODE(name) \
113 m_bytecodeIndex += OPCODE_LENGTH(name); \
114 break;
115
116#define DEFINE_BINARY_OP(name) \
117 case 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); \
122 NEXT_OPCODE(name); \
123 }
124
125#define DEFINE_UNARY_OP(name) \
126 case name: { \
127 JITStubCall stubCall(this, JITStubs::cti_##name); \
128 stubCall.addArgument(currentInstruction[2].u.operand, regT2); \
129 stubCall.call(currentInstruction[1].u.operand); \
130 NEXT_OPCODE(name); \
131 }
132
133#define DEFINE_OP(name) \
134 case name: { \
135 emit_##name(currentInstruction); \
136 NEXT_OPCODE(name); \
137 }
138
139#define DEFINE_SLOWCASE_OP(name) \
140 case name: { \
141 emitSlow_##name(currentInstruction, iter); \
142 NEXT_OPCODE(name); \
143 }
144
145void JIT::privateCompileMainPass()
146{
147 Instruction* instructionsBegin = m_codeBlock->instructions().begin();
148 unsigned instructionCount = m_codeBlock->instructions().size();
149
150 m_propertyAccessInstructionIndex = 0;
151 m_globalResolveInfoIndex = 0;
152 m_callLinkInfoIndex = 0;
153
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);
157
158#if ENABLE(OPCODE_SAMPLING)
159 if (m_bytecodeIndex > 0) // Avoid the overhead of sampling op_enter twice.
160 sampleInstruction(currentInstruction);
161#endif
162
163 if (m_labels[m_bytecodeIndex].isUsed())
164 killLastResultRegister();
165
166 m_labels[m_bytecodeIndex] = label();
167
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)
184
185 DEFINE_OP(op_add)
186 DEFINE_OP(op_bitand)
187 DEFINE_OP(op_bitnot)
188 DEFINE_OP(op_bitor)
189 DEFINE_OP(op_bitxor)
190 DEFINE_OP(op_call)
191 DEFINE_OP(op_call_eval)
192 DEFINE_OP(op_call_varargs)
193 DEFINE_OP(op_catch)
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)
199 DEFINE_OP(op_debug)
200 DEFINE_OP(op_del_by_id)
201 DEFINE_OP(op_end)
202 DEFINE_OP(op_enter)
203 DEFINE_OP(op_enter_with_activation)
204 DEFINE_OP(op_eq)
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)
212 DEFINE_OP(op_jfalse)
213 DEFINE_OP(op_jmp)
214 DEFINE_OP(op_jmp_scopes)
215 DEFINE_OP(op_jneq_null)
216 DEFINE_OP(op_jneq_ptr)
217 DEFINE_OP(op_jnless)
218 DEFINE_OP(op_jnlesseq)
219 DEFINE_OP(op_jsr)
220 DEFINE_OP(op_jtrue)
221 DEFINE_OP(op_load_varargs)
222 DEFINE_OP(op_loop)
223 DEFINE_OP(op_loop_if_less)
224 DEFINE_OP(op_loop_if_lesseq)
225 DEFINE_OP(op_loop_if_true)
226 DEFINE_OP(op_lshift)
227 DEFINE_OP(op_method_check)
228 DEFINE_OP(op_mod)
229 DEFINE_OP(op_mov)
230 DEFINE_OP(op_mul)
231 DEFINE_OP(op_neq)
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)
240 DEFINE_OP(op_not)
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)
264 DEFINE_OP(op_ret)
265 DEFINE_OP(op_rshift)
266 DEFINE_OP(op_sret)
267 DEFINE_OP(op_strcat)
268 DEFINE_OP(op_stricteq)
269 DEFINE_OP(op_sub)
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)
275 DEFINE_OP(op_throw)
276 DEFINE_OP(op_to_jsnumber)
277 DEFINE_OP(op_to_primitive)
278 DEFINE_OP(op_unexpected_load)
279
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();
292 }
293 }
294
295 ASSERT(m_propertyAccessInstructionIndex == m_codeBlock->numberOfStructureStubInfos());
296 ASSERT(m_callLinkInfoIndex == m_codeBlock->numberOfCallLinkInfos());
297
298#ifndef NDEBUG
299 // Reset this, in order to guard its use with ASSERTs.
300 m_bytecodeIndex = (unsigned)-1;
301#endif
302}
303
304
305void JIT::privateCompileLinkPass()
306{
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);
310 m_jmpTable.clear();
311}
312
313void JIT::privateCompileSlowCases()
314{
315 Instruction* instructionsBegin = m_codeBlock->instructions().begin();
316
317 m_propertyAccessInstructionIndex = 0;
318 m_callLinkInfoIndex = 0;
319
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();
323
324 m_bytecodeIndex = iter->to;
325#ifndef NDEBUG
326 unsigned firstTo = m_bytecodeIndex;
327#endif
328 Instruction* currentInstruction = instructionsBegin + m_bytecodeIndex;
329
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)
371 default:
372 ASSERT_NOT_REACHED();
373 }
374
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.");
377
378 emitJumpSlowToHot(jump(), 0);
379 }
380
381#if ENABLE(JIT_OPTIMIZE_PROPERTY_ACCESS)
382 ASSERT(m_propertyAccessInstructionIndex == m_codeBlock->numberOfStructureStubInfos());
383#endif
384 ASSERT(m_callLinkInfoIndex == m_codeBlock->numberOfCallLinkInfos());
385
386#ifndef NDEBUG
387 // Reset this, in order to guard its use with ASSERTs.
388 m_bytecodeIndex = (unsigned)-1;
389#endif
390}
391
392void JIT::privateCompile()
393{
394 sampleCodeBlock(m_codeBlock);
395#if ENABLE(OPCODE_SAMPLING)
396 sampleInstruction(m_codeBlock->instructions().begin());
397#endif
398
399 // Could use a pop_m, but would need to offset the following instruction if so.
400 preverveReturnAddressAfterCall(regT2);
401 emitPutToCallFrameHeader(regT2, RegisterFile::ReturnPC);
402
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);
408
409 peek(regT0, FIELD_OFFSET(JITStackFrame, registerFile) / sizeof (void*));
410 addPtr(Imm32(m_codeBlock->m_numCalleeRegisters * sizeof(Register)), callFrameRegister, regT1);
411
412 slowRegisterFileCheck = branchPtr(Above, regT1, Address(regT0, FIELD_OFFSET(RegisterFile, m_end)));
413 afterRegisterFileCheck = label();
414 }
415
416 privateCompileMainPass();
417 privateCompileLinkPass();
418 privateCompileSlowCases();
419
420 if (m_codeBlock->codeType() == FunctionCode) {
421 slowRegisterFileCheck.link(this);
422 m_bytecodeIndex = 0;
423 JITStubCall(this, JITStubs::cti_register_file_check).call();
424#ifndef NDEBUG
425 m_bytecodeIndex = (unsigned)-1; // Reset this, in order to guard its use with ASSERTs.
426#endif
427 jump(afterRegisterFileCheck);
428 }
429
430 ASSERT(m_jmpTable.isEmpty());
431
432 LinkBuffer patchBuffer(this, m_globalData->executableAllocator.poolForSize(m_assembler.size()));
433
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;
438
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());
442
443 record.jumpTable.simpleJumpTable->ctiDefault = patchBuffer.locationOf(m_labels[bytecodeIndex + 3 + record.defaultOffset]);
444
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;
448 }
449 } else {
450 ASSERT(record.type == SwitchRecord::String);
451
452 record.jumpTable.stringJumpTable->ctiDefault = patchBuffer.locationOf(m_labels[bytecodeIndex + 3 + record.defaultOffset]);
453
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;
458 }
459 }
460 }
461
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]);
465 }
466
467 for (Vector<CallRecord>::iterator iter = m_calls.begin(); iter != m_calls.end(); ++iter) {
468 if (iter->to)
469 patchBuffer.link(iter->from, FunctionPtr(iter->to));
470 }
471
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));
476 }
477
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());
481
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);
487 }
488#endif
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);
495 }
496#endif
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;
503 }
504
505 m_codeBlock->setJITCode(patchBuffer.finalizeCode());
506}
507
508void JIT::privateCompileCTIMachineTrampolines(RefPtr<ExecutablePool>* executablePool, JSGlobalData* globalData, CodePtr* ctiArrayLengthTrampoline, CodePtr* ctiStringLengthTrampoline, CodePtr* ctiVirtualCallPreLink, CodePtr* ctiVirtualCallLink, CodePtr* ctiVirtualCall, CodePtr* ctiNativeCallThunk)
509{
510#if ENABLE(JIT_OPTIMIZE_PROPERTY_ACCESS)
511 // (1) The first function provides fast property access for array length
512 Label arrayLengthBegin = align();
513
514 // Check eax is an array
515 Jump array_failureCases1 = emitJumpIfNotJSCell(regT0);
516 Jump array_failureCases2 = branchPtr(NotEqual, Address(regT0), ImmPtr(m_globalData->jsArrayVPtr));
517
518 // Checks out okay! - get the length from the storage
519 loadPtr(Address(regT0, FIELD_OFFSET(JSArray, m_storage)), regT0);
520 load32(Address(regT0, FIELD_OFFSET(ArrayStorage, m_length)), regT0);
521
522 Jump array_failureCases3 = branch32(Above, regT0, Imm32(JSImmediate::maxImmediateInt));
523
524 // regT0 contains a 64 bit value (is positive, is zero extended) so we don't need sign extend here.
525 emitFastArithIntToImmNoCheck(regT0, regT0);
526
527 ret();
528
529 // (2) The second function provides fast property access for string length
530 Label stringLengthBegin = align();
531
532 // Check eax is a string
533 Jump string_failureCases1 = emitJumpIfNotJSCell(regT0);
534 Jump string_failureCases2 = branchPtr(NotEqual, Address(regT0), ImmPtr(m_globalData->jsStringVPtr));
535
536 // Checks out okay! - get the length from the Ustring.
537 loadPtr(Address(regT0, FIELD_OFFSET(JSString, m_value) + FIELD_OFFSET(UString, m_rep)), regT0);
538 load32(Address(regT0, FIELD_OFFSET(UString::Rep, len)), regT0);
539
540 Jump string_failureCases3 = branch32(Above, regT0, Imm32(JSImmediate::maxImmediateInt));
541
542 // regT0 contains a 64 bit value (is positive, is zero extended) so we don't need sign extend here.
543 emitFastArithIntToImmNoCheck(regT0, regT0);
544
545 ret();
546#endif
547
548 // (3) Trampolines for the slow cases of op_call / op_call_eval / op_construct.
549
550 Label virtualCallPreLinkBegin = align();
551
552 // Load the callee CodeBlock* into eax
553 loadPtr(Address(regT2, FIELD_OFFSET(JSFunction, m_body)), regT3);
554 loadPtr(Address(regT3, FIELD_OFFSET(FunctionBodyNode, m_code)), regT0);
555 Jump hasCodeBlock1 = branchTestPtr(NonZero, regT0);
556 // If m_code is null and m_jitCode is not, then we have a native function, so arity is irrelevant
557 loadPtr(Address(regT3, FIELD_OFFSET(FunctionBodyNode, m_jitCode)), regT0);
558 Jump isNativeFunc1 = branchTestPtr(NonZero, regT0);
559 preverveReturnAddressAfterCall(regT3);
560 restoreArgumentReference();
561 Call callJSFunction1 = call();
562 emitGetJITStubArg(1, regT2);
563 emitGetJITStubArg(3, regT1);
564 restoreReturnAddressBeforeReturn(regT3);
565 hasCodeBlock1.link(this);
566
567 // Check argCount matches callee arity.
568 Jump arityCheckOkay1 = branch32(Equal, Address(regT0, FIELD_OFFSET(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);
580
581 compileOpCallInitializeCallFrame();
582
583 preverveReturnAddressAfterCall(regT3);
584 emitPutJITStubArg(regT3, 2);
585 restoreArgumentReference();
586 Call callDontLazyLinkCall = call();
587 emitGetJITStubArg(1, regT2);
588 restoreReturnAddressBeforeReturn(regT3);
589
590 jump(regT0);
591
592 Label virtualCallLinkBegin = align();
593
594 // Load the callee CodeBlock* into eax
595 loadPtr(Address(regT2, FIELD_OFFSET(JSFunction, m_body)), regT3);
596 loadPtr(Address(regT3, FIELD_OFFSET(FunctionBodyNode, m_code)), regT0);
597 Jump hasCodeBlock2 = branchTestPtr(NonZero, regT0);
598 // If m_code is null and m_jitCode is not, then we have a native function, so arity is irrelevant
599 loadPtr(Address(regT3, FIELD_OFFSET(FunctionBodyNode, m_jitCode)), regT0);
600 Jump isNativeFunc2 = branchTestPtr(NonZero, regT0);
601 preverveReturnAddressAfterCall(regT3);
602 restoreArgumentReference();
603 Call callJSFunction2 = call();
604 emitGetJITStubArg(1, regT2);
605 emitGetJITStubArg(3, regT1);
606 restoreReturnAddressBeforeReturn(regT3);
607 hasCodeBlock2.link(this);
608
609 // Check argCount matches callee arity.
610 Jump arityCheckOkay2 = branch32(Equal, Address(regT0, FIELD_OFFSET(CodeBlock, m_numParameters)), regT1);
611 preverveReturnAddressAfterCall(regT3);
612 emitPutJITStubArg(regT3, 2);
613 emitPutJITStubArg(regT0, 4);
614 restoreArgumentReference();
615 Call callArityCheck2 = call();
616 move(regT1, callFrameRegister);
617 emitGetJITStubArg(1, regT2);
618 emitGetJITStubArg(3, regT1);
619 restoreReturnAddressBeforeReturn(regT3);
620 arityCheckOkay2.link(this);
621 isNativeFunc2.link(this);
622
623 compileOpCallInitializeCallFrame();
624
625 preverveReturnAddressAfterCall(regT3);
626 emitPutJITStubArg(regT3, 2);
627 restoreArgumentReference();
628 Call callLazyLinkCall = call();
629 restoreReturnAddressBeforeReturn(regT3);
630
631 jump(regT0);
632
633 Label virtualCallBegin = align();
634
635 // Load the callee CodeBlock* into eax
636 loadPtr(Address(regT2, FIELD_OFFSET(JSFunction, m_body)), regT3);
637 loadPtr(Address(regT3, FIELD_OFFSET(FunctionBodyNode, m_code)), regT0);
638 Jump hasCodeBlock3 = branchTestPtr(NonZero, regT0);
639 // If m_code is null and m_jitCode is not, then we have a native function, so arity is irrelevant
640 loadPtr(Address(regT3, FIELD_OFFSET(FunctionBodyNode, m_jitCode)), regT0);
641 Jump isNativeFunc3 = branchTestPtr(NonZero, regT0);
642 preverveReturnAddressAfterCall(regT3);
643 restoreArgumentReference();
644 Call callJSFunction3 = call();
645 emitGetJITStubArg(1, regT2);
646 emitGetJITStubArg(3, regT1);
647 restoreReturnAddressBeforeReturn(regT3);
648 loadPtr(Address(regT2, FIELD_OFFSET(JSFunction, m_body)), regT3); // reload the function body nody, so we can reload the code pointer.
649 hasCodeBlock3.link(this);
650
651 // Check argCount matches callee arity.
652 Jump arityCheckOkay3 = branch32(Equal, Address(regT0, FIELD_OFFSET(CodeBlock, m_numParameters)), regT1);
653 preverveReturnAddressAfterCall(regT3);
654 emitPutJITStubArg(regT3, 2);
655 emitPutJITStubArg(regT0, 4);
656 restoreArgumentReference();
657 Call callArityCheck3 = call();
658 move(regT1, callFrameRegister);
659 emitGetJITStubArg(1, regT2);
660 emitGetJITStubArg(3, regT1);
661 restoreReturnAddressBeforeReturn(regT3);
662 loadPtr(Address(regT2, FIELD_OFFSET(JSFunction, m_body)), regT3); // reload the function body nody, so we can reload the code pointer.
663 arityCheckOkay3.link(this);
664 // load ctiCode from the new codeBlock.
665 loadPtr(Address(regT3, FIELD_OFFSET(FunctionBodyNode, m_jitCode)), regT0);
666 isNativeFunc3.link(this);
667
668 compileOpCallInitializeCallFrame();
669 jump(regT0);
670
671
672 Label nativeCallThunk = align();
673 preverveReturnAddressAfterCall(regT0);
674 emitPutToCallFrameHeader(regT0, RegisterFile::ReturnPC); // Push return address
675
676 // Load caller frame's scope chain into this callframe so that whatever we call can
677 // get to its global data.
678 emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame, regT1);
679 emitGetFromCallFrameHeaderPtr(RegisterFile::ScopeChain, regT1, regT1);
680 emitPutToCallFrameHeader(regT1, RegisterFile::ScopeChain);
681
682
683#if PLATFORM(X86_64)
684 emitGetFromCallFrameHeader32(RegisterFile::ArgumentCount, X86::ecx);
685
686 // Allocate stack space for our arglist
687 subPtr(Imm32(sizeof(ArgList)), stackPointerRegister);
688 COMPILE_ASSERT((sizeof(ArgList) & 0xf) == 0, ArgList_should_by_16byte_aligned);
689
690 // Set up arguments
691 subPtr(Imm32(1), X86::ecx); // Don't include 'this' in argcount
692
693 // Push argcount
694 storePtr(X86::ecx, Address(stackPointerRegister, FIELD_OFFSET(ArgList, m_argCount)));
695
696 // Calculate the start of the callframe header, and store in edx
697 addPtr(Imm32(-RegisterFile::CallFrameHeaderSize * (int32_t)sizeof(Register)), callFrameRegister, X86::edx);
698
699 // Calculate start of arguments as callframe header - sizeof(Register) * argcount (ecx)
700 mul32(Imm32(sizeof(Register)), X86::ecx, X86::ecx);
701 subPtr(X86::ecx, X86::edx);
702
703 // push pointer to arguments
704 storePtr(X86::edx, Address(stackPointerRegister, FIELD_OFFSET(ArgList, m_args)));
705
706 // ArgList is passed by reference so is stackPointerRegister
707 move(stackPointerRegister, X86::ecx);
708
709 // edx currently points to the first argument, edx-sizeof(Register) points to 'this'
710 loadPtr(Address(X86::edx, -(int32_t)sizeof(Register)), X86::edx);
711
712 emitGetFromCallFrameHeaderPtr(RegisterFile::Callee, X86::esi);
713
714 move(callFrameRegister, X86::edi);
715
716 call(Address(X86::esi, FIELD_OFFSET(JSFunction, m_data)));
717
718 addPtr(Imm32(sizeof(ArgList)), stackPointerRegister);
719#elif PLATFORM(X86)
720 emitGetFromCallFrameHeader32(RegisterFile::ArgumentCount, regT0);
721
722 /* We have two structs that we use to describe the stackframe we set up for our
723 * call to native code. NativeCallFrameStructure describes the how we set up the stack
724 * in advance of the call. NativeFunctionCalleeSignature describes the callframe
725 * as the native code expects it. We do this as we are using the fastcall calling
726 * convention which results in the callee popping its arguments off the stack, but
727 * not the rest of the callframe so we need a nice way to ensure we increment the
728 * stack pointer by the right amount after the call.
729 */
730#if COMPILER(MSVC) || PLATFORM(LINUX)
731 struct NativeCallFrameStructure {
732 // CallFrame* callFrame; // passed in EDX
733 JSObject* callee;
734 JSValue thisValue;
735 ArgList* argPointer;
736 ArgList args;
737 JSValue result;
738 };
739 struct NativeFunctionCalleeSignature {
740 JSObject* callee;
741 JSValue thisValue;
742 ArgList* argPointer;
743 };
744#else
745 struct NativeCallFrameStructure {
746 // CallFrame* callFrame; // passed in ECX
747 // JSObject* callee; // passed in EDX
748 JSValue thisValue;
749 ArgList* argPointer;
750 ArgList args;
751 };
752 struct NativeFunctionCalleeSignature {
753 JSValue thisValue;
754 ArgList* argPointer;
755 };
756#endif
757 const int NativeCallFrameSize = (sizeof(NativeCallFrameStructure) + 15) & ~15;
758 // Allocate system stack frame
759 subPtr(Imm32(NativeCallFrameSize), stackPointerRegister);
760
761 // Set up arguments
762 subPtr(Imm32(1), regT0); // Don't include 'this' in argcount
763
764 // push argcount
765 storePtr(regT0, Address(stackPointerRegister, FIELD_OFFSET(NativeCallFrameStructure, args) + FIELD_OFFSET(ArgList, m_argCount)));
766
767 // Calculate the start of the callframe header, and store in regT1
768 addPtr(Imm32(-RegisterFile::CallFrameHeaderSize * (int)sizeof(Register)), callFrameRegister, regT1);
769
770 // Calculate start of arguments as callframe header - sizeof(Register) * argcount (regT0)
771 mul32(Imm32(sizeof(Register)), regT0, regT0);
772 subPtr(regT0, regT1);
773 storePtr(regT1, Address(stackPointerRegister, FIELD_OFFSET(NativeCallFrameStructure, args) + FIELD_OFFSET(ArgList, m_args)));
774
775 // ArgList is passed by reference so is stackPointerRegister + 4 * sizeof(Register)
776 addPtr(Imm32(FIELD_OFFSET(NativeCallFrameStructure, args)), stackPointerRegister, regT0);
777 storePtr(regT0, Address(stackPointerRegister, FIELD_OFFSET(NativeCallFrameStructure, argPointer)));
778
779 // regT1 currently points to the first argument, regT1 - sizeof(Register) points to 'this'
780 loadPtr(Address(regT1, -(int)sizeof(Register)), regT1);
781 storePtr(regT1, Address(stackPointerRegister, FIELD_OFFSET(NativeCallFrameStructure, thisValue)));
782
783#if COMPILER(MSVC) || PLATFORM(LINUX)
784 // ArgList is passed by reference so is stackPointerRegister + 4 * sizeof(Register)
785 addPtr(Imm32(FIELD_OFFSET(NativeCallFrameStructure, result)), stackPointerRegister, X86::ecx);
786
787 // Plant callee
788 emitGetFromCallFrameHeaderPtr(RegisterFile::Callee, X86::eax);
789 storePtr(X86::eax, Address(stackPointerRegister, FIELD_OFFSET(NativeCallFrameStructure, callee)));
790
791 // Plant callframe
792 move(callFrameRegister, X86::edx);
793
794 call(Address(X86::eax, FIELD_OFFSET(JSFunction, m_data)));
795
796 // JSValue is a non-POD type
797 loadPtr(Address(X86::eax), X86::eax);
798#else
799 // Plant callee
800 emitGetFromCallFrameHeaderPtr(RegisterFile::Callee, X86::edx);
801
802 // Plant callframe
803 move(callFrameRegister, X86::ecx);
804 call(Address(X86::edx, FIELD_OFFSET(JSFunction, m_data)));
805#endif
806
807 // We've put a few temporaries on the stack in addition to the actual arguments
808 // so pull them off now
809 addPtr(Imm32(NativeCallFrameSize - sizeof(NativeFunctionCalleeSignature)), stackPointerRegister);
810
811#elif ENABLE(JIT_OPTIMIZE_NATIVE_CALL)
812#error "JIT_OPTIMIZE_NATIVE_CALL not yet supported on this platform."
813#else
814 breakpoint();
815#endif
816
817 // Check for an exception
818 loadPtr(&(globalData->exception), regT2);
819 Jump exceptionHandler = branchTestPtr(NonZero, regT2);
820
821 // Grab the return address.
822 emitGetFromCallFrameHeaderPtr(RegisterFile::ReturnPC, regT1);
823
824 // Restore our caller's "r".
825 emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame, callFrameRegister);
826
827 // Return.
828 restoreReturnAddressBeforeReturn(regT1);
829 ret();
830
831 // Handle an exception
832 exceptionHandler.link(this);
833 // Grab the return address.
834 emitGetFromCallFrameHeaderPtr(RegisterFile::ReturnPC, regT1);
835 move(ImmPtr(&globalData->exceptionLocation), regT2);
836 storePtr(regT1, regT2);
837 move(ImmPtr(reinterpret_cast<void*>(ctiVMThrowTrampoline)), regT2);
838 emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame, callFrameRegister);
839 poke(callFrameRegister, offsetof(struct JITStackFrame, callFrame) / sizeof (void*));
840 restoreReturnAddressBeforeReturn(regT2);
841 ret();
842
843
844#if ENABLE(JIT_OPTIMIZE_PROPERTY_ACCESS)
845 Call array_failureCases1Call = makeTailRecursiveCall(array_failureCases1);
846 Call array_failureCases2Call = makeTailRecursiveCall(array_failureCases2);
847 Call array_failureCases3Call = makeTailRecursiveCall(array_failureCases3);
848 Call string_failureCases1Call = makeTailRecursiveCall(string_failureCases1);
849 Call string_failureCases2Call = makeTailRecursiveCall(string_failureCases2);
850 Call string_failureCases3Call = makeTailRecursiveCall(string_failureCases3);
851#endif
852
853 // All trampolines constructed! copy the code, link up calls, and set the pointers on the Machine object.
854 LinkBuffer patchBuffer(this, m_globalData->executableAllocator.poolForSize(m_assembler.size()));
855
856#if ENABLE(JIT_OPTIMIZE_PROPERTY_ACCESS)
857 patchBuffer.link(array_failureCases1Call, FunctionPtr(JITStubs::cti_op_get_by_id_array_fail));
858 patchBuffer.link(array_failureCases2Call, FunctionPtr(JITStubs::cti_op_get_by_id_array_fail));
859 patchBuffer.link(array_failureCases3Call, FunctionPtr(JITStubs::cti_op_get_by_id_array_fail));
860 patchBuffer.link(string_failureCases1Call, FunctionPtr(JITStubs::cti_op_get_by_id_string_fail));
861 patchBuffer.link(string_failureCases2Call, FunctionPtr(JITStubs::cti_op_get_by_id_string_fail));
862 patchBuffer.link(string_failureCases3Call, FunctionPtr(JITStubs::cti_op_get_by_id_string_fail));
863#endif
864 patchBuffer.link(callArityCheck1, FunctionPtr(JITStubs::cti_op_call_arityCheck));
865 patchBuffer.link(callArityCheck2, FunctionPtr(JITStubs::cti_op_call_arityCheck));
866 patchBuffer.link(callArityCheck3, FunctionPtr(JITStubs::cti_op_call_arityCheck));
867 patchBuffer.link(callJSFunction1, FunctionPtr(JITStubs::cti_op_call_JSFunction));
868 patchBuffer.link(callJSFunction2, FunctionPtr(JITStubs::cti_op_call_JSFunction));
869 patchBuffer.link(callJSFunction3, FunctionPtr(JITStubs::cti_op_call_JSFunction));
870 patchBuffer.link(callDontLazyLinkCall, FunctionPtr(JITStubs::cti_vm_dontLazyLinkCall));
871 patchBuffer.link(callLazyLinkCall, FunctionPtr(JITStubs::cti_vm_lazyLinkCall));
872
873 CodeRef finalCode = patchBuffer.finalizeCode();
874 *executablePool = finalCode.m_executablePool;
875
876 *ctiVirtualCallPreLink = trampolineAt(finalCode, virtualCallPreLinkBegin);
877 *ctiVirtualCallLink = trampolineAt(finalCode, virtualCallLinkBegin);
878 *ctiVirtualCall = trampolineAt(finalCode, virtualCallBegin);
879 *ctiNativeCallThunk = trampolineAt(finalCode, nativeCallThunk);
880#if ENABLE(JIT_OPTIMIZE_PROPERTY_ACCESS)
881 *ctiArrayLengthTrampoline = trampolineAt(finalCode, arrayLengthBegin);
882 *ctiStringLengthTrampoline = trampolineAt(finalCode, stringLengthBegin);
883#else
884 UNUSED_PARAM(ctiArrayLengthTrampoline);
885 UNUSED_PARAM(ctiStringLengthTrampoline);
886#endif
887}
888
889void JIT::emitGetVariableObjectRegister(RegisterID variableObject, int index, RegisterID dst)
890{
891 loadPtr(Address(variableObject, FIELD_OFFSET(JSVariableObject, d)), dst);
892 loadPtr(Address(dst, FIELD_OFFSET(JSVariableObject::JSVariableObjectData, registers)), dst);
893 loadPtr(Address(dst, index * sizeof(Register)), dst);
894}
895
896void JIT::emitPutVariableObjectRegister(RegisterID src, RegisterID variableObject, int index)
897{
898 loadPtr(Address(variableObject, FIELD_OFFSET(JSVariableObject, d)), variableObject);
899 loadPtr(Address(variableObject, FIELD_OFFSET(JSVariableObject::JSVariableObjectData, registers)), variableObject);
900 storePtr(src, Address(variableObject, index * sizeof(Register)));
901}
902
903void JIT::unlinkCall(CallLinkInfo* callLinkInfo)
904{
905 // When the JSFunction is deleted the pointer embedded in the instruction stream will no longer be valid
906 // (and, if a new JSFunction happened to be constructed at the same location, we could get a false positive
907 // match). Reset the check so it no longer matches.
908 RepatchBuffer repatchBuffer;
909 repatchBuffer.repatch(callLinkInfo->hotPathBegin, JSValue::encode(JSValue()));
910}
911
912void JIT::linkCall(JSFunction* callee, CodeBlock* calleeCodeBlock, JITCode& code, CallLinkInfo* callLinkInfo, int callerArgCount, JSGlobalData* globalData)
913{
914 RepatchBuffer repatchBuffer;
915
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 (!calleeCodeBlock || callerArgCount == calleeCodeBlock->m_numParameters) {
919 ASSERT(!callLinkInfo->isLinked());
920
921 if (calleeCodeBlock)
922 calleeCodeBlock->addCaller(callLinkInfo);
923
924 repatchBuffer.repatch(callLinkInfo->hotPathBegin, callee);
925 repatchBuffer.relink(callLinkInfo->hotPathOther, code.addressForCall());
926 }
927
928 // patch the call so we do not continue to try to link.
929 repatchBuffer.relink(callLinkInfo->callReturnLocation, globalData->jitStubs.ctiVirtualCall());
930}
931
932} // namespace JSC
933
934#endif // ENABLE(JIT)
935
936// This probably does not belong here; adding here for now as a quick Windows build fix.
937#if ENABLE(ASSEMBLER)
938
939#if PLATFORM(X86) && !PLATFORM(MAC)
940JSC::MacroAssemblerX86Common::SSE2CheckState JSC::MacroAssemblerX86Common::s_sse2CheckState = NotCheckedSSE2;
941#endif
942
943#endif
Note: See TracBrowser for help on using the repository browser.