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 "assembler_x86_64.h"
18 
19 #include <inttypes.h>
20 #include <map>
21 #include <random>
22 
23 #include "base/bit_utils.h"
24 #include "base/stl_util.h"
25 #include "jni_macro_assembler_x86_64.h"
26 #include "utils/assembler_test.h"
27 #include "utils/jni_macro_assembler_test.h"
28 
29 namespace art {
30 
TEST(AssemblerX86_64,CreateBuffer)31 TEST(AssemblerX86_64, CreateBuffer) {
32   ArenaPool pool;
33   ArenaAllocator arena(&pool);
34   AssemblerBuffer buffer(&arena);
35   AssemblerBuffer::EnsureCapacity ensured(&buffer);
36   buffer.Emit<uint8_t>(0x42);
37   ASSERT_EQ(static_cast<size_t>(1), buffer.Size());
38   buffer.Emit<int32_t>(42);
39   ASSERT_EQ(static_cast<size_t>(5), buffer.Size());
40 }
41 
42 #ifdef ART_TARGET_ANDROID
43 static constexpr size_t kRandomIterations = 1000;  // Devices might be puny, don't stress them...
44 #else
45 static constexpr size_t kRandomIterations = 100000;  // Hosts are pretty powerful.
46 #endif
47 
TEST(AssemblerX86_64,SignExtension)48 TEST(AssemblerX86_64, SignExtension) {
49   // 32bit.
50   for (int32_t i = 0; i < 128; i++) {
51     EXPECT_TRUE(IsInt<8>(i)) << i;
52   }
53   for (int32_t i = 128; i < 255; i++) {
54     EXPECT_FALSE(IsInt<8>(i)) << i;
55   }
56   // Do some higher ones randomly.
57   std::random_device rd;
58   std::default_random_engine e1(rd());
59   std::uniform_int_distribution<int32_t> uniform_dist(256, INT32_MAX);
60   for (size_t i = 0; i < kRandomIterations; i++) {
61     int32_t value = uniform_dist(e1);
62     EXPECT_FALSE(IsInt<8>(value)) << value;
63   }
64 
65   // Negative ones.
66   for (int32_t i = -1; i >= -128; i--) {
67     EXPECT_TRUE(IsInt<8>(i)) << i;
68   }
69 
70   for (int32_t i = -129; i > -256; i--) {
71     EXPECT_FALSE(IsInt<8>(i)) << i;
72   }
73 
74   // Do some lower ones randomly.
75   std::uniform_int_distribution<int32_t> uniform_dist2(INT32_MIN, -256);
76   for (size_t i = 0; i < 100; i++) {
77     int32_t value = uniform_dist2(e1);
78     EXPECT_FALSE(IsInt<8>(value)) << value;
79   }
80 
81   // 64bit.
82   for (int64_t i = 0; i < 128; i++) {
83     EXPECT_TRUE(IsInt<8>(i)) << i;
84   }
85   for (int32_t i = 128; i < 255; i++) {
86     EXPECT_FALSE(IsInt<8>(i)) << i;
87   }
88   // Do some higher ones randomly.
89   std::uniform_int_distribution<int64_t> uniform_dist3(256, INT64_MAX);
90   for (size_t i = 0; i < 100; i++) {
91     int64_t value = uniform_dist3(e1);
92     EXPECT_FALSE(IsInt<8>(value)) << value;
93   }
94 
95   // Negative ones.
96   for (int64_t i = -1; i >= -128; i--) {
97     EXPECT_TRUE(IsInt<8>(i)) << i;
98   }
99 
100   for (int64_t i = -129; i > -256; i--) {
101     EXPECT_FALSE(IsInt<8>(i)) << i;
102   }
103 
104   // Do some lower ones randomly.
105   std::uniform_int_distribution<int64_t> uniform_dist4(INT64_MIN, -256);
106   for (size_t i = 0; i < kRandomIterations; i++) {
107     int64_t value = uniform_dist4(e1);
108     EXPECT_FALSE(IsInt<8>(value)) << value;
109   }
110 
111   int64_t value = INT64_C(0x1200000010);
112   x86_64::Immediate imm(value);
113   EXPECT_FALSE(imm.is_int8());
114   EXPECT_FALSE(imm.is_int16());
115   EXPECT_FALSE(imm.is_int32());
116   value = INT64_C(0x8000000000000001);
117   x86_64::Immediate imm2(value);
118   EXPECT_FALSE(imm2.is_int8());
119   EXPECT_FALSE(imm2.is_int16());
120   EXPECT_FALSE(imm2.is_int32());
121 }
122 
123 struct X86_64CpuRegisterCompare {
operator ()art::X86_64CpuRegisterCompare124     bool operator()(const x86_64::CpuRegister& a, const x86_64::CpuRegister& b) const {
125         return a.AsRegister() < b.AsRegister();
126     }
127 };
128 
129 class AssemblerX86_64Test : public AssemblerTest<x86_64::X86_64Assembler, x86_64::CpuRegister,
130                                                  x86_64::XmmRegister, x86_64::Immediate> {
131  public:
132   typedef AssemblerTest<x86_64::X86_64Assembler, x86_64::CpuRegister,
133                         x86_64::XmmRegister, x86_64::Immediate> Base;
134 
135  protected:
136   // Get the typically used name for this architecture, e.g., aarch64, x86-64, ...
GetArchitectureString()137   std::string GetArchitectureString() OVERRIDE {
138     return "x86_64";
139   }
140 
GetDisassembleParameters()141   std::string GetDisassembleParameters() OVERRIDE {
142     return " -D -bbinary -mi386:x86-64 -Mx86-64,addr64,data32 --no-show-raw-insn";
143   }
144 
SetUpHelpers()145   void SetUpHelpers() OVERRIDE {
146     if (registers_.size() == 0) {
147       registers_.push_back(new x86_64::CpuRegister(x86_64::RAX));
148       registers_.push_back(new x86_64::CpuRegister(x86_64::RBX));
149       registers_.push_back(new x86_64::CpuRegister(x86_64::RCX));
150       registers_.push_back(new x86_64::CpuRegister(x86_64::RDX));
151       registers_.push_back(new x86_64::CpuRegister(x86_64::RBP));
152       registers_.push_back(new x86_64::CpuRegister(x86_64::RSP));
153       registers_.push_back(new x86_64::CpuRegister(x86_64::RSI));
154       registers_.push_back(new x86_64::CpuRegister(x86_64::RDI));
155       registers_.push_back(new x86_64::CpuRegister(x86_64::R8));
156       registers_.push_back(new x86_64::CpuRegister(x86_64::R9));
157       registers_.push_back(new x86_64::CpuRegister(x86_64::R10));
158       registers_.push_back(new x86_64::CpuRegister(x86_64::R11));
159       registers_.push_back(new x86_64::CpuRegister(x86_64::R12));
160       registers_.push_back(new x86_64::CpuRegister(x86_64::R13));
161       registers_.push_back(new x86_64::CpuRegister(x86_64::R14));
162       registers_.push_back(new x86_64::CpuRegister(x86_64::R15));
163 
164       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::RAX), "eax");
165       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::RBX), "ebx");
166       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::RCX), "ecx");
167       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::RDX), "edx");
168       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::RBP), "ebp");
169       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::RSP), "esp");
170       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::RSI), "esi");
171       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::RDI), "edi");
172       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::R8), "r8d");
173       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::R9), "r9d");
174       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::R10), "r10d");
175       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::R11), "r11d");
176       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::R12), "r12d");
177       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::R13), "r13d");
178       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::R14), "r14d");
179       secondary_register_names_.emplace(x86_64::CpuRegister(x86_64::R15), "r15d");
180 
181       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::RAX), "ax");
182       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::RBX), "bx");
183       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::RCX), "cx");
184       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::RDX), "dx");
185       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::RBP), "bp");
186       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::RSP), "sp");
187       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::RSI), "si");
188       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::RDI), "di");
189       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::R8), "r8w");
190       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::R9), "r9w");
191       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::R10), "r10w");
192       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::R11), "r11w");
193       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::R12), "r12w");
194       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::R13), "r13w");
195       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::R14), "r14w");
196       tertiary_register_names_.emplace(x86_64::CpuRegister(x86_64::R15), "r15w");
197 
198       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::RAX), "al");
199       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::RBX), "bl");
200       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::RCX), "cl");
201       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::RDX), "dl");
202       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::RBP), "bpl");
203       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::RSP), "spl");
204       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::RSI), "sil");
205       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::RDI), "dil");
206       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::R8), "r8b");
207       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::R9), "r9b");
208       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::R10), "r10b");
209       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::R11), "r11b");
210       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::R12), "r12b");
211       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::R13), "r13b");
212       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::R14), "r14b");
213       quaternary_register_names_.emplace(x86_64::CpuRegister(x86_64::R15), "r15b");
214 
215       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM0));
216       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM1));
217       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM2));
218       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM3));
219       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM4));
220       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM5));
221       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM6));
222       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM7));
223       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM8));
224       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM9));
225       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM10));
226       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM11));
227       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM12));
228       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM13));
229       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM14));
230       fp_registers_.push_back(new x86_64::XmmRegister(x86_64::XMM15));
231     }
232   }
233 
TearDown()234   void TearDown() OVERRIDE {
235     AssemblerTest::TearDown();
236     STLDeleteElements(&registers_);
237     STLDeleteElements(&fp_registers_);
238   }
239 
GetRegisters()240   std::vector<x86_64::CpuRegister*> GetRegisters() OVERRIDE {
241     return registers_;
242   }
243 
GetFPRegisters()244   std::vector<x86_64::XmmRegister*> GetFPRegisters() OVERRIDE {
245     return fp_registers_;
246   }
247 
CreateImmediate(int64_t imm_value)248   x86_64::Immediate CreateImmediate(int64_t imm_value) OVERRIDE {
249     return x86_64::Immediate(imm_value);
250   }
251 
GetSecondaryRegisterName(const x86_64::CpuRegister & reg)252   std::string GetSecondaryRegisterName(const x86_64::CpuRegister& reg) OVERRIDE {
253     CHECK(secondary_register_names_.find(reg) != secondary_register_names_.end());
254     return secondary_register_names_[reg];
255   }
256 
GetTertiaryRegisterName(const x86_64::CpuRegister & reg)257   std::string GetTertiaryRegisterName(const x86_64::CpuRegister& reg) OVERRIDE {
258     CHECK(tertiary_register_names_.find(reg) != tertiary_register_names_.end());
259     return tertiary_register_names_[reg];
260   }
261 
GetQuaternaryRegisterName(const x86_64::CpuRegister & reg)262   std::string GetQuaternaryRegisterName(const x86_64::CpuRegister& reg) OVERRIDE {
263     CHECK(quaternary_register_names_.find(reg) != quaternary_register_names_.end());
264     return quaternary_register_names_[reg];
265   }
266 
267  private:
268   std::vector<x86_64::CpuRegister*> registers_;
269   std::map<x86_64::CpuRegister, std::string, X86_64CpuRegisterCompare> secondary_register_names_;
270   std::map<x86_64::CpuRegister, std::string, X86_64CpuRegisterCompare> tertiary_register_names_;
271   std::map<x86_64::CpuRegister, std::string, X86_64CpuRegisterCompare> quaternary_register_names_;
272 
273   std::vector<x86_64::XmmRegister*> fp_registers_;
274 };
275 
276 
TEST_F(AssemblerX86_64Test,Toolchain)277 TEST_F(AssemblerX86_64Test, Toolchain) {
278   EXPECT_TRUE(CheckTools());
279 }
280 
281 
TEST_F(AssemblerX86_64Test,PushqRegs)282 TEST_F(AssemblerX86_64Test, PushqRegs) {
283   DriverStr(RepeatR(&x86_64::X86_64Assembler::pushq, "pushq %{reg}"), "pushq");
284 }
285 
TEST_F(AssemblerX86_64Test,PushqImm)286 TEST_F(AssemblerX86_64Test, PushqImm) {
287   DriverStr(RepeatI(&x86_64::X86_64Assembler::pushq, 4U, "pushq ${imm}"), "pushqi");
288 }
289 
TEST_F(AssemblerX86_64Test,MovqRegs)290 TEST_F(AssemblerX86_64Test, MovqRegs) {
291   DriverStr(RepeatRR(&x86_64::X86_64Assembler::movq, "movq %{reg2}, %{reg1}"), "movq");
292 }
293 
TEST_F(AssemblerX86_64Test,MovqImm)294 TEST_F(AssemblerX86_64Test, MovqImm) {
295   DriverStr(RepeatRI(&x86_64::X86_64Assembler::movq, 8U, "movq ${imm}, %{reg}"), "movqi");
296 }
297 
TEST_F(AssemblerX86_64Test,MovlRegs)298 TEST_F(AssemblerX86_64Test, MovlRegs) {
299   DriverStr(Repeatrr(&x86_64::X86_64Assembler::movl, "mov %{reg2}, %{reg1}"), "movl");
300 }
301 
TEST_F(AssemblerX86_64Test,MovlImm)302 TEST_F(AssemblerX86_64Test, MovlImm) {
303   DriverStr(Repeatri(&x86_64::X86_64Assembler::movl, 4U, "mov ${imm}, %{reg}"), "movli");
304 }
305 
TEST_F(AssemblerX86_64Test,AddqRegs)306 TEST_F(AssemblerX86_64Test, AddqRegs) {
307   DriverStr(RepeatRR(&x86_64::X86_64Assembler::addq, "addq %{reg2}, %{reg1}"), "addq");
308 }
309 
TEST_F(AssemblerX86_64Test,AddqImm)310 TEST_F(AssemblerX86_64Test, AddqImm) {
311   DriverStr(RepeatRI(&x86_64::X86_64Assembler::addq, 4U, "addq ${imm}, %{reg}"), "addqi");
312 }
313 
TEST_F(AssemblerX86_64Test,AddlRegs)314 TEST_F(AssemblerX86_64Test, AddlRegs) {
315   DriverStr(Repeatrr(&x86_64::X86_64Assembler::addl, "add %{reg2}, %{reg1}"), "addl");
316 }
317 
TEST_F(AssemblerX86_64Test,AddlImm)318 TEST_F(AssemblerX86_64Test, AddlImm) {
319   DriverStr(Repeatri(&x86_64::X86_64Assembler::addl, 4U, "add ${imm}, %{reg}"), "addli");
320 }
321 
TEST_F(AssemblerX86_64Test,ImulqReg1)322 TEST_F(AssemblerX86_64Test, ImulqReg1) {
323   DriverStr(RepeatR(&x86_64::X86_64Assembler::imulq, "imulq %{reg}"), "imulq");
324 }
325 
TEST_F(AssemblerX86_64Test,ImulqRegs)326 TEST_F(AssemblerX86_64Test, ImulqRegs) {
327   DriverStr(RepeatRR(&x86_64::X86_64Assembler::imulq, "imulq %{reg2}, %{reg1}"), "imulq");
328 }
329 
TEST_F(AssemblerX86_64Test,ImulqImm)330 TEST_F(AssemblerX86_64Test, ImulqImm) {
331   DriverStr(RepeatRI(&x86_64::X86_64Assembler::imulq, 4U, "imulq ${imm}, %{reg}, %{reg}"),
332             "imulqi");
333 }
334 
TEST_F(AssemblerX86_64Test,ImullRegs)335 TEST_F(AssemblerX86_64Test, ImullRegs) {
336   DriverStr(Repeatrr(&x86_64::X86_64Assembler::imull, "imul %{reg2}, %{reg1}"), "imull");
337 }
338 
TEST_F(AssemblerX86_64Test,ImullImm)339 TEST_F(AssemblerX86_64Test, ImullImm) {
340   DriverStr(Repeatri(&x86_64::X86_64Assembler::imull, 4U, "imull ${imm}, %{reg}, %{reg}"),
341             "imulli");
342 }
343 
TEST_F(AssemblerX86_64Test,Mull)344 TEST_F(AssemblerX86_64Test, Mull) {
345   DriverStr(Repeatr(&x86_64::X86_64Assembler::mull, "mull %{reg}"), "mull");
346 }
347 
TEST_F(AssemblerX86_64Test,SubqRegs)348 TEST_F(AssemblerX86_64Test, SubqRegs) {
349   DriverStr(RepeatRR(&x86_64::X86_64Assembler::subq, "subq %{reg2}, %{reg1}"), "subq");
350 }
351 
TEST_F(AssemblerX86_64Test,SubqImm)352 TEST_F(AssemblerX86_64Test, SubqImm) {
353   DriverStr(RepeatRI(&x86_64::X86_64Assembler::subq, 4U, "subq ${imm}, %{reg}"), "subqi");
354 }
355 
TEST_F(AssemblerX86_64Test,SublRegs)356 TEST_F(AssemblerX86_64Test, SublRegs) {
357   DriverStr(Repeatrr(&x86_64::X86_64Assembler::subl, "sub %{reg2}, %{reg1}"), "subl");
358 }
359 
TEST_F(AssemblerX86_64Test,SublImm)360 TEST_F(AssemblerX86_64Test, SublImm) {
361   DriverStr(Repeatri(&x86_64::X86_64Assembler::subl, 4U, "sub ${imm}, %{reg}"), "subli");
362 }
363 
364 // Shll only allows CL as the shift count.
shll_fn(AssemblerX86_64Test::Base * assembler_test,x86_64::X86_64Assembler * assembler)365 std::string shll_fn(AssemblerX86_64Test::Base* assembler_test, x86_64::X86_64Assembler* assembler) {
366   std::ostringstream str;
367 
368   std::vector<x86_64::CpuRegister*> registers = assembler_test->GetRegisters();
369 
370   x86_64::CpuRegister shifter(x86_64::RCX);
371   for (auto reg : registers) {
372     assembler->shll(*reg, shifter);
373     str << "shll %cl, %" << assembler_test->GetSecondaryRegisterName(*reg) << "\n";
374   }
375 
376   return str.str();
377 }
378 
TEST_F(AssemblerX86_64Test,ShllReg)379 TEST_F(AssemblerX86_64Test, ShllReg) {
380   DriverFn(&shll_fn, "shll");
381 }
382 
TEST_F(AssemblerX86_64Test,ShllImm)383 TEST_F(AssemblerX86_64Test, ShllImm) {
384   DriverStr(Repeatri(&x86_64::X86_64Assembler::shll, 1U, "shll ${imm}, %{reg}"), "shlli");
385 }
386 
387 // Shlq only allows CL as the shift count.
shlq_fn(AssemblerX86_64Test::Base * assembler_test,x86_64::X86_64Assembler * assembler)388 std::string shlq_fn(AssemblerX86_64Test::Base* assembler_test, x86_64::X86_64Assembler* assembler) {
389   std::ostringstream str;
390 
391   std::vector<x86_64::CpuRegister*> registers = assembler_test->GetRegisters();
392 
393   x86_64::CpuRegister shifter(x86_64::RCX);
394   for (auto reg : registers) {
395     assembler->shlq(*reg, shifter);
396     str << "shlq %cl, %" << assembler_test->GetRegisterName(*reg) << "\n";
397   }
398 
399   return str.str();
400 }
401 
TEST_F(AssemblerX86_64Test,ShlqReg)402 TEST_F(AssemblerX86_64Test, ShlqReg) {
403   DriverFn(&shlq_fn, "shlq");
404 }
405 
TEST_F(AssemblerX86_64Test,ShlqImm)406 TEST_F(AssemblerX86_64Test, ShlqImm) {
407   DriverStr(RepeatRI(&x86_64::X86_64Assembler::shlq, 1U, "shlq ${imm}, %{reg}"), "shlqi");
408 }
409 
410 // Shrl only allows CL as the shift count.
shrl_fn(AssemblerX86_64Test::Base * assembler_test,x86_64::X86_64Assembler * assembler)411 std::string shrl_fn(AssemblerX86_64Test::Base* assembler_test, x86_64::X86_64Assembler* assembler) {
412   std::ostringstream str;
413 
414   std::vector<x86_64::CpuRegister*> registers = assembler_test->GetRegisters();
415 
416   x86_64::CpuRegister shifter(x86_64::RCX);
417   for (auto reg : registers) {
418     assembler->shrl(*reg, shifter);
419     str << "shrl %cl, %" << assembler_test->GetSecondaryRegisterName(*reg) << "\n";
420   }
421 
422   return str.str();
423 }
424 
TEST_F(AssemblerX86_64Test,ShrlReg)425 TEST_F(AssemblerX86_64Test, ShrlReg) {
426   DriverFn(&shrl_fn, "shrl");
427 }
428 
TEST_F(AssemblerX86_64Test,ShrlImm)429 TEST_F(AssemblerX86_64Test, ShrlImm) {
430   DriverStr(Repeatri(&x86_64::X86_64Assembler::shrl, 1U, "shrl ${imm}, %{reg}"), "shrli");
431 }
432 
433 // Shrq only allows CL as the shift count.
shrq_fn(AssemblerX86_64Test::Base * assembler_test,x86_64::X86_64Assembler * assembler)434 std::string shrq_fn(AssemblerX86_64Test::Base* assembler_test, x86_64::X86_64Assembler* assembler) {
435   std::ostringstream str;
436 
437   std::vector<x86_64::CpuRegister*> registers = assembler_test->GetRegisters();
438 
439   x86_64::CpuRegister shifter(x86_64::RCX);
440   for (auto reg : registers) {
441     assembler->shrq(*reg, shifter);
442     str << "shrq %cl, %" << assembler_test->GetRegisterName(*reg) << "\n";
443   }
444 
445   return str.str();
446 }
447 
TEST_F(AssemblerX86_64Test,ShrqReg)448 TEST_F(AssemblerX86_64Test, ShrqReg) {
449   DriverFn(&shrq_fn, "shrq");
450 }
451 
TEST_F(AssemblerX86_64Test,ShrqImm)452 TEST_F(AssemblerX86_64Test, ShrqImm) {
453   DriverStr(RepeatRI(&x86_64::X86_64Assembler::shrq, 1U, "shrq ${imm}, %{reg}"), "shrqi");
454 }
455 
456 // Sarl only allows CL as the shift count.
sarl_fn(AssemblerX86_64Test::Base * assembler_test,x86_64::X86_64Assembler * assembler)457 std::string sarl_fn(AssemblerX86_64Test::Base* assembler_test, x86_64::X86_64Assembler* assembler) {
458   std::ostringstream str;
459 
460   std::vector<x86_64::CpuRegister*> registers = assembler_test->GetRegisters();
461 
462   x86_64::CpuRegister shifter(x86_64::RCX);
463   for (auto reg : registers) {
464     assembler->sarl(*reg, shifter);
465     str << "sarl %cl, %" << assembler_test->GetSecondaryRegisterName(*reg) << "\n";
466   }
467 
468   return str.str();
469 }
470 
TEST_F(AssemblerX86_64Test,SarlReg)471 TEST_F(AssemblerX86_64Test, SarlReg) {
472   DriverFn(&sarl_fn, "sarl");
473 }
474 
TEST_F(AssemblerX86_64Test,SarlImm)475 TEST_F(AssemblerX86_64Test, SarlImm) {
476   DriverStr(Repeatri(&x86_64::X86_64Assembler::sarl, 1U, "sarl ${imm}, %{reg}"), "sarli");
477 }
478 
479 // Sarq only allows CL as the shift count.
sarq_fn(AssemblerX86_64Test::Base * assembler_test,x86_64::X86_64Assembler * assembler)480 std::string sarq_fn(AssemblerX86_64Test::Base* assembler_test, x86_64::X86_64Assembler* assembler) {
481   std::ostringstream str;
482 
483   std::vector<x86_64::CpuRegister*> registers = assembler_test->GetRegisters();
484 
485   x86_64::CpuRegister shifter(x86_64::RCX);
486   for (auto reg : registers) {
487     assembler->sarq(*reg, shifter);
488     str << "sarq %cl, %" << assembler_test->GetRegisterName(*reg) << "\n";
489   }
490 
491   return str.str();
492 }
493 
TEST_F(AssemblerX86_64Test,SarqReg)494 TEST_F(AssemblerX86_64Test, SarqReg) {
495   DriverFn(&sarq_fn, "sarq");
496 }
497 
TEST_F(AssemblerX86_64Test,SarqImm)498 TEST_F(AssemblerX86_64Test, SarqImm) {
499   DriverStr(RepeatRI(&x86_64::X86_64Assembler::sarq, 1U, "sarq ${imm}, %{reg}"), "sarqi");
500 }
501 
502 // Rorl only allows CL as the shift count.
rorl_fn(AssemblerX86_64Test::Base * assembler_test,x86_64::X86_64Assembler * assembler)503 std::string rorl_fn(AssemblerX86_64Test::Base* assembler_test, x86_64::X86_64Assembler* assembler) {
504   std::ostringstream str;
505 
506   std::vector<x86_64::CpuRegister*> registers = assembler_test->GetRegisters();
507 
508   x86_64::CpuRegister shifter(x86_64::RCX);
509   for (auto reg : registers) {
510     assembler->rorl(*reg, shifter);
511     str << "rorl %cl, %" << assembler_test->GetSecondaryRegisterName(*reg) << "\n";
512   }
513 
514   return str.str();
515 }
516 
TEST_F(AssemblerX86_64Test,RorlReg)517 TEST_F(AssemblerX86_64Test, RorlReg) {
518   DriverFn(&rorl_fn, "rorl");
519 }
520 
TEST_F(AssemblerX86_64Test,RorlImm)521 TEST_F(AssemblerX86_64Test, RorlImm) {
522   DriverStr(Repeatri(&x86_64::X86_64Assembler::rorl, 1U, "rorl ${imm}, %{reg}"), "rorli");
523 }
524 
525 // Roll only allows CL as the shift count.
roll_fn(AssemblerX86_64Test::Base * assembler_test,x86_64::X86_64Assembler * assembler)526 std::string roll_fn(AssemblerX86_64Test::Base* assembler_test, x86_64::X86_64Assembler* assembler) {
527   std::ostringstream str;
528 
529   std::vector<x86_64::CpuRegister*> registers = assembler_test->GetRegisters();
530 
531   x86_64::CpuRegister shifter(x86_64::RCX);
532   for (auto reg : registers) {
533     assembler->roll(*reg, shifter);
534     str << "roll %cl, %" << assembler_test->GetSecondaryRegisterName(*reg) << "\n";
535   }
536 
537   return str.str();
538 }
539 
TEST_F(AssemblerX86_64Test,RollReg)540 TEST_F(AssemblerX86_64Test, RollReg) {
541   DriverFn(&roll_fn, "roll");
542 }
543 
TEST_F(AssemblerX86_64Test,RollImm)544 TEST_F(AssemblerX86_64Test, RollImm) {
545   DriverStr(Repeatri(&x86_64::X86_64Assembler::roll, 1U, "roll ${imm}, %{reg}"), "rolli");
546 }
547 
548 // Rorq only allows CL as the shift count.
rorq_fn(AssemblerX86_64Test::Base * assembler_test,x86_64::X86_64Assembler * assembler)549 std::string rorq_fn(AssemblerX86_64Test::Base* assembler_test, x86_64::X86_64Assembler* assembler) {
550   std::ostringstream str;
551 
552   std::vector<x86_64::CpuRegister*> registers = assembler_test->GetRegisters();
553 
554   x86_64::CpuRegister shifter(x86_64::RCX);
555   for (auto reg : registers) {
556     assembler->rorq(*reg, shifter);
557     str << "rorq %cl, %" << assembler_test->GetRegisterName(*reg) << "\n";
558   }
559 
560   return str.str();
561 }
562 
TEST_F(AssemblerX86_64Test,RorqReg)563 TEST_F(AssemblerX86_64Test, RorqReg) {
564   DriverFn(&rorq_fn, "rorq");
565 }
566 
TEST_F(AssemblerX86_64Test,RorqImm)567 TEST_F(AssemblerX86_64Test, RorqImm) {
568   DriverStr(RepeatRI(&x86_64::X86_64Assembler::rorq, 1U, "rorq ${imm}, %{reg}"), "rorqi");
569 }
570 
571 // Rolq only allows CL as the shift count.
rolq_fn(AssemblerX86_64Test::Base * assembler_test,x86_64::X86_64Assembler * assembler)572 std::string rolq_fn(AssemblerX86_64Test::Base* assembler_test, x86_64::X86_64Assembler* assembler) {
573   std::ostringstream str;
574 
575   std::vector<x86_64::CpuRegister*> registers = assembler_test->GetRegisters();
576 
577   x86_64::CpuRegister shifter(x86_64::RCX);
578   for (auto reg : registers) {
579     assembler->rolq(*reg, shifter);
580     str << "rolq %cl, %" << assembler_test->GetRegisterName(*reg) << "\n";
581   }
582 
583   return str.str();
584 }
585 
TEST_F(AssemblerX86_64Test,RolqReg)586 TEST_F(AssemblerX86_64Test, RolqReg) {
587   DriverFn(&rolq_fn, "rolq");
588 }
589 
TEST_F(AssemblerX86_64Test,RolqImm)590 TEST_F(AssemblerX86_64Test, RolqImm) {
591   DriverStr(RepeatRI(&x86_64::X86_64Assembler::rolq, 1U, "rolq ${imm}, %{reg}"), "rolqi");
592 }
593 
TEST_F(AssemblerX86_64Test,CmpqRegs)594 TEST_F(AssemblerX86_64Test, CmpqRegs) {
595   DriverStr(RepeatRR(&x86_64::X86_64Assembler::cmpq, "cmpq %{reg2}, %{reg1}"), "cmpq");
596 }
597 
TEST_F(AssemblerX86_64Test,CmpqImm)598 TEST_F(AssemblerX86_64Test, CmpqImm) {
599   DriverStr(RepeatRI(&x86_64::X86_64Assembler::cmpq, 4U  /* cmpq only supports 32b imm */,
600                      "cmpq ${imm}, %{reg}"), "cmpqi");
601 }
602 
TEST_F(AssemblerX86_64Test,CmplRegs)603 TEST_F(AssemblerX86_64Test, CmplRegs) {
604   DriverStr(Repeatrr(&x86_64::X86_64Assembler::cmpl, "cmp %{reg2}, %{reg1}"), "cmpl");
605 }
606 
TEST_F(AssemblerX86_64Test,CmplImm)607 TEST_F(AssemblerX86_64Test, CmplImm) {
608   DriverStr(Repeatri(&x86_64::X86_64Assembler::cmpl, 4U, "cmpl ${imm}, %{reg}"), "cmpli");
609 }
610 
TEST_F(AssemblerX86_64Test,Testl)611 TEST_F(AssemblerX86_64Test, Testl) {
612   // Note: uses different order for GCC than usual. This makes GCC happy, and doesn't have an
613   // impact on functional correctness.
614   DriverStr(Repeatrr(&x86_64::X86_64Assembler::testl, "testl %{reg1}, %{reg2}"), "testl");
615 }
616 
TEST_F(AssemblerX86_64Test,Negq)617 TEST_F(AssemblerX86_64Test, Negq) {
618   DriverStr(RepeatR(&x86_64::X86_64Assembler::negq, "negq %{reg}"), "negq");
619 }
620 
TEST_F(AssemblerX86_64Test,Negl)621 TEST_F(AssemblerX86_64Test, Negl) {
622   DriverStr(Repeatr(&x86_64::X86_64Assembler::negl, "negl %{reg}"), "negl");
623 }
624 
TEST_F(AssemblerX86_64Test,Notq)625 TEST_F(AssemblerX86_64Test, Notq) {
626   DriverStr(RepeatR(&x86_64::X86_64Assembler::notq, "notq %{reg}"), "notq");
627 }
628 
TEST_F(AssemblerX86_64Test,Notl)629 TEST_F(AssemblerX86_64Test, Notl) {
630   DriverStr(Repeatr(&x86_64::X86_64Assembler::notl, "notl %{reg}"), "notl");
631 }
632 
TEST_F(AssemblerX86_64Test,AndqRegs)633 TEST_F(AssemblerX86_64Test, AndqRegs) {
634   DriverStr(RepeatRR(&x86_64::X86_64Assembler::andq, "andq %{reg2}, %{reg1}"), "andq");
635 }
636 
TEST_F(AssemblerX86_64Test,AndqImm)637 TEST_F(AssemblerX86_64Test, AndqImm) {
638   DriverStr(RepeatRI(&x86_64::X86_64Assembler::andq, 4U  /* andq only supports 32b imm */,
639                      "andq ${imm}, %{reg}"), "andqi");
640 }
641 
TEST_F(AssemblerX86_64Test,AndlRegs)642 TEST_F(AssemblerX86_64Test, AndlRegs) {
643   DriverStr(Repeatrr(&x86_64::X86_64Assembler::andl, "andl %{reg2}, %{reg1}"), "andl");
644 }
645 
TEST_F(AssemblerX86_64Test,AndlImm)646 TEST_F(AssemblerX86_64Test, AndlImm) {
647   DriverStr(Repeatri(&x86_64::X86_64Assembler::andl, 4U, "andl ${imm}, %{reg}"), "andli");
648 }
649 
TEST_F(AssemblerX86_64Test,OrqRegs)650 TEST_F(AssemblerX86_64Test, OrqRegs) {
651   DriverStr(RepeatRR(&x86_64::X86_64Assembler::orq, "orq %{reg2}, %{reg1}"), "orq");
652 }
653 
TEST_F(AssemblerX86_64Test,OrlRegs)654 TEST_F(AssemblerX86_64Test, OrlRegs) {
655   DriverStr(Repeatrr(&x86_64::X86_64Assembler::orl, "orl %{reg2}, %{reg1}"), "orl");
656 }
657 
TEST_F(AssemblerX86_64Test,OrlImm)658 TEST_F(AssemblerX86_64Test, OrlImm) {
659   DriverStr(Repeatri(&x86_64::X86_64Assembler::orl, 4U, "orl ${imm}, %{reg}"), "orli");
660 }
661 
TEST_F(AssemblerX86_64Test,XorqRegs)662 TEST_F(AssemblerX86_64Test, XorqRegs) {
663   DriverStr(RepeatRR(&x86_64::X86_64Assembler::xorq, "xorq %{reg2}, %{reg1}"), "xorq");
664 }
665 
TEST_F(AssemblerX86_64Test,XorqImm)666 TEST_F(AssemblerX86_64Test, XorqImm) {
667   DriverStr(RepeatRI(&x86_64::X86_64Assembler::xorq, 4U, "xorq ${imm}, %{reg}"), "xorqi");
668 }
669 
TEST_F(AssemblerX86_64Test,XorlRegs)670 TEST_F(AssemblerX86_64Test, XorlRegs) {
671   DriverStr(Repeatrr(&x86_64::X86_64Assembler::xorl, "xor %{reg2}, %{reg1}"), "xorl");
672 }
673 
TEST_F(AssemblerX86_64Test,XorlImm)674 TEST_F(AssemblerX86_64Test, XorlImm) {
675   DriverStr(Repeatri(&x86_64::X86_64Assembler::xorl, 4U, "xor ${imm}, %{reg}"), "xorli");
676 }
677 
TEST_F(AssemblerX86_64Test,Xchgq)678 TEST_F(AssemblerX86_64Test, Xchgq) {
679   DriverStr(RepeatRR(&x86_64::X86_64Assembler::xchgq, "xchgq %{reg2}, %{reg1}"), "xchgq");
680 }
681 
TEST_F(AssemblerX86_64Test,Xchgl)682 TEST_F(AssemblerX86_64Test, Xchgl) {
683   // Test is disabled because GCC generates 0x87 0xC0 for xchgl eax, eax. All other cases are the
684   // same. Anyone know why it doesn't emit a simple 0x90? It does so for xchgq rax, rax...
685   // DriverStr(Repeatrr(&x86_64::X86_64Assembler::xchgl, "xchgl %{reg2}, %{reg1}"), "xchgl");
686 }
687 
TEST_F(AssemblerX86_64Test,LockCmpxchgl)688 TEST_F(AssemblerX86_64Test, LockCmpxchgl) {
689   GetAssembler()->LockCmpxchgl(x86_64::Address(
690       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12),
691       x86_64::CpuRegister(x86_64::RSI));
692   GetAssembler()->LockCmpxchgl(x86_64::Address(
693       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12),
694       x86_64::CpuRegister(x86_64::RSI));
695   GetAssembler()->LockCmpxchgl(x86_64::Address(
696       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12),
697       x86_64::CpuRegister(x86_64::R8));
698   GetAssembler()->LockCmpxchgl(x86_64::Address(
699       x86_64::CpuRegister(x86_64::R13), 0), x86_64::CpuRegister(x86_64::RSI));
700   GetAssembler()->LockCmpxchgl(x86_64::Address(
701       x86_64::CpuRegister(x86_64::R13), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_1, 0),
702       x86_64::CpuRegister(x86_64::RSI));
703   const char* expected =
704     "lock cmpxchgl %ESI, 0xc(%RDI,%RBX,4)\n"
705     "lock cmpxchgl %ESI, 0xc(%RDI,%R9,4)\n"
706     "lock cmpxchgl %R8d, 0xc(%RDI,%R9,4)\n"
707     "lock cmpxchgl %ESI, (%R13)\n"
708     "lock cmpxchgl %ESI, (%R13,%R9,1)\n";
709 
710   DriverStr(expected, "lock_cmpxchgl");
711 }
712 
TEST_F(AssemblerX86_64Test,LockCmpxchgq)713 TEST_F(AssemblerX86_64Test, LockCmpxchgq) {
714   GetAssembler()->LockCmpxchgq(x86_64::Address(
715       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12),
716       x86_64::CpuRegister(x86_64::RSI));
717   GetAssembler()->LockCmpxchgq(x86_64::Address(
718       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12),
719       x86_64::CpuRegister(x86_64::RSI));
720   GetAssembler()->LockCmpxchgq(x86_64::Address(
721       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12),
722       x86_64::CpuRegister(x86_64::R8));
723   GetAssembler()->LockCmpxchgq(x86_64::Address(
724       x86_64::CpuRegister(x86_64::R13), 0), x86_64::CpuRegister(x86_64::RSI));
725   GetAssembler()->LockCmpxchgq(x86_64::Address(
726       x86_64::CpuRegister(x86_64::R13), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_1, 0),
727       x86_64::CpuRegister(x86_64::RSI));
728   const char* expected =
729     "lock cmpxchg %RSI, 0xc(%RDI,%RBX,4)\n"
730     "lock cmpxchg %RSI, 0xc(%RDI,%R9,4)\n"
731     "lock cmpxchg %R8, 0xc(%RDI,%R9,4)\n"
732     "lock cmpxchg %RSI, (%R13)\n"
733     "lock cmpxchg %RSI, (%R13,%R9,1)\n";
734 
735   DriverStr(expected, "lock_cmpxchg");
736 }
737 
TEST_F(AssemblerX86_64Test,Movl)738 TEST_F(AssemblerX86_64Test, Movl) {
739   GetAssembler()->movl(x86_64::CpuRegister(x86_64::RAX), x86_64::Address(
740       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
741   GetAssembler()->movl(x86_64::CpuRegister(x86_64::RAX), x86_64::Address(
742       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12));
743   GetAssembler()->movl(x86_64::CpuRegister(x86_64::R8), x86_64::Address(
744       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12));
745   GetAssembler()->movl(x86_64::CpuRegister(x86_64::RAX), x86_64::Address(
746       x86_64::CpuRegister(x86_64::R13), 0));
747   GetAssembler()->movl(x86_64::CpuRegister(x86_64::RAX), x86_64::Address(
748       x86_64::CpuRegister(x86_64::R13), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_1, 0));
749   const char* expected =
750     "movl 0xc(%RDI,%RBX,4), %EAX\n"
751     "movl 0xc(%RDI,%R9,4), %EAX\n"
752     "movl 0xc(%RDI,%R9,4), %R8d\n"
753     "movl (%R13), %EAX\n"
754     "movl (%R13,%R9,1), %EAX\n";
755 
756   DriverStr(expected, "movl");
757 }
758 
TEST_F(AssemblerX86_64Test,Movw)759 TEST_F(AssemblerX86_64Test, Movw) {
760   GetAssembler()->movw(x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0),
761                        x86_64::CpuRegister(x86_64::R9));
762   GetAssembler()->movw(x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0),
763                        x86_64::Immediate(0));
764   GetAssembler()->movw(x86_64::Address(x86_64::CpuRegister(x86_64::R9), 0),
765                        x86_64::Immediate(0));
766   GetAssembler()->movw(x86_64::Address(x86_64::CpuRegister(x86_64::R14), 0),
767                        x86_64::Immediate(0));
768   const char* expected =
769       "movw %R9w, 0(%RAX)\n"
770       "movw $0, 0(%RAX)\n"
771       "movw $0, 0(%R9)\n"
772       "movw $0, 0(%R14)\n";
773   DriverStr(expected, "movw");
774 }
775 
TEST_F(AssemblerX86_64Test,Cmpw)776 TEST_F(AssemblerX86_64Test, Cmpw) {
777   GetAssembler()->cmpw(x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0),
778                        x86_64::Immediate(0));
779   GetAssembler()->cmpw(x86_64::Address(x86_64::CpuRegister(x86_64::R9), 0),
780                        x86_64::Immediate(0));
781   GetAssembler()->cmpw(x86_64::Address(x86_64::CpuRegister(x86_64::R14), 0),
782                        x86_64::Immediate(0));
783   const char* expected =
784       "cmpw $0, 0(%RAX)\n"
785       "cmpw $0, 0(%R9)\n"
786       "cmpw $0, 0(%R14)\n";
787   DriverStr(expected, "cmpw");
788 }
789 
TEST_F(AssemblerX86_64Test,MovqAddrImm)790 TEST_F(AssemblerX86_64Test, MovqAddrImm) {
791   GetAssembler()->movq(x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0),
792                        x86_64::Immediate(-5));
793   const char* expected = "movq $-5, 0(%RAX)\n";
794   DriverStr(expected, "movq");
795 }
796 
TEST_F(AssemblerX86_64Test,Movntl)797 TEST_F(AssemblerX86_64Test, Movntl) {
798   GetAssembler()->movntl(x86_64::Address(
799       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12), x86_64::CpuRegister(x86_64::RAX));
800   GetAssembler()->movntl(x86_64::Address(
801       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12), x86_64::CpuRegister(x86_64::RAX));
802   GetAssembler()->movntl(x86_64::Address(
803       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12), x86_64::CpuRegister(x86_64::RAX));
804   GetAssembler()->movntl(x86_64::Address(x86_64::CpuRegister(x86_64::R13), 0), x86_64::CpuRegister(x86_64::RAX));
805   GetAssembler()->movntl(x86_64::Address(
806       x86_64::CpuRegister(x86_64::R13), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_1, 0), x86_64::CpuRegister(x86_64::R9));
807   const char* expected =
808     "movntil %EAX, 0xc(%RDI,%RBX,4)\n"
809     "movntil %EAX, 0xc(%RDI,%R9,4)\n"
810     "movntil %EAX, 0xc(%RDI,%R9,4)\n"
811     "movntil %EAX, (%R13)\n"
812     "movntil %R9d, (%R13,%R9,1)\n";
813 
814   DriverStr(expected, "movntl");
815 }
816 
TEST_F(AssemblerX86_64Test,Movntq)817 TEST_F(AssemblerX86_64Test, Movntq) {
818   GetAssembler()->movntq(x86_64::Address(
819       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12), x86_64::CpuRegister(x86_64::RAX));
820   GetAssembler()->movntq(x86_64::Address(
821       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12), x86_64::CpuRegister(x86_64::RAX));
822   GetAssembler()->movntq(x86_64::Address(
823       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12), x86_64::CpuRegister(x86_64::RAX));
824   GetAssembler()->movntq(x86_64::Address(x86_64::CpuRegister(x86_64::R13), 0), x86_64::CpuRegister(x86_64::RAX));
825   GetAssembler()->movntq(x86_64::Address(
826       x86_64::CpuRegister(x86_64::R13), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_1, 0), x86_64::CpuRegister(x86_64::R9));
827   const char* expected =
828     "movntiq %RAX, 0xc(%RDI,%RBX,4)\n"
829     "movntiq %RAX, 0xc(%RDI,%R9,4)\n"
830     "movntiq %RAX, 0xc(%RDI,%R9,4)\n"
831     "movntiq %RAX, (%R13)\n"
832     "movntiq %R9, (%R13,%R9,1)\n";
833 
834   DriverStr(expected, "movntq");
835 }
836 
TEST_F(AssemblerX86_64Test,Cvtsi2ssAddr)837 TEST_F(AssemblerX86_64Test, Cvtsi2ssAddr) {
838   GetAssembler()->cvtsi2ss(x86_64::XmmRegister(x86_64::XMM0),
839                            x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0),
840                            false);
841   GetAssembler()->cvtsi2ss(x86_64::XmmRegister(x86_64::XMM0),
842                            x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0),
843                            true);
844   const char* expected = "cvtsi2ss 0(%RAX), %xmm0\n"
845                          "cvtsi2ssq 0(%RAX), %xmm0\n";
846   DriverStr(expected, "cvtsi2ss");
847 }
848 
TEST_F(AssemblerX86_64Test,Cvtsi2sdAddr)849 TEST_F(AssemblerX86_64Test, Cvtsi2sdAddr) {
850   GetAssembler()->cvtsi2sd(x86_64::XmmRegister(x86_64::XMM0),
851                            x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0),
852                            false);
853   GetAssembler()->cvtsi2sd(x86_64::XmmRegister(x86_64::XMM0),
854                            x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0),
855                            true);
856   const char* expected = "cvtsi2sd 0(%RAX), %xmm0\n"
857                          "cvtsi2sdq 0(%RAX), %xmm0\n";
858   DriverStr(expected, "cvtsi2sd");
859 }
860 
TEST_F(AssemblerX86_64Test,CmpqAddr)861 TEST_F(AssemblerX86_64Test, CmpqAddr) {
862   GetAssembler()->cmpq(x86_64::CpuRegister(x86_64::R12),
863                        x86_64::Address(x86_64::CpuRegister(x86_64::R9), 0));
864   const char* expected = "cmpq 0(%R9), %R12\n";
865   DriverStr(expected, "cmpq");
866 }
867 
TEST_F(AssemblerX86_64Test,MovsxdAddr)868 TEST_F(AssemblerX86_64Test, MovsxdAddr) {
869   GetAssembler()->movsxd(x86_64::CpuRegister(x86_64::R12),
870                        x86_64::Address(x86_64::CpuRegister(x86_64::R9), 0));
871   const char* expected = "movslq 0(%R9), %R12\n";
872   DriverStr(expected, "movsxd");
873 }
874 
TEST_F(AssemblerX86_64Test,TestqAddr)875 TEST_F(AssemblerX86_64Test, TestqAddr) {
876   GetAssembler()->testq(x86_64::CpuRegister(x86_64::R12),
877                         x86_64::Address(x86_64::CpuRegister(x86_64::R9), 0));
878   const char* expected = "testq 0(%R9), %R12\n";
879   DriverStr(expected, "testq");
880 }
881 
TEST_F(AssemblerX86_64Test,AddqAddr)882 TEST_F(AssemblerX86_64Test, AddqAddr) {
883   GetAssembler()->addq(x86_64::CpuRegister(x86_64::R12),
884                         x86_64::Address(x86_64::CpuRegister(x86_64::R9), 0));
885   const char* expected = "addq 0(%R9), %R12\n";
886   DriverStr(expected, "addq");
887 }
888 
TEST_F(AssemblerX86_64Test,SubqAddr)889 TEST_F(AssemblerX86_64Test, SubqAddr) {
890   GetAssembler()->subq(x86_64::CpuRegister(x86_64::R12),
891                         x86_64::Address(x86_64::CpuRegister(x86_64::R9), 0));
892   const char* expected = "subq 0(%R9), %R12\n";
893   DriverStr(expected, "subq");
894 }
895 
TEST_F(AssemblerX86_64Test,Cvtss2sdAddr)896 TEST_F(AssemblerX86_64Test, Cvtss2sdAddr) {
897   GetAssembler()->cvtss2sd(x86_64::XmmRegister(x86_64::XMM0),
898                            x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0));
899   const char* expected = "cvtss2sd 0(%RAX), %xmm0\n";
900   DriverStr(expected, "cvtss2sd");
901 }
902 
TEST_F(AssemblerX86_64Test,Cvtsd2ssAddr)903 TEST_F(AssemblerX86_64Test, Cvtsd2ssAddr) {
904   GetAssembler()->cvtsd2ss(x86_64::XmmRegister(x86_64::XMM0),
905                            x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0));
906   const char* expected = "cvtsd2ss 0(%RAX), %xmm0\n";
907   DriverStr(expected, "cvtsd2ss");
908 }
909 
TEST_F(AssemblerX86_64Test,ComissAddr)910 TEST_F(AssemblerX86_64Test, ComissAddr) {
911   GetAssembler()->comiss(x86_64::XmmRegister(x86_64::XMM14),
912                            x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0));
913   const char* expected = "comiss 0(%RAX), %xmm14\n";
914   DriverStr(expected, "comiss");
915 }
916 
TEST_F(AssemblerX86_64Test,ComisdAddr)917 TEST_F(AssemblerX86_64Test, ComisdAddr) {
918   GetAssembler()->comisd(x86_64::XmmRegister(x86_64::XMM0),
919                            x86_64::Address(x86_64::CpuRegister(x86_64::R9), 0));
920   const char* expected = "comisd 0(%R9), %xmm0\n";
921   DriverStr(expected, "comisd");
922 }
923 
TEST_F(AssemblerX86_64Test,UComissAddr)924 TEST_F(AssemblerX86_64Test, UComissAddr) {
925   GetAssembler()->ucomiss(x86_64::XmmRegister(x86_64::XMM0),
926                            x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0));
927   const char* expected = "ucomiss 0(%RAX), %xmm0\n";
928   DriverStr(expected, "ucomiss");
929 }
930 
TEST_F(AssemblerX86_64Test,UComisdAddr)931 TEST_F(AssemblerX86_64Test, UComisdAddr) {
932   GetAssembler()->ucomisd(x86_64::XmmRegister(x86_64::XMM0),
933                            x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0));
934   const char* expected = "ucomisd 0(%RAX), %xmm0\n";
935   DriverStr(expected, "ucomisd");
936 }
937 
TEST_F(AssemblerX86_64Test,Andq)938 TEST_F(AssemblerX86_64Test, Andq) {
939   GetAssembler()->andq(x86_64::CpuRegister(x86_64::R9),
940                            x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0));
941   const char* expected = "andq 0(%RAX), %r9\n";
942   DriverStr(expected, "andq");
943 }
944 
TEST_F(AssemblerX86_64Test,Orq)945 TEST_F(AssemblerX86_64Test, Orq) {
946   GetAssembler()->orq(x86_64::CpuRegister(x86_64::R9),
947                            x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0));
948   const char* expected = "orq 0(%RAX), %r9\n";
949   DriverStr(expected, "orq");
950 }
951 
TEST_F(AssemblerX86_64Test,Xorq)952 TEST_F(AssemblerX86_64Test, Xorq) {
953   GetAssembler()->xorq(x86_64::CpuRegister(x86_64::R9),
954                            x86_64::Address(x86_64::CpuRegister(x86_64::RAX), 0));
955   const char* expected = "xorq 0(%RAX), %r9\n";
956   DriverStr(expected, "xorq");
957 }
958 
TEST_F(AssemblerX86_64Test,RepneScasb)959 TEST_F(AssemblerX86_64Test, RepneScasb) {
960   GetAssembler()->repne_scasb();
961   const char* expected = "repne scasb\n";
962   DriverStr(expected, "repne_scasb");
963 }
964 
TEST_F(AssemblerX86_64Test,RepneScasw)965 TEST_F(AssemblerX86_64Test, RepneScasw) {
966   GetAssembler()->repne_scasw();
967   const char* expected = "repne scasw\n";
968   DriverStr(expected, "repne_scasw");
969 }
970 
TEST_F(AssemblerX86_64Test,RepMovsw)971 TEST_F(AssemblerX86_64Test, RepMovsw) {
972   GetAssembler()->rep_movsw();
973   const char* expected = "rep movsw\n";
974   DriverStr(expected, "rep_movsw");
975 }
976 
TEST_F(AssemblerX86_64Test,Movsxd)977 TEST_F(AssemblerX86_64Test, Movsxd) {
978   DriverStr(RepeatRr(&x86_64::X86_64Assembler::movsxd, "movsxd %{reg2}, %{reg1}"), "movsxd");
979 }
980 
981 ///////////////////
982 // FP Operations //
983 ///////////////////
984 
TEST_F(AssemblerX86_64Test,Movaps)985 TEST_F(AssemblerX86_64Test, Movaps) {
986   DriverStr(RepeatFF(&x86_64::X86_64Assembler::movaps, "movaps %{reg2}, %{reg1}"), "movaps");
987 }
988 
TEST_F(AssemblerX86_64Test,MovapsAddr)989 TEST_F(AssemblerX86_64Test, MovapsAddr) {
990   GetAssembler()->movaps(x86_64::XmmRegister(x86_64::XMM0), x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 4));
991   GetAssembler()->movaps(x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 2), x86_64::XmmRegister(x86_64::XMM1));
992   const char* expected =
993     "movaps 0x4(%RSP), %xmm0\n"
994     "movaps %xmm1, 0x2(%RSP)\n";
995   DriverStr(expected, "movaps_address");
996 }
997 
TEST_F(AssemblerX86_64Test,MovupsAddr)998 TEST_F(AssemblerX86_64Test, MovupsAddr) {
999   GetAssembler()->movups(x86_64::XmmRegister(x86_64::XMM0), x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 4));
1000   GetAssembler()->movups(x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 2), x86_64::XmmRegister(x86_64::XMM1));
1001   const char* expected =
1002     "movups 0x4(%RSP), %xmm0\n"
1003     "movups %xmm1, 0x2(%RSP)\n";
1004   DriverStr(expected, "movups_address");
1005 }
1006 
TEST_F(AssemblerX86_64Test,Movss)1007 TEST_F(AssemblerX86_64Test, Movss) {
1008   DriverStr(RepeatFF(&x86_64::X86_64Assembler::movss, "movss %{reg2}, %{reg1}"), "movss");
1009 }
1010 
TEST_F(AssemblerX86_64Test,Movapd)1011 TEST_F(AssemblerX86_64Test, Movapd) {
1012   DriverStr(RepeatFF(&x86_64::X86_64Assembler::movapd, "movapd %{reg2}, %{reg1}"), "movapd");
1013 }
1014 
TEST_F(AssemblerX86_64Test,MovapdAddr)1015 TEST_F(AssemblerX86_64Test, MovapdAddr) {
1016   GetAssembler()->movapd(x86_64::XmmRegister(x86_64::XMM0), x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 4));
1017   GetAssembler()->movapd(x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 2), x86_64::XmmRegister(x86_64::XMM1));
1018   const char* expected =
1019     "movapd 0x4(%RSP), %xmm0\n"
1020     "movapd %xmm1, 0x2(%RSP)\n";
1021   DriverStr(expected, "movapd_address");
1022 }
1023 
TEST_F(AssemblerX86_64Test,MovupdAddr)1024 TEST_F(AssemblerX86_64Test, MovupdAddr) {
1025   GetAssembler()->movupd(x86_64::XmmRegister(x86_64::XMM0), x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 4));
1026   GetAssembler()->movupd(x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 2), x86_64::XmmRegister(x86_64::XMM1));
1027   const char* expected =
1028     "movupd 0x4(%RSP), %xmm0\n"
1029     "movupd %xmm1, 0x2(%RSP)\n";
1030   DriverStr(expected, "movupd_address");
1031 }
1032 
TEST_F(AssemblerX86_64Test,Movsd)1033 TEST_F(AssemblerX86_64Test, Movsd) {
1034   DriverStr(RepeatFF(&x86_64::X86_64Assembler::movsd, "movsd %{reg2}, %{reg1}"), "movsd");
1035 }
1036 
TEST_F(AssemblerX86_64Test,Movdqa)1037 TEST_F(AssemblerX86_64Test, Movdqa) {
1038   DriverStr(RepeatFF(&x86_64::X86_64Assembler::movdqa, "movdqa %{reg2}, %{reg1}"), "movapd");
1039 }
1040 
TEST_F(AssemblerX86_64Test,MovdqaAddr)1041 TEST_F(AssemblerX86_64Test, MovdqaAddr) {
1042   GetAssembler()->movdqa(x86_64::XmmRegister(x86_64::XMM0), x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 4));
1043   GetAssembler()->movdqa(x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 2), x86_64::XmmRegister(x86_64::XMM1));
1044   const char* expected =
1045     "movdqa 0x4(%RSP), %xmm0\n"
1046     "movdqa %xmm1, 0x2(%RSP)\n";
1047   DriverStr(expected, "movdqa_address");
1048 }
1049 
TEST_F(AssemblerX86_64Test,MovdquAddr)1050 TEST_F(AssemblerX86_64Test, MovdquAddr) {
1051   GetAssembler()->movdqu(x86_64::XmmRegister(x86_64::XMM0), x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 4));
1052   GetAssembler()->movdqu(x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 2), x86_64::XmmRegister(x86_64::XMM1));
1053   const char* expected =
1054     "movdqu 0x4(%RSP), %xmm0\n"
1055     "movdqu %xmm1, 0x2(%RSP)\n";
1056   DriverStr(expected, "movdqu_address");
1057 }
1058 
TEST_F(AssemblerX86_64Test,Movd1)1059 TEST_F(AssemblerX86_64Test, Movd1) {
1060   DriverStr(RepeatFR(&x86_64::X86_64Assembler::movd, "movd %{reg2}, %{reg1}"), "movd.1");
1061 }
1062 
TEST_F(AssemblerX86_64Test,Movd2)1063 TEST_F(AssemblerX86_64Test, Movd2) {
1064   DriverStr(RepeatRF(&x86_64::X86_64Assembler::movd, "movd %{reg2}, %{reg1}"), "movd.2");
1065 }
1066 
TEST_F(AssemblerX86_64Test,Addss)1067 TEST_F(AssemblerX86_64Test, Addss) {
1068   DriverStr(RepeatFF(&x86_64::X86_64Assembler::addss, "addss %{reg2}, %{reg1}"), "addss");
1069 }
1070 
TEST_F(AssemblerX86_64Test,Addsd)1071 TEST_F(AssemblerX86_64Test, Addsd) {
1072   DriverStr(RepeatFF(&x86_64::X86_64Assembler::addsd, "addsd %{reg2}, %{reg1}"), "addsd");
1073 }
1074 
TEST_F(AssemblerX86_64Test,Addps)1075 TEST_F(AssemblerX86_64Test, Addps) {
1076   DriverStr(RepeatFF(&x86_64::X86_64Assembler::addps, "addps %{reg2}, %{reg1}"), "addps");
1077 }
1078 
TEST_F(AssemblerX86_64Test,Addpd)1079 TEST_F(AssemblerX86_64Test, Addpd) {
1080   DriverStr(RepeatFF(&x86_64::X86_64Assembler::addpd, "addpd %{reg2}, %{reg1}"), "addpd");
1081 }
1082 
TEST_F(AssemblerX86_64Test,Subss)1083 TEST_F(AssemblerX86_64Test, Subss) {
1084   DriverStr(RepeatFF(&x86_64::X86_64Assembler::subss, "subss %{reg2}, %{reg1}"), "subss");
1085 }
1086 
TEST_F(AssemblerX86_64Test,Subsd)1087 TEST_F(AssemblerX86_64Test, Subsd) {
1088   DriverStr(RepeatFF(&x86_64::X86_64Assembler::subsd, "subsd %{reg2}, %{reg1}"), "subsd");
1089 }
1090 
TEST_F(AssemblerX86_64Test,Subps)1091 TEST_F(AssemblerX86_64Test, Subps) {
1092   DriverStr(RepeatFF(&x86_64::X86_64Assembler::subps, "subps %{reg2}, %{reg1}"), "subps");
1093 }
1094 
TEST_F(AssemblerX86_64Test,Subpd)1095 TEST_F(AssemblerX86_64Test, Subpd) {
1096   DriverStr(RepeatFF(&x86_64::X86_64Assembler::subpd, "subpd %{reg2}, %{reg1}"), "subpd");
1097 }
1098 
TEST_F(AssemblerX86_64Test,Mulss)1099 TEST_F(AssemblerX86_64Test, Mulss) {
1100   DriverStr(RepeatFF(&x86_64::X86_64Assembler::mulss, "mulss %{reg2}, %{reg1}"), "mulss");
1101 }
1102 
TEST_F(AssemblerX86_64Test,Mulsd)1103 TEST_F(AssemblerX86_64Test, Mulsd) {
1104   DriverStr(RepeatFF(&x86_64::X86_64Assembler::mulsd, "mulsd %{reg2}, %{reg1}"), "mulsd");
1105 }
1106 
TEST_F(AssemblerX86_64Test,Mulps)1107 TEST_F(AssemblerX86_64Test, Mulps) {
1108   DriverStr(RepeatFF(&x86_64::X86_64Assembler::mulps, "mulps %{reg2}, %{reg1}"), "mulps");
1109 }
1110 
TEST_F(AssemblerX86_64Test,Mulpd)1111 TEST_F(AssemblerX86_64Test, Mulpd) {
1112   DriverStr(RepeatFF(&x86_64::X86_64Assembler::mulpd, "mulpd %{reg2}, %{reg1}"), "mulpd");
1113 }
1114 
TEST_F(AssemblerX86_64Test,Divss)1115 TEST_F(AssemblerX86_64Test, Divss) {
1116   DriverStr(RepeatFF(&x86_64::X86_64Assembler::divss, "divss %{reg2}, %{reg1}"), "divss");
1117 }
1118 
TEST_F(AssemblerX86_64Test,Divsd)1119 TEST_F(AssemblerX86_64Test, Divsd) {
1120   DriverStr(RepeatFF(&x86_64::X86_64Assembler::divsd, "divsd %{reg2}, %{reg1}"), "divsd");
1121 }
1122 
TEST_F(AssemblerX86_64Test,Divps)1123 TEST_F(AssemblerX86_64Test, Divps) {
1124   DriverStr(RepeatFF(&x86_64::X86_64Assembler::divps, "divps %{reg2}, %{reg1}"), "divps");
1125 }
1126 
TEST_F(AssemblerX86_64Test,Divpd)1127 TEST_F(AssemblerX86_64Test, Divpd) {
1128   DriverStr(RepeatFF(&x86_64::X86_64Assembler::divpd, "divpd %{reg2}, %{reg1}"), "divpd");
1129 }
1130 
TEST_F(AssemblerX86_64Test,Paddb)1131 TEST_F(AssemblerX86_64Test, Paddb) {
1132   DriverStr(RepeatFF(&x86_64::X86_64Assembler::paddb, "paddb %{reg2}, %{reg1}"), "paddb");
1133 }
1134 
TEST_F(AssemblerX86_64Test,Psubb)1135 TEST_F(AssemblerX86_64Test, Psubb) {
1136   DriverStr(RepeatFF(&x86_64::X86_64Assembler::psubb, "psubb %{reg2}, %{reg1}"), "psubb");
1137 }
1138 
TEST_F(AssemblerX86_64Test,Paddw)1139 TEST_F(AssemblerX86_64Test, Paddw) {
1140   DriverStr(RepeatFF(&x86_64::X86_64Assembler::paddw, "paddw %{reg2}, %{reg1}"), "paddw");
1141 }
1142 
TEST_F(AssemblerX86_64Test,Psubw)1143 TEST_F(AssemblerX86_64Test, Psubw) {
1144   DriverStr(RepeatFF(&x86_64::X86_64Assembler::psubw, "psubw %{reg2}, %{reg1}"), "psubw");
1145 }
1146 
TEST_F(AssemblerX86_64Test,Pmullw)1147 TEST_F(AssemblerX86_64Test, Pmullw) {
1148   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pmullw, "pmullw %{reg2}, %{reg1}"), "pmullw");
1149 }
1150 
TEST_F(AssemblerX86_64Test,Paddd)1151 TEST_F(AssemblerX86_64Test, Paddd) {
1152   DriverStr(RepeatFF(&x86_64::X86_64Assembler::paddd, "paddd %{reg2}, %{reg1}"), "paddd");
1153 }
1154 
TEST_F(AssemblerX86_64Test,Psubd)1155 TEST_F(AssemblerX86_64Test, Psubd) {
1156   DriverStr(RepeatFF(&x86_64::X86_64Assembler::psubd, "psubd %{reg2}, %{reg1}"), "psubd");
1157 }
1158 
TEST_F(AssemblerX86_64Test,Pmulld)1159 TEST_F(AssemblerX86_64Test, Pmulld) {
1160   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pmulld, "pmulld %{reg2}, %{reg1}"), "pmulld");
1161 }
1162 
TEST_F(AssemblerX86_64Test,Paddq)1163 TEST_F(AssemblerX86_64Test, Paddq) {
1164   DriverStr(RepeatFF(&x86_64::X86_64Assembler::paddq, "paddq %{reg2}, %{reg1}"), "paddq");
1165 }
1166 
TEST_F(AssemblerX86_64Test,Psubq)1167 TEST_F(AssemblerX86_64Test, Psubq) {
1168   DriverStr(RepeatFF(&x86_64::X86_64Assembler::psubq, "psubq %{reg2}, %{reg1}"), "psubq");
1169 }
1170 
TEST_F(AssemblerX86_64Test,Cvtsi2ss)1171 TEST_F(AssemblerX86_64Test, Cvtsi2ss) {
1172   DriverStr(RepeatFr(&x86_64::X86_64Assembler::cvtsi2ss, "cvtsi2ss %{reg2}, %{reg1}"), "cvtsi2ss");
1173 }
1174 
TEST_F(AssemblerX86_64Test,Cvtsi2sd)1175 TEST_F(AssemblerX86_64Test, Cvtsi2sd) {
1176   DriverStr(RepeatFr(&x86_64::X86_64Assembler::cvtsi2sd, "cvtsi2sd %{reg2}, %{reg1}"), "cvtsi2sd");
1177 }
1178 
1179 
TEST_F(AssemblerX86_64Test,Cvtss2si)1180 TEST_F(AssemblerX86_64Test, Cvtss2si) {
1181   DriverStr(RepeatrF(&x86_64::X86_64Assembler::cvtss2si, "cvtss2si %{reg2}, %{reg1}"), "cvtss2si");
1182 }
1183 
1184 
TEST_F(AssemblerX86_64Test,Cvtss2sd)1185 TEST_F(AssemblerX86_64Test, Cvtss2sd) {
1186   DriverStr(RepeatFF(&x86_64::X86_64Assembler::cvtss2sd, "cvtss2sd %{reg2}, %{reg1}"), "cvtss2sd");
1187 }
1188 
1189 
TEST_F(AssemblerX86_64Test,Cvtsd2si)1190 TEST_F(AssemblerX86_64Test, Cvtsd2si) {
1191   DriverStr(RepeatrF(&x86_64::X86_64Assembler::cvtsd2si, "cvtsd2si %{reg2}, %{reg1}"), "cvtsd2si");
1192 }
1193 
TEST_F(AssemblerX86_64Test,Cvttss2si)1194 TEST_F(AssemblerX86_64Test, Cvttss2si) {
1195   DriverStr(RepeatrF(&x86_64::X86_64Assembler::cvttss2si, "cvttss2si %{reg2}, %{reg1}"),
1196             "cvttss2si");
1197 }
1198 
TEST_F(AssemblerX86_64Test,Cvttsd2si)1199 TEST_F(AssemblerX86_64Test, Cvttsd2si) {
1200   DriverStr(RepeatrF(&x86_64::X86_64Assembler::cvttsd2si, "cvttsd2si %{reg2}, %{reg1}"),
1201             "cvttsd2si");
1202 }
1203 
TEST_F(AssemblerX86_64Test,Cvtsd2ss)1204 TEST_F(AssemblerX86_64Test, Cvtsd2ss) {
1205   DriverStr(RepeatFF(&x86_64::X86_64Assembler::cvtsd2ss, "cvtsd2ss %{reg2}, %{reg1}"), "cvtsd2ss");
1206 }
1207 
TEST_F(AssemblerX86_64Test,Cvtdq2ps)1208 TEST_F(AssemblerX86_64Test, Cvtdq2ps) {
1209   DriverStr(RepeatFF(&x86_64::X86_64Assembler::cvtdq2ps, "cvtdq2ps %{reg2}, %{reg1}"), "cvtdq2ps");
1210 }
1211 
TEST_F(AssemblerX86_64Test,Cvtdq2pd)1212 TEST_F(AssemblerX86_64Test, Cvtdq2pd) {
1213   DriverStr(RepeatFF(&x86_64::X86_64Assembler::cvtdq2pd, "cvtdq2pd %{reg2}, %{reg1}"), "cvtdq2pd");
1214 }
1215 
TEST_F(AssemblerX86_64Test,Comiss)1216 TEST_F(AssemblerX86_64Test, Comiss) {
1217   DriverStr(RepeatFF(&x86_64::X86_64Assembler::comiss, "comiss %{reg2}, %{reg1}"), "comiss");
1218 }
1219 
TEST_F(AssemblerX86_64Test,Comisd)1220 TEST_F(AssemblerX86_64Test, Comisd) {
1221   DriverStr(RepeatFF(&x86_64::X86_64Assembler::comisd, "comisd %{reg2}, %{reg1}"), "comisd");
1222 }
1223 
TEST_F(AssemblerX86_64Test,Ucomiss)1224 TEST_F(AssemblerX86_64Test, Ucomiss) {
1225   DriverStr(RepeatFF(&x86_64::X86_64Assembler::ucomiss, "ucomiss %{reg2}, %{reg1}"), "ucomiss");
1226 }
1227 
TEST_F(AssemblerX86_64Test,Ucomisd)1228 TEST_F(AssemblerX86_64Test, Ucomisd) {
1229   DriverStr(RepeatFF(&x86_64::X86_64Assembler::ucomisd, "ucomisd %{reg2}, %{reg1}"), "ucomisd");
1230 }
1231 
TEST_F(AssemblerX86_64Test,Sqrtss)1232 TEST_F(AssemblerX86_64Test, Sqrtss) {
1233   DriverStr(RepeatFF(&x86_64::X86_64Assembler::sqrtss, "sqrtss %{reg2}, %{reg1}"), "sqrtss");
1234 }
1235 
TEST_F(AssemblerX86_64Test,Sqrtsd)1236 TEST_F(AssemblerX86_64Test, Sqrtsd) {
1237   DriverStr(RepeatFF(&x86_64::X86_64Assembler::sqrtsd, "sqrtsd %{reg2}, %{reg1}"), "sqrtsd");
1238 }
1239 
TEST_F(AssemblerX86_64Test,Roundss)1240 TEST_F(AssemblerX86_64Test, Roundss) {
1241   DriverStr(RepeatFFI(&x86_64::X86_64Assembler::roundss, 1, "roundss ${imm}, %{reg2}, %{reg1}"), "roundss");
1242 }
1243 
TEST_F(AssemblerX86_64Test,Roundsd)1244 TEST_F(AssemblerX86_64Test, Roundsd) {
1245   DriverStr(RepeatFFI(&x86_64::X86_64Assembler::roundsd, 1, "roundsd ${imm}, %{reg2}, %{reg1}"), "roundsd");
1246 }
1247 
TEST_F(AssemblerX86_64Test,Xorps)1248 TEST_F(AssemblerX86_64Test, Xorps) {
1249   DriverStr(RepeatFF(&x86_64::X86_64Assembler::xorps, "xorps %{reg2}, %{reg1}"), "xorps");
1250 }
1251 
TEST_F(AssemblerX86_64Test,Xorpd)1252 TEST_F(AssemblerX86_64Test, Xorpd) {
1253   DriverStr(RepeatFF(&x86_64::X86_64Assembler::xorpd, "xorpd %{reg2}, %{reg1}"), "xorpd");
1254 }
1255 
TEST_F(AssemblerX86_64Test,Pxor)1256 TEST_F(AssemblerX86_64Test, Pxor) {
1257   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pxor, "pxor %{reg2}, %{reg1}"), "pxor");
1258 }
1259 
TEST_F(AssemblerX86_64Test,Andps)1260 TEST_F(AssemblerX86_64Test, Andps) {
1261   DriverStr(RepeatFF(&x86_64::X86_64Assembler::andps, "andps %{reg2}, %{reg1}"), "andps");
1262 }
1263 
TEST_F(AssemblerX86_64Test,Andpd)1264 TEST_F(AssemblerX86_64Test, Andpd) {
1265   DriverStr(RepeatFF(&x86_64::X86_64Assembler::andpd, "andpd %{reg2}, %{reg1}"), "andpd");
1266 }
1267 
TEST_F(AssemblerX86_64Test,Pand)1268 TEST_F(AssemblerX86_64Test, Pand) {
1269   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pand, "pand %{reg2}, %{reg1}"), "pand");
1270 }
1271 
TEST_F(AssemblerX86_64Test,andnpd)1272 TEST_F(AssemblerX86_64Test, andnpd) {
1273   DriverStr(RepeatFF(&x86_64::X86_64Assembler::andnpd, "andnpd %{reg2}, %{reg1}"), "andnpd");
1274 }
1275 
TEST_F(AssemblerX86_64Test,andnps)1276 TEST_F(AssemblerX86_64Test, andnps) {
1277   DriverStr(RepeatFF(&x86_64::X86_64Assembler::andnps, "andnps %{reg2}, %{reg1}"), "andnps");
1278 }
1279 
TEST_F(AssemblerX86_64Test,Pandn)1280 TEST_F(AssemblerX86_64Test, Pandn) {
1281   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pandn, "pandn %{reg2}, %{reg1}"), "pandn");
1282 }
1283 
TEST_F(AssemblerX86_64Test,Orps)1284 TEST_F(AssemblerX86_64Test, Orps) {
1285   DriverStr(RepeatFF(&x86_64::X86_64Assembler::orps, "orps %{reg2}, %{reg1}"), "orps");
1286 }
1287 
TEST_F(AssemblerX86_64Test,Orpd)1288 TEST_F(AssemblerX86_64Test, Orpd) {
1289   DriverStr(RepeatFF(&x86_64::X86_64Assembler::orpd, "orpd %{reg2}, %{reg1}"), "orpd");
1290 }
1291 
TEST_F(AssemblerX86_64Test,Por)1292 TEST_F(AssemblerX86_64Test, Por) {
1293   DriverStr(RepeatFF(&x86_64::X86_64Assembler::por, "por %{reg2}, %{reg1}"), "por");
1294 }
1295 
TEST_F(AssemblerX86_64Test,Pavgb)1296 TEST_F(AssemblerX86_64Test, Pavgb) {
1297   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pavgb, "pavgb %{reg2}, %{reg1}"), "pavgb");
1298 }
1299 
TEST_F(AssemblerX86_64Test,Pavgw)1300 TEST_F(AssemblerX86_64Test, Pavgw) {
1301   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pavgw, "pavgw %{reg2}, %{reg1}"), "pavgw");
1302 }
1303 
TEST_F(AssemblerX86_64Test,PCmpeqb)1304 TEST_F(AssemblerX86_64Test, PCmpeqb) {
1305   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pcmpeqb, "pcmpeqb %{reg2}, %{reg1}"), "pcmpeqb");
1306 }
1307 
TEST_F(AssemblerX86_64Test,PCmpeqw)1308 TEST_F(AssemblerX86_64Test, PCmpeqw) {
1309   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pcmpeqw, "pcmpeqw %{reg2}, %{reg1}"), "pcmpeqw");
1310 }
1311 
TEST_F(AssemblerX86_64Test,PCmpeqd)1312 TEST_F(AssemblerX86_64Test, PCmpeqd) {
1313   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pcmpeqd, "pcmpeqd %{reg2}, %{reg1}"), "pcmpeqd");
1314 }
1315 
TEST_F(AssemblerX86_64Test,PCmpeqq)1316 TEST_F(AssemblerX86_64Test, PCmpeqq) {
1317   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pcmpeqq, "pcmpeqq %{reg2}, %{reg1}"), "pcmpeqq");
1318 }
1319 
TEST_F(AssemblerX86_64Test,PCmpgtb)1320 TEST_F(AssemblerX86_64Test, PCmpgtb) {
1321   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pcmpgtb, "pcmpgtb %{reg2}, %{reg1}"), "pcmpgtb");
1322 }
1323 
TEST_F(AssemblerX86_64Test,PCmpgtw)1324 TEST_F(AssemblerX86_64Test, PCmpgtw) {
1325   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pcmpgtw, "pcmpgtw %{reg2}, %{reg1}"), "pcmpgtw");
1326 }
1327 
TEST_F(AssemblerX86_64Test,PCmpgtd)1328 TEST_F(AssemblerX86_64Test, PCmpgtd) {
1329   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pcmpgtd, "pcmpgtd %{reg2}, %{reg1}"), "pcmpgtd");
1330 }
1331 
TEST_F(AssemblerX86_64Test,PCmpgtq)1332 TEST_F(AssemblerX86_64Test, PCmpgtq) {
1333   DriverStr(RepeatFF(&x86_64::X86_64Assembler::pcmpgtq, "pcmpgtq %{reg2}, %{reg1}"), "pcmpgtq");
1334 }
1335 
TEST_F(AssemblerX86_64Test,Shufps)1336 TEST_F(AssemblerX86_64Test, Shufps) {
1337   DriverStr(RepeatFFI(&x86_64::X86_64Assembler::shufps, 1, "shufps ${imm}, %{reg2}, %{reg1}"), "shufps");
1338 }
1339 
TEST_F(AssemblerX86_64Test,Shufpd)1340 TEST_F(AssemblerX86_64Test, Shufpd) {
1341   DriverStr(RepeatFFI(&x86_64::X86_64Assembler::shufpd, 1, "shufpd ${imm}, %{reg2}, %{reg1}"), "shufpd");
1342 }
1343 
TEST_F(AssemblerX86_64Test,PShufd)1344 TEST_F(AssemblerX86_64Test, PShufd) {
1345   DriverStr(RepeatFFI(&x86_64::X86_64Assembler::pshufd, 1, "pshufd ${imm}, %{reg2}, %{reg1}"), "pshufd");
1346 }
1347 
TEST_F(AssemblerX86_64Test,Punpcklbw)1348 TEST_F(AssemblerX86_64Test, Punpcklbw) {
1349   DriverStr(RepeatFF(&x86_64::X86_64Assembler::punpcklbw, "punpcklbw %{reg2}, %{reg1}"), "punpcklbw");
1350 }
1351 
TEST_F(AssemblerX86_64Test,Punpcklwd)1352 TEST_F(AssemblerX86_64Test, Punpcklwd) {
1353   DriverStr(RepeatFF(&x86_64::X86_64Assembler::punpcklwd, "punpcklwd %{reg2}, %{reg1}"), "punpcklwd");
1354 }
1355 
TEST_F(AssemblerX86_64Test,Punpckldq)1356 TEST_F(AssemblerX86_64Test, Punpckldq) {
1357   DriverStr(RepeatFF(&x86_64::X86_64Assembler::punpckldq, "punpckldq %{reg2}, %{reg1}"), "punpckldq");
1358 }
1359 
TEST_F(AssemblerX86_64Test,Punpcklqdq)1360 TEST_F(AssemblerX86_64Test, Punpcklqdq) {
1361   DriverStr(RepeatFF(&x86_64::X86_64Assembler::punpcklqdq, "punpcklqdq %{reg2}, %{reg1}"), "punpcklqdq");
1362 }
1363 
TEST_F(AssemblerX86_64Test,Psllw)1364 TEST_F(AssemblerX86_64Test, Psllw) {
1365   GetAssembler()->psllw(x86_64::XmmRegister(x86_64::XMM0),  x86_64::Immediate(1));
1366   GetAssembler()->psllw(x86_64::XmmRegister(x86_64::XMM15), x86_64::Immediate(2));
1367   DriverStr("psllw $1, %xmm0\n"
1368             "psllw $2, %xmm15\n", "psllwi");
1369 }
1370 
TEST_F(AssemblerX86_64Test,Pslld)1371 TEST_F(AssemblerX86_64Test, Pslld) {
1372   GetAssembler()->pslld(x86_64::XmmRegister(x86_64::XMM0),  x86_64::Immediate(1));
1373   GetAssembler()->pslld(x86_64::XmmRegister(x86_64::XMM15), x86_64::Immediate(2));
1374   DriverStr("pslld $1, %xmm0\n"
1375             "pslld $2, %xmm15\n", "pslldi");
1376 }
1377 
TEST_F(AssemblerX86_64Test,Psllq)1378 TEST_F(AssemblerX86_64Test, Psllq) {
1379   GetAssembler()->psllq(x86_64::XmmRegister(x86_64::XMM0),  x86_64::Immediate(1));
1380   GetAssembler()->psllq(x86_64::XmmRegister(x86_64::XMM15), x86_64::Immediate(2));
1381   DriverStr("psllq $1, %xmm0\n"
1382             "psllq $2, %xmm15\n", "psllqi");
1383 }
1384 
TEST_F(AssemblerX86_64Test,Psraw)1385 TEST_F(AssemblerX86_64Test, Psraw) {
1386   GetAssembler()->psraw(x86_64::XmmRegister(x86_64::XMM0),  x86_64::Immediate(1));
1387   GetAssembler()->psraw(x86_64::XmmRegister(x86_64::XMM15), x86_64::Immediate(2));
1388   DriverStr("psraw $1, %xmm0\n"
1389             "psraw $2, %xmm15\n", "psrawi");
1390 }
1391 
TEST_F(AssemblerX86_64Test,Psrad)1392 TEST_F(AssemblerX86_64Test, Psrad) {
1393   GetAssembler()->psrad(x86_64::XmmRegister(x86_64::XMM0),  x86_64::Immediate(1));
1394   GetAssembler()->psrad(x86_64::XmmRegister(x86_64::XMM15), x86_64::Immediate(2));
1395   DriverStr("psrad $1, %xmm0\n"
1396             "psrad $2, %xmm15\n", "psradi");
1397 }
1398 
TEST_F(AssemblerX86_64Test,Psrlw)1399 TEST_F(AssemblerX86_64Test, Psrlw) {
1400   GetAssembler()->psrlw(x86_64::XmmRegister(x86_64::XMM0),  x86_64::Immediate(1));
1401   GetAssembler()->psrlw(x86_64::XmmRegister(x86_64::XMM15), x86_64::Immediate(2));
1402   DriverStr("psrlw $1, %xmm0\n"
1403             "psrlw $2, %xmm15\n", "psrlwi");
1404 }
1405 
TEST_F(AssemblerX86_64Test,Psrld)1406 TEST_F(AssemblerX86_64Test, Psrld) {
1407   GetAssembler()->psrld(x86_64::XmmRegister(x86_64::XMM0),  x86_64::Immediate(1));
1408   GetAssembler()->psrld(x86_64::XmmRegister(x86_64::XMM15), x86_64::Immediate(2));
1409   DriverStr("psrld $1, %xmm0\n"
1410             "psrld $2, %xmm15\n", "pslldi");
1411 }
1412 
TEST_F(AssemblerX86_64Test,Psrlq)1413 TEST_F(AssemblerX86_64Test, Psrlq) {
1414   GetAssembler()->psrlq(x86_64::XmmRegister(x86_64::XMM0),  x86_64::Immediate(1));
1415   GetAssembler()->psrlq(x86_64::XmmRegister(x86_64::XMM15), x86_64::Immediate(2));
1416   DriverStr("psrlq $1, %xmm0\n"
1417             "psrlq $2, %xmm15\n", "pslrqi");
1418 }
1419 
TEST_F(AssemblerX86_64Test,UcomissAddress)1420 TEST_F(AssemblerX86_64Test, UcomissAddress) {
1421   GetAssembler()->ucomiss(x86_64::XmmRegister(x86_64::XMM0), x86_64::Address(
1422       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1423   GetAssembler()->ucomiss(x86_64::XmmRegister(x86_64::XMM1), x86_64::Address(
1424       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12));
1425   GetAssembler()->ucomiss(x86_64::XmmRegister(x86_64::XMM2), x86_64::Address(
1426       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12));
1427   GetAssembler()->ucomiss(x86_64::XmmRegister(x86_64::XMM3), x86_64::Address(
1428       x86_64::CpuRegister(x86_64::R13), 0));
1429   GetAssembler()->ucomiss(x86_64::XmmRegister(x86_64::XMM4), x86_64::Address(
1430       x86_64::CpuRegister(x86_64::R13), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_1, 0));
1431   const char* expected =
1432     "ucomiss 0xc(%RDI,%RBX,4), %xmm0\n"
1433     "ucomiss 0xc(%RDI,%R9,4), %xmm1\n"
1434     "ucomiss 0xc(%RDI,%R9,4), %xmm2\n"
1435     "ucomiss (%R13), %xmm3\n"
1436     "ucomiss (%R13,%R9,1), %xmm4\n";
1437 
1438   DriverStr(expected, "ucomiss_address");
1439 }
1440 
TEST_F(AssemblerX86_64Test,UcomisdAddress)1441 TEST_F(AssemblerX86_64Test, UcomisdAddress) {
1442   GetAssembler()->ucomisd(x86_64::XmmRegister(x86_64::XMM0), x86_64::Address(
1443       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1444   GetAssembler()->ucomisd(x86_64::XmmRegister(x86_64::XMM1), x86_64::Address(
1445       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12));
1446   GetAssembler()->ucomisd(x86_64::XmmRegister(x86_64::XMM2), x86_64::Address(
1447       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12));
1448   GetAssembler()->ucomisd(x86_64::XmmRegister(x86_64::XMM3), x86_64::Address(
1449       x86_64::CpuRegister(x86_64::R13), 0));
1450   GetAssembler()->ucomisd(x86_64::XmmRegister(x86_64::XMM4), x86_64::Address(
1451       x86_64::CpuRegister(x86_64::R13), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_1, 0));
1452   const char* expected =
1453     "ucomisd 0xc(%RDI,%RBX,4), %xmm0\n"
1454     "ucomisd 0xc(%RDI,%R9,4), %xmm1\n"
1455     "ucomisd 0xc(%RDI,%R9,4), %xmm2\n"
1456     "ucomisd (%R13), %xmm3\n"
1457     "ucomisd (%R13,%R9,1), %xmm4\n";
1458 
1459   DriverStr(expected, "ucomisd_address");
1460 }
1461 
1462 // X87
1463 
x87_fn(AssemblerX86_64Test::Base * assembler_test ATTRIBUTE_UNUSED,x86_64::X86_64Assembler * assembler)1464 std::string x87_fn(AssemblerX86_64Test::Base* assembler_test ATTRIBUTE_UNUSED,
1465                    x86_64::X86_64Assembler* assembler) {
1466   std::ostringstream str;
1467 
1468   assembler->fincstp();
1469   str << "fincstp\n";
1470 
1471   assembler->fsin();
1472   str << "fsin\n";
1473 
1474   assembler->fcos();
1475   str << "fcos\n";
1476 
1477   assembler->fptan();
1478   str << "fptan\n";
1479 
1480   return str.str();
1481 }
1482 
TEST_F(AssemblerX86_64Test,X87)1483 TEST_F(AssemblerX86_64Test, X87) {
1484   DriverFn(&x87_fn, "x87");
1485 }
1486 
TEST_F(AssemblerX86_64Test,FPUIntegerLoad)1487 TEST_F(AssemblerX86_64Test, FPUIntegerLoad) {
1488   GetAssembler()->filds(x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 4));
1489   GetAssembler()->fildl(x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 12));
1490   const char* expected =
1491       "fildl 0x4(%RSP)\n"
1492       "fildll 0xc(%RSP)\n";
1493   DriverStr(expected, "FPUIntegerLoad");
1494 }
1495 
TEST_F(AssemblerX86_64Test,FPUIntegerStore)1496 TEST_F(AssemblerX86_64Test, FPUIntegerStore) {
1497   GetAssembler()->fistps(x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 16));
1498   GetAssembler()->fistpl(x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 24));
1499   const char* expected =
1500       "fistpl 0x10(%RSP)\n"
1501       "fistpll 0x18(%RSP)\n";
1502   DriverStr(expected, "FPUIntegerStore");
1503 }
1504 
1505 ////////////////
1506 // CALL / JMP //
1507 ////////////////
1508 
TEST_F(AssemblerX86_64Test,Call)1509 TEST_F(AssemblerX86_64Test, Call) {
1510   DriverStr(RepeatR(&x86_64::X86_64Assembler::call, "call *%{reg}"), "call");
1511 }
1512 
TEST_F(AssemblerX86_64Test,Jmp)1513 TEST_F(AssemblerX86_64Test, Jmp) {
1514   DriverStr(RepeatR(&x86_64::X86_64Assembler::jmp, "jmp *%{reg}"), "jmp");
1515 }
1516 
TEST_F(AssemblerX86_64Test,Enter)1517 TEST_F(AssemblerX86_64Test, Enter) {
1518   DriverStr(RepeatI(&x86_64::X86_64Assembler::enter, 2U  /* 16b immediate */, "enter ${imm}, $0",
1519                     true  /* Only non-negative number */), "enter");
1520 }
1521 
TEST_F(AssemblerX86_64Test,RetImm)1522 TEST_F(AssemblerX86_64Test, RetImm) {
1523   DriverStr(RepeatI(&x86_64::X86_64Assembler::ret, 2U  /* 16b immediate */, "ret ${imm}",
1524                     true  /* Only non-negative number */), "reti");
1525 }
1526 
ret_and_leave_fn(AssemblerX86_64Test::Base * assembler_test ATTRIBUTE_UNUSED,x86_64::X86_64Assembler * assembler)1527 std::string ret_and_leave_fn(AssemblerX86_64Test::Base* assembler_test ATTRIBUTE_UNUSED,
1528                              x86_64::X86_64Assembler* assembler) {
1529   std::ostringstream str;
1530 
1531   assembler->ret();
1532   str << "ret\n";
1533 
1534   assembler->leave();
1535   str << "leave\n";
1536 
1537   return str.str();
1538 }
1539 
TEST_F(AssemblerX86_64Test,RetAndLeave)1540 TEST_F(AssemblerX86_64Test, RetAndLeave) {
1541   DriverFn(&ret_and_leave_fn, "retleave");
1542 }
1543 
1544 //////////
1545 // MISC //
1546 //////////
1547 
TEST_F(AssemblerX86_64Test,Bswapl)1548 TEST_F(AssemblerX86_64Test, Bswapl) {
1549   DriverStr(Repeatr(&x86_64::X86_64Assembler::bswapl, "bswap %{reg}"), "bswapl");
1550 }
1551 
TEST_F(AssemblerX86_64Test,Bswapq)1552 TEST_F(AssemblerX86_64Test, Bswapq) {
1553   DriverStr(RepeatR(&x86_64::X86_64Assembler::bswapq, "bswap %{reg}"), "bswapq");
1554 }
1555 
TEST_F(AssemblerX86_64Test,Bsfl)1556 TEST_F(AssemblerX86_64Test, Bsfl) {
1557   DriverStr(Repeatrr(&x86_64::X86_64Assembler::bsfl, "bsfl %{reg2}, %{reg1}"), "bsfl");
1558 }
1559 
TEST_F(AssemblerX86_64Test,BsflAddress)1560 TEST_F(AssemblerX86_64Test, BsflAddress) {
1561   GetAssembler()->bsfl(x86_64::CpuRegister(x86_64::R10), x86_64::Address(
1562       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1563   GetAssembler()->bsfl(x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1564       x86_64::CpuRegister(x86_64::R10), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1565   GetAssembler()->bsfl(x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1566       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12));
1567   const char* expected =
1568     "bsfl 0xc(%RDI,%RBX,4), %R10d\n"
1569     "bsfl 0xc(%R10,%RBX,4), %edi\n"
1570     "bsfl 0xc(%RDI,%R9,4), %edi\n";
1571 
1572   DriverStr(expected, "bsfl_address");
1573 }
1574 
TEST_F(AssemblerX86_64Test,Bsfq)1575 TEST_F(AssemblerX86_64Test, Bsfq) {
1576   DriverStr(RepeatRR(&x86_64::X86_64Assembler::bsfq, "bsfq %{reg2}, %{reg1}"), "bsfq");
1577 }
1578 
TEST_F(AssemblerX86_64Test,BsfqAddress)1579 TEST_F(AssemblerX86_64Test, BsfqAddress) {
1580   GetAssembler()->bsfq(x86_64::CpuRegister(x86_64::R10), x86_64::Address(
1581       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1582   GetAssembler()->bsfq(x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1583       x86_64::CpuRegister(x86_64::R10), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1584   GetAssembler()->bsfq(x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1585       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12));
1586   const char* expected =
1587     "bsfq 0xc(%RDI,%RBX,4), %R10\n"
1588     "bsfq 0xc(%R10,%RBX,4), %RDI\n"
1589     "bsfq 0xc(%RDI,%R9,4), %RDI\n";
1590 
1591   DriverStr(expected, "bsfq_address");
1592 }
1593 
TEST_F(AssemblerX86_64Test,Bsrl)1594 TEST_F(AssemblerX86_64Test, Bsrl) {
1595   DriverStr(Repeatrr(&x86_64::X86_64Assembler::bsrl, "bsrl %{reg2}, %{reg1}"), "bsrl");
1596 }
1597 
TEST_F(AssemblerX86_64Test,BsrlAddress)1598 TEST_F(AssemblerX86_64Test, BsrlAddress) {
1599   GetAssembler()->bsrl(x86_64::CpuRegister(x86_64::R10), x86_64::Address(
1600       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1601   GetAssembler()->bsrl(x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1602       x86_64::CpuRegister(x86_64::R10), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1603   GetAssembler()->bsrl(x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1604       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12));
1605   const char* expected =
1606     "bsrl 0xc(%RDI,%RBX,4), %R10d\n"
1607     "bsrl 0xc(%R10,%RBX,4), %edi\n"
1608     "bsrl 0xc(%RDI,%R9,4), %edi\n";
1609 
1610   DriverStr(expected, "bsrl_address");
1611 }
1612 
TEST_F(AssemblerX86_64Test,Bsrq)1613 TEST_F(AssemblerX86_64Test, Bsrq) {
1614   DriverStr(RepeatRR(&x86_64::X86_64Assembler::bsrq, "bsrq %{reg2}, %{reg1}"), "bsrq");
1615 }
1616 
TEST_F(AssemblerX86_64Test,BsrqAddress)1617 TEST_F(AssemblerX86_64Test, BsrqAddress) {
1618   GetAssembler()->bsrq(x86_64::CpuRegister(x86_64::R10), x86_64::Address(
1619       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1620   GetAssembler()->bsrq(x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1621       x86_64::CpuRegister(x86_64::R10), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1622   GetAssembler()->bsrq(x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1623       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12));
1624   const char* expected =
1625     "bsrq 0xc(%RDI,%RBX,4), %R10\n"
1626     "bsrq 0xc(%R10,%RBX,4), %RDI\n"
1627     "bsrq 0xc(%RDI,%R9,4), %RDI\n";
1628 
1629   DriverStr(expected, "bsrq_address");
1630 }
1631 
TEST_F(AssemblerX86_64Test,Popcntl)1632 TEST_F(AssemblerX86_64Test, Popcntl) {
1633   DriverStr(Repeatrr(&x86_64::X86_64Assembler::popcntl, "popcntl %{reg2}, %{reg1}"), "popcntl");
1634 }
1635 
TEST_F(AssemblerX86_64Test,PopcntlAddress)1636 TEST_F(AssemblerX86_64Test, PopcntlAddress) {
1637   GetAssembler()->popcntl(x86_64::CpuRegister(x86_64::R10), x86_64::Address(
1638       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1639   GetAssembler()->popcntl(x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1640       x86_64::CpuRegister(x86_64::R10), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1641   GetAssembler()->popcntl(x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1642       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12));
1643   const char* expected =
1644     "popcntl 0xc(%RDI,%RBX,4), %R10d\n"
1645     "popcntl 0xc(%R10,%RBX,4), %edi\n"
1646     "popcntl 0xc(%RDI,%R9,4), %edi\n";
1647 
1648   DriverStr(expected, "popcntl_address");
1649 }
1650 
TEST_F(AssemblerX86_64Test,Popcntq)1651 TEST_F(AssemblerX86_64Test, Popcntq) {
1652   DriverStr(RepeatRR(&x86_64::X86_64Assembler::popcntq, "popcntq %{reg2}, %{reg1}"), "popcntq");
1653 }
1654 
TEST_F(AssemblerX86_64Test,PopcntqAddress)1655 TEST_F(AssemblerX86_64Test, PopcntqAddress) {
1656   GetAssembler()->popcntq(x86_64::CpuRegister(x86_64::R10), x86_64::Address(
1657       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1658   GetAssembler()->popcntq(x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1659       x86_64::CpuRegister(x86_64::R10), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12));
1660   GetAssembler()->popcntq(x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1661       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12));
1662   const char* expected =
1663     "popcntq 0xc(%RDI,%RBX,4), %R10\n"
1664     "popcntq 0xc(%R10,%RBX,4), %RDI\n"
1665     "popcntq 0xc(%RDI,%R9,4), %RDI\n";
1666 
1667   DriverStr(expected, "popcntq_address");
1668 }
1669 
TEST_F(AssemblerX86_64Test,CmovlAddress)1670 TEST_F(AssemblerX86_64Test, CmovlAddress) {
1671   GetAssembler()->cmov(x86_64::kEqual, x86_64::CpuRegister(x86_64::R10), x86_64::Address(
1672       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12), false);
1673   GetAssembler()->cmov(x86_64::kNotEqual, x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1674       x86_64::CpuRegister(x86_64::R10), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12), false);
1675   GetAssembler()->cmov(x86_64::kEqual, x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1676       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12), false);
1677   const char* expected =
1678     "cmovzl 0xc(%RDI,%RBX,4), %R10d\n"
1679     "cmovnzl 0xc(%R10,%RBX,4), %edi\n"
1680     "cmovzl 0xc(%RDI,%R9,4), %edi\n";
1681 
1682   DriverStr(expected, "cmovl_address");
1683 }
1684 
TEST_F(AssemblerX86_64Test,CmovqAddress)1685 TEST_F(AssemblerX86_64Test, CmovqAddress) {
1686   GetAssembler()->cmov(x86_64::kEqual, x86_64::CpuRegister(x86_64::R10), x86_64::Address(
1687       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12), true);
1688   GetAssembler()->cmov(x86_64::kNotEqual, x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1689       x86_64::CpuRegister(x86_64::R10), x86_64::CpuRegister(x86_64::RBX), x86_64::TIMES_4, 12), true);
1690   GetAssembler()->cmov(x86_64::kEqual, x86_64::CpuRegister(x86_64::RDI), x86_64::Address(
1691       x86_64::CpuRegister(x86_64::RDI), x86_64::CpuRegister(x86_64::R9), x86_64::TIMES_4, 12), true);
1692   const char* expected =
1693     "cmovzq 0xc(%RDI,%RBX,4), %R10\n"
1694     "cmovnzq 0xc(%R10,%RBX,4), %rdi\n"
1695     "cmovzq 0xc(%RDI,%R9,4), %rdi\n";
1696 
1697   DriverStr(expected, "cmovq_address");
1698 }
1699 
1700 
1701 /////////////////
1702 // Near labels //
1703 /////////////////
1704 
TEST_F(AssemblerX86_64Test,Jrcxz)1705 TEST_F(AssemblerX86_64Test, Jrcxz) {
1706   x86_64::NearLabel target;
1707   GetAssembler()->jrcxz(&target);
1708   GetAssembler()->addl(x86_64::CpuRegister(x86_64::RDI),
1709                        x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 4));
1710   GetAssembler()->Bind(&target);
1711   const char* expected =
1712     "jrcxz 1f\n"
1713     "addl 4(%RSP),%EDI\n"
1714     "1:\n";
1715 
1716   DriverStr(expected, "jrcxz");
1717 }
1718 
TEST_F(AssemblerX86_64Test,NearLabel)1719 TEST_F(AssemblerX86_64Test, NearLabel) {
1720   // Test both forward and backward branches.
1721   x86_64::NearLabel start, target;
1722   GetAssembler()->Bind(&start);
1723   GetAssembler()->j(x86_64::kEqual, &target);
1724   GetAssembler()->jmp(&target);
1725   GetAssembler()->jrcxz(&target);
1726   GetAssembler()->addl(x86_64::CpuRegister(x86_64::RDI),
1727                        x86_64::Address(x86_64::CpuRegister(x86_64::RSP), 4));
1728   GetAssembler()->Bind(&target);
1729   GetAssembler()->j(x86_64::kNotEqual, &start);
1730   GetAssembler()->jmp(&start);
1731   const char* expected =
1732     "1: je 2f\n"
1733     "jmp 2f\n"
1734     "jrcxz 2f\n"
1735     "addl 4(%RSP),%EDI\n"
1736     "2: jne 1b\n"
1737     "jmp 1b\n";
1738 
1739   DriverStr(expected, "near_label");
1740 }
1741 
setcc_test_fn(AssemblerX86_64Test::Base * assembler_test,x86_64::X86_64Assembler * assembler)1742 std::string setcc_test_fn(AssemblerX86_64Test::Base* assembler_test,
1743                           x86_64::X86_64Assembler* assembler) {
1744   // From Condition
1745   /*
1746   kOverflow     =  0,
1747   kNoOverflow   =  1,
1748   kBelow        =  2,
1749   kAboveEqual   =  3,
1750   kEqual        =  4,
1751   kNotEqual     =  5,
1752   kBelowEqual   =  6,
1753   kAbove        =  7,
1754   kSign         =  8,
1755   kNotSign      =  9,
1756   kParityEven   = 10,
1757   kParityOdd    = 11,
1758   kLess         = 12,
1759   kGreaterEqual = 13,
1760   kLessEqual    = 14,
1761   */
1762   std::string suffixes[15] = { "o", "no", "b", "ae", "e", "ne", "be", "a", "s", "ns", "pe", "po",
1763                                "l", "ge", "le" };
1764 
1765   std::vector<x86_64::CpuRegister*> registers = assembler_test->GetRegisters();
1766   std::ostringstream str;
1767 
1768   for (auto reg : registers) {
1769     for (size_t i = 0; i < 15; ++i) {
1770       assembler->setcc(static_cast<x86_64::Condition>(i), *reg);
1771       str << "set" << suffixes[i] << " %" << assembler_test->GetQuaternaryRegisterName(*reg) << "\n";
1772     }
1773   }
1774 
1775   return str.str();
1776 }
1777 
TEST_F(AssemblerX86_64Test,SetCC)1778 TEST_F(AssemblerX86_64Test, SetCC) {
1779   DriverFn(&setcc_test_fn, "setcc");
1780 }
1781 
TEST_F(AssemblerX86_64Test,MovzxbRegs)1782 TEST_F(AssemblerX86_64Test, MovzxbRegs) {
1783   DriverStr(Repeatrb(&x86_64::X86_64Assembler::movzxb, "movzbl %{reg2}, %{reg1}"), "movzxb");
1784 }
1785 
TEST_F(AssemblerX86_64Test,MovsxbRegs)1786 TEST_F(AssemblerX86_64Test, MovsxbRegs) {
1787   DriverStr(Repeatrb(&x86_64::X86_64Assembler::movsxb, "movsbl %{reg2}, %{reg1}"), "movsxb");
1788 }
1789 
TEST_F(AssemblerX86_64Test,Repnescasw)1790 TEST_F(AssemblerX86_64Test, Repnescasw) {
1791   GetAssembler()->repne_scasw();
1792   const char* expected = "repne scasw\n";
1793   DriverStr(expected, "Repnescasw");
1794 }
1795 
TEST_F(AssemblerX86_64Test,Repecmpsw)1796 TEST_F(AssemblerX86_64Test, Repecmpsw) {
1797   GetAssembler()->repe_cmpsw();
1798   const char* expected = "repe cmpsw\n";
1799   DriverStr(expected, "Repecmpsw");
1800 }
1801 
TEST_F(AssemblerX86_64Test,Repecmpsl)1802 TEST_F(AssemblerX86_64Test, Repecmpsl) {
1803   GetAssembler()->repe_cmpsl();
1804   const char* expected = "repe cmpsl\n";
1805   DriverStr(expected, "Repecmpsl");
1806 }
1807 
TEST_F(AssemblerX86_64Test,Repecmpsq)1808 TEST_F(AssemblerX86_64Test, Repecmpsq) {
1809   GetAssembler()->repe_cmpsq();
1810   const char* expected = "repe cmpsq\n";
1811   DriverStr(expected, "Repecmpsq");
1812 }
1813 
TEST_F(AssemblerX86_64Test,Cmpb)1814 TEST_F(AssemblerX86_64Test, Cmpb) {
1815   GetAssembler()->cmpb(x86_64::Address(x86_64::CpuRegister(x86_64::RDI), 128),
1816                        x86_64::Immediate(0));
1817   const char* expected = "cmpb $0, 128(%RDI)\n";
1818   DriverStr(expected, "cmpb");
1819 }
1820 
TEST_F(AssemblerX86_64Test,TestbAddressImmediate)1821 TEST_F(AssemblerX86_64Test, TestbAddressImmediate) {
1822   GetAssembler()->testb(
1823       x86_64::Address(x86_64::CpuRegister(x86_64::RDI),
1824                       x86_64::CpuRegister(x86_64::RBX),
1825                       x86_64::TIMES_4,
1826                       12),
1827       x86_64::Immediate(1));
1828   GetAssembler()->testb(
1829       x86_64::Address(x86_64::CpuRegister(x86_64::RSP), FrameOffset(7)),
1830       x86_64::Immediate(-128));
1831   GetAssembler()->testb(
1832       x86_64::Address(x86_64::CpuRegister(x86_64::RBX), MemberOffset(130)),
1833       x86_64::Immediate(127));
1834   const char* expected =
1835       "testb $1, 0xc(%RDI,%RBX,4)\n"
1836       "testb $-128, 0x7(%RSP)\n"
1837       "testb $127, 0x82(%RBX)\n";
1838 
1839   DriverStr(expected, "TestbAddressImmediate");
1840 }
1841 
TEST_F(AssemblerX86_64Test,TestlAddressImmediate)1842 TEST_F(AssemblerX86_64Test, TestlAddressImmediate) {
1843   GetAssembler()->testl(
1844       x86_64::Address(x86_64::CpuRegister(x86_64::RDI),
1845                       x86_64::CpuRegister(x86_64::RBX),
1846                       x86_64::TIMES_4,
1847                       12),
1848       x86_64::Immediate(1));
1849   GetAssembler()->testl(
1850       x86_64::Address(x86_64::CpuRegister(x86_64::RSP), FrameOffset(7)),
1851       x86_64::Immediate(-100000));
1852   GetAssembler()->testl(
1853       x86_64::Address(x86_64::CpuRegister(x86_64::RBX), MemberOffset(130)),
1854       x86_64::Immediate(77777777));
1855   const char* expected =
1856       "testl $1, 0xc(%RDI,%RBX,4)\n"
1857       "testl $-100000, 0x7(%RSP)\n"
1858       "testl $77777777, 0x82(%RBX)\n";
1859 
1860   DriverStr(expected, "TestlAddressImmediate");
1861 }
1862 
1863 class JNIMacroAssemblerX86_64Test : public JNIMacroAssemblerTest<x86_64::X86_64JNIMacroAssembler> {
1864  public:
1865   using Base = JNIMacroAssemblerTest<x86_64::X86_64JNIMacroAssembler>;
1866 
1867  protected:
1868   // Get the typically used name for this architecture, e.g., aarch64, x86-64, ...
GetArchitectureString()1869   std::string GetArchitectureString() OVERRIDE {
1870     return "x86_64";
1871   }
1872 
GetDisassembleParameters()1873   std::string GetDisassembleParameters() OVERRIDE {
1874     return " -D -bbinary -mi386:x86-64 -Mx86-64,addr64,data32 --no-show-raw-insn";
1875   }
1876 
1877  private:
1878 };
1879 
ManagedFromCpu(x86_64::Register r)1880 static x86_64::X86_64ManagedRegister ManagedFromCpu(x86_64::Register r) {
1881   return x86_64::X86_64ManagedRegister::FromCpuRegister(r);
1882 }
1883 
ManagedFromFpu(x86_64::FloatRegister r)1884 static x86_64::X86_64ManagedRegister ManagedFromFpu(x86_64::FloatRegister r) {
1885   return x86_64::X86_64ManagedRegister::FromXmmRegister(r);
1886 }
1887 
buildframe_test_fn(JNIMacroAssemblerX86_64Test::Base * assembler_test ATTRIBUTE_UNUSED,x86_64::X86_64JNIMacroAssembler * assembler)1888 std::string buildframe_test_fn(JNIMacroAssemblerX86_64Test::Base* assembler_test ATTRIBUTE_UNUSED,
1889                                x86_64::X86_64JNIMacroAssembler* assembler) {
1890   // TODO: more interesting spill registers / entry spills.
1891 
1892   // Two random spill regs.
1893   const ManagedRegister raw_spill_regs[] = {
1894       ManagedFromCpu(x86_64::R10),
1895       ManagedFromCpu(x86_64::RSI)
1896   };
1897   ArrayRef<const ManagedRegister> spill_regs(raw_spill_regs);
1898 
1899   // Three random entry spills.
1900   ManagedRegisterEntrySpills entry_spills;
1901   ManagedRegisterSpill spill(ManagedFromCpu(x86_64::RAX), 8, 0);
1902   entry_spills.push_back(spill);
1903   ManagedRegisterSpill spill2(ManagedFromCpu(x86_64::RBX), 8, 8);
1904   entry_spills.push_back(spill2);
1905   ManagedRegisterSpill spill3(ManagedFromFpu(x86_64::XMM1), 8, 16);
1906   entry_spills.push_back(spill3);
1907 
1908   x86_64::X86_64ManagedRegister method_reg = ManagedFromCpu(x86_64::RDI);
1909 
1910   size_t frame_size = 10 * kStackAlignment;
1911   assembler->BuildFrame(10 * kStackAlignment, method_reg, spill_regs, entry_spills);
1912 
1913   // Construct assembly text counterpart.
1914   std::ostringstream str;
1915   // 1) Push the spill_regs.
1916   str << "pushq %rsi\n";
1917   str << "pushq %r10\n";
1918   // 2) Move down the stack pointer.
1919   ssize_t displacement = static_cast<ssize_t>(frame_size) - (spill_regs.size() * 8 + 8);
1920   str << "subq $" << displacement << ", %rsp\n";
1921   // 3) Store method reference.
1922   str << "movq %rdi, (%rsp)\n";
1923   // 4) Entry spills.
1924   str << "movq %rax, " << frame_size + 0 << "(%rsp)\n";
1925   str << "movq %rbx, " << frame_size + 8 << "(%rsp)\n";
1926   str << "movsd %xmm1, " << frame_size + 16 << "(%rsp)\n";
1927 
1928   return str.str();
1929 }
1930 
TEST_F(JNIMacroAssemblerX86_64Test,BuildFrame)1931 TEST_F(JNIMacroAssemblerX86_64Test, BuildFrame) {
1932   DriverFn(&buildframe_test_fn, "BuildFrame");
1933 }
1934 
removeframe_test_fn(JNIMacroAssemblerX86_64Test::Base * assembler_test ATTRIBUTE_UNUSED,x86_64::X86_64JNIMacroAssembler * assembler)1935 std::string removeframe_test_fn(JNIMacroAssemblerX86_64Test::Base* assembler_test ATTRIBUTE_UNUSED,
1936                                 x86_64::X86_64JNIMacroAssembler* assembler) {
1937   // TODO: more interesting spill registers / entry spills.
1938 
1939   // Two random spill regs.
1940   const ManagedRegister raw_spill_regs[] = {
1941       ManagedFromCpu(x86_64::R10),
1942       ManagedFromCpu(x86_64::RSI)
1943   };
1944   ArrayRef<const ManagedRegister> spill_regs(raw_spill_regs);
1945 
1946   size_t frame_size = 10 * kStackAlignment;
1947   assembler->RemoveFrame(10 * kStackAlignment, spill_regs);
1948 
1949   // Construct assembly text counterpart.
1950   std::ostringstream str;
1951   // 1) Move up the stack pointer.
1952   ssize_t displacement = static_cast<ssize_t>(frame_size) - spill_regs.size() * 8 - 8;
1953   str << "addq $" << displacement << ", %rsp\n";
1954   // 2) Pop spill regs.
1955   str << "popq %r10\n";
1956   str << "popq %rsi\n";
1957   str << "ret\n";
1958 
1959   return str.str();
1960 }
1961 
TEST_F(JNIMacroAssemblerX86_64Test,RemoveFrame)1962 TEST_F(JNIMacroAssemblerX86_64Test, RemoveFrame) {
1963   DriverFn(&removeframe_test_fn, "RemoveFrame");
1964 }
1965 
increaseframe_test_fn(JNIMacroAssemblerX86_64Test::Base * assembler_test ATTRIBUTE_UNUSED,x86_64::X86_64JNIMacroAssembler * assembler)1966 std::string increaseframe_test_fn(
1967     JNIMacroAssemblerX86_64Test::Base* assembler_test ATTRIBUTE_UNUSED,
1968     x86_64::X86_64JNIMacroAssembler* assembler) {
1969   assembler->IncreaseFrameSize(0U);
1970   assembler->IncreaseFrameSize(kStackAlignment);
1971   assembler->IncreaseFrameSize(10 * kStackAlignment);
1972 
1973   // Construct assembly text counterpart.
1974   std::ostringstream str;
1975   str << "addq $0, %rsp\n";
1976   str << "addq $-" << kStackAlignment << ", %rsp\n";
1977   str << "addq $-" << 10 * kStackAlignment << ", %rsp\n";
1978 
1979   return str.str();
1980 }
1981 
TEST_F(JNIMacroAssemblerX86_64Test,IncreaseFrame)1982 TEST_F(JNIMacroAssemblerX86_64Test, IncreaseFrame) {
1983   DriverFn(&increaseframe_test_fn, "IncreaseFrame");
1984 }
1985 
decreaseframe_test_fn(JNIMacroAssemblerX86_64Test::Base * assembler_test ATTRIBUTE_UNUSED,x86_64::X86_64JNIMacroAssembler * assembler)1986 std::string decreaseframe_test_fn(
1987     JNIMacroAssemblerX86_64Test::Base* assembler_test ATTRIBUTE_UNUSED,
1988     x86_64::X86_64JNIMacroAssembler* assembler) {
1989   assembler->DecreaseFrameSize(0U);
1990   assembler->DecreaseFrameSize(kStackAlignment);
1991   assembler->DecreaseFrameSize(10 * kStackAlignment);
1992 
1993   // Construct assembly text counterpart.
1994   std::ostringstream str;
1995   str << "addq $0, %rsp\n";
1996   str << "addq $" << kStackAlignment << ", %rsp\n";
1997   str << "addq $" << 10 * kStackAlignment << ", %rsp\n";
1998 
1999   return str.str();
2000 }
2001 
TEST_F(JNIMacroAssemblerX86_64Test,DecreaseFrame)2002 TEST_F(JNIMacroAssemblerX86_64Test, DecreaseFrame) {
2003   DriverFn(&decreaseframe_test_fn, "DecreaseFrame");
2004 }
2005 
2006 }  // namespace art
2007