1 /*
  2  * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef CPU_RISCV_STACKCHUNKFRAMESTREAM_RISCV_INLINE_HPP
 26 #define CPU_RISCV_STACKCHUNKFRAMESTREAM_RISCV_INLINE_HPP
 27 
 28 #include "interpreter/oopMapCache.hpp"
 29 #include "runtime/frame.inline.hpp"
 30 #include "runtime/registerMap.hpp"
 31 
 32 #ifdef ASSERT
 33 template <ChunkFrames frame_kind>
 34 inline bool StackChunkFrameStream<frame_kind>::is_in_frame(void* p0) const {
 35   assert(!is_done(), "");
 36   intptr_t* p = (intptr_t*)p0;
 37   int argsize = is_compiled() ? (_cb->as_nmethod()->num_stack_arg_slots() * VMRegImpl::stack_slot_size) >> LogBytesPerWord : 0;
 38   int frame_size = _cb->frame_size() + argsize;
 39   return p == sp() - 2 || ((p - unextended_sp()) >= 0 && (p - unextended_sp()) < frame_size);
 40 }
 41 #endif
 42 
 43 template <ChunkFrames frame_kind>
 44 inline frame StackChunkFrameStream<frame_kind>::to_frame() const {
 45   if (is_done()) {
 46     return frame(_sp, _sp, nullptr, nullptr, nullptr, nullptr, true);
 47   } else {
 48     return frame(sp(), unextended_sp(), fp(), pc(), cb(), _oopmap, true);
 49   }
 50 }
 51 
 52 template <ChunkFrames frame_kind>
 53 inline address StackChunkFrameStream<frame_kind>::get_pc() const {
 54   assert(!is_done(), "");
 55   return *(address*)(_sp - 1);
 56 }
 57 
 58 template <ChunkFrames frame_kind>
 59 inline intptr_t* StackChunkFrameStream<frame_kind>::fp() const {
 60   intptr_t* fp_addr = _sp - 2;
 61   return (frame_kind == ChunkFrames::Mixed && is_interpreted())
 62     ? fp_addr + *fp_addr // derelativize
 63     : *(intptr_t**)fp_addr;
 64 }
 65 
 66 template <ChunkFrames frame_kind>
 67 inline intptr_t* StackChunkFrameStream<frame_kind>::derelativize(int offset) const {
 68   intptr_t* fp = this->fp();
 69   assert(fp != nullptr, "");
 70   return fp + fp[offset];
 71 }
 72 
 73 template <ChunkFrames frame_kind>
 74 inline intptr_t* StackChunkFrameStream<frame_kind>::unextended_sp_for_interpreter_frame() const {
 75   assert_is_interpreted_and_frame_type_mixed();
 76   return derelativize(frame::interpreter_frame_last_sp_offset);
 77 }
 78 
 79 template <ChunkFrames frame_kind>
 80 inline void StackChunkFrameStream<frame_kind>::next_for_interpreter_frame() {
 81   assert_is_interpreted_and_frame_type_mixed();
 82   if (derelativize(frame::interpreter_frame_locals_offset) + 1 >= _end) {
 83     _unextended_sp = _end;
 84     _sp = _end;
 85   } else {
 86     intptr_t* fp = this->fp();
 87     _unextended_sp = fp + fp[frame::interpreter_frame_sender_sp_offset];
 88     _sp = fp + frame::sender_sp_offset;
 89   }
 90 }
 91 
 92 template <ChunkFrames frame_kind>
 93 inline int StackChunkFrameStream<frame_kind>::interpreter_frame_size() const {
 94   assert_is_interpreted_and_frame_type_mixed();
 95 
 96   intptr_t* top = unextended_sp(); // later subtract argsize if callee is interpreted
 97   intptr_t* bottom = derelativize(frame::interpreter_frame_locals_offset) + 1; // the sender's unextended sp: derelativize(frame::interpreter_frame_sender_sp_offset);
 98   return (int)(bottom - top);
 99 }
100 
101 template <ChunkFrames frame_kind>
102 inline int StackChunkFrameStream<frame_kind>::interpreter_frame_stack_argsize() const {
103   assert_is_interpreted_and_frame_type_mixed();
104   int diff = (int)(derelativize(frame::interpreter_frame_locals_offset) - derelativize(frame::interpreter_frame_sender_sp_offset) + 1);
105   return diff;
106 }
107 
108 template <ChunkFrames frame_kind>
109 inline int StackChunkFrameStream<frame_kind>::interpreter_frame_num_oops() const {
110   assert_is_interpreted_and_frame_type_mixed();
111   ResourceMark rm;
112   InterpreterOopMap mask;
113   frame f = to_frame();
114   f.interpreted_frame_oop_map(&mask);
115   return mask.num_oops()
116         + 1 // for the mirror oop
117         + (f.interpreter_frame_method()->is_native() ? 1 : 0) // temp oop slot
118         + pointer_delta_as_int((intptr_t*)f.interpreter_frame_monitor_begin(),
119               (intptr_t*)f.interpreter_frame_monitor_end()) / BasicObjectLock::size();
120 }
121 
122 template<>
123 template<>
124 inline void StackChunkFrameStream<ChunkFrames::Mixed>::update_reg_map_pd(RegisterMap* map) {
125   if (map->update_map()) {
126     frame::update_map_with_saved_link(map, map->in_cont() ? (intptr_t**)2 : (intptr_t**)(_sp - 2));
127   }
128 }
129 
130 template<>
131 template<>
132 inline void StackChunkFrameStream<ChunkFrames::CompiledOnly>::update_reg_map_pd(RegisterMap* map) {
133   if (map->update_map()) {
134     frame::update_map_with_saved_link(map, map->in_cont() ? (intptr_t**)2 : (intptr_t**)(_sp - 2));
135   }
136 }
137 
138 template <ChunkFrames frame_kind>
139 template <typename RegisterMapT>
140 inline void StackChunkFrameStream<frame_kind>::update_reg_map_pd(RegisterMapT* map) {}
141 
142 #endif // CPU_RISCV_STACKCHUNKFRAMESTREAM_RISCV_INLINE_HPP