1/*
2 * Copyright (C) 2010, 2016 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "BytecodeConventions.h"
29#include "CCallHelpers.h"
30#include "FPRInfo.h"
31#include "GPRInfo.h"
32#include "JSCJSValue.h"
33#include "JSString.h"
34#include "MacroAssembler.h"
35
36#if ENABLE(JIT)
37
38namespace JSC {
39 class JSInterfaceJIT : public CCallHelpers, public GPRInfo, public FPRInfo {
40 public:
41
42 JSInterfaceJIT(VM* vm = nullptr, CodeBlock* codeBlock = nullptr)
43 : CCallHelpers(codeBlock)
44 , m_vm(vm)
45 {
46 }
47
48 inline Jump emitLoadJSCell(unsigned virtualRegisterIndex, RegisterID payload);
49 inline Jump emitLoadInt32(unsigned virtualRegisterIndex, RegisterID dst);
50 inline Jump emitLoadDouble(unsigned virtualRegisterIndex, FPRegisterID dst, RegisterID scratch);
51
52#if USE(JSVALUE32_64)
53 inline Jump emitJumpIfNotJSCell(unsigned virtualRegisterIndex);
54#endif
55
56 void emitGetFromCallFrameHeaderPtr(int entry, RegisterID to, RegisterID from = callFrameRegister);
57 void emitPutToCallFrameHeader(RegisterID from, int entry);
58 void emitPutToCallFrameHeader(void* value, int entry);
59 void emitPutCellToCallFrameHeader(RegisterID from, int entry);
60
61 VM* vm() const { return m_vm; }
62
63 VM* m_vm;
64 };
65
66#if USE(JSVALUE32_64)
67 inline JSInterfaceJIT::Jump JSInterfaceJIT::emitLoadJSCell(unsigned virtualRegisterIndex, RegisterID payload)
68 {
69 loadPtr(payloadFor(virtualRegisterIndex), payload);
70 return emitJumpIfNotJSCell(virtualRegisterIndex);
71 }
72
73 inline JSInterfaceJIT::Jump JSInterfaceJIT::emitJumpIfNotJSCell(unsigned virtualRegisterIndex)
74 {
75 ASSERT(static_cast<int>(virtualRegisterIndex) < FirstConstantRegisterIndex);
76 return branch32(NotEqual, tagFor(virtualRegisterIndex), TrustedImm32(JSValue::CellTag));
77 }
78
79 inline JSInterfaceJIT::Jump JSInterfaceJIT::emitLoadInt32(unsigned virtualRegisterIndex, RegisterID dst)
80 {
81 ASSERT(static_cast<int>(virtualRegisterIndex) < FirstConstantRegisterIndex);
82 loadPtr(payloadFor(virtualRegisterIndex), dst);
83 return branch32(NotEqual, tagFor(static_cast<int>(virtualRegisterIndex)), TrustedImm32(JSValue::Int32Tag));
84 }
85
86 inline JSInterfaceJIT::Jump JSInterfaceJIT::emitLoadDouble(unsigned virtualRegisterIndex, FPRegisterID dst, RegisterID scratch)
87 {
88 ASSERT(static_cast<int>(virtualRegisterIndex) < FirstConstantRegisterIndex);
89 loadPtr(tagFor(virtualRegisterIndex), scratch);
90 Jump isDouble = branch32(Below, scratch, TrustedImm32(JSValue::LowestTag));
91 Jump notInt = branch32(NotEqual, scratch, TrustedImm32(JSValue::Int32Tag));
92 loadPtr(payloadFor(virtualRegisterIndex), scratch);
93 convertInt32ToDouble(scratch, dst);
94 Jump done = jump();
95 isDouble.link(this);
96 loadDouble(addressFor(virtualRegisterIndex), dst);
97 done.link(this);
98 return notInt;
99 }
100
101#endif
102
103#if USE(JSVALUE64)
104 inline JSInterfaceJIT::Jump JSInterfaceJIT::emitLoadJSCell(unsigned virtualRegisterIndex, RegisterID dst)
105 {
106 load64(addressFor(virtualRegisterIndex), dst);
107 return branchIfNotCell(dst);
108 }
109
110 inline JSInterfaceJIT::Jump JSInterfaceJIT::emitLoadInt32(unsigned virtualRegisterIndex, RegisterID dst)
111 {
112 load64(addressFor(virtualRegisterIndex), dst);
113 Jump notInt32 = branchIfNotInt32(dst);
114 zeroExtend32ToPtr(dst, dst);
115 return notInt32;
116 }
117
118 inline JSInterfaceJIT::Jump JSInterfaceJIT::emitLoadDouble(unsigned virtualRegisterIndex, FPRegisterID dst, RegisterID scratch)
119 {
120 load64(addressFor(virtualRegisterIndex), scratch);
121 Jump notNumber = branchIfNotNumber(scratch);
122 Jump notInt = branchIfNotInt32(scratch);
123 convertInt32ToDouble(scratch, dst);
124 Jump done = jump();
125 notInt.link(this);
126 unboxDouble(scratch, scratch, dst);
127 done.link(this);
128 return notNumber;
129 }
130#endif
131
132 ALWAYS_INLINE void JSInterfaceJIT::emitGetFromCallFrameHeaderPtr(int entry, RegisterID to, RegisterID from)
133 {
134 loadPtr(Address(from, entry * sizeof(Register)), to);
135 }
136
137 ALWAYS_INLINE void JSInterfaceJIT::emitPutToCallFrameHeader(RegisterID from, int entry)
138 {
139#if USE(JSVALUE32_64)
140 storePtr(from, payloadFor(entry));
141#else
142 store64(from, addressFor(entry));
143#endif
144 }
145
146 ALWAYS_INLINE void JSInterfaceJIT::emitPutToCallFrameHeader(void* value, int entry)
147 {
148 storePtr(TrustedImmPtr(value), addressFor(entry));
149 }
150
151 ALWAYS_INLINE void JSInterfaceJIT::emitPutCellToCallFrameHeader(RegisterID from, int entry)
152 {
153#if USE(JSVALUE32_64)
154 store32(TrustedImm32(JSValue::CellTag), tagFor(entry));
155 store32(from, payloadFor(entry));
156#else
157 store64(from, addressFor(entry));
158#endif
159 }
160
161} // namespace JSC
162
163#endif // ENABLE(JIT)
164