blob: d6f555082a5a42e5ab85aee18287469dd2a41e07 [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.
35// Copyright 2006-2008 the V8 project authors. All rights reserved.
36
37#include "v8.h"
38
39#include "disassembler.h"
40#include "macro-assembler.h"
41#include "serialize.h"
42
43namespace v8 {
44namespace internal {
45
46// -----------------------------------------------------------------------------
47// Implementation of CpuFeatures
48
49// Safe default is no features.
50uint64_t CpuFeatures::supported_ = 0;
51uint64_t CpuFeatures::enabled_ = 0;
Steve Blockd0582a62009-12-15 09:54:21 +000052uint64_t CpuFeatures::found_by_runtime_probing_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +000053
54
55// The Probe method needs executable memory, so it uses Heap::CreateCode.
56// Allocation failure is silent and leads to safe default.
57void CpuFeatures::Probe() {
58 ASSERT(Heap::HasBeenSetup());
59 ASSERT(supported_ == 0);
Steve Blockd0582a62009-12-15 09:54:21 +000060 if (Serializer::enabled()) {
61 supported_ |= OS::CpuFeaturesImpliedByPlatform();
62 return; // No features if we might serialize.
63 }
Steve Blocka7e24c12009-10-30 11:49:00 +000064
65 Assembler assm(NULL, 0);
66 Label cpuid, done;
67#define __ assm.
68 // Save old esp, since we are going to modify the stack.
69 __ push(ebp);
70 __ pushfd();
71 __ push(ecx);
72 __ push(ebx);
73 __ mov(ebp, Operand(esp));
74
75 // If we can modify bit 21 of the EFLAGS register, then CPUID is supported.
76 __ pushfd();
77 __ pop(eax);
78 __ mov(edx, Operand(eax));
79 __ xor_(eax, 0x200000); // Flip bit 21.
80 __ push(eax);
81 __ popfd();
82 __ pushfd();
83 __ pop(eax);
84 __ xor_(eax, Operand(edx)); // Different if CPUID is supported.
85 __ j(not_zero, &cpuid);
86
87 // CPUID not supported. Clear the supported features in edx:eax.
88 __ xor_(eax, Operand(eax));
89 __ xor_(edx, Operand(edx));
90 __ jmp(&done);
91
92 // Invoke CPUID with 1 in eax to get feature information in
93 // ecx:edx. Temporarily enable CPUID support because we know it's
94 // safe here.
95 __ bind(&cpuid);
96 __ mov(eax, 1);
97 supported_ = (1 << CPUID);
98 { Scope fscope(CPUID);
99 __ cpuid();
100 }
101 supported_ = 0;
102
103 // Move the result from ecx:edx to edx:eax and make sure to mark the
104 // CPUID feature as supported.
105 __ mov(eax, Operand(edx));
106 __ or_(eax, 1 << CPUID);
107 __ mov(edx, Operand(ecx));
108
109 // Done.
110 __ bind(&done);
111 __ mov(esp, Operand(ebp));
112 __ pop(ebx);
113 __ pop(ecx);
114 __ popfd();
115 __ pop(ebp);
116 __ ret(0);
117#undef __
118
119 CodeDesc desc;
120 assm.GetCode(&desc);
121 Object* code = Heap::CreateCode(desc,
122 NULL,
123 Code::ComputeFlags(Code::STUB),
124 Handle<Code>::null());
125 if (!code->IsCode()) return;
126 LOG(CodeCreateEvent(Logger::BUILTIN_TAG,
127 Code::cast(code), "CpuFeatures::Probe"));
128 typedef uint64_t (*F0)();
129 F0 probe = FUNCTION_CAST<F0>(Code::cast(code)->entry());
130 supported_ = probe();
Steve Blockd0582a62009-12-15 09:54:21 +0000131 found_by_runtime_probing_ = supported_;
132 uint64_t os_guarantees = OS::CpuFeaturesImpliedByPlatform();
133 supported_ |= os_guarantees;
134 found_by_runtime_probing_ &= ~os_guarantees;
Steve Blocka7e24c12009-10-30 11:49:00 +0000135}
136
137
138// -----------------------------------------------------------------------------
139// Implementation of Displacement
140
141void Displacement::init(Label* L, Type type) {
142 ASSERT(!L->is_bound());
143 int next = 0;
144 if (L->is_linked()) {
145 next = L->pos();
146 ASSERT(next > 0); // Displacements must be at positions > 0
147 }
148 // Ensure that we _never_ overflow the next field.
149 ASSERT(NextField::is_valid(Assembler::kMaximalBufferSize));
150 data_ = NextField::encode(next) | TypeField::encode(type);
151}
152
153
154// -----------------------------------------------------------------------------
155// Implementation of RelocInfo
156
157
158const int RelocInfo::kApplyMask =
159 RelocInfo::kCodeTargetMask | 1 << RelocInfo::RUNTIME_ENTRY |
160 1 << RelocInfo::JS_RETURN | 1 << RelocInfo::INTERNAL_REFERENCE;
161
162
163void RelocInfo::PatchCode(byte* instructions, int instruction_count) {
164 // Patch the code at the current address with the supplied instructions.
165 for (int i = 0; i < instruction_count; i++) {
166 *(pc_ + i) = *(instructions + i);
167 }
168
169 // Indicate that code has changed.
170 CPU::FlushICache(pc_, instruction_count);
171}
172
173
174// Patch the code at the current PC with a call to the target address.
175// Additional guard int3 instructions can be added if required.
176void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) {
177 // Call instruction takes up 5 bytes and int3 takes up one byte.
178 static const int kCallCodeSize = 5;
179 int code_size = kCallCodeSize + guard_bytes;
180
181 // Create a code patcher.
182 CodePatcher patcher(pc_, code_size);
183
184 // Add a label for checking the size of the code used for returning.
185#ifdef DEBUG
186 Label check_codesize;
187 patcher.masm()->bind(&check_codesize);
188#endif
189
190 // Patch the code.
191 patcher.masm()->call(target, RelocInfo::NONE);
192
193 // Check that the size of the code generated is as expected.
194 ASSERT_EQ(kCallCodeSize,
195 patcher.masm()->SizeOfCodeGeneratedSince(&check_codesize));
196
197 // Add the requested number of int3 instructions after the call.
198 for (int i = 0; i < guard_bytes; i++) {
199 patcher.masm()->int3();
200 }
201}
202
203
204// -----------------------------------------------------------------------------
205// Implementation of Operand
206
207Operand::Operand(Register base, int32_t disp, RelocInfo::Mode rmode) {
208 // [base + disp/r]
209 if (disp == 0 && rmode == RelocInfo::NONE && !base.is(ebp)) {
210 // [base]
211 set_modrm(0, base);
212 if (base.is(esp)) set_sib(times_1, esp, base);
213 } else if (is_int8(disp) && rmode == RelocInfo::NONE) {
214 // [base + disp8]
215 set_modrm(1, base);
216 if (base.is(esp)) set_sib(times_1, esp, base);
217 set_disp8(disp);
218 } else {
219 // [base + disp/r]
220 set_modrm(2, base);
221 if (base.is(esp)) set_sib(times_1, esp, base);
222 set_dispr(disp, rmode);
223 }
224}
225
226
227Operand::Operand(Register base,
228 Register index,
229 ScaleFactor scale,
230 int32_t disp,
231 RelocInfo::Mode rmode) {
232 ASSERT(!index.is(esp)); // illegal addressing mode
233 // [base + index*scale + disp/r]
234 if (disp == 0 && rmode == RelocInfo::NONE && !base.is(ebp)) {
235 // [base + index*scale]
236 set_modrm(0, esp);
237 set_sib(scale, index, base);
238 } else if (is_int8(disp) && rmode == RelocInfo::NONE) {
239 // [base + index*scale + disp8]
240 set_modrm(1, esp);
241 set_sib(scale, index, base);
242 set_disp8(disp);
243 } else {
244 // [base + index*scale + disp/r]
245 set_modrm(2, esp);
246 set_sib(scale, index, base);
247 set_dispr(disp, rmode);
248 }
249}
250
251
252Operand::Operand(Register index,
253 ScaleFactor scale,
254 int32_t disp,
255 RelocInfo::Mode rmode) {
256 ASSERT(!index.is(esp)); // illegal addressing mode
257 // [index*scale + disp/r]
258 set_modrm(0, esp);
259 set_sib(scale, index, ebp);
260 set_dispr(disp, rmode);
261}
262
263
264bool Operand::is_reg(Register reg) const {
265 return ((buf_[0] & 0xF8) == 0xC0) // addressing mode is register only.
266 && ((buf_[0] & 0x07) == reg.code()); // register codes match.
267}
268
269// -----------------------------------------------------------------------------
270// Implementation of Assembler
271
272// Emit a single byte. Must always be inlined.
273#define EMIT(x) \
274 *pc_++ = (x)
275
276
277#ifdef GENERATED_CODE_COVERAGE
278static void InitCoverageLog();
279#endif
280
281// spare_buffer_
282byte* Assembler::spare_buffer_ = NULL;
283
284Assembler::Assembler(void* buffer, int buffer_size) {
285 if (buffer == NULL) {
286 // do our own buffer management
287 if (buffer_size <= kMinimalBufferSize) {
288 buffer_size = kMinimalBufferSize;
289
290 if (spare_buffer_ != NULL) {
291 buffer = spare_buffer_;
292 spare_buffer_ = NULL;
293 }
294 }
295 if (buffer == NULL) {
296 buffer_ = NewArray<byte>(buffer_size);
297 } else {
298 buffer_ = static_cast<byte*>(buffer);
299 }
300 buffer_size_ = buffer_size;
301 own_buffer_ = true;
302 } else {
303 // use externally provided buffer instead
304 ASSERT(buffer_size > 0);
305 buffer_ = static_cast<byte*>(buffer);
306 buffer_size_ = buffer_size;
307 own_buffer_ = false;
308 }
309
310 // Clear the buffer in debug mode unless it was provided by the
311 // caller in which case we can't be sure it's okay to overwrite
312 // existing code in it; see CodePatcher::CodePatcher(...).
313#ifdef DEBUG
314 if (own_buffer_) {
315 memset(buffer_, 0xCC, buffer_size); // int3
316 }
317#endif
318
319 // setup buffer pointers
320 ASSERT(buffer_ != NULL);
321 pc_ = buffer_;
322 reloc_info_writer.Reposition(buffer_ + buffer_size, pc_);
323
324 last_pc_ = NULL;
325 current_statement_position_ = RelocInfo::kNoPosition;
326 current_position_ = RelocInfo::kNoPosition;
327 written_statement_position_ = current_statement_position_;
328 written_position_ = current_position_;
329#ifdef GENERATED_CODE_COVERAGE
330 InitCoverageLog();
331#endif
332}
333
334
335Assembler::~Assembler() {
336 if (own_buffer_) {
337 if (spare_buffer_ == NULL && buffer_size_ == kMinimalBufferSize) {
338 spare_buffer_ = buffer_;
339 } else {
340 DeleteArray(buffer_);
341 }
342 }
343}
344
345
346void Assembler::GetCode(CodeDesc* desc) {
347 // finalize code
348 // (at this point overflow() may be true, but the gap ensures that
349 // we are still not overlapping instructions and relocation info)
350 ASSERT(pc_ <= reloc_info_writer.pos()); // no overlap
351 // setup desc
352 desc->buffer = buffer_;
353 desc->buffer_size = buffer_size_;
354 desc->instr_size = pc_offset();
355 desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
356 desc->origin = this;
357
358 Counters::reloc_info_size.Increment(desc->reloc_size);
359}
360
361
362void Assembler::Align(int m) {
363 ASSERT(IsPowerOf2(m));
364 while ((pc_offset() & (m - 1)) != 0) {
365 nop();
366 }
367}
368
369
370void Assembler::cpuid() {
Steve Blockd0582a62009-12-15 09:54:21 +0000371 ASSERT(CpuFeatures::IsEnabled(CPUID));
Steve Blocka7e24c12009-10-30 11:49:00 +0000372 EnsureSpace ensure_space(this);
373 last_pc_ = pc_;
374 EMIT(0x0F);
375 EMIT(0xA2);
376}
377
378
379void Assembler::pushad() {
380 EnsureSpace ensure_space(this);
381 last_pc_ = pc_;
382 EMIT(0x60);
383}
384
385
386void Assembler::popad() {
387 EnsureSpace ensure_space(this);
388 last_pc_ = pc_;
389 EMIT(0x61);
390}
391
392
393void Assembler::pushfd() {
394 EnsureSpace ensure_space(this);
395 last_pc_ = pc_;
396 EMIT(0x9C);
397}
398
399
400void Assembler::popfd() {
401 EnsureSpace ensure_space(this);
402 last_pc_ = pc_;
403 EMIT(0x9D);
404}
405
406
407void Assembler::push(const Immediate& x) {
408 EnsureSpace ensure_space(this);
409 last_pc_ = pc_;
410 if (x.is_int8()) {
411 EMIT(0x6a);
412 EMIT(x.x_);
413 } else {
414 EMIT(0x68);
415 emit(x);
416 }
417}
418
419
420void Assembler::push(Register src) {
421 EnsureSpace ensure_space(this);
422 last_pc_ = pc_;
423 EMIT(0x50 | src.code());
424}
425
426
427void Assembler::push(const Operand& src) {
428 EnsureSpace ensure_space(this);
429 last_pc_ = pc_;
430 EMIT(0xFF);
431 emit_operand(esi, src);
432}
433
434
435void Assembler::pop(Register dst) {
436 ASSERT(reloc_info_writer.last_pc() != NULL);
437 if (FLAG_push_pop_elimination && (reloc_info_writer.last_pc() <= last_pc_)) {
438 // (last_pc_ != NULL) is rolled into the above check
439 // If a last_pc_ is set, we need to make sure that there has not been any
440 // relocation information generated between the last instruction and this
441 // pop instruction.
442 byte instr = last_pc_[0];
443 if ((instr & ~0x7) == 0x50) {
444 int push_reg_code = instr & 0x7;
445 if (push_reg_code == dst.code()) {
446 pc_ = last_pc_;
447 if (FLAG_print_push_pop_elimination) {
448 PrintF("%d push/pop (same reg) eliminated\n", pc_offset());
449 }
450 } else {
451 // Convert 'push src; pop dst' to 'mov dst, src'.
452 last_pc_[0] = 0x8b;
453 Register src = { push_reg_code };
454 EnsureSpace ensure_space(this);
455 emit_operand(dst, Operand(src));
456 if (FLAG_print_push_pop_elimination) {
457 PrintF("%d push/pop (reg->reg) eliminated\n", pc_offset());
458 }
459 }
460 last_pc_ = NULL;
461 return;
462 } else if (instr == 0xff) { // push of an operand, convert to a move
463 byte op1 = last_pc_[1];
464 // Check if the operation is really a push
465 if ((op1 & 0x38) == (6 << 3)) {
466 op1 = (op1 & ~0x38) | static_cast<byte>(dst.code() << 3);
467 last_pc_[0] = 0x8b;
468 last_pc_[1] = op1;
469 last_pc_ = NULL;
470 if (FLAG_print_push_pop_elimination) {
471 PrintF("%d push/pop (op->reg) eliminated\n", pc_offset());
472 }
473 return;
474 }
475 } else if ((instr == 0x89) &&
476 (last_pc_[1] == 0x04) &&
477 (last_pc_[2] == 0x24)) {
478 // 0x71283c 396 890424 mov [esp],eax
479 // 0x71283f 399 58 pop eax
480 if (dst.is(eax)) {
481 // change to
482 // 0x710fac 216 83c404 add esp,0x4
483 last_pc_[0] = 0x83;
484 last_pc_[1] = 0xc4;
485 last_pc_[2] = 0x04;
486 last_pc_ = NULL;
487 if (FLAG_print_push_pop_elimination) {
488 PrintF("%d push/pop (mov-pop) eliminated\n", pc_offset());
489 }
490 return;
491 }
492 } else if (instr == 0x6a && dst.is(eax)) { // push of immediate 8 bit
493 byte imm8 = last_pc_[1];
494 if (imm8 == 0) {
495 // 6a00 push 0x0
496 // 58 pop eax
497 last_pc_[0] = 0x31;
498 last_pc_[1] = 0xc0;
499 // change to
500 // 31c0 xor eax,eax
501 last_pc_ = NULL;
502 if (FLAG_print_push_pop_elimination) {
503 PrintF("%d push/pop (imm->reg) eliminated\n", pc_offset());
504 }
505 return;
506 } else {
507 // 6a00 push 0xXX
508 // 58 pop eax
509 last_pc_[0] = 0xb8;
510 EnsureSpace ensure_space(this);
511 if ((imm8 & 0x80) != 0) {
512 EMIT(0xff);
513 EMIT(0xff);
514 EMIT(0xff);
515 // change to
516 // b8XXffffff mov eax,0xffffffXX
517 } else {
518 EMIT(0x00);
519 EMIT(0x00);
520 EMIT(0x00);
521 // change to
522 // b8XX000000 mov eax,0x000000XX
523 }
524 last_pc_ = NULL;
525 if (FLAG_print_push_pop_elimination) {
526 PrintF("%d push/pop (imm->reg) eliminated\n", pc_offset());
527 }
528 return;
529 }
530 } else if (instr == 0x68 && dst.is(eax)) { // push of immediate 32 bit
531 // 68XXXXXXXX push 0xXXXXXXXX
532 // 58 pop eax
533 last_pc_[0] = 0xb8;
534 last_pc_ = NULL;
535 // change to
536 // b8XXXXXXXX mov eax,0xXXXXXXXX
537 if (FLAG_print_push_pop_elimination) {
538 PrintF("%d push/pop (imm->reg) eliminated\n", pc_offset());
539 }
540 return;
541 }
542
543 // Other potential patterns for peephole:
544 // 0x712716 102 890424 mov [esp], eax
545 // 0x712719 105 8b1424 mov edx, [esp]
546 }
547 EnsureSpace ensure_space(this);
548 last_pc_ = pc_;
549 EMIT(0x58 | dst.code());
550}
551
552
553void Assembler::pop(const Operand& dst) {
554 EnsureSpace ensure_space(this);
555 last_pc_ = pc_;
556 EMIT(0x8F);
557 emit_operand(eax, dst);
558}
559
560
561void Assembler::enter(const Immediate& size) {
562 EnsureSpace ensure_space(this);
563 last_pc_ = pc_;
564 EMIT(0xC8);
565 emit_w(size);
566 EMIT(0);
567}
568
569
570void Assembler::leave() {
571 EnsureSpace ensure_space(this);
572 last_pc_ = pc_;
573 EMIT(0xC9);
574}
575
576
577void Assembler::mov_b(Register dst, const Operand& src) {
578 EnsureSpace ensure_space(this);
579 last_pc_ = pc_;
580 EMIT(0x8A);
581 emit_operand(dst, src);
582}
583
584
585void Assembler::mov_b(const Operand& dst, int8_t imm8) {
586 EnsureSpace ensure_space(this);
587 last_pc_ = pc_;
588 EMIT(0xC6);
589 emit_operand(eax, dst);
590 EMIT(imm8);
591}
592
593
594void Assembler::mov_b(const Operand& dst, Register src) {
595 EnsureSpace ensure_space(this);
596 last_pc_ = pc_;
597 EMIT(0x88);
598 emit_operand(src, dst);
599}
600
601
602void Assembler::mov_w(Register dst, const Operand& src) {
603 EnsureSpace ensure_space(this);
604 last_pc_ = pc_;
605 EMIT(0x66);
606 EMIT(0x8B);
607 emit_operand(dst, src);
608}
609
610
611void Assembler::mov_w(const Operand& dst, Register src) {
612 EnsureSpace ensure_space(this);
613 last_pc_ = pc_;
614 EMIT(0x66);
615 EMIT(0x89);
616 emit_operand(src, dst);
617}
618
619
620void Assembler::mov(Register dst, int32_t imm32) {
621 EnsureSpace ensure_space(this);
622 last_pc_ = pc_;
623 EMIT(0xB8 | dst.code());
624 emit(imm32);
625}
626
627
628void Assembler::mov(Register dst, const Immediate& x) {
629 EnsureSpace ensure_space(this);
630 last_pc_ = pc_;
631 EMIT(0xB8 | dst.code());
632 emit(x);
633}
634
635
636void Assembler::mov(Register dst, Handle<Object> handle) {
637 EnsureSpace ensure_space(this);
638 last_pc_ = pc_;
639 EMIT(0xB8 | dst.code());
640 emit(handle);
641}
642
643
644void Assembler::mov(Register dst, const Operand& src) {
645 EnsureSpace ensure_space(this);
646 last_pc_ = pc_;
647 EMIT(0x8B);
648 emit_operand(dst, src);
649}
650
651
652void Assembler::mov(Register dst, Register src) {
653 EnsureSpace ensure_space(this);
654 last_pc_ = pc_;
655 EMIT(0x89);
656 EMIT(0xC0 | src.code() << 3 | dst.code());
657}
658
659
660void Assembler::mov(const Operand& dst, const Immediate& x) {
661 EnsureSpace ensure_space(this);
662 last_pc_ = pc_;
663 EMIT(0xC7);
664 emit_operand(eax, dst);
665 emit(x);
666}
667
668
669void Assembler::mov(const Operand& dst, Handle<Object> handle) {
670 EnsureSpace ensure_space(this);
671 last_pc_ = pc_;
672 EMIT(0xC7);
673 emit_operand(eax, dst);
674 emit(handle);
675}
676
677
678void Assembler::mov(const Operand& dst, Register src) {
679 EnsureSpace ensure_space(this);
680 last_pc_ = pc_;
681 EMIT(0x89);
682 emit_operand(src, dst);
683}
684
685
686void Assembler::movsx_b(Register dst, const Operand& src) {
687 EnsureSpace ensure_space(this);
688 last_pc_ = pc_;
689 EMIT(0x0F);
690 EMIT(0xBE);
691 emit_operand(dst, src);
692}
693
694
695void Assembler::movsx_w(Register dst, const Operand& src) {
696 EnsureSpace ensure_space(this);
697 last_pc_ = pc_;
698 EMIT(0x0F);
699 EMIT(0xBF);
700 emit_operand(dst, src);
701}
702
703
704void Assembler::movzx_b(Register dst, const Operand& src) {
705 EnsureSpace ensure_space(this);
706 last_pc_ = pc_;
707 EMIT(0x0F);
708 EMIT(0xB6);
709 emit_operand(dst, src);
710}
711
712
713void Assembler::movzx_w(Register dst, const Operand& src) {
714 EnsureSpace ensure_space(this);
715 last_pc_ = pc_;
716 EMIT(0x0F);
717 EMIT(0xB7);
718 emit_operand(dst, src);
719}
720
721
722void Assembler::cmov(Condition cc, Register dst, int32_t imm32) {
Steve Blockd0582a62009-12-15 09:54:21 +0000723 ASSERT(CpuFeatures::IsEnabled(CMOV));
Steve Blocka7e24c12009-10-30 11:49:00 +0000724 EnsureSpace ensure_space(this);
725 last_pc_ = pc_;
726 UNIMPLEMENTED();
727 USE(cc);
728 USE(dst);
729 USE(imm32);
730}
731
732
733void Assembler::cmov(Condition cc, Register dst, Handle<Object> handle) {
Steve Blockd0582a62009-12-15 09:54:21 +0000734 ASSERT(CpuFeatures::IsEnabled(CMOV));
Steve Blocka7e24c12009-10-30 11:49:00 +0000735 EnsureSpace ensure_space(this);
736 last_pc_ = pc_;
737 UNIMPLEMENTED();
738 USE(cc);
739 USE(dst);
740 USE(handle);
741}
742
743
744void Assembler::cmov(Condition cc, Register dst, const Operand& src) {
Steve Blockd0582a62009-12-15 09:54:21 +0000745 ASSERT(CpuFeatures::IsEnabled(CMOV));
Steve Blocka7e24c12009-10-30 11:49:00 +0000746 EnsureSpace ensure_space(this);
747 last_pc_ = pc_;
748 // Opcode: 0f 40 + cc /r
749 EMIT(0x0F);
750 EMIT(0x40 + cc);
751 emit_operand(dst, src);
752}
753
754
755void Assembler::xchg(Register dst, Register src) {
756 EnsureSpace ensure_space(this);
757 last_pc_ = pc_;
758 if (src.is(eax) || dst.is(eax)) { // Single-byte encoding
759 EMIT(0x90 | (src.is(eax) ? dst.code() : src.code()));
760 } else {
761 EMIT(0x87);
762 EMIT(0xC0 | src.code() << 3 | dst.code());
763 }
764}
765
766
767void Assembler::adc(Register dst, int32_t imm32) {
768 EnsureSpace ensure_space(this);
769 last_pc_ = pc_;
770 emit_arith(2, Operand(dst), Immediate(imm32));
771}
772
773
774void Assembler::adc(Register dst, const Operand& src) {
775 EnsureSpace ensure_space(this);
776 last_pc_ = pc_;
777 EMIT(0x13);
778 emit_operand(dst, src);
779}
780
781
782void Assembler::add(Register dst, const Operand& src) {
783 EnsureSpace ensure_space(this);
784 last_pc_ = pc_;
785 EMIT(0x03);
786 emit_operand(dst, src);
787}
788
789
790void Assembler::add(const Operand& dst, const Immediate& x) {
791 ASSERT(reloc_info_writer.last_pc() != NULL);
792 if (FLAG_push_pop_elimination && (reloc_info_writer.last_pc() <= last_pc_)) {
793 byte instr = last_pc_[0];
794 if ((instr & 0xf8) == 0x50) {
795 // Last instruction was a push. Check whether this is a pop without a
796 // result.
797 if ((dst.is_reg(esp)) &&
798 (x.x_ == kPointerSize) && (x.rmode_ == RelocInfo::NONE)) {
799 pc_ = last_pc_;
800 last_pc_ = NULL;
801 if (FLAG_print_push_pop_elimination) {
802 PrintF("%d push/pop(noreg) eliminated\n", pc_offset());
803 }
804 return;
805 }
806 }
807 }
808 EnsureSpace ensure_space(this);
809 last_pc_ = pc_;
810 emit_arith(0, dst, x);
811}
812
813
814void Assembler::and_(Register dst, int32_t imm32) {
815 EnsureSpace ensure_space(this);
816 last_pc_ = pc_;
817 emit_arith(4, Operand(dst), Immediate(imm32));
818}
819
820
821void Assembler::and_(Register dst, const Operand& src) {
822 EnsureSpace ensure_space(this);
823 last_pc_ = pc_;
824 EMIT(0x23);
825 emit_operand(dst, src);
826}
827
828
829void Assembler::and_(const Operand& dst, const Immediate& x) {
830 EnsureSpace ensure_space(this);
831 last_pc_ = pc_;
832 emit_arith(4, dst, x);
833}
834
835
836void Assembler::and_(const Operand& dst, Register src) {
837 EnsureSpace ensure_space(this);
838 last_pc_ = pc_;
839 EMIT(0x21);
840 emit_operand(src, dst);
841}
842
843
844void Assembler::cmpb(const Operand& op, int8_t imm8) {
845 EnsureSpace ensure_space(this);
846 last_pc_ = pc_;
847 EMIT(0x80);
848 emit_operand(edi, op); // edi == 7
849 EMIT(imm8);
850}
851
852
853void Assembler::cmpw(const Operand& op, Immediate imm16) {
854 ASSERT(imm16.is_int16());
855 EnsureSpace ensure_space(this);
856 last_pc_ = pc_;
857 EMIT(0x66);
858 EMIT(0x81);
859 emit_operand(edi, op);
860 emit_w(imm16);
861}
862
863
864void Assembler::cmp(Register reg, int32_t imm32) {
865 EnsureSpace ensure_space(this);
866 last_pc_ = pc_;
867 emit_arith(7, Operand(reg), Immediate(imm32));
868}
869
870
871void Assembler::cmp(Register reg, Handle<Object> handle) {
872 EnsureSpace ensure_space(this);
873 last_pc_ = pc_;
874 emit_arith(7, Operand(reg), Immediate(handle));
875}
876
877
878void Assembler::cmp(Register reg, const Operand& op) {
879 EnsureSpace ensure_space(this);
880 last_pc_ = pc_;
881 EMIT(0x3B);
882 emit_operand(reg, op);
883}
884
885
886void Assembler::cmp(const Operand& op, const Immediate& imm) {
887 EnsureSpace ensure_space(this);
888 last_pc_ = pc_;
889 emit_arith(7, op, imm);
890}
891
892
893void Assembler::cmp(const Operand& op, Handle<Object> handle) {
894 EnsureSpace ensure_space(this);
895 last_pc_ = pc_;
896 emit_arith(7, op, Immediate(handle));
897}
898
899
900void Assembler::cmpb_al(const Operand& op) {
901 EnsureSpace ensure_space(this);
902 last_pc_ = pc_;
903 EMIT(0x38); // CMP r/m8, r8
904 emit_operand(eax, op); // eax has same code as register al.
905}
906
907
908void Assembler::cmpw_ax(const Operand& op) {
909 EnsureSpace ensure_space(this);
910 last_pc_ = pc_;
911 EMIT(0x66);
912 EMIT(0x39); // CMP r/m16, r16
913 emit_operand(eax, op); // eax has same code as register ax.
914}
915
916
917void Assembler::dec_b(Register dst) {
918 EnsureSpace ensure_space(this);
919 last_pc_ = pc_;
920 EMIT(0xFE);
921 EMIT(0xC8 | dst.code());
922}
923
924
925void Assembler::dec(Register dst) {
926 EnsureSpace ensure_space(this);
927 last_pc_ = pc_;
928 EMIT(0x48 | dst.code());
929}
930
931
932void Assembler::dec(const Operand& dst) {
933 EnsureSpace ensure_space(this);
934 last_pc_ = pc_;
935 EMIT(0xFF);
936 emit_operand(ecx, dst);
937}
938
939
940void Assembler::cdq() {
941 EnsureSpace ensure_space(this);
942 last_pc_ = pc_;
943 EMIT(0x99);
944}
945
946
947void Assembler::idiv(Register src) {
948 EnsureSpace ensure_space(this);
949 last_pc_ = pc_;
950 EMIT(0xF7);
951 EMIT(0xF8 | src.code());
952}
953
954
955void Assembler::imul(Register reg) {
956 EnsureSpace ensure_space(this);
957 last_pc_ = pc_;
958 EMIT(0xF7);
959 EMIT(0xE8 | reg.code());
960}
961
962
963void Assembler::imul(Register dst, const Operand& src) {
964 EnsureSpace ensure_space(this);
965 last_pc_ = pc_;
966 EMIT(0x0F);
967 EMIT(0xAF);
968 emit_operand(dst, src);
969}
970
971
972void Assembler::imul(Register dst, Register src, int32_t imm32) {
973 EnsureSpace ensure_space(this);
974 last_pc_ = pc_;
975 if (is_int8(imm32)) {
976 EMIT(0x6B);
977 EMIT(0xC0 | dst.code() << 3 | src.code());
978 EMIT(imm32);
979 } else {
980 EMIT(0x69);
981 EMIT(0xC0 | dst.code() << 3 | src.code());
982 emit(imm32);
983 }
984}
985
986
987void Assembler::inc(Register dst) {
988 EnsureSpace ensure_space(this);
989 last_pc_ = pc_;
990 EMIT(0x40 | dst.code());
991}
992
993
994void Assembler::inc(const Operand& dst) {
995 EnsureSpace ensure_space(this);
996 last_pc_ = pc_;
997 EMIT(0xFF);
998 emit_operand(eax, dst);
999}
1000
1001
1002void Assembler::lea(Register dst, const Operand& src) {
1003 EnsureSpace ensure_space(this);
1004 last_pc_ = pc_;
1005 EMIT(0x8D);
1006 emit_operand(dst, src);
1007}
1008
1009
1010void Assembler::mul(Register src) {
1011 EnsureSpace ensure_space(this);
1012 last_pc_ = pc_;
1013 EMIT(0xF7);
1014 EMIT(0xE0 | src.code());
1015}
1016
1017
1018void Assembler::neg(Register dst) {
1019 EnsureSpace ensure_space(this);
1020 last_pc_ = pc_;
1021 EMIT(0xF7);
1022 EMIT(0xD8 | dst.code());
1023}
1024
1025
1026void Assembler::not_(Register dst) {
1027 EnsureSpace ensure_space(this);
1028 last_pc_ = pc_;
1029 EMIT(0xF7);
1030 EMIT(0xD0 | dst.code());
1031}
1032
1033
1034void Assembler::or_(Register dst, int32_t imm32) {
1035 EnsureSpace ensure_space(this);
1036 last_pc_ = pc_;
1037 emit_arith(1, Operand(dst), Immediate(imm32));
1038}
1039
1040
1041void Assembler::or_(Register dst, const Operand& src) {
1042 EnsureSpace ensure_space(this);
1043 last_pc_ = pc_;
1044 EMIT(0x0B);
1045 emit_operand(dst, src);
1046}
1047
1048
1049void Assembler::or_(const Operand& dst, const Immediate& x) {
1050 EnsureSpace ensure_space(this);
1051 last_pc_ = pc_;
1052 emit_arith(1, dst, x);
1053}
1054
1055
1056void Assembler::or_(const Operand& dst, Register src) {
1057 EnsureSpace ensure_space(this);
1058 last_pc_ = pc_;
1059 EMIT(0x09);
1060 emit_operand(src, dst);
1061}
1062
1063
1064void Assembler::rcl(Register dst, uint8_t imm8) {
1065 EnsureSpace ensure_space(this);
1066 last_pc_ = pc_;
1067 ASSERT(is_uint5(imm8)); // illegal shift count
1068 if (imm8 == 1) {
1069 EMIT(0xD1);
1070 EMIT(0xD0 | dst.code());
1071 } else {
1072 EMIT(0xC1);
1073 EMIT(0xD0 | dst.code());
1074 EMIT(imm8);
1075 }
1076}
1077
1078
1079void Assembler::sar(Register dst, uint8_t imm8) {
1080 EnsureSpace ensure_space(this);
1081 last_pc_ = pc_;
1082 ASSERT(is_uint5(imm8)); // illegal shift count
1083 if (imm8 == 1) {
1084 EMIT(0xD1);
1085 EMIT(0xF8 | dst.code());
1086 } else {
1087 EMIT(0xC1);
1088 EMIT(0xF8 | dst.code());
1089 EMIT(imm8);
1090 }
1091}
1092
1093
Steve Blockd0582a62009-12-15 09:54:21 +00001094void Assembler::sar_cl(Register dst) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001095 EnsureSpace ensure_space(this);
1096 last_pc_ = pc_;
1097 EMIT(0xD3);
1098 EMIT(0xF8 | dst.code());
1099}
1100
1101
1102void Assembler::sbb(Register dst, const Operand& src) {
1103 EnsureSpace ensure_space(this);
1104 last_pc_ = pc_;
1105 EMIT(0x1B);
1106 emit_operand(dst, src);
1107}
1108
1109
1110void Assembler::shld(Register dst, const Operand& src) {
1111 EnsureSpace ensure_space(this);
1112 last_pc_ = pc_;
1113 EMIT(0x0F);
1114 EMIT(0xA5);
1115 emit_operand(dst, src);
1116}
1117
1118
1119void Assembler::shl(Register dst, uint8_t imm8) {
1120 EnsureSpace ensure_space(this);
1121 last_pc_ = pc_;
1122 ASSERT(is_uint5(imm8)); // illegal shift count
1123 if (imm8 == 1) {
1124 EMIT(0xD1);
1125 EMIT(0xE0 | dst.code());
1126 } else {
1127 EMIT(0xC1);
1128 EMIT(0xE0 | dst.code());
1129 EMIT(imm8);
1130 }
1131}
1132
1133
Steve Blockd0582a62009-12-15 09:54:21 +00001134void Assembler::shl_cl(Register dst) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001135 EnsureSpace ensure_space(this);
1136 last_pc_ = pc_;
1137 EMIT(0xD3);
1138 EMIT(0xE0 | dst.code());
1139}
1140
1141
1142void Assembler::shrd(Register dst, const Operand& src) {
1143 EnsureSpace ensure_space(this);
1144 last_pc_ = pc_;
1145 EMIT(0x0F);
1146 EMIT(0xAD);
1147 emit_operand(dst, src);
1148}
1149
1150
1151void Assembler::shr(Register dst, uint8_t imm8) {
1152 EnsureSpace ensure_space(this);
1153 last_pc_ = pc_;
1154 ASSERT(is_uint5(imm8)); // illegal shift count
Steve Blockd0582a62009-12-15 09:54:21 +00001155 if (imm8 == 1) {
1156 EMIT(0xD1);
1157 EMIT(0xE8 | dst.code());
1158 } else {
1159 EMIT(0xC1);
1160 EMIT(0xE8 | dst.code());
1161 EMIT(imm8);
1162 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001163}
1164
1165
1166void Assembler::shr_cl(Register dst) {
1167 EnsureSpace ensure_space(this);
1168 last_pc_ = pc_;
Steve Blockd0582a62009-12-15 09:54:21 +00001169 EMIT(0xD3);
Steve Blocka7e24c12009-10-30 11:49:00 +00001170 EMIT(0xE8 | dst.code());
1171}
1172
1173
Steve Block3ce2e202009-11-05 08:53:23 +00001174void Assembler::subb(const Operand& op, int8_t imm8) {
1175 EnsureSpace ensure_space(this);
1176 last_pc_ = pc_;
1177 if (op.is_reg(eax)) {
1178 EMIT(0x2c);
1179 } else {
1180 EMIT(0x80);
1181 emit_operand(ebp, op); // ebp == 5
1182 }
1183 EMIT(imm8);
1184}
1185
1186
Steve Blocka7e24c12009-10-30 11:49:00 +00001187void Assembler::sub(const Operand& dst, const Immediate& x) {
1188 EnsureSpace ensure_space(this);
1189 last_pc_ = pc_;
1190 emit_arith(5, dst, x);
1191}
1192
1193
1194void Assembler::sub(Register dst, const Operand& src) {
1195 EnsureSpace ensure_space(this);
1196 last_pc_ = pc_;
1197 EMIT(0x2B);
1198 emit_operand(dst, src);
1199}
1200
1201
1202void Assembler::sub(const Operand& dst, Register src) {
1203 EnsureSpace ensure_space(this);
1204 last_pc_ = pc_;
1205 EMIT(0x29);
1206 emit_operand(src, dst);
1207}
1208
1209
1210void Assembler::test(Register reg, const Immediate& imm) {
1211 EnsureSpace ensure_space(this);
1212 last_pc_ = pc_;
1213 // Only use test against byte for registers that have a byte
1214 // variant: eax, ebx, ecx, and edx.
1215 if (imm.rmode_ == RelocInfo::NONE && is_uint8(imm.x_) && reg.code() < 4) {
1216 uint8_t imm8 = imm.x_;
1217 if (reg.is(eax)) {
1218 EMIT(0xA8);
1219 EMIT(imm8);
1220 } else {
1221 emit_arith_b(0xF6, 0xC0, reg, imm8);
1222 }
1223 } else {
1224 // This is not using emit_arith because test doesn't support
1225 // sign-extension of 8-bit operands.
1226 if (reg.is(eax)) {
1227 EMIT(0xA9);
1228 } else {
1229 EMIT(0xF7);
1230 EMIT(0xC0 | reg.code());
1231 }
1232 emit(imm);
1233 }
1234}
1235
1236
1237void Assembler::test(Register reg, const Operand& op) {
1238 EnsureSpace ensure_space(this);
1239 last_pc_ = pc_;
1240 EMIT(0x85);
1241 emit_operand(reg, op);
1242}
1243
1244
1245void Assembler::test(const Operand& op, const Immediate& imm) {
1246 EnsureSpace ensure_space(this);
1247 last_pc_ = pc_;
1248 EMIT(0xF7);
1249 emit_operand(eax, op);
1250 emit(imm);
1251}
1252
1253
1254void Assembler::xor_(Register dst, int32_t imm32) {
1255 EnsureSpace ensure_space(this);
1256 last_pc_ = pc_;
1257 emit_arith(6, Operand(dst), Immediate(imm32));
1258}
1259
1260
1261void Assembler::xor_(Register dst, const Operand& src) {
1262 EnsureSpace ensure_space(this);
1263 last_pc_ = pc_;
1264 EMIT(0x33);
1265 emit_operand(dst, src);
1266}
1267
1268
1269void Assembler::xor_(const Operand& src, Register dst) {
1270 EnsureSpace ensure_space(this);
1271 last_pc_ = pc_;
1272 EMIT(0x31);
1273 emit_operand(dst, src);
1274}
1275
1276
1277void Assembler::xor_(const Operand& dst, const Immediate& x) {
1278 EnsureSpace ensure_space(this);
1279 last_pc_ = pc_;
1280 emit_arith(6, dst, x);
1281}
1282
1283
1284void Assembler::bt(const Operand& dst, Register src) {
1285 EnsureSpace ensure_space(this);
1286 last_pc_ = pc_;
1287 EMIT(0x0F);
1288 EMIT(0xA3);
1289 emit_operand(src, dst);
1290}
1291
1292
1293void Assembler::bts(const Operand& dst, Register src) {
1294 EnsureSpace ensure_space(this);
1295 last_pc_ = pc_;
1296 EMIT(0x0F);
1297 EMIT(0xAB);
1298 emit_operand(src, dst);
1299}
1300
1301
1302void Assembler::hlt() {
1303 EnsureSpace ensure_space(this);
1304 last_pc_ = pc_;
1305 EMIT(0xF4);
1306}
1307
1308
1309void Assembler::int3() {
1310 EnsureSpace ensure_space(this);
1311 last_pc_ = pc_;
1312 EMIT(0xCC);
1313}
1314
1315
1316void Assembler::nop() {
1317 EnsureSpace ensure_space(this);
1318 last_pc_ = pc_;
1319 EMIT(0x90);
1320}
1321
1322
1323void Assembler::rdtsc() {
Steve Blockd0582a62009-12-15 09:54:21 +00001324 ASSERT(CpuFeatures::IsEnabled(RDTSC));
Steve Blocka7e24c12009-10-30 11:49:00 +00001325 EnsureSpace ensure_space(this);
1326 last_pc_ = pc_;
1327 EMIT(0x0F);
1328 EMIT(0x31);
1329}
1330
1331
1332void Assembler::ret(int imm16) {
1333 EnsureSpace ensure_space(this);
1334 last_pc_ = pc_;
1335 ASSERT(is_uint16(imm16));
1336 if (imm16 == 0) {
1337 EMIT(0xC3);
1338 } else {
1339 EMIT(0xC2);
1340 EMIT(imm16 & 0xFF);
1341 EMIT((imm16 >> 8) & 0xFF);
1342 }
1343}
1344
1345
1346// Labels refer to positions in the (to be) generated code.
1347// There are bound, linked, and unused labels.
1348//
1349// Bound labels refer to known positions in the already
1350// generated code. pos() is the position the label refers to.
1351//
1352// Linked labels refer to unknown positions in the code
1353// to be generated; pos() is the position of the 32bit
1354// Displacement of the last instruction using the label.
1355
1356
1357void Assembler::print(Label* L) {
1358 if (L->is_unused()) {
1359 PrintF("unused label\n");
1360 } else if (L->is_bound()) {
1361 PrintF("bound label to %d\n", L->pos());
1362 } else if (L->is_linked()) {
1363 Label l = *L;
1364 PrintF("unbound label");
1365 while (l.is_linked()) {
1366 Displacement disp = disp_at(&l);
1367 PrintF("@ %d ", l.pos());
1368 disp.print();
1369 PrintF("\n");
1370 disp.next(&l);
1371 }
1372 } else {
1373 PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
1374 }
1375}
1376
1377
1378void Assembler::bind_to(Label* L, int pos) {
1379 EnsureSpace ensure_space(this);
1380 last_pc_ = NULL;
1381 ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position
1382 while (L->is_linked()) {
1383 Displacement disp = disp_at(L);
1384 int fixup_pos = L->pos();
1385 if (disp.type() == Displacement::CODE_RELATIVE) {
1386 // Relative to Code* heap object pointer.
1387 long_at_put(fixup_pos, pos + Code::kHeaderSize - kHeapObjectTag);
1388 } else {
1389 if (disp.type() == Displacement::UNCONDITIONAL_JUMP) {
1390 ASSERT(byte_at(fixup_pos - 1) == 0xE9); // jmp expected
1391 }
1392 // relative address, relative to point after address
1393 int imm32 = pos - (fixup_pos + sizeof(int32_t));
1394 long_at_put(fixup_pos, imm32);
1395 }
1396 disp.next(L);
1397 }
1398 L->bind_to(pos);
1399}
1400
1401
1402void Assembler::link_to(Label* L, Label* appendix) {
1403 EnsureSpace ensure_space(this);
1404 last_pc_ = NULL;
1405 if (appendix->is_linked()) {
1406 if (L->is_linked()) {
1407 // append appendix to L's list
1408 Label p;
1409 Label q = *L;
1410 do {
1411 p = q;
1412 Displacement disp = disp_at(&q);
1413 disp.next(&q);
1414 } while (q.is_linked());
1415 Displacement disp = disp_at(&p);
1416 disp.link_to(appendix);
1417 disp_at_put(&p, disp);
1418 p.Unuse(); // to avoid assertion failure in ~Label
1419 } else {
1420 // L is empty, simply use appendix
1421 *L = *appendix;
1422 }
1423 }
1424 appendix->Unuse(); // appendix should not be used anymore
1425}
1426
1427
1428void Assembler::bind(Label* L) {
1429 EnsureSpace ensure_space(this);
1430 last_pc_ = NULL;
1431 ASSERT(!L->is_bound()); // label can only be bound once
1432 bind_to(L, pc_offset());
1433}
1434
1435
1436void Assembler::call(Label* L) {
1437 EnsureSpace ensure_space(this);
1438 last_pc_ = pc_;
1439 if (L->is_bound()) {
1440 const int long_size = 5;
1441 int offs = L->pos() - pc_offset();
1442 ASSERT(offs <= 0);
1443 // 1110 1000 #32-bit disp
1444 EMIT(0xE8);
1445 emit(offs - long_size);
1446 } else {
1447 // 1110 1000 #32-bit disp
1448 EMIT(0xE8);
1449 emit_disp(L, Displacement::OTHER);
1450 }
1451}
1452
1453
1454void Assembler::call(byte* entry, RelocInfo::Mode rmode) {
1455 EnsureSpace ensure_space(this);
1456 last_pc_ = pc_;
1457 ASSERT(!RelocInfo::IsCodeTarget(rmode));
1458 EMIT(0xE8);
1459 emit(entry - (pc_ + sizeof(int32_t)), rmode);
1460}
1461
1462
1463void Assembler::call(const Operand& adr) {
1464 EnsureSpace ensure_space(this);
1465 last_pc_ = pc_;
1466 EMIT(0xFF);
1467 emit_operand(edx, adr);
1468}
1469
1470
1471void Assembler::call(Handle<Code> code, RelocInfo::Mode rmode) {
1472 WriteRecordedPositions();
1473 EnsureSpace ensure_space(this);
1474 last_pc_ = pc_;
1475 ASSERT(RelocInfo::IsCodeTarget(rmode));
1476 EMIT(0xE8);
1477 emit(reinterpret_cast<intptr_t>(code.location()), rmode);
1478}
1479
1480
1481void Assembler::jmp(Label* L) {
1482 EnsureSpace ensure_space(this);
1483 last_pc_ = pc_;
1484 if (L->is_bound()) {
1485 const int short_size = 2;
1486 const int long_size = 5;
1487 int offs = L->pos() - pc_offset();
1488 ASSERT(offs <= 0);
1489 if (is_int8(offs - short_size)) {
1490 // 1110 1011 #8-bit disp
1491 EMIT(0xEB);
1492 EMIT((offs - short_size) & 0xFF);
1493 } else {
1494 // 1110 1001 #32-bit disp
1495 EMIT(0xE9);
1496 emit(offs - long_size);
1497 }
1498 } else {
1499 // 1110 1001 #32-bit disp
1500 EMIT(0xE9);
1501 emit_disp(L, Displacement::UNCONDITIONAL_JUMP);
1502 }
1503}
1504
1505
1506void Assembler::jmp(byte* entry, RelocInfo::Mode rmode) {
1507 EnsureSpace ensure_space(this);
1508 last_pc_ = pc_;
1509 ASSERT(!RelocInfo::IsCodeTarget(rmode));
1510 EMIT(0xE9);
1511 emit(entry - (pc_ + sizeof(int32_t)), rmode);
1512}
1513
1514
1515void Assembler::jmp(const Operand& adr) {
1516 EnsureSpace ensure_space(this);
1517 last_pc_ = pc_;
1518 EMIT(0xFF);
1519 emit_operand(esp, adr);
1520}
1521
1522
1523void Assembler::jmp(Handle<Code> code, RelocInfo::Mode rmode) {
1524 EnsureSpace ensure_space(this);
1525 last_pc_ = pc_;
1526 ASSERT(RelocInfo::IsCodeTarget(rmode));
1527 EMIT(0xE9);
1528 emit(reinterpret_cast<intptr_t>(code.location()), rmode);
1529}
1530
1531
1532
1533void Assembler::j(Condition cc, Label* L, Hint hint) {
1534 EnsureSpace ensure_space(this);
1535 last_pc_ = pc_;
1536 ASSERT(0 <= cc && cc < 16);
1537 if (FLAG_emit_branch_hints && hint != no_hint) EMIT(hint);
1538 if (L->is_bound()) {
1539 const int short_size = 2;
1540 const int long_size = 6;
1541 int offs = L->pos() - pc_offset();
1542 ASSERT(offs <= 0);
1543 if (is_int8(offs - short_size)) {
1544 // 0111 tttn #8-bit disp
1545 EMIT(0x70 | cc);
1546 EMIT((offs - short_size) & 0xFF);
1547 } else {
1548 // 0000 1111 1000 tttn #32-bit disp
1549 EMIT(0x0F);
1550 EMIT(0x80 | cc);
1551 emit(offs - long_size);
1552 }
1553 } else {
1554 // 0000 1111 1000 tttn #32-bit disp
1555 // Note: could eliminate cond. jumps to this jump if condition
1556 // is the same however, seems to be rather unlikely case.
1557 EMIT(0x0F);
1558 EMIT(0x80 | cc);
1559 emit_disp(L, Displacement::OTHER);
1560 }
1561}
1562
1563
1564void Assembler::j(Condition cc, byte* entry, RelocInfo::Mode rmode, Hint hint) {
1565 EnsureSpace ensure_space(this);
1566 last_pc_ = pc_;
1567 ASSERT((0 <= cc) && (cc < 16));
1568 if (FLAG_emit_branch_hints && hint != no_hint) EMIT(hint);
1569 // 0000 1111 1000 tttn #32-bit disp
1570 EMIT(0x0F);
1571 EMIT(0x80 | cc);
1572 emit(entry - (pc_ + sizeof(int32_t)), rmode);
1573}
1574
1575
1576void Assembler::j(Condition cc, Handle<Code> code, Hint hint) {
1577 EnsureSpace ensure_space(this);
1578 last_pc_ = pc_;
1579 if (FLAG_emit_branch_hints && hint != no_hint) EMIT(hint);
1580 // 0000 1111 1000 tttn #32-bit disp
1581 EMIT(0x0F);
1582 EMIT(0x80 | cc);
1583 emit(reinterpret_cast<intptr_t>(code.location()), RelocInfo::CODE_TARGET);
1584}
1585
1586
1587// FPU instructions
1588
1589
1590void Assembler::fld(int i) {
1591 EnsureSpace ensure_space(this);
1592 last_pc_ = pc_;
1593 emit_farith(0xD9, 0xC0, i);
1594}
1595
1596
1597void Assembler::fld1() {
1598 EnsureSpace ensure_space(this);
1599 last_pc_ = pc_;
1600 EMIT(0xD9);
1601 EMIT(0xE8);
1602}
1603
1604
1605void Assembler::fldz() {
1606 EnsureSpace ensure_space(this);
1607 last_pc_ = pc_;
1608 EMIT(0xD9);
1609 EMIT(0xEE);
1610}
1611
1612
1613void Assembler::fld_s(const Operand& adr) {
1614 EnsureSpace ensure_space(this);
1615 last_pc_ = pc_;
1616 EMIT(0xD9);
1617 emit_operand(eax, adr);
1618}
1619
1620
1621void Assembler::fld_d(const Operand& adr) {
1622 EnsureSpace ensure_space(this);
1623 last_pc_ = pc_;
1624 EMIT(0xDD);
1625 emit_operand(eax, adr);
1626}
1627
1628
1629void Assembler::fstp_s(const Operand& adr) {
1630 EnsureSpace ensure_space(this);
1631 last_pc_ = pc_;
1632 EMIT(0xD9);
1633 emit_operand(ebx, adr);
1634}
1635
1636
1637void Assembler::fstp_d(const Operand& adr) {
1638 EnsureSpace ensure_space(this);
1639 last_pc_ = pc_;
1640 EMIT(0xDD);
1641 emit_operand(ebx, adr);
1642}
1643
1644
1645void Assembler::fild_s(const Operand& adr) {
1646 EnsureSpace ensure_space(this);
1647 last_pc_ = pc_;
1648 EMIT(0xDB);
1649 emit_operand(eax, adr);
1650}
1651
1652
1653void Assembler::fild_d(const Operand& adr) {
1654 EnsureSpace ensure_space(this);
1655 last_pc_ = pc_;
1656 EMIT(0xDF);
1657 emit_operand(ebp, adr);
1658}
1659
1660
1661void Assembler::fistp_s(const Operand& adr) {
1662 EnsureSpace ensure_space(this);
1663 last_pc_ = pc_;
1664 EMIT(0xDB);
1665 emit_operand(ebx, adr);
1666}
1667
1668
1669void Assembler::fisttp_s(const Operand& adr) {
Steve Blockd0582a62009-12-15 09:54:21 +00001670 ASSERT(CpuFeatures::IsEnabled(SSE3));
Steve Blocka7e24c12009-10-30 11:49:00 +00001671 EnsureSpace ensure_space(this);
1672 last_pc_ = pc_;
1673 EMIT(0xDB);
1674 emit_operand(ecx, adr);
1675}
1676
1677
1678void Assembler::fist_s(const Operand& adr) {
1679 EnsureSpace ensure_space(this);
1680 last_pc_ = pc_;
1681 EMIT(0xDB);
1682 emit_operand(edx, adr);
1683}
1684
1685
1686void Assembler::fistp_d(const Operand& adr) {
1687 EnsureSpace ensure_space(this);
1688 last_pc_ = pc_;
1689 EMIT(0xDF);
1690 emit_operand(edi, adr);
1691}
1692
1693
1694void Assembler::fabs() {
1695 EnsureSpace ensure_space(this);
1696 last_pc_ = pc_;
1697 EMIT(0xD9);
1698 EMIT(0xE1);
1699}
1700
1701
1702void Assembler::fchs() {
1703 EnsureSpace ensure_space(this);
1704 last_pc_ = pc_;
1705 EMIT(0xD9);
1706 EMIT(0xE0);
1707}
1708
1709
1710void Assembler::fcos() {
1711 EnsureSpace ensure_space(this);
1712 last_pc_ = pc_;
1713 EMIT(0xD9);
1714 EMIT(0xFF);
1715}
1716
1717
1718void Assembler::fsin() {
1719 EnsureSpace ensure_space(this);
1720 last_pc_ = pc_;
1721 EMIT(0xD9);
1722 EMIT(0xFE);
1723}
1724
1725
1726void Assembler::fadd(int i) {
1727 EnsureSpace ensure_space(this);
1728 last_pc_ = pc_;
1729 emit_farith(0xDC, 0xC0, i);
1730}
1731
1732
1733void Assembler::fsub(int i) {
1734 EnsureSpace ensure_space(this);
1735 last_pc_ = pc_;
1736 emit_farith(0xDC, 0xE8, i);
1737}
1738
1739
1740void Assembler::fisub_s(const Operand& adr) {
1741 EnsureSpace ensure_space(this);
1742 last_pc_ = pc_;
1743 EMIT(0xDA);
1744 emit_operand(esp, adr);
1745}
1746
1747
1748void Assembler::fmul(int i) {
1749 EnsureSpace ensure_space(this);
1750 last_pc_ = pc_;
1751 emit_farith(0xDC, 0xC8, i);
1752}
1753
1754
1755void Assembler::fdiv(int i) {
1756 EnsureSpace ensure_space(this);
1757 last_pc_ = pc_;
1758 emit_farith(0xDC, 0xF8, i);
1759}
1760
1761
1762void Assembler::faddp(int i) {
1763 EnsureSpace ensure_space(this);
1764 last_pc_ = pc_;
1765 emit_farith(0xDE, 0xC0, i);
1766}
1767
1768
1769void Assembler::fsubp(int i) {
1770 EnsureSpace ensure_space(this);
1771 last_pc_ = pc_;
1772 emit_farith(0xDE, 0xE8, i);
1773}
1774
1775
1776void Assembler::fsubrp(int i) {
1777 EnsureSpace ensure_space(this);
1778 last_pc_ = pc_;
1779 emit_farith(0xDE, 0xE0, i);
1780}
1781
1782
1783void Assembler::fmulp(int i) {
1784 EnsureSpace ensure_space(this);
1785 last_pc_ = pc_;
1786 emit_farith(0xDE, 0xC8, i);
1787}
1788
1789
1790void Assembler::fdivp(int i) {
1791 EnsureSpace ensure_space(this);
1792 last_pc_ = pc_;
1793 emit_farith(0xDE, 0xF8, i);
1794}
1795
1796
1797void Assembler::fprem() {
1798 EnsureSpace ensure_space(this);
1799 last_pc_ = pc_;
1800 EMIT(0xD9);
1801 EMIT(0xF8);
1802}
1803
1804
1805void Assembler::fprem1() {
1806 EnsureSpace ensure_space(this);
1807 last_pc_ = pc_;
1808 EMIT(0xD9);
1809 EMIT(0xF5);
1810}
1811
1812
1813void Assembler::fxch(int i) {
1814 EnsureSpace ensure_space(this);
1815 last_pc_ = pc_;
1816 emit_farith(0xD9, 0xC8, i);
1817}
1818
1819
1820void Assembler::fincstp() {
1821 EnsureSpace ensure_space(this);
1822 last_pc_ = pc_;
1823 EMIT(0xD9);
1824 EMIT(0xF7);
1825}
1826
1827
1828void Assembler::ffree(int i) {
1829 EnsureSpace ensure_space(this);
1830 last_pc_ = pc_;
1831 emit_farith(0xDD, 0xC0, i);
1832}
1833
1834
1835void Assembler::ftst() {
1836 EnsureSpace ensure_space(this);
1837 last_pc_ = pc_;
1838 EMIT(0xD9);
1839 EMIT(0xE4);
1840}
1841
1842
1843void Assembler::fucomp(int i) {
1844 EnsureSpace ensure_space(this);
1845 last_pc_ = pc_;
1846 emit_farith(0xDD, 0xE8, i);
1847}
1848
1849
1850void Assembler::fucompp() {
1851 EnsureSpace ensure_space(this);
1852 last_pc_ = pc_;
1853 EMIT(0xDA);
1854 EMIT(0xE9);
1855}
1856
1857
Steve Block3ce2e202009-11-05 08:53:23 +00001858void Assembler::fucomi(int i) {
1859 EnsureSpace ensure_space(this);
1860 last_pc_ = pc_;
1861 EMIT(0xDB);
1862 EMIT(0xE8 + i);
1863}
1864
1865
1866void Assembler::fucomip() {
1867 EnsureSpace ensure_space(this);
1868 last_pc_ = pc_;
1869 EMIT(0xDF);
1870 EMIT(0xE9);
1871}
1872
1873
Steve Blocka7e24c12009-10-30 11:49:00 +00001874void Assembler::fcompp() {
1875 EnsureSpace ensure_space(this);
1876 last_pc_ = pc_;
1877 EMIT(0xDE);
1878 EMIT(0xD9);
1879}
1880
1881
1882void Assembler::fnstsw_ax() {
1883 EnsureSpace ensure_space(this);
1884 last_pc_ = pc_;
1885 EMIT(0xDF);
1886 EMIT(0xE0);
1887}
1888
1889
1890void Assembler::fwait() {
1891 EnsureSpace ensure_space(this);
1892 last_pc_ = pc_;
1893 EMIT(0x9B);
1894}
1895
1896
1897void Assembler::frndint() {
1898 EnsureSpace ensure_space(this);
1899 last_pc_ = pc_;
1900 EMIT(0xD9);
1901 EMIT(0xFC);
1902}
1903
1904
1905void Assembler::fnclex() {
1906 EnsureSpace ensure_space(this);
1907 last_pc_ = pc_;
1908 EMIT(0xDB);
1909 EMIT(0xE2);
1910}
1911
1912
1913void Assembler::sahf() {
1914 EnsureSpace ensure_space(this);
1915 last_pc_ = pc_;
1916 EMIT(0x9E);
1917}
1918
1919
1920void Assembler::setcc(Condition cc, Register reg) {
1921 ASSERT(reg.is_byte_register());
1922 EnsureSpace ensure_space(this);
1923 last_pc_ = pc_;
1924 EMIT(0x0F);
1925 EMIT(0x90 | cc);
1926 EMIT(0xC0 | reg.code());
1927}
1928
1929
1930void Assembler::cvttss2si(Register dst, const Operand& src) {
Steve Blockd0582a62009-12-15 09:54:21 +00001931 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001932 EnsureSpace ensure_space(this);
1933 last_pc_ = pc_;
1934 EMIT(0xF3);
1935 EMIT(0x0F);
1936 EMIT(0x2C);
1937 emit_operand(dst, src);
1938}
1939
1940
1941void Assembler::cvttsd2si(Register dst, const Operand& src) {
Steve Blockd0582a62009-12-15 09:54:21 +00001942 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001943 EnsureSpace ensure_space(this);
1944 last_pc_ = pc_;
1945 EMIT(0xF2);
1946 EMIT(0x0F);
1947 EMIT(0x2C);
1948 emit_operand(dst, src);
1949}
1950
1951
1952void Assembler::cvtsi2sd(XMMRegister dst, const Operand& src) {
Steve Blockd0582a62009-12-15 09:54:21 +00001953 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001954 EnsureSpace ensure_space(this);
1955 last_pc_ = pc_;
1956 EMIT(0xF2);
1957 EMIT(0x0F);
1958 EMIT(0x2A);
1959 emit_sse_operand(dst, src);
1960}
1961
1962
1963void Assembler::addsd(XMMRegister dst, XMMRegister src) {
Steve Blockd0582a62009-12-15 09:54:21 +00001964 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001965 EnsureSpace ensure_space(this);
1966 last_pc_ = pc_;
1967 EMIT(0xF2);
1968 EMIT(0x0F);
1969 EMIT(0x58);
1970 emit_sse_operand(dst, src);
1971}
1972
1973
1974void Assembler::mulsd(XMMRegister dst, XMMRegister src) {
Steve Blockd0582a62009-12-15 09:54:21 +00001975 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001976 EnsureSpace ensure_space(this);
1977 last_pc_ = pc_;
1978 EMIT(0xF2);
1979 EMIT(0x0F);
1980 EMIT(0x59);
1981 emit_sse_operand(dst, src);
1982}
1983
1984
1985void Assembler::subsd(XMMRegister dst, XMMRegister src) {
Steve Blockd0582a62009-12-15 09:54:21 +00001986 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001987 EnsureSpace ensure_space(this);
1988 last_pc_ = pc_;
1989 EMIT(0xF2);
1990 EMIT(0x0F);
1991 EMIT(0x5C);
1992 emit_sse_operand(dst, src);
1993}
1994
1995
1996void Assembler::divsd(XMMRegister dst, XMMRegister src) {
Steve Blockd0582a62009-12-15 09:54:21 +00001997 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001998 EnsureSpace ensure_space(this);
1999 last_pc_ = pc_;
2000 EMIT(0xF2);
2001 EMIT(0x0F);
2002 EMIT(0x5E);
2003 emit_sse_operand(dst, src);
2004}
2005
2006
2007void Assembler::comisd(XMMRegister dst, XMMRegister src) {
Steve Blockd0582a62009-12-15 09:54:21 +00002008 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00002009 EnsureSpace ensure_space(this);
2010 last_pc_ = pc_;
2011 EMIT(0x66);
2012 EMIT(0x0F);
2013 EMIT(0x2F);
2014 emit_sse_operand(dst, src);
2015}
2016
2017
2018void Assembler::movdbl(XMMRegister dst, const Operand& src) {
2019 EnsureSpace ensure_space(this);
2020 last_pc_ = pc_;
2021 movsd(dst, src);
2022}
2023
2024
2025void Assembler::movdbl(const Operand& dst, XMMRegister src) {
2026 EnsureSpace ensure_space(this);
2027 last_pc_ = pc_;
2028 movsd(dst, src);
2029}
2030
2031
2032void Assembler::movsd(const Operand& dst, XMMRegister src ) {
Steve Blockd0582a62009-12-15 09:54:21 +00002033 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00002034 EnsureSpace ensure_space(this);
2035 last_pc_ = pc_;
2036 EMIT(0xF2); // double
2037 EMIT(0x0F);
2038 EMIT(0x11); // store
2039 emit_sse_operand(src, dst);
2040}
2041
2042
2043void Assembler::movsd(XMMRegister dst, const Operand& src) {
Steve Blockd0582a62009-12-15 09:54:21 +00002044 ASSERT(CpuFeatures::IsEnabled(SSE2));
Steve Blocka7e24c12009-10-30 11:49:00 +00002045 EnsureSpace ensure_space(this);
2046 last_pc_ = pc_;
2047 EMIT(0xF2); // double
2048 EMIT(0x0F);
2049 EMIT(0x10); // load
2050 emit_sse_operand(dst, src);
2051}
2052
2053
2054void Assembler::emit_sse_operand(XMMRegister reg, const Operand& adr) {
2055 Register ireg = { reg.code() };
2056 emit_operand(ireg, adr);
2057}
2058
2059
2060void Assembler::emit_sse_operand(XMMRegister dst, XMMRegister src) {
2061 EMIT(0xC0 | dst.code() << 3 | src.code());
2062}
2063
2064
2065void Assembler::Print() {
2066 Disassembler::Decode(stdout, buffer_, pc_);
2067}
2068
2069
2070void Assembler::RecordJSReturn() {
2071 WriteRecordedPositions();
2072 EnsureSpace ensure_space(this);
2073 RecordRelocInfo(RelocInfo::JS_RETURN);
2074}
2075
2076
2077void Assembler::RecordComment(const char* msg) {
2078 if (FLAG_debug_code) {
2079 EnsureSpace ensure_space(this);
2080 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
2081 }
2082}
2083
2084
2085void Assembler::RecordPosition(int pos) {
2086 ASSERT(pos != RelocInfo::kNoPosition);
2087 ASSERT(pos >= 0);
2088 current_position_ = pos;
2089}
2090
2091
2092void Assembler::RecordStatementPosition(int pos) {
2093 ASSERT(pos != RelocInfo::kNoPosition);
2094 ASSERT(pos >= 0);
2095 current_statement_position_ = pos;
2096}
2097
2098
2099void Assembler::WriteRecordedPositions() {
2100 // Write the statement position if it is different from what was written last
2101 // time.
2102 if (current_statement_position_ != written_statement_position_) {
2103 EnsureSpace ensure_space(this);
2104 RecordRelocInfo(RelocInfo::STATEMENT_POSITION, current_statement_position_);
2105 written_statement_position_ = current_statement_position_;
2106 }
2107
2108 // Write the position if it is different from what was written last time and
2109 // also different from the written statement position.
2110 if (current_position_ != written_position_ &&
2111 current_position_ != written_statement_position_) {
2112 EnsureSpace ensure_space(this);
2113 RecordRelocInfo(RelocInfo::POSITION, current_position_);
2114 written_position_ = current_position_;
2115 }
2116}
2117
2118
2119void Assembler::GrowBuffer() {
2120 ASSERT(overflow()); // should not call this otherwise
2121 if (!own_buffer_) FATAL("external code buffer is too small");
2122
2123 // compute new buffer size
2124 CodeDesc desc; // the new buffer
2125 if (buffer_size_ < 4*KB) {
2126 desc.buffer_size = 4*KB;
2127 } else {
2128 desc.buffer_size = 2*buffer_size_;
2129 }
2130 // Some internal data structures overflow for very large buffers,
2131 // they must ensure that kMaximalBufferSize is not too large.
2132 if ((desc.buffer_size > kMaximalBufferSize) ||
Steve Block3ce2e202009-11-05 08:53:23 +00002133 (desc.buffer_size > Heap::MaxOldGenerationSize())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002134 V8::FatalProcessOutOfMemory("Assembler::GrowBuffer");
2135 }
2136
2137 // setup new buffer
2138 desc.buffer = NewArray<byte>(desc.buffer_size);
2139 desc.instr_size = pc_offset();
2140 desc.reloc_size = (buffer_ + buffer_size_) - (reloc_info_writer.pos());
2141
2142 // Clear the buffer in debug mode. Use 'int3' instructions to make
2143 // sure to get into problems if we ever run uninitialized code.
2144#ifdef DEBUG
2145 memset(desc.buffer, 0xCC, desc.buffer_size);
2146#endif
2147
2148 // copy the data
2149 int pc_delta = desc.buffer - buffer_;
2150 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
2151 memmove(desc.buffer, buffer_, desc.instr_size);
2152 memmove(rc_delta + reloc_info_writer.pos(),
2153 reloc_info_writer.pos(), desc.reloc_size);
2154
2155 // switch buffers
2156 if (spare_buffer_ == NULL && buffer_size_ == kMinimalBufferSize) {
2157 spare_buffer_ = buffer_;
2158 } else {
2159 DeleteArray(buffer_);
2160 }
2161 buffer_ = desc.buffer;
2162 buffer_size_ = desc.buffer_size;
2163 pc_ += pc_delta;
2164 if (last_pc_ != NULL) {
2165 last_pc_ += pc_delta;
2166 }
2167 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
2168 reloc_info_writer.last_pc() + pc_delta);
2169
2170 // relocate runtime entries
2171 for (RelocIterator it(desc); !it.done(); it.next()) {
2172 RelocInfo::Mode rmode = it.rinfo()->rmode();
2173 if (rmode == RelocInfo::RUNTIME_ENTRY) {
2174 int32_t* p = reinterpret_cast<int32_t*>(it.rinfo()->pc());
2175 *p -= pc_delta; // relocate entry
2176 } else if (rmode == RelocInfo::INTERNAL_REFERENCE) {
2177 int32_t* p = reinterpret_cast<int32_t*>(it.rinfo()->pc());
2178 if (*p != 0) { // 0 means uninitialized.
2179 *p += pc_delta;
2180 }
2181 }
2182 }
2183
2184 ASSERT(!overflow());
2185}
2186
2187
2188void Assembler::emit_arith_b(int op1, int op2, Register dst, int imm8) {
2189 ASSERT(is_uint8(op1) && is_uint8(op2)); // wrong opcode
2190 ASSERT(is_uint8(imm8));
2191 ASSERT((op1 & 0x01) == 0); // should be 8bit operation
2192 EMIT(op1);
2193 EMIT(op2 | dst.code());
2194 EMIT(imm8);
2195}
2196
2197
2198void Assembler::emit_arith(int sel, Operand dst, const Immediate& x) {
2199 ASSERT((0 <= sel) && (sel <= 7));
2200 Register ireg = { sel };
2201 if (x.is_int8()) {
2202 EMIT(0x83); // using a sign-extended 8-bit immediate.
2203 emit_operand(ireg, dst);
2204 EMIT(x.x_ & 0xFF);
2205 } else if (dst.is_reg(eax)) {
2206 EMIT((sel << 3) | 0x05); // short form if the destination is eax.
2207 emit(x);
2208 } else {
2209 EMIT(0x81); // using a literal 32-bit immediate.
2210 emit_operand(ireg, dst);
2211 emit(x);
2212 }
2213}
2214
2215
2216void Assembler::emit_operand(Register reg, const Operand& adr) {
2217 const unsigned length = adr.len_;
2218 ASSERT(length > 0);
2219
2220 // Emit updated ModRM byte containing the given register.
2221 pc_[0] = (adr.buf_[0] & ~0x38) | (reg.code() << 3);
2222
2223 // Emit the rest of the encoded operand.
2224 for (unsigned i = 1; i < length; i++) pc_[i] = adr.buf_[i];
2225 pc_ += length;
2226
2227 // Emit relocation information if necessary.
2228 if (length >= sizeof(int32_t) && adr.rmode_ != RelocInfo::NONE) {
2229 pc_ -= sizeof(int32_t); // pc_ must be *at* disp32
2230 RecordRelocInfo(adr.rmode_);
2231 pc_ += sizeof(int32_t);
2232 }
2233}
2234
2235
2236void Assembler::emit_farith(int b1, int b2, int i) {
2237 ASSERT(is_uint8(b1) && is_uint8(b2)); // wrong opcode
2238 ASSERT(0 <= i && i < 8); // illegal stack offset
2239 EMIT(b1);
2240 EMIT(b2 + i);
2241}
2242
2243
2244void Assembler::dd(uint32_t data, RelocInfo::Mode reloc_info) {
2245 EnsureSpace ensure_space(this);
2246 emit(data, reloc_info);
2247}
2248
2249
2250void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
2251 ASSERT(rmode != RelocInfo::NONE);
2252 // Don't record external references unless the heap will be serialized.
Steve Blockd0582a62009-12-15 09:54:21 +00002253 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
2254#ifdef DEBUG
2255 if (!Serializer::enabled()) {
2256 Serializer::TooLateToEnableNow();
2257 }
2258#endif
2259 if (!Serializer::enabled() && !FLAG_debug_code) {
2260 return;
2261 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002262 }
2263 RelocInfo rinfo(pc_, rmode, data);
2264 reloc_info_writer.Write(&rinfo);
2265}
2266
2267
2268#ifdef GENERATED_CODE_COVERAGE
2269static FILE* coverage_log = NULL;
2270
2271
2272static void InitCoverageLog() {
2273 char* file_name = getenv("V8_GENERATED_CODE_COVERAGE_LOG");
2274 if (file_name != NULL) {
2275 coverage_log = fopen(file_name, "aw+");
2276 }
2277}
2278
2279
2280void LogGeneratedCodeCoverage(const char* file_line) {
2281 const char* return_address = (&file_line)[-1];
2282 char* push_insn = const_cast<char*>(return_address - 12);
2283 push_insn[0] = 0xeb; // Relative branch insn.
2284 push_insn[1] = 13; // Skip over coverage insns.
2285 if (coverage_log != NULL) {
2286 fprintf(coverage_log, "%s\n", file_line);
2287 fflush(coverage_log);
2288 }
2289}
2290
2291#endif
2292
2293} } // namespace v8::internal