blob: 981141aee6ed1d1437b777afbfacc9fbb1cb32be [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
6#include <stdint.h>
7#include <string.h>
Jesse Wilsonc981ace2011-06-22 14:17:14 -07008#include "src/assembler.h"
Ian Rogersb033c752011-07-20 12:22:35 -07009#include "src/constants.h"
10#include "src/managed_register.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070011#include "src/macros.h"
12#include "src/utils.h"
13
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
175 static Address Absolute(const uword addr) {
176 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);
422
423 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister scratch);
424
425 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm,
426 ManagedRegister scratch);
427
428 void StoreImmediateToThread(ThreadOffset dest, uint32_t imm,
429 ManagedRegister scratch);
430
431 void Load(ManagedRegister dest, FrameOffset src, size_t size);
432
433 void LoadRef(ManagedRegister dest, FrameOffset src);
434
435 void LoadRef(ManagedRegister dest, ManagedRegister base, MemberOffset offs);
436
437 void LoadRawPtrFromThread(ManagedRegister dest, ThreadOffset offs);
438
439 void CopyRawPtrFromThread(FrameOffset fr_offs, ThreadOffset thr_offs,
440 ManagedRegister scratch);
441
442 void CopyRawPtrToThread(ThreadOffset thr_offs, FrameOffset fr_offs,
443 ManagedRegister scratch);
444
445 void StoreStackOffsetToThread(ThreadOffset thr_offs, FrameOffset fr_offs,
446 ManagedRegister scratch);
447 void Move(ManagedRegister dest, ManagedRegister src);
448
449 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister scratch,
450 unsigned int size);
451
452 void CreateStackHandle(ManagedRegister out_reg, FrameOffset handle_offset,
453 ManagedRegister in_reg, bool null_allowed);
454
455 void CreateStackHandle(FrameOffset out_off, FrameOffset handle_offset,
456 ManagedRegister scratch, bool null_allowed);
457
458 void LoadReferenceFromStackHandle(ManagedRegister dst, ManagedRegister src,
459 FrameOffset shb_offset);
460
461 void ValidateRef(ManagedRegister src, bool could_be_null);
462 void ValidateRef(FrameOffset src, bool could_be_null);
463
464 void Call(ManagedRegister base, MemberOffset offset, ManagedRegister scratch);
465
466 // Emit code that will lock the reference in the given frame location
467 void LockReferenceOnStack(FrameOffset fr_offs);
468
469 // Emit code that will unlock the reference in the given frame location
470 void UnLockReferenceOnStack(FrameOffset fr_offs);
471
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700472 void AddImmediate(Register reg, const Immediate& imm);
473
474 void LoadDoubleConstant(XmmRegister dst, double value);
475
476 void DoubleNegate(XmmRegister d);
477 void FloatNegate(XmmRegister f);
478
479 void DoubleAbs(XmmRegister reg);
480
481 void LockCmpxchgl(const Address& address, Register reg) {
482 lock();
483 cmpxchgl(address, reg);
484 }
485
Ian Rogersb033c752011-07-20 12:22:35 -0700486 //
487 // Misc. functionality
488 //
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700489 int PreferredLoopAlignment() { return 16; }
490 void Align(int alignment, int offset);
491 void Bind(Label* label);
492
Ian Rogersb033c752011-07-20 12:22:35 -0700493 size_t CodeSize() const { return buffer_.Size(); }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700494
495 void FinalizeInstructions(const MemoryRegion& region) {
496 buffer_.FinalizeInstructions(region);
497 }
498
499 // Debugging and bringup support.
500 void Stop(const char* message);
501 void Unimplemented(const char* message);
502 void Untested(const char* message);
503 void Unreachable(const char* message);
504
505 static void InitializeMemoryWithBreakpoints(byte* data, size_t length);
506
507 private:
508 AssemblerBuffer buffer_;
509
510 inline void EmitUint8(uint8_t value);
511 inline void EmitInt32(int32_t value);
512 inline void EmitRegisterOperand(int rm, int reg);
513 inline void EmitXmmRegisterOperand(int rm, XmmRegister reg);
514 inline void EmitFixup(AssemblerFixup* fixup);
515 inline void EmitOperandSizeOverride();
516
517 void EmitOperand(int rm, const Operand& operand);
518 void EmitImmediate(const Immediate& imm);
519 void EmitComplex(int rm, const Operand& operand, const Immediate& immediate);
520 void EmitLabel(Label* label, int instruction_size);
521 void EmitLabelLink(Label* label);
522 void EmitNearLabelLink(Label* label);
523
524 void EmitGenericShift(int rm, Register reg, const Immediate& imm);
525 void EmitGenericShift(int rm, Register operand, Register shifter);
526
527 DISALLOW_COPY_AND_ASSIGN(Assembler);
528};
529
530
531inline void Assembler::EmitUint8(uint8_t value) {
532 buffer_.Emit<uint8_t>(value);
533}
534
535
536inline void Assembler::EmitInt32(int32_t value) {
537 buffer_.Emit<int32_t>(value);
538}
539
540
541inline void Assembler::EmitRegisterOperand(int rm, int reg) {
542 CHECK_GE(rm, 0);
543 CHECK_LT(rm, 8);
544 buffer_.Emit<uint8_t>(0xC0 + (rm << 3) + reg);
545}
546
547
548inline void Assembler::EmitXmmRegisterOperand(int rm, XmmRegister reg) {
549 EmitRegisterOperand(rm, static_cast<Register>(reg));
550}
551
552
553inline void Assembler::EmitFixup(AssemblerFixup* fixup) {
554 buffer_.EmitFixup(fixup);
555}
556
557
558inline void Assembler::EmitOperandSizeOverride() {
559 EmitUint8(0x66);
560}
561
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700562} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700563
564#endif // ART_SRC_ASSEMBLER_X86_H_