1 /*
2  * Copyright (C) 2015 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 "dex_instruction-inl.h"
18 #include "gtest/gtest.h"
19 
20 namespace art {
21 
TEST(StaticGetters,PropertiesOfNopTest)22 TEST(StaticGetters, PropertiesOfNopTest) {
23   Instruction::Code nop = Instruction::NOP;
24   EXPECT_STREQ("nop", Instruction::Name(nop));
25   EXPECT_EQ(Instruction::k10x, Instruction::FormatOf(nop));
26   EXPECT_EQ(Instruction::kIndexNone, Instruction::IndexTypeOf(nop));
27   EXPECT_EQ(Instruction::kContinue, Instruction::FlagsOf(nop));
28   EXPECT_EQ(Instruction::kVerifyNone, Instruction::VerifyFlagsOf(nop));
29 }
30 
Build45cc(uint8_t num_args,uint16_t method_idx,uint16_t proto_idx,uint16_t arg_regs,uint16_t * out)31 static void Build45cc(uint8_t num_args, uint16_t method_idx, uint16_t proto_idx,
32                       uint16_t arg_regs, uint16_t* out) {
33   // A = num argument registers
34   // B = method_idx
35   // C - G = argument registers
36   // H = proto_idx
37   //
38   // op = 0xFA
39   //
40   // format:
41   // AG op BBBB FEDC HHHH
42   out[0] = 0;
43   out[0] |= (num_args << 12);
44   out[0] |= 0x00FA;
45 
46   out[1] = method_idx;
47   out[2] = arg_regs;
48   out[3] = proto_idx;
49 }
50 
Build4rcc(uint16_t num_args,uint16_t method_idx,uint16_t proto_idx,uint16_t arg_regs_start,uint16_t * out)51 static void Build4rcc(uint16_t num_args, uint16_t method_idx, uint16_t proto_idx,
52                       uint16_t arg_regs_start, uint16_t* out) {
53   // A = num argument registers
54   // B = method_idx
55   // C = first argument register
56   // H = proto_idx
57   //
58   // op = 0xFB
59   //
60   // format:
61   // AA op BBBB CCCC HHHH
62   out[0] = 0;
63   out[0] |= (num_args << 8);
64   out[0] |= 0x00FB;
65 
66   out[1] = method_idx;
67   out[2] = arg_regs_start;
68   out[3] = proto_idx;
69 }
70 
TEST(Instruction,PropertiesOf45cc)71 TEST(Instruction, PropertiesOf45cc) {
72   uint16_t instruction[4];
73   Build45cc(4u /* num_vregs */, 16u /* method_idx */, 32u /* proto_idx */,
74             0xcafe /* arg_regs */, instruction);
75 
76   const Instruction* ins = Instruction::At(instruction);
77   ASSERT_EQ(4u, ins->SizeInCodeUnits());
78 
79   ASSERT_TRUE(ins->HasVRegA());
80   ASSERT_EQ(4, ins->VRegA());
81   ASSERT_EQ(4u, ins->VRegA_45cc());
82   ASSERT_EQ(4u, ins->VRegA_45cc(instruction[0]));
83 
84   ASSERT_TRUE(ins->HasVRegB());
85   ASSERT_EQ(16, ins->VRegB());
86   ASSERT_EQ(16u, ins->VRegB_45cc());
87 
88   ASSERT_TRUE(ins->HasVRegC());
89   ASSERT_EQ(0xe, ins->VRegC());
90   ASSERT_EQ(0xe, ins->VRegC_45cc());
91 
92   ASSERT_TRUE(ins->HasVRegH());
93   ASSERT_EQ(32, ins->VRegH());
94   ASSERT_EQ(32, ins->VRegH_45cc());
95 
96   ASSERT_TRUE(ins->HasVarArgs());
97 
98   uint32_t arg_regs[Instruction::kMaxVarArgRegs];
99   ins->GetVarArgs(arg_regs);
100   ASSERT_EQ(0xeu, arg_regs[0]);
101   ASSERT_EQ(0xfu, arg_regs[1]);
102   ASSERT_EQ(0xau, arg_regs[2]);
103   ASSERT_EQ(0xcu, arg_regs[3]);
104 }
105 
TEST(Instruction,PropertiesOf4rcc)106 TEST(Instruction, PropertiesOf4rcc) {
107   uint16_t instruction[4];
108   Build4rcc(4u /* num_vregs */, 16u /* method_idx */, 32u /* proto_idx */,
109             0xcafe /* arg_regs */, instruction);
110 
111   const Instruction* ins = Instruction::At(instruction);
112   ASSERT_EQ(4u, ins->SizeInCodeUnits());
113 
114   ASSERT_TRUE(ins->HasVRegA());
115   ASSERT_EQ(4, ins->VRegA());
116   ASSERT_EQ(4u, ins->VRegA_4rcc());
117   ASSERT_EQ(4u, ins->VRegA_4rcc(instruction[0]));
118 
119   ASSERT_TRUE(ins->HasVRegB());
120   ASSERT_EQ(16, ins->VRegB());
121   ASSERT_EQ(16u, ins->VRegB_4rcc());
122 
123   ASSERT_TRUE(ins->HasVRegC());
124   ASSERT_EQ(0xcafe, ins->VRegC());
125   ASSERT_EQ(0xcafe, ins->VRegC_4rcc());
126 
127   ASSERT_TRUE(ins->HasVRegH());
128   ASSERT_EQ(32, ins->VRegH());
129   ASSERT_EQ(32, ins->VRegH_4rcc());
130 
131   ASSERT_FALSE(ins->HasVarArgs());
132 }
133 
134 }  // namespace art
135