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 emitDWordLE(uint64_t Binary);
80 void emitConstPoolInstruction(const MachineInstr &MI);
82 void emitMOVi2piecesInstruction(const MachineInstr &MI);
84 void emitLEApcrelJTInstruction(const MachineInstr &MI);
86 void addPCLabel(unsigned LabelID);
88 void emitPseudoInstruction(const MachineInstr &MI);
90 unsigned getMachineSoRegOpValue(const MachineInstr &MI,
91 const TargetInstrDesc &TID,
92 const MachineOperand &MO,
95 unsigned getMachineSoImmOpValue(unsigned SoImm);
97 unsigned getAddrModeSBit(const MachineInstr &MI,
98 const TargetInstrDesc &TID) const;
100 void emitDataProcessingInstruction(const MachineInstr &MI,
101 unsigned ImplicitRd = 0,
102 unsigned ImplicitRn = 0);
104 void emitLoadStoreInstruction(const MachineInstr &MI,
105 unsigned ImplicitRd = 0,
106 unsigned ImplicitRn = 0);
108 void emitMiscLoadStoreInstruction(const MachineInstr &MI,
109 unsigned ImplicitRn = 0);
111 void emitLoadStoreMultipleInstruction(const MachineInstr &MI);
113 void emitMulFrmInstruction(const MachineInstr &MI);
115 void emitExtendInstruction(const MachineInstr &MI);
117 void emitMiscArithInstruction(const MachineInstr &MI);
119 void emitBranchInstruction(const MachineInstr &MI);
121 void emitInlineJumpTable(unsigned JTIndex);
123 void emitMiscBranchInstruction(const MachineInstr &MI);
125 void emitVFPArithInstruction(const MachineInstr &MI);
127 void emitVFPConversionInstruction(const MachineInstr &MI);
129 void emitVFPLoadStoreInstruction(const MachineInstr &MI);
131 void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI);
133 void emitMiscInstruction(const MachineInstr &MI);
135 /// getBinaryCodeForInstr - This function, generated by the
136 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
137 /// machine instructions.
139 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
141 /// getMachineOpValue - Return binary encoding of operand. If the machine
142 /// operand requires relocation, record the relocation and return zero.
143 unsigned getMachineOpValue(const MachineInstr &MI,const MachineOperand &MO);
144 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) {
145 return getMachineOpValue(MI, MI.getOperand(OpIdx));
148 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
150 unsigned getShiftOp(unsigned Imm) const ;
152 /// Routines that handle operands which add machine relocations which are
153 /// fixed up by the relocation stage.
154 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
155 bool NeedStub, intptr_t ACPV = 0);
156 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
157 void emitConstPoolAddress(unsigned CPI, unsigned Reloc);
158 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc);
159 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
160 intptr_t JTBase = 0);
162 char ARMCodeEmitter::ID = 0;
165 /// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
166 /// to the specified MCE object.
167 FunctionPass *llvm::createARMCodeEmitterPass(ARMTargetMachine &TM,
168 MachineCodeEmitter &MCE) {
169 return new ARMCodeEmitter(TM, MCE);
172 bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
173 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
174 MF.getTarget().getRelocationModel() != Reloc::Static) &&
175 "JIT relocation model must be set to static or default!");
176 II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo();
177 TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData();
178 JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo();
179 MCPEs = &MF.getConstantPool()->getConstants();
180 MJTEs = &MF.getJumpTableInfo()->getJumpTables();
181 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
182 JTI->Initialize(MF, IsPIC);
185 DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
186 MCE.startFunction(MF);
187 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
189 MCE.StartMachineBasicBlock(MBB);
190 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
194 } while (MCE.finishFunction(MF));
199 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
201 unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
202 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
203 default: assert(0 && "Unknown shift opc!");
204 case ARM_AM::asr: return 2;
205 case ARM_AM::lsl: return 0;
206 case ARM_AM::lsr: return 1;
208 case ARM_AM::rrx: return 3;
213 /// getMachineOpValue - Return binary encoding of operand. If the machine
214 /// operand requires relocation, record the relocation and return zero.
215 unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
216 const MachineOperand &MO) {
218 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
220 return static_cast<unsigned>(MO.getImm());
221 else if (MO.isGlobal())
222 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true);
223 else if (MO.isSymbol())
224 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
225 else if (MO.isCPI()) {
226 const TargetInstrDesc &TID = MI.getDesc();
227 // For VFP load, the immediate offset is multiplied by 4.
228 unsigned Reloc = ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
229 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
230 emitConstPoolAddress(MO.getIndex(), Reloc);
231 } else if (MO.isJTI())
232 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
234 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
236 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
242 /// emitGlobalAddress - Emit the specified address to the code stream.
244 void ARMCodeEmitter::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
245 bool NeedStub, intptr_t ACPV) {
246 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
247 Reloc, GV, ACPV, NeedStub));
250 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
251 /// be emitted to the current location in the function, and allow it to be PC
253 void ARMCodeEmitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
254 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
258 /// emitConstPoolAddress - Arrange for the address of an constant pool
259 /// to be emitted to the current location in the function, and allow it to be PC
261 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) {
262 // Tell JIT emitter we'll resolve the address.
263 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
264 Reloc, CPI, 0, true));
267 /// emitJumpTableAddress - Arrange for the address of a jump table to
268 /// be emitted to the current location in the function, and allow it to be PC
270 void ARMCodeEmitter::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) {
271 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
272 Reloc, JTIndex, 0, true));
275 /// emitMachineBasicBlock - Emit the specified address basic block.
276 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
277 unsigned Reloc, intptr_t JTBase) {
278 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
282 void ARMCodeEmitter::emitWordLE(unsigned Binary) {
284 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
285 << Binary << std::dec << "\n";
287 MCE.emitWordLE(Binary);
290 void ARMCodeEmitter::emitDWordLE(uint64_t Binary) {
292 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
293 << (unsigned)Binary << std::dec << "\n";
294 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
295 << (unsigned)(Binary >> 32) << std::dec << "\n";
297 MCE.emitDWordLE(Binary);
300 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
301 DOUT << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI;
303 NumEmitted++; // Keep track of the # of mi's emitted
304 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
306 assert(0 && "Unhandled instruction encoding format!");
309 emitPseudoInstruction(MI);
312 case ARMII::DPSoRegFrm:
313 emitDataProcessingInstruction(MI);
317 emitLoadStoreInstruction(MI);
319 case ARMII::LdMiscFrm:
320 case ARMII::StMiscFrm:
321 emitMiscLoadStoreInstruction(MI);
323 case ARMII::LdStMulFrm:
324 emitLoadStoreMultipleInstruction(MI);
327 emitMulFrmInstruction(MI);
330 emitExtendInstruction(MI);
332 case ARMII::ArithMiscFrm:
333 emitMiscArithInstruction(MI);
336 emitBranchInstruction(MI);
338 case ARMII::BrMiscFrm:
339 emitMiscBranchInstruction(MI);
342 case ARMII::VFPUnaryFrm:
343 case ARMII::VFPBinaryFrm:
344 emitVFPArithInstruction(MI);
346 case ARMII::VFPConv1Frm:
347 case ARMII::VFPConv2Frm:
348 case ARMII::VFPConv3Frm:
349 case ARMII::VFPConv4Frm:
350 case ARMII::VFPConv5Frm:
351 emitVFPConversionInstruction(MI);
353 case ARMII::VFPLdStFrm:
354 emitVFPLoadStoreInstruction(MI);
356 case ARMII::VFPLdStMulFrm:
357 emitVFPLoadStoreMultipleInstruction(MI);
359 case ARMII::VFPMiscFrm:
360 emitMiscInstruction(MI);
365 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
366 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
367 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
368 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
370 // Remember the CONSTPOOL_ENTRY address for later relocation.
371 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
373 // Emit constpool island entry. In most cases, the actual values will be
374 // resolved and relocated after code emission.
375 if (MCPE.isMachineConstantPoolEntry()) {
376 ARMConstantPoolValue *ACPV =
377 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
379 DOUT << " ** ARM constant pool #" << CPI << " @ "
380 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n';
382 GlobalValue *GV = ACPV->getGV();
384 assert(!ACPV->isStub() && "Don't know how to deal this yet!");
385 if (ACPV->isNonLazyPointer())
386 MCE.addRelocation(MachineRelocation::getIndirectSymbol(
387 MCE.getCurrentPCOffset(), ARM::reloc_arm_machine_cp_entry, GV,
388 (intptr_t)ACPV, false));
390 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
391 ACPV->isStub(), (intptr_t)ACPV);
393 assert(!ACPV->isNonLazyPointer() && "Don't know how to deal this yet!");
394 emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
398 Constant *CV = MCPE.Val.ConstVal;
400 DOUT << " ** Constant pool #" << CPI << " @ "
401 << (void*)MCE.getCurrentPCValue() << " " << *CV << '\n';
403 if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
404 emitGlobalAddress(GV, ARM::reloc_arm_absolute, false);
406 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
407 uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
409 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
410 if (CFP->getType() == Type::FloatTy)
411 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
412 else if (CFP->getType() == Type::DoubleTy)
413 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
415 assert(0 && "Unable to handle this constantpool entry!");
419 assert(0 && "Unable to handle this constantpool entry!");
425 void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
426 const MachineOperand &MO0 = MI.getOperand(0);
427 const MachineOperand &MO1 = MI.getOperand(1);
428 assert(MO1.isImm() && "Not a valid so_imm value!");
429 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
430 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
432 // Emit the 'mov' instruction.
433 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
435 // Set the conditional execution predicate.
436 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
439 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
442 // Set bit I(25) to identify this is the immediate form of <shifter_op>
443 Binary |= 1 << ARMII::I_BitShift;
444 Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V1));
447 // Now the 'orr' instruction.
448 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
450 // Set the conditional execution predicate.
451 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
454 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
457 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
460 // Set bit I(25) to identify this is the immediate form of <shifter_op>
461 Binary |= 1 << ARMII::I_BitShift;
462 Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V2));
466 void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
467 // It's basically add r, pc, (LJTI - $+8)
469 const TargetInstrDesc &TID = MI.getDesc();
471 // Emit the 'add' instruction.
472 unsigned Binary = 0x4 << 21; // add: Insts{24-31} = 0b0100
474 // Set the conditional execution predicate
475 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
477 // Encode S bit if MI modifies CPSR.
478 Binary |= getAddrModeSBit(MI, TID);
481 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
483 // Encode Rn which is PC.
484 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
486 // Encode the displacement.
487 // Set bit I(25) to identify this is the immediate form of <shifter_op>.
488 Binary |= 1 << ARMII::I_BitShift;
489 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
494 void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
495 DOUT << " ** LPC" << LabelID << " @ "
496 << (void*)MCE.getCurrentPCValue() << '\n';
497 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
500 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
501 unsigned Opcode = MI.getDesc().Opcode;
505 case ARM::CONSTPOOL_ENTRY:
506 emitConstPoolInstruction(MI);
509 // Remember of the address of the PC label for relocation later.
510 addPCLabel(MI.getOperand(2).getImm());
511 // PICADD is just an add instruction that implicitly read pc.
512 emitDataProcessingInstruction(MI, 0, ARM::PC);
519 // Remember of the address of the PC label for relocation later.
520 addPCLabel(MI.getOperand(2).getImm());
521 // These are just load / store instructions that implicitly read pc.
522 emitLoadStoreInstruction(MI, 0, ARM::PC);
529 // Remember of the address of the PC label for relocation later.
530 addPCLabel(MI.getOperand(2).getImm());
531 // These are just load / store instructions that implicitly read pc.
532 emitMiscLoadStoreInstruction(MI, ARM::PC);
535 case ARM::MOVi2pieces:
536 // Two instructions to materialize a constant.
537 emitMOVi2piecesInstruction(MI);
539 case ARM::LEApcrelJT:
540 // Materialize jumptable address.
541 emitLEApcrelJTInstruction(MI);
547 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
548 const TargetInstrDesc &TID,
549 const MachineOperand &MO,
551 unsigned Binary = getMachineOpValue(MI, MO);
553 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
554 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
555 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
557 // Encode the shift opcode.
559 unsigned Rs = MO1.getReg();
561 // Set shift operand (bit[7:4]).
566 // RRX - 0110 and bit[11:8] clear.
568 default: assert(0 && "Unknown shift opc!");
569 case ARM_AM::lsl: SBits = 0x1; break;
570 case ARM_AM::lsr: SBits = 0x3; break;
571 case ARM_AM::asr: SBits = 0x5; break;
572 case ARM_AM::ror: SBits = 0x7; break;
573 case ARM_AM::rrx: SBits = 0x6; break;
576 // Set shift operand (bit[6:4]).
582 default: assert(0 && "Unknown shift opc!");
583 case ARM_AM::lsl: SBits = 0x0; break;
584 case ARM_AM::lsr: SBits = 0x2; break;
585 case ARM_AM::asr: SBits = 0x4; break;
586 case ARM_AM::ror: SBits = 0x6; break;
589 Binary |= SBits << 4;
590 if (SOpc == ARM_AM::rrx)
593 // Encode the shift operation Rs or shift_imm (except rrx).
595 // Encode Rs bit[11:8].
596 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
598 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
601 // Encode shift_imm bit[11:7].
602 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
605 unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
606 // Encode rotate_imm.
607 unsigned Binary = (ARM_AM::getSOImmValRot(SoImm) >> 1)
608 << ARMII::SoRotImmShift;
611 Binary |= ARM_AM::getSOImmValImm(SoImm);
615 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
616 const TargetInstrDesc &TID) const {
617 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
618 const MachineOperand &MO = MI.getOperand(i-1);
619 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
620 return 1 << ARMII::S_BitShift;
625 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
627 unsigned ImplicitRn) {
628 const TargetInstrDesc &TID = MI.getDesc();
630 // Part of binary is determined by TableGn.
631 unsigned Binary = getBinaryCodeForInstr(MI);
633 // Set the conditional execution predicate
634 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
636 // Encode S bit if MI modifies CPSR.
637 Binary |= getAddrModeSBit(MI, TID);
639 // Encode register def if there is one.
640 unsigned NumDefs = TID.getNumDefs();
643 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
645 // Special handling for implicit use (e.g. PC).
646 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
647 << ARMII::RegRdShift);
649 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
650 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
653 // Encode first non-shifter register operand if there is one.
654 bool isUnary = TID.TSFlags & ARMII::UnaryDP;
657 // Special handling for implicit use (e.g. PC).
658 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
659 << ARMII::RegRnShift);
661 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
666 // Encode shifter operand.
667 const MachineOperand &MO = MI.getOperand(OpIdx);
668 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
670 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
675 // Encode register Rm.
676 emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
681 // Set bit I(25) to identify this is the immediate form of <shifter_op>.
682 Binary |= 1 << ARMII::I_BitShift;
683 Binary |= getMachineSoImmOpValue(MO.getImm());
688 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
690 unsigned ImplicitRn) {
691 const TargetInstrDesc &TID = MI.getDesc();
692 unsigned Form = TID.TSFlags & ARMII::FormMask;
693 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
695 // Part of binary is determined by TableGn.
696 unsigned Binary = getBinaryCodeForInstr(MI);
698 // Set the conditional execution predicate
699 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
703 // Operand 0 of a pre- and post-indexed store is the address base
704 // writeback. Skip it.
705 bool Skipped = false;
706 if (IsPrePost && Form == ARMII::StFrm) {
713 // Special handling for implicit use (e.g. PC).
714 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
715 << ARMII::RegRdShift);
717 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
719 // Set second operand
721 // Special handling for implicit use (e.g. PC).
722 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
723 << ARMII::RegRnShift);
725 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
727 // If this is a two-address operand, skip it. e.g. LDR_PRE.
728 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
731 const MachineOperand &MO2 = MI.getOperand(OpIdx);
732 unsigned AM2Opc = (ImplicitRn == ARM::PC)
733 ? 0 : MI.getOperand(OpIdx+1).getImm();
735 // Set bit U(23) according to sign of immed value (positive or negative).
736 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
738 if (!MO2.getReg()) { // is immediate
739 if (ARM_AM::getAM2Offset(AM2Opc))
740 // Set the value of offset_12 field
741 Binary |= ARM_AM::getAM2Offset(AM2Opc);
746 // Set bit I(25), because this is not in immediate enconding.
747 Binary |= 1 << ARMII::I_BitShift;
748 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
749 // Set bit[3:0] to the corresponding Rm register
750 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
752 // If this instr is in scaled register offset/index instruction, set
753 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
754 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
755 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
756 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
762 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
763 unsigned ImplicitRn) {
764 const TargetInstrDesc &TID = MI.getDesc();
765 unsigned Form = TID.TSFlags & ARMII::FormMask;
766 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
768 // Part of binary is determined by TableGn.
769 unsigned Binary = getBinaryCodeForInstr(MI);
771 // Set the conditional execution predicate
772 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
776 // Operand 0 of a pre- and post-indexed store is the address base
777 // writeback. Skip it.
778 bool Skipped = false;
779 if (IsPrePost && Form == ARMII::StMiscFrm) {
785 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
787 // Set second operand
789 // Special handling for implicit use (e.g. PC).
790 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
791 << ARMII::RegRnShift);
793 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
795 // If this is a two-address operand, skip it. e.g. LDRH_POST.
796 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
799 const MachineOperand &MO2 = MI.getOperand(OpIdx);
800 unsigned AM3Opc = (ImplicitRn == ARM::PC)
801 ? 0 : MI.getOperand(OpIdx+1).getImm();
803 // Set bit U(23) according to sign of immed value (positive or negative)
804 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
807 // If this instr is in register offset/index encoding, set bit[3:0]
808 // to the corresponding Rm register.
810 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
815 // This instr is in immediate offset/index encoding, set bit 22 to 1.
816 Binary |= 1 << ARMII::AM3_I_BitShift;
817 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
819 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
820 Binary |= (ImmOffs & 0xF); // immedL
826 static unsigned getAddrModeUPBits(unsigned Mode) {
829 // Set addressing mode by modifying bits U(23) and P(24)
830 // IA - Increment after - bit U = 1 and bit P = 0
831 // IB - Increment before - bit U = 1 and bit P = 1
832 // DA - Decrement after - bit U = 0 and bit P = 0
833 // DB - Decrement before - bit U = 0 and bit P = 1
835 default: assert(0 && "Unknown addressing sub-mode!");
836 case ARM_AM::da: break;
837 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
838 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
839 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
845 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
846 // Part of binary is determined by TableGn.
847 unsigned Binary = getBinaryCodeForInstr(MI);
849 // Set the conditional execution predicate
850 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
852 // Set base address operand
853 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
855 // Set addressing mode by modifying bits U(23) and P(24)
856 const MachineOperand &MO = MI.getOperand(1);
857 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
860 if (ARM_AM::getAM4WBFlag(MO.getImm()))
861 Binary |= 0x1 << ARMII::W_BitShift;
864 for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
865 const MachineOperand &MO = MI.getOperand(i);
866 if (!MO.isReg() || MO.isImplicit())
868 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
869 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
871 Binary |= 0x1 << RegNum;
877 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
878 const TargetInstrDesc &TID = MI.getDesc();
880 // Part of binary is determined by TableGn.
881 unsigned Binary = getBinaryCodeForInstr(MI);
883 // Set the conditional execution predicate
884 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
886 // Encode S bit if MI modifies CPSR.
887 Binary |= getAddrModeSBit(MI, TID);
889 // 32x32->64bit operations have two destination registers. The number
890 // of register definitions will tell us if that's what we're dealing with.
892 if (TID.getNumDefs() == 2)
893 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
896 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
899 Binary |= getMachineOpValue(MI, OpIdx++);
902 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
904 // Many multiple instructions (e.g. MLA) have three src operands. Encode
905 // it as Rn (for multiply, that's in the same offset as RdLo.
906 if (TID.getNumOperands() > OpIdx &&
907 !TID.OpInfo[OpIdx].isPredicate() &&
908 !TID.OpInfo[OpIdx].isOptionalDef())
909 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
914 void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
915 const TargetInstrDesc &TID = MI.getDesc();
917 // Part of binary is determined by TableGn.
918 unsigned Binary = getBinaryCodeForInstr(MI);
920 // Set the conditional execution predicate
921 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
926 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
928 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
929 const MachineOperand &MO2 = MI.getOperand(OpIdx);
931 // Two register operand form.
933 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
936 Binary |= getMachineOpValue(MI, MO2);
939 Binary |= getMachineOpValue(MI, MO1);
942 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
943 if (MI.getOperand(OpIdx).isImm() &&
944 !TID.OpInfo[OpIdx].isPredicate() &&
945 !TID.OpInfo[OpIdx].isOptionalDef())
946 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
951 void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
952 const TargetInstrDesc &TID = MI.getDesc();
954 // Part of binary is determined by TableGn.
955 unsigned Binary = getBinaryCodeForInstr(MI);
957 // Set the conditional execution predicate
958 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
963 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
965 const MachineOperand &MO = MI.getOperand(OpIdx++);
966 if (OpIdx == TID.getNumOperands() ||
967 TID.OpInfo[OpIdx].isPredicate() ||
968 TID.OpInfo[OpIdx].isOptionalDef()) {
969 // Encode Rm and it's done.
970 Binary |= getMachineOpValue(MI, MO);
976 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
979 Binary |= getMachineOpValue(MI, OpIdx++);
982 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
983 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
984 Binary |= ShiftAmt << ARMII::ShiftShift;
989 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
990 const TargetInstrDesc &TID = MI.getDesc();
992 if (TID.Opcode == ARM::TPsoft)
995 // Part of binary is determined by TableGn.
996 unsigned Binary = getBinaryCodeForInstr(MI);
998 // Set the conditional execution predicate
999 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1001 // Set signed_immed_24 field
1002 Binary |= getMachineOpValue(MI, 0);
1007 void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
1008 // Remember the base address of the inline jump table.
1009 intptr_t JTBase = MCE.getCurrentPCValue();
1010 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
1011 DOUT << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase << '\n';
1013 // Now emit the jump table entries.
1014 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1015 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1017 // DestBB address - JT base.
1018 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
1020 // Absolute DestBB address.
1021 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1026 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
1027 const TargetInstrDesc &TID = MI.getDesc();
1029 // Handle jump tables.
1030 if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
1031 // First emit a ldr pc, [] instruction.
1032 emitDataProcessingInstruction(MI, ARM::PC);
1034 // Then emit the inline jump table.
1035 unsigned JTIndex = (TID.Opcode == ARM::BR_JTr)
1036 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1037 emitInlineJumpTable(JTIndex);
1039 } else if (TID.Opcode == ARM::BR_JTm) {
1040 // First emit a ldr pc, [] instruction.
1041 emitLoadStoreInstruction(MI, ARM::PC);
1043 // Then emit the inline jump table.
1044 emitInlineJumpTable(MI.getOperand(3).getIndex());
1048 // Part of binary is determined by TableGn.
1049 unsigned Binary = getBinaryCodeForInstr(MI);
1051 // Set the conditional execution predicate
1052 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1054 if (TID.Opcode == ARM::BX_RET)
1055 // The return register is LR.
1056 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
1058 // otherwise, set the return register
1059 Binary |= getMachineOpValue(MI, 0);
1064 static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) {
1065 unsigned RegD = MI.getOperand(OpIdx).getReg();
1066 unsigned Binary = 0;
1067 bool isSPVFP = false;
1068 RegD = ARMRegisterInfo::getRegisterNumbering(RegD, isSPVFP);
1070 Binary |= RegD << ARMII::RegRdShift;
1072 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1073 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1078 static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) {
1079 unsigned RegN = MI.getOperand(OpIdx).getReg();
1080 unsigned Binary = 0;
1081 bool isSPVFP = false;
1082 RegN = ARMRegisterInfo::getRegisterNumbering(RegN, isSPVFP);
1084 Binary |= RegN << ARMII::RegRnShift;
1086 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1087 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1092 static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) {
1093 unsigned RegM = MI.getOperand(OpIdx).getReg();
1094 unsigned Binary = 0;
1095 bool isSPVFP = false;
1096 RegM = ARMRegisterInfo::getRegisterNumbering(RegM, isSPVFP);
1100 Binary |= ((RegM & 0x1E) >> 1);
1101 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
1106 void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) {
1107 const TargetInstrDesc &TID = MI.getDesc();
1109 // Part of binary is determined by TableGn.
1110 unsigned Binary = getBinaryCodeForInstr(MI);
1112 // Set the conditional execution predicate
1113 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1116 assert((Binary & ARMII::D_BitShift) == 0 &&
1117 (Binary & ARMII::N_BitShift) == 0 &&
1118 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1121 Binary |= encodeVFPRd(MI, OpIdx++);
1123 // If this is a two-address operand, skip it, e.g. FMACD.
1124 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1128 if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
1129 Binary |= encodeVFPRn(MI, OpIdx++);
1131 if (OpIdx == TID.getNumOperands() ||
1132 TID.OpInfo[OpIdx].isPredicate() ||
1133 TID.OpInfo[OpIdx].isOptionalDef()) {
1134 // FCMPEZD etc. has only one operand.
1140 Binary |= encodeVFPRm(MI, OpIdx);
1145 void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) {
1146 const TargetInstrDesc &TID = MI.getDesc();
1147 unsigned Form = TID.TSFlags & ARMII::FormMask;
1149 // Part of binary is determined by TableGn.
1150 unsigned Binary = getBinaryCodeForInstr(MI);
1152 // Set the conditional execution predicate
1153 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1157 case ARMII::VFPConv1Frm:
1158 case ARMII::VFPConv2Frm:
1159 case ARMII::VFPConv3Frm:
1161 Binary |= encodeVFPRd(MI, 0);
1163 case ARMII::VFPConv4Frm:
1165 Binary |= encodeVFPRn(MI, 0);
1167 case ARMII::VFPConv5Frm:
1169 Binary |= encodeVFPRm(MI, 0);
1175 case ARMII::VFPConv1Frm:
1177 Binary |= encodeVFPRm(MI, 1);
1179 case ARMII::VFPConv2Frm:
1180 case ARMII::VFPConv3Frm:
1182 Binary |= encodeVFPRn(MI, 1);
1184 case ARMII::VFPConv4Frm:
1185 case ARMII::VFPConv5Frm:
1187 Binary |= encodeVFPRd(MI, 1);
1191 if (Form == ARMII::VFPConv5Frm)
1193 Binary |= encodeVFPRn(MI, 2);
1194 else if (Form == ARMII::VFPConv3Frm)
1196 Binary |= encodeVFPRm(MI, 2);
1201 void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
1202 // Part of binary is determined by TableGn.
1203 unsigned Binary = getBinaryCodeForInstr(MI);
1205 // Set the conditional execution predicate
1206 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1211 Binary |= encodeVFPRd(MI, OpIdx++);
1213 // Encode address base.
1214 const MachineOperand &Base = MI.getOperand(OpIdx++);
1215 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1217 // If there is a non-zero immediate offset, encode it.
1219 const MachineOperand &Offset = MI.getOperand(OpIdx);
1220 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1221 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1222 Binary |= 1 << ARMII::U_BitShift;
1229 // If immediate offset is omitted, default to +0.
1230 Binary |= 1 << ARMII::U_BitShift;
1236 ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
1237 // Part of binary is determined by TableGn.
1238 unsigned Binary = getBinaryCodeForInstr(MI);
1240 // Set the conditional execution predicate
1241 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1243 // Set base address operand
1244 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
1246 // Set addressing mode by modifying bits U(23) and P(24)
1247 const MachineOperand &MO = MI.getOperand(1);
1248 Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm()));
1251 if (ARM_AM::getAM5WBFlag(MO.getImm()))
1252 Binary |= 0x1 << ARMII::W_BitShift;
1254 // First register is encoded in Dd.
1255 Binary |= encodeVFPRd(MI, 4);
1257 // Number of registers are encoded in offset field.
1258 unsigned NumRegs = 1;
1259 for (unsigned i = 5, e = MI.getNumOperands(); i != e; ++i) {
1260 const MachineOperand &MO = MI.getOperand(i);
1261 if (!MO.isReg() || MO.isImplicit())
1265 Binary |= NumRegs * 2;
1270 void ARMCodeEmitter::emitMiscInstruction(const MachineInstr &MI) {
1271 // Part of binary is determined by TableGn.
1272 unsigned Binary = getBinaryCodeForInstr(MI);
1274 // Set the conditional execution predicate
1275 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1280 #include "ARMGenCodeEmitter.inc"