blob: 641e1a1205f19bd8af254ef75c33c5da2c0eae81 [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
15#define DEBUG_TYPE "arm-emitter"
Evan Cheng7602e112008-09-02 06:52:38 +000016#include "ARM.h"
17#include "ARMAddressingModes.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000018#include "ARMInstrInfo.h"
Evan Cheng7602e112008-09-02 06:52:38 +000019#include "ARMRelocations.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Jim Grosbachbc6d8762008-10-28 18:25:49 +000022#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
Evan Cheng42d5ee062008-09-13 01:15:21 +000024#include "llvm/Function.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000025#include "llvm/PassManager.h"
26#include "llvm/CodeGen/MachineCodeEmitter.h"
Evan Cheng057d0c32008-09-18 07:28:19 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineInstr.h"
30#include "llvm/CodeGen/Passes.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000031#include "llvm/ADT/Statistic.h"
32#include "llvm/Support/Compiler.h"
Evan Cheng42d5ee062008-09-13 01:15:21 +000033#include "llvm/Support/Debug.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000034using namespace llvm;
35
36STATISTIC(NumEmitted, "Number of machine instructions emitted");
37
38namespace {
Evan Cheng7602e112008-09-02 06:52:38 +000039 class VISIBILITY_HIDDEN ARMCodeEmitter : public MachineFunctionPass {
Evan Cheng057d0c32008-09-18 07:28:19 +000040 ARMJITInfo *JTI;
41 const ARMInstrInfo *II;
42 const TargetData *TD;
43 TargetMachine &TM;
44 MachineCodeEmitter &MCE;
45 const MachineConstantPool *MCP;
Evan Cheng148b6a42007-07-05 21:15:40 +000046 public:
47 static char ID;
Evan Cheng7602e112008-09-02 06:52:38 +000048 explicit ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce)
Evan Cheng057d0c32008-09-18 07:28:19 +000049 : MachineFunctionPass(&ID), JTI(0), II(0), TD(0), TM(tm),
50 MCE(mce), MCP(0) {}
Evan Cheng7602e112008-09-02 06:52:38 +000051 ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce,
Evan Cheng148b6a42007-07-05 21:15:40 +000052 const ARMInstrInfo &ii, const TargetData &td)
Evan Cheng057d0c32008-09-18 07:28:19 +000053 : MachineFunctionPass(&ID), JTI(0), II(&ii), TD(&td), TM(tm),
54 MCE(mce), MCP(0) {}
Evan Cheng148b6a42007-07-05 21:15:40 +000055
56 bool runOnMachineFunction(MachineFunction &MF);
57
58 virtual const char *getPassName() const {
59 return "ARM Machine Code Emitter";
60 }
61
62 void emitInstruction(const MachineInstr &MI);
Evan Cheng7602e112008-09-02 06:52:38 +000063
64 private:
Evan Cheng057d0c32008-09-18 07:28:19 +000065
66 void emitConstPoolInstruction(const MachineInstr &MI);
67
68 void emitPseudoInstruction(const MachineInstr &MI);
69
Evan Cheng7602e112008-09-02 06:52:38 +000070 unsigned getAddrModeNoneInstrBinary(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +000071 const TargetInstrDesc &TID,
Evan Cheng5f1db7b2008-09-12 22:01:15 +000072 unsigned Binary);
73
74 unsigned getMachineSoRegOpValue(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +000075 const TargetInstrDesc &TID,
Evan Cheng5f1db7b2008-09-12 22:01:15 +000076 unsigned OpIdx);
77
Evan Cheng49a9f292008-09-12 22:45:55 +000078 unsigned getAddrMode1SBit(const MachineInstr &MI,
79 const TargetInstrDesc &TID) const;
80
Evan Cheng7602e112008-09-02 06:52:38 +000081 unsigned getAddrMode1InstrBinary(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +000082 const TargetInstrDesc &TID,
Evan Cheng7602e112008-09-02 06:52:38 +000083 unsigned Binary);
84 unsigned getAddrMode2InstrBinary(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +000085 const TargetInstrDesc &TID,
Evan Cheng7602e112008-09-02 06:52:38 +000086 unsigned Binary);
87 unsigned getAddrMode3InstrBinary(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +000088 const TargetInstrDesc &TID,
Evan Cheng7602e112008-09-02 06:52:38 +000089 unsigned Binary);
90 unsigned getAddrMode4InstrBinary(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +000091 const TargetInstrDesc &TID,
Evan Cheng7602e112008-09-02 06:52:38 +000092 unsigned Binary);
93
94 /// getInstrBinary - Return binary encoding for the specified
95 /// machine instruction.
96 unsigned getInstrBinary(const MachineInstr &MI);
97
98 /// getBinaryCodeForInstr - This function, generated by the
99 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
100 /// machine instructions.
101 ///
Raul Herbster9c1a3822007-08-30 23:29:26 +0000102 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000103
Evan Cheng7602e112008-09-02 06:52:38 +0000104 /// getMachineOpValue - Return binary encoding of operand. If the machine
105 /// operand requires relocation, record the relocation and return zero.
106 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) {
107 return getMachineOpValue(MI, MI.getOperand(OpIdx));
108 }
109 unsigned getMachineOpValue(const MachineInstr &MI,
110 const MachineOperand &MO);
111
112 /// getBaseOpcodeFor - Return the opcode value.
113 ///
114 unsigned getBaseOpcodeFor(const TargetInstrDesc &TID) const {
115 return (TID.TSFlags & ARMII::OpcodeMask) >> ARMII::OpcodeShift;
116 }
117
118 /// getShiftOp - Return the shift opcode (bit[6:5]) of the machine operand.
119 ///
120 unsigned getShiftOp(const MachineOperand &MO) const ;
121
122 /// Routines that handle operands which add machine relocations which are
123 /// fixed up by the JIT fixup stage.
Evan Cheng057d0c32008-09-18 07:28:19 +0000124 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
Jim Grosbach016d34c2008-10-03 15:52:42 +0000125 bool NeedStub);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000126 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
127 void emitConstPoolAddress(unsigned CPI, unsigned Reloc,
128 int Disp = 0, unsigned PCAdj = 0 );
Evan Cheng057d0c32008-09-18 07:28:19 +0000129 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc,
Evan Cheng0ff94f72007-08-07 01:37:15 +0000130 unsigned PCAdj = 0);
Raul Herbster9c1a3822007-08-30 23:29:26 +0000131 void emitGlobalConstant(const Constant *CV);
132 void emitMachineBasicBlock(MachineBasicBlock *BB);
Evan Cheng148b6a42007-07-05 21:15:40 +0000133 };
Evan Cheng7602e112008-09-02 06:52:38 +0000134 char ARMCodeEmitter::ID = 0;
Evan Cheng148b6a42007-07-05 21:15:40 +0000135}
136
137/// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
138/// to the specified MCE object.
139FunctionPass *llvm::createARMCodeEmitterPass(ARMTargetMachine &TM,
140 MachineCodeEmitter &MCE) {
Evan Cheng7602e112008-09-02 06:52:38 +0000141 return new ARMCodeEmitter(TM, MCE);
Evan Cheng148b6a42007-07-05 21:15:40 +0000142}
143
Evan Cheng7602e112008-09-02 06:52:38 +0000144bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000145 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
146 MF.getTarget().getRelocationModel() != Reloc::Static) &&
147 "JIT relocation model must be set to static or default!");
148 II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo();
149 TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData();
Evan Cheng057d0c32008-09-18 07:28:19 +0000150 JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo();
151 MCP = MF.getConstantPool();
Evan Cheng148b6a42007-07-05 21:15:40 +0000152
153 do {
Evan Cheng42d5ee062008-09-13 01:15:21 +0000154 DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
Evan Cheng148b6a42007-07-05 21:15:40 +0000155 MCE.startFunction(MF);
156 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
157 MBB != E; ++MBB) {
158 MCE.StartMachineBasicBlock(MBB);
159 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
160 I != E; ++I)
161 emitInstruction(*I);
162 }
163 } while (MCE.finishFunction(MF));
164
165 return false;
166}
167
Evan Cheng7602e112008-09-02 06:52:38 +0000168/// getShiftOp - Return the shift opcode (bit[6:5]) of the machine operand.
169///
170unsigned ARMCodeEmitter::getShiftOp(const MachineOperand &MO) const {
171 switch (ARM_AM::getAM2ShiftOpc(MO.getImm())) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000172 default: assert(0 && "Unknown shift opc!");
Evan Cheng7602e112008-09-02 06:52:38 +0000173 case ARM_AM::asr: return 2;
174 case ARM_AM::lsl: return 0;
175 case ARM_AM::lsr: return 1;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000176 case ARM_AM::ror:
Evan Cheng7602e112008-09-02 06:52:38 +0000177 case ARM_AM::rrx: return 3;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000178 }
Evan Cheng7602e112008-09-02 06:52:38 +0000179 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000180}
181
Evan Cheng7602e112008-09-02 06:52:38 +0000182/// getMachineOpValue - Return binary encoding of operand. If the machine
183/// operand requires relocation, record the relocation and return zero.
184unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
185 const MachineOperand &MO) {
Dan Gohmand735b802008-10-03 15:45:36 +0000186 if (MO.isReg())
Evan Cheng7602e112008-09-02 06:52:38 +0000187 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000188 else if (MO.isImm())
Evan Cheng7602e112008-09-02 06:52:38 +0000189 return static_cast<unsigned>(MO.getImm());
Dan Gohmand735b802008-10-03 15:45:36 +0000190 else if (MO.isGlobal())
Jim Grosbach016d34c2008-10-03 15:52:42 +0000191 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true);
Dan Gohmand735b802008-10-03 15:45:36 +0000192 else if (MO.isSymbol())
Raul Herbster9c1a3822007-08-30 23:29:26 +0000193 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_relative);
Dan Gohmand735b802008-10-03 15:45:36 +0000194 else if (MO.isCPI())
Chris Lattner8aa797a2007-12-30 23:10:15 +0000195 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_relative);
Dan Gohmand735b802008-10-03 15:45:36 +0000196 else if (MO.isJTI())
Chris Lattner8aa797a2007-12-30 23:10:15 +0000197 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
Dan Gohmand735b802008-10-03 15:45:36 +0000198 else if (MO.isMBB())
Chris Lattner8aa797a2007-12-30 23:10:15 +0000199 emitMachineBasicBlock(MO.getMBB());
Evan Cheng2aa0e642008-09-13 01:55:59 +0000200 else {
201 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
202 abort();
203 }
Evan Cheng7602e112008-09-02 06:52:38 +0000204 return 0;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000205}
206
Evan Cheng057d0c32008-09-18 07:28:19 +0000207/// emitGlobalAddress - Emit the specified address to the code stream.
Evan Cheng0ff94f72007-08-07 01:37:15 +0000208///
Evan Cheng057d0c32008-09-18 07:28:19 +0000209void ARMCodeEmitter::emitGlobalAddress(GlobalValue *GV,
Jim Grosbach016d34c2008-10-03 15:52:42 +0000210 unsigned Reloc, bool NeedStub) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000211 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
Jim Grosbach016d34c2008-10-03 15:52:42 +0000212 Reloc, GV, 0, NeedStub));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000213}
214
215/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
216/// be emitted to the current location in the function, and allow it to be PC
217/// relative.
Evan Cheng7602e112008-09-02 06:52:38 +0000218void ARMCodeEmitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000219 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
220 Reloc, ES));
221}
222
223/// emitConstPoolAddress - Arrange for the address of an constant pool
224/// to be emitted to the current location in the function, and allow it to be PC
225/// relative.
Evan Cheng7602e112008-09-02 06:52:38 +0000226void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
227 int Disp /* = 0 */,
228 unsigned PCAdj /* = 0 */) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000229 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
230 Reloc, CPI, PCAdj));
231}
232
233/// emitJumpTableAddress - Arrange for the address of a jump table to
234/// be emitted to the current location in the function, and allow it to be PC
235/// relative.
Evan Cheng057d0c32008-09-18 07:28:19 +0000236void ARMCodeEmitter::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc,
Evan Cheng7602e112008-09-02 06:52:38 +0000237 unsigned PCAdj /* = 0 */) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000238 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng057d0c32008-09-18 07:28:19 +0000239 Reloc, JTIndex, PCAdj));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000240}
241
Raul Herbster9c1a3822007-08-30 23:29:26 +0000242/// emitMachineBasicBlock - Emit the specified address basic block.
Evan Cheng7602e112008-09-02 06:52:38 +0000243void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB) {
Raul Herbster9c1a3822007-08-30 23:29:26 +0000244 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Evan Cheng7602e112008-09-02 06:52:38 +0000245 ARM::reloc_arm_branch, BB));
Raul Herbster9c1a3822007-08-30 23:29:26 +0000246}
Evan Cheng0ff94f72007-08-07 01:37:15 +0000247
Evan Cheng7602e112008-09-02 06:52:38 +0000248void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
Evan Cheng42d5ee062008-09-13 01:15:21 +0000249 DOUT << MI;
250
Evan Cheng148b6a42007-07-05 21:15:40 +0000251 NumEmitted++; // Keep track of the # of mi's emitted
Evan Cheng057d0c32008-09-18 07:28:19 +0000252 if ((MI.getDesc().TSFlags & ARMII::FormMask) == ARMII::Pseudo)
253 emitPseudoInstruction(MI);
254 else
255 MCE.emitWordLE(getInstrBinary(MI));
Evan Cheng0ff94f72007-08-07 01:37:15 +0000256}
257
Evan Cheng7602e112008-09-02 06:52:38 +0000258unsigned ARMCodeEmitter::getAddrModeNoneInstrBinary(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000259 const TargetInstrDesc &TID,
Evan Cheng7602e112008-09-02 06:52:38 +0000260 unsigned Binary) {
Jim Grosbach33412622008-10-07 19:05:35 +0000261 // Set the conditional execution predicate
262 Binary |= II->getPredicate(&MI) << 28;
Evan Cheng057d0c32008-09-18 07:28:19 +0000263
Evan Cheng49a9f292008-09-12 22:45:55 +0000264 switch (TID.TSFlags & ARMII::FormMask) {
Evan Cheng7602e112008-09-02 06:52:38 +0000265 default:
266 assert(0 && "Unknown instruction subtype!");
267 break;
268 case ARMII::Branch: {
269 // Set signed_immed_24 field
270 Binary |= getMachineOpValue(MI, 0);
271
272 // if it is a conditional branch, set cond field
Evan Cheng49a9f292008-09-12 22:45:55 +0000273 if (TID.Opcode == ARM::Bcc) {
Evan Cheng7602e112008-09-02 06:52:38 +0000274 Binary &= 0x0FFFFFFF; // clear conditional field
275 Binary |= getMachineOpValue(MI, 1) << 28; // set conditional field
276 }
277 break;
278 }
279 case ARMII::BranchMisc: {
Evan Cheng7fd7ca42008-09-17 07:53:38 +0000280 if (TID.Opcode == ARM::BX)
281 abort(); // FIXME
Evan Cheng49a9f292008-09-12 22:45:55 +0000282 if (TID.Opcode == ARM::BX_RET)
Evan Cheng7602e112008-09-02 06:52:38 +0000283 Binary |= 0xe; // the return register is LR
284 else
285 // otherwise, set the return register
286 Binary |= getMachineOpValue(MI, 0);
287 break;
288 }
289 }
290
291 return Binary;
292}
293
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000294unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000295 const TargetInstrDesc &TID,
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000296 unsigned OpIdx) {
297 // Set last operand (register Rm)
298 unsigned Binary = getMachineOpValue(MI, OpIdx);
299
300 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
301 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
302 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
303
304 // Encode the shift opcode.
305 unsigned SBits = 0;
306 unsigned Rs = MO1.getReg();
307 if (Rs) {
308 // Set shift operand (bit[7:4]).
309 // LSL - 0001
310 // LSR - 0011
311 // ASR - 0101
312 // ROR - 0111
313 // RRX - 0110 and bit[11:8] clear.
314 switch (SOpc) {
315 default: assert(0 && "Unknown shift opc!");
316 case ARM_AM::lsl: SBits = 0x1; break;
317 case ARM_AM::lsr: SBits = 0x3; break;
318 case ARM_AM::asr: SBits = 0x5; break;
319 case ARM_AM::ror: SBits = 0x7; break;
320 case ARM_AM::rrx: SBits = 0x6; break;
321 }
322 } else {
323 // Set shift operand (bit[6:4]).
324 // LSL - 000
325 // LSR - 010
326 // ASR - 100
327 // ROR - 110
328 switch (SOpc) {
329 default: assert(0 && "Unknown shift opc!");
330 case ARM_AM::lsl: SBits = 0x0; break;
331 case ARM_AM::lsr: SBits = 0x2; break;
332 case ARM_AM::asr: SBits = 0x4; break;
333 case ARM_AM::ror: SBits = 0x6; break;
334 }
335 }
336 Binary |= SBits << 4;
337 if (SOpc == ARM_AM::rrx)
338 return Binary;
339
340 // Encode the shift operation Rs or shift_imm (except rrx).
341 if (Rs) {
342 // Encode Rs bit[11:8].
343 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
344 return Binary |
345 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
346 }
347
348 // Encode shift_imm bit[11:7].
349 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
350}
351
Evan Cheng49a9f292008-09-12 22:45:55 +0000352unsigned ARMCodeEmitter::getAddrMode1SBit(const MachineInstr &MI,
353 const TargetInstrDesc &TID) const {
354 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
355 const MachineOperand &MO = MI.getOperand(i-1);
Dan Gohmand735b802008-10-03 15:45:36 +0000356 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
Evan Cheng49a9f292008-09-12 22:45:55 +0000357 return 1 << ARMII::S_BitShift;
358 }
359 return 0;
360}
361
Evan Cheng057d0c32008-09-18 07:28:19 +0000362void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
Jim Grosbachbc6d8762008-10-28 18:25:49 +0000363 unsigned CPID = MI.getOperand(0).getImm();
364 unsigned CPIndex = MI.getOperand(1).getIndex();
365 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIndex];
366
367 //FIXME: Can we get these here?
368 assert (!MCPE.isMachineConstantPoolEntry());
369
370 const Constant *CV = MCPE.Val.ConstVal;
371 // FIXME: We can get other types here. Need to handle them.
372 // According to the constant island pass, everything is multiples,
373 // of 4-bytes in size, though, so that helps.
374 assert (CV->getType()->isInteger());
375 assert (cast<IntegerType>(CV->getType())->getBitWidth() == 32);
376
377 const ConstantInt *CI = dyn_cast<ConstantInt>(CV);
378 uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
379
380 DOUT << "Constant pool #" << CPID << ", value '" << Val << "' @ " <<
381 (void*)MCE.getCurrentPCValue() << "\n";
382
383 if (JTI)
384 JTI->mapCPIDtoAddress(CPID, MCE.getCurrentPCValue());
385 MCE.emitWordLE(Val);
Evan Cheng057d0c32008-09-18 07:28:19 +0000386}
387
388void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
389 unsigned Opcode = MI.getDesc().Opcode;
390 switch (Opcode) {
391 default:
392 abort(); // FIXME:
393 case ARM::CONSTPOOL_ENTRY: {
394 emitConstPoolInstruction(MI);
395 break;
396 }
397 }
398}
399
Evan Cheng7602e112008-09-02 06:52:38 +0000400unsigned ARMCodeEmitter::getAddrMode1InstrBinary(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000401 const TargetInstrDesc &TID,
Evan Cheng7602e112008-09-02 06:52:38 +0000402 unsigned Binary) {
Jim Grosbach33412622008-10-07 19:05:35 +0000403 // Set the conditional execution predicate
404 Binary |= II->getPredicate(&MI) << 28;
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000405
Evan Cheng49a9f292008-09-12 22:45:55 +0000406 // Encode S bit if MI modifies CPSR.
407 Binary |= getAddrMode1SBit(MI, TID);
408
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000409 // Encode register def if there is one.
Evan Cheng49a9f292008-09-12 22:45:55 +0000410 unsigned NumDefs = TID.getNumDefs();
Evan Chenga964b7d2008-09-12 23:15:39 +0000411 unsigned OpIdx = 0;
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000412 if (NumDefs) {
413 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdShift;
414 ++OpIdx;
Evan Cheng7602e112008-09-02 06:52:38 +0000415 }
416
Jim Grosbachefd30ba2008-10-01 18:16:49 +0000417 // Encode first non-shifter register operand if there is one.
Evan Cheng057d0c32008-09-18 07:28:19 +0000418 unsigned Format = TID.TSFlags & ARMII::FormMask;
Jim Grosbach9e729a22008-10-07 17:42:09 +0000419 bool hasRnOperand= !(Format == ARMII::DPRdMisc ||
420 Format == ARMII::DPRdIm ||
421 Format == ARMII::DPRdReg ||
422 Format == ARMII::DPRdSoReg);
423 if (hasRnOperand) {
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000424 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
425 ++OpIdx;
Evan Cheng7602e112008-09-02 06:52:38 +0000426 }
427
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000428 // Encode shifter operand.
Evan Chengbe3034c2008-09-13 01:38:29 +0000429 bool HasSoReg = (Format == ARMII::DPRdSoReg ||
430 Format == ARMII::DPRnSoReg ||
431 Format == ARMII::DPRSoReg ||
432 Format == ARMII::DPRSoRegS);
433 if (HasSoReg)
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000434 // Encode SoReg.
Evan Cheng49a9f292008-09-12 22:45:55 +0000435 return Binary | getMachineSoRegOpValue(MI, TID, OpIdx);
Evan Cheng7602e112008-09-02 06:52:38 +0000436
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000437 const MachineOperand &MO = MI.getOperand(OpIdx);
Dan Gohmand735b802008-10-03 15:45:36 +0000438 if (MO.isReg())
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000439 // Encode register Rm.
Jim Grosbach48b828f2008-10-03 15:53:56 +0000440 return Binary | getMachineOpValue(MI, NumDefs);
Evan Cheng7602e112008-09-02 06:52:38 +0000441
Evan Cheng5f1db7b2008-09-12 22:01:15 +0000442 // Encode so_imm.
443 // Set bit I(25) to identify this is the immediate form of <shifter_op>
444 Binary |= 1 << ARMII::I_BitShift;
445 unsigned SoImm = MO.getImm();
446 // Encode rotate_imm.
447 Binary |= ARM_AM::getSOImmValRot(SoImm) << ARMII::RotImmShift;
448 // Encode immed_8.
449 Binary |= ARM_AM::getSOImmVal(SoImm);
Evan Cheng7602e112008-09-02 06:52:38 +0000450 return Binary;
451}
452
453unsigned ARMCodeEmitter::getAddrMode2InstrBinary(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000454 const TargetInstrDesc &TID,
Evan Cheng7602e112008-09-02 06:52:38 +0000455 unsigned Binary) {
Jim Grosbach33412622008-10-07 19:05:35 +0000456 // Set the conditional execution predicate
457 Binary |= II->getPredicate(&MI) << 28;
Evan Cheng057d0c32008-09-18 07:28:19 +0000458
Evan Cheng7602e112008-09-02 06:52:38 +0000459 // Set first operand
460 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
461
462 // Set second operand
463 Binary |= getMachineOpValue(MI, 1) << ARMII::RegRnShift;
464
465 const MachineOperand &MO2 = MI.getOperand(2);
466 const MachineOperand &MO3 = MI.getOperand(3);
467
Evan Chenge7de7e32008-09-13 01:44:01 +0000468 // Set bit U(23) according to sign of immed value (positive or negative).
Evan Cheng7602e112008-09-02 06:52:38 +0000469 Binary |= ((ARM_AM::getAM2Op(MO3.getImm()) == ARM_AM::add ? 1 : 0) <<
Evan Chenge7de7e32008-09-13 01:44:01 +0000470 ARMII::U_BitShift);
Evan Cheng7602e112008-09-02 06:52:38 +0000471 if (!MO2.getReg()) { // is immediate
472 if (ARM_AM::getAM2Offset(MO3.getImm()))
473 // Set the value of offset_12 field
474 Binary |= ARM_AM::getAM2Offset(MO3.getImm());
475 return Binary;
476 }
477
478 // Set bit I(25), because this is not in immediate enconding.
479 Binary |= 1 << ARMII::I_BitShift;
480 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
481 // Set bit[3:0] to the corresponding Rm register
482 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
483
484 // if this instr is in scaled register offset/index instruction, set
485 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
486 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm())) {
487 Binary |= getShiftOp(MO3) << 5; // shift
488 Binary |= ShImm << 7; // shift_immed
489 }
490
491 return Binary;
492}
493
494unsigned ARMCodeEmitter::getAddrMode3InstrBinary(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000495 const TargetInstrDesc &TID,
Evan Cheng7602e112008-09-02 06:52:38 +0000496 unsigned Binary) {
Jim Grosbach33412622008-10-07 19:05:35 +0000497 // Set the conditional execution predicate
498 Binary |= II->getPredicate(&MI) << 28;
Evan Cheng057d0c32008-09-18 07:28:19 +0000499
Evan Cheng7602e112008-09-02 06:52:38 +0000500 // Set first operand
501 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
502
503 // Set second operand
504 Binary |= getMachineOpValue(MI, 1) << ARMII::RegRnShift;
505
506 const MachineOperand &MO2 = MI.getOperand(2);
507 const MachineOperand &MO3 = MI.getOperand(3);
508
Evan Chenge7de7e32008-09-13 01:44:01 +0000509 // Set bit U(23) according to sign of immed value (positive or negative)
Evan Cheng7602e112008-09-02 06:52:38 +0000510 Binary |= ((ARM_AM::getAM2Op(MO3.getImm()) == ARM_AM::add ? 1 : 0) <<
511 ARMII::U_BitShift);
512
513 // If this instr is in register offset/index encoding, set bit[3:0]
514 // to the corresponding Rm register.
515 if (MO2.getReg()) {
516 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
517 return Binary;
518 }
519
520 // if this instr is in immediate offset/index encoding, set bit 22 to 1
521 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm())) {
522 Binary |= 1 << 22;
523 // Set operands
524 Binary |= (ImmOffs >> 4) << 8; // immedH
525 Binary |= (ImmOffs & ~0xF); // immedL
526 }
527
528 return Binary;
529}
530
531unsigned ARMCodeEmitter::getAddrMode4InstrBinary(const MachineInstr &MI,
Evan Cheng49a9f292008-09-12 22:45:55 +0000532 const TargetInstrDesc &TID,
Evan Cheng7602e112008-09-02 06:52:38 +0000533 unsigned Binary) {
Jim Grosbach33412622008-10-07 19:05:35 +0000534 // Set the conditional execution predicate
535 Binary |= II->getPredicate(&MI) << 28;
Evan Cheng057d0c32008-09-18 07:28:19 +0000536
Evan Cheng7602e112008-09-02 06:52:38 +0000537 // Set first operand
538 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
539
540 // Set addressing mode by modifying bits U(23) and P(24)
541 // IA - Increment after - bit U = 1 and bit P = 0
542 // IB - Increment before - bit U = 1 and bit P = 1
543 // DA - Decrement after - bit U = 0 and bit P = 0
544 // DB - Decrement before - bit U = 0 and bit P = 1
545 const MachineOperand &MO = MI.getOperand(1);
546 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO.getImm());
547 switch (Mode) {
548 default: assert(0 && "Unknown addressing sub-mode!");
549 case ARM_AM::da: break;
550 case ARM_AM::db: Binary |= 0x1 << 24; break;
551 case ARM_AM::ia: Binary |= 0x1 << 23; break;
552 case ARM_AM::ib: Binary |= 0x3 << 23; break;
553 }
554
555 // Set bit W(21)
556 if (ARM_AM::getAM4WBFlag(MO.getImm()))
557 Binary |= 0x1 << 21;
558
559 // Set registers
560 for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
561 const MachineOperand &MO = MI.getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000562 if (MO.isReg() && MO.isImplicit())
Evan Cheng7602e112008-09-02 06:52:38 +0000563 continue;
564 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
565 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
566 RegNum < 16);
567 Binary |= 0x1 << RegNum;
568 }
569
570 return Binary;
571}
572
573/// getInstrBinary - Return binary encoding for the specified
574/// machine instruction.
575unsigned ARMCodeEmitter::getInstrBinary(const MachineInstr &MI) {
576 // Part of binary is determined by TableGn.
577 unsigned Binary = getBinaryCodeForInstr(MI);
578
Evan Cheng49a9f292008-09-12 22:45:55 +0000579 const TargetInstrDesc &TID = MI.getDesc();
580 switch (TID.TSFlags & ARMII::AddrModeMask) {
Evan Cheng7602e112008-09-02 06:52:38 +0000581 case ARMII::AddrModeNone:
Evan Cheng49a9f292008-09-12 22:45:55 +0000582 return getAddrModeNoneInstrBinary(MI, TID, Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000583 case ARMII::AddrMode1:
Evan Cheng49a9f292008-09-12 22:45:55 +0000584 return getAddrMode1InstrBinary(MI, TID, Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000585 case ARMII::AddrMode2:
Evan Cheng49a9f292008-09-12 22:45:55 +0000586 return getAddrMode2InstrBinary(MI, TID, Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000587 case ARMII::AddrMode3:
Evan Cheng49a9f292008-09-12 22:45:55 +0000588 return getAddrMode3InstrBinary(MI, TID, Binary);
Evan Cheng7602e112008-09-02 06:52:38 +0000589 case ARMII::AddrMode4:
Evan Cheng49a9f292008-09-12 22:45:55 +0000590 return getAddrMode4InstrBinary(MI, TID, Binary);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000591 }
592
Evan Cheng7602e112008-09-02 06:52:38 +0000593 abort();
594 return 0;
Evan Cheng148b6a42007-07-05 21:15:40 +0000595}
Evan Cheng7602e112008-09-02 06:52:38 +0000596
597#include "ARMGenCodeEmitter.inc"