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 the size of "out args" for @CriticalNative method stub.
42 // This must match the size of the frame emitted by the JNI compiler at the native call site.
GetCriticalNativeOutArgsSize(const char * shorty,uint32_t shorty_len)43 inline size_t GetCriticalNativeOutArgsSize(const char* shorty, uint32_t shorty_len) {
44 DCHECK_EQ(shorty_len, strlen(shorty));
45
46 size_t reg = 0; // Register for the current argument; if reg >= 4, we shall use stack.
47 for (size_t i = 1; i != shorty_len; ++i) {
48 if (shorty[i] == 'J' || shorty[i] == 'D') {
49 // 8-byte args need to start in even-numbered register or at aligned stack position.
50 reg += (reg & 1);
51 // Count first word and let the common path count the second.
52 reg += 1u;
53 }
54 reg += 1u;
55 }
56 size_t stack_args = std::max(reg, kJniArgumentRegisterCount) - kJniArgumentRegisterCount;
57 size_t size = kFramePointerSize * stack_args;
58
59 // Check if this is a tail call, i.e. there are no stack args and the return type
60 // is not an FP type (otherwise we need to move the result to FP register).
61 // No need to sign/zero extend small return types thanks to AAPCS.
62 if (size != 0u || shorty[0] == 'F' || shorty[0] == 'D') {
63 size += kFramePointerSize; // We need to spill LR with the args.
64 }
65 return RoundUp(size, kAapcsStackAlignment);
66 }
67
68 } // namespace arm
69 } // namespace art
70
71 #endif // ART_RUNTIME_ARCH_ARM_JNI_FRAME_ARM_H_
72
73