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