1 : // Copyright 2012 Google Inc.
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 : {
83 E : ValueImpl imm1;
84 :
85 E : EXPECT_EQ(0, imm1.value());
86 E : EXPECT_EQ(NULL, imm1.reference());
87 E : EXPECT_EQ(kSizeNone, imm1.size());
88 : }
89 :
90 : {
91 E : ValueImpl imm2(0xCAFEBABE, kSize32Bit);
92 :
93 E : EXPECT_EQ(0xCAFEBABE, imm2.value());
94 E : EXPECT_EQ(NULL, imm2.reference());
95 E : EXPECT_EQ(kSize32Bit, imm2.size());
96 : }
97 :
98 : {
99 E : int ref2 = 0;
100 E : ValueImpl imm3(0xCAFEBABE, kSize32Bit, &ref2);
101 :
102 E : EXPECT_EQ(0xCAFEBABE, imm3.value());
103 E : EXPECT_EQ(&ref2, imm3.reference());
104 E : EXPECT_EQ(kSize32Bit, imm3.size());
105 : }
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 jmp.
181 E : asm_.jmp(ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
182 E : EXPECT_BYTES(0xE9, 0xFB, 0xFF, 0xFF, 0xFF);
183 :
184 : // Indirect jmp - we test only one operand encoding, as the others
185 : // are well covered in the mov instruction.
186 E : asm_.jmp(OperandImpl(DisplacementImpl(0xCAFEBABE, kSize32Bit, NULL)));
187 E : EXPECT_BYTES(0xFF, 0x25, 0xBE, 0xBA, 0xFE, 0xCA);
188 E : }
189 :
190 E : TEST_F(AssemblerTest, Ret) {
191 E : asm_.ret();
192 E : EXPECT_BYTES(0xC3);
193 :
194 E : asm_.ret(0x4);
195 E : EXPECT_BYTES(0xC2, 0x04, 0x00);
196 E : }
197 :
198 E : TEST_F(AssemblerTest, MovByte) {
199 : asm_.mov_b(OperandImpl(eax, ebx, kTimes4,
200 : DisplacementImpl(0xCAFEBABE, kSize32Bit)),
201 E : ImmediateImpl(0xCB, kSize8Bit));
202 E : EXPECT_BYTES(0xC6, 0x84, 0x98, 0xBE, 0xBA, 0xFE, 0xCA, 0xCB);
203 E : }
204 :
205 E : TEST_F(AssemblerTest, MovImmediate) {
206 : // Immediate moves.
207 E : asm_.mov(eax, ImmediateImpl(0xCAFEBABE, kSize32Bit));
208 E : EXPECT_BYTES(0xB8, 0xBE, 0xBA, 0xFE, 0xCA);
209 E : asm_.mov(ebx, ImmediateImpl(0xCAFEBABE, kSize32Bit));
210 E : EXPECT_BYTES(0xBB, 0xBE, 0xBA, 0xFE, 0xCA);
211 E : }
212 :
213 E : TEST_F(AssemblerTest, MovRegisterToRegister) {
214 : // Register to register, one case each for source and dst.
215 E : asm_.mov(eax, ebx);
216 E : EXPECT_BYTES(0x8B, 0xC3);
217 E : asm_.mov(ecx, eax);
218 E : EXPECT_BYTES(0x8B, 0xC8);
219 E : asm_.mov(ebx, eax);
220 E : EXPECT_BYTES(0x8B, 0xD8);
221 E : asm_.mov(edx, eax);
222 E : EXPECT_BYTES(0x8B, 0xD0);
223 E : asm_.mov(esp, eax);
224 E : EXPECT_BYTES(0x8B, 0xE0);
225 E : asm_.mov(ebp, eax);
226 E : EXPECT_BYTES(0x8B, 0xE8);
227 E : asm_.mov(esi, eax);
228 E : EXPECT_BYTES(0x8B, 0xF0);
229 E : asm_.mov(edi, eax);
230 E : EXPECT_BYTES(0x8B, 0xF8);
231 :
232 E : asm_.mov(ebx, eax);
233 E : EXPECT_BYTES(0x8B, 0xD8);
234 E : asm_.mov(eax, ecx);
235 E : EXPECT_BYTES(0x8B, 0xC1);
236 E : asm_.mov(eax, ebx);
237 E : EXPECT_BYTES(0x8B, 0xC3);
238 E : asm_.mov(eax, edx);
239 E : EXPECT_BYTES(0x8B, 0xC2);
240 E : asm_.mov(eax, esp);
241 E : EXPECT_BYTES(0x8B, 0xC4);
242 E : asm_.mov(eax, ebp);
243 E : EXPECT_BYTES(0x8B, 0xC5);
244 E : asm_.mov(eax, esi);
245 E : EXPECT_BYTES(0x8B, 0xC6);
246 E : asm_.mov(eax, edi);
247 E : EXPECT_BYTES(0x8B, 0xC7);
248 E : }
249 :
250 E : TEST_F(AssemblerTest, MovRegisterIndirect) {
251 : // Indirect register only source modes.
252 E : asm_.mov(ebx, OperandImpl(eax));
253 E : EXPECT_BYTES(0x8B, 0x18);
254 E : asm_.mov(eax, OperandImpl(ecx));
255 E : EXPECT_BYTES(0x8B, 0x01);
256 E : asm_.mov(edx, OperandImpl(ebx));
257 E : EXPECT_BYTES(0x8B, 0x13);
258 E : asm_.mov(ecx, OperandImpl(edx));
259 E : EXPECT_BYTES(0x8B, 0x0A);
260 :
261 : // Note that EBP is a special case that always requires a displacement.
262 E : asm_.mov(ebx, OperandImpl(ebp));
263 E : EXPECT_BYTES(0x8B, 0x5D, 0x00);
264 :
265 : // Note that ESP is a special case that always requires a SIB byte.
266 E : asm_.mov(ecx, OperandImpl(esp));
267 E : EXPECT_BYTES(0x8B, 0x0C, 0x24);
268 :
269 E : asm_.mov(ebx, OperandImpl(esi));
270 E : EXPECT_BYTES(0x8B, 0x1E);
271 E : asm_.mov(eax, OperandImpl(edi));
272 E : EXPECT_BYTES(0x8B, 0x07);
273 :
274 : // Indirect register destination modes.
275 E : asm_.mov(OperandImpl(eax), ebx);
276 E : EXPECT_BYTES(0x89, 0x18);
277 E : asm_.mov(OperandImpl(ecx), eax);
278 E : EXPECT_BYTES(0x89, 0x01);
279 E : asm_.mov(OperandImpl(ebx), edx);
280 E : EXPECT_BYTES(0x89, 0x13);
281 E : asm_.mov(OperandImpl(edx), ecx);
282 E : EXPECT_BYTES(0x89, 0x0A);
283 :
284 : // Note that EBP is a special case that always requires a displacement.
285 E : asm_.mov(OperandImpl(ebp), ebx);
286 E : EXPECT_BYTES(0x89, 0x5D, 0x00);
287 :
288 : // Note that ESP is a special case that always requires a SIB byte.
289 E : asm_.mov(OperandImpl(esp), ecx);
290 E : EXPECT_BYTES(0x89, 0x0C, 0x24);
291 :
292 E : asm_.mov(OperandImpl(esi), ebx);
293 E : EXPECT_BYTES(0x89, 0x1E);
294 E : asm_.mov(OperandImpl(edi), eax);
295 E : EXPECT_BYTES(0x89, 0x07);
296 E : }
297 :
298 E : TEST_F(AssemblerTest, MovRegisterDisplacementIndirect) {
299 : // Register & displacement source modes.
300 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
301 :
302 E : asm_.mov(ebx, OperandImpl(eax, cafebabe));
303 E : EXPECT_BYTES(0x8B, 0x98, 0xBE, 0xBA, 0xFE, 0xCA);
304 E : asm_.mov(eax, OperandImpl(ecx, cafebabe));
305 E : EXPECT_BYTES(0x8B, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
306 E : asm_.mov(eax, OperandImpl(ebx, cafebabe));
307 E : EXPECT_BYTES(0x8B, 0x83, 0xBE, 0xBA, 0xFE, 0xCA);
308 E : asm_.mov(eax, OperandImpl(edx, cafebabe));
309 E : EXPECT_BYTES(0x8B, 0x82, 0xBE, 0xBA, 0xFE, 0xCA);
310 E : asm_.mov(eax, OperandImpl(ebp, cafebabe));
311 E : EXPECT_BYTES(0x8B, 0x85, 0xBE, 0xBA, 0xFE, 0xCA);
312 :
313 : // ESP requires a SIB byte and has a longer encoding.
314 E : asm_.mov(eax, OperandImpl(esp, cafebabe));
315 E : EXPECT_BYTES(0x8B, 0x84, 0x24, 0xBE, 0xBA, 0xFE, 0xCA);
316 :
317 E : asm_.mov(eax, OperandImpl(esi, cafebabe));
318 E : EXPECT_BYTES(0x8B, 0x86, 0xBE, 0xBA, 0xFE, 0xCA);
319 E : asm_.mov(eax, OperandImpl(edi, cafebabe));
320 E : EXPECT_BYTES(0x8B, 0x87, 0xBE, 0xBA, 0xFE, 0xCA);
321 :
322 : // And destination modes.
323 E : asm_.mov(OperandImpl(eax, cafebabe), ebx);
324 E : EXPECT_BYTES(0x89, 0x98, 0xBE, 0xBA, 0xFE, 0xCA);
325 E : asm_.mov(OperandImpl(ecx, cafebabe), eax);
326 E : EXPECT_BYTES(0x89, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
327 E : asm_.mov(OperandImpl(ebx, cafebabe), eax);
328 E : EXPECT_BYTES(0x89, 0x83, 0xBE, 0xBA, 0xFE, 0xCA);
329 E : asm_.mov(OperandImpl(edx, cafebabe), eax);
330 E : EXPECT_BYTES(0x89, 0x82, 0xBE, 0xBA, 0xFE, 0xCA);
331 E : asm_.mov(OperandImpl(ebp, cafebabe), eax);
332 E : EXPECT_BYTES(0x89, 0x85, 0xBE, 0xBA, 0xFE, 0xCA);
333 :
334 : // ESP requires a SIB byte and has a longer encoding.
335 E : asm_.mov(OperandImpl(esp, cafebabe), eax);
336 E : EXPECT_BYTES(0x89, 0x84, 0x24, 0xBE, 0xBA, 0xFE, 0xCA);
337 :
338 E : asm_.mov(OperandImpl(esi, cafebabe), eax);
339 E : EXPECT_BYTES(0x89, 0x86, 0xBE, 0xBA, 0xFE, 0xCA);
340 E : asm_.mov(OperandImpl(edi, cafebabe), eax);
341 E : EXPECT_BYTES(0x89, 0x87, 0xBE, 0xBA, 0xFE, 0xCA);
342 :
343 : // Test a sampling of 8-bit displacements.
344 E : DisplacementImpl ca(0xCA, kSize8Bit, NULL);
345 :
346 : // Source.
347 E : asm_.mov(ebx, OperandImpl(eax, ca));
348 E : EXPECT_BYTES(0x8B, 0x58, 0xCA);
349 :
350 : // ESP requires a SIB byte and has a longer encoding.
351 E : asm_.mov(eax, OperandImpl(esp, ca));
352 E : EXPECT_BYTES(0x8B, 0x44, 0x24, 0xCA);
353 :
354 : // And destination modes.
355 E : asm_.mov(OperandImpl(eax, ca), ebx);
356 E : EXPECT_BYTES(0x89, 0x58, 0xCA);
357 :
358 : // ESP requires a SIB byte and has a longer encoding.
359 E : asm_.mov(OperandImpl(esp, ca), eax);
360 E : EXPECT_BYTES(0x89, 0x44, 0x24, 0xCA);
361 E : }
362 :
363 E : TEST_F(AssemblerTest, MovDisplacementIndirect) {
364 : // Displacement-only mode.
365 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
366 :
367 : // Source, note EAX has a shortcut encoding.
368 E : asm_.mov(eax, OperandImpl(cafebabe));
369 E : EXPECT_BYTES(0xA1, 0xBE, 0xBA, 0xFE, 0xCA);
370 E : asm_.mov(ecx, OperandImpl(cafebabe));
371 E : EXPECT_BYTES(0x8B, 0x0D, 0xBE, 0xBA, 0xFE, 0xCA);
372 :
373 : // Destination, again EAX is special.
374 E : asm_.mov(OperandImpl(cafebabe), eax);
375 E : EXPECT_BYTES(0xA3, 0xBE, 0xBA, 0xFE, 0xCA);
376 :
377 E : asm_.mov(OperandImpl(cafebabe), ecx);
378 E : EXPECT_BYTES(0x89, 0x0D, 0xBE, 0xBA, 0xFE, 0xCA);
379 E : }
380 :
381 E : TEST_F(AssemblerTest, MovRegisterBaseDisplacementScaleIndirect) {
382 : // There are 8 base * 7 index * 4 scales = 224 combinations.
383 : // We don't test all of them, but rather cycle through each of base,
384 : // index and scale individually.
385 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
386 :
387 : // Source mode, base register.
388 E : asm_.mov(edx, OperandImpl(ecx, eax, kTimes4, cafebabe));
389 E : EXPECT_BYTES(0x8B, 0x94, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
390 E : asm_.mov(eax, OperandImpl(ecx, eax, kTimes4, cafebabe));
391 E : EXPECT_BYTES(0x8B, 0x84, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
392 E : asm_.mov(eax, OperandImpl(edx, eax, kTimes4, cafebabe));
393 E : EXPECT_BYTES(0x8B, 0x84, 0x82, 0xBE, 0xBA, 0xFE, 0xCA);
394 E : asm_.mov(eax, OperandImpl(ebx, eax, kTimes4, cafebabe));
395 E : EXPECT_BYTES(0x8B, 0x84, 0x83, 0xBE, 0xBA, 0xFE, 0xCA);
396 E : asm_.mov(eax, OperandImpl(esp, eax, kTimes4, cafebabe));
397 E : EXPECT_BYTES(0x8B, 0x84, 0x84, 0xBE, 0xBA, 0xFE, 0xCA);
398 E : asm_.mov(eax, OperandImpl(ebp, eax, kTimes4, cafebabe));
399 E : EXPECT_BYTES(0x8B, 0x84, 0x85, 0xBE, 0xBA, 0xFE, 0xCA);
400 E : asm_.mov(eax, OperandImpl(esi, eax, kTimes4, cafebabe));
401 E : EXPECT_BYTES(0x8B, 0x84, 0x86, 0xBE, 0xBA, 0xFE, 0xCA);
402 E : asm_.mov(eax, OperandImpl(edi, eax, kTimes4, cafebabe));
403 E : EXPECT_BYTES(0x8B, 0x84, 0x87, 0xBE, 0xBA, 0xFE, 0xCA);
404 :
405 : // Source mode, index register.
406 E : asm_.mov(ebx, OperandImpl(ecx, eax, kTimes4, cafebabe));
407 E : EXPECT_BYTES(0x8B, 0x9C, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
408 E : asm_.mov(eax, OperandImpl(eax, ecx, kTimes4, cafebabe));
409 E : EXPECT_BYTES(0x8B, 0x84, 0x88, 0xBE, 0xBA, 0xFE, 0xCA);
410 E : asm_.mov(eax, OperandImpl(eax, edx, kTimes4, cafebabe));
411 E : EXPECT_BYTES(0x8B, 0x84, 0x90, 0xBE, 0xBA, 0xFE, 0xCA);
412 E : asm_.mov(eax, OperandImpl(eax, ebx, kTimes4, cafebabe));
413 E : EXPECT_BYTES(0x8B, 0x84, 0x98, 0xBE, 0xBA, 0xFE, 0xCA);
414 E : asm_.mov(eax, OperandImpl(eax, ebp, kTimes4, cafebabe));
415 E : EXPECT_BYTES(0x8B, 0x84, 0xA8, 0xBE, 0xBA, 0xFE, 0xCA);
416 E : asm_.mov(eax, OperandImpl(eax, esi, kTimes4, cafebabe));
417 E : EXPECT_BYTES(0x8B, 0x84, 0xB0, 0xBE, 0xBA, 0xFE, 0xCA);
418 E : asm_.mov(eax, OperandImpl(eax, edi, kTimes4, cafebabe));
419 E : EXPECT_BYTES(0x8B, 0x84, 0xB8, 0xBE, 0xBA, 0xFE, 0xCA);
420 :
421 : // Source mode, Scale.
422 E : asm_.mov(ebx, OperandImpl(ecx, eax, kTimes1, cafebabe));
423 E : EXPECT_BYTES(0x8B, 0x9C, 0x01, 0xBE, 0xBA, 0xFE, 0xCA);
424 E : asm_.mov(ebx, OperandImpl(ecx, eax, kTimes2, cafebabe));
425 E : EXPECT_BYTES(0x8B, 0x9C, 0x41, 0xBE, 0xBA, 0xFE, 0xCA);
426 E : asm_.mov(ebx, OperandImpl(ecx, eax, kTimes4, cafebabe));
427 E : EXPECT_BYTES(0x8B, 0x9C, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
428 E : asm_.mov(ebx, OperandImpl(ecx, eax, kTimes8, cafebabe));
429 E : EXPECT_BYTES(0x8B, 0x9C, 0xC1, 0xBE, 0xBA, 0xFE, 0xCA);
430 :
431 : // Destination mode, base register.
432 E : asm_.mov(OperandImpl(eax, eax, kTimes4, cafebabe), ecx);
433 E : EXPECT_BYTES(0x89, 0x8C, 0x80, 0xBE, 0xBA, 0xFE, 0xCA);
434 E : asm_.mov(OperandImpl(ecx, eax, kTimes4, cafebabe), eax);
435 E : EXPECT_BYTES(0x89, 0x84, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
436 E : asm_.mov(OperandImpl(edx, eax, kTimes4, cafebabe), eax);
437 E : EXPECT_BYTES(0x89, 0x84, 0x82, 0xBE, 0xBA, 0xFE, 0xCA);
438 E : asm_.mov(OperandImpl(ebx, eax, kTimes4, cafebabe), eax);
439 E : EXPECT_BYTES(0x89, 0x84, 0x83, 0xBE, 0xBA, 0xFE, 0xCA);
440 E : asm_.mov(OperandImpl(esp, eax, kTimes4, cafebabe), eax);
441 E : EXPECT_BYTES(0x89, 0x84, 0x84, 0xBE, 0xBA, 0xFE, 0xCA);
442 E : asm_.mov(OperandImpl(ebp, eax, kTimes4, cafebabe), eax);
443 E : EXPECT_BYTES(0x89, 0x84, 0x85, 0xBE, 0xBA, 0xFE, 0xCA);
444 E : asm_.mov(OperandImpl(esi, eax, kTimes4, cafebabe), eax);
445 E : EXPECT_BYTES(0x89, 0x84, 0x86, 0xBE, 0xBA, 0xFE, 0xCA);
446 E : asm_.mov(OperandImpl(edi, eax, kTimes4, cafebabe), eax);
447 E : EXPECT_BYTES(0x89, 0x84, 0x87, 0xBE, 0xBA, 0xFE, 0xCA);
448 :
449 : // Destination mode, index register.
450 E : asm_.mov(OperandImpl(ecx, eax, kTimes4, cafebabe), ebx);
451 E : EXPECT_BYTES(0x89, 0x9C, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
452 E : asm_.mov(OperandImpl(eax, ecx, kTimes4, cafebabe), eax);
453 E : EXPECT_BYTES(0x89, 0x84, 0x88, 0xBE, 0xBA, 0xFE, 0xCA);
454 E : asm_.mov(OperandImpl(eax, edx, kTimes4, cafebabe), eax);
455 E : EXPECT_BYTES(0x89, 0x84, 0x90, 0xBE, 0xBA, 0xFE, 0xCA);
456 E : asm_.mov(OperandImpl(eax, ebx, kTimes4, cafebabe), eax);
457 E : EXPECT_BYTES(0x89, 0x84, 0x98, 0xBE, 0xBA, 0xFE, 0xCA);
458 E : asm_.mov(OperandImpl(eax, ebp, kTimes4, cafebabe), eax);
459 E : EXPECT_BYTES(0x89, 0x84, 0xA8, 0xBE, 0xBA, 0xFE, 0xCA);
460 E : asm_.mov(OperandImpl(eax, esi, kTimes4, cafebabe), eax);
461 E : EXPECT_BYTES(0x89, 0x84, 0xB0, 0xBE, 0xBA, 0xFE, 0xCA);
462 E : asm_.mov(OperandImpl(eax, edi, kTimes4, cafebabe), eax);
463 E : EXPECT_BYTES(0x89, 0x84, 0xB8, 0xBE, 0xBA, 0xFE, 0xCA);
464 :
465 : // Destination mode, Scale.
466 E : asm_.mov(OperandImpl(ecx, eax, kTimes1, cafebabe), ebx);
467 E : EXPECT_BYTES(0x89, 0x9C, 0x01, 0xBE, 0xBA, 0xFE, 0xCA);
468 E : asm_.mov(OperandImpl(ecx, eax, kTimes2, cafebabe), ebx);
469 E : EXPECT_BYTES(0x89, 0x9C, 0x41, 0xBE, 0xBA, 0xFE, 0xCA);
470 E : asm_.mov(OperandImpl(ecx, eax, kTimes4, cafebabe), ebx);
471 E : EXPECT_BYTES(0x89, 0x9C, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
472 E : asm_.mov(OperandImpl(ecx, eax, kTimes8, cafebabe), ebx);
473 E : EXPECT_BYTES(0x89, 0x9C, 0xC1, 0xBE, 0xBA, 0xFE, 0xCA);
474 E : }
475 :
476 E : TEST_F(AssemblerTest, MovRegisterBaseIndexScaleIndirect) {
477 : // Tests the displacement-less [base + index * scale].
478 E : asm_.mov(edx, OperandImpl(esi, eax, kTimes8));
479 E : EXPECT_BYTES(0x8B, 0x14, 0xC6);
480 E : }
481 :
482 E : TEST_F(AssemblerTest, MovRegisterDisplacementScaleIndirect) {
483 : // Tests [index * scale + displ] modes, which are always encoded with a
484 : // 32-bit displacement, including [index * scale], which has a zero 32-bit
485 : // displacement that will be omitted from disassembly.
486 :
487 E : DisplacementImpl one(1, kSize8Bit, NULL);
488 :
489 : // Source mode.
490 E : asm_.mov(edx, OperandImpl(eax, kTimes4, one));
491 E : EXPECT_BYTES(0x8B, 0x14, 0x85, 0x01, 0x00, 0x00, 0x00);
492 E : asm_.mov(edx, OperandImpl(ecx, kTimes4, one));
493 E : EXPECT_BYTES(0x8B, 0x14, 0x8D, 0x01, 0x00, 0x00, 0x00);
494 E : asm_.mov(edx, OperandImpl(edx, kTimes4, one));
495 E : EXPECT_BYTES(0x8B, 0x14, 0x95, 0x01, 0x00, 0x00, 0x00);
496 E : asm_.mov(edx, OperandImpl(ebx, kTimes4, one));
497 E : EXPECT_BYTES(0x8B, 0x14, 0x9D, 0x01, 0x00, 0x00, 0x00);
498 E : asm_.mov(edx, OperandImpl(ebp, kTimes4, one));
499 E : EXPECT_BYTES(0x8B, 0x14, 0xAD, 0x01, 0x00, 0x00, 0x00);
500 E : asm_.mov(edx, OperandImpl(esi, kTimes4, one));
501 E : EXPECT_BYTES(0x8B, 0x14, 0xB5, 0x01, 0x00, 0x00, 0x00);
502 E : asm_.mov(edx, OperandImpl(edi, kTimes4, one));
503 E : EXPECT_BYTES(0x8B, 0x14, 0xBD, 0x01, 0x00, 0x00, 0x00);
504 :
505 : // Destination mode.
506 E : asm_.mov(OperandImpl(eax, kTimes4, one), edx);
507 E : EXPECT_BYTES(0x89, 0x14, 0x85, 0x01, 0x00, 0x00, 0x00);
508 E : asm_.mov(OperandImpl(ecx, kTimes4, one), edx);
509 E : EXPECT_BYTES(0x89, 0x14, 0x8D, 0x01, 0x00, 0x00, 0x00);
510 E : asm_.mov(OperandImpl(edx, kTimes4, one), edx);
511 E : EXPECT_BYTES(0x89, 0x14, 0x95, 0x01, 0x00, 0x00, 0x00);
512 E : asm_.mov(OperandImpl(ebx, kTimes4, one), edx);
513 E : EXPECT_BYTES(0x89, 0x14, 0x9D, 0x01, 0x00, 0x00, 0x00);
514 E : asm_.mov(OperandImpl(ebp, kTimes4, one), edx);
515 E : EXPECT_BYTES(0x89, 0x14, 0xAD, 0x01, 0x00, 0x00, 0x00);
516 E : asm_.mov(OperandImpl(esi, kTimes4, one), edx);
517 E : EXPECT_BYTES(0x89, 0x14, 0xB5, 0x01, 0x00, 0x00, 0x00);
518 E : asm_.mov(OperandImpl(edi, kTimes4, one), edx);
519 E : EXPECT_BYTES(0x89, 0x14, 0xBD, 0x01, 0x00, 0x00, 0x00);
520 E : }
521 :
522 E : TEST_F(AssemblerTest, MovImmToRegisterDisplacementScaleIndirect) {
523 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
524 E : ImmediateImpl deadbeef(0xDEADBEEF, kSize32Bit, NULL);
525 :
526 : // We expect the operand encoding has been adequately tested elsewhere,
527 : // so we only test one variant here.
528 E : asm_.mov(OperandImpl(ecx, eax, kTimes4, cafebabe), deadbeef);
529 : EXPECT_BYTES(0xC7, 0x84, 0x81,
530 : 0xBE, 0xBA, 0xFE, 0xCA,
531 E : 0xEF, 0xBE, 0xAD, 0xDE);
532 E : }
533 :
534 E : TEST_F(AssemblerTest, LeaRegisterIndirect) {
535 : // Indirect register only source modes.
536 E : asm_.lea(ebx, OperandImpl(eax));
537 E : EXPECT_BYTES(0x8D, 0x18);
538 E : asm_.lea(eax, OperandImpl(ecx));
539 E : EXPECT_BYTES(0x8D, 0x01);
540 E : asm_.lea(edx, OperandImpl(ebx));
541 E : EXPECT_BYTES(0x8D, 0x13);
542 E : asm_.lea(ecx, OperandImpl(edx));
543 E : EXPECT_BYTES(0x8D, 0x0A);
544 :
545 : // Note that EBP is a special case that always requires a displacement.
546 E : asm_.lea(ebx, OperandImpl(ebp));
547 E : EXPECT_BYTES(0x8D, 0x5D, 0x00);
548 :
549 : // Note that ESP is a special case that always requires a SIB byte.
550 E : asm_.lea(ecx, OperandImpl(esp));
551 E : EXPECT_BYTES(0x8D, 0x0C, 0x24);
552 :
553 E : asm_.lea(ebx, OperandImpl(esi));
554 E : EXPECT_BYTES(0x8D, 0x1E);
555 E : asm_.lea(eax, OperandImpl(edi));
556 E : EXPECT_BYTES(0x8D, 0x07);
557 E : }
558 :
559 E : TEST_F(AssemblerTest, LeaRegisterDisplacementIndirect) {
560 : // Register & displacement source modes.
561 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
562 :
563 E : asm_.lea(ebx, OperandImpl(eax, cafebabe));
564 E : EXPECT_BYTES(0x8D, 0x98, 0xBE, 0xBA, 0xFE, 0xCA);
565 E : asm_.lea(eax, OperandImpl(ecx, cafebabe));
566 E : EXPECT_BYTES(0x8D, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
567 E : asm_.lea(eax, OperandImpl(ebx, cafebabe));
568 E : EXPECT_BYTES(0x8D, 0x83, 0xBE, 0xBA, 0xFE, 0xCA);
569 E : asm_.lea(eax, OperandImpl(edx, cafebabe));
570 E : EXPECT_BYTES(0x8D, 0x82, 0xBE, 0xBA, 0xFE, 0xCA);
571 E : asm_.lea(eax, OperandImpl(ebp, cafebabe));
572 E : EXPECT_BYTES(0x8D, 0x85, 0xBE, 0xBA, 0xFE, 0xCA);
573 :
574 : // ESP requires a SIB byte and has a longer encoding.
575 E : asm_.lea(eax, OperandImpl(esp, cafebabe));
576 E : EXPECT_BYTES(0x8D, 0x84, 0x24, 0xBE, 0xBA, 0xFE, 0xCA);
577 :
578 E : asm_.lea(eax, OperandImpl(esi, cafebabe));
579 E : EXPECT_BYTES(0x8D, 0x86, 0xBE, 0xBA, 0xFE, 0xCA);
580 E : asm_.lea(eax, OperandImpl(edi, cafebabe));
581 E : EXPECT_BYTES(0x8D, 0x87, 0xBE, 0xBA, 0xFE, 0xCA);
582 :
583 : // Test a sampling of 8-bit displacements.
584 E : DisplacementImpl ca(0xCA, kSize8Bit, NULL);
585 :
586 : // Source.
587 E : asm_.lea(ebx, OperandImpl(eax, ca));
588 E : EXPECT_BYTES(0x8D, 0x58, 0xCA);
589 :
590 : // ESP requires a SIB byte and has a longer encoding.
591 E : asm_.lea(eax, OperandImpl(esp, ca));
592 E : EXPECT_BYTES(0x8D, 0x44, 0x24, 0xCA);
593 E : }
594 :
595 E : TEST_F(AssemblerTest, LeaDisplacementIndirect) {
596 : // Displacement-only mode.
597 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
598 :
599 E : asm_.lea(eax, OperandImpl(cafebabe));
600 E : EXPECT_BYTES(0x8D, 0x05, 0xBE, 0xBA, 0xFE, 0xCA);
601 E : asm_.lea(ecx, OperandImpl(cafebabe));
602 E : EXPECT_BYTES(0x8D, 0x0D, 0xBE, 0xBA, 0xFE, 0xCA);
603 E : }
604 :
605 E : TEST_F(AssemblerTest, LeaRegisterDisplacementScaleIndirect) {
606 : // There are 8 base * 7 index * 4 scales = 224 combinations.
607 : // We don't test all of them, but rather cycle through each of base,
608 : // index and scale individually.
609 E : DisplacementImpl cafebabe(0xCAFEBABE, kSize32Bit, NULL);
610 :
611 : // Source mode, base register.
612 E : asm_.lea(edx, OperandImpl(ecx, eax, kTimes4, cafebabe));
613 E : EXPECT_BYTES(0x8D, 0x94, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
614 E : asm_.lea(eax, OperandImpl(ecx, eax, kTimes4, cafebabe));
615 E : EXPECT_BYTES(0x8D, 0x84, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
616 E : asm_.lea(eax, OperandImpl(edx, eax, kTimes4, cafebabe));
617 E : EXPECT_BYTES(0x8D, 0x84, 0x82, 0xBE, 0xBA, 0xFE, 0xCA);
618 E : asm_.lea(eax, OperandImpl(ebx, eax, kTimes4, cafebabe));
619 E : EXPECT_BYTES(0x8D, 0x84, 0x83, 0xBE, 0xBA, 0xFE, 0xCA);
620 E : asm_.lea(eax, OperandImpl(esp, eax, kTimes4, cafebabe));
621 E : EXPECT_BYTES(0x8D, 0x84, 0x84, 0xBE, 0xBA, 0xFE, 0xCA);
622 E : asm_.lea(eax, OperandImpl(ebp, eax, kTimes4, cafebabe));
623 E : EXPECT_BYTES(0x8D, 0x84, 0x85, 0xBE, 0xBA, 0xFE, 0xCA);
624 E : asm_.lea(eax, OperandImpl(esi, eax, kTimes4, cafebabe));
625 E : EXPECT_BYTES(0x8D, 0x84, 0x86, 0xBE, 0xBA, 0xFE, 0xCA);
626 E : asm_.lea(eax, OperandImpl(edi, eax, kTimes4, cafebabe));
627 E : EXPECT_BYTES(0x8D, 0x84, 0x87, 0xBE, 0xBA, 0xFE, 0xCA);
628 :
629 : // Source mode, index register.
630 E : asm_.lea(ebx, OperandImpl(ecx, eax, kTimes4, cafebabe));
631 E : EXPECT_BYTES(0x8D, 0x9C, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
632 E : asm_.lea(eax, OperandImpl(eax, ecx, kTimes4, cafebabe));
633 E : EXPECT_BYTES(0x8D, 0x84, 0x88, 0xBE, 0xBA, 0xFE, 0xCA);
634 E : asm_.lea(eax, OperandImpl(eax, edx, kTimes4, cafebabe));
635 E : EXPECT_BYTES(0x8D, 0x84, 0x90, 0xBE, 0xBA, 0xFE, 0xCA);
636 E : asm_.lea(eax, OperandImpl(eax, ebx, kTimes4, cafebabe));
637 E : EXPECT_BYTES(0x8D, 0x84, 0x98, 0xBE, 0xBA, 0xFE, 0xCA);
638 E : asm_.lea(eax, OperandImpl(eax, ebp, kTimes4, cafebabe));
639 E : EXPECT_BYTES(0x8D, 0x84, 0xA8, 0xBE, 0xBA, 0xFE, 0xCA);
640 E : asm_.lea(eax, OperandImpl(eax, esi, kTimes4, cafebabe));
641 E : EXPECT_BYTES(0x8D, 0x84, 0xB0, 0xBE, 0xBA, 0xFE, 0xCA);
642 E : asm_.lea(eax, OperandImpl(eax, edi, kTimes4, cafebabe));
643 E : EXPECT_BYTES(0x8D, 0x84, 0xB8, 0xBE, 0xBA, 0xFE, 0xCA);
644 :
645 : // Source mode, Scale.
646 E : asm_.lea(ebx, OperandImpl(ecx, eax, kTimes1, cafebabe));
647 E : EXPECT_BYTES(0x8D, 0x9C, 0x01, 0xBE, 0xBA, 0xFE, 0xCA);
648 E : asm_.lea(ebx, OperandImpl(ecx, eax, kTimes2, cafebabe));
649 E : EXPECT_BYTES(0x8D, 0x9C, 0x41, 0xBE, 0xBA, 0xFE, 0xCA);
650 E : asm_.lea(ebx, OperandImpl(ecx, eax, kTimes4, cafebabe));
651 E : EXPECT_BYTES(0x8D, 0x9C, 0x81, 0xBE, 0xBA, 0xFE, 0xCA);
652 E : asm_.lea(ebx, OperandImpl(ecx, eax, kTimes8, cafebabe));
653 E : EXPECT_BYTES(0x8D, 0x9C, 0xC1, 0xBE, 0xBA, 0xFE, 0xCA);
654 E : }
655 :
656 E : TEST_F(AssemblerTest, Push) {
657 : // Register push.
658 E : asm_.push(eax);
659 E : asm_.push(ecx);
660 E : asm_.push(edx);
661 E : asm_.push(ebx);
662 E : asm_.push(esp);
663 E : asm_.push(ebp);
664 E : asm_.push(esi);
665 E : asm_.push(edi);
666 E : EXPECT_BYTES(0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57);
667 :
668 : // Immediate push.
669 E : asm_.push(ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
670 E : EXPECT_BYTES(0x68, 0xBE, 0xBA, 0xFE, 0xCA);
671 :
672 : // General push, try one variant as the rest are OperandImpl encodings.
673 E : asm_.push(OperandImpl(DisplacementImpl(0xCAFEBABE, kSize32Bit, NULL)));
674 E : EXPECT_BYTES(0xFF, 0x35, 0xBE, 0xBA, 0xFE, 0xCA);
675 E : }
676 :
677 E : TEST_F(AssemblerTest, Pop) {
678 : // Register pop.
679 E : asm_.pop(eax);
680 E : asm_.pop(ecx);
681 E : asm_.pop(edx);
682 E : asm_.pop(ebx);
683 E : asm_.pop(esp);
684 E : asm_.pop(ebp);
685 E : asm_.pop(esi);
686 E : asm_.pop(edi);
687 E : EXPECT_BYTES(0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F);
688 :
689 : // General pop, try one variant as the rest are OperandImpl encodings.
690 E : asm_.pop(OperandImpl(DisplacementImpl(0xCAFEBABE, kSize32Bit, NULL)));
691 E : EXPECT_BYTES(0x8F, 0x05, 0xBE, 0xBA, 0xFE, 0xCA);
692 E : }
693 :
694 E : TEST_F(AssemblerTest, Ja) {
695 E : ConditionCode cc = kAbove;
696 E : asm_.set_location(0xCAFEBABE);
697 :
698 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
699 E : EXPECT_BYTES(0x77, 0xFE);
700 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
701 E : EXPECT_BYTES(0x0F, 0x87, 0xF8, 0xFF, 0xFF, 0xFF);
702 E : }
703 :
704 E : TEST_F(AssemblerTest, Jae) {
705 E : ConditionCode cc = kAboveEqual;
706 E : asm_.set_location(0xCAFEBABE);
707 :
708 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
709 E : EXPECT_BYTES(0x73, 0xFE);
710 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
711 E : EXPECT_BYTES(0x0F, 0x83, 0xF8, 0xFF, 0xFF, 0xFF);
712 E : }
713 :
714 E : TEST_F(AssemblerTest, Jb) {
715 E : ConditionCode cc = kBelow;
716 E : asm_.set_location(0xCAFEBABE);
717 :
718 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
719 E : EXPECT_BYTES(0x72, 0xFE);
720 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
721 E : EXPECT_BYTES(0x0F, 0x82, 0xF8, 0xFF, 0xFF, 0xFF);
722 E : }
723 :
724 E : TEST_F(AssemblerTest, Jbe) {
725 E : ConditionCode cc = kBelowEqual;
726 E : asm_.set_location(0xCAFEBABE);
727 :
728 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
729 E : EXPECT_BYTES(0x76, 0xFE);
730 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
731 E : EXPECT_BYTES(0x0F, 0x86, 0xF8, 0xFF, 0xFF, 0xFF);
732 E : }
733 :
734 E : TEST_F(AssemblerTest, Jc) {
735 E : ConditionCode cc = kCarry;
736 E : asm_.set_location(0xCAFEBABE);
737 :
738 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
739 E : EXPECT_BYTES(0x72, 0xFE);
740 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
741 E : EXPECT_BYTES(0x0F, 0x82, 0xF8, 0xFF, 0xFF, 0xFF);
742 E : }
743 :
744 E : TEST_F(AssemblerTest, Je) {
745 E : ConditionCode cc = kEqual;
746 E : asm_.set_location(0xCAFEBABE);
747 :
748 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
749 E : EXPECT_BYTES(0x74, 0xFE);
750 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
751 E : EXPECT_BYTES(0x0F, 0x84, 0xF8, 0xFF, 0xFF, 0xFF);
752 E : }
753 :
754 E : TEST_F(AssemblerTest, Jecxz) {
755 E : asm_.set_location(0xCAFEBABE);
756 :
757 E : asm_.jecxz(ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
758 E : EXPECT_BYTES(0xE3, 0xFE);
759 E : }
760 :
761 E : TEST_F(AssemblerTest, Jg) {
762 E : ConditionCode cc = kGreater;
763 E : asm_.set_location(0xCAFEBABE);
764 :
765 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
766 E : EXPECT_BYTES(0x7F, 0xFE);
767 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
768 E : EXPECT_BYTES(0x0F, 0x8F, 0xF8, 0xFF, 0xFF, 0xFF);
769 E : }
770 :
771 E : TEST_F(AssemblerTest, Jge) {
772 E : ConditionCode cc = kGreaterEqual;
773 E : asm_.set_location(0xCAFEBABE);
774 :
775 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
776 E : EXPECT_BYTES(0x7D, 0xFE);
777 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
778 E : EXPECT_BYTES(0x0F, 0x8D, 0xF8, 0xFF, 0xFF, 0xFF);
779 E : }
780 :
781 E : TEST_F(AssemblerTest, Jl) {
782 E : ConditionCode cc = kLess;
783 E : asm_.set_location(0xCAFEBABE);
784 :
785 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
786 E : EXPECT_BYTES(0x7C, 0xFE);
787 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
788 E : EXPECT_BYTES(0x0F, 0x8C, 0xF8, 0xFF, 0xFF, 0xFF);
789 E : }
790 :
791 E : TEST_F(AssemblerTest, Jle) {
792 E : ConditionCode cc = kLessEqual;
793 E : asm_.set_location(0xCAFEBABE);
794 :
795 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
796 E : EXPECT_BYTES(0x7E, 0xFE);
797 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
798 E : EXPECT_BYTES(0x0F, 0x8E, 0xF8, 0xFF, 0xFF, 0xFF);
799 E : }
800 :
801 E : TEST_F(AssemblerTest, Jo) {
802 E : ConditionCode cc = kOverflow;
803 E : asm_.set_location(0xCAFEBABE);
804 :
805 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
806 E : EXPECT_BYTES(0x70, 0xFE);
807 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
808 E : EXPECT_BYTES(0x0F, 0x80, 0xF8, 0xFF, 0xFF, 0xFF);
809 E : }
810 :
811 E : TEST_F(AssemblerTest, Jpe) {
812 E : ConditionCode cc = kParityEven;
813 E : asm_.set_location(0xCAFEBABE);
814 :
815 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
816 E : EXPECT_BYTES(0x7A, 0xFE);
817 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
818 E : EXPECT_BYTES(0x0F, 0x8A, 0xF8, 0xFF, 0xFF, 0xFF);
819 E : }
820 :
821 E : TEST_F(AssemblerTest, Jpo) {
822 E : ConditionCode cc = kParityOdd;
823 E : asm_.set_location(0xCAFEBABE);
824 :
825 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
826 E : EXPECT_BYTES(0x7B, 0xFE);
827 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
828 E : EXPECT_BYTES(0x0F, 0x8B, 0xF8, 0xFF, 0xFF, 0xFF);
829 E : }
830 :
831 E : TEST_F(AssemblerTest, Js) {
832 E : ConditionCode cc = kSign;
833 E : asm_.set_location(0xCAFEBABE);
834 : COMPILE_ASSERT(kSign == kNegative, kSignAndPositiveAreAliases);
835 :
836 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
837 E : EXPECT_BYTES(0x78, 0xFE);
838 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
839 E : EXPECT_BYTES(0x0F, 0x88, 0xF8, 0xFF, 0xFF, 0xFF);
840 E : }
841 :
842 E : TEST_F(AssemblerTest, Jz) {
843 E : ConditionCode cc = kZero;
844 E : asm_.set_location(0xCAFEBABE);
845 :
846 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
847 E : EXPECT_BYTES(0x74, 0xFE);
848 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
849 E : EXPECT_BYTES(0x0F, 0x84, 0xF8, 0xFF, 0xFF, 0xFF);
850 E : }
851 :
852 E : TEST_F(AssemblerTest, Jnc) {
853 E : ConditionCode cc = kNotCarry;
854 E : asm_.set_location(0xCAFEBABE);
855 :
856 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
857 E : EXPECT_BYTES(0x73, 0xFE);
858 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
859 E : EXPECT_BYTES(0x0F, 0x83, 0xF8, 0xFF, 0xFF, 0xFF);
860 E : }
861 :
862 E : TEST_F(AssemblerTest, Jne) {
863 E : ConditionCode cc = kNotEqual;
864 E : asm_.set_location(0xCAFEBABE);
865 :
866 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
867 E : EXPECT_BYTES(0x75, 0xFE);
868 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
869 E : EXPECT_BYTES(0x0F, 0x85, 0xF8, 0xFF, 0xFF, 0xFF);
870 E : }
871 :
872 E : TEST_F(AssemblerTest, Jno) {
873 E : ConditionCode cc = kNoOverflow;
874 E : asm_.set_location(0xCAFEBABE);
875 :
876 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
877 E : EXPECT_BYTES(0x71, 0xFE);
878 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
879 E : EXPECT_BYTES(0x0F, 0x81, 0xF8, 0xFF, 0xFF, 0xFF);
880 E : }
881 :
882 E : TEST_F(AssemblerTest, Jns) {
883 : COMPILE_ASSERT(kNotSign == kPositive, kSignAndPositiveAreAliases);
884 E : ConditionCode cc = kNotSign;
885 E : asm_.set_location(0xCAFEBABE);
886 :
887 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
888 E : EXPECT_BYTES(0x79, 0xFE);
889 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
890 E : EXPECT_BYTES(0x0F, 0x89, 0xF8, 0xFF, 0xFF, 0xFF);
891 E : }
892 :
893 E : TEST_F(AssemblerTest, Jnz) {
894 E : ConditionCode cc = kNotZero;
895 E : asm_.set_location(0xCAFEBABE);
896 :
897 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
898 E : EXPECT_BYTES(0x75, 0xFE);
899 E : asm_.j(cc, ImmediateImpl(0xCAFEBABE, kSize32Bit, NULL));
900 E : EXPECT_BYTES(0x0F, 0x85, 0xF8, 0xFF, 0xFF, 0xFF);
901 E : }
902 :
903 E : TEST_F(AssemblerTest, Loop) {
904 E : asm_.set_location(0xCAFEBABE);
905 :
906 E : asm_.loop(ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
907 E : EXPECT_BYTES(0xE2, 0xFE);
908 E : }
909 :
910 E : TEST_F(AssemblerTest, Loope) {
911 E : asm_.set_location(0xCAFEBABE);
912 :
913 E : asm_.loope(ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
914 E : EXPECT_BYTES(0xE1, 0xFE);
915 E : }
916 :
917 E : TEST_F(AssemblerTest, Loopne) {
918 E : asm_.set_location(0xCAFEBABE);
919 :
920 E : asm_.loopne(ImmediateImpl(0xCAFEBABE, kSize8Bit, NULL));
921 E : EXPECT_BYTES(0xE0, 0xFE);
922 E : }
923 :
924 E : TEST_F(AssemblerTest, References) {
925 : // We arbitrarily use the MOV instruction to test reference propagation.
926 : static const int ref1 = 1;
927 E : asm_.mov(eax, ImmediateImpl(0, kSize8Bit, &ref1));
928 :
929 : static const int ref2 = 2;
930 : asm_.mov(eax, OperandImpl(eax, ebx, kTimes4,
931 E : DisplacementImpl(0, kSize32Bit, &ref2)));
932 :
933 : static const int ref3 = 3;
934 : static const int ref4 = 4;
935 : asm_.mov(OperandImpl(eax, ebx, kTimes4,
936 : DisplacementImpl(0, kSize32Bit, &ref3)),
937 E : ImmediateImpl(0, kSize32Bit, &ref4));
938 :
939 E : EXPECT_EQ(4, serializer_.references.size());
940 :
941 E : EXPECT_EQ(1, serializer_.references[0].location);
942 E : EXPECT_EQ(&ref1, serializer_.references[0].ref);
943 :
944 E : EXPECT_EQ(8, serializer_.references[1].location);
945 E : EXPECT_EQ(&ref2, serializer_.references[1].ref);
946 :
947 E : EXPECT_EQ(15, serializer_.references[2].location);
948 E : EXPECT_EQ(&ref3, serializer_.references[2].ref);
949 :
950 E : EXPECT_EQ(19, serializer_.references[3].location);
951 E : EXPECT_EQ(&ref4, serializer_.references[3].ref);
952 E : }
953 :
954 : } // namespace core
|