mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1 | // Copyright (c) 1994-2006 Sun Microsystems Inc. |
| 2 | // All Rights Reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3 | // |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 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. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 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 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +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. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
| 37 | #include "v8.h" |
| 38 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 39 | #if defined(V8_TARGET_ARCH_ARM) |
| 40 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 41 | #include "arm/assembler-arm-inl.h" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 42 | #include "serialize.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 44 | namespace v8 { |
| 45 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +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 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 73 | void CpuFeatures::Probe() { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 74 | #ifndef __arm__ |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 75 | // For the simulator=arm build, use VFP when FLAG_enable_vfp3 is enabled. |
| 76 | if (FLAG_enable_vfp3) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 77 | supported_ |= 1u << VFP3; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 78 | } |
| 79 | // For the simulator=arm build, use ARMv7 when FLAG_enable_armv7 is enabled |
| 80 | if (FLAG_enable_armv7) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 81 | supported_ |= 1u << ARMv7; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 82 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 83 | #else // def __arm__ |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 84 | if (Serializer::enabled()) { |
| 85 | supported_ |= OS::CpuFeaturesImpliedByPlatform(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 86 | supported_ |= CpuFeaturesImpliedByCompiler(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +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 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 96 | |
| 97 | if (OS::ArmCpuHasFeature(ARMv7)) { |
| 98 | supported_ |= 1u << ARMv7; |
| 99 | found_by_runtime_probing_ |= 1u << ARMv7; |
| 100 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 101 | #endif |
| 102 | } |
| 103 | |
| 104 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | // ----------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | // Implementation of RelocInfo |
| 107 | |
| 108 | const int RelocInfo::kApplyMask = 0; |
| 109 | |
| 110 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [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 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 120 | void RelocInfo::PatchCode(byte* instructions, int instruction_count) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 121 | // Patch the code at the current address with the supplied instructions. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 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); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
| 133 | // Patch the code at the current PC with a call to the target address. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 134 | // Additional guard instructions can be added if required. |
| 135 | void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 152 | rmode_ = RelocInfo::EMBEDDED_OBJECT; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 153 | } else { |
| 154 | // no relocation needed |
| 155 | imm32_ = reinterpret_cast<intptr_t>(obj); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 156 | rmode_ = RelocInfo::NONE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | // ----------------------------------------------------------------------------- |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 214 | // Implementation of Assembler. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 215 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 216 | // Instruction encoding bits. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 232 | B6 = 1 << 6, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 233 | B7 = 1 << 7, |
| 234 | B8 = 1 << 8, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 235 | B9 = 1 << 9, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 236 | B12 = 1 << 12, |
| 237 | B16 = 1 << 16, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 238 | B18 = 1 << 18, |
| 239 | B19 = 1 << 19, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 249 | // Instruction bit masks. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 250 | RdMask = 15 << 12, // in str instruction |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 251 | CondMask = 15 << 28, |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 252 | CoprocessorMask = 15 << 8, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | OpCodeMask = 15 << 21, // in data-processing instructions |
| 254 | Imm24Mask = (1 << 24) - 1, |
| 255 | Off12Mask = (1 << 12) - 1, |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 256 | // Reserved condition. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 257 | nv = 15 << 28 |
| 258 | }; |
| 259 | |
| 260 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 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; |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 272 | // mov lr, pc |
| 273 | const Instr kMovLrPc = al | 13*B21 | pc.code() | lr.code() * B12; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [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; |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 282 | const Instr kMovMvnMask = 0x6d * B21 | 0xf * B16; |
| 283 | const Instr kMovMvnPattern = 0xd * B21; |
| 284 | const Instr kMovMvnFlip = B22; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 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; |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 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 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [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; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 315 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 316 | // Spare buffer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 322 | // Do our own buffer management. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 340 | // Use externally provided buffer instead. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 347 | // Setup buffer pointers. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 353 | const_pool_blocked_nesting_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | no_const_pool_before_ = 0; |
| 355 | last_const_pool_end_ = 0; |
| 356 | last_bound_pos_ = 0; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 357 | current_statement_position_ = RelocInfo::kNoPosition; |
| 358 | current_position_ = RelocInfo::kNoPosition; |
| 359 | written_statement_position_ = current_statement_position_; |
| 360 | written_position_ = current_position_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
| 364 | Assembler::~Assembler() { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 365 | ASSERT(const_pool_blocked_nesting_ == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 377 | // Emit constant pool if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 378 | CheckConstPool(true, false); |
| 379 | ASSERT(num_prinfo_ == 0); |
| 380 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 381 | // Setup code descriptor. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 397 | void Assembler::CodeTargetAlign() { |
| 398 | // Preferred alignment of jump targets on some ARM chips. |
| 399 | Align(8); |
| 400 | } |
| 401 | |
| 402 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [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 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 448 | Register Assembler::GetRd(Instr instr) { |
| 449 | Register reg; |
| 450 | reg.code_ = ((instr & kRdMask) >> kRdShift); |
| 451 | return reg; |
| 452 | } |
| 453 | |
| 454 | |
| 455 | bool Assembler::IsPush(Instr instr) { |
| 456 | return ((instr & ~kRdMask) == kPushRegPattern); |
| 457 | } |
| 458 | |
| 459 | |
| 460 | bool Assembler::IsPop(Instr instr) { |
| 461 | return ((instr & ~kRdMask) == kPopRegPattern); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | bool Assembler::IsStrRegFpOffset(Instr instr) { |
| 466 | return ((instr & kLdrStrInstrTypeMask) == kStrRegFpOffsetPattern); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | bool Assembler::IsLdrRegFpOffset(Instr instr) { |
| 471 | return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpOffsetPattern); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | bool Assembler::IsStrRegFpNegOffset(Instr instr) { |
| 476 | return ((instr & kLdrStrInstrTypeMask) == kStrRegFpNegOffsetPattern); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | bool Assembler::IsLdrRegFpNegOffset(Instr instr) { |
| 481 | return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpNegOffsetPattern); |
| 482 | } |
| 483 | |
| 484 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 485 | // Labels refer to positions in the (to be) generated code. |
| 486 | // There are bound, linked, and unused labels. |
| 487 | // |
| 488 | // Bound labels refer to known positions in the already |
| 489 | // generated code. pos() is the position the label refers to. |
| 490 | // |
| 491 | // Linked labels refer to unknown positions in the code |
| 492 | // to be generated; pos() is the position of the last |
| 493 | // instruction using the label. |
| 494 | |
| 495 | |
| 496 | // The link chain is terminated by a negative code position (must be aligned) |
| 497 | const int kEndOfChain = -4; |
| 498 | |
| 499 | |
| 500 | int Assembler::target_at(int pos) { |
| 501 | Instr instr = instr_at(pos); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 502 | if ((instr & ~Imm24Mask) == 0) { |
| 503 | // Emitted label constant, not part of a branch. |
| 504 | return instr - (Code::kHeaderSize - kHeapObjectTag); |
| 505 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 506 | ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24 |
| 507 | int imm26 = ((instr & Imm24Mask) << 8) >> 6; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 508 | if ((instr & CondMask) == nv && (instr & B24) != 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 509 | // blx uses bit 24 to encode bit 2 of imm26 |
| 510 | imm26 += 2; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 511 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 512 | return pos + kPcLoadDelta + imm26; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | |
| 516 | void Assembler::target_at_put(int pos, int target_pos) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 517 | Instr instr = instr_at(pos); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 518 | if ((instr & ~Imm24Mask) == 0) { |
| 519 | ASSERT(target_pos == kEndOfChain || target_pos >= 0); |
| 520 | // Emitted label constant, not part of a branch. |
| 521 | // Make label relative to Code* of generated Code object. |
| 522 | instr_at_put(pos, target_pos + (Code::kHeaderSize - kHeapObjectTag)); |
| 523 | return; |
| 524 | } |
| 525 | int imm26 = target_pos - (pos + kPcLoadDelta); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 526 | ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24 |
| 527 | if ((instr & CondMask) == nv) { |
| 528 | // blx uses bit 24 to encode bit 2 of imm26 |
| 529 | ASSERT((imm26 & 1) == 0); |
| 530 | instr = (instr & ~(B24 | Imm24Mask)) | ((imm26 & 2) >> 1)*B24; |
| 531 | } else { |
| 532 | ASSERT((imm26 & 3) == 0); |
| 533 | instr &= ~Imm24Mask; |
| 534 | } |
| 535 | int imm24 = imm26 >> 2; |
| 536 | ASSERT(is_int24(imm24)); |
| 537 | instr_at_put(pos, instr | (imm24 & Imm24Mask)); |
| 538 | } |
| 539 | |
| 540 | |
| 541 | void Assembler::print(Label* L) { |
| 542 | if (L->is_unused()) { |
| 543 | PrintF("unused label\n"); |
| 544 | } else if (L->is_bound()) { |
| 545 | PrintF("bound label to %d\n", L->pos()); |
| 546 | } else if (L->is_linked()) { |
| 547 | Label l = *L; |
| 548 | PrintF("unbound label"); |
| 549 | while (l.is_linked()) { |
| 550 | PrintF("@ %d ", l.pos()); |
| 551 | Instr instr = instr_at(l.pos()); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 552 | if ((instr & ~Imm24Mask) == 0) { |
| 553 | PrintF("value\n"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 554 | } else { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 555 | ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx |
| 556 | int cond = instr & CondMask; |
| 557 | const char* b; |
| 558 | const char* c; |
| 559 | if (cond == nv) { |
| 560 | b = "blx"; |
| 561 | c = ""; |
| 562 | } else { |
| 563 | if ((instr & B24) != 0) |
| 564 | b = "bl"; |
| 565 | else |
| 566 | b = "b"; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 567 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 568 | switch (cond) { |
| 569 | case eq: c = "eq"; break; |
| 570 | case ne: c = "ne"; break; |
| 571 | case hs: c = "hs"; break; |
| 572 | case lo: c = "lo"; break; |
| 573 | case mi: c = "mi"; break; |
| 574 | case pl: c = "pl"; break; |
| 575 | case vs: c = "vs"; break; |
| 576 | case vc: c = "vc"; break; |
| 577 | case hi: c = "hi"; break; |
| 578 | case ls: c = "ls"; break; |
| 579 | case ge: c = "ge"; break; |
| 580 | case lt: c = "lt"; break; |
| 581 | case gt: c = "gt"; break; |
| 582 | case le: c = "le"; break; |
| 583 | case al: c = ""; break; |
| 584 | default: |
| 585 | c = ""; |
| 586 | UNREACHABLE(); |
| 587 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 588 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 589 | PrintF("%s%s\n", b, c); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 590 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 591 | next(&l); |
| 592 | } |
| 593 | } else { |
| 594 | PrintF("label in inconsistent state (pos = %d)\n", L->pos_); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 599 | void Assembler::bind_to(Label* L, int pos) { |
| 600 | ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position |
| 601 | while (L->is_linked()) { |
| 602 | int fixup_pos = L->pos(); |
| 603 | next(L); // call next before overwriting link with target at fixup_pos |
| 604 | target_at_put(fixup_pos, pos); |
| 605 | } |
| 606 | L->bind_to(pos); |
| 607 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 608 | // Keep track of the last bound label so we don't eliminate any instructions |
| 609 | // before a bound label. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 610 | if (pos > last_bound_pos_) |
| 611 | last_bound_pos_ = pos; |
| 612 | } |
| 613 | |
| 614 | |
| 615 | void Assembler::link_to(Label* L, Label* appendix) { |
| 616 | if (appendix->is_linked()) { |
| 617 | if (L->is_linked()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 618 | // Append appendix to L's list. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 619 | int fixup_pos; |
| 620 | int link = L->pos(); |
| 621 | do { |
| 622 | fixup_pos = link; |
| 623 | link = target_at(fixup_pos); |
| 624 | } while (link > 0); |
| 625 | ASSERT(link == kEndOfChain); |
| 626 | target_at_put(fixup_pos, appendix->pos()); |
| 627 | } else { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 628 | // L is empty, simply use appendix. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 629 | *L = *appendix; |
| 630 | } |
| 631 | } |
| 632 | appendix->Unuse(); // appendix should not be used anymore |
| 633 | } |
| 634 | |
| 635 | |
| 636 | void Assembler::bind(Label* L) { |
| 637 | ASSERT(!L->is_bound()); // label can only be bound once |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 638 | bind_to(L, pc_offset()); |
| 639 | } |
| 640 | |
| 641 | |
| 642 | void Assembler::next(Label* L) { |
| 643 | ASSERT(L->is_linked()); |
| 644 | int link = target_at(L->pos()); |
| 645 | if (link > 0) { |
| 646 | L->link_to(link); |
| 647 | } else { |
| 648 | ASSERT(link == kEndOfChain); |
| 649 | L->Unuse(); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 654 | static Instr EncodeMovwImmediate(uint32_t immediate) { |
| 655 | ASSERT(immediate < 0x10000); |
| 656 | return ((immediate & 0xf000) << 4) | (immediate & 0xfff); |
| 657 | } |
| 658 | |
| 659 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 660 | // Low-level code emission routines depending on the addressing mode. |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 661 | // If this returns true then you have to use the rotate_imm and immed_8 |
| 662 | // that it returns, because it may have already changed the instruction |
| 663 | // to match them! |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 664 | static bool fits_shifter(uint32_t imm32, |
| 665 | uint32_t* rotate_imm, |
| 666 | uint32_t* immed_8, |
| 667 | Instr* instr) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 668 | // imm32 must be unsigned. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 669 | for (int rot = 0; rot < 16; rot++) { |
| 670 | uint32_t imm8 = (imm32 << 2*rot) | (imm32 >> (32 - 2*rot)); |
| 671 | if ((imm8 <= 0xff)) { |
| 672 | *rotate_imm = rot; |
| 673 | *immed_8 = imm8; |
| 674 | return true; |
| 675 | } |
| 676 | } |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 677 | // If the opcode is one with a complementary version and the complementary |
| 678 | // immediate fits, change the opcode. |
| 679 | if (instr != NULL) { |
| 680 | if ((*instr & kMovMvnMask) == kMovMvnPattern) { |
| 681 | if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) { |
| 682 | *instr ^= kMovMvnFlip; |
| 683 | return true; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 684 | } else if ((*instr & kMovLeaveCCMask) == kMovLeaveCCPattern) { |
| 685 | if (CpuFeatures::IsSupported(ARMv7)) { |
| 686 | if (imm32 < 0x10000) { |
| 687 | *instr ^= kMovwLeaveCCFlip; |
| 688 | *instr |= EncodeMovwImmediate(imm32); |
| 689 | *rotate_imm = *immed_8 = 0; // Not used for movw. |
| 690 | return true; |
| 691 | } |
| 692 | } |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 693 | } |
| 694 | } else if ((*instr & kCmpCmnMask) == kCmpCmnPattern) { |
| 695 | if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) { |
| 696 | *instr ^= kCmpCmnFlip; |
| 697 | return true; |
| 698 | } |
| 699 | } else { |
| 700 | Instr alu_insn = (*instr & kALUMask); |
| 701 | if (alu_insn == kAddPattern || |
| 702 | alu_insn == kSubPattern) { |
| 703 | if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) { |
| 704 | *instr ^= kAddSubFlip; |
| 705 | return true; |
| 706 | } |
| 707 | } else if (alu_insn == kAndPattern || |
| 708 | alu_insn == kBicPattern) { |
| 709 | if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) { |
| 710 | *instr ^= kAndBicFlip; |
| 711 | return true; |
| 712 | } |
| 713 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | return false; |
| 717 | } |
| 718 | |
| 719 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 720 | // We have to use the temporary register for things that can be relocated even |
| 721 | // if they can be encoded in the ARM's 12 bits of immediate-offset instruction |
| 722 | // space. There is no guarantee that the relocated location can be similarly |
| 723 | // encoded. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 724 | static bool MustUseConstantPool(RelocInfo::Mode rmode) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 725 | if (rmode == RelocInfo::EXTERNAL_REFERENCE) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 726 | #ifdef DEBUG |
| 727 | if (!Serializer::enabled()) { |
| 728 | Serializer::TooLateToEnableNow(); |
| 729 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 730 | #endif // def DEBUG |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 731 | return Serializer::enabled(); |
| 732 | } else if (rmode == RelocInfo::NONE) { |
| 733 | return false; |
| 734 | } |
| 735 | return true; |
| 736 | } |
| 737 | |
| 738 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 739 | bool Operand::is_single_instruction() const { |
| 740 | if (rm_.is_valid()) return true; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 741 | if (MustUseConstantPool(rmode_)) return false; |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 742 | uint32_t dummy1, dummy2; |
| 743 | return fits_shifter(imm32_, &dummy1, &dummy2, NULL); |
| 744 | } |
| 745 | |
| 746 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 747 | void Assembler::addrmod1(Instr instr, |
| 748 | Register rn, |
| 749 | Register rd, |
| 750 | const Operand& x) { |
| 751 | CheckBuffer(); |
| 752 | ASSERT((instr & ~(CondMask | OpCodeMask | S)) == 0); |
| 753 | if (!x.rm_.is_valid()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 754 | // Immediate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 755 | uint32_t rotate_imm; |
| 756 | uint32_t immed_8; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 757 | if (MustUseConstantPool(x.rmode_) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 758 | !fits_shifter(x.imm32_, &rotate_imm, &immed_8, &instr)) { |
| 759 | // The immediate operand cannot be encoded as a shifter operand, so load |
| 760 | // it first to register ip and change the original instruction to use ip. |
| 761 | // However, if the original instruction is a 'mov rd, x' (not setting the |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 762 | // condition code), then replace it with a 'ldr rd, [pc]'. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 763 | CHECK(!rn.is(ip)); // rn should never be ip, or will be trashed |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 764 | Condition cond = static_cast<Condition>(instr & CondMask); |
| 765 | if ((instr & ~CondMask) == 13*B21) { // mov, S not set |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 766 | if (MustUseConstantPool(x.rmode_) || |
| 767 | !CpuFeatures::IsSupported(ARMv7)) { |
| 768 | RecordRelocInfo(x.rmode_, x.imm32_); |
| 769 | ldr(rd, MemOperand(pc, 0), cond); |
| 770 | } else { |
| 771 | // Will probably use movw, will certainly not use constant pool. |
| 772 | mov(rd, Operand(x.imm32_ & 0xffff), LeaveCC, cond); |
| 773 | movt(rd, static_cast<uint32_t>(x.imm32_) >> 16, cond); |
| 774 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 775 | } else { |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 776 | // If this is not a mov or mvn instruction we may still be able to avoid |
| 777 | // a constant pool entry by using mvn or movw. |
| 778 | if (!MustUseConstantPool(x.rmode_) && |
| 779 | (instr & kMovMvnMask) != kMovMvnPattern) { |
| 780 | mov(ip, x, LeaveCC, cond); |
| 781 | } else { |
| 782 | RecordRelocInfo(x.rmode_, x.imm32_); |
| 783 | ldr(ip, MemOperand(pc, 0), cond); |
| 784 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 785 | addrmod1(instr, rn, rd, Operand(ip)); |
| 786 | } |
| 787 | return; |
| 788 | } |
| 789 | instr |= I | rotate_imm*B8 | immed_8; |
| 790 | } else if (!x.rs_.is_valid()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 791 | // Immediate shift. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 792 | instr |= x.shift_imm_*B7 | x.shift_op_ | x.rm_.code(); |
| 793 | } else { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 794 | // Register shift. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 795 | ASSERT(!rn.is(pc) && !rd.is(pc) && !x.rm_.is(pc) && !x.rs_.is(pc)); |
| 796 | instr |= x.rs_.code()*B8 | x.shift_op_ | B4 | x.rm_.code(); |
| 797 | } |
| 798 | emit(instr | rn.code()*B16 | rd.code()*B12); |
| 799 | if (rn.is(pc) || x.rm_.is(pc)) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 800 | // Block constant pool emission for one instruction after reading pc. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 801 | BlockConstPoolBefore(pc_offset() + kInstrSize); |
| 802 | } |
| 803 | |
| 804 | |
| 805 | void Assembler::addrmod2(Instr instr, Register rd, const MemOperand& x) { |
| 806 | ASSERT((instr & ~(CondMask | B | L)) == B26); |
| 807 | int am = x.am_; |
| 808 | if (!x.rm_.is_valid()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 809 | // Immediate offset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 810 | int offset_12 = x.offset_; |
| 811 | if (offset_12 < 0) { |
| 812 | offset_12 = -offset_12; |
| 813 | am ^= U; |
| 814 | } |
| 815 | if (!is_uint12(offset_12)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 816 | // Immediate offset cannot be encoded, load it first to register ip |
| 817 | // rn (and rd in a load) should never be ip, or will be trashed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 818 | ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip))); |
| 819 | mov(ip, Operand(x.offset_), LeaveCC, |
| 820 | static_cast<Condition>(instr & CondMask)); |
| 821 | addrmod2(instr, rd, MemOperand(x.rn_, ip, x.am_)); |
| 822 | return; |
| 823 | } |
| 824 | ASSERT(offset_12 >= 0); // no masking needed |
| 825 | instr |= offset_12; |
| 826 | } else { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 827 | // Register offset (shift_imm_ and shift_op_ are 0) or scaled |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 828 | // register offset the constructors make sure than both shift_imm_ |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 829 | // and shift_op_ are initialized. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 830 | ASSERT(!x.rm_.is(pc)); |
| 831 | instr |= B25 | x.shift_imm_*B7 | x.shift_op_ | x.rm_.code(); |
| 832 | } |
| 833 | ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback |
| 834 | emit(instr | am | x.rn_.code()*B16 | rd.code()*B12); |
| 835 | } |
| 836 | |
| 837 | |
| 838 | void Assembler::addrmod3(Instr instr, Register rd, const MemOperand& x) { |
| 839 | ASSERT((instr & ~(CondMask | L | S6 | H)) == (B4 | B7)); |
| 840 | ASSERT(x.rn_.is_valid()); |
| 841 | int am = x.am_; |
| 842 | if (!x.rm_.is_valid()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 843 | // Immediate offset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 844 | int offset_8 = x.offset_; |
| 845 | if (offset_8 < 0) { |
| 846 | offset_8 = -offset_8; |
| 847 | am ^= U; |
| 848 | } |
| 849 | if (!is_uint8(offset_8)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 850 | // Immediate offset cannot be encoded, load it first to register ip |
| 851 | // rn (and rd in a load) should never be ip, or will be trashed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 852 | ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip))); |
| 853 | mov(ip, Operand(x.offset_), LeaveCC, |
| 854 | static_cast<Condition>(instr & CondMask)); |
| 855 | addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_)); |
| 856 | return; |
| 857 | } |
| 858 | ASSERT(offset_8 >= 0); // no masking needed |
| 859 | instr |= B | (offset_8 >> 4)*B8 | (offset_8 & 0xf); |
| 860 | } else if (x.shift_imm_ != 0) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 861 | // Scaled register offset not supported, load index first |
| 862 | // rn (and rd in a load) should never be ip, or will be trashed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 863 | ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip))); |
| 864 | mov(ip, Operand(x.rm_, x.shift_op_, x.shift_imm_), LeaveCC, |
| 865 | static_cast<Condition>(instr & CondMask)); |
| 866 | addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_)); |
| 867 | return; |
| 868 | } else { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 869 | // Register offset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 870 | ASSERT((am & (P|W)) == P || !x.rm_.is(pc)); // no pc index with writeback |
| 871 | instr |= x.rm_.code(); |
| 872 | } |
| 873 | ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback |
| 874 | emit(instr | am | x.rn_.code()*B16 | rd.code()*B12); |
| 875 | } |
| 876 | |
| 877 | |
| 878 | void Assembler::addrmod4(Instr instr, Register rn, RegList rl) { |
| 879 | ASSERT((instr & ~(CondMask | P | U | W | L)) == B27); |
| 880 | ASSERT(rl != 0); |
| 881 | ASSERT(!rn.is(pc)); |
| 882 | emit(instr | rn.code()*B16 | rl); |
| 883 | } |
| 884 | |
| 885 | |
| 886 | void Assembler::addrmod5(Instr instr, CRegister crd, const MemOperand& x) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 887 | // Unindexed addressing is not encoded by this function. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 888 | ASSERT_EQ((B27 | B26), |
| 889 | (instr & ~(CondMask | CoprocessorMask | P | U | N | W | L))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 890 | ASSERT(x.rn_.is_valid() && !x.rm_.is_valid()); |
| 891 | int am = x.am_; |
| 892 | int offset_8 = x.offset_; |
| 893 | ASSERT((offset_8 & 3) == 0); // offset must be an aligned word offset |
| 894 | offset_8 >>= 2; |
| 895 | if (offset_8 < 0) { |
| 896 | offset_8 = -offset_8; |
| 897 | am ^= U; |
| 898 | } |
| 899 | ASSERT(is_uint8(offset_8)); // unsigned word offset must fit in a byte |
| 900 | ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback |
| 901 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 902 | // Post-indexed addressing requires W == 1; different than in addrmod2/3. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 903 | if ((am & P) == 0) |
| 904 | am |= W; |
| 905 | |
| 906 | ASSERT(offset_8 >= 0); // no masking needed |
| 907 | emit(instr | am | x.rn_.code()*B16 | crd.code()*B12 | offset_8); |
| 908 | } |
| 909 | |
| 910 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 911 | int Assembler::branch_offset(Label* L, bool jump_elimination_allowed) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 912 | int target_pos; |
| 913 | if (L->is_bound()) { |
| 914 | target_pos = L->pos(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 915 | } else { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 916 | if (L->is_linked()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 917 | target_pos = L->pos(); // L's link |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 918 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 919 | target_pos = kEndOfChain; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 920 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 921 | L->link_to(pc_offset()); |
| 922 | } |
| 923 | |
| 924 | // Block the emission of the constant pool, since the branch instruction must |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 925 | // be emitted at the pc offset recorded by the label. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 926 | BlockConstPoolBefore(pc_offset() + kInstrSize); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 927 | return target_pos - (pc_offset() + kPcLoadDelta); |
| 928 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 929 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 930 | |
| 931 | void Assembler::label_at_put(Label* L, int at_offset) { |
| 932 | int target_pos; |
| 933 | if (L->is_bound()) { |
| 934 | target_pos = L->pos(); |
| 935 | } else { |
| 936 | if (L->is_linked()) { |
| 937 | target_pos = L->pos(); // L's link |
| 938 | } else { |
| 939 | target_pos = kEndOfChain; |
| 940 | } |
| 941 | L->link_to(at_offset); |
| 942 | instr_at_put(at_offset, target_pos + (Code::kHeaderSize - kHeapObjectTag)); |
| 943 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 947 | // Branch instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 948 | void Assembler::b(int branch_offset, Condition cond) { |
| 949 | ASSERT((branch_offset & 3) == 0); |
| 950 | int imm24 = branch_offset >> 2; |
| 951 | ASSERT(is_int24(imm24)); |
| 952 | emit(cond | B27 | B25 | (imm24 & Imm24Mask)); |
| 953 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 954 | if (cond == al) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 955 | // Dead code is a good location to emit the constant pool. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 956 | CheckConstPool(false, false); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 957 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | |
| 961 | void Assembler::bl(int branch_offset, Condition cond) { |
| 962 | ASSERT((branch_offset & 3) == 0); |
| 963 | int imm24 = branch_offset >> 2; |
| 964 | ASSERT(is_int24(imm24)); |
| 965 | emit(cond | B27 | B25 | B24 | (imm24 & Imm24Mask)); |
| 966 | } |
| 967 | |
| 968 | |
| 969 | void Assembler::blx(int branch_offset) { // v5 and above |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 970 | WriteRecordedPositions(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 971 | ASSERT((branch_offset & 1) == 0); |
| 972 | int h = ((branch_offset & 2) >> 1)*B24; |
| 973 | int imm24 = branch_offset >> 2; |
| 974 | ASSERT(is_int24(imm24)); |
| 975 | emit(15 << 28 | B27 | B25 | h | (imm24 & Imm24Mask)); |
| 976 | } |
| 977 | |
| 978 | |
| 979 | void Assembler::blx(Register target, Condition cond) { // v5 and above |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 980 | WriteRecordedPositions(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 981 | ASSERT(!target.is(pc)); |
| 982 | emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | 3*B4 | target.code()); |
| 983 | } |
| 984 | |
| 985 | |
| 986 | void Assembler::bx(Register target, Condition cond) { // v5 and above, plus v4t |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 987 | WriteRecordedPositions(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 988 | ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged |
| 989 | emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | B4 | target.code()); |
| 990 | } |
| 991 | |
| 992 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 993 | // Data-processing instructions. |
| 994 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 995 | void Assembler::and_(Register dst, Register src1, const Operand& src2, |
| 996 | SBit s, Condition cond) { |
| 997 | addrmod1(cond | 0*B21 | s, src1, dst, src2); |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | void Assembler::eor(Register dst, Register src1, const Operand& src2, |
| 1002 | SBit s, Condition cond) { |
| 1003 | addrmod1(cond | 1*B21 | s, src1, dst, src2); |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | void Assembler::sub(Register dst, Register src1, const Operand& src2, |
| 1008 | SBit s, Condition cond) { |
| 1009 | addrmod1(cond | 2*B21 | s, src1, dst, src2); |
| 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | void Assembler::rsb(Register dst, Register src1, const Operand& src2, |
| 1014 | SBit s, Condition cond) { |
| 1015 | addrmod1(cond | 3*B21 | s, src1, dst, src2); |
| 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | void Assembler::add(Register dst, Register src1, const Operand& src2, |
| 1020 | SBit s, Condition cond) { |
| 1021 | addrmod1(cond | 4*B21 | s, src1, dst, src2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1022 | |
| 1023 | // Eliminate pattern: push(r), pop() |
| 1024 | // str(src, MemOperand(sp, 4, NegPreIndex), al); |
| 1025 | // add(sp, sp, Operand(kPointerSize)); |
| 1026 | // Both instructions can be eliminated. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1027 | if (can_peephole_optimize(2) && |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1028 | // Pattern. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1029 | instr_at(pc_ - 1 * kInstrSize) == kPopInstruction && |
| 1030 | (instr_at(pc_ - 2 * kInstrSize) & ~RdMask) == kPushRegPattern) { |
| 1031 | pc_ -= 2 * kInstrSize; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1032 | if (FLAG_print_peephole_optimization) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1033 | PrintF("%x push(reg)/pop() eliminated\n", pc_offset()); |
| 1034 | } |
| 1035 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | |
| 1039 | void Assembler::adc(Register dst, Register src1, const Operand& src2, |
| 1040 | SBit s, Condition cond) { |
| 1041 | addrmod1(cond | 5*B21 | s, src1, dst, src2); |
| 1042 | } |
| 1043 | |
| 1044 | |
| 1045 | void Assembler::sbc(Register dst, Register src1, const Operand& src2, |
| 1046 | SBit s, Condition cond) { |
| 1047 | addrmod1(cond | 6*B21 | s, src1, dst, src2); |
| 1048 | } |
| 1049 | |
| 1050 | |
| 1051 | void Assembler::rsc(Register dst, Register src1, const Operand& src2, |
| 1052 | SBit s, Condition cond) { |
| 1053 | addrmod1(cond | 7*B21 | s, src1, dst, src2); |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | void Assembler::tst(Register src1, const Operand& src2, Condition cond) { |
| 1058 | addrmod1(cond | 8*B21 | S, src1, r0, src2); |
| 1059 | } |
| 1060 | |
| 1061 | |
| 1062 | void Assembler::teq(Register src1, const Operand& src2, Condition cond) { |
| 1063 | addrmod1(cond | 9*B21 | S, src1, r0, src2); |
| 1064 | } |
| 1065 | |
| 1066 | |
| 1067 | void Assembler::cmp(Register src1, const Operand& src2, Condition cond) { |
| 1068 | addrmod1(cond | 10*B21 | S, src1, r0, src2); |
| 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | void Assembler::cmn(Register src1, const Operand& src2, Condition cond) { |
| 1073 | addrmod1(cond | 11*B21 | S, src1, r0, src2); |
| 1074 | } |
| 1075 | |
| 1076 | |
| 1077 | void Assembler::orr(Register dst, Register src1, const Operand& src2, |
| 1078 | SBit s, Condition cond) { |
| 1079 | addrmod1(cond | 12*B21 | s, src1, dst, src2); |
| 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | void Assembler::mov(Register dst, const Operand& src, SBit s, Condition cond) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1084 | if (dst.is(pc)) { |
| 1085 | WriteRecordedPositions(); |
| 1086 | } |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1087 | // Don't allow nop instructions in the form mov rn, rn to be generated using |
| 1088 | // the mov instruction. They must be generated using nop(int) |
| 1089 | // pseudo instructions. |
| 1090 | ASSERT(!(src.is_reg() && src.rm().is(dst) && s == LeaveCC && cond == al)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1091 | addrmod1(cond | 13*B21 | s, r0, dst, src); |
| 1092 | } |
| 1093 | |
| 1094 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 1095 | void Assembler::movw(Register reg, uint32_t immediate, Condition cond) { |
| 1096 | ASSERT(immediate < 0x10000); |
| 1097 | mov(reg, Operand(immediate), LeaveCC, cond); |
| 1098 | } |
| 1099 | |
| 1100 | |
| 1101 | void Assembler::movt(Register reg, uint32_t immediate, Condition cond) { |
| 1102 | emit(cond | 0x34*B20 | reg.code()*B12 | EncodeMovwImmediate(immediate)); |
| 1103 | } |
| 1104 | |
| 1105 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1106 | void Assembler::bic(Register dst, Register src1, const Operand& src2, |
| 1107 | SBit s, Condition cond) { |
| 1108 | addrmod1(cond | 14*B21 | s, src1, dst, src2); |
| 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) { |
| 1113 | addrmod1(cond | 15*B21 | s, r0, dst, src); |
| 1114 | } |
| 1115 | |
| 1116 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1117 | // Multiply instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1118 | void Assembler::mla(Register dst, Register src1, Register src2, Register srcA, |
| 1119 | SBit s, Condition cond) { |
| 1120 | ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc) && !srcA.is(pc)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1121 | emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 | |
| 1122 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1123 | } |
| 1124 | |
| 1125 | |
| 1126 | void Assembler::mul(Register dst, Register src1, Register src2, |
| 1127 | SBit s, Condition cond) { |
| 1128 | ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1129 | // dst goes in bits 16-19 for this instruction! |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1130 | emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1131 | } |
| 1132 | |
| 1133 | |
| 1134 | void Assembler::smlal(Register dstL, |
| 1135 | Register dstH, |
| 1136 | Register src1, |
| 1137 | Register src2, |
| 1138 | SBit s, |
| 1139 | Condition cond) { |
| 1140 | ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1141 | ASSERT(!dstL.is(dstH)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1142 | emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 | |
| 1143 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1144 | } |
| 1145 | |
| 1146 | |
| 1147 | void Assembler::smull(Register dstL, |
| 1148 | Register dstH, |
| 1149 | Register src1, |
| 1150 | Register src2, |
| 1151 | SBit s, |
| 1152 | Condition cond) { |
| 1153 | ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1154 | ASSERT(!dstL.is(dstH)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1155 | emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 | |
| 1156 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1157 | } |
| 1158 | |
| 1159 | |
| 1160 | void Assembler::umlal(Register dstL, |
| 1161 | Register dstH, |
| 1162 | Register src1, |
| 1163 | Register src2, |
| 1164 | SBit s, |
| 1165 | Condition cond) { |
| 1166 | ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1167 | ASSERT(!dstL.is(dstH)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1168 | emit(cond | B23 | A | s | dstH.code()*B16 | dstL.code()*B12 | |
| 1169 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | void Assembler::umull(Register dstL, |
| 1174 | Register dstH, |
| 1175 | Register src1, |
| 1176 | Register src2, |
| 1177 | SBit s, |
| 1178 | Condition cond) { |
| 1179 | ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1180 | ASSERT(!dstL.is(dstH)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1181 | emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1182 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1183 | } |
| 1184 | |
| 1185 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1186 | // Miscellaneous arithmetic instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1187 | void Assembler::clz(Register dst, Register src, Condition cond) { |
| 1188 | // v5 and above. |
| 1189 | ASSERT(!dst.is(pc) && !src.is(pc)); |
| 1190 | emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 | |
| 1191 | 15*B8 | B4 | src.code()); |
| 1192 | } |
| 1193 | |
| 1194 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1195 | // Bitfield manipulation instructions. |
| 1196 | |
| 1197 | // Unsigned bit field extract. |
| 1198 | // Extracts #width adjacent bits from position #lsb in a register, and |
| 1199 | // writes them to the low bits of a destination register. |
| 1200 | // ubfx dst, src, #lsb, #width |
| 1201 | void Assembler::ubfx(Register dst, |
| 1202 | Register src, |
| 1203 | int lsb, |
| 1204 | int width, |
| 1205 | Condition cond) { |
| 1206 | // v7 and above. |
| 1207 | ASSERT(CpuFeatures::IsSupported(ARMv7)); |
| 1208 | ASSERT(!dst.is(pc) && !src.is(pc)); |
| 1209 | ASSERT((lsb >= 0) && (lsb <= 31)); |
| 1210 | ASSERT((width >= 1) && (width <= (32 - lsb))); |
| 1211 | emit(cond | 0xf*B23 | B22 | B21 | (width - 1)*B16 | dst.code()*B12 | |
| 1212 | lsb*B7 | B6 | B4 | src.code()); |
| 1213 | } |
| 1214 | |
| 1215 | |
| 1216 | // Signed bit field extract. |
| 1217 | // Extracts #width adjacent bits from position #lsb in a register, and |
| 1218 | // writes them to the low bits of a destination register. The extracted |
| 1219 | // value is sign extended to fill the destination register. |
| 1220 | // sbfx dst, src, #lsb, #width |
| 1221 | void Assembler::sbfx(Register dst, |
| 1222 | Register src, |
| 1223 | int lsb, |
| 1224 | int width, |
| 1225 | Condition cond) { |
| 1226 | // v7 and above. |
| 1227 | ASSERT(CpuFeatures::IsSupported(ARMv7)); |
| 1228 | ASSERT(!dst.is(pc) && !src.is(pc)); |
| 1229 | ASSERT((lsb >= 0) && (lsb <= 31)); |
| 1230 | ASSERT((width >= 1) && (width <= (32 - lsb))); |
| 1231 | emit(cond | 0xf*B23 | B21 | (width - 1)*B16 | dst.code()*B12 | |
| 1232 | lsb*B7 | B6 | B4 | src.code()); |
| 1233 | } |
| 1234 | |
| 1235 | |
| 1236 | // Bit field clear. |
| 1237 | // Sets #width adjacent bits at position #lsb in the destination register |
| 1238 | // to zero, preserving the value of the other bits. |
| 1239 | // bfc dst, #lsb, #width |
| 1240 | void Assembler::bfc(Register dst, int lsb, int width, Condition cond) { |
| 1241 | // v7 and above. |
| 1242 | ASSERT(CpuFeatures::IsSupported(ARMv7)); |
| 1243 | ASSERT(!dst.is(pc)); |
| 1244 | ASSERT((lsb >= 0) && (lsb <= 31)); |
| 1245 | ASSERT((width >= 1) && (width <= (32 - lsb))); |
| 1246 | int msb = lsb + width - 1; |
| 1247 | emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 | 0xf); |
| 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | // Bit field insert. |
| 1252 | // Inserts #width adjacent bits from the low bits of the source register |
| 1253 | // into position #lsb of the destination register. |
| 1254 | // bfi dst, src, #lsb, #width |
| 1255 | void Assembler::bfi(Register dst, |
| 1256 | Register src, |
| 1257 | int lsb, |
| 1258 | int width, |
| 1259 | Condition cond) { |
| 1260 | // v7 and above. |
| 1261 | ASSERT(CpuFeatures::IsSupported(ARMv7)); |
| 1262 | ASSERT(!dst.is(pc) && !src.is(pc)); |
| 1263 | ASSERT((lsb >= 0) && (lsb <= 31)); |
| 1264 | ASSERT((width >= 1) && (width <= (32 - lsb))); |
| 1265 | int msb = lsb + width - 1; |
| 1266 | emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 | |
| 1267 | src.code()); |
| 1268 | } |
| 1269 | |
| 1270 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1271 | // Status register access instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1272 | void Assembler::mrs(Register dst, SRegister s, Condition cond) { |
| 1273 | ASSERT(!dst.is(pc)); |
| 1274 | emit(cond | B24 | s | 15*B16 | dst.code()*B12); |
| 1275 | } |
| 1276 | |
| 1277 | |
| 1278 | void Assembler::msr(SRegisterFieldMask fields, const Operand& src, |
| 1279 | Condition cond) { |
| 1280 | ASSERT(fields >= B16 && fields < B20); // at least one field set |
| 1281 | Instr instr; |
| 1282 | if (!src.rm_.is_valid()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1283 | // Immediate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1284 | uint32_t rotate_imm; |
| 1285 | uint32_t immed_8; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 1286 | if (MustUseConstantPool(src.rmode_) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1287 | !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1288 | // Immediate operand cannot be encoded, load it first to register ip. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1289 | RecordRelocInfo(src.rmode_, src.imm32_); |
| 1290 | ldr(ip, MemOperand(pc, 0), cond); |
| 1291 | msr(fields, Operand(ip), cond); |
| 1292 | return; |
| 1293 | } |
| 1294 | instr = I | rotate_imm*B8 | immed_8; |
| 1295 | } else { |
| 1296 | ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed |
| 1297 | instr = src.rm_.code(); |
| 1298 | } |
| 1299 | emit(cond | instr | B24 | B21 | fields | 15*B12); |
| 1300 | } |
| 1301 | |
| 1302 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1303 | // Load/Store instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1304 | void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1305 | if (dst.is(pc)) { |
| 1306 | WriteRecordedPositions(); |
| 1307 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1308 | addrmod2(cond | B26 | L, dst, src); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1309 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1310 | // Eliminate pattern: push(ry), pop(rx) |
| 1311 | // str(ry, MemOperand(sp, 4, NegPreIndex), al) |
| 1312 | // ldr(rx, MemOperand(sp, 4, PostIndex), al) |
| 1313 | // Both instructions can be eliminated if ry = rx. |
| 1314 | // If ry != rx, a register copy from ry to rx is inserted |
| 1315 | // after eliminating the push and the pop instructions. |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 1316 | if (can_peephole_optimize(2)) { |
| 1317 | Instr push_instr = instr_at(pc_ - 2 * kInstrSize); |
| 1318 | Instr pop_instr = instr_at(pc_ - 1 * kInstrSize); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1319 | |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 1320 | if (IsPush(push_instr) && IsPop(pop_instr)) { |
| 1321 | if ((pop_instr & kRdMask) != (push_instr & kRdMask)) { |
| 1322 | // For consecutive push and pop on different registers, |
| 1323 | // we delete both the push & pop and insert a register move. |
| 1324 | // push ry, pop rx --> mov rx, ry |
| 1325 | Register reg_pushed, reg_popped; |
| 1326 | reg_pushed = GetRd(push_instr); |
| 1327 | reg_popped = GetRd(pop_instr); |
| 1328 | pc_ -= 2 * kInstrSize; |
| 1329 | // Insert a mov instruction, which is better than a pair of push & pop |
| 1330 | mov(reg_popped, reg_pushed); |
| 1331 | if (FLAG_print_peephole_optimization) { |
| 1332 | PrintF("%x push/pop (diff reg) replaced by a reg move\n", |
| 1333 | pc_offset()); |
| 1334 | } |
| 1335 | } else { |
| 1336 | // For consecutive push and pop on the same register, |
| 1337 | // both the push and the pop can be deleted. |
| 1338 | pc_ -= 2 * kInstrSize; |
| 1339 | if (FLAG_print_peephole_optimization) { |
| 1340 | PrintF("%x push/pop (same reg) eliminated\n", pc_offset()); |
| 1341 | } |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | if (can_peephole_optimize(2)) { |
| 1347 | Instr str_instr = instr_at(pc_ - 2 * kInstrSize); |
| 1348 | Instr ldr_instr = instr_at(pc_ - 1 * kInstrSize); |
| 1349 | |
| 1350 | if ((IsStrRegFpOffset(str_instr) && |
| 1351 | IsLdrRegFpOffset(ldr_instr)) || |
| 1352 | (IsStrRegFpNegOffset(str_instr) && |
| 1353 | IsLdrRegFpNegOffset(ldr_instr))) { |
| 1354 | if ((ldr_instr & kLdrStrInstrArgumentMask) == |
| 1355 | (str_instr & kLdrStrInstrArgumentMask)) { |
| 1356 | // Pattern: Ldr/str same fp+offset, same register. |
| 1357 | // |
| 1358 | // The following: |
| 1359 | // str rx, [fp, #-12] |
| 1360 | // ldr rx, [fp, #-12] |
| 1361 | // |
| 1362 | // Becomes: |
| 1363 | // str rx, [fp, #-12] |
| 1364 | |
| 1365 | pc_ -= 1 * kInstrSize; |
| 1366 | if (FLAG_print_peephole_optimization) { |
| 1367 | PrintF("%x str/ldr (fp + same offset), same reg\n", pc_offset()); |
| 1368 | } |
| 1369 | } else if ((ldr_instr & kLdrStrOffsetMask) == |
| 1370 | (str_instr & kLdrStrOffsetMask)) { |
| 1371 | // Pattern: Ldr/str same fp+offset, different register. |
| 1372 | // |
| 1373 | // The following: |
| 1374 | // str rx, [fp, #-12] |
| 1375 | // ldr ry, [fp, #-12] |
| 1376 | // |
| 1377 | // Becomes: |
| 1378 | // str rx, [fp, #-12] |
| 1379 | // mov ry, rx |
| 1380 | |
| 1381 | Register reg_stored, reg_loaded; |
| 1382 | reg_stored = GetRd(str_instr); |
| 1383 | reg_loaded = GetRd(ldr_instr); |
| 1384 | pc_ -= 1 * kInstrSize; |
| 1385 | // Insert a mov instruction, which is better than ldr. |
| 1386 | mov(reg_loaded, reg_stored); |
| 1387 | if (FLAG_print_peephole_optimization) { |
| 1388 | PrintF("%x str/ldr (fp + same offset), diff reg \n", pc_offset()); |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | if (can_peephole_optimize(3)) { |
| 1395 | Instr mem_write_instr = instr_at(pc_ - 3 * kInstrSize); |
| 1396 | Instr ldr_instr = instr_at(pc_ - 2 * kInstrSize); |
| 1397 | Instr mem_read_instr = instr_at(pc_ - 1 * kInstrSize); |
| 1398 | if (IsPush(mem_write_instr) && |
| 1399 | IsPop(mem_read_instr)) { |
| 1400 | if ((IsLdrRegFpOffset(ldr_instr) || |
| 1401 | IsLdrRegFpNegOffset(ldr_instr))) { |
| 1402 | if ((mem_write_instr & kRdMask) == |
| 1403 | (mem_read_instr & kRdMask)) { |
| 1404 | // Pattern: push & pop from/to same register, |
| 1405 | // with a fp+offset ldr in between |
| 1406 | // |
| 1407 | // The following: |
| 1408 | // str rx, [sp, #-4]! |
| 1409 | // ldr rz, [fp, #-24] |
| 1410 | // ldr rx, [sp], #+4 |
| 1411 | // |
| 1412 | // Becomes: |
| 1413 | // if(rx == rz) |
| 1414 | // delete all |
| 1415 | // else |
| 1416 | // ldr rz, [fp, #-24] |
| 1417 | |
| 1418 | if ((mem_write_instr & kRdMask) == (ldr_instr & kRdMask)) { |
| 1419 | pc_ -= 3 * kInstrSize; |
| 1420 | } else { |
| 1421 | pc_ -= 3 * kInstrSize; |
| 1422 | // Reinsert back the ldr rz. |
| 1423 | emit(ldr_instr); |
| 1424 | } |
| 1425 | if (FLAG_print_peephole_optimization) { |
| 1426 | PrintF("%x push/pop -dead ldr fp+offset in middle\n", pc_offset()); |
| 1427 | } |
| 1428 | } else { |
| 1429 | // Pattern: push & pop from/to different registers |
| 1430 | // with a fp+offset ldr in between |
| 1431 | // |
| 1432 | // The following: |
| 1433 | // str rx, [sp, #-4]! |
| 1434 | // ldr rz, [fp, #-24] |
| 1435 | // ldr ry, [sp], #+4 |
| 1436 | // |
| 1437 | // Becomes: |
| 1438 | // if(ry == rz) |
| 1439 | // mov ry, rx; |
| 1440 | // else if(rx != rz) |
| 1441 | // ldr rz, [fp, #-24] |
| 1442 | // mov ry, rx |
| 1443 | // else if((ry != rz) || (rx == rz)) becomes: |
| 1444 | // mov ry, rx |
| 1445 | // ldr rz, [fp, #-24] |
| 1446 | |
| 1447 | Register reg_pushed, reg_popped; |
| 1448 | if ((mem_read_instr & kRdMask) == (ldr_instr & kRdMask)) { |
| 1449 | reg_pushed = GetRd(mem_write_instr); |
| 1450 | reg_popped = GetRd(mem_read_instr); |
| 1451 | pc_ -= 3 * kInstrSize; |
| 1452 | mov(reg_popped, reg_pushed); |
| 1453 | } else if ((mem_write_instr & kRdMask) |
| 1454 | != (ldr_instr & kRdMask)) { |
| 1455 | reg_pushed = GetRd(mem_write_instr); |
| 1456 | reg_popped = GetRd(mem_read_instr); |
| 1457 | pc_ -= 3 * kInstrSize; |
| 1458 | emit(ldr_instr); |
| 1459 | mov(reg_popped, reg_pushed); |
| 1460 | } else if (((mem_read_instr & kRdMask) |
| 1461 | != (ldr_instr & kRdMask)) || |
| 1462 | ((mem_write_instr & kRdMask) |
| 1463 | == (ldr_instr & kRdMask)) ) { |
| 1464 | reg_pushed = GetRd(mem_write_instr); |
| 1465 | reg_popped = GetRd(mem_read_instr); |
| 1466 | pc_ -= 3 * kInstrSize; |
| 1467 | mov(reg_popped, reg_pushed); |
| 1468 | emit(ldr_instr); |
| 1469 | } |
| 1470 | if (FLAG_print_peephole_optimization) { |
| 1471 | PrintF("%x push/pop (ldr fp+off in middle)\n", pc_offset()); |
| 1472 | } |
| 1473 | } |
| 1474 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1475 | } |
| 1476 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | |
| 1480 | void Assembler::str(Register src, const MemOperand& dst, Condition cond) { |
| 1481 | addrmod2(cond | B26, src, dst); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1482 | |
| 1483 | // Eliminate pattern: pop(), push(r) |
| 1484 | // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al |
| 1485 | // -> str r, [sp, 0], al |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1486 | if (can_peephole_optimize(2) && |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1487 | // Pattern. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1488 | instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) && |
| 1489 | instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) { |
| 1490 | pc_ -= 2 * kInstrSize; |
| 1491 | emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1492 | if (FLAG_print_peephole_optimization) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1493 | PrintF("%x pop()/push(reg) eliminated\n", pc_offset()); |
| 1494 | } |
| 1495 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | |
| 1499 | void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) { |
| 1500 | addrmod2(cond | B26 | B | L, dst, src); |
| 1501 | } |
| 1502 | |
| 1503 | |
| 1504 | void Assembler::strb(Register src, const MemOperand& dst, Condition cond) { |
| 1505 | addrmod2(cond | B26 | B, src, dst); |
| 1506 | } |
| 1507 | |
| 1508 | |
| 1509 | void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) { |
| 1510 | addrmod3(cond | L | B7 | H | B4, dst, src); |
| 1511 | } |
| 1512 | |
| 1513 | |
| 1514 | void Assembler::strh(Register src, const MemOperand& dst, Condition cond) { |
| 1515 | addrmod3(cond | B7 | H | B4, src, dst); |
| 1516 | } |
| 1517 | |
| 1518 | |
| 1519 | void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) { |
| 1520 | addrmod3(cond | L | B7 | S6 | B4, dst, src); |
| 1521 | } |
| 1522 | |
| 1523 | |
| 1524 | void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) { |
| 1525 | addrmod3(cond | L | B7 | S6 | H | B4, dst, src); |
| 1526 | } |
| 1527 | |
| 1528 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 1529 | void Assembler::ldrd(Register dst1, Register dst2, |
| 1530 | const MemOperand& src, Condition cond) { |
| 1531 | ASSERT(CpuFeatures::IsEnabled(ARMv7)); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1532 | ASSERT(src.rm().is(no_reg)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 1533 | ASSERT(!dst1.is(lr)); // r14. |
| 1534 | ASSERT_EQ(0, dst1.code() % 2); |
| 1535 | ASSERT_EQ(dst1.code() + 1, dst2.code()); |
| 1536 | addrmod3(cond | B7 | B6 | B4, dst1, src); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 1540 | void Assembler::strd(Register src1, Register src2, |
| 1541 | const MemOperand& dst, Condition cond) { |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1542 | ASSERT(dst.rm().is(no_reg)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 1543 | ASSERT(!src1.is(lr)); // r14. |
| 1544 | ASSERT_EQ(0, src1.code() % 2); |
| 1545 | ASSERT_EQ(src1.code() + 1, src2.code()); |
| 1546 | ASSERT(CpuFeatures::IsEnabled(ARMv7)); |
| 1547 | addrmod3(cond | B7 | B6 | B5 | B4, src1, dst); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1550 | // Load/Store multiple instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1551 | void Assembler::ldm(BlockAddrMode am, |
| 1552 | Register base, |
| 1553 | RegList dst, |
| 1554 | Condition cond) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1555 | // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1556 | ASSERT(base.is(sp) || (dst & sp.bit()) == 0); |
| 1557 | |
| 1558 | addrmod4(cond | B27 | am | L, base, dst); |
| 1559 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1560 | // Emit the constant pool after a function return implemented by ldm ..{..pc}. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1561 | if (cond == al && (dst & pc.bit()) != 0) { |
| 1562 | // There is a slight chance that the ldm instruction was actually a call, |
| 1563 | // in which case it would be wrong to return into the constant pool; we |
| 1564 | // recognize this case by checking if the emission of the pool was blocked |
| 1565 | // at the pc of the ldm instruction by a mov lr, pc instruction; if this is |
| 1566 | // the case, we emit a jump over the pool. |
| 1567 | CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize); |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | |
| 1572 | void Assembler::stm(BlockAddrMode am, |
| 1573 | Register base, |
| 1574 | RegList src, |
| 1575 | Condition cond) { |
| 1576 | addrmod4(cond | B27 | am, base, src); |
| 1577 | } |
| 1578 | |
| 1579 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1580 | // Exception-generating instructions and debugging support. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1581 | void Assembler::stop(const char* msg) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1582 | #ifndef __arm__ |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1583 | // The simulator handles these special instructions and stops execution. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1584 | emit(15 << 28 | ((intptr_t) msg)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1585 | #else // def __arm__ |
| 1586 | #ifdef CAN_USE_ARMV5_INSTRUCTIONS |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1587 | bkpt(0); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1588 | #else // ndef CAN_USE_ARMV5_INSTRUCTIONS |
| 1589 | swi(0x9f0001); |
| 1590 | #endif // ndef CAN_USE_ARMV5_INSTRUCTIONS |
| 1591 | #endif // def __arm__ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | |
| 1595 | void Assembler::bkpt(uint32_t imm16) { // v5 and above |
| 1596 | ASSERT(is_uint16(imm16)); |
| 1597 | emit(al | B24 | B21 | (imm16 >> 4)*B8 | 7*B4 | (imm16 & 0xf)); |
| 1598 | } |
| 1599 | |
| 1600 | |
| 1601 | void Assembler::swi(uint32_t imm24, Condition cond) { |
| 1602 | ASSERT(is_uint24(imm24)); |
| 1603 | emit(cond | 15*B24 | imm24); |
| 1604 | } |
| 1605 | |
| 1606 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1607 | // Coprocessor instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1608 | void Assembler::cdp(Coprocessor coproc, |
| 1609 | int opcode_1, |
| 1610 | CRegister crd, |
| 1611 | CRegister crn, |
| 1612 | CRegister crm, |
| 1613 | int opcode_2, |
| 1614 | Condition cond) { |
| 1615 | ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2)); |
| 1616 | emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 | |
| 1617 | crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code()); |
| 1618 | } |
| 1619 | |
| 1620 | |
| 1621 | void Assembler::cdp2(Coprocessor coproc, |
| 1622 | int opcode_1, |
| 1623 | CRegister crd, |
| 1624 | CRegister crn, |
| 1625 | CRegister crm, |
| 1626 | int opcode_2) { // v5 and above |
| 1627 | cdp(coproc, opcode_1, crd, crn, crm, opcode_2, static_cast<Condition>(nv)); |
| 1628 | } |
| 1629 | |
| 1630 | |
| 1631 | void Assembler::mcr(Coprocessor coproc, |
| 1632 | int opcode_1, |
| 1633 | Register rd, |
| 1634 | CRegister crn, |
| 1635 | CRegister crm, |
| 1636 | int opcode_2, |
| 1637 | Condition cond) { |
| 1638 | ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2)); |
| 1639 | emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 | |
| 1640 | rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code()); |
| 1641 | } |
| 1642 | |
| 1643 | |
| 1644 | void Assembler::mcr2(Coprocessor coproc, |
| 1645 | int opcode_1, |
| 1646 | Register rd, |
| 1647 | CRegister crn, |
| 1648 | CRegister crm, |
| 1649 | int opcode_2) { // v5 and above |
| 1650 | mcr(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv)); |
| 1651 | } |
| 1652 | |
| 1653 | |
| 1654 | void Assembler::mrc(Coprocessor coproc, |
| 1655 | int opcode_1, |
| 1656 | Register rd, |
| 1657 | CRegister crn, |
| 1658 | CRegister crm, |
| 1659 | int opcode_2, |
| 1660 | Condition cond) { |
| 1661 | ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2)); |
| 1662 | emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 | |
| 1663 | rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code()); |
| 1664 | } |
| 1665 | |
| 1666 | |
| 1667 | void Assembler::mrc2(Coprocessor coproc, |
| 1668 | int opcode_1, |
| 1669 | Register rd, |
| 1670 | CRegister crn, |
| 1671 | CRegister crm, |
| 1672 | int opcode_2) { // v5 and above |
| 1673 | mrc(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv)); |
| 1674 | } |
| 1675 | |
| 1676 | |
| 1677 | void Assembler::ldc(Coprocessor coproc, |
| 1678 | CRegister crd, |
| 1679 | const MemOperand& src, |
| 1680 | LFlag l, |
| 1681 | Condition cond) { |
| 1682 | addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src); |
| 1683 | } |
| 1684 | |
| 1685 | |
| 1686 | void Assembler::ldc(Coprocessor coproc, |
| 1687 | CRegister crd, |
| 1688 | Register rn, |
| 1689 | int option, |
| 1690 | LFlag l, |
| 1691 | Condition cond) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1692 | // Unindexed addressing. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1693 | ASSERT(is_uint8(option)); |
| 1694 | emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 | |
| 1695 | coproc*B8 | (option & 255)); |
| 1696 | } |
| 1697 | |
| 1698 | |
| 1699 | void Assembler::ldc2(Coprocessor coproc, |
| 1700 | CRegister crd, |
| 1701 | const MemOperand& src, |
| 1702 | LFlag l) { // v5 and above |
| 1703 | ldc(coproc, crd, src, l, static_cast<Condition>(nv)); |
| 1704 | } |
| 1705 | |
| 1706 | |
| 1707 | void Assembler::ldc2(Coprocessor coproc, |
| 1708 | CRegister crd, |
| 1709 | Register rn, |
| 1710 | int option, |
| 1711 | LFlag l) { // v5 and above |
| 1712 | ldc(coproc, crd, rn, option, l, static_cast<Condition>(nv)); |
| 1713 | } |
| 1714 | |
| 1715 | |
| 1716 | void Assembler::stc(Coprocessor coproc, |
| 1717 | CRegister crd, |
| 1718 | const MemOperand& dst, |
| 1719 | LFlag l, |
| 1720 | Condition cond) { |
| 1721 | addrmod5(cond | B27 | B26 | l | coproc*B8, crd, dst); |
| 1722 | } |
| 1723 | |
| 1724 | |
| 1725 | void Assembler::stc(Coprocessor coproc, |
| 1726 | CRegister crd, |
| 1727 | Register rn, |
| 1728 | int option, |
| 1729 | LFlag l, |
| 1730 | Condition cond) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1731 | // Unindexed addressing. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1732 | ASSERT(is_uint8(option)); |
| 1733 | emit(cond | B27 | B26 | U | l | rn.code()*B16 | crd.code()*B12 | |
| 1734 | coproc*B8 | (option & 255)); |
| 1735 | } |
| 1736 | |
| 1737 | |
| 1738 | void Assembler::stc2(Coprocessor |
| 1739 | coproc, CRegister crd, |
| 1740 | const MemOperand& dst, |
| 1741 | LFlag l) { // v5 and above |
| 1742 | stc(coproc, crd, dst, l, static_cast<Condition>(nv)); |
| 1743 | } |
| 1744 | |
| 1745 | |
| 1746 | void Assembler::stc2(Coprocessor coproc, |
| 1747 | CRegister crd, |
| 1748 | Register rn, |
| 1749 | int option, |
| 1750 | LFlag l) { // v5 and above |
| 1751 | stc(coproc, crd, rn, option, l, static_cast<Condition>(nv)); |
| 1752 | } |
| 1753 | |
| 1754 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1755 | // Support for VFP. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1756 | void Assembler::vldr(const DwVfpRegister dst, |
| 1757 | const Register base, |
| 1758 | int offset, |
| 1759 | const Condition cond) { |
| 1760 | // Ddst = MEM(Rbase + offset). |
| 1761 | // Instruction details available in ARM DDI 0406A, A8-628. |
| 1762 | // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) | |
| 1763 | // Vdst(15-12) | 1011(11-8) | offset |
| 1764 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1765 | ASSERT(offset % 4 == 0); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1766 | ASSERT((offset / 4) < 256); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1767 | emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 | |
| 1768 | 0xB*B8 | ((offset / 4) & 255)); |
| 1769 | } |
| 1770 | |
| 1771 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1772 | void Assembler::vldr(const SwVfpRegister dst, |
| 1773 | const Register base, |
| 1774 | int offset, |
| 1775 | const Condition cond) { |
| 1776 | // Sdst = MEM(Rbase + offset). |
| 1777 | // Instruction details available in ARM DDI 0406A, A8-628. |
| 1778 | // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) | |
| 1779 | // Vdst(15-12) | 1010(11-8) | offset |
| 1780 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1781 | ASSERT(offset % 4 == 0); |
| 1782 | ASSERT((offset / 4) < 256); |
| 1783 | emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 | |
| 1784 | 0xA*B8 | ((offset / 4) & 255)); |
| 1785 | } |
| 1786 | |
| 1787 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1788 | void Assembler::vstr(const DwVfpRegister src, |
| 1789 | const Register base, |
| 1790 | int offset, |
| 1791 | const Condition cond) { |
| 1792 | // MEM(Rbase + offset) = Dsrc. |
| 1793 | // Instruction details available in ARM DDI 0406A, A8-786. |
| 1794 | // cond(31-28) | 1101(27-24)| 1000(23-20) | | Rbase(19-16) | |
| 1795 | // Vsrc(15-12) | 1011(11-8) | (offset/4) |
| 1796 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1797 | ASSERT(offset % 4 == 0); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1798 | ASSERT((offset / 4) < 256); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1799 | emit(cond | 0xD8*B20 | base.code()*B16 | src.code()*B12 | |
| 1800 | 0xB*B8 | ((offset / 4) & 255)); |
| 1801 | } |
| 1802 | |
| 1803 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1804 | void Assembler::vmov(const DwVfpRegister dst, |
| 1805 | const Register src1, |
| 1806 | const Register src2, |
| 1807 | const Condition cond) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1808 | // Dm = <Rt,Rt2>. |
| 1809 | // Instruction details available in ARM DDI 0406A, A8-646. |
| 1810 | // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) | |
| 1811 | // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm |
| 1812 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1813 | ASSERT(!src1.is(pc) && !src2.is(pc)); |
| 1814 | emit(cond | 0xC*B24 | B22 | src2.code()*B16 | |
| 1815 | src1.code()*B12 | 0xB*B8 | B4 | dst.code()); |
| 1816 | } |
| 1817 | |
| 1818 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1819 | void Assembler::vmov(const Register dst1, |
| 1820 | const Register dst2, |
| 1821 | const DwVfpRegister src, |
| 1822 | const Condition cond) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1823 | // <Rt,Rt2> = Dm. |
| 1824 | // Instruction details available in ARM DDI 0406A, A8-646. |
| 1825 | // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) | |
| 1826 | // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm |
| 1827 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1828 | ASSERT(!dst1.is(pc) && !dst2.is(pc)); |
| 1829 | emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 | |
| 1830 | dst1.code()*B12 | 0xB*B8 | B4 | src.code()); |
| 1831 | } |
| 1832 | |
| 1833 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1834 | void Assembler::vmov(const SwVfpRegister dst, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1835 | const Register src, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1836 | const Condition cond) { |
| 1837 | // Sn = Rt. |
| 1838 | // Instruction details available in ARM DDI 0406A, A8-642. |
| 1839 | // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) | |
| 1840 | // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0) |
| 1841 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1842 | ASSERT(!src.is(pc)); |
| 1843 | emit(cond | 0xE*B24 | (dst.code() >> 1)*B16 | |
| 1844 | src.code()*B12 | 0xA*B8 | (0x1 & dst.code())*B7 | B4); |
| 1845 | } |
| 1846 | |
| 1847 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1848 | void Assembler::vmov(const Register dst, |
| 1849 | const SwVfpRegister src, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1850 | const Condition cond) { |
| 1851 | // Rt = Sn. |
| 1852 | // Instruction details available in ARM DDI 0406A, A8-642. |
| 1853 | // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) | |
| 1854 | // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0) |
| 1855 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1856 | ASSERT(!dst.is(pc)); |
| 1857 | emit(cond | 0xE*B24 | B20 | (src.code() >> 1)*B16 | |
| 1858 | dst.code()*B12 | 0xA*B8 | (0x1 & src.code())*B7 | B4); |
| 1859 | } |
| 1860 | |
| 1861 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1862 | // Type of data to read from or write to VFP register. |
| 1863 | // Used as specifier in generic vcvt instruction. |
| 1864 | enum VFPType { S32, U32, F32, F64 }; |
| 1865 | |
| 1866 | |
| 1867 | static bool IsSignedVFPType(VFPType type) { |
| 1868 | switch (type) { |
| 1869 | case S32: |
| 1870 | return true; |
| 1871 | case U32: |
| 1872 | return false; |
| 1873 | default: |
| 1874 | UNREACHABLE(); |
| 1875 | return false; |
| 1876 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1880 | static bool IsIntegerVFPType(VFPType type) { |
| 1881 | switch (type) { |
| 1882 | case S32: |
| 1883 | case U32: |
| 1884 | return true; |
| 1885 | case F32: |
| 1886 | case F64: |
| 1887 | return false; |
| 1888 | default: |
| 1889 | UNREACHABLE(); |
| 1890 | return false; |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | |
| 1895 | static bool IsDoubleVFPType(VFPType type) { |
| 1896 | switch (type) { |
| 1897 | case F32: |
| 1898 | return false; |
| 1899 | case F64: |
| 1900 | return true; |
| 1901 | default: |
| 1902 | UNREACHABLE(); |
| 1903 | return false; |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | |
| 1908 | // Depending on split_last_bit split binary representation of reg_code into Vm:M |
| 1909 | // or M:Vm form (where M is single bit). |
| 1910 | static void SplitRegCode(bool split_last_bit, |
| 1911 | int reg_code, |
| 1912 | int* vm, |
| 1913 | int* m) { |
| 1914 | if (split_last_bit) { |
| 1915 | *m = reg_code & 0x1; |
| 1916 | *vm = reg_code >> 1; |
| 1917 | } else { |
| 1918 | *m = (reg_code & 0x10) >> 4; |
| 1919 | *vm = reg_code & 0x0F; |
| 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | |
| 1924 | // Encode vcvt.src_type.dst_type instruction. |
| 1925 | static Instr EncodeVCVT(const VFPType dst_type, |
| 1926 | const int dst_code, |
| 1927 | const VFPType src_type, |
| 1928 | const int src_code, |
| 1929 | const Condition cond) { |
| 1930 | if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) { |
| 1931 | // Conversion between IEEE floating point and 32-bit integer. |
| 1932 | // Instruction details available in ARM DDI 0406B, A8.6.295. |
| 1933 | // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) | |
| 1934 | // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0) |
| 1935 | ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type)); |
| 1936 | |
| 1937 | int sz, opc2, D, Vd, M, Vm, op; |
| 1938 | |
| 1939 | if (IsIntegerVFPType(dst_type)) { |
| 1940 | opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4; |
| 1941 | sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0; |
| 1942 | op = 1; // round towards zero |
| 1943 | SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M); |
| 1944 | SplitRegCode(true, dst_code, &Vd, &D); |
| 1945 | } else { |
| 1946 | ASSERT(IsIntegerVFPType(src_type)); |
| 1947 | |
| 1948 | opc2 = 0x0; |
| 1949 | sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0; |
| 1950 | op = IsSignedVFPType(src_type) ? 0x1 : 0x0; |
| 1951 | SplitRegCode(true, src_code, &Vm, &M); |
| 1952 | SplitRegCode(!IsDoubleVFPType(dst_type), dst_code, &Vd, &D); |
| 1953 | } |
| 1954 | |
| 1955 | return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 | |
| 1956 | Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm); |
| 1957 | } else { |
| 1958 | // Conversion between IEEE double and single precision. |
| 1959 | // Instruction details available in ARM DDI 0406B, A8.6.298. |
| 1960 | // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) | |
| 1961 | // Vd(15-12) | 101(11-9) | sz(8) | 1(7) | 1(6) | M(5) | 0(4) | Vm(3-0) |
| 1962 | int sz, D, Vd, M, Vm; |
| 1963 | |
| 1964 | ASSERT(IsDoubleVFPType(dst_type) != IsDoubleVFPType(src_type)); |
| 1965 | sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0; |
| 1966 | SplitRegCode(IsDoubleVFPType(src_type), dst_code, &Vd, &D); |
| 1967 | SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M); |
| 1968 | |
| 1969 | return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 | |
| 1970 | Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm); |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | |
| 1975 | void Assembler::vcvt_f64_s32(const DwVfpRegister dst, |
| 1976 | const SwVfpRegister src, |
| 1977 | const Condition cond) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1978 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1979 | emit(EncodeVCVT(F64, dst.code(), S32, src.code(), cond)); |
| 1980 | } |
| 1981 | |
| 1982 | |
| 1983 | void Assembler::vcvt_f32_s32(const SwVfpRegister dst, |
| 1984 | const SwVfpRegister src, |
| 1985 | const Condition cond) { |
| 1986 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1987 | emit(EncodeVCVT(F32, dst.code(), S32, src.code(), cond)); |
| 1988 | } |
| 1989 | |
| 1990 | |
| 1991 | void Assembler::vcvt_f64_u32(const DwVfpRegister dst, |
| 1992 | const SwVfpRegister src, |
| 1993 | const Condition cond) { |
| 1994 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1995 | emit(EncodeVCVT(F64, dst.code(), U32, src.code(), cond)); |
| 1996 | } |
| 1997 | |
| 1998 | |
| 1999 | void Assembler::vcvt_s32_f64(const SwVfpRegister dst, |
| 2000 | const DwVfpRegister src, |
| 2001 | const Condition cond) { |
| 2002 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2003 | emit(EncodeVCVT(S32, dst.code(), F64, src.code(), cond)); |
| 2004 | } |
| 2005 | |
| 2006 | |
| 2007 | void Assembler::vcvt_u32_f64(const SwVfpRegister dst, |
| 2008 | const DwVfpRegister src, |
| 2009 | const Condition cond) { |
| 2010 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2011 | emit(EncodeVCVT(U32, dst.code(), F64, src.code(), cond)); |
| 2012 | } |
| 2013 | |
| 2014 | |
| 2015 | void Assembler::vcvt_f64_f32(const DwVfpRegister dst, |
| 2016 | const SwVfpRegister src, |
| 2017 | const Condition cond) { |
| 2018 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2019 | emit(EncodeVCVT(F64, dst.code(), F32, src.code(), cond)); |
| 2020 | } |
| 2021 | |
| 2022 | |
| 2023 | void Assembler::vcvt_f32_f64(const SwVfpRegister dst, |
| 2024 | const DwVfpRegister src, |
| 2025 | const Condition cond) { |
| 2026 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2027 | emit(EncodeVCVT(F32, dst.code(), F64, src.code(), cond)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 2031 | void Assembler::vadd(const DwVfpRegister dst, |
| 2032 | const DwVfpRegister src1, |
| 2033 | const DwVfpRegister src2, |
| 2034 | const Condition cond) { |
| 2035 | // Dd = vadd(Dn, Dm) double precision floating point addition. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2036 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 2037 | // Instruction details available in ARM DDI 0406A, A8-536. |
| 2038 | // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) | |
| 2039 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0) |
| 2040 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2041 | emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 | |
| 2042 | dst.code()*B12 | 0x5*B9 | B8 | src2.code()); |
| 2043 | } |
| 2044 | |
| 2045 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 2046 | void Assembler::vsub(const DwVfpRegister dst, |
| 2047 | const DwVfpRegister src1, |
| 2048 | const DwVfpRegister src2, |
| 2049 | const Condition cond) { |
| 2050 | // Dd = vsub(Dn, Dm) double precision floating point subtraction. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2051 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 2052 | // Instruction details available in ARM DDI 0406A, A8-784. |
| 2053 | // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) | |
| 2054 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 1(6) | M=?(5) | 0(4) | Vm(3-0) |
| 2055 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2056 | emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 | |
| 2057 | dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code()); |
| 2058 | } |
| 2059 | |
| 2060 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 2061 | void Assembler::vmul(const DwVfpRegister dst, |
| 2062 | const DwVfpRegister src1, |
| 2063 | const DwVfpRegister src2, |
| 2064 | const Condition cond) { |
| 2065 | // Dd = vmul(Dn, Dm) double precision floating point multiplication. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2066 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 2067 | // Instruction details available in ARM DDI 0406A, A8-784. |
| 2068 | // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) | |
| 2069 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0) |
| 2070 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2071 | emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 | |
| 2072 | dst.code()*B12 | 0x5*B9 | B8 | src2.code()); |
| 2073 | } |
| 2074 | |
| 2075 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 2076 | void Assembler::vdiv(const DwVfpRegister dst, |
| 2077 | const DwVfpRegister src1, |
| 2078 | const DwVfpRegister src2, |
| 2079 | const Condition cond) { |
| 2080 | // Dd = vdiv(Dn, Dm) double precision floating point division. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2081 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 2082 | // Instruction details available in ARM DDI 0406A, A8-584. |
| 2083 | // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) | |
| 2084 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=? | 0(6) | M=?(5) | 0(4) | Vm(3-0) |
| 2085 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2086 | emit(cond | 0xE*B24 | B23 | src1.code()*B16 | |
| 2087 | dst.code()*B12 | 0x5*B9 | B8 | src2.code()); |
| 2088 | } |
| 2089 | |
| 2090 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 2091 | void Assembler::vcmp(const DwVfpRegister src1, |
| 2092 | const DwVfpRegister src2, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2093 | const SBit s, |
| 2094 | const Condition cond) { |
| 2095 | // vcmp(Dd, Dm) double precision floating point comparison. |
| 2096 | // Instruction details available in ARM DDI 0406A, A8-570. |
| 2097 | // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) | |
| 2098 | // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=? | 1(6) | M(5)=? | 0(4) | Vm(3-0) |
| 2099 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2100 | emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 | |
| 2101 | src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code()); |
| 2102 | } |
| 2103 | |
| 2104 | |
| 2105 | void Assembler::vmrs(Register dst, Condition cond) { |
| 2106 | // Instruction details available in ARM DDI 0406A, A8-652. |
| 2107 | // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) | |
| 2108 | // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0) |
| 2109 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2110 | emit(cond | 0xE*B24 | 0xF*B20 | B16 | |
| 2111 | dst.code()*B12 | 0xA*B8 | B4); |
| 2112 | } |
| 2113 | |
| 2114 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame^] | 2115 | |
| 2116 | void Assembler::vsqrt(const DwVfpRegister dst, |
| 2117 | const DwVfpRegister src, |
| 2118 | const Condition cond) { |
| 2119 | // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0001 (19-16) | |
| 2120 | // Vd(15-12) | 101(11-9) | sz(8)=1 | 11 (7-6) | M(5)=? | 0(4) | Vm(3-0) |
| 2121 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 2122 | emit(cond | 0xE*B24 | B23 | 0x3*B20 | B16 | |
| 2123 | dst.code()*B12 | 0x5*B9 | B8 | 3*B6 | src.code()); |
| 2124 | } |
| 2125 | |
| 2126 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2127 | // Pseudo instructions. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2128 | void Assembler::nop(int type) { |
| 2129 | // This is mov rx, rx. |
| 2130 | ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop. |
| 2131 | emit(al | 13*B21 | type*B12 | type); |
| 2132 | } |
| 2133 | |
| 2134 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2135 | bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) { |
| 2136 | uint32_t dummy1; |
| 2137 | uint32_t dummy2; |
| 2138 | return fits_shifter(imm32, &dummy1, &dummy2, NULL); |
| 2139 | } |
| 2140 | |
| 2141 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 2142 | void Assembler::BlockConstPoolFor(int instructions) { |
| 2143 | BlockConstPoolBefore(pc_offset() + instructions * kInstrSize); |
| 2144 | } |
| 2145 | |
| 2146 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2147 | // Debugging. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 2148 | void Assembler::RecordJSReturn() { |
| 2149 | WriteRecordedPositions(); |
| 2150 | CheckBuffer(); |
| 2151 | RecordRelocInfo(RelocInfo::JS_RETURN); |
| 2152 | } |
| 2153 | |
| 2154 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2155 | void Assembler::RecordDebugBreakSlot() { |
| 2156 | WriteRecordedPositions(); |
| 2157 | CheckBuffer(); |
| 2158 | RecordRelocInfo(RelocInfo::DEBUG_BREAK_SLOT); |
| 2159 | } |
| 2160 | |
| 2161 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2162 | void Assembler::RecordComment(const char* msg) { |
| 2163 | if (FLAG_debug_code) { |
| 2164 | CheckBuffer(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2165 | RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2166 | } |
| 2167 | } |
| 2168 | |
| 2169 | |
| 2170 | void Assembler::RecordPosition(int pos) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2171 | if (pos == RelocInfo::kNoPosition) return; |
| 2172 | ASSERT(pos >= 0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2173 | current_position_ = pos; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | |
| 2177 | void Assembler::RecordStatementPosition(int pos) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2178 | if (pos == RelocInfo::kNoPosition) return; |
| 2179 | ASSERT(pos >= 0); |
| 2180 | current_statement_position_ = pos; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2184 | bool Assembler::WriteRecordedPositions() { |
| 2185 | bool written = false; |
| 2186 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2187 | // Write the statement position if it is different from what was written last |
| 2188 | // time. |
| 2189 | if (current_statement_position_ != written_statement_position_) { |
| 2190 | CheckBuffer(); |
| 2191 | RecordRelocInfo(RelocInfo::STATEMENT_POSITION, current_statement_position_); |
| 2192 | written_statement_position_ = current_statement_position_; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2193 | written = true; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | // Write the position if it is different from what was written last time and |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2197 | // also different from the written statement position. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2198 | if (current_position_ != written_position_ && |
| 2199 | current_position_ != written_statement_position_) { |
| 2200 | CheckBuffer(); |
| 2201 | RecordRelocInfo(RelocInfo::POSITION, current_position_); |
| 2202 | written_position_ = current_position_; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2203 | written = true; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2204 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2205 | |
| 2206 | // Return whether something was written. |
| 2207 | return written; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | |
| 2211 | void Assembler::GrowBuffer() { |
| 2212 | if (!own_buffer_) FATAL("external code buffer is too small"); |
| 2213 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2214 | // Compute new buffer size. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2215 | CodeDesc desc; // the new buffer |
| 2216 | if (buffer_size_ < 4*KB) { |
| 2217 | desc.buffer_size = 4*KB; |
| 2218 | } else if (buffer_size_ < 1*MB) { |
| 2219 | desc.buffer_size = 2*buffer_size_; |
| 2220 | } else { |
| 2221 | desc.buffer_size = buffer_size_ + 1*MB; |
| 2222 | } |
| 2223 | CHECK_GT(desc.buffer_size, 0); // no overflow |
| 2224 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2225 | // Setup new buffer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2226 | desc.buffer = NewArray<byte>(desc.buffer_size); |
| 2227 | |
| 2228 | desc.instr_size = pc_offset(); |
| 2229 | desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos(); |
| 2230 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2231 | // Copy the data. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2232 | int pc_delta = desc.buffer - buffer_; |
| 2233 | int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_); |
| 2234 | memmove(desc.buffer, buffer_, desc.instr_size); |
| 2235 | memmove(reloc_info_writer.pos() + rc_delta, |
| 2236 | reloc_info_writer.pos(), desc.reloc_size); |
| 2237 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2238 | // Switch buffers. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2239 | DeleteArray(buffer_); |
| 2240 | buffer_ = desc.buffer; |
| 2241 | buffer_size_ = desc.buffer_size; |
| 2242 | pc_ += pc_delta; |
| 2243 | reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta, |
| 2244 | reloc_info_writer.last_pc() + pc_delta); |
| 2245 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2246 | // None of our relocation types are pc relative pointing outside the code |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2247 | // buffer nor pc absolute pointing inside the code buffer, so there is no need |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2248 | // to relocate any emitted relocation entries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2249 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2250 | // Relocate pending relocation entries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2251 | for (int i = 0; i < num_prinfo_; i++) { |
| 2252 | RelocInfo& rinfo = prinfo_[i]; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2253 | ASSERT(rinfo.rmode() != RelocInfo::COMMENT && |
| 2254 | rinfo.rmode() != RelocInfo::POSITION); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 2255 | if (rinfo.rmode() != RelocInfo::JS_RETURN) { |
| 2256 | rinfo.set_pc(rinfo.pc() + pc_delta); |
| 2257 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2262 | void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2263 | RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2264 | if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::DEBUG_BREAK_SLOT) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2265 | // Adjust code for new modes. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 2266 | ASSERT(RelocInfo::IsDebugBreakSlot(rmode) |
| 2267 | || RelocInfo::IsJSReturn(rmode) |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 2268 | || RelocInfo::IsComment(rmode) |
| 2269 | || RelocInfo::IsPosition(rmode)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2270 | // These modes do not need an entry in the constant pool. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2271 | } else { |
| 2272 | ASSERT(num_prinfo_ < kMaxNumPRInfo); |
| 2273 | prinfo_[num_prinfo_++] = rinfo; |
| 2274 | // Make sure the constant pool is not emitted in place of the next |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2275 | // instruction for which we just recorded relocation info. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2276 | BlockConstPoolBefore(pc_offset() + kInstrSize); |
| 2277 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2278 | if (rinfo.rmode() != RelocInfo::NONE) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2279 | // Don't record external references unless the heap will be serialized. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2280 | if (rmode == RelocInfo::EXTERNAL_REFERENCE) { |
| 2281 | #ifdef DEBUG |
| 2282 | if (!Serializer::enabled()) { |
| 2283 | Serializer::TooLateToEnableNow(); |
| 2284 | } |
| 2285 | #endif |
| 2286 | if (!Serializer::enabled() && !FLAG_debug_code) { |
| 2287 | return; |
| 2288 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2289 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2290 | ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here |
| 2291 | reloc_info_writer.Write(&rinfo); |
| 2292 | } |
| 2293 | } |
| 2294 | |
| 2295 | |
| 2296 | void Assembler::CheckConstPool(bool force_emit, bool require_jump) { |
| 2297 | // Calculate the offset of the next check. It will be overwritten |
| 2298 | // when a const pool is generated or when const pools are being |
| 2299 | // blocked for a specific range. |
| 2300 | next_buffer_check_ = pc_offset() + kCheckConstInterval; |
| 2301 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2302 | // There is nothing to do if there are no pending relocation info entries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2303 | if (num_prinfo_ == 0) return; |
| 2304 | |
| 2305 | // We emit a constant pool at regular intervals of about kDistBetweenPools |
| 2306 | // or when requested by parameter force_emit (e.g. after each function). |
| 2307 | // We prefer not to emit a jump unless the max distance is reached or if we |
| 2308 | // are running low on slots, which can happen if a lot of constants are being |
| 2309 | // emitted (e.g. --debug-code and many static references). |
| 2310 | int dist = pc_offset() - last_const_pool_end_; |
| 2311 | if (!force_emit && dist < kMaxDistBetweenPools && |
| 2312 | (require_jump || dist < kDistBetweenPools) && |
| 2313 | // TODO(1236125): Cleanup the "magic" number below. We know that |
| 2314 | // the code generation will test every kCheckConstIntervalInst. |
| 2315 | // Thus we are safe as long as we generate less than 7 constant |
| 2316 | // entries per instruction. |
| 2317 | (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) { |
| 2318 | return; |
| 2319 | } |
| 2320 | |
| 2321 | // If we did not return by now, we need to emit the constant pool soon. |
| 2322 | |
| 2323 | // However, some small sequences of instructions must not be broken up by the |
| 2324 | // insertion of a constant pool; such sequences are protected by setting |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2325 | // either const_pool_blocked_nesting_ or no_const_pool_before_, which are |
| 2326 | // both checked here. Also, recursive calls to CheckConstPool are blocked by |
| 2327 | // no_const_pool_before_. |
| 2328 | if (const_pool_blocked_nesting_ > 0 || pc_offset() < no_const_pool_before_) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2329 | // Emission is currently blocked; make sure we try again as soon as |
| 2330 | // possible. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2331 | if (const_pool_blocked_nesting_ > 0) { |
| 2332 | next_buffer_check_ = pc_offset() + kInstrSize; |
| 2333 | } else { |
| 2334 | next_buffer_check_ = no_const_pool_before_; |
| 2335 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2336 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2337 | // Something is wrong if emission is forced and blocked at the same time. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2338 | ASSERT(!force_emit); |
| 2339 | return; |
| 2340 | } |
| 2341 | |
| 2342 | int jump_instr = require_jump ? kInstrSize : 0; |
| 2343 | |
| 2344 | // Check that the code buffer is large enough before emitting the constant |
| 2345 | // pool and relocation information (include the jump over the pool and the |
| 2346 | // constant pool marker). |
| 2347 | int max_needed_space = |
| 2348 | jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize); |
| 2349 | while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer(); |
| 2350 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2351 | // Block recursive calls to CheckConstPool. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2352 | BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize + |
| 2353 | num_prinfo_*kInstrSize); |
| 2354 | // Don't bother to check for the emit calls below. |
| 2355 | next_buffer_check_ = no_const_pool_before_; |
| 2356 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2357 | // Emit jump over constant pool if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2358 | Label after_pool; |
| 2359 | if (require_jump) b(&after_pool); |
| 2360 | |
| 2361 | RecordComment("[ Constant Pool"); |
| 2362 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2363 | // Put down constant pool marker "Undefined instruction" as specified by |
| 2364 | // A3.1 Instruction set encoding. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2365 | emit(0x03000000 | num_prinfo_); |
| 2366 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2367 | // Emit constant pool entries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2368 | for (int i = 0; i < num_prinfo_; i++) { |
| 2369 | RelocInfo& rinfo = prinfo_[i]; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2370 | ASSERT(rinfo.rmode() != RelocInfo::COMMENT && |
| 2371 | rinfo.rmode() != RelocInfo::POSITION && |
| 2372 | rinfo.rmode() != RelocInfo::STATEMENT_POSITION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2373 | Instr instr = instr_at(rinfo.pc()); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 2374 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2375 | // Instruction to patch must be a ldr/str [pc, #offset]. |
| 2376 | // P and U set, B and W clear, Rn == pc, offset12 still 0. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2377 | ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | Off12Mask)) == |
| 2378 | (2*B25 | P | U | pc.code()*B16)); |
| 2379 | int delta = pc_ - rinfo.pc() - 8; |
| 2380 | ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32 |
| 2381 | if (delta < 0) { |
| 2382 | instr &= ~U; |
| 2383 | delta = -delta; |
| 2384 | } |
| 2385 | ASSERT(is_uint12(delta)); |
| 2386 | instr_at_put(rinfo.pc(), instr + delta); |
| 2387 | emit(rinfo.data()); |
| 2388 | } |
| 2389 | num_prinfo_ = 0; |
| 2390 | last_const_pool_end_ = pc_offset(); |
| 2391 | |
| 2392 | RecordComment("]"); |
| 2393 | |
| 2394 | if (after_pool.is_linked()) { |
| 2395 | bind(&after_pool); |
| 2396 | } |
| 2397 | |
| 2398 | // Since a constant pool was just emitted, move the check offset forward by |
| 2399 | // the standard interval. |
| 2400 | next_buffer_check_ = pc_offset() + kCheckConstInterval; |
| 2401 | } |
| 2402 | |
| 2403 | |
| 2404 | } } // namespace v8::internal |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 2405 | |
| 2406 | #endif // V8_TARGET_ARCH_ARM |