blob: 7359a1129488d293fd7ad4d2e48ed0878cbc0223 [file] [log] [blame]
Jim Grosbach00351b72010-10-08 17:28:40 +00001//===-- ARM/ARMCodeEmitter.cpp - Convert ARM code to machine code ---------===//
Evan Cheng9546a5c2007-07-05 21:15:40 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng9546a5c2007-07-05 21:15:40 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the pass that transforms the ARM machine instructions into
11// relocatable machine code.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng19d64ba2008-10-29 23:55:43 +000015#define DEBUG_TYPE "jit"
Evan Cheng3be5b722008-09-02 06:52:38 +000016#include "ARM.h"
Craig Topper07720d82012-03-25 23:49:58 +000017#include "ARMBaseInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "ARMConstantPoolValue.h"
Evan Cheng3be5b722008-09-02 06:52:38 +000019#include "ARMRelocations.h"
Evan Cheng9546a5c2007-07-05 21:15:40 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Evan Chenga20cde32011-07-20 23:34:39 +000022#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/ADT/Statistic.h"
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000024#include "llvm/CodeGen/JITCodeEmitter.h"
Evan Cheng933b3922008-09-18 07:28:19 +000025#include "llvm/CodeGen/MachineConstantPool.h"
Evan Cheng9546a5c2007-07-05 21:15:40 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng7095cd22008-11-07 09:06:08 +000028#include "llvm/CodeGen/MachineJumpTableInfo.h"
Daniel Dunbarbc528b12009-09-21 05:58:35 +000029#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Cheng9546a5c2007-07-05 21:15:40 +000030#include "llvm/CodeGen/Passes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/Constants.h"
32#include "llvm/IR/DerivedTypes.h"
33#include "llvm/IR/Function.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/PassManager.h"
Evan Cheng25a39092008-09-13 01:15:21 +000035#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000036#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/raw_ostream.h"
Evan Cheng7095cd22008-11-07 09:06:08 +000038#ifndef NDEBUG
39#include <iomanip>
40#endif
Evan Cheng9546a5c2007-07-05 21:15:40 +000041using namespace llvm;
42
43STATISTIC(NumEmitted, "Number of machine instructions emitted");
44
45namespace {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000046
Chris Lattner8d806872010-02-02 21:48:51 +000047 class ARMCodeEmitter : public MachineFunctionPass {
Evan Cheng933b3922008-09-18 07:28:19 +000048 ARMJITInfo *JTI;
Craig Topper07720d82012-03-25 23:49:58 +000049 const ARMBaseInstrInfo *II;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000050 const DataLayout *TD;
Evan Chengf6b24042009-09-10 01:23:53 +000051 const ARMSubtarget *Subtarget;
Evan Cheng933b3922008-09-18 07:28:19 +000052 TargetMachine &TM;
Chris Lattner8d806872010-02-02 21:48:51 +000053 JITCodeEmitter &MCE;
Chris Lattner34adc8d2010-03-14 01:41:15 +000054 MachineModuleInfo *MMI;
Evan Cheng20dbb3b2008-10-31 19:55:13 +000055 const std::vector<MachineConstantPoolEntry> *MCPEs;
Evan Cheng7095cd22008-11-07 09:06:08 +000056 const std::vector<MachineJumpTableEntry> *MJTEs;
57 bool IsPIC;
Bob Wilson4469a892010-06-28 22:23:17 +000058 bool IsThumb;
Bob Wilsona6fe21a2010-03-17 21:16:45 +000059
Craig Topper6bc27bf2014-03-10 02:09:33 +000060 void getAnalysisUsage(AnalysisUsage &AU) const override {
Daniel Dunbarbc528b12009-09-21 05:58:35 +000061 AU.addRequired<MachineModuleInfo>();
62 MachineFunctionPass::getAnalysisUsage(AU);
63 }
Bob Wilsona6fe21a2010-03-17 21:16:45 +000064
Evan Cheng9546a5c2007-07-05 21:15:40 +000065 static char ID;
Chris Lattner8d806872010-02-02 21:48:51 +000066 public:
67 ARMCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
Owen Andersona7aed182010-08-06 18:33:48 +000068 : MachineFunctionPass(ID), JTI(0),
Craig Topper07720d82012-03-25 23:49:58 +000069 II((const ARMBaseInstrInfo *)tm.getInstrInfo()),
Micah Villmowcdfe20b2012-10-08 16:38:25 +000070 TD(tm.getDataLayout()), TM(tm),
Bob Wilson4469a892010-06-28 22:23:17 +000071 MCE(mce), MCPEs(0), MJTEs(0),
72 IsPIC(TM.getRelocationModel() == Reloc::PIC_), IsThumb(false) {}
Bob Wilsona6fe21a2010-03-17 21:16:45 +000073
Chris Lattner8d806872010-02-02 21:48:51 +000074 /// getBinaryCodeForInstr - This function, generated by the
75 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
76 /// machine instructions.
Owen Andersond845d9d2012-01-24 18:37:29 +000077 uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const;
Evan Cheng9546a5c2007-07-05 21:15:40 +000078
Craig Topper6bc27bf2014-03-10 02:09:33 +000079 bool runOnMachineFunction(MachineFunction &MF) override;
Evan Cheng9546a5c2007-07-05 21:15:40 +000080
Craig Topper6bc27bf2014-03-10 02:09:33 +000081 const char *getPassName() const override {
Evan Cheng9546a5c2007-07-05 21:15:40 +000082 return "ARM Machine Code Emitter";
83 }
84
85 void emitInstruction(const MachineInstr &MI);
Evan Cheng3be5b722008-09-02 06:52:38 +000086
87 private:
Evan Cheng933b3922008-09-18 07:28:19 +000088
Evan Chengfd2adbf2008-11-05 23:22:34 +000089 void emitWordLE(unsigned Binary);
Evan Chengad519bb2008-11-11 22:19:31 +000090 void emitDWordLE(uint64_t Binary);
Evan Cheng933b3922008-09-18 07:28:19 +000091 void emitConstPoolInstruction(const MachineInstr &MI);
Zonr Chang2da5aa12010-05-25 08:42:45 +000092 void emitMOVi32immInstruction(const MachineInstr &MI);
Evan Chengb870fd82008-11-06 02:25:39 +000093 void emitMOVi2piecesInstruction(const MachineInstr &MI);
Evan Cheng7095cd22008-11-07 09:06:08 +000094 void emitLEApcrelJTInstruction(const MachineInstr &MI);
Evan Cheng30f6f8f2008-11-14 20:09:11 +000095 void emitPseudoMoveInstruction(const MachineInstr &MI);
Evan Chengfd2adbf2008-11-05 23:22:34 +000096 void addPCLabel(unsigned LabelID);
Evan Cheng933b3922008-09-18 07:28:19 +000097 void emitPseudoInstruction(const MachineInstr &MI);
Evan Cheng33fa89c6f2008-09-12 22:01:15 +000098 unsigned getMachineSoRegOpValue(const MachineInstr &MI,
Evan Cheng6cc775f2011-06-28 19:10:37 +000099 const MCInstrDesc &MCID,
Evan Cheng467e6e82008-10-31 19:10:44 +0000100 const MachineOperand &MO,
Evan Cheng33fa89c6f2008-09-12 22:01:15 +0000101 unsigned OpIdx);
102
Evan Chengb870fd82008-11-06 02:25:39 +0000103 unsigned getMachineSoImmOpValue(unsigned SoImm);
Jim Grosbach4d0549e2008-11-03 18:38:31 +0000104 unsigned getAddrModeSBit(const MachineInstr &MI,
Evan Cheng6cc775f2011-06-28 19:10:37 +0000105 const MCInstrDesc &MCID) const;
Evan Chengd1424c42008-09-12 22:45:55 +0000106
Evan Chengfd2adbf2008-11-05 23:22:34 +0000107 void emitDataProcessingInstruction(const MachineInstr &MI,
Evan Cheng8467e242008-11-07 22:30:53 +0000108 unsigned ImplicitRd = 0,
Evan Chengfd2adbf2008-11-05 23:22:34 +0000109 unsigned ImplicitRn = 0);
Evan Cheng3be5b722008-09-02 06:52:38 +0000110
Evan Chengfd2adbf2008-11-05 23:22:34 +0000111 void emitLoadStoreInstruction(const MachineInstr &MI,
Evan Cheng7095cd22008-11-07 09:06:08 +0000112 unsigned ImplicitRd = 0,
Evan Chengfd2adbf2008-11-05 23:22:34 +0000113 unsigned ImplicitRn = 0);
Evan Cheng81889d012008-11-05 18:35:52 +0000114
Evan Chengfd2adbf2008-11-05 23:22:34 +0000115 void emitMiscLoadStoreInstruction(const MachineInstr &MI,
116 unsigned ImplicitRn = 0);
Evan Cheng81889d012008-11-05 18:35:52 +0000117
118 void emitLoadStoreMultipleInstruction(const MachineInstr &MI);
119
Evan Cheng2686c8f2008-11-06 01:21:28 +0000120 void emitMulFrmInstruction(const MachineInstr &MI);
Evan Cheng81889d012008-11-05 18:35:52 +0000121
Evan Cheng49d66522008-11-06 22:15:19 +0000122 void emitExtendInstruction(const MachineInstr &MI);
123
Evan Cheng98dc53e2008-11-07 01:41:35 +0000124 void emitMiscArithInstruction(const MachineInstr &MI);
125
Bob Wilson96649842010-08-11 00:01:18 +0000126 void emitSaturateInstruction(const MachineInstr &MI);
127
Evan Cheng81889d012008-11-05 18:35:52 +0000128 void emitBranchInstruction(const MachineInstr &MI);
129
Evan Cheng8467e242008-11-07 22:30:53 +0000130 void emitInlineJumpTable(unsigned JTIndex);
Evan Cheng7095cd22008-11-07 09:06:08 +0000131
Evan Cheng81889d012008-11-05 18:35:52 +0000132 void emitMiscBranchInstruction(const MachineInstr &MI);
Evan Cheng3be5b722008-09-02 06:52:38 +0000133
Evan Chengac2af2f2008-11-11 02:11:05 +0000134 void emitVFPArithInstruction(const MachineInstr &MI);
135
Evan Cheng38c9a142008-11-11 19:40:26 +0000136 void emitVFPConversionInstruction(const MachineInstr &MI);
137
Evan Cheng8cbbcb12008-11-11 21:48:44 +0000138 void emitVFPLoadStoreInstruction(const MachineInstr &MI);
139
140 void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI);
141
Bob Wilsonab0819e2010-06-29 17:34:07 +0000142 void emitNEONLaneInstruction(const MachineInstr &MI);
Bob Wilsonbe157b02010-06-29 20:13:29 +0000143 void emitNEONDupInstruction(const MachineInstr &MI);
Bob Wilsone70c8b12010-06-25 21:17:19 +0000144 void emitNEON1RegModImmInstruction(const MachineInstr &MI);
145 void emitNEON2RegInstruction(const MachineInstr &MI);
Bob Wilson2530ca02010-06-25 22:40:46 +0000146 void emitNEON3RegInstruction(const MachineInstr &MI);
Bob Wilson6eae5202010-06-11 21:34:50 +0000147
Evan Cheng3be5b722008-09-02 06:52:38 +0000148 /// getMachineOpValue - Return binary encoding of operand. If the machine
149 /// operand requires relocation, record the relocation and return zero.
Jim Grosbachb770c002010-10-08 17:45:54 +0000150 unsigned getMachineOpValue(const MachineInstr &MI,
151 const MachineOperand &MO) const;
152 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) const {
Evan Cheng3be5b722008-09-02 06:52:38 +0000153 return getMachineOpValue(MI, MI.getOperand(OpIdx));
154 }
Evan Cheng3be5b722008-09-02 06:52:38 +0000155
Jim Grosbachd9d31da2010-10-12 23:00:24 +0000156 // FIXME: The legacy JIT ARMCodeEmitter doesn't rely on the the
157 // TableGen'erated getBinaryCodeForInstr() function to encode any
158 // operand values, instead querying getMachineOpValue() directly for
159 // each operand it needs to encode. Thus, any of the new encoder
160 // helper functions can simply return 0 as the values the return
161 // are already handled elsewhere. They are placeholders to allow this
162 // encoder to continue to function until the MC encoder is sufficiently
163 // far along that this one can be eliminated entirely.
Jim Grosbach05dec8b12011-09-02 18:46:15 +0000164 unsigned NEONThumb2DataIPostEncoder(const MachineInstr &MI, unsigned Val)
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000165 const { return 0; }
Jim Grosbach05dec8b12011-09-02 18:46:15 +0000166 unsigned NEONThumb2LoadStorePostEncoder(const MachineInstr &MI,unsigned Val)
Owen Anderson99a8cb42010-11-11 21:36:43 +0000167 const { return 0; }
Jim Grosbach05dec8b12011-09-02 18:46:15 +0000168 unsigned NEONThumb2DupPostEncoder(const MachineInstr &MI,unsigned Val)
Owen Andersonce2250f2010-11-11 23:12:55 +0000169 const { return 0; }
Joey Goulydf686002013-07-17 13:59:38 +0000170 unsigned NEONThumb2V8PostEncoder(const MachineInstr &MI,unsigned Val)
171 const { return 0; }
Bill Wendling87240d42010-12-01 21:54:50 +0000172 unsigned VFPThumb2PostEncoder(const MachineInstr&MI, unsigned Val)
173 const { return 0; }
Jim Grosbachdc35e062010-12-01 19:47:31 +0000174 unsigned getAdrLabelOpValue(const MachineInstr &MI, unsigned Op)
175 const { return 0; }
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000176 unsigned getThumbAdrLabelOpValue(const MachineInstr &MI, unsigned Op)
177 const { return 0; }
Jim Grosbach9e199462010-12-06 23:57:07 +0000178 unsigned getThumbBLTargetOpValue(const MachineInstr &MI, unsigned Op)
179 const { return 0; }
Bill Wendling3392bfc2010-12-09 00:39:08 +0000180 unsigned getThumbBLXTargetOpValue(const MachineInstr &MI, unsigned Op)
181 const { return 0; }
Jim Grosbache119da12010-12-10 18:21:33 +0000182 unsigned getThumbBRTargetOpValue(const MachineInstr &MI, unsigned Op)
183 const { return 0; }
Jim Grosbach78485ad2010-12-10 17:13:40 +0000184 unsigned getThumbBCCTargetOpValue(const MachineInstr &MI, unsigned Op)
185 const { return 0; }
Jim Grosbach62b68112010-12-09 19:04:53 +0000186 unsigned getThumbCBTargetOpValue(const MachineInstr &MI, unsigned Op)
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000187 const { return 0; }
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000188 unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned Op)
189 const { return 0; }
Owen Anderson578074b2010-12-13 19:31:11 +0000190 unsigned getUnconditionalBranchTargetOpValue(const MachineInstr &MI,
191 unsigned Op) const { return 0; }
Jason W Kimd2e2f562011-02-04 19:47:15 +0000192 unsigned getARMBranchTargetOpValue(const MachineInstr &MI, unsigned Op)
193 const { return 0; }
Jim Grosbach7b811d32012-02-27 21:36:23 +0000194 unsigned getARMBLTargetOpValue(const MachineInstr &MI, unsigned Op)
195 const { return 0; }
Owen Andersonb205c022011-08-26 23:32:08 +0000196 unsigned getARMBLXTargetOpValue(const MachineInstr &MI, unsigned Op)
197 const { return 0; }
Jim Grosbachd9d31da2010-10-12 23:00:24 +0000198 unsigned getCCOutOpValue(const MachineInstr &MI, unsigned Op)
199 const { return 0; }
Jim Grosbach12e493a2010-10-12 23:18:08 +0000200 unsigned getSOImmOpValue(const MachineInstr &MI, unsigned Op)
201 const { return 0; }
Owen Anderson8fdd1722010-11-12 21:12:40 +0000202 unsigned getT2SOImmOpValue(const MachineInstr &MI, unsigned Op)
203 const { return 0; }
Owen Anderson04912702011-07-21 23:38:37 +0000204 unsigned getSORegRegOpValue(const MachineInstr &MI, unsigned Op)
205 const { return 0; }
206 unsigned getSORegImmOpValue(const MachineInstr &MI, unsigned Op)
Jim Grosbachefd53692010-10-12 23:53:58 +0000207 const { return 0; }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000208 unsigned getThumbAddrModeRegRegOpValue(const MachineInstr &MI, unsigned Op)
Owen Andersonb0fa1272010-12-10 22:11:13 +0000209 const { return 0; }
Owen Anderson50d662b2010-11-29 22:44:32 +0000210 unsigned getT2AddrModeImm8OpValue(const MachineInstr &MI, unsigned Op)
211 const { return 0; }
Jim Grosbach7db8d692011-09-08 22:07:06 +0000212 unsigned getT2Imm8s4OpValue(const MachineInstr &MI, unsigned Op)
213 const { return 0; }
Owen Anderson943fb602010-12-01 19:18:46 +0000214 unsigned getT2AddrModeImm8s4OpValue(const MachineInstr &MI, unsigned Op)
215 const { return 0; }
Jim Grosbacha05627e2011-09-09 18:37:27 +0000216 unsigned getT2AddrModeImm0_1020s4OpValue(const MachineInstr &MI,unsigned Op)
217 const { return 0; }
Owen Andersone22c7322010-11-30 00:14:31 +0000218 unsigned getT2AddrModeImm8OffsetOpValue(const MachineInstr &MI, unsigned Op)
219 const { return 0; }
Owen Anderson50d662b2010-11-29 22:44:32 +0000220 unsigned getT2AddrModeSORegOpValue(const MachineInstr &MI, unsigned Op)
221 const { return 0; }
Owen Anderson8fdd1722010-11-12 21:12:40 +0000222 unsigned getT2SORegOpValue(const MachineInstr &MI, unsigned Op)
223 const { return 0; }
Owen Anderson6d375e52010-12-14 00:36:49 +0000224 unsigned getT2AdrLabelOpValue(const MachineInstr &MI, unsigned Op)
225 const { return 0; }
Owen Andersona4b63e12010-11-02 22:28:01 +0000226 unsigned getAddrMode6AddressOpValue(const MachineInstr &MI, unsigned Op)
Owen Andersonad402342010-11-02 00:05:05 +0000227 const { return 0; }
Mon P Wang92ff16b2011-05-09 17:47:27 +0000228 unsigned getAddrMode6OneLane32AddressOpValue(const MachineInstr &MI,
229 unsigned Op)
230 const { return 0; }
Bob Wilson318ce7c2010-11-30 00:00:42 +0000231 unsigned getAddrMode6DupAddressOpValue(const MachineInstr &MI, unsigned Op)
232 const { return 0; }
Owen Andersona4b63e12010-11-02 22:28:01 +0000233 unsigned getAddrMode6OffsetOpValue(const MachineInstr &MI, unsigned Op)
Owen Anderson526ffd52010-11-02 01:24:55 +0000234 const { return 0; }
Jim Grosbach5edb03e2010-10-21 22:03:21 +0000235 unsigned getBitfieldInvertedMaskOpValue(const MachineInstr &MI,
236 unsigned Op) const { return 0; }
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000237 uint32_t getLdStSORegOpValue(const MachineInstr &MI, unsigned OpIdx)
238 const { return 0; }
Bill Wendlinge84eb992010-11-03 01:49:29 +0000239
240 unsigned getAddrModeImm12OpValue(const MachineInstr &MI, unsigned Op)
241 const {
242 // {17-13} = reg
243 // {12} = (U)nsigned (add == '1', sub == '0')
244 // {11-0} = imm12
Bill Wendling603bd8f2010-11-02 22:31:46 +0000245 const MachineOperand &MO = MI.getOperand(Op);
246 const MachineOperand &MO1 = MI.getOperand(Op + 1);
247 if (!MO.isReg()) {
248 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
249 return 0;
Jim Grosbach333b0a92010-10-27 19:55:59 +0000250 }
Eric Christopher6ac277c2012-08-09 22:10:21 +0000251 unsigned Reg = II->getRegisterInfo().getEncodingValue(MO.getReg());
Bill Wendlinge84eb992010-11-03 01:49:29 +0000252 int32_t Imm12 = MO1.getImm();
Bill Wendling603bd8f2010-11-02 22:31:46 +0000253 uint32_t Binary;
Bill Wendlinge84eb992010-11-03 01:49:29 +0000254 Binary = Imm12 & 0xfff;
255 if (Imm12 >= 0)
256 Binary |= (1 << 12);
257 Binary |= (Reg << 13);
258 return Binary;
259 }
Jason W Kim5a97bd82010-11-18 23:37:15 +0000260
Evan Cheng965b3c72011-01-13 07:58:56 +0000261 unsigned getHiLo16ImmOpValue(const MachineInstr &MI, unsigned Op) const {
Jason W Kim5a97bd82010-11-18 23:37:15 +0000262 return 0;
263 }
264
Jim Grosbach38b469e2010-11-15 20:47:07 +0000265 uint32_t getAddrMode2OffsetOpValue(const MachineInstr &MI, unsigned OpIdx)
266 const { return 0;}
Jim Grosbachd3595712011-08-03 23:50:40 +0000267 uint32_t getPostIdxRegOpValue(const MachineInstr &MI, unsigned OpIdx)
268 const { return 0;}
Jim Grosbach68685e62010-11-11 16:55:29 +0000269 uint32_t getAddrMode3OffsetOpValue(const MachineInstr &MI, unsigned OpIdx)
270 const { return 0;}
Bill Wendling811c9362010-11-30 07:44:32 +0000271 uint32_t getAddrMode3OpValue(const MachineInstr &MI, unsigned Op)
272 const { return 0; }
Jim Grosbach49bcd6f2010-12-07 21:50:47 +0000273 uint32_t getAddrModeThumbSPOpValue(const MachineInstr &MI, unsigned Op)
274 const { return 0; }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000275 uint32_t getAddrModeISOpValue(const MachineInstr &MI, unsigned Op)
276 const { return 0; }
Bill Wendling8a6449c2010-12-08 01:57:09 +0000277 uint32_t getAddrModePCOpValue(const MachineInstr &MI, unsigned Op)
278 const { return 0; }
Bill Wendlinge84eb992010-11-03 01:49:29 +0000279 uint32_t getAddrMode5OpValue(const MachineInstr &MI, unsigned Op) const {
Bill Wendling0914d442010-11-20 00:26:37 +0000280 // {17-13} = reg
281 // {12} = (U)nsigned (add == '1', sub == '0')
282 // {11-0} = imm12
Bill Wendlinge84eb992010-11-03 01:49:29 +0000283 const MachineOperand &MO = MI.getOperand(Op);
284 const MachineOperand &MO1 = MI.getOperand(Op + 1);
285 if (!MO.isReg()) {
286 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
287 return 0;
288 }
Eric Christopher6ac277c2012-08-09 22:10:21 +0000289 unsigned Reg = II->getRegisterInfo().getEncodingValue(MO.getReg());
Bill Wendling0914d442010-11-20 00:26:37 +0000290 int32_t Imm12 = MO1.getImm();
291
292 // Special value for #-0
293 if (Imm12 == INT32_MIN)
294 Imm12 = 0;
295
296 // Immediate is always encoded as positive. The 'U' bit controls add vs
297 // sub.
298 bool isAdd = true;
299 if (Imm12 < 0) {
300 Imm12 = -Imm12;
301 isAdd = false;
302 }
303
304 uint32_t Binary = Imm12 & 0xfff;
305 if (isAdd)
306 Binary |= (1 << 12);
307 Binary |= (Reg << 13);
Bill Wendling603bd8f2010-11-02 22:31:46 +0000308 return Binary;
309 }
Jim Grosbach5f0d6162010-10-29 23:21:57 +0000310 unsigned getNEONVcvtImm32OpValue(const MachineInstr &MI, unsigned Op)
311 const { return 0; }
Jim Grosbachd9d31da2010-10-12 23:00:24 +0000312
Jim Grosbach74ef9e12010-10-30 00:37:59 +0000313 unsigned getRegisterListOpValue(const MachineInstr &MI, unsigned Op)
314 const { return 0; }
315
Bill Wendling77ad1dc2011-03-07 23:38:41 +0000316 unsigned getShiftRight8Imm(const MachineInstr &MI, unsigned Op)
Bill Wendling3b1459b2011-03-01 01:00:59 +0000317 const { return 0; }
Bill Wendling77ad1dc2011-03-07 23:38:41 +0000318 unsigned getShiftRight16Imm(const MachineInstr &MI, unsigned Op)
Bill Wendling3b1459b2011-03-01 01:00:59 +0000319 const { return 0; }
Bill Wendling77ad1dc2011-03-07 23:38:41 +0000320 unsigned getShiftRight32Imm(const MachineInstr &MI, unsigned Op)
321 const { return 0; }
322 unsigned getShiftRight64Imm(const MachineInstr &MI, unsigned Op)
Bill Wendling3b1459b2011-03-01 01:00:59 +0000323 const { return 0; }
324
Shih-wei Liaoe22abfa2010-05-26 00:02:28 +0000325 /// getMovi32Value - Return binary encoding of operand for movw/movt. If the
Jim Grosbach84511e12010-06-02 21:53:11 +0000326 /// machine operand requires relocation, record the relocation and return
327 /// zero.
Shih-wei Liaoe22abfa2010-05-26 00:02:28 +0000328 unsigned getMovi32Value(const MachineInstr &MI,const MachineOperand &MO,
Zonr Chang2da5aa12010-05-25 08:42:45 +0000329 unsigned Reloc);
Zonr Chang2da5aa12010-05-25 08:42:45 +0000330
Evan Chengfd2adbf2008-11-05 23:22:34 +0000331 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng3be5b722008-09-02 06:52:38 +0000332 ///
Evan Chengfd2adbf2008-11-05 23:22:34 +0000333 unsigned getShiftOp(unsigned Imm) const ;
Evan Cheng3be5b722008-09-02 06:52:38 +0000334
335 /// Routines that handle operands which add machine relocations which are
Evan Cheng8467e242008-11-07 22:30:53 +0000336 /// fixed up by the relocation stage.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000337 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
Jeffrey Yasskindb5f24c2009-11-07 08:51:52 +0000338 bool MayNeedFarStub, bool Indirect,
Jim Grosbachb770c002010-10-08 17:45:54 +0000339 intptr_t ACPV = 0) const;
340 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
341 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
342 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const;
Evan Cheng8467e242008-11-07 22:30:53 +0000343 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
Jim Grosbachb770c002010-10-08 17:45:54 +0000344 intptr_t JTBase = 0) const;
Eric Christopher6ac277c2012-08-09 22:10:21 +0000345 unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) const;
346 unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) const;
347 unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) const;
348 unsigned encodeNEONRd(const MachineInstr &MI, unsigned OpIdx) const;
349 unsigned encodeNEONRn(const MachineInstr &MI, unsigned OpIdx) const;
350 unsigned encodeNEONRm(const MachineInstr &MI, unsigned OpIdx) const;
Evan Cheng9546a5c2007-07-05 21:15:40 +0000351 };
Evan Cheng9546a5c2007-07-05 21:15:40 +0000352}
353
Chris Lattner8d806872010-02-02 21:48:51 +0000354char ARMCodeEmitter::ID = 0;
355
Bob Wilsona6fe21a2010-03-17 21:16:45 +0000356/// createARMJITCodeEmitterPass - Return a pass that emits the collected ARM
Chris Lattnerc83cfb9d2010-02-02 21:38:59 +0000357/// code to the specified MCE object.
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000358FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
359 JITCodeEmitter &JCE) {
Chris Lattner8d806872010-02-02 21:48:51 +0000360 return new ARMCodeEmitter(TM, JCE);
Evan Cheng9546a5c2007-07-05 21:15:40 +0000361}
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000362
Chris Lattner8d806872010-02-02 21:48:51 +0000363bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Benjamin Kramer5521b942012-12-21 19:09:53 +0000364 TargetMachine &Target = const_cast<TargetMachine&>(MF.getTarget());
365
366 assert((Target.getRelocationModel() != Reloc::Default ||
367 Target.getRelocationModel() != Reloc::Static) &&
Evan Cheng9546a5c2007-07-05 21:15:40 +0000368 "JIT relocation model must be set to static or default!");
Benjamin Kramer5521b942012-12-21 19:09:53 +0000369
370 JTI = static_cast<ARMJITInfo*>(Target.getJITInfo());
371 II = static_cast<const ARMBaseInstrInfo*>(Target.getInstrInfo());
372 TD = Target.getDataLayout();
373
Evan Chengf6b24042009-09-10 01:23:53 +0000374 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng20dbb3b2008-10-31 19:55:13 +0000375 MCPEs = &MF.getConstantPool()->getConstants();
Chris Lattnera14ac3fd2010-01-25 23:22:00 +0000376 MJTEs = 0;
377 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
Evan Cheng7095cd22008-11-07 09:06:08 +0000378 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Bob Wilson4469a892010-06-28 22:23:17 +0000379 IsThumb = MF.getInfo<ARMFunctionInfo>()->isThumbFunction();
Evan Cheng98161f52008-11-08 07:38:22 +0000380 JTI->Initialize(MF, IsPIC);
Chris Lattner34adc8d2010-03-14 01:41:15 +0000381 MMI = &getAnalysis<MachineModuleInfo>();
382 MCE.setModuleInfo(MMI);
Evan Cheng9546a5c2007-07-05 21:15:40 +0000383
384 do {
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000385 DEBUG(errs() << "JITTing function '"
Craig Toppera538d832012-08-22 06:07:19 +0000386 << MF.getName() << "'\n");
Evan Cheng9546a5c2007-07-05 21:15:40 +0000387 MCE.startFunction(MF);
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000388 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
Evan Cheng9546a5c2007-07-05 21:15:40 +0000389 MBB != E; ++MBB) {
390 MCE.StartMachineBasicBlock(MBB);
Evan Cheng2a81dd42011-12-06 22:12:01 +0000391 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Evan Cheng9546a5c2007-07-05 21:15:40 +0000392 I != E; ++I)
393 emitInstruction(*I);
394 }
395 } while (MCE.finishFunction(MF));
396
397 return false;
398}
399
Evan Chengfd2adbf2008-11-05 23:22:34 +0000400/// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng3be5b722008-09-02 06:52:38 +0000401///
Chris Lattner8d806872010-02-02 21:48:51 +0000402unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
Evan Chengfd2adbf2008-11-05 23:22:34 +0000403 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000404 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng3be5b722008-09-02 06:52:38 +0000405 case ARM_AM::asr: return 2;
406 case ARM_AM::lsl: return 0;
407 case ARM_AM::lsr: return 1;
Evan Chengf7c6eff2007-08-07 01:37:15 +0000408 case ARM_AM::ror:
Evan Cheng3be5b722008-09-02 06:52:38 +0000409 case ARM_AM::rrx: return 3;
Evan Chengf7c6eff2007-08-07 01:37:15 +0000410 }
Evan Chengf7c6eff2007-08-07 01:37:15 +0000411}
412
Shih-wei Liaoe22abfa2010-05-26 00:02:28 +0000413/// getMovi32Value - Return binary encoding of operand for movw/movt. If the
Zonr Chang2da5aa12010-05-25 08:42:45 +0000414/// machine operand requires relocation, record the relocation and return zero.
415unsigned ARMCodeEmitter::getMovi32Value(const MachineInstr &MI,
Shih-wei Liaoe22abfa2010-05-26 00:02:28 +0000416 const MachineOperand &MO,
Zonr Chang2da5aa12010-05-25 08:42:45 +0000417 unsigned Reloc) {
Shih-wei Liaoe22abfa2010-05-26 00:02:28 +0000418 assert(((Reloc == ARM::reloc_arm_movt) || (Reloc == ARM::reloc_arm_movw))
Zonr Chang2da5aa12010-05-25 08:42:45 +0000419 && "Relocation to this function should be for movt or movw");
420
421 if (MO.isImm())
422 return static_cast<unsigned>(MO.getImm());
423 else if (MO.isGlobal())
424 emitGlobalAddress(MO.getGlobal(), Reloc, true, false);
425 else if (MO.isSymbol())
426 emitExternalSymbolAddress(MO.getSymbolName(), Reloc);
427 else if (MO.isMBB())
428 emitMachineBasicBlock(MO.getMBB(), Reloc);
429 else {
430#ifndef NDEBUG
431 errs() << MO;
432#endif
433 llvm_unreachable("Unsupported operand type for movw/movt");
434 }
435 return 0;
436}
437
Evan Cheng3be5b722008-09-02 06:52:38 +0000438/// getMachineOpValue - Return binary encoding of operand. If the machine
439/// operand requires relocation, record the relocation and return zero.
Chris Lattner8d806872010-02-02 21:48:51 +0000440unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
Jim Grosbachb770c002010-10-08 17:45:54 +0000441 const MachineOperand &MO) const {
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000442 if (MO.isReg())
Eric Christopher6ac277c2012-08-09 22:10:21 +0000443 return II->getRegisterInfo().getEncodingValue(MO.getReg());
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000444 else if (MO.isImm())
Evan Cheng3be5b722008-09-02 06:52:38 +0000445 return static_cast<unsigned>(MO.getImm());
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000446 else if (MO.isGlobal())
Evan Chengf6b24042009-09-10 01:23:53 +0000447 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true, false);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000448 else if (MO.isSymbol())
Evan Chengbb373c42008-11-08 07:22:33 +0000449 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
Evan Chengbfcee5b2008-11-12 01:02:24 +0000450 else if (MO.isCPI()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000451 const MCInstrDesc &MCID = MI.getDesc();
Evan Chengbfcee5b2008-11-12 01:02:24 +0000452 // For VFP load, the immediate offset is multiplied by 4.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000453 unsigned Reloc = ((MCID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
Evan Chengbfcee5b2008-11-12 01:02:24 +0000454 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
455 emitConstPoolAddress(MO.getIndex(), Reloc);
456 } else if (MO.isJTI())
Chris Lattnera5bb3702007-12-30 23:10:15 +0000457 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000458 else if (MO.isMBB())
Evan Cheng7095cd22008-11-07 09:06:08 +0000459 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
Jim Grosbach2aeb8b92010-11-19 00:27:09 +0000460 else
461 llvm_unreachable("Unable to encode MachineOperand!");
Evan Cheng3be5b722008-09-02 06:52:38 +0000462 return 0;
Evan Chengf7c6eff2007-08-07 01:37:15 +0000463}
464
Evan Cheng933b3922008-09-18 07:28:19 +0000465/// emitGlobalAddress - Emit the specified address to the code stream.
Evan Chengf7c6eff2007-08-07 01:37:15 +0000466///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000467void ARMCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
Chris Lattner8d806872010-02-02 21:48:51 +0000468 bool MayNeedFarStub, bool Indirect,
Jim Grosbachb770c002010-10-08 17:45:54 +0000469 intptr_t ACPV) const {
Evan Chengf6b24042009-09-10 01:23:53 +0000470 MachineRelocation MR = Indirect
471 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000472 const_cast<GlobalValue *>(GV),
473 ACPV, MayNeedFarStub)
Evan Chengf6b24042009-09-10 01:23:53 +0000474 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000475 const_cast<GlobalValue *>(GV), ACPV,
476 MayNeedFarStub);
Evan Chengf6b24042009-09-10 01:23:53 +0000477 MCE.addRelocation(MR);
Evan Chengf7c6eff2007-08-07 01:37:15 +0000478}
479
480/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
481/// be emitted to the current location in the function, and allow it to be PC
482/// relative.
Jim Grosbachb770c002010-10-08 17:45:54 +0000483void ARMCodeEmitter::
484emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
Evan Chengf7c6eff2007-08-07 01:37:15 +0000485 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
486 Reloc, ES));
487}
488
489/// emitConstPoolAddress - Arrange for the address of an constant pool
490/// to be emitted to the current location in the function, and allow it to be PC
491/// relative.
Jim Grosbachb770c002010-10-08 17:45:54 +0000492void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
Evan Cheng19d64ba2008-10-29 23:55:43 +0000493 // Tell JIT emitter we'll resolve the address.
Evan Chengf7c6eff2007-08-07 01:37:15 +0000494 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng8467e242008-11-07 22:30:53 +0000495 Reloc, CPI, 0, true));
Evan Chengf7c6eff2007-08-07 01:37:15 +0000496}
497
498/// emitJumpTableAddress - Arrange for the address of a jump table to
499/// be emitted to the current location in the function, and allow it to be PC
500/// relative.
Jim Grosbachb770c002010-10-08 17:45:54 +0000501void ARMCodeEmitter::
502emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
Evan Chengf7c6eff2007-08-07 01:37:15 +0000503 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng8467e242008-11-07 22:30:53 +0000504 Reloc, JTIndex, 0, true));
Evan Chengf7c6eff2007-08-07 01:37:15 +0000505}
506
Raul Herbster1457b2b2007-08-30 23:29:26 +0000507/// emitMachineBasicBlock - Emit the specified address basic block.
Chris Lattner8d806872010-02-02 21:48:51 +0000508void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
Jim Grosbachb770c002010-10-08 17:45:54 +0000509 unsigned Reloc,
510 intptr_t JTBase) const {
Raul Herbster1457b2b2007-08-30 23:29:26 +0000511 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Evan Cheng8467e242008-11-07 22:30:53 +0000512 Reloc, BB, JTBase));
Raul Herbster1457b2b2007-08-30 23:29:26 +0000513}
Evan Chengf7c6eff2007-08-07 01:37:15 +0000514
Chris Lattner8d806872010-02-02 21:48:51 +0000515void ARMCodeEmitter::emitWordLE(unsigned Binary) {
Chris Lattneraf29ea62009-08-23 06:49:22 +0000516 DEBUG(errs() << " 0x";
517 errs().write_hex(Binary) << "\n");
Evan Chengfd2adbf2008-11-05 23:22:34 +0000518 MCE.emitWordLE(Binary);
519}
520
Chris Lattner8d806872010-02-02 21:48:51 +0000521void ARMCodeEmitter::emitDWordLE(uint64_t Binary) {
Chris Lattneraf29ea62009-08-23 06:49:22 +0000522 DEBUG(errs() << " 0x";
523 errs().write_hex(Binary) << "\n");
Evan Chengad519bb2008-11-11 22:19:31 +0000524 MCE.emitDWordLE(Binary);
525}
526
Chris Lattner8d806872010-02-02 21:48:51 +0000527void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
Chris Lattnera6f074f2009-08-23 03:41:05 +0000528 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
Evan Cheng25a39092008-09-13 01:15:21 +0000529
Devang Patel051454a2009-10-06 02:19:11 +0000530 MCE.processDebugLoc(MI.getDebugLoc(), true);
Jeffrey Yasskin15d54b92009-07-17 18:49:39 +0000531
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000532 ++NumEmitted; // Keep track of the # of mi's emitted
Evan Cheng81889d012008-11-05 18:35:52 +0000533 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
Evan Chengfabdcce2008-11-13 23:36:57 +0000534 default: {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000535 llvm_unreachable("Unhandled instruction encoding format!");
Evan Chengfabdcce2008-11-13 23:36:57 +0000536 }
Jim Grosbach56f47172010-11-17 23:33:14 +0000537 case ARMII::MiscFrm:
538 if (MI.getOpcode() == ARM::LEApcrelJT) {
539 // Materialize jumptable address.
540 emitLEApcrelJTInstruction(MI);
541 break;
542 }
543 llvm_unreachable("Unhandled instruction encoding!");
Evan Cheng81889d012008-11-05 18:35:52 +0000544 case ARMII::Pseudo:
Evan Cheng933b3922008-09-18 07:28:19 +0000545 emitPseudoInstruction(MI);
Evan Cheng81889d012008-11-05 18:35:52 +0000546 break;
547 case ARMII::DPFrm:
548 case ARMII::DPSoRegFrm:
549 emitDataProcessingInstruction(MI);
550 break;
Evan Cheng2666f592008-11-13 07:34:59 +0000551 case ARMII::LdFrm:
552 case ARMII::StFrm:
Evan Cheng81889d012008-11-05 18:35:52 +0000553 emitLoadStoreInstruction(MI);
554 break;
Evan Cheng2666f592008-11-13 07:34:59 +0000555 case ARMII::LdMiscFrm:
556 case ARMII::StMiscFrm:
Evan Cheng81889d012008-11-05 18:35:52 +0000557 emitMiscLoadStoreInstruction(MI);
558 break;
Evan Chengaf644b52008-11-12 07:18:38 +0000559 case ARMII::LdStMulFrm:
Evan Cheng81889d012008-11-05 18:35:52 +0000560 emitLoadStoreMultipleInstruction(MI);
561 break;
Evan Cheng2686c8f2008-11-06 01:21:28 +0000562 case ARMII::MulFrm:
563 emitMulFrmInstruction(MI);
Evan Cheng81889d012008-11-05 18:35:52 +0000564 break;
Evan Cheng49d66522008-11-06 22:15:19 +0000565 case ARMII::ExtFrm:
566 emitExtendInstruction(MI);
567 break;
Evan Cheng98dc53e2008-11-07 01:41:35 +0000568 case ARMII::ArithMiscFrm:
569 emitMiscArithInstruction(MI);
570 break;
Bob Wilson96649842010-08-11 00:01:18 +0000571 case ARMII::SatFrm:
572 emitSaturateInstruction(MI);
573 break;
Evan Chengaa03cd32008-11-06 17:48:05 +0000574 case ARMII::BrFrm:
Evan Cheng81889d012008-11-05 18:35:52 +0000575 emitBranchInstruction(MI);
576 break;
Evan Chengaa03cd32008-11-06 17:48:05 +0000577 case ARMII::BrMiscFrm:
Evan Cheng81889d012008-11-05 18:35:52 +0000578 emitMiscBranchInstruction(MI);
579 break;
Evan Chengac2af2f2008-11-11 02:11:05 +0000580 // VFP instructions.
581 case ARMII::VFPUnaryFrm:
582 case ARMII::VFPBinaryFrm:
583 emitVFPArithInstruction(MI);
584 break;
Evan Cheng38c9a142008-11-11 19:40:26 +0000585 case ARMII::VFPConv1Frm:
586 case ARMII::VFPConv2Frm:
Evan Cheng97ccab82008-11-11 22:46:12 +0000587 case ARMII::VFPConv3Frm:
Evan Cheng4b6c7ef2008-11-12 06:41:41 +0000588 case ARMII::VFPConv4Frm:
589 case ARMII::VFPConv5Frm:
Evan Cheng38c9a142008-11-11 19:40:26 +0000590 emitVFPConversionInstruction(MI);
591 break;
Evan Cheng8cbbcb12008-11-11 21:48:44 +0000592 case ARMII::VFPLdStFrm:
593 emitVFPLoadStoreInstruction(MI);
594 break;
595 case ARMII::VFPLdStMulFrm:
596 emitVFPLoadStoreMultipleInstruction(MI);
597 break;
Bill Wendling5f5b9222010-10-15 23:35:12 +0000598
Bob Wilson6eae5202010-06-11 21:34:50 +0000599 // NEON instructions.
Bob Wilson0248da92010-06-26 04:07:15 +0000600 case ARMII::NGetLnFrm:
Bob Wilsonab0819e2010-06-29 17:34:07 +0000601 case ARMII::NSetLnFrm:
602 emitNEONLaneInstruction(MI);
Bob Wilson0248da92010-06-26 04:07:15 +0000603 break;
Bob Wilsonbe157b02010-06-29 20:13:29 +0000604 case ARMII::NDupFrm:
605 emitNEONDupInstruction(MI);
606 break;
Bob Wilson6eae5202010-06-11 21:34:50 +0000607 case ARMII::N1RegModImmFrm:
Bob Wilsone70c8b12010-06-25 21:17:19 +0000608 emitNEON1RegModImmInstruction(MI);
609 break;
610 case ARMII::N2RegFrm:
611 emitNEON2RegInstruction(MI);
Bob Wilson6eae5202010-06-11 21:34:50 +0000612 break;
Bob Wilson2530ca02010-06-25 22:40:46 +0000613 case ARMII::N3RegFrm:
614 emitNEON3RegInstruction(MI);
615 break;
Evan Cheng81889d012008-11-05 18:35:52 +0000616 }
Devang Patel051454a2009-10-06 02:19:11 +0000617 MCE.processDebugLoc(MI.getDebugLoc(), false);
Evan Chengf7c6eff2007-08-07 01:37:15 +0000618}
619
Chris Lattner8d806872010-02-02 21:48:51 +0000620void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
Evan Cheng8467e242008-11-07 22:30:53 +0000621 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
622 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
Evan Cheng20dbb3b2008-10-31 19:55:13 +0000623 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000624
Evan Cheng467e6e82008-10-31 19:10:44 +0000625 // Remember the CONSTPOOL_ENTRY address for later relocation.
626 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
627
628 // Emit constpool island entry. In most cases, the actual values will be
629 // resolved and relocated after code emission.
630 if (MCPE.isMachineConstantPoolEntry()) {
631 ARMConstantPoolValue *ACPV =
632 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
633
Chris Lattnera6f074f2009-08-23 03:41:05 +0000634 DEBUG(errs() << " ** ARM constant pool #" << CPI << " @ "
635 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n');
Evan Cheng467e6e82008-10-31 19:10:44 +0000636
Bob Wilson433ab092009-11-02 16:59:06 +0000637 assert(ACPV->isGlobalValue() && "unsupported constant pool value");
Bill Wendling7753d662011-10-01 08:00:54 +0000638 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
Evan Cheng467e6e82008-10-31 19:10:44 +0000639 if (GV) {
Evan Chengf6b24042009-09-10 01:23:53 +0000640 Reloc::Model RelocM = TM.getRelocationModel();
Evan Cheng43b9ca62009-08-28 23:18:09 +0000641 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
Evan Chengf6b24042009-09-10 01:23:53 +0000642 isa<Function>(GV),
643 Subtarget->GVIsIndirectSymbol(GV, RelocM),
644 (intptr_t)ACPV);
Bill Wendlingc214cb02011-10-01 08:58:29 +0000645 } else {
646 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
647 emitExternalSymbolAddress(Sym, ARM::reloc_arm_absolute);
Evan Cheng467e6e82008-10-31 19:10:44 +0000648 }
Evan Chengfd2adbf2008-11-05 23:22:34 +0000649 emitWordLE(0);
Evan Cheng467e6e82008-10-31 19:10:44 +0000650 } else {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000651 const Constant *CV = MCPE.Val.ConstVal;
Evan Cheng467e6e82008-10-31 19:10:44 +0000652
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000653 DEBUG({
654 errs() << " ** Constant pool #" << CPI << " @ "
655 << (void*)MCE.getCurrentPCValue() << " ";
656 if (const Function *F = dyn_cast<Function>(CV))
657 errs() << F->getName();
658 else
659 errs() << *CV;
660 errs() << '\n';
661 });
Evan Cheng467e6e82008-10-31 19:10:44 +0000662
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000663 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Evan Chengf6b24042009-09-10 01:23:53 +0000664 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV), false);
Evan Chengfd2adbf2008-11-05 23:22:34 +0000665 emitWordLE(0);
Evan Chengad519bb2008-11-11 22:19:31 +0000666 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Gabor Greifb171ca02010-10-22 23:16:11 +0000667 uint32_t Val = uint32_t(*CI->getValue().getRawData());
Evan Chengfd2adbf2008-11-05 23:22:34 +0000668 emitWordLE(Val);
Evan Chengad519bb2008-11-11 22:19:31 +0000669 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Chris Lattnerfdd87902009-10-05 05:54:46 +0000670 if (CFP->getType()->isFloatTy())
Evan Chengad519bb2008-11-11 22:19:31 +0000671 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Chris Lattnerfdd87902009-10-05 05:54:46 +0000672 else if (CFP->getType()->isDoubleTy())
Evan Chengad519bb2008-11-11 22:19:31 +0000673 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
674 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000675 llvm_unreachable("Unable to handle this constantpool entry!");
Evan Chengad519bb2008-11-11 22:19:31 +0000676 }
677 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000678 llvm_unreachable("Unable to handle this constantpool entry!");
Evan Cheng467e6e82008-10-31 19:10:44 +0000679 }
680 }
681}
682
Zonr Chang2da5aa12010-05-25 08:42:45 +0000683void ARMCodeEmitter::emitMOVi32immInstruction(const MachineInstr &MI) {
684 const MachineOperand &MO0 = MI.getOperand(0);
685 const MachineOperand &MO1 = MI.getOperand(1);
686
687 // Emit the 'movw' instruction.
688 unsigned Binary = 0x30 << 20; // mov: Insts{27-20} = 0b00110000
689
690 unsigned Lo16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movw) & 0xFFFF;
691
692 // Set the conditional execution predicate.
693 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
694
695 // Encode Rd.
696 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
697
698 // Encode imm16 as imm4:imm12
699 Binary |= Lo16 & 0xFFF; // Insts{11-0} = imm12
700 Binary |= ((Lo16 >> 12) & 0xF) << 16; // Insts{19-16} = imm4
701 emitWordLE(Binary);
702
703 unsigned Hi16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movt) >> 16;
704 // Emit the 'movt' instruction.
705 Binary = 0x34 << 20; // movt: Insts{27-20} = 0b00110100
706
707 // Set the conditional execution predicate.
708 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
709
710 // Encode Rd.
711 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
712
713 // Encode imm16 as imm4:imm1, same as movw above.
714 Binary |= Hi16 & 0xFFF;
715 Binary |= ((Hi16 >> 12) & 0xF) << 16;
716 emitWordLE(Binary);
717}
718
Chris Lattner8d806872010-02-02 21:48:51 +0000719void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
Evan Chengb870fd82008-11-06 02:25:39 +0000720 const MachineOperand &MO0 = MI.getOperand(0);
721 const MachineOperand &MO1 = MI.getOperand(1);
Bob Wilson1b0e6142010-03-11 00:46:22 +0000722 assert(MO1.isImm() && ARM_AM::isSOImmTwoPartVal(MO1.getImm()) &&
723 "Not a valid so_imm value!");
Evan Chengb870fd82008-11-06 02:25:39 +0000724 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
725 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
726
727 // Emit the 'mov' instruction.
728 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
729
730 // Set the conditional execution predicate.
Evan Cheng49d66522008-11-06 22:15:19 +0000731 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengb870fd82008-11-06 02:25:39 +0000732
733 // Encode Rd.
734 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
735
736 // Encode so_imm.
737 // Set bit I(25) to identify this is the immediate form of <shifter_op>
738 Binary |= 1 << ARMII::I_BitShift;
Evan Chenge3a53c42009-07-08 21:03:57 +0000739 Binary |= getMachineSoImmOpValue(V1);
Evan Chengb870fd82008-11-06 02:25:39 +0000740 emitWordLE(Binary);
741
742 // Now the 'orr' instruction.
743 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
744
745 // Set the conditional execution predicate.
Evan Cheng49d66522008-11-06 22:15:19 +0000746 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengb870fd82008-11-06 02:25:39 +0000747
748 // Encode Rd.
749 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
750
751 // Encode Rn.
752 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
753
754 // Encode so_imm.
755 // Set bit I(25) to identify this is the immediate form of <shifter_op>
756 Binary |= 1 << ARMII::I_BitShift;
Evan Chenge3a53c42009-07-08 21:03:57 +0000757 Binary |= getMachineSoImmOpValue(V2);
Evan Chengb870fd82008-11-06 02:25:39 +0000758 emitWordLE(Binary);
759}
760
Chris Lattner8d806872010-02-02 21:48:51 +0000761void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
Evan Cheng7095cd22008-11-07 09:06:08 +0000762 // It's basically add r, pc, (LJTI - $+8)
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000763
Evan Cheng6cc775f2011-06-28 19:10:37 +0000764 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng7095cd22008-11-07 09:06:08 +0000765
766 // Emit the 'add' instruction.
Jim Grosbach4ded8f22010-11-17 21:57:51 +0000767 unsigned Binary = 0x4 << 21; // add: Insts{24-21} = 0b0100
Evan Cheng7095cd22008-11-07 09:06:08 +0000768
769 // Set the conditional execution predicate
770 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
771
772 // Encode S bit if MI modifies CPSR.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000773 Binary |= getAddrModeSBit(MI, MCID);
Evan Cheng7095cd22008-11-07 09:06:08 +0000774
775 // Encode Rd.
776 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
777
778 // Encode Rn which is PC.
Eric Christopher6ac277c2012-08-09 22:10:21 +0000779 Binary |= II->getRegisterInfo().getEncodingValue(ARM::PC) << ARMII::RegRnShift;
Evan Cheng7095cd22008-11-07 09:06:08 +0000780
781 // Encode the displacement.
Evan Cheng7095cd22008-11-07 09:06:08 +0000782 Binary |= 1 << ARMII::I_BitShift;
783 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
784
785 emitWordLE(Binary);
786}
787
Chris Lattner8d806872010-02-02 21:48:51 +0000788void ARMCodeEmitter::emitPseudoMoveInstruction(const MachineInstr &MI) {
Evan Cheng30f6f8f2008-11-14 20:09:11 +0000789 unsigned Opcode = MI.getDesc().Opcode;
790
791 // Part of binary is determined by TableGn.
792 unsigned Binary = getBinaryCodeForInstr(MI);
793
794 // Set the conditional execution predicate
795 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
796
797 // Encode S bit if MI modifies CPSR.
798 if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag)
799 Binary |= 1 << ARMII::S_BitShift;
800
801 // Encode register def if there is one.
802 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
803
804 // Encode the shift operation.
805 switch (Opcode) {
806 default: break;
Jim Grosbach062749c2010-10-14 20:43:44 +0000807 case ARM::RRX:
Evan Cheng30f6f8f2008-11-14 20:09:11 +0000808 // rrx
809 Binary |= 0x6 << 4;
810 break;
811 case ARM::MOVsrl_flag:
812 // lsr #1
813 Binary |= (0x2 << 4) | (1 << 7);
814 break;
815 case ARM::MOVsra_flag:
816 // asr #1
817 Binary |= (0x4 << 4) | (1 << 7);
818 break;
819 }
820
821 // Encode register Rm.
822 Binary |= getMachineOpValue(MI, 1);
823
824 emitWordLE(Binary);
825}
826
Chris Lattner8d806872010-02-02 21:48:51 +0000827void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
Chris Lattneraf29ea62009-08-23 06:49:22 +0000828 DEBUG(errs() << " ** LPC" << LabelID << " @ "
829 << (void*)MCE.getCurrentPCValue() << '\n');
Evan Chengfd2adbf2008-11-05 23:22:34 +0000830 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
831}
832
Chris Lattner8d806872010-02-02 21:48:51 +0000833void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
Evan Cheng467e6e82008-10-31 19:10:44 +0000834 unsigned Opcode = MI.getDesc().Opcode;
835 switch (Opcode) {
836 default:
Evan Cheng83e0d482009-09-28 09:14:39 +0000837 llvm_unreachable("ARMCodeEmitter::emitPseudoInstruction");
Jim Grosbach027bd472010-11-30 00:24:05 +0000838 case ARM::BX_CALL:
Jakob Stoklund Olesen6a2e99a2012-04-06 00:04:58 +0000839 case ARM::BMOVPCRX_CALL: {
Xerxes Ranbyff66cd42010-07-22 17:28:34 +0000840 // First emit mov lr, pc
841 unsigned Binary = 0x01a0e00f;
842 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
843 emitWordLE(Binary);
844
845 // and then emit the branch.
846 emitMiscBranchInstruction(MI);
847 break;
848 }
Chris Lattnerb06015a2010-02-09 19:54:29 +0000849 case TargetOpcode::INLINEASM: {
Evan Cheng59213d62008-11-19 23:21:33 +0000850 // We allow inline assembler nodes with empty bodies - they can
851 // implicitly define registers, which is ok for JIT.
852 if (MI.getOperand(0).getSymbolName()[0]) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000853 report_fatal_error("JIT does not support inline asm!");
Evan Cheng59213d62008-11-19 23:21:33 +0000854 }
Evan Chengfabdcce2008-11-13 23:36:57 +0000855 break;
856 }
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000857 case TargetOpcode::CFI_INSTRUCTION:
858 break;
Chris Lattneree2fbbc2010-03-14 02:33:54 +0000859 case TargetOpcode::EH_LABEL:
860 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
861 break;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000862 case TargetOpcode::IMPLICIT_DEF:
863 case TargetOpcode::KILL:
Evan Chengfabdcce2008-11-13 23:36:57 +0000864 // Do nothing.
865 break;
Evan Cheng467e6e82008-10-31 19:10:44 +0000866 case ARM::CONSTPOOL_ENTRY:
867 emitConstPoolInstruction(MI);
868 break;
869 case ARM::PICADD: {
Evan Cheng6dd08b62008-11-04 00:50:32 +0000870 // Remember of the address of the PC label for relocation later.
Evan Chengfd2adbf2008-11-05 23:22:34 +0000871 addPCLabel(MI.getOperand(2).getImm());
Evan Cheng467e6e82008-10-31 19:10:44 +0000872 // PICADD is just an add instruction that implicitly read pc.
Evan Cheng8467e242008-11-07 22:30:53 +0000873 emitDataProcessingInstruction(MI, 0, ARM::PC);
Evan Chengfd2adbf2008-11-05 23:22:34 +0000874 break;
875 }
876 case ARM::PICLDR:
877 case ARM::PICLDRB:
878 case ARM::PICSTR:
879 case ARM::PICSTRB: {
880 // Remember of the address of the PC label for relocation later.
881 addPCLabel(MI.getOperand(2).getImm());
882 // These are just load / store instructions that implicitly read pc.
Evan Cheng7095cd22008-11-07 09:06:08 +0000883 emitLoadStoreInstruction(MI, 0, ARM::PC);
Evan Chengfd2adbf2008-11-05 23:22:34 +0000884 break;
885 }
886 case ARM::PICLDRH:
887 case ARM::PICLDRSH:
888 case ARM::PICLDRSB:
889 case ARM::PICSTRH: {
890 // Remember of the address of the PC label for relocation later.
891 addPCLabel(MI.getOperand(2).getImm());
892 // These are just load / store instructions that implicitly read pc.
893 emitMiscLoadStoreInstruction(MI, ARM::PC);
Evan Cheng467e6e82008-10-31 19:10:44 +0000894 break;
895 }
Zonr Chang2da5aa12010-05-25 08:42:45 +0000896
897 case ARM::MOVi32imm:
Evan Chengf478cf92010-11-12 23:03:38 +0000898 // Two instructions to materialize a constant.
899 if (Subtarget->hasV6T2Ops())
900 emitMOVi32immInstruction(MI);
901 else
902 emitMOVi2piecesInstruction(MI);
Zonr Chang2da5aa12010-05-25 08:42:45 +0000903 break;
904
Evan Cheng7095cd22008-11-07 09:06:08 +0000905 case ARM::LEApcrelJT:
906 // Materialize jumptable address.
907 emitLEApcrelJTInstruction(MI);
908 break;
Jim Grosbach062749c2010-10-14 20:43:44 +0000909 case ARM::RRX:
Evan Cheng30f6f8f2008-11-14 20:09:11 +0000910 case ARM::MOVsrl_flag:
911 case ARM::MOVsra_flag:
912 emitPseudoMoveInstruction(MI);
913 break;
Evan Cheng467e6e82008-10-31 19:10:44 +0000914 }
915}
916
Bob Wilsona6fe21a2010-03-17 21:16:45 +0000917unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
Evan Cheng6cc775f2011-06-28 19:10:37 +0000918 const MCInstrDesc &MCID,
Evan Cheng467e6e82008-10-31 19:10:44 +0000919 const MachineOperand &MO,
Evan Cheng33fa89c6f2008-09-12 22:01:15 +0000920 unsigned OpIdx) {
Evan Cheng467e6e82008-10-31 19:10:44 +0000921 unsigned Binary = getMachineOpValue(MI, MO);
Evan Cheng33fa89c6f2008-09-12 22:01:15 +0000922
923 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
924 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
925 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
926
927 // Encode the shift opcode.
928 unsigned SBits = 0;
929 unsigned Rs = MO1.getReg();
930 if (Rs) {
931 // Set shift operand (bit[7:4]).
932 // LSL - 0001
933 // LSR - 0011
934 // ASR - 0101
935 // ROR - 0111
936 // RRX - 0110 and bit[11:8] clear.
937 switch (SOpc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000938 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng33fa89c6f2008-09-12 22:01:15 +0000939 case ARM_AM::lsl: SBits = 0x1; break;
940 case ARM_AM::lsr: SBits = 0x3; break;
941 case ARM_AM::asr: SBits = 0x5; break;
942 case ARM_AM::ror: SBits = 0x7; break;
943 case ARM_AM::rrx: SBits = 0x6; break;
944 }
945 } else {
946 // Set shift operand (bit[6:4]).
947 // LSL - 000
948 // LSR - 010
949 // ASR - 100
950 // ROR - 110
951 switch (SOpc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000952 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng33fa89c6f2008-09-12 22:01:15 +0000953 case ARM_AM::lsl: SBits = 0x0; break;
954 case ARM_AM::lsr: SBits = 0x2; break;
955 case ARM_AM::asr: SBits = 0x4; break;
956 case ARM_AM::ror: SBits = 0x6; break;
957 }
958 }
959 Binary |= SBits << 4;
960 if (SOpc == ARM_AM::rrx)
961 return Binary;
962
963 // Encode the shift operation Rs or shift_imm (except rrx).
964 if (Rs) {
965 // Encode Rs bit[11:8].
966 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
Eric Christopher6ac277c2012-08-09 22:10:21 +0000967 return Binary | (II->getRegisterInfo().getEncodingValue(Rs) << ARMII::RegRsShift);
Evan Cheng33fa89c6f2008-09-12 22:01:15 +0000968 }
969
970 // Encode shift_imm bit[11:7].
971 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
972}
973
Chris Lattner8d806872010-02-02 21:48:51 +0000974unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000975 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
976 assert(SoImmVal != -1 && "Not a valid so_imm value!");
977
Evan Cheng467e6e82008-10-31 19:10:44 +0000978 // Encode rotate_imm.
Evan Chenge3a53c42009-07-08 21:03:57 +0000979 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
Evan Cheng49d66522008-11-06 22:15:19 +0000980 << ARMII::SoRotImmShift;
981
Evan Cheng467e6e82008-10-31 19:10:44 +0000982 // Encode immed_8.
Evan Chenge3a53c42009-07-08 21:03:57 +0000983 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
Evan Cheng467e6e82008-10-31 19:10:44 +0000984 return Binary;
985}
986
Chris Lattner8d806872010-02-02 21:48:51 +0000987unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
Evan Cheng6cc775f2011-06-28 19:10:37 +0000988 const MCInstrDesc &MCID) const {
Jim Grosbach05dec8b12011-09-02 18:46:15 +0000989 for (unsigned i = MI.getNumOperands(), e = MCID.getNumOperands(); i >= e;--i){
Evan Chengd1424c42008-09-12 22:45:55 +0000990 const MachineOperand &MO = MI.getOperand(i-1);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000991 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
Evan Chengd1424c42008-09-12 22:45:55 +0000992 return 1 << ARMII::S_BitShift;
993 }
994 return 0;
995}
996
Bob Wilsona6fe21a2010-03-17 21:16:45 +0000997void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
Evan Cheng8467e242008-11-07 22:30:53 +0000998 unsigned ImplicitRd,
Evan Chengfd2adbf2008-11-05 23:22:34 +0000999 unsigned ImplicitRn) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001000 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng81889d012008-11-05 18:35:52 +00001001
1002 // Part of binary is determined by TableGn.
1003 unsigned Binary = getBinaryCodeForInstr(MI);
1004
Jim Grosbachc084e842008-10-07 19:05:35 +00001005 // Set the conditional execution predicate
Evan Cheng49d66522008-11-06 22:15:19 +00001006 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng33fa89c6f2008-09-12 22:01:15 +00001007
Evan Chengd1424c42008-09-12 22:45:55 +00001008 // Encode S bit if MI modifies CPSR.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001009 Binary |= getAddrModeSBit(MI, MCID);
Evan Chengd1424c42008-09-12 22:45:55 +00001010
Evan Cheng33fa89c6f2008-09-12 22:01:15 +00001011 // Encode register def if there is one.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001012 unsigned NumDefs = MCID.getNumDefs();
Evan Chengc5c74f32008-09-12 23:15:39 +00001013 unsigned OpIdx = 0;
Evan Cheng8467e242008-11-07 22:30:53 +00001014 if (NumDefs)
1015 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1016 else if (ImplicitRd)
1017 // Special handling for implicit use (e.g. PC).
Eric Christopher6ac277c2012-08-09 22:10:21 +00001018 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRd) << ARMII::RegRdShift);
Evan Cheng3be5b722008-09-02 06:52:38 +00001019
Evan Cheng6cc775f2011-06-28 19:10:37 +00001020 if (MCID.Opcode == ARM::MOVi16) {
Zonr Chang2da5aa12010-05-25 08:42:45 +00001021 // Get immediate from MI.
1022 unsigned Lo16 = getMovi32Value(MI, MI.getOperand(OpIdx),
1023 ARM::reloc_arm_movw);
1024 // Encode imm which is the same as in emitMOVi32immInstruction().
1025 Binary |= Lo16 & 0xFFF;
1026 Binary |= ((Lo16 >> 12) & 0xF) << 16;
1027 emitWordLE(Binary);
1028 return;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001029 } else if(MCID.Opcode == ARM::MOVTi16) {
Zonr Chang2da5aa12010-05-25 08:42:45 +00001030 unsigned Hi16 = (getMovi32Value(MI, MI.getOperand(OpIdx),
1031 ARM::reloc_arm_movt) >> 16);
1032 Binary |= Hi16 & 0xFFF;
1033 Binary |= ((Hi16 >> 12) & 0xF) << 16;
1034 emitWordLE(Binary);
1035 return;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001036 } else if ((MCID.Opcode == ARM::BFC) || (MCID.Opcode == ARM::BFI)) {
Shih-wei Liaob6e0bc92010-05-26 00:25:05 +00001037 uint32_t v = ~MI.getOperand(2).getImm();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001038 int32_t lsb = countTrailingZeros(v);
1039 int32_t msb = (32 - countLeadingZeros(v)) - 1;
Shih-wei Liao0568ca02010-05-26 03:21:39 +00001040 // Instr{20-16} = msb, Instr{11-7} = lsb
Shih-wei Liaob6e0bc92010-05-26 00:25:05 +00001041 Binary |= (msb & 0x1F) << 16;
1042 Binary |= (lsb & 0x1F) << 7;
1043 emitWordLE(Binary);
1044 return;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001045 } else if ((MCID.Opcode == ARM::UBFX) || (MCID.Opcode == ARM::SBFX)) {
Shih-wei Liao0568ca02010-05-26 03:21:39 +00001046 // Encode Rn in Instr{0-3}
1047 Binary |= getMachineOpValue(MI, OpIdx++);
1048
1049 uint32_t lsb = MI.getOperand(OpIdx++).getImm();
1050 uint32_t widthm1 = MI.getOperand(OpIdx++).getImm() - 1;
1051
1052 // Instr{20-16} = widthm1, Instr{11-7} = lsb
1053 Binary |= (widthm1 & 0x1F) << 16;
1054 Binary |= (lsb & 0x1F) << 7;
1055 emitWordLE(Binary);
1056 return;
Zonr Chang2da5aa12010-05-25 08:42:45 +00001057 }
1058
Evan Cheng47b546d2008-11-06 08:47:38 +00001059 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001060 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Evan Cheng47b546d2008-11-06 08:47:38 +00001061 ++OpIdx;
1062
Jim Grosbach3dc0a3b2008-10-01 18:16:49 +00001063 // Encode first non-shifter register operand if there is one.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001064 bool isUnary = MCID.TSFlags & ARMII::UnaryDP;
Evan Cheng81889d012008-11-05 18:35:52 +00001065 if (!isUnary) {
Evan Chengfd2adbf2008-11-05 23:22:34 +00001066 if (ImplicitRn)
1067 // Special handling for implicit use (e.g. PC).
Eric Christopher6ac277c2012-08-09 22:10:21 +00001068 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRn) << ARMII::RegRnShift);
Evan Cheng467e6e82008-10-31 19:10:44 +00001069 else {
1070 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
1071 ++OpIdx;
1072 }
Evan Cheng3be5b722008-09-02 06:52:38 +00001073 }
1074
Evan Cheng33fa89c6f2008-09-12 22:01:15 +00001075 // Encode shifter operand.
Evan Cheng33fa89c6f2008-09-12 22:01:15 +00001076 const MachineOperand &MO = MI.getOperand(OpIdx);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001077 if ((MCID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
Evan Cheng467e6e82008-10-31 19:10:44 +00001078 // Encode SoReg.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001079 emitWordLE(Binary | getMachineSoRegOpValue(MI, MCID, MO, OpIdx));
Evan Cheng81889d012008-11-05 18:35:52 +00001080 return;
1081 }
Evan Cheng467e6e82008-10-31 19:10:44 +00001082
Evan Cheng81889d012008-11-05 18:35:52 +00001083 if (MO.isReg()) {
Evan Cheng33fa89c6f2008-09-12 22:01:15 +00001084 // Encode register Rm.
Eric Christopher6ac277c2012-08-09 22:10:21 +00001085 emitWordLE(Binary | II->getRegisterInfo().getEncodingValue(MO.getReg()));
Evan Cheng81889d012008-11-05 18:35:52 +00001086 return;
1087 }
Evan Cheng3be5b722008-09-02 06:52:38 +00001088
Evan Cheng33fa89c6f2008-09-12 22:01:15 +00001089 // Encode so_imm.
Evan Chenge3a53c42009-07-08 21:03:57 +00001090 Binary |= getMachineSoImmOpValue((unsigned)MO.getImm());
Evan Cheng81889d012008-11-05 18:35:52 +00001091
Evan Chengfd2adbf2008-11-05 23:22:34 +00001092 emitWordLE(Binary);
Evan Cheng3be5b722008-09-02 06:52:38 +00001093}
1094
Bob Wilsona6fe21a2010-03-17 21:16:45 +00001095void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
Evan Cheng7095cd22008-11-07 09:06:08 +00001096 unsigned ImplicitRd,
Evan Chengfd2adbf2008-11-05 23:22:34 +00001097 unsigned ImplicitRn) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001098 const MCInstrDesc &MCID = MI.getDesc();
1099 unsigned Form = MCID.TSFlags & ARMII::FormMask;
1100 bool IsPrePost = (MCID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng077c8f82008-11-08 01:44:13 +00001101
Evan Cheng81889d012008-11-05 18:35:52 +00001102 // Part of binary is determined by TableGn.
1103 unsigned Binary = getBinaryCodeForInstr(MI);
1104
Jim Grosbach338de3e2010-10-27 23:12:14 +00001105 // If this is an LDRi12, STRi12 or LDRcp, nothing more needs be done.
1106 if (MI.getOpcode() == ARM::LDRi12 || MI.getOpcode() == ARM::LDRcp ||
1107 MI.getOpcode() == ARM::STRi12) {
Jim Grosbachba1c6cd2010-10-27 17:52:51 +00001108 emitWordLE(Binary);
1109 return;
1110 }
1111
Jim Grosbachc084e842008-10-07 19:05:35 +00001112 // Set the conditional execution predicate
Evan Cheng49d66522008-11-06 22:15:19 +00001113 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng933b3922008-09-18 07:28:19 +00001114
Evan Cheng7095cd22008-11-07 09:06:08 +00001115 unsigned OpIdx = 0;
Evan Cheng2666f592008-11-13 07:34:59 +00001116
1117 // Operand 0 of a pre- and post-indexed store is the address base
1118 // writeback. Skip it.
1119 bool Skipped = false;
1120 if (IsPrePost && Form == ARMII::StFrm) {
1121 ++OpIdx;
1122 Skipped = true;
1123 }
1124
1125 // Set first operand
Evan Cheng7095cd22008-11-07 09:06:08 +00001126 if (ImplicitRd)
1127 // Special handling for implicit use (e.g. PC).
Eric Christopher6ac277c2012-08-09 22:10:21 +00001128 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRd) << ARMII::RegRdShift);
Evan Cheng7095cd22008-11-07 09:06:08 +00001129 else
1130 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng3be5b722008-09-02 06:52:38 +00001131
1132 // Set second operand
Evan Chengfd2adbf2008-11-05 23:22:34 +00001133 if (ImplicitRn)
1134 // Special handling for implicit use (e.g. PC).
Eric Christopher6ac277c2012-08-09 22:10:21 +00001135 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRn) << ARMII::RegRnShift);
Evan Cheng7095cd22008-11-07 09:06:08 +00001136 else
1137 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng3be5b722008-09-02 06:52:38 +00001138
Evan Cheng077c8f82008-11-08 01:44:13 +00001139 // If this is a two-address operand, skip it. e.g. LDR_PRE.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001140 if (!Skipped && MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Evan Cheng077c8f82008-11-08 01:44:13 +00001141 ++OpIdx;
1142
Evan Chengfd2adbf2008-11-05 23:22:34 +00001143 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Cheng47b546d2008-11-06 08:47:38 +00001144 unsigned AM2Opc = (ImplicitRn == ARM::PC)
Evan Chengfd2adbf2008-11-05 23:22:34 +00001145 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng3be5b722008-09-02 06:52:38 +00001146
Evan Cheng380482a2008-09-13 01:44:01 +00001147 // Set bit U(23) according to sign of immed value (positive or negative).
Evan Chengfd2adbf2008-11-05 23:22:34 +00001148 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
Evan Cheng380482a2008-09-13 01:44:01 +00001149 ARMII::U_BitShift);
Evan Cheng3be5b722008-09-02 06:52:38 +00001150 if (!MO2.getReg()) { // is immediate
Evan Chengfd2adbf2008-11-05 23:22:34 +00001151 if (ARM_AM::getAM2Offset(AM2Opc))
Evan Cheng3be5b722008-09-02 06:52:38 +00001152 // Set the value of offset_12 field
Evan Chengfd2adbf2008-11-05 23:22:34 +00001153 Binary |= ARM_AM::getAM2Offset(AM2Opc);
1154 emitWordLE(Binary);
Evan Cheng81889d012008-11-05 18:35:52 +00001155 return;
Evan Cheng3be5b722008-09-02 06:52:38 +00001156 }
1157
Bill Wendling05819052010-10-20 22:44:54 +00001158 // Set bit I(25), because this is not in immediate encoding.
Evan Cheng3be5b722008-09-02 06:52:38 +00001159 Binary |= 1 << ARMII::I_BitShift;
1160 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
1161 // Set bit[3:0] to the corresponding Rm register
Eric Christopher6ac277c2012-08-09 22:10:21 +00001162 Binary |= II->getRegisterInfo().getEncodingValue(MO2.getReg());
Evan Cheng3be5b722008-09-02 06:52:38 +00001163
Evan Cheng2836d912008-11-12 07:34:37 +00001164 // If this instr is in scaled register offset/index instruction, set
Evan Cheng3be5b722008-09-02 06:52:38 +00001165 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
Evan Chengfd2adbf2008-11-05 23:22:34 +00001166 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
Evan Cheng2836d912008-11-12 07:34:37 +00001167 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
1168 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
Evan Cheng3be5b722008-09-02 06:52:38 +00001169 }
1170
Evan Chengfd2adbf2008-11-05 23:22:34 +00001171 emitWordLE(Binary);
Evan Cheng3be5b722008-09-02 06:52:38 +00001172}
1173
Chris Lattner8d806872010-02-02 21:48:51 +00001174void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
Bob Wilsona6fe21a2010-03-17 21:16:45 +00001175 unsigned ImplicitRn) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001176 const MCInstrDesc &MCID = MI.getDesc();
1177 unsigned Form = MCID.TSFlags & ARMII::FormMask;
1178 bool IsPrePost = (MCID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng077c8f82008-11-08 01:44:13 +00001179
Evan Cheng81889d012008-11-05 18:35:52 +00001180 // Part of binary is determined by TableGn.
1181 unsigned Binary = getBinaryCodeForInstr(MI);
1182
Jim Grosbachc084e842008-10-07 19:05:35 +00001183 // Set the conditional execution predicate
Evan Cheng49d66522008-11-06 22:15:19 +00001184 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng933b3922008-09-18 07:28:19 +00001185
Evan Cheng2666f592008-11-13 07:34:59 +00001186 unsigned OpIdx = 0;
1187
1188 // Operand 0 of a pre- and post-indexed store is the address base
1189 // writeback. Skip it.
1190 bool Skipped = false;
1191 if (IsPrePost && Form == ARMII::StMiscFrm) {
1192 ++OpIdx;
1193 Skipped = true;
1194 }
1195
Evan Cheng3be5b722008-09-02 06:52:38 +00001196 // Set first operand
Evan Cheng2666f592008-11-13 07:34:59 +00001197 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng3be5b722008-09-02 06:52:38 +00001198
Evan Cheng1283c6a2009-06-15 08:28:29 +00001199 // Skip LDRD and STRD's second operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001200 if (MCID.Opcode == ARM::LDRD || MCID.Opcode == ARM::STRD)
Evan Cheng1283c6a2009-06-15 08:28:29 +00001201 ++OpIdx;
1202
Evan Cheng3be5b722008-09-02 06:52:38 +00001203 // Set second operand
Evan Chengfd2adbf2008-11-05 23:22:34 +00001204 if (ImplicitRn)
1205 // Special handling for implicit use (e.g. PC).
Eric Christopher6ac277c2012-08-09 22:10:21 +00001206 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRn) << ARMII::RegRnShift);
Evan Cheng7095cd22008-11-07 09:06:08 +00001207 else
1208 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng3be5b722008-09-02 06:52:38 +00001209
Evan Cheng077c8f82008-11-08 01:44:13 +00001210 // If this is a two-address operand, skip it. e.g. LDRH_POST.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001211 if (!Skipped && MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Evan Cheng077c8f82008-11-08 01:44:13 +00001212 ++OpIdx;
1213
Evan Chengfd2adbf2008-11-05 23:22:34 +00001214 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Cheng47b546d2008-11-06 08:47:38 +00001215 unsigned AM3Opc = (ImplicitRn == ARM::PC)
Evan Chengfd2adbf2008-11-05 23:22:34 +00001216 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng3be5b722008-09-02 06:52:38 +00001217
Evan Cheng380482a2008-09-13 01:44:01 +00001218 // Set bit U(23) according to sign of immed value (positive or negative)
Evan Chengfd2adbf2008-11-05 23:22:34 +00001219 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
Evan Cheng3be5b722008-09-02 06:52:38 +00001220 ARMII::U_BitShift);
1221
1222 // If this instr is in register offset/index encoding, set bit[3:0]
1223 // to the corresponding Rm register.
1224 if (MO2.getReg()) {
Eric Christopher6ac277c2012-08-09 22:10:21 +00001225 Binary |= II->getRegisterInfo().getEncodingValue(MO2.getReg());
Evan Chengfd2adbf2008-11-05 23:22:34 +00001226 emitWordLE(Binary);
Evan Cheng81889d012008-11-05 18:35:52 +00001227 return;
Evan Cheng3be5b722008-09-02 06:52:38 +00001228 }
1229
Evan Cheng47b546d2008-11-06 08:47:38 +00001230 // This instr is in immediate offset/index encoding, set bit 22 to 1.
Evan Cheng49d66522008-11-06 22:15:19 +00001231 Binary |= 1 << ARMII::AM3_I_BitShift;
Evan Chengfd2adbf2008-11-05 23:22:34 +00001232 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
Evan Cheng3be5b722008-09-02 06:52:38 +00001233 // Set operands
Evan Cheng2836d912008-11-12 07:34:37 +00001234 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
1235 Binary |= (ImmOffs & 0xF); // immedL
Evan Cheng3be5b722008-09-02 06:52:38 +00001236 }
1237
Evan Chengfd2adbf2008-11-05 23:22:34 +00001238 emitWordLE(Binary);
Evan Cheng3be5b722008-09-02 06:52:38 +00001239}
1240
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001241static unsigned getAddrModeUPBits(unsigned Mode) {
1242 unsigned Binary = 0;
Evan Cheng3be5b722008-09-02 06:52:38 +00001243
1244 // Set addressing mode by modifying bits U(23) and P(24)
1245 // IA - Increment after - bit U = 1 and bit P = 0
1246 // IB - Increment before - bit U = 1 and bit P = 1
1247 // DA - Decrement after - bit U = 0 and bit P = 0
1248 // DB - Decrement before - bit U = 0 and bit P = 1
Evan Cheng3be5b722008-09-02 06:52:38 +00001249 switch (Mode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001250 default: llvm_unreachable("Unknown addressing sub-mode!");
Evan Cheng71140342009-09-09 23:55:03 +00001251 case ARM_AM::da: break;
Evan Cheng49d66522008-11-06 22:15:19 +00001252 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
1253 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
1254 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
Evan Cheng3be5b722008-09-02 06:52:38 +00001255 }
1256
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001257 return Binary;
1258}
1259
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001260void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001261 const MCInstrDesc &MCID = MI.getDesc();
1262 bool IsUpdating = (MCID.TSFlags & ARMII::IndexModeMask) != 0;
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001263
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001264 // Part of binary is determined by TableGn.
1265 unsigned Binary = getBinaryCodeForInstr(MI);
1266
1267 // Set the conditional execution predicate
1268 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1269
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001270 // Skip operand 0 of an instruction with base register update.
1271 unsigned OpIdx = 0;
1272 if (IsUpdating)
1273 ++OpIdx;
1274
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001275 // Set base address operand
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001276 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001277
1278 // Set addressing mode by modifying bits U(23) and P(24)
Bill Wendlingb100f912010-11-17 05:31:09 +00001279 ARM_AM::AMSubMode Mode = ARM_AM::getLoadStoreMultipleSubMode(MI.getOpcode());
1280 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(Mode));
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001281
Evan Cheng3be5b722008-09-02 06:52:38 +00001282 // Set bit W(21)
Bob Wilsond6243b42010-03-16 17:46:45 +00001283 if (IsUpdating)
Evan Cheng49d66522008-11-06 22:15:19 +00001284 Binary |= 0x1 << ARMII::W_BitShift;
Evan Cheng3be5b722008-09-02 06:52:38 +00001285
1286 // Set registers
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001287 for (unsigned i = OpIdx+2, e = MI.getNumOperands(); i != e; ++i) {
Evan Cheng3be5b722008-09-02 06:52:38 +00001288 const MachineOperand &MO = MI.getOperand(i);
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001289 if (!MO.isReg() || MO.isImplicit())
1290 break;
Eric Christopher6ac277c2012-08-09 22:10:21 +00001291 unsigned RegNum = II->getRegisterInfo().getEncodingValue(MO.getReg());
Evan Cheng3be5b722008-09-02 06:52:38 +00001292 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
1293 RegNum < 16);
1294 Binary |= 0x1 << RegNum;
1295 }
1296
Evan Chengfd2adbf2008-11-05 23:22:34 +00001297 emitWordLE(Binary);
Evan Cheng3be5b722008-09-02 06:52:38 +00001298}
1299
Chris Lattner8d806872010-02-02 21:48:51 +00001300void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001301 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng81889d012008-11-05 18:35:52 +00001302
1303 // Part of binary is determined by TableGn.
1304 unsigned Binary = getBinaryCodeForInstr(MI);
1305
Jim Grosbach4d0549e2008-11-03 18:38:31 +00001306 // Set the conditional execution predicate
Evan Cheng49d66522008-11-06 22:15:19 +00001307 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Jim Grosbach4d0549e2008-11-03 18:38:31 +00001308
1309 // Encode S bit if MI modifies CPSR.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001310 Binary |= getAddrModeSBit(MI, MCID);
Jim Grosbach4d0549e2008-11-03 18:38:31 +00001311
1312 // 32x32->64bit operations have two destination registers. The number
1313 // of register definitions will tell us if that's what we're dealing with.
Evan Cheng49d66522008-11-06 22:15:19 +00001314 unsigned OpIdx = 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001315 if (MCID.getNumDefs() == 2)
Jim Grosbach4d0549e2008-11-03 18:38:31 +00001316 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
1317
1318 // Encode Rd
1319 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
1320
1321 // Encode Rm
1322 Binary |= getMachineOpValue(MI, OpIdx++);
1323
1324 // Encode Rs
1325 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
1326
Evan Cheng2686c8f2008-11-06 01:21:28 +00001327 // Many multiple instructions (e.g. MLA) have three src operands. Encode
1328 // it as Rn (for multiply, that's in the same offset as RdLo.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001329 if (MCID.getNumOperands() > OpIdx &&
1330 !MCID.OpInfo[OpIdx].isPredicate() &&
1331 !MCID.OpInfo[OpIdx].isOptionalDef())
Evan Cheng49d66522008-11-06 22:15:19 +00001332 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
1333
1334 emitWordLE(Binary);
1335}
1336
Chris Lattner8d806872010-02-02 21:48:51 +00001337void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001338 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng49d66522008-11-06 22:15:19 +00001339
1340 // Part of binary is determined by TableGn.
1341 unsigned Binary = getBinaryCodeForInstr(MI);
1342
1343 // Set the conditional execution predicate
1344 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1345
1346 unsigned OpIdx = 0;
1347
1348 // Encode Rd
1349 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1350
1351 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
1352 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1353 if (MO2.isReg()) {
1354 // Two register operand form.
1355 // Encode Rn.
1356 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
1357
1358 // Encode Rm.
1359 Binary |= getMachineOpValue(MI, MO2);
1360 ++OpIdx;
1361 } else {
1362 Binary |= getMachineOpValue(MI, MO1);
1363 }
1364
1365 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
1366 if (MI.getOperand(OpIdx).isImm() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001367 !MCID.OpInfo[OpIdx].isPredicate() &&
1368 !MCID.OpInfo[OpIdx].isOptionalDef())
Evan Cheng49d66522008-11-06 22:15:19 +00001369 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
Evan Cheng2686c8f2008-11-06 01:21:28 +00001370
Evan Chengfd2adbf2008-11-05 23:22:34 +00001371 emitWordLE(Binary);
Jim Grosbach4d0549e2008-11-03 18:38:31 +00001372}
1373
Chris Lattner8d806872010-02-02 21:48:51 +00001374void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001375 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng98dc53e2008-11-07 01:41:35 +00001376
1377 // Part of binary is determined by TableGn.
1378 unsigned Binary = getBinaryCodeForInstr(MI);
1379
1380 // Set the conditional execution predicate
1381 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1382
Eric Christopher1e3db022011-05-07 04:37:27 +00001383 // PKH instructions are finished at this point
Evan Cheng6cc775f2011-06-28 19:10:37 +00001384 if (MCID.Opcode == ARM::PKHBT || MCID.Opcode == ARM::PKHTB) {
Eric Christopher1e3db022011-05-07 04:37:27 +00001385 emitWordLE(Binary);
1386 return;
1387 }
1388
Evan Cheng98dc53e2008-11-07 01:41:35 +00001389 unsigned OpIdx = 0;
1390
1391 // Encode Rd
1392 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1393
1394 const MachineOperand &MO = MI.getOperand(OpIdx++);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001395 if (OpIdx == MCID.getNumOperands() ||
1396 MCID.OpInfo[OpIdx].isPredicate() ||
1397 MCID.OpInfo[OpIdx].isOptionalDef()) {
Evan Cheng98dc53e2008-11-07 01:41:35 +00001398 // Encode Rm and it's done.
1399 Binary |= getMachineOpValue(MI, MO);
1400 emitWordLE(Binary);
1401 return;
1402 }
1403
1404 // Encode Rn.
1405 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1406
1407 // Encode Rm.
1408 Binary |= getMachineOpValue(MI, OpIdx++);
1409
1410 // Encode shift_imm.
1411 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001412 if (MCID.Opcode == ARM::PKHTB) {
Bob Wilson942b10f2010-08-17 17:23:19 +00001413 assert(ShiftAmt != 0 && "PKHTB shift_imm is 0!");
1414 if (ShiftAmt == 32)
1415 ShiftAmt = 0;
1416 }
Evan Cheng98dc53e2008-11-07 01:41:35 +00001417 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1418 Binary |= ShiftAmt << ARMII::ShiftShift;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001419
Evan Cheng98dc53e2008-11-07 01:41:35 +00001420 emitWordLE(Binary);
1421}
1422
Bob Wilson96649842010-08-11 00:01:18 +00001423void ARMCodeEmitter::emitSaturateInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001424 const MCInstrDesc &MCID = MI.getDesc();
Bob Wilson96649842010-08-11 00:01:18 +00001425
1426 // Part of binary is determined by TableGen.
1427 unsigned Binary = getBinaryCodeForInstr(MI);
1428
1429 // Set the conditional execution predicate
1430 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1431
1432 // Encode Rd
1433 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
1434
1435 // Encode saturate bit position.
1436 unsigned Pos = MI.getOperand(1).getImm();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001437 if (MCID.Opcode == ARM::SSAT || MCID.Opcode == ARM::SSAT16)
Bob Wilson96649842010-08-11 00:01:18 +00001438 Pos -= 1;
1439 assert((Pos < 16 || (Pos < 32 &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001440 MCID.Opcode != ARM::SSAT16 &&
1441 MCID.Opcode != ARM::USAT16)) &&
Bob Wilson96649842010-08-11 00:01:18 +00001442 "saturate bit position out of range");
1443 Binary |= Pos << 16;
1444
1445 // Encode Rm
1446 Binary |= getMachineOpValue(MI, 2);
1447
1448 // Encode shift_imm.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001449 if (MCID.getNumOperands() == 4) {
Bob Wilsonadd513112010-08-11 23:10:46 +00001450 unsigned ShiftOp = MI.getOperand(3).getImm();
1451 ARM_AM::ShiftOpc Opc = ARM_AM::getSORegShOp(ShiftOp);
1452 if (Opc == ARM_AM::asr)
1453 Binary |= (1 << 6);
Bob Wilson96649842010-08-11 00:01:18 +00001454 unsigned ShiftAmt = MI.getOperand(3).getImm();
Bob Wilsonadd513112010-08-11 23:10:46 +00001455 if (ShiftAmt == 32 && Opc == ARM_AM::asr)
Bob Wilson96649842010-08-11 00:01:18 +00001456 ShiftAmt = 0;
1457 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1458 Binary |= ShiftAmt << ARMII::ShiftShift;
1459 }
1460
1461 emitWordLE(Binary);
1462}
1463
Chris Lattner8d806872010-02-02 21:48:51 +00001464void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001465 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng81889d012008-11-05 18:35:52 +00001466
Evan Cheng6cc775f2011-06-28 19:10:37 +00001467 if (MCID.Opcode == ARM::TPsoft) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001468 llvm_unreachable("ARM::TPsoft FIXME"); // FIXME
Torok Edwinfb8d6d52009-07-08 20:53:28 +00001469 }
Evan Chengaa03cd32008-11-06 17:48:05 +00001470
Evan Cheng3be5b722008-09-02 06:52:38 +00001471 // Part of binary is determined by TableGn.
1472 unsigned Binary = getBinaryCodeForInstr(MI);
1473
Evan Cheng81889d012008-11-05 18:35:52 +00001474 // Set the conditional execution predicate
Evan Cheng49d66522008-11-06 22:15:19 +00001475 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng81889d012008-11-05 18:35:52 +00001476
1477 // Set signed_immed_24 field
1478 Binary |= getMachineOpValue(MI, 0);
1479
Evan Chengfd2adbf2008-11-05 23:22:34 +00001480 emitWordLE(Binary);
Evan Cheng81889d012008-11-05 18:35:52 +00001481}
1482
Chris Lattner8d806872010-02-02 21:48:51 +00001483void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
Evan Cheng7095cd22008-11-07 09:06:08 +00001484 // Remember the base address of the inline jump table.
Evan Cheng0b773192008-12-10 02:32:19 +00001485 uintptr_t JTBase = MCE.getCurrentPCValue();
Evan Cheng8467e242008-11-07 22:30:53 +00001486 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
Chris Lattneraf29ea62009-08-23 06:49:22 +00001487 DEBUG(errs() << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase
1488 << '\n');
Evan Cheng7095cd22008-11-07 09:06:08 +00001489
1490 // Now emit the jump table entries.
1491 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1492 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1493 if (IsPIC)
1494 // DestBB address - JT base.
Evan Cheng8467e242008-11-07 22:30:53 +00001495 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
Evan Cheng7095cd22008-11-07 09:06:08 +00001496 else
1497 // Absolute DestBB address.
1498 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1499 emitWordLE(0);
1500 }
1501}
1502
Chris Lattner8d806872010-02-02 21:48:51 +00001503void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001504 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng81889d012008-11-05 18:35:52 +00001505
Evan Cheng8467e242008-11-07 22:30:53 +00001506 // Handle jump tables.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001507 if (MCID.Opcode == ARM::BR_JTr || MCID.Opcode == ARM::BR_JTadd) {
Evan Cheng8467e242008-11-07 22:30:53 +00001508 // First emit a ldr pc, [] instruction.
1509 emitDataProcessingInstruction(MI, ARM::PC);
1510
1511 // Then emit the inline jump table.
Evan Chengb61e3a82009-07-08 00:05:05 +00001512 unsigned JTIndex =
Evan Cheng6cc775f2011-06-28 19:10:37 +00001513 (MCID.Opcode == ARM::BR_JTr)
Evan Cheng8467e242008-11-07 22:30:53 +00001514 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1515 emitInlineJumpTable(JTIndex);
1516 return;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001517 } else if (MCID.Opcode == ARM::BR_JTm) {
Evan Cheng7095cd22008-11-07 09:06:08 +00001518 // First emit a ldr pc, [] instruction.
1519 emitLoadStoreInstruction(MI, ARM::PC);
1520
1521 // Then emit the inline jump table.
Evan Cheng8467e242008-11-07 22:30:53 +00001522 emitInlineJumpTable(MI.getOperand(3).getIndex());
Evan Cheng7095cd22008-11-07 09:06:08 +00001523 return;
1524 }
1525
Evan Cheng81889d012008-11-05 18:35:52 +00001526 // Part of binary is determined by TableGn.
1527 unsigned Binary = getBinaryCodeForInstr(MI);
1528
1529 // Set the conditional execution predicate
Evan Cheng49d66522008-11-06 22:15:19 +00001530 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng81889d012008-11-05 18:35:52 +00001531
Evan Cheng6cc775f2011-06-28 19:10:37 +00001532 if (MCID.Opcode == ARM::BX_RET || MCID.Opcode == ARM::MOVPCLR)
Evan Cheng81889d012008-11-05 18:35:52 +00001533 // The return register is LR.
Eric Christopher6ac277c2012-08-09 22:10:21 +00001534 Binary |= II->getRegisterInfo().getEncodingValue(ARM::LR);
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001535 else
Evan Cheng81889d012008-11-05 18:35:52 +00001536 // otherwise, set the return register
1537 Binary |= getMachineOpValue(MI, 0);
1538
Evan Chengfd2adbf2008-11-05 23:22:34 +00001539 emitWordLE(Binary);
Evan Cheng9546a5c2007-07-05 21:15:40 +00001540}
Evan Cheng3be5b722008-09-02 06:52:38 +00001541
Eric Christopher6ac277c2012-08-09 22:10:21 +00001542unsigned ARMCodeEmitter::encodeVFPRd(const MachineInstr &MI,
1543 unsigned OpIdx) const {
Evan Chenga0e2f262008-11-12 02:19:38 +00001544 unsigned RegD = MI.getOperand(OpIdx).getReg();
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001545 unsigned Binary = 0;
Craig Topperc7242e02012-04-20 07:30:17 +00001546 bool isSPVFP = ARM::SPRRegClass.contains(RegD);
Eric Christopher6ac277c2012-08-09 22:10:21 +00001547 RegD = II->getRegisterInfo().getEncodingValue(RegD);
Evan Chenga0e2f262008-11-12 02:19:38 +00001548 if (!isSPVFP)
1549 Binary |= RegD << ARMII::RegRdShift;
1550 else {
1551 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1552 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1553 }
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001554 return Binary;
1555}
Evan Cheng38c9a142008-11-11 19:40:26 +00001556
Eric Christopher6ac277c2012-08-09 22:10:21 +00001557unsigned ARMCodeEmitter::encodeVFPRn(const MachineInstr &MI,
1558 unsigned OpIdx) const {
Evan Chenga0e2f262008-11-12 02:19:38 +00001559 unsigned RegN = MI.getOperand(OpIdx).getReg();
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001560 unsigned Binary = 0;
Craig Topperc7242e02012-04-20 07:30:17 +00001561 bool isSPVFP = ARM::SPRRegClass.contains(RegN);
Eric Christopher6ac277c2012-08-09 22:10:21 +00001562 RegN = II->getRegisterInfo().getEncodingValue(RegN);
Evan Chenga0e2f262008-11-12 02:19:38 +00001563 if (!isSPVFP)
1564 Binary |= RegN << ARMII::RegRnShift;
1565 else {
1566 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1567 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1568 }
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001569 return Binary;
1570}
Evan Chenga0e2f262008-11-12 02:19:38 +00001571
Eric Christopher6ac277c2012-08-09 22:10:21 +00001572unsigned ARMCodeEmitter::encodeVFPRm(const MachineInstr &MI,
1573 unsigned OpIdx) const {
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001574 unsigned RegM = MI.getOperand(OpIdx).getReg();
1575 unsigned Binary = 0;
Craig Topperc7242e02012-04-20 07:30:17 +00001576 bool isSPVFP = ARM::SPRRegClass.contains(RegM);
Eric Christopher6ac277c2012-08-09 22:10:21 +00001577 RegM = II->getRegisterInfo().getEncodingValue(RegM);
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001578 if (!isSPVFP)
1579 Binary |= RegM;
1580 else {
1581 Binary |= ((RegM & 0x1E) >> 1);
1582 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
Evan Cheng38c9a142008-11-11 19:40:26 +00001583 }
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001584 return Binary;
1585}
1586
Chris Lattner8d806872010-02-02 21:48:51 +00001587void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001588 const MCInstrDesc &MCID = MI.getDesc();
Evan Chengaf644b52008-11-12 07:18:38 +00001589
1590 // Part of binary is determined by TableGn.
1591 unsigned Binary = getBinaryCodeForInstr(MI);
1592
1593 // Set the conditional execution predicate
1594 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1595
1596 unsigned OpIdx = 0;
1597 assert((Binary & ARMII::D_BitShift) == 0 &&
1598 (Binary & ARMII::N_BitShift) == 0 &&
1599 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1600
1601 // Encode Dd / Sd.
1602 Binary |= encodeVFPRd(MI, OpIdx++);
1603
1604 // If this is a two-address operand, skip it, e.g. FMACD.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001605 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Evan Chengaf644b52008-11-12 07:18:38 +00001606 ++OpIdx;
1607
1608 // Encode Dn / Sn.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001609 if ((MCID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
Evan Cheng052f20d2008-11-12 08:14:21 +00001610 Binary |= encodeVFPRn(MI, OpIdx++);
Evan Chengaf644b52008-11-12 07:18:38 +00001611
Evan Cheng6cc775f2011-06-28 19:10:37 +00001612 if (OpIdx == MCID.getNumOperands() ||
1613 MCID.OpInfo[OpIdx].isPredicate() ||
1614 MCID.OpInfo[OpIdx].isOptionalDef()) {
Evan Chengaf644b52008-11-12 07:18:38 +00001615 // FCMPEZD etc. has only one operand.
1616 emitWordLE(Binary);
1617 return;
1618 }
1619
1620 // Encode Dm / Sm.
1621 Binary |= encodeVFPRm(MI, OpIdx);
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001622
Evan Chengaf644b52008-11-12 07:18:38 +00001623 emitWordLE(Binary);
1624}
1625
Bob Wilsona6fe21a2010-03-17 21:16:45 +00001626void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001627 const MCInstrDesc &MCID = MI.getDesc();
1628 unsigned Form = MCID.TSFlags & ARMII::FormMask;
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001629
1630 // Part of binary is determined by TableGn.
1631 unsigned Binary = getBinaryCodeForInstr(MI);
1632
1633 // Set the conditional execution predicate
1634 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1635
1636 switch (Form) {
1637 default: break;
1638 case ARMII::VFPConv1Frm:
1639 case ARMII::VFPConv2Frm:
1640 case ARMII::VFPConv3Frm:
1641 // Encode Dd / Sd.
1642 Binary |= encodeVFPRd(MI, 0);
1643 break;
1644 case ARMII::VFPConv4Frm:
1645 // Encode Dn / Sn.
1646 Binary |= encodeVFPRn(MI, 0);
1647 break;
1648 case ARMII::VFPConv5Frm:
1649 // Encode Dm / Sm.
1650 Binary |= encodeVFPRm(MI, 0);
1651 break;
1652 }
1653
1654 switch (Form) {
1655 default: break;
1656 case ARMII::VFPConv1Frm:
1657 // Encode Dm / Sm.
1658 Binary |= encodeVFPRm(MI, 1);
Evan Cheng4af89f72008-11-13 07:46:59 +00001659 break;
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001660 case ARMII::VFPConv2Frm:
1661 case ARMII::VFPConv3Frm:
1662 // Encode Dn / Sn.
1663 Binary |= encodeVFPRn(MI, 1);
1664 break;
1665 case ARMII::VFPConv4Frm:
1666 case ARMII::VFPConv5Frm:
1667 // Encode Dd / Sd.
1668 Binary |= encodeVFPRd(MI, 1);
1669 break;
1670 }
1671
1672 if (Form == ARMII::VFPConv5Frm)
1673 // Encode Dn / Sn.
1674 Binary |= encodeVFPRn(MI, 2);
1675 else if (Form == ARMII::VFPConv3Frm)
1676 // Encode Dm / Sm.
1677 Binary |= encodeVFPRm(MI, 2);
Evan Cheng38c9a142008-11-11 19:40:26 +00001678
1679 emitWordLE(Binary);
1680}
1681
Chris Lattner8d806872010-02-02 21:48:51 +00001682void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001683 // Part of binary is determined by TableGn.
1684 unsigned Binary = getBinaryCodeForInstr(MI);
1685
1686 // Set the conditional execution predicate
1687 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1688
1689 unsigned OpIdx = 0;
1690
1691 // Encode Dd / Sd.
Evan Chengaf644b52008-11-12 07:18:38 +00001692 Binary |= encodeVFPRd(MI, OpIdx++);
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001693
1694 // Encode address base.
1695 const MachineOperand &Base = MI.getOperand(OpIdx++);
1696 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1697
1698 // If there is a non-zero immediate offset, encode it.
1699 if (Base.isReg()) {
1700 const MachineOperand &Offset = MI.getOperand(OpIdx);
1701 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1702 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1703 Binary |= 1 << ARMII::U_BitShift;
Evan Cheng45d030a2008-11-12 08:21:12 +00001704 Binary |= ImmOffs;
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001705 emitWordLE(Binary);
1706 return;
1707 }
1708 }
1709
1710 // If immediate offset is omitted, default to +0.
1711 Binary |= 1 << ARMII::U_BitShift;
1712
1713 emitWordLE(Binary);
1714}
1715
Bob Wilsona6fe21a2010-03-17 21:16:45 +00001716void
1717ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001718 const MCInstrDesc &MCID = MI.getDesc();
1719 bool IsUpdating = (MCID.TSFlags & ARMII::IndexModeMask) != 0;
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001720
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001721 // Part of binary is determined by TableGn.
1722 unsigned Binary = getBinaryCodeForInstr(MI);
1723
1724 // Set the conditional execution predicate
1725 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1726
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001727 // Skip operand 0 of an instruction with base register update.
1728 unsigned OpIdx = 0;
1729 if (IsUpdating)
1730 ++OpIdx;
1731
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001732 // Set base address operand
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001733 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001734
1735 // Set addressing mode by modifying bits U(23) and P(24)
Bill Wendlingb100f912010-11-17 05:31:09 +00001736 ARM_AM::AMSubMode Mode = ARM_AM::getLoadStoreMultipleSubMode(MI.getOpcode());
1737 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(Mode));
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001738
1739 // Set bit W(21)
Bob Wilson466d1e32010-03-16 18:38:09 +00001740 if (IsUpdating)
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001741 Binary |= 0x1 << ARMII::W_BitShift;
1742
1743 // First register is encoded in Dd.
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001744 Binary |= encodeVFPRd(MI, OpIdx+2);
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001745
Bob Wilson13ce07f2010-08-27 23:18:17 +00001746 // Count the number of registers.
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001747 unsigned NumRegs = 1;
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001748 for (unsigned i = OpIdx+3, e = MI.getNumOperands(); i != e; ++i) {
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001749 const MachineOperand &MO = MI.getOperand(i);
1750 if (!MO.isReg() || MO.isImplicit())
1751 break;
1752 ++NumRegs;
1753 }
Shih-wei Liaoe22abfa2010-05-26 00:02:28 +00001754 // Bit 8 will be set if <list> is consecutive 64-bit registers (e.g., D0)
1755 // Otherwise, it will be 0, in the case of 32-bit registers.
1756 if(Binary & 0x100)
1757 Binary |= NumRegs * 2;
1758 else
1759 Binary |= NumRegs;
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001760
1761 emitWordLE(Binary);
1762}
1763
Eric Christopher6ac277c2012-08-09 22:10:21 +00001764unsigned ARMCodeEmitter::encodeNEONRd(const MachineInstr &MI,
1765 unsigned OpIdx) const {
Bob Wilson6eae5202010-06-11 21:34:50 +00001766 unsigned RegD = MI.getOperand(OpIdx).getReg();
1767 unsigned Binary = 0;
Eric Christopher6ac277c2012-08-09 22:10:21 +00001768 RegD = II->getRegisterInfo().getEncodingValue(RegD);
Bob Wilson6eae5202010-06-11 21:34:50 +00001769 Binary |= (RegD & 0xf) << ARMII::RegRdShift;
1770 Binary |= ((RegD >> 4) & 1) << ARMII::D_BitShift;
1771 return Binary;
1772}
1773
Eric Christopher6ac277c2012-08-09 22:10:21 +00001774unsigned ARMCodeEmitter::encodeNEONRn(const MachineInstr &MI,
1775 unsigned OpIdx) const {
Bob Wilson2530ca02010-06-25 22:40:46 +00001776 unsigned RegN = MI.getOperand(OpIdx).getReg();
1777 unsigned Binary = 0;
Eric Christopher6ac277c2012-08-09 22:10:21 +00001778 RegN = II->getRegisterInfo().getEncodingValue(RegN);
Bob Wilson2530ca02010-06-25 22:40:46 +00001779 Binary |= (RegN & 0xf) << ARMII::RegRnShift;
1780 Binary |= ((RegN >> 4) & 1) << ARMII::N_BitShift;
1781 return Binary;
1782}
1783
Eric Christopher6ac277c2012-08-09 22:10:21 +00001784unsigned ARMCodeEmitter::encodeNEONRm(const MachineInstr &MI,
1785 unsigned OpIdx) const {
Bob Wilsone70c8b12010-06-25 21:17:19 +00001786 unsigned RegM = MI.getOperand(OpIdx).getReg();
1787 unsigned Binary = 0;
Eric Christopher6ac277c2012-08-09 22:10:21 +00001788 RegM = II->getRegisterInfo().getEncodingValue(RegM);
Bob Wilsone70c8b12010-06-25 21:17:19 +00001789 Binary |= (RegM & 0xf);
1790 Binary |= ((RegM >> 4) & 1) << ARMII::M_BitShift;
1791 return Binary;
1792}
1793
Bob Wilson584387d2010-06-28 21:12:19 +00001794/// convertNEONDataProcToThumb - Convert the ARM mode encoding for a NEON
1795/// data-processing instruction to the corresponding Thumb encoding.
1796static unsigned convertNEONDataProcToThumb(unsigned Binary) {
1797 assert((Binary & 0xfe000000) == 0xf2000000 &&
1798 "not an ARM NEON data-processing instruction");
1799 unsigned UBit = (Binary >> 24) & 1;
1800 return 0xef000000 | (UBit << 28) | (Binary & 0xffffff);
1801}
1802
Bob Wilsonab0819e2010-06-29 17:34:07 +00001803void ARMCodeEmitter::emitNEONLaneInstruction(const MachineInstr &MI) {
Bob Wilson0248da92010-06-26 04:07:15 +00001804 unsigned Binary = getBinaryCodeForInstr(MI);
1805
Bob Wilsonab0819e2010-06-29 17:34:07 +00001806 unsigned RegTOpIdx, RegNOpIdx, LnOpIdx;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001807 const MCInstrDesc &MCID = MI.getDesc();
1808 if ((MCID.TSFlags & ARMII::FormMask) == ARMII::NGetLnFrm) {
Bob Wilsonab0819e2010-06-29 17:34:07 +00001809 RegTOpIdx = 0;
1810 RegNOpIdx = 1;
1811 LnOpIdx = 2;
1812 } else { // ARMII::NSetLnFrm
1813 RegTOpIdx = 2;
1814 RegNOpIdx = 0;
1815 LnOpIdx = 3;
1816 }
1817
Bob Wilson0248da92010-06-26 04:07:15 +00001818 // Set the conditional execution predicate
Bob Wilson3d12ff72010-06-29 00:26:13 +00001819 Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift;
Bob Wilson0248da92010-06-26 04:07:15 +00001820
Bob Wilsonab0819e2010-06-29 17:34:07 +00001821 unsigned RegT = MI.getOperand(RegTOpIdx).getReg();
Eric Christopher6ac277c2012-08-09 22:10:21 +00001822 RegT = II->getRegisterInfo().getEncodingValue(RegT);
Bob Wilson0248da92010-06-26 04:07:15 +00001823 Binary |= (RegT << ARMII::RegRdShift);
Bob Wilsonab0819e2010-06-29 17:34:07 +00001824 Binary |= encodeNEONRn(MI, RegNOpIdx);
Bob Wilson0248da92010-06-26 04:07:15 +00001825
1826 unsigned LaneShift;
1827 if ((Binary & (1 << 22)) != 0)
1828 LaneShift = 0; // 8-bit elements
1829 else if ((Binary & (1 << 5)) != 0)
1830 LaneShift = 1; // 16-bit elements
1831 else
1832 LaneShift = 2; // 32-bit elements
1833
Bob Wilsonab0819e2010-06-29 17:34:07 +00001834 unsigned Lane = MI.getOperand(LnOpIdx).getImm() << LaneShift;
Bob Wilson0248da92010-06-26 04:07:15 +00001835 unsigned Opc1 = Lane >> 2;
1836 unsigned Opc2 = Lane & 3;
1837 assert((Opc1 & 3) == 0 && "out-of-range lane number operand");
1838 Binary |= (Opc1 << 21);
1839 Binary |= (Opc2 << 5);
1840
1841 emitWordLE(Binary);
1842}
1843
Bob Wilsonbe157b02010-06-29 20:13:29 +00001844void ARMCodeEmitter::emitNEONDupInstruction(const MachineInstr &MI) {
1845 unsigned Binary = getBinaryCodeForInstr(MI);
1846
1847 // Set the conditional execution predicate
1848 Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift;
1849
1850 unsigned RegT = MI.getOperand(1).getReg();
Eric Christopher6ac277c2012-08-09 22:10:21 +00001851 RegT = II->getRegisterInfo().getEncodingValue(RegT);
Bob Wilsonbe157b02010-06-29 20:13:29 +00001852 Binary |= (RegT << ARMII::RegRdShift);
1853 Binary |= encodeNEONRn(MI, 0);
1854 emitWordLE(Binary);
1855}
1856
Bob Wilsone70c8b12010-06-25 21:17:19 +00001857void ARMCodeEmitter::emitNEON1RegModImmInstruction(const MachineInstr &MI) {
Bob Wilson6eae5202010-06-11 21:34:50 +00001858 unsigned Binary = getBinaryCodeForInstr(MI);
1859 // Destination register is encoded in Dd.
1860 Binary |= encodeNEONRd(MI, 0);
1861 // Immediate fields: Op, Cmode, I, Imm3, Imm4
1862 unsigned Imm = MI.getOperand(1).getImm();
1863 unsigned Op = (Imm >> 12) & 1;
Bob Wilson6eae5202010-06-11 21:34:50 +00001864 unsigned Cmode = (Imm >> 8) & 0xf;
Bob Wilson6eae5202010-06-11 21:34:50 +00001865 unsigned I = (Imm >> 7) & 1;
Bob Wilson6eae5202010-06-11 21:34:50 +00001866 unsigned Imm3 = (Imm >> 4) & 0x7;
Bob Wilson6eae5202010-06-11 21:34:50 +00001867 unsigned Imm4 = Imm & 0xf;
Bob Wilson544317d2010-06-28 21:16:30 +00001868 Binary |= (I << 24) | (Imm3 << 16) | (Cmode << 8) | (Op << 5) | Imm4;
Bob Wilson4469a892010-06-28 22:23:17 +00001869 if (IsThumb)
Bob Wilson584387d2010-06-28 21:12:19 +00001870 Binary = convertNEONDataProcToThumb(Binary);
Bob Wilson6eae5202010-06-11 21:34:50 +00001871 emitWordLE(Binary);
1872}
1873
Bob Wilsone70c8b12010-06-25 21:17:19 +00001874void ARMCodeEmitter::emitNEON2RegInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001875 const MCInstrDesc &MCID = MI.getDesc();
Bob Wilsone70c8b12010-06-25 21:17:19 +00001876 unsigned Binary = getBinaryCodeForInstr(MI);
Bob Wilson2530ca02010-06-25 22:40:46 +00001877 // Destination register is encoded in Dd; source register in Dm.
1878 unsigned OpIdx = 0;
1879 Binary |= encodeNEONRd(MI, OpIdx++);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001880 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Bob Wilson2530ca02010-06-25 22:40:46 +00001881 ++OpIdx;
1882 Binary |= encodeNEONRm(MI, OpIdx);
Bob Wilson4469a892010-06-28 22:23:17 +00001883 if (IsThumb)
Bob Wilson584387d2010-06-28 21:12:19 +00001884 Binary = convertNEONDataProcToThumb(Binary);
Bob Wilsone70c8b12010-06-25 21:17:19 +00001885 // FIXME: This does not handle VDUPfdf or VDUPfqf.
1886 emitWordLE(Binary);
1887}
1888
Bob Wilson2530ca02010-06-25 22:40:46 +00001889void ARMCodeEmitter::emitNEON3RegInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001890 const MCInstrDesc &MCID = MI.getDesc();
Bob Wilson2530ca02010-06-25 22:40:46 +00001891 unsigned Binary = getBinaryCodeForInstr(MI);
1892 // Destination register is encoded in Dd; source registers in Dn and Dm.
1893 unsigned OpIdx = 0;
1894 Binary |= encodeNEONRd(MI, OpIdx++);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001895 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Bob Wilson2530ca02010-06-25 22:40:46 +00001896 ++OpIdx;
1897 Binary |= encodeNEONRn(MI, OpIdx++);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001898 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Bob Wilson2530ca02010-06-25 22:40:46 +00001899 ++OpIdx;
1900 Binary |= encodeNEONRm(MI, OpIdx);
Bob Wilson4469a892010-06-28 22:23:17 +00001901 if (IsThumb)
Bob Wilson584387d2010-06-28 21:12:19 +00001902 Binary = convertNEONDataProcToThumb(Binary);
Bob Wilson2530ca02010-06-25 22:40:46 +00001903 // FIXME: This does not handle VMOVDneon or VMOVQ.
1904 emitWordLE(Binary);
1905}
1906
Evan Cheng3be5b722008-09-02 06:52:38 +00001907#include "ARMGenCodeEmitter.inc"