Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // 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 Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 33 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | |
| 35 | // A light-weight IA32 Assembler. |
| 36 | |
| 37 | #ifndef V8_IA32_ASSEMBLER_IA32_H_ |
| 38 | #define V8_IA32_ASSEMBLER_IA32_H_ |
| 39 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 40 | #include "isolate.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 41 | #include "serialize.h" |
| 42 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 43 | namespace v8 { |
| 44 | namespace 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 | // |
| 67 | struct Register { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 68 | static const int kNumAllocatableRegisters = 6; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 69 | static const int kNumRegisters = 8; |
| 70 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 71 | static inline const char* AllocationIndexToString(int index); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 72 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 73 | static inline int ToAllocationIndex(Register reg); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 74 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 75 | static inline Register FromAllocationIndex(int index); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 76 | |
| 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 Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 82 | bool is(Register reg) const { return code_ == reg.code_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | // eax, ebx, ecx and edx are byte registers, the rest are not. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 84 | bool is_byte_register() const { return code_ <= 3; } |
| 85 | int code() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | ASSERT(is_valid()); |
| 87 | return code_; |
| 88 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 89 | int bit() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 90 | ASSERT(is_valid()); |
| 91 | return 1 << code_; |
| 92 | } |
| 93 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 94 | // Unfortunately we can't make this private in a struct. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | int code_; |
| 96 | }; |
| 97 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 98 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 99 | const Register eax = { 0 }; |
| 100 | const Register ecx = { 1 }; |
| 101 | const Register edx = { 2 }; |
| 102 | const Register ebx = { 3 }; |
| 103 | const Register esp = { 4 }; |
| 104 | const Register ebp = { 5 }; |
| 105 | const Register esi = { 6 }; |
| 106 | const Register edi = { 7 }; |
| 107 | const Register no_reg = { -1 }; |
| 108 | |
| 109 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 110 | inline 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 | |
| 118 | inline 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 | |
| 124 | inline 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | struct XMMRegister { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 131 | 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 Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 165 | int code() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 166 | ASSERT(is_valid()); |
| 167 | return code_; |
| 168 | } |
| 169 | |
| 170 | int code_; |
| 171 | }; |
| 172 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 173 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 174 | const XMMRegister xmm0 = { 0 }; |
| 175 | const XMMRegister xmm1 = { 1 }; |
| 176 | const XMMRegister xmm2 = { 2 }; |
| 177 | const XMMRegister xmm3 = { 3 }; |
| 178 | const XMMRegister xmm4 = { 4 }; |
| 179 | const XMMRegister xmm5 = { 5 }; |
| 180 | const XMMRegister xmm6 = { 6 }; |
| 181 | const XMMRegister xmm7 = { 7 }; |
| 182 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 183 | |
| 184 | typedef XMMRegister DoubleRegister; |
| 185 | |
| 186 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | enum 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 Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 222 | inline Condition NegateCondition(Condition cc) { |
| 223 | return static_cast<Condition>(cc ^ 1); |
| 224 | } |
| 225 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | |
| 227 | // Corresponds to transposing the operands of a comparison. |
| 228 | inline 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 Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 251 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 252 | // ----------------------------------------------------------------------------- |
| 253 | // Machine instruction Immediates |
| 254 | |
| 255 | class Immediate BASE_EMBEDDED { |
| 256 | public: |
| 257 | inline explicit Immediate(int x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 258 | inline explicit Immediate(const ExternalReference& ext); |
| 259 | inline explicit Immediate(Handle<Object> handle); |
| 260 | inline explicit Immediate(Smi* value); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 261 | inline explicit Immediate(Address addr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 262 | |
| 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 Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 282 | friend class MacroAssembler; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | |
| 286 | // ----------------------------------------------------------------------------- |
| 287 | // Machine instruction Operands |
| 288 | |
| 289 | enum ScaleFactor { |
| 290 | times_1 = 0, |
| 291 | times_2 = 1, |
| 292 | times_4 = 2, |
| 293 | times_8 = 3, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 294 | times_int_size = times_4, |
| 295 | times_half_pointer_size = times_2, |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 296 | times_pointer_size = times_4, |
| 297 | times_twice_pointer_size = times_8 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | |
| 301 | class Operand BASE_EMBEDDED { |
| 302 | public: |
| 303 | // reg |
| 304 | INLINE(explicit Operand(Register reg)); |
| 305 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 306 | // XMM reg |
| 307 | INLINE(explicit Operand(XMMRegister xmm_reg)); |
| 308 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 309 | // [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 Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 342 | static Operand Cell(Handle<JSGlobalPropertyCell> cell) { |
| 343 | return Operand(reinterpret_cast<int32_t>(cell.location()), |
| 344 | RelocInfo::GLOBAL_PROPERTY_CELL); |
| 345 | } |
| 346 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 347 | // 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 | |
| 388 | class 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 Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 433 | class CpuFeatures : public AllStatic { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 434 | public: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 435 | // Detect features of the target CPU. Set safe defaults if the serializer |
| 436 | // is enabled (snapshots must be portable). |
| 437 | static void Probe(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 438 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 439 | // Check whether a feature is supported by the target CPU. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 440 | static bool IsSupported(CpuFeature f) { |
| 441 | ASSERT(initialized_); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 442 | if (f == SSE2 && !FLAG_enable_sse2) return false; |
| 443 | if (f == SSE3 && !FLAG_enable_sse3) return false; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 444 | if (f == SSE4_1 && !FLAG_enable_sse4_1) return false; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 445 | if (f == CMOV && !FLAG_enable_cmov) return false; |
| 446 | if (f == RDTSC && !FLAG_enable_rdtsc) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 447 | return (supported_ & (static_cast<uint64_t>(1) << f)) != 0; |
| 448 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 449 | |
| 450 | #ifdef DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 451 | // Check whether a feature is currently enabled. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 452 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 462 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 463 | #endif |
| 464 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 465 | // Enable a specified feature within a scope. |
| 466 | class Scope BASE_EMBEDDED { |
| 467 | #ifdef DEBUG |
| 468 | public: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 469 | explicit Scope(CpuFeature f) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 470 | uint64_t mask = static_cast<uint64_t>(1) << f; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 471 | ASSERT(CpuFeatures::IsSupported(f)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 472 | ASSERT(!Serializer::enabled() || |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 473 | (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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 480 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 481 | ~Scope() { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 482 | ASSERT_EQ(Isolate::UncheckedCurrent(), isolate_); |
| 483 | if (isolate_ != NULL) { |
| 484 | isolate_->set_enabled_cpu_features(old_enabled_); |
| 485 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 486 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 487 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 488 | Isolate* isolate_; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 489 | uint64_t old_enabled_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 490 | #else |
| 491 | public: |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 492 | explicit Scope(CpuFeature f) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 493 | #endif |
| 494 | }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 495 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 496 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 522 | private: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 523 | #ifdef DEBUG |
| 524 | static bool initialized_; |
| 525 | #endif |
| 526 | static uint64_t supported_; |
| 527 | static uint64_t found_by_runtime_probing_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 528 | |
| 529 | DISALLOW_COPY_AND_ASSIGN(CpuFeatures); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 530 | }; |
| 531 | |
| 532 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 533 | class Assembler : public AssemblerBase { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 534 | 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 Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 560 | // TODO(vitalyr): the assembler does not need an isolate. |
| 561 | Assembler(Isolate* isolate, void* buffer, int buffer_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 562 | ~Assembler(); |
| 563 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 564 | // Overrides the default provided by FLAG_debug_code. |
| 565 | void set_emit_debug_code(bool value) { emit_debug_code_ = value; } |
| 566 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 567 | // 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 Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 576 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 593 | // 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 Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 600 | // 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 Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 604 | static const int kCallInstructionLength = 5; |
| 605 | static const int kJSReturnSequenceLength = 6; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 606 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 607 | // The debug break slot must be able to contain a call instruction. |
| 608 | static const int kDebugBreakSlotLength = kCallInstructionLength; |
| 609 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 610 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 624 | // --------------------------------------------------------------------------- |
| 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 Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 648 | // Aligns code to something that's optimal for a jump target for the platform. |
| 649 | void CodeTargetAlign(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 650 | |
| 651 | // Stack |
| 652 | void pushad(); |
| 653 | void popad(); |
| 654 | |
| 655 | void pushfd(); |
| 656 | void popfd(); |
| 657 | |
| 658 | void push(const Immediate& x); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 659 | void push_imm32(int32_t imm32); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 660 | void push(Register src); |
| 661 | void push(const Operand& src); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 662 | void push(Handle<Object> handle); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 663 | |
| 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 700 | // Flag management. |
| 701 | void cld(); |
| 702 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 703 | // Repetitive string instructions. |
| 704 | void rep_movs(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 705 | void rep_stos(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 706 | void stos(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 707 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 708 | // 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 Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 719 | void and_(Register dst, const Immediate& x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 720 | 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 Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 725 | void cmpb(Register src, const Operand& dst); |
| 726 | void cmpb(const Operand& dst, Register src); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 727 | 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 Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 737 | void dec_b(const Operand& dst); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 738 | |
| 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 Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 769 | void rcr(Register dst, uint8_t imm8); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 770 | |
| 771 | void sar(Register dst, uint8_t imm8); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 772 | void sar_cl(Register dst); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 773 | |
| 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 Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 779 | void shl_cl(Register dst); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 780 | |
| 781 | void shrd(Register dst, const Operand& src); |
| 782 | |
| 783 | void shr(Register dst, uint8_t imm8); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 784 | void shr_cl(Register dst); |
| 785 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 786 | void subb(const Operand& dst, int8_t imm8); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 787 | void subb(Register dst, const Operand& src); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 788 | 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 Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 794 | void test_b(Register reg, const Operand& op); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 795 | void test(const Operand& op, const Immediate& imm); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 796 | void test_b(const Operand& op, uint8_t imm8); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 797 | |
| 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 834 | int CallSize(const Operand& adr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 835 | void call(const Operand& adr); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 836 | int CallSize(Handle<Code> code, RelocInfo::Mode mode); |
| 837 | void call(Handle<Code> code, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 838 | RelocInfo::Mode rmode = RelocInfo::CODE_TARGET, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 839 | unsigned ast_id = kNoASTId); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 840 | |
| 841 | // Jumps |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 842 | // unconditional jump to L |
| 843 | void jmp(Label* L, Label::Distance distance = Label::kFar); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 844 | 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 849 | 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 Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 854 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 855 | // Floating-point operations |
| 856 | void fld(int i); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 857 | void fstp(int i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 858 | |
| 859 | void fld1(); |
| 860 | void fldz(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 861 | void fldpi(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 862 | void fldln2(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 863 | |
| 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 Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 869 | void fst_d(const Operand& adr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 870 | |
| 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 879 | // The fisttp instructions require SSE3. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 880 | void fisttp_s(const Operand& adr); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 881 | void fisttp_d(const Operand& adr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 882 | |
| 883 | void fabs(); |
| 884 | void fchs(); |
| 885 | void fcos(); |
| 886 | void fsin(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 887 | void fyl2x(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 888 | |
| 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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 911 | void fucomi(int i); |
| 912 | void fucomip(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 913 | 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 930 | void cvtss2sd(XMMRegister dst, XMMRegister src); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 931 | void cvtsd2ss(XMMRegister dst, XMMRegister src); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 932 | |
| 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 Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 937 | void xorpd(XMMRegister dst, XMMRegister src); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 938 | void xorps(XMMRegister dst, XMMRegister src); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 939 | void sqrtsd(XMMRegister dst, XMMRegister src); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 940 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 941 | void andpd(XMMRegister dst, XMMRegister src); |
| 942 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 943 | void ucomisd(XMMRegister dst, XMMRegister src); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 944 | |
| 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 954 | void movmskpd(Register dst, XMMRegister src); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 955 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 956 | void cmpltsd(XMMRegister dst, XMMRegister src); |
| 957 | |
| 958 | void movaps(XMMRegister dst, XMMRegister src); |
| 959 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 960 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 965 | // Use either movsd or movlpd. |
| 966 | void movdbl(XMMRegister dst, const Operand& src); |
| 967 | void movdbl(const Operand& dst, XMMRegister src); |
| 968 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 969 | void movd(XMMRegister dst, const Operand& src); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 970 | void movd(const Operand& src, XMMRegister dst); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 971 | void movsd(XMMRegister dst, XMMRegister src); |
| 972 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 973 | void movss(XMMRegister dst, const Operand& src); |
| 974 | void movss(const Operand& src, XMMRegister dst); |
| 975 | void movss(XMMRegister dst, XMMRegister src); |
| 976 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 977 | void pand(XMMRegister dst, XMMRegister src); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 978 | void pxor(XMMRegister dst, XMMRegister src); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 979 | void por(XMMRegister dst, XMMRegister src); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 980 | void ptest(XMMRegister dst, XMMRegister src); |
| 981 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 982 | void psllq(XMMRegister reg, int8_t shift); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 983 | void psllq(XMMRegister dst, XMMRegister src); |
| 984 | void psrlq(XMMRegister reg, int8_t shift); |
| 985 | void psrlq(XMMRegister dst, XMMRegister src); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 986 | void pshufd(XMMRegister dst, XMMRegister src, int8_t shuffle); |
| 987 | void pextrd(const Operand& dst, XMMRegister src, int8_t offset); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 988 | void pinsrd(XMMRegister dst, const Operand& src, int8_t offset); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 989 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 990 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 999 | // Debugging |
| 1000 | void Print(); |
| 1001 | |
| 1002 | // Check the code size generated from label to here. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1003 | int SizeOfCodeGeneratedSince(Label* label) { |
| 1004 | return pc_offset() - label->pos(); |
| 1005 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1006 | |
| 1007 | // Mark address of the ExitJSFrame code. |
| 1008 | void RecordJSReturn(); |
| 1009 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1010 | // Mark address of a debug break slot. |
| 1011 | void RecordDebugBreakSlot(); |
| 1012 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1013 | // Record a comment relocation entry that can be used by a disassembler. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1014 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1017 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1018 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1022 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1023 | int pc_offset() const { return pc_ - buffer_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1024 | |
| 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 Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1033 | static bool IsNop(Address addr) { return *addr == 0x90; } |
| 1034 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1035 | PositionsRecorder* positions_recorder() { return &positions_recorder_; } |
| 1036 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1037 | int relocation_writer_size() { |
| 1038 | return (buffer_ + buffer_size_) - reloc_info_writer.pos(); |
| 1039 | } |
| 1040 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1041 | // Avoid overflows for displacements etc. |
| 1042 | static const int kMaximalBufferSize = 512*MB; |
| 1043 | static const int kMinimalBufferSize = 4*KB; |
| 1044 | |
| 1045 | protected: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1046 | bool emit_debug_code() const { return emit_debug_code_; } |
| 1047 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1048 | 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1053 | void emit_sse_operand(Register dst, XMMRegister src); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1054 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1055 | byte* addr_at(int pos) { return buffer_ + pos; } |
| 1056 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1057 | private: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1058 | byte byte_at(int pos) { return buffer_[pos]; } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1059 | void set_byte_at(int pos, byte value) { buffer_[pos] = value; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1060 | 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1071 | inline void emit(uint32_t x, |
| 1072 | RelocInfo::Mode rmode, |
| 1073 | unsigned ast_id = kNoASTId); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1074 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1096 | |
| 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1101 | inline void emit_near_disp(Label* L); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1102 | |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1115 | |
| 1116 | // code generation |
| 1117 | byte* pc_; // the program counter; moves forward |
| 1118 | RelocInfoWriter reloc_info_writer; |
| 1119 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1120 | PositionsRecorder positions_recorder_; |
| 1121 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1122 | bool emit_debug_code_; |
| 1123 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1124 | friend class PositionsRecorder; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1125 | }; |
| 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. |
| 1132 | class 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_ |