REGRESSION(r173031): crashes during run-layout-jsc on x86/Linux
[WebKit-https.git] / Source / JavaScriptCore / jit / JIT.h
1 /*
2  * Copyright (C) 2008, 2012, 2013, 2014 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 #ifndef JIT_h
27 #define JIT_h
28
29 #if ENABLE(JIT)
30
31 // We've run into some problems where changing the size of the class JIT leads to
32 // performance fluctuations.  Try forcing alignment in an attempt to stabalize this.
33 #if COMPILER(GCC)
34 #define JIT_CLASS_ALIGNMENT __attribute__ ((aligned (32)))
35 #else
36 #define JIT_CLASS_ALIGNMENT
37 #endif
38
39 #define ASSERT_JIT_OFFSET(actual, expected) ASSERT_WITH_MESSAGE(actual == expected, "JIT Offset \"%s\" should be %d, not %d.\n", #expected, static_cast<int>(expected), static_cast<int>(actual));
40
41 #include "CodeBlock.h"
42 #include "CompactJITCodeMap.h"
43 #include "Interpreter.h"
44 #include "JITDisassembler.h"
45 #include "JITInlineCacheGenerator.h"
46 #include "JSInterfaceJIT.h"
47 #include "Opcode.h"
48 #include "ResultType.h"
49 #include "SamplingTool.h"
50 #include "UnusedPointer.h"
51
52 namespace JSC {
53
54     class ArrayAllocationProfile;
55     class CodeBlock;
56     class FunctionExecutable;
57     class JIT;
58     class Identifier;
59     class Interpreter;
60     class JSScope;
61     class JSStack;
62     class MarkedAllocator;
63     class Register;
64     class StructureChain;
65
66     struct CallLinkInfo;
67     struct Instruction;
68     struct OperandTypes;
69     struct PolymorphicAccessStructureList;
70     struct SimpleJumpTable;
71     struct StringJumpTable;
72     struct StructureStubInfo;
73
74     struct CallRecord {
75         MacroAssembler::Call from;
76         unsigned bytecodeOffset;
77         void* to;
78
79         CallRecord()
80         {
81         }
82
83         CallRecord(MacroAssembler::Call from, unsigned bytecodeOffset, void* to = 0)
84             : from(from)
85             , bytecodeOffset(bytecodeOffset)
86             , to(to)
87         {
88         }
89     };
90
91     struct JumpTable {
92         MacroAssembler::Jump from;
93         unsigned toBytecodeOffset;
94
95         JumpTable(MacroAssembler::Jump f, unsigned t)
96             : from(f)
97             , toBytecodeOffset(t)
98         {
99         }
100     };
101
102     struct SlowCaseEntry {
103         MacroAssembler::Jump from;
104         unsigned to;
105         unsigned hint;
106         
107         SlowCaseEntry(MacroAssembler::Jump f, unsigned t, unsigned h = 0)
108             : from(f)
109             , to(t)
110             , hint(h)
111         {
112         }
113     };
114
115     struct SwitchRecord {
116         enum Type {
117             Immediate,
118             Character,
119             String
120         };
121
122         Type type;
123
124         union {
125             SimpleJumpTable* simpleJumpTable;
126             StringJumpTable* stringJumpTable;
127         } jumpTable;
128
129         unsigned bytecodeOffset;
130         unsigned defaultOffset;
131
132         SwitchRecord(SimpleJumpTable* jumpTable, unsigned bytecodeOffset, unsigned defaultOffset, Type type)
133             : type(type)
134             , bytecodeOffset(bytecodeOffset)
135             , defaultOffset(defaultOffset)
136         {
137             this->jumpTable.simpleJumpTable = jumpTable;
138         }
139
140         SwitchRecord(StringJumpTable* jumpTable, unsigned bytecodeOffset, unsigned defaultOffset)
141             : type(String)
142             , bytecodeOffset(bytecodeOffset)
143             , defaultOffset(defaultOffset)
144         {
145             this->jumpTable.stringJumpTable = jumpTable;
146         }
147     };
148
149     struct ByValCompilationInfo {
150         ByValCompilationInfo() { }
151         
152         ByValCompilationInfo(unsigned bytecodeIndex, MacroAssembler::PatchableJump badTypeJump, JITArrayMode arrayMode, MacroAssembler::Label doneTarget)
153             : bytecodeIndex(bytecodeIndex)
154             , badTypeJump(badTypeJump)
155             , arrayMode(arrayMode)
156             , doneTarget(doneTarget)
157         {
158         }
159         
160         unsigned bytecodeIndex;
161         MacroAssembler::PatchableJump badTypeJump;
162         JITArrayMode arrayMode;
163         MacroAssembler::Label doneTarget;
164         MacroAssembler::Label slowPathTarget;
165         MacroAssembler::Call returnAddress;
166     };
167
168     struct CallCompilationInfo {
169         MacroAssembler::DataLabelPtr hotPathBegin;
170         MacroAssembler::Call hotPathOther;
171         MacroAssembler::Call callReturnLocation;
172         CallLinkInfo* callLinkInfo;
173     };
174
175     // Near calls can only be patched to other JIT code, regular calls can be patched to JIT code or relinked to stub functions.
176     void ctiPatchNearCallByReturnAddress(CodeBlock* codeblock, ReturnAddressPtr returnAddress, MacroAssemblerCodePtr newCalleeFunction);
177     void ctiPatchCallByReturnAddress(CodeBlock* codeblock, ReturnAddressPtr returnAddress, MacroAssemblerCodePtr newCalleeFunction);
178     void ctiPatchCallByReturnAddress(CodeBlock* codeblock, ReturnAddressPtr returnAddress, FunctionPtr newCalleeFunction);
179
180     class JIT : private JSInterfaceJIT {
181         friend class JITSlowPathCall;
182         friend class JITStubCall;
183
184         using MacroAssembler::Jump;
185         using MacroAssembler::JumpList;
186         using MacroAssembler::Label;
187
188         static const uintptr_t patchGetByIdDefaultStructure = unusedPointer;
189         static const int patchGetByIdDefaultOffset = 0;
190         // Magic number - initial offset cannot be representable as a signed 8bit value, or the X86Assembler
191         // will compress the displacement, and we may not be able to fit a patched offset.
192         static const int patchPutByIdDefaultOffset = 256;
193
194     public:
195         static CompilationResult compile(VM* vm, CodeBlock* codeBlock, JITCompilationEffort effort)
196         {
197             return JIT(vm, codeBlock).privateCompile(effort);
198         }
199         
200         static void compileGetByVal(VM* vm, CodeBlock* codeBlock, ByValInfo* byValInfo, ReturnAddressPtr returnAddress, JITArrayMode arrayMode)
201         {
202             JIT jit(vm, codeBlock);
203             jit.m_bytecodeOffset = byValInfo->bytecodeIndex;
204             jit.privateCompileGetByVal(byValInfo, returnAddress, arrayMode);
205         }
206
207         static void compilePutByVal(VM* vm, CodeBlock* codeBlock, ByValInfo* byValInfo, ReturnAddressPtr returnAddress, JITArrayMode arrayMode)
208         {
209             JIT jit(vm, codeBlock);
210             jit.m_bytecodeOffset = byValInfo->bytecodeIndex;
211             jit.privateCompilePutByVal(byValInfo, returnAddress, arrayMode);
212         }
213         
214         static void compileDirectPutByVal(VM* vm, CodeBlock* codeBlock, ByValInfo* byValInfo, ReturnAddressPtr returnAddress, JITArrayMode arrayMode)
215         {
216             JIT jit(vm, codeBlock);
217             jit.m_bytecodeOffset = byValInfo->bytecodeIndex;
218             jit.privateCompilePutByVal(byValInfo, returnAddress, arrayMode);
219         }
220
221         static void compileHasIndexedProperty(VM* vm, CodeBlock* codeBlock, ByValInfo* byValInfo, ReturnAddressPtr returnAddress, JITArrayMode arrayMode)
222         {
223             JIT jit(vm, codeBlock);
224             jit.m_bytecodeOffset = byValInfo->bytecodeIndex;
225             jit.privateCompileHasIndexedProperty(byValInfo, returnAddress, arrayMode);
226         }
227
228         static CodeRef compileCTINativeCall(VM* vm, NativeFunction func)
229         {
230             if (!vm->canUseJIT()) {
231                 return CodeRef::createLLIntCodeRef(llint_native_call_trampoline);
232             }
233             JIT jit(vm, 0);
234             return jit.privateCompileCTINativeCall(vm, func);
235         }
236
237         static unsigned frameRegisterCountFor(CodeBlock*);
238         static int stackPointerOffsetFor(CodeBlock*);
239
240     private:
241         JIT(VM*, CodeBlock* = 0);
242
243         void privateCompileMainPass();
244         void privateCompileLinkPass();
245         void privateCompileSlowCases();
246         CompilationResult privateCompile(JITCompilationEffort);
247         
248         void privateCompileGetByVal(ByValInfo*, ReturnAddressPtr, JITArrayMode);
249         void privateCompilePutByVal(ByValInfo*, ReturnAddressPtr, JITArrayMode);
250
251         void privateCompileHasIndexedProperty(ByValInfo*, ReturnAddressPtr, JITArrayMode);
252
253         Label privateCompileCTINativeCall(VM*, bool isConstruct = false);
254         CodeRef privateCompileCTINativeCall(VM*, NativeFunction);
255         void privateCompilePatchGetArrayLength(ReturnAddressPtr returnAddress);
256
257         // Add a call out from JIT code, without an exception check.
258         Call appendCall(const FunctionPtr& function)
259         {
260             Call functionCall = call();
261             m_calls.append(CallRecord(functionCall, m_bytecodeOffset, function.value()));
262             return functionCall;
263         }
264
265 #if OS(WINDOWS) && CPU(X86_64)
266         Call appendCallWithSlowPathReturnType(const FunctionPtr& function)
267         {
268             Call functionCall = callWithSlowPathReturnType();
269             m_calls.append(CallRecord(functionCall, m_bytecodeOffset, function.value()));
270             return functionCall;
271         }
272 #endif
273
274         void exceptionCheck(Jump jumpToHandler)
275         {
276             m_exceptionChecks.append(jumpToHandler);
277         }
278
279         void exceptionCheck()
280         {
281             m_exceptionChecks.append(emitExceptionCheck());
282         }
283
284         void exceptionCheckWithCallFrameRollback()
285         {
286             m_exceptionChecksWithCallFrameRollback.append(emitExceptionCheck());
287         }
288
289         void privateCompileExceptionHandlers();
290
291         void addSlowCase(Jump);
292         void addSlowCase(JumpList);
293         void addSlowCase();
294         void addJump(Jump, int);
295         void emitJumpSlowToHot(Jump, int);
296
297         void compileOpCall(OpcodeID, Instruction*, unsigned callLinkInfoIndex);
298         void compileOpCallSlowCase(OpcodeID, Instruction*, Vector<SlowCaseEntry>::iterator&, unsigned callLinkInfoIndex);
299         void compileLoadVarargs(Instruction*);
300         void compileCallEval(Instruction*);
301         void compileCallEvalSlowCase(Instruction*, Vector<SlowCaseEntry>::iterator&);
302         void emitPutCallResult(Instruction*);
303
304         enum CompileOpStrictEqType { OpStrictEq, OpNStrictEq };
305         void compileOpStrictEq(Instruction* instruction, CompileOpStrictEqType type);
306         bool isOperandConstantImmediateDouble(int src);
307         
308         void emitLoadDouble(int index, FPRegisterID value);
309         void emitLoadInt32ToDouble(int index, FPRegisterID value);
310         Jump emitJumpIfCellNotObject(RegisterID cellReg);
311
312         enum WriteBarrierMode { UnconditionalWriteBarrier, ShouldFilterBase, ShouldFilterValue, ShouldFilterBaseAndValue };
313         // value register in write barrier is used before any scratch registers
314         // so may safely be the same as either of the scratch registers.
315         void emitWriteBarrier(unsigned owner, unsigned value, WriteBarrierMode);
316         void emitWriteBarrier(JSCell* owner, unsigned value, WriteBarrierMode);
317         void emitWriteBarrier(JSCell* owner);
318
319         template<typename StructureType> // StructureType can be RegisterID or ImmPtr.
320         void emitAllocateJSObject(RegisterID allocator, StructureType, RegisterID result, RegisterID scratch);
321         
322         // This assumes that the value to profile is in regT0 and that regT3 is available for
323         // scratch.
324         void emitValueProfilingSite(ValueProfile*);
325         void emitValueProfilingSite(unsigned bytecodeOffset);
326         void emitValueProfilingSite();
327         void emitArrayProfilingSiteWithCell(RegisterID cell, RegisterID indexingType, ArrayProfile*);
328         void emitArrayProfilingSiteForBytecodeIndexWithCell(RegisterID cell, RegisterID indexingType, unsigned bytecodeIndex);
329         void emitArrayProfileStoreToHoleSpecialCase(ArrayProfile*);
330         void emitArrayProfileOutOfBoundsSpecialCase(ArrayProfile*);
331         
332         JITArrayMode chooseArrayMode(ArrayProfile*);
333         
334         // Property is in regT1, base is in regT0. regT2 contains indexing type.
335         // Property is int-checked and zero extended. Base is cell checked.
336         // Structure is already profiled. Returns the slow cases. Fall-through
337         // case contains result in regT0, and it is not yet profiled.
338         JumpList emitInt32Load(Instruction* instruction, PatchableJump& badType) { return emitContiguousLoad(instruction, badType, Int32Shape); }
339         JumpList emitDoubleLoad(Instruction*, PatchableJump& badType);
340         JumpList emitContiguousLoad(Instruction*, PatchableJump& badType, IndexingType expectedShape = ContiguousShape);
341         JumpList emitArrayStorageLoad(Instruction*, PatchableJump& badType);
342         JumpList emitLoadForArrayMode(Instruction*, JITArrayMode, PatchableJump& badType);
343
344         JumpList emitInt32GetByVal(Instruction* instruction, PatchableJump& badType) { return emitContiguousGetByVal(instruction, badType, Int32Shape); }
345         JumpList emitDoubleGetByVal(Instruction*, PatchableJump& badType);
346         JumpList emitContiguousGetByVal(Instruction*, PatchableJump& badType, IndexingType expectedShape = ContiguousShape);
347         JumpList emitArrayStorageGetByVal(Instruction*, PatchableJump& badType);
348         JumpList emitIntTypedArrayGetByVal(Instruction*, PatchableJump& badType, TypedArrayType);
349         JumpList emitFloatTypedArrayGetByVal(Instruction*, PatchableJump& badType, TypedArrayType);
350         
351         // Property is in regT0, base is in regT0. regT2 contains indecing type.
352         // The value to store is not yet loaded. Property is int-checked and
353         // zero-extended. Base is cell checked. Structure is already profiled.
354         // returns the slow cases.
355         JumpList emitInt32PutByVal(Instruction* currentInstruction, PatchableJump& badType)
356         {
357             return emitGenericContiguousPutByVal(currentInstruction, badType, Int32Shape);
358         }
359         JumpList emitDoublePutByVal(Instruction* currentInstruction, PatchableJump& badType)
360         {
361             return emitGenericContiguousPutByVal(currentInstruction, badType, DoubleShape);
362         }
363         JumpList emitContiguousPutByVal(Instruction* currentInstruction, PatchableJump& badType)
364         {
365             return emitGenericContiguousPutByVal(currentInstruction, badType);
366         }
367         JumpList emitGenericContiguousPutByVal(Instruction*, PatchableJump& badType, IndexingType indexingShape = ContiguousShape);
368         JumpList emitArrayStoragePutByVal(Instruction*, PatchableJump& badType);
369         JumpList emitIntTypedArrayPutByVal(Instruction*, PatchableJump& badType, TypedArrayType);
370         JumpList emitFloatTypedArrayPutByVal(Instruction*, PatchableJump& badType, TypedArrayType);
371         
372         enum FinalObjectMode { MayBeFinal, KnownNotFinal };
373
374         template <typename T> Jump branchStructure(RelationalCondition, T leftHandSide, Structure*);
375
376 #if USE(JSVALUE32_64)
377         bool getOperandConstantImmediateInt(int op1, int op2, int& op, int32_t& constant);
378
379         void emitLoadTag(int index, RegisterID tag);
380         void emitLoadPayload(int index, RegisterID payload);
381
382         void emitLoad(const JSValue& v, RegisterID tag, RegisterID payload);
383         void emitLoad(int index, RegisterID tag, RegisterID payload, RegisterID base = callFrameRegister);
384         void emitLoad2(int index1, RegisterID tag1, RegisterID payload1, int index2, RegisterID tag2, RegisterID payload2);
385
386         void emitStore(int index, RegisterID tag, RegisterID payload, RegisterID base = callFrameRegister);
387         void emitStore(int index, const JSValue constant, RegisterID base = callFrameRegister);
388         void emitStoreInt32(int index, RegisterID payload, bool indexIsInt32 = false);
389         void emitStoreInt32(int index, TrustedImm32 payload, bool indexIsInt32 = false);
390         void emitStoreCell(int index, RegisterID payload, bool indexIsCell = false);
391         void emitStoreBool(int index, RegisterID payload, bool indexIsBool = false);
392         void emitStoreDouble(int index, FPRegisterID value);
393
394         void emitJumpSlowCaseIfNotJSCell(int virtualRegisterIndex);
395         void emitJumpSlowCaseIfNotJSCell(int virtualRegisterIndex, RegisterID tag);
396
397         void compileGetByIdHotPath(const Identifier*);
398         void compileGetDirectOffset(RegisterID base, RegisterID resultTag, RegisterID resultPayload, PropertyOffset cachedOffset);
399         void compileGetDirectOffset(JSObject* base, RegisterID resultTag, RegisterID resultPayload, PropertyOffset cachedOffset);
400         void compileGetDirectOffset(RegisterID base, RegisterID resultTag, RegisterID resultPayload, RegisterID offset, FinalObjectMode = MayBeFinal);
401         void compilePutDirectOffset(RegisterID base, RegisterID valueTag, RegisterID valuePayload, PropertyOffset cachedOffset);
402
403         // Arithmetic opcode helpers
404         void emitAdd32Constant(int dst, int op, int32_t constant, ResultType opType);
405         void emitSub32Constant(int dst, int op, int32_t constant, ResultType opType);
406         void emitBinaryDoubleOp(OpcodeID, int dst, int op1, int op2, OperandTypes, JumpList& notInt32Op1, JumpList& notInt32Op2, bool op1IsInRegisters = true, bool op2IsInRegisters = true);
407
408 #else // USE(JSVALUE32_64)
409         void emitGetVirtualRegister(int src, RegisterID dst);
410         void emitGetVirtualRegister(VirtualRegister src, RegisterID dst);
411         void emitGetVirtualRegisters(int src1, RegisterID dst1, int src2, RegisterID dst2);
412         void emitGetVirtualRegisters(VirtualRegister src1, RegisterID dst1, VirtualRegister src2, RegisterID dst2);
413         void emitPutVirtualRegister(int dst, RegisterID from = regT0);
414         void emitPutVirtualRegister(VirtualRegister dst, RegisterID from = regT0);
415         void emitStoreCell(int dst, RegisterID payload, bool /* only used in JSValue32_64 */ = false)
416         {
417             emitPutVirtualRegister(dst, payload);
418         }
419         void emitStoreCell(VirtualRegister dst, RegisterID payload)
420         {
421             emitPutVirtualRegister(dst, payload);
422         }
423
424         int32_t getConstantOperandImmediateInt(int src);
425
426         Jump emitJumpIfJSCell(RegisterID);
427         Jump emitJumpIfBothJSCells(RegisterID, RegisterID, RegisterID);
428         void emitJumpSlowCaseIfJSCell(RegisterID);
429         void emitJumpSlowCaseIfNotJSCell(RegisterID);
430         void emitJumpSlowCaseIfNotJSCell(RegisterID, int VReg);
431         Jump emitJumpIfImmediateInteger(RegisterID);
432         Jump emitJumpIfNotImmediateInteger(RegisterID);
433         Jump emitJumpIfNotImmediateIntegers(RegisterID, RegisterID, RegisterID);
434         void emitJumpSlowCaseIfNotImmediateInteger(RegisterID);
435         void emitJumpSlowCaseIfNotImmediateNumber(RegisterID);
436         void emitJumpSlowCaseIfNotImmediateIntegers(RegisterID, RegisterID, RegisterID);
437
438         void emitFastArithReTagImmediate(RegisterID src, RegisterID dest);
439
440         void emitTagAsBoolImmediate(RegisterID reg);
441         void compileBinaryArithOp(OpcodeID, int dst, int src1, int src2, OperandTypes opi);
442         void compileBinaryArithOpSlowCase(Instruction*, OpcodeID, Vector<SlowCaseEntry>::iterator&, int dst, int src1, int src2, OperandTypes, bool op1HasImmediateIntFastCase, bool op2HasImmediateIntFastCase);
443
444         void compileGetByIdHotPath(int baseVReg, const Identifier*);
445         void compileGetDirectOffset(RegisterID base, RegisterID result, PropertyOffset cachedOffset);
446         void compileGetDirectOffset(JSObject* base, RegisterID result, PropertyOffset cachedOffset);
447         void compileGetDirectOffset(RegisterID base, RegisterID result, RegisterID offset, RegisterID scratch, FinalObjectMode = MayBeFinal);
448         void compilePutDirectOffset(RegisterID base, RegisterID value, PropertyOffset cachedOffset);
449
450 #endif // USE(JSVALUE32_64)
451
452         void emit_compareAndJump(OpcodeID, int op1, int op2, unsigned target, RelationalCondition);
453         void emit_compareAndJumpSlow(int op1, int op2, unsigned target, DoubleCondition, size_t (JIT_OPERATION *operation)(ExecState*, EncodedJSValue, EncodedJSValue), bool invert, Vector<SlowCaseEntry>::iterator&);
454         
455         void assertStackPointerOffset();
456
457         void emit_op_touch_entry(Instruction*);
458         void emit_op_add(Instruction*);
459         void emit_op_bitand(Instruction*);
460         void emit_op_bitor(Instruction*);
461         void emit_op_bitxor(Instruction*);
462         void emit_op_call(Instruction*);
463         void emit_op_call_eval(Instruction*);
464         void emit_op_call_varargs(Instruction*);
465         void emit_op_construct_varargs(Instruction*);
466         void emit_op_captured_mov(Instruction*);
467         void emit_op_catch(Instruction*);
468         void emit_op_construct(Instruction*);
469         void emit_op_get_callee(Instruction*);
470         void emit_op_create_this(Instruction*);
471         void emit_op_to_this(Instruction*);
472         void emit_op_create_arguments(Instruction*);
473         void emit_op_debug(Instruction*);
474         void emit_op_del_by_id(Instruction*);
475         void emit_op_div(Instruction*);
476         void emit_op_end(Instruction*);
477         void emit_op_enter(Instruction*);
478         void emit_op_create_activation(Instruction*);
479         void emit_op_eq(Instruction*);
480         void emit_op_eq_null(Instruction*);
481         void emit_op_get_by_id(Instruction*);
482         void emit_op_get_arguments_length(Instruction*);
483         void emit_op_get_by_val(Instruction*);
484         void emit_op_get_argument_by_val(Instruction*);
485         void emit_op_init_lazy_reg(Instruction*);
486         void emit_op_check_has_instance(Instruction*);
487         void emit_op_instanceof(Instruction*);
488         void emit_op_is_undefined(Instruction*);
489         void emit_op_is_boolean(Instruction*);
490         void emit_op_is_number(Instruction*);
491         void emit_op_is_string(Instruction*);
492         void emit_op_jeq_null(Instruction*);
493         void emit_op_jfalse(Instruction*);
494         void emit_op_jmp(Instruction*);
495         void emit_op_jneq_null(Instruction*);
496         void emit_op_jneq_ptr(Instruction*);
497         void emit_op_jless(Instruction*);
498         void emit_op_jlesseq(Instruction*);
499         void emit_op_jgreater(Instruction*);
500         void emit_op_jgreatereq(Instruction*);
501         void emit_op_jnless(Instruction*);
502         void emit_op_jnlesseq(Instruction*);
503         void emit_op_jngreater(Instruction*);
504         void emit_op_jngreatereq(Instruction*);
505         void emit_op_jtrue(Instruction*);
506         void emit_op_loop_hint(Instruction*);
507         void emit_op_lshift(Instruction*);
508         void emit_op_mod(Instruction*);
509         void emit_op_mov(Instruction*);
510         void emit_op_mul(Instruction*);
511         void emit_op_negate(Instruction*);
512         void emit_op_neq(Instruction*);
513         void emit_op_neq_null(Instruction*);
514         void emit_op_new_array(Instruction*);
515         void emit_op_new_array_with_size(Instruction*);
516         void emit_op_new_array_buffer(Instruction*);
517         void emit_op_new_func(Instruction*);
518         void emit_op_new_captured_func(Instruction*);
519         void emit_op_new_func_exp(Instruction*);
520         void emit_op_new_object(Instruction*);
521         void emit_op_new_regexp(Instruction*);
522         void emit_op_not(Instruction*);
523         void emit_op_nstricteq(Instruction*);
524         void emit_op_pop_scope(Instruction*);
525         void emit_op_dec(Instruction*);
526         void emit_op_inc(Instruction*);
527         void emit_op_profile_did_call(Instruction*);
528         void emit_op_profile_will_call(Instruction*);
529         void emit_op_profile_type(Instruction*);
530         void emit_op_push_name_scope(Instruction*);
531         void emit_op_push_with_scope(Instruction*);
532         void emit_op_put_by_id(Instruction*);
533         void emit_op_put_by_index(Instruction*);
534         void emit_op_put_by_val(Instruction*);
535         void emit_op_put_getter_setter(Instruction*);
536         void emit_op_init_global_const(Instruction*);
537         void emit_op_ret(Instruction*);
538         void emit_op_ret_object_or_this(Instruction*);
539         void emit_op_rshift(Instruction*);
540         void emit_op_strcat(Instruction*);
541         void emit_op_stricteq(Instruction*);
542         void emit_op_sub(Instruction*);
543         void emit_op_switch_char(Instruction*);
544         void emit_op_switch_imm(Instruction*);
545         void emit_op_switch_string(Instruction*);
546         void emit_op_tear_off_activation(Instruction*);
547         void emit_op_tear_off_arguments(Instruction*);
548         void emit_op_throw(Instruction*);
549         void emit_op_throw_static_error(Instruction*);
550         void emit_op_to_number(Instruction*);
551         void emit_op_to_primitive(Instruction*);
552         void emit_op_unexpected_load(Instruction*);
553         void emit_op_unsigned(Instruction*);
554         void emit_op_urshift(Instruction*);
555         void emit_op_get_enumerable_length(Instruction*);
556         void emit_op_has_generic_property(Instruction*);
557         void emit_op_has_structure_property(Instruction*);
558         void emit_op_has_indexed_property(Instruction*);
559         void emit_op_get_direct_pname(Instruction*);
560         void emit_op_get_structure_property_enumerator(Instruction*);
561         void emit_op_get_generic_property_enumerator(Instruction*);
562         void emit_op_next_enumerator_pname(Instruction*);
563         void emit_op_to_index_string(Instruction*);
564
565         void emitSlow_op_add(Instruction*, Vector<SlowCaseEntry>::iterator&);
566         void emitSlow_op_bitand(Instruction*, Vector<SlowCaseEntry>::iterator&);
567         void emitSlow_op_bitor(Instruction*, Vector<SlowCaseEntry>::iterator&);
568         void emitSlow_op_bitxor(Instruction*, Vector<SlowCaseEntry>::iterator&);
569         void emitSlow_op_call(Instruction*, Vector<SlowCaseEntry>::iterator&);
570         void emitSlow_op_call_eval(Instruction*, Vector<SlowCaseEntry>::iterator&);
571         void emitSlow_op_call_varargs(Instruction*, Vector<SlowCaseEntry>::iterator&);
572         void emitSlow_op_construct_varargs(Instruction*, Vector<SlowCaseEntry>::iterator&);
573         void emitSlow_op_captured_mov(Instruction*, Vector<SlowCaseEntry>::iterator&);
574         void emitSlow_op_construct(Instruction*, Vector<SlowCaseEntry>::iterator&);
575         void emitSlow_op_to_this(Instruction*, Vector<SlowCaseEntry>::iterator&);
576         void emitSlow_op_create_this(Instruction*, Vector<SlowCaseEntry>::iterator&);
577         void emitSlow_op_div(Instruction*, Vector<SlowCaseEntry>::iterator&);
578         void emitSlow_op_eq(Instruction*, Vector<SlowCaseEntry>::iterator&);
579         void emitSlow_op_get_callee(Instruction*, Vector<SlowCaseEntry>::iterator&);
580         void emitSlow_op_get_by_id(Instruction*, Vector<SlowCaseEntry>::iterator&);
581         void emitSlow_op_get_arguments_length(Instruction*, Vector<SlowCaseEntry>::iterator&);
582         void emitSlow_op_get_by_val(Instruction*, Vector<SlowCaseEntry>::iterator&);
583         void emitSlow_op_get_argument_by_val(Instruction*, Vector<SlowCaseEntry>::iterator&);
584         void emitSlow_op_check_has_instance(Instruction*, Vector<SlowCaseEntry>::iterator&);
585         void emitSlow_op_instanceof(Instruction*, Vector<SlowCaseEntry>::iterator&);
586         void emitSlow_op_jfalse(Instruction*, Vector<SlowCaseEntry>::iterator&);
587         void emitSlow_op_jless(Instruction*, Vector<SlowCaseEntry>::iterator&);
588         void emitSlow_op_jlesseq(Instruction*, Vector<SlowCaseEntry>::iterator&);
589         void emitSlow_op_jgreater(Instruction*, Vector<SlowCaseEntry>::iterator&);
590         void emitSlow_op_jgreatereq(Instruction*, Vector<SlowCaseEntry>::iterator&);
591         void emitSlow_op_jnless(Instruction*, Vector<SlowCaseEntry>::iterator&);
592         void emitSlow_op_jnlesseq(Instruction*, Vector<SlowCaseEntry>::iterator&);
593         void emitSlow_op_jngreater(Instruction*, Vector<SlowCaseEntry>::iterator&);
594         void emitSlow_op_jngreatereq(Instruction*, Vector<SlowCaseEntry>::iterator&);
595         void emitSlow_op_jtrue(Instruction*, Vector<SlowCaseEntry>::iterator&);
596         void emitSlow_op_loop_hint(Instruction*, Vector<SlowCaseEntry>::iterator&);
597         void emitSlow_op_lshift(Instruction*, Vector<SlowCaseEntry>::iterator&);
598         void emitSlow_op_mod(Instruction*, Vector<SlowCaseEntry>::iterator&);
599         void emitSlow_op_mul(Instruction*, Vector<SlowCaseEntry>::iterator&);
600         void emitSlow_op_negate(Instruction*, Vector<SlowCaseEntry>::iterator&);
601         void emitSlow_op_neq(Instruction*, Vector<SlowCaseEntry>::iterator&);
602         void emitSlow_op_new_object(Instruction*, Vector<SlowCaseEntry>::iterator&);
603         void emitSlow_op_not(Instruction*, Vector<SlowCaseEntry>::iterator&);
604         void emitSlow_op_nstricteq(Instruction*, Vector<SlowCaseEntry>::iterator&);
605         void emitSlow_op_dec(Instruction*, Vector<SlowCaseEntry>::iterator&);
606         void emitSlow_op_inc(Instruction*, Vector<SlowCaseEntry>::iterator&);
607         void emitSlow_op_put_by_id(Instruction*, Vector<SlowCaseEntry>::iterator&);
608         void emitSlow_op_put_by_val(Instruction*, Vector<SlowCaseEntry>::iterator&);
609         void emitSlow_op_rshift(Instruction*, Vector<SlowCaseEntry>::iterator&);
610         void emitSlow_op_stricteq(Instruction*, Vector<SlowCaseEntry>::iterator&);
611         void emitSlow_op_sub(Instruction*, Vector<SlowCaseEntry>::iterator&);
612         void emitSlow_op_to_number(Instruction*, Vector<SlowCaseEntry>::iterator&);
613         void emitSlow_op_to_primitive(Instruction*, Vector<SlowCaseEntry>::iterator&);
614         void emitSlow_op_unsigned(Instruction*, Vector<SlowCaseEntry>::iterator&);
615         void emitSlow_op_urshift(Instruction*, Vector<SlowCaseEntry>::iterator&);
616         void emitSlow_op_has_indexed_property(Instruction*, Vector<SlowCaseEntry>::iterator&);
617         void emitSlow_op_has_structure_property(Instruction*, Vector<SlowCaseEntry>::iterator&);
618         void emitSlow_op_get_direct_pname(Instruction*, Vector<SlowCaseEntry>::iterator&);
619
620         void emit_op_resolve_scope(Instruction*);
621         void emit_op_get_from_scope(Instruction*);
622         void emit_op_put_to_scope(Instruction*);
623         void emitSlow_op_resolve_scope(Instruction*, Vector<SlowCaseEntry>::iterator&);
624         void emitSlow_op_get_from_scope(Instruction*, Vector<SlowCaseEntry>::iterator&);
625         void emitSlow_op_put_to_scope(Instruction*, Vector<SlowCaseEntry>::iterator&);
626
627         void emitRightShift(Instruction*, bool isUnsigned);
628         void emitRightShiftSlowCase(Instruction*, Vector<SlowCaseEntry>::iterator&, bool isUnsigned);
629
630         void emitVarInjectionCheck(bool needsVarInjectionChecks);
631         void emitResolveClosure(int dst, bool needsVarInjectionChecks, unsigned depth);
632         void emitLoadWithStructureCheck(int scope, Structure** structureSlot);
633         void emitGetGlobalProperty(uintptr_t* operandSlot);
634         void emitGetGlobalVar(uintptr_t operand);
635         void emitGetClosureVar(int scope, uintptr_t operand);
636         void emitPutGlobalProperty(uintptr_t* operandSlot, int value);
637 #if USE(JSVALUE64)
638         void emitNotifyWrite(RegisterID value, RegisterID scratch, VariableWatchpointSet*);
639 #else
640         void emitNotifyWrite(RegisterID tag, RegisterID payload, RegisterID scratch, VariableWatchpointSet*);
641 #endif
642         void emitPutGlobalVar(uintptr_t operand, int value, VariableWatchpointSet*);
643         void emitPutClosureVar(int scope, uintptr_t operand, int value);
644
645         void emitInitRegister(int dst);
646
647         void emitPutIntToCallFrameHeader(RegisterID from, JSStack::CallFrameHeaderEntry);
648         void emitGetFromCallFrameHeaderPtr(JSStack::CallFrameHeaderEntry, RegisterID to, RegisterID from = callFrameRegister);
649         void emitGetFromCallFrameHeader32(JSStack::CallFrameHeaderEntry, RegisterID to, RegisterID from = callFrameRegister);
650 #if USE(JSVALUE64)
651         void emitGetFromCallFrameHeader64(JSStack::CallFrameHeaderEntry, RegisterID to, RegisterID from = callFrameRegister);
652 #endif
653
654         JSValue getConstantOperand(int src);
655         bool isOperandConstantImmediateInt(int src);
656         bool isOperandConstantImmediateChar(int src);
657
658         Jump getSlowCase(Vector<SlowCaseEntry>::iterator& iter)
659         {
660             return iter++->from;
661         }
662         void linkSlowCase(Vector<SlowCaseEntry>::iterator& iter)
663         {
664             iter->from.link(this);
665             ++iter;
666         }
667         void linkDummySlowCase(Vector<SlowCaseEntry>::iterator& iter)
668         {
669             ASSERT(!iter->from.isSet());
670             ++iter;
671         }
672         void linkSlowCaseIfNotJSCell(Vector<SlowCaseEntry>::iterator&, int virtualRegisterIndex);
673
674         MacroAssembler::Call appendCallWithExceptionCheck(const FunctionPtr&);
675 #if OS(WINDOWS) && CPU(X86_64)
676         MacroAssembler::Call appendCallWithExceptionCheckAndSlowPathReturnType(const FunctionPtr&);
677 #endif
678         MacroAssembler::Call appendCallWithCallFrameRollbackOnException(const FunctionPtr&);
679         MacroAssembler::Call appendCallWithExceptionCheckSetJSValueResult(const FunctionPtr&, int);
680         MacroAssembler::Call appendCallWithExceptionCheckSetJSValueResultWithProfile(const FunctionPtr&, int);
681         
682         enum WithProfileTag { WithProfile };
683         
684         MacroAssembler::Call callOperation(C_JITOperation_E);
685         MacroAssembler::Call callOperation(C_JITOperation_EO, GPRReg);
686         MacroAssembler::Call callOperation(C_JITOperation_ESt, Structure*);
687         MacroAssembler::Call callOperation(C_JITOperation_EZ, int32_t);
688         MacroAssembler::Call callOperation(F_JITOperation_EJZZ, GPRReg, int32_t, int32_t);
689         MacroAssembler::Call callOperation(J_JITOperation_E, int);
690         MacroAssembler::Call callOperation(J_JITOperation_EAapJ, int, ArrayAllocationProfile*, GPRReg);
691         MacroAssembler::Call callOperation(J_JITOperation_EAapJcpZ, int, ArrayAllocationProfile*, GPRReg, int32_t);
692         MacroAssembler::Call callOperation(J_JITOperation_EAapJcpZ, int, ArrayAllocationProfile*, const JSValue*, int32_t);
693         MacroAssembler::Call callOperation(J_JITOperation_EC, int, JSCell*);
694         MacroAssembler::Call callOperation(V_JITOperation_EC, JSCell*);
695         MacroAssembler::Call callOperation(J_JITOperation_EJ, int, GPRReg);
696 #if USE(JSVALUE64)
697         MacroAssembler::Call callOperation(WithProfileTag, J_JITOperation_ESsiJI, int, StructureStubInfo*, GPRReg, StringImpl*);
698 #else
699         MacroAssembler::Call callOperation(WithProfileTag, J_JITOperation_ESsiJI, int, StructureStubInfo*, GPRReg, GPRReg, StringImpl*);
700 #endif
701         MacroAssembler::Call callOperation(J_JITOperation_EJIdc, int, GPRReg, const Identifier*);
702         MacroAssembler::Call callOperation(J_JITOperation_EJJ, int, GPRReg, GPRReg);
703 #if USE(JSVALUE64)
704         MacroAssembler::Call callOperation(WithProfileTag, J_JITOperation_EJJ, int, GPRReg, GPRReg);
705 #else
706         MacroAssembler::Call callOperation(WithProfileTag, J_JITOperation_EJJ, int, GPRReg, GPRReg, GPRReg, GPRReg);
707 #endif
708         MacroAssembler::Call callOperation(J_JITOperation_EP, int, void*);
709         MacroAssembler::Call callOperation(WithProfileTag, J_JITOperation_EPc, int, Instruction*);
710         MacroAssembler::Call callOperation(J_JITOperation_EZ, int, int32_t);
711         MacroAssembler::Call callOperation(P_JITOperation_EJS, GPRReg, size_t);
712         MacroAssembler::Call callOperation(S_JITOperation_ECC, RegisterID, RegisterID);
713         MacroAssembler::Call callOperation(S_JITOperation_EJ, RegisterID);
714         MacroAssembler::Call callOperation(S_JITOperation_EJJ, RegisterID, RegisterID);
715         MacroAssembler::Call callOperation(S_JITOperation_EOJss, RegisterID, RegisterID);
716         MacroAssembler::Call callOperation(Sprt_JITOperation_EZ, int32_t);
717         MacroAssembler::Call callOperation(V_JITOperation_E);
718         MacroAssembler::Call callOperation(V_JITOperation_EC, RegisterID);
719         MacroAssembler::Call callOperation(V_JITOperation_ECC, RegisterID, RegisterID);
720         MacroAssembler::Call callOperation(V_JITOperation_ECICC, RegisterID, const Identifier*, RegisterID, RegisterID);
721         MacroAssembler::Call callOperation(J_JITOperation_EE, RegisterID);
722         MacroAssembler::Call callOperation(V_JITOperation_EIdJZ, const Identifier*, RegisterID, int32_t);
723         MacroAssembler::Call callOperation(V_JITOperation_EJ, RegisterID);
724 #if USE(JSVALUE64)
725         MacroAssembler::Call callOperationNoExceptionCheck(V_JITOperation_EJ, RegisterID);
726 #else
727         MacroAssembler::Call callOperationNoExceptionCheck(V_JITOperation_EJ, RegisterID, RegisterID);
728 #endif
729         MacroAssembler::Call callOperation(V_JITOperation_EJIdJJ, RegisterID, const Identifier*, RegisterID, RegisterID);
730 #if USE(JSVALUE64)
731         MacroAssembler::Call callOperation(F_JITOperation_EFJJZ, RegisterID, RegisterID, RegisterID, int32_t);
732         MacroAssembler::Call callOperation(V_JITOperation_ESsiJJI, StructureStubInfo*, RegisterID, RegisterID, StringImpl*);
733 #else
734         MacroAssembler::Call callOperation(V_JITOperation_ESsiJJI, StructureStubInfo*, RegisterID, RegisterID, RegisterID, RegisterID, StringImpl*);
735 #endif
736         MacroAssembler::Call callOperation(V_JITOperation_EJJJ, RegisterID, RegisterID, RegisterID);
737         MacroAssembler::Call callOperation(V_JITOperation_EJZJ, RegisterID, int32_t, RegisterID);
738         MacroAssembler::Call callOperation(V_JITOperation_EJZ, RegisterID, int32_t);
739         MacroAssembler::Call callOperation(V_JITOperation_EPc, Instruction*);
740         MacroAssembler::Call callOperation(V_JITOperation_EZ, int32_t);
741         MacroAssembler::Call callOperationWithCallFrameRollbackOnException(J_JITOperation_E);
742         MacroAssembler::Call callOperationWithCallFrameRollbackOnException(V_JITOperation_ECb, CodeBlock*);
743         MacroAssembler::Call callOperationWithCallFrameRollbackOnException(Z_JITOperation_E);
744 #if USE(JSVALUE32_64)
745         MacroAssembler::Call callOperation(F_JITOperation_EFJJZ, RegisterID, RegisterID, RegisterID, RegisterID, RegisterID, int32_t);
746         MacroAssembler::Call callOperation(F_JITOperation_EJZZ, GPRReg, GPRReg, int32_t, int32_t);
747         MacroAssembler::Call callOperation(J_JITOperation_EAapJ, int, ArrayAllocationProfile*, GPRReg, GPRReg);
748         MacroAssembler::Call callOperation(J_JITOperation_EJ, int, GPRReg, GPRReg);
749         MacroAssembler::Call callOperation(J_JITOperation_EJIdc, int, GPRReg, GPRReg, const Identifier*);
750         MacroAssembler::Call callOperation(J_JITOperation_EJJ, int, GPRReg, GPRReg, GPRReg, GPRReg);
751         MacroAssembler::Call callOperation(P_JITOperation_EJS, GPRReg, GPRReg, size_t);
752         MacroAssembler::Call callOperation(S_JITOperation_EJ, RegisterID, RegisterID);
753         MacroAssembler::Call callOperation(S_JITOperation_EJJ, RegisterID, RegisterID, RegisterID, RegisterID);
754         MacroAssembler::Call callOperation(V_JITOperation_EIdJZ, const Identifier*, RegisterID, RegisterID, int32_t);
755         MacroAssembler::Call callOperation(V_JITOperation_EJ, RegisterID, RegisterID);
756         MacroAssembler::Call callOperation(V_JITOperation_EJJJ, RegisterID, RegisterID, RegisterID, RegisterID, RegisterID, RegisterID);
757         MacroAssembler::Call callOperation(V_JITOperation_EJZ, RegisterID, RegisterID, int32_t);
758         MacroAssembler::Call callOperation(V_JITOperation_EJZJ, RegisterID, RegisterID, int32_t, RegisterID, RegisterID);
759 #endif
760
761         Jump checkStructure(RegisterID reg, Structure* structure);
762
763         void updateTopCallFrame();
764
765         Call emitNakedCall(CodePtr function = CodePtr());
766
767         // Loads the character value of a single character string into dst.
768         void emitLoadCharacterString(RegisterID src, RegisterID dst, JumpList& failures);
769         
770 #if ENABLE(DFG_JIT)
771         void emitEnterOptimizationCheck();
772 #else
773         void emitEnterOptimizationCheck() { }
774 #endif
775
776 #ifndef NDEBUG
777         void printBytecodeOperandTypes(int src1, int src2);
778 #endif
779
780 #if ENABLE(SAMPLING_FLAGS)
781         void setSamplingFlag(int32_t);
782         void clearSamplingFlag(int32_t);
783 #endif
784
785 #if ENABLE(SAMPLING_COUNTERS)
786         void emitCount(AbstractSamplingCounter&, int32_t = 1);
787 #endif
788
789 #if ENABLE(OPCODE_SAMPLING)
790         void sampleInstruction(Instruction*, bool = false);
791 #endif
792
793 #if ENABLE(CODEBLOCK_SAMPLING)
794         void sampleCodeBlock(CodeBlock*);
795 #else
796         void sampleCodeBlock(CodeBlock*) {}
797 #endif
798
799 #if ENABLE(DFG_JIT)
800         bool canBeOptimized() { return m_canBeOptimized; }
801         bool canBeOptimizedOrInlined() { return m_canBeOptimizedOrInlined; }
802         bool shouldEmitProfiling() { return m_shouldEmitProfiling; }
803 #else
804         bool canBeOptimized() { return false; }
805         bool canBeOptimizedOrInlined() { return false; }
806         // Enables use of value profiler with tiered compilation turned off,
807         // in which case all code gets profiled.
808         bool shouldEmitProfiling() { return false; }
809 #endif
810
811         Interpreter* m_interpreter;
812
813         Vector<CallRecord> m_calls;
814         Vector<Label> m_labels;
815         Vector<JITGetByIdGenerator> m_getByIds;
816         Vector<JITPutByIdGenerator> m_putByIds;
817         Vector<ByValCompilationInfo> m_byValCompilationInfo;
818         Vector<CallCompilationInfo> m_callCompilationInfo;
819         Vector<JumpTable> m_jmpTable;
820
821         unsigned m_bytecodeOffset;
822         Vector<SlowCaseEntry> m_slowCases;
823         Vector<SwitchRecord> m_switches;
824
825         JumpList m_exceptionChecks;
826         JumpList m_exceptionChecksWithCallFrameRollback;
827
828         unsigned m_getByIdIndex;
829         unsigned m_putByIdIndex;
830         unsigned m_byValInstructionIndex;
831         unsigned m_callLinkInfoIndex;
832
833         OwnPtr<JITDisassembler> m_disassembler;
834         RefPtr<Profiler::Compilation> m_compilation;
835         WeakRandom m_randomGenerator;
836         static CodeRef stringGetByValStubGenerator(VM*);
837
838         bool m_canBeOptimized;
839         bool m_canBeOptimizedOrInlined;
840         bool m_shouldEmitProfiling;
841     } JIT_CLASS_ALIGNMENT;
842
843 } // namespace JSC
844
845 #endif // ENABLE(JIT)
846
847 #endif // JIT_h