blob: 35502aa4cd93b608d260bc9e9a3da7135583ede7 [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 {
Evan Cheng42d5ee062008-09-13 01:15:21 +0000207 DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
Evan Cheng148b6a42007-07-05 21:15:40 +0000208 MCE.startFunction(MF);
209 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
210 MBB != E; ++MBB) {
211 MCE.StartMachineBasicBlock(MBB);
212 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
213 I != E; ++I)
214 emitInstruction(*I);
215 }
216 } while (MCE.finishFunction(MF));
217
218 return false;
219}
220
Evan Cheng83b5cf02008-11-05 23:22:34 +0000221/// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng7602e112008-09-02 06:52:38 +0000222///
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000223template<class CodeEmitter>
224unsigned Emitter<CodeEmitter>::getShiftOp(unsigned Imm) const {
Evan Cheng83b5cf02008-11-05 23:22:34 +0000225 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
Torok Edwinab7c09b2009-07-08 18:01:40 +0000226 default: LLVM_UNREACHABLE("Unknown shift opc!");
Evan Cheng7602e112008-09-02 06:52:38 +0000227 case ARM_AM::asr: return 2;
228 case ARM_AM::lsl: return 0;
229 case ARM_AM::lsr: return 1;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000230 case ARM_AM::ror:
Evan Cheng7602e112008-09-02 06:52:38 +0000231 case ARM_AM::rrx: return 3;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000232 }
Evan Cheng7602e112008-09-02 06:52:38 +0000233 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000234}
235
Evan Cheng7602e112008-09-02 06:52:38 +0000236/// getMachineOpValue - Return binary encoding of operand. If the machine
237/// operand requires relocation, record the relocation and return zero.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000238template<class CodeEmitter>
239unsigned Emitter<CodeEmitter>::getMachineOpValue(const MachineInstr &MI,
240 const MachineOperand &MO) {
Dan Gohmand735b802008-10-03 15:45:36 +0000241 if (MO.isReg())
Evan Cheng7602e112008-09-02 06:52:38 +0000242 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000243 else if (MO.isImm())
Evan Cheng7602e112008-09-02 06:52:38 +0000244 return static_cast<unsigned>(MO.getImm());
Dan Gohmand735b802008-10-03 15:45:36 +0000245 else if (MO.isGlobal())
Jim Grosbach016d34c2008-10-03 15:52:42 +0000246 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true);
Dan Gohmand735b802008-10-03 15:45:36 +0000247 else if (MO.isSymbol())
Evan Cheng10332512008-11-08 07:22:33 +0000248 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
Evan Cheng580c0df2008-11-12 01:02:24 +0000249 else if (MO.isCPI()) {
250 const TargetInstrDesc &TID = MI.getDesc();
251 // For VFP load, the immediate offset is multiplied by 4.
252 unsigned Reloc = ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
253 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
254 emitConstPoolAddress(MO.getIndex(), Reloc);
255 } else if (MO.isJTI())
Chris Lattner8aa797a2007-12-30 23:10:15 +0000256 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
Dan Gohmand735b802008-10-03 15:45:36 +0000257 else if (MO.isMBB())
Evan Cheng4df60f52008-11-07 09:06:08 +0000258 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
Evan Cheng2aa0e642008-09-13 01:55:59 +0000259 else {
Torok Edwindac237e2009-07-08 20:53:28 +0000260#ifndef NDEBUG
261 cerr << MO;
262#endif
263 llvm_unreachable();
Evan Cheng2aa0e642008-09-13 01:55:59 +0000264 }
Evan Cheng7602e112008-09-02 06:52:38 +0000265 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000266}
267
Evan Cheng057d0c32008-09-18 07:28:19 +0000268/// emitGlobalAddress - Emit the specified address to the code stream.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000269///
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000270template<class CodeEmitter>
271void Emitter<CodeEmitter>::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
272 bool NeedStub, intptr_t ACPV) {
273 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
274 GV, ACPV, NeedStub));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000275}
276
277/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
278/// be emitted to the current location in the function, and allow it to be PC
279/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000280template<class CodeEmitter>
281void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES,
282 unsigned Reloc) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000283 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
284 Reloc, ES));
285}
286
287/// emitConstPoolAddress - Arrange for the address of an constant pool
288/// to be emitted to the current location in the function, and allow it to be PC
289/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000290template<class CodeEmitter>
291void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI,
292 unsigned Reloc) {
Evan Cheng0f282432008-10-29 23:55:43 +0000293 // Tell JIT emitter we'll resolve the address.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000294 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000295 Reloc, CPI, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000296}
297
298/// emitJumpTableAddress - Arrange for the address of a jump table to
299/// be emitted to the current location in the function, and allow it to be PC
300/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000301template<class CodeEmitter>
302void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTIndex,
303 unsigned Reloc) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000304 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000305 Reloc, JTIndex, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000306}
307
Raul Herbster9c1a3822007-08-30 23:29:26 +0000308/// emitMachineBasicBlock - Emit the specified address basic block.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000309template<class CodeEmitter>
310void Emitter<CodeEmitter>::emitMachineBasicBlock(MachineBasicBlock *BB,
311 unsigned Reloc, intptr_t JTBase) {
Raul Herbster9c1a3822007-08-30 23:29:26 +0000312 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000313 Reloc, BB, JTBase));
Raul Herbster9c1a3822007-08-30 23:29:26 +0000314}
Evan Cheng0ff94f72007-08-07 01:37:15 +0000315
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000316template<class CodeEmitter>
317void Emitter<CodeEmitter>::emitWordLE(unsigned Binary) {
Evan Cheng4df60f52008-11-07 09:06:08 +0000318#ifndef NDEBUG
319 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
320 << Binary << std::dec << "\n";
321#endif
Evan Cheng83b5cf02008-11-05 23:22:34 +0000322 MCE.emitWordLE(Binary);
323}
324
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000325template<class CodeEmitter>
326void Emitter<CodeEmitter>::emitDWordLE(uint64_t Binary) {
Evan Chengcb5201f2008-11-11 22:19:31 +0000327#ifndef NDEBUG
328 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
329 << (unsigned)Binary << std::dec << "\n";
330 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
331 << (unsigned)(Binary >> 32) << std::dec << "\n";
332#endif
333 MCE.emitDWordLE(Binary);
334}
335
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000336template<class CodeEmitter>
337void Emitter<CodeEmitter>::emitInstruction(const MachineInstr &MI) {
Evan Cheng25e04782008-11-04 00:50:32 +0000338 DOUT << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI;
Evan Cheng42d5ee062008-09-13 01:15:21 +0000339
Evan Cheng148b6a42007-07-05 21:15:40 +0000340 NumEmitted++; // Keep track of the # of mi's emitted
Evan Chengedda31c2008-11-05 18:35:52 +0000341 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
Evan Chengffa6d962008-11-13 23:36:57 +0000342 default: {
Torok Edwinab7c09b2009-07-08 18:01:40 +0000343 LLVM_UNREACHABLE("Unhandled instruction encoding format!");
Evan Chengedda31c2008-11-05 18:35:52 +0000344 break;
Evan Chengffa6d962008-11-13 23:36:57 +0000345 }
Evan Chengedda31c2008-11-05 18:35:52 +0000346 case ARMII::Pseudo:
Evan Cheng057d0c32008-09-18 07:28:19 +0000347 emitPseudoInstruction(MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000348 break;
349 case ARMII::DPFrm:
350 case ARMII::DPSoRegFrm:
351 emitDataProcessingInstruction(MI);
352 break;
Evan Cheng148cad82008-11-13 07:34:59 +0000353 case ARMII::LdFrm:
354 case ARMII::StFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000355 emitLoadStoreInstruction(MI);
356 break;
Evan Cheng148cad82008-11-13 07:34:59 +0000357 case ARMII::LdMiscFrm:
358 case ARMII::StMiscFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000359 emitMiscLoadStoreInstruction(MI);
360 break;
Evan Cheng3c4a4ff2008-11-12 07:18:38 +0000361 case ARMII::LdStMulFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000362 emitLoadStoreMultipleInstruction(MI);
363 break;
Evan Chengfbc9d412008-11-06 01:21:28 +0000364 case ARMII::MulFrm:
365 emitMulFrmInstruction(MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000366 break;
Evan Cheng97f48c32008-11-06 22:15:19 +0000367 case ARMII::ExtFrm:
368 emitExtendInstruction(MI);
369 break;
Evan Cheng8b59db32008-11-07 01:41:35 +0000370 case ARMII::ArithMiscFrm:
371 emitMiscArithInstruction(MI);
372 break;
Evan Cheng12c3a532008-11-06 17:48:05 +0000373 case ARMII::BrFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000374 emitBranchInstruction(MI);
375 break;
Evan Cheng12c3a532008-11-06 17:48:05 +0000376 case ARMII::BrMiscFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000377 emitMiscBranchInstruction(MI);
378 break;
Evan Cheng96581d32008-11-11 02:11:05 +0000379 // VFP instructions.
380 case ARMII::VFPUnaryFrm:
381 case ARMII::VFPBinaryFrm:
382 emitVFPArithInstruction(MI);
383 break;
Evan Cheng78be83d2008-11-11 19:40:26 +0000384 case ARMII::VFPConv1Frm:
385 case ARMII::VFPConv2Frm:
Evan Cheng0a0ab132008-11-11 22:46:12 +0000386 case ARMII::VFPConv3Frm:
Evan Cheng80a11982008-11-12 06:41:41 +0000387 case ARMII::VFPConv4Frm:
388 case ARMII::VFPConv5Frm:
Evan Cheng78be83d2008-11-11 19:40:26 +0000389 emitVFPConversionInstruction(MI);
390 break;
Evan Chengcd8e66a2008-11-11 21:48:44 +0000391 case ARMII::VFPLdStFrm:
392 emitVFPLoadStoreInstruction(MI);
393 break;
394 case ARMII::VFPLdStMulFrm:
395 emitVFPLoadStoreMultipleInstruction(MI);
396 break;
397 case ARMII::VFPMiscFrm:
398 emitMiscInstruction(MI);
399 break;
Evan Chengedda31c2008-11-05 18:35:52 +0000400 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000401}
402
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000403template<class CodeEmitter>
404void Emitter<CodeEmitter>::emitConstPoolInstruction(const MachineInstr &MI) {
Evan Cheng437c1732008-11-07 22:30:53 +0000405 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
406 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
Evan Cheng938b9d82008-10-31 19:55:13 +0000407 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000408
409 // Remember the CONSTPOOL_ENTRY address for later relocation.
410 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
411
412 // Emit constpool island entry. In most cases, the actual values will be
413 // resolved and relocated after code emission.
414 if (MCPE.isMachineConstantPoolEntry()) {
415 ARMConstantPoolValue *ACPV =
416 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
417
Evan Cheng12c3a532008-11-06 17:48:05 +0000418 DOUT << " ** ARM constant pool #" << CPI << " @ "
Evan Cheng437c1732008-11-07 22:30:53 +0000419 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n';
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000420
421 GlobalValue *GV = ACPV->getGV();
422 if (GV) {
423 assert(!ACPV->isStub() && "Don't know how to deal this yet!");
Evan Chenge96a4902008-11-08 01:31:27 +0000424 if (ACPV->isNonLazyPointer())
Evan Cheng9ed2f802008-11-10 01:08:07 +0000425 MCE.addRelocation(MachineRelocation::getIndirectSymbol(
Evan Chenge96a4902008-11-08 01:31:27 +0000426 MCE.getCurrentPCOffset(), ARM::reloc_arm_machine_cp_entry, GV,
427 (intptr_t)ACPV, false));
428 else
429 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
Evan Cheng35b0bfd2008-11-13 19:22:28 +0000430 ACPV->isStub() || isa<Function>(GV), (intptr_t)ACPV);
Evan Cheng25e04782008-11-04 00:50:32 +0000431 } else {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000432 assert(!ACPV->isNonLazyPointer() && "Don't know how to deal this yet!");
433 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
Evan Cheng35b0bfd2008-11-13 19:22:28 +0000439#ifndef NDEBUG
Evan Cheng12c3a532008-11-06 17:48:05 +0000440 DOUT << " ** Constant pool #" << CPI << " @ "
Evan Cheng35b0bfd2008-11-13 19:22:28 +0000441 << (void*)MCE.getCurrentPCValue() << " ";
442 if (const Function *F = dyn_cast<Function>(CV))
443 DOUT << F->getName();
444 else
445 DOUT << *CV;
446 DOUT << '\n';
447#endif
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000448
449 if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Evan Cheng35b0bfd2008-11-13 19:22:28 +0000450 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV));
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)) {
456 if (CFP->getType() == Type::FloatTy)
457 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
458 else if (CFP->getType() == Type::DoubleTy)
459 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
460 else {
Torok Edwinab7c09b2009-07-08 18:01:40 +0000461 LLVM_UNREACHABLE("Unable to handle this constantpool entry!");
Evan Chengcb5201f2008-11-11 22:19:31 +0000462 }
463 } else {
Torok Edwinab7c09b2009-07-08 18:01:40 +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)
515
516 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) {
Evan Cheng12c3a532008-11-06 17:48:05 +0000582 DOUT << " ** LPC" << LabelID << " @ "
Evan Cheng83b5cf02008-11-05 23:22:34 +0000583 << (void*)MCE.getCurrentPCValue() << '\n';
584 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 Edwindac237e2009-07-08 20:53:28 +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 Edwinab7c09b2009-07-08 18:01:40 +0000597 llvm_report_error("JIT does not support inline asm!\n");
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:
606 case TargetInstrInfo::DECLARE:
607 case ARM::DWARF_LOC:
608 // Do nothing.
609 break;
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000610 case ARM::CONSTPOOL_ENTRY:
611 emitConstPoolInstruction(MI);
612 break;
613 case ARM::PICADD: {
Evan Cheng25e04782008-11-04 00:50:32 +0000614 // Remember of the address of the PC label for relocation later.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000615 addPCLabel(MI.getOperand(2).getImm());
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000616 // PICADD is just an add instruction that implicitly read pc.
Evan Cheng437c1732008-11-07 22:30:53 +0000617 emitDataProcessingInstruction(MI, 0, ARM::PC);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000618 break;
619 }
620 case ARM::PICLDR:
621 case ARM::PICLDRB:
622 case ARM::PICSTR:
623 case ARM::PICSTRB: {
624 // Remember of the address of the PC label for relocation later.
625 addPCLabel(MI.getOperand(2).getImm());
626 // These are just load / store instructions that implicitly read pc.
Evan Cheng4df60f52008-11-07 09:06:08 +0000627 emitLoadStoreInstruction(MI, 0, ARM::PC);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000628 break;
629 }
630 case ARM::PICLDRH:
631 case ARM::PICLDRSH:
632 case ARM::PICLDRSB:
633 case ARM::PICSTRH: {
634 // Remember of the address of the PC label for relocation later.
635 addPCLabel(MI.getOperand(2).getImm());
636 // These are just load / store instructions that implicitly read pc.
637 emitMiscLoadStoreInstruction(MI, ARM::PC);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000638 break;
639 }
Evan Cheng90922132008-11-06 02:25:39 +0000640 case ARM::MOVi2pieces:
641 // Two instructions to materialize a constant.
642 emitMOVi2piecesInstruction(MI);
643 break;
Evan Cheng4df60f52008-11-07 09:06:08 +0000644 case ARM::LEApcrelJT:
645 // Materialize jumptable address.
646 emitLEApcrelJTInstruction(MI);
647 break;
Evan Chenga9562552008-11-14 20:09:11 +0000648 case ARM::MOVrx:
649 case ARM::MOVsrl_flag:
650 case ARM::MOVsra_flag:
651 emitPseudoMoveInstruction(MI);
652 break;
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000653 }
654}
655
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000656template<class CodeEmitter>
657unsigned Emitter<CodeEmitter>::getMachineSoRegOpValue(
658 const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000659 const TargetInstrDesc &TID,
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000660 const MachineOperand &MO,
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000661 unsigned OpIdx) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000662 unsigned Binary = getMachineOpValue(MI, MO);
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000663
664 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
665 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
666 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
667
668 // Encode the shift opcode.
669 unsigned SBits = 0;
670 unsigned Rs = MO1.getReg();
671 if (Rs) {
672 // Set shift operand (bit[7:4]).
673 // LSL - 0001
674 // LSR - 0011
675 // ASR - 0101
676 // ROR - 0111
677 // RRX - 0110 and bit[11:8] clear.
678 switch (SOpc) {
Torok Edwinab7c09b2009-07-08 18:01:40 +0000679 default: LLVM_UNREACHABLE("Unknown shift opc!");
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000680 case ARM_AM::lsl: SBits = 0x1; break;
681 case ARM_AM::lsr: SBits = 0x3; break;
682 case ARM_AM::asr: SBits = 0x5; break;
683 case ARM_AM::ror: SBits = 0x7; break;
684 case ARM_AM::rrx: SBits = 0x6; break;
685 }
686 } else {
687 // Set shift operand (bit[6:4]).
688 // LSL - 000
689 // LSR - 010
690 // ASR - 100
691 // ROR - 110
692 switch (SOpc) {
Torok Edwinab7c09b2009-07-08 18:01:40 +0000693 default: LLVM_UNREACHABLE("Unknown shift opc!");
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000694 case ARM_AM::lsl: SBits = 0x0; break;
695 case ARM_AM::lsr: SBits = 0x2; break;
696 case ARM_AM::asr: SBits = 0x4; break;
697 case ARM_AM::ror: SBits = 0x6; break;
698 }
699 }
700 Binary |= SBits << 4;
701 if (SOpc == ARM_AM::rrx)
702 return Binary;
703
704 // Encode the shift operation Rs or shift_imm (except rrx).
705 if (Rs) {
706 // Encode Rs bit[11:8].
707 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
708 return Binary |
709 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
710 }
711
712 // Encode shift_imm bit[11:7].
713 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
714}
715
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000716template<class CodeEmitter>
717unsigned Emitter<CodeEmitter>::getMachineSoImmOpValue(unsigned SoImm) {
Evan Chenge7cbe412009-07-08 21:03:57 +0000718 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
719 assert(SoImmVal != -1 && "Not a valid so_imm value!");
720
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000721 // Encode rotate_imm.
Evan Chenge7cbe412009-07-08 21:03:57 +0000722 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
Evan Cheng97f48c32008-11-06 22:15:19 +0000723 << ARMII::SoRotImmShift;
724
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000725 // Encode immed_8.
Evan Chenge7cbe412009-07-08 21:03:57 +0000726 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000727 return Binary;
728}
729
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000730template<class CodeEmitter>
731unsigned Emitter<CodeEmitter>::getAddrModeSBit(const MachineInstr &MI,
732 const TargetInstrDesc &TID) const {
Evan Cheng97c573d2008-11-20 02:25:51 +0000733 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
Evan Cheng49a9f292008-09-12 22:45:55 +0000734 const MachineOperand &MO = MI.getOperand(i-1);
Dan Gohmand735b802008-10-03 15:45:36 +0000735 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
Evan Cheng49a9f292008-09-12 22:45:55 +0000736 return 1 << ARMII::S_BitShift;
737 }
738 return 0;
739}
740
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000741template<class CodeEmitter>
742void Emitter<CodeEmitter>::emitDataProcessingInstruction(
743 const MachineInstr &MI,
Evan Cheng437c1732008-11-07 22:30:53 +0000744 unsigned ImplicitRd,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000745 unsigned ImplicitRn) {
Evan Chengedda31c2008-11-05 18:35:52 +0000746 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +0000747
Evan Cheng36a0aeb2009-07-06 22:23:46 +0000748 if (TID.Opcode == ARM::BFC) {
Torok Edwinab7c09b2009-07-08 18:01:40 +0000749 llvm_report_error("ERROR: ARMv6t2 JIT is not yet supported.");
Evan Cheng36a0aeb2009-07-06 22:23:46 +0000750 }
751
Evan Chengedda31c2008-11-05 18:35:52 +0000752 // Part of binary is determined by TableGn.
753 unsigned Binary = getBinaryCodeForInstr(MI);
754
Jim Grosbach33412622008-10-07 19:05:35 +0000755 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000756 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000757
Evan Cheng49a9f292008-09-12 22:45:55 +0000758 // Encode S bit if MI modifies CPSR.
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +0000759 Binary |= getAddrModeSBit(MI, TID);
Evan Cheng49a9f292008-09-12 22:45:55 +0000760
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000761 // Encode register def if there is one.
Evan Cheng49a9f292008-09-12 22:45:55 +0000762 unsigned NumDefs = TID.getNumDefs();
Evan Chenga964b7d2008-09-12 23:15:39 +0000763 unsigned OpIdx = 0;
Evan Cheng437c1732008-11-07 22:30:53 +0000764 if (NumDefs)
765 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
766 else if (ImplicitRd)
767 // Special handling for implicit use (e.g. PC).
768 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
769 << ARMII::RegRdShift);
Evan Cheng7602e112008-09-02 06:52:38 +0000770
Evan Chengd87293c2008-11-06 08:47:38 +0000771 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
772 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
773 ++OpIdx;
774
Jim Grosbachefd30ba2008-10-01 18:16:49 +0000775 // Encode first non-shifter register operand if there is one.
Evan Chengedda31c2008-11-05 18:35:52 +0000776 bool isUnary = TID.TSFlags & ARMII::UnaryDP;
777 if (!isUnary) {
Evan Cheng83b5cf02008-11-05 23:22:34 +0000778 if (ImplicitRn)
779 // Special handling for implicit use (e.g. PC).
780 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
Evan Chengedda31c2008-11-05 18:35:52 +0000781 << ARMII::RegRnShift);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000782 else {
783 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
784 ++OpIdx;
785 }
Evan Cheng7602e112008-09-02 06:52:38 +0000786 }
787
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000788 // Encode shifter operand.
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000789 const MachineOperand &MO = MI.getOperand(OpIdx);
Evan Chengedda31c2008-11-05 18:35:52 +0000790 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000791 // Encode SoReg.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000792 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
Evan Chengedda31c2008-11-05 18:35:52 +0000793 return;
794 }
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000795
Evan Chengedda31c2008-11-05 18:35:52 +0000796 if (MO.isReg()) {
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000797 // Encode register Rm.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000798 emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
Evan Chengedda31c2008-11-05 18:35:52 +0000799 return;
800 }
Evan Cheng7602e112008-09-02 06:52:38 +0000801
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000802 // Encode so_imm.
Evan Chenge7cbe412009-07-08 21:03:57 +0000803 Binary |= getMachineSoImmOpValue((unsigned)MO.getImm());
Evan Chengedda31c2008-11-05 18:35:52 +0000804
Evan Cheng83b5cf02008-11-05 23:22:34 +0000805 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000806}
807
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000808template<class CodeEmitter>
809void Emitter<CodeEmitter>::emitLoadStoreInstruction(
810 const MachineInstr &MI,
Evan Cheng4df60f52008-11-07 09:06:08 +0000811 unsigned ImplicitRd,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000812 unsigned ImplicitRn) {
Evan Cheng05c356e2008-11-08 01:44:13 +0000813 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng148cad82008-11-13 07:34:59 +0000814 unsigned Form = TID.TSFlags & ARMII::FormMask;
815 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +0000816
Evan Chengedda31c2008-11-05 18:35:52 +0000817 // Part of binary is determined by TableGn.
818 unsigned Binary = getBinaryCodeForInstr(MI);
819
Jim Grosbach33412622008-10-07 19:05:35 +0000820 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000821 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +0000822
Evan Cheng4df60f52008-11-07 09:06:08 +0000823 unsigned OpIdx = 0;
Evan Cheng148cad82008-11-13 07:34:59 +0000824
825 // Operand 0 of a pre- and post-indexed store is the address base
826 // writeback. Skip it.
827 bool Skipped = false;
828 if (IsPrePost && Form == ARMII::StFrm) {
829 ++OpIdx;
830 Skipped = true;
831 }
832
833 // Set first operand
Evan Cheng4df60f52008-11-07 09:06:08 +0000834 if (ImplicitRd)
835 // Special handling for implicit use (e.g. PC).
836 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
837 << ARMII::RegRdShift);
838 else
839 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000840
841 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +0000842 if (ImplicitRn)
843 // Special handling for implicit use (e.g. PC).
844 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
845 << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +0000846 else
847 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000848
Evan Cheng05c356e2008-11-08 01:44:13 +0000849 // If this is a two-address operand, skip it. e.g. LDR_PRE.
Evan Cheng148cad82008-11-13 07:34:59 +0000850 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +0000851 ++OpIdx;
852
Evan Cheng83b5cf02008-11-05 23:22:34 +0000853 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +0000854 unsigned AM2Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000855 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +0000856
Evan Chenge7de7e32008-09-13 01:44:01 +0000857 // Set bit U(23) according to sign of immed value (positive or negative).
Evan Cheng83b5cf02008-11-05 23:22:34 +0000858 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
Evan Chenge7de7e32008-09-13 01:44:01 +0000859 ARMII::U_BitShift);
Evan Cheng7602e112008-09-02 06:52:38 +0000860 if (!MO2.getReg()) { // is immediate
Evan Cheng83b5cf02008-11-05 23:22:34 +0000861 if (ARM_AM::getAM2Offset(AM2Opc))
Evan Cheng7602e112008-09-02 06:52:38 +0000862 // Set the value of offset_12 field
Evan Cheng83b5cf02008-11-05 23:22:34 +0000863 Binary |= ARM_AM::getAM2Offset(AM2Opc);
864 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +0000865 return;
Evan Cheng7602e112008-09-02 06:52:38 +0000866 }
867
868 // Set bit I(25), because this is not in immediate enconding.
869 Binary |= 1 << ARMII::I_BitShift;
870 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
871 // Set bit[3:0] to the corresponding Rm register
872 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
873
Evan Cheng70632912008-11-12 07:34:37 +0000874 // If this instr is in scaled register offset/index instruction, set
Evan Cheng7602e112008-09-02 06:52:38 +0000875 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000876 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
Evan Cheng70632912008-11-12 07:34:37 +0000877 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
878 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
Evan Cheng7602e112008-09-02 06:52:38 +0000879 }
880
Evan Cheng83b5cf02008-11-05 23:22:34 +0000881 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000882}
883
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000884template<class CodeEmitter>
885void Emitter<CodeEmitter>::emitMiscLoadStoreInstruction(const MachineInstr &MI,
886 unsigned ImplicitRn) {
Evan Cheng05c356e2008-11-08 01:44:13 +0000887 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng148cad82008-11-13 07:34:59 +0000888 unsigned Form = TID.TSFlags & ARMII::FormMask;
889 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +0000890
Evan Chengedda31c2008-11-05 18:35:52 +0000891 // Part of binary is determined by TableGn.
892 unsigned Binary = getBinaryCodeForInstr(MI);
893
Jim Grosbach33412622008-10-07 19:05:35 +0000894 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000895 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +0000896
Evan Cheng148cad82008-11-13 07:34:59 +0000897 unsigned OpIdx = 0;
898
899 // Operand 0 of a pre- and post-indexed store is the address base
900 // writeback. Skip it.
901 bool Skipped = false;
902 if (IsPrePost && Form == ARMII::StMiscFrm) {
903 ++OpIdx;
904 Skipped = true;
905 }
906
Evan Cheng7602e112008-09-02 06:52:38 +0000907 // Set first operand
Evan Cheng148cad82008-11-13 07:34:59 +0000908 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000909
Evan Cheng358dec52009-06-15 08:28:29 +0000910 // Skip LDRD and STRD's second operand.
911 if (TID.Opcode == ARM::LDRD || TID.Opcode == ARM::STRD)
912 ++OpIdx;
913
Evan Cheng7602e112008-09-02 06:52:38 +0000914 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +0000915 if (ImplicitRn)
916 // Special handling for implicit use (e.g. PC).
917 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
918 << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +0000919 else
920 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000921
Evan Cheng05c356e2008-11-08 01:44:13 +0000922 // If this is a two-address operand, skip it. e.g. LDRH_POST.
Evan Cheng148cad82008-11-13 07:34:59 +0000923 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +0000924 ++OpIdx;
925
Evan Cheng83b5cf02008-11-05 23:22:34 +0000926 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +0000927 unsigned AM3Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000928 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +0000929
Evan Chenge7de7e32008-09-13 01:44:01 +0000930 // Set bit U(23) according to sign of immed value (positive or negative)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000931 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
Evan Cheng7602e112008-09-02 06:52:38 +0000932 ARMII::U_BitShift);
933
934 // If this instr is in register offset/index encoding, set bit[3:0]
935 // to the corresponding Rm register.
936 if (MO2.getReg()) {
937 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
Evan Cheng83b5cf02008-11-05 23:22:34 +0000938 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +0000939 return;
Evan Cheng7602e112008-09-02 06:52:38 +0000940 }
941
Evan Chengd87293c2008-11-06 08:47:38 +0000942 // This instr is in immediate offset/index encoding, set bit 22 to 1.
Evan Cheng97f48c32008-11-06 22:15:19 +0000943 Binary |= 1 << ARMII::AM3_I_BitShift;
Evan Cheng83b5cf02008-11-05 23:22:34 +0000944 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
Evan Cheng7602e112008-09-02 06:52:38 +0000945 // Set operands
Evan Cheng70632912008-11-12 07:34:37 +0000946 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
947 Binary |= (ImmOffs & 0xF); // immedL
Evan Cheng7602e112008-09-02 06:52:38 +0000948 }
949
Evan Cheng83b5cf02008-11-05 23:22:34 +0000950 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000951}
952
Evan Chengcd8e66a2008-11-11 21:48:44 +0000953static unsigned getAddrModeUPBits(unsigned Mode) {
954 unsigned Binary = 0;
Evan Cheng7602e112008-09-02 06:52:38 +0000955
956 // Set addressing mode by modifying bits U(23) and P(24)
957 // IA - Increment after - bit U = 1 and bit P = 0
958 // IB - Increment before - bit U = 1 and bit P = 1
959 // DA - Decrement after - bit U = 0 and bit P = 0
960 // DB - Decrement before - bit U = 0 and bit P = 1
Evan Cheng7602e112008-09-02 06:52:38 +0000961 switch (Mode) {
Torok Edwinab7c09b2009-07-08 18:01:40 +0000962 default: LLVM_UNREACHABLE("Unknown addressing sub-mode!");
Evan Cheng7602e112008-09-02 06:52:38 +0000963 case ARM_AM::da: break;
Evan Cheng97f48c32008-11-06 22:15:19 +0000964 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
965 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
966 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
Evan Cheng7602e112008-09-02 06:52:38 +0000967 }
968
Evan Chengcd8e66a2008-11-11 21:48:44 +0000969 return Binary;
970}
971
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000972template<class CodeEmitter>
973void Emitter<CodeEmitter>::emitLoadStoreMultipleInstruction(
974 const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +0000975 // Part of binary is determined by TableGn.
976 unsigned Binary = getBinaryCodeForInstr(MI);
977
978 // Set the conditional execution predicate
979 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
980
981 // Set base address operand
982 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
983
984 // Set addressing mode by modifying bits U(23) and P(24)
985 const MachineOperand &MO = MI.getOperand(1);
986 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
987
Evan Cheng7602e112008-09-02 06:52:38 +0000988 // Set bit W(21)
989 if (ARM_AM::getAM4WBFlag(MO.getImm()))
Evan Cheng97f48c32008-11-06 22:15:19 +0000990 Binary |= 0x1 << ARMII::W_BitShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000991
992 // Set registers
993 for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
994 const MachineOperand &MO = MI.getOperand(i);
Evan Chengcd8e66a2008-11-11 21:48:44 +0000995 if (!MO.isReg() || MO.isImplicit())
996 break;
Evan Cheng7602e112008-09-02 06:52:38 +0000997 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
998 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
999 RegNum < 16);
1000 Binary |= 0x1 << RegNum;
1001 }
1002
Evan Cheng83b5cf02008-11-05 23:22:34 +00001003 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +00001004}
1005
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001006template<class CodeEmitter>
1007void Emitter<CodeEmitter>::emitMulFrmInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001008 const TargetInstrDesc &TID = MI.getDesc();
1009
1010 // Part of binary is determined by TableGn.
1011 unsigned Binary = getBinaryCodeForInstr(MI);
1012
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001013 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001014 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001015
1016 // Encode S bit if MI modifies CPSR.
1017 Binary |= getAddrModeSBit(MI, TID);
1018
1019 // 32x32->64bit operations have two destination registers. The number
1020 // of register definitions will tell us if that's what we're dealing with.
Evan Cheng97f48c32008-11-06 22:15:19 +00001021 unsigned OpIdx = 0;
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001022 if (TID.getNumDefs() == 2)
1023 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
1024
1025 // Encode Rd
1026 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
1027
1028 // Encode Rm
1029 Binary |= getMachineOpValue(MI, OpIdx++);
1030
1031 // Encode Rs
1032 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
1033
Evan Chengfbc9d412008-11-06 01:21:28 +00001034 // Many multiple instructions (e.g. MLA) have three src operands. Encode
1035 // it as Rn (for multiply, that's in the same offset as RdLo.
Evan Cheng97f48c32008-11-06 22:15:19 +00001036 if (TID.getNumOperands() > OpIdx &&
1037 !TID.OpInfo[OpIdx].isPredicate() &&
1038 !TID.OpInfo[OpIdx].isOptionalDef())
1039 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
1040
1041 emitWordLE(Binary);
1042}
1043
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001044template<class CodeEmitter>
1045void Emitter<CodeEmitter>::emitExtendInstruction(const MachineInstr &MI) {
Evan Cheng97f48c32008-11-06 22:15:19 +00001046 const TargetInstrDesc &TID = MI.getDesc();
1047
1048 // Part of binary is determined by TableGn.
1049 unsigned Binary = getBinaryCodeForInstr(MI);
1050
1051 // Set the conditional execution predicate
1052 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1053
1054 unsigned OpIdx = 0;
1055
1056 // Encode Rd
1057 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1058
1059 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
1060 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1061 if (MO2.isReg()) {
1062 // Two register operand form.
1063 // Encode Rn.
1064 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
1065
1066 // Encode Rm.
1067 Binary |= getMachineOpValue(MI, MO2);
1068 ++OpIdx;
1069 } else {
1070 Binary |= getMachineOpValue(MI, MO1);
1071 }
1072
1073 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
1074 if (MI.getOperand(OpIdx).isImm() &&
1075 !TID.OpInfo[OpIdx].isPredicate() &&
1076 !TID.OpInfo[OpIdx].isOptionalDef())
1077 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
Evan Chengfbc9d412008-11-06 01:21:28 +00001078
Evan Cheng83b5cf02008-11-05 23:22:34 +00001079 emitWordLE(Binary);
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001080}
1081
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001082template<class CodeEmitter>
1083void Emitter<CodeEmitter>::emitMiscArithInstruction(const MachineInstr &MI) {
Evan Cheng8b59db32008-11-07 01:41:35 +00001084 const TargetInstrDesc &TID = MI.getDesc();
1085
1086 // Part of binary is determined by TableGn.
1087 unsigned Binary = getBinaryCodeForInstr(MI);
1088
1089 // Set the conditional execution predicate
1090 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1091
1092 unsigned OpIdx = 0;
1093
1094 // Encode Rd
1095 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1096
1097 const MachineOperand &MO = MI.getOperand(OpIdx++);
1098 if (OpIdx == TID.getNumOperands() ||
1099 TID.OpInfo[OpIdx].isPredicate() ||
1100 TID.OpInfo[OpIdx].isOptionalDef()) {
1101 // Encode Rm and it's done.
1102 Binary |= getMachineOpValue(MI, MO);
1103 emitWordLE(Binary);
1104 return;
1105 }
1106
1107 // Encode Rn.
1108 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1109
1110 // Encode Rm.
1111 Binary |= getMachineOpValue(MI, OpIdx++);
1112
1113 // Encode shift_imm.
1114 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
1115 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1116 Binary |= ShiftAmt << ARMII::ShiftShift;
1117
1118 emitWordLE(Binary);
1119}
1120
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001121template<class CodeEmitter>
1122void Emitter<CodeEmitter>::emitBranchInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001123 const TargetInstrDesc &TID = MI.getDesc();
1124
Torok Edwindac237e2009-07-08 20:53:28 +00001125 if (TID.Opcode == ARM::TPsoft) {
1126 LLVM_UNREACHABLE("ARM::TPsoft FIXME"); // FIXME
1127 }
Evan Cheng12c3a532008-11-06 17:48:05 +00001128
Evan Cheng7602e112008-09-02 06:52:38 +00001129 // Part of binary is determined by TableGn.
1130 unsigned Binary = getBinaryCodeForInstr(MI);
1131
Evan Chengedda31c2008-11-05 18:35:52 +00001132 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001133 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001134
1135 // Set signed_immed_24 field
1136 Binary |= getMachineOpValue(MI, 0);
1137
Evan Cheng83b5cf02008-11-05 23:22:34 +00001138 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +00001139}
1140
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001141template<class CodeEmitter>
1142void Emitter<CodeEmitter>::emitInlineJumpTable(unsigned JTIndex) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001143 // Remember the base address of the inline jump table.
Evan Cheng5788d1a2008-12-10 02:32:19 +00001144 uintptr_t JTBase = MCE.getCurrentPCValue();
Evan Cheng437c1732008-11-07 22:30:53 +00001145 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
1146 DOUT << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase << '\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.
David Goodwinc9a59b52009-06-30 19:50:22 +00001166 if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd ||
1167 TID.Opcode == ARM::t2BR_JTr || TID.Opcode == ARM::t2BR_JTadd) {
Evan Cheng437c1732008-11-07 22:30:53 +00001168 // First emit a ldr pc, [] instruction.
1169 emitDataProcessingInstruction(MI, ARM::PC);
1170
1171 // Then emit the inline jump table.
Evan Chengc9a41532009-07-08 00:05:05 +00001172 unsigned JTIndex =
1173 (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::t2BR_JTr)
Evan Cheng437c1732008-11-07 22:30:53 +00001174 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1175 emitInlineJumpTable(JTIndex);
1176 return;
David Goodwinc9a59b52009-06-30 19:50:22 +00001177 } else if (TID.Opcode == ARM::BR_JTm || TID.Opcode == ARM::t2BR_JTm) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001178 // First emit a ldr pc, [] instruction.
1179 emitLoadStoreInstruction(MI, ARM::PC);
1180
1181 // Then emit the inline jump table.
Evan Cheng437c1732008-11-07 22:30:53 +00001182 emitInlineJumpTable(MI.getOperand(3).getIndex());
Evan Cheng4df60f52008-11-07 09:06:08 +00001183 return;
1184 }
1185
Evan Chengedda31c2008-11-05 18:35:52 +00001186 // Part of binary is determined by TableGn.
1187 unsigned Binary = getBinaryCodeForInstr(MI);
1188
1189 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001190 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001191
1192 if (TID.Opcode == ARM::BX_RET)
1193 // The return register is LR.
1194 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
1195 else
1196 // otherwise, set the return register
1197 Binary |= getMachineOpValue(MI, 0);
1198
Evan Cheng83b5cf02008-11-05 23:22:34 +00001199 emitWordLE(Binary);
Evan Cheng148b6a42007-07-05 21:15:40 +00001200}
Evan Cheng7602e112008-09-02 06:52:38 +00001201
Evan Cheng80a11982008-11-12 06:41:41 +00001202static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) {
Evan Chengd06d48d2008-11-12 02:19:38 +00001203 unsigned RegD = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001204 unsigned Binary = 0;
Evan Chengd06d48d2008-11-12 02:19:38 +00001205 bool isSPVFP = false;
1206 RegD = ARMRegisterInfo::getRegisterNumbering(RegD, isSPVFP);
1207 if (!isSPVFP)
1208 Binary |= RegD << ARMII::RegRdShift;
1209 else {
1210 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1211 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1212 }
Evan Cheng80a11982008-11-12 06:41:41 +00001213 return Binary;
1214}
Evan Cheng78be83d2008-11-11 19:40:26 +00001215
Evan Cheng80a11982008-11-12 06:41:41 +00001216static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) {
Evan Chengd06d48d2008-11-12 02:19:38 +00001217 unsigned RegN = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001218 unsigned Binary = 0;
1219 bool isSPVFP = false;
Evan Chengd06d48d2008-11-12 02:19:38 +00001220 RegN = ARMRegisterInfo::getRegisterNumbering(RegN, isSPVFP);
1221 if (!isSPVFP)
1222 Binary |= RegN << ARMII::RegRnShift;
1223 else {
1224 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1225 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1226 }
Evan Cheng80a11982008-11-12 06:41:41 +00001227 return Binary;
1228}
Evan Chengd06d48d2008-11-12 02:19:38 +00001229
Evan Cheng80a11982008-11-12 06:41:41 +00001230static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) {
1231 unsigned RegM = MI.getOperand(OpIdx).getReg();
1232 unsigned Binary = 0;
1233 bool isSPVFP = false;
1234 RegM = ARMRegisterInfo::getRegisterNumbering(RegM, isSPVFP);
1235 if (!isSPVFP)
1236 Binary |= RegM;
1237 else {
1238 Binary |= ((RegM & 0x1E) >> 1);
1239 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
Evan Cheng78be83d2008-11-11 19:40:26 +00001240 }
Evan Cheng80a11982008-11-12 06:41:41 +00001241 return Binary;
1242}
1243
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001244template<class CodeEmitter>
1245void Emitter<CodeEmitter>::emitVFPArithInstruction(const MachineInstr &MI) {
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001246 const TargetInstrDesc &TID = MI.getDesc();
1247
1248 // Part of binary is determined by TableGn.
1249 unsigned Binary = getBinaryCodeForInstr(MI);
1250
1251 // Set the conditional execution predicate
1252 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1253
1254 unsigned OpIdx = 0;
1255 assert((Binary & ARMII::D_BitShift) == 0 &&
1256 (Binary & ARMII::N_BitShift) == 0 &&
1257 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1258
1259 // Encode Dd / Sd.
1260 Binary |= encodeVFPRd(MI, OpIdx++);
1261
1262 // If this is a two-address operand, skip it, e.g. FMACD.
1263 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1264 ++OpIdx;
1265
1266 // Encode Dn / Sn.
1267 if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
Evan Cheng3f4924e2008-11-12 08:14:21 +00001268 Binary |= encodeVFPRn(MI, OpIdx++);
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001269
1270 if (OpIdx == TID.getNumOperands() ||
1271 TID.OpInfo[OpIdx].isPredicate() ||
1272 TID.OpInfo[OpIdx].isOptionalDef()) {
1273 // FCMPEZD etc. has only one operand.
1274 emitWordLE(Binary);
1275 return;
1276 }
1277
1278 // Encode Dm / Sm.
1279 Binary |= encodeVFPRm(MI, OpIdx);
1280
1281 emitWordLE(Binary);
1282}
1283
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001284template<class CodeEmitter>
1285void Emitter<CodeEmitter>::emitVFPConversionInstruction(
1286 const MachineInstr &MI) {
Evan Cheng80a11982008-11-12 06:41:41 +00001287 const TargetInstrDesc &TID = MI.getDesc();
1288 unsigned Form = TID.TSFlags & ARMII::FormMask;
1289
1290 // Part of binary is determined by TableGn.
1291 unsigned Binary = getBinaryCodeForInstr(MI);
1292
1293 // Set the conditional execution predicate
1294 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1295
1296 switch (Form) {
1297 default: break;
1298 case ARMII::VFPConv1Frm:
1299 case ARMII::VFPConv2Frm:
1300 case ARMII::VFPConv3Frm:
1301 // Encode Dd / Sd.
1302 Binary |= encodeVFPRd(MI, 0);
1303 break;
1304 case ARMII::VFPConv4Frm:
1305 // Encode Dn / Sn.
1306 Binary |= encodeVFPRn(MI, 0);
1307 break;
1308 case ARMII::VFPConv5Frm:
1309 // Encode Dm / Sm.
1310 Binary |= encodeVFPRm(MI, 0);
1311 break;
1312 }
1313
1314 switch (Form) {
1315 default: break;
1316 case ARMII::VFPConv1Frm:
1317 // Encode Dm / Sm.
1318 Binary |= encodeVFPRm(MI, 1);
Evan Cheng67fd91f2008-11-13 07:46:59 +00001319 break;
Evan Cheng80a11982008-11-12 06:41:41 +00001320 case ARMII::VFPConv2Frm:
1321 case ARMII::VFPConv3Frm:
1322 // Encode Dn / Sn.
1323 Binary |= encodeVFPRn(MI, 1);
1324 break;
1325 case ARMII::VFPConv4Frm:
1326 case ARMII::VFPConv5Frm:
1327 // Encode Dd / Sd.
1328 Binary |= encodeVFPRd(MI, 1);
1329 break;
1330 }
1331
1332 if (Form == ARMII::VFPConv5Frm)
1333 // Encode Dn / Sn.
1334 Binary |= encodeVFPRn(MI, 2);
1335 else if (Form == ARMII::VFPConv3Frm)
1336 // Encode Dm / Sm.
1337 Binary |= encodeVFPRm(MI, 2);
Evan Cheng78be83d2008-11-11 19:40:26 +00001338
1339 emitWordLE(Binary);
1340}
1341
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001342template<class CodeEmitter>
1343void Emitter<CodeEmitter>::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001344 // Part of binary is determined by TableGn.
1345 unsigned Binary = getBinaryCodeForInstr(MI);
1346
1347 // Set the conditional execution predicate
1348 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1349
1350 unsigned OpIdx = 0;
1351
1352 // Encode Dd / Sd.
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001353 Binary |= encodeVFPRd(MI, OpIdx++);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001354
1355 // Encode address base.
1356 const MachineOperand &Base = MI.getOperand(OpIdx++);
1357 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1358
1359 // If there is a non-zero immediate offset, encode it.
1360 if (Base.isReg()) {
1361 const MachineOperand &Offset = MI.getOperand(OpIdx);
1362 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1363 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1364 Binary |= 1 << ARMII::U_BitShift;
Evan Cheng607f1b42008-11-12 08:21:12 +00001365 Binary |= ImmOffs;
Evan Chengcd8e66a2008-11-11 21:48:44 +00001366 emitWordLE(Binary);
1367 return;
1368 }
1369 }
1370
1371 // If immediate offset is omitted, default to +0.
1372 Binary |= 1 << ARMII::U_BitShift;
1373
1374 emitWordLE(Binary);
1375}
1376
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001377template<class CodeEmitter>
1378void Emitter<CodeEmitter>::emitVFPLoadStoreMultipleInstruction(
1379 const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001380 // Part of binary is determined by TableGn.
1381 unsigned Binary = getBinaryCodeForInstr(MI);
1382
1383 // Set the conditional execution predicate
1384 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1385
1386 // Set base address operand
1387 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
1388
1389 // Set addressing mode by modifying bits U(23) and P(24)
1390 const MachineOperand &MO = MI.getOperand(1);
1391 Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm()));
1392
1393 // Set bit W(21)
1394 if (ARM_AM::getAM5WBFlag(MO.getImm()))
1395 Binary |= 0x1 << ARMII::W_BitShift;
1396
1397 // First register is encoded in Dd.
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001398 Binary |= encodeVFPRd(MI, 4);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001399
1400 // Number of registers are encoded in offset field.
1401 unsigned NumRegs = 1;
1402 for (unsigned i = 5, e = MI.getNumOperands(); i != e; ++i) {
1403 const MachineOperand &MO = MI.getOperand(i);
1404 if (!MO.isReg() || MO.isImplicit())
1405 break;
1406 ++NumRegs;
1407 }
1408 Binary |= NumRegs * 2;
1409
1410 emitWordLE(Binary);
1411}
1412
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001413template<class CodeEmitter>
1414void Emitter<CodeEmitter>::emitMiscInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001415 // Part of binary is determined by TableGn.
1416 unsigned Binary = getBinaryCodeForInstr(MI);
1417
1418 // Set the conditional execution predicate
1419 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1420
1421 emitWordLE(Binary);
1422}
1423
Evan Cheng7602e112008-09-02 06:52:38 +00001424#include "ARMGenCodeEmitter.inc"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001425