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
27 namespace art {
28 namespace arm {
29
30 constexpr size_t kFramePointerSize = static_cast<size_t>(PointerSize::k32);
31 static_assert(kArmPointerSize == PointerSize::k32, "Unexpected ARM pointer size");
32
33 // The AAPCS requires 8-byte alignement. This is not as strict as the Managed ABI stack alignment.
34 static constexpr size_t kAapcsStackAlignment = 8u;
35 static_assert(kAapcsStackAlignment < kStackAlignment);
36
37 // How many registers can be used for passing arguments.
38 // Note: AAPCS is soft-float, so these are all core registers.
39 constexpr size_t kJniArgumentRegisterCount = 4u;
40
41 // Get stack args size for @CriticalNative method calls.
GetCriticalNativeCallArgsSize(const char * shorty,uint32_t shorty_len)42 inline size_t GetCriticalNativeCallArgsSize(const char* shorty, uint32_t shorty_len) {
43 DCHECK_EQ(shorty_len, strlen(shorty));
44
45 size_t reg = 0; // Register for the current argument; if reg >= 4, we shall use stack.
46 for (size_t i = 1; i != shorty_len; ++i) {
47 if (shorty[i] == 'J' || shorty[i] == 'D') {
48 // 8-byte args need to start in even-numbered register or at aligned stack position.
49 reg += (reg & 1);
50 // Count first word and let the common path count the second.
51 reg += 1u;
52 }
53 reg += 1u;
54 }
55 size_t stack_args = std::max(reg, kJniArgumentRegisterCount) - kJniArgumentRegisterCount;
56 return kFramePointerSize * stack_args;
57 }
58
59 // Get the frame size for @CriticalNative method stub.
60 // This must match the size of the frame emitted by the JNI compiler at the native call site.
GetCriticalNativeStubFrameSize(const char * shorty,uint32_t shorty_len)61 inline size_t GetCriticalNativeStubFrameSize(const char* shorty, uint32_t shorty_len) {
62 // The size of outgoing arguments.
63 size_t size = GetCriticalNativeCallArgsSize(shorty, shorty_len);
64
65 // Check if this is a tail call, i.e. there are no stack args and the return type
66 // is not an FP type (otherwise we need to move the result to FP register).
67 // No need to sign/zero extend small return types thanks to AAPCS.
68 if (size != 0u || shorty[0] == 'F' || shorty[0] == 'D') {
69 size += kFramePointerSize; // We need to spill LR with the args.
70 }
71 return RoundUp(size, kAapcsStackAlignment);
72 }
73
74 // Get the frame size for direct call to a @CriticalNative method.
75 // This must match the size of the extra frame emitted by the compiler at the native call site.
GetCriticalNativeDirectCallFrameSize(const char * shorty,uint32_t shorty_len)76 inline size_t GetCriticalNativeDirectCallFrameSize(const char* shorty, uint32_t shorty_len) {
77 // The size of outgoing arguments.
78 size_t size = GetCriticalNativeCallArgsSize(shorty, shorty_len);
79
80 // No return PC to save, zero- and sign-extension and FP value moves are handled by the caller.
81 return RoundUp(size, kAapcsStackAlignment);
82 }
83
84 } // namespace arm
85 } // namespace art
86
87 #endif // ART_RUNTIME_ARCH_ARM_JNI_FRAME_ARM_H_
88
89