MovHint should be a strong use
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGSpeculativeJIT.cpp
1 /*
2  * Copyright (C) 2011-2015 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 "DFGSpeculativeJIT.h"
28
29 #if ENABLE(DFG_JIT)
30
31 #include "BinarySwitch.h"
32 #include "DFGAbstractInterpreterInlines.h"
33 #include "DFGArrayifySlowPathGenerator.h"
34 #include "DFGCallArrayAllocatorSlowPathGenerator.h"
35 #include "DFGCallCreateDirectArgumentsSlowPathGenerator.h"
36 #include "DFGMayExit.h"
37 #include "DFGSaneStringGetByValSlowPathGenerator.h"
38 #include "DFGSlowPathGenerator.h"
39 #include "DirectArguments.h"
40 #include "JSCInlines.h"
41 #include "JSEnvironmentRecord.h"
42 #include "JSLexicalEnvironment.h"
43 #include "LinkBuffer.h"
44 #include "ScopedArguments.h"
45 #include "ScratchRegisterAllocator.h"
46 #include "WriteBarrierBuffer.h"
47 #include <wtf/MathExtras.h>
48
49 namespace JSC { namespace DFG {
50
51 SpeculativeJIT::SpeculativeJIT(JITCompiler& jit)
52     : m_compileOkay(true)
53     , m_jit(jit)
54     , m_currentNode(0)
55     , m_lastGeneratedNode(LastNodeType)
56     , m_indexInBlock(0)
57     , m_generationInfo(m_jit.graph().frameRegisterCount())
58     , m_state(m_jit.graph())
59     , m_interpreter(m_jit.graph(), m_state)
60     , m_stream(&jit.jitCode()->variableEventStream)
61     , m_minifiedGraph(&jit.jitCode()->minifiedDFG)
62     , m_isCheckingArgumentTypes(false)
63 {
64 }
65
66 SpeculativeJIT::~SpeculativeJIT()
67 {
68 }
69
70 void SpeculativeJIT::emitAllocateJSArray(GPRReg resultGPR, Structure* structure, GPRReg storageGPR, unsigned numElements)
71 {
72     ASSERT(hasUndecided(structure->indexingType()) || hasInt32(structure->indexingType()) || hasDouble(structure->indexingType()) || hasContiguous(structure->indexingType()));
73     
74     GPRTemporary scratch(this);
75     GPRTemporary scratch2(this);
76     GPRReg scratchGPR = scratch.gpr();
77     GPRReg scratch2GPR = scratch2.gpr();
78     
79     unsigned vectorLength = std::max(BASE_VECTOR_LEN, numElements);
80     
81     JITCompiler::JumpList slowCases;
82     
83     slowCases.append(
84         emitAllocateBasicStorage(TrustedImm32(vectorLength * sizeof(JSValue) + sizeof(IndexingHeader)), storageGPR));
85     m_jit.subPtr(TrustedImm32(vectorLength * sizeof(JSValue)), storageGPR);
86     emitAllocateJSObject<JSArray>(resultGPR, TrustedImmPtr(structure), storageGPR, scratchGPR, scratch2GPR, slowCases);
87     
88     m_jit.store32(TrustedImm32(numElements), MacroAssembler::Address(storageGPR, Butterfly::offsetOfPublicLength()));
89     m_jit.store32(TrustedImm32(vectorLength), MacroAssembler::Address(storageGPR, Butterfly::offsetOfVectorLength()));
90     
91     if (hasDouble(structure->indexingType()) && numElements < vectorLength) {
92 #if USE(JSVALUE64)
93         m_jit.move(TrustedImm64(bitwise_cast<int64_t>(PNaN)), scratchGPR);
94         for (unsigned i = numElements; i < vectorLength; ++i)
95             m_jit.store64(scratchGPR, MacroAssembler::Address(storageGPR, sizeof(double) * i));
96 #else
97         EncodedValueDescriptor value;
98         value.asInt64 = JSValue::encode(JSValue(JSValue::EncodeAsDouble, PNaN));
99         for (unsigned i = numElements; i < vectorLength; ++i) {
100             m_jit.store32(TrustedImm32(value.asBits.tag), MacroAssembler::Address(storageGPR, sizeof(double) * i + OBJECT_OFFSETOF(JSValue, u.asBits.tag)));
101             m_jit.store32(TrustedImm32(value.asBits.payload), MacroAssembler::Address(storageGPR, sizeof(double) * i + OBJECT_OFFSETOF(JSValue, u.asBits.payload)));
102         }
103 #endif
104     }
105     
106     // I want a slow path that also loads out the storage pointer, and that's
107     // what this custom CallArrayAllocatorSlowPathGenerator gives me. It's a lot
108     // of work for a very small piece of functionality. :-/
109     addSlowPathGenerator(std::make_unique<CallArrayAllocatorSlowPathGenerator>(
110         slowCases, this, operationNewArrayWithSize, resultGPR, storageGPR,
111         structure, numElements));
112 }
113
114 void SpeculativeJIT::emitGetLength(InlineCallFrame* inlineCallFrame, GPRReg lengthGPR, bool includeThis)
115 {
116     if (inlineCallFrame && !inlineCallFrame->isVarargs())
117         m_jit.move(TrustedImm32(inlineCallFrame->arguments.size() - !includeThis), lengthGPR);
118     else {
119         VirtualRegister argumentCountRegister;
120         if (!inlineCallFrame)
121             argumentCountRegister = VirtualRegister(JSStack::ArgumentCount);
122         else
123             argumentCountRegister = inlineCallFrame->argumentCountRegister;
124         m_jit.load32(JITCompiler::payloadFor(argumentCountRegister), lengthGPR);
125         if (!includeThis)
126             m_jit.sub32(TrustedImm32(1), lengthGPR);
127     }
128 }
129
130 void SpeculativeJIT::emitGetLength(CodeOrigin origin, GPRReg lengthGPR, bool includeThis)
131 {
132     emitGetLength(origin.inlineCallFrame, lengthGPR, includeThis);
133 }
134
135 void SpeculativeJIT::emitGetCallee(CodeOrigin origin, GPRReg calleeGPR)
136 {
137     if (origin.inlineCallFrame) {
138         if (origin.inlineCallFrame->isClosureCall) {
139             m_jit.loadPtr(
140                 JITCompiler::addressFor(origin.inlineCallFrame->calleeRecovery.virtualRegister()),
141                 calleeGPR);
142         } else {
143             m_jit.move(
144                 TrustedImmPtr(origin.inlineCallFrame->calleeRecovery.constant().asCell()),
145                 calleeGPR);
146         }
147     } else
148         m_jit.loadPtr(JITCompiler::addressFor(JSStack::Callee), calleeGPR);
149 }
150
151 void SpeculativeJIT::emitGetArgumentStart(CodeOrigin origin, GPRReg startGPR)
152 {
153     m_jit.addPtr(
154         TrustedImm32(
155             JITCompiler::argumentsStart(origin).offset() * static_cast<int>(sizeof(Register))),
156         GPRInfo::callFrameRegister, startGPR);
157 }
158
159 void SpeculativeJIT::speculationCheck(ExitKind kind, JSValueSource jsValueSource, Node* node, MacroAssembler::Jump jumpToFail)
160 {
161     if (!m_compileOkay)
162         return;
163     ASSERT(m_isCheckingArgumentTypes || m_canExit);
164     m_jit.appendExitInfo(jumpToFail);
165     m_jit.jitCode()->appendOSRExit(OSRExit(kind, jsValueSource, m_jit.graph().methodOfGettingAValueProfileFor(node), this, m_stream->size()));
166 }
167
168 void SpeculativeJIT::speculationCheck(ExitKind kind, JSValueSource jsValueSource, Node* node, const MacroAssembler::JumpList& jumpsToFail)
169 {
170     if (!m_compileOkay)
171         return;
172     ASSERT(m_isCheckingArgumentTypes || m_canExit);
173     m_jit.appendExitInfo(jumpsToFail);
174     m_jit.jitCode()->appendOSRExit(OSRExit(kind, jsValueSource, m_jit.graph().methodOfGettingAValueProfileFor(node), this, m_stream->size()));
175 }
176
177 OSRExitJumpPlaceholder SpeculativeJIT::speculationCheck(ExitKind kind, JSValueSource jsValueSource, Node* node)
178 {
179     if (!m_compileOkay)
180         return OSRExitJumpPlaceholder();
181     ASSERT(m_isCheckingArgumentTypes || m_canExit);
182     unsigned index = m_jit.jitCode()->osrExit.size();
183     m_jit.appendExitInfo();
184     m_jit.jitCode()->appendOSRExit(OSRExit(kind, jsValueSource, m_jit.graph().methodOfGettingAValueProfileFor(node), this, m_stream->size()));
185     return OSRExitJumpPlaceholder(index);
186 }
187
188 OSRExitJumpPlaceholder SpeculativeJIT::speculationCheck(ExitKind kind, JSValueSource jsValueSource, Edge nodeUse)
189 {
190     ASSERT(m_isCheckingArgumentTypes || m_canExit);
191     return speculationCheck(kind, jsValueSource, nodeUse.node());
192 }
193
194 void SpeculativeJIT::speculationCheck(ExitKind kind, JSValueSource jsValueSource, Edge nodeUse, MacroAssembler::Jump jumpToFail)
195 {
196     ASSERT(m_isCheckingArgumentTypes || m_canExit);
197     speculationCheck(kind, jsValueSource, nodeUse.node(), jumpToFail);
198 }
199
200 void SpeculativeJIT::speculationCheck(ExitKind kind, JSValueSource jsValueSource, Edge nodeUse, const MacroAssembler::JumpList& jumpsToFail)
201 {
202     ASSERT(m_isCheckingArgumentTypes || m_canExit);
203     speculationCheck(kind, jsValueSource, nodeUse.node(), jumpsToFail);
204 }
205
206 void SpeculativeJIT::speculationCheck(ExitKind kind, JSValueSource jsValueSource, Node* node, MacroAssembler::Jump jumpToFail, const SpeculationRecovery& recovery)
207 {
208     if (!m_compileOkay)
209         return;
210     ASSERT(m_isCheckingArgumentTypes || m_canExit);
211     unsigned recoveryIndex = m_jit.jitCode()->appendSpeculationRecovery(recovery);
212     m_jit.appendExitInfo(jumpToFail);
213     m_jit.jitCode()->appendOSRExit(OSRExit(kind, jsValueSource, m_jit.graph().methodOfGettingAValueProfileFor(node), this, m_stream->size(), recoveryIndex));
214 }
215
216 void SpeculativeJIT::speculationCheck(ExitKind kind, JSValueSource jsValueSource, Edge nodeUse, MacroAssembler::Jump jumpToFail, const SpeculationRecovery& recovery)
217 {
218     ASSERT(m_isCheckingArgumentTypes || m_canExit);
219     speculationCheck(kind, jsValueSource, nodeUse.node(), jumpToFail, recovery);
220 }
221
222 void SpeculativeJIT::emitInvalidationPoint(Node* node)
223 {
224     if (!m_compileOkay)
225         return;
226     ASSERT(m_canExit);
227     OSRExitCompilationInfo& info = m_jit.appendExitInfo(JITCompiler::JumpList());
228     m_jit.jitCode()->appendOSRExit(OSRExit(
229         UncountableInvalidation, JSValueSource(),
230         m_jit.graph().methodOfGettingAValueProfileFor(node),
231         this, m_stream->size()));
232     info.m_replacementSource = m_jit.watchpointLabel();
233     ASSERT(info.m_replacementSource.isSet());
234     noResult(node);
235 }
236
237 void SpeculativeJIT::terminateSpeculativeExecution(ExitKind kind, JSValueRegs jsValueRegs, Node* node)
238 {
239     ASSERT(m_isCheckingArgumentTypes || m_canExit);
240     if (!m_compileOkay)
241         return;
242     speculationCheck(kind, jsValueRegs, node, m_jit.jump());
243     m_compileOkay = false;
244     if (verboseCompilationEnabled())
245         dataLog("Bailing compilation.\n");
246 }
247
248 void SpeculativeJIT::terminateSpeculativeExecution(ExitKind kind, JSValueRegs jsValueRegs, Edge nodeUse)
249 {
250     ASSERT(m_isCheckingArgumentTypes || m_canExit);
251     terminateSpeculativeExecution(kind, jsValueRegs, nodeUse.node());
252 }
253
254 void SpeculativeJIT::typeCheck(JSValueSource source, Edge edge, SpeculatedType typesPassedThrough, MacroAssembler::Jump jumpToFail)
255 {
256     ASSERT(needsTypeCheck(edge, typesPassedThrough));
257     m_interpreter.filter(edge, typesPassedThrough);
258     speculationCheck(BadType, source, edge.node(), jumpToFail);
259 }
260
261 RegisterSet SpeculativeJIT::usedRegisters()
262 {
263     RegisterSet result;
264     
265     for (unsigned i = GPRInfo::numberOfRegisters; i--;) {
266         GPRReg gpr = GPRInfo::toRegister(i);
267         if (m_gprs.isInUse(gpr))
268             result.set(gpr);
269     }
270     for (unsigned i = FPRInfo::numberOfRegisters; i--;) {
271         FPRReg fpr = FPRInfo::toRegister(i);
272         if (m_fprs.isInUse(fpr))
273             result.set(fpr);
274     }
275     
276     result.merge(RegisterSet::specialRegisters());
277     
278     return result;
279 }
280
281 void SpeculativeJIT::addSlowPathGenerator(std::unique_ptr<SlowPathGenerator> slowPathGenerator)
282 {
283     m_slowPathGenerators.append(WTF::move(slowPathGenerator));
284 }
285
286 void SpeculativeJIT::runSlowPathGenerators()
287 {
288     for (unsigned i = 0; i < m_slowPathGenerators.size(); ++i)
289         m_slowPathGenerators[i]->generate(this);
290 }
291
292 // On Windows we need to wrap fmod; on other platforms we can call it directly.
293 // On ARMv7 we assert that all function pointers have to low bit set (point to thumb code).
294 #if CALLING_CONVENTION_IS_STDCALL || CPU(ARM_THUMB2)
295 static double JIT_OPERATION fmodAsDFGOperation(double x, double y)
296 {
297     return fmod(x, y);
298 }
299 #else
300 #define fmodAsDFGOperation fmod
301 #endif
302
303 void SpeculativeJIT::clearGenerationInfo()
304 {
305     for (unsigned i = 0; i < m_generationInfo.size(); ++i)
306         m_generationInfo[i] = GenerationInfo();
307     m_gprs = RegisterBank<GPRInfo>();
308     m_fprs = RegisterBank<FPRInfo>();
309 }
310
311 SilentRegisterSavePlan SpeculativeJIT::silentSavePlanForGPR(VirtualRegister spillMe, GPRReg source)
312 {
313     GenerationInfo& info = generationInfoFromVirtualRegister(spillMe);
314     Node* node = info.node();
315     DataFormat registerFormat = info.registerFormat();
316     ASSERT(registerFormat != DataFormatNone);
317     ASSERT(registerFormat != DataFormatDouble);
318         
319     SilentSpillAction spillAction;
320     SilentFillAction fillAction;
321         
322     if (!info.needsSpill())
323         spillAction = DoNothingForSpill;
324     else {
325 #if USE(JSVALUE64)
326         ASSERT(info.gpr() == source);
327         if (registerFormat == DataFormatInt32)
328             spillAction = Store32Payload;
329         else if (registerFormat == DataFormatCell || registerFormat == DataFormatStorage)
330             spillAction = StorePtr;
331         else if (registerFormat == DataFormatInt52 || registerFormat == DataFormatStrictInt52)
332             spillAction = Store64;
333         else {
334             ASSERT(registerFormat & DataFormatJS);
335             spillAction = Store64;
336         }
337 #elif USE(JSVALUE32_64)
338         if (registerFormat & DataFormatJS) {
339             ASSERT(info.tagGPR() == source || info.payloadGPR() == source);
340             spillAction = source == info.tagGPR() ? Store32Tag : Store32Payload;
341         } else {
342             ASSERT(info.gpr() == source);
343             spillAction = Store32Payload;
344         }
345 #endif
346     }
347         
348     if (registerFormat == DataFormatInt32) {
349         ASSERT(info.gpr() == source);
350         ASSERT(isJSInt32(info.registerFormat()));
351         if (node->hasConstant()) {
352             ASSERT(node->isInt32Constant());
353             fillAction = SetInt32Constant;
354         } else
355             fillAction = Load32Payload;
356     } else if (registerFormat == DataFormatBoolean) {
357 #if USE(JSVALUE64)
358         RELEASE_ASSERT_NOT_REACHED();
359 #if COMPILER_QUIRK(CONSIDERS_UNREACHABLE_CODE)
360         fillAction = DoNothingForFill;
361 #endif
362 #elif USE(JSVALUE32_64)
363         ASSERT(info.gpr() == source);
364         if (node->hasConstant()) {
365             ASSERT(node->isBooleanConstant());
366             fillAction = SetBooleanConstant;
367         } else
368             fillAction = Load32Payload;
369 #endif
370     } else if (registerFormat == DataFormatCell) {
371         ASSERT(info.gpr() == source);
372         if (node->hasConstant()) {
373             DFG_ASSERT(m_jit.graph(), m_currentNode, node->isCellConstant());
374             node->asCell(); // To get the assertion.
375             fillAction = SetCellConstant;
376         } else {
377 #if USE(JSVALUE64)
378             fillAction = LoadPtr;
379 #else
380             fillAction = Load32Payload;
381 #endif
382         }
383     } else if (registerFormat == DataFormatStorage) {
384         ASSERT(info.gpr() == source);
385         fillAction = LoadPtr;
386     } else if (registerFormat == DataFormatInt52) {
387         if (node->hasConstant())
388             fillAction = SetInt52Constant;
389         else if (info.spillFormat() == DataFormatInt52)
390             fillAction = Load64;
391         else if (info.spillFormat() == DataFormatStrictInt52)
392             fillAction = Load64ShiftInt52Left;
393         else if (info.spillFormat() == DataFormatNone)
394             fillAction = Load64;
395         else {
396             RELEASE_ASSERT_NOT_REACHED();
397 #if COMPILER_QUIRK(CONSIDERS_UNREACHABLE_CODE)
398             fillAction = Load64; // Make GCC happy.
399 #endif
400         }
401     } else if (registerFormat == DataFormatStrictInt52) {
402         if (node->hasConstant())
403             fillAction = SetStrictInt52Constant;
404         else if (info.spillFormat() == DataFormatInt52)
405             fillAction = Load64ShiftInt52Right;
406         else if (info.spillFormat() == DataFormatStrictInt52)
407             fillAction = Load64;
408         else if (info.spillFormat() == DataFormatNone)
409             fillAction = Load64;
410         else {
411             RELEASE_ASSERT_NOT_REACHED();
412 #if COMPILER_QUIRK(CONSIDERS_UNREACHABLE_CODE)
413             fillAction = Load64; // Make GCC happy.
414 #endif
415         }
416     } else {
417         ASSERT(registerFormat & DataFormatJS);
418 #if USE(JSVALUE64)
419         ASSERT(info.gpr() == source);
420         if (node->hasConstant()) {
421             if (node->isCellConstant())
422                 fillAction = SetTrustedJSConstant;
423             else
424                 fillAction = SetJSConstant;
425         } else if (info.spillFormat() == DataFormatInt32) {
426             ASSERT(registerFormat == DataFormatJSInt32);
427             fillAction = Load32PayloadBoxInt;
428         } else
429             fillAction = Load64;
430 #else
431         ASSERT(info.tagGPR() == source || info.payloadGPR() == source);
432         if (node->hasConstant())
433             fillAction = info.tagGPR() == source ? SetJSConstantTag : SetJSConstantPayload;
434         else if (info.payloadGPR() == source)
435             fillAction = Load32Payload;
436         else { // Fill the Tag
437             switch (info.spillFormat()) {
438             case DataFormatInt32:
439                 ASSERT(registerFormat == DataFormatJSInt32);
440                 fillAction = SetInt32Tag;
441                 break;
442             case DataFormatCell:
443                 ASSERT(registerFormat == DataFormatJSCell);
444                 fillAction = SetCellTag;
445                 break;
446             case DataFormatBoolean:
447                 ASSERT(registerFormat == DataFormatJSBoolean);
448                 fillAction = SetBooleanTag;
449                 break;
450             default:
451                 fillAction = Load32Tag;
452                 break;
453             }
454         }
455 #endif
456     }
457         
458     return SilentRegisterSavePlan(spillAction, fillAction, node, source);
459 }
460     
461 SilentRegisterSavePlan SpeculativeJIT::silentSavePlanForFPR(VirtualRegister spillMe, FPRReg source)
462 {
463     GenerationInfo& info = generationInfoFromVirtualRegister(spillMe);
464     Node* node = info.node();
465     ASSERT(info.registerFormat() == DataFormatDouble);
466
467     SilentSpillAction spillAction;
468     SilentFillAction fillAction;
469         
470     if (!info.needsSpill())
471         spillAction = DoNothingForSpill;
472     else {
473         ASSERT(!node->hasConstant());
474         ASSERT(info.spillFormat() == DataFormatNone);
475         ASSERT(info.fpr() == source);
476         spillAction = StoreDouble;
477     }
478         
479 #if USE(JSVALUE64)
480     if (node->hasConstant()) {
481         node->asNumber(); // To get the assertion.
482         fillAction = SetDoubleConstant;
483     } else {
484         ASSERT(info.spillFormat() == DataFormatNone || info.spillFormat() == DataFormatDouble);
485         fillAction = LoadDouble;
486     }
487 #elif USE(JSVALUE32_64)
488     ASSERT(info.registerFormat() == DataFormatDouble);
489     if (node->hasConstant()) {
490         node->asNumber(); // To get the assertion.
491         fillAction = SetDoubleConstant;
492     } else
493         fillAction = LoadDouble;
494 #endif
495
496     return SilentRegisterSavePlan(spillAction, fillAction, node, source);
497 }
498     
499 void SpeculativeJIT::silentSpill(const SilentRegisterSavePlan& plan)
500 {
501     switch (plan.spillAction()) {
502     case DoNothingForSpill:
503         break;
504     case Store32Tag:
505         m_jit.store32(plan.gpr(), JITCompiler::tagFor(plan.node()->virtualRegister()));
506         break;
507     case Store32Payload:
508         m_jit.store32(plan.gpr(), JITCompiler::payloadFor(plan.node()->virtualRegister()));
509         break;
510     case StorePtr:
511         m_jit.storePtr(plan.gpr(), JITCompiler::addressFor(plan.node()->virtualRegister()));
512         break;
513 #if USE(JSVALUE64)
514     case Store64:
515         m_jit.store64(plan.gpr(), JITCompiler::addressFor(plan.node()->virtualRegister()));
516         break;
517 #endif
518     case StoreDouble:
519         m_jit.storeDouble(plan.fpr(), JITCompiler::addressFor(plan.node()->virtualRegister()));
520         break;
521     default:
522         RELEASE_ASSERT_NOT_REACHED();
523     }
524 }
525     
526 void SpeculativeJIT::silentFill(const SilentRegisterSavePlan& plan, GPRReg canTrample)
527 {
528 #if USE(JSVALUE32_64)
529     UNUSED_PARAM(canTrample);
530 #endif
531     switch (plan.fillAction()) {
532     case DoNothingForFill:
533         break;
534     case SetInt32Constant:
535         m_jit.move(Imm32(plan.node()->asInt32()), plan.gpr());
536         break;
537 #if USE(JSVALUE64)
538     case SetInt52Constant:
539         m_jit.move(Imm64(plan.node()->asMachineInt() << JSValue::int52ShiftAmount), plan.gpr());
540         break;
541     case SetStrictInt52Constant:
542         m_jit.move(Imm64(plan.node()->asMachineInt()), plan.gpr());
543         break;
544 #endif // USE(JSVALUE64)
545     case SetBooleanConstant:
546         m_jit.move(TrustedImm32(plan.node()->asBoolean()), plan.gpr());
547         break;
548     case SetCellConstant:
549         m_jit.move(TrustedImmPtr(plan.node()->asCell()), plan.gpr());
550         break;
551 #if USE(JSVALUE64)
552     case SetTrustedJSConstant:
553         m_jit.move(valueOfJSConstantAsImm64(plan.node()).asTrustedImm64(), plan.gpr());
554         break;
555     case SetJSConstant:
556         m_jit.move(valueOfJSConstantAsImm64(plan.node()), plan.gpr());
557         break;
558     case SetDoubleConstant:
559         m_jit.move(Imm64(reinterpretDoubleToInt64(plan.node()->asNumber())), canTrample);
560         m_jit.move64ToDouble(canTrample, plan.fpr());
561         break;
562     case Load32PayloadBoxInt:
563         m_jit.load32(JITCompiler::payloadFor(plan.node()->virtualRegister()), plan.gpr());
564         m_jit.or64(GPRInfo::tagTypeNumberRegister, plan.gpr());
565         break;
566     case Load32PayloadConvertToInt52:
567         m_jit.load32(JITCompiler::payloadFor(plan.node()->virtualRegister()), plan.gpr());
568         m_jit.signExtend32ToPtr(plan.gpr(), plan.gpr());
569         m_jit.lshift64(TrustedImm32(JSValue::int52ShiftAmount), plan.gpr());
570         break;
571     case Load32PayloadSignExtend:
572         m_jit.load32(JITCompiler::payloadFor(plan.node()->virtualRegister()), plan.gpr());
573         m_jit.signExtend32ToPtr(plan.gpr(), plan.gpr());
574         break;
575 #else
576     case SetJSConstantTag:
577         m_jit.move(Imm32(plan.node()->asJSValue().tag()), plan.gpr());
578         break;
579     case SetJSConstantPayload:
580         m_jit.move(Imm32(plan.node()->asJSValue().payload()), plan.gpr());
581         break;
582     case SetInt32Tag:
583         m_jit.move(TrustedImm32(JSValue::Int32Tag), plan.gpr());
584         break;
585     case SetCellTag:
586         m_jit.move(TrustedImm32(JSValue::CellTag), plan.gpr());
587         break;
588     case SetBooleanTag:
589         m_jit.move(TrustedImm32(JSValue::BooleanTag), plan.gpr());
590         break;
591     case SetDoubleConstant:
592         m_jit.loadDouble(TrustedImmPtr(m_jit.addressOfDoubleConstant(plan.node())), plan.fpr());
593         break;
594 #endif
595     case Load32Tag:
596         m_jit.load32(JITCompiler::tagFor(plan.node()->virtualRegister()), plan.gpr());
597         break;
598     case Load32Payload:
599         m_jit.load32(JITCompiler::payloadFor(plan.node()->virtualRegister()), plan.gpr());
600         break;
601     case LoadPtr:
602         m_jit.loadPtr(JITCompiler::addressFor(plan.node()->virtualRegister()), plan.gpr());
603         break;
604 #if USE(JSVALUE64)
605     case Load64:
606         m_jit.load64(JITCompiler::addressFor(plan.node()->virtualRegister()), plan.gpr());
607         break;
608     case Load64ShiftInt52Right:
609         m_jit.load64(JITCompiler::addressFor(plan.node()->virtualRegister()), plan.gpr());
610         m_jit.rshift64(TrustedImm32(JSValue::int52ShiftAmount), plan.gpr());
611         break;
612     case Load64ShiftInt52Left:
613         m_jit.load64(JITCompiler::addressFor(plan.node()->virtualRegister()), plan.gpr());
614         m_jit.lshift64(TrustedImm32(JSValue::int52ShiftAmount), plan.gpr());
615         break;
616 #endif
617     case LoadDouble:
618         m_jit.loadDouble(JITCompiler::addressFor(plan.node()->virtualRegister()), plan.fpr());
619         break;
620     default:
621         RELEASE_ASSERT_NOT_REACHED();
622     }
623 }
624     
625 JITCompiler::Jump SpeculativeJIT::jumpSlowForUnwantedArrayMode(GPRReg tempGPR, ArrayMode arrayMode, IndexingType shape)
626 {
627     switch (arrayMode.arrayClass()) {
628     case Array::OriginalArray: {
629         CRASH();
630 #if COMPILER_QUIRK(CONSIDERS_UNREACHABLE_CODE)
631         JITCompiler::Jump result; // I already know that VC++ takes unkindly to the expression "return Jump()", so I'm doing it this way in anticipation of someone eventually using VC++ to compile the DFG.
632         return result;
633 #endif
634     }
635         
636     case Array::Array:
637         m_jit.and32(TrustedImm32(IsArray | IndexingShapeMask), tempGPR);
638         return m_jit.branch32(
639             MacroAssembler::NotEqual, tempGPR, TrustedImm32(IsArray | shape));
640         
641     case Array::NonArray:
642     case Array::OriginalNonArray:
643         m_jit.and32(TrustedImm32(IsArray | IndexingShapeMask), tempGPR);
644         return m_jit.branch32(
645             MacroAssembler::NotEqual, tempGPR, TrustedImm32(shape));
646         
647     case Array::PossiblyArray:
648         m_jit.and32(TrustedImm32(IndexingShapeMask), tempGPR);
649         return m_jit.branch32(MacroAssembler::NotEqual, tempGPR, TrustedImm32(shape));
650     }
651     
652     RELEASE_ASSERT_NOT_REACHED();
653     return JITCompiler::Jump();
654 }
655
656 JITCompiler::JumpList SpeculativeJIT::jumpSlowForUnwantedArrayMode(GPRReg tempGPR, ArrayMode arrayMode)
657 {
658     JITCompiler::JumpList result;
659     
660     switch (arrayMode.type()) {
661     case Array::Int32:
662         return jumpSlowForUnwantedArrayMode(tempGPR, arrayMode, Int32Shape);
663
664     case Array::Double:
665         return jumpSlowForUnwantedArrayMode(tempGPR, arrayMode, DoubleShape);
666
667     case Array::Contiguous:
668         return jumpSlowForUnwantedArrayMode(tempGPR, arrayMode, ContiguousShape);
669
670     case Array::ArrayStorage:
671     case Array::SlowPutArrayStorage: {
672         ASSERT(!arrayMode.isJSArrayWithOriginalStructure());
673         
674         if (arrayMode.isJSArray()) {
675             if (arrayMode.isSlowPut()) {
676                 result.append(
677                     m_jit.branchTest32(
678                         MacroAssembler::Zero, tempGPR, MacroAssembler::TrustedImm32(IsArray)));
679                 m_jit.and32(TrustedImm32(IndexingShapeMask), tempGPR);
680                 m_jit.sub32(TrustedImm32(ArrayStorageShape), tempGPR);
681                 result.append(
682                     m_jit.branch32(
683                         MacroAssembler::Above, tempGPR,
684                         TrustedImm32(SlowPutArrayStorageShape - ArrayStorageShape)));
685                 break;
686             }
687             m_jit.and32(TrustedImm32(IsArray | IndexingShapeMask), tempGPR);
688             result.append(
689                 m_jit.branch32(MacroAssembler::NotEqual, tempGPR, TrustedImm32(IsArray | ArrayStorageShape)));
690             break;
691         }
692         m_jit.and32(TrustedImm32(IndexingShapeMask), tempGPR);
693         if (arrayMode.isSlowPut()) {
694             m_jit.sub32(TrustedImm32(ArrayStorageShape), tempGPR);
695             result.append(
696                 m_jit.branch32(
697                     MacroAssembler::Above, tempGPR,
698                     TrustedImm32(SlowPutArrayStorageShape - ArrayStorageShape)));
699             break;
700         }
701         result.append(
702             m_jit.branch32(MacroAssembler::NotEqual, tempGPR, TrustedImm32(ArrayStorageShape)));
703         break;
704     }
705     default:
706         CRASH();
707         break;
708     }
709     
710     return result;
711 }
712
713 void SpeculativeJIT::checkArray(Node* node)
714 {
715     ASSERT(node->arrayMode().isSpecific());
716     ASSERT(!node->arrayMode().doesConversion());
717     
718     SpeculateCellOperand base(this, node->child1());
719     GPRReg baseReg = base.gpr();
720     
721     if (node->arrayMode().alreadyChecked(m_jit.graph(), node, m_state.forNode(node->child1()))) {
722         noResult(m_currentNode);
723         return;
724     }
725     
726     const ClassInfo* expectedClassInfo = 0;
727     
728     switch (node->arrayMode().type()) {
729     case Array::String:
730         RELEASE_ASSERT_NOT_REACHED(); // Should have been a Phantom(String:)
731         break;
732     case Array::Int32:
733     case Array::Double:
734     case Array::Contiguous:
735     case Array::ArrayStorage:
736     case Array::SlowPutArrayStorage: {
737         GPRTemporary temp(this);
738         GPRReg tempGPR = temp.gpr();
739         m_jit.load8(MacroAssembler::Address(baseReg, JSCell::indexingTypeOffset()), tempGPR);
740         speculationCheck(
741             BadIndexingType, JSValueSource::unboxedCell(baseReg), 0,
742             jumpSlowForUnwantedArrayMode(tempGPR, node->arrayMode()));
743         
744         noResult(m_currentNode);
745         return;
746     }
747     case Array::DirectArguments:
748         speculateCellTypeWithoutTypeFiltering(node->child1(), baseReg, DirectArgumentsType);
749         noResult(m_currentNode);
750         return;
751     case Array::ScopedArguments:
752         speculateCellTypeWithoutTypeFiltering(node->child1(), baseReg, ScopedArgumentsType);
753         noResult(m_currentNode);
754         return;
755     default:
756         speculateCellTypeWithoutTypeFiltering(
757             node->child1(), baseReg,
758             typeForTypedArrayType(node->arrayMode().typedArrayType()));
759         noResult(m_currentNode);
760         return;
761     }
762     
763     RELEASE_ASSERT(expectedClassInfo);
764     
765     GPRTemporary temp(this);
766     GPRTemporary temp2(this);
767     m_jit.emitLoadStructure(baseReg, temp.gpr(), temp2.gpr());
768     speculationCheck(
769         BadType, JSValueSource::unboxedCell(baseReg), node,
770         m_jit.branchPtr(
771             MacroAssembler::NotEqual,
772             MacroAssembler::Address(temp.gpr(), Structure::classInfoOffset()),
773             MacroAssembler::TrustedImmPtr(expectedClassInfo)));
774     
775     noResult(m_currentNode);
776 }
777
778 void SpeculativeJIT::arrayify(Node* node, GPRReg baseReg, GPRReg propertyReg)
779 {
780     ASSERT(node->arrayMode().doesConversion());
781     
782     GPRTemporary temp(this);
783     GPRTemporary structure;
784     GPRReg tempGPR = temp.gpr();
785     GPRReg structureGPR = InvalidGPRReg;
786     
787     if (node->op() != ArrayifyToStructure) {
788         GPRTemporary realStructure(this);
789         structure.adopt(realStructure);
790         structureGPR = structure.gpr();
791     }
792         
793     // We can skip all that comes next if we already have array storage.
794     MacroAssembler::JumpList slowPath;
795     
796     if (node->op() == ArrayifyToStructure) {
797         slowPath.append(m_jit.branchWeakStructure(
798             JITCompiler::NotEqual,
799             JITCompiler::Address(baseReg, JSCell::structureIDOffset()),
800             node->structure()));
801     } else {
802         m_jit.load8(
803             MacroAssembler::Address(baseReg, JSCell::indexingTypeOffset()), tempGPR);
804         
805         slowPath.append(jumpSlowForUnwantedArrayMode(tempGPR, node->arrayMode()));
806     }
807     
808     addSlowPathGenerator(std::make_unique<ArrayifySlowPathGenerator>(
809         slowPath, this, node, baseReg, propertyReg, tempGPR, structureGPR));
810     
811     noResult(m_currentNode);
812 }
813
814 void SpeculativeJIT::arrayify(Node* node)
815 {
816     ASSERT(node->arrayMode().isSpecific());
817     
818     SpeculateCellOperand base(this, node->child1());
819     
820     if (!node->child2()) {
821         arrayify(node, base.gpr(), InvalidGPRReg);
822         return;
823     }
824     
825     SpeculateInt32Operand property(this, node->child2());
826     
827     arrayify(node, base.gpr(), property.gpr());
828 }
829
830 GPRReg SpeculativeJIT::fillStorage(Edge edge)
831 {
832     VirtualRegister virtualRegister = edge->virtualRegister();
833     GenerationInfo& info = generationInfoFromVirtualRegister(virtualRegister);
834     
835     switch (info.registerFormat()) {
836     case DataFormatNone: {
837         if (info.spillFormat() == DataFormatStorage) {
838             GPRReg gpr = allocate();
839             m_gprs.retain(gpr, virtualRegister, SpillOrderSpilled);
840             m_jit.loadPtr(JITCompiler::addressFor(virtualRegister), gpr);
841             info.fillStorage(*m_stream, gpr);
842             return gpr;
843         }
844         
845         // Must be a cell; fill it as a cell and then return the pointer.
846         return fillSpeculateCell(edge);
847     }
848         
849     case DataFormatStorage: {
850         GPRReg gpr = info.gpr();
851         m_gprs.lock(gpr);
852         return gpr;
853     }
854         
855     default:
856         return fillSpeculateCell(edge);
857     }
858 }
859
860 void SpeculativeJIT::useChildren(Node* node)
861 {
862     if (node->flags() & NodeHasVarArgs) {
863         for (unsigned childIdx = node->firstChild(); childIdx < node->firstChild() + node->numChildren(); childIdx++) {
864             if (!!m_jit.graph().m_varArgChildren[childIdx])
865                 use(m_jit.graph().m_varArgChildren[childIdx]);
866         }
867     } else {
868         Edge child1 = node->child1();
869         if (!child1) {
870             ASSERT(!node->child2() && !node->child3());
871             return;
872         }
873         use(child1);
874         
875         Edge child2 = node->child2();
876         if (!child2) {
877             ASSERT(!node->child3());
878             return;
879         }
880         use(child2);
881         
882         Edge child3 = node->child3();
883         if (!child3)
884             return;
885         use(child3);
886     }
887 }
888
889 void SpeculativeJIT::compileIn(Node* node)
890 {
891     SpeculateCellOperand base(this, node->child2());
892     GPRReg baseGPR = base.gpr();
893     
894     if (JSString* string = node->child1()->dynamicCastConstant<JSString*>()) {
895         if (string->tryGetValueImpl() && string->tryGetValueImpl()->isAtomic()) {
896             StructureStubInfo* stubInfo = m_jit.codeBlock()->addStubInfo();
897             
898             GPRTemporary result(this);
899             GPRReg resultGPR = result.gpr();
900
901             use(node->child1());
902             
903             MacroAssembler::PatchableJump jump = m_jit.patchableJump();
904             MacroAssembler::Label done = m_jit.label();
905             
906             auto slowPath = slowPathCall(
907                 jump.m_jump, this, operationInOptimize,
908                 JSValueRegs::payloadOnly(resultGPR), stubInfo, baseGPR,
909                 string->tryGetValueImpl());
910             
911             stubInfo->codeOrigin = node->origin.semantic;
912             stubInfo->patch.baseGPR = static_cast<int8_t>(baseGPR);
913             stubInfo->patch.valueGPR = static_cast<int8_t>(resultGPR);
914             stubInfo->patch.usedRegisters = usedRegisters();
915             stubInfo->patch.spillMode = NeedToSpill;
916
917             m_jit.addIn(InRecord(jump, done, slowPath.get(), stubInfo));
918             addSlowPathGenerator(WTF::move(slowPath));
919
920             base.use();
921
922             blessedBooleanResult(resultGPR, node, UseChildrenCalledExplicitly);
923             return;
924         }
925     }
926
927     JSValueOperand key(this, node->child1());
928     JSValueRegs regs = key.jsValueRegs();
929         
930     GPRFlushedCallResult result(this);
931     GPRReg resultGPR = result.gpr();
932         
933     base.use();
934     key.use();
935         
936     flushRegisters();
937     callOperation(
938         operationGenericIn, extractResult(JSValueRegs::payloadOnly(resultGPR)),
939         baseGPR, regs);
940     blessedBooleanResult(resultGPR, node, UseChildrenCalledExplicitly);
941 }
942
943 bool SpeculativeJIT::nonSpeculativeCompare(Node* node, MacroAssembler::RelationalCondition cond, S_JITOperation_EJJ helperFunction)
944 {
945     unsigned branchIndexInBlock = detectPeepHoleBranch();
946     if (branchIndexInBlock != UINT_MAX) {
947         Node* branchNode = m_block->at(branchIndexInBlock);
948
949         ASSERT(node->adjustedRefCount() == 1);
950         
951         nonSpeculativePeepholeBranch(node, branchNode, cond, helperFunction);
952     
953         m_indexInBlock = branchIndexInBlock;
954         m_currentNode = branchNode;
955         
956         return true;
957     }
958     
959     nonSpeculativeNonPeepholeCompare(node, cond, helperFunction);
960     
961     return false;
962 }
963
964 bool SpeculativeJIT::nonSpeculativeStrictEq(Node* node, bool invert)
965 {
966     unsigned branchIndexInBlock = detectPeepHoleBranch();
967     if (branchIndexInBlock != UINT_MAX) {
968         Node* branchNode = m_block->at(branchIndexInBlock);
969
970         ASSERT(node->adjustedRefCount() == 1);
971         
972         nonSpeculativePeepholeStrictEq(node, branchNode, invert);
973     
974         m_indexInBlock = branchIndexInBlock;
975         m_currentNode = branchNode;
976         
977         return true;
978     }
979     
980     nonSpeculativeNonPeepholeStrictEq(node, invert);
981     
982     return false;
983 }
984
985 static const char* dataFormatString(DataFormat format)
986 {
987     // These values correspond to the DataFormat enum.
988     const char* strings[] = {
989         "[  ]",
990         "[ i]",
991         "[ d]",
992         "[ c]",
993         "Err!",
994         "Err!",
995         "Err!",
996         "Err!",
997         "[J ]",
998         "[Ji]",
999         "[Jd]",
1000         "[Jc]",
1001         "Err!",
1002         "Err!",
1003         "Err!",
1004         "Err!",
1005     };
1006     return strings[format];
1007 }
1008
1009 void SpeculativeJIT::dump(const char* label)
1010 {
1011     if (label)
1012         dataLogF("<%s>\n", label);
1013
1014     dataLogF("  gprs:\n");
1015     m_gprs.dump();
1016     dataLogF("  fprs:\n");
1017     m_fprs.dump();
1018     dataLogF("  VirtualRegisters:\n");
1019     for (unsigned i = 0; i < m_generationInfo.size(); ++i) {
1020         GenerationInfo& info = m_generationInfo[i];
1021         if (info.alive())
1022             dataLogF("    % 3d:%s%s", i, dataFormatString(info.registerFormat()), dataFormatString(info.spillFormat()));
1023         else
1024             dataLogF("    % 3d:[__][__]", i);
1025         if (info.registerFormat() == DataFormatDouble)
1026             dataLogF(":fpr%d\n", info.fpr());
1027         else if (info.registerFormat() != DataFormatNone
1028 #if USE(JSVALUE32_64)
1029             && !(info.registerFormat() & DataFormatJS)
1030 #endif
1031             ) {
1032             ASSERT(info.gpr() != InvalidGPRReg);
1033             dataLogF(":%s\n", GPRInfo::debugName(info.gpr()));
1034         } else
1035             dataLogF("\n");
1036     }
1037     if (label)
1038         dataLogF("</%s>\n", label);
1039 }
1040
1041 GPRTemporary::GPRTemporary()
1042     : m_jit(0)
1043     , m_gpr(InvalidGPRReg)
1044 {
1045 }
1046
1047 GPRTemporary::GPRTemporary(SpeculativeJIT* jit)
1048     : m_jit(jit)
1049     , m_gpr(InvalidGPRReg)
1050 {
1051     m_gpr = m_jit->allocate();
1052 }
1053
1054 GPRTemporary::GPRTemporary(SpeculativeJIT* jit, GPRReg specific)
1055     : m_jit(jit)
1056     , m_gpr(InvalidGPRReg)
1057 {
1058     m_gpr = m_jit->allocate(specific);
1059 }
1060
1061 #if USE(JSVALUE32_64)
1062 GPRTemporary::GPRTemporary(
1063     SpeculativeJIT* jit, ReuseTag, JSValueOperand& op1, WhichValueWord which)
1064     : m_jit(jit)
1065     , m_gpr(InvalidGPRReg)
1066 {
1067     if (!op1.isDouble() && m_jit->canReuse(op1.node()))
1068         m_gpr = m_jit->reuse(op1.gpr(which));
1069     else
1070         m_gpr = m_jit->allocate();
1071 }
1072 #endif // USE(JSVALUE32_64)
1073
1074 JSValueRegsTemporary::JSValueRegsTemporary() { }
1075
1076 JSValueRegsTemporary::JSValueRegsTemporary(SpeculativeJIT* jit)
1077 #if USE(JSVALUE64)
1078     : m_gpr(jit)
1079 #else
1080     : m_payloadGPR(jit)
1081     , m_tagGPR(jit)
1082 #endif
1083 {
1084 }
1085
1086 JSValueRegsTemporary::~JSValueRegsTemporary() { }
1087
1088 JSValueRegs JSValueRegsTemporary::regs()
1089 {
1090 #if USE(JSVALUE64)
1091     return JSValueRegs(m_gpr.gpr());
1092 #else
1093     return JSValueRegs(m_tagGPR.gpr(), m_payloadGPR.gpr());
1094 #endif
1095 }
1096
1097 void GPRTemporary::adopt(GPRTemporary& other)
1098 {
1099     ASSERT(!m_jit);
1100     ASSERT(m_gpr == InvalidGPRReg);
1101     ASSERT(other.m_jit);
1102     ASSERT(other.m_gpr != InvalidGPRReg);
1103     m_jit = other.m_jit;
1104     m_gpr = other.m_gpr;
1105     other.m_jit = 0;
1106     other.m_gpr = InvalidGPRReg;
1107 }
1108
1109 FPRTemporary::FPRTemporary(SpeculativeJIT* jit)
1110     : m_jit(jit)
1111     , m_fpr(InvalidFPRReg)
1112 {
1113     m_fpr = m_jit->fprAllocate();
1114 }
1115
1116 FPRTemporary::FPRTemporary(SpeculativeJIT* jit, SpeculateDoubleOperand& op1)
1117     : m_jit(jit)
1118     , m_fpr(InvalidFPRReg)
1119 {
1120     if (m_jit->canReuse(op1.node()))
1121         m_fpr = m_jit->reuse(op1.fpr());
1122     else
1123         m_fpr = m_jit->fprAllocate();
1124 }
1125
1126 FPRTemporary::FPRTemporary(SpeculativeJIT* jit, SpeculateDoubleOperand& op1, SpeculateDoubleOperand& op2)
1127     : m_jit(jit)
1128     , m_fpr(InvalidFPRReg)
1129 {
1130     if (m_jit->canReuse(op1.node()))
1131         m_fpr = m_jit->reuse(op1.fpr());
1132     else if (m_jit->canReuse(op2.node()))
1133         m_fpr = m_jit->reuse(op2.fpr());
1134     else
1135         m_fpr = m_jit->fprAllocate();
1136 }
1137
1138 #if USE(JSVALUE32_64)
1139 FPRTemporary::FPRTemporary(SpeculativeJIT* jit, JSValueOperand& op1)
1140     : m_jit(jit)
1141     , m_fpr(InvalidFPRReg)
1142 {
1143     if (op1.isDouble() && m_jit->canReuse(op1.node()))
1144         m_fpr = m_jit->reuse(op1.fpr());
1145     else
1146         m_fpr = m_jit->fprAllocate();
1147 }
1148 #endif
1149
1150 void SpeculativeJIT::compilePeepHoleDoubleBranch(Node* node, Node* branchNode, JITCompiler::DoubleCondition condition)
1151 {
1152     BasicBlock* taken = branchNode->branchData()->taken.block;
1153     BasicBlock* notTaken = branchNode->branchData()->notTaken.block;
1154     
1155     SpeculateDoubleOperand op1(this, node->child1());
1156     SpeculateDoubleOperand op2(this, node->child2());
1157     
1158     branchDouble(condition, op1.fpr(), op2.fpr(), taken);
1159     jump(notTaken);
1160 }
1161
1162 void SpeculativeJIT::compilePeepHoleObjectEquality(Node* node, Node* branchNode)
1163 {
1164     BasicBlock* taken = branchNode->branchData()->taken.block;
1165     BasicBlock* notTaken = branchNode->branchData()->notTaken.block;
1166
1167     MacroAssembler::RelationalCondition condition = MacroAssembler::Equal;
1168     
1169     if (taken == nextBlock()) {
1170         condition = MacroAssembler::NotEqual;
1171         BasicBlock* tmp = taken;
1172         taken = notTaken;
1173         notTaken = tmp;
1174     }
1175
1176     SpeculateCellOperand op1(this, node->child1());
1177     SpeculateCellOperand op2(this, node->child2());
1178     
1179     GPRReg op1GPR = op1.gpr();
1180     GPRReg op2GPR = op2.gpr();
1181     
1182     if (masqueradesAsUndefinedWatchpointIsStillValid()) {
1183         if (m_state.forNode(node->child1()).m_type & ~SpecObject) {
1184             speculationCheck(
1185                 BadType, JSValueSource::unboxedCell(op1GPR), node->child1(), branchNotObject(op1GPR));
1186         }
1187         if (m_state.forNode(node->child2()).m_type & ~SpecObject) {
1188             speculationCheck(
1189                 BadType, JSValueSource::unboxedCell(op2GPR), node->child2(), branchNotObject(op2GPR));
1190         }
1191     } else {
1192         if (m_state.forNode(node->child1()).m_type & ~SpecObject) {
1193             speculationCheck(
1194                 BadType, JSValueSource::unboxedCell(op1GPR), node->child1(),
1195                 branchNotObject(op1GPR));
1196         }
1197         speculationCheck(BadType, JSValueSource::unboxedCell(op1GPR), node->child1(),
1198             m_jit.branchTest8(
1199                 MacroAssembler::NonZero, 
1200                 MacroAssembler::Address(op1GPR, JSCell::typeInfoFlagsOffset()), 
1201                 MacroAssembler::TrustedImm32(MasqueradesAsUndefined)));
1202
1203         if (m_state.forNode(node->child2()).m_type & ~SpecObject) {
1204             speculationCheck(
1205                 BadType, JSValueSource::unboxedCell(op2GPR), node->child2(),
1206                 branchNotObject(op2GPR));
1207         }
1208         speculationCheck(BadType, JSValueSource::unboxedCell(op2GPR), node->child2(),
1209             m_jit.branchTest8(
1210                 MacroAssembler::NonZero, 
1211                 MacroAssembler::Address(op2GPR, JSCell::typeInfoFlagsOffset()), 
1212                 MacroAssembler::TrustedImm32(MasqueradesAsUndefined)));
1213     }
1214
1215     branchPtr(condition, op1GPR, op2GPR, taken);
1216     jump(notTaken);
1217 }
1218
1219 void SpeculativeJIT::compilePeepHoleBooleanBranch(Node* node, Node* branchNode, JITCompiler::RelationalCondition condition)
1220 {
1221     BasicBlock* taken = branchNode->branchData()->taken.block;
1222     BasicBlock* notTaken = branchNode->branchData()->notTaken.block;
1223
1224     // The branch instruction will branch to the taken block.
1225     // If taken is next, switch taken with notTaken & invert the branch condition so we can fall through.
1226     if (taken == nextBlock()) {
1227         condition = JITCompiler::invert(condition);
1228         BasicBlock* tmp = taken;
1229         taken = notTaken;
1230         notTaken = tmp;
1231     }
1232
1233     if (node->child1()->isBooleanConstant()) {
1234         bool imm = node->child1()->asBoolean();
1235         SpeculateBooleanOperand op2(this, node->child2());
1236         branch32(condition, JITCompiler::Imm32(static_cast<int32_t>(JSValue::encode(jsBoolean(imm)))), op2.gpr(), taken);
1237     } else if (node->child2()->isBooleanConstant()) {
1238         SpeculateBooleanOperand op1(this, node->child1());
1239         bool imm = node->child2()->asBoolean();
1240         branch32(condition, op1.gpr(), JITCompiler::Imm32(static_cast<int32_t>(JSValue::encode(jsBoolean(imm)))), taken);
1241     } else {
1242         SpeculateBooleanOperand op1(this, node->child1());
1243         SpeculateBooleanOperand op2(this, node->child2());
1244         branch32(condition, op1.gpr(), op2.gpr(), taken);
1245     }
1246
1247     jump(notTaken);
1248 }
1249
1250 void SpeculativeJIT::compilePeepHoleInt32Branch(Node* node, Node* branchNode, JITCompiler::RelationalCondition condition)
1251 {
1252     BasicBlock* taken = branchNode->branchData()->taken.block;
1253     BasicBlock* notTaken = branchNode->branchData()->notTaken.block;
1254
1255     // The branch instruction will branch to the taken block.
1256     // If taken is next, switch taken with notTaken & invert the branch condition so we can fall through.
1257     if (taken == nextBlock()) {
1258         condition = JITCompiler::invert(condition);
1259         BasicBlock* tmp = taken;
1260         taken = notTaken;
1261         notTaken = tmp;
1262     }
1263
1264     if (node->child1()->isInt32Constant()) {
1265         int32_t imm = node->child1()->asInt32();
1266         SpeculateInt32Operand op2(this, node->child2());
1267         branch32(condition, JITCompiler::Imm32(imm), op2.gpr(), taken);
1268     } else if (node->child2()->isInt32Constant()) {
1269         SpeculateInt32Operand op1(this, node->child1());
1270         int32_t imm = node->child2()->asInt32();
1271         branch32(condition, op1.gpr(), JITCompiler::Imm32(imm), taken);
1272     } else {
1273         SpeculateInt32Operand op1(this, node->child1());
1274         SpeculateInt32Operand op2(this, node->child2());
1275         branch32(condition, op1.gpr(), op2.gpr(), taken);
1276     }
1277
1278     jump(notTaken);
1279 }
1280
1281 // Returns true if the compare is fused with a subsequent branch.
1282 bool SpeculativeJIT::compilePeepHoleBranch(Node* node, MacroAssembler::RelationalCondition condition, MacroAssembler::DoubleCondition doubleCondition, S_JITOperation_EJJ operation)
1283 {
1284     // Fused compare & branch.
1285     unsigned branchIndexInBlock = detectPeepHoleBranch();
1286     if (branchIndexInBlock != UINT_MAX) {
1287         Node* branchNode = m_block->at(branchIndexInBlock);
1288
1289         // detectPeepHoleBranch currently only permits the branch to be the very next node,
1290         // so can be no intervening nodes to also reference the compare. 
1291         ASSERT(node->adjustedRefCount() == 1);
1292
1293         if (node->isBinaryUseKind(Int32Use))
1294             compilePeepHoleInt32Branch(node, branchNode, condition);
1295 #if USE(JSVALUE64)
1296         else if (node->isBinaryUseKind(Int52RepUse))
1297             compilePeepHoleInt52Branch(node, branchNode, condition);
1298 #endif // USE(JSVALUE64)
1299         else if (node->isBinaryUseKind(DoubleRepUse))
1300             compilePeepHoleDoubleBranch(node, branchNode, doubleCondition);
1301         else if (node->op() == CompareEq) {
1302             if (node->isBinaryUseKind(StringUse) || node->isBinaryUseKind(StringIdentUse)) {
1303                 // Use non-peephole comparison, for now.
1304                 return false;
1305             }
1306             if (node->isBinaryUseKind(BooleanUse))
1307                 compilePeepHoleBooleanBranch(node, branchNode, condition);
1308             else if (node->isBinaryUseKind(ObjectUse))
1309                 compilePeepHoleObjectEquality(node, branchNode);
1310             else if (node->isBinaryUseKind(ObjectUse, ObjectOrOtherUse))
1311                 compilePeepHoleObjectToObjectOrOtherEquality(node->child1(), node->child2(), branchNode);
1312             else if (node->isBinaryUseKind(ObjectOrOtherUse, ObjectUse))
1313                 compilePeepHoleObjectToObjectOrOtherEquality(node->child2(), node->child1(), branchNode);
1314             else {
1315                 nonSpeculativePeepholeBranch(node, branchNode, condition, operation);
1316                 return true;
1317             }
1318         } else {
1319             nonSpeculativePeepholeBranch(node, branchNode, condition, operation);
1320             return true;
1321         }
1322
1323         use(node->child1());
1324         use(node->child2());
1325         m_indexInBlock = branchIndexInBlock;
1326         m_currentNode = branchNode;
1327         return true;
1328     }
1329     return false;
1330 }
1331
1332 void SpeculativeJIT::noticeOSRBirth(Node* node)
1333 {
1334     if (!node->hasVirtualRegister())
1335         return;
1336     
1337     VirtualRegister virtualRegister = node->virtualRegister();
1338     GenerationInfo& info = generationInfoFromVirtualRegister(virtualRegister);
1339     
1340     info.noticeOSRBirth(*m_stream, node, virtualRegister);
1341 }
1342
1343 void SpeculativeJIT::compileMovHint(Node* node)
1344 {
1345     ASSERT(node->containsMovHint() && node->op() != ZombieHint);
1346     
1347     Node* child = node->child1().node();
1348     noticeOSRBirth(child);
1349     
1350     m_stream->appendAndLog(VariableEvent::movHint(MinifiedID(child), node->unlinkedLocal()));
1351 }
1352
1353 void SpeculativeJIT::bail(AbortReason reason)
1354 {
1355     if (verboseCompilationEnabled())
1356         dataLog("Bailing compilation.\n");
1357     m_compileOkay = true;
1358     m_jit.abortWithReason(reason, m_lastGeneratedNode);
1359     clearGenerationInfo();
1360 }
1361
1362 void SpeculativeJIT::compileCurrentBlock()
1363 {
1364     ASSERT(m_compileOkay);
1365     
1366     if (!m_block)
1367         return;
1368     
1369     ASSERT(m_block->isReachable);
1370     
1371     m_jit.blockHeads()[m_block->index] = m_jit.label();
1372
1373     if (!m_block->intersectionOfCFAHasVisited) {
1374         // Don't generate code for basic blocks that are unreachable according to CFA.
1375         // But to be sure that nobody has generated a jump to this block, drop in a
1376         // breakpoint here.
1377         m_jit.abortWithReason(DFGUnreachableBasicBlock);
1378         return;
1379     }
1380
1381     m_stream->appendAndLog(VariableEvent::reset());
1382     
1383     m_jit.jitAssertHasValidCallFrame();
1384     m_jit.jitAssertTagsInPlace();
1385     m_jit.jitAssertArgumentCountSane();
1386
1387     m_state.reset();
1388     m_state.beginBasicBlock(m_block);
1389     
1390     for (size_t i = m_block->variablesAtHead.size(); i--;) {
1391         int operand = m_block->variablesAtHead.operandForIndex(i);
1392         Node* node = m_block->variablesAtHead[i];
1393         if (!node)
1394             continue; // No need to record dead SetLocal's.
1395         
1396         VariableAccessData* variable = node->variableAccessData();
1397         DataFormat format;
1398         if (!node->refCount())
1399             continue; // No need to record dead SetLocal's.
1400         format = dataFormatFor(variable->flushFormat());
1401         m_stream->appendAndLog(
1402             VariableEvent::setLocal(
1403                 VirtualRegister(operand),
1404                 variable->machineLocal(),
1405                 format));
1406     }
1407     
1408     m_codeOriginForExitTarget = CodeOrigin();
1409     m_codeOriginForExitProfile = CodeOrigin();
1410     
1411     for (m_indexInBlock = 0; m_indexInBlock < m_block->size(); ++m_indexInBlock) {
1412         m_currentNode = m_block->at(m_indexInBlock);
1413         
1414         // We may have hit a contradiction that the CFA was aware of but that the JIT
1415         // didn't cause directly.
1416         if (!m_state.isValid()) {
1417             bail(DFGBailedAtTopOfBlock);
1418             return;
1419         }
1420
1421         if (ASSERT_DISABLED)
1422             m_canExit = true; // Essentially disable the assertions.
1423         else
1424             m_canExit = mayExit(m_jit.graph(), m_currentNode);
1425         
1426         m_interpreter.startExecuting();
1427         m_jit.setForNode(m_currentNode);
1428         m_codeOriginForExitTarget = m_currentNode->origin.forExit;
1429         m_codeOriginForExitProfile = m_currentNode->origin.semantic;
1430         m_lastGeneratedNode = m_currentNode->op();
1431         if (!m_currentNode->shouldGenerate()) {
1432             if (belongsInMinifiedGraph(m_currentNode->op()))
1433                 m_minifiedGraph->append(MinifiedNode::fromNode(m_currentNode));
1434         } else {
1435             if (verboseCompilationEnabled()) {
1436                 dataLogF(
1437                     "SpeculativeJIT generating Node @%d (bc#%u) at JIT offset 0x%x",
1438                     (int)m_currentNode->index(),
1439                     m_currentNode->origin.semantic.bytecodeIndex, m_jit.debugOffset());
1440                 dataLog("\n");
1441             }
1442             
1443             compile(m_currentNode);
1444
1445 #if ENABLE(DFG_REGISTER_ALLOCATION_VALIDATION)
1446             m_jit.clearRegisterAllocationOffsets();
1447 #endif
1448
1449             if (!m_compileOkay) {
1450                 bail(DFGBailedAtEndOfNode);
1451                 return;
1452             }
1453             
1454             if (belongsInMinifiedGraph(m_currentNode->op())) {
1455                 m_minifiedGraph->append(MinifiedNode::fromNode(m_currentNode));
1456                 noticeOSRBirth(m_currentNode);
1457             }
1458         }
1459         
1460         // Make sure that the abstract state is rematerialized for the next node.
1461         m_interpreter.executeEffects(m_indexInBlock);
1462     }
1463     
1464     // Perform the most basic verification that children have been used correctly.
1465     if (!ASSERT_DISABLED) {
1466         for (unsigned index = 0; index < m_generationInfo.size(); ++index) {
1467             GenerationInfo& info = m_generationInfo[index];
1468             RELEASE_ASSERT(!info.alive());
1469         }
1470     }
1471 }
1472
1473 // If we are making type predictions about our arguments then
1474 // we need to check that they are correct on function entry.
1475 void SpeculativeJIT::checkArgumentTypes()
1476 {
1477     ASSERT(!m_currentNode);
1478     m_isCheckingArgumentTypes = true;
1479     m_codeOriginForExitTarget = CodeOrigin(0);
1480     m_codeOriginForExitProfile = CodeOrigin(0);
1481
1482     for (int i = 0; i < m_jit.codeBlock()->numParameters(); ++i) {
1483         Node* node = m_jit.graph().m_arguments[i];
1484         if (!node) {
1485             // The argument is dead. We don't do any checks for such arguments.
1486             continue;
1487         }
1488         
1489         ASSERT(node->op() == SetArgument);
1490         ASSERT(node->shouldGenerate());
1491
1492         VariableAccessData* variableAccessData = node->variableAccessData();
1493         FlushFormat format = variableAccessData->flushFormat();
1494         
1495         if (format == FlushedJSValue)
1496             continue;
1497         
1498         VirtualRegister virtualRegister = variableAccessData->local();
1499
1500         JSValueSource valueSource = JSValueSource(JITCompiler::addressFor(virtualRegister));
1501         
1502 #if USE(JSVALUE64)
1503         switch (format) {
1504         case FlushedInt32: {
1505             speculationCheck(BadType, valueSource, node, m_jit.branch64(MacroAssembler::Below, JITCompiler::addressFor(virtualRegister), GPRInfo::tagTypeNumberRegister));
1506             break;
1507         }
1508         case FlushedBoolean: {
1509             GPRTemporary temp(this);
1510             m_jit.load64(JITCompiler::addressFor(virtualRegister), temp.gpr());
1511             m_jit.xor64(TrustedImm32(static_cast<int32_t>(ValueFalse)), temp.gpr());
1512             speculationCheck(BadType, valueSource, node, m_jit.branchTest64(MacroAssembler::NonZero, temp.gpr(), TrustedImm32(static_cast<int32_t>(~1))));
1513             break;
1514         }
1515         case FlushedCell: {
1516             speculationCheck(BadType, valueSource, node, m_jit.branchTest64(MacroAssembler::NonZero, JITCompiler::addressFor(virtualRegister), GPRInfo::tagMaskRegister));
1517             break;
1518         }
1519         default:
1520             RELEASE_ASSERT_NOT_REACHED();
1521             break;
1522         }
1523 #else
1524         switch (format) {
1525         case FlushedInt32: {
1526             speculationCheck(BadType, valueSource, node, m_jit.branch32(MacroAssembler::NotEqual, JITCompiler::tagFor(virtualRegister), TrustedImm32(JSValue::Int32Tag)));
1527             break;
1528         }
1529         case FlushedBoolean: {
1530             speculationCheck(BadType, valueSource, node, m_jit.branch32(MacroAssembler::NotEqual, JITCompiler::tagFor(virtualRegister), TrustedImm32(JSValue::BooleanTag)));
1531             break;
1532         }
1533         case FlushedCell: {
1534             speculationCheck(BadType, valueSource, node, m_jit.branch32(MacroAssembler::NotEqual, JITCompiler::tagFor(virtualRegister), TrustedImm32(JSValue::CellTag)));
1535             break;
1536         }
1537         default:
1538             RELEASE_ASSERT_NOT_REACHED();
1539             break;
1540         }
1541 #endif
1542     }
1543     m_isCheckingArgumentTypes = false;
1544 }
1545
1546 bool SpeculativeJIT::compile()
1547 {
1548     checkArgumentTypes();
1549     
1550     ASSERT(!m_currentNode);
1551     for (BlockIndex blockIndex = 0; blockIndex < m_jit.graph().numBlocks(); ++blockIndex) {
1552         m_jit.setForBlockIndex(blockIndex);
1553         m_block = m_jit.graph().block(blockIndex);
1554         compileCurrentBlock();
1555     }
1556     linkBranches();
1557     return true;
1558 }
1559
1560 void SpeculativeJIT::createOSREntries()
1561 {
1562     for (BlockIndex blockIndex = 0; blockIndex < m_jit.graph().numBlocks(); ++blockIndex) {
1563         BasicBlock* block = m_jit.graph().block(blockIndex);
1564         if (!block)
1565             continue;
1566         if (!block->isOSRTarget)
1567             continue;
1568         
1569         // Currently we don't have OSR entry trampolines. We could add them
1570         // here if need be.
1571         m_osrEntryHeads.append(m_jit.blockHeads()[blockIndex]);
1572     }
1573 }
1574
1575 void SpeculativeJIT::linkOSREntries(LinkBuffer& linkBuffer)
1576 {
1577     unsigned osrEntryIndex = 0;
1578     for (BlockIndex blockIndex = 0; blockIndex < m_jit.graph().numBlocks(); ++blockIndex) {
1579         BasicBlock* block = m_jit.graph().block(blockIndex);
1580         if (!block)
1581             continue;
1582         if (!block->isOSRTarget)
1583             continue;
1584         m_jit.noticeOSREntry(*block, m_osrEntryHeads[osrEntryIndex++], linkBuffer);
1585     }
1586     ASSERT(osrEntryIndex == m_osrEntryHeads.size());
1587 }
1588
1589 void SpeculativeJIT::compileDoublePutByVal(Node* node, SpeculateCellOperand& base, SpeculateStrictInt32Operand& property)
1590 {
1591     Edge child3 = m_jit.graph().varArgChild(node, 2);
1592     Edge child4 = m_jit.graph().varArgChild(node, 3);
1593
1594     ArrayMode arrayMode = node->arrayMode();
1595     
1596     GPRReg baseReg = base.gpr();
1597     GPRReg propertyReg = property.gpr();
1598     
1599     SpeculateDoubleOperand value(this, child3);
1600
1601     FPRReg valueReg = value.fpr();
1602     
1603     DFG_TYPE_CHECK(
1604         JSValueRegs(), child3, SpecFullRealNumber,
1605         m_jit.branchDouble(
1606             MacroAssembler::DoubleNotEqualOrUnordered, valueReg, valueReg));
1607     
1608     if (!m_compileOkay)
1609         return;
1610     
1611     StorageOperand storage(this, child4);
1612     GPRReg storageReg = storage.gpr();
1613
1614     if (node->op() == PutByValAlias) {
1615         // Store the value to the array.
1616         GPRReg propertyReg = property.gpr();
1617         FPRReg valueReg = value.fpr();
1618         m_jit.storeDouble(valueReg, MacroAssembler::BaseIndex(storageReg, propertyReg, MacroAssembler::TimesEight));
1619         
1620         noResult(m_currentNode);
1621         return;
1622     }
1623     
1624     GPRTemporary temporary;
1625     GPRReg temporaryReg = temporaryRegisterForPutByVal(temporary, node);
1626
1627     MacroAssembler::Jump slowCase;
1628     
1629     if (arrayMode.isInBounds()) {
1630         speculationCheck(
1631             OutOfBounds, JSValueRegs(), 0,
1632             m_jit.branch32(MacroAssembler::AboveOrEqual, propertyReg, MacroAssembler::Address(storageReg, Butterfly::offsetOfPublicLength())));
1633     } else {
1634         MacroAssembler::Jump inBounds = m_jit.branch32(MacroAssembler::Below, propertyReg, MacroAssembler::Address(storageReg, Butterfly::offsetOfPublicLength()));
1635         
1636         slowCase = m_jit.branch32(MacroAssembler::AboveOrEqual, propertyReg, MacroAssembler::Address(storageReg, Butterfly::offsetOfVectorLength()));
1637         
1638         if (!arrayMode.isOutOfBounds())
1639             speculationCheck(OutOfBounds, JSValueRegs(), 0, slowCase);
1640         
1641         m_jit.add32(TrustedImm32(1), propertyReg, temporaryReg);
1642         m_jit.store32(temporaryReg, MacroAssembler::Address(storageReg, Butterfly::offsetOfPublicLength()));
1643         
1644         inBounds.link(&m_jit);
1645     }
1646     
1647     m_jit.storeDouble(valueReg, MacroAssembler::BaseIndex(storageReg, propertyReg, MacroAssembler::TimesEight));
1648
1649     base.use();
1650     property.use();
1651     value.use();
1652     storage.use();
1653     
1654     if (arrayMode.isOutOfBounds()) {
1655         addSlowPathGenerator(
1656             slowPathCall(
1657                 slowCase, this,
1658                 m_jit.codeBlock()->isStrictMode() ? operationPutDoubleByValBeyondArrayBoundsStrict : operationPutDoubleByValBeyondArrayBoundsNonStrict,
1659                 NoResult, baseReg, propertyReg, valueReg));
1660     }
1661
1662     noResult(m_currentNode, UseChildrenCalledExplicitly);
1663 }
1664
1665 void SpeculativeJIT::compileGetCharCodeAt(Node* node)
1666 {
1667     SpeculateCellOperand string(this, node->child1());
1668     SpeculateStrictInt32Operand index(this, node->child2());
1669     StorageOperand storage(this, node->child3());
1670
1671     GPRReg stringReg = string.gpr();
1672     GPRReg indexReg = index.gpr();
1673     GPRReg storageReg = storage.gpr();
1674     
1675     ASSERT(speculationChecked(m_state.forNode(node->child1()).m_type, SpecString));
1676
1677     // unsigned comparison so we can filter out negative indices and indices that are too large
1678     speculationCheck(Uncountable, JSValueRegs(), 0, m_jit.branch32(MacroAssembler::AboveOrEqual, indexReg, MacroAssembler::Address(stringReg, JSString::offsetOfLength())));
1679
1680     GPRTemporary scratch(this);
1681     GPRReg scratchReg = scratch.gpr();
1682
1683     m_jit.loadPtr(MacroAssembler::Address(stringReg, JSString::offsetOfValue()), scratchReg);
1684
1685     // Load the character into scratchReg
1686     JITCompiler::Jump is16Bit = m_jit.branchTest32(MacroAssembler::Zero, MacroAssembler::Address(scratchReg, StringImpl::flagsOffset()), TrustedImm32(StringImpl::flagIs8Bit()));
1687
1688     m_jit.load8(MacroAssembler::BaseIndex(storageReg, indexReg, MacroAssembler::TimesOne, 0), scratchReg);
1689     JITCompiler::Jump cont8Bit = m_jit.jump();
1690
1691     is16Bit.link(&m_jit);
1692
1693     m_jit.load16(MacroAssembler::BaseIndex(storageReg, indexReg, MacroAssembler::TimesTwo, 0), scratchReg);
1694
1695     cont8Bit.link(&m_jit);
1696
1697     int32Result(scratchReg, m_currentNode);
1698 }
1699
1700 void SpeculativeJIT::compileGetByValOnString(Node* node)
1701 {
1702     SpeculateCellOperand base(this, node->child1());
1703     SpeculateStrictInt32Operand property(this, node->child2());
1704     StorageOperand storage(this, node->child3());
1705     GPRReg baseReg = base.gpr();
1706     GPRReg propertyReg = property.gpr();
1707     GPRReg storageReg = storage.gpr();
1708
1709     GPRTemporary scratch(this);
1710     GPRReg scratchReg = scratch.gpr();
1711 #if USE(JSVALUE32_64)
1712     GPRTemporary resultTag;
1713     GPRReg resultTagReg = InvalidGPRReg;
1714     if (node->arrayMode().isOutOfBounds()) {
1715         GPRTemporary realResultTag(this);
1716         resultTag.adopt(realResultTag);
1717         resultTagReg = resultTag.gpr();
1718     }
1719 #endif
1720
1721     ASSERT(ArrayMode(Array::String).alreadyChecked(m_jit.graph(), node, m_state.forNode(node->child1())));
1722
1723     // unsigned comparison so we can filter out negative indices and indices that are too large
1724     JITCompiler::Jump outOfBounds = m_jit.branch32(
1725         MacroAssembler::AboveOrEqual, propertyReg,
1726         MacroAssembler::Address(baseReg, JSString::offsetOfLength()));
1727     if (node->arrayMode().isInBounds())
1728         speculationCheck(OutOfBounds, JSValueRegs(), 0, outOfBounds);
1729
1730     m_jit.loadPtr(MacroAssembler::Address(baseReg, JSString::offsetOfValue()), scratchReg);
1731
1732     // Load the character into scratchReg
1733     JITCompiler::Jump is16Bit = m_jit.branchTest32(MacroAssembler::Zero, MacroAssembler::Address(scratchReg, StringImpl::flagsOffset()), TrustedImm32(StringImpl::flagIs8Bit()));
1734
1735     m_jit.load8(MacroAssembler::BaseIndex(storageReg, propertyReg, MacroAssembler::TimesOne, 0), scratchReg);
1736     JITCompiler::Jump cont8Bit = m_jit.jump();
1737
1738     is16Bit.link(&m_jit);
1739
1740     m_jit.load16(MacroAssembler::BaseIndex(storageReg, propertyReg, MacroAssembler::TimesTwo, 0), scratchReg);
1741
1742     JITCompiler::Jump bigCharacter =
1743         m_jit.branch32(MacroAssembler::AboveOrEqual, scratchReg, TrustedImm32(0x100));
1744
1745     // 8 bit string values don't need the isASCII check.
1746     cont8Bit.link(&m_jit);
1747
1748     m_jit.lshift32(MacroAssembler::TrustedImm32(sizeof(void*) == 4 ? 2 : 3), scratchReg);
1749     m_jit.addPtr(MacroAssembler::TrustedImmPtr(m_jit.vm()->smallStrings.singleCharacterStrings()), scratchReg);
1750     m_jit.loadPtr(scratchReg, scratchReg);
1751
1752     addSlowPathGenerator(
1753         slowPathCall(
1754             bigCharacter, this, operationSingleCharacterString, scratchReg, scratchReg));
1755
1756     if (node->arrayMode().isOutOfBounds()) {
1757 #if USE(JSVALUE32_64)
1758         m_jit.move(TrustedImm32(JSValue::CellTag), resultTagReg);
1759 #endif
1760
1761         JSGlobalObject* globalObject = m_jit.globalObjectFor(node->origin.semantic);
1762         if (globalObject->stringPrototypeChainIsSane()) {
1763 #if USE(JSVALUE64)
1764             addSlowPathGenerator(std::make_unique<SaneStringGetByValSlowPathGenerator>(
1765                 outOfBounds, this, JSValueRegs(scratchReg), baseReg, propertyReg));
1766 #else
1767             addSlowPathGenerator(std::make_unique<SaneStringGetByValSlowPathGenerator>(
1768                 outOfBounds, this, JSValueRegs(resultTagReg, scratchReg),
1769                 baseReg, propertyReg));
1770 #endif
1771         } else {
1772 #if USE(JSVALUE64)
1773             addSlowPathGenerator(
1774                 slowPathCall(
1775                     outOfBounds, this, operationGetByValStringInt,
1776                     scratchReg, baseReg, propertyReg));
1777 #else
1778             addSlowPathGenerator(
1779                 slowPathCall(
1780                     outOfBounds, this, operationGetByValStringInt,
1781                     resultTagReg, scratchReg, baseReg, propertyReg));
1782 #endif
1783         }
1784         
1785 #if USE(JSVALUE64)
1786         jsValueResult(scratchReg, m_currentNode);
1787 #else
1788         jsValueResult(resultTagReg, scratchReg, m_currentNode);
1789 #endif
1790     } else
1791         cellResult(scratchReg, m_currentNode);
1792 }
1793
1794 void SpeculativeJIT::compileFromCharCode(Node* node)
1795 {
1796     SpeculateStrictInt32Operand property(this, node->child1());
1797     GPRReg propertyReg = property.gpr();
1798     GPRTemporary smallStrings(this);
1799     GPRTemporary scratch(this);
1800     GPRReg scratchReg = scratch.gpr();
1801     GPRReg smallStringsReg = smallStrings.gpr();
1802
1803     JITCompiler::JumpList slowCases;
1804     slowCases.append(m_jit.branch32(MacroAssembler::AboveOrEqual, propertyReg, TrustedImm32(0xff)));
1805     m_jit.move(MacroAssembler::TrustedImmPtr(m_jit.vm()->smallStrings.singleCharacterStrings()), smallStringsReg);
1806     m_jit.loadPtr(MacroAssembler::BaseIndex(smallStringsReg, propertyReg, MacroAssembler::ScalePtr, 0), scratchReg);
1807
1808     slowCases.append(m_jit.branchTest32(MacroAssembler::Zero, scratchReg));
1809     addSlowPathGenerator(slowPathCall(slowCases, this, operationStringFromCharCode, scratchReg, propertyReg));
1810     cellResult(scratchReg, m_currentNode);
1811 }
1812
1813 GeneratedOperandType SpeculativeJIT::checkGeneratedTypeForToInt32(Node* node)
1814 {
1815     VirtualRegister virtualRegister = node->virtualRegister();
1816     GenerationInfo& info = generationInfoFromVirtualRegister(virtualRegister);
1817
1818     switch (info.registerFormat()) {
1819     case DataFormatStorage:
1820         RELEASE_ASSERT_NOT_REACHED();
1821
1822     case DataFormatBoolean:
1823     case DataFormatCell:
1824         terminateSpeculativeExecution(Uncountable, JSValueRegs(), 0);
1825         return GeneratedOperandTypeUnknown;
1826
1827     case DataFormatNone:
1828     case DataFormatJSCell:
1829     case DataFormatJS:
1830     case DataFormatJSBoolean:
1831     case DataFormatJSDouble:
1832         return GeneratedOperandJSValue;
1833
1834     case DataFormatJSInt32:
1835     case DataFormatInt32:
1836         return GeneratedOperandInteger;
1837
1838     default:
1839         RELEASE_ASSERT_NOT_REACHED();
1840         return GeneratedOperandTypeUnknown;
1841     }
1842 }
1843
1844 void SpeculativeJIT::compileValueToInt32(Node* node)
1845 {
1846     switch (node->child1().useKind()) {
1847 #if USE(JSVALUE64)
1848     case Int52RepUse: {
1849         SpeculateStrictInt52Operand op1(this, node->child1());
1850         GPRTemporary result(this, Reuse, op1);
1851         GPRReg op1GPR = op1.gpr();
1852         GPRReg resultGPR = result.gpr();
1853         m_jit.zeroExtend32ToPtr(op1GPR, resultGPR);
1854         int32Result(resultGPR, node, DataFormatInt32);
1855         return;
1856     }
1857 #endif // USE(JSVALUE64)
1858         
1859     case DoubleRepUse: {
1860         GPRTemporary result(this);
1861         SpeculateDoubleOperand op1(this, node->child1());
1862         FPRReg fpr = op1.fpr();
1863         GPRReg gpr = result.gpr();
1864         JITCompiler::Jump notTruncatedToInteger = m_jit.branchTruncateDoubleToInt32(fpr, gpr, JITCompiler::BranchIfTruncateFailed);
1865         
1866         addSlowPathGenerator(slowPathCall(notTruncatedToInteger, this, toInt32, gpr, fpr));
1867         
1868         int32Result(gpr, node);
1869         return;
1870     }
1871     
1872     case NumberUse:
1873     case NotCellUse: {
1874         switch (checkGeneratedTypeForToInt32(node->child1().node())) {
1875         case GeneratedOperandInteger: {
1876             SpeculateInt32Operand op1(this, node->child1(), ManualOperandSpeculation);
1877             GPRTemporary result(this, Reuse, op1);
1878             m_jit.move(op1.gpr(), result.gpr());
1879             int32Result(result.gpr(), node, op1.format());
1880             return;
1881         }
1882         case GeneratedOperandJSValue: {
1883             GPRTemporary result(this);
1884 #if USE(JSVALUE64)
1885             JSValueOperand op1(this, node->child1(), ManualOperandSpeculation);
1886
1887             GPRReg gpr = op1.gpr();
1888             GPRReg resultGpr = result.gpr();
1889             FPRTemporary tempFpr(this);
1890             FPRReg fpr = tempFpr.fpr();
1891
1892             JITCompiler::Jump isInteger = m_jit.branch64(MacroAssembler::AboveOrEqual, gpr, GPRInfo::tagTypeNumberRegister);
1893             JITCompiler::JumpList converted;
1894
1895             if (node->child1().useKind() == NumberUse) {
1896                 DFG_TYPE_CHECK(
1897                     JSValueRegs(gpr), node->child1(), SpecBytecodeNumber,
1898                     m_jit.branchTest64(
1899                         MacroAssembler::Zero, gpr, GPRInfo::tagTypeNumberRegister));
1900             } else {
1901                 JITCompiler::Jump isNumber = m_jit.branchTest64(MacroAssembler::NonZero, gpr, GPRInfo::tagTypeNumberRegister);
1902                 
1903                 DFG_TYPE_CHECK(
1904                     JSValueRegs(gpr), node->child1(), ~SpecCell, branchIsCell(JSValueRegs(gpr)));
1905                 
1906                 // It's not a cell: so true turns into 1 and all else turns into 0.
1907                 m_jit.compare64(JITCompiler::Equal, gpr, TrustedImm32(ValueTrue), resultGpr);
1908                 converted.append(m_jit.jump());
1909                 
1910                 isNumber.link(&m_jit);
1911             }
1912
1913             // First, if we get here we have a double encoded as a JSValue
1914             m_jit.move(gpr, resultGpr);
1915             unboxDouble(resultGpr, fpr);
1916
1917             silentSpillAllRegisters(resultGpr);
1918             callOperation(toInt32, resultGpr, fpr);
1919             silentFillAllRegisters(resultGpr);
1920
1921             converted.append(m_jit.jump());
1922
1923             isInteger.link(&m_jit);
1924             m_jit.zeroExtend32ToPtr(gpr, resultGpr);
1925
1926             converted.link(&m_jit);
1927 #else
1928             Node* childNode = node->child1().node();
1929             VirtualRegister virtualRegister = childNode->virtualRegister();
1930             GenerationInfo& info = generationInfoFromVirtualRegister(virtualRegister);
1931
1932             JSValueOperand op1(this, node->child1(), ManualOperandSpeculation);
1933
1934             GPRReg payloadGPR = op1.payloadGPR();
1935             GPRReg resultGpr = result.gpr();
1936         
1937             JITCompiler::JumpList converted;
1938
1939             if (info.registerFormat() == DataFormatJSInt32)
1940                 m_jit.move(payloadGPR, resultGpr);
1941             else {
1942                 GPRReg tagGPR = op1.tagGPR();
1943                 FPRTemporary tempFpr(this);
1944                 FPRReg fpr = tempFpr.fpr();
1945                 FPRTemporary scratch(this);
1946
1947                 JITCompiler::Jump isInteger = m_jit.branch32(MacroAssembler::Equal, tagGPR, TrustedImm32(JSValue::Int32Tag));
1948
1949                 if (node->child1().useKind() == NumberUse) {
1950                     DFG_TYPE_CHECK(
1951                         op1.jsValueRegs(), node->child1(), SpecBytecodeNumber,
1952                         m_jit.branch32(
1953                             MacroAssembler::AboveOrEqual, tagGPR,
1954                             TrustedImm32(JSValue::LowestTag)));
1955                 } else {
1956                     JITCompiler::Jump isNumber = m_jit.branch32(MacroAssembler::Below, tagGPR, TrustedImm32(JSValue::LowestTag));
1957                     
1958                     DFG_TYPE_CHECK(
1959                         op1.jsValueRegs(), node->child1(), ~SpecCell,
1960                         branchIsCell(op1.jsValueRegs()));
1961                     
1962                     // It's not a cell: so true turns into 1 and all else turns into 0.
1963                     JITCompiler::Jump isBoolean = m_jit.branch32(JITCompiler::Equal, tagGPR, TrustedImm32(JSValue::BooleanTag));
1964                     m_jit.move(TrustedImm32(0), resultGpr);
1965                     converted.append(m_jit.jump());
1966                     
1967                     isBoolean.link(&m_jit);
1968                     m_jit.move(payloadGPR, resultGpr);
1969                     converted.append(m_jit.jump());
1970                     
1971                     isNumber.link(&m_jit);
1972                 }
1973
1974                 unboxDouble(tagGPR, payloadGPR, fpr, scratch.fpr());
1975
1976                 silentSpillAllRegisters(resultGpr);
1977                 callOperation(toInt32, resultGpr, fpr);
1978                 silentFillAllRegisters(resultGpr);
1979
1980                 converted.append(m_jit.jump());
1981
1982                 isInteger.link(&m_jit);
1983                 m_jit.move(payloadGPR, resultGpr);
1984
1985                 converted.link(&m_jit);
1986             }
1987 #endif
1988             int32Result(resultGpr, node);
1989             return;
1990         }
1991         case GeneratedOperandTypeUnknown:
1992             RELEASE_ASSERT(!m_compileOkay);
1993             return;
1994         }
1995         RELEASE_ASSERT_NOT_REACHED();
1996         return;
1997     }
1998     
1999     default:
2000         ASSERT(!m_compileOkay);
2001         return;
2002     }
2003 }
2004
2005 void SpeculativeJIT::compileUInt32ToNumber(Node* node)
2006 {
2007     if (doesOverflow(node->arithMode())) {
2008         // We know that this sometimes produces doubles. So produce a double every
2009         // time. This at least allows subsequent code to not have weird conditionals.
2010             
2011         SpeculateInt32Operand op1(this, node->child1());
2012         FPRTemporary result(this);
2013             
2014         GPRReg inputGPR = op1.gpr();
2015         FPRReg outputFPR = result.fpr();
2016             
2017         m_jit.convertInt32ToDouble(inputGPR, outputFPR);
2018             
2019         JITCompiler::Jump positive = m_jit.branch32(MacroAssembler::GreaterThanOrEqual, inputGPR, TrustedImm32(0));
2020         m_jit.addDouble(JITCompiler::AbsoluteAddress(&AssemblyHelpers::twoToThe32), outputFPR);
2021         positive.link(&m_jit);
2022             
2023         doubleResult(outputFPR, node);
2024         return;
2025     }
2026     
2027     RELEASE_ASSERT(node->arithMode() == Arith::CheckOverflow);
2028
2029     SpeculateInt32Operand op1(this, node->child1());
2030     GPRTemporary result(this);
2031
2032     m_jit.move(op1.gpr(), result.gpr());
2033
2034     speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branch32(MacroAssembler::LessThan, result.gpr(), TrustedImm32(0)));
2035
2036     int32Result(result.gpr(), node, op1.format());
2037 }
2038
2039 void SpeculativeJIT::compileDoubleAsInt32(Node* node)
2040 {
2041     SpeculateDoubleOperand op1(this, node->child1());
2042     FPRTemporary scratch(this);
2043     GPRTemporary result(this);
2044     
2045     FPRReg valueFPR = op1.fpr();
2046     FPRReg scratchFPR = scratch.fpr();
2047     GPRReg resultGPR = result.gpr();
2048
2049     JITCompiler::JumpList failureCases;
2050     RELEASE_ASSERT(shouldCheckOverflow(node->arithMode()));
2051     m_jit.branchConvertDoubleToInt32(
2052         valueFPR, resultGPR, failureCases, scratchFPR,
2053         shouldCheckNegativeZero(node->arithMode()));
2054     speculationCheck(Overflow, JSValueRegs(), 0, failureCases);
2055
2056     int32Result(resultGPR, node);
2057 }
2058
2059 void SpeculativeJIT::compileDoubleRep(Node* node)
2060 {
2061     switch (node->child1().useKind()) {
2062     case NumberUse: {
2063         ASSERT(!node->child1()->isNumberConstant()); // This should have been constant folded.
2064     
2065         if (isInt32Speculation(m_state.forNode(node->child1()).m_type)) {
2066             SpeculateInt32Operand op1(this, node->child1(), ManualOperandSpeculation);
2067             FPRTemporary result(this);
2068             m_jit.convertInt32ToDouble(op1.gpr(), result.fpr());
2069             doubleResult(result.fpr(), node);
2070             return;
2071         }
2072     
2073         JSValueOperand op1(this, node->child1(), ManualOperandSpeculation);
2074         FPRTemporary result(this);
2075     
2076 #if USE(JSVALUE64)
2077         GPRTemporary temp(this);
2078
2079         GPRReg op1GPR = op1.gpr();
2080         GPRReg tempGPR = temp.gpr();
2081         FPRReg resultFPR = result.fpr();
2082     
2083         JITCompiler::Jump isInteger = m_jit.branch64(
2084             MacroAssembler::AboveOrEqual, op1GPR, GPRInfo::tagTypeNumberRegister);
2085     
2086         if (needsTypeCheck(node->child1(), SpecBytecodeNumber)) {
2087             typeCheck(
2088                 JSValueRegs(op1GPR), node->child1(), SpecBytecodeNumber,
2089                 m_jit.branchTest64(MacroAssembler::Zero, op1GPR, GPRInfo::tagTypeNumberRegister));
2090         }
2091     
2092         m_jit.move(op1GPR, tempGPR);
2093         unboxDouble(tempGPR, resultFPR);
2094         JITCompiler::Jump done = m_jit.jump();
2095     
2096         isInteger.link(&m_jit);
2097         m_jit.convertInt32ToDouble(op1GPR, resultFPR);
2098         done.link(&m_jit);
2099 #else // USE(JSVALUE64) -> this is the 32_64 case
2100         FPRTemporary temp(this);
2101     
2102         GPRReg op1TagGPR = op1.tagGPR();
2103         GPRReg op1PayloadGPR = op1.payloadGPR();
2104         FPRReg tempFPR = temp.fpr();
2105         FPRReg resultFPR = result.fpr();
2106     
2107         JITCompiler::Jump isInteger = m_jit.branch32(
2108             MacroAssembler::Equal, op1TagGPR, TrustedImm32(JSValue::Int32Tag));
2109     
2110         if (needsTypeCheck(node->child1(), SpecBytecodeNumber)) {
2111             typeCheck(
2112                 JSValueRegs(op1TagGPR, op1PayloadGPR), node->child1(), SpecBytecodeNumber,
2113                 m_jit.branch32(MacroAssembler::AboveOrEqual, op1TagGPR, TrustedImm32(JSValue::LowestTag)));
2114         }
2115     
2116         unboxDouble(op1TagGPR, op1PayloadGPR, resultFPR, tempFPR);
2117         JITCompiler::Jump done = m_jit.jump();
2118     
2119         isInteger.link(&m_jit);
2120         m_jit.convertInt32ToDouble(op1PayloadGPR, resultFPR);
2121         done.link(&m_jit);
2122 #endif // USE(JSVALUE64)
2123     
2124         doubleResult(resultFPR, node);
2125         return;
2126     }
2127         
2128 #if USE(JSVALUE64)
2129     case Int52RepUse: {
2130         SpeculateStrictInt52Operand value(this, node->child1());
2131         FPRTemporary result(this);
2132         
2133         GPRReg valueGPR = value.gpr();
2134         FPRReg resultFPR = result.fpr();
2135
2136         m_jit.convertInt64ToDouble(valueGPR, resultFPR);
2137         
2138         doubleResult(resultFPR, node);
2139         return;
2140     }
2141 #endif // USE(JSVALUE64)
2142         
2143     default:
2144         RELEASE_ASSERT_NOT_REACHED();
2145         return;
2146     }
2147 }
2148
2149 void SpeculativeJIT::compileValueRep(Node* node)
2150 {
2151     switch (node->child1().useKind()) {
2152     case DoubleRepUse: {
2153         SpeculateDoubleOperand value(this, node->child1());
2154         JSValueRegsTemporary result(this);
2155         
2156         FPRReg valueFPR = value.fpr();
2157         JSValueRegs resultRegs = result.regs();
2158         
2159         // It's very tempting to in-place filter the value to indicate that it's not impure NaN
2160         // anymore. Unfortunately, this would be unsound. If it's a GetLocal or if the value was
2161         // subject to a prior SetLocal, filtering the value would imply that the corresponding
2162         // local was purified.
2163         if (needsTypeCheck(node->child1(), ~SpecDoubleImpureNaN))
2164             m_jit.purifyNaN(valueFPR);
2165
2166         boxDouble(valueFPR, resultRegs);
2167         
2168         jsValueResult(resultRegs, node);
2169         return;
2170     }
2171         
2172 #if USE(JSVALUE64)
2173     case Int52RepUse: {
2174         SpeculateStrictInt52Operand value(this, node->child1());
2175         GPRTemporary result(this);
2176         
2177         GPRReg valueGPR = value.gpr();
2178         GPRReg resultGPR = result.gpr();
2179         
2180         boxInt52(valueGPR, resultGPR, DataFormatStrictInt52);
2181         
2182         jsValueResult(resultGPR, node);
2183         return;
2184     }
2185 #endif // USE(JSVALUE64)
2186         
2187     default:
2188         RELEASE_ASSERT_NOT_REACHED();
2189         return;
2190     }
2191 }
2192
2193 static double clampDoubleToByte(double d)
2194 {
2195     d += 0.5;
2196     if (!(d > 0))
2197         d = 0;
2198     else if (d > 255)
2199         d = 255;
2200     return d;
2201 }
2202
2203 static void compileClampIntegerToByte(JITCompiler& jit, GPRReg result)
2204 {
2205     MacroAssembler::Jump inBounds = jit.branch32(MacroAssembler::BelowOrEqual, result, JITCompiler::TrustedImm32(0xff));
2206     MacroAssembler::Jump tooBig = jit.branch32(MacroAssembler::GreaterThan, result, JITCompiler::TrustedImm32(0xff));
2207     jit.xorPtr(result, result);
2208     MacroAssembler::Jump clamped = jit.jump();
2209     tooBig.link(&jit);
2210     jit.move(JITCompiler::TrustedImm32(255), result);
2211     clamped.link(&jit);
2212     inBounds.link(&jit);
2213 }
2214
2215 static void compileClampDoubleToByte(JITCompiler& jit, GPRReg result, FPRReg source, FPRReg scratch)
2216 {
2217     // Unordered compare so we pick up NaN
2218     static const double zero = 0;
2219     static const double byteMax = 255;
2220     static const double half = 0.5;
2221     jit.loadDouble(MacroAssembler::TrustedImmPtr(&zero), scratch);
2222     MacroAssembler::Jump tooSmall = jit.branchDouble(MacroAssembler::DoubleLessThanOrEqualOrUnordered, source, scratch);
2223     jit.loadDouble(MacroAssembler::TrustedImmPtr(&byteMax), scratch);
2224     MacroAssembler::Jump tooBig = jit.branchDouble(MacroAssembler::DoubleGreaterThan, source, scratch);
2225     
2226     jit.loadDouble(MacroAssembler::TrustedImmPtr(&half), scratch);
2227     // FIXME: This should probably just use a floating point round!
2228     // https://bugs.webkit.org/show_bug.cgi?id=72054
2229     jit.addDouble(source, scratch);
2230     jit.truncateDoubleToInt32(scratch, result);   
2231     MacroAssembler::Jump truncatedInt = jit.jump();
2232     
2233     tooSmall.link(&jit);
2234     jit.xorPtr(result, result);
2235     MacroAssembler::Jump zeroed = jit.jump();
2236     
2237     tooBig.link(&jit);
2238     jit.move(JITCompiler::TrustedImm32(255), result);
2239     
2240     truncatedInt.link(&jit);
2241     zeroed.link(&jit);
2242
2243 }
2244
2245 JITCompiler::Jump SpeculativeJIT::jumpForTypedArrayOutOfBounds(Node* node, GPRReg baseGPR, GPRReg indexGPR)
2246 {
2247     if (node->op() == PutByValAlias)
2248         return JITCompiler::Jump();
2249     JSArrayBufferView* view = m_jit.graph().tryGetFoldableView(
2250         m_state.forNode(m_jit.graph().child(node, 0)).m_value, node->arrayMode());
2251     if (view) {
2252         uint32_t length = view->length();
2253         Node* indexNode = m_jit.graph().child(node, 1).node();
2254         if (indexNode->isInt32Constant() && indexNode->asUInt32() < length)
2255             return JITCompiler::Jump();
2256         return m_jit.branch32(
2257             MacroAssembler::AboveOrEqual, indexGPR, MacroAssembler::Imm32(length));
2258     }
2259     return m_jit.branch32(
2260         MacroAssembler::AboveOrEqual, indexGPR,
2261         MacroAssembler::Address(baseGPR, JSArrayBufferView::offsetOfLength()));
2262 }
2263
2264 void SpeculativeJIT::emitTypedArrayBoundsCheck(Node* node, GPRReg baseGPR, GPRReg indexGPR)
2265 {
2266     JITCompiler::Jump jump = jumpForTypedArrayOutOfBounds(node, baseGPR, indexGPR);
2267     if (!jump.isSet())
2268         return;
2269     speculationCheck(OutOfBounds, JSValueRegs(), 0, jump);
2270 }
2271
2272 void SpeculativeJIT::compileGetByValOnIntTypedArray(Node* node, TypedArrayType type)
2273 {
2274     ASSERT(isInt(type));
2275     
2276     SpeculateCellOperand base(this, node->child1());
2277     SpeculateStrictInt32Operand property(this, node->child2());
2278     StorageOperand storage(this, node->child3());
2279
2280     GPRReg baseReg = base.gpr();
2281     GPRReg propertyReg = property.gpr();
2282     GPRReg storageReg = storage.gpr();
2283
2284     GPRTemporary result(this);
2285     GPRReg resultReg = result.gpr();
2286
2287     ASSERT(node->arrayMode().alreadyChecked(m_jit.graph(), node, m_state.forNode(node->child1())));
2288
2289     emitTypedArrayBoundsCheck(node, baseReg, propertyReg);
2290     switch (elementSize(type)) {
2291     case 1:
2292         if (isSigned(type))
2293             m_jit.load8SignedExtendTo32(MacroAssembler::BaseIndex(storageReg, propertyReg, MacroAssembler::TimesOne), resultReg);
2294         else
2295             m_jit.load8(MacroAssembler::BaseIndex(storageReg, propertyReg, MacroAssembler::TimesOne), resultReg);
2296         break;
2297     case 2:
2298         if (isSigned(type))
2299             m_jit.load16SignedExtendTo32(MacroAssembler::BaseIndex(storageReg, propertyReg, MacroAssembler::TimesTwo), resultReg);
2300         else
2301             m_jit.load16(MacroAssembler::BaseIndex(storageReg, propertyReg, MacroAssembler::TimesTwo), resultReg);
2302         break;
2303     case 4:
2304         m_jit.load32(MacroAssembler::BaseIndex(storageReg, propertyReg, MacroAssembler::TimesFour), resultReg);
2305         break;
2306     default:
2307         CRASH();
2308     }
2309     if (elementSize(type) < 4 || isSigned(type)) {
2310         int32Result(resultReg, node);
2311         return;
2312     }
2313     
2314     ASSERT(elementSize(type) == 4 && !isSigned(type));
2315     if (node->shouldSpeculateInt32()) {
2316         speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branch32(MacroAssembler::LessThan, resultReg, TrustedImm32(0)));
2317         int32Result(resultReg, node);
2318         return;
2319     }
2320     
2321 #if USE(JSVALUE64)
2322     if (node->shouldSpeculateMachineInt()) {
2323         m_jit.zeroExtend32ToPtr(resultReg, resultReg);
2324         strictInt52Result(resultReg, node);
2325         return;
2326     }
2327 #endif
2328     
2329     FPRTemporary fresult(this);
2330     m_jit.convertInt32ToDouble(resultReg, fresult.fpr());
2331     JITCompiler::Jump positive = m_jit.branch32(MacroAssembler::GreaterThanOrEqual, resultReg, TrustedImm32(0));
2332     m_jit.addDouble(JITCompiler::AbsoluteAddress(&AssemblyHelpers::twoToThe32), fresult.fpr());
2333     positive.link(&m_jit);
2334     doubleResult(fresult.fpr(), node);
2335 }
2336
2337 void SpeculativeJIT::compilePutByValForIntTypedArray(GPRReg base, GPRReg property, Node* node, TypedArrayType type)
2338 {
2339     ASSERT(isInt(type));
2340     
2341     StorageOperand storage(this, m_jit.graph().varArgChild(node, 3));
2342     GPRReg storageReg = storage.gpr();
2343     
2344     Edge valueUse = m_jit.graph().varArgChild(node, 2);
2345     
2346     GPRTemporary value;
2347     GPRReg valueGPR = InvalidGPRReg;
2348     
2349     if (valueUse->isConstant()) {
2350         JSValue jsValue = valueUse->asJSValue();
2351         if (!jsValue.isNumber()) {
2352             terminateSpeculativeExecution(Uncountable, JSValueRegs(), 0);
2353             noResult(node);
2354             return;
2355         }
2356         double d = jsValue.asNumber();
2357         if (isClamped(type)) {
2358             ASSERT(elementSize(type) == 1);
2359             d = clampDoubleToByte(d);
2360         }
2361         GPRTemporary scratch(this);
2362         GPRReg scratchReg = scratch.gpr();
2363         m_jit.move(Imm32(toInt32(d)), scratchReg);
2364         value.adopt(scratch);
2365         valueGPR = scratchReg;
2366     } else {
2367         switch (valueUse.useKind()) {
2368         case Int32Use: {
2369             SpeculateInt32Operand valueOp(this, valueUse);
2370             GPRTemporary scratch(this);
2371             GPRReg scratchReg = scratch.gpr();
2372             m_jit.move(valueOp.gpr(), scratchReg);
2373             if (isClamped(type)) {
2374                 ASSERT(elementSize(type) == 1);
2375                 compileClampIntegerToByte(m_jit, scratchReg);
2376             }
2377             value.adopt(scratch);
2378             valueGPR = scratchReg;
2379             break;
2380         }
2381             
2382 #if USE(JSVALUE64)
2383         case Int52RepUse: {
2384             SpeculateStrictInt52Operand valueOp(this, valueUse);
2385             GPRTemporary scratch(this);
2386             GPRReg scratchReg = scratch.gpr();
2387             m_jit.move(valueOp.gpr(), scratchReg);
2388             if (isClamped(type)) {
2389                 ASSERT(elementSize(type) == 1);
2390                 MacroAssembler::Jump inBounds = m_jit.branch64(
2391                     MacroAssembler::BelowOrEqual, scratchReg, JITCompiler::TrustedImm64(0xff));
2392                 MacroAssembler::Jump tooBig = m_jit.branch64(
2393                     MacroAssembler::GreaterThan, scratchReg, JITCompiler::TrustedImm64(0xff));
2394                 m_jit.move(TrustedImm32(0), scratchReg);
2395                 MacroAssembler::Jump clamped = m_jit.jump();
2396                 tooBig.link(&m_jit);
2397                 m_jit.move(JITCompiler::TrustedImm32(255), scratchReg);
2398                 clamped.link(&m_jit);
2399                 inBounds.link(&m_jit);
2400             }
2401             value.adopt(scratch);
2402             valueGPR = scratchReg;
2403             break;
2404         }
2405 #endif // USE(JSVALUE64)
2406             
2407         case DoubleRepUse: {
2408             if (isClamped(type)) {
2409                 ASSERT(elementSize(type) == 1);
2410                 SpeculateDoubleOperand valueOp(this, valueUse);
2411                 GPRTemporary result(this);
2412                 FPRTemporary floatScratch(this);
2413                 FPRReg fpr = valueOp.fpr();
2414                 GPRReg gpr = result.gpr();
2415                 compileClampDoubleToByte(m_jit, gpr, fpr, floatScratch.fpr());
2416                 value.adopt(result);
2417                 valueGPR = gpr;
2418             } else {
2419                 SpeculateDoubleOperand valueOp(this, valueUse);
2420                 GPRTemporary result(this);
2421                 FPRReg fpr = valueOp.fpr();
2422                 GPRReg gpr = result.gpr();
2423                 MacroAssembler::Jump notNaN = m_jit.branchDouble(MacroAssembler::DoubleEqual, fpr, fpr);
2424                 m_jit.xorPtr(gpr, gpr);
2425                 MacroAssembler::Jump fixed = m_jit.jump();
2426                 notNaN.link(&m_jit);
2427                 
2428                 MacroAssembler::Jump failed = m_jit.branchTruncateDoubleToInt32(
2429                     fpr, gpr, MacroAssembler::BranchIfTruncateFailed);
2430                 
2431                 addSlowPathGenerator(slowPathCall(failed, this, toInt32, gpr, fpr));
2432                 
2433                 fixed.link(&m_jit);
2434                 value.adopt(result);
2435                 valueGPR = gpr;
2436             }
2437             break;
2438         }
2439             
2440         default:
2441             RELEASE_ASSERT_NOT_REACHED();
2442             break;
2443         }
2444     }
2445     
2446     ASSERT_UNUSED(valueGPR, valueGPR != property);
2447     ASSERT(valueGPR != base);
2448     ASSERT(valueGPR != storageReg);
2449     MacroAssembler::Jump outOfBounds = jumpForTypedArrayOutOfBounds(node, base, property);
2450     if (node->arrayMode().isInBounds() && outOfBounds.isSet()) {
2451         speculationCheck(OutOfBounds, JSValueSource(), 0, outOfBounds);
2452         outOfBounds = MacroAssembler::Jump();
2453     }
2454
2455     switch (elementSize(type)) {
2456     case 1:
2457         m_jit.store8(value.gpr(), MacroAssembler::BaseIndex(storageReg, property, MacroAssembler::TimesOne));
2458         break;
2459     case 2:
2460         m_jit.store16(value.gpr(), MacroAssembler::BaseIndex(storageReg, property, MacroAssembler::TimesTwo));
2461         break;
2462     case 4:
2463         m_jit.store32(value.gpr(), MacroAssembler::BaseIndex(storageReg, property, MacroAssembler::TimesFour));
2464         break;
2465     default:
2466         CRASH();
2467     }
2468     if (outOfBounds.isSet())
2469         outOfBounds.link(&m_jit);
2470     noResult(node);
2471 }
2472
2473 void SpeculativeJIT::compileGetByValOnFloatTypedArray(Node* node, TypedArrayType type)
2474 {
2475     ASSERT(isFloat(type));
2476     
2477     SpeculateCellOperand base(this, node->child1());
2478     SpeculateStrictInt32Operand property(this, node->child2());
2479     StorageOperand storage(this, node->child3());
2480
2481     GPRReg baseReg = base.gpr();
2482     GPRReg propertyReg = property.gpr();
2483     GPRReg storageReg = storage.gpr();
2484
2485     ASSERT(node->arrayMode().alreadyChecked(m_jit.graph(), node, m_state.forNode(node->child1())));
2486
2487     FPRTemporary result(this);
2488     FPRReg resultReg = result.fpr();
2489     emitTypedArrayBoundsCheck(node, baseReg, propertyReg);
2490     switch (elementSize(type)) {
2491     case 4:
2492         m_jit.loadFloat(MacroAssembler::BaseIndex(storageReg, propertyReg, MacroAssembler::TimesFour), resultReg);
2493         m_jit.convertFloatToDouble(resultReg, resultReg);
2494         break;
2495     case 8: {
2496         m_jit.loadDouble(MacroAssembler::BaseIndex(storageReg, propertyReg, MacroAssembler::TimesEight), resultReg);
2497         break;
2498     }
2499     default:
2500         RELEASE_ASSERT_NOT_REACHED();
2501     }
2502     
2503     doubleResult(resultReg, node);
2504 }
2505
2506 void SpeculativeJIT::compilePutByValForFloatTypedArray(GPRReg base, GPRReg property, Node* node, TypedArrayType type)
2507 {
2508     ASSERT(isFloat(type));
2509     
2510     StorageOperand storage(this, m_jit.graph().varArgChild(node, 3));
2511     GPRReg storageReg = storage.gpr();
2512     
2513     Edge baseUse = m_jit.graph().varArgChild(node, 0);
2514     Edge valueUse = m_jit.graph().varArgChild(node, 2);
2515
2516     SpeculateDoubleOperand valueOp(this, valueUse);
2517     FPRTemporary scratch(this);
2518     FPRReg valueFPR = valueOp.fpr();
2519     FPRReg scratchFPR = scratch.fpr();
2520
2521     ASSERT_UNUSED(baseUse, node->arrayMode().alreadyChecked(m_jit.graph(), node, m_state.forNode(baseUse)));
2522     
2523     MacroAssembler::Jump outOfBounds = jumpForTypedArrayOutOfBounds(node, base, property);
2524     if (node->arrayMode().isInBounds() && outOfBounds.isSet()) {
2525         speculationCheck(OutOfBounds, JSValueSource(), 0, outOfBounds);
2526         outOfBounds = MacroAssembler::Jump();
2527     }
2528     
2529     switch (elementSize(type)) {
2530     case 4: {
2531         m_jit.moveDouble(valueFPR, scratchFPR);
2532         m_jit.convertDoubleToFloat(valueFPR, scratchFPR);
2533         m_jit.storeFloat(scratchFPR, MacroAssembler::BaseIndex(storageReg, property, MacroAssembler::TimesFour));
2534         break;
2535     }
2536     case 8:
2537         m_jit.storeDouble(valueFPR, MacroAssembler::BaseIndex(storageReg, property, MacroAssembler::TimesEight));
2538         break;
2539     default:
2540         RELEASE_ASSERT_NOT_REACHED();
2541     }
2542     if (outOfBounds.isSet())
2543         outOfBounds.link(&m_jit);
2544     noResult(node);
2545 }
2546
2547 void SpeculativeJIT::compileInstanceOfForObject(Node*, GPRReg valueReg, GPRReg prototypeReg, GPRReg scratchReg, GPRReg scratch2Reg)
2548 {
2549     // Check that prototype is an object.
2550     speculationCheck(BadType, JSValueRegs(), 0, m_jit.branchIfCellNotObject(prototypeReg));
2551     
2552     // Initialize scratchReg with the value being checked.
2553     m_jit.move(valueReg, scratchReg);
2554     
2555     // Walk up the prototype chain of the value (in scratchReg), comparing to prototypeReg.
2556     MacroAssembler::Label loop(&m_jit);
2557     m_jit.emitLoadStructure(scratchReg, scratchReg, scratch2Reg);
2558     m_jit.loadPtr(MacroAssembler::Address(scratchReg, Structure::prototypeOffset() + CellPayloadOffset), scratchReg);
2559     MacroAssembler::Jump isInstance = m_jit.branchPtr(MacroAssembler::Equal, scratchReg, prototypeReg);
2560 #if USE(JSVALUE64)
2561     branchIsCell(JSValueRegs(scratchReg)).linkTo(loop, &m_jit);
2562 #else
2563     m_jit.branchTestPtr(MacroAssembler::NonZero, scratchReg).linkTo(loop, &m_jit);
2564 #endif
2565     
2566     // No match - result is false.
2567 #if USE(JSVALUE64)
2568     m_jit.move(MacroAssembler::TrustedImm64(JSValue::encode(jsBoolean(false))), scratchReg);
2569 #else
2570     m_jit.move(MacroAssembler::TrustedImm32(0), scratchReg);
2571 #endif
2572     MacroAssembler::Jump putResult = m_jit.jump();
2573     
2574     isInstance.link(&m_jit);
2575 #if USE(JSVALUE64)
2576     m_jit.move(MacroAssembler::TrustedImm64(JSValue::encode(jsBoolean(true))), scratchReg);
2577 #else
2578     m_jit.move(MacroAssembler::TrustedImm32(1), scratchReg);
2579 #endif
2580     
2581     putResult.link(&m_jit);
2582 }
2583
2584 void SpeculativeJIT::compileInstanceOf(Node* node)
2585 {
2586     if (node->child1().useKind() == UntypedUse) {
2587         // It might not be a cell. Speculate less aggressively.
2588         // Or: it might only be used once (i.e. by us), so we get zero benefit
2589         // from speculating any more aggressively than we absolutely need to.
2590         
2591         JSValueOperand value(this, node->child1());
2592         SpeculateCellOperand prototype(this, node->child2());
2593         GPRTemporary scratch(this);
2594         GPRTemporary scratch2(this);
2595         
2596         GPRReg prototypeReg = prototype.gpr();
2597         GPRReg scratchReg = scratch.gpr();
2598         GPRReg scratch2Reg = scratch2.gpr();
2599         
2600         MacroAssembler::Jump isCell = branchIsCell(value.jsValueRegs());
2601         GPRReg valueReg = value.jsValueRegs().payloadGPR();
2602         moveFalseTo(scratchReg);
2603
2604         MacroAssembler::Jump done = m_jit.jump();
2605         
2606         isCell.link(&m_jit);
2607         
2608         compileInstanceOfForObject(node, valueReg, prototypeReg, scratchReg, scratch2Reg);
2609         
2610         done.link(&m_jit);
2611
2612         blessedBooleanResult(scratchReg, node);
2613         return;
2614     }
2615     
2616     SpeculateCellOperand value(this, node->child1());
2617     SpeculateCellOperand prototype(this, node->child2());
2618     
2619     GPRTemporary scratch(this);
2620     GPRTemporary scratch2(this);
2621     
2622     GPRReg valueReg = value.gpr();
2623     GPRReg prototypeReg = prototype.gpr();
2624     GPRReg scratchReg = scratch.gpr();
2625     GPRReg scratch2Reg = scratch2.gpr();
2626     
2627     compileInstanceOfForObject(node, valueReg, prototypeReg, scratchReg, scratch2Reg);
2628
2629     blessedBooleanResult(scratchReg, node);
2630 }
2631
2632 void SpeculativeJIT::compileAdd(Node* node)
2633 {
2634     switch (node->binaryUseKind()) {
2635     case Int32Use: {
2636         ASSERT(!shouldCheckNegativeZero(node->arithMode()));
2637         
2638         if (node->child1()->isInt32Constant()) {
2639             int32_t imm1 = node->child1()->asInt32();
2640             SpeculateInt32Operand op2(this, node->child2());
2641             GPRTemporary result(this);
2642
2643             if (!shouldCheckOverflow(node->arithMode())) {
2644                 m_jit.move(op2.gpr(), result.gpr());
2645                 m_jit.add32(Imm32(imm1), result.gpr());
2646             } else
2647                 speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branchAdd32(MacroAssembler::Overflow, op2.gpr(), Imm32(imm1), result.gpr()));
2648
2649             int32Result(result.gpr(), node);
2650             return;
2651         }
2652         
2653         if (node->child2()->isInt32Constant()) {
2654             SpeculateInt32Operand op1(this, node->child1());
2655             int32_t imm2 = node->child2()->asInt32();
2656             GPRTemporary result(this);
2657                 
2658             if (!shouldCheckOverflow(node->arithMode())) {
2659                 m_jit.move(op1.gpr(), result.gpr());
2660                 m_jit.add32(Imm32(imm2), result.gpr());
2661             } else
2662                 speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branchAdd32(MacroAssembler::Overflow, op1.gpr(), Imm32(imm2), result.gpr()));
2663
2664             int32Result(result.gpr(), node);
2665             return;
2666         }
2667                 
2668         SpeculateInt32Operand op1(this, node->child1());
2669         SpeculateInt32Operand op2(this, node->child2());
2670         GPRTemporary result(this, Reuse, op1, op2);
2671
2672         GPRReg gpr1 = op1.gpr();
2673         GPRReg gpr2 = op2.gpr();
2674         GPRReg gprResult = result.gpr();
2675
2676         if (!shouldCheckOverflow(node->arithMode())) {
2677             if (gpr1 == gprResult)
2678                 m_jit.add32(gpr2, gprResult);
2679             else {
2680                 m_jit.move(gpr2, gprResult);
2681                 m_jit.add32(gpr1, gprResult);
2682             }
2683         } else {
2684             MacroAssembler::Jump check = m_jit.branchAdd32(MacroAssembler::Overflow, gpr1, gpr2, gprResult);
2685                 
2686             if (gpr1 == gprResult)
2687                 speculationCheck(Overflow, JSValueRegs(), 0, check, SpeculationRecovery(SpeculativeAdd, gprResult, gpr2));
2688             else if (gpr2 == gprResult)
2689                 speculationCheck(Overflow, JSValueRegs(), 0, check, SpeculationRecovery(SpeculativeAdd, gprResult, gpr1));
2690             else
2691                 speculationCheck(Overflow, JSValueRegs(), 0, check);
2692         }
2693
2694         int32Result(gprResult, node);
2695         return;
2696     }
2697         
2698 #if USE(JSVALUE64)
2699     case Int52RepUse: {
2700         ASSERT(shouldCheckOverflow(node->arithMode()));
2701         ASSERT(!shouldCheckNegativeZero(node->arithMode()));
2702
2703         // Will we need an overflow check? If we can prove that neither input can be
2704         // Int52 then the overflow check will not be necessary.
2705         if (!m_state.forNode(node->child1()).couldBeType(SpecInt52)
2706             && !m_state.forNode(node->child2()).couldBeType(SpecInt52)) {
2707             SpeculateWhicheverInt52Operand op1(this, node->child1());
2708             SpeculateWhicheverInt52Operand op2(this, node->child2(), op1);
2709             GPRTemporary result(this, Reuse, op1);
2710             m_jit.move(op1.gpr(), result.gpr());
2711             m_jit.add64(op2.gpr(), result.gpr());
2712             int52Result(result.gpr(), node, op1.format());
2713             return;
2714         }
2715         
2716         SpeculateInt52Operand op1(this, node->child1());
2717         SpeculateInt52Operand op2(this, node->child2());
2718         GPRTemporary result(this);
2719         m_jit.move(op1.gpr(), result.gpr());
2720         speculationCheck(
2721             Int52Overflow, JSValueRegs(), 0,
2722             m_jit.branchAdd64(MacroAssembler::Overflow, op2.gpr(), result.gpr()));
2723         int52Result(result.gpr(), node);
2724         return;
2725     }
2726 #endif // USE(JSVALUE64)
2727     
2728     case DoubleRepUse: {
2729         SpeculateDoubleOperand op1(this, node->child1());
2730         SpeculateDoubleOperand op2(this, node->child2());
2731         FPRTemporary result(this, op1, op2);
2732
2733         FPRReg reg1 = op1.fpr();
2734         FPRReg reg2 = op2.fpr();
2735         m_jit.addDouble(reg1, reg2, result.fpr());
2736
2737         doubleResult(result.fpr(), node);
2738         return;
2739     }
2740         
2741     default:
2742         RELEASE_ASSERT_NOT_REACHED();
2743         break;
2744     }
2745 }
2746
2747 void SpeculativeJIT::compileMakeRope(Node* node)
2748 {
2749     ASSERT(node->child1().useKind() == KnownStringUse);
2750     ASSERT(node->child2().useKind() == KnownStringUse);
2751     ASSERT(!node->child3() || node->child3().useKind() == KnownStringUse);
2752     
2753     SpeculateCellOperand op1(this, node->child1());
2754     SpeculateCellOperand op2(this, node->child2());
2755     SpeculateCellOperand op3(this, node->child3());
2756     GPRTemporary result(this);
2757     GPRTemporary allocator(this);
2758     GPRTemporary scratch(this);
2759     
2760     GPRReg opGPRs[3];
2761     unsigned numOpGPRs;
2762     opGPRs[0] = op1.gpr();
2763     opGPRs[1] = op2.gpr();
2764     if (node->child3()) {
2765         opGPRs[2] = op3.gpr();
2766         numOpGPRs = 3;
2767     } else {
2768         opGPRs[2] = InvalidGPRReg;
2769         numOpGPRs = 2;
2770     }
2771     GPRReg resultGPR = result.gpr();
2772     GPRReg allocatorGPR = allocator.gpr();
2773     GPRReg scratchGPR = scratch.gpr();
2774     
2775     JITCompiler::JumpList slowPath;
2776     MarkedAllocator& markedAllocator = m_jit.vm()->heap.allocatorForObjectWithDestructor(sizeof(JSRopeString));
2777     m_jit.move(TrustedImmPtr(&markedAllocator), allocatorGPR);
2778     emitAllocateJSCell(resultGPR, allocatorGPR, TrustedImmPtr(m_jit.vm()->stringStructure.get()), scratchGPR, slowPath);
2779         
2780     m_jit.storePtr(TrustedImmPtr(0), JITCompiler::Address(resultGPR, JSString::offsetOfValue()));
2781     for (unsigned i = 0; i < numOpGPRs; ++i)
2782         m_jit.storePtr(opGPRs[i], JITCompiler::Address(resultGPR, JSRopeString::offsetOfFibers() + sizeof(WriteBarrier<JSString>) * i));
2783     for (unsigned i = numOpGPRs; i < JSRopeString::s_maxInternalRopeLength; ++i)
2784         m_jit.storePtr(TrustedImmPtr(0), JITCompiler::Address(resultGPR, JSRopeString::offsetOfFibers() + sizeof(WriteBarrier<JSString>) * i));
2785     m_jit.load32(JITCompiler::Address(opGPRs[0], JSString::offsetOfFlags()), scratchGPR);
2786     m_jit.load32(JITCompiler::Address(opGPRs[0], JSString::offsetOfLength()), allocatorGPR);
2787     if (!ASSERT_DISABLED) {
2788         JITCompiler::Jump ok = m_jit.branch32(
2789             JITCompiler::GreaterThanOrEqual, allocatorGPR, TrustedImm32(0));
2790         m_jit.abortWithReason(DFGNegativeStringLength);
2791         ok.link(&m_jit);
2792     }
2793     for (unsigned i = 1; i < numOpGPRs; ++i) {
2794         m_jit.and32(JITCompiler::Address(opGPRs[i], JSString::offsetOfFlags()), scratchGPR);
2795         speculationCheck(
2796             Uncountable, JSValueSource(), nullptr,
2797             m_jit.branchAdd32(
2798                 JITCompiler::Overflow,
2799                 JITCompiler::Address(opGPRs[i], JSString::offsetOfLength()), allocatorGPR));
2800     }
2801     m_jit.and32(JITCompiler::TrustedImm32(JSString::Is8Bit), scratchGPR);
2802     m_jit.store32(scratchGPR, JITCompiler::Address(resultGPR, JSString::offsetOfFlags()));
2803     if (!ASSERT_DISABLED) {
2804         JITCompiler::Jump ok = m_jit.branch32(
2805             JITCompiler::GreaterThanOrEqual, allocatorGPR, TrustedImm32(0));
2806         m_jit.abortWithReason(DFGNegativeStringLength);
2807         ok.link(&m_jit);
2808     }
2809     m_jit.store32(allocatorGPR, JITCompiler::Address(resultGPR, JSString::offsetOfLength()));
2810     
2811     switch (numOpGPRs) {
2812     case 2:
2813         addSlowPathGenerator(slowPathCall(
2814             slowPath, this, operationMakeRope2, resultGPR, opGPRs[0], opGPRs[1]));
2815         break;
2816     case 3:
2817         addSlowPathGenerator(slowPathCall(
2818             slowPath, this, operationMakeRope3, resultGPR, opGPRs[0], opGPRs[1], opGPRs[2]));
2819         break;
2820     default:
2821         RELEASE_ASSERT_NOT_REACHED();
2822         break;
2823     }
2824         
2825     cellResult(resultGPR, node);
2826 }
2827
2828 void SpeculativeJIT::compileArithSub(Node* node)
2829 {
2830     switch (node->binaryUseKind()) {
2831     case Int32Use: {
2832         ASSERT(!shouldCheckNegativeZero(node->arithMode()));
2833         
2834         if (node->child2()->isNumberConstant()) {
2835             SpeculateInt32Operand op1(this, node->child1());
2836             int32_t imm2 = node->child2()->asInt32();
2837             GPRTemporary result(this);
2838
2839             if (!shouldCheckOverflow(node->arithMode())) {
2840                 m_jit.move(op1.gpr(), result.gpr());
2841                 m_jit.sub32(Imm32(imm2), result.gpr());
2842             } else {
2843                 GPRTemporary scratch(this);
2844                 speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branchSub32(MacroAssembler::Overflow, op1.gpr(), Imm32(imm2), result.gpr(), scratch.gpr()));
2845             }
2846
2847             int32Result(result.gpr(), node);
2848             return;
2849         }
2850             
2851         if (node->child1()->isNumberConstant()) {
2852             int32_t imm1 = node->child1()->asInt32();
2853             SpeculateInt32Operand op2(this, node->child2());
2854             GPRTemporary result(this);
2855                 
2856             m_jit.move(Imm32(imm1), result.gpr());
2857             if (!shouldCheckOverflow(node->arithMode()))
2858                 m_jit.sub32(op2.gpr(), result.gpr());
2859             else
2860                 speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branchSub32(MacroAssembler::Overflow, op2.gpr(), result.gpr()));
2861                 
2862             int32Result(result.gpr(), node);
2863             return;
2864         }
2865             
2866         SpeculateInt32Operand op1(this, node->child1());
2867         SpeculateInt32Operand op2(this, node->child2());
2868         GPRTemporary result(this);
2869
2870         if (!shouldCheckOverflow(node->arithMode())) {
2871             m_jit.move(op1.gpr(), result.gpr());
2872             m_jit.sub32(op2.gpr(), result.gpr());
2873         } else
2874             speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branchSub32(MacroAssembler::Overflow, op1.gpr(), op2.gpr(), result.gpr()));
2875
2876         int32Result(result.gpr(), node);
2877         return;
2878     }
2879         
2880 #if USE(JSVALUE64)
2881     case Int52RepUse: {
2882         ASSERT(shouldCheckOverflow(node->arithMode()));
2883         ASSERT(!shouldCheckNegativeZero(node->arithMode()));
2884
2885         // Will we need an overflow check? If we can prove that neither input can be
2886         // Int52 then the overflow check will not be necessary.
2887         if (!m_state.forNode(node->child1()).couldBeType(SpecInt52)
2888             && !m_state.forNode(node->child2()).couldBeType(SpecInt52)) {
2889             SpeculateWhicheverInt52Operand op1(this, node->child1());
2890             SpeculateWhicheverInt52Operand op2(this, node->child2(), op1);
2891             GPRTemporary result(this, Reuse, op1);
2892             m_jit.move(op1.gpr(), result.gpr());
2893             m_jit.sub64(op2.gpr(), result.gpr());
2894             int52Result(result.gpr(), node, op1.format());
2895             return;
2896         }
2897         
2898         SpeculateInt52Operand op1(this, node->child1());
2899         SpeculateInt52Operand op2(this, node->child2());
2900         GPRTemporary result(this);
2901         m_jit.move(op1.gpr(), result.gpr());
2902         speculationCheck(
2903             Int52Overflow, JSValueRegs(), 0,
2904             m_jit.branchSub64(MacroAssembler::Overflow, op2.gpr(), result.gpr()));
2905         int52Result(result.gpr(), node);
2906         return;
2907     }
2908 #endif // USE(JSVALUE64)
2909
2910     case DoubleRepUse: {
2911         SpeculateDoubleOperand op1(this, node->child1());
2912         SpeculateDoubleOperand op2(this, node->child2());
2913         FPRTemporary result(this, op1);
2914
2915         FPRReg reg1 = op1.fpr();
2916         FPRReg reg2 = op2.fpr();
2917         m_jit.subDouble(reg1, reg2, result.fpr());
2918
2919         doubleResult(result.fpr(), node);
2920         return;
2921     }
2922         
2923     default:
2924         RELEASE_ASSERT_NOT_REACHED();
2925         return;
2926     }
2927 }
2928
2929 void SpeculativeJIT::compileArithNegate(Node* node)
2930 {
2931     switch (node->child1().useKind()) {
2932     case Int32Use: {
2933         SpeculateInt32Operand op1(this, node->child1());
2934         GPRTemporary result(this);
2935
2936         m_jit.move(op1.gpr(), result.gpr());
2937
2938         // Note: there is no notion of being not used as a number, but someone
2939         // caring about negative zero.
2940         
2941         if (!shouldCheckOverflow(node->arithMode()))
2942             m_jit.neg32(result.gpr());
2943         else if (!shouldCheckNegativeZero(node->arithMode()))
2944             speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branchNeg32(MacroAssembler::Overflow, result.gpr()));
2945         else {
2946             speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branchTest32(MacroAssembler::Zero, result.gpr(), TrustedImm32(0x7fffffff)));
2947             m_jit.neg32(result.gpr());
2948         }
2949
2950         int32Result(result.gpr(), node);
2951         return;
2952     }
2953
2954 #if USE(JSVALUE64)
2955     case Int52RepUse: {
2956         ASSERT(shouldCheckOverflow(node->arithMode()));
2957         
2958         if (!m_state.forNode(node->child1()).couldBeType(SpecInt52)) {
2959             SpeculateWhicheverInt52Operand op1(this, node->child1());
2960             GPRTemporary result(this);
2961             GPRReg op1GPR = op1.gpr();
2962             GPRReg resultGPR = result.gpr();
2963             m_jit.move(op1GPR, resultGPR);
2964             m_jit.neg64(resultGPR);
2965             if (shouldCheckNegativeZero(node->arithMode())) {
2966                 speculationCheck(
2967                     NegativeZero, JSValueRegs(), 0,
2968                     m_jit.branchTest64(MacroAssembler::Zero, resultGPR));
2969             }
2970             int52Result(resultGPR, node, op1.format());
2971             return;
2972         }
2973         
2974         SpeculateInt52Operand op1(this, node->child1());
2975         GPRTemporary result(this);
2976         GPRReg op1GPR = op1.gpr();
2977         GPRReg resultGPR = result.gpr();
2978         m_jit.move(op1GPR, resultGPR);
2979         speculationCheck(
2980             Int52Overflow, JSValueRegs(), 0,
2981             m_jit.branchNeg64(MacroAssembler::Overflow, resultGPR));
2982         if (shouldCheckNegativeZero(node->arithMode())) {
2983             speculationCheck(
2984                 NegativeZero, JSValueRegs(), 0,
2985                 m_jit.branchTest64(MacroAssembler::Zero, resultGPR));
2986         }
2987         int52Result(resultGPR, node);
2988         return;
2989     }
2990 #endif // USE(JSVALUE64)
2991         
2992     case DoubleRepUse: {
2993         SpeculateDoubleOperand op1(this, node->child1());
2994         FPRTemporary result(this);
2995         
2996         m_jit.negateDouble(op1.fpr(), result.fpr());
2997         
2998         doubleResult(result.fpr(), node);
2999         return;
3000     }
3001         
3002     default:
3003         RELEASE_ASSERT_NOT_REACHED();
3004         return;
3005     }
3006 }
3007 void SpeculativeJIT::compileArithMul(Node* node)
3008 {
3009     switch (node->binaryUseKind()) {
3010     case Int32Use: {
3011         SpeculateInt32Operand op1(this, node->child1());
3012         SpeculateInt32Operand op2(this, node->child2());
3013         GPRTemporary result(this);
3014
3015         GPRReg reg1 = op1.gpr();
3016         GPRReg reg2 = op2.gpr();
3017
3018         // We can perform truncated multiplications if we get to this point, because if the
3019         // fixup phase could not prove that it would be safe, it would have turned us into
3020         // a double multiplication.
3021         if (!shouldCheckOverflow(node->arithMode())) {
3022             m_jit.move(reg1, result.gpr());
3023             m_jit.mul32(reg2, result.gpr());
3024         } else {
3025             speculationCheck(
3026                 Overflow, JSValueRegs(), 0,
3027                 m_jit.branchMul32(MacroAssembler::Overflow, reg1, reg2, result.gpr()));
3028         }
3029             
3030         // Check for negative zero, if the users of this node care about such things.
3031         if (shouldCheckNegativeZero(node->arithMode())) {
3032             MacroAssembler::Jump resultNonZero = m_jit.branchTest32(MacroAssembler::NonZero, result.gpr());
3033             speculationCheck(NegativeZero, JSValueRegs(), 0, m_jit.branch32(MacroAssembler::LessThan, reg1, TrustedImm32(0)));
3034             speculationCheck(NegativeZero, JSValueRegs(), 0, m_jit.branch32(MacroAssembler::LessThan, reg2, TrustedImm32(0)));
3035             resultNonZero.link(&m_jit);
3036         }
3037
3038         int32Result(result.gpr(), node);
3039         return;
3040     }
3041     
3042 #if USE(JSVALUE64)   
3043     case Int52RepUse: {
3044         ASSERT(shouldCheckOverflow(node->arithMode()));
3045         
3046         // This is super clever. We want to do an int52 multiplication and check the
3047         // int52 overflow bit. There is no direct hardware support for this, but we do
3048         // have the ability to do an int64 multiplication and check the int64 overflow
3049         // bit. We leverage that. Consider that a, b are int52 numbers inside int64
3050         // registers, with the high 12 bits being sign-extended. We can do:
3051         //
3052         //     (a * (b << 12))
3053         //
3054         // This will give us a left-shifted int52 (value is in high 52 bits, low 16
3055         // bits are zero) plus the int52 overflow bit. I.e. whether this 64-bit
3056         // multiplication overflows is identical to whether the 'a * b' 52-bit
3057         // multiplication overflows.
3058         //
3059         // In our nomenclature, this is:
3060         //
3061         //     strictInt52(a) * int52(b) => int52
3062         //
3063         // That is "strictInt52" means unshifted and "int52" means left-shifted by 16
3064         // bits.
3065         //
3066         // We don't care which of op1 or op2 serves as the left-shifted operand, so
3067         // we just do whatever is more convenient for op1 and have op2 do the
3068         // opposite. This ensures that we do at most one shift.
3069
3070         SpeculateWhicheverInt52Operand op1(this, node->child1());
3071         SpeculateWhicheverInt52Operand op2(this, node->child2(), OppositeShift, op1);
3072         GPRTemporary result(this);
3073         
3074         GPRReg op1GPR = op1.gpr();
3075         GPRReg op2GPR = op2.gpr();
3076         GPRReg resultGPR = result.gpr();
3077         
3078         m_jit.move(op1GPR, resultGPR);
3079         speculationCheck(
3080             Int52Overflow, JSValueRegs(), 0,
3081             m_jit.branchMul64(MacroAssembler::Overflow, op2GPR, resultGPR));
3082         
3083         if (shouldCheckNegativeZero(node->arithMode())) {
3084             MacroAssembler::Jump resultNonZero = m_jit.branchTest64(
3085                 MacroAssembler::NonZero, resultGPR);
3086             speculationCheck(
3087                 NegativeZero, JSValueRegs(), 0,
3088                 m_jit.branch64(MacroAssembler::LessThan, op1GPR, TrustedImm64(0)));
3089             speculationCheck(
3090                 NegativeZero, JSValueRegs(), 0,
3091                 m_jit.branch64(MacroAssembler::LessThan, op2GPR, TrustedImm64(0)));
3092             resultNonZero.link(&m_jit);
3093         }
3094         
3095         int52Result(resultGPR, node);
3096         return;
3097     }
3098 #endif // USE(JSVALUE64)
3099         
3100     case DoubleRepUse: {
3101         SpeculateDoubleOperand op1(this, node->child1());
3102         SpeculateDoubleOperand op2(this, node->child2());
3103         FPRTemporary result(this, op1, op2);
3104         
3105         FPRReg reg1 = op1.fpr();
3106         FPRReg reg2 = op2.fpr();
3107         
3108         m_jit.mulDouble(reg1, reg2, result.fpr());
3109         
3110         doubleResult(result.fpr(), node);
3111         return;
3112     }
3113         
3114     default:
3115         RELEASE_ASSERT_NOT_REACHED();
3116         return;
3117     }
3118 }
3119
3120 void SpeculativeJIT::compileArithDiv(Node* node)
3121 {
3122     switch (node->binaryUseKind()) {
3123     case Int32Use: {
3124 #if CPU(X86) || CPU(X86_64)
3125         SpeculateInt32Operand op1(this, node->child1());
3126         SpeculateInt32Operand op2(this, node->child2());
3127         GPRTemporary eax(this, X86Registers::eax);
3128         GPRTemporary edx(this, X86Registers::edx);
3129         GPRReg op1GPR = op1.gpr();
3130         GPRReg op2GPR = op2.gpr();
3131     
3132         GPRReg op2TempGPR;
3133         GPRReg temp;
3134         if (op2GPR == X86Registers::eax || op2GPR == X86Registers::edx) {
3135             op2TempGPR = allocate();
3136             temp = op2TempGPR;
3137         } else {
3138             op2TempGPR = InvalidGPRReg;
3139             if (op1GPR == X86Registers::eax)
3140                 temp = X86Registers::edx;
3141             else
3142                 temp = X86Registers::eax;
3143         }
3144     
3145         ASSERT(temp != op1GPR);
3146         ASSERT(temp != op2GPR);
3147     
3148         m_jit.add32(JITCompiler::TrustedImm32(1), op2GPR, temp);
3149     
3150         JITCompiler::Jump safeDenominator = m_jit.branch32(JITCompiler::Above, temp, JITCompiler::TrustedImm32(1));
3151     
3152         JITCompiler::JumpList done;
3153         if (shouldCheckOverflow(node->arithMode())) {
3154             speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branchTest32(JITCompiler::Zero, op2GPR));
3155             speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branch32(JITCompiler::Equal, op1GPR, TrustedImm32(-2147483647-1)));
3156         } else {
3157             // This is the case where we convert the result to an int after we're done, and we
3158             // already know that the denominator is either -1 or 0. So, if the denominator is
3159             // zero, then the result should be zero. If the denominator is not zero (i.e. it's
3160             // -1) and the numerator is -2^31 then the result should be -2^31. Otherwise we
3161             // are happy to fall through to a normal division, since we're just dividing
3162             // something by negative 1.
3163         
3164             JITCompiler::Jump notZero = m_jit.branchTest32(JITCompiler::NonZero, op2GPR);
3165             m_jit.move(TrustedImm32(0), eax.gpr());
3166             done.append(m_jit.jump());
3167         
3168             notZero.link(&m_jit);
3169             JITCompiler::Jump notNeg2ToThe31 =
3170                 m_jit.branch32(JITCompiler::NotEqual, op1GPR, TrustedImm32(-2147483647-1));
3171             m_jit.zeroExtend32ToPtr(op1GPR, eax.gpr());
3172             done.append(m_jit.jump());
3173         
3174             notNeg2ToThe31.link(&m_jit);
3175         }
3176     
3177         safeDenominator.link(&m_jit);
3178     
3179         // If the user cares about negative zero, then speculate that we're not about
3180         // to produce negative zero.
3181         if (shouldCheckNegativeZero(node->arithMode())) {
3182             MacroAssembler::Jump numeratorNonZero = m_jit.branchTest32(MacroAssembler::NonZero, op1GPR);
3183             speculationCheck(NegativeZero, JSValueRegs(), 0, m_jit.branch32(MacroAssembler::LessThan, op2GPR, TrustedImm32(0)));
3184             numeratorNonZero.link(&m_jit);
3185         }
3186     
3187         if (op2TempGPR != InvalidGPRReg) {
3188             m_jit.move(op2GPR, op2TempGPR);
3189             op2GPR = op2TempGPR;
3190         }
3191             
3192         m_jit.move(op1GPR, eax.gpr());
3193         m_jit.assembler().cdq();
3194         m_jit.assembler().idivl_r(op2GPR);
3195             
3196         if (op2TempGPR != InvalidGPRReg)
3197             unlock(op2TempGPR);
3198
3199         // Check that there was no remainder. If there had been, then we'd be obligated to
3200         // produce a double result instead.
3201         if (shouldCheckOverflow(node->arithMode()))
3202             speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branchTest32(JITCompiler::NonZero, edx.gpr()));
3203         
3204         done.link(&m_jit);
3205         int32Result(eax.gpr(), node);
3206 #elif HAVE(ARM_IDIV_INSTRUCTIONS) || CPU(ARM64)
3207         SpeculateInt32Operand op1(this, node->child1());
3208         SpeculateInt32Operand op2(this, node->child2());
3209         GPRReg op1GPR = op1.gpr();
3210         GPRReg op2GPR = op2.gpr();
3211         GPRTemporary quotient(this);
3212         GPRTemporary multiplyAnswer(this);
3213
3214         // If the user cares about negative zero, then speculate that we're not about
3215         // to produce negative zero.
3216         if (shouldCheckNegativeZero(node->arithMode())) {
3217             MacroAssembler::Jump numeratorNonZero = m_jit.branchTest32(MacroAssembler::NonZero, op1GPR);
3218             speculationCheck(NegativeZero, JSValueRegs(), 0, m_jit.branch32(MacroAssembler::LessThan, op2GPR, TrustedImm32(0)));
3219             numeratorNonZero.link(&m_jit);
3220         }
3221
3222         m_jit.assembler().sdiv<32>(quotient.gpr(), op1GPR, op2GPR);
3223
3224         // Check that there was no remainder. If there had been, then we'd be obligated to
3225         // produce a double result instead.
3226         if (shouldCheckOverflow(node->arithMode())) {
3227             speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branchMul32(JITCompiler::Overflow, quotient.gpr(), op2GPR, multiplyAnswer.gpr()));
3228             speculationCheck(Overflow, JSValueRegs(), 0, m_jit.branch32(JITCompiler::NotEqual, multiplyAnswer.gpr(), op1GPR));
3229         }
3230
3231         int32Result(quotient.gpr(), node);
3232 #else
3233         RELEASE_ASSERT_NOT_REACHED();
3234 #endif
3235         break;
3236     }
3237         
3238     case DoubleRepUse: {
3239         SpeculateDoubleOperand op1(this, node->child1());
3240         SpeculateDoubleOperand op2(this, node->child2());
3241         FPRTemporary result(this, op1);
3242         
3243         FPRReg reg1 = op1.fpr();
3244         FPRReg reg2 = op2.fpr();
3245         m_jit.divDouble(reg1, reg2, result.fpr());
3246         
3247         doubleResult(result.fpr(), node);
3248         break;
3249     }
3250         
3251     default:
3252         RELEASE_ASSERT_NOT_REACHED();
3253         break;
3254     }
3255 }
3256
3257 void SpeculativeJIT::compileArithMod(Node* node)
3258 {
3259     switch (node->binaryUseKind()) {
3260     case Int32Use: {
3261         // In the fast path, the dividend value could be the final result
3262         // (in case of |dividend| < |divisor|), so we speculate it as strict int32.
3263         SpeculateStrictInt32Operand op1(this, node->child1());
3264         
3265         if (node->child2()->isInt32Constant()) {
3266             int32_t divisor = node->child2()->asInt32();
3267             if (divisor > 1 && hasOneBitSet(divisor)) {
3268                 unsigned logarithm = WTF::fastLog2(divisor);
3269                 GPRReg dividendGPR = op1.gpr();
3270                 GPRTemporary result(this);
3271                 GPRReg resultGPR = result.gpr();
3272
3273                 // This is what LLVM generates. It's pretty crazy. Here's my
3274                 // attempt at understanding it.
3275                 
3276                 // First, compute either divisor - 1, or 0, depending on whether
3277                 // the dividend is negative:
3278                 //
3279                 // If dividend < 0:  resultGPR = divisor - 1
3280                 // If dividend >= 0: resultGPR = 0
3281                 m_jit.move(dividendGPR, resultGPR);
3282                 m_jit.rshift32(TrustedImm32(31), resultGPR);
3283                 m_jit.urshift32(TrustedImm32(32 - logarithm), resultGPR);
3284                 
3285                 // Add in the dividend, so that:
3286                 //
3287                 // If dividend < 0:  resultGPR = dividend + divisor - 1
3288                 // If dividend >= 0: resultGPR = dividend
3289                 m_jit.add32(dividendGPR, resultGPR);
3290                 
3291                 // Mask so as to only get the *high* bits. This rounds down
3292                 // (towards negative infinity) resultGPR to the nearest multiple
3293                 // of divisor, so that:
3294                 //
3295                 // If dividend < 0:  resultGPR = floor((dividend + divisor - 1) / divisor)
3296                 // If dividend >= 0: resultGPR = floor(dividend / divisor)
3297                 //
3298                 // Note that this can be simplified to:
3299                 //
3300                 // If dividend < 0:  resultGPR = ceil(dividend / divisor)