1 /*
2  * Copyright (C) 2014 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 #include <stdint.h>
18 
19 #include "art_method-inl.h"
20 #include "common_runtime_test.h"
21 #include "quick/quick_method_frame_info.h"
22 // Common tests are declared next to the constants.
23 #define ADD_TEST_EQ(x, y) EXPECT_EQ(x, y);
24 #include "asm_support.h"
25 
26 namespace art {
27 
28 class ArchTest : public CommonRuntimeTest {
29  protected:
SetUpRuntimeOptions(RuntimeOptions * options)30   void SetUpRuntimeOptions(RuntimeOptions *options) OVERRIDE {
31     // Use 64-bit ISA for runtime setup to make method size potentially larger
32     // than necessary (rather than smaller) during CreateCalleeSaveMethod
33     options->push_back(std::make_pair("imageinstructionset", "x86_64"));
34   }
35 
36   // Do not do any of the finalization. We don't want to run any code, we don't need the heap
37   // prepared, it actually will be a problem with setting the instruction set to x86_64 in
38   // SetUpRuntimeOptions.
FinalizeSetup()39   void FinalizeSetup() OVERRIDE {
40     ASSERT_EQ(InstructionSet::kX86_64, Runtime::Current()->GetInstructionSet());
41   }
42 
CheckFrameSize(InstructionSet isa,Runtime::CalleeSaveType type,uint32_t save_size)43   static void CheckFrameSize(InstructionSet isa, Runtime::CalleeSaveType type, uint32_t save_size)
44       NO_THREAD_SAFETY_ANALYSIS {
45     Runtime* const runtime = Runtime::Current();
46     Thread* const self = Thread::Current();
47     ScopedObjectAccess soa(self);  // So we can create callee-save methods.
48 
49     runtime->SetInstructionSet(isa);
50     ArtMethod* save_method = runtime->CreateCalleeSaveMethod();
51     runtime->SetCalleeSaveMethod(save_method, type);
52     QuickMethodFrameInfo frame_info =  runtime->GetRuntimeMethodFrameInfo(save_method);
53     EXPECT_EQ(frame_info.FrameSizeInBytes(), save_size) << "Expected and real size differs for "
54         << type << " core spills=" << std::hex << frame_info.CoreSpillMask() << " fp spills="
55         << frame_info.FpSpillMask() << std::dec;
56   }
57 };
58 
TEST_F(ArchTest,CheckCommonOffsetsAndSizes)59 TEST_F(ArchTest, CheckCommonOffsetsAndSizes) {
60   CheckAsmSupportOffsetsAndSizes();
61 }
62 
63 // Grab architecture specific constants.
64 namespace arm {
65 #include "arch/arm/asm_support_arm.h"
66 static constexpr size_t kFrameSizeSaveAllCalleeSaves = FRAME_SIZE_SAVE_ALL_CALLEE_SAVES;
67 #undef FRAME_SIZE_SAVE_ALL_CALLEE_SAVES
68 static constexpr size_t kFrameSizeSaveRefsOnly = FRAME_SIZE_SAVE_REFS_ONLY;
69 #undef FRAME_SIZE_SAVE_REFS_ONLY
70 static constexpr size_t kFrameSizeSaveRefsAndArgs = FRAME_SIZE_SAVE_REFS_AND_ARGS;
71 #undef FRAME_SIZE_SAVE_REFS_AND_ARGS
72 static constexpr size_t kFrameSizeSaveEverything = FRAME_SIZE_SAVE_EVERYTHING;
73 #undef FRAME_SIZE_SAVE_EVERYTHING
74 }  // namespace arm
75 
76 namespace arm64 {
77 #include "arch/arm64/asm_support_arm64.h"
78 static constexpr size_t kFrameSizeSaveAllCalleeSaves = FRAME_SIZE_SAVE_ALL_CALLEE_SAVES;
79 #undef FRAME_SIZE_SAVE_ALL_CALLEE_SAVES
80 static constexpr size_t kFrameSizeSaveRefsOnly = FRAME_SIZE_SAVE_REFS_ONLY;
81 #undef FRAME_SIZE_SAVE_REFS_ONLY
82 static constexpr size_t kFrameSizeSaveRefsAndArgs = FRAME_SIZE_SAVE_REFS_AND_ARGS;
83 #undef FRAME_SIZE_SAVE_REFS_AND_ARGS
84 static constexpr size_t kFrameSizeSaveEverything = FRAME_SIZE_SAVE_EVERYTHING;
85 #undef FRAME_SIZE_SAVE_EVERYTHING
86 }  // namespace arm64
87 
88 namespace mips {
89 #include "arch/mips/asm_support_mips.h"
90 static constexpr size_t kFrameSizeSaveAllCalleeSaves = FRAME_SIZE_SAVE_ALL_CALLEE_SAVES;
91 #undef FRAME_SIZE_SAVE_ALL_CALLEE_SAVES
92 static constexpr size_t kFrameSizeSaveRefsOnly = FRAME_SIZE_SAVE_REFS_ONLY;
93 #undef FRAME_SIZE_SAVE_REFS_ONLY
94 static constexpr size_t kFrameSizeSaveRefsAndArgs = FRAME_SIZE_SAVE_REFS_AND_ARGS;
95 #undef FRAME_SIZE_SAVE_REFS_AND_ARGS
96 static constexpr size_t kFrameSizeSaveEverything = FRAME_SIZE_SAVE_EVERYTHING;
97 #undef FRAME_SIZE_SAVE_EVERYTHING
98 }  // namespace mips
99 
100 namespace mips64 {
101 #include "arch/mips64/asm_support_mips64.h"
102 static constexpr size_t kFrameSizeSaveAllCalleeSaves = FRAME_SIZE_SAVE_ALL_CALLEE_SAVES;
103 #undef FRAME_SIZE_SAVE_ALL_CALLEE_SAVES
104 static constexpr size_t kFrameSizeSaveRefsOnly = FRAME_SIZE_SAVE_REFS_ONLY;
105 #undef FRAME_SIZE_SAVE_REFS_ONLY
106 static constexpr size_t kFrameSizeSaveRefsAndArgs = FRAME_SIZE_SAVE_REFS_AND_ARGS;
107 #undef FRAME_SIZE_SAVE_REFS_AND_ARGS
108 static constexpr size_t kFrameSizeSaveEverything = FRAME_SIZE_SAVE_EVERYTHING;
109 #undef FRAME_SIZE_SAVE_EVERYTHING
110 }  // namespace mips64
111 
112 namespace x86 {
113 #include "arch/x86/asm_support_x86.h"
114 static constexpr size_t kFrameSizeSaveAllCalleeSaves = FRAME_SIZE_SAVE_ALL_CALLEE_SAVES;
115 #undef FRAME_SIZE_SAVE_ALL_CALLEE_SAVES
116 static constexpr size_t kFrameSizeSaveRefsOnly = FRAME_SIZE_SAVE_REFS_ONLY;
117 #undef FRAME_SIZE_SAVE_REFS_ONLY
118 static constexpr size_t kFrameSizeSaveRefsAndArgs = FRAME_SIZE_SAVE_REFS_AND_ARGS;
119 #undef FRAME_SIZE_SAVE_REFS_AND_ARGS
120 static constexpr size_t kFrameSizeSaveEverything = FRAME_SIZE_SAVE_EVERYTHING;
121 #undef FRAME_SIZE_SAVE_EVERYTHING
122 }  // namespace x86
123 
124 namespace x86_64 {
125 #include "arch/x86_64/asm_support_x86_64.h"
126 static constexpr size_t kFrameSizeSaveAllCalleeSaves = FRAME_SIZE_SAVE_ALL_CALLEE_SAVES;
127 #undef FRAME_SIZE_SAVE_ALL_CALLEE_SAVES
128 static constexpr size_t kFrameSizeSaveRefsOnly = FRAME_SIZE_SAVE_REFS_ONLY;
129 #undef FRAME_SIZE_SAVE_REFS_ONLY
130 static constexpr size_t kFrameSizeSaveRefsAndArgs = FRAME_SIZE_SAVE_REFS_AND_ARGS;
131 #undef FRAME_SIZE_SAVE_REFS_AND_ARGS
132 static constexpr size_t kFrameSizeSaveEverything = FRAME_SIZE_SAVE_EVERYTHING;
133 #undef FRAME_SIZE_SAVE_EVERYTHING
134 }  // namespace x86_64
135 
136 // Check architecture specific constants are sound.
137 #define TEST_ARCH(Arch, arch)                             \
138   TEST_F(ArchTest, Arch) {                                \
139     CheckFrameSize(InstructionSet::k##Arch,               \
140                    Runtime::kSaveAllCalleeSaves,          \
141                    arch::kFrameSizeSaveAllCalleeSaves);   \
142     CheckFrameSize(InstructionSet::k##Arch,               \
143                    Runtime::kSaveRefsOnly,                \
144                    arch::kFrameSizeSaveRefsOnly);         \
145     CheckFrameSize(InstructionSet::k##Arch,               \
146                    Runtime::kSaveRefsAndArgs,             \
147                    arch::kFrameSizeSaveRefsAndArgs);      \
148     CheckFrameSize(InstructionSet::k##Arch,               \
149                    Runtime::kSaveEverything,              \
150                    arch::kFrameSizeSaveEverything);       \
151   }
152 TEST_ARCH(Arm, arm)
153 TEST_ARCH(Arm64, arm64)
154 TEST_ARCH(Mips, mips)
155 TEST_ARCH(Mips64, mips64)
156 TEST_ARCH(X86, x86)
157 TEST_ARCH(X86_64, x86_64)
158 
159 }  // namespace art
160