blob: 37aa5eb06b8f769f92b3a66dd2d1cf49e096d098 [file] [log] [blame]
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +00001// Copyright 2012 the V8 project authors. All rights reserved.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002// 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 "v8.h"
32
33#include "debug.h"
34#include "disasm.h"
35#include "disassembler.h"
36#include "macro-assembler.h"
37#include "serialize.h"
38#include "cctest.h"
39
40using namespace v8::internal;
41
42
43static v8::Persistent<v8::Context> env;
44
45static void InitializeVM() {
46 // Disable compilation of natives.
47 FLAG_disable_native_files = true;
48 if (env.IsEmpty()) {
49 env = v8::Context::New();
50 }
51}
52
53
54bool DisassembleAndCompare(byte* pc, const char* compare_string) {
55 disasm::NameConverter converter;
56 disasm::Disassembler disasm(converter);
57 EmbeddedVector<char, 128> disasm_buffer;
58
59 disasm.InstructionDecode(disasm_buffer, pc);
60
61 if (strcmp(compare_string, disasm_buffer.start()) != 0) {
62 fprintf(stderr,
63 "expected: \n"
64 "%s\n"
65 "disassembled: \n"
66 "%s\n\n",
67 compare_string, disasm_buffer.start());
68 return false;
69 }
70 return true;
71}
72
73
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000074// Set up V8 to a state where we can at least run the assembler and
karlklose@chromium.org83a47282011-05-11 11:54:09 +000075// disassembler. Declare the variables and allocate the data structures used
76// in the rest of the macros.
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +000077#define SET_UP() \
karlklose@chromium.org83a47282011-05-11 11:54:09 +000078 InitializeVM(); \
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +000079 Isolate* isolate = Isolate::Current(); \
80 HandleScope scope(isolate); \
karlklose@chromium.org83a47282011-05-11 11:54:09 +000081 byte *buffer = reinterpret_cast<byte*>(malloc(4*1024)); \
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +000082 Assembler assm(isolate, buffer, 4*1024); \
karlklose@chromium.org83a47282011-05-11 11:54:09 +000083 bool failure = false;
84
85
86// This macro assembles one instruction using the preallocated assembler and
87// disassembles the generated instruction, comparing the output to the expected
88// value. If the comparison fails an error message is printed, but the test
89// continues to run until the end.
90#define COMPARE(asm_, compare_string) \
91 { \
92 int pc_offset = assm.pc_offset(); \
93 byte *progcounter = &buffer[pc_offset]; \
94 assm.asm_; \
95 if (!DisassembleAndCompare(progcounter, compare_string)) failure = true; \
96 }
97
98
99// Verify that all invocations of the COMPARE macro passed successfully.
100// Exit with a failure if at least one of the tests failed.
101#define VERIFY_RUN() \
102if (failure) { \
103 V8_Fatal(__FILE__, __LINE__, "MIPS Disassembler tests failed.\n"); \
104 }
105
106
107TEST(Type0) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000108 SET_UP();
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000109
110 COMPARE(addu(a0, a1, a2),
111 "00a62021 addu a0, a1, a2");
112 COMPARE(addu(t2, t3, t4),
113 "016c5021 addu t2, t3, t4");
114 COMPARE(addu(v0, v1, s0),
115 "00701021 addu v0, v1, s0");
116
117 COMPARE(subu(a0, a1, a2),
118 "00a62023 subu a0, a1, a2");
119 COMPARE(subu(t2, t3, t4),
120 "016c5023 subu t2, t3, t4");
121 COMPARE(subu(v0, v1, s0),
122 "00701023 subu v0, v1, s0");
123
124 COMPARE(mult(a0, a1),
125 "00850018 mult a0, a1");
126 COMPARE(mult(t2, t3),
127 "014b0018 mult t2, t3");
128 COMPARE(mult(v0, v1),
129 "00430018 mult v0, v1");
130
131 COMPARE(multu(a0, a1),
132 "00850019 multu a0, a1");
133 COMPARE(multu(t2, t3),
134 "014b0019 multu t2, t3");
135 COMPARE(multu(v0, v1),
136 "00430019 multu v0, v1");
137
138 COMPARE(div(a0, a1),
139 "0085001a div a0, a1");
140 COMPARE(div(t2, t3),
141 "014b001a div t2, t3");
142 COMPARE(div(v0, v1),
143 "0043001a div v0, v1");
144
145 COMPARE(divu(a0, a1),
146 "0085001b divu a0, a1");
147 COMPARE(divu(t2, t3),
148 "014b001b divu t2, t3");
149 COMPARE(divu(v0, v1),
150 "0043001b divu v0, v1");
151
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +0000152 if (kArchVariant != kLoongson) {
153 COMPARE(mul(a0, a1, a2),
154 "70a62002 mul a0, a1, a2");
155 COMPARE(mul(t2, t3, t4),
156 "716c5002 mul t2, t3, t4");
157 COMPARE(mul(v0, v1, s0),
158 "70701002 mul v0, v1, s0");
159 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000160
161 COMPARE(addiu(a0, a1, 0x0),
162 "24a40000 addiu a0, a1, 0");
163 COMPARE(addiu(s0, s1, 32767),
164 "26307fff addiu s0, s1, 32767");
165 COMPARE(addiu(t2, t3, -32768),
166 "256a8000 addiu t2, t3, -32768");
167 COMPARE(addiu(v0, v1, -1),
168 "2462ffff addiu v0, v1, -1");
169
170 COMPARE(and_(a0, a1, a2),
171 "00a62024 and a0, a1, a2");
172 COMPARE(and_(s0, s1, s2),
173 "02328024 and s0, s1, s2");
174 COMPARE(and_(t2, t3, t4),
175 "016c5024 and t2, t3, t4");
176 COMPARE(and_(v0, v1, a2),
177 "00661024 and v0, v1, a2");
178
179 COMPARE(or_(a0, a1, a2),
180 "00a62025 or a0, a1, a2");
181 COMPARE(or_(s0, s1, s2),
182 "02328025 or s0, s1, s2");
183 COMPARE(or_(t2, t3, t4),
184 "016c5025 or t2, t3, t4");
185 COMPARE(or_(v0, v1, a2),
186 "00661025 or v0, v1, a2");
187
188 COMPARE(xor_(a0, a1, a2),
189 "00a62026 xor a0, a1, a2");
190 COMPARE(xor_(s0, s1, s2),
191 "02328026 xor s0, s1, s2");
192 COMPARE(xor_(t2, t3, t4),
193 "016c5026 xor t2, t3, t4");
194 COMPARE(xor_(v0, v1, a2),
195 "00661026 xor v0, v1, a2");
196
197 COMPARE(nor(a0, a1, a2),
198 "00a62027 nor a0, a1, a2");
199 COMPARE(nor(s0, s1, s2),
200 "02328027 nor s0, s1, s2");
201 COMPARE(nor(t2, t3, t4),
202 "016c5027 nor t2, t3, t4");
203 COMPARE(nor(v0, v1, a2),
204 "00661027 nor v0, v1, a2");
205
206 COMPARE(andi(a0, a1, 0x1),
207 "30a40001 andi a0, a1, 0x1");
208 COMPARE(andi(v0, v1, 0xffff),
209 "3062ffff andi v0, v1, 0xffff");
210
211 COMPARE(ori(a0, a1, 0x1),
212 "34a40001 ori a0, a1, 0x1");
213 COMPARE(ori(v0, v1, 0xffff),
214 "3462ffff ori v0, v1, 0xffff");
215
216 COMPARE(xori(a0, a1, 0x1),
217 "38a40001 xori a0, a1, 0x1");
218 COMPARE(xori(v0, v1, 0xffff),
219 "3862ffff xori v0, v1, 0xffff");
220
221 COMPARE(lui(a0, 0x1),
222 "3c040001 lui a0, 0x1");
223 COMPARE(lui(v0, 0xffff),
224 "3c02ffff lui v0, 0xffff");
225
226 COMPARE(sll(a0, a1, 0),
227 "00052000 sll a0, a1, 0");
228 COMPARE(sll(s0, s1, 8),
229 "00118200 sll s0, s1, 8");
230 COMPARE(sll(t2, t3, 24),
231 "000b5600 sll t2, t3, 24");
232 COMPARE(sll(v0, v1, 31),
233 "000317c0 sll v0, v1, 31");
234
235 COMPARE(sllv(a0, a1, a2),
236 "00c52004 sllv a0, a1, a2");
237 COMPARE(sllv(s0, s1, s2),
238 "02518004 sllv s0, s1, s2");
239 COMPARE(sllv(t2, t3, t4),
240 "018b5004 sllv t2, t3, t4");
241 COMPARE(sllv(v0, v1, fp),
242 "03c31004 sllv v0, v1, fp");
243
244 COMPARE(srl(a0, a1, 0),
245 "00052002 srl a0, a1, 0");
246 COMPARE(srl(s0, s1, 8),
247 "00118202 srl s0, s1, 8");
248 COMPARE(srl(t2, t3, 24),
249 "000b5602 srl t2, t3, 24");
250 COMPARE(srl(v0, v1, 31),
251 "000317c2 srl v0, v1, 31");
252
253 COMPARE(srlv(a0, a1, a2),
254 "00c52006 srlv a0, a1, a2");
255 COMPARE(srlv(s0, s1, s2),
256 "02518006 srlv s0, s1, s2");
257 COMPARE(srlv(t2, t3, t4),
258 "018b5006 srlv t2, t3, t4");
259 COMPARE(srlv(v0, v1, fp),
260 "03c31006 srlv v0, v1, fp");
261
262 COMPARE(sra(a0, a1, 0),
263 "00052003 sra a0, a1, 0");
264 COMPARE(sra(s0, s1, 8),
265 "00118203 sra s0, s1, 8");
266 COMPARE(sra(t2, t3, 24),
267 "000b5603 sra t2, t3, 24");
268 COMPARE(sra(v0, v1, 31),
269 "000317c3 sra v0, v1, 31");
270
271 COMPARE(srav(a0, a1, a2),
272 "00c52007 srav a0, a1, a2");
273 COMPARE(srav(s0, s1, s2),
274 "02518007 srav s0, s1, s2");
275 COMPARE(srav(t2, t3, t4),
276 "018b5007 srav t2, t3, t4");
277 COMPARE(srav(v0, v1, fp),
278 "03c31007 srav v0, v1, fp");
279
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +0000280 if (kArchVariant == kMips32r2) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000281 COMPARE(rotr(a0, a1, 0),
282 "00252002 rotr a0, a1, 0");
283 COMPARE(rotr(s0, s1, 8),
284 "00318202 rotr s0, s1, 8");
285 COMPARE(rotr(t2, t3, 24),
286 "002b5602 rotr t2, t3, 24");
287 COMPARE(rotr(v0, v1, 31),
288 "002317c2 rotr v0, v1, 31");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000289
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000290 COMPARE(rotrv(a0, a1, a2),
291 "00c52046 rotrv a0, a1, a2");
292 COMPARE(rotrv(s0, s1, s2),
293 "02518046 rotrv s0, s1, s2");
294 COMPARE(rotrv(t2, t3, t4),
295 "018b5046 rotrv t2, t3, t4");
296 COMPARE(rotrv(v0, v1, fp),
297 "03c31046 rotrv v0, v1, fp");
298 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000299
300 COMPARE(break_(0),
301 "0000000d break, code: 0x00000 (0)");
302 COMPARE(break_(261120),
303 "00ff000d break, code: 0x3fc00 (261120)");
304 COMPARE(break_(1047552),
305 "03ff000d break, code: 0xffc00 (1047552)");
306
307 COMPARE(tge(a0, a1, 0),
308 "00850030 tge a0, a1, code: 0x000");
309 COMPARE(tge(s0, s1, 1023),
310 "0211fff0 tge s0, s1, code: 0x3ff");
311 COMPARE(tgeu(a0, a1, 0),
312 "00850031 tgeu a0, a1, code: 0x000");
313 COMPARE(tgeu(s0, s1, 1023),
314 "0211fff1 tgeu s0, s1, code: 0x3ff");
315 COMPARE(tlt(a0, a1, 0),
316 "00850032 tlt a0, a1, code: 0x000");
317 COMPARE(tlt(s0, s1, 1023),
318 "0211fff2 tlt s0, s1, code: 0x3ff");
319 COMPARE(tltu(a0, a1, 0),
320 "00850033 tltu a0, a1, code: 0x000");
321 COMPARE(tltu(s0, s1, 1023),
322 "0211fff3 tltu s0, s1, code: 0x3ff");
323 COMPARE(teq(a0, a1, 0),
324 "00850034 teq a0, a1, code: 0x000");
325 COMPARE(teq(s0, s1, 1023),
326 "0211fff4 teq s0, s1, code: 0x3ff");
327 COMPARE(tne(a0, a1, 0),
328 "00850036 tne a0, a1, code: 0x000");
329 COMPARE(tne(s0, s1, 1023),
330 "0211fff6 tne s0, s1, code: 0x3ff");
331
332 COMPARE(mfhi(a0),
333 "00002010 mfhi a0");
334 COMPARE(mfhi(s2),
335 "00009010 mfhi s2");
336 COMPARE(mfhi(t4),
337 "00006010 mfhi t4");
338 COMPARE(mfhi(v1),
339 "00001810 mfhi v1");
340 COMPARE(mflo(a0),
341 "00002012 mflo a0");
342 COMPARE(mflo(s2),
343 "00009012 mflo s2");
344 COMPARE(mflo(t4),
345 "00006012 mflo t4");
346 COMPARE(mflo(v1),
347 "00001812 mflo v1");
348
349 COMPARE(slt(a0, a1, a2),
350 "00a6202a slt a0, a1, a2");
351 COMPARE(slt(s0, s1, s2),
352 "0232802a slt s0, s1, s2");
353 COMPARE(slt(t2, t3, t4),
354 "016c502a slt t2, t3, t4");
355 COMPARE(slt(v0, v1, a2),
356 "0066102a slt v0, v1, a2");
357 COMPARE(sltu(a0, a1, a2),
358 "00a6202b sltu a0, a1, a2");
359 COMPARE(sltu(s0, s1, s2),
360 "0232802b sltu s0, s1, s2");
361 COMPARE(sltu(t2, t3, t4),
362 "016c502b sltu t2, t3, t4");
363 COMPARE(sltu(v0, v1, a2),
364 "0066102b sltu v0, v1, a2");
365
366 COMPARE(slti(a0, a1, 0),
367 "28a40000 slti a0, a1, 0");
368 COMPARE(slti(s0, s1, 32767),
369 "2a307fff slti s0, s1, 32767");
370 COMPARE(slti(t2, t3, -32768),
371 "296a8000 slti t2, t3, -32768");
372 COMPARE(slti(v0, v1, -1),
373 "2862ffff slti v0, v1, -1");
374 COMPARE(sltiu(a0, a1, 0),
375 "2ca40000 sltiu a0, a1, 0");
376 COMPARE(sltiu(s0, s1, 32767),
377 "2e307fff sltiu s0, s1, 32767");
378 COMPARE(sltiu(t2, t3, -32768),
379 "2d6a8000 sltiu t2, t3, -32768");
380 COMPARE(sltiu(v0, v1, -1),
381 "2c62ffff sltiu v0, v1, -1");
382
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +0000383 if (kArchVariant != kLoongson) {
384 COMPARE(movz(a0, a1, a2),
385 "00a6200a movz a0, a1, a2");
386 COMPARE(movz(s0, s1, s2),
387 "0232800a movz s0, s1, s2");
388 COMPARE(movz(t2, t3, t4),
389 "016c500a movz t2, t3, t4");
390 COMPARE(movz(v0, v1, a2),
391 "0066100a movz v0, v1, a2");
392 COMPARE(movn(a0, a1, a2),
393 "00a6200b movn a0, a1, a2");
394 COMPARE(movn(s0, s1, s2),
395 "0232800b movn s0, s1, s2");
396 COMPARE(movn(t2, t3, t4),
397 "016c500b movn t2, t3, t4");
398 COMPARE(movn(v0, v1, a2),
399 "0066100b movn v0, v1, a2");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000400
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +0000401 COMPARE(movt(a0, a1, 1),
402 "00a52001 movt a0, a1, 1");
403 COMPARE(movt(s0, s1, 2),
404 "02298001 movt s0, s1, 2");
405 COMPARE(movt(t2, t3, 3),
406 "016d5001 movt t2, t3, 3");
407 COMPARE(movt(v0, v1, 7),
408 "007d1001 movt v0, v1, 7");
409 COMPARE(movf(a0, a1, 0),
410 "00a02001 movf a0, a1, 0");
411 COMPARE(movf(s0, s1, 4),
412 "02308001 movf s0, s1, 4");
413 COMPARE(movf(t2, t3, 5),
414 "01745001 movf t2, t3, 5");
415 COMPARE(movf(v0, v1, 6),
416 "00781001 movf v0, v1, 6");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000417
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +0000418 COMPARE(clz(a0, a1),
419 "70a42020 clz a0, a1");
420 COMPARE(clz(s6, s7),
421 "72f6b020 clz s6, s7");
422 COMPARE(clz(v0, v1),
423 "70621020 clz v0, v1");
424 }
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000425
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +0000426 if (kArchVariant == kMips32r2) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000427 COMPARE(ins_(a0, a1, 31, 1),
428 "7ca4ffc4 ins a0, a1, 31, 1");
429 COMPARE(ins_(s6, s7, 30, 2),
430 "7ef6ff84 ins s6, s7, 30, 2");
431 COMPARE(ins_(v0, v1, 0, 32),
432 "7c62f804 ins v0, v1, 0, 32");
433 COMPARE(ext_(a0, a1, 31, 1),
434 "7ca407c0 ext a0, a1, 31, 1");
435 COMPARE(ext_(s6, s7, 30, 2),
436 "7ef60f80 ext s6, s7, 30, 2");
437 COMPARE(ext_(v0, v1, 0, 32),
438 "7c62f800 ext v0, v1, 0, 32");
439 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000440
441 VERIFY_RUN();
442}