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 2012 the V8 project authors. All rights reserved.
34
35// A light-weight IA32 Assembler.
36
37#ifndef V8_CODEGEN_IA32_ASSEMBLER_IA32_INL_H_
38#define V8_CODEGEN_IA32_ASSEMBLER_IA32_INL_H_
39
40#include "src/codegen/ia32/assembler-ia32.h"
41
42#include "src/base/memory.h"
43#include "src/codegen/assembler.h"
44#include "src/debug/debug.h"
45#include "src/objects/objects-inl.h"
46
47namespace v8 {
48namespace internal {
49
50bool CpuFeatures::SupportsOptimizer() { return true; }
51
52// The modes possibly affected by apply must be in kApplyMask.
53void RelocInfo::apply(intptr_t delta) {
54  DCHECK_EQ(kApplyMask, (RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
55                         RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE) |
56                         RelocInfo::ModeMask(RelocInfo::OFF_HEAP_TARGET) |
57                         RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY)));
58  if (IsRuntimeEntry(rmode_) || IsCodeTarget(rmode_) ||
59      IsOffHeapTarget(rmode_)) {
60    base::WriteUnalignedValue(pc_,
61                              base::ReadUnalignedValue<int32_t>(pc_) - delta);
62  } else if (IsInternalReference(rmode_)) {
63    // Absolute code pointer inside code object moves with the code object.
64    base::WriteUnalignedValue(pc_,
65                              base::ReadUnalignedValue<int32_t>(pc_) + delta);
66  }
67}
68
69Address RelocInfo::target_address() {
70  DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_) || IsWasmCall(rmode_));
71  return Assembler::target_address_at(pc_, constant_pool_);
72}
73
74Address RelocInfo::target_address_address() {
75  DCHECK(HasTargetAddressAddress());
76  return pc_;
77}
78
79Address RelocInfo::constant_pool_entry_address() { UNREACHABLE(); }
80
81int RelocInfo::target_address_size() { return Assembler::kSpecialTargetSize; }
82
83HeapObject RelocInfo::target_object(PtrComprCageBase cage_base) {
84  DCHECK(IsCodeTarget(rmode_) || IsFullEmbeddedObject(rmode_) ||
85         IsDataEmbeddedObject(rmode_));
86  return HeapObject::cast(Object(ReadUnalignedValue<Address>(pc_)));
87}
88
89Handle<HeapObject> RelocInfo::target_object_handle(Assembler* origin) {
90  DCHECK(IsCodeTarget(rmode_) || IsFullEmbeddedObject(rmode_) ||
91         IsDataEmbeddedObject(rmode_));
92  return Handle<HeapObject>::cast(ReadUnalignedValue<Handle<Object>>(pc_));
93}
94
95void RelocInfo::set_target_object(Heap* heap, HeapObject target,
96                                  WriteBarrierMode write_barrier_mode,
97                                  ICacheFlushMode icache_flush_mode) {
98  DCHECK(IsCodeTarget(rmode_) || IsFullEmbeddedObject(rmode_) ||
99         IsDataEmbeddedObject(rmode_));
100  WriteUnalignedValue(pc_, target.ptr());
101  if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
102    FlushInstructionCache(pc_, sizeof(Address));
103  }
104  if (write_barrier_mode == UPDATE_WRITE_BARRIER && !host().is_null() &&
105      !FLAG_disable_write_barriers) {
106    WriteBarrierForCode(host(), this, target);
107  }
108}
109
110Address RelocInfo::target_external_reference() {
111  DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
112  return ReadUnalignedValue<Address>(pc_);
113}
114
115void RelocInfo::set_target_external_reference(
116    Address target, ICacheFlushMode icache_flush_mode) {
117  DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
118  WriteUnalignedValue(pc_, target);
119  if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
120    FlushInstructionCache(pc_, sizeof(Address));
121  }
122}
123
124Address RelocInfo::target_internal_reference() {
125  DCHECK(rmode_ == INTERNAL_REFERENCE);
126  return ReadUnalignedValue<Address>(pc_);
127}
128
129Address RelocInfo::target_internal_reference_address() {
130  DCHECK(rmode_ == INTERNAL_REFERENCE);
131  return pc_;
132}
133
134Address RelocInfo::target_runtime_entry(Assembler* origin) {
135  DCHECK(IsRuntimeEntry(rmode_));
136  return ReadUnalignedValue<Address>(pc_);
137}
138
139void RelocInfo::set_target_runtime_entry(Address target,
140                                         WriteBarrierMode write_barrier_mode,
141                                         ICacheFlushMode icache_flush_mode) {
142  DCHECK(IsRuntimeEntry(rmode_));
143  if (target_address() != target) {
144    set_target_address(target, write_barrier_mode, icache_flush_mode);
145  }
146}
147
148Address RelocInfo::target_off_heap_target() {
149  DCHECK(IsOffHeapTarget(rmode_));
150  return Assembler::target_address_at(pc_, constant_pool_);
151}
152
153void RelocInfo::WipeOut() {
154  if (IsFullEmbeddedObject(rmode_) || IsExternalReference(rmode_) ||
155      IsInternalReference(rmode_)) {
156    WriteUnalignedValue(pc_, kNullAddress);
157  } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_) ||
158             IsOffHeapTarget(rmode_)) {
159    // Effectively write zero into the relocation.
160    Assembler::set_target_address_at(pc_, constant_pool_,
161                                     pc_ + sizeof(int32_t));
162  } else {
163    UNREACHABLE();
164  }
165}
166
167void Assembler::emit(uint32_t x) {
168  WriteUnalignedValue(reinterpret_cast<Address>(pc_), x);
169  pc_ += sizeof(uint32_t);
170}
171
172void Assembler::emit_q(uint64_t x) {
173  WriteUnalignedValue(reinterpret_cast<Address>(pc_), x);
174  pc_ += sizeof(uint64_t);
175}
176
177void Assembler::emit(Handle<HeapObject> handle) {
178  emit(handle.address(), RelocInfo::FULL_EMBEDDED_OBJECT);
179}
180
181void Assembler::emit(uint32_t x, RelocInfo::Mode rmode) {
182  if (!RelocInfo::IsNoInfo(rmode)) {
183    RecordRelocInfo(rmode);
184  }
185  emit(x);
186}
187
188void Assembler::emit(Handle<Code> code, RelocInfo::Mode rmode) {
189  emit(code.address(), rmode);
190}
191
192void Assembler::emit(const Immediate& x) {
193  if (x.rmode_ == RelocInfo::INTERNAL_REFERENCE) {
194    Label* label = reinterpret_cast<Label*>(x.immediate());
195    emit_code_relative_offset(label);
196    return;
197  }
198  if (!RelocInfo::IsNoInfo(x.rmode_)) RecordRelocInfo(x.rmode_);
199  if (x.is_heap_object_request()) {
200    RequestHeapObject(x.heap_object_request());
201    emit(0);
202    return;
203  }
204  emit(x.immediate());
205}
206
207void Assembler::emit_code_relative_offset(Label* label) {
208  if (label->is_bound()) {
209    int32_t pos;
210    pos = label->pos() + Code::kHeaderSize - kHeapObjectTag;
211    emit(pos);
212  } else {
213    emit_disp(label, Displacement::CODE_RELATIVE);
214  }
215}
216
217void Assembler::emit_b(Immediate x) {
218  DCHECK(x.is_int8() || x.is_uint8());
219  uint8_t value = static_cast<uint8_t>(x.immediate());
220  *pc_++ = value;
221}
222
223void Assembler::emit_w(const Immediate& x) {
224  DCHECK(RelocInfo::IsNoInfo(x.rmode_));
225  uint16_t value = static_cast<uint16_t>(x.immediate());
226  WriteUnalignedValue(reinterpret_cast<Address>(pc_), value);
227  pc_ += sizeof(uint16_t);
228}
229
230Address Assembler::target_address_at(Address pc, Address constant_pool) {
231  return pc + sizeof(int32_t) + ReadUnalignedValue<int32_t>(pc);
232}
233
234void Assembler::set_target_address_at(Address pc, Address constant_pool,
235                                      Address target,
236                                      ICacheFlushMode icache_flush_mode) {
237  WriteUnalignedValue(pc, target - (pc + sizeof(int32_t)));
238  if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
239    FlushInstructionCache(pc, sizeof(int32_t));
240  }
241}
242
243void Assembler::deserialization_set_special_target_at(
244    Address instruction_payload, Code code, Address target) {
245  set_target_address_at(instruction_payload,
246                        !code.is_null() ? code.constant_pool() : kNullAddress,
247                        target);
248}
249
250int Assembler::deserialization_special_target_size(
251    Address instruction_payload) {
252  return kSpecialTargetSize;
253}
254
255Displacement Assembler::disp_at(Label* L) {
256  return Displacement(long_at(L->pos()));
257}
258
259void Assembler::disp_at_put(Label* L, Displacement disp) {
260  long_at_put(L->pos(), disp.data());
261}
262
263void Assembler::emit_disp(Label* L, Displacement::Type type) {
264  Displacement disp(L, type);
265  L->link_to(pc_offset());
266  emit(static_cast<int>(disp.data()));
267}
268
269void Assembler::emit_near_disp(Label* L) {
270  byte disp = 0x00;
271  if (L->is_near_linked()) {
272    int offset = L->near_link_pos() - pc_offset();
273    DCHECK(is_int8(offset));
274    disp = static_cast<byte>(offset & 0xFF);
275  }
276  L->link_to(pc_offset(), Label::kNear);
277  *pc_++ = disp;
278}
279
280void Assembler::deserialization_set_target_internal_reference_at(
281    Address pc, Address target, RelocInfo::Mode mode) {
282  WriteUnalignedValue(pc, target);
283}
284
285void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
286  DCHECK_EQ(len_, 1);
287  DCHECK_EQ(scale & -4, 0);
288  // Use SIB with no index register only for base esp.
289  DCHECK(index != esp || base == esp);
290  buf_[1] = scale << 6 | index.code() << 3 | base.code();
291  len_ = 2;
292}
293
294void Operand::set_disp8(int8_t disp) {
295  DCHECK(len_ == 1 || len_ == 2);
296  *reinterpret_cast<int8_t*>(&buf_[len_++]) = disp;
297}
298
299}  // namespace internal
300}  // namespace v8
301
302#endif  // V8_CODEGEN_IA32_ASSEMBLER_IA32_INL_H_
303