blob: 8424c2eaed1d78b2f8e7c38e6f84b69cd415c97b [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:
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000048 /// getBinaryCodeForInstr - This function, generated by the
49 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
50 /// machine instructions.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000051 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
52 };
53
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000054 template<class CodeEmitter>
55 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
56 public ARMCodeEmitter {
Evan Cheng057d0c32008-09-18 07:28:19 +000057 ARMJITInfo *JTI;
58 const ARMInstrInfo *II;
59 const TargetData *TD;
60 TargetMachine &TM;
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000061 CodeEmitter &MCE;
Evan Cheng938b9d82008-10-31 19:55:13 +000062 const std::vector<MachineConstantPoolEntry> *MCPEs;
Evan Cheng4df60f52008-11-07 09:06:08 +000063 const std::vector<MachineJumpTableEntry> *MJTEs;
64 bool IsPIC;
65
Evan Cheng148b6a42007-07-05 21:15:40 +000066 public:
67 static char ID;
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000068 explicit Emitter(TargetMachine &tm, CodeEmitter &mce)
Evan Cheng057d0c32008-09-18 07:28:19 +000069 : MachineFunctionPass(&ID), JTI(0), II(0), TD(0), TM(tm),
Evan Cheng4df60f52008-11-07 09:06:08 +000070 MCE(mce), MCPEs(0), MJTEs(0),
71 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000072 Emitter(TargetMachine &tm, CodeEmitter &mce,
Evan Cheng148b6a42007-07-05 21:15:40 +000073 const ARMInstrInfo &ii, const TargetData &td)
Evan Cheng057d0c32008-09-18 07:28:19 +000074 : MachineFunctionPass(&ID), JTI(0), II(&ii), TD(&td), TM(tm),
Evan Cheng4df60f52008-11-07 09:06:08 +000075 MCE(mce), MCPEs(0), MJTEs(0),
76 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Evan Cheng148b6a42007-07-05 21:15:40 +000077
78 bool runOnMachineFunction(MachineFunction &MF);
79
80 virtual const char *getPassName() const {
81 return "ARM Machine Code Emitter";
82 }
83
84 void emitInstruction(const MachineInstr &MI);
Evan Cheng7602e112008-09-02 06:52:38 +000085
86 private:
Evan Cheng057d0c32008-09-18 07:28:19 +000087
Evan Cheng83b5cf02008-11-05 23:22:34 +000088 void emitWordLE(unsigned Binary);
89
Evan Chengcb5201f2008-11-11 22:19:31 +000090 void emitDWordLE(uint64_t Binary);
91
Evan Cheng057d0c32008-09-18 07:28:19 +000092 void emitConstPoolInstruction(const MachineInstr &MI);
93
Evan Cheng90922132008-11-06 02:25:39 +000094 void emitMOVi2piecesInstruction(const MachineInstr &MI);
95
Evan Cheng4df60f52008-11-07 09:06:08 +000096 void emitLEApcrelJTInstruction(const MachineInstr &MI);
97
Evan Chenga9562552008-11-14 20:09:11 +000098 void emitPseudoMoveInstruction(const MachineInstr &MI);
99
Evan Cheng83b5cf02008-11-05 23:22:34 +0000100 void addPCLabel(unsigned LabelID);
101
Evan Cheng057d0c32008-09-18 07:28:19 +0000102 void emitPseudoInstruction(const MachineInstr &MI);
103
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000104 unsigned getMachineSoRegOpValue(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000105 const TargetInstrDesc &TID,
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000106 const MachineOperand &MO,
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000107 unsigned OpIdx);
108
Evan Cheng90922132008-11-06 02:25:39 +0000109 unsigned getMachineSoImmOpValue(unsigned SoImm);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000110
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +0000111 unsigned getAddrModeSBit(const MachineInstr &MI,
112 const TargetInstrDesc &TID) const;
Evan Cheng49a9f292008-09-12 22:45:55 +0000113
Evan Cheng83b5cf02008-11-05 23:22:34 +0000114 void emitDataProcessingInstruction(const MachineInstr &MI,
Evan Cheng437c1732008-11-07 22:30:53 +0000115 unsigned ImplicitRd = 0,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000116 unsigned ImplicitRn = 0);
Evan Cheng7602e112008-09-02 06:52:38 +0000117
Evan Cheng83b5cf02008-11-05 23:22:34 +0000118 void emitLoadStoreInstruction(const MachineInstr &MI,
Evan Cheng4df60f52008-11-07 09:06:08 +0000119 unsigned ImplicitRd = 0,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000120 unsigned ImplicitRn = 0);
Evan Chengedda31c2008-11-05 18:35:52 +0000121
Evan Cheng83b5cf02008-11-05 23:22:34 +0000122 void emitMiscLoadStoreInstruction(const MachineInstr &MI,
123 unsigned ImplicitRn = 0);
Evan Chengedda31c2008-11-05 18:35:52 +0000124
125 void emitLoadStoreMultipleInstruction(const MachineInstr &MI);
126
Evan Chengfbc9d412008-11-06 01:21:28 +0000127 void emitMulFrmInstruction(const MachineInstr &MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000128
Evan Cheng97f48c32008-11-06 22:15:19 +0000129 void emitExtendInstruction(const MachineInstr &MI);
130
Evan Cheng8b59db32008-11-07 01:41:35 +0000131 void emitMiscArithInstruction(const MachineInstr &MI);
132
Evan Chengedda31c2008-11-05 18:35:52 +0000133 void emitBranchInstruction(const MachineInstr &MI);
134
Evan Cheng437c1732008-11-07 22:30:53 +0000135 void emitInlineJumpTable(unsigned JTIndex);
Evan Cheng4df60f52008-11-07 09:06:08 +0000136
Evan Chengedda31c2008-11-05 18:35:52 +0000137 void emitMiscBranchInstruction(const MachineInstr &MI);
Evan Cheng7602e112008-09-02 06:52:38 +0000138
Evan Cheng96581d32008-11-11 02:11:05 +0000139 void emitVFPArithInstruction(const MachineInstr &MI);
140
Evan Cheng78be83d2008-11-11 19:40:26 +0000141 void emitVFPConversionInstruction(const MachineInstr &MI);
142
Evan Chengcd8e66a2008-11-11 21:48:44 +0000143 void emitVFPLoadStoreInstruction(const MachineInstr &MI);
144
145 void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI);
146
147 void emitMiscInstruction(const MachineInstr &MI);
148
Evan Cheng7602e112008-09-02 06:52:38 +0000149 /// getMachineOpValue - Return binary encoding of operand. If the machine
150 /// operand requires relocation, record the relocation and return zero.
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000151 unsigned getMachineOpValue(const MachineInstr &MI,const MachineOperand &MO);
Evan Cheng7602e112008-09-02 06:52:38 +0000152 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) {
153 return getMachineOpValue(MI, MI.getOperand(OpIdx));
154 }
Evan Cheng7602e112008-09-02 06:52:38 +0000155
Evan Cheng83b5cf02008-11-05 23:22:34 +0000156 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng7602e112008-09-02 06:52:38 +0000157 ///
Evan Cheng83b5cf02008-11-05 23:22:34 +0000158 unsigned getShiftOp(unsigned Imm) const ;
Evan Cheng7602e112008-09-02 06:52:38 +0000159
160 /// Routines that handle operands which add machine relocations which are
Evan Cheng437c1732008-11-07 22:30:53 +0000161 /// fixed up by the relocation stage.
Evan Cheng057d0c32008-09-18 07:28:19 +0000162 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
Evan Cheng413a89f2008-11-07 22:57:53 +0000163 bool NeedStub, intptr_t ACPV = 0);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000164 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
Evan Cheng437c1732008-11-07 22:30:53 +0000165 void emitConstPoolAddress(unsigned CPI, unsigned Reloc);
166 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc);
167 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
168 intptr_t JTBase = 0);
Evan Cheng148b6a42007-07-05 21:15:40 +0000169 };
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000170 template <class CodeEmitter>
171 char Emitter<CodeEmitter>::ID = 0;
Evan Cheng148b6a42007-07-05 21:15:40 +0000172}
173
174/// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
175/// to the specified MCE object.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000176
177namespace llvm {
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000178
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000179FunctionPass *createARMCodeEmitterPass(ARMBaseTargetMachine &TM,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000180 MachineCodeEmitter &MCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000181 return new Emitter<MachineCodeEmitter>(TM, MCE);
182}
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000183FunctionPass *createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000184 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000185 return new Emitter<JITCodeEmitter>(TM, JCE);
Evan Cheng148b6a42007-07-05 21:15:40 +0000186}
187
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000188} // end namespace llvm
189
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000190template<class CodeEmitter>
191bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000192 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
193 MF.getTarget().getRelocationModel() != Reloc::Static) &&
194 "JIT relocation model must be set to static or default!");
195 II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo();
196 TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData();
Evan Cheng057d0c32008-09-18 07:28:19 +0000197 JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo();
Evan Cheng938b9d82008-10-31 19:55:13 +0000198 MCPEs = &MF.getConstantPool()->getConstants();
Evan Cheng4df60f52008-11-07 09:06:08 +0000199 MJTEs = &MF.getJumpTableInfo()->getJumpTables();
200 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Evan Cheng3cc82232008-11-08 07:38:22 +0000201 JTI->Initialize(MF, IsPIC);
Evan Cheng148b6a42007-07-05 21:15:40 +0000202
203 do {
Evan Cheng42d5ee062008-09-13 01:15:21 +0000204 DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
Evan Cheng148b6a42007-07-05 21:15:40 +0000205 MCE.startFunction(MF);
206 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
207 MBB != E; ++MBB) {
208 MCE.StartMachineBasicBlock(MBB);
209 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
210 I != E; ++I)
211 emitInstruction(*I);
212 }
213 } while (MCE.finishFunction(MF));
214
215 return false;
216}
217
Evan Cheng83b5cf02008-11-05 23:22:34 +0000218/// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
Evan Cheng7602e112008-09-02 06:52:38 +0000219///
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000220template<class CodeEmitter>
221unsigned Emitter<CodeEmitter>::getShiftOp(unsigned Imm) const {
Evan Cheng83b5cf02008-11-05 23:22:34 +0000222 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000223 default: assert(0 && "Unknown shift opc!");
Evan Cheng7602e112008-09-02 06:52:38 +0000224 case ARM_AM::asr: return 2;
225 case ARM_AM::lsl: return 0;
226 case ARM_AM::lsr: return 1;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000227 case ARM_AM::ror:
Evan Cheng7602e112008-09-02 06:52:38 +0000228 case ARM_AM::rrx: return 3;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000229 }
Evan Cheng7602e112008-09-02 06:52:38 +0000230 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000231}
232
Evan Cheng7602e112008-09-02 06:52:38 +0000233/// getMachineOpValue - Return binary encoding of operand. If the machine
234/// operand requires relocation, record the relocation and return zero.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000235template<class CodeEmitter>
236unsigned Emitter<CodeEmitter>::getMachineOpValue(const MachineInstr &MI,
237 const MachineOperand &MO) {
Dan Gohmand735b802008-10-03 15:45:36 +0000238 if (MO.isReg())
Evan Cheng7602e112008-09-02 06:52:38 +0000239 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000240 else if (MO.isImm())
Evan Cheng7602e112008-09-02 06:52:38 +0000241 return static_cast<unsigned>(MO.getImm());
Dan Gohmand735b802008-10-03 15:45:36 +0000242 else if (MO.isGlobal())
Jim Grosbach016d34c2008-10-03 15:52:42 +0000243 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true);
Dan Gohmand735b802008-10-03 15:45:36 +0000244 else if (MO.isSymbol())
Evan Cheng10332512008-11-08 07:22:33 +0000245 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
Evan Cheng580c0df2008-11-12 01:02:24 +0000246 else if (MO.isCPI()) {
247 const TargetInstrDesc &TID = MI.getDesc();
248 // For VFP load, the immediate offset is multiplied by 4.
249 unsigned Reloc = ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
250 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
251 emitConstPoolAddress(MO.getIndex(), Reloc);
252 } else if (MO.isJTI())
Chris Lattner8aa797a2007-12-30 23:10:15 +0000253 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
Dan Gohmand735b802008-10-03 15:45:36 +0000254 else if (MO.isMBB())
Evan Cheng4df60f52008-11-07 09:06:08 +0000255 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
Evan Cheng2aa0e642008-09-13 01:55:59 +0000256 else {
257 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
258 abort();
259 }
Evan Cheng7602e112008-09-02 06:52:38 +0000260 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000261}
262
Evan Cheng057d0c32008-09-18 07:28:19 +0000263/// emitGlobalAddress - Emit the specified address to the code stream.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000264///
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000265template<class CodeEmitter>
266void Emitter<CodeEmitter>::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
267 bool NeedStub, intptr_t ACPV) {
268 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
269 GV, ACPV, NeedStub));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000270}
271
272/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
273/// be emitted to the current location in the function, and allow it to be PC
274/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000275template<class CodeEmitter>
276void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES,
277 unsigned Reloc) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000278 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
279 Reloc, ES));
280}
281
282/// emitConstPoolAddress - Arrange for the address of an constant pool
283/// to be emitted to the current location in the function, and allow it to be PC
284/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000285template<class CodeEmitter>
286void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI,
287 unsigned Reloc) {
Evan Cheng0f282432008-10-29 23:55:43 +0000288 // Tell JIT emitter we'll resolve the address.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000289 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000290 Reloc, CPI, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000291}
292
293/// emitJumpTableAddress - Arrange for the address of a jump table to
294/// be emitted to the current location in the function, and allow it to be PC
295/// relative.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000296template<class CodeEmitter>
297void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTIndex,
298 unsigned Reloc) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000299 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000300 Reloc, JTIndex, 0, true));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000301}
302
Raul Herbster9c1a3822007-08-30 23:29:26 +0000303/// emitMachineBasicBlock - Emit the specified address basic block.
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000304template<class CodeEmitter>
305void Emitter<CodeEmitter>::emitMachineBasicBlock(MachineBasicBlock *BB,
306 unsigned Reloc, intptr_t JTBase) {
Raul Herbster9c1a3822007-08-30 23:29:26 +0000307 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Evan Cheng437c1732008-11-07 22:30:53 +0000308 Reloc, BB, JTBase));
Raul Herbster9c1a3822007-08-30 23:29:26 +0000309}
Evan Cheng0ff94f72007-08-07 01:37:15 +0000310
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000311template<class CodeEmitter>
312void Emitter<CodeEmitter>::emitWordLE(unsigned Binary) {
Evan Cheng4df60f52008-11-07 09:06:08 +0000313#ifndef NDEBUG
314 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
315 << Binary << std::dec << "\n";
316#endif
Evan Cheng83b5cf02008-11-05 23:22:34 +0000317 MCE.emitWordLE(Binary);
318}
319
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000320template<class CodeEmitter>
321void Emitter<CodeEmitter>::emitDWordLE(uint64_t Binary) {
Evan Chengcb5201f2008-11-11 22:19:31 +0000322#ifndef NDEBUG
323 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
324 << (unsigned)Binary << std::dec << "\n";
325 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
326 << (unsigned)(Binary >> 32) << std::dec << "\n";
327#endif
328 MCE.emitDWordLE(Binary);
329}
330
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000331template<class CodeEmitter>
332void Emitter<CodeEmitter>::emitInstruction(const MachineInstr &MI) {
Evan Cheng25e04782008-11-04 00:50:32 +0000333 DOUT << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI;
Evan Cheng42d5ee062008-09-13 01:15:21 +0000334
Evan Cheng148b6a42007-07-05 21:15:40 +0000335 NumEmitted++; // Keep track of the # of mi's emitted
Evan Chengedda31c2008-11-05 18:35:52 +0000336 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
Evan Chengffa6d962008-11-13 23:36:57 +0000337 default: {
Evan Chengedda31c2008-11-05 18:35:52 +0000338 assert(0 && "Unhandled instruction encoding format!");
339 break;
Evan Chengffa6d962008-11-13 23:36:57 +0000340 }
Evan Chengedda31c2008-11-05 18:35:52 +0000341 case ARMII::Pseudo:
Evan Cheng057d0c32008-09-18 07:28:19 +0000342 emitPseudoInstruction(MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000343 break;
344 case ARMII::DPFrm:
345 case ARMII::DPSoRegFrm:
346 emitDataProcessingInstruction(MI);
347 break;
Evan Cheng148cad82008-11-13 07:34:59 +0000348 case ARMII::LdFrm:
349 case ARMII::StFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000350 emitLoadStoreInstruction(MI);
351 break;
Evan Cheng148cad82008-11-13 07:34:59 +0000352 case ARMII::LdMiscFrm:
353 case ARMII::StMiscFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000354 emitMiscLoadStoreInstruction(MI);
355 break;
Evan Cheng3c4a4ff2008-11-12 07:18:38 +0000356 case ARMII::LdStMulFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000357 emitLoadStoreMultipleInstruction(MI);
358 break;
Evan Chengfbc9d412008-11-06 01:21:28 +0000359 case ARMII::MulFrm:
360 emitMulFrmInstruction(MI);
Evan Chengedda31c2008-11-05 18:35:52 +0000361 break;
Evan Cheng97f48c32008-11-06 22:15:19 +0000362 case ARMII::ExtFrm:
363 emitExtendInstruction(MI);
364 break;
Evan Cheng8b59db32008-11-07 01:41:35 +0000365 case ARMII::ArithMiscFrm:
366 emitMiscArithInstruction(MI);
367 break;
Evan Cheng12c3a532008-11-06 17:48:05 +0000368 case ARMII::BrFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000369 emitBranchInstruction(MI);
370 break;
Evan Cheng12c3a532008-11-06 17:48:05 +0000371 case ARMII::BrMiscFrm:
Evan Chengedda31c2008-11-05 18:35:52 +0000372 emitMiscBranchInstruction(MI);
373 break;
Evan Cheng96581d32008-11-11 02:11:05 +0000374 // VFP instructions.
375 case ARMII::VFPUnaryFrm:
376 case ARMII::VFPBinaryFrm:
377 emitVFPArithInstruction(MI);
378 break;
Evan Cheng78be83d2008-11-11 19:40:26 +0000379 case ARMII::VFPConv1Frm:
380 case ARMII::VFPConv2Frm:
Evan Cheng0a0ab132008-11-11 22:46:12 +0000381 case ARMII::VFPConv3Frm:
Evan Cheng80a11982008-11-12 06:41:41 +0000382 case ARMII::VFPConv4Frm:
383 case ARMII::VFPConv5Frm:
Evan Cheng78be83d2008-11-11 19:40:26 +0000384 emitVFPConversionInstruction(MI);
385 break;
Evan Chengcd8e66a2008-11-11 21:48:44 +0000386 case ARMII::VFPLdStFrm:
387 emitVFPLoadStoreInstruction(MI);
388 break;
389 case ARMII::VFPLdStMulFrm:
390 emitVFPLoadStoreMultipleInstruction(MI);
391 break;
392 case ARMII::VFPMiscFrm:
393 emitMiscInstruction(MI);
394 break;
Evan Chengedda31c2008-11-05 18:35:52 +0000395 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000396}
397
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000398template<class CodeEmitter>
399void Emitter<CodeEmitter>::emitConstPoolInstruction(const MachineInstr &MI) {
Evan Cheng437c1732008-11-07 22:30:53 +0000400 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
401 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
Evan Cheng938b9d82008-10-31 19:55:13 +0000402 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000403
404 // Remember the CONSTPOOL_ENTRY address for later relocation.
405 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
406
407 // Emit constpool island entry. In most cases, the actual values will be
408 // resolved and relocated after code emission.
409 if (MCPE.isMachineConstantPoolEntry()) {
410 ARMConstantPoolValue *ACPV =
411 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
412
Evan Cheng12c3a532008-11-06 17:48:05 +0000413 DOUT << " ** ARM constant pool #" << CPI << " @ "
Evan Cheng437c1732008-11-07 22:30:53 +0000414 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n';
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000415
416 GlobalValue *GV = ACPV->getGV();
417 if (GV) {
418 assert(!ACPV->isStub() && "Don't know how to deal this yet!");
Evan Chenge96a4902008-11-08 01:31:27 +0000419 if (ACPV->isNonLazyPointer())
Evan Cheng9ed2f802008-11-10 01:08:07 +0000420 MCE.addRelocation(MachineRelocation::getIndirectSymbol(
Evan Chenge96a4902008-11-08 01:31:27 +0000421 MCE.getCurrentPCOffset(), ARM::reloc_arm_machine_cp_entry, GV,
422 (intptr_t)ACPV, false));
423 else
424 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
Evan Cheng35b0bfd2008-11-13 19:22:28 +0000425 ACPV->isStub() || isa<Function>(GV), (intptr_t)ACPV);
Evan Cheng25e04782008-11-04 00:50:32 +0000426 } else {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000427 assert(!ACPV->isNonLazyPointer() && "Don't know how to deal this yet!");
428 emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
429 }
Evan Cheng83b5cf02008-11-05 23:22:34 +0000430 emitWordLE(0);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000431 } else {
432 Constant *CV = MCPE.Val.ConstVal;
433
Evan Cheng35b0bfd2008-11-13 19:22:28 +0000434#ifndef NDEBUG
Evan Cheng12c3a532008-11-06 17:48:05 +0000435 DOUT << " ** Constant pool #" << CPI << " @ "
Evan Cheng35b0bfd2008-11-13 19:22:28 +0000436 << (void*)MCE.getCurrentPCValue() << " ";
437 if (const Function *F = dyn_cast<Function>(CV))
438 DOUT << F->getName();
439 else
440 DOUT << *CV;
441 DOUT << '\n';
442#endif
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000443
444 if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Evan Cheng35b0bfd2008-11-13 19:22:28 +0000445 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV));
Evan Cheng83b5cf02008-11-05 23:22:34 +0000446 emitWordLE(0);
Evan Chengcb5201f2008-11-11 22:19:31 +0000447 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000448 uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
Evan Cheng83b5cf02008-11-05 23:22:34 +0000449 emitWordLE(Val);
Evan Chengcb5201f2008-11-11 22:19:31 +0000450 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
451 if (CFP->getType() == Type::FloatTy)
452 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
453 else if (CFP->getType() == Type::DoubleTy)
454 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
455 else {
456 assert(0 && "Unable to handle this constantpool entry!");
457 abort();
458 }
459 } else {
460 assert(0 && "Unable to handle this constantpool entry!");
461 abort();
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000462 }
463 }
464}
465
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000466template<class CodeEmitter>
467void Emitter<CodeEmitter>::emitMOVi2piecesInstruction(const MachineInstr &MI) {
Evan Cheng90922132008-11-06 02:25:39 +0000468 const MachineOperand &MO0 = MI.getOperand(0);
469 const MachineOperand &MO1 = MI.getOperand(1);
470 assert(MO1.isImm() && "Not a valid so_imm value!");
471 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
472 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
473
474 // Emit the 'mov' instruction.
475 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
476
477 // Set the conditional execution predicate.
Evan Cheng97f48c32008-11-06 22:15:19 +0000478 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng90922132008-11-06 02:25:39 +0000479
480 // Encode Rd.
481 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
482
483 // Encode so_imm.
484 // Set bit I(25) to identify this is the immediate form of <shifter_op>
485 Binary |= 1 << ARMII::I_BitShift;
486 Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V1));
487 emitWordLE(Binary);
488
489 // Now the 'orr' instruction.
490 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
491
492 // Set the conditional execution predicate.
Evan Cheng97f48c32008-11-06 22:15:19 +0000493 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng90922132008-11-06 02:25:39 +0000494
495 // Encode Rd.
496 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
497
498 // Encode Rn.
499 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
500
501 // Encode so_imm.
502 // Set bit I(25) to identify this is the immediate form of <shifter_op>
503 Binary |= 1 << ARMII::I_BitShift;
504 Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V2));
505 emitWordLE(Binary);
506}
507
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000508template<class CodeEmitter>
509void Emitter<CodeEmitter>::emitLEApcrelJTInstruction(const MachineInstr &MI) {
Evan Cheng4df60f52008-11-07 09:06:08 +0000510 // It's basically add r, pc, (LJTI - $+8)
511
512 const TargetInstrDesc &TID = MI.getDesc();
513
514 // Emit the 'add' instruction.
515 unsigned Binary = 0x4 << 21; // add: Insts{24-31} = 0b0100
516
517 // Set the conditional execution predicate
518 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
519
520 // Encode S bit if MI modifies CPSR.
521 Binary |= getAddrModeSBit(MI, TID);
522
523 // Encode Rd.
524 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
525
526 // Encode Rn which is PC.
527 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
528
529 // Encode the displacement.
530 // Set bit I(25) to identify this is the immediate form of <shifter_op>.
531 Binary |= 1 << ARMII::I_BitShift;
532 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
533
534 emitWordLE(Binary);
535}
536
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000537template<class CodeEmitter>
538void Emitter<CodeEmitter>::emitPseudoMoveInstruction(const MachineInstr &MI) {
Evan Chenga9562552008-11-14 20:09:11 +0000539 unsigned Opcode = MI.getDesc().Opcode;
540
541 // Part of binary is determined by TableGn.
542 unsigned Binary = getBinaryCodeForInstr(MI);
543
544 // Set the conditional execution predicate
545 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
546
547 // Encode S bit if MI modifies CPSR.
548 if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag)
549 Binary |= 1 << ARMII::S_BitShift;
550
551 // Encode register def if there is one.
552 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
553
554 // Encode the shift operation.
555 switch (Opcode) {
556 default: break;
557 case ARM::MOVrx:
558 // rrx
559 Binary |= 0x6 << 4;
560 break;
561 case ARM::MOVsrl_flag:
562 // lsr #1
563 Binary |= (0x2 << 4) | (1 << 7);
564 break;
565 case ARM::MOVsra_flag:
566 // asr #1
567 Binary |= (0x4 << 4) | (1 << 7);
568 break;
569 }
570
571 // Encode register Rm.
572 Binary |= getMachineOpValue(MI, 1);
573
574 emitWordLE(Binary);
575}
576
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000577template<class CodeEmitter>
578void Emitter<CodeEmitter>::addPCLabel(unsigned LabelID) {
Evan Cheng12c3a532008-11-06 17:48:05 +0000579 DOUT << " ** LPC" << LabelID << " @ "
Evan Cheng83b5cf02008-11-05 23:22:34 +0000580 << (void*)MCE.getCurrentPCValue() << '\n';
581 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
582}
583
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000584template<class CodeEmitter>
585void Emitter<CodeEmitter>::emitPseudoInstruction(const MachineInstr &MI) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000586 unsigned Opcode = MI.getDesc().Opcode;
587 switch (Opcode) {
588 default:
589 abort(); // FIXME:
Evan Chengffa6d962008-11-13 23:36:57 +0000590 case TargetInstrInfo::INLINEASM: {
Evan Chenge3066ab2008-11-19 23:21:33 +0000591 // We allow inline assembler nodes with empty bodies - they can
592 // implicitly define registers, which is ok for JIT.
593 if (MI.getOperand(0).getSymbolName()[0]) {
594 assert(0 && "JIT does not support inline asm!\n");
595 abort();
596 }
Evan Chengffa6d962008-11-13 23:36:57 +0000597 break;
598 }
599 case TargetInstrInfo::DBG_LABEL:
600 case TargetInstrInfo::EH_LABEL:
601 MCE.emitLabel(MI.getOperand(0).getImm());
602 break;
603 case TargetInstrInfo::IMPLICIT_DEF:
604 case TargetInstrInfo::DECLARE:
605 case ARM::DWARF_LOC:
606 // Do nothing.
607 break;
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000608 case ARM::CONSTPOOL_ENTRY:
609 emitConstPoolInstruction(MI);
610 break;
611 case ARM::PICADD: {
Evan Cheng25e04782008-11-04 00:50:32 +0000612 // Remember of the address of the PC label for relocation later.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000613 addPCLabel(MI.getOperand(2).getImm());
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000614 // PICADD is just an add instruction that implicitly read pc.
Evan Cheng437c1732008-11-07 22:30:53 +0000615 emitDataProcessingInstruction(MI, 0, ARM::PC);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000616 break;
617 }
618 case ARM::PICLDR:
619 case ARM::PICLDRB:
620 case ARM::PICSTR:
621 case ARM::PICSTRB: {
622 // Remember of the address of the PC label for relocation later.
623 addPCLabel(MI.getOperand(2).getImm());
624 // These are just load / store instructions that implicitly read pc.
Evan Cheng4df60f52008-11-07 09:06:08 +0000625 emitLoadStoreInstruction(MI, 0, ARM::PC);
Evan Cheng83b5cf02008-11-05 23:22:34 +0000626 break;
627 }
628 case ARM::PICLDRH:
629 case ARM::PICLDRSH:
630 case ARM::PICLDRSB:
631 case ARM::PICSTRH: {
632 // Remember of the address of the PC label for relocation later.
633 addPCLabel(MI.getOperand(2).getImm());
634 // These are just load / store instructions that implicitly read pc.
635 emitMiscLoadStoreInstruction(MI, ARM::PC);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000636 break;
637 }
Evan Cheng90922132008-11-06 02:25:39 +0000638 case ARM::MOVi2pieces:
639 // Two instructions to materialize a constant.
640 emitMOVi2piecesInstruction(MI);
641 break;
Evan Cheng4df60f52008-11-07 09:06:08 +0000642 case ARM::LEApcrelJT:
643 // Materialize jumptable address.
644 emitLEApcrelJTInstruction(MI);
645 break;
Evan Chenga9562552008-11-14 20:09:11 +0000646 case ARM::MOVrx:
647 case ARM::MOVsrl_flag:
648 case ARM::MOVsra_flag:
649 emitPseudoMoveInstruction(MI);
650 break;
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000651 }
652}
653
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000654template<class CodeEmitter>
655unsigned Emitter<CodeEmitter>::getMachineSoRegOpValue(
656 const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000657 const TargetInstrDesc &TID,
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000658 const MachineOperand &MO,
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000659 unsigned OpIdx) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000660 unsigned Binary = getMachineOpValue(MI, MO);
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000661
662 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
663 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
664 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
665
666 // Encode the shift opcode.
667 unsigned SBits = 0;
668 unsigned Rs = MO1.getReg();
669 if (Rs) {
670 // Set shift operand (bit[7:4]).
671 // LSL - 0001
672 // LSR - 0011
673 // ASR - 0101
674 // ROR - 0111
675 // RRX - 0110 and bit[11:8] clear.
676 switch (SOpc) {
677 default: assert(0 && "Unknown shift opc!");
678 case ARM_AM::lsl: SBits = 0x1; break;
679 case ARM_AM::lsr: SBits = 0x3; break;
680 case ARM_AM::asr: SBits = 0x5; break;
681 case ARM_AM::ror: SBits = 0x7; break;
682 case ARM_AM::rrx: SBits = 0x6; break;
683 }
684 } else {
685 // Set shift operand (bit[6:4]).
686 // LSL - 000
687 // LSR - 010
688 // ASR - 100
689 // ROR - 110
690 switch (SOpc) {
691 default: assert(0 && "Unknown shift opc!");
692 case ARM_AM::lsl: SBits = 0x0; break;
693 case ARM_AM::lsr: SBits = 0x2; break;
694 case ARM_AM::asr: SBits = 0x4; break;
695 case ARM_AM::ror: SBits = 0x6; break;
696 }
697 }
698 Binary |= SBits << 4;
699 if (SOpc == ARM_AM::rrx)
700 return Binary;
701
702 // Encode the shift operation Rs or shift_imm (except rrx).
703 if (Rs) {
704 // Encode Rs bit[11:8].
705 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
706 return Binary |
707 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
708 }
709
710 // Encode shift_imm bit[11:7].
711 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
712}
713
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000714template<class CodeEmitter>
715unsigned Emitter<CodeEmitter>::getMachineSoImmOpValue(unsigned SoImm) {
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000716 // Encode rotate_imm.
Evan Cheng97f48c32008-11-06 22:15:19 +0000717 unsigned Binary = (ARM_AM::getSOImmValRot(SoImm) >> 1)
718 << ARMII::SoRotImmShift;
719
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000720 // Encode immed_8.
Evan Cheng90922132008-11-06 02:25:39 +0000721 Binary |= ARM_AM::getSOImmValImm(SoImm);
Evan Chengeb4ed4b2008-10-31 19:10:44 +0000722 return Binary;
723}
724
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000725template<class CodeEmitter>
726unsigned Emitter<CodeEmitter>::getAddrModeSBit(const MachineInstr &MI,
727 const TargetInstrDesc &TID) const {
Evan Cheng97c573d2008-11-20 02:25:51 +0000728 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
Evan Cheng49a9f292008-09-12 22:45:55 +0000729 const MachineOperand &MO = MI.getOperand(i-1);
Dan Gohmand735b802008-10-03 15:45:36 +0000730 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
Evan Cheng49a9f292008-09-12 22:45:55 +0000731 return 1 << ARMII::S_BitShift;
732 }
733 return 0;
734}
735
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000736template<class CodeEmitter>
737void Emitter<CodeEmitter>::emitDataProcessingInstruction(
738 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 Lopes434dd4f2009-06-01 19:57:37 +0000801template<class CodeEmitter>
802void Emitter<CodeEmitter>::emitLoadStoreInstruction(
803 const MachineInstr &MI,
Evan Cheng4df60f52008-11-07 09:06:08 +0000804 unsigned ImplicitRd,
Evan Cheng83b5cf02008-11-05 23:22:34 +0000805 unsigned ImplicitRn) {
Evan Cheng05c356e2008-11-08 01:44:13 +0000806 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng148cad82008-11-13 07:34:59 +0000807 unsigned Form = TID.TSFlags & ARMII::FormMask;
808 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +0000809
Evan Chengedda31c2008-11-05 18:35:52 +0000810 // Part of binary is determined by TableGn.
811 unsigned Binary = getBinaryCodeForInstr(MI);
812
Jim Grosbach33412622008-10-07 19:05:35 +0000813 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000814 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +0000815
Evan Cheng4df60f52008-11-07 09:06:08 +0000816 unsigned OpIdx = 0;
Evan Cheng148cad82008-11-13 07:34:59 +0000817
818 // Operand 0 of a pre- and post-indexed store is the address base
819 // writeback. Skip it.
820 bool Skipped = false;
821 if (IsPrePost && Form == ARMII::StFrm) {
822 ++OpIdx;
823 Skipped = true;
824 }
825
826 // Set first operand
Evan Cheng4df60f52008-11-07 09:06:08 +0000827 if (ImplicitRd)
828 // Special handling for implicit use (e.g. PC).
829 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
830 << ARMII::RegRdShift);
831 else
832 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000833
834 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +0000835 if (ImplicitRn)
836 // Special handling for implicit use (e.g. PC).
837 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
838 << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +0000839 else
840 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000841
Evan Cheng05c356e2008-11-08 01:44:13 +0000842 // If this is a two-address operand, skip it. e.g. LDR_PRE.
Evan Cheng148cad82008-11-13 07:34:59 +0000843 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +0000844 ++OpIdx;
845
Evan Cheng83b5cf02008-11-05 23:22:34 +0000846 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +0000847 unsigned AM2Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000848 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +0000849
Evan Chenge7de7e32008-09-13 01:44:01 +0000850 // Set bit U(23) according to sign of immed value (positive or negative).
Evan Cheng83b5cf02008-11-05 23:22:34 +0000851 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
Evan Chenge7de7e32008-09-13 01:44:01 +0000852 ARMII::U_BitShift);
Evan Cheng7602e112008-09-02 06:52:38 +0000853 if (!MO2.getReg()) { // is immediate
Evan Cheng83b5cf02008-11-05 23:22:34 +0000854 if (ARM_AM::getAM2Offset(AM2Opc))
Evan Cheng7602e112008-09-02 06:52:38 +0000855 // Set the value of offset_12 field
Evan Cheng83b5cf02008-11-05 23:22:34 +0000856 Binary |= ARM_AM::getAM2Offset(AM2Opc);
857 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +0000858 return;
Evan Cheng7602e112008-09-02 06:52:38 +0000859 }
860
861 // Set bit I(25), because this is not in immediate enconding.
862 Binary |= 1 << ARMII::I_BitShift;
863 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
864 // Set bit[3:0] to the corresponding Rm register
865 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
866
Evan Cheng70632912008-11-12 07:34:37 +0000867 // If this instr is in scaled register offset/index instruction, set
Evan Cheng7602e112008-09-02 06:52:38 +0000868 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
Evan Cheng83b5cf02008-11-05 23:22:34 +0000869 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
Evan Cheng70632912008-11-12 07:34:37 +0000870 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
871 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
Evan Cheng7602e112008-09-02 06:52:38 +0000872 }
873
Evan Cheng83b5cf02008-11-05 23:22:34 +0000874 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000875}
876
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000877template<class CodeEmitter>
878void Emitter<CodeEmitter>::emitMiscLoadStoreInstruction(const MachineInstr &MI,
879 unsigned ImplicitRn) {
Evan Cheng05c356e2008-11-08 01:44:13 +0000880 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng148cad82008-11-13 07:34:59 +0000881 unsigned Form = TID.TSFlags & ARMII::FormMask;
882 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
Evan Cheng05c356e2008-11-08 01:44:13 +0000883
Evan Chengedda31c2008-11-05 18:35:52 +0000884 // Part of binary is determined by TableGn.
885 unsigned Binary = getBinaryCodeForInstr(MI);
886
Jim Grosbach33412622008-10-07 19:05:35 +0000887 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +0000888 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Cheng057d0c32008-09-18 07:28:19 +0000889
Evan Cheng148cad82008-11-13 07:34:59 +0000890 unsigned OpIdx = 0;
891
892 // Operand 0 of a pre- and post-indexed store is the address base
893 // writeback. Skip it.
894 bool Skipped = false;
895 if (IsPrePost && Form == ARMII::StMiscFrm) {
896 ++OpIdx;
897 Skipped = true;
898 }
899
Evan Cheng7602e112008-09-02 06:52:38 +0000900 // Set first operand
Evan Cheng148cad82008-11-13 07:34:59 +0000901 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000902
Evan Cheng358dec52009-06-15 08:28:29 +0000903 // Skip LDRD and STRD's second operand.
904 if (TID.Opcode == ARM::LDRD || TID.Opcode == ARM::STRD)
905 ++OpIdx;
906
Evan Cheng7602e112008-09-02 06:52:38 +0000907 // Set second operand
Evan Cheng83b5cf02008-11-05 23:22:34 +0000908 if (ImplicitRn)
909 // Special handling for implicit use (e.g. PC).
910 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
911 << ARMII::RegRnShift);
Evan Cheng4df60f52008-11-07 09:06:08 +0000912 else
913 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000914
Evan Cheng05c356e2008-11-08 01:44:13 +0000915 // If this is a two-address operand, skip it. e.g. LDRH_POST.
Evan Cheng148cad82008-11-13 07:34:59 +0000916 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
Evan Cheng05c356e2008-11-08 01:44:13 +0000917 ++OpIdx;
918
Evan Cheng83b5cf02008-11-05 23:22:34 +0000919 const MachineOperand &MO2 = MI.getOperand(OpIdx);
Evan Chengd87293c2008-11-06 08:47:38 +0000920 unsigned AM3Opc = (ImplicitRn == ARM::PC)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000921 ? 0 : MI.getOperand(OpIdx+1).getImm();
Evan Cheng7602e112008-09-02 06:52:38 +0000922
Evan Chenge7de7e32008-09-13 01:44:01 +0000923 // Set bit U(23) according to sign of immed value (positive or negative)
Evan Cheng83b5cf02008-11-05 23:22:34 +0000924 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
Evan Cheng7602e112008-09-02 06:52:38 +0000925 ARMII::U_BitShift);
926
927 // If this instr is in register offset/index encoding, set bit[3:0]
928 // to the corresponding Rm register.
929 if (MO2.getReg()) {
930 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
Evan Cheng83b5cf02008-11-05 23:22:34 +0000931 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +0000932 return;
Evan Cheng7602e112008-09-02 06:52:38 +0000933 }
934
Evan Chengd87293c2008-11-06 08:47:38 +0000935 // This instr is in immediate offset/index encoding, set bit 22 to 1.
Evan Cheng97f48c32008-11-06 22:15:19 +0000936 Binary |= 1 << ARMII::AM3_I_BitShift;
Evan Cheng83b5cf02008-11-05 23:22:34 +0000937 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
Evan Cheng7602e112008-09-02 06:52:38 +0000938 // Set operands
Evan Cheng70632912008-11-12 07:34:37 +0000939 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
940 Binary |= (ImmOffs & 0xF); // immedL
Evan Cheng7602e112008-09-02 06:52:38 +0000941 }
942
Evan Cheng83b5cf02008-11-05 23:22:34 +0000943 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000944}
945
Evan Chengcd8e66a2008-11-11 21:48:44 +0000946static unsigned getAddrModeUPBits(unsigned Mode) {
947 unsigned Binary = 0;
Evan Cheng7602e112008-09-02 06:52:38 +0000948
949 // Set addressing mode by modifying bits U(23) and P(24)
950 // IA - Increment after - bit U = 1 and bit P = 0
951 // IB - Increment before - bit U = 1 and bit P = 1
952 // DA - Decrement after - bit U = 0 and bit P = 0
953 // DB - Decrement before - bit U = 0 and bit P = 1
Evan Cheng7602e112008-09-02 06:52:38 +0000954 switch (Mode) {
955 default: assert(0 && "Unknown addressing sub-mode!");
956 case ARM_AM::da: break;
Evan Cheng97f48c32008-11-06 22:15:19 +0000957 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
958 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
959 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
Evan Cheng7602e112008-09-02 06:52:38 +0000960 }
961
Evan Chengcd8e66a2008-11-11 21:48:44 +0000962 return Binary;
963}
964
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000965template<class CodeEmitter>
966void Emitter<CodeEmitter>::emitLoadStoreMultipleInstruction(
967 const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +0000968 // Part of binary is determined by TableGn.
969 unsigned Binary = getBinaryCodeForInstr(MI);
970
971 // Set the conditional execution predicate
972 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
973
974 // Set base address operand
975 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
976
977 // Set addressing mode by modifying bits U(23) and P(24)
978 const MachineOperand &MO = MI.getOperand(1);
979 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
980
Evan Cheng7602e112008-09-02 06:52:38 +0000981 // Set bit W(21)
982 if (ARM_AM::getAM4WBFlag(MO.getImm()))
Evan Cheng97f48c32008-11-06 22:15:19 +0000983 Binary |= 0x1 << ARMII::W_BitShift;
Evan Cheng7602e112008-09-02 06:52:38 +0000984
985 // Set registers
986 for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
987 const MachineOperand &MO = MI.getOperand(i);
Evan Chengcd8e66a2008-11-11 21:48:44 +0000988 if (!MO.isReg() || MO.isImplicit())
989 break;
Evan Cheng7602e112008-09-02 06:52:38 +0000990 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
991 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
992 RegNum < 16);
993 Binary |= 0x1 << RegNum;
994 }
995
Evan Cheng83b5cf02008-11-05 23:22:34 +0000996 emitWordLE(Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000997}
998
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000999template<class CodeEmitter>
1000void Emitter<CodeEmitter>::emitMulFrmInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001001 const TargetInstrDesc &TID = MI.getDesc();
1002
1003 // Part of binary is determined by TableGn.
1004 unsigned Binary = getBinaryCodeForInstr(MI);
1005
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001006 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001007 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001008
1009 // Encode S bit if MI modifies CPSR.
1010 Binary |= getAddrModeSBit(MI, TID);
1011
1012 // 32x32->64bit operations have two destination registers. The number
1013 // of register definitions will tell us if that's what we're dealing with.
Evan Cheng97f48c32008-11-06 22:15:19 +00001014 unsigned OpIdx = 0;
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001015 if (TID.getNumDefs() == 2)
1016 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
1017
1018 // Encode Rd
1019 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
1020
1021 // Encode Rm
1022 Binary |= getMachineOpValue(MI, OpIdx++);
1023
1024 // Encode Rs
1025 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
1026
Evan Chengfbc9d412008-11-06 01:21:28 +00001027 // Many multiple instructions (e.g. MLA) have three src operands. Encode
1028 // it as Rn (for multiply, that's in the same offset as RdLo.
Evan Cheng97f48c32008-11-06 22:15:19 +00001029 if (TID.getNumOperands() > OpIdx &&
1030 !TID.OpInfo[OpIdx].isPredicate() &&
1031 !TID.OpInfo[OpIdx].isOptionalDef())
1032 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
1033
1034 emitWordLE(Binary);
1035}
1036
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001037template<class CodeEmitter>
1038void Emitter<CodeEmitter>::emitExtendInstruction(const MachineInstr &MI) {
Evan Cheng97f48c32008-11-06 22:15:19 +00001039 const TargetInstrDesc &TID = MI.getDesc();
1040
1041 // Part of binary is determined by TableGn.
1042 unsigned Binary = getBinaryCodeForInstr(MI);
1043
1044 // Set the conditional execution predicate
1045 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1046
1047 unsigned OpIdx = 0;
1048
1049 // Encode Rd
1050 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1051
1052 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
1053 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1054 if (MO2.isReg()) {
1055 // Two register operand form.
1056 // Encode Rn.
1057 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
1058
1059 // Encode Rm.
1060 Binary |= getMachineOpValue(MI, MO2);
1061 ++OpIdx;
1062 } else {
1063 Binary |= getMachineOpValue(MI, MO1);
1064 }
1065
1066 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
1067 if (MI.getOperand(OpIdx).isImm() &&
1068 !TID.OpInfo[OpIdx].isPredicate() &&
1069 !TID.OpInfo[OpIdx].isOptionalDef())
1070 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
Evan Chengfbc9d412008-11-06 01:21:28 +00001071
Evan Cheng83b5cf02008-11-05 23:22:34 +00001072 emitWordLE(Binary);
Jim Grosbach0a4b9dc2008-11-03 18:38:31 +00001073}
1074
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001075template<class CodeEmitter>
1076void Emitter<CodeEmitter>::emitMiscArithInstruction(const MachineInstr &MI) {
Evan Cheng8b59db32008-11-07 01:41:35 +00001077 const TargetInstrDesc &TID = MI.getDesc();
1078
1079 // Part of binary is determined by TableGn.
1080 unsigned Binary = getBinaryCodeForInstr(MI);
1081
1082 // Set the conditional execution predicate
1083 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1084
1085 unsigned OpIdx = 0;
1086
1087 // Encode Rd
1088 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1089
1090 const MachineOperand &MO = MI.getOperand(OpIdx++);
1091 if (OpIdx == TID.getNumOperands() ||
1092 TID.OpInfo[OpIdx].isPredicate() ||
1093 TID.OpInfo[OpIdx].isOptionalDef()) {
1094 // Encode Rm and it's done.
1095 Binary |= getMachineOpValue(MI, MO);
1096 emitWordLE(Binary);
1097 return;
1098 }
1099
1100 // Encode Rn.
1101 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1102
1103 // Encode Rm.
1104 Binary |= getMachineOpValue(MI, OpIdx++);
1105
1106 // Encode shift_imm.
1107 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
1108 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1109 Binary |= ShiftAmt << ARMII::ShiftShift;
1110
1111 emitWordLE(Binary);
1112}
1113
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001114template<class CodeEmitter>
1115void Emitter<CodeEmitter>::emitBranchInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001116 const TargetInstrDesc &TID = MI.getDesc();
1117
Evan Cheng12c3a532008-11-06 17:48:05 +00001118 if (TID.Opcode == ARM::TPsoft)
1119 abort(); // FIXME
1120
Evan Cheng7602e112008-09-02 06:52:38 +00001121 // Part of binary is determined by TableGn.
1122 unsigned Binary = getBinaryCodeForInstr(MI);
1123
Evan Chengedda31c2008-11-05 18:35:52 +00001124 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001125 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001126
1127 // Set signed_immed_24 field
1128 Binary |= getMachineOpValue(MI, 0);
1129
Evan Cheng83b5cf02008-11-05 23:22:34 +00001130 emitWordLE(Binary);
Evan Chengedda31c2008-11-05 18:35:52 +00001131}
1132
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001133template<class CodeEmitter>
1134void Emitter<CodeEmitter>::emitInlineJumpTable(unsigned JTIndex) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001135 // Remember the base address of the inline jump table.
Evan Cheng5788d1a2008-12-10 02:32:19 +00001136 uintptr_t JTBase = MCE.getCurrentPCValue();
Evan Cheng437c1732008-11-07 22:30:53 +00001137 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
1138 DOUT << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase << '\n';
Evan Cheng4df60f52008-11-07 09:06:08 +00001139
1140 // Now emit the jump table entries.
1141 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1142 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1143 if (IsPIC)
1144 // DestBB address - JT base.
Evan Cheng437c1732008-11-07 22:30:53 +00001145 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
Evan Cheng4df60f52008-11-07 09:06:08 +00001146 else
1147 // Absolute DestBB address.
1148 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1149 emitWordLE(0);
1150 }
1151}
1152
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001153template<class CodeEmitter>
1154void Emitter<CodeEmitter>::emitMiscBranchInstruction(const MachineInstr &MI) {
Evan Chengedda31c2008-11-05 18:35:52 +00001155 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengedda31c2008-11-05 18:35:52 +00001156
Evan Cheng437c1732008-11-07 22:30:53 +00001157 // Handle jump tables.
1158 if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
1159 // First emit a ldr pc, [] instruction.
1160 emitDataProcessingInstruction(MI, ARM::PC);
1161
1162 // Then emit the inline jump table.
1163 unsigned JTIndex = (TID.Opcode == ARM::BR_JTr)
1164 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1165 emitInlineJumpTable(JTIndex);
1166 return;
1167 } else if (TID.Opcode == ARM::BR_JTm) {
Evan Cheng4df60f52008-11-07 09:06:08 +00001168 // First emit a ldr pc, [] instruction.
1169 emitLoadStoreInstruction(MI, ARM::PC);
1170
1171 // Then emit the inline jump table.
Evan Cheng437c1732008-11-07 22:30:53 +00001172 emitInlineJumpTable(MI.getOperand(3).getIndex());
Evan Cheng4df60f52008-11-07 09:06:08 +00001173 return;
1174 }
1175
Evan Chengedda31c2008-11-05 18:35:52 +00001176 // Part of binary is determined by TableGn.
1177 unsigned Binary = getBinaryCodeForInstr(MI);
1178
1179 // Set the conditional execution predicate
Evan Cheng97f48c32008-11-06 22:15:19 +00001180 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
Evan Chengedda31c2008-11-05 18:35:52 +00001181
1182 if (TID.Opcode == ARM::BX_RET)
1183 // The return register is LR.
1184 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
1185 else
1186 // otherwise, set the return register
1187 Binary |= getMachineOpValue(MI, 0);
1188
Evan Cheng83b5cf02008-11-05 23:22:34 +00001189 emitWordLE(Binary);
Evan Cheng148b6a42007-07-05 21:15:40 +00001190}
Evan Cheng7602e112008-09-02 06:52:38 +00001191
Evan Cheng80a11982008-11-12 06:41:41 +00001192static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) {
Evan Chengd06d48d2008-11-12 02:19:38 +00001193 unsigned RegD = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001194 unsigned Binary = 0;
Evan Chengd06d48d2008-11-12 02:19:38 +00001195 bool isSPVFP = false;
1196 RegD = ARMRegisterInfo::getRegisterNumbering(RegD, isSPVFP);
1197 if (!isSPVFP)
1198 Binary |= RegD << ARMII::RegRdShift;
1199 else {
1200 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1201 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1202 }
Evan Cheng80a11982008-11-12 06:41:41 +00001203 return Binary;
1204}
Evan Cheng78be83d2008-11-11 19:40:26 +00001205
Evan Cheng80a11982008-11-12 06:41:41 +00001206static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) {
Evan Chengd06d48d2008-11-12 02:19:38 +00001207 unsigned RegN = MI.getOperand(OpIdx).getReg();
Evan Cheng80a11982008-11-12 06:41:41 +00001208 unsigned Binary = 0;
1209 bool isSPVFP = false;
Evan Chengd06d48d2008-11-12 02:19:38 +00001210 RegN = ARMRegisterInfo::getRegisterNumbering(RegN, isSPVFP);
1211 if (!isSPVFP)
1212 Binary |= RegN << ARMII::RegRnShift;
1213 else {
1214 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1215 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1216 }
Evan Cheng80a11982008-11-12 06:41:41 +00001217 return Binary;
1218}
Evan Chengd06d48d2008-11-12 02:19:38 +00001219
Evan Cheng80a11982008-11-12 06:41:41 +00001220static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) {
1221 unsigned RegM = MI.getOperand(OpIdx).getReg();
1222 unsigned Binary = 0;
1223 bool isSPVFP = false;
1224 RegM = ARMRegisterInfo::getRegisterNumbering(RegM, isSPVFP);
1225 if (!isSPVFP)
1226 Binary |= RegM;
1227 else {
1228 Binary |= ((RegM & 0x1E) >> 1);
1229 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
Evan Cheng78be83d2008-11-11 19:40:26 +00001230 }
Evan Cheng80a11982008-11-12 06:41:41 +00001231 return Binary;
1232}
1233
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001234template<class CodeEmitter>
1235void Emitter<CodeEmitter>::emitVFPArithInstruction(const MachineInstr &MI) {
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001236 const TargetInstrDesc &TID = MI.getDesc();
1237
1238 // Part of binary is determined by TableGn.
1239 unsigned Binary = getBinaryCodeForInstr(MI);
1240
1241 // Set the conditional execution predicate
1242 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1243
1244 unsigned OpIdx = 0;
1245 assert((Binary & ARMII::D_BitShift) == 0 &&
1246 (Binary & ARMII::N_BitShift) == 0 &&
1247 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1248
1249 // Encode Dd / Sd.
1250 Binary |= encodeVFPRd(MI, OpIdx++);
1251
1252 // If this is a two-address operand, skip it, e.g. FMACD.
1253 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1254 ++OpIdx;
1255
1256 // Encode Dn / Sn.
1257 if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
Evan Cheng3f4924e2008-11-12 08:14:21 +00001258 Binary |= encodeVFPRn(MI, OpIdx++);
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001259
1260 if (OpIdx == TID.getNumOperands() ||
1261 TID.OpInfo[OpIdx].isPredicate() ||
1262 TID.OpInfo[OpIdx].isOptionalDef()) {
1263 // FCMPEZD etc. has only one operand.
1264 emitWordLE(Binary);
1265 return;
1266 }
1267
1268 // Encode Dm / Sm.
1269 Binary |= encodeVFPRm(MI, OpIdx);
1270
1271 emitWordLE(Binary);
1272}
1273
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001274template<class CodeEmitter>
1275void Emitter<CodeEmitter>::emitVFPConversionInstruction(
1276 const MachineInstr &MI) {
Evan Cheng80a11982008-11-12 06:41:41 +00001277 const TargetInstrDesc &TID = MI.getDesc();
1278 unsigned Form = TID.TSFlags & ARMII::FormMask;
1279
1280 // Part of binary is determined by TableGn.
1281 unsigned Binary = getBinaryCodeForInstr(MI);
1282
1283 // Set the conditional execution predicate
1284 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1285
1286 switch (Form) {
1287 default: break;
1288 case ARMII::VFPConv1Frm:
1289 case ARMII::VFPConv2Frm:
1290 case ARMII::VFPConv3Frm:
1291 // Encode Dd / Sd.
1292 Binary |= encodeVFPRd(MI, 0);
1293 break;
1294 case ARMII::VFPConv4Frm:
1295 // Encode Dn / Sn.
1296 Binary |= encodeVFPRn(MI, 0);
1297 break;
1298 case ARMII::VFPConv5Frm:
1299 // Encode Dm / Sm.
1300 Binary |= encodeVFPRm(MI, 0);
1301 break;
1302 }
1303
1304 switch (Form) {
1305 default: break;
1306 case ARMII::VFPConv1Frm:
1307 // Encode Dm / Sm.
1308 Binary |= encodeVFPRm(MI, 1);
Evan Cheng67fd91f2008-11-13 07:46:59 +00001309 break;
Evan Cheng80a11982008-11-12 06:41:41 +00001310 case ARMII::VFPConv2Frm:
1311 case ARMII::VFPConv3Frm:
1312 // Encode Dn / Sn.
1313 Binary |= encodeVFPRn(MI, 1);
1314 break;
1315 case ARMII::VFPConv4Frm:
1316 case ARMII::VFPConv5Frm:
1317 // Encode Dd / Sd.
1318 Binary |= encodeVFPRd(MI, 1);
1319 break;
1320 }
1321
1322 if (Form == ARMII::VFPConv5Frm)
1323 // Encode Dn / Sn.
1324 Binary |= encodeVFPRn(MI, 2);
1325 else if (Form == ARMII::VFPConv3Frm)
1326 // Encode Dm / Sm.
1327 Binary |= encodeVFPRm(MI, 2);
Evan Cheng78be83d2008-11-11 19:40:26 +00001328
1329 emitWordLE(Binary);
1330}
1331
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001332template<class CodeEmitter>
1333void Emitter<CodeEmitter>::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001334 // Part of binary is determined by TableGn.
1335 unsigned Binary = getBinaryCodeForInstr(MI);
1336
1337 // Set the conditional execution predicate
1338 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1339
1340 unsigned OpIdx = 0;
1341
1342 // Encode Dd / Sd.
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001343 Binary |= encodeVFPRd(MI, OpIdx++);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001344
1345 // Encode address base.
1346 const MachineOperand &Base = MI.getOperand(OpIdx++);
1347 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1348
1349 // If there is a non-zero immediate offset, encode it.
1350 if (Base.isReg()) {
1351 const MachineOperand &Offset = MI.getOperand(OpIdx);
1352 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1353 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1354 Binary |= 1 << ARMII::U_BitShift;
Evan Cheng607f1b42008-11-12 08:21:12 +00001355 Binary |= ImmOffs;
Evan Chengcd8e66a2008-11-11 21:48:44 +00001356 emitWordLE(Binary);
1357 return;
1358 }
1359 }
1360
1361 // If immediate offset is omitted, default to +0.
1362 Binary |= 1 << ARMII::U_BitShift;
1363
1364 emitWordLE(Binary);
1365}
1366
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001367template<class CodeEmitter>
1368void Emitter<CodeEmitter>::emitVFPLoadStoreMultipleInstruction(
1369 const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001370 // Part of binary is determined by TableGn.
1371 unsigned Binary = getBinaryCodeForInstr(MI);
1372
1373 // Set the conditional execution predicate
1374 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1375
1376 // Set base address operand
1377 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
1378
1379 // Set addressing mode by modifying bits U(23) and P(24)
1380 const MachineOperand &MO = MI.getOperand(1);
1381 Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm()));
1382
1383 // Set bit W(21)
1384 if (ARM_AM::getAM5WBFlag(MO.getImm()))
1385 Binary |= 0x1 << ARMII::W_BitShift;
1386
1387 // First register is encoded in Dd.
Evan Cheng3c4a4ff2008-11-12 07:18:38 +00001388 Binary |= encodeVFPRd(MI, 4);
Evan Chengcd8e66a2008-11-11 21:48:44 +00001389
1390 // Number of registers are encoded in offset field.
1391 unsigned NumRegs = 1;
1392 for (unsigned i = 5, e = MI.getNumOperands(); i != e; ++i) {
1393 const MachineOperand &MO = MI.getOperand(i);
1394 if (!MO.isReg() || MO.isImplicit())
1395 break;
1396 ++NumRegs;
1397 }
1398 Binary |= NumRegs * 2;
1399
1400 emitWordLE(Binary);
1401}
1402
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +00001403template<class CodeEmitter>
1404void Emitter<CodeEmitter>::emitMiscInstruction(const MachineInstr &MI) {
Evan Chengcd8e66a2008-11-11 21:48:44 +00001405 // Part of binary is determined by TableGn.
1406 unsigned Binary = getBinaryCodeForInstr(MI);
1407
1408 // Set the conditional execution predicate
1409 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1410
1411 emitWordLE(Binary);
1412}
1413
Evan Cheng7602e112008-09-02 06:52:38 +00001414#include "ARMGenCodeEmitter.inc"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +00001415