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);
144 void emitNEON3RegInstruction(const MachineInstr &MI);
146 /// getMachineOpValue - Return binary encoding of operand. If the machine
147 /// operand requires relocation, record the relocation and return zero.
148 unsigned getMachineOpValue(const MachineInstr &MI,const MachineOperand &MO);
149 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) {
150 return getMachineOpValue(MI, MI.getOperand(OpIdx));
153 /// getMovi32Value - Return binary encoding of operand for movw/movt. If the
154 /// machine operand requires relocation, record the relocation and return
156 unsigned getMovi32Value(const MachineInstr &MI,const MachineOperand &MO,
158 unsigned getMovi32Value(const MachineInstr &MI, unsigned OpIdx,
160 return getMovi32Value(MI, MI.getOperand(OpIdx), Reloc);
163 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
165 unsigned getShiftOp(unsigned Imm) const ;
167 /// Routines that handle operands which add machine relocations which are
168 /// fixed up by the relocation stage.
169 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
170 bool MayNeedFarStub, bool Indirect,
172 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
173 void emitConstPoolAddress(unsigned CPI, unsigned Reloc);
174 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc);
175 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
176 intptr_t JTBase = 0);
180 char ARMCodeEmitter::ID = 0;
182 /// createARMJITCodeEmitterPass - Return a pass that emits the collected ARM
183 /// code to the specified MCE object.
184 FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
185 JITCodeEmitter &JCE) {
186 return new ARMCodeEmitter(TM, JCE);
189 bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
190 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
191 MF.getTarget().getRelocationModel() != Reloc::Static) &&
192 "JIT relocation model must be set to static or default!");
193 JTI = ((ARMTargetMachine &)MF.getTarget()).getJITInfo();
194 II = ((const ARMTargetMachine &)MF.getTarget()).getInstrInfo();
195 TD = ((const ARMTargetMachine &)MF.getTarget()).getTargetData();
196 Subtarget = &TM.getSubtarget<ARMSubtarget>();
197 MCPEs = &MF.getConstantPool()->getConstants();
199 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
200 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
201 JTI->Initialize(MF, IsPIC);
202 MMI = &getAnalysis<MachineModuleInfo>();
203 MCE.setModuleInfo(MMI);
206 DEBUG(errs() << "JITTing function '"
207 << MF.getFunction()->getName() << "'\n");
208 MCE.startFunction(MF);
209 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
211 MCE.StartMachineBasicBlock(MBB);
212 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
216 } while (MCE.finishFunction(MF));
221 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
223 unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
224 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
225 default: llvm_unreachable("Unknown shift opc!");
226 case ARM_AM::asr: return 2;
227 case ARM_AM::lsl: return 0;
228 case ARM_AM::lsr: return 1;
230 case ARM_AM::rrx: return 3;
235 /// getMovi32Value - Return binary encoding of operand for movw/movt. If the
236 /// machine operand requires relocation, record the relocation and return zero.
237 unsigned ARMCodeEmitter::getMovi32Value(const MachineInstr &MI,
238 const MachineOperand &MO,
240 assert(((Reloc == ARM::reloc_arm_movt) || (Reloc == ARM::reloc_arm_movw))
241 && "Relocation to this function should be for movt or movw");
244 return static_cast<unsigned>(MO.getImm());
245 else if (MO.isGlobal())
246 emitGlobalAddress(MO.getGlobal(), Reloc, true, false);
247 else if (MO.isSymbol())
248 emitExternalSymbolAddress(MO.getSymbolName(), Reloc);
250 emitMachineBasicBlock(MO.getMBB(), Reloc);
255 llvm_unreachable("Unsupported operand type for movw/movt");
260 /// getMachineOpValue - Return binary encoding of operand. If the machine
261 /// operand requires relocation, record the relocation and return zero.
262 unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
263 const MachineOperand &MO) {
265 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
267 return static_cast<unsigned>(MO.getImm());
268 else if (MO.isGlobal())
269 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true, false);
270 else if (MO.isSymbol())
271 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
272 else if (MO.isCPI()) {
273 const TargetInstrDesc &TID = MI.getDesc();
274 // For VFP load, the immediate offset is multiplied by 4.
275 unsigned Reloc = ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
276 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
277 emitConstPoolAddress(MO.getIndex(), Reloc);
278 } else if (MO.isJTI())
279 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
281 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
291 /// emitGlobalAddress - Emit the specified address to the code stream.
293 void ARMCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
294 bool MayNeedFarStub, bool Indirect,
296 MachineRelocation MR = Indirect
297 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
298 const_cast<GlobalValue *>(GV),
299 ACPV, MayNeedFarStub)
300 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
301 const_cast<GlobalValue *>(GV), ACPV,
303 MCE.addRelocation(MR);
306 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
307 /// be emitted to the current location in the function, and allow it to be PC
309 void ARMCodeEmitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
310 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
314 /// emitConstPoolAddress - Arrange for the address of an constant pool
315 /// to be emitted to the current location in the function, and allow it to be PC
317 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) {
318 // Tell JIT emitter we'll resolve the address.
319 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
320 Reloc, CPI, 0, true));
323 /// emitJumpTableAddress - Arrange for the address of a jump table to
324 /// be emitted to the current location in the function, and allow it to be PC
326 void ARMCodeEmitter::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) {
327 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
328 Reloc, JTIndex, 0, true));
331 /// emitMachineBasicBlock - Emit the specified address basic block.
332 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
333 unsigned Reloc, intptr_t JTBase) {
334 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
338 void ARMCodeEmitter::emitWordLE(unsigned Binary) {
339 DEBUG(errs() << " 0x";
340 errs().write_hex(Binary) << "\n");
341 MCE.emitWordLE(Binary);
344 void ARMCodeEmitter::emitDWordLE(uint64_t Binary) {
345 DEBUG(errs() << " 0x";
346 errs().write_hex(Binary) << "\n");
347 MCE.emitDWordLE(Binary);
350 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
351 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
353 MCE.processDebugLoc(MI.getDebugLoc(), true);
355 ++NumEmitted; // Keep track of the # of mi's emitted
356 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
358 llvm_unreachable("Unhandled instruction encoding format!");
362 emitPseudoInstruction(MI);
365 case ARMII::DPSoRegFrm:
366 emitDataProcessingInstruction(MI);
370 emitLoadStoreInstruction(MI);
372 case ARMII::LdMiscFrm:
373 case ARMII::StMiscFrm:
374 emitMiscLoadStoreInstruction(MI);
376 case ARMII::LdStMulFrm:
377 emitLoadStoreMultipleInstruction(MI);
380 emitMulFrmInstruction(MI);
383 emitExtendInstruction(MI);
385 case ARMII::ArithMiscFrm:
386 emitMiscArithInstruction(MI);
389 emitBranchInstruction(MI);
391 case ARMII::BrMiscFrm:
392 emitMiscBranchInstruction(MI);
395 case ARMII::VFPUnaryFrm:
396 case ARMII::VFPBinaryFrm:
397 emitVFPArithInstruction(MI);
399 case ARMII::VFPConv1Frm:
400 case ARMII::VFPConv2Frm:
401 case ARMII::VFPConv3Frm:
402 case ARMII::VFPConv4Frm:
403 case ARMII::VFPConv5Frm:
404 emitVFPConversionInstruction(MI);
406 case ARMII::VFPLdStFrm:
407 emitVFPLoadStoreInstruction(MI);
409 case ARMII::VFPLdStMulFrm:
410 emitVFPLoadStoreMultipleInstruction(MI);
412 case ARMII::VFPMiscFrm:
413 emitMiscInstruction(MI);
415 // NEON instructions.
416 case ARMII::N1RegModImmFrm:
417 emitNEON1RegModImmInstruction(MI);
419 case ARMII::N2RegFrm:
420 emitNEON2RegInstruction(MI);
422 case ARMII::N3RegFrm:
423 emitNEON3RegInstruction(MI);
426 MCE.processDebugLoc(MI.getDebugLoc(), false);
429 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
430 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
431 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
432 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
434 // Remember the CONSTPOOL_ENTRY address for later relocation.
435 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
437 // Emit constpool island entry. In most cases, the actual values will be
438 // resolved and relocated after code emission.
439 if (MCPE.isMachineConstantPoolEntry()) {
440 ARMConstantPoolValue *ACPV =
441 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
443 DEBUG(errs() << " ** ARM constant pool #" << CPI << " @ "
444 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n');
446 assert(ACPV->isGlobalValue() && "unsupported constant pool value");
447 const GlobalValue *GV = ACPV->getGV();
449 Reloc::Model RelocM = TM.getRelocationModel();
450 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
452 Subtarget->GVIsIndirectSymbol(GV, RelocM),
455 emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
459 const Constant *CV = MCPE.Val.ConstVal;
462 errs() << " ** Constant pool #" << CPI << " @ "
463 << (void*)MCE.getCurrentPCValue() << " ";
464 if (const Function *F = dyn_cast<Function>(CV))
465 errs() << F->getName();
471 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
472 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV), false);
474 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
475 uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
477 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
478 if (CFP->getType()->isFloatTy())
479 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
480 else if (CFP->getType()->isDoubleTy())
481 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
483 llvm_unreachable("Unable to handle this constantpool entry!");
486 llvm_unreachable("Unable to handle this constantpool entry!");
491 void ARMCodeEmitter::emitMOVi32immInstruction(const MachineInstr &MI) {
492 const MachineOperand &MO0 = MI.getOperand(0);
493 const MachineOperand &MO1 = MI.getOperand(1);
495 // Emit the 'movw' instruction.
496 unsigned Binary = 0x30 << 20; // mov: Insts{27-20} = 0b00110000
498 unsigned Lo16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movw) & 0xFFFF;
500 // Set the conditional execution predicate.
501 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
504 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
506 // Encode imm16 as imm4:imm12
507 Binary |= Lo16 & 0xFFF; // Insts{11-0} = imm12
508 Binary |= ((Lo16 >> 12) & 0xF) << 16; // Insts{19-16} = imm4
511 unsigned Hi16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movt) >> 16;
512 // Emit the 'movt' instruction.
513 Binary = 0x34 << 20; // movt: Insts{27-20} = 0b00110100
515 // Set the conditional execution predicate.
516 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
519 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
521 // Encode imm16 as imm4:imm1, same as movw above.
522 Binary |= Hi16 & 0xFFF;
523 Binary |= ((Hi16 >> 12) & 0xF) << 16;
527 void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
528 const MachineOperand &MO0 = MI.getOperand(0);
529 const MachineOperand &MO1 = MI.getOperand(1);
530 assert(MO1.isImm() && ARM_AM::isSOImmTwoPartVal(MO1.getImm()) &&
531 "Not a valid so_imm value!");
532 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
533 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
535 // Emit the 'mov' instruction.
536 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
538 // Set the conditional execution predicate.
539 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
542 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
545 // Set bit I(25) to identify this is the immediate form of <shifter_op>
546 Binary |= 1 << ARMII::I_BitShift;
547 Binary |= getMachineSoImmOpValue(V1);
550 // Now the 'orr' instruction.
551 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
553 // Set the conditional execution predicate.
554 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
557 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
560 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
563 // Set bit I(25) to identify this is the immediate form of <shifter_op>
564 Binary |= 1 << ARMII::I_BitShift;
565 Binary |= getMachineSoImmOpValue(V2);
569 void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
570 // It's basically add r, pc, (LJTI - $+8)
572 const TargetInstrDesc &TID = MI.getDesc();
574 // Emit the 'add' instruction.
575 unsigned Binary = 0x4 << 21; // add: Insts{24-31} = 0b0100
577 // Set the conditional execution predicate
578 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
580 // Encode S bit if MI modifies CPSR.
581 Binary |= getAddrModeSBit(MI, TID);
584 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
586 // Encode Rn which is PC.
587 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
589 // Encode the displacement.
590 Binary |= 1 << ARMII::I_BitShift;
591 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
596 void ARMCodeEmitter::emitPseudoMoveInstruction(const MachineInstr &MI) {
597 unsigned Opcode = MI.getDesc().Opcode;
599 // Part of binary is determined by TableGn.
600 unsigned Binary = getBinaryCodeForInstr(MI);
602 // Set the conditional execution predicate
603 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
605 // Encode S bit if MI modifies CPSR.
606 if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag)
607 Binary |= 1 << ARMII::S_BitShift;
609 // Encode register def if there is one.
610 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
612 // Encode the shift operation.
619 case ARM::MOVsrl_flag:
621 Binary |= (0x2 << 4) | (1 << 7);
623 case ARM::MOVsra_flag:
625 Binary |= (0x4 << 4) | (1 << 7);
629 // Encode register Rm.
630 Binary |= getMachineOpValue(MI, 1);
635 void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
636 DEBUG(errs() << " ** LPC" << LabelID << " @ "
637 << (void*)MCE.getCurrentPCValue() << '\n');
638 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
641 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
642 unsigned Opcode = MI.getDesc().Opcode;
645 llvm_unreachable("ARMCodeEmitter::emitPseudoInstruction");
646 case TargetOpcode::INLINEASM: {
647 // We allow inline assembler nodes with empty bodies - they can
648 // implicitly define registers, which is ok for JIT.
649 if (MI.getOperand(0).getSymbolName()[0]) {
650 report_fatal_error("JIT does not support inline asm!");
654 case TargetOpcode::DBG_LABEL:
655 case TargetOpcode::EH_LABEL:
656 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
658 case TargetOpcode::IMPLICIT_DEF:
659 case TargetOpcode::KILL:
662 case ARM::CONSTPOOL_ENTRY:
663 emitConstPoolInstruction(MI);
666 // Remember of the address of the PC label for relocation later.
667 addPCLabel(MI.getOperand(2).getImm());
668 // PICADD is just an add instruction that implicitly read pc.
669 emitDataProcessingInstruction(MI, 0, ARM::PC);
676 // Remember of the address of the PC label for relocation later.
677 addPCLabel(MI.getOperand(2).getImm());
678 // These are just load / store instructions that implicitly read pc.
679 emitLoadStoreInstruction(MI, 0, ARM::PC);
686 // Remember of the address of the PC label for relocation later.
687 addPCLabel(MI.getOperand(2).getImm());
688 // These are just load / store instructions that implicitly read pc.
689 emitMiscLoadStoreInstruction(MI, ARM::PC);
694 emitMOVi32immInstruction(MI);
697 case ARM::MOVi2pieces:
698 // Two instructions to materialize a constant.
699 emitMOVi2piecesInstruction(MI);
701 case ARM::LEApcrelJT:
702 // Materialize jumptable address.
703 emitLEApcrelJTInstruction(MI);
706 case ARM::MOVsrl_flag:
707 case ARM::MOVsra_flag:
708 emitPseudoMoveInstruction(MI);
713 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
714 const TargetInstrDesc &TID,
715 const MachineOperand &MO,
717 unsigned Binary = getMachineOpValue(MI, MO);
719 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
720 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
721 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
723 // Encode the shift opcode.
725 unsigned Rs = MO1.getReg();
727 // Set shift operand (bit[7:4]).
732 // RRX - 0110 and bit[11:8] clear.
734 default: llvm_unreachable("Unknown shift opc!");
735 case ARM_AM::lsl: SBits = 0x1; break;
736 case ARM_AM::lsr: SBits = 0x3; break;
737 case ARM_AM::asr: SBits = 0x5; break;
738 case ARM_AM::ror: SBits = 0x7; break;
739 case ARM_AM::rrx: SBits = 0x6; break;
742 // Set shift operand (bit[6:4]).
748 default: llvm_unreachable("Unknown shift opc!");
749 case ARM_AM::lsl: SBits = 0x0; break;
750 case ARM_AM::lsr: SBits = 0x2; break;
751 case ARM_AM::asr: SBits = 0x4; break;
752 case ARM_AM::ror: SBits = 0x6; break;
755 Binary |= SBits << 4;
756 if (SOpc == ARM_AM::rrx)
759 // Encode the shift operation Rs or shift_imm (except rrx).
761 // Encode Rs bit[11:8].
762 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
764 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
767 // Encode shift_imm bit[11:7].
768 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
771 unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
772 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
773 assert(SoImmVal != -1 && "Not a valid so_imm value!");
775 // Encode rotate_imm.
776 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
777 << ARMII::SoRotImmShift;
780 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
784 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
785 const TargetInstrDesc &TID) const {
786 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
787 const MachineOperand &MO = MI.getOperand(i-1);
788 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
789 return 1 << ARMII::S_BitShift;
794 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
796 unsigned ImplicitRn) {
797 const TargetInstrDesc &TID = MI.getDesc();
799 // Part of binary is determined by TableGn.
800 unsigned Binary = getBinaryCodeForInstr(MI);
802 // Set the conditional execution predicate
803 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
805 // Encode S bit if MI modifies CPSR.
806 Binary |= getAddrModeSBit(MI, TID);
808 // Encode register def if there is one.
809 unsigned NumDefs = TID.getNumDefs();
812 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
814 // Special handling for implicit use (e.g. PC).
815 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
816 << ARMII::RegRdShift);
818 if (TID.Opcode == ARM::MOVi16) {
819 // Get immediate from MI.
820 unsigned Lo16 = getMovi32Value(MI, MI.getOperand(OpIdx),
821 ARM::reloc_arm_movw);
822 // Encode imm which is the same as in emitMOVi32immInstruction().
823 Binary |= Lo16 & 0xFFF;
824 Binary |= ((Lo16 >> 12) & 0xF) << 16;
827 } else if(TID.Opcode == ARM::MOVTi16) {
828 unsigned Hi16 = (getMovi32Value(MI, MI.getOperand(OpIdx),
829 ARM::reloc_arm_movt) >> 16);
830 Binary |= Hi16 & 0xFFF;
831 Binary |= ((Hi16 >> 12) & 0xF) << 16;
834 } else if ((TID.Opcode == ARM::BFC) || (TID.Opcode == ARM::BFI)) {
835 uint32_t v = ~MI.getOperand(2).getImm();
836 int32_t lsb = CountTrailingZeros_32(v);
837 int32_t msb = (32 - CountLeadingZeros_32(v)) - 1;
838 // Instr{20-16} = msb, Instr{11-7} = lsb
839 Binary |= (msb & 0x1F) << 16;
840 Binary |= (lsb & 0x1F) << 7;
843 } else if ((TID.Opcode == ARM::UBFX) || (TID.Opcode == ARM::SBFX)) {
844 // Encode Rn in Instr{0-3}
845 Binary |= getMachineOpValue(MI, OpIdx++);
847 uint32_t lsb = MI.getOperand(OpIdx++).getImm();
848 uint32_t widthm1 = MI.getOperand(OpIdx++).getImm() - 1;
850 // Instr{20-16} = widthm1, Instr{11-7} = lsb
851 Binary |= (widthm1 & 0x1F) << 16;
852 Binary |= (lsb & 0x1F) << 7;
857 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
858 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
861 // Encode first non-shifter register operand if there is one.
862 bool isUnary = TID.TSFlags & ARMII::UnaryDP;
865 // Special handling for implicit use (e.g. PC).
866 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
867 << ARMII::RegRnShift);
869 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
874 // Encode shifter operand.
875 const MachineOperand &MO = MI.getOperand(OpIdx);
876 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
878 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
883 // Encode register Rm.
884 emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
889 Binary |= getMachineSoImmOpValue((unsigned)MO.getImm());
894 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
896 unsigned ImplicitRn) {
897 const TargetInstrDesc &TID = MI.getDesc();
898 unsigned Form = TID.TSFlags & ARMII::FormMask;
899 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
901 // Part of binary is determined by TableGn.
902 unsigned Binary = getBinaryCodeForInstr(MI);
904 // Set the conditional execution predicate
905 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
909 // Operand 0 of a pre- and post-indexed store is the address base
910 // writeback. Skip it.
911 bool Skipped = false;
912 if (IsPrePost && Form == ARMII::StFrm) {
919 // Special handling for implicit use (e.g. PC).
920 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
921 << ARMII::RegRdShift);
923 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
925 // Set second operand
927 // Special handling for implicit use (e.g. PC).
928 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
929 << ARMII::RegRnShift);
931 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
933 // If this is a two-address operand, skip it. e.g. LDR_PRE.
934 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
937 const MachineOperand &MO2 = MI.getOperand(OpIdx);
938 unsigned AM2Opc = (ImplicitRn == ARM::PC)
939 ? 0 : MI.getOperand(OpIdx+1).getImm();
941 // Set bit U(23) according to sign of immed value (positive or negative).
942 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
944 if (!MO2.getReg()) { // is immediate
945 if (ARM_AM::getAM2Offset(AM2Opc))
946 // Set the value of offset_12 field
947 Binary |= ARM_AM::getAM2Offset(AM2Opc);
952 // Set bit I(25), because this is not in immediate enconding.
953 Binary |= 1 << ARMII::I_BitShift;
954 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
955 // Set bit[3:0] to the corresponding Rm register
956 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
958 // If this instr is in scaled register offset/index instruction, set
959 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
960 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
961 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
962 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
968 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
969 unsigned ImplicitRn) {
970 const TargetInstrDesc &TID = MI.getDesc();
971 unsigned Form = TID.TSFlags & ARMII::FormMask;
972 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
974 // Part of binary is determined by TableGn.
975 unsigned Binary = getBinaryCodeForInstr(MI);
977 // Set the conditional execution predicate
978 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
982 // Operand 0 of a pre- and post-indexed store is the address base
983 // writeback. Skip it.
984 bool Skipped = false;
985 if (IsPrePost && Form == ARMII::StMiscFrm) {
991 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
993 // Skip LDRD and STRD's second operand.
994 if (TID.Opcode == ARM::LDRD || TID.Opcode == ARM::STRD)
997 // Set second operand
999 // Special handling for implicit use (e.g. PC).
1000 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
1001 << ARMII::RegRnShift);
1003 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1005 // If this is a two-address operand, skip it. e.g. LDRH_POST.
1006 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1009 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1010 unsigned AM3Opc = (ImplicitRn == ARM::PC)
1011 ? 0 : MI.getOperand(OpIdx+1).getImm();
1013 // Set bit U(23) according to sign of immed value (positive or negative)
1014 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
1017 // If this instr is in register offset/index encoding, set bit[3:0]
1018 // to the corresponding Rm register.
1020 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
1025 // This instr is in immediate offset/index encoding, set bit 22 to 1.
1026 Binary |= 1 << ARMII::AM3_I_BitShift;
1027 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
1029 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
1030 Binary |= (ImmOffs & 0xF); // immedL
1036 static unsigned getAddrModeUPBits(unsigned Mode) {
1037 unsigned Binary = 0;
1039 // Set addressing mode by modifying bits U(23) and P(24)
1040 // IA - Increment after - bit U = 1 and bit P = 0
1041 // IB - Increment before - bit U = 1 and bit P = 1
1042 // DA - Decrement after - bit U = 0 and bit P = 0
1043 // DB - Decrement before - bit U = 0 and bit P = 1
1045 default: llvm_unreachable("Unknown addressing sub-mode!");
1046 case ARM_AM::da: break;
1047 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
1048 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
1049 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
1055 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
1056 const TargetInstrDesc &TID = MI.getDesc();
1057 bool IsUpdating = (TID.TSFlags & ARMII::IndexModeMask) != 0;
1059 // Part of binary is determined by TableGn.
1060 unsigned Binary = getBinaryCodeForInstr(MI);
1062 // Set the conditional execution predicate
1063 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1065 // Skip operand 0 of an instruction with base register update.
1070 // Set base address operand
1071 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1073 // Set addressing mode by modifying bits U(23) and P(24)
1074 const MachineOperand &MO = MI.getOperand(OpIdx++);
1075 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
1079 Binary |= 0x1 << ARMII::W_BitShift;
1082 for (unsigned i = OpIdx+2, e = MI.getNumOperands(); i != e; ++i) {
1083 const MachineOperand &MO = MI.getOperand(i);
1084 if (!MO.isReg() || MO.isImplicit())
1086 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
1087 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
1089 Binary |= 0x1 << RegNum;
1095 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
1096 const TargetInstrDesc &TID = MI.getDesc();
1098 // Part of binary is determined by TableGn.
1099 unsigned Binary = getBinaryCodeForInstr(MI);
1101 // Set the conditional execution predicate
1102 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1104 // Encode S bit if MI modifies CPSR.
1105 Binary |= getAddrModeSBit(MI, TID);
1107 // 32x32->64bit operations have two destination registers. The number
1108 // of register definitions will tell us if that's what we're dealing with.
1110 if (TID.getNumDefs() == 2)
1111 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
1114 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
1117 Binary |= getMachineOpValue(MI, OpIdx++);
1120 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
1122 // Many multiple instructions (e.g. MLA) have three src operands. Encode
1123 // it as Rn (for multiply, that's in the same offset as RdLo.
1124 if (TID.getNumOperands() > OpIdx &&
1125 !TID.OpInfo[OpIdx].isPredicate() &&
1126 !TID.OpInfo[OpIdx].isOptionalDef())
1127 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
1132 void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
1133 const TargetInstrDesc &TID = MI.getDesc();
1135 // Part of binary is determined by TableGn.
1136 unsigned Binary = getBinaryCodeForInstr(MI);
1138 // Set the conditional execution predicate
1139 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1144 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1146 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
1147 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1149 // Two register operand form.
1151 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
1154 Binary |= getMachineOpValue(MI, MO2);
1157 Binary |= getMachineOpValue(MI, MO1);
1160 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
1161 if (MI.getOperand(OpIdx).isImm() &&
1162 !TID.OpInfo[OpIdx].isPredicate() &&
1163 !TID.OpInfo[OpIdx].isOptionalDef())
1164 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
1169 void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
1170 const TargetInstrDesc &TID = MI.getDesc();
1172 // Part of binary is determined by TableGn.
1173 unsigned Binary = getBinaryCodeForInstr(MI);
1175 // Set the conditional execution predicate
1176 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1181 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1183 const MachineOperand &MO = MI.getOperand(OpIdx++);
1184 if (OpIdx == TID.getNumOperands() ||
1185 TID.OpInfo[OpIdx].isPredicate() ||
1186 TID.OpInfo[OpIdx].isOptionalDef()) {
1187 // Encode Rm and it's done.
1188 Binary |= getMachineOpValue(MI, MO);
1194 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1197 Binary |= getMachineOpValue(MI, OpIdx++);
1199 // Encode shift_imm.
1200 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
1201 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1202 Binary |= ShiftAmt << ARMII::ShiftShift;
1207 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
1208 const TargetInstrDesc &TID = MI.getDesc();
1210 if (TID.Opcode == ARM::TPsoft) {
1211 llvm_unreachable("ARM::TPsoft FIXME"); // FIXME
1214 // Part of binary is determined by TableGn.
1215 unsigned Binary = getBinaryCodeForInstr(MI);
1217 // Set the conditional execution predicate
1218 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1220 // Set signed_immed_24 field
1221 Binary |= getMachineOpValue(MI, 0);
1226 void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
1227 // Remember the base address of the inline jump table.
1228 uintptr_t JTBase = MCE.getCurrentPCValue();
1229 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
1230 DEBUG(errs() << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase
1233 // Now emit the jump table entries.
1234 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1235 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1237 // DestBB address - JT base.
1238 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
1240 // Absolute DestBB address.
1241 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1246 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
1247 const TargetInstrDesc &TID = MI.getDesc();
1249 // Handle jump tables.
1250 if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
1251 // First emit a ldr pc, [] instruction.
1252 emitDataProcessingInstruction(MI, ARM::PC);
1254 // Then emit the inline jump table.
1256 (TID.Opcode == ARM::BR_JTr)
1257 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1258 emitInlineJumpTable(JTIndex);
1260 } else if (TID.Opcode == ARM::BR_JTm) {
1261 // First emit a ldr pc, [] instruction.
1262 emitLoadStoreInstruction(MI, ARM::PC);
1264 // Then emit the inline jump table.
1265 emitInlineJumpTable(MI.getOperand(3).getIndex());
1269 // Part of binary is determined by TableGn.
1270 unsigned Binary = getBinaryCodeForInstr(MI);
1272 // Set the conditional execution predicate
1273 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1275 if (TID.Opcode == ARM::BX_RET || TID.Opcode == ARM::MOVPCLR)
1276 // The return register is LR.
1277 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
1279 // otherwise, set the return register
1280 Binary |= getMachineOpValue(MI, 0);
1285 static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) {
1286 unsigned RegD = MI.getOperand(OpIdx).getReg();
1287 unsigned Binary = 0;
1288 bool isSPVFP = false;
1289 RegD = ARMRegisterInfo::getRegisterNumbering(RegD, &isSPVFP);
1291 Binary |= RegD << ARMII::RegRdShift;
1293 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1294 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1299 static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) {
1300 unsigned RegN = MI.getOperand(OpIdx).getReg();
1301 unsigned Binary = 0;
1302 bool isSPVFP = false;
1303 RegN = ARMRegisterInfo::getRegisterNumbering(RegN, &isSPVFP);
1305 Binary |= RegN << ARMII::RegRnShift;
1307 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1308 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1313 static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) {
1314 unsigned RegM = MI.getOperand(OpIdx).getReg();
1315 unsigned Binary = 0;
1316 bool isSPVFP = false;
1317 RegM = ARMRegisterInfo::getRegisterNumbering(RegM, &isSPVFP);
1321 Binary |= ((RegM & 0x1E) >> 1);
1322 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
1327 void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) {
1328 const TargetInstrDesc &TID = MI.getDesc();
1330 // Part of binary is determined by TableGn.
1331 unsigned Binary = getBinaryCodeForInstr(MI);
1333 // Set the conditional execution predicate
1334 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1337 assert((Binary & ARMII::D_BitShift) == 0 &&
1338 (Binary & ARMII::N_BitShift) == 0 &&
1339 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1342 Binary |= encodeVFPRd(MI, OpIdx++);
1344 // If this is a two-address operand, skip it, e.g. FMACD.
1345 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1349 if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
1350 Binary |= encodeVFPRn(MI, OpIdx++);
1352 if (OpIdx == TID.getNumOperands() ||
1353 TID.OpInfo[OpIdx].isPredicate() ||
1354 TID.OpInfo[OpIdx].isOptionalDef()) {
1355 // FCMPEZD etc. has only one operand.
1361 Binary |= encodeVFPRm(MI, OpIdx);
1366 void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) {
1367 const TargetInstrDesc &TID = MI.getDesc();
1368 unsigned Form = TID.TSFlags & ARMII::FormMask;
1370 // Part of binary is determined by TableGn.
1371 unsigned Binary = getBinaryCodeForInstr(MI);
1373 // Set the conditional execution predicate
1374 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1378 case ARMII::VFPConv1Frm:
1379 case ARMII::VFPConv2Frm:
1380 case ARMII::VFPConv3Frm:
1382 Binary |= encodeVFPRd(MI, 0);
1384 case ARMII::VFPConv4Frm:
1386 Binary |= encodeVFPRn(MI, 0);
1388 case ARMII::VFPConv5Frm:
1390 Binary |= encodeVFPRm(MI, 0);
1396 case ARMII::VFPConv1Frm:
1398 Binary |= encodeVFPRm(MI, 1);
1400 case ARMII::VFPConv2Frm:
1401 case ARMII::VFPConv3Frm:
1403 Binary |= encodeVFPRn(MI, 1);
1405 case ARMII::VFPConv4Frm:
1406 case ARMII::VFPConv5Frm:
1408 Binary |= encodeVFPRd(MI, 1);
1412 if (Form == ARMII::VFPConv5Frm)
1414 Binary |= encodeVFPRn(MI, 2);
1415 else if (Form == ARMII::VFPConv3Frm)
1417 Binary |= encodeVFPRm(MI, 2);
1422 void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
1423 // Part of binary is determined by TableGn.
1424 unsigned Binary = getBinaryCodeForInstr(MI);
1426 // Set the conditional execution predicate
1427 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1432 Binary |= encodeVFPRd(MI, OpIdx++);
1434 // Encode address base.
1435 const MachineOperand &Base = MI.getOperand(OpIdx++);
1436 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1438 // If there is a non-zero immediate offset, encode it.
1440 const MachineOperand &Offset = MI.getOperand(OpIdx);
1441 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1442 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1443 Binary |= 1 << ARMII::U_BitShift;
1450 // If immediate offset is omitted, default to +0.
1451 Binary |= 1 << ARMII::U_BitShift;
1457 ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
1458 const TargetInstrDesc &TID = MI.getDesc();
1459 bool IsUpdating = (TID.TSFlags & ARMII::IndexModeMask) != 0;
1461 // Part of binary is determined by TableGn.
1462 unsigned Binary = getBinaryCodeForInstr(MI);
1464 // Set the conditional execution predicate
1465 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1467 // Skip operand 0 of an instruction with base register update.
1472 // Set base address operand
1473 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1475 // Set addressing mode by modifying bits U(23) and P(24)
1476 const MachineOperand &MO = MI.getOperand(OpIdx++);
1477 Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm()));
1481 Binary |= 0x1 << ARMII::W_BitShift;
1483 // First register is encoded in Dd.
1484 Binary |= encodeVFPRd(MI, OpIdx+2);
1486 // Number of registers are encoded in offset field.
1487 unsigned NumRegs = 1;
1488 for (unsigned i = OpIdx+3, e = MI.getNumOperands(); i != e; ++i) {
1489 const MachineOperand &MO = MI.getOperand(i);
1490 if (!MO.isReg() || MO.isImplicit())
1494 // Bit 8 will be set if <list> is consecutive 64-bit registers (e.g., D0)
1495 // Otherwise, it will be 0, in the case of 32-bit registers.
1497 Binary |= NumRegs * 2;
1504 void ARMCodeEmitter::emitMiscInstruction(const MachineInstr &MI) {
1505 unsigned Opcode = MI.getDesc().Opcode;
1506 // Part of binary is determined by TableGn.
1507 unsigned Binary = getBinaryCodeForInstr(MI);
1509 // Set the conditional execution predicate
1510 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1514 llvm_unreachable("ARMCodeEmitter::emitMiscInstruction");
1517 // No further encoding needed.
1522 const MachineOperand &MO0 = MI.getOperand(0);
1524 Binary |= ARMRegisterInfo::getRegisterNumbering(MO0.getReg())
1525 << ARMII::RegRdShift;
1530 case ARM::FCONSTS: {
1532 Binary |= encodeVFPRd(MI, 0);
1534 // Encode imm., Table A7-18 VFP modified immediate constants
1535 const MachineOperand &MO1 = MI.getOperand(1);
1536 unsigned Imm = static_cast<unsigned>(MO1.getFPImm()->getValueAPF()
1537 .bitcastToAPInt().getHiBits(32).getLimitedValue());
1538 unsigned ModifiedImm;
1540 if(Opcode == ARM::FCONSTS)
1541 ModifiedImm = (Imm & 0x80000000) >> 24 | // a
1542 (Imm & 0x03F80000) >> 19; // bcdefgh
1543 else // Opcode == ARM::FCONSTD
1544 ModifiedImm = (Imm & 0x80000000) >> 24 | // a
1545 (Imm & 0x007F0000) >> 16; // bcdefgh
1547 // Insts{19-16} = abcd, Insts{3-0} = efgh
1548 Binary |= ((ModifiedImm & 0xF0) >> 4) << 16;
1549 Binary |= (ModifiedImm & 0xF);
1557 static unsigned encodeNEONRd(const MachineInstr &MI, unsigned OpIdx) {
1558 unsigned RegD = MI.getOperand(OpIdx).getReg();
1559 unsigned Binary = 0;
1560 RegD = ARMRegisterInfo::getRegisterNumbering(RegD);
1561 Binary |= (RegD & 0xf) << ARMII::RegRdShift;
1562 Binary |= ((RegD >> 4) & 1) << ARMII::D_BitShift;
1566 static unsigned encodeNEONRn(const MachineInstr &MI, unsigned OpIdx) {
1567 unsigned RegN = MI.getOperand(OpIdx).getReg();
1568 unsigned Binary = 0;
1569 RegN = ARMRegisterInfo::getRegisterNumbering(RegN);
1570 Binary |= (RegN & 0xf) << ARMII::RegRnShift;
1571 Binary |= ((RegN >> 4) & 1) << ARMII::N_BitShift;
1575 static unsigned encodeNEONRm(const MachineInstr &MI, unsigned OpIdx) {
1576 unsigned RegM = MI.getOperand(OpIdx).getReg();
1577 unsigned Binary = 0;
1578 RegM = ARMRegisterInfo::getRegisterNumbering(RegM);
1579 Binary |= (RegM & 0xf);
1580 Binary |= ((RegM >> 4) & 1) << ARMII::M_BitShift;
1584 void ARMCodeEmitter::emitNEON1RegModImmInstruction(const MachineInstr &MI) {
1585 unsigned Binary = getBinaryCodeForInstr(MI);
1586 // Destination register is encoded in Dd.
1587 Binary |= encodeNEONRd(MI, 0);
1588 // Immediate fields: Op, Cmode, I, Imm3, Imm4
1589 unsigned Imm = MI.getOperand(1).getImm();
1590 unsigned Op = (Imm >> 12) & 1;
1591 Binary |= (Op << 5);
1592 unsigned Cmode = (Imm >> 8) & 0xf;
1593 Binary |= (Cmode << 8);
1594 unsigned I = (Imm >> 7) & 1;
1595 Binary |= (I << 24);
1596 unsigned Imm3 = (Imm >> 4) & 0x7;
1597 Binary |= (Imm3 << 16);
1598 unsigned Imm4 = Imm & 0xf;
1603 void ARMCodeEmitter::emitNEON2RegInstruction(const MachineInstr &MI) {
1604 const TargetInstrDesc &TID = MI.getDesc();
1605 unsigned Binary = getBinaryCodeForInstr(MI);
1606 // Destination register is encoded in Dd; source register in Dm.
1608 Binary |= encodeNEONRd(MI, OpIdx++);
1609 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1611 Binary |= encodeNEONRm(MI, OpIdx);
1612 // FIXME: This does not handle VDUPfdf or VDUPfqf.
1616 void ARMCodeEmitter::emitNEON3RegInstruction(const MachineInstr &MI) {
1617 const TargetInstrDesc &TID = MI.getDesc();
1618 unsigned Binary = getBinaryCodeForInstr(MI);
1619 // Destination register is encoded in Dd; source registers in Dn and Dm.
1621 Binary |= encodeNEONRd(MI, OpIdx++);
1622 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1624 Binary |= encodeNEONRn(MI, OpIdx++);
1625 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1627 Binary |= encodeNEONRm(MI, OpIdx);
1628 // FIXME: This does not handle VMOVDneon or VMOVQ.
1632 #include "ARMGenCodeEmitter.inc"