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