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