1 // Copyright (c) 1994-2006 Sun Microsystems Inc.
2 // All Rights Reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // - Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
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.
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.
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.
31 // The original source code covered by the above license above has been
32 // modified significantly by Google Inc.
33 // Copyright 2011 the V8 project authors. All rights reserved.
35 // A light-weight IA32 Assembler.
37 #ifndef V8_IA32_ASSEMBLER_IA32_INL_H_
38 #define V8_IA32_ASSEMBLER_IA32_INL_H_
47 // The modes possibly affected by apply must be in kApplyMask.
48 void 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));
74 Address RelocInfo::target_address() {
75 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
76 return Assembler::target_address_at(pc_);
80 Address RelocInfo::target_address_address() {
81 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
82 return reinterpret_cast<Address>(pc_);
86 int RelocInfo::target_address_size() {
87 return Assembler::kExternalTargetSize;
91 void RelocInfo::set_target_address(Address target, WriteBarrierMode mode) {
92 Assembler::set_target_address_at(pc_, target);
93 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
94 if (mode == UPDATE_WRITE_BARRIER && host() != NULL && IsCodeTarget(rmode_)) {
95 Object* target_code = Code::GetCodeFromTargetAddress(target);
96 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
97 host(), this, HeapObject::cast(target_code));
102 Object* RelocInfo::target_object() {
103 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
104 return Memory::Object_at(pc_);
108 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
109 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
110 return Memory::Object_Handle_at(pc_);
114 Object** RelocInfo::target_object_address() {
115 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
116 return &Memory::Object_at(pc_);
120 void RelocInfo::set_target_object(Object* target, WriteBarrierMode mode) {
121 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
122 Memory::Object_at(pc_) = target;
123 CPU::FlushICache(pc_, sizeof(Address));
124 if (mode == UPDATE_WRITE_BARRIER &&
126 target->IsHeapObject()) {
127 host()->GetHeap()->incremental_marking()->RecordWrite(
128 host(), &Memory::Object_at(pc_), HeapObject::cast(target));
133 Address* RelocInfo::target_reference_address() {
134 ASSERT(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
135 return reinterpret_cast<Address*>(pc_);
139 Handle<JSGlobalPropertyCell> RelocInfo::target_cell_handle() {
140 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
141 Address address = Memory::Address_at(pc_);
142 return Handle<JSGlobalPropertyCell>(
143 reinterpret_cast<JSGlobalPropertyCell**>(address));
147 JSGlobalPropertyCell* RelocInfo::target_cell() {
148 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
149 Address address = Memory::Address_at(pc_);
150 Object* object = HeapObject::FromAddress(
151 address - JSGlobalPropertyCell::kValueOffset);
152 return reinterpret_cast<JSGlobalPropertyCell*>(object);
156 void RelocInfo::set_target_cell(JSGlobalPropertyCell* cell,
157 WriteBarrierMode mode) {
158 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
159 Address address = cell->address() + JSGlobalPropertyCell::kValueOffset;
160 Memory::Address_at(pc_) = address;
161 CPU::FlushICache(pc_, sizeof(Address));
162 if (mode == UPDATE_WRITE_BARRIER && host() != NULL) {
163 // TODO(1550) We are passing NULL as a slot because cell can never be on
164 // evacuation candidate.
165 host()->GetHeap()->incremental_marking()->RecordWrite(
171 Address RelocInfo::call_address() {
172 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
173 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
174 return Assembler::target_address_at(pc_ + 1);
178 void RelocInfo::set_call_address(Address target) {
179 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
180 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
181 Assembler::set_target_address_at(pc_ + 1, target);
182 if (host() != NULL) {
183 Object* target_code = Code::GetCodeFromTargetAddress(target);
184 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
185 host(), this, HeapObject::cast(target_code));
190 Object* RelocInfo::call_object() {
191 return *call_object_address();
195 void RelocInfo::set_call_object(Object* target) {
196 *call_object_address() = target;
200 Object** RelocInfo::call_object_address() {
201 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
202 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
203 return reinterpret_cast<Object**>(pc_ + 1);
207 bool RelocInfo::IsPatchedReturnSequence() {
212 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
213 return !Assembler::IsNop(pc());
217 void RelocInfo::Visit(ObjectVisitor* visitor) {
218 RelocInfo::Mode mode = rmode();
219 if (mode == RelocInfo::EMBEDDED_OBJECT) {
220 visitor->VisitEmbeddedPointer(this);
221 CPU::FlushICache(pc_, sizeof(Address));
222 } else if (RelocInfo::IsCodeTarget(mode)) {
223 visitor->VisitCodeTarget(this);
224 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
225 visitor->VisitGlobalPropertyCell(this);
226 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
227 visitor->VisitExternalReference(target_reference_address());
228 CPU::FlushICache(pc_, sizeof(Address));
229 #ifdef ENABLE_DEBUGGER_SUPPORT
230 // TODO(isolates): Get a cached isolate below.
231 } else if (((RelocInfo::IsJSReturn(mode) &&
232 IsPatchedReturnSequence()) ||
233 (RelocInfo::IsDebugBreakSlot(mode) &&
234 IsPatchedDebugBreakSlotSequence())) &&
235 Isolate::Current()->debug()->has_break_points()) {
236 visitor->VisitDebugTarget(this);
238 } else if (mode == RelocInfo::RUNTIME_ENTRY) {
239 visitor->VisitRuntimeEntry(this);
244 template<typename StaticVisitor>
245 void RelocInfo::Visit(Heap* heap) {
246 RelocInfo::Mode mode = rmode();
247 if (mode == RelocInfo::EMBEDDED_OBJECT) {
248 StaticVisitor::VisitEmbeddedPointer(heap, this);
249 CPU::FlushICache(pc_, sizeof(Address));
250 } else if (RelocInfo::IsCodeTarget(mode)) {
251 StaticVisitor::VisitCodeTarget(heap, this);
252 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
253 StaticVisitor::VisitGlobalPropertyCell(heap, this);
254 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
255 StaticVisitor::VisitExternalReference(target_reference_address());
256 CPU::FlushICache(pc_, sizeof(Address));
257 #ifdef ENABLE_DEBUGGER_SUPPORT
258 } else if (heap->isolate()->debug()->has_break_points() &&
259 ((RelocInfo::IsJSReturn(mode) &&
260 IsPatchedReturnSequence()) ||
261 (RelocInfo::IsDebugBreakSlot(mode) &&
262 IsPatchedDebugBreakSlotSequence()))) {
263 StaticVisitor::VisitDebugTarget(heap, this);
265 } else if (mode == RelocInfo::RUNTIME_ENTRY) {
266 StaticVisitor::VisitRuntimeEntry(this);
272 Immediate::Immediate(int x) {
274 rmode_ = RelocInfo::NONE;
278 Immediate::Immediate(const ExternalReference& ext) {
279 x_ = reinterpret_cast<int32_t>(ext.address());
280 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
284 Immediate::Immediate(Label* internal_offset) {
285 x_ = reinterpret_cast<int32_t>(internal_offset);
286 rmode_ = RelocInfo::INTERNAL_REFERENCE;
290 Immediate::Immediate(Handle<Object> handle) {
291 // Verify all Objects referred by code are NOT in new space.
292 Object* obj = *handle;
293 ASSERT(!HEAP->InNewSpace(obj));
294 if (obj->IsHeapObject()) {
295 x_ = reinterpret_cast<intptr_t>(handle.location());
296 rmode_ = RelocInfo::EMBEDDED_OBJECT;
298 // no relocation needed
299 x_ = reinterpret_cast<intptr_t>(obj);
300 rmode_ = RelocInfo::NONE;
305 Immediate::Immediate(Smi* value) {
306 x_ = reinterpret_cast<intptr_t>(value);
307 rmode_ = RelocInfo::NONE;
311 Immediate::Immediate(Address addr) {
312 x_ = reinterpret_cast<int32_t>(addr);
313 rmode_ = RelocInfo::NONE;
317 void Assembler::emit(uint32_t x) {
318 *reinterpret_cast<uint32_t*>(pc_) = x;
319 pc_ += sizeof(uint32_t);
323 void Assembler::emit(Handle<Object> handle) {
324 // Verify all Objects referred by code are NOT in new space.
325 Object* obj = *handle;
326 ASSERT(!isolate()->heap()->InNewSpace(obj));
327 if (obj->IsHeapObject()) {
328 emit(reinterpret_cast<intptr_t>(handle.location()),
329 RelocInfo::EMBEDDED_OBJECT);
331 // no relocation needed
332 emit(reinterpret_cast<intptr_t>(obj));
337 void Assembler::emit(uint32_t x, RelocInfo::Mode rmode, unsigned id) {
338 if (rmode == RelocInfo::CODE_TARGET && id != kNoASTId) {
339 RecordRelocInfo(RelocInfo::CODE_TARGET_WITH_ID, static_cast<intptr_t>(id));
340 } else if (rmode != RelocInfo::NONE) {
341 RecordRelocInfo(rmode);
347 void Assembler::emit(const Immediate& x) {
348 if (x.rmode_ == RelocInfo::INTERNAL_REFERENCE) {
349 Label* label = reinterpret_cast<Label*>(x.x_);
350 emit_code_relative_offset(label);
353 if (x.rmode_ != RelocInfo::NONE) RecordRelocInfo(x.rmode_);
358 void Assembler::emit_code_relative_offset(Label* label) {
359 if (label->is_bound()) {
361 pos = label->pos() + Code::kHeaderSize - kHeapObjectTag;
364 emit_disp(label, Displacement::CODE_RELATIVE);
369 void Assembler::emit_w(const Immediate& x) {
370 ASSERT(x.rmode_ == RelocInfo::NONE);
371 uint16_t value = static_cast<uint16_t>(x.x_);
372 reinterpret_cast<uint16_t*>(pc_)[0] = value;
373 pc_ += sizeof(uint16_t);
377 Address Assembler::target_address_at(Address pc) {
378 return pc + sizeof(int32_t) + *reinterpret_cast<int32_t*>(pc);
382 void Assembler::set_target_address_at(Address pc, Address target) {
383 int32_t* p = reinterpret_cast<int32_t*>(pc);
384 *p = target - (pc + sizeof(int32_t));
385 CPU::FlushICache(p, sizeof(int32_t));
389 Displacement Assembler::disp_at(Label* L) {
390 return Displacement(long_at(L->pos()));
394 void Assembler::disp_at_put(Label* L, Displacement disp) {
395 long_at_put(L->pos(), disp.data());
399 void Assembler::emit_disp(Label* L, Displacement::Type type) {
400 Displacement disp(L, type);
401 L->link_to(pc_offset());
402 emit(static_cast<int>(disp.data()));
406 void Assembler::emit_near_disp(Label* L) {
408 if (L->is_near_linked()) {
409 int offset = L->near_link_pos() - pc_offset();
410 ASSERT(is_int8(offset));
411 disp = static_cast<byte>(offset & 0xFF);
413 L->link_to(pc_offset(), Label::kNear);
418 void Operand::set_modrm(int mod, Register rm) {
419 ASSERT((mod & -4) == 0);
420 buf_[0] = mod << 6 | rm.code();
425 void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
427 ASSERT((scale & -4) == 0);
428 // Use SIB with no index register only for base esp.
429 ASSERT(!index.is(esp) || base.is(esp));
430 buf_[1] = scale << 6 | index.code() << 3 | base.code();
435 void Operand::set_disp8(int8_t disp) {
436 ASSERT(len_ == 1 || len_ == 2);
437 *reinterpret_cast<int8_t*>(&buf_[len_++]) = disp;
441 void Operand::set_dispr(int32_t disp, RelocInfo::Mode rmode) {
442 ASSERT(len_ == 1 || len_ == 2);
443 int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]);
445 len_ += sizeof(int32_t);
449 Operand::Operand(Register reg) {
455 Operand::Operand(XMMRegister xmm_reg) {
456 Register reg = { xmm_reg.code() };
461 Operand::Operand(int32_t disp, RelocInfo::Mode rmode) {
464 set_dispr(disp, rmode);
467 } } // namespace v8::internal
469 #endif // V8_IA32_ASSEMBLER_IA32_INL_H_