1 //===-- ARM/ARMCodeEmitter.cpp - Convert ARM code to machine code ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the pass that transforms the ARM machine instructions into
11 // relocatable machine code.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "jit"
17 #include "ARMAddressingModes.h"
18 #include "ARMConstantPoolValue.h"
19 #include "ARMInstrInfo.h"
20 #include "ARMRelocations.h"
21 #include "ARMSubtarget.h"
22 #include "ARMTargetMachine.h"
23 #include "llvm/Constants.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/Function.h"
26 #include "llvm/PassManager.h"
27 #include "llvm/CodeGen/MachineCodeEmitter.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineFunctionPass.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineJumpTableInfo.h"
32 #include "llvm/CodeGen/Passes.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/Debug.h"
41 STATISTIC(NumEmitted, "Number of machine instructions emitted");
44 class VISIBILITY_HIDDEN ARMCodeEmitter : public MachineFunctionPass {
46 const ARMInstrInfo *II;
49 MachineCodeEmitter &MCE;
50 const std::vector<MachineConstantPoolEntry> *MCPEs;
51 const std::vector<MachineJumpTableEntry> *MJTEs;
56 explicit ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce)
57 : MachineFunctionPass(&ID), JTI(0), II(0), TD(0), TM(tm),
58 MCE(mce), MCPEs(0), MJTEs(0),
59 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
60 ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce,
61 const ARMInstrInfo &ii, const TargetData &td)
62 : MachineFunctionPass(&ID), JTI(0), II(&ii), TD(&td), TM(tm),
63 MCE(mce), MCPEs(0), MJTEs(0),
64 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
66 bool runOnMachineFunction(MachineFunction &MF);
68 virtual const char *getPassName() const {
69 return "ARM Machine Code Emitter";
72 void emitInstruction(const MachineInstr &MI);
76 void emitWordLE(unsigned Binary);
78 void emitConstPoolInstruction(const MachineInstr &MI);
80 void emitMOVi2piecesInstruction(const MachineInstr &MI);
82 void emitLEApcrelJTInstruction(const MachineInstr &MI);
84 void addPCLabel(unsigned LabelID);
86 void emitPseudoInstruction(const MachineInstr &MI);
88 unsigned getMachineSoRegOpValue(const MachineInstr &MI,
89 const TargetInstrDesc &TID,
90 const MachineOperand &MO,
93 unsigned getMachineSoImmOpValue(unsigned SoImm);
95 unsigned getAddrModeSBit(const MachineInstr &MI,
96 const TargetInstrDesc &TID) const;
98 void emitDataProcessingInstruction(const MachineInstr &MI,
99 unsigned ImplicitRd = 0,
100 unsigned ImplicitRn = 0);
102 void emitLoadStoreInstruction(const MachineInstr &MI,
103 unsigned ImplicitRd = 0,
104 unsigned ImplicitRn = 0);
106 void emitMiscLoadStoreInstruction(const MachineInstr &MI,
107 unsigned ImplicitRn = 0);
109 void emitLoadStoreMultipleInstruction(const MachineInstr &MI);
111 void emitMulFrmInstruction(const MachineInstr &MI);
113 void emitExtendInstruction(const MachineInstr &MI);
115 void emitMiscArithInstruction(const MachineInstr &MI);
117 void emitBranchInstruction(const MachineInstr &MI);
119 void emitInlineJumpTable(unsigned JTIndex);
121 void emitMiscBranchInstruction(const MachineInstr &MI);
123 /// getBinaryCodeForInstr - This function, generated by the
124 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
125 /// machine instructions.
127 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
129 /// getMachineOpValue - Return binary encoding of operand. If the machine
130 /// operand requires relocation, record the relocation and return zero.
131 unsigned getMachineOpValue(const MachineInstr &MI,const MachineOperand &MO);
132 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) {
133 return getMachineOpValue(MI, MI.getOperand(OpIdx));
136 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
138 unsigned getShiftOp(unsigned Imm) const ;
140 /// Routines that handle operands which add machine relocations which are
141 /// fixed up by the relocation stage.
142 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
143 bool NeedStub, unsigned CPIdx = 0);
144 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
145 void emitConstPoolAddress(unsigned CPI, unsigned Reloc);
146 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc);
147 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
148 intptr_t JTBase = 0);
150 char ARMCodeEmitter::ID = 0;
153 /// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
154 /// to the specified MCE object.
155 FunctionPass *llvm::createARMCodeEmitterPass(ARMTargetMachine &TM,
156 MachineCodeEmitter &MCE) {
157 return new ARMCodeEmitter(TM, MCE);
160 bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
161 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
162 MF.getTarget().getRelocationModel() != Reloc::Static) &&
163 "JIT relocation model must be set to static or default!");
164 II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo();
165 TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData();
166 JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo();
167 MCPEs = &MF.getConstantPool()->getConstants();
168 MJTEs = &MF.getJumpTableInfo()->getJumpTables();
169 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
173 DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
174 MCE.startFunction(MF);
175 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
177 MCE.StartMachineBasicBlock(MBB);
178 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
182 } while (MCE.finishFunction(MF));
187 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
189 unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
190 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
191 default: assert(0 && "Unknown shift opc!");
192 case ARM_AM::asr: return 2;
193 case ARM_AM::lsl: return 0;
194 case ARM_AM::lsr: return 1;
196 case ARM_AM::rrx: return 3;
201 /// getMachineOpValue - Return binary encoding of operand. If the machine
202 /// operand requires relocation, record the relocation and return zero.
203 unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
204 const MachineOperand &MO) {
206 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
208 return static_cast<unsigned>(MO.getImm());
209 else if (MO.isGlobal())
210 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true);
211 else if (MO.isSymbol())
212 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_relative);
214 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
216 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
218 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
220 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
226 /// emitGlobalAddress - Emit the specified address to the code stream.
228 void ARMCodeEmitter::emitGlobalAddress(GlobalValue *GV,
229 unsigned Reloc, bool NeedStub,
231 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
232 Reloc, GV, CPIdx, NeedStub));
235 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
236 /// be emitted to the current location in the function, and allow it to be PC
238 void ARMCodeEmitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
239 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
243 /// emitConstPoolAddress - Arrange for the address of an constant pool
244 /// to be emitted to the current location in the function, and allow it to be PC
246 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) {
247 // Tell JIT emitter we'll resolve the address.
248 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
249 Reloc, CPI, 0, true));
252 /// emitJumpTableAddress - Arrange for the address of a jump table to
253 /// be emitted to the current location in the function, and allow it to be PC
255 void ARMCodeEmitter::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) {
256 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
257 Reloc, JTIndex, 0, true));
260 /// emitMachineBasicBlock - Emit the specified address basic block.
261 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
262 unsigned Reloc, intptr_t JTBase) {
263 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
267 void ARMCodeEmitter::emitWordLE(unsigned Binary) {
269 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
270 << Binary << std::dec << "\n";
272 MCE.emitWordLE(Binary);
275 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
276 DOUT << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI;
278 NumEmitted++; // Keep track of the # of mi's emitted
279 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
281 assert(0 && "Unhandled instruction encoding format!");
284 emitPseudoInstruction(MI);
287 case ARMII::DPSoRegFrm:
288 emitDataProcessingInstruction(MI);
292 emitLoadStoreInstruction(MI);
294 case ARMII::LdMiscFrm:
295 case ARMII::StMiscFrm:
296 emitMiscLoadStoreInstruction(MI);
298 case ARMII::LdMulFrm:
299 case ARMII::StMulFrm:
300 emitLoadStoreMultipleInstruction(MI);
303 emitMulFrmInstruction(MI);
306 emitExtendInstruction(MI);
308 case ARMII::ArithMiscFrm:
309 emitMiscArithInstruction(MI);
312 emitBranchInstruction(MI);
314 case ARMII::BrMiscFrm:
315 emitMiscBranchInstruction(MI);
320 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
321 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
322 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
323 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
325 // Remember the CONSTPOOL_ENTRY address for later relocation.
326 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
328 // Emit constpool island entry. In most cases, the actual values will be
329 // resolved and relocated after code emission.
330 if (MCPE.isMachineConstantPoolEntry()) {
331 ARMConstantPoolValue *ACPV =
332 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
334 DOUT << " ** ARM constant pool #" << CPI << " @ "
335 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n';
337 GlobalValue *GV = ACPV->getGV();
339 assert(!ACPV->isStub() && "Don't know how to deal this yet!");
340 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry, false, CPIndex);
342 assert(!ACPV->isNonLazyPointer() && "Don't know how to deal this yet!");
343 emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
347 Constant *CV = MCPE.Val.ConstVal;
349 DOUT << " ** Constant pool #" << CPI << " @ "
350 << (void*)MCE.getCurrentPCValue() << " " << *CV << '\n';
352 if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
353 emitGlobalAddress(GV, ARM::reloc_arm_absolute, false);
356 assert(CV->getType()->isInteger() &&
357 "Not expecting non-integer constpool entries yet!");
358 const ConstantInt *CI = dyn_cast<ConstantInt>(CV);
359 uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
365 void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
366 const MachineOperand &MO0 = MI.getOperand(0);
367 const MachineOperand &MO1 = MI.getOperand(1);
368 assert(MO1.isImm() && "Not a valid so_imm value!");
369 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
370 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
372 // Emit the 'mov' instruction.
373 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
375 // Set the conditional execution predicate.
376 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
379 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
382 // Set bit I(25) to identify this is the immediate form of <shifter_op>
383 Binary |= 1 << ARMII::I_BitShift;
384 Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V1));
387 // Now the 'orr' instruction.
388 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
390 // Set the conditional execution predicate.
391 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
394 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
397 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
400 // Set bit I(25) to identify this is the immediate form of <shifter_op>
401 Binary |= 1 << ARMII::I_BitShift;
402 Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V2));
406 void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
407 // It's basically add r, pc, (LJTI - $+8)
409 const TargetInstrDesc &TID = MI.getDesc();
411 // Emit the 'add' instruction.
412 unsigned Binary = 0x4 << 21; // add: Insts{24-31} = 0b0100
414 // Set the conditional execution predicate
415 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
417 // Encode S bit if MI modifies CPSR.
418 Binary |= getAddrModeSBit(MI, TID);
421 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
423 // Encode Rn which is PC.
424 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
426 // Encode the displacement.
427 // Set bit I(25) to identify this is the immediate form of <shifter_op>.
428 Binary |= 1 << ARMII::I_BitShift;
429 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
434 void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
435 DOUT << " ** LPC" << LabelID << " @ "
436 << (void*)MCE.getCurrentPCValue() << '\n';
437 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
440 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
441 unsigned Opcode = MI.getDesc().Opcode;
445 case ARM::CONSTPOOL_ENTRY:
446 emitConstPoolInstruction(MI);
449 // Remember of the address of the PC label for relocation later.
450 addPCLabel(MI.getOperand(2).getImm());
451 // PICADD is just an add instruction that implicitly read pc.
452 emitDataProcessingInstruction(MI, 0, ARM::PC);
459 // Remember of the address of the PC label for relocation later.
460 addPCLabel(MI.getOperand(2).getImm());
461 // These are just load / store instructions that implicitly read pc.
462 emitLoadStoreInstruction(MI, 0, ARM::PC);
469 // Remember of the address of the PC label for relocation later.
470 addPCLabel(MI.getOperand(2).getImm());
471 // These are just load / store instructions that implicitly read pc.
472 emitMiscLoadStoreInstruction(MI, ARM::PC);
475 case ARM::MOVi2pieces:
476 // Two instructions to materialize a constant.
477 emitMOVi2piecesInstruction(MI);
479 case ARM::LEApcrelJT:
480 // Materialize jumptable address.
481 emitLEApcrelJTInstruction(MI);
487 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
488 const TargetInstrDesc &TID,
489 const MachineOperand &MO,
491 unsigned Binary = getMachineOpValue(MI, MO);
493 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
494 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
495 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
497 // Encode the shift opcode.
499 unsigned Rs = MO1.getReg();
501 // Set shift operand (bit[7:4]).
506 // RRX - 0110 and bit[11:8] clear.
508 default: assert(0 && "Unknown shift opc!");
509 case ARM_AM::lsl: SBits = 0x1; break;
510 case ARM_AM::lsr: SBits = 0x3; break;
511 case ARM_AM::asr: SBits = 0x5; break;
512 case ARM_AM::ror: SBits = 0x7; break;
513 case ARM_AM::rrx: SBits = 0x6; break;
516 // Set shift operand (bit[6:4]).
522 default: assert(0 && "Unknown shift opc!");
523 case ARM_AM::lsl: SBits = 0x0; break;
524 case ARM_AM::lsr: SBits = 0x2; break;
525 case ARM_AM::asr: SBits = 0x4; break;
526 case ARM_AM::ror: SBits = 0x6; break;
529 Binary |= SBits << 4;
530 if (SOpc == ARM_AM::rrx)
533 // Encode the shift operation Rs or shift_imm (except rrx).
535 // Encode Rs bit[11:8].
536 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
538 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
541 // Encode shift_imm bit[11:7].
542 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
545 unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
546 // Encode rotate_imm.
547 unsigned Binary = (ARM_AM::getSOImmValRot(SoImm) >> 1)
548 << ARMII::SoRotImmShift;
551 Binary |= ARM_AM::getSOImmValImm(SoImm);
555 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
556 const TargetInstrDesc &TID) const {
557 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
558 const MachineOperand &MO = MI.getOperand(i-1);
559 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
560 return 1 << ARMII::S_BitShift;
565 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
567 unsigned ImplicitRn) {
568 const TargetInstrDesc &TID = MI.getDesc();
570 // Part of binary is determined by TableGn.
571 unsigned Binary = getBinaryCodeForInstr(MI);
573 // Set the conditional execution predicate
574 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
576 // Encode S bit if MI modifies CPSR.
577 Binary |= getAddrModeSBit(MI, TID);
579 // Encode register def if there is one.
580 unsigned NumDefs = TID.getNumDefs();
583 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
585 // Special handling for implicit use (e.g. PC).
586 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
587 << ARMII::RegRdShift);
589 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
590 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
593 // Encode first non-shifter register operand if there is one.
594 bool isUnary = TID.TSFlags & ARMII::UnaryDP;
597 // Special handling for implicit use (e.g. PC).
598 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
599 << ARMII::RegRnShift);
601 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
606 // Encode shifter operand.
607 const MachineOperand &MO = MI.getOperand(OpIdx);
608 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
610 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
615 // Encode register Rm.
616 emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
621 // Set bit I(25) to identify this is the immediate form of <shifter_op>.
622 Binary |= 1 << ARMII::I_BitShift;
623 Binary |= getMachineSoImmOpValue(MO.getImm());
628 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
630 unsigned ImplicitRn) {
631 // Part of binary is determined by TableGn.
632 unsigned Binary = getBinaryCodeForInstr(MI);
634 // Set the conditional execution predicate
635 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
640 // Special handling for implicit use (e.g. PC).
641 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
642 << ARMII::RegRdShift);
644 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
646 // Set second operand
648 // Special handling for implicit use (e.g. PC).
649 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
650 << ARMII::RegRnShift);
652 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
654 const MachineOperand &MO2 = MI.getOperand(OpIdx);
655 unsigned AM2Opc = (ImplicitRn == ARM::PC)
656 ? 0 : MI.getOperand(OpIdx+1).getImm();
658 // Set bit U(23) according to sign of immed value (positive or negative).
659 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
661 if (!MO2.getReg()) { // is immediate
662 if (ARM_AM::getAM2Offset(AM2Opc))
663 // Set the value of offset_12 field
664 Binary |= ARM_AM::getAM2Offset(AM2Opc);
669 // Set bit I(25), because this is not in immediate enconding.
670 Binary |= 1 << ARMII::I_BitShift;
671 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
672 // Set bit[3:0] to the corresponding Rm register
673 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
675 // if this instr is in scaled register offset/index instruction, set
676 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
677 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
678 Binary |= getShiftOp(AM2Opc) << 5; // shift
679 Binary |= ShImm << 7; // shift_immed
685 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
686 unsigned ImplicitRn) {
687 // Part of binary is determined by TableGn.
688 unsigned Binary = getBinaryCodeForInstr(MI);
690 // Set the conditional execution predicate
691 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
694 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
696 // Set second operand
699 // Special handling for implicit use (e.g. PC).
700 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
701 << ARMII::RegRnShift);
703 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
705 const MachineOperand &MO2 = MI.getOperand(OpIdx);
706 unsigned AM3Opc = (ImplicitRn == ARM::PC)
707 ? 0 : MI.getOperand(OpIdx+1).getImm();
709 // Set bit U(23) according to sign of immed value (positive or negative)
710 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
713 // If this instr is in register offset/index encoding, set bit[3:0]
714 // to the corresponding Rm register.
716 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
721 // This instr is in immediate offset/index encoding, set bit 22 to 1.
722 Binary |= 1 << ARMII::AM3_I_BitShift;
723 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
725 Binary |= (ImmOffs >> 4) << 8; // immedH
726 Binary |= (ImmOffs & ~0xF); // immedL
732 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
733 // Part of binary is determined by TableGn.
734 unsigned Binary = getBinaryCodeForInstr(MI);
736 // Set the conditional execution predicate
737 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
740 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
742 // Set addressing mode by modifying bits U(23) and P(24)
743 // IA - Increment after - bit U = 1 and bit P = 0
744 // IB - Increment before - bit U = 1 and bit P = 1
745 // DA - Decrement after - bit U = 0 and bit P = 0
746 // DB - Decrement before - bit U = 0 and bit P = 1
747 const MachineOperand &MO = MI.getOperand(1);
748 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO.getImm());
750 default: assert(0 && "Unknown addressing sub-mode!");
751 case ARM_AM::da: break;
752 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
753 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
754 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
758 if (ARM_AM::getAM4WBFlag(MO.getImm()))
759 Binary |= 0x1 << ARMII::W_BitShift;
762 for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
763 const MachineOperand &MO = MI.getOperand(i);
764 if (MO.isReg() && MO.isImplicit())
766 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
767 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
769 Binary |= 0x1 << RegNum;
775 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
776 const TargetInstrDesc &TID = MI.getDesc();
778 // Part of binary is determined by TableGn.
779 unsigned Binary = getBinaryCodeForInstr(MI);
781 // Set the conditional execution predicate
782 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
784 // Encode S bit if MI modifies CPSR.
785 Binary |= getAddrModeSBit(MI, TID);
787 // 32x32->64bit operations have two destination registers. The number
788 // of register definitions will tell us if that's what we're dealing with.
790 if (TID.getNumDefs() == 2)
791 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
794 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
797 Binary |= getMachineOpValue(MI, OpIdx++);
800 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
802 // Many multiple instructions (e.g. MLA) have three src operands. Encode
803 // it as Rn (for multiply, that's in the same offset as RdLo.
804 if (TID.getNumOperands() > OpIdx &&
805 !TID.OpInfo[OpIdx].isPredicate() &&
806 !TID.OpInfo[OpIdx].isOptionalDef())
807 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
812 void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
813 const TargetInstrDesc &TID = MI.getDesc();
815 // Part of binary is determined by TableGn.
816 unsigned Binary = getBinaryCodeForInstr(MI);
818 // Set the conditional execution predicate
819 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
824 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
826 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
827 const MachineOperand &MO2 = MI.getOperand(OpIdx);
829 // Two register operand form.
831 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
834 Binary |= getMachineOpValue(MI, MO2);
837 Binary |= getMachineOpValue(MI, MO1);
840 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
841 if (MI.getOperand(OpIdx).isImm() &&
842 !TID.OpInfo[OpIdx].isPredicate() &&
843 !TID.OpInfo[OpIdx].isOptionalDef())
844 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
849 void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
850 const TargetInstrDesc &TID = MI.getDesc();
852 // Part of binary is determined by TableGn.
853 unsigned Binary = getBinaryCodeForInstr(MI);
855 // Set the conditional execution predicate
856 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
861 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
863 const MachineOperand &MO = MI.getOperand(OpIdx++);
864 if (OpIdx == TID.getNumOperands() ||
865 TID.OpInfo[OpIdx].isPredicate() ||
866 TID.OpInfo[OpIdx].isOptionalDef()) {
867 // Encode Rm and it's done.
868 Binary |= getMachineOpValue(MI, MO);
874 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
877 Binary |= getMachineOpValue(MI, OpIdx++);
880 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
881 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
882 Binary |= ShiftAmt << ARMII::ShiftShift;
887 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
888 const TargetInstrDesc &TID = MI.getDesc();
890 if (TID.Opcode == ARM::TPsoft)
893 // Part of binary is determined by TableGn.
894 unsigned Binary = getBinaryCodeForInstr(MI);
896 // Set the conditional execution predicate
897 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
899 // Set signed_immed_24 field
900 Binary |= getMachineOpValue(MI, 0);
905 void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
906 // Remember the base address of the inline jump table.
907 intptr_t JTBase = MCE.getCurrentPCValue();
908 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
909 DOUT << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase << '\n';
911 // Now emit the jump table entries.
912 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
913 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
915 // DestBB address - JT base.
916 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
918 // Absolute DestBB address.
919 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
924 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
925 const TargetInstrDesc &TID = MI.getDesc();
927 // Handle jump tables.
928 if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
929 // First emit a ldr pc, [] instruction.
930 emitDataProcessingInstruction(MI, ARM::PC);
932 // Then emit the inline jump table.
933 unsigned JTIndex = (TID.Opcode == ARM::BR_JTr)
934 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
935 emitInlineJumpTable(JTIndex);
937 } else if (TID.Opcode == ARM::BR_JTm) {
938 // First emit a ldr pc, [] instruction.
939 emitLoadStoreInstruction(MI, ARM::PC);
941 // Then emit the inline jump table.
942 emitInlineJumpTable(MI.getOperand(3).getIndex());
946 // Part of binary is determined by TableGn.
947 unsigned Binary = getBinaryCodeForInstr(MI);
949 // Set the conditional execution predicate
950 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
952 if (TID.Opcode == ARM::BX_RET)
953 // The return register is LR.
954 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
956 // otherwise, set the return register
957 Binary |= getMachineOpValue(MI, 0);
962 #include "ARMGenCodeEmitter.inc"