Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_X64_ASSEMBLER_X64_INL_H_ |
| 6 | #define V8_X64_ASSEMBLER_X64_INL_H_ |
| 7 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "src/x64/assembler-x64.h" |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/base/cpu.h" |
| 11 | #include "src/debug.h" |
| 12 | #include "src/v8memory.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13 | |
| 14 | namespace v8 { |
| 15 | namespace internal { |
| 16 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17 | bool CpuFeatures::SupportsCrankshaft() { return true; } |
| 18 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19 | |
| 20 | // ----------------------------------------------------------------------------- |
| 21 | // Implementation of Assembler |
| 22 | |
| 23 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 24 | static const byte kCallOpcode = 0xE8; |
| 25 | // The length of pushq(rbp), movp(rbp, rsp), Push(rsi) and Push(rdi). |
| 26 | static const int kNoCodeAgeSequenceLength = kPointerSize == kInt64Size ? 6 : 17; |
| 27 | |
| 28 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 29 | void Assembler::emitl(uint32_t x) { |
| 30 | Memory::uint32_at(pc_) = x; |
| 31 | pc_ += sizeof(uint32_t); |
| 32 | } |
| 33 | |
| 34 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 35 | void Assembler::emitp(void* x, RelocInfo::Mode rmode) { |
| 36 | uintptr_t value = reinterpret_cast<uintptr_t>(x); |
| 37 | Memory::uintptr_at(pc_) = value; |
| 38 | if (!RelocInfo::IsNone(rmode)) { |
| 39 | RecordRelocInfo(rmode, value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 41 | pc_ += sizeof(uintptr_t); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | void Assembler::emitq(uint64_t x) { |
| 46 | Memory::uint64_at(pc_) = x; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 47 | pc_ += sizeof(uint64_t); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | void Assembler::emitw(uint16_t x) { |
| 52 | Memory::uint16_at(pc_) = x; |
| 53 | pc_ += sizeof(uint16_t); |
| 54 | } |
| 55 | |
| 56 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 57 | void Assembler::emit_code_target(Handle<Code> target, |
| 58 | RelocInfo::Mode rmode, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 59 | TypeFeedbackId ast_id) { |
| 60 | DCHECK(RelocInfo::IsCodeTarget(rmode) || |
| 61 | rmode == RelocInfo::CODE_AGE_SEQUENCE); |
| 62 | if (rmode == RelocInfo::CODE_TARGET && !ast_id.IsNone()) { |
| 63 | RecordRelocInfo(RelocInfo::CODE_TARGET_WITH_ID, ast_id.ToInt()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 64 | } else { |
| 65 | RecordRelocInfo(rmode); |
| 66 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 67 | int current = code_targets_.length(); |
| 68 | if (current > 0 && code_targets_.last().is_identical_to(target)) { |
| 69 | // Optimization if we keep jumping to the same code target. |
| 70 | emitl(current - 1); |
| 71 | } else { |
| 72 | code_targets_.Add(target); |
| 73 | emitl(current); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 78 | void Assembler::emit_runtime_entry(Address entry, RelocInfo::Mode rmode) { |
| 79 | DCHECK(RelocInfo::IsRuntimeEntry(rmode)); |
| 80 | RecordRelocInfo(rmode); |
| 81 | emitl(static_cast<uint32_t>(entry - isolate()->code_range()->start())); |
| 82 | } |
| 83 | |
| 84 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | void Assembler::emit_rex_64(Register reg, Register rm_reg) { |
| 86 | emit(0x48 | reg.high_bit() << 2 | rm_reg.high_bit()); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | void Assembler::emit_rex_64(XMMRegister reg, Register rm_reg) { |
| 91 | emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3); |
| 92 | } |
| 93 | |
| 94 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 95 | void Assembler::emit_rex_64(Register reg, XMMRegister rm_reg) { |
| 96 | emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3); |
| 97 | } |
| 98 | |
| 99 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 100 | void Assembler::emit_rex_64(Register reg, const Operand& op) { |
| 101 | emit(0x48 | reg.high_bit() << 2 | op.rex_); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void Assembler::emit_rex_64(XMMRegister reg, const Operand& op) { |
| 106 | emit(0x48 | (reg.code() & 0x8) >> 1 | op.rex_); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | void Assembler::emit_rex_64(Register rm_reg) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 111 | DCHECK_EQ(rm_reg.code() & 0xf, rm_reg.code()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | emit(0x48 | rm_reg.high_bit()); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | void Assembler::emit_rex_64(const Operand& op) { |
| 117 | emit(0x48 | op.rex_); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | void Assembler::emit_rex_32(Register reg, Register rm_reg) { |
| 122 | emit(0x40 | reg.high_bit() << 2 | rm_reg.high_bit()); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | void Assembler::emit_rex_32(Register reg, const Operand& op) { |
| 127 | emit(0x40 | reg.high_bit() << 2 | op.rex_); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | void Assembler::emit_rex_32(Register rm_reg) { |
| 132 | emit(0x40 | rm_reg.high_bit()); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | void Assembler::emit_rex_32(const Operand& op) { |
| 137 | emit(0x40 | op.rex_); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | void Assembler::emit_optional_rex_32(Register reg, Register rm_reg) { |
| 142 | byte rex_bits = reg.high_bit() << 2 | rm_reg.high_bit(); |
| 143 | if (rex_bits != 0) emit(0x40 | rex_bits); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void Assembler::emit_optional_rex_32(Register reg, const Operand& op) { |
| 148 | byte rex_bits = reg.high_bit() << 2 | op.rex_; |
| 149 | if (rex_bits != 0) emit(0x40 | rex_bits); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | void Assembler::emit_optional_rex_32(XMMRegister reg, const Operand& op) { |
| 154 | byte rex_bits = (reg.code() & 0x8) >> 1 | op.rex_; |
| 155 | if (rex_bits != 0) emit(0x40 | rex_bits); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | void Assembler::emit_optional_rex_32(XMMRegister reg, XMMRegister base) { |
| 160 | byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3; |
| 161 | if (rex_bits != 0) emit(0x40 | rex_bits); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | void Assembler::emit_optional_rex_32(XMMRegister reg, Register base) { |
| 166 | byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3; |
| 167 | if (rex_bits != 0) emit(0x40 | rex_bits); |
| 168 | } |
| 169 | |
| 170 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 171 | void Assembler::emit_optional_rex_32(Register reg, XMMRegister base) { |
| 172 | byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3; |
| 173 | if (rex_bits != 0) emit(0x40 | rex_bits); |
| 174 | } |
| 175 | |
| 176 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | void Assembler::emit_optional_rex_32(Register rm_reg) { |
| 178 | if (rm_reg.high_bit()) emit(0x41); |
| 179 | } |
| 180 | |
| 181 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 182 | void Assembler::emit_optional_rex_32(XMMRegister rm_reg) { |
| 183 | if (rm_reg.high_bit()) emit(0x41); |
| 184 | } |
| 185 | |
| 186 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | void Assembler::emit_optional_rex_32(const Operand& op) { |
| 188 | if (op.rex_ != 0) emit(0x40 | op.rex_); |
| 189 | } |
| 190 | |
| 191 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 192 | // byte 1 of 3-byte VEX |
| 193 | void Assembler::emit_vex3_byte1(XMMRegister reg, XMMRegister rm, |
| 194 | LeadingOpcode m) { |
| 195 | byte rxb = ~((reg.high_bit() << 2) | rm.high_bit()) << 5; |
| 196 | emit(rxb | m); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | // byte 1 of 3-byte VEX |
| 201 | void Assembler::emit_vex3_byte1(XMMRegister reg, const Operand& rm, |
| 202 | LeadingOpcode m) { |
| 203 | byte rxb = ~((reg.high_bit() << 2) | rm.rex_) << 5; |
| 204 | emit(rxb | m); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | // byte 1 of 2-byte VEX |
| 209 | void Assembler::emit_vex2_byte1(XMMRegister reg, XMMRegister v, VectorLength l, |
| 210 | SIMDPrefix pp) { |
| 211 | byte rv = ~((reg.high_bit() << 4) | v.code()) << 3; |
| 212 | emit(rv | l | pp); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | // byte 2 of 3-byte VEX |
| 217 | void Assembler::emit_vex3_byte2(VexW w, XMMRegister v, VectorLength l, |
| 218 | SIMDPrefix pp) { |
| 219 | emit(w | ((~v.code() & 0xf) << 3) | l | pp); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | void Assembler::emit_vex_prefix(XMMRegister reg, XMMRegister vreg, |
| 224 | XMMRegister rm, VectorLength l, SIMDPrefix pp, |
| 225 | LeadingOpcode mm, VexW w) { |
| 226 | if (rm.high_bit() || mm != k0F || w != kW0) { |
| 227 | emit_vex3_byte0(); |
| 228 | emit_vex3_byte1(reg, rm, mm); |
| 229 | emit_vex3_byte2(w, vreg, l, pp); |
| 230 | } else { |
| 231 | emit_vex2_byte0(); |
| 232 | emit_vex2_byte1(reg, vreg, l, pp); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
| 237 | void Assembler::emit_vex_prefix(XMMRegister reg, XMMRegister vreg, |
| 238 | const Operand& rm, VectorLength l, |
| 239 | SIMDPrefix pp, LeadingOpcode mm, VexW w) { |
| 240 | if (rm.rex_ || mm != k0F || w != kW0) { |
| 241 | emit_vex3_byte0(); |
| 242 | emit_vex3_byte1(reg, rm, mm); |
| 243 | emit_vex3_byte2(w, vreg, l, pp); |
| 244 | } else { |
| 245 | emit_vex2_byte0(); |
| 246 | emit_vex2_byte1(reg, vreg, l, pp); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 251 | Address Assembler::target_address_at(Address pc, |
| 252 | ConstantPoolArray* constant_pool) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 253 | return Memory::int32_at(pc) + pc + 4; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 257 | void Assembler::set_target_address_at(Address pc, |
| 258 | ConstantPoolArray* constant_pool, |
| 259 | Address target, |
| 260 | ICacheFlushMode icache_flush_mode) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 261 | Memory::int32_at(pc) = static_cast<int32_t>(target - pc - 4); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 262 | if (icache_flush_mode != SKIP_ICACHE_FLUSH) { |
| 263 | CpuFeatures::FlushICache(pc, sizeof(int32_t)); |
| 264 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 267 | |
| 268 | Address Assembler::target_address_from_return_address(Address pc) { |
| 269 | return pc - kCallTargetAddressOffset; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | Address Assembler::break_address_from_return_address(Address pc) { |
| 274 | return pc - Assembler::kPatchDebugBreakSlotReturnOffset; |
| 275 | } |
| 276 | |
| 277 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 278 | Handle<Object> Assembler::code_target_object_handle_at(Address pc) { |
| 279 | return code_targets_[Memory::int32_at(pc)]; |
| 280 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 281 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 282 | |
| 283 | Address Assembler::runtime_entry_at(Address pc) { |
| 284 | return Memory::int32_at(pc) + isolate()->code_range()->start(); |
| 285 | } |
| 286 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 287 | // ----------------------------------------------------------------------------- |
| 288 | // Implementation of RelocInfo |
| 289 | |
| 290 | // The modes possibly affected by apply must be in kApplyMask. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 291 | void RelocInfo::apply(intptr_t delta, ICacheFlushMode icache_flush_mode) { |
| 292 | bool flush_icache = icache_flush_mode != SKIP_ICACHE_FLUSH; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 293 | if (IsInternalReference(rmode_)) { |
| 294 | // absolute code pointer inside code object moves with the code object. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 295 | Memory::Address_at(pc_) += static_cast<int32_t>(delta); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 296 | if (flush_icache) CpuFeatures::FlushICache(pc_, sizeof(Address)); |
| 297 | } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 298 | Memory::int32_at(pc_) -= static_cast<int32_t>(delta); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 299 | if (flush_icache) CpuFeatures::FlushICache(pc_, sizeof(int32_t)); |
| 300 | } else if (rmode_ == CODE_AGE_SEQUENCE) { |
| 301 | if (*pc_ == kCallOpcode) { |
| 302 | int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1); |
| 303 | *p -= static_cast<int32_t>(delta); // Relocate entry. |
| 304 | if (flush_icache) CpuFeatures::FlushICache(p, sizeof(uint32_t)); |
| 305 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | |
| 310 | Address RelocInfo::target_address() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 311 | DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)); |
| 312 | return Assembler::target_address_at(pc_, host_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | |
| 316 | Address RelocInfo::target_address_address() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 317 | DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_) |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 318 | || rmode_ == EMBEDDED_OBJECT |
| 319 | || rmode_ == EXTERNAL_REFERENCE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 320 | return reinterpret_cast<Address>(pc_); |
| 321 | } |
| 322 | |
| 323 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 324 | Address RelocInfo::constant_pool_entry_address() { |
| 325 | UNREACHABLE(); |
| 326 | return NULL; |
| 327 | } |
| 328 | |
| 329 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 330 | int RelocInfo::target_address_size() { |
| 331 | if (IsCodedSpecially()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 332 | return Assembler::kSpecialTargetSize; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 333 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 334 | return kPointerSize; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 339 | void RelocInfo::set_target_address(Address target, |
| 340 | WriteBarrierMode write_barrier_mode, |
| 341 | ICacheFlushMode icache_flush_mode) { |
| 342 | DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)); |
| 343 | Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode); |
| 344 | if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL && |
| 345 | IsCodeTarget(rmode_)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 346 | Object* target_code = Code::GetCodeFromTargetAddress(target); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 347 | host()->GetHeap()->incremental_marking()->RecordWriteIntoCode( |
| 348 | host(), this, HeapObject::cast(target_code)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 349 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
| 353 | Object* RelocInfo::target_object() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 354 | DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 355 | return Memory::Object_at(pc_); |
| 356 | } |
| 357 | |
| 358 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 359 | Handle<Object> RelocInfo::target_object_handle(Assembler* origin) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 360 | DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 361 | if (rmode_ == EMBEDDED_OBJECT) { |
| 362 | return Memory::Object_Handle_at(pc_); |
| 363 | } else { |
| 364 | return origin->code_target_object_handle_at(pc_); |
| 365 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 369 | Address RelocInfo::target_reference() { |
| 370 | DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE); |
| 371 | return Memory::Address_at(pc_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 375 | void RelocInfo::set_target_object(Object* target, |
| 376 | WriteBarrierMode write_barrier_mode, |
| 377 | ICacheFlushMode icache_flush_mode) { |
| 378 | DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 379 | Memory::Object_at(pc_) = target; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 380 | if (icache_flush_mode != SKIP_ICACHE_FLUSH) { |
| 381 | CpuFeatures::FlushICache(pc_, sizeof(Address)); |
| 382 | } |
| 383 | if (write_barrier_mode == UPDATE_WRITE_BARRIER && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 384 | host() != NULL && |
| 385 | target->IsHeapObject()) { |
| 386 | host()->GetHeap()->incremental_marking()->RecordWrite( |
| 387 | host(), &Memory::Object_at(pc_), HeapObject::cast(target)); |
| 388 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 392 | Address RelocInfo::target_runtime_entry(Assembler* origin) { |
| 393 | DCHECK(IsRuntimeEntry(rmode_)); |
| 394 | return origin->runtime_entry_at(pc_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 398 | void RelocInfo::set_target_runtime_entry(Address target, |
| 399 | WriteBarrierMode write_barrier_mode, |
| 400 | ICacheFlushMode icache_flush_mode) { |
| 401 | DCHECK(IsRuntimeEntry(rmode_)); |
| 402 | if (target_address() != target) { |
| 403 | set_target_address(target, write_barrier_mode, icache_flush_mode); |
| 404 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 408 | Handle<Cell> RelocInfo::target_cell_handle() { |
| 409 | DCHECK(rmode_ == RelocInfo::CELL); |
| 410 | Address address = Memory::Address_at(pc_); |
| 411 | return Handle<Cell>(reinterpret_cast<Cell**>(address)); |
| 412 | } |
| 413 | |
| 414 | |
| 415 | Cell* RelocInfo::target_cell() { |
| 416 | DCHECK(rmode_ == RelocInfo::CELL); |
| 417 | return Cell::FromValueAddress(Memory::Address_at(pc_)); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | void RelocInfo::set_target_cell(Cell* cell, |
| 422 | WriteBarrierMode write_barrier_mode, |
| 423 | ICacheFlushMode icache_flush_mode) { |
| 424 | DCHECK(rmode_ == RelocInfo::CELL); |
| 425 | Address address = cell->address() + Cell::kValueOffset; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 426 | Memory::Address_at(pc_) = address; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 427 | if (icache_flush_mode != SKIP_ICACHE_FLUSH) { |
| 428 | CpuFeatures::FlushICache(pc_, sizeof(Address)); |
| 429 | } |
| 430 | if (write_barrier_mode == UPDATE_WRITE_BARRIER && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 431 | host() != NULL) { |
| 432 | // TODO(1550) We are passing NULL as a slot because cell can never be on |
| 433 | // evacuation candidate. |
| 434 | host()->GetHeap()->incremental_marking()->RecordWrite( |
| 435 | host(), NULL, cell); |
| 436 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 440 | void RelocInfo::WipeOut() { |
| 441 | if (IsEmbeddedObject(rmode_) || IsExternalReference(rmode_)) { |
| 442 | Memory::Address_at(pc_) = NULL; |
| 443 | } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) { |
| 444 | // Effectively write zero into the relocation. |
| 445 | Assembler::set_target_address_at(pc_, host_, pc_ + sizeof(int32_t)); |
| 446 | } else { |
| 447 | UNREACHABLE(); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 452 | bool RelocInfo::IsPatchedReturnSequence() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 453 | // The recognized call sequence is: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 454 | // movq(kScratchRegister, address); call(kScratchRegister); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 455 | // It only needs to be distinguished from a return sequence |
| 456 | // movq(rsp, rbp); pop(rbp); ret(n); int3 *6 |
| 457 | // The 11th byte is int3 (0xCC) in the return sequence and |
| 458 | // REX.WB (0x48+register bit) for the call sequence. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 459 | return pc_[Assembler::kMoveAddressIntoScratchRegisterInstructionLength] != |
| 460 | 0xCC; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 464 | bool RelocInfo::IsPatchedDebugBreakSlotSequence() { |
| 465 | return !Assembler::IsNop(pc()); |
| 466 | } |
| 467 | |
| 468 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 469 | Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) { |
| 470 | DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE); |
| 471 | DCHECK(*pc_ == kCallOpcode); |
| 472 | return origin->code_target_object_handle_at(pc_ + 1); |
| 473 | } |
| 474 | |
| 475 | |
| 476 | Code* RelocInfo::code_age_stub() { |
| 477 | DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE); |
| 478 | DCHECK(*pc_ == kCallOpcode); |
| 479 | return Code::GetCodeFromTargetAddress( |
| 480 | Assembler::target_address_at(pc_ + 1, host_)); |
| 481 | } |
| 482 | |
| 483 | |
| 484 | void RelocInfo::set_code_age_stub(Code* stub, |
| 485 | ICacheFlushMode icache_flush_mode) { |
| 486 | DCHECK(*pc_ == kCallOpcode); |
| 487 | DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE); |
| 488 | Assembler::set_target_address_at(pc_ + 1, host_, stub->instruction_start(), |
| 489 | icache_flush_mode); |
| 490 | } |
| 491 | |
| 492 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 493 | Address RelocInfo::call_address() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 494 | DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) || |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 495 | (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence())); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 496 | return Memory::Address_at( |
| 497 | pc_ + Assembler::kRealPatchReturnSequenceAddressOffset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | |
| 501 | void RelocInfo::set_call_address(Address target) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 502 | DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) || |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 503 | (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence())); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 504 | Memory::Address_at(pc_ + Assembler::kRealPatchReturnSequenceAddressOffset) = |
| 505 | target; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 506 | CpuFeatures::FlushICache( |
| 507 | pc_ + Assembler::kRealPatchReturnSequenceAddressOffset, sizeof(Address)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 508 | if (host() != NULL) { |
| 509 | Object* target_code = Code::GetCodeFromTargetAddress(target); |
| 510 | host()->GetHeap()->incremental_marking()->RecordWriteIntoCode( |
| 511 | host(), this, HeapObject::cast(target_code)); |
| 512 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | |
| 516 | Object* RelocInfo::call_object() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 517 | return *call_object_address(); |
| 518 | } |
| 519 | |
| 520 | |
| 521 | void RelocInfo::set_call_object(Object* target) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 522 | *call_object_address() = target; |
| 523 | } |
| 524 | |
| 525 | |
| 526 | Object** RelocInfo::call_object_address() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 527 | DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) || |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 528 | (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 529 | return reinterpret_cast<Object**>( |
| 530 | pc_ + Assembler::kPatchReturnSequenceAddressOffset); |
| 531 | } |
| 532 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 533 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 534 | void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 535 | RelocInfo::Mode mode = rmode(); |
| 536 | if (mode == RelocInfo::EMBEDDED_OBJECT) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 537 | visitor->VisitEmbeddedPointer(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 538 | CpuFeatures::FlushICache(pc_, sizeof(Address)); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 539 | } else if (RelocInfo::IsCodeTarget(mode)) { |
| 540 | visitor->VisitCodeTarget(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 541 | } else if (mode == RelocInfo::CELL) { |
| 542 | visitor->VisitCell(this); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 543 | } else if (mode == RelocInfo::EXTERNAL_REFERENCE) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 544 | visitor->VisitExternalReference(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 545 | CpuFeatures::FlushICache(pc_, sizeof(Address)); |
| 546 | } else if (RelocInfo::IsCodeAgeSequence(mode)) { |
| 547 | visitor->VisitCodeAgeSequence(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 548 | } else if (((RelocInfo::IsJSReturn(mode) && |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 549 | IsPatchedReturnSequence()) || |
| 550 | (RelocInfo::IsDebugBreakSlot(mode) && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 551 | IsPatchedDebugBreakSlotSequence())) && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 552 | isolate->debug()->has_break_points()) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 553 | visitor->VisitDebugTarget(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 554 | } else if (RelocInfo::IsRuntimeEntry(mode)) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 555 | visitor->VisitRuntimeEntry(this); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 560 | template<typename StaticVisitor> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 561 | void RelocInfo::Visit(Heap* heap) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 562 | RelocInfo::Mode mode = rmode(); |
| 563 | if (mode == RelocInfo::EMBEDDED_OBJECT) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 564 | StaticVisitor::VisitEmbeddedPointer(heap, this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 565 | CpuFeatures::FlushICache(pc_, sizeof(Address)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 566 | } else if (RelocInfo::IsCodeTarget(mode)) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 567 | StaticVisitor::VisitCodeTarget(heap, this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 568 | } else if (mode == RelocInfo::CELL) { |
| 569 | StaticVisitor::VisitCell(heap, this); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 570 | } else if (mode == RelocInfo::EXTERNAL_REFERENCE) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 571 | StaticVisitor::VisitExternalReference(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 572 | CpuFeatures::FlushICache(pc_, sizeof(Address)); |
| 573 | } else if (RelocInfo::IsCodeAgeSequence(mode)) { |
| 574 | StaticVisitor::VisitCodeAgeSequence(heap, this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 575 | } else if (heap->isolate()->debug()->has_break_points() && |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 576 | ((RelocInfo::IsJSReturn(mode) && |
| 577 | IsPatchedReturnSequence()) || |
| 578 | (RelocInfo::IsDebugBreakSlot(mode) && |
| 579 | IsPatchedDebugBreakSlotSequence()))) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 580 | StaticVisitor::VisitDebugTarget(heap, this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 581 | } else if (RelocInfo::IsRuntimeEntry(mode)) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 582 | StaticVisitor::VisitRuntimeEntry(this); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 587 | // ----------------------------------------------------------------------------- |
| 588 | // Implementation of Operand |
| 589 | |
| 590 | void Operand::set_modrm(int mod, Register rm_reg) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 591 | DCHECK(is_uint2(mod)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 592 | buf_[0] = mod << 6 | rm_reg.low_bits(); |
| 593 | // Set REX.B to the high bit of rm.code(). |
| 594 | rex_ |= rm_reg.high_bit(); |
| 595 | } |
| 596 | |
| 597 | |
| 598 | void Operand::set_sib(ScaleFactor scale, Register index, Register base) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 599 | DCHECK(len_ == 1); |
| 600 | DCHECK(is_uint2(scale)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 601 | // Use SIB with no index register only for base rsp or r12. Otherwise we |
| 602 | // would skip the SIB byte entirely. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 603 | DCHECK(!index.is(rsp) || base.is(rsp) || base.is(r12)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 604 | buf_[1] = (scale << 6) | (index.low_bits() << 3) | base.low_bits(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 605 | rex_ |= index.high_bit() << 1 | base.high_bit(); |
| 606 | len_ = 2; |
| 607 | } |
| 608 | |
| 609 | void Operand::set_disp8(int disp) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 610 | DCHECK(is_int8(disp)); |
| 611 | DCHECK(len_ == 1 || len_ == 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 612 | int8_t* p = reinterpret_cast<int8_t*>(&buf_[len_]); |
| 613 | *p = disp; |
| 614 | len_ += sizeof(int8_t); |
| 615 | } |
| 616 | |
| 617 | void Operand::set_disp32(int disp) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 618 | DCHECK(len_ == 1 || len_ == 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 619 | int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]); |
| 620 | *p = disp; |
| 621 | len_ += sizeof(int32_t); |
| 622 | } |
| 623 | |
| 624 | |
| 625 | } } // namespace v8::internal |
| 626 | |
| 627 | #endif // V8_X64_ASSEMBLER_X64_INL_H_ |