1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 
29 #include <stdlib.h>
30 
31 #include "src/init/v8.h"
32 
33 #include "src/codegen/macro-assembler.h"
34 #include "src/debug/debug.h"
35 #include "src/diagnostics/disasm.h"
36 #include "src/diagnostics/disassembler.h"
37 #include "src/execution/frames-inl.h"
38 #include "test/cctest/cctest.h"
39 
40 namespace v8 {
41 namespace internal {
42 
DisassembleAndCompare(byte * pc,const char * compare_string)43 bool DisassembleAndCompare(byte* pc, const char* compare_string) {
44   disasm::NameConverter converter;
45   disasm::Disassembler disasm(converter);
46   base::EmbeddedVector<char, 128> disasm_buffer;
47 
48   disasm.InstructionDecode(disasm_buffer, pc);
49 
50   if (strcmp(compare_string, disasm_buffer.begin()) != 0) {
51     fprintf(stderr,
52             "expected: \n"
53             "%s\n"
54             "disassembled: \n"
55             "%s\n\n",
56             compare_string, disasm_buffer.begin());
57     return false;
58   }
59   return true;
60 }
61 
62 // Set up V8 to a state where we can at least run the assembler and
63 // disassembler. Declare the variables and allocate the data structures used
64 // in the rest of the macros.
65 #define SET_UP()                                             \
66   CcTest::InitializeVM();                                    \
67   Isolate* isolate = CcTest::i_isolate();                    \
68   HandleScope scope(isolate);                                \
69   byte* buffer = reinterpret_cast<byte*>(malloc(4 * 1024));  \
70   Assembler assm(AssemblerOptions{},                         \
71                  ExternalAssemblerBuffer(buffer, 4 * 1024)); \
72   bool failure = false;
73 
74 // This macro assembles one instruction using the preallocated assembler and
75 // disassembles the generated instruction, comparing the output to the expected
76 // value. If the comparison fails an error message is printed, but the test
77 // continues to run until the end.
78 #define COMPARE(asm_, compare_string)                                        \
79   {                                                                          \
80     int pc_offset = assm.pc_offset();                                        \
81     byte* progcounter = &buffer[pc_offset];                                  \
82     assm.asm_;                                                               \
83     if (!DisassembleAndCompare(progcounter, compare_string)) failure = true; \
84   }
85 
86 // Force emission of any pending literals into a pool.
87 #define EMIT_PENDING_LITERALS() assm.CheckConstPool(true, false)
88 
89 // Verify that all invocations of the COMPARE macro passed successfully.
90 // Exit with a failure if at least one of the tests failed.
91 #define VERIFY_RUN()                            \
92   if (failure) {                                \
93     FATAL("S390 Disassembler tests failed.\n"); \
94   }
95 
TEST(TwoBytes)96 TEST(TwoBytes) {
97   SET_UP();
98 
99   COMPARE(ar(r3, r10), "1a3a           ar\tr3,r10");
100   COMPARE(sr(r8, ip), "1b8c           sr\tr8,ip");
101   COMPARE(mr_z(r0, r6), "1c06           mr\tr0,r6");
102   COMPARE(dr(r0, r5), "1d05           dr\tr0,r5");
103   COMPARE(or_z(r4, r2), "1642           or\tr4,r2");
104   COMPARE(nr(fp, r9), "14b9           nr\tfp,r9");
105   COMPARE(xr(r10, ip), "17ac           xr\tr10,ip");
106   COMPARE(lr(r2, r13), "182d           lr\tr2,r13");
107   COMPARE(cr_z(r9, r3), "1993           cr\tr9,r3");
108   COMPARE(clr(sp, r4), "15f4           clr\tsp,r4");
109   COMPARE(bcr(eq, r8), "0788           bcr\t0x8,r8");
110   COMPARE(ltr(r10, r1), "12a1           ltr\tr10,r1");
111   COMPARE(alr(r6, r8), "1e68           alr\tr6,r8");
112   COMPARE(slr(r3, ip), "1f3c           slr\tr3,ip");
113   COMPARE(lnr(r4, r1), "1141           lnr\tr4,r1");
114   COMPARE(lcr(r0, r3), "1303           lcr\tr0,r3");
115   COMPARE(basr(r14, r7), "0de7           basr\tr14,r7");
116   COMPARE(ldr(d4, d6), "2846           ldr\td4,d6");
117 
118   VERIFY_RUN();
119 }
120 
TEST(FourBytes)121 TEST(FourBytes) {
122   SET_UP();
123 
124 #if V8_TARGET_ARCH_S390X
125   COMPARE(aghi(r5, Operand(1)), "a75b0001       aghi\tr5,1");
126   COMPARE(lghi(r6, Operand(8)), "a7690008       lghi\tr6,8");
127   COMPARE(mghi(r1, Operand(2)), "a71d0002       mghi\tr1,2");
128   COMPARE(cghi(r3, Operand(7)), "a73f0007       cghi\tr3,7");
129 #else
130   COMPARE(ahi(r3, Operand(9)), "a73a0009       ahi\tr3,9");
131   COMPARE(lhi(r7, Operand::Zero()), "a7780000       lhi\tr7,0");
132   COMPARE(mhi(r8, Operand(3)), "a78c0003       mhi\tr8,3");
133   COMPARE(chi(r4, Operand(5)), "a74e0005       chi\tr4,5");
134   COMPARE(a(r13, MemOperand(r8, 70)), "5ad08046       a\tr13,70(r8)");
135   COMPARE(s(r9, MemOperand(sp, 9)), "5b90f009       s\tr9,9(sp)");
136   COMPARE(m(r6, MemOperand(r7, ip, 20)), "5c67c014       m\tr6,20(r7,ip)");
137   COMPARE(d(r14, MemOperand(r7, 15)), "5de0700f       d\tr14,15(r7)");
138   COMPARE(o(r7, MemOperand(r3, r2, 10)), "5673200a       o\tr7,10(r3,r2)");
139   COMPARE(n(r9, MemOperand(r5, sp, 9)), "5495f009       n\tr9,9(r5,sp)");
140   COMPARE(l(r0, MemOperand(r4, fp, 1)), "5804b001       l\tr0,1(r4,fp)");
141   COMPARE(c(r8, MemOperand(r5, r7, 18)), "59857012       c\tr8,18(r5,r7)");
142   COMPARE(al_z(r6, MemOperand(r9, sp, 2000)),
143           "5e69f7d0       al\tr6,2000(r9,sp)");
144   COMPARE(sl(r8, MemOperand(r1, 100)), "5f801064       sl\tr8,100(r1)");
145   COMPARE(la(r5, MemOperand(r9, 9)), "41509009       la\tr5,9(r9)");
146   COMPARE(ch(r0, MemOperand(r3, 0)), "49003000       ch\tr0,0(r3)");
147   COMPARE(cl(r1, MemOperand(r8, 5)), "55108005       cl\tr1,5(r8)");
148   COMPARE(cli(MemOperand(r9, 64), Operand(5)), "95059040       cli\t64(r9),5");
149   COMPARE(tm(MemOperand(r0, 8), Operand(7)), "91070008       tm\t8(r0),7");
150   COMPARE(bct(r1, MemOperand(r9, 15)), "4610900f       bct\tr1,15(r9)");
151   COMPARE(st(r5, MemOperand(r9, r8, 7)), "50598007       st\tr5,7(r9,r8)");
152   COMPARE(stc(r13, MemOperand(r5, r1, 8)), "42d51008       stc\tr13,8(r5,r1)");
153   COMPARE(ic_z(r9, MemOperand(r1, 90)), "4390105a       ic\tr9,90(r1)");
154   COMPARE(sth(r5, MemOperand(r9, r0, 87)), "40590057       sth\tr5,87(r9,r0)");
155 #endif
156   COMPARE(iihh(r10, Operand(8)), "a5a00008       iihh\tr10,8");
157   COMPARE(iihl(r9, Operand(10)), "a591000a       iihl\tr9,10");
158   COMPARE(iilh(r0, Operand(40)), "a5020028       iilh\tr0,40");
159   COMPARE(iill(r6, Operand(19)), "a5630013       iill\tr6,19");
160   COMPARE(oill(r9, Operand(9)), "a59b0009       oill\tr9,9");
161   COMPARE(tmll(r4, Operand(7)), "a7410007       tmll\tr4,7");
162   COMPARE(stm(r2, r5, MemOperand(r9, 44)), "9025902c       stm\tr2,r5,44(r9)");
163   COMPARE(lm(r8, r0, MemOperand(sp, 88)), "9880f058       lm\tr8,r0,88(sp)");
164   COMPARE(nill(r7, Operand(30)), "a577001e       nill\tr7,30");
165   COMPARE(nilh(r8, Operand(4)), "a5860004       nilh\tr8,4");
166   COMPARE(ah(r9, MemOperand(r5, r4, 4)), "4a954004       ah\tr9,4(r5,r4)");
167   COMPARE(sh(r8, MemOperand(r1, r2, 6)), "4b812006       sh\tr8,6(r1,r2)");
168   COMPARE(mh(r5, MemOperand(r9, r8, 7)), "4c598007       mh\tr5,7(r9,r8)");
169 
170   VERIFY_RUN();
171 }
172 
TEST(SixBytes)173 TEST(SixBytes) {
174   SET_UP();
175 
176 #if V8_TARGET_ARCH_S390X
177   COMPARE(llihf(ip, Operand(90000)), "c0ce00015f90   llihf\tip,90000");
178   COMPARE(agsi(MemOperand(r9, 1000), Operand(70)),
179           "eb4693e8007a   agsi\t1000(r9),70");
180   COMPARE(clgfi(r7, Operand(80)), "c27e00000050   clgfi\tr7,80");
181   COMPARE(cgfi(r8, Operand(10)), "c28c0000000a   cgfi\tr8,10");
182   COMPARE(xihf(fp, Operand(8)), "c0b600000008   xihf\tfp,8");
183   COMPARE(sllg(r0, r1, r2), "eb012000000d   sllg\tr0,r1,0(r2)");
184   COMPARE(sllg(r0, r1, Operand(10)), "eb01000a000d   sllg\tr0,r1,10(r0)");
185   COMPARE(srlg(r1, r3, Operand(10)), "eb13000a000c   srlg\tr1,r3,10(r0)");
186   COMPARE(srlg(r1, r3, r10), "eb13a000000c   srlg\tr1,r3,0(r10)");
187   COMPARE(slag(r1, r3, Operand(2)), "eb130002000b   slag\tr1,r3,2(r0)");
188   COMPARE(slag(r1, r3, r2), "eb132000000b   slag\tr1,r3,0(r2)");
189   COMPARE(srag(r1, r3, r2), "eb132000000a   srag\tr1,r3,0(r2)");
190   COMPARE(srag(r1, r3, Operand(2)), "eb130002000a   srag\tr1,r3,2(r0)");
191   COMPARE(risbg(r1, r2, Operand(3), Operand(5), Operand(2)),
192           "ec1203050255   risbg\tr1,r2,3,5,2");
193   COMPARE(risbgn(r1, r2, Operand(3), Operand(5), Operand(2)),
194           "ec1203050259   risbgn\tr1,r2,3,5,2");
195   COMPARE(stmg(r3, r4, MemOperand(sp, 10)),
196           "eb34f00a0024   stmg\tr3,r4,10(sp)");
197   COMPARE(ltg(r1, MemOperand(r4, sp, 10)), "e314f00a0002   ltg\tr1,10(r4,sp)");
198   COMPARE(lgh(r8, MemOperand(r1, 8888)), "e38012b80215   lgh\tr8,8888(r1)");
199   COMPARE(ag(r4, MemOperand(r9, r4, 2046)),
200           "e34947fe0008   ag\tr4,2046(r9,r4)");
201   COMPARE(agf(r1, MemOperand(r3, sp, 9)), "e313f0090018   agf\tr1,9(r3,sp)");
202   COMPARE(sg(r9, MemOperand(r5, 15)), "e390500f0009   sg\tr9,15(r5)");
203   COMPARE(ng(r7, MemOperand(r5, r6, 1000)),
204           "e37563e80080   ng\tr7,1000(r5,r6)");
205   COMPARE(og(r2, MemOperand(r8, r0, 1000)),
206           "e32803e80081   og\tr2,1000(r8,r0)");
207   COMPARE(xg(r9, MemOperand(r3, 8888)), "e39032b80282   xg\tr9,8888(r3)");
208   COMPARE(ng(r0, MemOperand(r9, r3, 900)), "e30933840080   ng\tr0,900(r9,r3)");
209   COMPARE(og(r3, MemOperand(r8, r2, 8888)),
210           "e33822b80281   og\tr3,8888(r8,r2)");
211   COMPARE(xg(r9, MemOperand(r3, 15)), "e390300f0082   xg\tr9,15(r3)");
212   COMPARE(cg(r0, MemOperand(r5, r4, 4)), "e30540040020   cg\tr0,4(r5,r4)");
213   COMPARE(lg(r1, MemOperand(r7, r8, 90)), "e317805a0004   lg\tr1,90(r7,r8)");
214   COMPARE(lgf(r1, MemOperand(sp, 15)), "e310f00f0014   lgf\tr1,15(sp)");
215   COMPARE(llgf(r0, MemOperand(r3, r4, 8)), "e30340080016   llgf\tr0,8(r3,r4)");
216   COMPARE(alg(r8, MemOperand(r4, 11)), "e380400b000a   alg\tr8,11(r4)");
217   COMPARE(slg(r1, MemOperand(r5, r6, 11)), "e315600b000b   slg\tr1,11(r5,r6)");
218   COMPARE(sgf(r0, MemOperand(r4, r5, 8888)),
219           "e30452b80219   sgf\tr0,8888(r4,r5)");
220   COMPARE(llgh(r4, MemOperand(r1, 8000)), "e3401f400191   llgh\tr4,8000(r1)");
221   COMPARE(llgc(r0, MemOperand(r4, r5, 30)),
222           "e304501e0090   llgc\tr0,30(r4,r5)");
223   COMPARE(lgb(r9, MemOperand(r8, r7, 10)), "e398700a0077   lgb\tr9,10(r8,r7)");
224   COMPARE(stg(r0, MemOperand(r9, 10)), "e300900a0024   stg\tr0,10(r9)");
225   COMPARE(mvghi(MemOperand(r7, 25), Operand(100)),
226           "e54870190064   mvghi\t25(r7),100");
227   COMPARE(algfi(r1, Operand(34250)), "c21a000085ca   algfi\tr1,34250");
228   COMPARE(slgfi(r1, Operand(87654321)), "c21405397fb1   slgfi\tr1,87654321");
229   COMPARE(nihf(r2, Operand(8888)), "c02a000022b8   nihf\tr2,8888");
230   COMPARE(oihf(r6, Operand(9000)), "c06c00002328   oihf\tr6,9000");
231   COMPARE(msgfi(r6, Operand(90000)), "c26000015f90   msgfi\tr6,90000");
232 #else
233   COMPARE(llilf(r10, Operand(72354)), "c0af00011aa2   llilf\tr10,72354");
234   COMPARE(iilf(r4, Operand(11)), "c0490000000b   iilf\tr4,11");
235   COMPARE(afi(r2, Operand(8000)), "c22900001f40   afi\tr2,8000");
236   COMPARE(asi(MemOperand(r9, 1000), Operand(70)),
237           "eb4693e8006a   asi\t1000(r9),70");
238   COMPARE(alfi(r1, Operand(90)), "c21b0000005a   alfi\tr1,90");
239   COMPARE(clfi(r9, Operand(60)), "c29f0000003c   clfi\tr9,60");
240   COMPARE(cfi(r8, Operand(10)), "c28d0000000a   cfi\tr8,10");
241   COMPARE(xilf(r3, Operand(15)), "c0370000000f   xilf\tr3,15");
242   COMPARE(sllk(r6, r7, Operand(10)), "eb67000a00df   sllk\tr6,r7,10(r0)");
243   COMPARE(sllk(r6, r7, r8), "eb67800000df   sllk\tr6,r7,0(r8)");
244   COMPARE(slak(r1, r3, r2), "eb13200000dd   slak\tr1,r3,0(r2)");
245   COMPARE(slak(r1, r3, Operand(2)), "eb13000200dd   slak\tr1,r3,2(r0)");
246   COMPARE(srak(r1, r3, Operand(2)), "eb13000200dc   srak\tr1,r3,2(r0)");
247   COMPARE(srak(r1, r3, r2), "eb13200000dc   srak\tr1,r3,0(r2)");
248   COMPARE(stmy(r3, r4, MemOperand(sp, 10)),
249           "eb34f00a0090   stmy\tr3,r4,10(sp)");
250   COMPARE(lt_z(r1, MemOperand(r4, sp, 10)), "e314f00a0012   lt\tr1,10(r4,sp)");
251   COMPARE(ml(r0, MemOperand(r3, r9, 2046)),
252           "e30397fe0096   ml\tr0,2046(r3,r9)");
253   COMPARE(ay(r5, MemOperand(r7, 8888)), "e35072b8025a   ay\tr5,8888(r7)");
254   COMPARE(sy(r8, MemOperand(r6, r7, 2046)),
255           "e38677fe005b   sy\tr8,2046(r6,r7)");
256   COMPARE(ny(r2, MemOperand(r9, r0, 8888)),
257           "e32902b80254   ny\tr2,8888(r9,r0)");
258   COMPARE(oy(r8, MemOperand(r4, 321)), "e38041410056   oy\tr8,321(r4)");
259   COMPARE(xy(r5, MemOperand(r3, r2, 0)), "e35320000057   xy\tr5,0(r3,r2)");
260   COMPARE(cy(r9, MemOperand(r4, 321)), "e39041410059   cy\tr9,321(r4)");
261   COMPARE(ahy(r1, MemOperand(r5, r6, 8888)),
262           "e31562b8027a   ahy\tr1,8888(r5,r6)");
263   COMPARE(shy(r1, MemOperand(r5, r6, 8888)),
264           "e31562b8027b   shy\tr1,8888(r5,r6)");
265   COMPARE(lb(r7, MemOperand(sp, 15)), "e370f00f0076   lb\tr7,15(sp)");
266   COMPARE(ly(r0, MemOperand(r1, r2, 321)), "e30121410058   ly\tr0,321(r1,r2)");
267   COMPARE(aly(r9, MemOperand(r2, r3, 10)), "e392300a005e   aly\tr9,10(r2,r3)");
268   COMPARE(sly(r2, MemOperand(r9, r1, 15)), "e329100f005f   sly\tr2,15(r9,r1)");
269   COMPARE(llh(r4, MemOperand(r1, 10)), "e340100a0095   llh\tr4,10(r1)");
270   COMPARE(llc(r0, MemOperand(r4, r5, 30)), "e304501e0094   llc\tr0,30(r4,r5)");
271   COMPARE(chy(r9, MemOperand(r8, r7, 30)), "e398701e0079   chy\tr9,30(r8,r7)");
272   COMPARE(cly(r8, MemOperand(r5, r4, 14)), "e385400e0055   cly\tr8,14(r5,r4)");
273   COMPARE(sty(r0, MemOperand(r0, 15)), "e300000f0050   sty\tr0,15(r0)");
274   COMPARE(mvhi(MemOperand(r7, 25), Operand(100)),
275           "e54c70190064   mvhi\t25(r7),100");
276   COMPARE(slfi(r4, Operand(100)), "c24500000064   slfi\tr4,100");
277   COMPARE(msfi(r8, Operand(1000)), "c281000003e8   msfi\tr8,1000");
278 #endif
279   COMPARE(iihf(r6, Operand(9)), "c06800000009   iihf\tr6,9");
280   COMPARE(srlk(r1, r3, r2), "eb13200000de   srlk\tr1,r3,0(r2)");
281   COMPARE(srlk(r1, r3, Operand(2)), "eb13000200de   srlk\tr1,r3,2(r0)");
282   COMPARE(lmy(r9, r10, MemOperand(r8, 100)),
283           "eb9a80640098   lmy\tr9,r10,100(r8)");
284   COMPARE(lmg(r7, r8, MemOperand(r9, 100)),
285           "eb7890640004   lmg\tr7,r8,100(r9)");
286   COMPARE(lay(fp, MemOperand(sp, 8000)), "e3b0ff400171   lay\tfp,8000(sp)");
287   COMPARE(cliy(MemOperand(sp, 80), Operand(80)),
288           "eb50f0500055   cliy\t80(sp),80");
289   COMPARE(tmy(MemOperand(r0, 20), Operand(10)),
290           "eb0a00140051   tmy\t20(r0),10");
291   COMPARE(clg(r9, MemOperand(r6, r7, 19)), "e39670130021   clg\tr9,19(r6,r7)");
292   COMPARE(bctg(r8, MemOperand(sp, 10)), "e380f00a0046   bctg\tr8,10(sp)");
293   COMPARE(icy(r2, MemOperand(r3, 2)), "e32030020073   icy\tr2,2(r3)");
294   COMPARE(mvc(MemOperand(r9, 9), MemOperand(r3, 15), Operand(10)),
295           "d20a9009300f   mvc\t9(10,r9),15(r3)");
296   COMPARE(nilf(r0, Operand(8000)), "c00b00001f40   nilf\tr0,8000");
297   COMPARE(oilf(r9, Operand(1000)), "c09d000003e8   oilf\tr9,1000");
298 
299   VERIFY_RUN();
300 }
301 
302 #undef SET_UP
303 #undef COMPARE
304 #undef EMIT_PENDING_LITERALS
305 #undef VERIFY_RUN
306 
307 }  // namespace internal
308 }  // namespace v8
309