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 | |
| 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); |
| 806 | } |
| 807 | |
| 808 | |
| 809 | void Assembler::bl(int branch_offset, Condition cond) { |
| 810 | ASSERT((branch_offset & 3) == 0); |
| 811 | int imm24 = branch_offset >> 2; |
| 812 | ASSERT(is_int24(imm24)); |
| 813 | emit(cond | B27 | B25 | B24 | (imm24 & Imm24Mask)); |
| 814 | } |
| 815 | |
| 816 | |
| 817 | void Assembler::blx(int branch_offset) { // v5 and above |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 818 | WriteRecordedPositions(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 819 | ASSERT((branch_offset & 1) == 0); |
| 820 | int h = ((branch_offset & 2) >> 1)*B24; |
| 821 | int imm24 = branch_offset >> 2; |
| 822 | ASSERT(is_int24(imm24)); |
| 823 | emit(15 << 28 | B27 | B25 | h | (imm24 & Imm24Mask)); |
| 824 | } |
| 825 | |
| 826 | |
| 827 | void Assembler::blx(Register target, Condition cond) { // v5 and above |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 828 | WriteRecordedPositions(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 829 | ASSERT(!target.is(pc)); |
| 830 | emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | 3*B4 | target.code()); |
| 831 | } |
| 832 | |
| 833 | |
| 834 | 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] | 835 | WriteRecordedPositions(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 836 | ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged |
| 837 | emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | B4 | target.code()); |
| 838 | } |
| 839 | |
| 840 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 841 | // Data-processing instructions. |
| 842 | |
| 843 | // UBFX <Rd>,<Rn>,#<lsb>,#<width - 1> |
| 844 | // Instruction details available in ARM DDI 0406A, A8-464. |
| 845 | // cond(31-28) | 01111(27-23)| 1(22) | 1(21) | widthm1(20-16) | |
| 846 | // Rd(15-12) | lsb(11-7) | 101(6-4) | Rn(3-0) |
| 847 | void Assembler::ubfx(Register dst, Register src1, const Operand& src2, |
| 848 | const Operand& src3, Condition cond) { |
| 849 | ASSERT(!src2.rm_.is_valid() && !src3.rm_.is_valid()); |
| 850 | ASSERT(static_cast<uint32_t>(src2.imm32_) <= 0x1f); |
| 851 | ASSERT(static_cast<uint32_t>(src3.imm32_) <= 0x1f); |
| 852 | emit(cond | 0x3F*B21 | src3.imm32_*B16 | |
| 853 | dst.code()*B12 | src2.imm32_*B7 | 0x5*B4 | src1.code()); |
| 854 | } |
| 855 | |
| 856 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 857 | void Assembler::and_(Register dst, Register src1, const Operand& src2, |
| 858 | SBit s, Condition cond) { |
| 859 | addrmod1(cond | 0*B21 | s, src1, dst, src2); |
| 860 | } |
| 861 | |
| 862 | |
| 863 | void Assembler::eor(Register dst, Register src1, const Operand& src2, |
| 864 | SBit s, Condition cond) { |
| 865 | addrmod1(cond | 1*B21 | s, src1, dst, src2); |
| 866 | } |
| 867 | |
| 868 | |
| 869 | void Assembler::sub(Register dst, Register src1, const Operand& src2, |
| 870 | SBit s, Condition cond) { |
| 871 | addrmod1(cond | 2*B21 | s, src1, dst, src2); |
| 872 | } |
| 873 | |
| 874 | |
| 875 | void Assembler::rsb(Register dst, Register src1, const Operand& src2, |
| 876 | SBit s, Condition cond) { |
| 877 | addrmod1(cond | 3*B21 | s, src1, dst, src2); |
| 878 | } |
| 879 | |
| 880 | |
| 881 | void Assembler::add(Register dst, Register src1, const Operand& src2, |
| 882 | SBit s, Condition cond) { |
| 883 | addrmod1(cond | 4*B21 | s, src1, dst, src2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 884 | |
| 885 | // Eliminate pattern: push(r), pop() |
| 886 | // str(src, MemOperand(sp, 4, NegPreIndex), al); |
| 887 | // add(sp, sp, Operand(kPointerSize)); |
| 888 | // Both instructions can be eliminated. |
| 889 | int pattern_size = 2 * kInstrSize; |
| 890 | if (FLAG_push_pop_elimination && |
| 891 | last_bound_pos_ <= (pc_offset() - pattern_size) && |
| 892 | reloc_info_writer.last_pc() <= (pc_ - pattern_size) && |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 893 | // Pattern. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 894 | instr_at(pc_ - 1 * kInstrSize) == kPopInstruction && |
| 895 | (instr_at(pc_ - 2 * kInstrSize) & ~RdMask) == kPushRegPattern) { |
| 896 | pc_ -= 2 * kInstrSize; |
| 897 | if (FLAG_print_push_pop_elimination) { |
| 898 | PrintF("%x push(reg)/pop() eliminated\n", pc_offset()); |
| 899 | } |
| 900 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | |
| 904 | void Assembler::adc(Register dst, Register src1, const Operand& src2, |
| 905 | SBit s, Condition cond) { |
| 906 | addrmod1(cond | 5*B21 | s, src1, dst, src2); |
| 907 | } |
| 908 | |
| 909 | |
| 910 | void Assembler::sbc(Register dst, Register src1, const Operand& src2, |
| 911 | SBit s, Condition cond) { |
| 912 | addrmod1(cond | 6*B21 | s, src1, dst, src2); |
| 913 | } |
| 914 | |
| 915 | |
| 916 | void Assembler::rsc(Register dst, Register src1, const Operand& src2, |
| 917 | SBit s, Condition cond) { |
| 918 | addrmod1(cond | 7*B21 | s, src1, dst, src2); |
| 919 | } |
| 920 | |
| 921 | |
| 922 | void Assembler::tst(Register src1, const Operand& src2, Condition cond) { |
| 923 | addrmod1(cond | 8*B21 | S, src1, r0, src2); |
| 924 | } |
| 925 | |
| 926 | |
| 927 | void Assembler::teq(Register src1, const Operand& src2, Condition cond) { |
| 928 | addrmod1(cond | 9*B21 | S, src1, r0, src2); |
| 929 | } |
| 930 | |
| 931 | |
| 932 | void Assembler::cmp(Register src1, const Operand& src2, Condition cond) { |
| 933 | addrmod1(cond | 10*B21 | S, src1, r0, src2); |
| 934 | } |
| 935 | |
| 936 | |
| 937 | void Assembler::cmn(Register src1, const Operand& src2, Condition cond) { |
| 938 | addrmod1(cond | 11*B21 | S, src1, r0, src2); |
| 939 | } |
| 940 | |
| 941 | |
| 942 | void Assembler::orr(Register dst, Register src1, const Operand& src2, |
| 943 | SBit s, Condition cond) { |
| 944 | addrmod1(cond | 12*B21 | s, src1, dst, src2); |
| 945 | } |
| 946 | |
| 947 | |
| 948 | 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] | 949 | if (dst.is(pc)) { |
| 950 | WriteRecordedPositions(); |
| 951 | } |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame^] | 952 | // Don't allow nop instructions in the form mov rn, rn to be generated using |
| 953 | // the mov instruction. They must be generated using nop(int) |
| 954 | // pseudo instructions. |
| 955 | 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] | 956 | addrmod1(cond | 13*B21 | s, r0, dst, src); |
| 957 | } |
| 958 | |
| 959 | |
| 960 | void Assembler::bic(Register dst, Register src1, const Operand& src2, |
| 961 | SBit s, Condition cond) { |
| 962 | addrmod1(cond | 14*B21 | s, src1, dst, src2); |
| 963 | } |
| 964 | |
| 965 | |
| 966 | void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) { |
| 967 | addrmod1(cond | 15*B21 | s, r0, dst, src); |
| 968 | } |
| 969 | |
| 970 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 971 | // Multiply instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 972 | void Assembler::mla(Register dst, Register src1, Register src2, Register srcA, |
| 973 | SBit s, Condition cond) { |
| 974 | 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] | 975 | emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 | |
| 976 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 977 | } |
| 978 | |
| 979 | |
| 980 | void Assembler::mul(Register dst, Register src1, Register src2, |
| 981 | SBit s, Condition cond) { |
| 982 | ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 983 | // dst goes in bits 16-19 for this instruction! |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 984 | emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 985 | } |
| 986 | |
| 987 | |
| 988 | void Assembler::smlal(Register dstL, |
| 989 | Register dstH, |
| 990 | Register src1, |
| 991 | Register src2, |
| 992 | SBit s, |
| 993 | Condition cond) { |
| 994 | 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] | 995 | ASSERT(!dstL.is(dstH)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 996 | emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 | |
| 997 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | void Assembler::smull(Register dstL, |
| 1002 | Register dstH, |
| 1003 | Register src1, |
| 1004 | Register src2, |
| 1005 | SBit s, |
| 1006 | Condition cond) { |
| 1007 | 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] | 1008 | ASSERT(!dstL.is(dstH)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1009 | emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 | |
| 1010 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | void Assembler::umlal(Register dstL, |
| 1015 | Register dstH, |
| 1016 | Register src1, |
| 1017 | Register src2, |
| 1018 | SBit s, |
| 1019 | Condition cond) { |
| 1020 | 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] | 1021 | ASSERT(!dstL.is(dstH)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1022 | emit(cond | B23 | A | s | dstH.code()*B16 | dstL.code()*B12 | |
| 1023 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | void Assembler::umull(Register dstL, |
| 1028 | Register dstH, |
| 1029 | Register src1, |
| 1030 | Register src2, |
| 1031 | SBit s, |
| 1032 | Condition cond) { |
| 1033 | 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] | 1034 | ASSERT(!dstL.is(dstH)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1035 | emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1036 | src2.code()*B8 | B7 | B4 | src1.code()); |
| 1037 | } |
| 1038 | |
| 1039 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1040 | // Miscellaneous arithmetic instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1041 | void Assembler::clz(Register dst, Register src, Condition cond) { |
| 1042 | // v5 and above. |
| 1043 | ASSERT(!dst.is(pc) && !src.is(pc)); |
| 1044 | emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 | |
| 1045 | 15*B8 | B4 | src.code()); |
| 1046 | } |
| 1047 | |
| 1048 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1049 | // Status register access instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1050 | void Assembler::mrs(Register dst, SRegister s, Condition cond) { |
| 1051 | ASSERT(!dst.is(pc)); |
| 1052 | emit(cond | B24 | s | 15*B16 | dst.code()*B12); |
| 1053 | } |
| 1054 | |
| 1055 | |
| 1056 | void Assembler::msr(SRegisterFieldMask fields, const Operand& src, |
| 1057 | Condition cond) { |
| 1058 | ASSERT(fields >= B16 && fields < B20); // at least one field set |
| 1059 | Instr instr; |
| 1060 | if (!src.rm_.is_valid()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1061 | // Immediate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1062 | uint32_t rotate_imm; |
| 1063 | uint32_t immed_8; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1064 | if (MustUseIp(src.rmode_) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1065 | !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1066 | // Immediate operand cannot be encoded, load it first to register ip. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1067 | RecordRelocInfo(src.rmode_, src.imm32_); |
| 1068 | ldr(ip, MemOperand(pc, 0), cond); |
| 1069 | msr(fields, Operand(ip), cond); |
| 1070 | return; |
| 1071 | } |
| 1072 | instr = I | rotate_imm*B8 | immed_8; |
| 1073 | } else { |
| 1074 | ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed |
| 1075 | instr = src.rm_.code(); |
| 1076 | } |
| 1077 | emit(cond | instr | B24 | B21 | fields | 15*B12); |
| 1078 | } |
| 1079 | |
| 1080 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1081 | // Load/Store instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1082 | void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1083 | if (dst.is(pc)) { |
| 1084 | WriteRecordedPositions(); |
| 1085 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1086 | addrmod2(cond | B26 | L, dst, src); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1087 | |
| 1088 | // Eliminate pattern: push(r), pop(r) |
| 1089 | // str(r, MemOperand(sp, 4, NegPreIndex), al) |
| 1090 | // ldr(r, MemOperand(sp, 4, PostIndex), al) |
| 1091 | // Both instructions can be eliminated. |
| 1092 | int pattern_size = 2 * kInstrSize; |
| 1093 | if (FLAG_push_pop_elimination && |
| 1094 | last_bound_pos_ <= (pc_offset() - pattern_size) && |
| 1095 | reloc_info_writer.last_pc() <= (pc_ - pattern_size) && |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1096 | // Pattern. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1097 | instr_at(pc_ - 1 * kInstrSize) == (kPopRegPattern | dst.code() * B12) && |
| 1098 | instr_at(pc_ - 2 * kInstrSize) == (kPushRegPattern | dst.code() * B12)) { |
| 1099 | pc_ -= 2 * kInstrSize; |
| 1100 | if (FLAG_print_push_pop_elimination) { |
| 1101 | PrintF("%x push/pop (same reg) eliminated\n", pc_offset()); |
| 1102 | } |
| 1103 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | |
| 1107 | void Assembler::str(Register src, const MemOperand& dst, Condition cond) { |
| 1108 | addrmod2(cond | B26, src, dst); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1109 | |
| 1110 | // Eliminate pattern: pop(), push(r) |
| 1111 | // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al |
| 1112 | // -> str r, [sp, 0], al |
| 1113 | int pattern_size = 2 * kInstrSize; |
| 1114 | if (FLAG_push_pop_elimination && |
| 1115 | last_bound_pos_ <= (pc_offset() - pattern_size) && |
| 1116 | reloc_info_writer.last_pc() <= (pc_ - pattern_size) && |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1117 | // Pattern. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1118 | instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) && |
| 1119 | instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) { |
| 1120 | pc_ -= 2 * kInstrSize; |
| 1121 | emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12); |
| 1122 | if (FLAG_print_push_pop_elimination) { |
| 1123 | PrintF("%x pop()/push(reg) eliminated\n", pc_offset()); |
| 1124 | } |
| 1125 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | |
| 1129 | void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) { |
| 1130 | addrmod2(cond | B26 | B | L, dst, src); |
| 1131 | } |
| 1132 | |
| 1133 | |
| 1134 | void Assembler::strb(Register src, const MemOperand& dst, Condition cond) { |
| 1135 | addrmod2(cond | B26 | B, src, dst); |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) { |
| 1140 | addrmod3(cond | L | B7 | H | B4, dst, src); |
| 1141 | } |
| 1142 | |
| 1143 | |
| 1144 | void Assembler::strh(Register src, const MemOperand& dst, Condition cond) { |
| 1145 | addrmod3(cond | B7 | H | B4, src, dst); |
| 1146 | } |
| 1147 | |
| 1148 | |
| 1149 | void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) { |
| 1150 | addrmod3(cond | L | B7 | S6 | B4, dst, src); |
| 1151 | } |
| 1152 | |
| 1153 | |
| 1154 | void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) { |
| 1155 | addrmod3(cond | L | B7 | S6 | H | B4, dst, src); |
| 1156 | } |
| 1157 | |
| 1158 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1159 | // Load/Store multiple instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1160 | void Assembler::ldm(BlockAddrMode am, |
| 1161 | Register base, |
| 1162 | RegList dst, |
| 1163 | Condition cond) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1164 | // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1165 | ASSERT(base.is(sp) || (dst & sp.bit()) == 0); |
| 1166 | |
| 1167 | addrmod4(cond | B27 | am | L, base, dst); |
| 1168 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1169 | // 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] | 1170 | if (cond == al && (dst & pc.bit()) != 0) { |
| 1171 | // There is a slight chance that the ldm instruction was actually a call, |
| 1172 | // in which case it would be wrong to return into the constant pool; we |
| 1173 | // recognize this case by checking if the emission of the pool was blocked |
| 1174 | // at the pc of the ldm instruction by a mov lr, pc instruction; if this is |
| 1175 | // the case, we emit a jump over the pool. |
| 1176 | CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize); |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | |
| 1181 | void Assembler::stm(BlockAddrMode am, |
| 1182 | Register base, |
| 1183 | RegList src, |
| 1184 | Condition cond) { |
| 1185 | addrmod4(cond | B27 | am, base, src); |
| 1186 | } |
| 1187 | |
| 1188 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1189 | // Semaphore instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1190 | void Assembler::swp(Register dst, Register src, Register base, Condition cond) { |
| 1191 | ASSERT(!dst.is(pc) && !src.is(pc) && !base.is(pc)); |
| 1192 | ASSERT(!dst.is(base) && !src.is(base)); |
| 1193 | emit(cond | P | base.code()*B16 | dst.code()*B12 | |
| 1194 | B7 | B4 | src.code()); |
| 1195 | } |
| 1196 | |
| 1197 | |
| 1198 | void Assembler::swpb(Register dst, |
| 1199 | Register src, |
| 1200 | Register base, |
| 1201 | Condition cond) { |
| 1202 | ASSERT(!dst.is(pc) && !src.is(pc) && !base.is(pc)); |
| 1203 | ASSERT(!dst.is(base) && !src.is(base)); |
| 1204 | emit(cond | P | B | base.code()*B16 | dst.code()*B12 | |
| 1205 | B7 | B4 | src.code()); |
| 1206 | } |
| 1207 | |
| 1208 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1209 | // Exception-generating instructions and debugging support. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1210 | void Assembler::stop(const char* msg) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1211 | #ifndef __arm__ |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1212 | // The simulator handles these special instructions and stops execution. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1213 | emit(15 << 28 | ((intptr_t) msg)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1214 | #else // def __arm__ |
| 1215 | #ifdef CAN_USE_ARMV5_INSTRUCTIONS |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1216 | bkpt(0); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1217 | #else // ndef CAN_USE_ARMV5_INSTRUCTIONS |
| 1218 | swi(0x9f0001); |
| 1219 | #endif // ndef CAN_USE_ARMV5_INSTRUCTIONS |
| 1220 | #endif // def __arm__ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | |
| 1224 | void Assembler::bkpt(uint32_t imm16) { // v5 and above |
| 1225 | ASSERT(is_uint16(imm16)); |
| 1226 | emit(al | B24 | B21 | (imm16 >> 4)*B8 | 7*B4 | (imm16 & 0xf)); |
| 1227 | } |
| 1228 | |
| 1229 | |
| 1230 | void Assembler::swi(uint32_t imm24, Condition cond) { |
| 1231 | ASSERT(is_uint24(imm24)); |
| 1232 | emit(cond | 15*B24 | imm24); |
| 1233 | } |
| 1234 | |
| 1235 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1236 | // Coprocessor instructions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1237 | void Assembler::cdp(Coprocessor coproc, |
| 1238 | int opcode_1, |
| 1239 | CRegister crd, |
| 1240 | CRegister crn, |
| 1241 | CRegister crm, |
| 1242 | int opcode_2, |
| 1243 | Condition cond) { |
| 1244 | ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2)); |
| 1245 | emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 | |
| 1246 | crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code()); |
| 1247 | } |
| 1248 | |
| 1249 | |
| 1250 | void Assembler::cdp2(Coprocessor coproc, |
| 1251 | int opcode_1, |
| 1252 | CRegister crd, |
| 1253 | CRegister crn, |
| 1254 | CRegister crm, |
| 1255 | int opcode_2) { // v5 and above |
| 1256 | cdp(coproc, opcode_1, crd, crn, crm, opcode_2, static_cast<Condition>(nv)); |
| 1257 | } |
| 1258 | |
| 1259 | |
| 1260 | void Assembler::mcr(Coprocessor coproc, |
| 1261 | int opcode_1, |
| 1262 | Register rd, |
| 1263 | CRegister crn, |
| 1264 | CRegister crm, |
| 1265 | int opcode_2, |
| 1266 | Condition cond) { |
| 1267 | ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2)); |
| 1268 | emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 | |
| 1269 | rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code()); |
| 1270 | } |
| 1271 | |
| 1272 | |
| 1273 | void Assembler::mcr2(Coprocessor coproc, |
| 1274 | int opcode_1, |
| 1275 | Register rd, |
| 1276 | CRegister crn, |
| 1277 | CRegister crm, |
| 1278 | int opcode_2) { // v5 and above |
| 1279 | mcr(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv)); |
| 1280 | } |
| 1281 | |
| 1282 | |
| 1283 | void Assembler::mrc(Coprocessor coproc, |
| 1284 | int opcode_1, |
| 1285 | Register rd, |
| 1286 | CRegister crn, |
| 1287 | CRegister crm, |
| 1288 | int opcode_2, |
| 1289 | Condition cond) { |
| 1290 | ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2)); |
| 1291 | emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 | |
| 1292 | rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code()); |
| 1293 | } |
| 1294 | |
| 1295 | |
| 1296 | void Assembler::mrc2(Coprocessor coproc, |
| 1297 | int opcode_1, |
| 1298 | Register rd, |
| 1299 | CRegister crn, |
| 1300 | CRegister crm, |
| 1301 | int opcode_2) { // v5 and above |
| 1302 | mrc(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv)); |
| 1303 | } |
| 1304 | |
| 1305 | |
| 1306 | void Assembler::ldc(Coprocessor coproc, |
| 1307 | CRegister crd, |
| 1308 | const MemOperand& src, |
| 1309 | LFlag l, |
| 1310 | Condition cond) { |
| 1311 | addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src); |
| 1312 | } |
| 1313 | |
| 1314 | |
| 1315 | void Assembler::ldc(Coprocessor coproc, |
| 1316 | CRegister crd, |
| 1317 | Register rn, |
| 1318 | int option, |
| 1319 | LFlag l, |
| 1320 | Condition cond) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1321 | // Unindexed addressing. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1322 | ASSERT(is_uint8(option)); |
| 1323 | emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 | |
| 1324 | coproc*B8 | (option & 255)); |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | void Assembler::ldc2(Coprocessor coproc, |
| 1329 | CRegister crd, |
| 1330 | const MemOperand& src, |
| 1331 | LFlag l) { // v5 and above |
| 1332 | ldc(coproc, crd, src, l, static_cast<Condition>(nv)); |
| 1333 | } |
| 1334 | |
| 1335 | |
| 1336 | void Assembler::ldc2(Coprocessor coproc, |
| 1337 | CRegister crd, |
| 1338 | Register rn, |
| 1339 | int option, |
| 1340 | LFlag l) { // v5 and above |
| 1341 | ldc(coproc, crd, rn, option, l, static_cast<Condition>(nv)); |
| 1342 | } |
| 1343 | |
| 1344 | |
| 1345 | void Assembler::stc(Coprocessor coproc, |
| 1346 | CRegister crd, |
| 1347 | const MemOperand& dst, |
| 1348 | LFlag l, |
| 1349 | Condition cond) { |
| 1350 | addrmod5(cond | B27 | B26 | l | coproc*B8, crd, dst); |
| 1351 | } |
| 1352 | |
| 1353 | |
| 1354 | void Assembler::stc(Coprocessor coproc, |
| 1355 | CRegister crd, |
| 1356 | Register rn, |
| 1357 | int option, |
| 1358 | LFlag l, |
| 1359 | Condition cond) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1360 | // Unindexed addressing. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1361 | ASSERT(is_uint8(option)); |
| 1362 | emit(cond | B27 | B26 | U | l | rn.code()*B16 | crd.code()*B12 | |
| 1363 | coproc*B8 | (option & 255)); |
| 1364 | } |
| 1365 | |
| 1366 | |
| 1367 | void Assembler::stc2(Coprocessor |
| 1368 | coproc, CRegister crd, |
| 1369 | const MemOperand& dst, |
| 1370 | LFlag l) { // v5 and above |
| 1371 | stc(coproc, crd, dst, l, static_cast<Condition>(nv)); |
| 1372 | } |
| 1373 | |
| 1374 | |
| 1375 | void Assembler::stc2(Coprocessor coproc, |
| 1376 | CRegister crd, |
| 1377 | Register rn, |
| 1378 | int option, |
| 1379 | LFlag l) { // v5 and above |
| 1380 | stc(coproc, crd, rn, option, l, static_cast<Condition>(nv)); |
| 1381 | } |
| 1382 | |
| 1383 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1384 | // Support for VFP. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1385 | void Assembler::vldr(const DwVfpRegister dst, |
| 1386 | const Register base, |
| 1387 | int offset, |
| 1388 | const Condition cond) { |
| 1389 | // Ddst = MEM(Rbase + offset). |
| 1390 | // Instruction details available in ARM DDI 0406A, A8-628. |
| 1391 | // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) | |
| 1392 | // Vdst(15-12) | 1011(11-8) | offset |
| 1393 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1394 | ASSERT(offset % 4 == 0); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1395 | ASSERT((offset / 4) < 256); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1396 | emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 | |
| 1397 | 0xB*B8 | ((offset / 4) & 255)); |
| 1398 | } |
| 1399 | |
| 1400 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1401 | void Assembler::vldr(const SwVfpRegister dst, |
| 1402 | const Register base, |
| 1403 | int offset, |
| 1404 | const Condition cond) { |
| 1405 | // Sdst = MEM(Rbase + offset). |
| 1406 | // Instruction details available in ARM DDI 0406A, A8-628. |
| 1407 | // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) | |
| 1408 | // Vdst(15-12) | 1010(11-8) | offset |
| 1409 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1410 | ASSERT(offset % 4 == 0); |
| 1411 | ASSERT((offset / 4) < 256); |
| 1412 | emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 | |
| 1413 | 0xA*B8 | ((offset / 4) & 255)); |
| 1414 | } |
| 1415 | |
| 1416 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1417 | void Assembler::vstr(const DwVfpRegister src, |
| 1418 | const Register base, |
| 1419 | int offset, |
| 1420 | const Condition cond) { |
| 1421 | // MEM(Rbase + offset) = Dsrc. |
| 1422 | // Instruction details available in ARM DDI 0406A, A8-786. |
| 1423 | // cond(31-28) | 1101(27-24)| 1000(23-20) | | Rbase(19-16) | |
| 1424 | // Vsrc(15-12) | 1011(11-8) | (offset/4) |
| 1425 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1426 | ASSERT(offset % 4 == 0); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1427 | ASSERT((offset / 4) < 256); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1428 | emit(cond | 0xD8*B20 | base.code()*B16 | src.code()*B12 | |
| 1429 | 0xB*B8 | ((offset / 4) & 255)); |
| 1430 | } |
| 1431 | |
| 1432 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1433 | void Assembler::vmov(const DwVfpRegister dst, |
| 1434 | const Register src1, |
| 1435 | const Register src2, |
| 1436 | const Condition cond) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1437 | // Dm = <Rt,Rt2>. |
| 1438 | // Instruction details available in ARM DDI 0406A, A8-646. |
| 1439 | // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) | |
| 1440 | // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm |
| 1441 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1442 | ASSERT(!src1.is(pc) && !src2.is(pc)); |
| 1443 | emit(cond | 0xC*B24 | B22 | src2.code()*B16 | |
| 1444 | src1.code()*B12 | 0xB*B8 | B4 | dst.code()); |
| 1445 | } |
| 1446 | |
| 1447 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1448 | void Assembler::vmov(const Register dst1, |
| 1449 | const Register dst2, |
| 1450 | const DwVfpRegister src, |
| 1451 | const Condition cond) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1452 | // <Rt,Rt2> = Dm. |
| 1453 | // Instruction details available in ARM DDI 0406A, A8-646. |
| 1454 | // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) | |
| 1455 | // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm |
| 1456 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1457 | ASSERT(!dst1.is(pc) && !dst2.is(pc)); |
| 1458 | emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 | |
| 1459 | dst1.code()*B12 | 0xB*B8 | B4 | src.code()); |
| 1460 | } |
| 1461 | |
| 1462 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1463 | void Assembler::vmov(const SwVfpRegister dst, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1464 | const Register src, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1465 | const Condition cond) { |
| 1466 | // Sn = Rt. |
| 1467 | // Instruction details available in ARM DDI 0406A, A8-642. |
| 1468 | // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) | |
| 1469 | // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0) |
| 1470 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1471 | ASSERT(!src.is(pc)); |
| 1472 | emit(cond | 0xE*B24 | (dst.code() >> 1)*B16 | |
| 1473 | src.code()*B12 | 0xA*B8 | (0x1 & dst.code())*B7 | B4); |
| 1474 | } |
| 1475 | |
| 1476 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1477 | void Assembler::vmov(const Register dst, |
| 1478 | const SwVfpRegister src, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1479 | const Condition cond) { |
| 1480 | // Rt = Sn. |
| 1481 | // Instruction details available in ARM DDI 0406A, A8-642. |
| 1482 | // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) | |
| 1483 | // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0) |
| 1484 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1485 | ASSERT(!dst.is(pc)); |
| 1486 | emit(cond | 0xE*B24 | B20 | (src.code() >> 1)*B16 | |
| 1487 | dst.code()*B12 | 0xA*B8 | (0x1 & src.code())*B7 | B4); |
| 1488 | } |
| 1489 | |
| 1490 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1491 | // Type of data to read from or write to VFP register. |
| 1492 | // Used as specifier in generic vcvt instruction. |
| 1493 | enum VFPType { S32, U32, F32, F64 }; |
| 1494 | |
| 1495 | |
| 1496 | static bool IsSignedVFPType(VFPType type) { |
| 1497 | switch (type) { |
| 1498 | case S32: |
| 1499 | return true; |
| 1500 | case U32: |
| 1501 | return false; |
| 1502 | default: |
| 1503 | UNREACHABLE(); |
| 1504 | return false; |
| 1505 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1509 | static bool IsIntegerVFPType(VFPType type) { |
| 1510 | switch (type) { |
| 1511 | case S32: |
| 1512 | case U32: |
| 1513 | return true; |
| 1514 | case F32: |
| 1515 | case F64: |
| 1516 | return false; |
| 1517 | default: |
| 1518 | UNREACHABLE(); |
| 1519 | return false; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | |
| 1524 | static bool IsDoubleVFPType(VFPType type) { |
| 1525 | switch (type) { |
| 1526 | case F32: |
| 1527 | return false; |
| 1528 | case F64: |
| 1529 | return true; |
| 1530 | default: |
| 1531 | UNREACHABLE(); |
| 1532 | return false; |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | |
| 1537 | // Depending on split_last_bit split binary representation of reg_code into Vm:M |
| 1538 | // or M:Vm form (where M is single bit). |
| 1539 | static void SplitRegCode(bool split_last_bit, |
| 1540 | int reg_code, |
| 1541 | int* vm, |
| 1542 | int* m) { |
| 1543 | if (split_last_bit) { |
| 1544 | *m = reg_code & 0x1; |
| 1545 | *vm = reg_code >> 1; |
| 1546 | } else { |
| 1547 | *m = (reg_code & 0x10) >> 4; |
| 1548 | *vm = reg_code & 0x0F; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | |
| 1553 | // Encode vcvt.src_type.dst_type instruction. |
| 1554 | static Instr EncodeVCVT(const VFPType dst_type, |
| 1555 | const int dst_code, |
| 1556 | const VFPType src_type, |
| 1557 | const int src_code, |
| 1558 | const Condition cond) { |
| 1559 | if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) { |
| 1560 | // Conversion between IEEE floating point and 32-bit integer. |
| 1561 | // Instruction details available in ARM DDI 0406B, A8.6.295. |
| 1562 | // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) | |
| 1563 | // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0) |
| 1564 | ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type)); |
| 1565 | |
| 1566 | int sz, opc2, D, Vd, M, Vm, op; |
| 1567 | |
| 1568 | if (IsIntegerVFPType(dst_type)) { |
| 1569 | opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4; |
| 1570 | sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0; |
| 1571 | op = 1; // round towards zero |
| 1572 | SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M); |
| 1573 | SplitRegCode(true, dst_code, &Vd, &D); |
| 1574 | } else { |
| 1575 | ASSERT(IsIntegerVFPType(src_type)); |
| 1576 | |
| 1577 | opc2 = 0x0; |
| 1578 | sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0; |
| 1579 | op = IsSignedVFPType(src_type) ? 0x1 : 0x0; |
| 1580 | SplitRegCode(true, src_code, &Vm, &M); |
| 1581 | SplitRegCode(!IsDoubleVFPType(dst_type), dst_code, &Vd, &D); |
| 1582 | } |
| 1583 | |
| 1584 | return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 | |
| 1585 | Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm); |
| 1586 | } else { |
| 1587 | // Conversion between IEEE double and single precision. |
| 1588 | // Instruction details available in ARM DDI 0406B, A8.6.298. |
| 1589 | // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) | |
| 1590 | // Vd(15-12) | 101(11-9) | sz(8) | 1(7) | 1(6) | M(5) | 0(4) | Vm(3-0) |
| 1591 | int sz, D, Vd, M, Vm; |
| 1592 | |
| 1593 | ASSERT(IsDoubleVFPType(dst_type) != IsDoubleVFPType(src_type)); |
| 1594 | sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0; |
| 1595 | SplitRegCode(IsDoubleVFPType(src_type), dst_code, &Vd, &D); |
| 1596 | SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M); |
| 1597 | |
| 1598 | return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 | |
| 1599 | Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm); |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | |
| 1604 | void Assembler::vcvt_f64_s32(const DwVfpRegister dst, |
| 1605 | const SwVfpRegister src, |
| 1606 | const Condition cond) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1607 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1608 | emit(EncodeVCVT(F64, dst.code(), S32, src.code(), cond)); |
| 1609 | } |
| 1610 | |
| 1611 | |
| 1612 | void Assembler::vcvt_f32_s32(const SwVfpRegister dst, |
| 1613 | const SwVfpRegister src, |
| 1614 | const Condition cond) { |
| 1615 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1616 | emit(EncodeVCVT(F32, dst.code(), S32, src.code(), cond)); |
| 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | void Assembler::vcvt_f64_u32(const DwVfpRegister dst, |
| 1621 | const SwVfpRegister src, |
| 1622 | const Condition cond) { |
| 1623 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1624 | emit(EncodeVCVT(F64, dst.code(), U32, src.code(), cond)); |
| 1625 | } |
| 1626 | |
| 1627 | |
| 1628 | void Assembler::vcvt_s32_f64(const SwVfpRegister dst, |
| 1629 | const DwVfpRegister src, |
| 1630 | const Condition cond) { |
| 1631 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1632 | emit(EncodeVCVT(S32, dst.code(), F64, src.code(), cond)); |
| 1633 | } |
| 1634 | |
| 1635 | |
| 1636 | void Assembler::vcvt_u32_f64(const SwVfpRegister dst, |
| 1637 | const DwVfpRegister src, |
| 1638 | const Condition cond) { |
| 1639 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1640 | emit(EncodeVCVT(U32, dst.code(), F64, src.code(), cond)); |
| 1641 | } |
| 1642 | |
| 1643 | |
| 1644 | void Assembler::vcvt_f64_f32(const DwVfpRegister dst, |
| 1645 | const SwVfpRegister src, |
| 1646 | const Condition cond) { |
| 1647 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1648 | emit(EncodeVCVT(F64, dst.code(), F32, src.code(), cond)); |
| 1649 | } |
| 1650 | |
| 1651 | |
| 1652 | void Assembler::vcvt_f32_f64(const SwVfpRegister dst, |
| 1653 | const DwVfpRegister src, |
| 1654 | const Condition cond) { |
| 1655 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1656 | emit(EncodeVCVT(F32, dst.code(), F64, src.code(), cond)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1660 | void Assembler::vadd(const DwVfpRegister dst, |
| 1661 | const DwVfpRegister src1, |
| 1662 | const DwVfpRegister src2, |
| 1663 | const Condition cond) { |
| 1664 | // Dd = vadd(Dn, Dm) double precision floating point addition. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1665 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 1666 | // Instruction details available in ARM DDI 0406A, A8-536. |
| 1667 | // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) | |
| 1668 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0) |
| 1669 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1670 | emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 | |
| 1671 | dst.code()*B12 | 0x5*B9 | B8 | src2.code()); |
| 1672 | } |
| 1673 | |
| 1674 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1675 | void Assembler::vsub(const DwVfpRegister dst, |
| 1676 | const DwVfpRegister src1, |
| 1677 | const DwVfpRegister src2, |
| 1678 | const Condition cond) { |
| 1679 | // Dd = vsub(Dn, Dm) double precision floating point subtraction. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1680 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 1681 | // Instruction details available in ARM DDI 0406A, A8-784. |
| 1682 | // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) | |
| 1683 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 1(6) | M=?(5) | 0(4) | Vm(3-0) |
| 1684 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1685 | emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 | |
| 1686 | dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code()); |
| 1687 | } |
| 1688 | |
| 1689 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1690 | void Assembler::vmul(const DwVfpRegister dst, |
| 1691 | const DwVfpRegister src1, |
| 1692 | const DwVfpRegister src2, |
| 1693 | const Condition cond) { |
| 1694 | // Dd = vmul(Dn, Dm) double precision floating point multiplication. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1695 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 1696 | // Instruction details available in ARM DDI 0406A, A8-784. |
| 1697 | // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) | |
| 1698 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0) |
| 1699 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1700 | emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 | |
| 1701 | dst.code()*B12 | 0x5*B9 | B8 | src2.code()); |
| 1702 | } |
| 1703 | |
| 1704 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1705 | void Assembler::vdiv(const DwVfpRegister dst, |
| 1706 | const DwVfpRegister src1, |
| 1707 | const DwVfpRegister src2, |
| 1708 | const Condition cond) { |
| 1709 | // Dd = vdiv(Dn, Dm) double precision floating point division. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1710 | // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm. |
| 1711 | // Instruction details available in ARM DDI 0406A, A8-584. |
| 1712 | // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) | |
| 1713 | // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=? | 0(6) | M=?(5) | 0(4) | Vm(3-0) |
| 1714 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1715 | emit(cond | 0xE*B24 | B23 | src1.code()*B16 | |
| 1716 | dst.code()*B12 | 0x5*B9 | B8 | src2.code()); |
| 1717 | } |
| 1718 | |
| 1719 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1720 | void Assembler::vcmp(const DwVfpRegister src1, |
| 1721 | const DwVfpRegister src2, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1722 | const SBit s, |
| 1723 | const Condition cond) { |
| 1724 | // vcmp(Dd, Dm) double precision floating point comparison. |
| 1725 | // Instruction details available in ARM DDI 0406A, A8-570. |
| 1726 | // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) | |
| 1727 | // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=? | 1(6) | M(5)=? | 0(4) | Vm(3-0) |
| 1728 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1729 | emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 | |
| 1730 | src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code()); |
| 1731 | } |
| 1732 | |
| 1733 | |
| 1734 | void Assembler::vmrs(Register dst, Condition cond) { |
| 1735 | // Instruction details available in ARM DDI 0406A, A8-652. |
| 1736 | // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) | |
| 1737 | // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0) |
| 1738 | ASSERT(CpuFeatures::IsEnabled(VFP3)); |
| 1739 | emit(cond | 0xE*B24 | 0xF*B20 | B16 | |
| 1740 | dst.code()*B12 | 0xA*B8 | B4); |
| 1741 | } |
| 1742 | |
| 1743 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1744 | // Pseudo instructions. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame^] | 1745 | void Assembler::nop(int type) { |
| 1746 | // This is mov rx, rx. |
| 1747 | ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop. |
| 1748 | emit(al | 13*B21 | type*B12 | type); |
| 1749 | } |
| 1750 | |
| 1751 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1752 | void Assembler::lea(Register dst, |
| 1753 | const MemOperand& x, |
| 1754 | SBit s, |
| 1755 | Condition cond) { |
| 1756 | int am = x.am_; |
| 1757 | if (!x.rm_.is_valid()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1758 | // Immediate offset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1759 | if ((am & P) == 0) // post indexing |
| 1760 | mov(dst, Operand(x.rn_), s, cond); |
| 1761 | else if ((am & U) == 0) // negative indexing |
| 1762 | sub(dst, x.rn_, Operand(x.offset_), s, cond); |
| 1763 | else |
| 1764 | add(dst, x.rn_, Operand(x.offset_), s, cond); |
| 1765 | } else { |
| 1766 | // Register offset (shift_imm_ and shift_op_ are 0) or scaled |
| 1767 | // register offset the constructors make sure than both shift_imm_ |
| 1768 | // and shift_op_ are initialized. |
| 1769 | ASSERT(!x.rm_.is(pc)); |
| 1770 | if ((am & P) == 0) // post indexing |
| 1771 | mov(dst, Operand(x.rn_), s, cond); |
| 1772 | else if ((am & U) == 0) // negative indexing |
| 1773 | sub(dst, x.rn_, Operand(x.rm_, x.shift_op_, x.shift_imm_), s, cond); |
| 1774 | else |
| 1775 | add(dst, x.rn_, Operand(x.rm_, x.shift_op_, x.shift_imm_), s, cond); |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1780 | bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) { |
| 1781 | uint32_t dummy1; |
| 1782 | uint32_t dummy2; |
| 1783 | return fits_shifter(imm32, &dummy1, &dummy2, NULL); |
| 1784 | } |
| 1785 | |
| 1786 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1787 | // Debugging. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1788 | void Assembler::RecordJSReturn() { |
| 1789 | WriteRecordedPositions(); |
| 1790 | CheckBuffer(); |
| 1791 | RecordRelocInfo(RelocInfo::JS_RETURN); |
| 1792 | } |
| 1793 | |
| 1794 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1795 | void Assembler::RecordComment(const char* msg) { |
| 1796 | if (FLAG_debug_code) { |
| 1797 | CheckBuffer(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1798 | RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | |
| 1803 | void Assembler::RecordPosition(int pos) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1804 | if (pos == RelocInfo::kNoPosition) return; |
| 1805 | ASSERT(pos >= 0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1806 | current_position_ = pos; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | |
| 1810 | void Assembler::RecordStatementPosition(int pos) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1811 | if (pos == RelocInfo::kNoPosition) return; |
| 1812 | ASSERT(pos >= 0); |
| 1813 | current_statement_position_ = pos; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | |
| 1817 | void Assembler::WriteRecordedPositions() { |
| 1818 | // Write the statement position if it is different from what was written last |
| 1819 | // time. |
| 1820 | if (current_statement_position_ != written_statement_position_) { |
| 1821 | CheckBuffer(); |
| 1822 | RecordRelocInfo(RelocInfo::STATEMENT_POSITION, current_statement_position_); |
| 1823 | written_statement_position_ = current_statement_position_; |
| 1824 | } |
| 1825 | |
| 1826 | // 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] | 1827 | // also different from the written statement position. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1828 | if (current_position_ != written_position_ && |
| 1829 | current_position_ != written_statement_position_) { |
| 1830 | CheckBuffer(); |
| 1831 | RecordRelocInfo(RelocInfo::POSITION, current_position_); |
| 1832 | written_position_ = current_position_; |
| 1833 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | |
| 1837 | void Assembler::GrowBuffer() { |
| 1838 | if (!own_buffer_) FATAL("external code buffer is too small"); |
| 1839 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1840 | // Compute new buffer size. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1841 | CodeDesc desc; // the new buffer |
| 1842 | if (buffer_size_ < 4*KB) { |
| 1843 | desc.buffer_size = 4*KB; |
| 1844 | } else if (buffer_size_ < 1*MB) { |
| 1845 | desc.buffer_size = 2*buffer_size_; |
| 1846 | } else { |
| 1847 | desc.buffer_size = buffer_size_ + 1*MB; |
| 1848 | } |
| 1849 | CHECK_GT(desc.buffer_size, 0); // no overflow |
| 1850 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1851 | // Setup new buffer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1852 | desc.buffer = NewArray<byte>(desc.buffer_size); |
| 1853 | |
| 1854 | desc.instr_size = pc_offset(); |
| 1855 | desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos(); |
| 1856 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1857 | // Copy the data. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1858 | int pc_delta = desc.buffer - buffer_; |
| 1859 | int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_); |
| 1860 | memmove(desc.buffer, buffer_, desc.instr_size); |
| 1861 | memmove(reloc_info_writer.pos() + rc_delta, |
| 1862 | reloc_info_writer.pos(), desc.reloc_size); |
| 1863 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1864 | // Switch buffers. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1865 | DeleteArray(buffer_); |
| 1866 | buffer_ = desc.buffer; |
| 1867 | buffer_size_ = desc.buffer_size; |
| 1868 | pc_ += pc_delta; |
| 1869 | reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta, |
| 1870 | reloc_info_writer.last_pc() + pc_delta); |
| 1871 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1872 | // 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] | 1873 | // 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] | 1874 | // to relocate any emitted relocation entries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1875 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1876 | // Relocate pending relocation entries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1877 | for (int i = 0; i < num_prinfo_; i++) { |
| 1878 | RelocInfo& rinfo = prinfo_[i]; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1879 | ASSERT(rinfo.rmode() != RelocInfo::COMMENT && |
| 1880 | rinfo.rmode() != RelocInfo::POSITION); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1881 | if (rinfo.rmode() != RelocInfo::JS_RETURN) { |
| 1882 | rinfo.set_pc(rinfo.pc() + pc_delta); |
| 1883 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1888 | void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1889 | 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] | 1890 | if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::STATEMENT_POSITION) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1891 | // Adjust code for new modes. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1892 | ASSERT(RelocInfo::IsJSReturn(rmode) |
| 1893 | || RelocInfo::IsComment(rmode) |
| 1894 | || RelocInfo::IsPosition(rmode)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1895 | // These modes do not need an entry in the constant pool. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1896 | } else { |
| 1897 | ASSERT(num_prinfo_ < kMaxNumPRInfo); |
| 1898 | prinfo_[num_prinfo_++] = rinfo; |
| 1899 | // 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] | 1900 | // instruction for which we just recorded relocation info. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1901 | BlockConstPoolBefore(pc_offset() + kInstrSize); |
| 1902 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1903 | if (rinfo.rmode() != RelocInfo::NONE) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1904 | // Don't record external references unless the heap will be serialized. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1905 | if (rmode == RelocInfo::EXTERNAL_REFERENCE) { |
| 1906 | #ifdef DEBUG |
| 1907 | if (!Serializer::enabled()) { |
| 1908 | Serializer::TooLateToEnableNow(); |
| 1909 | } |
| 1910 | #endif |
| 1911 | if (!Serializer::enabled() && !FLAG_debug_code) { |
| 1912 | return; |
| 1913 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1914 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1915 | ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here |
| 1916 | reloc_info_writer.Write(&rinfo); |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | |
| 1921 | void Assembler::CheckConstPool(bool force_emit, bool require_jump) { |
| 1922 | // Calculate the offset of the next check. It will be overwritten |
| 1923 | // when a const pool is generated or when const pools are being |
| 1924 | // blocked for a specific range. |
| 1925 | next_buffer_check_ = pc_offset() + kCheckConstInterval; |
| 1926 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1927 | // 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] | 1928 | if (num_prinfo_ == 0) return; |
| 1929 | |
| 1930 | // We emit a constant pool at regular intervals of about kDistBetweenPools |
| 1931 | // or when requested by parameter force_emit (e.g. after each function). |
| 1932 | // We prefer not to emit a jump unless the max distance is reached or if we |
| 1933 | // are running low on slots, which can happen if a lot of constants are being |
| 1934 | // emitted (e.g. --debug-code and many static references). |
| 1935 | int dist = pc_offset() - last_const_pool_end_; |
| 1936 | if (!force_emit && dist < kMaxDistBetweenPools && |
| 1937 | (require_jump || dist < kDistBetweenPools) && |
| 1938 | // TODO(1236125): Cleanup the "magic" number below. We know that |
| 1939 | // the code generation will test every kCheckConstIntervalInst. |
| 1940 | // Thus we are safe as long as we generate less than 7 constant |
| 1941 | // entries per instruction. |
| 1942 | (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) { |
| 1943 | return; |
| 1944 | } |
| 1945 | |
| 1946 | // If we did not return by now, we need to emit the constant pool soon. |
| 1947 | |
| 1948 | // However, some small sequences of instructions must not be broken up by the |
| 1949 | // insertion of a constant pool; such sequences are protected by setting |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame^] | 1950 | // either const_pool_blocked_nesting_ or no_const_pool_before_, which are |
| 1951 | // both checked here. Also, recursive calls to CheckConstPool are blocked by |
| 1952 | // no_const_pool_before_. |
| 1953 | 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] | 1954 | // Emission is currently blocked; make sure we try again as soon as |
| 1955 | // possible. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame^] | 1956 | if (const_pool_blocked_nesting_ > 0) { |
| 1957 | next_buffer_check_ = pc_offset() + kInstrSize; |
| 1958 | } else { |
| 1959 | next_buffer_check_ = no_const_pool_before_; |
| 1960 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1961 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1962 | // 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] | 1963 | ASSERT(!force_emit); |
| 1964 | return; |
| 1965 | } |
| 1966 | |
| 1967 | int jump_instr = require_jump ? kInstrSize : 0; |
| 1968 | |
| 1969 | // Check that the code buffer is large enough before emitting the constant |
| 1970 | // pool and relocation information (include the jump over the pool and the |
| 1971 | // constant pool marker). |
| 1972 | int max_needed_space = |
| 1973 | jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize); |
| 1974 | while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer(); |
| 1975 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1976 | // Block recursive calls to CheckConstPool. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1977 | BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize + |
| 1978 | num_prinfo_*kInstrSize); |
| 1979 | // Don't bother to check for the emit calls below. |
| 1980 | next_buffer_check_ = no_const_pool_before_; |
| 1981 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1982 | // Emit jump over constant pool if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1983 | Label after_pool; |
| 1984 | if (require_jump) b(&after_pool); |
| 1985 | |
| 1986 | RecordComment("[ Constant Pool"); |
| 1987 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1988 | // Put down constant pool marker "Undefined instruction" as specified by |
| 1989 | // A3.1 Instruction set encoding. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1990 | emit(0x03000000 | num_prinfo_); |
| 1991 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1992 | // Emit constant pool entries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1993 | for (int i = 0; i < num_prinfo_; i++) { |
| 1994 | RelocInfo& rinfo = prinfo_[i]; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1995 | ASSERT(rinfo.rmode() != RelocInfo::COMMENT && |
| 1996 | rinfo.rmode() != RelocInfo::POSITION && |
| 1997 | rinfo.rmode() != RelocInfo::STATEMENT_POSITION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1998 | Instr instr = instr_at(rinfo.pc()); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1999 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2000 | // Instruction to patch must be a ldr/str [pc, #offset]. |
| 2001 | // 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] | 2002 | ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | Off12Mask)) == |
| 2003 | (2*B25 | P | U | pc.code()*B16)); |
| 2004 | int delta = pc_ - rinfo.pc() - 8; |
| 2005 | ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32 |
| 2006 | if (delta < 0) { |
| 2007 | instr &= ~U; |
| 2008 | delta = -delta; |
| 2009 | } |
| 2010 | ASSERT(is_uint12(delta)); |
| 2011 | instr_at_put(rinfo.pc(), instr + delta); |
| 2012 | emit(rinfo.data()); |
| 2013 | } |
| 2014 | num_prinfo_ = 0; |
| 2015 | last_const_pool_end_ = pc_offset(); |
| 2016 | |
| 2017 | RecordComment("]"); |
| 2018 | |
| 2019 | if (after_pool.is_linked()) { |
| 2020 | bind(&after_pool); |
| 2021 | } |
| 2022 | |
| 2023 | // Since a constant pool was just emitted, move the check offset forward by |
| 2024 | // the standard interval. |
| 2025 | next_buffer_check_ = pc_offset() + kCheckConstInterval; |
| 2026 | } |
| 2027 | |
| 2028 | |
| 2029 | } } // namespace v8::internal |