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