1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ART_RUNTIME_ARCH_ARM_JNI_FRAME_ARM_H_
18 #define ART_RUNTIME_ARCH_ARM_JNI_FRAME_ARM_H_
19
20 #include <string.h>
21
22 #include "arch/instruction_set.h"
23 #include "base/bit_utils.h"
24 #include "base/globals.h"
25 #include "base/logging.h"
26 #include "base/macros.h"
27
28 namespace art HIDDEN {
29 namespace arm {
30
31 constexpr size_t kFramePointerSize = static_cast<size_t>(PointerSize::k32);
32 static_assert(kArmPointerSize == PointerSize::k32, "Unexpected ARM pointer size");
33
34 // The AAPCS requires 8-byte alignment. This is not as strict as the Managed ABI stack alignment.
35 static constexpr size_t kAapcsStackAlignment = 8u;
36 static_assert(kAapcsStackAlignment < kStackAlignment);
37
38 // How many registers can be used for passing arguments.
39 // Note: AAPCS is soft-float, so these are all core registers.
40 constexpr size_t kJniArgumentRegisterCount = 4u;
41
42 // Get stack args size for @CriticalNative method calls.
GetCriticalNativeCallArgsSize(std::string_view shorty)43 inline size_t GetCriticalNativeCallArgsSize(std::string_view shorty) {
44 size_t reg = 0; // Register for the current argument; if reg >= 4, we shall use stack.
45 for (size_t i = 1; i != shorty.length(); ++i) {
46 if (shorty[i] == 'J' || shorty[i] == 'D') {
47 // 8-byte args need to start in even-numbered register or at aligned stack position.
48 reg += (reg & 1);
49 // Count first word and let the common path count the second.
50 reg += 1u;
51 }
52 reg += 1u;
53 }
54 size_t stack_args = std::max(reg, kJniArgumentRegisterCount) - kJniArgumentRegisterCount;
55 return kFramePointerSize * stack_args;
56 }
57
58 // Get the frame size for @CriticalNative method stub.
59 // This must match the size of the frame emitted by the JNI compiler at the native call site.
GetCriticalNativeStubFrameSize(std::string_view shorty)60 inline size_t GetCriticalNativeStubFrameSize(std::string_view shorty) {
61 // The size of outgoing arguments.
62 size_t size = GetCriticalNativeCallArgsSize(shorty);
63
64 // Check if this is a tail call, i.e. there are no stack args and the return type
65 // is not an FP type (otherwise we need to move the result to FP register).
66 // No need to sign/zero extend small return types thanks to AAPCS.
67 if (size != 0u || shorty[0] == 'F' || shorty[0] == 'D') {
68 size += kFramePointerSize; // We need to spill LR with the args.
69 }
70 return RoundUp(size, kAapcsStackAlignment);
71 }
72
73 // Get the frame size for direct call to a @CriticalNative method.
74 // This must match the size of the extra frame emitted by the compiler at the native call site.
GetCriticalNativeDirectCallFrameSize(std::string_view shorty)75 inline size_t GetCriticalNativeDirectCallFrameSize(std::string_view shorty) {
76 // The size of outgoing arguments.
77 size_t size = GetCriticalNativeCallArgsSize(shorty);
78
79 // No return PC to save, zero- and sign-extension and FP value moves are handled by the caller.
80 return RoundUp(size, kAapcsStackAlignment);
81 }
82
83 } // namespace arm
84 } // namespace art
85
86 #endif // ART_RUNTIME_ARCH_ARM_JNI_FRAME_ARM_H_
87