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!");
310 emitPseudoInstruction(MI);
313 case ARMII::DPSoRegFrm:
314 emitDataProcessingInstruction(MI);
318 emitLoadStoreInstruction(MI);
320 case ARMII::LdMiscFrm:
321 case ARMII::StMiscFrm:
322 emitMiscLoadStoreInstruction(MI);
324 case ARMII::LdStMulFrm:
325 emitLoadStoreMultipleInstruction(MI);
328 emitMulFrmInstruction(MI);
331 emitExtendInstruction(MI);
333 case ARMII::ArithMiscFrm:
334 emitMiscArithInstruction(MI);
337 emitBranchInstruction(MI);
339 case ARMII::BrMiscFrm:
340 emitMiscBranchInstruction(MI);
343 case ARMII::VFPUnaryFrm:
344 case ARMII::VFPBinaryFrm:
345 emitVFPArithInstruction(MI);
347 case ARMII::VFPConv1Frm:
348 case ARMII::VFPConv2Frm:
349 case ARMII::VFPConv3Frm:
350 case ARMII::VFPConv4Frm:
351 case ARMII::VFPConv5Frm:
352 emitVFPConversionInstruction(MI);
354 case ARMII::VFPLdStFrm:
355 emitVFPLoadStoreInstruction(MI);
357 case ARMII::VFPLdStMulFrm:
358 emitVFPLoadStoreMultipleInstruction(MI);
360 case ARMII::VFPMiscFrm:
361 emitMiscInstruction(MI);
366 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
367 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
368 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
369 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
371 // Remember the CONSTPOOL_ENTRY address for later relocation.
372 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
374 // Emit constpool island entry. In most cases, the actual values will be
375 // resolved and relocated after code emission.
376 if (MCPE.isMachineConstantPoolEntry()) {
377 ARMConstantPoolValue *ACPV =
378 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
380 DOUT << " ** ARM constant pool #" << CPI << " @ "
381 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n';
383 GlobalValue *GV = ACPV->getGV();
385 assert(!ACPV->isStub() && "Don't know how to deal this yet!");
386 if (ACPV->isNonLazyPointer())
387 MCE.addRelocation(MachineRelocation::getIndirectSymbol(
388 MCE.getCurrentPCOffset(), ARM::reloc_arm_machine_cp_entry, GV,
389 (intptr_t)ACPV, false));
391 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
392 ACPV->isStub() || isa<Function>(GV), (intptr_t)ACPV);
394 assert(!ACPV->isNonLazyPointer() && "Don't know how to deal this yet!");
395 emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
399 Constant *CV = MCPE.Val.ConstVal;
402 DOUT << " ** Constant pool #" << CPI << " @ "
403 << (void*)MCE.getCurrentPCValue() << " ";
404 if (const Function *F = dyn_cast<Function>(CV))
405 DOUT << F->getName();
411 if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
412 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV));
414 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
415 uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
417 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
418 if (CFP->getType() == Type::FloatTy)
419 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
420 else if (CFP->getType() == Type::DoubleTy)
421 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
423 assert(0 && "Unable to handle this constantpool entry!");
427 assert(0 && "Unable to handle this constantpool entry!");
433 void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
434 const MachineOperand &MO0 = MI.getOperand(0);
435 const MachineOperand &MO1 = MI.getOperand(1);
436 assert(MO1.isImm() && "Not a valid so_imm value!");
437 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
438 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
440 // Emit the 'mov' instruction.
441 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
443 // Set the conditional execution predicate.
444 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
447 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
450 // Set bit I(25) to identify this is the immediate form of <shifter_op>
451 Binary |= 1 << ARMII::I_BitShift;
452 Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V1));
455 // Now the 'orr' instruction.
456 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
458 // Set the conditional execution predicate.
459 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
462 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
465 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
468 // Set bit I(25) to identify this is the immediate form of <shifter_op>
469 Binary |= 1 << ARMII::I_BitShift;
470 Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V2));
474 void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
475 // It's basically add r, pc, (LJTI - $+8)
477 const TargetInstrDesc &TID = MI.getDesc();
479 // Emit the 'add' instruction.
480 unsigned Binary = 0x4 << 21; // add: Insts{24-31} = 0b0100
482 // Set the conditional execution predicate
483 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
485 // Encode S bit if MI modifies CPSR.
486 Binary |= getAddrModeSBit(MI, TID);
489 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
491 // Encode Rn which is PC.
492 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
494 // Encode the displacement.
495 // Set bit I(25) to identify this is the immediate form of <shifter_op>.
496 Binary |= 1 << ARMII::I_BitShift;
497 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
502 void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
503 DOUT << " ** LPC" << LabelID << " @ "
504 << (void*)MCE.getCurrentPCValue() << '\n';
505 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
508 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
509 unsigned Opcode = MI.getDesc().Opcode;
513 case TargetInstrInfo::INLINEASM: {
514 const char* Value = MI.getOperand(0).getSymbolName();
515 /* We allow inline assembler nodes with empty bodies - they can
516 implicitly define registers, which is ok for JIT. */
517 assert((Value[0] == 0) && "JIT does not support inline asm!\n");
520 case TargetInstrInfo::DBG_LABEL:
521 case TargetInstrInfo::EH_LABEL:
522 MCE.emitLabel(MI.getOperand(0).getImm());
524 case TargetInstrInfo::IMPLICIT_DEF:
525 case TargetInstrInfo::DECLARE:
529 case ARM::CONSTPOOL_ENTRY:
530 emitConstPoolInstruction(MI);
533 // Remember of the address of the PC label for relocation later.
534 addPCLabel(MI.getOperand(2).getImm());
535 // PICADD is just an add instruction that implicitly read pc.
536 emitDataProcessingInstruction(MI, 0, ARM::PC);
543 // Remember of the address of the PC label for relocation later.
544 addPCLabel(MI.getOperand(2).getImm());
545 // These are just load / store instructions that implicitly read pc.
546 emitLoadStoreInstruction(MI, 0, ARM::PC);
553 // Remember of the address of the PC label for relocation later.
554 addPCLabel(MI.getOperand(2).getImm());
555 // These are just load / store instructions that implicitly read pc.
556 emitMiscLoadStoreInstruction(MI, ARM::PC);
559 case ARM::MOVi2pieces:
560 // Two instructions to materialize a constant.
561 emitMOVi2piecesInstruction(MI);
563 case ARM::LEApcrelJT:
564 // Materialize jumptable address.
565 emitLEApcrelJTInstruction(MI);
571 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
572 const TargetInstrDesc &TID,
573 const MachineOperand &MO,
575 unsigned Binary = getMachineOpValue(MI, MO);
577 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
578 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
579 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
581 // Encode the shift opcode.
583 unsigned Rs = MO1.getReg();
585 // Set shift operand (bit[7:4]).
590 // RRX - 0110 and bit[11:8] clear.
592 default: assert(0 && "Unknown shift opc!");
593 case ARM_AM::lsl: SBits = 0x1; break;
594 case ARM_AM::lsr: SBits = 0x3; break;
595 case ARM_AM::asr: SBits = 0x5; break;
596 case ARM_AM::ror: SBits = 0x7; break;
597 case ARM_AM::rrx: SBits = 0x6; break;
600 // Set shift operand (bit[6:4]).
606 default: assert(0 && "Unknown shift opc!");
607 case ARM_AM::lsl: SBits = 0x0; break;
608 case ARM_AM::lsr: SBits = 0x2; break;
609 case ARM_AM::asr: SBits = 0x4; break;
610 case ARM_AM::ror: SBits = 0x6; break;
613 Binary |= SBits << 4;
614 if (SOpc == ARM_AM::rrx)
617 // Encode the shift operation Rs or shift_imm (except rrx).
619 // Encode Rs bit[11:8].
620 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
622 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
625 // Encode shift_imm bit[11:7].
626 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
629 unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
630 // Encode rotate_imm.
631 unsigned Binary = (ARM_AM::getSOImmValRot(SoImm) >> 1)
632 << ARMII::SoRotImmShift;
635 Binary |= ARM_AM::getSOImmValImm(SoImm);
639 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
640 const TargetInstrDesc &TID) const {
641 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
642 const MachineOperand &MO = MI.getOperand(i-1);
643 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
644 return 1 << ARMII::S_BitShift;
649 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
651 unsigned ImplicitRn) {
652 const TargetInstrDesc &TID = MI.getDesc();
654 // Part of binary is determined by TableGn.
655 unsigned Binary = getBinaryCodeForInstr(MI);
657 // Set the conditional execution predicate
658 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
660 // Encode S bit if MI modifies CPSR.
661 Binary |= getAddrModeSBit(MI, TID);
663 // Encode register def if there is one.
664 unsigned NumDefs = TID.getNumDefs();
667 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
669 // Special handling for implicit use (e.g. PC).
670 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
671 << ARMII::RegRdShift);
673 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
674 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
677 // Encode first non-shifter register operand if there is one.
678 bool isUnary = TID.TSFlags & ARMII::UnaryDP;
681 // Special handling for implicit use (e.g. PC).
682 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
683 << ARMII::RegRnShift);
685 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
690 // Encode shifter operand.
691 const MachineOperand &MO = MI.getOperand(OpIdx);
692 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
694 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
699 // Encode register Rm.
700 emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
705 // Set bit I(25) to identify this is the immediate form of <shifter_op>.
706 Binary |= 1 << ARMII::I_BitShift;
707 Binary |= getMachineSoImmOpValue(MO.getImm());
712 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
714 unsigned ImplicitRn) {
715 const TargetInstrDesc &TID = MI.getDesc();
716 unsigned Form = TID.TSFlags & ARMII::FormMask;
717 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
719 // Part of binary is determined by TableGn.
720 unsigned Binary = getBinaryCodeForInstr(MI);
722 // Set the conditional execution predicate
723 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
727 // Operand 0 of a pre- and post-indexed store is the address base
728 // writeback. Skip it.
729 bool Skipped = false;
730 if (IsPrePost && Form == ARMII::StFrm) {
737 // Special handling for implicit use (e.g. PC).
738 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
739 << ARMII::RegRdShift);
741 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
743 // Set second operand
745 // Special handling for implicit use (e.g. PC).
746 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
747 << ARMII::RegRnShift);
749 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
751 // If this is a two-address operand, skip it. e.g. LDR_PRE.
752 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
755 const MachineOperand &MO2 = MI.getOperand(OpIdx);
756 unsigned AM2Opc = (ImplicitRn == ARM::PC)
757 ? 0 : MI.getOperand(OpIdx+1).getImm();
759 // Set bit U(23) according to sign of immed value (positive or negative).
760 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
762 if (!MO2.getReg()) { // is immediate
763 if (ARM_AM::getAM2Offset(AM2Opc))
764 // Set the value of offset_12 field
765 Binary |= ARM_AM::getAM2Offset(AM2Opc);
770 // Set bit I(25), because this is not in immediate enconding.
771 Binary |= 1 << ARMII::I_BitShift;
772 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
773 // Set bit[3:0] to the corresponding Rm register
774 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
776 // If this instr is in scaled register offset/index instruction, set
777 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
778 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
779 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
780 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
786 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
787 unsigned ImplicitRn) {
788 const TargetInstrDesc &TID = MI.getDesc();
789 unsigned Form = TID.TSFlags & ARMII::FormMask;
790 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
792 // Part of binary is determined by TableGn.
793 unsigned Binary = getBinaryCodeForInstr(MI);
795 // Set the conditional execution predicate
796 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
800 // Operand 0 of a pre- and post-indexed store is the address base
801 // writeback. Skip it.
802 bool Skipped = false;
803 if (IsPrePost && Form == ARMII::StMiscFrm) {
809 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
811 // Set second operand
813 // Special handling for implicit use (e.g. PC).
814 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
815 << ARMII::RegRnShift);
817 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
819 // If this is a two-address operand, skip it. e.g. LDRH_POST.
820 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
823 const MachineOperand &MO2 = MI.getOperand(OpIdx);
824 unsigned AM3Opc = (ImplicitRn == ARM::PC)
825 ? 0 : MI.getOperand(OpIdx+1).getImm();
827 // Set bit U(23) according to sign of immed value (positive or negative)
828 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
831 // If this instr is in register offset/index encoding, set bit[3:0]
832 // to the corresponding Rm register.
834 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
839 // This instr is in immediate offset/index encoding, set bit 22 to 1.
840 Binary |= 1 << ARMII::AM3_I_BitShift;
841 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
843 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
844 Binary |= (ImmOffs & 0xF); // immedL
850 static unsigned getAddrModeUPBits(unsigned Mode) {
853 // Set addressing mode by modifying bits U(23) and P(24)
854 // IA - Increment after - bit U = 1 and bit P = 0
855 // IB - Increment before - bit U = 1 and bit P = 1
856 // DA - Decrement after - bit U = 0 and bit P = 0
857 // DB - Decrement before - bit U = 0 and bit P = 1
859 default: assert(0 && "Unknown addressing sub-mode!");
860 case ARM_AM::da: break;
861 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
862 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
863 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
869 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
870 // Part of binary is determined by TableGn.
871 unsigned Binary = getBinaryCodeForInstr(MI);
873 // Set the conditional execution predicate
874 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
876 // Set base address operand
877 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
879 // Set addressing mode by modifying bits U(23) and P(24)
880 const MachineOperand &MO = MI.getOperand(1);
881 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
884 if (ARM_AM::getAM4WBFlag(MO.getImm()))
885 Binary |= 0x1 << ARMII::W_BitShift;
888 for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
889 const MachineOperand &MO = MI.getOperand(i);
890 if (!MO.isReg() || MO.isImplicit())
892 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
893 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
895 Binary |= 0x1 << RegNum;
901 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
902 const TargetInstrDesc &TID = MI.getDesc();
904 // Part of binary is determined by TableGn.
905 unsigned Binary = getBinaryCodeForInstr(MI);
907 // Set the conditional execution predicate
908 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
910 // Encode S bit if MI modifies CPSR.
911 Binary |= getAddrModeSBit(MI, TID);
913 // 32x32->64bit operations have two destination registers. The number
914 // of register definitions will tell us if that's what we're dealing with.
916 if (TID.getNumDefs() == 2)
917 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
920 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
923 Binary |= getMachineOpValue(MI, OpIdx++);
926 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
928 // Many multiple instructions (e.g. MLA) have three src operands. Encode
929 // it as Rn (for multiply, that's in the same offset as RdLo.
930 if (TID.getNumOperands() > OpIdx &&
931 !TID.OpInfo[OpIdx].isPredicate() &&
932 !TID.OpInfo[OpIdx].isOptionalDef())
933 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
938 void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
939 const TargetInstrDesc &TID = MI.getDesc();
941 // Part of binary is determined by TableGn.
942 unsigned Binary = getBinaryCodeForInstr(MI);
944 // Set the conditional execution predicate
945 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
950 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
952 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
953 const MachineOperand &MO2 = MI.getOperand(OpIdx);
955 // Two register operand form.
957 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
960 Binary |= getMachineOpValue(MI, MO2);
963 Binary |= getMachineOpValue(MI, MO1);
966 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
967 if (MI.getOperand(OpIdx).isImm() &&
968 !TID.OpInfo[OpIdx].isPredicate() &&
969 !TID.OpInfo[OpIdx].isOptionalDef())
970 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
975 void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
976 const TargetInstrDesc &TID = MI.getDesc();
978 // Part of binary is determined by TableGn.
979 unsigned Binary = getBinaryCodeForInstr(MI);
981 // Set the conditional execution predicate
982 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
987 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
989 const MachineOperand &MO = MI.getOperand(OpIdx++);
990 if (OpIdx == TID.getNumOperands() ||
991 TID.OpInfo[OpIdx].isPredicate() ||
992 TID.OpInfo[OpIdx].isOptionalDef()) {
993 // Encode Rm and it's done.
994 Binary |= getMachineOpValue(MI, MO);
1000 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1003 Binary |= getMachineOpValue(MI, OpIdx++);
1005 // Encode shift_imm.
1006 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
1007 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1008 Binary |= ShiftAmt << ARMII::ShiftShift;
1013 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
1014 const TargetInstrDesc &TID = MI.getDesc();
1016 if (TID.Opcode == ARM::TPsoft)
1019 // Part of binary is determined by TableGn.
1020 unsigned Binary = getBinaryCodeForInstr(MI);
1022 // Set the conditional execution predicate
1023 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1025 // Set signed_immed_24 field
1026 Binary |= getMachineOpValue(MI, 0);
1031 void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
1032 // Remember the base address of the inline jump table.
1033 intptr_t JTBase = MCE.getCurrentPCValue();
1034 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
1035 DOUT << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase << '\n';
1037 // Now emit the jump table entries.
1038 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1039 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1041 // DestBB address - JT base.
1042 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
1044 // Absolute DestBB address.
1045 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1050 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
1051 const TargetInstrDesc &TID = MI.getDesc();
1053 // Handle jump tables.
1054 if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
1055 // First emit a ldr pc, [] instruction.
1056 emitDataProcessingInstruction(MI, ARM::PC);
1058 // Then emit the inline jump table.
1059 unsigned JTIndex = (TID.Opcode == ARM::BR_JTr)
1060 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1061 emitInlineJumpTable(JTIndex);
1063 } else if (TID.Opcode == ARM::BR_JTm) {
1064 // First emit a ldr pc, [] instruction.
1065 emitLoadStoreInstruction(MI, ARM::PC);
1067 // Then emit the inline jump table.
1068 emitInlineJumpTable(MI.getOperand(3).getIndex());
1072 // Part of binary is determined by TableGn.
1073 unsigned Binary = getBinaryCodeForInstr(MI);
1075 // Set the conditional execution predicate
1076 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1078 if (TID.Opcode == ARM::BX_RET)
1079 // The return register is LR.
1080 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
1082 // otherwise, set the return register
1083 Binary |= getMachineOpValue(MI, 0);
1088 static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) {
1089 unsigned RegD = MI.getOperand(OpIdx).getReg();
1090 unsigned Binary = 0;
1091 bool isSPVFP = false;
1092 RegD = ARMRegisterInfo::getRegisterNumbering(RegD, isSPVFP);
1094 Binary |= RegD << ARMII::RegRdShift;
1096 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1097 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1102 static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) {
1103 unsigned RegN = MI.getOperand(OpIdx).getReg();
1104 unsigned Binary = 0;
1105 bool isSPVFP = false;
1106 RegN = ARMRegisterInfo::getRegisterNumbering(RegN, isSPVFP);
1108 Binary |= RegN << ARMII::RegRnShift;
1110 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1111 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1116 static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) {
1117 unsigned RegM = MI.getOperand(OpIdx).getReg();
1118 unsigned Binary = 0;
1119 bool isSPVFP = false;
1120 RegM = ARMRegisterInfo::getRegisterNumbering(RegM, isSPVFP);
1124 Binary |= ((RegM & 0x1E) >> 1);
1125 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
1130 void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) {
1131 const TargetInstrDesc &TID = MI.getDesc();
1133 // Part of binary is determined by TableGn.
1134 unsigned Binary = getBinaryCodeForInstr(MI);
1136 // Set the conditional execution predicate
1137 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1140 assert((Binary & ARMII::D_BitShift) == 0 &&
1141 (Binary & ARMII::N_BitShift) == 0 &&
1142 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1145 Binary |= encodeVFPRd(MI, OpIdx++);
1147 // If this is a two-address operand, skip it, e.g. FMACD.
1148 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1152 if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
1153 Binary |= encodeVFPRn(MI, OpIdx++);
1155 if (OpIdx == TID.getNumOperands() ||
1156 TID.OpInfo[OpIdx].isPredicate() ||
1157 TID.OpInfo[OpIdx].isOptionalDef()) {
1158 // FCMPEZD etc. has only one operand.
1164 Binary |= encodeVFPRm(MI, OpIdx);
1169 void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) {
1170 const TargetInstrDesc &TID = MI.getDesc();
1171 unsigned Form = TID.TSFlags & ARMII::FormMask;
1173 // Part of binary is determined by TableGn.
1174 unsigned Binary = getBinaryCodeForInstr(MI);
1176 // Set the conditional execution predicate
1177 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1181 case ARMII::VFPConv1Frm:
1182 case ARMII::VFPConv2Frm:
1183 case ARMII::VFPConv3Frm:
1185 Binary |= encodeVFPRd(MI, 0);
1187 case ARMII::VFPConv4Frm:
1189 Binary |= encodeVFPRn(MI, 0);
1191 case ARMII::VFPConv5Frm:
1193 Binary |= encodeVFPRm(MI, 0);
1199 case ARMII::VFPConv1Frm:
1201 Binary |= encodeVFPRm(MI, 1);
1203 case ARMII::VFPConv2Frm:
1204 case ARMII::VFPConv3Frm:
1206 Binary |= encodeVFPRn(MI, 1);
1208 case ARMII::VFPConv4Frm:
1209 case ARMII::VFPConv5Frm:
1211 Binary |= encodeVFPRd(MI, 1);
1215 if (Form == ARMII::VFPConv5Frm)
1217 Binary |= encodeVFPRn(MI, 2);
1218 else if (Form == ARMII::VFPConv3Frm)
1220 Binary |= encodeVFPRm(MI, 2);
1225 void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
1226 // Part of binary is determined by TableGn.
1227 unsigned Binary = getBinaryCodeForInstr(MI);
1229 // Set the conditional execution predicate
1230 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1235 Binary |= encodeVFPRd(MI, OpIdx++);
1237 // Encode address base.
1238 const MachineOperand &Base = MI.getOperand(OpIdx++);
1239 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1241 // If there is a non-zero immediate offset, encode it.
1243 const MachineOperand &Offset = MI.getOperand(OpIdx);
1244 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1245 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1246 Binary |= 1 << ARMII::U_BitShift;
1253 // If immediate offset is omitted, default to +0.
1254 Binary |= 1 << ARMII::U_BitShift;
1260 ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
1261 // Part of binary is determined by TableGn.
1262 unsigned Binary = getBinaryCodeForInstr(MI);
1264 // Set the conditional execution predicate
1265 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1267 // Set base address operand
1268 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
1270 // Set addressing mode by modifying bits U(23) and P(24)
1271 const MachineOperand &MO = MI.getOperand(1);
1272 Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm()));
1275 if (ARM_AM::getAM5WBFlag(MO.getImm()))
1276 Binary |= 0x1 << ARMII::W_BitShift;
1278 // First register is encoded in Dd.
1279 Binary |= encodeVFPRd(MI, 4);
1281 // Number of registers are encoded in offset field.
1282 unsigned NumRegs = 1;
1283 for (unsigned i = 5, e = MI.getNumOperands(); i != e; ++i) {
1284 const MachineOperand &MO = MI.getOperand(i);
1285 if (!MO.isReg() || MO.isImplicit())
1289 Binary |= NumRegs * 2;
1294 void ARMCodeEmitter::emitMiscInstruction(const MachineInstr &MI) {
1295 // Part of binary is determined by TableGn.
1296 unsigned Binary = getBinaryCodeForInstr(MI);
1298 // Set the conditional execution predicate
1299 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1304 #include "ARMGenCodeEmitter.inc"