deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / test / cctest / test-disasm-mips.cc
1 // Copyright 2012 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.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
42 bool DisassembleAndCompare(byte* pc, const char* compare_string) {
43   disasm::NameConverter converter;
44   disasm::Disassembler disasm(converter);
45   EmbeddedVector<char, 128> disasm_buffer;
46
47   disasm.InstructionDecode(disasm_buffer, pc);
48
49   if (strcmp(compare_string, disasm_buffer.start()) != 0) {
50     fprintf(stderr,
51             "expected: \n"
52             "%s\n"
53             "disassembled: \n"
54             "%s\n\n",
55             compare_string, disasm_buffer.start());
56     return false;
57   }
58   return true;
59 }
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(isolate, buffer, 4*1024);                \
71   bool failure = false;
72
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
87 // Verify that all invocations of the COMPARE macro passed successfully.
88 // Exit with a failure if at least one of the tests failed.
89 #define VERIFY_RUN() \
90 if (failure) { \
91     V8_Fatal(__FILE__, __LINE__, "MIPS Disassembler tests failed.\n"); \
92   }
93
94
95 TEST(Type0) {
96   SET_UP();
97
98   COMPARE(addu(a0, a1, a2),
99           "00a62021       addu    a0, a1, a2");
100   COMPARE(addu(t2, t3, t4),
101           "016c5021       addu    t2, t3, t4");
102   COMPARE(addu(v0, v1, s0),
103           "00701021       addu    v0, v1, s0");
104
105   COMPARE(subu(a0, a1, a2),
106           "00a62023       subu    a0, a1, a2");
107   COMPARE(subu(t2, t3, t4),
108           "016c5023       subu    t2, t3, t4");
109   COMPARE(subu(v0, v1, s0),
110           "00701023       subu    v0, v1, s0");
111
112   if (!IsMipsArchVariant(kMips32r6)) {
113     COMPARE(mult(a0, a1),
114             "00850018       mult    a0, a1");
115     COMPARE(mult(t2, t3),
116             "014b0018       mult    t2, t3");
117     COMPARE(mult(v0, v1),
118             "00430018       mult    v0, v1");
119
120     COMPARE(multu(a0, a1),
121             "00850019       multu   a0, a1");
122     COMPARE(multu(t2, t3),
123             "014b0019       multu   t2, t3");
124     COMPARE(multu(v0, v1),
125             "00430019       multu   v0, v1");
126
127     COMPARE(div(a0, a1),
128             "0085001a       div     a0, a1");
129     COMPARE(div(t2, t3),
130             "014b001a       div     t2, t3");
131     COMPARE(div(v0, v1),
132             "0043001a       div     v0, v1");
133
134     COMPARE(divu(a0, a1),
135             "0085001b       divu    a0, a1");
136     COMPARE(divu(t2, t3),
137             "014b001b       divu    t2, t3");
138     COMPARE(divu(v0, v1),
139             "0043001b       divu    v0, v1");
140
141     if (!IsMipsArchVariant(kLoongson)) {
142       COMPARE(mul(a0, a1, a2),
143               "70a62002       mul     a0, a1, a2");
144       COMPARE(mul(t2, t3, t4),
145               "716c5002       mul     t2, t3, t4");
146       COMPARE(mul(v0, v1, s0),
147               "70701002       mul     v0, v1, s0");
148     }
149   } else {  // MIPS32r6.
150     COMPARE(mul(a0, a1, a2),
151             "00a62098       mul    a0, a1, a2");
152     COMPARE(muh(a0, a1, a2),
153             "00a620d8       muh    a0, a1, a2");
154     COMPARE(mul(t1, t2, t3),
155             "014b4898       mul    t1, t2, t3");
156     COMPARE(muh(t1, t2, t3),
157             "014b48d8       muh    t1, t2, t3");
158     COMPARE(mul(v0, v1, a0),
159             "00641098       mul    v0, v1, a0");
160     COMPARE(muh(v0, v1, a0),
161             "006410d8       muh    v0, v1, a0");
162
163     COMPARE(mulu(a0, a1, a2),
164             "00a62099       mulu   a0, a1, a2");
165     COMPARE(muhu(a0, a1, a2),
166             "00a620d9       muhu   a0, a1, a2");
167     COMPARE(mulu(t1, t2, t3),
168             "014b4899       mulu   t1, t2, t3");
169     COMPARE(muhu(t1, t2, t3),
170             "014b48d9       muhu   t1, t2, t3");
171     COMPARE(mulu(v0, v1, a0),
172             "00641099       mulu   v0, v1, a0");
173     COMPARE(muhu(v0, v1, a0),
174             "006410d9       muhu   v0, v1, a0");
175
176     COMPARE(div(a0, a1, a2),
177             "00a6209a       div    a0, a1, a2");
178     COMPARE(mod(a0, a1, a2),
179             "00a620da       mod    a0, a1, a2");
180     COMPARE(div(t1, t2, t3),
181             "014b489a       div    t1, t2, t3");
182     COMPARE(mod(t1, t2, t3),
183             "014b48da       mod    t1, t2, t3");
184     COMPARE(div(v0, v1, a0),
185             "0064109a       div    v0, v1, a0");
186     COMPARE(mod(v0, v1, a0),
187             "006410da       mod    v0, v1, a0");
188
189     COMPARE(divu(a0, a1, a2),
190             "00a6209b       divu   a0, a1, a2");
191     COMPARE(modu(a0, a1, a2),
192             "00a620db       modu   a0, a1, a2");
193     COMPARE(divu(t1, t2, t3),
194             "014b489b       divu   t1, t2, t3");
195     COMPARE(modu(t1, t2, t3),
196             "014b48db       modu   t1, t2, t3");
197     COMPARE(divu(v0, v1, a0),
198             "0064109b       divu   v0, v1, a0");
199     COMPARE(modu(v0, v1, a0),
200             "006410db       modu   v0, v1, a0");
201
202     COMPARE(bovc(a0, a0, static_cast<int16_t>(0)),
203             "20840000       bovc  a0, a0, 0");
204     COMPARE(bovc(a1, a0, static_cast<int16_t>(0)),
205             "20a40000       bovc  a1, a0, 0");
206     COMPARE(bovc(a1, a0, 32767),
207             "20a47fff       bovc  a1, a0, 32767");
208     COMPARE(bovc(a1, a0, -32768),
209             "20a48000       bovc  a1, a0, -32768");
210
211     COMPARE(bnvc(a0, a0, static_cast<int16_t>(0)),
212             "60840000       bnvc  a0, a0, 0");
213     COMPARE(bnvc(a1, a0, static_cast<int16_t>(0)),
214             "60a40000       bnvc  a1, a0, 0");
215     COMPARE(bnvc(a1, a0, 32767),
216             "60a47fff       bnvc  a1, a0, 32767");
217     COMPARE(bnvc(a1, a0, -32768),
218             "60a48000       bnvc  a1, a0, -32768");
219
220     COMPARE(beqzc(a0, 0),
221             "d8800000       beqzc   a0, 0x0");
222     COMPARE(beqzc(a0, 0xfffff),                   // 0x0fffff ==  1048575.
223             "d88fffff       beqzc   a0, 0xfffff");
224     COMPARE(beqzc(a0, 0x100000),                  // 0x100000 == -1048576.
225             "d8900000       beqzc   a0, 0x100000");
226
227     COMPARE(bnezc(a0, 0),
228             "f8800000       bnezc   a0, 0x0");
229     COMPARE(bnezc(a0, 0xfffff),                   // 0x0fffff ==  1048575.
230             "f88fffff       bnezc   a0, 0xfffff");
231     COMPARE(bnezc(a0, 0x100000),                  // 0x100000 == -1048576.
232             "f8900000       bnezc   a0, 0x100000");
233   }
234
235   COMPARE(addiu(a0, a1, 0x0),
236           "24a40000       addiu   a0, a1, 0");
237   COMPARE(addiu(s0, s1, 32767),
238           "26307fff       addiu   s0, s1, 32767");
239   COMPARE(addiu(t2, t3, -32768),
240           "256a8000       addiu   t2, t3, -32768");
241   COMPARE(addiu(v0, v1, -1),
242           "2462ffff       addiu   v0, v1, -1");
243
244   COMPARE(and_(a0, a1, a2),
245           "00a62024       and     a0, a1, a2");
246   COMPARE(and_(s0, s1, s2),
247           "02328024       and     s0, s1, s2");
248   COMPARE(and_(t2, t3, t4),
249           "016c5024       and     t2, t3, t4");
250   COMPARE(and_(v0, v1, a2),
251           "00661024       and     v0, v1, a2");
252
253   COMPARE(or_(a0, a1, a2),
254           "00a62025       or      a0, a1, a2");
255   COMPARE(or_(s0, s1, s2),
256           "02328025       or      s0, s1, s2");
257   COMPARE(or_(t2, t3, t4),
258           "016c5025       or      t2, t3, t4");
259   COMPARE(or_(v0, v1, a2),
260           "00661025       or      v0, v1, a2");
261
262   COMPARE(xor_(a0, a1, a2),
263           "00a62026       xor     a0, a1, a2");
264   COMPARE(xor_(s0, s1, s2),
265           "02328026       xor     s0, s1, s2");
266   COMPARE(xor_(t2, t3, t4),
267           "016c5026       xor     t2, t3, t4");
268   COMPARE(xor_(v0, v1, a2),
269           "00661026       xor     v0, v1, a2");
270
271   COMPARE(nor(a0, a1, a2),
272           "00a62027       nor     a0, a1, a2");
273   COMPARE(nor(s0, s1, s2),
274           "02328027       nor     s0, s1, s2");
275   COMPARE(nor(t2, t3, t4),
276           "016c5027       nor     t2, t3, t4");
277   COMPARE(nor(v0, v1, a2),
278           "00661027       nor     v0, v1, a2");
279
280   COMPARE(andi(a0, a1, 0x1),
281           "30a40001       andi    a0, a1, 0x1");
282   COMPARE(andi(v0, v1, 0xffff),
283           "3062ffff       andi    v0, v1, 0xffff");
284
285   COMPARE(ori(a0, a1, 0x1),
286           "34a40001       ori     a0, a1, 0x1");
287   COMPARE(ori(v0, v1, 0xffff),
288           "3462ffff       ori     v0, v1, 0xffff");
289
290   COMPARE(xori(a0, a1, 0x1),
291           "38a40001       xori    a0, a1, 0x1");
292   COMPARE(xori(v0, v1, 0xffff),
293           "3862ffff       xori    v0, v1, 0xffff");
294
295   COMPARE(lui(a0, 0x1),
296           "3c040001       lui     a0, 0x1");
297   COMPARE(lui(v0, 0xffff),
298           "3c02ffff       lui     v0, 0xffff");
299
300   COMPARE(sll(a0, a1, 0),
301           "00052000       sll     a0, a1, 0");
302   COMPARE(sll(s0, s1, 8),
303           "00118200       sll     s0, s1, 8");
304   COMPARE(sll(t2, t3, 24),
305           "000b5600       sll     t2, t3, 24");
306   COMPARE(sll(v0, v1, 31),
307           "000317c0       sll     v0, v1, 31");
308
309   COMPARE(sllv(a0, a1, a2),
310           "00c52004       sllv    a0, a1, a2");
311   COMPARE(sllv(s0, s1, s2),
312           "02518004       sllv    s0, s1, s2");
313   COMPARE(sllv(t2, t3, t4),
314           "018b5004       sllv    t2, t3, t4");
315   COMPARE(sllv(v0, v1, fp),
316           "03c31004       sllv    v0, v1, fp");
317
318   COMPARE(srl(a0, a1, 0),
319           "00052002       srl     a0, a1, 0");
320   COMPARE(srl(s0, s1, 8),
321           "00118202       srl     s0, s1, 8");
322   COMPARE(srl(t2, t3, 24),
323           "000b5602       srl     t2, t3, 24");
324   COMPARE(srl(v0, v1, 31),
325           "000317c2       srl     v0, v1, 31");
326
327   COMPARE(srlv(a0, a1, a2),
328           "00c52006       srlv    a0, a1, a2");
329   COMPARE(srlv(s0, s1, s2),
330           "02518006       srlv    s0, s1, s2");
331   COMPARE(srlv(t2, t3, t4),
332           "018b5006       srlv    t2, t3, t4");
333   COMPARE(srlv(v0, v1, fp),
334           "03c31006       srlv    v0, v1, fp");
335
336   COMPARE(sra(a0, a1, 0),
337           "00052003       sra     a0, a1, 0");
338   COMPARE(sra(s0, s1, 8),
339           "00118203       sra     s0, s1, 8");
340   COMPARE(sra(t2, t3, 24),
341           "000b5603       sra     t2, t3, 24");
342   COMPARE(sra(v0, v1, 31),
343           "000317c3       sra     v0, v1, 31");
344
345   COMPARE(srav(a0, a1, a2),
346           "00c52007       srav    a0, a1, a2");
347   COMPARE(srav(s0, s1, s2),
348           "02518007       srav    s0, s1, s2");
349   COMPARE(srav(t2, t3, t4),
350           "018b5007       srav    t2, t3, t4");
351   COMPARE(srav(v0, v1, fp),
352           "03c31007       srav    v0, v1, fp");
353
354   if (IsMipsArchVariant(kMips32r2)) {
355     COMPARE(rotr(a0, a1, 0),
356             "00252002       rotr    a0, a1, 0");
357     COMPARE(rotr(s0, s1, 8),
358             "00318202       rotr    s0, s1, 8");
359     COMPARE(rotr(t2, t3, 24),
360             "002b5602       rotr    t2, t3, 24");
361     COMPARE(rotr(v0, v1, 31),
362             "002317c2       rotr    v0, v1, 31");
363
364     COMPARE(rotrv(a0, a1, a2),
365             "00c52046       rotrv   a0, a1, a2");
366     COMPARE(rotrv(s0, s1, s2),
367             "02518046       rotrv   s0, s1, s2");
368     COMPARE(rotrv(t2, t3, t4),
369             "018b5046       rotrv   t2, t3, t4");
370     COMPARE(rotrv(v0, v1, fp),
371             "03c31046       rotrv   v0, v1, fp");
372   }
373
374   COMPARE(break_(0),
375           "0000000d       break, code: 0x00000 (0)");
376   COMPARE(break_(261120),
377           "00ff000d       break, code: 0x3fc00 (261120)");
378   COMPARE(break_(1047552),
379           "03ff000d       break, code: 0xffc00 (1047552)");
380
381   COMPARE(tge(a0, a1, 0),
382           "00850030       tge     a0, a1, code: 0x000");
383   COMPARE(tge(s0, s1, 1023),
384           "0211fff0       tge     s0, s1, code: 0x3ff");
385   COMPARE(tgeu(a0, a1, 0),
386           "00850031       tgeu    a0, a1, code: 0x000");
387   COMPARE(tgeu(s0, s1, 1023),
388           "0211fff1       tgeu    s0, s1, code: 0x3ff");
389   COMPARE(tlt(a0, a1, 0),
390           "00850032       tlt     a0, a1, code: 0x000");
391   COMPARE(tlt(s0, s1, 1023),
392           "0211fff2       tlt     s0, s1, code: 0x3ff");
393   COMPARE(tltu(a0, a1, 0),
394           "00850033       tltu    a0, a1, code: 0x000");
395   COMPARE(tltu(s0, s1, 1023),
396           "0211fff3       tltu    s0, s1, code: 0x3ff");
397   COMPARE(teq(a0, a1, 0),
398           "00850034       teq     a0, a1, code: 0x000");
399   COMPARE(teq(s0, s1, 1023),
400           "0211fff4       teq     s0, s1, code: 0x3ff");
401   COMPARE(tne(a0, a1, 0),
402           "00850036       tne     a0, a1, code: 0x000");
403   COMPARE(tne(s0, s1, 1023),
404           "0211fff6       tne     s0, s1, code: 0x3ff");
405
406   COMPARE(mfhi(a0),
407           "00002010       mfhi    a0");
408   COMPARE(mfhi(s2),
409           "00009010       mfhi    s2");
410   COMPARE(mfhi(t4),
411           "00006010       mfhi    t4");
412   COMPARE(mfhi(v1),
413           "00001810       mfhi    v1");
414   COMPARE(mflo(a0),
415           "00002012       mflo    a0");
416   COMPARE(mflo(s2),
417           "00009012       mflo    s2");
418   COMPARE(mflo(t4),
419           "00006012       mflo    t4");
420   COMPARE(mflo(v1),
421           "00001812       mflo    v1");
422
423   COMPARE(slt(a0, a1, a2),
424           "00a6202a       slt     a0, a1, a2");
425   COMPARE(slt(s0, s1, s2),
426           "0232802a       slt     s0, s1, s2");
427   COMPARE(slt(t2, t3, t4),
428           "016c502a       slt     t2, t3, t4");
429   COMPARE(slt(v0, v1, a2),
430           "0066102a       slt     v0, v1, a2");
431   COMPARE(sltu(a0, a1, a2),
432           "00a6202b       sltu    a0, a1, a2");
433   COMPARE(sltu(s0, s1, s2),
434           "0232802b       sltu    s0, s1, s2");
435   COMPARE(sltu(t2, t3, t4),
436           "016c502b       sltu    t2, t3, t4");
437   COMPARE(sltu(v0, v1, a2),
438           "0066102b       sltu    v0, v1, a2");
439
440   COMPARE(slti(a0, a1, 0),
441           "28a40000       slti    a0, a1, 0");
442   COMPARE(slti(s0, s1, 32767),
443           "2a307fff       slti    s0, s1, 32767");
444   COMPARE(slti(t2, t3, -32768),
445           "296a8000       slti    t2, t3, -32768");
446   COMPARE(slti(v0, v1, -1),
447           "2862ffff       slti    v0, v1, -1");
448   COMPARE(sltiu(a0, a1, 0),
449           "2ca40000       sltiu   a0, a1, 0");
450   COMPARE(sltiu(s0, s1, 32767),
451           "2e307fff       sltiu   s0, s1, 32767");
452   COMPARE(sltiu(t2, t3, -32768),
453           "2d6a8000       sltiu   t2, t3, -32768");
454   COMPARE(sltiu(v0, v1, -1),
455           "2c62ffff       sltiu   v0, v1, -1");
456
457   if (!IsMipsArchVariant(kLoongson)) {
458     COMPARE(movz(a0, a1, a2),
459             "00a6200a       movz    a0, a1, a2");
460     COMPARE(movz(s0, s1, s2),
461             "0232800a       movz    s0, s1, s2");
462     COMPARE(movz(t2, t3, t4),
463             "016c500a       movz    t2, t3, t4");
464     COMPARE(movz(v0, v1, a2),
465             "0066100a       movz    v0, v1, a2");
466     COMPARE(movn(a0, a1, a2),
467             "00a6200b       movn    a0, a1, a2");
468     COMPARE(movn(s0, s1, s2),
469             "0232800b       movn    s0, s1, s2");
470     COMPARE(movn(t2, t3, t4),
471             "016c500b       movn    t2, t3, t4");
472     COMPARE(movn(v0, v1, a2),
473             "0066100b       movn    v0, v1, a2");
474
475     COMPARE(movt(a0, a1, 1),
476             "00a52001       movt    a0, a1, 1");
477     COMPARE(movt(s0, s1, 2),
478             "02298001       movt    s0, s1, 2");
479     COMPARE(movt(t2, t3, 3),
480             "016d5001       movt    t2, t3, 3");
481     COMPARE(movt(v0, v1, 7),
482             "007d1001       movt    v0, v1, 7");
483     COMPARE(movf(a0, a1, 0),
484             "00a02001       movf    a0, a1, 0");
485     COMPARE(movf(s0, s1, 4),
486             "02308001       movf    s0, s1, 4");
487     COMPARE(movf(t2, t3, 5),
488             "01745001       movf    t2, t3, 5");
489     COMPARE(movf(v0, v1, 6),
490             "00781001       movf    v0, v1, 6");
491
492     if (IsMipsArchVariant(kMips32r6)) {
493       COMPARE(clz(a0, a1),
494               "00a02050       clz     a0, a1");
495       COMPARE(clz(s6, s7),
496               "02e0b050       clz     s6, s7");
497       COMPARE(clz(v0, v1),
498               "00601050       clz     v0, v1");
499     } else {
500       COMPARE(clz(a0, a1),
501               "70a42020       clz     a0, a1");
502       COMPARE(clz(s6, s7),
503               "72f6b020       clz     s6, s7");
504       COMPARE(clz(v0, v1),
505               "70621020       clz     v0, v1");
506     }
507   }
508
509   if (IsMipsArchVariant(kMips32r2)) {
510     COMPARE(ins_(a0, a1, 31, 1),
511             "7ca4ffc4       ins     a0, a1, 31, 1");
512     COMPARE(ins_(s6, s7, 30, 2),
513             "7ef6ff84       ins     s6, s7, 30, 2");
514     COMPARE(ins_(v0, v1, 0, 32),
515             "7c62f804       ins     v0, v1, 0, 32");
516     COMPARE(ext_(a0, a1, 31, 1),
517             "7ca407c0       ext     a0, a1, 31, 1");
518     COMPARE(ext_(s6, s7, 30, 2),
519             "7ef60f80       ext     s6, s7, 30, 2");
520     COMPARE(ext_(v0, v1, 0, 32),
521             "7c62f800       ext     v0, v1, 0, 32");
522   }
523
524   VERIFY_RUN();
525 }