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 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 39 | #include "arm/assembler-arm-inl.h" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 40 | #include "serialize.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 42 | namespace v8 { |
| 43 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 45 | // Safe default is no features. |
| 46 | unsigned CpuFeatures::supported_ = 0; |
| 47 | unsigned CpuFeatures::enabled_ = 0; |
| 48 | unsigned CpuFeatures::found_by_runtime_probing_ = 0; |
| 49 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 50 | |
| 51 | #ifdef __arm__ |
| 52 | static uint64_t CpuFeaturesImpliedByCompiler() { |
| 53 | uint64_t answer = 0; |
| 54 | #ifdef CAN_USE_ARMV7_INSTRUCTIONS |
| 55 | answer |= 1u << ARMv7; |
| 56 | #endif // def CAN_USE_ARMV7_INSTRUCTIONS |
| 57 | // If the compiler is allowed to use VFP then we can use VFP too in our code |
| 58 | // generation even when generating snapshots. This won't work for cross |
| 59 | // compilation. |
| 60 | #if defined(__VFP_FP__) && !defined(__SOFTFP__) |
| 61 | answer |= 1u << VFP3; |
| 62 | #endif // defined(__VFP_FP__) && !defined(__SOFTFP__) |
| 63 | #ifdef CAN_USE_VFP_INSTRUCTIONS |
| 64 | answer |= 1u << VFP3; |
| 65 | #endif // def CAN_USE_VFP_INSTRUCTIONS |
| 66 | return answer; |
| 67 | } |
| 68 | #endif // def __arm__ |
| 69 | |
| 70 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 71 | void CpuFeatures::Probe() { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 72 | #ifndef __arm__ |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 73 | // For the simulator=arm build, use VFP when FLAG_enable_vfp3 is enabled. |
| 74 | if (FLAG_enable_vfp3) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 75 | supported_ |= 1u << VFP3; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 76 | } |
| 77 | // For the simulator=arm build, use ARMv7 when FLAG_enable_armv7 is enabled |
| 78 | if (FLAG_enable_armv7) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 79 | supported_ |= 1u << ARMv7; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 80 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 81 | #else // def __arm__ |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 82 | if (Serializer::enabled()) { |
| 83 | supported_ |= OS::CpuFeaturesImpliedByPlatform(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 84 | supported_ |= CpuFeaturesImpliedByCompiler(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 85 | return; // No features if we might serialize. |
| 86 | } |
| 87 | |
| 88 | if (OS::ArmCpuHasFeature(VFP3)) { |
| 89 | // This implementation also sets the VFP flags if |
| 90 | // runtime detection of VFP returns true. |
| 91 | supported_ |= 1u << VFP3; |
| 92 | found_by_runtime_probing_ |= 1u << VFP3; |
| 93 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 94 | |
| 95 | if (OS::ArmCpuHasFeature(ARMv7)) { |
| 96 | supported_ |= 1u << ARMv7; |
| 97 | found_by_runtime_probing_ |= 1u << ARMv7; |
| 98 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 99 | #endif |
| 100 | } |
| 101 | |
| 102 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | // ----------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | // Implementation of RelocInfo |
| 105 | |
| 106 | const int RelocInfo::kApplyMask = 0; |
| 107 | |
| 108 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 109 | void RelocInfo::PatchCode(byte* instructions, int instruction_count) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 110 | // Patch the code at the current address with the supplied instructions. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 111 | Instr* pc = reinterpret_cast<Instr*>(pc_); |
| 112 | Instr* instr = reinterpret_cast<Instr*>(instructions); |
| 113 | for (int i = 0; i < instruction_count; i++) { |
| 114 | *(pc + i) = *(instr + i); |
| 115 | } |
| 116 | |
| 117 | // Indicate that code has changed. |
| 118 | CPU::FlushICache(pc_, instruction_count * Assembler::kInstrSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
| 122 | // 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] | 123 | // Additional guard instructions can be added if required. |
| 124 | void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 125 | // Patch the code at the current address with a call to the target. |
| 126 | UNIMPLEMENTED(); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | // ----------------------------------------------------------------------------- |
| 131 | // Implementation of Operand and MemOperand |
| 132 | // See assembler-arm-inl.h for inlined constructors |
| 133 | |
| 134 | Operand::Operand(Handle<Object> handle) { |
| 135 | rm_ = no_reg; |
| 136 | // Verify all Objects referred by code are NOT in new space. |
| 137 | Object* obj = *handle; |
| 138 | ASSERT(!Heap::InNewSpace(obj)); |
| 139 | if (obj->IsHeapObject()) { |
| 140 | imm32_ = reinterpret_cast<intptr_t>(handle.location()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 141 | rmode_ = RelocInfo::EMBEDDED_OBJECT; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | } else { |
| 143 | // no relocation needed |
| 144 | imm32_ = reinterpret_cast<intptr_t>(obj); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 145 | rmode_ = RelocInfo::NONE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | |
| 150 | Operand::Operand(Register rm, ShiftOp shift_op, int shift_imm) { |
| 151 | ASSERT(is_uint5(shift_imm)); |
| 152 | ASSERT(shift_op != ROR || shift_imm != 0); // use RRX if you mean it |
| 153 | rm_ = rm; |
| 154 | rs_ = no_reg; |
| 155 | shift_op_ = shift_op; |
| 156 | shift_imm_ = shift_imm & 31; |
| 157 | if (shift_op == RRX) { |
| 158 | // encoded as ROR with shift_imm == 0 |
| 159 | ASSERT(shift_imm == 0); |
| 160 | shift_op_ = ROR; |
| 161 | shift_imm_ = 0; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | Operand::Operand(Register rm, ShiftOp shift_op, Register rs) { |
| 167 | ASSERT(shift_op != RRX); |
| 168 | rm_ = rm; |
| 169 | rs_ = no_reg; |
| 170 | shift_op_ = shift_op; |
| 171 | rs_ = rs; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | MemOperand::MemOperand(Register rn, int32_t offset, AddrMode am) { |
| 176 | rn_ = rn; |
| 177 | rm_ = no_reg; |
| 178 | offset_ = offset; |
| 179 | am_ = am; |
| 180 | } |
| 181 | |
| 182 | MemOperand::MemOperand(Register rn, Register rm, AddrMode am) { |
| 183 | rn_ = rn; |
| 184 | rm_ = rm; |
| 185 | shift_op_ = LSL; |
| 186 | shift_imm_ = 0; |
| 187 | am_ = am; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | MemOperand::MemOperand(Register rn, Register rm, |
| 192 | ShiftOp shift_op, int shift_imm, AddrMode am) { |
| 193 | ASSERT(is_uint5(shift_imm)); |
| 194 | rn_ = rn; |
| 195 | rm_ = rm; |
| 196 | shift_op_ = shift_op; |
| 197 | shift_imm_ = shift_imm & 31; |
| 198 | am_ = am; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | // ----------------------------------------------------------------------------- |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 203 | // Implementation of Assembler. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 204 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 205 | // Instruction encoding bits. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 206 | enum { |
| 207 | H = 1 << 5, // halfword (or byte) |
| 208 | S6 = 1 << 6, // signed (or unsigned) |
| 209 | L = 1 << 20, // load (or store) |
| 210 | S = 1 << 20, // set condition code (or leave unchanged) |
| 211 | W = 1 << 21, // writeback base register (or leave unchanged) |
| 212 | A = 1 << 21, // accumulate in multiply instruction (or not) |
| 213 | B = 1 << 22, // unsigned byte (or word) |
| 214 | N = 1 << 22, // long (or short) |
| 215 | U = 1 << 23, // positive (or negative) offset/index |
| 216 | P = 1 << 24, // offset/pre-indexed addressing (or post-indexed addressing) |
| 217 | I = 1 << 25, // immediate shifter operand (or not) |
| 218 | |
| 219 | B4 = 1 << 4, |
| 220 | B5 = 1 << 5, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 221 | B6 = 1 << 6, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | B7 = 1 << 7, |
| 223 | B8 = 1 << 8, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 224 | B9 = 1 << 9, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 225 | B12 = 1 << 12, |
| 226 | B16 = 1 << 16, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 227 | B18 = 1 << 18, |
| 228 | B19 = 1 << 19, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 229 | B20 = 1 << 20, |
| 230 | B21 = 1 << 21, |
| 231 | B22 = 1 << 22, |
| 232 | B23 = 1 << 23, |
| 233 | B24 = 1 << 24, |
| 234 | B25 = 1 << 25, |
| 235 | B26 = 1 << 26, |
| 236 | B27 = 1 << 27, |
| 237 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 238 | // Instruction bit masks. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 239 | RdMask = 15 << 12, // in str instruction |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 240 | CondMask = 15 << 28, |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 241 | CoprocessorMask = 15 << 8, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | OpCodeMask = 15 << 21, // in data-processing instructions |
| 243 | Imm24Mask = (1 << 24) - 1, |
| 244 | Off12Mask = (1 << 12) - 1, |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 245 | // Reserved condition. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 246 | nv = 15 << 28 |
| 247 | }; |
| 248 | |
| 249 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 250 | // add(sp, sp, 4) instruction (aka Pop()) |
| 251 | static const Instr kPopInstruction = |
| 252 | al | 4 * B21 | 4 | LeaveCC | I | sp.code() * B16 | sp.code() * B12; |
| 253 | // str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r)) |
| 254 | // register r is not encoded. |
| 255 | static const Instr kPushRegPattern = |
| 256 | al | B26 | 4 | NegPreIndex | sp.code() * B16; |
| 257 | // ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r)) |
| 258 | // register r is not encoded. |
| 259 | static const Instr kPopRegPattern = |
| 260 | al | B26 | L | 4 | PostIndex | sp.code() * B16; |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 261 | // mov lr, pc |
| 262 | const Instr kMovLrPc = al | 13*B21 | pc.code() | lr.code() * B12; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 263 | // ldr rd, [pc, #offset] |
| 264 | const Instr kLdrPCMask = CondMask | 15 * B24 | 7 * B20 | 15 * B16; |
| 265 | const Instr kLdrPCPattern = al | 5 * B24 | L | pc.code() * B16; |
| 266 | // blxcc rm |
| 267 | const Instr kBlxRegMask = |
| 268 | 15 * B24 | 15 * B20 | 15 * B16 | 15 * B12 | 15 * B8 | 15 * B4; |
| 269 | const Instr kBlxRegPattern = |
| 270 | B24 | B21 | 15 * B16 | 15 * B12 | 15 * B8 | 3 * B4; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 271 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 272 | // Spare buffer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 273 | static const int kMinimalBufferSize = 4*KB; |
| 274 | static byte* spare_buffer_ = NULL; |
| 275 | |
| 276 | Assembler::Assembler(void* buffer, int buffer_size) { |
| 277 | if (buffer == NULL) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 278 | // Do our own buffer management. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | if (buffer_size <= kMinimalBufferSize) { |
| 280 | buffer_size = kMinimalBufferSize; |
| 281 | |
| 282 | if (spare_buffer_ != NULL) { |
| 283 | buffer = spare_buffer_; |
| 284 | spare_buffer_ = NULL; |
| 285 | } |
| 286 | } |
| 287 | if (buffer == NULL) { |
| 288 | buffer_ = NewArray<byte>(buffer_size); |
| 289 | } else { |
| 290 | buffer_ = static_cast<byte*>(buffer); |
| 291 | } |
| 292 | buffer_size_ = buffer_size; |
| 293 | own_buffer_ = true; |
| 294 | |
| 295 | } else { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 296 | // Use externally provided buffer instead. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 297 | ASSERT(buffer_size > 0); |
| 298 | buffer_ = static_cast<byte*>(buffer); |
| 299 | buffer_size_ = buffer_size; |
| 300 | own_buffer_ = false; |
| 301 | } |
| 302 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 303 | // Setup buffer pointers. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 304 | ASSERT(buffer_ != NULL); |
| 305 | pc_ = buffer_; |
| 306 | reloc_info_writer.Reposition(buffer_ + buffer_size, pc_); |
| 307 | num_prinfo_ = 0; |
| 308 | next_buffer_check_ = 0; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 309 | const_pool_blocked_nesting_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 310 | no_const_pool_before_ = 0; |
| 311 | last_const_pool_end_ = 0; |
| 312 | last_bound_pos_ = 0; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 313 | current_statement_position_ = RelocInfo::kNoPosition; |
| 314 | current_position_ = RelocInfo::kNoPosition; |
| 315 | written_statement_position_ = current_statement_position_; |
| 316 | written_position_ = current_position_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
| 320 | Assembler::~Assembler() { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 321 | ASSERT(const_pool_blocked_nesting_ == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 322 | if (own_buffer_) { |
| 323 | if (spare_buffer_ == NULL && buffer_size_ == kMinimalBufferSize) { |
| 324 | spare_buffer_ = buffer_; |
| 325 | } else { |
| 326 | DeleteArray(buffer_); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | |
| 332 | void Assembler::GetCode(CodeDesc* desc) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 333 | // Emit constant pool if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 334 | CheckConstPool(true, false); |
| 335 | ASSERT(num_prinfo_ == 0); |
| 336 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 337 | // Setup code descriptor. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 338 | desc->buffer = buffer_; |
| 339 | desc->buffer_size = buffer_size_; |
| 340 | desc->instr_size = pc_offset(); |
| 341 | desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos(); |
| 342 | } |
| 343 | |
| 344 | |
| 345 | void Assembler::Align(int m) { |
| 346 | ASSERT(m >= 4 && IsPowerOf2(m)); |
| 347 | while ((pc_offset() & (m - 1)) != 0) { |
| 348 | nop(); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 353 | bool Assembler::IsNop(Instr instr, int type) { |
| 354 | // Check for mov rx, rx. |
| 355 | ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop. |
| 356 | return instr == (al | 13*B21 | type*B12 | type); |
| 357 | } |
| 358 | |
| 359 | |
| 360 | bool Assembler::IsBranch(Instr instr) { |
| 361 | return (instr & (B27 | B25)) == (B27 | B25); |
| 362 | } |
| 363 | |
| 364 | |
| 365 | int Assembler::GetBranchOffset(Instr instr) { |
| 366 | ASSERT(IsBranch(instr)); |
| 367 | // Take the jump offset in the lower 24 bits, sign extend it and multiply it |
| 368 | // with 4 to get the offset in bytes. |
| 369 | return ((instr & Imm24Mask) << 8) >> 6; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | bool Assembler::IsLdrRegisterImmediate(Instr instr) { |
| 374 | return (instr & (B27 | B26 | B25 | B22 | B20)) == (B26 | B20); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | int Assembler::GetLdrRegisterImmediateOffset(Instr instr) { |
| 379 | ASSERT(IsLdrRegisterImmediate(instr)); |
| 380 | bool positive = (instr & B23) == B23; |
| 381 | int offset = instr & Off12Mask; // Zero extended offset. |
| 382 | return positive ? offset : -offset; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | Instr Assembler::SetLdrRegisterImmediateOffset(Instr instr, int offset) { |
| 387 | ASSERT(IsLdrRegisterImmediate(instr)); |
| 388 | bool positive = offset >= 0; |
| 389 | if (!positive) offset = -offset; |
| 390 | ASSERT(is_uint12(offset)); |
| 391 | // Set bit indicating whether the offset should be added. |
| 392 | instr = (instr & ~B23) | (positive ? B23 : 0); |
| 393 | // Set the actual offset. |
| 394 | return (instr & ~Off12Mask) | offset; |
| 395 | } |
| 396 | |
| 397 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 398 | // Labels refer to positions in the (to be) generated code. |
| 399 | // There are bound, linked, and unused labels. |
| 400 | // |
| 401 | // Bound labels refer to known positions in the already |
| 402 | // generated code. pos() is the position the label refers to. |
| 403 | // |
| 404 | // Linked labels refer to unknown positions in the code |
| 405 | // to be generated; pos() is the position of the last |
| 406 | // instruction using the label. |
| 407 | |
| 408 | |
| 409 | // The link chain is terminated by a negative code position (must be aligned) |
| 410 | const int kEndOfChain = -4; |
| 411 | |
| 412 | |
| 413 | int Assembler::target_at(int pos) { |
| 414 | Instr instr = instr_at(pos); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 415 | if ((instr & ~Imm24Mask) == 0) { |
| 416 | // Emitted label constant, not part of a branch. |
| 417 | return instr - (Code::kHeaderSize - kHeapObjectTag); |
| 418 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 419 | ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24 |
| 420 | int imm26 = ((instr & Imm24Mask) << 8) >> 6; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 421 | if ((instr & CondMask) == nv && (instr & B24) != 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 422 | // blx uses bit 24 to encode bit 2 of imm26 |
| 423 | imm26 += 2; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 424 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 425 | return pos + kPcLoadDelta + imm26; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | |
| 429 | void Assembler::target_at_put(int pos, int target_pos) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 430 | Instr instr = instr_at(pos); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 431 | if ((instr & ~Imm24Mask) == 0) { |
| 432 | ASSERT(target_pos == kEndOfChain || target_pos >= 0); |
| 433 | // Emitted label constant, not part of a branch. |
| 434 | // Make label relative to Code* of generated Code object. |
| 435 | instr_at_put(pos, target_pos + (Code::kHeaderSize - kHeapObjectTag)); |
| 436 | return; |
| 437 | } |
| 438 | int imm26 = target_pos - (pos + kPcLoadDelta); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 439 | ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24 |
| 440 | if ((instr & CondMask) == nv) { |
| 441 | // blx uses bit 24 to encode bit 2 of imm26 |
| 442 | ASSERT((imm26 & 1) == 0); |
| 443 | instr = (instr & ~(B24 | Imm24Mask)) | ((imm26 & 2) >> 1)*B24; |
| 444 | } else { |
| 445 | ASSERT((imm26 & 3) == 0); |
| 446 | instr &= ~Imm24Mask; |
| 447 | } |
| 448 | int imm24 = imm26 >> 2; |
| 449 | ASSERT(is_int24(imm24)); |
| 450 | instr_at_put(pos, instr | (imm24 & Imm24Mask)); |
| 451 | } |
| 452 | |
| 453 | |
| 454 | void Assembler::print(Label* L) { |
| 455 | if (L->is_unused()) { |
| 456 | PrintF("unused label\n"); |
| 457 | } else if (L->is_bound()) { |
| 458 | PrintF("bound label to %d\n", L->pos()); |
| 459 | } else if (L->is_linked()) { |
| 460 | Label l = *L; |
| 461 | PrintF("unbound label"); |
| 462 | while (l.is_linked()) { |
| 463 | PrintF("@ %d ", l.pos()); |
| 464 | Instr instr = instr_at(l.pos()); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 465 | if ((instr & ~Imm24Mask) == 0) { |
| 466 | PrintF("value\n"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 467 | } else { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 468 | ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx |
| 469 | int cond = instr & CondMask; |
| 470 | const char* b; |
| 471 | const char* c; |
| 472 | if (cond == nv) { |
| 473 | b = "blx"; |
| 474 | c = ""; |
| 475 | } else { |
| 476 | if ((instr & B24) != 0) |
| 477 | b = "bl"; |
| 478 | else |
| 479 | b = "b"; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 480 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 481 | switch (cond) { |
| 482 | case eq: c = "eq"; break; |
| 483 | case ne: c = "ne"; break; |
| 484 | case hs: c = "hs"; break; |
| 485 | case lo: c = "lo"; break; |
| 486 | case mi: c = "mi"; break; |
| 487 | case pl: c = "pl"; break; |
| 488 | case vs: c = "vs"; break; |
| 489 | case vc: c = "vc"; break; |
| 490 | case hi: c = "hi"; break; |
| 491 | case ls: c = "ls"; break; |
| 492 | case ge: c = "ge"; break; |
| 493 | case lt: c = "lt"; break; |
| 494 | case gt: c = "gt"; break; |
| 495 | case le: c = "le"; break; |
| 496 | case al: c = ""; break; |
| 497 | default: |
| 498 | c = ""; |
| 499 | UNREACHABLE(); |
| 500 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 501 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 502 | PrintF("%s%s\n", b, c); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 503 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 504 | next(&l); |
| 505 | } |
| 506 | } else { |
| 507 | PrintF("label in inconsistent state (pos = %d)\n", L->pos_); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 512 | void Assembler::bind_to(Label* L, int pos) { |
| 513 | ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position |
| 514 | while (L->is_linked()) { |
| 515 | int fixup_pos = L->pos(); |
| 516 | next(L); // call next before overwriting link with target at fixup_pos |
| 517 | target_at_put(fixup_pos, pos); |
| 518 | } |
| 519 | L->bind_to(pos); |
| 520 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 521 | // Keep track of the last bound label so we don't eliminate any instructions |
| 522 | // before a bound label. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 523 | if (pos > last_bound_pos_) |
| 524 | last_bound_pos_ = pos; |
| 525 | } |
| 526 | |
| 527 | |
| 528 | void Assembler::link_to(Label* L, Label* appendix) { |
| 529 | if (appendix->is_linked()) { |
| 530 | if (L->is_linked()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 531 | // Append appendix to L's list. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | int fixup_pos; |
| 533 | int link = L->pos(); |
| 534 | do { |
| 535 | fixup_pos = link; |
| 536 | link = target_at(fixup_pos); |
| 537 | } while (link > 0); |
| 538 | ASSERT(link == kEndOfChain); |
| 539 | target_at_put(fixup_pos, appendix->pos()); |
| 540 | } else { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 541 | // L is empty, simply use appendix. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 542 | *L = *appendix; |
| 543 | } |
| 544 | } |
| 545 | appendix->Unuse(); // appendix should not be used anymore |
| 546 | } |
| 547 | |
| 548 | |
| 549 | void Assembler::bind(Label* L) { |
| 550 | ASSERT(!L->is_bound()); // label can only be bound once |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 551 | bind_to(L, pc_offset()); |
| 552 | } |
| 553 | |
| 554 | |
| 555 | void Assembler::next(Label* L) { |
| 556 | ASSERT(L->is_linked()); |
| 557 | int link = target_at(L->pos()); |
| 558 | if (link > 0) { |
| 559 | L->link_to(link); |
| 560 | } else { |
| 561 | ASSERT(link == kEndOfChain); |
| 562 | L->Unuse(); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 567 | // Low-level code emission routines depending on the addressing mode. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 568 | static bool fits_shifter(uint32_t imm32, |
| 569 | uint32_t* rotate_imm, |
| 570 | uint32_t* immed_8, |
| 571 | Instr* instr) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 572 | // imm32 must be unsigned. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 573 | for (int rot = 0; rot < 16; rot++) { |
| 574 | uint32_t imm8 = (imm32 << 2*rot) | (imm32 >> (32 - 2*rot)); |
| 575 | if ((imm8 <= 0xff)) { |
| 576 | *rotate_imm = rot; |
| 577 | *immed_8 = imm8; |
| 578 | return true; |
| 579 | } |
| 580 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 581 | // If the opcode is mov or mvn and if ~imm32 fits, change the opcode. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 582 | if (instr != NULL && (*instr & 0xd*B21) == 0xd*B21) { |
| 583 | if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) { |
| 584 | *instr ^= 0x2*B21; |
| 585 | return true; |
| 586 | } |
| 587 | } |
| 588 | return false; |
| 589 | } |
| 590 | |
| 591 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 592 | // We have to use the temporary register for things that can be relocated even |
| 593 | // if they can be encoded in the ARM's 12 bits of immediate-offset instruction |
| 594 | // space. There is no guarantee that the relocated location can be similarly |
| 595 | // encoded. |
| 596 | static bool MustUseIp(RelocInfo::Mode rmode) { |
| 597 | if (rmode == RelocInfo::EXTERNAL_REFERENCE) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 598 | #ifdef DEBUG |
| 599 | if (!Serializer::enabled()) { |
| 600 | Serializer::TooLateToEnableNow(); |
| 601 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 602 | #endif // def DEBUG |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 603 | return Serializer::enabled(); |
| 604 | } else if (rmode == RelocInfo::NONE) { |
| 605 | return false; |
| 606 | } |
| 607 | return true; |
| 608 | } |
| 609 | |
| 610 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 611 | void Assembler::addrmod1(Instr instr, |
| 612 | Register rn, |
| 613 | Register rd, |
| 614 | const Operand& x) { |
| 615 | CheckBuffer(); |
| 616 | ASSERT((instr & ~(CondMask | OpCodeMask | S)) == 0); |
| 617 | if (!x.rm_.is_valid()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 618 | // Immediate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 619 | uint32_t rotate_imm; |
| 620 | uint32_t immed_8; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 621 | if (MustUseIp(x.rmode_) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 622 | !fits_shifter(x.imm32_, &rotate_imm, &immed_8, &instr)) { |
| 623 | // The immediate operand cannot be encoded as a shifter operand, so load |
| 624 | // it first to register ip and change the original instruction to use ip. |
| 625 | // 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] | 626 | // condition code), then replace it with a 'ldr rd, [pc]'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 627 | RecordRelocInfo(x.rmode_, x.imm32_); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 628 | 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] | 629 | Condition cond = static_cast<Condition>(instr & CondMask); |
| 630 | if ((instr & ~CondMask) == 13*B21) { // mov, S not set |
| 631 | ldr(rd, MemOperand(pc, 0), cond); |
| 632 | } else { |
| 633 | ldr(ip, MemOperand(pc, 0), cond); |
| 634 | addrmod1(instr, rn, rd, Operand(ip)); |
| 635 | } |
| 636 | return; |
| 637 | } |
| 638 | instr |= I | rotate_imm*B8 | immed_8; |
| 639 | } else if (!x.rs_.is_valid()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 640 | // Immediate shift. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 641 | instr |= x.shift_imm_*B7 | x.shift_op_ | x.rm_.code(); |
| 642 | } else { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 643 | // Register shift. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 644 | ASSERT(!rn.is(pc) && !rd.is(pc) && !x.rm_.is(pc) && !x.rs_.is(pc)); |
| 645 | instr |= x.rs_.code()*B8 | x.shift_op_ | B4 | x.rm_.code(); |
| 646 | } |
| 647 | emit(instr | rn.code()*B16 | rd.code()*B12); |
| 648 | if (rn.is(pc) || x.rm_.is(pc)) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 649 | // Block constant pool emission for one instruction after reading pc. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 650 | BlockConstPoolBefore(pc_offset() + kInstrSize); |
| 651 | } |
| 652 | |
| 653 | |
| 654 | void Assembler::addrmod2(Instr instr, Register rd, const MemOperand& x) { |
| 655 | ASSERT((instr & ~(CondMask | B | L)) == B26); |
| 656 | int am = x.am_; |
| 657 | if (!x.rm_.is_valid()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 658 | // Immediate offset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 659 | int offset_12 = x.offset_; |
| 660 | if (offset_12 < 0) { |
| 661 | offset_12 = -offset_12; |
| 662 | am ^= U; |
| 663 | } |
| 664 | if (!is_uint12(offset_12)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 665 | // Immediate offset cannot be encoded, load it first to register ip |
| 666 | // 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] | 667 | ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip))); |
| 668 | mov(ip, Operand(x.offset_), LeaveCC, |
| 669 | static_cast<Condition>(instr & CondMask)); |
| 670 | addrmod2(instr, rd, MemOperand(x.rn_, ip, x.am_)); |
| 671 | return; |
| 672 | } |
| 673 | ASSERT(offset_12 >= 0); // no masking needed |
| 674 | instr |= offset_12; |
| 675 | } else { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 676 | // Register offset (shift_imm_ and shift_op_ are 0) or scaled |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 677 | // register offset the constructors make sure than both shift_imm_ |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 678 | // and shift_op_ are initialized. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 679 | ASSERT(!x.rm_.is(pc)); |
| 680 | instr |= B25 | x.shift_imm_*B7 | x.shift_op_ | x.rm_.code(); |
| 681 | } |
| 682 | ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback |
| 683 | emit(instr | am | x.rn_.code()*B16 | rd.code()*B12); |
| 684 | } |
| 685 | |
| 686 | |
| 687 | void Assembler::addrmod3(Instr instr, Register rd, const MemOperand& x) { |
| 688 | ASSERT((instr & ~(CondMask | L | S6 | H)) == (B4 | B7)); |
| 689 | ASSERT(x.rn_.is_valid()); |
| 690 | int am = x.am_; |
| 691 | if (!x.rm_.is_valid()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 692 | // Immediate offset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 693 | int offset_8 = x.offset_; |
| 694 | if (offset_8 < 0) { |
| 695 | offset_8 = -offset_8; |
| 696 | am ^= U; |
| 697 | } |
| 698 | if (!is_uint8(offset_8)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 699 | // Immediate offset cannot be encoded, load it first to register ip |
| 700 | // 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] | 701 | ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip))); |
| 702 | mov(ip, Operand(x.offset_), LeaveCC, |
| 703 | static_cast<Condition>(instr & CondMask)); |
| 704 | addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_)); |
| 705 | return; |
| 706 | } |
| 707 | ASSERT(offset_8 >= 0); // no masking needed |
| 708 | instr |= B | (offset_8 >> 4)*B8 | (offset_8 & 0xf); |
| 709 | } else if (x.shift_imm_ != 0) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 710 | // Scaled register offset not supported, load index first |
| 711 | // 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] | 712 | ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip))); |
| 713 | mov(ip, Operand(x.rm_, x.shift_op_, x.shift_imm_), LeaveCC, |
| 714 | static_cast<Condition>(instr & CondMask)); |
| 715 | addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_)); |
| 716 | return; |
| 717 | } else { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 718 | // Register offset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 719 | ASSERT((am & (P|W)) == P || !x.rm_.is(pc)); // no pc index with writeback |
| 720 | instr |= x.rm_.code(); |
| 721 | } |
| 722 | ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback |
| 723 | emit(instr | am | x.rn_.code()*B16 | rd.code()*B12); |
| 724 | } |
| 725 | |
| 726 | |
| 727 | void Assembler::addrmod4(Instr instr, Register rn, RegList rl) { |
| 728 | ASSERT((instr & ~(CondMask | P | U | W | L)) == B27); |
| 729 | ASSERT(rl != 0); |
| 730 | ASSERT(!rn.is(pc)); |
| 731 | emit(instr | rn.code()*B16 | rl); |
| 732 | } |
| 733 | |
| 734 | |
| 735 | void Assembler::addrmod5(Instr instr, CRegister crd, const MemOperand& x) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 736 | // Unindexed addressing is not encoded by this function. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 737 | ASSERT_EQ((B27 | B26), |
| 738 | (instr & ~(CondMask | CoprocessorMask | P | U | N | W | L))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 739 | ASSERT(x.rn_.is_valid() && !x.rm_.is_valid()); |
| 740 | int am = x.am_; |
| 741 | int offset_8 = x.offset_; |
| 742 | ASSERT((offset_8 & 3) == 0); // offset must be an aligned word offset |
| 743 | offset_8 >>= 2; |
| 744 | if (offset_8 < 0) { |
| 745 | offset_8 = -offset_8; |
| 746 | am ^= U; |
| 747 | } |
| 748 | ASSERT(is_uint8(offset_8)); // unsigned word offset must fit in a byte |
| 749 | ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback |
| 750 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 751 | // Post-indexed addressing requires W == 1; different than in addrmod2/3. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 752 | if ((am & P) == 0) |
| 753 | am |= W; |
| 754 | |
| 755 | ASSERT(offset_8 >= 0); // no masking needed |
| 756 | emit(instr | am | x.rn_.code()*B16 | crd.code()*B12 | offset_8); |
| 757 | } |
| 758 | |
| 759 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 760 | int Assembler::branch_offset(Label* L, bool jump_elimination_allowed) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 761 | int target_pos; |
| 762 | if (L->is_bound()) { |
| 763 | target_pos = L->pos(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 764 | } else { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 765 | if (L->is_linked()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 766 | target_pos = L->pos(); // L's link |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 767 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 768 | target_pos = kEndOfChain; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 769 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 770 | L->link_to(pc_offset()); |
| 771 | } |
| 772 | |
| 773 | // 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] | 774 | // be emitted at the pc offset recorded by the label. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 775 | BlockConstPoolBefore(pc_offset() + kInstrSize); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 776 | return target_pos - (pc_offset() + kPcLoadDelta); |
| 777 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 778 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 779 | |
| 780 | void Assembler::label_at_put(Label* L, int at_offset) { |
| 781 | int target_pos; |
| 782 | if (L->is_bound()) { |
| 783 | target_pos = L->pos(); |
| 784 | } else { |
| 785 | if (L->is_linked()) { |
| 786 | target_pos = L->pos(); // L's link |
| 787 | } else { |
| 788 | target_pos = kEndOfChain; |
| 789 | } |
| 790 | L->link_to(at_offset); |
| 791 | instr_at_put(at_offset, target_pos + (Code::kHeaderSize - kHeapObjectTag)); |
| 792 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 796 | // Branch instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 797 | void Assembler::b(int branch_offset, Condition cond) { |
| 798 | ASSERT((branch_offset & 3) == 0); |
| 799 | int imm24 = branch_offset >> 2; |
| 800 | ASSERT(is_int24(imm24)); |
| 801 | emit(cond | B27 | B25 | (imm24 & Imm24Mask)); |
| 802 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame^] | 803 | if (cond == al) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 804 | // Dead code is a good location to emit the constant pool. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 805 | CheckConstPool(false, false); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame^] | 806 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | |
| 810 | void Assembler::bl(int branch_offset, Condition cond) { |
| 811 | ASSERT((branch_offset & 3) == 0); |
| 812 | int imm24 = branch_offset >> 2; |
| 813 | ASSERT(is_int24(imm24)); |
| 814 | emit(cond | B27 | B25 | B24 | (imm24 & Imm24Mask)); |
| 815 | } |
| 816 | |
| 817 | |
| 818 | void Assembler::blx(int branch_offset) { // v5 and above |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 819 | WriteRecordedPositions(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 820 | ASSERT((branch_offset & 1) == 0); |
| 821 | int h = ((branch_offset & 2) >> 1)*B24; |
| 822 | int imm24 = branch_offset >> 2; |
| 823 | ASSERT(is_int24(imm24)); |
| 824 | emit(15 << 28 | B27 | B25 | h | (imm24 & Imm24Mask)); |
| 825 | } |
| 826 | |
| 827 | |
| 828 | void Assembler::blx(Register target, Condition cond) { // v5 and above |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 829 | WriteRecordedPositions(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 830 | ASSERT(!target.is(pc)); |
| 831 | emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | 3*B4 | target.code()); |
| 832 | } |
| 833 | |
| 834 | |
| 835 | 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] | 836 | WriteRecordedPositions(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 837 | ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged |
| 838 | emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | B4 | target.code()); |
| 839 | } |
| 840 | |
| 841 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 842 | // Data-processing instructions. |
| 843 | |
| 844 | // UBFX <Rd>,<Rn>,#<lsb>,#<width - 1> |
| 845 | // Instruction details available in ARM DDI 0406A, A8-464. |
| 846 | // cond(31-28) | 01111(27-23)| 1(22) | 1(21) | widthm1(20-16) | |
| 847 | // Rd(15-12) | lsb(11-7) | 101(6-4) | Rn(3-0) |
| 848 | void Assembler::ubfx(Register dst, Register src1, const Operand& src2, |
| 849 | const Operand& src3, Condition cond) { |
| 850 | ASSERT(!src2.rm_.is_valid() && !src3.rm_.is_valid()); |
| 851 | ASSERT(static_cast<uint32_t>(src2.imm32_) <= 0x1f); |
| 852 | ASSERT(static_cast<uint32_t>(src3.imm32_) <= 0x1f); |
| 853 | emit(cond | 0x3F*B21 | src3.imm32_*B16 | |
| 854 | dst.code()*B12 | src2.imm32_*B7 | 0x5*B4 | src1.code()); |
| 855 | } |
| 856 | |
| 857 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 858 | void Assembler::and_(Register dst, Register src1, const Operand& src2, |
| 859 | SBit s, Condition cond) { |
| 860 | addrmod1(cond | 0*B21 | s, src1, dst, src2); |
| 861 | } |
| 862 | |
| 863 | |
| 864 | void Assembler::eor(Register dst, Register src1, const Operand& src2, |
| 865 | SBit s, Condition cond) { |
| 866 | addrmod1(cond | 1*B21 | s, src1, dst, src2); |
| 867 | } |
| 868 | |
| 869 | |
| 870 | void Assembler::sub(Register dst, Register src1, const Operand& src2, |
| 871 | SBit s, Condition cond) { |
| 872 | addrmod1(cond | 2*B21 | s, src1, dst, src2); |
| 873 | } |
| 874 | |
| 875 | |
| 876 | void Assembler::rsb(Register dst, Register src1, const Operand& src2, |
| 877 | SBit s, Condition cond) { |
| 878 | addrmod1(cond | 3*B21 | s, src1, dst, src2); |
| 879 | } |
| 880 | |
| 881 | |
| 882 | void Assembler::add(Register dst, Register src1, const Operand& src2, |
| 883 | SBit s, Condition cond) { |
| 884 | addrmod1(cond | 4*B21 | s, src1, dst, src2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 885 | |
| 886 | // Eliminate pattern: push(r), pop() |
| 887 | // str(src, MemOperand(sp, 4, NegPreIndex), al); |
| 888 | // add(sp, sp, Operand(kPointerSize)); |
| 889 | // Both instructions can be eliminated. |
| 890 | int pattern_size = 2 * kInstrSize; |
| 891 | if (FLAG_push_pop_elimination && |
| 892 | last_bound_pos_ <= (pc_offset() - pattern_size) && |
| 893 | reloc_info_writer.last_pc() <= (pc_ - pattern_size) && |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 894 | // Pattern. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 895 | instr_at(pc_ - 1 * kInstrSize) == kPopInstruction && |
| 896 | (instr_at(pc_ - 2 * kInstrSize) & ~RdMask) == kPushRegPattern) { |
| 897 | pc_ -= 2 * kInstrSize; |
| 898 | if (FLAG_print_push_pop_elimination) { |
| 899 | PrintF("%x push(reg)/pop() eliminated\n", pc_offset()); |
| 900 | } |
| 901 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | |
| 905 | void Assembler::adc(Register dst, Register src1, const Operand& src2, |
| 906 | SBit s, Condition cond) { |
| 907 | addrmod1(cond | 5*B21 | s, src1, dst, src2); |
| 908 | } |
| 909 | |
| 910 | |
| 911 | void Assembler::sbc(Register dst, Register src1, const Operand& src2, |
| 912 | SBit s, Condition cond) { |
| 913 | addrmod1(cond | 6*B21 | s, src1, dst, src2); |
| 914 | } |
| 915 | |
| 916 | |
| 917 | void Assembler::rsc(Register dst, Register src1, const Operand& src2, |
| 918 | SBit s, Condition cond) { |
| 919 | addrmod1(cond | 7*B21 | s, src1, dst, src2); |
| 920 | } |
| 921 | |
| 922 | |
| 923 | void Assembler::tst(Register src1, const Operand& src2, Condition cond) { |
| 924 | addrmod1(cond | 8*B21 | S, src1, r0, src2); |
| 925 | } |
| 926 | |
| 927 | |
| 928 | void Assembler::teq(Register src1, const Operand& src2, Condition cond) { |
| 929 | addrmod1(cond | 9*B21 | S, src1, r0, src2); |
| 930 | } |
| 931 | |
| 932 | |
| 933 | void Assembler::cmp(Register src1, const Operand& src2, Condition cond) { |
| 934 | addrmod1(cond | 10*B21 | S, src1, r0, src2); |
| 935 | } |
| 936 | |
| 937 | |
| 938 | void Assembler::cmn(Register src1, const Operand& src2, Condition cond) { |
| 939 | addrmod1(cond | 11*B21 | S, src1, r0, src2); |
| 940 | } |
| 941 | |
| 942 | |
| 943 | void Assembler::orr(Register dst, Register src1, const Operand& src2, |
| 944 | SBit s, Condition cond) { |
| 945 | addrmod1(cond | 12*B21 | s, src1, dst, src2); |
| 946 | } |
| 947 | |
| 948 | |
| 949 | 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] | 950 | if (dst.is(pc)) { |
| 951 | WriteRecordedPositions(); |
| 952 | } |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 953 | // Don't allow nop instructions in the form mov rn, rn to be generated using |
| 954 | // the mov instruction. They must be generated using nop(int) |
| 955 | // pseudo instructions. |
| 956 | 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] | 957 | addrmod1(cond | 13*B21 | s, r0, dst, src); |
| 958 | } |
| 959 | |
| 960 | |
| 961 | void Assembler::bic(Register dst, Register src1, const Operand& src2, |
| 962 | SBit s, Condition cond) { |
| 963 | addrmod1(cond | 14*B21 | s, src1, dst, src2); |
| 964 | } |
| 965 | |
| 966 | |
| 967 | void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) { |
| 968 | addrmod1(cond | 15*B21 | s, r0, dst, src); |
| 969 | } |
| 970 | |
| 971 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 972 | // Multiply instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 973 | void Assembler::mla(Register dst, Register src1, Register src2, Register srcA, |
| 974 | SBit s, Condition cond) { |
| 975 | 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] | 976 | emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 | |
| 977 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 978 | } |
| 979 | |
| 980 | |
| 981 | void Assembler::mul(Register dst, Register src1, Register src2, |
| 982 | SBit s, Condition cond) { |
| 983 | ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 984 | // dst goes in bits 16-19 for this instruction! |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 985 | emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 986 | } |
| 987 | |
| 988 | |
| 989 | void Assembler::smlal(Register dstL, |
| 990 | Register dstH, |
| 991 | Register src1, |
| 992 | Register src2, |
| 993 | SBit s, |
| 994 | Condition cond) { |
| 995 | 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] | 996 | ASSERT(!dstL.is(dstH)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 997 | emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 | |
| 998 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 999 | } |
| 1000 | |
| 1001 | |
| 1002 | void Assembler::smull(Register dstL, |
| 1003 | Register dstH, |
| 1004 | Register src1, |
| 1005 | Register src2, |
| 1006 | SBit s, |
| 1007 | Condition cond) { |
| 1008 | 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] | 1009 | ASSERT(!dstL.is(dstH)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1010 | emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 | |
| 1011 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1012 | } |
| 1013 | |
| 1014 | |
| 1015 | void Assembler::umlal(Register dstL, |
| 1016 | Register dstH, |
| 1017 | Register src1, |
| 1018 | Register src2, |
| 1019 | SBit s, |
| 1020 | Condition cond) { |
| 1021 | 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] | 1022 | ASSERT(!dstL.is(dstH)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1023 | emit(cond | B23 | A | s | dstH.code()*B16 | dstL.code()*B12 | |
| 1024 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1025 | } |
| 1026 | |
| 1027 | |
| 1028 | void Assembler::umull(Register dstL, |
| 1029 | Register dstH, |
| 1030 | Register src1, |
| 1031 | Register src2, |
| 1032 | SBit s, |
| 1033 | Condition cond) { |
| 1034 | 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] | 1035 | ASSERT(!dstL.is(dstH)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1036 | emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1037 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1038 | } |
| 1039 | |
| 1040 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1041 | // Miscellaneous arithmetic instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1042 | void Assembler::clz(Register dst, Register src, Condition cond) { |
| 1043 | // v5 and above. |
| 1044 | ASSERT(!dst.is(pc) && !src.is(pc)); |
| 1045 | emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 | |
| 1046 | 15*B8 | B4 | src.code()); |
| 1047 | } |
| 1048 | |
| 1049 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1050 | // Status register access instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1051 | void Assembler::mrs(Register dst, SRegister s, Condition cond) { |
| 1052 | ASSERT(!dst.is(pc)); |
| 1053 | emit(cond | B24 | s | 15*B16 | dst.code()*B12); |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | void Assembler::msr(SRegisterFieldMask fields, const Operand& src, |
| 1058 | Condition cond) { |
| 1059 | ASSERT(fields >= B16 && fields < B20); // at least one field set |
| 1060 | Instr instr; |
| 1061 | if (!src.rm_.is_valid()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1062 | // Immediate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1063 | uint32_t rotate_imm; |
| 1064 | uint32_t immed_8; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1065 | if (MustUseIp(src.rmode_) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1066 | !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1067 | // Immediate operand cannot be encoded, load it first to register ip. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1068 | RecordRelocInfo(src.rmode_, src.imm32_); |
| 1069 | ldr(ip, MemOperand(pc, 0), cond); |
| 1070 | msr(fields, Operand(ip), cond); |
| 1071 | return; |
| 1072 | } |
| 1073 | instr = I | rotate_imm*B8 | immed_8; |
| 1074 | } else { |
| 1075 | ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed |
| 1076 | instr = src.rm_.code(); |
| 1077 | } |
| 1078 | emit(cond | instr | B24 | B21 | fields | 15*B12); |
| 1079 | } |
| 1080 | |
| 1081 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1082 | // Load/Store instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1083 | void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1084 | if (dst.is(pc)) { |
| 1085 | WriteRecordedPositions(); |
| 1086 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1087 | addrmod2(cond | B26 | L, dst, src); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1088 | |
| 1089 | // Eliminate pattern: push(r), pop(r) |
| 1090 | // str(r, MemOperand(sp, 4, NegPreIndex), al) |
| 1091 | // ldr(r, MemOperand(sp, 4, PostIndex), al) |
| 1092 | // Both instructions can be eliminated. |
| 1093 | int pattern_size = 2 * kInstrSize; |
| 1094 | if (FLAG_push_pop_elimination && |
| 1095 | last_bound_pos_ <= (pc_offset() - pattern_size) && |
| 1096 | reloc_info_writer.last_pc() <= (pc_ - pattern_size) && |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1097 | // Pattern. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1098 | instr_at(pc_ - 1 * kInstrSize) == (kPopRegPattern | dst.code() * B12) && |
| 1099 | instr_at(pc_ - 2 * kInstrSize) == (kPushRegPattern | dst.code() * B12)) { |
| 1100 | pc_ -= 2 * kInstrSize; |
| 1101 | if (FLAG_print_push_pop_elimination) { |
| 1102 | PrintF("%x push/pop (same reg) eliminated\n", pc_offset()); |
| 1103 | } |
| 1104 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | void Assembler::str(Register src, const MemOperand& dst, Condition cond) { |
| 1109 | addrmod2(cond | B26, src, dst); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1110 | |
| 1111 | // Eliminate pattern: pop(), push(r) |
| 1112 | // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al |
| 1113 | // -> str r, [sp, 0], al |
| 1114 | int pattern_size = 2 * kInstrSize; |
| 1115 | if (FLAG_push_pop_elimination && |
| 1116 | last_bound_pos_ <= (pc_offset() - pattern_size) && |
| 1117 | reloc_info_writer.last_pc() <= (pc_ - pattern_size) && |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1118 | // Pattern. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1119 | instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) && |
| 1120 | instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) { |
| 1121 | pc_ -= 2 * kInstrSize; |
| 1122 | emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12); |
| 1123 | if (FLAG_print_push_pop_elimination) { |
| 1124 | PrintF("%x pop()/push(reg) eliminated\n", pc_offset()); |
| 1125 | } |
| 1126 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | |
| 1130 | void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) { |
| 1131 | addrmod2(cond | B26 | B | L, dst, src); |
| 1132 | } |
| 1133 | |
| 1134 | |
| 1135 | void Assembler::strb(Register src, const MemOperand& dst, Condition cond) { |
| 1136 | addrmod2(cond | B26 | B, src, dst); |
| 1137 | } |
| 1138 | |
| 1139 | |
| 1140 | void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) { |
| 1141 | addrmod3(cond | L | B7 | H | B4, dst, src); |
| 1142 | } |
| 1143 | |
| 1144 | |
| 1145 | void Assembler::strh(Register src, const MemOperand& dst, Condition cond) { |
| 1146 | addrmod3(cond | B7 | H | B4, src, dst); |
| 1147 | } |
| 1148 | |
| 1149 | |
| 1150 | void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) { |
| 1151 | addrmod3(cond | L | B7 | S6 | B4, dst, src); |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) { |
| 1156 | addrmod3(cond | L | B7 | S6 | H | B4, dst, src); |
| 1157 | } |
| 1158 | |
| 1159 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1160 | // Load/Store multiple instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1161 | void Assembler::ldm(BlockAddrMode am, |
| 1162 | Register base, |
| 1163 | RegList dst, |
| 1164 | Condition cond) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1165 | // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1166 | ASSERT(base.is(sp) || (dst & sp.bit()) == 0); |
| 1167 | |
| 1168 | addrmod4(cond | B27 | am | L, base, dst); |
| 1169 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1170 | // 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] | 1171 | if (cond == al && (dst & pc.bit()) != 0) { |
| 1172 | // There is a slight chance that the ldm instruction was actually a call, |
| 1173 | // in which case it would be wrong to return into the constant pool; we |
| 1174 | // recognize this case by checking if the emission of the pool was blocked |
| 1175 | // at the pc of the ldm instruction by a mov lr, pc instruction; if this is |
| 1176 | // the case, we emit a jump over the pool. |
| 1177 | CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize); |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | |
| 1182 | void Assembler::stm(BlockAddrMode am, |
| 1183 | Register base, |
| 1184 | RegList src, |
| 1185 | Condition cond) { |
| 1186 | addrmod4(cond | B27 | am, base, src); |
| 1187 | } |
| 1188 | |
| 1189 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1190 | // Semaphore instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1191 | void Assembler::swp(Register dst, Register src, Register base, Condition cond) { |
| 1192 | ASSERT(!dst.is(pc) && !src.is(pc) && !base.is(pc)); |
| 1193 | ASSERT(!dst.is(base) && !src.is(base)); |
| 1194 | emit(cond | P | base.code()*B16 | dst.code()*B12 | |
| 1195 | B7 | B4 | src.code()); |
| 1196 | } |
| 1197 | |
| 1198 | |
| 1199 | void Assembler::swpb(Register dst, |
| 1200 | Register src, |
| 1201 | Register base, |
| 1202 | Condition cond) { |
| 1203 | ASSERT(!dst.is(pc) && !src.is(pc) && !base.is(pc)); |
| 1204 | ASSERT(!dst.is(base) && !src.is(base)); |
| 1205 | emit(cond | P | B | base.code()*B16 | dst.code()*B12 | |
| 1206 | B7 | B4 | src.code()); |
| 1207 | } |
| 1208 | |
| 1209 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1210 | // Exception-generating instructions and debugging support. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1211 | void Assembler::stop(const char* msg) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1212 | #ifndef __arm__ |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1213 | // The simulator handles these special instructions and stops execution. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1214 | emit(15 << 28 | ((intptr_t) msg)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1215 | #else // def __arm__ |
| 1216 | #ifdef CAN_USE_ARMV5_INSTRUCTIONS |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1217 | bkpt(0); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1218 | #else // ndef CAN_USE_ARMV5_INSTRUCTIONS |
| 1219 | swi(0x9f0001); |
| 1220 | #endif // ndef CAN_USE_ARMV5_INSTRUCTIONS |
| 1221 | #endif // def __arm__ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | |
| 1225 | void Assembler::bkpt(uint32_t imm16) { // v5 and above |
| 1226 | ASSERT(is_uint16(imm16)); |
| 1227 | emit(al | B24 | B21 | (imm16 >> 4)*B8 | 7*B4 | (imm16 & 0xf)); |
| 1228 | } |
| 1229 | |
| 1230 | |
| 1231 | void Assembler::swi(uint32_t imm24, Condition cond) { |
| 1232 | ASSERT(is_uint24(imm24)); |
| 1233 | emit(cond | 15*B24 | imm24); |
| 1234 | } |
| 1235 | |
| 1236 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1237 | // Coprocessor instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1238 | void Assembler::cdp(Coprocessor coproc, |
| 1239 | int opcode_1, |
| 1240 | CRegister crd, |
| 1241 | CRegister crn, |
| 1242 | CRegister crm, |
| 1243 | int opcode_2, |
| 1244 | Condition cond) { |
| 1245 | ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2)); |
| 1246 | emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 | |
| 1247 | crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code()); |
| 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | void Assembler::cdp2(Coprocessor coproc, |
| 1252 | int opcode_1, |
| 1253 | CRegister crd, |
| 1254 | CRegister crn, |
| 1255 | CRegister crm, |
| 1256 | int opcode_2) { // v5 and above |
| 1257 | cdp(coproc, opcode_1, crd, crn, crm, opcode_2, static_cast<Condition>(nv)); |
| 1258 | } |
| 1259 | |
| 1260 | |
| 1261 | void Assembler::mcr(Coprocessor coproc, |
| 1262 | int opcode_1, |
| 1263 | Register rd, |
| 1264 | CRegister crn, |
| 1265 | CRegister crm, |
| 1266 | int opcode_2, |
| 1267 | Condition cond) { |
| 1268 | ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2)); |
| 1269 | emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 | |
| 1270 | rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code()); |
| 1271 | } |
| 1272 | |
| 1273 | |
| 1274 | void Assembler::mcr2(Coprocessor coproc, |
| 1275 | int opcode_1, |
| 1276 | Register rd, |
| 1277 | CRegister crn, |
| 1278 | CRegister crm, |
| 1279 | int opcode_2) { // v5 and above |
| 1280 | mcr(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv)); |
| 1281 | } |
| 1282 | |
| 1283 | |
| 1284 | void Assembler::mrc(Coprocessor coproc, |
| 1285 | int opcode_1, |
| 1286 | Register rd, |
| 1287 | CRegister crn, |
| 1288 | CRegister crm, |
| 1289 | int opcode_2, |
| 1290 | Condition cond) { |
| 1291 | ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2)); |
| 1292 | emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 | |
| 1293 | rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code()); |
| 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | void Assembler::mrc2(Coprocessor coproc, |
| 1298 | int opcode_1, |
| 1299 | Register rd, |
| 1300 | CRegister crn, |
| 1301 | CRegister crm, |
| 1302 | int opcode_2) { // v5 and above |
| 1303 | mrc(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv)); |
| 1304 | } |
| 1305 | |
| 1306 | |
| 1307 | void Assembler::ldc(Coprocessor coproc, |
| 1308 | CRegister crd, |
| 1309 | const MemOperand& src, |
| 1310 | LFlag l, |
| 1311 | Condition cond) { |
| 1312 | addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src); |
| 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | void Assembler::ldc(Coprocessor coproc, |
| 1317 | CRegister crd, |
| 1318 | Register rn, |
| 1319 | int option, |
| 1320 | LFlag l, |
| 1321 | Condition cond) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1322 | // Unindexed addressing. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1323 | ASSERT(is_uint8(option)); |
| 1324 | emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 | |
| 1325 | coproc*B8 | (option & 255)); |
| 1326 | } |
| 1327 | |
| 1328 | |
| 1329 | void Assembler::ldc2(Coprocessor coproc, |
| 1330 | CRegister crd, |
| 1331 | const MemOperand& src, |
| 1332 | LFlag l) { // v5 and above |
| 1333 | ldc(coproc, crd, src, l, static_cast<Condition>(nv)); |
| 1334 | } |
| 1335 | |
| 1336 | |
| 1337 | void Assembler::ldc2(Coprocessor coproc, |
| 1338 | CRegister crd, |
| 1339 | Register rn, |
| 1340 | int option, |
| 1341 | LFlag l) { // v5 and above |
| 1342 | ldc(coproc, crd, rn, option, l, static_cast<Condition>(nv)); |
| 1343 | } |
| 1344 | |
| 1345 | |
| 1346 | void Assembler::stc(Coprocessor coproc, |
| 1347 | CRegister crd, |
| 1348 | const MemOperand& dst, |
| 1349 | LFlag l, |
| 1350 | Condition cond) { |
| 1351 | addrmod5(cond | B27 | B26 | l | coproc*B8, crd, dst); |
| 1352 | } |
| 1353 | |
| 1354 | |
| 1355 | void Assembler::stc(Coprocessor coproc, |
| 1356 | CRegister crd, |
| 1357 | Register rn, |
| 1358 | int option, |
| 1359 | LFlag l, |
| 1360 | Condition cond) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1361 | // Unindexed addressing. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1362 | ASSERT(is_uint8(option)); |
| 1363 | emit(cond | B27 | B26 | U | l | rn.code()*B16 | crd.code()*B12 | |
| 1364 | coproc*B8 | (option & 255)); |
| 1365 | } |
| 1366 | |
| 1367 | |
| 1368 | void Assembler::stc2(Coprocessor |
| 1369 | coproc, CRegister crd, |
| 1370 | const MemOperand& dst, |
| 1371 | LFlag l) { // v5 and above |
| 1372 | stc(coproc, crd, dst, l, static_cast<Condition>(nv)); |
| 1373 | } |
| 1374 | |
| 1375 | |
| 1376 | void Assembler::stc2(Coprocessor coproc, |
| 1377 | CRegister crd, |
| 1378 | Register rn, |
| 1379 | int option, |
| 1380 | LFlag l) { // v5 and above |
| 1381 | stc(coproc, crd, rn, option, l, static_cast<Condition>(nv)); |
| 1382 | } |
| 1383 | |
| 1384 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1385 | // Support for VFP. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1386 | void Assembler::vldr(const DwVfpRegister dst, |
| 1387 | const Register base, |
| 1388 | int offset, |
| 1389 | const Condition cond) { |
| 1390 | // Ddst = MEM(Rbase + offset). |
| 1391 | // Instruction details available in ARM DDI 0406A, A8-628. |
| 1392 | // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) | |
| 1393 | // Vdst(15-12) | 1011(11-8) | offset |
| 1394 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1395 | ASSERT(offset % 4 == 0); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1396 | ASSERT((offset / 4) < 256); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1397 | emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 | |
| 1398 | 0xB*B8 | ((offset / 4) & 255)); |
| 1399 | } |
| 1400 | |
| 1401 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1402 | void Assembler::vldr(const SwVfpRegister dst, |
| 1403 | const Register base, |
| 1404 | int offset, |
| 1405 | const Condition cond) { |
| 1406 | // Sdst = MEM(Rbase + offset). |
| 1407 | // Instruction details available in ARM DDI 0406A, A8-628. |
| 1408 | // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) | |
| 1409 | // Vdst(15-12) | 1010(11-8) | offset |
| 1410 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1411 | ASSERT(offset % 4 == 0); |
| 1412 | ASSERT((offset / 4) < 256); |
| 1413 | emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 | |
| 1414 | 0xA*B8 | ((offset / 4) & 255)); |
| 1415 | } |
| 1416 | |
| 1417 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1418 | void Assembler::vstr(const DwVfpRegister src, |
| 1419 | const Register base, |
| 1420 | int offset, |
| 1421 | const Condition cond) { |
| 1422 | // MEM(Rbase + offset) = Dsrc. |
| 1423 | // Instruction details available in ARM DDI 0406A, A8-786. |
| 1424 | // cond(31-28) | 1101(27-24)| 1000(23-20) | | Rbase(19-16) | |
| 1425 | // Vsrc(15-12) | 1011(11-8) | (offset/4) |
| 1426 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1427 | ASSERT(offset % 4 == 0); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1428 | ASSERT((offset / 4) < 256); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1429 | emit(cond | 0xD8*B20 | base.code()*B16 | src.code()*B12 | |
| 1430 | 0xB*B8 | ((offset / 4) & 255)); |
| 1431 | } |
| 1432 | |
| 1433 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1434 | void Assembler::vmov(const DwVfpRegister dst, |
| 1435 | const Register src1, |
| 1436 | const Register src2, |
| 1437 | const Condition cond) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1438 | // Dm = <Rt,Rt2>. |
| 1439 | // Instruction details available in ARM DDI 0406A, A8-646. |
| 1440 | // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) | |
| 1441 | // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm |
| 1442 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1443 | ASSERT(!src1.is(pc) && !src2.is(pc)); |
| 1444 | emit(cond | 0xC*B24 | B22 | src2.code()*B16 | |
| 1445 | src1.code()*B12 | 0xB*B8 | B4 | dst.code()); |
| 1446 | } |
| 1447 | |
| 1448 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1449 | void Assembler::vmov(const Register dst1, |
| 1450 | const Register dst2, |
| 1451 | const DwVfpRegister src, |
| 1452 | const Condition cond) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1453 | // <Rt,Rt2> = Dm. |
| 1454 | // Instruction details available in ARM DDI 0406A, A8-646. |
| 1455 | // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) | |
| 1456 | // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm |
| 1457 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1458 | ASSERT(!dst1.is(pc) && !dst2.is(pc)); |
| 1459 | emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 | |
| 1460 | dst1.code()*B12 | 0xB*B8 | B4 | src.code()); |
| 1461 | } |
| 1462 | |
| 1463 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1464 | void Assembler::vmov(const SwVfpRegister dst, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1465 | const Register src, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1466 | const Condition cond) { |
| 1467 | // Sn = Rt. |
| 1468 | // Instruction details available in ARM DDI 0406A, A8-642. |
| 1469 | // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) | |
| 1470 | // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0) |
| 1471 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1472 | ASSERT(!src.is(pc)); |
| 1473 | emit(cond | 0xE*B24 | (dst.code() >> 1)*B16 | |
| 1474 | src.code()*B12 | 0xA*B8 | (0x1 & dst.code())*B7 | B4); |
| 1475 | } |
| 1476 | |
| 1477 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1478 | void Assembler::vmov(const Register dst, |
| 1479 | const SwVfpRegister src, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1480 | const Condition cond) { |
| 1481 | // Rt = Sn. |
| 1482 | // Instruction details available in ARM DDI 0406A, A8-642. |
| 1483 | // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) | |
| 1484 | // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0) |
| 1485 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1486 | ASSERT(!dst.is(pc)); |
| 1487 | emit(cond | 0xE*B24 | B20 | (src.code() >> 1)*B16 | |
| 1488 | dst.code()*B12 | 0xA*B8 | (0x1 & src.code())*B7 | B4); |
| 1489 | } |
| 1490 | |
| 1491 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1492 | // Type of data to read from or write to VFP register. |
| 1493 | // Used as specifier in generic vcvt instruction. |
| 1494 | enum VFPType { S32, U32, F32, F64 }; |
| 1495 | |
| 1496 | |
| 1497 | static bool IsSignedVFPType(VFPType type) { |
| 1498 | switch (type) { |
| 1499 | case S32: |
| 1500 | return true; |
| 1501 | case U32: |
| 1502 | return false; |
| 1503 | default: |
| 1504 | UNREACHABLE(); |
| 1505 | return false; |
| 1506 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1510 | static bool IsIntegerVFPType(VFPType type) { |
| 1511 | switch (type) { |
| 1512 | case S32: |
| 1513 | case U32: |
| 1514 | return true; |
| 1515 | case F32: |
| 1516 | case F64: |
| 1517 | return false; |
| 1518 | default: |
| 1519 | UNREACHABLE(); |
| 1520 | return false; |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | |
| 1525 | static bool IsDoubleVFPType(VFPType type) { |
| 1526 | switch (type) { |
| 1527 | case F32: |
| 1528 | return false; |
| 1529 | case F64: |
| 1530 | return true; |
| 1531 | default: |
| 1532 | UNREACHABLE(); |
| 1533 | return false; |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | |
| 1538 | // Depending on split_last_bit split binary representation of reg_code into Vm:M |
| 1539 | // or M:Vm form (where M is single bit). |
| 1540 | static void SplitRegCode(bool split_last_bit, |
| 1541 | int reg_code, |
| 1542 | int* vm, |
| 1543 | int* m) { |
| 1544 | if (split_last_bit) { |
| 1545 | *m = reg_code & 0x1; |
| 1546 | *vm = reg_code >> 1; |
| 1547 | } else { |
| 1548 | *m = (reg_code & 0x10) >> 4; |
| 1549 | *vm = reg_code & 0x0F; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | |
| 1554 | // Encode vcvt.src_type.dst_type instruction. |
| 1555 | static Instr EncodeVCVT(const VFPType dst_type, |
| 1556 | const int dst_code, |
| 1557 | const VFPType src_type, |
| 1558 | const int src_code, |
| 1559 | const Condition cond) { |
| 1560 | if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) { |
| 1561 | // Conversion between IEEE floating point and 32-bit integer. |
| 1562 | // Instruction details available in ARM DDI 0406B, A8.6.295. |
| 1563 | // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) | |
| 1564 | // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0) |
| 1565 | ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type)); |
| 1566 | |
| 1567 | int sz, opc2, D, Vd, M, Vm, op; |
| 1568 | |
| 1569 | if (IsIntegerVFPType(dst_type)) { |
| 1570 | opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4; |
| 1571 | sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0; |
| 1572 | op = 1; // round towards zero |
| 1573 | SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M); |
| 1574 | SplitRegCode(true, dst_code, &Vd, &D); |
| 1575 | } else { |
| 1576 | ASSERT(IsIntegerVFPType(src_type)); |
| 1577 | |
| 1578 | opc2 = 0x0; |
| 1579 | sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0; |
| 1580 | op = IsSignedVFPType(src_type) ? 0x1 : 0x0; |
| 1581 | SplitRegCode(true, src_code, &Vm, &M); |
| 1582 | SplitRegCode(!IsDoubleVFPType(dst_type), dst_code, &Vd, &D); |
| 1583 | } |
| 1584 | |
| 1585 | return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 | |
| 1586 | Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm); |
| 1587 | } else { |
| 1588 | // Conversion between IEEE double and single precision. |
| 1589 | // Instruction details available in ARM DDI 0406B, A8.6.298. |
| 1590 | // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) | |
| 1591 | // Vd(15-12) | 101(11-9) | sz(8) | 1(7) | 1(6) | M(5) | 0(4) | Vm(3-0) |
| 1592 | int sz, D, Vd, M, Vm; |
| 1593 | |
| 1594 | ASSERT(IsDoubleVFPType(dst_type) != IsDoubleVFPType(src_type)); |
| 1595 | sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0; |
| 1596 | SplitRegCode(IsDoubleVFPType(src_type), dst_code, &Vd, &D); |
| 1597 | SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M); |
| 1598 | |
| 1599 | return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 | |
| 1600 | Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm); |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | |
| 1605 | void Assembler::vcvt_f64_s32(const DwVfpRegister dst, |
| 1606 | const SwVfpRegister src, |
| 1607 | const Condition cond) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1608 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1609 | emit(EncodeVCVT(F64, dst.code(), S32, src.code(), cond)); |
| 1610 | } |
| 1611 | |
| 1612 | |
| 1613 | void Assembler::vcvt_f32_s32(const SwVfpRegister dst, |
| 1614 | const SwVfpRegister src, |
| 1615 | const Condition cond) { |
| 1616 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1617 | emit(EncodeVCVT(F32, dst.code(), S32, src.code(), cond)); |
| 1618 | } |
| 1619 | |
| 1620 | |
| 1621 | void Assembler::vcvt_f64_u32(const DwVfpRegister dst, |
| 1622 | const SwVfpRegister src, |
| 1623 | const Condition cond) { |
| 1624 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1625 | emit(EncodeVCVT(F64, dst.code(), U32, src.code(), cond)); |
| 1626 | } |
| 1627 | |
| 1628 | |
| 1629 | void Assembler::vcvt_s32_f64(const SwVfpRegister dst, |
| 1630 | const DwVfpRegister src, |
| 1631 | const Condition cond) { |
| 1632 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1633 | emit(EncodeVCVT(S32, dst.code(), F64, src.code(), cond)); |
| 1634 | } |
| 1635 | |
| 1636 | |
| 1637 | void Assembler::vcvt_u32_f64(const SwVfpRegister dst, |
| 1638 | const DwVfpRegister src, |
| 1639 | const Condition cond) { |
| 1640 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1641 | emit(EncodeVCVT(U32, dst.code(), F64, src.code(), cond)); |
| 1642 | } |
| 1643 | |
| 1644 | |
| 1645 | void Assembler::vcvt_f64_f32(const DwVfpRegister dst, |
| 1646 | const SwVfpRegister src, |
| 1647 | const Condition cond) { |
| 1648 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1649 | emit(EncodeVCVT(F64, dst.code(), F32, src.code(), cond)); |
| 1650 | } |
| 1651 | |
| 1652 | |
| 1653 | void Assembler::vcvt_f32_f64(const SwVfpRegister dst, |
| 1654 | const DwVfpRegister src, |
| 1655 | const Condition cond) { |
| 1656 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1657 | emit(EncodeVCVT(F32, dst.code(), F64, src.code(), cond)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1661 | void Assembler::vadd(const DwVfpRegister dst, |
| 1662 | const DwVfpRegister src1, |
| 1663 | const DwVfpRegister src2, |
| 1664 | const Condition cond) { |
| 1665 | // Dd = vadd(Dn, Dm) double precision floating point addition. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1666 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 1667 | // Instruction details available in ARM DDI 0406A, A8-536. |
| 1668 | // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) | |
| 1669 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0) |
| 1670 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1671 | emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 | |
| 1672 | dst.code()*B12 | 0x5*B9 | B8 | src2.code()); |
| 1673 | } |
| 1674 | |
| 1675 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1676 | void Assembler::vsub(const DwVfpRegister dst, |
| 1677 | const DwVfpRegister src1, |
| 1678 | const DwVfpRegister src2, |
| 1679 | const Condition cond) { |
| 1680 | // Dd = vsub(Dn, Dm) double precision floating point subtraction. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1681 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 1682 | // Instruction details available in ARM DDI 0406A, A8-784. |
| 1683 | // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) | |
| 1684 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 1(6) | M=?(5) | 0(4) | Vm(3-0) |
| 1685 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1686 | emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 | |
| 1687 | dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code()); |
| 1688 | } |
| 1689 | |
| 1690 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1691 | void Assembler::vmul(const DwVfpRegister dst, |
| 1692 | const DwVfpRegister src1, |
| 1693 | const DwVfpRegister src2, |
| 1694 | const Condition cond) { |
| 1695 | // Dd = vmul(Dn, Dm) double precision floating point multiplication. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1696 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 1697 | // Instruction details available in ARM DDI 0406A, A8-784. |
| 1698 | // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) | |
| 1699 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0) |
| 1700 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1701 | emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 | |
| 1702 | dst.code()*B12 | 0x5*B9 | B8 | src2.code()); |
| 1703 | } |
| 1704 | |
| 1705 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1706 | void Assembler::vdiv(const DwVfpRegister dst, |
| 1707 | const DwVfpRegister src1, |
| 1708 | const DwVfpRegister src2, |
| 1709 | const Condition cond) { |
| 1710 | // Dd = vdiv(Dn, Dm) double precision floating point division. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1711 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 1712 | // Instruction details available in ARM DDI 0406A, A8-584. |
| 1713 | // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) | |
| 1714 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=? | 0(6) | M=?(5) | 0(4) | Vm(3-0) |
| 1715 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1716 | emit(cond | 0xE*B24 | B23 | src1.code()*B16 | |
| 1717 | dst.code()*B12 | 0x5*B9 | B8 | src2.code()); |
| 1718 | } |
| 1719 | |
| 1720 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1721 | void Assembler::vcmp(const DwVfpRegister src1, |
| 1722 | const DwVfpRegister src2, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1723 | const SBit s, |
| 1724 | const Condition cond) { |
| 1725 | // vcmp(Dd, Dm) double precision floating point comparison. |
| 1726 | // Instruction details available in ARM DDI 0406A, A8-570. |
| 1727 | // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) | |
| 1728 | // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=? | 1(6) | M(5)=? | 0(4) | Vm(3-0) |
| 1729 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1730 | emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 | |
| 1731 | src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code()); |
| 1732 | } |
| 1733 | |
| 1734 | |
| 1735 | void Assembler::vmrs(Register dst, Condition cond) { |
| 1736 | // Instruction details available in ARM DDI 0406A, A8-652. |
| 1737 | // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) | |
| 1738 | // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0) |
| 1739 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1740 | emit(cond | 0xE*B24 | 0xF*B20 | B16 | |
| 1741 | dst.code()*B12 | 0xA*B8 | B4); |
| 1742 | } |
| 1743 | |
| 1744 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1745 | // Pseudo instructions. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1746 | void Assembler::nop(int type) { |
| 1747 | // This is mov rx, rx. |
| 1748 | ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop. |
| 1749 | emit(al | 13*B21 | type*B12 | type); |
| 1750 | } |
| 1751 | |
| 1752 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1753 | void Assembler::lea(Register dst, |
| 1754 | const MemOperand& x, |
| 1755 | SBit s, |
| 1756 | Condition cond) { |
| 1757 | int am = x.am_; |
| 1758 | if (!x.rm_.is_valid()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1759 | // Immediate offset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1760 | if ((am & P) == 0) // post indexing |
| 1761 | mov(dst, Operand(x.rn_), s, cond); |
| 1762 | else if ((am & U) == 0) // negative indexing |
| 1763 | sub(dst, x.rn_, Operand(x.offset_), s, cond); |
| 1764 | else |
| 1765 | add(dst, x.rn_, Operand(x.offset_), s, cond); |
| 1766 | } else { |
| 1767 | // Register offset (shift_imm_ and shift_op_ are 0) or scaled |
| 1768 | // register offset the constructors make sure than both shift_imm_ |
| 1769 | // and shift_op_ are initialized. |
| 1770 | ASSERT(!x.rm_.is(pc)); |
| 1771 | if ((am & P) == 0) // post indexing |
| 1772 | mov(dst, Operand(x.rn_), s, cond); |
| 1773 | else if ((am & U) == 0) // negative indexing |
| 1774 | sub(dst, x.rn_, Operand(x.rm_, x.shift_op_, x.shift_imm_), s, cond); |
| 1775 | else |
| 1776 | add(dst, x.rn_, Operand(x.rm_, x.shift_op_, x.shift_imm_), s, cond); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1781 | bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) { |
| 1782 | uint32_t dummy1; |
| 1783 | uint32_t dummy2; |
| 1784 | return fits_shifter(imm32, &dummy1, &dummy2, NULL); |
| 1785 | } |
| 1786 | |
| 1787 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame^] | 1788 | void Assembler::BlockConstPoolFor(int instructions) { |
| 1789 | BlockConstPoolBefore(pc_offset() + instructions * kInstrSize); |
| 1790 | } |
| 1791 | |
| 1792 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1793 | // Debugging. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1794 | void Assembler::RecordJSReturn() { |
| 1795 | WriteRecordedPositions(); |
| 1796 | CheckBuffer(); |
| 1797 | RecordRelocInfo(RelocInfo::JS_RETURN); |
| 1798 | } |
| 1799 | |
| 1800 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1801 | void Assembler::RecordComment(const char* msg) { |
| 1802 | if (FLAG_debug_code) { |
| 1803 | CheckBuffer(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1804 | RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | |
| 1809 | void Assembler::RecordPosition(int pos) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1810 | if (pos == RelocInfo::kNoPosition) return; |
| 1811 | ASSERT(pos >= 0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1812 | current_position_ = pos; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | |
| 1816 | void Assembler::RecordStatementPosition(int pos) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1817 | if (pos == RelocInfo::kNoPosition) return; |
| 1818 | ASSERT(pos >= 0); |
| 1819 | current_statement_position_ = pos; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | |
| 1823 | void Assembler::WriteRecordedPositions() { |
| 1824 | // Write the statement position if it is different from what was written last |
| 1825 | // time. |
| 1826 | if (current_statement_position_ != written_statement_position_) { |
| 1827 | CheckBuffer(); |
| 1828 | RecordRelocInfo(RelocInfo::STATEMENT_POSITION, current_statement_position_); |
| 1829 | written_statement_position_ = current_statement_position_; |
| 1830 | } |
| 1831 | |
| 1832 | // 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] | 1833 | // also different from the written statement position. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1834 | if (current_position_ != written_position_ && |
| 1835 | current_position_ != written_statement_position_) { |
| 1836 | CheckBuffer(); |
| 1837 | RecordRelocInfo(RelocInfo::POSITION, current_position_); |
| 1838 | written_position_ = current_position_; |
| 1839 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | |
| 1843 | void Assembler::GrowBuffer() { |
| 1844 | if (!own_buffer_) FATAL("external code buffer is too small"); |
| 1845 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1846 | // Compute new buffer size. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1847 | CodeDesc desc; // the new buffer |
| 1848 | if (buffer_size_ < 4*KB) { |
| 1849 | desc.buffer_size = 4*KB; |
| 1850 | } else if (buffer_size_ < 1*MB) { |
| 1851 | desc.buffer_size = 2*buffer_size_; |
| 1852 | } else { |
| 1853 | desc.buffer_size = buffer_size_ + 1*MB; |
| 1854 | } |
| 1855 | CHECK_GT(desc.buffer_size, 0); // no overflow |
| 1856 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1857 | // Setup new buffer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1858 | desc.buffer = NewArray<byte>(desc.buffer_size); |
| 1859 | |
| 1860 | desc.instr_size = pc_offset(); |
| 1861 | desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos(); |
| 1862 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1863 | // Copy the data. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1864 | int pc_delta = desc.buffer - buffer_; |
| 1865 | int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_); |
| 1866 | memmove(desc.buffer, buffer_, desc.instr_size); |
| 1867 | memmove(reloc_info_writer.pos() + rc_delta, |
| 1868 | reloc_info_writer.pos(), desc.reloc_size); |
| 1869 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1870 | // Switch buffers. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1871 | DeleteArray(buffer_); |
| 1872 | buffer_ = desc.buffer; |
| 1873 | buffer_size_ = desc.buffer_size; |
| 1874 | pc_ += pc_delta; |
| 1875 | reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta, |
| 1876 | reloc_info_writer.last_pc() + pc_delta); |
| 1877 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1878 | // 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] | 1879 | // 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] | 1880 | // to relocate any emitted relocation entries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1881 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1882 | // Relocate pending relocation entries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1883 | for (int i = 0; i < num_prinfo_; i++) { |
| 1884 | RelocInfo& rinfo = prinfo_[i]; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1885 | ASSERT(rinfo.rmode() != RelocInfo::COMMENT && |
| 1886 | rinfo.rmode() != RelocInfo::POSITION); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1887 | if (rinfo.rmode() != RelocInfo::JS_RETURN) { |
| 1888 | rinfo.set_pc(rinfo.pc() + pc_delta); |
| 1889 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1894 | void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1895 | RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1896 | if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::STATEMENT_POSITION) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1897 | // Adjust code for new modes. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1898 | ASSERT(RelocInfo::IsJSReturn(rmode) |
| 1899 | || RelocInfo::IsComment(rmode) |
| 1900 | || RelocInfo::IsPosition(rmode)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1901 | // These modes do not need an entry in the constant pool. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1902 | } else { |
| 1903 | ASSERT(num_prinfo_ < kMaxNumPRInfo); |
| 1904 | prinfo_[num_prinfo_++] = rinfo; |
| 1905 | // 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] | 1906 | // instruction for which we just recorded relocation info. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1907 | BlockConstPoolBefore(pc_offset() + kInstrSize); |
| 1908 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1909 | if (rinfo.rmode() != RelocInfo::NONE) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1910 | // Don't record external references unless the heap will be serialized. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1911 | if (rmode == RelocInfo::EXTERNAL_REFERENCE) { |
| 1912 | #ifdef DEBUG |
| 1913 | if (!Serializer::enabled()) { |
| 1914 | Serializer::TooLateToEnableNow(); |
| 1915 | } |
| 1916 | #endif |
| 1917 | if (!Serializer::enabled() && !FLAG_debug_code) { |
| 1918 | return; |
| 1919 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1920 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1921 | ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here |
| 1922 | reloc_info_writer.Write(&rinfo); |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | |
| 1927 | void Assembler::CheckConstPool(bool force_emit, bool require_jump) { |
| 1928 | // Calculate the offset of the next check. It will be overwritten |
| 1929 | // when a const pool is generated or when const pools are being |
| 1930 | // blocked for a specific range. |
| 1931 | next_buffer_check_ = pc_offset() + kCheckConstInterval; |
| 1932 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1933 | // 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] | 1934 | if (num_prinfo_ == 0) return; |
| 1935 | |
| 1936 | // We emit a constant pool at regular intervals of about kDistBetweenPools |
| 1937 | // or when requested by parameter force_emit (e.g. after each function). |
| 1938 | // We prefer not to emit a jump unless the max distance is reached or if we |
| 1939 | // are running low on slots, which can happen if a lot of constants are being |
| 1940 | // emitted (e.g. --debug-code and many static references). |
| 1941 | int dist = pc_offset() - last_const_pool_end_; |
| 1942 | if (!force_emit && dist < kMaxDistBetweenPools && |
| 1943 | (require_jump || dist < kDistBetweenPools) && |
| 1944 | // TODO(1236125): Cleanup the "magic" number below. We know that |
| 1945 | // the code generation will test every kCheckConstIntervalInst. |
| 1946 | // Thus we are safe as long as we generate less than 7 constant |
| 1947 | // entries per instruction. |
| 1948 | (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) { |
| 1949 | return; |
| 1950 | } |
| 1951 | |
| 1952 | // If we did not return by now, we need to emit the constant pool soon. |
| 1953 | |
| 1954 | // However, some small sequences of instructions must not be broken up by the |
| 1955 | // insertion of a constant pool; such sequences are protected by setting |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1956 | // either const_pool_blocked_nesting_ or no_const_pool_before_, which are |
| 1957 | // both checked here. Also, recursive calls to CheckConstPool are blocked by |
| 1958 | // no_const_pool_before_. |
| 1959 | 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] | 1960 | // Emission is currently blocked; make sure we try again as soon as |
| 1961 | // possible. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1962 | if (const_pool_blocked_nesting_ > 0) { |
| 1963 | next_buffer_check_ = pc_offset() + kInstrSize; |
| 1964 | } else { |
| 1965 | next_buffer_check_ = no_const_pool_before_; |
| 1966 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1967 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1968 | // 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] | 1969 | ASSERT(!force_emit); |
| 1970 | return; |
| 1971 | } |
| 1972 | |
| 1973 | int jump_instr = require_jump ? kInstrSize : 0; |
| 1974 | |
| 1975 | // Check that the code buffer is large enough before emitting the constant |
| 1976 | // pool and relocation information (include the jump over the pool and the |
| 1977 | // constant pool marker). |
| 1978 | int max_needed_space = |
| 1979 | jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize); |
| 1980 | while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer(); |
| 1981 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1982 | // Block recursive calls to CheckConstPool. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1983 | BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize + |
| 1984 | num_prinfo_*kInstrSize); |
| 1985 | // Don't bother to check for the emit calls below. |
| 1986 | next_buffer_check_ = no_const_pool_before_; |
| 1987 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1988 | // Emit jump over constant pool if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1989 | Label after_pool; |
| 1990 | if (require_jump) b(&after_pool); |
| 1991 | |
| 1992 | RecordComment("[ Constant Pool"); |
| 1993 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1994 | // Put down constant pool marker "Undefined instruction" as specified by |
| 1995 | // A3.1 Instruction set encoding. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1996 | emit(0x03000000 | num_prinfo_); |
| 1997 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1998 | // Emit constant pool entries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1999 | for (int i = 0; i < num_prinfo_; i++) { |
| 2000 | RelocInfo& rinfo = prinfo_[i]; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2001 | ASSERT(rinfo.rmode() != RelocInfo::COMMENT && |
| 2002 | rinfo.rmode() != RelocInfo::POSITION && |
| 2003 | rinfo.rmode() != RelocInfo::STATEMENT_POSITION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2004 | Instr instr = instr_at(rinfo.pc()); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 2005 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2006 | // Instruction to patch must be a ldr/str [pc, #offset]. |
| 2007 | // 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] | 2008 | ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | Off12Mask)) == |
| 2009 | (2*B25 | P | U | pc.code()*B16)); |
| 2010 | int delta = pc_ - rinfo.pc() - 8; |
| 2011 | ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32 |
| 2012 | if (delta < 0) { |
| 2013 | instr &= ~U; |
| 2014 | delta = -delta; |
| 2015 | } |
| 2016 | ASSERT(is_uint12(delta)); |
| 2017 | instr_at_put(rinfo.pc(), instr + delta); |
| 2018 | emit(rinfo.data()); |
| 2019 | } |
| 2020 | num_prinfo_ = 0; |
| 2021 | last_const_pool_end_ = pc_offset(); |
| 2022 | |
| 2023 | RecordComment("]"); |
| 2024 | |
| 2025 | if (after_pool.is_linked()) { |
| 2026 | bind(&after_pool); |
| 2027 | } |
| 2028 | |
| 2029 | // Since a constant pool was just emitted, move the check offset forward by |
| 2030 | // the standard interval. |
| 2031 | next_buffer_check_ = pc_offset() + kCheckConstInterval; |
| 2032 | } |
| 2033 | |
| 2034 | |
| 2035 | } } // namespace v8::internal |