blob: 66fda5787f99b12a5a15a2f7c9c431659bcc4b3d [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +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.
35// Copyright 2012 the V8 project authors. All rights reserved.
36
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037#include "src/x87/assembler-x87.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038
39#if V8_TARGET_ARCH_X87
40
41#include "src/base/bits.h"
42#include "src/base/cpu.h"
43#include "src/disassembler.h"
44#include "src/macro-assembler.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045#include "src/v8.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046
47namespace v8 {
48namespace internal {
49
50// -----------------------------------------------------------------------------
51// Implementation of CpuFeatures
52
53void CpuFeatures::ProbeImpl(bool cross_compile) {
54 base::CPU cpu;
55
56 // Only use statically determined features for cross compile (snapshot).
57 if (cross_compile) return;
58}
59
60
61void CpuFeatures::PrintTarget() { }
62void CpuFeatures::PrintFeatures() { }
63
64
65// -----------------------------------------------------------------------------
66// Implementation of Displacement
67
68void Displacement::init(Label* L, Type type) {
69 DCHECK(!L->is_bound());
70 int next = 0;
71 if (L->is_linked()) {
72 next = L->pos();
73 DCHECK(next > 0); // Displacements must be at positions > 0
74 }
75 // Ensure that we _never_ overflow the next field.
76 DCHECK(NextField::is_valid(Assembler::kMaximalBufferSize));
77 data_ = NextField::encode(next) | TypeField::encode(type);
78}
79
80
81// -----------------------------------------------------------------------------
82// Implementation of RelocInfo
83
84
85const int RelocInfo::kApplyMask =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000086 RelocInfo::kCodeTargetMask | 1 << RelocInfo::RUNTIME_ENTRY |
87 1 << RelocInfo::INTERNAL_REFERENCE | 1 << RelocInfo::CODE_AGE_SEQUENCE |
88 RelocInfo::kDebugBreakSlotMask;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089
90
91bool RelocInfo::IsCodedSpecially() {
92 // The deserializer needs to know whether a pointer is specially coded. Being
93 // specially coded on IA32 means that it is a relative address, as used by
94 // branch instructions. These are also the ones that need changing when a
95 // code object moves.
96 return (1 << rmode_) & kApplyMask;
97}
98
99
100bool RelocInfo::IsInConstantPool() {
101 return false;
102}
103
104
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105// -----------------------------------------------------------------------------
106// Implementation of Operand
107
108Operand::Operand(Register base, int32_t disp, RelocInfo::Mode rmode) {
109 // [base + disp/r]
110 if (disp == 0 && RelocInfo::IsNone(rmode) && !base.is(ebp)) {
111 // [base]
112 set_modrm(0, base);
113 if (base.is(esp)) set_sib(times_1, esp, base);
114 } else if (is_int8(disp) && RelocInfo::IsNone(rmode)) {
115 // [base + disp8]
116 set_modrm(1, base);
117 if (base.is(esp)) set_sib(times_1, esp, base);
118 set_disp8(disp);
119 } else {
120 // [base + disp/r]
121 set_modrm(2, base);
122 if (base.is(esp)) set_sib(times_1, esp, base);
123 set_dispr(disp, rmode);
124 }
125}
126
127
128Operand::Operand(Register base,
129 Register index,
130 ScaleFactor scale,
131 int32_t disp,
132 RelocInfo::Mode rmode) {
133 DCHECK(!index.is(esp)); // illegal addressing mode
134 // [base + index*scale + disp/r]
135 if (disp == 0 && RelocInfo::IsNone(rmode) && !base.is(ebp)) {
136 // [base + index*scale]
137 set_modrm(0, esp);
138 set_sib(scale, index, base);
139 } else if (is_int8(disp) && RelocInfo::IsNone(rmode)) {
140 // [base + index*scale + disp8]
141 set_modrm(1, esp);
142 set_sib(scale, index, base);
143 set_disp8(disp);
144 } else {
145 // [base + index*scale + disp/r]
146 set_modrm(2, esp);
147 set_sib(scale, index, base);
148 set_dispr(disp, rmode);
149 }
150}
151
152
153Operand::Operand(Register index,
154 ScaleFactor scale,
155 int32_t disp,
156 RelocInfo::Mode rmode) {
157 DCHECK(!index.is(esp)); // illegal addressing mode
158 // [index*scale + disp/r]
159 set_modrm(0, esp);
160 set_sib(scale, index, ebp);
161 set_dispr(disp, rmode);
162}
163
164
165bool Operand::is_reg(Register reg) const {
166 return ((buf_[0] & 0xF8) == 0xC0) // addressing mode is register only.
167 && ((buf_[0] & 0x07) == reg.code()); // register codes match.
168}
169
170
171bool Operand::is_reg_only() const {
172 return (buf_[0] & 0xF8) == 0xC0; // Addressing mode is register only.
173}
174
175
176Register Operand::reg() const {
177 DCHECK(is_reg_only());
178 return Register::from_code(buf_[0] & 0x07);
179}
180
181
182// -----------------------------------------------------------------------------
183// Implementation of Assembler.
184
185// Emit a single byte. Must always be inlined.
186#define EMIT(x) \
187 *pc_++ = (x)
188
189
190#ifdef GENERATED_CODE_COVERAGE
191static void InitCoverageLog();
192#endif
193
194Assembler::Assembler(Isolate* isolate, void* buffer, int buffer_size)
195 : AssemblerBase(isolate, buffer, buffer_size),
196 positions_recorder_(this) {
197 // Clear the buffer in debug mode unless it was provided by the
198 // caller in which case we can't be sure it's okay to overwrite
199 // existing code in it; see CodePatcher::CodePatcher(...).
200#ifdef DEBUG
201 if (own_buffer_) {
202 memset(buffer_, 0xCC, buffer_size_); // int3
203 }
204#endif
205
206 reloc_info_writer.Reposition(buffer_ + buffer_size_, pc_);
207
208#ifdef GENERATED_CODE_COVERAGE
209 InitCoverageLog();
210#endif
211}
212
213
214void Assembler::GetCode(CodeDesc* desc) {
215 // Finalize code (at this point overflow() may be true, but the gap ensures
216 // that we are still not overlapping instructions and relocation info).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000217 reloc_info_writer.Finish();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218 DCHECK(pc_ <= reloc_info_writer.pos()); // No overlap.
219 // Set up code descriptor.
220 desc->buffer = buffer_;
221 desc->buffer_size = buffer_size_;
222 desc->instr_size = pc_offset();
223 desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
224 desc->origin = this;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000225 desc->constant_pool_size = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000226}
227
228
229void Assembler::Align(int m) {
230 DCHECK(base::bits::IsPowerOfTwo32(m));
231 int mask = m - 1;
232 int addr = pc_offset();
233 Nop((m - (addr & mask)) & mask);
234}
235
236
237bool Assembler::IsNop(Address addr) {
238 Address a = addr;
239 while (*a == 0x66) a++;
240 if (*a == 0x90) return true;
241 if (a[0] == 0xf && a[1] == 0x1f) return true;
242 return false;
243}
244
245
246void Assembler::Nop(int bytes) {
247 EnsureSpace ensure_space(this);
248
249 // Older CPUs that do not support SSE2 may not support multibyte NOP
250 // instructions.
251 for (; bytes > 0; bytes--) {
252 EMIT(0x90);
253 }
254 return;
255}
256
257
258void Assembler::CodeTargetAlign() {
259 Align(16); // Preferred alignment of jump targets on ia32.
260}
261
262
263void Assembler::cpuid() {
264 EnsureSpace ensure_space(this);
265 EMIT(0x0F);
266 EMIT(0xA2);
267}
268
269
270void Assembler::pushad() {
271 EnsureSpace ensure_space(this);
272 EMIT(0x60);
273}
274
275
276void Assembler::popad() {
277 EnsureSpace ensure_space(this);
278 EMIT(0x61);
279}
280
281
282void Assembler::pushfd() {
283 EnsureSpace ensure_space(this);
284 EMIT(0x9C);
285}
286
287
288void Assembler::popfd() {
289 EnsureSpace ensure_space(this);
290 EMIT(0x9D);
291}
292
293
294void Assembler::push(const Immediate& x) {
295 EnsureSpace ensure_space(this);
296 if (x.is_int8()) {
297 EMIT(0x6a);
298 EMIT(x.x_);
299 } else {
300 EMIT(0x68);
301 emit(x);
302 }
303}
304
305
306void Assembler::push_imm32(int32_t imm32) {
307 EnsureSpace ensure_space(this);
308 EMIT(0x68);
309 emit(imm32);
310}
311
312
313void Assembler::push(Register src) {
314 EnsureSpace ensure_space(this);
315 EMIT(0x50 | src.code());
316}
317
318
319void Assembler::push(const Operand& src) {
320 EnsureSpace ensure_space(this);
321 EMIT(0xFF);
322 emit_operand(esi, src);
323}
324
325
326void Assembler::pop(Register dst) {
327 DCHECK(reloc_info_writer.last_pc() != NULL);
328 EnsureSpace ensure_space(this);
329 EMIT(0x58 | dst.code());
330}
331
332
333void Assembler::pop(const Operand& dst) {
334 EnsureSpace ensure_space(this);
335 EMIT(0x8F);
336 emit_operand(eax, dst);
337}
338
339
340void Assembler::enter(const Immediate& size) {
341 EnsureSpace ensure_space(this);
342 EMIT(0xC8);
343 emit_w(size);
344 EMIT(0);
345}
346
347
348void Assembler::leave() {
349 EnsureSpace ensure_space(this);
350 EMIT(0xC9);
351}
352
353
354void Assembler::mov_b(Register dst, const Operand& src) {
355 CHECK(dst.is_byte_register());
356 EnsureSpace ensure_space(this);
357 EMIT(0x8A);
358 emit_operand(dst, src);
359}
360
361
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000362void Assembler::mov_b(const Operand& dst, const Immediate& src) {
363 EnsureSpace ensure_space(this);
364 EMIT(0xC6);
365 emit_operand(eax, dst);
366 EMIT(static_cast<int8_t>(src.x_));
367}
368
369
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000370void Assembler::mov_b(const Operand& dst, int8_t imm8) {
371 EnsureSpace ensure_space(this);
372 EMIT(0xC6);
373 emit_operand(eax, dst);
374 EMIT(imm8);
375}
376
377
378void Assembler::mov_b(const Operand& dst, Register src) {
379 CHECK(src.is_byte_register());
380 EnsureSpace ensure_space(this);
381 EMIT(0x88);
382 emit_operand(src, dst);
383}
384
385
386void Assembler::mov_w(Register dst, const Operand& src) {
387 EnsureSpace ensure_space(this);
388 EMIT(0x66);
389 EMIT(0x8B);
390 emit_operand(dst, src);
391}
392
393
394void Assembler::mov_w(const Operand& dst, Register src) {
395 EnsureSpace ensure_space(this);
396 EMIT(0x66);
397 EMIT(0x89);
398 emit_operand(src, dst);
399}
400
401
402void Assembler::mov_w(const Operand& dst, int16_t imm16) {
403 EnsureSpace ensure_space(this);
404 EMIT(0x66);
405 EMIT(0xC7);
406 emit_operand(eax, dst);
407 EMIT(static_cast<int8_t>(imm16 & 0xff));
408 EMIT(static_cast<int8_t>(imm16 >> 8));
409}
410
411
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000412void Assembler::mov_w(const Operand& dst, const Immediate& src) {
413 EnsureSpace ensure_space(this);
414 EMIT(0x66);
415 EMIT(0xC7);
416 emit_operand(eax, dst);
417 EMIT(static_cast<int8_t>(src.x_ & 0xff));
418 EMIT(static_cast<int8_t>(src.x_ >> 8));
419}
420
421
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000422void Assembler::mov(Register dst, int32_t imm32) {
423 EnsureSpace ensure_space(this);
424 EMIT(0xB8 | dst.code());
425 emit(imm32);
426}
427
428
429void Assembler::mov(Register dst, const Immediate& x) {
430 EnsureSpace ensure_space(this);
431 EMIT(0xB8 | dst.code());
432 emit(x);
433}
434
435
436void Assembler::mov(Register dst, Handle<Object> handle) {
437 EnsureSpace ensure_space(this);
438 EMIT(0xB8 | dst.code());
439 emit(handle);
440}
441
442
443void Assembler::mov(Register dst, const Operand& src) {
444 EnsureSpace ensure_space(this);
445 EMIT(0x8B);
446 emit_operand(dst, src);
447}
448
449
450void Assembler::mov(Register dst, Register src) {
451 EnsureSpace ensure_space(this);
452 EMIT(0x89);
453 EMIT(0xC0 | src.code() << 3 | dst.code());
454}
455
456
457void Assembler::mov(const Operand& dst, const Immediate& x) {
458 EnsureSpace ensure_space(this);
459 EMIT(0xC7);
460 emit_operand(eax, dst);
461 emit(x);
462}
463
464
465void Assembler::mov(const Operand& dst, Handle<Object> handle) {
466 EnsureSpace ensure_space(this);
467 EMIT(0xC7);
468 emit_operand(eax, dst);
469 emit(handle);
470}
471
472
473void Assembler::mov(const Operand& dst, Register src) {
474 EnsureSpace ensure_space(this);
475 EMIT(0x89);
476 emit_operand(src, dst);
477}
478
479
480void Assembler::movsx_b(Register dst, const Operand& src) {
481 EnsureSpace ensure_space(this);
482 EMIT(0x0F);
483 EMIT(0xBE);
484 emit_operand(dst, src);
485}
486
487
488void Assembler::movsx_w(Register dst, const Operand& src) {
489 EnsureSpace ensure_space(this);
490 EMIT(0x0F);
491 EMIT(0xBF);
492 emit_operand(dst, src);
493}
494
495
496void Assembler::movzx_b(Register dst, const Operand& src) {
497 EnsureSpace ensure_space(this);
498 EMIT(0x0F);
499 EMIT(0xB6);
500 emit_operand(dst, src);
501}
502
503
504void Assembler::movzx_w(Register dst, const Operand& src) {
505 EnsureSpace ensure_space(this);
506 EMIT(0x0F);
507 EMIT(0xB7);
508 emit_operand(dst, src);
509}
510
511
512void Assembler::cld() {
513 EnsureSpace ensure_space(this);
514 EMIT(0xFC);
515}
516
517
518void Assembler::rep_movs() {
519 EnsureSpace ensure_space(this);
520 EMIT(0xF3);
521 EMIT(0xA5);
522}
523
524
525void Assembler::rep_stos() {
526 EnsureSpace ensure_space(this);
527 EMIT(0xF3);
528 EMIT(0xAB);
529}
530
531
532void Assembler::stos() {
533 EnsureSpace ensure_space(this);
534 EMIT(0xAB);
535}
536
537
538void Assembler::xchg(Register dst, Register src) {
539 EnsureSpace ensure_space(this);
540 if (src.is(eax) || dst.is(eax)) { // Single-byte encoding.
541 EMIT(0x90 | (src.is(eax) ? dst.code() : src.code()));
542 } else {
543 EMIT(0x87);
544 EMIT(0xC0 | src.code() << 3 | dst.code());
545 }
546}
547
548
549void Assembler::xchg(Register dst, const Operand& src) {
550 EnsureSpace ensure_space(this);
551 EMIT(0x87);
552 emit_operand(dst, src);
553}
554
555
556void Assembler::adc(Register dst, int32_t imm32) {
557 EnsureSpace ensure_space(this);
558 emit_arith(2, Operand(dst), Immediate(imm32));
559}
560
561
562void Assembler::adc(Register dst, const Operand& src) {
563 EnsureSpace ensure_space(this);
564 EMIT(0x13);
565 emit_operand(dst, src);
566}
567
568
569void Assembler::add(Register dst, const Operand& src) {
570 EnsureSpace ensure_space(this);
571 EMIT(0x03);
572 emit_operand(dst, src);
573}
574
575
576void Assembler::add(const Operand& dst, Register src) {
577 EnsureSpace ensure_space(this);
578 EMIT(0x01);
579 emit_operand(src, dst);
580}
581
582
583void Assembler::add(const Operand& dst, const Immediate& x) {
584 DCHECK(reloc_info_writer.last_pc() != NULL);
585 EnsureSpace ensure_space(this);
586 emit_arith(0, dst, x);
587}
588
589
590void Assembler::and_(Register dst, int32_t imm32) {
591 and_(dst, Immediate(imm32));
592}
593
594
595void Assembler::and_(Register dst, const Immediate& x) {
596 EnsureSpace ensure_space(this);
597 emit_arith(4, Operand(dst), x);
598}
599
600
601void Assembler::and_(Register dst, const Operand& src) {
602 EnsureSpace ensure_space(this);
603 EMIT(0x23);
604 emit_operand(dst, src);
605}
606
607
608void Assembler::and_(const Operand& dst, const Immediate& x) {
609 EnsureSpace ensure_space(this);
610 emit_arith(4, dst, x);
611}
612
613
614void Assembler::and_(const Operand& dst, Register src) {
615 EnsureSpace ensure_space(this);
616 EMIT(0x21);
617 emit_operand(src, dst);
618}
619
620
621void Assembler::cmpb(const Operand& op, int8_t imm8) {
622 EnsureSpace ensure_space(this);
623 if (op.is_reg(eax)) {
624 EMIT(0x3C);
625 } else {
626 EMIT(0x80);
627 emit_operand(edi, op); // edi == 7
628 }
629 EMIT(imm8);
630}
631
632
633void Assembler::cmpb(const Operand& op, Register reg) {
634 CHECK(reg.is_byte_register());
635 EnsureSpace ensure_space(this);
636 EMIT(0x38);
637 emit_operand(reg, op);
638}
639
640
641void Assembler::cmpb(Register reg, const Operand& op) {
642 CHECK(reg.is_byte_register());
643 EnsureSpace ensure_space(this);
644 EMIT(0x3A);
645 emit_operand(reg, op);
646}
647
648
649void Assembler::cmpw(const Operand& op, Immediate imm16) {
650 DCHECK(imm16.is_int16());
651 EnsureSpace ensure_space(this);
652 EMIT(0x66);
653 EMIT(0x81);
654 emit_operand(edi, op);
655 emit_w(imm16);
656}
657
658
659void Assembler::cmp(Register reg, int32_t imm32) {
660 EnsureSpace ensure_space(this);
661 emit_arith(7, Operand(reg), Immediate(imm32));
662}
663
664
665void Assembler::cmp(Register reg, Handle<Object> handle) {
666 EnsureSpace ensure_space(this);
667 emit_arith(7, Operand(reg), Immediate(handle));
668}
669
670
671void Assembler::cmp(Register reg, const Operand& op) {
672 EnsureSpace ensure_space(this);
673 EMIT(0x3B);
674 emit_operand(reg, op);
675}
676
Ben Murdoch097c5b22016-05-18 11:27:45 +0100677void Assembler::cmp(const Operand& op, Register reg) {
678 EnsureSpace ensure_space(this);
679 EMIT(0x39);
680 emit_operand(reg, op);
681}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000682
683void Assembler::cmp(const Operand& op, const Immediate& imm) {
684 EnsureSpace ensure_space(this);
685 emit_arith(7, op, imm);
686}
687
688
689void Assembler::cmp(const Operand& op, Handle<Object> handle) {
690 EnsureSpace ensure_space(this);
691 emit_arith(7, op, Immediate(handle));
692}
693
694
695void Assembler::cmpb_al(const Operand& op) {
696 EnsureSpace ensure_space(this);
697 EMIT(0x38); // CMP r/m8, r8
698 emit_operand(eax, op); // eax has same code as register al.
699}
700
701
702void Assembler::cmpw_ax(const Operand& op) {
703 EnsureSpace ensure_space(this);
704 EMIT(0x66);
705 EMIT(0x39); // CMP r/m16, r16
706 emit_operand(eax, op); // eax has same code as register ax.
707}
708
709
710void Assembler::dec_b(Register dst) {
711 CHECK(dst.is_byte_register());
712 EnsureSpace ensure_space(this);
713 EMIT(0xFE);
714 EMIT(0xC8 | dst.code());
715}
716
717
718void Assembler::dec_b(const Operand& dst) {
719 EnsureSpace ensure_space(this);
720 EMIT(0xFE);
721 emit_operand(ecx, dst);
722}
723
724
725void Assembler::dec(Register dst) {
726 EnsureSpace ensure_space(this);
727 EMIT(0x48 | dst.code());
728}
729
730
731void Assembler::dec(const Operand& dst) {
732 EnsureSpace ensure_space(this);
733 EMIT(0xFF);
734 emit_operand(ecx, dst);
735}
736
737
738void Assembler::cdq() {
739 EnsureSpace ensure_space(this);
740 EMIT(0x99);
741}
742
743
744void Assembler::idiv(const Operand& src) {
745 EnsureSpace ensure_space(this);
746 EMIT(0xF7);
747 emit_operand(edi, src);
748}
749
750
751void Assembler::div(const Operand& src) {
752 EnsureSpace ensure_space(this);
753 EMIT(0xF7);
754 emit_operand(esi, src);
755}
756
757
758void Assembler::imul(Register reg) {
759 EnsureSpace ensure_space(this);
760 EMIT(0xF7);
761 EMIT(0xE8 | reg.code());
762}
763
764
765void Assembler::imul(Register dst, const Operand& src) {
766 EnsureSpace ensure_space(this);
767 EMIT(0x0F);
768 EMIT(0xAF);
769 emit_operand(dst, src);
770}
771
772
773void Assembler::imul(Register dst, Register src, int32_t imm32) {
774 imul(dst, Operand(src), imm32);
775}
776
777
778void Assembler::imul(Register dst, const Operand& src, int32_t imm32) {
779 EnsureSpace ensure_space(this);
780 if (is_int8(imm32)) {
781 EMIT(0x6B);
782 emit_operand(dst, src);
783 EMIT(imm32);
784 } else {
785 EMIT(0x69);
786 emit_operand(dst, src);
787 emit(imm32);
788 }
789}
790
791
792void Assembler::inc(Register dst) {
793 EnsureSpace ensure_space(this);
794 EMIT(0x40 | dst.code());
795}
796
797
798void Assembler::inc(const Operand& dst) {
799 EnsureSpace ensure_space(this);
800 EMIT(0xFF);
801 emit_operand(eax, dst);
802}
803
804
805void Assembler::lea(Register dst, const Operand& src) {
806 EnsureSpace ensure_space(this);
807 EMIT(0x8D);
808 emit_operand(dst, src);
809}
810
811
812void Assembler::mul(Register src) {
813 EnsureSpace ensure_space(this);
814 EMIT(0xF7);
815 EMIT(0xE0 | src.code());
816}
817
818
819void Assembler::neg(Register dst) {
820 EnsureSpace ensure_space(this);
821 EMIT(0xF7);
822 EMIT(0xD8 | dst.code());
823}
824
825
826void Assembler::neg(const Operand& dst) {
827 EnsureSpace ensure_space(this);
828 EMIT(0xF7);
829 emit_operand(ebx, dst);
830}
831
832
833void Assembler::not_(Register dst) {
834 EnsureSpace ensure_space(this);
835 EMIT(0xF7);
836 EMIT(0xD0 | dst.code());
837}
838
839
840void Assembler::not_(const Operand& dst) {
841 EnsureSpace ensure_space(this);
842 EMIT(0xF7);
843 emit_operand(edx, dst);
844}
845
846
847void Assembler::or_(Register dst, int32_t imm32) {
848 EnsureSpace ensure_space(this);
849 emit_arith(1, Operand(dst), Immediate(imm32));
850}
851
852
853void Assembler::or_(Register dst, const Operand& src) {
854 EnsureSpace ensure_space(this);
855 EMIT(0x0B);
856 emit_operand(dst, src);
857}
858
859
860void Assembler::or_(const Operand& dst, const Immediate& x) {
861 EnsureSpace ensure_space(this);
862 emit_arith(1, dst, x);
863}
864
865
866void Assembler::or_(const Operand& dst, Register src) {
867 EnsureSpace ensure_space(this);
868 EMIT(0x09);
869 emit_operand(src, dst);
870}
871
872
873void Assembler::rcl(Register dst, uint8_t imm8) {
874 EnsureSpace ensure_space(this);
875 DCHECK(is_uint5(imm8)); // illegal shift count
876 if (imm8 == 1) {
877 EMIT(0xD1);
878 EMIT(0xD0 | dst.code());
879 } else {
880 EMIT(0xC1);
881 EMIT(0xD0 | dst.code());
882 EMIT(imm8);
883 }
884}
885
886
887void Assembler::rcr(Register dst, uint8_t imm8) {
888 EnsureSpace ensure_space(this);
889 DCHECK(is_uint5(imm8)); // illegal shift count
890 if (imm8 == 1) {
891 EMIT(0xD1);
892 EMIT(0xD8 | dst.code());
893 } else {
894 EMIT(0xC1);
895 EMIT(0xD8 | dst.code());
896 EMIT(imm8);
897 }
898}
899
900
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400901void Assembler::ror(const Operand& dst, uint8_t imm8) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000902 EnsureSpace ensure_space(this);
903 DCHECK(is_uint5(imm8)); // illegal shift count
904 if (imm8 == 1) {
905 EMIT(0xD1);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400906 emit_operand(ecx, dst);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000907 } else {
908 EMIT(0xC1);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400909 emit_operand(ecx, dst);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000910 EMIT(imm8);
911 }
912}
913
914
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400915void Assembler::ror_cl(const Operand& dst) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000916 EnsureSpace ensure_space(this);
917 EMIT(0xD3);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400918 emit_operand(ecx, dst);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000919}
920
921
922void Assembler::sar(const Operand& dst, uint8_t imm8) {
923 EnsureSpace ensure_space(this);
924 DCHECK(is_uint5(imm8)); // illegal shift count
925 if (imm8 == 1) {
926 EMIT(0xD1);
927 emit_operand(edi, dst);
928 } else {
929 EMIT(0xC1);
930 emit_operand(edi, dst);
931 EMIT(imm8);
932 }
933}
934
935
936void Assembler::sar_cl(const Operand& dst) {
937 EnsureSpace ensure_space(this);
938 EMIT(0xD3);
939 emit_operand(edi, dst);
940}
941
942
943void Assembler::sbb(Register dst, const Operand& src) {
944 EnsureSpace ensure_space(this);
945 EMIT(0x1B);
946 emit_operand(dst, src);
947}
948
949
950void Assembler::shld(Register dst, const Operand& src) {
951 EnsureSpace ensure_space(this);
952 EMIT(0x0F);
953 EMIT(0xA5);
954 emit_operand(dst, src);
955}
956
957
958void Assembler::shl(const Operand& dst, uint8_t imm8) {
959 EnsureSpace ensure_space(this);
960 DCHECK(is_uint5(imm8)); // illegal shift count
961 if (imm8 == 1) {
962 EMIT(0xD1);
963 emit_operand(esp, dst);
964 } else {
965 EMIT(0xC1);
966 emit_operand(esp, dst);
967 EMIT(imm8);
968 }
969}
970
971
972void Assembler::shl_cl(const Operand& dst) {
973 EnsureSpace ensure_space(this);
974 EMIT(0xD3);
975 emit_operand(esp, dst);
976}
977
978
979void Assembler::shrd(Register dst, const Operand& src) {
980 EnsureSpace ensure_space(this);
981 EMIT(0x0F);
982 EMIT(0xAD);
983 emit_operand(dst, src);
984}
985
986
987void Assembler::shr(const Operand& dst, uint8_t imm8) {
988 EnsureSpace ensure_space(this);
989 DCHECK(is_uint5(imm8)); // illegal shift count
990 if (imm8 == 1) {
991 EMIT(0xD1);
992 emit_operand(ebp, dst);
993 } else {
994 EMIT(0xC1);
995 emit_operand(ebp, dst);
996 EMIT(imm8);
997 }
998}
999
1000
1001void Assembler::shr_cl(const Operand& dst) {
1002 EnsureSpace ensure_space(this);
1003 EMIT(0xD3);
1004 emit_operand(ebp, dst);
1005}
1006
1007
1008void Assembler::sub(const Operand& dst, const Immediate& x) {
1009 EnsureSpace ensure_space(this);
1010 emit_arith(5, dst, x);
1011}
1012
1013
1014void Assembler::sub(Register dst, const Operand& src) {
1015 EnsureSpace ensure_space(this);
1016 EMIT(0x2B);
1017 emit_operand(dst, src);
1018}
1019
1020
1021void Assembler::sub(const Operand& dst, Register src) {
1022 EnsureSpace ensure_space(this);
1023 EMIT(0x29);
1024 emit_operand(src, dst);
1025}
1026
1027
1028void Assembler::test(Register reg, const Immediate& imm) {
1029 if (RelocInfo::IsNone(imm.rmode_) && is_uint8(imm.x_)) {
1030 test_b(reg, imm.x_);
1031 return;
1032 }
1033
1034 EnsureSpace ensure_space(this);
1035 // This is not using emit_arith because test doesn't support
1036 // sign-extension of 8-bit operands.
1037 if (reg.is(eax)) {
1038 EMIT(0xA9);
1039 } else {
1040 EMIT(0xF7);
1041 EMIT(0xC0 | reg.code());
1042 }
1043 emit(imm);
1044}
1045
1046
1047void Assembler::test(Register reg, const Operand& op) {
1048 EnsureSpace ensure_space(this);
1049 EMIT(0x85);
1050 emit_operand(reg, op);
1051}
1052
1053
1054void Assembler::test_b(Register reg, const Operand& op) {
1055 CHECK(reg.is_byte_register());
1056 EnsureSpace ensure_space(this);
1057 EMIT(0x84);
1058 emit_operand(reg, op);
1059}
1060
1061
1062void Assembler::test(const Operand& op, const Immediate& imm) {
1063 if (op.is_reg_only()) {
1064 test(op.reg(), imm);
1065 return;
1066 }
1067 if (RelocInfo::IsNone(imm.rmode_) && is_uint8(imm.x_)) {
1068 return test_b(op, imm.x_);
1069 }
1070 EnsureSpace ensure_space(this);
1071 EMIT(0xF7);
1072 emit_operand(eax, op);
1073 emit(imm);
1074}
1075
1076
1077void Assembler::test_b(Register reg, uint8_t imm8) {
1078 EnsureSpace ensure_space(this);
1079 // Only use test against byte for registers that have a byte
1080 // variant: eax, ebx, ecx, and edx.
1081 if (reg.is(eax)) {
1082 EMIT(0xA8);
1083 EMIT(imm8);
1084 } else if (reg.is_byte_register()) {
1085 emit_arith_b(0xF6, 0xC0, reg, imm8);
1086 } else {
1087 EMIT(0xF7);
1088 EMIT(0xC0 | reg.code());
1089 emit(imm8);
1090 }
1091}
1092
1093
1094void Assembler::test_b(const Operand& op, uint8_t imm8) {
1095 if (op.is_reg_only()) {
1096 test_b(op.reg(), imm8);
1097 return;
1098 }
1099 EnsureSpace ensure_space(this);
1100 EMIT(0xF6);
1101 emit_operand(eax, op);
1102 EMIT(imm8);
1103}
1104
1105
1106void Assembler::xor_(Register dst, int32_t imm32) {
1107 EnsureSpace ensure_space(this);
1108 emit_arith(6, Operand(dst), Immediate(imm32));
1109}
1110
1111
1112void Assembler::xor_(Register dst, const Operand& src) {
1113 EnsureSpace ensure_space(this);
1114 EMIT(0x33);
1115 emit_operand(dst, src);
1116}
1117
1118
1119void Assembler::xor_(const Operand& dst, Register src) {
1120 EnsureSpace ensure_space(this);
1121 EMIT(0x31);
1122 emit_operand(src, dst);
1123}
1124
1125
1126void Assembler::xor_(const Operand& dst, const Immediate& x) {
1127 EnsureSpace ensure_space(this);
1128 emit_arith(6, dst, x);
1129}
1130
1131
1132void Assembler::bt(const Operand& dst, Register src) {
1133 EnsureSpace ensure_space(this);
1134 EMIT(0x0F);
1135 EMIT(0xA3);
1136 emit_operand(src, dst);
1137}
1138
1139
1140void Assembler::bts(const Operand& dst, Register src) {
1141 EnsureSpace ensure_space(this);
1142 EMIT(0x0F);
1143 EMIT(0xAB);
1144 emit_operand(src, dst);
1145}
1146
1147
1148void Assembler::bsr(Register dst, const Operand& src) {
1149 EnsureSpace ensure_space(this);
1150 EMIT(0x0F);
1151 EMIT(0xBD);
1152 emit_operand(dst, src);
1153}
1154
1155
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001156void Assembler::bsf(Register dst, const Operand& src) {
1157 EnsureSpace ensure_space(this);
1158 EMIT(0x0F);
1159 EMIT(0xBC);
1160 emit_operand(dst, src);
1161}
1162
1163
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001164void Assembler::hlt() {
1165 EnsureSpace ensure_space(this);
1166 EMIT(0xF4);
1167}
1168
1169
1170void Assembler::int3() {
1171 EnsureSpace ensure_space(this);
1172 EMIT(0xCC);
1173}
1174
1175
1176void Assembler::nop() {
1177 EnsureSpace ensure_space(this);
1178 EMIT(0x90);
1179}
1180
1181
1182void Assembler::ret(int imm16) {
1183 EnsureSpace ensure_space(this);
1184 DCHECK(is_uint16(imm16));
1185 if (imm16 == 0) {
1186 EMIT(0xC3);
1187 } else {
1188 EMIT(0xC2);
1189 EMIT(imm16 & 0xFF);
1190 EMIT((imm16 >> 8) & 0xFF);
1191 }
1192}
1193
1194
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001195void Assembler::ud2() {
1196 EnsureSpace ensure_space(this);
1197 EMIT(0x0F);
1198 EMIT(0x0B);
1199}
1200
1201
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001202// Labels refer to positions in the (to be) generated code.
1203// There are bound, linked, and unused labels.
1204//
1205// Bound labels refer to known positions in the already
1206// generated code. pos() is the position the label refers to.
1207//
1208// Linked labels refer to unknown positions in the code
1209// to be generated; pos() is the position of the 32bit
1210// Displacement of the last instruction using the label.
1211
1212
1213void Assembler::print(Label* L) {
1214 if (L->is_unused()) {
1215 PrintF("unused label\n");
1216 } else if (L->is_bound()) {
1217 PrintF("bound label to %d\n", L->pos());
1218 } else if (L->is_linked()) {
1219 Label l = *L;
1220 PrintF("unbound label");
1221 while (l.is_linked()) {
1222 Displacement disp = disp_at(&l);
1223 PrintF("@ %d ", l.pos());
1224 disp.print();
1225 PrintF("\n");
1226 disp.next(&l);
1227 }
1228 } else {
1229 PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
1230 }
1231}
1232
1233
1234void Assembler::bind_to(Label* L, int pos) {
1235 EnsureSpace ensure_space(this);
1236 DCHECK(0 <= pos && pos <= pc_offset()); // must have a valid binding position
1237 while (L->is_linked()) {
1238 Displacement disp = disp_at(L);
1239 int fixup_pos = L->pos();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001240 if (disp.type() == Displacement::CODE_ABSOLUTE) {
1241 long_at_put(fixup_pos, reinterpret_cast<int>(buffer_ + pos));
1242 internal_reference_positions_.push_back(fixup_pos);
1243 } else if (disp.type() == Displacement::CODE_RELATIVE) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001244 // Relative to Code* heap object pointer.
1245 long_at_put(fixup_pos, pos + Code::kHeaderSize - kHeapObjectTag);
1246 } else {
1247 if (disp.type() == Displacement::UNCONDITIONAL_JUMP) {
1248 DCHECK(byte_at(fixup_pos - 1) == 0xE9); // jmp expected
1249 }
1250 // Relative address, relative to point after address.
1251 int imm32 = pos - (fixup_pos + sizeof(int32_t));
1252 long_at_put(fixup_pos, imm32);
1253 }
1254 disp.next(L);
1255 }
1256 while (L->is_near_linked()) {
1257 int fixup_pos = L->near_link_pos();
1258 int offset_to_next =
1259 static_cast<int>(*reinterpret_cast<int8_t*>(addr_at(fixup_pos)));
1260 DCHECK(offset_to_next <= 0);
1261 // Relative address, relative to point after address.
1262 int disp = pos - fixup_pos - sizeof(int8_t);
1263 CHECK(0 <= disp && disp <= 127);
1264 set_byte_at(fixup_pos, disp);
1265 if (offset_to_next < 0) {
1266 L->link_to(fixup_pos + offset_to_next, Label::kNear);
1267 } else {
1268 L->UnuseNear();
1269 }
1270 }
1271 L->bind_to(pos);
1272}
1273
1274
1275void Assembler::bind(Label* L) {
1276 EnsureSpace ensure_space(this);
1277 DCHECK(!L->is_bound()); // label can only be bound once
1278 bind_to(L, pc_offset());
1279}
1280
1281
1282void Assembler::call(Label* L) {
1283 positions_recorder()->WriteRecordedPositions();
1284 EnsureSpace ensure_space(this);
1285 if (L->is_bound()) {
1286 const int long_size = 5;
1287 int offs = L->pos() - pc_offset();
1288 DCHECK(offs <= 0);
1289 // 1110 1000 #32-bit disp.
1290 EMIT(0xE8);
1291 emit(offs - long_size);
1292 } else {
1293 // 1110 1000 #32-bit disp.
1294 EMIT(0xE8);
1295 emit_disp(L, Displacement::OTHER);
1296 }
1297}
1298
1299
1300void Assembler::call(byte* entry, RelocInfo::Mode rmode) {
1301 positions_recorder()->WriteRecordedPositions();
1302 EnsureSpace ensure_space(this);
1303 DCHECK(!RelocInfo::IsCodeTarget(rmode));
1304 EMIT(0xE8);
1305 if (RelocInfo::IsRuntimeEntry(rmode)) {
1306 emit(reinterpret_cast<uint32_t>(entry), rmode);
1307 } else {
1308 emit(entry - (pc_ + sizeof(int32_t)), rmode);
1309 }
1310}
1311
1312
1313int Assembler::CallSize(const Operand& adr) {
1314 // Call size is 1 (opcode) + adr.len_ (operand).
1315 return 1 + adr.len_;
1316}
1317
1318
1319void Assembler::call(const Operand& adr) {
1320 positions_recorder()->WriteRecordedPositions();
1321 EnsureSpace ensure_space(this);
1322 EMIT(0xFF);
1323 emit_operand(edx, adr);
1324}
1325
1326
1327int Assembler::CallSize(Handle<Code> code, RelocInfo::Mode rmode) {
1328 return 1 /* EMIT */ + sizeof(uint32_t) /* emit */;
1329}
1330
1331
1332void Assembler::call(Handle<Code> code,
1333 RelocInfo::Mode rmode,
1334 TypeFeedbackId ast_id) {
1335 positions_recorder()->WriteRecordedPositions();
1336 EnsureSpace ensure_space(this);
1337 DCHECK(RelocInfo::IsCodeTarget(rmode)
1338 || rmode == RelocInfo::CODE_AGE_SEQUENCE);
1339 EMIT(0xE8);
1340 emit(code, rmode, ast_id);
1341}
1342
1343
1344void Assembler::jmp(Label* L, Label::Distance distance) {
1345 EnsureSpace ensure_space(this);
1346 if (L->is_bound()) {
1347 const int short_size = 2;
1348 const int long_size = 5;
1349 int offs = L->pos() - pc_offset();
1350 DCHECK(offs <= 0);
1351 if (is_int8(offs - short_size)) {
1352 // 1110 1011 #8-bit disp.
1353 EMIT(0xEB);
1354 EMIT((offs - short_size) & 0xFF);
1355 } else {
1356 // 1110 1001 #32-bit disp.
1357 EMIT(0xE9);
1358 emit(offs - long_size);
1359 }
1360 } else if (distance == Label::kNear) {
1361 EMIT(0xEB);
1362 emit_near_disp(L);
1363 } else {
1364 // 1110 1001 #32-bit disp.
1365 EMIT(0xE9);
1366 emit_disp(L, Displacement::UNCONDITIONAL_JUMP);
1367 }
1368}
1369
1370
1371void Assembler::jmp(byte* entry, RelocInfo::Mode rmode) {
1372 EnsureSpace ensure_space(this);
1373 DCHECK(!RelocInfo::IsCodeTarget(rmode));
1374 EMIT(0xE9);
1375 if (RelocInfo::IsRuntimeEntry(rmode)) {
1376 emit(reinterpret_cast<uint32_t>(entry), rmode);
1377 } else {
1378 emit(entry - (pc_ + sizeof(int32_t)), rmode);
1379 }
1380}
1381
1382
1383void Assembler::jmp(const Operand& adr) {
1384 EnsureSpace ensure_space(this);
1385 EMIT(0xFF);
1386 emit_operand(esp, adr);
1387}
1388
1389
1390void Assembler::jmp(Handle<Code> code, RelocInfo::Mode rmode) {
1391 EnsureSpace ensure_space(this);
1392 DCHECK(RelocInfo::IsCodeTarget(rmode));
1393 EMIT(0xE9);
1394 emit(code, rmode);
1395}
1396
1397
1398void Assembler::j(Condition cc, Label* L, Label::Distance distance) {
1399 EnsureSpace ensure_space(this);
1400 DCHECK(0 <= cc && static_cast<int>(cc) < 16);
1401 if (L->is_bound()) {
1402 const int short_size = 2;
1403 const int long_size = 6;
1404 int offs = L->pos() - pc_offset();
1405 DCHECK(offs <= 0);
1406 if (is_int8(offs - short_size)) {
1407 // 0111 tttn #8-bit disp
1408 EMIT(0x70 | cc);
1409 EMIT((offs - short_size) & 0xFF);
1410 } else {
1411 // 0000 1111 1000 tttn #32-bit disp
1412 EMIT(0x0F);
1413 EMIT(0x80 | cc);
1414 emit(offs - long_size);
1415 }
1416 } else if (distance == Label::kNear) {
1417 EMIT(0x70 | cc);
1418 emit_near_disp(L);
1419 } else {
1420 // 0000 1111 1000 tttn #32-bit disp
1421 // Note: could eliminate cond. jumps to this jump if condition
1422 // is the same however, seems to be rather unlikely case.
1423 EMIT(0x0F);
1424 EMIT(0x80 | cc);
1425 emit_disp(L, Displacement::OTHER);
1426 }
1427}
1428
1429
1430void Assembler::j(Condition cc, byte* entry, RelocInfo::Mode rmode) {
1431 EnsureSpace ensure_space(this);
1432 DCHECK((0 <= cc) && (static_cast<int>(cc) < 16));
1433 // 0000 1111 1000 tttn #32-bit disp.
1434 EMIT(0x0F);
1435 EMIT(0x80 | cc);
1436 if (RelocInfo::IsRuntimeEntry(rmode)) {
1437 emit(reinterpret_cast<uint32_t>(entry), rmode);
1438 } else {
1439 emit(entry - (pc_ + sizeof(int32_t)), rmode);
1440 }
1441}
1442
1443
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001444void Assembler::j(Condition cc, Handle<Code> code, RelocInfo::Mode rmode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001445 EnsureSpace ensure_space(this);
1446 // 0000 1111 1000 tttn #32-bit disp
1447 EMIT(0x0F);
1448 EMIT(0x80 | cc);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001449 emit(code, rmode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001450}
1451
1452
1453// FPU instructions.
1454
1455void Assembler::fld(int i) {
1456 EnsureSpace ensure_space(this);
1457 emit_farith(0xD9, 0xC0, i);
1458}
1459
1460
1461void Assembler::fstp(int i) {
1462 EnsureSpace ensure_space(this);
1463 emit_farith(0xDD, 0xD8, i);
1464}
1465
1466
1467void Assembler::fld1() {
1468 EnsureSpace ensure_space(this);
1469 EMIT(0xD9);
1470 EMIT(0xE8);
1471}
1472
1473
1474void Assembler::fldpi() {
1475 EnsureSpace ensure_space(this);
1476 EMIT(0xD9);
1477 EMIT(0xEB);
1478}
1479
1480
1481void Assembler::fldz() {
1482 EnsureSpace ensure_space(this);
1483 EMIT(0xD9);
1484 EMIT(0xEE);
1485}
1486
1487
1488void Assembler::fldln2() {
1489 EnsureSpace ensure_space(this);
1490 EMIT(0xD9);
1491 EMIT(0xED);
1492}
1493
1494
1495void Assembler::fld_s(const Operand& adr) {
1496 EnsureSpace ensure_space(this);
1497 EMIT(0xD9);
1498 emit_operand(eax, adr);
1499}
1500
1501
1502void Assembler::fld_d(const Operand& adr) {
1503 EnsureSpace ensure_space(this);
1504 EMIT(0xDD);
1505 emit_operand(eax, adr);
1506}
1507
1508
1509void Assembler::fstp_s(const Operand& adr) {
1510 EnsureSpace ensure_space(this);
1511 EMIT(0xD9);
1512 emit_operand(ebx, adr);
1513}
1514
1515
1516void Assembler::fst_s(const Operand& adr) {
1517 EnsureSpace ensure_space(this);
1518 EMIT(0xD9);
1519 emit_operand(edx, adr);
1520}
1521
1522
1523void Assembler::fldcw(const Operand& adr) {
1524 EnsureSpace ensure_space(this);
1525 EMIT(0xD9);
1526 emit_operand(ebp, adr);
1527}
1528
1529
1530void Assembler::fnstcw(const Operand& adr) {
1531 EnsureSpace ensure_space(this);
1532 EMIT(0xD9);
1533 emit_operand(edi, adr);
1534}
1535
1536
1537void Assembler::fstp_d(const Operand& adr) {
1538 EnsureSpace ensure_space(this);
1539 EMIT(0xDD);
1540 emit_operand(ebx, adr);
1541}
1542
1543
1544void Assembler::fst_d(const Operand& adr) {
1545 EnsureSpace ensure_space(this);
1546 EMIT(0xDD);
1547 emit_operand(edx, adr);
1548}
1549
1550
1551void Assembler::fild_s(const Operand& adr) {
1552 EnsureSpace ensure_space(this);
1553 EMIT(0xDB);
1554 emit_operand(eax, adr);
1555}
1556
1557
1558void Assembler::fild_d(const Operand& adr) {
1559 EnsureSpace ensure_space(this);
1560 EMIT(0xDF);
1561 emit_operand(ebp, adr);
1562}
1563
1564
1565void Assembler::fistp_s(const Operand& adr) {
1566 EnsureSpace ensure_space(this);
1567 EMIT(0xDB);
1568 emit_operand(ebx, adr);
1569}
1570
1571
1572void Assembler::fisttp_s(const Operand& adr) {
1573 DCHECK(IsEnabled(SSE3));
1574 EnsureSpace ensure_space(this);
1575 EMIT(0xDB);
1576 emit_operand(ecx, adr);
1577}
1578
1579
1580void Assembler::fisttp_d(const Operand& adr) {
1581 DCHECK(IsEnabled(SSE3));
1582 EnsureSpace ensure_space(this);
1583 EMIT(0xDD);
1584 emit_operand(ecx, adr);
1585}
1586
1587
1588void Assembler::fist_s(const Operand& adr) {
1589 EnsureSpace ensure_space(this);
1590 EMIT(0xDB);
1591 emit_operand(edx, adr);
1592}
1593
1594
1595void Assembler::fistp_d(const Operand& adr) {
1596 EnsureSpace ensure_space(this);
1597 EMIT(0xDF);
1598 emit_operand(edi, adr);
1599}
1600
1601
1602void Assembler::fabs() {
1603 EnsureSpace ensure_space(this);
1604 EMIT(0xD9);
1605 EMIT(0xE1);
1606}
1607
1608
1609void Assembler::fchs() {
1610 EnsureSpace ensure_space(this);
1611 EMIT(0xD9);
1612 EMIT(0xE0);
1613}
1614
1615
1616void Assembler::fsqrt() {
1617 EnsureSpace ensure_space(this);
1618 EMIT(0xD9);
1619 EMIT(0xFA);
1620}
1621
1622
1623void Assembler::fcos() {
1624 EnsureSpace ensure_space(this);
1625 EMIT(0xD9);
1626 EMIT(0xFF);
1627}
1628
1629
1630void Assembler::fsin() {
1631 EnsureSpace ensure_space(this);
1632 EMIT(0xD9);
1633 EMIT(0xFE);
1634}
1635
1636
1637void Assembler::fptan() {
1638 EnsureSpace ensure_space(this);
1639 EMIT(0xD9);
1640 EMIT(0xF2);
1641}
1642
1643
1644void Assembler::fyl2x() {
1645 EnsureSpace ensure_space(this);
1646 EMIT(0xD9);
1647 EMIT(0xF1);
1648}
1649
1650
1651void Assembler::f2xm1() {
1652 EnsureSpace ensure_space(this);
1653 EMIT(0xD9);
1654 EMIT(0xF0);
1655}
1656
1657
1658void Assembler::fscale() {
1659 EnsureSpace ensure_space(this);
1660 EMIT(0xD9);
1661 EMIT(0xFD);
1662}
1663
1664
1665void Assembler::fninit() {
1666 EnsureSpace ensure_space(this);
1667 EMIT(0xDB);
1668 EMIT(0xE3);
1669}
1670
1671
1672void Assembler::fadd(int i) {
1673 EnsureSpace ensure_space(this);
1674 emit_farith(0xDC, 0xC0, i);
1675}
1676
1677
1678void Assembler::fadd_i(int i) {
1679 EnsureSpace ensure_space(this);
1680 emit_farith(0xD8, 0xC0, i);
1681}
1682
1683
1684void Assembler::fadd_d(const Operand& adr) {
1685 EnsureSpace ensure_space(this);
1686 EMIT(0xDC);
1687 emit_operand(eax, adr);
1688}
1689
1690
1691void Assembler::fsub(int i) {
1692 EnsureSpace ensure_space(this);
1693 emit_farith(0xDC, 0xE8, i);
1694}
1695
1696
1697void Assembler::fsub_i(int i) {
1698 EnsureSpace ensure_space(this);
1699 emit_farith(0xD8, 0xE0, i);
1700}
1701
1702
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001703void Assembler::fsubr_d(const Operand& adr) {
1704 EnsureSpace ensure_space(this);
1705 EMIT(0xDC);
1706 emit_operand(ebp, adr);
1707}
1708
1709
1710void Assembler::fsub_d(const Operand& adr) {
1711 EnsureSpace ensure_space(this);
1712 EMIT(0xDC);
1713 emit_operand(esp, adr);
1714}
1715
1716
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001717void Assembler::fisub_s(const Operand& adr) {
1718 EnsureSpace ensure_space(this);
1719 EMIT(0xDA);
1720 emit_operand(esp, adr);
1721}
1722
1723
1724void Assembler::fmul_i(int i) {
1725 EnsureSpace ensure_space(this);
1726 emit_farith(0xD8, 0xC8, i);
1727}
1728
1729
1730void Assembler::fmul(int i) {
1731 EnsureSpace ensure_space(this);
1732 emit_farith(0xDC, 0xC8, i);
1733}
1734
1735
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001736void Assembler::fmul_d(const Operand& adr) {
1737 EnsureSpace ensure_space(this);
1738 EMIT(0xDC);
1739 emit_operand(ecx, adr);
1740}
1741
1742
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001743void Assembler::fdiv(int i) {
1744 EnsureSpace ensure_space(this);
1745 emit_farith(0xDC, 0xF8, i);
1746}
1747
1748
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001749void Assembler::fdiv_d(const Operand& adr) {
1750 EnsureSpace ensure_space(this);
1751 EMIT(0xDC);
1752 emit_operand(esi, adr);
1753}
1754
1755
1756void Assembler::fdivr_d(const Operand& adr) {
1757 EnsureSpace ensure_space(this);
1758 EMIT(0xDC);
1759 emit_operand(edi, adr);
1760}
1761
1762
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001763void Assembler::fdiv_i(int i) {
1764 EnsureSpace ensure_space(this);
1765 emit_farith(0xD8, 0xF0, i);
1766}
1767
1768
1769void Assembler::faddp(int i) {
1770 EnsureSpace ensure_space(this);
1771 emit_farith(0xDE, 0xC0, i);
1772}
1773
1774
1775void Assembler::fsubp(int i) {
1776 EnsureSpace ensure_space(this);
1777 emit_farith(0xDE, 0xE8, i);
1778}
1779
1780
1781void Assembler::fsubrp(int i) {
1782 EnsureSpace ensure_space(this);
1783 emit_farith(0xDE, 0xE0, i);
1784}
1785
1786
1787void Assembler::fmulp(int i) {
1788 EnsureSpace ensure_space(this);
1789 emit_farith(0xDE, 0xC8, i);
1790}
1791
1792
1793void Assembler::fdivp(int i) {
1794 EnsureSpace ensure_space(this);
1795 emit_farith(0xDE, 0xF8, i);
1796}
1797
1798
1799void Assembler::fprem() {
1800 EnsureSpace ensure_space(this);
1801 EMIT(0xD9);
1802 EMIT(0xF8);
1803}
1804
1805
1806void Assembler::fprem1() {
1807 EnsureSpace ensure_space(this);
1808 EMIT(0xD9);
1809 EMIT(0xF5);
1810}
1811
1812
1813void Assembler::fxch(int i) {
1814 EnsureSpace ensure_space(this);
1815 emit_farith(0xD9, 0xC8, i);
1816}
1817
1818
1819void Assembler::fincstp() {
1820 EnsureSpace ensure_space(this);
1821 EMIT(0xD9);
1822 EMIT(0xF7);
1823}
1824
1825
1826void Assembler::ffree(int i) {
1827 EnsureSpace ensure_space(this);
1828 emit_farith(0xDD, 0xC0, i);
1829}
1830
1831
1832void Assembler::ftst() {
1833 EnsureSpace ensure_space(this);
1834 EMIT(0xD9);
1835 EMIT(0xE4);
1836}
1837
1838
1839void Assembler::fxam() {
1840 EnsureSpace ensure_space(this);
1841 EMIT(0xD9);
1842 EMIT(0xE5);
1843}
1844
1845
1846void Assembler::fucomp(int i) {
1847 EnsureSpace ensure_space(this);
1848 emit_farith(0xDD, 0xE8, i);
1849}
1850
1851
1852void Assembler::fucompp() {
1853 EnsureSpace ensure_space(this);
1854 EMIT(0xDA);
1855 EMIT(0xE9);
1856}
1857
1858
1859void Assembler::fucomi(int i) {
1860 EnsureSpace ensure_space(this);
1861 EMIT(0xDB);
1862 EMIT(0xE8 + i);
1863}
1864
1865
1866void Assembler::fucomip() {
1867 EnsureSpace ensure_space(this);
1868 EMIT(0xDF);
1869 EMIT(0xE9);
1870}
1871
1872
1873void Assembler::fcompp() {
1874 EnsureSpace ensure_space(this);
1875 EMIT(0xDE);
1876 EMIT(0xD9);
1877}
1878
1879
1880void Assembler::fnstsw_ax() {
1881 EnsureSpace ensure_space(this);
1882 EMIT(0xDF);
1883 EMIT(0xE0);
1884}
1885
1886
1887void Assembler::fwait() {
1888 EnsureSpace ensure_space(this);
1889 EMIT(0x9B);
1890}
1891
1892
1893void Assembler::frndint() {
1894 EnsureSpace ensure_space(this);
1895 EMIT(0xD9);
1896 EMIT(0xFC);
1897}
1898
1899
1900void Assembler::fnclex() {
1901 EnsureSpace ensure_space(this);
1902 EMIT(0xDB);
1903 EMIT(0xE2);
1904}
1905
1906
1907void Assembler::fnsave(const Operand& adr) {
1908 EnsureSpace ensure_space(this);
1909 EMIT(0xDD);
1910 emit_operand(esi, adr);
1911}
1912
1913
1914void Assembler::frstor(const Operand& adr) {
1915 EnsureSpace ensure_space(this);
1916 EMIT(0xDD);
1917 emit_operand(esp, adr);
1918}
1919
1920
1921void Assembler::sahf() {
1922 EnsureSpace ensure_space(this);
1923 EMIT(0x9E);
1924}
1925
1926
1927void Assembler::setcc(Condition cc, Register reg) {
1928 DCHECK(reg.is_byte_register());
1929 EnsureSpace ensure_space(this);
1930 EMIT(0x0F);
1931 EMIT(0x90 | cc);
1932 EMIT(0xC0 | reg.code());
1933}
1934
1935
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001936void Assembler::GrowBuffer() {
1937 DCHECK(buffer_overflow());
1938 if (!own_buffer_) FATAL("external code buffer is too small");
1939
1940 // Compute new buffer size.
1941 CodeDesc desc; // the new buffer
1942 desc.buffer_size = 2 * buffer_size_;
1943
1944 // Some internal data structures overflow for very large buffers,
1945 // they must ensure that kMaximalBufferSize is not too large.
1946 if ((desc.buffer_size > kMaximalBufferSize) ||
1947 (desc.buffer_size > isolate()->heap()->MaxOldGenerationSize())) {
1948 V8::FatalProcessOutOfMemory("Assembler::GrowBuffer");
1949 }
1950
1951 // Set up new buffer.
1952 desc.buffer = NewArray<byte>(desc.buffer_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001953 desc.origin = this;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001954 desc.instr_size = pc_offset();
1955 desc.reloc_size = (buffer_ + buffer_size_) - (reloc_info_writer.pos());
1956
1957 // Clear the buffer in debug mode. Use 'int3' instructions to make
1958 // sure to get into problems if we ever run uninitialized code.
1959#ifdef DEBUG
1960 memset(desc.buffer, 0xCC, desc.buffer_size);
1961#endif
1962
1963 // Copy the data.
1964 int pc_delta = desc.buffer - buffer_;
1965 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
1966 MemMove(desc.buffer, buffer_, desc.instr_size);
1967 MemMove(rc_delta + reloc_info_writer.pos(), reloc_info_writer.pos(),
1968 desc.reloc_size);
1969
1970 DeleteArray(buffer_);
1971 buffer_ = desc.buffer;
1972 buffer_size_ = desc.buffer_size;
1973 pc_ += pc_delta;
1974 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
1975 reloc_info_writer.last_pc() + pc_delta);
1976
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001977 // Relocate internal references.
1978 for (auto pos : internal_reference_positions_) {
1979 int32_t* p = reinterpret_cast<int32_t*>(buffer_ + pos);
1980 *p += pc_delta;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001981 }
1982
1983 DCHECK(!buffer_overflow());
1984}
1985
1986
1987void Assembler::emit_arith_b(int op1, int op2, Register dst, int imm8) {
1988 DCHECK(is_uint8(op1) && is_uint8(op2)); // wrong opcode
1989 DCHECK(is_uint8(imm8));
1990 DCHECK((op1 & 0x01) == 0); // should be 8bit operation
1991 EMIT(op1);
1992 EMIT(op2 | dst.code());
1993 EMIT(imm8);
1994}
1995
1996
1997void Assembler::emit_arith(int sel, Operand dst, const Immediate& x) {
1998 DCHECK((0 <= sel) && (sel <= 7));
1999 Register ireg = { sel };
2000 if (x.is_int8()) {
2001 EMIT(0x83); // using a sign-extended 8-bit immediate.
2002 emit_operand(ireg, dst);
2003 EMIT(x.x_ & 0xFF);
2004 } else if (dst.is_reg(eax)) {
2005 EMIT((sel << 3) | 0x05); // short form if the destination is eax.
2006 emit(x);
2007 } else {
2008 EMIT(0x81); // using a literal 32-bit immediate.
2009 emit_operand(ireg, dst);
2010 emit(x);
2011 }
2012}
2013
2014
2015void Assembler::emit_operand(Register reg, const Operand& adr) {
2016 const unsigned length = adr.len_;
2017 DCHECK(length > 0);
2018
2019 // Emit updated ModRM byte containing the given register.
2020 pc_[0] = (adr.buf_[0] & ~0x38) | (reg.code() << 3);
2021
2022 // Emit the rest of the encoded operand.
2023 for (unsigned i = 1; i < length; i++) pc_[i] = adr.buf_[i];
2024 pc_ += length;
2025
2026 // Emit relocation information if necessary.
2027 if (length >= sizeof(int32_t) && !RelocInfo::IsNone(adr.rmode_)) {
2028 pc_ -= sizeof(int32_t); // pc_ must be *at* disp32
2029 RecordRelocInfo(adr.rmode_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002030 if (adr.rmode_ == RelocInfo::INTERNAL_REFERENCE) { // Fixup for labels
2031 emit_label(*reinterpret_cast<Label**>(pc_));
2032 } else {
2033 pc_ += sizeof(int32_t);
2034 }
2035 }
2036}
2037
2038
2039void Assembler::emit_label(Label* label) {
2040 if (label->is_bound()) {
2041 internal_reference_positions_.push_back(pc_offset());
2042 emit(reinterpret_cast<uint32_t>(buffer_ + label->pos()));
2043 } else {
2044 emit_disp(label, Displacement::CODE_ABSOLUTE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002045 }
2046}
2047
2048
2049void Assembler::emit_farith(int b1, int b2, int i) {
2050 DCHECK(is_uint8(b1) && is_uint8(b2)); // wrong opcode
2051 DCHECK(0 <= i && i < 8); // illegal stack offset
2052 EMIT(b1);
2053 EMIT(b2 + i);
2054}
2055
2056
2057void Assembler::db(uint8_t data) {
2058 EnsureSpace ensure_space(this);
2059 EMIT(data);
2060}
2061
2062
2063void Assembler::dd(uint32_t data) {
2064 EnsureSpace ensure_space(this);
2065 emit(data);
2066}
2067
2068
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002069void Assembler::dq(uint64_t data) {
2070 EnsureSpace ensure_space(this);
2071 emit_q(data);
2072}
2073
2074
2075void Assembler::dd(Label* label) {
2076 EnsureSpace ensure_space(this);
2077 RecordRelocInfo(RelocInfo::INTERNAL_REFERENCE);
2078 emit_label(label);
2079}
2080
2081
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002082void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
2083 DCHECK(!RelocInfo::IsNone(rmode));
2084 // Don't record external references unless the heap will be serialized.
2085 if (rmode == RelocInfo::EXTERNAL_REFERENCE &&
2086 !serializer_enabled() && !emit_debug_code()) {
2087 return;
2088 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002089 RelocInfo rinfo(isolate(), pc_, rmode, data, NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002090 reloc_info_writer.Write(&rinfo);
2091}
2092
2093
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002094#ifdef GENERATED_CODE_COVERAGE
2095static FILE* coverage_log = NULL;
2096
2097
2098static void InitCoverageLog() {
2099 char* file_name = getenv("V8_GENERATED_CODE_COVERAGE_LOG");
2100 if (file_name != NULL) {
2101 coverage_log = fopen(file_name, "aw+");
2102 }
2103}
2104
2105
2106void LogGeneratedCodeCoverage(const char* file_line) {
2107 const char* return_address = (&file_line)[-1];
2108 char* push_insn = const_cast<char*>(return_address - 12);
2109 push_insn[0] = 0xeb; // Relative branch insn.
2110 push_insn[1] = 13; // Skip over coverage insns.
2111 if (coverage_log != NULL) {
2112 fprintf(coverage_log, "%s\n", file_line);
2113 fflush(coverage_log);
2114 }
2115}
2116
2117#endif
2118
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002119} // namespace internal
2120} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002121
2122#endif // V8_TARGET_ARCH_X87