blob: aa2822c2c36a73117695a841ce604234c3805a5d [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;
63 TargetMachine &TM;
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000064 CodeEmitter &MCE;
Evan Cheng938b9d82008-10-31 19:55:13 +000065 const std::vector<MachineConstantPoolEntry> *MCPEs;
Evan Cheng4df60f52008-11-07 09:06:08 +000066 const std::vector<MachineJumpTableEntry> *MJTEs;
67 bool IsPIC;
68
Evan Cheng148b6a42007-07-05 21:15:40 +000069 public:
70 static char ID;
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000071 explicit Emitter(TargetMachine &tm, CodeEmitter &mce)
Evan Cheng057d0c32008-09-18 07:28:19 +000072 : MachineFunctionPass(&ID), JTI(0), II(0), TD(0), TM(tm),
Evan Cheng4df60f52008-11-07 09:06:08 +000073 MCE(mce), MCPEs(0), MJTEs(0),
74 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000075 Emitter(TargetMachine &tm, CodeEmitter &mce,
Evan Cheng148b6a42007-07-05 21:15:40 +000076 const ARMInstrInfo &ii, const TargetData &td)
Evan Cheng057d0c32008-09-18 07:28:19 +000077 : MachineFunctionPass(&ID), JTI(0), II(&ii), TD(&td), TM(tm),
Evan Cheng4df60f52008-11-07 09:06:08 +000078 MCE(mce), MCPEs(0), MJTEs(0),
79 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Evan Cheng148b6a42007-07-05 21:15:40 +000080
81 bool runOnMachineFunction(MachineFunction &MF);
82
83 virtual const char *getPassName() const {
84 return "ARM Machine Code Emitter";
85 }
86
87 void emitInstruction(const MachineInstr &MI);
Evan Cheng7602e112008-09-02 06:52:38 +000088
89 private:
Evan Cheng057d0c32008-09-18 07:28:19 +000090
Evan Cheng83b5cf02008-11-05 23:22:34 +000091 void emitWordLE(unsigned Binary);
92
Evan Chengcb5201f2008-11-11 22:19:31 +000093 void emitDWordLE(uint64_t Binary);
94
Evan Cheng057d0c32008-09-18 07:28:19 +000095 void emitConstPoolInstruction(const MachineInstr &MI);
96
Evan Cheng90922132008-11-06 02:25:39 +000097 void emitMOVi2piecesInstruction(const MachineInstr &MI);
98
Evan Cheng4df60f52008-11-07 09:06:08 +000099 void emitLEApcrelJTInstruction(const MachineInstr &MI);
100
Evan Chenga9562552008-11-14 20:09:11 +0000101 void emitPseudoMoveInstruction(const MachineInstr &MI);
102
Evan Cheng83b5cf02008-11-05 23:22:34 +0000103 void addPCLabel(unsigned LabelID);
104
Evan Cheng057d0c32008-09-18 07:28:19 +0000105 void emitPseudoInstruction(const MachineInstr &MI);
106
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000107 unsigned getMachineSoRegOpValue(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000108 const TargetInstrDesc &TID,
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000109 const MachineOperand &MO,
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000110 unsigned OpIdx);
111
Evan Cheng90922132008-11-06 02:25:39 +0000112 unsigned getMachineSoImmOpValue(unsigned SoImm);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000113
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +0000114 unsigned getAddrModeSBit(const MachineInstr &MI,
115 const TargetInstrDesc &TID) const;
Evan Cheng49a9f292008-09-12 22:45:55 +0000116
Evan Cheng83b5cf02008-11-05 23:22:34 +0000117 void emitDataProcessingInstruction(const MachineInstr &MI,
Evan Cheng437c1732008-11-07 22:30:53 +0000118 unsigned ImplicitRd = 0,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000119 unsigned ImplicitRn = 0);
Evan Cheng7602e112008-09-02 06:52:38 +0000120
Evan Cheng83b5cf02008-11-05 23:22:34 +0000121 void emitLoadStoreInstruction(const MachineInstr &MI,
Evan Cheng4df60f52008-11-07 09:06:08 +0000122 unsigned ImplicitRd = 0,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000123 unsigned ImplicitRn = 0);
Evan Chengedda31c2008-11-05 18:35:52 +0000124
Evan Cheng83b5cf02008-11-05 23:22:34 +0000125 void emitMiscLoadStoreInstruction(const MachineInstr &MI,
126 unsigned ImplicitRn = 0);
Evan Chengedda31c2008-11-05 18:35:52 +0000127
128 void emitLoadStoreMultipleInstruction(const MachineInstr &MI);
129
Evan Chengfbc9d412008-11-06 01:21:28 +0000130 void emitMulFrmInstruction(const MachineInstr &MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000131
Evan Cheng97f48c32008-11-06 22:15:19 +0000132 void emitExtendInstruction(const MachineInstr &MI);
133
Evan Cheng8b59db32008-11-07 01:41:35 +0000134 void emitMiscArithInstruction(const MachineInstr &MI);
135
Evan Chengedda31c2008-11-05 18:35:52 +0000136 void emitBranchInstruction(const MachineInstr &MI);
137
Evan Cheng437c1732008-11-07 22:30:53 +0000138 void emitInlineJumpTable(unsigned JTIndex);
Evan Cheng4df60f52008-11-07 09:06:08 +0000139
Evan Chengedda31c2008-11-05 18:35:52 +0000140 void emitMiscBranchInstruction(const MachineInstr &MI);
Evan Cheng7602e112008-09-02 06:52:38 +0000141
Evan Cheng96581d32008-11-11 02:11:05 +0000142 void emitVFPArithInstruction(const MachineInstr &MI);
143
Evan Cheng78be83d2008-11-11 19:40:26 +0000144 void emitVFPConversionInstruction(const MachineInstr &MI);
145
Evan Chengcd8e66a2008-11-11 21:48:44 +0000146 void emitVFPLoadStoreInstruction(const MachineInstr &MI);
147
148 void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI);
149
150 void emitMiscInstruction(const MachineInstr &MI);
151
Evan Cheng7602e112008-09-02 06:52:38 +0000152 /// getMachineOpValue - Return binary encoding of operand. If the machine
153 /// operand requires relocation, record the relocation and return zero.
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000154 unsigned getMachineOpValue(const MachineInstr &MI,const MachineOperand &MO);
Evan Cheng7602e112008-09-02 06:52:38 +0000155 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) {
156 return getMachineOpValue(MI, MI.getOperand(OpIdx));
157 }
Evan Cheng7602e112008-09-02 06:52:38 +0000158
Evan Cheng83b5cf02008-11-05 23:22:34 +0000159 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng7602e112008-09-02 06:52:38 +0000160 ///
Evan Cheng83b5cf02008-11-05 23:22:34 +0000161 unsigned getShiftOp(unsigned Imm) const ;
Evan Cheng7602e112008-09-02 06:52:38 +0000162
163 /// Routines that handle operands which add machine relocations which are
Evan Cheng437c1732008-11-07 22:30:53 +0000164 /// fixed up by the relocation stage.
Evan Cheng057d0c32008-09-18 07:28:19 +0000165 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
Evan Cheng413a89f2008-11-07 22:57:53 +0000166 bool NeedStub, intptr_t ACPV = 0);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000167 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
Evan Cheng437c1732008-11-07 22:30:53 +0000168 void emitConstPoolAddress(unsigned CPI, unsigned Reloc);
169 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc);
170 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
171 intptr_t JTBase = 0);
Evan Cheng148b6a42007-07-05 21:15:40 +0000172 };
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000173 template <class CodeEmitter>
174 char Emitter<CodeEmitter>::ID = 0;
Evan Cheng148b6a42007-07-05 21:15:40 +0000175}
176
177/// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
178/// to the specified MCE object.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000179
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000180FunctionPass *llvm::createARMCodeEmitterPass(ARMBaseTargetMachine &TM,
181 MachineCodeEmitter &MCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000182 return new Emitter<MachineCodeEmitter>(TM, MCE);
183}
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000184FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
185 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000186 return new Emitter<JITCodeEmitter>(TM, JCE);
Evan Cheng148b6a42007-07-05 21:15:40 +0000187}
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000188FunctionPass *llvm::createARMObjectCodeEmitterPass(ARMBaseTargetMachine &TM,
189 ObjectCodeEmitter &OCE) {
190 return new Emitter<ObjectCodeEmitter>(TM, OCE);
191}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000192
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000193template<class CodeEmitter>
194bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000195 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
196 MF.getTarget().getRelocationModel() != Reloc::Static) &&
197 "JIT relocation model must be set to static or default!");
198 II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo();
199 TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData();
Evan Cheng057d0c32008-09-18 07:28:19 +0000200 JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo();
Evan Cheng938b9d82008-10-31 19:55:13 +0000201 MCPEs = &MF.getConstantPool()->getConstants();
Evan Cheng4df60f52008-11-07 09:06:08 +0000202 MJTEs = &MF.getJumpTableInfo()->getJumpTables();
203 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Evan Cheng3cc82232008-11-08 07:38:22 +0000204 JTI->Initialize(MF, IsPIC);
Evan Cheng148b6a42007-07-05 21:15:40 +0000205
206 do {
Jim Grosbach764ab522009-08-11 15:33:49 +0000207 DEBUG(errs() << "JITTing function '"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000208 << MF.getFunction()->getName() << "'\n");
Evan Cheng148b6a42007-07-05 21:15:40 +0000209 MCE.startFunction(MF);
Jim Grosbach764ab522009-08-11 15:33:49 +0000210 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
Evan Cheng148b6a42007-07-05 21:15:40 +0000211 MBB != E; ++MBB) {
212 MCE.StartMachineBasicBlock(MBB);
213 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
214 I != E; ++I)
215 emitInstruction(*I);
216 }
217 } while (MCE.finishFunction(MF));
218
219 return false;
220}
221
Evan Cheng83b5cf02008-11-05 23:22:34 +0000222/// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng7602e112008-09-02 06:52:38 +0000223///
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000224template<class CodeEmitter>
225unsigned Emitter<CodeEmitter>::getShiftOp(unsigned Imm) const {
Evan Cheng83b5cf02008-11-05 23:22:34 +0000226 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000227 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng7602e112008-09-02 06:52:38 +0000228 case ARM_AM::asr: return 2;
229 case ARM_AM::lsl: return 0;
230 case ARM_AM::lsr: return 1;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000231 case ARM_AM::ror:
Evan Cheng7602e112008-09-02 06:52:38 +0000232 case ARM_AM::rrx: return 3;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000233 }
Evan Cheng7602e112008-09-02 06:52:38 +0000234 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000235}
236
Evan Cheng7602e112008-09-02 06:52:38 +0000237/// getMachineOpValue - Return binary encoding of operand. If the machine
238/// operand requires relocation, record the relocation and return zero.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000239template<class CodeEmitter>
240unsigned Emitter<CodeEmitter>::getMachineOpValue(const MachineInstr &MI,
241 const MachineOperand &MO) {
Dan Gohmand735b802008-10-03 15:45:36 +0000242 if (MO.isReg())
Evan Cheng7602e112008-09-02 06:52:38 +0000243 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000244 else if (MO.isImm())
Evan Cheng7602e112008-09-02 06:52:38 +0000245 return static_cast<unsigned>(MO.getImm());
Dan Gohmand735b802008-10-03 15:45:36 +0000246 else if (MO.isGlobal())
Jim Grosbach016d34c2008-10-03 15:52:42 +0000247 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true);
Dan Gohmand735b802008-10-03 15:45:36 +0000248 else if (MO.isSymbol())
Evan Cheng10332512008-11-08 07:22:33 +0000249 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
Evan Cheng580c0df2008-11-12 01:02:24 +0000250 else if (MO.isCPI()) {
251 const TargetInstrDesc &TID = MI.getDesc();
252 // For VFP load, the immediate offset is multiplied by 4.
253 unsigned Reloc = ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
254 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
255 emitConstPoolAddress(MO.getIndex(), Reloc);
256 } else if (MO.isJTI())
Chris Lattner8aa797a2007-12-30 23:10:15 +0000257 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
Dan Gohmand735b802008-10-03 15:45:36 +0000258 else if (MO.isMBB())
Evan Cheng4df60f52008-11-07 09:06:08 +0000259 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
Evan Cheng2aa0e642008-09-13 01:55:59 +0000260 else {
Torok Edwindac237e2009-07-08 20:53:28 +0000261#ifndef NDEBUG
Chris Lattner705e07f2009-08-23 03:41:05 +0000262 errs() << MO;
Torok Edwindac237e2009-07-08 20:53:28 +0000263#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000264 llvm_unreachable(0);
Evan Cheng2aa0e642008-09-13 01:55:59 +0000265 }
Evan Cheng7602e112008-09-02 06:52:38 +0000266 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000267}
268
Evan Cheng057d0c32008-09-18 07:28:19 +0000269/// emitGlobalAddress - Emit the specified address to the code stream.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000270///
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000271template<class CodeEmitter>
272void Emitter<CodeEmitter>::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
273 bool NeedStub, intptr_t ACPV) {
274 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
275 GV, ACPV, NeedStub));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000276}
277
278/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
279/// be emitted to the current location in the function, and allow it to be PC
280/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000281template<class CodeEmitter>
282void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES,
283 unsigned Reloc) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000284 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
285 Reloc, ES));
286}
287
288/// emitConstPoolAddress - Arrange for the address of an constant pool
289/// to be emitted to the current location in the function, and allow it to be PC
290/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000291template<class CodeEmitter>
292void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI,
293 unsigned Reloc) {
Evan Cheng0f282432008-10-29 23:55:43 +0000294 // Tell JIT emitter we'll resolve the address.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000295 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000296 Reloc, CPI, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000297}
298
299/// emitJumpTableAddress - Arrange for the address of a jump table to
300/// be emitted to the current location in the function, and allow it to be PC
301/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000302template<class CodeEmitter>
Jim Grosbach764ab522009-08-11 15:33:49 +0000303void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTIndex,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000304 unsigned Reloc) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000305 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000306 Reloc, JTIndex, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000307}
308
Raul Herbster9c1a3822007-08-30 23:29:26 +0000309/// emitMachineBasicBlock - Emit the specified address basic block.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000310template<class CodeEmitter>
311void Emitter<CodeEmitter>::emitMachineBasicBlock(MachineBasicBlock *BB,
312 unsigned Reloc, intptr_t JTBase) {
Raul Herbster9c1a3822007-08-30 23:29:26 +0000313 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000314 Reloc, BB, JTBase));
Raul Herbster9c1a3822007-08-30 23:29:26 +0000315}
Evan Cheng0ff94f72007-08-07 01:37:15 +0000316
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000317template<class CodeEmitter>
318void Emitter<CodeEmitter>::emitWordLE(unsigned Binary) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000319 DEBUG(errs() << " 0x";
320 errs().write_hex(Binary) << "\n");
Evan Cheng83b5cf02008-11-05 23:22:34 +0000321 MCE.emitWordLE(Binary);
322}
323
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000324template<class CodeEmitter>
325void Emitter<CodeEmitter>::emitDWordLE(uint64_t Binary) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000326 DEBUG(errs() << " 0x";
327 errs().write_hex(Binary) << "\n");
Evan Chengcb5201f2008-11-11 22:19:31 +0000328 MCE.emitDWordLE(Binary);
329}
330
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000331template<class CodeEmitter>
332void Emitter<CodeEmitter>::emitInstruction(const MachineInstr &MI) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000333 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
Evan Cheng42d5ee062008-09-13 01:15:21 +0000334
Jeffrey Yasskin75402822009-07-17 18:49:39 +0000335 MCE.processDebugLoc(MI.getDebugLoc());
336
Evan Cheng148b6a42007-07-05 21:15:40 +0000337 NumEmitted++; // Keep track of the # of mi's emitted
Evan Chengedda31c2008-11-05 18:35:52 +0000338 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
Evan Chengffa6d962008-11-13 23:36:57 +0000339 default: {
Torok Edwinc23197a2009-07-14 16:55:14 +0000340 llvm_unreachable("Unhandled instruction encoding format!");
Evan Chengedda31c2008-11-05 18:35:52 +0000341 break;
Evan Chengffa6d962008-11-13 23:36:57 +0000342 }
Evan Chengedda31c2008-11-05 18:35:52 +0000343 case ARMII::Pseudo:
Evan Cheng057d0c32008-09-18 07:28:19 +0000344 emitPseudoInstruction(MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000345 break;
346 case ARMII::DPFrm:
347 case ARMII::DPSoRegFrm:
348 emitDataProcessingInstruction(MI);
349 break;
Evan Cheng148cad82008-11-13 07:34:59 +0000350 case ARMII::LdFrm:
351 case ARMII::StFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000352 emitLoadStoreInstruction(MI);
353 break;
Evan Cheng148cad82008-11-13 07:34:59 +0000354 case ARMII::LdMiscFrm:
355 case ARMII::StMiscFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000356 emitMiscLoadStoreInstruction(MI);
357 break;
Evan Cheng3c4a4ff2008-11-12 07:18:38 +0000358 case ARMII::LdStMulFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000359 emitLoadStoreMultipleInstruction(MI);
360 break;
Evan Chengfbc9d412008-11-06 01:21:28 +0000361 case ARMII::MulFrm:
362 emitMulFrmInstruction(MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000363 break;
Evan Cheng97f48c32008-11-06 22:15:19 +0000364 case ARMII::ExtFrm:
365 emitExtendInstruction(MI);
366 break;
Evan Cheng8b59db32008-11-07 01:41:35 +0000367 case ARMII::ArithMiscFrm:
368 emitMiscArithInstruction(MI);
369 break;
Evan Cheng12c3a532008-11-06 17:48:05 +0000370 case ARMII::BrFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000371 emitBranchInstruction(MI);
372 break;
Evan Cheng12c3a532008-11-06 17:48:05 +0000373 case ARMII::BrMiscFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000374 emitMiscBranchInstruction(MI);
375 break;
Evan Cheng96581d32008-11-11 02:11:05 +0000376 // VFP instructions.
377 case ARMII::VFPUnaryFrm:
378 case ARMII::VFPBinaryFrm:
379 emitVFPArithInstruction(MI);
380 break;
Evan Cheng78be83d2008-11-11 19:40:26 +0000381 case ARMII::VFPConv1Frm:
382 case ARMII::VFPConv2Frm:
Evan Cheng0a0ab132008-11-11 22:46:12 +0000383 case ARMII::VFPConv3Frm:
Evan Cheng80a11982008-11-12 06:41:41 +0000384 case ARMII::VFPConv4Frm:
385 case ARMII::VFPConv5Frm:
Evan Cheng78be83d2008-11-11 19:40:26 +0000386 emitVFPConversionInstruction(MI);
387 break;
Evan Chengcd8e66a2008-11-11 21:48:44 +0000388 case ARMII::VFPLdStFrm:
389 emitVFPLoadStoreInstruction(MI);
390 break;
391 case ARMII::VFPLdStMulFrm:
392 emitVFPLoadStoreMultipleInstruction(MI);
393 break;
394 case ARMII::VFPMiscFrm:
395 emitMiscInstruction(MI);
396 break;
Evan Chengedda31c2008-11-05 18:35:52 +0000397 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000398}
399
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000400template<class CodeEmitter>
401void Emitter<CodeEmitter>::emitConstPoolInstruction(const MachineInstr &MI) {
Evan Cheng437c1732008-11-07 22:30:53 +0000402 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
403 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
Evan Cheng938b9d82008-10-31 19:55:13 +0000404 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
Jim Grosbach764ab522009-08-11 15:33:49 +0000405
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000406 // Remember the CONSTPOOL_ENTRY address for later relocation.
407 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
408
409 // Emit constpool island entry. In most cases, the actual values will be
410 // resolved and relocated after code emission.
411 if (MCPE.isMachineConstantPoolEntry()) {
412 ARMConstantPoolValue *ACPV =
413 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
414
Chris Lattner705e07f2009-08-23 03:41:05 +0000415 DEBUG(errs() << " ** ARM constant pool #" << CPI << " @ "
416 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n');
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000417
418 GlobalValue *GV = ACPV->getGV();
419 if (GV) {
420 assert(!ACPV->isStub() && "Don't know how to deal this yet!");
Evan Chenge96a4902008-11-08 01:31:27 +0000421 if (ACPV->isNonLazyPointer())
Evan Cheng9ed2f802008-11-10 01:08:07 +0000422 MCE.addRelocation(MachineRelocation::getIndirectSymbol(
Evan Chenge96a4902008-11-08 01:31:27 +0000423 MCE.getCurrentPCOffset(), ARM::reloc_arm_machine_cp_entry, GV,
424 (intptr_t)ACPV, false));
Jim Grosbach764ab522009-08-11 15:33:49 +0000425 else
Evan Chenge96a4902008-11-08 01:31:27 +0000426 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
Evan Cheng35b0bfd2008-11-13 19:22:28 +0000427 ACPV->isStub() || isa<Function>(GV), (intptr_t)ACPV);
Evan Cheng25e04782008-11-04 00:50:32 +0000428 } else {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000429 assert(!ACPV->isNonLazyPointer() && "Don't know how to deal this yet!");
430 emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
431 }
Evan Cheng83b5cf02008-11-05 23:22:34 +0000432 emitWordLE(0);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000433 } else {
434 Constant *CV = MCPE.Val.ConstVal;
435
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000436 DEBUG({
437 errs() << " ** Constant pool #" << CPI << " @ "
438 << (void*)MCE.getCurrentPCValue() << " ";
439 if (const Function *F = dyn_cast<Function>(CV))
440 errs() << F->getName();
441 else
442 errs() << *CV;
443 errs() << '\n';
444 });
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000445
446 if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Evan Cheng35b0bfd2008-11-13 19:22:28 +0000447 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV));
Evan Cheng83b5cf02008-11-05 23:22:34 +0000448 emitWordLE(0);
Evan Chengcb5201f2008-11-11 22:19:31 +0000449 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000450 uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
Evan Cheng83b5cf02008-11-05 23:22:34 +0000451 emitWordLE(Val);
Evan Chengcb5201f2008-11-11 22:19:31 +0000452 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000453 if (CFP->getType() == Type::getFloatTy(CFP->getContext()))
Evan Chengcb5201f2008-11-11 22:19:31 +0000454 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Owen Anderson1d0be152009-08-13 21:58:54 +0000455 else if (CFP->getType() == Type::getDoubleTy(CFP->getContext()))
Evan Chengcb5201f2008-11-11 22:19:31 +0000456 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
457 else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000458 llvm_unreachable("Unable to handle this constantpool entry!");
Evan Chengcb5201f2008-11-11 22:19:31 +0000459 }
460 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000461 llvm_unreachable("Unable to handle this constantpool entry!");
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000462 }
463 }
464}
465
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000466template<class CodeEmitter>
467void Emitter<CodeEmitter>::emitMOVi2piecesInstruction(const MachineInstr &MI) {
Evan Cheng90922132008-11-06 02:25:39 +0000468 const MachineOperand &MO0 = MI.getOperand(0);
469 const MachineOperand &MO1 = MI.getOperand(1);
Evan Chenge7cbe412009-07-08 21:03:57 +0000470 assert(MO1.isImm() && ARM_AM::getSOImmVal(MO1.isImm()) != -1 &&
471 "Not a valid so_imm value!");
Evan Cheng90922132008-11-06 02:25:39 +0000472 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
473 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
474
475 // Emit the 'mov' instruction.
476 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
477
478 // Set the conditional execution predicate.
Evan Cheng97f48c32008-11-06 22:15:19 +0000479 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng90922132008-11-06 02:25:39 +0000480
481 // Encode Rd.
482 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
483
484 // Encode so_imm.
485 // Set bit I(25) to identify this is the immediate form of <shifter_op>
486 Binary |= 1 << ARMII::I_BitShift;
Evan Chenge7cbe412009-07-08 21:03:57 +0000487 Binary |= getMachineSoImmOpValue(V1);
Evan Cheng90922132008-11-06 02:25:39 +0000488 emitWordLE(Binary);
489
490 // Now the 'orr' instruction.
491 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
492
493 // Set the conditional execution predicate.
Evan Cheng97f48c32008-11-06 22:15:19 +0000494 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng90922132008-11-06 02:25:39 +0000495
496 // Encode Rd.
497 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
498
499 // Encode Rn.
500 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
501
502 // Encode so_imm.
503 // Set bit I(25) to identify this is the immediate form of <shifter_op>
504 Binary |= 1 << ARMII::I_BitShift;
Evan Chenge7cbe412009-07-08 21:03:57 +0000505 Binary |= getMachineSoImmOpValue(V2);
Evan Cheng90922132008-11-06 02:25:39 +0000506 emitWordLE(Binary);
507}
508
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000509template<class CodeEmitter>
510void Emitter<CodeEmitter>::emitLEApcrelJTInstruction(const MachineInstr &MI) {
Evan Cheng4df60f52008-11-07 09:06:08 +0000511 // It's basically add r, pc, (LJTI - $+8)
Jim Grosbach764ab522009-08-11 15:33:49 +0000512
Evan Cheng4df60f52008-11-07 09:06:08 +0000513 const TargetInstrDesc &TID = MI.getDesc();
514
515 // Emit the 'add' instruction.
516 unsigned Binary = 0x4 << 21; // add: Insts{24-31} = 0b0100
517
518 // Set the conditional execution predicate
519 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
520
521 // Encode S bit if MI modifies CPSR.
522 Binary |= getAddrModeSBit(MI, TID);
523
524 // Encode Rd.
525 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
526
527 // Encode Rn which is PC.
528 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
529
530 // Encode the displacement.
Evan Cheng4df60f52008-11-07 09:06:08 +0000531 Binary |= 1 << ARMII::I_BitShift;
532 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
533
534 emitWordLE(Binary);
535}
536
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000537template<class CodeEmitter>
538void Emitter<CodeEmitter>::emitPseudoMoveInstruction(const MachineInstr &MI) {
Evan Chenga9562552008-11-14 20:09:11 +0000539 unsigned Opcode = MI.getDesc().Opcode;
540
541 // Part of binary is determined by TableGn.
542 unsigned Binary = getBinaryCodeForInstr(MI);
543
544 // Set the conditional execution predicate
545 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
546
547 // Encode S bit if MI modifies CPSR.
548 if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag)
549 Binary |= 1 << ARMII::S_BitShift;
550
551 // Encode register def if there is one.
552 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
553
554 // Encode the shift operation.
555 switch (Opcode) {
556 default: break;
557 case ARM::MOVrx:
558 // rrx
559 Binary |= 0x6 << 4;
560 break;
561 case ARM::MOVsrl_flag:
562 // lsr #1
563 Binary |= (0x2 << 4) | (1 << 7);
564 break;
565 case ARM::MOVsra_flag:
566 // asr #1
567 Binary |= (0x4 << 4) | (1 << 7);
568 break;
569 }
570
571 // Encode register Rm.
572 Binary |= getMachineOpValue(MI, 1);
573
574 emitWordLE(Binary);
575}
576
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000577template<class CodeEmitter>
578void Emitter<CodeEmitter>::addPCLabel(unsigned LabelID) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000579 DEBUG(errs() << " ** LPC" << LabelID << " @ "
580 << (void*)MCE.getCurrentPCValue() << '\n');
Evan Cheng83b5cf02008-11-05 23:22:34 +0000581 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
582}
583
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000584template<class CodeEmitter>
585void Emitter<CodeEmitter>::emitPseudoInstruction(const MachineInstr &MI) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000586 unsigned Opcode = MI.getDesc().Opcode;
587 switch (Opcode) {
588 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000589 llvm_unreachable("ARMCodeEmitter::emitPseudoInstruction");//FIXME:
Evan Chengffa6d962008-11-13 23:36:57 +0000590 case TargetInstrInfo::INLINEASM: {
Evan Chenge3066ab2008-11-19 23:21:33 +0000591 // We allow inline assembler nodes with empty bodies - they can
592 // implicitly define registers, which is ok for JIT.
593 if (MI.getOperand(0).getSymbolName()[0]) {
Torok Edwin29fd0562009-07-12 07:15:17 +0000594 llvm_report_error("JIT does not support inline asm!");
Evan Chenge3066ab2008-11-19 23:21:33 +0000595 }
Evan Chengffa6d962008-11-13 23:36:57 +0000596 break;
597 }
598 case TargetInstrInfo::DBG_LABEL:
599 case TargetInstrInfo::EH_LABEL:
600 MCE.emitLabel(MI.getOperand(0).getImm());
601 break;
602 case TargetInstrInfo::IMPLICIT_DEF:
Evan Chengffa6d962008-11-13 23:36:57 +0000603 case ARM::DWARF_LOC:
604 // Do nothing.
605 break;
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000606 case ARM::CONSTPOOL_ENTRY:
607 emitConstPoolInstruction(MI);
608 break;
609 case ARM::PICADD: {
Evan Cheng25e04782008-11-04 00:50:32 +0000610 // Remember of the address of the PC label for relocation later.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000611 addPCLabel(MI.getOperand(2).getImm());
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000612 // PICADD is just an add instruction that implicitly read pc.
Evan Cheng437c1732008-11-07 22:30:53 +0000613 emitDataProcessingInstruction(MI, 0, ARM::PC);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000614 break;
615 }
616 case ARM::PICLDR:
617 case ARM::PICLDRB:
618 case ARM::PICSTR:
619 case ARM::PICSTRB: {
620 // Remember of the address of the PC label for relocation later.
621 addPCLabel(MI.getOperand(2).getImm());
622 // These are just load / store instructions that implicitly read pc.
Evan Cheng4df60f52008-11-07 09:06:08 +0000623 emitLoadStoreInstruction(MI, 0, ARM::PC);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000624 break;
625 }
626 case ARM::PICLDRH:
627 case ARM::PICLDRSH:
628 case ARM::PICLDRSB:
629 case ARM::PICSTRH: {
630 // Remember of the address of the PC label for relocation later.
631 addPCLabel(MI.getOperand(2).getImm());
632 // These are just load / store instructions that implicitly read pc.
633 emitMiscLoadStoreInstruction(MI, ARM::PC);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000634 break;
635 }
Evan Cheng90922132008-11-06 02:25:39 +0000636 case ARM::MOVi2pieces:
637 // Two instructions to materialize a constant.
638 emitMOVi2piecesInstruction(MI);
639 break;
Evan Cheng4df60f52008-11-07 09:06:08 +0000640 case ARM::LEApcrelJT:
641 // Materialize jumptable address.
642 emitLEApcrelJTInstruction(MI);
643 break;
Evan Chenga9562552008-11-14 20:09:11 +0000644 case ARM::MOVrx:
645 case ARM::MOVsrl_flag:
646 case ARM::MOVsra_flag:
647 emitPseudoMoveInstruction(MI);
648 break;
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000649 }
650}
651
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000652template<class CodeEmitter>
653unsigned Emitter<CodeEmitter>::getMachineSoRegOpValue(
654 const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000655 const TargetInstrDesc &TID,
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000656 const MachineOperand &MO,
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000657 unsigned OpIdx) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000658 unsigned Binary = getMachineOpValue(MI, MO);
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000659
660 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
661 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
662 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
663
664 // Encode the shift opcode.
665 unsigned SBits = 0;
666 unsigned Rs = MO1.getReg();
667 if (Rs) {
668 // Set shift operand (bit[7:4]).
669 // LSL - 0001
670 // LSR - 0011
671 // ASR - 0101
672 // ROR - 0111
673 // RRX - 0110 and bit[11:8] clear.
674 switch (SOpc) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000675 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000676 case ARM_AM::lsl: SBits = 0x1; break;
677 case ARM_AM::lsr: SBits = 0x3; break;
678 case ARM_AM::asr: SBits = 0x5; break;
679 case ARM_AM::ror: SBits = 0x7; break;
680 case ARM_AM::rrx: SBits = 0x6; break;
681 }
682 } else {
683 // Set shift operand (bit[6:4]).
684 // LSL - 000
685 // LSR - 010
686 // ASR - 100
687 // ROR - 110
688 switch (SOpc) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000689 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000690 case ARM_AM::lsl: SBits = 0x0; break;
691 case ARM_AM::lsr: SBits = 0x2; break;
692 case ARM_AM::asr: SBits = 0x4; break;
693 case ARM_AM::ror: SBits = 0x6; break;
694 }
695 }
696 Binary |= SBits << 4;
697 if (SOpc == ARM_AM::rrx)
698 return Binary;
699
700 // Encode the shift operation Rs or shift_imm (except rrx).
701 if (Rs) {
702 // Encode Rs bit[11:8].
703 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
704 return Binary |
705 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
706 }
707
708 // Encode shift_imm bit[11:7].
709 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
710}
711
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000712template<class CodeEmitter>
713unsigned Emitter<CodeEmitter>::getMachineSoImmOpValue(unsigned SoImm) {
Evan Chenge7cbe412009-07-08 21:03:57 +0000714 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
715 assert(SoImmVal != -1 && "Not a valid so_imm value!");
716
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000717 // Encode rotate_imm.
Evan Chenge7cbe412009-07-08 21:03:57 +0000718 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
Evan Cheng97f48c32008-11-06 22:15:19 +0000719 << ARMII::SoRotImmShift;
720
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000721 // Encode immed_8.
Evan Chenge7cbe412009-07-08 21:03:57 +0000722 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000723 return Binary;
724}
725
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000726template<class CodeEmitter>
727unsigned Emitter<CodeEmitter>::getAddrModeSBit(const MachineInstr &MI,
728 const TargetInstrDesc &TID) const {
Evan Cheng97c573d2008-11-20 02:25:51 +0000729 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
Evan Cheng49a9f292008-09-12 22:45:55 +0000730 const MachineOperand &MO = MI.getOperand(i-1);
Dan Gohmand735b802008-10-03 15:45:36 +0000731 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
Evan Cheng49a9f292008-09-12 22:45:55 +0000732 return 1 << ARMII::S_BitShift;
733 }
734 return 0;
735}
736
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000737template<class CodeEmitter>
738void Emitter<CodeEmitter>::emitDataProcessingInstruction(
739 const MachineInstr &MI,
Evan Cheng437c1732008-11-07 22:30:53 +0000740 unsigned ImplicitRd,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000741 unsigned ImplicitRn) {
Evan Chengedda31c2008-11-05 18:35:52 +0000742 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +0000743
Evan Cheng36a0aeb2009-07-06 22:23:46 +0000744 if (TID.Opcode == ARM::BFC) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +0000745 llvm_report_error("ARMv6t2 JIT is not yet supported.");
Evan Cheng36a0aeb2009-07-06 22:23:46 +0000746 }
747
Evan Chengedda31c2008-11-05 18:35:52 +0000748 // Part of binary is determined by TableGn.
749 unsigned Binary = getBinaryCodeForInstr(MI);
750
Jim Grosbach33412622008-10-07 19:05:35 +0000751 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000752 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000753
Evan Cheng49a9f292008-09-12 22:45:55 +0000754 // Encode S bit if MI modifies CPSR.
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +0000755 Binary |= getAddrModeSBit(MI, TID);
Evan Cheng49a9f292008-09-12 22:45:55 +0000756
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000757 // Encode register def if there is one.
Evan Cheng49a9f292008-09-12 22:45:55 +0000758 unsigned NumDefs = TID.getNumDefs();
Evan Chenga964b7d2008-09-12 23:15:39 +0000759 unsigned OpIdx = 0;
Evan Cheng437c1732008-11-07 22:30:53 +0000760 if (NumDefs)
761 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
762 else if (ImplicitRd)
763 // Special handling for implicit use (e.g. PC).
764 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
765 << ARMII::RegRdShift);
Evan Cheng7602e112008-09-02 06:52:38 +0000766
Evan Chengd87293c2008-11-06 08:47:38 +0000767 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
768 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
769 ++OpIdx;
770
Jim Grosbachefd30ba2008-10-01 18:16:49 +0000771 // Encode first non-shifter register operand if there is one.
Evan Chengedda31c2008-11-05 18:35:52 +0000772 bool isUnary = TID.TSFlags & ARMII::UnaryDP;
773 if (!isUnary) {
Evan Cheng83b5cf02008-11-05 23:22:34 +0000774 if (ImplicitRn)
775 // Special handling for implicit use (e.g. PC).
776 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
Evan Chengedda31c2008-11-05 18:35:52 +0000777 << ARMII::RegRnShift);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000778 else {
779 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
780 ++OpIdx;
781 }
Evan Cheng7602e112008-09-02 06:52:38 +0000782 }
783
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000784 // Encode shifter operand.
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000785 const MachineOperand &MO = MI.getOperand(OpIdx);
Evan Chengedda31c2008-11-05 18:35:52 +0000786 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000787 // Encode SoReg.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000788 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
Evan Chengedda31c2008-11-05 18:35:52 +0000789 return;
790 }
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000791
Evan Chengedda31c2008-11-05 18:35:52 +0000792 if (MO.isReg()) {
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000793 // Encode register Rm.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000794 emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
Evan Chengedda31c2008-11-05 18:35:52 +0000795 return;
796 }
Evan Cheng7602e112008-09-02 06:52:38 +0000797
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000798 // Encode so_imm.
Evan Chenge7cbe412009-07-08 21:03:57 +0000799 Binary |= getMachineSoImmOpValue((unsigned)MO.getImm());
Evan Chengedda31c2008-11-05 18:35:52 +0000800
Evan Cheng83b5cf02008-11-05 23:22:34 +0000801 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000802}
803
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000804template<class CodeEmitter>
805void Emitter<CodeEmitter>::emitLoadStoreInstruction(
806 const MachineInstr &MI,
Evan Cheng4df60f52008-11-07 09:06:08 +0000807 unsigned ImplicitRd,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000808 unsigned ImplicitRn) {
Evan Cheng05c356e2008-11-08 01:44:13 +0000809 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng148cad82008-11-13 07:34:59 +0000810 unsigned Form = TID.TSFlags & ARMII::FormMask;
811 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +0000812
Evan Chengedda31c2008-11-05 18:35:52 +0000813 // Part of binary is determined by TableGn.
814 unsigned Binary = getBinaryCodeForInstr(MI);
815
Jim Grosbach33412622008-10-07 19:05:35 +0000816 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000817 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +0000818
Evan Cheng4df60f52008-11-07 09:06:08 +0000819 unsigned OpIdx = 0;
Evan Cheng148cad82008-11-13 07:34:59 +0000820
821 // Operand 0 of a pre- and post-indexed store is the address base
822 // writeback. Skip it.
823 bool Skipped = false;
824 if (IsPrePost && Form == ARMII::StFrm) {
825 ++OpIdx;
826 Skipped = true;
827 }
828
829 // Set first operand
Evan Cheng4df60f52008-11-07 09:06:08 +0000830 if (ImplicitRd)
831 // Special handling for implicit use (e.g. PC).
832 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
833 << ARMII::RegRdShift);
834 else
835 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000836
837 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +0000838 if (ImplicitRn)
839 // Special handling for implicit use (e.g. PC).
840 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
841 << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +0000842 else
843 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000844
Evan Cheng05c356e2008-11-08 01:44:13 +0000845 // If this is a two-address operand, skip it. e.g. LDR_PRE.
Evan Cheng148cad82008-11-13 07:34:59 +0000846 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +0000847 ++OpIdx;
848
Evan Cheng83b5cf02008-11-05 23:22:34 +0000849 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +0000850 unsigned AM2Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000851 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +0000852
Evan Chenge7de7e32008-09-13 01:44:01 +0000853 // Set bit U(23) according to sign of immed value (positive or negative).
Evan Cheng83b5cf02008-11-05 23:22:34 +0000854 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
Evan Chenge7de7e32008-09-13 01:44:01 +0000855 ARMII::U_BitShift);
Evan Cheng7602e112008-09-02 06:52:38 +0000856 if (!MO2.getReg()) { // is immediate
Evan Cheng83b5cf02008-11-05 23:22:34 +0000857 if (ARM_AM::getAM2Offset(AM2Opc))
Evan Cheng7602e112008-09-02 06:52:38 +0000858 // Set the value of offset_12 field
Evan Cheng83b5cf02008-11-05 23:22:34 +0000859 Binary |= ARM_AM::getAM2Offset(AM2Opc);
860 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +0000861 return;
Evan Cheng7602e112008-09-02 06:52:38 +0000862 }
863
864 // Set bit I(25), because this is not in immediate enconding.
865 Binary |= 1 << ARMII::I_BitShift;
866 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
867 // Set bit[3:0] to the corresponding Rm register
868 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
869
Evan Cheng70632912008-11-12 07:34:37 +0000870 // If this instr is in scaled register offset/index instruction, set
Evan Cheng7602e112008-09-02 06:52:38 +0000871 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000872 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
Evan Cheng70632912008-11-12 07:34:37 +0000873 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
874 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
Evan Cheng7602e112008-09-02 06:52:38 +0000875 }
876
Evan Cheng83b5cf02008-11-05 23:22:34 +0000877 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000878}
879
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000880template<class CodeEmitter>
881void Emitter<CodeEmitter>::emitMiscLoadStoreInstruction(const MachineInstr &MI,
882 unsigned ImplicitRn) {
Evan Cheng05c356e2008-11-08 01:44:13 +0000883 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng148cad82008-11-13 07:34:59 +0000884 unsigned Form = TID.TSFlags & ARMII::FormMask;
885 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +0000886
Evan Chengedda31c2008-11-05 18:35:52 +0000887 // Part of binary is determined by TableGn.
888 unsigned Binary = getBinaryCodeForInstr(MI);
889
Jim Grosbach33412622008-10-07 19:05:35 +0000890 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000891 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +0000892
Evan Cheng148cad82008-11-13 07:34:59 +0000893 unsigned OpIdx = 0;
894
895 // Operand 0 of a pre- and post-indexed store is the address base
896 // writeback. Skip it.
897 bool Skipped = false;
898 if (IsPrePost && Form == ARMII::StMiscFrm) {
899 ++OpIdx;
900 Skipped = true;
901 }
902
Evan Cheng7602e112008-09-02 06:52:38 +0000903 // Set first operand
Evan Cheng148cad82008-11-13 07:34:59 +0000904 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000905
Evan Cheng358dec52009-06-15 08:28:29 +0000906 // Skip LDRD and STRD's second operand.
907 if (TID.Opcode == ARM::LDRD || TID.Opcode == ARM::STRD)
908 ++OpIdx;
909
Evan Cheng7602e112008-09-02 06:52:38 +0000910 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +0000911 if (ImplicitRn)
912 // Special handling for implicit use (e.g. PC).
913 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
914 << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +0000915 else
916 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000917
Evan Cheng05c356e2008-11-08 01:44:13 +0000918 // If this is a two-address operand, skip it. e.g. LDRH_POST.
Evan Cheng148cad82008-11-13 07:34:59 +0000919 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +0000920 ++OpIdx;
921
Evan Cheng83b5cf02008-11-05 23:22:34 +0000922 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +0000923 unsigned AM3Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000924 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +0000925
Evan Chenge7de7e32008-09-13 01:44:01 +0000926 // Set bit U(23) according to sign of immed value (positive or negative)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000927 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
Evan Cheng7602e112008-09-02 06:52:38 +0000928 ARMII::U_BitShift);
929
930 // If this instr is in register offset/index encoding, set bit[3:0]
931 // to the corresponding Rm register.
932 if (MO2.getReg()) {
933 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
Evan Cheng83b5cf02008-11-05 23:22:34 +0000934 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +0000935 return;
Evan Cheng7602e112008-09-02 06:52:38 +0000936 }
937
Evan Chengd87293c2008-11-06 08:47:38 +0000938 // This instr is in immediate offset/index encoding, set bit 22 to 1.
Evan Cheng97f48c32008-11-06 22:15:19 +0000939 Binary |= 1 << ARMII::AM3_I_BitShift;
Evan Cheng83b5cf02008-11-05 23:22:34 +0000940 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
Evan Cheng7602e112008-09-02 06:52:38 +0000941 // Set operands
Evan Cheng70632912008-11-12 07:34:37 +0000942 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
943 Binary |= (ImmOffs & 0xF); // immedL
Evan Cheng7602e112008-09-02 06:52:38 +0000944 }
945
Evan Cheng83b5cf02008-11-05 23:22:34 +0000946 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000947}
948
Evan Chengcd8e66a2008-11-11 21:48:44 +0000949static unsigned getAddrModeUPBits(unsigned Mode) {
950 unsigned Binary = 0;
Evan Cheng7602e112008-09-02 06:52:38 +0000951
952 // Set addressing mode by modifying bits U(23) and P(24)
953 // IA - Increment after - bit U = 1 and bit P = 0
954 // IB - Increment before - bit U = 1 and bit P = 1
955 // DA - Decrement after - bit U = 0 and bit P = 0
956 // DB - Decrement before - bit U = 0 and bit P = 1
Evan Cheng7602e112008-09-02 06:52:38 +0000957 switch (Mode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000958 default: llvm_unreachable("Unknown addressing sub-mode!");
Evan Cheng7602e112008-09-02 06:52:38 +0000959 case ARM_AM::da: break;
Evan Cheng97f48c32008-11-06 22:15:19 +0000960 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
961 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
962 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
Evan Cheng7602e112008-09-02 06:52:38 +0000963 }
964
Evan Chengcd8e66a2008-11-11 21:48:44 +0000965 return Binary;
966}
967
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000968template<class CodeEmitter>
969void Emitter<CodeEmitter>::emitLoadStoreMultipleInstruction(
970 const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +0000971 // Part of binary is determined by TableGn.
972 unsigned Binary = getBinaryCodeForInstr(MI);
973
974 // Set the conditional execution predicate
975 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
976
977 // Set base address operand
978 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
979
980 // Set addressing mode by modifying bits U(23) and P(24)
981 const MachineOperand &MO = MI.getOperand(1);
982 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
983
Evan Cheng7602e112008-09-02 06:52:38 +0000984 // Set bit W(21)
985 if (ARM_AM::getAM4WBFlag(MO.getImm()))
Evan Cheng97f48c32008-11-06 22:15:19 +0000986 Binary |= 0x1 << ARMII::W_BitShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000987
988 // Set registers
989 for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
990 const MachineOperand &MO = MI.getOperand(i);
Evan Chengcd8e66a2008-11-11 21:48:44 +0000991 if (!MO.isReg() || MO.isImplicit())
992 break;
Evan Cheng7602e112008-09-02 06:52:38 +0000993 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
994 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
995 RegNum < 16);
996 Binary |= 0x1 << RegNum;
997 }
998
Evan Cheng83b5cf02008-11-05 23:22:34 +0000999 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +00001000}
1001
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001002template<class CodeEmitter>
1003void Emitter<CodeEmitter>::emitMulFrmInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001004 const TargetInstrDesc &TID = MI.getDesc();
1005
1006 // Part of binary is determined by TableGn.
1007 unsigned Binary = getBinaryCodeForInstr(MI);
1008
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001009 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001010 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001011
1012 // Encode S bit if MI modifies CPSR.
1013 Binary |= getAddrModeSBit(MI, TID);
1014
1015 // 32x32->64bit operations have two destination registers. The number
1016 // of register definitions will tell us if that's what we're dealing with.
Evan Cheng97f48c32008-11-06 22:15:19 +00001017 unsigned OpIdx = 0;
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001018 if (TID.getNumDefs() == 2)
1019 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
1020
1021 // Encode Rd
1022 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
1023
1024 // Encode Rm
1025 Binary |= getMachineOpValue(MI, OpIdx++);
1026
1027 // Encode Rs
1028 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
1029
Evan Chengfbc9d412008-11-06 01:21:28 +00001030 // Many multiple instructions (e.g. MLA) have three src operands. Encode
1031 // it as Rn (for multiply, that's in the same offset as RdLo.
Evan Cheng97f48c32008-11-06 22:15:19 +00001032 if (TID.getNumOperands() > OpIdx &&
1033 !TID.OpInfo[OpIdx].isPredicate() &&
1034 !TID.OpInfo[OpIdx].isOptionalDef())
1035 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
1036
1037 emitWordLE(Binary);
1038}
1039
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001040template<class CodeEmitter>
1041void Emitter<CodeEmitter>::emitExtendInstruction(const MachineInstr &MI) {
Evan Cheng97f48c32008-11-06 22:15:19 +00001042 const TargetInstrDesc &TID = MI.getDesc();
1043
1044 // Part of binary is determined by TableGn.
1045 unsigned Binary = getBinaryCodeForInstr(MI);
1046
1047 // Set the conditional execution predicate
1048 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1049
1050 unsigned OpIdx = 0;
1051
1052 // Encode Rd
1053 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1054
1055 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
1056 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1057 if (MO2.isReg()) {
1058 // Two register operand form.
1059 // Encode Rn.
1060 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
1061
1062 // Encode Rm.
1063 Binary |= getMachineOpValue(MI, MO2);
1064 ++OpIdx;
1065 } else {
1066 Binary |= getMachineOpValue(MI, MO1);
1067 }
1068
1069 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
1070 if (MI.getOperand(OpIdx).isImm() &&
1071 !TID.OpInfo[OpIdx].isPredicate() &&
1072 !TID.OpInfo[OpIdx].isOptionalDef())
1073 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
Evan Chengfbc9d412008-11-06 01:21:28 +00001074
Evan Cheng83b5cf02008-11-05 23:22:34 +00001075 emitWordLE(Binary);
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001076}
1077
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001078template<class CodeEmitter>
1079void Emitter<CodeEmitter>::emitMiscArithInstruction(const MachineInstr &MI) {
Evan Cheng8b59db32008-11-07 01:41:35 +00001080 const TargetInstrDesc &TID = MI.getDesc();
1081
1082 // Part of binary is determined by TableGn.
1083 unsigned Binary = getBinaryCodeForInstr(MI);
1084
1085 // Set the conditional execution predicate
1086 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1087
1088 unsigned OpIdx = 0;
1089
1090 // Encode Rd
1091 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1092
1093 const MachineOperand &MO = MI.getOperand(OpIdx++);
1094 if (OpIdx == TID.getNumOperands() ||
1095 TID.OpInfo[OpIdx].isPredicate() ||
1096 TID.OpInfo[OpIdx].isOptionalDef()) {
1097 // Encode Rm and it's done.
1098 Binary |= getMachineOpValue(MI, MO);
1099 emitWordLE(Binary);
1100 return;
1101 }
1102
1103 // Encode Rn.
1104 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1105
1106 // Encode Rm.
1107 Binary |= getMachineOpValue(MI, OpIdx++);
1108
1109 // Encode shift_imm.
1110 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
1111 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1112 Binary |= ShiftAmt << ARMII::ShiftShift;
Jim Grosbach764ab522009-08-11 15:33:49 +00001113
Evan Cheng8b59db32008-11-07 01:41:35 +00001114 emitWordLE(Binary);
1115}
1116
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001117template<class CodeEmitter>
1118void Emitter<CodeEmitter>::emitBranchInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001119 const TargetInstrDesc &TID = MI.getDesc();
1120
Torok Edwindac237e2009-07-08 20:53:28 +00001121 if (TID.Opcode == ARM::TPsoft) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001122 llvm_unreachable("ARM::TPsoft FIXME"); // FIXME
Torok Edwindac237e2009-07-08 20:53:28 +00001123 }
Evan Cheng12c3a532008-11-06 17:48:05 +00001124
Evan Cheng7602e112008-09-02 06:52:38 +00001125 // Part of binary is determined by TableGn.
1126 unsigned Binary = getBinaryCodeForInstr(MI);
1127
Evan Chengedda31c2008-11-05 18:35:52 +00001128 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001129 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001130
1131 // Set signed_immed_24 field
1132 Binary |= getMachineOpValue(MI, 0);
1133
Evan Cheng83b5cf02008-11-05 23:22:34 +00001134 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +00001135}
1136
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001137template<class CodeEmitter>
1138void Emitter<CodeEmitter>::emitInlineJumpTable(unsigned JTIndex) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001139 // Remember the base address of the inline jump table.
Evan Cheng5788d1a2008-12-10 02:32:19 +00001140 uintptr_t JTBase = MCE.getCurrentPCValue();
Evan Cheng437c1732008-11-07 22:30:53 +00001141 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
Chris Lattner893e1c92009-08-23 06:49:22 +00001142 DEBUG(errs() << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase
1143 << '\n');
Evan Cheng4df60f52008-11-07 09:06:08 +00001144
1145 // Now emit the jump table entries.
1146 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1147 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1148 if (IsPIC)
1149 // DestBB address - JT base.
Evan Cheng437c1732008-11-07 22:30:53 +00001150 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
Evan Cheng4df60f52008-11-07 09:06:08 +00001151 else
1152 // Absolute DestBB address.
1153 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1154 emitWordLE(0);
1155 }
1156}
1157
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001158template<class CodeEmitter>
1159void Emitter<CodeEmitter>::emitMiscBranchInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001160 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +00001161
Evan Cheng437c1732008-11-07 22:30:53 +00001162 // Handle jump tables.
Evan Cheng90daf4d2009-07-25 00:13:11 +00001163 if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
Evan Cheng437c1732008-11-07 22:30:53 +00001164 // First emit a ldr pc, [] instruction.
1165 emitDataProcessingInstruction(MI, ARM::PC);
1166
1167 // Then emit the inline jump table.
Evan Chengc9a41532009-07-08 00:05:05 +00001168 unsigned JTIndex =
Evan Cheng90daf4d2009-07-25 00:13:11 +00001169 (TID.Opcode == ARM::BR_JTr)
Evan Cheng437c1732008-11-07 22:30:53 +00001170 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1171 emitInlineJumpTable(JTIndex);
1172 return;
Evan Cheng90daf4d2009-07-25 00:13:11 +00001173 } else if (TID.Opcode == ARM::BR_JTm) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001174 // First emit a ldr pc, [] instruction.
1175 emitLoadStoreInstruction(MI, ARM::PC);
1176
1177 // Then emit the inline jump table.
Evan Cheng437c1732008-11-07 22:30:53 +00001178 emitInlineJumpTable(MI.getOperand(3).getIndex());
Evan Cheng4df60f52008-11-07 09:06:08 +00001179 return;
1180 }
1181
Evan Chengedda31c2008-11-05 18:35:52 +00001182 // Part of binary is determined by TableGn.
1183 unsigned Binary = getBinaryCodeForInstr(MI);
1184
1185 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001186 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001187
1188 if (TID.Opcode == ARM::BX_RET)
1189 // The return register is LR.
1190 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
Jim Grosbach764ab522009-08-11 15:33:49 +00001191 else
Evan Chengedda31c2008-11-05 18:35:52 +00001192 // otherwise, set the return register
1193 Binary |= getMachineOpValue(MI, 0);
1194
Evan Cheng83b5cf02008-11-05 23:22:34 +00001195 emitWordLE(Binary);
Evan Cheng148b6a42007-07-05 21:15:40 +00001196}
Evan Cheng7602e112008-09-02 06:52:38 +00001197
Evan Cheng80a11982008-11-12 06:41:41 +00001198static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) {
Evan Chengd06d48d2008-11-12 02:19:38 +00001199 unsigned RegD = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001200 unsigned Binary = 0;
Evan Chengd06d48d2008-11-12 02:19:38 +00001201 bool isSPVFP = false;
Evan Cheng8295d992009-07-22 05:55:18 +00001202 RegD = ARMRegisterInfo::getRegisterNumbering(RegD, &isSPVFP);
Evan Chengd06d48d2008-11-12 02:19:38 +00001203 if (!isSPVFP)
1204 Binary |= RegD << ARMII::RegRdShift;
1205 else {
1206 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1207 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1208 }
Evan Cheng80a11982008-11-12 06:41:41 +00001209 return Binary;
1210}
Evan Cheng78be83d2008-11-11 19:40:26 +00001211
Evan Cheng80a11982008-11-12 06:41:41 +00001212static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) {
Evan Chengd06d48d2008-11-12 02:19:38 +00001213 unsigned RegN = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001214 unsigned Binary = 0;
1215 bool isSPVFP = false;
Evan Cheng8295d992009-07-22 05:55:18 +00001216 RegN = ARMRegisterInfo::getRegisterNumbering(RegN, &isSPVFP);
Evan Chengd06d48d2008-11-12 02:19:38 +00001217 if (!isSPVFP)
1218 Binary |= RegN << ARMII::RegRnShift;
1219 else {
1220 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1221 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1222 }
Evan Cheng80a11982008-11-12 06:41:41 +00001223 return Binary;
1224}
Evan Chengd06d48d2008-11-12 02:19:38 +00001225
Evan Cheng80a11982008-11-12 06:41:41 +00001226static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) {
1227 unsigned RegM = MI.getOperand(OpIdx).getReg();
1228 unsigned Binary = 0;
1229 bool isSPVFP = false;
Evan Cheng8295d992009-07-22 05:55:18 +00001230 RegM = ARMRegisterInfo::getRegisterNumbering(RegM, &isSPVFP);
Evan Cheng80a11982008-11-12 06:41:41 +00001231 if (!isSPVFP)
1232 Binary |= RegM;
1233 else {
1234 Binary |= ((RegM & 0x1E) >> 1);
1235 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
Evan Cheng78be83d2008-11-11 19:40:26 +00001236 }
Evan Cheng80a11982008-11-12 06:41:41 +00001237 return Binary;
1238}
1239
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001240template<class CodeEmitter>
1241void Emitter<CodeEmitter>::emitVFPArithInstruction(const MachineInstr &MI) {
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001242 const TargetInstrDesc &TID = MI.getDesc();
1243
1244 // Part of binary is determined by TableGn.
1245 unsigned Binary = getBinaryCodeForInstr(MI);
1246
1247 // Set the conditional execution predicate
1248 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1249
1250 unsigned OpIdx = 0;
1251 assert((Binary & ARMII::D_BitShift) == 0 &&
1252 (Binary & ARMII::N_BitShift) == 0 &&
1253 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1254
1255 // Encode Dd / Sd.
1256 Binary |= encodeVFPRd(MI, OpIdx++);
1257
1258 // If this is a two-address operand, skip it, e.g. FMACD.
1259 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1260 ++OpIdx;
1261
1262 // Encode Dn / Sn.
1263 if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
Evan Cheng3f4924e2008-11-12 08:14:21 +00001264 Binary |= encodeVFPRn(MI, OpIdx++);
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001265
1266 if (OpIdx == TID.getNumOperands() ||
1267 TID.OpInfo[OpIdx].isPredicate() ||
1268 TID.OpInfo[OpIdx].isOptionalDef()) {
1269 // FCMPEZD etc. has only one operand.
1270 emitWordLE(Binary);
1271 return;
1272 }
1273
1274 // Encode Dm / Sm.
1275 Binary |= encodeVFPRm(MI, OpIdx);
Jim Grosbach764ab522009-08-11 15:33:49 +00001276
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001277 emitWordLE(Binary);
1278}
1279
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001280template<class CodeEmitter>
1281void Emitter<CodeEmitter>::emitVFPConversionInstruction(
1282 const MachineInstr &MI) {
Evan Cheng80a11982008-11-12 06:41:41 +00001283 const TargetInstrDesc &TID = MI.getDesc();
1284 unsigned Form = TID.TSFlags & ARMII::FormMask;
1285
1286 // Part of binary is determined by TableGn.
1287 unsigned Binary = getBinaryCodeForInstr(MI);
1288
1289 // Set the conditional execution predicate
1290 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1291
1292 switch (Form) {
1293 default: break;
1294 case ARMII::VFPConv1Frm:
1295 case ARMII::VFPConv2Frm:
1296 case ARMII::VFPConv3Frm:
1297 // Encode Dd / Sd.
1298 Binary |= encodeVFPRd(MI, 0);
1299 break;
1300 case ARMII::VFPConv4Frm:
1301 // Encode Dn / Sn.
1302 Binary |= encodeVFPRn(MI, 0);
1303 break;
1304 case ARMII::VFPConv5Frm:
1305 // Encode Dm / Sm.
1306 Binary |= encodeVFPRm(MI, 0);
1307 break;
1308 }
1309
1310 switch (Form) {
1311 default: break;
1312 case ARMII::VFPConv1Frm:
1313 // Encode Dm / Sm.
1314 Binary |= encodeVFPRm(MI, 1);
Evan Cheng67fd91f2008-11-13 07:46:59 +00001315 break;
Evan Cheng80a11982008-11-12 06:41:41 +00001316 case ARMII::VFPConv2Frm:
1317 case ARMII::VFPConv3Frm:
1318 // Encode Dn / Sn.
1319 Binary |= encodeVFPRn(MI, 1);
1320 break;
1321 case ARMII::VFPConv4Frm:
1322 case ARMII::VFPConv5Frm:
1323 // Encode Dd / Sd.
1324 Binary |= encodeVFPRd(MI, 1);
1325 break;
1326 }
1327
1328 if (Form == ARMII::VFPConv5Frm)
1329 // Encode Dn / Sn.
1330 Binary |= encodeVFPRn(MI, 2);
1331 else if (Form == ARMII::VFPConv3Frm)
1332 // Encode Dm / Sm.
1333 Binary |= encodeVFPRm(MI, 2);
Evan Cheng78be83d2008-11-11 19:40:26 +00001334
1335 emitWordLE(Binary);
1336}
1337
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001338template<class CodeEmitter>
1339void Emitter<CodeEmitter>::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001340 // Part of binary is determined by TableGn.
1341 unsigned Binary = getBinaryCodeForInstr(MI);
1342
1343 // Set the conditional execution predicate
1344 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1345
1346 unsigned OpIdx = 0;
1347
1348 // Encode Dd / Sd.
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001349 Binary |= encodeVFPRd(MI, OpIdx++);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001350
1351 // Encode address base.
1352 const MachineOperand &Base = MI.getOperand(OpIdx++);
1353 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1354
1355 // If there is a non-zero immediate offset, encode it.
1356 if (Base.isReg()) {
1357 const MachineOperand &Offset = MI.getOperand(OpIdx);
1358 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1359 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1360 Binary |= 1 << ARMII::U_BitShift;
Evan Cheng607f1b42008-11-12 08:21:12 +00001361 Binary |= ImmOffs;
Evan Chengcd8e66a2008-11-11 21:48:44 +00001362 emitWordLE(Binary);
1363 return;
1364 }
1365 }
1366
1367 // If immediate offset is omitted, default to +0.
1368 Binary |= 1 << ARMII::U_BitShift;
1369
1370 emitWordLE(Binary);
1371}
1372
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001373template<class CodeEmitter>
1374void Emitter<CodeEmitter>::emitVFPLoadStoreMultipleInstruction(
1375 const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001376 // Part of binary is determined by TableGn.
1377 unsigned Binary = getBinaryCodeForInstr(MI);
1378
1379 // Set the conditional execution predicate
1380 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1381
1382 // Set base address operand
1383 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
1384
1385 // Set addressing mode by modifying bits U(23) and P(24)
1386 const MachineOperand &MO = MI.getOperand(1);
1387 Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm()));
1388
1389 // Set bit W(21)
1390 if (ARM_AM::getAM5WBFlag(MO.getImm()))
1391 Binary |= 0x1 << ARMII::W_BitShift;
1392
1393 // First register is encoded in Dd.
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001394 Binary |= encodeVFPRd(MI, 4);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001395
1396 // Number of registers are encoded in offset field.
1397 unsigned NumRegs = 1;
1398 for (unsigned i = 5, e = MI.getNumOperands(); i != e; ++i) {
1399 const MachineOperand &MO = MI.getOperand(i);
1400 if (!MO.isReg() || MO.isImplicit())
1401 break;
1402 ++NumRegs;
1403 }
1404 Binary |= NumRegs * 2;
1405
1406 emitWordLE(Binary);
1407}
1408
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001409template<class CodeEmitter>
1410void Emitter<CodeEmitter>::emitMiscInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001411 // Part of binary is determined by TableGn.
1412 unsigned Binary = getBinaryCodeForInstr(MI);
1413
1414 // Set the conditional execution predicate
1415 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1416
1417 emitWordLE(Binary);
1418}
1419
Evan Cheng7602e112008-09-02 06:52:38 +00001420#include "ARMGenCodeEmitter.inc"