blob: 568ca858c4d26e110f14e9ac3164b0cef13646c9 [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
Daniel Dunbarbc528b12009-09-21 05:58:35 +000060 void getAnalysisUsage(AnalysisUsage &AU) const {
61 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
79 bool runOnMachineFunction(MachineFunction &MF);
80
81 virtual const char *getPassName() const {
82 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 getT2AddrModeImm12OpValue(const MachineInstr &MI, unsigned Op)
211 const { return 0; }
212 unsigned getT2AddrModeImm8OpValue(const MachineInstr &MI, unsigned Op)
213 const { return 0; }
Jim Grosbach7db8d692011-09-08 22:07:06 +0000214 unsigned getT2Imm8s4OpValue(const MachineInstr &MI, unsigned Op)
215 const { return 0; }
Owen Anderson943fb602010-12-01 19:18:46 +0000216 unsigned getT2AddrModeImm8s4OpValue(const MachineInstr &MI, unsigned Op)
217 const { return 0; }
Jim Grosbacha05627e2011-09-09 18:37:27 +0000218 unsigned getT2AddrModeImm0_1020s4OpValue(const MachineInstr &MI,unsigned Op)
219 const { return 0; }
Owen Andersone22c7322010-11-30 00:14:31 +0000220 unsigned getT2AddrModeImm8OffsetOpValue(const MachineInstr &MI, unsigned Op)
221 const { return 0; }
Owen Anderson299382e2010-11-30 19:19:31 +0000222 unsigned getT2AddrModeImm12OffsetOpValue(const MachineInstr &MI,unsigned Op)
223 const { return 0; }
Owen Anderson50d662b2010-11-29 22:44:32 +0000224 unsigned getT2AddrModeSORegOpValue(const MachineInstr &MI, unsigned Op)
225 const { return 0; }
Owen Anderson8fdd1722010-11-12 21:12:40 +0000226 unsigned getT2SORegOpValue(const MachineInstr &MI, unsigned Op)
227 const { return 0; }
Owen Anderson6d375e52010-12-14 00:36:49 +0000228 unsigned getT2AdrLabelOpValue(const MachineInstr &MI, unsigned Op)
229 const { return 0; }
Owen Andersona4b63e12010-11-02 22:28:01 +0000230 unsigned getAddrMode6AddressOpValue(const MachineInstr &MI, unsigned Op)
Owen Andersonad402342010-11-02 00:05:05 +0000231 const { return 0; }
Mon P Wang92ff16b2011-05-09 17:47:27 +0000232 unsigned getAddrMode6OneLane32AddressOpValue(const MachineInstr &MI,
233 unsigned Op)
234 const { return 0; }
Bob Wilson318ce7c2010-11-30 00:00:42 +0000235 unsigned getAddrMode6DupAddressOpValue(const MachineInstr &MI, unsigned Op)
236 const { return 0; }
Owen Andersona4b63e12010-11-02 22:28:01 +0000237 unsigned getAddrMode6OffsetOpValue(const MachineInstr &MI, unsigned Op)
Owen Anderson526ffd52010-11-02 01:24:55 +0000238 const { return 0; }
Jim Grosbach5edb03e2010-10-21 22:03:21 +0000239 unsigned getBitfieldInvertedMaskOpValue(const MachineInstr &MI,
240 unsigned Op) const { return 0; }
Bruno Cardoso Lopes394f5162011-05-31 03:33:27 +0000241 unsigned getSsatBitPosValue(const MachineInstr &MI,
242 unsigned Op) const { return 0; }
Jim Grosbachcc4a4912010-11-10 23:38:36 +0000243 uint32_t getLdStmModeOpValue(const MachineInstr &MI, unsigned OpIdx)
244 const {return 0; }
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000245 uint32_t getLdStSORegOpValue(const MachineInstr &MI, unsigned OpIdx)
246 const { return 0; }
Bill Wendlinge84eb992010-11-03 01:49:29 +0000247
248 unsigned getAddrModeImm12OpValue(const MachineInstr &MI, unsigned Op)
249 const {
250 // {17-13} = reg
251 // {12} = (U)nsigned (add == '1', sub == '0')
252 // {11-0} = imm12
Bill Wendling603bd8f2010-11-02 22:31:46 +0000253 const MachineOperand &MO = MI.getOperand(Op);
254 const MachineOperand &MO1 = MI.getOperand(Op + 1);
255 if (!MO.isReg()) {
256 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
257 return 0;
Jim Grosbach333b0a92010-10-27 19:55:59 +0000258 }
Eric Christopher6ac277c2012-08-09 22:10:21 +0000259 unsigned Reg = II->getRegisterInfo().getEncodingValue(MO.getReg());
Bill Wendlinge84eb992010-11-03 01:49:29 +0000260 int32_t Imm12 = MO1.getImm();
Bill Wendling603bd8f2010-11-02 22:31:46 +0000261 uint32_t Binary;
Bill Wendlinge84eb992010-11-03 01:49:29 +0000262 Binary = Imm12 & 0xfff;
263 if (Imm12 >= 0)
264 Binary |= (1 << 12);
265 Binary |= (Reg << 13);
266 return Binary;
267 }
Jason W Kim5a97bd82010-11-18 23:37:15 +0000268
Evan Cheng965b3c72011-01-13 07:58:56 +0000269 unsigned getHiLo16ImmOpValue(const MachineInstr &MI, unsigned Op) const {
Jason W Kim5a97bd82010-11-18 23:37:15 +0000270 return 0;
271 }
272
Jim Grosbach38b469e2010-11-15 20:47:07 +0000273 uint32_t getAddrMode2OpValue(const MachineInstr &MI, unsigned OpIdx)
274 const { return 0;}
275 uint32_t getAddrMode2OffsetOpValue(const MachineInstr &MI, unsigned OpIdx)
276 const { return 0;}
Jim Grosbachd3595712011-08-03 23:50:40 +0000277 uint32_t getPostIdxRegOpValue(const MachineInstr &MI, unsigned OpIdx)
278 const { return 0;}
Jim Grosbach68685e62010-11-11 16:55:29 +0000279 uint32_t getAddrMode3OffsetOpValue(const MachineInstr &MI, unsigned OpIdx)
280 const { return 0;}
Bill Wendling811c9362010-11-30 07:44:32 +0000281 uint32_t getAddrMode3OpValue(const MachineInstr &MI, unsigned Op)
282 const { return 0; }
Jim Grosbach49bcd6f2010-12-07 21:50:47 +0000283 uint32_t getAddrModeThumbSPOpValue(const MachineInstr &MI, unsigned Op)
284 const { return 0; }
Bill Wendling0c4838b2010-12-09 21:49:07 +0000285 uint32_t getAddrModeSOpValue(const MachineInstr &MI, unsigned Op)
Bill Wendlinga9e3df72010-11-30 22:57:21 +0000286 const { return 0; }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000287 uint32_t getAddrModeISOpValue(const MachineInstr &MI, unsigned Op)
288 const { return 0; }
Bill Wendling8a6449c2010-12-08 01:57:09 +0000289 uint32_t getAddrModePCOpValue(const MachineInstr &MI, unsigned Op)
290 const { return 0; }
Bill Wendlinge84eb992010-11-03 01:49:29 +0000291 uint32_t getAddrMode5OpValue(const MachineInstr &MI, unsigned Op) const {
Bill Wendling0914d442010-11-20 00:26:37 +0000292 // {17-13} = reg
293 // {12} = (U)nsigned (add == '1', sub == '0')
294 // {11-0} = imm12
Bill Wendlinge84eb992010-11-03 01:49:29 +0000295 const MachineOperand &MO = MI.getOperand(Op);
296 const MachineOperand &MO1 = MI.getOperand(Op + 1);
297 if (!MO.isReg()) {
298 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
299 return 0;
300 }
Eric Christopher6ac277c2012-08-09 22:10:21 +0000301 unsigned Reg = II->getRegisterInfo().getEncodingValue(MO.getReg());
Bill Wendling0914d442010-11-20 00:26:37 +0000302 int32_t Imm12 = MO1.getImm();
303
304 // Special value for #-0
305 if (Imm12 == INT32_MIN)
306 Imm12 = 0;
307
308 // Immediate is always encoded as positive. The 'U' bit controls add vs
309 // sub.
310 bool isAdd = true;
311 if (Imm12 < 0) {
312 Imm12 = -Imm12;
313 isAdd = false;
314 }
315
316 uint32_t Binary = Imm12 & 0xfff;
317 if (isAdd)
318 Binary |= (1 << 12);
319 Binary |= (Reg << 13);
Bill Wendling603bd8f2010-11-02 22:31:46 +0000320 return Binary;
321 }
Jim Grosbach5f0d6162010-10-29 23:21:57 +0000322 unsigned getNEONVcvtImm32OpValue(const MachineInstr &MI, unsigned Op)
323 const { return 0; }
Jim Grosbachd9d31da2010-10-12 23:00:24 +0000324
Jim Grosbach74ef9e12010-10-30 00:37:59 +0000325 unsigned getRegisterListOpValue(const MachineInstr &MI, unsigned Op)
326 const { return 0; }
327
Bill Wendling77ad1dc2011-03-07 23:38:41 +0000328 unsigned getShiftRight8Imm(const MachineInstr &MI, unsigned Op)
Bill Wendling3b1459b2011-03-01 01:00:59 +0000329 const { return 0; }
Bill Wendling77ad1dc2011-03-07 23:38:41 +0000330 unsigned getShiftRight16Imm(const MachineInstr &MI, unsigned Op)
Bill Wendling3b1459b2011-03-01 01:00:59 +0000331 const { return 0; }
Bill Wendling77ad1dc2011-03-07 23:38:41 +0000332 unsigned getShiftRight32Imm(const MachineInstr &MI, unsigned Op)
333 const { return 0; }
334 unsigned getShiftRight64Imm(const MachineInstr &MI, unsigned Op)
Bill Wendling3b1459b2011-03-01 01:00:59 +0000335 const { return 0; }
336
Shih-wei Liaoe22abfa2010-05-26 00:02:28 +0000337 /// getMovi32Value - Return binary encoding of operand for movw/movt. If the
Jim Grosbach84511e12010-06-02 21:53:11 +0000338 /// machine operand requires relocation, record the relocation and return
339 /// zero.
Shih-wei Liaoe22abfa2010-05-26 00:02:28 +0000340 unsigned getMovi32Value(const MachineInstr &MI,const MachineOperand &MO,
Zonr Chang2da5aa12010-05-25 08:42:45 +0000341 unsigned Reloc);
Zonr Chang2da5aa12010-05-25 08:42:45 +0000342
Evan Chengfd2adbf2008-11-05 23:22:34 +0000343 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng3be5b722008-09-02 06:52:38 +0000344 ///
Evan Chengfd2adbf2008-11-05 23:22:34 +0000345 unsigned getShiftOp(unsigned Imm) const ;
Evan Cheng3be5b722008-09-02 06:52:38 +0000346
347 /// Routines that handle operands which add machine relocations which are
Evan Cheng8467e242008-11-07 22:30:53 +0000348 /// fixed up by the relocation stage.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000349 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
Jeffrey Yasskindb5f24c2009-11-07 08:51:52 +0000350 bool MayNeedFarStub, bool Indirect,
Jim Grosbachb770c002010-10-08 17:45:54 +0000351 intptr_t ACPV = 0) const;
352 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
353 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
354 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const;
Evan Cheng8467e242008-11-07 22:30:53 +0000355 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
Jim Grosbachb770c002010-10-08 17:45:54 +0000356 intptr_t JTBase = 0) const;
Eric Christopher6ac277c2012-08-09 22:10:21 +0000357 unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) const;
358 unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) const;
359 unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) const;
360 unsigned encodeNEONRd(const MachineInstr &MI, unsigned OpIdx) const;
361 unsigned encodeNEONRn(const MachineInstr &MI, unsigned OpIdx) const;
362 unsigned encodeNEONRm(const MachineInstr &MI, unsigned OpIdx) const;
Evan Cheng9546a5c2007-07-05 21:15:40 +0000363 };
Evan Cheng9546a5c2007-07-05 21:15:40 +0000364}
365
Chris Lattner8d806872010-02-02 21:48:51 +0000366char ARMCodeEmitter::ID = 0;
367
Bob Wilsona6fe21a2010-03-17 21:16:45 +0000368/// createARMJITCodeEmitterPass - Return a pass that emits the collected ARM
Chris Lattnerc83cfb9d2010-02-02 21:38:59 +0000369/// code to the specified MCE object.
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000370FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
371 JITCodeEmitter &JCE) {
Chris Lattner8d806872010-02-02 21:48:51 +0000372 return new ARMCodeEmitter(TM, JCE);
Evan Cheng9546a5c2007-07-05 21:15:40 +0000373}
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000374
Chris Lattner8d806872010-02-02 21:48:51 +0000375bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Benjamin Kramer5521b942012-12-21 19:09:53 +0000376 TargetMachine &Target = const_cast<TargetMachine&>(MF.getTarget());
377
378 assert((Target.getRelocationModel() != Reloc::Default ||
379 Target.getRelocationModel() != Reloc::Static) &&
Evan Cheng9546a5c2007-07-05 21:15:40 +0000380 "JIT relocation model must be set to static or default!");
Benjamin Kramer5521b942012-12-21 19:09:53 +0000381
382 JTI = static_cast<ARMJITInfo*>(Target.getJITInfo());
383 II = static_cast<const ARMBaseInstrInfo*>(Target.getInstrInfo());
384 TD = Target.getDataLayout();
385
Evan Chengf6b24042009-09-10 01:23:53 +0000386 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng20dbb3b2008-10-31 19:55:13 +0000387 MCPEs = &MF.getConstantPool()->getConstants();
Chris Lattnera14ac3fd2010-01-25 23:22:00 +0000388 MJTEs = 0;
389 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
Evan Cheng7095cd22008-11-07 09:06:08 +0000390 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Bob Wilson4469a892010-06-28 22:23:17 +0000391 IsThumb = MF.getInfo<ARMFunctionInfo>()->isThumbFunction();
Evan Cheng98161f52008-11-08 07:38:22 +0000392 JTI->Initialize(MF, IsPIC);
Chris Lattner34adc8d2010-03-14 01:41:15 +0000393 MMI = &getAnalysis<MachineModuleInfo>();
394 MCE.setModuleInfo(MMI);
Evan Cheng9546a5c2007-07-05 21:15:40 +0000395
396 do {
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000397 DEBUG(errs() << "JITTing function '"
Craig Toppera538d832012-08-22 06:07:19 +0000398 << MF.getName() << "'\n");
Evan Cheng9546a5c2007-07-05 21:15:40 +0000399 MCE.startFunction(MF);
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000400 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
Evan Cheng9546a5c2007-07-05 21:15:40 +0000401 MBB != E; ++MBB) {
402 MCE.StartMachineBasicBlock(MBB);
Evan Cheng2a81dd42011-12-06 22:12:01 +0000403 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Evan Cheng9546a5c2007-07-05 21:15:40 +0000404 I != E; ++I)
405 emitInstruction(*I);
406 }
407 } while (MCE.finishFunction(MF));
408
409 return false;
410}
411
Evan Chengfd2adbf2008-11-05 23:22:34 +0000412/// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng3be5b722008-09-02 06:52:38 +0000413///
Chris Lattner8d806872010-02-02 21:48:51 +0000414unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
Evan Chengfd2adbf2008-11-05 23:22:34 +0000415 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000416 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng3be5b722008-09-02 06:52:38 +0000417 case ARM_AM::asr: return 2;
418 case ARM_AM::lsl: return 0;
419 case ARM_AM::lsr: return 1;
Evan Chengf7c6eff2007-08-07 01:37:15 +0000420 case ARM_AM::ror:
Evan Cheng3be5b722008-09-02 06:52:38 +0000421 case ARM_AM::rrx: return 3;
Evan Chengf7c6eff2007-08-07 01:37:15 +0000422 }
Evan Chengf7c6eff2007-08-07 01:37:15 +0000423}
424
Shih-wei Liaoe22abfa2010-05-26 00:02:28 +0000425/// getMovi32Value - Return binary encoding of operand for movw/movt. If the
Zonr Chang2da5aa12010-05-25 08:42:45 +0000426/// machine operand requires relocation, record the relocation and return zero.
427unsigned ARMCodeEmitter::getMovi32Value(const MachineInstr &MI,
Shih-wei Liaoe22abfa2010-05-26 00:02:28 +0000428 const MachineOperand &MO,
Zonr Chang2da5aa12010-05-25 08:42:45 +0000429 unsigned Reloc) {
Shih-wei Liaoe22abfa2010-05-26 00:02:28 +0000430 assert(((Reloc == ARM::reloc_arm_movt) || (Reloc == ARM::reloc_arm_movw))
Zonr Chang2da5aa12010-05-25 08:42:45 +0000431 && "Relocation to this function should be for movt or movw");
432
433 if (MO.isImm())
434 return static_cast<unsigned>(MO.getImm());
435 else if (MO.isGlobal())
436 emitGlobalAddress(MO.getGlobal(), Reloc, true, false);
437 else if (MO.isSymbol())
438 emitExternalSymbolAddress(MO.getSymbolName(), Reloc);
439 else if (MO.isMBB())
440 emitMachineBasicBlock(MO.getMBB(), Reloc);
441 else {
442#ifndef NDEBUG
443 errs() << MO;
444#endif
445 llvm_unreachable("Unsupported operand type for movw/movt");
446 }
447 return 0;
448}
449
Evan Cheng3be5b722008-09-02 06:52:38 +0000450/// getMachineOpValue - Return binary encoding of operand. If the machine
451/// operand requires relocation, record the relocation and return zero.
Chris Lattner8d806872010-02-02 21:48:51 +0000452unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
Jim Grosbachb770c002010-10-08 17:45:54 +0000453 const MachineOperand &MO) const {
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000454 if (MO.isReg())
Eric Christopher6ac277c2012-08-09 22:10:21 +0000455 return II->getRegisterInfo().getEncodingValue(MO.getReg());
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000456 else if (MO.isImm())
Evan Cheng3be5b722008-09-02 06:52:38 +0000457 return static_cast<unsigned>(MO.getImm());
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000458 else if (MO.isGlobal())
Evan Chengf6b24042009-09-10 01:23:53 +0000459 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true, false);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000460 else if (MO.isSymbol())
Evan Chengbb373c42008-11-08 07:22:33 +0000461 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
Evan Chengbfcee5b2008-11-12 01:02:24 +0000462 else if (MO.isCPI()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000463 const MCInstrDesc &MCID = MI.getDesc();
Evan Chengbfcee5b2008-11-12 01:02:24 +0000464 // For VFP load, the immediate offset is multiplied by 4.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000465 unsigned Reloc = ((MCID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
Evan Chengbfcee5b2008-11-12 01:02:24 +0000466 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
467 emitConstPoolAddress(MO.getIndex(), Reloc);
468 } else if (MO.isJTI())
Chris Lattnera5bb3702007-12-30 23:10:15 +0000469 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000470 else if (MO.isMBB())
Evan Cheng7095cd22008-11-07 09:06:08 +0000471 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
Jim Grosbach2aeb8b92010-11-19 00:27:09 +0000472 else
473 llvm_unreachable("Unable to encode MachineOperand!");
Evan Cheng3be5b722008-09-02 06:52:38 +0000474 return 0;
Evan Chengf7c6eff2007-08-07 01:37:15 +0000475}
476
Evan Cheng933b3922008-09-18 07:28:19 +0000477/// emitGlobalAddress - Emit the specified address to the code stream.
Evan Chengf7c6eff2007-08-07 01:37:15 +0000478///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000479void ARMCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
Chris Lattner8d806872010-02-02 21:48:51 +0000480 bool MayNeedFarStub, bool Indirect,
Jim Grosbachb770c002010-10-08 17:45:54 +0000481 intptr_t ACPV) const {
Evan Chengf6b24042009-09-10 01:23:53 +0000482 MachineRelocation MR = Indirect
483 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000484 const_cast<GlobalValue *>(GV),
485 ACPV, MayNeedFarStub)
Evan Chengf6b24042009-09-10 01:23:53 +0000486 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000487 const_cast<GlobalValue *>(GV), ACPV,
488 MayNeedFarStub);
Evan Chengf6b24042009-09-10 01:23:53 +0000489 MCE.addRelocation(MR);
Evan Chengf7c6eff2007-08-07 01:37:15 +0000490}
491
492/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
493/// be emitted to the current location in the function, and allow it to be PC
494/// relative.
Jim Grosbachb770c002010-10-08 17:45:54 +0000495void ARMCodeEmitter::
496emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
Evan Chengf7c6eff2007-08-07 01:37:15 +0000497 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
498 Reloc, ES));
499}
500
501/// emitConstPoolAddress - Arrange for the address of an constant pool
502/// to be emitted to the current location in the function, and allow it to be PC
503/// relative.
Jim Grosbachb770c002010-10-08 17:45:54 +0000504void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
Evan Cheng19d64ba2008-10-29 23:55:43 +0000505 // Tell JIT emitter we'll resolve the address.
Evan Chengf7c6eff2007-08-07 01:37:15 +0000506 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng8467e242008-11-07 22:30:53 +0000507 Reloc, CPI, 0, true));
Evan Chengf7c6eff2007-08-07 01:37:15 +0000508}
509
510/// emitJumpTableAddress - Arrange for the address of a jump table to
511/// be emitted to the current location in the function, and allow it to be PC
512/// relative.
Jim Grosbachb770c002010-10-08 17:45:54 +0000513void ARMCodeEmitter::
514emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
Evan Chengf7c6eff2007-08-07 01:37:15 +0000515 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng8467e242008-11-07 22:30:53 +0000516 Reloc, JTIndex, 0, true));
Evan Chengf7c6eff2007-08-07 01:37:15 +0000517}
518
Raul Herbster1457b2b2007-08-30 23:29:26 +0000519/// emitMachineBasicBlock - Emit the specified address basic block.
Chris Lattner8d806872010-02-02 21:48:51 +0000520void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
Jim Grosbachb770c002010-10-08 17:45:54 +0000521 unsigned Reloc,
522 intptr_t JTBase) const {
Raul Herbster1457b2b2007-08-30 23:29:26 +0000523 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Evan Cheng8467e242008-11-07 22:30:53 +0000524 Reloc, BB, JTBase));
Raul Herbster1457b2b2007-08-30 23:29:26 +0000525}
Evan Chengf7c6eff2007-08-07 01:37:15 +0000526
Chris Lattner8d806872010-02-02 21:48:51 +0000527void ARMCodeEmitter::emitWordLE(unsigned Binary) {
Chris Lattneraf29ea62009-08-23 06:49:22 +0000528 DEBUG(errs() << " 0x";
529 errs().write_hex(Binary) << "\n");
Evan Chengfd2adbf2008-11-05 23:22:34 +0000530 MCE.emitWordLE(Binary);
531}
532
Chris Lattner8d806872010-02-02 21:48:51 +0000533void ARMCodeEmitter::emitDWordLE(uint64_t Binary) {
Chris Lattneraf29ea62009-08-23 06:49:22 +0000534 DEBUG(errs() << " 0x";
535 errs().write_hex(Binary) << "\n");
Evan Chengad519bb2008-11-11 22:19:31 +0000536 MCE.emitDWordLE(Binary);
537}
538
Chris Lattner8d806872010-02-02 21:48:51 +0000539void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
Chris Lattnera6f074f2009-08-23 03:41:05 +0000540 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
Evan Cheng25a39092008-09-13 01:15:21 +0000541
Devang Patel051454a2009-10-06 02:19:11 +0000542 MCE.processDebugLoc(MI.getDebugLoc(), true);
Jeffrey Yasskin15d54b92009-07-17 18:49:39 +0000543
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000544 ++NumEmitted; // Keep track of the # of mi's emitted
Evan Cheng81889d012008-11-05 18:35:52 +0000545 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
Evan Chengfabdcce2008-11-13 23:36:57 +0000546 default: {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000547 llvm_unreachable("Unhandled instruction encoding format!");
Evan Chengfabdcce2008-11-13 23:36:57 +0000548 }
Jim Grosbach56f47172010-11-17 23:33:14 +0000549 case ARMII::MiscFrm:
550 if (MI.getOpcode() == ARM::LEApcrelJT) {
551 // Materialize jumptable address.
552 emitLEApcrelJTInstruction(MI);
553 break;
554 }
555 llvm_unreachable("Unhandled instruction encoding!");
Evan Cheng81889d012008-11-05 18:35:52 +0000556 case ARMII::Pseudo:
Evan Cheng933b3922008-09-18 07:28:19 +0000557 emitPseudoInstruction(MI);
Evan Cheng81889d012008-11-05 18:35:52 +0000558 break;
559 case ARMII::DPFrm:
560 case ARMII::DPSoRegFrm:
561 emitDataProcessingInstruction(MI);
562 break;
Evan Cheng2666f592008-11-13 07:34:59 +0000563 case ARMII::LdFrm:
564 case ARMII::StFrm:
Evan Cheng81889d012008-11-05 18:35:52 +0000565 emitLoadStoreInstruction(MI);
566 break;
Evan Cheng2666f592008-11-13 07:34:59 +0000567 case ARMII::LdMiscFrm:
568 case ARMII::StMiscFrm:
Evan Cheng81889d012008-11-05 18:35:52 +0000569 emitMiscLoadStoreInstruction(MI);
570 break;
Evan Chengaf644b52008-11-12 07:18:38 +0000571 case ARMII::LdStMulFrm:
Evan Cheng81889d012008-11-05 18:35:52 +0000572 emitLoadStoreMultipleInstruction(MI);
573 break;
Evan Cheng2686c8f2008-11-06 01:21:28 +0000574 case ARMII::MulFrm:
575 emitMulFrmInstruction(MI);
Evan Cheng81889d012008-11-05 18:35:52 +0000576 break;
Evan Cheng49d66522008-11-06 22:15:19 +0000577 case ARMII::ExtFrm:
578 emitExtendInstruction(MI);
579 break;
Evan Cheng98dc53e2008-11-07 01:41:35 +0000580 case ARMII::ArithMiscFrm:
581 emitMiscArithInstruction(MI);
582 break;
Bob Wilson96649842010-08-11 00:01:18 +0000583 case ARMII::SatFrm:
584 emitSaturateInstruction(MI);
585 break;
Evan Chengaa03cd32008-11-06 17:48:05 +0000586 case ARMII::BrFrm:
Evan Cheng81889d012008-11-05 18:35:52 +0000587 emitBranchInstruction(MI);
588 break;
Evan Chengaa03cd32008-11-06 17:48:05 +0000589 case ARMII::BrMiscFrm:
Evan Cheng81889d012008-11-05 18:35:52 +0000590 emitMiscBranchInstruction(MI);
591 break;
Evan Chengac2af2f2008-11-11 02:11:05 +0000592 // VFP instructions.
593 case ARMII::VFPUnaryFrm:
594 case ARMII::VFPBinaryFrm:
595 emitVFPArithInstruction(MI);
596 break;
Evan Cheng38c9a142008-11-11 19:40:26 +0000597 case ARMII::VFPConv1Frm:
598 case ARMII::VFPConv2Frm:
Evan Cheng97ccab82008-11-11 22:46:12 +0000599 case ARMII::VFPConv3Frm:
Evan Cheng4b6c7ef2008-11-12 06:41:41 +0000600 case ARMII::VFPConv4Frm:
601 case ARMII::VFPConv5Frm:
Evan Cheng38c9a142008-11-11 19:40:26 +0000602 emitVFPConversionInstruction(MI);
603 break;
Evan Cheng8cbbcb12008-11-11 21:48:44 +0000604 case ARMII::VFPLdStFrm:
605 emitVFPLoadStoreInstruction(MI);
606 break;
607 case ARMII::VFPLdStMulFrm:
608 emitVFPLoadStoreMultipleInstruction(MI);
609 break;
Bill Wendling5f5b9222010-10-15 23:35:12 +0000610
Bob Wilson6eae5202010-06-11 21:34:50 +0000611 // NEON instructions.
Bob Wilson0248da92010-06-26 04:07:15 +0000612 case ARMII::NGetLnFrm:
Bob Wilsonab0819e2010-06-29 17:34:07 +0000613 case ARMII::NSetLnFrm:
614 emitNEONLaneInstruction(MI);
Bob Wilson0248da92010-06-26 04:07:15 +0000615 break;
Bob Wilsonbe157b02010-06-29 20:13:29 +0000616 case ARMII::NDupFrm:
617 emitNEONDupInstruction(MI);
618 break;
Bob Wilson6eae5202010-06-11 21:34:50 +0000619 case ARMII::N1RegModImmFrm:
Bob Wilsone70c8b12010-06-25 21:17:19 +0000620 emitNEON1RegModImmInstruction(MI);
621 break;
622 case ARMII::N2RegFrm:
623 emitNEON2RegInstruction(MI);
Bob Wilson6eae5202010-06-11 21:34:50 +0000624 break;
Bob Wilson2530ca02010-06-25 22:40:46 +0000625 case ARMII::N3RegFrm:
626 emitNEON3RegInstruction(MI);
627 break;
Evan Cheng81889d012008-11-05 18:35:52 +0000628 }
Devang Patel051454a2009-10-06 02:19:11 +0000629 MCE.processDebugLoc(MI.getDebugLoc(), false);
Evan Chengf7c6eff2007-08-07 01:37:15 +0000630}
631
Chris Lattner8d806872010-02-02 21:48:51 +0000632void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
Evan Cheng8467e242008-11-07 22:30:53 +0000633 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
634 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
Evan Cheng20dbb3b2008-10-31 19:55:13 +0000635 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000636
Evan Cheng467e6e82008-10-31 19:10:44 +0000637 // Remember the CONSTPOOL_ENTRY address for later relocation.
638 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
639
640 // Emit constpool island entry. In most cases, the actual values will be
641 // resolved and relocated after code emission.
642 if (MCPE.isMachineConstantPoolEntry()) {
643 ARMConstantPoolValue *ACPV =
644 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
645
Chris Lattnera6f074f2009-08-23 03:41:05 +0000646 DEBUG(errs() << " ** ARM constant pool #" << CPI << " @ "
647 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n');
Evan Cheng467e6e82008-10-31 19:10:44 +0000648
Bob Wilson433ab092009-11-02 16:59:06 +0000649 assert(ACPV->isGlobalValue() && "unsupported constant pool value");
Bill Wendling7753d662011-10-01 08:00:54 +0000650 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
Evan Cheng467e6e82008-10-31 19:10:44 +0000651 if (GV) {
Evan Chengf6b24042009-09-10 01:23:53 +0000652 Reloc::Model RelocM = TM.getRelocationModel();
Evan Cheng43b9ca62009-08-28 23:18:09 +0000653 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
Evan Chengf6b24042009-09-10 01:23:53 +0000654 isa<Function>(GV),
655 Subtarget->GVIsIndirectSymbol(GV, RelocM),
656 (intptr_t)ACPV);
Bill Wendlingc214cb02011-10-01 08:58:29 +0000657 } else {
658 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
659 emitExternalSymbolAddress(Sym, ARM::reloc_arm_absolute);
Evan Cheng467e6e82008-10-31 19:10:44 +0000660 }
Evan Chengfd2adbf2008-11-05 23:22:34 +0000661 emitWordLE(0);
Evan Cheng467e6e82008-10-31 19:10:44 +0000662 } else {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000663 const Constant *CV = MCPE.Val.ConstVal;
Evan Cheng467e6e82008-10-31 19:10:44 +0000664
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000665 DEBUG({
666 errs() << " ** Constant pool #" << CPI << " @ "
667 << (void*)MCE.getCurrentPCValue() << " ";
668 if (const Function *F = dyn_cast<Function>(CV))
669 errs() << F->getName();
670 else
671 errs() << *CV;
672 errs() << '\n';
673 });
Evan Cheng467e6e82008-10-31 19:10:44 +0000674
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000675 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Evan Chengf6b24042009-09-10 01:23:53 +0000676 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV), false);
Evan Chengfd2adbf2008-11-05 23:22:34 +0000677 emitWordLE(0);
Evan Chengad519bb2008-11-11 22:19:31 +0000678 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Gabor Greifb171ca02010-10-22 23:16:11 +0000679 uint32_t Val = uint32_t(*CI->getValue().getRawData());
Evan Chengfd2adbf2008-11-05 23:22:34 +0000680 emitWordLE(Val);
Evan Chengad519bb2008-11-11 22:19:31 +0000681 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Chris Lattnerfdd87902009-10-05 05:54:46 +0000682 if (CFP->getType()->isFloatTy())
Evan Chengad519bb2008-11-11 22:19:31 +0000683 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Chris Lattnerfdd87902009-10-05 05:54:46 +0000684 else if (CFP->getType()->isDoubleTy())
Evan Chengad519bb2008-11-11 22:19:31 +0000685 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
686 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000687 llvm_unreachable("Unable to handle this constantpool entry!");
Evan Chengad519bb2008-11-11 22:19:31 +0000688 }
689 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000690 llvm_unreachable("Unable to handle this constantpool entry!");
Evan Cheng467e6e82008-10-31 19:10:44 +0000691 }
692 }
693}
694
Zonr Chang2da5aa12010-05-25 08:42:45 +0000695void ARMCodeEmitter::emitMOVi32immInstruction(const MachineInstr &MI) {
696 const MachineOperand &MO0 = MI.getOperand(0);
697 const MachineOperand &MO1 = MI.getOperand(1);
698
699 // Emit the 'movw' instruction.
700 unsigned Binary = 0x30 << 20; // mov: Insts{27-20} = 0b00110000
701
702 unsigned Lo16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movw) & 0xFFFF;
703
704 // Set the conditional execution predicate.
705 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
706
707 // Encode Rd.
708 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
709
710 // Encode imm16 as imm4:imm12
711 Binary |= Lo16 & 0xFFF; // Insts{11-0} = imm12
712 Binary |= ((Lo16 >> 12) & 0xF) << 16; // Insts{19-16} = imm4
713 emitWordLE(Binary);
714
715 unsigned Hi16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movt) >> 16;
716 // Emit the 'movt' instruction.
717 Binary = 0x34 << 20; // movt: Insts{27-20} = 0b00110100
718
719 // Set the conditional execution predicate.
720 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
721
722 // Encode Rd.
723 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
724
725 // Encode imm16 as imm4:imm1, same as movw above.
726 Binary |= Hi16 & 0xFFF;
727 Binary |= ((Hi16 >> 12) & 0xF) << 16;
728 emitWordLE(Binary);
729}
730
Chris Lattner8d806872010-02-02 21:48:51 +0000731void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
Evan Chengb870fd82008-11-06 02:25:39 +0000732 const MachineOperand &MO0 = MI.getOperand(0);
733 const MachineOperand &MO1 = MI.getOperand(1);
Bob Wilson1b0e6142010-03-11 00:46:22 +0000734 assert(MO1.isImm() && ARM_AM::isSOImmTwoPartVal(MO1.getImm()) &&
735 "Not a valid so_imm value!");
Evan Chengb870fd82008-11-06 02:25:39 +0000736 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
737 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
738
739 // Emit the 'mov' instruction.
740 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
741
742 // Set the conditional execution predicate.
Evan Cheng49d66522008-11-06 22:15:19 +0000743 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengb870fd82008-11-06 02:25:39 +0000744
745 // Encode Rd.
746 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
747
748 // Encode so_imm.
749 // Set bit I(25) to identify this is the immediate form of <shifter_op>
750 Binary |= 1 << ARMII::I_BitShift;
Evan Chenge3a53c42009-07-08 21:03:57 +0000751 Binary |= getMachineSoImmOpValue(V1);
Evan Chengb870fd82008-11-06 02:25:39 +0000752 emitWordLE(Binary);
753
754 // Now the 'orr' instruction.
755 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
756
757 // Set the conditional execution predicate.
Evan Cheng49d66522008-11-06 22:15:19 +0000758 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengb870fd82008-11-06 02:25:39 +0000759
760 // Encode Rd.
761 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
762
763 // Encode Rn.
764 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
765
766 // Encode so_imm.
767 // Set bit I(25) to identify this is the immediate form of <shifter_op>
768 Binary |= 1 << ARMII::I_BitShift;
Evan Chenge3a53c42009-07-08 21:03:57 +0000769 Binary |= getMachineSoImmOpValue(V2);
Evan Chengb870fd82008-11-06 02:25:39 +0000770 emitWordLE(Binary);
771}
772
Chris Lattner8d806872010-02-02 21:48:51 +0000773void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
Evan Cheng7095cd22008-11-07 09:06:08 +0000774 // It's basically add r, pc, (LJTI - $+8)
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000775
Evan Cheng6cc775f2011-06-28 19:10:37 +0000776 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng7095cd22008-11-07 09:06:08 +0000777
778 // Emit the 'add' instruction.
Jim Grosbach4ded8f22010-11-17 21:57:51 +0000779 unsigned Binary = 0x4 << 21; // add: Insts{24-21} = 0b0100
Evan Cheng7095cd22008-11-07 09:06:08 +0000780
781 // Set the conditional execution predicate
782 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
783
784 // Encode S bit if MI modifies CPSR.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000785 Binary |= getAddrModeSBit(MI, MCID);
Evan Cheng7095cd22008-11-07 09:06:08 +0000786
787 // Encode Rd.
788 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
789
790 // Encode Rn which is PC.
Eric Christopher6ac277c2012-08-09 22:10:21 +0000791 Binary |= II->getRegisterInfo().getEncodingValue(ARM::PC) << ARMII::RegRnShift;
Evan Cheng7095cd22008-11-07 09:06:08 +0000792
793 // Encode the displacement.
Evan Cheng7095cd22008-11-07 09:06:08 +0000794 Binary |= 1 << ARMII::I_BitShift;
795 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
796
797 emitWordLE(Binary);
798}
799
Chris Lattner8d806872010-02-02 21:48:51 +0000800void ARMCodeEmitter::emitPseudoMoveInstruction(const MachineInstr &MI) {
Evan Cheng30f6f8f2008-11-14 20:09:11 +0000801 unsigned Opcode = MI.getDesc().Opcode;
802
803 // Part of binary is determined by TableGn.
804 unsigned Binary = getBinaryCodeForInstr(MI);
805
806 // Set the conditional execution predicate
807 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
808
809 // Encode S bit if MI modifies CPSR.
810 if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag)
811 Binary |= 1 << ARMII::S_BitShift;
812
813 // Encode register def if there is one.
814 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
815
816 // Encode the shift operation.
817 switch (Opcode) {
818 default: break;
Jim Grosbach062749c2010-10-14 20:43:44 +0000819 case ARM::RRX:
Evan Cheng30f6f8f2008-11-14 20:09:11 +0000820 // rrx
821 Binary |= 0x6 << 4;
822 break;
823 case ARM::MOVsrl_flag:
824 // lsr #1
825 Binary |= (0x2 << 4) | (1 << 7);
826 break;
827 case ARM::MOVsra_flag:
828 // asr #1
829 Binary |= (0x4 << 4) | (1 << 7);
830 break;
831 }
832
833 // Encode register Rm.
834 Binary |= getMachineOpValue(MI, 1);
835
836 emitWordLE(Binary);
837}
838
Chris Lattner8d806872010-02-02 21:48:51 +0000839void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
Chris Lattneraf29ea62009-08-23 06:49:22 +0000840 DEBUG(errs() << " ** LPC" << LabelID << " @ "
841 << (void*)MCE.getCurrentPCValue() << '\n');
Evan Chengfd2adbf2008-11-05 23:22:34 +0000842 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
843}
844
Chris Lattner8d806872010-02-02 21:48:51 +0000845void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
Evan Cheng467e6e82008-10-31 19:10:44 +0000846 unsigned Opcode = MI.getDesc().Opcode;
847 switch (Opcode) {
848 default:
Evan Cheng83e0d482009-09-28 09:14:39 +0000849 llvm_unreachable("ARMCodeEmitter::emitPseudoInstruction");
Jim Grosbach027bd472010-11-30 00:24:05 +0000850 case ARM::BX_CALL:
Jakob Stoklund Olesen6a2e99a2012-04-06 00:04:58 +0000851 case ARM::BMOVPCRX_CALL: {
Xerxes Ranbyff66cd42010-07-22 17:28:34 +0000852 // First emit mov lr, pc
853 unsigned Binary = 0x01a0e00f;
854 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
855 emitWordLE(Binary);
856
857 // and then emit the branch.
858 emitMiscBranchInstruction(MI);
859 break;
860 }
Chris Lattnerb06015a2010-02-09 19:54:29 +0000861 case TargetOpcode::INLINEASM: {
Evan Cheng59213d62008-11-19 23:21:33 +0000862 // We allow inline assembler nodes with empty bodies - they can
863 // implicitly define registers, which is ok for JIT.
864 if (MI.getOperand(0).getSymbolName()[0]) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000865 report_fatal_error("JIT does not support inline asm!");
Evan Cheng59213d62008-11-19 23:21:33 +0000866 }
Evan Chengfabdcce2008-11-13 23:36:57 +0000867 break;
868 }
Bill Wendling499f7972010-07-16 22:20:36 +0000869 case TargetOpcode::PROLOG_LABEL:
Chris Lattneree2fbbc2010-03-14 02:33:54 +0000870 case TargetOpcode::EH_LABEL:
871 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
872 break;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000873 case TargetOpcode::IMPLICIT_DEF:
874 case TargetOpcode::KILL:
Evan Chengfabdcce2008-11-13 23:36:57 +0000875 // Do nothing.
876 break;
Evan Cheng467e6e82008-10-31 19:10:44 +0000877 case ARM::CONSTPOOL_ENTRY:
878 emitConstPoolInstruction(MI);
879 break;
880 case ARM::PICADD: {
Evan Cheng6dd08b62008-11-04 00:50:32 +0000881 // Remember of the address of the PC label for relocation later.
Evan Chengfd2adbf2008-11-05 23:22:34 +0000882 addPCLabel(MI.getOperand(2).getImm());
Evan Cheng467e6e82008-10-31 19:10:44 +0000883 // PICADD is just an add instruction that implicitly read pc.
Evan Cheng8467e242008-11-07 22:30:53 +0000884 emitDataProcessingInstruction(MI, 0, ARM::PC);
Evan Chengfd2adbf2008-11-05 23:22:34 +0000885 break;
886 }
887 case ARM::PICLDR:
888 case ARM::PICLDRB:
889 case ARM::PICSTR:
890 case ARM::PICSTRB: {
891 // Remember of the address of the PC label for relocation later.
892 addPCLabel(MI.getOperand(2).getImm());
893 // These are just load / store instructions that implicitly read pc.
Evan Cheng7095cd22008-11-07 09:06:08 +0000894 emitLoadStoreInstruction(MI, 0, ARM::PC);
Evan Chengfd2adbf2008-11-05 23:22:34 +0000895 break;
896 }
897 case ARM::PICLDRH:
898 case ARM::PICLDRSH:
899 case ARM::PICLDRSB:
900 case ARM::PICSTRH: {
901 // Remember of the address of the PC label for relocation later.
902 addPCLabel(MI.getOperand(2).getImm());
903 // These are just load / store instructions that implicitly read pc.
904 emitMiscLoadStoreInstruction(MI, ARM::PC);
Evan Cheng467e6e82008-10-31 19:10:44 +0000905 break;
906 }
Zonr Chang2da5aa12010-05-25 08:42:45 +0000907
908 case ARM::MOVi32imm:
Evan Chengf478cf92010-11-12 23:03:38 +0000909 // Two instructions to materialize a constant.
910 if (Subtarget->hasV6T2Ops())
911 emitMOVi32immInstruction(MI);
912 else
913 emitMOVi2piecesInstruction(MI);
Zonr Chang2da5aa12010-05-25 08:42:45 +0000914 break;
915
Evan Cheng7095cd22008-11-07 09:06:08 +0000916 case ARM::LEApcrelJT:
917 // Materialize jumptable address.
918 emitLEApcrelJTInstruction(MI);
919 break;
Jim Grosbach062749c2010-10-14 20:43:44 +0000920 case ARM::RRX:
Evan Cheng30f6f8f2008-11-14 20:09:11 +0000921 case ARM::MOVsrl_flag:
922 case ARM::MOVsra_flag:
923 emitPseudoMoveInstruction(MI);
924 break;
Evan Cheng467e6e82008-10-31 19:10:44 +0000925 }
926}
927
Bob Wilsona6fe21a2010-03-17 21:16:45 +0000928unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
Evan Cheng6cc775f2011-06-28 19:10:37 +0000929 const MCInstrDesc &MCID,
Evan Cheng467e6e82008-10-31 19:10:44 +0000930 const MachineOperand &MO,
Evan Cheng33fa89c6f2008-09-12 22:01:15 +0000931 unsigned OpIdx) {
Evan Cheng467e6e82008-10-31 19:10:44 +0000932 unsigned Binary = getMachineOpValue(MI, MO);
Evan Cheng33fa89c6f2008-09-12 22:01:15 +0000933
934 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
935 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
936 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
937
938 // Encode the shift opcode.
939 unsigned SBits = 0;
940 unsigned Rs = MO1.getReg();
941 if (Rs) {
942 // Set shift operand (bit[7:4]).
943 // LSL - 0001
944 // LSR - 0011
945 // ASR - 0101
946 // ROR - 0111
947 // RRX - 0110 and bit[11:8] clear.
948 switch (SOpc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000949 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng33fa89c6f2008-09-12 22:01:15 +0000950 case ARM_AM::lsl: SBits = 0x1; break;
951 case ARM_AM::lsr: SBits = 0x3; break;
952 case ARM_AM::asr: SBits = 0x5; break;
953 case ARM_AM::ror: SBits = 0x7; break;
954 case ARM_AM::rrx: SBits = 0x6; break;
955 }
956 } else {
957 // Set shift operand (bit[6:4]).
958 // LSL - 000
959 // LSR - 010
960 // ASR - 100
961 // ROR - 110
962 switch (SOpc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000963 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng33fa89c6f2008-09-12 22:01:15 +0000964 case ARM_AM::lsl: SBits = 0x0; break;
965 case ARM_AM::lsr: SBits = 0x2; break;
966 case ARM_AM::asr: SBits = 0x4; break;
967 case ARM_AM::ror: SBits = 0x6; break;
968 }
969 }
970 Binary |= SBits << 4;
971 if (SOpc == ARM_AM::rrx)
972 return Binary;
973
974 // Encode the shift operation Rs or shift_imm (except rrx).
975 if (Rs) {
976 // Encode Rs bit[11:8].
977 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
Eric Christopher6ac277c2012-08-09 22:10:21 +0000978 return Binary | (II->getRegisterInfo().getEncodingValue(Rs) << ARMII::RegRsShift);
Evan Cheng33fa89c6f2008-09-12 22:01:15 +0000979 }
980
981 // Encode shift_imm bit[11:7].
982 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
983}
984
Chris Lattner8d806872010-02-02 21:48:51 +0000985unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000986 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
987 assert(SoImmVal != -1 && "Not a valid so_imm value!");
988
Evan Cheng467e6e82008-10-31 19:10:44 +0000989 // Encode rotate_imm.
Evan Chenge3a53c42009-07-08 21:03:57 +0000990 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
Evan Cheng49d66522008-11-06 22:15:19 +0000991 << ARMII::SoRotImmShift;
992
Evan Cheng467e6e82008-10-31 19:10:44 +0000993 // Encode immed_8.
Evan Chenge3a53c42009-07-08 21:03:57 +0000994 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
Evan Cheng467e6e82008-10-31 19:10:44 +0000995 return Binary;
996}
997
Chris Lattner8d806872010-02-02 21:48:51 +0000998unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
Evan Cheng6cc775f2011-06-28 19:10:37 +0000999 const MCInstrDesc &MCID) const {
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001000 for (unsigned i = MI.getNumOperands(), e = MCID.getNumOperands(); i >= e;--i){
Evan Chengd1424c42008-09-12 22:45:55 +00001001 const MachineOperand &MO = MI.getOperand(i-1);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001002 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
Evan Chengd1424c42008-09-12 22:45:55 +00001003 return 1 << ARMII::S_BitShift;
1004 }
1005 return 0;
1006}
1007
Bob Wilsona6fe21a2010-03-17 21:16:45 +00001008void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
Evan Cheng8467e242008-11-07 22:30:53 +00001009 unsigned ImplicitRd,
Evan Chengfd2adbf2008-11-05 23:22:34 +00001010 unsigned ImplicitRn) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001011 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng81889d012008-11-05 18:35:52 +00001012
1013 // Part of binary is determined by TableGn.
1014 unsigned Binary = getBinaryCodeForInstr(MI);
1015
Jim Grosbachc084e842008-10-07 19:05:35 +00001016 // Set the conditional execution predicate
Evan Cheng49d66522008-11-06 22:15:19 +00001017 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng33fa89c6f2008-09-12 22:01:15 +00001018
Evan Chengd1424c42008-09-12 22:45:55 +00001019 // Encode S bit if MI modifies CPSR.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001020 Binary |= getAddrModeSBit(MI, MCID);
Evan Chengd1424c42008-09-12 22:45:55 +00001021
Evan Cheng33fa89c6f2008-09-12 22:01:15 +00001022 // Encode register def if there is one.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001023 unsigned NumDefs = MCID.getNumDefs();
Evan Chengc5c74f32008-09-12 23:15:39 +00001024 unsigned OpIdx = 0;
Evan Cheng8467e242008-11-07 22:30:53 +00001025 if (NumDefs)
1026 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1027 else if (ImplicitRd)
1028 // Special handling for implicit use (e.g. PC).
Eric Christopher6ac277c2012-08-09 22:10:21 +00001029 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRd) << ARMII::RegRdShift);
Evan Cheng3be5b722008-09-02 06:52:38 +00001030
Evan Cheng6cc775f2011-06-28 19:10:37 +00001031 if (MCID.Opcode == ARM::MOVi16) {
Zonr Chang2da5aa12010-05-25 08:42:45 +00001032 // Get immediate from MI.
1033 unsigned Lo16 = getMovi32Value(MI, MI.getOperand(OpIdx),
1034 ARM::reloc_arm_movw);
1035 // Encode imm which is the same as in emitMOVi32immInstruction().
1036 Binary |= Lo16 & 0xFFF;
1037 Binary |= ((Lo16 >> 12) & 0xF) << 16;
1038 emitWordLE(Binary);
1039 return;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001040 } else if(MCID.Opcode == ARM::MOVTi16) {
Zonr Chang2da5aa12010-05-25 08:42:45 +00001041 unsigned Hi16 = (getMovi32Value(MI, MI.getOperand(OpIdx),
1042 ARM::reloc_arm_movt) >> 16);
1043 Binary |= Hi16 & 0xFFF;
1044 Binary |= ((Hi16 >> 12) & 0xF) << 16;
1045 emitWordLE(Binary);
1046 return;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001047 } else if ((MCID.Opcode == ARM::BFC) || (MCID.Opcode == ARM::BFI)) {
Shih-wei Liaob6e0bc92010-05-26 00:25:05 +00001048 uint32_t v = ~MI.getOperand(2).getImm();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001049 int32_t lsb = countTrailingZeros(v);
1050 int32_t msb = (32 - countLeadingZeros(v)) - 1;
Shih-wei Liao0568ca02010-05-26 03:21:39 +00001051 // Instr{20-16} = msb, Instr{11-7} = lsb
Shih-wei Liaob6e0bc92010-05-26 00:25:05 +00001052 Binary |= (msb & 0x1F) << 16;
1053 Binary |= (lsb & 0x1F) << 7;
1054 emitWordLE(Binary);
1055 return;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001056 } else if ((MCID.Opcode == ARM::UBFX) || (MCID.Opcode == ARM::SBFX)) {
Shih-wei Liao0568ca02010-05-26 03:21:39 +00001057 // Encode Rn in Instr{0-3}
1058 Binary |= getMachineOpValue(MI, OpIdx++);
1059
1060 uint32_t lsb = MI.getOperand(OpIdx++).getImm();
1061 uint32_t widthm1 = MI.getOperand(OpIdx++).getImm() - 1;
1062
1063 // Instr{20-16} = widthm1, Instr{11-7} = lsb
1064 Binary |= (widthm1 & 0x1F) << 16;
1065 Binary |= (lsb & 0x1F) << 7;
1066 emitWordLE(Binary);
1067 return;
Zonr Chang2da5aa12010-05-25 08:42:45 +00001068 }
1069
Evan Cheng47b546d2008-11-06 08:47:38 +00001070 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001071 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Evan Cheng47b546d2008-11-06 08:47:38 +00001072 ++OpIdx;
1073
Jim Grosbach3dc0a3b2008-10-01 18:16:49 +00001074 // Encode first non-shifter register operand if there is one.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001075 bool isUnary = MCID.TSFlags & ARMII::UnaryDP;
Evan Cheng81889d012008-11-05 18:35:52 +00001076 if (!isUnary) {
Evan Chengfd2adbf2008-11-05 23:22:34 +00001077 if (ImplicitRn)
1078 // Special handling for implicit use (e.g. PC).
Eric Christopher6ac277c2012-08-09 22:10:21 +00001079 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRn) << ARMII::RegRnShift);
Evan Cheng467e6e82008-10-31 19:10:44 +00001080 else {
1081 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
1082 ++OpIdx;
1083 }
Evan Cheng3be5b722008-09-02 06:52:38 +00001084 }
1085
Evan Cheng33fa89c6f2008-09-12 22:01:15 +00001086 // Encode shifter operand.
Evan Cheng33fa89c6f2008-09-12 22:01:15 +00001087 const MachineOperand &MO = MI.getOperand(OpIdx);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001088 if ((MCID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
Evan Cheng467e6e82008-10-31 19:10:44 +00001089 // Encode SoReg.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001090 emitWordLE(Binary | getMachineSoRegOpValue(MI, MCID, MO, OpIdx));
Evan Cheng81889d012008-11-05 18:35:52 +00001091 return;
1092 }
Evan Cheng467e6e82008-10-31 19:10:44 +00001093
Evan Cheng81889d012008-11-05 18:35:52 +00001094 if (MO.isReg()) {
Evan Cheng33fa89c6f2008-09-12 22:01:15 +00001095 // Encode register Rm.
Eric Christopher6ac277c2012-08-09 22:10:21 +00001096 emitWordLE(Binary | II->getRegisterInfo().getEncodingValue(MO.getReg()));
Evan Cheng81889d012008-11-05 18:35:52 +00001097 return;
1098 }
Evan Cheng3be5b722008-09-02 06:52:38 +00001099
Evan Cheng33fa89c6f2008-09-12 22:01:15 +00001100 // Encode so_imm.
Evan Chenge3a53c42009-07-08 21:03:57 +00001101 Binary |= getMachineSoImmOpValue((unsigned)MO.getImm());
Evan Cheng81889d012008-11-05 18:35:52 +00001102
Evan Chengfd2adbf2008-11-05 23:22:34 +00001103 emitWordLE(Binary);
Evan Cheng3be5b722008-09-02 06:52:38 +00001104}
1105
Bob Wilsona6fe21a2010-03-17 21:16:45 +00001106void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
Evan Cheng7095cd22008-11-07 09:06:08 +00001107 unsigned ImplicitRd,
Evan Chengfd2adbf2008-11-05 23:22:34 +00001108 unsigned ImplicitRn) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001109 const MCInstrDesc &MCID = MI.getDesc();
1110 unsigned Form = MCID.TSFlags & ARMII::FormMask;
1111 bool IsPrePost = (MCID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng077c8f82008-11-08 01:44:13 +00001112
Evan Cheng81889d012008-11-05 18:35:52 +00001113 // Part of binary is determined by TableGn.
1114 unsigned Binary = getBinaryCodeForInstr(MI);
1115
Jim Grosbach338de3e2010-10-27 23:12:14 +00001116 // If this is an LDRi12, STRi12 or LDRcp, nothing more needs be done.
1117 if (MI.getOpcode() == ARM::LDRi12 || MI.getOpcode() == ARM::LDRcp ||
1118 MI.getOpcode() == ARM::STRi12) {
Jim Grosbachba1c6cd2010-10-27 17:52:51 +00001119 emitWordLE(Binary);
1120 return;
1121 }
1122
Jim Grosbachc084e842008-10-07 19:05:35 +00001123 // Set the conditional execution predicate
Evan Cheng49d66522008-11-06 22:15:19 +00001124 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng933b3922008-09-18 07:28:19 +00001125
Evan Cheng7095cd22008-11-07 09:06:08 +00001126 unsigned OpIdx = 0;
Evan Cheng2666f592008-11-13 07:34:59 +00001127
1128 // Operand 0 of a pre- and post-indexed store is the address base
1129 // writeback. Skip it.
1130 bool Skipped = false;
1131 if (IsPrePost && Form == ARMII::StFrm) {
1132 ++OpIdx;
1133 Skipped = true;
1134 }
1135
1136 // Set first operand
Evan Cheng7095cd22008-11-07 09:06:08 +00001137 if (ImplicitRd)
1138 // Special handling for implicit use (e.g. PC).
Eric Christopher6ac277c2012-08-09 22:10:21 +00001139 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRd) << ARMII::RegRdShift);
Evan Cheng7095cd22008-11-07 09:06:08 +00001140 else
1141 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng3be5b722008-09-02 06:52:38 +00001142
1143 // Set second operand
Evan Chengfd2adbf2008-11-05 23:22:34 +00001144 if (ImplicitRn)
1145 // Special handling for implicit use (e.g. PC).
Eric Christopher6ac277c2012-08-09 22:10:21 +00001146 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRn) << ARMII::RegRnShift);
Evan Cheng7095cd22008-11-07 09:06:08 +00001147 else
1148 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng3be5b722008-09-02 06:52:38 +00001149
Evan Cheng077c8f82008-11-08 01:44:13 +00001150 // If this is a two-address operand, skip it. e.g. LDR_PRE.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001151 if (!Skipped && MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Evan Cheng077c8f82008-11-08 01:44:13 +00001152 ++OpIdx;
1153
Evan Chengfd2adbf2008-11-05 23:22:34 +00001154 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Cheng47b546d2008-11-06 08:47:38 +00001155 unsigned AM2Opc = (ImplicitRn == ARM::PC)
Evan Chengfd2adbf2008-11-05 23:22:34 +00001156 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng3be5b722008-09-02 06:52:38 +00001157
Evan Cheng380482a2008-09-13 01:44:01 +00001158 // Set bit U(23) according to sign of immed value (positive or negative).
Evan Chengfd2adbf2008-11-05 23:22:34 +00001159 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
Evan Cheng380482a2008-09-13 01:44:01 +00001160 ARMII::U_BitShift);
Evan Cheng3be5b722008-09-02 06:52:38 +00001161 if (!MO2.getReg()) { // is immediate
Evan Chengfd2adbf2008-11-05 23:22:34 +00001162 if (ARM_AM::getAM2Offset(AM2Opc))
Evan Cheng3be5b722008-09-02 06:52:38 +00001163 // Set the value of offset_12 field
Evan Chengfd2adbf2008-11-05 23:22:34 +00001164 Binary |= ARM_AM::getAM2Offset(AM2Opc);
1165 emitWordLE(Binary);
Evan Cheng81889d012008-11-05 18:35:52 +00001166 return;
Evan Cheng3be5b722008-09-02 06:52:38 +00001167 }
1168
Bill Wendling05819052010-10-20 22:44:54 +00001169 // Set bit I(25), because this is not in immediate encoding.
Evan Cheng3be5b722008-09-02 06:52:38 +00001170 Binary |= 1 << ARMII::I_BitShift;
1171 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
1172 // Set bit[3:0] to the corresponding Rm register
Eric Christopher6ac277c2012-08-09 22:10:21 +00001173 Binary |= II->getRegisterInfo().getEncodingValue(MO2.getReg());
Evan Cheng3be5b722008-09-02 06:52:38 +00001174
Evan Cheng2836d912008-11-12 07:34:37 +00001175 // If this instr is in scaled register offset/index instruction, set
Evan Cheng3be5b722008-09-02 06:52:38 +00001176 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
Evan Chengfd2adbf2008-11-05 23:22:34 +00001177 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
Evan Cheng2836d912008-11-12 07:34:37 +00001178 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
1179 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
Evan Cheng3be5b722008-09-02 06:52:38 +00001180 }
1181
Evan Chengfd2adbf2008-11-05 23:22:34 +00001182 emitWordLE(Binary);
Evan Cheng3be5b722008-09-02 06:52:38 +00001183}
1184
Chris Lattner8d806872010-02-02 21:48:51 +00001185void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
Bob Wilsona6fe21a2010-03-17 21:16:45 +00001186 unsigned ImplicitRn) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001187 const MCInstrDesc &MCID = MI.getDesc();
1188 unsigned Form = MCID.TSFlags & ARMII::FormMask;
1189 bool IsPrePost = (MCID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng077c8f82008-11-08 01:44:13 +00001190
Evan Cheng81889d012008-11-05 18:35:52 +00001191 // Part of binary is determined by TableGn.
1192 unsigned Binary = getBinaryCodeForInstr(MI);
1193
Jim Grosbachc084e842008-10-07 19:05:35 +00001194 // Set the conditional execution predicate
Evan Cheng49d66522008-11-06 22:15:19 +00001195 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng933b3922008-09-18 07:28:19 +00001196
Evan Cheng2666f592008-11-13 07:34:59 +00001197 unsigned OpIdx = 0;
1198
1199 // Operand 0 of a pre- and post-indexed store is the address base
1200 // writeback. Skip it.
1201 bool Skipped = false;
1202 if (IsPrePost && Form == ARMII::StMiscFrm) {
1203 ++OpIdx;
1204 Skipped = true;
1205 }
1206
Evan Cheng3be5b722008-09-02 06:52:38 +00001207 // Set first operand
Evan Cheng2666f592008-11-13 07:34:59 +00001208 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng3be5b722008-09-02 06:52:38 +00001209
Evan Cheng1283c6a2009-06-15 08:28:29 +00001210 // Skip LDRD and STRD's second operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001211 if (MCID.Opcode == ARM::LDRD || MCID.Opcode == ARM::STRD)
Evan Cheng1283c6a2009-06-15 08:28:29 +00001212 ++OpIdx;
1213
Evan Cheng3be5b722008-09-02 06:52:38 +00001214 // Set second operand
Evan Chengfd2adbf2008-11-05 23:22:34 +00001215 if (ImplicitRn)
1216 // Special handling for implicit use (e.g. PC).
Eric Christopher6ac277c2012-08-09 22:10:21 +00001217 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRn) << ARMII::RegRnShift);
Evan Cheng7095cd22008-11-07 09:06:08 +00001218 else
1219 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng3be5b722008-09-02 06:52:38 +00001220
Evan Cheng077c8f82008-11-08 01:44:13 +00001221 // If this is a two-address operand, skip it. e.g. LDRH_POST.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001222 if (!Skipped && MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Evan Cheng077c8f82008-11-08 01:44:13 +00001223 ++OpIdx;
1224
Evan Chengfd2adbf2008-11-05 23:22:34 +00001225 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Cheng47b546d2008-11-06 08:47:38 +00001226 unsigned AM3Opc = (ImplicitRn == ARM::PC)
Evan Chengfd2adbf2008-11-05 23:22:34 +00001227 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng3be5b722008-09-02 06:52:38 +00001228
Evan Cheng380482a2008-09-13 01:44:01 +00001229 // Set bit U(23) according to sign of immed value (positive or negative)
Evan Chengfd2adbf2008-11-05 23:22:34 +00001230 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
Evan Cheng3be5b722008-09-02 06:52:38 +00001231 ARMII::U_BitShift);
1232
1233 // If this instr is in register offset/index encoding, set bit[3:0]
1234 // to the corresponding Rm register.
1235 if (MO2.getReg()) {
Eric Christopher6ac277c2012-08-09 22:10:21 +00001236 Binary |= II->getRegisterInfo().getEncodingValue(MO2.getReg());
Evan Chengfd2adbf2008-11-05 23:22:34 +00001237 emitWordLE(Binary);
Evan Cheng81889d012008-11-05 18:35:52 +00001238 return;
Evan Cheng3be5b722008-09-02 06:52:38 +00001239 }
1240
Evan Cheng47b546d2008-11-06 08:47:38 +00001241 // This instr is in immediate offset/index encoding, set bit 22 to 1.
Evan Cheng49d66522008-11-06 22:15:19 +00001242 Binary |= 1 << ARMII::AM3_I_BitShift;
Evan Chengfd2adbf2008-11-05 23:22:34 +00001243 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
Evan Cheng3be5b722008-09-02 06:52:38 +00001244 // Set operands
Evan Cheng2836d912008-11-12 07:34:37 +00001245 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
1246 Binary |= (ImmOffs & 0xF); // immedL
Evan Cheng3be5b722008-09-02 06:52:38 +00001247 }
1248
Evan Chengfd2adbf2008-11-05 23:22:34 +00001249 emitWordLE(Binary);
Evan Cheng3be5b722008-09-02 06:52:38 +00001250}
1251
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001252static unsigned getAddrModeUPBits(unsigned Mode) {
1253 unsigned Binary = 0;
Evan Cheng3be5b722008-09-02 06:52:38 +00001254
1255 // Set addressing mode by modifying bits U(23) and P(24)
1256 // IA - Increment after - bit U = 1 and bit P = 0
1257 // IB - Increment before - bit U = 1 and bit P = 1
1258 // DA - Decrement after - bit U = 0 and bit P = 0
1259 // DB - Decrement before - bit U = 0 and bit P = 1
Evan Cheng3be5b722008-09-02 06:52:38 +00001260 switch (Mode) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001261 default: llvm_unreachable("Unknown addressing sub-mode!");
Evan Cheng71140342009-09-09 23:55:03 +00001262 case ARM_AM::da: break;
Evan Cheng49d66522008-11-06 22:15:19 +00001263 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
1264 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
1265 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
Evan Cheng3be5b722008-09-02 06:52:38 +00001266 }
1267
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001268 return Binary;
1269}
1270
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001271void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001272 const MCInstrDesc &MCID = MI.getDesc();
1273 bool IsUpdating = (MCID.TSFlags & ARMII::IndexModeMask) != 0;
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001274
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001275 // Part of binary is determined by TableGn.
1276 unsigned Binary = getBinaryCodeForInstr(MI);
1277
1278 // Set the conditional execution predicate
1279 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1280
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001281 // Skip operand 0 of an instruction with base register update.
1282 unsigned OpIdx = 0;
1283 if (IsUpdating)
1284 ++OpIdx;
1285
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001286 // Set base address operand
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001287 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001288
1289 // Set addressing mode by modifying bits U(23) and P(24)
Bill Wendlingb100f912010-11-17 05:31:09 +00001290 ARM_AM::AMSubMode Mode = ARM_AM::getLoadStoreMultipleSubMode(MI.getOpcode());
1291 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(Mode));
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001292
Evan Cheng3be5b722008-09-02 06:52:38 +00001293 // Set bit W(21)
Bob Wilsond6243b42010-03-16 17:46:45 +00001294 if (IsUpdating)
Evan Cheng49d66522008-11-06 22:15:19 +00001295 Binary |= 0x1 << ARMII::W_BitShift;
Evan Cheng3be5b722008-09-02 06:52:38 +00001296
1297 // Set registers
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001298 for (unsigned i = OpIdx+2, e = MI.getNumOperands(); i != e; ++i) {
Evan Cheng3be5b722008-09-02 06:52:38 +00001299 const MachineOperand &MO = MI.getOperand(i);
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001300 if (!MO.isReg() || MO.isImplicit())
1301 break;
Eric Christopher6ac277c2012-08-09 22:10:21 +00001302 unsigned RegNum = II->getRegisterInfo().getEncodingValue(MO.getReg());
Evan Cheng3be5b722008-09-02 06:52:38 +00001303 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
1304 RegNum < 16);
1305 Binary |= 0x1 << RegNum;
1306 }
1307
Evan Chengfd2adbf2008-11-05 23:22:34 +00001308 emitWordLE(Binary);
Evan Cheng3be5b722008-09-02 06:52:38 +00001309}
1310
Chris Lattner8d806872010-02-02 21:48:51 +00001311void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001312 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng81889d012008-11-05 18:35:52 +00001313
1314 // Part of binary is determined by TableGn.
1315 unsigned Binary = getBinaryCodeForInstr(MI);
1316
Jim Grosbach4d0549e2008-11-03 18:38:31 +00001317 // Set the conditional execution predicate
Evan Cheng49d66522008-11-06 22:15:19 +00001318 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Jim Grosbach4d0549e2008-11-03 18:38:31 +00001319
1320 // Encode S bit if MI modifies CPSR.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001321 Binary |= getAddrModeSBit(MI, MCID);
Jim Grosbach4d0549e2008-11-03 18:38:31 +00001322
1323 // 32x32->64bit operations have two destination registers. The number
1324 // of register definitions will tell us if that's what we're dealing with.
Evan Cheng49d66522008-11-06 22:15:19 +00001325 unsigned OpIdx = 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001326 if (MCID.getNumDefs() == 2)
Jim Grosbach4d0549e2008-11-03 18:38:31 +00001327 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
1328
1329 // Encode Rd
1330 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
1331
1332 // Encode Rm
1333 Binary |= getMachineOpValue(MI, OpIdx++);
1334
1335 // Encode Rs
1336 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
1337
Evan Cheng2686c8f2008-11-06 01:21:28 +00001338 // Many multiple instructions (e.g. MLA) have three src operands. Encode
1339 // it as Rn (for multiply, that's in the same offset as RdLo.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001340 if (MCID.getNumOperands() > OpIdx &&
1341 !MCID.OpInfo[OpIdx].isPredicate() &&
1342 !MCID.OpInfo[OpIdx].isOptionalDef())
Evan Cheng49d66522008-11-06 22:15:19 +00001343 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
1344
1345 emitWordLE(Binary);
1346}
1347
Chris Lattner8d806872010-02-02 21:48:51 +00001348void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001349 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng49d66522008-11-06 22:15:19 +00001350
1351 // Part of binary is determined by TableGn.
1352 unsigned Binary = getBinaryCodeForInstr(MI);
1353
1354 // Set the conditional execution predicate
1355 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1356
1357 unsigned OpIdx = 0;
1358
1359 // Encode Rd
1360 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1361
1362 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
1363 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1364 if (MO2.isReg()) {
1365 // Two register operand form.
1366 // Encode Rn.
1367 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
1368
1369 // Encode Rm.
1370 Binary |= getMachineOpValue(MI, MO2);
1371 ++OpIdx;
1372 } else {
1373 Binary |= getMachineOpValue(MI, MO1);
1374 }
1375
1376 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
1377 if (MI.getOperand(OpIdx).isImm() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001378 !MCID.OpInfo[OpIdx].isPredicate() &&
1379 !MCID.OpInfo[OpIdx].isOptionalDef())
Evan Cheng49d66522008-11-06 22:15:19 +00001380 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
Evan Cheng2686c8f2008-11-06 01:21:28 +00001381
Evan Chengfd2adbf2008-11-05 23:22:34 +00001382 emitWordLE(Binary);
Jim Grosbach4d0549e2008-11-03 18:38:31 +00001383}
1384
Chris Lattner8d806872010-02-02 21:48:51 +00001385void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001386 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng98dc53e2008-11-07 01:41:35 +00001387
1388 // Part of binary is determined by TableGn.
1389 unsigned Binary = getBinaryCodeForInstr(MI);
1390
1391 // Set the conditional execution predicate
1392 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1393
Eric Christopher1e3db022011-05-07 04:37:27 +00001394 // PKH instructions are finished at this point
Evan Cheng6cc775f2011-06-28 19:10:37 +00001395 if (MCID.Opcode == ARM::PKHBT || MCID.Opcode == ARM::PKHTB) {
Eric Christopher1e3db022011-05-07 04:37:27 +00001396 emitWordLE(Binary);
1397 return;
1398 }
1399
Evan Cheng98dc53e2008-11-07 01:41:35 +00001400 unsigned OpIdx = 0;
1401
1402 // Encode Rd
1403 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1404
1405 const MachineOperand &MO = MI.getOperand(OpIdx++);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001406 if (OpIdx == MCID.getNumOperands() ||
1407 MCID.OpInfo[OpIdx].isPredicate() ||
1408 MCID.OpInfo[OpIdx].isOptionalDef()) {
Evan Cheng98dc53e2008-11-07 01:41:35 +00001409 // Encode Rm and it's done.
1410 Binary |= getMachineOpValue(MI, MO);
1411 emitWordLE(Binary);
1412 return;
1413 }
1414
1415 // Encode Rn.
1416 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1417
1418 // Encode Rm.
1419 Binary |= getMachineOpValue(MI, OpIdx++);
1420
1421 // Encode shift_imm.
1422 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001423 if (MCID.Opcode == ARM::PKHTB) {
Bob Wilson942b10f2010-08-17 17:23:19 +00001424 assert(ShiftAmt != 0 && "PKHTB shift_imm is 0!");
1425 if (ShiftAmt == 32)
1426 ShiftAmt = 0;
1427 }
Evan Cheng98dc53e2008-11-07 01:41:35 +00001428 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1429 Binary |= ShiftAmt << ARMII::ShiftShift;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001430
Evan Cheng98dc53e2008-11-07 01:41:35 +00001431 emitWordLE(Binary);
1432}
1433
Bob Wilson96649842010-08-11 00:01:18 +00001434void ARMCodeEmitter::emitSaturateInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001435 const MCInstrDesc &MCID = MI.getDesc();
Bob Wilson96649842010-08-11 00:01:18 +00001436
1437 // Part of binary is determined by TableGen.
1438 unsigned Binary = getBinaryCodeForInstr(MI);
1439
1440 // Set the conditional execution predicate
1441 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1442
1443 // Encode Rd
1444 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
1445
1446 // Encode saturate bit position.
1447 unsigned Pos = MI.getOperand(1).getImm();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001448 if (MCID.Opcode == ARM::SSAT || MCID.Opcode == ARM::SSAT16)
Bob Wilson96649842010-08-11 00:01:18 +00001449 Pos -= 1;
1450 assert((Pos < 16 || (Pos < 32 &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001451 MCID.Opcode != ARM::SSAT16 &&
1452 MCID.Opcode != ARM::USAT16)) &&
Bob Wilson96649842010-08-11 00:01:18 +00001453 "saturate bit position out of range");
1454 Binary |= Pos << 16;
1455
1456 // Encode Rm
1457 Binary |= getMachineOpValue(MI, 2);
1458
1459 // Encode shift_imm.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001460 if (MCID.getNumOperands() == 4) {
Bob Wilsonadd513112010-08-11 23:10:46 +00001461 unsigned ShiftOp = MI.getOperand(3).getImm();
1462 ARM_AM::ShiftOpc Opc = ARM_AM::getSORegShOp(ShiftOp);
1463 if (Opc == ARM_AM::asr)
1464 Binary |= (1 << 6);
Bob Wilson96649842010-08-11 00:01:18 +00001465 unsigned ShiftAmt = MI.getOperand(3).getImm();
Bob Wilsonadd513112010-08-11 23:10:46 +00001466 if (ShiftAmt == 32 && Opc == ARM_AM::asr)
Bob Wilson96649842010-08-11 00:01:18 +00001467 ShiftAmt = 0;
1468 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1469 Binary |= ShiftAmt << ARMII::ShiftShift;
1470 }
1471
1472 emitWordLE(Binary);
1473}
1474
Chris Lattner8d806872010-02-02 21:48:51 +00001475void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001476 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng81889d012008-11-05 18:35:52 +00001477
Evan Cheng6cc775f2011-06-28 19:10:37 +00001478 if (MCID.Opcode == ARM::TPsoft) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001479 llvm_unreachable("ARM::TPsoft FIXME"); // FIXME
Torok Edwinfb8d6d52009-07-08 20:53:28 +00001480 }
Evan Chengaa03cd32008-11-06 17:48:05 +00001481
Evan Cheng3be5b722008-09-02 06:52:38 +00001482 // Part of binary is determined by TableGn.
1483 unsigned Binary = getBinaryCodeForInstr(MI);
1484
Evan Cheng81889d012008-11-05 18:35:52 +00001485 // Set the conditional execution predicate
Evan Cheng49d66522008-11-06 22:15:19 +00001486 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng81889d012008-11-05 18:35:52 +00001487
1488 // Set signed_immed_24 field
1489 Binary |= getMachineOpValue(MI, 0);
1490
Evan Chengfd2adbf2008-11-05 23:22:34 +00001491 emitWordLE(Binary);
Evan Cheng81889d012008-11-05 18:35:52 +00001492}
1493
Chris Lattner8d806872010-02-02 21:48:51 +00001494void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
Evan Cheng7095cd22008-11-07 09:06:08 +00001495 // Remember the base address of the inline jump table.
Evan Cheng0b773192008-12-10 02:32:19 +00001496 uintptr_t JTBase = MCE.getCurrentPCValue();
Evan Cheng8467e242008-11-07 22:30:53 +00001497 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
Chris Lattneraf29ea62009-08-23 06:49:22 +00001498 DEBUG(errs() << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase
1499 << '\n');
Evan Cheng7095cd22008-11-07 09:06:08 +00001500
1501 // Now emit the jump table entries.
1502 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1503 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1504 if (IsPIC)
1505 // DestBB address - JT base.
Evan Cheng8467e242008-11-07 22:30:53 +00001506 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
Evan Cheng7095cd22008-11-07 09:06:08 +00001507 else
1508 // Absolute DestBB address.
1509 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1510 emitWordLE(0);
1511 }
1512}
1513
Chris Lattner8d806872010-02-02 21:48:51 +00001514void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001515 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng81889d012008-11-05 18:35:52 +00001516
Evan Cheng8467e242008-11-07 22:30:53 +00001517 // Handle jump tables.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001518 if (MCID.Opcode == ARM::BR_JTr || MCID.Opcode == ARM::BR_JTadd) {
Evan Cheng8467e242008-11-07 22:30:53 +00001519 // First emit a ldr pc, [] instruction.
1520 emitDataProcessingInstruction(MI, ARM::PC);
1521
1522 // Then emit the inline jump table.
Evan Chengb61e3a82009-07-08 00:05:05 +00001523 unsigned JTIndex =
Evan Cheng6cc775f2011-06-28 19:10:37 +00001524 (MCID.Opcode == ARM::BR_JTr)
Evan Cheng8467e242008-11-07 22:30:53 +00001525 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1526 emitInlineJumpTable(JTIndex);
1527 return;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001528 } else if (MCID.Opcode == ARM::BR_JTm) {
Evan Cheng7095cd22008-11-07 09:06:08 +00001529 // First emit a ldr pc, [] instruction.
1530 emitLoadStoreInstruction(MI, ARM::PC);
1531
1532 // Then emit the inline jump table.
Evan Cheng8467e242008-11-07 22:30:53 +00001533 emitInlineJumpTable(MI.getOperand(3).getIndex());
Evan Cheng7095cd22008-11-07 09:06:08 +00001534 return;
1535 }
1536
Evan Cheng81889d012008-11-05 18:35:52 +00001537 // Part of binary is determined by TableGn.
1538 unsigned Binary = getBinaryCodeForInstr(MI);
1539
1540 // Set the conditional execution predicate
Evan Cheng49d66522008-11-06 22:15:19 +00001541 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng81889d012008-11-05 18:35:52 +00001542
Evan Cheng6cc775f2011-06-28 19:10:37 +00001543 if (MCID.Opcode == ARM::BX_RET || MCID.Opcode == ARM::MOVPCLR)
Evan Cheng81889d012008-11-05 18:35:52 +00001544 // The return register is LR.
Eric Christopher6ac277c2012-08-09 22:10:21 +00001545 Binary |= II->getRegisterInfo().getEncodingValue(ARM::LR);
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001546 else
Evan Cheng81889d012008-11-05 18:35:52 +00001547 // otherwise, set the return register
1548 Binary |= getMachineOpValue(MI, 0);
1549
Evan Chengfd2adbf2008-11-05 23:22:34 +00001550 emitWordLE(Binary);
Evan Cheng9546a5c2007-07-05 21:15:40 +00001551}
Evan Cheng3be5b722008-09-02 06:52:38 +00001552
Eric Christopher6ac277c2012-08-09 22:10:21 +00001553unsigned ARMCodeEmitter::encodeVFPRd(const MachineInstr &MI,
1554 unsigned OpIdx) const {
Evan Chenga0e2f262008-11-12 02:19:38 +00001555 unsigned RegD = MI.getOperand(OpIdx).getReg();
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001556 unsigned Binary = 0;
Craig Topperc7242e02012-04-20 07:30:17 +00001557 bool isSPVFP = ARM::SPRRegClass.contains(RegD);
Eric Christopher6ac277c2012-08-09 22:10:21 +00001558 RegD = II->getRegisterInfo().getEncodingValue(RegD);
Evan Chenga0e2f262008-11-12 02:19:38 +00001559 if (!isSPVFP)
1560 Binary |= RegD << ARMII::RegRdShift;
1561 else {
1562 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1563 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1564 }
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001565 return Binary;
1566}
Evan Cheng38c9a142008-11-11 19:40:26 +00001567
Eric Christopher6ac277c2012-08-09 22:10:21 +00001568unsigned ARMCodeEmitter::encodeVFPRn(const MachineInstr &MI,
1569 unsigned OpIdx) const {
Evan Chenga0e2f262008-11-12 02:19:38 +00001570 unsigned RegN = MI.getOperand(OpIdx).getReg();
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001571 unsigned Binary = 0;
Craig Topperc7242e02012-04-20 07:30:17 +00001572 bool isSPVFP = ARM::SPRRegClass.contains(RegN);
Eric Christopher6ac277c2012-08-09 22:10:21 +00001573 RegN = II->getRegisterInfo().getEncodingValue(RegN);
Evan Chenga0e2f262008-11-12 02:19:38 +00001574 if (!isSPVFP)
1575 Binary |= RegN << ARMII::RegRnShift;
1576 else {
1577 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1578 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1579 }
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001580 return Binary;
1581}
Evan Chenga0e2f262008-11-12 02:19:38 +00001582
Eric Christopher6ac277c2012-08-09 22:10:21 +00001583unsigned ARMCodeEmitter::encodeVFPRm(const MachineInstr &MI,
1584 unsigned OpIdx) const {
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001585 unsigned RegM = MI.getOperand(OpIdx).getReg();
1586 unsigned Binary = 0;
Craig Topperc7242e02012-04-20 07:30:17 +00001587 bool isSPVFP = ARM::SPRRegClass.contains(RegM);
Eric Christopher6ac277c2012-08-09 22:10:21 +00001588 RegM = II->getRegisterInfo().getEncodingValue(RegM);
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001589 if (!isSPVFP)
1590 Binary |= RegM;
1591 else {
1592 Binary |= ((RegM & 0x1E) >> 1);
1593 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
Evan Cheng38c9a142008-11-11 19:40:26 +00001594 }
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001595 return Binary;
1596}
1597
Chris Lattner8d806872010-02-02 21:48:51 +00001598void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001599 const MCInstrDesc &MCID = MI.getDesc();
Evan Chengaf644b52008-11-12 07:18:38 +00001600
1601 // Part of binary is determined by TableGn.
1602 unsigned Binary = getBinaryCodeForInstr(MI);
1603
1604 // Set the conditional execution predicate
1605 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1606
1607 unsigned OpIdx = 0;
1608 assert((Binary & ARMII::D_BitShift) == 0 &&
1609 (Binary & ARMII::N_BitShift) == 0 &&
1610 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1611
1612 // Encode Dd / Sd.
1613 Binary |= encodeVFPRd(MI, OpIdx++);
1614
1615 // If this is a two-address operand, skip it, e.g. FMACD.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001616 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Evan Chengaf644b52008-11-12 07:18:38 +00001617 ++OpIdx;
1618
1619 // Encode Dn / Sn.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001620 if ((MCID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
Evan Cheng052f20d2008-11-12 08:14:21 +00001621 Binary |= encodeVFPRn(MI, OpIdx++);
Evan Chengaf644b52008-11-12 07:18:38 +00001622
Evan Cheng6cc775f2011-06-28 19:10:37 +00001623 if (OpIdx == MCID.getNumOperands() ||
1624 MCID.OpInfo[OpIdx].isPredicate() ||
1625 MCID.OpInfo[OpIdx].isOptionalDef()) {
Evan Chengaf644b52008-11-12 07:18:38 +00001626 // FCMPEZD etc. has only one operand.
1627 emitWordLE(Binary);
1628 return;
1629 }
1630
1631 // Encode Dm / Sm.
1632 Binary |= encodeVFPRm(MI, OpIdx);
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001633
Evan Chengaf644b52008-11-12 07:18:38 +00001634 emitWordLE(Binary);
1635}
1636
Bob Wilsona6fe21a2010-03-17 21:16:45 +00001637void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001638 const MCInstrDesc &MCID = MI.getDesc();
1639 unsigned Form = MCID.TSFlags & ARMII::FormMask;
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001640
1641 // Part of binary is determined by TableGn.
1642 unsigned Binary = getBinaryCodeForInstr(MI);
1643
1644 // Set the conditional execution predicate
1645 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1646
1647 switch (Form) {
1648 default: break;
1649 case ARMII::VFPConv1Frm:
1650 case ARMII::VFPConv2Frm:
1651 case ARMII::VFPConv3Frm:
1652 // Encode Dd / Sd.
1653 Binary |= encodeVFPRd(MI, 0);
1654 break;
1655 case ARMII::VFPConv4Frm:
1656 // Encode Dn / Sn.
1657 Binary |= encodeVFPRn(MI, 0);
1658 break;
1659 case ARMII::VFPConv5Frm:
1660 // Encode Dm / Sm.
1661 Binary |= encodeVFPRm(MI, 0);
1662 break;
1663 }
1664
1665 switch (Form) {
1666 default: break;
1667 case ARMII::VFPConv1Frm:
1668 // Encode Dm / Sm.
1669 Binary |= encodeVFPRm(MI, 1);
Evan Cheng4af89f72008-11-13 07:46:59 +00001670 break;
Evan Cheng4b6c7ef2008-11-12 06:41:41 +00001671 case ARMII::VFPConv2Frm:
1672 case ARMII::VFPConv3Frm:
1673 // Encode Dn / Sn.
1674 Binary |= encodeVFPRn(MI, 1);
1675 break;
1676 case ARMII::VFPConv4Frm:
1677 case ARMII::VFPConv5Frm:
1678 // Encode Dd / Sd.
1679 Binary |= encodeVFPRd(MI, 1);
1680 break;
1681 }
1682
1683 if (Form == ARMII::VFPConv5Frm)
1684 // Encode Dn / Sn.
1685 Binary |= encodeVFPRn(MI, 2);
1686 else if (Form == ARMII::VFPConv3Frm)
1687 // Encode Dm / Sm.
1688 Binary |= encodeVFPRm(MI, 2);
Evan Cheng38c9a142008-11-11 19:40:26 +00001689
1690 emitWordLE(Binary);
1691}
1692
Chris Lattner8d806872010-02-02 21:48:51 +00001693void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001694 // Part of binary is determined by TableGn.
1695 unsigned Binary = getBinaryCodeForInstr(MI);
1696
1697 // Set the conditional execution predicate
1698 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1699
1700 unsigned OpIdx = 0;
1701
1702 // Encode Dd / Sd.
Evan Chengaf644b52008-11-12 07:18:38 +00001703 Binary |= encodeVFPRd(MI, OpIdx++);
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001704
1705 // Encode address base.
1706 const MachineOperand &Base = MI.getOperand(OpIdx++);
1707 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1708
1709 // If there is a non-zero immediate offset, encode it.
1710 if (Base.isReg()) {
1711 const MachineOperand &Offset = MI.getOperand(OpIdx);
1712 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1713 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1714 Binary |= 1 << ARMII::U_BitShift;
Evan Cheng45d030a2008-11-12 08:21:12 +00001715 Binary |= ImmOffs;
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001716 emitWordLE(Binary);
1717 return;
1718 }
1719 }
1720
1721 // If immediate offset is omitted, default to +0.
1722 Binary |= 1 << ARMII::U_BitShift;
1723
1724 emitWordLE(Binary);
1725}
1726
Bob Wilsona6fe21a2010-03-17 21:16:45 +00001727void
1728ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001729 const MCInstrDesc &MCID = MI.getDesc();
1730 bool IsUpdating = (MCID.TSFlags & ARMII::IndexModeMask) != 0;
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001731
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001732 // Part of binary is determined by TableGn.
1733 unsigned Binary = getBinaryCodeForInstr(MI);
1734
1735 // Set the conditional execution predicate
1736 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1737
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001738 // Skip operand 0 of an instruction with base register update.
1739 unsigned OpIdx = 0;
1740 if (IsUpdating)
1741 ++OpIdx;
1742
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001743 // Set base address operand
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001744 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001745
1746 // Set addressing mode by modifying bits U(23) and P(24)
Bill Wendlingb100f912010-11-17 05:31:09 +00001747 ARM_AM::AMSubMode Mode = ARM_AM::getLoadStoreMultipleSubMode(MI.getOpcode());
1748 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(Mode));
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001749
1750 // Set bit W(21)
Bob Wilson466d1e32010-03-16 18:38:09 +00001751 if (IsUpdating)
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001752 Binary |= 0x1 << ARMII::W_BitShift;
1753
1754 // First register is encoded in Dd.
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001755 Binary |= encodeVFPRd(MI, OpIdx+2);
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001756
Bob Wilson13ce07f2010-08-27 23:18:17 +00001757 // Count the number of registers.
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001758 unsigned NumRegs = 1;
Bob Wilsonf1e8f7f2010-03-13 07:34:35 +00001759 for (unsigned i = OpIdx+3, e = MI.getNumOperands(); i != e; ++i) {
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001760 const MachineOperand &MO = MI.getOperand(i);
1761 if (!MO.isReg() || MO.isImplicit())
1762 break;
1763 ++NumRegs;
1764 }
Shih-wei Liaoe22abfa2010-05-26 00:02:28 +00001765 // Bit 8 will be set if <list> is consecutive 64-bit registers (e.g., D0)
1766 // Otherwise, it will be 0, in the case of 32-bit registers.
1767 if(Binary & 0x100)
1768 Binary |= NumRegs * 2;
1769 else
1770 Binary |= NumRegs;
Evan Cheng8cbbcb12008-11-11 21:48:44 +00001771
1772 emitWordLE(Binary);
1773}
1774
Eric Christopher6ac277c2012-08-09 22:10:21 +00001775unsigned ARMCodeEmitter::encodeNEONRd(const MachineInstr &MI,
1776 unsigned OpIdx) const {
Bob Wilson6eae5202010-06-11 21:34:50 +00001777 unsigned RegD = MI.getOperand(OpIdx).getReg();
1778 unsigned Binary = 0;
Eric Christopher6ac277c2012-08-09 22:10:21 +00001779 RegD = II->getRegisterInfo().getEncodingValue(RegD);
Bob Wilson6eae5202010-06-11 21:34:50 +00001780 Binary |= (RegD & 0xf) << ARMII::RegRdShift;
1781 Binary |= ((RegD >> 4) & 1) << ARMII::D_BitShift;
1782 return Binary;
1783}
1784
Eric Christopher6ac277c2012-08-09 22:10:21 +00001785unsigned ARMCodeEmitter::encodeNEONRn(const MachineInstr &MI,
1786 unsigned OpIdx) const {
Bob Wilson2530ca02010-06-25 22:40:46 +00001787 unsigned RegN = MI.getOperand(OpIdx).getReg();
1788 unsigned Binary = 0;
Eric Christopher6ac277c2012-08-09 22:10:21 +00001789 RegN = II->getRegisterInfo().getEncodingValue(RegN);
Bob Wilson2530ca02010-06-25 22:40:46 +00001790 Binary |= (RegN & 0xf) << ARMII::RegRnShift;
1791 Binary |= ((RegN >> 4) & 1) << ARMII::N_BitShift;
1792 return Binary;
1793}
1794
Eric Christopher6ac277c2012-08-09 22:10:21 +00001795unsigned ARMCodeEmitter::encodeNEONRm(const MachineInstr &MI,
1796 unsigned OpIdx) const {
Bob Wilsone70c8b12010-06-25 21:17:19 +00001797 unsigned RegM = MI.getOperand(OpIdx).getReg();
1798 unsigned Binary = 0;
Eric Christopher6ac277c2012-08-09 22:10:21 +00001799 RegM = II->getRegisterInfo().getEncodingValue(RegM);
Bob Wilsone70c8b12010-06-25 21:17:19 +00001800 Binary |= (RegM & 0xf);
1801 Binary |= ((RegM >> 4) & 1) << ARMII::M_BitShift;
1802 return Binary;
1803}
1804
Bob Wilson584387d2010-06-28 21:12:19 +00001805/// convertNEONDataProcToThumb - Convert the ARM mode encoding for a NEON
1806/// data-processing instruction to the corresponding Thumb encoding.
1807static unsigned convertNEONDataProcToThumb(unsigned Binary) {
1808 assert((Binary & 0xfe000000) == 0xf2000000 &&
1809 "not an ARM NEON data-processing instruction");
1810 unsigned UBit = (Binary >> 24) & 1;
1811 return 0xef000000 | (UBit << 28) | (Binary & 0xffffff);
1812}
1813
Bob Wilsonab0819e2010-06-29 17:34:07 +00001814void ARMCodeEmitter::emitNEONLaneInstruction(const MachineInstr &MI) {
Bob Wilson0248da92010-06-26 04:07:15 +00001815 unsigned Binary = getBinaryCodeForInstr(MI);
1816
Bob Wilsonab0819e2010-06-29 17:34:07 +00001817 unsigned RegTOpIdx, RegNOpIdx, LnOpIdx;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001818 const MCInstrDesc &MCID = MI.getDesc();
1819 if ((MCID.TSFlags & ARMII::FormMask) == ARMII::NGetLnFrm) {
Bob Wilsonab0819e2010-06-29 17:34:07 +00001820 RegTOpIdx = 0;
1821 RegNOpIdx = 1;
1822 LnOpIdx = 2;
1823 } else { // ARMII::NSetLnFrm
1824 RegTOpIdx = 2;
1825 RegNOpIdx = 0;
1826 LnOpIdx = 3;
1827 }
1828
Bob Wilson0248da92010-06-26 04:07:15 +00001829 // Set the conditional execution predicate
Bob Wilson3d12ff72010-06-29 00:26:13 +00001830 Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift;
Bob Wilson0248da92010-06-26 04:07:15 +00001831
Bob Wilsonab0819e2010-06-29 17:34:07 +00001832 unsigned RegT = MI.getOperand(RegTOpIdx).getReg();
Eric Christopher6ac277c2012-08-09 22:10:21 +00001833 RegT = II->getRegisterInfo().getEncodingValue(RegT);
Bob Wilson0248da92010-06-26 04:07:15 +00001834 Binary |= (RegT << ARMII::RegRdShift);
Bob Wilsonab0819e2010-06-29 17:34:07 +00001835 Binary |= encodeNEONRn(MI, RegNOpIdx);
Bob Wilson0248da92010-06-26 04:07:15 +00001836
1837 unsigned LaneShift;
1838 if ((Binary & (1 << 22)) != 0)
1839 LaneShift = 0; // 8-bit elements
1840 else if ((Binary & (1 << 5)) != 0)
1841 LaneShift = 1; // 16-bit elements
1842 else
1843 LaneShift = 2; // 32-bit elements
1844
Bob Wilsonab0819e2010-06-29 17:34:07 +00001845 unsigned Lane = MI.getOperand(LnOpIdx).getImm() << LaneShift;
Bob Wilson0248da92010-06-26 04:07:15 +00001846 unsigned Opc1 = Lane >> 2;
1847 unsigned Opc2 = Lane & 3;
1848 assert((Opc1 & 3) == 0 && "out-of-range lane number operand");
1849 Binary |= (Opc1 << 21);
1850 Binary |= (Opc2 << 5);
1851
1852 emitWordLE(Binary);
1853}
1854
Bob Wilsonbe157b02010-06-29 20:13:29 +00001855void ARMCodeEmitter::emitNEONDupInstruction(const MachineInstr &MI) {
1856 unsigned Binary = getBinaryCodeForInstr(MI);
1857
1858 // Set the conditional execution predicate
1859 Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift;
1860
1861 unsigned RegT = MI.getOperand(1).getReg();
Eric Christopher6ac277c2012-08-09 22:10:21 +00001862 RegT = II->getRegisterInfo().getEncodingValue(RegT);
Bob Wilsonbe157b02010-06-29 20:13:29 +00001863 Binary |= (RegT << ARMII::RegRdShift);
1864 Binary |= encodeNEONRn(MI, 0);
1865 emitWordLE(Binary);
1866}
1867
Bob Wilsone70c8b12010-06-25 21:17:19 +00001868void ARMCodeEmitter::emitNEON1RegModImmInstruction(const MachineInstr &MI) {
Bob Wilson6eae5202010-06-11 21:34:50 +00001869 unsigned Binary = getBinaryCodeForInstr(MI);
1870 // Destination register is encoded in Dd.
1871 Binary |= encodeNEONRd(MI, 0);
1872 // Immediate fields: Op, Cmode, I, Imm3, Imm4
1873 unsigned Imm = MI.getOperand(1).getImm();
1874 unsigned Op = (Imm >> 12) & 1;
Bob Wilson6eae5202010-06-11 21:34:50 +00001875 unsigned Cmode = (Imm >> 8) & 0xf;
Bob Wilson6eae5202010-06-11 21:34:50 +00001876 unsigned I = (Imm >> 7) & 1;
Bob Wilson6eae5202010-06-11 21:34:50 +00001877 unsigned Imm3 = (Imm >> 4) & 0x7;
Bob Wilson6eae5202010-06-11 21:34:50 +00001878 unsigned Imm4 = Imm & 0xf;
Bob Wilson544317d2010-06-28 21:16:30 +00001879 Binary |= (I << 24) | (Imm3 << 16) | (Cmode << 8) | (Op << 5) | Imm4;
Bob Wilson4469a892010-06-28 22:23:17 +00001880 if (IsThumb)
Bob Wilson584387d2010-06-28 21:12:19 +00001881 Binary = convertNEONDataProcToThumb(Binary);
Bob Wilson6eae5202010-06-11 21:34:50 +00001882 emitWordLE(Binary);
1883}
1884
Bob Wilsone70c8b12010-06-25 21:17:19 +00001885void ARMCodeEmitter::emitNEON2RegInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001886 const MCInstrDesc &MCID = MI.getDesc();
Bob Wilsone70c8b12010-06-25 21:17:19 +00001887 unsigned Binary = getBinaryCodeForInstr(MI);
Bob Wilson2530ca02010-06-25 22:40:46 +00001888 // Destination register is encoded in Dd; source register in Dm.
1889 unsigned OpIdx = 0;
1890 Binary |= encodeNEONRd(MI, OpIdx++);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001891 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Bob Wilson2530ca02010-06-25 22:40:46 +00001892 ++OpIdx;
1893 Binary |= encodeNEONRm(MI, OpIdx);
Bob Wilson4469a892010-06-28 22:23:17 +00001894 if (IsThumb)
Bob Wilson584387d2010-06-28 21:12:19 +00001895 Binary = convertNEONDataProcToThumb(Binary);
Bob Wilsone70c8b12010-06-25 21:17:19 +00001896 // FIXME: This does not handle VDUPfdf or VDUPfqf.
1897 emitWordLE(Binary);
1898}
1899
Bob Wilson2530ca02010-06-25 22:40:46 +00001900void ARMCodeEmitter::emitNEON3RegInstruction(const MachineInstr &MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001901 const MCInstrDesc &MCID = MI.getDesc();
Bob Wilson2530ca02010-06-25 22:40:46 +00001902 unsigned Binary = getBinaryCodeForInstr(MI);
1903 // Destination register is encoded in Dd; source registers in Dn and Dm.
1904 unsigned OpIdx = 0;
1905 Binary |= encodeNEONRd(MI, OpIdx++);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001906 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Bob Wilson2530ca02010-06-25 22:40:46 +00001907 ++OpIdx;
1908 Binary |= encodeNEONRn(MI, OpIdx++);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001909 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Bob Wilson2530ca02010-06-25 22:40:46 +00001910 ++OpIdx;
1911 Binary |= encodeNEONRm(MI, OpIdx);
Bob Wilson4469a892010-06-28 22:23:17 +00001912 if (IsThumb)
Bob Wilson584387d2010-06-28 21:12:19 +00001913 Binary = convertNEONDataProcToThumb(Binary);
Bob Wilson2530ca02010-06-25 22:40:46 +00001914 // FIXME: This does not handle VMOVDneon or VMOVQ.
1915 emitWordLE(Binary);
1916}
1917
Evan Cheng3be5b722008-09-02 06:52:38 +00001918#include "ARMGenCodeEmitter.inc"