Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(915)

Side by Side Diff: src/x64/assembler-x64.cc

Issue 887013003: [x64] Assembler support for internal references and RIP relative addressing. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/x64/assembler-x64.h ('k') | src/x64/assembler-x64-inl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/x64/assembler-x64.h" 5 #include "src/x64/assembler-x64.h"
6 6
7 #include <cstring> 7 #include <cstring>
8 8
9 #if V8_TARGET_ARCH_X64 9 #if V8_TARGET_ARCH_X64
10 10
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 ScaleFactor scale, 212 ScaleFactor scale,
213 int32_t disp) : rex_(0) { 213 int32_t disp) : rex_(0) {
214 DCHECK(!index.is(rsp)); 214 DCHECK(!index.is(rsp));
215 len_ = 1; 215 len_ = 1;
216 set_modrm(0, rsp); 216 set_modrm(0, rsp);
217 set_sib(scale, index, rbp); 217 set_sib(scale, index, rbp);
218 set_disp32(disp); 218 set_disp32(disp);
219 } 219 }
220 220
221 221
222 Operand::Operand(Label* label) : rex_(0), len_(1) {
223 DCHECK_NOT_NULL(label);
224 set_modrm(0, rbp);
225 set_disp64(reinterpret_cast<intptr_t>(label));
226 }
227
228
222 Operand::Operand(const Operand& operand, int32_t offset) { 229 Operand::Operand(const Operand& operand, int32_t offset) {
223 DCHECK(operand.len_ >= 1); 230 DCHECK(operand.len_ >= 1);
224 // Operand encodes REX ModR/M [SIB] [Disp]. 231 // Operand encodes REX ModR/M [SIB] [Disp].
225 byte modrm = operand.buf_[0]; 232 byte modrm = operand.buf_[0];
226 DCHECK(modrm < 0xC0); // Disallow mode 3 (register target). 233 DCHECK(modrm < 0xC0); // Disallow mode 3 (register target).
227 bool has_sib = ((modrm & 0x07) == 0x04); 234 bool has_sib = ((modrm & 0x07) == 0x04);
228 byte mode = modrm & 0xC0; 235 byte mode = modrm & 0xC0;
229 int disp_offset = has_sib ? 2 : 1; 236 int disp_offset = has_sib ? 2 : 1;
230 int base_reg = (has_sib ? operand.buf_[1] : modrm) & 0x07; 237 int base_reg = (has_sib ? operand.buf_[1] : modrm) & 0x07;
231 // Mode 0 with rbp/r13 as ModR/M or SIB base register always has a 32-bit 238 // Mode 0 with rbp/r13 as ModR/M or SIB base register always has a 32-bit
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 } 365 }
359 366
360 367
361 void Assembler::bind_to(Label* L, int pos) { 368 void Assembler::bind_to(Label* L, int pos) {
362 DCHECK(!L->is_bound()); // Label may only be bound once. 369 DCHECK(!L->is_bound()); // Label may only be bound once.
363 DCHECK(0 <= pos && pos <= pc_offset()); // Position must be valid. 370 DCHECK(0 <= pos && pos <= pc_offset()); // Position must be valid.
364 if (L->is_linked()) { 371 if (L->is_linked()) {
365 int current = L->pos(); 372 int current = L->pos();
366 int next = long_at(current); 373 int next = long_at(current);
367 while (next != current) { 374 while (next != current) {
368 // Relative address, relative to point after address. 375 if (current >= 4 && long_at(current - 4) == 0) {
369 int imm32 = pos - (current + sizeof(int32_t)); 376 // Absolute address.
370 long_at_put(current, imm32); 377 intptr_t imm64 = reinterpret_cast<intptr_t>(buffer_ + pos);
378 *reinterpret_cast<intptr_t*>(addr_at(current - 4)) = imm64;
379 internal_reference_positions_.push_back(current - 4);
380 } else {
381 // Relative address, relative to point after address.
382 int imm32 = pos - (current + sizeof(int32_t));
383 long_at_put(current, imm32);
384 }
371 current = next; 385 current = next;
372 next = long_at(next); 386 next = long_at(next);
373 } 387 }
374 // Fix up last fixup on linked list. 388 // Fix up last fixup on linked list.
375 int last_imm32 = pos - (current + sizeof(int32_t)); 389 if (current >= 4 && long_at(current - 4) == 0) {
376 long_at_put(current, last_imm32); 390 // Absolute address.
391 intptr_t imm64 = reinterpret_cast<intptr_t>(buffer_ + pos);
392 *reinterpret_cast<intptr_t*>(addr_at(current - 4)) = imm64;
393 internal_reference_positions_.push_back(current - 4);
394 } else {
395 // Relative address, relative to point after address.
396 int imm32 = pos - (current + sizeof(int32_t));
397 long_at_put(current, imm32);
398 }
377 } 399 }
378 while (L->is_near_linked()) { 400 while (L->is_near_linked()) {
379 int fixup_pos = L->near_link_pos(); 401 int fixup_pos = L->near_link_pos();
380 int offset_to_next = 402 int offset_to_next =
381 static_cast<int>(*reinterpret_cast<int8_t*>(addr_at(fixup_pos))); 403 static_cast<int>(*reinterpret_cast<int8_t*>(addr_at(fixup_pos)));
382 DCHECK(offset_to_next <= 0); 404 DCHECK(offset_to_next <= 0);
383 int disp = pos - (fixup_pos + sizeof(int8_t)); 405 int disp = pos - (fixup_pos + sizeof(int8_t));
384 CHECK(is_int8(disp)); 406 CHECK(is_int8(disp));
385 set_byte_at(fixup_pos, disp); 407 set_byte_at(fixup_pos, disp);
386 if (offset_to_next < 0) { 408 if (offset_to_next < 0) {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 desc.reloc_size); 456 desc.reloc_size);
435 457
436 // Switch buffers. 458 // Switch buffers.
437 DeleteArray(buffer_); 459 DeleteArray(buffer_);
438 buffer_ = desc.buffer; 460 buffer_ = desc.buffer;
439 buffer_size_ = desc.buffer_size; 461 buffer_size_ = desc.buffer_size;
440 pc_ += pc_delta; 462 pc_ += pc_delta;
441 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta, 463 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
442 reloc_info_writer.last_pc() + pc_delta); 464 reloc_info_writer.last_pc() + pc_delta);
443 465
444 // Relocate runtime entries. 466 // Relocate internal references.
445 for (RelocIterator it(desc); !it.done(); it.next()) { 467 for (auto pos : internal_reference_positions_) {
446 RelocInfo::Mode rmode = it.rinfo()->rmode(); 468 intptr_t* p = reinterpret_cast<intptr_t*>(buffer_ + pos);
447 if (rmode == RelocInfo::INTERNAL_REFERENCE) { 469 *p += pc_delta;
448 intptr_t* p = reinterpret_cast<intptr_t*>(it.rinfo()->pc());
449 if (*p != 0) { // 0 means uninitialized.
450 *p += pc_delta;
451 }
452 }
453 } 470 }
454 471
455 DCHECK(!buffer_overflow()); 472 DCHECK(!buffer_overflow());
456 } 473 }
457 474
458 475
459 void Assembler::emit_operand(int code, const Operand& adr) { 476 void Assembler::emit_operand(int code, const Operand& adr) {
460 DCHECK(is_uint3(code)); 477 DCHECK(is_uint3(code));
461 const unsigned length = adr.len_; 478 const unsigned length = adr.len_;
462 DCHECK(length > 0); 479 DCHECK(length > 0);
463 480
464 // Emit updated ModR/M byte containing the given register. 481 // Emit updated ModR/M byte containing the given register.
465 DCHECK((adr.buf_[0] & 0x38) == 0); 482 DCHECK((adr.buf_[0] & 0x38) == 0);
466 pc_[0] = adr.buf_[0] | code << 3; 483 *pc_++ = adr.buf_[0] | code << 3;
467 484
468 // Emit the rest of the encoded operand. 485 // Recognize RIP relative addressing.
469 for (unsigned i = 1; i < length; i++) pc_[i] = adr.buf_[i]; 486 if (adr.buf_[0] == 5) {
470 pc_ += length; 487 DCHECK_EQ(9u, length);
488 Label* label = *reinterpret_cast<Label* const*>(&adr.buf_[1]);
489 if (label->is_bound()) {
490 int offset = label->pos() - pc_offset() - sizeof(int32_t);
491 DCHECK_GE(0, offset);
492 emitl(offset);
493 } else if (label->is_linked()) {
494 emitl(label->pos());
495 label->link_to(pc_offset() - sizeof(int32_t));
496 } else {
497 DCHECK(label->is_unused());
498 int32_t current = pc_offset();
499 emitl(current);
500 label->link_to(current);
501 }
502 } else {
503 // Emit the rest of the encoded operand.
504 for (unsigned i = 1; i < length; i++) *pc_++ = adr.buf_[i];
505 }
471 } 506 }
472 507
473 508
474 // Assembler Instruction implementations. 509 // Assembler Instruction implementations.
475 510
476 void Assembler::arithmetic_op(byte opcode, 511 void Assembler::arithmetic_op(byte opcode,
477 Register reg, 512 Register reg,
478 const Operand& op, 513 const Operand& op,
479 int size) { 514 int size) {
480 EnsureSpace ensure_space(this); 515 EnsureSpace ensure_space(this);
(...skipping 1327 matching lines...) Expand 10 before | Expand all | Expand 10 after
1808 if (imm16 == 0) { 1843 if (imm16 == 0) {
1809 emit(0xC3); 1844 emit(0xC3);
1810 } else { 1845 } else {
1811 emit(0xC2); 1846 emit(0xC2);
1812 emit(imm16 & 0xFF); 1847 emit(imm16 & 0xFF);
1813 emit((imm16 >> 8) & 0xFF); 1848 emit((imm16 >> 8) & 0xFF);
1814 } 1849 }
1815 } 1850 }
1816 1851
1817 1852
1853 void Assembler::ud2() {
1854 EnsureSpace ensure_space(this);
1855 emit(0x0F);
1856 emit(0x0B);
1857 }
1858
1859
1818 void Assembler::setcc(Condition cc, Register reg) { 1860 void Assembler::setcc(Condition cc, Register reg) {
1819 if (cc > last_condition) { 1861 if (cc > last_condition) {
1820 movb(reg, Immediate(cc == always ? 1 : 0)); 1862 movb(reg, Immediate(cc == always ? 1 : 0));
1821 return; 1863 return;
1822 } 1864 }
1823 EnsureSpace ensure_space(this); 1865 EnsureSpace ensure_space(this);
1824 DCHECK(is_uint4(cc)); 1866 DCHECK(is_uint4(cc));
1825 if (!reg.is_byte_register()) { 1867 if (!reg.is_byte_register()) {
1826 // Register is not one of al, bl, cl, dl. Its encoding needs REX. 1868 // Register is not one of al, bl, cl, dl. Its encoding needs REX.
1827 emit_rex_32(reg); 1869 emit_rex_32(reg);
(...skipping 1512 matching lines...) Expand 10 before | Expand all | Expand 10 after
3340 emit(data); 3382 emit(data);
3341 } 3383 }
3342 3384
3343 3385
3344 void Assembler::dd(uint32_t data) { 3386 void Assembler::dd(uint32_t data) {
3345 EnsureSpace ensure_space(this); 3387 EnsureSpace ensure_space(this);
3346 emitl(data); 3388 emitl(data);
3347 } 3389 }
3348 3390
3349 3391
3392 void Assembler::dq(Label* label) {
3393 EnsureSpace ensure_space(this);
3394 if (label->is_bound()) {
3395 internal_reference_positions_.push_back(pc_offset());
3396 emitp(buffer_ + label->pos(), RelocInfo::INTERNAL_REFERENCE);
3397 } else {
3398 RecordRelocInfo(RelocInfo::INTERNAL_REFERENCE);
3399 emitl(0); // Zero for the first 32bit marks it as 64bit absolute address.
3400 if (label->is_linked()) {
3401 emitl(label->pos());
3402 label->link_to(pc_offset() - sizeof(int32_t));
3403 } else {
3404 DCHECK(label->is_unused());
3405 int32_t current = pc_offset();
3406 emitl(current);
3407 label->link_to(current);
3408 }
3409 }
3410 }
3411
3412
3350 // Relocation information implementations. 3413 // Relocation information implementations.
3351 3414
3352 void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) { 3415 void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
3353 DCHECK(!RelocInfo::IsNone(rmode)); 3416 DCHECK(!RelocInfo::IsNone(rmode));
3354 // Don't record external references unless the heap will be serialized. 3417 // Don't record external references unless the heap will be serialized.
3355 if (rmode == RelocInfo::EXTERNAL_REFERENCE && 3418 if (rmode == RelocInfo::EXTERNAL_REFERENCE &&
3356 !serializer_enabled() && !emit_debug_code()) { 3419 !serializer_enabled() && !emit_debug_code()) {
3357 return; 3420 return;
3358 } else if (rmode == RelocInfo::CODE_AGE_SEQUENCE) { 3421 } else if (rmode == RelocInfo::CODE_AGE_SEQUENCE) {
3359 // Don't record psuedo relocation info for code age sequence mode. 3422 // Don't record psuedo relocation info for code age sequence mode.
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
3424 3487
3425 3488
3426 bool RelocInfo::IsInConstantPool() { 3489 bool RelocInfo::IsInConstantPool() {
3427 return false; 3490 return false;
3428 } 3491 }
3429 3492
3430 3493
3431 } } // namespace v8::internal 3494 } } // namespace v8::internal
3432 3495
3433 #endif // V8_TARGET_ARCH_X64 3496 #endif // V8_TARGET_ARCH_X64
OLDNEW
« no previous file with comments | « src/x64/assembler-x64.h ('k') | src/x64/assembler-x64-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698