b9271005f13b01132d0510f0411b00ed0eadae99
[WebKit-https.git] / Source / JavaScriptCore / assembler / MacroAssemblerX86.h
1 /*
2  * Copyright (C) 2008, 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 #pragma once
27
28 #if ENABLE(ASSEMBLER) && CPU(X86)
29
30 #include "MacroAssemblerX86Common.h"
31
32 namespace JSC {
33
34 class MacroAssemblerX86 : public MacroAssemblerX86Common {
35 public:
36     static const unsigned numGPRs = 8;
37     static const unsigned numFPRs = 8;
38     
39     static const Scale ScalePtr = TimesFour;
40
41     using MacroAssemblerX86Common::add32;
42     using MacroAssemblerX86Common::and32;
43     using MacroAssemblerX86Common::branchAdd32;
44     using MacroAssemblerX86Common::branchSub32;
45     using MacroAssemblerX86Common::sub32;
46     using MacroAssemblerX86Common::or32;
47     using MacroAssemblerX86Common::load32;
48     using MacroAssemblerX86Common::load8;
49     using MacroAssemblerX86Common::store32;
50     using MacroAssemblerX86Common::store8;
51     using MacroAssemblerX86Common::branch32;
52     using MacroAssemblerX86Common::call;
53     using MacroAssemblerX86Common::jump;
54     using MacroAssemblerX86Common::addDouble;
55     using MacroAssemblerX86Common::loadDouble;
56     using MacroAssemblerX86Common::storeDouble;
57     using MacroAssemblerX86Common::convertInt32ToDouble;
58     using MacroAssemblerX86Common::branch8;
59     using MacroAssemblerX86Common::branchTest8;
60
61     void add32(TrustedImm32 imm, RegisterID src, RegisterID dest)
62     {
63         m_assembler.leal_mr(imm.m_value, src, dest);
64     }
65
66     void add32(TrustedImm32 imm, AbsoluteAddress address)
67     {
68         m_assembler.addl_im(imm.m_value, address.m_ptr);
69     }
70     
71     void add32(AbsoluteAddress address, RegisterID dest)
72     {
73         m_assembler.addl_mr(address.m_ptr, dest);
74     }
75     
76     void add64(TrustedImm32 imm, AbsoluteAddress address)
77     {
78         m_assembler.addl_im(imm.m_value, address.m_ptr);
79         m_assembler.adcl_im(imm.m_value >> 31, reinterpret_cast<const char*>(address.m_ptr) + sizeof(int32_t));
80     }
81
82     void getEffectiveAddress(BaseIndex address, RegisterID dest)
83     {
84         return x86Lea32(address, dest);
85     }
86
87     void and32(TrustedImm32 imm, AbsoluteAddress address)
88     {
89         m_assembler.andl_im(imm.m_value, address.m_ptr);
90     }
91     
92     void or32(TrustedImm32 imm, AbsoluteAddress address)
93     {
94         m_assembler.orl_im(imm.m_value, address.m_ptr);
95     }
96     
97     void or32(RegisterID reg, AbsoluteAddress address)
98     {
99         m_assembler.orl_rm(reg, address.m_ptr);
100     }
101     
102     void sub32(TrustedImm32 imm, AbsoluteAddress address)
103     {
104         m_assembler.subl_im(imm.m_value, address.m_ptr);
105     }
106
107     void load32(const void* address, RegisterID dest)
108     {
109         m_assembler.movl_mr(address, dest);
110     }
111     
112     void load8(const void* address, RegisterID dest)
113     {
114         m_assembler.movzbl_mr(address, dest);
115     }
116
117     void abortWithReason(AbortReason reason)
118     {
119         move(TrustedImm32(reason), X86Registers::eax);
120         breakpoint();
121     }
122
123     void abortWithReason(AbortReason reason, intptr_t misc)
124     {
125         move(TrustedImm32(misc), X86Registers::edx);
126         abortWithReason(reason);
127     }
128
129     ConvertibleLoadLabel convertibleLoadPtr(Address address, RegisterID dest)
130     {
131         ConvertibleLoadLabel result = ConvertibleLoadLabel(this);
132         m_assembler.movl_mr(address.offset, address.base, dest);
133         return result;
134     }
135
136     void addDouble(AbsoluteAddress address, FPRegisterID dest)
137     {
138         m_assembler.addsd_mr(address.m_ptr, dest);
139     }
140
141     void storeDouble(FPRegisterID src, TrustedImmPtr address)
142     {
143         ASSERT(isSSE2Present());
144         ASSERT(address.m_value);
145         m_assembler.movsd_rm(src, address.asPtr());
146     }
147
148     void convertInt32ToDouble(AbsoluteAddress src, FPRegisterID dest)
149     {
150         m_assembler.cvtsi2sd_mr(src.m_ptr, dest);
151     }
152
153     void store32(TrustedImm32 imm, void* address)
154     {
155         m_assembler.movl_i32m(imm.m_value, address);
156     }
157
158     void store32(RegisterID src, void* address)
159     {
160         m_assembler.movl_rm(src, address);
161     }
162     
163     void store8(RegisterID src, void* address)
164     {
165         m_assembler.movb_rm(src, address);
166     }
167
168     void store8(TrustedImm32 imm, void* address)
169     {
170         TrustedImm32 imm8(static_cast<int8_t>(imm.m_value));
171         m_assembler.movb_i8m(imm8.m_value, address);
172     }
173     
174     void moveDoubleToInts(FPRegisterID src, RegisterID dest1, RegisterID dest2)
175     {
176         ASSERT(isSSE2Present());
177         m_assembler.pextrw_irr(3, src, dest1);
178         m_assembler.pextrw_irr(2, src, dest2);
179         lshift32(TrustedImm32(16), dest1);
180         or32(dest1, dest2);
181         moveFloatTo32(src, dest1);
182     }
183
184     void moveIntsToDouble(RegisterID src1, RegisterID src2, FPRegisterID dest, FPRegisterID scratch)
185     {
186         move32ToFloat(src1, dest);
187         move32ToFloat(src2, scratch);
188         lshiftPacked(TrustedImm32(32), scratch);
189         orPacked(scratch, dest);
190     }
191
192     Jump branchAdd32(ResultCondition cond, TrustedImm32 imm, AbsoluteAddress dest)
193     {
194         m_assembler.addl_im(imm.m_value, dest.m_ptr);
195         return Jump(m_assembler.jCC(x86Condition(cond)));
196     }
197
198     Jump branchSub32(ResultCondition cond, TrustedImm32 imm, AbsoluteAddress dest)
199     {
200         m_assembler.subl_im(imm.m_value, dest.m_ptr);
201         return Jump(m_assembler.jCC(x86Condition(cond)));
202     }
203
204     Jump branch32(RelationalCondition cond, AbsoluteAddress left, RegisterID right)
205     {
206         m_assembler.cmpl_rm(right, left.m_ptr);
207         return Jump(m_assembler.jCC(x86Condition(cond)));
208     }
209
210     Jump branch32(RelationalCondition cond, AbsoluteAddress left, TrustedImm32 right)
211     {
212         m_assembler.cmpl_im(right.m_value, left.m_ptr);
213         return Jump(m_assembler.jCC(x86Condition(cond)));
214     }
215
216     Call call()
217     {
218         return Call(m_assembler.call(), Call::Linkable);
219     }
220
221     // Address is a memory location containing the address to jump to
222     void jump(AbsoluteAddress address)
223     {
224         m_assembler.jmp_m(address.m_ptr);
225     }
226
227     Call tailRecursiveCall()
228     {
229         return Call::fromTailJump(jump());
230     }
231
232     Call makeTailRecursiveCall(Jump oldJump)
233     {
234         return Call::fromTailJump(oldJump);
235     }
236
237
238     DataLabelPtr moveWithPatch(TrustedImmPtr initialValue, RegisterID dest)
239     {
240         padBeforePatch();
241         m_assembler.movl_i32r(initialValue.asIntptr(), dest);
242         return DataLabelPtr(this);
243     }
244     
245     Jump branch8(RelationalCondition cond, AbsoluteAddress left, TrustedImm32 right)
246     {
247         TrustedImm32 right8(static_cast<int8_t>(right.m_value));
248         m_assembler.cmpb_im(right8.m_value, left.m_ptr);
249         return Jump(m_assembler.jCC(x86Condition(cond)));
250     }
251
252     Jump branchTest8(ResultCondition cond, AbsoluteAddress address, TrustedImm32 mask = TrustedImm32(-1))
253     {
254         TrustedImm32 mask8(static_cast<int8_t>(mask.m_value));
255         if (mask8.m_value == -1)
256             m_assembler.cmpb_im(0, address.m_ptr);
257         else
258             m_assembler.testb_im(mask8.m_value, address.m_ptr);
259         return Jump(m_assembler.jCC(x86Condition(cond)));
260     }
261
262     Jump branchPtrWithPatch(RelationalCondition cond, RegisterID left, DataLabelPtr& dataLabel, TrustedImmPtr initialRightValue = TrustedImmPtr(nullptr))
263     {
264         padBeforePatch();
265         m_assembler.cmpl_ir_force32(initialRightValue.asIntptr(), left);
266         dataLabel = DataLabelPtr(this);
267         return Jump(m_assembler.jCC(x86Condition(cond)));
268     }
269
270     Jump branchPtrWithPatch(RelationalCondition cond, Address left, DataLabelPtr& dataLabel, TrustedImmPtr initialRightValue = TrustedImmPtr(nullptr))
271     {
272         padBeforePatch();
273         m_assembler.cmpl_im_force32(initialRightValue.asIntptr(), left.offset, left.base);
274         dataLabel = DataLabelPtr(this);
275         return Jump(m_assembler.jCC(x86Condition(cond)));
276     }
277
278     Jump branch32WithPatch(RelationalCondition cond, Address left, DataLabel32& dataLabel, TrustedImm32 initialRightValue = TrustedImm32(0))
279     {
280         padBeforePatch();
281         m_assembler.cmpl_im_force32(initialRightValue.m_value, left.offset, left.base);
282         dataLabel = DataLabel32(this);
283         return Jump(m_assembler.jCC(x86Condition(cond)));
284     }
285
286     DataLabelPtr storePtrWithPatch(TrustedImmPtr initialValue, ImplicitAddress address)
287     {
288         padBeforePatch();
289         m_assembler.movl_i32m(initialValue.asIntptr(), address.offset, address.base);
290         return DataLabelPtr(this);
291     }
292
293     static bool supportsFloatingPoint() { return isSSE2Present(); }
294     static bool supportsFloatingPointTruncate() { return isSSE2Present(); }
295     static bool supportsFloatingPointSqrt() { return isSSE2Present(); }
296     static bool supportsFloatingPointAbs() { return isSSE2Present(); }
297     
298     static FunctionPtr readCallTarget(CodeLocationCall call)
299     {
300         intptr_t offset = reinterpret_cast<int32_t*>(call.dataLocation())[-1];
301         return FunctionPtr(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(call.dataLocation()) + offset));
302     }
303
304     static bool canJumpReplacePatchableBranchPtrWithPatch() { return true; }
305     static bool canJumpReplacePatchableBranch32WithPatch() { return true; }
306     
307     static CodeLocationLabel startOfBranchPtrWithPatchOnRegister(CodeLocationDataLabelPtr label)
308     {
309         const int opcodeBytes = 1;
310         const int modRMBytes = 1;
311         const int immediateBytes = 4;
312         const int totalBytes = opcodeBytes + modRMBytes + immediateBytes;
313         ASSERT(totalBytes >= maxJumpReplacementSize());
314         return label.labelAtOffset(-totalBytes);
315     }
316     
317     static CodeLocationLabel startOfPatchableBranchPtrWithPatchOnAddress(CodeLocationDataLabelPtr label)
318     {
319         const int opcodeBytes = 1;
320         const int modRMBytes = 1;
321         const int offsetBytes = 0;
322         const int immediateBytes = 4;
323         const int totalBytes = opcodeBytes + modRMBytes + offsetBytes + immediateBytes;
324         ASSERT(totalBytes >= maxJumpReplacementSize());
325         return label.labelAtOffset(-totalBytes);
326     }
327     
328     static CodeLocationLabel startOfPatchableBranch32WithPatchOnAddress(CodeLocationDataLabel32 label)
329     {
330         const int opcodeBytes = 1;
331         const int modRMBytes = 1;
332         const int offsetBytes = 0;
333         const int immediateBytes = 4;
334         const int totalBytes = opcodeBytes + modRMBytes + offsetBytes + immediateBytes;
335         ASSERT(totalBytes >= maxJumpReplacementSize());
336         return label.labelAtOffset(-totalBytes);
337     }
338     
339     static void revertJumpReplacementToBranchPtrWithPatch(CodeLocationLabel instructionStart, RegisterID reg, void* initialValue)
340     {
341         X86Assembler::revertJumpTo_cmpl_ir_force32(instructionStart.executableAddress(), reinterpret_cast<intptr_t>(initialValue), reg);
342     }
343
344     static void revertJumpReplacementToPatchableBranchPtrWithPatch(CodeLocationLabel instructionStart, Address address, void* initialValue)
345     {
346         ASSERT(!address.offset);
347         X86Assembler::revertJumpTo_cmpl_im_force32(instructionStart.executableAddress(), reinterpret_cast<intptr_t>(initialValue), 0, address.base);
348     }
349
350     static void revertJumpReplacementToPatchableBranch32WithPatch(CodeLocationLabel instructionStart, Address address, int32_t initialValue)
351     {
352         ASSERT(!address.offset);
353         X86Assembler::revertJumpTo_cmpl_im_force32(instructionStart.executableAddress(), initialValue, 0, address.base);
354     }
355
356     static void repatchCall(CodeLocationCall call, CodeLocationLabel destination)
357     {
358         X86Assembler::relinkCall(call.dataLocation(), destination.executableAddress());
359     }
360
361     static void repatchCall(CodeLocationCall call, FunctionPtr destination)
362     {
363         X86Assembler::relinkCall(call.dataLocation(), destination.executableAddress());
364     }
365
366 private:
367     friend class LinkBuffer;
368
369     static void linkCall(void* code, Call call, FunctionPtr function)
370     {
371         if (call.isFlagSet(Call::Tail))
372             X86Assembler::linkJump(code, call.m_label, function.value());
373         else
374             X86Assembler::linkCall(code, call.m_label, function.value());
375     }
376 };
377
378 } // namespace JSC
379
380 #endif // ENABLE(ASSEMBLER)