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/Passes.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/Debug.h"
37 STATISTIC(NumEmitted, "Number of machine instructions emitted");
40 class VISIBILITY_HIDDEN ARMCodeEmitter : public MachineFunctionPass {
42 const ARMInstrInfo *II;
45 MachineCodeEmitter &MCE;
46 const std::vector<MachineConstantPoolEntry> *MCPEs;
50 explicit ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce)
51 : MachineFunctionPass(&ID), JTI(0), II(0), TD(0), TM(tm),
53 ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce,
54 const ARMInstrInfo &ii, const TargetData &td)
55 : MachineFunctionPass(&ID), JTI(0), II(&ii), TD(&td), TM(tm),
58 bool runOnMachineFunction(MachineFunction &MF);
60 virtual const char *getPassName() const {
61 return "ARM Machine Code Emitter";
64 void emitInstruction(const MachineInstr &MI);
68 void emitWordLE(unsigned Binary);
70 void emitConstPoolInstruction(const MachineInstr &MI);
72 void emitMOVi2piecesInstruction(const MachineInstr &MI);
74 void addPCLabel(unsigned LabelID);
76 void emitPseudoInstruction(const MachineInstr &MI);
78 unsigned getMachineSoRegOpValue(const MachineInstr &MI,
79 const TargetInstrDesc &TID,
80 const MachineOperand &MO,
83 unsigned getMachineSoImmOpValue(unsigned SoImm);
85 unsigned getAddrModeSBit(const MachineInstr &MI,
86 const TargetInstrDesc &TID) const;
88 void emitDataProcessingInstruction(const MachineInstr &MI,
89 unsigned ImplicitRn = 0);
91 void emitLoadStoreInstruction(const MachineInstr &MI,
92 unsigned ImplicitRn = 0);
94 void emitMiscLoadStoreInstruction(const MachineInstr &MI,
95 unsigned ImplicitRn = 0);
97 void emitLoadStoreMultipleInstruction(const MachineInstr &MI);
99 void emitMulFrmInstruction(const MachineInstr &MI);
101 void emitBranchInstruction(const MachineInstr &MI);
103 void emitMiscBranchInstruction(const MachineInstr &MI);
105 /// getBinaryCodeForInstr - This function, generated by the
106 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
107 /// machine instructions.
109 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
111 /// getMachineOpValue - Return binary encoding of operand. If the machine
112 /// operand requires relocation, record the relocation and return zero.
113 unsigned getMachineOpValue(const MachineInstr &MI,const MachineOperand &MO);
114 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) {
115 return getMachineOpValue(MI, MI.getOperand(OpIdx));
118 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
120 unsigned getShiftOp(unsigned Imm) const ;
122 /// Routines that handle operands which add machine relocations which are
123 /// fixed up by the JIT fixup stage.
124 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
126 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
127 void emitConstPoolAddress(unsigned CPI, unsigned Reloc,
128 int Disp = 0, unsigned PCAdj = 0 );
129 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc,
131 void emitGlobalConstant(const Constant *CV);
132 void emitMachineBasicBlock(MachineBasicBlock *BB);
134 char ARMCodeEmitter::ID = 0;
137 /// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
138 /// to the specified MCE object.
139 FunctionPass *llvm::createARMCodeEmitterPass(ARMTargetMachine &TM,
140 MachineCodeEmitter &MCE) {
141 return new ARMCodeEmitter(TM, MCE);
144 bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
145 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();
150 JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo();
151 MCPEs = &MF.getConstantPool()->getConstants();
152 JTI->Initialize(MCPEs);
155 DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
156 MCE.startFunction(MF);
157 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
159 MCE.StartMachineBasicBlock(MBB);
160 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
164 } while (MCE.finishFunction(MF));
169 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
171 unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
172 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
173 default: assert(0 && "Unknown shift opc!");
174 case ARM_AM::asr: return 2;
175 case ARM_AM::lsl: return 0;
176 case ARM_AM::lsr: return 1;
178 case ARM_AM::rrx: return 3;
183 /// getMachineOpValue - Return binary encoding of operand. If the machine
184 /// operand requires relocation, record the relocation and return zero.
185 unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
186 const MachineOperand &MO) {
188 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
190 return static_cast<unsigned>(MO.getImm());
191 else if (MO.isGlobal())
192 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true);
193 else if (MO.isSymbol())
194 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_relative);
196 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
198 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
200 emitMachineBasicBlock(MO.getMBB());
202 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
208 /// emitGlobalAddress - Emit the specified address to the code stream.
210 void ARMCodeEmitter::emitGlobalAddress(GlobalValue *GV,
211 unsigned Reloc, bool NeedStub) {
212 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
213 Reloc, GV, 0, NeedStub));
216 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
217 /// be emitted to the current location in the function, and allow it to be PC
219 void ARMCodeEmitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
220 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
224 /// emitConstPoolAddress - Arrange for the address of an constant pool
225 /// to be emitted to the current location in the function, and allow it to be PC
227 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
229 unsigned PCAdj /* = 0 */) {
230 // Tell JIT emitter we'll resolve the address.
231 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
232 Reloc, CPI, PCAdj, true));
235 /// emitJumpTableAddress - Arrange for the address of a jump table to
236 /// be emitted to the current location in the function, and allow it to be PC
238 void ARMCodeEmitter::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc,
239 unsigned PCAdj /* = 0 */) {
240 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
241 Reloc, JTIndex, PCAdj));
244 /// emitMachineBasicBlock - Emit the specified address basic block.
245 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB) {
246 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
247 ARM::reloc_arm_branch, BB));
250 void ARMCodeEmitter::emitWordLE(unsigned Binary) {
251 DOUT << " " << (void*)Binary << "\n";
252 MCE.emitWordLE(Binary);
255 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
256 DOUT << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI;
258 NumEmitted++; // Keep track of the # of mi's emitted
259 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
261 assert(0 && "Unhandled instruction encoding format!");
264 emitPseudoInstruction(MI);
267 case ARMII::DPSoRegFrm:
268 emitDataProcessingInstruction(MI);
272 emitLoadStoreInstruction(MI);
274 case ARMII::LdMiscFrm:
275 case ARMII::StMiscFrm:
276 emitMiscLoadStoreInstruction(MI);
278 case ARMII::LdMulFrm:
279 case ARMII::StMulFrm:
280 emitLoadStoreMultipleInstruction(MI);
283 emitMulFrmInstruction(MI);
286 emitBranchInstruction(MI);
288 case ARMII::BrMiscFrm:
289 emitMiscBranchInstruction(MI);
294 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
295 unsigned CPI = MI.getOperand(0).getImm();
296 unsigned CPIndex = MI.getOperand(1).getIndex();
297 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
299 // Remember the CONSTPOOL_ENTRY address for later relocation.
300 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
302 // Emit constpool island entry. In most cases, the actual values will be
303 // resolved and relocated after code emission.
304 if (MCPE.isMachineConstantPoolEntry()) {
305 ARMConstantPoolValue *ACPV =
306 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
308 DOUT << " ** ARM constant pool #" << CPI << " @ "
309 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << "\n";
311 GlobalValue *GV = ACPV->getGV();
313 assert(!ACPV->isStub() && "Don't know how to deal this yet!");
314 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
315 ARM::reloc_arm_machine_cp_entry,
316 GV, CPIndex, false));
318 assert(!ACPV->isNonLazyPointer() && "Don't know how to deal this yet!");
319 emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
323 Constant *CV = MCPE.Val.ConstVal;
325 DOUT << " ** Constant pool #" << CPI << " @ "
326 << (void*)MCE.getCurrentPCValue() << " " << *CV << "\n";
328 if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
329 emitGlobalAddress(GV, ARM::reloc_arm_absolute, false);
332 assert(CV->getType()->isInteger() &&
333 "Not expecting non-integer constpool entries yet!");
334 const ConstantInt *CI = dyn_cast<ConstantInt>(CV);
335 uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
341 void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
342 const MachineOperand &MO0 = MI.getOperand(0);
343 const MachineOperand &MO1 = MI.getOperand(1);
344 assert(MO1.isImm() && "Not a valid so_imm value!");
345 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
346 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
348 // Emit the 'mov' instruction.
349 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
351 // Set the conditional execution predicate.
352 Binary |= II->getPredicate(&MI) << 28;
355 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
358 // Set bit I(25) to identify this is the immediate form of <shifter_op>
359 Binary |= 1 << ARMII::I_BitShift;
360 Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V1));
363 // Now the 'orr' instruction.
364 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
366 // Set the conditional execution predicate.
367 Binary |= II->getPredicate(&MI) << 28;
370 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
373 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
376 // Set bit I(25) to identify this is the immediate form of <shifter_op>
377 Binary |= 1 << ARMII::I_BitShift;
378 Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V2));
382 void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
383 DOUT << " ** LPC" << LabelID << " @ "
384 << (void*)MCE.getCurrentPCValue() << '\n';
385 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
388 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
389 unsigned Opcode = MI.getDesc().Opcode;
393 case ARM::CONSTPOOL_ENTRY:
394 emitConstPoolInstruction(MI);
397 // Remember of the address of the PC label for relocation later.
398 addPCLabel(MI.getOperand(2).getImm());
399 // PICADD is just an add instruction that implicitly read pc.
400 emitDataProcessingInstruction(MI, ARM::PC);
407 // Remember of the address of the PC label for relocation later.
408 addPCLabel(MI.getOperand(2).getImm());
409 // These are just load / store instructions that implicitly read pc.
410 emitLoadStoreInstruction(MI, ARM::PC);
417 // Remember of the address of the PC label for relocation later.
418 addPCLabel(MI.getOperand(2).getImm());
419 // These are just load / store instructions that implicitly read pc.
420 emitMiscLoadStoreInstruction(MI, ARM::PC);
423 case ARM::MOVi2pieces:
424 // Two instructions to materialize a constant.
425 emitMOVi2piecesInstruction(MI);
431 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
432 const TargetInstrDesc &TID,
433 const MachineOperand &MO,
435 unsigned Binary = getMachineOpValue(MI, MO);
437 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
438 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
439 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
441 // Encode the shift opcode.
443 unsigned Rs = MO1.getReg();
445 // Set shift operand (bit[7:4]).
450 // RRX - 0110 and bit[11:8] clear.
452 default: assert(0 && "Unknown shift opc!");
453 case ARM_AM::lsl: SBits = 0x1; break;
454 case ARM_AM::lsr: SBits = 0x3; break;
455 case ARM_AM::asr: SBits = 0x5; break;
456 case ARM_AM::ror: SBits = 0x7; break;
457 case ARM_AM::rrx: SBits = 0x6; break;
460 // Set shift operand (bit[6:4]).
466 default: assert(0 && "Unknown shift opc!");
467 case ARM_AM::lsl: SBits = 0x0; break;
468 case ARM_AM::lsr: SBits = 0x2; break;
469 case ARM_AM::asr: SBits = 0x4; break;
470 case ARM_AM::ror: SBits = 0x6; break;
473 Binary |= SBits << 4;
474 if (SOpc == ARM_AM::rrx)
477 // Encode the shift operation Rs or shift_imm (except rrx).
479 // Encode Rs bit[11:8].
480 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
482 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
485 // Encode shift_imm bit[11:7].
486 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
489 unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
490 // Encode rotate_imm.
491 unsigned Binary = (ARM_AM::getSOImmValRot(SoImm) >> 1) << ARMII::RotImmShift;
493 Binary |= ARM_AM::getSOImmValImm(SoImm);
497 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
498 const TargetInstrDesc &TID) const {
499 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
500 const MachineOperand &MO = MI.getOperand(i-1);
501 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
502 return 1 << ARMII::S_BitShift;
507 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
508 unsigned ImplicitRn) {
509 const TargetInstrDesc &TID = MI.getDesc();
511 // Part of binary is determined by TableGn.
512 unsigned Binary = getBinaryCodeForInstr(MI);
514 // Set the conditional execution predicate
515 Binary |= II->getPredicate(&MI) << 28;
517 // Encode S bit if MI modifies CPSR.
518 Binary |= getAddrModeSBit(MI, TID);
520 // Encode register def if there is one.
521 unsigned NumDefs = TID.getNumDefs();
524 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdShift;
528 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
529 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
532 // Encode first non-shifter register operand if there is one.
533 bool isUnary = TID.TSFlags & ARMII::UnaryDP;
536 // Special handling for implicit use (e.g. PC).
537 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
538 << ARMII::RegRnShift);
540 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
545 // Encode shifter operand.
546 const MachineOperand &MO = MI.getOperand(OpIdx);
547 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
549 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
554 // Encode register Rm.
555 emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
560 // Set bit I(25) to identify this is the immediate form of <shifter_op>
561 Binary |= 1 << ARMII::I_BitShift;
562 Binary |= getMachineSoImmOpValue(MO.getImm());
567 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
568 unsigned ImplicitRn) {
569 // Part of binary is determined by TableGn.
570 unsigned Binary = getBinaryCodeForInstr(MI);
572 // Set the conditional execution predicate
573 Binary |= II->getPredicate(&MI) << 28;
576 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
578 // Set second operand
581 // Special handling for implicit use (e.g. PC).
582 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
583 << ARMII::RegRnShift);
585 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
589 const MachineOperand &MO2 = MI.getOperand(OpIdx);
590 unsigned AM2Opc = (ImplicitRn == ARM::PC)
591 ? 0 : MI.getOperand(OpIdx+1).getImm();
593 // Set bit U(23) according to sign of immed value (positive or negative).
594 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
596 if (!MO2.getReg()) { // is immediate
597 if (ARM_AM::getAM2Offset(AM2Opc))
598 // Set the value of offset_12 field
599 Binary |= ARM_AM::getAM2Offset(AM2Opc);
604 // Set bit I(25), because this is not in immediate enconding.
605 Binary |= 1 << ARMII::I_BitShift;
606 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
607 // Set bit[3:0] to the corresponding Rm register
608 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
610 // if this instr is in scaled register offset/index instruction, set
611 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
612 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
613 Binary |= getShiftOp(AM2Opc) << 5; // shift
614 Binary |= ShImm << 7; // shift_immed
620 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
621 unsigned ImplicitRn) {
622 // Part of binary is determined by TableGn.
623 unsigned Binary = getBinaryCodeForInstr(MI);
625 // Set the conditional execution predicate
626 Binary |= II->getPredicate(&MI) << 28;
629 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
631 // Set second operand
634 // Special handling for implicit use (e.g. PC).
635 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
636 << ARMII::RegRnShift);
638 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
642 const MachineOperand &MO2 = MI.getOperand(OpIdx);
643 unsigned AM3Opc = (ImplicitRn == ARM::PC)
644 ? 0 : MI.getOperand(OpIdx+1).getImm();
646 // Set bit U(23) according to sign of immed value (positive or negative)
647 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
650 // If this instr is in register offset/index encoding, set bit[3:0]
651 // to the corresponding Rm register.
653 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
658 // This instr is in immediate offset/index encoding, set bit 22 to 1.
660 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
662 Binary |= (ImmOffs >> 4) << 8; // immedH
663 Binary |= (ImmOffs & ~0xF); // immedL
669 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
670 // Part of binary is determined by TableGn.
671 unsigned Binary = getBinaryCodeForInstr(MI);
673 // Set the conditional execution predicate
674 Binary |= II->getPredicate(&MI) << 28;
677 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
679 // Set addressing mode by modifying bits U(23) and P(24)
680 // IA - Increment after - bit U = 1 and bit P = 0
681 // IB - Increment before - bit U = 1 and bit P = 1
682 // DA - Decrement after - bit U = 0 and bit P = 0
683 // DB - Decrement before - bit U = 0 and bit P = 1
684 const MachineOperand &MO = MI.getOperand(1);
685 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO.getImm());
687 default: assert(0 && "Unknown addressing sub-mode!");
688 case ARM_AM::da: break;
689 case ARM_AM::db: Binary |= 0x1 << 24; break;
690 case ARM_AM::ia: Binary |= 0x1 << 23; break;
691 case ARM_AM::ib: Binary |= 0x3 << 23; break;
695 if (ARM_AM::getAM4WBFlag(MO.getImm()))
699 for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
700 const MachineOperand &MO = MI.getOperand(i);
701 if (MO.isReg() && MO.isImplicit())
703 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
704 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
706 Binary |= 0x1 << RegNum;
712 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
713 const TargetInstrDesc &TID = MI.getDesc();
715 // Part of binary is determined by TableGn.
716 unsigned Binary = getBinaryCodeForInstr(MI);
718 // Set the conditional execution predicate
719 Binary |= II->getPredicate(&MI) << 28;
721 // Encode S bit if MI modifies CPSR.
722 Binary |= getAddrModeSBit(MI, TID);
724 // 32x32->64bit operations have two destination registers. The number
725 // of register definitions will tell us if that's what we're dealing with.
727 if (TID.getNumDefs() == 2)
728 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
731 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
734 Binary |= getMachineOpValue(MI, OpIdx++);
737 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
739 // Many multiple instructions (e.g. MLA) have three src operands. Encode
740 // it as Rn (for multiply, that's in the same offset as RdLo.
741 if (TID.getNumOperands() - TID.getNumDefs() == 3)
742 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdLoShift;
747 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
748 const TargetInstrDesc &TID = MI.getDesc();
750 if (TID.Opcode == ARM::TPsoft)
753 // Part of binary is determined by TableGn.
754 unsigned Binary = getBinaryCodeForInstr(MI);
756 // Set the conditional execution predicate
757 Binary |= II->getPredicate(&MI) << 28;
759 // Set signed_immed_24 field
760 Binary |= getMachineOpValue(MI, 0);
762 // if it is a conditional branch, set cond field
763 if (TID.Opcode == ARM::Bcc) {
764 Binary &= 0x0FFFFFFF; // clear conditional field
765 Binary |= getMachineOpValue(MI, 1) << 28; // set conditional field
771 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
772 const TargetInstrDesc &TID = MI.getDesc();
773 if (TID.Opcode == ARM::BX ||
774 TID.Opcode == ARM::BR_JTr ||
775 TID.Opcode == ARM::BR_JTm ||
776 TID.Opcode == ARM::BR_JTadd)
779 // Part of binary is determined by TableGn.
780 unsigned Binary = getBinaryCodeForInstr(MI);
782 // Set the conditional execution predicate
783 Binary |= II->getPredicate(&MI) << 28;
785 if (TID.Opcode == ARM::BX_RET)
786 // The return register is LR.
787 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
789 // otherwise, set the return register
790 Binary |= getMachineOpValue(MI, 0);
795 #include "ARMGenCodeEmitter.inc"