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 addPCLabel(unsigned LabelID);
74 void emitPseudoInstruction(const MachineInstr &MI);
76 unsigned getMachineSoRegOpValue(const MachineInstr &MI,
77 const TargetInstrDesc &TID,
78 const MachineOperand &MO,
81 unsigned getMachineSoImmOpValue(const MachineInstr &MI,
82 const TargetInstrDesc &TID,
83 const MachineOperand &MO);
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 /// getBaseOpcodeFor - Return the opcode value.
120 unsigned getBaseOpcodeFor(const TargetInstrDesc &TID) const {
121 return (TID.TSFlags & ARMII::OpcodeMask) >> ARMII::OpcodeShift;
124 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
126 unsigned getShiftOp(unsigned Imm) const ;
128 /// Routines that handle operands which add machine relocations which are
129 /// fixed up by the JIT fixup stage.
130 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
132 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
133 void emitConstPoolAddress(unsigned CPI, unsigned Reloc,
134 int Disp = 0, unsigned PCAdj = 0 );
135 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc,
137 void emitGlobalConstant(const Constant *CV);
138 void emitMachineBasicBlock(MachineBasicBlock *BB);
140 char ARMCodeEmitter::ID = 0;
143 /// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
144 /// to the specified MCE object.
145 FunctionPass *llvm::createARMCodeEmitterPass(ARMTargetMachine &TM,
146 MachineCodeEmitter &MCE) {
147 return new ARMCodeEmitter(TM, MCE);
150 bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
151 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
152 MF.getTarget().getRelocationModel() != Reloc::Static) &&
153 "JIT relocation model must be set to static or default!");
154 II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo();
155 TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData();
156 JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo();
157 MCPEs = &MF.getConstantPool()->getConstants();
158 JTI->Initialize(MCPEs);
161 DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
162 MCE.startFunction(MF);
163 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
165 MCE.StartMachineBasicBlock(MBB);
166 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
170 } while (MCE.finishFunction(MF));
175 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
177 unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
178 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
179 default: assert(0 && "Unknown shift opc!");
180 case ARM_AM::asr: return 2;
181 case ARM_AM::lsl: return 0;
182 case ARM_AM::lsr: return 1;
184 case ARM_AM::rrx: return 3;
189 /// getMachineOpValue - Return binary encoding of operand. If the machine
190 /// operand requires relocation, record the relocation and return zero.
191 unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
192 const MachineOperand &MO) {
194 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
196 return static_cast<unsigned>(MO.getImm());
197 else if (MO.isGlobal())
198 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true);
199 else if (MO.isSymbol())
200 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_relative);
202 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
204 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
206 emitMachineBasicBlock(MO.getMBB());
208 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
214 /// emitGlobalAddress - Emit the specified address to the code stream.
216 void ARMCodeEmitter::emitGlobalAddress(GlobalValue *GV,
217 unsigned Reloc, bool NeedStub) {
218 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
219 Reloc, GV, 0, NeedStub));
222 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
223 /// be emitted to the current location in the function, and allow it to be PC
225 void ARMCodeEmitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
226 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
230 /// emitConstPoolAddress - Arrange for the address of an constant pool
231 /// to be emitted to the current location in the function, and allow it to be PC
233 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
235 unsigned PCAdj /* = 0 */) {
236 // Tell JIT emitter we'll resolve the address.
237 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
238 Reloc, CPI, PCAdj, true));
241 /// emitJumpTableAddress - Arrange for the address of a jump table to
242 /// be emitted to the current location in the function, and allow it to be PC
244 void ARMCodeEmitter::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc,
245 unsigned PCAdj /* = 0 */) {
246 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
247 Reloc, JTIndex, PCAdj));
250 /// emitMachineBasicBlock - Emit the specified address basic block.
251 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB) {
252 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
253 ARM::reloc_arm_branch, BB));
256 void ARMCodeEmitter::emitWordLE(unsigned Binary) {
257 DOUT << "\t" << (void*)Binary << "\n";
258 MCE.emitWordLE(Binary);
261 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
262 DOUT << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI;
264 NumEmitted++; // Keep track of the # of mi's emitted
265 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
267 assert(0 && "Unhandled instruction encoding format!");
270 emitPseudoInstruction(MI);
273 case ARMII::DPSoRegFrm:
274 emitDataProcessingInstruction(MI);
278 emitLoadStoreInstruction(MI);
280 case ARMII::LdMiscFrm:
281 case ARMII::StMiscFrm:
282 emitMiscLoadStoreInstruction(MI);
284 case ARMII::LdMulFrm:
285 case ARMII::StMulFrm:
286 emitLoadStoreMultipleInstruction(MI);
289 emitMulFrmInstruction(MI);
292 emitBranchInstruction(MI);
294 case ARMII::BranchMisc:
295 emitMiscBranchInstruction(MI);
300 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
301 unsigned CPI = MI.getOperand(0).getImm();
302 unsigned CPIndex = MI.getOperand(1).getIndex();
303 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
305 // Remember the CONSTPOOL_ENTRY address for later relocation.
306 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
308 // Emit constpool island entry. In most cases, the actual values will be
309 // resolved and relocated after code emission.
310 if (MCPE.isMachineConstantPoolEntry()) {
311 ARMConstantPoolValue *ACPV =
312 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
314 DOUT << "\t** ARM constant pool #" << CPI << " @ "
315 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << "\n";
317 GlobalValue *GV = ACPV->getGV();
319 assert(!ACPV->isStub() && "Don't know how to deal this yet!");
320 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
321 ARM::reloc_arm_machine_cp_entry,
322 GV, CPIndex, false));
324 assert(!ACPV->isNonLazyPointer() && "Don't know how to deal this yet!");
325 emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
329 Constant *CV = MCPE.Val.ConstVal;
331 DOUT << "\t** Constant pool #" << CPI << " @ "
332 << (void*)MCE.getCurrentPCValue() << " " << *CV << "\n";
334 if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
335 emitGlobalAddress(GV, ARM::reloc_arm_absolute, false);
338 assert(CV->getType()->isInteger() &&
339 "Not expecting non-integer constpool entries yet!");
340 const ConstantInt *CI = dyn_cast<ConstantInt>(CV);
341 uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
347 void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
348 DOUT << "\t** LPC" << LabelID << " @ "
349 << (void*)MCE.getCurrentPCValue() << '\n';
350 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
353 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
354 unsigned Opcode = MI.getDesc().Opcode;
358 case ARM::CONSTPOOL_ENTRY:
359 emitConstPoolInstruction(MI);
362 // Remember of the address of the PC label for relocation later.
363 addPCLabel(MI.getOperand(2).getImm());
364 // PICADD is just an add instruction that implicitly read pc.
365 emitDataProcessingInstruction(MI, ARM::PC);
372 // Remember of the address of the PC label for relocation later.
373 addPCLabel(MI.getOperand(2).getImm());
374 // These are just load / store instructions that implicitly read pc.
375 emitLoadStoreInstruction(MI, ARM::PC);
382 // Remember of the address of the PC label for relocation later.
383 addPCLabel(MI.getOperand(2).getImm());
384 // These are just load / store instructions that implicitly read pc.
385 emitMiscLoadStoreInstruction(MI, ARM::PC);
392 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
393 const TargetInstrDesc &TID,
394 const MachineOperand &MO,
396 unsigned Binary = getMachineOpValue(MI, MO);
398 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
399 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
400 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
402 // Encode the shift opcode.
404 unsigned Rs = MO1.getReg();
406 // Set shift operand (bit[7:4]).
411 // RRX - 0110 and bit[11:8] clear.
413 default: assert(0 && "Unknown shift opc!");
414 case ARM_AM::lsl: SBits = 0x1; break;
415 case ARM_AM::lsr: SBits = 0x3; break;
416 case ARM_AM::asr: SBits = 0x5; break;
417 case ARM_AM::ror: SBits = 0x7; break;
418 case ARM_AM::rrx: SBits = 0x6; break;
421 // Set shift operand (bit[6:4]).
427 default: assert(0 && "Unknown shift opc!");
428 case ARM_AM::lsl: SBits = 0x0; break;
429 case ARM_AM::lsr: SBits = 0x2; break;
430 case ARM_AM::asr: SBits = 0x4; break;
431 case ARM_AM::ror: SBits = 0x6; break;
434 Binary |= SBits << 4;
435 if (SOpc == ARM_AM::rrx)
438 // Encode the shift operation Rs or shift_imm (except rrx).
440 // Encode Rs bit[11:8].
441 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
443 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
446 // Encode shift_imm bit[11:7].
447 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
450 unsigned ARMCodeEmitter::getMachineSoImmOpValue(const MachineInstr &MI,
451 const TargetInstrDesc &TID,
452 const MachineOperand &MO) {
453 unsigned SoImm = MO.getImm();
454 // Encode rotate_imm.
455 unsigned Binary = ARM_AM::getSOImmValRot(SoImm) << ARMII::RotImmShift;
457 Binary |= ARM_AM::getSOImmVal(SoImm);
461 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
462 const TargetInstrDesc &TID) const {
463 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
464 const MachineOperand &MO = MI.getOperand(i-1);
465 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
466 return 1 << ARMII::S_BitShift;
471 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
472 unsigned ImplicitRn) {
473 const TargetInstrDesc &TID = MI.getDesc();
474 if (TID.getOpcode() == ARM::MOVi2pieces)
477 // Part of binary is determined by TableGn.
478 unsigned Binary = getBinaryCodeForInstr(MI);
480 // Set the conditional execution predicate
481 Binary |= II->getPredicate(&MI) << 28;
483 // Encode S bit if MI modifies CPSR.
484 Binary |= getAddrModeSBit(MI, TID);
486 // Encode register def if there is one.
487 unsigned NumDefs = TID.getNumDefs();
490 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdShift;
494 // Encode first non-shifter register operand if there is one.
495 bool isUnary = TID.TSFlags & ARMII::UnaryDP;
498 // Special handling for implicit use (e.g. PC).
499 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
500 << ARMII::RegRnShift);
502 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
507 // Encode shifter operand.
508 const MachineOperand &MO = MI.getOperand(OpIdx);
509 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
511 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
516 // Encode register Rm.
517 emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
522 // Set bit I(25) to identify this is the immediate form of <shifter_op>
523 Binary |= 1 << ARMII::I_BitShift;
524 Binary |= getMachineSoImmOpValue(MI, TID, MO);
529 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
530 unsigned ImplicitRn) {
531 const TargetInstrDesc &TID = MI.getDesc();
533 // Part of binary is determined by TableGn.
534 unsigned Binary = getBinaryCodeForInstr(MI);
536 // Set the conditional execution predicate
537 Binary |= II->getPredicate(&MI) << 28;
540 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
542 // Set second operand
545 // Special handling for implicit use (e.g. PC).
546 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
547 << ARMII::RegRnShift);
549 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
553 const MachineOperand &MO2 = MI.getOperand(OpIdx);
554 unsigned AM2Opc = (OpIdx == TID.getNumOperands())
555 ? 0 : MI.getOperand(OpIdx+1).getImm();
557 // Set bit U(23) according to sign of immed value (positive or negative).
558 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
560 if (!MO2.getReg()) { // is immediate
561 if (ARM_AM::getAM2Offset(AM2Opc))
562 // Set the value of offset_12 field
563 Binary |= ARM_AM::getAM2Offset(AM2Opc);
568 // Set bit I(25), because this is not in immediate enconding.
569 Binary |= 1 << ARMII::I_BitShift;
570 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
571 // Set bit[3:0] to the corresponding Rm register
572 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
574 // if this instr is in scaled register offset/index instruction, set
575 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
576 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
577 Binary |= getShiftOp(AM2Opc) << 5; // shift
578 Binary |= ShImm << 7; // shift_immed
584 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
585 unsigned ImplicitRn) {
586 const TargetInstrDesc &TID = MI.getDesc();
588 // Part of binary is determined by TableGn.
589 unsigned Binary = getBinaryCodeForInstr(MI);
591 // Set the conditional execution predicate
592 Binary |= II->getPredicate(&MI) << 28;
595 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
597 // Set second operand
600 // Special handling for implicit use (e.g. PC).
601 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
602 << ARMII::RegRnShift);
604 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
608 const MachineOperand &MO2 = MI.getOperand(OpIdx);
609 unsigned AM3Opc = (OpIdx == TID.getNumOperands())
610 ? 0 : MI.getOperand(OpIdx+1).getImm();
612 // Set bit U(23) according to sign of immed value (positive or negative)
613 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
616 // If this instr is in register offset/index encoding, set bit[3:0]
617 // to the corresponding Rm register.
619 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
624 // if this instr is in immediate offset/index encoding, set bit 22 to 1
625 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
628 Binary |= (ImmOffs >> 4) << 8; // immedH
629 Binary |= (ImmOffs & ~0xF); // immedL
635 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
636 // Part of binary is determined by TableGn.
637 unsigned Binary = getBinaryCodeForInstr(MI);
639 // Set the conditional execution predicate
640 Binary |= II->getPredicate(&MI) << 28;
643 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
645 // Set addressing mode by modifying bits U(23) and P(24)
646 // IA - Increment after - bit U = 1 and bit P = 0
647 // IB - Increment before - bit U = 1 and bit P = 1
648 // DA - Decrement after - bit U = 0 and bit P = 0
649 // DB - Decrement before - bit U = 0 and bit P = 1
650 const MachineOperand &MO = MI.getOperand(1);
651 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO.getImm());
653 default: assert(0 && "Unknown addressing sub-mode!");
654 case ARM_AM::da: break;
655 case ARM_AM::db: Binary |= 0x1 << 24; break;
656 case ARM_AM::ia: Binary |= 0x1 << 23; break;
657 case ARM_AM::ib: Binary |= 0x3 << 23; break;
661 if (ARM_AM::getAM4WBFlag(MO.getImm()))
665 for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
666 const MachineOperand &MO = MI.getOperand(i);
667 if (MO.isReg() && MO.isImplicit())
669 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
670 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
672 Binary |= 0x1 << RegNum;
678 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
679 const TargetInstrDesc &TID = MI.getDesc();
681 // Part of binary is determined by TableGn.
682 unsigned Binary = getBinaryCodeForInstr(MI);
684 // Set the conditional execution predicate
685 Binary |= II->getPredicate(&MI) << 28;
687 // Encode S bit if MI modifies CPSR.
688 Binary |= getAddrModeSBit(MI, TID);
690 // 32x32->64bit operations have two destination registers. The number
691 // of register definitions will tell us if that's what we're dealing with.
693 if (TID.getNumDefs() == 2)
694 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
697 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
700 Binary |= getMachineOpValue(MI, OpIdx++);
703 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
705 // Many multiple instructions (e.g. MLA) have three src operands. Encode
706 // it as Rn (for multiply, that's in the same offset as RdLo.
707 if (TID.getNumOperands() - TID.getNumDefs() == 3)
708 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdLoShift;
713 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
714 const TargetInstrDesc &TID = MI.getDesc();
716 // Part of binary is determined by TableGn.
717 unsigned Binary = getBinaryCodeForInstr(MI);
719 // Set the conditional execution predicate
720 Binary |= II->getPredicate(&MI) << 28;
722 // Set signed_immed_24 field
723 Binary |= getMachineOpValue(MI, 0);
725 // if it is a conditional branch, set cond field
726 if (TID.Opcode == ARM::Bcc) {
727 Binary &= 0x0FFFFFFF; // clear conditional field
728 Binary |= getMachineOpValue(MI, 1) << 28; // set conditional field
734 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
735 const TargetInstrDesc &TID = MI.getDesc();
736 if (TID.Opcode == ARM::BX)
739 // Part of binary is determined by TableGn.
740 unsigned Binary = getBinaryCodeForInstr(MI);
742 // Set the conditional execution predicate
743 Binary |= II->getPredicate(&MI) << 28;
745 if (TID.Opcode == ARM::BX_RET)
746 // The return register is LR.
747 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
749 // otherwise, set the return register
750 Binary |= getMachineOpValue(MI, 0);
755 #include "ARMGenCodeEmitter.inc"