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 "out args" for @CriticalNative method stub.
37 // 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)38 inline size_t GetCriticalNativeOutArgsSize(const char* shorty, uint32_t shorty_len) {
39   DCHECK_EQ(shorty_len, strlen(shorty));
40 
41   size_t num_long_or_double_args = 0u;
42   for (size_t i = 1; i != shorty_len; ++i) {
43     if (shorty[i] == 'J' || shorty[i] == 'D') {
44       num_long_or_double_args += 1u;
45     }
46   }
47   size_t num_arg_words = shorty_len - 1u + num_long_or_double_args;
48 
49   // The size of outgoing arguments.
50   size_t size = num_arg_words * static_cast<size_t>(kX86PointerSize);
51 
52   // Add return address size.
53   size += kFramePointerSize;
54   // We can make a tail call if there are no stack args and the return type is not
55   // FP type (needs moving from ST0 to MMX0) and we do not need to extend the result.
56   bool return_type_ok = shorty[0] == 'I' || shorty[0] == 'J' || shorty[0] == 'V';
57   if (return_type_ok && size == kFramePointerSize) {
58     return kFramePointerSize;
59   }
60 
61   return RoundUp(size, kNativeStackAlignment);
62 }
63 
64 }  // namespace x86
65 }  // namespace art
66 
67 #endif  // ART_RUNTIME_ARCH_X86_JNI_FRAME_X86_H_
68 
69