blob: 5e0c11e5da873e797c8eeaaaa944aef77291fa42 [file] [log] [blame]
Evan Cheng148b6a42007-07-05 21:15:40 +00001//===-- ARM/ARMCodeEmitter.cpp - Convert ARM code to machine code ---------===//
2//
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"
17#include "ARMAddressingModes.h"
Evan Cheng0f282432008-10-29 23:55:43 +000018#include "ARMConstantPoolValue.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000019#include "ARMInstrInfo.h"
Evan Cheng7602e112008-09-02 06:52:38 +000020#include "ARMRelocations.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000021#include "ARMSubtarget.h"
22#include "ARMTargetMachine.h"
Jim Grosbachbc6d8762008-10-28 18:25:49 +000023#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
Evan Cheng42d5ee062008-09-13 01:15:21 +000025#include "llvm/Function.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000026#include "llvm/PassManager.h"
27#include "llvm/CodeGen/MachineCodeEmitter.h"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000028#include "llvm/CodeGen/JITCodeEmitter.h"
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000029#include "llvm/CodeGen/ObjectCodeEmitter.h"
Evan Cheng057d0c32008-09-18 07:28:19 +000030#include "llvm/CodeGen/MachineConstantPool.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
32#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng4df60f52008-11-07 09:06:08 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Daniel Dunbar003de662009-09-21 05:58:35 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000035#include "llvm/CodeGen/Passes.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000036#include "llvm/ADT/Statistic.h"
37#include "llvm/Support/Compiler.h"
Evan Cheng42d5ee062008-09-13 01:15:21 +000038#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000039#include "llvm/Support/ErrorHandling.h"
40#include "llvm/Support/raw_ostream.h"
Evan Cheng4df60f52008-11-07 09:06:08 +000041#ifndef NDEBUG
42#include <iomanip>
43#endif
Evan Cheng148b6a42007-07-05 21:15:40 +000044using namespace llvm;
45
46STATISTIC(NumEmitted, "Number of machine instructions emitted");
47
48namespace {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000049
50 class ARMCodeEmitter {
51 public:
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000052 /// getBinaryCodeForInstr - This function, generated by the
53 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
54 /// machine instructions.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000055 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
56 };
57
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000058 template<class CodeEmitter>
59 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
60 public ARMCodeEmitter {
Evan Cheng057d0c32008-09-18 07:28:19 +000061 ARMJITInfo *JTI;
62 const ARMInstrInfo *II;
63 const TargetData *TD;
Evan Cheng08669742009-09-10 01:23:53 +000064 const ARMSubtarget *Subtarget;
Evan Cheng057d0c32008-09-18 07:28:19 +000065 TargetMachine &TM;
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000066 CodeEmitter &MCE;
Evan Cheng938b9d82008-10-31 19:55:13 +000067 const std::vector<MachineConstantPoolEntry> *MCPEs;
Evan Cheng4df60f52008-11-07 09:06:08 +000068 const std::vector<MachineJumpTableEntry> *MJTEs;
69 bool IsPIC;
70
Daniel Dunbar003de662009-09-21 05:58:35 +000071 void getAnalysisUsage(AnalysisUsage &AU) const {
72 AU.addRequired<MachineModuleInfo>();
73 MachineFunctionPass::getAnalysisUsage(AU);
74 }
75
Evan Cheng148b6a42007-07-05 21:15:40 +000076 public:
77 static char ID;
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000078 explicit Emitter(TargetMachine &tm, CodeEmitter &mce)
Evan Cheng057d0c32008-09-18 07:28:19 +000079 : MachineFunctionPass(&ID), JTI(0), II(0), TD(0), TM(tm),
Evan Cheng4df60f52008-11-07 09:06:08 +000080 MCE(mce), MCPEs(0), MJTEs(0),
81 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000082 Emitter(TargetMachine &tm, CodeEmitter &mce,
Evan Cheng148b6a42007-07-05 21:15:40 +000083 const ARMInstrInfo &ii, const TargetData &td)
Evan Cheng057d0c32008-09-18 07:28:19 +000084 : MachineFunctionPass(&ID), JTI(0), II(&ii), TD(&td), TM(tm),
Evan Cheng4df60f52008-11-07 09:06:08 +000085 MCE(mce), MCPEs(0), MJTEs(0),
86 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Evan Cheng148b6a42007-07-05 21:15:40 +000087
88 bool runOnMachineFunction(MachineFunction &MF);
89
90 virtual const char *getPassName() const {
91 return "ARM Machine Code Emitter";
92 }
93
94 void emitInstruction(const MachineInstr &MI);
Evan Cheng7602e112008-09-02 06:52:38 +000095
96 private:
Evan Cheng057d0c32008-09-18 07:28:19 +000097
Evan Cheng83b5cf02008-11-05 23:22:34 +000098 void emitWordLE(unsigned Binary);
99
Evan Chengcb5201f2008-11-11 22:19:31 +0000100 void emitDWordLE(uint64_t Binary);
101
Evan Cheng057d0c32008-09-18 07:28:19 +0000102 void emitConstPoolInstruction(const MachineInstr &MI);
103
Evan Cheng90922132008-11-06 02:25:39 +0000104 void emitMOVi2piecesInstruction(const MachineInstr &MI);
105
Evan Cheng4df60f52008-11-07 09:06:08 +0000106 void emitLEApcrelJTInstruction(const MachineInstr &MI);
107
Evan Chenga9562552008-11-14 20:09:11 +0000108 void emitPseudoMoveInstruction(const MachineInstr &MI);
109
Evan Cheng83b5cf02008-11-05 23:22:34 +0000110 void addPCLabel(unsigned LabelID);
111
Evan Cheng057d0c32008-09-18 07:28:19 +0000112 void emitPseudoInstruction(const MachineInstr &MI);
113
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000114 unsigned getMachineSoRegOpValue(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000115 const TargetInstrDesc &TID,
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000116 const MachineOperand &MO,
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000117 unsigned OpIdx);
118
Evan Cheng90922132008-11-06 02:25:39 +0000119 unsigned getMachineSoImmOpValue(unsigned SoImm);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000120
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +0000121 unsigned getAddrModeSBit(const MachineInstr &MI,
122 const TargetInstrDesc &TID) const;
Evan Cheng49a9f292008-09-12 22:45:55 +0000123
Evan Cheng83b5cf02008-11-05 23:22:34 +0000124 void emitDataProcessingInstruction(const MachineInstr &MI,
Evan Cheng437c1732008-11-07 22:30:53 +0000125 unsigned ImplicitRd = 0,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000126 unsigned ImplicitRn = 0);
Evan Cheng7602e112008-09-02 06:52:38 +0000127
Evan Cheng83b5cf02008-11-05 23:22:34 +0000128 void emitLoadStoreInstruction(const MachineInstr &MI,
Evan Cheng4df60f52008-11-07 09:06:08 +0000129 unsigned ImplicitRd = 0,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000130 unsigned ImplicitRn = 0);
Evan Chengedda31c2008-11-05 18:35:52 +0000131
Evan Cheng83b5cf02008-11-05 23:22:34 +0000132 void emitMiscLoadStoreInstruction(const MachineInstr &MI,
133 unsigned ImplicitRn = 0);
Evan Chengedda31c2008-11-05 18:35:52 +0000134
135 void emitLoadStoreMultipleInstruction(const MachineInstr &MI);
136
Evan Chengfbc9d412008-11-06 01:21:28 +0000137 void emitMulFrmInstruction(const MachineInstr &MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000138
Evan Cheng97f48c32008-11-06 22:15:19 +0000139 void emitExtendInstruction(const MachineInstr &MI);
140
Evan Cheng8b59db32008-11-07 01:41:35 +0000141 void emitMiscArithInstruction(const MachineInstr &MI);
142
Evan Chengedda31c2008-11-05 18:35:52 +0000143 void emitBranchInstruction(const MachineInstr &MI);
144
Evan Cheng437c1732008-11-07 22:30:53 +0000145 void emitInlineJumpTable(unsigned JTIndex);
Evan Cheng4df60f52008-11-07 09:06:08 +0000146
Evan Chengedda31c2008-11-05 18:35:52 +0000147 void emitMiscBranchInstruction(const MachineInstr &MI);
Evan Cheng7602e112008-09-02 06:52:38 +0000148
Evan Cheng96581d32008-11-11 02:11:05 +0000149 void emitVFPArithInstruction(const MachineInstr &MI);
150
Evan Cheng78be83d2008-11-11 19:40:26 +0000151 void emitVFPConversionInstruction(const MachineInstr &MI);
152
Evan Chengcd8e66a2008-11-11 21:48:44 +0000153 void emitVFPLoadStoreInstruction(const MachineInstr &MI);
154
155 void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI);
156
157 void emitMiscInstruction(const MachineInstr &MI);
158
Evan Cheng7602e112008-09-02 06:52:38 +0000159 /// getMachineOpValue - Return binary encoding of operand. If the machine
160 /// operand requires relocation, record the relocation and return zero.
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000161 unsigned getMachineOpValue(const MachineInstr &MI,const MachineOperand &MO);
Evan Cheng7602e112008-09-02 06:52:38 +0000162 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) {
163 return getMachineOpValue(MI, MI.getOperand(OpIdx));
164 }
Evan Cheng7602e112008-09-02 06:52:38 +0000165
Evan Cheng83b5cf02008-11-05 23:22:34 +0000166 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng7602e112008-09-02 06:52:38 +0000167 ///
Evan Cheng83b5cf02008-11-05 23:22:34 +0000168 unsigned getShiftOp(unsigned Imm) const ;
Evan Cheng7602e112008-09-02 06:52:38 +0000169
170 /// Routines that handle operands which add machine relocations which are
Evan Cheng437c1732008-11-07 22:30:53 +0000171 /// fixed up by the relocation stage.
Evan Cheng057d0c32008-09-18 07:28:19 +0000172 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
Evan Cheng08669742009-09-10 01:23:53 +0000173 bool NeedStub, bool Indirect, intptr_t ACPV = 0);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000174 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
Evan Cheng437c1732008-11-07 22:30:53 +0000175 void emitConstPoolAddress(unsigned CPI, unsigned Reloc);
176 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc);
177 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
178 intptr_t JTBase = 0);
Evan Cheng148b6a42007-07-05 21:15:40 +0000179 };
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000180 template <class CodeEmitter>
181 char Emitter<CodeEmitter>::ID = 0;
Evan Cheng148b6a42007-07-05 21:15:40 +0000182}
183
184/// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
185/// to the specified MCE object.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000186
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000187FunctionPass *llvm::createARMCodeEmitterPass(ARMBaseTargetMachine &TM,
188 MachineCodeEmitter &MCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000189 return new Emitter<MachineCodeEmitter>(TM, MCE);
190}
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000191FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
192 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000193 return new Emitter<JITCodeEmitter>(TM, JCE);
Evan Cheng148b6a42007-07-05 21:15:40 +0000194}
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000195FunctionPass *llvm::createARMObjectCodeEmitterPass(ARMBaseTargetMachine &TM,
196 ObjectCodeEmitter &OCE) {
197 return new Emitter<ObjectCodeEmitter>(TM, OCE);
198}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000199
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000200template<class CodeEmitter>
201bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000202 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
203 MF.getTarget().getRelocationModel() != Reloc::Static) &&
204 "JIT relocation model must be set to static or default!");
Evan Cheng08669742009-09-10 01:23:53 +0000205 JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo();
Evan Cheng148b6a42007-07-05 21:15:40 +0000206 II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo();
207 TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData();
Evan Cheng08669742009-09-10 01:23:53 +0000208 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng938b9d82008-10-31 19:55:13 +0000209 MCPEs = &MF.getConstantPool()->getConstants();
Evan Cheng4df60f52008-11-07 09:06:08 +0000210 MJTEs = &MF.getJumpTableInfo()->getJumpTables();
211 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Evan Cheng3cc82232008-11-08 07:38:22 +0000212 JTI->Initialize(MF, IsPIC);
Daniel Dunbar003de662009-09-21 05:58:35 +0000213 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
Evan Cheng148b6a42007-07-05 21:15:40 +0000214
215 do {
Jim Grosbach764ab522009-08-11 15:33:49 +0000216 DEBUG(errs() << "JITTing function '"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000217 << MF.getFunction()->getName() << "'\n");
Evan Cheng148b6a42007-07-05 21:15:40 +0000218 MCE.startFunction(MF);
Jim Grosbach764ab522009-08-11 15:33:49 +0000219 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
Evan Cheng148b6a42007-07-05 21:15:40 +0000220 MBB != E; ++MBB) {
221 MCE.StartMachineBasicBlock(MBB);
222 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
223 I != E; ++I)
224 emitInstruction(*I);
225 }
226 } while (MCE.finishFunction(MF));
227
228 return false;
229}
230
Evan Cheng83b5cf02008-11-05 23:22:34 +0000231/// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng7602e112008-09-02 06:52:38 +0000232///
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000233template<class CodeEmitter>
234unsigned Emitter<CodeEmitter>::getShiftOp(unsigned Imm) const {
Evan Cheng83b5cf02008-11-05 23:22:34 +0000235 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000236 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng7602e112008-09-02 06:52:38 +0000237 case ARM_AM::asr: return 2;
238 case ARM_AM::lsl: return 0;
239 case ARM_AM::lsr: return 1;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000240 case ARM_AM::ror:
Evan Cheng7602e112008-09-02 06:52:38 +0000241 case ARM_AM::rrx: return 3;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000242 }
Evan Cheng7602e112008-09-02 06:52:38 +0000243 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000244}
245
Evan Cheng7602e112008-09-02 06:52:38 +0000246/// getMachineOpValue - Return binary encoding of operand. If the machine
247/// operand requires relocation, record the relocation and return zero.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000248template<class CodeEmitter>
249unsigned Emitter<CodeEmitter>::getMachineOpValue(const MachineInstr &MI,
250 const MachineOperand &MO) {
Dan Gohmand735b802008-10-03 15:45:36 +0000251 if (MO.isReg())
Evan Cheng7602e112008-09-02 06:52:38 +0000252 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000253 else if (MO.isImm())
Evan Cheng7602e112008-09-02 06:52:38 +0000254 return static_cast<unsigned>(MO.getImm());
Dan Gohmand735b802008-10-03 15:45:36 +0000255 else if (MO.isGlobal())
Evan Cheng08669742009-09-10 01:23:53 +0000256 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true, false);
Dan Gohmand735b802008-10-03 15:45:36 +0000257 else if (MO.isSymbol())
Evan Cheng10332512008-11-08 07:22:33 +0000258 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
Evan Cheng580c0df2008-11-12 01:02:24 +0000259 else if (MO.isCPI()) {
260 const TargetInstrDesc &TID = MI.getDesc();
261 // For VFP load, the immediate offset is multiplied by 4.
262 unsigned Reloc = ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
263 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
264 emitConstPoolAddress(MO.getIndex(), Reloc);
265 } else if (MO.isJTI())
Chris Lattner8aa797a2007-12-30 23:10:15 +0000266 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
Dan Gohmand735b802008-10-03 15:45:36 +0000267 else if (MO.isMBB())
Evan Cheng4df60f52008-11-07 09:06:08 +0000268 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
Evan Cheng2aa0e642008-09-13 01:55:59 +0000269 else {
Torok Edwindac237e2009-07-08 20:53:28 +0000270#ifndef NDEBUG
Chris Lattner705e07f2009-08-23 03:41:05 +0000271 errs() << MO;
Torok Edwindac237e2009-07-08 20:53:28 +0000272#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000273 llvm_unreachable(0);
Evan Cheng2aa0e642008-09-13 01:55:59 +0000274 }
Evan Cheng7602e112008-09-02 06:52:38 +0000275 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000276}
277
Evan Cheng057d0c32008-09-18 07:28:19 +0000278/// emitGlobalAddress - Emit the specified address to the code stream.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000279///
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000280template<class CodeEmitter>
281void Emitter<CodeEmitter>::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
Evan Cheng08669742009-09-10 01:23:53 +0000282 bool NeedStub, bool Indirect,
283 intptr_t ACPV) {
284 MachineRelocation MR = Indirect
285 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
286 GV, ACPV, NeedStub)
287 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
288 GV, ACPV, NeedStub);
289 MCE.addRelocation(MR);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000290}
291
292/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
293/// be emitted to the current location in the function, and allow it to be PC
294/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000295template<class CodeEmitter>
296void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES,
297 unsigned Reloc) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000298 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
299 Reloc, ES));
300}
301
302/// emitConstPoolAddress - Arrange for the address of an constant pool
303/// to be emitted to the current location in the function, and allow it to be PC
304/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000305template<class CodeEmitter>
306void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI,
307 unsigned Reloc) {
Evan Cheng0f282432008-10-29 23:55:43 +0000308 // Tell JIT emitter we'll resolve the address.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000309 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000310 Reloc, CPI, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000311}
312
313/// emitJumpTableAddress - Arrange for the address of a jump table to
314/// be emitted to the current location in the function, and allow it to be PC
315/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000316template<class CodeEmitter>
Jim Grosbach764ab522009-08-11 15:33:49 +0000317void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTIndex,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000318 unsigned Reloc) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000319 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000320 Reloc, JTIndex, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000321}
322
Raul Herbster9c1a3822007-08-30 23:29:26 +0000323/// emitMachineBasicBlock - Emit the specified address basic block.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000324template<class CodeEmitter>
325void Emitter<CodeEmitter>::emitMachineBasicBlock(MachineBasicBlock *BB,
326 unsigned Reloc, intptr_t JTBase) {
Raul Herbster9c1a3822007-08-30 23:29:26 +0000327 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000328 Reloc, BB, JTBase));
Raul Herbster9c1a3822007-08-30 23:29:26 +0000329}
Evan Cheng0ff94f72007-08-07 01:37:15 +0000330
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000331template<class CodeEmitter>
332void Emitter<CodeEmitter>::emitWordLE(unsigned Binary) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000333 DEBUG(errs() << " 0x";
334 errs().write_hex(Binary) << "\n");
Evan Cheng83b5cf02008-11-05 23:22:34 +0000335 MCE.emitWordLE(Binary);
336}
337
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000338template<class CodeEmitter>
339void Emitter<CodeEmitter>::emitDWordLE(uint64_t Binary) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000340 DEBUG(errs() << " 0x";
341 errs().write_hex(Binary) << "\n");
Evan Chengcb5201f2008-11-11 22:19:31 +0000342 MCE.emitDWordLE(Binary);
343}
344
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000345template<class CodeEmitter>
346void Emitter<CodeEmitter>::emitInstruction(const MachineInstr &MI) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000347 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
Evan Cheng42d5ee062008-09-13 01:15:21 +0000348
Jeffrey Yasskin75402822009-07-17 18:49:39 +0000349 MCE.processDebugLoc(MI.getDebugLoc());
350
Evan Cheng148b6a42007-07-05 21:15:40 +0000351 NumEmitted++; // Keep track of the # of mi's emitted
Evan Chengedda31c2008-11-05 18:35:52 +0000352 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
Evan Chengffa6d962008-11-13 23:36:57 +0000353 default: {
Torok Edwinc23197a2009-07-14 16:55:14 +0000354 llvm_unreachable("Unhandled instruction encoding format!");
Evan Chengedda31c2008-11-05 18:35:52 +0000355 break;
Evan Chengffa6d962008-11-13 23:36:57 +0000356 }
Evan Chengedda31c2008-11-05 18:35:52 +0000357 case ARMII::Pseudo:
Evan Cheng057d0c32008-09-18 07:28:19 +0000358 emitPseudoInstruction(MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000359 break;
360 case ARMII::DPFrm:
361 case ARMII::DPSoRegFrm:
362 emitDataProcessingInstruction(MI);
363 break;
Evan Cheng148cad82008-11-13 07:34:59 +0000364 case ARMII::LdFrm:
365 case ARMII::StFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000366 emitLoadStoreInstruction(MI);
367 break;
Evan Cheng148cad82008-11-13 07:34:59 +0000368 case ARMII::LdMiscFrm:
369 case ARMII::StMiscFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000370 emitMiscLoadStoreInstruction(MI);
371 break;
Evan Cheng3c4a4ff2008-11-12 07:18:38 +0000372 case ARMII::LdStMulFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000373 emitLoadStoreMultipleInstruction(MI);
374 break;
Evan Chengfbc9d412008-11-06 01:21:28 +0000375 case ARMII::MulFrm:
376 emitMulFrmInstruction(MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000377 break;
Evan Cheng97f48c32008-11-06 22:15:19 +0000378 case ARMII::ExtFrm:
379 emitExtendInstruction(MI);
380 break;
Evan Cheng8b59db32008-11-07 01:41:35 +0000381 case ARMII::ArithMiscFrm:
382 emitMiscArithInstruction(MI);
383 break;
Evan Cheng12c3a532008-11-06 17:48:05 +0000384 case ARMII::BrFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000385 emitBranchInstruction(MI);
386 break;
Evan Cheng12c3a532008-11-06 17:48:05 +0000387 case ARMII::BrMiscFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000388 emitMiscBranchInstruction(MI);
389 break;
Evan Cheng96581d32008-11-11 02:11:05 +0000390 // VFP instructions.
391 case ARMII::VFPUnaryFrm:
392 case ARMII::VFPBinaryFrm:
393 emitVFPArithInstruction(MI);
394 break;
Evan Cheng78be83d2008-11-11 19:40:26 +0000395 case ARMII::VFPConv1Frm:
396 case ARMII::VFPConv2Frm:
Evan Cheng0a0ab132008-11-11 22:46:12 +0000397 case ARMII::VFPConv3Frm:
Evan Cheng80a11982008-11-12 06:41:41 +0000398 case ARMII::VFPConv4Frm:
399 case ARMII::VFPConv5Frm:
Evan Cheng78be83d2008-11-11 19:40:26 +0000400 emitVFPConversionInstruction(MI);
401 break;
Evan Chengcd8e66a2008-11-11 21:48:44 +0000402 case ARMII::VFPLdStFrm:
403 emitVFPLoadStoreInstruction(MI);
404 break;
405 case ARMII::VFPLdStMulFrm:
406 emitVFPLoadStoreMultipleInstruction(MI);
407 break;
408 case ARMII::VFPMiscFrm:
409 emitMiscInstruction(MI);
410 break;
Evan Chengedda31c2008-11-05 18:35:52 +0000411 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000412}
413
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000414template<class CodeEmitter>
415void Emitter<CodeEmitter>::emitConstPoolInstruction(const MachineInstr &MI) {
Evan Cheng437c1732008-11-07 22:30:53 +0000416 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
417 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
Evan Cheng938b9d82008-10-31 19:55:13 +0000418 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
Jim Grosbach764ab522009-08-11 15:33:49 +0000419
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000420 // Remember the CONSTPOOL_ENTRY address for later relocation.
421 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
422
423 // Emit constpool island entry. In most cases, the actual values will be
424 // resolved and relocated after code emission.
425 if (MCPE.isMachineConstantPoolEntry()) {
426 ARMConstantPoolValue *ACPV =
427 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
428
Chris Lattner705e07f2009-08-23 03:41:05 +0000429 DEBUG(errs() << " ** ARM constant pool #" << CPI << " @ "
430 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n');
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000431
432 GlobalValue *GV = ACPV->getGV();
433 if (GV) {
Evan Cheng08669742009-09-10 01:23:53 +0000434 Reloc::Model RelocM = TM.getRelocationModel();
Evan Chenge4e4ed32009-08-28 23:18:09 +0000435 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
Evan Cheng08669742009-09-10 01:23:53 +0000436 isa<Function>(GV),
437 Subtarget->GVIsIndirectSymbol(GV, RelocM),
438 (intptr_t)ACPV);
Evan Cheng25e04782008-11-04 00:50:32 +0000439 } else {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000440 emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
441 }
Evan Cheng83b5cf02008-11-05 23:22:34 +0000442 emitWordLE(0);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000443 } else {
444 Constant *CV = MCPE.Val.ConstVal;
445
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000446 DEBUG({
447 errs() << " ** Constant pool #" << CPI << " @ "
448 << (void*)MCE.getCurrentPCValue() << " ";
449 if (const Function *F = dyn_cast<Function>(CV))
450 errs() << F->getName();
451 else
452 errs() << *CV;
453 errs() << '\n';
454 });
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000455
456 if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Evan Cheng08669742009-09-10 01:23:53 +0000457 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV), false);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000458 emitWordLE(0);
Evan Chengcb5201f2008-11-11 22:19:31 +0000459 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000460 uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
Evan Cheng83b5cf02008-11-05 23:22:34 +0000461 emitWordLE(Val);
Evan Chengcb5201f2008-11-11 22:19:31 +0000462 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000463 if (CFP->getType() == Type::getFloatTy(CFP->getContext()))
Evan Chengcb5201f2008-11-11 22:19:31 +0000464 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Owen Anderson1d0be152009-08-13 21:58:54 +0000465 else if (CFP->getType() == Type::getDoubleTy(CFP->getContext()))
Evan Chengcb5201f2008-11-11 22:19:31 +0000466 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
467 else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000468 llvm_unreachable("Unable to handle this constantpool entry!");
Evan Chengcb5201f2008-11-11 22:19:31 +0000469 }
470 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000471 llvm_unreachable("Unable to handle this constantpool entry!");
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000472 }
473 }
474}
475
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000476template<class CodeEmitter>
477void Emitter<CodeEmitter>::emitMOVi2piecesInstruction(const MachineInstr &MI) {
Evan Cheng90922132008-11-06 02:25:39 +0000478 const MachineOperand &MO0 = MI.getOperand(0);
479 const MachineOperand &MO1 = MI.getOperand(1);
Evan Chenge7cbe412009-07-08 21:03:57 +0000480 assert(MO1.isImm() && ARM_AM::getSOImmVal(MO1.isImm()) != -1 &&
481 "Not a valid so_imm value!");
Evan Cheng90922132008-11-06 02:25:39 +0000482 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
483 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
484
485 // Emit the 'mov' instruction.
486 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
487
488 // Set the conditional execution predicate.
Evan Cheng97f48c32008-11-06 22:15:19 +0000489 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng90922132008-11-06 02:25:39 +0000490
491 // Encode Rd.
492 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
493
494 // Encode so_imm.
495 // Set bit I(25) to identify this is the immediate form of <shifter_op>
496 Binary |= 1 << ARMII::I_BitShift;
Evan Chenge7cbe412009-07-08 21:03:57 +0000497 Binary |= getMachineSoImmOpValue(V1);
Evan Cheng90922132008-11-06 02:25:39 +0000498 emitWordLE(Binary);
499
500 // Now the 'orr' instruction.
501 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
502
503 // Set the conditional execution predicate.
Evan Cheng97f48c32008-11-06 22:15:19 +0000504 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng90922132008-11-06 02:25:39 +0000505
506 // Encode Rd.
507 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
508
509 // Encode Rn.
510 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
511
512 // Encode so_imm.
513 // Set bit I(25) to identify this is the immediate form of <shifter_op>
514 Binary |= 1 << ARMII::I_BitShift;
Evan Chenge7cbe412009-07-08 21:03:57 +0000515 Binary |= getMachineSoImmOpValue(V2);
Evan Cheng90922132008-11-06 02:25:39 +0000516 emitWordLE(Binary);
517}
518
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000519template<class CodeEmitter>
520void Emitter<CodeEmitter>::emitLEApcrelJTInstruction(const MachineInstr &MI) {
Evan Cheng4df60f52008-11-07 09:06:08 +0000521 // It's basically add r, pc, (LJTI - $+8)
Jim Grosbach764ab522009-08-11 15:33:49 +0000522
Evan Cheng4df60f52008-11-07 09:06:08 +0000523 const TargetInstrDesc &TID = MI.getDesc();
524
525 // Emit the 'add' instruction.
526 unsigned Binary = 0x4 << 21; // add: Insts{24-31} = 0b0100
527
528 // Set the conditional execution predicate
529 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
530
531 // Encode S bit if MI modifies CPSR.
532 Binary |= getAddrModeSBit(MI, TID);
533
534 // Encode Rd.
535 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
536
537 // Encode Rn which is PC.
538 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
539
540 // Encode the displacement.
Evan Cheng4df60f52008-11-07 09:06:08 +0000541 Binary |= 1 << ARMII::I_BitShift;
542 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
543
544 emitWordLE(Binary);
545}
546
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000547template<class CodeEmitter>
548void Emitter<CodeEmitter>::emitPseudoMoveInstruction(const MachineInstr &MI) {
Evan Chenga9562552008-11-14 20:09:11 +0000549 unsigned Opcode = MI.getDesc().Opcode;
550
551 // Part of binary is determined by TableGn.
552 unsigned Binary = getBinaryCodeForInstr(MI);
553
554 // Set the conditional execution predicate
555 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
556
557 // Encode S bit if MI modifies CPSR.
558 if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag)
559 Binary |= 1 << ARMII::S_BitShift;
560
561 // Encode register def if there is one.
562 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
563
564 // Encode the shift operation.
565 switch (Opcode) {
566 default: break;
567 case ARM::MOVrx:
568 // rrx
569 Binary |= 0x6 << 4;
570 break;
571 case ARM::MOVsrl_flag:
572 // lsr #1
573 Binary |= (0x2 << 4) | (1 << 7);
574 break;
575 case ARM::MOVsra_flag:
576 // asr #1
577 Binary |= (0x4 << 4) | (1 << 7);
578 break;
579 }
580
581 // Encode register Rm.
582 Binary |= getMachineOpValue(MI, 1);
583
584 emitWordLE(Binary);
585}
586
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000587template<class CodeEmitter>
588void Emitter<CodeEmitter>::addPCLabel(unsigned LabelID) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000589 DEBUG(errs() << " ** LPC" << LabelID << " @ "
590 << (void*)MCE.getCurrentPCValue() << '\n');
Evan Cheng83b5cf02008-11-05 23:22:34 +0000591 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
592}
593
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000594template<class CodeEmitter>
595void Emitter<CodeEmitter>::emitPseudoInstruction(const MachineInstr &MI) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000596 unsigned Opcode = MI.getDesc().Opcode;
597 switch (Opcode) {
598 default:
Evan Cheng5adb66a2009-09-28 09:14:39 +0000599 llvm_unreachable("ARMCodeEmitter::emitPseudoInstruction");
600 // FIXME: Add support for MOVimm32.
Evan Chengffa6d962008-11-13 23:36:57 +0000601 case TargetInstrInfo::INLINEASM: {
Evan Chenge3066ab2008-11-19 23:21:33 +0000602 // We allow inline assembler nodes with empty bodies - they can
603 // implicitly define registers, which is ok for JIT.
604 if (MI.getOperand(0).getSymbolName()[0]) {
Torok Edwin29fd0562009-07-12 07:15:17 +0000605 llvm_report_error("JIT does not support inline asm!");
Evan Chenge3066ab2008-11-19 23:21:33 +0000606 }
Evan Chengffa6d962008-11-13 23:36:57 +0000607 break;
608 }
609 case TargetInstrInfo::DBG_LABEL:
610 case TargetInstrInfo::EH_LABEL:
611 MCE.emitLabel(MI.getOperand(0).getImm());
612 break;
613 case TargetInstrInfo::IMPLICIT_DEF:
Evan Chengffa6d962008-11-13 23:36:57 +0000614 case ARM::DWARF_LOC:
615 // Do nothing.
616 break;
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000617 case ARM::CONSTPOOL_ENTRY:
618 emitConstPoolInstruction(MI);
619 break;
620 case ARM::PICADD: {
Evan Cheng25e04782008-11-04 00:50:32 +0000621 // Remember of the address of the PC label for relocation later.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000622 addPCLabel(MI.getOperand(2).getImm());
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000623 // PICADD is just an add instruction that implicitly read pc.
Evan Cheng437c1732008-11-07 22:30:53 +0000624 emitDataProcessingInstruction(MI, 0, ARM::PC);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000625 break;
626 }
627 case ARM::PICLDR:
628 case ARM::PICLDRB:
629 case ARM::PICSTR:
630 case ARM::PICSTRB: {
631 // Remember of the address of the PC label for relocation later.
632 addPCLabel(MI.getOperand(2).getImm());
633 // These are just load / store instructions that implicitly read pc.
Evan Cheng4df60f52008-11-07 09:06:08 +0000634 emitLoadStoreInstruction(MI, 0, ARM::PC);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000635 break;
636 }
637 case ARM::PICLDRH:
638 case ARM::PICLDRSH:
639 case ARM::PICLDRSB:
640 case ARM::PICSTRH: {
641 // Remember of the address of the PC label for relocation later.
642 addPCLabel(MI.getOperand(2).getImm());
643 // These are just load / store instructions that implicitly read pc.
644 emitMiscLoadStoreInstruction(MI, ARM::PC);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000645 break;
646 }
Evan Cheng90922132008-11-06 02:25:39 +0000647 case ARM::MOVi2pieces:
648 // Two instructions to materialize a constant.
649 emitMOVi2piecesInstruction(MI);
650 break;
Evan Cheng4df60f52008-11-07 09:06:08 +0000651 case ARM::LEApcrelJT:
652 // Materialize jumptable address.
653 emitLEApcrelJTInstruction(MI);
654 break;
Evan Chenga9562552008-11-14 20:09:11 +0000655 case ARM::MOVrx:
656 case ARM::MOVsrl_flag:
657 case ARM::MOVsra_flag:
658 emitPseudoMoveInstruction(MI);
659 break;
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000660 }
661}
662
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000663template<class CodeEmitter>
664unsigned Emitter<CodeEmitter>::getMachineSoRegOpValue(
665 const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000666 const TargetInstrDesc &TID,
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000667 const MachineOperand &MO,
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000668 unsigned OpIdx) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000669 unsigned Binary = getMachineOpValue(MI, MO);
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000670
671 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
672 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
673 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
674
675 // Encode the shift opcode.
676 unsigned SBits = 0;
677 unsigned Rs = MO1.getReg();
678 if (Rs) {
679 // Set shift operand (bit[7:4]).
680 // LSL - 0001
681 // LSR - 0011
682 // ASR - 0101
683 // ROR - 0111
684 // RRX - 0110 and bit[11:8] clear.
685 switch (SOpc) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000686 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000687 case ARM_AM::lsl: SBits = 0x1; break;
688 case ARM_AM::lsr: SBits = 0x3; break;
689 case ARM_AM::asr: SBits = 0x5; break;
690 case ARM_AM::ror: SBits = 0x7; break;
691 case ARM_AM::rrx: SBits = 0x6; break;
692 }
693 } else {
694 // Set shift operand (bit[6:4]).
695 // LSL - 000
696 // LSR - 010
697 // ASR - 100
698 // ROR - 110
699 switch (SOpc) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000700 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000701 case ARM_AM::lsl: SBits = 0x0; break;
702 case ARM_AM::lsr: SBits = 0x2; break;
703 case ARM_AM::asr: SBits = 0x4; break;
704 case ARM_AM::ror: SBits = 0x6; break;
705 }
706 }
707 Binary |= SBits << 4;
708 if (SOpc == ARM_AM::rrx)
709 return Binary;
710
711 // Encode the shift operation Rs or shift_imm (except rrx).
712 if (Rs) {
713 // Encode Rs bit[11:8].
714 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
715 return Binary |
716 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
717 }
718
719 // Encode shift_imm bit[11:7].
720 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
721}
722
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000723template<class CodeEmitter>
724unsigned Emitter<CodeEmitter>::getMachineSoImmOpValue(unsigned SoImm) {
Evan Chenge7cbe412009-07-08 21:03:57 +0000725 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
726 assert(SoImmVal != -1 && "Not a valid so_imm value!");
727
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000728 // Encode rotate_imm.
Evan Chenge7cbe412009-07-08 21:03:57 +0000729 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
Evan Cheng97f48c32008-11-06 22:15:19 +0000730 << ARMII::SoRotImmShift;
731
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000732 // Encode immed_8.
Evan Chenge7cbe412009-07-08 21:03:57 +0000733 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000734 return Binary;
735}
736
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000737template<class CodeEmitter>
738unsigned Emitter<CodeEmitter>::getAddrModeSBit(const MachineInstr &MI,
739 const TargetInstrDesc &TID) const {
Evan Cheng97c573d2008-11-20 02:25:51 +0000740 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
Evan Cheng49a9f292008-09-12 22:45:55 +0000741 const MachineOperand &MO = MI.getOperand(i-1);
Dan Gohmand735b802008-10-03 15:45:36 +0000742 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
Evan Cheng49a9f292008-09-12 22:45:55 +0000743 return 1 << ARMII::S_BitShift;
744 }
745 return 0;
746}
747
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000748template<class CodeEmitter>
749void Emitter<CodeEmitter>::emitDataProcessingInstruction(
750 const MachineInstr &MI,
Evan Cheng437c1732008-11-07 22:30:53 +0000751 unsigned ImplicitRd,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000752 unsigned ImplicitRn) {
Evan Chengedda31c2008-11-05 18:35:52 +0000753 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +0000754
Evan Cheng36a0aeb2009-07-06 22:23:46 +0000755 if (TID.Opcode == ARM::BFC) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +0000756 llvm_report_error("ARMv6t2 JIT is not yet supported.");
Evan Cheng36a0aeb2009-07-06 22:23:46 +0000757 }
758
Evan Chengedda31c2008-11-05 18:35:52 +0000759 // Part of binary is determined by TableGn.
760 unsigned Binary = getBinaryCodeForInstr(MI);
761
Jim Grosbach33412622008-10-07 19:05:35 +0000762 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000763 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000764
Evan Cheng49a9f292008-09-12 22:45:55 +0000765 // Encode S bit if MI modifies CPSR.
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +0000766 Binary |= getAddrModeSBit(MI, TID);
Evan Cheng49a9f292008-09-12 22:45:55 +0000767
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000768 // Encode register def if there is one.
Evan Cheng49a9f292008-09-12 22:45:55 +0000769 unsigned NumDefs = TID.getNumDefs();
Evan Chenga964b7d2008-09-12 23:15:39 +0000770 unsigned OpIdx = 0;
Evan Cheng437c1732008-11-07 22:30:53 +0000771 if (NumDefs)
772 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
773 else if (ImplicitRd)
774 // Special handling for implicit use (e.g. PC).
775 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
776 << ARMII::RegRdShift);
Evan Cheng7602e112008-09-02 06:52:38 +0000777
Evan Chengd87293c2008-11-06 08:47:38 +0000778 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
779 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
780 ++OpIdx;
781
Jim Grosbachefd30ba2008-10-01 18:16:49 +0000782 // Encode first non-shifter register operand if there is one.
Evan Chengedda31c2008-11-05 18:35:52 +0000783 bool isUnary = TID.TSFlags & ARMII::UnaryDP;
784 if (!isUnary) {
Evan Cheng83b5cf02008-11-05 23:22:34 +0000785 if (ImplicitRn)
786 // Special handling for implicit use (e.g. PC).
787 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
Evan Chengedda31c2008-11-05 18:35:52 +0000788 << ARMII::RegRnShift);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000789 else {
790 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
791 ++OpIdx;
792 }
Evan Cheng7602e112008-09-02 06:52:38 +0000793 }
794
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000795 // Encode shifter operand.
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000796 const MachineOperand &MO = MI.getOperand(OpIdx);
Evan Chengedda31c2008-11-05 18:35:52 +0000797 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000798 // Encode SoReg.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000799 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
Evan Chengedda31c2008-11-05 18:35:52 +0000800 return;
801 }
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000802
Evan Chengedda31c2008-11-05 18:35:52 +0000803 if (MO.isReg()) {
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000804 // Encode register Rm.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000805 emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
Evan Chengedda31c2008-11-05 18:35:52 +0000806 return;
807 }
Evan Cheng7602e112008-09-02 06:52:38 +0000808
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000809 // Encode so_imm.
Evan Chenge7cbe412009-07-08 21:03:57 +0000810 Binary |= getMachineSoImmOpValue((unsigned)MO.getImm());
Evan Chengedda31c2008-11-05 18:35:52 +0000811
Evan Cheng83b5cf02008-11-05 23:22:34 +0000812 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000813}
814
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000815template<class CodeEmitter>
816void Emitter<CodeEmitter>::emitLoadStoreInstruction(
817 const MachineInstr &MI,
Evan Cheng4df60f52008-11-07 09:06:08 +0000818 unsigned ImplicitRd,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000819 unsigned ImplicitRn) {
Evan Cheng05c356e2008-11-08 01:44:13 +0000820 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng148cad82008-11-13 07:34:59 +0000821 unsigned Form = TID.TSFlags & ARMII::FormMask;
822 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +0000823
Evan Chengedda31c2008-11-05 18:35:52 +0000824 // Part of binary is determined by TableGn.
825 unsigned Binary = getBinaryCodeForInstr(MI);
826
Jim Grosbach33412622008-10-07 19:05:35 +0000827 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000828 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +0000829
Evan Cheng4df60f52008-11-07 09:06:08 +0000830 unsigned OpIdx = 0;
Evan Cheng148cad82008-11-13 07:34:59 +0000831
832 // Operand 0 of a pre- and post-indexed store is the address base
833 // writeback. Skip it.
834 bool Skipped = false;
835 if (IsPrePost && Form == ARMII::StFrm) {
836 ++OpIdx;
837 Skipped = true;
838 }
839
840 // Set first operand
Evan Cheng4df60f52008-11-07 09:06:08 +0000841 if (ImplicitRd)
842 // Special handling for implicit use (e.g. PC).
843 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
844 << ARMII::RegRdShift);
845 else
846 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000847
848 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +0000849 if (ImplicitRn)
850 // Special handling for implicit use (e.g. PC).
851 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
852 << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +0000853 else
854 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000855
Evan Cheng05c356e2008-11-08 01:44:13 +0000856 // If this is a two-address operand, skip it. e.g. LDR_PRE.
Evan Cheng148cad82008-11-13 07:34:59 +0000857 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +0000858 ++OpIdx;
859
Evan Cheng83b5cf02008-11-05 23:22:34 +0000860 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +0000861 unsigned AM2Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000862 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +0000863
Evan Chenge7de7e32008-09-13 01:44:01 +0000864 // Set bit U(23) according to sign of immed value (positive or negative).
Evan Cheng83b5cf02008-11-05 23:22:34 +0000865 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
Evan Chenge7de7e32008-09-13 01:44:01 +0000866 ARMII::U_BitShift);
Evan Cheng7602e112008-09-02 06:52:38 +0000867 if (!MO2.getReg()) { // is immediate
Evan Cheng83b5cf02008-11-05 23:22:34 +0000868 if (ARM_AM::getAM2Offset(AM2Opc))
Evan Cheng7602e112008-09-02 06:52:38 +0000869 // Set the value of offset_12 field
Evan Cheng83b5cf02008-11-05 23:22:34 +0000870 Binary |= ARM_AM::getAM2Offset(AM2Opc);
871 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +0000872 return;
Evan Cheng7602e112008-09-02 06:52:38 +0000873 }
874
875 // Set bit I(25), because this is not in immediate enconding.
876 Binary |= 1 << ARMII::I_BitShift;
877 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
878 // Set bit[3:0] to the corresponding Rm register
879 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
880
Evan Cheng70632912008-11-12 07:34:37 +0000881 // If this instr is in scaled register offset/index instruction, set
Evan Cheng7602e112008-09-02 06:52:38 +0000882 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000883 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
Evan Cheng70632912008-11-12 07:34:37 +0000884 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
885 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
Evan Cheng7602e112008-09-02 06:52:38 +0000886 }
887
Evan Cheng83b5cf02008-11-05 23:22:34 +0000888 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000889}
890
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000891template<class CodeEmitter>
892void Emitter<CodeEmitter>::emitMiscLoadStoreInstruction(const MachineInstr &MI,
893 unsigned ImplicitRn) {
Evan Cheng05c356e2008-11-08 01:44:13 +0000894 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng148cad82008-11-13 07:34:59 +0000895 unsigned Form = TID.TSFlags & ARMII::FormMask;
896 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +0000897
Evan Chengedda31c2008-11-05 18:35:52 +0000898 // Part of binary is determined by TableGn.
899 unsigned Binary = getBinaryCodeForInstr(MI);
900
Jim Grosbach33412622008-10-07 19:05:35 +0000901 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000902 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +0000903
Evan Cheng148cad82008-11-13 07:34:59 +0000904 unsigned OpIdx = 0;
905
906 // Operand 0 of a pre- and post-indexed store is the address base
907 // writeback. Skip it.
908 bool Skipped = false;
909 if (IsPrePost && Form == ARMII::StMiscFrm) {
910 ++OpIdx;
911 Skipped = true;
912 }
913
Evan Cheng7602e112008-09-02 06:52:38 +0000914 // Set first operand
Evan Cheng148cad82008-11-13 07:34:59 +0000915 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000916
Evan Cheng358dec52009-06-15 08:28:29 +0000917 // Skip LDRD and STRD's second operand.
918 if (TID.Opcode == ARM::LDRD || TID.Opcode == ARM::STRD)
919 ++OpIdx;
920
Evan Cheng7602e112008-09-02 06:52:38 +0000921 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +0000922 if (ImplicitRn)
923 // Special handling for implicit use (e.g. PC).
924 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
925 << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +0000926 else
927 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000928
Evan Cheng05c356e2008-11-08 01:44:13 +0000929 // If this is a two-address operand, skip it. e.g. LDRH_POST.
Evan Cheng148cad82008-11-13 07:34:59 +0000930 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +0000931 ++OpIdx;
932
Evan Cheng83b5cf02008-11-05 23:22:34 +0000933 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +0000934 unsigned AM3Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000935 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +0000936
Evan Chenge7de7e32008-09-13 01:44:01 +0000937 // Set bit U(23) according to sign of immed value (positive or negative)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000938 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
Evan Cheng7602e112008-09-02 06:52:38 +0000939 ARMII::U_BitShift);
940
941 // If this instr is in register offset/index encoding, set bit[3:0]
942 // to the corresponding Rm register.
943 if (MO2.getReg()) {
944 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
Evan Cheng83b5cf02008-11-05 23:22:34 +0000945 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +0000946 return;
Evan Cheng7602e112008-09-02 06:52:38 +0000947 }
948
Evan Chengd87293c2008-11-06 08:47:38 +0000949 // This instr is in immediate offset/index encoding, set bit 22 to 1.
Evan Cheng97f48c32008-11-06 22:15:19 +0000950 Binary |= 1 << ARMII::AM3_I_BitShift;
Evan Cheng83b5cf02008-11-05 23:22:34 +0000951 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
Evan Cheng7602e112008-09-02 06:52:38 +0000952 // Set operands
Evan Cheng70632912008-11-12 07:34:37 +0000953 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
954 Binary |= (ImmOffs & 0xF); // immedL
Evan Cheng7602e112008-09-02 06:52:38 +0000955 }
956
Evan Cheng83b5cf02008-11-05 23:22:34 +0000957 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000958}
959
Evan Chengcd8e66a2008-11-11 21:48:44 +0000960static unsigned getAddrModeUPBits(unsigned Mode) {
961 unsigned Binary = 0;
Evan Cheng7602e112008-09-02 06:52:38 +0000962
963 // Set addressing mode by modifying bits U(23) and P(24)
964 // IA - Increment after - bit U = 1 and bit P = 0
965 // IB - Increment before - bit U = 1 and bit P = 1
966 // DA - Decrement after - bit U = 0 and bit P = 0
967 // DB - Decrement before - bit U = 0 and bit P = 1
Evan Cheng7602e112008-09-02 06:52:38 +0000968 switch (Mode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000969 default: llvm_unreachable("Unknown addressing sub-mode!");
Evan Cheng10bf7342009-09-09 23:55:03 +0000970 case ARM_AM::da: break;
Evan Cheng97f48c32008-11-06 22:15:19 +0000971 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
972 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
973 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
Evan Cheng7602e112008-09-02 06:52:38 +0000974 }
975
Evan Chengcd8e66a2008-11-11 21:48:44 +0000976 return Binary;
977}
978
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000979template<class CodeEmitter>
980void Emitter<CodeEmitter>::emitLoadStoreMultipleInstruction(
981 const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +0000982 // Part of binary is determined by TableGn.
983 unsigned Binary = getBinaryCodeForInstr(MI);
984
985 // Set the conditional execution predicate
986 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
987
988 // Set base address operand
989 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
990
991 // Set addressing mode by modifying bits U(23) and P(24)
992 const MachineOperand &MO = MI.getOperand(1);
993 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
994
Evan Cheng7602e112008-09-02 06:52:38 +0000995 // Set bit W(21)
996 if (ARM_AM::getAM4WBFlag(MO.getImm()))
Evan Cheng97f48c32008-11-06 22:15:19 +0000997 Binary |= 0x1 << ARMII::W_BitShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000998
999 // Set registers
1000 for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
1001 const MachineOperand &MO = MI.getOperand(i);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001002 if (!MO.isReg() || MO.isImplicit())
1003 break;
Evan Cheng7602e112008-09-02 06:52:38 +00001004 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
1005 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
1006 RegNum < 16);
1007 Binary |= 0x1 << RegNum;
1008 }
1009
Evan Cheng83b5cf02008-11-05 23:22:34 +00001010 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +00001011}
1012
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001013template<class CodeEmitter>
1014void Emitter<CodeEmitter>::emitMulFrmInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001015 const TargetInstrDesc &TID = MI.getDesc();
1016
1017 // Part of binary is determined by TableGn.
1018 unsigned Binary = getBinaryCodeForInstr(MI);
1019
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001020 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001021 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001022
1023 // Encode S bit if MI modifies CPSR.
1024 Binary |= getAddrModeSBit(MI, TID);
1025
1026 // 32x32->64bit operations have two destination registers. The number
1027 // of register definitions will tell us if that's what we're dealing with.
Evan Cheng97f48c32008-11-06 22:15:19 +00001028 unsigned OpIdx = 0;
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001029 if (TID.getNumDefs() == 2)
1030 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
1031
1032 // Encode Rd
1033 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
1034
1035 // Encode Rm
1036 Binary |= getMachineOpValue(MI, OpIdx++);
1037
1038 // Encode Rs
1039 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
1040
Evan Chengfbc9d412008-11-06 01:21:28 +00001041 // Many multiple instructions (e.g. MLA) have three src operands. Encode
1042 // it as Rn (for multiply, that's in the same offset as RdLo.
Evan Cheng97f48c32008-11-06 22:15:19 +00001043 if (TID.getNumOperands() > OpIdx &&
1044 !TID.OpInfo[OpIdx].isPredicate() &&
1045 !TID.OpInfo[OpIdx].isOptionalDef())
1046 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
1047
1048 emitWordLE(Binary);
1049}
1050
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001051template<class CodeEmitter>
1052void Emitter<CodeEmitter>::emitExtendInstruction(const MachineInstr &MI) {
Evan Cheng97f48c32008-11-06 22:15:19 +00001053 const TargetInstrDesc &TID = MI.getDesc();
1054
1055 // Part of binary is determined by TableGn.
1056 unsigned Binary = getBinaryCodeForInstr(MI);
1057
1058 // Set the conditional execution predicate
1059 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1060
1061 unsigned OpIdx = 0;
1062
1063 // Encode Rd
1064 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1065
1066 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
1067 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1068 if (MO2.isReg()) {
1069 // Two register operand form.
1070 // Encode Rn.
1071 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
1072
1073 // Encode Rm.
1074 Binary |= getMachineOpValue(MI, MO2);
1075 ++OpIdx;
1076 } else {
1077 Binary |= getMachineOpValue(MI, MO1);
1078 }
1079
1080 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
1081 if (MI.getOperand(OpIdx).isImm() &&
1082 !TID.OpInfo[OpIdx].isPredicate() &&
1083 !TID.OpInfo[OpIdx].isOptionalDef())
1084 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
Evan Chengfbc9d412008-11-06 01:21:28 +00001085
Evan Cheng83b5cf02008-11-05 23:22:34 +00001086 emitWordLE(Binary);
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001087}
1088
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001089template<class CodeEmitter>
1090void Emitter<CodeEmitter>::emitMiscArithInstruction(const MachineInstr &MI) {
Evan Cheng8b59db32008-11-07 01:41:35 +00001091 const TargetInstrDesc &TID = MI.getDesc();
1092
1093 // Part of binary is determined by TableGn.
1094 unsigned Binary = getBinaryCodeForInstr(MI);
1095
1096 // Set the conditional execution predicate
1097 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1098
1099 unsigned OpIdx = 0;
1100
1101 // Encode Rd
1102 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1103
1104 const MachineOperand &MO = MI.getOperand(OpIdx++);
1105 if (OpIdx == TID.getNumOperands() ||
1106 TID.OpInfo[OpIdx].isPredicate() ||
1107 TID.OpInfo[OpIdx].isOptionalDef()) {
1108 // Encode Rm and it's done.
1109 Binary |= getMachineOpValue(MI, MO);
1110 emitWordLE(Binary);
1111 return;
1112 }
1113
1114 // Encode Rn.
1115 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1116
1117 // Encode Rm.
1118 Binary |= getMachineOpValue(MI, OpIdx++);
1119
1120 // Encode shift_imm.
1121 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
1122 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1123 Binary |= ShiftAmt << ARMII::ShiftShift;
Jim Grosbach764ab522009-08-11 15:33:49 +00001124
Evan Cheng8b59db32008-11-07 01:41:35 +00001125 emitWordLE(Binary);
1126}
1127
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001128template<class CodeEmitter>
1129void Emitter<CodeEmitter>::emitBranchInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001130 const TargetInstrDesc &TID = MI.getDesc();
1131
Torok Edwindac237e2009-07-08 20:53:28 +00001132 if (TID.Opcode == ARM::TPsoft) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001133 llvm_unreachable("ARM::TPsoft FIXME"); // FIXME
Torok Edwindac237e2009-07-08 20:53:28 +00001134 }
Evan Cheng12c3a532008-11-06 17:48:05 +00001135
Evan Cheng7602e112008-09-02 06:52:38 +00001136 // Part of binary is determined by TableGn.
1137 unsigned Binary = getBinaryCodeForInstr(MI);
1138
Evan Chengedda31c2008-11-05 18:35:52 +00001139 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001140 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001141
1142 // Set signed_immed_24 field
1143 Binary |= getMachineOpValue(MI, 0);
1144
Evan Cheng83b5cf02008-11-05 23:22:34 +00001145 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +00001146}
1147
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001148template<class CodeEmitter>
1149void Emitter<CodeEmitter>::emitInlineJumpTable(unsigned JTIndex) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001150 // Remember the base address of the inline jump table.
Evan Cheng5788d1a2008-12-10 02:32:19 +00001151 uintptr_t JTBase = MCE.getCurrentPCValue();
Evan Cheng437c1732008-11-07 22:30:53 +00001152 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
Chris Lattner893e1c92009-08-23 06:49:22 +00001153 DEBUG(errs() << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase
1154 << '\n');
Evan Cheng4df60f52008-11-07 09:06:08 +00001155
1156 // Now emit the jump table entries.
1157 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1158 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1159 if (IsPIC)
1160 // DestBB address - JT base.
Evan Cheng437c1732008-11-07 22:30:53 +00001161 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
Evan Cheng4df60f52008-11-07 09:06:08 +00001162 else
1163 // Absolute DestBB address.
1164 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1165 emitWordLE(0);
1166 }
1167}
1168
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001169template<class CodeEmitter>
1170void Emitter<CodeEmitter>::emitMiscBranchInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001171 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +00001172
Evan Cheng437c1732008-11-07 22:30:53 +00001173 // Handle jump tables.
Evan Cheng90daf4d2009-07-25 00:13:11 +00001174 if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
Evan Cheng437c1732008-11-07 22:30:53 +00001175 // First emit a ldr pc, [] instruction.
1176 emitDataProcessingInstruction(MI, ARM::PC);
1177
1178 // Then emit the inline jump table.
Evan Chengc9a41532009-07-08 00:05:05 +00001179 unsigned JTIndex =
Evan Cheng90daf4d2009-07-25 00:13:11 +00001180 (TID.Opcode == ARM::BR_JTr)
Evan Cheng437c1732008-11-07 22:30:53 +00001181 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1182 emitInlineJumpTable(JTIndex);
1183 return;
Evan Cheng90daf4d2009-07-25 00:13:11 +00001184 } else if (TID.Opcode == ARM::BR_JTm) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001185 // First emit a ldr pc, [] instruction.
1186 emitLoadStoreInstruction(MI, ARM::PC);
1187
1188 // Then emit the inline jump table.
Evan Cheng437c1732008-11-07 22:30:53 +00001189 emitInlineJumpTable(MI.getOperand(3).getIndex());
Evan Cheng4df60f52008-11-07 09:06:08 +00001190 return;
1191 }
1192
Evan Chengedda31c2008-11-05 18:35:52 +00001193 // Part of binary is determined by TableGn.
1194 unsigned Binary = getBinaryCodeForInstr(MI);
1195
1196 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001197 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001198
1199 if (TID.Opcode == ARM::BX_RET)
1200 // The return register is LR.
1201 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
Jim Grosbach764ab522009-08-11 15:33:49 +00001202 else
Evan Chengedda31c2008-11-05 18:35:52 +00001203 // otherwise, set the return register
1204 Binary |= getMachineOpValue(MI, 0);
1205
Evan Cheng83b5cf02008-11-05 23:22:34 +00001206 emitWordLE(Binary);
Evan Cheng148b6a42007-07-05 21:15:40 +00001207}
Evan Cheng7602e112008-09-02 06:52:38 +00001208
Evan Cheng80a11982008-11-12 06:41:41 +00001209static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) {
Evan Chengd06d48d2008-11-12 02:19:38 +00001210 unsigned RegD = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001211 unsigned Binary = 0;
Evan Chengd06d48d2008-11-12 02:19:38 +00001212 bool isSPVFP = false;
Evan Cheng8295d992009-07-22 05:55:18 +00001213 RegD = ARMRegisterInfo::getRegisterNumbering(RegD, &isSPVFP);
Evan Chengd06d48d2008-11-12 02:19:38 +00001214 if (!isSPVFP)
1215 Binary |= RegD << ARMII::RegRdShift;
1216 else {
1217 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1218 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1219 }
Evan Cheng80a11982008-11-12 06:41:41 +00001220 return Binary;
1221}
Evan Cheng78be83d2008-11-11 19:40:26 +00001222
Evan Cheng80a11982008-11-12 06:41:41 +00001223static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) {
Evan Chengd06d48d2008-11-12 02:19:38 +00001224 unsigned RegN = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001225 unsigned Binary = 0;
1226 bool isSPVFP = false;
Evan Cheng8295d992009-07-22 05:55:18 +00001227 RegN = ARMRegisterInfo::getRegisterNumbering(RegN, &isSPVFP);
Evan Chengd06d48d2008-11-12 02:19:38 +00001228 if (!isSPVFP)
1229 Binary |= RegN << ARMII::RegRnShift;
1230 else {
1231 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1232 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1233 }
Evan Cheng80a11982008-11-12 06:41:41 +00001234 return Binary;
1235}
Evan Chengd06d48d2008-11-12 02:19:38 +00001236
Evan Cheng80a11982008-11-12 06:41:41 +00001237static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) {
1238 unsigned RegM = MI.getOperand(OpIdx).getReg();
1239 unsigned Binary = 0;
1240 bool isSPVFP = false;
Evan Cheng8295d992009-07-22 05:55:18 +00001241 RegM = ARMRegisterInfo::getRegisterNumbering(RegM, &isSPVFP);
Evan Cheng80a11982008-11-12 06:41:41 +00001242 if (!isSPVFP)
1243 Binary |= RegM;
1244 else {
1245 Binary |= ((RegM & 0x1E) >> 1);
1246 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
Evan Cheng78be83d2008-11-11 19:40:26 +00001247 }
Evan Cheng80a11982008-11-12 06:41:41 +00001248 return Binary;
1249}
1250
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001251template<class CodeEmitter>
1252void Emitter<CodeEmitter>::emitVFPArithInstruction(const MachineInstr &MI) {
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001253 const TargetInstrDesc &TID = MI.getDesc();
1254
1255 // Part of binary is determined by TableGn.
1256 unsigned Binary = getBinaryCodeForInstr(MI);
1257
1258 // Set the conditional execution predicate
1259 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1260
1261 unsigned OpIdx = 0;
1262 assert((Binary & ARMII::D_BitShift) == 0 &&
1263 (Binary & ARMII::N_BitShift) == 0 &&
1264 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1265
1266 // Encode Dd / Sd.
1267 Binary |= encodeVFPRd(MI, OpIdx++);
1268
1269 // If this is a two-address operand, skip it, e.g. FMACD.
1270 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1271 ++OpIdx;
1272
1273 // Encode Dn / Sn.
1274 if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
Evan Cheng3f4924e2008-11-12 08:14:21 +00001275 Binary |= encodeVFPRn(MI, OpIdx++);
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001276
1277 if (OpIdx == TID.getNumOperands() ||
1278 TID.OpInfo[OpIdx].isPredicate() ||
1279 TID.OpInfo[OpIdx].isOptionalDef()) {
1280 // FCMPEZD etc. has only one operand.
1281 emitWordLE(Binary);
1282 return;
1283 }
1284
1285 // Encode Dm / Sm.
1286 Binary |= encodeVFPRm(MI, OpIdx);
Jim Grosbach764ab522009-08-11 15:33:49 +00001287
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001288 emitWordLE(Binary);
1289}
1290
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001291template<class CodeEmitter>
1292void Emitter<CodeEmitter>::emitVFPConversionInstruction(
1293 const MachineInstr &MI) {
Evan Cheng80a11982008-11-12 06:41:41 +00001294 const TargetInstrDesc &TID = MI.getDesc();
1295 unsigned Form = TID.TSFlags & ARMII::FormMask;
1296
1297 // Part of binary is determined by TableGn.
1298 unsigned Binary = getBinaryCodeForInstr(MI);
1299
1300 // Set the conditional execution predicate
1301 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1302
1303 switch (Form) {
1304 default: break;
1305 case ARMII::VFPConv1Frm:
1306 case ARMII::VFPConv2Frm:
1307 case ARMII::VFPConv3Frm:
1308 // Encode Dd / Sd.
1309 Binary |= encodeVFPRd(MI, 0);
1310 break;
1311 case ARMII::VFPConv4Frm:
1312 // Encode Dn / Sn.
1313 Binary |= encodeVFPRn(MI, 0);
1314 break;
1315 case ARMII::VFPConv5Frm:
1316 // Encode Dm / Sm.
1317 Binary |= encodeVFPRm(MI, 0);
1318 break;
1319 }
1320
1321 switch (Form) {
1322 default: break;
1323 case ARMII::VFPConv1Frm:
1324 // Encode Dm / Sm.
1325 Binary |= encodeVFPRm(MI, 1);
Evan Cheng67fd91f2008-11-13 07:46:59 +00001326 break;
Evan Cheng80a11982008-11-12 06:41:41 +00001327 case ARMII::VFPConv2Frm:
1328 case ARMII::VFPConv3Frm:
1329 // Encode Dn / Sn.
1330 Binary |= encodeVFPRn(MI, 1);
1331 break;
1332 case ARMII::VFPConv4Frm:
1333 case ARMII::VFPConv5Frm:
1334 // Encode Dd / Sd.
1335 Binary |= encodeVFPRd(MI, 1);
1336 break;
1337 }
1338
1339 if (Form == ARMII::VFPConv5Frm)
1340 // Encode Dn / Sn.
1341 Binary |= encodeVFPRn(MI, 2);
1342 else if (Form == ARMII::VFPConv3Frm)
1343 // Encode Dm / Sm.
1344 Binary |= encodeVFPRm(MI, 2);
Evan Cheng78be83d2008-11-11 19:40:26 +00001345
1346 emitWordLE(Binary);
1347}
1348
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001349template<class CodeEmitter>
1350void Emitter<CodeEmitter>::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001351 // Part of binary is determined by TableGn.
1352 unsigned Binary = getBinaryCodeForInstr(MI);
1353
1354 // Set the conditional execution predicate
1355 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1356
1357 unsigned OpIdx = 0;
1358
1359 // Encode Dd / Sd.
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001360 Binary |= encodeVFPRd(MI, OpIdx++);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001361
1362 // Encode address base.
1363 const MachineOperand &Base = MI.getOperand(OpIdx++);
1364 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1365
1366 // If there is a non-zero immediate offset, encode it.
1367 if (Base.isReg()) {
1368 const MachineOperand &Offset = MI.getOperand(OpIdx);
1369 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1370 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1371 Binary |= 1 << ARMII::U_BitShift;
Evan Cheng607f1b42008-11-12 08:21:12 +00001372 Binary |= ImmOffs;
Evan Chengcd8e66a2008-11-11 21:48:44 +00001373 emitWordLE(Binary);
1374 return;
1375 }
1376 }
1377
1378 // If immediate offset is omitted, default to +0.
1379 Binary |= 1 << ARMII::U_BitShift;
1380
1381 emitWordLE(Binary);
1382}
1383
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001384template<class CodeEmitter>
1385void Emitter<CodeEmitter>::emitVFPLoadStoreMultipleInstruction(
1386 const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001387 // Part of binary is determined by TableGn.
1388 unsigned Binary = getBinaryCodeForInstr(MI);
1389
1390 // Set the conditional execution predicate
1391 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1392
1393 // Set base address operand
1394 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
1395
1396 // Set addressing mode by modifying bits U(23) and P(24)
1397 const MachineOperand &MO = MI.getOperand(1);
1398 Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm()));
1399
1400 // Set bit W(21)
1401 if (ARM_AM::getAM5WBFlag(MO.getImm()))
1402 Binary |= 0x1 << ARMII::W_BitShift;
1403
1404 // First register is encoded in Dd.
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001405 Binary |= encodeVFPRd(MI, 4);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001406
1407 // Number of registers are encoded in offset field.
1408 unsigned NumRegs = 1;
1409 for (unsigned i = 5, e = MI.getNumOperands(); i != e; ++i) {
1410 const MachineOperand &MO = MI.getOperand(i);
1411 if (!MO.isReg() || MO.isImplicit())
1412 break;
1413 ++NumRegs;
1414 }
1415 Binary |= NumRegs * 2;
1416
1417 emitWordLE(Binary);
1418}
1419
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001420template<class CodeEmitter>
1421void Emitter<CodeEmitter>::emitMiscInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001422 // Part of binary is determined by TableGn.
1423 unsigned Binary = getBinaryCodeForInstr(MI);
1424
1425 // Set the conditional execution predicate
1426 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1427
1428 emitWordLE(Binary);
1429}
1430
Evan Cheng7602e112008-09-02 06:52:38 +00001431#include "ARMGenCodeEmitter.inc"