blob: e44f2a0d3bb969ad27ac949b8f196a69e038a483 [file] [log] [blame]
Jim Grosbach2cee75a2010-10-08 17:28:40 +00001//===-- ARM/ARMCodeEmitter.cpp - Convert ARM code to machine code ---------===//
Evan Cheng148b6a42007-07-05 21:15:40 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Cheng148b6a42007-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 Cheng0f282432008-10-29 23:55:43 +000015#define DEBUG_TYPE "jit"
Evan Cheng7602e112008-09-02 06:52:38 +000016#include "ARM.h"
Craig Topperacf20772012-03-25 23:49:58 +000017#include "ARMBaseInstrInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "ARMConstantPoolValue.h"
Evan Cheng7602e112008-09-02 06:52:38 +000019#include "ARMRelocations.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000022#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/ADT/Statistic.h"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000024#include "llvm/CodeGen/JITCodeEmitter.h"
Evan Cheng057d0c32008-09-18 07:28:19 +000025#include "llvm/CodeGen/MachineConstantPool.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng4df60f52008-11-07 09:06:08 +000028#include "llvm/CodeGen/MachineJumpTableInfo.h"
Daniel Dunbar003de662009-09-21 05:58:35 +000029#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000030#include "llvm/CodeGen/Passes.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Constants.h"
32#include "llvm/DerivedTypes.h"
33#include "llvm/Function.h"
34#include "llvm/PassManager.h"
Evan Cheng42d5ee062008-09-13 01:15:21 +000035#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000036#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/raw_ostream.h"
Evan Cheng4df60f52008-11-07 09:06:08 +000038#ifndef NDEBUG
39#include <iomanip>
40#endif
Evan Cheng148b6a42007-07-05 21:15:40 +000041using namespace llvm;
42
43STATISTIC(NumEmitted, "Number of machine instructions emitted");
44
45namespace {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000046
Chris Lattner33fabd72010-02-02 21:48:51 +000047 class ARMCodeEmitter : public MachineFunctionPass {
Evan Cheng057d0c32008-09-18 07:28:19 +000048 ARMJITInfo *JTI;
Craig Topperacf20772012-03-25 23:49:58 +000049 const ARMBaseInstrInfo *II;
Micah Villmow3574eca2012-10-08 16:38:25 +000050 const DataLayout *TD;
Evan Cheng08669742009-09-10 01:23:53 +000051 const ARMSubtarget *Subtarget;
Evan Cheng057d0c32008-09-18 07:28:19 +000052 TargetMachine &TM;
Chris Lattner33fabd72010-02-02 21:48:51 +000053 JITCodeEmitter &MCE;
Chris Lattner16112732010-03-14 01:41:15 +000054 MachineModuleInfo *MMI;
Evan Cheng938b9d82008-10-31 19:55:13 +000055 const std::vector<MachineConstantPoolEntry> *MCPEs;
Evan Cheng4df60f52008-11-07 09:06:08 +000056 const std::vector<MachineJumpTableEntry> *MJTEs;
57 bool IsPIC;
Bob Wilson62d24a42010-06-28 22:23:17 +000058 bool IsThumb;
Bob Wilson87949d42010-03-17 21:16:45 +000059
Daniel Dunbar003de662009-09-21 05:58:35 +000060 void getAnalysisUsage(AnalysisUsage &AU) const {
61 AU.addRequired<MachineModuleInfo>();
62 MachineFunctionPass::getAnalysisUsage(AU);
63 }
Bob Wilson87949d42010-03-17 21:16:45 +000064
Evan Cheng148b6a42007-07-05 21:15:40 +000065 static char ID;
Chris Lattner33fabd72010-02-02 21:48:51 +000066 public:
67 ARMCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
Owen Anderson90c579d2010-08-06 18:33:48 +000068 : MachineFunctionPass(ID), JTI(0),
Craig Topperacf20772012-03-25 23:49:58 +000069 II((const ARMBaseInstrInfo *)tm.getInstrInfo()),
Micah Villmow3574eca2012-10-08 16:38:25 +000070 TD(tm.getDataLayout()), TM(tm),
Bob Wilson62d24a42010-06-28 22:23:17 +000071 MCE(mce), MCPEs(0), MJTEs(0),
72 IsPIC(TM.getRelocationModel() == Reloc::PIC_), IsThumb(false) {}
Bob Wilson87949d42010-03-17 21:16:45 +000073
Chris Lattner33fabd72010-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 Anderson4f8dc7b2012-01-24 18:37:29 +000077 uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const;
Evan Cheng148b6a42007-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 Cheng7602e112008-09-02 06:52:38 +000086
87 private:
Evan Cheng057d0c32008-09-18 07:28:19 +000088
Evan Cheng83b5cf02008-11-05 23:22:34 +000089 void emitWordLE(unsigned Binary);
Evan Chengcb5201f2008-11-11 22:19:31 +000090 void emitDWordLE(uint64_t Binary);
Evan Cheng057d0c32008-09-18 07:28:19 +000091 void emitConstPoolInstruction(const MachineInstr &MI);
Zonr Changf86399b2010-05-25 08:42:45 +000092 void emitMOVi32immInstruction(const MachineInstr &MI);
Evan Cheng90922132008-11-06 02:25:39 +000093 void emitMOVi2piecesInstruction(const MachineInstr &MI);
Evan Cheng4df60f52008-11-07 09:06:08 +000094 void emitLEApcrelJTInstruction(const MachineInstr &MI);
Evan Chenga9562552008-11-14 20:09:11 +000095 void emitPseudoMoveInstruction(const MachineInstr &MI);
Evan Cheng83b5cf02008-11-05 23:22:34 +000096 void addPCLabel(unsigned LabelID);
Evan Cheng057d0c32008-09-18 07:28:19 +000097 void emitPseudoInstruction(const MachineInstr &MI);
Evan Cheng5f1db7b2008-09-12 22:01:15 +000098 unsigned getMachineSoRegOpValue(const MachineInstr &MI,
Evan Chenge837dea2011-06-28 19:10:37 +000099 const MCInstrDesc &MCID,
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000100 const MachineOperand &MO,
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000101 unsigned OpIdx);
102
Evan Cheng90922132008-11-06 02:25:39 +0000103 unsigned getMachineSoImmOpValue(unsigned SoImm);
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +0000104 unsigned getAddrModeSBit(const MachineInstr &MI,
Evan Chenge837dea2011-06-28 19:10:37 +0000105 const MCInstrDesc &MCID) const;
Evan Cheng49a9f292008-09-12 22:45:55 +0000106
Evan Cheng83b5cf02008-11-05 23:22:34 +0000107 void emitDataProcessingInstruction(const MachineInstr &MI,
Evan Cheng437c1732008-11-07 22:30:53 +0000108 unsigned ImplicitRd = 0,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000109 unsigned ImplicitRn = 0);
Evan Cheng7602e112008-09-02 06:52:38 +0000110
Evan Cheng83b5cf02008-11-05 23:22:34 +0000111 void emitLoadStoreInstruction(const MachineInstr &MI,
Evan Cheng4df60f52008-11-07 09:06:08 +0000112 unsigned ImplicitRd = 0,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000113 unsigned ImplicitRn = 0);
Evan Chengedda31c2008-11-05 18:35:52 +0000114
Evan Cheng83b5cf02008-11-05 23:22:34 +0000115 void emitMiscLoadStoreInstruction(const MachineInstr &MI,
116 unsigned ImplicitRn = 0);
Evan Chengedda31c2008-11-05 18:35:52 +0000117
118 void emitLoadStoreMultipleInstruction(const MachineInstr &MI);
119
Evan Chengfbc9d412008-11-06 01:21:28 +0000120 void emitMulFrmInstruction(const MachineInstr &MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000121
Evan Cheng97f48c32008-11-06 22:15:19 +0000122 void emitExtendInstruction(const MachineInstr &MI);
123
Evan Cheng8b59db32008-11-07 01:41:35 +0000124 void emitMiscArithInstruction(const MachineInstr &MI);
125
Bob Wilson9a1c1892010-08-11 00:01:18 +0000126 void emitSaturateInstruction(const MachineInstr &MI);
127
Evan Chengedda31c2008-11-05 18:35:52 +0000128 void emitBranchInstruction(const MachineInstr &MI);
129
Evan Cheng437c1732008-11-07 22:30:53 +0000130 void emitInlineJumpTable(unsigned JTIndex);
Evan Cheng4df60f52008-11-07 09:06:08 +0000131
Evan Chengedda31c2008-11-05 18:35:52 +0000132 void emitMiscBranchInstruction(const MachineInstr &MI);
Evan Cheng7602e112008-09-02 06:52:38 +0000133
Evan Cheng96581d32008-11-11 02:11:05 +0000134 void emitVFPArithInstruction(const MachineInstr &MI);
135
Evan Cheng78be83d2008-11-11 19:40:26 +0000136 void emitVFPConversionInstruction(const MachineInstr &MI);
137
Evan Chengcd8e66a2008-11-11 21:48:44 +0000138 void emitVFPLoadStoreInstruction(const MachineInstr &MI);
139
140 void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI);
141
Bob Wilsond5a563d2010-06-29 17:34:07 +0000142 void emitNEONLaneInstruction(const MachineInstr &MI);
Bob Wilson21773e72010-06-29 20:13:29 +0000143 void emitNEONDupInstruction(const MachineInstr &MI);
Bob Wilson583a2a02010-06-25 21:17:19 +0000144 void emitNEON1RegModImmInstruction(const MachineInstr &MI);
145 void emitNEON2RegInstruction(const MachineInstr &MI);
Bob Wilson5e7b6072010-06-25 22:40:46 +0000146 void emitNEON3RegInstruction(const MachineInstr &MI);
Bob Wilson1a913ed2010-06-11 21:34:50 +0000147
Evan Cheng7602e112008-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 Grosbach3e094132010-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 Cheng7602e112008-09-02 06:52:38 +0000153 return getMachineOpValue(MI, MI.getOperand(OpIdx));
154 }
Evan Cheng7602e112008-09-02 06:52:38 +0000155
Jim Grosbach08bd5492010-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 Grosbach8e0c7692011-09-02 18:46:15 +0000164 unsigned NEONThumb2DataIPostEncoder(const MachineInstr &MI, unsigned Val)
Owen Andersonc7139a62010-11-11 19:07:48 +0000165 const { return 0; }
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000166 unsigned NEONThumb2LoadStorePostEncoder(const MachineInstr &MI,unsigned Val)
Owen Anderson57dac882010-11-11 21:36:43 +0000167 const { return 0; }
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000168 unsigned NEONThumb2DupPostEncoder(const MachineInstr &MI,unsigned Val)
Owen Anderson8f143912010-11-11 23:12:55 +0000169 const { return 0; }
Bill Wendlingcf590262010-12-01 21:54:50 +0000170 unsigned VFPThumb2PostEncoder(const MachineInstr&MI, unsigned Val)
171 const { return 0; }
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000172 unsigned getAdrLabelOpValue(const MachineInstr &MI, unsigned Op)
173 const { return 0; }
Jim Grosbachd40963c2010-12-14 22:28:03 +0000174 unsigned getThumbAdrLabelOpValue(const MachineInstr &MI, unsigned Op)
175 const { return 0; }
Jim Grosbach662a8162010-12-06 23:57:07 +0000176 unsigned getThumbBLTargetOpValue(const MachineInstr &MI, unsigned Op)
177 const { return 0; }
Bill Wendling09aa3f02010-12-09 00:39:08 +0000178 unsigned getThumbBLXTargetOpValue(const MachineInstr &MI, unsigned Op)
179 const { return 0; }
Jim Grosbache2467172010-12-10 18:21:33 +0000180 unsigned getThumbBRTargetOpValue(const MachineInstr &MI, unsigned Op)
181 const { return 0; }
Jim Grosbach01086452010-12-10 17:13:40 +0000182 unsigned getThumbBCCTargetOpValue(const MachineInstr &MI, unsigned Op)
183 const { return 0; }
Jim Grosbach027d6e82010-12-09 19:04:53 +0000184 unsigned getThumbCBTargetOpValue(const MachineInstr &MI, unsigned Op)
Bill Wendlingdff2f712010-12-08 23:01:43 +0000185 const { return 0; }
Jim Grosbachc466b932010-11-11 18:04:49 +0000186 unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned Op)
187 const { return 0; }
Owen Andersonc2666002010-12-13 19:31:11 +0000188 unsigned getUnconditionalBranchTargetOpValue(const MachineInstr &MI,
189 unsigned Op) const { return 0; }
Jason W Kim685c3502011-02-04 19:47:15 +0000190 unsigned getARMBranchTargetOpValue(const MachineInstr &MI, unsigned Op)
191 const { return 0; }
Jim Grosbach7b25ecf2012-02-27 21:36:23 +0000192 unsigned getARMBLTargetOpValue(const MachineInstr &MI, unsigned Op)
193 const { return 0; }
Owen Andersonf1eab592011-08-26 23:32:08 +0000194 unsigned getARMBLXTargetOpValue(const MachineInstr &MI, unsigned Op)
195 const { return 0; }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000196 unsigned getCCOutOpValue(const MachineInstr &MI, unsigned Op)
197 const { return 0; }
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000198 unsigned getSOImmOpValue(const MachineInstr &MI, unsigned Op)
199 const { return 0; }
Owen Anderson5de6d842010-11-12 21:12:40 +0000200 unsigned getT2SOImmOpValue(const MachineInstr &MI, unsigned Op)
201 const { return 0; }
Owen Anderson152d4a42011-07-21 23:38:37 +0000202 unsigned getSORegRegOpValue(const MachineInstr &MI, unsigned Op)
203 const { return 0; }
204 unsigned getSORegImmOpValue(const MachineInstr &MI, unsigned Op)
Jim Grosbachef324d72010-10-12 23:53:58 +0000205 const { return 0; }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000206 unsigned getThumbAddrModeRegRegOpValue(const MachineInstr &MI, unsigned Op)
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000207 const { return 0; }
Owen Anderson75579f72010-11-29 22:44:32 +0000208 unsigned getT2AddrModeImm12OpValue(const MachineInstr &MI, unsigned Op)
209 const { return 0; }
210 unsigned getT2AddrModeImm8OpValue(const MachineInstr &MI, unsigned Op)
211 const { return 0; }
Jim Grosbacha77295d2011-09-08 22:07:06 +0000212 unsigned getT2Imm8s4OpValue(const MachineInstr &MI, unsigned Op)
213 const { return 0; }
Owen Anderson9d63d902010-12-01 19:18:46 +0000214 unsigned getT2AddrModeImm8s4OpValue(const MachineInstr &MI, unsigned Op)
215 const { return 0; }
Jim Grosbachb6aed502011-09-09 18:37:27 +0000216 unsigned getT2AddrModeImm0_1020s4OpValue(const MachineInstr &MI,unsigned Op)
217 const { return 0; }
Owen Anderson6af50f72010-11-30 00:14:31 +0000218 unsigned getT2AddrModeImm8OffsetOpValue(const MachineInstr &MI, unsigned Op)
219 const { return 0; }
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000220 unsigned getT2AddrModeImm12OffsetOpValue(const MachineInstr &MI,unsigned Op)
221 const { return 0; }
Owen Anderson75579f72010-11-29 22:44:32 +0000222 unsigned getT2AddrModeSORegOpValue(const MachineInstr &MI, unsigned Op)
223 const { return 0; }
Owen Anderson5de6d842010-11-12 21:12:40 +0000224 unsigned getT2SORegOpValue(const MachineInstr &MI, unsigned Op)
225 const { return 0; }
Owen Andersona838a252010-12-14 00:36:49 +0000226 unsigned getT2AdrLabelOpValue(const MachineInstr &MI, unsigned Op)
227 const { return 0; }
Owen Andersona2b50b32010-11-02 22:28:01 +0000228 unsigned getAddrMode6AddressOpValue(const MachineInstr &MI, unsigned Op)
Owen Andersond9aa7d32010-11-02 00:05:05 +0000229 const { return 0; }
Mon P Wang183c6272011-05-09 17:47:27 +0000230 unsigned getAddrMode6OneLane32AddressOpValue(const MachineInstr &MI,
231 unsigned Op)
232 const { return 0; }
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000233 unsigned getAddrMode6DupAddressOpValue(const MachineInstr &MI, unsigned Op)
234 const { return 0; }
Owen Andersona2b50b32010-11-02 22:28:01 +0000235 unsigned getAddrMode6OffsetOpValue(const MachineInstr &MI, unsigned Op)
Owen Andersoncf667be2010-11-02 01:24:55 +0000236 const { return 0; }
Jim Grosbach3fea191052010-10-21 22:03:21 +0000237 unsigned getBitfieldInvertedMaskOpValue(const MachineInstr &MI,
238 unsigned Op) const { return 0; }
Bruno Cardoso Lopes895c1e22011-05-31 03:33:27 +0000239 unsigned getSsatBitPosValue(const MachineInstr &MI,
240 unsigned Op) const { return 0; }
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000241 uint32_t getLdStmModeOpValue(const MachineInstr &MI, unsigned OpIdx)
242 const {return 0; }
Jim Grosbach54fea632010-11-09 17:20:53 +0000243 uint32_t getLdStSORegOpValue(const MachineInstr &MI, unsigned OpIdx)
244 const { return 0; }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000245
246 unsigned getAddrModeImm12OpValue(const MachineInstr &MI, unsigned Op)
247 const {
248 // {17-13} = reg
249 // {12} = (U)nsigned (add == '1', sub == '0')
250 // {11-0} = imm12
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000251 const MachineOperand &MO = MI.getOperand(Op);
252 const MachineOperand &MO1 = MI.getOperand(Op + 1);
253 if (!MO.isReg()) {
254 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
255 return 0;
Jim Grosbachf31430f2010-10-27 19:55:59 +0000256 }
Eric Christopherdf1c6372012-08-09 22:10:21 +0000257 unsigned Reg = II->getRegisterInfo().getEncodingValue(MO.getReg());
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000258 int32_t Imm12 = MO1.getImm();
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000259 uint32_t Binary;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000260 Binary = Imm12 & 0xfff;
261 if (Imm12 >= 0)
262 Binary |= (1 << 12);
263 Binary |= (Reg << 13);
264 return Binary;
265 }
Jason W Kim837caa92010-11-18 23:37:15 +0000266
Evan Cheng75972122011-01-13 07:58:56 +0000267 unsigned getHiLo16ImmOpValue(const MachineInstr &MI, unsigned Op) const {
Jason W Kim837caa92010-11-18 23:37:15 +0000268 return 0;
269 }
270
Jim Grosbach99f53d12010-11-15 20:47:07 +0000271 uint32_t getAddrMode2OpValue(const MachineInstr &MI, unsigned OpIdx)
272 const { return 0;}
273 uint32_t getAddrMode2OffsetOpValue(const MachineInstr &MI, unsigned OpIdx)
274 const { return 0;}
Jim Grosbach7ce05792011-08-03 23:50:40 +0000275 uint32_t getPostIdxRegOpValue(const MachineInstr &MI, unsigned OpIdx)
276 const { return 0;}
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000277 uint32_t getAddrMode3OffsetOpValue(const MachineInstr &MI, unsigned OpIdx)
278 const { return 0;}
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000279 uint32_t getAddrMode3OpValue(const MachineInstr &MI, unsigned Op)
280 const { return 0; }
Jim Grosbachd967cd02010-12-07 21:50:47 +0000281 uint32_t getAddrModeThumbSPOpValue(const MachineInstr &MI, unsigned Op)
282 const { return 0; }
Bill Wendling272df512010-12-09 21:49:07 +0000283 uint32_t getAddrModeSOpValue(const MachineInstr &MI, unsigned Op)
Bill Wendling1fd374e2010-11-30 22:57:21 +0000284 const { return 0; }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000285 uint32_t getAddrModeISOpValue(const MachineInstr &MI, unsigned Op)
286 const { return 0; }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000287 uint32_t getAddrModePCOpValue(const MachineInstr &MI, unsigned Op)
288 const { return 0; }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000289 uint32_t getAddrMode5OpValue(const MachineInstr &MI, unsigned Op) const {
Bill Wendling20272a72010-11-20 00:26:37 +0000290 // {17-13} = reg
291 // {12} = (U)nsigned (add == '1', sub == '0')
292 // {11-0} = imm12
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000293 const MachineOperand &MO = MI.getOperand(Op);
294 const MachineOperand &MO1 = MI.getOperand(Op + 1);
295 if (!MO.isReg()) {
296 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
297 return 0;
298 }
Eric Christopherdf1c6372012-08-09 22:10:21 +0000299 unsigned Reg = II->getRegisterInfo().getEncodingValue(MO.getReg());
Bill Wendling20272a72010-11-20 00:26:37 +0000300 int32_t Imm12 = MO1.getImm();
301
302 // Special value for #-0
303 if (Imm12 == INT32_MIN)
304 Imm12 = 0;
305
306 // Immediate is always encoded as positive. The 'U' bit controls add vs
307 // sub.
308 bool isAdd = true;
309 if (Imm12 < 0) {
310 Imm12 = -Imm12;
311 isAdd = false;
312 }
313
314 uint32_t Binary = Imm12 & 0xfff;
315 if (isAdd)
316 Binary |= (1 << 12);
317 Binary |= (Reg << 13);
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000318 return Binary;
319 }
Jim Grosbachc4bc2112010-10-29 23:21:57 +0000320 unsigned getNEONVcvtImm32OpValue(const MachineInstr &MI, unsigned Op)
321 const { return 0; }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000322
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000323 unsigned getRegisterListOpValue(const MachineInstr &MI, unsigned Op)
324 const { return 0; }
325
Bill Wendling3116dce2011-03-07 23:38:41 +0000326 unsigned getShiftRight8Imm(const MachineInstr &MI, unsigned Op)
Bill Wendlinga656b632011-03-01 01:00:59 +0000327 const { return 0; }
Bill Wendling3116dce2011-03-07 23:38:41 +0000328 unsigned getShiftRight16Imm(const MachineInstr &MI, unsigned Op)
Bill Wendlinga656b632011-03-01 01:00:59 +0000329 const { return 0; }
Bill Wendling3116dce2011-03-07 23:38:41 +0000330 unsigned getShiftRight32Imm(const MachineInstr &MI, unsigned Op)
331 const { return 0; }
332 unsigned getShiftRight64Imm(const MachineInstr &MI, unsigned Op)
Bill Wendlinga656b632011-03-01 01:00:59 +0000333 const { return 0; }
334
Shih-wei Liao5170b712010-05-26 00:02:28 +0000335 /// getMovi32Value - Return binary encoding of operand for movw/movt. If the
Jim Grosbach18f30e62010-06-02 21:53:11 +0000336 /// machine operand requires relocation, record the relocation and return
337 /// zero.
Shih-wei Liao5170b712010-05-26 00:02:28 +0000338 unsigned getMovi32Value(const MachineInstr &MI,const MachineOperand &MO,
Zonr Changf86399b2010-05-25 08:42:45 +0000339 unsigned Reloc);
Zonr Changf86399b2010-05-25 08:42:45 +0000340
Evan Cheng83b5cf02008-11-05 23:22:34 +0000341 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng7602e112008-09-02 06:52:38 +0000342 ///
Evan Cheng83b5cf02008-11-05 23:22:34 +0000343 unsigned getShiftOp(unsigned Imm) const ;
Evan Cheng7602e112008-09-02 06:52:38 +0000344
345 /// Routines that handle operands which add machine relocations which are
Evan Cheng437c1732008-11-07 22:30:53 +0000346 /// fixed up by the relocation stage.
Dan Gohman46510a72010-04-15 01:51:59 +0000347 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000348 bool MayNeedFarStub, bool Indirect,
Jim Grosbach3e094132010-10-08 17:45:54 +0000349 intptr_t ACPV = 0) const;
350 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
351 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
352 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const;
Evan Cheng437c1732008-11-07 22:30:53 +0000353 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
Jim Grosbach3e094132010-10-08 17:45:54 +0000354 intptr_t JTBase = 0) const;
Eric Christopherdf1c6372012-08-09 22:10:21 +0000355 unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) const;
356 unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) const;
357 unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) const;
358 unsigned encodeNEONRd(const MachineInstr &MI, unsigned OpIdx) const;
359 unsigned encodeNEONRn(const MachineInstr &MI, unsigned OpIdx) const;
360 unsigned encodeNEONRm(const MachineInstr &MI, unsigned OpIdx) const;
Evan Cheng148b6a42007-07-05 21:15:40 +0000361 };
Evan Cheng148b6a42007-07-05 21:15:40 +0000362}
363
Chris Lattner33fabd72010-02-02 21:48:51 +0000364char ARMCodeEmitter::ID = 0;
365
Bob Wilson87949d42010-03-17 21:16:45 +0000366/// createARMJITCodeEmitterPass - Return a pass that emits the collected ARM
Chris Lattnere0faa542010-02-02 21:38:59 +0000367/// code to the specified MCE object.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000368FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
369 JITCodeEmitter &JCE) {
Chris Lattner33fabd72010-02-02 21:48:51 +0000370 return new ARMCodeEmitter(TM, JCE);
Evan Cheng148b6a42007-07-05 21:15:40 +0000371}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000372
Chris Lattner33fabd72010-02-02 21:48:51 +0000373bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Benjamin Kramer4e23ebe2012-12-21 19:09:53 +0000374 TargetMachine &Target = const_cast<TargetMachine&>(MF.getTarget());
375
376 assert((Target.getRelocationModel() != Reloc::Default ||
377 Target.getRelocationModel() != Reloc::Static) &&
Evan Cheng148b6a42007-07-05 21:15:40 +0000378 "JIT relocation model must be set to static or default!");
Benjamin Kramer4e23ebe2012-12-21 19:09:53 +0000379
380 JTI = static_cast<ARMJITInfo*>(Target.getJITInfo());
381 II = static_cast<const ARMBaseInstrInfo*>(Target.getInstrInfo());
382 TD = Target.getDataLayout();
383
Evan Cheng08669742009-09-10 01:23:53 +0000384 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng938b9d82008-10-31 19:55:13 +0000385 MCPEs = &MF.getConstantPool()->getConstants();
Chris Lattnerb1e80392010-01-25 23:22:00 +0000386 MJTEs = 0;
387 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
Evan Cheng4df60f52008-11-07 09:06:08 +0000388 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Bob Wilson62d24a42010-06-28 22:23:17 +0000389 IsThumb = MF.getInfo<ARMFunctionInfo>()->isThumbFunction();
Evan Cheng3cc82232008-11-08 07:38:22 +0000390 JTI->Initialize(MF, IsPIC);
Chris Lattner16112732010-03-14 01:41:15 +0000391 MMI = &getAnalysis<MachineModuleInfo>();
392 MCE.setModuleInfo(MMI);
Evan Cheng148b6a42007-07-05 21:15:40 +0000393
394 do {
Jim Grosbach764ab522009-08-11 15:33:49 +0000395 DEBUG(errs() << "JITTing function '"
Craig Topper96601ca2012-08-22 06:07:19 +0000396 << MF.getName() << "'\n");
Evan Cheng148b6a42007-07-05 21:15:40 +0000397 MCE.startFunction(MF);
Jim Grosbach764ab522009-08-11 15:33:49 +0000398 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
Evan Cheng148b6a42007-07-05 21:15:40 +0000399 MBB != E; ++MBB) {
400 MCE.StartMachineBasicBlock(MBB);
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000401 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Evan Cheng148b6a42007-07-05 21:15:40 +0000402 I != E; ++I)
403 emitInstruction(*I);
404 }
405 } while (MCE.finishFunction(MF));
406
407 return false;
408}
409
Evan Cheng83b5cf02008-11-05 23:22:34 +0000410/// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng7602e112008-09-02 06:52:38 +0000411///
Chris Lattner33fabd72010-02-02 21:48:51 +0000412unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
Evan Cheng83b5cf02008-11-05 23:22:34 +0000413 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000414 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng7602e112008-09-02 06:52:38 +0000415 case ARM_AM::asr: return 2;
416 case ARM_AM::lsl: return 0;
417 case ARM_AM::lsr: return 1;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000418 case ARM_AM::ror:
Evan Cheng7602e112008-09-02 06:52:38 +0000419 case ARM_AM::rrx: return 3;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000420 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000421}
422
Shih-wei Liao5170b712010-05-26 00:02:28 +0000423/// getMovi32Value - Return binary encoding of operand for movw/movt. If the
Zonr Changf86399b2010-05-25 08:42:45 +0000424/// machine operand requires relocation, record the relocation and return zero.
425unsigned ARMCodeEmitter::getMovi32Value(const MachineInstr &MI,
Shih-wei Liao5170b712010-05-26 00:02:28 +0000426 const MachineOperand &MO,
Zonr Changf86399b2010-05-25 08:42:45 +0000427 unsigned Reloc) {
Shih-wei Liao5170b712010-05-26 00:02:28 +0000428 assert(((Reloc == ARM::reloc_arm_movt) || (Reloc == ARM::reloc_arm_movw))
Zonr Changf86399b2010-05-25 08:42:45 +0000429 && "Relocation to this function should be for movt or movw");
430
431 if (MO.isImm())
432 return static_cast<unsigned>(MO.getImm());
433 else if (MO.isGlobal())
434 emitGlobalAddress(MO.getGlobal(), Reloc, true, false);
435 else if (MO.isSymbol())
436 emitExternalSymbolAddress(MO.getSymbolName(), Reloc);
437 else if (MO.isMBB())
438 emitMachineBasicBlock(MO.getMBB(), Reloc);
439 else {
440#ifndef NDEBUG
441 errs() << MO;
442#endif
443 llvm_unreachable("Unsupported operand type for movw/movt");
444 }
445 return 0;
446}
447
Evan Cheng7602e112008-09-02 06:52:38 +0000448/// getMachineOpValue - Return binary encoding of operand. If the machine
449/// operand requires relocation, record the relocation and return zero.
Chris Lattner33fabd72010-02-02 21:48:51 +0000450unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
Jim Grosbach3e094132010-10-08 17:45:54 +0000451 const MachineOperand &MO) const {
Dan Gohmand735b802008-10-03 15:45:36 +0000452 if (MO.isReg())
Eric Christopherdf1c6372012-08-09 22:10:21 +0000453 return II->getRegisterInfo().getEncodingValue(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000454 else if (MO.isImm())
Evan Cheng7602e112008-09-02 06:52:38 +0000455 return static_cast<unsigned>(MO.getImm());
Dan Gohmand735b802008-10-03 15:45:36 +0000456 else if (MO.isGlobal())
Evan Cheng08669742009-09-10 01:23:53 +0000457 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true, false);
Dan Gohmand735b802008-10-03 15:45:36 +0000458 else if (MO.isSymbol())
Evan Cheng10332512008-11-08 07:22:33 +0000459 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
Evan Cheng580c0df2008-11-12 01:02:24 +0000460 else if (MO.isCPI()) {
Evan Chenge837dea2011-06-28 19:10:37 +0000461 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng580c0df2008-11-12 01:02:24 +0000462 // For VFP load, the immediate offset is multiplied by 4.
Evan Chenge837dea2011-06-28 19:10:37 +0000463 unsigned Reloc = ((MCID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
Evan Cheng580c0df2008-11-12 01:02:24 +0000464 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
465 emitConstPoolAddress(MO.getIndex(), Reloc);
466 } else if (MO.isJTI())
Chris Lattner8aa797a2007-12-30 23:10:15 +0000467 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
Dan Gohmand735b802008-10-03 15:45:36 +0000468 else if (MO.isMBB())
Evan Cheng4df60f52008-11-07 09:06:08 +0000469 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
Jim Grosbach817c1a62010-11-19 00:27:09 +0000470 else
471 llvm_unreachable("Unable to encode MachineOperand!");
Evan Cheng7602e112008-09-02 06:52:38 +0000472 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000473}
474
Evan Cheng057d0c32008-09-18 07:28:19 +0000475/// emitGlobalAddress - Emit the specified address to the code stream.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000476///
Dan Gohman46510a72010-04-15 01:51:59 +0000477void ARMCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
Chris Lattner33fabd72010-02-02 21:48:51 +0000478 bool MayNeedFarStub, bool Indirect,
Jim Grosbach3e094132010-10-08 17:45:54 +0000479 intptr_t ACPV) const {
Evan Cheng08669742009-09-10 01:23:53 +0000480 MachineRelocation MR = Indirect
481 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
Dan Gohman46510a72010-04-15 01:51:59 +0000482 const_cast<GlobalValue *>(GV),
483 ACPV, MayNeedFarStub)
Evan Cheng08669742009-09-10 01:23:53 +0000484 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Dan Gohman46510a72010-04-15 01:51:59 +0000485 const_cast<GlobalValue *>(GV), ACPV,
486 MayNeedFarStub);
Evan Cheng08669742009-09-10 01:23:53 +0000487 MCE.addRelocation(MR);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000488}
489
490/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
491/// be emitted to the current location in the function, and allow it to be PC
492/// relative.
Jim Grosbach3e094132010-10-08 17:45:54 +0000493void ARMCodeEmitter::
494emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000495 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
496 Reloc, ES));
497}
498
499/// emitConstPoolAddress - Arrange for the address of an constant pool
500/// to be emitted to the current location in the function, and allow it to be PC
501/// relative.
Jim Grosbach3e094132010-10-08 17:45:54 +0000502void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
Evan Cheng0f282432008-10-29 23:55:43 +0000503 // Tell JIT emitter we'll resolve the address.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000504 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000505 Reloc, CPI, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000506}
507
508/// emitJumpTableAddress - Arrange for the address of a jump table to
509/// be emitted to the current location in the function, and allow it to be PC
510/// relative.
Jim Grosbach3e094132010-10-08 17:45:54 +0000511void ARMCodeEmitter::
512emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000513 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000514 Reloc, JTIndex, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000515}
516
Raul Herbster9c1a3822007-08-30 23:29:26 +0000517/// emitMachineBasicBlock - Emit the specified address basic block.
Chris Lattner33fabd72010-02-02 21:48:51 +0000518void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
Jim Grosbach3e094132010-10-08 17:45:54 +0000519 unsigned Reloc,
520 intptr_t JTBase) const {
Raul Herbster9c1a3822007-08-30 23:29:26 +0000521 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000522 Reloc, BB, JTBase));
Raul Herbster9c1a3822007-08-30 23:29:26 +0000523}
Evan Cheng0ff94f72007-08-07 01:37:15 +0000524
Chris Lattner33fabd72010-02-02 21:48:51 +0000525void ARMCodeEmitter::emitWordLE(unsigned Binary) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000526 DEBUG(errs() << " 0x";
527 errs().write_hex(Binary) << "\n");
Evan Cheng83b5cf02008-11-05 23:22:34 +0000528 MCE.emitWordLE(Binary);
529}
530
Chris Lattner33fabd72010-02-02 21:48:51 +0000531void ARMCodeEmitter::emitDWordLE(uint64_t Binary) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000532 DEBUG(errs() << " 0x";
533 errs().write_hex(Binary) << "\n");
Evan Chengcb5201f2008-11-11 22:19:31 +0000534 MCE.emitDWordLE(Binary);
535}
536
Chris Lattner33fabd72010-02-02 21:48:51 +0000537void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000538 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
Evan Cheng42d5ee062008-09-13 01:15:21 +0000539
Devang Patelaf0e2722009-10-06 02:19:11 +0000540 MCE.processDebugLoc(MI.getDebugLoc(), true);
Jeffrey Yasskin75402822009-07-17 18:49:39 +0000541
Dan Gohmanfe601042010-06-22 15:08:57 +0000542 ++NumEmitted; // Keep track of the # of mi's emitted
Evan Chengedda31c2008-11-05 18:35:52 +0000543 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
Evan Chengffa6d962008-11-13 23:36:57 +0000544 default: {
Torok Edwinc23197a2009-07-14 16:55:14 +0000545 llvm_unreachable("Unhandled instruction encoding format!");
Evan Chengffa6d962008-11-13 23:36:57 +0000546 }
Jim Grosbach85eb54c2010-11-17 23:33:14 +0000547 case ARMII::MiscFrm:
548 if (MI.getOpcode() == ARM::LEApcrelJT) {
549 // Materialize jumptable address.
550 emitLEApcrelJTInstruction(MI);
551 break;
552 }
553 llvm_unreachable("Unhandled instruction encoding!");
Evan Chengedda31c2008-11-05 18:35:52 +0000554 case ARMII::Pseudo:
Evan Cheng057d0c32008-09-18 07:28:19 +0000555 emitPseudoInstruction(MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000556 break;
557 case ARMII::DPFrm:
558 case ARMII::DPSoRegFrm:
559 emitDataProcessingInstruction(MI);
560 break;
Evan Cheng148cad82008-11-13 07:34:59 +0000561 case ARMII::LdFrm:
562 case ARMII::StFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000563 emitLoadStoreInstruction(MI);
564 break;
Evan Cheng148cad82008-11-13 07:34:59 +0000565 case ARMII::LdMiscFrm:
566 case ARMII::StMiscFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000567 emitMiscLoadStoreInstruction(MI);
568 break;
Evan Cheng3c4a4ff2008-11-12 07:18:38 +0000569 case ARMII::LdStMulFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000570 emitLoadStoreMultipleInstruction(MI);
571 break;
Evan Chengfbc9d412008-11-06 01:21:28 +0000572 case ARMII::MulFrm:
573 emitMulFrmInstruction(MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000574 break;
Evan Cheng97f48c32008-11-06 22:15:19 +0000575 case ARMII::ExtFrm:
576 emitExtendInstruction(MI);
577 break;
Evan Cheng8b59db32008-11-07 01:41:35 +0000578 case ARMII::ArithMiscFrm:
579 emitMiscArithInstruction(MI);
580 break;
Bob Wilson9a1c1892010-08-11 00:01:18 +0000581 case ARMII::SatFrm:
582 emitSaturateInstruction(MI);
583 break;
Evan Cheng12c3a532008-11-06 17:48:05 +0000584 case ARMII::BrFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000585 emitBranchInstruction(MI);
586 break;
Evan Cheng12c3a532008-11-06 17:48:05 +0000587 case ARMII::BrMiscFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000588 emitMiscBranchInstruction(MI);
589 break;
Evan Cheng96581d32008-11-11 02:11:05 +0000590 // VFP instructions.
591 case ARMII::VFPUnaryFrm:
592 case ARMII::VFPBinaryFrm:
593 emitVFPArithInstruction(MI);
594 break;
Evan Cheng78be83d2008-11-11 19:40:26 +0000595 case ARMII::VFPConv1Frm:
596 case ARMII::VFPConv2Frm:
Evan Cheng0a0ab132008-11-11 22:46:12 +0000597 case ARMII::VFPConv3Frm:
Evan Cheng80a11982008-11-12 06:41:41 +0000598 case ARMII::VFPConv4Frm:
599 case ARMII::VFPConv5Frm:
Evan Cheng78be83d2008-11-11 19:40:26 +0000600 emitVFPConversionInstruction(MI);
601 break;
Evan Chengcd8e66a2008-11-11 21:48:44 +0000602 case ARMII::VFPLdStFrm:
603 emitVFPLoadStoreInstruction(MI);
604 break;
605 case ARMII::VFPLdStMulFrm:
606 emitVFPLoadStoreMultipleInstruction(MI);
607 break;
Bill Wendling07fda9f2010-10-15 23:35:12 +0000608
Bob Wilson1a913ed2010-06-11 21:34:50 +0000609 // NEON instructions.
Bob Wilson52e4a0a2010-06-26 04:07:15 +0000610 case ARMII::NGetLnFrm:
Bob Wilsond5a563d2010-06-29 17:34:07 +0000611 case ARMII::NSetLnFrm:
612 emitNEONLaneInstruction(MI);
Bob Wilson52e4a0a2010-06-26 04:07:15 +0000613 break;
Bob Wilson21773e72010-06-29 20:13:29 +0000614 case ARMII::NDupFrm:
615 emitNEONDupInstruction(MI);
616 break;
Bob Wilson1a913ed2010-06-11 21:34:50 +0000617 case ARMII::N1RegModImmFrm:
Bob Wilson583a2a02010-06-25 21:17:19 +0000618 emitNEON1RegModImmInstruction(MI);
619 break;
620 case ARMII::N2RegFrm:
621 emitNEON2RegInstruction(MI);
Bob Wilson1a913ed2010-06-11 21:34:50 +0000622 break;
Bob Wilson5e7b6072010-06-25 22:40:46 +0000623 case ARMII::N3RegFrm:
624 emitNEON3RegInstruction(MI);
625 break;
Evan Chengedda31c2008-11-05 18:35:52 +0000626 }
Devang Patelaf0e2722009-10-06 02:19:11 +0000627 MCE.processDebugLoc(MI.getDebugLoc(), false);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000628}
629
Chris Lattner33fabd72010-02-02 21:48:51 +0000630void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
Evan Cheng437c1732008-11-07 22:30:53 +0000631 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
632 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
Evan Cheng938b9d82008-10-31 19:55:13 +0000633 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
Jim Grosbach764ab522009-08-11 15:33:49 +0000634
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000635 // Remember the CONSTPOOL_ENTRY address for later relocation.
636 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
637
638 // Emit constpool island entry. In most cases, the actual values will be
639 // resolved and relocated after code emission.
640 if (MCPE.isMachineConstantPoolEntry()) {
641 ARMConstantPoolValue *ACPV =
642 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
643
Chris Lattner705e07f2009-08-23 03:41:05 +0000644 DEBUG(errs() << " ** ARM constant pool #" << CPI << " @ "
645 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n');
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000646
Bob Wilson28989a82009-11-02 16:59:06 +0000647 assert(ACPV->isGlobalValue() && "unsupported constant pool value");
Bill Wendling5bb77992011-10-01 08:00:54 +0000648 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000649 if (GV) {
Evan Cheng08669742009-09-10 01:23:53 +0000650 Reloc::Model RelocM = TM.getRelocationModel();
Evan Chenge4e4ed32009-08-28 23:18:09 +0000651 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
Evan Cheng08669742009-09-10 01:23:53 +0000652 isa<Function>(GV),
653 Subtarget->GVIsIndirectSymbol(GV, RelocM),
654 (intptr_t)ACPV);
Bill Wendlingfe31e672011-10-01 08:58:29 +0000655 } else {
656 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
657 emitExternalSymbolAddress(Sym, ARM::reloc_arm_absolute);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000658 }
Evan Cheng83b5cf02008-11-05 23:22:34 +0000659 emitWordLE(0);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000660 } else {
Dan Gohman46510a72010-04-15 01:51:59 +0000661 const Constant *CV = MCPE.Val.ConstVal;
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000662
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000663 DEBUG({
664 errs() << " ** Constant pool #" << CPI << " @ "
665 << (void*)MCE.getCurrentPCValue() << " ";
666 if (const Function *F = dyn_cast<Function>(CV))
667 errs() << F->getName();
668 else
669 errs() << *CV;
670 errs() << '\n';
671 });
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000672
Dan Gohman46510a72010-04-15 01:51:59 +0000673 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Evan Cheng08669742009-09-10 01:23:53 +0000674 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV), false);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000675 emitWordLE(0);
Evan Chengcb5201f2008-11-11 22:19:31 +0000676 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Gabor Greif41f31ef2010-10-22 23:16:11 +0000677 uint32_t Val = uint32_t(*CI->getValue().getRawData());
Evan Cheng83b5cf02008-11-05 23:22:34 +0000678 emitWordLE(Val);
Evan Chengcb5201f2008-11-11 22:19:31 +0000679 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000680 if (CFP->getType()->isFloatTy())
Evan Chengcb5201f2008-11-11 22:19:31 +0000681 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000682 else if (CFP->getType()->isDoubleTy())
Evan Chengcb5201f2008-11-11 22:19:31 +0000683 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
684 else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000685 llvm_unreachable("Unable to handle this constantpool entry!");
Evan Chengcb5201f2008-11-11 22:19:31 +0000686 }
687 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000688 llvm_unreachable("Unable to handle this constantpool entry!");
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000689 }
690 }
691}
692
Zonr Changf86399b2010-05-25 08:42:45 +0000693void ARMCodeEmitter::emitMOVi32immInstruction(const MachineInstr &MI) {
694 const MachineOperand &MO0 = MI.getOperand(0);
695 const MachineOperand &MO1 = MI.getOperand(1);
696
697 // Emit the 'movw' instruction.
698 unsigned Binary = 0x30 << 20; // mov: Insts{27-20} = 0b00110000
699
700 unsigned Lo16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movw) & 0xFFFF;
701
702 // Set the conditional execution predicate.
703 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
704
705 // Encode Rd.
706 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
707
708 // Encode imm16 as imm4:imm12
709 Binary |= Lo16 & 0xFFF; // Insts{11-0} = imm12
710 Binary |= ((Lo16 >> 12) & 0xF) << 16; // Insts{19-16} = imm4
711 emitWordLE(Binary);
712
713 unsigned Hi16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movt) >> 16;
714 // Emit the 'movt' instruction.
715 Binary = 0x34 << 20; // movt: Insts{27-20} = 0b00110100
716
717 // Set the conditional execution predicate.
718 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
719
720 // Encode Rd.
721 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
722
723 // Encode imm16 as imm4:imm1, same as movw above.
724 Binary |= Hi16 & 0xFFF;
725 Binary |= ((Hi16 >> 12) & 0xF) << 16;
726 emitWordLE(Binary);
727}
728
Chris Lattner33fabd72010-02-02 21:48:51 +0000729void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
Evan Cheng90922132008-11-06 02:25:39 +0000730 const MachineOperand &MO0 = MI.getOperand(0);
731 const MachineOperand &MO1 = MI.getOperand(1);
Bob Wilson5265a122010-03-11 00:46:22 +0000732 assert(MO1.isImm() && ARM_AM::isSOImmTwoPartVal(MO1.getImm()) &&
733 "Not a valid so_imm value!");
Evan Cheng90922132008-11-06 02:25:39 +0000734 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
735 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
736
737 // Emit the 'mov' instruction.
738 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
739
740 // Set the conditional execution predicate.
Evan Cheng97f48c32008-11-06 22:15:19 +0000741 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng90922132008-11-06 02:25:39 +0000742
743 // Encode Rd.
744 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
745
746 // Encode so_imm.
747 // Set bit I(25) to identify this is the immediate form of <shifter_op>
748 Binary |= 1 << ARMII::I_BitShift;
Evan Chenge7cbe412009-07-08 21:03:57 +0000749 Binary |= getMachineSoImmOpValue(V1);
Evan Cheng90922132008-11-06 02:25:39 +0000750 emitWordLE(Binary);
751
752 // Now the 'orr' instruction.
753 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
754
755 // Set the conditional execution predicate.
Evan Cheng97f48c32008-11-06 22:15:19 +0000756 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng90922132008-11-06 02:25:39 +0000757
758 // Encode Rd.
759 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
760
761 // Encode Rn.
762 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
763
764 // Encode so_imm.
765 // Set bit I(25) to identify this is the immediate form of <shifter_op>
766 Binary |= 1 << ARMII::I_BitShift;
Evan Chenge7cbe412009-07-08 21:03:57 +0000767 Binary |= getMachineSoImmOpValue(V2);
Evan Cheng90922132008-11-06 02:25:39 +0000768 emitWordLE(Binary);
769}
770
Chris Lattner33fabd72010-02-02 21:48:51 +0000771void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
Evan Cheng4df60f52008-11-07 09:06:08 +0000772 // It's basically add r, pc, (LJTI - $+8)
Jim Grosbach764ab522009-08-11 15:33:49 +0000773
Evan Chenge837dea2011-06-28 19:10:37 +0000774 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng4df60f52008-11-07 09:06:08 +0000775
776 // Emit the 'add' instruction.
Jim Grosbach0129be22010-11-17 21:57:51 +0000777 unsigned Binary = 0x4 << 21; // add: Insts{24-21} = 0b0100
Evan Cheng4df60f52008-11-07 09:06:08 +0000778
779 // Set the conditional execution predicate
780 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
781
782 // Encode S bit if MI modifies CPSR.
Evan Chenge837dea2011-06-28 19:10:37 +0000783 Binary |= getAddrModeSBit(MI, MCID);
Evan Cheng4df60f52008-11-07 09:06:08 +0000784
785 // Encode Rd.
786 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
787
788 // Encode Rn which is PC.
Eric Christopherdf1c6372012-08-09 22:10:21 +0000789 Binary |= II->getRegisterInfo().getEncodingValue(ARM::PC) << ARMII::RegRnShift;
Evan Cheng4df60f52008-11-07 09:06:08 +0000790
791 // Encode the displacement.
Evan Cheng4df60f52008-11-07 09:06:08 +0000792 Binary |= 1 << ARMII::I_BitShift;
793 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
794
795 emitWordLE(Binary);
796}
797
Chris Lattner33fabd72010-02-02 21:48:51 +0000798void ARMCodeEmitter::emitPseudoMoveInstruction(const MachineInstr &MI) {
Evan Chenga9562552008-11-14 20:09:11 +0000799 unsigned Opcode = MI.getDesc().Opcode;
800
801 // Part of binary is determined by TableGn.
802 unsigned Binary = getBinaryCodeForInstr(MI);
803
804 // Set the conditional execution predicate
805 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
806
807 // Encode S bit if MI modifies CPSR.
808 if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag)
809 Binary |= 1 << ARMII::S_BitShift;
810
811 // Encode register def if there is one.
812 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
813
814 // Encode the shift operation.
815 switch (Opcode) {
816 default: break;
Jim Grosbach792e9792010-10-14 20:43:44 +0000817 case ARM::RRX:
Evan Chenga9562552008-11-14 20:09:11 +0000818 // rrx
819 Binary |= 0x6 << 4;
820 break;
821 case ARM::MOVsrl_flag:
822 // lsr #1
823 Binary |= (0x2 << 4) | (1 << 7);
824 break;
825 case ARM::MOVsra_flag:
826 // asr #1
827 Binary |= (0x4 << 4) | (1 << 7);
828 break;
829 }
830
831 // Encode register Rm.
832 Binary |= getMachineOpValue(MI, 1);
833
834 emitWordLE(Binary);
835}
836
Chris Lattner33fabd72010-02-02 21:48:51 +0000837void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000838 DEBUG(errs() << " ** LPC" << LabelID << " @ "
839 << (void*)MCE.getCurrentPCValue() << '\n');
Evan Cheng83b5cf02008-11-05 23:22:34 +0000840 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
841}
842
Chris Lattner33fabd72010-02-02 21:48:51 +0000843void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000844 unsigned Opcode = MI.getDesc().Opcode;
845 switch (Opcode) {
846 default:
Evan Cheng5adb66a2009-09-28 09:14:39 +0000847 llvm_unreachable("ARMCodeEmitter::emitPseudoInstruction");
Jim Grosbach532c2f12010-11-30 00:24:05 +0000848 case ARM::BX_CALL:
Jakob Stoklund Olesenf16936e2012-04-06 00:04:58 +0000849 case ARM::BMOVPCRX_CALL: {
Xerxes Ranby99ccffe2010-07-22 17:28:34 +0000850 // First emit mov lr, pc
851 unsigned Binary = 0x01a0e00f;
852 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
853 emitWordLE(Binary);
854
855 // and then emit the branch.
856 emitMiscBranchInstruction(MI);
857 break;
858 }
Chris Lattner518bb532010-02-09 19:54:29 +0000859 case TargetOpcode::INLINEASM: {
Evan Chenge3066ab2008-11-19 23:21:33 +0000860 // We allow inline assembler nodes with empty bodies - they can
861 // implicitly define registers, which is ok for JIT.
862 if (MI.getOperand(0).getSymbolName()[0]) {
Chris Lattner75361b62010-04-07 22:58:41 +0000863 report_fatal_error("JIT does not support inline asm!");
Evan Chenge3066ab2008-11-19 23:21:33 +0000864 }
Evan Chengffa6d962008-11-13 23:36:57 +0000865 break;
866 }
Bill Wendling7431bea2010-07-16 22:20:36 +0000867 case TargetOpcode::PROLOG_LABEL:
Chris Lattner7561d482010-03-14 02:33:54 +0000868 case TargetOpcode::EH_LABEL:
869 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
870 break;
Chris Lattner518bb532010-02-09 19:54:29 +0000871 case TargetOpcode::IMPLICIT_DEF:
872 case TargetOpcode::KILL:
Evan Chengffa6d962008-11-13 23:36:57 +0000873 // Do nothing.
874 break;
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000875 case ARM::CONSTPOOL_ENTRY:
876 emitConstPoolInstruction(MI);
877 break;
878 case ARM::PICADD: {
Evan Cheng25e04782008-11-04 00:50:32 +0000879 // Remember of the address of the PC label for relocation later.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000880 addPCLabel(MI.getOperand(2).getImm());
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000881 // PICADD is just an add instruction that implicitly read pc.
Evan Cheng437c1732008-11-07 22:30:53 +0000882 emitDataProcessingInstruction(MI, 0, ARM::PC);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000883 break;
884 }
885 case ARM::PICLDR:
886 case ARM::PICLDRB:
887 case ARM::PICSTR:
888 case ARM::PICSTRB: {
889 // Remember of the address of the PC label for relocation later.
890 addPCLabel(MI.getOperand(2).getImm());
891 // These are just load / store instructions that implicitly read pc.
Evan Cheng4df60f52008-11-07 09:06:08 +0000892 emitLoadStoreInstruction(MI, 0, ARM::PC);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000893 break;
894 }
895 case ARM::PICLDRH:
896 case ARM::PICLDRSH:
897 case ARM::PICLDRSB:
898 case ARM::PICSTRH: {
899 // Remember of the address of the PC label for relocation later.
900 addPCLabel(MI.getOperand(2).getImm());
901 // These are just load / store instructions that implicitly read pc.
902 emitMiscLoadStoreInstruction(MI, ARM::PC);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000903 break;
904 }
Zonr Changf86399b2010-05-25 08:42:45 +0000905
906 case ARM::MOVi32imm:
Evan Cheng893d7fe2010-11-12 23:03:38 +0000907 // Two instructions to materialize a constant.
908 if (Subtarget->hasV6T2Ops())
909 emitMOVi32immInstruction(MI);
910 else
911 emitMOVi2piecesInstruction(MI);
Zonr Changf86399b2010-05-25 08:42:45 +0000912 break;
913
Evan Cheng4df60f52008-11-07 09:06:08 +0000914 case ARM::LEApcrelJT:
915 // Materialize jumptable address.
916 emitLEApcrelJTInstruction(MI);
917 break;
Jim Grosbach792e9792010-10-14 20:43:44 +0000918 case ARM::RRX:
Evan Chenga9562552008-11-14 20:09:11 +0000919 case ARM::MOVsrl_flag:
920 case ARM::MOVsra_flag:
921 emitPseudoMoveInstruction(MI);
922 break;
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000923 }
924}
925
Bob Wilson87949d42010-03-17 21:16:45 +0000926unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
Evan Chenge837dea2011-06-28 19:10:37 +0000927 const MCInstrDesc &MCID,
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000928 const MachineOperand &MO,
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000929 unsigned OpIdx) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000930 unsigned Binary = getMachineOpValue(MI, MO);
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000931
932 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
933 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
934 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
935
936 // Encode the shift opcode.
937 unsigned SBits = 0;
938 unsigned Rs = MO1.getReg();
939 if (Rs) {
940 // Set shift operand (bit[7:4]).
941 // LSL - 0001
942 // LSR - 0011
943 // ASR - 0101
944 // ROR - 0111
945 // RRX - 0110 and bit[11:8] clear.
946 switch (SOpc) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000947 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000948 case ARM_AM::lsl: SBits = 0x1; break;
949 case ARM_AM::lsr: SBits = 0x3; break;
950 case ARM_AM::asr: SBits = 0x5; break;
951 case ARM_AM::ror: SBits = 0x7; break;
952 case ARM_AM::rrx: SBits = 0x6; break;
953 }
954 } else {
955 // Set shift operand (bit[6:4]).
956 // LSL - 000
957 // LSR - 010
958 // ASR - 100
959 // ROR - 110
960 switch (SOpc) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000961 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000962 case ARM_AM::lsl: SBits = 0x0; break;
963 case ARM_AM::lsr: SBits = 0x2; break;
964 case ARM_AM::asr: SBits = 0x4; break;
965 case ARM_AM::ror: SBits = 0x6; break;
966 }
967 }
968 Binary |= SBits << 4;
969 if (SOpc == ARM_AM::rrx)
970 return Binary;
971
972 // Encode the shift operation Rs or shift_imm (except rrx).
973 if (Rs) {
974 // Encode Rs bit[11:8].
975 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
Eric Christopherdf1c6372012-08-09 22:10:21 +0000976 return Binary | (II->getRegisterInfo().getEncodingValue(Rs) << ARMII::RegRsShift);
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000977 }
978
979 // Encode shift_imm bit[11:7].
980 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
981}
982
Chris Lattner33fabd72010-02-02 21:48:51 +0000983unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
Evan Chenge7cbe412009-07-08 21:03:57 +0000984 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
985 assert(SoImmVal != -1 && "Not a valid so_imm value!");
986
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000987 // Encode rotate_imm.
Evan Chenge7cbe412009-07-08 21:03:57 +0000988 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
Evan Cheng97f48c32008-11-06 22:15:19 +0000989 << ARMII::SoRotImmShift;
990
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000991 // Encode immed_8.
Evan Chenge7cbe412009-07-08 21:03:57 +0000992 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000993 return Binary;
994}
995
Chris Lattner33fabd72010-02-02 21:48:51 +0000996unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
Evan Chenge837dea2011-06-28 19:10:37 +0000997 const MCInstrDesc &MCID) const {
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000998 for (unsigned i = MI.getNumOperands(), e = MCID.getNumOperands(); i >= e;--i){
Evan Cheng49a9f292008-09-12 22:45:55 +0000999 const MachineOperand &MO = MI.getOperand(i-1);
Dan Gohmand735b802008-10-03 15:45:36 +00001000 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
Evan Cheng49a9f292008-09-12 22:45:55 +00001001 return 1 << ARMII::S_BitShift;
1002 }
1003 return 0;
1004}
1005
Bob Wilson87949d42010-03-17 21:16:45 +00001006void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
Evan Cheng437c1732008-11-07 22:30:53 +00001007 unsigned ImplicitRd,
Evan Cheng83b5cf02008-11-05 23:22:34 +00001008 unsigned ImplicitRn) {
Evan Chenge837dea2011-06-28 19:10:37 +00001009 const MCInstrDesc &MCID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +00001010
1011 // Part of binary is determined by TableGn.
1012 unsigned Binary = getBinaryCodeForInstr(MI);
1013
Jim Grosbach33412622008-10-07 19:05:35 +00001014 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001015 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng5f1db7b2008-09-12 22:01:15 +00001016
Evan Cheng49a9f292008-09-12 22:45:55 +00001017 // Encode S bit if MI modifies CPSR.
Evan Chenge837dea2011-06-28 19:10:37 +00001018 Binary |= getAddrModeSBit(MI, MCID);
Evan Cheng49a9f292008-09-12 22:45:55 +00001019
Evan Cheng5f1db7b2008-09-12 22:01:15 +00001020 // Encode register def if there is one.
Evan Chenge837dea2011-06-28 19:10:37 +00001021 unsigned NumDefs = MCID.getNumDefs();
Evan Chenga964b7d2008-09-12 23:15:39 +00001022 unsigned OpIdx = 0;
Evan Cheng437c1732008-11-07 22:30:53 +00001023 if (NumDefs)
1024 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1025 else if (ImplicitRd)
1026 // Special handling for implicit use (e.g. PC).
Eric Christopherdf1c6372012-08-09 22:10:21 +00001027 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRd) << ARMII::RegRdShift);
Evan Cheng7602e112008-09-02 06:52:38 +00001028
Evan Chenge837dea2011-06-28 19:10:37 +00001029 if (MCID.Opcode == ARM::MOVi16) {
Zonr Changf86399b2010-05-25 08:42:45 +00001030 // Get immediate from MI.
1031 unsigned Lo16 = getMovi32Value(MI, MI.getOperand(OpIdx),
1032 ARM::reloc_arm_movw);
1033 // Encode imm which is the same as in emitMOVi32immInstruction().
1034 Binary |= Lo16 & 0xFFF;
1035 Binary |= ((Lo16 >> 12) & 0xF) << 16;
1036 emitWordLE(Binary);
1037 return;
Evan Chenge837dea2011-06-28 19:10:37 +00001038 } else if(MCID.Opcode == ARM::MOVTi16) {
Zonr Changf86399b2010-05-25 08:42:45 +00001039 unsigned Hi16 = (getMovi32Value(MI, MI.getOperand(OpIdx),
1040 ARM::reloc_arm_movt) >> 16);
1041 Binary |= Hi16 & 0xFFF;
1042 Binary |= ((Hi16 >> 12) & 0xF) << 16;
1043 emitWordLE(Binary);
1044 return;
Evan Chenge837dea2011-06-28 19:10:37 +00001045 } else if ((MCID.Opcode == ARM::BFC) || (MCID.Opcode == ARM::BFI)) {
Shih-wei Liao6d37a292010-05-26 00:25:05 +00001046 uint32_t v = ~MI.getOperand(2).getImm();
1047 int32_t lsb = CountTrailingZeros_32(v);
1048 int32_t msb = (32 - CountLeadingZeros_32(v)) - 1;
Shih-wei Liao45469f32010-05-26 03:21:39 +00001049 // Instr{20-16} = msb, Instr{11-7} = lsb
Shih-wei Liao6d37a292010-05-26 00:25:05 +00001050 Binary |= (msb & 0x1F) << 16;
1051 Binary |= (lsb & 0x1F) << 7;
1052 emitWordLE(Binary);
1053 return;
Evan Chenge837dea2011-06-28 19:10:37 +00001054 } else if ((MCID.Opcode == ARM::UBFX) || (MCID.Opcode == ARM::SBFX)) {
Shih-wei Liao45469f32010-05-26 03:21:39 +00001055 // Encode Rn in Instr{0-3}
1056 Binary |= getMachineOpValue(MI, OpIdx++);
1057
1058 uint32_t lsb = MI.getOperand(OpIdx++).getImm();
1059 uint32_t widthm1 = MI.getOperand(OpIdx++).getImm() - 1;
1060
1061 // Instr{20-16} = widthm1, Instr{11-7} = lsb
1062 Binary |= (widthm1 & 0x1F) << 16;
1063 Binary |= (lsb & 0x1F) << 7;
1064 emitWordLE(Binary);
1065 return;
Zonr Changf86399b2010-05-25 08:42:45 +00001066 }
1067
Evan Chengd87293c2008-11-06 08:47:38 +00001068 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
Evan Chenge837dea2011-06-28 19:10:37 +00001069 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Evan Chengd87293c2008-11-06 08:47:38 +00001070 ++OpIdx;
1071
Jim Grosbachefd30ba2008-10-01 18:16:49 +00001072 // Encode first non-shifter register operand if there is one.
Evan Chenge837dea2011-06-28 19:10:37 +00001073 bool isUnary = MCID.TSFlags & ARMII::UnaryDP;
Evan Chengedda31c2008-11-05 18:35:52 +00001074 if (!isUnary) {
Evan Cheng83b5cf02008-11-05 23:22:34 +00001075 if (ImplicitRn)
1076 // Special handling for implicit use (e.g. PC).
Eric Christopherdf1c6372012-08-09 22:10:21 +00001077 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRn) << ARMII::RegRnShift);
Evan Chengeb4ed4b2008-10-31 19:10:44 +00001078 else {
1079 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
1080 ++OpIdx;
1081 }
Evan Cheng7602e112008-09-02 06:52:38 +00001082 }
1083
Evan Cheng5f1db7b2008-09-12 22:01:15 +00001084 // Encode shifter operand.
Evan Cheng5f1db7b2008-09-12 22:01:15 +00001085 const MachineOperand &MO = MI.getOperand(OpIdx);
Evan Chenge837dea2011-06-28 19:10:37 +00001086 if ((MCID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +00001087 // Encode SoReg.
Evan Chenge837dea2011-06-28 19:10:37 +00001088 emitWordLE(Binary | getMachineSoRegOpValue(MI, MCID, MO, OpIdx));
Evan Chengedda31c2008-11-05 18:35:52 +00001089 return;
1090 }
Evan Chengeb4ed4b2008-10-31 19:10:44 +00001091
Evan Chengedda31c2008-11-05 18:35:52 +00001092 if (MO.isReg()) {
Evan Cheng5f1db7b2008-09-12 22:01:15 +00001093 // Encode register Rm.
Eric Christopherdf1c6372012-08-09 22:10:21 +00001094 emitWordLE(Binary | II->getRegisterInfo().getEncodingValue(MO.getReg()));
Evan Chengedda31c2008-11-05 18:35:52 +00001095 return;
1096 }
Evan Cheng7602e112008-09-02 06:52:38 +00001097
Evan Cheng5f1db7b2008-09-12 22:01:15 +00001098 // Encode so_imm.
Evan Chenge7cbe412009-07-08 21:03:57 +00001099 Binary |= getMachineSoImmOpValue((unsigned)MO.getImm());
Evan Chengedda31c2008-11-05 18:35:52 +00001100
Evan Cheng83b5cf02008-11-05 23:22:34 +00001101 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +00001102}
1103
Bob Wilson87949d42010-03-17 21:16:45 +00001104void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
Evan Cheng4df60f52008-11-07 09:06:08 +00001105 unsigned ImplicitRd,
Evan Cheng83b5cf02008-11-05 23:22:34 +00001106 unsigned ImplicitRn) {
Evan Chenge837dea2011-06-28 19:10:37 +00001107 const MCInstrDesc &MCID = MI.getDesc();
1108 unsigned Form = MCID.TSFlags & ARMII::FormMask;
1109 bool IsPrePost = (MCID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +00001110
Evan Chengedda31c2008-11-05 18:35:52 +00001111 // Part of binary is determined by TableGn.
1112 unsigned Binary = getBinaryCodeForInstr(MI);
1113
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001114 // If this is an LDRi12, STRi12 or LDRcp, nothing more needs be done.
1115 if (MI.getOpcode() == ARM::LDRi12 || MI.getOpcode() == ARM::LDRcp ||
1116 MI.getOpcode() == ARM::STRi12) {
Jim Grosbach093177d2010-10-27 17:52:51 +00001117 emitWordLE(Binary);
1118 return;
1119 }
1120
Jim Grosbach33412622008-10-07 19:05:35 +00001121 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001122 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +00001123
Evan Cheng4df60f52008-11-07 09:06:08 +00001124 unsigned OpIdx = 0;
Evan Cheng148cad82008-11-13 07:34:59 +00001125
1126 // Operand 0 of a pre- and post-indexed store is the address base
1127 // writeback. Skip it.
1128 bool Skipped = false;
1129 if (IsPrePost && Form == ARMII::StFrm) {
1130 ++OpIdx;
1131 Skipped = true;
1132 }
1133
1134 // Set first operand
Evan Cheng4df60f52008-11-07 09:06:08 +00001135 if (ImplicitRd)
1136 // Special handling for implicit use (e.g. PC).
Eric Christopherdf1c6372012-08-09 22:10:21 +00001137 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRd) << ARMII::RegRdShift);
Evan Cheng4df60f52008-11-07 09:06:08 +00001138 else
1139 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +00001140
1141 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +00001142 if (ImplicitRn)
1143 // Special handling for implicit use (e.g. PC).
Eric Christopherdf1c6372012-08-09 22:10:21 +00001144 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRn) << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +00001145 else
1146 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +00001147
Evan Cheng05c356e2008-11-08 01:44:13 +00001148 // If this is a two-address operand, skip it. e.g. LDR_PRE.
Evan Chenge837dea2011-06-28 19:10:37 +00001149 if (!Skipped && MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +00001150 ++OpIdx;
1151
Evan Cheng83b5cf02008-11-05 23:22:34 +00001152 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +00001153 unsigned AM2Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +00001154 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +00001155
Evan Chenge7de7e32008-09-13 01:44:01 +00001156 // Set bit U(23) according to sign of immed value (positive or negative).
Evan Cheng83b5cf02008-11-05 23:22:34 +00001157 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
Evan Chenge7de7e32008-09-13 01:44:01 +00001158 ARMII::U_BitShift);
Evan Cheng7602e112008-09-02 06:52:38 +00001159 if (!MO2.getReg()) { // is immediate
Evan Cheng83b5cf02008-11-05 23:22:34 +00001160 if (ARM_AM::getAM2Offset(AM2Opc))
Evan Cheng7602e112008-09-02 06:52:38 +00001161 // Set the value of offset_12 field
Evan Cheng83b5cf02008-11-05 23:22:34 +00001162 Binary |= ARM_AM::getAM2Offset(AM2Opc);
1163 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +00001164 return;
Evan Cheng7602e112008-09-02 06:52:38 +00001165 }
1166
Bill Wendling7d31a162010-10-20 22:44:54 +00001167 // Set bit I(25), because this is not in immediate encoding.
Evan Cheng7602e112008-09-02 06:52:38 +00001168 Binary |= 1 << ARMII::I_BitShift;
1169 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
1170 // Set bit[3:0] to the corresponding Rm register
Eric Christopherdf1c6372012-08-09 22:10:21 +00001171 Binary |= II->getRegisterInfo().getEncodingValue(MO2.getReg());
Evan Cheng7602e112008-09-02 06:52:38 +00001172
Evan Cheng70632912008-11-12 07:34:37 +00001173 // If this instr is in scaled register offset/index instruction, set
Evan Cheng7602e112008-09-02 06:52:38 +00001174 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
Evan Cheng83b5cf02008-11-05 23:22:34 +00001175 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
Evan Cheng70632912008-11-12 07:34:37 +00001176 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
1177 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
Evan Cheng7602e112008-09-02 06:52:38 +00001178 }
1179
Evan Cheng83b5cf02008-11-05 23:22:34 +00001180 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +00001181}
1182
Chris Lattner33fabd72010-02-02 21:48:51 +00001183void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
Bob Wilson87949d42010-03-17 21:16:45 +00001184 unsigned ImplicitRn) {
Evan Chenge837dea2011-06-28 19:10:37 +00001185 const MCInstrDesc &MCID = MI.getDesc();
1186 unsigned Form = MCID.TSFlags & ARMII::FormMask;
1187 bool IsPrePost = (MCID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +00001188
Evan Chengedda31c2008-11-05 18:35:52 +00001189 // Part of binary is determined by TableGn.
1190 unsigned Binary = getBinaryCodeForInstr(MI);
1191
Jim Grosbach33412622008-10-07 19:05:35 +00001192 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001193 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +00001194
Evan Cheng148cad82008-11-13 07:34:59 +00001195 unsigned OpIdx = 0;
1196
1197 // Operand 0 of a pre- and post-indexed store is the address base
1198 // writeback. Skip it.
1199 bool Skipped = false;
1200 if (IsPrePost && Form == ARMII::StMiscFrm) {
1201 ++OpIdx;
1202 Skipped = true;
1203 }
1204
Evan Cheng7602e112008-09-02 06:52:38 +00001205 // Set first operand
Evan Cheng148cad82008-11-13 07:34:59 +00001206 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +00001207
Evan Cheng358dec52009-06-15 08:28:29 +00001208 // Skip LDRD and STRD's second operand.
Evan Chenge837dea2011-06-28 19:10:37 +00001209 if (MCID.Opcode == ARM::LDRD || MCID.Opcode == ARM::STRD)
Evan Cheng358dec52009-06-15 08:28:29 +00001210 ++OpIdx;
1211
Evan Cheng7602e112008-09-02 06:52:38 +00001212 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +00001213 if (ImplicitRn)
1214 // Special handling for implicit use (e.g. PC).
Eric Christopherdf1c6372012-08-09 22:10:21 +00001215 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRn) << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +00001216 else
1217 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +00001218
Evan Cheng05c356e2008-11-08 01:44:13 +00001219 // If this is a two-address operand, skip it. e.g. LDRH_POST.
Evan Chenge837dea2011-06-28 19:10:37 +00001220 if (!Skipped && MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +00001221 ++OpIdx;
1222
Evan Cheng83b5cf02008-11-05 23:22:34 +00001223 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +00001224 unsigned AM3Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +00001225 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +00001226
Evan Chenge7de7e32008-09-13 01:44:01 +00001227 // Set bit U(23) according to sign of immed value (positive or negative)
Evan Cheng83b5cf02008-11-05 23:22:34 +00001228 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
Evan Cheng7602e112008-09-02 06:52:38 +00001229 ARMII::U_BitShift);
1230
1231 // If this instr is in register offset/index encoding, set bit[3:0]
1232 // to the corresponding Rm register.
1233 if (MO2.getReg()) {
Eric Christopherdf1c6372012-08-09 22:10:21 +00001234 Binary |= II->getRegisterInfo().getEncodingValue(MO2.getReg());
Evan Cheng83b5cf02008-11-05 23:22:34 +00001235 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +00001236 return;
Evan Cheng7602e112008-09-02 06:52:38 +00001237 }
1238
Evan Chengd87293c2008-11-06 08:47:38 +00001239 // This instr is in immediate offset/index encoding, set bit 22 to 1.
Evan Cheng97f48c32008-11-06 22:15:19 +00001240 Binary |= 1 << ARMII::AM3_I_BitShift;
Evan Cheng83b5cf02008-11-05 23:22:34 +00001241 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
Evan Cheng7602e112008-09-02 06:52:38 +00001242 // Set operands
Evan Cheng70632912008-11-12 07:34:37 +00001243 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
1244 Binary |= (ImmOffs & 0xF); // immedL
Evan Cheng7602e112008-09-02 06:52:38 +00001245 }
1246
Evan Cheng83b5cf02008-11-05 23:22:34 +00001247 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +00001248}
1249
Evan Chengcd8e66a2008-11-11 21:48:44 +00001250static unsigned getAddrModeUPBits(unsigned Mode) {
1251 unsigned Binary = 0;
Evan Cheng7602e112008-09-02 06:52:38 +00001252
1253 // Set addressing mode by modifying bits U(23) and P(24)
1254 // IA - Increment after - bit U = 1 and bit P = 0
1255 // IB - Increment before - bit U = 1 and bit P = 1
1256 // DA - Decrement after - bit U = 0 and bit P = 0
1257 // DB - Decrement before - bit U = 0 and bit P = 1
Evan Cheng7602e112008-09-02 06:52:38 +00001258 switch (Mode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001259 default: llvm_unreachable("Unknown addressing sub-mode!");
Evan Cheng10bf7342009-09-09 23:55:03 +00001260 case ARM_AM::da: break;
Evan Cheng97f48c32008-11-06 22:15:19 +00001261 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
1262 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
1263 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
Evan Cheng7602e112008-09-02 06:52:38 +00001264 }
1265
Evan Chengcd8e66a2008-11-11 21:48:44 +00001266 return Binary;
1267}
1268
Bob Wilsonbffb5b32010-03-13 07:34:35 +00001269void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
Evan Chenge837dea2011-06-28 19:10:37 +00001270 const MCInstrDesc &MCID = MI.getDesc();
1271 bool IsUpdating = (MCID.TSFlags & ARMII::IndexModeMask) != 0;
Bob Wilsonbffb5b32010-03-13 07:34:35 +00001272
Evan Chengcd8e66a2008-11-11 21:48:44 +00001273 // Part of binary is determined by TableGn.
1274 unsigned Binary = getBinaryCodeForInstr(MI);
1275
1276 // Set the conditional execution predicate
1277 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1278
Bob Wilsonbffb5b32010-03-13 07:34:35 +00001279 // Skip operand 0 of an instruction with base register update.
1280 unsigned OpIdx = 0;
1281 if (IsUpdating)
1282 ++OpIdx;
1283
Evan Chengcd8e66a2008-11-11 21:48:44 +00001284 // Set base address operand
Bob Wilsonbffb5b32010-03-13 07:34:35 +00001285 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Chengcd8e66a2008-11-11 21:48:44 +00001286
1287 // Set addressing mode by modifying bits U(23) and P(24)
Bill Wendling2567eec2010-11-17 05:31:09 +00001288 ARM_AM::AMSubMode Mode = ARM_AM::getLoadStoreMultipleSubMode(MI.getOpcode());
1289 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(Mode));
Evan Chengcd8e66a2008-11-11 21:48:44 +00001290
Evan Cheng7602e112008-09-02 06:52:38 +00001291 // Set bit W(21)
Bob Wilsonab346052010-03-16 17:46:45 +00001292 if (IsUpdating)
Evan Cheng97f48c32008-11-06 22:15:19 +00001293 Binary |= 0x1 << ARMII::W_BitShift;
Evan Cheng7602e112008-09-02 06:52:38 +00001294
1295 // Set registers
Bob Wilsonbffb5b32010-03-13 07:34:35 +00001296 for (unsigned i = OpIdx+2, e = MI.getNumOperands(); i != e; ++i) {
Evan Cheng7602e112008-09-02 06:52:38 +00001297 const MachineOperand &MO = MI.getOperand(i);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001298 if (!MO.isReg() || MO.isImplicit())
1299 break;
Eric Christopherdf1c6372012-08-09 22:10:21 +00001300 unsigned RegNum = II->getRegisterInfo().getEncodingValue(MO.getReg());
Evan Cheng7602e112008-09-02 06:52:38 +00001301 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
1302 RegNum < 16);
1303 Binary |= 0x1 << RegNum;
1304 }
1305
Evan Cheng83b5cf02008-11-05 23:22:34 +00001306 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +00001307}
1308
Chris Lattner33fabd72010-02-02 21:48:51 +00001309void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
Evan Chenge837dea2011-06-28 19:10:37 +00001310 const MCInstrDesc &MCID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +00001311
1312 // Part of binary is determined by TableGn.
1313 unsigned Binary = getBinaryCodeForInstr(MI);
1314
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001315 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001316 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001317
1318 // Encode S bit if MI modifies CPSR.
Evan Chenge837dea2011-06-28 19:10:37 +00001319 Binary |= getAddrModeSBit(MI, MCID);
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001320
1321 // 32x32->64bit operations have two destination registers. The number
1322 // of register definitions will tell us if that's what we're dealing with.
Evan Cheng97f48c32008-11-06 22:15:19 +00001323 unsigned OpIdx = 0;
Evan Chenge837dea2011-06-28 19:10:37 +00001324 if (MCID.getNumDefs() == 2)
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001325 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
1326
1327 // Encode Rd
1328 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
1329
1330 // Encode Rm
1331 Binary |= getMachineOpValue(MI, OpIdx++);
1332
1333 // Encode Rs
1334 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
1335
Evan Chengfbc9d412008-11-06 01:21:28 +00001336 // Many multiple instructions (e.g. MLA) have three src operands. Encode
1337 // it as Rn (for multiply, that's in the same offset as RdLo.
Evan Chenge837dea2011-06-28 19:10:37 +00001338 if (MCID.getNumOperands() > OpIdx &&
1339 !MCID.OpInfo[OpIdx].isPredicate() &&
1340 !MCID.OpInfo[OpIdx].isOptionalDef())
Evan Cheng97f48c32008-11-06 22:15:19 +00001341 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
1342
1343 emitWordLE(Binary);
1344}
1345
Chris Lattner33fabd72010-02-02 21:48:51 +00001346void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
Evan Chenge837dea2011-06-28 19:10:37 +00001347 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng97f48c32008-11-06 22:15:19 +00001348
1349 // Part of binary is determined by TableGn.
1350 unsigned Binary = getBinaryCodeForInstr(MI);
1351
1352 // Set the conditional execution predicate
1353 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1354
1355 unsigned OpIdx = 0;
1356
1357 // Encode Rd
1358 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1359
1360 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
1361 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1362 if (MO2.isReg()) {
1363 // Two register operand form.
1364 // Encode Rn.
1365 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
1366
1367 // Encode Rm.
1368 Binary |= getMachineOpValue(MI, MO2);
1369 ++OpIdx;
1370 } else {
1371 Binary |= getMachineOpValue(MI, MO1);
1372 }
1373
1374 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
1375 if (MI.getOperand(OpIdx).isImm() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001376 !MCID.OpInfo[OpIdx].isPredicate() &&
1377 !MCID.OpInfo[OpIdx].isOptionalDef())
Evan Cheng97f48c32008-11-06 22:15:19 +00001378 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
Evan Chengfbc9d412008-11-06 01:21:28 +00001379
Evan Cheng83b5cf02008-11-05 23:22:34 +00001380 emitWordLE(Binary);
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001381}
1382
Chris Lattner33fabd72010-02-02 21:48:51 +00001383void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
Evan Chenge837dea2011-06-28 19:10:37 +00001384 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng8b59db32008-11-07 01:41:35 +00001385
1386 // Part of binary is determined by TableGn.
1387 unsigned Binary = getBinaryCodeForInstr(MI);
1388
1389 // Set the conditional execution predicate
1390 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1391
Eric Christopher33c110e2011-05-07 04:37:27 +00001392 // PKH instructions are finished at this point
Evan Chenge837dea2011-06-28 19:10:37 +00001393 if (MCID.Opcode == ARM::PKHBT || MCID.Opcode == ARM::PKHTB) {
Eric Christopher33c110e2011-05-07 04:37:27 +00001394 emitWordLE(Binary);
1395 return;
1396 }
1397
Evan Cheng8b59db32008-11-07 01:41:35 +00001398 unsigned OpIdx = 0;
1399
1400 // Encode Rd
1401 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1402
1403 const MachineOperand &MO = MI.getOperand(OpIdx++);
Evan Chenge837dea2011-06-28 19:10:37 +00001404 if (OpIdx == MCID.getNumOperands() ||
1405 MCID.OpInfo[OpIdx].isPredicate() ||
1406 MCID.OpInfo[OpIdx].isOptionalDef()) {
Evan Cheng8b59db32008-11-07 01:41:35 +00001407 // Encode Rm and it's done.
1408 Binary |= getMachineOpValue(MI, MO);
1409 emitWordLE(Binary);
1410 return;
1411 }
1412
1413 // Encode Rn.
1414 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1415
1416 // Encode Rm.
1417 Binary |= getMachineOpValue(MI, OpIdx++);
1418
1419 // Encode shift_imm.
1420 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
Evan Chenge837dea2011-06-28 19:10:37 +00001421 if (MCID.Opcode == ARM::PKHTB) {
Bob Wilsonf955f292010-08-17 17:23:19 +00001422 assert(ShiftAmt != 0 && "PKHTB shift_imm is 0!");
1423 if (ShiftAmt == 32)
1424 ShiftAmt = 0;
1425 }
Evan Cheng8b59db32008-11-07 01:41:35 +00001426 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1427 Binary |= ShiftAmt << ARMII::ShiftShift;
Jim Grosbach764ab522009-08-11 15:33:49 +00001428
Evan Cheng8b59db32008-11-07 01:41:35 +00001429 emitWordLE(Binary);
1430}
1431
Bob Wilson9a1c1892010-08-11 00:01:18 +00001432void ARMCodeEmitter::emitSaturateInstruction(const MachineInstr &MI) {
Evan Chenge837dea2011-06-28 19:10:37 +00001433 const MCInstrDesc &MCID = MI.getDesc();
Bob Wilson9a1c1892010-08-11 00:01:18 +00001434
1435 // Part of binary is determined by TableGen.
1436 unsigned Binary = getBinaryCodeForInstr(MI);
1437
1438 // Set the conditional execution predicate
1439 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1440
1441 // Encode Rd
1442 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
1443
1444 // Encode saturate bit position.
1445 unsigned Pos = MI.getOperand(1).getImm();
Evan Chenge837dea2011-06-28 19:10:37 +00001446 if (MCID.Opcode == ARM::SSAT || MCID.Opcode == ARM::SSAT16)
Bob Wilson9a1c1892010-08-11 00:01:18 +00001447 Pos -= 1;
1448 assert((Pos < 16 || (Pos < 32 &&
Evan Chenge837dea2011-06-28 19:10:37 +00001449 MCID.Opcode != ARM::SSAT16 &&
1450 MCID.Opcode != ARM::USAT16)) &&
Bob Wilson9a1c1892010-08-11 00:01:18 +00001451 "saturate bit position out of range");
1452 Binary |= Pos << 16;
1453
1454 // Encode Rm
1455 Binary |= getMachineOpValue(MI, 2);
1456
1457 // Encode shift_imm.
Evan Chenge837dea2011-06-28 19:10:37 +00001458 if (MCID.getNumOperands() == 4) {
Bob Wilsoneaf1c982010-08-11 23:10:46 +00001459 unsigned ShiftOp = MI.getOperand(3).getImm();
1460 ARM_AM::ShiftOpc Opc = ARM_AM::getSORegShOp(ShiftOp);
1461 if (Opc == ARM_AM::asr)
1462 Binary |= (1 << 6);
Bob Wilson9a1c1892010-08-11 00:01:18 +00001463 unsigned ShiftAmt = MI.getOperand(3).getImm();
Bob Wilsoneaf1c982010-08-11 23:10:46 +00001464 if (ShiftAmt == 32 && Opc == ARM_AM::asr)
Bob Wilson9a1c1892010-08-11 00:01:18 +00001465 ShiftAmt = 0;
1466 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1467 Binary |= ShiftAmt << ARMII::ShiftShift;
1468 }
1469
1470 emitWordLE(Binary);
1471}
1472
Chris Lattner33fabd72010-02-02 21:48:51 +00001473void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
Evan Chenge837dea2011-06-28 19:10:37 +00001474 const MCInstrDesc &MCID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +00001475
Evan Chenge837dea2011-06-28 19:10:37 +00001476 if (MCID.Opcode == ARM::TPsoft) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001477 llvm_unreachable("ARM::TPsoft FIXME"); // FIXME
Torok Edwindac237e2009-07-08 20:53:28 +00001478 }
Evan Cheng12c3a532008-11-06 17:48:05 +00001479
Evan Cheng7602e112008-09-02 06:52:38 +00001480 // Part of binary is determined by TableGn.
1481 unsigned Binary = getBinaryCodeForInstr(MI);
1482
Evan Chengedda31c2008-11-05 18:35:52 +00001483 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001484 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001485
1486 // Set signed_immed_24 field
1487 Binary |= getMachineOpValue(MI, 0);
1488
Evan Cheng83b5cf02008-11-05 23:22:34 +00001489 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +00001490}
1491
Chris Lattner33fabd72010-02-02 21:48:51 +00001492void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001493 // Remember the base address of the inline jump table.
Evan Cheng5788d1a2008-12-10 02:32:19 +00001494 uintptr_t JTBase = MCE.getCurrentPCValue();
Evan Cheng437c1732008-11-07 22:30:53 +00001495 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
Chris Lattner893e1c92009-08-23 06:49:22 +00001496 DEBUG(errs() << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase
1497 << '\n');
Evan Cheng4df60f52008-11-07 09:06:08 +00001498
1499 // Now emit the jump table entries.
1500 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1501 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1502 if (IsPIC)
1503 // DestBB address - JT base.
Evan Cheng437c1732008-11-07 22:30:53 +00001504 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
Evan Cheng4df60f52008-11-07 09:06:08 +00001505 else
1506 // Absolute DestBB address.
1507 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1508 emitWordLE(0);
1509 }
1510}
1511
Chris Lattner33fabd72010-02-02 21:48:51 +00001512void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
Evan Chenge837dea2011-06-28 19:10:37 +00001513 const MCInstrDesc &MCID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +00001514
Evan Cheng437c1732008-11-07 22:30:53 +00001515 // Handle jump tables.
Evan Chenge837dea2011-06-28 19:10:37 +00001516 if (MCID.Opcode == ARM::BR_JTr || MCID.Opcode == ARM::BR_JTadd) {
Evan Cheng437c1732008-11-07 22:30:53 +00001517 // First emit a ldr pc, [] instruction.
1518 emitDataProcessingInstruction(MI, ARM::PC);
1519
1520 // Then emit the inline jump table.
Evan Chengc9a41532009-07-08 00:05:05 +00001521 unsigned JTIndex =
Evan Chenge837dea2011-06-28 19:10:37 +00001522 (MCID.Opcode == ARM::BR_JTr)
Evan Cheng437c1732008-11-07 22:30:53 +00001523 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1524 emitInlineJumpTable(JTIndex);
1525 return;
Evan Chenge837dea2011-06-28 19:10:37 +00001526 } else if (MCID.Opcode == ARM::BR_JTm) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001527 // First emit a ldr pc, [] instruction.
1528 emitLoadStoreInstruction(MI, ARM::PC);
1529
1530 // Then emit the inline jump table.
Evan Cheng437c1732008-11-07 22:30:53 +00001531 emitInlineJumpTable(MI.getOperand(3).getIndex());
Evan Cheng4df60f52008-11-07 09:06:08 +00001532 return;
1533 }
1534
Evan Chengedda31c2008-11-05 18:35:52 +00001535 // Part of binary is determined by TableGn.
1536 unsigned Binary = getBinaryCodeForInstr(MI);
1537
1538 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001539 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001540
Evan Chenge837dea2011-06-28 19:10:37 +00001541 if (MCID.Opcode == ARM::BX_RET || MCID.Opcode == ARM::MOVPCLR)
Evan Chengedda31c2008-11-05 18:35:52 +00001542 // The return register is LR.
Eric Christopherdf1c6372012-08-09 22:10:21 +00001543 Binary |= II->getRegisterInfo().getEncodingValue(ARM::LR);
Jim Grosbach764ab522009-08-11 15:33:49 +00001544 else
Evan Chengedda31c2008-11-05 18:35:52 +00001545 // otherwise, set the return register
1546 Binary |= getMachineOpValue(MI, 0);
1547
Evan Cheng83b5cf02008-11-05 23:22:34 +00001548 emitWordLE(Binary);
Evan Cheng148b6a42007-07-05 21:15:40 +00001549}
Evan Cheng7602e112008-09-02 06:52:38 +00001550
Eric Christopherdf1c6372012-08-09 22:10:21 +00001551unsigned ARMCodeEmitter::encodeVFPRd(const MachineInstr &MI,
1552 unsigned OpIdx) const {
Evan Chengd06d48d2008-11-12 02:19:38 +00001553 unsigned RegD = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001554 unsigned Binary = 0;
Craig Topper420761a2012-04-20 07:30:17 +00001555 bool isSPVFP = ARM::SPRRegClass.contains(RegD);
Eric Christopherdf1c6372012-08-09 22:10:21 +00001556 RegD = II->getRegisterInfo().getEncodingValue(RegD);
Evan Chengd06d48d2008-11-12 02:19:38 +00001557 if (!isSPVFP)
1558 Binary |= RegD << ARMII::RegRdShift;
1559 else {
1560 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1561 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1562 }
Evan Cheng80a11982008-11-12 06:41:41 +00001563 return Binary;
1564}
Evan Cheng78be83d2008-11-11 19:40:26 +00001565
Eric Christopherdf1c6372012-08-09 22:10:21 +00001566unsigned ARMCodeEmitter::encodeVFPRn(const MachineInstr &MI,
1567 unsigned OpIdx) const {
Evan Chengd06d48d2008-11-12 02:19:38 +00001568 unsigned RegN = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001569 unsigned Binary = 0;
Craig Topper420761a2012-04-20 07:30:17 +00001570 bool isSPVFP = ARM::SPRRegClass.contains(RegN);
Eric Christopherdf1c6372012-08-09 22:10:21 +00001571 RegN = II->getRegisterInfo().getEncodingValue(RegN);
Evan Chengd06d48d2008-11-12 02:19:38 +00001572 if (!isSPVFP)
1573 Binary |= RegN << ARMII::RegRnShift;
1574 else {
1575 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1576 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1577 }
Evan Cheng80a11982008-11-12 06:41:41 +00001578 return Binary;
1579}
Evan Chengd06d48d2008-11-12 02:19:38 +00001580
Eric Christopherdf1c6372012-08-09 22:10:21 +00001581unsigned ARMCodeEmitter::encodeVFPRm(const MachineInstr &MI,
1582 unsigned OpIdx) const {
Evan Cheng80a11982008-11-12 06:41:41 +00001583 unsigned RegM = MI.getOperand(OpIdx).getReg();
1584 unsigned Binary = 0;
Craig Topper420761a2012-04-20 07:30:17 +00001585 bool isSPVFP = ARM::SPRRegClass.contains(RegM);
Eric Christopherdf1c6372012-08-09 22:10:21 +00001586 RegM = II->getRegisterInfo().getEncodingValue(RegM);
Evan Cheng80a11982008-11-12 06:41:41 +00001587 if (!isSPVFP)
1588 Binary |= RegM;
1589 else {
1590 Binary |= ((RegM & 0x1E) >> 1);
1591 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
Evan Cheng78be83d2008-11-11 19:40:26 +00001592 }
Evan Cheng80a11982008-11-12 06:41:41 +00001593 return Binary;
1594}
1595
Chris Lattner33fabd72010-02-02 21:48:51 +00001596void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) {
Evan Chenge837dea2011-06-28 19:10:37 +00001597 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001598
1599 // Part of binary is determined by TableGn.
1600 unsigned Binary = getBinaryCodeForInstr(MI);
1601
1602 // Set the conditional execution predicate
1603 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1604
1605 unsigned OpIdx = 0;
1606 assert((Binary & ARMII::D_BitShift) == 0 &&
1607 (Binary & ARMII::N_BitShift) == 0 &&
1608 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1609
1610 // Encode Dd / Sd.
1611 Binary |= encodeVFPRd(MI, OpIdx++);
1612
1613 // If this is a two-address operand, skip it, e.g. FMACD.
Evan Chenge837dea2011-06-28 19:10:37 +00001614 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001615 ++OpIdx;
1616
1617 // Encode Dn / Sn.
Evan Chenge837dea2011-06-28 19:10:37 +00001618 if ((MCID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
Evan Cheng3f4924e2008-11-12 08:14:21 +00001619 Binary |= encodeVFPRn(MI, OpIdx++);
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001620
Evan Chenge837dea2011-06-28 19:10:37 +00001621 if (OpIdx == MCID.getNumOperands() ||
1622 MCID.OpInfo[OpIdx].isPredicate() ||
1623 MCID.OpInfo[OpIdx].isOptionalDef()) {
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001624 // FCMPEZD etc. has only one operand.
1625 emitWordLE(Binary);
1626 return;
1627 }
1628
1629 // Encode Dm / Sm.
1630 Binary |= encodeVFPRm(MI, OpIdx);
Jim Grosbach764ab522009-08-11 15:33:49 +00001631
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001632 emitWordLE(Binary);
1633}
1634
Bob Wilson87949d42010-03-17 21:16:45 +00001635void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) {
Evan Chenge837dea2011-06-28 19:10:37 +00001636 const MCInstrDesc &MCID = MI.getDesc();
1637 unsigned Form = MCID.TSFlags & ARMII::FormMask;
Evan Cheng80a11982008-11-12 06:41:41 +00001638
1639 // Part of binary is determined by TableGn.
1640 unsigned Binary = getBinaryCodeForInstr(MI);
1641
1642 // Set the conditional execution predicate
1643 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1644
1645 switch (Form) {
1646 default: break;
1647 case ARMII::VFPConv1Frm:
1648 case ARMII::VFPConv2Frm:
1649 case ARMII::VFPConv3Frm:
1650 // Encode Dd / Sd.
1651 Binary |= encodeVFPRd(MI, 0);
1652 break;
1653 case ARMII::VFPConv4Frm:
1654 // Encode Dn / Sn.
1655 Binary |= encodeVFPRn(MI, 0);
1656 break;
1657 case ARMII::VFPConv5Frm:
1658 // Encode Dm / Sm.
1659 Binary |= encodeVFPRm(MI, 0);
1660 break;
1661 }
1662
1663 switch (Form) {
1664 default: break;
1665 case ARMII::VFPConv1Frm:
1666 // Encode Dm / Sm.
1667 Binary |= encodeVFPRm(MI, 1);
Evan Cheng67fd91f2008-11-13 07:46:59 +00001668 break;
Evan Cheng80a11982008-11-12 06:41:41 +00001669 case ARMII::VFPConv2Frm:
1670 case ARMII::VFPConv3Frm:
1671 // Encode Dn / Sn.
1672 Binary |= encodeVFPRn(MI, 1);
1673 break;
1674 case ARMII::VFPConv4Frm:
1675 case ARMII::VFPConv5Frm:
1676 // Encode Dd / Sd.
1677 Binary |= encodeVFPRd(MI, 1);
1678 break;
1679 }
1680
1681 if (Form == ARMII::VFPConv5Frm)
1682 // Encode Dn / Sn.
1683 Binary |= encodeVFPRn(MI, 2);
1684 else if (Form == ARMII::VFPConv3Frm)
1685 // Encode Dm / Sm.
1686 Binary |= encodeVFPRm(MI, 2);
Evan Cheng78be83d2008-11-11 19:40:26 +00001687
1688 emitWordLE(Binary);
1689}
1690
Chris Lattner33fabd72010-02-02 21:48:51 +00001691void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001692 // Part of binary is determined by TableGn.
1693 unsigned Binary = getBinaryCodeForInstr(MI);
1694
1695 // Set the conditional execution predicate
1696 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1697
1698 unsigned OpIdx = 0;
1699
1700 // Encode Dd / Sd.
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001701 Binary |= encodeVFPRd(MI, OpIdx++);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001702
1703 // Encode address base.
1704 const MachineOperand &Base = MI.getOperand(OpIdx++);
1705 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1706
1707 // If there is a non-zero immediate offset, encode it.
1708 if (Base.isReg()) {
1709 const MachineOperand &Offset = MI.getOperand(OpIdx);
1710 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1711 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1712 Binary |= 1 << ARMII::U_BitShift;
Evan Cheng607f1b42008-11-12 08:21:12 +00001713 Binary |= ImmOffs;
Evan Chengcd8e66a2008-11-11 21:48:44 +00001714 emitWordLE(Binary);
1715 return;
1716 }
1717 }
1718
1719 // If immediate offset is omitted, default to +0.
1720 Binary |= 1 << ARMII::U_BitShift;
1721
1722 emitWordLE(Binary);
1723}
1724
Bob Wilson87949d42010-03-17 21:16:45 +00001725void
1726ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
Evan Chenge837dea2011-06-28 19:10:37 +00001727 const MCInstrDesc &MCID = MI.getDesc();
1728 bool IsUpdating = (MCID.TSFlags & ARMII::IndexModeMask) != 0;
Bob Wilsonbffb5b32010-03-13 07:34:35 +00001729
Evan Chengcd8e66a2008-11-11 21:48:44 +00001730 // Part of binary is determined by TableGn.
1731 unsigned Binary = getBinaryCodeForInstr(MI);
1732
1733 // Set the conditional execution predicate
1734 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1735
Bob Wilsonbffb5b32010-03-13 07:34:35 +00001736 // Skip operand 0 of an instruction with base register update.
1737 unsigned OpIdx = 0;
1738 if (IsUpdating)
1739 ++OpIdx;
1740
Evan Chengcd8e66a2008-11-11 21:48:44 +00001741 // Set base address operand
Bob Wilsonbffb5b32010-03-13 07:34:35 +00001742 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Chengcd8e66a2008-11-11 21:48:44 +00001743
1744 // Set addressing mode by modifying bits U(23) and P(24)
Bill Wendling2567eec2010-11-17 05:31:09 +00001745 ARM_AM::AMSubMode Mode = ARM_AM::getLoadStoreMultipleSubMode(MI.getOpcode());
1746 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(Mode));
Evan Chengcd8e66a2008-11-11 21:48:44 +00001747
1748 // Set bit W(21)
Bob Wilson2d357f62010-03-16 18:38:09 +00001749 if (IsUpdating)
Evan Chengcd8e66a2008-11-11 21:48:44 +00001750 Binary |= 0x1 << ARMII::W_BitShift;
1751
1752 // First register is encoded in Dd.
Bob Wilsonbffb5b32010-03-13 07:34:35 +00001753 Binary |= encodeVFPRd(MI, OpIdx+2);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001754
Bob Wilsond4bfd542010-08-27 23:18:17 +00001755 // Count the number of registers.
Evan Chengcd8e66a2008-11-11 21:48:44 +00001756 unsigned NumRegs = 1;
Bob Wilsonbffb5b32010-03-13 07:34:35 +00001757 for (unsigned i = OpIdx+3, e = MI.getNumOperands(); i != e; ++i) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001758 const MachineOperand &MO = MI.getOperand(i);
1759 if (!MO.isReg() || MO.isImplicit())
1760 break;
1761 ++NumRegs;
1762 }
Shih-wei Liao5170b712010-05-26 00:02:28 +00001763 // Bit 8 will be set if <list> is consecutive 64-bit registers (e.g., D0)
1764 // Otherwise, it will be 0, in the case of 32-bit registers.
1765 if(Binary & 0x100)
1766 Binary |= NumRegs * 2;
1767 else
1768 Binary |= NumRegs;
Evan Chengcd8e66a2008-11-11 21:48:44 +00001769
1770 emitWordLE(Binary);
1771}
1772
Eric Christopherdf1c6372012-08-09 22:10:21 +00001773unsigned ARMCodeEmitter::encodeNEONRd(const MachineInstr &MI,
1774 unsigned OpIdx) const {
Bob Wilson1a913ed2010-06-11 21:34:50 +00001775 unsigned RegD = MI.getOperand(OpIdx).getReg();
1776 unsigned Binary = 0;
Eric Christopherdf1c6372012-08-09 22:10:21 +00001777 RegD = II->getRegisterInfo().getEncodingValue(RegD);
Bob Wilson1a913ed2010-06-11 21:34:50 +00001778 Binary |= (RegD & 0xf) << ARMII::RegRdShift;
1779 Binary |= ((RegD >> 4) & 1) << ARMII::D_BitShift;
1780 return Binary;
1781}
1782
Eric Christopherdf1c6372012-08-09 22:10:21 +00001783unsigned ARMCodeEmitter::encodeNEONRn(const MachineInstr &MI,
1784 unsigned OpIdx) const {
Bob Wilson5e7b6072010-06-25 22:40:46 +00001785 unsigned RegN = MI.getOperand(OpIdx).getReg();
1786 unsigned Binary = 0;
Eric Christopherdf1c6372012-08-09 22:10:21 +00001787 RegN = II->getRegisterInfo().getEncodingValue(RegN);
Bob Wilson5e7b6072010-06-25 22:40:46 +00001788 Binary |= (RegN & 0xf) << ARMII::RegRnShift;
1789 Binary |= ((RegN >> 4) & 1) << ARMII::N_BitShift;
1790 return Binary;
1791}
1792
Eric Christopherdf1c6372012-08-09 22:10:21 +00001793unsigned ARMCodeEmitter::encodeNEONRm(const MachineInstr &MI,
1794 unsigned OpIdx) const {
Bob Wilson583a2a02010-06-25 21:17:19 +00001795 unsigned RegM = MI.getOperand(OpIdx).getReg();
1796 unsigned Binary = 0;
Eric Christopherdf1c6372012-08-09 22:10:21 +00001797 RegM = II->getRegisterInfo().getEncodingValue(RegM);
Bob Wilson583a2a02010-06-25 21:17:19 +00001798 Binary |= (RegM & 0xf);
1799 Binary |= ((RegM >> 4) & 1) << ARMII::M_BitShift;
1800 return Binary;
1801}
1802
Bob Wilsond896a972010-06-28 21:12:19 +00001803/// convertNEONDataProcToThumb - Convert the ARM mode encoding for a NEON
1804/// data-processing instruction to the corresponding Thumb encoding.
1805static unsigned convertNEONDataProcToThumb(unsigned Binary) {
1806 assert((Binary & 0xfe000000) == 0xf2000000 &&
1807 "not an ARM NEON data-processing instruction");
1808 unsigned UBit = (Binary >> 24) & 1;
1809 return 0xef000000 | (UBit << 28) | (Binary & 0xffffff);
1810}
1811
Bob Wilsond5a563d2010-06-29 17:34:07 +00001812void ARMCodeEmitter::emitNEONLaneInstruction(const MachineInstr &MI) {
Bob Wilson52e4a0a2010-06-26 04:07:15 +00001813 unsigned Binary = getBinaryCodeForInstr(MI);
1814
Bob Wilsond5a563d2010-06-29 17:34:07 +00001815 unsigned RegTOpIdx, RegNOpIdx, LnOpIdx;
Evan Chenge837dea2011-06-28 19:10:37 +00001816 const MCInstrDesc &MCID = MI.getDesc();
1817 if ((MCID.TSFlags & ARMII::FormMask) == ARMII::NGetLnFrm) {
Bob Wilsond5a563d2010-06-29 17:34:07 +00001818 RegTOpIdx = 0;
1819 RegNOpIdx = 1;
1820 LnOpIdx = 2;
1821 } else { // ARMII::NSetLnFrm
1822 RegTOpIdx = 2;
1823 RegNOpIdx = 0;
1824 LnOpIdx = 3;
1825 }
1826
Bob Wilson52e4a0a2010-06-26 04:07:15 +00001827 // Set the conditional execution predicate
Bob Wilson5cdede42010-06-29 00:26:13 +00001828 Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift;
Bob Wilson52e4a0a2010-06-26 04:07:15 +00001829
Bob Wilsond5a563d2010-06-29 17:34:07 +00001830 unsigned RegT = MI.getOperand(RegTOpIdx).getReg();
Eric Christopherdf1c6372012-08-09 22:10:21 +00001831 RegT = II->getRegisterInfo().getEncodingValue(RegT);
Bob Wilson52e4a0a2010-06-26 04:07:15 +00001832 Binary |= (RegT << ARMII::RegRdShift);
Bob Wilsond5a563d2010-06-29 17:34:07 +00001833 Binary |= encodeNEONRn(MI, RegNOpIdx);
Bob Wilson52e4a0a2010-06-26 04:07:15 +00001834
1835 unsigned LaneShift;
1836 if ((Binary & (1 << 22)) != 0)
1837 LaneShift = 0; // 8-bit elements
1838 else if ((Binary & (1 << 5)) != 0)
1839 LaneShift = 1; // 16-bit elements
1840 else
1841 LaneShift = 2; // 32-bit elements
1842
Bob Wilsond5a563d2010-06-29 17:34:07 +00001843 unsigned Lane = MI.getOperand(LnOpIdx).getImm() << LaneShift;
Bob Wilson52e4a0a2010-06-26 04:07:15 +00001844 unsigned Opc1 = Lane >> 2;
1845 unsigned Opc2 = Lane & 3;
1846 assert((Opc1 & 3) == 0 && "out-of-range lane number operand");
1847 Binary |= (Opc1 << 21);
1848 Binary |= (Opc2 << 5);
1849
1850 emitWordLE(Binary);
1851}
1852
Bob Wilson21773e72010-06-29 20:13:29 +00001853void ARMCodeEmitter::emitNEONDupInstruction(const MachineInstr &MI) {
1854 unsigned Binary = getBinaryCodeForInstr(MI);
1855
1856 // Set the conditional execution predicate
1857 Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift;
1858
1859 unsigned RegT = MI.getOperand(1).getReg();
Eric Christopherdf1c6372012-08-09 22:10:21 +00001860 RegT = II->getRegisterInfo().getEncodingValue(RegT);
Bob Wilson21773e72010-06-29 20:13:29 +00001861 Binary |= (RegT << ARMII::RegRdShift);
1862 Binary |= encodeNEONRn(MI, 0);
1863 emitWordLE(Binary);
1864}
1865
Bob Wilson583a2a02010-06-25 21:17:19 +00001866void ARMCodeEmitter::emitNEON1RegModImmInstruction(const MachineInstr &MI) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00001867 unsigned Binary = getBinaryCodeForInstr(MI);
1868 // Destination register is encoded in Dd.
1869 Binary |= encodeNEONRd(MI, 0);
1870 // Immediate fields: Op, Cmode, I, Imm3, Imm4
1871 unsigned Imm = MI.getOperand(1).getImm();
1872 unsigned Op = (Imm >> 12) & 1;
Bob Wilson1a913ed2010-06-11 21:34:50 +00001873 unsigned Cmode = (Imm >> 8) & 0xf;
Bob Wilson1a913ed2010-06-11 21:34:50 +00001874 unsigned I = (Imm >> 7) & 1;
Bob Wilson1a913ed2010-06-11 21:34:50 +00001875 unsigned Imm3 = (Imm >> 4) & 0x7;
Bob Wilson1a913ed2010-06-11 21:34:50 +00001876 unsigned Imm4 = Imm & 0xf;
Bob Wilson08baddb2010-06-28 21:16:30 +00001877 Binary |= (I << 24) | (Imm3 << 16) | (Cmode << 8) | (Op << 5) | Imm4;
Bob Wilson62d24a42010-06-28 22:23:17 +00001878 if (IsThumb)
Bob Wilsond896a972010-06-28 21:12:19 +00001879 Binary = convertNEONDataProcToThumb(Binary);
Bob Wilson1a913ed2010-06-11 21:34:50 +00001880 emitWordLE(Binary);
1881}
1882
Bob Wilson583a2a02010-06-25 21:17:19 +00001883void ARMCodeEmitter::emitNEON2RegInstruction(const MachineInstr &MI) {
Evan Chenge837dea2011-06-28 19:10:37 +00001884 const MCInstrDesc &MCID = MI.getDesc();
Bob Wilson583a2a02010-06-25 21:17:19 +00001885 unsigned Binary = getBinaryCodeForInstr(MI);
Bob Wilson5e7b6072010-06-25 22:40:46 +00001886 // Destination register is encoded in Dd; source register in Dm.
1887 unsigned OpIdx = 0;
1888 Binary |= encodeNEONRd(MI, OpIdx++);
Evan Chenge837dea2011-06-28 19:10:37 +00001889 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Bob Wilson5e7b6072010-06-25 22:40:46 +00001890 ++OpIdx;
1891 Binary |= encodeNEONRm(MI, OpIdx);
Bob Wilson62d24a42010-06-28 22:23:17 +00001892 if (IsThumb)
Bob Wilsond896a972010-06-28 21:12:19 +00001893 Binary = convertNEONDataProcToThumb(Binary);
Bob Wilson583a2a02010-06-25 21:17:19 +00001894 // FIXME: This does not handle VDUPfdf or VDUPfqf.
1895 emitWordLE(Binary);
1896}
1897
Bob Wilson5e7b6072010-06-25 22:40:46 +00001898void ARMCodeEmitter::emitNEON3RegInstruction(const MachineInstr &MI) {
Evan Chenge837dea2011-06-28 19:10:37 +00001899 const MCInstrDesc &MCID = MI.getDesc();
Bob Wilson5e7b6072010-06-25 22:40:46 +00001900 unsigned Binary = getBinaryCodeForInstr(MI);
1901 // Destination register is encoded in Dd; source registers in Dn and Dm.
1902 unsigned OpIdx = 0;
1903 Binary |= encodeNEONRd(MI, OpIdx++);
Evan Chenge837dea2011-06-28 19:10:37 +00001904 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Bob Wilson5e7b6072010-06-25 22:40:46 +00001905 ++OpIdx;
1906 Binary |= encodeNEONRn(MI, OpIdx++);
Evan Chenge837dea2011-06-28 19:10:37 +00001907 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1)
Bob Wilson5e7b6072010-06-25 22:40:46 +00001908 ++OpIdx;
1909 Binary |= encodeNEONRm(MI, OpIdx);
Bob Wilson62d24a42010-06-28 22:23:17 +00001910 if (IsThumb)
Bob Wilsond896a972010-06-28 21:12:19 +00001911 Binary = convertNEONDataProcToThumb(Binary);
Bob Wilson5e7b6072010-06-25 22:40:46 +00001912 // FIXME: This does not handle VMOVDneon or VMOVQ.
1913 emitWordLE(Binary);
1914}
1915
Evan Cheng7602e112008-09-02 06:52:38 +00001916#include "ARMGenCodeEmitter.inc"