1 /*
2 * Copyright (c) 2019, 2025, 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_PPC_STACKCHUNKFRAMESTREAM_PPC_INLINE_HPP
26 #define CPU_PPC_STACKCHUNKFRAMESTREAM_PPC_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 assert(is_compiled(), "");
37 assert(!_cb->as_nmethod()->needs_stack_repair(), "unsupported");
38 intptr_t* p = (intptr_t*)p0;
39 int argsize = (_cb->as_nmethod()->num_stack_arg_slots() * VMRegImpl::stack_slot_size) >> LogBytesPerWord;
40 int frame_size = _cb->frame_size() + (argsize > 0 ? argsize + frame::metadata_words_at_top : 0);
41 return (p - unextended_sp()) >= 0 && (p - unextended_sp()) < frame_size;
42 }
43 #endif
44
45 template <ChunkFrames frame_kind>
46 inline frame StackChunkFrameStream<frame_kind>::to_frame() const {
47 if (is_done()) {
48 return frame(_sp, _sp, nullptr, nullptr, nullptr, nullptr, true);
49 } else {
50 // Compiled frames on heap don't have back links. See FreezeBase::patch_pd() and frame::setup().
51 return frame(sp(), unextended_sp(), Interpreter::contains(pc()) ? fp() : nullptr, pc(), cb(), _oopmap, true);
52 }
53 }
54
55 template <ChunkFrames frame_kind>
56 inline address StackChunkFrameStream<frame_kind>::get_pc() const {
57 assert(!is_done(), "");
58 return (address)((frame::common_abi*) _sp)->lr;
59 }
60
61 template <ChunkFrames frame_kind>
62 inline intptr_t* StackChunkFrameStream<frame_kind>::fp() const {
63 // See FreezeBase::patch_pd() and frame::setup()
64 assert((frame_kind == ChunkFrames::Mixed && is_interpreted()), "");
65 intptr_t* fp_addr = (intptr_t*)&((frame::common_abi*)_sp)->callers_sp;
66 assert(*(intptr_t**)fp_addr != nullptr, "");
67 // derelativize
68 return fp_addr + *fp_addr;
69 }
70
71 template <ChunkFrames frame_kind>
72 inline intptr_t* StackChunkFrameStream<frame_kind>::derelativize(int offset) const {
73 intptr_t* fp = this->fp();
74 assert(fp != nullptr, "");
75 return fp + fp[offset];
76 }
77
78 template <ChunkFrames frame_kind>
79 inline intptr_t* StackChunkFrameStream<frame_kind>::unextended_sp_for_interpreter_frame() const {
80 assert_is_interpreted_and_frame_type_mixed();
81 return derelativize(ijava_idx(esp)) + 1 - frame::metadata_words; // On PPC esp points to the next free slot
82 }
83
84 template <ChunkFrames frame_kind>
85 inline void StackChunkFrameStream<frame_kind>::next_for_interpreter_frame() {
86 assert_is_interpreted_and_frame_type_mixed();
87 if (derelativize(ijava_idx(locals)) + 1 >= _end) {
88 _unextended_sp = _end;
89 _sp = _end;
90 } else {
91 _unextended_sp = derelativize(ijava_idx(sender_sp));
92 _sp = this->fp();
93 }
94 }
95
96 // Details for the comment on StackChunkFrameStream<frame_kind>::frame_size()
97 //
98 // Interpreted caller frames get extended even if the callee is also
99 // interpreted. This is done to accomodate non-parameter locals.
100 //
101 // The size of a single frame is from the unextended sp to the bottom of the
102 // locals array. The combined size of caller/callee is the single size with the
103 // overlap deducted. The overlap is the size of the call parameters plus the
104 // size of the metadata at the sp (frame::metadata_words_at_top).
105 //
106 //
107 // Case 1: no metadata between a frame Case 2: metadata is located between
108 // and its locals a frame and its locals as on ppc64
109 //
110 // | | L0 aka P0 | | | L0 aka P0 |
111 // | | : : | | | : : |
112 // | | : Pn | | | : Pn |
113 // | | : | | | : |
114 // | | Lm | | | Lm |
115 // | ======================== | |----------------------|
116 // S0 | | Frame F0 | | | Metadata@top |
117 // | | | S0 | | |
118 // | | | | | |
119 // | |----------------------| | | |
120 // || | L0 aka P0 | | ========================
121 // over- || | : : | | | Frame F0 |
122 // lap || | : Pn |<- unext. SP | | |
123 // | | : | | | |<- bottom_of_locals
124 // | | Lm |<- SP | |----------------------|
125 // | ======================== || | L0 aka P0 |
126 // | | Frame F1 | || | : : |
127 // S1 | | | over- || | : Pn |<- unext. SP
128 // | | | lap || | : | + metadata_words_at_top
129 // | |----------------------| || | Lm |
130 // | | L0 aka P0 | || |----------------------|
131 // | | : : | || | Metadata@top |
132 // | | : Pn |<- unext. SP || | |<- unextended SP
133 // | : | | | |
134 // | Lm |<- SP | | |<- SP
135 // ======================== | ========================
136 // | | Frame F1 |
137 // | | |
138 // | | |
139 // | |----------------------|
140 // overlap = size of stackargs S1 | | L0 aka P0 |
141 // | | : : |
142 // | | : Pn |<- unext. SP
143 // | | : | + metadata_words_at_top
144 // | | Lm |
145 // | |----------------------|
146 // | | Metadata@top |
147 // | | |<- unextended SP
148 // | |
149 // | |<- SP
150 // ========================
151 //
152 // sizeof(Metadata@top) = frame::metadata_words_at_top
153 // bottom_of_locals = unext. sp + sizeof(Metadata@top) + stackargs
154 // overlap = bottom_of_locals - unext. sp
155 // = stackargs + sizeof(Metadata@top)
156 template <ChunkFrames frame_kind>
157 inline int StackChunkFrameStream<frame_kind>::interpreter_frame_size() const {
158 assert_is_interpreted_and_frame_type_mixed();
159 intptr_t* top = unextended_sp(); // later subtract argsize if callee is interpreted
160 intptr_t* bottom = derelativize(ijava_idx(locals)) + 1;
161 return (int)(bottom - top);
162 }
163
164 // Size of stack args in words (P0..Pn above). Only valid if the caller is also
165 // interpreted. The function is also called if the caller is compiled but the
166 // result is not used in that case (same on x86).
167 // See also setting of sender_sp in ContinuationHelper::InterpretedFrame::patch_sender_sp()
168 template <ChunkFrames frame_kind>
169 inline int StackChunkFrameStream<frame_kind>::interpreter_frame_stack_argsize() const {
170 assert_is_interpreted_and_frame_type_mixed();
171 frame::ijava_state* state = (frame::ijava_state*)((uintptr_t)fp() - frame::ijava_state_size);
172 int diff = (int)(state->locals - (state->sender_sp + frame::metadata_words_at_top) + 1);
173 assert(diff == -frame::metadata_words_at_top || ((Method*)state->method)->size_of_parameters() == diff,
174 "size_of_parameters(): %d diff: %d sp: " PTR_FORMAT " fp:" PTR_FORMAT,
175 ((Method*)state->method)->size_of_parameters(), diff, p2i(sp()), p2i(fp()));
176 return diff;
177 }
178
179 template <ChunkFrames frame_kind>
180 template <typename RegisterMapT>
181 inline int StackChunkFrameStream<frame_kind>::interpreter_frame_num_oops(RegisterMapT* map) const {
182 assert_is_interpreted_and_frame_type_mixed();
183 ResourceMark rm;
184 frame f = to_frame();
185 InterpreterOopCount closure;
186 f.oops_interpreted_do(&closure, map);
187 return closure.count();
188 }
189
190 template<>
191 template<>
192 inline void StackChunkFrameStream<ChunkFrames::Mixed>::update_reg_map_pd(RegisterMap* map) {
193 // Nothing to do (no non-volatile registers in java calling convention)
194 }
195
196 template<>
197 template<>
198 inline void StackChunkFrameStream<ChunkFrames::CompiledOnly>::update_reg_map_pd(RegisterMap* map) {
199 // Nothing to do (no non-volatile registers in java calling convention)
200 }
201
202 template <ChunkFrames frame_kind>
203 template <typename RegisterMapT>
204 inline void StackChunkFrameStream<frame_kind>::update_reg_map_pd(RegisterMapT* map) {}
205
206 #endif // CPU_PPC_STACKCHUNKFRAMESTREAM_PPC_INLINE_HPP