blob: 6275f516697f856e50e97c12baf2790e9aa32b66 [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"
Evan Cheng148b6a42007-07-05 21:15:40 +000034#include "llvm/CodeGen/Passes.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000035#include "llvm/ADT/Statistic.h"
36#include "llvm/Support/Compiler.h"
Evan Cheng42d5ee062008-09-13 01:15:21 +000037#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000038#include "llvm/Support/ErrorHandling.h"
39#include "llvm/Support/raw_ostream.h"
Evan Cheng4df60f52008-11-07 09:06:08 +000040#ifndef NDEBUG
41#include <iomanip>
42#endif
Evan Cheng148b6a42007-07-05 21:15:40 +000043using namespace llvm;
44
45STATISTIC(NumEmitted, "Number of machine instructions emitted");
46
47namespace {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000048
49 class ARMCodeEmitter {
50 public:
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000051 /// getBinaryCodeForInstr - This function, generated by the
52 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
53 /// machine instructions.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000054 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
55 };
56
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000057 template<class CodeEmitter>
58 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
59 public ARMCodeEmitter {
Evan Cheng057d0c32008-09-18 07:28:19 +000060 ARMJITInfo *JTI;
61 const ARMInstrInfo *II;
62 const TargetData *TD;
Evan Cheng08669742009-09-10 01:23:53 +000063 const ARMSubtarget *Subtarget;
Evan Cheng057d0c32008-09-18 07:28:19 +000064 TargetMachine &TM;
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000065 CodeEmitter &MCE;
Evan Cheng938b9d82008-10-31 19:55:13 +000066 const std::vector<MachineConstantPoolEntry> *MCPEs;
Evan Cheng4df60f52008-11-07 09:06:08 +000067 const std::vector<MachineJumpTableEntry> *MJTEs;
68 bool IsPIC;
69
Evan Cheng148b6a42007-07-05 21:15:40 +000070 public:
71 static char ID;
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000072 explicit Emitter(TargetMachine &tm, CodeEmitter &mce)
Evan Cheng057d0c32008-09-18 07:28:19 +000073 : MachineFunctionPass(&ID), JTI(0), II(0), TD(0), TM(tm),
Evan Cheng4df60f52008-11-07 09:06:08 +000074 MCE(mce), MCPEs(0), MJTEs(0),
75 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000076 Emitter(TargetMachine &tm, CodeEmitter &mce,
Evan Cheng148b6a42007-07-05 21:15:40 +000077 const ARMInstrInfo &ii, const TargetData &td)
Evan Cheng057d0c32008-09-18 07:28:19 +000078 : MachineFunctionPass(&ID), JTI(0), II(&ii), TD(&td), TM(tm),
Evan Cheng4df60f52008-11-07 09:06:08 +000079 MCE(mce), MCPEs(0), MJTEs(0),
80 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Evan Cheng148b6a42007-07-05 21:15:40 +000081
82 bool runOnMachineFunction(MachineFunction &MF);
83
84 virtual const char *getPassName() const {
85 return "ARM Machine Code Emitter";
86 }
87
88 void emitInstruction(const MachineInstr &MI);
Evan Cheng7602e112008-09-02 06:52:38 +000089
90 private:
Evan Cheng057d0c32008-09-18 07:28:19 +000091
Evan Cheng83b5cf02008-11-05 23:22:34 +000092 void emitWordLE(unsigned Binary);
93
Evan Chengcb5201f2008-11-11 22:19:31 +000094 void emitDWordLE(uint64_t Binary);
95
Evan Cheng057d0c32008-09-18 07:28:19 +000096 void emitConstPoolInstruction(const MachineInstr &MI);
97
Evan Cheng90922132008-11-06 02:25:39 +000098 void emitMOVi2piecesInstruction(const MachineInstr &MI);
99
Evan Cheng4df60f52008-11-07 09:06:08 +0000100 void emitLEApcrelJTInstruction(const MachineInstr &MI);
101
Evan Chenga9562552008-11-14 20:09:11 +0000102 void emitPseudoMoveInstruction(const MachineInstr &MI);
103
Evan Cheng83b5cf02008-11-05 23:22:34 +0000104 void addPCLabel(unsigned LabelID);
105
Evan Cheng057d0c32008-09-18 07:28:19 +0000106 void emitPseudoInstruction(const MachineInstr &MI);
107
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000108 unsigned getMachineSoRegOpValue(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000109 const TargetInstrDesc &TID,
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000110 const MachineOperand &MO,
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000111 unsigned OpIdx);
112
Evan Cheng90922132008-11-06 02:25:39 +0000113 unsigned getMachineSoImmOpValue(unsigned SoImm);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000114
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +0000115 unsigned getAddrModeSBit(const MachineInstr &MI,
116 const TargetInstrDesc &TID) const;
Evan Cheng49a9f292008-09-12 22:45:55 +0000117
Evan Cheng83b5cf02008-11-05 23:22:34 +0000118 void emitDataProcessingInstruction(const MachineInstr &MI,
Evan Cheng437c1732008-11-07 22:30:53 +0000119 unsigned ImplicitRd = 0,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000120 unsigned ImplicitRn = 0);
Evan Cheng7602e112008-09-02 06:52:38 +0000121
Evan Cheng83b5cf02008-11-05 23:22:34 +0000122 void emitLoadStoreInstruction(const MachineInstr &MI,
Evan Cheng4df60f52008-11-07 09:06:08 +0000123 unsigned ImplicitRd = 0,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000124 unsigned ImplicitRn = 0);
Evan Chengedda31c2008-11-05 18:35:52 +0000125
Evan Cheng83b5cf02008-11-05 23:22:34 +0000126 void emitMiscLoadStoreInstruction(const MachineInstr &MI,
127 unsigned ImplicitRn = 0);
Evan Chengedda31c2008-11-05 18:35:52 +0000128
129 void emitLoadStoreMultipleInstruction(const MachineInstr &MI);
130
Evan Chengfbc9d412008-11-06 01:21:28 +0000131 void emitMulFrmInstruction(const MachineInstr &MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000132
Evan Cheng97f48c32008-11-06 22:15:19 +0000133 void emitExtendInstruction(const MachineInstr &MI);
134
Evan Cheng8b59db32008-11-07 01:41:35 +0000135 void emitMiscArithInstruction(const MachineInstr &MI);
136
Evan Chengedda31c2008-11-05 18:35:52 +0000137 void emitBranchInstruction(const MachineInstr &MI);
138
Evan Cheng437c1732008-11-07 22:30:53 +0000139 void emitInlineJumpTable(unsigned JTIndex);
Evan Cheng4df60f52008-11-07 09:06:08 +0000140
Evan Chengedda31c2008-11-05 18:35:52 +0000141 void emitMiscBranchInstruction(const MachineInstr &MI);
Evan Cheng7602e112008-09-02 06:52:38 +0000142
Evan Cheng96581d32008-11-11 02:11:05 +0000143 void emitVFPArithInstruction(const MachineInstr &MI);
144
Evan Cheng78be83d2008-11-11 19:40:26 +0000145 void emitVFPConversionInstruction(const MachineInstr &MI);
146
Evan Chengcd8e66a2008-11-11 21:48:44 +0000147 void emitVFPLoadStoreInstruction(const MachineInstr &MI);
148
149 void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI);
150
151 void emitMiscInstruction(const MachineInstr &MI);
152
Evan Cheng7602e112008-09-02 06:52:38 +0000153 /// getMachineOpValue - Return binary encoding of operand. If the machine
154 /// operand requires relocation, record the relocation and return zero.
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000155 unsigned getMachineOpValue(const MachineInstr &MI,const MachineOperand &MO);
Evan Cheng7602e112008-09-02 06:52:38 +0000156 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) {
157 return getMachineOpValue(MI, MI.getOperand(OpIdx));
158 }
Evan Cheng7602e112008-09-02 06:52:38 +0000159
Evan Cheng83b5cf02008-11-05 23:22:34 +0000160 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng7602e112008-09-02 06:52:38 +0000161 ///
Evan Cheng83b5cf02008-11-05 23:22:34 +0000162 unsigned getShiftOp(unsigned Imm) const ;
Evan Cheng7602e112008-09-02 06:52:38 +0000163
164 /// Routines that handle operands which add machine relocations which are
Evan Cheng437c1732008-11-07 22:30:53 +0000165 /// fixed up by the relocation stage.
Evan Cheng057d0c32008-09-18 07:28:19 +0000166 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
Evan Cheng08669742009-09-10 01:23:53 +0000167 bool NeedStub, bool Indirect, intptr_t ACPV = 0);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000168 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
Evan Cheng437c1732008-11-07 22:30:53 +0000169 void emitConstPoolAddress(unsigned CPI, unsigned Reloc);
170 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc);
171 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
172 intptr_t JTBase = 0);
Evan Cheng148b6a42007-07-05 21:15:40 +0000173 };
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000174 template <class CodeEmitter>
175 char Emitter<CodeEmitter>::ID = 0;
Evan Cheng148b6a42007-07-05 21:15:40 +0000176}
177
178/// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
179/// to the specified MCE object.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000180
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000181FunctionPass *llvm::createARMCodeEmitterPass(ARMBaseTargetMachine &TM,
182 MachineCodeEmitter &MCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000183 return new Emitter<MachineCodeEmitter>(TM, MCE);
184}
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000185FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
186 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000187 return new Emitter<JITCodeEmitter>(TM, JCE);
Evan Cheng148b6a42007-07-05 21:15:40 +0000188}
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000189FunctionPass *llvm::createARMObjectCodeEmitterPass(ARMBaseTargetMachine &TM,
190 ObjectCodeEmitter &OCE) {
191 return new Emitter<ObjectCodeEmitter>(TM, OCE);
192}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000193
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000194template<class CodeEmitter>
195bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000196 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
197 MF.getTarget().getRelocationModel() != Reloc::Static) &&
198 "JIT relocation model must be set to static or default!");
Evan Cheng08669742009-09-10 01:23:53 +0000199 JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo();
Evan Cheng148b6a42007-07-05 21:15:40 +0000200 II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo();
201 TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData();
Evan Cheng08669742009-09-10 01:23:53 +0000202 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng938b9d82008-10-31 19:55:13 +0000203 MCPEs = &MF.getConstantPool()->getConstants();
Evan Cheng4df60f52008-11-07 09:06:08 +0000204 MJTEs = &MF.getJumpTableInfo()->getJumpTables();
205 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Evan Cheng3cc82232008-11-08 07:38:22 +0000206 JTI->Initialize(MF, IsPIC);
Evan Cheng148b6a42007-07-05 21:15:40 +0000207
208 do {
Jim Grosbach764ab522009-08-11 15:33:49 +0000209 DEBUG(errs() << "JITTing function '"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000210 << MF.getFunction()->getName() << "'\n");
Evan Cheng148b6a42007-07-05 21:15:40 +0000211 MCE.startFunction(MF);
Jim Grosbach764ab522009-08-11 15:33:49 +0000212 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
Evan Cheng148b6a42007-07-05 21:15:40 +0000213 MBB != E; ++MBB) {
214 MCE.StartMachineBasicBlock(MBB);
215 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
216 I != E; ++I)
217 emitInstruction(*I);
218 }
219 } while (MCE.finishFunction(MF));
220
221 return false;
222}
223
Evan Cheng83b5cf02008-11-05 23:22:34 +0000224/// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng7602e112008-09-02 06:52:38 +0000225///
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000226template<class CodeEmitter>
227unsigned Emitter<CodeEmitter>::getShiftOp(unsigned Imm) const {
Evan Cheng83b5cf02008-11-05 23:22:34 +0000228 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000229 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng7602e112008-09-02 06:52:38 +0000230 case ARM_AM::asr: return 2;
231 case ARM_AM::lsl: return 0;
232 case ARM_AM::lsr: return 1;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000233 case ARM_AM::ror:
Evan Cheng7602e112008-09-02 06:52:38 +0000234 case ARM_AM::rrx: return 3;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000235 }
Evan Cheng7602e112008-09-02 06:52:38 +0000236 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000237}
238
Evan Cheng7602e112008-09-02 06:52:38 +0000239/// getMachineOpValue - Return binary encoding of operand. If the machine
240/// operand requires relocation, record the relocation and return zero.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000241template<class CodeEmitter>
242unsigned Emitter<CodeEmitter>::getMachineOpValue(const MachineInstr &MI,
243 const MachineOperand &MO) {
Dan Gohmand735b802008-10-03 15:45:36 +0000244 if (MO.isReg())
Evan Cheng7602e112008-09-02 06:52:38 +0000245 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000246 else if (MO.isImm())
Evan Cheng7602e112008-09-02 06:52:38 +0000247 return static_cast<unsigned>(MO.getImm());
Dan Gohmand735b802008-10-03 15:45:36 +0000248 else if (MO.isGlobal())
Evan Cheng08669742009-09-10 01:23:53 +0000249 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true, false);
Dan Gohmand735b802008-10-03 15:45:36 +0000250 else if (MO.isSymbol())
Evan Cheng10332512008-11-08 07:22:33 +0000251 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
Evan Cheng580c0df2008-11-12 01:02:24 +0000252 else if (MO.isCPI()) {
253 const TargetInstrDesc &TID = MI.getDesc();
254 // For VFP load, the immediate offset is multiplied by 4.
255 unsigned Reloc = ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
256 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
257 emitConstPoolAddress(MO.getIndex(), Reloc);
258 } else if (MO.isJTI())
Chris Lattner8aa797a2007-12-30 23:10:15 +0000259 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
Dan Gohmand735b802008-10-03 15:45:36 +0000260 else if (MO.isMBB())
Evan Cheng4df60f52008-11-07 09:06:08 +0000261 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
Evan Cheng2aa0e642008-09-13 01:55:59 +0000262 else {
Torok Edwindac237e2009-07-08 20:53:28 +0000263#ifndef NDEBUG
Chris Lattner705e07f2009-08-23 03:41:05 +0000264 errs() << MO;
Torok Edwindac237e2009-07-08 20:53:28 +0000265#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000266 llvm_unreachable(0);
Evan Cheng2aa0e642008-09-13 01:55:59 +0000267 }
Evan Cheng7602e112008-09-02 06:52:38 +0000268 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000269}
270
Evan Cheng057d0c32008-09-18 07:28:19 +0000271/// emitGlobalAddress - Emit the specified address to the code stream.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000272///
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000273template<class CodeEmitter>
274void Emitter<CodeEmitter>::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
Evan Cheng08669742009-09-10 01:23:53 +0000275 bool NeedStub, bool Indirect,
276 intptr_t ACPV) {
277 MachineRelocation MR = Indirect
278 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
279 GV, ACPV, NeedStub)
280 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
281 GV, ACPV, NeedStub);
282 MCE.addRelocation(MR);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000283}
284
285/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
286/// be emitted to the current location in the function, and allow it to be PC
287/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000288template<class CodeEmitter>
289void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES,
290 unsigned Reloc) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000291 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
292 Reloc, ES));
293}
294
295/// emitConstPoolAddress - Arrange for the address of an constant pool
296/// to be emitted to the current location in the function, and allow it to be PC
297/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000298template<class CodeEmitter>
299void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI,
300 unsigned Reloc) {
Evan Cheng0f282432008-10-29 23:55:43 +0000301 // Tell JIT emitter we'll resolve the address.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000302 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000303 Reloc, CPI, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000304}
305
306/// emitJumpTableAddress - Arrange for the address of a jump table to
307/// be emitted to the current location in the function, and allow it to be PC
308/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000309template<class CodeEmitter>
Jim Grosbach764ab522009-08-11 15:33:49 +0000310void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTIndex,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000311 unsigned Reloc) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000312 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000313 Reloc, JTIndex, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000314}
315
Raul Herbster9c1a3822007-08-30 23:29:26 +0000316/// emitMachineBasicBlock - Emit the specified address basic block.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000317template<class CodeEmitter>
318void Emitter<CodeEmitter>::emitMachineBasicBlock(MachineBasicBlock *BB,
319 unsigned Reloc, intptr_t JTBase) {
Raul Herbster9c1a3822007-08-30 23:29:26 +0000320 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000321 Reloc, BB, JTBase));
Raul Herbster9c1a3822007-08-30 23:29:26 +0000322}
Evan Cheng0ff94f72007-08-07 01:37:15 +0000323
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000324template<class CodeEmitter>
325void Emitter<CodeEmitter>::emitWordLE(unsigned Binary) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000326 DEBUG(errs() << " 0x";
327 errs().write_hex(Binary) << "\n");
Evan Cheng83b5cf02008-11-05 23:22:34 +0000328 MCE.emitWordLE(Binary);
329}
330
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000331template<class CodeEmitter>
332void Emitter<CodeEmitter>::emitDWordLE(uint64_t Binary) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000333 DEBUG(errs() << " 0x";
334 errs().write_hex(Binary) << "\n");
Evan Chengcb5201f2008-11-11 22:19:31 +0000335 MCE.emitDWordLE(Binary);
336}
337
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000338template<class CodeEmitter>
339void Emitter<CodeEmitter>::emitInstruction(const MachineInstr &MI) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000340 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
Evan Cheng42d5ee062008-09-13 01:15:21 +0000341
Jeffrey Yasskin75402822009-07-17 18:49:39 +0000342 MCE.processDebugLoc(MI.getDebugLoc());
343
Evan Cheng148b6a42007-07-05 21:15:40 +0000344 NumEmitted++; // Keep track of the # of mi's emitted
Evan Chengedda31c2008-11-05 18:35:52 +0000345 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
Evan Chengffa6d962008-11-13 23:36:57 +0000346 default: {
Torok Edwinc23197a2009-07-14 16:55:14 +0000347 llvm_unreachable("Unhandled instruction encoding format!");
Evan Chengedda31c2008-11-05 18:35:52 +0000348 break;
Evan Chengffa6d962008-11-13 23:36:57 +0000349 }
Evan Chengedda31c2008-11-05 18:35:52 +0000350 case ARMII::Pseudo:
Evan Cheng057d0c32008-09-18 07:28:19 +0000351 emitPseudoInstruction(MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000352 break;
353 case ARMII::DPFrm:
354 case ARMII::DPSoRegFrm:
355 emitDataProcessingInstruction(MI);
356 break;
Evan Cheng148cad82008-11-13 07:34:59 +0000357 case ARMII::LdFrm:
358 case ARMII::StFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000359 emitLoadStoreInstruction(MI);
360 break;
Evan Cheng148cad82008-11-13 07:34:59 +0000361 case ARMII::LdMiscFrm:
362 case ARMII::StMiscFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000363 emitMiscLoadStoreInstruction(MI);
364 break;
Evan Cheng3c4a4ff2008-11-12 07:18:38 +0000365 case ARMII::LdStMulFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000366 emitLoadStoreMultipleInstruction(MI);
367 break;
Evan Chengfbc9d412008-11-06 01:21:28 +0000368 case ARMII::MulFrm:
369 emitMulFrmInstruction(MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000370 break;
Evan Cheng97f48c32008-11-06 22:15:19 +0000371 case ARMII::ExtFrm:
372 emitExtendInstruction(MI);
373 break;
Evan Cheng8b59db32008-11-07 01:41:35 +0000374 case ARMII::ArithMiscFrm:
375 emitMiscArithInstruction(MI);
376 break;
Evan Cheng12c3a532008-11-06 17:48:05 +0000377 case ARMII::BrFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000378 emitBranchInstruction(MI);
379 break;
Evan Cheng12c3a532008-11-06 17:48:05 +0000380 case ARMII::BrMiscFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000381 emitMiscBranchInstruction(MI);
382 break;
Evan Cheng96581d32008-11-11 02:11:05 +0000383 // VFP instructions.
384 case ARMII::VFPUnaryFrm:
385 case ARMII::VFPBinaryFrm:
386 emitVFPArithInstruction(MI);
387 break;
Evan Cheng78be83d2008-11-11 19:40:26 +0000388 case ARMII::VFPConv1Frm:
389 case ARMII::VFPConv2Frm:
Evan Cheng0a0ab132008-11-11 22:46:12 +0000390 case ARMII::VFPConv3Frm:
Evan Cheng80a11982008-11-12 06:41:41 +0000391 case ARMII::VFPConv4Frm:
392 case ARMII::VFPConv5Frm:
Evan Cheng78be83d2008-11-11 19:40:26 +0000393 emitVFPConversionInstruction(MI);
394 break;
Evan Chengcd8e66a2008-11-11 21:48:44 +0000395 case ARMII::VFPLdStFrm:
396 emitVFPLoadStoreInstruction(MI);
397 break;
398 case ARMII::VFPLdStMulFrm:
399 emitVFPLoadStoreMultipleInstruction(MI);
400 break;
401 case ARMII::VFPMiscFrm:
402 emitMiscInstruction(MI);
403 break;
Evan Chengedda31c2008-11-05 18:35:52 +0000404 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000405}
406
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000407template<class CodeEmitter>
408void Emitter<CodeEmitter>::emitConstPoolInstruction(const MachineInstr &MI) {
Evan Cheng437c1732008-11-07 22:30:53 +0000409 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
410 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
Evan Cheng938b9d82008-10-31 19:55:13 +0000411 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
Jim Grosbach764ab522009-08-11 15:33:49 +0000412
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000413 // Remember the CONSTPOOL_ENTRY address for later relocation.
414 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
415
416 // Emit constpool island entry. In most cases, the actual values will be
417 // resolved and relocated after code emission.
418 if (MCPE.isMachineConstantPoolEntry()) {
419 ARMConstantPoolValue *ACPV =
420 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
421
Chris Lattner705e07f2009-08-23 03:41:05 +0000422 DEBUG(errs() << " ** ARM constant pool #" << CPI << " @ "
423 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n');
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000424
425 GlobalValue *GV = ACPV->getGV();
426 if (GV) {
Evan Cheng08669742009-09-10 01:23:53 +0000427 Reloc::Model RelocM = TM.getRelocationModel();
Evan Chenge4e4ed32009-08-28 23:18:09 +0000428 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
Evan Cheng08669742009-09-10 01:23:53 +0000429 isa<Function>(GV),
430 Subtarget->GVIsIndirectSymbol(GV, RelocM),
431 (intptr_t)ACPV);
Evan Cheng25e04782008-11-04 00:50:32 +0000432 } else {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000433 emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
434 }
Evan Cheng83b5cf02008-11-05 23:22:34 +0000435 emitWordLE(0);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000436 } else {
437 Constant *CV = MCPE.Val.ConstVal;
438
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000439 DEBUG({
440 errs() << " ** Constant pool #" << CPI << " @ "
441 << (void*)MCE.getCurrentPCValue() << " ";
442 if (const Function *F = dyn_cast<Function>(CV))
443 errs() << F->getName();
444 else
445 errs() << *CV;
446 errs() << '\n';
447 });
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000448
449 if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Evan Cheng08669742009-09-10 01:23:53 +0000450 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV), false);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000451 emitWordLE(0);
Evan Chengcb5201f2008-11-11 22:19:31 +0000452 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000453 uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
Evan Cheng83b5cf02008-11-05 23:22:34 +0000454 emitWordLE(Val);
Evan Chengcb5201f2008-11-11 22:19:31 +0000455 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000456 if (CFP->getType() == Type::getFloatTy(CFP->getContext()))
Evan Chengcb5201f2008-11-11 22:19:31 +0000457 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Owen Anderson1d0be152009-08-13 21:58:54 +0000458 else if (CFP->getType() == Type::getDoubleTy(CFP->getContext()))
Evan Chengcb5201f2008-11-11 22:19:31 +0000459 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
460 else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000461 llvm_unreachable("Unable to handle this constantpool entry!");
Evan Chengcb5201f2008-11-11 22:19:31 +0000462 }
463 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000464 llvm_unreachable("Unable to handle this constantpool entry!");
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000465 }
466 }
467}
468
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000469template<class CodeEmitter>
470void Emitter<CodeEmitter>::emitMOVi2piecesInstruction(const MachineInstr &MI) {
Evan Cheng90922132008-11-06 02:25:39 +0000471 const MachineOperand &MO0 = MI.getOperand(0);
472 const MachineOperand &MO1 = MI.getOperand(1);
Evan Chenge7cbe412009-07-08 21:03:57 +0000473 assert(MO1.isImm() && ARM_AM::getSOImmVal(MO1.isImm()) != -1 &&
474 "Not a valid so_imm value!");
Evan Cheng90922132008-11-06 02:25:39 +0000475 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
476 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
477
478 // Emit the 'mov' instruction.
479 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
480
481 // Set the conditional execution predicate.
Evan Cheng97f48c32008-11-06 22:15:19 +0000482 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng90922132008-11-06 02:25:39 +0000483
484 // Encode Rd.
485 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
486
487 // Encode so_imm.
488 // Set bit I(25) to identify this is the immediate form of <shifter_op>
489 Binary |= 1 << ARMII::I_BitShift;
Evan Chenge7cbe412009-07-08 21:03:57 +0000490 Binary |= getMachineSoImmOpValue(V1);
Evan Cheng90922132008-11-06 02:25:39 +0000491 emitWordLE(Binary);
492
493 // Now the 'orr' instruction.
494 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
495
496 // Set the conditional execution predicate.
Evan Cheng97f48c32008-11-06 22:15:19 +0000497 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng90922132008-11-06 02:25:39 +0000498
499 // Encode Rd.
500 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
501
502 // Encode Rn.
503 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
504
505 // Encode so_imm.
506 // Set bit I(25) to identify this is the immediate form of <shifter_op>
507 Binary |= 1 << ARMII::I_BitShift;
Evan Chenge7cbe412009-07-08 21:03:57 +0000508 Binary |= getMachineSoImmOpValue(V2);
Evan Cheng90922132008-11-06 02:25:39 +0000509 emitWordLE(Binary);
510}
511
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000512template<class CodeEmitter>
513void Emitter<CodeEmitter>::emitLEApcrelJTInstruction(const MachineInstr &MI) {
Evan Cheng4df60f52008-11-07 09:06:08 +0000514 // It's basically add r, pc, (LJTI - $+8)
Jim Grosbach764ab522009-08-11 15:33:49 +0000515
Evan Cheng4df60f52008-11-07 09:06:08 +0000516 const TargetInstrDesc &TID = MI.getDesc();
517
518 // Emit the 'add' instruction.
519 unsigned Binary = 0x4 << 21; // add: Insts{24-31} = 0b0100
520
521 // Set the conditional execution predicate
522 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
523
524 // Encode S bit if MI modifies CPSR.
525 Binary |= getAddrModeSBit(MI, TID);
526
527 // Encode Rd.
528 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
529
530 // Encode Rn which is PC.
531 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
532
533 // Encode the displacement.
Evan Cheng4df60f52008-11-07 09:06:08 +0000534 Binary |= 1 << ARMII::I_BitShift;
535 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
536
537 emitWordLE(Binary);
538}
539
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000540template<class CodeEmitter>
541void Emitter<CodeEmitter>::emitPseudoMoveInstruction(const MachineInstr &MI) {
Evan Chenga9562552008-11-14 20:09:11 +0000542 unsigned Opcode = MI.getDesc().Opcode;
543
544 // Part of binary is determined by TableGn.
545 unsigned Binary = getBinaryCodeForInstr(MI);
546
547 // Set the conditional execution predicate
548 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
549
550 // Encode S bit if MI modifies CPSR.
551 if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag)
552 Binary |= 1 << ARMII::S_BitShift;
553
554 // Encode register def if there is one.
555 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
556
557 // Encode the shift operation.
558 switch (Opcode) {
559 default: break;
560 case ARM::MOVrx:
561 // rrx
562 Binary |= 0x6 << 4;
563 break;
564 case ARM::MOVsrl_flag:
565 // lsr #1
566 Binary |= (0x2 << 4) | (1 << 7);
567 break;
568 case ARM::MOVsra_flag:
569 // asr #1
570 Binary |= (0x4 << 4) | (1 << 7);
571 break;
572 }
573
574 // Encode register Rm.
575 Binary |= getMachineOpValue(MI, 1);
576
577 emitWordLE(Binary);
578}
579
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000580template<class CodeEmitter>
581void Emitter<CodeEmitter>::addPCLabel(unsigned LabelID) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000582 DEBUG(errs() << " ** LPC" << LabelID << " @ "
583 << (void*)MCE.getCurrentPCValue() << '\n');
Evan Cheng83b5cf02008-11-05 23:22:34 +0000584 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
585}
586
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000587template<class CodeEmitter>
588void Emitter<CodeEmitter>::emitPseudoInstruction(const MachineInstr &MI) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000589 unsigned Opcode = MI.getDesc().Opcode;
590 switch (Opcode) {
591 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000592 llvm_unreachable("ARMCodeEmitter::emitPseudoInstruction");//FIXME:
Evan Chengffa6d962008-11-13 23:36:57 +0000593 case TargetInstrInfo::INLINEASM: {
Evan Chenge3066ab2008-11-19 23:21:33 +0000594 // We allow inline assembler nodes with empty bodies - they can
595 // implicitly define registers, which is ok for JIT.
596 if (MI.getOperand(0).getSymbolName()[0]) {
Torok Edwin29fd0562009-07-12 07:15:17 +0000597 llvm_report_error("JIT does not support inline asm!");
Evan Chenge3066ab2008-11-19 23:21:33 +0000598 }
Evan Chengffa6d962008-11-13 23:36:57 +0000599 break;
600 }
601 case TargetInstrInfo::DBG_LABEL:
602 case TargetInstrInfo::EH_LABEL:
603 MCE.emitLabel(MI.getOperand(0).getImm());
604 break;
605 case TargetInstrInfo::IMPLICIT_DEF:
Evan Chengffa6d962008-11-13 23:36:57 +0000606 case ARM::DWARF_LOC:
607 // Do nothing.
608 break;
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000609 case ARM::CONSTPOOL_ENTRY:
610 emitConstPoolInstruction(MI);
611 break;
612 case ARM::PICADD: {
Evan Cheng25e04782008-11-04 00:50:32 +0000613 // Remember of the address of the PC label for relocation later.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000614 addPCLabel(MI.getOperand(2).getImm());
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000615 // PICADD is just an add instruction that implicitly read pc.
Evan Cheng437c1732008-11-07 22:30:53 +0000616 emitDataProcessingInstruction(MI, 0, ARM::PC);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000617 break;
618 }
619 case ARM::PICLDR:
620 case ARM::PICLDRB:
621 case ARM::PICSTR:
622 case ARM::PICSTRB: {
623 // Remember of the address of the PC label for relocation later.
624 addPCLabel(MI.getOperand(2).getImm());
625 // These are just load / store instructions that implicitly read pc.
Evan Cheng4df60f52008-11-07 09:06:08 +0000626 emitLoadStoreInstruction(MI, 0, ARM::PC);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000627 break;
628 }
629 case ARM::PICLDRH:
630 case ARM::PICLDRSH:
631 case ARM::PICLDRSB:
632 case ARM::PICSTRH: {
633 // Remember of the address of the PC label for relocation later.
634 addPCLabel(MI.getOperand(2).getImm());
635 // These are just load / store instructions that implicitly read pc.
636 emitMiscLoadStoreInstruction(MI, ARM::PC);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000637 break;
638 }
Evan Cheng90922132008-11-06 02:25:39 +0000639 case ARM::MOVi2pieces:
640 // Two instructions to materialize a constant.
641 emitMOVi2piecesInstruction(MI);
642 break;
Evan Cheng4df60f52008-11-07 09:06:08 +0000643 case ARM::LEApcrelJT:
644 // Materialize jumptable address.
645 emitLEApcrelJTInstruction(MI);
646 break;
Evan Chenga9562552008-11-14 20:09:11 +0000647 case ARM::MOVrx:
648 case ARM::MOVsrl_flag:
649 case ARM::MOVsra_flag:
650 emitPseudoMoveInstruction(MI);
651 break;
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000652 }
653}
654
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000655template<class CodeEmitter>
656unsigned Emitter<CodeEmitter>::getMachineSoRegOpValue(
657 const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000658 const TargetInstrDesc &TID,
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000659 const MachineOperand &MO,
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000660 unsigned OpIdx) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000661 unsigned Binary = getMachineOpValue(MI, MO);
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000662
663 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
664 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
665 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
666
667 // Encode the shift opcode.
668 unsigned SBits = 0;
669 unsigned Rs = MO1.getReg();
670 if (Rs) {
671 // Set shift operand (bit[7:4]).
672 // LSL - 0001
673 // LSR - 0011
674 // ASR - 0101
675 // ROR - 0111
676 // RRX - 0110 and bit[11:8] clear.
677 switch (SOpc) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000678 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000679 case ARM_AM::lsl: SBits = 0x1; break;
680 case ARM_AM::lsr: SBits = 0x3; break;
681 case ARM_AM::asr: SBits = 0x5; break;
682 case ARM_AM::ror: SBits = 0x7; break;
683 case ARM_AM::rrx: SBits = 0x6; break;
684 }
685 } else {
686 // Set shift operand (bit[6:4]).
687 // LSL - 000
688 // LSR - 010
689 // ASR - 100
690 // ROR - 110
691 switch (SOpc) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000692 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000693 case ARM_AM::lsl: SBits = 0x0; break;
694 case ARM_AM::lsr: SBits = 0x2; break;
695 case ARM_AM::asr: SBits = 0x4; break;
696 case ARM_AM::ror: SBits = 0x6; break;
697 }
698 }
699 Binary |= SBits << 4;
700 if (SOpc == ARM_AM::rrx)
701 return Binary;
702
703 // Encode the shift operation Rs or shift_imm (except rrx).
704 if (Rs) {
705 // Encode Rs bit[11:8].
706 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
707 return Binary |
708 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
709 }
710
711 // Encode shift_imm bit[11:7].
712 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
713}
714
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000715template<class CodeEmitter>
716unsigned Emitter<CodeEmitter>::getMachineSoImmOpValue(unsigned SoImm) {
Evan Chenge7cbe412009-07-08 21:03:57 +0000717 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
718 assert(SoImmVal != -1 && "Not a valid so_imm value!");
719
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000720 // Encode rotate_imm.
Evan Chenge7cbe412009-07-08 21:03:57 +0000721 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
Evan Cheng97f48c32008-11-06 22:15:19 +0000722 << ARMII::SoRotImmShift;
723
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000724 // Encode immed_8.
Evan Chenge7cbe412009-07-08 21:03:57 +0000725 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000726 return Binary;
727}
728
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000729template<class CodeEmitter>
730unsigned Emitter<CodeEmitter>::getAddrModeSBit(const MachineInstr &MI,
731 const TargetInstrDesc &TID) const {
Evan Cheng97c573d2008-11-20 02:25:51 +0000732 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
Evan Cheng49a9f292008-09-12 22:45:55 +0000733 const MachineOperand &MO = MI.getOperand(i-1);
Dan Gohmand735b802008-10-03 15:45:36 +0000734 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
Evan Cheng49a9f292008-09-12 22:45:55 +0000735 return 1 << ARMII::S_BitShift;
736 }
737 return 0;
738}
739
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000740template<class CodeEmitter>
741void Emitter<CodeEmitter>::emitDataProcessingInstruction(
742 const MachineInstr &MI,
Evan Cheng437c1732008-11-07 22:30:53 +0000743 unsigned ImplicitRd,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000744 unsigned ImplicitRn) {
Evan Chengedda31c2008-11-05 18:35:52 +0000745 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +0000746
Evan Cheng36a0aeb2009-07-06 22:23:46 +0000747 if (TID.Opcode == ARM::BFC) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +0000748 llvm_report_error("ARMv6t2 JIT is not yet supported.");
Evan Cheng36a0aeb2009-07-06 22:23:46 +0000749 }
750
Evan Chengedda31c2008-11-05 18:35:52 +0000751 // Part of binary is determined by TableGn.
752 unsigned Binary = getBinaryCodeForInstr(MI);
753
Jim Grosbach33412622008-10-07 19:05:35 +0000754 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000755 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000756
Evan Cheng49a9f292008-09-12 22:45:55 +0000757 // Encode S bit if MI modifies CPSR.
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +0000758 Binary |= getAddrModeSBit(MI, TID);
Evan Cheng49a9f292008-09-12 22:45:55 +0000759
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000760 // Encode register def if there is one.
Evan Cheng49a9f292008-09-12 22:45:55 +0000761 unsigned NumDefs = TID.getNumDefs();
Evan Chenga964b7d2008-09-12 23:15:39 +0000762 unsigned OpIdx = 0;
Evan Cheng437c1732008-11-07 22:30:53 +0000763 if (NumDefs)
764 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
765 else if (ImplicitRd)
766 // Special handling for implicit use (e.g. PC).
767 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
768 << ARMII::RegRdShift);
Evan Cheng7602e112008-09-02 06:52:38 +0000769
Evan Chengd87293c2008-11-06 08:47:38 +0000770 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
771 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
772 ++OpIdx;
773
Jim Grosbachefd30ba2008-10-01 18:16:49 +0000774 // Encode first non-shifter register operand if there is one.
Evan Chengedda31c2008-11-05 18:35:52 +0000775 bool isUnary = TID.TSFlags & ARMII::UnaryDP;
776 if (!isUnary) {
Evan Cheng83b5cf02008-11-05 23:22:34 +0000777 if (ImplicitRn)
778 // Special handling for implicit use (e.g. PC).
779 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
Evan Chengedda31c2008-11-05 18:35:52 +0000780 << ARMII::RegRnShift);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000781 else {
782 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
783 ++OpIdx;
784 }
Evan Cheng7602e112008-09-02 06:52:38 +0000785 }
786
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000787 // Encode shifter operand.
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000788 const MachineOperand &MO = MI.getOperand(OpIdx);
Evan Chengedda31c2008-11-05 18:35:52 +0000789 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000790 // Encode SoReg.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000791 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
Evan Chengedda31c2008-11-05 18:35:52 +0000792 return;
793 }
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000794
Evan Chengedda31c2008-11-05 18:35:52 +0000795 if (MO.isReg()) {
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000796 // Encode register Rm.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000797 emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
Evan Chengedda31c2008-11-05 18:35:52 +0000798 return;
799 }
Evan Cheng7602e112008-09-02 06:52:38 +0000800
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000801 // Encode so_imm.
Evan Chenge7cbe412009-07-08 21:03:57 +0000802 Binary |= getMachineSoImmOpValue((unsigned)MO.getImm());
Evan Chengedda31c2008-11-05 18:35:52 +0000803
Evan Cheng83b5cf02008-11-05 23:22:34 +0000804 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000805}
806
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000807template<class CodeEmitter>
808void Emitter<CodeEmitter>::emitLoadStoreInstruction(
809 const MachineInstr &MI,
Evan Cheng4df60f52008-11-07 09:06:08 +0000810 unsigned ImplicitRd,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000811 unsigned ImplicitRn) {
Evan Cheng05c356e2008-11-08 01:44:13 +0000812 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng148cad82008-11-13 07:34:59 +0000813 unsigned Form = TID.TSFlags & ARMII::FormMask;
814 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +0000815
Evan Chengedda31c2008-11-05 18:35:52 +0000816 // Part of binary is determined by TableGn.
817 unsigned Binary = getBinaryCodeForInstr(MI);
818
Jim Grosbach33412622008-10-07 19:05:35 +0000819 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000820 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +0000821
Evan Cheng4df60f52008-11-07 09:06:08 +0000822 unsigned OpIdx = 0;
Evan Cheng148cad82008-11-13 07:34:59 +0000823
824 // Operand 0 of a pre- and post-indexed store is the address base
825 // writeback. Skip it.
826 bool Skipped = false;
827 if (IsPrePost && Form == ARMII::StFrm) {
828 ++OpIdx;
829 Skipped = true;
830 }
831
832 // Set first operand
Evan Cheng4df60f52008-11-07 09:06:08 +0000833 if (ImplicitRd)
834 // Special handling for implicit use (e.g. PC).
835 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
836 << ARMII::RegRdShift);
837 else
838 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000839
840 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +0000841 if (ImplicitRn)
842 // Special handling for implicit use (e.g. PC).
843 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
844 << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +0000845 else
846 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000847
Evan Cheng05c356e2008-11-08 01:44:13 +0000848 // If this is a two-address operand, skip it. e.g. LDR_PRE.
Evan Cheng148cad82008-11-13 07:34:59 +0000849 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +0000850 ++OpIdx;
851
Evan Cheng83b5cf02008-11-05 23:22:34 +0000852 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +0000853 unsigned AM2Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000854 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +0000855
Evan Chenge7de7e32008-09-13 01:44:01 +0000856 // Set bit U(23) according to sign of immed value (positive or negative).
Evan Cheng83b5cf02008-11-05 23:22:34 +0000857 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
Evan Chenge7de7e32008-09-13 01:44:01 +0000858 ARMII::U_BitShift);
Evan Cheng7602e112008-09-02 06:52:38 +0000859 if (!MO2.getReg()) { // is immediate
Evan Cheng83b5cf02008-11-05 23:22:34 +0000860 if (ARM_AM::getAM2Offset(AM2Opc))
Evan Cheng7602e112008-09-02 06:52:38 +0000861 // Set the value of offset_12 field
Evan Cheng83b5cf02008-11-05 23:22:34 +0000862 Binary |= ARM_AM::getAM2Offset(AM2Opc);
863 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +0000864 return;
Evan Cheng7602e112008-09-02 06:52:38 +0000865 }
866
867 // Set bit I(25), because this is not in immediate enconding.
868 Binary |= 1 << ARMII::I_BitShift;
869 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
870 // Set bit[3:0] to the corresponding Rm register
871 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
872
Evan Cheng70632912008-11-12 07:34:37 +0000873 // If this instr is in scaled register offset/index instruction, set
Evan Cheng7602e112008-09-02 06:52:38 +0000874 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000875 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
Evan Cheng70632912008-11-12 07:34:37 +0000876 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
877 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
Evan Cheng7602e112008-09-02 06:52:38 +0000878 }
879
Evan Cheng83b5cf02008-11-05 23:22:34 +0000880 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000881}
882
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000883template<class CodeEmitter>
884void Emitter<CodeEmitter>::emitMiscLoadStoreInstruction(const MachineInstr &MI,
885 unsigned ImplicitRn) {
Evan Cheng05c356e2008-11-08 01:44:13 +0000886 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng148cad82008-11-13 07:34:59 +0000887 unsigned Form = TID.TSFlags & ARMII::FormMask;
888 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +0000889
Evan Chengedda31c2008-11-05 18:35:52 +0000890 // Part of binary is determined by TableGn.
891 unsigned Binary = getBinaryCodeForInstr(MI);
892
Jim Grosbach33412622008-10-07 19:05:35 +0000893 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000894 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +0000895
Evan Cheng148cad82008-11-13 07:34:59 +0000896 unsigned OpIdx = 0;
897
898 // Operand 0 of a pre- and post-indexed store is the address base
899 // writeback. Skip it.
900 bool Skipped = false;
901 if (IsPrePost && Form == ARMII::StMiscFrm) {
902 ++OpIdx;
903 Skipped = true;
904 }
905
Evan Cheng7602e112008-09-02 06:52:38 +0000906 // Set first operand
Evan Cheng148cad82008-11-13 07:34:59 +0000907 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000908
Evan Cheng358dec52009-06-15 08:28:29 +0000909 // Skip LDRD and STRD's second operand.
910 if (TID.Opcode == ARM::LDRD || TID.Opcode == ARM::STRD)
911 ++OpIdx;
912
Evan Cheng7602e112008-09-02 06:52:38 +0000913 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +0000914 if (ImplicitRn)
915 // Special handling for implicit use (e.g. PC).
916 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
917 << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +0000918 else
919 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000920
Evan Cheng05c356e2008-11-08 01:44:13 +0000921 // If this is a two-address operand, skip it. e.g. LDRH_POST.
Evan Cheng148cad82008-11-13 07:34:59 +0000922 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +0000923 ++OpIdx;
924
Evan Cheng83b5cf02008-11-05 23:22:34 +0000925 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +0000926 unsigned AM3Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000927 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +0000928
Evan Chenge7de7e32008-09-13 01:44:01 +0000929 // Set bit U(23) according to sign of immed value (positive or negative)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000930 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
Evan Cheng7602e112008-09-02 06:52:38 +0000931 ARMII::U_BitShift);
932
933 // If this instr is in register offset/index encoding, set bit[3:0]
934 // to the corresponding Rm register.
935 if (MO2.getReg()) {
936 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
Evan Cheng83b5cf02008-11-05 23:22:34 +0000937 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +0000938 return;
Evan Cheng7602e112008-09-02 06:52:38 +0000939 }
940
Evan Chengd87293c2008-11-06 08:47:38 +0000941 // This instr is in immediate offset/index encoding, set bit 22 to 1.
Evan Cheng97f48c32008-11-06 22:15:19 +0000942 Binary |= 1 << ARMII::AM3_I_BitShift;
Evan Cheng83b5cf02008-11-05 23:22:34 +0000943 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
Evan Cheng7602e112008-09-02 06:52:38 +0000944 // Set operands
Evan Cheng70632912008-11-12 07:34:37 +0000945 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
946 Binary |= (ImmOffs & 0xF); // immedL
Evan Cheng7602e112008-09-02 06:52:38 +0000947 }
948
Evan Cheng83b5cf02008-11-05 23:22:34 +0000949 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000950}
951
Evan Chengcd8e66a2008-11-11 21:48:44 +0000952static unsigned getAddrModeUPBits(unsigned Mode) {
953 unsigned Binary = 0;
Evan Cheng7602e112008-09-02 06:52:38 +0000954
955 // Set addressing mode by modifying bits U(23) and P(24)
956 // IA - Increment after - bit U = 1 and bit P = 0
957 // IB - Increment before - bit U = 1 and bit P = 1
958 // DA - Decrement after - bit U = 0 and bit P = 0
959 // DB - Decrement before - bit U = 0 and bit P = 1
Evan Cheng7602e112008-09-02 06:52:38 +0000960 switch (Mode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000961 default: llvm_unreachable("Unknown addressing sub-mode!");
Evan Cheng10bf7342009-09-09 23:55:03 +0000962 case ARM_AM::da: break;
Evan Cheng97f48c32008-11-06 22:15:19 +0000963 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
964 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
965 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
Evan Cheng7602e112008-09-02 06:52:38 +0000966 }
967
Evan Chengcd8e66a2008-11-11 21:48:44 +0000968 return Binary;
969}
970
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000971template<class CodeEmitter>
972void Emitter<CodeEmitter>::emitLoadStoreMultipleInstruction(
973 const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +0000974 // Part of binary is determined by TableGn.
975 unsigned Binary = getBinaryCodeForInstr(MI);
976
977 // Set the conditional execution predicate
978 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
979
980 // Set base address operand
981 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
982
983 // Set addressing mode by modifying bits U(23) and P(24)
984 const MachineOperand &MO = MI.getOperand(1);
985 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
986
Evan Cheng7602e112008-09-02 06:52:38 +0000987 // Set bit W(21)
988 if (ARM_AM::getAM4WBFlag(MO.getImm()))
Evan Cheng97f48c32008-11-06 22:15:19 +0000989 Binary |= 0x1 << ARMII::W_BitShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000990
991 // Set registers
992 for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
993 const MachineOperand &MO = MI.getOperand(i);
Evan Chengcd8e66a2008-11-11 21:48:44 +0000994 if (!MO.isReg() || MO.isImplicit())
995 break;
Evan Cheng7602e112008-09-02 06:52:38 +0000996 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
997 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
998 RegNum < 16);
999 Binary |= 0x1 << RegNum;
1000 }
1001
Evan Cheng83b5cf02008-11-05 23:22:34 +00001002 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +00001003}
1004
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001005template<class CodeEmitter>
1006void Emitter<CodeEmitter>::emitMulFrmInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001007 const TargetInstrDesc &TID = MI.getDesc();
1008
1009 // Part of binary is determined by TableGn.
1010 unsigned Binary = getBinaryCodeForInstr(MI);
1011
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001012 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001013 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001014
1015 // Encode S bit if MI modifies CPSR.
1016 Binary |= getAddrModeSBit(MI, TID);
1017
1018 // 32x32->64bit operations have two destination registers. The number
1019 // of register definitions will tell us if that's what we're dealing with.
Evan Cheng97f48c32008-11-06 22:15:19 +00001020 unsigned OpIdx = 0;
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001021 if (TID.getNumDefs() == 2)
1022 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
1023
1024 // Encode Rd
1025 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
1026
1027 // Encode Rm
1028 Binary |= getMachineOpValue(MI, OpIdx++);
1029
1030 // Encode Rs
1031 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
1032
Evan Chengfbc9d412008-11-06 01:21:28 +00001033 // Many multiple instructions (e.g. MLA) have three src operands. Encode
1034 // it as Rn (for multiply, that's in the same offset as RdLo.
Evan Cheng97f48c32008-11-06 22:15:19 +00001035 if (TID.getNumOperands() > OpIdx &&
1036 !TID.OpInfo[OpIdx].isPredicate() &&
1037 !TID.OpInfo[OpIdx].isOptionalDef())
1038 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
1039
1040 emitWordLE(Binary);
1041}
1042
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001043template<class CodeEmitter>
1044void Emitter<CodeEmitter>::emitExtendInstruction(const MachineInstr &MI) {
Evan Cheng97f48c32008-11-06 22:15:19 +00001045 const TargetInstrDesc &TID = MI.getDesc();
1046
1047 // Part of binary is determined by TableGn.
1048 unsigned Binary = getBinaryCodeForInstr(MI);
1049
1050 // Set the conditional execution predicate
1051 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1052
1053 unsigned OpIdx = 0;
1054
1055 // Encode Rd
1056 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1057
1058 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
1059 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1060 if (MO2.isReg()) {
1061 // Two register operand form.
1062 // Encode Rn.
1063 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
1064
1065 // Encode Rm.
1066 Binary |= getMachineOpValue(MI, MO2);
1067 ++OpIdx;
1068 } else {
1069 Binary |= getMachineOpValue(MI, MO1);
1070 }
1071
1072 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
1073 if (MI.getOperand(OpIdx).isImm() &&
1074 !TID.OpInfo[OpIdx].isPredicate() &&
1075 !TID.OpInfo[OpIdx].isOptionalDef())
1076 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
Evan Chengfbc9d412008-11-06 01:21:28 +00001077
Evan Cheng83b5cf02008-11-05 23:22:34 +00001078 emitWordLE(Binary);
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001079}
1080
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001081template<class CodeEmitter>
1082void Emitter<CodeEmitter>::emitMiscArithInstruction(const MachineInstr &MI) {
Evan Cheng8b59db32008-11-07 01:41:35 +00001083 const TargetInstrDesc &TID = MI.getDesc();
1084
1085 // Part of binary is determined by TableGn.
1086 unsigned Binary = getBinaryCodeForInstr(MI);
1087
1088 // Set the conditional execution predicate
1089 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1090
1091 unsigned OpIdx = 0;
1092
1093 // Encode Rd
1094 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1095
1096 const MachineOperand &MO = MI.getOperand(OpIdx++);
1097 if (OpIdx == TID.getNumOperands() ||
1098 TID.OpInfo[OpIdx].isPredicate() ||
1099 TID.OpInfo[OpIdx].isOptionalDef()) {
1100 // Encode Rm and it's done.
1101 Binary |= getMachineOpValue(MI, MO);
1102 emitWordLE(Binary);
1103 return;
1104 }
1105
1106 // Encode Rn.
1107 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1108
1109 // Encode Rm.
1110 Binary |= getMachineOpValue(MI, OpIdx++);
1111
1112 // Encode shift_imm.
1113 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
1114 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1115 Binary |= ShiftAmt << ARMII::ShiftShift;
Jim Grosbach764ab522009-08-11 15:33:49 +00001116
Evan Cheng8b59db32008-11-07 01:41:35 +00001117 emitWordLE(Binary);
1118}
1119
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001120template<class CodeEmitter>
1121void Emitter<CodeEmitter>::emitBranchInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001122 const TargetInstrDesc &TID = MI.getDesc();
1123
Torok Edwindac237e2009-07-08 20:53:28 +00001124 if (TID.Opcode == ARM::TPsoft) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001125 llvm_unreachable("ARM::TPsoft FIXME"); // FIXME
Torok Edwindac237e2009-07-08 20:53:28 +00001126 }
Evan Cheng12c3a532008-11-06 17:48:05 +00001127
Evan Cheng7602e112008-09-02 06:52:38 +00001128 // Part of binary is determined by TableGn.
1129 unsigned Binary = getBinaryCodeForInstr(MI);
1130
Evan Chengedda31c2008-11-05 18:35:52 +00001131 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001132 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001133
1134 // Set signed_immed_24 field
1135 Binary |= getMachineOpValue(MI, 0);
1136
Evan Cheng83b5cf02008-11-05 23:22:34 +00001137 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +00001138}
1139
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001140template<class CodeEmitter>
1141void Emitter<CodeEmitter>::emitInlineJumpTable(unsigned JTIndex) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001142 // Remember the base address of the inline jump table.
Evan Cheng5788d1a2008-12-10 02:32:19 +00001143 uintptr_t JTBase = MCE.getCurrentPCValue();
Evan Cheng437c1732008-11-07 22:30:53 +00001144 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
Chris Lattner893e1c92009-08-23 06:49:22 +00001145 DEBUG(errs() << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase
1146 << '\n');
Evan Cheng4df60f52008-11-07 09:06:08 +00001147
1148 // Now emit the jump table entries.
1149 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1150 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1151 if (IsPIC)
1152 // DestBB address - JT base.
Evan Cheng437c1732008-11-07 22:30:53 +00001153 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
Evan Cheng4df60f52008-11-07 09:06:08 +00001154 else
1155 // Absolute DestBB address.
1156 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1157 emitWordLE(0);
1158 }
1159}
1160
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001161template<class CodeEmitter>
1162void Emitter<CodeEmitter>::emitMiscBranchInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001163 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +00001164
Evan Cheng437c1732008-11-07 22:30:53 +00001165 // Handle jump tables.
Evan Cheng90daf4d2009-07-25 00:13:11 +00001166 if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
Evan Cheng437c1732008-11-07 22:30:53 +00001167 // First emit a ldr pc, [] instruction.
1168 emitDataProcessingInstruction(MI, ARM::PC);
1169
1170 // Then emit the inline jump table.
Evan Chengc9a41532009-07-08 00:05:05 +00001171 unsigned JTIndex =
Evan Cheng90daf4d2009-07-25 00:13:11 +00001172 (TID.Opcode == ARM::BR_JTr)
Evan Cheng437c1732008-11-07 22:30:53 +00001173 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1174 emitInlineJumpTable(JTIndex);
1175 return;
Evan Cheng90daf4d2009-07-25 00:13:11 +00001176 } else if (TID.Opcode == ARM::BR_JTm) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001177 // First emit a ldr pc, [] instruction.
1178 emitLoadStoreInstruction(MI, ARM::PC);
1179
1180 // Then emit the inline jump table.
Evan Cheng437c1732008-11-07 22:30:53 +00001181 emitInlineJumpTable(MI.getOperand(3).getIndex());
Evan Cheng4df60f52008-11-07 09:06:08 +00001182 return;
1183 }
1184
Evan Chengedda31c2008-11-05 18:35:52 +00001185 // Part of binary is determined by TableGn.
1186 unsigned Binary = getBinaryCodeForInstr(MI);
1187
1188 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001189 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001190
1191 if (TID.Opcode == ARM::BX_RET)
1192 // The return register is LR.
1193 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
Jim Grosbach764ab522009-08-11 15:33:49 +00001194 else
Evan Chengedda31c2008-11-05 18:35:52 +00001195 // otherwise, set the return register
1196 Binary |= getMachineOpValue(MI, 0);
1197
Evan Cheng83b5cf02008-11-05 23:22:34 +00001198 emitWordLE(Binary);
Evan Cheng148b6a42007-07-05 21:15:40 +00001199}
Evan Cheng7602e112008-09-02 06:52:38 +00001200
Evan Cheng80a11982008-11-12 06:41:41 +00001201static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) {
Evan Chengd06d48d2008-11-12 02:19:38 +00001202 unsigned RegD = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001203 unsigned Binary = 0;
Evan Chengd06d48d2008-11-12 02:19:38 +00001204 bool isSPVFP = false;
Evan Cheng8295d992009-07-22 05:55:18 +00001205 RegD = ARMRegisterInfo::getRegisterNumbering(RegD, &isSPVFP);
Evan Chengd06d48d2008-11-12 02:19:38 +00001206 if (!isSPVFP)
1207 Binary |= RegD << ARMII::RegRdShift;
1208 else {
1209 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1210 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1211 }
Evan Cheng80a11982008-11-12 06:41:41 +00001212 return Binary;
1213}
Evan Cheng78be83d2008-11-11 19:40:26 +00001214
Evan Cheng80a11982008-11-12 06:41:41 +00001215static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) {
Evan Chengd06d48d2008-11-12 02:19:38 +00001216 unsigned RegN = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001217 unsigned Binary = 0;
1218 bool isSPVFP = false;
Evan Cheng8295d992009-07-22 05:55:18 +00001219 RegN = ARMRegisterInfo::getRegisterNumbering(RegN, &isSPVFP);
Evan Chengd06d48d2008-11-12 02:19:38 +00001220 if (!isSPVFP)
1221 Binary |= RegN << ARMII::RegRnShift;
1222 else {
1223 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1224 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1225 }
Evan Cheng80a11982008-11-12 06:41:41 +00001226 return Binary;
1227}
Evan Chengd06d48d2008-11-12 02:19:38 +00001228
Evan Cheng80a11982008-11-12 06:41:41 +00001229static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) {
1230 unsigned RegM = MI.getOperand(OpIdx).getReg();
1231 unsigned Binary = 0;
1232 bool isSPVFP = false;
Evan Cheng8295d992009-07-22 05:55:18 +00001233 RegM = ARMRegisterInfo::getRegisterNumbering(RegM, &isSPVFP);
Evan Cheng80a11982008-11-12 06:41:41 +00001234 if (!isSPVFP)
1235 Binary |= RegM;
1236 else {
1237 Binary |= ((RegM & 0x1E) >> 1);
1238 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
Evan Cheng78be83d2008-11-11 19:40:26 +00001239 }
Evan Cheng80a11982008-11-12 06:41:41 +00001240 return Binary;
1241}
1242
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001243template<class CodeEmitter>
1244void Emitter<CodeEmitter>::emitVFPArithInstruction(const MachineInstr &MI) {
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001245 const TargetInstrDesc &TID = MI.getDesc();
1246
1247 // Part of binary is determined by TableGn.
1248 unsigned Binary = getBinaryCodeForInstr(MI);
1249
1250 // Set the conditional execution predicate
1251 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1252
1253 unsigned OpIdx = 0;
1254 assert((Binary & ARMII::D_BitShift) == 0 &&
1255 (Binary & ARMII::N_BitShift) == 0 &&
1256 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1257
1258 // Encode Dd / Sd.
1259 Binary |= encodeVFPRd(MI, OpIdx++);
1260
1261 // If this is a two-address operand, skip it, e.g. FMACD.
1262 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1263 ++OpIdx;
1264
1265 // Encode Dn / Sn.
1266 if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
Evan Cheng3f4924e2008-11-12 08:14:21 +00001267 Binary |= encodeVFPRn(MI, OpIdx++);
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001268
1269 if (OpIdx == TID.getNumOperands() ||
1270 TID.OpInfo[OpIdx].isPredicate() ||
1271 TID.OpInfo[OpIdx].isOptionalDef()) {
1272 // FCMPEZD etc. has only one operand.
1273 emitWordLE(Binary);
1274 return;
1275 }
1276
1277 // Encode Dm / Sm.
1278 Binary |= encodeVFPRm(MI, OpIdx);
Jim Grosbach764ab522009-08-11 15:33:49 +00001279
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001280 emitWordLE(Binary);
1281}
1282
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001283template<class CodeEmitter>
1284void Emitter<CodeEmitter>::emitVFPConversionInstruction(
1285 const MachineInstr &MI) {
Evan Cheng80a11982008-11-12 06:41:41 +00001286 const TargetInstrDesc &TID = MI.getDesc();
1287 unsigned Form = TID.TSFlags & ARMII::FormMask;
1288
1289 // Part of binary is determined by TableGn.
1290 unsigned Binary = getBinaryCodeForInstr(MI);
1291
1292 // Set the conditional execution predicate
1293 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1294
1295 switch (Form) {
1296 default: break;
1297 case ARMII::VFPConv1Frm:
1298 case ARMII::VFPConv2Frm:
1299 case ARMII::VFPConv3Frm:
1300 // Encode Dd / Sd.
1301 Binary |= encodeVFPRd(MI, 0);
1302 break;
1303 case ARMII::VFPConv4Frm:
1304 // Encode Dn / Sn.
1305 Binary |= encodeVFPRn(MI, 0);
1306 break;
1307 case ARMII::VFPConv5Frm:
1308 // Encode Dm / Sm.
1309 Binary |= encodeVFPRm(MI, 0);
1310 break;
1311 }
1312
1313 switch (Form) {
1314 default: break;
1315 case ARMII::VFPConv1Frm:
1316 // Encode Dm / Sm.
1317 Binary |= encodeVFPRm(MI, 1);
Evan Cheng67fd91f2008-11-13 07:46:59 +00001318 break;
Evan Cheng80a11982008-11-12 06:41:41 +00001319 case ARMII::VFPConv2Frm:
1320 case ARMII::VFPConv3Frm:
1321 // Encode Dn / Sn.
1322 Binary |= encodeVFPRn(MI, 1);
1323 break;
1324 case ARMII::VFPConv4Frm:
1325 case ARMII::VFPConv5Frm:
1326 // Encode Dd / Sd.
1327 Binary |= encodeVFPRd(MI, 1);
1328 break;
1329 }
1330
1331 if (Form == ARMII::VFPConv5Frm)
1332 // Encode Dn / Sn.
1333 Binary |= encodeVFPRn(MI, 2);
1334 else if (Form == ARMII::VFPConv3Frm)
1335 // Encode Dm / Sm.
1336 Binary |= encodeVFPRm(MI, 2);
Evan Cheng78be83d2008-11-11 19:40:26 +00001337
1338 emitWordLE(Binary);
1339}
1340
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001341template<class CodeEmitter>
1342void Emitter<CodeEmitter>::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001343 // Part of binary is determined by TableGn.
1344 unsigned Binary = getBinaryCodeForInstr(MI);
1345
1346 // Set the conditional execution predicate
1347 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1348
1349 unsigned OpIdx = 0;
1350
1351 // Encode Dd / Sd.
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001352 Binary |= encodeVFPRd(MI, OpIdx++);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001353
1354 // Encode address base.
1355 const MachineOperand &Base = MI.getOperand(OpIdx++);
1356 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1357
1358 // If there is a non-zero immediate offset, encode it.
1359 if (Base.isReg()) {
1360 const MachineOperand &Offset = MI.getOperand(OpIdx);
1361 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1362 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1363 Binary |= 1 << ARMII::U_BitShift;
Evan Cheng607f1b42008-11-12 08:21:12 +00001364 Binary |= ImmOffs;
Evan Chengcd8e66a2008-11-11 21:48:44 +00001365 emitWordLE(Binary);
1366 return;
1367 }
1368 }
1369
1370 // If immediate offset is omitted, default to +0.
1371 Binary |= 1 << ARMII::U_BitShift;
1372
1373 emitWordLE(Binary);
1374}
1375
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001376template<class CodeEmitter>
1377void Emitter<CodeEmitter>::emitVFPLoadStoreMultipleInstruction(
1378 const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001379 // Part of binary is determined by TableGn.
1380 unsigned Binary = getBinaryCodeForInstr(MI);
1381
1382 // Set the conditional execution predicate
1383 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1384
1385 // Set base address operand
1386 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
1387
1388 // Set addressing mode by modifying bits U(23) and P(24)
1389 const MachineOperand &MO = MI.getOperand(1);
1390 Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm()));
1391
1392 // Set bit W(21)
1393 if (ARM_AM::getAM5WBFlag(MO.getImm()))
1394 Binary |= 0x1 << ARMII::W_BitShift;
1395
1396 // First register is encoded in Dd.
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001397 Binary |= encodeVFPRd(MI, 4);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001398
1399 // Number of registers are encoded in offset field.
1400 unsigned NumRegs = 1;
1401 for (unsigned i = 5, e = MI.getNumOperands(); i != e; ++i) {
1402 const MachineOperand &MO = MI.getOperand(i);
1403 if (!MO.isReg() || MO.isImplicit())
1404 break;
1405 ++NumRegs;
1406 }
1407 Binary |= NumRegs * 2;
1408
1409 emitWordLE(Binary);
1410}
1411
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001412template<class CodeEmitter>
1413void Emitter<CodeEmitter>::emitMiscInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001414 // Part of binary is determined by TableGn.
1415 unsigned Binary = getBinaryCodeForInstr(MI);
1416
1417 // Set the conditional execution predicate
1418 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1419
1420 emitWordLE(Binary);
1421}
1422
Evan Cheng7602e112008-09-02 06:52:38 +00001423#include "ARMGenCodeEmitter.inc"