1 /*
2  * Copyright (C) 2011 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 "assembler_x86.h"
18 
19 #include "base/stl_util.h"
20 #include "utils/assembler_test.h"
21 
22 namespace art {
23 
TEST(AssemblerX86,CreateBuffer)24 TEST(AssemblerX86, CreateBuffer) {
25   AssemblerBuffer buffer;
26   AssemblerBuffer::EnsureCapacity ensured(&buffer);
27   buffer.Emit<uint8_t>(0x42);
28   ASSERT_EQ(static_cast<size_t>(1), buffer.Size());
29   buffer.Emit<int32_t>(42);
30   ASSERT_EQ(static_cast<size_t>(5), buffer.Size());
31 }
32 
33 class AssemblerX86Test : public AssemblerTest<x86::X86Assembler, x86::Register,
34                                               x86::XmmRegister, x86::Immediate> {
35  protected:
GetArchitectureString()36   std::string GetArchitectureString() OVERRIDE {
37     return "x86";
38   }
39 
GetAssemblerParameters()40   std::string GetAssemblerParameters() OVERRIDE {
41     return " --32";
42   }
43 
GetDisassembleParameters()44   std::string GetDisassembleParameters() OVERRIDE {
45     return " -D -bbinary -mi386 --no-show-raw-insn";
46   }
47 
SetUpHelpers()48   void SetUpHelpers() OVERRIDE {
49     if (registers_.size() == 0) {
50       registers_.insert(end(registers_),
51                         {  // NOLINT(whitespace/braces)
52                           new x86::Register(x86::EAX),
53                           new x86::Register(x86::EBX),
54                           new x86::Register(x86::ECX),
55                           new x86::Register(x86::EDX),
56                           new x86::Register(x86::EBP),
57                           new x86::Register(x86::ESP),
58                           new x86::Register(x86::ESI),
59                           new x86::Register(x86::EDI)
60                         });
61     }
62 
63     if (fp_registers_.size() == 0) {
64       fp_registers_.insert(end(fp_registers_),
65                            {  // NOLINT(whitespace/braces)
66                              new x86::XmmRegister(x86::XMM0),
67                              new x86::XmmRegister(x86::XMM1),
68                              new x86::XmmRegister(x86::XMM2),
69                              new x86::XmmRegister(x86::XMM3),
70                              new x86::XmmRegister(x86::XMM4),
71                              new x86::XmmRegister(x86::XMM5),
72                              new x86::XmmRegister(x86::XMM6),
73                              new x86::XmmRegister(x86::XMM7)
74                            });
75     }
76   }
77 
TearDown()78   void TearDown() OVERRIDE {
79     AssemblerTest::TearDown();
80     STLDeleteElements(&registers_);
81     STLDeleteElements(&fp_registers_);
82   }
83 
GetRegisters()84   std::vector<x86::Register*> GetRegisters() OVERRIDE {
85     return registers_;
86   }
87 
GetFPRegisters()88   std::vector<x86::XmmRegister*> GetFPRegisters() OVERRIDE {
89     return fp_registers_;
90   }
91 
CreateImmediate(int64_t imm_value)92   x86::Immediate CreateImmediate(int64_t imm_value) OVERRIDE {
93     return x86::Immediate(imm_value);
94   }
95 
96  private:
97   std::vector<x86::Register*> registers_;
98   std::vector<x86::XmmRegister*> fp_registers_;
99 };
100 
101 
TEST_F(AssemblerX86Test,Movl)102 TEST_F(AssemblerX86Test, Movl) {
103   GetAssembler()->movl(x86::EAX, x86::EBX);
104   const char* expected = "mov %ebx, %eax\n";
105   DriverStr(expected, "movl");
106 }
107 
TEST_F(AssemblerX86Test,psrlq)108 TEST_F(AssemblerX86Test, psrlq) {
109   GetAssembler()->psrlq(x86::XMM0, CreateImmediate(32));
110   const char* expected = "psrlq $0x20, %xmm0\n";
111   DriverStr(expected, "psrlq");
112 }
113 
TEST_F(AssemblerX86Test,punpckldq)114 TEST_F(AssemblerX86Test, punpckldq) {
115   GetAssembler()->punpckldq(x86::XMM0, x86::XMM1);
116   const char* expected = "punpckldq %xmm1, %xmm0\n";
117   DriverStr(expected, "punpckldq");
118 }
119 
TEST_F(AssemblerX86Test,LoadLongConstant)120 TEST_F(AssemblerX86Test, LoadLongConstant) {
121   GetAssembler()->LoadLongConstant(x86::XMM0, 51);
122   const char* expected =
123       "push $0x0\n"
124       "push $0x33\n"
125       "movsd 0(%esp), %xmm0\n"
126       "add $8, %esp\n";
127   DriverStr(expected, "LoadLongConstant");
128 }
129 
TEST_F(AssemblerX86Test,LockCmpxchgl)130 TEST_F(AssemblerX86Test, LockCmpxchgl) {
131   GetAssembler()->LockCmpxchgl(x86::Address(
132         x86::Register(x86::EDI), x86::Register(x86::EBX), x86::TIMES_4, 12),
133       x86::Register(x86::ESI));
134   GetAssembler()->LockCmpxchgl(x86::Address(
135         x86::Register(x86::EDI), x86::Register(x86::ESI), x86::TIMES_4, 12),
136       x86::Register(x86::ESI));
137   GetAssembler()->LockCmpxchgl(x86::Address(
138         x86::Register(x86::EDI), x86::Register(x86::ESI), x86::TIMES_4, 12),
139       x86::Register(x86::EDI));
140   GetAssembler()->LockCmpxchgl(x86::Address(
141       x86::Register(x86::EBP), 0), x86::Register(x86::ESI));
142   GetAssembler()->LockCmpxchgl(x86::Address(
143         x86::Register(x86::EBP), x86::Register(x86::ESI), x86::TIMES_1, 0),
144       x86::Register(x86::ESI));
145   const char* expected =
146     "lock cmpxchgl %ESI, 0xc(%EDI,%EBX,4)\n"
147     "lock cmpxchgl %ESI, 0xc(%EDI,%ESI,4)\n"
148     "lock cmpxchgl %EDI, 0xc(%EDI,%ESI,4)\n"
149     "lock cmpxchgl %ESI, (%EBP)\n"
150     "lock cmpxchgl %ESI, (%EBP,%ESI,1)\n";
151 
152   DriverStr(expected, "lock_cmpxchgl");
153 }
154 
TEST_F(AssemblerX86Test,LockCmpxchg8b)155 TEST_F(AssemblerX86Test, LockCmpxchg8b) {
156   GetAssembler()->LockCmpxchg8b(x86::Address(
157       x86::Register(x86::EDI), x86::Register(x86::EBX), x86::TIMES_4, 12));
158   GetAssembler()->LockCmpxchg8b(x86::Address(
159       x86::Register(x86::EDI), x86::Register(x86::ESI), x86::TIMES_4, 12));
160   GetAssembler()->LockCmpxchg8b(x86::Address(
161       x86::Register(x86::EDI), x86::Register(x86::ESI), x86::TIMES_4, 12));
162   GetAssembler()->LockCmpxchg8b(x86::Address(x86::Register(x86::EBP), 0));
163   GetAssembler()->LockCmpxchg8b(x86::Address(
164       x86::Register(x86::EBP), x86::Register(x86::ESI), x86::TIMES_1, 0));
165   const char* expected =
166     "lock cmpxchg8b 0xc(%EDI,%EBX,4)\n"
167     "lock cmpxchg8b 0xc(%EDI,%ESI,4)\n"
168     "lock cmpxchg8b 0xc(%EDI,%ESI,4)\n"
169     "lock cmpxchg8b (%EBP)\n"
170     "lock cmpxchg8b (%EBP,%ESI,1)\n";
171 
172   DriverStr(expected, "lock_cmpxchg8b");
173 }
174 
TEST_F(AssemblerX86Test,FPUIntegerLoad)175 TEST_F(AssemblerX86Test, FPUIntegerLoad) {
176   GetAssembler()->filds(x86::Address(x86::Register(x86::ESP), 4));
177   GetAssembler()->fildl(x86::Address(x86::Register(x86::ESP), 12));
178   const char* expected =
179       "fildl 0x4(%ESP)\n"
180       "fildll 0xc(%ESP)\n";
181   DriverStr(expected, "FPUIntegerLoad");
182 }
183 
TEST_F(AssemblerX86Test,FPUIntegerStore)184 TEST_F(AssemblerX86Test, FPUIntegerStore) {
185   GetAssembler()->fistps(x86::Address(x86::Register(x86::ESP), 16));
186   GetAssembler()->fistpl(x86::Address(x86::Register(x86::ESP), 24));
187   const char* expected =
188       "fistpl 0x10(%ESP)\n"
189       "fistpll 0x18(%ESP)\n";
190   DriverStr(expected, "FPUIntegerStore");
191 }
192 
TEST_F(AssemblerX86Test,Repnescasw)193 TEST_F(AssemblerX86Test, Repnescasw) {
194   GetAssembler()->repne_scasw();
195   const char* expected = "repne scasw\n";
196   DriverStr(expected, "Repnescasw");
197 }
198 
199 }  // namespace art
200