blob: 662ebc90220978d86590167694096b0c24929e54 [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
543 // Exchange two registers
544 void xchg(Register dst, Register src);
545
546 // Arithmetics
547 void adc(Register dst, int32_t imm32);
548 void adc(Register dst, const Operand& src);
549
550 void add(Register dst, const Operand& src);
551 void add(const Operand& dst, const Immediate& x);
552
553 void and_(Register dst, int32_t imm32);
554 void and_(Register dst, const Operand& src);
555 void and_(const Operand& src, Register dst);
556 void and_(const Operand& dst, const Immediate& x);
557
558 void cmpb(const Operand& op, int8_t imm8);
559 void cmpb_al(const Operand& op);
560 void cmpw_ax(const Operand& op);
561 void cmpw(const Operand& op, Immediate imm16);
562 void cmp(Register reg, int32_t imm32);
563 void cmp(Register reg, Handle<Object> handle);
564 void cmp(Register reg, const Operand& op);
565 void cmp(const Operand& op, const Immediate& imm);
566 void cmp(const Operand& op, Handle<Object> handle);
567
568 void dec_b(Register dst);
569
570 void dec(Register dst);
571 void dec(const Operand& dst);
572
573 void cdq();
574
575 void idiv(Register src);
576
577 // Signed multiply instructions.
578 void imul(Register src); // edx:eax = eax * src.
579 void imul(Register dst, const Operand& src); // dst = dst * src.
580 void imul(Register dst, Register src, int32_t imm32); // dst = src * imm32.
581
582 void inc(Register dst);
583 void inc(const Operand& dst);
584
585 void lea(Register dst, const Operand& src);
586
587 // Unsigned multiply instruction.
588 void mul(Register src); // edx:eax = eax * reg.
589
590 void neg(Register dst);
591
592 void not_(Register dst);
593
594 void or_(Register dst, int32_t imm32);
595 void or_(Register dst, const Operand& src);
596 void or_(const Operand& dst, Register src);
597 void or_(const Operand& dst, const Immediate& x);
598
599 void rcl(Register dst, uint8_t imm8);
600
601 void sar(Register dst, uint8_t imm8);
Steve Blockd0582a62009-12-15 09:54:21 +0000602 void sar_cl(Register dst);
Steve Blocka7e24c12009-10-30 11:49:00 +0000603
604 void sbb(Register dst, const Operand& src);
605
606 void shld(Register dst, const Operand& src);
607
608 void shl(Register dst, uint8_t imm8);
Steve Blockd0582a62009-12-15 09:54:21 +0000609 void shl_cl(Register dst);
Steve Blocka7e24c12009-10-30 11:49:00 +0000610
611 void shrd(Register dst, const Operand& src);
612
613 void shr(Register dst, uint8_t imm8);
Steve Blocka7e24c12009-10-30 11:49:00 +0000614 void shr_cl(Register dst);
615
Steve Block3ce2e202009-11-05 08:53:23 +0000616 void subb(const Operand& dst, int8_t imm8);
Steve Blocka7e24c12009-10-30 11:49:00 +0000617 void sub(const Operand& dst, const Immediate& x);
618 void sub(Register dst, const Operand& src);
619 void sub(const Operand& dst, Register src);
620
621 void test(Register reg, const Immediate& imm);
622 void test(Register reg, const Operand& op);
623 void test(const Operand& op, const Immediate& imm);
624
625 void xor_(Register dst, int32_t imm32);
626 void xor_(Register dst, const Operand& src);
627 void xor_(const Operand& src, Register dst);
628 void xor_(const Operand& dst, const Immediate& x);
629
630 // Bit operations.
631 void bt(const Operand& dst, Register src);
632 void bts(const Operand& dst, Register src);
633
634 // Miscellaneous
635 void hlt();
636 void int3();
637 void nop();
638 void rdtsc();
639 void ret(int imm16);
640
641 // Label operations & relative jumps (PPUM Appendix D)
642 //
643 // Takes a branch opcode (cc) and a label (L) and generates
644 // either a backward branch or a forward branch and links it
645 // to the label fixup chain. Usage:
646 //
647 // Label L; // unbound label
648 // j(cc, &L); // forward branch to unbound label
649 // bind(&L); // bind label to the current pc
650 // j(cc, &L); // backward branch to bound label
651 // bind(&L); // illegal: a label may be bound only once
652 //
653 // Note: The same Label can be used for forward and backward branches
654 // but it may be bound only once.
655
656 void bind(Label* L); // binds an unbound label L to the current code position
657
658 // Calls
659 void call(Label* L);
660 void call(byte* entry, RelocInfo::Mode rmode);
661 void call(const Operand& adr);
662 void call(Handle<Code> code, RelocInfo::Mode rmode);
663
664 // Jumps
665 void jmp(Label* L); // unconditional jump to L
666 void jmp(byte* entry, RelocInfo::Mode rmode);
667 void jmp(const Operand& adr);
668 void jmp(Handle<Code> code, RelocInfo::Mode rmode);
669
670 // Conditional jumps
671 void j(Condition cc, Label* L, Hint hint = no_hint);
672 void j(Condition cc, byte* entry, RelocInfo::Mode rmode, Hint hint = no_hint);
673 void j(Condition cc, Handle<Code> code, Hint hint = no_hint);
674
675 // Floating-point operations
676 void fld(int i);
677
678 void fld1();
679 void fldz();
680
681 void fld_s(const Operand& adr);
682 void fld_d(const Operand& adr);
683
684 void fstp_s(const Operand& adr);
685 void fstp_d(const Operand& adr);
686
687 void fild_s(const Operand& adr);
688 void fild_d(const Operand& adr);
689
690 void fist_s(const Operand& adr);
691
692 void fistp_s(const Operand& adr);
693 void fistp_d(const Operand& adr);
694
695 void fisttp_s(const Operand& adr);
696
697 void fabs();
698 void fchs();
699 void fcos();
700 void fsin();
701
702 void fadd(int i);
703 void fsub(int i);
704 void fmul(int i);
705 void fdiv(int i);
706
707 void fisub_s(const Operand& adr);
708
709 void faddp(int i = 1);
710 void fsubp(int i = 1);
711 void fsubrp(int i = 1);
712 void fmulp(int i = 1);
713 void fdivp(int i = 1);
714 void fprem();
715 void fprem1();
716
717 void fxch(int i = 1);
718 void fincstp();
719 void ffree(int i = 0);
720
721 void ftst();
722 void fucomp(int i);
723 void fucompp();
Steve Block3ce2e202009-11-05 08:53:23 +0000724 void fucomi(int i);
725 void fucomip();
Steve Blocka7e24c12009-10-30 11:49:00 +0000726 void fcompp();
727 void fnstsw_ax();
728 void fwait();
729 void fnclex();
730
731 void frndint();
732
733 void sahf();
734 void setcc(Condition cc, Register reg);
735
736 void cpuid();
737
738 // SSE2 instructions
739 void cvttss2si(Register dst, const Operand& src);
740 void cvttsd2si(Register dst, const Operand& src);
741
742 void cvtsi2sd(XMMRegister dst, const Operand& src);
743
744 void addsd(XMMRegister dst, XMMRegister src);
745 void subsd(XMMRegister dst, XMMRegister src);
746 void mulsd(XMMRegister dst, XMMRegister src);
747 void divsd(XMMRegister dst, XMMRegister src);
748
749 void comisd(XMMRegister dst, XMMRegister src);
750
751 // Use either movsd or movlpd.
752 void movdbl(XMMRegister dst, const Operand& src);
753 void movdbl(const Operand& dst, XMMRegister src);
754
755 // Debugging
756 void Print();
757
758 // Check the code size generated from label to here.
759 int SizeOfCodeGeneratedSince(Label* l) { return pc_offset() - l->pos(); }
760
761 // Mark address of the ExitJSFrame code.
762 void RecordJSReturn();
763
764 // Record a comment relocation entry that can be used by a disassembler.
765 // Use --debug_code to enable.
766 void RecordComment(const char* msg);
767
768 void RecordPosition(int pos);
769 void RecordStatementPosition(int pos);
770 void WriteRecordedPositions();
771
772 // Writes a single word of data in the code stream.
773 // Used for inline tables, e.g., jump-tables.
774 void dd(uint32_t data, RelocInfo::Mode reloc_info);
775
776 int pc_offset() const { return pc_ - buffer_; }
777 int current_statement_position() const { return current_statement_position_; }
778 int current_position() const { return current_position_; }
779
780 // Check if there is less than kGap bytes available in the buffer.
781 // If this is the case, we need to grow the buffer before emitting
782 // an instruction or relocation information.
783 inline bool overflow() const { return pc_ >= reloc_info_writer.pos() - kGap; }
784
785 // Get the number of bytes available in the buffer.
786 inline int available_space() const { return reloc_info_writer.pos() - pc_; }
787
788 // Avoid overflows for displacements etc.
789 static const int kMaximalBufferSize = 512*MB;
790 static const int kMinimalBufferSize = 4*KB;
791
792 protected:
793 void movsd(XMMRegister dst, const Operand& src);
794 void movsd(const Operand& dst, XMMRegister src);
795
796 void emit_sse_operand(XMMRegister reg, const Operand& adr);
797 void emit_sse_operand(XMMRegister dst, XMMRegister src);
798
799
800 private:
801 byte* addr_at(int pos) { return buffer_ + pos; }
802 byte byte_at(int pos) { return buffer_[pos]; }
803 uint32_t long_at(int pos) {
804 return *reinterpret_cast<uint32_t*>(addr_at(pos));
805 }
806 void long_at_put(int pos, uint32_t x) {
807 *reinterpret_cast<uint32_t*>(addr_at(pos)) = x;
808 }
809
810 // code emission
811 void GrowBuffer();
812 inline void emit(uint32_t x);
813 inline void emit(Handle<Object> handle);
814 inline void emit(uint32_t x, RelocInfo::Mode rmode);
815 inline void emit(const Immediate& x);
816 inline void emit_w(const Immediate& x);
817
818 // Emit the code-object-relative offset of the label's position
819 inline void emit_code_relative_offset(Label* label);
820
821 // instruction generation
822 void emit_arith_b(int op1, int op2, Register dst, int imm8);
823
824 // Emit a basic arithmetic instruction (i.e. first byte of the family is 0x81)
825 // with a given destination expression and an immediate operand. It attempts
826 // to use the shortest encoding possible.
827 // sel specifies the /n in the modrm byte (see the Intel PRM).
828 void emit_arith(int sel, Operand dst, const Immediate& x);
829
830 void emit_operand(Register reg, const Operand& adr);
831
832 void emit_farith(int b1, int b2, int i);
833
834 // labels
835 void print(Label* L);
836 void bind_to(Label* L, int pos);
837 void link_to(Label* L, Label* appendix);
838
839 // displacements
840 inline Displacement disp_at(Label* L);
841 inline void disp_at_put(Label* L, Displacement disp);
842 inline void emit_disp(Label* L, Displacement::Type type);
843
844 // record reloc info for current pc_
845 void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
846
847 friend class CodePatcher;
848 friend class EnsureSpace;
849
850 // Code buffer:
851 // The buffer into which code and relocation info are generated.
852 byte* buffer_;
853 int buffer_size_;
854 // True if the assembler owns the buffer, false if buffer is external.
855 bool own_buffer_;
856 // A previously allocated buffer of kMinimalBufferSize bytes, or NULL.
857 static byte* spare_buffer_;
858
859 // code generation
860 byte* pc_; // the program counter; moves forward
861 RelocInfoWriter reloc_info_writer;
862
863 // push-pop elimination
864 byte* last_pc_;
865
866 // source position information
867 int current_statement_position_;
868 int current_position_;
869 int written_statement_position_;
870 int written_position_;
871};
872
873
874// Helper class that ensures that there is enough space for generating
875// instructions and relocation information. The constructor makes
876// sure that there is enough space and (in debug mode) the destructor
877// checks that we did not generate too much.
878class EnsureSpace BASE_EMBEDDED {
879 public:
880 explicit EnsureSpace(Assembler* assembler) : assembler_(assembler) {
881 if (assembler_->overflow()) assembler_->GrowBuffer();
882#ifdef DEBUG
883 space_before_ = assembler_->available_space();
884#endif
885 }
886
887#ifdef DEBUG
888 ~EnsureSpace() {
889 int bytes_generated = space_before_ - assembler_->available_space();
890 ASSERT(bytes_generated < assembler_->kGap);
891 }
892#endif
893
894 private:
895 Assembler* assembler_;
896#ifdef DEBUG
897 int space_before_;
898#endif
899};
900
901} } // namespace v8::internal
902
903#endif // V8_IA32_ASSEMBLER_IA32_H_