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