Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +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 2011 the V8 project authors. All rights reserved. |
| 34 | |
| 35 | // A light-weight IA32 Assembler. |
| 36 | |
| 37 | #ifndef V8_X87_ASSEMBLER_X87_H_ |
| 38 | #define V8_X87_ASSEMBLER_X87_H_ |
| 39 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 40 | #include <deque> |
| 41 | |
| 42 | #include "src/assembler.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | #include "src/isolate.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 44 | #include "src/utils.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 45 | |
| 46 | namespace v8 { |
| 47 | namespace internal { |
| 48 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 49 | #define GENERAL_REGISTERS(V) \ |
| 50 | V(eax) \ |
| 51 | V(ecx) \ |
| 52 | V(edx) \ |
| 53 | V(ebx) \ |
| 54 | V(esp) \ |
| 55 | V(ebp) \ |
| 56 | V(esi) \ |
| 57 | V(edi) |
| 58 | |
| 59 | #define ALLOCATABLE_GENERAL_REGISTERS(V) \ |
| 60 | V(eax) \ |
| 61 | V(ecx) \ |
| 62 | V(edx) \ |
| 63 | V(ebx) \ |
| 64 | V(esi) \ |
| 65 | V(edi) |
| 66 | |
| 67 | #define DOUBLE_REGISTERS(V) \ |
| 68 | V(stX_0) \ |
| 69 | V(stX_1) \ |
| 70 | V(stX_2) \ |
| 71 | V(stX_3) \ |
| 72 | V(stX_4) \ |
| 73 | V(stX_5) \ |
| 74 | V(stX_6) \ |
| 75 | V(stX_7) |
| 76 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 77 | #define FLOAT_REGISTERS DOUBLE_REGISTERS |
| 78 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 79 | #define ALLOCATABLE_DOUBLE_REGISTERS(V) \ |
| 80 | V(stX_0) \ |
| 81 | V(stX_1) \ |
| 82 | V(stX_2) \ |
| 83 | V(stX_3) \ |
| 84 | V(stX_4) \ |
| 85 | V(stX_5) |
| 86 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 87 | // CPU Registers. |
| 88 | // |
| 89 | // 1) We would prefer to use an enum, but enum values are assignment- |
| 90 | // compatible with int, which has caused code-generation bugs. |
| 91 | // |
| 92 | // 2) We would prefer to use a class instead of a struct but we don't like |
| 93 | // the register initialization to depend on the particular initialization |
| 94 | // order (which appears to be different on OS X, Linux, and Windows for the |
| 95 | // installed versions of C++ we tried). Using a struct permits C-style |
| 96 | // "initialization". Also, the Register objects cannot be const as this |
| 97 | // forces initialization stubs in MSVC, making us dependent on initialization |
| 98 | // order. |
| 99 | // |
| 100 | // 3) By not using an enum, we are possibly preventing the compiler from |
| 101 | // doing certain constant folds, which may significantly reduce the |
| 102 | // code generated for some assembly instructions (because they boil down |
| 103 | // to a few constants). If this is a problem, we could change the code |
| 104 | // such that we use an enum in optimized mode, and the struct in debug |
| 105 | // mode. This way we get the compile-time error checking in debug mode |
| 106 | // and best performance in optimized code. |
| 107 | // |
| 108 | struct Register { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 109 | enum Code { |
| 110 | #define REGISTER_CODE(R) kCode_##R, |
| 111 | GENERAL_REGISTERS(REGISTER_CODE) |
| 112 | #undef REGISTER_CODE |
| 113 | kAfterLast, |
| 114 | kCode_no_reg = -1 |
| 115 | }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 116 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 117 | static const int kNumRegisters = Code::kAfterLast; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 118 | |
| 119 | static Register from_code(int code) { |
| 120 | DCHECK(code >= 0); |
| 121 | DCHECK(code < kNumRegisters); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 122 | Register r = {code}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 123 | return r; |
| 124 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 125 | const char* ToString(); |
| 126 | bool IsAllocatable() const; |
| 127 | bool is_valid() const { return 0 <= reg_code && reg_code < kNumRegisters; } |
| 128 | bool is(Register reg) const { return reg_code == reg.reg_code; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 129 | int code() const { |
| 130 | DCHECK(is_valid()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 131 | return reg_code; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 132 | } |
| 133 | int bit() const { |
| 134 | DCHECK(is_valid()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 135 | return 1 << reg_code; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 138 | bool is_byte_register() const { return reg_code <= 3; } |
| 139 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 140 | // Unfortunately we can't make this private in a struct. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 141 | int reg_code; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 144 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 145 | #define DECLARE_REGISTER(R) const Register R = {Register::kCode_##R}; |
| 146 | GENERAL_REGISTERS(DECLARE_REGISTER) |
| 147 | #undef DECLARE_REGISTER |
| 148 | const Register no_reg = {Register::kCode_no_reg}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 149 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 150 | struct X87Register { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 151 | enum Code { |
| 152 | #define REGISTER_CODE(R) kCode_##R, |
| 153 | DOUBLE_REGISTERS(REGISTER_CODE) |
| 154 | #undef REGISTER_CODE |
| 155 | kAfterLast, |
| 156 | kCode_no_reg = -1 |
| 157 | }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 158 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 159 | static const int kMaxNumRegisters = Code::kAfterLast; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 160 | static const int kMaxNumAllocatableRegisters = 6; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 161 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 162 | static X87Register from_code(int code) { |
| 163 | X87Register result = {code}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 164 | return result; |
| 165 | } |
| 166 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 167 | bool IsAllocatable() const; |
| 168 | bool is_valid() const { return 0 <= reg_code && reg_code < kMaxNumRegisters; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 169 | |
| 170 | int code() const { |
| 171 | DCHECK(is_valid()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 172 | return reg_code; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 175 | bool is(X87Register reg) const { return reg_code == reg.reg_code; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 176 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 177 | const char* ToString(); |
| 178 | |
| 179 | int reg_code; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 182 | typedef X87Register FloatRegister; |
| 183 | |
| 184 | typedef X87Register DoubleRegister; |
| 185 | |
| 186 | // TODO(x87) Define SIMD registers. |
| 187 | typedef X87Register Simd128Register; |
| 188 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 189 | #define DECLARE_REGISTER(R) \ |
| 190 | const DoubleRegister R = {DoubleRegister::kCode_##R}; |
| 191 | DOUBLE_REGISTERS(DECLARE_REGISTER) |
| 192 | #undef DECLARE_REGISTER |
| 193 | const DoubleRegister no_double_reg = {DoubleRegister::kCode_no_reg}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 194 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 195 | enum Condition { |
| 196 | // any value < 0 is considered no_condition |
| 197 | no_condition = -1, |
| 198 | |
| 199 | overflow = 0, |
| 200 | no_overflow = 1, |
| 201 | below = 2, |
| 202 | above_equal = 3, |
| 203 | equal = 4, |
| 204 | not_equal = 5, |
| 205 | below_equal = 6, |
| 206 | above = 7, |
| 207 | negative = 8, |
| 208 | positive = 9, |
| 209 | parity_even = 10, |
| 210 | parity_odd = 11, |
| 211 | less = 12, |
| 212 | greater_equal = 13, |
| 213 | less_equal = 14, |
| 214 | greater = 15, |
| 215 | |
| 216 | // aliases |
| 217 | carry = below, |
| 218 | not_carry = above_equal, |
| 219 | zero = equal, |
| 220 | not_zero = not_equal, |
| 221 | sign = negative, |
| 222 | not_sign = positive |
| 223 | }; |
| 224 | |
| 225 | |
| 226 | // Returns the equivalent of !cc. |
| 227 | // Negation of the default no_condition (-1) results in a non-default |
| 228 | // no_condition value (-2). As long as tests for no_condition check |
| 229 | // for condition < 0, this will work as expected. |
| 230 | inline Condition NegateCondition(Condition cc) { |
| 231 | return static_cast<Condition>(cc ^ 1); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | // Commute a condition such that {a cond b == b cond' a}. |
| 236 | inline Condition CommuteCondition(Condition cc) { |
| 237 | switch (cc) { |
| 238 | case below: |
| 239 | return above; |
| 240 | case above: |
| 241 | return below; |
| 242 | case above_equal: |
| 243 | return below_equal; |
| 244 | case below_equal: |
| 245 | return above_equal; |
| 246 | case less: |
| 247 | return greater; |
| 248 | case greater: |
| 249 | return less; |
| 250 | case greater_equal: |
| 251 | return less_equal; |
| 252 | case less_equal: |
| 253 | return greater_equal; |
| 254 | default: |
| 255 | return cc; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 260 | enum RoundingMode { |
| 261 | kRoundToNearest = 0x0, |
| 262 | kRoundDown = 0x1, |
| 263 | kRoundUp = 0x2, |
| 264 | kRoundToZero = 0x3 |
| 265 | }; |
| 266 | |
| 267 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 268 | // ----------------------------------------------------------------------------- |
| 269 | // Machine instruction Immediates |
| 270 | |
| 271 | class Immediate BASE_EMBEDDED { |
| 272 | public: |
| 273 | inline explicit Immediate(int x); |
| 274 | inline explicit Immediate(const ExternalReference& ext); |
| 275 | inline explicit Immediate(Handle<Object> handle); |
| 276 | inline explicit Immediate(Smi* value); |
| 277 | inline explicit Immediate(Address addr); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 278 | inline explicit Immediate(Address x, RelocInfo::Mode rmode); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 279 | |
| 280 | static Immediate CodeRelativeOffset(Label* label) { |
| 281 | return Immediate(label); |
| 282 | } |
| 283 | |
| 284 | bool is_zero() const { return x_ == 0 && RelocInfo::IsNone(rmode_); } |
| 285 | bool is_int8() const { |
| 286 | return -128 <= x_ && x_ < 128 && RelocInfo::IsNone(rmode_); |
| 287 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 288 | bool is_uint8() const { |
| 289 | return v8::internal::is_uint8(x_) && RelocInfo::IsNone(rmode_); |
| 290 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 291 | bool is_int16() const { |
| 292 | return -32768 <= x_ && x_ < 32768 && RelocInfo::IsNone(rmode_); |
| 293 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 294 | bool is_uint16() const { |
| 295 | return v8::internal::is_uint16(x_) && RelocInfo::IsNone(rmode_); |
| 296 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 297 | |
| 298 | private: |
| 299 | inline explicit Immediate(Label* value); |
| 300 | |
| 301 | int x_; |
| 302 | RelocInfo::Mode rmode_; |
| 303 | |
| 304 | friend class Operand; |
| 305 | friend class Assembler; |
| 306 | friend class MacroAssembler; |
| 307 | }; |
| 308 | |
| 309 | |
| 310 | // ----------------------------------------------------------------------------- |
| 311 | // Machine instruction Operands |
| 312 | |
| 313 | enum ScaleFactor { |
| 314 | times_1 = 0, |
| 315 | times_2 = 1, |
| 316 | times_4 = 2, |
| 317 | times_8 = 3, |
| 318 | times_int_size = times_4, |
| 319 | times_half_pointer_size = times_2, |
| 320 | times_pointer_size = times_4, |
| 321 | times_twice_pointer_size = times_8 |
| 322 | }; |
| 323 | |
| 324 | |
| 325 | class Operand BASE_EMBEDDED { |
| 326 | public: |
| 327 | // reg |
| 328 | INLINE(explicit Operand(Register reg)); |
| 329 | |
| 330 | // [disp/r] |
| 331 | INLINE(explicit Operand(int32_t disp, RelocInfo::Mode rmode)); |
| 332 | |
| 333 | // [disp/r] |
| 334 | INLINE(explicit Operand(Immediate imm)); |
| 335 | |
| 336 | // [base + disp/r] |
| 337 | explicit Operand(Register base, int32_t disp, |
| 338 | RelocInfo::Mode rmode = RelocInfo::NONE32); |
| 339 | |
| 340 | // [base + index*scale + disp/r] |
| 341 | explicit Operand(Register base, |
| 342 | Register index, |
| 343 | ScaleFactor scale, |
| 344 | int32_t disp, |
| 345 | RelocInfo::Mode rmode = RelocInfo::NONE32); |
| 346 | |
| 347 | // [index*scale + disp/r] |
| 348 | explicit Operand(Register index, |
| 349 | ScaleFactor scale, |
| 350 | int32_t disp, |
| 351 | RelocInfo::Mode rmode = RelocInfo::NONE32); |
| 352 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 353 | static Operand JumpTable(Register index, ScaleFactor scale, Label* table) { |
| 354 | return Operand(index, scale, reinterpret_cast<int32_t>(table), |
| 355 | RelocInfo::INTERNAL_REFERENCE); |
| 356 | } |
| 357 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 358 | static Operand StaticVariable(const ExternalReference& ext) { |
| 359 | return Operand(reinterpret_cast<int32_t>(ext.address()), |
| 360 | RelocInfo::EXTERNAL_REFERENCE); |
| 361 | } |
| 362 | |
| 363 | static Operand StaticArray(Register index, |
| 364 | ScaleFactor scale, |
| 365 | const ExternalReference& arr) { |
| 366 | return Operand(index, scale, reinterpret_cast<int32_t>(arr.address()), |
| 367 | RelocInfo::EXTERNAL_REFERENCE); |
| 368 | } |
| 369 | |
| 370 | static Operand ForCell(Handle<Cell> cell) { |
| 371 | AllowDeferredHandleDereference embedding_raw_address; |
| 372 | return Operand(reinterpret_cast<int32_t>(cell.location()), |
| 373 | RelocInfo::CELL); |
| 374 | } |
| 375 | |
| 376 | static Operand ForRegisterPlusImmediate(Register base, Immediate imm) { |
| 377 | return Operand(base, imm.x_, imm.rmode_); |
| 378 | } |
| 379 | |
| 380 | // Returns true if this Operand is a wrapper for the specified register. |
| 381 | bool is_reg(Register reg) const; |
| 382 | |
| 383 | // Returns true if this Operand is a wrapper for one register. |
| 384 | bool is_reg_only() const; |
| 385 | |
| 386 | // Asserts that this Operand is a wrapper for one register and returns the |
| 387 | // register. |
| 388 | Register reg() const; |
| 389 | |
| 390 | private: |
| 391 | // Set the ModRM byte without an encoded 'reg' register. The |
| 392 | // register is encoded later as part of the emit_operand operation. |
| 393 | inline void set_modrm(int mod, Register rm); |
| 394 | |
| 395 | inline void set_sib(ScaleFactor scale, Register index, Register base); |
| 396 | inline void set_disp8(int8_t disp); |
| 397 | inline void set_dispr(int32_t disp, RelocInfo::Mode rmode); |
| 398 | |
| 399 | byte buf_[6]; |
| 400 | // The number of bytes in buf_. |
| 401 | unsigned int len_; |
| 402 | // Only valid if len_ > 4. |
| 403 | RelocInfo::Mode rmode_; |
| 404 | |
| 405 | friend class Assembler; |
| 406 | friend class MacroAssembler; |
| 407 | }; |
| 408 | |
| 409 | |
| 410 | // ----------------------------------------------------------------------------- |
| 411 | // A Displacement describes the 32bit immediate field of an instruction which |
| 412 | // may be used together with a Label in order to refer to a yet unknown code |
| 413 | // position. Displacements stored in the instruction stream are used to describe |
| 414 | // the instruction and to chain a list of instructions using the same Label. |
| 415 | // A Displacement contains 2 different fields: |
| 416 | // |
| 417 | // next field: position of next displacement in the chain (0 = end of list) |
| 418 | // type field: instruction type |
| 419 | // |
| 420 | // A next value of null (0) indicates the end of a chain (note that there can |
| 421 | // be no displacement at position zero, because there is always at least one |
| 422 | // instruction byte before the displacement). |
| 423 | // |
| 424 | // Displacement _data field layout |
| 425 | // |
| 426 | // |31.....2|1......0| |
| 427 | // [ next | type | |
| 428 | |
| 429 | class Displacement BASE_EMBEDDED { |
| 430 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 431 | enum Type { UNCONDITIONAL_JUMP, CODE_RELATIVE, OTHER, CODE_ABSOLUTE }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 432 | |
| 433 | int data() const { return data_; } |
| 434 | Type type() const { return TypeField::decode(data_); } |
| 435 | void next(Label* L) const { |
| 436 | int n = NextField::decode(data_); |
| 437 | n > 0 ? L->link_to(n) : L->Unuse(); |
| 438 | } |
| 439 | void link_to(Label* L) { init(L, type()); } |
| 440 | |
| 441 | explicit Displacement(int data) { data_ = data; } |
| 442 | |
| 443 | Displacement(Label* L, Type type) { init(L, type); } |
| 444 | |
| 445 | void print() { |
| 446 | PrintF("%s (%x) ", (type() == UNCONDITIONAL_JUMP ? "jmp" : "[other]"), |
| 447 | NextField::decode(data_)); |
| 448 | } |
| 449 | |
| 450 | private: |
| 451 | int data_; |
| 452 | |
| 453 | class TypeField: public BitField<Type, 0, 2> {}; |
| 454 | class NextField: public BitField<int, 2, 32-2> {}; |
| 455 | |
| 456 | void init(Label* L, Type type); |
| 457 | }; |
| 458 | |
| 459 | |
| 460 | class Assembler : public AssemblerBase { |
| 461 | private: |
| 462 | // We check before assembling an instruction that there is sufficient |
| 463 | // space to write an instruction and its relocation information. |
| 464 | // The relocation writer's position must be kGap bytes above the end of |
| 465 | // the generated instructions. This leaves enough space for the |
| 466 | // longest possible ia32 instruction, 15 bytes, and the longest possible |
| 467 | // relocation information encoding, RelocInfoWriter::kMaxLength == 16. |
| 468 | // (There is a 15 byte limit on ia32 instruction length that rules out some |
| 469 | // otherwise valid instructions.) |
| 470 | // This allows for a single, fast space check per instruction. |
| 471 | static const int kGap = 32; |
| 472 | |
| 473 | public: |
| 474 | // Create an assembler. Instructions and relocation information are emitted |
| 475 | // into a buffer, with the instructions starting from the beginning and the |
| 476 | // relocation information starting from the end of the buffer. See CodeDesc |
| 477 | // for a detailed comment on the layout (globals.h). |
| 478 | // |
| 479 | // If the provided buffer is NULL, the assembler allocates and grows its own |
| 480 | // buffer, and buffer_size determines the initial buffer size. The buffer is |
| 481 | // owned by the assembler and deallocated upon destruction of the assembler. |
| 482 | // |
| 483 | // If the provided buffer is not NULL, the assembler uses the provided buffer |
| 484 | // for code generation and assumes its size to be buffer_size. If the buffer |
| 485 | // is too small, a fatal error occurs. No deallocation of the buffer is done |
| 486 | // upon destruction of the assembler. |
| 487 | // TODO(vitalyr): the assembler does not need an isolate. |
| 488 | Assembler(Isolate* isolate, void* buffer, int buffer_size); |
| 489 | virtual ~Assembler() { } |
| 490 | |
| 491 | // GetCode emits any pending (non-emitted) code and fills the descriptor |
| 492 | // desc. GetCode() is idempotent; it returns the same result if no other |
| 493 | // Assembler functions are invoked in between GetCode() calls. |
| 494 | void GetCode(CodeDesc* desc); |
| 495 | |
| 496 | // Read/Modify the code target in the branch/call instruction at pc. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 497 | inline static Address target_address_at(Address pc, Address constant_pool); |
| 498 | inline static void set_target_address_at( |
| 499 | Isolate* isolate, Address pc, Address constant_pool, Address target, |
| 500 | ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 501 | static inline Address target_address_at(Address pc, Code* code) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 502 | Address constant_pool = code ? code->constant_pool() : NULL; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 503 | return target_address_at(pc, constant_pool); |
| 504 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 505 | static inline void set_target_address_at( |
| 506 | Isolate* isolate, Address pc, Code* code, Address target, |
| 507 | ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED) { |
| 508 | Address constant_pool = code ? code->constant_pool() : NULL; |
| 509 | set_target_address_at(isolate, pc, constant_pool, target); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | // Return the code target address at a call site from the return address |
| 513 | // of that call in the instruction stream. |
| 514 | inline static Address target_address_from_return_address(Address pc); |
| 515 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 516 | // This sets the branch destination (which is in the instruction on x86). |
| 517 | // This is for calls and branches within generated code. |
| 518 | inline static void deserialization_set_special_target_at( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 519 | Isolate* isolate, Address instruction_payload, Code* code, |
| 520 | Address target) { |
| 521 | set_target_address_at(isolate, instruction_payload, code, target); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 524 | // This sets the internal reference at the pc. |
| 525 | inline static void deserialization_set_target_internal_reference_at( |
| 526 | Isolate* isolate, Address pc, Address target, |
| 527 | RelocInfo::Mode mode = RelocInfo::INTERNAL_REFERENCE); |
| 528 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 529 | static const int kSpecialTargetSize = kPointerSize; |
| 530 | |
| 531 | // Distance between the address of the code target in the call instruction |
| 532 | // and the return address |
| 533 | static const int kCallTargetAddressOffset = kPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 534 | |
| 535 | static const int kCallInstructionLength = 5; |
| 536 | |
| 537 | // The debug break slot must be able to contain a call instruction. |
| 538 | static const int kDebugBreakSlotLength = kCallInstructionLength; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 539 | |
| 540 | // Distance between start of patched debug break slot and the emitted address |
| 541 | // to jump to. |
| 542 | static const int kPatchDebugBreakSlotAddressOffset = 1; // JMP imm32. |
| 543 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 544 | // One byte opcode for test al, 0xXX. |
| 545 | static const byte kTestAlByte = 0xA8; |
| 546 | // One byte opcode for nop. |
| 547 | static const byte kNopByte = 0x90; |
| 548 | |
| 549 | // One byte opcode for a short unconditional jump. |
| 550 | static const byte kJmpShortOpcode = 0xEB; |
| 551 | // One byte prefix for a short conditional jump. |
| 552 | static const byte kJccShortPrefix = 0x70; |
| 553 | static const byte kJncShortOpcode = kJccShortPrefix | not_carry; |
| 554 | static const byte kJcShortOpcode = kJccShortPrefix | carry; |
| 555 | static const byte kJnzShortOpcode = kJccShortPrefix | not_zero; |
| 556 | static const byte kJzShortOpcode = kJccShortPrefix | zero; |
| 557 | |
| 558 | |
| 559 | // --------------------------------------------------------------------------- |
| 560 | // Code generation |
| 561 | // |
| 562 | // - function names correspond one-to-one to ia32 instruction mnemonics |
| 563 | // - unless specified otherwise, instructions operate on 32bit operands |
| 564 | // - instructions on 8bit (byte) operands/registers have a trailing '_b' |
| 565 | // - instructions on 16bit (word) operands/registers have a trailing '_w' |
| 566 | // - naming conflicts with C++ keywords are resolved via a trailing '_' |
| 567 | |
| 568 | // NOTE ON INTERFACE: Currently, the interface is not very consistent |
| 569 | // in the sense that some operations (e.g. mov()) can be called in more |
| 570 | // the one way to generate the same instruction: The Register argument |
| 571 | // can in some cases be replaced with an Operand(Register) argument. |
| 572 | // This should be cleaned up and made more orthogonal. The questions |
| 573 | // is: should we always use Operands instead of Registers where an |
| 574 | // Operand is possible, or should we have a Register (overloaded) form |
| 575 | // instead? We must be careful to make sure that the selected instruction |
| 576 | // is obvious from the parameters to avoid hard-to-find code generation |
| 577 | // bugs. |
| 578 | |
| 579 | // Insert the smallest number of nop instructions |
| 580 | // possible to align the pc offset to a multiple |
| 581 | // of m. m must be a power of 2. |
| 582 | void Align(int m); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 583 | // Insert the smallest number of zero bytes possible to align the pc offset |
| 584 | // to a mulitple of m. m must be a power of 2 (>= 2). |
| 585 | void DataAlign(int m); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 586 | void Nop(int bytes = 1); |
| 587 | // Aligns code to something that's optimal for a jump target for the platform. |
| 588 | void CodeTargetAlign(); |
| 589 | |
| 590 | // Stack |
| 591 | void pushad(); |
| 592 | void popad(); |
| 593 | |
| 594 | void pushfd(); |
| 595 | void popfd(); |
| 596 | |
| 597 | void push(const Immediate& x); |
| 598 | void push_imm32(int32_t imm32); |
| 599 | void push(Register src); |
| 600 | void push(const Operand& src); |
| 601 | |
| 602 | void pop(Register dst); |
| 603 | void pop(const Operand& dst); |
| 604 | |
| 605 | void enter(const Immediate& size); |
| 606 | void leave(); |
| 607 | |
| 608 | // Moves |
| 609 | void mov_b(Register dst, Register src) { mov_b(dst, Operand(src)); } |
| 610 | void mov_b(Register dst, const Operand& src); |
| 611 | void mov_b(Register dst, int8_t imm8) { mov_b(Operand(dst), imm8); } |
| 612 | void mov_b(const Operand& dst, int8_t imm8); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 613 | void mov_b(const Operand& dst, const Immediate& src); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 614 | void mov_b(const Operand& dst, Register src); |
| 615 | |
| 616 | void mov_w(Register dst, const Operand& src); |
| 617 | void mov_w(const Operand& dst, Register src); |
| 618 | void mov_w(const Operand& dst, int16_t imm16); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 619 | void mov_w(const Operand& dst, const Immediate& src); |
| 620 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 621 | |
| 622 | void mov(Register dst, int32_t imm32); |
| 623 | void mov(Register dst, const Immediate& x); |
| 624 | void mov(Register dst, Handle<Object> handle); |
| 625 | void mov(Register dst, const Operand& src); |
| 626 | void mov(Register dst, Register src); |
| 627 | void mov(const Operand& dst, const Immediate& x); |
| 628 | void mov(const Operand& dst, Handle<Object> handle); |
| 629 | void mov(const Operand& dst, Register src); |
| 630 | |
| 631 | void movsx_b(Register dst, Register src) { movsx_b(dst, Operand(src)); } |
| 632 | void movsx_b(Register dst, const Operand& src); |
| 633 | |
| 634 | void movsx_w(Register dst, Register src) { movsx_w(dst, Operand(src)); } |
| 635 | void movsx_w(Register dst, const Operand& src); |
| 636 | |
| 637 | void movzx_b(Register dst, Register src) { movzx_b(dst, Operand(src)); } |
| 638 | void movzx_b(Register dst, const Operand& src); |
| 639 | |
| 640 | void movzx_w(Register dst, Register src) { movzx_w(dst, Operand(src)); } |
| 641 | void movzx_w(Register dst, const Operand& src); |
| 642 | |
| 643 | // Flag management. |
| 644 | void cld(); |
| 645 | |
| 646 | // Repetitive string instructions. |
| 647 | void rep_movs(); |
| 648 | void rep_stos(); |
| 649 | void stos(); |
| 650 | |
| 651 | // Exchange |
| 652 | void xchg(Register dst, Register src); |
| 653 | void xchg(Register dst, const Operand& src); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 654 | void xchg_b(Register reg, const Operand& op); |
| 655 | void xchg_w(Register reg, const Operand& op); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 656 | |
| 657 | // Arithmetics |
| 658 | void adc(Register dst, int32_t imm32); |
| 659 | void adc(Register dst, const Operand& src); |
| 660 | |
| 661 | void add(Register dst, Register src) { add(dst, Operand(src)); } |
| 662 | void add(Register dst, const Operand& src); |
| 663 | void add(const Operand& dst, Register src); |
| 664 | void add(Register dst, const Immediate& imm) { add(Operand(dst), imm); } |
| 665 | void add(const Operand& dst, const Immediate& x); |
| 666 | |
| 667 | void and_(Register dst, int32_t imm32); |
| 668 | void and_(Register dst, const Immediate& x); |
| 669 | void and_(Register dst, Register src) { and_(dst, Operand(src)); } |
| 670 | void and_(Register dst, const Operand& src); |
| 671 | void and_(const Operand& dst, Register src); |
| 672 | void and_(const Operand& dst, const Immediate& x); |
| 673 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 674 | void cmpb(Register reg, Immediate imm8) { cmpb(Operand(reg), imm8); } |
| 675 | void cmpb(const Operand& op, Immediate imm8); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 676 | void cmpb(Register reg, const Operand& op); |
| 677 | void cmpb(const Operand& op, Register reg); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 678 | void cmpb(Register dst, Register src) { cmpb(Operand(dst), src); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 679 | void cmpb_al(const Operand& op); |
| 680 | void cmpw_ax(const Operand& op); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 681 | void cmpw(const Operand& dst, Immediate src); |
| 682 | void cmpw(Register dst, Immediate src) { cmpw(Operand(dst), src); } |
| 683 | void cmpw(Register dst, const Operand& src); |
| 684 | void cmpw(Register dst, Register src) { cmpw(Operand(dst), src); } |
| 685 | void cmpw(const Operand& dst, Register src); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 686 | void cmp(Register reg, int32_t imm32); |
| 687 | void cmp(Register reg, Handle<Object> handle); |
| 688 | void cmp(Register reg0, Register reg1) { cmp(reg0, Operand(reg1)); } |
| 689 | void cmp(Register reg, const Operand& op); |
| 690 | void cmp(Register reg, const Immediate& imm) { cmp(Operand(reg), imm); } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 691 | void cmp(const Operand& op, Register reg); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 692 | void cmp(const Operand& op, const Immediate& imm); |
| 693 | void cmp(const Operand& op, Handle<Object> handle); |
| 694 | |
| 695 | void dec_b(Register dst); |
| 696 | void dec_b(const Operand& dst); |
| 697 | |
| 698 | void dec(Register dst); |
| 699 | void dec(const Operand& dst); |
| 700 | |
| 701 | void cdq(); |
| 702 | |
| 703 | void idiv(Register src) { idiv(Operand(src)); } |
| 704 | void idiv(const Operand& src); |
| 705 | void div(Register src) { div(Operand(src)); } |
| 706 | void div(const Operand& src); |
| 707 | |
| 708 | // Signed multiply instructions. |
| 709 | void imul(Register src); // edx:eax = eax * src. |
| 710 | void imul(Register dst, Register src) { imul(dst, Operand(src)); } |
| 711 | void imul(Register dst, const Operand& src); // dst = dst * src. |
| 712 | void imul(Register dst, Register src, int32_t imm32); // dst = src * imm32. |
| 713 | void imul(Register dst, const Operand& src, int32_t imm32); |
| 714 | |
| 715 | void inc(Register dst); |
| 716 | void inc(const Operand& dst); |
| 717 | |
| 718 | void lea(Register dst, const Operand& src); |
| 719 | |
| 720 | // Unsigned multiply instruction. |
| 721 | void mul(Register src); // edx:eax = eax * reg. |
| 722 | |
| 723 | void neg(Register dst); |
| 724 | void neg(const Operand& dst); |
| 725 | |
| 726 | void not_(Register dst); |
| 727 | void not_(const Operand& dst); |
| 728 | |
| 729 | void or_(Register dst, int32_t imm32); |
| 730 | void or_(Register dst, Register src) { or_(dst, Operand(src)); } |
| 731 | void or_(Register dst, const Operand& src); |
| 732 | void or_(const Operand& dst, Register src); |
| 733 | void or_(Register dst, const Immediate& imm) { or_(Operand(dst), imm); } |
| 734 | void or_(const Operand& dst, const Immediate& x); |
| 735 | |
| 736 | void rcl(Register dst, uint8_t imm8); |
| 737 | void rcr(Register dst, uint8_t imm8); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 738 | |
| 739 | void ror(Register dst, uint8_t imm8) { ror(Operand(dst), imm8); } |
| 740 | void ror(const Operand& dst, uint8_t imm8); |
| 741 | void ror_cl(Register dst) { ror_cl(Operand(dst)); } |
| 742 | void ror_cl(const Operand& dst); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 743 | |
| 744 | void sar(Register dst, uint8_t imm8) { sar(Operand(dst), imm8); } |
| 745 | void sar(const Operand& dst, uint8_t imm8); |
| 746 | void sar_cl(Register dst) { sar_cl(Operand(dst)); } |
| 747 | void sar_cl(const Operand& dst); |
| 748 | |
| 749 | void sbb(Register dst, const Operand& src); |
| 750 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 751 | void shl(Register dst, uint8_t imm8) { shl(Operand(dst), imm8); } |
| 752 | void shl(const Operand& dst, uint8_t imm8); |
| 753 | void shl_cl(Register dst) { shl_cl(Operand(dst)); } |
| 754 | void shl_cl(const Operand& dst); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 755 | void shld(Register dst, Register src, uint8_t shift); |
| 756 | void shld_cl(Register dst, Register src); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 757 | |
| 758 | void shr(Register dst, uint8_t imm8) { shr(Operand(dst), imm8); } |
| 759 | void shr(const Operand& dst, uint8_t imm8); |
| 760 | void shr_cl(Register dst) { shr_cl(Operand(dst)); } |
| 761 | void shr_cl(const Operand& dst); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 762 | void shrd(Register dst, Register src, uint8_t shift); |
| 763 | void shrd_cl(Register dst, Register src) { shrd_cl(Operand(dst), src); } |
| 764 | void shrd_cl(const Operand& dst, Register src); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 765 | |
| 766 | void sub(Register dst, const Immediate& imm) { sub(Operand(dst), imm); } |
| 767 | void sub(const Operand& dst, const Immediate& x); |
| 768 | void sub(Register dst, Register src) { sub(dst, Operand(src)); } |
| 769 | void sub(Register dst, const Operand& src); |
| 770 | void sub(const Operand& dst, Register src); |
| 771 | |
| 772 | void test(Register reg, const Immediate& imm); |
| 773 | void test(Register reg0, Register reg1) { test(reg0, Operand(reg1)); } |
| 774 | void test(Register reg, const Operand& op); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 775 | void test(const Operand& op, const Immediate& imm); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 776 | void test(const Operand& op, Register reg) { test(reg, op); } |
| 777 | void test_b(Register reg, const Operand& op); |
| 778 | void test_b(Register reg, Immediate imm8); |
| 779 | void test_b(const Operand& op, Immediate imm8); |
| 780 | void test_b(const Operand& op, Register reg) { test_b(reg, op); } |
| 781 | void test_b(Register dst, Register src) { test_b(dst, Operand(src)); } |
| 782 | void test_w(Register reg, const Operand& op); |
| 783 | void test_w(Register reg, Immediate imm16); |
| 784 | void test_w(const Operand& op, Immediate imm16); |
| 785 | void test_w(const Operand& op, Register reg) { test_w(reg, op); } |
| 786 | void test_w(Register dst, Register src) { test_w(dst, Operand(src)); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 787 | |
| 788 | void xor_(Register dst, int32_t imm32); |
| 789 | void xor_(Register dst, Register src) { xor_(dst, Operand(src)); } |
| 790 | void xor_(Register dst, const Operand& src); |
| 791 | void xor_(const Operand& dst, Register src); |
| 792 | void xor_(Register dst, const Immediate& imm) { xor_(Operand(dst), imm); } |
| 793 | void xor_(const Operand& dst, const Immediate& x); |
| 794 | |
| 795 | // Bit operations. |
| 796 | void bt(const Operand& dst, Register src); |
| 797 | void bts(Register dst, Register src) { bts(Operand(dst), src); } |
| 798 | void bts(const Operand& dst, Register src); |
| 799 | void bsr(Register dst, Register src) { bsr(dst, Operand(src)); } |
| 800 | void bsr(Register dst, const Operand& src); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 801 | void bsf(Register dst, Register src) { bsf(dst, Operand(src)); } |
| 802 | void bsf(Register dst, const Operand& src); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 803 | |
| 804 | // Miscellaneous |
| 805 | void hlt(); |
| 806 | void int3(); |
| 807 | void nop(); |
| 808 | void ret(int imm16); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 809 | void ud2(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 810 | |
| 811 | // Label operations & relative jumps (PPUM Appendix D) |
| 812 | // |
| 813 | // Takes a branch opcode (cc) and a label (L) and generates |
| 814 | // either a backward branch or a forward branch and links it |
| 815 | // to the label fixup chain. Usage: |
| 816 | // |
| 817 | // Label L; // unbound label |
| 818 | // j(cc, &L); // forward branch to unbound label |
| 819 | // bind(&L); // bind label to the current pc |
| 820 | // j(cc, &L); // backward branch to bound label |
| 821 | // bind(&L); // illegal: a label may be bound only once |
| 822 | // |
| 823 | // Note: The same Label can be used for forward and backward branches |
| 824 | // but it may be bound only once. |
| 825 | |
| 826 | void bind(Label* L); // binds an unbound label L to the current code position |
| 827 | |
| 828 | // Calls |
| 829 | void call(Label* L); |
| 830 | void call(byte* entry, RelocInfo::Mode rmode); |
| 831 | int CallSize(const Operand& adr); |
| 832 | void call(Register reg) { call(Operand(reg)); } |
| 833 | void call(const Operand& adr); |
| 834 | int CallSize(Handle<Code> code, RelocInfo::Mode mode); |
| 835 | void call(Handle<Code> code, |
| 836 | RelocInfo::Mode rmode, |
| 837 | TypeFeedbackId id = TypeFeedbackId::None()); |
| 838 | |
| 839 | // Jumps |
| 840 | // unconditional jump to L |
| 841 | void jmp(Label* L, Label::Distance distance = Label::kFar); |
| 842 | void jmp(byte* entry, RelocInfo::Mode rmode); |
| 843 | void jmp(Register reg) { jmp(Operand(reg)); } |
| 844 | void jmp(const Operand& adr); |
| 845 | void jmp(Handle<Code> code, RelocInfo::Mode rmode); |
| 846 | |
| 847 | // Conditional jumps |
| 848 | void j(Condition cc, |
| 849 | Label* L, |
| 850 | Label::Distance distance = Label::kFar); |
| 851 | void j(Condition cc, byte* entry, RelocInfo::Mode rmode); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 852 | void j(Condition cc, Handle<Code> code, |
| 853 | RelocInfo::Mode rmode = RelocInfo::CODE_TARGET); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 854 | |
| 855 | // Floating-point operations |
| 856 | void fld(int i); |
| 857 | void fstp(int i); |
| 858 | |
| 859 | void fld1(); |
| 860 | void fldz(); |
| 861 | void fldpi(); |
| 862 | void fldln2(); |
| 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 fst_s(const Operand& adr); |
| 869 | void fstp_d(const Operand& adr); |
| 870 | void fst_d(const Operand& adr); |
| 871 | |
| 872 | void fild_s(const Operand& adr); |
| 873 | void fild_d(const Operand& adr); |
| 874 | |
| 875 | void fist_s(const Operand& adr); |
| 876 | |
| 877 | void fistp_s(const Operand& adr); |
| 878 | void fistp_d(const Operand& adr); |
| 879 | |
| 880 | // The fisttp instructions require SSE3. |
| 881 | void fisttp_s(const Operand& adr); |
| 882 | void fisttp_d(const Operand& adr); |
| 883 | |
| 884 | void fabs(); |
| 885 | void fchs(); |
| 886 | void fsqrt(); |
| 887 | void fcos(); |
| 888 | void fsin(); |
| 889 | void fptan(); |
| 890 | void fyl2x(); |
| 891 | void f2xm1(); |
| 892 | void fscale(); |
| 893 | void fninit(); |
| 894 | |
| 895 | void fadd(int i); |
| 896 | void fadd_i(int i); |
| 897 | void fadd_d(const Operand& adr); |
| 898 | void fsub(int i); |
| 899 | void fsub_i(int i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 900 | void fsub_d(const Operand& adr); |
| 901 | void fsubr_d(const Operand& adr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 902 | void fmul(int i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 903 | void fmul_d(const Operand& adr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 904 | void fmul_i(int i); |
| 905 | void fdiv(int i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 906 | void fdiv_d(const Operand& adr); |
| 907 | void fdivr_d(const Operand& adr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 908 | void fdiv_i(int i); |
| 909 | |
| 910 | void fisub_s(const Operand& adr); |
| 911 | |
| 912 | void faddp(int i = 1); |
| 913 | void fsubp(int i = 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 914 | void fsubr(int i = 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 915 | void fsubrp(int i = 1); |
| 916 | void fmulp(int i = 1); |
| 917 | void fdivp(int i = 1); |
| 918 | void fprem(); |
| 919 | void fprem1(); |
| 920 | |
| 921 | void fxch(int i = 1); |
| 922 | void fincstp(); |
| 923 | void ffree(int i = 0); |
| 924 | |
| 925 | void ftst(); |
| 926 | void fxam(); |
| 927 | void fucomp(int i); |
| 928 | void fucompp(); |
| 929 | void fucomi(int i); |
| 930 | void fucomip(); |
| 931 | void fcompp(); |
| 932 | void fnstsw_ax(); |
| 933 | void fldcw(const Operand& adr); |
| 934 | void fnstcw(const Operand& adr); |
| 935 | void fwait(); |
| 936 | void fnclex(); |
| 937 | void fnsave(const Operand& adr); |
| 938 | void frstor(const Operand& adr); |
| 939 | |
| 940 | void frndint(); |
| 941 | |
| 942 | void sahf(); |
| 943 | void setcc(Condition cc, Register reg); |
| 944 | |
| 945 | void cpuid(); |
| 946 | |
| 947 | // TODO(lrn): Need SFENCE for movnt? |
| 948 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 949 | // Check the code size generated from label to here. |
| 950 | int SizeOfCodeGeneratedSince(Label* label) { |
| 951 | return pc_offset() - label->pos(); |
| 952 | } |
| 953 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 954 | // Mark generator continuation. |
| 955 | void RecordGeneratorContinuation(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 956 | |
| 957 | // Mark address of a debug break slot. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 958 | void RecordDebugBreakSlot(RelocInfo::Mode mode); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 959 | |
| 960 | // Record a comment relocation entry that can be used by a disassembler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 961 | // Use --code-comments to enable. |
| 962 | void RecordComment(const char* msg); |
| 963 | |
| 964 | // Record a deoptimization reason that can be used by a log or cpu profiler. |
| 965 | // Use --trace-deopt to enable. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 966 | void RecordDeoptReason(const int reason, int raw_position, int id); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 967 | |
| 968 | // Writes a single byte or word of data in the code stream. Used for |
| 969 | // inline tables, e.g., jump-tables. |
| 970 | void db(uint8_t data); |
| 971 | void dd(uint32_t data); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 972 | void dq(uint64_t data); |
| 973 | void dp(uintptr_t data) { dd(data); } |
| 974 | void dd(Label* label); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 975 | |
| 976 | // Check if there is less than kGap bytes available in the buffer. |
| 977 | // If this is the case, we need to grow the buffer before emitting |
| 978 | // an instruction or relocation information. |
| 979 | inline bool buffer_overflow() const { |
| 980 | return pc_ >= reloc_info_writer.pos() - kGap; |
| 981 | } |
| 982 | |
| 983 | // Get the number of bytes available in the buffer. |
| 984 | inline int available_space() const { return reloc_info_writer.pos() - pc_; } |
| 985 | |
| 986 | static bool IsNop(Address addr); |
| 987 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 988 | AssemblerPositionsRecorder* positions_recorder() { |
| 989 | return &positions_recorder_; |
| 990 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 991 | |
| 992 | int relocation_writer_size() { |
| 993 | return (buffer_ + buffer_size_) - reloc_info_writer.pos(); |
| 994 | } |
| 995 | |
| 996 | // Avoid overflows for displacements etc. |
| 997 | static const int kMaximalBufferSize = 512*MB; |
| 998 | |
| 999 | byte byte_at(int pos) { return buffer_[pos]; } |
| 1000 | void set_byte_at(int pos, byte value) { buffer_[pos] = value; } |
| 1001 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1002 | void PatchConstantPoolAccessInstruction(int pc_offset, int offset, |
| 1003 | ConstantPoolEntry::Access access, |
| 1004 | ConstantPoolEntry::Type type) { |
| 1005 | // No embedded constant pool support. |
| 1006 | UNREACHABLE(); |
| 1007 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1008 | |
| 1009 | protected: |
| 1010 | byte* addr_at(int pos) { return buffer_ + pos; } |
| 1011 | |
| 1012 | |
| 1013 | private: |
| 1014 | uint32_t long_at(int pos) { |
| 1015 | return *reinterpret_cast<uint32_t*>(addr_at(pos)); |
| 1016 | } |
| 1017 | void long_at_put(int pos, uint32_t x) { |
| 1018 | *reinterpret_cast<uint32_t*>(addr_at(pos)) = x; |
| 1019 | } |
| 1020 | |
| 1021 | // code emission |
| 1022 | void GrowBuffer(); |
| 1023 | inline void emit(uint32_t x); |
| 1024 | inline void emit(Handle<Object> handle); |
| 1025 | inline void emit(uint32_t x, |
| 1026 | RelocInfo::Mode rmode, |
| 1027 | TypeFeedbackId id = TypeFeedbackId::None()); |
| 1028 | inline void emit(Handle<Code> code, |
| 1029 | RelocInfo::Mode rmode, |
| 1030 | TypeFeedbackId id = TypeFeedbackId::None()); |
| 1031 | inline void emit(const Immediate& x); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1032 | inline void emit_b(Immediate x); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1033 | inline void emit_w(const Immediate& x); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1034 | inline void emit_q(uint64_t x); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1035 | |
| 1036 | // Emit the code-object-relative offset of the label's position |
| 1037 | inline void emit_code_relative_offset(Label* label); |
| 1038 | |
| 1039 | // instruction generation |
| 1040 | void emit_arith_b(int op1, int op2, Register dst, int imm8); |
| 1041 | |
| 1042 | // Emit a basic arithmetic instruction (i.e. first byte of the family is 0x81) |
| 1043 | // with a given destination expression and an immediate operand. It attempts |
| 1044 | // to use the shortest encoding possible. |
| 1045 | // sel specifies the /n in the modrm byte (see the Intel PRM). |
| 1046 | void emit_arith(int sel, Operand dst, const Immediate& x); |
| 1047 | |
| 1048 | void emit_operand(Register reg, const Operand& adr); |
| 1049 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1050 | void emit_label(Label* label); |
| 1051 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1052 | void emit_farith(int b1, int b2, int i); |
| 1053 | |
| 1054 | // labels |
| 1055 | void print(Label* L); |
| 1056 | void bind_to(Label* L, int pos); |
| 1057 | |
| 1058 | // displacements |
| 1059 | inline Displacement disp_at(Label* L); |
| 1060 | inline void disp_at_put(Label* L, Displacement disp); |
| 1061 | inline void emit_disp(Label* L, Displacement::Type type); |
| 1062 | inline void emit_near_disp(Label* L); |
| 1063 | |
| 1064 | // record reloc info for current pc_ |
| 1065 | void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0); |
| 1066 | |
| 1067 | friend class CodePatcher; |
| 1068 | friend class EnsureSpace; |
| 1069 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1070 | // Internal reference positions, required for (potential) patching in |
| 1071 | // GrowBuffer(); contains only those internal references whose labels |
| 1072 | // are already bound. |
| 1073 | std::deque<int> internal_reference_positions_; |
| 1074 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1075 | // code generation |
| 1076 | RelocInfoWriter reloc_info_writer; |
| 1077 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1078 | AssemblerPositionsRecorder positions_recorder_; |
| 1079 | friend class AssemblerPositionsRecorder; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1080 | }; |
| 1081 | |
| 1082 | |
| 1083 | // Helper class that ensures that there is enough space for generating |
| 1084 | // instructions and relocation information. The constructor makes |
| 1085 | // sure that there is enough space and (in debug mode) the destructor |
| 1086 | // checks that we did not generate too much. |
| 1087 | class EnsureSpace BASE_EMBEDDED { |
| 1088 | public: |
| 1089 | explicit EnsureSpace(Assembler* assembler) : assembler_(assembler) { |
| 1090 | if (assembler_->buffer_overflow()) assembler_->GrowBuffer(); |
| 1091 | #ifdef DEBUG |
| 1092 | space_before_ = assembler_->available_space(); |
| 1093 | #endif |
| 1094 | } |
| 1095 | |
| 1096 | #ifdef DEBUG |
| 1097 | ~EnsureSpace() { |
| 1098 | int bytes_generated = space_before_ - assembler_->available_space(); |
| 1099 | DCHECK(bytes_generated < assembler_->kGap); |
| 1100 | } |
| 1101 | #endif |
| 1102 | |
| 1103 | private: |
| 1104 | Assembler* assembler_; |
| 1105 | #ifdef DEBUG |
| 1106 | int space_before_; |
| 1107 | #endif |
| 1108 | }; |
| 1109 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1110 | } // namespace internal |
| 1111 | } // namespace v8 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1112 | |
| 1113 | #endif // V8_X87_ASSEMBLER_X87_H_ |