blob: a7602e7d8e73bf31d6697494f5d52c53477741a3 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright (c) 1994-2006 Sun Microsystems Inc.
2// All Rights Reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7//
8// - Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10//
11// - Redistribution in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the
14// distribution.
15//
16// - Neither the name of Sun Microsystems or the names of contributors may
17// be used to endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31// OF THE POSSIBILITY OF SUCH DAMAGE.
32
33// The original source code covered by the above license above has been modified
34// significantly by Google Inc.
Ben Murdoch257744e2011-11-30 15:57:28 +000035// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +000036
37#include "v8.h"
38
Leon Clarkef7060e22010-06-03 12:02:55 +010039#if defined(V8_TARGET_ARCH_IA32)
40
Steve Blocka7e24c12009-10-30 11:49:00 +000041#include "disassembler.h"
42#include "macro-assembler.h"
43#include "serialize.h"
44
45namespace v8 {
46namespace internal {
47
48// -----------------------------------------------------------------------------
49// Implementation of CpuFeatures
50
Ben Murdoch8b112d22011-06-08 16:22:53 +010051#ifdef DEBUG
52bool CpuFeatures::initialized_ = false;
53#endif
54uint64_t CpuFeatures::supported_ = 0;
55uint64_t CpuFeatures::found_by_runtime_probing_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +000056
57
Ben Murdoch8b112d22011-06-08 16:22:53 +010058void CpuFeatures::Probe() {
59 ASSERT(!initialized_);
Steve Blocka7e24c12009-10-30 11:49:00 +000060 ASSERT(supported_ == 0);
Ben Murdoch8b112d22011-06-08 16:22:53 +010061#ifdef DEBUG
62 initialized_ = true;
63#endif
64 if (Serializer::enabled()) {
Steve Blockd0582a62009-12-15 09:54:21 +000065 supported_ |= OS::CpuFeaturesImpliedByPlatform();
66 return; // No features if we might serialize.
67 }
Steve Blocka7e24c12009-10-30 11:49:00 +000068
Ben Murdoch8b112d22011-06-08 16:22:53 +010069 const int kBufferSize = 4 * KB;
70 VirtualMemory* memory = new VirtualMemory(kBufferSize);
71 if (!memory->IsReserved()) {
72 delete memory;
73 return;
74 }
75 ASSERT(memory->size() >= static_cast<size_t>(kBufferSize));
76 if (!memory->Commit(memory->address(), kBufferSize, true/*executable*/)) {
77 delete memory;
78 return;
79 }
80
81 Assembler assm(NULL, memory->address(), kBufferSize);
Steve Blocka7e24c12009-10-30 11:49:00 +000082 Label cpuid, done;
83#define __ assm.
84 // Save old esp, since we are going to modify the stack.
85 __ push(ebp);
86 __ pushfd();
87 __ push(ecx);
88 __ push(ebx);
89 __ mov(ebp, Operand(esp));
90
91 // If we can modify bit 21 of the EFLAGS register, then CPUID is supported.
92 __ pushfd();
93 __ pop(eax);
94 __ mov(edx, Operand(eax));
95 __ xor_(eax, 0x200000); // Flip bit 21.
96 __ push(eax);
97 __ popfd();
98 __ pushfd();
99 __ pop(eax);
100 __ xor_(eax, Operand(edx)); // Different if CPUID is supported.
101 __ j(not_zero, &cpuid);
102
103 // CPUID not supported. Clear the supported features in edx:eax.
104 __ xor_(eax, Operand(eax));
105 __ xor_(edx, Operand(edx));
106 __ jmp(&done);
107
108 // Invoke CPUID with 1 in eax to get feature information in
109 // ecx:edx. Temporarily enable CPUID support because we know it's
110 // safe here.
111 __ bind(&cpuid);
112 __ mov(eax, 1);
113 supported_ = (1 << CPUID);
114 { Scope fscope(CPUID);
115 __ cpuid();
116 }
117 supported_ = 0;
118
119 // Move the result from ecx:edx to edx:eax and make sure to mark the
120 // CPUID feature as supported.
121 __ mov(eax, Operand(edx));
122 __ or_(eax, 1 << CPUID);
123 __ mov(edx, Operand(ecx));
124
125 // Done.
126 __ bind(&done);
127 __ mov(esp, Operand(ebp));
128 __ pop(ebx);
129 __ pop(ecx);
130 __ popfd();
131 __ pop(ebp);
132 __ ret(0);
133#undef __
134
Steve Blocka7e24c12009-10-30 11:49:00 +0000135 typedef uint64_t (*F0)();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100136 F0 probe = FUNCTION_CAST<F0>(reinterpret_cast<Address>(memory->address()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 supported_ = probe();
Steve Blockd0582a62009-12-15 09:54:21 +0000138 found_by_runtime_probing_ = supported_;
139 uint64_t os_guarantees = OS::CpuFeaturesImpliedByPlatform();
140 supported_ |= os_guarantees;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100141 found_by_runtime_probing_ &= ~os_guarantees;
142
143 delete memory;
Steve Blocka7e24c12009-10-30 11:49:00 +0000144}
145
146
147// -----------------------------------------------------------------------------
148// Implementation of Displacement
149
150void Displacement::init(Label* L, Type type) {
151 ASSERT(!L->is_bound());
152 int next = 0;
153 if (L->is_linked()) {
154 next = L->pos();
155 ASSERT(next > 0); // Displacements must be at positions > 0
156 }
157 // Ensure that we _never_ overflow the next field.
158 ASSERT(NextField::is_valid(Assembler::kMaximalBufferSize));
159 data_ = NextField::encode(next) | TypeField::encode(type);
160}
161
162
163// -----------------------------------------------------------------------------
164// Implementation of RelocInfo
165
166
167const int RelocInfo::kApplyMask =
168 RelocInfo::kCodeTargetMask | 1 << RelocInfo::RUNTIME_ENTRY |
Ben Murdochbb769b22010-08-11 14:56:33 +0100169 1 << RelocInfo::JS_RETURN | 1 << RelocInfo::INTERNAL_REFERENCE |
170 1 << RelocInfo::DEBUG_BREAK_SLOT;
Steve Blocka7e24c12009-10-30 11:49:00 +0000171
172
Leon Clarkef7060e22010-06-03 12:02:55 +0100173bool RelocInfo::IsCodedSpecially() {
174 // The deserializer needs to know whether a pointer is specially coded. Being
175 // specially coded on IA32 means that it is a relative address, as used by
176 // branch instructions. These are also the ones that need changing when a
177 // code object moves.
178 return (1 << rmode_) & kApplyMask;
179}
180
181
Steve Blocka7e24c12009-10-30 11:49:00 +0000182void RelocInfo::PatchCode(byte* instructions, int instruction_count) {
183 // Patch the code at the current address with the supplied instructions.
184 for (int i = 0; i < instruction_count; i++) {
185 *(pc_ + i) = *(instructions + i);
186 }
187
188 // Indicate that code has changed.
189 CPU::FlushICache(pc_, instruction_count);
190}
191
192
193// Patch the code at the current PC with a call to the target address.
194// Additional guard int3 instructions can be added if required.
195void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) {
196 // Call instruction takes up 5 bytes and int3 takes up one byte.
197 static const int kCallCodeSize = 5;
198 int code_size = kCallCodeSize + guard_bytes;
199
200 // Create a code patcher.
201 CodePatcher patcher(pc_, code_size);
202
203 // Add a label for checking the size of the code used for returning.
204#ifdef DEBUG
205 Label check_codesize;
206 patcher.masm()->bind(&check_codesize);
207#endif
208
209 // Patch the code.
210 patcher.masm()->call(target, RelocInfo::NONE);
211
212 // Check that the size of the code generated is as expected.
213 ASSERT_EQ(kCallCodeSize,
214 patcher.masm()->SizeOfCodeGeneratedSince(&check_codesize));
215
216 // Add the requested number of int3 instructions after the call.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100217 ASSERT_GE(guard_bytes, 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000218 for (int i = 0; i < guard_bytes; i++) {
219 patcher.masm()->int3();
220 }
221}
222
223
224// -----------------------------------------------------------------------------
225// Implementation of Operand
226
227Operand::Operand(Register base, int32_t disp, RelocInfo::Mode rmode) {
228 // [base + disp/r]
229 if (disp == 0 && rmode == RelocInfo::NONE && !base.is(ebp)) {
230 // [base]
231 set_modrm(0, base);
232 if (base.is(esp)) set_sib(times_1, esp, base);
233 } else if (is_int8(disp) && rmode == RelocInfo::NONE) {
234 // [base + disp8]
235 set_modrm(1, base);
236 if (base.is(esp)) set_sib(times_1, esp, base);
237 set_disp8(disp);
238 } else {
239 // [base + disp/r]
240 set_modrm(2, base);
241 if (base.is(esp)) set_sib(times_1, esp, base);
242 set_dispr(disp, rmode);
243 }
244}
245
246
247Operand::Operand(Register base,
248 Register index,
249 ScaleFactor scale,
250 int32_t disp,
251 RelocInfo::Mode rmode) {
252 ASSERT(!index.is(esp)); // illegal addressing mode
253 // [base + index*scale + disp/r]
254 if (disp == 0 && rmode == RelocInfo::NONE && !base.is(ebp)) {
255 // [base + index*scale]
256 set_modrm(0, esp);
257 set_sib(scale, index, base);
258 } else if (is_int8(disp) && rmode == RelocInfo::NONE) {
259 // [base + index*scale + disp8]
260 set_modrm(1, esp);
261 set_sib(scale, index, base);
262 set_disp8(disp);
263 } else {
264 // [base + index*scale + disp/r]
265 set_modrm(2, esp);
266 set_sib(scale, index, base);
267 set_dispr(disp, rmode);
268 }
269}
270
271
272Operand::Operand(Register index,
273 ScaleFactor scale,
274 int32_t disp,
275 RelocInfo::Mode rmode) {
276 ASSERT(!index.is(esp)); // illegal addressing mode
277 // [index*scale + disp/r]
278 set_modrm(0, esp);
279 set_sib(scale, index, ebp);
280 set_dispr(disp, rmode);
281}
282
283
284bool Operand::is_reg(Register reg) const {
285 return ((buf_[0] & 0xF8) == 0xC0) // addressing mode is register only.
286 && ((buf_[0] & 0x07) == reg.code()); // register codes match.
287}
288
289// -----------------------------------------------------------------------------
Andrei Popescu31002712010-02-23 13:46:05 +0000290// Implementation of Assembler.
Steve Blocka7e24c12009-10-30 11:49:00 +0000291
292// Emit a single byte. Must always be inlined.
293#define EMIT(x) \
294 *pc_++ = (x)
295
296
297#ifdef GENERATED_CODE_COVERAGE
298static void InitCoverageLog();
299#endif
300
Ben Murdoch8b112d22011-06-08 16:22:53 +0100301Assembler::Assembler(Isolate* arg_isolate, void* buffer, int buffer_size)
302 : AssemblerBase(arg_isolate),
Steve Block44f0eee2011-05-26 01:26:41 +0100303 positions_recorder_(this),
304 emit_debug_code_(FLAG_debug_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 if (buffer == NULL) {
Andrei Popescu31002712010-02-23 13:46:05 +0000306 // Do our own buffer management.
Steve Blocka7e24c12009-10-30 11:49:00 +0000307 if (buffer_size <= kMinimalBufferSize) {
308 buffer_size = kMinimalBufferSize;
309
Steve Block44f0eee2011-05-26 01:26:41 +0100310 if (isolate()->assembler_spare_buffer() != NULL) {
311 buffer = isolate()->assembler_spare_buffer();
312 isolate()->set_assembler_spare_buffer(NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 }
314 }
315 if (buffer == NULL) {
316 buffer_ = NewArray<byte>(buffer_size);
317 } else {
318 buffer_ = static_cast<byte*>(buffer);
319 }
320 buffer_size_ = buffer_size;
321 own_buffer_ = true;
322 } else {
Andrei Popescu31002712010-02-23 13:46:05 +0000323 // Use externally provided buffer instead.
Steve Blocka7e24c12009-10-30 11:49:00 +0000324 ASSERT(buffer_size > 0);
325 buffer_ = static_cast<byte*>(buffer);
326 buffer_size_ = buffer_size;
327 own_buffer_ = false;
328 }
329
330 // Clear the buffer in debug mode unless it was provided by the
331 // caller in which case we can't be sure it's okay to overwrite
332 // existing code in it; see CodePatcher::CodePatcher(...).
333#ifdef DEBUG
334 if (own_buffer_) {
335 memset(buffer_, 0xCC, buffer_size); // int3
336 }
337#endif
338
Andrei Popescu31002712010-02-23 13:46:05 +0000339 // Setup buffer pointers.
Steve Blocka7e24c12009-10-30 11:49:00 +0000340 ASSERT(buffer_ != NULL);
341 pc_ = buffer_;
342 reloc_info_writer.Reposition(buffer_ + buffer_size, pc_);
343
Steve Blocka7e24c12009-10-30 11:49:00 +0000344#ifdef GENERATED_CODE_COVERAGE
345 InitCoverageLog();
346#endif
347}
348
349
350Assembler::~Assembler() {
351 if (own_buffer_) {
Steve Block44f0eee2011-05-26 01:26:41 +0100352 if (isolate()->assembler_spare_buffer() == NULL &&
353 buffer_size_ == kMinimalBufferSize) {
354 isolate()->set_assembler_spare_buffer(buffer_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000355 } else {
356 DeleteArray(buffer_);
357 }
358 }
359}
360
361
362void Assembler::GetCode(CodeDesc* desc) {
Andrei Popescu31002712010-02-23 13:46:05 +0000363 // Finalize code (at this point overflow() may be true, but the gap ensures
364 // that we are still not overlapping instructions and relocation info).
365 ASSERT(pc_ <= reloc_info_writer.pos()); // No overlap.
366 // Setup code descriptor.
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 desc->buffer = buffer_;
368 desc->buffer_size = buffer_size_;
369 desc->instr_size = pc_offset();
370 desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
371 desc->origin = this;
Steve Blocka7e24c12009-10-30 11:49:00 +0000372}
373
374
375void Assembler::Align(int m) {
376 ASSERT(IsPowerOf2(m));
377 while ((pc_offset() & (m - 1)) != 0) {
378 nop();
379 }
380}
381
382
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100383void Assembler::CodeTargetAlign() {
384 Align(16); // Preferred alignment of jump targets on ia32.
385}
386
387
Steve Blocka7e24c12009-10-30 11:49:00 +0000388void Assembler::cpuid() {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100389 ASSERT(CpuFeatures::IsEnabled(CPUID));
Steve Blocka7e24c12009-10-30 11:49:00 +0000390 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000391 EMIT(0x0F);
392 EMIT(0xA2);
393}
394
395
396void Assembler::pushad() {
397 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000398 EMIT(0x60);
399}
400
401
402void Assembler::popad() {
403 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 EMIT(0x61);
405}
406
407
408void Assembler::pushfd() {
409 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000410 EMIT(0x9C);
411}
412
413
414void Assembler::popfd() {
415 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000416 EMIT(0x9D);
417}
418
419
420void Assembler::push(const Immediate& x) {
421 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000422 if (x.is_int8()) {
423 EMIT(0x6a);
424 EMIT(x.x_);
425 } else {
426 EMIT(0x68);
427 emit(x);
428 }
429}
430
431
Ben Murdochb0fe1622011-05-05 13:52:32 +0100432void Assembler::push_imm32(int32_t imm32) {
433 EnsureSpace ensure_space(this);
434 EMIT(0x68);
435 emit(imm32);
436}
437
438
Steve Blocka7e24c12009-10-30 11:49:00 +0000439void Assembler::push(Register src) {
440 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000441 EMIT(0x50 | src.code());
442}
443
444
445void Assembler::push(const Operand& src) {
446 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000447 EMIT(0xFF);
448 emit_operand(esi, src);
449}
450
451
452void Assembler::pop(Register dst) {
453 ASSERT(reloc_info_writer.last_pc() != NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000454 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000455 EMIT(0x58 | dst.code());
456}
457
458
459void Assembler::pop(const Operand& dst) {
460 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000461 EMIT(0x8F);
462 emit_operand(eax, dst);
463}
464
465
466void Assembler::enter(const Immediate& size) {
467 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000468 EMIT(0xC8);
469 emit_w(size);
470 EMIT(0);
471}
472
473
474void Assembler::leave() {
475 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000476 EMIT(0xC9);
477}
478
479
480void Assembler::mov_b(Register dst, const Operand& src) {
Leon Clarkee46be812010-01-19 14:06:41 +0000481 ASSERT(dst.code() < 4);
Steve Blocka7e24c12009-10-30 11:49:00 +0000482 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000483 EMIT(0x8A);
484 emit_operand(dst, src);
485}
486
487
488void Assembler::mov_b(const Operand& dst, int8_t imm8) {
489 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000490 EMIT(0xC6);
491 emit_operand(eax, dst);
492 EMIT(imm8);
493}
494
495
496void Assembler::mov_b(const Operand& dst, Register src) {
Leon Clarkee46be812010-01-19 14:06:41 +0000497 ASSERT(src.code() < 4);
Steve Blocka7e24c12009-10-30 11:49:00 +0000498 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 EMIT(0x88);
500 emit_operand(src, dst);
501}
502
503
504void Assembler::mov_w(Register dst, const Operand& src) {
505 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000506 EMIT(0x66);
507 EMIT(0x8B);
508 emit_operand(dst, src);
509}
510
511
512void Assembler::mov_w(const Operand& dst, Register src) {
513 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000514 EMIT(0x66);
515 EMIT(0x89);
516 emit_operand(src, dst);
517}
518
519
520void Assembler::mov(Register dst, int32_t imm32) {
521 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000522 EMIT(0xB8 | dst.code());
523 emit(imm32);
524}
525
526
527void Assembler::mov(Register dst, const Immediate& x) {
528 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000529 EMIT(0xB8 | dst.code());
530 emit(x);
531}
532
533
534void Assembler::mov(Register dst, Handle<Object> handle) {
535 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 EMIT(0xB8 | dst.code());
537 emit(handle);
538}
539
540
541void Assembler::mov(Register dst, const Operand& src) {
542 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000543 EMIT(0x8B);
544 emit_operand(dst, src);
545}
546
547
548void Assembler::mov(Register dst, Register src) {
549 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000550 EMIT(0x89);
551 EMIT(0xC0 | src.code() << 3 | dst.code());
552}
553
554
555void Assembler::mov(const Operand& dst, const Immediate& x) {
556 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000557 EMIT(0xC7);
558 emit_operand(eax, dst);
559 emit(x);
560}
561
562
563void Assembler::mov(const Operand& dst, Handle<Object> handle) {
564 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000565 EMIT(0xC7);
566 emit_operand(eax, dst);
567 emit(handle);
568}
569
570
571void Assembler::mov(const Operand& dst, Register src) {
572 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000573 EMIT(0x89);
574 emit_operand(src, dst);
575}
576
577
578void Assembler::movsx_b(Register dst, const Operand& src) {
579 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000580 EMIT(0x0F);
581 EMIT(0xBE);
582 emit_operand(dst, src);
583}
584
585
586void Assembler::movsx_w(Register dst, const Operand& src) {
587 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000588 EMIT(0x0F);
589 EMIT(0xBF);
590 emit_operand(dst, src);
591}
592
593
594void Assembler::movzx_b(Register dst, const Operand& src) {
595 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000596 EMIT(0x0F);
597 EMIT(0xB6);
598 emit_operand(dst, src);
599}
600
601
602void Assembler::movzx_w(Register dst, const Operand& src) {
603 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000604 EMIT(0x0F);
605 EMIT(0xB7);
606 emit_operand(dst, src);
607}
608
609
610void Assembler::cmov(Condition cc, Register dst, int32_t imm32) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100611 ASSERT(CpuFeatures::IsEnabled(CMOV));
Steve Blocka7e24c12009-10-30 11:49:00 +0000612 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000613 UNIMPLEMENTED();
614 USE(cc);
615 USE(dst);
616 USE(imm32);
617}
618
619
620void Assembler::cmov(Condition cc, Register dst, Handle<Object> handle) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100621 ASSERT(CpuFeatures::IsEnabled(CMOV));
Steve Blocka7e24c12009-10-30 11:49:00 +0000622 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000623 UNIMPLEMENTED();
624 USE(cc);
625 USE(dst);
626 USE(handle);
627}
628
629
630void Assembler::cmov(Condition cc, Register dst, const Operand& src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100631 ASSERT(CpuFeatures::IsEnabled(CMOV));
Steve Blocka7e24c12009-10-30 11:49:00 +0000632 EnsureSpace ensure_space(this);
Andrei Popescu31002712010-02-23 13:46:05 +0000633 // Opcode: 0f 40 + cc /r.
Steve Blocka7e24c12009-10-30 11:49:00 +0000634 EMIT(0x0F);
635 EMIT(0x40 + cc);
636 emit_operand(dst, src);
637}
638
639
Steve Block6ded16b2010-05-10 14:33:55 +0100640void Assembler::cld() {
641 EnsureSpace ensure_space(this);
Steve Block6ded16b2010-05-10 14:33:55 +0100642 EMIT(0xFC);
643}
644
645
Leon Clarkee46be812010-01-19 14:06:41 +0000646void Assembler::rep_movs() {
647 EnsureSpace ensure_space(this);
Leon Clarkee46be812010-01-19 14:06:41 +0000648 EMIT(0xF3);
649 EMIT(0xA5);
650}
651
652
Steve Block6ded16b2010-05-10 14:33:55 +0100653void Assembler::rep_stos() {
654 EnsureSpace ensure_space(this);
Steve Block6ded16b2010-05-10 14:33:55 +0100655 EMIT(0xF3);
656 EMIT(0xAB);
657}
658
659
Leon Clarkef7060e22010-06-03 12:02:55 +0100660void Assembler::stos() {
661 EnsureSpace ensure_space(this);
Leon Clarkef7060e22010-06-03 12:02:55 +0100662 EMIT(0xAB);
663}
664
665
Steve Blocka7e24c12009-10-30 11:49:00 +0000666void Assembler::xchg(Register dst, Register src) {
667 EnsureSpace ensure_space(this);
Andrei Popescu31002712010-02-23 13:46:05 +0000668 if (src.is(eax) || dst.is(eax)) { // Single-byte encoding.
Steve Blocka7e24c12009-10-30 11:49:00 +0000669 EMIT(0x90 | (src.is(eax) ? dst.code() : src.code()));
670 } else {
671 EMIT(0x87);
672 EMIT(0xC0 | src.code() << 3 | dst.code());
673 }
674}
675
676
677void Assembler::adc(Register dst, int32_t imm32) {
678 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000679 emit_arith(2, Operand(dst), Immediate(imm32));
680}
681
682
683void Assembler::adc(Register dst, const Operand& src) {
684 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000685 EMIT(0x13);
686 emit_operand(dst, src);
687}
688
689
690void Assembler::add(Register dst, const Operand& src) {
691 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000692 EMIT(0x03);
693 emit_operand(dst, src);
694}
695
696
697void Assembler::add(const Operand& dst, const Immediate& x) {
698 ASSERT(reloc_info_writer.last_pc() != NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000699 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000700 emit_arith(0, dst, x);
701}
702
703
704void Assembler::and_(Register dst, int32_t imm32) {
Steve Block59151502010-09-22 15:07:15 +0100705 and_(dst, Immediate(imm32));
706}
707
708
709void Assembler::and_(Register dst, const Immediate& x) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000710 EnsureSpace ensure_space(this);
Steve Block59151502010-09-22 15:07:15 +0100711 emit_arith(4, Operand(dst), x);
Steve Blocka7e24c12009-10-30 11:49:00 +0000712}
713
714
715void Assembler::and_(Register dst, const Operand& src) {
716 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000717 EMIT(0x23);
718 emit_operand(dst, src);
719}
720
721
722void Assembler::and_(const Operand& dst, const Immediate& x) {
723 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000724 emit_arith(4, dst, x);
725}
726
727
728void Assembler::and_(const Operand& dst, Register src) {
729 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000730 EMIT(0x21);
731 emit_operand(src, dst);
732}
733
734
735void Assembler::cmpb(const Operand& op, int8_t imm8) {
736 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000737 EMIT(0x80);
738 emit_operand(edi, op); // edi == 7
739 EMIT(imm8);
740}
741
742
Leon Clarked91b9f72010-01-27 17:25:45 +0000743void Assembler::cmpb(const Operand& dst, Register src) {
744 ASSERT(src.is_byte_register());
745 EnsureSpace ensure_space(this);
Leon Clarked91b9f72010-01-27 17:25:45 +0000746 EMIT(0x38);
747 emit_operand(src, dst);
748}
749
750
751void Assembler::cmpb(Register dst, const Operand& src) {
752 ASSERT(dst.is_byte_register());
753 EnsureSpace ensure_space(this);
Leon Clarked91b9f72010-01-27 17:25:45 +0000754 EMIT(0x3A);
755 emit_operand(dst, src);
756}
757
758
Steve Blocka7e24c12009-10-30 11:49:00 +0000759void Assembler::cmpw(const Operand& op, Immediate imm16) {
760 ASSERT(imm16.is_int16());
761 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000762 EMIT(0x66);
763 EMIT(0x81);
764 emit_operand(edi, op);
765 emit_w(imm16);
766}
767
768
769void Assembler::cmp(Register reg, int32_t imm32) {
770 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000771 emit_arith(7, Operand(reg), Immediate(imm32));
772}
773
774
775void Assembler::cmp(Register reg, Handle<Object> handle) {
776 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000777 emit_arith(7, Operand(reg), Immediate(handle));
778}
779
780
781void Assembler::cmp(Register reg, const Operand& op) {
782 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000783 EMIT(0x3B);
784 emit_operand(reg, op);
785}
786
787
788void Assembler::cmp(const Operand& op, const Immediate& imm) {
789 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000790 emit_arith(7, op, imm);
791}
792
793
794void Assembler::cmp(const Operand& op, Handle<Object> handle) {
795 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000796 emit_arith(7, op, Immediate(handle));
797}
798
799
800void Assembler::cmpb_al(const Operand& op) {
801 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000802 EMIT(0x38); // CMP r/m8, r8
803 emit_operand(eax, op); // eax has same code as register al.
804}
805
806
807void Assembler::cmpw_ax(const Operand& op) {
808 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000809 EMIT(0x66);
810 EMIT(0x39); // CMP r/m16, r16
811 emit_operand(eax, op); // eax has same code as register ax.
812}
813
814
815void Assembler::dec_b(Register dst) {
816 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000817 EMIT(0xFE);
818 EMIT(0xC8 | dst.code());
819}
820
821
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100822void Assembler::dec_b(const Operand& dst) {
823 EnsureSpace ensure_space(this);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100824 EMIT(0xFE);
825 emit_operand(ecx, dst);
826}
827
828
Steve Blocka7e24c12009-10-30 11:49:00 +0000829void Assembler::dec(Register dst) {
830 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000831 EMIT(0x48 | dst.code());
832}
833
834
835void Assembler::dec(const Operand& dst) {
836 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000837 EMIT(0xFF);
838 emit_operand(ecx, dst);
839}
840
841
842void Assembler::cdq() {
843 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000844 EMIT(0x99);
845}
846
847
848void Assembler::idiv(Register src) {
849 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000850 EMIT(0xF7);
851 EMIT(0xF8 | src.code());
852}
853
854
855void Assembler::imul(Register reg) {
856 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000857 EMIT(0xF7);
858 EMIT(0xE8 | reg.code());
859}
860
861
862void Assembler::imul(Register dst, const Operand& src) {
863 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000864 EMIT(0x0F);
865 EMIT(0xAF);
866 emit_operand(dst, src);
867}
868
869
870void Assembler::imul(Register dst, Register src, int32_t imm32) {
871 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000872 if (is_int8(imm32)) {
873 EMIT(0x6B);
874 EMIT(0xC0 | dst.code() << 3 | src.code());
875 EMIT(imm32);
876 } else {
877 EMIT(0x69);
878 EMIT(0xC0 | dst.code() << 3 | src.code());
879 emit(imm32);
880 }
881}
882
883
884void Assembler::inc(Register dst) {
885 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000886 EMIT(0x40 | dst.code());
887}
888
889
890void Assembler::inc(const Operand& dst) {
891 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000892 EMIT(0xFF);
893 emit_operand(eax, dst);
894}
895
896
897void Assembler::lea(Register dst, const Operand& src) {
898 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000899 EMIT(0x8D);
900 emit_operand(dst, src);
901}
902
903
904void Assembler::mul(Register src) {
905 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000906 EMIT(0xF7);
907 EMIT(0xE0 | src.code());
908}
909
910
911void Assembler::neg(Register dst) {
912 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000913 EMIT(0xF7);
914 EMIT(0xD8 | dst.code());
915}
916
917
918void Assembler::not_(Register dst) {
919 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000920 EMIT(0xF7);
921 EMIT(0xD0 | dst.code());
922}
923
924
925void Assembler::or_(Register dst, int32_t imm32) {
926 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000927 emit_arith(1, Operand(dst), Immediate(imm32));
928}
929
930
931void Assembler::or_(Register dst, const Operand& src) {
932 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000933 EMIT(0x0B);
934 emit_operand(dst, src);
935}
936
937
938void Assembler::or_(const Operand& dst, const Immediate& x) {
939 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000940 emit_arith(1, dst, x);
941}
942
943
944void Assembler::or_(const Operand& dst, Register src) {
945 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000946 EMIT(0x09);
947 emit_operand(src, dst);
948}
949
950
951void Assembler::rcl(Register dst, uint8_t imm8) {
952 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000953 ASSERT(is_uint5(imm8)); // illegal shift count
954 if (imm8 == 1) {
955 EMIT(0xD1);
956 EMIT(0xD0 | dst.code());
957 } else {
958 EMIT(0xC1);
959 EMIT(0xD0 | dst.code());
960 EMIT(imm8);
961 }
962}
963
964
Iain Merrick75681382010-08-19 15:07:18 +0100965void Assembler::rcr(Register dst, uint8_t imm8) {
966 EnsureSpace ensure_space(this);
Iain Merrick75681382010-08-19 15:07:18 +0100967 ASSERT(is_uint5(imm8)); // illegal shift count
968 if (imm8 == 1) {
969 EMIT(0xD1);
970 EMIT(0xD8 | dst.code());
971 } else {
972 EMIT(0xC1);
973 EMIT(0xD8 | dst.code());
974 EMIT(imm8);
975 }
976}
977
978
Steve Blocka7e24c12009-10-30 11:49:00 +0000979void Assembler::sar(Register dst, uint8_t imm8) {
980 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000981 ASSERT(is_uint5(imm8)); // illegal shift count
982 if (imm8 == 1) {
983 EMIT(0xD1);
984 EMIT(0xF8 | dst.code());
985 } else {
986 EMIT(0xC1);
987 EMIT(0xF8 | dst.code());
988 EMIT(imm8);
989 }
990}
991
992
Steve Blockd0582a62009-12-15 09:54:21 +0000993void Assembler::sar_cl(Register dst) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000994 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000995 EMIT(0xD3);
996 EMIT(0xF8 | dst.code());
997}
998
999
1000void Assembler::sbb(Register dst, const Operand& src) {
1001 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001002 EMIT(0x1B);
1003 emit_operand(dst, src);
1004}
1005
1006
1007void Assembler::shld(Register dst, const Operand& src) {
1008 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001009 EMIT(0x0F);
1010 EMIT(0xA5);
1011 emit_operand(dst, src);
1012}
1013
1014
1015void Assembler::shl(Register dst, uint8_t imm8) {
1016 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001017 ASSERT(is_uint5(imm8)); // illegal shift count
1018 if (imm8 == 1) {
1019 EMIT(0xD1);
1020 EMIT(0xE0 | dst.code());
1021 } else {
1022 EMIT(0xC1);
1023 EMIT(0xE0 | dst.code());
1024 EMIT(imm8);
1025 }
1026}
1027
1028
Steve Blockd0582a62009-12-15 09:54:21 +00001029void Assembler::shl_cl(Register dst) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001030 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001031 EMIT(0xD3);
1032 EMIT(0xE0 | dst.code());
1033}
1034
1035
1036void Assembler::shrd(Register dst, const Operand& src) {
1037 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001038 EMIT(0x0F);
1039 EMIT(0xAD);
1040 emit_operand(dst, src);
1041}
1042
1043
1044void Assembler::shr(Register dst, uint8_t imm8) {
1045 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001046 ASSERT(is_uint5(imm8)); // illegal shift count
Steve Blockd0582a62009-12-15 09:54:21 +00001047 if (imm8 == 1) {
1048 EMIT(0xD1);
1049 EMIT(0xE8 | dst.code());
1050 } else {
1051 EMIT(0xC1);
1052 EMIT(0xE8 | dst.code());
1053 EMIT(imm8);
1054 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001055}
1056
1057
1058void Assembler::shr_cl(Register dst) {
1059 EnsureSpace ensure_space(this);
Steve Blockd0582a62009-12-15 09:54:21 +00001060 EMIT(0xD3);
Steve Blocka7e24c12009-10-30 11:49:00 +00001061 EMIT(0xE8 | dst.code());
1062}
1063
1064
Steve Block3ce2e202009-11-05 08:53:23 +00001065void Assembler::subb(const Operand& op, int8_t imm8) {
1066 EnsureSpace ensure_space(this);
Steve Block3ce2e202009-11-05 08:53:23 +00001067 if (op.is_reg(eax)) {
1068 EMIT(0x2c);
1069 } else {
1070 EMIT(0x80);
1071 emit_operand(ebp, op); // ebp == 5
1072 }
1073 EMIT(imm8);
1074}
1075
1076
Steve Blocka7e24c12009-10-30 11:49:00 +00001077void Assembler::sub(const Operand& dst, const Immediate& x) {
1078 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001079 emit_arith(5, dst, x);
1080}
1081
1082
1083void Assembler::sub(Register dst, const Operand& src) {
1084 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001085 EMIT(0x2B);
1086 emit_operand(dst, src);
1087}
1088
1089
Leon Clarkee46be812010-01-19 14:06:41 +00001090void Assembler::subb(Register dst, const Operand& src) {
1091 ASSERT(dst.code() < 4);
1092 EnsureSpace ensure_space(this);
Leon Clarkee46be812010-01-19 14:06:41 +00001093 EMIT(0x2A);
1094 emit_operand(dst, src);
1095}
1096
1097
Steve Blocka7e24c12009-10-30 11:49:00 +00001098void Assembler::sub(const Operand& dst, Register src) {
1099 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001100 EMIT(0x29);
1101 emit_operand(src, dst);
1102}
1103
1104
1105void Assembler::test(Register reg, const Immediate& imm) {
1106 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001107 // Only use test against byte for registers that have a byte
1108 // variant: eax, ebx, ecx, and edx.
1109 if (imm.rmode_ == RelocInfo::NONE && is_uint8(imm.x_) && reg.code() < 4) {
1110 uint8_t imm8 = imm.x_;
1111 if (reg.is(eax)) {
1112 EMIT(0xA8);
1113 EMIT(imm8);
1114 } else {
1115 emit_arith_b(0xF6, 0xC0, reg, imm8);
1116 }
1117 } else {
1118 // This is not using emit_arith because test doesn't support
1119 // sign-extension of 8-bit operands.
1120 if (reg.is(eax)) {
1121 EMIT(0xA9);
1122 } else {
1123 EMIT(0xF7);
1124 EMIT(0xC0 | reg.code());
1125 }
1126 emit(imm);
1127 }
1128}
1129
1130
1131void Assembler::test(Register reg, const Operand& op) {
1132 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001133 EMIT(0x85);
1134 emit_operand(reg, op);
1135}
1136
1137
Leon Clarkee46be812010-01-19 14:06:41 +00001138void Assembler::test_b(Register reg, const Operand& op) {
1139 EnsureSpace ensure_space(this);
Leon Clarkee46be812010-01-19 14:06:41 +00001140 EMIT(0x84);
1141 emit_operand(reg, op);
1142}
1143
1144
Steve Blocka7e24c12009-10-30 11:49:00 +00001145void Assembler::test(const Operand& op, const Immediate& imm) {
1146 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001147 EMIT(0xF7);
1148 emit_operand(eax, op);
1149 emit(imm);
1150}
1151
1152
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001153void Assembler::test_b(const Operand& op, uint8_t imm8) {
1154 EnsureSpace ensure_space(this);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001155 EMIT(0xF6);
1156 emit_operand(eax, op);
1157 EMIT(imm8);
1158}
1159
1160
Steve Blocka7e24c12009-10-30 11:49:00 +00001161void Assembler::xor_(Register dst, int32_t imm32) {
1162 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001163 emit_arith(6, Operand(dst), Immediate(imm32));
1164}
1165
1166
1167void Assembler::xor_(Register dst, const Operand& src) {
1168 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001169 EMIT(0x33);
1170 emit_operand(dst, src);
1171}
1172
1173
1174void Assembler::xor_(const Operand& src, Register dst) {
1175 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001176 EMIT(0x31);
1177 emit_operand(dst, src);
1178}
1179
1180
1181void Assembler::xor_(const Operand& dst, const Immediate& x) {
1182 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001183 emit_arith(6, dst, x);
1184}
1185
1186
1187void Assembler::bt(const Operand& dst, Register src) {
1188 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001189 EMIT(0x0F);
1190 EMIT(0xA3);
1191 emit_operand(src, dst);
1192}
1193
1194
1195void Assembler::bts(const Operand& dst, Register src) {
1196 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001197 EMIT(0x0F);
1198 EMIT(0xAB);
1199 emit_operand(src, dst);
1200}
1201
1202
1203void Assembler::hlt() {
1204 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001205 EMIT(0xF4);
1206}
1207
1208
1209void Assembler::int3() {
1210 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001211 EMIT(0xCC);
1212}
1213
1214
1215void Assembler::nop() {
1216 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001217 EMIT(0x90);
1218}
1219
1220
1221void Assembler::rdtsc() {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001222 ASSERT(CpuFeatures::IsEnabled(RDTSC));
Steve Blocka7e24c12009-10-30 11:49:00 +00001223 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001224 EMIT(0x0F);
1225 EMIT(0x31);
1226}
1227
1228
1229void Assembler::ret(int imm16) {
1230 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001231 ASSERT(is_uint16(imm16));
1232 if (imm16 == 0) {
1233 EMIT(0xC3);
1234 } else {
1235 EMIT(0xC2);
1236 EMIT(imm16 & 0xFF);
1237 EMIT((imm16 >> 8) & 0xFF);
1238 }
1239}
1240
1241
1242// Labels refer to positions in the (to be) generated code.
1243// There are bound, linked, and unused labels.
1244//
1245// Bound labels refer to known positions in the already
1246// generated code. pos() is the position the label refers to.
1247//
1248// Linked labels refer to unknown positions in the code
1249// to be generated; pos() is the position of the 32bit
1250// Displacement of the last instruction using the label.
1251
1252
1253void Assembler::print(Label* L) {
1254 if (L->is_unused()) {
1255 PrintF("unused label\n");
1256 } else if (L->is_bound()) {
1257 PrintF("bound label to %d\n", L->pos());
1258 } else if (L->is_linked()) {
1259 Label l = *L;
1260 PrintF("unbound label");
1261 while (l.is_linked()) {
1262 Displacement disp = disp_at(&l);
1263 PrintF("@ %d ", l.pos());
1264 disp.print();
1265 PrintF("\n");
1266 disp.next(&l);
1267 }
1268 } else {
1269 PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
1270 }
1271}
1272
1273
1274void Assembler::bind_to(Label* L, int pos) {
1275 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001276 ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position
1277 while (L->is_linked()) {
1278 Displacement disp = disp_at(L);
1279 int fixup_pos = L->pos();
1280 if (disp.type() == Displacement::CODE_RELATIVE) {
1281 // Relative to Code* heap object pointer.
1282 long_at_put(fixup_pos, pos + Code::kHeaderSize - kHeapObjectTag);
1283 } else {
1284 if (disp.type() == Displacement::UNCONDITIONAL_JUMP) {
1285 ASSERT(byte_at(fixup_pos - 1) == 0xE9); // jmp expected
1286 }
Andrei Popescu31002712010-02-23 13:46:05 +00001287 // Relative address, relative to point after address.
Steve Blocka7e24c12009-10-30 11:49:00 +00001288 int imm32 = pos - (fixup_pos + sizeof(int32_t));
1289 long_at_put(fixup_pos, imm32);
1290 }
1291 disp.next(L);
1292 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001293 while (L->is_near_linked()) {
1294 int fixup_pos = L->near_link_pos();
1295 int offset_to_next =
1296 static_cast<int>(*reinterpret_cast<int8_t*>(addr_at(fixup_pos)));
1297 ASSERT(offset_to_next <= 0);
1298 // Relative address, relative to point after address.
1299 int disp = pos - fixup_pos - sizeof(int8_t);
1300 ASSERT(0 <= disp && disp <= 127);
1301 set_byte_at(fixup_pos, disp);
1302 if (offset_to_next < 0) {
1303 L->link_to(fixup_pos + offset_to_next, Label::kNear);
1304 } else {
1305 L->UnuseNear();
1306 }
1307 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001308 L->bind_to(pos);
1309}
1310
1311
Steve Blocka7e24c12009-10-30 11:49:00 +00001312void Assembler::bind(Label* L) {
1313 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001314 ASSERT(!L->is_bound()); // label can only be bound once
1315 bind_to(L, pc_offset());
1316}
1317
1318
1319void Assembler::call(Label* L) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001320 positions_recorder()->WriteRecordedPositions();
Steve Blocka7e24c12009-10-30 11:49:00 +00001321 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001322 if (L->is_bound()) {
1323 const int long_size = 5;
1324 int offs = L->pos() - pc_offset();
1325 ASSERT(offs <= 0);
Andrei Popescu31002712010-02-23 13:46:05 +00001326 // 1110 1000 #32-bit disp.
Steve Blocka7e24c12009-10-30 11:49:00 +00001327 EMIT(0xE8);
1328 emit(offs - long_size);
1329 } else {
Andrei Popescu31002712010-02-23 13:46:05 +00001330 // 1110 1000 #32-bit disp.
Steve Blocka7e24c12009-10-30 11:49:00 +00001331 EMIT(0xE8);
1332 emit_disp(L, Displacement::OTHER);
1333 }
1334}
1335
1336
1337void Assembler::call(byte* entry, RelocInfo::Mode rmode) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001338 positions_recorder()->WriteRecordedPositions();
Steve Blocka7e24c12009-10-30 11:49:00 +00001339 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001340 ASSERT(!RelocInfo::IsCodeTarget(rmode));
1341 EMIT(0xE8);
1342 emit(entry - (pc_ + sizeof(int32_t)), rmode);
1343}
1344
1345
Ben Murdoch257744e2011-11-30 15:57:28 +00001346int Assembler::CallSize(const Operand& adr) {
1347 // Call size is 1 (opcode) + adr.len_ (operand).
1348 return 1 + adr.len_;
1349}
1350
1351
Steve Blocka7e24c12009-10-30 11:49:00 +00001352void Assembler::call(const Operand& adr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001353 positions_recorder()->WriteRecordedPositions();
Steve Blocka7e24c12009-10-30 11:49:00 +00001354 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001355 EMIT(0xFF);
1356 emit_operand(edx, adr);
1357}
1358
1359
Ben Murdoch257744e2011-11-30 15:57:28 +00001360int Assembler::CallSize(Handle<Code> code, RelocInfo::Mode rmode) {
1361 return 1 /* EMIT */ + sizeof(uint32_t) /* emit */;
Steve Blocka7e24c12009-10-30 11:49:00 +00001362}
1363
1364
Ben Murdoch257744e2011-11-30 15:57:28 +00001365void Assembler::call(Handle<Code> code,
1366 RelocInfo::Mode rmode,
1367 unsigned ast_id) {
1368 positions_recorder()->WriteRecordedPositions();
Steve Blocka7e24c12009-10-30 11:49:00 +00001369 EnsureSpace ensure_space(this);
Ben Murdoch257744e2011-11-30 15:57:28 +00001370 ASSERT(RelocInfo::IsCodeTarget(rmode));
1371 EMIT(0xE8);
1372 emit(reinterpret_cast<intptr_t>(code.location()), rmode, ast_id);
1373}
1374
1375
1376void Assembler::jmp(Label* L, Label::Distance distance) {
1377 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001378 if (L->is_bound()) {
1379 const int short_size = 2;
1380 const int long_size = 5;
1381 int offs = L->pos() - pc_offset();
1382 ASSERT(offs <= 0);
1383 if (is_int8(offs - short_size)) {
Andrei Popescu31002712010-02-23 13:46:05 +00001384 // 1110 1011 #8-bit disp.
Steve Blocka7e24c12009-10-30 11:49:00 +00001385 EMIT(0xEB);
1386 EMIT((offs - short_size) & 0xFF);
1387 } else {
Andrei Popescu31002712010-02-23 13:46:05 +00001388 // 1110 1001 #32-bit disp.
Steve Blocka7e24c12009-10-30 11:49:00 +00001389 EMIT(0xE9);
1390 emit(offs - long_size);
1391 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001392 } else if (distance == Label::kNear) {
1393 EMIT(0xEB);
1394 emit_near_disp(L);
Steve Blocka7e24c12009-10-30 11:49:00 +00001395 } else {
Andrei Popescu31002712010-02-23 13:46:05 +00001396 // 1110 1001 #32-bit disp.
Steve Blocka7e24c12009-10-30 11:49:00 +00001397 EMIT(0xE9);
1398 emit_disp(L, Displacement::UNCONDITIONAL_JUMP);
1399 }
1400}
1401
1402
1403void Assembler::jmp(byte* entry, RelocInfo::Mode rmode) {
1404 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001405 ASSERT(!RelocInfo::IsCodeTarget(rmode));
1406 EMIT(0xE9);
1407 emit(entry - (pc_ + sizeof(int32_t)), rmode);
1408}
1409
1410
1411void Assembler::jmp(const Operand& adr) {
1412 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001413 EMIT(0xFF);
1414 emit_operand(esp, adr);
1415}
1416
1417
1418void Assembler::jmp(Handle<Code> code, RelocInfo::Mode rmode) {
1419 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001420 ASSERT(RelocInfo::IsCodeTarget(rmode));
1421 EMIT(0xE9);
1422 emit(reinterpret_cast<intptr_t>(code.location()), rmode);
1423}
1424
1425
Ben Murdoch257744e2011-11-30 15:57:28 +00001426void Assembler::j(Condition cc, Label* L, Label::Distance distance) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001427 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001428 ASSERT(0 <= cc && cc < 16);
Steve Blocka7e24c12009-10-30 11:49:00 +00001429 if (L->is_bound()) {
1430 const int short_size = 2;
1431 const int long_size = 6;
1432 int offs = L->pos() - pc_offset();
1433 ASSERT(offs <= 0);
1434 if (is_int8(offs - short_size)) {
1435 // 0111 tttn #8-bit disp
1436 EMIT(0x70 | cc);
1437 EMIT((offs - short_size) & 0xFF);
1438 } else {
1439 // 0000 1111 1000 tttn #32-bit disp
1440 EMIT(0x0F);
1441 EMIT(0x80 | cc);
1442 emit(offs - long_size);
1443 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001444 } else if (distance == Label::kNear) {
1445 EMIT(0x70 | cc);
1446 emit_near_disp(L);
Steve Blocka7e24c12009-10-30 11:49:00 +00001447 } else {
1448 // 0000 1111 1000 tttn #32-bit disp
1449 // Note: could eliminate cond. jumps to this jump if condition
1450 // is the same however, seems to be rather unlikely case.
1451 EMIT(0x0F);
1452 EMIT(0x80 | cc);
1453 emit_disp(L, Displacement::OTHER);
1454 }
1455}
1456
1457
Ben Murdoch257744e2011-11-30 15:57:28 +00001458void Assembler::j(Condition cc, byte* entry, RelocInfo::Mode rmode) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001459 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001460 ASSERT((0 <= cc) && (cc < 16));
Andrei Popescu31002712010-02-23 13:46:05 +00001461 // 0000 1111 1000 tttn #32-bit disp.
Steve Blocka7e24c12009-10-30 11:49:00 +00001462 EMIT(0x0F);
1463 EMIT(0x80 | cc);
1464 emit(entry - (pc_ + sizeof(int32_t)), rmode);
1465}
1466
1467
Ben Murdoch257744e2011-11-30 15:57:28 +00001468void Assembler::j(Condition cc, Handle<Code> code) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001469 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001470 // 0000 1111 1000 tttn #32-bit disp
1471 EMIT(0x0F);
1472 EMIT(0x80 | cc);
1473 emit(reinterpret_cast<intptr_t>(code.location()), RelocInfo::CODE_TARGET);
1474}
1475
1476
Andrei Popescu31002712010-02-23 13:46:05 +00001477// FPU instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001478
Steve Blocka7e24c12009-10-30 11:49:00 +00001479void Assembler::fld(int i) {
1480 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001481 emit_farith(0xD9, 0xC0, i);
1482}
1483
1484
Andrei Popescu402d9372010-02-26 13:31:12 +00001485void Assembler::fstp(int i) {
1486 EnsureSpace ensure_space(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00001487 emit_farith(0xDD, 0xD8, i);
1488}
1489
1490
Steve Blocka7e24c12009-10-30 11:49:00 +00001491void Assembler::fld1() {
1492 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001493 EMIT(0xD9);
1494 EMIT(0xE8);
1495}
1496
1497
Andrei Popescu402d9372010-02-26 13:31:12 +00001498void Assembler::fldpi() {
1499 EnsureSpace ensure_space(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00001500 EMIT(0xD9);
1501 EMIT(0xEB);
1502}
1503
1504
Steve Blocka7e24c12009-10-30 11:49:00 +00001505void Assembler::fldz() {
1506 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001507 EMIT(0xD9);
1508 EMIT(0xEE);
1509}
1510
1511
Ben Murdochb0fe1622011-05-05 13:52:32 +01001512void Assembler::fldln2() {
1513 EnsureSpace ensure_space(this);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001514 EMIT(0xD9);
1515 EMIT(0xED);
1516}
1517
1518
Steve Blocka7e24c12009-10-30 11:49:00 +00001519void Assembler::fld_s(const Operand& adr) {
1520 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001521 EMIT(0xD9);
1522 emit_operand(eax, adr);
1523}
1524
1525
1526void Assembler::fld_d(const Operand& adr) {
1527 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001528 EMIT(0xDD);
1529 emit_operand(eax, adr);
1530}
1531
1532
1533void Assembler::fstp_s(const Operand& adr) {
1534 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001535 EMIT(0xD9);
1536 emit_operand(ebx, adr);
1537}
1538
1539
1540void Assembler::fstp_d(const Operand& adr) {
1541 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001542 EMIT(0xDD);
1543 emit_operand(ebx, adr);
1544}
1545
1546
Andrei Popescu402d9372010-02-26 13:31:12 +00001547void Assembler::fst_d(const Operand& adr) {
1548 EnsureSpace ensure_space(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00001549 EMIT(0xDD);
1550 emit_operand(edx, adr);
1551}
1552
1553
Steve Blocka7e24c12009-10-30 11:49:00 +00001554void Assembler::fild_s(const Operand& adr) {
1555 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001556 EMIT(0xDB);
1557 emit_operand(eax, adr);
1558}
1559
1560
1561void Assembler::fild_d(const Operand& adr) {
1562 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001563 EMIT(0xDF);
1564 emit_operand(ebp, adr);
1565}
1566
1567
1568void Assembler::fistp_s(const Operand& adr) {
1569 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001570 EMIT(0xDB);
1571 emit_operand(ebx, adr);
1572}
1573
1574
1575void Assembler::fisttp_s(const Operand& adr) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001576 ASSERT(CpuFeatures::IsEnabled(SSE3));
Steve Blocka7e24c12009-10-30 11:49:00 +00001577 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001578 EMIT(0xDB);
1579 emit_operand(ecx, adr);
1580}
1581
1582
Leon Clarkee46be812010-01-19 14:06:41 +00001583void Assembler::fisttp_d(const Operand& adr) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001584 ASSERT(CpuFeatures::IsEnabled(SSE3));
Leon Clarkee46be812010-01-19 14:06:41 +00001585 EnsureSpace ensure_space(this);
Leon Clarkee46be812010-01-19 14:06:41 +00001586 EMIT(0xDD);
1587 emit_operand(ecx, adr);
1588}
1589
1590
Steve Blocka7e24c12009-10-30 11:49:00 +00001591void Assembler::fist_s(const Operand& adr) {
1592 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001593 EMIT(0xDB);
1594 emit_operand(edx, adr);
1595}
1596
1597
1598void Assembler::fistp_d(const Operand& adr) {
1599 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001600 EMIT(0xDF);
1601 emit_operand(edi, adr);
1602}
1603
1604
1605void Assembler::fabs() {
1606 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001607 EMIT(0xD9);
1608 EMIT(0xE1);
1609}
1610
1611
1612void Assembler::fchs() {
1613 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001614 EMIT(0xD9);
1615 EMIT(0xE0);
1616}
1617
1618
1619void Assembler::fcos() {
1620 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001621 EMIT(0xD9);
1622 EMIT(0xFF);
1623}
1624
1625
1626void Assembler::fsin() {
1627 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001628 EMIT(0xD9);
1629 EMIT(0xFE);
1630}
1631
1632
Ben Murdochb0fe1622011-05-05 13:52:32 +01001633void Assembler::fyl2x() {
1634 EnsureSpace ensure_space(this);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001635 EMIT(0xD9);
1636 EMIT(0xF1);
1637}
1638
1639
Steve Blocka7e24c12009-10-30 11:49:00 +00001640void Assembler::fadd(int i) {
1641 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001642 emit_farith(0xDC, 0xC0, i);
1643}
1644
1645
1646void Assembler::fsub(int i) {
1647 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001648 emit_farith(0xDC, 0xE8, i);
1649}
1650
1651
1652void Assembler::fisub_s(const Operand& adr) {
1653 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001654 EMIT(0xDA);
1655 emit_operand(esp, adr);
1656}
1657
1658
1659void Assembler::fmul(int i) {
1660 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001661 emit_farith(0xDC, 0xC8, i);
1662}
1663
1664
1665void Assembler::fdiv(int i) {
1666 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001667 emit_farith(0xDC, 0xF8, i);
1668}
1669
1670
1671void Assembler::faddp(int i) {
1672 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001673 emit_farith(0xDE, 0xC0, i);
1674}
1675
1676
1677void Assembler::fsubp(int i) {
1678 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001679 emit_farith(0xDE, 0xE8, i);
1680}
1681
1682
1683void Assembler::fsubrp(int i) {
1684 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001685 emit_farith(0xDE, 0xE0, i);
1686}
1687
1688
1689void Assembler::fmulp(int i) {
1690 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001691 emit_farith(0xDE, 0xC8, i);
1692}
1693
1694
1695void Assembler::fdivp(int i) {
1696 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001697 emit_farith(0xDE, 0xF8, i);
1698}
1699
1700
1701void Assembler::fprem() {
1702 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001703 EMIT(0xD9);
1704 EMIT(0xF8);
1705}
1706
1707
1708void Assembler::fprem1() {
1709 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001710 EMIT(0xD9);
1711 EMIT(0xF5);
1712}
1713
1714
1715void Assembler::fxch(int i) {
1716 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001717 emit_farith(0xD9, 0xC8, i);
1718}
1719
1720
1721void Assembler::fincstp() {
1722 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001723 EMIT(0xD9);
1724 EMIT(0xF7);
1725}
1726
1727
1728void Assembler::ffree(int i) {
1729 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001730 emit_farith(0xDD, 0xC0, i);
1731}
1732
1733
1734void Assembler::ftst() {
1735 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001736 EMIT(0xD9);
1737 EMIT(0xE4);
1738}
1739
1740
1741void Assembler::fucomp(int i) {
1742 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001743 emit_farith(0xDD, 0xE8, i);
1744}
1745
1746
1747void Assembler::fucompp() {
1748 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001749 EMIT(0xDA);
1750 EMIT(0xE9);
1751}
1752
1753
Steve Block3ce2e202009-11-05 08:53:23 +00001754void Assembler::fucomi(int i) {
1755 EnsureSpace ensure_space(this);
Steve Block3ce2e202009-11-05 08:53:23 +00001756 EMIT(0xDB);
1757 EMIT(0xE8 + i);
1758}
1759
1760
1761void Assembler::fucomip() {
1762 EnsureSpace ensure_space(this);
Steve Block3ce2e202009-11-05 08:53:23 +00001763 EMIT(0xDF);
1764 EMIT(0xE9);
1765}
1766
1767
Steve Blocka7e24c12009-10-30 11:49:00 +00001768void Assembler::fcompp() {
1769 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001770 EMIT(0xDE);
1771 EMIT(0xD9);
1772}
1773
1774
1775void Assembler::fnstsw_ax() {
1776 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001777 EMIT(0xDF);
1778 EMIT(0xE0);
1779}
1780
1781
1782void Assembler::fwait() {
1783 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001784 EMIT(0x9B);
1785}
1786
1787
1788void Assembler::frndint() {
1789 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001790 EMIT(0xD9);
1791 EMIT(0xFC);
1792}
1793
1794
1795void Assembler::fnclex() {
1796 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001797 EMIT(0xDB);
1798 EMIT(0xE2);
1799}
1800
1801
1802void Assembler::sahf() {
1803 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001804 EMIT(0x9E);
1805}
1806
1807
1808void Assembler::setcc(Condition cc, Register reg) {
1809 ASSERT(reg.is_byte_register());
1810 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001811 EMIT(0x0F);
1812 EMIT(0x90 | cc);
1813 EMIT(0xC0 | reg.code());
1814}
1815
1816
1817void Assembler::cvttss2si(Register dst, const Operand& src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001818 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001819 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001820 EMIT(0xF3);
1821 EMIT(0x0F);
1822 EMIT(0x2C);
1823 emit_operand(dst, src);
1824}
1825
1826
1827void Assembler::cvttsd2si(Register dst, const Operand& src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001828 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001829 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001830 EMIT(0xF2);
1831 EMIT(0x0F);
1832 EMIT(0x2C);
1833 emit_operand(dst, src);
1834}
1835
1836
1837void Assembler::cvtsi2sd(XMMRegister dst, const Operand& src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001838 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001839 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001840 EMIT(0xF2);
1841 EMIT(0x0F);
1842 EMIT(0x2A);
1843 emit_sse_operand(dst, src);
1844}
1845
1846
Steve Block6ded16b2010-05-10 14:33:55 +01001847void Assembler::cvtss2sd(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001848 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Block6ded16b2010-05-10 14:33:55 +01001849 EnsureSpace ensure_space(this);
Steve Block6ded16b2010-05-10 14:33:55 +01001850 EMIT(0xF3);
1851 EMIT(0x0F);
1852 EMIT(0x5A);
1853 emit_sse_operand(dst, src);
1854}
1855
1856
Steve Block44f0eee2011-05-26 01:26:41 +01001857void Assembler::cvtsd2ss(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001858 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Block44f0eee2011-05-26 01:26:41 +01001859 EnsureSpace ensure_space(this);
Steve Block44f0eee2011-05-26 01:26:41 +01001860 EMIT(0xF2);
1861 EMIT(0x0F);
1862 EMIT(0x5A);
1863 emit_sse_operand(dst, src);
1864}
1865
1866
Steve Blocka7e24c12009-10-30 11:49:00 +00001867void Assembler::addsd(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001868 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001869 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001870 EMIT(0xF2);
1871 EMIT(0x0F);
1872 EMIT(0x58);
1873 emit_sse_operand(dst, src);
1874}
1875
1876
1877void Assembler::mulsd(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001878 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001879 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001880 EMIT(0xF2);
1881 EMIT(0x0F);
1882 EMIT(0x59);
1883 emit_sse_operand(dst, src);
1884}
1885
1886
1887void Assembler::subsd(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001888 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001889 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001890 EMIT(0xF2);
1891 EMIT(0x0F);
1892 EMIT(0x5C);
1893 emit_sse_operand(dst, src);
1894}
1895
1896
1897void Assembler::divsd(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001898 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001899 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001900 EMIT(0xF2);
1901 EMIT(0x0F);
1902 EMIT(0x5E);
1903 emit_sse_operand(dst, src);
1904}
1905
1906
Leon Clarkee46be812010-01-19 14:06:41 +00001907void Assembler::xorpd(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001908 ASSERT(CpuFeatures::IsEnabled(SSE2));
Leon Clarkee46be812010-01-19 14:06:41 +00001909 EnsureSpace ensure_space(this);
Leon Clarkee46be812010-01-19 14:06:41 +00001910 EMIT(0x66);
1911 EMIT(0x0F);
1912 EMIT(0x57);
1913 emit_sse_operand(dst, src);
1914}
1915
1916
Ben Murdoch257744e2011-11-30 15:57:28 +00001917void Assembler::xorps(XMMRegister dst, XMMRegister src) {
1918 EnsureSpace ensure_space(this);
1919 EMIT(0x0F);
1920 EMIT(0x57);
1921 emit_sse_operand(dst, src);
1922}
1923
1924
Steve Block6ded16b2010-05-10 14:33:55 +01001925void Assembler::sqrtsd(XMMRegister dst, XMMRegister src) {
1926 EnsureSpace ensure_space(this);
Steve Block6ded16b2010-05-10 14:33:55 +01001927 EMIT(0xF2);
1928 EMIT(0x0F);
1929 EMIT(0x51);
1930 emit_sse_operand(dst, src);
1931}
1932
1933
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001934void Assembler::andpd(XMMRegister dst, XMMRegister src) {
1935 EnsureSpace ensure_space(this);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001936 EMIT(0x66);
1937 EMIT(0x0F);
1938 EMIT(0x54);
1939 emit_sse_operand(dst, src);
1940}
1941
1942
Steve Block6ded16b2010-05-10 14:33:55 +01001943void Assembler::ucomisd(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001944 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Block6ded16b2010-05-10 14:33:55 +01001945 EnsureSpace ensure_space(this);
Steve Block6ded16b2010-05-10 14:33:55 +01001946 EMIT(0x66);
1947 EMIT(0x0F);
1948 EMIT(0x2E);
1949 emit_sse_operand(dst, src);
1950}
1951
1952
1953void Assembler::movmskpd(Register dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001954 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Block6ded16b2010-05-10 14:33:55 +01001955 EnsureSpace ensure_space(this);
Steve Block6ded16b2010-05-10 14:33:55 +01001956 EMIT(0x66);
1957 EMIT(0x0F);
1958 EMIT(0x50);
1959 emit_sse_operand(dst, src);
1960}
1961
1962
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001963void Assembler::cmpltsd(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001964 ASSERT(CpuFeatures::IsEnabled(SSE2));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001965 EnsureSpace ensure_space(this);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001966 EMIT(0xF2);
1967 EMIT(0x0F);
1968 EMIT(0xC2);
1969 emit_sse_operand(dst, src);
1970 EMIT(1); // LT == 1
1971}
1972
1973
1974void Assembler::movaps(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001975 ASSERT(CpuFeatures::IsEnabled(SSE2));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001976 EnsureSpace ensure_space(this);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001977 EMIT(0x0F);
1978 EMIT(0x28);
1979 emit_sse_operand(dst, src);
1980}
1981
1982
1983void Assembler::movdqa(const Operand& dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001984 ASSERT(CpuFeatures::IsEnabled(SSE2));
Leon Clarkee46be812010-01-19 14:06:41 +00001985 EnsureSpace ensure_space(this);
Leon Clarkee46be812010-01-19 14:06:41 +00001986 EMIT(0x66);
1987 EMIT(0x0F);
1988 EMIT(0x7F);
1989 emit_sse_operand(src, dst);
1990}
1991
1992
1993void Assembler::movdqa(XMMRegister dst, const Operand& src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001994 ASSERT(CpuFeatures::IsEnabled(SSE2));
Leon Clarkee46be812010-01-19 14:06:41 +00001995 EnsureSpace ensure_space(this);
Leon Clarkee46be812010-01-19 14:06:41 +00001996 EMIT(0x66);
1997 EMIT(0x0F);
1998 EMIT(0x6F);
1999 emit_sse_operand(dst, src);
2000}
2001
2002
2003void Assembler::movdqu(const Operand& dst, XMMRegister src ) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002004 ASSERT(CpuFeatures::IsEnabled(SSE2));
Leon Clarkee46be812010-01-19 14:06:41 +00002005 EnsureSpace ensure_space(this);
Leon Clarkee46be812010-01-19 14:06:41 +00002006 EMIT(0xF3);
2007 EMIT(0x0F);
2008 EMIT(0x7F);
2009 emit_sse_operand(src, dst);
2010}
2011
2012
2013void Assembler::movdqu(XMMRegister dst, const Operand& src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002014 ASSERT(CpuFeatures::IsEnabled(SSE2));
Leon Clarkee46be812010-01-19 14:06:41 +00002015 EnsureSpace ensure_space(this);
Leon Clarkee46be812010-01-19 14:06:41 +00002016 EMIT(0xF3);
2017 EMIT(0x0F);
2018 EMIT(0x6F);
2019 emit_sse_operand(dst, src);
2020}
2021
2022
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002023void Assembler::movntdqa(XMMRegister dst, const Operand& src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002024 ASSERT(CpuFeatures::IsEnabled(SSE4_1));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002025 EnsureSpace ensure_space(this);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002026 EMIT(0x66);
2027 EMIT(0x0F);
2028 EMIT(0x38);
2029 EMIT(0x2A);
2030 emit_sse_operand(dst, src);
2031}
2032
2033
2034void Assembler::movntdq(const Operand& dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002035 ASSERT(CpuFeatures::IsEnabled(SSE2));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002036 EnsureSpace ensure_space(this);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002037 EMIT(0x66);
2038 EMIT(0x0F);
2039 EMIT(0xE7);
2040 emit_sse_operand(src, dst);
2041}
2042
2043
2044void Assembler::prefetch(const Operand& src, int level) {
2045 ASSERT(is_uint2(level));
2046 EnsureSpace ensure_space(this);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002047 EMIT(0x0F);
2048 EMIT(0x18);
2049 XMMRegister code = { level }; // Emit hint number in Reg position of RegR/M.
2050 emit_sse_operand(code, src);
2051}
2052
2053
Steve Blocka7e24c12009-10-30 11:49:00 +00002054void Assembler::movdbl(XMMRegister dst, const Operand& src) {
2055 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00002056 movsd(dst, src);
2057}
2058
2059
2060void Assembler::movdbl(const Operand& dst, XMMRegister src) {
2061 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00002062 movsd(dst, src);
2063}
2064
2065
2066void Assembler::movsd(const Operand& dst, XMMRegister src ) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002067 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00002068 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00002069 EMIT(0xF2); // double
2070 EMIT(0x0F);
2071 EMIT(0x11); // store
2072 emit_sse_operand(src, dst);
2073}
2074
2075
2076void Assembler::movsd(XMMRegister dst, const Operand& src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002077 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00002078 EnsureSpace ensure_space(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00002079 EMIT(0xF2); // double
2080 EMIT(0x0F);
2081 EMIT(0x10); // load
2082 emit_sse_operand(dst, src);
2083}
2084
Ben Murdochb0fe1622011-05-05 13:52:32 +01002085
Steve Block6ded16b2010-05-10 14:33:55 +01002086void Assembler::movsd(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002087 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Block6ded16b2010-05-10 14:33:55 +01002088 EnsureSpace ensure_space(this);
Steve Block6ded16b2010-05-10 14:33:55 +01002089 EMIT(0xF2);
2090 EMIT(0x0F);
2091 EMIT(0x10);
2092 emit_sse_operand(dst, src);
2093}
2094
2095
Steve Block44f0eee2011-05-26 01:26:41 +01002096void Assembler::movss(const Operand& dst, XMMRegister src ) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002097 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Block44f0eee2011-05-26 01:26:41 +01002098 EnsureSpace ensure_space(this);
Steve Block44f0eee2011-05-26 01:26:41 +01002099 EMIT(0xF3); // float
2100 EMIT(0x0F);
2101 EMIT(0x11); // store
2102 emit_sse_operand(src, dst);
2103}
2104
2105
2106void Assembler::movss(XMMRegister dst, const Operand& src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002107 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Block44f0eee2011-05-26 01:26:41 +01002108 EnsureSpace ensure_space(this);
Steve Block44f0eee2011-05-26 01:26:41 +01002109 EMIT(0xF3); // float
2110 EMIT(0x0F);
2111 EMIT(0x10); // load
2112 emit_sse_operand(dst, src);
2113}
2114
2115
2116void Assembler::movss(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002117 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Block44f0eee2011-05-26 01:26:41 +01002118 EnsureSpace ensure_space(this);
Steve Block44f0eee2011-05-26 01:26:41 +01002119 EMIT(0xF3);
2120 EMIT(0x0F);
2121 EMIT(0x10);
2122 emit_sse_operand(dst, src);
2123}
2124
2125
Steve Block6ded16b2010-05-10 14:33:55 +01002126void Assembler::movd(XMMRegister dst, const Operand& src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002127 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Block6ded16b2010-05-10 14:33:55 +01002128 EnsureSpace ensure_space(this);
Steve Block6ded16b2010-05-10 14:33:55 +01002129 EMIT(0x66);
2130 EMIT(0x0F);
2131 EMIT(0x6E);
2132 emit_sse_operand(dst, src);
2133}
2134
2135
Ben Murdochb0fe1622011-05-05 13:52:32 +01002136void Assembler::movd(const Operand& dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002137 ASSERT(CpuFeatures::IsEnabled(SSE2));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002138 EnsureSpace ensure_space(this);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002139 EMIT(0x66);
2140 EMIT(0x0F);
2141 EMIT(0x7E);
2142 emit_sse_operand(src, dst);
2143}
2144
2145
2146void Assembler::pand(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002147 ASSERT(CpuFeatures::IsEnabled(SSE2));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002148 EnsureSpace ensure_space(this);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002149 EMIT(0x66);
2150 EMIT(0x0F);
2151 EMIT(0xDB);
2152 emit_sse_operand(dst, src);
2153}
2154
2155
Steve Block6ded16b2010-05-10 14:33:55 +01002156void Assembler::pxor(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002157 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Block6ded16b2010-05-10 14:33:55 +01002158 EnsureSpace ensure_space(this);
Steve Block6ded16b2010-05-10 14:33:55 +01002159 EMIT(0x66);
2160 EMIT(0x0F);
2161 EMIT(0xEF);
2162 emit_sse_operand(dst, src);
2163}
2164
2165
Ben Murdochb8e0da22011-05-16 14:20:40 +01002166void Assembler::por(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002167 ASSERT(CpuFeatures::IsEnabled(SSE2));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002168 EnsureSpace ensure_space(this);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002169 EMIT(0x66);
2170 EMIT(0x0F);
2171 EMIT(0xEB);
2172 emit_sse_operand(dst, src);
2173}
2174
2175
Steve Block6ded16b2010-05-10 14:33:55 +01002176void Assembler::ptest(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002177 ASSERT(CpuFeatures::IsEnabled(SSE4_1));
Steve Block6ded16b2010-05-10 14:33:55 +01002178 EnsureSpace ensure_space(this);
Steve Block6ded16b2010-05-10 14:33:55 +01002179 EMIT(0x66);
2180 EMIT(0x0F);
2181 EMIT(0x38);
2182 EMIT(0x17);
2183 emit_sse_operand(dst, src);
2184}
2185
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002186
Ben Murdochb0fe1622011-05-05 13:52:32 +01002187void Assembler::psllq(XMMRegister reg, int8_t shift) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002188 ASSERT(CpuFeatures::IsEnabled(SSE2));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002189 EnsureSpace ensure_space(this);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002190 EMIT(0x66);
2191 EMIT(0x0F);
2192 EMIT(0x73);
2193 emit_sse_operand(esi, reg); // esi == 6
Ben Murdochb0fe1622011-05-05 13:52:32 +01002194 EMIT(shift);
2195}
2196
2197
Ben Murdochb8e0da22011-05-16 14:20:40 +01002198void Assembler::psllq(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002199 ASSERT(CpuFeatures::IsEnabled(SSE2));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002200 EnsureSpace ensure_space(this);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002201 EMIT(0x66);
2202 EMIT(0x0F);
2203 EMIT(0xF3);
2204 emit_sse_operand(dst, src);
2205}
2206
2207
2208void Assembler::psrlq(XMMRegister reg, int8_t shift) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002209 ASSERT(CpuFeatures::IsEnabled(SSE2));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002210 EnsureSpace ensure_space(this);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002211 EMIT(0x66);
2212 EMIT(0x0F);
2213 EMIT(0x73);
2214 emit_sse_operand(edx, reg); // edx == 2
2215 EMIT(shift);
2216}
2217
2218
2219void Assembler::psrlq(XMMRegister dst, XMMRegister src) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002220 ASSERT(CpuFeatures::IsEnabled(SSE2));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002221 EnsureSpace ensure_space(this);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002222 EMIT(0x66);
2223 EMIT(0x0F);
2224 EMIT(0xD3);
2225 emit_sse_operand(dst, src);
2226}
2227
2228
Ben Murdochb0fe1622011-05-05 13:52:32 +01002229void Assembler::pshufd(XMMRegister dst, XMMRegister src, int8_t shuffle) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002230 ASSERT(CpuFeatures::IsEnabled(SSE2));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002231 EnsureSpace ensure_space(this);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002232 EMIT(0x66);
2233 EMIT(0x0F);
2234 EMIT(0x70);
2235 emit_sse_operand(dst, src);
2236 EMIT(shuffle);
2237}
2238
2239
2240void Assembler::pextrd(const Operand& dst, XMMRegister src, int8_t offset) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002241 ASSERT(CpuFeatures::IsEnabled(SSE4_1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002242 EnsureSpace ensure_space(this);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002243 EMIT(0x66);
2244 EMIT(0x0F);
2245 EMIT(0x3A);
2246 EMIT(0x16);
2247 emit_sse_operand(src, dst);
2248 EMIT(offset);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002249}
2250
2251
Steve Block1e0659c2011-05-24 12:43:12 +01002252void Assembler::pinsrd(XMMRegister dst, const Operand& src, int8_t offset) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002253 ASSERT(CpuFeatures::IsEnabled(SSE4_1));
Steve Block1e0659c2011-05-24 12:43:12 +01002254 EnsureSpace ensure_space(this);
Steve Block1e0659c2011-05-24 12:43:12 +01002255 EMIT(0x66);
2256 EMIT(0x0F);
2257 EMIT(0x3A);
2258 EMIT(0x22);
2259 emit_sse_operand(dst, src);
2260 EMIT(offset);
2261}
2262
2263
Steve Blocka7e24c12009-10-30 11:49:00 +00002264void Assembler::emit_sse_operand(XMMRegister reg, const Operand& adr) {
2265 Register ireg = { reg.code() };
2266 emit_operand(ireg, adr);
2267}
2268
2269
2270void Assembler::emit_sse_operand(XMMRegister dst, XMMRegister src) {
2271 EMIT(0xC0 | dst.code() << 3 | src.code());
2272}
2273
2274
Steve Block6ded16b2010-05-10 14:33:55 +01002275void Assembler::emit_sse_operand(Register dst, XMMRegister src) {
2276 EMIT(0xC0 | dst.code() << 3 | src.code());
2277}
2278
2279
Steve Blocka7e24c12009-10-30 11:49:00 +00002280void Assembler::Print() {
2281 Disassembler::Decode(stdout, buffer_, pc_);
2282}
2283
2284
2285void Assembler::RecordJSReturn() {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002286 positions_recorder()->WriteRecordedPositions();
Steve Blocka7e24c12009-10-30 11:49:00 +00002287 EnsureSpace ensure_space(this);
2288 RecordRelocInfo(RelocInfo::JS_RETURN);
2289}
2290
2291
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002292void Assembler::RecordDebugBreakSlot() {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002293 positions_recorder()->WriteRecordedPositions();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002294 EnsureSpace ensure_space(this);
2295 RecordRelocInfo(RelocInfo::DEBUG_BREAK_SLOT);
2296}
2297
2298
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002299void Assembler::RecordComment(const char* msg, bool force) {
2300 if (FLAG_code_comments || force) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002301 EnsureSpace ensure_space(this);
2302 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
2303 }
2304}
2305
2306
Steve Blocka7e24c12009-10-30 11:49:00 +00002307void Assembler::GrowBuffer() {
Andrei Popescu31002712010-02-23 13:46:05 +00002308 ASSERT(overflow());
Steve Blocka7e24c12009-10-30 11:49:00 +00002309 if (!own_buffer_) FATAL("external code buffer is too small");
2310
Andrei Popescu31002712010-02-23 13:46:05 +00002311 // Compute new buffer size.
Steve Blocka7e24c12009-10-30 11:49:00 +00002312 CodeDesc desc; // the new buffer
2313 if (buffer_size_ < 4*KB) {
2314 desc.buffer_size = 4*KB;
2315 } else {
2316 desc.buffer_size = 2*buffer_size_;
2317 }
2318 // Some internal data structures overflow for very large buffers,
2319 // they must ensure that kMaximalBufferSize is not too large.
2320 if ((desc.buffer_size > kMaximalBufferSize) ||
Steve Block44f0eee2011-05-26 01:26:41 +01002321 (desc.buffer_size > isolate()->heap()->MaxOldGenerationSize())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002322 V8::FatalProcessOutOfMemory("Assembler::GrowBuffer");
2323 }
2324
Andrei Popescu31002712010-02-23 13:46:05 +00002325 // Setup new buffer.
Steve Blocka7e24c12009-10-30 11:49:00 +00002326 desc.buffer = NewArray<byte>(desc.buffer_size);
2327 desc.instr_size = pc_offset();
2328 desc.reloc_size = (buffer_ + buffer_size_) - (reloc_info_writer.pos());
2329
2330 // Clear the buffer in debug mode. Use 'int3' instructions to make
2331 // sure to get into problems if we ever run uninitialized code.
2332#ifdef DEBUG
2333 memset(desc.buffer, 0xCC, desc.buffer_size);
2334#endif
2335
Andrei Popescu31002712010-02-23 13:46:05 +00002336 // Copy the data.
Steve Blocka7e24c12009-10-30 11:49:00 +00002337 int pc_delta = desc.buffer - buffer_;
2338 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
2339 memmove(desc.buffer, buffer_, desc.instr_size);
2340 memmove(rc_delta + reloc_info_writer.pos(),
2341 reloc_info_writer.pos(), desc.reloc_size);
2342
Andrei Popescu31002712010-02-23 13:46:05 +00002343 // Switch buffers.
Steve Block44f0eee2011-05-26 01:26:41 +01002344 if (isolate()->assembler_spare_buffer() == NULL &&
2345 buffer_size_ == kMinimalBufferSize) {
2346 isolate()->set_assembler_spare_buffer(buffer_);
Steve Blocka7e24c12009-10-30 11:49:00 +00002347 } else {
2348 DeleteArray(buffer_);
2349 }
2350 buffer_ = desc.buffer;
2351 buffer_size_ = desc.buffer_size;
2352 pc_ += pc_delta;
Steve Blocka7e24c12009-10-30 11:49:00 +00002353 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
2354 reloc_info_writer.last_pc() + pc_delta);
2355
Andrei Popescu31002712010-02-23 13:46:05 +00002356 // Relocate runtime entries.
Steve Blocka7e24c12009-10-30 11:49:00 +00002357 for (RelocIterator it(desc); !it.done(); it.next()) {
2358 RelocInfo::Mode rmode = it.rinfo()->rmode();
2359 if (rmode == RelocInfo::RUNTIME_ENTRY) {
2360 int32_t* p = reinterpret_cast<int32_t*>(it.rinfo()->pc());
2361 *p -= pc_delta; // relocate entry
2362 } else if (rmode == RelocInfo::INTERNAL_REFERENCE) {
2363 int32_t* p = reinterpret_cast<int32_t*>(it.rinfo()->pc());
2364 if (*p != 0) { // 0 means uninitialized.
2365 *p += pc_delta;
2366 }
2367 }
2368 }
2369
2370 ASSERT(!overflow());
2371}
2372
2373
2374void Assembler::emit_arith_b(int op1, int op2, Register dst, int imm8) {
2375 ASSERT(is_uint8(op1) && is_uint8(op2)); // wrong opcode
2376 ASSERT(is_uint8(imm8));
2377 ASSERT((op1 & 0x01) == 0); // should be 8bit operation
2378 EMIT(op1);
2379 EMIT(op2 | dst.code());
2380 EMIT(imm8);
2381}
2382
2383
2384void Assembler::emit_arith(int sel, Operand dst, const Immediate& x) {
2385 ASSERT((0 <= sel) && (sel <= 7));
2386 Register ireg = { sel };
2387 if (x.is_int8()) {
2388 EMIT(0x83); // using a sign-extended 8-bit immediate.
2389 emit_operand(ireg, dst);
2390 EMIT(x.x_ & 0xFF);
2391 } else if (dst.is_reg(eax)) {
2392 EMIT((sel << 3) | 0x05); // short form if the destination is eax.
2393 emit(x);
2394 } else {
2395 EMIT(0x81); // using a literal 32-bit immediate.
2396 emit_operand(ireg, dst);
2397 emit(x);
2398 }
2399}
2400
2401
2402void Assembler::emit_operand(Register reg, const Operand& adr) {
2403 const unsigned length = adr.len_;
2404 ASSERT(length > 0);
2405
2406 // Emit updated ModRM byte containing the given register.
2407 pc_[0] = (adr.buf_[0] & ~0x38) | (reg.code() << 3);
2408
2409 // Emit the rest of the encoded operand.
2410 for (unsigned i = 1; i < length; i++) pc_[i] = adr.buf_[i];
2411 pc_ += length;
2412
2413 // Emit relocation information if necessary.
2414 if (length >= sizeof(int32_t) && adr.rmode_ != RelocInfo::NONE) {
2415 pc_ -= sizeof(int32_t); // pc_ must be *at* disp32
2416 RecordRelocInfo(adr.rmode_);
2417 pc_ += sizeof(int32_t);
2418 }
2419}
2420
2421
2422void Assembler::emit_farith(int b1, int b2, int i) {
2423 ASSERT(is_uint8(b1) && is_uint8(b2)); // wrong opcode
2424 ASSERT(0 <= i && i < 8); // illegal stack offset
2425 EMIT(b1);
2426 EMIT(b2 + i);
2427}
2428
2429
Ben Murdochb0fe1622011-05-05 13:52:32 +01002430void Assembler::db(uint8_t data) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002431 EnsureSpace ensure_space(this);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002432 EMIT(data);
2433}
2434
2435
2436void Assembler::dd(uint32_t data) {
2437 EnsureSpace ensure_space(this);
2438 emit(data);
Steve Blocka7e24c12009-10-30 11:49:00 +00002439}
2440
2441
2442void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
2443 ASSERT(rmode != RelocInfo::NONE);
2444 // Don't record external references unless the heap will be serialized.
Steve Blockd0582a62009-12-15 09:54:21 +00002445 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
2446#ifdef DEBUG
2447 if (!Serializer::enabled()) {
2448 Serializer::TooLateToEnableNow();
2449 }
2450#endif
Steve Block44f0eee2011-05-26 01:26:41 +01002451 if (!Serializer::enabled() && !emit_debug_code()) {
Steve Blockd0582a62009-12-15 09:54:21 +00002452 return;
2453 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002454 }
2455 RelocInfo rinfo(pc_, rmode, data);
2456 reloc_info_writer.Write(&rinfo);
2457}
2458
2459
2460#ifdef GENERATED_CODE_COVERAGE
2461static FILE* coverage_log = NULL;
2462
2463
2464static void InitCoverageLog() {
2465 char* file_name = getenv("V8_GENERATED_CODE_COVERAGE_LOG");
2466 if (file_name != NULL) {
2467 coverage_log = fopen(file_name, "aw+");
2468 }
2469}
2470
2471
2472void LogGeneratedCodeCoverage(const char* file_line) {
2473 const char* return_address = (&file_line)[-1];
2474 char* push_insn = const_cast<char*>(return_address - 12);
2475 push_insn[0] = 0xeb; // Relative branch insn.
2476 push_insn[1] = 13; // Skip over coverage insns.
2477 if (coverage_log != NULL) {
2478 fprintf(coverage_log, "%s\n", file_line);
2479 fflush(coverage_log);
2480 }
2481}
2482
2483#endif
2484
2485} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002486
2487#endif // V8_TARGET_ARCH_IA32