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