blob: 70b5d788b48ddd3752149886a85f0d3c1377c59e [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"
Evan Cheng057d0c32008-09-18 07:28:19 +000029#include "llvm/CodeGen/MachineConstantPool.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000030#include "llvm/CodeGen/MachineFunctionPass.h"
31#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng4df60f52008-11-07 09:06:08 +000032#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000033#include "llvm/CodeGen/Passes.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000034#include "llvm/ADT/Statistic.h"
35#include "llvm/Support/Compiler.h"
Evan Cheng42d5ee062008-09-13 01:15:21 +000036#include "llvm/Support/Debug.h"
Evan Cheng4df60f52008-11-07 09:06:08 +000037#ifndef NDEBUG
38#include <iomanip>
39#endif
Evan Cheng148b6a42007-07-05 21:15:40 +000040using namespace llvm;
41
42STATISTIC(NumEmitted, "Number of machine instructions emitted");
43
44namespace {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000045
46 class ARMCodeEmitter {
47 public:
48
49 /// getBinaryCodeForInstr - This function, generated by the
50 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
51 /// machine instructions.
52
53 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
54 };
55
56 template< class machineCodeEmitter>
57 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
58 public ARMCodeEmitter
59 {
Evan Cheng057d0c32008-09-18 07:28:19 +000060 ARMJITInfo *JTI;
61 const ARMInstrInfo *II;
62 const TargetData *TD;
63 TargetMachine &TM;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000064 machineCodeEmitter &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 Lopesa3f99f92009-05-30 20:51:52 +000071 explicit Emitter(TargetMachine &tm, machineCodeEmitter &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 Lopesa3f99f92009-05-30 20:51:52 +000075 Emitter(TargetMachine &tm, machineCodeEmitter &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 Lopesa3f99f92009-05-30 20:51:52 +0000173 template <class machineCodeEmitter>
174 char Emitter<machineCodeEmitter>::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
180namespace llvm {
181
182FunctionPass *createARMCodeEmitterPass(
183 ARMTargetMachine &TM, MachineCodeEmitter &MCE)
184{
185 return new Emitter<MachineCodeEmitter>(TM, MCE);
186}
187FunctionPass *createARMJITCodeEmitterPass(
188 ARMTargetMachine &TM, JITCodeEmitter &JCE)
189{
190 return new Emitter<JITCodeEmitter>(TM, JCE);
Evan Cheng148b6a42007-07-05 21:15:40 +0000191}
192
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000193} // end namespace llvm
194
195template< class machineCodeEmitter>
196bool Emitter< machineCodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000197 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
198 MF.getTarget().getRelocationModel() != Reloc::Static) &&
199 "JIT relocation model must be set to static or default!");
200 II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo();
201 TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData();
Evan Cheng057d0c32008-09-18 07:28:19 +0000202 JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo();
Evan Cheng938b9d82008-10-31 19:55:13 +0000203 MCPEs = &MF.getConstantPool()->getConstants();
Evan Cheng4df60f52008-11-07 09:06:08 +0000204 MJTEs = &MF.getJumpTableInfo()->getJumpTables();
205 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Evan Cheng3cc82232008-11-08 07:38:22 +0000206 JTI->Initialize(MF, IsPIC);
Evan Cheng148b6a42007-07-05 21:15:40 +0000207
208 do {
Evan Cheng42d5ee062008-09-13 01:15:21 +0000209 DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
Evan Cheng148b6a42007-07-05 21:15:40 +0000210 MCE.startFunction(MF);
211 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
212 MBB != E; ++MBB) {
213 MCE.StartMachineBasicBlock(MBB);
214 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
215 I != E; ++I)
216 emitInstruction(*I);
217 }
218 } while (MCE.finishFunction(MF));
219
220 return false;
221}
222
Evan Cheng83b5cf02008-11-05 23:22:34 +0000223/// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng7602e112008-09-02 06:52:38 +0000224///
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000225template< class machineCodeEmitter>
226unsigned Emitter< machineCodeEmitter>::getShiftOp(unsigned Imm) const {
Evan Cheng83b5cf02008-11-05 23:22:34 +0000227 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000228 default: assert(0 && "Unknown shift opc!");
Evan Cheng7602e112008-09-02 06:52:38 +0000229 case ARM_AM::asr: return 2;
230 case ARM_AM::lsl: return 0;
231 case ARM_AM::lsr: return 1;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000232 case ARM_AM::ror:
Evan Cheng7602e112008-09-02 06:52:38 +0000233 case ARM_AM::rrx: return 3;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000234 }
Evan Cheng7602e112008-09-02 06:52:38 +0000235 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000236}
237
Evan Cheng7602e112008-09-02 06:52:38 +0000238/// getMachineOpValue - Return binary encoding of operand. If the machine
239/// operand requires relocation, record the relocation and return zero.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000240template< class machineCodeEmitter>
241unsigned Emitter< machineCodeEmitter>::getMachineOpValue(const MachineInstr &MI,
Evan Cheng7602e112008-09-02 06:52:38 +0000242 const MachineOperand &MO) {
Dan Gohmand735b802008-10-03 15:45:36 +0000243 if (MO.isReg())
Evan Cheng7602e112008-09-02 06:52:38 +0000244 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000245 else if (MO.isImm())
Evan Cheng7602e112008-09-02 06:52:38 +0000246 return static_cast<unsigned>(MO.getImm());
Dan Gohmand735b802008-10-03 15:45:36 +0000247 else if (MO.isGlobal())
Jim Grosbach016d34c2008-10-03 15:52:42 +0000248 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true);
Dan Gohmand735b802008-10-03 15:45:36 +0000249 else if (MO.isSymbol())
Evan Cheng10332512008-11-08 07:22:33 +0000250 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
Evan Cheng580c0df2008-11-12 01:02:24 +0000251 else if (MO.isCPI()) {
252 const TargetInstrDesc &TID = MI.getDesc();
253 // For VFP load, the immediate offset is multiplied by 4.
254 unsigned Reloc = ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
255 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
256 emitConstPoolAddress(MO.getIndex(), Reloc);
257 } else if (MO.isJTI())
Chris Lattner8aa797a2007-12-30 23:10:15 +0000258 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
Dan Gohmand735b802008-10-03 15:45:36 +0000259 else if (MO.isMBB())
Evan Cheng4df60f52008-11-07 09:06:08 +0000260 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
Evan Cheng2aa0e642008-09-13 01:55:59 +0000261 else {
262 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
263 abort();
264 }
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 Lopesa3f99f92009-05-30 20:51:52 +0000270template< class machineCodeEmitter>
271void Emitter< machineCodeEmitter>::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
Evan Cheng413a89f2008-11-07 22:57:53 +0000272 bool NeedStub, intptr_t ACPV) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000273 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
Evan Cheng413a89f2008-11-07 22:57:53 +0000274 Reloc, 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 Lopesa3f99f92009-05-30 20:51:52 +0000280template< class machineCodeEmitter>
281void Emitter< machineCodeEmitter>::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000282 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
283 Reloc, ES));
284}
285
286/// emitConstPoolAddress - Arrange for the address of an constant pool
287/// to be emitted to the current location in the function, and allow it to be PC
288/// relative.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000289template< class machineCodeEmitter>
290void Emitter< machineCodeEmitter>::emitConstPoolAddress(unsigned CPI, unsigned Reloc) {
Evan Cheng0f282432008-10-29 23:55:43 +0000291 // Tell JIT emitter we'll resolve the address.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000292 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000293 Reloc, CPI, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000294}
295
296/// emitJumpTableAddress - Arrange for the address of a jump table to
297/// be emitted to the current location in the function, and allow it to be PC
298/// relative.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000299template< class machineCodeEmitter>
300void Emitter< machineCodeEmitter>::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000301 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000302 Reloc, JTIndex, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000303}
304
Raul Herbster9c1a3822007-08-30 23:29:26 +0000305/// emitMachineBasicBlock - Emit the specified address basic block.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000306template< class machineCodeEmitter>
307void Emitter< machineCodeEmitter>::emitMachineBasicBlock(MachineBasicBlock *BB,
Evan Cheng437c1732008-11-07 22:30:53 +0000308 unsigned Reloc, intptr_t JTBase) {
Raul Herbster9c1a3822007-08-30 23:29:26 +0000309 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000310 Reloc, BB, JTBase));
Raul Herbster9c1a3822007-08-30 23:29:26 +0000311}
Evan Cheng0ff94f72007-08-07 01:37:15 +0000312
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000313template< class machineCodeEmitter>
314void Emitter< machineCodeEmitter>::emitWordLE(unsigned Binary) {
Evan Cheng4df60f52008-11-07 09:06:08 +0000315#ifndef NDEBUG
316 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
317 << Binary << std::dec << "\n";
318#endif
Evan Cheng83b5cf02008-11-05 23:22:34 +0000319 MCE.emitWordLE(Binary);
320}
321
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000322template< class machineCodeEmitter>
323void Emitter< machineCodeEmitter>::emitDWordLE(uint64_t Binary) {
Evan Chengcb5201f2008-11-11 22:19:31 +0000324#ifndef NDEBUG
325 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
326 << (unsigned)Binary << std::dec << "\n";
327 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
328 << (unsigned)(Binary >> 32) << std::dec << "\n";
329#endif
330 MCE.emitDWordLE(Binary);
331}
332
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000333template< class machineCodeEmitter>
334void Emitter< machineCodeEmitter>::emitInstruction(const MachineInstr &MI) {
Evan Cheng25e04782008-11-04 00:50:32 +0000335 DOUT << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI;
Evan Cheng42d5ee062008-09-13 01:15:21 +0000336
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: {
Evan Chengedda31c2008-11-05 18:35:52 +0000340 assert(0 && "Unhandled instruction encoding format!");
341 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 Lopesa3f99f92009-05-30 20:51:52 +0000400template< class machineCodeEmitter>
401void Emitter< machineCodeEmitter>::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];
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000405
406 // 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
Evan Cheng12c3a532008-11-06 17:48:05 +0000415 DOUT << " ** ARM constant pool #" << CPI << " @ "
Evan Cheng437c1732008-11-07 22:30:53 +0000416 << (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));
425 else
426 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
Evan Cheng35b0bfd2008-11-13 19:22:28 +0000436#ifndef NDEBUG
Evan Cheng12c3a532008-11-06 17:48:05 +0000437 DOUT << " ** Constant pool #" << CPI << " @ "
Evan Cheng35b0bfd2008-11-13 19:22:28 +0000438 << (void*)MCE.getCurrentPCValue() << " ";
439 if (const Function *F = dyn_cast<Function>(CV))
440 DOUT << F->getName();
441 else
442 DOUT << *CV;
443 DOUT << '\n';
444#endif
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)) {
453 if (CFP->getType() == Type::FloatTy)
454 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
455 else if (CFP->getType() == Type::DoubleTy)
456 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
457 else {
458 assert(0 && "Unable to handle this constantpool entry!");
459 abort();
460 }
461 } else {
462 assert(0 && "Unable to handle this constantpool entry!");
463 abort();
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000464 }
465 }
466}
467
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000468template< class machineCodeEmitter>
469void Emitter< machineCodeEmitter>::emitMOVi2piecesInstruction(const MachineInstr &MI) {
Evan Cheng90922132008-11-06 02:25:39 +0000470 const MachineOperand &MO0 = MI.getOperand(0);
471 const MachineOperand &MO1 = MI.getOperand(1);
472 assert(MO1.isImm() && "Not a valid so_imm value!");
473 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
474 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
475
476 // Emit the 'mov' instruction.
477 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
478
479 // Set the conditional execution predicate.
Evan Cheng97f48c32008-11-06 22:15:19 +0000480 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng90922132008-11-06 02:25:39 +0000481
482 // Encode Rd.
483 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
484
485 // Encode so_imm.
486 // Set bit I(25) to identify this is the immediate form of <shifter_op>
487 Binary |= 1 << ARMII::I_BitShift;
488 Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V1));
489 emitWordLE(Binary);
490
491 // Now the 'orr' instruction.
492 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
493
494 // Set the conditional execution predicate.
Evan Cheng97f48c32008-11-06 22:15:19 +0000495 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng90922132008-11-06 02:25:39 +0000496
497 // Encode Rd.
498 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
499
500 // Encode Rn.
501 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
502
503 // Encode so_imm.
504 // Set bit I(25) to identify this is the immediate form of <shifter_op>
505 Binary |= 1 << ARMII::I_BitShift;
506 Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V2));
507 emitWordLE(Binary);
508}
509
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000510template< class machineCodeEmitter>
511void Emitter< machineCodeEmitter>::emitLEApcrelJTInstruction(const MachineInstr &MI) {
Evan Cheng4df60f52008-11-07 09:06:08 +0000512 // It's basically add r, pc, (LJTI - $+8)
513
514 const TargetInstrDesc &TID = MI.getDesc();
515
516 // Emit the 'add' instruction.
517 unsigned Binary = 0x4 << 21; // add: Insts{24-31} = 0b0100
518
519 // Set the conditional execution predicate
520 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
521
522 // Encode S bit if MI modifies CPSR.
523 Binary |= getAddrModeSBit(MI, TID);
524
525 // Encode Rd.
526 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
527
528 // Encode Rn which is PC.
529 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
530
531 // Encode the displacement.
532 // Set bit I(25) to identify this is the immediate form of <shifter_op>.
533 Binary |= 1 << ARMII::I_BitShift;
534 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
535
536 emitWordLE(Binary);
537}
538
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000539template< class machineCodeEmitter>
540void Emitter< machineCodeEmitter>::emitPseudoMoveInstruction(const MachineInstr &MI) {
Evan Chenga9562552008-11-14 20:09:11 +0000541 unsigned Opcode = MI.getDesc().Opcode;
542
543 // Part of binary is determined by TableGn.
544 unsigned Binary = getBinaryCodeForInstr(MI);
545
546 // Set the conditional execution predicate
547 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
548
549 // Encode S bit if MI modifies CPSR.
550 if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag)
551 Binary |= 1 << ARMII::S_BitShift;
552
553 // Encode register def if there is one.
554 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
555
556 // Encode the shift operation.
557 switch (Opcode) {
558 default: break;
559 case ARM::MOVrx:
560 // rrx
561 Binary |= 0x6 << 4;
562 break;
563 case ARM::MOVsrl_flag:
564 // lsr #1
565 Binary |= (0x2 << 4) | (1 << 7);
566 break;
567 case ARM::MOVsra_flag:
568 // asr #1
569 Binary |= (0x4 << 4) | (1 << 7);
570 break;
571 }
572
573 // Encode register Rm.
574 Binary |= getMachineOpValue(MI, 1);
575
576 emitWordLE(Binary);
577}
578
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000579template< class machineCodeEmitter>
580void Emitter< machineCodeEmitter>::addPCLabel(unsigned LabelID) {
Evan Cheng12c3a532008-11-06 17:48:05 +0000581 DOUT << " ** LPC" << LabelID << " @ "
Evan Cheng83b5cf02008-11-05 23:22:34 +0000582 << (void*)MCE.getCurrentPCValue() << '\n';
583 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
584}
585
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000586template< class machineCodeEmitter>
587void Emitter< machineCodeEmitter>::emitPseudoInstruction(const MachineInstr &MI) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000588 unsigned Opcode = MI.getDesc().Opcode;
589 switch (Opcode) {
590 default:
591 abort(); // FIXME:
Evan Chengffa6d962008-11-13 23:36:57 +0000592 case TargetInstrInfo::INLINEASM: {
Evan Chenge3066ab2008-11-19 23:21:33 +0000593 // We allow inline assembler nodes with empty bodies - they can
594 // implicitly define registers, which is ok for JIT.
595 if (MI.getOperand(0).getSymbolName()[0]) {
596 assert(0 && "JIT does not support inline asm!\n");
597 abort();
598 }
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 Lopesa3f99f92009-05-30 20:51:52 +0000656template< class machineCodeEmitter>
657unsigned Emitter< machineCodeEmitter>::getMachineSoRegOpValue(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000658 const TargetInstrDesc &TID,
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000659 const MachineOperand &MO,
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000660 unsigned OpIdx) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000661 unsigned Binary = getMachineOpValue(MI, MO);
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000662
663 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
664 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
665 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
666
667 // Encode the shift opcode.
668 unsigned SBits = 0;
669 unsigned Rs = MO1.getReg();
670 if (Rs) {
671 // Set shift operand (bit[7:4]).
672 // LSL - 0001
673 // LSR - 0011
674 // ASR - 0101
675 // ROR - 0111
676 // RRX - 0110 and bit[11:8] clear.
677 switch (SOpc) {
678 default: assert(0 && "Unknown shift opc!");
679 case ARM_AM::lsl: SBits = 0x1; break;
680 case ARM_AM::lsr: SBits = 0x3; break;
681 case ARM_AM::asr: SBits = 0x5; break;
682 case ARM_AM::ror: SBits = 0x7; break;
683 case ARM_AM::rrx: SBits = 0x6; break;
684 }
685 } else {
686 // Set shift operand (bit[6:4]).
687 // LSL - 000
688 // LSR - 010
689 // ASR - 100
690 // ROR - 110
691 switch (SOpc) {
692 default: assert(0 && "Unknown shift opc!");
693 case ARM_AM::lsl: SBits = 0x0; break;
694 case ARM_AM::lsr: SBits = 0x2; break;
695 case ARM_AM::asr: SBits = 0x4; break;
696 case ARM_AM::ror: SBits = 0x6; break;
697 }
698 }
699 Binary |= SBits << 4;
700 if (SOpc == ARM_AM::rrx)
701 return Binary;
702
703 // Encode the shift operation Rs or shift_imm (except rrx).
704 if (Rs) {
705 // Encode Rs bit[11:8].
706 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
707 return Binary |
708 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
709 }
710
711 // Encode shift_imm bit[11:7].
712 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
713}
714
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000715template< class machineCodeEmitter>
716unsigned Emitter< machineCodeEmitter>::getMachineSoImmOpValue(unsigned SoImm) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000717 // Encode rotate_imm.
Evan Cheng97f48c32008-11-06 22:15:19 +0000718 unsigned Binary = (ARM_AM::getSOImmValRot(SoImm) >> 1)
719 << ARMII::SoRotImmShift;
720
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000721 // Encode immed_8.
Evan Cheng90922132008-11-06 02:25:39 +0000722 Binary |= ARM_AM::getSOImmValImm(SoImm);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000723 return Binary;
724}
725
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000726template< class machineCodeEmitter>
727unsigned Emitter< machineCodeEmitter>::getAddrModeSBit(const MachineInstr &MI,
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +0000728 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 Lopesa3f99f92009-05-30 20:51:52 +0000737template< class machineCodeEmitter>
738void Emitter< machineCodeEmitter>::emitDataProcessingInstruction(const MachineInstr &MI,
Evan Cheng437c1732008-11-07 22:30:53 +0000739 unsigned ImplicitRd,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000740 unsigned ImplicitRn) {
Evan Chengedda31c2008-11-05 18:35:52 +0000741 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +0000742
743 // Part of binary is determined by TableGn.
744 unsigned Binary = getBinaryCodeForInstr(MI);
745
Jim Grosbach33412622008-10-07 19:05:35 +0000746 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000747 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000748
Evan Cheng49a9f292008-09-12 22:45:55 +0000749 // Encode S bit if MI modifies CPSR.
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +0000750 Binary |= getAddrModeSBit(MI, TID);
Evan Cheng49a9f292008-09-12 22:45:55 +0000751
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000752 // Encode register def if there is one.
Evan Cheng49a9f292008-09-12 22:45:55 +0000753 unsigned NumDefs = TID.getNumDefs();
Evan Chenga964b7d2008-09-12 23:15:39 +0000754 unsigned OpIdx = 0;
Evan Cheng437c1732008-11-07 22:30:53 +0000755 if (NumDefs)
756 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
757 else if (ImplicitRd)
758 // Special handling for implicit use (e.g. PC).
759 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
760 << ARMII::RegRdShift);
Evan Cheng7602e112008-09-02 06:52:38 +0000761
Evan Chengd87293c2008-11-06 08:47:38 +0000762 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
763 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
764 ++OpIdx;
765
Jim Grosbachefd30ba2008-10-01 18:16:49 +0000766 // Encode first non-shifter register operand if there is one.
Evan Chengedda31c2008-11-05 18:35:52 +0000767 bool isUnary = TID.TSFlags & ARMII::UnaryDP;
768 if (!isUnary) {
Evan Cheng83b5cf02008-11-05 23:22:34 +0000769 if (ImplicitRn)
770 // Special handling for implicit use (e.g. PC).
771 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
Evan Chengedda31c2008-11-05 18:35:52 +0000772 << ARMII::RegRnShift);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000773 else {
774 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
775 ++OpIdx;
776 }
Evan Cheng7602e112008-09-02 06:52:38 +0000777 }
778
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000779 // Encode shifter operand.
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000780 const MachineOperand &MO = MI.getOperand(OpIdx);
Evan Chengedda31c2008-11-05 18:35:52 +0000781 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000782 // Encode SoReg.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000783 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
Evan Chengedda31c2008-11-05 18:35:52 +0000784 return;
785 }
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000786
Evan Chengedda31c2008-11-05 18:35:52 +0000787 if (MO.isReg()) {
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000788 // Encode register Rm.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000789 emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
Evan Chengedda31c2008-11-05 18:35:52 +0000790 return;
791 }
Evan Cheng7602e112008-09-02 06:52:38 +0000792
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000793 // Encode so_imm.
Evan Cheng4df60f52008-11-07 09:06:08 +0000794 // Set bit I(25) to identify this is the immediate form of <shifter_op>.
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000795 Binary |= 1 << ARMII::I_BitShift;
Evan Cheng90922132008-11-06 02:25:39 +0000796 Binary |= getMachineSoImmOpValue(MO.getImm());
Evan Chengedda31c2008-11-05 18:35:52 +0000797
Evan Cheng83b5cf02008-11-05 23:22:34 +0000798 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000799}
800
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000801template< class machineCodeEmitter>
802void Emitter< machineCodeEmitter>::emitLoadStoreInstruction(const MachineInstr &MI,
Evan Cheng4df60f52008-11-07 09:06:08 +0000803 unsigned ImplicitRd,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000804 unsigned ImplicitRn) {
Evan Cheng05c356e2008-11-08 01:44:13 +0000805 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng148cad82008-11-13 07:34:59 +0000806 unsigned Form = TID.TSFlags & ARMII::FormMask;
807 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +0000808
Evan Chengedda31c2008-11-05 18:35:52 +0000809 // Part of binary is determined by TableGn.
810 unsigned Binary = getBinaryCodeForInstr(MI);
811
Jim Grosbach33412622008-10-07 19:05:35 +0000812 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000813 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +0000814
Evan Cheng4df60f52008-11-07 09:06:08 +0000815 unsigned OpIdx = 0;
Evan Cheng148cad82008-11-13 07:34:59 +0000816
817 // Operand 0 of a pre- and post-indexed store is the address base
818 // writeback. Skip it.
819 bool Skipped = false;
820 if (IsPrePost && Form == ARMII::StFrm) {
821 ++OpIdx;
822 Skipped = true;
823 }
824
825 // Set first operand
Evan Cheng4df60f52008-11-07 09:06:08 +0000826 if (ImplicitRd)
827 // Special handling for implicit use (e.g. PC).
828 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
829 << ARMII::RegRdShift);
830 else
831 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000832
833 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +0000834 if (ImplicitRn)
835 // Special handling for implicit use (e.g. PC).
836 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
837 << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +0000838 else
839 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000840
Evan Cheng05c356e2008-11-08 01:44:13 +0000841 // If this is a two-address operand, skip it. e.g. LDR_PRE.
Evan Cheng148cad82008-11-13 07:34:59 +0000842 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +0000843 ++OpIdx;
844
Evan Cheng83b5cf02008-11-05 23:22:34 +0000845 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +0000846 unsigned AM2Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000847 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +0000848
Evan Chenge7de7e32008-09-13 01:44:01 +0000849 // Set bit U(23) according to sign of immed value (positive or negative).
Evan Cheng83b5cf02008-11-05 23:22:34 +0000850 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
Evan Chenge7de7e32008-09-13 01:44:01 +0000851 ARMII::U_BitShift);
Evan Cheng7602e112008-09-02 06:52:38 +0000852 if (!MO2.getReg()) { // is immediate
Evan Cheng83b5cf02008-11-05 23:22:34 +0000853 if (ARM_AM::getAM2Offset(AM2Opc))
Evan Cheng7602e112008-09-02 06:52:38 +0000854 // Set the value of offset_12 field
Evan Cheng83b5cf02008-11-05 23:22:34 +0000855 Binary |= ARM_AM::getAM2Offset(AM2Opc);
856 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +0000857 return;
Evan Cheng7602e112008-09-02 06:52:38 +0000858 }
859
860 // Set bit I(25), because this is not in immediate enconding.
861 Binary |= 1 << ARMII::I_BitShift;
862 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
863 // Set bit[3:0] to the corresponding Rm register
864 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
865
Evan Cheng70632912008-11-12 07:34:37 +0000866 // If this instr is in scaled register offset/index instruction, set
Evan Cheng7602e112008-09-02 06:52:38 +0000867 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000868 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
Evan Cheng70632912008-11-12 07:34:37 +0000869 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
870 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
Evan Cheng7602e112008-09-02 06:52:38 +0000871 }
872
Evan Cheng83b5cf02008-11-05 23:22:34 +0000873 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000874}
875
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000876template< class machineCodeEmitter>
877void Emitter< machineCodeEmitter>::emitMiscLoadStoreInstruction(const MachineInstr &MI,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000878 unsigned ImplicitRn) {
Evan Cheng05c356e2008-11-08 01:44:13 +0000879 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng148cad82008-11-13 07:34:59 +0000880 unsigned Form = TID.TSFlags & ARMII::FormMask;
881 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +0000882
Evan Chengedda31c2008-11-05 18:35:52 +0000883 // Part of binary is determined by TableGn.
884 unsigned Binary = getBinaryCodeForInstr(MI);
885
Jim Grosbach33412622008-10-07 19:05:35 +0000886 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000887 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +0000888
Evan Cheng148cad82008-11-13 07:34:59 +0000889 unsigned OpIdx = 0;
890
891 // Operand 0 of a pre- and post-indexed store is the address base
892 // writeback. Skip it.
893 bool Skipped = false;
894 if (IsPrePost && Form == ARMII::StMiscFrm) {
895 ++OpIdx;
896 Skipped = true;
897 }
898
Evan Cheng7602e112008-09-02 06:52:38 +0000899 // Set first operand
Evan Cheng148cad82008-11-13 07:34:59 +0000900 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000901
902 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +0000903 if (ImplicitRn)
904 // Special handling for implicit use (e.g. PC).
905 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
906 << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +0000907 else
908 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000909
Evan Cheng05c356e2008-11-08 01:44:13 +0000910 // If this is a two-address operand, skip it. e.g. LDRH_POST.
Evan Cheng148cad82008-11-13 07:34:59 +0000911 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +0000912 ++OpIdx;
913
Evan Cheng83b5cf02008-11-05 23:22:34 +0000914 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +0000915 unsigned AM3Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000916 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +0000917
Evan Chenge7de7e32008-09-13 01:44:01 +0000918 // Set bit U(23) according to sign of immed value (positive or negative)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000919 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
Evan Cheng7602e112008-09-02 06:52:38 +0000920 ARMII::U_BitShift);
921
922 // If this instr is in register offset/index encoding, set bit[3:0]
923 // to the corresponding Rm register.
924 if (MO2.getReg()) {
925 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
Evan Cheng83b5cf02008-11-05 23:22:34 +0000926 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +0000927 return;
Evan Cheng7602e112008-09-02 06:52:38 +0000928 }
929
Evan Chengd87293c2008-11-06 08:47:38 +0000930 // This instr is in immediate offset/index encoding, set bit 22 to 1.
Evan Cheng97f48c32008-11-06 22:15:19 +0000931 Binary |= 1 << ARMII::AM3_I_BitShift;
Evan Cheng83b5cf02008-11-05 23:22:34 +0000932 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
Evan Cheng7602e112008-09-02 06:52:38 +0000933 // Set operands
Evan Cheng70632912008-11-12 07:34:37 +0000934 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
935 Binary |= (ImmOffs & 0xF); // immedL
Evan Cheng7602e112008-09-02 06:52:38 +0000936 }
937
Evan Cheng83b5cf02008-11-05 23:22:34 +0000938 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000939}
940
Evan Chengcd8e66a2008-11-11 21:48:44 +0000941static unsigned getAddrModeUPBits(unsigned Mode) {
942 unsigned Binary = 0;
Evan Cheng7602e112008-09-02 06:52:38 +0000943
944 // Set addressing mode by modifying bits U(23) and P(24)
945 // IA - Increment after - bit U = 1 and bit P = 0
946 // IB - Increment before - bit U = 1 and bit P = 1
947 // DA - Decrement after - bit U = 0 and bit P = 0
948 // DB - Decrement before - bit U = 0 and bit P = 1
Evan Cheng7602e112008-09-02 06:52:38 +0000949 switch (Mode) {
950 default: assert(0 && "Unknown addressing sub-mode!");
951 case ARM_AM::da: break;
Evan Cheng97f48c32008-11-06 22:15:19 +0000952 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
953 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
954 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
Evan Cheng7602e112008-09-02 06:52:38 +0000955 }
956
Evan Chengcd8e66a2008-11-11 21:48:44 +0000957 return Binary;
958}
959
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000960template< class machineCodeEmitter>
961void Emitter< machineCodeEmitter>::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +0000962 // Part of binary is determined by TableGn.
963 unsigned Binary = getBinaryCodeForInstr(MI);
964
965 // Set the conditional execution predicate
966 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
967
968 // Set base address operand
969 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
970
971 // Set addressing mode by modifying bits U(23) and P(24)
972 const MachineOperand &MO = MI.getOperand(1);
973 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
974
Evan Cheng7602e112008-09-02 06:52:38 +0000975 // Set bit W(21)
976 if (ARM_AM::getAM4WBFlag(MO.getImm()))
Evan Cheng97f48c32008-11-06 22:15:19 +0000977 Binary |= 0x1 << ARMII::W_BitShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000978
979 // Set registers
980 for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
981 const MachineOperand &MO = MI.getOperand(i);
Evan Chengcd8e66a2008-11-11 21:48:44 +0000982 if (!MO.isReg() || MO.isImplicit())
983 break;
Evan Cheng7602e112008-09-02 06:52:38 +0000984 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
985 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
986 RegNum < 16);
987 Binary |= 0x1 << RegNum;
988 }
989
Evan Cheng83b5cf02008-11-05 23:22:34 +0000990 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000991}
992
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000993template< class machineCodeEmitter>
994void Emitter< machineCodeEmitter>::emitMulFrmInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +0000995 const TargetInstrDesc &TID = MI.getDesc();
996
997 // Part of binary is determined by TableGn.
998 unsigned Binary = getBinaryCodeForInstr(MI);
999
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001000 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001001 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001002
1003 // Encode S bit if MI modifies CPSR.
1004 Binary |= getAddrModeSBit(MI, TID);
1005
1006 // 32x32->64bit operations have two destination registers. The number
1007 // of register definitions will tell us if that's what we're dealing with.
Evan Cheng97f48c32008-11-06 22:15:19 +00001008 unsigned OpIdx = 0;
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001009 if (TID.getNumDefs() == 2)
1010 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
1011
1012 // Encode Rd
1013 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
1014
1015 // Encode Rm
1016 Binary |= getMachineOpValue(MI, OpIdx++);
1017
1018 // Encode Rs
1019 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
1020
Evan Chengfbc9d412008-11-06 01:21:28 +00001021 // Many multiple instructions (e.g. MLA) have three src operands. Encode
1022 // it as Rn (for multiply, that's in the same offset as RdLo.
Evan Cheng97f48c32008-11-06 22:15:19 +00001023 if (TID.getNumOperands() > OpIdx &&
1024 !TID.OpInfo[OpIdx].isPredicate() &&
1025 !TID.OpInfo[OpIdx].isOptionalDef())
1026 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
1027
1028 emitWordLE(Binary);
1029}
1030
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001031template< class machineCodeEmitter>
1032void Emitter< machineCodeEmitter>::emitExtendInstruction(const MachineInstr &MI) {
Evan Cheng97f48c32008-11-06 22:15:19 +00001033 const TargetInstrDesc &TID = MI.getDesc();
1034
1035 // Part of binary is determined by TableGn.
1036 unsigned Binary = getBinaryCodeForInstr(MI);
1037
1038 // Set the conditional execution predicate
1039 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1040
1041 unsigned OpIdx = 0;
1042
1043 // Encode Rd
1044 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1045
1046 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
1047 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1048 if (MO2.isReg()) {
1049 // Two register operand form.
1050 // Encode Rn.
1051 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
1052
1053 // Encode Rm.
1054 Binary |= getMachineOpValue(MI, MO2);
1055 ++OpIdx;
1056 } else {
1057 Binary |= getMachineOpValue(MI, MO1);
1058 }
1059
1060 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
1061 if (MI.getOperand(OpIdx).isImm() &&
1062 !TID.OpInfo[OpIdx].isPredicate() &&
1063 !TID.OpInfo[OpIdx].isOptionalDef())
1064 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
Evan Chengfbc9d412008-11-06 01:21:28 +00001065
Evan Cheng83b5cf02008-11-05 23:22:34 +00001066 emitWordLE(Binary);
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001067}
1068
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001069template< class machineCodeEmitter>
1070void Emitter< machineCodeEmitter>::emitMiscArithInstruction(const MachineInstr &MI) {
Evan Cheng8b59db32008-11-07 01:41:35 +00001071 const TargetInstrDesc &TID = MI.getDesc();
1072
1073 // Part of binary is determined by TableGn.
1074 unsigned Binary = getBinaryCodeForInstr(MI);
1075
1076 // Set the conditional execution predicate
1077 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1078
1079 unsigned OpIdx = 0;
1080
1081 // Encode Rd
1082 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1083
1084 const MachineOperand &MO = MI.getOperand(OpIdx++);
1085 if (OpIdx == TID.getNumOperands() ||
1086 TID.OpInfo[OpIdx].isPredicate() ||
1087 TID.OpInfo[OpIdx].isOptionalDef()) {
1088 // Encode Rm and it's done.
1089 Binary |= getMachineOpValue(MI, MO);
1090 emitWordLE(Binary);
1091 return;
1092 }
1093
1094 // Encode Rn.
1095 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1096
1097 // Encode Rm.
1098 Binary |= getMachineOpValue(MI, OpIdx++);
1099
1100 // Encode shift_imm.
1101 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
1102 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1103 Binary |= ShiftAmt << ARMII::ShiftShift;
1104
1105 emitWordLE(Binary);
1106}
1107
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001108template< class machineCodeEmitter>
1109void Emitter< machineCodeEmitter>::emitBranchInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001110 const TargetInstrDesc &TID = MI.getDesc();
1111
Evan Cheng12c3a532008-11-06 17:48:05 +00001112 if (TID.Opcode == ARM::TPsoft)
1113 abort(); // FIXME
1114
Evan Cheng7602e112008-09-02 06:52:38 +00001115 // Part of binary is determined by TableGn.
1116 unsigned Binary = getBinaryCodeForInstr(MI);
1117
Evan Chengedda31c2008-11-05 18:35:52 +00001118 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001119 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001120
1121 // Set signed_immed_24 field
1122 Binary |= getMachineOpValue(MI, 0);
1123
Evan Cheng83b5cf02008-11-05 23:22:34 +00001124 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +00001125}
1126
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001127template< class machineCodeEmitter>
1128void Emitter< machineCodeEmitter>::emitInlineJumpTable(unsigned JTIndex) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001129 // Remember the base address of the inline jump table.
Evan Cheng5788d1a2008-12-10 02:32:19 +00001130 uintptr_t JTBase = MCE.getCurrentPCValue();
Evan Cheng437c1732008-11-07 22:30:53 +00001131 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
1132 DOUT << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase << '\n';
Evan Cheng4df60f52008-11-07 09:06:08 +00001133
1134 // Now emit the jump table entries.
1135 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1136 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1137 if (IsPIC)
1138 // DestBB address - JT base.
Evan Cheng437c1732008-11-07 22:30:53 +00001139 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
Evan Cheng4df60f52008-11-07 09:06:08 +00001140 else
1141 // Absolute DestBB address.
1142 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1143 emitWordLE(0);
1144 }
1145}
1146
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001147template< class machineCodeEmitter>
1148void Emitter< machineCodeEmitter>::emitMiscBranchInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001149 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +00001150
Evan Cheng437c1732008-11-07 22:30:53 +00001151 // Handle jump tables.
1152 if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
1153 // First emit a ldr pc, [] instruction.
1154 emitDataProcessingInstruction(MI, ARM::PC);
1155
1156 // Then emit the inline jump table.
1157 unsigned JTIndex = (TID.Opcode == ARM::BR_JTr)
1158 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1159 emitInlineJumpTable(JTIndex);
1160 return;
1161 } else if (TID.Opcode == ARM::BR_JTm) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001162 // First emit a ldr pc, [] instruction.
1163 emitLoadStoreInstruction(MI, ARM::PC);
1164
1165 // Then emit the inline jump table.
Evan Cheng437c1732008-11-07 22:30:53 +00001166 emitInlineJumpTable(MI.getOperand(3).getIndex());
Evan Cheng4df60f52008-11-07 09:06:08 +00001167 return;
1168 }
1169
Evan Chengedda31c2008-11-05 18:35:52 +00001170 // Part of binary is determined by TableGn.
1171 unsigned Binary = getBinaryCodeForInstr(MI);
1172
1173 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001174 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001175
1176 if (TID.Opcode == ARM::BX_RET)
1177 // The return register is LR.
1178 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
1179 else
1180 // otherwise, set the return register
1181 Binary |= getMachineOpValue(MI, 0);
1182
Evan Cheng83b5cf02008-11-05 23:22:34 +00001183 emitWordLE(Binary);
Evan Cheng148b6a42007-07-05 21:15:40 +00001184}
Evan Cheng7602e112008-09-02 06:52:38 +00001185
Evan Cheng80a11982008-11-12 06:41:41 +00001186static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) {
Evan Chengd06d48d2008-11-12 02:19:38 +00001187 unsigned RegD = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001188 unsigned Binary = 0;
Evan Chengd06d48d2008-11-12 02:19:38 +00001189 bool isSPVFP = false;
1190 RegD = ARMRegisterInfo::getRegisterNumbering(RegD, isSPVFP);
1191 if (!isSPVFP)
1192 Binary |= RegD << ARMII::RegRdShift;
1193 else {
1194 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1195 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1196 }
Evan Cheng80a11982008-11-12 06:41:41 +00001197 return Binary;
1198}
Evan Cheng78be83d2008-11-11 19:40:26 +00001199
Evan Cheng80a11982008-11-12 06:41:41 +00001200static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) {
Evan Chengd06d48d2008-11-12 02:19:38 +00001201 unsigned RegN = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001202 unsigned Binary = 0;
1203 bool isSPVFP = false;
Evan Chengd06d48d2008-11-12 02:19:38 +00001204 RegN = ARMRegisterInfo::getRegisterNumbering(RegN, isSPVFP);
1205 if (!isSPVFP)
1206 Binary |= RegN << ARMII::RegRnShift;
1207 else {
1208 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1209 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1210 }
Evan Cheng80a11982008-11-12 06:41:41 +00001211 return Binary;
1212}
Evan Chengd06d48d2008-11-12 02:19:38 +00001213
Evan Cheng80a11982008-11-12 06:41:41 +00001214static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) {
1215 unsigned RegM = MI.getOperand(OpIdx).getReg();
1216 unsigned Binary = 0;
1217 bool isSPVFP = false;
1218 RegM = ARMRegisterInfo::getRegisterNumbering(RegM, isSPVFP);
1219 if (!isSPVFP)
1220 Binary |= RegM;
1221 else {
1222 Binary |= ((RegM & 0x1E) >> 1);
1223 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
Evan Cheng78be83d2008-11-11 19:40:26 +00001224 }
Evan Cheng80a11982008-11-12 06:41:41 +00001225 return Binary;
1226}
1227
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001228template< class machineCodeEmitter>
1229void Emitter< machineCodeEmitter>::emitVFPArithInstruction(const MachineInstr &MI) {
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001230 const TargetInstrDesc &TID = MI.getDesc();
1231
1232 // Part of binary is determined by TableGn.
1233 unsigned Binary = getBinaryCodeForInstr(MI);
1234
1235 // Set the conditional execution predicate
1236 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1237
1238 unsigned OpIdx = 0;
1239 assert((Binary & ARMII::D_BitShift) == 0 &&
1240 (Binary & ARMII::N_BitShift) == 0 &&
1241 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1242
1243 // Encode Dd / Sd.
1244 Binary |= encodeVFPRd(MI, OpIdx++);
1245
1246 // If this is a two-address operand, skip it, e.g. FMACD.
1247 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1248 ++OpIdx;
1249
1250 // Encode Dn / Sn.
1251 if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
Evan Cheng3f4924e2008-11-12 08:14:21 +00001252 Binary |= encodeVFPRn(MI, OpIdx++);
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001253
1254 if (OpIdx == TID.getNumOperands() ||
1255 TID.OpInfo[OpIdx].isPredicate() ||
1256 TID.OpInfo[OpIdx].isOptionalDef()) {
1257 // FCMPEZD etc. has only one operand.
1258 emitWordLE(Binary);
1259 return;
1260 }
1261
1262 // Encode Dm / Sm.
1263 Binary |= encodeVFPRm(MI, OpIdx);
1264
1265 emitWordLE(Binary);
1266}
1267
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001268template< class machineCodeEmitter>
1269void Emitter< machineCodeEmitter>::emitVFPConversionInstruction(const MachineInstr &MI) {
Evan Cheng80a11982008-11-12 06:41:41 +00001270 const TargetInstrDesc &TID = MI.getDesc();
1271 unsigned Form = TID.TSFlags & ARMII::FormMask;
1272
1273 // Part of binary is determined by TableGn.
1274 unsigned Binary = getBinaryCodeForInstr(MI);
1275
1276 // Set the conditional execution predicate
1277 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1278
1279 switch (Form) {
1280 default: break;
1281 case ARMII::VFPConv1Frm:
1282 case ARMII::VFPConv2Frm:
1283 case ARMII::VFPConv3Frm:
1284 // Encode Dd / Sd.
1285 Binary |= encodeVFPRd(MI, 0);
1286 break;
1287 case ARMII::VFPConv4Frm:
1288 // Encode Dn / Sn.
1289 Binary |= encodeVFPRn(MI, 0);
1290 break;
1291 case ARMII::VFPConv5Frm:
1292 // Encode Dm / Sm.
1293 Binary |= encodeVFPRm(MI, 0);
1294 break;
1295 }
1296
1297 switch (Form) {
1298 default: break;
1299 case ARMII::VFPConv1Frm:
1300 // Encode Dm / Sm.
1301 Binary |= encodeVFPRm(MI, 1);
Evan Cheng67fd91f2008-11-13 07:46:59 +00001302 break;
Evan Cheng80a11982008-11-12 06:41:41 +00001303 case ARMII::VFPConv2Frm:
1304 case ARMII::VFPConv3Frm:
1305 // Encode Dn / Sn.
1306 Binary |= encodeVFPRn(MI, 1);
1307 break;
1308 case ARMII::VFPConv4Frm:
1309 case ARMII::VFPConv5Frm:
1310 // Encode Dd / Sd.
1311 Binary |= encodeVFPRd(MI, 1);
1312 break;
1313 }
1314
1315 if (Form == ARMII::VFPConv5Frm)
1316 // Encode Dn / Sn.
1317 Binary |= encodeVFPRn(MI, 2);
1318 else if (Form == ARMII::VFPConv3Frm)
1319 // Encode Dm / Sm.
1320 Binary |= encodeVFPRm(MI, 2);
Evan Cheng78be83d2008-11-11 19:40:26 +00001321
1322 emitWordLE(Binary);
1323}
1324
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001325template< class machineCodeEmitter>
1326void Emitter< machineCodeEmitter>::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001327 // Part of binary is determined by TableGn.
1328 unsigned Binary = getBinaryCodeForInstr(MI);
1329
1330 // Set the conditional execution predicate
1331 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1332
1333 unsigned OpIdx = 0;
1334
1335 // Encode Dd / Sd.
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001336 Binary |= encodeVFPRd(MI, OpIdx++);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001337
1338 // Encode address base.
1339 const MachineOperand &Base = MI.getOperand(OpIdx++);
1340 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1341
1342 // If there is a non-zero immediate offset, encode it.
1343 if (Base.isReg()) {
1344 const MachineOperand &Offset = MI.getOperand(OpIdx);
1345 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1346 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1347 Binary |= 1 << ARMII::U_BitShift;
Evan Cheng607f1b42008-11-12 08:21:12 +00001348 Binary |= ImmOffs;
Evan Chengcd8e66a2008-11-11 21:48:44 +00001349 emitWordLE(Binary);
1350 return;
1351 }
1352 }
1353
1354 // If immediate offset is omitted, default to +0.
1355 Binary |= 1 << ARMII::U_BitShift;
1356
1357 emitWordLE(Binary);
1358}
1359
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001360template< class machineCodeEmitter>
1361void Emitter< machineCodeEmitter>::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001362 // Part of binary is determined by TableGn.
1363 unsigned Binary = getBinaryCodeForInstr(MI);
1364
1365 // Set the conditional execution predicate
1366 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1367
1368 // Set base address operand
1369 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
1370
1371 // Set addressing mode by modifying bits U(23) and P(24)
1372 const MachineOperand &MO = MI.getOperand(1);
1373 Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm()));
1374
1375 // Set bit W(21)
1376 if (ARM_AM::getAM5WBFlag(MO.getImm()))
1377 Binary |= 0x1 << ARMII::W_BitShift;
1378
1379 // First register is encoded in Dd.
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001380 Binary |= encodeVFPRd(MI, 4);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001381
1382 // Number of registers are encoded in offset field.
1383 unsigned NumRegs = 1;
1384 for (unsigned i = 5, e = MI.getNumOperands(); i != e; ++i) {
1385 const MachineOperand &MO = MI.getOperand(i);
1386 if (!MO.isReg() || MO.isImplicit())
1387 break;
1388 ++NumRegs;
1389 }
1390 Binary |= NumRegs * 2;
1391
1392 emitWordLE(Binary);
1393}
1394
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001395template< class machineCodeEmitter>
1396void Emitter< machineCodeEmitter>::emitMiscInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001397 // Part of binary is determined by TableGn.
1398 unsigned Binary = getBinaryCodeForInstr(MI);
1399
1400 // Set the conditional execution predicate
1401 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1402
1403 emitWordLE(Binary);
1404}
1405
Evan Cheng7602e112008-09-02 06:52:38 +00001406#include "ARMGenCodeEmitter.inc"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001407