Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_COMPILER_UTILS_ARM_ASSEMBLER_THUMB2_H_ |
| 18 | #define ART_COMPILER_UTILS_ARM_ASSEMBLER_THUMB2_H_ |
| 19 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 20 | #include <deque> |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 21 | #include <utility> |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| 24 | #include "base/logging.h" |
| 25 | #include "constants_arm.h" |
| 26 | #include "utils/arm/managed_register_arm.h" |
| 27 | #include "utils/arm/assembler_arm.h" |
Vladimir Marko | 6b756b5 | 2015-07-14 11:58:38 +0100 | [diff] [blame] | 28 | #include "utils/array_ref.h" |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 29 | #include "offsets.h" |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | namespace arm { |
| 33 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 34 | class Thumb2Assembler FINAL : public ArmAssembler { |
| 35 | public: |
Nicolas Geoffray | d126ba1 | 2015-05-20 11:25:27 +0100 | [diff] [blame] | 36 | explicit Thumb2Assembler(bool can_relocate_branches = true) |
| 37 | : can_relocate_branches_(can_relocate_branches), |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 38 | force_32bit_(false), |
| 39 | it_cond_index_(kNoItCondition), |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 40 | next_condition_(AL), |
| 41 | fixups_(), |
Vladimir Marko | 6b756b5 | 2015-07-14 11:58:38 +0100 | [diff] [blame] | 42 | fixup_dependents_(), |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 43 | literals_(), |
| 44 | last_position_adjustment_(0u), |
| 45 | last_old_position_(0u), |
| 46 | last_fixup_id_(0u) { |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 47 | cfi().DelayEmittingAdvancePCs(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | virtual ~Thumb2Assembler() { |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool IsThumb() const OVERRIDE { |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | bool IsForced32Bit() const { |
| 58 | return force_32bit_; |
| 59 | } |
| 60 | |
Nicolas Geoffray | d126ba1 | 2015-05-20 11:25:27 +0100 | [diff] [blame] | 61 | bool CanRelocateBranches() const { |
| 62 | return can_relocate_branches_; |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 65 | void FinalizeCode() OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 66 | |
| 67 | // Data-processing instructions. |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 68 | virtual void and_(Register rd, Register rn, const ShifterOperand& so, |
| 69 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 70 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 71 | virtual void eor(Register rd, Register rn, const ShifterOperand& so, |
| 72 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 73 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 74 | virtual void sub(Register rd, Register rn, const ShifterOperand& so, |
| 75 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 76 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 77 | virtual void rsb(Register rd, Register rn, const ShifterOperand& so, |
| 78 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 79 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 80 | virtual void add(Register rd, Register rn, const ShifterOperand& so, |
| 81 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 82 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 83 | virtual void adc(Register rd, Register rn, const ShifterOperand& so, |
| 84 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 85 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 86 | virtual void sbc(Register rd, Register rn, const ShifterOperand& so, |
| 87 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 88 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 89 | virtual void rsc(Register rd, Register rn, const ShifterOperand& so, |
| 90 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 91 | |
| 92 | void tst(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 93 | |
| 94 | void teq(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 95 | |
| 96 | void cmp(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 97 | |
| 98 | void cmn(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 99 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 100 | virtual void orr(Register rd, Register rn, const ShifterOperand& so, |
| 101 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 102 | |
Vladimir Marko | d2b4ca2 | 2015-09-14 15:13:26 +0100 | [diff] [blame] | 103 | virtual void orn(Register rd, Register rn, const ShifterOperand& so, |
| 104 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
| 105 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 106 | virtual void mov(Register rd, const ShifterOperand& so, |
| 107 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 108 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 109 | virtual void bic(Register rd, Register rn, const ShifterOperand& so, |
| 110 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 111 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 112 | virtual void mvn(Register rd, const ShifterOperand& so, |
| 113 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 114 | |
| 115 | // Miscellaneous data-processing instructions. |
| 116 | void clz(Register rd, Register rm, Condition cond = AL) OVERRIDE; |
| 117 | void movw(Register rd, uint16_t imm16, Condition cond = AL) OVERRIDE; |
| 118 | void movt(Register rd, uint16_t imm16, Condition cond = AL) OVERRIDE; |
Scott Wakeling | 9ee23f4 | 2015-07-23 10:44:35 +0100 | [diff] [blame] | 119 | void rbit(Register rd, Register rm, Condition cond = AL) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 120 | |
| 121 | // Multiply instructions. |
| 122 | void mul(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE; |
| 123 | void mla(Register rd, Register rn, Register rm, Register ra, |
| 124 | Condition cond = AL) OVERRIDE; |
| 125 | void mls(Register rd, Register rn, Register rm, Register ra, |
| 126 | Condition cond = AL) OVERRIDE; |
Zheng Xu | c666710 | 2015-05-15 16:08:45 +0800 | [diff] [blame] | 127 | void smull(Register rd_lo, Register rd_hi, Register rn, Register rm, |
| 128 | Condition cond = AL) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 129 | void umull(Register rd_lo, Register rd_hi, Register rn, Register rm, |
| 130 | Condition cond = AL) OVERRIDE; |
| 131 | |
| 132 | void sdiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE; |
| 133 | void udiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE; |
| 134 | |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 135 | // Bit field extract instructions. |
Roland Levillain | 51d3fc4 | 2014-11-13 14:11:42 +0000 | [diff] [blame] | 136 | void sbfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond = AL) OVERRIDE; |
Roland Levillain | 981e454 | 2014-11-14 11:47:14 +0000 | [diff] [blame] | 137 | void ubfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond = AL) OVERRIDE; |
Roland Levillain | 51d3fc4 | 2014-11-13 14:11:42 +0000 | [diff] [blame] | 138 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 139 | // Load/store instructions. |
| 140 | void ldr(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 141 | void str(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 142 | |
| 143 | void ldrb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 144 | void strb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 145 | |
| 146 | void ldrh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 147 | void strh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 148 | |
| 149 | void ldrsb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 150 | void ldrsh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 151 | |
Roland Levillain | 4af147e | 2015-04-07 13:54:49 +0100 | [diff] [blame] | 152 | // Load/store register dual instructions using registers `rd` and `rd` + 1. |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 153 | void ldrd(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 154 | void strd(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 155 | |
Roland Levillain | 4af147e | 2015-04-07 13:54:49 +0100 | [diff] [blame] | 156 | // Load/store register dual instructions using registers `rd` and `rd2`. |
| 157 | // Note that contrary to the ARM A1 encoding, the Thumb-2 T1 encoding |
| 158 | // does not require `rd` to be even, nor `rd2' to be equal to `rd` + 1. |
| 159 | void ldrd(Register rd, Register rd2, const Address& ad, Condition cond); |
| 160 | void strd(Register rd, Register rd2, const Address& ad, Condition cond); |
| 161 | |
| 162 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 163 | void ldm(BlockAddressMode am, Register base, |
| 164 | RegList regs, Condition cond = AL) OVERRIDE; |
| 165 | void stm(BlockAddressMode am, Register base, |
| 166 | RegList regs, Condition cond = AL) OVERRIDE; |
| 167 | |
| 168 | void ldrex(Register rd, Register rn, Condition cond = AL) OVERRIDE; |
| 169 | void strex(Register rd, Register rt, Register rn, Condition cond = AL) OVERRIDE; |
| 170 | |
| 171 | void ldrex(Register rd, Register rn, uint16_t imm, Condition cond = AL); |
| 172 | void strex(Register rd, Register rt, Register rn, uint16_t imm, Condition cond = AL); |
| 173 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 174 | void ldrexd(Register rt, Register rt2, Register rn, Condition cond = AL) OVERRIDE; |
| 175 | void strexd(Register rd, Register rt, Register rt2, Register rn, Condition cond = AL) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 176 | |
| 177 | // Miscellaneous instructions. |
| 178 | void clrex(Condition cond = AL) OVERRIDE; |
| 179 | void nop(Condition cond = AL) OVERRIDE; |
| 180 | |
| 181 | void bkpt(uint16_t imm16) OVERRIDE; |
| 182 | void svc(uint32_t imm24) OVERRIDE; |
| 183 | |
| 184 | // If-then |
| 185 | void it(Condition firstcond, ItState i1 = kItOmitted, |
| 186 | ItState i2 = kItOmitted, ItState i3 = kItOmitted) OVERRIDE; |
| 187 | |
| 188 | void cbz(Register rn, Label* target) OVERRIDE; |
| 189 | void cbnz(Register rn, Label* target) OVERRIDE; |
| 190 | |
| 191 | // Floating point instructions (VFPv3-D16 and VFPv3-D32 profiles). |
| 192 | void vmovsr(SRegister sn, Register rt, Condition cond = AL) OVERRIDE; |
| 193 | void vmovrs(Register rt, SRegister sn, Condition cond = AL) OVERRIDE; |
| 194 | void vmovsrr(SRegister sm, Register rt, Register rt2, Condition cond = AL) OVERRIDE; |
| 195 | void vmovrrs(Register rt, Register rt2, SRegister sm, Condition cond = AL) OVERRIDE; |
| 196 | void vmovdrr(DRegister dm, Register rt, Register rt2, Condition cond = AL) OVERRIDE; |
| 197 | void vmovrrd(Register rt, Register rt2, DRegister dm, Condition cond = AL) OVERRIDE; |
| 198 | void vmovs(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 199 | void vmovd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 200 | |
| 201 | // Returns false if the immediate cannot be encoded. |
| 202 | bool vmovs(SRegister sd, float s_imm, Condition cond = AL) OVERRIDE; |
| 203 | bool vmovd(DRegister dd, double d_imm, Condition cond = AL) OVERRIDE; |
| 204 | |
| 205 | void vldrs(SRegister sd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 206 | void vstrs(SRegister sd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 207 | void vldrd(DRegister dd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 208 | void vstrd(DRegister dd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 209 | |
| 210 | void vadds(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE; |
| 211 | void vaddd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE; |
| 212 | void vsubs(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE; |
| 213 | void vsubd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE; |
| 214 | void vmuls(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE; |
| 215 | void vmuld(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE; |
| 216 | void vmlas(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE; |
| 217 | void vmlad(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE; |
| 218 | void vmlss(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE; |
| 219 | void vmlsd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE; |
| 220 | void vdivs(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE; |
| 221 | void vdivd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE; |
| 222 | |
| 223 | void vabss(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 224 | void vabsd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 225 | void vnegs(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 226 | void vnegd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 227 | void vsqrts(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 228 | void vsqrtd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 229 | |
| 230 | void vcvtsd(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 231 | void vcvtds(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 232 | void vcvtis(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 233 | void vcvtid(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 234 | void vcvtsi(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 235 | void vcvtdi(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 236 | void vcvtus(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 237 | void vcvtud(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 238 | void vcvtsu(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 239 | void vcvtdu(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 240 | |
| 241 | void vcmps(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 242 | void vcmpd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 243 | void vcmpsz(SRegister sd, Condition cond = AL) OVERRIDE; |
| 244 | void vcmpdz(DRegister dd, Condition cond = AL) OVERRIDE; |
| 245 | void vmstat(Condition cond = AL) OVERRIDE; // VMRS APSR_nzcv, FPSCR |
| 246 | |
| 247 | void vpushs(SRegister reg, int nregs, Condition cond = AL) OVERRIDE; |
| 248 | void vpushd(DRegister reg, int nregs, Condition cond = AL) OVERRIDE; |
| 249 | void vpops(SRegister reg, int nregs, Condition cond = AL) OVERRIDE; |
| 250 | void vpopd(DRegister reg, int nregs, Condition cond = AL) OVERRIDE; |
| 251 | |
| 252 | // Branch instructions. |
| 253 | void b(Label* label, Condition cond = AL); |
| 254 | void bl(Label* label, Condition cond = AL); |
| 255 | void blx(Label* label); |
| 256 | void blx(Register rm, Condition cond = AL) OVERRIDE; |
| 257 | void bx(Register rm, Condition cond = AL) OVERRIDE; |
| 258 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 259 | virtual void Lsl(Register rd, Register rm, uint32_t shift_imm, |
| 260 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
| 261 | virtual void Lsr(Register rd, Register rm, uint32_t shift_imm, |
| 262 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
| 263 | virtual void Asr(Register rd, Register rm, uint32_t shift_imm, |
| 264 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
| 265 | virtual void Ror(Register rd, Register rm, uint32_t shift_imm, |
| 266 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
| 267 | virtual void Rrx(Register rd, Register rm, |
| 268 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 45fdb93 | 2014-06-25 12:37:10 -0700 | [diff] [blame] | 269 | |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 270 | virtual void Lsl(Register rd, Register rm, Register rn, |
| 271 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
| 272 | virtual void Lsr(Register rd, Register rm, Register rn, |
| 273 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
| 274 | virtual void Asr(Register rd, Register rm, Register rn, |
| 275 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
| 276 | virtual void Ror(Register rd, Register rm, Register rn, |
| 277 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 278 | |
| 279 | void Push(Register rd, Condition cond = AL) OVERRIDE; |
| 280 | void Pop(Register rd, Condition cond = AL) OVERRIDE; |
| 281 | |
| 282 | void PushList(RegList regs, Condition cond = AL) OVERRIDE; |
| 283 | void PopList(RegList regs, Condition cond = AL) OVERRIDE; |
| 284 | |
| 285 | void Mov(Register rd, Register rm, Condition cond = AL) OVERRIDE; |
| 286 | |
| 287 | void CompareAndBranchIfZero(Register r, Label* label) OVERRIDE; |
| 288 | void CompareAndBranchIfNonZero(Register r, Label* label) OVERRIDE; |
| 289 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 290 | // Memory barriers. |
| 291 | void dmb(DmbOptions flavor) OVERRIDE; |
| 292 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 293 | // Get the final position of a label after local fixup based on the old position |
| 294 | // recorded before FinalizeCode(). |
| 295 | uint32_t GetAdjustedPosition(uint32_t old_position) OVERRIDE; |
| 296 | |
| 297 | using ArmAssembler::NewLiteral; // Make the helper template visible. |
| 298 | |
| 299 | Literal* NewLiteral(size_t size, const uint8_t* data) OVERRIDE; |
| 300 | void LoadLiteral(Register rt, Literal* literal) OVERRIDE; |
| 301 | void LoadLiteral(Register rt, Register rt2, Literal* literal) OVERRIDE; |
| 302 | void LoadLiteral(SRegister sd, Literal* literal) OVERRIDE; |
| 303 | void LoadLiteral(DRegister dd, Literal* literal) OVERRIDE; |
| 304 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 305 | // Add signed constant value to rd. May clobber IP. |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 306 | void AddConstant(Register rd, Register rn, int32_t value, |
Vladimir Marko | 449b109 | 2015-09-08 12:16:45 +0100 | [diff] [blame] | 307 | Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 308 | |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 309 | void CmpConstant(Register rn, int32_t value, Condition cond = AL) OVERRIDE; |
| 310 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 311 | // Load and Store. May clobber IP. |
| 312 | void LoadImmediate(Register rd, int32_t value, Condition cond = AL) OVERRIDE; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 313 | void MarkExceptionHandler(Label* label) OVERRIDE; |
| 314 | void LoadFromOffset(LoadOperandType type, |
| 315 | Register reg, |
| 316 | Register base, |
| 317 | int32_t offset, |
| 318 | Condition cond = AL) OVERRIDE; |
| 319 | void StoreToOffset(StoreOperandType type, |
| 320 | Register reg, |
| 321 | Register base, |
| 322 | int32_t offset, |
| 323 | Condition cond = AL) OVERRIDE; |
| 324 | void LoadSFromOffset(SRegister reg, |
| 325 | Register base, |
| 326 | int32_t offset, |
| 327 | Condition cond = AL) OVERRIDE; |
| 328 | void StoreSToOffset(SRegister reg, |
| 329 | Register base, |
| 330 | int32_t offset, |
| 331 | Condition cond = AL) OVERRIDE; |
| 332 | void LoadDFromOffset(DRegister reg, |
| 333 | Register base, |
| 334 | int32_t offset, |
| 335 | Condition cond = AL) OVERRIDE; |
| 336 | void StoreDToOffset(DRegister reg, |
| 337 | Register base, |
| 338 | int32_t offset, |
| 339 | Condition cond = AL) OVERRIDE; |
| 340 | |
Nicolas Geoffray | 3bcc8ea | 2014-11-28 15:00:02 +0000 | [diff] [blame] | 341 | bool ShifterOperandCanHold(Register rd, |
| 342 | Register rn, |
| 343 | Opcode opcode, |
| 344 | uint32_t immediate, |
| 345 | ShifterOperand* shifter_op) OVERRIDE; |
| 346 | |
Nicolas Geoffray | 5bd05a5 | 2015-10-13 09:48:30 +0100 | [diff] [blame] | 347 | bool ShifterOperandCanAlwaysHold(uint32_t immediate) OVERRIDE; |
| 348 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 349 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 350 | static bool IsInstructionForExceptionHandling(uintptr_t pc); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 351 | |
| 352 | // Emit data (e.g. encoded instruction or immediate) to the. |
| 353 | // instruction stream. |
| 354 | void Emit32(int32_t value); // Emit a 32 bit instruction in thumb format. |
| 355 | void Emit16(int16_t value); // Emit a 16 bit instruction in little endian format. |
| 356 | void Bind(Label* label) OVERRIDE; |
| 357 | |
| 358 | void MemoryBarrier(ManagedRegister scratch) OVERRIDE; |
| 359 | |
| 360 | // Force the assembler to generate 32 bit instructions. |
| 361 | void Force32Bit() { |
| 362 | force_32bit_ = true; |
| 363 | } |
| 364 | |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 365 | // Emit an ADR (or a sequence of instructions) to load the jump table address into base_reg. This |
| 366 | // will generate a fixup. |
| 367 | JumpTable* CreateJumpTable(std::vector<Label*>&& labels, Register base_reg) OVERRIDE; |
| 368 | // Emit an ADD PC, X to dispatch a jump-table jump. This will generate a fixup. |
| 369 | void EmitJumpTableDispatch(JumpTable* jump_table, Register displacement_reg) OVERRIDE; |
| 370 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 371 | private: |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 372 | typedef uint16_t FixupId; |
| 373 | |
| 374 | // Fixup: branches and literal pool references. |
| 375 | // |
| 376 | // The thumb2 architecture allows branches to be either 16 or 32 bit instructions. This |
| 377 | // depends on both the type of branch and the offset to which it is branching. The 16-bit |
| 378 | // cbz and cbnz instructions may also need to be replaced with a separate 16-bit compare |
| 379 | // instruction and a 16- or 32-bit branch instruction. Load from a literal pool can also be |
| 380 | // 16-bit or 32-bit instruction and, if the method is large, we may need to use a sequence |
| 381 | // of instructions to make up for the limited range of load literal instructions (up to |
| 382 | // 4KiB for the 32-bit variant). When generating code for these insns we don't know the |
| 383 | // size before hand, so we assume it is the smallest available size and determine the final |
| 384 | // code offsets and sizes and emit code in FinalizeCode(). |
| 385 | // |
| 386 | // To handle this, we keep a record of every branch and literal pool load in the program. |
| 387 | // The actual instruction encoding for these is delayed until we know the final size of |
| 388 | // every instruction. When we bind a label to a branch we don't know the final location yet |
| 389 | // as some preceding instructions may need to be expanded, so we record a non-final offset. |
| 390 | // In FinalizeCode(), we expand the sizes of branches and literal loads that are out of |
| 391 | // range. With each expansion, we need to update dependent Fixups, i.e. insntructios with |
| 392 | // target on the other side of the expanded insn, as their offsets change and this may |
| 393 | // trigger further expansion. |
| 394 | // |
| 395 | // All Fixups have a 'fixup id' which is a 16 bit unsigned number used to identify the |
| 396 | // Fixup. For each unresolved label we keep a singly-linked list of all Fixups pointing |
| 397 | // to it, using the fixup ids as links. The first link is stored in the label's position |
| 398 | // (the label is linked but not bound), the following links are stored in the code buffer, |
| 399 | // in the placeholder where we will eventually emit the actual code. |
| 400 | |
| 401 | class Fixup { |
| 402 | public: |
| 403 | // Branch type. |
| 404 | enum Type : uint8_t { |
| 405 | kConditional, // B<cond>. |
| 406 | kUnconditional, // B. |
| 407 | kUnconditionalLink, // BL. |
| 408 | kUnconditionalLinkX, // BLX. |
| 409 | kCompareAndBranchXZero, // cbz/cbnz. |
| 410 | kLoadLiteralNarrow, // Load narrrow integer literal. |
| 411 | kLoadLiteralWide, // Load wide integer literal. |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 412 | kLoadLiteralAddr, // Load address of literal (used for jump table). |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 413 | kLoadFPLiteralSingle, // Load FP literal single. |
| 414 | kLoadFPLiteralDouble, // Load FP literal double. |
| 415 | }; |
| 416 | |
| 417 | // Calculated size of branch instruction based on type and offset. |
| 418 | enum Size : uint8_t { |
| 419 | // Branch variants. |
| 420 | kBranch16Bit, |
| 421 | kBranch32Bit, |
| 422 | // NOTE: We don't support branches which would require multiple instructions, i.e. |
| 423 | // conditinoal branches beyond +-1MiB and unconditional branches beyond +-16MiB. |
| 424 | |
| 425 | // CBZ/CBNZ variants. |
| 426 | kCbxz16Bit, // CBZ/CBNZ rX, label; X < 8; 7-bit positive offset. |
| 427 | kCbxz32Bit, // CMP rX, #0 + Bcc label; X < 8; 16-bit Bcc; +-8-bit offset. |
| 428 | kCbxz48Bit, // CMP rX, #0 + Bcc label; X < 8; 32-bit Bcc; up to +-1MiB offset. |
| 429 | |
| 430 | // Load integer literal variants. |
| 431 | // LDR rX, label; X < 8; 16-bit variant up to 1KiB offset; 2 bytes. |
| 432 | kLiteral1KiB, |
| 433 | // LDR rX, label; 32-bit variant up to 4KiB offset; 4 bytes. |
| 434 | kLiteral4KiB, |
| 435 | // MOV rX, imm16 + ADD rX, pc + LDR rX, [rX]; X < 8; up to 64KiB offset; 8 bytes. |
| 436 | kLiteral64KiB, |
| 437 | // MOV rX, modimm + ADD rX, pc + LDR rX, [rX, #imm12]; up to 1MiB offset; 10 bytes. |
| 438 | kLiteral1MiB, |
| 439 | // NOTE: We don't provide the 12-byte version of kLiteralFar below where the LDR is 16-bit. |
| 440 | // MOV rX, imm16 + MOVT rX, imm16 + ADD rX, pc + LDR rX, [rX]; any offset; 14 bytes. |
| 441 | kLiteralFar, |
| 442 | |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 443 | // Load literal base addr. |
| 444 | // ADR rX, label; X < 8; 8 bit immediate, shifted to 10 bit. 2 bytes. |
| 445 | kLiteralAddr1KiB, |
| 446 | // ADR rX, label; 4KiB offset. 4 bytes. |
| 447 | kLiteralAddr4KiB, |
| 448 | // MOV rX, imm16 + ADD rX, pc; 64KiB offset. 6 bytes. |
| 449 | kLiteralAddr64KiB, |
| 450 | // MOV rX, imm16 + MOVT rX, imm16 + ADD rX, pc; any offset; 10 bytes. |
| 451 | kLiteralAddrFar, |
| 452 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 453 | // Load long or FP literal variants. |
| 454 | // VLDR s/dX, label; 32-bit insn, up to 1KiB offset; 4 bytes. |
| 455 | kLongOrFPLiteral1KiB, |
| 456 | // MOV ip, modimm + ADD ip, pc + VLDR s/dX, [IP, #imm8*4]; up to 256KiB offset; 10 bytes. |
| 457 | kLongOrFPLiteral256KiB, |
| 458 | // MOV ip, imm16 + MOVT ip, imm16 + ADD ip, pc + VLDR s/dX, [IP]; any offset; 14 bytes. |
| 459 | kLongOrFPLiteralFar, |
| 460 | }; |
| 461 | |
| 462 | // Unresolved branch possibly with a condition. |
| 463 | static Fixup Branch(uint32_t location, Type type, Size size = kBranch16Bit, |
| 464 | Condition cond = AL) { |
| 465 | DCHECK(type == kConditional || type == kUnconditional || |
| 466 | type == kUnconditionalLink || type == kUnconditionalLinkX); |
| 467 | DCHECK(size == kBranch16Bit || size == kBranch32Bit); |
| 468 | DCHECK(size == kBranch32Bit || (type == kConditional || type == kUnconditional)); |
| 469 | return Fixup(kNoRegister, kNoRegister, kNoSRegister, kNoDRegister, |
| 470 | cond, type, size, location); |
| 471 | } |
| 472 | |
| 473 | // Unresolved compare-and-branch instruction with a register and condition (EQ or NE). |
| 474 | static Fixup CompareAndBranch(uint32_t location, Register rn, Condition cond) { |
| 475 | DCHECK(cond == EQ || cond == NE); |
| 476 | return Fixup(rn, kNoRegister, kNoSRegister, kNoDRegister, |
| 477 | cond, kCompareAndBranchXZero, kCbxz16Bit, location); |
| 478 | } |
| 479 | |
| 480 | // Load narrow literal. |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 481 | static Fixup LoadNarrowLiteral(uint32_t location, Register rt, Size size) { |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 482 | DCHECK(size == kLiteral1KiB || size == kLiteral4KiB || size == kLiteral64KiB || |
| 483 | size == kLiteral1MiB || size == kLiteralFar); |
| 484 | DCHECK(!IsHighRegister(rt) || (size != kLiteral1KiB && size != kLiteral64KiB)); |
| 485 | return Fixup(rt, kNoRegister, kNoSRegister, kNoDRegister, |
| 486 | AL, kLoadLiteralNarrow, size, location); |
| 487 | } |
| 488 | |
| 489 | // Load wide literal. |
| 490 | static Fixup LoadWideLiteral(uint32_t location, Register rt, Register rt2, |
| 491 | Size size = kLongOrFPLiteral1KiB) { |
| 492 | DCHECK(size == kLongOrFPLiteral1KiB || size == kLongOrFPLiteral256KiB || |
| 493 | size == kLongOrFPLiteralFar); |
| 494 | DCHECK(!IsHighRegister(rt) || (size != kLiteral1KiB && size != kLiteral64KiB)); |
| 495 | return Fixup(rt, rt2, kNoSRegister, kNoDRegister, |
| 496 | AL, kLoadLiteralWide, size, location); |
| 497 | } |
| 498 | |
| 499 | // Load FP single literal. |
| 500 | static Fixup LoadSingleLiteral(uint32_t location, SRegister sd, |
| 501 | Size size = kLongOrFPLiteral1KiB) { |
| 502 | DCHECK(size == kLongOrFPLiteral1KiB || size == kLongOrFPLiteral256KiB || |
| 503 | size == kLongOrFPLiteralFar); |
| 504 | return Fixup(kNoRegister, kNoRegister, sd, kNoDRegister, |
| 505 | AL, kLoadFPLiteralSingle, size, location); |
| 506 | } |
| 507 | |
| 508 | // Load FP double literal. |
| 509 | static Fixup LoadDoubleLiteral(uint32_t location, DRegister dd, |
| 510 | Size size = kLongOrFPLiteral1KiB) { |
| 511 | DCHECK(size == kLongOrFPLiteral1KiB || size == kLongOrFPLiteral256KiB || |
| 512 | size == kLongOrFPLiteralFar); |
| 513 | return Fixup(kNoRegister, kNoRegister, kNoSRegister, dd, |
| 514 | AL, kLoadFPLiteralDouble, size, location); |
| 515 | } |
| 516 | |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 517 | static Fixup LoadLiteralAddress(uint32_t location, Register rt, Size size) { |
| 518 | DCHECK(size == kLiteralAddr1KiB || size == kLiteralAddr4KiB || size == kLiteralAddr64KiB || |
| 519 | size == kLiteralAddrFar); |
| 520 | DCHECK(!IsHighRegister(rt) || size != kLiteralAddr1KiB); |
| 521 | return Fixup(rt, kNoRegister, kNoSRegister, kNoDRegister, |
| 522 | AL, kLoadLiteralAddr, size, location); |
| 523 | } |
| 524 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 525 | Type GetType() const { |
| 526 | return type_; |
| 527 | } |
| 528 | |
Vladimir Marko | 663c934 | 2015-07-22 11:28:14 +0100 | [diff] [blame] | 529 | bool IsLoadLiteral() const { |
| 530 | return GetType() >= kLoadLiteralNarrow; |
| 531 | } |
| 532 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 533 | Size GetOriginalSize() const { |
| 534 | return original_size_; |
| 535 | } |
| 536 | |
| 537 | Size GetSize() const { |
| 538 | return size_; |
| 539 | } |
| 540 | |
| 541 | uint32_t GetOriginalSizeInBytes() const; |
| 542 | |
| 543 | uint32_t GetSizeInBytes() const; |
| 544 | |
| 545 | uint32_t GetLocation() const { |
| 546 | return location_; |
| 547 | } |
| 548 | |
| 549 | uint32_t GetAdjustment() const { |
| 550 | return adjustment_; |
| 551 | } |
| 552 | |
Vladimir Marko | 6b756b5 | 2015-07-14 11:58:38 +0100 | [diff] [blame] | 553 | // Prepare the assembler->fixup_dependents_ and each Fixup's dependents_start_/count_. |
| 554 | static void PrepareDependents(Thumb2Assembler* assembler); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 555 | |
Vladimir Marko | 6b756b5 | 2015-07-14 11:58:38 +0100 | [diff] [blame] | 556 | ArrayRef<FixupId> Dependents(const Thumb2Assembler& assembler) const { |
| 557 | return ArrayRef<FixupId>(assembler.fixup_dependents_.get() + dependents_start_, |
| 558 | dependents_count_); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | // Resolve a branch when the target is known. |
| 562 | void Resolve(uint32_t target) { |
| 563 | DCHECK_EQ(target_, kUnresolved); |
| 564 | DCHECK_NE(target, kUnresolved); |
| 565 | target_ = target; |
| 566 | } |
| 567 | |
| 568 | // Check if the current size is OK for current location_, target_ and adjustment_. |
| 569 | // If not, increase the size. Return the size increase, 0 if unchanged. |
| 570 | // If the target if after this Fixup, also add the difference to adjustment_, |
| 571 | // so that we don't need to consider forward Fixups as their own dependencies. |
| 572 | uint32_t AdjustSizeIfNeeded(uint32_t current_code_size); |
| 573 | |
| 574 | // Increase adjustments. This is called for dependents of a Fixup when its size changes. |
| 575 | void IncreaseAdjustment(uint32_t increase) { |
| 576 | adjustment_ += increase; |
| 577 | } |
| 578 | |
| 579 | // Finalize the branch with an adjustment to the location. Both location and target are updated. |
| 580 | void Finalize(uint32_t location_adjustment) { |
| 581 | DCHECK_NE(target_, kUnresolved); |
| 582 | location_ += location_adjustment; |
| 583 | target_ += location_adjustment; |
| 584 | } |
| 585 | |
| 586 | // Emit the branch instruction into the assembler buffer. This does the |
| 587 | // encoding into the thumb instruction. |
| 588 | void Emit(AssemblerBuffer* buffer, uint32_t code_size) const; |
| 589 | |
| 590 | private: |
| 591 | Fixup(Register rn, Register rt2, SRegister sd, DRegister dd, |
| 592 | Condition cond, Type type, Size size, uint32_t location) |
| 593 | : rn_(rn), |
| 594 | rt2_(rt2), |
| 595 | sd_(sd), |
| 596 | dd_(dd), |
| 597 | cond_(cond), |
| 598 | type_(type), |
| 599 | original_size_(size), size_(size), |
| 600 | location_(location), |
| 601 | target_(kUnresolved), |
| 602 | adjustment_(0u), |
Vladimir Marko | 6b756b5 | 2015-07-14 11:58:38 +0100 | [diff] [blame] | 603 | dependents_count_(0u), |
| 604 | dependents_start_(0u) { |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 605 | } |
| 606 | static size_t SizeInBytes(Size size); |
| 607 | |
| 608 | // The size of padding added before the literal pool. |
| 609 | static size_t LiteralPoolPaddingSize(uint32_t current_code_size); |
| 610 | |
| 611 | // Returns the offset from the PC-using insn to the target. |
| 612 | int32_t GetOffset(uint32_t current_code_size) const; |
| 613 | |
| 614 | size_t IncreaseSize(Size new_size); |
| 615 | |
| 616 | int32_t LoadWideOrFpEncoding(Register rbase, int32_t offset) const; |
| 617 | |
| 618 | static constexpr uint32_t kUnresolved = 0xffffffff; // Value for target_ for unresolved. |
| 619 | |
| 620 | const Register rn_; // Rn for cbnz/cbz, Rt for literal loads. |
| 621 | Register rt2_; // For kLoadLiteralWide. |
| 622 | SRegister sd_; // For kLoadFPLiteralSingle. |
| 623 | DRegister dd_; // For kLoadFPLiteralDouble. |
| 624 | const Condition cond_; |
| 625 | const Type type_; |
| 626 | Size original_size_; |
| 627 | Size size_; |
| 628 | uint32_t location_; // Offset into assembler buffer in bytes. |
| 629 | uint32_t target_; // Offset into assembler buffer in bytes. |
| 630 | uint32_t adjustment_; // The number of extra bytes inserted between location_ and target_. |
Vladimir Marko | 6b756b5 | 2015-07-14 11:58:38 +0100 | [diff] [blame] | 631 | // Fixups that require adjustment when current size changes are stored in a single |
| 632 | // array in the assembler and we store only the start index and count here. |
| 633 | uint32_t dependents_count_; |
| 634 | uint32_t dependents_start_; |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 635 | }; |
| 636 | |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 637 | // Emit a single 32 or 16 bit data processing instruction. |
| 638 | void EmitDataProcessing(Condition cond, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 639 | Opcode opcode, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 640 | SetCc set_cc, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 641 | Register rn, |
| 642 | Register rd, |
| 643 | const ShifterOperand& so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 644 | |
| 645 | // Must the instruction be 32 bits or can it possibly be encoded |
| 646 | // in 16 bits? |
| 647 | bool Is32BitDataProcessing(Condition cond, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 648 | Opcode opcode, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 649 | SetCc set_cc, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 650 | Register rn, |
| 651 | Register rd, |
| 652 | const ShifterOperand& so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 653 | |
| 654 | // Emit a 32 bit data processing instruction. |
| 655 | void Emit32BitDataProcessing(Condition cond, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 656 | Opcode opcode, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 657 | SetCc set_cc, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 658 | Register rn, |
| 659 | Register rd, |
| 660 | const ShifterOperand& so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 661 | |
| 662 | // Emit a 16 bit data processing instruction. |
| 663 | void Emit16BitDataProcessing(Condition cond, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 664 | Opcode opcode, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 665 | SetCc set_cc, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 666 | Register rn, |
| 667 | Register rd, |
| 668 | const ShifterOperand& so); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 669 | |
| 670 | void Emit16BitAddSub(Condition cond, |
| 671 | Opcode opcode, |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 672 | SetCc set_cc, |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 673 | Register rn, |
| 674 | Register rd, |
| 675 | const ShifterOperand& so); |
| 676 | |
| 677 | uint16_t EmitCompareAndBranch(Register rn, uint16_t prev, bool n); |
| 678 | |
| 679 | void EmitLoadStore(Condition cond, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 680 | bool load, |
| 681 | bool byte, |
| 682 | bool half, |
| 683 | bool is_signed, |
| 684 | Register rd, |
| 685 | const Address& ad); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 686 | |
| 687 | void EmitMemOpAddressMode3(Condition cond, |
| 688 | int32_t mode, |
| 689 | Register rd, |
| 690 | const Address& ad); |
| 691 | |
| 692 | void EmitMultiMemOp(Condition cond, |
| 693 | BlockAddressMode am, |
| 694 | bool load, |
| 695 | Register base, |
| 696 | RegList regs); |
| 697 | |
| 698 | void EmitMulOp(Condition cond, |
| 699 | int32_t opcode, |
| 700 | Register rd, |
| 701 | Register rn, |
| 702 | Register rm, |
| 703 | Register rs); |
| 704 | |
| 705 | void EmitVFPsss(Condition cond, |
| 706 | int32_t opcode, |
| 707 | SRegister sd, |
| 708 | SRegister sn, |
| 709 | SRegister sm); |
| 710 | |
| 711 | void EmitVFPddd(Condition cond, |
| 712 | int32_t opcode, |
| 713 | DRegister dd, |
| 714 | DRegister dn, |
| 715 | DRegister dm); |
| 716 | |
| 717 | void EmitVFPsd(Condition cond, |
| 718 | int32_t opcode, |
| 719 | SRegister sd, |
| 720 | DRegister dm); |
| 721 | |
| 722 | void EmitVFPds(Condition cond, |
| 723 | int32_t opcode, |
| 724 | DRegister dd, |
| 725 | SRegister sm); |
| 726 | |
| 727 | void EmitVPushPop(uint32_t reg, int nregs, bool push, bool dbl, Condition cond); |
| 728 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 729 | void EmitBranch(Condition cond, Label* label, bool link, bool x); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 730 | static int32_t EncodeBranchOffset(int32_t offset, int32_t inst); |
| 731 | static int DecodeBranchOffset(int32_t inst); |
| 732 | int32_t EncodeTstOffset(int offset, int32_t inst); |
| 733 | int DecodeTstOffset(int32_t inst); |
Vladimir Marko | 73cf0fb | 2015-07-30 15:07:22 +0100 | [diff] [blame] | 734 | void EmitShift(Register rd, Register rm, Shift shift, uint8_t amount, |
| 735 | Condition cond = AL, SetCc set_cc = kCcDontCare); |
| 736 | void EmitShift(Register rd, Register rn, Shift shift, Register rm, |
| 737 | Condition cond = AL, SetCc set_cc = kCcDontCare); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 738 | |
Nicolas Geoffray | d126ba1 | 2015-05-20 11:25:27 +0100 | [diff] [blame] | 739 | // Whether the assembler can relocate branches. If false, unresolved branches will be |
| 740 | // emitted on 32bits. |
| 741 | bool can_relocate_branches_; |
| 742 | |
| 743 | // Force the assembler to use 32 bit thumb2 instructions. |
| 744 | bool force_32bit_; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 745 | |
| 746 | // IfThen conditions. Used to check that conditional instructions match the preceding IT. |
| 747 | Condition it_conditions_[4]; |
| 748 | uint8_t it_cond_index_; |
| 749 | Condition next_condition_; |
| 750 | |
| 751 | void SetItCondition(ItState s, Condition cond, uint8_t index); |
| 752 | |
| 753 | void CheckCondition(Condition cond) { |
| 754 | CHECK_EQ(cond, next_condition_); |
| 755 | |
| 756 | // Move to the next condition if there is one. |
| 757 | if (it_cond_index_ < 3) { |
| 758 | ++it_cond_index_; |
| 759 | next_condition_ = it_conditions_[it_cond_index_]; |
| 760 | } else { |
| 761 | next_condition_ = AL; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | void CheckConditionLastIt(Condition cond) { |
| 766 | if (it_cond_index_ < 3) { |
| 767 | // Check that the next condition is AL. This means that the |
| 768 | // current condition is the last in the IT block. |
| 769 | CHECK_EQ(it_conditions_[it_cond_index_ + 1], AL); |
| 770 | } |
| 771 | CheckCondition(cond); |
| 772 | } |
| 773 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 774 | FixupId AddFixup(Fixup fixup) { |
| 775 | FixupId fixup_id = static_cast<FixupId>(fixups_.size()); |
| 776 | fixups_.push_back(fixup); |
| 777 | // For iterating using FixupId, we need the next id to be representable. |
| 778 | DCHECK_EQ(static_cast<size_t>(static_cast<FixupId>(fixups_.size())), fixups_.size()); |
| 779 | return fixup_id; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 782 | Fixup* GetFixup(FixupId fixup_id) { |
| 783 | DCHECK_LT(fixup_id, fixups_.size()); |
| 784 | return &fixups_[fixup_id]; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 787 | void BindLabel(Label* label, uint32_t bound_pc); |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 788 | uint32_t BindLiterals(); |
| 789 | void BindJumpTables(uint32_t code_size); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 790 | void AdjustFixupIfNeeded(Fixup* fixup, uint32_t* current_code_size, |
| 791 | std::deque<FixupId>* fixups_to_recalculate); |
| 792 | uint32_t AdjustFixups(); |
| 793 | void EmitFixups(uint32_t adjusted_code_size); |
| 794 | void EmitLiterals(); |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 795 | void EmitJumpTables(); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 796 | void PatchCFI(); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 797 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 798 | static int16_t BEncoding16(int32_t offset, Condition cond); |
| 799 | static int32_t BEncoding32(int32_t offset, Condition cond); |
| 800 | static int16_t CbxzEncoding16(Register rn, int32_t offset, Condition cond); |
| 801 | static int16_t CmpRnImm8Encoding16(Register rn, int32_t value); |
| 802 | static int16_t AddRdnRmEncoding16(Register rdn, Register rm); |
| 803 | static int32_t MovwEncoding32(Register rd, int32_t value); |
| 804 | static int32_t MovtEncoding32(Register rd, int32_t value); |
| 805 | static int32_t MovModImmEncoding32(Register rd, int32_t value); |
| 806 | static int16_t LdrLitEncoding16(Register rt, int32_t offset); |
| 807 | static int32_t LdrLitEncoding32(Register rt, int32_t offset); |
| 808 | static int32_t LdrdEncoding32(Register rt, Register rt2, Register rn, int32_t offset); |
| 809 | static int32_t VldrsEncoding32(SRegister sd, Register rn, int32_t offset); |
| 810 | static int32_t VldrdEncoding32(DRegister dd, Register rn, int32_t offset); |
| 811 | static int16_t LdrRtRnImm5Encoding16(Register rt, Register rn, int32_t offset); |
| 812 | static int32_t LdrRtRnImm12Encoding(Register rt, Register rn, int32_t offset); |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 813 | static int16_t AdrEncoding16(Register rd, int32_t offset); |
| 814 | static int32_t AdrEncoding32(Register rd, int32_t offset); |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 815 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 816 | std::vector<Fixup> fixups_; |
Vladimir Marko | 6b756b5 | 2015-07-14 11:58:38 +0100 | [diff] [blame] | 817 | std::unique_ptr<FixupId[]> fixup_dependents_; |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 818 | |
| 819 | // Use std::deque<> for literal labels to allow insertions at the end |
| 820 | // without invalidating pointers and references to existing elements. |
| 821 | std::deque<Literal> literals_; |
| 822 | |
Andreas Gampe | 7cffc3b | 2015-10-19 21:31:53 -0700 | [diff] [blame] | 823 | // Jump table list. |
| 824 | std::deque<JumpTable> jump_tables_; |
| 825 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 826 | // Data for AdjustedPosition(), see the description there. |
| 827 | uint32_t last_position_adjustment_; |
| 828 | uint32_t last_old_position_; |
| 829 | FixupId last_fixup_id_; |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 830 | }; |
| 831 | |
| 832 | } // namespace arm |
| 833 | } // namespace art |
| 834 | |
| 835 | #endif // ART_COMPILER_UTILS_ARM_ASSEMBLER_THUMB2_H_ |