Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / v8 / test / cctest / test-disasm-ia32.cc
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
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 #include <stdlib.h>
29
30 #include "v8.h"
31 #include "stub-cache.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
40 using namespace v8::internal;
41
42
43 #define __ assm.
44
45
46 static void DummyStaticFunction(Object* result) {
47 }
48
49
50 TEST(DisasmIa320) {
51   CcTest::InitializeVM();
52   Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
53   HandleScope scope(isolate);
54   v8::internal::byte buffer[2048];
55   Assembler assm(isolate, buffer, sizeof buffer);
56   DummyStaticFunction(NULL);  // just bloody use it (DELETE; debugging)
57
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);
66   __ cmp(eax, foo);
67
68   // ---- This one caused crash
69   __ mov(ebx,  Operand(esp, ecx, times_2, 0));  // [esp+ecx*4]
70
71   // ---- All instructions that I can think of
72   __ add(edx, ebx);
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(esp, 0));
78   __ add(edx, Operand(esp, 16));
79   __ add(edx, Operand(esp, 1999));
80   __ nop();
81   __ add(edi, Operand(ebp, ecx, times_4, 0));
82   __ add(edi, Operand(ebp, ecx, times_4, 12));
83   __ add(Operand(ebp, ecx, times_4, 12), Immediate(12));
84
85   __ nop();
86   __ add(ebx, Immediate(12));
87   __ nop();
88   __ adc(ecx, 12);
89   __ adc(ecx, 1000);
90   __ nop();
91   __ and_(edx, 3);
92   __ and_(edx, Operand(esp, 4));
93   __ cmp(edx, 3);
94   __ cmp(edx, Operand(esp, 4));
95   __ cmp(Operand(ebp, ecx, times_4, 0), Immediate(1000));
96   Handle<FixedArray> foo2 = isolate->factory()->NewFixedArray(10, TENURED);
97   __ cmp(ebx, foo2);
98   __ cmpb(ebx, Operand(ebp, ecx, times_2, 0));
99   __ cmpb(Operand(ebp, ecx, times_2, 0), ebx);
100   __ or_(edx, 3);
101   __ xor_(edx, 3);
102   __ nop();
103   __ cpuid();
104   __ movsx_b(edx, ecx);
105   __ movsx_w(edx, ecx);
106   __ movzx_b(edx, ecx);
107   __ movzx_w(edx, ecx);
108
109   __ nop();
110   __ imul(edx, ecx);
111   __ shld(edx, ecx);
112   __ shrd(edx, ecx);
113   __ bts(edx, ecx);
114   __ bts(Operand(ebx, ecx, times_4, 0), ecx);
115   __ nop();
116   __ pushad();
117   __ popad();
118   __ pushfd();
119   __ popfd();
120   __ push(Immediate(12));
121   __ push(Immediate(23456));
122   __ push(ecx);
123   __ push(esi);
124   __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
125   __ push(Operand(ebx, ecx, times_4, 0));
126   __ push(Operand(ebx, ecx, times_4, 0));
127   __ push(Operand(ebx, ecx, times_4, 10000));
128   __ pop(edx);
129   __ pop(eax);
130   __ pop(Operand(ebx, ecx, times_4, 0));
131   __ nop();
132
133   __ add(edx, Operand(esp, 16));
134   __ add(edx, ecx);
135   __ mov_b(edx, ecx);
136   __ mov_b(ecx, 6);
137   __ mov_b(Operand(ebx, ecx, times_4, 10000), 6);
138   __ mov_b(Operand(esp, 16), edx);
139   __ mov_w(edx, Operand(esp, 16));
140   __ mov_w(Operand(esp, 16), edx);
141   __ nop();
142   __ movsx_w(edx, Operand(esp, 12));
143   __ movsx_b(edx, Operand(esp, 12));
144   __ movzx_w(edx, Operand(esp, 12));
145   __ movzx_b(edx, Operand(esp, 12));
146   __ nop();
147   __ mov(edx, 1234567);
148   __ mov(edx, Operand(esp, 12));
149   __ mov(Operand(ebx, ecx, times_4, 10000), Immediate(12345));
150   __ mov(Operand(ebx, ecx, times_4, 10000), edx);
151   __ nop();
152   __ dec_b(edx);
153   __ dec_b(Operand(eax, 10));
154   __ dec_b(Operand(ebx, ecx, times_4, 10000));
155   __ dec(edx);
156   __ cdq();
157
158   __ nop();
159   __ idiv(edx);
160   __ mul(edx);
161   __ neg(edx);
162   __ not_(edx);
163   __ test(Operand(ebx, ecx, times_4, 10000), Immediate(123456));
164
165   __ imul(edx, Operand(ebx, ecx, times_4, 10000));
166   __ imul(edx, ecx, 12);
167   __ imul(edx, ecx, 1000);
168
169   __ inc(edx);
170   __ inc(Operand(ebx, ecx, times_4, 10000));
171   __ push(Operand(ebx, ecx, times_4, 10000));
172   __ pop(Operand(ebx, ecx, times_4, 10000));
173   __ call(Operand(ebx, ecx, times_4, 10000));
174   __ jmp(Operand(ebx, ecx, times_4, 10000));
175
176   __ lea(edx, Operand(ebx, ecx, times_4, 10000));
177   __ or_(edx, 12345);
178   __ or_(edx, Operand(ebx, ecx, times_4, 10000));
179
180   __ nop();
181
182   __ rcl(edx, 1);
183   __ rcl(edx, 7);
184   __ rcr(edx, 1);
185   __ rcr(edx, 7);
186   __ sar(edx, 1);
187   __ sar(edx, 6);
188   __ sar_cl(edx);
189   __ sbb(edx, Operand(ebx, ecx, times_4, 10000));
190   __ shld(edx, Operand(ebx, ecx, times_4, 10000));
191   __ shl(edx, 1);
192   __ shl(edx, 6);
193   __ shl_cl(edx);
194   __ shrd(edx, Operand(ebx, ecx, times_4, 10000));
195   __ shr(edx, 1);
196   __ shr(edx, 7);
197   __ shr_cl(edx);
198
199
200   // Immediates
201
202   __ adc(edx, 12345);
203
204   __ add(ebx, Immediate(12));
205   __ add(Operand(edx, ecx, times_4, 10000), Immediate(12));
206
207   __ and_(ebx, 12345);
208
209   __ cmp(ebx, 12345);
210   __ cmp(ebx, Immediate(12));
211   __ cmp(Operand(edx, ecx, times_4, 10000), Immediate(12));
212   __ cmpb(eax, 100);
213
214   __ or_(ebx, 12345);
215
216   __ sub(ebx, Immediate(12));
217   __ sub(Operand(edx, ecx, times_4, 10000), Immediate(12));
218
219   __ xor_(ebx, 12345);
220
221   __ imul(edx, ecx, 12);
222   __ imul(edx, ecx, 1000);
223
224   __ cld();
225   __ rep_movs();
226   __ rep_stos();
227   __ stos();
228
229   __ sub(edx, Operand(ebx, ecx, times_4, 10000));
230   __ sub(edx, ebx);
231
232   __ test(edx, Immediate(12345));
233   __ test(edx, Operand(ebx, ecx, times_8, 10000));
234   __ test(Operand(esi, edi, times_1, -20000000), Immediate(300000000));
235   __ test_b(edx, Operand(ecx, ebx, times_2, 1000));
236   __ test_b(Operand(eax, -20), 0x9A);
237   __ nop();
238
239   __ xor_(edx, 12345);
240   __ xor_(edx, Operand(ebx, ecx, times_8, 10000));
241   __ bts(Operand(ebx, ecx, times_8, 10000), edx);
242   __ hlt();
243   __ int3();
244   __ ret(0);
245   __ ret(8);
246
247   // Calls
248
249   Label L1, L2;
250   __ bind(&L1);
251   __ nop();
252   __ call(&L1);
253   __ call(&L2);
254   __ nop();
255   __ bind(&L2);
256   __ call(Operand(ebx, ecx, times_4, 10000));
257   __ nop();
258   Handle<Code> ic(LoadIC::initialize_stub(isolate, NOT_CONTEXTUAL));
259   __ call(ic, RelocInfo::CODE_TARGET);
260   __ nop();
261   __ call(FUNCTION_ADDR(DummyStaticFunction), RelocInfo::RUNTIME_ENTRY);
262   __ nop();
263
264   __ jmp(&L1);
265   __ jmp(Operand(ebx, ecx, times_4, 10000));
266 #ifdef ENABLE_DEBUGGER_SUPPORT
267   ExternalReference after_break_target =
268       ExternalReference(Debug_Address::AfterBreakTarget(), isolate);
269   __ jmp(Operand::StaticVariable(after_break_target));
270 #endif  // ENABLE_DEBUGGER_SUPPORT
271   __ jmp(ic, RelocInfo::CODE_TARGET);
272   __ nop();
273
274
275   Label Ljcc;
276   __ nop();
277   // long jumps
278   __ j(overflow, &Ljcc);
279   __ j(no_overflow, &Ljcc);
280   __ j(below, &Ljcc);
281   __ j(above_equal, &Ljcc);
282   __ j(equal, &Ljcc);
283   __ j(not_equal, &Ljcc);
284   __ j(below_equal, &Ljcc);
285   __ j(above, &Ljcc);
286   __ j(sign, &Ljcc);
287   __ j(not_sign, &Ljcc);
288   __ j(parity_even, &Ljcc);
289   __ j(parity_odd, &Ljcc);
290   __ j(less, &Ljcc);
291   __ j(greater_equal, &Ljcc);
292   __ j(less_equal, &Ljcc);
293   __ j(greater, &Ljcc);
294   __ nop();
295   __ bind(&Ljcc);
296   // short jumps
297   __ j(overflow, &Ljcc);
298   __ j(no_overflow, &Ljcc);
299   __ j(below, &Ljcc);
300   __ j(above_equal, &Ljcc);
301   __ j(equal, &Ljcc);
302   __ j(not_equal, &Ljcc);
303   __ j(below_equal, &Ljcc);
304   __ j(above, &Ljcc);
305   __ j(sign, &Ljcc);
306   __ j(not_sign, &Ljcc);
307   __ j(parity_even, &Ljcc);
308   __ j(parity_odd, &Ljcc);
309   __ j(less, &Ljcc);
310   __ j(greater_equal, &Ljcc);
311   __ j(less_equal, &Ljcc);
312   __ j(greater, &Ljcc);
313
314   // 0xD9 instructions
315   __ nop();
316
317   __ fld(1);
318   __ fld1();
319   __ fldz();
320   __ fldpi();
321   __ fabs();
322   __ fchs();
323   __ fprem();
324   __ fprem1();
325   __ fincstp();
326   __ ftst();
327   __ fxch(3);
328   __ fld_s(Operand(ebx, ecx, times_4, 10000));
329   __ fstp_s(Operand(ebx, ecx, times_4, 10000));
330   __ ffree(3);
331   __ fld_d(Operand(ebx, ecx, times_4, 10000));
332   __ fstp_d(Operand(ebx, ecx, times_4, 10000));
333   __ nop();
334
335   __ fild_s(Operand(ebx, ecx, times_4, 10000));
336   __ fistp_s(Operand(ebx, ecx, times_4, 10000));
337   __ fild_d(Operand(ebx, ecx, times_4, 10000));
338   __ fistp_d(Operand(ebx, ecx, times_4, 10000));
339   __ fnstsw_ax();
340   __ nop();
341   __ fadd(3);
342   __ fsub(3);
343   __ fmul(3);
344   __ fdiv(3);
345
346   __ faddp(3);
347   __ fsubp(3);
348   __ fmulp(3);
349   __ fdivp(3);
350   __ fcompp();
351   __ fwait();
352   __ nop();
353
354   // SSE instruction
355   {
356     if (CpuFeatures::IsSupported(SSE2)) {
357       CpuFeatureScope fscope(&assm, SSE2);
358       // Move operation
359       __ movaps(xmm0, xmm1);
360       __ shufps(xmm0, xmm0, 0x0);
361
362       // logic operation
363       __ andps(xmm0, xmm1);
364       __ andps(xmm0, Operand(ebx, ecx, times_4, 10000));
365       __ orps(xmm0, xmm1);
366       __ orps(xmm0, Operand(ebx, ecx, times_4, 10000));
367       __ xorps(xmm0, xmm1);
368       __ xorps(xmm0, Operand(ebx, ecx, times_4, 10000));
369
370       // Arithmetic operation
371       __ addps(xmm1, xmm0);
372       __ addps(xmm1, Operand(ebx, ecx, times_4, 10000));
373       __ subps(xmm1, xmm0);
374       __ subps(xmm1, Operand(ebx, ecx, times_4, 10000));
375       __ mulps(xmm1, xmm0);
376       __ mulps(xmm1, Operand(ebx, ecx, times_4, 10000));
377       __ divps(xmm1, xmm0);
378       __ divps(xmm1, Operand(ebx, ecx, times_4, 10000));
379     }
380   }
381   {
382     if (CpuFeatures::IsSupported(SSE2)) {
383       CpuFeatureScope fscope(&assm, SSE2);
384       __ cvttss2si(edx, Operand(ebx, ecx, times_4, 10000));
385       __ cvtsi2sd(xmm1, Operand(ebx, ecx, times_4, 10000));
386       __ movsd(xmm1, Operand(ebx, ecx, times_4, 10000));
387       __ movsd(Operand(ebx, ecx, times_4, 10000), xmm1);
388       // 128 bit move instructions.
389       __ movdqa(xmm0, Operand(ebx, ecx, times_4, 10000));
390       __ movdqa(Operand(ebx, ecx, times_4, 10000), xmm0);
391       __ movdqu(xmm0, Operand(ebx, ecx, times_4, 10000));
392       __ movdqu(Operand(ebx, ecx, times_4, 10000), xmm0);
393
394       __ addsd(xmm1, xmm0);
395       __ mulsd(xmm1, xmm0);
396       __ subsd(xmm1, xmm0);
397       __ divsd(xmm1, xmm0);
398       __ ucomisd(xmm0, xmm1);
399       __ cmpltsd(xmm0, xmm1);
400
401       __ andpd(xmm0, xmm1);
402       __ psllq(xmm0, 17);
403       __ psllq(xmm0, xmm1);
404       __ psrlq(xmm0, 17);
405       __ psrlq(xmm0, xmm1);
406       __ por(xmm0, xmm1);
407     }
408   }
409
410   // cmov.
411   {
412     if (CpuFeatures::IsSupported(CMOV)) {
413       CpuFeatureScope use_cmov(&assm, CMOV);
414       __ cmov(overflow, eax, Operand(eax, 0));
415       __ cmov(no_overflow, eax, Operand(eax, 1));
416       __ cmov(below, eax, Operand(eax, 2));
417       __ cmov(above_equal, eax, Operand(eax, 3));
418       __ cmov(equal, eax, Operand(ebx, 0));
419       __ cmov(not_equal, eax, Operand(ebx, 1));
420       __ cmov(below_equal, eax, Operand(ebx, 2));
421       __ cmov(above, eax, Operand(ebx, 3));
422       __ cmov(sign, eax, Operand(ecx, 0));
423       __ cmov(not_sign, eax, Operand(ecx, 1));
424       __ cmov(parity_even, eax, Operand(ecx, 2));
425       __ cmov(parity_odd, eax, Operand(ecx, 3));
426       __ cmov(less, eax, Operand(edx, 0));
427       __ cmov(greater_equal, eax, Operand(edx, 1));
428       __ cmov(less_equal, eax, Operand(edx, 2));
429       __ cmov(greater, eax, Operand(edx, 3));
430     }
431   }
432
433   {
434     if (CpuFeatures::IsSupported(SSE2) &&
435         CpuFeatures::IsSupported(SSE4_1)) {
436       CpuFeatureScope scope(&assm, SSE4_1);
437       __ pextrd(eax, xmm0, 1);
438       __ pinsrd(xmm1, eax, 0);
439       __ extractps(eax, xmm1, 0);
440     }
441   }
442
443   // Nop instructions
444   for (int i = 0; i < 16; i++) {
445     __ Nop(i);
446   }
447
448   __ ret(0);
449
450   CodeDesc desc;
451   assm.GetCode(&desc);
452   Object* code = isolate->heap()->CreateCode(
453       desc,
454       Code::ComputeFlags(Code::STUB),
455       Handle<Code>())->ToObjectChecked();
456   CHECK(code->IsCode());
457 #ifdef OBJECT_PRINT
458   Code::cast(code)->Print();
459   byte* begin = Code::cast(code)->instruction_start();
460   byte* end = begin + Code::cast(code)->instruction_size();
461   disasm::Disassembler::Disassemble(stdout, begin, end);
462 #endif
463 }
464
465 #undef __