1 /*
 2  * Copyright (c) 2022, 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_AARCH64_FRAME_HELPERS_AARCH64_INLINE_HPP
26 #define CPU_AARCH64_FRAME_HELPERS_AARCH64_INLINE_HPP
27 
28 
29 #ifdef ASSERT
30 bool Frame::assert_frame_laid_out(frame f) {
31   intptr_t* sp = f.sp();
32   address pc = *(address*)(sp - frame::sender_sp_ret_address_offset());
33   intptr_t* fp = *(intptr_t**)(sp - frame::sender_sp_offset);
34   assert (f.raw_pc() == pc, "f.ra_pc: " INTPTR_FORMAT " actual: " INTPTR_FORMAT, p2i(f.raw_pc()), p2i(pc));
35   assert (f.fp() == fp, "f.fp: " INTPTR_FORMAT " actual: " INTPTR_FORMAT, p2i(f.fp()), p2i(fp));
36   return f.raw_pc() == pc && f.fp() == fp;
37 }
38 #endif
39 
40 inline intptr_t** Frame::callee_link_address(const frame& f) {
41   return (intptr_t**)(f.sp() - frame::sender_sp_offset);
42 }
43 
44 inline address* Frame::return_pc_address(const frame& f) {
45   return (address*)(f.real_fp() - 1);
46 }
47 
48 inline address* Interpreted::return_pc_address(const frame& f) {
49   return (address*)(f.fp() + frame::return_addr_offset);
50 }
51 
52 template <bool relative>
53 void Interpreted::patch_sender_sp(frame& f, intptr_t* sp) {
54   assert (f.is_interpreted_frame(), "");
55   intptr_t* la = f.addr_at(frame::interpreter_frame_sender_sp_offset);
56   *la = relative ? (intptr_t)(sp - f.fp()) : (intptr_t)sp;
57 }
58 
59 // inline address* Frame::pc_address(const frame& f) {
60 //   return (address*)(f.sp() - frame::return_addr_offset);
61 // }
62 
63 inline address Frame::real_pc(const frame& f) {
64   address* pc_addr = &(((address*) f.sp())[-1]);
65   return *pc_addr;
66 }
67 
68 inline void Frame::patch_pc(const frame& f, address pc) {
69   address* pc_addr = &(((address*) f.sp())[-1]);
70   *pc_addr = pc;
71 }
72 
73 inline intptr_t* Interpreted::frame_top(const frame& f, InterpreterOopMap* mask) { // inclusive; this will be copied with the frame
74   // interpreter_frame_last_sp_offset, points to unextended_sp includes arguments in the frame
75   // interpreter_frame_initial_sp_offset excludes expression stack slots
76   int expression_stack_sz = expression_stack_size(f, mask);
77   intptr_t* res = *(intptr_t**)f.addr_at(frame::interpreter_frame_initial_sp_offset) - expression_stack_sz;
78   assert (res == (intptr_t*)f.interpreter_frame_monitor_end() - expression_stack_sz, "");
79   assert (res >= f.unextended_sp(),
80     "res: " INTPTR_FORMAT " initial_sp: " INTPTR_FORMAT " last_sp: " INTPTR_FORMAT " unextended_sp: " INTPTR_FORMAT " expression_stack_size: %d", 
81     p2i(res), p2i(f.addr_at(frame::interpreter_frame_initial_sp_offset)), f.at(frame::interpreter_frame_last_sp_offset), p2i(f.unextended_sp()), expression_stack_sz);
82   return res;
83   // Not true, but using unextended_sp might work
84   // assert (res == f.unextended_sp(), "res: " INTPTR_FORMAT " unextended_sp: " INTPTR_FORMAT, p2i(res), p2i(f.unextended_sp() + 1));
85 }
86 
87 template <bool relative>
88 inline intptr_t* Interpreted::frame_bottom(const frame& f) { // exclusive; this will not be copied with the frame
89   return (intptr_t*)f.at<relative>(frame::interpreter_frame_locals_offset) + 1; // exclusive, so we add 1 word
90 }
91 
92 inline intptr_t* Interpreted::frame_top(const frame& f, int callee_argsize, bool callee_interpreted) {
93   return f.unextended_sp() + (callee_interpreted ? callee_argsize : 0);
94 }
95 
96 #endif // CPU_AARCH64_FRAME_HELPERS_AARCH64_INLINE_HPP