blob: 1b882e000257f9d6e9b4e7a2d7d0907fe74344d1 [file] [log] [blame]
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_ASSEMBLER_X86_H_
4#define ART_SRC_ASSEMBLER_X86_H_
5
Ian Rogers0d666d82011-08-14 16:03:46 -07006#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007#include "assembler.h"
8#include "constants.h"
9#include "globals.h"
10#include "managed_register.h"
11#include "macros.h"
12#include "offsets.h"
13#include "utils.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070014
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070015namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070016
17class Immediate {
18 public:
19 explicit Immediate(int32_t value) : value_(value) {}
20
21 int32_t value() const { return value_; }
22
23 bool is_int8() const { return IsInt(8, value_); }
24 bool is_uint8() const { return IsUint(8, value_); }
25 bool is_uint16() const { return IsUint(16, value_); }
26
27 private:
28 const int32_t value_;
29
30 DISALLOW_COPY_AND_ASSIGN(Immediate);
31};
32
33
34class Operand {
35 public:
36 uint8_t mod() const {
37 return (encoding_at(0) >> 6) & 3;
38 }
39
40 Register rm() const {
41 return static_cast<Register>(encoding_at(0) & 7);
42 }
43
44 ScaleFactor scale() const {
45 return static_cast<ScaleFactor>((encoding_at(1) >> 6) & 3);
46 }
47
48 Register index() const {
49 return static_cast<Register>((encoding_at(1) >> 3) & 7);
50 }
51
52 Register base() const {
53 return static_cast<Register>(encoding_at(1) & 7);
54 }
55
56 int8_t disp8() const {
57 CHECK_GE(length_, 2);
58 return static_cast<int8_t>(encoding_[length_ - 1]);
59 }
60
61 int32_t disp32() const {
62 CHECK_GE(length_, 5);
63 int32_t value;
64 memcpy(&value, &encoding_[length_ - 4], sizeof(value));
65 return value;
66 }
67
68 bool IsRegister(Register reg) const {
69 return ((encoding_[0] & 0xF8) == 0xC0) // Addressing mode is register only.
70 && ((encoding_[0] & 0x07) == reg); // Register codes match.
71 }
72
73 protected:
74 // Operand can be sub classed (e.g: Address).
75 Operand() : length_(0) { }
76
77 void SetModRM(int mod, Register rm) {
78 CHECK_EQ(mod & ~3, 0);
79 encoding_[0] = (mod << 6) | rm;
80 length_ = 1;
81 }
82
83 void SetSIB(ScaleFactor scale, Register index, Register base) {
84 CHECK_EQ(length_, 1);
85 CHECK_EQ(scale & ~3, 0);
86 encoding_[1] = (scale << 6) | (index << 3) | base;
87 length_ = 2;
88 }
89
90 void SetDisp8(int8_t disp) {
91 CHECK(length_ == 1 || length_ == 2);
92 encoding_[length_++] = static_cast<uint8_t>(disp);
93 }
94
95 void SetDisp32(int32_t disp) {
96 CHECK(length_ == 1 || length_ == 2);
97 int disp_size = sizeof(disp);
98 memmove(&encoding_[length_], &disp, disp_size);
99 length_ += disp_size;
100 }
101
102 private:
103 byte length_;
104 byte encoding_[6];
105 byte padding_;
106
107 explicit Operand(Register reg) { SetModRM(3, reg); }
108
109 // Get the operand encoding byte at the given index.
110 uint8_t encoding_at(int index) const {
111 CHECK_GE(index, 0);
112 CHECK_LT(index, length_);
113 return encoding_[index];
114 }
115
116 friend class Assembler;
117
118 DISALLOW_COPY_AND_ASSIGN(Operand);
119};
120
121
122class Address : public Operand {
123 public:
124 Address(Register base, int32_t disp) {
Ian Rogersb033c752011-07-20 12:22:35 -0700125 Init(base, disp);
126 }
127
Ian Rogersa04d3972011-08-17 11:33:44 -0700128 Address(Register base, Offset disp) {
129 Init(base, disp.Int32Value());
130 }
131
Ian Rogersb033c752011-07-20 12:22:35 -0700132 Address(Register base, FrameOffset disp) {
133 CHECK_EQ(base, ESP);
134 Init(ESP, disp.Int32Value());
135 }
136
137 Address(Register base, MemberOffset disp) {
138 Init(base, disp.Int32Value());
139 }
140
141 void Init(Register base, int32_t disp) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700142 if (disp == 0 && base != EBP) {
143 SetModRM(0, base);
144 if (base == ESP) SetSIB(TIMES_1, ESP, base);
145 } else if (disp >= -128 && disp <= 127) {
146 SetModRM(1, base);
147 if (base == ESP) SetSIB(TIMES_1, ESP, base);
148 SetDisp8(disp);
149 } else {
150 SetModRM(2, base);
151 if (base == ESP) SetSIB(TIMES_1, ESP, base);
152 SetDisp32(disp);
153 }
154 }
155
Ian Rogersb033c752011-07-20 12:22:35 -0700156
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700157 Address(Register index, ScaleFactor scale, int32_t disp) {
158 CHECK_NE(index, ESP); // Illegal addressing mode.
159 SetModRM(0, ESP);
160 SetSIB(scale, index, EBP);
161 SetDisp32(disp);
162 }
163
164 Address(Register base, Register index, ScaleFactor scale, int32_t disp) {
165 CHECK_NE(index, ESP); // Illegal addressing mode.
166 if (disp == 0 && base != EBP) {
167 SetModRM(0, ESP);
168 SetSIB(scale, index, base);
169 } else if (disp >= -128 && disp <= 127) {
170 SetModRM(1, ESP);
171 SetSIB(scale, index, base);
172 SetDisp8(disp);
173 } else {
174 SetModRM(2, ESP);
175 SetSIB(scale, index, base);
176 SetDisp32(disp);
177 }
178 }
179
Carl Shapiro69759ea2011-07-21 18:13:35 -0700180 static Address Absolute(uword addr) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700181 Address result;
182 result.SetModRM(0, EBP);
183 result.SetDisp32(addr);
184 return result;
185 }
186
Ian Rogersb033c752011-07-20 12:22:35 -0700187 static Address Absolute(ThreadOffset addr) {
188 return Absolute(addr.Int32Value());
189 }
190
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700191 private:
192 Address() {}
193
194 DISALLOW_COPY_AND_ASSIGN(Address);
195};
196
197
198class Assembler {
199 public:
200 Assembler() : buffer_() {}
201 ~Assembler() {}
202
buzbeec143c552011-08-20 17:38:58 -0700203 InstructionSet GetInstructionSet() const {
204 return kX86;
205 }
206
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700207 /*
208 * Emit Machine Instructions.
209 */
210 void call(Register reg);
211 void call(const Address& address);
212 void call(Label* label);
213
214 void pushl(Register reg);
215 void pushl(const Address& address);
216 void pushl(const Immediate& imm);
217
218 void popl(Register reg);
219 void popl(const Address& address);
220
221 void movl(Register dst, const Immediate& src);
222 void movl(Register dst, Register src);
223
224 void movl(Register dst, const Address& src);
225 void movl(const Address& dst, Register src);
226 void movl(const Address& dst, const Immediate& imm);
227
228 void movzxb(Register dst, ByteRegister src);
229 void movzxb(Register dst, const Address& src);
230 void movsxb(Register dst, ByteRegister src);
231 void movsxb(Register dst, const Address& src);
232 void movb(Register dst, const Address& src);
233 void movb(const Address& dst, ByteRegister src);
234 void movb(const Address& dst, const Immediate& imm);
235
236 void movzxw(Register dst, Register src);
237 void movzxw(Register dst, const Address& src);
238 void movsxw(Register dst, Register src);
239 void movsxw(Register dst, const Address& src);
240 void movw(Register dst, const Address& src);
241 void movw(const Address& dst, Register src);
242
243 void leal(Register dst, const Address& src);
244
Ian Rogersb033c752011-07-20 12:22:35 -0700245 void cmovl(Condition condition, Register dst, Register src);
246
247 void setb(Condition condition, Register dst);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700248
249 void movss(XmmRegister dst, const Address& src);
250 void movss(const Address& dst, XmmRegister src);
251 void movss(XmmRegister dst, XmmRegister src);
252
253 void movd(XmmRegister dst, Register src);
254 void movd(Register dst, XmmRegister src);
255
256 void addss(XmmRegister dst, XmmRegister src);
257 void addss(XmmRegister dst, const Address& src);
258 void subss(XmmRegister dst, XmmRegister src);
259 void subss(XmmRegister dst, const Address& src);
260 void mulss(XmmRegister dst, XmmRegister src);
261 void mulss(XmmRegister dst, const Address& src);
262 void divss(XmmRegister dst, XmmRegister src);
263 void divss(XmmRegister dst, const Address& src);
264
265 void movsd(XmmRegister dst, const Address& src);
266 void movsd(const Address& dst, XmmRegister src);
267 void movsd(XmmRegister dst, XmmRegister src);
268
269 void addsd(XmmRegister dst, XmmRegister src);
270 void addsd(XmmRegister dst, const Address& src);
271 void subsd(XmmRegister dst, XmmRegister src);
272 void subsd(XmmRegister dst, const Address& src);
273 void mulsd(XmmRegister dst, XmmRegister src);
274 void mulsd(XmmRegister dst, const Address& src);
275 void divsd(XmmRegister dst, XmmRegister src);
276 void divsd(XmmRegister dst, const Address& src);
277
278 void cvtsi2ss(XmmRegister dst, Register src);
279 void cvtsi2sd(XmmRegister dst, Register src);
280
281 void cvtss2si(Register dst, XmmRegister src);
282 void cvtss2sd(XmmRegister dst, XmmRegister src);
283
284 void cvtsd2si(Register dst, XmmRegister src);
285 void cvtsd2ss(XmmRegister dst, XmmRegister src);
286
287 void cvttss2si(Register dst, XmmRegister src);
288 void cvttsd2si(Register dst, XmmRegister src);
289
290 void cvtdq2pd(XmmRegister dst, XmmRegister src);
291
292 void comiss(XmmRegister a, XmmRegister b);
293 void comisd(XmmRegister a, XmmRegister b);
294
295 void sqrtsd(XmmRegister dst, XmmRegister src);
296 void sqrtss(XmmRegister dst, XmmRegister src);
297
298 void xorpd(XmmRegister dst, const Address& src);
299 void xorpd(XmmRegister dst, XmmRegister src);
300 void xorps(XmmRegister dst, const Address& src);
301 void xorps(XmmRegister dst, XmmRegister src);
302
303 void andpd(XmmRegister dst, const Address& src);
304
305 void flds(const Address& src);
306 void fstps(const Address& dst);
307
308 void fldl(const Address& src);
309 void fstpl(const Address& dst);
310
311 void fnstcw(const Address& dst);
312 void fldcw(const Address& src);
313
314 void fistpl(const Address& dst);
315 void fistps(const Address& dst);
316 void fildl(const Address& src);
317
318 void fincstp();
319 void ffree(const Immediate& index);
320
321 void fsin();
322 void fcos();
323 void fptan();
324
325 void xchgl(Register dst, Register src);
326
327 void cmpl(Register reg, const Immediate& imm);
328 void cmpl(Register reg0, Register reg1);
329 void cmpl(Register reg, const Address& address);
330
331 void cmpl(const Address& address, Register reg);
332 void cmpl(const Address& address, const Immediate& imm);
333
334 void testl(Register reg1, Register reg2);
335 void testl(Register reg, const Immediate& imm);
336
337 void andl(Register dst, const Immediate& imm);
338 void andl(Register dst, Register src);
339
340 void orl(Register dst, const Immediate& imm);
341 void orl(Register dst, Register src);
342
343 void xorl(Register dst, Register src);
344
345 void addl(Register dst, Register src);
346 void addl(Register reg, const Immediate& imm);
347 void addl(Register reg, const Address& address);
348
349 void addl(const Address& address, Register reg);
350 void addl(const Address& address, const Immediate& imm);
351
352 void adcl(Register dst, Register src);
353 void adcl(Register reg, const Immediate& imm);
354 void adcl(Register dst, const Address& address);
355
356 void subl(Register dst, Register src);
357 void subl(Register reg, const Immediate& imm);
358 void subl(Register reg, const Address& address);
359
360 void cdq();
361
362 void idivl(Register reg);
363
364 void imull(Register dst, Register src);
365 void imull(Register reg, const Immediate& imm);
366 void imull(Register reg, const Address& address);
367
368 void imull(Register reg);
369 void imull(const Address& address);
370
371 void mull(Register reg);
372 void mull(const Address& address);
373
374 void sbbl(Register dst, Register src);
375 void sbbl(Register reg, const Immediate& imm);
376 void sbbl(Register reg, const Address& address);
377
378 void incl(Register reg);
379 void incl(const Address& address);
380
381 void decl(Register reg);
382 void decl(const Address& address);
383
384 void shll(Register reg, const Immediate& imm);
385 void shll(Register operand, Register shifter);
386 void shrl(Register reg, const Immediate& imm);
387 void shrl(Register operand, Register shifter);
388 void sarl(Register reg, const Immediate& imm);
389 void sarl(Register operand, Register shifter);
390 void shld(Register dst, Register src);
391
392 void negl(Register reg);
393 void notl(Register reg);
394
395 void enter(const Immediate& imm);
396 void leave();
397
398 void ret();
399 void ret(const Immediate& imm);
400
401 void nop();
402 void int3();
403 void hlt();
404
405 void j(Condition condition, Label* label);
406
407 void jmp(Register reg);
408 void jmp(Label* label);
409
Ian Rogers0d666d82011-08-14 16:03:46 -0700410 Assembler* lock();
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700411 void cmpxchgl(const Address& address, Register reg);
412
Ian Rogers0d666d82011-08-14 16:03:46 -0700413 Assembler* fs();
Ian Rogersb033c752011-07-20 12:22:35 -0700414
415 //
416 // Macros for High-level operations.
417 //
418
419 // Emit code that will create an activation on the stack
Ian Rogers0d666d82011-08-14 16:03:46 -0700420 void BuildFrame(size_t frame_size, ManagedRegister method_reg,
421 const std::vector<ManagedRegister>& spill_regs);
Ian Rogersb033c752011-07-20 12:22:35 -0700422
423 // Emit code that will remove an activation from the stack
Ian Rogers0d666d82011-08-14 16:03:46 -0700424 void RemoveFrame(size_t frame_size,
425 const std::vector<ManagedRegister>& spill_regs);
426
427 // Fill registers from spill area - no-op on x86
428 void FillFromSpillArea(const std::vector<ManagedRegister>& spill_regs,
429 size_t displacement);
Ian Rogersb033c752011-07-20 12:22:35 -0700430
431 void IncreaseFrameSize(size_t adjust);
432 void DecreaseFrameSize(size_t adjust);
433
434 // Store bytes from the given register onto the stack
435 void Store(FrameOffset offs, ManagedRegister src, size_t size);
436 void StoreRef(FrameOffset dest, ManagedRegister src);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700437 void StoreRawPtr(FrameOffset dest, ManagedRegister src);
Ian Rogersb033c752011-07-20 12:22:35 -0700438
439 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister scratch);
440
441 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm,
442 ManagedRegister scratch);
443
444 void StoreImmediateToThread(ThreadOffset dest, uint32_t imm,
445 ManagedRegister scratch);
446
447 void Load(ManagedRegister dest, FrameOffset src, size_t size);
448
449 void LoadRef(ManagedRegister dest, FrameOffset src);
450
451 void LoadRef(ManagedRegister dest, ManagedRegister base, MemberOffset offs);
452
Ian Rogersa04d3972011-08-17 11:33:44 -0700453 void LoadRawPtr(ManagedRegister dest, ManagedRegister base, Offset offs);
454
Ian Rogersb033c752011-07-20 12:22:35 -0700455 void LoadRawPtrFromThread(ManagedRegister dest, ThreadOffset offs);
456
457 void CopyRawPtrFromThread(FrameOffset fr_offs, ThreadOffset thr_offs,
458 ManagedRegister scratch);
459
460 void CopyRawPtrToThread(ThreadOffset thr_offs, FrameOffset fr_offs,
461 ManagedRegister scratch);
462
463 void StoreStackOffsetToThread(ThreadOffset thr_offs, FrameOffset fr_offs,
464 ManagedRegister scratch);
Ian Rogers45a76cb2011-07-21 22:00:15 -0700465 void StoreStackPointerToThread(ThreadOffset thr_offs);
466
Ian Rogersb033c752011-07-20 12:22:35 -0700467 void Move(ManagedRegister dest, ManagedRegister src);
468
469 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister scratch,
470 unsigned int size);
471
Ian Rogers408f79a2011-08-23 18:22:33 -0700472 void CreateSirtEntry(ManagedRegister out_reg, FrameOffset sirt_offset,
473 ManagedRegister in_reg, bool null_allowed);
Ian Rogersb033c752011-07-20 12:22:35 -0700474
Ian Rogers408f79a2011-08-23 18:22:33 -0700475 void CreateSirtEntry(FrameOffset out_off, FrameOffset sirt_offset,
476 ManagedRegister scratch, bool null_allowed);
Ian Rogersb033c752011-07-20 12:22:35 -0700477
Ian Rogers408f79a2011-08-23 18:22:33 -0700478 void LoadReferenceFromSirt(ManagedRegister dst, ManagedRegister src);
Ian Rogersb033c752011-07-20 12:22:35 -0700479
Ian Rogers408f79a2011-08-23 18:22:33 -0700480 void VerifyObject(ManagedRegister src, bool could_be_null);
481 void VerifyObject(FrameOffset src, bool could_be_null);
Ian Rogersb033c752011-07-20 12:22:35 -0700482
Ian Rogersdf20fe02011-07-20 20:34:16 -0700483 void Call(ManagedRegister base, Offset offset, ManagedRegister scratch);
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700484 void Call(FrameOffset base, Offset offset, ManagedRegister scratch);
Shih-wei Liao668512a2011-09-01 14:18:34 -0700485 void Call(uintptr_t addr, ManagedRegister scratch);
486
487 void GetCurrentThread(ManagedRegister tr);
488 void GetCurrentThread(FrameOffset offset, ManagedRegister scratch);
Ian Rogersb033c752011-07-20 12:22:35 -0700489
Ian Rogers45a76cb2011-07-21 22:00:15 -0700490 // Generate code to check if Thread::Current()->suspend_count_ is non-zero
491 // and branch to a SuspendSlowPath if it is. The SuspendSlowPath will continue
492 // at the next instruction.
493 void SuspendPoll(ManagedRegister scratch, ManagedRegister return_reg,
494 FrameOffset return_save_location, size_t return_size);
495
496 // Generate code to check if Thread::Current()->exception_ is non-null
497 // and branch to a ExceptionSlowPath if it is.
498 void ExceptionPoll(ManagedRegister scratch);
499
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700500 void AddImmediate(Register reg, const Immediate& imm);
501
502 void LoadDoubleConstant(XmmRegister dst, double value);
503
504 void DoubleNegate(XmmRegister d);
505 void FloatNegate(XmmRegister f);
506
507 void DoubleAbs(XmmRegister reg);
508
509 void LockCmpxchgl(const Address& address, Register reg) {
Ian Rogers0d666d82011-08-14 16:03:46 -0700510 lock()->cmpxchgl(address, reg);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700511 }
512
Ian Rogersb033c752011-07-20 12:22:35 -0700513 //
514 // Misc. functionality
515 //
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700516 int PreferredLoopAlignment() { return 16; }
517 void Align(int alignment, int offset);
518 void Bind(Label* label);
519
Ian Rogers45a76cb2011-07-21 22:00:15 -0700520 void EmitSlowPaths() { buffer_.EmitSlowPaths(this); }
521
Ian Rogersb033c752011-07-20 12:22:35 -0700522 size_t CodeSize() const { return buffer_.Size(); }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700523
524 void FinalizeInstructions(const MemoryRegion& region) {
525 buffer_.FinalizeInstructions(region);
526 }
527
528 // Debugging and bringup support.
529 void Stop(const char* message);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700530
531 static void InitializeMemoryWithBreakpoints(byte* data, size_t length);
532
533 private:
534 AssemblerBuffer buffer_;
535
536 inline void EmitUint8(uint8_t value);
537 inline void EmitInt32(int32_t value);
538 inline void EmitRegisterOperand(int rm, int reg);
539 inline void EmitXmmRegisterOperand(int rm, XmmRegister reg);
540 inline void EmitFixup(AssemblerFixup* fixup);
541 inline void EmitOperandSizeOverride();
542
543 void EmitOperand(int rm, const Operand& operand);
544 void EmitImmediate(const Immediate& imm);
545 void EmitComplex(int rm, const Operand& operand, const Immediate& immediate);
546 void EmitLabel(Label* label, int instruction_size);
547 void EmitLabelLink(Label* label);
548 void EmitNearLabelLink(Label* label);
549
550 void EmitGenericShift(int rm, Register reg, const Immediate& imm);
551 void EmitGenericShift(int rm, Register operand, Register shifter);
552
553 DISALLOW_COPY_AND_ASSIGN(Assembler);
554};
555
556
557inline void Assembler::EmitUint8(uint8_t value) {
558 buffer_.Emit<uint8_t>(value);
559}
560
561
562inline void Assembler::EmitInt32(int32_t value) {
563 buffer_.Emit<int32_t>(value);
564}
565
566
567inline void Assembler::EmitRegisterOperand(int rm, int reg) {
568 CHECK_GE(rm, 0);
569 CHECK_LT(rm, 8);
570 buffer_.Emit<uint8_t>(0xC0 + (rm << 3) + reg);
571}
572
573
574inline void Assembler::EmitXmmRegisterOperand(int rm, XmmRegister reg) {
575 EmitRegisterOperand(rm, static_cast<Register>(reg));
576}
577
578
579inline void Assembler::EmitFixup(AssemblerFixup* fixup) {
580 buffer_.EmitFixup(fixup);
581}
582
583
584inline void Assembler::EmitOperandSizeOverride() {
585 EmitUint8(0x66);
586}
587
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700588} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700589
590#endif // ART_SRC_ASSEMBLER_X86_H_