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