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 2021 the V8 project authors. All rights reserved.
34 
35 #ifndef V8_CODEGEN_RISCV64_ASSEMBLER_RISCV64_INL_H_
36 #define V8_CODEGEN_RISCV64_ASSEMBLER_RISCV64_INL_H_
37 
38 #include "src/codegen/assembler.h"
39 #include "src/codegen/riscv64/assembler-riscv64.h"
40 #include "src/debug/debug.h"
41 #include "src/objects/objects-inl.h"
42 
43 namespace v8 {
44 namespace internal {
45 
SupportsOptimizer()46 bool CpuFeatures::SupportsOptimizer() { return IsSupported(FPU); }
47 
48 // -----------------------------------------------------------------------------
49 // Operand and MemOperand.
50 
is_reg()51 bool Operand::is_reg() const { return rm_.is_valid(); }
52 
immediate()53 int64_t Operand::immediate() const {
54   DCHECK(!is_reg());
55   DCHECK(!IsHeapObjectRequest());
56   return value_.immediate;
57 }
58 
59 // -----------------------------------------------------------------------------
60 // RelocInfo.
61 
apply(intptr_t delta)62 void RelocInfo::apply(intptr_t delta) {
63   if (IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_)) {
64     // Absolute code pointer inside code object moves with the code object.
65     Assembler::RelocateInternalReference(rmode_, pc_, delta);
66   } else {
67     DCHECK(IsRelativeCodeTarget(rmode_));
68     Assembler::RelocateRelativeReference(rmode_, pc_, delta);
69   }
70 }
71 
target_address()72 Address RelocInfo::target_address() {
73   DCHECK(IsCodeTargetMode(rmode_) || IsRuntimeEntry(rmode_) ||
74          IsWasmCall(rmode_));
75   return Assembler::target_address_at(pc_, constant_pool_);
76 }
77 
target_address_address()78 Address RelocInfo::target_address_address() {
79   DCHECK(HasTargetAddressAddress());
80   // Read the address of the word containing the target_address in an
81   // instruction stream.
82   // The only architecture-independent user of this function is the serializer.
83   // The serializer uses it to find out how many raw bytes of instruction to
84   // output before the next target.
85   // For an instruction like LUI/ORI where the target bits are mixed into the
86   // instruction bits, the size of the target will be zero, indicating that the
87   // serializer should not step forward in memory after a target is resolved
88   // and written. In this case the target_address_address function should
89   // return the end of the instructions to be patched, allowing the
90   // deserializer to deserialize the instructions as raw bytes and put them in
91   // place, ready to be patched with the target. After jump optimization,
92   // that is the address of the instruction that follows J/JAL/JR/JALR
93   // instruction.
94   return pc_ + Assembler::kInstructionsFor64BitConstant * kInstrSize;
95 }
96 
constant_pool_entry_address()97 Address RelocInfo::constant_pool_entry_address() { UNREACHABLE(); }
98 
target_address_size()99 int RelocInfo::target_address_size() {
100   if (IsCodedSpecially()) {
101     return Assembler::kSpecialTargetSize;
102   } else {
103     return kSystemPointerSize;
104   }
105 }
106 
set_target_compressed_address_at(Address pc,Address constant_pool,Tagged_t target,ICacheFlushMode icache_flush_mode)107 void Assembler::set_target_compressed_address_at(
108     Address pc, Address constant_pool, Tagged_t target,
109     ICacheFlushMode icache_flush_mode) {
110   Assembler::set_target_address_at(
111       pc, constant_pool, static_cast<Address>(target), icache_flush_mode);
112 }
113 
target_compressed_address_at(Address pc,Address constant_pool)114 Tagged_t Assembler::target_compressed_address_at(Address pc,
115                                                  Address constant_pool) {
116   return static_cast<Tagged_t>(target_address_at(pc, constant_pool));
117 }
118 
code_target_object_handle_at(Address pc,Address constant_pool)119 Handle<Object> Assembler::code_target_object_handle_at(Address pc,
120                                                        Address constant_pool) {
121   int index =
122       static_cast<int>(target_address_at(pc, constant_pool)) & 0xFFFFFFFF;
123   return GetCodeTarget(index);
124 }
125 
compressed_embedded_object_handle_at(Address pc,Address const_pool)126 Handle<HeapObject> Assembler::compressed_embedded_object_handle_at(
127     Address pc, Address const_pool) {
128   return GetEmbeddedObject(target_compressed_address_at(pc, const_pool));
129 }
130 
deserialization_set_special_target_at(Address instruction_payload,Code code,Address target)131 void Assembler::deserialization_set_special_target_at(
132     Address instruction_payload, Code code, Address target) {
133   set_target_address_at(instruction_payload,
134                         !code.is_null() ? code.constant_pool() : kNullAddress,
135                         target);
136 }
137 
deserialization_special_target_size(Address instruction_payload)138 int Assembler::deserialization_special_target_size(
139     Address instruction_payload) {
140   return kSpecialTargetSize;
141 }
142 
set_target_internal_reference_encoded_at(Address pc,Address target)143 void Assembler::set_target_internal_reference_encoded_at(Address pc,
144                                                          Address target) {
145   set_target_value_at(pc, static_cast<uint64_t>(target));
146 }
147 
deserialization_set_target_internal_reference_at(Address pc,Address target,RelocInfo::Mode mode)148 void Assembler::deserialization_set_target_internal_reference_at(
149     Address pc, Address target, RelocInfo::Mode mode) {
150   if (RelocInfo::IsInternalReferenceEncoded(mode)) {
151     DCHECK(IsLui(instr_at(pc)));
152     set_target_internal_reference_encoded_at(pc, target);
153   } else {
154     DCHECK(RelocInfo::IsInternalReference(mode));
155     Memory<Address>(pc) = target;
156   }
157 }
158 
target_object()159 HeapObject RelocInfo::target_object() {
160   DCHECK(IsCodeTarget(rmode_) || IsEmbeddedObjectMode(rmode_));
161   if (IsDataEmbeddedObject(rmode_)) {
162     return HeapObject::cast(Object(ReadUnalignedValue<Address>(pc_)));
163   } else if (IsCompressedEmbeddedObject(rmode_)) {
164     return HeapObject::cast(Object(DecompressTaggedAny(
165         host_.address(),
166         Assembler::target_compressed_address_at(pc_, constant_pool_))));
167   } else {
168     return HeapObject::cast(
169         Object(Assembler::target_address_at(pc_, constant_pool_)));
170   }
171 }
172 
target_object_no_host(PtrComprCageBase cage_base)173 HeapObject RelocInfo::target_object_no_host(PtrComprCageBase cage_base) {
174   if (IsCompressedEmbeddedObject(rmode_)) {
175     return HeapObject::cast(Object(DecompressTaggedAny(
176         cage_base,
177         Assembler::target_compressed_address_at(pc_, constant_pool_))));
178   } else {
179     return target_object();
180   }
181 }
182 
target_object_handle(Assembler * origin)183 Handle<HeapObject> RelocInfo::target_object_handle(Assembler* origin) {
184   if (IsDataEmbeddedObject(rmode_)) {
185     return Handle<HeapObject>::cast(ReadUnalignedValue<Handle<Object>>(pc_));
186   } else if (IsCodeTarget(rmode_)) {
187     return Handle<HeapObject>::cast(
188         origin->code_target_object_handle_at(pc_, constant_pool_));
189   } else if (IsCompressedEmbeddedObject(rmode_)) {
190     return origin->compressed_embedded_object_handle_at(pc_, constant_pool_);
191   } else if (IsFullEmbeddedObject(rmode_)) {
192     return Handle<HeapObject>(reinterpret_cast<Address*>(
193         Assembler::target_address_at(pc_, constant_pool_)));
194   } else {
195     DCHECK(IsRelativeCodeTarget(rmode_));
196     return origin->relative_code_target_object_handle_at(pc_);
197   }
198 }
199 
set_target_object(Heap * heap,HeapObject target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)200 void RelocInfo::set_target_object(Heap* heap, HeapObject target,
201                                   WriteBarrierMode write_barrier_mode,
202                                   ICacheFlushMode icache_flush_mode) {
203   DCHECK(IsCodeTarget(rmode_) || IsEmbeddedObjectMode(rmode_));
204   if (IsDataEmbeddedObject(rmode_)) {
205     WriteUnalignedValue(pc_, target.ptr());
206     // No need to flush icache since no instructions were changed.
207   } else if (IsCompressedEmbeddedObject(rmode_)) {
208     Assembler::set_target_compressed_address_at(
209         pc_, constant_pool_, CompressTagged(target.ptr()), icache_flush_mode);
210   } else {
211     DCHECK(IsFullEmbeddedObject(rmode_));
212     Assembler::set_target_address_at(pc_, constant_pool_, target.ptr(),
213                                      icache_flush_mode);
214   }
215   if (write_barrier_mode == UPDATE_WRITE_BARRIER && !host().is_null() &&
216       !FLAG_disable_write_barriers) {
217     WriteBarrierForCode(host(), this, target);
218   }
219 }
220 
target_external_reference()221 Address RelocInfo::target_external_reference() {
222   DCHECK(rmode_ == EXTERNAL_REFERENCE);
223   return Assembler::target_address_at(pc_, constant_pool_);
224 }
225 
set_target_external_reference(Address target,ICacheFlushMode icache_flush_mode)226 void RelocInfo::set_target_external_reference(
227     Address target, ICacheFlushMode icache_flush_mode) {
228   DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
229   Assembler::set_target_address_at(pc_, constant_pool_, target,
230                                    icache_flush_mode);
231 }
232 
target_internal_reference()233 Address RelocInfo::target_internal_reference() {
234   if (IsInternalReference(rmode_)) {
235     return Memory<Address>(pc_);
236   } else {
237     // Encoded internal references are j/jal instructions.
238     DCHECK(IsInternalReferenceEncoded(rmode_));
239     DCHECK(Assembler::IsLui(Assembler::instr_at(pc_ + 0 * kInstrSize)));
240     Address address = Assembler::target_address_at(pc_);
241     return address;
242   }
243 }
244 
target_internal_reference_address()245 Address RelocInfo::target_internal_reference_address() {
246   DCHECK(IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
247   return pc_;
248 }
249 
relative_code_target_object_handle_at(Address pc)250 Handle<Code> Assembler::relative_code_target_object_handle_at(
251     Address pc) const {
252   Instr instr1 = Assembler::instr_at(pc);
253   Instr instr2 = Assembler::instr_at(pc + kInstrSize);
254   DCHECK(IsAuipc(instr1));
255   DCHECK(IsJalr(instr2));
256   int32_t code_target_index = BrachlongOffset(instr1, instr2);
257   return GetCodeTarget(code_target_index);
258 }
259 
target_runtime_entry(Assembler * origin)260 Address RelocInfo::target_runtime_entry(Assembler* origin) {
261   DCHECK(IsRuntimeEntry(rmode_));
262   return target_address();
263 }
264 
set_target_runtime_entry(Address target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)265 void RelocInfo::set_target_runtime_entry(Address target,
266                                          WriteBarrierMode write_barrier_mode,
267                                          ICacheFlushMode icache_flush_mode) {
268   DCHECK(IsRuntimeEntry(rmode_));
269   if (target_address() != target)
270     set_target_address(target, write_barrier_mode, icache_flush_mode);
271 }
272 
target_off_heap_target()273 Address RelocInfo::target_off_heap_target() {
274   DCHECK(IsOffHeapTarget(rmode_));
275   return Assembler::target_address_at(pc_, constant_pool_);
276 }
277 
WipeOut()278 void RelocInfo::WipeOut() {
279   DCHECK(IsFullEmbeddedObject(rmode_) || IsCodeTarget(rmode_) ||
280          IsRuntimeEntry(rmode_) || IsExternalReference(rmode_) ||
281          IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_) ||
282          IsOffHeapTarget(rmode_));
283   if (IsInternalReference(rmode_)) {
284     Memory<Address>(pc_) = kNullAddress;
285   } else if (IsInternalReferenceEncoded(rmode_)) {
286     Assembler::set_target_internal_reference_encoded_at(pc_, kNullAddress);
287   } else {
288     Assembler::set_target_address_at(pc_, constant_pool_, kNullAddress);
289   }
290 }
291 
292 // -----------------------------------------------------------------------------
293 // Assembler.
294 
CheckBuffer()295 void Assembler::CheckBuffer() {
296   if (buffer_space() <= kGap) {
297     GrowBuffer();
298   }
299 }
300 
301 template <typename T>
EmitHelper(T x)302 void Assembler::EmitHelper(T x) {
303   *reinterpret_cast<T*>(pc_) = x;
304   pc_ += sizeof(x);
305 }
306 
emit(Instr x)307 void Assembler::emit(Instr x) {
308   if (!is_buffer_growth_blocked()) {
309     CheckBuffer();
310   }
311   DEBUG_PRINTF("%p: ", pc_);
312   disassembleInstr(x);
313   EmitHelper(x);
314   CheckTrampolinePoolQuick();
315 }
316 
emit(ShortInstr x)317 void Assembler::emit(ShortInstr x) {
318   if (!is_buffer_growth_blocked()) {
319     CheckBuffer();
320   }
321   DEBUG_PRINTF("%p: ", pc_);
322   disassembleInstr(x);
323   EmitHelper(x);
324   CheckTrampolinePoolQuick();
325 }
326 
emit(uint64_t data)327 void Assembler::emit(uint64_t data) {
328   if (!is_buffer_growth_blocked()) CheckBuffer();
329   EmitHelper(data);
330 }
331 
EnsureSpace(Assembler * assembler)332 EnsureSpace::EnsureSpace(Assembler* assembler) { assembler->CheckBuffer(); }
333 
334 }  // namespace internal
335 }  // namespace v8
336 
337 #endif  // V8_CODEGEN_RISCV64_ASSEMBLER_RISCV64_INL_H_
338