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