blob: af9fb97e5bff4ade9d27c2f26c4b2221a2b5bd27 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2007-2008 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
41static v8::Persistent<v8::Context> env;
42
43static void InitializeVM() {
44 if (env.IsEmpty()) {
45 env = v8::Context::New();
46 }
47}
48
49
50#define __ assm.
51
52
53static void DummyStaticFunction(Object* result) {
54}
55
56
57TEST(DisasmIa320) {
58 InitializeVM();
59 v8::HandleScope scope;
60 v8::internal::byte buffer[1024];
61 Assembler assm(buffer, sizeof buffer);
62 DummyStaticFunction(NULL); // just bloody use it (DELETE; debugging)
63
64 // Short immediate instructions
65 __ adc(eax, 12345678);
66 __ add(Operand(eax), Immediate(12345678));
67 __ or_(eax, 12345678);
68 __ sub(Operand(eax), Immediate(12345678));
69 __ xor_(eax, 12345678);
70 __ and_(eax, 12345678);
71 Handle<FixedArray> foo = Factory::NewFixedArray(10, TENURED);
72 __ cmp(eax, foo);
73
74 // ---- This one caused crash
75 __ mov(ebx, Operand(esp, ecx, times_2, 0)); // [esp+ecx*4]
76
77 // ---- All instructions that I can think of
78 __ add(edx, Operand(ebx));
79 __ add(edx, Operand(12, RelocInfo::NONE));
80 __ add(edx, Operand(ebx, 0));
81 __ add(edx, Operand(ebx, 16));
82 __ add(edx, Operand(ebx, 1999));
83 __ add(edx, Operand(esp, 0));
84 __ add(edx, Operand(esp, 16));
85 __ add(edx, Operand(esp, 1999));
86 __ nop();
87 __ add(edi, Operand(ebp, ecx, times_4, 0));
88 __ add(edi, Operand(ebp, ecx, times_4, 12));
89 __ add(Operand(ebp, ecx, times_4, 12), Immediate(12));
90
91 __ nop();
92 __ add(Operand(ebx), Immediate(12));
93 __ nop();
94 __ adc(ecx, 12);
95 __ adc(ecx, 1000);
96 __ nop();
97 __ and_(edx, 3);
98 __ and_(edx, Operand(esp, 4));
99 __ cmp(edx, 3);
100 __ cmp(edx, Operand(esp, 4));
101 __ cmp(Operand(ebp, ecx, times_4, 0), Immediate(1000));
102 Handle<FixedArray> foo2 = Factory::NewFixedArray(10, TENURED);
103 __ cmp(ebx, foo2);
104 __ or_(edx, 3);
105 __ xor_(edx, 3);
106 __ nop();
107 {
108 CHECK(CpuFeatures::IsSupported(CpuFeatures::CPUID));
109 CpuFeatures::Scope fscope(CpuFeatures::CPUID);
110 __ cpuid();
111 }
112 {
113 CHECK(CpuFeatures::IsSupported(CpuFeatures::RDTSC));
114 CpuFeatures::Scope fscope(CpuFeatures::RDTSC);
115 __ rdtsc();
116 }
117 __ movsx_b(edx, Operand(ecx));
118 __ movsx_w(edx, Operand(ecx));
119 __ movzx_b(edx, Operand(ecx));
120 __ movzx_w(edx, Operand(ecx));
121
122 __ nop();
123 __ imul(edx, Operand(ecx));
124 __ shld(edx, Operand(ecx));
125 __ shrd(edx, Operand(ecx));
126 __ bts(Operand(edx), ecx);
127 __ bts(Operand(ebx, ecx, times_4, 0), ecx);
128 __ nop();
129 __ pushad();
130 __ popad();
131 __ pushfd();
132 __ popfd();
133 __ push(Immediate(12));
134 __ push(Immediate(23456));
135 __ push(ecx);
136 __ push(esi);
137 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
138 __ push(Operand(ebx, ecx, times_4, 0));
139 __ push(Operand(ebx, ecx, times_4, 0));
140 __ push(Operand(ebx, ecx, times_4, 10000));
141 __ pop(edx);
142 __ pop(eax);
143 __ pop(Operand(ebx, ecx, times_4, 0));
144 __ nop();
145
146 __ add(edx, Operand(esp, 16));
147 __ add(edx, Operand(ecx));
148 __ mov_b(edx, Operand(ecx));
149 __ mov_b(Operand(ecx), 6);
150 __ mov_b(Operand(ebx, ecx, times_4, 10000), 6);
151 __ mov_b(Operand(esp, 16), edx);
152 __ mov_w(edx, Operand(esp, 16));
153 __ mov_w(Operand(esp, 16), edx);
154 __ nop();
155 __ movsx_w(edx, Operand(esp, 12));
156 __ movsx_b(edx, Operand(esp, 12));
157 __ movzx_w(edx, Operand(esp, 12));
158 __ movzx_b(edx, Operand(esp, 12));
159 __ nop();
160 __ mov(edx, 1234567);
161 __ mov(edx, Operand(esp, 12));
162 __ mov(Operand(ebx, ecx, times_4, 10000), Immediate(12345));
163 __ mov(Operand(ebx, ecx, times_4, 10000), edx);
164 __ nop();
165 __ dec_b(edx);
166 __ dec(edx);
167 __ cdq();
168
169 __ nop();
170 __ idiv(edx);
171 __ mul(edx);
172 __ neg(edx);
173 __ not_(edx);
174 __ test(Operand(ebx, ecx, times_4, 10000), Immediate(123456));
175
176 __ imul(edx, Operand(ebx, ecx, times_4, 10000));
177 __ imul(edx, ecx, 12);
178 __ imul(edx, ecx, 1000);
179
180 __ inc(edx);
181 __ inc(Operand(ebx, ecx, times_4, 10000));
182 __ push(Operand(ebx, ecx, times_4, 10000));
183 __ pop(Operand(ebx, ecx, times_4, 10000));
184 __ call(Operand(ebx, ecx, times_4, 10000));
185 __ jmp(Operand(ebx, ecx, times_4, 10000));
186
187 __ lea(edx, Operand(ebx, ecx, times_4, 10000));
188 __ or_(edx, 12345);
189 __ or_(edx, Operand(ebx, ecx, times_4, 10000));
190
191 __ nop();
192
193 __ rcl(edx, 1);
194 __ rcl(edx, 7);
195 __ sar(edx, 1);
196 __ sar(edx, 6);
197 __ sar(edx);
198 __ sbb(edx, Operand(ebx, ecx, times_4, 10000));
199 __ shld(edx, Operand(ebx, ecx, times_4, 10000));
200 __ shl(edx, 1);
201 __ shl(edx, 6);
202 __ shl(edx);
203 __ shrd(edx, Operand(ebx, ecx, times_4, 10000));
204 __ shr(edx, 7);
205 __ shr(edx);
206
207
208 // Immediates
209
210 __ adc(edx, 12345);
211
212 __ add(Operand(ebx), Immediate(12));
213 __ add(Operand(edx, ecx, times_4, 10000), Immediate(12));
214
215 __ and_(ebx, 12345);
216
217 __ cmp(ebx, 12345);
218 __ cmp(Operand(ebx), Immediate(12));
219 __ cmp(Operand(edx, ecx, times_4, 10000), Immediate(12));
220
221 __ or_(ebx, 12345);
222
223 __ sub(Operand(ebx), Immediate(12));
224 __ sub(Operand(edx, ecx, times_4, 10000), Immediate(12));
225
226 __ xor_(ebx, 12345);
227
228 __ imul(edx, ecx, 12);
229 __ imul(edx, ecx, 1000);
230
231
232
233 __ sub(edx, Operand(ebx, ecx, times_4, 10000));
234 __ sub(edx, Operand(ebx));
235
236 __ test(edx, Immediate(12345));
237 __ test(edx, Operand(ebx, ecx, times_8, 10000));
238 __ nop();
239
240 __ xor_(edx, 12345);
241 __ xor_(edx, Operand(ebx, ecx, times_8, 10000));
242 __ bts(Operand(ebx, ecx, times_8, 10000), edx);
243 __ hlt();
244 __ int3();
245 __ ret(0);
246 __ ret(8);
247
248 // Calls
249
250 Label L1, L2;
251 __ bind(&L1);
252 __ nop();
253 __ call(&L1);
254 __ call(&L2);
255 __ nop();
256 __ bind(&L2);
257 __ call(Operand(ebx, ecx, times_4, 10000));
258 __ nop();
259 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
260 __ call(ic, RelocInfo::CODE_TARGET);
261 __ nop();
262 __ call(FUNCTION_ADDR(DummyStaticFunction), RelocInfo::RUNTIME_ENTRY);
263 __ nop();
264
265 __ jmp(&L1);
266 __ jmp(Operand(ebx, ecx, times_4, 10000));
267 ExternalReference after_break_target =
268 ExternalReference(Debug_Address::AfterBreakTarget());
269 __ jmp(Operand::StaticVariable(after_break_target));
270 __ jmp(ic, RelocInfo::CODE_TARGET);
271 __ nop();
272
273
274 Label Ljcc;
275 __ nop();
276 // long jumps
277 __ j(overflow, &Ljcc);
278 __ j(no_overflow, &Ljcc);
279 __ j(below, &Ljcc);
280 __ j(above_equal, &Ljcc);
281 __ j(equal, &Ljcc);
282 __ j(not_equal, &Ljcc);
283 __ j(below_equal, &Ljcc);
284 __ j(above, &Ljcc);
285 __ j(sign, &Ljcc);
286 __ j(not_sign, &Ljcc);
287 __ j(parity_even, &Ljcc);
288 __ j(parity_odd, &Ljcc);
289 __ j(less, &Ljcc);
290 __ j(greater_equal, &Ljcc);
291 __ j(less_equal, &Ljcc);
292 __ j(greater, &Ljcc);
293 __ nop();
294 __ bind(&Ljcc);
295 // short jumps
296 __ j(overflow, &Ljcc);
297 __ j(no_overflow, &Ljcc);
298 __ j(below, &Ljcc);
299 __ j(above_equal, &Ljcc);
300 __ j(equal, &Ljcc);
301 __ j(not_equal, &Ljcc);
302 __ j(below_equal, &Ljcc);
303 __ j(above, &Ljcc);
304 __ j(sign, &Ljcc);
305 __ j(not_sign, &Ljcc);
306 __ j(parity_even, &Ljcc);
307 __ j(parity_odd, &Ljcc);
308 __ j(less, &Ljcc);
309 __ j(greater_equal, &Ljcc);
310 __ j(less_equal, &Ljcc);
311 __ j(greater, &Ljcc);
312
313 // checking hints
314 __ j(zero, &Ljcc, taken);
315 __ j(zero, &Ljcc, not_taken);
316
317 // __ mov(Operand::StaticVariable(Top::handler_address()), eax);
318 // 0xD9 instructions
319 __ nop();
320
321 __ fld1();
322 __ fldz();
323 __ fabs();
324 __ fchs();
325 __ fprem();
326 __ fprem1();
327 __ fincstp();
328 __ ftst();
329 __ fxch(3);
330 __ fld_s(Operand(ebx, ecx, times_4, 10000));
331 __ fstp_s(Operand(ebx, ecx, times_4, 10000));
332 __ ffree(3);
333 __ fld_d(Operand(ebx, ecx, times_4, 10000));
334 __ fstp_d(Operand(ebx, ecx, times_4, 10000));
335 __ nop();
336
337 __ fild_s(Operand(ebx, ecx, times_4, 10000));
338 __ fistp_s(Operand(ebx, ecx, times_4, 10000));
339 __ fild_d(Operand(ebx, ecx, times_4, 10000));
340 __ fistp_d(Operand(ebx, ecx, times_4, 10000));
341 __ fnstsw_ax();
342 __ nop();
343 __ fadd(3);
344 __ fsub(3);
345 __ fmul(3);
346 __ fdiv(3);
347
348 __ faddp(3);
349 __ fsubp(3);
350 __ fmulp(3);
351 __ fdivp(3);
352 __ fcompp();
353 __ fwait();
354 __ nop();
355 {
356 CHECK(CpuFeatures::IsSupported(CpuFeatures::SSE2));
357 CpuFeatures::Scope fscope(CpuFeatures::SSE2);
358 __ cvttss2si(edx, Operand(ebx, ecx, times_4, 10000));
359 __ cvtsi2sd(xmm1, Operand(ebx, ecx, times_4, 10000));
360 __ addsd(xmm1, xmm0);
361 __ mulsd(xmm1, xmm0);
362 __ subsd(xmm1, xmm0);
363 __ divsd(xmm1, xmm0);
364 __ movdbl(xmm1, Operand(ebx, ecx, times_4, 10000));
365 __ movdbl(Operand(ebx, ecx, times_4, 10000), xmm1);
366 }
367 __ ret(0);
368
369 CodeDesc desc;
370 assm.GetCode(&desc);
371 Object* code = Heap::CreateCode(desc,
372 NULL,
373 Code::ComputeFlags(Code::STUB),
374 Handle<Object>(Heap::undefined_value()));
375 CHECK(code->IsCode());
376#ifdef DEBUG
377 Code::cast(code)->Print();
378 byte* begin = Code::cast(code)->instruction_start();
379 byte* end = begin + Code::cast(code)->instruction_size();
380 disasm::Disassembler::Disassemble(stdout, begin, end);
381#endif
382}
383
384#undef __