Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright (c) 1994-2006 Sun Microsystems Inc. |
| 2 | // All Rights Reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions |
| 6 | // are 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 |
| 14 | // distribution. |
| 15 | // |
| 16 | // - Neither the name of Sun Microsystems or the names of contributors may |
| 17 | // be used to endorse or promote products derived from this software without |
| 18 | // specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 23 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 24 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 26 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 27 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 29 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 31 | // OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 33 | // The original source code covered by the above license above has been |
| 34 | // modified significantly by Google Inc. |
| 35 | // Copyright 2010 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | |
| 37 | #include "v8.h" |
| 38 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 39 | #if defined(V8_TARGET_ARCH_ARM) |
| 40 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | #include "arm/assembler-arm-inl.h" |
| 42 | #include "serialize.h" |
| 43 | |
| 44 | namespace v8 { |
| 45 | namespace internal { |
| 46 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 47 | // Safe default is no features. |
| 48 | unsigned CpuFeatures::supported_ = 0; |
| 49 | unsigned CpuFeatures::enabled_ = 0; |
| 50 | unsigned CpuFeatures::found_by_runtime_probing_ = 0; |
| 51 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 52 | |
| 53 | #ifdef __arm__ |
| 54 | static uint64_t CpuFeaturesImpliedByCompiler() { |
| 55 | uint64_t answer = 0; |
| 56 | #ifdef CAN_USE_ARMV7_INSTRUCTIONS |
| 57 | answer |= 1u << ARMv7; |
| 58 | #endif // def CAN_USE_ARMV7_INSTRUCTIONS |
| 59 | // If the compiler is allowed to use VFP then we can use VFP too in our code |
| 60 | // generation even when generating snapshots. This won't work for cross |
| 61 | // compilation. |
| 62 | #if defined(__VFP_FP__) && !defined(__SOFTFP__) |
| 63 | answer |= 1u << VFP3; |
| 64 | #endif // defined(__VFP_FP__) && !defined(__SOFTFP__) |
| 65 | #ifdef CAN_USE_VFP_INSTRUCTIONS |
| 66 | answer |= 1u << VFP3; |
| 67 | #endif // def CAN_USE_VFP_INSTRUCTIONS |
| 68 | return answer; |
| 69 | } |
| 70 | #endif // def __arm__ |
| 71 | |
| 72 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 73 | void CpuFeatures::Probe() { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 74 | #ifndef __arm__ |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 75 | // For the simulator=arm build, use VFP when FLAG_enable_vfp3 is enabled. |
| 76 | if (FLAG_enable_vfp3) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 77 | supported_ |= 1u << VFP3; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 78 | } |
| 79 | // For the simulator=arm build, use ARMv7 when FLAG_enable_armv7 is enabled |
| 80 | if (FLAG_enable_armv7) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 81 | supported_ |= 1u << ARMv7; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 82 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 83 | #else // def __arm__ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 84 | if (Serializer::enabled()) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 85 | supported_ |= OS::CpuFeaturesImpliedByPlatform(); |
| 86 | supported_ |= CpuFeaturesImpliedByCompiler(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 87 | return; // No features if we might serialize. |
| 88 | } |
| 89 | |
| 90 | if (OS::ArmCpuHasFeature(VFP3)) { |
| 91 | // This implementation also sets the VFP flags if |
| 92 | // runtime detection of VFP returns true. |
| 93 | supported_ |= 1u << VFP3; |
| 94 | found_by_runtime_probing_ |= 1u << VFP3; |
| 95 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 96 | |
| 97 | if (OS::ArmCpuHasFeature(ARMv7)) { |
| 98 | supported_ |= 1u << ARMv7; |
| 99 | found_by_runtime_probing_ |= 1u << ARMv7; |
| 100 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 101 | #endif |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | // ----------------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | // Implementation of RelocInfo |
| 107 | |
| 108 | const int RelocInfo::kApplyMask = 0; |
| 109 | |
| 110 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 111 | bool RelocInfo::IsCodedSpecially() { |
| 112 | // The deserializer needs to know whether a pointer is specially coded. Being |
| 113 | // specially coded on ARM means that it is a movw/movt instruction. We don't |
| 114 | // generate those yet. |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 120 | void RelocInfo::PatchCode(byte* instructions, int instruction_count) { |
| 121 | // Patch the code at the current address with the supplied instructions. |
| 122 | Instr* pc = reinterpret_cast<Instr*>(pc_); |
| 123 | Instr* instr = reinterpret_cast<Instr*>(instructions); |
| 124 | for (int i = 0; i < instruction_count; i++) { |
| 125 | *(pc + i) = *(instr + i); |
| 126 | } |
| 127 | |
| 128 | // Indicate that code has changed. |
| 129 | CPU::FlushICache(pc_, instruction_count * Assembler::kInstrSize); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | // Patch the code at the current PC with a call to the target address. |
| 134 | // Additional guard instructions can be added if required. |
| 135 | void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) { |
| 136 | // Patch the code at the current address with a call to the target. |
| 137 | UNIMPLEMENTED(); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | // ----------------------------------------------------------------------------- |
| 142 | // Implementation of Operand and MemOperand |
| 143 | // See assembler-arm-inl.h for inlined constructors |
| 144 | |
| 145 | Operand::Operand(Handle<Object> handle) { |
| 146 | rm_ = no_reg; |
| 147 | // Verify all Objects referred by code are NOT in new space. |
| 148 | Object* obj = *handle; |
| 149 | ASSERT(!Heap::InNewSpace(obj)); |
| 150 | if (obj->IsHeapObject()) { |
| 151 | imm32_ = reinterpret_cast<intptr_t>(handle.location()); |
| 152 | rmode_ = RelocInfo::EMBEDDED_OBJECT; |
| 153 | } else { |
| 154 | // no relocation needed |
| 155 | imm32_ = reinterpret_cast<intptr_t>(obj); |
| 156 | rmode_ = RelocInfo::NONE; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | |
| 161 | Operand::Operand(Register rm, ShiftOp shift_op, int shift_imm) { |
| 162 | ASSERT(is_uint5(shift_imm)); |
| 163 | ASSERT(shift_op != ROR || shift_imm != 0); // use RRX if you mean it |
| 164 | rm_ = rm; |
| 165 | rs_ = no_reg; |
| 166 | shift_op_ = shift_op; |
| 167 | shift_imm_ = shift_imm & 31; |
| 168 | if (shift_op == RRX) { |
| 169 | // encoded as ROR with shift_imm == 0 |
| 170 | ASSERT(shift_imm == 0); |
| 171 | shift_op_ = ROR; |
| 172 | shift_imm_ = 0; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | |
| 177 | Operand::Operand(Register rm, ShiftOp shift_op, Register rs) { |
| 178 | ASSERT(shift_op != RRX); |
| 179 | rm_ = rm; |
| 180 | rs_ = no_reg; |
| 181 | shift_op_ = shift_op; |
| 182 | rs_ = rs; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | MemOperand::MemOperand(Register rn, int32_t offset, AddrMode am) { |
| 187 | rn_ = rn; |
| 188 | rm_ = no_reg; |
| 189 | offset_ = offset; |
| 190 | am_ = am; |
| 191 | } |
| 192 | |
| 193 | MemOperand::MemOperand(Register rn, Register rm, AddrMode am) { |
| 194 | rn_ = rn; |
| 195 | rm_ = rm; |
| 196 | shift_op_ = LSL; |
| 197 | shift_imm_ = 0; |
| 198 | am_ = am; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | MemOperand::MemOperand(Register rn, Register rm, |
| 203 | ShiftOp shift_op, int shift_imm, AddrMode am) { |
| 204 | ASSERT(is_uint5(shift_imm)); |
| 205 | rn_ = rn; |
| 206 | rm_ = rm; |
| 207 | shift_op_ = shift_op; |
| 208 | shift_imm_ = shift_imm & 31; |
| 209 | am_ = am; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | // ----------------------------------------------------------------------------- |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 214 | // Implementation of Assembler. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 215 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 216 | // Instruction encoding bits. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 217 | enum { |
| 218 | H = 1 << 5, // halfword (or byte) |
| 219 | S6 = 1 << 6, // signed (or unsigned) |
| 220 | L = 1 << 20, // load (or store) |
| 221 | S = 1 << 20, // set condition code (or leave unchanged) |
| 222 | W = 1 << 21, // writeback base register (or leave unchanged) |
| 223 | A = 1 << 21, // accumulate in multiply instruction (or not) |
| 224 | B = 1 << 22, // unsigned byte (or word) |
| 225 | N = 1 << 22, // long (or short) |
| 226 | U = 1 << 23, // positive (or negative) offset/index |
| 227 | P = 1 << 24, // offset/pre-indexed addressing (or post-indexed addressing) |
| 228 | I = 1 << 25, // immediate shifter operand (or not) |
| 229 | |
| 230 | B4 = 1 << 4, |
| 231 | B5 = 1 << 5, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 232 | B6 = 1 << 6, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 233 | B7 = 1 << 7, |
| 234 | B8 = 1 << 8, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 235 | B9 = 1 << 9, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 236 | B12 = 1 << 12, |
| 237 | B16 = 1 << 16, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 238 | B18 = 1 << 18, |
| 239 | B19 = 1 << 19, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 240 | B20 = 1 << 20, |
| 241 | B21 = 1 << 21, |
| 242 | B22 = 1 << 22, |
| 243 | B23 = 1 << 23, |
| 244 | B24 = 1 << 24, |
| 245 | B25 = 1 << 25, |
| 246 | B26 = 1 << 26, |
| 247 | B27 = 1 << 27, |
| 248 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 249 | // Instruction bit masks. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 250 | RdMask = 15 << 12, // in str instruction |
| 251 | CondMask = 15 << 28, |
| 252 | CoprocessorMask = 15 << 8, |
| 253 | OpCodeMask = 15 << 21, // in data-processing instructions |
| 254 | Imm24Mask = (1 << 24) - 1, |
| 255 | Off12Mask = (1 << 12) - 1, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 256 | // Reserved condition. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 257 | nv = 15 << 28 |
| 258 | }; |
| 259 | |
| 260 | |
| 261 | // add(sp, sp, 4) instruction (aka Pop()) |
| 262 | static const Instr kPopInstruction = |
| 263 | al | 4 * B21 | 4 | LeaveCC | I | sp.code() * B16 | sp.code() * B12; |
| 264 | // str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r)) |
| 265 | // register r is not encoded. |
| 266 | static const Instr kPushRegPattern = |
| 267 | al | B26 | 4 | NegPreIndex | sp.code() * B16; |
| 268 | // ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r)) |
| 269 | // register r is not encoded. |
| 270 | static const Instr kPopRegPattern = |
| 271 | al | B26 | L | 4 | PostIndex | sp.code() * B16; |
| 272 | // mov lr, pc |
| 273 | const Instr kMovLrPc = al | 13*B21 | pc.code() | lr.code() * B12; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 274 | // ldr rd, [pc, #offset] |
| 275 | const Instr kLdrPCMask = CondMask | 15 * B24 | 7 * B20 | 15 * B16; |
| 276 | const Instr kLdrPCPattern = al | 5 * B24 | L | pc.code() * B16; |
| 277 | // blxcc rm |
| 278 | const Instr kBlxRegMask = |
| 279 | 15 * B24 | 15 * B20 | 15 * B16 | 15 * B12 | 15 * B8 | 15 * B4; |
| 280 | const Instr kBlxRegPattern = |
| 281 | B24 | B21 | 15 * B16 | 15 * B12 | 15 * B8 | 3 * B4; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 282 | const Instr kMovMvnMask = 0x6d * B21 | 0xf * B16; |
| 283 | const Instr kMovMvnPattern = 0xd * B21; |
| 284 | const Instr kMovMvnFlip = B22; |
| 285 | const Instr kMovLeaveCCMask = 0xdff * B16; |
| 286 | const Instr kMovLeaveCCPattern = 0x1a0 * B16; |
| 287 | const Instr kMovwMask = 0xff * B20; |
| 288 | const Instr kMovwPattern = 0x30 * B20; |
| 289 | const Instr kMovwLeaveCCFlip = 0x5 * B21; |
| 290 | const Instr kCmpCmnMask = 0xdd * B20 | 0xf * B12; |
| 291 | const Instr kCmpCmnPattern = 0x15 * B20; |
| 292 | const Instr kCmpCmnFlip = B21; |
| 293 | const Instr kALUMask = 0x6f * B21; |
| 294 | const Instr kAddPattern = 0x4 * B21; |
| 295 | const Instr kSubPattern = 0x2 * B21; |
| 296 | const Instr kBicPattern = 0xe * B21; |
| 297 | const Instr kAndPattern = 0x0 * B21; |
| 298 | const Instr kAddSubFlip = 0x6 * B21; |
| 299 | const Instr kAndBicFlip = 0xe * B21; |
| 300 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 301 | // A mask for the Rd register for push, pop, ldr, str instructions. |
| 302 | const Instr kRdMask = 0x0000f000; |
| 303 | static const int kRdShift = 12; |
| 304 | static const Instr kLdrRegFpOffsetPattern = |
| 305 | al | B26 | L | Offset | fp.code() * B16; |
| 306 | static const Instr kStrRegFpOffsetPattern = |
| 307 | al | B26 | Offset | fp.code() * B16; |
| 308 | static const Instr kLdrRegFpNegOffsetPattern = |
| 309 | al | B26 | L | NegOffset | fp.code() * B16; |
| 310 | static const Instr kStrRegFpNegOffsetPattern = |
| 311 | al | B26 | NegOffset | fp.code() * B16; |
| 312 | static const Instr kLdrStrInstrTypeMask = 0xffff0000; |
| 313 | static const Instr kLdrStrInstrArgumentMask = 0x0000ffff; |
| 314 | static const Instr kLdrStrOffsetMask = 0x00000fff; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 315 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 316 | // Spare buffer. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 317 | static const int kMinimalBufferSize = 4*KB; |
| 318 | static byte* spare_buffer_ = NULL; |
| 319 | |
| 320 | Assembler::Assembler(void* buffer, int buffer_size) { |
| 321 | if (buffer == NULL) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 322 | // Do our own buffer management. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | if (buffer_size <= kMinimalBufferSize) { |
| 324 | buffer_size = kMinimalBufferSize; |
| 325 | |
| 326 | if (spare_buffer_ != NULL) { |
| 327 | buffer = spare_buffer_; |
| 328 | spare_buffer_ = NULL; |
| 329 | } |
| 330 | } |
| 331 | if (buffer == NULL) { |
| 332 | buffer_ = NewArray<byte>(buffer_size); |
| 333 | } else { |
| 334 | buffer_ = static_cast<byte*>(buffer); |
| 335 | } |
| 336 | buffer_size_ = buffer_size; |
| 337 | own_buffer_ = true; |
| 338 | |
| 339 | } else { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 340 | // Use externally provided buffer instead. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 341 | ASSERT(buffer_size > 0); |
| 342 | buffer_ = static_cast<byte*>(buffer); |
| 343 | buffer_size_ = buffer_size; |
| 344 | own_buffer_ = false; |
| 345 | } |
| 346 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 347 | // Setup buffer pointers. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 348 | ASSERT(buffer_ != NULL); |
| 349 | pc_ = buffer_; |
| 350 | reloc_info_writer.Reposition(buffer_ + buffer_size, pc_); |
| 351 | num_prinfo_ = 0; |
| 352 | next_buffer_check_ = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 353 | const_pool_blocked_nesting_ = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 354 | no_const_pool_before_ = 0; |
| 355 | last_const_pool_end_ = 0; |
| 356 | last_bound_pos_ = 0; |
| 357 | current_statement_position_ = RelocInfo::kNoPosition; |
| 358 | current_position_ = RelocInfo::kNoPosition; |
| 359 | written_statement_position_ = current_statement_position_; |
| 360 | written_position_ = current_position_; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | Assembler::~Assembler() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 365 | ASSERT(const_pool_blocked_nesting_ == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 366 | if (own_buffer_) { |
| 367 | if (spare_buffer_ == NULL && buffer_size_ == kMinimalBufferSize) { |
| 368 | spare_buffer_ = buffer_; |
| 369 | } else { |
| 370 | DeleteArray(buffer_); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | |
| 376 | void Assembler::GetCode(CodeDesc* desc) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 377 | // Emit constant pool if necessary. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 378 | CheckConstPool(true, false); |
| 379 | ASSERT(num_prinfo_ == 0); |
| 380 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 381 | // Setup code descriptor. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 382 | desc->buffer = buffer_; |
| 383 | desc->buffer_size = buffer_size_; |
| 384 | desc->instr_size = pc_offset(); |
| 385 | desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos(); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | void Assembler::Align(int m) { |
| 390 | ASSERT(m >= 4 && IsPowerOf2(m)); |
| 391 | while ((pc_offset() & (m - 1)) != 0) { |
| 392 | nop(); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 397 | void Assembler::CodeTargetAlign() { |
| 398 | // Preferred alignment of jump targets on some ARM chips. |
| 399 | Align(8); |
| 400 | } |
| 401 | |
| 402 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 403 | bool Assembler::IsNop(Instr instr, int type) { |
| 404 | // Check for mov rx, rx. |
| 405 | ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop. |
| 406 | return instr == (al | 13*B21 | type*B12 | type); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | bool Assembler::IsBranch(Instr instr) { |
| 411 | return (instr & (B27 | B25)) == (B27 | B25); |
| 412 | } |
| 413 | |
| 414 | |
| 415 | int Assembler::GetBranchOffset(Instr instr) { |
| 416 | ASSERT(IsBranch(instr)); |
| 417 | // Take the jump offset in the lower 24 bits, sign extend it and multiply it |
| 418 | // with 4 to get the offset in bytes. |
| 419 | return ((instr & Imm24Mask) << 8) >> 6; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | bool Assembler::IsLdrRegisterImmediate(Instr instr) { |
| 424 | return (instr & (B27 | B26 | B25 | B22 | B20)) == (B26 | B20); |
| 425 | } |
| 426 | |
| 427 | |
| 428 | int Assembler::GetLdrRegisterImmediateOffset(Instr instr) { |
| 429 | ASSERT(IsLdrRegisterImmediate(instr)); |
| 430 | bool positive = (instr & B23) == B23; |
| 431 | int offset = instr & Off12Mask; // Zero extended offset. |
| 432 | return positive ? offset : -offset; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | Instr Assembler::SetLdrRegisterImmediateOffset(Instr instr, int offset) { |
| 437 | ASSERT(IsLdrRegisterImmediate(instr)); |
| 438 | bool positive = offset >= 0; |
| 439 | if (!positive) offset = -offset; |
| 440 | ASSERT(is_uint12(offset)); |
| 441 | // Set bit indicating whether the offset should be added. |
| 442 | instr = (instr & ~B23) | (positive ? B23 : 0); |
| 443 | // Set the actual offset. |
| 444 | return (instr & ~Off12Mask) | offset; |
| 445 | } |
| 446 | |
| 447 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 448 | bool Assembler::IsStrRegisterImmediate(Instr instr) { |
| 449 | return (instr & (B27 | B26 | B25 | B22 | B20)) == B26; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | Instr Assembler::SetStrRegisterImmediateOffset(Instr instr, int offset) { |
| 454 | ASSERT(IsStrRegisterImmediate(instr)); |
| 455 | bool positive = offset >= 0; |
| 456 | if (!positive) offset = -offset; |
| 457 | ASSERT(is_uint12(offset)); |
| 458 | // Set bit indicating whether the offset should be added. |
| 459 | instr = (instr & ~B23) | (positive ? B23 : 0); |
| 460 | // Set the actual offset. |
| 461 | return (instr & ~Off12Mask) | offset; |
| 462 | } |
| 463 | |
| 464 | |
| 465 | bool Assembler::IsAddRegisterImmediate(Instr instr) { |
| 466 | return (instr & (B27 | B26 | B25 | B24 | B23 | B22 | B21)) == (B25 | B23); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | Instr Assembler::SetAddRegisterImmediateOffset(Instr instr, int offset) { |
| 471 | ASSERT(IsAddRegisterImmediate(instr)); |
| 472 | ASSERT(offset >= 0); |
| 473 | ASSERT(is_uint12(offset)); |
| 474 | // Set the offset. |
| 475 | return (instr & ~Off12Mask) | offset; |
| 476 | } |
| 477 | |
| 478 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 479 | Register Assembler::GetRd(Instr instr) { |
| 480 | Register reg; |
| 481 | reg.code_ = ((instr & kRdMask) >> kRdShift); |
| 482 | return reg; |
| 483 | } |
| 484 | |
| 485 | |
| 486 | bool Assembler::IsPush(Instr instr) { |
| 487 | return ((instr & ~kRdMask) == kPushRegPattern); |
| 488 | } |
| 489 | |
| 490 | |
| 491 | bool Assembler::IsPop(Instr instr) { |
| 492 | return ((instr & ~kRdMask) == kPopRegPattern); |
| 493 | } |
| 494 | |
| 495 | |
| 496 | bool Assembler::IsStrRegFpOffset(Instr instr) { |
| 497 | return ((instr & kLdrStrInstrTypeMask) == kStrRegFpOffsetPattern); |
| 498 | } |
| 499 | |
| 500 | |
| 501 | bool Assembler::IsLdrRegFpOffset(Instr instr) { |
| 502 | return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpOffsetPattern); |
| 503 | } |
| 504 | |
| 505 | |
| 506 | bool Assembler::IsStrRegFpNegOffset(Instr instr) { |
| 507 | return ((instr & kLdrStrInstrTypeMask) == kStrRegFpNegOffsetPattern); |
| 508 | } |
| 509 | |
| 510 | |
| 511 | bool Assembler::IsLdrRegFpNegOffset(Instr instr) { |
| 512 | return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpNegOffsetPattern); |
| 513 | } |
| 514 | |
| 515 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 516 | // Labels refer to positions in the (to be) generated code. |
| 517 | // There are bound, linked, and unused labels. |
| 518 | // |
| 519 | // Bound labels refer to known positions in the already |
| 520 | // generated code. pos() is the position the label refers to. |
| 521 | // |
| 522 | // Linked labels refer to unknown positions in the code |
| 523 | // to be generated; pos() is the position of the last |
| 524 | // instruction using the label. |
| 525 | |
| 526 | |
| 527 | // The link chain is terminated by a negative code position (must be aligned) |
| 528 | const int kEndOfChain = -4; |
| 529 | |
| 530 | |
| 531 | int Assembler::target_at(int pos) { |
| 532 | Instr instr = instr_at(pos); |
| 533 | if ((instr & ~Imm24Mask) == 0) { |
| 534 | // Emitted label constant, not part of a branch. |
| 535 | return instr - (Code::kHeaderSize - kHeapObjectTag); |
| 536 | } |
| 537 | ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24 |
| 538 | int imm26 = ((instr & Imm24Mask) << 8) >> 6; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 539 | if ((instr & CondMask) == nv && (instr & B24) != 0) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 540 | // blx uses bit 24 to encode bit 2 of imm26 |
| 541 | imm26 += 2; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 542 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 543 | return pos + kPcLoadDelta + imm26; |
| 544 | } |
| 545 | |
| 546 | |
| 547 | void Assembler::target_at_put(int pos, int target_pos) { |
| 548 | Instr instr = instr_at(pos); |
| 549 | if ((instr & ~Imm24Mask) == 0) { |
| 550 | ASSERT(target_pos == kEndOfChain || target_pos >= 0); |
| 551 | // Emitted label constant, not part of a branch. |
| 552 | // Make label relative to Code* of generated Code object. |
| 553 | instr_at_put(pos, target_pos + (Code::kHeaderSize - kHeapObjectTag)); |
| 554 | return; |
| 555 | } |
| 556 | int imm26 = target_pos - (pos + kPcLoadDelta); |
| 557 | ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24 |
| 558 | if ((instr & CondMask) == nv) { |
| 559 | // blx uses bit 24 to encode bit 2 of imm26 |
| 560 | ASSERT((imm26 & 1) == 0); |
| 561 | instr = (instr & ~(B24 | Imm24Mask)) | ((imm26 & 2) >> 1)*B24; |
| 562 | } else { |
| 563 | ASSERT((imm26 & 3) == 0); |
| 564 | instr &= ~Imm24Mask; |
| 565 | } |
| 566 | int imm24 = imm26 >> 2; |
| 567 | ASSERT(is_int24(imm24)); |
| 568 | instr_at_put(pos, instr | (imm24 & Imm24Mask)); |
| 569 | } |
| 570 | |
| 571 | |
| 572 | void Assembler::print(Label* L) { |
| 573 | if (L->is_unused()) { |
| 574 | PrintF("unused label\n"); |
| 575 | } else if (L->is_bound()) { |
| 576 | PrintF("bound label to %d\n", L->pos()); |
| 577 | } else if (L->is_linked()) { |
| 578 | Label l = *L; |
| 579 | PrintF("unbound label"); |
| 580 | while (l.is_linked()) { |
| 581 | PrintF("@ %d ", l.pos()); |
| 582 | Instr instr = instr_at(l.pos()); |
| 583 | if ((instr & ~Imm24Mask) == 0) { |
| 584 | PrintF("value\n"); |
| 585 | } else { |
| 586 | ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx |
| 587 | int cond = instr & CondMask; |
| 588 | const char* b; |
| 589 | const char* c; |
| 590 | if (cond == nv) { |
| 591 | b = "blx"; |
| 592 | c = ""; |
| 593 | } else { |
| 594 | if ((instr & B24) != 0) |
| 595 | b = "bl"; |
| 596 | else |
| 597 | b = "b"; |
| 598 | |
| 599 | switch (cond) { |
| 600 | case eq: c = "eq"; break; |
| 601 | case ne: c = "ne"; break; |
| 602 | case hs: c = "hs"; break; |
| 603 | case lo: c = "lo"; break; |
| 604 | case mi: c = "mi"; break; |
| 605 | case pl: c = "pl"; break; |
| 606 | case vs: c = "vs"; break; |
| 607 | case vc: c = "vc"; break; |
| 608 | case hi: c = "hi"; break; |
| 609 | case ls: c = "ls"; break; |
| 610 | case ge: c = "ge"; break; |
| 611 | case lt: c = "lt"; break; |
| 612 | case gt: c = "gt"; break; |
| 613 | case le: c = "le"; break; |
| 614 | case al: c = ""; break; |
| 615 | default: |
| 616 | c = ""; |
| 617 | UNREACHABLE(); |
| 618 | } |
| 619 | } |
| 620 | PrintF("%s%s\n", b, c); |
| 621 | } |
| 622 | next(&l); |
| 623 | } |
| 624 | } else { |
| 625 | PrintF("label in inconsistent state (pos = %d)\n", L->pos_); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | |
| 630 | void Assembler::bind_to(Label* L, int pos) { |
| 631 | ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position |
| 632 | while (L->is_linked()) { |
| 633 | int fixup_pos = L->pos(); |
| 634 | next(L); // call next before overwriting link with target at fixup_pos |
| 635 | target_at_put(fixup_pos, pos); |
| 636 | } |
| 637 | L->bind_to(pos); |
| 638 | |
| 639 | // Keep track of the last bound label so we don't eliminate any instructions |
| 640 | // before a bound label. |
| 641 | if (pos > last_bound_pos_) |
| 642 | last_bound_pos_ = pos; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | void Assembler::link_to(Label* L, Label* appendix) { |
| 647 | if (appendix->is_linked()) { |
| 648 | if (L->is_linked()) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 649 | // Append appendix to L's list. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 650 | int fixup_pos; |
| 651 | int link = L->pos(); |
| 652 | do { |
| 653 | fixup_pos = link; |
| 654 | link = target_at(fixup_pos); |
| 655 | } while (link > 0); |
| 656 | ASSERT(link == kEndOfChain); |
| 657 | target_at_put(fixup_pos, appendix->pos()); |
| 658 | } else { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 659 | // L is empty, simply use appendix. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 660 | *L = *appendix; |
| 661 | } |
| 662 | } |
| 663 | appendix->Unuse(); // appendix should not be used anymore |
| 664 | } |
| 665 | |
| 666 | |
| 667 | void Assembler::bind(Label* L) { |
| 668 | ASSERT(!L->is_bound()); // label can only be bound once |
| 669 | bind_to(L, pc_offset()); |
| 670 | } |
| 671 | |
| 672 | |
| 673 | void Assembler::next(Label* L) { |
| 674 | ASSERT(L->is_linked()); |
| 675 | int link = target_at(L->pos()); |
| 676 | if (link > 0) { |
| 677 | L->link_to(link); |
| 678 | } else { |
| 679 | ASSERT(link == kEndOfChain); |
| 680 | L->Unuse(); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 685 | static Instr EncodeMovwImmediate(uint32_t immediate) { |
| 686 | ASSERT(immediate < 0x10000); |
| 687 | return ((immediate & 0xf000) << 4) | (immediate & 0xfff); |
| 688 | } |
| 689 | |
| 690 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 691 | // Low-level code emission routines depending on the addressing mode. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 692 | // If this returns true then you have to use the rotate_imm and immed_8 |
| 693 | // that it returns, because it may have already changed the instruction |
| 694 | // to match them! |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 695 | static bool fits_shifter(uint32_t imm32, |
| 696 | uint32_t* rotate_imm, |
| 697 | uint32_t* immed_8, |
| 698 | Instr* instr) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 699 | // imm32 must be unsigned. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 700 | for (int rot = 0; rot < 16; rot++) { |
| 701 | uint32_t imm8 = (imm32 << 2*rot) | (imm32 >> (32 - 2*rot)); |
| 702 | if ((imm8 <= 0xff)) { |
| 703 | *rotate_imm = rot; |
| 704 | *immed_8 = imm8; |
| 705 | return true; |
| 706 | } |
| 707 | } |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 708 | // If the opcode is one with a complementary version and the complementary |
| 709 | // immediate fits, change the opcode. |
| 710 | if (instr != NULL) { |
| 711 | if ((*instr & kMovMvnMask) == kMovMvnPattern) { |
| 712 | if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) { |
| 713 | *instr ^= kMovMvnFlip; |
| 714 | return true; |
| 715 | } else if ((*instr & kMovLeaveCCMask) == kMovLeaveCCPattern) { |
| 716 | if (CpuFeatures::IsSupported(ARMv7)) { |
| 717 | if (imm32 < 0x10000) { |
| 718 | *instr ^= kMovwLeaveCCFlip; |
| 719 | *instr |= EncodeMovwImmediate(imm32); |
| 720 | *rotate_imm = *immed_8 = 0; // Not used for movw. |
| 721 | return true; |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | } else if ((*instr & kCmpCmnMask) == kCmpCmnPattern) { |
| 726 | if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) { |
| 727 | *instr ^= kCmpCmnFlip; |
| 728 | return true; |
| 729 | } |
| 730 | } else { |
| 731 | Instr alu_insn = (*instr & kALUMask); |
| 732 | if (alu_insn == kAddPattern || |
| 733 | alu_insn == kSubPattern) { |
| 734 | if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) { |
| 735 | *instr ^= kAddSubFlip; |
| 736 | return true; |
| 737 | } |
| 738 | } else if (alu_insn == kAndPattern || |
| 739 | alu_insn == kBicPattern) { |
| 740 | if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) { |
| 741 | *instr ^= kAndBicFlip; |
| 742 | return true; |
| 743 | } |
| 744 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | return false; |
| 748 | } |
| 749 | |
| 750 | |
| 751 | // We have to use the temporary register for things that can be relocated even |
| 752 | // if they can be encoded in the ARM's 12 bits of immediate-offset instruction |
| 753 | // space. There is no guarantee that the relocated location can be similarly |
| 754 | // encoded. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 755 | static bool MustUseConstantPool(RelocInfo::Mode rmode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 756 | if (rmode == RelocInfo::EXTERNAL_REFERENCE) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 757 | #ifdef DEBUG |
| 758 | if (!Serializer::enabled()) { |
| 759 | Serializer::TooLateToEnableNow(); |
| 760 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 761 | #endif // def DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 762 | return Serializer::enabled(); |
| 763 | } else if (rmode == RelocInfo::NONE) { |
| 764 | return false; |
| 765 | } |
| 766 | return true; |
| 767 | } |
| 768 | |
| 769 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 770 | bool Operand::is_single_instruction() const { |
| 771 | if (rm_.is_valid()) return true; |
| 772 | if (MustUseConstantPool(rmode_)) return false; |
| 773 | uint32_t dummy1, dummy2; |
| 774 | return fits_shifter(imm32_, &dummy1, &dummy2, NULL); |
| 775 | } |
| 776 | |
| 777 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 778 | void Assembler::addrmod1(Instr instr, |
| 779 | Register rn, |
| 780 | Register rd, |
| 781 | const Operand& x) { |
| 782 | CheckBuffer(); |
| 783 | ASSERT((instr & ~(CondMask | OpCodeMask | S)) == 0); |
| 784 | if (!x.rm_.is_valid()) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 785 | // Immediate. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 786 | uint32_t rotate_imm; |
| 787 | uint32_t immed_8; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 788 | if (MustUseConstantPool(x.rmode_) || |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 789 | !fits_shifter(x.imm32_, &rotate_imm, &immed_8, &instr)) { |
| 790 | // The immediate operand cannot be encoded as a shifter operand, so load |
| 791 | // it first to register ip and change the original instruction to use ip. |
| 792 | // However, if the original instruction is a 'mov rd, x' (not setting the |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 793 | // condition code), then replace it with a 'ldr rd, [pc]'. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 794 | CHECK(!rn.is(ip)); // rn should never be ip, or will be trashed |
| 795 | Condition cond = static_cast<Condition>(instr & CondMask); |
| 796 | if ((instr & ~CondMask) == 13*B21) { // mov, S not set |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 797 | if (MustUseConstantPool(x.rmode_) || |
| 798 | !CpuFeatures::IsSupported(ARMv7)) { |
| 799 | RecordRelocInfo(x.rmode_, x.imm32_); |
| 800 | ldr(rd, MemOperand(pc, 0), cond); |
| 801 | } else { |
| 802 | // Will probably use movw, will certainly not use constant pool. |
| 803 | mov(rd, Operand(x.imm32_ & 0xffff), LeaveCC, cond); |
| 804 | movt(rd, static_cast<uint32_t>(x.imm32_) >> 16, cond); |
| 805 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 806 | } else { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 807 | // If this is not a mov or mvn instruction we may still be able to avoid |
| 808 | // a constant pool entry by using mvn or movw. |
| 809 | if (!MustUseConstantPool(x.rmode_) && |
| 810 | (instr & kMovMvnMask) != kMovMvnPattern) { |
| 811 | mov(ip, x, LeaveCC, cond); |
| 812 | } else { |
| 813 | RecordRelocInfo(x.rmode_, x.imm32_); |
| 814 | ldr(ip, MemOperand(pc, 0), cond); |
| 815 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 816 | addrmod1(instr, rn, rd, Operand(ip)); |
| 817 | } |
| 818 | return; |
| 819 | } |
| 820 | instr |= I | rotate_imm*B8 | immed_8; |
| 821 | } else if (!x.rs_.is_valid()) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 822 | // Immediate shift. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 823 | instr |= x.shift_imm_*B7 | x.shift_op_ | x.rm_.code(); |
| 824 | } else { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 825 | // Register shift. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 826 | ASSERT(!rn.is(pc) && !rd.is(pc) && !x.rm_.is(pc) && !x.rs_.is(pc)); |
| 827 | instr |= x.rs_.code()*B8 | x.shift_op_ | B4 | x.rm_.code(); |
| 828 | } |
| 829 | emit(instr | rn.code()*B16 | rd.code()*B12); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 830 | if (rn.is(pc) || x.rm_.is(pc)) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 831 | // Block constant pool emission for one instruction after reading pc. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 832 | BlockConstPoolBefore(pc_offset() + kInstrSize); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 833 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | |
| 837 | void Assembler::addrmod2(Instr instr, Register rd, const MemOperand& x) { |
| 838 | ASSERT((instr & ~(CondMask | B | L)) == B26); |
| 839 | int am = x.am_; |
| 840 | if (!x.rm_.is_valid()) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 841 | // Immediate offset. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 842 | int offset_12 = x.offset_; |
| 843 | if (offset_12 < 0) { |
| 844 | offset_12 = -offset_12; |
| 845 | am ^= U; |
| 846 | } |
| 847 | if (!is_uint12(offset_12)) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 848 | // Immediate offset cannot be encoded, load it first to register ip |
| 849 | // rn (and rd in a load) should never be ip, or will be trashed. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 850 | ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip))); |
| 851 | mov(ip, Operand(x.offset_), LeaveCC, |
| 852 | static_cast<Condition>(instr & CondMask)); |
| 853 | addrmod2(instr, rd, MemOperand(x.rn_, ip, x.am_)); |
| 854 | return; |
| 855 | } |
| 856 | ASSERT(offset_12 >= 0); // no masking needed |
| 857 | instr |= offset_12; |
| 858 | } else { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 859 | // Register offset (shift_imm_ and shift_op_ are 0) or scaled |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 860 | // register offset the constructors make sure than both shift_imm_ |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 861 | // and shift_op_ are initialized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 862 | ASSERT(!x.rm_.is(pc)); |
| 863 | instr |= B25 | x.shift_imm_*B7 | x.shift_op_ | x.rm_.code(); |
| 864 | } |
| 865 | ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback |
| 866 | emit(instr | am | x.rn_.code()*B16 | rd.code()*B12); |
| 867 | } |
| 868 | |
| 869 | |
| 870 | void Assembler::addrmod3(Instr instr, Register rd, const MemOperand& x) { |
| 871 | ASSERT((instr & ~(CondMask | L | S6 | H)) == (B4 | B7)); |
| 872 | ASSERT(x.rn_.is_valid()); |
| 873 | int am = x.am_; |
| 874 | if (!x.rm_.is_valid()) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 875 | // Immediate offset. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 876 | int offset_8 = x.offset_; |
| 877 | if (offset_8 < 0) { |
| 878 | offset_8 = -offset_8; |
| 879 | am ^= U; |
| 880 | } |
| 881 | if (!is_uint8(offset_8)) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 882 | // Immediate offset cannot be encoded, load it first to register ip |
| 883 | // rn (and rd in a load) should never be ip, or will be trashed. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 884 | ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip))); |
| 885 | mov(ip, Operand(x.offset_), LeaveCC, |
| 886 | static_cast<Condition>(instr & CondMask)); |
| 887 | addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_)); |
| 888 | return; |
| 889 | } |
| 890 | ASSERT(offset_8 >= 0); // no masking needed |
| 891 | instr |= B | (offset_8 >> 4)*B8 | (offset_8 & 0xf); |
| 892 | } else if (x.shift_imm_ != 0) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 893 | // Scaled register offset not supported, load index first |
| 894 | // rn (and rd in a load) should never be ip, or will be trashed. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 895 | ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip))); |
| 896 | mov(ip, Operand(x.rm_, x.shift_op_, x.shift_imm_), LeaveCC, |
| 897 | static_cast<Condition>(instr & CondMask)); |
| 898 | addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_)); |
| 899 | return; |
| 900 | } else { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 901 | // Register offset. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 902 | ASSERT((am & (P|W)) == P || !x.rm_.is(pc)); // no pc index with writeback |
| 903 | instr |= x.rm_.code(); |
| 904 | } |
| 905 | ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback |
| 906 | emit(instr | am | x.rn_.code()*B16 | rd.code()*B12); |
| 907 | } |
| 908 | |
| 909 | |
| 910 | void Assembler::addrmod4(Instr instr, Register rn, RegList rl) { |
| 911 | ASSERT((instr & ~(CondMask | P | U | W | L)) == B27); |
| 912 | ASSERT(rl != 0); |
| 913 | ASSERT(!rn.is(pc)); |
| 914 | emit(instr | rn.code()*B16 | rl); |
| 915 | } |
| 916 | |
| 917 | |
| 918 | void Assembler::addrmod5(Instr instr, CRegister crd, const MemOperand& x) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 919 | // Unindexed addressing is not encoded by this function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 920 | ASSERT_EQ((B27 | B26), |
| 921 | (instr & ~(CondMask | CoprocessorMask | P | U | N | W | L))); |
| 922 | ASSERT(x.rn_.is_valid() && !x.rm_.is_valid()); |
| 923 | int am = x.am_; |
| 924 | int offset_8 = x.offset_; |
| 925 | ASSERT((offset_8 & 3) == 0); // offset must be an aligned word offset |
| 926 | offset_8 >>= 2; |
| 927 | if (offset_8 < 0) { |
| 928 | offset_8 = -offset_8; |
| 929 | am ^= U; |
| 930 | } |
| 931 | ASSERT(is_uint8(offset_8)); // unsigned word offset must fit in a byte |
| 932 | ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback |
| 933 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 934 | // Post-indexed addressing requires W == 1; different than in addrmod2/3. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 935 | if ((am & P) == 0) |
| 936 | am |= W; |
| 937 | |
| 938 | ASSERT(offset_8 >= 0); // no masking needed |
| 939 | emit(instr | am | x.rn_.code()*B16 | crd.code()*B12 | offset_8); |
| 940 | } |
| 941 | |
| 942 | |
| 943 | int Assembler::branch_offset(Label* L, bool jump_elimination_allowed) { |
| 944 | int target_pos; |
| 945 | if (L->is_bound()) { |
| 946 | target_pos = L->pos(); |
| 947 | } else { |
| 948 | if (L->is_linked()) { |
| 949 | target_pos = L->pos(); // L's link |
| 950 | } else { |
| 951 | target_pos = kEndOfChain; |
| 952 | } |
| 953 | L->link_to(pc_offset()); |
| 954 | } |
| 955 | |
| 956 | // Block the emission of the constant pool, since the branch instruction must |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 957 | // be emitted at the pc offset recorded by the label. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 958 | BlockConstPoolBefore(pc_offset() + kInstrSize); |
| 959 | return target_pos - (pc_offset() + kPcLoadDelta); |
| 960 | } |
| 961 | |
| 962 | |
| 963 | void Assembler::label_at_put(Label* L, int at_offset) { |
| 964 | int target_pos; |
| 965 | if (L->is_bound()) { |
| 966 | target_pos = L->pos(); |
| 967 | } else { |
| 968 | if (L->is_linked()) { |
| 969 | target_pos = L->pos(); // L's link |
| 970 | } else { |
| 971 | target_pos = kEndOfChain; |
| 972 | } |
| 973 | L->link_to(at_offset); |
| 974 | instr_at_put(at_offset, target_pos + (Code::kHeaderSize - kHeapObjectTag)); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 979 | // Branch instructions. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 980 | void Assembler::b(int branch_offset, Condition cond) { |
| 981 | ASSERT((branch_offset & 3) == 0); |
| 982 | int imm24 = branch_offset >> 2; |
| 983 | ASSERT(is_int24(imm24)); |
| 984 | emit(cond | B27 | B25 | (imm24 & Imm24Mask)); |
| 985 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 986 | if (cond == al) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 987 | // Dead code is a good location to emit the constant pool. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 988 | CheckConstPool(false, false); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 989 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | |
| 993 | void Assembler::bl(int branch_offset, Condition cond) { |
| 994 | ASSERT((branch_offset & 3) == 0); |
| 995 | int imm24 = branch_offset >> 2; |
| 996 | ASSERT(is_int24(imm24)); |
| 997 | emit(cond | B27 | B25 | B24 | (imm24 & Imm24Mask)); |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | void Assembler::blx(int branch_offset) { // v5 and above |
| 1002 | WriteRecordedPositions(); |
| 1003 | ASSERT((branch_offset & 1) == 0); |
| 1004 | int h = ((branch_offset & 2) >> 1)*B24; |
| 1005 | int imm24 = branch_offset >> 2; |
| 1006 | ASSERT(is_int24(imm24)); |
| 1007 | emit(15 << 28 | B27 | B25 | h | (imm24 & Imm24Mask)); |
| 1008 | } |
| 1009 | |
| 1010 | |
| 1011 | void Assembler::blx(Register target, Condition cond) { // v5 and above |
| 1012 | WriteRecordedPositions(); |
| 1013 | ASSERT(!target.is(pc)); |
| 1014 | emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | 3*B4 | target.code()); |
| 1015 | } |
| 1016 | |
| 1017 | |
| 1018 | void Assembler::bx(Register target, Condition cond) { // v5 and above, plus v4t |
| 1019 | WriteRecordedPositions(); |
| 1020 | ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged |
| 1021 | emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | B4 | target.code()); |
| 1022 | } |
| 1023 | |
| 1024 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1025 | // Data-processing instructions. |
| 1026 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1027 | void Assembler::and_(Register dst, Register src1, const Operand& src2, |
| 1028 | SBit s, Condition cond) { |
| 1029 | addrmod1(cond | 0*B21 | s, src1, dst, src2); |
| 1030 | } |
| 1031 | |
| 1032 | |
| 1033 | void Assembler::eor(Register dst, Register src1, const Operand& src2, |
| 1034 | SBit s, Condition cond) { |
| 1035 | addrmod1(cond | 1*B21 | s, src1, dst, src2); |
| 1036 | } |
| 1037 | |
| 1038 | |
| 1039 | void Assembler::sub(Register dst, Register src1, const Operand& src2, |
| 1040 | SBit s, Condition cond) { |
| 1041 | addrmod1(cond | 2*B21 | s, src1, dst, src2); |
| 1042 | } |
| 1043 | |
| 1044 | |
| 1045 | void Assembler::rsb(Register dst, Register src1, const Operand& src2, |
| 1046 | SBit s, Condition cond) { |
| 1047 | addrmod1(cond | 3*B21 | s, src1, dst, src2); |
| 1048 | } |
| 1049 | |
| 1050 | |
| 1051 | void Assembler::add(Register dst, Register src1, const Operand& src2, |
| 1052 | SBit s, Condition cond) { |
| 1053 | addrmod1(cond | 4*B21 | s, src1, dst, src2); |
| 1054 | |
| 1055 | // Eliminate pattern: push(r), pop() |
| 1056 | // str(src, MemOperand(sp, 4, NegPreIndex), al); |
| 1057 | // add(sp, sp, Operand(kPointerSize)); |
| 1058 | // Both instructions can be eliminated. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1059 | if (can_peephole_optimize(2) && |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1060 | // Pattern. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1061 | instr_at(pc_ - 1 * kInstrSize) == kPopInstruction && |
| 1062 | (instr_at(pc_ - 2 * kInstrSize) & ~RdMask) == kPushRegPattern) { |
| 1063 | pc_ -= 2 * kInstrSize; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1064 | if (FLAG_print_peephole_optimization) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1065 | PrintF("%x push(reg)/pop() eliminated\n", pc_offset()); |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | void Assembler::adc(Register dst, Register src1, const Operand& src2, |
| 1072 | SBit s, Condition cond) { |
| 1073 | addrmod1(cond | 5*B21 | s, src1, dst, src2); |
| 1074 | } |
| 1075 | |
| 1076 | |
| 1077 | void Assembler::sbc(Register dst, Register src1, const Operand& src2, |
| 1078 | SBit s, Condition cond) { |
| 1079 | addrmod1(cond | 6*B21 | s, src1, dst, src2); |
| 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | void Assembler::rsc(Register dst, Register src1, const Operand& src2, |
| 1084 | SBit s, Condition cond) { |
| 1085 | addrmod1(cond | 7*B21 | s, src1, dst, src2); |
| 1086 | } |
| 1087 | |
| 1088 | |
| 1089 | void Assembler::tst(Register src1, const Operand& src2, Condition cond) { |
| 1090 | addrmod1(cond | 8*B21 | S, src1, r0, src2); |
| 1091 | } |
| 1092 | |
| 1093 | |
| 1094 | void Assembler::teq(Register src1, const Operand& src2, Condition cond) { |
| 1095 | addrmod1(cond | 9*B21 | S, src1, r0, src2); |
| 1096 | } |
| 1097 | |
| 1098 | |
| 1099 | void Assembler::cmp(Register src1, const Operand& src2, Condition cond) { |
| 1100 | addrmod1(cond | 10*B21 | S, src1, r0, src2); |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | void Assembler::cmn(Register src1, const Operand& src2, Condition cond) { |
| 1105 | addrmod1(cond | 11*B21 | S, src1, r0, src2); |
| 1106 | } |
| 1107 | |
| 1108 | |
| 1109 | void Assembler::orr(Register dst, Register src1, const Operand& src2, |
| 1110 | SBit s, Condition cond) { |
| 1111 | addrmod1(cond | 12*B21 | s, src1, dst, src2); |
| 1112 | } |
| 1113 | |
| 1114 | |
| 1115 | void Assembler::mov(Register dst, const Operand& src, SBit s, Condition cond) { |
| 1116 | if (dst.is(pc)) { |
| 1117 | WriteRecordedPositions(); |
| 1118 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1119 | // Don't allow nop instructions in the form mov rn, rn to be generated using |
| 1120 | // the mov instruction. They must be generated using nop(int) |
| 1121 | // pseudo instructions. |
| 1122 | ASSERT(!(src.is_reg() && src.rm().is(dst) && s == LeaveCC && cond == al)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1123 | addrmod1(cond | 13*B21 | s, r0, dst, src); |
| 1124 | } |
| 1125 | |
| 1126 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1127 | void Assembler::movw(Register reg, uint32_t immediate, Condition cond) { |
| 1128 | ASSERT(immediate < 0x10000); |
| 1129 | mov(reg, Operand(immediate), LeaveCC, cond); |
| 1130 | } |
| 1131 | |
| 1132 | |
| 1133 | void Assembler::movt(Register reg, uint32_t immediate, Condition cond) { |
| 1134 | emit(cond | 0x34*B20 | reg.code()*B12 | EncodeMovwImmediate(immediate)); |
| 1135 | } |
| 1136 | |
| 1137 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1138 | void Assembler::bic(Register dst, Register src1, const Operand& src2, |
| 1139 | SBit s, Condition cond) { |
| 1140 | addrmod1(cond | 14*B21 | s, src1, dst, src2); |
| 1141 | } |
| 1142 | |
| 1143 | |
| 1144 | void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) { |
| 1145 | addrmod1(cond | 15*B21 | s, r0, dst, src); |
| 1146 | } |
| 1147 | |
| 1148 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1149 | // Multiply instructions. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1150 | void Assembler::mla(Register dst, Register src1, Register src2, Register srcA, |
| 1151 | SBit s, Condition cond) { |
| 1152 | ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc) && !srcA.is(pc)); |
| 1153 | emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 | |
| 1154 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | void Assembler::mul(Register dst, Register src1, Register src2, |
| 1159 | SBit s, Condition cond) { |
| 1160 | ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc)); |
| 1161 | // dst goes in bits 16-19 for this instruction! |
| 1162 | emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1163 | } |
| 1164 | |
| 1165 | |
| 1166 | void Assembler::smlal(Register dstL, |
| 1167 | Register dstH, |
| 1168 | Register src1, |
| 1169 | Register src2, |
| 1170 | SBit s, |
| 1171 | Condition cond) { |
| 1172 | ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc)); |
| 1173 | ASSERT(!dstL.is(dstH)); |
| 1174 | emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 | |
| 1175 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | void Assembler::smull(Register dstL, |
| 1180 | Register dstH, |
| 1181 | Register src1, |
| 1182 | Register src2, |
| 1183 | SBit s, |
| 1184 | Condition cond) { |
| 1185 | ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc)); |
| 1186 | ASSERT(!dstL.is(dstH)); |
| 1187 | emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 | |
| 1188 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1189 | } |
| 1190 | |
| 1191 | |
| 1192 | void Assembler::umlal(Register dstL, |
| 1193 | Register dstH, |
| 1194 | Register src1, |
| 1195 | Register src2, |
| 1196 | SBit s, |
| 1197 | Condition cond) { |
| 1198 | ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc)); |
| 1199 | ASSERT(!dstL.is(dstH)); |
| 1200 | emit(cond | B23 | A | s | dstH.code()*B16 | dstL.code()*B12 | |
| 1201 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1202 | } |
| 1203 | |
| 1204 | |
| 1205 | void Assembler::umull(Register dstL, |
| 1206 | Register dstH, |
| 1207 | Register src1, |
| 1208 | Register src2, |
| 1209 | SBit s, |
| 1210 | Condition cond) { |
| 1211 | ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc)); |
| 1212 | ASSERT(!dstL.is(dstH)); |
| 1213 | emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 | |
| 1214 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1215 | } |
| 1216 | |
| 1217 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1218 | // Miscellaneous arithmetic instructions. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1219 | void Assembler::clz(Register dst, Register src, Condition cond) { |
| 1220 | // v5 and above. |
| 1221 | ASSERT(!dst.is(pc) && !src.is(pc)); |
| 1222 | emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 | |
| 1223 | 15*B8 | B4 | src.code()); |
| 1224 | } |
| 1225 | |
| 1226 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 1227 | // Saturating instructions. |
| 1228 | |
| 1229 | // Unsigned saturate. |
| 1230 | void Assembler::usat(Register dst, |
| 1231 | int satpos, |
| 1232 | const Operand& src, |
| 1233 | Condition cond) { |
| 1234 | // v6 and above. |
| 1235 | ASSERT(CpuFeatures::IsSupported(ARMv7)); |
| 1236 | ASSERT(!dst.is(pc) && !src.rm_.is(pc)); |
| 1237 | ASSERT((satpos >= 0) && (satpos <= 31)); |
| 1238 | ASSERT((src.shift_op_ == ASR) || (src.shift_op_ == LSL)); |
| 1239 | ASSERT(src.rs_.is(no_reg)); |
| 1240 | |
| 1241 | int sh = 0; |
| 1242 | if (src.shift_op_ == ASR) { |
| 1243 | sh = 1; |
| 1244 | } |
| 1245 | |
| 1246 | emit(cond | 0x6*B24 | 0xe*B20 | satpos*B16 | dst.code()*B12 | |
| 1247 | src.shift_imm_*B7 | sh*B6 | 0x1*B4 | src.rm_.code()); |
| 1248 | } |
| 1249 | |
| 1250 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1251 | // Bitfield manipulation instructions. |
| 1252 | |
| 1253 | // Unsigned bit field extract. |
| 1254 | // Extracts #width adjacent bits from position #lsb in a register, and |
| 1255 | // writes them to the low bits of a destination register. |
| 1256 | // ubfx dst, src, #lsb, #width |
| 1257 | void Assembler::ubfx(Register dst, |
| 1258 | Register src, |
| 1259 | int lsb, |
| 1260 | int width, |
| 1261 | Condition cond) { |
| 1262 | // v7 and above. |
| 1263 | ASSERT(CpuFeatures::IsSupported(ARMv7)); |
| 1264 | ASSERT(!dst.is(pc) && !src.is(pc)); |
| 1265 | ASSERT((lsb >= 0) && (lsb <= 31)); |
| 1266 | ASSERT((width >= 1) && (width <= (32 - lsb))); |
| 1267 | emit(cond | 0xf*B23 | B22 | B21 | (width - 1)*B16 | dst.code()*B12 | |
| 1268 | lsb*B7 | B6 | B4 | src.code()); |
| 1269 | } |
| 1270 | |
| 1271 | |
| 1272 | // Signed bit field extract. |
| 1273 | // Extracts #width adjacent bits from position #lsb in a register, and |
| 1274 | // writes them to the low bits of a destination register. The extracted |
| 1275 | // value is sign extended to fill the destination register. |
| 1276 | // sbfx dst, src, #lsb, #width |
| 1277 | void Assembler::sbfx(Register dst, |
| 1278 | Register src, |
| 1279 | int lsb, |
| 1280 | int width, |
| 1281 | Condition cond) { |
| 1282 | // v7 and above. |
| 1283 | ASSERT(CpuFeatures::IsSupported(ARMv7)); |
| 1284 | ASSERT(!dst.is(pc) && !src.is(pc)); |
| 1285 | ASSERT((lsb >= 0) && (lsb <= 31)); |
| 1286 | ASSERT((width >= 1) && (width <= (32 - lsb))); |
| 1287 | emit(cond | 0xf*B23 | B21 | (width - 1)*B16 | dst.code()*B12 | |
| 1288 | lsb*B7 | B6 | B4 | src.code()); |
| 1289 | } |
| 1290 | |
| 1291 | |
| 1292 | // Bit field clear. |
| 1293 | // Sets #width adjacent bits at position #lsb in the destination register |
| 1294 | // to zero, preserving the value of the other bits. |
| 1295 | // bfc dst, #lsb, #width |
| 1296 | void Assembler::bfc(Register dst, int lsb, int width, Condition cond) { |
| 1297 | // v7 and above. |
| 1298 | ASSERT(CpuFeatures::IsSupported(ARMv7)); |
| 1299 | ASSERT(!dst.is(pc)); |
| 1300 | ASSERT((lsb >= 0) && (lsb <= 31)); |
| 1301 | ASSERT((width >= 1) && (width <= (32 - lsb))); |
| 1302 | int msb = lsb + width - 1; |
| 1303 | emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 | 0xf); |
| 1304 | } |
| 1305 | |
| 1306 | |
| 1307 | // Bit field insert. |
| 1308 | // Inserts #width adjacent bits from the low bits of the source register |
| 1309 | // into position #lsb of the destination register. |
| 1310 | // bfi dst, src, #lsb, #width |
| 1311 | void Assembler::bfi(Register dst, |
| 1312 | Register src, |
| 1313 | int lsb, |
| 1314 | int width, |
| 1315 | Condition cond) { |
| 1316 | // v7 and above. |
| 1317 | ASSERT(CpuFeatures::IsSupported(ARMv7)); |
| 1318 | ASSERT(!dst.is(pc) && !src.is(pc)); |
| 1319 | ASSERT((lsb >= 0) && (lsb <= 31)); |
| 1320 | ASSERT((width >= 1) && (width <= (32 - lsb))); |
| 1321 | int msb = lsb + width - 1; |
| 1322 | emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 | |
| 1323 | src.code()); |
| 1324 | } |
| 1325 | |
| 1326 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1327 | // Status register access instructions. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1328 | void Assembler::mrs(Register dst, SRegister s, Condition cond) { |
| 1329 | ASSERT(!dst.is(pc)); |
| 1330 | emit(cond | B24 | s | 15*B16 | dst.code()*B12); |
| 1331 | } |
| 1332 | |
| 1333 | |
| 1334 | void Assembler::msr(SRegisterFieldMask fields, const Operand& src, |
| 1335 | Condition cond) { |
| 1336 | ASSERT(fields >= B16 && fields < B20); // at least one field set |
| 1337 | Instr instr; |
| 1338 | if (!src.rm_.is_valid()) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1339 | // Immediate. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1340 | uint32_t rotate_imm; |
| 1341 | uint32_t immed_8; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1342 | if (MustUseConstantPool(src.rmode_) || |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1343 | !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1344 | // Immediate operand cannot be encoded, load it first to register ip. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1345 | RecordRelocInfo(src.rmode_, src.imm32_); |
| 1346 | ldr(ip, MemOperand(pc, 0), cond); |
| 1347 | msr(fields, Operand(ip), cond); |
| 1348 | return; |
| 1349 | } |
| 1350 | instr = I | rotate_imm*B8 | immed_8; |
| 1351 | } else { |
| 1352 | ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed |
| 1353 | instr = src.rm_.code(); |
| 1354 | } |
| 1355 | emit(cond | instr | B24 | B21 | fields | 15*B12); |
| 1356 | } |
| 1357 | |
| 1358 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1359 | // Load/Store instructions. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1360 | void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) { |
| 1361 | if (dst.is(pc)) { |
| 1362 | WriteRecordedPositions(); |
| 1363 | } |
| 1364 | addrmod2(cond | B26 | L, dst, src); |
| 1365 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1366 | // Eliminate pattern: push(ry), pop(rx) |
| 1367 | // str(ry, MemOperand(sp, 4, NegPreIndex), al) |
| 1368 | // ldr(rx, MemOperand(sp, 4, PostIndex), al) |
| 1369 | // Both instructions can be eliminated if ry = rx. |
| 1370 | // If ry != rx, a register copy from ry to rx is inserted |
| 1371 | // after eliminating the push and the pop instructions. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1372 | if (can_peephole_optimize(2)) { |
| 1373 | Instr push_instr = instr_at(pc_ - 2 * kInstrSize); |
| 1374 | Instr pop_instr = instr_at(pc_ - 1 * kInstrSize); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1375 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1376 | if (IsPush(push_instr) && IsPop(pop_instr)) { |
| 1377 | if ((pop_instr & kRdMask) != (push_instr & kRdMask)) { |
| 1378 | // For consecutive push and pop on different registers, |
| 1379 | // we delete both the push & pop and insert a register move. |
| 1380 | // push ry, pop rx --> mov rx, ry |
| 1381 | Register reg_pushed, reg_popped; |
| 1382 | reg_pushed = GetRd(push_instr); |
| 1383 | reg_popped = GetRd(pop_instr); |
| 1384 | pc_ -= 2 * kInstrSize; |
| 1385 | // Insert a mov instruction, which is better than a pair of push & pop |
| 1386 | mov(reg_popped, reg_pushed); |
| 1387 | if (FLAG_print_peephole_optimization) { |
| 1388 | PrintF("%x push/pop (diff reg) replaced by a reg move\n", |
| 1389 | pc_offset()); |
| 1390 | } |
| 1391 | } else { |
| 1392 | // For consecutive push and pop on the same register, |
| 1393 | // both the push and the pop can be deleted. |
| 1394 | pc_ -= 2 * kInstrSize; |
| 1395 | if (FLAG_print_peephole_optimization) { |
| 1396 | PrintF("%x push/pop (same reg) eliminated\n", pc_offset()); |
| 1397 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | if (can_peephole_optimize(2)) { |
| 1403 | Instr str_instr = instr_at(pc_ - 2 * kInstrSize); |
| 1404 | Instr ldr_instr = instr_at(pc_ - 1 * kInstrSize); |
| 1405 | |
| 1406 | if ((IsStrRegFpOffset(str_instr) && |
| 1407 | IsLdrRegFpOffset(ldr_instr)) || |
| 1408 | (IsStrRegFpNegOffset(str_instr) && |
| 1409 | IsLdrRegFpNegOffset(ldr_instr))) { |
| 1410 | if ((ldr_instr & kLdrStrInstrArgumentMask) == |
| 1411 | (str_instr & kLdrStrInstrArgumentMask)) { |
| 1412 | // Pattern: Ldr/str same fp+offset, same register. |
| 1413 | // |
| 1414 | // The following: |
| 1415 | // str rx, [fp, #-12] |
| 1416 | // ldr rx, [fp, #-12] |
| 1417 | // |
| 1418 | // Becomes: |
| 1419 | // str rx, [fp, #-12] |
| 1420 | |
| 1421 | pc_ -= 1 * kInstrSize; |
| 1422 | if (FLAG_print_peephole_optimization) { |
| 1423 | PrintF("%x str/ldr (fp + same offset), same reg\n", pc_offset()); |
| 1424 | } |
| 1425 | } else if ((ldr_instr & kLdrStrOffsetMask) == |
| 1426 | (str_instr & kLdrStrOffsetMask)) { |
| 1427 | // Pattern: Ldr/str same fp+offset, different register. |
| 1428 | // |
| 1429 | // The following: |
| 1430 | // str rx, [fp, #-12] |
| 1431 | // ldr ry, [fp, #-12] |
| 1432 | // |
| 1433 | // Becomes: |
| 1434 | // str rx, [fp, #-12] |
| 1435 | // mov ry, rx |
| 1436 | |
| 1437 | Register reg_stored, reg_loaded; |
| 1438 | reg_stored = GetRd(str_instr); |
| 1439 | reg_loaded = GetRd(ldr_instr); |
| 1440 | pc_ -= 1 * kInstrSize; |
| 1441 | // Insert a mov instruction, which is better than ldr. |
| 1442 | mov(reg_loaded, reg_stored); |
| 1443 | if (FLAG_print_peephole_optimization) { |
| 1444 | PrintF("%x str/ldr (fp + same offset), diff reg \n", pc_offset()); |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | if (can_peephole_optimize(3)) { |
| 1451 | Instr mem_write_instr = instr_at(pc_ - 3 * kInstrSize); |
| 1452 | Instr ldr_instr = instr_at(pc_ - 2 * kInstrSize); |
| 1453 | Instr mem_read_instr = instr_at(pc_ - 1 * kInstrSize); |
| 1454 | if (IsPush(mem_write_instr) && |
| 1455 | IsPop(mem_read_instr)) { |
| 1456 | if ((IsLdrRegFpOffset(ldr_instr) || |
| 1457 | IsLdrRegFpNegOffset(ldr_instr))) { |
| 1458 | if ((mem_write_instr & kRdMask) == |
| 1459 | (mem_read_instr & kRdMask)) { |
| 1460 | // Pattern: push & pop from/to same register, |
| 1461 | // with a fp+offset ldr in between |
| 1462 | // |
| 1463 | // The following: |
| 1464 | // str rx, [sp, #-4]! |
| 1465 | // ldr rz, [fp, #-24] |
| 1466 | // ldr rx, [sp], #+4 |
| 1467 | // |
| 1468 | // Becomes: |
| 1469 | // if(rx == rz) |
| 1470 | // delete all |
| 1471 | // else |
| 1472 | // ldr rz, [fp, #-24] |
| 1473 | |
| 1474 | if ((mem_write_instr & kRdMask) == (ldr_instr & kRdMask)) { |
| 1475 | pc_ -= 3 * kInstrSize; |
| 1476 | } else { |
| 1477 | pc_ -= 3 * kInstrSize; |
| 1478 | // Reinsert back the ldr rz. |
| 1479 | emit(ldr_instr); |
| 1480 | } |
| 1481 | if (FLAG_print_peephole_optimization) { |
| 1482 | PrintF("%x push/pop -dead ldr fp+offset in middle\n", pc_offset()); |
| 1483 | } |
| 1484 | } else { |
| 1485 | // Pattern: push & pop from/to different registers |
| 1486 | // with a fp+offset ldr in between |
| 1487 | // |
| 1488 | // The following: |
| 1489 | // str rx, [sp, #-4]! |
| 1490 | // ldr rz, [fp, #-24] |
| 1491 | // ldr ry, [sp], #+4 |
| 1492 | // |
| 1493 | // Becomes: |
| 1494 | // if(ry == rz) |
| 1495 | // mov ry, rx; |
| 1496 | // else if(rx != rz) |
| 1497 | // ldr rz, [fp, #-24] |
| 1498 | // mov ry, rx |
| 1499 | // else if((ry != rz) || (rx == rz)) becomes: |
| 1500 | // mov ry, rx |
| 1501 | // ldr rz, [fp, #-24] |
| 1502 | |
| 1503 | Register reg_pushed, reg_popped; |
| 1504 | if ((mem_read_instr & kRdMask) == (ldr_instr & kRdMask)) { |
| 1505 | reg_pushed = GetRd(mem_write_instr); |
| 1506 | reg_popped = GetRd(mem_read_instr); |
| 1507 | pc_ -= 3 * kInstrSize; |
| 1508 | mov(reg_popped, reg_pushed); |
| 1509 | } else if ((mem_write_instr & kRdMask) |
| 1510 | != (ldr_instr & kRdMask)) { |
| 1511 | reg_pushed = GetRd(mem_write_instr); |
| 1512 | reg_popped = GetRd(mem_read_instr); |
| 1513 | pc_ -= 3 * kInstrSize; |
| 1514 | emit(ldr_instr); |
| 1515 | mov(reg_popped, reg_pushed); |
| 1516 | } else if (((mem_read_instr & kRdMask) |
| 1517 | != (ldr_instr & kRdMask)) || |
| 1518 | ((mem_write_instr & kRdMask) |
| 1519 | == (ldr_instr & kRdMask)) ) { |
| 1520 | reg_pushed = GetRd(mem_write_instr); |
| 1521 | reg_popped = GetRd(mem_read_instr); |
| 1522 | pc_ -= 3 * kInstrSize; |
| 1523 | mov(reg_popped, reg_pushed); |
| 1524 | emit(ldr_instr); |
| 1525 | } |
| 1526 | if (FLAG_print_peephole_optimization) { |
| 1527 | PrintF("%x push/pop (ldr fp+off in middle)\n", pc_offset()); |
| 1528 | } |
| 1529 | } |
| 1530 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | |
| 1536 | void Assembler::str(Register src, const MemOperand& dst, Condition cond) { |
| 1537 | addrmod2(cond | B26, src, dst); |
| 1538 | |
| 1539 | // Eliminate pattern: pop(), push(r) |
| 1540 | // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al |
| 1541 | // -> str r, [sp, 0], al |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1542 | if (can_peephole_optimize(2) && |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1543 | // Pattern. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1544 | instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) && |
| 1545 | instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) { |
| 1546 | pc_ -= 2 * kInstrSize; |
| 1547 | emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1548 | if (FLAG_print_peephole_optimization) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1549 | PrintF("%x pop()/push(reg) eliminated\n", pc_offset()); |
| 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | |
| 1555 | void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) { |
| 1556 | addrmod2(cond | B26 | B | L, dst, src); |
| 1557 | } |
| 1558 | |
| 1559 | |
| 1560 | void Assembler::strb(Register src, const MemOperand& dst, Condition cond) { |
| 1561 | addrmod2(cond | B26 | B, src, dst); |
| 1562 | } |
| 1563 | |
| 1564 | |
| 1565 | void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) { |
| 1566 | addrmod3(cond | L | B7 | H | B4, dst, src); |
| 1567 | } |
| 1568 | |
| 1569 | |
| 1570 | void Assembler::strh(Register src, const MemOperand& dst, Condition cond) { |
| 1571 | addrmod3(cond | B7 | H | B4, src, dst); |
| 1572 | } |
| 1573 | |
| 1574 | |
| 1575 | void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) { |
| 1576 | addrmod3(cond | L | B7 | S6 | B4, dst, src); |
| 1577 | } |
| 1578 | |
| 1579 | |
| 1580 | void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) { |
| 1581 | addrmod3(cond | L | B7 | S6 | H | B4, dst, src); |
| 1582 | } |
| 1583 | |
| 1584 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1585 | void Assembler::ldrd(Register dst1, Register dst2, |
| 1586 | const MemOperand& src, Condition cond) { |
| 1587 | ASSERT(CpuFeatures::IsEnabled(ARMv7)); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1588 | ASSERT(src.rm().is(no_reg)); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1589 | ASSERT(!dst1.is(lr)); // r14. |
| 1590 | ASSERT_EQ(0, dst1.code() % 2); |
| 1591 | ASSERT_EQ(dst1.code() + 1, dst2.code()); |
| 1592 | addrmod3(cond | B7 | B6 | B4, dst1, src); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1596 | void Assembler::strd(Register src1, Register src2, |
| 1597 | const MemOperand& dst, Condition cond) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1598 | ASSERT(dst.rm().is(no_reg)); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1599 | ASSERT(!src1.is(lr)); // r14. |
| 1600 | ASSERT_EQ(0, src1.code() % 2); |
| 1601 | ASSERT_EQ(src1.code() + 1, src2.code()); |
| 1602 | ASSERT(CpuFeatures::IsEnabled(ARMv7)); |
| 1603 | addrmod3(cond | B7 | B6 | B5 | B4, src1, dst); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1604 | } |
| 1605 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1606 | // Load/Store multiple instructions. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1607 | void Assembler::ldm(BlockAddrMode am, |
| 1608 | Register base, |
| 1609 | RegList dst, |
| 1610 | Condition cond) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1611 | // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1612 | ASSERT(base.is(sp) || (dst & sp.bit()) == 0); |
| 1613 | |
| 1614 | addrmod4(cond | B27 | am | L, base, dst); |
| 1615 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1616 | // Emit the constant pool after a function return implemented by ldm ..{..pc}. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1617 | if (cond == al && (dst & pc.bit()) != 0) { |
| 1618 | // There is a slight chance that the ldm instruction was actually a call, |
| 1619 | // in which case it would be wrong to return into the constant pool; we |
| 1620 | // recognize this case by checking if the emission of the pool was blocked |
| 1621 | // at the pc of the ldm instruction by a mov lr, pc instruction; if this is |
| 1622 | // the case, we emit a jump over the pool. |
| 1623 | CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize); |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | |
| 1628 | void Assembler::stm(BlockAddrMode am, |
| 1629 | Register base, |
| 1630 | RegList src, |
| 1631 | Condition cond) { |
| 1632 | addrmod4(cond | B27 | am, base, src); |
| 1633 | } |
| 1634 | |
| 1635 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1636 | // Exception-generating instructions and debugging support. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1637 | void Assembler::stop(const char* msg) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1638 | #ifndef __arm__ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1639 | // The simulator handles these special instructions and stops execution. |
| 1640 | emit(15 << 28 | ((intptr_t) msg)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1641 | #else // def __arm__ |
| 1642 | #ifdef CAN_USE_ARMV5_INSTRUCTIONS |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1643 | bkpt(0); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1644 | #else // ndef CAN_USE_ARMV5_INSTRUCTIONS |
| 1645 | swi(0x9f0001); |
| 1646 | #endif // ndef CAN_USE_ARMV5_INSTRUCTIONS |
| 1647 | #endif // def __arm__ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | |
| 1651 | void Assembler::bkpt(uint32_t imm16) { // v5 and above |
| 1652 | ASSERT(is_uint16(imm16)); |
| 1653 | emit(al | B24 | B21 | (imm16 >> 4)*B8 | 7*B4 | (imm16 & 0xf)); |
| 1654 | } |
| 1655 | |
| 1656 | |
| 1657 | void Assembler::swi(uint32_t imm24, Condition cond) { |
| 1658 | ASSERT(is_uint24(imm24)); |
| 1659 | emit(cond | 15*B24 | imm24); |
| 1660 | } |
| 1661 | |
| 1662 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1663 | // Coprocessor instructions. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1664 | void Assembler::cdp(Coprocessor coproc, |
| 1665 | int opcode_1, |
| 1666 | CRegister crd, |
| 1667 | CRegister crn, |
| 1668 | CRegister crm, |
| 1669 | int opcode_2, |
| 1670 | Condition cond) { |
| 1671 | ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2)); |
| 1672 | emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 | |
| 1673 | crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code()); |
| 1674 | } |
| 1675 | |
| 1676 | |
| 1677 | void Assembler::cdp2(Coprocessor coproc, |
| 1678 | int opcode_1, |
| 1679 | CRegister crd, |
| 1680 | CRegister crn, |
| 1681 | CRegister crm, |
| 1682 | int opcode_2) { // v5 and above |
| 1683 | cdp(coproc, opcode_1, crd, crn, crm, opcode_2, static_cast<Condition>(nv)); |
| 1684 | } |
| 1685 | |
| 1686 | |
| 1687 | void Assembler::mcr(Coprocessor coproc, |
| 1688 | int opcode_1, |
| 1689 | Register rd, |
| 1690 | CRegister crn, |
| 1691 | CRegister crm, |
| 1692 | int opcode_2, |
| 1693 | Condition cond) { |
| 1694 | ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2)); |
| 1695 | emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 | |
| 1696 | rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code()); |
| 1697 | } |
| 1698 | |
| 1699 | |
| 1700 | void Assembler::mcr2(Coprocessor coproc, |
| 1701 | int opcode_1, |
| 1702 | Register rd, |
| 1703 | CRegister crn, |
| 1704 | CRegister crm, |
| 1705 | int opcode_2) { // v5 and above |
| 1706 | mcr(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv)); |
| 1707 | } |
| 1708 | |
| 1709 | |
| 1710 | void Assembler::mrc(Coprocessor coproc, |
| 1711 | int opcode_1, |
| 1712 | Register rd, |
| 1713 | CRegister crn, |
| 1714 | CRegister crm, |
| 1715 | int opcode_2, |
| 1716 | Condition cond) { |
| 1717 | ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2)); |
| 1718 | emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 | |
| 1719 | rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code()); |
| 1720 | } |
| 1721 | |
| 1722 | |
| 1723 | void Assembler::mrc2(Coprocessor coproc, |
| 1724 | int opcode_1, |
| 1725 | Register rd, |
| 1726 | CRegister crn, |
| 1727 | CRegister crm, |
| 1728 | int opcode_2) { // v5 and above |
| 1729 | mrc(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv)); |
| 1730 | } |
| 1731 | |
| 1732 | |
| 1733 | void Assembler::ldc(Coprocessor coproc, |
| 1734 | CRegister crd, |
| 1735 | const MemOperand& src, |
| 1736 | LFlag l, |
| 1737 | Condition cond) { |
| 1738 | addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src); |
| 1739 | } |
| 1740 | |
| 1741 | |
| 1742 | void Assembler::ldc(Coprocessor coproc, |
| 1743 | CRegister crd, |
| 1744 | Register rn, |
| 1745 | int option, |
| 1746 | LFlag l, |
| 1747 | Condition cond) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1748 | // Unindexed addressing. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1749 | ASSERT(is_uint8(option)); |
| 1750 | emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 | |
| 1751 | coproc*B8 | (option & 255)); |
| 1752 | } |
| 1753 | |
| 1754 | |
| 1755 | void Assembler::ldc2(Coprocessor coproc, |
| 1756 | CRegister crd, |
| 1757 | const MemOperand& src, |
| 1758 | LFlag l) { // v5 and above |
| 1759 | ldc(coproc, crd, src, l, static_cast<Condition>(nv)); |
| 1760 | } |
| 1761 | |
| 1762 | |
| 1763 | void Assembler::ldc2(Coprocessor coproc, |
| 1764 | CRegister crd, |
| 1765 | Register rn, |
| 1766 | int option, |
| 1767 | LFlag l) { // v5 and above |
| 1768 | ldc(coproc, crd, rn, option, l, static_cast<Condition>(nv)); |
| 1769 | } |
| 1770 | |
| 1771 | |
| 1772 | void Assembler::stc(Coprocessor coproc, |
| 1773 | CRegister crd, |
| 1774 | const MemOperand& dst, |
| 1775 | LFlag l, |
| 1776 | Condition cond) { |
| 1777 | addrmod5(cond | B27 | B26 | l | coproc*B8, crd, dst); |
| 1778 | } |
| 1779 | |
| 1780 | |
| 1781 | void Assembler::stc(Coprocessor coproc, |
| 1782 | CRegister crd, |
| 1783 | Register rn, |
| 1784 | int option, |
| 1785 | LFlag l, |
| 1786 | Condition cond) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1787 | // Unindexed addressing. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1788 | ASSERT(is_uint8(option)); |
| 1789 | emit(cond | B27 | B26 | U | l | rn.code()*B16 | crd.code()*B12 | |
| 1790 | coproc*B8 | (option & 255)); |
| 1791 | } |
| 1792 | |
| 1793 | |
| 1794 | void Assembler::stc2(Coprocessor |
| 1795 | coproc, CRegister crd, |
| 1796 | const MemOperand& dst, |
| 1797 | LFlag l) { // v5 and above |
| 1798 | stc(coproc, crd, dst, l, static_cast<Condition>(nv)); |
| 1799 | } |
| 1800 | |
| 1801 | |
| 1802 | void Assembler::stc2(Coprocessor coproc, |
| 1803 | CRegister crd, |
| 1804 | Register rn, |
| 1805 | int option, |
| 1806 | LFlag l) { // v5 and above |
| 1807 | stc(coproc, crd, rn, option, l, static_cast<Condition>(nv)); |
| 1808 | } |
| 1809 | |
| 1810 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1811 | // Support for VFP. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1812 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1813 | void Assembler::vldr(const DwVfpRegister dst, |
| 1814 | const Register base, |
| 1815 | int offset, |
| 1816 | const Condition cond) { |
| 1817 | // Ddst = MEM(Rbase + offset). |
| 1818 | // Instruction details available in ARM DDI 0406A, A8-628. |
| 1819 | // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) | |
| 1820 | // Vdst(15-12) | 1011(11-8) | offset |
| 1821 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1822 | ASSERT(offset % 4 == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1823 | ASSERT((offset / 4) < 256); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1824 | ASSERT(offset >= 0); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1825 | emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 | |
| 1826 | 0xB*B8 | ((offset / 4) & 255)); |
| 1827 | } |
| 1828 | |
| 1829 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1830 | void Assembler::vldr(const SwVfpRegister dst, |
| 1831 | const Register base, |
| 1832 | int offset, |
| 1833 | const Condition cond) { |
| 1834 | // Sdst = MEM(Rbase + offset). |
| 1835 | // Instruction details available in ARM DDI 0406A, A8-628. |
| 1836 | // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) | |
| 1837 | // Vdst(15-12) | 1010(11-8) | offset |
| 1838 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1839 | ASSERT(offset % 4 == 0); |
| 1840 | ASSERT((offset / 4) < 256); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1841 | ASSERT(offset >= 0); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1842 | int sd, d; |
| 1843 | dst.split_code(&sd, &d); |
| 1844 | emit(cond | d*B22 | 0xD9*B20 | base.code()*B16 | sd*B12 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1845 | 0xA*B8 | ((offset / 4) & 255)); |
| 1846 | } |
| 1847 | |
| 1848 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1849 | void Assembler::vstr(const DwVfpRegister src, |
| 1850 | const Register base, |
| 1851 | int offset, |
| 1852 | const Condition cond) { |
| 1853 | // MEM(Rbase + offset) = Dsrc. |
| 1854 | // Instruction details available in ARM DDI 0406A, A8-786. |
| 1855 | // cond(31-28) | 1101(27-24)| 1000(23-20) | | Rbase(19-16) | |
| 1856 | // Vsrc(15-12) | 1011(11-8) | (offset/4) |
| 1857 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1858 | ASSERT(offset % 4 == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1859 | ASSERT((offset / 4) < 256); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1860 | ASSERT(offset >= 0); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1861 | emit(cond | 0xD8*B20 | base.code()*B16 | src.code()*B12 | |
| 1862 | 0xB*B8 | ((offset / 4) & 255)); |
| 1863 | } |
| 1864 | |
| 1865 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1866 | void Assembler::vstr(const SwVfpRegister src, |
| 1867 | const Register base, |
| 1868 | int offset, |
| 1869 | const Condition cond) { |
| 1870 | // MEM(Rbase + offset) = SSrc. |
| 1871 | // Instruction details available in ARM DDI 0406A, A8-786. |
| 1872 | // cond(31-28) | 1101(27-24)| 1000(23-20) | Rbase(19-16) | |
| 1873 | // Vdst(15-12) | 1010(11-8) | (offset/4) |
| 1874 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1875 | ASSERT(offset % 4 == 0); |
| 1876 | ASSERT((offset / 4) < 256); |
| 1877 | ASSERT(offset >= 0); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1878 | int sd, d; |
| 1879 | src.split_code(&sd, &d); |
| 1880 | emit(cond | d*B22 | 0xD8*B20 | base.code()*B16 | sd*B12 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1881 | 0xA*B8 | ((offset / 4) & 255)); |
| 1882 | } |
| 1883 | |
| 1884 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1885 | static void DoubleAsTwoUInt32(double d, uint32_t* lo, uint32_t* hi) { |
| 1886 | uint64_t i; |
| 1887 | memcpy(&i, &d, 8); |
| 1888 | |
| 1889 | *lo = i & 0xffffffff; |
| 1890 | *hi = i >> 32; |
| 1891 | } |
| 1892 | |
| 1893 | // Only works for little endian floating point formats. |
| 1894 | // We don't support VFP on the mixed endian floating point platform. |
| 1895 | static bool FitsVMOVDoubleImmediate(double d, uint32_t *encoding) { |
| 1896 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1897 | |
| 1898 | // VMOV can accept an immediate of the form: |
| 1899 | // |
| 1900 | // +/- m * 2^(-n) where 16 <= m <= 31 and 0 <= n <= 7 |
| 1901 | // |
| 1902 | // The immediate is encoded using an 8-bit quantity, comprised of two |
| 1903 | // 4-bit fields. For an 8-bit immediate of the form: |
| 1904 | // |
| 1905 | // [abcdefgh] |
| 1906 | // |
| 1907 | // where a is the MSB and h is the LSB, an immediate 64-bit double can be |
| 1908 | // created of the form: |
| 1909 | // |
| 1910 | // [aBbbbbbb,bbcdefgh,00000000,00000000, |
| 1911 | // 00000000,00000000,00000000,00000000] |
| 1912 | // |
| 1913 | // where B = ~b. |
| 1914 | // |
| 1915 | |
| 1916 | uint32_t lo, hi; |
| 1917 | DoubleAsTwoUInt32(d, &lo, &hi); |
| 1918 | |
| 1919 | // The most obvious constraint is the long block of zeroes. |
| 1920 | if ((lo != 0) || ((hi & 0xffff) != 0)) { |
| 1921 | return false; |
| 1922 | } |
| 1923 | |
| 1924 | // Bits 62:55 must be all clear or all set. |
| 1925 | if (((hi & 0x3fc00000) != 0) && ((hi & 0x3fc00000) != 0x3fc00000)) { |
| 1926 | return false; |
| 1927 | } |
| 1928 | |
| 1929 | // Bit 63 must be NOT bit 62. |
| 1930 | if (((hi ^ (hi << 1)) & (0x40000000)) == 0) { |
| 1931 | return false; |
| 1932 | } |
| 1933 | |
| 1934 | // Create the encoded immediate in the form: |
| 1935 | // [00000000,0000abcd,00000000,0000efgh] |
| 1936 | *encoding = (hi >> 16) & 0xf; // Low nybble. |
| 1937 | *encoding |= (hi >> 4) & 0x70000; // Low three bits of the high nybble. |
| 1938 | *encoding |= (hi >> 12) & 0x80000; // Top bit of the high nybble. |
| 1939 | |
| 1940 | return true; |
| 1941 | } |
| 1942 | |
| 1943 | |
| 1944 | void Assembler::vmov(const DwVfpRegister dst, |
| 1945 | double imm, |
| 1946 | const Condition cond) { |
| 1947 | // Dd = immediate |
| 1948 | // Instruction details available in ARM DDI 0406B, A8-640. |
| 1949 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1950 | |
| 1951 | uint32_t enc; |
| 1952 | if (FitsVMOVDoubleImmediate(imm, &enc)) { |
| 1953 | // The double can be encoded in the instruction. |
| 1954 | emit(cond | 0xE*B24 | 0xB*B20 | dst.code()*B12 | 0xB*B8 | enc); |
| 1955 | } else { |
| 1956 | // Synthesise the double from ARM immediates. This could be implemented |
| 1957 | // using vldr from a constant pool. |
| 1958 | uint32_t lo, hi; |
| 1959 | DoubleAsTwoUInt32(imm, &lo, &hi); |
| 1960 | |
| 1961 | if (lo == hi) { |
| 1962 | // If the lo and hi parts of the double are equal, the literal is easier |
| 1963 | // to create. This is the case with 0.0. |
| 1964 | mov(ip, Operand(lo)); |
| 1965 | vmov(dst, ip, ip); |
| 1966 | } else { |
| 1967 | // Move the low part of the double into the lower of the corresponsing S |
| 1968 | // registers of D register dst. |
| 1969 | mov(ip, Operand(lo)); |
| 1970 | vmov(dst.low(), ip, cond); |
| 1971 | |
| 1972 | // Move the high part of the double into the higher of the corresponsing S |
| 1973 | // registers of D register dst. |
| 1974 | mov(ip, Operand(hi)); |
| 1975 | vmov(dst.high(), ip, cond); |
| 1976 | } |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | |
| 1981 | void Assembler::vmov(const SwVfpRegister dst, |
| 1982 | const SwVfpRegister src, |
| 1983 | const Condition cond) { |
| 1984 | // Sd = Sm |
| 1985 | // Instruction details available in ARM DDI 0406B, A8-642. |
| 1986 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1987 | int sd, d, sm, m; |
| 1988 | dst.split_code(&sd, &d); |
| 1989 | src.split_code(&sm, &m); |
| 1990 | emit(cond | 0xE*B24 | d*B22 | 0xB*B20 | sd*B12 | 0xA*B8 | B6 | m*B5 | sm); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1994 | void Assembler::vmov(const DwVfpRegister dst, |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1995 | const DwVfpRegister src, |
| 1996 | const Condition cond) { |
| 1997 | // Dd = Dm |
| 1998 | // Instruction details available in ARM DDI 0406B, A8-642. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1999 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2000 | emit(cond | 0xE*B24 | 0xB*B20 | |
| 2001 | dst.code()*B12 | 0x5*B9 | B8 | B6 | src.code()); |
| 2002 | } |
| 2003 | |
| 2004 | |
| 2005 | void Assembler::vmov(const DwVfpRegister dst, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2006 | const Register src1, |
| 2007 | const Register src2, |
| 2008 | const Condition cond) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2009 | // Dm = <Rt,Rt2>. |
| 2010 | // Instruction details available in ARM DDI 0406A, A8-646. |
| 2011 | // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) | |
| 2012 | // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm |
| 2013 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2014 | ASSERT(!src1.is(pc) && !src2.is(pc)); |
| 2015 | emit(cond | 0xC*B24 | B22 | src2.code()*B16 | |
| 2016 | src1.code()*B12 | 0xB*B8 | B4 | dst.code()); |
| 2017 | } |
| 2018 | |
| 2019 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2020 | void Assembler::vmov(const Register dst1, |
| 2021 | const Register dst2, |
| 2022 | const DwVfpRegister src, |
| 2023 | const Condition cond) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2024 | // <Rt,Rt2> = Dm. |
| 2025 | // Instruction details available in ARM DDI 0406A, A8-646. |
| 2026 | // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) | |
| 2027 | // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm |
| 2028 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2029 | ASSERT(!dst1.is(pc) && !dst2.is(pc)); |
| 2030 | emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 | |
| 2031 | dst1.code()*B12 | 0xB*B8 | B4 | src.code()); |
| 2032 | } |
| 2033 | |
| 2034 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2035 | void Assembler::vmov(const SwVfpRegister dst, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2036 | const Register src, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2037 | const Condition cond) { |
| 2038 | // Sn = Rt. |
| 2039 | // Instruction details available in ARM DDI 0406A, A8-642. |
| 2040 | // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) | |
| 2041 | // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0) |
| 2042 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2043 | ASSERT(!src.is(pc)); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2044 | int sn, n; |
| 2045 | dst.split_code(&sn, &n); |
| 2046 | emit(cond | 0xE*B24 | sn*B16 | src.code()*B12 | 0xA*B8 | n*B7 | B4); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2050 | void Assembler::vmov(const Register dst, |
| 2051 | const SwVfpRegister src, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2052 | const Condition cond) { |
| 2053 | // Rt = Sn. |
| 2054 | // Instruction details available in ARM DDI 0406A, A8-642. |
| 2055 | // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) | |
| 2056 | // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0) |
| 2057 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2058 | ASSERT(!dst.is(pc)); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2059 | int sn, n; |
| 2060 | src.split_code(&sn, &n); |
| 2061 | emit(cond | 0xE*B24 | B20 | sn*B16 | dst.code()*B12 | 0xA*B8 | n*B7 | B4); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2065 | // Type of data to read from or write to VFP register. |
| 2066 | // Used as specifier in generic vcvt instruction. |
| 2067 | enum VFPType { S32, U32, F32, F64 }; |
| 2068 | |
| 2069 | |
| 2070 | static bool IsSignedVFPType(VFPType type) { |
| 2071 | switch (type) { |
| 2072 | case S32: |
| 2073 | return true; |
| 2074 | case U32: |
| 2075 | return false; |
| 2076 | default: |
| 2077 | UNREACHABLE(); |
| 2078 | return false; |
| 2079 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2083 | static bool IsIntegerVFPType(VFPType type) { |
| 2084 | switch (type) { |
| 2085 | case S32: |
| 2086 | case U32: |
| 2087 | return true; |
| 2088 | case F32: |
| 2089 | case F64: |
| 2090 | return false; |
| 2091 | default: |
| 2092 | UNREACHABLE(); |
| 2093 | return false; |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | |
| 2098 | static bool IsDoubleVFPType(VFPType type) { |
| 2099 | switch (type) { |
| 2100 | case F32: |
| 2101 | return false; |
| 2102 | case F64: |
| 2103 | return true; |
| 2104 | default: |
| 2105 | UNREACHABLE(); |
| 2106 | return false; |
| 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2111 | // Split five bit reg_code based on size of reg_type. |
| 2112 | // 32-bit register codes are Vm:M |
| 2113 | // 64-bit register codes are M:Vm |
| 2114 | // where Vm is four bits, and M is a single bit. |
| 2115 | static void SplitRegCode(VFPType reg_type, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2116 | int reg_code, |
| 2117 | int* vm, |
| 2118 | int* m) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2119 | ASSERT((reg_code >= 0) && (reg_code <= 31)); |
| 2120 | if (IsIntegerVFPType(reg_type) || !IsDoubleVFPType(reg_type)) { |
| 2121 | // 32 bit type. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2122 | *m = reg_code & 0x1; |
| 2123 | *vm = reg_code >> 1; |
| 2124 | } else { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2125 | // 64 bit type. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2126 | *m = (reg_code & 0x10) >> 4; |
| 2127 | *vm = reg_code & 0x0F; |
| 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | |
| 2132 | // Encode vcvt.src_type.dst_type instruction. |
| 2133 | static Instr EncodeVCVT(const VFPType dst_type, |
| 2134 | const int dst_code, |
| 2135 | const VFPType src_type, |
| 2136 | const int src_code, |
| 2137 | const Condition cond) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2138 | ASSERT(src_type != dst_type); |
| 2139 | int D, Vd, M, Vm; |
| 2140 | SplitRegCode(src_type, src_code, &Vm, &M); |
| 2141 | SplitRegCode(dst_type, dst_code, &Vd, &D); |
| 2142 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2143 | if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) { |
| 2144 | // Conversion between IEEE floating point and 32-bit integer. |
| 2145 | // Instruction details available in ARM DDI 0406B, A8.6.295. |
| 2146 | // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) | |
| 2147 | // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0) |
| 2148 | ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type)); |
| 2149 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2150 | int sz, opc2, op; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2151 | |
| 2152 | if (IsIntegerVFPType(dst_type)) { |
| 2153 | opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4; |
| 2154 | sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0; |
| 2155 | op = 1; // round towards zero |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2156 | } else { |
| 2157 | ASSERT(IsIntegerVFPType(src_type)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2158 | opc2 = 0x0; |
| 2159 | sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0; |
| 2160 | op = IsSignedVFPType(src_type) ? 0x1 : 0x0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 | |
| 2164 | Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm); |
| 2165 | } else { |
| 2166 | // Conversion between IEEE double and single precision. |
| 2167 | // Instruction details available in ARM DDI 0406B, A8.6.298. |
| 2168 | // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) | |
| 2169 | // Vd(15-12) | 101(11-9) | sz(8) | 1(7) | 1(6) | M(5) | 0(4) | Vm(3-0) |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2170 | int sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2171 | return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 | |
| 2172 | Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm); |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | |
| 2177 | void Assembler::vcvt_f64_s32(const DwVfpRegister dst, |
| 2178 | const SwVfpRegister src, |
| 2179 | const Condition cond) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2180 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2181 | emit(EncodeVCVT(F64, dst.code(), S32, src.code(), cond)); |
| 2182 | } |
| 2183 | |
| 2184 | |
| 2185 | void Assembler::vcvt_f32_s32(const SwVfpRegister dst, |
| 2186 | const SwVfpRegister src, |
| 2187 | const Condition cond) { |
| 2188 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2189 | emit(EncodeVCVT(F32, dst.code(), S32, src.code(), cond)); |
| 2190 | } |
| 2191 | |
| 2192 | |
| 2193 | void Assembler::vcvt_f64_u32(const DwVfpRegister dst, |
| 2194 | const SwVfpRegister src, |
| 2195 | const Condition cond) { |
| 2196 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2197 | emit(EncodeVCVT(F64, dst.code(), U32, src.code(), cond)); |
| 2198 | } |
| 2199 | |
| 2200 | |
| 2201 | void Assembler::vcvt_s32_f64(const SwVfpRegister dst, |
| 2202 | const DwVfpRegister src, |
| 2203 | const Condition cond) { |
| 2204 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2205 | emit(EncodeVCVT(S32, dst.code(), F64, src.code(), cond)); |
| 2206 | } |
| 2207 | |
| 2208 | |
| 2209 | void Assembler::vcvt_u32_f64(const SwVfpRegister dst, |
| 2210 | const DwVfpRegister src, |
| 2211 | const Condition cond) { |
| 2212 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2213 | emit(EncodeVCVT(U32, dst.code(), F64, src.code(), cond)); |
| 2214 | } |
| 2215 | |
| 2216 | |
| 2217 | void Assembler::vcvt_f64_f32(const DwVfpRegister dst, |
| 2218 | const SwVfpRegister src, |
| 2219 | const Condition cond) { |
| 2220 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2221 | emit(EncodeVCVT(F64, dst.code(), F32, src.code(), cond)); |
| 2222 | } |
| 2223 | |
| 2224 | |
| 2225 | void Assembler::vcvt_f32_f64(const SwVfpRegister dst, |
| 2226 | const DwVfpRegister src, |
| 2227 | const Condition cond) { |
| 2228 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2229 | emit(EncodeVCVT(F32, dst.code(), F64, src.code(), cond)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2233 | void Assembler::vadd(const DwVfpRegister dst, |
| 2234 | const DwVfpRegister src1, |
| 2235 | const DwVfpRegister src2, |
| 2236 | const Condition cond) { |
| 2237 | // Dd = vadd(Dn, Dm) double precision floating point addition. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2238 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 2239 | // Instruction details available in ARM DDI 0406A, A8-536. |
| 2240 | // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) | |
| 2241 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0) |
| 2242 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2243 | emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 | |
| 2244 | dst.code()*B12 | 0x5*B9 | B8 | src2.code()); |
| 2245 | } |
| 2246 | |
| 2247 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2248 | void Assembler::vsub(const DwVfpRegister dst, |
| 2249 | const DwVfpRegister src1, |
| 2250 | const DwVfpRegister src2, |
| 2251 | const Condition cond) { |
| 2252 | // Dd = vsub(Dn, Dm) double precision floating point subtraction. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2253 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 2254 | // Instruction details available in ARM DDI 0406A, A8-784. |
| 2255 | // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) | |
| 2256 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 1(6) | M=?(5) | 0(4) | Vm(3-0) |
| 2257 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2258 | emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 | |
| 2259 | dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code()); |
| 2260 | } |
| 2261 | |
| 2262 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2263 | void Assembler::vmul(const DwVfpRegister dst, |
| 2264 | const DwVfpRegister src1, |
| 2265 | const DwVfpRegister src2, |
| 2266 | const Condition cond) { |
| 2267 | // Dd = vmul(Dn, Dm) double precision floating point multiplication. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2268 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 2269 | // Instruction details available in ARM DDI 0406A, A8-784. |
| 2270 | // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) | |
| 2271 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0) |
| 2272 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2273 | emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 | |
| 2274 | dst.code()*B12 | 0x5*B9 | B8 | src2.code()); |
| 2275 | } |
| 2276 | |
| 2277 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2278 | void Assembler::vdiv(const DwVfpRegister dst, |
| 2279 | const DwVfpRegister src1, |
| 2280 | const DwVfpRegister src2, |
| 2281 | const Condition cond) { |
| 2282 | // Dd = vdiv(Dn, Dm) double precision floating point division. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2283 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 2284 | // Instruction details available in ARM DDI 0406A, A8-584. |
| 2285 | // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) | |
| 2286 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=? | 0(6) | M=?(5) | 0(4) | Vm(3-0) |
| 2287 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2288 | emit(cond | 0xE*B24 | B23 | src1.code()*B16 | |
| 2289 | dst.code()*B12 | 0x5*B9 | B8 | src2.code()); |
| 2290 | } |
| 2291 | |
| 2292 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2293 | void Assembler::vcmp(const DwVfpRegister src1, |
| 2294 | const DwVfpRegister src2, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2295 | const SBit s, |
| 2296 | const Condition cond) { |
| 2297 | // vcmp(Dd, Dm) double precision floating point comparison. |
| 2298 | // Instruction details available in ARM DDI 0406A, A8-570. |
| 2299 | // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) | |
| 2300 | // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=? | 1(6) | M(5)=? | 0(4) | Vm(3-0) |
| 2301 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2302 | emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 | |
| 2303 | src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code()); |
| 2304 | } |
| 2305 | |
| 2306 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2307 | void Assembler::vcmp(const DwVfpRegister src1, |
| 2308 | const double src2, |
| 2309 | const SBit s, |
| 2310 | const Condition cond) { |
| 2311 | // vcmp(Dd, Dm) double precision floating point comparison. |
| 2312 | // Instruction details available in ARM DDI 0406A, A8-570. |
| 2313 | // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0101 (19-16) | |
| 2314 | // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=? | 1(6) | M(5)=? | 0(4) | 0000(3-0) |
| 2315 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2316 | ASSERT(src2 == 0.0); |
| 2317 | emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 | B16 | |
| 2318 | src1.code()*B12 | 0x5*B9 | B8 | B6); |
| 2319 | } |
| 2320 | |
| 2321 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2322 | void Assembler::vmrs(Register dst, Condition cond) { |
| 2323 | // Instruction details available in ARM DDI 0406A, A8-652. |
| 2324 | // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) | |
| 2325 | // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0) |
| 2326 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2327 | emit(cond | 0xE*B24 | 0xF*B20 | B16 | |
| 2328 | dst.code()*B12 | 0xA*B8 | B4); |
| 2329 | } |
| 2330 | |
| 2331 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2332 | |
| 2333 | void Assembler::vsqrt(const DwVfpRegister dst, |
| 2334 | const DwVfpRegister src, |
| 2335 | const Condition cond) { |
| 2336 | // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0001 (19-16) | |
| 2337 | // Vd(15-12) | 101(11-9) | sz(8)=1 | 11 (7-6) | M(5)=? | 0(4) | Vm(3-0) |
| 2338 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2339 | emit(cond | 0xE*B24 | B23 | 0x3*B20 | B16 | |
| 2340 | dst.code()*B12 | 0x5*B9 | B8 | 3*B6 | src.code()); |
| 2341 | } |
| 2342 | |
| 2343 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2344 | // Pseudo instructions. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2345 | void Assembler::nop(int type) { |
| 2346 | // This is mov rx, rx. |
| 2347 | ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop. |
| 2348 | emit(al | 13*B21 | type*B12 | type); |
| 2349 | } |
| 2350 | |
| 2351 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2352 | bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) { |
| 2353 | uint32_t dummy1; |
| 2354 | uint32_t dummy2; |
| 2355 | return fits_shifter(imm32, &dummy1, &dummy2, NULL); |
| 2356 | } |
| 2357 | |
| 2358 | |
| 2359 | void Assembler::BlockConstPoolFor(int instructions) { |
| 2360 | BlockConstPoolBefore(pc_offset() + instructions * kInstrSize); |
| 2361 | } |
| 2362 | |
| 2363 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2364 | // Debugging. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2365 | void Assembler::RecordJSReturn() { |
| 2366 | WriteRecordedPositions(); |
| 2367 | CheckBuffer(); |
| 2368 | RecordRelocInfo(RelocInfo::JS_RETURN); |
| 2369 | } |
| 2370 | |
| 2371 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2372 | void Assembler::RecordDebugBreakSlot() { |
| 2373 | WriteRecordedPositions(); |
| 2374 | CheckBuffer(); |
| 2375 | RecordRelocInfo(RelocInfo::DEBUG_BREAK_SLOT); |
| 2376 | } |
| 2377 | |
| 2378 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2379 | void Assembler::RecordComment(const char* msg) { |
| 2380 | if (FLAG_debug_code) { |
| 2381 | CheckBuffer(); |
| 2382 | RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg)); |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | |
| 2387 | void Assembler::RecordPosition(int pos) { |
| 2388 | if (pos == RelocInfo::kNoPosition) return; |
| 2389 | ASSERT(pos >= 0); |
| 2390 | current_position_ = pos; |
| 2391 | } |
| 2392 | |
| 2393 | |
| 2394 | void Assembler::RecordStatementPosition(int pos) { |
| 2395 | if (pos == RelocInfo::kNoPosition) return; |
| 2396 | ASSERT(pos >= 0); |
| 2397 | current_statement_position_ = pos; |
| 2398 | } |
| 2399 | |
| 2400 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2401 | bool Assembler::WriteRecordedPositions() { |
| 2402 | bool written = false; |
| 2403 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2404 | // Write the statement position if it is different from what was written last |
| 2405 | // time. |
| 2406 | if (current_statement_position_ != written_statement_position_) { |
| 2407 | CheckBuffer(); |
| 2408 | RecordRelocInfo(RelocInfo::STATEMENT_POSITION, current_statement_position_); |
| 2409 | written_statement_position_ = current_statement_position_; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2410 | written = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | // Write the position if it is different from what was written last time and |
| 2414 | // also different from the written statement position. |
| 2415 | if (current_position_ != written_position_ && |
| 2416 | current_position_ != written_statement_position_) { |
| 2417 | CheckBuffer(); |
| 2418 | RecordRelocInfo(RelocInfo::POSITION, current_position_); |
| 2419 | written_position_ = current_position_; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2420 | written = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2421 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2422 | |
| 2423 | // Return whether something was written. |
| 2424 | return written; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | |
| 2428 | void Assembler::GrowBuffer() { |
| 2429 | if (!own_buffer_) FATAL("external code buffer is too small"); |
| 2430 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2431 | // Compute new buffer size. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2432 | CodeDesc desc; // the new buffer |
| 2433 | if (buffer_size_ < 4*KB) { |
| 2434 | desc.buffer_size = 4*KB; |
| 2435 | } else if (buffer_size_ < 1*MB) { |
| 2436 | desc.buffer_size = 2*buffer_size_; |
| 2437 | } else { |
| 2438 | desc.buffer_size = buffer_size_ + 1*MB; |
| 2439 | } |
| 2440 | CHECK_GT(desc.buffer_size, 0); // no overflow |
| 2441 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2442 | // Setup new buffer. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2443 | desc.buffer = NewArray<byte>(desc.buffer_size); |
| 2444 | |
| 2445 | desc.instr_size = pc_offset(); |
| 2446 | desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos(); |
| 2447 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2448 | // Copy the data. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2449 | int pc_delta = desc.buffer - buffer_; |
| 2450 | int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_); |
| 2451 | memmove(desc.buffer, buffer_, desc.instr_size); |
| 2452 | memmove(reloc_info_writer.pos() + rc_delta, |
| 2453 | reloc_info_writer.pos(), desc.reloc_size); |
| 2454 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2455 | // Switch buffers. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2456 | DeleteArray(buffer_); |
| 2457 | buffer_ = desc.buffer; |
| 2458 | buffer_size_ = desc.buffer_size; |
| 2459 | pc_ += pc_delta; |
| 2460 | reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta, |
| 2461 | reloc_info_writer.last_pc() + pc_delta); |
| 2462 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2463 | // None of our relocation types are pc relative pointing outside the code |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2464 | // buffer nor pc absolute pointing inside the code buffer, so there is no need |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2465 | // to relocate any emitted relocation entries. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2466 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2467 | // Relocate pending relocation entries. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2468 | for (int i = 0; i < num_prinfo_; i++) { |
| 2469 | RelocInfo& rinfo = prinfo_[i]; |
| 2470 | ASSERT(rinfo.rmode() != RelocInfo::COMMENT && |
| 2471 | rinfo.rmode() != RelocInfo::POSITION); |
| 2472 | if (rinfo.rmode() != RelocInfo::JS_RETURN) { |
| 2473 | rinfo.set_pc(rinfo.pc() + pc_delta); |
| 2474 | } |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | |
| 2479 | void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) { |
| 2480 | RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2481 | if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::DEBUG_BREAK_SLOT) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2482 | // Adjust code for new modes. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2483 | ASSERT(RelocInfo::IsDebugBreakSlot(rmode) |
| 2484 | || RelocInfo::IsJSReturn(rmode) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2485 | || RelocInfo::IsComment(rmode) |
| 2486 | || RelocInfo::IsPosition(rmode)); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2487 | // These modes do not need an entry in the constant pool. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2488 | } else { |
| 2489 | ASSERT(num_prinfo_ < kMaxNumPRInfo); |
| 2490 | prinfo_[num_prinfo_++] = rinfo; |
| 2491 | // Make sure the constant pool is not emitted in place of the next |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2492 | // instruction for which we just recorded relocation info. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2493 | BlockConstPoolBefore(pc_offset() + kInstrSize); |
| 2494 | } |
| 2495 | if (rinfo.rmode() != RelocInfo::NONE) { |
| 2496 | // Don't record external references unless the heap will be serialized. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2497 | if (rmode == RelocInfo::EXTERNAL_REFERENCE) { |
| 2498 | #ifdef DEBUG |
| 2499 | if (!Serializer::enabled()) { |
| 2500 | Serializer::TooLateToEnableNow(); |
| 2501 | } |
| 2502 | #endif |
| 2503 | if (!Serializer::enabled() && !FLAG_debug_code) { |
| 2504 | return; |
| 2505 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2506 | } |
| 2507 | ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here |
| 2508 | reloc_info_writer.Write(&rinfo); |
| 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | |
| 2513 | void Assembler::CheckConstPool(bool force_emit, bool require_jump) { |
| 2514 | // Calculate the offset of the next check. It will be overwritten |
| 2515 | // when a const pool is generated or when const pools are being |
| 2516 | // blocked for a specific range. |
| 2517 | next_buffer_check_ = pc_offset() + kCheckConstInterval; |
| 2518 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2519 | // There is nothing to do if there are no pending relocation info entries. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2520 | if (num_prinfo_ == 0) return; |
| 2521 | |
| 2522 | // We emit a constant pool at regular intervals of about kDistBetweenPools |
| 2523 | // or when requested by parameter force_emit (e.g. after each function). |
| 2524 | // We prefer not to emit a jump unless the max distance is reached or if we |
| 2525 | // are running low on slots, which can happen if a lot of constants are being |
| 2526 | // emitted (e.g. --debug-code and many static references). |
| 2527 | int dist = pc_offset() - last_const_pool_end_; |
| 2528 | if (!force_emit && dist < kMaxDistBetweenPools && |
| 2529 | (require_jump || dist < kDistBetweenPools) && |
| 2530 | // TODO(1236125): Cleanup the "magic" number below. We know that |
| 2531 | // the code generation will test every kCheckConstIntervalInst. |
| 2532 | // Thus we are safe as long as we generate less than 7 constant |
| 2533 | // entries per instruction. |
| 2534 | (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) { |
| 2535 | return; |
| 2536 | } |
| 2537 | |
| 2538 | // If we did not return by now, we need to emit the constant pool soon. |
| 2539 | |
| 2540 | // However, some small sequences of instructions must not be broken up by the |
| 2541 | // insertion of a constant pool; such sequences are protected by setting |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2542 | // either const_pool_blocked_nesting_ or no_const_pool_before_, which are |
| 2543 | // both checked here. Also, recursive calls to CheckConstPool are blocked by |
| 2544 | // no_const_pool_before_. |
| 2545 | if (const_pool_blocked_nesting_ > 0 || pc_offset() < no_const_pool_before_) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2546 | // Emission is currently blocked; make sure we try again as soon as |
| 2547 | // possible. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2548 | if (const_pool_blocked_nesting_ > 0) { |
| 2549 | next_buffer_check_ = pc_offset() + kInstrSize; |
| 2550 | } else { |
| 2551 | next_buffer_check_ = no_const_pool_before_; |
| 2552 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2553 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2554 | // Something is wrong if emission is forced and blocked at the same time. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2555 | ASSERT(!force_emit); |
| 2556 | return; |
| 2557 | } |
| 2558 | |
| 2559 | int jump_instr = require_jump ? kInstrSize : 0; |
| 2560 | |
| 2561 | // Check that the code buffer is large enough before emitting the constant |
| 2562 | // pool and relocation information (include the jump over the pool and the |
| 2563 | // constant pool marker). |
| 2564 | int max_needed_space = |
| 2565 | jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize); |
| 2566 | while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer(); |
| 2567 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2568 | // Block recursive calls to CheckConstPool. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2569 | BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize + |
| 2570 | num_prinfo_*kInstrSize); |
| 2571 | // Don't bother to check for the emit calls below. |
| 2572 | next_buffer_check_ = no_const_pool_before_; |
| 2573 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2574 | // Emit jump over constant pool if necessary. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2575 | Label after_pool; |
| 2576 | if (require_jump) b(&after_pool); |
| 2577 | |
| 2578 | RecordComment("[ Constant Pool"); |
| 2579 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2580 | // Put down constant pool marker "Undefined instruction" as specified by |
| 2581 | // A3.1 Instruction set encoding. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2582 | emit(0x03000000 | num_prinfo_); |
| 2583 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2584 | // Emit constant pool entries. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2585 | for (int i = 0; i < num_prinfo_; i++) { |
| 2586 | RelocInfo& rinfo = prinfo_[i]; |
| 2587 | ASSERT(rinfo.rmode() != RelocInfo::COMMENT && |
| 2588 | rinfo.rmode() != RelocInfo::POSITION && |
| 2589 | rinfo.rmode() != RelocInfo::STATEMENT_POSITION); |
| 2590 | Instr instr = instr_at(rinfo.pc()); |
| 2591 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2592 | // Instruction to patch must be a ldr/str [pc, #offset]. |
| 2593 | // P and U set, B and W clear, Rn == pc, offset12 still 0. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2594 | ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | Off12Mask)) == |
| 2595 | (2*B25 | P | U | pc.code()*B16)); |
| 2596 | int delta = pc_ - rinfo.pc() - 8; |
| 2597 | ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32 |
| 2598 | if (delta < 0) { |
| 2599 | instr &= ~U; |
| 2600 | delta = -delta; |
| 2601 | } |
| 2602 | ASSERT(is_uint12(delta)); |
| 2603 | instr_at_put(rinfo.pc(), instr + delta); |
| 2604 | emit(rinfo.data()); |
| 2605 | } |
| 2606 | num_prinfo_ = 0; |
| 2607 | last_const_pool_end_ = pc_offset(); |
| 2608 | |
| 2609 | RecordComment("]"); |
| 2610 | |
| 2611 | if (after_pool.is_linked()) { |
| 2612 | bind(&after_pool); |
| 2613 | } |
| 2614 | |
| 2615 | // Since a constant pool was just emitted, move the check offset forward by |
| 2616 | // the standard interval. |
| 2617 | next_buffer_check_ = pc_offset() + kCheckConstInterval; |
| 2618 | } |
| 2619 | |
| 2620 | |
| 2621 | } } // namespace v8::internal |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2622 | |
| 2623 | #endif // V8_TARGET_ARCH_ARM |