Merge "Save and re-bind previously bounded texture when using cairo_gl_surface_set_bi...
[framework/web/webkit-efl.git] / Source / JavaScriptCore / assembler / ARMAssembler.h
1 /*
2  * Copyright (C) 2009, 2010 University of Szeged
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifndef ARMAssembler_h
28 #define ARMAssembler_h
29
30 #if ENABLE(ASSEMBLER) && CPU(ARM_TRADITIONAL)
31
32 #include "AssemblerBufferWithConstantPool.h"
33 #include "JITCompilationEffort.h"
34 #include <wtf/Assertions.h>
35 namespace JSC {
36
37     typedef uint32_t ARMWord;
38
39     namespace ARMRegisters {
40         typedef enum {
41             r0 = 0,
42             r1,
43             r2,
44             r3, S0 = r3, /* Same as thumb assembler. */
45             r4,
46             r5,
47             r6,
48             r7,
49             r8,
50             r9,
51             r10,
52             r11,
53             r12, S1 = r12,
54             r13, sp = r13,
55             r14, lr = r14,
56             r15, pc = r15
57         } RegisterID;
58
59         typedef enum {
60             d0,
61             d1,
62             d2,
63             d3,
64             d4,
65             d5,
66             d6,
67             d7, SD0 = d7, /* Same as thumb assembler. */
68             d8,
69             d9,
70             d10,
71             d11,
72             d12,
73             d13,
74             d14,
75             d15,
76             d16,
77             d17,
78             d18,
79             d19,
80             d20,
81             d21,
82             d22,
83             d23,
84             d24,
85             d25,
86             d26,
87             d27,
88             d28,
89             d29,
90             d30,
91             d31
92         } FPRegisterID;
93
94     } // namespace ARMRegisters
95
96     class ARMAssembler {
97     public:
98         typedef ARMRegisters::RegisterID RegisterID;
99         typedef ARMRegisters::FPRegisterID FPRegisterID;
100         typedef AssemblerBufferWithConstantPool<2048, 4, 4, ARMAssembler> ARMBuffer;
101         typedef SegmentedVector<AssemblerLabel, 64> Jumps;
102
103         ARMAssembler()
104             : m_indexOfTailOfLastWatchpoint(1)
105         {
106         }
107
108         // ARM conditional constants
109         typedef enum {
110             EQ = 0x00000000, // Zero
111             NE = 0x10000000, // Non-zero
112             CS = 0x20000000,
113             CC = 0x30000000,
114             MI = 0x40000000,
115             PL = 0x50000000,
116             VS = 0x60000000,
117             VC = 0x70000000,
118             HI = 0x80000000,
119             LS = 0x90000000,
120             GE = 0xa0000000,
121             LT = 0xb0000000,
122             GT = 0xc0000000,
123             LE = 0xd0000000,
124             AL = 0xe0000000
125         } Condition;
126
127         // ARM instruction constants
128         enum {
129             AND = (0x0 << 21),
130             EOR = (0x1 << 21),
131             SUB = (0x2 << 21),
132             RSB = (0x3 << 21),
133             ADD = (0x4 << 21),
134             ADC = (0x5 << 21),
135             SBC = (0x6 << 21),
136             RSC = (0x7 << 21),
137             TST = (0x8 << 21),
138             TEQ = (0x9 << 21),
139             CMP = (0xa << 21),
140             CMN = (0xb << 21),
141             ORR = (0xc << 21),
142             MOV = (0xd << 21),
143             BIC = (0xe << 21),
144             MVN = (0xf << 21),
145             MUL = 0x00000090,
146             MULL = 0x00c00090,
147             VMOV_F64 = 0x0eb00b40,
148             VADD_F64 = 0x0e300b00,
149             VDIV_F64 = 0x0e800b00,
150             VSUB_F64 = 0x0e300b40,
151             VMUL_F64 = 0x0e200b00,
152             VCMP_F64 = 0x0eb40b40,
153             VSQRT_F64 = 0x0eb10bc0,
154             VABS_F64 = 0x0eb00bc0,
155             VNEG_F64 = 0x0eb10b40,
156             STMDB = 0x09200000,
157             LDMIA = 0x08b00000,
158             B = 0x0a000000,
159             BL = 0x0b000000,
160             BX = 0x012fff10,
161             VMOV_VFP64 = 0x0c400a10,
162             VMOV_ARM64 = 0x0c500a10,
163             VMOV_VFP32 = 0x0e000a10,
164             VMOV_ARM32 = 0x0e100a10,
165             VCVT_F64_S32 = 0x0eb80bc0,
166             VCVT_S32_F64 = 0x0ebd0b40,
167             VCVT_U32_F64 = 0x0ebc0b40,
168             VCVT_F32_F64 = 0x0eb70bc0,
169             VCVT_F64_F32 = 0x0eb70ac0,
170             VMRS_APSR = 0x0ef1fa10,
171             CLZ = 0x016f0f10,
172             BKPT = 0xe1200070,
173             BLX = 0x012fff30,
174 #if WTF_ARM_ARCH_AT_LEAST(7)
175             MOVW = 0x03000000,
176             MOVT = 0x03400000,
177 #endif
178             NOP = 0xe1a00000,
179         };
180
181         enum {
182             OP2_IMM = (1 << 25),
183             OP2_IMM_HALF = (1 << 22),
184             OP2_INV_IMM = (1 << 26),
185             SET_CC = (1 << 20),
186             OP2_OFSREG = (1 << 25),
187             // Data transfer flags.
188             DT_UP = (1 << 23),
189             DT_WB = (1 << 21),
190             DT_PRE = (1 << 24),
191             DT_LOAD = (1 << 20),
192             DT_BYTE = (1 << 22),
193         };
194
195         enum DataTransferTypeA {
196             LoadUint32 = 0x05000000 | DT_LOAD,
197             LoadUint8 = 0x05400000 | DT_LOAD,
198             StoreUint32 = 0x05000000,
199             StoreUint8 = 0x05400000,
200         };
201
202         enum DataTransferTypeB {
203             LoadUint16 = 0x010000b0 | DT_LOAD,
204             LoadInt16 = 0x010000f0 | DT_LOAD,
205             LoadInt8 = 0x010000d0 | DT_LOAD,
206             StoreUint16 = 0x010000b0,
207         };
208
209         enum DataTransferTypeFloat {
210             LoadFloat = 0x0d000a00 | DT_LOAD,
211             LoadDouble = 0x0d000b00 | DT_LOAD,
212             StoreFloat = 0x0d000a00,
213             StoreDouble = 0x0d000b00,
214         };
215
216         // Masks of ARM instructions
217         enum {
218             BRANCH_MASK = 0x00ffffff,
219             NONARM = 0xf0000000,
220             SDT_MASK = 0x0c000000,
221             SDT_OFFSET_MASK = 0xfff,
222         };
223
224         enum {
225             BOFFSET_MIN = -0x00800000,
226             BOFFSET_MAX = 0x007fffff,
227             SDT = 0x04000000,
228         };
229
230         enum {
231             padForAlign8  = 0x00,
232             padForAlign16 = 0x0000,
233             padForAlign32 = 0xe12fff7f // 'bkpt 0xffff' instruction.
234         };
235
236         static const ARMWord INVALID_IMM = 0xf0000000;
237         static const ARMWord InvalidBranchTarget = 0xffffffff;
238         static const int DefaultPrefetching = 2;
239
240         // Instruction formating
241
242         void emitInst(ARMWord op, int rd, int rn, ARMWord op2)
243         {
244             ASSERT(((op2 & ~OP2_IMM) <= 0xfff) || (((op2 & ~OP2_IMM_HALF) <= 0xfff)));
245             m_buffer.putInt(op | RN(rn) | RD(rd) | op2);
246         }
247
248         void emitDoublePrecisionInst(ARMWord op, int dd, int dn, int dm)
249         {
250             ASSERT((dd >= 0 && dd <= 31) && (dn >= 0 && dn <= 31) && (dm >= 0 && dm <= 31));
251             m_buffer.putInt(op | ((dd & 0xf) << 12) | ((dd & 0x10) << (22 - 4))
252                                | ((dn & 0xf) << 16) | ((dn & 0x10) << (7 - 4))
253                                | (dm & 0xf) | ((dm & 0x10) << (5 - 4)));
254         }
255
256         void emitSinglePrecisionInst(ARMWord op, int sd, int sn, int sm)
257         {
258             ASSERT((sd >= 0 && sd <= 31) && (sn >= 0 && sn <= 31) && (sm >= 0 && sm <= 31));
259             m_buffer.putInt(op | ((sd >> 1) << 12) | ((sd & 0x1) << 22)
260                                | ((sn >> 1) << 16) | ((sn & 0x1) << 7)
261                                | (sm >> 1) | ((sm & 0x1) << 5));
262         }
263
264         void and_r(int rd, int rn, ARMWord op2, Condition cc = AL)
265         {
266             emitInst(static_cast<ARMWord>(cc) | AND, rd, rn, op2);
267         }
268
269         void ands_r(int rd, int rn, ARMWord op2, Condition cc = AL)
270         {
271             emitInst(static_cast<ARMWord>(cc) | AND | SET_CC, rd, rn, op2);
272         }
273
274         void eor_r(int rd, int rn, ARMWord op2, Condition cc = AL)
275         {
276             emitInst(static_cast<ARMWord>(cc) | EOR, rd, rn, op2);
277         }
278
279         void eors_r(int rd, int rn, ARMWord op2, Condition cc = AL)
280         {
281             emitInst(static_cast<ARMWord>(cc) | EOR | SET_CC, rd, rn, op2);
282         }
283
284         void sub_r(int rd, int rn, ARMWord op2, Condition cc = AL)
285         {
286             emitInst(static_cast<ARMWord>(cc) | SUB, rd, rn, op2);
287         }
288
289         void subs_r(int rd, int rn, ARMWord op2, Condition cc = AL)
290         {
291             emitInst(static_cast<ARMWord>(cc) | SUB | SET_CC, rd, rn, op2);
292         }
293
294         void rsb_r(int rd, int rn, ARMWord op2, Condition cc = AL)
295         {
296             emitInst(static_cast<ARMWord>(cc) | RSB, rd, rn, op2);
297         }
298
299         void rsbs_r(int rd, int rn, ARMWord op2, Condition cc = AL)
300         {
301             emitInst(static_cast<ARMWord>(cc) | RSB | SET_CC, rd, rn, op2);
302         }
303
304         void add_r(int rd, int rn, ARMWord op2, Condition cc = AL)
305         {
306             emitInst(static_cast<ARMWord>(cc) | ADD, rd, rn, op2);
307         }
308
309         void adds_r(int rd, int rn, ARMWord op2, Condition cc = AL)
310         {
311             emitInst(static_cast<ARMWord>(cc) | ADD | SET_CC, rd, rn, op2);
312         }
313
314         void adc_r(int rd, int rn, ARMWord op2, Condition cc = AL)
315         {
316             emitInst(static_cast<ARMWord>(cc) | ADC, rd, rn, op2);
317         }
318
319         void adcs_r(int rd, int rn, ARMWord op2, Condition cc = AL)
320         {
321             emitInst(static_cast<ARMWord>(cc) | ADC | SET_CC, rd, rn, op2);
322         }
323
324         void sbc_r(int rd, int rn, ARMWord op2, Condition cc = AL)
325         {
326             emitInst(static_cast<ARMWord>(cc) | SBC, rd, rn, op2);
327         }
328
329         void sbcs_r(int rd, int rn, ARMWord op2, Condition cc = AL)
330         {
331             emitInst(static_cast<ARMWord>(cc) | SBC | SET_CC, rd, rn, op2);
332         }
333
334         void rsc_r(int rd, int rn, ARMWord op2, Condition cc = AL)
335         {
336             emitInst(static_cast<ARMWord>(cc) | RSC, rd, rn, op2);
337         }
338
339         void rscs_r(int rd, int rn, ARMWord op2, Condition cc = AL)
340         {
341             emitInst(static_cast<ARMWord>(cc) | RSC | SET_CC, rd, rn, op2);
342         }
343
344         void tst_r(int rn, ARMWord op2, Condition cc = AL)
345         {
346             emitInst(static_cast<ARMWord>(cc) | TST | SET_CC, 0, rn, op2);
347         }
348
349         void teq_r(int rn, ARMWord op2, Condition cc = AL)
350         {
351             emitInst(static_cast<ARMWord>(cc) | TEQ | SET_CC, 0, rn, op2);
352         }
353
354         void cmp_r(int rn, ARMWord op2, Condition cc = AL)
355         {
356             emitInst(static_cast<ARMWord>(cc) | CMP | SET_CC, 0, rn, op2);
357         }
358
359         void cmn_r(int rn, ARMWord op2, Condition cc = AL)
360         {
361             emitInst(static_cast<ARMWord>(cc) | CMN | SET_CC, 0, rn, op2);
362         }
363
364         void orr_r(int rd, int rn, ARMWord op2, Condition cc = AL)
365         {
366             emitInst(static_cast<ARMWord>(cc) | ORR, rd, rn, op2);
367         }
368
369         void orrs_r(int rd, int rn, ARMWord op2, Condition cc = AL)
370         {
371             emitInst(static_cast<ARMWord>(cc) | ORR | SET_CC, rd, rn, op2);
372         }
373
374         void mov_r(int rd, ARMWord op2, Condition cc = AL)
375         {
376             emitInst(static_cast<ARMWord>(cc) | MOV, rd, ARMRegisters::r0, op2);
377         }
378
379 #if WTF_ARM_ARCH_AT_LEAST(7)
380         void movw_r(int rd, ARMWord op2, Condition cc = AL)
381         {
382             ASSERT((op2 | 0xf0fff) == 0xf0fff);
383             m_buffer.putInt(static_cast<ARMWord>(cc) | MOVW | RD(rd) | op2);
384         }
385
386         void movt_r(int rd, ARMWord op2, Condition cc = AL)
387         {
388             ASSERT((op2 | 0xf0fff) == 0xf0fff);
389             m_buffer.putInt(static_cast<ARMWord>(cc) | MOVT | RD(rd) | op2);
390         }
391 #endif
392
393         void movs_r(int rd, ARMWord op2, Condition cc = AL)
394         {
395             emitInst(static_cast<ARMWord>(cc) | MOV | SET_CC, rd, ARMRegisters::r0, op2);
396         }
397
398         void bic_r(int rd, int rn, ARMWord op2, Condition cc = AL)
399         {
400             emitInst(static_cast<ARMWord>(cc) | BIC, rd, rn, op2);
401         }
402
403         void bics_r(int rd, int rn, ARMWord op2, Condition cc = AL)
404         {
405             emitInst(static_cast<ARMWord>(cc) | BIC | SET_CC, rd, rn, op2);
406         }
407
408         void mvn_r(int rd, ARMWord op2, Condition cc = AL)
409         {
410             emitInst(static_cast<ARMWord>(cc) | MVN, rd, ARMRegisters::r0, op2);
411         }
412
413         void mvns_r(int rd, ARMWord op2, Condition cc = AL)
414         {
415             emitInst(static_cast<ARMWord>(cc) | MVN | SET_CC, rd, ARMRegisters::r0, op2);
416         }
417
418         void mul_r(int rd, int rn, int rm, Condition cc = AL)
419         {
420             m_buffer.putInt(static_cast<ARMWord>(cc) | MUL | RN(rd) | RS(rn) | RM(rm));
421         }
422
423         void muls_r(int rd, int rn, int rm, Condition cc = AL)
424         {
425             m_buffer.putInt(static_cast<ARMWord>(cc) | MUL | SET_CC | RN(rd) | RS(rn) | RM(rm));
426         }
427
428         void mull_r(int rdhi, int rdlo, int rn, int rm, Condition cc = AL)
429         {
430             m_buffer.putInt(static_cast<ARMWord>(cc) | MULL | RN(rdhi) | RD(rdlo) | RS(rn) | RM(rm));
431         }
432
433         void vmov_f64_r(int dd, int dm, Condition cc = AL)
434         {
435             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VMOV_F64, dd, 0, dm);
436         }
437
438         void vadd_f64_r(int dd, int dn, int dm, Condition cc = AL)
439         {
440             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VADD_F64, dd, dn, dm);
441         }
442
443         void vdiv_f64_r(int dd, int dn, int dm, Condition cc = AL)
444         {
445             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VDIV_F64, dd, dn, dm);
446         }
447
448         void vsub_f64_r(int dd, int dn, int dm, Condition cc = AL)
449         {
450             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VSUB_F64, dd, dn, dm);
451         }
452
453         void vmul_f64_r(int dd, int dn, int dm, Condition cc = AL)
454         {
455             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VMUL_F64, dd, dn, dm);
456         }
457
458         void vcmp_f64_r(int dd, int dm, Condition cc = AL)
459         {
460             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VCMP_F64, dd, 0, dm);
461         }
462
463         void vsqrt_f64_r(int dd, int dm, Condition cc = AL)
464         {
465             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VSQRT_F64, dd, 0, dm);
466         }
467
468         void vabs_f64_r(int dd, int dm, Condition cc = AL)
469         {
470             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VABS_F64, dd, 0, dm);
471         }
472
473         void vneg_f64_r(int dd, int dm, Condition cc = AL)
474         {
475             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VNEG_F64, dd, 0, dm);
476         }
477
478         void ldr_imm(int rd, ARMWord imm, Condition cc = AL)
479         {
480             m_buffer.putIntWithConstantInt(static_cast<ARMWord>(cc) | LoadUint32 | DT_UP | RN(ARMRegisters::pc) | RD(rd), imm, true);
481         }
482
483         void ldr_un_imm(int rd, ARMWord imm, Condition cc = AL)
484         {
485             m_buffer.putIntWithConstantInt(static_cast<ARMWord>(cc) | LoadUint32 | DT_UP | RN(ARMRegisters::pc) | RD(rd), imm);
486         }
487
488         void dtr_u(DataTransferTypeA transferType, int rd, int rb, ARMWord op2, Condition cc = AL)
489         {
490             emitInst(static_cast<ARMWord>(cc) | transferType | DT_UP, rd, rb, op2);
491         }
492
493         void dtr_ur(DataTransferTypeA transferType, int rd, int rb, int rm, Condition cc = AL)
494         {
495             emitInst(static_cast<ARMWord>(cc) | transferType | DT_UP | OP2_OFSREG, rd, rb, rm);
496         }
497
498         void dtr_d(DataTransferTypeA transferType, int rd, int rb, ARMWord op2, Condition cc = AL)
499         {
500             emitInst(static_cast<ARMWord>(cc) | transferType, rd, rb, op2);
501         }
502
503         void dtr_dr(DataTransferTypeA transferType, int rd, int rb, int rm, Condition cc = AL)
504         {
505             emitInst(static_cast<ARMWord>(cc) | transferType | OP2_OFSREG, rd, rb, rm);
506         }
507
508         void dtrh_u(DataTransferTypeB transferType, int rd, int rb, ARMWord op2, Condition cc = AL)
509         {
510             emitInst(static_cast<ARMWord>(cc) | transferType | DT_UP, rd, rb, op2);
511         }
512
513         void dtrh_ur(DataTransferTypeB transferType, int rd, int rn, int rm, Condition cc = AL)
514         {
515             emitInst(static_cast<ARMWord>(cc) | transferType | DT_UP, rd, rn, rm);
516         }
517
518         void dtrh_d(DataTransferTypeB transferType, int rd, int rb, ARMWord op2, Condition cc = AL)
519         {
520             emitInst(static_cast<ARMWord>(cc) | transferType, rd, rb, op2);
521         }
522
523         void dtrh_dr(DataTransferTypeB transferType, int rd, int rn, int rm, Condition cc = AL)
524         {
525             emitInst(static_cast<ARMWord>(cc) | transferType, rd, rn, rm);
526         }
527
528         void fdtr_u(DataTransferTypeFloat type, int rd, int rb, ARMWord op2, Condition cc = AL)
529         {
530             ASSERT(op2 <= 0xff && rd <= 15);
531             /* Only d0-d15 and s0, s2, s4 ... s30 are supported. */
532             m_buffer.putInt(static_cast<ARMWord>(cc) | DT_UP | type | (rd << 12) | RN(rb) | op2);
533         }
534
535         void fdtr_d(DataTransferTypeFloat type, int rd, int rb, ARMWord op2, Condition cc = AL)
536         {
537             ASSERT(op2 <= 0xff && rd <= 15);
538             /* Only d0-d15 and s0, s2, s4 ... s30 are supported. */
539             m_buffer.putInt(static_cast<ARMWord>(cc) | type | (rd << 12) | RN(rb) | op2);
540         }
541
542         void push_r(int reg, Condition cc = AL)
543         {
544             ASSERT(ARMWord(reg) <= 0xf);
545             m_buffer.putInt(static_cast<ARMWord>(cc) | StoreUint32 | DT_WB | RN(ARMRegisters::sp) | RD(reg) | 0x4);
546         }
547
548         void pop_r(int reg, Condition cc = AL)
549         {
550             ASSERT(ARMWord(reg) <= 0xf);
551             m_buffer.putInt(static_cast<ARMWord>(cc) | (LoadUint32 ^ DT_PRE) | DT_UP | RN(ARMRegisters::sp) | RD(reg) | 0x4);
552         }
553
554         inline void poke_r(int reg, Condition cc = AL)
555         {
556             dtr_d(StoreUint32, ARMRegisters::sp, 0, reg, cc);
557         }
558
559         inline void peek_r(int reg, Condition cc = AL)
560         {
561             dtr_u(LoadUint32, reg, ARMRegisters::sp, 0, cc);
562         }
563
564         void vmov_vfp64_r(int sm, int rt, int rt2, Condition cc = AL)
565         {
566             ASSERT(rt != rt2);
567             m_buffer.putInt(static_cast<ARMWord>(cc) | VMOV_VFP64 | RN(rt2) | RD(rt) | (sm & 0xf) | ((sm & 0x10) << (5 - 4)));
568         }
569
570         void vmov_arm64_r(int rt, int rt2, int sm, Condition cc = AL)
571         {
572             ASSERT(rt != rt2);
573             m_buffer.putInt(static_cast<ARMWord>(cc) | VMOV_ARM64 | RN(rt2) | RD(rt) | (sm & 0xf) | ((sm & 0x10) << (5 - 4)));
574         }
575
576         void vmov_vfp32_r(int sn, int rt, Condition cc = AL)
577         {
578             ASSERT(rt <= 15);
579             emitSinglePrecisionInst(static_cast<ARMWord>(cc) | VMOV_VFP32, rt << 1, sn, 0);
580         }
581
582         void vmov_arm32_r(int rt, int sn, Condition cc = AL)
583         {
584             ASSERT(rt <= 15);
585             emitSinglePrecisionInst(static_cast<ARMWord>(cc) | VMOV_ARM32, rt << 1, sn, 0);
586         }
587
588         void vcvt_f64_s32_r(int dd, int sm, Condition cc = AL)
589         {
590             ASSERT(!(sm & 0x1)); // sm must be divisible by 2
591             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VCVT_F64_S32, dd, 0, (sm >> 1));
592         }
593
594         void vcvt_s32_f64_r(int sd, int dm, Condition cc = AL)
595         {
596             ASSERT(!(sd & 0x1)); // sd must be divisible by 2
597             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VCVT_S32_F64, (sd >> 1), 0, dm);
598         }
599
600         void vcvt_u32_f64_r(int sd, int dm, Condition cc = AL)
601         {
602             ASSERT(!(sd & 0x1)); // sd must be divisible by 2
603             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VCVT_U32_F64, (sd >> 1), 0, dm);
604         }
605
606         void vcvt_f64_f32_r(int dd, int sm, Condition cc = AL)
607         {
608             ASSERT(dd <= 15 && sm <= 15);
609             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VCVT_F64_F32, dd, 0, sm);
610         }
611
612         void vcvt_f32_f64_r(int dd, int sm, Condition cc = AL)
613         {
614             ASSERT(dd <= 15 && sm <= 15);
615             emitDoublePrecisionInst(static_cast<ARMWord>(cc) | VCVT_F32_F64, dd, 0, sm);
616         }
617
618         void vmrs_apsr(Condition cc = AL)
619         {
620             m_buffer.putInt(static_cast<ARMWord>(cc) | VMRS_APSR);
621         }
622
623         void clz_r(int rd, int rm, Condition cc = AL)
624         {
625             m_buffer.putInt(static_cast<ARMWord>(cc) | CLZ | RD(rd) | RM(rm));
626         }
627
628         void bkpt(ARMWord value)
629         {
630             m_buffer.putInt(BKPT | ((value & 0xff0) << 4) | (value & 0xf));
631         }
632
633         void nop()
634         {
635             m_buffer.putInt(NOP);
636         }
637
638         void bx(int rm, Condition cc = AL)
639         {
640             emitInst(static_cast<ARMWord>(cc) | BX, 0, 0, RM(rm));
641         }
642
643         AssemblerLabel blx(int rm, Condition cc = AL)
644         {
645             emitInst(static_cast<ARMWord>(cc) | BLX, 0, 0, RM(rm));
646             return m_buffer.label();
647         }
648
649         static ARMWord lsl(int reg, ARMWord value)
650         {
651             ASSERT(reg <= ARMRegisters::pc);
652             ASSERT(value <= 0x1f);
653             return reg | (value << 7) | 0x00;
654         }
655
656         static ARMWord lsr(int reg, ARMWord value)
657         {
658             ASSERT(reg <= ARMRegisters::pc);
659             ASSERT(value <= 0x1f);
660             return reg | (value << 7) | 0x20;
661         }
662
663         static ARMWord asr(int reg, ARMWord value)
664         {
665             ASSERT(reg <= ARMRegisters::pc);
666             ASSERT(value <= 0x1f);
667             return reg | (value << 7) | 0x40;
668         }
669
670         static ARMWord lsl_r(int reg, int shiftReg)
671         {
672             ASSERT(reg <= ARMRegisters::pc);
673             ASSERT(shiftReg <= ARMRegisters::pc);
674             return reg | (shiftReg << 8) | 0x10;
675         }
676
677         static ARMWord lsr_r(int reg, int shiftReg)
678         {
679             ASSERT(reg <= ARMRegisters::pc);
680             ASSERT(shiftReg <= ARMRegisters::pc);
681             return reg | (shiftReg << 8) | 0x30;
682         }
683
684         static ARMWord asr_r(int reg, int shiftReg)
685         {
686             ASSERT(reg <= ARMRegisters::pc);
687             ASSERT(shiftReg <= ARMRegisters::pc);
688             return reg | (shiftReg << 8) | 0x50;
689         }
690
691         // General helpers
692
693         size_t codeSize() const
694         {
695             return m_buffer.codeSize();
696         }
697
698         void ensureSpace(int insnSpace, int constSpace)
699         {
700             m_buffer.ensureSpace(insnSpace, constSpace);
701         }
702
703         int sizeOfConstantPool()
704         {
705             return m_buffer.sizeOfConstantPool();
706         }
707
708         AssemblerLabel labelIgnoringWatchpoints()
709         {
710             m_buffer.ensureSpaceForAnyInstruction();
711             return m_buffer.label();
712         }
713
714         AssemblerLabel labelForWatchpoint()
715         {
716             m_buffer.ensureSpaceForAnyInstruction(maxJumpReplacementSize() / sizeof(ARMWord));
717             AssemblerLabel result = m_buffer.label();
718             if (result.m_offset != (m_indexOfTailOfLastWatchpoint - maxJumpReplacementSize()))
719                 result = label();
720             m_indexOfTailOfLastWatchpoint = result.m_offset + maxJumpReplacementSize();
721             return label();
722         }
723
724         AssemblerLabel label()
725         {
726             AssemblerLabel result = labelIgnoringWatchpoints();
727             while (result.m_offset + 1 < m_indexOfTailOfLastWatchpoint) {
728                 nop();
729                 // The available number of instructions are ensured by labelForWatchpoint.
730                 result = m_buffer.label();
731             }
732             return result;
733         }
734
735         AssemblerLabel align(int alignment)
736         {
737             while (!m_buffer.isAligned(alignment))
738                 mov_r(ARMRegisters::r0, ARMRegisters::r0);
739
740             return label();
741         }
742
743         AssemblerLabel loadBranchTarget(int rd, Condition cc = AL, int useConstantPool = 0)
744         {
745             ensureSpace(sizeof(ARMWord), sizeof(ARMWord));
746             m_jumps.append(m_buffer.codeSize() | (useConstantPool & 0x1));
747             ldr_un_imm(rd, InvalidBranchTarget, cc);
748             return m_buffer.label();
749         }
750
751         AssemblerLabel jmp(Condition cc = AL, int useConstantPool = 0)
752         {
753             return loadBranchTarget(ARMRegisters::pc, cc, useConstantPool);
754         }
755
756         PassRefPtr<ExecutableMemoryHandle> executableCopy(JSGlobalData&, void* ownerUID, JITCompilationEffort);
757
758         unsigned debugOffset() { return m_buffer.debugOffset(); }
759
760         // DFG assembly helpers for moving data between fp and registers.
761         void vmov(RegisterID rd1, RegisterID rd2, FPRegisterID rn)
762         {
763             vmov_arm64_r(rd1, rd2, rn);
764         }
765
766         void vmov(FPRegisterID rd, RegisterID rn1, RegisterID rn2)
767         {
768             vmov_vfp64_r(rd, rn1, rn2);
769         }
770
771         // Patching helpers
772
773         static ARMWord* getLdrImmAddress(ARMWord* insn)
774         {
775             // Check for call
776             if ((*insn & 0x0f7f0000) != 0x051f0000) {
777                 // Must be BLX
778                 ASSERT((*insn & 0x012fff30) == 0x012fff30);
779                 insn--;
780             }
781
782             // Must be an ldr ..., [pc +/- imm]
783             ASSERT((*insn & 0x0f7f0000) == 0x051f0000);
784
785             ARMWord addr = reinterpret_cast<ARMWord>(insn) + DefaultPrefetching * sizeof(ARMWord);
786             if (*insn & DT_UP)
787                 return reinterpret_cast<ARMWord*>(addr + (*insn & SDT_OFFSET_MASK));
788             return reinterpret_cast<ARMWord*>(addr - (*insn & SDT_OFFSET_MASK));
789         }
790
791         static ARMWord* getLdrImmAddressOnPool(ARMWord* insn, uint32_t* constPool)
792         {
793             // Must be an ldr ..., [pc +/- imm]
794             ASSERT((*insn & 0x0f7f0000) == 0x051f0000);
795
796             if (*insn & 0x1)
797                 return reinterpret_cast<ARMWord*>(constPool + ((*insn & SDT_OFFSET_MASK) >> 1));
798             return getLdrImmAddress(insn);
799         }
800
801         static void patchPointerInternal(intptr_t from, void* to)
802         {
803             ARMWord* insn = reinterpret_cast<ARMWord*>(from);
804             ARMWord* addr = getLdrImmAddress(insn);
805             *addr = reinterpret_cast<ARMWord>(to);
806         }
807
808         static ARMWord patchConstantPoolLoad(ARMWord load, ARMWord value)
809         {
810             value = (value << 1) + 1;
811             ASSERT(!(value & ~0xfff));
812             return (load & ~0xfff) | value;
813         }
814
815         static void patchConstantPoolLoad(void* loadAddr, void* constPoolAddr);
816
817         // Read pointers
818         static void* readPointer(void* from)
819         {
820             ARMWord* instruction = reinterpret_cast<ARMWord*>(from);
821             ARMWord* address = getLdrImmAddress(instruction);
822             return *reinterpret_cast<void**>(address);
823         }
824
825         // Patch pointers
826
827         static void linkPointer(void* code, AssemblerLabel from, void* to)
828         {
829             patchPointerInternal(reinterpret_cast<intptr_t>(code) + from.m_offset, to);
830         }
831
832         static void repatchInt32(void* where, int32_t to)
833         {
834             patchPointerInternal(reinterpret_cast<intptr_t>(where), reinterpret_cast<void*>(to));
835         }
836
837         static void repatchCompact(void* where, int32_t value)
838         {
839             ARMWord* instruction = reinterpret_cast<ARMWord*>(where);
840             ASSERT((*instruction & 0x0f700000) == LoadUint32);
841             if (value >= 0)
842                 *instruction = (*instruction & 0xff7ff000) | DT_UP | value;
843             else
844                 *instruction = (*instruction & 0xff7ff000) | -value;
845             cacheFlush(instruction, sizeof(ARMWord));
846         }
847
848         static void repatchPointer(void* from, void* to)
849         {
850             patchPointerInternal(reinterpret_cast<intptr_t>(from), to);
851         }
852
853         // Linkers
854         static intptr_t getAbsoluteJumpAddress(void* base, int offset = 0)
855         {
856             return reinterpret_cast<intptr_t>(base) + offset - sizeof(ARMWord);
857         }
858
859         void linkJump(AssemblerLabel from, AssemblerLabel to)
860         {
861             ARMWord* insn = reinterpret_cast<ARMWord*>(getAbsoluteJumpAddress(m_buffer.data(), from.m_offset));
862             ARMWord* addr = getLdrImmAddressOnPool(insn, m_buffer.poolAddress());
863             *addr = static_cast<ARMWord>(to.m_offset);
864         }
865
866         static void linkJump(void* code, AssemblerLabel from, void* to)
867         {
868             patchPointerInternal(getAbsoluteJumpAddress(code, from.m_offset), to);
869         }
870
871         static void relinkJump(void* from, void* to)
872         {
873             patchPointerInternal(getAbsoluteJumpAddress(from), to);
874         }
875
876         static void linkCall(void* code, AssemblerLabel from, void* to)
877         {
878             patchPointerInternal(getAbsoluteJumpAddress(code, from.m_offset), to);
879         }
880
881         static void relinkCall(void* from, void* to)
882         {
883             patchPointerInternal(getAbsoluteJumpAddress(from), to);
884         }
885
886         static void* readCallTarget(void* from)
887         {
888             return reinterpret_cast<void*>(readPointer(reinterpret_cast<void*>(getAbsoluteJumpAddress(from))));
889         }
890
891         static void replaceWithJump(void* instructionStart, void* to)
892         {
893             ARMWord* instruction = reinterpret_cast<ARMWord*>(instructionStart) - 1;
894             intptr_t difference = reinterpret_cast<intptr_t>(to) - (reinterpret_cast<intptr_t>(instruction) + DefaultPrefetching * sizeof(ARMWord));
895
896             if (!(difference & 1)) {
897                 difference >>= 2;
898                 if ((difference <= BOFFSET_MAX && difference >= BOFFSET_MIN)) {
899                      // Direct branch.
900                      instruction[0] = B | AL | (difference & BRANCH_MASK);
901                      cacheFlush(instruction, sizeof(ARMWord));
902                      return;
903                 }
904             }
905
906             // Load target.
907             instruction[0] = LoadUint32 | AL | RN(ARMRegisters::pc) | RD(ARMRegisters::pc) | 4;
908             instruction[1] = reinterpret_cast<ARMWord>(to);
909             cacheFlush(instruction, sizeof(ARMWord) * 2);
910         }
911
912         static ptrdiff_t maxJumpReplacementSize()
913         {
914             return sizeof(ARMWord) * 2;
915         }
916
917         static void replaceWithLoad(void* instructionStart)
918         {
919             ARMWord* instruction = reinterpret_cast<ARMWord*>(instructionStart);
920             cacheFlush(instruction, sizeof(ARMWord));
921
922             ASSERT((*instruction & 0x0ff00000) == 0x02800000 || (*instruction & 0x0ff00000) == 0x05900000);
923             if ((*instruction & 0x0ff00000) == 0x02800000) {
924                  *instruction = (*instruction & 0xf00fffff) | 0x05900000;
925                  cacheFlush(instruction, sizeof(ARMWord));
926             }
927         }
928
929         static void replaceWithAddressComputation(void* instructionStart)
930         {
931             ARMWord* instruction = reinterpret_cast<ARMWord*>(instructionStart);
932             cacheFlush(instruction, sizeof(ARMWord));
933
934             ASSERT((*instruction & 0x0ff00000) == 0x02800000 || (*instruction & 0x0ff00000) == 0x05900000);
935             if ((*instruction & 0x0ff00000) == 0x05900000) {
936                  *instruction = (*instruction & 0xf00fffff) | 0x02800000;
937                  cacheFlush(instruction, sizeof(ARMWord));
938             }
939         }
940
941         // Address operations
942
943         static void* getRelocatedAddress(void* code, AssemblerLabel label)
944         {
945             return reinterpret_cast<void*>(reinterpret_cast<char*>(code) + label.m_offset);
946         }
947
948         // Address differences
949
950         static int getDifferenceBetweenLabels(AssemblerLabel a, AssemblerLabel b)
951         {
952             return b.m_offset - a.m_offset;
953         }
954
955         static unsigned getCallReturnOffset(AssemblerLabel call)
956         {
957             return call.m_offset;
958         }
959
960         // Handle immediates
961
962         static ARMWord getOp2(ARMWord imm);
963
964         // Fast case if imm is known to be between 0 and 0xff
965         static ARMWord getOp2Byte(ARMWord imm)
966         {
967             ASSERT(imm <= 0xff);
968             return OP2_IMM | imm;
969         }
970
971         static ARMWord getOp2Half(ARMWord imm)
972         {
973             ASSERT(imm <= 0xff);
974             return OP2_IMM_HALF | (imm & 0x0f) | ((imm & 0xf0) << 4);
975         }
976
977 #if WTF_ARM_ARCH_AT_LEAST(7)
978         static ARMWord getImm16Op2(ARMWord imm)
979         {
980             if (imm <= 0xffff)
981                 return (imm & 0xf000) << 4 | (imm & 0xfff);
982             return INVALID_IMM;
983         }
984 #endif
985         ARMWord getImm(ARMWord imm, int tmpReg, bool invert = false);
986         void moveImm(ARMWord imm, int dest);
987         ARMWord encodeComplexImm(ARMWord imm, int dest);
988
989         // Memory load/store helpers
990
991         void dataTransfer32(DataTransferTypeA, RegisterID srcDst, RegisterID base, int32_t offset);
992         void baseIndexTransfer32(DataTransferTypeA, RegisterID srcDst, RegisterID base, RegisterID index, int scale, int32_t offset);
993         void dataTransfer16(DataTransferTypeB, RegisterID srcDst, RegisterID base, int32_t offset);
994         void baseIndexTransfer16(DataTransferTypeB, RegisterID srcDst, RegisterID base, RegisterID index, int scale, int32_t offset);
995         void dataTransferFloat(DataTransferTypeFloat, FPRegisterID srcDst, RegisterID base, int32_t offset);
996         void baseIndexTransferFloat(DataTransferTypeFloat, FPRegisterID srcDst, RegisterID base, RegisterID index, int scale, int32_t offset);
997
998         // Constant pool hnadlers
999
1000         static ARMWord placeConstantPoolBarrier(int offset)
1001         {
1002             offset = (offset - sizeof(ARMWord)) >> 2;
1003             ASSERT((offset <= BOFFSET_MAX && offset >= BOFFSET_MIN));
1004             return AL | B | (offset & BRANCH_MASK);
1005         }
1006
1007 #if OS(LINUX) && COMPILER(RVCT)
1008         static __asm void cacheFlush(void* code, size_t);
1009 #else
1010         static void cacheFlush(void* code, size_t size)
1011         {
1012 #if OS(LINUX) && COMPILER(GCC)
1013             uintptr_t currentPage = reinterpret_cast<uintptr_t>(code) & ~(pageSize() - 1);
1014             uintptr_t lastPage = (reinterpret_cast<uintptr_t>(code) + size) & ~(pageSize() - 1);
1015             do {
1016                 asm volatile(
1017                     "push    {r7}\n"
1018                     "mov     r0, %0\n"
1019                     "mov     r1, %1\n"
1020                     "mov     r7, #0xf0000\n"
1021                     "add     r7, r7, #0x2\n"
1022                     "mov     r2, #0x0\n"
1023                     "svc     0x0\n"
1024                     "pop     {r7}\n"
1025                     :
1026                     : "r" (currentPage), "r" (currentPage + pageSize())
1027                     : "r0", "r1", "r2");
1028                 currentPage += pageSize();
1029             } while (lastPage >= currentPage);
1030 #elif OS(WINCE)
1031             CacheRangeFlush(code, size, CACHE_SYNC_ALL);
1032 #elif OS(QNX) && ENABLE(ASSEMBLER_WX_EXCLUSIVE)
1033             UNUSED_PARAM(code);
1034             UNUSED_PARAM(size);
1035 #elif OS(QNX)
1036             msync(code, size, MS_INVALIDATE_ICACHE);
1037 #else
1038 #error "The cacheFlush support is missing on this platform."
1039 #endif
1040         }
1041 #endif
1042
1043     private:
1044         static ARMWord RM(int reg)
1045         {
1046             ASSERT(reg <= ARMRegisters::pc);
1047             return reg;
1048         }
1049
1050         static ARMWord RS(int reg)
1051         {
1052             ASSERT(reg <= ARMRegisters::pc);
1053             return reg << 8;
1054         }
1055
1056         static ARMWord RD(int reg)
1057         {
1058             ASSERT(reg <= ARMRegisters::pc);
1059             return reg << 12;
1060         }
1061
1062         static ARMWord RN(int reg)
1063         {
1064             ASSERT(reg <= ARMRegisters::pc);
1065             return reg << 16;
1066         }
1067
1068         static ARMWord getConditionalField(ARMWord i)
1069         {
1070             return i & 0xf0000000;
1071         }
1072
1073         int genInt(int reg, ARMWord imm, bool positive);
1074
1075         ARMBuffer m_buffer;
1076         Jumps m_jumps;
1077         uint32_t m_indexOfTailOfLastWatchpoint;
1078     };
1079
1080 } // namespace JSC
1081
1082 #endif // ENABLE(ASSEMBLER) && CPU(ARM_TRADITIONAL)
1083
1084 #endif // ARMAssembler_h