blob: 15fc29c29f8539d71a0d6439e87de5f2003a3b31 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright (c) 1994-2006 Sun Microsystems Inc.
2// All Rights Reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// - Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10//
11// - Redistribution in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// - Neither the name of Sun Microsystems or the names of contributors may
16// be used to endorse or promote products derived from this software without
17// specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// The original source code covered by the above license above has been
32// modified significantly by Google Inc.
33// Copyright 2011 the V8 project authors. All rights reserved.
34
35// A light-weight IA32 Assembler.
36
37#ifndef V8_X87_ASSEMBLER_X87_H_
38#define V8_X87_ASSEMBLER_X87_H_
39
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040#include <deque>
41
42#include "src/assembler.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043#include "src/isolate.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044#include "src/utils.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045
46namespace v8 {
47namespace internal {
48
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049#define GENERAL_REGISTERS(V) \
50 V(eax) \
51 V(ecx) \
52 V(edx) \
53 V(ebx) \
54 V(esp) \
55 V(ebp) \
56 V(esi) \
57 V(edi)
58
59#define ALLOCATABLE_GENERAL_REGISTERS(V) \
60 V(eax) \
61 V(ecx) \
62 V(edx) \
63 V(ebx) \
64 V(esi) \
65 V(edi)
66
67#define DOUBLE_REGISTERS(V) \
68 V(stX_0) \
69 V(stX_1) \
70 V(stX_2) \
71 V(stX_3) \
72 V(stX_4) \
73 V(stX_5) \
74 V(stX_6) \
75 V(stX_7)
76
77#define ALLOCATABLE_DOUBLE_REGISTERS(V) \
78 V(stX_0) \
79 V(stX_1) \
80 V(stX_2) \
81 V(stX_3) \
82 V(stX_4) \
83 V(stX_5)
84
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085// CPU Registers.
86//
87// 1) We would prefer to use an enum, but enum values are assignment-
88// compatible with int, which has caused code-generation bugs.
89//
90// 2) We would prefer to use a class instead of a struct but we don't like
91// the register initialization to depend on the particular initialization
92// order (which appears to be different on OS X, Linux, and Windows for the
93// installed versions of C++ we tried). Using a struct permits C-style
94// "initialization". Also, the Register objects cannot be const as this
95// forces initialization stubs in MSVC, making us dependent on initialization
96// order.
97//
98// 3) By not using an enum, we are possibly preventing the compiler from
99// doing certain constant folds, which may significantly reduce the
100// code generated for some assembly instructions (because they boil down
101// to a few constants). If this is a problem, we could change the code
102// such that we use an enum in optimized mode, and the struct in debug
103// mode. This way we get the compile-time error checking in debug mode
104// and best performance in optimized code.
105//
106struct Register {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000107 enum Code {
108#define REGISTER_CODE(R) kCode_##R,
109 GENERAL_REGISTERS(REGISTER_CODE)
110#undef REGISTER_CODE
111 kAfterLast,
112 kCode_no_reg = -1
113 };
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115 static const int kNumRegisters = Code::kAfterLast;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116
117 static Register from_code(int code) {
118 DCHECK(code >= 0);
119 DCHECK(code < kNumRegisters);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000120 Register r = {code};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 return r;
122 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000123 const char* ToString();
124 bool IsAllocatable() const;
125 bool is_valid() const { return 0 <= reg_code && reg_code < kNumRegisters; }
126 bool is(Register reg) const { return reg_code == reg.reg_code; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000127 int code() const {
128 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129 return reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000130 }
131 int bit() const {
132 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000133 return 1 << reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000134 }
135
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136 bool is_byte_register() const { return reg_code <= 3; }
137
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 // Unfortunately we can't make this private in a struct.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000139 int reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140};
141
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143#define DECLARE_REGISTER(R) const Register R = {Register::kCode_##R};
144GENERAL_REGISTERS(DECLARE_REGISTER)
145#undef DECLARE_REGISTER
146const Register no_reg = {Register::kCode_no_reg};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000147
148
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149struct DoubleRegister {
150 enum Code {
151#define REGISTER_CODE(R) kCode_##R,
152 DOUBLE_REGISTERS(REGISTER_CODE)
153#undef REGISTER_CODE
154 kAfterLast,
155 kCode_no_reg = -1
156 };
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000157
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000158 static const int kMaxNumRegisters = Code::kAfterLast;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 static const int kMaxNumAllocatableRegisters = 6;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161 static DoubleRegister from_code(int code) {
162 DoubleRegister result = {code};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 return result;
164 }
165
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000166 bool IsAllocatable() const;
167 bool is_valid() const { return 0 <= reg_code && reg_code < kMaxNumRegisters; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168
169 int code() const {
170 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000171 return reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172 }
173
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000174 bool is(DoubleRegister reg) const { return reg_code == reg.reg_code; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000175
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000176 const char* ToString();
177
178 int reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179};
180
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000181#define DECLARE_REGISTER(R) \
182 const DoubleRegister R = {DoubleRegister::kCode_##R};
183DOUBLE_REGISTERS(DECLARE_REGISTER)
184#undef DECLARE_REGISTER
185const DoubleRegister no_double_reg = {DoubleRegister::kCode_no_reg};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000187typedef DoubleRegister X87Register;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188
Ben Murdoch097c5b22016-05-18 11:27:45 +0100189// TODO(x87) Define SIMD registers.
190typedef DoubleRegister Simd128Register;
191
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192enum Condition {
193 // any value < 0 is considered no_condition
194 no_condition = -1,
195
196 overflow = 0,
197 no_overflow = 1,
198 below = 2,
199 above_equal = 3,
200 equal = 4,
201 not_equal = 5,
202 below_equal = 6,
203 above = 7,
204 negative = 8,
205 positive = 9,
206 parity_even = 10,
207 parity_odd = 11,
208 less = 12,
209 greater_equal = 13,
210 less_equal = 14,
211 greater = 15,
212
213 // aliases
214 carry = below,
215 not_carry = above_equal,
216 zero = equal,
217 not_zero = not_equal,
218 sign = negative,
219 not_sign = positive
220};
221
222
223// Returns the equivalent of !cc.
224// Negation of the default no_condition (-1) results in a non-default
225// no_condition value (-2). As long as tests for no_condition check
226// for condition < 0, this will work as expected.
227inline Condition NegateCondition(Condition cc) {
228 return static_cast<Condition>(cc ^ 1);
229}
230
231
232// Commute a condition such that {a cond b == b cond' a}.
233inline Condition CommuteCondition(Condition cc) {
234 switch (cc) {
235 case below:
236 return above;
237 case above:
238 return below;
239 case above_equal:
240 return below_equal;
241 case below_equal:
242 return above_equal;
243 case less:
244 return greater;
245 case greater:
246 return less;
247 case greater_equal:
248 return less_equal;
249 case less_equal:
250 return greater_equal;
251 default:
252 return cc;
253 }
254}
255
256
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000257enum RoundingMode {
258 kRoundToNearest = 0x0,
259 kRoundDown = 0x1,
260 kRoundUp = 0x2,
261 kRoundToZero = 0x3
262};
263
264
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000265// -----------------------------------------------------------------------------
266// Machine instruction Immediates
267
268class Immediate BASE_EMBEDDED {
269 public:
270 inline explicit Immediate(int x);
271 inline explicit Immediate(const ExternalReference& ext);
272 inline explicit Immediate(Handle<Object> handle);
273 inline explicit Immediate(Smi* value);
274 inline explicit Immediate(Address addr);
275
276 static Immediate CodeRelativeOffset(Label* label) {
277 return Immediate(label);
278 }
279
280 bool is_zero() const { return x_ == 0 && RelocInfo::IsNone(rmode_); }
281 bool is_int8() const {
282 return -128 <= x_ && x_ < 128 && RelocInfo::IsNone(rmode_);
283 }
284 bool is_int16() const {
285 return -32768 <= x_ && x_ < 32768 && RelocInfo::IsNone(rmode_);
286 }
287
288 private:
289 inline explicit Immediate(Label* value);
290
291 int x_;
292 RelocInfo::Mode rmode_;
293
294 friend class Operand;
295 friend class Assembler;
296 friend class MacroAssembler;
297};
298
299
300// -----------------------------------------------------------------------------
301// Machine instruction Operands
302
303enum ScaleFactor {
304 times_1 = 0,
305 times_2 = 1,
306 times_4 = 2,
307 times_8 = 3,
308 times_int_size = times_4,
309 times_half_pointer_size = times_2,
310 times_pointer_size = times_4,
311 times_twice_pointer_size = times_8
312};
313
314
315class Operand BASE_EMBEDDED {
316 public:
317 // reg
318 INLINE(explicit Operand(Register reg));
319
320 // [disp/r]
321 INLINE(explicit Operand(int32_t disp, RelocInfo::Mode rmode));
322
323 // [disp/r]
324 INLINE(explicit Operand(Immediate imm));
325
326 // [base + disp/r]
327 explicit Operand(Register base, int32_t disp,
328 RelocInfo::Mode rmode = RelocInfo::NONE32);
329
330 // [base + index*scale + disp/r]
331 explicit Operand(Register base,
332 Register index,
333 ScaleFactor scale,
334 int32_t disp,
335 RelocInfo::Mode rmode = RelocInfo::NONE32);
336
337 // [index*scale + disp/r]
338 explicit Operand(Register index,
339 ScaleFactor scale,
340 int32_t disp,
341 RelocInfo::Mode rmode = RelocInfo::NONE32);
342
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000343 static Operand JumpTable(Register index, ScaleFactor scale, Label* table) {
344 return Operand(index, scale, reinterpret_cast<int32_t>(table),
345 RelocInfo::INTERNAL_REFERENCE);
346 }
347
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348 static Operand StaticVariable(const ExternalReference& ext) {
349 return Operand(reinterpret_cast<int32_t>(ext.address()),
350 RelocInfo::EXTERNAL_REFERENCE);
351 }
352
353 static Operand StaticArray(Register index,
354 ScaleFactor scale,
355 const ExternalReference& arr) {
356 return Operand(index, scale, reinterpret_cast<int32_t>(arr.address()),
357 RelocInfo::EXTERNAL_REFERENCE);
358 }
359
360 static Operand ForCell(Handle<Cell> cell) {
361 AllowDeferredHandleDereference embedding_raw_address;
362 return Operand(reinterpret_cast<int32_t>(cell.location()),
363 RelocInfo::CELL);
364 }
365
366 static Operand ForRegisterPlusImmediate(Register base, Immediate imm) {
367 return Operand(base, imm.x_, imm.rmode_);
368 }
369
370 // Returns true if this Operand is a wrapper for the specified register.
371 bool is_reg(Register reg) const;
372
373 // Returns true if this Operand is a wrapper for one register.
374 bool is_reg_only() const;
375
376 // Asserts that this Operand is a wrapper for one register and returns the
377 // register.
378 Register reg() const;
379
380 private:
381 // Set the ModRM byte without an encoded 'reg' register. The
382 // register is encoded later as part of the emit_operand operation.
383 inline void set_modrm(int mod, Register rm);
384
385 inline void set_sib(ScaleFactor scale, Register index, Register base);
386 inline void set_disp8(int8_t disp);
387 inline void set_dispr(int32_t disp, RelocInfo::Mode rmode);
388
389 byte buf_[6];
390 // The number of bytes in buf_.
391 unsigned int len_;
392 // Only valid if len_ > 4.
393 RelocInfo::Mode rmode_;
394
395 friend class Assembler;
396 friend class MacroAssembler;
397};
398
399
400// -----------------------------------------------------------------------------
401// A Displacement describes the 32bit immediate field of an instruction which
402// may be used together with a Label in order to refer to a yet unknown code
403// position. Displacements stored in the instruction stream are used to describe
404// the instruction and to chain a list of instructions using the same Label.
405// A Displacement contains 2 different fields:
406//
407// next field: position of next displacement in the chain (0 = end of list)
408// type field: instruction type
409//
410// A next value of null (0) indicates the end of a chain (note that there can
411// be no displacement at position zero, because there is always at least one
412// instruction byte before the displacement).
413//
414// Displacement _data field layout
415//
416// |31.....2|1......0|
417// [ next | type |
418
419class Displacement BASE_EMBEDDED {
420 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000421 enum Type { UNCONDITIONAL_JUMP, CODE_RELATIVE, OTHER, CODE_ABSOLUTE };
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000422
423 int data() const { return data_; }
424 Type type() const { return TypeField::decode(data_); }
425 void next(Label* L) const {
426 int n = NextField::decode(data_);
427 n > 0 ? L->link_to(n) : L->Unuse();
428 }
429 void link_to(Label* L) { init(L, type()); }
430
431 explicit Displacement(int data) { data_ = data; }
432
433 Displacement(Label* L, Type type) { init(L, type); }
434
435 void print() {
436 PrintF("%s (%x) ", (type() == UNCONDITIONAL_JUMP ? "jmp" : "[other]"),
437 NextField::decode(data_));
438 }
439
440 private:
441 int data_;
442
443 class TypeField: public BitField<Type, 0, 2> {};
444 class NextField: public BitField<int, 2, 32-2> {};
445
446 void init(Label* L, Type type);
447};
448
449
450class Assembler : public AssemblerBase {
451 private:
452 // We check before assembling an instruction that there is sufficient
453 // space to write an instruction and its relocation information.
454 // The relocation writer's position must be kGap bytes above the end of
455 // the generated instructions. This leaves enough space for the
456 // longest possible ia32 instruction, 15 bytes, and the longest possible
457 // relocation information encoding, RelocInfoWriter::kMaxLength == 16.
458 // (There is a 15 byte limit on ia32 instruction length that rules out some
459 // otherwise valid instructions.)
460 // This allows for a single, fast space check per instruction.
461 static const int kGap = 32;
462
463 public:
464 // Create an assembler. Instructions and relocation information are emitted
465 // into a buffer, with the instructions starting from the beginning and the
466 // relocation information starting from the end of the buffer. See CodeDesc
467 // for a detailed comment on the layout (globals.h).
468 //
469 // If the provided buffer is NULL, the assembler allocates and grows its own
470 // buffer, and buffer_size determines the initial buffer size. The buffer is
471 // owned by the assembler and deallocated upon destruction of the assembler.
472 //
473 // If the provided buffer is not NULL, the assembler uses the provided buffer
474 // for code generation and assumes its size to be buffer_size. If the buffer
475 // is too small, a fatal error occurs. No deallocation of the buffer is done
476 // upon destruction of the assembler.
477 // TODO(vitalyr): the assembler does not need an isolate.
478 Assembler(Isolate* isolate, void* buffer, int buffer_size);
479 virtual ~Assembler() { }
480
481 // GetCode emits any pending (non-emitted) code and fills the descriptor
482 // desc. GetCode() is idempotent; it returns the same result if no other
483 // Assembler functions are invoked in between GetCode() calls.
484 void GetCode(CodeDesc* desc);
485
486 // Read/Modify the code target in the branch/call instruction at pc.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000487 inline static Address target_address_at(Address pc, Address constant_pool);
488 inline static void set_target_address_at(
489 Isolate* isolate, Address pc, Address constant_pool, Address target,
490 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000491 static inline Address target_address_at(Address pc, Code* code) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000492 Address constant_pool = code ? code->constant_pool() : NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000493 return target_address_at(pc, constant_pool);
494 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000495 static inline void set_target_address_at(
496 Isolate* isolate, Address pc, Code* code, Address target,
497 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED) {
498 Address constant_pool = code ? code->constant_pool() : NULL;
499 set_target_address_at(isolate, pc, constant_pool, target);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000500 }
501
502 // Return the code target address at a call site from the return address
503 // of that call in the instruction stream.
504 inline static Address target_address_from_return_address(Address pc);
505
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000506 // This sets the branch destination (which is in the instruction on x86).
507 // This is for calls and branches within generated code.
508 inline static void deserialization_set_special_target_at(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000509 Isolate* isolate, Address instruction_payload, Code* code,
510 Address target) {
511 set_target_address_at(isolate, instruction_payload, code, target);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000512 }
513
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000514 // This sets the internal reference at the pc.
515 inline static void deserialization_set_target_internal_reference_at(
516 Isolate* isolate, Address pc, Address target,
517 RelocInfo::Mode mode = RelocInfo::INTERNAL_REFERENCE);
518
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000519 static const int kSpecialTargetSize = kPointerSize;
520
521 // Distance between the address of the code target in the call instruction
522 // and the return address
523 static const int kCallTargetAddressOffset = kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000524
525 static const int kCallInstructionLength = 5;
526
527 // The debug break slot must be able to contain a call instruction.
528 static const int kDebugBreakSlotLength = kCallInstructionLength;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000529
530 // Distance between start of patched debug break slot and the emitted address
531 // to jump to.
532 static const int kPatchDebugBreakSlotAddressOffset = 1; // JMP imm32.
533
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000534 // One byte opcode for test al, 0xXX.
535 static const byte kTestAlByte = 0xA8;
536 // One byte opcode for nop.
537 static const byte kNopByte = 0x90;
538
539 // One byte opcode for a short unconditional jump.
540 static const byte kJmpShortOpcode = 0xEB;
541 // One byte prefix for a short conditional jump.
542 static const byte kJccShortPrefix = 0x70;
543 static const byte kJncShortOpcode = kJccShortPrefix | not_carry;
544 static const byte kJcShortOpcode = kJccShortPrefix | carry;
545 static const byte kJnzShortOpcode = kJccShortPrefix | not_zero;
546 static const byte kJzShortOpcode = kJccShortPrefix | zero;
547
548
549 // ---------------------------------------------------------------------------
550 // Code generation
551 //
552 // - function names correspond one-to-one to ia32 instruction mnemonics
553 // - unless specified otherwise, instructions operate on 32bit operands
554 // - instructions on 8bit (byte) operands/registers have a trailing '_b'
555 // - instructions on 16bit (word) operands/registers have a trailing '_w'
556 // - naming conflicts with C++ keywords are resolved via a trailing '_'
557
558 // NOTE ON INTERFACE: Currently, the interface is not very consistent
559 // in the sense that some operations (e.g. mov()) can be called in more
560 // the one way to generate the same instruction: The Register argument
561 // can in some cases be replaced with an Operand(Register) argument.
562 // This should be cleaned up and made more orthogonal. The questions
563 // is: should we always use Operands instead of Registers where an
564 // Operand is possible, or should we have a Register (overloaded) form
565 // instead? We must be careful to make sure that the selected instruction
566 // is obvious from the parameters to avoid hard-to-find code generation
567 // bugs.
568
569 // Insert the smallest number of nop instructions
570 // possible to align the pc offset to a multiple
571 // of m. m must be a power of 2.
572 void Align(int m);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000573 // Insert the smallest number of zero bytes possible to align the pc offset
574 // to a mulitple of m. m must be a power of 2 (>= 2).
575 void DataAlign(int m);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000576 void Nop(int bytes = 1);
577 // Aligns code to something that's optimal for a jump target for the platform.
578 void CodeTargetAlign();
579
580 // Stack
581 void pushad();
582 void popad();
583
584 void pushfd();
585 void popfd();
586
587 void push(const Immediate& x);
588 void push_imm32(int32_t imm32);
589 void push(Register src);
590 void push(const Operand& src);
591
592 void pop(Register dst);
593 void pop(const Operand& dst);
594
595 void enter(const Immediate& size);
596 void leave();
597
598 // Moves
599 void mov_b(Register dst, Register src) { mov_b(dst, Operand(src)); }
600 void mov_b(Register dst, const Operand& src);
601 void mov_b(Register dst, int8_t imm8) { mov_b(Operand(dst), imm8); }
602 void mov_b(const Operand& dst, int8_t imm8);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000603 void mov_b(const Operand& dst, const Immediate& src);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000604 void mov_b(const Operand& dst, Register src);
605
606 void mov_w(Register dst, const Operand& src);
607 void mov_w(const Operand& dst, Register src);
608 void mov_w(const Operand& dst, int16_t imm16);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000609 void mov_w(const Operand& dst, const Immediate& src);
610
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000611
612 void mov(Register dst, int32_t imm32);
613 void mov(Register dst, const Immediate& x);
614 void mov(Register dst, Handle<Object> handle);
615 void mov(Register dst, const Operand& src);
616 void mov(Register dst, Register src);
617 void mov(const Operand& dst, const Immediate& x);
618 void mov(const Operand& dst, Handle<Object> handle);
619 void mov(const Operand& dst, Register src);
620
621 void movsx_b(Register dst, Register src) { movsx_b(dst, Operand(src)); }
622 void movsx_b(Register dst, const Operand& src);
623
624 void movsx_w(Register dst, Register src) { movsx_w(dst, Operand(src)); }
625 void movsx_w(Register dst, const Operand& src);
626
627 void movzx_b(Register dst, Register src) { movzx_b(dst, Operand(src)); }
628 void movzx_b(Register dst, const Operand& src);
629
630 void movzx_w(Register dst, Register src) { movzx_w(dst, Operand(src)); }
631 void movzx_w(Register dst, const Operand& src);
632
633 // Flag management.
634 void cld();
635
636 // Repetitive string instructions.
637 void rep_movs();
638 void rep_stos();
639 void stos();
640
641 // Exchange
642 void xchg(Register dst, Register src);
643 void xchg(Register dst, const Operand& src);
644
645 // Arithmetics
646 void adc(Register dst, int32_t imm32);
647 void adc(Register dst, const Operand& src);
648
649 void add(Register dst, Register src) { add(dst, Operand(src)); }
650 void add(Register dst, const Operand& src);
651 void add(const Operand& dst, Register src);
652 void add(Register dst, const Immediate& imm) { add(Operand(dst), imm); }
653 void add(const Operand& dst, const Immediate& x);
654
655 void and_(Register dst, int32_t imm32);
656 void and_(Register dst, const Immediate& x);
657 void and_(Register dst, Register src) { and_(dst, Operand(src)); }
658 void and_(Register dst, const Operand& src);
659 void and_(const Operand& dst, Register src);
660 void and_(const Operand& dst, const Immediate& x);
661
662 void cmpb(Register reg, int8_t imm8) { cmpb(Operand(reg), imm8); }
663 void cmpb(const Operand& op, int8_t imm8);
664 void cmpb(Register reg, const Operand& op);
665 void cmpb(const Operand& op, Register reg);
666 void cmpb_al(const Operand& op);
667 void cmpw_ax(const Operand& op);
668 void cmpw(const Operand& op, Immediate imm16);
669 void cmp(Register reg, int32_t imm32);
670 void cmp(Register reg, Handle<Object> handle);
671 void cmp(Register reg0, Register reg1) { cmp(reg0, Operand(reg1)); }
672 void cmp(Register reg, const Operand& op);
673 void cmp(Register reg, const Immediate& imm) { cmp(Operand(reg), imm); }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100674 void cmp(const Operand& op, Register reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000675 void cmp(const Operand& op, const Immediate& imm);
676 void cmp(const Operand& op, Handle<Object> handle);
677
678 void dec_b(Register dst);
679 void dec_b(const Operand& dst);
680
681 void dec(Register dst);
682 void dec(const Operand& dst);
683
684 void cdq();
685
686 void idiv(Register src) { idiv(Operand(src)); }
687 void idiv(const Operand& src);
688 void div(Register src) { div(Operand(src)); }
689 void div(const Operand& src);
690
691 // Signed multiply instructions.
692 void imul(Register src); // edx:eax = eax * src.
693 void imul(Register dst, Register src) { imul(dst, Operand(src)); }
694 void imul(Register dst, const Operand& src); // dst = dst * src.
695 void imul(Register dst, Register src, int32_t imm32); // dst = src * imm32.
696 void imul(Register dst, const Operand& src, int32_t imm32);
697
698 void inc(Register dst);
699 void inc(const Operand& dst);
700
701 void lea(Register dst, const Operand& src);
702
703 // Unsigned multiply instruction.
704 void mul(Register src); // edx:eax = eax * reg.
705
706 void neg(Register dst);
707 void neg(const Operand& dst);
708
709 void not_(Register dst);
710 void not_(const Operand& dst);
711
712 void or_(Register dst, int32_t imm32);
713 void or_(Register dst, Register src) { or_(dst, Operand(src)); }
714 void or_(Register dst, const Operand& src);
715 void or_(const Operand& dst, Register src);
716 void or_(Register dst, const Immediate& imm) { or_(Operand(dst), imm); }
717 void or_(const Operand& dst, const Immediate& x);
718
719 void rcl(Register dst, uint8_t imm8);
720 void rcr(Register dst, uint8_t imm8);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400721
722 void ror(Register dst, uint8_t imm8) { ror(Operand(dst), imm8); }
723 void ror(const Operand& dst, uint8_t imm8);
724 void ror_cl(Register dst) { ror_cl(Operand(dst)); }
725 void ror_cl(const Operand& dst);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000726
727 void sar(Register dst, uint8_t imm8) { sar(Operand(dst), imm8); }
728 void sar(const Operand& dst, uint8_t imm8);
729 void sar_cl(Register dst) { sar_cl(Operand(dst)); }
730 void sar_cl(const Operand& dst);
731
732 void sbb(Register dst, const Operand& src);
733
734 void shld(Register dst, Register src) { shld(dst, Operand(src)); }
735 void shld(Register dst, const Operand& src);
736
737 void shl(Register dst, uint8_t imm8) { shl(Operand(dst), imm8); }
738 void shl(const Operand& dst, uint8_t imm8);
739 void shl_cl(Register dst) { shl_cl(Operand(dst)); }
740 void shl_cl(const Operand& dst);
741
742 void shrd(Register dst, Register src) { shrd(dst, Operand(src)); }
743 void shrd(Register dst, const Operand& src);
744
745 void shr(Register dst, uint8_t imm8) { shr(Operand(dst), imm8); }
746 void shr(const Operand& dst, uint8_t imm8);
747 void shr_cl(Register dst) { shr_cl(Operand(dst)); }
748 void shr_cl(const Operand& dst);
749
750 void sub(Register dst, const Immediate& imm) { sub(Operand(dst), imm); }
751 void sub(const Operand& dst, const Immediate& x);
752 void sub(Register dst, Register src) { sub(dst, Operand(src)); }
753 void sub(Register dst, const Operand& src);
754 void sub(const Operand& dst, Register src);
755
756 void test(Register reg, const Immediate& imm);
757 void test(Register reg0, Register reg1) { test(reg0, Operand(reg1)); }
758 void test(Register reg, const Operand& op);
759 void test_b(Register reg, const Operand& op);
760 void test(const Operand& op, const Immediate& imm);
761 void test_b(Register reg, uint8_t imm8);
762 void test_b(const Operand& op, uint8_t imm8);
763
764 void xor_(Register dst, int32_t imm32);
765 void xor_(Register dst, Register src) { xor_(dst, Operand(src)); }
766 void xor_(Register dst, const Operand& src);
767 void xor_(const Operand& dst, Register src);
768 void xor_(Register dst, const Immediate& imm) { xor_(Operand(dst), imm); }
769 void xor_(const Operand& dst, const Immediate& x);
770
771 // Bit operations.
772 void bt(const Operand& dst, Register src);
773 void bts(Register dst, Register src) { bts(Operand(dst), src); }
774 void bts(const Operand& dst, Register src);
775 void bsr(Register dst, Register src) { bsr(dst, Operand(src)); }
776 void bsr(Register dst, const Operand& src);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000777 void bsf(Register dst, Register src) { bsf(dst, Operand(src)); }
778 void bsf(Register dst, const Operand& src);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000779
780 // Miscellaneous
781 void hlt();
782 void int3();
783 void nop();
784 void ret(int imm16);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000785 void ud2();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000786
787 // Label operations & relative jumps (PPUM Appendix D)
788 //
789 // Takes a branch opcode (cc) and a label (L) and generates
790 // either a backward branch or a forward branch and links it
791 // to the label fixup chain. Usage:
792 //
793 // Label L; // unbound label
794 // j(cc, &L); // forward branch to unbound label
795 // bind(&L); // bind label to the current pc
796 // j(cc, &L); // backward branch to bound label
797 // bind(&L); // illegal: a label may be bound only once
798 //
799 // Note: The same Label can be used for forward and backward branches
800 // but it may be bound only once.
801
802 void bind(Label* L); // binds an unbound label L to the current code position
803
804 // Calls
805 void call(Label* L);
806 void call(byte* entry, RelocInfo::Mode rmode);
807 int CallSize(const Operand& adr);
808 void call(Register reg) { call(Operand(reg)); }
809 void call(const Operand& adr);
810 int CallSize(Handle<Code> code, RelocInfo::Mode mode);
811 void call(Handle<Code> code,
812 RelocInfo::Mode rmode,
813 TypeFeedbackId id = TypeFeedbackId::None());
814
815 // Jumps
816 // unconditional jump to L
817 void jmp(Label* L, Label::Distance distance = Label::kFar);
818 void jmp(byte* entry, RelocInfo::Mode rmode);
819 void jmp(Register reg) { jmp(Operand(reg)); }
820 void jmp(const Operand& adr);
821 void jmp(Handle<Code> code, RelocInfo::Mode rmode);
822
823 // Conditional jumps
824 void j(Condition cc,
825 Label* L,
826 Label::Distance distance = Label::kFar);
827 void j(Condition cc, byte* entry, RelocInfo::Mode rmode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000828 void j(Condition cc, Handle<Code> code,
829 RelocInfo::Mode rmode = RelocInfo::CODE_TARGET);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000830
831 // Floating-point operations
832 void fld(int i);
833 void fstp(int i);
834
835 void fld1();
836 void fldz();
837 void fldpi();
838 void fldln2();
839
840 void fld_s(const Operand& adr);
841 void fld_d(const Operand& adr);
842
843 void fstp_s(const Operand& adr);
844 void fst_s(const Operand& adr);
845 void fstp_d(const Operand& adr);
846 void fst_d(const Operand& adr);
847
848 void fild_s(const Operand& adr);
849 void fild_d(const Operand& adr);
850
851 void fist_s(const Operand& adr);
852
853 void fistp_s(const Operand& adr);
854 void fistp_d(const Operand& adr);
855
856 // The fisttp instructions require SSE3.
857 void fisttp_s(const Operand& adr);
858 void fisttp_d(const Operand& adr);
859
860 void fabs();
861 void fchs();
862 void fsqrt();
863 void fcos();
864 void fsin();
865 void fptan();
866 void fyl2x();
867 void f2xm1();
868 void fscale();
869 void fninit();
870
871 void fadd(int i);
872 void fadd_i(int i);
873 void fadd_d(const Operand& adr);
874 void fsub(int i);
875 void fsub_i(int i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000876 void fsub_d(const Operand& adr);
877 void fsubr_d(const Operand& adr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000878 void fmul(int i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000879 void fmul_d(const Operand& adr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000880 void fmul_i(int i);
881 void fdiv(int i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000882 void fdiv_d(const Operand& adr);
883 void fdivr_d(const Operand& adr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000884 void fdiv_i(int i);
885
886 void fisub_s(const Operand& adr);
887
888 void faddp(int i = 1);
889 void fsubp(int i = 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000890 void fsubr(int i = 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000891 void fsubrp(int i = 1);
892 void fmulp(int i = 1);
893 void fdivp(int i = 1);
894 void fprem();
895 void fprem1();
896
897 void fxch(int i = 1);
898 void fincstp();
899 void ffree(int i = 0);
900
901 void ftst();
902 void fxam();
903 void fucomp(int i);
904 void fucompp();
905 void fucomi(int i);
906 void fucomip();
907 void fcompp();
908 void fnstsw_ax();
909 void fldcw(const Operand& adr);
910 void fnstcw(const Operand& adr);
911 void fwait();
912 void fnclex();
913 void fnsave(const Operand& adr);
914 void frstor(const Operand& adr);
915
916 void frndint();
917
918 void sahf();
919 void setcc(Condition cc, Register reg);
920
921 void cpuid();
922
923 // TODO(lrn): Need SFENCE for movnt?
924
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000925 // Check the code size generated from label to here.
926 int SizeOfCodeGeneratedSince(Label* label) {
927 return pc_offset() - label->pos();
928 }
929
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000930 // Mark generator continuation.
931 void RecordGeneratorContinuation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000932
933 // Mark address of a debug break slot.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000934 void RecordDebugBreakSlot(RelocInfo::Mode mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000935
936 // Record a comment relocation entry that can be used by a disassembler.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000937 // Use --code-comments to enable.
938 void RecordComment(const char* msg);
939
940 // Record a deoptimization reason that can be used by a log or cpu profiler.
941 // Use --trace-deopt to enable.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100942 void RecordDeoptReason(const int reason, int raw_position);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000943
944 // Writes a single byte or word of data in the code stream. Used for
945 // inline tables, e.g., jump-tables.
946 void db(uint8_t data);
947 void dd(uint32_t data);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000948 void dq(uint64_t data);
949 void dp(uintptr_t data) { dd(data); }
950 void dd(Label* label);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000951
952 // Check if there is less than kGap bytes available in the buffer.
953 // If this is the case, we need to grow the buffer before emitting
954 // an instruction or relocation information.
955 inline bool buffer_overflow() const {
956 return pc_ >= reloc_info_writer.pos() - kGap;
957 }
958
959 // Get the number of bytes available in the buffer.
960 inline int available_space() const { return reloc_info_writer.pos() - pc_; }
961
962 static bool IsNop(Address addr);
963
964 PositionsRecorder* positions_recorder() { return &positions_recorder_; }
965
966 int relocation_writer_size() {
967 return (buffer_ + buffer_size_) - reloc_info_writer.pos();
968 }
969
970 // Avoid overflows for displacements etc.
971 static const int kMaximalBufferSize = 512*MB;
972
973 byte byte_at(int pos) { return buffer_[pos]; }
974 void set_byte_at(int pos, byte value) { buffer_[pos] = value; }
975
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000976 void PatchConstantPoolAccessInstruction(int pc_offset, int offset,
977 ConstantPoolEntry::Access access,
978 ConstantPoolEntry::Type type) {
979 // No embedded constant pool support.
980 UNREACHABLE();
981 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000982
983 protected:
984 byte* addr_at(int pos) { return buffer_ + pos; }
985
986
987 private:
988 uint32_t long_at(int pos) {
989 return *reinterpret_cast<uint32_t*>(addr_at(pos));
990 }
991 void long_at_put(int pos, uint32_t x) {
992 *reinterpret_cast<uint32_t*>(addr_at(pos)) = x;
993 }
994
995 // code emission
996 void GrowBuffer();
997 inline void emit(uint32_t x);
998 inline void emit(Handle<Object> handle);
999 inline void emit(uint32_t x,
1000 RelocInfo::Mode rmode,
1001 TypeFeedbackId id = TypeFeedbackId::None());
1002 inline void emit(Handle<Code> code,
1003 RelocInfo::Mode rmode,
1004 TypeFeedbackId id = TypeFeedbackId::None());
1005 inline void emit(const Immediate& x);
1006 inline void emit_w(const Immediate& x);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001007 inline void emit_q(uint64_t x);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001008
1009 // Emit the code-object-relative offset of the label's position
1010 inline void emit_code_relative_offset(Label* label);
1011
1012 // instruction generation
1013 void emit_arith_b(int op1, int op2, Register dst, int imm8);
1014
1015 // Emit a basic arithmetic instruction (i.e. first byte of the family is 0x81)
1016 // with a given destination expression and an immediate operand. It attempts
1017 // to use the shortest encoding possible.
1018 // sel specifies the /n in the modrm byte (see the Intel PRM).
1019 void emit_arith(int sel, Operand dst, const Immediate& x);
1020
1021 void emit_operand(Register reg, const Operand& adr);
1022
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001023 void emit_label(Label* label);
1024
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001025 void emit_farith(int b1, int b2, int i);
1026
1027 // labels
1028 void print(Label* L);
1029 void bind_to(Label* L, int pos);
1030
1031 // displacements
1032 inline Displacement disp_at(Label* L);
1033 inline void disp_at_put(Label* L, Displacement disp);
1034 inline void emit_disp(Label* L, Displacement::Type type);
1035 inline void emit_near_disp(Label* L);
1036
1037 // record reloc info for current pc_
1038 void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
1039
1040 friend class CodePatcher;
1041 friend class EnsureSpace;
1042
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001043 // Internal reference positions, required for (potential) patching in
1044 // GrowBuffer(); contains only those internal references whose labels
1045 // are already bound.
1046 std::deque<int> internal_reference_positions_;
1047
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001048 // code generation
1049 RelocInfoWriter reloc_info_writer;
1050
1051 PositionsRecorder positions_recorder_;
1052 friend class PositionsRecorder;
1053};
1054
1055
1056// Helper class that ensures that there is enough space for generating
1057// instructions and relocation information. The constructor makes
1058// sure that there is enough space and (in debug mode) the destructor
1059// checks that we did not generate too much.
1060class EnsureSpace BASE_EMBEDDED {
1061 public:
1062 explicit EnsureSpace(Assembler* assembler) : assembler_(assembler) {
1063 if (assembler_->buffer_overflow()) assembler_->GrowBuffer();
1064#ifdef DEBUG
1065 space_before_ = assembler_->available_space();
1066#endif
1067 }
1068
1069#ifdef DEBUG
1070 ~EnsureSpace() {
1071 int bytes_generated = space_before_ - assembler_->available_space();
1072 DCHECK(bytes_generated < assembler_->kGap);
1073 }
1074#endif
1075
1076 private:
1077 Assembler* assembler_;
1078#ifdef DEBUG
1079 int space_before_;
1080#endif
1081};
1082
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001083} // namespace internal
1084} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001085
1086#endif // V8_X87_ASSEMBLER_X87_H_