1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/core/assembler.h"
16 :
17 : #include <vector>
18 : #include "gtest/gtest.h"
19 :
20 : namespace core {
21 :
22 : namespace {
23 :
24 : class TestSerializer : public core::AssemblerImpl::InstructionSerializer {
25 : public:
26 : struct Reference {
27 : uint32 location;
28 : const void* ref;
29 : };
30 :
31 E : TestSerializer () {
32 E : }
33 :
34 : virtual void AppendInstruction(uint32 location,
35 : const uint8* bytes,
36 : size_t num_bytes,
37 : const uint32 *ref_locations,
38 : const void* const* refs,
39 E : size_t num_refs) {
40 E : for (size_t i = 0; i < num_refs; ++i) {
41 E : Reference ref = { code.size() + ref_locations[i], refs[i] };
42 E : references.push_back(ref);
43 E : }
44 E : code.insert(code.end(), bytes, bytes + num_bytes);
45 E : }
46 :
47 : std::vector<uint8> code;
48 : std::vector<Reference> references;
49 : };
50 :
51 : class AssemblerTest : public testing::Test {
52 : public:
53 E : AssemblerTest() : asm_(0, &serializer_) {
54 E : }
55 :
56 : TestSerializer serializer_;
57 : AssemblerImpl asm_;
58 : };
59 :
60 : #define EXPECT_BYTES(...) \
61 : do { \
62 : uint8 data[] = { __VA_ARGS__ }; \
63 : ASSERT_EQ(arraysize(data), serializer_.code.size()); \
64 : EXPECT_EQ(0, memcmp(data, &serializer_.code.at(0), arraysize(data))); \
65 : serializer_.code.clear(); \
66 : } while (0)
67 :
68 : } // namespace
69 :
70 E : TEST_F(AssemblerTest, Registers) {
71 E : EXPECT_EQ(kRegisterEax, eax.code());
72 E : EXPECT_EQ(kRegisterEcx, ecx.code());
73 E : EXPECT_EQ(kRegisterEdx, edx.code());
74 E : EXPECT_EQ(kRegisterEbx, ebx.code());
75 E : EXPECT_EQ(kRegisterEsp, esp.code());
76 E : EXPECT_EQ(kRegisterEbp, ebp.code());
77 E : EXPECT_EQ(kRegisterEsi, esi.code());
78 E : EXPECT_EQ(kRegisterEdi, edi.code());
79 E : }
80 :
81 E : TEST_F(AssemblerTest, ValueImpl) {
82 E : ValueImpl imm1;
83 E : EXPECT_EQ(0, imm1.value());
84 E : EXPECT_EQ(NULL, imm1.reference());
85 E : EXPECT_EQ(kSizeNone, imm1.size());
86 E : EXPECT_TRUE(imm1 == imm1);
87 :
88 E : ValueImpl imm2(0xCAFEBABE, kSize32Bit);
89 E : EXPECT_EQ(0xCAFEBABE, imm2.value());
90 E : EXPECT_EQ(NULL, imm2.reference());
91 E : EXPECT_EQ(kSize32Bit, imm2.size());
92 E : EXPECT_TRUE(imm2 == imm2);
93 E : EXPECT_FALSE(imm2 == imm1);
94 :
95 E : int ref2 = 0;
96 E : ValueImpl imm3(0xCAFEBABE, kSize32Bit, &ref2);
97 E : EXPECT_EQ(0xCAFEBABE, imm3.value());
98 E : EXPECT_EQ(&ref2, imm3.reference());
99 E : EXPECT_EQ(kSize32Bit, imm3.size());
100 E : EXPECT_TRUE(imm3 == imm3);
101 E : EXPECT_FALSE(imm3 == imm2);
102 E : EXPECT_FALSE(imm3 == imm1);
103 :
104 E : ValueImpl imm4(0xCAFEBABE, kSize32Bit, &ref2);
105 E : EXPECT_TRUE(imm4 == imm3);
106 E : }
107 :
108 E : TEST_F(AssemblerTest, OperandImpl) {
109 : {
110 E : OperandImpl op(edi);
111 E : EXPECT_EQ(kRegisterEdi, op.base());
112 E : EXPECT_EQ(kRegisterNone, op.index());
113 E : EXPECT_EQ(kTimes1, op.scale());
114 E : EXPECT_EQ(0, op.displacement().value());
115 E : EXPECT_EQ(NULL, op.displacement().reference());
116 E : EXPECT_EQ(kSizeNone, op.displacement().size());
117 : }
118 :
119 : {
120 E : int ref = 0;
121 E : OperandImpl op(ecx, DisplacementImpl(0xCAFEBABE, kSize32Bit, &ref));
122 E : EXPECT_EQ(kRegisterEcx, op.base());
123 E : EXPECT_EQ(kRegisterNone, op.index());
124 E : EXPECT_EQ(kTimes1, op.scale());
125 E : EXPECT_EQ(0xCAFEBABE, op.displacement().value());
126 E : EXPECT_EQ(&ref, op.displacement().reference());
127 E : EXPECT_EQ(kSize32Bit, op.displacement().size());
128 : }
129 :
130 : {
131 E : int ref = 0;
132 E : OperandImpl op(DisplacementImpl(0xCAFEBABE, kSize32Bit, &ref));
133 E : EXPECT_EQ(kRegisterNone, op.base());
134 E : EXPECT_EQ(kRegisterNone, op.index());
135 E : EXPECT_EQ(kTimes1, op.scale());
136 E : EXPECT_EQ(0xCAFEBABE, op.displacement().value());
137 E : EXPECT_EQ(&ref, op.displacement().reference());
138 E : EXPECT_EQ(kSize32Bit, op.displacement().size());
139 : }
140 :
141 : {
142 E : OperandImpl op(ebp, ecx, kTimes8);
143 E : EXPECT_EQ(kRegisterEbp, op.base());
144 E : EXPECT_EQ(kRegisterEcx, op.index());
145 E : EXPECT_EQ(kTimes8, op.scale());
146 E : EXPECT_EQ(0, op.displacement().value());
147 E : EXPECT_EQ(NULL, op.displacement().reference());
148 E : EXPECT_EQ(kSizeNone, op.displacement().size());
149 : }
150 :
151 : {
152 E : int ref = 0;
153 : OperandImpl
154 E : op(ebp, ecx, kTimes2, DisplacementImpl(0xCA, kSize8Bit, &ref));
155 E : EXPECT_EQ(kRegisterEbp, op.base());
156 E : EXPECT_EQ(kRegisterEcx, op.index());
157 E : EXPECT_EQ(kTimes2, op.scale());
158 E : EXPECT_EQ(0xCA, op.displacement().value());
159 E : EXPECT_EQ(&ref, op.displacement().reference());
160 E : EXPECT_EQ(kSize8Bit, op.displacement().size());
161 : }
162 E : }
163 :
164 E : TEST_F(AssemblerTest, Call) {
165 E : asm_.set_location(0xCAFEBABE);
166 :
167 : // Immediate call.
168 E : asm_.call(ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
169 E : EXPECT_BYTES(0xE8, 0xFB, 0xFF, 0xFF, 0xFF);
170 :
171 : // Indirect call - we test only one operand encoding, as the others
172 : // are well covered in the mov instruction.
173 E : asm_.call(OperandImpl(DisplacementImpl(0xCAFEBABE, kSize32Bit, NULL)));
174 E : EXPECT_BYTES(0xFF, 0x15, 0xBE, 0xBA, 0xFE, 0xCA);
175 E : }
176 :
177 E : TEST_F(AssemblerTest, Jmp) {
178 E : asm_.set_location(0xCAFEBABE);
179 :
180 : // Immediate 8-bit reach jmp.
181 E : asm_.jmp(ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
182 E : EXPECT_BYTES(0xEB, 0xFE);
183 :
184 E : ASSERT_EQ(1, AssemblerImpl::kShortJumpOpcodeSize);
185 E : ASSERT_EQ(2, AssemblerImpl::kShortJumpSize);
186 :
187 : // Immediate 32-bit reach jmp.
188 E : asm_.jmp(ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
189 E : EXPECT_BYTES(0xE9, 0xF9, 0xFF, 0xFF, 0xFF);
190 :
191 E : ASSERT_EQ(1, AssemblerImpl::kLongJumpOpcodeSize);
192 E : ASSERT_EQ(5, AssemblerImpl::kLongJumpSize);
193 :
194 : // Indirect jmp - we test only one operand encoding, as the others
195 : // are well covered in the mov instruction.
196 E : asm_.jmp(OperandImpl(DisplacementImpl(0xCAFEBABE, kSize32Bit, NULL)));
197 E : EXPECT_BYTES(0xFF, 0x25, 0xBE, 0xBA, 0xFE, 0xCA);
198 E : }
199 :
200 E : TEST_F(AssemblerTest, Ret) {
201 E : asm_.ret();
202 E : EXPECT_BYTES(0xC3);
203 :
204 E : asm_.ret(0x4);
205 E : EXPECT_BYTES(0xC2, 0x04, 0x00);
206 E : }
207 :
208 E : TEST_F(AssemblerTest, MovByte) {
209 : asm_.mov_b(OperandImpl(eax, ebx, kTimes4,
210 : DisplacementImpl(0xCAFEBABE, kSize32Bit)),
211 E : ImmediateImpl(0xCB, kSize8Bit));
212 E : EXPECT_BYTES(0xC6, 0x84, 0x98, 0xBE, 0xBA, 0xFE, 0xCA, 0xCB);
213 E : }
214 :
215 E : TEST_F(AssemblerTest, MovzxByte) {
216 E : asm_.movzx_b(eax, OperandImpl(ebx));
217 E : EXPECT_BYTES(0x0F, 0xB6, 0x03);
218 :
219 E : asm_.movzx_b(ecx, OperandImpl(ecx, edx, kTimes2));
220 E : EXPECT_BYTES(0x0F, 0xB6, 0x0C, 0x51);
221 E : }
222 :
223 E : TEST_F(AssemblerTest, MovImmediate) {
224 : // Immediate moves.
225 E : asm_.mov(eax, ImmediateImpl(0xCAFEBABE, kSize32Bit));
226 E : EXPECT_BYTES(0xB8, 0xBE, 0xBA, 0xFE, 0xCA);
227 E : asm_.mov(ebx, ImmediateImpl(0xCAFEBABE, kSize32Bit));
228 E : EXPECT_BYTES(0xBB, 0xBE, 0xBA, 0xFE, 0xCA);
229 E : }
230 :
231 E : TEST_F(AssemblerTest, MovRegisterToRegister) {
232 : // Register to register, one case each for source and dst.
233 E : asm_.mov(eax, ebx);
234 E : EXPECT_BYTES(0x8B, 0xC3);
235 E : asm_.mov(ecx, eax);
236 E : EXPECT_BYTES(0x8B, 0xC8);
237 E : asm_.mov(ebx, eax);
238 E : EXPECT_BYTES(0x8B, 0xD8);
239 E : asm_.mov(edx, eax);
240 E : EXPECT_BYTES(0x8B, 0xD0);
241 E : asm_.mov(esp, eax);
242 E : EXPECT_BYTES(0x8B, 0xE0);
243 E : asm_.mov(ebp, eax);
244 E : EXPECT_BYTES(0x8B, 0xE8);
245 E : asm_.mov(esi, eax);
246 E : EXPECT_BYTES(0x8B, 0xF0);
247 E : asm_.mov(edi, eax);
248 E : EXPECT_BYTES(0x8B, 0xF8);
249 :
250 E : asm_.mov(ebx, eax);
251 E : EXPECT_BYTES(0x8B, 0xD8);
252 E : asm_.mov(eax, ecx);
253 E : EXPECT_BYTES(0x8B, 0xC1);
254 E : asm_.mov(eax, ebx);
255 E : EXPECT_BYTES(0x8B, 0xC3);
256 E : asm_.mov(eax, edx);
257 E : EXPECT_BYTES(0x8B, 0xC2);
258 E : asm_.mov(eax, esp);
259 E : EXPECT_BYTES(0x8B, 0xC4);
260 E : asm_.mov(eax, ebp);
261 E : EXPECT_BYTES(0x8B, 0xC5);
262 E : asm_.mov(eax, esi);
263 E : EXPECT_BYTES(0x8B, 0xC6);
264 E : asm_.mov(eax, edi);
265 E : EXPECT_BYTES(0x8B, 0xC7);
266 E : }
267 :
268 E : TEST_F(AssemblerTest, MovRegisterIndirect) {
269 : // Indirect register only source modes.
270 E : asm_.mov(ebx, OperandImpl(eax));
271 E : EXPECT_BYTES(0x8B, 0x18);
272 E : asm_.mov(eax, OperandImpl(ecx));
273 E : EXPECT_BYTES(0x8B, 0x01);
274 E : asm_.mov(edx, OperandImpl(ebx));
275 E : EXPECT_BYTES(0x8B, 0x13);
276 E : asm_.mov(ecx, OperandImpl(edx));
277 E : EXPECT_BYTES(0x8B, 0x0A);
278 :
279 : // Note that EBP is a special case that always requires a displacement.
280 E : asm_.mov(ebx, OperandImpl(ebp));
281 E : EXPECT_BYTES(0x8B, 0x5D, 0x00);
282 :
283 : // Note that ESP is a special case that always requires a SIB byte.
284 E : asm_.mov(ecx, OperandImpl(esp));
285 E : EXPECT_BYTES(0x8B, 0x0C, 0x24);
286 :
287 E : asm_.mov(ebx, OperandImpl(esi));
288 E : EXPECT_BYTES(0x8B, 0x1E);
289 E : asm_.mov(eax, OperandImpl(edi));
290 E : EXPECT_BYTES(0x8B, 0x07);
291 :
292 : // Indirect register destination modes.
293 E : asm_.mov(OperandImpl(eax), ebx);
294 E : EXPECT_BYTES(0x89, 0x18);
295 E : asm_.mov(OperandImpl(ecx), eax);
296 E : EXPECT_BYTES(0x89, 0x01);
297 E : asm_.mov(OperandImpl(ebx), edx);
298 E : EXPECT_BYTES(0x89, 0x13);
299 E : asm_.mov(OperandImpl(edx), ecx);
300 E : EXPECT_BYTES(0x89, 0x0A);
301 :
302 : // Note that EBP is a special case that always requires a displacement.
303 E : asm_.mov(OperandImpl(ebp), ebx);
304 E : EXPECT_BYTES(0x89, 0x5D, 0x00);
305 :
306 : // Note that ESP is a special case that always requires a SIB byte.
307 E : asm_.mov(OperandImpl(esp), ecx);
308 E : EXPECT_BYTES(0x89, 0x0C, 0x24);
309 :
310 E : asm_.mov(OperandImpl(esi), ebx);
311 E : EXPECT_BYTES(0x89, 0x1E);
312 E : asm_.mov(OperandImpl(edi), eax);
313 E : EXPECT_BYTES(0x89, 0x07);
314 E : }
315 :
316 E : TEST_F(AssemblerTest, MovRegisterDisplacementIndirect) {
317 : // Register & displacement source modes.
318 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
319 :
320 E : asm_.mov(ebx, OperandImpl(eax, cafebabe));
321 E : EXPECT_BYTES(0x8B, 0x98, 0xBE, 0xBA, 0xFE, 0xCA);
322 E : asm_.mov(eax, OperandImpl(ecx, cafebabe));
323 E : EXPECT_BYTES(0x8B, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
324 E : asm_.mov(eax, OperandImpl(ebx, cafebabe));
325 E : EXPECT_BYTES(0x8B, 0x83, 0xBE, 0xBA, 0xFE, 0xCA);
326 E : asm_.mov(eax, OperandImpl(edx, cafebabe));
327 E : EXPECT_BYTES(0x8B, 0x82, 0xBE, 0xBA, 0xFE, 0xCA);
328 E : asm_.mov(eax, OperandImpl(ebp, cafebabe));
329 E : EXPECT_BYTES(0x8B, 0x85, 0xBE, 0xBA, 0xFE, 0xCA);
330 :
331 : // ESP requires a SIB byte and has a longer encoding.
332 E : asm_.mov(eax, OperandImpl(esp, cafebabe));
333 E : EXPECT_BYTES(0x8B, 0x84, 0x24, 0xBE, 0xBA, 0xFE, 0xCA);
334 :
335 E : asm_.mov(eax, OperandImpl(esi, cafebabe));
336 E : EXPECT_BYTES(0x8B, 0x86, 0xBE, 0xBA, 0xFE, 0xCA);
337 E : asm_.mov(eax, OperandImpl(edi, cafebabe));
338 E : EXPECT_BYTES(0x8B, 0x87, 0xBE, 0xBA, 0xFE, 0xCA);
339 :
340 : // And destination modes.
341 E : asm_.mov(OperandImpl(eax, cafebabe), ebx);
342 E : EXPECT_BYTES(0x89, 0x98, 0xBE, 0xBA, 0xFE, 0xCA);
343 E : asm_.mov(OperandImpl(ecx, cafebabe), eax);
344 E : EXPECT_BYTES(0x89, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
345 E : asm_.mov(OperandImpl(ebx, cafebabe), eax);
346 E : EXPECT_BYTES(0x89, 0x83, 0xBE, 0xBA, 0xFE, 0xCA);
347 E : asm_.mov(OperandImpl(edx, cafebabe), eax);
348 E : EXPECT_BYTES(0x89, 0x82, 0xBE, 0xBA, 0xFE, 0xCA);
349 E : asm_.mov(OperandImpl(ebp, cafebabe), eax);
350 E : EXPECT_BYTES(0x89, 0x85, 0xBE, 0xBA, 0xFE, 0xCA);
351 :
352 : // ESP requires a SIB byte and has a longer encoding.
353 E : asm_.mov(OperandImpl(esp, cafebabe), eax);
354 E : EXPECT_BYTES(0x89, 0x84, 0x24, 0xBE, 0xBA, 0xFE, 0xCA);
355 :
356 E : asm_.mov(OperandImpl(esi, cafebabe), eax);
357 E : EXPECT_BYTES(0x89, 0x86, 0xBE, 0xBA, 0xFE, 0xCA);
358 E : asm_.mov(OperandImpl(edi, cafebabe), eax);
359 E : EXPECT_BYTES(0x89, 0x87, 0xBE, 0xBA, 0xFE, 0xCA);
360 :
361 : // Test a sampling of 8-bit displacements.
362 E : DisplacementImpl ca(0xCA, kSize8Bit, NULL);
363 :
364 : // Source.
365 E : asm_.mov(ebx, OperandImpl(eax, ca));
366 E : EXPECT_BYTES(0x8B, 0x58, 0xCA);
367 :
368 : // ESP requires a SIB byte and has a longer encoding.
369 E : asm_.mov(eax, OperandImpl(esp, ca));
370 E : EXPECT_BYTES(0x8B, 0x44, 0x24, 0xCA);
371 :
372 : // And destination modes.
373 E : asm_.mov(OperandImpl(eax, ca), ebx);
374 E : EXPECT_BYTES(0x89, 0x58, 0xCA);
375 :
376 : // ESP requires a SIB byte and has a longer encoding.
377 E : asm_.mov(OperandImpl(esp, ca), eax);
378 E : EXPECT_BYTES(0x89, 0x44, 0x24, 0xCA);
379 E : }
380 :
381 E : TEST_F(AssemblerTest, MovDisplacementIndirect) {
382 : // Displacement-only mode.
383 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
384 :
385 : // Source, note EAX has a shortcut encoding.
386 E : asm_.mov(eax, OperandImpl(cafebabe));
387 E : EXPECT_BYTES(0xA1, 0xBE, 0xBA, 0xFE, 0xCA);
388 E : asm_.mov(ecx, OperandImpl(cafebabe));
389 E : EXPECT_BYTES(0x8B, 0x0D, 0xBE, 0xBA, 0xFE, 0xCA);
390 :
391 : // Destination, again EAX is special.
392 E : asm_.mov(OperandImpl(cafebabe), eax);
393 E : EXPECT_BYTES(0xA3, 0xBE, 0xBA, 0xFE, 0xCA);
394 :
395 E : asm_.mov(OperandImpl(cafebabe), ecx);
396 E : EXPECT_BYTES(0x89, 0x0D, 0xBE, 0xBA, 0xFE, 0xCA);
397 E : }
398 :
399 E : TEST_F(AssemblerTest, MovRegisterBaseDisplacementScaleIndirect) {
400 : // There are 8 base * 7 index * 4 scales = 224 combinations.
401 : // We don't test all of them, but rather cycle through each of base,
402 : // index and scale individually.
403 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
404 :
405 : // Source mode, base register.
406 E : asm_.mov(edx, OperandImpl(ecx, eax, kTimes4, cafebabe));
407 E : EXPECT_BYTES(0x8B, 0x94, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
408 E : asm_.mov(eax, OperandImpl(ecx, eax, kTimes4, cafebabe));
409 E : EXPECT_BYTES(0x8B, 0x84, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
410 E : asm_.mov(eax, OperandImpl(edx, eax, kTimes4, cafebabe));
411 E : EXPECT_BYTES(0x8B, 0x84, 0x82, 0xBE, 0xBA, 0xFE, 0xCA);
412 E : asm_.mov(eax, OperandImpl(ebx, eax, kTimes4, cafebabe));
413 E : EXPECT_BYTES(0x8B, 0x84, 0x83, 0xBE, 0xBA, 0xFE, 0xCA);
414 E : asm_.mov(eax, OperandImpl(esp, eax, kTimes4, cafebabe));
415 E : EXPECT_BYTES(0x8B, 0x84, 0x84, 0xBE, 0xBA, 0xFE, 0xCA);
416 E : asm_.mov(eax, OperandImpl(ebp, eax, kTimes4, cafebabe));
417 E : EXPECT_BYTES(0x8B, 0x84, 0x85, 0xBE, 0xBA, 0xFE, 0xCA);
418 E : asm_.mov(eax, OperandImpl(esi, eax, kTimes4, cafebabe));
419 E : EXPECT_BYTES(0x8B, 0x84, 0x86, 0xBE, 0xBA, 0xFE, 0xCA);
420 E : asm_.mov(eax, OperandImpl(edi, eax, kTimes4, cafebabe));
421 E : EXPECT_BYTES(0x8B, 0x84, 0x87, 0xBE, 0xBA, 0xFE, 0xCA);
422 :
423 : // Source mode, index register.
424 E : asm_.mov(ebx, OperandImpl(ecx, eax, kTimes4, cafebabe));
425 E : EXPECT_BYTES(0x8B, 0x9C, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
426 E : asm_.mov(eax, OperandImpl(eax, ecx, kTimes4, cafebabe));
427 E : EXPECT_BYTES(0x8B, 0x84, 0x88, 0xBE, 0xBA, 0xFE, 0xCA);
428 E : asm_.mov(eax, OperandImpl(eax, edx, kTimes4, cafebabe));
429 E : EXPECT_BYTES(0x8B, 0x84, 0x90, 0xBE, 0xBA, 0xFE, 0xCA);
430 E : asm_.mov(eax, OperandImpl(eax, ebx, kTimes4, cafebabe));
431 E : EXPECT_BYTES(0x8B, 0x84, 0x98, 0xBE, 0xBA, 0xFE, 0xCA);
432 E : asm_.mov(eax, OperandImpl(eax, ebp, kTimes4, cafebabe));
433 E : EXPECT_BYTES(0x8B, 0x84, 0xA8, 0xBE, 0xBA, 0xFE, 0xCA);
434 E : asm_.mov(eax, OperandImpl(eax, esi, kTimes4, cafebabe));
435 E : EXPECT_BYTES(0x8B, 0x84, 0xB0, 0xBE, 0xBA, 0xFE, 0xCA);
436 E : asm_.mov(eax, OperandImpl(eax, edi, kTimes4, cafebabe));
437 E : EXPECT_BYTES(0x8B, 0x84, 0xB8, 0xBE, 0xBA, 0xFE, 0xCA);
438 :
439 : // Source mode, Scale.
440 E : asm_.mov(ebx, OperandImpl(ecx, eax, kTimes1, cafebabe));
441 E : EXPECT_BYTES(0x8B, 0x9C, 0x01, 0xBE, 0xBA, 0xFE, 0xCA);
442 E : asm_.mov(ebx, OperandImpl(ecx, eax, kTimes2, cafebabe));
443 E : EXPECT_BYTES(0x8B, 0x9C, 0x41, 0xBE, 0xBA, 0xFE, 0xCA);
444 E : asm_.mov(ebx, OperandImpl(ecx, eax, kTimes4, cafebabe));
445 E : EXPECT_BYTES(0x8B, 0x9C, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
446 E : asm_.mov(ebx, OperandImpl(ecx, eax, kTimes8, cafebabe));
447 E : EXPECT_BYTES(0x8B, 0x9C, 0xC1, 0xBE, 0xBA, 0xFE, 0xCA);
448 :
449 : // Destination mode, base register.
450 E : asm_.mov(OperandImpl(eax, eax, kTimes4, cafebabe), ecx);
451 E : EXPECT_BYTES(0x89, 0x8C, 0x80, 0xBE, 0xBA, 0xFE, 0xCA);
452 E : asm_.mov(OperandImpl(ecx, eax, kTimes4, cafebabe), eax);
453 E : EXPECT_BYTES(0x89, 0x84, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
454 E : asm_.mov(OperandImpl(edx, eax, kTimes4, cafebabe), eax);
455 E : EXPECT_BYTES(0x89, 0x84, 0x82, 0xBE, 0xBA, 0xFE, 0xCA);
456 E : asm_.mov(OperandImpl(ebx, eax, kTimes4, cafebabe), eax);
457 E : EXPECT_BYTES(0x89, 0x84, 0x83, 0xBE, 0xBA, 0xFE, 0xCA);
458 E : asm_.mov(OperandImpl(esp, eax, kTimes4, cafebabe), eax);
459 E : EXPECT_BYTES(0x89, 0x84, 0x84, 0xBE, 0xBA, 0xFE, 0xCA);
460 E : asm_.mov(OperandImpl(ebp, eax, kTimes4, cafebabe), eax);
461 E : EXPECT_BYTES(0x89, 0x84, 0x85, 0xBE, 0xBA, 0xFE, 0xCA);
462 E : asm_.mov(OperandImpl(esi, eax, kTimes4, cafebabe), eax);
463 E : EXPECT_BYTES(0x89, 0x84, 0x86, 0xBE, 0xBA, 0xFE, 0xCA);
464 E : asm_.mov(OperandImpl(edi, eax, kTimes4, cafebabe), eax);
465 E : EXPECT_BYTES(0x89, 0x84, 0x87, 0xBE, 0xBA, 0xFE, 0xCA);
466 :
467 : // Destination mode, index register.
468 E : asm_.mov(OperandImpl(ecx, eax, kTimes4, cafebabe), ebx);
469 E : EXPECT_BYTES(0x89, 0x9C, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
470 E : asm_.mov(OperandImpl(eax, ecx, kTimes4, cafebabe), eax);
471 E : EXPECT_BYTES(0x89, 0x84, 0x88, 0xBE, 0xBA, 0xFE, 0xCA);
472 E : asm_.mov(OperandImpl(eax, edx, kTimes4, cafebabe), eax);
473 E : EXPECT_BYTES(0x89, 0x84, 0x90, 0xBE, 0xBA, 0xFE, 0xCA);
474 E : asm_.mov(OperandImpl(eax, ebx, kTimes4, cafebabe), eax);
475 E : EXPECT_BYTES(0x89, 0x84, 0x98, 0xBE, 0xBA, 0xFE, 0xCA);
476 E : asm_.mov(OperandImpl(eax, ebp, kTimes4, cafebabe), eax);
477 E : EXPECT_BYTES(0x89, 0x84, 0xA8, 0xBE, 0xBA, 0xFE, 0xCA);
478 E : asm_.mov(OperandImpl(eax, esi, kTimes4, cafebabe), eax);
479 E : EXPECT_BYTES(0x89, 0x84, 0xB0, 0xBE, 0xBA, 0xFE, 0xCA);
480 E : asm_.mov(OperandImpl(eax, edi, kTimes4, cafebabe), eax);
481 E : EXPECT_BYTES(0x89, 0x84, 0xB8, 0xBE, 0xBA, 0xFE, 0xCA);
482 :
483 : // Destination mode, Scale.
484 E : asm_.mov(OperandImpl(ecx, eax, kTimes1, cafebabe), ebx);
485 E : EXPECT_BYTES(0x89, 0x9C, 0x01, 0xBE, 0xBA, 0xFE, 0xCA);
486 E : asm_.mov(OperandImpl(ecx, eax, kTimes2, cafebabe), ebx);
487 E : EXPECT_BYTES(0x89, 0x9C, 0x41, 0xBE, 0xBA, 0xFE, 0xCA);
488 E : asm_.mov(OperandImpl(ecx, eax, kTimes4, cafebabe), ebx);
489 E : EXPECT_BYTES(0x89, 0x9C, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
490 E : asm_.mov(OperandImpl(ecx, eax, kTimes8, cafebabe), ebx);
491 E : EXPECT_BYTES(0x89, 0x9C, 0xC1, 0xBE, 0xBA, 0xFE, 0xCA);
492 E : }
493 :
494 E : TEST_F(AssemblerTest, MovRegisterBaseIndexScaleIndirect) {
495 : // Tests the displacement-less [base + index * scale].
496 E : asm_.mov(edx, OperandImpl(esi, eax, kTimes8));
497 E : EXPECT_BYTES(0x8B, 0x14, 0xC6);
498 E : }
499 :
500 E : TEST_F(AssemblerTest, MovRegisterDisplacementScaleIndirect) {
501 : // Tests [index * scale + displ] modes, which are always encoded with a
502 : // 32-bit displacement, including [index * scale], which has a zero 32-bit
503 : // displacement that will be omitted from disassembly.
504 :
505 E : DisplacementImpl one(1, kSize8Bit, NULL);
506 :
507 : // Source mode.
508 E : asm_.mov(edx, OperandImpl(eax, kTimes4, one));
509 E : EXPECT_BYTES(0x8B, 0x14, 0x85, 0x01, 0x00, 0x00, 0x00);
510 E : asm_.mov(edx, OperandImpl(ecx, kTimes4, one));
511 E : EXPECT_BYTES(0x8B, 0x14, 0x8D, 0x01, 0x00, 0x00, 0x00);
512 E : asm_.mov(edx, OperandImpl(edx, kTimes4, one));
513 E : EXPECT_BYTES(0x8B, 0x14, 0x95, 0x01, 0x00, 0x00, 0x00);
514 E : asm_.mov(edx, OperandImpl(ebx, kTimes4, one));
515 E : EXPECT_BYTES(0x8B, 0x14, 0x9D, 0x01, 0x00, 0x00, 0x00);
516 E : asm_.mov(edx, OperandImpl(ebp, kTimes4, one));
517 E : EXPECT_BYTES(0x8B, 0x14, 0xAD, 0x01, 0x00, 0x00, 0x00);
518 E : asm_.mov(edx, OperandImpl(esi, kTimes4, one));
519 E : EXPECT_BYTES(0x8B, 0x14, 0xB5, 0x01, 0x00, 0x00, 0x00);
520 E : asm_.mov(edx, OperandImpl(edi, kTimes4, one));
521 E : EXPECT_BYTES(0x8B, 0x14, 0xBD, 0x01, 0x00, 0x00, 0x00);
522 :
523 : // Destination mode.
524 E : asm_.mov(OperandImpl(eax, kTimes4, one), edx);
525 E : EXPECT_BYTES(0x89, 0x14, 0x85, 0x01, 0x00, 0x00, 0x00);
526 E : asm_.mov(OperandImpl(ecx, kTimes4, one), edx);
527 E : EXPECT_BYTES(0x89, 0x14, 0x8D, 0x01, 0x00, 0x00, 0x00);
528 E : asm_.mov(OperandImpl(edx, kTimes4, one), edx);
529 E : EXPECT_BYTES(0x89, 0x14, 0x95, 0x01, 0x00, 0x00, 0x00);
530 E : asm_.mov(OperandImpl(ebx, kTimes4, one), edx);
531 E : EXPECT_BYTES(0x89, 0x14, 0x9D, 0x01, 0x00, 0x00, 0x00);
532 E : asm_.mov(OperandImpl(ebp, kTimes4, one), edx);
533 E : EXPECT_BYTES(0x89, 0x14, 0xAD, 0x01, 0x00, 0x00, 0x00);
534 E : asm_.mov(OperandImpl(esi, kTimes4, one), edx);
535 E : EXPECT_BYTES(0x89, 0x14, 0xB5, 0x01, 0x00, 0x00, 0x00);
536 E : asm_.mov(OperandImpl(edi, kTimes4, one), edx);
537 E : EXPECT_BYTES(0x89, 0x14, 0xBD, 0x01, 0x00, 0x00, 0x00);
538 E : }
539 :
540 E : TEST_F(AssemblerTest, MovImmToRegisterDisplacementScaleIndirect) {
541 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
542 E : ImmediateImpl deadbeef(0xDEADBEEF, kSize32Bit, NULL);
543 :
544 : // We expect the operand encoding has been adequately tested elsewhere,
545 : // so we only test one variant here.
546 E : asm_.mov(OperandImpl(ecx, eax, kTimes4, cafebabe), deadbeef);
547 : EXPECT_BYTES(0xC7, 0x84, 0x81,
548 : 0xBE, 0xBA, 0xFE, 0xCA,
549 E : 0xEF, 0xBE, 0xAD, 0xDE);
550 E : }
551 :
552 E : TEST_F(AssemblerTest, MovWithSegmentPrefix) {
553 : // Indirect register destination modes.
554 E : asm_.mov_fs(OperandImpl(eax), ebx);
555 E : EXPECT_BYTES(0x64, 0x89, 0x18);
556 E : asm_.mov_fs(OperandImpl(ecx), eax);
557 E : EXPECT_BYTES(0x64, 0x89, 0x01);
558 E : asm_.mov_fs(OperandImpl(ebx), edx);
559 E : EXPECT_BYTES(0x64, 0x89, 0x13);
560 E : asm_.mov_fs(OperandImpl(edx), ecx);
561 E : EXPECT_BYTES(0x64, 0x89, 0x0A);
562 :
563 : // Indirect register only source modes.
564 E : asm_.mov_fs(ebx, OperandImpl(eax));
565 E : EXPECT_BYTES(0x64, 0x8B, 0x18);
566 E : asm_.mov_fs(eax, OperandImpl(ecx));
567 E : EXPECT_BYTES(0x64, 0x8B, 0x01);
568 E : asm_.mov_fs(edx, OperandImpl(ebx));
569 E : EXPECT_BYTES(0x64, 0x8B, 0x13);
570 E : asm_.mov_fs(ecx, OperandImpl(edx));
571 E : EXPECT_BYTES(0x64, 0x8B, 0x0A);
572 E : }
573 :
574 E : TEST_F(AssemblerTest, LeaRegisterIndirect) {
575 : // Indirect register only source modes.
576 E : asm_.lea(ebx, OperandImpl(eax));
577 E : EXPECT_BYTES(0x8D, 0x18);
578 E : asm_.lea(eax, OperandImpl(ecx));
579 E : EXPECT_BYTES(0x8D, 0x01);
580 E : asm_.lea(edx, OperandImpl(ebx));
581 E : EXPECT_BYTES(0x8D, 0x13);
582 E : asm_.lea(ecx, OperandImpl(edx));
583 E : EXPECT_BYTES(0x8D, 0x0A);
584 :
585 : // Note that EBP is a special case that always requires a displacement.
586 E : asm_.lea(ebx, OperandImpl(ebp));
587 E : EXPECT_BYTES(0x8D, 0x5D, 0x00);
588 :
589 : // Note that ESP is a special case that always requires a SIB byte.
590 E : asm_.lea(ecx, OperandImpl(esp));
591 E : EXPECT_BYTES(0x8D, 0x0C, 0x24);
592 :
593 E : asm_.lea(ebx, OperandImpl(esi));
594 E : EXPECT_BYTES(0x8D, 0x1E);
595 E : asm_.lea(eax, OperandImpl(edi));
596 E : EXPECT_BYTES(0x8D, 0x07);
597 E : }
598 :
599 E : TEST_F(AssemblerTest, LeaRegisterDisplacementIndirect) {
600 : // Register & displacement source modes.
601 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
602 :
603 E : asm_.lea(ebx, OperandImpl(eax, cafebabe));
604 E : EXPECT_BYTES(0x8D, 0x98, 0xBE, 0xBA, 0xFE, 0xCA);
605 E : asm_.lea(eax, OperandImpl(ecx, cafebabe));
606 E : EXPECT_BYTES(0x8D, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
607 E : asm_.lea(eax, OperandImpl(ebx, cafebabe));
608 E : EXPECT_BYTES(0x8D, 0x83, 0xBE, 0xBA, 0xFE, 0xCA);
609 E : asm_.lea(eax, OperandImpl(edx, cafebabe));
610 E : EXPECT_BYTES(0x8D, 0x82, 0xBE, 0xBA, 0xFE, 0xCA);
611 E : asm_.lea(eax, OperandImpl(ebp, cafebabe));
612 E : EXPECT_BYTES(0x8D, 0x85, 0xBE, 0xBA, 0xFE, 0xCA);
613 :
614 : // ESP requires a SIB byte and has a longer encoding.
615 E : asm_.lea(eax, OperandImpl(esp, cafebabe));
616 E : EXPECT_BYTES(0x8D, 0x84, 0x24, 0xBE, 0xBA, 0xFE, 0xCA);
617 :
618 E : asm_.lea(eax, OperandImpl(esi, cafebabe));
619 E : EXPECT_BYTES(0x8D, 0x86, 0xBE, 0xBA, 0xFE, 0xCA);
620 E : asm_.lea(eax, OperandImpl(edi, cafebabe));
621 E : EXPECT_BYTES(0x8D, 0x87, 0xBE, 0xBA, 0xFE, 0xCA);
622 :
623 : // Test a sampling of 8-bit displacements.
624 E : DisplacementImpl ca(0xCA, kSize8Bit, NULL);
625 :
626 : // Source.
627 E : asm_.lea(ebx, OperandImpl(eax, ca));
628 E : EXPECT_BYTES(0x8D, 0x58, 0xCA);
629 :
630 : // ESP requires a SIB byte and has a longer encoding.
631 E : asm_.lea(eax, OperandImpl(esp, ca));
632 E : EXPECT_BYTES(0x8D, 0x44, 0x24, 0xCA);
633 E : }
634 :
635 E : TEST_F(AssemblerTest, LeaDisplacementIndirect) {
636 : // Displacement-only mode.
637 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
638 :
639 E : asm_.lea(eax, OperandImpl(cafebabe));
640 E : EXPECT_BYTES(0x8D, 0x05, 0xBE, 0xBA, 0xFE, 0xCA);
641 E : asm_.lea(ecx, OperandImpl(cafebabe));
642 E : EXPECT_BYTES(0x8D, 0x0D, 0xBE, 0xBA, 0xFE, 0xCA);
643 E : }
644 :
645 E : TEST_F(AssemblerTest, LeaRegisterDisplacementScaleIndirect) {
646 : // There are 8 base * 7 index * 4 scales = 224 combinations.
647 : // We don't test all of them, but rather cycle through each of base,
648 : // index and scale individually.
649 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
650 :
651 : // Source mode, base register.
652 E : asm_.lea(edx, OperandImpl(ecx, eax, kTimes4, cafebabe));
653 E : EXPECT_BYTES(0x8D, 0x94, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
654 E : asm_.lea(eax, OperandImpl(ecx, eax, kTimes4, cafebabe));
655 E : EXPECT_BYTES(0x8D, 0x84, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
656 E : asm_.lea(eax, OperandImpl(edx, eax, kTimes4, cafebabe));
657 E : EXPECT_BYTES(0x8D, 0x84, 0x82, 0xBE, 0xBA, 0xFE, 0xCA);
658 E : asm_.lea(eax, OperandImpl(ebx, eax, kTimes4, cafebabe));
659 E : EXPECT_BYTES(0x8D, 0x84, 0x83, 0xBE, 0xBA, 0xFE, 0xCA);
660 E : asm_.lea(eax, OperandImpl(esp, eax, kTimes4, cafebabe));
661 E : EXPECT_BYTES(0x8D, 0x84, 0x84, 0xBE, 0xBA, 0xFE, 0xCA);
662 E : asm_.lea(eax, OperandImpl(ebp, eax, kTimes4, cafebabe));
663 E : EXPECT_BYTES(0x8D, 0x84, 0x85, 0xBE, 0xBA, 0xFE, 0xCA);
664 E : asm_.lea(eax, OperandImpl(esi, eax, kTimes4, cafebabe));
665 E : EXPECT_BYTES(0x8D, 0x84, 0x86, 0xBE, 0xBA, 0xFE, 0xCA);
666 E : asm_.lea(eax, OperandImpl(edi, eax, kTimes4, cafebabe));
667 E : EXPECT_BYTES(0x8D, 0x84, 0x87, 0xBE, 0xBA, 0xFE, 0xCA);
668 :
669 : // Source mode, index register.
670 E : asm_.lea(ebx, OperandImpl(ecx, eax, kTimes4, cafebabe));
671 E : EXPECT_BYTES(0x8D, 0x9C, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
672 E : asm_.lea(eax, OperandImpl(eax, ecx, kTimes4, cafebabe));
673 E : EXPECT_BYTES(0x8D, 0x84, 0x88, 0xBE, 0xBA, 0xFE, 0xCA);
674 E : asm_.lea(eax, OperandImpl(eax, edx, kTimes4, cafebabe));
675 E : EXPECT_BYTES(0x8D, 0x84, 0x90, 0xBE, 0xBA, 0xFE, 0xCA);
676 E : asm_.lea(eax, OperandImpl(eax, ebx, kTimes4, cafebabe));
677 E : EXPECT_BYTES(0x8D, 0x84, 0x98, 0xBE, 0xBA, 0xFE, 0xCA);
678 E : asm_.lea(eax, OperandImpl(eax, ebp, kTimes4, cafebabe));
679 E : EXPECT_BYTES(0x8D, 0x84, 0xA8, 0xBE, 0xBA, 0xFE, 0xCA);
680 E : asm_.lea(eax, OperandImpl(eax, esi, kTimes4, cafebabe));
681 E : EXPECT_BYTES(0x8D, 0x84, 0xB0, 0xBE, 0xBA, 0xFE, 0xCA);
682 E : asm_.lea(eax, OperandImpl(eax, edi, kTimes4, cafebabe));
683 E : EXPECT_BYTES(0x8D, 0x84, 0xB8, 0xBE, 0xBA, 0xFE, 0xCA);
684 :
685 : // Source mode, Scale.
686 E : asm_.lea(ebx, OperandImpl(ecx, eax, kTimes1, cafebabe));
687 E : EXPECT_BYTES(0x8D, 0x9C, 0x01, 0xBE, 0xBA, 0xFE, 0xCA);
688 E : asm_.lea(ebx, OperandImpl(ecx, eax, kTimes2, cafebabe));
689 E : EXPECT_BYTES(0x8D, 0x9C, 0x41, 0xBE, 0xBA, 0xFE, 0xCA);
690 E : asm_.lea(ebx, OperandImpl(ecx, eax, kTimes4, cafebabe));
691 E : EXPECT_BYTES(0x8D, 0x9C, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
692 E : asm_.lea(ebx, OperandImpl(ecx, eax, kTimes8, cafebabe));
693 E : EXPECT_BYTES(0x8D, 0x9C, 0xC1, 0xBE, 0xBA, 0xFE, 0xCA);
694 E : }
695 :
696 E : TEST_F(AssemblerTest, Push) {
697 : // Register push.
698 E : asm_.push(eax);
699 E : asm_.push(ecx);
700 E : asm_.push(edx);
701 E : asm_.push(ebx);
702 E : asm_.push(esp);
703 E : asm_.push(ebp);
704 E : asm_.push(esi);
705 E : asm_.push(edi);
706 E : EXPECT_BYTES(0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57);
707 :
708 : // Immediate push.
709 E : asm_.push(ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
710 E : EXPECT_BYTES(0x68, 0xBE, 0xBA, 0xFE, 0xCA);
711 :
712 : // General push, try one variant as the rest are OperandImpl encodings.
713 E : asm_.push(OperandImpl(DisplacementImpl(0xCAFEBABE, kSize32Bit, NULL)));
714 E : EXPECT_BYTES(0xFF, 0x35, 0xBE, 0xBA, 0xFE, 0xCA);
715 E : }
716 :
717 E : TEST_F(AssemblerTest, Pop) {
718 : // Register pop.
719 E : asm_.pop(eax);
720 E : asm_.pop(ecx);
721 E : asm_.pop(edx);
722 E : asm_.pop(ebx);
723 E : asm_.pop(esp);
724 E : asm_.pop(ebp);
725 E : asm_.pop(esi);
726 E : asm_.pop(edi);
727 E : EXPECT_BYTES(0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F);
728 :
729 : // General pop, try one variant as the rest are OperandImpl encodings.
730 E : asm_.pop(OperandImpl(DisplacementImpl(0xCAFEBABE, kSize32Bit, NULL)));
731 E : EXPECT_BYTES(0x8F, 0x05, 0xBE, 0xBA, 0xFE, 0xCA);
732 E : }
733 :
734 E : TEST_F(AssemblerTest, Flags) {
735 E : asm_.pushfd();
736 E : asm_.popfd();
737 E : asm_.lahf();
738 E : asm_.sahf();
739 E : EXPECT_BYTES(0x9C, 0x9D, 0x9F, 0x9E);
740 E : }
741 :
742 E : TEST_F(AssemblerTest, TestByte) {
743 E : asm_.test_b(eax, ebx);
744 E : EXPECT_BYTES(0x84, 0xC3);
745 E : asm_.test_b(ebx, eax);
746 E : EXPECT_BYTES(0x84, 0xD8);
747 :
748 E : asm_.test_b(eax, ImmediateImpl(0x0A, kSize8Bit));
749 E : EXPECT_BYTES(0xA8, 0x0A);
750 E : asm_.test_b(ebx, ImmediateImpl(0x0A, kSize8Bit));
751 E : EXPECT_BYTES(0xF6, 0xC3, 0x0A);
752 E : }
753 :
754 E : TEST_F(AssemblerTest, Test) {
755 E : asm_.test(eax, ecx);
756 E : EXPECT_BYTES(0x85, 0xC1);
757 E : asm_.test(ecx, OperandImpl(eax));
758 E : EXPECT_BYTES(0x85, 0x08);
759 E : asm_.test(ecx, OperandImpl(eax, DisplacementImpl(10, kSize8Bit)));
760 E : EXPECT_BYTES(0x85, 0x48, 0x0A);
761 E : asm_.test(ecx, OperandImpl(eax, DisplacementImpl(10, kSize32Bit)));
762 E : EXPECT_BYTES(0x85, 0x88, 0x0A, 0x00, 0x00, 0x00);
763 :
764 E : asm_.test(ecx, eax);
765 E : EXPECT_BYTES(0x85, 0xC8);
766 E : asm_.test(ecx, OperandImpl(eax));
767 E : EXPECT_BYTES(0x85, 0x08);
768 E : asm_.test(ecx, OperandImpl(eax, DisplacementImpl(10, kSize8Bit)));
769 E : EXPECT_BYTES(0x85, 0x48, 0x0A);
770 E : asm_.test(ecx, OperandImpl(eax, DisplacementImpl(10, kSize32Bit)));
771 E : EXPECT_BYTES(0x85, 0x88, 0x0A, 0x00, 0x00, 0x00);
772 :
773 E : asm_.test(OperandImpl(eax), ecx);
774 E : EXPECT_BYTES(0x85, 0x08);
775 E : asm_.test(OperandImpl(eax, DisplacementImpl(10, kSize8Bit)), ecx);
776 E : EXPECT_BYTES(0x85, 0x48, 0x0A);
777 E : asm_.test(OperandImpl(eax, DisplacementImpl(10, kSize32Bit)), ecx);
778 E : EXPECT_BYTES(0x85, 0x88, 0x0A, 0x00, 0x00, 0x00);
779 :
780 E : asm_.test(eax, ImmediateImpl(0x0A, kSize8Bit));
781 E : EXPECT_BYTES(0xA9, 0x0A, 0x00, 0x00, 0x00);
782 E : asm_.test(ecx, ImmediateImpl(0x0A, kSize8Bit));
783 E : EXPECT_BYTES(0xF7, 0xC1, 0x0A, 0x00, 0x00, 0x00);
784 E : asm_.test(ecx, ImmediateImpl(0xDEADBEEF, kSize32Bit));
785 E : EXPECT_BYTES(0xF7, 0xC1, 0xEF, 0xBE, 0xAD, 0xDE);
786 :
787 E : asm_.test(OperandImpl(eax), ImmediateImpl(1, kSize8Bit));
788 E : EXPECT_BYTES(0xF7, 0x00, 0x01, 0x00, 0x00, 0x00);
789 E : asm_.test(OperandImpl(eax), ImmediateImpl(0xDEADBEEF, kSize32Bit));
790 E : EXPECT_BYTES(0xF7, 0x00, 0xEF, 0xBE, 0xAD, 0xDE);
791 : asm_.test(OperandImpl(eax, DisplacementImpl(10, kSize8Bit)),
792 E : ImmediateImpl(0x1, kSize8Bit));
793 E : EXPECT_BYTES(0xF7, 0x40, 0x0A, 0x01, 0x00, 0x00, 0x00);
794 : asm_.test(OperandImpl(eax, DisplacementImpl(10, kSize8Bit)),
795 E : ImmediateImpl(0xDEADBEEF, kSize32Bit));
796 E : EXPECT_BYTES(0xF7, 0x40, 0x0A, 0xEF, 0xBE, 0xAD, 0xDE);
797 : asm_.test(OperandImpl(eax, DisplacementImpl(10, kSize32Bit)),
798 E : ImmediateImpl(0xDEADBEEF, kSize32Bit));
799 E : EXPECT_BYTES(0xF7, 0x80, 0x0A, 0x00, 0x00, 0x00, 0xEF, 0xBE, 0xAD, 0xDE);
800 :
801 : // Special EAX mode + immediate.
802 E : asm_.test(eax, ImmediateImpl(0xDEADBEEF, kSize32Bit));
803 E : EXPECT_BYTES(0xA9, 0xEF, 0xBE, 0xAD, 0xDE);
804 E : }
805 :
806 E : TEST_F(AssemblerTest, CmpByte) {
807 E : asm_.cmp_b(eax, ebx);
808 E : EXPECT_BYTES(0x3A, 0xC3);
809 E : asm_.cmp_b(ebx, eax);
810 E : EXPECT_BYTES(0x3A, 0xD8);
811 :
812 E : asm_.cmp_b(eax, ImmediateImpl(0x0A, kSize8Bit));
813 E : EXPECT_BYTES(0x3C, 0x0A);
814 E : asm_.cmp_b(ebx, ImmediateImpl(0x0A, kSize8Bit));
815 E : EXPECT_BYTES(0x80, 0xFB, 0x0A);
816 E : }
817 :
818 E : TEST_F(AssemblerTest, Cmp) {
819 E : asm_.cmp(eax, ecx);
820 E : EXPECT_BYTES(0x3B, 0xC1);
821 E : asm_.cmp(ecx, OperandImpl(eax));
822 E : EXPECT_BYTES(0x3B, 0x08);
823 E : asm_.cmp(ecx, OperandImpl(eax, DisplacementImpl(10, kSize8Bit)));
824 E : EXPECT_BYTES(0x3B, 0x48, 0x0A);
825 E : asm_.cmp(ecx, OperandImpl(eax, DisplacementImpl(10, kSize32Bit)));
826 E : EXPECT_BYTES(0x3B, 0x88, 0x0A, 0x00, 0x00, 0x00);
827 :
828 E : asm_.cmp(ecx, eax);
829 E : EXPECT_BYTES(0x3B, 0xC8);
830 E : asm_.cmp(ecx, OperandImpl(eax));
831 E : EXPECT_BYTES(0x3B, 0x08);
832 E : asm_.cmp(ecx, OperandImpl(eax, DisplacementImpl(10, kSize8Bit)));
833 E : EXPECT_BYTES(0x3B, 0x48, 0x0A);
834 E : asm_.cmp(ecx, OperandImpl(eax, DisplacementImpl(10, kSize32Bit)));
835 E : EXPECT_BYTES(0x3B, 0x88, 0x0A, 0x00, 0x00, 0x00);
836 :
837 E : asm_.cmp(OperandImpl(eax), ecx);
838 E : EXPECT_BYTES(0x39, 0x08);
839 E : asm_.cmp(OperandImpl(eax, DisplacementImpl(10, kSize8Bit)), ecx);
840 E : EXPECT_BYTES(0x39, 0x48, 0x0A);
841 E : asm_.cmp(OperandImpl(eax, DisplacementImpl(10, kSize32Bit)), ecx);
842 E : EXPECT_BYTES(0x39, 0x88, 0x0A, 0x00, 0x00, 0x00);
843 :
844 E : asm_.cmp(eax, ImmediateImpl(0x0A, kSize8Bit));
845 E : EXPECT_BYTES(0x83, 0xF8, 0x0A);
846 E : asm_.cmp(ecx, ImmediateImpl(0x0A, kSize8Bit));
847 E : EXPECT_BYTES(0x83, 0xF9, 0x0A);
848 E : asm_.cmp(ecx, ImmediateImpl(0xDEADBEEF, kSize32Bit));
849 E : EXPECT_BYTES(0x81, 0xF9, 0xEF, 0xBE, 0xAD, 0xDE);
850 :
851 E : asm_.cmp(OperandImpl(eax), ImmediateImpl(1, kSize8Bit));
852 E : EXPECT_BYTES(0x83, 0x38, 0x01);
853 E : asm_.cmp(OperandImpl(eax), ImmediateImpl(0xDEADBEEF, kSize32Bit));
854 E : EXPECT_BYTES(0x81, 0x38, 0xEF, 0xBE, 0xAD, 0xDE);
855 : asm_.cmp(OperandImpl(eax, DisplacementImpl(10, kSize8Bit)),
856 E : ImmediateImpl(0x1, kSize8Bit));
857 E : EXPECT_BYTES(0x83, 0x78, 0x0A, 0x1);
858 : asm_.cmp(OperandImpl(eax, DisplacementImpl(10, kSize8Bit)),
859 E : ImmediateImpl(0xDEADBEEF, kSize32Bit));
860 E : EXPECT_BYTES(0x81, 0x78, 0x0A, 0xEF, 0xBE, 0xAD, 0xDE);
861 : asm_.cmp(OperandImpl(eax, DisplacementImpl(10, kSize32Bit)),
862 E : ImmediateImpl(0xDEADBEEF, kSize32Bit));
863 E : EXPECT_BYTES(0x81, 0xB8, 0x0A, 0x00, 0x00, 0x00, 0xEF, 0xBE, 0xAD, 0xDE);
864 :
865 : // Special EAX mode + immediate.
866 E : asm_.cmp(eax, ImmediateImpl(0xDEADBEEF, kSize32Bit));
867 E : EXPECT_BYTES(0x3D, 0xEF, 0xBE, 0xAD, 0xDE);
868 E : }
869 :
870 E : TEST_F(AssemblerTest, AddByte) {
871 E : asm_.add_b(eax, ebx);
872 E : EXPECT_BYTES(0x02, 0xC3);
873 E : asm_.add_b(ebx, eax);
874 E : EXPECT_BYTES(0x02, 0xD8);
875 :
876 E : asm_.add_b(eax, ImmediateImpl(0x0A, kSize8Bit));
877 E : EXPECT_BYTES(0x04, 0x0A);
878 E : asm_.add_b(ebx, ImmediateImpl(0x0A, kSize8Bit));
879 E : EXPECT_BYTES(0x80, 0xC3, 0x0A);
880 E : }
881 :
882 :
883 E : TEST_F(AssemblerTest, Add) {
884 E : asm_.add(eax, eax);
885 E : EXPECT_BYTES(0x03, 0xC0);
886 E : asm_.add(eax, OperandImpl(eax));
887 E : EXPECT_BYTES(0x03, 0x00);
888 E : asm_.add(eax, OperandImpl(eax, DisplacementImpl(10, kSize8Bit)));
889 E : EXPECT_BYTES(0x03, 0x40, 0x0A);
890 E : asm_.add(eax, OperandImpl(eax, DisplacementImpl(10, kSize32Bit)));
891 E : EXPECT_BYTES(0x03, 0x80, 0x0A, 0x00, 0x00, 0x00);
892 :
893 E : asm_.add(ecx, eax);
894 E : EXPECT_BYTES(0x03, 0xC8);
895 E : asm_.add(ecx, OperandImpl(eax));
896 E : EXPECT_BYTES(0x03, 0x08);
897 E : asm_.add(ecx, OperandImpl(eax, DisplacementImpl(10, kSize8Bit)));
898 E : EXPECT_BYTES(0x03, 0x48, 0x0A);
899 E : asm_.add(ecx, OperandImpl(eax, DisplacementImpl(10, kSize32Bit)));
900 E : EXPECT_BYTES(0x03, 0x88, 0x0A, 0x00, 0x00, 0x00);
901 :
902 E : asm_.add(eax, ecx);
903 E : EXPECT_BYTES(0x03, 0xC1);
904 E : asm_.add(OperandImpl(eax), ecx);
905 E : EXPECT_BYTES(0x01, 0x08);
906 E : asm_.add(OperandImpl(eax, DisplacementImpl(10, kSize8Bit)), ecx);
907 E : EXPECT_BYTES(0x01, 0x48, 0x0A);
908 E : asm_.add(OperandImpl(eax, DisplacementImpl(10, kSize32Bit)), ecx);
909 E : EXPECT_BYTES(0x01, 0x88, 0x0A, 0x00, 0x00, 0x00);
910 :
911 E : asm_.add(eax, ImmediateImpl(0x0A, kSize8Bit));
912 E : EXPECT_BYTES(0x83, 0xC0, 0x0A);
913 E : asm_.add(ecx, ImmediateImpl(0x0A, kSize8Bit));
914 E : EXPECT_BYTES(0x83, 0xC1, 0x0A);
915 E : asm_.add(ecx, ImmediateImpl(0xDEADBEEF, kSize32Bit));
916 E : EXPECT_BYTES(0x81, 0xC1, 0xEF, 0xBE, 0xAD, 0xDE);
917 :
918 E : asm_.add(OperandImpl(eax), ImmediateImpl(1, kSize8Bit));
919 E : EXPECT_BYTES(0x83, 0x00, 0x01);
920 E : asm_.add(OperandImpl(eax), ImmediateImpl(0xDEADBEEF, kSize32Bit));
921 E : EXPECT_BYTES(0x81, 0x00, 0xEF, 0xBE, 0xAD, 0xDE);
922 : asm_.add(OperandImpl(eax, DisplacementImpl(10, kSize8Bit)),
923 E : ImmediateImpl(0xDEADBEEF, kSize32Bit));
924 E : EXPECT_BYTES(0x81, 0x40, 0x0A, 0xEF, 0xBE, 0xAD, 0xDE);
925 : asm_.add(OperandImpl(eax, DisplacementImpl(10, kSize32Bit)),
926 E : ImmediateImpl(0xDEADBEEF, kSize32Bit));
927 E : EXPECT_BYTES(0x81, 0x80, 0x0A, 0x00, 0x00, 0x00, 0xEF, 0xBE, 0xAD, 0xDE);
928 :
929 : // Special EAX mode + immediate.
930 E : asm_.add(eax, ImmediateImpl(0xDEADBEEF, kSize32Bit));
931 E : EXPECT_BYTES(0x05, 0xEF, 0xBE, 0xAD, 0xDE);
932 E : }
933 :
934 E : TEST_F(AssemblerTest, SubByte) {
935 E : asm_.sub_b(eax, ebx);
936 E : EXPECT_BYTES(0x2A, 0xC3);
937 E : asm_.sub_b(ebx, eax);
938 E : EXPECT_BYTES(0x2A, 0xD8);
939 :
940 E : asm_.sub_b(eax, ImmediateImpl(0x0A, kSize8Bit));
941 E : EXPECT_BYTES(0x2C, 0x0A);
942 E : asm_.sub_b(ebx, ImmediateImpl(0x0A, kSize8Bit));
943 E : EXPECT_BYTES(0x80, 0xEB, 0x0A);
944 E : }
945 :
946 E : TEST_F(AssemblerTest, Sub) {
947 E : asm_.sub(eax, eax);
948 E : EXPECT_BYTES(0x2B, 0xC0);
949 E : asm_.sub(eax, OperandImpl(eax));
950 E : EXPECT_BYTES(0x2B, 0x00);
951 E : asm_.sub(eax, OperandImpl(eax, DisplacementImpl(10, kSize8Bit)));
952 E : EXPECT_BYTES(0x2B, 0x40, 0x0A);
953 E : asm_.sub(eax, OperandImpl(eax, DisplacementImpl(10, kSize32Bit)));
954 E : EXPECT_BYTES(0x2B, 0x80, 0x0A, 0x00, 0x00, 0x00);
955 :
956 E : asm_.sub(ecx, eax);
957 E : EXPECT_BYTES(0x2B, 0xC8);
958 E : asm_.sub(ecx, OperandImpl(eax));
959 E : EXPECT_BYTES(0x2B, 0x08);
960 E : asm_.sub(ecx, OperandImpl(eax, DisplacementImpl(10, kSize8Bit)));
961 E : EXPECT_BYTES(0x2B, 0x48, 0x0A);
962 E : asm_.sub(ecx, OperandImpl(eax, DisplacementImpl(10, kSize32Bit)));
963 E : EXPECT_BYTES(0x2B, 0x88, 0x0A, 0x00, 0x00, 0x00);
964 :
965 E : asm_.sub(eax, ecx);
966 E : EXPECT_BYTES(0x2B, 0xC1);
967 E : asm_.sub(OperandImpl(eax), ecx);
968 E : EXPECT_BYTES(0x29, 0x08);
969 E : asm_.sub(OperandImpl(eax, DisplacementImpl(10, kSize8Bit)), ecx);
970 E : EXPECT_BYTES(0x29, 0x48, 0x0A);
971 E : asm_.sub(OperandImpl(eax, DisplacementImpl(10, kSize32Bit)), ecx);
972 E : EXPECT_BYTES(0x29, 0x88, 0x0A, 0x00, 0x00, 0x00);
973 :
974 E : asm_.sub(eax, ImmediateImpl(0x0A, kSize8Bit));
975 E : EXPECT_BYTES(0x83, 0xE8, 0x0A);
976 E : asm_.sub(ecx, ImmediateImpl(0x0A, kSize8Bit));
977 E : EXPECT_BYTES(0x83, 0xE9, 0x0A);
978 E : asm_.sub(ecx, ImmediateImpl(0xDEADBEEF, kSize32Bit));
979 E : EXPECT_BYTES(0x81, 0xE9, 0xEF, 0xBE, 0xAD, 0xDE);
980 :
981 E : asm_.sub(OperandImpl(eax), ImmediateImpl(0x1, kSize8Bit));
982 E : EXPECT_BYTES(0x83, 0x28, 0x01);
983 E : asm_.sub(OperandImpl(eax), ImmediateImpl(0xDEADBEEF, kSize32Bit));
984 E : EXPECT_BYTES(0x81, 0x28, 0xEF, 0xBE, 0xAD, 0xDE);
985 : asm_.sub(OperandImpl(eax, DisplacementImpl(10, kSize8Bit)),
986 E : ImmediateImpl(0xDEADBEEF, kSize32Bit));
987 E : EXPECT_BYTES(0x81, 0x68, 0x0A, 0xEF, 0xBE, 0xAD, 0xDE);
988 : asm_.sub(OperandImpl(eax, DisplacementImpl(10, kSize32Bit)),
989 E : ImmediateImpl(0xDEADBEEF, kSize32Bit));
990 E : EXPECT_BYTES(0x81, 0xA8, 0x0A, 0x00, 0x00, 0x00, 0xEF, 0xBE, 0xAD, 0xDE);
991 :
992 : // Special EAX mode + immediate.
993 E : asm_.sub(eax, ImmediateImpl(0xDEADBEEF, kSize32Bit));
994 E : EXPECT_BYTES(0x2D, 0xEF, 0xBE, 0xAD, 0xDE);
995 E : }
996 :
997 E : TEST_F(AssemblerTest, Shl) {
998 E : asm_.shl(eax, ImmediateImpl(0x1, kSize8Bit));
999 E : EXPECT_BYTES(0xD1, 0xE0);
1000 E : asm_.shl(eax, ImmediateImpl(0x3, kSize8Bit));
1001 E : EXPECT_BYTES(0xC1, 0xE0, 0x03);
1002 E : asm_.shl(ecx, ImmediateImpl(0x1, kSize8Bit));
1003 E : EXPECT_BYTES(0xD1, 0xE1);
1004 E : asm_.shl(ecx, ImmediateImpl(0x3, kSize8Bit));
1005 E : EXPECT_BYTES(0xC1, 0xE1, 0x03);
1006 E : }
1007 :
1008 E : TEST_F(AssemblerTest, Shr) {
1009 E : asm_.shr(eax, ImmediateImpl(0x1, kSize8Bit));
1010 E : EXPECT_BYTES(0xD1, 0xE8);
1011 E : asm_.shr(eax, ImmediateImpl(0x3, kSize8Bit));
1012 E : EXPECT_BYTES(0xC1, 0xE8, 0x03);
1013 E : asm_.shr(ecx, ImmediateImpl(0x1, kSize8Bit));
1014 E : EXPECT_BYTES(0xD1, 0xE9);
1015 E : asm_.shr(ecx, ImmediateImpl(0x3, kSize8Bit));
1016 E : EXPECT_BYTES(0xC1, 0xE9, 0x03);
1017 E : }
1018 :
1019 E : TEST_F(AssemblerTest, Ja) {
1020 E : ConditionCode cc = kAbove;
1021 E : asm_.set_location(0xCAFEBABE);
1022 :
1023 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1024 E : EXPECT_BYTES(0x77, 0xFE);
1025 :
1026 E : ASSERT_EQ(1, AssemblerImpl::kShortBranchOpcodeSize);
1027 E : ASSERT_EQ(2, AssemblerImpl::kShortBranchSize);
1028 :
1029 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1030 E : EXPECT_BYTES(0x0F, 0x87, 0xF8, 0xFF, 0xFF, 0xFF);
1031 :
1032 E : ASSERT_EQ(2, AssemblerImpl::kLongBranchOpcodeSize);
1033 E : ASSERT_EQ(6, AssemblerImpl::kLongBranchSize);
1034 E : }
1035 :
1036 E : TEST_F(AssemblerTest, Jae) {
1037 E : ConditionCode cc = kAboveEqual;
1038 E : asm_.set_location(0xCAFEBABE);
1039 :
1040 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1041 E : EXPECT_BYTES(0x73, 0xFE);
1042 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1043 E : EXPECT_BYTES(0x0F, 0x83, 0xF8, 0xFF, 0xFF, 0xFF);
1044 E : }
1045 :
1046 E : TEST_F(AssemblerTest, Jb) {
1047 E : ConditionCode cc = kBelow;
1048 E : asm_.set_location(0xCAFEBABE);
1049 :
1050 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1051 E : EXPECT_BYTES(0x72, 0xFE);
1052 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1053 E : EXPECT_BYTES(0x0F, 0x82, 0xF8, 0xFF, 0xFF, 0xFF);
1054 E : }
1055 :
1056 E : TEST_F(AssemblerTest, Jbe) {
1057 E : ConditionCode cc = kBelowEqual;
1058 E : asm_.set_location(0xCAFEBABE);
1059 :
1060 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1061 E : EXPECT_BYTES(0x76, 0xFE);
1062 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1063 E : EXPECT_BYTES(0x0F, 0x86, 0xF8, 0xFF, 0xFF, 0xFF);
1064 E : }
1065 :
1066 E : TEST_F(AssemblerTest, Jc) {
1067 E : ConditionCode cc = kCarry;
1068 E : asm_.set_location(0xCAFEBABE);
1069 :
1070 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1071 E : EXPECT_BYTES(0x72, 0xFE);
1072 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1073 E : EXPECT_BYTES(0x0F, 0x82, 0xF8, 0xFF, 0xFF, 0xFF);
1074 E : }
1075 :
1076 E : TEST_F(AssemblerTest, Je) {
1077 E : ConditionCode cc = kEqual;
1078 E : asm_.set_location(0xCAFEBABE);
1079 :
1080 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1081 E : EXPECT_BYTES(0x74, 0xFE);
1082 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1083 E : EXPECT_BYTES(0x0F, 0x84, 0xF8, 0xFF, 0xFF, 0xFF);
1084 E : }
1085 :
1086 E : TEST_F(AssemblerTest, Jecxz) {
1087 E : asm_.set_location(0xCAFEBABE);
1088 :
1089 E : asm_.jecxz(ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1090 E : EXPECT_BYTES(0xE3, 0xFE);
1091 E : }
1092 :
1093 E : TEST_F(AssemblerTest, Jg) {
1094 E : ConditionCode cc = kGreater;
1095 E : asm_.set_location(0xCAFEBABE);
1096 :
1097 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1098 E : EXPECT_BYTES(0x7F, 0xFE);
1099 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1100 E : EXPECT_BYTES(0x0F, 0x8F, 0xF8, 0xFF, 0xFF, 0xFF);
1101 E : }
1102 :
1103 E : TEST_F(AssemblerTest, Jge) {
1104 E : ConditionCode cc = kGreaterEqual;
1105 E : asm_.set_location(0xCAFEBABE);
1106 :
1107 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1108 E : EXPECT_BYTES(0x7D, 0xFE);
1109 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1110 E : EXPECT_BYTES(0x0F, 0x8D, 0xF8, 0xFF, 0xFF, 0xFF);
1111 E : }
1112 :
1113 E : TEST_F(AssemblerTest, Jl) {
1114 E : ConditionCode cc = kLess;
1115 E : asm_.set_location(0xCAFEBABE);
1116 :
1117 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1118 E : EXPECT_BYTES(0x7C, 0xFE);
1119 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1120 E : EXPECT_BYTES(0x0F, 0x8C, 0xF8, 0xFF, 0xFF, 0xFF);
1121 E : }
1122 :
1123 E : TEST_F(AssemblerTest, Jle) {
1124 E : ConditionCode cc = kLessEqual;
1125 E : asm_.set_location(0xCAFEBABE);
1126 :
1127 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1128 E : EXPECT_BYTES(0x7E, 0xFE);
1129 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1130 E : EXPECT_BYTES(0x0F, 0x8E, 0xF8, 0xFF, 0xFF, 0xFF);
1131 E : }
1132 :
1133 E : TEST_F(AssemblerTest, Jo) {
1134 E : ConditionCode cc = kOverflow;
1135 E : asm_.set_location(0xCAFEBABE);
1136 :
1137 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1138 E : EXPECT_BYTES(0x70, 0xFE);
1139 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1140 E : EXPECT_BYTES(0x0F, 0x80, 0xF8, 0xFF, 0xFF, 0xFF);
1141 E : }
1142 :
1143 E : TEST_F(AssemblerTest, Jpe) {
1144 E : ConditionCode cc = kParityEven;
1145 E : asm_.set_location(0xCAFEBABE);
1146 :
1147 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1148 E : EXPECT_BYTES(0x7A, 0xFE);
1149 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1150 E : EXPECT_BYTES(0x0F, 0x8A, 0xF8, 0xFF, 0xFF, 0xFF);
1151 E : }
1152 :
1153 E : TEST_F(AssemblerTest, Jpo) {
1154 E : ConditionCode cc = kParityOdd;
1155 E : asm_.set_location(0xCAFEBABE);
1156 :
1157 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1158 E : EXPECT_BYTES(0x7B, 0xFE);
1159 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1160 E : EXPECT_BYTES(0x0F, 0x8B, 0xF8, 0xFF, 0xFF, 0xFF);
1161 E : }
1162 :
1163 E : TEST_F(AssemblerTest, Js) {
1164 E : ConditionCode cc = kSign;
1165 E : asm_.set_location(0xCAFEBABE);
1166 : COMPILE_ASSERT(kSign == kNegative, kSignAndPositiveAreAliases);
1167 :
1168 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1169 E : EXPECT_BYTES(0x78, 0xFE);
1170 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1171 E : EXPECT_BYTES(0x0F, 0x88, 0xF8, 0xFF, 0xFF, 0xFF);
1172 E : }
1173 :
1174 E : TEST_F(AssemblerTest, Jz) {
1175 E : ConditionCode cc = kZero;
1176 E : asm_.set_location(0xCAFEBABE);
1177 :
1178 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1179 E : EXPECT_BYTES(0x74, 0xFE);
1180 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1181 E : EXPECT_BYTES(0x0F, 0x84, 0xF8, 0xFF, 0xFF, 0xFF);
1182 E : }
1183 :
1184 E : TEST_F(AssemblerTest, Jnc) {
1185 E : ConditionCode cc = kNotCarry;
1186 E : asm_.set_location(0xCAFEBABE);
1187 :
1188 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1189 E : EXPECT_BYTES(0x73, 0xFE);
1190 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1191 E : EXPECT_BYTES(0x0F, 0x83, 0xF8, 0xFF, 0xFF, 0xFF);
1192 E : }
1193 :
1194 E : TEST_F(AssemblerTest, Jne) {
1195 E : ConditionCode cc = kNotEqual;
1196 E : asm_.set_location(0xCAFEBABE);
1197 :
1198 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1199 E : EXPECT_BYTES(0x75, 0xFE);
1200 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1201 E : EXPECT_BYTES(0x0F, 0x85, 0xF8, 0xFF, 0xFF, 0xFF);
1202 E : }
1203 :
1204 E : TEST_F(AssemblerTest, Jno) {
1205 E : ConditionCode cc = kNoOverflow;
1206 E : asm_.set_location(0xCAFEBABE);
1207 :
1208 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1209 E : EXPECT_BYTES(0x71, 0xFE);
1210 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1211 E : EXPECT_BYTES(0x0F, 0x81, 0xF8, 0xFF, 0xFF, 0xFF);
1212 E : }
1213 :
1214 E : TEST_F(AssemblerTest, Jns) {
1215 : COMPILE_ASSERT(kNotSign == kPositive, kSignAndPositiveAreAliases);
1216 E : ConditionCode cc = kNotSign;
1217 E : asm_.set_location(0xCAFEBABE);
1218 :
1219 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1220 E : EXPECT_BYTES(0x79, 0xFE);
1221 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1222 E : EXPECT_BYTES(0x0F, 0x89, 0xF8, 0xFF, 0xFF, 0xFF);
1223 E : }
1224 :
1225 E : TEST_F(AssemblerTest, Jnz) {
1226 E : ConditionCode cc = kNotZero;
1227 E : asm_.set_location(0xCAFEBABE);
1228 :
1229 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1230 E : EXPECT_BYTES(0x75, 0xFE);
1231 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
1232 E : EXPECT_BYTES(0x0F, 0x85, 0xF8, 0xFF, 0xFF, 0xFF);
1233 E : }
1234 :
1235 E : TEST_F(AssemblerTest, Seto) {
1236 E : asm_.set_location(0xCAFEBABE);
1237 E : asm_.set(kOverflow, core::eax);
1238 E : EXPECT_BYTES(0x0F, 0x90, 0xC0);
1239 E : }
1240 :
1241 E : TEST_F(AssemblerTest, Setno) {
1242 E : asm_.set(kNoOverflow, core::ebx);
1243 E : EXPECT_BYTES(0x0F, 0x91, 0xC3);
1244 E : }
1245 :
1246 E : TEST_F(AssemblerTest, Sete) {
1247 E : asm_.set(kEqual, core::eax);
1248 E : EXPECT_BYTES(0x0F, 0x94, 0xC0);
1249 E : }
1250 :
1251 E : TEST_F(AssemblerTest, Setne) {
1252 E : asm_.set(kNotEqual, core::eax);
1253 E : EXPECT_BYTES(0x0F, 0x95, 0xC0);
1254 E : }
1255 :
1256 E : TEST_F(AssemblerTest, Setb) {
1257 E : asm_.set(kBelow, core::eax);
1258 E : EXPECT_BYTES(0x0F, 0x92, 0xC0);
1259 E : }
1260 :
1261 E : TEST_F(AssemblerTest, Loop) {
1262 E : asm_.set_location(0xCAFEBABE);
1263 :
1264 E : asm_.loop(ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1265 E : EXPECT_BYTES(0xE2, 0xFE);
1266 E : }
1267 :
1268 E : TEST_F(AssemblerTest, Loope) {
1269 E : asm_.set_location(0xCAFEBABE);
1270 :
1271 E : asm_.loope(ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1272 E : EXPECT_BYTES(0xE1, 0xFE);
1273 E : }
1274 :
1275 E : TEST_F(AssemblerTest, Loopne) {
1276 E : asm_.set_location(0xCAFEBABE);
1277 :
1278 E : asm_.loopne(ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
1279 E : EXPECT_BYTES(0xE0, 0xFE);
1280 E : }
1281 :
1282 E : TEST_F(AssemblerTest, References) {
1283 : // We arbitrarily use the MOV instruction to test reference propagation.
1284 : static const int ref1 = 1;
1285 E : asm_.mov(eax, ImmediateImpl(0, kSize8Bit, &ref1));
1286 :
1287 : static const int ref2 = 2;
1288 : asm_.mov(eax, OperandImpl(eax, ebx, kTimes4,
1289 E : DisplacementImpl(0, kSize32Bit, &ref2)));
1290 :
1291 : static const int ref3 = 3;
1292 : static const int ref4 = 4;
1293 : asm_.mov(OperandImpl(eax, ebx, kTimes4,
1294 : DisplacementImpl(0, kSize32Bit, &ref3)),
1295 E : ImmediateImpl(0, kSize32Bit, &ref4));
1296 :
1297 E : EXPECT_EQ(4, serializer_.references.size());
1298 :
1299 E : EXPECT_EQ(1, serializer_.references[0].location);
1300 E : EXPECT_EQ(&ref1, serializer_.references[0].ref);
1301 :
1302 E : EXPECT_EQ(8, serializer_.references[1].location);
1303 E : EXPECT_EQ(&ref2, serializer_.references[1].ref);
1304 :
1305 E : EXPECT_EQ(15, serializer_.references[2].location);
1306 E : EXPECT_EQ(&ref3, serializer_.references[2].ref);
1307 :
1308 E : EXPECT_EQ(19, serializer_.references[3].location);
1309 E : EXPECT_EQ(&ref4, serializer_.references[3].ref);
1310 E : }
1311 :
1312 : } // namespace core
|