1 // Copyright 2011 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
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.
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.
32 #include "src/debug.h"
33 #include "src/disasm.h"
34 #include "src/disassembler.h"
35 #include "src/ic/ic.h"
36 #include "src/macro-assembler.h"
37 #include "src/serialize.h"
38 #include "test/cctest/cctest.h"
40 using namespace v8::internal;
46 static void DummyStaticFunction(Object* result) {
51 CcTest::InitializeVM();
52 Isolate* isolate = CcTest::i_isolate();
53 HandleScope scope(isolate);
54 v8::internal::byte buffer[4096];
55 Assembler assm(isolate, buffer, sizeof buffer);
56 DummyStaticFunction(NULL); // just bloody use it (DELETE; debugging)
58 // Short immediate instructions
59 __ adc(eax, 12345678);
60 __ add(eax, Immediate(12345678));
61 __ or_(eax, 12345678);
62 __ sub(eax, Immediate(12345678));
63 __ xor_(eax, 12345678);
64 __ and_(eax, 12345678);
65 Handle<FixedArray> foo = isolate->factory()->NewFixedArray(10, TENURED);
68 // ---- This one caused crash
69 __ mov(ebx, Operand(esp, ecx, times_2, 0)); // [esp+ecx*4]
71 // ---- All instructions that I can think of
73 __ add(edx, Operand(12, RelocInfo::NONE32));
74 __ add(edx, Operand(ebx, 0));
75 __ add(edx, Operand(ebx, 16));
76 __ add(edx, Operand(ebx, 1999));
77 __ add(edx, Operand(ebx, -4));
78 __ add(edx, Operand(ebx, -1999));
79 __ add(edx, Operand(esp, 0));
80 __ add(edx, Operand(esp, 16));
81 __ add(edx, Operand(esp, 1999));
82 __ add(edx, Operand(esp, -4));
83 __ add(edx, Operand(esp, -1999));
85 __ add(esi, Operand(ecx, times_4, 0));
86 __ add(esi, Operand(ecx, times_4, 24));
87 __ add(esi, Operand(ecx, times_4, -4));
88 __ add(esi, Operand(ecx, times_4, -1999));
90 __ add(edi, Operand(ebp, ecx, times_4, 0));
91 __ add(edi, Operand(ebp, ecx, times_4, 12));
92 __ add(edi, Operand(ebp, ecx, times_4, -8));
93 __ add(edi, Operand(ebp, ecx, times_4, -3999));
94 __ add(Operand(ebp, ecx, times_4, 12), Immediate(12));
97 __ add(ebx, Immediate(12));
103 __ and_(edx, Operand(esp, 4));
105 __ cmp(edx, Operand(esp, 4));
106 __ cmp(Operand(ebp, ecx, times_4, 0), Immediate(1000));
107 Handle<FixedArray> foo2 = isolate->factory()->NewFixedArray(10, TENURED);
109 __ cmpb(ebx, Operand(ebp, ecx, times_2, 0));
110 __ cmpb(Operand(ebp, ecx, times_2, 0), ebx);
115 __ movsx_b(edx, ecx);
116 __ movsx_w(edx, ecx);
117 __ movzx_b(edx, ecx);
118 __ movzx_w(edx, ecx);
125 __ bts(Operand(ebx, ecx, times_4, 0), ecx);
131 __ push(Immediate(12));
132 __ push(Immediate(23456));
135 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
136 __ push(Operand(ebx, ecx, times_4, 0));
137 __ push(Operand(ebx, ecx, times_4, 0));
138 __ push(Operand(ebx, ecx, times_4, 10000));
141 __ pop(Operand(ebx, ecx, times_4, 0));
144 __ add(edx, Operand(esp, 16));
148 __ mov_b(Operand(ebx, ecx, times_4, 10000), 6);
149 __ mov_b(Operand(esp, 16), edx);
150 __ mov_w(edx, Operand(esp, 16));
151 __ mov_w(Operand(esp, 16), edx);
153 __ movsx_w(edx, Operand(esp, 12));
154 __ movsx_b(edx, Operand(esp, 12));
155 __ movzx_w(edx, Operand(esp, 12));
156 __ movzx_b(edx, Operand(esp, 12));
158 __ mov(edx, 1234567);
159 __ mov(edx, Operand(esp, 12));
160 __ mov(Operand(ebx, ecx, times_4, 10000), Immediate(12345));
161 __ mov(Operand(ebx, ecx, times_4, 10000), edx);
164 __ dec_b(Operand(eax, 10));
165 __ dec_b(Operand(ebx, ecx, times_4, 10000));
171 __ idiv(Operand(edx, ecx, times_1, 1));
172 __ idiv(Operand(esp, 12));
174 __ div(Operand(edx, ecx, times_1, 1));
175 __ div(Operand(esp, 12));
179 __ test(Operand(ebx, ecx, times_4, 10000), Immediate(123456));
181 __ imul(edx, Operand(ebx, ecx, times_4, 10000));
182 __ imul(edx, ecx, 12);
183 __ imul(edx, Operand(edx, eax, times_2, 42), 8);
184 __ imul(edx, ecx, 1000);
185 __ imul(edx, Operand(ebx, ecx, times_4, 1), 9000);
188 __ inc(Operand(ebx, ecx, times_4, 10000));
189 __ push(Operand(ebx, ecx, times_4, 10000));
190 __ pop(Operand(ebx, ecx, times_4, 10000));
191 __ call(Operand(ebx, ecx, times_4, 10000));
192 __ jmp(Operand(ebx, ecx, times_4, 10000));
194 __ lea(edx, Operand(ebx, ecx, times_4, 10000));
196 __ or_(edx, Operand(ebx, ecx, times_4, 10000));
207 __ ror(Operand(ebx, ecx, times_4, 10000), 1);
208 __ ror(Operand(ebx, ecx, times_4, 10000), 6);
209 __ ror_cl(Operand(ebx, ecx, times_4, 10000));
213 __ sar(Operand(ebx, ecx, times_4, 10000), 1);
214 __ sar(Operand(ebx, ecx, times_4, 10000), 6);
215 __ sar_cl(Operand(ebx, ecx, times_4, 10000));
216 __ sbb(edx, Operand(ebx, ecx, times_4, 10000));
217 __ shld(edx, Operand(ebx, ecx, times_4, 10000));
221 __ shl(Operand(ebx, ecx, times_4, 10000), 1);
222 __ shl(Operand(ebx, ecx, times_4, 10000), 6);
223 __ shl_cl(Operand(ebx, ecx, times_4, 10000));
224 __ shrd(edx, Operand(ebx, ecx, times_4, 10000));
228 __ shr(Operand(ebx, ecx, times_4, 10000), 1);
229 __ shr(Operand(ebx, ecx, times_4, 10000), 6);
230 __ shr_cl(Operand(ebx, ecx, times_4, 10000));
237 __ add(ebx, Immediate(12));
238 __ add(Operand(edx, ecx, times_4, 10000), Immediate(12));
243 __ cmp(ebx, Immediate(12));
244 __ cmp(Operand(edx, ecx, times_4, 10000), Immediate(12));
249 __ sub(ebx, Immediate(12));
250 __ sub(Operand(edx, ecx, times_4, 10000), Immediate(12));
254 __ imul(edx, ecx, 12);
255 __ imul(edx, ecx, 1000);
262 __ sub(edx, Operand(ebx, ecx, times_4, 10000));
265 __ test(edx, Immediate(12345));
266 __ test(edx, Operand(ebx, ecx, times_8, 10000));
267 __ test(Operand(esi, edi, times_1, -20000000), Immediate(300000000));
268 __ test_b(edx, Operand(ecx, ebx, times_2, 1000));
269 __ test_b(Operand(eax, -20), 0x9A);
273 __ xor_(edx, Operand(ebx, ecx, times_8, 10000));
274 __ bts(Operand(ebx, ecx, times_8, 10000), edx);
289 __ call(Operand(ebx, ecx, times_4, 10000));
291 Handle<Code> ic(LoadIC::initialize_stub(isolate, NOT_CONTEXTUAL));
292 __ call(ic, RelocInfo::CODE_TARGET);
294 __ call(FUNCTION_ADDR(DummyStaticFunction), RelocInfo::RUNTIME_ENTRY);
298 __ jmp(Operand(ebx, ecx, times_4, 10000));
299 ExternalReference after_break_target =
300 ExternalReference::debug_after_break_target_address(isolate);
301 __ jmp(Operand::StaticVariable(after_break_target));
302 __ jmp(ic, RelocInfo::CODE_TARGET);
309 __ j(overflow, &Ljcc);
310 __ j(no_overflow, &Ljcc);
312 __ j(above_equal, &Ljcc);
314 __ j(not_equal, &Ljcc);
315 __ j(below_equal, &Ljcc);
318 __ j(not_sign, &Ljcc);
319 __ j(parity_even, &Ljcc);
320 __ j(parity_odd, &Ljcc);
322 __ j(greater_equal, &Ljcc);
323 __ j(less_equal, &Ljcc);
324 __ j(greater, &Ljcc);
328 __ j(overflow, &Ljcc);
329 __ j(no_overflow, &Ljcc);
331 __ j(above_equal, &Ljcc);
333 __ j(not_equal, &Ljcc);
334 __ j(below_equal, &Ljcc);
337 __ j(not_sign, &Ljcc);
338 __ j(parity_even, &Ljcc);
339 __ j(parity_odd, &Ljcc);
341 __ j(greater_equal, &Ljcc);
342 __ j(less_equal, &Ljcc);
343 __ j(greater, &Ljcc);
359 __ fld_s(Operand(ebx, ecx, times_4, 10000));
360 __ fstp_s(Operand(ebx, ecx, times_4, 10000));
362 __ fld_d(Operand(ebx, ecx, times_4, 10000));
363 __ fstp_d(Operand(ebx, ecx, times_4, 10000));
366 __ fild_s(Operand(ebx, ecx, times_4, 10000));
367 __ fistp_s(Operand(ebx, ecx, times_4, 10000));
368 __ fild_d(Operand(ebx, ecx, times_4, 10000));
369 __ fistp_d(Operand(ebx, ecx, times_4, 10000));
390 __ movaps(xmm0, xmm1);
391 __ shufps(xmm0, xmm0, 0x0);
392 __ cvtsd2ss(xmm0, xmm1);
393 __ cvtsd2ss(xmm0, Operand(ebx, ecx, times_4, 10000));
396 __ andps(xmm0, xmm1);
397 __ andps(xmm0, Operand(ebx, ecx, times_4, 10000));
399 __ orps(xmm0, Operand(ebx, ecx, times_4, 10000));
400 __ xorps(xmm0, xmm1);
401 __ xorps(xmm0, Operand(ebx, ecx, times_4, 10000));
403 // Arithmetic operation
404 __ addss(xmm1, xmm0);
405 __ addss(xmm1, Operand(ebx, ecx, times_4, 10000));
406 __ mulss(xmm1, xmm0);
407 __ mulss(xmm1, Operand(ebx, ecx, times_4, 10000));
408 __ subss(xmm1, xmm0);
409 __ subss(xmm1, Operand(ebx, ecx, times_4, 10000));
410 __ divss(xmm1, xmm0);
411 __ divss(xmm1, Operand(ebx, ecx, times_4, 10000));
412 __ addps(xmm1, xmm0);
413 __ addps(xmm1, Operand(ebx, ecx, times_4, 10000));
414 __ subps(xmm1, xmm0);
415 __ subps(xmm1, Operand(ebx, ecx, times_4, 10000));
416 __ mulps(xmm1, xmm0);
417 __ mulps(xmm1, Operand(ebx, ecx, times_4, 10000));
418 __ divps(xmm1, xmm0);
419 __ divps(xmm1, Operand(ebx, ecx, times_4, 10000));
421 __ ucomiss(xmm0, xmm1);
422 __ ucomiss(xmm0, Operand(ebx, ecx, times_4, 10000));
425 __ cvttss2si(edx, Operand(ebx, ecx, times_4, 10000));
426 __ cvtsi2sd(xmm1, Operand(ebx, ecx, times_4, 10000));
427 __ cvtss2sd(xmm1, Operand(ebx, ecx, times_4, 10000));
428 __ cvtss2sd(xmm1, xmm0);
429 __ movsd(xmm1, Operand(ebx, ecx, times_4, 10000));
430 __ movsd(Operand(ebx, ecx, times_4, 10000), xmm1);
431 // 128 bit move instructions.
432 __ movdqa(xmm0, Operand(ebx, ecx, times_4, 10000));
433 __ movdqa(Operand(ebx, ecx, times_4, 10000), xmm0);
434 __ movdqu(xmm0, Operand(ebx, ecx, times_4, 10000));
435 __ movdqu(Operand(ebx, ecx, times_4, 10000), xmm0);
437 __ addsd(xmm1, xmm0);
438 __ addsd(xmm1, Operand(ebx, ecx, times_4, 10000));
439 __ mulsd(xmm1, xmm0);
440 __ mulsd(xmm1, Operand(ebx, ecx, times_4, 10000));
441 __ subsd(xmm1, xmm0);
442 __ subsd(xmm1, Operand(ebx, ecx, times_4, 10000));
443 __ divsd(xmm1, xmm0);
444 __ divsd(xmm1, Operand(ebx, ecx, times_4, 10000));
445 __ ucomisd(xmm0, xmm1);
446 __ cmpltsd(xmm0, xmm1);
448 __ andpd(xmm0, xmm1);
450 __ psllq(xmm0, xmm1);
452 __ psrlq(xmm0, xmm1);
458 __ cmov(overflow, eax, Operand(eax, 0));
459 __ cmov(no_overflow, eax, Operand(eax, 1));
460 __ cmov(below, eax, Operand(eax, 2));
461 __ cmov(above_equal, eax, Operand(eax, 3));
462 __ cmov(equal, eax, Operand(ebx, 0));
463 __ cmov(not_equal, eax, Operand(ebx, 1));
464 __ cmov(below_equal, eax, Operand(ebx, 2));
465 __ cmov(above, eax, Operand(ebx, 3));
466 __ cmov(sign, eax, Operand(ecx, 0));
467 __ cmov(not_sign, eax, Operand(ecx, 1));
468 __ cmov(parity_even, eax, Operand(ecx, 2));
469 __ cmov(parity_odd, eax, Operand(ecx, 3));
470 __ cmov(less, eax, Operand(edx, 0));
471 __ cmov(greater_equal, eax, Operand(edx, 1));
472 __ cmov(less_equal, eax, Operand(edx, 2));
473 __ cmov(greater, eax, Operand(edx, 3));
477 if (CpuFeatures::IsSupported(SSE4_1)) {
478 CpuFeatureScope scope(&assm, SSE4_1);
479 __ pextrd(eax, xmm0, 1);
480 __ pinsrd(xmm1, eax, 0);
481 __ extractps(eax, xmm1, 0);
487 if (CpuFeatures::IsSupported(AVX)) {
488 CpuFeatureScope scope(&assm, AVX);
489 __ vaddsd(xmm0, xmm1, xmm2);
490 __ vaddsd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
491 __ vmulsd(xmm0, xmm1, xmm2);
492 __ vmulsd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
493 __ vsubsd(xmm0, xmm1, xmm2);
494 __ vsubsd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
495 __ vdivsd(xmm0, xmm1, xmm2);
496 __ vdivsd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
502 if (CpuFeatures::IsSupported(FMA3)) {
503 CpuFeatureScope scope(&assm, FMA3);
504 __ vfmadd132sd(xmm0, xmm1, xmm2);
505 __ vfmadd132sd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
506 __ vfmadd213sd(xmm0, xmm1, xmm2);
507 __ vfmadd213sd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
508 __ vfmadd231sd(xmm0, xmm1, xmm2);
509 __ vfmadd231sd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
511 __ vfmsub132sd(xmm0, xmm1, xmm2);
512 __ vfmsub132sd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
513 __ vfmsub213sd(xmm0, xmm1, xmm2);
514 __ vfmsub213sd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
515 __ vfmsub231sd(xmm0, xmm1, xmm2);
516 __ vfmsub231sd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
518 __ vfnmadd132sd(xmm0, xmm1, xmm2);
519 __ vfnmadd132sd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
520 __ vfnmadd213sd(xmm0, xmm1, xmm2);
521 __ vfnmadd213sd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
522 __ vfnmadd231sd(xmm0, xmm1, xmm2);
523 __ vfnmadd231sd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
525 __ vfnmsub132sd(xmm0, xmm1, xmm2);
526 __ vfnmsub132sd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
527 __ vfnmsub213sd(xmm0, xmm1, xmm2);
528 __ vfnmsub213sd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
529 __ vfnmsub231sd(xmm0, xmm1, xmm2);
530 __ vfnmsub231sd(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
532 __ vfmadd132ss(xmm0, xmm1, xmm2);
533 __ vfmadd132ss(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
534 __ vfmadd213ss(xmm0, xmm1, xmm2);
535 __ vfmadd213ss(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
536 __ vfmadd231ss(xmm0, xmm1, xmm2);
537 __ vfmadd231ss(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
539 __ vfmsub132ss(xmm0, xmm1, xmm2);
540 __ vfmsub132ss(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
541 __ vfmsub213ss(xmm0, xmm1, xmm2);
542 __ vfmsub213ss(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
543 __ vfmsub231ss(xmm0, xmm1, xmm2);
544 __ vfmsub231ss(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
546 __ vfnmadd132ss(xmm0, xmm1, xmm2);
547 __ vfnmadd132ss(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
548 __ vfnmadd213ss(xmm0, xmm1, xmm2);
549 __ vfnmadd213ss(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
550 __ vfnmadd231ss(xmm0, xmm1, xmm2);
551 __ vfnmadd231ss(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
553 __ vfnmsub132ss(xmm0, xmm1, xmm2);
554 __ vfnmsub132ss(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
555 __ vfnmsub213ss(xmm0, xmm1, xmm2);
556 __ vfnmsub213ss(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
557 __ vfnmsub231ss(xmm0, xmm1, xmm2);
558 __ vfnmsub231ss(xmm0, xmm1, Operand(ebx, ecx, times_4, 10000));
567 __ xchg(ebx, Operand(esp, 12));
571 for (int i = 0; i < 16; i++) {
579 Handle<Code> code = isolate->factory()->NewCode(
580 desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
585 byte* begin = code->instruction_start();
586 byte* end = begin + code->instruction_size();
587 disasm::Disassembler::Disassemble(stdout, begin, end);