blob: c186094b39770274d4e4124a945e419e3696cb90 [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.
Steve Block1e0659c2011-05-24 12:43:12 +010033// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +000034
35// A light-weight IA32 Assembler.
36
37#ifndef V8_IA32_ASSEMBLER_IA32_H_
38#define V8_IA32_ASSEMBLER_IA32_H_
39
Steve Block44f0eee2011-05-26 01:26:41 +010040#include "isolate.h"
Steve Blockd0582a62009-12-15 09:54:21 +000041#include "serialize.h"
42
Steve Blocka7e24c12009-10-30 11:49:00 +000043namespace v8 {
44namespace internal {
45
46// CPU Registers.
47//
48// 1) We would prefer to use an enum, but enum values are assignment-
49// compatible with int, which has caused code-generation bugs.
50//
51// 2) We would prefer to use a class instead of a struct but we don't like
52// the register initialization to depend on the particular initialization
53// order (which appears to be different on OS X, Linux, and Windows for the
54// installed versions of C++ we tried). Using a struct permits C-style
55// "initialization". Also, the Register objects cannot be const as this
56// forces initialization stubs in MSVC, making us dependent on initialization
57// order.
58//
59// 3) By not using an enum, we are possibly preventing the compiler from
60// doing certain constant folds, which may significantly reduce the
61// code generated for some assembly instructions (because they boil down
62// to a few constants). If this is a problem, we could change the code
63// such that we use an enum in optimized mode, and the struct in debug
64// mode. This way we get the compile-time error checking in debug mode
65// and best performance in optimized code.
66//
67struct Register {
Steve Block1e0659c2011-05-24 12:43:12 +010068 static const int kNumAllocatableRegisters = 6;
Ben Murdochb0fe1622011-05-05 13:52:32 +010069 static const int kNumRegisters = 8;
70
Steve Block1e0659c2011-05-24 12:43:12 +010071 static inline const char* AllocationIndexToString(int index);
Ben Murdochb0fe1622011-05-05 13:52:32 +010072
Steve Block1e0659c2011-05-24 12:43:12 +010073 static inline int ToAllocationIndex(Register reg);
Ben Murdochb0fe1622011-05-05 13:52:32 +010074
Steve Block1e0659c2011-05-24 12:43:12 +010075 static inline Register FromAllocationIndex(int index);
Ben Murdochb0fe1622011-05-05 13:52:32 +010076
77 static Register from_code(int code) {
78 Register r = { code };
79 return r;
80 }
81 bool is_valid() const { return 0 <= code_ && code_ < kNumRegisters; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +010082 bool is(Register reg) const { return code_ == reg.code_; }
Steve Blocka7e24c12009-10-30 11:49:00 +000083 // eax, ebx, ecx and edx are byte registers, the rest are not.
Kristian Monsen0d5e1162010-09-30 15:31:59 +010084 bool is_byte_register() const { return code_ <= 3; }
85 int code() const {
Steve Blocka7e24c12009-10-30 11:49:00 +000086 ASSERT(is_valid());
87 return code_;
88 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +010089 int bit() const {
Steve Blocka7e24c12009-10-30 11:49:00 +000090 ASSERT(is_valid());
91 return 1 << code_;
92 }
93
Andrei Popescu31002712010-02-23 13:46:05 +000094 // Unfortunately we can't make this private in a struct.
Steve Blocka7e24c12009-10-30 11:49:00 +000095 int code_;
96};
97
Steve Block1e0659c2011-05-24 12:43:12 +010098
Steve Blocka7e24c12009-10-30 11:49:00 +000099const Register eax = { 0 };
100const Register ecx = { 1 };
101const Register edx = { 2 };
102const Register ebx = { 3 };
103const Register esp = { 4 };
104const Register ebp = { 5 };
105const Register esi = { 6 };
106const Register edi = { 7 };
107const Register no_reg = { -1 };
108
109
Steve Block1e0659c2011-05-24 12:43:12 +0100110inline const char* Register::AllocationIndexToString(int index) {
111 ASSERT(index >= 0 && index < kNumAllocatableRegisters);
112 // This is the mapping of allocation indices to registers.
113 const char* const kNames[] = { "eax", "ecx", "edx", "ebx", "esi", "edi" };
114 return kNames[index];
115}
116
117
118inline int Register::ToAllocationIndex(Register reg) {
119 ASSERT(reg.is_valid() && !reg.is(esp) && !reg.is(ebp));
120 return (reg.code() >= 6) ? reg.code() - 2 : reg.code();
121}
122
123
124inline Register Register::FromAllocationIndex(int index) {
125 ASSERT(index >= 0 && index < kNumAllocatableRegisters);
126 return (index >= 4) ? from_code(index + 2) : from_code(index);
127}
128
129
Steve Blocka7e24c12009-10-30 11:49:00 +0000130struct XMMRegister {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100131 static const int kNumAllocatableRegisters = 7;
132 static const int kNumRegisters = 8;
133
134 static int ToAllocationIndex(XMMRegister reg) {
135 ASSERT(reg.code() != 0);
136 return reg.code() - 1;
137 }
138
139 static XMMRegister FromAllocationIndex(int index) {
140 ASSERT(index >= 0 && index < kNumAllocatableRegisters);
141 return from_code(index + 1);
142 }
143
144 static const char* AllocationIndexToString(int index) {
145 ASSERT(index >= 0 && index < kNumAllocatableRegisters);
146 const char* const names[] = {
147 "xmm1",
148 "xmm2",
149 "xmm3",
150 "xmm4",
151 "xmm5",
152 "xmm6",
153 "xmm7"
154 };
155 return names[index];
156 }
157
158 static XMMRegister from_code(int code) {
159 XMMRegister r = { code };
160 return r;
161 }
162
163 bool is_valid() const { return 0 <= code_ && code_ < kNumRegisters; }
164 bool is(XMMRegister reg) const { return code_ == reg.code_; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100165 int code() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000166 ASSERT(is_valid());
167 return code_;
168 }
169
170 int code_;
171};
172
Ben Murdochb0fe1622011-05-05 13:52:32 +0100173
Steve Blocka7e24c12009-10-30 11:49:00 +0000174const XMMRegister xmm0 = { 0 };
175const XMMRegister xmm1 = { 1 };
176const XMMRegister xmm2 = { 2 };
177const XMMRegister xmm3 = { 3 };
178const XMMRegister xmm4 = { 4 };
179const XMMRegister xmm5 = { 5 };
180const XMMRegister xmm6 = { 6 };
181const XMMRegister xmm7 = { 7 };
182
Ben Murdochb0fe1622011-05-05 13:52:32 +0100183
184typedef XMMRegister DoubleRegister;
185
186
Steve Blocka7e24c12009-10-30 11:49:00 +0000187enum Condition {
188 // any value < 0 is considered no_condition
189 no_condition = -1,
190
191 overflow = 0,
192 no_overflow = 1,
193 below = 2,
194 above_equal = 3,
195 equal = 4,
196 not_equal = 5,
197 below_equal = 6,
198 above = 7,
199 negative = 8,
200 positive = 9,
201 parity_even = 10,
202 parity_odd = 11,
203 less = 12,
204 greater_equal = 13,
205 less_equal = 14,
206 greater = 15,
207
208 // aliases
209 carry = below,
210 not_carry = above_equal,
211 zero = equal,
212 not_zero = not_equal,
213 sign = negative,
214 not_sign = positive
215};
216
217
218// Returns the equivalent of !cc.
219// Negation of the default no_condition (-1) results in a non-default
220// no_condition value (-2). As long as tests for no_condition check
221// for condition < 0, this will work as expected.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100222inline Condition NegateCondition(Condition cc) {
223 return static_cast<Condition>(cc ^ 1);
224}
225
Steve Blocka7e24c12009-10-30 11:49:00 +0000226
227// Corresponds to transposing the operands of a comparison.
228inline Condition ReverseCondition(Condition cc) {
229 switch (cc) {
230 case below:
231 return above;
232 case above:
233 return below;
234 case above_equal:
235 return below_equal;
236 case below_equal:
237 return above_equal;
238 case less:
239 return greater;
240 case greater:
241 return less;
242 case greater_equal:
243 return less_equal;
244 case less_equal:
245 return greater_equal;
246 default:
247 return cc;
248 };
249}
250
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100251
Steve Blocka7e24c12009-10-30 11:49:00 +0000252// -----------------------------------------------------------------------------
253// Machine instruction Immediates
254
255class Immediate BASE_EMBEDDED {
256 public:
257 inline explicit Immediate(int x);
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 inline explicit Immediate(const ExternalReference& ext);
259 inline explicit Immediate(Handle<Object> handle);
260 inline explicit Immediate(Smi* value);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100261 inline explicit Immediate(Address addr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000262
263 static Immediate CodeRelativeOffset(Label* label) {
264 return Immediate(label);
265 }
266
267 bool is_zero() const { return x_ == 0 && rmode_ == RelocInfo::NONE; }
268 bool is_int8() const {
269 return -128 <= x_ && x_ < 128 && rmode_ == RelocInfo::NONE;
270 }
271 bool is_int16() const {
272 return -32768 <= x_ && x_ < 32768 && rmode_ == RelocInfo::NONE;
273 }
274
275 private:
276 inline explicit Immediate(Label* value);
277
278 int x_;
279 RelocInfo::Mode rmode_;
280
281 friend class Assembler;
Steve Block053d10c2011-06-13 19:13:29 +0100282 friend class MacroAssembler;
Steve Blocka7e24c12009-10-30 11:49:00 +0000283};
284
285
286// -----------------------------------------------------------------------------
287// Machine instruction Operands
288
289enum ScaleFactor {
290 times_1 = 0,
291 times_2 = 1,
292 times_4 = 2,
293 times_8 = 3,
Leon Clarke4515c472010-02-03 11:58:03 +0000294 times_int_size = times_4,
295 times_half_pointer_size = times_2,
Andrei Popescu402d9372010-02-26 13:31:12 +0000296 times_pointer_size = times_4,
297 times_twice_pointer_size = times_8
Steve Blocka7e24c12009-10-30 11:49:00 +0000298};
299
300
301class Operand BASE_EMBEDDED {
302 public:
303 // reg
304 INLINE(explicit Operand(Register reg));
305
Steve Block6ded16b2010-05-10 14:33:55 +0100306 // XMM reg
307 INLINE(explicit Operand(XMMRegister xmm_reg));
308
Steve Blocka7e24c12009-10-30 11:49:00 +0000309 // [disp/r]
310 INLINE(explicit Operand(int32_t disp, RelocInfo::Mode rmode));
311 // disp only must always be relocated
312
313 // [base + disp/r]
314 explicit Operand(Register base, int32_t disp,
315 RelocInfo::Mode rmode = RelocInfo::NONE);
316
317 // [base + index*scale + disp/r]
318 explicit Operand(Register base,
319 Register index,
320 ScaleFactor scale,
321 int32_t disp,
322 RelocInfo::Mode rmode = RelocInfo::NONE);
323
324 // [index*scale + disp/r]
325 explicit Operand(Register index,
326 ScaleFactor scale,
327 int32_t disp,
328 RelocInfo::Mode rmode = RelocInfo::NONE);
329
330 static Operand StaticVariable(const ExternalReference& ext) {
331 return Operand(reinterpret_cast<int32_t>(ext.address()),
332 RelocInfo::EXTERNAL_REFERENCE);
333 }
334
335 static Operand StaticArray(Register index,
336 ScaleFactor scale,
337 const ExternalReference& arr) {
338 return Operand(index, scale, reinterpret_cast<int32_t>(arr.address()),
339 RelocInfo::EXTERNAL_REFERENCE);
340 }
341
Ben Murdochb0fe1622011-05-05 13:52:32 +0100342 static Operand Cell(Handle<JSGlobalPropertyCell> cell) {
343 return Operand(reinterpret_cast<int32_t>(cell.location()),
344 RelocInfo::GLOBAL_PROPERTY_CELL);
345 }
346
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 // Returns true if this Operand is a wrapper for the specified register.
348 bool is_reg(Register reg) const;
349
350 private:
351 byte buf_[6];
352 // The number of bytes in buf_.
353 unsigned int len_;
354 // Only valid if len_ > 4.
355 RelocInfo::Mode rmode_;
356
357 // Set the ModRM byte without an encoded 'reg' register. The
358 // register is encoded later as part of the emit_operand operation.
359 inline void set_modrm(int mod, Register rm);
360
361 inline void set_sib(ScaleFactor scale, Register index, Register base);
362 inline void set_disp8(int8_t disp);
363 inline void set_dispr(int32_t disp, RelocInfo::Mode rmode);
364
365 friend class Assembler;
366};
367
368
369// -----------------------------------------------------------------------------
370// A Displacement describes the 32bit immediate field of an instruction which
371// may be used together with a Label in order to refer to a yet unknown code
372// position. Displacements stored in the instruction stream are used to describe
373// the instruction and to chain a list of instructions using the same Label.
374// A Displacement contains 2 different fields:
375//
376// next field: position of next displacement in the chain (0 = end of list)
377// type field: instruction type
378//
379// A next value of null (0) indicates the end of a chain (note that there can
380// be no displacement at position zero, because there is always at least one
381// instruction byte before the displacement).
382//
383// Displacement _data field layout
384//
385// |31.....2|1......0|
386// [ next | type |
387
388class Displacement BASE_EMBEDDED {
389 public:
390 enum Type {
391 UNCONDITIONAL_JUMP,
392 CODE_RELATIVE,
393 OTHER
394 };
395
396 int data() const { return data_; }
397 Type type() const { return TypeField::decode(data_); }
398 void next(Label* L) const {
399 int n = NextField::decode(data_);
400 n > 0 ? L->link_to(n) : L->Unuse();
401 }
402 void link_to(Label* L) { init(L, type()); }
403
404 explicit Displacement(int data) { data_ = data; }
405
406 Displacement(Label* L, Type type) { init(L, type); }
407
408 void print() {
409 PrintF("%s (%x) ", (type() == UNCONDITIONAL_JUMP ? "jmp" : "[other]"),
410 NextField::decode(data_));
411 }
412
413 private:
414 int data_;
415
416 class TypeField: public BitField<Type, 0, 2> {};
417 class NextField: public BitField<int, 2, 32-2> {};
418
419 void init(Label* L, Type type);
420};
421
422
423
424// CpuFeatures keeps track of which features are supported by the target CPU.
425// Supported features must be enabled by a Scope before use.
426// Example:
427// if (CpuFeatures::IsSupported(SSE2)) {
428// CpuFeatures::Scope fscope(SSE2);
429// // Generate SSE2 floating point code.
430// } else {
431// // Generate standard x87 floating point code.
432// }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100433class CpuFeatures : public AllStatic {
Steve Blocka7e24c12009-10-30 11:49:00 +0000434 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100435 // Detect features of the target CPU. Set safe defaults if the serializer
436 // is enabled (snapshots must be portable).
437 static void Probe();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100438
Steve Blocka7e24c12009-10-30 11:49:00 +0000439 // Check whether a feature is supported by the target CPU.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100440 static bool IsSupported(CpuFeature f) {
441 ASSERT(initialized_);
Steve Block3ce2e202009-11-05 08:53:23 +0000442 if (f == SSE2 && !FLAG_enable_sse2) return false;
443 if (f == SSE3 && !FLAG_enable_sse3) return false;
Ben Murdochf87a2032010-10-22 12:50:53 +0100444 if (f == SSE4_1 && !FLAG_enable_sse4_1) return false;
Steve Block3ce2e202009-11-05 08:53:23 +0000445 if (f == CMOV && !FLAG_enable_cmov) return false;
446 if (f == RDTSC && !FLAG_enable_rdtsc) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000447 return (supported_ & (static_cast<uint64_t>(1) << f)) != 0;
448 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100449
450#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000451 // Check whether a feature is currently enabled.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100452 static bool IsEnabled(CpuFeature f) {
453 ASSERT(initialized_);
454 Isolate* isolate = Isolate::UncheckedCurrent();
455 if (isolate == NULL) {
456 // When no isolate is available, work as if we're running in
457 // release mode.
458 return IsSupported(f);
459 }
460 uint64_t enabled = isolate->enabled_cpu_features();
461 return (enabled & (static_cast<uint64_t>(1) << f)) != 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000462 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100463#endif
464
Steve Blocka7e24c12009-10-30 11:49:00 +0000465 // Enable a specified feature within a scope.
466 class Scope BASE_EMBEDDED {
467#ifdef DEBUG
468 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100469 explicit Scope(CpuFeature f) {
Steve Blockd0582a62009-12-15 09:54:21 +0000470 uint64_t mask = static_cast<uint64_t>(1) << f;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100471 ASSERT(CpuFeatures::IsSupported(f));
Steve Block44f0eee2011-05-26 01:26:41 +0100472 ASSERT(!Serializer::enabled() ||
Ben Murdoch8b112d22011-06-08 16:22:53 +0100473 (CpuFeatures::found_by_runtime_probing_ & mask) == 0);
474 isolate_ = Isolate::UncheckedCurrent();
475 old_enabled_ = 0;
476 if (isolate_ != NULL) {
477 old_enabled_ = isolate_->enabled_cpu_features();
478 isolate_->set_enabled_cpu_features(old_enabled_ | mask);
479 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000480 }
Steve Block44f0eee2011-05-26 01:26:41 +0100481 ~Scope() {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100482 ASSERT_EQ(Isolate::UncheckedCurrent(), isolate_);
483 if (isolate_ != NULL) {
484 isolate_->set_enabled_cpu_features(old_enabled_);
485 }
Steve Block44f0eee2011-05-26 01:26:41 +0100486 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000487 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100488 Isolate* isolate_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100489 uint64_t old_enabled_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000490#else
491 public:
Steve Blockd0582a62009-12-15 09:54:21 +0000492 explicit Scope(CpuFeature f) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000493#endif
494 };
Steve Block44f0eee2011-05-26 01:26:41 +0100495
Ben Murdoch8b112d22011-06-08 16:22:53 +0100496 class TryForceFeatureScope BASE_EMBEDDED {
497 public:
498 explicit TryForceFeatureScope(CpuFeature f)
499 : old_supported_(CpuFeatures::supported_) {
500 if (CanForce()) {
501 CpuFeatures::supported_ |= (static_cast<uint64_t>(1) << f);
502 }
503 }
504
505 ~TryForceFeatureScope() {
506 if (CanForce()) {
507 CpuFeatures::supported_ = old_supported_;
508 }
509 }
510
511 private:
512 static bool CanForce() {
513 // It's only safe to temporarily force support of CPU features
514 // when there's only a single isolate, which is guaranteed when
515 // the serializer is enabled.
516 return Serializer::enabled();
517 }
518
519 const uint64_t old_supported_;
520 };
521
Steve Blocka7e24c12009-10-30 11:49:00 +0000522 private:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100523#ifdef DEBUG
524 static bool initialized_;
525#endif
526 static uint64_t supported_;
527 static uint64_t found_by_runtime_probing_;
Steve Block44f0eee2011-05-26 01:26:41 +0100528
529 DISALLOW_COPY_AND_ASSIGN(CpuFeatures);
Steve Blocka7e24c12009-10-30 11:49:00 +0000530};
531
532
Steve Block44f0eee2011-05-26 01:26:41 +0100533class Assembler : public AssemblerBase {
Steve Blocka7e24c12009-10-30 11:49:00 +0000534 private:
535 // We check before assembling an instruction that there is sufficient
536 // space to write an instruction and its relocation information.
537 // The relocation writer's position must be kGap bytes above the end of
538 // the generated instructions. This leaves enough space for the
539 // longest possible ia32 instruction, 15 bytes, and the longest possible
540 // relocation information encoding, RelocInfoWriter::kMaxLength == 16.
541 // (There is a 15 byte limit on ia32 instruction length that rules out some
542 // otherwise valid instructions.)
543 // This allows for a single, fast space check per instruction.
544 static const int kGap = 32;
545
546 public:
547 // Create an assembler. Instructions and relocation information are emitted
548 // into a buffer, with the instructions starting from the beginning and the
549 // relocation information starting from the end of the buffer. See CodeDesc
550 // for a detailed comment on the layout (globals.h).
551 //
552 // If the provided buffer is NULL, the assembler allocates and grows its own
553 // buffer, and buffer_size determines the initial buffer size. The buffer is
554 // owned by the assembler and deallocated upon destruction of the assembler.
555 //
556 // If the provided buffer is not NULL, the assembler uses the provided buffer
557 // for code generation and assumes its size to be buffer_size. If the buffer
558 // is too small, a fatal error occurs. No deallocation of the buffer is done
559 // upon destruction of the assembler.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100560 // TODO(vitalyr): the assembler does not need an isolate.
561 Assembler(Isolate* isolate, void* buffer, int buffer_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000562 ~Assembler();
563
Steve Block44f0eee2011-05-26 01:26:41 +0100564 // Overrides the default provided by FLAG_debug_code.
565 void set_emit_debug_code(bool value) { emit_debug_code_ = value; }
566
Steve Blocka7e24c12009-10-30 11:49:00 +0000567 // GetCode emits any pending (non-emitted) code and fills the descriptor
568 // desc. GetCode() is idempotent; it returns the same result if no other
569 // Assembler functions are invoked in between GetCode() calls.
570 void GetCode(CodeDesc* desc);
571
572 // Read/Modify the code target in the branch/call instruction at pc.
573 inline static Address target_address_at(Address pc);
574 inline static void set_target_address_at(Address pc, Address target);
575
Steve Blockd0582a62009-12-15 09:54:21 +0000576 // This sets the branch destination (which is in the instruction on x86).
577 // This is for calls and branches within generated code.
578 inline static void set_target_at(Address instruction_payload,
579 Address target) {
580 set_target_address_at(instruction_payload, target);
581 }
582
583 // This sets the branch destination (which is in the instruction on x86).
584 // This is for calls and branches to runtime code.
585 inline static void set_external_target_at(Address instruction_payload,
586 Address target) {
587 set_target_address_at(instruction_payload, target);
588 }
589
590 static const int kCallTargetSize = kPointerSize;
591 static const int kExternalTargetSize = kPointerSize;
592
Steve Blocka7e24c12009-10-30 11:49:00 +0000593 // Distance between the address of the code target in the call instruction
594 // and the return address
595 static const int kCallTargetAddressOffset = kPointerSize;
596 // Distance between start of patched return sequence and the emitted address
597 // to jump to.
598 static const int kPatchReturnSequenceAddressOffset = 1; // JMP imm32.
599
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100600 // Distance between start of patched debug break slot and the emitted address
601 // to jump to.
602 static const int kPatchDebugBreakSlotAddressOffset = 1; // JMP imm32.
603
Steve Blockd0582a62009-12-15 09:54:21 +0000604 static const int kCallInstructionLength = 5;
605 static const int kJSReturnSequenceLength = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +0000606
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100607 // The debug break slot must be able to contain a call instruction.
608 static const int kDebugBreakSlotLength = kCallInstructionLength;
609
Ben Murdochb0fe1622011-05-05 13:52:32 +0100610 // One byte opcode for test eax,0xXXXXXXXX.
611 static const byte kTestEaxByte = 0xA9;
612 // One byte opcode for test al, 0xXX.
613 static const byte kTestAlByte = 0xA8;
614 // One byte opcode for nop.
615 static const byte kNopByte = 0x90;
616
617 // One byte opcode for a short unconditional jump.
618 static const byte kJmpShortOpcode = 0xEB;
619 // One byte prefix for a short conditional jump.
620 static const byte kJccShortPrefix = 0x70;
621 static const byte kJncShortOpcode = kJccShortPrefix | not_carry;
622 static const byte kJcShortOpcode = kJccShortPrefix | carry;
623
Steve Blocka7e24c12009-10-30 11:49:00 +0000624 // ---------------------------------------------------------------------------
625 // Code generation
626 //
627 // - function names correspond one-to-one to ia32 instruction mnemonics
628 // - unless specified otherwise, instructions operate on 32bit operands
629 // - instructions on 8bit (byte) operands/registers have a trailing '_b'
630 // - instructions on 16bit (word) operands/registers have a trailing '_w'
631 // - naming conflicts with C++ keywords are resolved via a trailing '_'
632
633 // NOTE ON INTERFACE: Currently, the interface is not very consistent
634 // in the sense that some operations (e.g. mov()) can be called in more
635 // the one way to generate the same instruction: The Register argument
636 // can in some cases be replaced with an Operand(Register) argument.
637 // This should be cleaned up and made more orthogonal. The questions
638 // is: should we always use Operands instead of Registers where an
639 // Operand is possible, or should we have a Register (overloaded) form
640 // instead? We must be careful to make sure that the selected instruction
641 // is obvious from the parameters to avoid hard-to-find code generation
642 // bugs.
643
644 // Insert the smallest number of nop instructions
645 // possible to align the pc offset to a multiple
646 // of m. m must be a power of 2.
647 void Align(int m);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100648 // Aligns code to something that's optimal for a jump target for the platform.
649 void CodeTargetAlign();
Steve Blocka7e24c12009-10-30 11:49:00 +0000650
651 // Stack
652 void pushad();
653 void popad();
654
655 void pushfd();
656 void popfd();
657
658 void push(const Immediate& x);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100659 void push_imm32(int32_t imm32);
Steve Blocka7e24c12009-10-30 11:49:00 +0000660 void push(Register src);
661 void push(const Operand& src);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000662 void push(Handle<Object> handle);
Steve Blocka7e24c12009-10-30 11:49:00 +0000663
664 void pop(Register dst);
665 void pop(const Operand& dst);
666
667 void enter(const Immediate& size);
668 void leave();
669
670 // Moves
671 void mov_b(Register dst, const Operand& src);
672 void mov_b(const Operand& dst, int8_t imm8);
673 void mov_b(const Operand& dst, Register src);
674
675 void mov_w(Register dst, const Operand& src);
676 void mov_w(const Operand& dst, Register src);
677
678 void mov(Register dst, int32_t imm32);
679 void mov(Register dst, const Immediate& x);
680 void mov(Register dst, Handle<Object> handle);
681 void mov(Register dst, const Operand& src);
682 void mov(Register dst, Register src);
683 void mov(const Operand& dst, const Immediate& x);
684 void mov(const Operand& dst, Handle<Object> handle);
685 void mov(const Operand& dst, Register src);
686
687 void movsx_b(Register dst, const Operand& src);
688
689 void movsx_w(Register dst, const Operand& src);
690
691 void movzx_b(Register dst, const Operand& src);
692
693 void movzx_w(Register dst, const Operand& src);
694
695 // Conditional moves
696 void cmov(Condition cc, Register dst, int32_t imm32);
697 void cmov(Condition cc, Register dst, Handle<Object> handle);
698 void cmov(Condition cc, Register dst, const Operand& src);
699
Steve Block6ded16b2010-05-10 14:33:55 +0100700 // Flag management.
701 void cld();
702
Leon Clarkee46be812010-01-19 14:06:41 +0000703 // Repetitive string instructions.
704 void rep_movs();
Steve Block6ded16b2010-05-10 14:33:55 +0100705 void rep_stos();
Leon Clarkef7060e22010-06-03 12:02:55 +0100706 void stos();
Leon Clarkee46be812010-01-19 14:06:41 +0000707
Steve Blocka7e24c12009-10-30 11:49:00 +0000708 // Exchange two registers
709 void xchg(Register dst, Register src);
710
711 // Arithmetics
712 void adc(Register dst, int32_t imm32);
713 void adc(Register dst, const Operand& src);
714
715 void add(Register dst, const Operand& src);
716 void add(const Operand& dst, const Immediate& x);
717
718 void and_(Register dst, int32_t imm32);
Steve Block59151502010-09-22 15:07:15 +0100719 void and_(Register dst, const Immediate& x);
Steve Blocka7e24c12009-10-30 11:49:00 +0000720 void and_(Register dst, const Operand& src);
721 void and_(const Operand& src, Register dst);
722 void and_(const Operand& dst, const Immediate& x);
723
724 void cmpb(const Operand& op, int8_t imm8);
Leon Clarked91b9f72010-01-27 17:25:45 +0000725 void cmpb(Register src, const Operand& dst);
726 void cmpb(const Operand& dst, Register src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000727 void cmpb_al(const Operand& op);
728 void cmpw_ax(const Operand& op);
729 void cmpw(const Operand& op, Immediate imm16);
730 void cmp(Register reg, int32_t imm32);
731 void cmp(Register reg, Handle<Object> handle);
732 void cmp(Register reg, const Operand& op);
733 void cmp(const Operand& op, const Immediate& imm);
734 void cmp(const Operand& op, Handle<Object> handle);
735
736 void dec_b(Register dst);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100737 void dec_b(const Operand& dst);
Steve Blocka7e24c12009-10-30 11:49:00 +0000738
739 void dec(Register dst);
740 void dec(const Operand& dst);
741
742 void cdq();
743
744 void idiv(Register src);
745
746 // Signed multiply instructions.
747 void imul(Register src); // edx:eax = eax * src.
748 void imul(Register dst, const Operand& src); // dst = dst * src.
749 void imul(Register dst, Register src, int32_t imm32); // dst = src * imm32.
750
751 void inc(Register dst);
752 void inc(const Operand& dst);
753
754 void lea(Register dst, const Operand& src);
755
756 // Unsigned multiply instruction.
757 void mul(Register src); // edx:eax = eax * reg.
758
759 void neg(Register dst);
760
761 void not_(Register dst);
762
763 void or_(Register dst, int32_t imm32);
764 void or_(Register dst, const Operand& src);
765 void or_(const Operand& dst, Register src);
766 void or_(const Operand& dst, const Immediate& x);
767
768 void rcl(Register dst, uint8_t imm8);
Iain Merrick75681382010-08-19 15:07:18 +0100769 void rcr(Register dst, uint8_t imm8);
Steve Blocka7e24c12009-10-30 11:49:00 +0000770
771 void sar(Register dst, uint8_t imm8);
Steve Blockd0582a62009-12-15 09:54:21 +0000772 void sar_cl(Register dst);
Steve Blocka7e24c12009-10-30 11:49:00 +0000773
774 void sbb(Register dst, const Operand& src);
775
776 void shld(Register dst, const Operand& src);
777
778 void shl(Register dst, uint8_t imm8);
Steve Blockd0582a62009-12-15 09:54:21 +0000779 void shl_cl(Register dst);
Steve Blocka7e24c12009-10-30 11:49:00 +0000780
781 void shrd(Register dst, const Operand& src);
782
783 void shr(Register dst, uint8_t imm8);
Steve Blocka7e24c12009-10-30 11:49:00 +0000784 void shr_cl(Register dst);
785
Steve Block3ce2e202009-11-05 08:53:23 +0000786 void subb(const Operand& dst, int8_t imm8);
Leon Clarkee46be812010-01-19 14:06:41 +0000787 void subb(Register dst, const Operand& src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000788 void sub(const Operand& dst, const Immediate& x);
789 void sub(Register dst, const Operand& src);
790 void sub(const Operand& dst, Register src);
791
792 void test(Register reg, const Immediate& imm);
793 void test(Register reg, const Operand& op);
Leon Clarkee46be812010-01-19 14:06:41 +0000794 void test_b(Register reg, const Operand& op);
Steve Blocka7e24c12009-10-30 11:49:00 +0000795 void test(const Operand& op, const Immediate& imm);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100796 void test_b(const Operand& op, uint8_t imm8);
Steve Blocka7e24c12009-10-30 11:49:00 +0000797
798 void xor_(Register dst, int32_t imm32);
799 void xor_(Register dst, const Operand& src);
800 void xor_(const Operand& src, Register dst);
801 void xor_(const Operand& dst, const Immediate& x);
802
803 // Bit operations.
804 void bt(const Operand& dst, Register src);
805 void bts(const Operand& dst, Register src);
806
807 // Miscellaneous
808 void hlt();
809 void int3();
810 void nop();
811 void rdtsc();
812 void ret(int imm16);
813
814 // Label operations & relative jumps (PPUM Appendix D)
815 //
816 // Takes a branch opcode (cc) and a label (L) and generates
817 // either a backward branch or a forward branch and links it
818 // to the label fixup chain. Usage:
819 //
820 // Label L; // unbound label
821 // j(cc, &L); // forward branch to unbound label
822 // bind(&L); // bind label to the current pc
823 // j(cc, &L); // backward branch to bound label
824 // bind(&L); // illegal: a label may be bound only once
825 //
826 // Note: The same Label can be used for forward and backward branches
827 // but it may be bound only once.
828
829 void bind(Label* L); // binds an unbound label L to the current code position
830
831 // Calls
832 void call(Label* L);
833 void call(byte* entry, RelocInfo::Mode rmode);
Ben Murdoch257744e2011-11-30 15:57:28 +0000834 int CallSize(const Operand& adr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000835 void call(const Operand& adr);
Ben Murdoch257744e2011-11-30 15:57:28 +0000836 int CallSize(Handle<Code> code, RelocInfo::Mode mode);
837 void call(Handle<Code> code,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000838 RelocInfo::Mode rmode = RelocInfo::CODE_TARGET,
Ben Murdoch257744e2011-11-30 15:57:28 +0000839 unsigned ast_id = kNoASTId);
Steve Blocka7e24c12009-10-30 11:49:00 +0000840
841 // Jumps
Ben Murdoch257744e2011-11-30 15:57:28 +0000842 // unconditional jump to L
843 void jmp(Label* L, Label::Distance distance = Label::kFar);
Steve Blocka7e24c12009-10-30 11:49:00 +0000844 void jmp(byte* entry, RelocInfo::Mode rmode);
845 void jmp(const Operand& adr);
846 void jmp(Handle<Code> code, RelocInfo::Mode rmode);
847
848 // Conditional jumps
Ben Murdoch257744e2011-11-30 15:57:28 +0000849 void j(Condition cc,
850 Label* L,
851 Label::Distance distance = Label::kFar);
852 void j(Condition cc, byte* entry, RelocInfo::Mode rmode);
853 void j(Condition cc, Handle<Code> code);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100854
Steve Blocka7e24c12009-10-30 11:49:00 +0000855 // Floating-point operations
856 void fld(int i);
Andrei Popescu402d9372010-02-26 13:31:12 +0000857 void fstp(int i);
Steve Blocka7e24c12009-10-30 11:49:00 +0000858
859 void fld1();
860 void fldz();
Andrei Popescu402d9372010-02-26 13:31:12 +0000861 void fldpi();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100862 void fldln2();
Steve Blocka7e24c12009-10-30 11:49:00 +0000863
864 void fld_s(const Operand& adr);
865 void fld_d(const Operand& adr);
866
867 void fstp_s(const Operand& adr);
868 void fstp_d(const Operand& adr);
Andrei Popescu402d9372010-02-26 13:31:12 +0000869 void fst_d(const Operand& adr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000870
871 void fild_s(const Operand& adr);
872 void fild_d(const Operand& adr);
873
874 void fist_s(const Operand& adr);
875
876 void fistp_s(const Operand& adr);
877 void fistp_d(const Operand& adr);
878
Steve Block6ded16b2010-05-10 14:33:55 +0100879 // The fisttp instructions require SSE3.
Steve Blocka7e24c12009-10-30 11:49:00 +0000880 void fisttp_s(const Operand& adr);
Leon Clarkee46be812010-01-19 14:06:41 +0000881 void fisttp_d(const Operand& adr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000882
883 void fabs();
884 void fchs();
885 void fcos();
886 void fsin();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100887 void fyl2x();
Steve Blocka7e24c12009-10-30 11:49:00 +0000888
889 void fadd(int i);
890 void fsub(int i);
891 void fmul(int i);
892 void fdiv(int i);
893
894 void fisub_s(const Operand& adr);
895
896 void faddp(int i = 1);
897 void fsubp(int i = 1);
898 void fsubrp(int i = 1);
899 void fmulp(int i = 1);
900 void fdivp(int i = 1);
901 void fprem();
902 void fprem1();
903
904 void fxch(int i = 1);
905 void fincstp();
906 void ffree(int i = 0);
907
908 void ftst();
909 void fucomp(int i);
910 void fucompp();
Steve Block3ce2e202009-11-05 08:53:23 +0000911 void fucomi(int i);
912 void fucomip();
Steve Blocka7e24c12009-10-30 11:49:00 +0000913 void fcompp();
914 void fnstsw_ax();
915 void fwait();
916 void fnclex();
917
918 void frndint();
919
920 void sahf();
921 void setcc(Condition cc, Register reg);
922
923 void cpuid();
924
925 // SSE2 instructions
926 void cvttss2si(Register dst, const Operand& src);
927 void cvttsd2si(Register dst, const Operand& src);
928
929 void cvtsi2sd(XMMRegister dst, const Operand& src);
Steve Block6ded16b2010-05-10 14:33:55 +0100930 void cvtss2sd(XMMRegister dst, XMMRegister src);
Steve Block44f0eee2011-05-26 01:26:41 +0100931 void cvtsd2ss(XMMRegister dst, XMMRegister src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000932
933 void addsd(XMMRegister dst, XMMRegister src);
934 void subsd(XMMRegister dst, XMMRegister src);
935 void mulsd(XMMRegister dst, XMMRegister src);
936 void divsd(XMMRegister dst, XMMRegister src);
Leon Clarkee46be812010-01-19 14:06:41 +0000937 void xorpd(XMMRegister dst, XMMRegister src);
Ben Murdoch257744e2011-11-30 15:57:28 +0000938 void xorps(XMMRegister dst, XMMRegister src);
Steve Block6ded16b2010-05-10 14:33:55 +0100939 void sqrtsd(XMMRegister dst, XMMRegister src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000940
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100941 void andpd(XMMRegister dst, XMMRegister src);
942
Steve Block6ded16b2010-05-10 14:33:55 +0100943 void ucomisd(XMMRegister dst, XMMRegister src);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000944
945 enum RoundingMode {
946 kRoundToNearest = 0x0,
947 kRoundDown = 0x1,
948 kRoundUp = 0x2,
949 kRoundToZero = 0x3
950 };
951
952 void roundsd(XMMRegister dst, XMMRegister src, RoundingMode mode);
953
Steve Block6ded16b2010-05-10 14:33:55 +0100954 void movmskpd(Register dst, XMMRegister src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000955
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100956 void cmpltsd(XMMRegister dst, XMMRegister src);
957
958 void movaps(XMMRegister dst, XMMRegister src);
959
Leon Clarkee46be812010-01-19 14:06:41 +0000960 void movdqa(XMMRegister dst, const Operand& src);
961 void movdqa(const Operand& dst, XMMRegister src);
962 void movdqu(XMMRegister dst, const Operand& src);
963 void movdqu(const Operand& dst, XMMRegister src);
964
Steve Blocka7e24c12009-10-30 11:49:00 +0000965 // Use either movsd or movlpd.
966 void movdbl(XMMRegister dst, const Operand& src);
967 void movdbl(const Operand& dst, XMMRegister src);
968
Steve Block6ded16b2010-05-10 14:33:55 +0100969 void movd(XMMRegister dst, const Operand& src);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100970 void movd(const Operand& src, XMMRegister dst);
Steve Block6ded16b2010-05-10 14:33:55 +0100971 void movsd(XMMRegister dst, XMMRegister src);
972
Steve Block44f0eee2011-05-26 01:26:41 +0100973 void movss(XMMRegister dst, const Operand& src);
974 void movss(const Operand& src, XMMRegister dst);
975 void movss(XMMRegister dst, XMMRegister src);
976
Ben Murdochb0fe1622011-05-05 13:52:32 +0100977 void pand(XMMRegister dst, XMMRegister src);
Steve Block6ded16b2010-05-10 14:33:55 +0100978 void pxor(XMMRegister dst, XMMRegister src);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100979 void por(XMMRegister dst, XMMRegister src);
Steve Block6ded16b2010-05-10 14:33:55 +0100980 void ptest(XMMRegister dst, XMMRegister src);
981
Ben Murdochb0fe1622011-05-05 13:52:32 +0100982 void psllq(XMMRegister reg, int8_t shift);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100983 void psllq(XMMRegister dst, XMMRegister src);
984 void psrlq(XMMRegister reg, int8_t shift);
985 void psrlq(XMMRegister dst, XMMRegister src);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100986 void pshufd(XMMRegister dst, XMMRegister src, int8_t shuffle);
987 void pextrd(const Operand& dst, XMMRegister src, int8_t offset);
Steve Block1e0659c2011-05-24 12:43:12 +0100988 void pinsrd(XMMRegister dst, const Operand& src, int8_t offset);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100989
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100990 // Parallel XMM operations.
991 void movntdqa(XMMRegister src, const Operand& dst);
992 void movntdq(const Operand& dst, XMMRegister src);
993 // Prefetch src position into cache level.
994 // Level 1, 2 or 3 specifies CPU cache level. Level 0 specifies a
995 // non-temporal
996 void prefetch(const Operand& src, int level);
997 // TODO(lrn): Need SFENCE for movnt?
998
Steve Blocka7e24c12009-10-30 11:49:00 +0000999 // Debugging
1000 void Print();
1001
1002 // Check the code size generated from label to here.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001003 int SizeOfCodeGeneratedSince(Label* label) {
1004 return pc_offset() - label->pos();
1005 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001006
1007 // Mark address of the ExitJSFrame code.
1008 void RecordJSReturn();
1009
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001010 // Mark address of a debug break slot.
1011 void RecordDebugBreakSlot();
1012
Steve Blocka7e24c12009-10-30 11:49:00 +00001013 // Record a comment relocation entry that can be used by a disassembler.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001014 // Use --code-comments to enable, or provide "force = true" flag to always
1015 // write a comment.
1016 void RecordComment(const char* msg, bool force = false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001017
Ben Murdochb0fe1622011-05-05 13:52:32 +01001018 // Writes a single byte or word of data in the code stream. Used for
1019 // inline tables, e.g., jump-tables.
1020 void db(uint8_t data);
1021 void dd(uint32_t data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001022
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001023 int pc_offset() const { return pc_ - buffer_; }
Steve Blocka7e24c12009-10-30 11:49:00 +00001024
1025 // Check if there is less than kGap bytes available in the buffer.
1026 // If this is the case, we need to grow the buffer before emitting
1027 // an instruction or relocation information.
1028 inline bool overflow() const { return pc_ >= reloc_info_writer.pos() - kGap; }
1029
1030 // Get the number of bytes available in the buffer.
1031 inline int available_space() const { return reloc_info_writer.pos() - pc_; }
1032
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001033 static bool IsNop(Address addr) { return *addr == 0x90; }
1034
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001035 PositionsRecorder* positions_recorder() { return &positions_recorder_; }
1036
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001037 int relocation_writer_size() {
1038 return (buffer_ + buffer_size_) - reloc_info_writer.pos();
1039 }
1040
Steve Blocka7e24c12009-10-30 11:49:00 +00001041 // Avoid overflows for displacements etc.
1042 static const int kMaximalBufferSize = 512*MB;
1043 static const int kMinimalBufferSize = 4*KB;
1044
1045 protected:
Steve Block44f0eee2011-05-26 01:26:41 +01001046 bool emit_debug_code() const { return emit_debug_code_; }
1047
Steve Blocka7e24c12009-10-30 11:49:00 +00001048 void movsd(XMMRegister dst, const Operand& src);
1049 void movsd(const Operand& dst, XMMRegister src);
1050
1051 void emit_sse_operand(XMMRegister reg, const Operand& adr);
1052 void emit_sse_operand(XMMRegister dst, XMMRegister src);
Steve Block6ded16b2010-05-10 14:33:55 +01001053 void emit_sse_operand(Register dst, XMMRegister src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001054
Steve Block44f0eee2011-05-26 01:26:41 +01001055 byte* addr_at(int pos) { return buffer_ + pos; }
1056
Ben Murdochb0fe1622011-05-05 13:52:32 +01001057 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00001058 byte byte_at(int pos) { return buffer_[pos]; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001059 void set_byte_at(int pos, byte value) { buffer_[pos] = value; }
Steve Blocka7e24c12009-10-30 11:49:00 +00001060 uint32_t long_at(int pos) {
1061 return *reinterpret_cast<uint32_t*>(addr_at(pos));
1062 }
1063 void long_at_put(int pos, uint32_t x) {
1064 *reinterpret_cast<uint32_t*>(addr_at(pos)) = x;
1065 }
1066
1067 // code emission
1068 void GrowBuffer();
1069 inline void emit(uint32_t x);
1070 inline void emit(Handle<Object> handle);
Ben Murdoch257744e2011-11-30 15:57:28 +00001071 inline void emit(uint32_t x,
1072 RelocInfo::Mode rmode,
1073 unsigned ast_id = kNoASTId);
Steve Blocka7e24c12009-10-30 11:49:00 +00001074 inline void emit(const Immediate& x);
1075 inline void emit_w(const Immediate& x);
1076
1077 // Emit the code-object-relative offset of the label's position
1078 inline void emit_code_relative_offset(Label* label);
1079
1080 // instruction generation
1081 void emit_arith_b(int op1, int op2, Register dst, int imm8);
1082
1083 // Emit a basic arithmetic instruction (i.e. first byte of the family is 0x81)
1084 // with a given destination expression and an immediate operand. It attempts
1085 // to use the shortest encoding possible.
1086 // sel specifies the /n in the modrm byte (see the Intel PRM).
1087 void emit_arith(int sel, Operand dst, const Immediate& x);
1088
1089 void emit_operand(Register reg, const Operand& adr);
1090
1091 void emit_farith(int b1, int b2, int i);
1092
1093 // labels
1094 void print(Label* L);
1095 void bind_to(Label* L, int pos);
Steve Blocka7e24c12009-10-30 11:49:00 +00001096
1097 // displacements
1098 inline Displacement disp_at(Label* L);
1099 inline void disp_at_put(Label* L, Displacement disp);
1100 inline void emit_disp(Label* L, Displacement::Type type);
Ben Murdoch257744e2011-11-30 15:57:28 +00001101 inline void emit_near_disp(Label* L);
Steve Blocka7e24c12009-10-30 11:49:00 +00001102
1103 // record reloc info for current pc_
1104 void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
1105
1106 friend class CodePatcher;
1107 friend class EnsureSpace;
1108
1109 // Code buffer:
1110 // The buffer into which code and relocation info are generated.
1111 byte* buffer_;
1112 int buffer_size_;
1113 // True if the assembler owns the buffer, false if buffer is external.
1114 bool own_buffer_;
Steve Blocka7e24c12009-10-30 11:49:00 +00001115
1116 // code generation
1117 byte* pc_; // the program counter; moves forward
1118 RelocInfoWriter reloc_info_writer;
1119
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001120 PositionsRecorder positions_recorder_;
1121
Steve Block44f0eee2011-05-26 01:26:41 +01001122 bool emit_debug_code_;
1123
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001124 friend class PositionsRecorder;
Steve Blocka7e24c12009-10-30 11:49:00 +00001125};
1126
1127
1128// Helper class that ensures that there is enough space for generating
1129// instructions and relocation information. The constructor makes
1130// sure that there is enough space and (in debug mode) the destructor
1131// checks that we did not generate too much.
1132class EnsureSpace BASE_EMBEDDED {
1133 public:
1134 explicit EnsureSpace(Assembler* assembler) : assembler_(assembler) {
1135 if (assembler_->overflow()) assembler_->GrowBuffer();
1136#ifdef DEBUG
1137 space_before_ = assembler_->available_space();
1138#endif
1139 }
1140
1141#ifdef DEBUG
1142 ~EnsureSpace() {
1143 int bytes_generated = space_before_ - assembler_->available_space();
1144 ASSERT(bytes_generated < assembler_->kGap);
1145 }
1146#endif
1147
1148 private:
1149 Assembler* assembler_;
1150#ifdef DEBUG
1151 int space_before_;
1152#endif
1153};
1154
1155} } // namespace v8::internal
1156
1157#endif // V8_IA32_ASSEMBLER_IA32_H_