blob: 9fe73dea020b70609b84fa3a51a12d259e9afe5f [file] [log] [blame]
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001// 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
32#include "debug.h"
33#include "disasm.h"
34#include "disassembler.h"
35#include "macro-assembler.h"
36#include "serialize.h"
37#include "cctest.h"
38
39using namespace v8::internal;
40
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +000041
42#define __ assm.
43
44
45static void DummyStaticFunction(Object* result) {
46}
47
48
49TEST(DisasmX64) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000050 CcTest::InitializeVM();
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +000051 v8::HandleScope scope;
52 v8::internal::byte buffer[2048];
machenbach@chromium.org528ce022013-09-23 14:09:36 +000053 Assembler assm(CcTest::i_isolate(), buffer, sizeof buffer);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +000054 DummyStaticFunction(NULL); // just bloody use it (DELETE; debugging)
55
56 // Short immediate instructions
57 __ addq(rax, Immediate(12345678));
58 __ or_(rax, Immediate(12345678));
59 __ subq(rax, Immediate(12345678));
60 __ xor_(rax, Immediate(12345678));
61 __ and_(rax, Immediate(12345678));
62
63 // ---- This one caused crash
64 __ movq(rbx, Operand(rsp, rcx, times_2, 0)); // [rsp+rcx*4]
65
66 // ---- All instructions that I can think of
67 __ addq(rdx, rbx);
68 __ addq(rdx, Operand(rbx, 0));
69 __ addq(rdx, Operand(rbx, 16));
70 __ addq(rdx, Operand(rbx, 1999));
71 __ addq(rdx, Operand(rsp, 0));
72 __ addq(rdx, Operand(rsp, 16));
73 __ addq(rdx, Operand(rsp, 1999));
74 __ nop();
75 __ addq(rdi, Operand(rbp, rcx, times_4, 0));
76 __ addq(rdi, Operand(rbp, rcx, times_4, 12));
77 __ addq(Operand(rbp, rcx, times_4, 12), Immediate(12));
78
79 __ nop();
80 __ addq(rbx, Immediate(12));
81 __ nop();
82 __ nop();
83 __ and_(rdx, Immediate(3));
84 __ and_(rdx, Operand(rsp, 4));
85 __ cmpq(rdx, Immediate(3));
86 __ cmpq(rdx, Operand(rsp, 4));
87 __ cmpq(Operand(rbp, rcx, times_4, 0), Immediate(1000));
88 __ cmpb(rbx, Operand(rbp, rcx, times_2, 0));
89 __ cmpb(Operand(rbp, rcx, times_2, 0), rbx);
90 __ or_(rdx, Immediate(3));
91 __ xor_(rdx, Immediate(3));
92 __ nop();
93 {
94 CHECK(CpuFeatures::IsSupported(CPUID));
95 CpuFeatures::Scope fscope(CPUID);
96 __ cpuid();
97 }
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +000098 __ movsxbq(rdx, Operand(rcx, 0));
99 __ movsxwq(rdx, Operand(rcx, 0));
100 __ movzxbl(rdx, Operand(rcx, 0));
101 __ movzxwl(rdx, Operand(rcx, 0));
102 __ movzxbq(rdx, Operand(rcx, 0));
103 __ movzxwq(rdx, Operand(rcx, 0));
104
105 __ nop();
106 __ imul(rdx, rcx);
107 __ shld(rdx, rcx);
108 __ shrd(rdx, rcx);
109 __ bts(Operand(rdx, 0), rcx);
110 __ bts(Operand(rbx, rcx, times_4, 0), rcx);
111 __ nop();
112 __ push(Immediate(12));
113 __ push(Immediate(23456));
114 __ push(rcx);
115 __ push(rsi);
116 __ push(Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
117 __ push(Operand(rbx, rcx, times_4, 0));
118 __ push(Operand(rbx, rcx, times_4, 0));
119 __ push(Operand(rbx, rcx, times_4, 10000));
120 __ pop(rdx);
121 __ pop(rax);
122 __ pop(Operand(rbx, rcx, times_4, 0));
123 __ nop();
124
125 __ addq(rdx, Operand(rsp, 16));
126 __ addq(rdx, rcx);
127 __ movb(rdx, Operand(rcx, 0));
128 __ movb(rcx, Immediate(6));
129 __ movb(Operand(rsp, 16), rdx);
130 __ movw(Operand(rsp, 16), rdx);
131 __ nop();
132 __ movsxwq(rdx, Operand(rsp, 12));
133 __ movsxbq(rdx, Operand(rsp, 12));
134 __ movsxlq(rdx, Operand(rsp, 12));
135 __ movzxwq(rdx, Operand(rsp, 12));
136 __ movzxbq(rdx, Operand(rsp, 12));
137 __ nop();
138 __ movq(rdx, Immediate(1234567));
139 __ movq(rdx, Operand(rsp, 12));
140 __ movq(Operand(rbx, rcx, times_4, 10000), Immediate(12345));
141 __ movq(Operand(rbx, rcx, times_4, 10000), rdx);
142 __ nop();
143 __ decb(rdx);
144 __ decb(Operand(rax, 10));
145 __ decb(Operand(rbx, rcx, times_4, 10000));
146 __ decq(rdx);
147 __ cdq();
148
149 __ nop();
150 __ idivq(rdx);
151 __ mul(rdx);
152 __ neg(rdx);
153 __ not_(rdx);
154 __ testq(Operand(rbx, rcx, times_4, 10000), rdx);
155
156 __ imul(rdx, Operand(rbx, rcx, times_4, 10000));
157 __ imul(rdx, rcx, Immediate(12));
158 __ imul(rdx, rcx, Immediate(1000));
159
160 __ incq(rdx);
161 __ incq(Operand(rbx, rcx, times_4, 10000));
162 __ push(Operand(rbx, rcx, times_4, 10000));
163 __ pop(Operand(rbx, rcx, times_4, 10000));
164 __ jmp(Operand(rbx, rcx, times_4, 10000));
165
166 __ lea(rdx, Operand(rbx, rcx, times_4, 10000));
167 __ or_(rdx, Immediate(12345));
168 __ or_(rdx, Operand(rbx, rcx, times_4, 10000));
169
170 __ nop();
171
172 __ rcl(rdx, Immediate(1));
173 __ rcl(rdx, Immediate(7));
174 __ rcr(rdx, Immediate(1));
175 __ rcr(rdx, Immediate(7));
176 __ sar(rdx, Immediate(1));
177 __ sar(rdx, Immediate(6));
178 __ sar_cl(rdx);
179 __ sbbq(rdx, rbx);
180 __ shld(rdx, rbx);
181 __ shl(rdx, Immediate(1));
182 __ shl(rdx, Immediate(6));
183 __ shl_cl(rdx);
184 __ shrd(rdx, rbx);
185 __ shr(rdx, Immediate(1));
186 __ shr(rdx, Immediate(7));
187 __ shr_cl(rdx);
188
189
190 // Immediates
191
192 __ addq(rbx, Immediate(12));
193 __ addq(Operand(rdx, rcx, times_4, 10000), Immediate(12));
194
195 __ and_(rbx, Immediate(12345));
196
197 __ cmpq(rbx, Immediate(12345));
198 __ cmpq(rbx, Immediate(12));
199 __ cmpq(Operand(rdx, rcx, times_4, 10000), Immediate(12));
200 __ cmpb(rax, Immediate(100));
201
202 __ or_(rbx, Immediate(12345));
203
204 __ subq(rbx, Immediate(12));
205 __ subq(Operand(rdx, rcx, times_4, 10000), Immediate(12));
206
207 __ xor_(rbx, Immediate(12345));
208
209 __ imul(rdx, rcx, Immediate(12));
210 __ imul(rdx, rcx, Immediate(1000));
211
212 __ cld();
213
214 __ subq(rdx, Operand(rbx, rcx, times_4, 10000));
215 __ subq(rdx, rbx);
216
217 __ testq(rdx, Immediate(12345));
218 __ testq(Operand(rbx, rcx, times_8, 10000), rdx);
219 __ testb(Operand(rcx, rbx, times_2, 1000), rdx);
220 __ testb(Operand(rax, -20), Immediate(0x9A));
221 __ nop();
222
223 __ xor_(rdx, Immediate(12345));
224 __ xor_(rdx, Operand(rbx, rcx, times_8, 10000));
225 __ bts(Operand(rbx, rcx, times_8, 10000), rdx);
226 __ hlt();
227 __ int3();
228 __ ret(0);
229 __ ret(8);
230
231 // Calls
232
233 Label L1, L2;
234 __ bind(&L1);
235 __ nop();
236 __ call(&L1);
237 __ call(&L2);
238 __ nop();
239 __ bind(&L2);
240 __ call(Operand(rbx, rcx, times_4, 10000));
241 __ nop();
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000242 Handle<Code> ic(CcTest::i_isolate()->builtins()->builtin(
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000243 Builtins::kLoadIC_Initialize));
244 __ call(ic, RelocInfo::CODE_TARGET);
245 __ nop();
246 __ nop();
247
248 __ jmp(&L1);
249 __ jmp(Operand(rbx, rcx, times_4, 10000));
250#ifdef ENABLE_DEBUGGER_SUPPORT
251 ExternalReference after_break_target =
252 ExternalReference(Debug_Address::AfterBreakTarget(),
253 assm.isolate());
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000254 USE(after_break_target);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000255#endif // ENABLE_DEBUGGER_SUPPORT
256 __ jmp(ic, RelocInfo::CODE_TARGET);
257 __ nop();
258
259
260 Label Ljcc;
261 __ nop();
262 // long jumps
263 __ j(overflow, &Ljcc);
264 __ j(no_overflow, &Ljcc);
265 __ j(below, &Ljcc);
266 __ j(above_equal, &Ljcc);
267 __ j(equal, &Ljcc);
268 __ j(not_equal, &Ljcc);
269 __ j(below_equal, &Ljcc);
270 __ j(above, &Ljcc);
271 __ j(sign, &Ljcc);
272 __ j(not_sign, &Ljcc);
273 __ j(parity_even, &Ljcc);
274 __ j(parity_odd, &Ljcc);
275 __ j(less, &Ljcc);
276 __ j(greater_equal, &Ljcc);
277 __ j(less_equal, &Ljcc);
278 __ j(greater, &Ljcc);
279 __ nop();
280 __ bind(&Ljcc);
281 // short jumps
282 __ j(overflow, &Ljcc);
283 __ j(no_overflow, &Ljcc);
284 __ j(below, &Ljcc);
285 __ j(above_equal, &Ljcc);
286 __ j(equal, &Ljcc);
287 __ j(not_equal, &Ljcc);
288 __ j(below_equal, &Ljcc);
289 __ j(above, &Ljcc);
290 __ j(sign, &Ljcc);
291 __ j(not_sign, &Ljcc);
292 __ j(parity_even, &Ljcc);
293 __ j(parity_odd, &Ljcc);
294 __ j(less, &Ljcc);
295 __ j(greater_equal, &Ljcc);
296 __ j(less_equal, &Ljcc);
297 __ j(greater, &Ljcc);
298
299 // 0xD9 instructions
300 __ nop();
301
302 __ fld(1);
303 __ fld1();
304 __ fldz();
305 __ fldpi();
306 __ fabs();
307 __ fchs();
308 __ fprem();
309 __ fprem1();
310 __ fincstp();
311 __ ftst();
312 __ fxch(3);
313 __ fld_s(Operand(rbx, rcx, times_4, 10000));
314 __ fstp_s(Operand(rbx, rcx, times_4, 10000));
315 __ ffree(3);
316 __ fld_d(Operand(rbx, rcx, times_4, 10000));
317 __ fstp_d(Operand(rbx, rcx, times_4, 10000));
318 __ nop();
319
320 __ fild_s(Operand(rbx, rcx, times_4, 10000));
321 __ fistp_s(Operand(rbx, rcx, times_4, 10000));
322 __ fild_d(Operand(rbx, rcx, times_4, 10000));
323 __ fistp_d(Operand(rbx, rcx, times_4, 10000));
324 __ fnstsw_ax();
325 __ nop();
326 __ fadd(3);
327 __ fsub(3);
328 __ fmul(3);
329 __ fdiv(3);
330
331 __ faddp(3);
332 __ fsubp(3);
333 __ fmulp(3);
334 __ fdivp(3);
335 __ fcompp();
336 __ fwait();
337 __ nop();
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000338
jkummerow@chromium.orgef33a542013-10-28 18:03:37 +0000339 // SSE instruction
340 {
341 __ cvttss2si(rdx, Operand(rbx, rcx, times_4, 10000));
342 __ cvttss2si(rdx, xmm1);
343 __ movaps(xmm0, xmm1);
344
345 __ andps(xmm0, xmm1);
verwaest@chromium.org057bd502013-11-06 12:03:29 +0000346 __ orps(xmm0, xmm1);
347 __ xorps(xmm0, xmm1);
jkummerow@chromium.orgef33a542013-10-28 18:03:37 +0000348 }
349 // SSE 2 instructions
350 {
351 __ cvttsd2si(rdx, Operand(rbx, rcx, times_4, 10000));
352 __ cvttsd2si(rdx, xmm1);
353 __ cvttsd2siq(rdx, xmm1);
354 __ movsd(xmm1, Operand(rbx, rcx, times_4, 10000));
355 __ movsd(Operand(rbx, rcx, times_4, 10000), xmm1);
356 // 128 bit move instructions.
357 __ movdqa(xmm0, Operand(rbx, rcx, times_4, 10000));
358 __ movdqa(Operand(rbx, rcx, times_4, 10000), xmm0);
359
360 __ addsd(xmm1, xmm0);
361 __ mulsd(xmm1, xmm0);
362 __ subsd(xmm1, xmm0);
363 __ divsd(xmm1, xmm0);
364 __ ucomisd(xmm0, xmm1);
365
366 __ andpd(xmm0, xmm1);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000367 }
368
369 // cmov.
370 {
jkummerow@chromium.orgef33a542013-10-28 18:03:37 +0000371 __ cmovq(overflow, rax, Operand(rax, 0));
372 __ cmovq(no_overflow, rax, Operand(rax, 1));
373 __ cmovq(below, rax, Operand(rax, 2));
374 __ cmovq(above_equal, rax, Operand(rax, 3));
375 __ cmovq(equal, rax, Operand(rbx, 0));
376 __ cmovq(not_equal, rax, Operand(rbx, 1));
377 __ cmovq(below_equal, rax, Operand(rbx, 2));
378 __ cmovq(above, rax, Operand(rbx, 3));
379 __ cmovq(sign, rax, Operand(rcx, 0));
380 __ cmovq(not_sign, rax, Operand(rcx, 1));
381 __ cmovq(parity_even, rax, Operand(rcx, 2));
382 __ cmovq(parity_odd, rax, Operand(rcx, 3));
383 __ cmovq(less, rax, Operand(rdx, 0));
384 __ cmovq(greater_equal, rax, Operand(rdx, 1));
385 __ cmovq(less_equal, rax, Operand(rdx, 2));
386 __ cmovq(greater, rax, Operand(rdx, 3));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000387 }
388
mstarzinger@chromium.orgb4968be2013-10-16 09:00:56 +0000389 {
390 if (CpuFeatures::IsSupported(SSE4_1)) {
391 CpuFeatureScope scope(&assm, SSE4_1);
392 __ extractps(rax, xmm1, 0);
393 }
394 }
395
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000396 // Nop instructions
397 for (int i = 0; i < 16; i++) {
398 __ Nop(i);
399 }
400
401 __ ret(0);
402
403 CodeDesc desc;
404 assm.GetCode(&desc);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000405 Object* code = CcTest::heap()->CreateCode(
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000406 desc,
407 Code::ComputeFlags(Code::STUB),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000408 Handle<Code>())->ToObjectChecked();
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000409 CHECK(code->IsCode());
410#ifdef OBJECT_PRINT
411 Code::cast(code)->Print();
412 byte* begin = Code::cast(code)->instruction_start();
413 byte* end = begin + Code::cast(code)->instruction_size();
414 disasm::Disassembler::Disassemble(stdout, begin, end);
415#endif
416}
417
418#undef __