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/JITCodeEmitter.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/MachineModuleInfo.h"
33 #include "llvm/CodeGen/Passes.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/raw_ostream.h"
43 STATISTIC(NumEmitted, "Number of machine instructions emitted");
47 class ARMCodeEmitter : public MachineFunctionPass {
49 const ARMInstrInfo *II;
51 const ARMSubtarget *Subtarget;
54 MachineModuleInfo *MMI;
55 const std::vector<MachineConstantPoolEntry> *MCPEs;
56 const std::vector<MachineJumpTableEntry> *MJTEs;
59 void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.addRequired<MachineModuleInfo>();
61 MachineFunctionPass::getAnalysisUsage(AU);
66 ARMCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
67 : MachineFunctionPass(&ID), JTI(0),
68 II((const ARMInstrInfo *)tm.getInstrInfo()),
69 TD(tm.getTargetData()), TM(tm),
70 MCE(mce), MCPEs(0), MJTEs(0),
71 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
73 /// getBinaryCodeForInstr - This function, generated by the
74 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
75 /// machine instructions.
76 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
78 bool runOnMachineFunction(MachineFunction &MF);
80 virtual const char *getPassName() const {
81 return "ARM Machine Code Emitter";
84 void emitInstruction(const MachineInstr &MI);
88 void emitWordLE(unsigned Binary);
89 void emitDWordLE(uint64_t Binary);
90 void emitConstPoolInstruction(const MachineInstr &MI);
91 void emitMOVi32immInstruction(const MachineInstr &MI);
92 void emitMOVi2piecesInstruction(const MachineInstr &MI);
93 void emitLEApcrelJTInstruction(const MachineInstr &MI);
94 void emitPseudoMoveInstruction(const MachineInstr &MI);
95 void addPCLabel(unsigned LabelID);
96 void emitPseudoInstruction(const MachineInstr &MI);
97 unsigned getMachineSoRegOpValue(const MachineInstr &MI,
98 const TargetInstrDesc &TID,
99 const MachineOperand &MO,
102 unsigned getMachineSoImmOpValue(unsigned SoImm);
104 unsigned getAddrModeSBit(const MachineInstr &MI,
105 const TargetInstrDesc &TID) const;
107 void emitDataProcessingInstruction(const MachineInstr &MI,
108 unsigned ImplicitRd = 0,
109 unsigned ImplicitRn = 0);
111 void emitLoadStoreInstruction(const MachineInstr &MI,
112 unsigned ImplicitRd = 0,
113 unsigned ImplicitRn = 0);
115 void emitMiscLoadStoreInstruction(const MachineInstr &MI,
116 unsigned ImplicitRn = 0);
118 void emitLoadStoreMultipleInstruction(const MachineInstr &MI);
120 void emitMulFrmInstruction(const MachineInstr &MI);
122 void emitExtendInstruction(const MachineInstr &MI);
124 void emitMiscArithInstruction(const MachineInstr &MI);
126 void emitBranchInstruction(const MachineInstr &MI);
128 void emitInlineJumpTable(unsigned JTIndex);
130 void emitMiscBranchInstruction(const MachineInstr &MI);
132 void emitVFPArithInstruction(const MachineInstr &MI);
134 void emitVFPConversionInstruction(const MachineInstr &MI);
136 void emitVFPLoadStoreInstruction(const MachineInstr &MI);
138 void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI);
140 void emitMiscInstruction(const MachineInstr &MI);
142 void emitNEON1RegModImmInstruction(const MachineInstr &MI);
143 void emitNEON2RegInstruction(const MachineInstr &MI);
145 /// getMachineOpValue - Return binary encoding of operand. If the machine
146 /// operand requires relocation, record the relocation and return zero.
147 unsigned getMachineOpValue(const MachineInstr &MI,const MachineOperand &MO);
148 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) {
149 return getMachineOpValue(MI, MI.getOperand(OpIdx));
152 /// getMovi32Value - Return binary encoding of operand for movw/movt. If the
153 /// machine operand requires relocation, record the relocation and return
155 unsigned getMovi32Value(const MachineInstr &MI,const MachineOperand &MO,
157 unsigned getMovi32Value(const MachineInstr &MI, unsigned OpIdx,
159 return getMovi32Value(MI, MI.getOperand(OpIdx), Reloc);
162 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
164 unsigned getShiftOp(unsigned Imm) const ;
166 /// Routines that handle operands which add machine relocations which are
167 /// fixed up by the relocation stage.
168 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
169 bool MayNeedFarStub, bool Indirect,
171 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
172 void emitConstPoolAddress(unsigned CPI, unsigned Reloc);
173 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc);
174 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
175 intptr_t JTBase = 0);
179 char ARMCodeEmitter::ID = 0;
181 /// createARMJITCodeEmitterPass - Return a pass that emits the collected ARM
182 /// code to the specified MCE object.
183 FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
184 JITCodeEmitter &JCE) {
185 return new ARMCodeEmitter(TM, JCE);
188 bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
189 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
190 MF.getTarget().getRelocationModel() != Reloc::Static) &&
191 "JIT relocation model must be set to static or default!");
192 JTI = ((ARMTargetMachine &)MF.getTarget()).getJITInfo();
193 II = ((const ARMTargetMachine &)MF.getTarget()).getInstrInfo();
194 TD = ((const ARMTargetMachine &)MF.getTarget()).getTargetData();
195 Subtarget = &TM.getSubtarget<ARMSubtarget>();
196 MCPEs = &MF.getConstantPool()->getConstants();
198 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
199 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
200 JTI->Initialize(MF, IsPIC);
201 MMI = &getAnalysis<MachineModuleInfo>();
202 MCE.setModuleInfo(MMI);
205 DEBUG(errs() << "JITTing function '"
206 << MF.getFunction()->getName() << "'\n");
207 MCE.startFunction(MF);
208 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
210 MCE.StartMachineBasicBlock(MBB);
211 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
215 } while (MCE.finishFunction(MF));
220 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
222 unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
223 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
224 default: llvm_unreachable("Unknown shift opc!");
225 case ARM_AM::asr: return 2;
226 case ARM_AM::lsl: return 0;
227 case ARM_AM::lsr: return 1;
229 case ARM_AM::rrx: return 3;
234 /// getMovi32Value - Return binary encoding of operand for movw/movt. If the
235 /// machine operand requires relocation, record the relocation and return zero.
236 unsigned ARMCodeEmitter::getMovi32Value(const MachineInstr &MI,
237 const MachineOperand &MO,
239 assert(((Reloc == ARM::reloc_arm_movt) || (Reloc == ARM::reloc_arm_movw))
240 && "Relocation to this function should be for movt or movw");
243 return static_cast<unsigned>(MO.getImm());
244 else if (MO.isGlobal())
245 emitGlobalAddress(MO.getGlobal(), Reloc, true, false);
246 else if (MO.isSymbol())
247 emitExternalSymbolAddress(MO.getSymbolName(), Reloc);
249 emitMachineBasicBlock(MO.getMBB(), Reloc);
254 llvm_unreachable("Unsupported operand type for movw/movt");
259 /// getMachineOpValue - Return binary encoding of operand. If the machine
260 /// operand requires relocation, record the relocation and return zero.
261 unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
262 const MachineOperand &MO) {
264 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
266 return static_cast<unsigned>(MO.getImm());
267 else if (MO.isGlobal())
268 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true, false);
269 else if (MO.isSymbol())
270 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
271 else if (MO.isCPI()) {
272 const TargetInstrDesc &TID = MI.getDesc();
273 // For VFP load, the immediate offset is multiplied by 4.
274 unsigned Reloc = ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
275 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
276 emitConstPoolAddress(MO.getIndex(), Reloc);
277 } else if (MO.isJTI())
278 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
280 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
290 /// emitGlobalAddress - Emit the specified address to the code stream.
292 void ARMCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
293 bool MayNeedFarStub, bool Indirect,
295 MachineRelocation MR = Indirect
296 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
297 const_cast<GlobalValue *>(GV),
298 ACPV, MayNeedFarStub)
299 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
300 const_cast<GlobalValue *>(GV), ACPV,
302 MCE.addRelocation(MR);
305 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
306 /// be emitted to the current location in the function, and allow it to be PC
308 void ARMCodeEmitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
309 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
313 /// emitConstPoolAddress - Arrange for the address of an constant pool
314 /// to be emitted to the current location in the function, and allow it to be PC
316 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) {
317 // Tell JIT emitter we'll resolve the address.
318 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
319 Reloc, CPI, 0, true));
322 /// emitJumpTableAddress - Arrange for the address of a jump table to
323 /// be emitted to the current location in the function, and allow it to be PC
325 void ARMCodeEmitter::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) {
326 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
327 Reloc, JTIndex, 0, true));
330 /// emitMachineBasicBlock - Emit the specified address basic block.
331 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
332 unsigned Reloc, intptr_t JTBase) {
333 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
337 void ARMCodeEmitter::emitWordLE(unsigned Binary) {
338 DEBUG(errs() << " 0x";
339 errs().write_hex(Binary) << "\n");
340 MCE.emitWordLE(Binary);
343 void ARMCodeEmitter::emitDWordLE(uint64_t Binary) {
344 DEBUG(errs() << " 0x";
345 errs().write_hex(Binary) << "\n");
346 MCE.emitDWordLE(Binary);
349 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
350 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
352 MCE.processDebugLoc(MI.getDebugLoc(), true);
354 ++NumEmitted; // Keep track of the # of mi's emitted
355 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
357 llvm_unreachable("Unhandled instruction encoding format!");
361 emitPseudoInstruction(MI);
364 case ARMII::DPSoRegFrm:
365 emitDataProcessingInstruction(MI);
369 emitLoadStoreInstruction(MI);
371 case ARMII::LdMiscFrm:
372 case ARMII::StMiscFrm:
373 emitMiscLoadStoreInstruction(MI);
375 case ARMII::LdStMulFrm:
376 emitLoadStoreMultipleInstruction(MI);
379 emitMulFrmInstruction(MI);
382 emitExtendInstruction(MI);
384 case ARMII::ArithMiscFrm:
385 emitMiscArithInstruction(MI);
388 emitBranchInstruction(MI);
390 case ARMII::BrMiscFrm:
391 emitMiscBranchInstruction(MI);
394 case ARMII::VFPUnaryFrm:
395 case ARMII::VFPBinaryFrm:
396 emitVFPArithInstruction(MI);
398 case ARMII::VFPConv1Frm:
399 case ARMII::VFPConv2Frm:
400 case ARMII::VFPConv3Frm:
401 case ARMII::VFPConv4Frm:
402 case ARMII::VFPConv5Frm:
403 emitVFPConversionInstruction(MI);
405 case ARMII::VFPLdStFrm:
406 emitVFPLoadStoreInstruction(MI);
408 case ARMII::VFPLdStMulFrm:
409 emitVFPLoadStoreMultipleInstruction(MI);
411 case ARMII::VFPMiscFrm:
412 emitMiscInstruction(MI);
414 // NEON instructions.
415 case ARMII::N1RegModImmFrm:
416 emitNEON1RegModImmInstruction(MI);
418 case ARMII::N2RegFrm:
419 emitNEON2RegInstruction(MI);
422 MCE.processDebugLoc(MI.getDebugLoc(), false);
425 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
426 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
427 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
428 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
430 // Remember the CONSTPOOL_ENTRY address for later relocation.
431 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
433 // Emit constpool island entry. In most cases, the actual values will be
434 // resolved and relocated after code emission.
435 if (MCPE.isMachineConstantPoolEntry()) {
436 ARMConstantPoolValue *ACPV =
437 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
439 DEBUG(errs() << " ** ARM constant pool #" << CPI << " @ "
440 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n');
442 assert(ACPV->isGlobalValue() && "unsupported constant pool value");
443 const GlobalValue *GV = ACPV->getGV();
445 Reloc::Model RelocM = TM.getRelocationModel();
446 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
448 Subtarget->GVIsIndirectSymbol(GV, RelocM),
451 emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
455 const Constant *CV = MCPE.Val.ConstVal;
458 errs() << " ** Constant pool #" << CPI << " @ "
459 << (void*)MCE.getCurrentPCValue() << " ";
460 if (const Function *F = dyn_cast<Function>(CV))
461 errs() << F->getName();
467 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
468 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV), false);
470 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
471 uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
473 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
474 if (CFP->getType()->isFloatTy())
475 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
476 else if (CFP->getType()->isDoubleTy())
477 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
479 llvm_unreachable("Unable to handle this constantpool entry!");
482 llvm_unreachable("Unable to handle this constantpool entry!");
487 void ARMCodeEmitter::emitMOVi32immInstruction(const MachineInstr &MI) {
488 const MachineOperand &MO0 = MI.getOperand(0);
489 const MachineOperand &MO1 = MI.getOperand(1);
491 // Emit the 'movw' instruction.
492 unsigned Binary = 0x30 << 20; // mov: Insts{27-20} = 0b00110000
494 unsigned Lo16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movw) & 0xFFFF;
496 // Set the conditional execution predicate.
497 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
500 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
502 // Encode imm16 as imm4:imm12
503 Binary |= Lo16 & 0xFFF; // Insts{11-0} = imm12
504 Binary |= ((Lo16 >> 12) & 0xF) << 16; // Insts{19-16} = imm4
507 unsigned Hi16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movt) >> 16;
508 // Emit the 'movt' instruction.
509 Binary = 0x34 << 20; // movt: Insts{27-20} = 0b00110100
511 // Set the conditional execution predicate.
512 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
515 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
517 // Encode imm16 as imm4:imm1, same as movw above.
518 Binary |= Hi16 & 0xFFF;
519 Binary |= ((Hi16 >> 12) & 0xF) << 16;
523 void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
524 const MachineOperand &MO0 = MI.getOperand(0);
525 const MachineOperand &MO1 = MI.getOperand(1);
526 assert(MO1.isImm() && ARM_AM::isSOImmTwoPartVal(MO1.getImm()) &&
527 "Not a valid so_imm value!");
528 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
529 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
531 // Emit the 'mov' instruction.
532 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
534 // Set the conditional execution predicate.
535 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
538 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
541 // Set bit I(25) to identify this is the immediate form of <shifter_op>
542 Binary |= 1 << ARMII::I_BitShift;
543 Binary |= getMachineSoImmOpValue(V1);
546 // Now the 'orr' instruction.
547 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
549 // Set the conditional execution predicate.
550 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
553 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
556 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
559 // Set bit I(25) to identify this is the immediate form of <shifter_op>
560 Binary |= 1 << ARMII::I_BitShift;
561 Binary |= getMachineSoImmOpValue(V2);
565 void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
566 // It's basically add r, pc, (LJTI - $+8)
568 const TargetInstrDesc &TID = MI.getDesc();
570 // Emit the 'add' instruction.
571 unsigned Binary = 0x4 << 21; // add: Insts{24-31} = 0b0100
573 // Set the conditional execution predicate
574 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
576 // Encode S bit if MI modifies CPSR.
577 Binary |= getAddrModeSBit(MI, TID);
580 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
582 // Encode Rn which is PC.
583 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
585 // Encode the displacement.
586 Binary |= 1 << ARMII::I_BitShift;
587 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
592 void ARMCodeEmitter::emitPseudoMoveInstruction(const MachineInstr &MI) {
593 unsigned Opcode = MI.getDesc().Opcode;
595 // Part of binary is determined by TableGn.
596 unsigned Binary = getBinaryCodeForInstr(MI);
598 // Set the conditional execution predicate
599 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
601 // Encode S bit if MI modifies CPSR.
602 if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag)
603 Binary |= 1 << ARMII::S_BitShift;
605 // Encode register def if there is one.
606 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
608 // Encode the shift operation.
615 case ARM::MOVsrl_flag:
617 Binary |= (0x2 << 4) | (1 << 7);
619 case ARM::MOVsra_flag:
621 Binary |= (0x4 << 4) | (1 << 7);
625 // Encode register Rm.
626 Binary |= getMachineOpValue(MI, 1);
631 void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
632 DEBUG(errs() << " ** LPC" << LabelID << " @ "
633 << (void*)MCE.getCurrentPCValue() << '\n');
634 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
637 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
638 unsigned Opcode = MI.getDesc().Opcode;
641 llvm_unreachable("ARMCodeEmitter::emitPseudoInstruction");
642 case TargetOpcode::INLINEASM: {
643 // We allow inline assembler nodes with empty bodies - they can
644 // implicitly define registers, which is ok for JIT.
645 if (MI.getOperand(0).getSymbolName()[0]) {
646 report_fatal_error("JIT does not support inline asm!");
650 case TargetOpcode::DBG_LABEL:
651 case TargetOpcode::EH_LABEL:
652 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
654 case TargetOpcode::IMPLICIT_DEF:
655 case TargetOpcode::KILL:
658 case ARM::CONSTPOOL_ENTRY:
659 emitConstPoolInstruction(MI);
662 // Remember of the address of the PC label for relocation later.
663 addPCLabel(MI.getOperand(2).getImm());
664 // PICADD is just an add instruction that implicitly read pc.
665 emitDataProcessingInstruction(MI, 0, ARM::PC);
672 // Remember of the address of the PC label for relocation later.
673 addPCLabel(MI.getOperand(2).getImm());
674 // These are just load / store instructions that implicitly read pc.
675 emitLoadStoreInstruction(MI, 0, ARM::PC);
682 // Remember of the address of the PC label for relocation later.
683 addPCLabel(MI.getOperand(2).getImm());
684 // These are just load / store instructions that implicitly read pc.
685 emitMiscLoadStoreInstruction(MI, ARM::PC);
690 emitMOVi32immInstruction(MI);
693 case ARM::MOVi2pieces:
694 // Two instructions to materialize a constant.
695 emitMOVi2piecesInstruction(MI);
697 case ARM::LEApcrelJT:
698 // Materialize jumptable address.
699 emitLEApcrelJTInstruction(MI);
702 case ARM::MOVsrl_flag:
703 case ARM::MOVsra_flag:
704 emitPseudoMoveInstruction(MI);
709 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
710 const TargetInstrDesc &TID,
711 const MachineOperand &MO,
713 unsigned Binary = getMachineOpValue(MI, MO);
715 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
716 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
717 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
719 // Encode the shift opcode.
721 unsigned Rs = MO1.getReg();
723 // Set shift operand (bit[7:4]).
728 // RRX - 0110 and bit[11:8] clear.
730 default: llvm_unreachable("Unknown shift opc!");
731 case ARM_AM::lsl: SBits = 0x1; break;
732 case ARM_AM::lsr: SBits = 0x3; break;
733 case ARM_AM::asr: SBits = 0x5; break;
734 case ARM_AM::ror: SBits = 0x7; break;
735 case ARM_AM::rrx: SBits = 0x6; break;
738 // Set shift operand (bit[6:4]).
744 default: llvm_unreachable("Unknown shift opc!");
745 case ARM_AM::lsl: SBits = 0x0; break;
746 case ARM_AM::lsr: SBits = 0x2; break;
747 case ARM_AM::asr: SBits = 0x4; break;
748 case ARM_AM::ror: SBits = 0x6; break;
751 Binary |= SBits << 4;
752 if (SOpc == ARM_AM::rrx)
755 // Encode the shift operation Rs or shift_imm (except rrx).
757 // Encode Rs bit[11:8].
758 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
760 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
763 // Encode shift_imm bit[11:7].
764 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
767 unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
768 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
769 assert(SoImmVal != -1 && "Not a valid so_imm value!");
771 // Encode rotate_imm.
772 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
773 << ARMII::SoRotImmShift;
776 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
780 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
781 const TargetInstrDesc &TID) const {
782 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
783 const MachineOperand &MO = MI.getOperand(i-1);
784 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
785 return 1 << ARMII::S_BitShift;
790 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
792 unsigned ImplicitRn) {
793 const TargetInstrDesc &TID = MI.getDesc();
795 // Part of binary is determined by TableGn.
796 unsigned Binary = getBinaryCodeForInstr(MI);
798 // Set the conditional execution predicate
799 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
801 // Encode S bit if MI modifies CPSR.
802 Binary |= getAddrModeSBit(MI, TID);
804 // Encode register def if there is one.
805 unsigned NumDefs = TID.getNumDefs();
808 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
810 // Special handling for implicit use (e.g. PC).
811 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
812 << ARMII::RegRdShift);
814 if (TID.Opcode == ARM::MOVi16) {
815 // Get immediate from MI.
816 unsigned Lo16 = getMovi32Value(MI, MI.getOperand(OpIdx),
817 ARM::reloc_arm_movw);
818 // Encode imm which is the same as in emitMOVi32immInstruction().
819 Binary |= Lo16 & 0xFFF;
820 Binary |= ((Lo16 >> 12) & 0xF) << 16;
823 } else if(TID.Opcode == ARM::MOVTi16) {
824 unsigned Hi16 = (getMovi32Value(MI, MI.getOperand(OpIdx),
825 ARM::reloc_arm_movt) >> 16);
826 Binary |= Hi16 & 0xFFF;
827 Binary |= ((Hi16 >> 12) & 0xF) << 16;
830 } else if ((TID.Opcode == ARM::BFC) || (TID.Opcode == ARM::BFI)) {
831 uint32_t v = ~MI.getOperand(2).getImm();
832 int32_t lsb = CountTrailingZeros_32(v);
833 int32_t msb = (32 - CountLeadingZeros_32(v)) - 1;
834 // Instr{20-16} = msb, Instr{11-7} = lsb
835 Binary |= (msb & 0x1F) << 16;
836 Binary |= (lsb & 0x1F) << 7;
839 } else if ((TID.Opcode == ARM::UBFX) || (TID.Opcode == ARM::SBFX)) {
840 // Encode Rn in Instr{0-3}
841 Binary |= getMachineOpValue(MI, OpIdx++);
843 uint32_t lsb = MI.getOperand(OpIdx++).getImm();
844 uint32_t widthm1 = MI.getOperand(OpIdx++).getImm() - 1;
846 // Instr{20-16} = widthm1, Instr{11-7} = lsb
847 Binary |= (widthm1 & 0x1F) << 16;
848 Binary |= (lsb & 0x1F) << 7;
853 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
854 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
857 // Encode first non-shifter register operand if there is one.
858 bool isUnary = TID.TSFlags & ARMII::UnaryDP;
861 // Special handling for implicit use (e.g. PC).
862 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
863 << ARMII::RegRnShift);
865 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
870 // Encode shifter operand.
871 const MachineOperand &MO = MI.getOperand(OpIdx);
872 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
874 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
879 // Encode register Rm.
880 emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
885 Binary |= getMachineSoImmOpValue((unsigned)MO.getImm());
890 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
892 unsigned ImplicitRn) {
893 const TargetInstrDesc &TID = MI.getDesc();
894 unsigned Form = TID.TSFlags & ARMII::FormMask;
895 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
897 // Part of binary is determined by TableGn.
898 unsigned Binary = getBinaryCodeForInstr(MI);
900 // Set the conditional execution predicate
901 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
905 // Operand 0 of a pre- and post-indexed store is the address base
906 // writeback. Skip it.
907 bool Skipped = false;
908 if (IsPrePost && Form == ARMII::StFrm) {
915 // Special handling for implicit use (e.g. PC).
916 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
917 << ARMII::RegRdShift);
919 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
921 // Set second operand
923 // Special handling for implicit use (e.g. PC).
924 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
925 << ARMII::RegRnShift);
927 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
929 // If this is a two-address operand, skip it. e.g. LDR_PRE.
930 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
933 const MachineOperand &MO2 = MI.getOperand(OpIdx);
934 unsigned AM2Opc = (ImplicitRn == ARM::PC)
935 ? 0 : MI.getOperand(OpIdx+1).getImm();
937 // Set bit U(23) according to sign of immed value (positive or negative).
938 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
940 if (!MO2.getReg()) { // is immediate
941 if (ARM_AM::getAM2Offset(AM2Opc))
942 // Set the value of offset_12 field
943 Binary |= ARM_AM::getAM2Offset(AM2Opc);
948 // Set bit I(25), because this is not in immediate enconding.
949 Binary |= 1 << ARMII::I_BitShift;
950 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
951 // Set bit[3:0] to the corresponding Rm register
952 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
954 // If this instr is in scaled register offset/index instruction, set
955 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
956 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
957 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
958 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
964 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
965 unsigned ImplicitRn) {
966 const TargetInstrDesc &TID = MI.getDesc();
967 unsigned Form = TID.TSFlags & ARMII::FormMask;
968 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
970 // Part of binary is determined by TableGn.
971 unsigned Binary = getBinaryCodeForInstr(MI);
973 // Set the conditional execution predicate
974 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
978 // Operand 0 of a pre- and post-indexed store is the address base
979 // writeback. Skip it.
980 bool Skipped = false;
981 if (IsPrePost && Form == ARMII::StMiscFrm) {
987 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
989 // Skip LDRD and STRD's second operand.
990 if (TID.Opcode == ARM::LDRD || TID.Opcode == ARM::STRD)
993 // Set second operand
995 // Special handling for implicit use (e.g. PC).
996 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
997 << ARMII::RegRnShift);
999 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1001 // If this is a two-address operand, skip it. e.g. LDRH_POST.
1002 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1005 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1006 unsigned AM3Opc = (ImplicitRn == ARM::PC)
1007 ? 0 : MI.getOperand(OpIdx+1).getImm();
1009 // Set bit U(23) according to sign of immed value (positive or negative)
1010 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
1013 // If this instr is in register offset/index encoding, set bit[3:0]
1014 // to the corresponding Rm register.
1016 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
1021 // This instr is in immediate offset/index encoding, set bit 22 to 1.
1022 Binary |= 1 << ARMII::AM3_I_BitShift;
1023 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
1025 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
1026 Binary |= (ImmOffs & 0xF); // immedL
1032 static unsigned getAddrModeUPBits(unsigned Mode) {
1033 unsigned Binary = 0;
1035 // Set addressing mode by modifying bits U(23) and P(24)
1036 // IA - Increment after - bit U = 1 and bit P = 0
1037 // IB - Increment before - bit U = 1 and bit P = 1
1038 // DA - Decrement after - bit U = 0 and bit P = 0
1039 // DB - Decrement before - bit U = 0 and bit P = 1
1041 default: llvm_unreachable("Unknown addressing sub-mode!");
1042 case ARM_AM::da: break;
1043 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
1044 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
1045 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
1051 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
1052 const TargetInstrDesc &TID = MI.getDesc();
1053 bool IsUpdating = (TID.TSFlags & ARMII::IndexModeMask) != 0;
1055 // Part of binary is determined by TableGn.
1056 unsigned Binary = getBinaryCodeForInstr(MI);
1058 // Set the conditional execution predicate
1059 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1061 // Skip operand 0 of an instruction with base register update.
1066 // Set base address operand
1067 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1069 // Set addressing mode by modifying bits U(23) and P(24)
1070 const MachineOperand &MO = MI.getOperand(OpIdx++);
1071 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
1075 Binary |= 0x1 << ARMII::W_BitShift;
1078 for (unsigned i = OpIdx+2, e = MI.getNumOperands(); i != e; ++i) {
1079 const MachineOperand &MO = MI.getOperand(i);
1080 if (!MO.isReg() || MO.isImplicit())
1082 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
1083 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
1085 Binary |= 0x1 << RegNum;
1091 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
1092 const TargetInstrDesc &TID = MI.getDesc();
1094 // Part of binary is determined by TableGn.
1095 unsigned Binary = getBinaryCodeForInstr(MI);
1097 // Set the conditional execution predicate
1098 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1100 // Encode S bit if MI modifies CPSR.
1101 Binary |= getAddrModeSBit(MI, TID);
1103 // 32x32->64bit operations have two destination registers. The number
1104 // of register definitions will tell us if that's what we're dealing with.
1106 if (TID.getNumDefs() == 2)
1107 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
1110 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
1113 Binary |= getMachineOpValue(MI, OpIdx++);
1116 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
1118 // Many multiple instructions (e.g. MLA) have three src operands. Encode
1119 // it as Rn (for multiply, that's in the same offset as RdLo.
1120 if (TID.getNumOperands() > OpIdx &&
1121 !TID.OpInfo[OpIdx].isPredicate() &&
1122 !TID.OpInfo[OpIdx].isOptionalDef())
1123 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
1128 void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
1129 const TargetInstrDesc &TID = MI.getDesc();
1131 // Part of binary is determined by TableGn.
1132 unsigned Binary = getBinaryCodeForInstr(MI);
1134 // Set the conditional execution predicate
1135 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1140 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1142 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
1143 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1145 // Two register operand form.
1147 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
1150 Binary |= getMachineOpValue(MI, MO2);
1153 Binary |= getMachineOpValue(MI, MO1);
1156 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
1157 if (MI.getOperand(OpIdx).isImm() &&
1158 !TID.OpInfo[OpIdx].isPredicate() &&
1159 !TID.OpInfo[OpIdx].isOptionalDef())
1160 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
1165 void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
1166 const TargetInstrDesc &TID = MI.getDesc();
1168 // Part of binary is determined by TableGn.
1169 unsigned Binary = getBinaryCodeForInstr(MI);
1171 // Set the conditional execution predicate
1172 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1177 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1179 const MachineOperand &MO = MI.getOperand(OpIdx++);
1180 if (OpIdx == TID.getNumOperands() ||
1181 TID.OpInfo[OpIdx].isPredicate() ||
1182 TID.OpInfo[OpIdx].isOptionalDef()) {
1183 // Encode Rm and it's done.
1184 Binary |= getMachineOpValue(MI, MO);
1190 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1193 Binary |= getMachineOpValue(MI, OpIdx++);
1195 // Encode shift_imm.
1196 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
1197 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1198 Binary |= ShiftAmt << ARMII::ShiftShift;
1203 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
1204 const TargetInstrDesc &TID = MI.getDesc();
1206 if (TID.Opcode == ARM::TPsoft) {
1207 llvm_unreachable("ARM::TPsoft FIXME"); // FIXME
1210 // Part of binary is determined by TableGn.
1211 unsigned Binary = getBinaryCodeForInstr(MI);
1213 // Set the conditional execution predicate
1214 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1216 // Set signed_immed_24 field
1217 Binary |= getMachineOpValue(MI, 0);
1222 void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
1223 // Remember the base address of the inline jump table.
1224 uintptr_t JTBase = MCE.getCurrentPCValue();
1225 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
1226 DEBUG(errs() << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase
1229 // Now emit the jump table entries.
1230 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1231 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1233 // DestBB address - JT base.
1234 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
1236 // Absolute DestBB address.
1237 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1242 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
1243 const TargetInstrDesc &TID = MI.getDesc();
1245 // Handle jump tables.
1246 if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
1247 // First emit a ldr pc, [] instruction.
1248 emitDataProcessingInstruction(MI, ARM::PC);
1250 // Then emit the inline jump table.
1252 (TID.Opcode == ARM::BR_JTr)
1253 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1254 emitInlineJumpTable(JTIndex);
1256 } else if (TID.Opcode == ARM::BR_JTm) {
1257 // First emit a ldr pc, [] instruction.
1258 emitLoadStoreInstruction(MI, ARM::PC);
1260 // Then emit the inline jump table.
1261 emitInlineJumpTable(MI.getOperand(3).getIndex());
1265 // Part of binary is determined by TableGn.
1266 unsigned Binary = getBinaryCodeForInstr(MI);
1268 // Set the conditional execution predicate
1269 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1271 if (TID.Opcode == ARM::BX_RET || TID.Opcode == ARM::MOVPCLR)
1272 // The return register is LR.
1273 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
1275 // otherwise, set the return register
1276 Binary |= getMachineOpValue(MI, 0);
1281 static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) {
1282 unsigned RegD = MI.getOperand(OpIdx).getReg();
1283 unsigned Binary = 0;
1284 bool isSPVFP = false;
1285 RegD = ARMRegisterInfo::getRegisterNumbering(RegD, &isSPVFP);
1287 Binary |= RegD << ARMII::RegRdShift;
1289 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1290 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1295 static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) {
1296 unsigned RegN = MI.getOperand(OpIdx).getReg();
1297 unsigned Binary = 0;
1298 bool isSPVFP = false;
1299 RegN = ARMRegisterInfo::getRegisterNumbering(RegN, &isSPVFP);
1301 Binary |= RegN << ARMII::RegRnShift;
1303 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1304 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1309 static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) {
1310 unsigned RegM = MI.getOperand(OpIdx).getReg();
1311 unsigned Binary = 0;
1312 bool isSPVFP = false;
1313 RegM = ARMRegisterInfo::getRegisterNumbering(RegM, &isSPVFP);
1317 Binary |= ((RegM & 0x1E) >> 1);
1318 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
1323 void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) {
1324 const TargetInstrDesc &TID = MI.getDesc();
1326 // Part of binary is determined by TableGn.
1327 unsigned Binary = getBinaryCodeForInstr(MI);
1329 // Set the conditional execution predicate
1330 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1333 assert((Binary & ARMII::D_BitShift) == 0 &&
1334 (Binary & ARMII::N_BitShift) == 0 &&
1335 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1338 Binary |= encodeVFPRd(MI, OpIdx++);
1340 // If this is a two-address operand, skip it, e.g. FMACD.
1341 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1345 if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
1346 Binary |= encodeVFPRn(MI, OpIdx++);
1348 if (OpIdx == TID.getNumOperands() ||
1349 TID.OpInfo[OpIdx].isPredicate() ||
1350 TID.OpInfo[OpIdx].isOptionalDef()) {
1351 // FCMPEZD etc. has only one operand.
1357 Binary |= encodeVFPRm(MI, OpIdx);
1362 void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) {
1363 const TargetInstrDesc &TID = MI.getDesc();
1364 unsigned Form = TID.TSFlags & ARMII::FormMask;
1366 // Part of binary is determined by TableGn.
1367 unsigned Binary = getBinaryCodeForInstr(MI);
1369 // Set the conditional execution predicate
1370 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1374 case ARMII::VFPConv1Frm:
1375 case ARMII::VFPConv2Frm:
1376 case ARMII::VFPConv3Frm:
1378 Binary |= encodeVFPRd(MI, 0);
1380 case ARMII::VFPConv4Frm:
1382 Binary |= encodeVFPRn(MI, 0);
1384 case ARMII::VFPConv5Frm:
1386 Binary |= encodeVFPRm(MI, 0);
1392 case ARMII::VFPConv1Frm:
1394 Binary |= encodeVFPRm(MI, 1);
1396 case ARMII::VFPConv2Frm:
1397 case ARMII::VFPConv3Frm:
1399 Binary |= encodeVFPRn(MI, 1);
1401 case ARMII::VFPConv4Frm:
1402 case ARMII::VFPConv5Frm:
1404 Binary |= encodeVFPRd(MI, 1);
1408 if (Form == ARMII::VFPConv5Frm)
1410 Binary |= encodeVFPRn(MI, 2);
1411 else if (Form == ARMII::VFPConv3Frm)
1413 Binary |= encodeVFPRm(MI, 2);
1418 void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
1419 // Part of binary is determined by TableGn.
1420 unsigned Binary = getBinaryCodeForInstr(MI);
1422 // Set the conditional execution predicate
1423 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1428 Binary |= encodeVFPRd(MI, OpIdx++);
1430 // Encode address base.
1431 const MachineOperand &Base = MI.getOperand(OpIdx++);
1432 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1434 // If there is a non-zero immediate offset, encode it.
1436 const MachineOperand &Offset = MI.getOperand(OpIdx);
1437 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1438 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1439 Binary |= 1 << ARMII::U_BitShift;
1446 // If immediate offset is omitted, default to +0.
1447 Binary |= 1 << ARMII::U_BitShift;
1453 ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
1454 const TargetInstrDesc &TID = MI.getDesc();
1455 bool IsUpdating = (TID.TSFlags & ARMII::IndexModeMask) != 0;
1457 // Part of binary is determined by TableGn.
1458 unsigned Binary = getBinaryCodeForInstr(MI);
1460 // Set the conditional execution predicate
1461 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1463 // Skip operand 0 of an instruction with base register update.
1468 // Set base address operand
1469 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1471 // Set addressing mode by modifying bits U(23) and P(24)
1472 const MachineOperand &MO = MI.getOperand(OpIdx++);
1473 Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm()));
1477 Binary |= 0x1 << ARMII::W_BitShift;
1479 // First register is encoded in Dd.
1480 Binary |= encodeVFPRd(MI, OpIdx+2);
1482 // Number of registers are encoded in offset field.
1483 unsigned NumRegs = 1;
1484 for (unsigned i = OpIdx+3, e = MI.getNumOperands(); i != e; ++i) {
1485 const MachineOperand &MO = MI.getOperand(i);
1486 if (!MO.isReg() || MO.isImplicit())
1490 // Bit 8 will be set if <list> is consecutive 64-bit registers (e.g., D0)
1491 // Otherwise, it will be 0, in the case of 32-bit registers.
1493 Binary |= NumRegs * 2;
1500 void ARMCodeEmitter::emitMiscInstruction(const MachineInstr &MI) {
1501 unsigned Opcode = MI.getDesc().Opcode;
1502 // Part of binary is determined by TableGn.
1503 unsigned Binary = getBinaryCodeForInstr(MI);
1505 // Set the conditional execution predicate
1506 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1510 llvm_unreachable("ARMCodeEmitter::emitMiscInstruction");
1513 // No further encoding needed.
1518 const MachineOperand &MO0 = MI.getOperand(0);
1520 Binary |= ARMRegisterInfo::getRegisterNumbering(MO0.getReg())
1521 << ARMII::RegRdShift;
1526 case ARM::FCONSTS: {
1528 Binary |= encodeVFPRd(MI, 0);
1530 // Encode imm., Table A7-18 VFP modified immediate constants
1531 const MachineOperand &MO1 = MI.getOperand(1);
1532 unsigned Imm = static_cast<unsigned>(MO1.getFPImm()->getValueAPF()
1533 .bitcastToAPInt().getHiBits(32).getLimitedValue());
1534 unsigned ModifiedImm;
1536 if(Opcode == ARM::FCONSTS)
1537 ModifiedImm = (Imm & 0x80000000) >> 24 | // a
1538 (Imm & 0x03F80000) >> 19; // bcdefgh
1539 else // Opcode == ARM::FCONSTD
1540 ModifiedImm = (Imm & 0x80000000) >> 24 | // a
1541 (Imm & 0x007F0000) >> 16; // bcdefgh
1543 // Insts{19-16} = abcd, Insts{3-0} = efgh
1544 Binary |= ((ModifiedImm & 0xF0) >> 4) << 16;
1545 Binary |= (ModifiedImm & 0xF);
1553 static unsigned encodeNEONRd(const MachineInstr &MI, unsigned OpIdx) {
1554 unsigned RegD = MI.getOperand(OpIdx).getReg();
1555 unsigned Binary = 0;
1556 RegD = ARMRegisterInfo::getRegisterNumbering(RegD);
1557 Binary |= (RegD & 0xf) << ARMII::RegRdShift;
1558 Binary |= ((RegD >> 4) & 1) << ARMII::D_BitShift;
1562 static unsigned encodeNEONRm(const MachineInstr &MI, unsigned OpIdx) {
1563 unsigned RegM = MI.getOperand(OpIdx).getReg();
1564 unsigned Binary = 0;
1565 RegM = ARMRegisterInfo::getRegisterNumbering(RegM);
1566 Binary |= (RegM & 0xf);
1567 Binary |= ((RegM >> 4) & 1) << ARMII::M_BitShift;
1571 void ARMCodeEmitter::emitNEON1RegModImmInstruction(const MachineInstr &MI) {
1572 unsigned Binary = getBinaryCodeForInstr(MI);
1573 // Destination register is encoded in Dd.
1574 Binary |= encodeNEONRd(MI, 0);
1575 // Immediate fields: Op, Cmode, I, Imm3, Imm4
1576 unsigned Imm = MI.getOperand(1).getImm();
1577 unsigned Op = (Imm >> 12) & 1;
1578 Binary |= (Op << 5);
1579 unsigned Cmode = (Imm >> 8) & 0xf;
1580 Binary |= (Cmode << 8);
1581 unsigned I = (Imm >> 7) & 1;
1582 Binary |= (I << 24);
1583 unsigned Imm3 = (Imm >> 4) & 0x7;
1584 Binary |= (Imm3 << 16);
1585 unsigned Imm4 = Imm & 0xf;
1590 void ARMCodeEmitter::emitNEON2RegInstruction(const MachineInstr &MI) {
1591 unsigned Binary = getBinaryCodeForInstr(MI);
1592 // Destination register is encoded in Dd.
1593 Binary |= encodeNEONRd(MI, 0);
1594 Binary |= encodeNEONRm(MI, 1);
1595 // FIXME: This does not handle VDUPfdf or VDUPfqf.
1599 #include "ARMGenCodeEmitter.inc"