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_X86_JNI_FRAME_X86_H_
18 #define ART_RUNTIME_ARCH_X86_JNI_FRAME_X86_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 x86 {
30
31 constexpr size_t kFramePointerSize = static_cast<size_t>(PointerSize::k32);
32 static_assert(kX86PointerSize == PointerSize::k32, "Unexpected x86 pointer size");
33
34 static constexpr size_t kNativeStackAlignment = 16; // IA-32 cdecl requires 16 byte alignment.
35 static_assert(kNativeStackAlignment == kStackAlignment);
36
37 // Get the size of the arguments for a native call.
GetNativeOutArgsSize(size_t num_args,size_t num_long_or_double_args)38 inline size_t GetNativeOutArgsSize(size_t num_args, size_t num_long_or_double_args) {
39 size_t num_arg_words = num_args + num_long_or_double_args;
40 return num_arg_words * static_cast<size_t>(kX86PointerSize);
41 }
42
43 // Get stack args size for @CriticalNative method calls.
GetCriticalNativeCallArgsSize(std::string_view shorty)44 inline size_t GetCriticalNativeCallArgsSize(std::string_view shorty) {
45 size_t num_long_or_double_args =
46 std::count_if(shorty.begin() + 1, shorty.end(), [](char c) { return c == 'J' || c == 'D'; });
47
48 return GetNativeOutArgsSize(/*num_args=*/ shorty.length() - 1u, num_long_or_double_args);
49 }
50
51 // Get the frame size for @CriticalNative method stub.
52 // This must match the size of the frame emitted by the JNI compiler at the native call site.
GetCriticalNativeStubFrameSize(std::string_view shorty)53 inline size_t GetCriticalNativeStubFrameSize(std::string_view shorty) {
54 // The size of outgoing arguments.
55 size_t size = GetCriticalNativeCallArgsSize(shorty);
56
57 // We can make a tail call if there are no stack args and the return type is not
58 // FP type (needs moving from ST0 to MMX0) and we do not need to extend the result.
59 bool return_type_ok = shorty[0] == 'I' || shorty[0] == 'J' || shorty[0] == 'V';
60 if (return_type_ok && size == 0u) {
61 return 0u;
62 }
63
64 // Add return address size.
65 size += kFramePointerSize;
66 return RoundUp(size, kNativeStackAlignment);
67 }
68
69 // Get the frame size for direct call to a @CriticalNative method.
70 // This must match the size of the extra frame emitted by the compiler at the native call site.
GetCriticalNativeDirectCallFrameSize(std::string_view shorty)71 inline size_t GetCriticalNativeDirectCallFrameSize(std::string_view shorty) {
72 // The size of outgoing arguments.
73 size_t size = GetCriticalNativeCallArgsSize(shorty);
74
75 // No return PC to save, zero- and sign-extension and FP value moves are handled by the caller.
76 return RoundUp(size, kNativeStackAlignment);
77 }
78
79 } // namespace x86
80 } // namespace art
81
82 #endif // ART_RUNTIME_ARCH_X86_JNI_FRAME_X86_H_
83