| OLD | NEW |
| 1 //===- subzero/src/IceAssemblerARM32.cpp - Assembler for ARM32 --*- C++ -*-===// | 1 //===- subzero/src/IceAssemblerARM32.cpp - Assembler for ARM32 --*- C++ -*-===// |
| 2 // | 2 // |
| 3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 4 // for details. All rights reserved. Use of this source code is governed by a | 4 // for details. All rights reserved. Use of this source code is governed by a |
| 5 // BSD-style license that can be found in the LICENSE file. | 5 // BSD-style license that can be found in the LICENSE file. |
| 6 // | 6 // |
| 7 // Modified by the Subzero authors. | 7 // Modified by the Subzero authors. |
| 8 // | 8 // |
| 9 //===----------------------------------------------------------------------===// | 9 //===----------------------------------------------------------------------===// |
| 10 // | 10 // |
| 11 // The Subzero Code Generator | 11 // The Subzero Code Generator |
| 12 // | 12 // |
| 13 // This file is distributed under the University of Illinois Open Source | 13 // This file is distributed under the University of Illinois Open Source |
| 14 // License. See LICENSE.TXT for details. | 14 // License. See LICENSE.TXT for details. |
| 15 // | 15 // |
| 16 //===----------------------------------------------------------------------===// | 16 //===----------------------------------------------------------------------===// |
| 17 /// | 17 /// |
| 18 /// \file | 18 /// \file |
| 19 /// This file implements the Assembler class for ARM32. | 19 /// This file implements the Assembler class for ARM32. |
| 20 /// | 20 /// |
| 21 //===----------------------------------------------------------------------===// | 21 //===----------------------------------------------------------------------===// |
| 22 | 22 |
| 23 #include "IceAssemblerARM32.h" | 23 #include "IceAssemblerARM32.h" |
| 24 | 24 |
| 25 namespace Ice { | 25 namespace { |
| 26 |
| 27 using namespace Ice; |
| 26 | 28 |
| 27 // The following define individual bits. | 29 // The following define individual bits. |
| 28 static constexpr uint32_t B0 = 1; | 30 static constexpr uint32_t B0 = 1; |
| 29 static constexpr uint32_t B2 = 1 << 2; | 31 static constexpr uint32_t B2 = 1 << 2; |
| 30 static constexpr uint32_t B3 = 1 << 3; | 32 static constexpr uint32_t B3 = 1 << 3; |
| 31 static constexpr uint32_t B4 = 1 << 4; | 33 static constexpr uint32_t B4 = 1 << 4; |
| 32 static constexpr uint32_t B5 = 1 << 5; | 34 static constexpr uint32_t B5 = 1 << 5; |
| 33 static constexpr uint32_t B6 = 1 << 6; | 35 static constexpr uint32_t B6 = 1 << 6; |
| 34 static constexpr uint32_t B21 = 1 << 21; | 36 static constexpr uint32_t B21 = 1 << 21; |
| 35 static constexpr uint32_t B24 = 1 << 24; | 37 static constexpr uint32_t B24 = 1 << 24; |
| 36 | 38 |
| 37 // Constants used for the decoding or encoding of the individual fields of | 39 // Constants used for the decoding or encoding of the individual fields of |
| 38 // instructions. Based on ARM section A5.1. | 40 // instructions. Based on ARM section A5.1. |
| 39 static constexpr uint32_t kConditionShift = 28; | 41 static constexpr uint32_t kConditionShift = 28; |
| 40 static constexpr uint32_t kOpcodeShift = 21; | 42 static constexpr uint32_t kOpcodeShift = 21; |
| 41 static constexpr uint32_t kRdShift = 12; | 43 static constexpr uint32_t kRdShift = 12; |
| 42 static constexpr uint32_t kRmShift = 0; | 44 static constexpr uint32_t kRmShift = 0; |
| 43 static constexpr uint32_t kRnShift = 16; | 45 static constexpr uint32_t kRnShift = 16; |
| 44 static constexpr uint32_t kSShift = 20; | 46 static constexpr uint32_t kSShift = 20; |
| 45 static constexpr uint32_t kTypeShift = 25; | 47 static constexpr uint32_t kTypeShift = 25; |
| 46 | 48 |
| 47 // Immediate instruction fields encoding. | 49 // Immediate instruction fields encoding. |
| 48 static constexpr uint32_t kImmed8Bits = 8; | 50 static constexpr uint32_t kImmed8Bits = 8; |
| 49 static constexpr uint32_t kImmed8Shift = 0; | 51 static constexpr uint32_t kImmed8Shift = 0; |
| 50 static constexpr uint32_t kRotateBits = 4; | 52 static constexpr uint32_t kRotateBits = 4; |
| 51 static constexpr uint32_t kRotateShift = 8; | 53 static constexpr uint32_t kRotateShift = 8; |
| 52 | 54 |
| 53 // Types of instructions. | |
| 54 static constexpr uint32_t kInstTypeImmediate = 1; | |
| 55 | |
| 56 inline uint32_t encodeBool(bool b) { return b ? 1 : 0; } | 55 inline uint32_t encodeBool(bool b) { return b ? 1 : 0; } |
| 57 | 56 |
| 58 inline uint32_t encodeGPRRegister(RegARM32::GPRRegister Rn) { | 57 inline uint32_t encodeGPRRegister(RegARM32::GPRRegister Rn) { |
| 59 return static_cast<uint32_t>(Rn); | 58 return static_cast<uint32_t>(Rn); |
| 60 } | 59 } |
| 61 | 60 |
| 62 inline bool isGPRRegisterDefined(RegARM32::GPRRegister R) { | 61 inline bool isGPRRegisterDefined(RegARM32::GPRRegister R) { |
| 63 return R != RegARM32::Encoded_Not_GPR; | 62 return R != RegARM32::Encoded_Not_GPR; |
| 64 } | 63 } |
| 65 | 64 |
| 66 inline bool isGPRRegisterDefined(uint32_t R) { | 65 inline bool isGPRRegisterDefined(uint32_t R) { |
| 67 return R != encodeGPRRegister(RegARM32::Encoded_Not_GPR); | 66 return R != encodeGPRRegister(RegARM32::Encoded_Not_GPR); |
| 68 } | 67 } |
| 69 | 68 |
| 70 inline bool isConditionDefined(CondARM32::Cond Cond) { | 69 inline bool isConditionDefined(CondARM32::Cond Cond) { |
| 71 return Cond != CondARM32::kNone; | 70 return Cond != CondARM32::kNone; |
| 72 } | 71 } |
| 73 | 72 |
| 74 inline uint32_t encodeCondition(CondARM32::Cond Cond) { | 73 inline uint32_t encodeCondition(CondARM32::Cond Cond) { |
| 75 return static_cast<uint32_t>(Cond); | 74 return static_cast<uint32_t>(Cond); |
| 76 } | 75 } |
| 77 | 76 |
| 78 // Converts rotated immediate into imm12. | 77 // The way an operand was decoded in function decode below. |
| 79 inline uint32_t encodeImm12FromFlexImm(const OperandARM32FlexImm &FlexImm) { | 78 enum DecodedResult { |
| 80 uint32_t Immed8 = FlexImm.getImm(); | 79 CantDecode = 0, // I.e. will fail in test. |
| 81 uint32_t Rotate = FlexImm.getRotateAmt(); | 80 DecodedAsRegister, |
| 82 assert((Rotate < (1 << kRotateBits)) && (Immed8 < (1 << kImmed8Bits))); | 81 DecodedAsRotatedImm8 |
| 83 return (Rotate << kRotateShift) | (Immed8 << kImmed8Shift); | 82 }; |
| 83 |
| 84 DecodedResult decode(const Operand *Opnd, uint32_t &Value) { |
| 85 if (const auto *Var = llvm::dyn_cast<Variable>(Opnd)) { |
| 86 if (Var->hasReg()) { |
| 87 Value = Var->getRegNum(); |
| 88 return DecodedAsRegister; |
| 89 } |
| 90 } else if (const auto *FlexImm = llvm::dyn_cast<OperandARM32FlexImm>(Opnd)) { |
| 91 uint32_t Immed8 = FlexImm->getImm(); |
| 92 uint32_t Rotate = FlexImm->getRotateAmt(); |
| 93 assert((Rotate < (1 << kRotateBits)) && (Immed8 < (1 << kImmed8Bits))); |
| 94 Value = (Rotate << kRotateShift) | (Immed8 << kImmed8Shift); |
| 95 return DecodedAsRotatedImm8; |
| 96 } |
| 97 return CantDecode; |
| 84 } | 98 } |
| 85 | 99 |
| 100 } // end of anonymous namespace |
| 101 |
| 102 namespace Ice { |
| 103 |
| 86 Label *ARM32::AssemblerARM32::getOrCreateLabel(SizeT Number, | 104 Label *ARM32::AssemblerARM32::getOrCreateLabel(SizeT Number, |
| 87 LabelVector &Labels) { | 105 LabelVector &Labels) { |
| 88 Label *L = nullptr; | 106 Label *L = nullptr; |
| 89 if (Number == Labels.size()) { | 107 if (Number == Labels.size()) { |
| 90 L = new (this->allocate<Label>()) Label(); | 108 L = new (this->allocate<Label>()) Label(); |
| 91 Labels.push_back(L); | 109 Labels.push_back(L); |
| 92 return L; | 110 return L; |
| 93 } | 111 } |
| 94 if (Number > Labels.size()) { | 112 if (Number > Labels.size()) { |
| 95 Labels.resize(Number + 1); | 113 Labels.resize(Number + 1); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 113 } | 131 } |
| 114 // TODO(kschimpf) Decide if we have near jumps. | 132 // TODO(kschimpf) Decide if we have near jumps. |
| 115 label->bindTo(bound); | 133 label->bindTo(bound); |
| 116 } | 134 } |
| 117 | 135 |
| 118 void ARM32::AssemblerARM32::emitType01(CondARM32::Cond Cond, uint32_t Type, | 136 void ARM32::AssemblerARM32::emitType01(CondARM32::Cond Cond, uint32_t Type, |
| 119 uint32_t Opcode, bool SetCc, uint32_t Rn, | 137 uint32_t Opcode, bool SetCc, uint32_t Rn, |
| 120 uint32_t Rd, uint32_t Imm12) { | 138 uint32_t Rd, uint32_t Imm12) { |
| 121 assert(isGPRRegisterDefined(Rd)); | 139 assert(isGPRRegisterDefined(Rd)); |
| 122 assert(Cond != CondARM32::kNone); | 140 assert(Cond != CondARM32::kNone); |
| 141 AssemblerBuffer::EnsureCapacity ensured(&Buffer); |
| 123 uint32_t Encoding = encodeCondition(Cond) << kConditionShift | | 142 uint32_t Encoding = encodeCondition(Cond) << kConditionShift | |
| 124 (Type << kTypeShift) | (Opcode << kOpcodeShift) | | 143 (Type << kTypeShift) | (Opcode << kOpcodeShift) | |
| 125 (encodeBool(SetCc) << kSShift) | (Rn << kRnShift) | | 144 (encodeBool(SetCc) << kSShift) | (Rn << kRnShift) | |
| 126 (Rd << kRdShift) | Imm12; | 145 (Rd << kRdShift) | Imm12; |
| 127 emitInst(Encoding); | 146 emitInst(Encoding); |
| 128 } | 147 } |
| 129 | 148 |
| 149 void ARM32::AssemblerARM32::add(const Operand *OpRd, const Operand *OpRn, |
| 150 const Operand *OpSrc1, bool SetFlags, |
| 151 CondARM32::Cond Cond) { |
| 152 // Note: Loop is used so that we can short circuit using break; |
| 153 do { |
| 154 uint32_t Rd; |
| 155 if (decode(OpRd, Rd) != DecodedAsRegister) |
| 156 break; |
| 157 uint32_t Rn; |
| 158 if (decode(OpRn, Rn) != DecodedAsRegister) |
| 159 break; |
| 160 uint32_t Src1Value; |
| 161 // TODO(kschimpf) Other possible decodings of add. |
| 162 if (decode(OpSrc1, Src1Value) == DecodedAsRotatedImm8) { |
| 163 // ADD (Immediate): See ARM section A8.8.5, rule A1. |
| 164 // cccc0010100snnnnddddiiiiiiiiiiii where cccc=Cond, dddd=Rd, nnnn=Rn, |
| 165 // s=SetFlags and iiiiiiiiiiii=Src1Value |
| 166 if (!isConditionDefined(Cond) || (Rd == RegARM32::Reg_pc && SetFlags) || |
| 167 (Rn == RegARM32::Reg_lr) || (Rn == RegARM32::Reg_pc && SetFlags)) |
| 168 // Conditions of rule violated. |
| 169 break; |
| 170 uint32_t Add = B2; // 0100 |
| 171 uint32_t InstType = 1; |
| 172 emitType01(Cond, InstType, Add, SetFlags, Rn, Rd, Src1Value); |
| 173 return; |
| 174 } |
| 175 } while (0); |
| 176 UnimplementedError(Ctx->getFlags()); |
| 177 } |
| 178 |
| 130 void ARM32::AssemblerARM32::bkpt(uint16_t imm16) { | 179 void ARM32::AssemblerARM32::bkpt(uint16_t imm16) { |
| 131 AssemblerBuffer::EnsureCapacity ensured(&Buffer); | 180 AssemblerBuffer::EnsureCapacity ensured(&Buffer); |
| 132 uint32_t Encoding = (CondARM32::AL << kConditionShift) | B24 | B21 | | 181 uint32_t Encoding = (CondARM32::AL << kConditionShift) | B24 | B21 | |
| 133 ((imm16 >> 4) << 8) | B6 | B5 | B4 | (imm16 & 0xf); | 182 ((imm16 >> 4) << 8) | B6 | B5 | B4 | (imm16 & 0xf); |
| 134 emitInst(Encoding); | 183 emitInst(Encoding); |
| 135 } | 184 } |
| 136 | 185 |
| 137 void ARM32::AssemblerARM32::bx(RegARM32::GPRRegister Rm, CondARM32::Cond Cond) { | 186 void ARM32::AssemblerARM32::bx(RegARM32::GPRRegister Rm, CondARM32::Cond Cond) { |
| 138 // cccc000100101111111111110001mmmm where mmmm=rm and cccc=Cond. | 187 // cccc000100101111111111110001mmmm where mmmm=rm and cccc=Cond. |
| 139 // (ARM section A8.8.27, encoding A1). | 188 // (ARM section A8.8.27, encoding A1). |
| 140 assert(isGPRRegisterDefined(Rm)); | 189 assert(isGPRRegisterDefined(Rm)); |
| 141 assert(isConditionDefined(Cond)); | 190 assert(isConditionDefined(Cond)); |
| 142 AssemblerBuffer::EnsureCapacity ensured(&Buffer); | 191 AssemblerBuffer::EnsureCapacity ensured(&Buffer); |
| 143 uint32_t Encoding = (encodeCondition(Cond) << kConditionShift) | B24 | B21 | | 192 uint32_t Encoding = (encodeCondition(Cond) << kConditionShift) | B24 | B21 | |
| 144 (0xfff << 8) | B4 | (encodeGPRRegister(Rm) << kRmShift); | 193 (0xfff << 8) | B4 | (encodeGPRRegister(Rm) << kRmShift); |
| 145 emitInst(Encoding); | 194 emitInst(Encoding); |
| 146 } | 195 } |
| 147 | 196 |
| 148 void ARM32::AssemblerARM32::mov(RegARM32::GPRRegister Rd, | 197 void ARM32::AssemblerARM32::mov(const Operand *OpRd, const Operand *OpSrc, |
| 149 const OperandARM32FlexImm &FlexImm, | |
| 150 CondARM32::Cond Cond) { | 198 CondARM32::Cond Cond) { |
| 151 // cccc0011101s0000ddddiiiiiiiiiiii (ARM section A8.8.102, encoding A1) | 199 // Note: Loop is used so that we can short ciruit using break; |
| 152 assert(isConditionDefined(Cond)); | 200 do { |
| 153 AssemblerBuffer::EnsureCapacity ensured(&Buffer); | 201 uint32_t Rd; |
| 154 bool SetCc = false; // Note: We don't use movs in this assembler. | 202 if (decode(OpRd, Rd) != DecodedAsRegister) |
| 155 uint32_t Rn = 0; | 203 break; |
| 156 uint32_t Mov = B3 | B2 | B0; // 1101. | 204 uint32_t Src; |
| 157 emitType01(Cond, kInstTypeImmediate, Mov, SetCc, Rn, encodeGPRRegister(Rd), | 205 // TODO(kschimpf) Handle other forms of mov. |
| 158 encodeImm12FromFlexImm(FlexImm)); | 206 if (decode(OpSrc, Src) == DecodedAsRotatedImm8) { |
| 207 // cccc0011101s0000ddddiiiiiiiiiiii (ARM section A8.8.102, encoding A1) |
| 208 // Note: We don't use movs in this assembler. |
| 209 constexpr bool SetFlags = false; |
| 210 if (!isConditionDefined(Cond) || (Rd == RegARM32::Reg_pc && SetFlags)) |
| 211 // Conditions of rule violated. |
| 212 break; |
| 213 uint32_t Rn = 0; |
| 214 uint32_t Mov = B3 | B2 | B0; // 1101. |
| 215 uint32_t InstType = 1; |
| 216 emitType01(Cond, InstType, Mov, SetFlags, Rn, Rd, Src); |
| 217 return; |
| 218 } |
| 219 } while (0); |
| 220 UnimplementedError(Ctx->getFlags()); |
| 159 } | 221 } |
| 160 | 222 |
| 161 } // end of namespace Ice | 223 } // end of namespace Ice |
| OLD | NEW |