1 /*
2  * Copyright (C) 2023 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 BERBERIS_BACKEND_X86_64_LOOP_GUEST_CONTEXT_OPTIMIZER_TEST_CHECKS_H_
18 #define BERBERIS_BACKEND_X86_64_LOOP_GUEST_CONTEXT_OPTIMIZER_TEST_CHECKS_H_
19 
20 #include "gtest/gtest.h"
21 
22 #include "berberis/backend/x86_64/loop_guest_context_optimizer.h"
23 #include "berberis/backend/x86_64/machine_ir.h"
24 #include "berberis/base/checks.h"
25 
26 namespace berberis::x86_64 {
27 
CheckCopyGetInsnAndObtainMappedReg(MachineInsn * get_insn,MachineReg expected_dst)28 inline MachineReg CheckCopyGetInsnAndObtainMappedReg(MachineInsn* get_insn,
29                                                      MachineReg expected_dst) {
30   EXPECT_EQ(get_insn->opcode(), kMachineOpPseudoCopy);
31   EXPECT_EQ(get_insn->RegAt(0), expected_dst);
32   return get_insn->RegAt(1);
33 }
34 
CheckCopyPutInsnAndObtainMappedReg(MachineInsn * put_insn,MachineReg expected_src)35 inline MachineReg CheckCopyPutInsnAndObtainMappedReg(MachineInsn* put_insn,
36                                                      MachineReg expected_src) {
37   EXPECT_EQ(put_insn->opcode(), kMachineOpPseudoCopy);
38   EXPECT_EQ(put_insn->RegAt(1), expected_src);
39   return put_insn->RegAt(0);
40 }
41 
CheckMemRegMap(MemRegMap mem_reg_map,size_t offset,MachineReg mapped_reg,MovType mov_type,bool is_modified)42 inline void CheckMemRegMap(MemRegMap mem_reg_map,
43                            size_t offset,
44                            MachineReg mapped_reg,
45                            MovType mov_type,
46                            bool is_modified) {
47   EXPECT_TRUE(mem_reg_map[offset].has_value());
48   EXPECT_EQ(mem_reg_map[offset].value().reg, mapped_reg);
49   EXPECT_EQ(mem_reg_map[offset].value().mov_type, mov_type);
50   EXPECT_EQ(mem_reg_map[offset].value().is_modified, is_modified);
51 }
52 
CheckGetInsn(MachineInsn * insn,MachineOpcode opcode,MachineReg reg,size_t disp)53 inline void CheckGetInsn(MachineInsn* insn, MachineOpcode opcode, MachineReg reg, size_t disp) {
54   auto get_insn = AsMachineInsnX86_64(insn);
55   EXPECT_TRUE(get_insn->IsCPUStateGet());
56   EXPECT_EQ(get_insn->opcode(), opcode);
57   EXPECT_EQ(get_insn->RegAt(0), reg);
58   EXPECT_EQ(get_insn->disp(), disp);
59 }
60 
CheckPutInsn(MachineInsn * insn,MachineOpcode opcode,MachineReg reg,size_t disp)61 inline void CheckPutInsn(MachineInsn* insn, MachineOpcode opcode, MachineReg reg, size_t disp) {
62   auto put_insn = AsMachineInsnX86_64(insn);
63   EXPECT_TRUE(put_insn->IsCPUStatePut());
64   EXPECT_EQ(put_insn->opcode(), opcode);
65   EXPECT_EQ(put_insn->RegAt(1), reg);
66   EXPECT_EQ(put_insn->disp(), disp);
67 }
68 
69 }  // namespace berberis::x86_64
70 
71 #endif  // BERBERIS_BACKEND_X86_64_LOOP_GUEST_CONTEXT_OPTIMIZER_TEST_CHECKS_H_
72