assembler-ia32-inl.h revision 1e0659c275bb392c045087af4f6b0d7565cb3d77
1// 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 are 6// 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 distribution. 14// 15// - Neither the name of Sun Microsystems or the names of contributors may 16// be used to endorse or promote products derived from this software without 17// specific prior written permission. 18// 19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31// The original source code covered by the above license above has been 32// modified significantly by Google Inc. 33// Copyright 2006-2008 the V8 project authors. All rights reserved. 34 35// A light-weight IA32 Assembler. 36 37#ifndef V8_IA32_ASSEMBLER_IA32_INL_H_ 38#define V8_IA32_ASSEMBLER_IA32_INL_H_ 39 40#include "cpu.h" 41#include "debug.h" 42 43namespace v8 { 44namespace internal { 45 46 47// The modes possibly affected by apply must be in kApplyMask. 48void RelocInfo::apply(intptr_t delta) { 49 if (rmode_ == RUNTIME_ENTRY || IsCodeTarget(rmode_)) { 50 int32_t* p = reinterpret_cast<int32_t*>(pc_); 51 *p -= delta; // Relocate entry. 52 CPU::FlushICache(p, sizeof(uint32_t)); 53 } else if (rmode_ == JS_RETURN && IsPatchedReturnSequence()) { 54 // Special handling of js_return when a break point is set (call 55 // instruction has been inserted). 56 int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1); 57 *p -= delta; // Relocate entry. 58 CPU::FlushICache(p, sizeof(uint32_t)); 59 } else if (rmode_ == DEBUG_BREAK_SLOT && IsPatchedDebugBreakSlotSequence()) { 60 // Special handling of a debug break slot when a break point is set (call 61 // instruction has been inserted). 62 int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1); 63 *p -= delta; // Relocate entry. 64 CPU::FlushICache(p, sizeof(uint32_t)); 65 } else if (IsInternalReference(rmode_)) { 66 // absolute code pointer inside code object moves with the code object. 67 int32_t* p = reinterpret_cast<int32_t*>(pc_); 68 *p += delta; // Relocate entry. 69 CPU::FlushICache(p, sizeof(uint32_t)); 70 } 71} 72 73 74Address RelocInfo::target_address() { 75 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY); 76 return Assembler::target_address_at(pc_); 77} 78 79 80Address RelocInfo::target_address_address() { 81 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY); 82 return reinterpret_cast<Address>(pc_); 83} 84 85 86int RelocInfo::target_address_size() { 87 return Assembler::kExternalTargetSize; 88} 89 90 91void RelocInfo::set_target_address(Address target) { 92 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY); 93 Assembler::set_target_address_at(pc_, target); 94} 95 96 97Object* RelocInfo::target_object() { 98 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); 99 return Memory::Object_at(pc_); 100} 101 102 103Handle<Object> RelocInfo::target_object_handle(Assembler* origin) { 104 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); 105 return Memory::Object_Handle_at(pc_); 106} 107 108 109Object** RelocInfo::target_object_address() { 110 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); 111 return &Memory::Object_at(pc_); 112} 113 114 115void RelocInfo::set_target_object(Object* target) { 116 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); 117 Memory::Object_at(pc_) = target; 118 CPU::FlushICache(pc_, sizeof(Address)); 119} 120 121 122Address* RelocInfo::target_reference_address() { 123 ASSERT(rmode_ == RelocInfo::EXTERNAL_REFERENCE); 124 return reinterpret_cast<Address*>(pc_); 125} 126 127 128Handle<JSGlobalPropertyCell> RelocInfo::target_cell_handle() { 129 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL); 130 Address address = Memory::Address_at(pc_); 131 return Handle<JSGlobalPropertyCell>( 132 reinterpret_cast<JSGlobalPropertyCell**>(address)); 133} 134 135 136JSGlobalPropertyCell* RelocInfo::target_cell() { 137 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL); 138 Address address = Memory::Address_at(pc_); 139 Object* object = HeapObject::FromAddress( 140 address - JSGlobalPropertyCell::kValueOffset); 141 return reinterpret_cast<JSGlobalPropertyCell*>(object); 142} 143 144 145void RelocInfo::set_target_cell(JSGlobalPropertyCell* cell) { 146 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL); 147 Address address = cell->address() + JSGlobalPropertyCell::kValueOffset; 148 Memory::Address_at(pc_) = address; 149 CPU::FlushICache(pc_, sizeof(Address)); 150} 151 152 153Address RelocInfo::call_address() { 154 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) || 155 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence())); 156 return Assembler::target_address_at(pc_ + 1); 157} 158 159 160void RelocInfo::set_call_address(Address target) { 161 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) || 162 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence())); 163 Assembler::set_target_address_at(pc_ + 1, target); 164} 165 166 167Object* RelocInfo::call_object() { 168 return *call_object_address(); 169} 170 171 172void RelocInfo::set_call_object(Object* target) { 173 *call_object_address() = target; 174} 175 176 177Object** RelocInfo::call_object_address() { 178 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) || 179 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence())); 180 return reinterpret_cast<Object**>(pc_ + 1); 181} 182 183 184bool RelocInfo::IsPatchedReturnSequence() { 185 return *pc_ == 0xE8; 186} 187 188 189bool RelocInfo::IsPatchedDebugBreakSlotSequence() { 190 return !Assembler::IsNop(pc()); 191} 192 193 194void RelocInfo::Visit(ObjectVisitor* visitor) { 195 RelocInfo::Mode mode = rmode(); 196 if (mode == RelocInfo::EMBEDDED_OBJECT) { 197 visitor->VisitPointer(target_object_address()); 198 CPU::FlushICache(pc_, sizeof(Address)); 199 } else if (RelocInfo::IsCodeTarget(mode)) { 200 visitor->VisitCodeTarget(this); 201 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) { 202 visitor->VisitGlobalPropertyCell(this); 203 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) { 204 visitor->VisitExternalReference(target_reference_address()); 205 CPU::FlushICache(pc_, sizeof(Address)); 206#ifdef ENABLE_DEBUGGER_SUPPORT 207 } else if (Debug::has_break_points() && 208 ((RelocInfo::IsJSReturn(mode) && 209 IsPatchedReturnSequence()) || 210 (RelocInfo::IsDebugBreakSlot(mode) && 211 IsPatchedDebugBreakSlotSequence()))) { 212 visitor->VisitDebugTarget(this); 213#endif 214 } else if (mode == RelocInfo::RUNTIME_ENTRY) { 215 visitor->VisitRuntimeEntry(this); 216 } 217} 218 219 220template<typename StaticVisitor> 221void RelocInfo::Visit() { 222 RelocInfo::Mode mode = rmode(); 223 if (mode == RelocInfo::EMBEDDED_OBJECT) { 224 StaticVisitor::VisitPointer(target_object_address()); 225 CPU::FlushICache(pc_, sizeof(Address)); 226 } else if (RelocInfo::IsCodeTarget(mode)) { 227 StaticVisitor::VisitCodeTarget(this); 228 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) { 229 StaticVisitor::VisitGlobalPropertyCell(this); 230 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) { 231 StaticVisitor::VisitExternalReference(target_reference_address()); 232 CPU::FlushICache(pc_, sizeof(Address)); 233#ifdef ENABLE_DEBUGGER_SUPPORT 234 } else if (Debug::has_break_points() && 235 ((RelocInfo::IsJSReturn(mode) && 236 IsPatchedReturnSequence()) || 237 (RelocInfo::IsDebugBreakSlot(mode) && 238 IsPatchedDebugBreakSlotSequence()))) { 239 StaticVisitor::VisitDebugTarget(this); 240#endif 241 } else if (mode == RelocInfo::RUNTIME_ENTRY) { 242 StaticVisitor::VisitRuntimeEntry(this); 243 } 244} 245 246 247 248Immediate::Immediate(int x) { 249 x_ = x; 250 rmode_ = RelocInfo::NONE; 251} 252 253 254Immediate::Immediate(const ExternalReference& ext) { 255 x_ = reinterpret_cast<int32_t>(ext.address()); 256 rmode_ = RelocInfo::EXTERNAL_REFERENCE; 257} 258 259 260Immediate::Immediate(Label* internal_offset) { 261 x_ = reinterpret_cast<int32_t>(internal_offset); 262 rmode_ = RelocInfo::INTERNAL_REFERENCE; 263} 264 265 266Immediate::Immediate(Handle<Object> handle) { 267 // Verify all Objects referred by code are NOT in new space. 268 Object* obj = *handle; 269 ASSERT(!Heap::InNewSpace(obj)); 270 if (obj->IsHeapObject()) { 271 x_ = reinterpret_cast<intptr_t>(handle.location()); 272 rmode_ = RelocInfo::EMBEDDED_OBJECT; 273 } else { 274 // no relocation needed 275 x_ = reinterpret_cast<intptr_t>(obj); 276 rmode_ = RelocInfo::NONE; 277 } 278} 279 280 281Immediate::Immediate(Smi* value) { 282 x_ = reinterpret_cast<intptr_t>(value); 283 rmode_ = RelocInfo::NONE; 284} 285 286 287Immediate::Immediate(Address addr) { 288 x_ = reinterpret_cast<int32_t>(addr); 289 rmode_ = RelocInfo::NONE; 290} 291 292 293void Assembler::emit(uint32_t x) { 294 *reinterpret_cast<uint32_t*>(pc_) = x; 295 pc_ += sizeof(uint32_t); 296} 297 298 299void Assembler::emit(Handle<Object> handle) { 300 // Verify all Objects referred by code are NOT in new space. 301 Object* obj = *handle; 302 ASSERT(!Heap::InNewSpace(obj)); 303 if (obj->IsHeapObject()) { 304 emit(reinterpret_cast<intptr_t>(handle.location()), 305 RelocInfo::EMBEDDED_OBJECT); 306 } else { 307 // no relocation needed 308 emit(reinterpret_cast<intptr_t>(obj)); 309 } 310} 311 312 313void Assembler::emit(uint32_t x, RelocInfo::Mode rmode) { 314 if (rmode != RelocInfo::NONE) RecordRelocInfo(rmode); 315 emit(x); 316} 317 318 319void Assembler::emit(const Immediate& x) { 320 if (x.rmode_ == RelocInfo::INTERNAL_REFERENCE) { 321 Label* label = reinterpret_cast<Label*>(x.x_); 322 emit_code_relative_offset(label); 323 return; 324 } 325 if (x.rmode_ != RelocInfo::NONE) RecordRelocInfo(x.rmode_); 326 emit(x.x_); 327} 328 329 330void Assembler::emit_code_relative_offset(Label* label) { 331 if (label->is_bound()) { 332 int32_t pos; 333 pos = label->pos() + Code::kHeaderSize - kHeapObjectTag; 334 emit(pos); 335 } else { 336 emit_disp(label, Displacement::CODE_RELATIVE); 337 } 338} 339 340 341void Assembler::emit_w(const Immediate& x) { 342 ASSERT(x.rmode_ == RelocInfo::NONE); 343 uint16_t value = static_cast<uint16_t>(x.x_); 344 reinterpret_cast<uint16_t*>(pc_)[0] = value; 345 pc_ += sizeof(uint16_t); 346} 347 348 349Address Assembler::target_address_at(Address pc) { 350 return pc + sizeof(int32_t) + *reinterpret_cast<int32_t*>(pc); 351} 352 353 354void Assembler::set_target_address_at(Address pc, Address target) { 355 int32_t* p = reinterpret_cast<int32_t*>(pc); 356 *p = target - (pc + sizeof(int32_t)); 357 CPU::FlushICache(p, sizeof(int32_t)); 358} 359 360 361Displacement Assembler::disp_at(Label* L) { 362 return Displacement(long_at(L->pos())); 363} 364 365 366void Assembler::disp_at_put(Label* L, Displacement disp) { 367 long_at_put(L->pos(), disp.data()); 368} 369 370 371void Assembler::emit_disp(Label* L, Displacement::Type type) { 372 Displacement disp(L, type); 373 L->link_to(pc_offset()); 374 emit(static_cast<int>(disp.data())); 375} 376 377 378void Operand::set_modrm(int mod, Register rm) { 379 ASSERT((mod & -4) == 0); 380 buf_[0] = mod << 6 | rm.code(); 381 len_ = 1; 382} 383 384 385void Operand::set_sib(ScaleFactor scale, Register index, Register base) { 386 ASSERT(len_ == 1); 387 ASSERT((scale & -4) == 0); 388 // Use SIB with no index register only for base esp. 389 ASSERT(!index.is(esp) || base.is(esp)); 390 buf_[1] = scale << 6 | index.code() << 3 | base.code(); 391 len_ = 2; 392} 393 394 395void Operand::set_disp8(int8_t disp) { 396 ASSERT(len_ == 1 || len_ == 2); 397 *reinterpret_cast<int8_t*>(&buf_[len_++]) = disp; 398} 399 400 401void Operand::set_dispr(int32_t disp, RelocInfo::Mode rmode) { 402 ASSERT(len_ == 1 || len_ == 2); 403 int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]); 404 *p = disp; 405 len_ += sizeof(int32_t); 406 rmode_ = rmode; 407} 408 409Operand::Operand(Register reg) { 410 // reg 411 set_modrm(3, reg); 412} 413 414 415Operand::Operand(XMMRegister xmm_reg) { 416 Register reg = { xmm_reg.code() }; 417 set_modrm(3, reg); 418} 419 420 421Operand::Operand(int32_t disp, RelocInfo::Mode rmode) { 422 // [disp/r] 423 set_modrm(0, ebp); 424 set_dispr(disp, rmode); 425} 426 427} } // namespace v8::internal 428 429#endif // V8_IA32_ASSEMBLER_IA32_INL_H_ 430