assembler-ia32-inl.h revision 9dcf7e2f83591d471e88bf7d230651900b8e424b
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 } else if (rmode_ == JS_RETURN && IsPatchedReturnSequence()) { 53 // Special handling of js_return when a break point is set (call 54 // instruction has been inserted). 55 int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1); 56 *p -= delta; // Relocate entry. 57 } else if (rmode_ == DEBUG_BREAK_SLOT && IsPatchedDebugBreakSlotSequence()) { 58 // Special handling of a debug break slot when a break point is set (call 59 // instruction has been inserted). 60 int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1); 61 *p -= delta; // Relocate entry. 62 } else if (IsInternalReference(rmode_)) { 63 // absolute code pointer inside code object moves with the code object. 64 int32_t* p = reinterpret_cast<int32_t*>(pc_); 65 *p += delta; // Relocate entry. 66 } 67} 68 69 70Address RelocInfo::target_address() { 71 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY); 72 return Assembler::target_address_at(pc_); 73} 74 75 76Address RelocInfo::target_address_address() { 77 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY); 78 return reinterpret_cast<Address>(pc_); 79} 80 81 82int RelocInfo::target_address_size() { 83 return Assembler::kExternalTargetSize; 84} 85 86 87void RelocInfo::set_target_address(Address target) { 88 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY); 89 Assembler::set_target_address_at(pc_, target); 90} 91 92 93Object* RelocInfo::target_object() { 94 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); 95 return Memory::Object_at(pc_); 96} 97 98 99Handle<Object> RelocInfo::target_object_handle(Assembler* origin) { 100 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); 101 return Memory::Object_Handle_at(pc_); 102} 103 104 105Object** RelocInfo::target_object_address() { 106 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); 107 return &Memory::Object_at(pc_); 108} 109 110 111void RelocInfo::set_target_object(Object* target) { 112 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); 113 Memory::Object_at(pc_) = target; 114} 115 116 117Address* RelocInfo::target_reference_address() { 118 ASSERT(rmode_ == RelocInfo::EXTERNAL_REFERENCE); 119 return reinterpret_cast<Address*>(pc_); 120} 121 122 123Address RelocInfo::call_address() { 124 ASSERT(IsPatchedReturnSequence()); 125 return Assembler::target_address_at(pc_ + 1); 126} 127 128 129void RelocInfo::set_call_address(Address target) { 130 ASSERT(IsPatchedReturnSequence()); 131 Assembler::set_target_address_at(pc_ + 1, target); 132} 133 134 135Object* RelocInfo::call_object() { 136 ASSERT(IsPatchedReturnSequence()); 137 return *call_object_address(); 138} 139 140 141Object** RelocInfo::call_object_address() { 142 ASSERT(IsPatchedReturnSequence()); 143 return reinterpret_cast<Object**>(pc_ + 1); 144} 145 146 147void RelocInfo::set_call_object(Object* target) { 148 ASSERT(IsPatchedReturnSequence()); 149 *call_object_address() = target; 150} 151 152 153bool RelocInfo::IsPatchedReturnSequence() { 154 return *pc_ == 0xE8; 155} 156 157 158bool RelocInfo::IsPatchedDebugBreakSlotSequence() { 159 return !Assembler::IsNop(pc()); 160} 161 162 163void RelocInfo::Visit(ObjectVisitor* visitor) { 164 RelocInfo::Mode mode = rmode(); 165 if (mode == RelocInfo::EMBEDDED_OBJECT) { 166 visitor->VisitPointer(target_object_address()); 167 } else if (RelocInfo::IsCodeTarget(mode)) { 168 visitor->VisitCodeTarget(this); 169 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) { 170 visitor->VisitExternalReference(target_reference_address()); 171#ifdef ENABLE_DEBUGGER_SUPPORT 172 } else if (Debug::has_break_points() && 173 ((RelocInfo::IsJSReturn(mode) && 174 IsPatchedReturnSequence()) || 175 (RelocInfo::IsDebugBreakSlot(mode) && 176 IsPatchedDebugBreakSlotSequence()))) { 177 visitor->VisitDebugTarget(this); 178#endif 179 } else if (mode == RelocInfo::RUNTIME_ENTRY) { 180 visitor->VisitRuntimeEntry(this); 181 } 182} 183 184 185Immediate::Immediate(int x) { 186 x_ = x; 187 rmode_ = RelocInfo::NONE; 188} 189 190 191Immediate::Immediate(const ExternalReference& ext) { 192 x_ = reinterpret_cast<int32_t>(ext.address()); 193 rmode_ = RelocInfo::EXTERNAL_REFERENCE; 194} 195 196 197Immediate::Immediate(Label* internal_offset) { 198 x_ = reinterpret_cast<int32_t>(internal_offset); 199 rmode_ = RelocInfo::INTERNAL_REFERENCE; 200} 201 202 203Immediate::Immediate(Handle<Object> handle) { 204 // Verify all Objects referred by code are NOT in new space. 205 Object* obj = *handle; 206 ASSERT(!Heap::InNewSpace(obj)); 207 if (obj->IsHeapObject()) { 208 x_ = reinterpret_cast<intptr_t>(handle.location()); 209 rmode_ = RelocInfo::EMBEDDED_OBJECT; 210 } else { 211 // no relocation needed 212 x_ = reinterpret_cast<intptr_t>(obj); 213 rmode_ = RelocInfo::NONE; 214 } 215} 216 217 218Immediate::Immediate(Smi* value) { 219 x_ = reinterpret_cast<intptr_t>(value); 220 rmode_ = RelocInfo::NONE; 221} 222 223 224void Assembler::emit(uint32_t x) { 225 *reinterpret_cast<uint32_t*>(pc_) = x; 226 pc_ += sizeof(uint32_t); 227} 228 229 230void Assembler::emit(Handle<Object> handle) { 231 // Verify all Objects referred by code are NOT in new space. 232 Object* obj = *handle; 233 ASSERT(!Heap::InNewSpace(obj)); 234 if (obj->IsHeapObject()) { 235 emit(reinterpret_cast<intptr_t>(handle.location()), 236 RelocInfo::EMBEDDED_OBJECT); 237 } else { 238 // no relocation needed 239 emit(reinterpret_cast<intptr_t>(obj)); 240 } 241} 242 243 244void Assembler::emit(uint32_t x, RelocInfo::Mode rmode) { 245 if (rmode != RelocInfo::NONE) RecordRelocInfo(rmode); 246 emit(x); 247} 248 249 250void Assembler::emit(const Immediate& x) { 251 if (x.rmode_ == RelocInfo::INTERNAL_REFERENCE) { 252 Label* label = reinterpret_cast<Label*>(x.x_); 253 emit_code_relative_offset(label); 254 return; 255 } 256 if (x.rmode_ != RelocInfo::NONE) RecordRelocInfo(x.rmode_); 257 emit(x.x_); 258} 259 260 261void Assembler::emit_code_relative_offset(Label* label) { 262 if (label->is_bound()) { 263 int32_t pos; 264 pos = label->pos() + Code::kHeaderSize - kHeapObjectTag; 265 emit(pos); 266 } else { 267 emit_disp(label, Displacement::CODE_RELATIVE); 268 } 269} 270 271 272void Assembler::emit_w(const Immediate& x) { 273 ASSERT(x.rmode_ == RelocInfo::NONE); 274 uint16_t value = static_cast<uint16_t>(x.x_); 275 reinterpret_cast<uint16_t*>(pc_)[0] = value; 276 pc_ += sizeof(uint16_t); 277} 278 279 280Address Assembler::target_address_at(Address pc) { 281 return pc + sizeof(int32_t) + *reinterpret_cast<int32_t*>(pc); 282} 283 284 285void Assembler::set_target_address_at(Address pc, Address target) { 286 int32_t* p = reinterpret_cast<int32_t*>(pc); 287 *p = target - (pc + sizeof(int32_t)); 288 CPU::FlushICache(p, sizeof(int32_t)); 289} 290 291 292Displacement Assembler::disp_at(Label* L) { 293 return Displacement(long_at(L->pos())); 294} 295 296 297void Assembler::disp_at_put(Label* L, Displacement disp) { 298 long_at_put(L->pos(), disp.data()); 299} 300 301 302void Assembler::emit_disp(Label* L, Displacement::Type type) { 303 Displacement disp(L, type); 304 L->link_to(pc_offset()); 305 emit(static_cast<int>(disp.data())); 306} 307 308 309void Operand::set_modrm(int mod, Register rm) { 310 ASSERT((mod & -4) == 0); 311 buf_[0] = mod << 6 | rm.code(); 312 len_ = 1; 313} 314 315 316void Operand::set_sib(ScaleFactor scale, Register index, Register base) { 317 ASSERT(len_ == 1); 318 ASSERT((scale & -4) == 0); 319 // Use SIB with no index register only for base esp. 320 ASSERT(!index.is(esp) || base.is(esp)); 321 buf_[1] = scale << 6 | index.code() << 3 | base.code(); 322 len_ = 2; 323} 324 325 326void Operand::set_disp8(int8_t disp) { 327 ASSERT(len_ == 1 || len_ == 2); 328 *reinterpret_cast<int8_t*>(&buf_[len_++]) = disp; 329} 330 331 332void Operand::set_dispr(int32_t disp, RelocInfo::Mode rmode) { 333 ASSERT(len_ == 1 || len_ == 2); 334 int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]); 335 *p = disp; 336 len_ += sizeof(int32_t); 337 rmode_ = rmode; 338} 339 340Operand::Operand(Register reg) { 341 // reg 342 set_modrm(3, reg); 343} 344 345 346Operand::Operand(XMMRegister xmm_reg) { 347 Register reg = { xmm_reg.code() }; 348 set_modrm(3, reg); 349} 350 351 352Operand::Operand(int32_t disp, RelocInfo::Mode rmode) { 353 // [disp/r] 354 set_modrm(0, ebp); 355 set_dispr(disp, rmode); 356} 357 358} } // namespace v8::internal 359 360#endif // V8_IA32_ASSEMBLER_IA32_INL_H_ 361