Unreviewed, rolling out r184123.
[WebKit-https.git] / Source / JavaScriptCore / jit / JITOpcodes.cpp
1 /*
2  * Copyright (C) 2009, 2012-2015 Apple Inc. All rights reserved.
3  * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25  */
26
27 #include "config.h"
28 #if ENABLE(JIT)
29 #include "JIT.h"
30
31 #include "BasicBlockLocation.h"
32 #include "CopiedSpaceInlines.h"
33 #include "Debugger.h"
34 #include "Heap.h"
35 #include "JITInlines.h"
36 #include "JSArray.h"
37 #include "JSCell.h"
38 #include "JSFunction.h"
39 #include "JSNameScope.h"
40 #include "JSPropertyNameEnumerator.h"
41 #include "LinkBuffer.h"
42 #include "MaxFrameExtentForSlowPathCall.h"
43 #include "RepatchBuffer.h"
44 #include "SlowPathCall.h"
45 #include "TypeLocation.h"
46 #include "TypeProfilerLog.h"
47 #include "VirtualRegister.h"
48
49 namespace JSC {
50
51 #if USE(JSVALUE64)
52
53 JIT::CodeRef JIT::privateCompileCTINativeCall(VM* vm, NativeFunction)
54 {
55     return vm->getCTIStub(nativeCallGenerator);
56 }
57
58 void JIT::emit_op_mov(Instruction* currentInstruction)
59 {
60     int dst = currentInstruction[1].u.operand;
61     int src = currentInstruction[2].u.operand;
62
63     emitGetVirtualRegister(src, regT0);
64     emitPutVirtualRegister(dst);
65 }
66
67
68 void JIT::emit_op_end(Instruction* currentInstruction)
69 {
70     RELEASE_ASSERT(returnValueGPR != callFrameRegister);
71     emitGetVirtualRegister(currentInstruction[1].u.operand, returnValueGPR);
72     emitFunctionEpilogue();
73     ret();
74 }
75
76 void JIT::emit_op_jmp(Instruction* currentInstruction)
77 {
78     unsigned target = currentInstruction[1].u.operand;
79     addJump(jump(), target);
80 }
81
82 void JIT::emit_op_new_object(Instruction* currentInstruction)
83 {
84     Structure* structure = currentInstruction[3].u.objectAllocationProfile->structure();
85     size_t allocationSize = JSFinalObject::allocationSize(structure->inlineCapacity());
86     MarkedAllocator* allocator = &m_vm->heap.allocatorForObjectWithoutDestructor(allocationSize);
87
88     RegisterID resultReg = regT0;
89     RegisterID allocatorReg = regT1;
90     RegisterID scratchReg = regT2;
91
92     move(TrustedImmPtr(allocator), allocatorReg);
93     emitAllocateJSObject(allocatorReg, TrustedImmPtr(structure), resultReg, scratchReg);
94     emitPutVirtualRegister(currentInstruction[1].u.operand);
95 }
96
97 void JIT::emitSlow_op_new_object(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
98 {
99     linkSlowCase(iter);
100     int dst = currentInstruction[1].u.operand;
101     Structure* structure = currentInstruction[3].u.objectAllocationProfile->structure();
102     callOperation(operationNewObject, structure);
103     emitStoreCell(dst, returnValueGPR);
104 }
105
106 void JIT::emit_op_check_has_instance(Instruction* currentInstruction)
107 {
108     int baseVal = currentInstruction[3].u.operand;
109
110     emitGetVirtualRegister(baseVal, regT0);
111
112     // Check that baseVal is a cell.
113     emitJumpSlowCaseIfNotJSCell(regT0, baseVal);
114
115     // Check that baseVal 'ImplementsHasInstance'.
116     addSlowCase(branchTest8(Zero, Address(regT0, JSCell::typeInfoFlagsOffset()), TrustedImm32(ImplementsDefaultHasInstance)));
117 }
118
119 void JIT::emit_op_instanceof(Instruction* currentInstruction)
120 {
121     int dst = currentInstruction[1].u.operand;
122     int value = currentInstruction[2].u.operand;
123     int proto = currentInstruction[3].u.operand;
124
125     // Load the operands (baseVal, proto, and value respectively) into registers.
126     // We use regT0 for baseVal since we will be done with this first, and we can then use it for the result.
127     emitGetVirtualRegister(value, regT2);
128     emitGetVirtualRegister(proto, regT1);
129
130     // Check that proto are cells.  baseVal must be a cell - this is checked by op_check_has_instance.
131     emitJumpSlowCaseIfNotJSCell(regT2, value);
132     emitJumpSlowCaseIfNotJSCell(regT1, proto);
133
134     // Check that prototype is an object
135     addSlowCase(emitJumpIfCellNotObject(regT1));
136     
137     // Optimistically load the result true, and start looping.
138     // Initially, regT1 still contains proto and regT2 still contains value.
139     // As we loop regT2 will be updated with its prototype, recursively walking the prototype chain.
140     move(TrustedImm64(JSValue::encode(jsBoolean(true))), regT0);
141     Label loop(this);
142
143     // Load the prototype of the object in regT2.  If this is equal to regT1 - WIN!
144     // Otherwise, check if we've hit null - if we have then drop out of the loop, if not go again.
145     emitLoadStructure(regT2, regT2, regT3);
146     load64(Address(regT2, Structure::prototypeOffset()), regT2);
147     Jump isInstance = branchPtr(Equal, regT2, regT1);
148     emitJumpIfJSCell(regT2).linkTo(loop, this);
149
150     // We get here either by dropping out of the loop, or if value was not an Object.  Result is false.
151     move(TrustedImm64(JSValue::encode(jsBoolean(false))), regT0);
152
153     // isInstance jumps right down to here, to skip setting the result to false (it has already set true).
154     isInstance.link(this);
155     emitPutVirtualRegister(dst);
156 }
157
158 void JIT::emit_op_is_undefined(Instruction* currentInstruction)
159 {
160     int dst = currentInstruction[1].u.operand;
161     int value = currentInstruction[2].u.operand;
162     
163     emitGetVirtualRegister(value, regT0);
164     Jump isCell = emitJumpIfJSCell(regT0);
165
166     compare64(Equal, regT0, TrustedImm32(ValueUndefined), regT0);
167     Jump done = jump();
168     
169     isCell.link(this);
170     Jump isMasqueradesAsUndefined = branchTest8(NonZero, Address(regT0, JSCell::typeInfoFlagsOffset()), TrustedImm32(MasqueradesAsUndefined));
171     move(TrustedImm32(0), regT0);
172     Jump notMasqueradesAsUndefined = jump();
173
174     isMasqueradesAsUndefined.link(this);
175     emitLoadStructure(regT0, regT1, regT2);
176     move(TrustedImmPtr(m_codeBlock->globalObject()), regT0);
177     loadPtr(Address(regT1, Structure::globalObjectOffset()), regT1);
178     comparePtr(Equal, regT0, regT1, regT0);
179
180     notMasqueradesAsUndefined.link(this);
181     done.link(this);
182     emitTagAsBoolImmediate(regT0);
183     emitPutVirtualRegister(dst);
184 }
185
186 void JIT::emit_op_is_boolean(Instruction* currentInstruction)
187 {
188     int dst = currentInstruction[1].u.operand;
189     int value = currentInstruction[2].u.operand;
190     
191     emitGetVirtualRegister(value, regT0);
192     xor64(TrustedImm32(static_cast<int32_t>(ValueFalse)), regT0);
193     test64(Zero, regT0, TrustedImm32(static_cast<int32_t>(~1)), regT0);
194     emitTagAsBoolImmediate(regT0);
195     emitPutVirtualRegister(dst);
196 }
197
198 void JIT::emit_op_is_number(Instruction* currentInstruction)
199 {
200     int dst = currentInstruction[1].u.operand;
201     int value = currentInstruction[2].u.operand;
202     
203     emitGetVirtualRegister(value, regT0);
204     test64(NonZero, regT0, tagTypeNumberRegister, regT0);
205     emitTagAsBoolImmediate(regT0);
206     emitPutVirtualRegister(dst);
207 }
208
209 void JIT::emit_op_is_string(Instruction* currentInstruction)
210 {
211     int dst = currentInstruction[1].u.operand;
212     int value = currentInstruction[2].u.operand;
213     
214     emitGetVirtualRegister(value, regT0);
215     Jump isNotCell = emitJumpIfNotJSCell(regT0);
216     
217     compare8(Equal, Address(regT0, JSCell::typeInfoTypeOffset()), TrustedImm32(StringType), regT0);
218     emitTagAsBoolImmediate(regT0);
219     Jump done = jump();
220     
221     isNotCell.link(this);
222     move(TrustedImm32(ValueFalse), regT0);
223     
224     done.link(this);
225     emitPutVirtualRegister(dst);
226 }
227
228 void JIT::emit_op_is_object(Instruction* currentInstruction)
229 {
230     int dst = currentInstruction[1].u.operand;
231     int value = currentInstruction[2].u.operand;
232
233     emitGetVirtualRegister(value, regT0);
234     Jump isNotCell = emitJumpIfNotJSCell(regT0);
235
236     compare8(AboveOrEqual, Address(regT0, JSCell::typeInfoTypeOffset()), TrustedImm32(ObjectType), regT0);
237     emitTagAsBoolImmediate(regT0);
238     Jump done = jump();
239
240     isNotCell.link(this);
241     move(TrustedImm32(ValueFalse), regT0);
242
243     done.link(this);
244     emitPutVirtualRegister(dst);
245 }
246
247 void JIT::emit_op_ret(Instruction* currentInstruction)
248 {
249     ASSERT(callFrameRegister != regT1);
250     ASSERT(regT1 != returnValueGPR);
251     ASSERT(returnValueGPR != callFrameRegister);
252
253     // Return the result in %eax.
254     emitGetVirtualRegister(currentInstruction[1].u.operand, returnValueGPR);
255
256     checkStackPointerAlignment();
257     emitFunctionEpilogue();
258     ret();
259 }
260
261 void JIT::emit_op_to_primitive(Instruction* currentInstruction)
262 {
263     int dst = currentInstruction[1].u.operand;
264     int src = currentInstruction[2].u.operand;
265
266     emitGetVirtualRegister(src, regT0);
267     
268     Jump isImm = emitJumpIfNotJSCell(regT0);
269     addSlowCase(emitJumpIfCellObject(regT0));
270     isImm.link(this);
271
272     if (dst != src)
273         emitPutVirtualRegister(dst);
274
275 }
276
277 void JIT::emit_op_strcat(Instruction* currentInstruction)
278 {
279     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_strcat);
280     slowPathCall.call();
281 }
282
283 void JIT::emit_op_not(Instruction* currentInstruction)
284 {
285     emitGetVirtualRegister(currentInstruction[2].u.operand, regT0);
286
287     // Invert against JSValue(false); if the value was tagged as a boolean, then all bits will be
288     // clear other than the low bit (which will be 0 or 1 for false or true inputs respectively).
289     // Then invert against JSValue(true), which will add the tag back in, and flip the low bit.
290     xor64(TrustedImm32(static_cast<int32_t>(ValueFalse)), regT0);
291     addSlowCase(branchTestPtr(NonZero, regT0, TrustedImm32(static_cast<int32_t>(~1))));
292     xor64(TrustedImm32(static_cast<int32_t>(ValueTrue)), regT0);
293
294     emitPutVirtualRegister(currentInstruction[1].u.operand);
295 }
296
297 void JIT::emit_op_jfalse(Instruction* currentInstruction)
298 {
299     unsigned target = currentInstruction[2].u.operand;
300     emitGetVirtualRegister(currentInstruction[1].u.operand, regT0);
301
302     addJump(branch64(Equal, regT0, TrustedImm64(JSValue::encode(jsNumber(0)))), target);
303     Jump isNonZero = emitJumpIfImmediateInteger(regT0);
304
305     addJump(branch64(Equal, regT0, TrustedImm64(JSValue::encode(jsBoolean(false)))), target);
306     addSlowCase(branch64(NotEqual, regT0, TrustedImm64(JSValue::encode(jsBoolean(true)))));
307
308     isNonZero.link(this);
309 }
310
311 void JIT::emit_op_jeq_null(Instruction* currentInstruction)
312 {
313     int src = currentInstruction[1].u.operand;
314     unsigned target = currentInstruction[2].u.operand;
315
316     emitGetVirtualRegister(src, regT0);
317     Jump isImmediate = emitJumpIfNotJSCell(regT0);
318
319     // First, handle JSCell cases - check MasqueradesAsUndefined bit on the structure.
320     Jump isNotMasqueradesAsUndefined = branchTest8(Zero, Address(regT0, JSCell::typeInfoFlagsOffset()), TrustedImm32(MasqueradesAsUndefined));
321     emitLoadStructure(regT0, regT2, regT1);
322     move(TrustedImmPtr(m_codeBlock->globalObject()), regT0);
323     addJump(branchPtr(Equal, Address(regT2, Structure::globalObjectOffset()), regT0), target);
324     Jump masqueradesGlobalObjectIsForeign = jump();
325
326     // Now handle the immediate cases - undefined & null
327     isImmediate.link(this);
328     and64(TrustedImm32(~TagBitUndefined), regT0);
329     addJump(branch64(Equal, regT0, TrustedImm64(JSValue::encode(jsNull()))), target);            
330
331     isNotMasqueradesAsUndefined.link(this);
332     masqueradesGlobalObjectIsForeign.link(this);
333 };
334 void JIT::emit_op_jneq_null(Instruction* currentInstruction)
335 {
336     int src = currentInstruction[1].u.operand;
337     unsigned target = currentInstruction[2].u.operand;
338
339     emitGetVirtualRegister(src, regT0);
340     Jump isImmediate = emitJumpIfNotJSCell(regT0);
341
342     // First, handle JSCell cases - check MasqueradesAsUndefined bit on the structure.
343     addJump(branchTest8(Zero, Address(regT0, JSCell::typeInfoFlagsOffset()), TrustedImm32(MasqueradesAsUndefined)), target);
344     emitLoadStructure(regT0, regT2, regT1);
345     move(TrustedImmPtr(m_codeBlock->globalObject()), regT0);
346     addJump(branchPtr(NotEqual, Address(regT2, Structure::globalObjectOffset()), regT0), target);
347     Jump wasNotImmediate = jump();
348
349     // Now handle the immediate cases - undefined & null
350     isImmediate.link(this);
351     and64(TrustedImm32(~TagBitUndefined), regT0);
352     addJump(branch64(NotEqual, regT0, TrustedImm64(JSValue::encode(jsNull()))), target);            
353
354     wasNotImmediate.link(this);
355 }
356
357 void JIT::emit_op_jneq_ptr(Instruction* currentInstruction)
358 {
359     int src = currentInstruction[1].u.operand;
360     Special::Pointer ptr = currentInstruction[2].u.specialPointer;
361     unsigned target = currentInstruction[3].u.operand;
362     
363     emitGetVirtualRegister(src, regT0);
364     addJump(branchPtr(NotEqual, regT0, TrustedImmPtr(actualPointerFor(m_codeBlock, ptr))), target);
365 }
366
367 void JIT::emit_op_eq(Instruction* currentInstruction)
368 {
369     emitGetVirtualRegisters(currentInstruction[2].u.operand, regT0, currentInstruction[3].u.operand, regT1);
370     emitJumpSlowCaseIfNotImmediateIntegers(regT0, regT1, regT2);
371     compare32(Equal, regT1, regT0, regT0);
372     emitTagAsBoolImmediate(regT0);
373     emitPutVirtualRegister(currentInstruction[1].u.operand);
374 }
375
376 void JIT::emit_op_jtrue(Instruction* currentInstruction)
377 {
378     unsigned target = currentInstruction[2].u.operand;
379     emitGetVirtualRegister(currentInstruction[1].u.operand, regT0);
380
381     Jump isZero = branch64(Equal, regT0, TrustedImm64(JSValue::encode(jsNumber(0))));
382     addJump(emitJumpIfImmediateInteger(regT0), target);
383
384     addJump(branch64(Equal, regT0, TrustedImm64(JSValue::encode(jsBoolean(true)))), target);
385     addSlowCase(branch64(NotEqual, regT0, TrustedImm64(JSValue::encode(jsBoolean(false)))));
386
387     isZero.link(this);
388 }
389
390 void JIT::emit_op_neq(Instruction* currentInstruction)
391 {
392     emitGetVirtualRegisters(currentInstruction[2].u.operand, regT0, currentInstruction[3].u.operand, regT1);
393     emitJumpSlowCaseIfNotImmediateIntegers(regT0, regT1, regT2);
394     compare32(NotEqual, regT1, regT0, regT0);
395     emitTagAsBoolImmediate(regT0);
396
397     emitPutVirtualRegister(currentInstruction[1].u.operand);
398
399 }
400
401 void JIT::emit_op_bitxor(Instruction* currentInstruction)
402 {
403     emitGetVirtualRegisters(currentInstruction[2].u.operand, regT0, currentInstruction[3].u.operand, regT1);
404     emitJumpSlowCaseIfNotImmediateIntegers(regT0, regT1, regT2);
405     xor64(regT1, regT0);
406     emitFastArithReTagImmediate(regT0, regT0);
407     emitPutVirtualRegister(currentInstruction[1].u.operand);
408 }
409
410 void JIT::emit_op_bitor(Instruction* currentInstruction)
411 {
412     emitGetVirtualRegisters(currentInstruction[2].u.operand, regT0, currentInstruction[3].u.operand, regT1);
413     emitJumpSlowCaseIfNotImmediateIntegers(regT0, regT1, regT2);
414     or64(regT1, regT0);
415     emitPutVirtualRegister(currentInstruction[1].u.operand);
416 }
417
418 void JIT::emit_op_throw(Instruction* currentInstruction)
419 {
420     ASSERT(regT0 == returnValueGPR);
421     emitGetVirtualRegister(currentInstruction[1].u.operand, regT0);
422     callOperationNoExceptionCheck(operationThrow, regT0);
423     jumpToExceptionHandler();
424 }
425
426 void JIT::emit_op_push_with_scope(Instruction* currentInstruction)
427 {
428     int dst = currentInstruction[1].u.operand;
429     emitGetVirtualRegister(currentInstruction[2].u.operand, regT0);
430     callOperation(operationPushWithScope, dst, regT0);
431 }
432
433 void JIT::emit_op_pop_scope(Instruction* currentInstruction)
434 {
435     int scope = currentInstruction[1].u.operand;
436
437     callOperation(operationPopScope, scope);
438 }
439
440 void JIT::compileOpStrictEq(Instruction* currentInstruction, CompileOpStrictEqType type)
441 {
442     int dst = currentInstruction[1].u.operand;
443     int src1 = currentInstruction[2].u.operand;
444     int src2 = currentInstruction[3].u.operand;
445
446     emitGetVirtualRegisters(src1, regT0, src2, regT1);
447     
448     // Jump slow if both are cells (to cover strings).
449     move(regT0, regT2);
450     or64(regT1, regT2);
451     addSlowCase(emitJumpIfJSCell(regT2));
452     
453     // Jump slow if either is a double. First test if it's an integer, which is fine, and then test
454     // if it's a double.
455     Jump leftOK = emitJumpIfImmediateInteger(regT0);
456     addSlowCase(emitJumpIfImmediateNumber(regT0));
457     leftOK.link(this);
458     Jump rightOK = emitJumpIfImmediateInteger(regT1);
459     addSlowCase(emitJumpIfImmediateNumber(regT1));
460     rightOK.link(this);
461
462     if (type == OpStrictEq)
463         compare64(Equal, regT1, regT0, regT0);
464     else
465         compare64(NotEqual, regT1, regT0, regT0);
466     emitTagAsBoolImmediate(regT0);
467
468     emitPutVirtualRegister(dst);
469 }
470
471 void JIT::emit_op_stricteq(Instruction* currentInstruction)
472 {
473     compileOpStrictEq(currentInstruction, OpStrictEq);
474 }
475
476 void JIT::emit_op_nstricteq(Instruction* currentInstruction)
477 {
478     compileOpStrictEq(currentInstruction, OpNStrictEq);
479 }
480
481 void JIT::emit_op_to_number(Instruction* currentInstruction)
482 {
483     int srcVReg = currentInstruction[2].u.operand;
484     emitGetVirtualRegister(srcVReg, regT0);
485     
486     addSlowCase(emitJumpIfNotImmediateNumber(regT0));
487
488     emitPutVirtualRegister(currentInstruction[1].u.operand);
489 }
490
491 void JIT::emit_op_to_string(Instruction* currentInstruction)
492 {
493     int srcVReg = currentInstruction[2].u.operand;
494     emitGetVirtualRegister(srcVReg, regT0);
495
496     addSlowCase(emitJumpIfNotJSCell(regT0));
497     addSlowCase(branch8(NotEqual, Address(regT0, JSCell::typeInfoTypeOffset()), TrustedImm32(StringType)));
498
499     emitPutVirtualRegister(currentInstruction[1].u.operand);
500 }
501
502 void JIT::emit_op_push_name_scope(Instruction* currentInstruction)
503 {
504     int dst = currentInstruction[1].u.operand;
505     emitGetVirtualRegister(currentInstruction[2].u.operand, regT0);
506     if (currentInstruction[4].u.operand == JSNameScope::CatchScope) {
507         callOperation(operationPushCatchScope, dst, jsCast<SymbolTable*>(getConstantOperand(currentInstruction[3].u.operand)), regT0);
508         return;
509     }
510
511     RELEASE_ASSERT(currentInstruction[4].u.operand == JSNameScope::FunctionNameScope);
512     callOperation(operationPushFunctionNameScope, dst, jsCast<SymbolTable*>(getConstantOperand(currentInstruction[3].u.operand)), regT0);
513 }
514
515 void JIT::emit_op_catch(Instruction* currentInstruction)
516 {
517     // Gotta restore the tag registers. We could be throwing from FTL, which may
518     // clobber them.
519     move(TrustedImm64(TagTypeNumber), tagTypeNumberRegister);
520     move(TrustedImm64(TagMask), tagMaskRegister);
521     
522     move(TrustedImmPtr(m_vm), regT3);
523     load64(Address(regT3, VM::callFrameForThrowOffset()), callFrameRegister);
524     load64(Address(regT3, VM::vmEntryFrameForThrowOffset()), regT0);
525     store64(regT0, Address(regT3, VM::topVMEntryFrameOffset()));
526
527     addPtr(TrustedImm32(stackPointerOffsetFor(codeBlock()) * sizeof(Register)), callFrameRegister, stackPointerRegister);
528
529     load64(Address(regT3, VM::exceptionOffset()), regT0);
530     store64(TrustedImm64(JSValue::encode(JSValue())), Address(regT3, VM::exceptionOffset()));
531     emitPutVirtualRegister(currentInstruction[1].u.operand);
532 }
533
534 void JIT::emit_op_switch_imm(Instruction* currentInstruction)
535 {
536     size_t tableIndex = currentInstruction[1].u.operand;
537     unsigned defaultOffset = currentInstruction[2].u.operand;
538     unsigned scrutinee = currentInstruction[3].u.operand;
539
540     // create jump table for switch destinations, track this switch statement.
541     SimpleJumpTable* jumpTable = &m_codeBlock->switchJumpTable(tableIndex);
542     m_switches.append(SwitchRecord(jumpTable, m_bytecodeOffset, defaultOffset, SwitchRecord::Immediate));
543     jumpTable->ensureCTITable();
544
545     emitGetVirtualRegister(scrutinee, regT0);
546     callOperation(operationSwitchImmWithUnknownKeyType, regT0, tableIndex);
547     jump(returnValueGPR);
548 }
549
550 void JIT::emit_op_switch_char(Instruction* currentInstruction)
551 {
552     size_t tableIndex = currentInstruction[1].u.operand;
553     unsigned defaultOffset = currentInstruction[2].u.operand;
554     unsigned scrutinee = currentInstruction[3].u.operand;
555
556     // create jump table for switch destinations, track this switch statement.
557     SimpleJumpTable* jumpTable = &m_codeBlock->switchJumpTable(tableIndex);
558     m_switches.append(SwitchRecord(jumpTable, m_bytecodeOffset, defaultOffset, SwitchRecord::Character));
559     jumpTable->ensureCTITable();
560
561     emitGetVirtualRegister(scrutinee, regT0);
562     callOperation(operationSwitchCharWithUnknownKeyType, regT0, tableIndex);
563     jump(returnValueGPR);
564 }
565
566 void JIT::emit_op_switch_string(Instruction* currentInstruction)
567 {
568     size_t tableIndex = currentInstruction[1].u.operand;
569     unsigned defaultOffset = currentInstruction[2].u.operand;
570     unsigned scrutinee = currentInstruction[3].u.operand;
571
572     // create jump table for switch destinations, track this switch statement.
573     StringJumpTable* jumpTable = &m_codeBlock->stringSwitchJumpTable(tableIndex);
574     m_switches.append(SwitchRecord(jumpTable, m_bytecodeOffset, defaultOffset));
575
576     emitGetVirtualRegister(scrutinee, regT0);
577     callOperation(operationSwitchStringWithUnknownKeyType, regT0, tableIndex);
578     jump(returnValueGPR);
579 }
580
581 void JIT::emit_op_throw_static_error(Instruction* currentInstruction)
582 {
583     move(TrustedImm64(JSValue::encode(m_codeBlock->getConstant(currentInstruction[1].u.operand))), regT0);
584     callOperation(operationThrowStaticError, regT0, currentInstruction[2].u.operand);
585 }
586
587 void JIT::emit_op_debug(Instruction* currentInstruction)
588 {
589     load32(codeBlock()->debuggerRequestsAddress(), regT0);
590     Jump noDebuggerRequests = branchTest32(Zero, regT0);
591     callOperation(operationDebug, currentInstruction[1].u.operand);
592     noDebuggerRequests.link(this);
593 }
594
595 void JIT::emit_op_eq_null(Instruction* currentInstruction)
596 {
597     int dst = currentInstruction[1].u.operand;
598     int src1 = currentInstruction[2].u.operand;
599
600     emitGetVirtualRegister(src1, regT0);
601     Jump isImmediate = emitJumpIfNotJSCell(regT0);
602
603     Jump isMasqueradesAsUndefined = branchTest8(NonZero, Address(regT0, JSCell::typeInfoFlagsOffset()), TrustedImm32(MasqueradesAsUndefined));
604     move(TrustedImm32(0), regT0);
605     Jump wasNotMasqueradesAsUndefined = jump();
606
607     isMasqueradesAsUndefined.link(this);
608     emitLoadStructure(regT0, regT2, regT1);
609     move(TrustedImmPtr(m_codeBlock->globalObject()), regT0);
610     loadPtr(Address(regT2, Structure::globalObjectOffset()), regT2);
611     comparePtr(Equal, regT0, regT2, regT0);
612     Jump wasNotImmediate = jump();
613
614     isImmediate.link(this);
615
616     and64(TrustedImm32(~TagBitUndefined), regT0);
617     compare64(Equal, regT0, TrustedImm32(ValueNull), regT0);
618
619     wasNotImmediate.link(this);
620     wasNotMasqueradesAsUndefined.link(this);
621
622     emitTagAsBoolImmediate(regT0);
623     emitPutVirtualRegister(dst);
624
625 }
626
627 void JIT::emit_op_neq_null(Instruction* currentInstruction)
628 {
629     int dst = currentInstruction[1].u.operand;
630     int src1 = currentInstruction[2].u.operand;
631
632     emitGetVirtualRegister(src1, regT0);
633     Jump isImmediate = emitJumpIfNotJSCell(regT0);
634
635     Jump isMasqueradesAsUndefined = branchTest8(NonZero, Address(regT0, JSCell::typeInfoFlagsOffset()), TrustedImm32(MasqueradesAsUndefined));
636     move(TrustedImm32(1), regT0);
637     Jump wasNotMasqueradesAsUndefined = jump();
638
639     isMasqueradesAsUndefined.link(this);
640     emitLoadStructure(regT0, regT2, regT1);
641     move(TrustedImmPtr(m_codeBlock->globalObject()), regT0);
642     loadPtr(Address(regT2, Structure::globalObjectOffset()), regT2);
643     comparePtr(NotEqual, regT0, regT2, regT0);
644     Jump wasNotImmediate = jump();
645
646     isImmediate.link(this);
647
648     and64(TrustedImm32(~TagBitUndefined), regT0);
649     compare64(NotEqual, regT0, TrustedImm32(ValueNull), regT0);
650
651     wasNotImmediate.link(this);
652     wasNotMasqueradesAsUndefined.link(this);
653
654     emitTagAsBoolImmediate(regT0);
655     emitPutVirtualRegister(dst);
656 }
657
658 void JIT::emit_op_enter(Instruction*)
659 {
660     // Even though CTI doesn't use them, we initialize our constant
661     // registers to zap stale pointers, to avoid unnecessarily prolonging
662     // object lifetime and increasing GC pressure.
663     size_t count = m_codeBlock->m_numVars;
664     for (size_t j = 0; j < count; ++j)
665         emitInitRegister(virtualRegisterForLocal(j).offset());
666
667     emitWriteBarrier(m_codeBlock->ownerExecutable());
668
669     emitEnterOptimizationCheck();
670 }
671
672 void JIT::emit_op_create_lexical_environment(Instruction* currentInstruction)
673 {
674     int dst = currentInstruction[1].u.operand;
675     int scope = currentInstruction[2].u.operand;
676
677     emitGetVirtualRegister(scope, regT0);
678     callOperation(operationCreateActivation, regT0);
679     emitStoreCell(dst, returnValueGPR);
680     emitStoreCell(scope, returnValueGPR);
681 }
682
683 void JIT::emit_op_get_scope(Instruction* currentInstruction)
684 {
685     int dst = currentInstruction[1].u.operand;
686     emitGetFromCallFrameHeaderPtr(JSStack::Callee, regT0);
687     loadPtr(Address(regT0, JSFunction::offsetOfScopeChain()), regT0);
688     emitStoreCell(dst, regT0);
689 }
690
691 void JIT::emit_op_to_this(Instruction* currentInstruction)
692 {
693     WriteBarrierBase<Structure>* cachedStructure = &currentInstruction[2].u.structure;
694     emitGetVirtualRegister(currentInstruction[1].u.operand, regT1);
695
696     emitJumpSlowCaseIfNotJSCell(regT1);
697
698     addSlowCase(branch8(NotEqual, Address(regT1, JSCell::typeInfoTypeOffset()), TrustedImm32(FinalObjectType)));
699     loadPtr(cachedStructure, regT2);
700     addSlowCase(branchTestPtr(Zero, regT2));
701     load32(Address(regT2, Structure::structureIDOffset()), regT2);
702     addSlowCase(branch32(NotEqual, Address(regT1, JSCell::structureIDOffset()), regT2));
703 }
704
705 void JIT::emit_op_create_this(Instruction* currentInstruction)
706 {
707     int callee = currentInstruction[2].u.operand;
708     RegisterID calleeReg = regT0;
709     RegisterID rareDataReg = regT0;
710     RegisterID resultReg = regT0;
711     RegisterID allocatorReg = regT1;
712     RegisterID structureReg = regT2;
713     RegisterID scratchReg = regT3;
714
715     emitGetVirtualRegister(callee, calleeReg);
716     loadPtr(Address(calleeReg, JSFunction::offsetOfRareData()), rareDataReg);
717     addSlowCase(branchTestPtr(Zero, rareDataReg));
718     loadPtr(Address(rareDataReg, FunctionRareData::offsetOfAllocationProfile() + ObjectAllocationProfile::offsetOfAllocator()), allocatorReg);
719     loadPtr(Address(rareDataReg, FunctionRareData::offsetOfAllocationProfile() + ObjectAllocationProfile::offsetOfStructure()), structureReg);
720     addSlowCase(branchTestPtr(Zero, allocatorReg));
721
722     emitAllocateJSObject(allocatorReg, structureReg, resultReg, scratchReg);
723     emitPutVirtualRegister(currentInstruction[1].u.operand);
724 }
725
726 void JIT::emitSlow_op_create_this(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
727 {
728     linkSlowCase(iter); // doesn't have rare data
729     linkSlowCase(iter); // doesn't have an allocation profile
730     linkSlowCase(iter); // allocation failed
731
732     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_create_this);
733     slowPathCall.call();
734 }
735
736 void JIT::emit_op_check_tdz(Instruction* currentInstruction)
737 {
738     emitGetVirtualRegister(currentInstruction[1].u.operand, regT0);
739     addSlowCase(branchTest64(Zero, regT0));
740 }
741
742 void JIT::emitSlow_op_check_tdz(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
743 {
744     linkSlowCase(iter);
745     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_throw_tdz_error);
746     slowPathCall.call();
747 }
748
749 void JIT::emit_op_profile_will_call(Instruction* currentInstruction)
750 {
751     Jump profilerDone = branchTestPtr(Zero, AbsoluteAddress(m_vm->enabledProfilerAddress()));
752     emitGetVirtualRegister(currentInstruction[1].u.operand, regT0);
753     callOperation(operationProfileWillCall, regT0);
754     profilerDone.link(this);
755 }
756
757 void JIT::emit_op_profile_did_call(Instruction* currentInstruction)
758 {
759     Jump profilerDone = branchTestPtr(Zero, AbsoluteAddress(m_vm->enabledProfilerAddress()));
760     emitGetVirtualRegister(currentInstruction[1].u.operand, regT0);
761     callOperation(operationProfileDidCall, regT0);
762     profilerDone.link(this);
763 }
764
765
766 // Slow cases
767
768 void JIT::emitSlow_op_to_this(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
769 {
770     linkSlowCase(iter);
771     linkSlowCase(iter);
772     linkSlowCase(iter);
773     linkSlowCase(iter);
774
775     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_to_this);
776     slowPathCall.call();
777 }
778
779 void JIT::emitSlow_op_to_primitive(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
780 {
781     linkSlowCase(iter);
782
783     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_to_primitive);
784     slowPathCall.call();
785 }
786
787 void JIT::emitSlow_op_not(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
788 {
789     linkSlowCase(iter);
790     
791     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_not);
792     slowPathCall.call();
793 }
794
795 void JIT::emitSlow_op_jfalse(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
796 {
797     linkSlowCase(iter);
798     callOperation(operationConvertJSValueToBoolean, regT0);
799     emitJumpSlowToHot(branchTest32(Zero, returnValueGPR), currentInstruction[2].u.operand); // inverted!
800 }
801
802 void JIT::emitSlow_op_jtrue(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
803 {
804     linkSlowCase(iter);
805     callOperation(operationConvertJSValueToBoolean, regT0);
806     emitJumpSlowToHot(branchTest32(NonZero, returnValueGPR), currentInstruction[2].u.operand);
807 }
808
809 void JIT::emitSlow_op_bitxor(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
810 {
811     linkSlowCase(iter);
812     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_bitxor);
813     slowPathCall.call();
814 }
815
816 void JIT::emitSlow_op_bitor(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
817 {
818     linkSlowCase(iter);
819     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_bitor);
820     slowPathCall.call();
821 }
822
823 void JIT::emitSlow_op_eq(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
824 {
825     linkSlowCase(iter);
826     callOperation(operationCompareEq, regT0, regT1);
827     emitTagAsBoolImmediate(returnValueGPR);
828     emitPutVirtualRegister(currentInstruction[1].u.operand, returnValueGPR);
829 }
830
831 void JIT::emitSlow_op_neq(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
832 {
833     linkSlowCase(iter);
834     callOperation(operationCompareEq, regT0, regT1);
835     xor32(TrustedImm32(0x1), regT0);
836     emitTagAsBoolImmediate(returnValueGPR);
837     emitPutVirtualRegister(currentInstruction[1].u.operand, returnValueGPR);
838 }
839
840 void JIT::emitSlow_op_stricteq(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
841 {
842     linkSlowCase(iter);
843     linkSlowCase(iter);
844     linkSlowCase(iter);
845     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_stricteq);
846     slowPathCall.call();
847 }
848
849 void JIT::emitSlow_op_nstricteq(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
850 {
851     linkSlowCase(iter);
852     linkSlowCase(iter);
853     linkSlowCase(iter);
854     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_nstricteq);
855     slowPathCall.call();
856 }
857
858 void JIT::emitSlow_op_check_has_instance(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
859 {
860     int dst = currentInstruction[1].u.operand;
861     int value = currentInstruction[2].u.operand;
862     int baseVal = currentInstruction[3].u.operand;
863
864     linkSlowCaseIfNotJSCell(iter, baseVal);
865     linkSlowCase(iter);
866     emitGetVirtualRegister(value, regT0);
867     emitGetVirtualRegister(baseVal, regT1);
868     callOperation(operationCheckHasInstance, dst, regT0, regT1);
869
870     emitJumpSlowToHot(jump(), currentInstruction[4].u.operand);
871 }
872
873 void JIT::emitSlow_op_instanceof(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
874 {
875     int dst = currentInstruction[1].u.operand;
876     int value = currentInstruction[2].u.operand;
877     int proto = currentInstruction[3].u.operand;
878
879     linkSlowCaseIfNotJSCell(iter, value);
880     linkSlowCaseIfNotJSCell(iter, proto);
881     linkSlowCase(iter);
882     emitGetVirtualRegister(value, regT0);
883     emitGetVirtualRegister(proto, regT1);
884     callOperation(operationInstanceOf, dst, regT0, regT1);
885 }
886
887 void JIT::emitSlow_op_to_number(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
888 {
889     linkSlowCase(iter);
890
891     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_to_number);
892     slowPathCall.call();
893 }
894
895 void JIT::emitSlow_op_to_string(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
896 {
897     linkSlowCase(iter); // Not JSCell.
898     linkSlowCase(iter); // Not JSString.
899
900     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_to_string);
901     slowPathCall.call();
902 }
903
904 #endif // USE(JSVALUE64)
905
906 void JIT::emit_op_loop_hint(Instruction*)
907 {
908     // Emit the JIT optimization check: 
909     if (canBeOptimized()) {
910         addSlowCase(branchAdd32(PositiveOrZero, TrustedImm32(Options::executionCounterIncrementForLoop()),
911             AbsoluteAddress(m_codeBlock->addressOfJITExecuteCounter())));
912     }
913
914     // Emit the watchdog timer check:
915     if (m_vm->watchdog && m_vm->watchdog->isEnabled())
916         addSlowCase(branchTest8(NonZero, AbsoluteAddress(m_vm->watchdog->timerDidFireAddress())));
917 }
918
919 void JIT::emitSlow_op_loop_hint(Instruction*, Vector<SlowCaseEntry>::iterator& iter)
920 {
921 #if ENABLE(DFG_JIT)
922     // Emit the slow path for the JIT optimization check:
923     if (canBeOptimized()) {
924         linkSlowCase(iter);
925         
926         callOperation(operationOptimize, m_bytecodeOffset);
927         Jump noOptimizedEntry = branchTestPtr(Zero, returnValueGPR);
928         if (!ASSERT_DISABLED) {
929             Jump ok = branchPtr(MacroAssembler::Above, regT0, TrustedImmPtr(bitwise_cast<void*>(static_cast<intptr_t>(1000))));
930             abortWithReason(JITUnreasonableLoopHintJumpTarget);
931             ok.link(this);
932         }
933         jump(returnValueGPR);
934         noOptimizedEntry.link(this);
935
936         emitJumpSlowToHot(jump(), OPCODE_LENGTH(op_loop_hint));
937     }
938 #endif
939
940     // Emit the slow path of the watchdog timer check:
941     if (m_vm->watchdog && m_vm->watchdog->isEnabled()) {
942         linkSlowCase(iter);
943         callOperation(operationHandleWatchdogTimer);
944
945         emitJumpSlowToHot(jump(), OPCODE_LENGTH(op_loop_hint));
946     }
947
948 }
949
950 void JIT::emit_op_new_regexp(Instruction* currentInstruction)
951 {
952     callOperation(operationNewRegexp, currentInstruction[1].u.operand, m_codeBlock->regexp(currentInstruction[2].u.operand));
953 }
954
955 void JIT::emit_op_new_func(Instruction* currentInstruction)
956 {
957     Jump lazyJump;
958     int dst = currentInstruction[1].u.operand;
959
960 #if USE(JSVALUE64)
961     emitGetVirtualRegister(currentInstruction[2].u.operand, regT0);
962 #else
963     emitLoadPayload(currentInstruction[2].u.operand, regT0);
964 #endif
965     FunctionExecutable* funcExec = m_codeBlock->functionDecl(currentInstruction[3].u.operand);
966     callOperation(operationNewFunction, dst, regT0, funcExec);
967 }
968
969 void JIT::emit_op_new_func_exp(Instruction* currentInstruction)
970 {
971     Jump notUndefinedScope;
972     int dst = currentInstruction[1].u.operand;
973 #if USE(JSVALUE64)
974     emitGetVirtualRegister(currentInstruction[2].u.operand, regT0);
975     notUndefinedScope = branch64(NotEqual, regT0, TrustedImm64(JSValue::encode(jsUndefined())));
976     store64(TrustedImm64(JSValue::encode(jsUndefined())), Address(callFrameRegister, sizeof(Register) * dst));
977 #else
978     emitLoadPayload(currentInstruction[2].u.operand, regT0);
979     notUndefinedScope = branch32(NotEqual, tagFor(currentInstruction[2].u.operand), TrustedImm32(JSValue::UndefinedTag));
980     emitStore(dst, jsUndefined());
981 #endif
982
983     Jump done = jump();
984     notUndefinedScope.link(this);
985
986     FunctionExecutable* funcExpr = m_codeBlock->functionExpr(currentInstruction[3].u.operand);
987     callOperation(operationNewFunction, dst, regT0, funcExpr);
988     done.link(this);
989 }
990
991 void JIT::emit_op_new_array(Instruction* currentInstruction)
992 {
993     int dst = currentInstruction[1].u.operand;
994     int valuesIndex = currentInstruction[2].u.operand;
995     int size = currentInstruction[3].u.operand;
996     addPtr(TrustedImm32(valuesIndex * sizeof(Register)), callFrameRegister, regT0);
997     callOperation(operationNewArrayWithProfile, dst,
998         currentInstruction[4].u.arrayAllocationProfile, regT0, size);
999 }
1000
1001 void JIT::emit_op_new_array_with_size(Instruction* currentInstruction)
1002 {
1003     int dst = currentInstruction[1].u.operand;
1004     int sizeIndex = currentInstruction[2].u.operand;
1005 #if USE(JSVALUE64)
1006     emitGetVirtualRegister(sizeIndex, regT0);
1007     callOperation(operationNewArrayWithSizeAndProfile, dst,
1008         currentInstruction[3].u.arrayAllocationProfile, regT0);
1009 #else
1010     emitLoad(sizeIndex, regT1, regT0);
1011     callOperation(operationNewArrayWithSizeAndProfile, dst,
1012         currentInstruction[3].u.arrayAllocationProfile, regT1, regT0);
1013 #endif
1014 }
1015
1016 void JIT::emit_op_new_array_buffer(Instruction* currentInstruction)
1017 {
1018     int dst = currentInstruction[1].u.operand;
1019     int valuesIndex = currentInstruction[2].u.operand;
1020     int size = currentInstruction[3].u.operand;
1021     const JSValue* values = codeBlock()->constantBuffer(valuesIndex);
1022     callOperation(operationNewArrayBufferWithProfile, dst, currentInstruction[4].u.arrayAllocationProfile, values, size);
1023 }
1024
1025 #if USE(JSVALUE64)
1026 void JIT::emit_op_has_structure_property(Instruction* currentInstruction)
1027 {
1028     int dst = currentInstruction[1].u.operand;
1029     int base = currentInstruction[2].u.operand;
1030     int enumerator = currentInstruction[4].u.operand;
1031
1032     emitGetVirtualRegister(base, regT0);
1033     emitGetVirtualRegister(enumerator, regT1);
1034     emitJumpSlowCaseIfNotJSCell(regT0, base);
1035
1036     load32(Address(regT0, JSCell::structureIDOffset()), regT0);
1037     addSlowCase(branch32(NotEqual, regT0, Address(regT1, JSPropertyNameEnumerator::cachedStructureIDOffset())));
1038     
1039     move(TrustedImm64(JSValue::encode(jsBoolean(true))), regT0);
1040     emitPutVirtualRegister(dst);
1041 }
1042
1043 void JIT::privateCompileHasIndexedProperty(ByValInfo* byValInfo, ReturnAddressPtr returnAddress, JITArrayMode arrayMode)
1044 {
1045     Instruction* currentInstruction = m_codeBlock->instructions().begin() + byValInfo->bytecodeIndex;
1046     
1047     PatchableJump badType;
1048     
1049     // FIXME: Add support for other types like TypedArrays and Arguments.
1050     // See https://bugs.webkit.org/show_bug.cgi?id=135033 and https://bugs.webkit.org/show_bug.cgi?id=135034.
1051     JumpList slowCases = emitLoadForArrayMode(currentInstruction, arrayMode, badType);
1052     move(TrustedImm64(JSValue::encode(jsBoolean(true))), regT0);
1053     Jump done = jump();
1054
1055     LinkBuffer patchBuffer(*m_vm, *this, m_codeBlock);
1056     
1057     patchBuffer.link(badType, CodeLocationLabel(MacroAssemblerCodePtr::createFromExecutableAddress(returnAddress.value())).labelAtOffset(byValInfo->returnAddressToSlowPath));
1058     patchBuffer.link(slowCases, CodeLocationLabel(MacroAssemblerCodePtr::createFromExecutableAddress(returnAddress.value())).labelAtOffset(byValInfo->returnAddressToSlowPath));
1059     
1060     patchBuffer.link(done, byValInfo->badTypeJump.labelAtOffset(byValInfo->badTypeJumpToDone));
1061     
1062     byValInfo->stubRoutine = FINALIZE_CODE_FOR_STUB(
1063         m_codeBlock, patchBuffer,
1064         ("Baseline has_indexed_property stub for %s, return point %p", toCString(*m_codeBlock).data(), returnAddress.value()));
1065     
1066     RepatchBuffer repatchBuffer(m_codeBlock);
1067     repatchBuffer.relink(byValInfo->badTypeJump, CodeLocationLabel(byValInfo->stubRoutine->code().code()));
1068     repatchBuffer.relinkCallerToFunction(returnAddress, FunctionPtr(operationHasIndexedPropertyGeneric));
1069 }
1070
1071 void JIT::emit_op_has_indexed_property(Instruction* currentInstruction)
1072 {
1073     int dst = currentInstruction[1].u.operand;
1074     int base = currentInstruction[2].u.operand;
1075     int property = currentInstruction[3].u.operand;
1076     ArrayProfile* profile = currentInstruction[4].u.arrayProfile;
1077     
1078     emitGetVirtualRegisters(base, regT0, property, regT1);
1079
1080     // This is technically incorrect - we're zero-extending an int32. On the hot path this doesn't matter.
1081     // We check the value as if it was a uint32 against the m_vectorLength - which will always fail if
1082     // number was signed since m_vectorLength is always less than intmax (since the total allocation
1083     // size is always less than 4Gb). As such zero extending will have been correct (and extending the value
1084     // to 64-bits is necessary since it's used in the address calculation. We zero extend rather than sign
1085     // extending since it makes it easier to re-tag the value in the slow case.
1086     zeroExtend32ToPtr(regT1, regT1);
1087
1088     emitJumpSlowCaseIfNotJSCell(regT0, base);
1089     emitArrayProfilingSiteWithCell(regT0, regT2, profile);
1090     and32(TrustedImm32(IndexingShapeMask), regT2);
1091
1092     JITArrayMode mode = chooseArrayMode(profile);
1093     PatchableJump badType;
1094
1095     // FIXME: Add support for other types like TypedArrays and Arguments.
1096     // See https://bugs.webkit.org/show_bug.cgi?id=135033 and https://bugs.webkit.org/show_bug.cgi?id=135034.
1097     JumpList slowCases = emitLoadForArrayMode(currentInstruction, mode, badType);
1098     
1099     move(TrustedImm64(JSValue::encode(jsBoolean(true))), regT0);
1100
1101     addSlowCase(badType);
1102     addSlowCase(slowCases);
1103     
1104     Label done = label();
1105     
1106     emitPutVirtualRegister(dst);
1107     
1108     m_byValCompilationInfo.append(ByValCompilationInfo(m_bytecodeOffset, badType, mode, done));
1109 }
1110
1111 void JIT::emitSlow_op_has_indexed_property(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
1112 {
1113     int dst = currentInstruction[1].u.operand;
1114     int base = currentInstruction[2].u.operand;
1115     int property = currentInstruction[3].u.operand;
1116     ArrayProfile* profile = currentInstruction[4].u.arrayProfile;
1117     
1118     linkSlowCaseIfNotJSCell(iter, base); // base cell check
1119     linkSlowCase(iter); // base array check
1120     
1121     Jump skipProfiling = jump();
1122     
1123     linkSlowCase(iter); // vector length check
1124     linkSlowCase(iter); // empty value
1125     
1126     emitArrayProfileOutOfBoundsSpecialCase(profile);
1127     
1128     skipProfiling.link(this);
1129     
1130     Label slowPath = label();
1131     
1132     emitGetVirtualRegister(base, regT0);
1133     emitGetVirtualRegister(property, regT1);
1134     Call call = callOperation(operationHasIndexedPropertyDefault, dst, regT0, regT1);
1135
1136     m_byValCompilationInfo[m_byValInstructionIndex].slowPathTarget = slowPath;
1137     m_byValCompilationInfo[m_byValInstructionIndex].returnAddress = call;
1138     m_byValInstructionIndex++;
1139 }
1140
1141 void JIT::emit_op_get_direct_pname(Instruction* currentInstruction)
1142 {
1143     int dst = currentInstruction[1].u.operand;
1144     int base = currentInstruction[2].u.operand;
1145     int index = currentInstruction[4].u.operand;
1146     int enumerator = currentInstruction[5].u.operand;
1147
1148     // Check that base is a cell
1149     emitGetVirtualRegister(base, regT0);
1150     emitJumpSlowCaseIfNotJSCell(regT0, base);
1151
1152     // Check the structure
1153     emitGetVirtualRegister(enumerator, regT2);
1154     load32(Address(regT0, JSCell::structureIDOffset()), regT1);
1155     addSlowCase(branch32(NotEqual, regT1, Address(regT2, JSPropertyNameEnumerator::cachedStructureIDOffset())));
1156
1157     // Compute the offset
1158     emitGetVirtualRegister(index, regT1);
1159     // If index is less than the enumerator's cached inline storage, then it's an inline access
1160     Jump outOfLineAccess = branch32(AboveOrEqual, regT1, Address(regT2, JSPropertyNameEnumerator::cachedInlineCapacityOffset()));
1161     addPtr(TrustedImm32(JSObject::offsetOfInlineStorage()), regT0);
1162     signExtend32ToPtr(regT1, regT1);
1163     load64(BaseIndex(regT0, regT1, TimesEight), regT0);
1164     
1165     Jump done = jump();
1166
1167     // Otherwise it's out of line
1168     outOfLineAccess.link(this);
1169     loadPtr(Address(regT0, JSObject::butterflyOffset()), regT0);
1170     sub32(Address(regT2, JSPropertyNameEnumerator::cachedInlineCapacityOffset()), regT1);
1171     neg32(regT1);
1172     signExtend32ToPtr(regT1, regT1);
1173     int32_t offsetOfFirstProperty = static_cast<int32_t>(offsetInButterfly(firstOutOfLineOffset)) * sizeof(EncodedJSValue);
1174     load64(BaseIndex(regT0, regT1, TimesEight, offsetOfFirstProperty), regT0);
1175     
1176     done.link(this);
1177     emitValueProfilingSite();
1178     emitPutVirtualRegister(dst, regT0);
1179 }
1180
1181 void JIT::emitSlow_op_get_direct_pname(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
1182 {
1183     int base = currentInstruction[2].u.operand;
1184     linkSlowCaseIfNotJSCell(iter, base);
1185     linkSlowCase(iter);
1186
1187     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_get_direct_pname);
1188     slowPathCall.call();
1189 }
1190
1191 void JIT::emit_op_enumerator_structure_pname(Instruction* currentInstruction)
1192 {
1193     int dst = currentInstruction[1].u.operand;
1194     int enumerator = currentInstruction[2].u.operand;
1195     int index = currentInstruction[3].u.operand;
1196
1197     emitGetVirtualRegister(index, regT0);
1198     emitGetVirtualRegister(enumerator, regT1);
1199     Jump inBounds = branch32(Below, regT0, Address(regT1, JSPropertyNameEnumerator::endStructurePropertyIndexOffset()));
1200
1201     move(TrustedImm64(JSValue::encode(jsNull())), regT0);
1202
1203     Jump done = jump();
1204     inBounds.link(this);
1205
1206     loadPtr(Address(regT1, JSPropertyNameEnumerator::cachedPropertyNamesVectorOffset()), regT1);
1207     signExtend32ToPtr(regT0, regT0);
1208     load64(BaseIndex(regT1, regT0, TimesEight), regT0);
1209
1210     done.link(this);
1211     emitPutVirtualRegister(dst);
1212 }
1213
1214 void JIT::emit_op_enumerator_generic_pname(Instruction* currentInstruction)
1215 {
1216     int dst = currentInstruction[1].u.operand;
1217     int enumerator = currentInstruction[2].u.operand;
1218     int index = currentInstruction[3].u.operand;
1219
1220     emitGetVirtualRegister(index, regT0);
1221     emitGetVirtualRegister(enumerator, regT1);
1222     Jump inBounds = branch32(Below, regT0, Address(regT1, JSPropertyNameEnumerator::endGenericPropertyIndexOffset()));
1223
1224     move(TrustedImm64(JSValue::encode(jsNull())), regT0);
1225
1226     Jump done = jump();
1227     inBounds.link(this);
1228
1229     loadPtr(Address(regT1, JSPropertyNameEnumerator::cachedPropertyNamesVectorOffset()), regT1);
1230     signExtend32ToPtr(regT0, regT0);
1231     load64(BaseIndex(regT1, regT0, TimesEight), regT0);
1232     
1233     done.link(this);
1234     emitPutVirtualRegister(dst);
1235 }
1236
1237 void JIT::emit_op_profile_type(Instruction* currentInstruction)
1238 {
1239     TypeLocation* cachedTypeLocation = currentInstruction[2].u.location;
1240     int valueToProfile = currentInstruction[1].u.operand;
1241
1242     emitGetVirtualRegister(valueToProfile, regT0);
1243
1244     JumpList jumpToEnd;
1245
1246     // Compile in a predictive type check, if possible, to see if we can skip writing to the log.
1247     // These typechecks are inlined to match those of the 64-bit JSValue type checks.
1248     if (cachedTypeLocation->m_lastSeenType == TypeUndefined)
1249         jumpToEnd.append(branch64(Equal, regT0, TrustedImm64(JSValue::encode(jsUndefined()))));
1250     else if (cachedTypeLocation->m_lastSeenType == TypeNull)
1251         jumpToEnd.append(branch64(Equal, regT0, TrustedImm64(JSValue::encode(jsNull()))));
1252     else if (cachedTypeLocation->m_lastSeenType == TypeBoolean) {
1253         move(regT0, regT1);
1254         and64(TrustedImm32(~1), regT1);
1255         jumpToEnd.append(branch64(Equal, regT1, TrustedImm64(ValueFalse)));
1256     } else if (cachedTypeLocation->m_lastSeenType == TypeMachineInt)
1257         jumpToEnd.append(emitJumpIfImmediateInteger(regT0));
1258     else if (cachedTypeLocation->m_lastSeenType == TypeNumber)
1259         jumpToEnd.append(emitJumpIfImmediateNumber(regT0));
1260     else if (cachedTypeLocation->m_lastSeenType == TypeString) {
1261         Jump isNotCell = emitJumpIfNotJSCell(regT0);
1262         jumpToEnd.append(branch8(Equal, Address(regT0, JSCell::typeInfoTypeOffset()), TrustedImm32(StringType)));
1263         isNotCell.link(this);
1264     }
1265
1266     // Load the type profiling log into T2.
1267     TypeProfilerLog* cachedTypeProfilerLog = m_vm->typeProfilerLog();
1268     move(TrustedImmPtr(cachedTypeProfilerLog), regT2);
1269     // Load the next log entry into T1.
1270     loadPtr(Address(regT2, TypeProfilerLog::currentLogEntryOffset()), regT1);
1271
1272     // Store the JSValue onto the log entry.
1273     store64(regT0, Address(regT1, TypeProfilerLog::LogEntry::valueOffset()));
1274
1275     // Store the structureID of the cell if T0 is a cell, otherwise, store 0 on the log entry.
1276     Jump notCell = emitJumpIfNotJSCell(regT0);
1277     load32(Address(regT0, JSCell::structureIDOffset()), regT0);
1278     store32(regT0, Address(regT1, TypeProfilerLog::LogEntry::structureIDOffset()));
1279     Jump skipIsCell = jump();
1280     notCell.link(this);
1281     store32(TrustedImm32(0), Address(regT1, TypeProfilerLog::LogEntry::structureIDOffset()));
1282     skipIsCell.link(this);
1283
1284     // Store the typeLocation on the log entry.
1285     move(TrustedImmPtr(cachedTypeLocation), regT0);
1286     store64(regT0, Address(regT1, TypeProfilerLog::LogEntry::locationOffset()));
1287
1288     // Increment the current log entry.
1289     addPtr(TrustedImm32(sizeof(TypeProfilerLog::LogEntry)), regT1);
1290     store64(regT1, Address(regT2, TypeProfilerLog::currentLogEntryOffset()));
1291     Jump skipClearLog = branchPtr(NotEqual, regT1, TrustedImmPtr(cachedTypeProfilerLog->logEndPtr()));
1292     // Clear the log if we're at the end of the log.
1293     callOperation(operationProcessTypeProfilerLog);
1294     skipClearLog.link(this);
1295
1296     jumpToEnd.link(this);
1297 }
1298
1299 #endif // USE(JSVALUE64)
1300
1301 void JIT::emit_op_get_enumerable_length(Instruction* currentInstruction)
1302 {
1303     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_get_enumerable_length);
1304     slowPathCall.call();
1305 }
1306
1307 void JIT::emitSlow_op_has_structure_property(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
1308 {
1309     linkSlowCase(iter);
1310     linkSlowCase(iter);
1311
1312     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_has_structure_property);
1313     slowPathCall.call();
1314 }
1315
1316 void JIT::emit_op_has_generic_property(Instruction* currentInstruction)
1317 {
1318     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_has_generic_property);
1319     slowPathCall.call();
1320 }
1321
1322 void JIT::emit_op_get_property_enumerator(Instruction* currentInstruction)
1323 {
1324     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_get_property_enumerator);
1325     slowPathCall.call();
1326 }
1327
1328 void JIT::emit_op_to_index_string(Instruction* currentInstruction)
1329 {
1330     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_to_index_string);
1331     slowPathCall.call();
1332 }
1333
1334 void JIT::emit_op_profile_control_flow(Instruction* currentInstruction)
1335 {
1336     BasicBlockLocation* basicBlockLocation = currentInstruction[1].u.basicBlockLocation;
1337     if (!basicBlockLocation->hasExecuted())
1338         basicBlockLocation->emitExecuteCode(*this, regT1);
1339 }
1340
1341 void JIT::emit_op_create_direct_arguments(Instruction* currentInstruction)
1342 {
1343     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_create_direct_arguments);
1344     slowPathCall.call();
1345 }
1346
1347 void JIT::emit_op_create_scoped_arguments(Instruction* currentInstruction)
1348 {
1349     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_create_scoped_arguments);
1350     slowPathCall.call();
1351 }
1352
1353 void JIT::emit_op_create_out_of_band_arguments(Instruction* currentInstruction)
1354 {
1355     JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_create_out_of_band_arguments);
1356     slowPathCall.call();
1357 }
1358
1359 } // namespace JSC
1360
1361 #endif // ENABLE(JIT)