2 * Copyright (C) 2009-2011 STMicroelectronics. All rights reserved.
3 * Copyright (C) 2008 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``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 APPLE INC. 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.
27 #ifndef SH4Assembler_h
28 #define SH4Assembler_h
30 #if ENABLE(ASSEMBLER) && CPU(SH4)
32 #include "AssemblerBuffer.h"
33 #include "AssemblerBufferWithConstantPool.h"
37 #include <wtf/Assertions.h>
38 #include <wtf/Vector.h>
41 #define SH4_ASSEMBLER_TRACING
45 typedef uint16_t SH4Word;
48 INVALID_OPCODE = 0xffff,
50 ADDIMM_OPCODE = 0x7000,
54 ANDIMM_OPCODE = 0xc900,
69 CMPEQ_OPCODE = 0x3000,
70 CMPEQIMM_OPCODE = 0x8800,
71 CMPGE_OPCODE = 0x3003,
72 CMPGT_OPCODE = 0x3007,
73 CMPHI_OPCODE = 0x3006,
74 CMPHS_OPCODE = 0x3002,
75 CMPPL_OPCODE = 0x4015,
76 CMPPZ_OPCODE = 0x4011,
77 CMPSTR_OPCODE = 0x200c,
79 FCMPEQ_OPCODE = 0xf004,
80 FCMPGT_OPCODE = 0xf005,
89 LDSPR_OPCODE = 0x402a,
90 LDSLPR_OPCODE = 0x4026,
92 MOVIMM_OPCODE = 0xe000,
93 MOVB_WRITE_RN_OPCODE = 0x2000,
94 MOVB_WRITE_RNDEC_OPCODE = 0x2004,
95 MOVB_WRITE_R0RN_OPCODE = 0x0004,
96 MOVB_WRITE_OFFGBR_OPCODE = 0xc000,
97 MOVB_WRITE_OFFRN_OPCODE = 0x8000,
98 MOVB_READ_RM_OPCODE = 0x6000,
99 MOVB_READ_RMINC_OPCODE = 0x6004,
100 MOVB_READ_R0RM_OPCODE = 0x000c,
101 MOVB_READ_OFFGBR_OPCODE = 0xc400,
102 MOVB_READ_OFFRM_OPCODE = 0x8400,
103 MOVL_WRITE_RN_OPCODE = 0x2002,
104 MOVL_WRITE_RNDEC_OPCODE = 0x2006,
105 MOVL_WRITE_R0RN_OPCODE = 0x0006,
106 MOVL_WRITE_OFFGBR_OPCODE = 0xc200,
107 MOVL_WRITE_OFFRN_OPCODE = 0x1000,
108 MOVL_READ_RM_OPCODE = 0x6002,
109 MOVL_READ_RMINC_OPCODE = 0x6006,
110 MOVL_READ_R0RM_OPCODE = 0x000e,
111 MOVL_READ_OFFGBR_OPCODE = 0xc600,
112 MOVL_READ_OFFPC_OPCODE = 0xd000,
113 MOVL_READ_OFFRM_OPCODE = 0x5000,
114 MOVW_WRITE_RN_OPCODE = 0x2001,
115 MOVW_READ_RM_OPCODE = 0x6001,
116 MOVW_READ_R0RM_OPCODE = 0x000d,
117 MOVW_READ_OFFRM_OPCODE = 0x8500,
118 MOVW_READ_OFFPC_OPCODE = 0x9000,
119 MOVA_READ_OFFPC_OPCODE = 0xc700,
120 MOVT_OPCODE = 0x0029,
121 MULL_OPCODE = 0x0007,
122 DMULL_L_OPCODE = 0x3005,
123 STSMACL_OPCODE = 0x001a,
124 STSMACH_OPCODE = 0x000a,
125 DMULSL_OPCODE = 0x300d,
127 NEGC_OPCODE = 0x600a,
130 ORIMM_OPCODE = 0xcb00,
131 ORBIMM_OPCODE = 0xcf00,
132 SETS_OPCODE = 0x0058,
133 SETT_OPCODE = 0x0018,
134 SHAD_OPCODE = 0x400c,
135 SHAL_OPCODE = 0x4020,
136 SHAR_OPCODE = 0x4021,
137 SHLD_OPCODE = 0x400d,
138 SHLL_OPCODE = 0x4000,
139 SHLL2_OPCODE = 0x4008,
140 SHLL8_OPCODE = 0x4018,
141 SHLL16_OPCODE = 0x4028,
142 SHLR_OPCODE = 0x4001,
143 SHLR2_OPCODE = 0x4009,
144 SHLR8_OPCODE = 0x4019,
145 SHLR16_OPCODE = 0x4029,
146 STSPR_OPCODE = 0x002a,
147 STSLPR_OPCODE = 0x4022,
148 FLOAT_OPCODE = 0xf02d,
150 SUBC_OPCODE = 0x300a,
151 SUBV_OPCODE = 0x300b,
153 TSTIMM_OPCODE = 0xc800,
154 TSTB_OPCODE = 0xcc00,
155 EXTUB_OPCODE = 0x600c,
156 EXTUW_OPCODE = 0x600d,
158 XORIMM_OPCODE = 0xca00,
159 XORB_OPCODE = 0xce00,
160 FMOVS_READ_RM_INC_OPCODE = 0xf009,
161 FMOVS_READ_RM_OPCODE = 0xf008,
162 FMOVS_READ_R0RM_OPCODE = 0xf006,
163 FMOVS_WRITE_RN_OPCODE = 0xf00a,
164 FMOVS_WRITE_RN_DEC_OPCODE = 0xf00b,
165 FMOVS_WRITE_R0RN_OPCODE = 0xf007,
166 FCNVDS_DRM_FPUL_OPCODE = 0xf0bd,
167 LDS_RM_FPUL_OPCODE = 0x405a,
168 FLDS_FRM_FPUL_OPCODE = 0xf01d,
169 STS_FPUL_RN_OPCODE = 0x005a,
170 FSTS_FPUL_FRN_OPCODE = 0xF00d,
171 LDSFPSCR_OPCODE = 0x406a,
172 STSFPSCR_OPCODE = 0x006a,
173 LDSRMFPUL_OPCODE = 0x405a,
174 FSTSFPULFRN_OPCODE = 0xf00d,
175 FSQRT_OPCODE = 0xf06d,
176 FSCHG_OPCODE = 0xf3fd,
180 namespace SH4Registers {
222 inline uint16_t getOpcodeGroup1(uint16_t opc, int rm, int rn)
224 return (opc | ((rm & 0xf) << 8) | ((rn & 0xf) << 4));
227 inline uint16_t getOpcodeGroup2(uint16_t opc, int rm)
229 return (opc | ((rm & 0xf) << 8));
232 inline uint16_t getOpcodeGroup3(uint16_t opc, int rm, int rn)
234 return (opc | ((rm & 0xf) << 8) | (rn & 0xff));
237 inline uint16_t getOpcodeGroup4(uint16_t opc, int rm, int rn, int offset)
239 return (opc | ((rm & 0xf) << 8) | ((rn & 0xf) << 4) | (offset & 0xf));
242 inline uint16_t getOpcodeGroup5(uint16_t opc, int rm)
244 return (opc | (rm & 0xff));
247 inline uint16_t getOpcodeGroup6(uint16_t opc, int rm)
249 return (opc | (rm & 0xfff));
252 inline uint16_t getOpcodeGroup7(uint16_t opc, int rm)
254 return (opc | ((rm & 0x7) << 9));
257 inline uint16_t getOpcodeGroup8(uint16_t opc, int rm, int rn)
259 return (opc | ((rm & 0x7) << 9) | ((rn & 0x7) << 5));
262 inline uint16_t getOpcodeGroup9(uint16_t opc, int rm, int rn)
264 return (opc | ((rm & 0xf) << 8) | ((rn & 0x7) << 5));
267 inline uint16_t getOpcodeGroup10(uint16_t opc, int rm, int rn)
269 return (opc | ((rm & 0x7) << 9) | ((rn & 0xf) << 4));
272 inline uint16_t getOpcodeGroup11(uint16_t opc, int rm, int rn)
274 return (opc | ((rm & 0xf) << 4) | (rn & 0xf));
277 inline uint16_t getRn(uint16_t x)
279 return ((x & 0xf00) >> 8);
282 inline uint16_t getRm(uint16_t x)
284 return ((x & 0xf0) >> 4);
287 inline uint16_t getDisp(uint16_t x)
292 inline uint16_t getImm8(uint16_t x)
297 inline uint16_t getImm12(uint16_t x)
302 inline uint16_t getDRn(uint16_t x)
304 return ((x & 0xe00) >> 9);
307 inline uint16_t getDRm(uint16_t x)
309 return ((x & 0xe0) >> 5);
314 typedef SH4Registers::RegisterID RegisterID;
315 typedef SH4Registers::FPRegisterID FPRegisterID;
316 typedef AssemblerBufferWithConstantPool<512, 4, 2, SH4Assembler> SH4Buffer;
317 static const RegisterID scratchReg1 = SH4Registers::r3;
318 static const RegisterID scratchReg2 = SH4Registers::r11;
319 static const uint32_t maxInstructionSize = 16;
323 padForAlign16 = 0x0009,
324 padForAlign32 = 0x00090009,
329 m_claimscratchReg = 0x0;
332 // SH4 condition codes
335 NE = 0x1, // Not Equal
336 HS = 0x2, // Unsigend Greater Than equal
337 HI = 0x3, // Unsigend Greater Than
338 LS = 0x4, // Unsigend Lower or Same
339 LI = 0x5, // Unsigend Lower
340 GE = 0x6, // Greater or Equal
341 LT = 0x7, // Less Than
342 GT = 0x8, // Greater Than
343 LE = 0x9, // Less or Equal
344 OF = 0xa, // OverFlow
346 EQU= 0xc, // Equal or unordered(NaN)
354 // Opaque label types
356 bool isImmediate(int constant)
358 return ((constant <= 127) && (constant >= -128));
361 RegisterID claimScratch()
363 ASSERT((m_claimscratchReg != 0x3));
365 if (!(m_claimscratchReg & 0x1)) {
366 m_claimscratchReg = (m_claimscratchReg | 0x1);
370 m_claimscratchReg = (m_claimscratchReg | 0x2);
374 void releaseScratch(RegisterID scratchR)
376 if (scratchR == scratchReg1)
377 m_claimscratchReg = (m_claimscratchReg & 0x2);
379 m_claimscratchReg = (m_claimscratchReg & 0x1);
384 void pushReg(RegisterID reg)
386 if (reg == SH4Registers::pr) {
387 oneShortOp(getOpcodeGroup2(STSLPR_OPCODE, SH4Registers::sp));
391 oneShortOp(getOpcodeGroup1(MOVL_WRITE_RNDEC_OPCODE, SH4Registers::sp, reg));
394 void popReg(RegisterID reg)
396 if (reg == SH4Registers::pr) {
397 oneShortOp(getOpcodeGroup2(LDSLPR_OPCODE, SH4Registers::sp));
401 oneShortOp(getOpcodeGroup1(MOVL_READ_RMINC_OPCODE, reg, SH4Registers::sp));
404 void movt(RegisterID dst)
406 uint16_t opc = getOpcodeGroup2(MOVT_OPCODE, dst);
410 // Arithmetic operations
412 void addlRegReg(RegisterID src, RegisterID dst)
414 uint16_t opc = getOpcodeGroup1(ADD_OPCODE, dst, src);
418 void addclRegReg(RegisterID src, RegisterID dst)
420 uint16_t opc = getOpcodeGroup1(ADDC_OPCODE, dst, src);
424 void addvlRegReg(RegisterID src, RegisterID dst)
426 uint16_t opc = getOpcodeGroup1(ADDV_OPCODE, dst, src);
430 void addlImm8r(int imm8, RegisterID dst)
432 ASSERT((imm8 <= 127) && (imm8 >= -128));
434 uint16_t opc = getOpcodeGroup3(ADDIMM_OPCODE, dst, imm8);
438 void andlRegReg(RegisterID src, RegisterID dst)
440 uint16_t opc = getOpcodeGroup1(AND_OPCODE, dst, src);
444 void andlImm8r(int imm8, RegisterID dst)
446 ASSERT((imm8 <= 255) && (imm8 >= 0));
447 ASSERT(dst == SH4Registers::r0);
449 uint16_t opc = getOpcodeGroup5(ANDIMM_OPCODE, imm8);
453 void div1lRegReg(RegisterID src, RegisterID dst)
455 uint16_t opc = getOpcodeGroup1(DIV1_OPCODE, dst, src);
459 void div0lRegReg(RegisterID src, RegisterID dst)
461 uint16_t opc = getOpcodeGroup1(DIV0_OPCODE, dst, src);
465 void notlReg(RegisterID src, RegisterID dst)
467 uint16_t opc = getOpcodeGroup1(NOT_OPCODE, dst, src);
471 void orlRegReg(RegisterID src, RegisterID dst)
473 uint16_t opc = getOpcodeGroup1(OR_OPCODE, dst, src);
477 void orlImm8r(int imm8, RegisterID dst)
479 ASSERT((imm8 <= 255) && (imm8 >= 0));
480 ASSERT(dst == SH4Registers::r0);
482 uint16_t opc = getOpcodeGroup5(ORIMM_OPCODE, imm8);
486 void sublRegReg(RegisterID src, RegisterID dst)
488 uint16_t opc = getOpcodeGroup1(SUB_OPCODE, dst, src);
492 void subvlRegReg(RegisterID src, RegisterID dst)
494 uint16_t opc = getOpcodeGroup1(SUBV_OPCODE, dst, src);
498 void xorlRegReg(RegisterID src, RegisterID dst)
500 uint16_t opc = getOpcodeGroup1(XOR_OPCODE, dst, src);
504 void xorlImm8r(int imm8, RegisterID dst)
506 ASSERT((imm8 <= 255) && (imm8 >= 0));
507 ASSERT(dst == SH4Registers::r0);
509 uint16_t opc = getOpcodeGroup5(XORIMM_OPCODE, imm8);
513 void shllImm8r(int imm, RegisterID dst)
517 oneShortOp(getOpcodeGroup2(SHLL_OPCODE, dst));
520 oneShortOp(getOpcodeGroup2(SHLL2_OPCODE, dst));
523 oneShortOp(getOpcodeGroup2(SHLL8_OPCODE, dst));
526 oneShortOp(getOpcodeGroup2(SHLL16_OPCODE, dst));
529 ASSERT_NOT_REACHED();
533 void neg(RegisterID dst, RegisterID src)
535 uint16_t opc = getOpcodeGroup1(NEG_OPCODE, dst, src);
539 void shllRegReg(RegisterID dst, RegisterID rShift)
541 uint16_t opc = getOpcodeGroup1(SHLD_OPCODE, dst, rShift);
545 void shlrRegReg(RegisterID dst, RegisterID rShift)
548 shllRegReg(dst, rShift);
551 void sharRegReg(RegisterID dst, RegisterID rShift)
554 shaRegReg(dst, rShift);
557 void shaRegReg(RegisterID dst, RegisterID rShift)
559 uint16_t opc = getOpcodeGroup1(SHAD_OPCODE, dst, rShift);
563 void shlrImm8r(int imm, RegisterID dst)
567 oneShortOp(getOpcodeGroup2(SHLR_OPCODE, dst));
570 oneShortOp(getOpcodeGroup2(SHLR2_OPCODE, dst));
573 oneShortOp(getOpcodeGroup2(SHLR8_OPCODE, dst));
576 oneShortOp(getOpcodeGroup2(SHLR16_OPCODE, dst));
579 ASSERT_NOT_REACHED();
583 void imullRegReg(RegisterID src, RegisterID dst)
585 uint16_t opc = getOpcodeGroup1(MULL_OPCODE, dst, src);
589 void dmullRegReg(RegisterID src, RegisterID dst)
591 uint16_t opc = getOpcodeGroup1(DMULL_L_OPCODE, dst, src);
595 void dmulslRegReg(RegisterID src, RegisterID dst)
597 uint16_t opc = getOpcodeGroup1(DMULSL_OPCODE, dst, src);
601 void stsmacl(RegisterID reg)
603 uint16_t opc = getOpcodeGroup2(STSMACL_OPCODE, reg);
607 void stsmach(RegisterID reg)
609 uint16_t opc = getOpcodeGroup2(STSMACH_OPCODE, reg);
615 void cmplRegReg(RegisterID left, RegisterID right, Condition cond)
619 oneShortOp(getOpcodeGroup1(CMPEQ_OPCODE, right, left));
622 oneShortOp(getOpcodeGroup1(CMPGT_OPCODE, right, left));
625 oneShortOp(getOpcodeGroup1(CMPEQ_OPCODE, right, left));
628 oneShortOp(getOpcodeGroup1(CMPGE_OPCODE, right, left));
631 oneShortOp(getOpcodeGroup1(CMPHS_OPCODE, right, left));
634 oneShortOp(getOpcodeGroup1(CMPHI_OPCODE, right, left));
637 oneShortOp(getOpcodeGroup1(CMPHI_OPCODE, left, right));
640 oneShortOp(getOpcodeGroup1(CMPHS_OPCODE, left, right));
643 oneShortOp(getOpcodeGroup1(CMPGE_OPCODE, left, right));
646 oneShortOp(getOpcodeGroup1(CMPGT_OPCODE, left, right));
649 ASSERT_NOT_REACHED();
653 void cmppl(RegisterID reg)
655 uint16_t opc = getOpcodeGroup2(CMPPL_OPCODE, reg);
659 void cmppz(RegisterID reg)
661 uint16_t opc = getOpcodeGroup2(CMPPZ_OPCODE, reg);
665 void cmpEqImmR0(int imm, RegisterID dst)
667 uint16_t opc = getOpcodeGroup5(CMPEQIMM_OPCODE, imm);
671 void testlRegReg(RegisterID src, RegisterID dst)
673 uint16_t opc = getOpcodeGroup1(TST_OPCODE, dst, src);
677 void testlImm8r(int imm, RegisterID dst)
679 ASSERT((dst == SH4Registers::r0) && (imm <= 255) && (imm >= 0));
681 uint16_t opc = getOpcodeGroup5(TSTIMM_OPCODE, imm);
687 oneShortOp(NOP_OPCODE, false);
692 oneShortOp(SETT_OPCODE);
697 oneShortOp(CLRT_OPCODE);
702 oneShortOp(FSCHG_OPCODE);
707 oneShortOp(BRK_OPCODE, false);
710 void branch(uint16_t opc, int label)
714 ASSERT((label <= 127) && (label >= -128));
715 oneShortOp(getOpcodeGroup5(BT_OPCODE, label));
718 ASSERT((label <= 2047) && (label >= -2048));
719 oneShortOp(getOpcodeGroup6(BRA_OPCODE, label));
722 ASSERT((label <= 127) && (label >= -128));
723 oneShortOp(getOpcodeGroup5(BF_OPCODE, label));
726 ASSERT_NOT_REACHED();
730 void branch(uint16_t opc, RegisterID reg)
734 oneShortOp(getOpcodeGroup2(BRAF_OPCODE, reg));
737 oneShortOp(getOpcodeGroup2(JMP_OPCODE, reg));
740 oneShortOp(getOpcodeGroup2(JSR_OPCODE, reg));
743 oneShortOp(getOpcodeGroup2(BSRF_OPCODE, reg));
746 ASSERT_NOT_REACHED();
750 void ldspr(RegisterID reg)
752 uint16_t opc = getOpcodeGroup2(LDSPR_OPCODE, reg);
756 void stspr(RegisterID reg)
758 uint16_t opc = getOpcodeGroup2(STSPR_OPCODE, reg);
762 void extub(RegisterID src, RegisterID dst)
764 uint16_t opc = getOpcodeGroup1(EXTUB_OPCODE, dst, src);
768 void extuw(RegisterID src, RegisterID dst)
770 uint16_t opc = getOpcodeGroup1(EXTUW_OPCODE, dst, src);
776 void ldsrmfpul(RegisterID src)
778 uint16_t opc = getOpcodeGroup2(LDS_RM_FPUL_OPCODE, src);
782 void fneg(FPRegisterID dst)
784 uint16_t opc = getOpcodeGroup2(FNEG_OPCODE, dst);
785 oneShortOp(opc, true, false);
788 void fsqrt(FPRegisterID dst)
790 uint16_t opc = getOpcodeGroup2(FSQRT_OPCODE, dst);
791 oneShortOp(opc, true, false);
794 void stsfpulReg(RegisterID src)
796 uint16_t opc = getOpcodeGroup2(STS_FPUL_RN_OPCODE, src);
800 void floatfpulfrn(RegisterID src)
802 uint16_t opc = getOpcodeGroup2(FLOAT_OPCODE, src);
803 oneShortOp(opc, true, false);
806 void fmull(FPRegisterID src, FPRegisterID dst)
808 uint16_t opc = getOpcodeGroup1(FMUL_OPCODE, dst, src);
809 oneShortOp(opc, true, false);
812 void fmovsReadrm(RegisterID src, FPRegisterID dst)
814 uint16_t opc = getOpcodeGroup1(FMOVS_READ_RM_OPCODE, dst, src);
815 oneShortOp(opc, true, false);
818 void fmovsWriterm(FPRegisterID src, RegisterID dst)
820 uint16_t opc = getOpcodeGroup1(FMOVS_WRITE_RN_OPCODE, dst, src);
821 oneShortOp(opc, true, false);
824 void fmovsWriter0r(FPRegisterID src, RegisterID dst)
826 uint16_t opc = getOpcodeGroup1(FMOVS_WRITE_R0RN_OPCODE, dst, src);
827 oneShortOp(opc, true, false);
830 void fmovsReadr0r(RegisterID src, FPRegisterID dst)
832 uint16_t opc = getOpcodeGroup1(FMOVS_READ_R0RM_OPCODE, dst, src);
833 oneShortOp(opc, true, false);
836 void fmovsReadrminc(RegisterID src, FPRegisterID dst)
838 uint16_t opc = getOpcodeGroup1(FMOVS_READ_RM_INC_OPCODE, dst, src);
839 oneShortOp(opc, true, false);
842 void fmovsWriterndec(FPRegisterID src, RegisterID dst)
844 uint16_t opc = getOpcodeGroup1(FMOVS_WRITE_RN_DEC_OPCODE, dst, src);
845 oneShortOp(opc, true, false);
848 void ftrcRegfpul(FPRegisterID src)
850 uint16_t opc = getOpcodeGroup2(FTRC_OPCODE, src);
851 oneShortOp(opc, true, false);
854 void fldsfpul(RegisterID src)
856 uint16_t opc = getOpcodeGroup2(FLDS_FRM_FPUL_OPCODE, src);
860 void fstsfpul(RegisterID src)
862 uint16_t opc = getOpcodeGroup2(FSTS_FPUL_FRN_OPCODE, src);
866 void ldsfpscr(RegisterID reg)
868 uint16_t opc = getOpcodeGroup2(LDSFPSCR_OPCODE, reg);
872 void stsfpscr(RegisterID reg)
874 uint16_t opc = getOpcodeGroup2(STSFPSCR_OPCODE, reg);
880 void dcnvds(FPRegisterID src)
882 uint16_t opc = getOpcodeGroup7(FCNVDS_DRM_FPUL_OPCODE, src >> 1);
886 void dcmppeq(FPRegisterID src, FPRegisterID dst)
888 uint16_t opc = getOpcodeGroup8(FCMPEQ_OPCODE, dst >> 1, src >> 1);
892 void dcmppgt(FPRegisterID src, FPRegisterID dst)
894 uint16_t opc = getOpcodeGroup8(FCMPGT_OPCODE, dst >> 1, src >> 1);
898 void dmulRegReg(FPRegisterID src, FPRegisterID dst)
900 uint16_t opc = getOpcodeGroup8(FMUL_OPCODE, dst >> 1, src >> 1);
904 void dsubRegReg(FPRegisterID src, FPRegisterID dst)
906 uint16_t opc = getOpcodeGroup8(FSUB_OPCODE, dst >> 1, src >> 1);
910 void daddRegReg(FPRegisterID src, FPRegisterID dst)
912 uint16_t opc = getOpcodeGroup8(FADD_OPCODE, dst >> 1, src >> 1);
916 void dmovRegReg(FPRegisterID src, FPRegisterID dst)
918 uint16_t opc = getOpcodeGroup8(FMOV_OPCODE, dst >> 1, src >> 1);
922 void ddivRegReg(FPRegisterID src, FPRegisterID dst)
924 uint16_t opc = getOpcodeGroup8(FDIV_OPCODE, dst >> 1, src >> 1);
928 void dsqrt(FPRegisterID dst)
930 uint16_t opc = getOpcodeGroup7(FSQRT_OPCODE, dst >> 1);
934 void dneg(FPRegisterID dst)
936 uint16_t opc = getOpcodeGroup7(FNEG_OPCODE, dst >> 1);
940 void fmovReadrm(RegisterID src, FPRegisterID dst)
942 uint16_t opc = getOpcodeGroup10(FMOVS_READ_RM_OPCODE, dst >> 1, src);
946 void fmovWriterm(FPRegisterID src, RegisterID dst)
948 uint16_t opc = getOpcodeGroup9(FMOVS_WRITE_RN_OPCODE, dst, src >> 1);
952 void fmovWriter0r(FPRegisterID src, RegisterID dst)
954 uint16_t opc = getOpcodeGroup9(FMOVS_WRITE_R0RN_OPCODE, dst, src >> 1);
958 void fmovReadr0r(RegisterID src, FPRegisterID dst)
960 uint16_t opc = getOpcodeGroup10(FMOVS_READ_R0RM_OPCODE, dst >> 1, src);
964 void fmovReadrminc(RegisterID src, FPRegisterID dst)
966 uint16_t opc = getOpcodeGroup10(FMOVS_READ_RM_INC_OPCODE, dst >> 1, src);
970 void fmovWriterndec(FPRegisterID src, RegisterID dst)
972 uint16_t opc = getOpcodeGroup9(FMOVS_WRITE_RN_DEC_OPCODE, dst, src >> 1);
976 void floatfpulDreg(FPRegisterID src)
978 uint16_t opc = getOpcodeGroup7(FLOAT_OPCODE, src >> 1);
982 void ftrcdrmfpul(FPRegisterID src)
984 uint16_t opc = getOpcodeGroup7(FTRC_OPCODE, src >> 1);
990 void movImm8(int imm8, RegisterID dst)
992 ASSERT((imm8 <= 127) && (imm8 >= -128));
994 uint16_t opc = getOpcodeGroup3(MOVIMM_OPCODE, dst, imm8);
998 void movlRegReg(RegisterID src, RegisterID dst)
1000 uint16_t opc = getOpcodeGroup1(MOV_OPCODE, dst, src);
1004 void movwRegMem(RegisterID src, RegisterID dst)
1006 uint16_t opc = getOpcodeGroup1(MOVW_WRITE_RN_OPCODE, dst, src);
1010 void movwMemReg(RegisterID src, RegisterID dst)
1012 uint16_t opc = getOpcodeGroup1(MOVW_READ_RM_OPCODE, dst, src);
1016 void movwPCReg(int offset, RegisterID base, RegisterID dst)
1018 ASSERT(base == SH4Registers::pc);
1019 ASSERT((offset <= 255) && (offset >= 0));
1021 uint16_t opc = getOpcodeGroup3(MOVW_READ_OFFPC_OPCODE, dst, offset);
1025 void movwMemReg(int offset, RegisterID base, RegisterID dst)
1027 ASSERT(dst == SH4Registers::r0);
1029 uint16_t opc = getOpcodeGroup11(MOVW_READ_OFFRM_OPCODE, base, offset);
1033 void movwR0mr(RegisterID src, RegisterID dst)
1035 uint16_t opc = getOpcodeGroup1(MOVW_READ_R0RM_OPCODE, dst, src);
1039 void movlRegMem(RegisterID src, int offset, RegisterID base)
1041 ASSERT((offset <= 15) && (offset >= 0));
1044 oneShortOp(getOpcodeGroup1(MOVL_WRITE_RN_OPCODE, base, src));
1048 oneShortOp(getOpcodeGroup4(MOVL_WRITE_OFFRN_OPCODE, base, src, offset));
1051 void movlRegMem(RegisterID src, RegisterID base)
1053 uint16_t opc = getOpcodeGroup1(MOVL_WRITE_RN_OPCODE, base, src);
1057 void movlMemReg(int offset, RegisterID base, RegisterID dst)
1059 if (base == SH4Registers::pc) {
1060 ASSERT((offset <= 255) && (offset >= 0));
1061 oneShortOp(getOpcodeGroup3(MOVL_READ_OFFPC_OPCODE, dst, offset));
1065 ASSERT((offset <= 15) && (offset >= 0));
1067 oneShortOp(getOpcodeGroup1(MOVL_READ_RM_OPCODE, dst, base));
1071 oneShortOp(getOpcodeGroup4(MOVL_READ_OFFRM_OPCODE, dst, base, offset));
1074 void movlMemRegCompact(int offset, RegisterID base, RegisterID dst)
1076 oneShortOp(getOpcodeGroup4(MOVL_READ_OFFRM_OPCODE, dst, base, offset));
1079 void movbMemReg(int offset, RegisterID base, RegisterID dst)
1081 ASSERT(dst == SH4Registers::r0);
1083 uint16_t opc = getOpcodeGroup11(MOVB_READ_OFFRM_OPCODE, base, offset);
1087 void movbR0mr(RegisterID src, RegisterID dst)
1089 uint16_t opc = getOpcodeGroup1(MOVB_READ_R0RM_OPCODE, dst, src);
1093 void movbMemReg(RegisterID src, RegisterID dst)
1095 uint16_t opc = getOpcodeGroup1(MOVB_READ_RM_OPCODE, dst, src);
1099 void movlMemReg(RegisterID base, RegisterID dst)
1101 uint16_t opc = getOpcodeGroup1(MOVL_READ_RM_OPCODE, dst, base);
1105 void movlMemRegIn(RegisterID base, RegisterID dst)
1107 uint16_t opc = getOpcodeGroup1(MOVL_READ_RMINC_OPCODE, dst, base);
1111 void movlR0mr(RegisterID src, RegisterID dst)
1113 uint16_t opc = getOpcodeGroup1(MOVL_READ_R0RM_OPCODE, dst, src);
1117 void movlRegMemr0(RegisterID src, RegisterID dst)
1119 uint16_t opc = getOpcodeGroup1(MOVL_WRITE_R0RN_OPCODE, dst, src);
1123 void movlImm8r(int imm8, RegisterID dst)
1125 ASSERT((imm8 <= 127) && (imm8 >= -128));
1127 uint16_t opc = getOpcodeGroup3(MOVIMM_OPCODE, dst, imm8);
1131 void loadConstant(uint32_t constant, RegisterID dst)
1133 if (((int)constant <= 0x7f) && ((int)constant >= -0x80)) {
1134 movImm8(constant, dst);
1138 uint16_t opc = getOpcodeGroup3(MOVIMM_OPCODE, dst, 0);
1140 m_buffer.ensureSpace(maxInstructionSize, sizeof(uint32_t));
1141 printInstr(getOpcodeGroup3(MOVIMM_OPCODE, dst, constant), m_buffer.codeSize());
1142 m_buffer.putShortWithConstantInt(opc, constant, true);
1145 void loadConstantUnReusable(uint32_t constant, RegisterID dst, bool ensureSpace = false)
1147 uint16_t opc = getOpcodeGroup3(MOVIMM_OPCODE, dst, 0);
1150 m_buffer.ensureSpace(maxInstructionSize, sizeof(uint32_t));
1152 printInstr(getOpcodeGroup3(MOVIMM_OPCODE, dst, constant), m_buffer.codeSize());
1153 m_buffer.putShortWithConstantInt(opc, constant);
1158 AssemblerLabel call()
1160 RegisterID scr = claimScratch();
1161 m_buffer.ensureSpace(maxInstructionSize + 4, sizeof(uint32_t));
1162 loadConstantUnReusable(0x0, scr);
1163 branch(JSR_OPCODE, scr);
1165 releaseScratch(scr);
1166 return m_buffer.label();
1169 AssemblerLabel call(RegisterID dst)
1171 m_buffer.ensureSpace(maxInstructionSize + 2);
1172 branch(JSR_OPCODE, dst);
1174 return m_buffer.label();
1177 AssemblerLabel jmp()
1179 RegisterID scr = claimScratch();
1180 m_buffer.ensureSpace(maxInstructionSize + 4, sizeof(uint32_t));
1181 AssemblerLabel label = m_buffer.label();
1182 loadConstantUnReusable(0x0, scr);
1183 branch(BRAF_OPCODE, scr);
1185 releaseScratch(scr);
1189 AssemblerLabel jmp(RegisterID dst)
1192 return m_buffer.label();
1195 void jmpReg(RegisterID dst)
1197 m_buffer.ensureSpace(maxInstructionSize + 2);
1198 branch(JMP_OPCODE, dst);
1202 AssemblerLabel jne()
1204 AssemblerLabel label = m_buffer.label();
1205 branch(BF_OPCODE, 0);
1211 AssemblerLabel label = m_buffer.label();
1212 branch(BT_OPCODE, 0);
1218 m_buffer.ensureSpace(maxInstructionSize + 2);
1219 oneShortOp(RTS_OPCODE, false);
1222 AssemblerLabel label()
1224 m_buffer.ensureSpaceForAnyOneInstruction();
1225 return m_buffer.label();
1228 int sizeOfConstantPool()
1230 return m_buffer.sizeOfConstantPool();
1233 AssemblerLabel align(int alignment)
1235 m_buffer.ensureSpace(maxInstructionSize + 2);
1236 while (!m_buffer.isAligned(alignment)) {
1238 m_buffer.ensureSpace(maxInstructionSize + 2);
1243 static void changePCrelativeAddress(int offset, uint16_t* instructionPtr, uint32_t newAddress)
1245 uint32_t address = (offset << 2) + ((reinterpret_cast<uint32_t>(instructionPtr) + 4) &(~0x3));
1246 *reinterpret_cast<uint32_t*>(address) = newAddress;
1249 static uint32_t readPCrelativeAddress(int offset, uint16_t* instructionPtr)
1251 uint32_t address = (offset << 2) + ((reinterpret_cast<uint32_t>(instructionPtr) + 4) &(~0x3));
1252 return *reinterpret_cast<uint32_t*>(address);
1255 static uint16_t* getInstructionPtr(void* code, int offset)
1257 return reinterpret_cast<uint16_t*> (reinterpret_cast<uint32_t>(code) + offset);
1260 static void linkJump(void* code, AssemblerLabel from, void* to)
1262 ASSERT(from.isSet());
1264 uint16_t* instructionPtr = getInstructionPtr(code, from.m_offset);
1265 uint16_t instruction = *instructionPtr;
1266 int offsetBits = (reinterpret_cast<uint32_t>(to) - reinterpret_cast<uint32_t>(code)) - from.m_offset;
1268 if (((instruction & 0xff00) == BT_OPCODE) || ((instruction & 0xff00) == BF_OPCODE)) {
1269 /* BT label ==> BF 2
1275 instruction ^= 0x0202;
1276 *instructionPtr++ = instruction;
1277 changePCrelativeAddress((*instructionPtr & 0xff), instructionPtr, offsetBits);
1278 instruction = (BRAF_OPCODE | (*instructionPtr++ & 0xf00));
1279 *instructionPtr = instruction;
1280 printBlockInstr(instructionPtr - 2, from.m_offset, 3);
1284 /* MOV #imm, reg => LDR reg
1288 ASSERT((*(instructionPtr + 1) & BRAF_OPCODE) == BRAF_OPCODE);
1291 if (offsetBits >= -4096 && offsetBits <= 4094) {
1292 *instructionPtr = getOpcodeGroup6(BRA_OPCODE, offsetBits >> 1);
1293 *(++instructionPtr) = NOP_OPCODE;
1294 printBlockInstr(instructionPtr - 1, from.m_offset, 2);
1298 changePCrelativeAddress((*instructionPtr & 0xff), instructionPtr, offsetBits - 2);
1299 printInstr(*instructionPtr, from.m_offset + 2);
1302 static void linkCall(void* code, AssemblerLabel from, void* to)
1304 uint16_t* instructionPtr = getInstructionPtr(code, from.m_offset);
1305 instructionPtr -= 3;
1306 changePCrelativeAddress((*instructionPtr & 0xff), instructionPtr, reinterpret_cast<uint32_t>(to));
1309 static void linkPointer(void* code, AssemblerLabel where, void* value)
1311 uint16_t* instructionPtr = getInstructionPtr(code, where.m_offset);
1312 changePCrelativeAddress((*instructionPtr & 0xff), instructionPtr, reinterpret_cast<uint32_t>(value));
1315 static unsigned getCallReturnOffset(AssemblerLabel call)
1317 ASSERT(call.isSet());
1318 return call.m_offset;
1321 static uint32_t* getLdrImmAddressOnPool(SH4Word* insn, uint32_t* constPool)
1323 return (constPool + (*insn & 0xff));
1326 static SH4Word patchConstantPoolLoad(SH4Word load, int value)
1328 return ((load & ~0xff) | value);
1331 static SH4Buffer::TwoShorts placeConstantPoolBarrier(int offset)
1333 ASSERT(((offset >> 1) <=2047) && ((offset >> 1) >= -2048));
1335 SH4Buffer::TwoShorts m_barrier;
1336 m_barrier.high = (BRA_OPCODE | (offset >> 1));
1337 m_barrier.low = NOP_OPCODE;
1338 printInstr(((BRA_OPCODE | (offset >> 1))), 0);
1339 printInstr(NOP_OPCODE, 0);
1343 static void patchConstantPoolLoad(void* loadAddr, void* constPoolAddr)
1345 SH4Word* instructionPtr = reinterpret_cast<SH4Word*>(loadAddr);
1346 SH4Word instruction = *instructionPtr;
1347 SH4Word index = instruction & 0xff;
1349 if ((instruction & 0xf000) != MOVIMM_OPCODE)
1352 ASSERT((((reinterpret_cast<uint32_t>(constPoolAddr) - reinterpret_cast<uint32_t>(loadAddr)) + index * 4)) < 1024);
1354 int offset = reinterpret_cast<uint32_t>(constPoolAddr) + (index * 4) - ((reinterpret_cast<uint32_t>(instructionPtr) & ~0x03) + 4);
1355 instruction &=0xf00;
1356 instruction |= 0xd000;
1358 instruction |= (offset >> 2);
1359 *instructionPtr = instruction;
1360 printInstr(instruction, reinterpret_cast<uint32_t>(loadAddr));
1363 static void repatchPointer(void* where, void* value)
1365 patchPointer(where, value);
1368 static void* readPointer(void* code)
1370 return reinterpret_cast<void*>(readInt32(code));
1373 static void repatchInt32(void* where, int32_t value)
1375 uint16_t* instructionPtr = reinterpret_cast<uint16_t*>(where);
1376 changePCrelativeAddress((*instructionPtr & 0xff), instructionPtr, value);
1379 static void repatchCompact(void* where, int32_t value)
1382 ASSERT(value <= 60);
1383 *reinterpret_cast<uint16_t*>(where) = ((*reinterpret_cast<uint16_t*>(where) & 0xfff0) | (value >> 2));
1384 ExecutableAllocator::cacheFlush(reinterpret_cast<uint16_t*>(where), sizeof(uint16_t));
1387 static void relinkCall(void* from, void* to)
1389 uint16_t* instructionPtr = reinterpret_cast<uint16_t*>(from);
1390 instructionPtr -= 3;
1391 changePCrelativeAddress((*instructionPtr & 0xff), instructionPtr, reinterpret_cast<uint32_t>(to));
1394 static void relinkJump(void* from, void* to)
1396 uint16_t* instructionPtr = reinterpret_cast<uint16_t*> (from);
1397 uint16_t instruction = *instructionPtr;
1398 int32_t offsetBits = (reinterpret_cast<uint32_t>(to) - reinterpret_cast<uint32_t>(from));
1400 if (((*instructionPtr & 0xff00) == BT_OPCODE) || ((*instructionPtr & 0xff00) == BF_OPCODE)) {
1403 changePCrelativeAddress((*instructionPtr & 0xff), instructionPtr, offsetBits);
1404 instruction = (BRAF_OPCODE | (*instructionPtr++ & 0xf00));
1405 *instructionPtr = instruction;
1406 printBlockInstr(instructionPtr, reinterpret_cast<uint32_t>(from) + 1, 3);
1410 ASSERT((*(instructionPtr + 1) & BRAF_OPCODE) == BRAF_OPCODE);
1412 if (offsetBits >= -4096 && offsetBits <= 4094) {
1413 *instructionPtr = getOpcodeGroup6(BRA_OPCODE, offsetBits >> 1);
1414 *(++instructionPtr) = NOP_OPCODE;
1415 printBlockInstr(instructionPtr - 2, reinterpret_cast<uint32_t>(from), 2);
1419 changePCrelativeAddress((*instructionPtr & 0xff), instructionPtr, offsetBits - 2);
1420 printInstr(*instructionPtr, reinterpret_cast<uint32_t>(from));
1423 // Linking & patching
1425 void linkJump(AssemblerLabel from, AssemblerLabel to)
1428 ASSERT(from.isSet());
1430 uint16_t* instructionPtr = getInstructionPtr(data(), from.m_offset);
1431 uint16_t instruction = *instructionPtr;
1434 if (((instruction & 0xff00) == BT_OPCODE) || ((instruction & 0xff00) == BF_OPCODE)) {
1440 offsetBits = (to.m_offset - from.m_offset) - 8;
1441 instruction ^= 0x0202;
1442 *instructionPtr++ = instruction;
1443 if ((*instructionPtr & 0xf000) == 0xe000) {
1444 uint32_t* addr = getLdrImmAddressOnPool(instructionPtr, m_buffer.poolAddress());
1447 changePCrelativeAddress((*instructionPtr & 0xff), instructionPtr, offsetBits);
1448 instruction = (BRAF_OPCODE | (*instructionPtr++ & 0xf00));
1449 *instructionPtr = instruction;
1450 printBlockInstr(instructionPtr - 2, from.m_offset, 3);
1454 /* MOV # imm, reg => LDR reg
1458 ASSERT((*(instructionPtr + 1) & BRAF_OPCODE) == BRAF_OPCODE);
1459 offsetBits = (to.m_offset - from.m_offset) - 4;
1460 if (offsetBits >= -4096 && offsetBits <= 4094) {
1461 *instructionPtr = getOpcodeGroup6(BRA_OPCODE, offsetBits >> 1);
1462 *(++instructionPtr) = NOP_OPCODE;
1463 printBlockInstr(instructionPtr - 1, from.m_offset, 2);
1467 instruction = *instructionPtr;
1468 if ((instruction & 0xf000) == 0xe000) {
1469 uint32_t* addr = getLdrImmAddressOnPool(instructionPtr, m_buffer.poolAddress());
1470 *addr = offsetBits - 2;
1471 printInstr(*instructionPtr, from.m_offset + 2);
1475 changePCrelativeAddress((*instructionPtr & 0xff), instructionPtr, offsetBits - 2);
1476 printInstr(*instructionPtr, from.m_offset + 2);
1479 static void* getRelocatedAddress(void* code, AssemblerLabel label)
1481 return reinterpret_cast<void*>(reinterpret_cast<char*>(code) + label.m_offset);
1484 static int getDifferenceBetweenLabels(AssemblerLabel a, AssemblerLabel b)
1486 return b.m_offset - a.m_offset;
1489 static void patchPointer(void* code, AssemblerLabel where, void* value)
1491 patchPointer(reinterpret_cast<uint32_t*>(code) + where.m_offset, value);
1494 static void patchPointer(void* code, void* value)
1496 patchInt32(code, reinterpret_cast<uint32_t>(value));
1499 static void patchInt32(void* code, uint32_t value)
1501 changePCrelativeAddress((*(reinterpret_cast<uint16_t*>(code)) & 0xff), reinterpret_cast<uint16_t*>(code), value);
1504 static uint32_t readInt32(void* code)
1506 return readPCrelativeAddress((*(reinterpret_cast<uint16_t*>(code)) & 0xff), reinterpret_cast<uint16_t*>(code));
1509 void* executableCopy(JSGlobalData& globalData, ExecutablePool* allocator)
1511 return m_buffer.executableCopy(globalData, allocator);
1514 void prefix(uint16_t pre)
1516 m_buffer.putByte(pre);
1519 void oneShortOp(uint16_t opcode, bool checksize = true, bool isDouble = true)
1521 printInstr(opcode, m_buffer.codeSize(), isDouble);
1523 m_buffer.ensureSpace(maxInstructionSize);
1524 m_buffer.putShortUnchecked(opcode);
1527 void ensureSpace(int space)
1529 m_buffer.ensureSpace(space);
1532 void ensureSpace(int insnSpace, int constSpace)
1534 m_buffer.ensureSpace(insnSpace, constSpace);
1537 // Administrative methods
1539 void* data() const { return m_buffer.data(); }
1540 size_t codeSize() const { return m_buffer.codeSize(); }
1542 #ifdef SH4_ASSEMBLER_TRACING
1543 static void printInstr(uint16_t opc, unsigned int size, bool isdoubleInst = true)
1545 if (!getenv("JavaScriptCoreDumpJIT"))
1548 const char *format = 0;
1549 printfStdoutInstr("offset: 0x%8.8x\t", size);
1570 format = " FSCHG\n";
1574 printfStdoutInstr(format);
1577 switch (opc & 0xf0ff) {
1579 format = " *BRAF R%d\n";
1582 format = " DT R%d\n";
1585 format = " CMP/PL R%d\n";
1588 format = " CMP/PZ R%d\n";
1591 format = " *JMP @R%d\n";
1594 format = " *JSR @R%d\n";
1597 format = " LDS R%d, PR\n";
1600 format = " LDS.L @R%d+, PR\n";
1603 format = " MOVT R%d\n";
1606 format = " SHAL R%d\n";
1609 format = " SHAR R%d\n";
1612 format = " SHLL R%d\n";
1615 format = " SHLL2 R%d\n";
1618 format = " SHLL8 R%d\n";
1621 format = " SHLL16 R%d\n";
1624 format = " SHLR R%d\n";
1627 format = " SHLR2 R%d\n";
1630 format = " SHLR8 R%d\n";
1633 format = " SHLR16 R%d\n";
1636 format = " STS PR, R%d\n";
1639 format = " STS.L PR, @-R%d\n";
1641 case LDS_RM_FPUL_OPCODE:
1642 format = " LDS R%d, FPUL\n";
1644 case STS_FPUL_RN_OPCODE:
1645 format = " STS FPUL, R%d \n";
1647 case FLDS_FRM_FPUL_OPCODE:
1648 format = " FLDS FR%d, FPUL\n";
1650 case FSTS_FPUL_FRN_OPCODE:
1651 format = " FSTS FPUL, R%d \n";
1653 case LDSFPSCR_OPCODE:
1654 format = " LDS R%d, FPSCR \n";
1656 case STSFPSCR_OPCODE:
1657 format = " STS FPSCR, R%d \n";
1659 case STSMACL_OPCODE:
1660 format = " STS MACL, R%d \n";
1662 case STSMACH_OPCODE:
1663 format = " STS MACH, R%d \n";
1666 format = " *BSRF R%d";
1669 format = " FTRC FR%d, FPUL\n";
1673 printfStdoutInstr(format, getRn(opc));
1676 switch (opc & 0xf0ff) {
1678 format = " FNEG DR%d\n";
1681 format = " FLOAT DR%d\n";
1684 format = " FTRC FR%d, FPUL\n";
1687 format = " FSQRT FR%d\n";
1689 case FCNVDS_DRM_FPUL_OPCODE:
1690 format = " FCNVDS FR%d, FPUL\n";
1695 printfStdoutInstr(format, getDRn(opc) << 1);
1697 printfStdoutInstr(format, getRn(opc));
1700 switch (opc & 0xf00f) {
1702 format = " ADD R%d, R%d\n";
1705 format = " ADDC R%d, R%d\n";
1708 format = " ADDV R%d, R%d\n";
1711 format = " AND R%d, R%d\n";
1714 format = " DIV1 R%d, R%d\n";
1717 format = " CMP/EQ R%d, R%d\n";
1720 format = " CMP/GE R%d, R%d\n";
1723 format = " CMP/GT R%d, R%d\n";
1726 format = " CMP/HI R%d, R%d\n";
1729 format = " CMP/HS R%d, R%d\n";
1732 format = " MOV R%d, R%d\n";
1734 case MOVB_WRITE_RN_OPCODE:
1735 format = " MOV.B R%d, @R%d\n";
1737 case MOVB_WRITE_RNDEC_OPCODE:
1738 format = " MOV.B R%d, @-R%d\n";
1740 case MOVB_WRITE_R0RN_OPCODE:
1741 format = " MOV.B R%d, @(R0, R%d)\n";
1743 case MOVB_READ_RM_OPCODE:
1744 format = " MOV.B @R%d, R%d\n";
1746 case MOVB_READ_RMINC_OPCODE:
1747 format = " MOV.B @R%d+, R%d\n";
1749 case MOVB_READ_R0RM_OPCODE:
1750 format = " MOV.B @(R0, R%d), R%d\n";
1752 case MOVL_WRITE_RN_OPCODE:
1753 format = " MOV.L R%d, @R%d\n";
1755 case MOVL_WRITE_RNDEC_OPCODE:
1756 format = " MOV.L R%d, @-R%d\n";
1758 case MOVL_WRITE_R0RN_OPCODE:
1759 format = " MOV.L R%d, @(R0, R%d)\n";
1761 case MOVL_READ_RM_OPCODE:
1762 format = " MOV.L @R%d, R%d\n";
1764 case MOVL_READ_RMINC_OPCODE:
1765 format = " MOV.L @R%d+, R%d\n";
1767 case MOVL_READ_R0RM_OPCODE:
1768 format = " MOV.L @(R0, R%d), R%d\n";
1771 format = " MUL.L R%d, R%d\n";
1773 case DMULL_L_OPCODE:
1774 format = " DMULU.L R%d, R%d\n";
1777 format = " DMULS.L R%d, R%d\n";
1780 format = " NEG R%d, R%d\n";
1783 format = " NEGC R%d, R%d\n";
1786 format = " NOT R%d, R%d\n";
1789 format = " OR R%d, R%d\n";
1792 format = " SHAD R%d, R%d\n";
1795 format = " SHLD R%d, R%d\n";
1798 format = " SUB R%d, R%d\n";
1801 format = " SUBC R%d, R%d\n";
1804 format = " SUBV R%d, R%d\n";
1807 format = " TST R%d, R%d\n";
1810 format = " XOR R%d, R%d\n";break;
1811 case MOVW_WRITE_RN_OPCODE:
1812 format = " MOV.W R%d, @R%d\n";
1814 case MOVW_READ_RM_OPCODE:
1815 format = " MOV.W @R%d, R%d\n";
1817 case MOVW_READ_R0RM_OPCODE:
1818 format = " MOV.W @(R0, R%d), R%d\n";
1821 format = " EXTU.B R%d, R%d\n";
1824 format = " EXTU.W R%d, R%d\n";
1828 printfStdoutInstr(format, getRm(opc), getRn(opc));
1831 switch (opc & 0xf00f) {
1833 format = " FSUB FR%d, FR%d\n";
1836 format = " FADD FR%d, FR%d\n";
1839 format = " FDIV FR%d, FR%d\n";
1842 format = " DMULL FR%d, FR%d\n";
1845 format = " FMOV FR%d, FR%d\n";
1848 format = " FCMP/EQ FR%d, FR%d\n";
1851 format = " FCMP/GT FR%d, FR%d\n";
1856 printfStdoutInstr(format, getDRm(opc) << 1, getDRn(opc) << 1);
1858 printfStdoutInstr(format, getRm(opc), getRn(opc));
1861 switch (opc & 0xf00f) {
1862 case FMOVS_WRITE_RN_DEC_OPCODE:
1863 format = " %s FR%d, @-R%d\n";
1865 case FMOVS_WRITE_RN_OPCODE:
1866 format = " %s FR%d, @R%d\n";
1868 case FMOVS_WRITE_R0RN_OPCODE:
1869 format = " %s FR%d, @(R0, R%d)\n";
1874 printfStdoutInstr(format, "FMOV", getDRm(opc) << 1, getDRn(opc));
1876 printfStdoutInstr(format, "FMOV.S", getRm(opc), getRn(opc));
1879 switch (opc & 0xf00f) {
1880 case FMOVS_READ_RM_OPCODE:
1881 format = " %s @R%d, FR%d\n";
1883 case FMOVS_READ_RM_INC_OPCODE:
1884 format = " %s @R%d+, FR%d\n";
1886 case FMOVS_READ_R0RM_OPCODE:
1887 format = " %s @(R0, R%d), FR%d\n";
1892 printfStdoutInstr(format, "FMOV", getDRm(opc), getDRn(opc) << 1);
1894 printfStdoutInstr(format, "FMOV.S", getRm(opc), getRn(opc));
1897 switch (opc & 0xff00) {
1899 format = " BF %d\n";
1902 format = " *BF/S %d\n";
1905 format = " AND #%d, R0\n";
1908 format = " BT %d\n";
1911 format = " *BT/S %d\n";
1913 case CMPEQIMM_OPCODE:
1914 format = " CMP/EQ #%d, R0\n";
1916 case MOVB_WRITE_OFFGBR_OPCODE:
1917 format = " MOV.B R0, @(%d, GBR)\n";
1919 case MOVB_READ_OFFGBR_OPCODE:
1920 format = " MOV.B @(%d, GBR), R0\n";
1922 case MOVL_WRITE_OFFGBR_OPCODE:
1923 format = " MOV.L R0, @(%d, GBR)\n";
1925 case MOVL_READ_OFFGBR_OPCODE:
1926 format = " MOV.L @(%d, GBR), R0\n";
1928 case MOVA_READ_OFFPC_OPCODE:
1929 format = " MOVA @(%d, PC), R0\n";
1932 format = " OR #%d, R0\n";
1935 format = " OR.B #%d, @(R0, GBR)\n";
1938 format = " TST #%d, R0\n";
1941 format = " TST.B %d, @(R0, GBR)\n";
1944 format = " XOR #%d, R0\n";
1947 format = " XOR.B %d, @(R0, GBR)\n";
1951 printfStdoutInstr(format, getImm8(opc));
1954 switch (opc & 0xff00) {
1955 case MOVB_WRITE_OFFRN_OPCODE:
1956 format = " MOV.B R0, @(%d, R%d)\n";
1958 case MOVB_READ_OFFRM_OPCODE:
1959 format = " MOV.B @(%d, R%d), R0\n";
1963 printfStdoutInstr(format, getDisp(opc), getRm(opc));
1966 switch (opc & 0xf000) {
1968 format = " *BRA %d\n";
1971 format = " *BSR %d\n";
1975 printfStdoutInstr(format, getImm12(opc));
1978 switch (opc & 0xf000) {
1979 case MOVL_READ_OFFPC_OPCODE:
1980 format = " MOV.L @(%d, PC), R%d\n";
1983 format = " ADD #%d, R%d\n";
1986 format = " MOV #%d, R%d\n";
1988 case MOVW_READ_OFFPC_OPCODE:
1989 format = " MOV.W @(%d, PC), R%d\n";
1993 printfStdoutInstr(format, getImm8(opc), getRn(opc));
1996 switch (opc & 0xf000) {
1997 case MOVL_WRITE_OFFRN_OPCODE:
1998 format = " MOV.L R%d, @(%d, R%d)\n";
1999 printfStdoutInstr(format, getRm(opc), getDisp(opc), getRn(opc));
2001 case MOVL_READ_OFFRM_OPCODE:
2002 format = " MOV.L @(%d, R%d), R%d\n";
2003 printfStdoutInstr(format, getDisp(opc), getRm(opc), getRn(opc));
2008 static void printfStdoutInstr(const char* format, ...)
2010 if (getenv("JavaScriptCoreDumpJIT")) {
2012 va_start(args, format);
2013 vprintfStdoutInstr(format, args);
2018 static void vprintfStdoutInstr(const char* format, va_list args)
2020 if (getenv("JavaScriptCoreDumpJIT"))
2021 vfprintf(stdout, format, args);
2024 static void printBlockInstr(uint16_t* first, unsigned int offset, int nbInstr)
2026 printfStdoutInstr(">> repatch instructions after link\n");
2027 for (int i = 0; i <= nbInstr; i++)
2028 printInstr(*(first + i), offset + i);
2029 printfStdoutInstr(">> end repatch\n");
2032 static void printInstr(uint16_t opc, unsigned int size, bool isdoubleInst = true) {};
2033 static void printBlockInstr(uint16_t* first, unsigned int offset, int nbInstr) {};
2038 int m_claimscratchReg;
2043 #endif // ENABLE(ASSEMBLER) && CPU(SH4)
2045 #endif // SH4Assembler_h