blob: da27fd09f5c03226eed4c70b44508bacbca340a7 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +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 2006-2008 the V8 project authors. All rights reserved.
34
35// A light-weight IA32 Assembler.
36
37#ifndef V8_IA32_ASSEMBLER_IA32_H_
38#define V8_IA32_ASSEMBLER_IA32_H_
39
Steve Blockd0582a62009-12-15 09:54:21 +000040#include "serialize.h"
41
Steve Blocka7e24c12009-10-30 11:49:00 +000042namespace v8 {
43namespace internal {
44
45// CPU Registers.
46//
47// 1) We would prefer to use an enum, but enum values are assignment-
48// compatible with int, which has caused code-generation bugs.
49//
50// 2) We would prefer to use a class instead of a struct but we don't like
51// the register initialization to depend on the particular initialization
52// order (which appears to be different on OS X, Linux, and Windows for the
53// installed versions of C++ we tried). Using a struct permits C-style
54// "initialization". Also, the Register objects cannot be const as this
55// forces initialization stubs in MSVC, making us dependent on initialization
56// order.
57//
58// 3) By not using an enum, we are possibly preventing the compiler from
59// doing certain constant folds, which may significantly reduce the
60// code generated for some assembly instructions (because they boil down
61// to a few constants). If this is a problem, we could change the code
62// such that we use an enum in optimized mode, and the struct in debug
63// mode. This way we get the compile-time error checking in debug mode
64// and best performance in optimized code.
65//
66struct Register {
67 bool is_valid() const { return 0 <= code_ && code_ < 8; }
68 bool is(Register reg) const { return code_ == reg.code_; }
69 // eax, ebx, ecx and edx are byte registers, the rest are not.
70 bool is_byte_register() const { return code_ <= 3; }
71 int code() const {
72 ASSERT(is_valid());
73 return code_;
74 }
75 int bit() const {
76 ASSERT(is_valid());
77 return 1 << code_;
78 }
79
80 // (unfortunately we can't make this private in a struct)
81 int code_;
82};
83
84const Register eax = { 0 };
85const Register ecx = { 1 };
86const Register edx = { 2 };
87const Register ebx = { 3 };
88const Register esp = { 4 };
89const Register ebp = { 5 };
90const Register esi = { 6 };
91const Register edi = { 7 };
92const Register no_reg = { -1 };
93
94
95struct XMMRegister {
96 bool is_valid() const { return 0 <= code_ && code_ < 2; } // currently
97 int code() const {
98 ASSERT(is_valid());
99 return code_;
100 }
101
102 int code_;
103};
104
105const XMMRegister xmm0 = { 0 };
106const XMMRegister xmm1 = { 1 };
107const XMMRegister xmm2 = { 2 };
108const XMMRegister xmm3 = { 3 };
109const XMMRegister xmm4 = { 4 };
110const XMMRegister xmm5 = { 5 };
111const XMMRegister xmm6 = { 6 };
112const XMMRegister xmm7 = { 7 };
113
114enum Condition {
115 // any value < 0 is considered no_condition
116 no_condition = -1,
117
118 overflow = 0,
119 no_overflow = 1,
120 below = 2,
121 above_equal = 3,
122 equal = 4,
123 not_equal = 5,
124 below_equal = 6,
125 above = 7,
126 negative = 8,
127 positive = 9,
128 parity_even = 10,
129 parity_odd = 11,
130 less = 12,
131 greater_equal = 13,
132 less_equal = 14,
133 greater = 15,
134
135 // aliases
136 carry = below,
137 not_carry = above_equal,
138 zero = equal,
139 not_zero = not_equal,
140 sign = negative,
141 not_sign = positive
142};
143
144
145// Returns the equivalent of !cc.
146// Negation of the default no_condition (-1) results in a non-default
147// no_condition value (-2). As long as tests for no_condition check
148// for condition < 0, this will work as expected.
149inline Condition NegateCondition(Condition cc);
150
151// Corresponds to transposing the operands of a comparison.
152inline Condition ReverseCondition(Condition cc) {
153 switch (cc) {
154 case below:
155 return above;
156 case above:
157 return below;
158 case above_equal:
159 return below_equal;
160 case below_equal:
161 return above_equal;
162 case less:
163 return greater;
164 case greater:
165 return less;
166 case greater_equal:
167 return less_equal;
168 case less_equal:
169 return greater_equal;
170 default:
171 return cc;
172 };
173}
174
175enum Hint {
176 no_hint = 0,
177 not_taken = 0x2e,
178 taken = 0x3e
179};
180
181// The result of negating a hint is as if the corresponding condition
182// were negated by NegateCondition. That is, no_hint is mapped to
183// itself and not_taken and taken are mapped to each other.
184inline Hint NegateHint(Hint hint) {
185 return (hint == no_hint)
186 ? no_hint
187 : ((hint == not_taken) ? taken : not_taken);
188}
189
190
191// -----------------------------------------------------------------------------
192// Machine instruction Immediates
193
194class Immediate BASE_EMBEDDED {
195 public:
196 inline explicit Immediate(int x);
197 inline explicit Immediate(const char* s);
198 inline explicit Immediate(const ExternalReference& ext);
199 inline explicit Immediate(Handle<Object> handle);
200 inline explicit Immediate(Smi* value);
201
202 static Immediate CodeRelativeOffset(Label* label) {
203 return Immediate(label);
204 }
205
206 bool is_zero() const { return x_ == 0 && rmode_ == RelocInfo::NONE; }
207 bool is_int8() const {
208 return -128 <= x_ && x_ < 128 && rmode_ == RelocInfo::NONE;
209 }
210 bool is_int16() const {
211 return -32768 <= x_ && x_ < 32768 && rmode_ == RelocInfo::NONE;
212 }
213
214 private:
215 inline explicit Immediate(Label* value);
216
217 int x_;
218 RelocInfo::Mode rmode_;
219
220 friend class Assembler;
221};
222
223
224// -----------------------------------------------------------------------------
225// Machine instruction Operands
226
227enum ScaleFactor {
228 times_1 = 0,
229 times_2 = 1,
230 times_4 = 2,
231 times_8 = 3,
232 times_pointer_size = times_4,
233 times_half_pointer_size = times_2
234};
235
236
237class Operand BASE_EMBEDDED {
238 public:
239 // reg
240 INLINE(explicit Operand(Register reg));
241
242 // [disp/r]
243 INLINE(explicit Operand(int32_t disp, RelocInfo::Mode rmode));
244 // disp only must always be relocated
245
246 // [base + disp/r]
247 explicit Operand(Register base, int32_t disp,
248 RelocInfo::Mode rmode = RelocInfo::NONE);
249
250 // [base + index*scale + disp/r]
251 explicit Operand(Register base,
252 Register index,
253 ScaleFactor scale,
254 int32_t disp,
255 RelocInfo::Mode rmode = RelocInfo::NONE);
256
257 // [index*scale + disp/r]
258 explicit Operand(Register index,
259 ScaleFactor scale,
260 int32_t disp,
261 RelocInfo::Mode rmode = RelocInfo::NONE);
262
263 static Operand StaticVariable(const ExternalReference& ext) {
264 return Operand(reinterpret_cast<int32_t>(ext.address()),
265 RelocInfo::EXTERNAL_REFERENCE);
266 }
267
268 static Operand StaticArray(Register index,
269 ScaleFactor scale,
270 const ExternalReference& arr) {
271 return Operand(index, scale, reinterpret_cast<int32_t>(arr.address()),
272 RelocInfo::EXTERNAL_REFERENCE);
273 }
274
275 // Returns true if this Operand is a wrapper for the specified register.
276 bool is_reg(Register reg) const;
277
278 private:
279 byte buf_[6];
280 // The number of bytes in buf_.
281 unsigned int len_;
282 // Only valid if len_ > 4.
283 RelocInfo::Mode rmode_;
284
285 // Set the ModRM byte without an encoded 'reg' register. The
286 // register is encoded later as part of the emit_operand operation.
287 inline void set_modrm(int mod, Register rm);
288
289 inline void set_sib(ScaleFactor scale, Register index, Register base);
290 inline void set_disp8(int8_t disp);
291 inline void set_dispr(int32_t disp, RelocInfo::Mode rmode);
292
293 friend class Assembler;
294};
295
296
297// -----------------------------------------------------------------------------
298// A Displacement describes the 32bit immediate field of an instruction which
299// may be used together with a Label in order to refer to a yet unknown code
300// position. Displacements stored in the instruction stream are used to describe
301// the instruction and to chain a list of instructions using the same Label.
302// A Displacement contains 2 different fields:
303//
304// next field: position of next displacement in the chain (0 = end of list)
305// type field: instruction type
306//
307// A next value of null (0) indicates the end of a chain (note that there can
308// be no displacement at position zero, because there is always at least one
309// instruction byte before the displacement).
310//
311// Displacement _data field layout
312//
313// |31.....2|1......0|
314// [ next | type |
315
316class Displacement BASE_EMBEDDED {
317 public:
318 enum Type {
319 UNCONDITIONAL_JUMP,
320 CODE_RELATIVE,
321 OTHER
322 };
323
324 int data() const { return data_; }
325 Type type() const { return TypeField::decode(data_); }
326 void next(Label* L) const {
327 int n = NextField::decode(data_);
328 n > 0 ? L->link_to(n) : L->Unuse();
329 }
330 void link_to(Label* L) { init(L, type()); }
331
332 explicit Displacement(int data) { data_ = data; }
333
334 Displacement(Label* L, Type type) { init(L, type); }
335
336 void print() {
337 PrintF("%s (%x) ", (type() == UNCONDITIONAL_JUMP ? "jmp" : "[other]"),
338 NextField::decode(data_));
339 }
340
341 private:
342 int data_;
343
344 class TypeField: public BitField<Type, 0, 2> {};
345 class NextField: public BitField<int, 2, 32-2> {};
346
347 void init(Label* L, Type type);
348};
349
350
351
352// CpuFeatures keeps track of which features are supported by the target CPU.
353// Supported features must be enabled by a Scope before use.
354// Example:
355// if (CpuFeatures::IsSupported(SSE2)) {
356// CpuFeatures::Scope fscope(SSE2);
357// // Generate SSE2 floating point code.
358// } else {
359// // Generate standard x87 floating point code.
360// }
361class CpuFeatures : public AllStatic {
362 public:
Steve Blocka7e24c12009-10-30 11:49:00 +0000363 // Detect features of the target CPU. Set safe defaults if the serializer
364 // is enabled (snapshots must be portable).
365 static void Probe();
366 // Check whether a feature is supported by the target CPU.
Steve Blockd0582a62009-12-15 09:54:21 +0000367 static bool IsSupported(CpuFeature f) {
Steve Block3ce2e202009-11-05 08:53:23 +0000368 if (f == SSE2 && !FLAG_enable_sse2) return false;
369 if (f == SSE3 && !FLAG_enable_sse3) return false;
370 if (f == CMOV && !FLAG_enable_cmov) return false;
371 if (f == RDTSC && !FLAG_enable_rdtsc) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000372 return (supported_ & (static_cast<uint64_t>(1) << f)) != 0;
373 }
374 // Check whether a feature is currently enabled.
Steve Blockd0582a62009-12-15 09:54:21 +0000375 static bool IsEnabled(CpuFeature f) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000376 return (enabled_ & (static_cast<uint64_t>(1) << f)) != 0;
377 }
378 // Enable a specified feature within a scope.
379 class Scope BASE_EMBEDDED {
380#ifdef DEBUG
381 public:
Steve Blockd0582a62009-12-15 09:54:21 +0000382 explicit Scope(CpuFeature f) {
383 uint64_t mask = static_cast<uint64_t>(1) << f;
Steve Blocka7e24c12009-10-30 11:49:00 +0000384 ASSERT(CpuFeatures::IsSupported(f));
Steve Blockd0582a62009-12-15 09:54:21 +0000385 ASSERT(!Serializer::enabled() || (found_by_runtime_probing_ & mask) == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000386 old_enabled_ = CpuFeatures::enabled_;
Steve Blockd0582a62009-12-15 09:54:21 +0000387 CpuFeatures::enabled_ |= mask;
Steve Blocka7e24c12009-10-30 11:49:00 +0000388 }
389 ~Scope() { CpuFeatures::enabled_ = old_enabled_; }
390 private:
391 uint64_t old_enabled_;
392#else
393 public:
Steve Blockd0582a62009-12-15 09:54:21 +0000394 explicit Scope(CpuFeature f) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000395#endif
396 };
397 private:
398 static uint64_t supported_;
399 static uint64_t enabled_;
Steve Blockd0582a62009-12-15 09:54:21 +0000400 static uint64_t found_by_runtime_probing_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000401};
402
403
404class Assembler : public Malloced {
405 private:
406 // We check before assembling an instruction that there is sufficient
407 // space to write an instruction and its relocation information.
408 // The relocation writer's position must be kGap bytes above the end of
409 // the generated instructions. This leaves enough space for the
410 // longest possible ia32 instruction, 15 bytes, and the longest possible
411 // relocation information encoding, RelocInfoWriter::kMaxLength == 16.
412 // (There is a 15 byte limit on ia32 instruction length that rules out some
413 // otherwise valid instructions.)
414 // This allows for a single, fast space check per instruction.
415 static const int kGap = 32;
416
417 public:
418 // Create an assembler. Instructions and relocation information are emitted
419 // into a buffer, with the instructions starting from the beginning and the
420 // relocation information starting from the end of the buffer. See CodeDesc
421 // for a detailed comment on the layout (globals.h).
422 //
423 // If the provided buffer is NULL, the assembler allocates and grows its own
424 // buffer, and buffer_size determines the initial buffer size. The buffer is
425 // owned by the assembler and deallocated upon destruction of the assembler.
426 //
427 // If the provided buffer is not NULL, the assembler uses the provided buffer
428 // for code generation and assumes its size to be buffer_size. If the buffer
429 // is too small, a fatal error occurs. No deallocation of the buffer is done
430 // upon destruction of the assembler.
431 Assembler(void* buffer, int buffer_size);
432 ~Assembler();
433
434 // GetCode emits any pending (non-emitted) code and fills the descriptor
435 // desc. GetCode() is idempotent; it returns the same result if no other
436 // Assembler functions are invoked in between GetCode() calls.
437 void GetCode(CodeDesc* desc);
438
439 // Read/Modify the code target in the branch/call instruction at pc.
440 inline static Address target_address_at(Address pc);
441 inline static void set_target_address_at(Address pc, Address target);
442
Steve Blockd0582a62009-12-15 09:54:21 +0000443 // This sets the branch destination (which is in the instruction on x86).
444 // This is for calls and branches within generated code.
445 inline static void set_target_at(Address instruction_payload,
446 Address target) {
447 set_target_address_at(instruction_payload, target);
448 }
449
450 // This sets the branch destination (which is in the instruction on x86).
451 // This is for calls and branches to runtime code.
452 inline static void set_external_target_at(Address instruction_payload,
453 Address target) {
454 set_target_address_at(instruction_payload, target);
455 }
456
457 static const int kCallTargetSize = kPointerSize;
458 static const int kExternalTargetSize = kPointerSize;
459
Steve Blocka7e24c12009-10-30 11:49:00 +0000460 // Distance between the address of the code target in the call instruction
461 // and the return address
462 static const int kCallTargetAddressOffset = kPointerSize;
463 // Distance between start of patched return sequence and the emitted address
464 // to jump to.
465 static const int kPatchReturnSequenceAddressOffset = 1; // JMP imm32.
466
Steve Blockd0582a62009-12-15 09:54:21 +0000467 static const int kCallInstructionLength = 5;
468 static const int kJSReturnSequenceLength = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +0000469
470 // ---------------------------------------------------------------------------
471 // Code generation
472 //
473 // - function names correspond one-to-one to ia32 instruction mnemonics
474 // - unless specified otherwise, instructions operate on 32bit operands
475 // - instructions on 8bit (byte) operands/registers have a trailing '_b'
476 // - instructions on 16bit (word) operands/registers have a trailing '_w'
477 // - naming conflicts with C++ keywords are resolved via a trailing '_'
478
479 // NOTE ON INTERFACE: Currently, the interface is not very consistent
480 // in the sense that some operations (e.g. mov()) can be called in more
481 // the one way to generate the same instruction: The Register argument
482 // can in some cases be replaced with an Operand(Register) argument.
483 // This should be cleaned up and made more orthogonal. The questions
484 // is: should we always use Operands instead of Registers where an
485 // Operand is possible, or should we have a Register (overloaded) form
486 // instead? We must be careful to make sure that the selected instruction
487 // is obvious from the parameters to avoid hard-to-find code generation
488 // bugs.
489
490 // Insert the smallest number of nop instructions
491 // possible to align the pc offset to a multiple
492 // of m. m must be a power of 2.
493 void Align(int m);
494
495 // Stack
496 void pushad();
497 void popad();
498
499 void pushfd();
500 void popfd();
501
502 void push(const Immediate& x);
503 void push(Register src);
504 void push(const Operand& src);
505 void push(Label* label, RelocInfo::Mode relocation_mode);
506
507 void pop(Register dst);
508 void pop(const Operand& dst);
509
510 void enter(const Immediate& size);
511 void leave();
512
513 // Moves
514 void mov_b(Register dst, const Operand& src);
515 void mov_b(const Operand& dst, int8_t imm8);
516 void mov_b(const Operand& dst, Register src);
517
518 void mov_w(Register dst, const Operand& src);
519 void mov_w(const Operand& dst, Register src);
520
521 void mov(Register dst, int32_t imm32);
522 void mov(Register dst, const Immediate& x);
523 void mov(Register dst, Handle<Object> handle);
524 void mov(Register dst, const Operand& src);
525 void mov(Register dst, Register src);
526 void mov(const Operand& dst, const Immediate& x);
527 void mov(const Operand& dst, Handle<Object> handle);
528 void mov(const Operand& dst, Register src);
529
530 void movsx_b(Register dst, const Operand& src);
531
532 void movsx_w(Register dst, const Operand& src);
533
534 void movzx_b(Register dst, const Operand& src);
535
536 void movzx_w(Register dst, const Operand& src);
537
538 // Conditional moves
539 void cmov(Condition cc, Register dst, int32_t imm32);
540 void cmov(Condition cc, Register dst, Handle<Object> handle);
541 void cmov(Condition cc, Register dst, const Operand& src);
542
Leon Clarkee46be812010-01-19 14:06:41 +0000543 // Repetitive string instructions.
544 void rep_movs();
545
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 // Exchange two registers
547 void xchg(Register dst, Register src);
548
549 // Arithmetics
550 void adc(Register dst, int32_t imm32);
551 void adc(Register dst, const Operand& src);
552
553 void add(Register dst, const Operand& src);
554 void add(const Operand& dst, const Immediate& x);
555
556 void and_(Register dst, int32_t imm32);
557 void and_(Register dst, const Operand& src);
558 void and_(const Operand& src, Register dst);
559 void and_(const Operand& dst, const Immediate& x);
560
561 void cmpb(const Operand& op, int8_t imm8);
Leon Clarked91b9f72010-01-27 17:25:45 +0000562 void cmpb(Register src, const Operand& dst);
563 void cmpb(const Operand& dst, Register src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000564 void cmpb_al(const Operand& op);
565 void cmpw_ax(const Operand& op);
566 void cmpw(const Operand& op, Immediate imm16);
567 void cmp(Register reg, int32_t imm32);
568 void cmp(Register reg, Handle<Object> handle);
569 void cmp(Register reg, const Operand& op);
570 void cmp(const Operand& op, const Immediate& imm);
571 void cmp(const Operand& op, Handle<Object> handle);
572
573 void dec_b(Register dst);
574
575 void dec(Register dst);
576 void dec(const Operand& dst);
577
578 void cdq();
579
580 void idiv(Register src);
581
582 // Signed multiply instructions.
583 void imul(Register src); // edx:eax = eax * src.
584 void imul(Register dst, const Operand& src); // dst = dst * src.
585 void imul(Register dst, Register src, int32_t imm32); // dst = src * imm32.
586
587 void inc(Register dst);
588 void inc(const Operand& dst);
589
590 void lea(Register dst, const Operand& src);
591
592 // Unsigned multiply instruction.
593 void mul(Register src); // edx:eax = eax * reg.
594
595 void neg(Register dst);
596
597 void not_(Register dst);
598
599 void or_(Register dst, int32_t imm32);
600 void or_(Register dst, const Operand& src);
601 void or_(const Operand& dst, Register src);
602 void or_(const Operand& dst, const Immediate& x);
603
604 void rcl(Register dst, uint8_t imm8);
605
606 void sar(Register dst, uint8_t imm8);
Steve Blockd0582a62009-12-15 09:54:21 +0000607 void sar_cl(Register dst);
Steve Blocka7e24c12009-10-30 11:49:00 +0000608
609 void sbb(Register dst, const Operand& src);
610
611 void shld(Register dst, const Operand& src);
612
613 void shl(Register dst, uint8_t imm8);
Steve Blockd0582a62009-12-15 09:54:21 +0000614 void shl_cl(Register dst);
Steve Blocka7e24c12009-10-30 11:49:00 +0000615
616 void shrd(Register dst, const Operand& src);
617
618 void shr(Register dst, uint8_t imm8);
Steve Blocka7e24c12009-10-30 11:49:00 +0000619 void shr_cl(Register dst);
620
Steve Block3ce2e202009-11-05 08:53:23 +0000621 void subb(const Operand& dst, int8_t imm8);
Leon Clarkee46be812010-01-19 14:06:41 +0000622 void subb(Register dst, const Operand& src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000623 void sub(const Operand& dst, const Immediate& x);
624 void sub(Register dst, const Operand& src);
625 void sub(const Operand& dst, Register src);
626
627 void test(Register reg, const Immediate& imm);
628 void test(Register reg, const Operand& op);
Leon Clarkee46be812010-01-19 14:06:41 +0000629 void test_b(Register reg, const Operand& op);
Steve Blocka7e24c12009-10-30 11:49:00 +0000630 void test(const Operand& op, const Immediate& imm);
631
632 void xor_(Register dst, int32_t imm32);
633 void xor_(Register dst, const Operand& src);
634 void xor_(const Operand& src, Register dst);
635 void xor_(const Operand& dst, const Immediate& x);
636
637 // Bit operations.
638 void bt(const Operand& dst, Register src);
639 void bts(const Operand& dst, Register src);
640
641 // Miscellaneous
642 void hlt();
643 void int3();
644 void nop();
645 void rdtsc();
646 void ret(int imm16);
647
648 // Label operations & relative jumps (PPUM Appendix D)
649 //
650 // Takes a branch opcode (cc) and a label (L) and generates
651 // either a backward branch or a forward branch and links it
652 // to the label fixup chain. Usage:
653 //
654 // Label L; // unbound label
655 // j(cc, &L); // forward branch to unbound label
656 // bind(&L); // bind label to the current pc
657 // j(cc, &L); // backward branch to bound label
658 // bind(&L); // illegal: a label may be bound only once
659 //
660 // Note: The same Label can be used for forward and backward branches
661 // but it may be bound only once.
662
663 void bind(Label* L); // binds an unbound label L to the current code position
664
665 // Calls
666 void call(Label* L);
667 void call(byte* entry, RelocInfo::Mode rmode);
668 void call(const Operand& adr);
669 void call(Handle<Code> code, RelocInfo::Mode rmode);
670
671 // Jumps
672 void jmp(Label* L); // unconditional jump to L
673 void jmp(byte* entry, RelocInfo::Mode rmode);
674 void jmp(const Operand& adr);
675 void jmp(Handle<Code> code, RelocInfo::Mode rmode);
676
677 // Conditional jumps
678 void j(Condition cc, Label* L, Hint hint = no_hint);
679 void j(Condition cc, byte* entry, RelocInfo::Mode rmode, Hint hint = no_hint);
680 void j(Condition cc, Handle<Code> code, Hint hint = no_hint);
681
682 // Floating-point operations
683 void fld(int i);
684
685 void fld1();
686 void fldz();
687
688 void fld_s(const Operand& adr);
689 void fld_d(const Operand& adr);
690
691 void fstp_s(const Operand& adr);
692 void fstp_d(const Operand& adr);
693
694 void fild_s(const Operand& adr);
695 void fild_d(const Operand& adr);
696
697 void fist_s(const Operand& adr);
698
699 void fistp_s(const Operand& adr);
700 void fistp_d(const Operand& adr);
701
702 void fisttp_s(const Operand& adr);
Leon Clarkee46be812010-01-19 14:06:41 +0000703 void fisttp_d(const Operand& adr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000704
705 void fabs();
706 void fchs();
707 void fcos();
708 void fsin();
709
710 void fadd(int i);
711 void fsub(int i);
712 void fmul(int i);
713 void fdiv(int i);
714
715 void fisub_s(const Operand& adr);
716
717 void faddp(int i = 1);
718 void fsubp(int i = 1);
719 void fsubrp(int i = 1);
720 void fmulp(int i = 1);
721 void fdivp(int i = 1);
722 void fprem();
723 void fprem1();
724
725 void fxch(int i = 1);
726 void fincstp();
727 void ffree(int i = 0);
728
729 void ftst();
730 void fucomp(int i);
731 void fucompp();
Steve Block3ce2e202009-11-05 08:53:23 +0000732 void fucomi(int i);
733 void fucomip();
Steve Blocka7e24c12009-10-30 11:49:00 +0000734 void fcompp();
735 void fnstsw_ax();
736 void fwait();
737 void fnclex();
738
739 void frndint();
740
741 void sahf();
742 void setcc(Condition cc, Register reg);
743
744 void cpuid();
745
746 // SSE2 instructions
747 void cvttss2si(Register dst, const Operand& src);
748 void cvttsd2si(Register dst, const Operand& src);
749
750 void cvtsi2sd(XMMRegister dst, const Operand& src);
751
752 void addsd(XMMRegister dst, XMMRegister src);
753 void subsd(XMMRegister dst, XMMRegister src);
754 void mulsd(XMMRegister dst, XMMRegister src);
755 void divsd(XMMRegister dst, XMMRegister src);
Leon Clarkee46be812010-01-19 14:06:41 +0000756 void xorpd(XMMRegister dst, XMMRegister src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000757
758 void comisd(XMMRegister dst, XMMRegister src);
759
Leon Clarkee46be812010-01-19 14:06:41 +0000760 void movdqa(XMMRegister dst, const Operand& src);
761 void movdqa(const Operand& dst, XMMRegister src);
762 void movdqu(XMMRegister dst, const Operand& src);
763 void movdqu(const Operand& dst, XMMRegister src);
764
Steve Blocka7e24c12009-10-30 11:49:00 +0000765 // Use either movsd or movlpd.
766 void movdbl(XMMRegister dst, const Operand& src);
767 void movdbl(const Operand& dst, XMMRegister src);
768
769 // Debugging
770 void Print();
771
772 // Check the code size generated from label to here.
773 int SizeOfCodeGeneratedSince(Label* l) { return pc_offset() - l->pos(); }
774
775 // Mark address of the ExitJSFrame code.
776 void RecordJSReturn();
777
778 // Record a comment relocation entry that can be used by a disassembler.
779 // Use --debug_code to enable.
780 void RecordComment(const char* msg);
781
782 void RecordPosition(int pos);
783 void RecordStatementPosition(int pos);
784 void WriteRecordedPositions();
785
786 // Writes a single word of data in the code stream.
787 // Used for inline tables, e.g., jump-tables.
788 void dd(uint32_t data, RelocInfo::Mode reloc_info);
789
790 int pc_offset() const { return pc_ - buffer_; }
791 int current_statement_position() const { return current_statement_position_; }
792 int current_position() const { return current_position_; }
793
794 // Check if there is less than kGap bytes available in the buffer.
795 // If this is the case, we need to grow the buffer before emitting
796 // an instruction or relocation information.
797 inline bool overflow() const { return pc_ >= reloc_info_writer.pos() - kGap; }
798
799 // Get the number of bytes available in the buffer.
800 inline int available_space() const { return reloc_info_writer.pos() - pc_; }
801
802 // Avoid overflows for displacements etc.
803 static const int kMaximalBufferSize = 512*MB;
804 static const int kMinimalBufferSize = 4*KB;
805
806 protected:
807 void movsd(XMMRegister dst, const Operand& src);
808 void movsd(const Operand& dst, XMMRegister src);
809
810 void emit_sse_operand(XMMRegister reg, const Operand& adr);
811 void emit_sse_operand(XMMRegister dst, XMMRegister src);
812
813
814 private:
815 byte* addr_at(int pos) { return buffer_ + pos; }
816 byte byte_at(int pos) { return buffer_[pos]; }
817 uint32_t long_at(int pos) {
818 return *reinterpret_cast<uint32_t*>(addr_at(pos));
819 }
820 void long_at_put(int pos, uint32_t x) {
821 *reinterpret_cast<uint32_t*>(addr_at(pos)) = x;
822 }
823
824 // code emission
825 void GrowBuffer();
826 inline void emit(uint32_t x);
827 inline void emit(Handle<Object> handle);
828 inline void emit(uint32_t x, RelocInfo::Mode rmode);
829 inline void emit(const Immediate& x);
830 inline void emit_w(const Immediate& x);
831
832 // Emit the code-object-relative offset of the label's position
833 inline void emit_code_relative_offset(Label* label);
834
835 // instruction generation
836 void emit_arith_b(int op1, int op2, Register dst, int imm8);
837
838 // Emit a basic arithmetic instruction (i.e. first byte of the family is 0x81)
839 // with a given destination expression and an immediate operand. It attempts
840 // to use the shortest encoding possible.
841 // sel specifies the /n in the modrm byte (see the Intel PRM).
842 void emit_arith(int sel, Operand dst, const Immediate& x);
843
844 void emit_operand(Register reg, const Operand& adr);
845
846 void emit_farith(int b1, int b2, int i);
847
848 // labels
849 void print(Label* L);
850 void bind_to(Label* L, int pos);
851 void link_to(Label* L, Label* appendix);
852
853 // displacements
854 inline Displacement disp_at(Label* L);
855 inline void disp_at_put(Label* L, Displacement disp);
856 inline void emit_disp(Label* L, Displacement::Type type);
857
858 // record reloc info for current pc_
859 void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
860
861 friend class CodePatcher;
862 friend class EnsureSpace;
863
864 // Code buffer:
865 // The buffer into which code and relocation info are generated.
866 byte* buffer_;
867 int buffer_size_;
868 // True if the assembler owns the buffer, false if buffer is external.
869 bool own_buffer_;
870 // A previously allocated buffer of kMinimalBufferSize bytes, or NULL.
871 static byte* spare_buffer_;
872
873 // code generation
874 byte* pc_; // the program counter; moves forward
875 RelocInfoWriter reloc_info_writer;
876
877 // push-pop elimination
878 byte* last_pc_;
879
880 // source position information
881 int current_statement_position_;
882 int current_position_;
883 int written_statement_position_;
884 int written_position_;
885};
886
887
888// Helper class that ensures that there is enough space for generating
889// instructions and relocation information. The constructor makes
890// sure that there is enough space and (in debug mode) the destructor
891// checks that we did not generate too much.
892class EnsureSpace BASE_EMBEDDED {
893 public:
894 explicit EnsureSpace(Assembler* assembler) : assembler_(assembler) {
895 if (assembler_->overflow()) assembler_->GrowBuffer();
896#ifdef DEBUG
897 space_before_ = assembler_->available_space();
898#endif
899 }
900
901#ifdef DEBUG
902 ~EnsureSpace() {
903 int bytes_generated = space_before_ - assembler_->available_space();
904 ASSERT(bytes_generated < assembler_->kGap);
905 }
906#endif
907
908 private:
909 Assembler* assembler_;
910#ifdef DEBUG
911 int space_before_;
912#endif
913};
914
915} } // namespace v8::internal
916
917#endif // V8_IA32_ASSEMBLER_IA32_H_