blob: a93616c3e565ab335f0e9a6d04ac5ab3b6729179 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070016
Ian Rogers166db042013-07-26 12:05:57 -070017#ifndef ART_COMPILER_UTILS_X86_ASSEMBLER_X86_H_
18#define ART_COMPILER_UTILS_X86_ASSEMBLER_X86_H_
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070019
Ian Rogers0d666d82011-08-14 16:03:46 -070020#include <vector>
Vladimir Marko93205e32016-04-13 11:59:46 +010021
22#include "base/arena_containers.h"
David Brazdild9c90372016-09-14 16:53:55 +010023#include "base/array_ref.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010024#include "base/bit_utils.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070025#include "base/enums.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/macros.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070027#include "constants_x86.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "globals.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -070029#include "managed_register_x86.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "offsets.h"
Ian Rogers166db042013-07-26 12:05:57 -070031#include "utils/assembler.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070032
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070033namespace art {
Ian Rogers2c8f6532011-09-02 17:16:34 -070034namespace x86 {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070035
Ian Rogerscf7f1912014-10-22 22:06:39 -070036class Immediate : public ValueObject {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070037 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -080038 explicit Immediate(int32_t value_in) : value_(value_in) {}
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070039
40 int32_t value() const { return value_; }
41
Andreas Gampeab1eb0d2015-02-13 19:23:55 -080042 bool is_int8() const { return IsInt<8>(value_); }
43 bool is_uint8() const { return IsUint<8>(value_); }
44 bool is_int16() const { return IsInt<16>(value_); }
45 bool is_uint16() const { return IsUint<16>(value_); }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070046
47 private:
48 const int32_t value_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070049};
50
51
Ian Rogerscf7f1912014-10-22 22:06:39 -070052class Operand : public ValueObject {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070053 public:
54 uint8_t mod() const {
55 return (encoding_at(0) >> 6) & 3;
56 }
57
58 Register rm() const {
59 return static_cast<Register>(encoding_at(0) & 7);
60 }
61
62 ScaleFactor scale() const {
63 return static_cast<ScaleFactor>((encoding_at(1) >> 6) & 3);
64 }
65
66 Register index() const {
67 return static_cast<Register>((encoding_at(1) >> 3) & 7);
68 }
69
70 Register base() const {
71 return static_cast<Register>(encoding_at(1) & 7);
72 }
73
74 int8_t disp8() const {
75 CHECK_GE(length_, 2);
76 return static_cast<int8_t>(encoding_[length_ - 1]);
77 }
78
79 int32_t disp32() const {
80 CHECK_GE(length_, 5);
81 int32_t value;
82 memcpy(&value, &encoding_[length_ - 4], sizeof(value));
83 return value;
84 }
85
86 bool IsRegister(Register reg) const {
87 return ((encoding_[0] & 0xF8) == 0xC0) // Addressing mode is register only.
88 && ((encoding_[0] & 0x07) == reg); // Register codes match.
89 }
90
91 protected:
92 // Operand can be sub classed (e.g: Address).
Mark Mendell0616ae02015-04-17 12:49:27 -040093 Operand() : length_(0), fixup_(nullptr) { }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070094
Andreas Gampe277ccbd2014-11-03 21:36:10 -080095 void SetModRM(int mod_in, Register rm_in) {
96 CHECK_EQ(mod_in & ~3, 0);
97 encoding_[0] = (mod_in << 6) | rm_in;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070098 length_ = 1;
99 }
100
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800101 void SetSIB(ScaleFactor scale_in, Register index_in, Register base_in) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700102 CHECK_EQ(length_, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800103 CHECK_EQ(scale_in & ~3, 0);
104 encoding_[1] = (scale_in << 6) | (index_in << 3) | base_in;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700105 length_ = 2;
106 }
107
108 void SetDisp8(int8_t disp) {
109 CHECK(length_ == 1 || length_ == 2);
110 encoding_[length_++] = static_cast<uint8_t>(disp);
111 }
112
113 void SetDisp32(int32_t disp) {
114 CHECK(length_ == 1 || length_ == 2);
115 int disp_size = sizeof(disp);
116 memmove(&encoding_[length_], &disp, disp_size);
117 length_ += disp_size;
118 }
119
Mark Mendell0616ae02015-04-17 12:49:27 -0400120 AssemblerFixup* GetFixup() const {
121 return fixup_;
122 }
123
124 void SetFixup(AssemblerFixup* fixup) {
125 fixup_ = fixup;
126 }
127
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700128 private:
Ian Rogers13735952014-10-08 12:43:28 -0700129 uint8_t length_;
130 uint8_t encoding_[6];
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700131
Mark Mendell0616ae02015-04-17 12:49:27 -0400132 // A fixup can be associated with the operand, in order to be applied after the
133 // code has been generated. This is used for constant area fixups.
134 AssemblerFixup* fixup_;
135
136 explicit Operand(Register reg) : fixup_(nullptr) { SetModRM(3, reg); }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700137
138 // Get the operand encoding byte at the given index.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800139 uint8_t encoding_at(int index_in) const {
140 CHECK_GE(index_in, 0);
141 CHECK_LT(index_in, length_);
142 return encoding_[index_in];
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700143 }
144
Ian Rogers2c8f6532011-09-02 17:16:34 -0700145 friend class X86Assembler;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700146};
147
148
149class Address : public Operand {
150 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800151 Address(Register base_in, int32_t disp) {
152 Init(base_in, disp);
Ian Rogersb033c752011-07-20 12:22:35 -0700153 }
154
Mark Mendell0616ae02015-04-17 12:49:27 -0400155 Address(Register base_in, int32_t disp, AssemblerFixup *fixup) {
156 Init(base_in, disp);
157 SetFixup(fixup);
158 }
159
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800160 Address(Register base_in, Offset disp) {
161 Init(base_in, disp.Int32Value());
Ian Rogersa04d3972011-08-17 11:33:44 -0700162 }
163
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800164 Address(Register base_in, FrameOffset disp) {
165 CHECK_EQ(base_in, ESP);
Ian Rogersb033c752011-07-20 12:22:35 -0700166 Init(ESP, disp.Int32Value());
167 }
168
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800169 Address(Register base_in, MemberOffset disp) {
170 Init(base_in, disp.Int32Value());
Ian Rogersb033c752011-07-20 12:22:35 -0700171 }
172
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800173 Address(Register index_in, ScaleFactor scale_in, int32_t disp) {
174 CHECK_NE(index_in, ESP); // Illegal addressing mode.
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700175 SetModRM(0, ESP);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800176 SetSIB(scale_in, index_in, EBP);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700177 SetDisp32(disp);
178 }
179
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800180 Address(Register base_in, Register index_in, ScaleFactor scale_in, int32_t disp) {
Mark Mendell805b3b52015-09-18 14:10:29 -0400181 Init(base_in, index_in, scale_in, disp);
182 }
183
184 Address(Register base_in,
185 Register index_in,
186 ScaleFactor scale_in,
187 int32_t disp, AssemblerFixup *fixup) {
188 Init(base_in, index_in, scale_in, disp);
189 SetFixup(fixup);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700190 }
191
Ian Rogers13735952014-10-08 12:43:28 -0700192 static Address Absolute(uintptr_t addr) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700193 Address result;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700194 result.SetModRM(0, EBP);
195 result.SetDisp32(addr);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700196 return result;
197 }
198
Andreas Gampe542451c2016-07-26 09:02:02 -0700199 static Address Absolute(ThreadOffset32 addr) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700200 return Absolute(addr.Int32Value());
Ian Rogersb033c752011-07-20 12:22:35 -0700201 }
202
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700203 private:
204 Address() {}
Mark Mendell805b3b52015-09-18 14:10:29 -0400205
206 void Init(Register base_in, int32_t disp) {
207 if (disp == 0 && base_in != EBP) {
208 SetModRM(0, base_in);
209 if (base_in == ESP) SetSIB(TIMES_1, ESP, base_in);
210 } else if (disp >= -128 && disp <= 127) {
211 SetModRM(1, base_in);
212 if (base_in == ESP) SetSIB(TIMES_1, ESP, base_in);
213 SetDisp8(disp);
214 } else {
215 SetModRM(2, base_in);
216 if (base_in == ESP) SetSIB(TIMES_1, ESP, base_in);
217 SetDisp32(disp);
218 }
219 }
220
221 void Init(Register base_in, Register index_in, ScaleFactor scale_in, int32_t disp) {
222 CHECK_NE(index_in, ESP); // Illegal addressing mode.
223 if (disp == 0 && base_in != EBP) {
224 SetModRM(0, ESP);
225 SetSIB(scale_in, index_in, base_in);
226 } else if (disp >= -128 && disp <= 127) {
227 SetModRM(1, ESP);
228 SetSIB(scale_in, index_in, base_in);
229 SetDisp8(disp);
230 } else {
231 SetModRM(2, ESP);
232 SetSIB(scale_in, index_in, base_in);
233 SetDisp32(disp);
234 }
235 }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700236};
237
238
Mark Mendell73f455e2015-08-21 09:30:05 -0400239// This is equivalent to the Label class, used in a slightly different context. We
240// inherit the functionality of the Label class, but prevent unintended
241// derived-to-base conversions by making the base class private.
242class NearLabel : private Label {
243 public:
244 NearLabel() : Label() {}
245
246 // Expose the Label routines that we need.
247 using Label::Position;
248 using Label::LinkPosition;
249 using Label::IsBound;
250 using Label::IsUnused;
251 using Label::IsLinked;
252
253 private:
254 using Label::BindTo;
255 using Label::LinkTo;
256
257 friend class x86::X86Assembler;
258
259 DISALLOW_COPY_AND_ASSIGN(NearLabel);
260};
261
Mark Mendell0616ae02015-04-17 12:49:27 -0400262/**
263 * Class to handle constant area values.
264 */
265class ConstantArea {
266 public:
Vladimir Marko93205e32016-04-13 11:59:46 +0100267 explicit ConstantArea(ArenaAllocator* arena) : buffer_(arena->Adapter(kArenaAllocAssembler)) {}
Mark Mendell0616ae02015-04-17 12:49:27 -0400268
269 // Add a double to the constant area, returning the offset into
270 // the constant area where the literal resides.
Mark Mendell805b3b52015-09-18 14:10:29 -0400271 size_t AddDouble(double v);
Mark Mendell0616ae02015-04-17 12:49:27 -0400272
273 // Add a float to the constant area, returning the offset into
274 // the constant area where the literal resides.
Mark Mendell805b3b52015-09-18 14:10:29 -0400275 size_t AddFloat(float v);
Mark Mendell0616ae02015-04-17 12:49:27 -0400276
277 // Add an int32_t to the constant area, returning the offset into
278 // the constant area where the literal resides.
Mark Mendell805b3b52015-09-18 14:10:29 -0400279 size_t AddInt32(int32_t v);
280
281 // Add an int32_t to the end of the constant area, returning the offset into
282 // the constant area where the literal resides.
283 size_t AppendInt32(int32_t v);
Mark Mendell0616ae02015-04-17 12:49:27 -0400284
285 // Add an int64_t to the constant area, returning the offset into
286 // the constant area where the literal resides.
Mark Mendell805b3b52015-09-18 14:10:29 -0400287 size_t AddInt64(int64_t v);
Mark Mendell0616ae02015-04-17 12:49:27 -0400288
289 bool IsEmpty() const {
290 return buffer_.size() == 0;
291 }
292
Mark Mendell805b3b52015-09-18 14:10:29 -0400293 size_t GetSize() const {
294 return buffer_.size() * elem_size_;
295 }
296
Vladimir Marko93205e32016-04-13 11:59:46 +0100297 ArrayRef<const int32_t> GetBuffer() const {
298 return ArrayRef<const int32_t>(buffer_);
Mark Mendell0616ae02015-04-17 12:49:27 -0400299 }
300
Mark Mendell0616ae02015-04-17 12:49:27 -0400301 private:
Mark Mendell805b3b52015-09-18 14:10:29 -0400302 static constexpr size_t elem_size_ = sizeof(int32_t);
Vladimir Marko93205e32016-04-13 11:59:46 +0100303 ArenaVector<int32_t> buffer_;
Mark Mendell0616ae02015-04-17 12:49:27 -0400304};
Mark Mendell73f455e2015-08-21 09:30:05 -0400305
Andreas Gampe9954e3b2016-08-05 20:34:39 -0700306class X86Assembler FINAL : public Assembler {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700307 public:
Vladimir Marko93205e32016-04-13 11:59:46 +0100308 explicit X86Assembler(ArenaAllocator* arena) : Assembler(arena), constant_area_(arena) {}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700309 virtual ~X86Assembler() {}
buzbeec143c552011-08-20 17:38:58 -0700310
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700311 /*
312 * Emit Machine Instructions.
313 */
314 void call(Register reg);
315 void call(const Address& address);
316 void call(Label* label);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000317 void call(const ExternalLabel& label);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700318
319 void pushl(Register reg);
320 void pushl(const Address& address);
321 void pushl(const Immediate& imm);
322
323 void popl(Register reg);
324 void popl(const Address& address);
325
326 void movl(Register dst, const Immediate& src);
327 void movl(Register dst, Register src);
328
329 void movl(Register dst, const Address& src);
330 void movl(const Address& dst, Register src);
331 void movl(const Address& dst, const Immediate& imm);
Ian Rogersbdb03912011-09-14 00:55:44 -0700332 void movl(const Address& dst, Label* lbl);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700333
Mark Mendell7a08fb52015-07-15 14:09:35 -0400334 void movntl(const Address& dst, Register src);
335
Mark Mendell09ed1a32015-03-25 08:30:06 -0400336 void bswapl(Register dst);
Aart Bikc39dac12016-01-21 08:59:48 -0800337
Mark Mendellbcee0922015-09-15 21:45:01 -0400338 void bsfl(Register dst, Register src);
339 void bsfl(Register dst, const Address& src);
Mark Mendell8ae3ffb2015-08-12 21:16:41 -0400340 void bsrl(Register dst, Register src);
341 void bsrl(Register dst, const Address& src);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400342
Aart Bikc39dac12016-01-21 08:59:48 -0800343 void popcntl(Register dst, Register src);
344 void popcntl(Register dst, const Address& src);
345
Mark Mendellbcee0922015-09-15 21:45:01 -0400346 void rorl(Register reg, const Immediate& imm);
347 void rorl(Register operand, Register shifter);
348 void roll(Register reg, const Immediate& imm);
349 void roll(Register operand, Register shifter);
350
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700351 void movzxb(Register dst, ByteRegister src);
352 void movzxb(Register dst, const Address& src);
353 void movsxb(Register dst, ByteRegister src);
354 void movsxb(Register dst, const Address& src);
355 void movb(Register dst, const Address& src);
356 void movb(const Address& dst, ByteRegister src);
357 void movb(const Address& dst, const Immediate& imm);
358
359 void movzxw(Register dst, Register src);
360 void movzxw(Register dst, const Address& src);
361 void movsxw(Register dst, Register src);
362 void movsxw(Register dst, const Address& src);
363 void movw(Register dst, const Address& src);
364 void movw(const Address& dst, Register src);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100365 void movw(const Address& dst, const Immediate& imm);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700366
367 void leal(Register dst, const Address& src);
368
Ian Rogersb033c752011-07-20 12:22:35 -0700369 void cmovl(Condition condition, Register dst, Register src);
Mark Mendellabdac472016-02-12 13:49:03 -0500370 void cmovl(Condition condition, Register dst, const Address& src);
Ian Rogersb033c752011-07-20 12:22:35 -0700371
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000372 void setb(Condition condition, Register dst);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700373
Aart Bikc7782262017-01-13 16:20:08 -0800374 void movaps(XmmRegister dst, XmmRegister src); // move
375 void movaps(XmmRegister dst, const Address& src); // load aligned
376 void movups(XmmRegister dst, const Address& src); // load unaligned
377 void movaps(const Address& dst, XmmRegister src); // store aligned
378 void movups(const Address& dst, XmmRegister src); // store unaligned
379
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700380 void movss(XmmRegister dst, const Address& src);
381 void movss(const Address& dst, XmmRegister src);
382 void movss(XmmRegister dst, XmmRegister src);
383
384 void movd(XmmRegister dst, Register src);
385 void movd(Register dst, XmmRegister src);
386
387 void addss(XmmRegister dst, XmmRegister src);
388 void addss(XmmRegister dst, const Address& src);
389 void subss(XmmRegister dst, XmmRegister src);
390 void subss(XmmRegister dst, const Address& src);
391 void mulss(XmmRegister dst, XmmRegister src);
392 void mulss(XmmRegister dst, const Address& src);
393 void divss(XmmRegister dst, XmmRegister src);
394 void divss(XmmRegister dst, const Address& src);
395
Aart Bikc7782262017-01-13 16:20:08 -0800396 void addps(XmmRegister dst, XmmRegister src); // no addr variant (for now)
397 void subps(XmmRegister dst, XmmRegister src);
398 void mulps(XmmRegister dst, XmmRegister src);
399 void divps(XmmRegister dst, XmmRegister src);
400
401 void movapd(XmmRegister dst, XmmRegister src); // move
402 void movapd(XmmRegister dst, const Address& src); // load aligned
403 void movupd(XmmRegister dst, const Address& src); // load unaligned
404 void movapd(const Address& dst, XmmRegister src); // store aligned
405 void movupd(const Address& dst, XmmRegister src); // store unaligned
406
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700407 void movsd(XmmRegister dst, const Address& src);
408 void movsd(const Address& dst, XmmRegister src);
409 void movsd(XmmRegister dst, XmmRegister src);
410
Calin Juravle52c48962014-12-16 17:02:57 +0000411 void psrlq(XmmRegister reg, const Immediate& shift_count);
412 void punpckldq(XmmRegister dst, XmmRegister src);
413
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000414 void movhpd(XmmRegister dst, const Address& src);
415 void movhpd(const Address& dst, XmmRegister src);
416
417 void psrldq(XmmRegister reg, const Immediate& shift_count);
418
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700419 void addsd(XmmRegister dst, XmmRegister src);
420 void addsd(XmmRegister dst, const Address& src);
421 void subsd(XmmRegister dst, XmmRegister src);
422 void subsd(XmmRegister dst, const Address& src);
423 void mulsd(XmmRegister dst, XmmRegister src);
424 void mulsd(XmmRegister dst, const Address& src);
425 void divsd(XmmRegister dst, XmmRegister src);
426 void divsd(XmmRegister dst, const Address& src);
427
Aart Bikc7782262017-01-13 16:20:08 -0800428 void addpd(XmmRegister dst, XmmRegister src); // no addr variant (for now)
429 void subpd(XmmRegister dst, XmmRegister src);
430 void mulpd(XmmRegister dst, XmmRegister src);
431 void divpd(XmmRegister dst, XmmRegister src);
432
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700433 void cvtsi2ss(XmmRegister dst, Register src);
434 void cvtsi2sd(XmmRegister dst, Register src);
435
436 void cvtss2si(Register dst, XmmRegister src);
437 void cvtss2sd(XmmRegister dst, XmmRegister src);
438
439 void cvtsd2si(Register dst, XmmRegister src);
440 void cvtsd2ss(XmmRegister dst, XmmRegister src);
441
442 void cvttss2si(Register dst, XmmRegister src);
443 void cvttsd2si(Register dst, XmmRegister src);
444
445 void cvtdq2pd(XmmRegister dst, XmmRegister src);
446
447 void comiss(XmmRegister a, XmmRegister b);
Aart Bik18ba1212016-08-01 14:11:20 -0700448 void comiss(XmmRegister a, const Address& b);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700449 void comisd(XmmRegister a, XmmRegister b);
Aart Bik18ba1212016-08-01 14:11:20 -0700450 void comisd(XmmRegister a, const Address& b);
Calin Juravleddb7df22014-11-25 20:56:51 +0000451 void ucomiss(XmmRegister a, XmmRegister b);
Mark Mendell9f51f262015-10-30 09:21:37 -0400452 void ucomiss(XmmRegister a, const Address& b);
Calin Juravleddb7df22014-11-25 20:56:51 +0000453 void ucomisd(XmmRegister a, XmmRegister b);
Mark Mendell9f51f262015-10-30 09:21:37 -0400454 void ucomisd(XmmRegister a, const Address& b);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700455
Mark Mendellfb8d2792015-03-31 22:16:59 -0400456 void roundsd(XmmRegister dst, XmmRegister src, const Immediate& imm);
457 void roundss(XmmRegister dst, XmmRegister src, const Immediate& imm);
458
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700459 void sqrtsd(XmmRegister dst, XmmRegister src);
460 void sqrtss(XmmRegister dst, XmmRegister src);
461
462 void xorpd(XmmRegister dst, const Address& src);
463 void xorpd(XmmRegister dst, XmmRegister src);
464 void xorps(XmmRegister dst, const Address& src);
465 void xorps(XmmRegister dst, XmmRegister src);
466
Mark Mendell09ed1a32015-03-25 08:30:06 -0400467 void andpd(XmmRegister dst, XmmRegister src);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700468 void andpd(XmmRegister dst, const Address& src);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400469 void andps(XmmRegister dst, XmmRegister src);
470 void andps(XmmRegister dst, const Address& src);
471
472 void orpd(XmmRegister dst, XmmRegister src);
473 void orps(XmmRegister dst, XmmRegister src);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700474
475 void flds(const Address& src);
476 void fstps(const Address& dst);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500477 void fsts(const Address& dst);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700478
479 void fldl(const Address& src);
480 void fstpl(const Address& dst);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500481 void fstl(const Address& dst);
482
483 void fstsw();
484
485 void fucompp();
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700486
487 void fnstcw(const Address& dst);
488 void fldcw(const Address& src);
489
490 void fistpl(const Address& dst);
491 void fistps(const Address& dst);
492 void fildl(const Address& src);
Roland Levillain0a186012015-04-13 17:00:20 +0100493 void filds(const Address& src);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700494
495 void fincstp();
496 void ffree(const Immediate& index);
497
498 void fsin();
499 void fcos();
500 void fptan();
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500501 void fprem();
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700502
503 void xchgl(Register dst, Register src);
Ian Rogers7caad772012-03-30 01:07:54 -0700504 void xchgl(Register reg, const Address& address);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700505
Serguei Katkov3b625932016-05-06 10:24:17 +0600506 void cmpb(const Address& address, const Immediate& imm);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100507 void cmpw(const Address& address, const Immediate& imm);
508
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700509 void cmpl(Register reg, const Immediate& imm);
510 void cmpl(Register reg0, Register reg1);
511 void cmpl(Register reg, const Address& address);
512
513 void cmpl(const Address& address, Register reg);
514 void cmpl(const Address& address, const Immediate& imm);
515
516 void testl(Register reg1, Register reg2);
517 void testl(Register reg, const Immediate& imm);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100518 void testl(Register reg1, const Address& address);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700519
Vladimir Marko953437b2016-08-24 08:30:46 +0000520 void testb(const Address& dst, const Immediate& imm);
521 void testl(const Address& dst, const Immediate& imm);
522
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700523 void andl(Register dst, const Immediate& imm);
524 void andl(Register dst, Register src);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000525 void andl(Register dst, const Address& address);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700526
527 void orl(Register dst, const Immediate& imm);
528 void orl(Register dst, Register src);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000529 void orl(Register dst, const Address& address);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700530
531 void xorl(Register dst, Register src);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100532 void xorl(Register dst, const Immediate& imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000533 void xorl(Register dst, const Address& address);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700534
535 void addl(Register dst, Register src);
536 void addl(Register reg, const Immediate& imm);
537 void addl(Register reg, const Address& address);
538
539 void addl(const Address& address, Register reg);
540 void addl(const Address& address, const Immediate& imm);
541
542 void adcl(Register dst, Register src);
543 void adcl(Register reg, const Immediate& imm);
544 void adcl(Register dst, const Address& address);
545
546 void subl(Register dst, Register src);
547 void subl(Register reg, const Immediate& imm);
548 void subl(Register reg, const Address& address);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400549 void subl(const Address& address, Register src);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700550
551 void cdq();
552
553 void idivl(Register reg);
554
555 void imull(Register dst, Register src);
556 void imull(Register reg, const Immediate& imm);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -0400557 void imull(Register dst, Register src, const Immediate& imm);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700558 void imull(Register reg, const Address& address);
559
560 void imull(Register reg);
561 void imull(const Address& address);
562
563 void mull(Register reg);
564 void mull(const Address& address);
565
566 void sbbl(Register dst, Register src);
567 void sbbl(Register reg, const Immediate& imm);
568 void sbbl(Register reg, const Address& address);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400569 void sbbl(const Address& address, Register src);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700570
571 void incl(Register reg);
572 void incl(const Address& address);
573
574 void decl(Register reg);
575 void decl(const Address& address);
576
577 void shll(Register reg, const Immediate& imm);
578 void shll(Register operand, Register shifter);
Mark P Mendell73945692015-04-29 14:56:17 +0000579 void shll(const Address& address, const Immediate& imm);
580 void shll(const Address& address, Register shifter);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700581 void shrl(Register reg, const Immediate& imm);
582 void shrl(Register operand, Register shifter);
Mark P Mendell73945692015-04-29 14:56:17 +0000583 void shrl(const Address& address, const Immediate& imm);
584 void shrl(const Address& address, Register shifter);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700585 void sarl(Register reg, const Immediate& imm);
586 void sarl(Register operand, Register shifter);
Mark P Mendell73945692015-04-29 14:56:17 +0000587 void sarl(const Address& address, const Immediate& imm);
588 void sarl(const Address& address, Register shifter);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000589 void shld(Register dst, Register src, Register shifter);
Mark P Mendell73945692015-04-29 14:56:17 +0000590 void shld(Register dst, Register src, const Immediate& imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000591 void shrd(Register dst, Register src, Register shifter);
Mark P Mendell73945692015-04-29 14:56:17 +0000592 void shrd(Register dst, Register src, const Immediate& imm);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700593
594 void negl(Register reg);
595 void notl(Register reg);
596
597 void enter(const Immediate& imm);
598 void leave();
599
600 void ret();
601 void ret(const Immediate& imm);
602
603 void nop();
604 void int3();
605 void hlt();
606
607 void j(Condition condition, Label* label);
Mark Mendell73f455e2015-08-21 09:30:05 -0400608 void j(Condition condition, NearLabel* label);
609 void jecxz(NearLabel* label);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700610
611 void jmp(Register reg);
Ian Rogers7caad772012-03-30 01:07:54 -0700612 void jmp(const Address& address);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700613 void jmp(Label* label);
Mark Mendell73f455e2015-08-21 09:30:05 -0400614 void jmp(NearLabel* label);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700615
jessicahandojob03d6402016-09-07 12:16:53 -0700616 void repne_scasb();
Andreas Gampe21030dd2015-05-07 14:46:15 -0700617 void repne_scasw();
jessicahandojob03d6402016-09-07 12:16:53 -0700618 void repe_cmpsb();
agicsaki71311f82015-07-27 11:34:13 -0700619 void repe_cmpsw();
agicsaki970abfb2015-07-31 10:31:14 -0700620 void repe_cmpsl();
jessicahandojob03d6402016-09-07 12:16:53 -0700621 void rep_movsb();
Mark Mendellb9c4bbe2015-07-01 14:26:52 -0400622 void rep_movsw();
Andreas Gampe21030dd2015-05-07 14:46:15 -0700623
Ian Rogers2c8f6532011-09-02 17:16:34 -0700624 X86Assembler* lock();
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700625 void cmpxchgl(const Address& address, Register reg);
Mark Mendell58d25fd2015-04-03 14:52:31 -0400626 void cmpxchg8b(const Address& address);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700627
Elliott Hughes79ab9e32012-03-12 15:41:35 -0700628 void mfence();
629
Ian Rogers2c8f6532011-09-02 17:16:34 -0700630 X86Assembler* fs();
Ian Rogersbefbd572014-03-06 01:13:39 -0800631 X86Assembler* gs();
Ian Rogersb033c752011-07-20 12:22:35 -0700632
633 //
634 // Macros for High-level operations.
635 //
636
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700637 void AddImmediate(Register reg, const Immediate& imm);
638
Roland Levillain647b9ed2014-11-27 12:06:00 +0000639 void LoadLongConstant(XmmRegister dst, int64_t value);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700640 void LoadDoubleConstant(XmmRegister dst, double value);
641
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700642 void LockCmpxchgl(const Address& address, Register reg) {
Ian Rogers0d666d82011-08-14 16:03:46 -0700643 lock()->cmpxchgl(address, reg);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700644 }
645
Mark Mendell58d25fd2015-04-03 14:52:31 -0400646 void LockCmpxchg8b(const Address& address) {
647 lock()->cmpxchg8b(address);
648 }
649
Ian Rogersb033c752011-07-20 12:22:35 -0700650 //
651 // Misc. functionality
652 //
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700653 int PreferredLoopAlignment() { return 16; }
654 void Align(int alignment, int offset);
Andreas Gampe85b62f22015-09-09 13:15:38 -0700655 void Bind(Label* label) OVERRIDE;
656 void Jump(Label* label) OVERRIDE {
657 jmp(label);
658 }
Mark Mendell73f455e2015-08-21 09:30:05 -0400659 void Bind(NearLabel* label);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700660
Ian Rogers2c8f6532011-09-02 17:16:34 -0700661 //
Roland Levillain4d027112015-07-01 15:41:14 +0100662 // Heap poisoning.
663 //
664
665 // Poison a heap reference contained in `reg`.
666 void PoisonHeapReference(Register reg) { negl(reg); }
667 // Unpoison a heap reference contained in `reg`.
668 void UnpoisonHeapReference(Register reg) { negl(reg); }
Roland Levillain0b671c02016-08-19 12:02:34 +0100669 // Poison a heap reference contained in `reg` if heap poisoning is enabled.
670 void MaybePoisonHeapReference(Register reg) {
671 if (kPoisonHeapReferences) {
672 PoisonHeapReference(reg);
673 }
674 }
Roland Levillain4d027112015-07-01 15:41:14 +0100675 // Unpoison a heap reference contained in `reg` if heap poisoning is enabled.
676 void MaybeUnpoisonHeapReference(Register reg) {
677 if (kPoisonHeapReferences) {
678 UnpoisonHeapReference(reg);
679 }
680 }
681
Mark Mendell0616ae02015-04-17 12:49:27 -0400682 // Add a double to the constant area, returning the offset into
683 // the constant area where the literal resides.
Mark Mendell805b3b52015-09-18 14:10:29 -0400684 size_t AddDouble(double v) { return constant_area_.AddDouble(v); }
Mark Mendell0616ae02015-04-17 12:49:27 -0400685
686 // Add a float to the constant area, returning the offset into
687 // the constant area where the literal resides.
Mark Mendell805b3b52015-09-18 14:10:29 -0400688 size_t AddFloat(float v) { return constant_area_.AddFloat(v); }
Mark Mendell0616ae02015-04-17 12:49:27 -0400689
690 // Add an int32_t to the constant area, returning the offset into
691 // the constant area where the literal resides.
Mark Mendell805b3b52015-09-18 14:10:29 -0400692 size_t AddInt32(int32_t v) {
693 return constant_area_.AddInt32(v);
694 }
695
696 // Add an int32_t to the end of the constant area, returning the offset into
697 // the constant area where the literal resides.
698 size_t AppendInt32(int32_t v) {
699 return constant_area_.AppendInt32(v);
700 }
Mark Mendell0616ae02015-04-17 12:49:27 -0400701
702 // Add an int64_t to the constant area, returning the offset into
703 // the constant area where the literal resides.
Mark Mendell805b3b52015-09-18 14:10:29 -0400704 size_t AddInt64(int64_t v) { return constant_area_.AddInt64(v); }
Mark Mendell0616ae02015-04-17 12:49:27 -0400705
706 // Add the contents of the constant area to the assembler buffer.
707 void AddConstantArea();
708
709 // Is the constant area empty? Return true if there are no literals in the constant area.
710 bool IsConstantAreaEmpty() const { return constant_area_.IsEmpty(); }
Mark Mendell805b3b52015-09-18 14:10:29 -0400711
712 // Return the current size of the constant area.
713 size_t ConstantAreaSize() const { return constant_area_.GetSize(); }
Mark Mendell0616ae02015-04-17 12:49:27 -0400714
Ian Rogers2c8f6532011-09-02 17:16:34 -0700715 private:
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700716 inline void EmitUint8(uint8_t value);
717 inline void EmitInt32(int32_t value);
718 inline void EmitRegisterOperand(int rm, int reg);
719 inline void EmitXmmRegisterOperand(int rm, XmmRegister reg);
720 inline void EmitFixup(AssemblerFixup* fixup);
721 inline void EmitOperandSizeOverride();
722
723 void EmitOperand(int rm, const Operand& operand);
724 void EmitImmediate(const Immediate& imm);
725 void EmitComplex(int rm, const Operand& operand, const Immediate& immediate);
726 void EmitLabel(Label* label, int instruction_size);
727 void EmitLabelLink(Label* label);
Mark Mendell73f455e2015-08-21 09:30:05 -0400728 void EmitLabelLink(NearLabel* label);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700729
Mark P Mendell73945692015-04-29 14:56:17 +0000730 void EmitGenericShift(int rm, const Operand& operand, const Immediate& imm);
731 void EmitGenericShift(int rm, const Operand& operand, Register shifter);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700732
Mark Mendell0616ae02015-04-17 12:49:27 -0400733 ConstantArea constant_area_;
734
Ian Rogers2c8f6532011-09-02 17:16:34 -0700735 DISALLOW_COPY_AND_ASSIGN(X86Assembler);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700736};
737
Ian Rogers2c8f6532011-09-02 17:16:34 -0700738inline void X86Assembler::EmitUint8(uint8_t value) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700739 buffer_.Emit<uint8_t>(value);
740}
741
Ian Rogers2c8f6532011-09-02 17:16:34 -0700742inline void X86Assembler::EmitInt32(int32_t value) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700743 buffer_.Emit<int32_t>(value);
744}
745
Ian Rogers2c8f6532011-09-02 17:16:34 -0700746inline void X86Assembler::EmitRegisterOperand(int rm, int reg) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700747 CHECK_GE(rm, 0);
748 CHECK_LT(rm, 8);
749 buffer_.Emit<uint8_t>(0xC0 + (rm << 3) + reg);
750}
751
Ian Rogers2c8f6532011-09-02 17:16:34 -0700752inline void X86Assembler::EmitXmmRegisterOperand(int rm, XmmRegister reg) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700753 EmitRegisterOperand(rm, static_cast<Register>(reg));
754}
755
Ian Rogers2c8f6532011-09-02 17:16:34 -0700756inline void X86Assembler::EmitFixup(AssemblerFixup* fixup) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700757 buffer_.EmitFixup(fixup);
758}
759
Ian Rogers2c8f6532011-09-02 17:16:34 -0700760inline void X86Assembler::EmitOperandSizeOverride() {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700761 EmitUint8(0x66);
762}
763
Ian Rogers2c8f6532011-09-02 17:16:34 -0700764} // namespace x86
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700765} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700766
Ian Rogers166db042013-07-26 12:05:57 -0700767#endif // ART_COMPILER_UTILS_X86_ASSEMBLER_X86_H_