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;
60 void getAnalysisUsage(AnalysisUsage &AU) const {
61 AU.addRequired<MachineModuleInfo>();
62 MachineFunctionPass::getAnalysisUsage(AU);
67 ARMCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
68 : MachineFunctionPass(ID), JTI(0),
69 II((const ARMInstrInfo *)tm.getInstrInfo()),
70 TD(tm.getTargetData()), TM(tm),
71 MCE(mce), MCPEs(0), MJTEs(0),
72 IsPIC(TM.getRelocationModel() == Reloc::PIC_), IsThumb(false) {}
74 /// getBinaryCodeForInstr - This function, generated by the
75 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
76 /// machine instructions.
77 unsigned getBinaryCodeForInstr(const MachineInstr &MI) const;
79 bool runOnMachineFunction(MachineFunction &MF);
81 virtual const char *getPassName() const {
82 return "ARM Machine Code Emitter";
85 void emitInstruction(const MachineInstr &MI);
89 void emitWordLE(unsigned Binary);
90 void emitDWordLE(uint64_t Binary);
91 void emitConstPoolInstruction(const MachineInstr &MI);
92 void emitMOVi32immInstruction(const MachineInstr &MI);
93 void emitMOVi2piecesInstruction(const MachineInstr &MI);
94 void emitLEApcrelJTInstruction(const MachineInstr &MI);
95 void emitPseudoMoveInstruction(const MachineInstr &MI);
96 void addPCLabel(unsigned LabelID);
97 void emitPseudoInstruction(const MachineInstr &MI);
98 unsigned getMachineSoRegOpValue(const MachineInstr &MI,
99 const TargetInstrDesc &TID,
100 const MachineOperand &MO,
103 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 emitSaturateInstruction(const MachineInstr &MI);
128 void emitBranchInstruction(const MachineInstr &MI);
130 void emitInlineJumpTable(unsigned JTIndex);
132 void emitMiscBranchInstruction(const MachineInstr &MI);
134 void emitVFPArithInstruction(const MachineInstr &MI);
136 void emitVFPConversionInstruction(const MachineInstr &MI);
138 void emitVFPLoadStoreInstruction(const MachineInstr &MI);
140 void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI);
142 void emitNEONLaneInstruction(const MachineInstr &MI);
143 void emitNEONDupInstruction(const MachineInstr &MI);
144 void emitNEON1RegModImmInstruction(const MachineInstr &MI);
145 void emitNEON2RegInstruction(const MachineInstr &MI);
146 void emitNEON3RegInstruction(const MachineInstr &MI);
148 /// getMachineOpValue - Return binary encoding of operand. If the machine
149 /// operand requires relocation, record the relocation and return zero.
150 unsigned getMachineOpValue(const MachineInstr &MI,
151 const MachineOperand &MO) const;
152 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) const {
153 return getMachineOpValue(MI, MI.getOperand(OpIdx));
156 // FIXME: The legacy JIT ARMCodeEmitter doesn't rely on the the
157 // TableGen'erated getBinaryCodeForInstr() function to encode any
158 // operand values, instead querying getMachineOpValue() directly for
159 // each operand it needs to encode. Thus, any of the new encoder
160 // helper functions can simply return 0 as the values the return
161 // are already handled elsewhere. They are placeholders to allow this
162 // encoder to continue to function until the MC encoder is sufficiently
163 // far along that this one can be eliminated entirely.
164 unsigned NEONThumb2DataIPostEncoder(const MachineInstr &MI, unsigned Val)
166 unsigned NEONThumb2LoadStorePostEncoder(const MachineInstr &MI,unsigned Val)
168 unsigned NEONThumb2DupPostEncoder(const MachineInstr &MI,unsigned Val)
170 unsigned VFPThumb2PostEncoder(const MachineInstr&MI, unsigned Val)
172 unsigned getAdrLabelOpValue(const MachineInstr &MI, unsigned Op)
174 unsigned getThumbAdrLabelOpValue(const MachineInstr &MI, unsigned Op)
176 unsigned getThumbBLTargetOpValue(const MachineInstr &MI, unsigned Op)
178 unsigned getThumbBLXTargetOpValue(const MachineInstr &MI, unsigned Op)
180 unsigned getThumbBRTargetOpValue(const MachineInstr &MI, unsigned Op)
182 unsigned getThumbBCCTargetOpValue(const MachineInstr &MI, unsigned Op)
184 unsigned getThumbCBTargetOpValue(const MachineInstr &MI, unsigned Op)
186 unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned Op)
188 unsigned getUnconditionalBranchTargetOpValue(const MachineInstr &MI,
189 unsigned Op) const { return 0; }
190 unsigned getARMBranchTargetOpValue(const MachineInstr &MI, unsigned Op)
192 unsigned getCCOutOpValue(const MachineInstr &MI, unsigned Op)
194 unsigned getSOImmOpValue(const MachineInstr &MI, unsigned Op)
196 unsigned getT2SOImmOpValue(const MachineInstr &MI, unsigned Op)
198 unsigned getSORegOpValue(const MachineInstr &MI, unsigned Op)
200 unsigned getThumbAddrModeRegRegOpValue(const MachineInstr &MI, unsigned Op)
202 unsigned getT2AddrModeImm12OpValue(const MachineInstr &MI, unsigned Op)
204 unsigned getT2AddrModeImm8OpValue(const MachineInstr &MI, unsigned Op)
206 unsigned getT2AddrModeImm8s4OpValue(const MachineInstr &MI, unsigned Op)
208 unsigned getT2AddrModeImm8OffsetOpValue(const MachineInstr &MI, unsigned Op)
210 unsigned getT2AddrModeImm12OffsetOpValue(const MachineInstr &MI,unsigned Op)
212 unsigned getT2AddrModeSORegOpValue(const MachineInstr &MI, unsigned Op)
214 unsigned getT2SORegOpValue(const MachineInstr &MI, unsigned Op)
216 unsigned getRotImmOpValue(const MachineInstr &MI, unsigned Op)
218 unsigned getImmMinusOneOpValue(const MachineInstr &MI, unsigned Op)
220 unsigned getT2AdrLabelOpValue(const MachineInstr &MI, unsigned Op)
222 unsigned getAddrMode6AddressOpValue(const MachineInstr &MI, unsigned Op)
224 unsigned getAddrMode6DupAddressOpValue(const MachineInstr &MI, unsigned Op)
226 unsigned getAddrMode6OffsetOpValue(const MachineInstr &MI, unsigned Op)
228 unsigned getBitfieldInvertedMaskOpValue(const MachineInstr &MI,
229 unsigned Op) const { return 0; }
230 unsigned getMsbOpValue(const MachineInstr &MI,
231 unsigned Op) const { return 0; }
232 uint32_t getLdStmModeOpValue(const MachineInstr &MI, unsigned OpIdx)
234 uint32_t getLdStSORegOpValue(const MachineInstr &MI, unsigned OpIdx)
237 unsigned getAddrModeImm12OpValue(const MachineInstr &MI, unsigned Op)
240 // {12} = (U)nsigned (add == '1', sub == '0')
242 const MachineOperand &MO = MI.getOperand(Op);
243 const MachineOperand &MO1 = MI.getOperand(Op + 1);
245 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
248 unsigned Reg = getARMRegisterNumbering(MO.getReg());
249 int32_t Imm12 = MO1.getImm();
251 Binary = Imm12 & 0xfff;
254 Binary |= (Reg << 13);
258 unsigned getHiLo16ImmOpValue(const MachineInstr &MI, unsigned Op) const {
262 uint32_t getAddrMode2OpValue(const MachineInstr &MI, unsigned OpIdx)
264 uint32_t getAddrMode2OffsetOpValue(const MachineInstr &MI, unsigned OpIdx)
266 uint32_t getAddrMode3OffsetOpValue(const MachineInstr &MI, unsigned OpIdx)
268 uint32_t getAddrMode3OpValue(const MachineInstr &MI, unsigned Op)
270 uint32_t getAddrModeThumbSPOpValue(const MachineInstr &MI, unsigned Op)
272 uint32_t getAddrModeSOpValue(const MachineInstr &MI, unsigned Op)
274 uint32_t getAddrModeISOpValue(const MachineInstr &MI, unsigned Op)
276 uint32_t getAddrModePCOpValue(const MachineInstr &MI, unsigned Op)
278 uint32_t getAddrMode5OpValue(const MachineInstr &MI, unsigned Op) const {
280 // {12} = (U)nsigned (add == '1', sub == '0')
282 const MachineOperand &MO = MI.getOperand(Op);
283 const MachineOperand &MO1 = MI.getOperand(Op + 1);
285 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
288 unsigned Reg = getARMRegisterNumbering(MO.getReg());
289 int32_t Imm12 = MO1.getImm();
291 // Special value for #-0
292 if (Imm12 == INT32_MIN)
295 // Immediate is always encoded as positive. The 'U' bit controls add vs
303 uint32_t Binary = Imm12 & 0xfff;
306 Binary |= (Reg << 13);
309 unsigned getNEONVcvtImm32OpValue(const MachineInstr &MI, unsigned Op)
312 unsigned getRegisterListOpValue(const MachineInstr &MI, unsigned Op)
315 /// getMovi32Value - Return binary encoding of operand for movw/movt. If the
316 /// machine operand requires relocation, record the relocation and return
318 unsigned getMovi32Value(const MachineInstr &MI,const MachineOperand &MO,
321 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
323 unsigned getShiftOp(unsigned Imm) const ;
325 /// Routines that handle operands which add machine relocations which are
326 /// fixed up by the relocation stage.
327 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
328 bool MayNeedFarStub, bool Indirect,
329 intptr_t ACPV = 0) const;
330 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
331 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
332 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const;
333 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
334 intptr_t JTBase = 0) const;
338 char ARMCodeEmitter::ID = 0;
340 /// createARMJITCodeEmitterPass - Return a pass that emits the collected ARM
341 /// code to the specified MCE object.
342 FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
343 JITCodeEmitter &JCE) {
344 return new ARMCodeEmitter(TM, JCE);
347 bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
348 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
349 MF.getTarget().getRelocationModel() != Reloc::Static) &&
350 "JIT relocation model must be set to static or default!");
351 JTI = ((ARMTargetMachine &)MF.getTarget()).getJITInfo();
352 II = ((const ARMTargetMachine &)MF.getTarget()).getInstrInfo();
353 TD = ((const ARMTargetMachine &)MF.getTarget()).getTargetData();
354 Subtarget = &TM.getSubtarget<ARMSubtarget>();
355 MCPEs = &MF.getConstantPool()->getConstants();
357 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
358 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
359 IsThumb = MF.getInfo<ARMFunctionInfo>()->isThumbFunction();
360 JTI->Initialize(MF, IsPIC);
361 MMI = &getAnalysis<MachineModuleInfo>();
362 MCE.setModuleInfo(MMI);
365 DEBUG(errs() << "JITTing function '"
366 << MF.getFunction()->getName() << "'\n");
367 MCE.startFunction(MF);
368 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
370 MCE.StartMachineBasicBlock(MBB);
371 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
375 } while (MCE.finishFunction(MF));
380 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
382 unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
383 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
384 default: llvm_unreachable("Unknown shift opc!");
385 case ARM_AM::asr: return 2;
386 case ARM_AM::lsl: return 0;
387 case ARM_AM::lsr: return 1;
389 case ARM_AM::rrx: return 3;
394 /// getMovi32Value - Return binary encoding of operand for movw/movt. If the
395 /// machine operand requires relocation, record the relocation and return zero.
396 unsigned ARMCodeEmitter::getMovi32Value(const MachineInstr &MI,
397 const MachineOperand &MO,
399 assert(((Reloc == ARM::reloc_arm_movt) || (Reloc == ARM::reloc_arm_movw))
400 && "Relocation to this function should be for movt or movw");
403 return static_cast<unsigned>(MO.getImm());
404 else if (MO.isGlobal())
405 emitGlobalAddress(MO.getGlobal(), Reloc, true, false);
406 else if (MO.isSymbol())
407 emitExternalSymbolAddress(MO.getSymbolName(), Reloc);
409 emitMachineBasicBlock(MO.getMBB(), Reloc);
414 llvm_unreachable("Unsupported operand type for movw/movt");
419 /// getMachineOpValue - Return binary encoding of operand. If the machine
420 /// operand requires relocation, record the relocation and return zero.
421 unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
422 const MachineOperand &MO) const {
424 return getARMRegisterNumbering(MO.getReg());
426 return static_cast<unsigned>(MO.getImm());
427 else if (MO.isGlobal())
428 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true, false);
429 else if (MO.isSymbol())
430 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
431 else if (MO.isCPI()) {
432 const TargetInstrDesc &TID = MI.getDesc();
433 // For VFP load, the immediate offset is multiplied by 4.
434 unsigned Reloc = ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
435 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
436 emitConstPoolAddress(MO.getIndex(), Reloc);
437 } else if (MO.isJTI())
438 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
440 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
442 llvm_unreachable("Unable to encode MachineOperand!");
446 /// emitGlobalAddress - Emit the specified address to the code stream.
448 void ARMCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
449 bool MayNeedFarStub, bool Indirect,
450 intptr_t ACPV) const {
451 MachineRelocation MR = Indirect
452 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
453 const_cast<GlobalValue *>(GV),
454 ACPV, MayNeedFarStub)
455 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
456 const_cast<GlobalValue *>(GV), ACPV,
458 MCE.addRelocation(MR);
461 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
462 /// be emitted to the current location in the function, and allow it to be PC
464 void ARMCodeEmitter::
465 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
466 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
470 /// emitConstPoolAddress - Arrange for the address of an constant pool
471 /// to be emitted to the current location in the function, and allow it to be PC
473 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
474 // Tell JIT emitter we'll resolve the address.
475 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
476 Reloc, CPI, 0, true));
479 /// emitJumpTableAddress - Arrange for the address of a jump table to
480 /// be emitted to the current location in the function, and allow it to be PC
482 void ARMCodeEmitter::
483 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
484 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
485 Reloc, JTIndex, 0, true));
488 /// emitMachineBasicBlock - Emit the specified address basic block.
489 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
491 intptr_t JTBase) const {
492 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
496 void ARMCodeEmitter::emitWordLE(unsigned Binary) {
497 DEBUG(errs() << " 0x";
498 errs().write_hex(Binary) << "\n");
499 MCE.emitWordLE(Binary);
502 void ARMCodeEmitter::emitDWordLE(uint64_t Binary) {
503 DEBUG(errs() << " 0x";
504 errs().write_hex(Binary) << "\n");
505 MCE.emitDWordLE(Binary);
508 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
509 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
511 MCE.processDebugLoc(MI.getDebugLoc(), true);
513 ++NumEmitted; // Keep track of the # of mi's emitted
514 switch (MI.getDesc().TSFlags & ARMII::FormMask) {
516 llvm_unreachable("Unhandled instruction encoding format!");
520 if (MI.getOpcode() == ARM::LEApcrelJT) {
521 // Materialize jumptable address.
522 emitLEApcrelJTInstruction(MI);
525 llvm_unreachable("Unhandled instruction encoding!");
528 emitPseudoInstruction(MI);
531 case ARMII::DPSoRegFrm:
532 emitDataProcessingInstruction(MI);
536 emitLoadStoreInstruction(MI);
538 case ARMII::LdMiscFrm:
539 case ARMII::StMiscFrm:
540 emitMiscLoadStoreInstruction(MI);
542 case ARMII::LdStMulFrm:
543 emitLoadStoreMultipleInstruction(MI);
546 emitMulFrmInstruction(MI);
549 emitExtendInstruction(MI);
551 case ARMII::ArithMiscFrm:
552 emitMiscArithInstruction(MI);
555 emitSaturateInstruction(MI);
558 emitBranchInstruction(MI);
560 case ARMII::BrMiscFrm:
561 emitMiscBranchInstruction(MI);
564 case ARMII::VFPUnaryFrm:
565 case ARMII::VFPBinaryFrm:
566 emitVFPArithInstruction(MI);
568 case ARMII::VFPConv1Frm:
569 case ARMII::VFPConv2Frm:
570 case ARMII::VFPConv3Frm:
571 case ARMII::VFPConv4Frm:
572 case ARMII::VFPConv5Frm:
573 emitVFPConversionInstruction(MI);
575 case ARMII::VFPLdStFrm:
576 emitVFPLoadStoreInstruction(MI);
578 case ARMII::VFPLdStMulFrm:
579 emitVFPLoadStoreMultipleInstruction(MI);
582 // NEON instructions.
583 case ARMII::NGetLnFrm:
584 case ARMII::NSetLnFrm:
585 emitNEONLaneInstruction(MI);
588 emitNEONDupInstruction(MI);
590 case ARMII::N1RegModImmFrm:
591 emitNEON1RegModImmInstruction(MI);
593 case ARMII::N2RegFrm:
594 emitNEON2RegInstruction(MI);
596 case ARMII::N3RegFrm:
597 emitNEON3RegInstruction(MI);
600 MCE.processDebugLoc(MI.getDebugLoc(), false);
603 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
604 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
605 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
606 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
608 // Remember the CONSTPOOL_ENTRY address for later relocation.
609 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
611 // Emit constpool island entry. In most cases, the actual values will be
612 // resolved and relocated after code emission.
613 if (MCPE.isMachineConstantPoolEntry()) {
614 ARMConstantPoolValue *ACPV =
615 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
617 DEBUG(errs() << " ** ARM constant pool #" << CPI << " @ "
618 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n');
620 assert(ACPV->isGlobalValue() && "unsupported constant pool value");
621 const GlobalValue *GV = ACPV->getGV();
623 Reloc::Model RelocM = TM.getRelocationModel();
624 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
626 Subtarget->GVIsIndirectSymbol(GV, RelocM),
629 emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
633 const Constant *CV = MCPE.Val.ConstVal;
636 errs() << " ** Constant pool #" << CPI << " @ "
637 << (void*)MCE.getCurrentPCValue() << " ";
638 if (const Function *F = dyn_cast<Function>(CV))
639 errs() << F->getName();
645 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
646 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV), false);
648 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
649 uint32_t Val = uint32_t(*CI->getValue().getRawData());
651 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
652 if (CFP->getType()->isFloatTy())
653 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
654 else if (CFP->getType()->isDoubleTy())
655 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
657 llvm_unreachable("Unable to handle this constantpool entry!");
660 llvm_unreachable("Unable to handle this constantpool entry!");
665 void ARMCodeEmitter::emitMOVi32immInstruction(const MachineInstr &MI) {
666 const MachineOperand &MO0 = MI.getOperand(0);
667 const MachineOperand &MO1 = MI.getOperand(1);
669 // Emit the 'movw' instruction.
670 unsigned Binary = 0x30 << 20; // mov: Insts{27-20} = 0b00110000
672 unsigned Lo16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movw) & 0xFFFF;
674 // Set the conditional execution predicate.
675 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
678 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
680 // Encode imm16 as imm4:imm12
681 Binary |= Lo16 & 0xFFF; // Insts{11-0} = imm12
682 Binary |= ((Lo16 >> 12) & 0xF) << 16; // Insts{19-16} = imm4
685 unsigned Hi16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movt) >> 16;
686 // Emit the 'movt' instruction.
687 Binary = 0x34 << 20; // movt: Insts{27-20} = 0b00110100
689 // Set the conditional execution predicate.
690 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
693 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
695 // Encode imm16 as imm4:imm1, same as movw above.
696 Binary |= Hi16 & 0xFFF;
697 Binary |= ((Hi16 >> 12) & 0xF) << 16;
701 void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
702 const MachineOperand &MO0 = MI.getOperand(0);
703 const MachineOperand &MO1 = MI.getOperand(1);
704 assert(MO1.isImm() && ARM_AM::isSOImmTwoPartVal(MO1.getImm()) &&
705 "Not a valid so_imm value!");
706 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
707 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
709 // Emit the 'mov' instruction.
710 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101
712 // Set the conditional execution predicate.
713 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
716 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
719 // Set bit I(25) to identify this is the immediate form of <shifter_op>
720 Binary |= 1 << ARMII::I_BitShift;
721 Binary |= getMachineSoImmOpValue(V1);
724 // Now the 'orr' instruction.
725 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100
727 // Set the conditional execution predicate.
728 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
731 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
734 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
737 // Set bit I(25) to identify this is the immediate form of <shifter_op>
738 Binary |= 1 << ARMII::I_BitShift;
739 Binary |= getMachineSoImmOpValue(V2);
743 void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
744 // It's basically add r, pc, (LJTI - $+8)
746 const TargetInstrDesc &TID = MI.getDesc();
748 // Emit the 'add' instruction.
749 unsigned Binary = 0x4 << 21; // add: Insts{24-21} = 0b0100
751 // Set the conditional execution predicate
752 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
754 // Encode S bit if MI modifies CPSR.
755 Binary |= getAddrModeSBit(MI, TID);
758 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
760 // Encode Rn which is PC.
761 Binary |= getARMRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
763 // Encode the displacement.
764 Binary |= 1 << ARMII::I_BitShift;
765 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
770 void ARMCodeEmitter::emitPseudoMoveInstruction(const MachineInstr &MI) {
771 unsigned Opcode = MI.getDesc().Opcode;
773 // Part of binary is determined by TableGn.
774 unsigned Binary = getBinaryCodeForInstr(MI);
776 // Set the conditional execution predicate
777 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
779 // Encode S bit if MI modifies CPSR.
780 if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag)
781 Binary |= 1 << ARMII::S_BitShift;
783 // Encode register def if there is one.
784 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
786 // Encode the shift operation.
793 case ARM::MOVsrl_flag:
795 Binary |= (0x2 << 4) | (1 << 7);
797 case ARM::MOVsra_flag:
799 Binary |= (0x4 << 4) | (1 << 7);
803 // Encode register Rm.
804 Binary |= getMachineOpValue(MI, 1);
809 void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
810 DEBUG(errs() << " ** LPC" << LabelID << " @ "
811 << (void*)MCE.getCurrentPCValue() << '\n');
812 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
815 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
816 unsigned Opcode = MI.getDesc().Opcode;
819 llvm_unreachable("ARMCodeEmitter::emitPseudoInstruction");
821 case ARM::BMOVPCRX_CALL:
823 case ARM::BMOVPCRXr9_CALL: {
824 // First emit mov lr, pc
825 unsigned Binary = 0x01a0e00f;
826 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
829 // and then emit the branch.
830 emitMiscBranchInstruction(MI);
833 case TargetOpcode::INLINEASM: {
834 // We allow inline assembler nodes with empty bodies - they can
835 // implicitly define registers, which is ok for JIT.
836 if (MI.getOperand(0).getSymbolName()[0]) {
837 report_fatal_error("JIT does not support inline asm!");
841 case TargetOpcode::PROLOG_LABEL:
842 case TargetOpcode::EH_LABEL:
843 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
845 case TargetOpcode::IMPLICIT_DEF:
846 case TargetOpcode::KILL:
849 case ARM::CONSTPOOL_ENTRY:
850 emitConstPoolInstruction(MI);
853 // Remember of the address of the PC label for relocation later.
854 addPCLabel(MI.getOperand(2).getImm());
855 // PICADD is just an add instruction that implicitly read pc.
856 emitDataProcessingInstruction(MI, 0, ARM::PC);
863 // Remember of the address of the PC label for relocation later.
864 addPCLabel(MI.getOperand(2).getImm());
865 // These are just load / store instructions that implicitly read pc.
866 emitLoadStoreInstruction(MI, 0, ARM::PC);
873 // Remember of the address of the PC label for relocation later.
874 addPCLabel(MI.getOperand(2).getImm());
875 // These are just load / store instructions that implicitly read pc.
876 emitMiscLoadStoreInstruction(MI, ARM::PC);
881 // Two instructions to materialize a constant.
882 if (Subtarget->hasV6T2Ops())
883 emitMOVi32immInstruction(MI);
885 emitMOVi2piecesInstruction(MI);
888 case ARM::LEApcrelJT:
889 // Materialize jumptable address.
890 emitLEApcrelJTInstruction(MI);
893 case ARM::MOVsrl_flag:
894 case ARM::MOVsra_flag:
895 emitPseudoMoveInstruction(MI);
900 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
901 const TargetInstrDesc &TID,
902 const MachineOperand &MO,
904 unsigned Binary = getMachineOpValue(MI, MO);
906 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
907 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
908 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
910 // Encode the shift opcode.
912 unsigned Rs = MO1.getReg();
914 // Set shift operand (bit[7:4]).
919 // RRX - 0110 and bit[11:8] clear.
921 default: llvm_unreachable("Unknown shift opc!");
922 case ARM_AM::lsl: SBits = 0x1; break;
923 case ARM_AM::lsr: SBits = 0x3; break;
924 case ARM_AM::asr: SBits = 0x5; break;
925 case ARM_AM::ror: SBits = 0x7; break;
926 case ARM_AM::rrx: SBits = 0x6; break;
929 // Set shift operand (bit[6:4]).
935 default: llvm_unreachable("Unknown shift opc!");
936 case ARM_AM::lsl: SBits = 0x0; break;
937 case ARM_AM::lsr: SBits = 0x2; break;
938 case ARM_AM::asr: SBits = 0x4; break;
939 case ARM_AM::ror: SBits = 0x6; break;
942 Binary |= SBits << 4;
943 if (SOpc == ARM_AM::rrx)
946 // Encode the shift operation Rs or shift_imm (except rrx).
948 // Encode Rs bit[11:8].
949 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
950 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
953 // Encode shift_imm bit[11:7].
954 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
957 unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
958 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
959 assert(SoImmVal != -1 && "Not a valid so_imm value!");
961 // Encode rotate_imm.
962 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
963 << ARMII::SoRotImmShift;
966 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
970 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
971 const TargetInstrDesc &TID) const {
972 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
973 const MachineOperand &MO = MI.getOperand(i-1);
974 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
975 return 1 << ARMII::S_BitShift;
980 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
982 unsigned ImplicitRn) {
983 const TargetInstrDesc &TID = MI.getDesc();
985 // Part of binary is determined by TableGn.
986 unsigned Binary = getBinaryCodeForInstr(MI);
988 // Set the conditional execution predicate
989 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
991 // Encode S bit if MI modifies CPSR.
992 Binary |= getAddrModeSBit(MI, TID);
994 // Encode register def if there is one.
995 unsigned NumDefs = TID.getNumDefs();
998 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1000 // Special handling for implicit use (e.g. PC).
1001 Binary |= (getARMRegisterNumbering(ImplicitRd) << ARMII::RegRdShift);
1003 if (TID.Opcode == ARM::MOVi16) {
1004 // Get immediate from MI.
1005 unsigned Lo16 = getMovi32Value(MI, MI.getOperand(OpIdx),
1006 ARM::reloc_arm_movw);
1007 // Encode imm which is the same as in emitMOVi32immInstruction().
1008 Binary |= Lo16 & 0xFFF;
1009 Binary |= ((Lo16 >> 12) & 0xF) << 16;
1012 } else if(TID.Opcode == ARM::MOVTi16) {
1013 unsigned Hi16 = (getMovi32Value(MI, MI.getOperand(OpIdx),
1014 ARM::reloc_arm_movt) >> 16);
1015 Binary |= Hi16 & 0xFFF;
1016 Binary |= ((Hi16 >> 12) & 0xF) << 16;
1019 } else if ((TID.Opcode == ARM::BFC) || (TID.Opcode == ARM::BFI)) {
1020 uint32_t v = ~MI.getOperand(2).getImm();
1021 int32_t lsb = CountTrailingZeros_32(v);
1022 int32_t msb = (32 - CountLeadingZeros_32(v)) - 1;
1023 // Instr{20-16} = msb, Instr{11-7} = lsb
1024 Binary |= (msb & 0x1F) << 16;
1025 Binary |= (lsb & 0x1F) << 7;
1028 } else if ((TID.Opcode == ARM::UBFX) || (TID.Opcode == ARM::SBFX)) {
1029 // Encode Rn in Instr{0-3}
1030 Binary |= getMachineOpValue(MI, OpIdx++);
1032 uint32_t lsb = MI.getOperand(OpIdx++).getImm();
1033 uint32_t widthm1 = MI.getOperand(OpIdx++).getImm() - 1;
1035 // Instr{20-16} = widthm1, Instr{11-7} = lsb
1036 Binary |= (widthm1 & 0x1F) << 16;
1037 Binary |= (lsb & 0x1F) << 7;
1042 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
1043 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1046 // Encode first non-shifter register operand if there is one.
1047 bool isUnary = TID.TSFlags & ARMII::UnaryDP;
1050 // Special handling for implicit use (e.g. PC).
1051 Binary |= (getARMRegisterNumbering(ImplicitRn) << ARMII::RegRnShift);
1053 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
1058 // Encode shifter operand.
1059 const MachineOperand &MO = MI.getOperand(OpIdx);
1060 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
1062 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
1067 // Encode register Rm.
1068 emitWordLE(Binary | getARMRegisterNumbering(MO.getReg()));
1073 Binary |= getMachineSoImmOpValue((unsigned)MO.getImm());
1078 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
1079 unsigned ImplicitRd,
1080 unsigned ImplicitRn) {
1081 const TargetInstrDesc &TID = MI.getDesc();
1082 unsigned Form = TID.TSFlags & ARMII::FormMask;
1083 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
1085 // Part of binary is determined by TableGn.
1086 unsigned Binary = getBinaryCodeForInstr(MI);
1088 // If this is an LDRi12, STRi12 or LDRcp, nothing more needs be done.
1089 if (MI.getOpcode() == ARM::LDRi12 || MI.getOpcode() == ARM::LDRcp ||
1090 MI.getOpcode() == ARM::STRi12) {
1095 // Set the conditional execution predicate
1096 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1100 // Operand 0 of a pre- and post-indexed store is the address base
1101 // writeback. Skip it.
1102 bool Skipped = false;
1103 if (IsPrePost && Form == ARMII::StFrm) {
1108 // Set first operand
1110 // Special handling for implicit use (e.g. PC).
1111 Binary |= (getARMRegisterNumbering(ImplicitRd) << ARMII::RegRdShift);
1113 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1115 // Set second operand
1117 // Special handling for implicit use (e.g. PC).
1118 Binary |= (getARMRegisterNumbering(ImplicitRn) << ARMII::RegRnShift);
1120 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1122 // If this is a two-address operand, skip it. e.g. LDR_PRE.
1123 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1126 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1127 unsigned AM2Opc = (ImplicitRn == ARM::PC)
1128 ? 0 : MI.getOperand(OpIdx+1).getImm();
1130 // Set bit U(23) according to sign of immed value (positive or negative).
1131 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
1133 if (!MO2.getReg()) { // is immediate
1134 if (ARM_AM::getAM2Offset(AM2Opc))
1135 // Set the value of offset_12 field
1136 Binary |= ARM_AM::getAM2Offset(AM2Opc);
1141 // Set bit I(25), because this is not in immediate encoding.
1142 Binary |= 1 << ARMII::I_BitShift;
1143 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
1144 // Set bit[3:0] to the corresponding Rm register
1145 Binary |= getARMRegisterNumbering(MO2.getReg());
1147 // If this instr is in scaled register offset/index instruction, set
1148 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
1149 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
1150 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift
1151 Binary |= ShImm << ARMII::ShiftShift; // shift_immed
1157 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
1158 unsigned ImplicitRn) {
1159 const TargetInstrDesc &TID = MI.getDesc();
1160 unsigned Form = TID.TSFlags & ARMII::FormMask;
1161 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
1163 // Part of binary is determined by TableGn.
1164 unsigned Binary = getBinaryCodeForInstr(MI);
1166 // Set the conditional execution predicate
1167 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1171 // Operand 0 of a pre- and post-indexed store is the address base
1172 // writeback. Skip it.
1173 bool Skipped = false;
1174 if (IsPrePost && Form == ARMII::StMiscFrm) {
1179 // Set first operand
1180 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1182 // Skip LDRD and STRD's second operand.
1183 if (TID.Opcode == ARM::LDRD || TID.Opcode == ARM::STRD)
1186 // Set second operand
1188 // Special handling for implicit use (e.g. PC).
1189 Binary |= (getARMRegisterNumbering(ImplicitRn) << ARMII::RegRnShift);
1191 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1193 // If this is a two-address operand, skip it. e.g. LDRH_POST.
1194 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1197 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1198 unsigned AM3Opc = (ImplicitRn == ARM::PC)
1199 ? 0 : MI.getOperand(OpIdx+1).getImm();
1201 // Set bit U(23) according to sign of immed value (positive or negative)
1202 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
1205 // If this instr is in register offset/index encoding, set bit[3:0]
1206 // to the corresponding Rm register.
1208 Binary |= getARMRegisterNumbering(MO2.getReg());
1213 // This instr is in immediate offset/index encoding, set bit 22 to 1.
1214 Binary |= 1 << ARMII::AM3_I_BitShift;
1215 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
1217 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH
1218 Binary |= (ImmOffs & 0xF); // immedL
1224 static unsigned getAddrModeUPBits(unsigned Mode) {
1225 unsigned Binary = 0;
1227 // Set addressing mode by modifying bits U(23) and P(24)
1228 // IA - Increment after - bit U = 1 and bit P = 0
1229 // IB - Increment before - bit U = 1 and bit P = 1
1230 // DA - Decrement after - bit U = 0 and bit P = 0
1231 // DB - Decrement before - bit U = 0 and bit P = 1
1233 default: llvm_unreachable("Unknown addressing sub-mode!");
1234 case ARM_AM::da: break;
1235 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
1236 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
1237 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
1243 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
1244 const TargetInstrDesc &TID = MI.getDesc();
1245 bool IsUpdating = (TID.TSFlags & ARMII::IndexModeMask) != 0;
1247 // Part of binary is determined by TableGn.
1248 unsigned Binary = getBinaryCodeForInstr(MI);
1250 // Set the conditional execution predicate
1251 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1253 // Skip operand 0 of an instruction with base register update.
1258 // Set base address operand
1259 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1261 // Set addressing mode by modifying bits U(23) and P(24)
1262 ARM_AM::AMSubMode Mode = ARM_AM::getLoadStoreMultipleSubMode(MI.getOpcode());
1263 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(Mode));
1267 Binary |= 0x1 << ARMII::W_BitShift;
1270 for (unsigned i = OpIdx+2, e = MI.getNumOperands(); i != e; ++i) {
1271 const MachineOperand &MO = MI.getOperand(i);
1272 if (!MO.isReg() || MO.isImplicit())
1274 unsigned RegNum = getARMRegisterNumbering(MO.getReg());
1275 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
1277 Binary |= 0x1 << RegNum;
1283 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
1284 const TargetInstrDesc &TID = MI.getDesc();
1286 // Part of binary is determined by TableGn.
1287 unsigned Binary = getBinaryCodeForInstr(MI);
1289 // Set the conditional execution predicate
1290 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1292 // Encode S bit if MI modifies CPSR.
1293 Binary |= getAddrModeSBit(MI, TID);
1295 // 32x32->64bit operations have two destination registers. The number
1296 // of register definitions will tell us if that's what we're dealing with.
1298 if (TID.getNumDefs() == 2)
1299 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
1302 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
1305 Binary |= getMachineOpValue(MI, OpIdx++);
1308 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
1310 // Many multiple instructions (e.g. MLA) have three src operands. Encode
1311 // it as Rn (for multiply, that's in the same offset as RdLo.
1312 if (TID.getNumOperands() > OpIdx &&
1313 !TID.OpInfo[OpIdx].isPredicate() &&
1314 !TID.OpInfo[OpIdx].isOptionalDef())
1315 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
1320 void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
1321 const TargetInstrDesc &TID = MI.getDesc();
1323 // Part of binary is determined by TableGn.
1324 unsigned Binary = getBinaryCodeForInstr(MI);
1326 // Set the conditional execution predicate
1327 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1332 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1334 const MachineOperand &MO1 = MI.getOperand(OpIdx++);
1335 const MachineOperand &MO2 = MI.getOperand(OpIdx);
1337 // Two register operand form.
1339 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
1342 Binary |= getMachineOpValue(MI, MO2);
1345 Binary |= getMachineOpValue(MI, MO1);
1348 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
1349 if (MI.getOperand(OpIdx).isImm() &&
1350 !TID.OpInfo[OpIdx].isPredicate() &&
1351 !TID.OpInfo[OpIdx].isOptionalDef())
1352 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
1357 void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
1358 const TargetInstrDesc &TID = MI.getDesc();
1360 // Part of binary is determined by TableGn.
1361 unsigned Binary = getBinaryCodeForInstr(MI);
1363 // Set the conditional execution predicate
1364 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1369 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1371 const MachineOperand &MO = MI.getOperand(OpIdx++);
1372 if (OpIdx == TID.getNumOperands() ||
1373 TID.OpInfo[OpIdx].isPredicate() ||
1374 TID.OpInfo[OpIdx].isOptionalDef()) {
1375 // Encode Rm and it's done.
1376 Binary |= getMachineOpValue(MI, MO);
1382 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1385 Binary |= getMachineOpValue(MI, OpIdx++);
1387 // Encode shift_imm.
1388 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
1389 if (TID.Opcode == ARM::PKHTB) {
1390 assert(ShiftAmt != 0 && "PKHTB shift_imm is 0!");
1394 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1395 Binary |= ShiftAmt << ARMII::ShiftShift;
1400 void ARMCodeEmitter::emitSaturateInstruction(const MachineInstr &MI) {
1401 const TargetInstrDesc &TID = MI.getDesc();
1403 // Part of binary is determined by TableGen.
1404 unsigned Binary = getBinaryCodeForInstr(MI);
1406 // Set the conditional execution predicate
1407 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1410 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
1412 // Encode saturate bit position.
1413 unsigned Pos = MI.getOperand(1).getImm();
1414 if (TID.Opcode == ARM::SSAT || TID.Opcode == ARM::SSAT16)
1416 assert((Pos < 16 || (Pos < 32 &&
1417 TID.Opcode != ARM::SSAT16 &&
1418 TID.Opcode != ARM::USAT16)) &&
1419 "saturate bit position out of range");
1420 Binary |= Pos << 16;
1423 Binary |= getMachineOpValue(MI, 2);
1425 // Encode shift_imm.
1426 if (TID.getNumOperands() == 4) {
1427 unsigned ShiftOp = MI.getOperand(3).getImm();
1428 ARM_AM::ShiftOpc Opc = ARM_AM::getSORegShOp(ShiftOp);
1429 if (Opc == ARM_AM::asr)
1431 unsigned ShiftAmt = MI.getOperand(3).getImm();
1432 if (ShiftAmt == 32 && Opc == ARM_AM::asr)
1434 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1435 Binary |= ShiftAmt << ARMII::ShiftShift;
1441 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
1442 const TargetInstrDesc &TID = MI.getDesc();
1444 if (TID.Opcode == ARM::TPsoft) {
1445 llvm_unreachable("ARM::TPsoft FIXME"); // FIXME
1448 // Part of binary is determined by TableGn.
1449 unsigned Binary = getBinaryCodeForInstr(MI);
1451 // Set the conditional execution predicate
1452 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1454 // Set signed_immed_24 field
1455 Binary |= getMachineOpValue(MI, 0);
1460 void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
1461 // Remember the base address of the inline jump table.
1462 uintptr_t JTBase = MCE.getCurrentPCValue();
1463 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
1464 DEBUG(errs() << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase
1467 // Now emit the jump table entries.
1468 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1469 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1471 // DestBB address - JT base.
1472 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
1474 // Absolute DestBB address.
1475 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1480 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
1481 const TargetInstrDesc &TID = MI.getDesc();
1483 // Handle jump tables.
1484 if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
1485 // First emit a ldr pc, [] instruction.
1486 emitDataProcessingInstruction(MI, ARM::PC);
1488 // Then emit the inline jump table.
1490 (TID.Opcode == ARM::BR_JTr)
1491 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1492 emitInlineJumpTable(JTIndex);
1494 } else if (TID.Opcode == ARM::BR_JTm) {
1495 // First emit a ldr pc, [] instruction.
1496 emitLoadStoreInstruction(MI, ARM::PC);
1498 // Then emit the inline jump table.
1499 emitInlineJumpTable(MI.getOperand(3).getIndex());
1503 // Part of binary is determined by TableGn.
1504 unsigned Binary = getBinaryCodeForInstr(MI);
1506 // Set the conditional execution predicate
1507 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1509 if (TID.Opcode == ARM::BX_RET || TID.Opcode == ARM::MOVPCLR)
1510 // The return register is LR.
1511 Binary |= getARMRegisterNumbering(ARM::LR);
1513 // otherwise, set the return register
1514 Binary |= getMachineOpValue(MI, 0);
1519 static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) {
1520 unsigned RegD = MI.getOperand(OpIdx).getReg();
1521 unsigned Binary = 0;
1522 bool isSPVFP = ARM::SPRRegisterClass->contains(RegD);
1523 RegD = getARMRegisterNumbering(RegD);
1525 Binary |= RegD << ARMII::RegRdShift;
1527 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1528 Binary |= (RegD & 0x01) << ARMII::D_BitShift;
1533 static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) {
1534 unsigned RegN = MI.getOperand(OpIdx).getReg();
1535 unsigned Binary = 0;
1536 bool isSPVFP = ARM::SPRRegisterClass->contains(RegN);
1537 RegN = getARMRegisterNumbering(RegN);
1539 Binary |= RegN << ARMII::RegRnShift;
1541 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1542 Binary |= (RegN & 0x01) << ARMII::N_BitShift;
1547 static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) {
1548 unsigned RegM = MI.getOperand(OpIdx).getReg();
1549 unsigned Binary = 0;
1550 bool isSPVFP = ARM::SPRRegisterClass->contains(RegM);
1551 RegM = getARMRegisterNumbering(RegM);
1555 Binary |= ((RegM & 0x1E) >> 1);
1556 Binary |= (RegM & 0x01) << ARMII::M_BitShift;
1561 void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) {
1562 const TargetInstrDesc &TID = MI.getDesc();
1564 // Part of binary is determined by TableGn.
1565 unsigned Binary = getBinaryCodeForInstr(MI);
1567 // Set the conditional execution predicate
1568 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1571 assert((Binary & ARMII::D_BitShift) == 0 &&
1572 (Binary & ARMII::N_BitShift) == 0 &&
1573 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1576 Binary |= encodeVFPRd(MI, OpIdx++);
1578 // If this is a two-address operand, skip it, e.g. FMACD.
1579 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1583 if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
1584 Binary |= encodeVFPRn(MI, OpIdx++);
1586 if (OpIdx == TID.getNumOperands() ||
1587 TID.OpInfo[OpIdx].isPredicate() ||
1588 TID.OpInfo[OpIdx].isOptionalDef()) {
1589 // FCMPEZD etc. has only one operand.
1595 Binary |= encodeVFPRm(MI, OpIdx);
1600 void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) {
1601 const TargetInstrDesc &TID = MI.getDesc();
1602 unsigned Form = TID.TSFlags & ARMII::FormMask;
1604 // Part of binary is determined by TableGn.
1605 unsigned Binary = getBinaryCodeForInstr(MI);
1607 // Set the conditional execution predicate
1608 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1612 case ARMII::VFPConv1Frm:
1613 case ARMII::VFPConv2Frm:
1614 case ARMII::VFPConv3Frm:
1616 Binary |= encodeVFPRd(MI, 0);
1618 case ARMII::VFPConv4Frm:
1620 Binary |= encodeVFPRn(MI, 0);
1622 case ARMII::VFPConv5Frm:
1624 Binary |= encodeVFPRm(MI, 0);
1630 case ARMII::VFPConv1Frm:
1632 Binary |= encodeVFPRm(MI, 1);
1634 case ARMII::VFPConv2Frm:
1635 case ARMII::VFPConv3Frm:
1637 Binary |= encodeVFPRn(MI, 1);
1639 case ARMII::VFPConv4Frm:
1640 case ARMII::VFPConv5Frm:
1642 Binary |= encodeVFPRd(MI, 1);
1646 if (Form == ARMII::VFPConv5Frm)
1648 Binary |= encodeVFPRn(MI, 2);
1649 else if (Form == ARMII::VFPConv3Frm)
1651 Binary |= encodeVFPRm(MI, 2);
1656 void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
1657 // Part of binary is determined by TableGn.
1658 unsigned Binary = getBinaryCodeForInstr(MI);
1660 // Set the conditional execution predicate
1661 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1666 Binary |= encodeVFPRd(MI, OpIdx++);
1668 // Encode address base.
1669 const MachineOperand &Base = MI.getOperand(OpIdx++);
1670 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1672 // If there is a non-zero immediate offset, encode it.
1674 const MachineOperand &Offset = MI.getOperand(OpIdx);
1675 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1676 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1677 Binary |= 1 << ARMII::U_BitShift;
1684 // If immediate offset is omitted, default to +0.
1685 Binary |= 1 << ARMII::U_BitShift;
1691 ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
1692 const TargetInstrDesc &TID = MI.getDesc();
1693 bool IsUpdating = (TID.TSFlags & ARMII::IndexModeMask) != 0;
1695 // Part of binary is determined by TableGn.
1696 unsigned Binary = getBinaryCodeForInstr(MI);
1698 // Set the conditional execution predicate
1699 Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1701 // Skip operand 0 of an instruction with base register update.
1706 // Set base address operand
1707 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1709 // Set addressing mode by modifying bits U(23) and P(24)
1710 ARM_AM::AMSubMode Mode = ARM_AM::getLoadStoreMultipleSubMode(MI.getOpcode());
1711 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(Mode));
1715 Binary |= 0x1 << ARMII::W_BitShift;
1717 // First register is encoded in Dd.
1718 Binary |= encodeVFPRd(MI, OpIdx+2);
1720 // Count the number of registers.
1721 unsigned NumRegs = 1;
1722 for (unsigned i = OpIdx+3, e = MI.getNumOperands(); i != e; ++i) {
1723 const MachineOperand &MO = MI.getOperand(i);
1724 if (!MO.isReg() || MO.isImplicit())
1728 // Bit 8 will be set if <list> is consecutive 64-bit registers (e.g., D0)
1729 // Otherwise, it will be 0, in the case of 32-bit registers.
1731 Binary |= NumRegs * 2;
1738 static unsigned encodeNEONRd(const MachineInstr &MI, unsigned OpIdx) {
1739 unsigned RegD = MI.getOperand(OpIdx).getReg();
1740 unsigned Binary = 0;
1741 RegD = getARMRegisterNumbering(RegD);
1742 Binary |= (RegD & 0xf) << ARMII::RegRdShift;
1743 Binary |= ((RegD >> 4) & 1) << ARMII::D_BitShift;
1747 static unsigned encodeNEONRn(const MachineInstr &MI, unsigned OpIdx) {
1748 unsigned RegN = MI.getOperand(OpIdx).getReg();
1749 unsigned Binary = 0;
1750 RegN = getARMRegisterNumbering(RegN);
1751 Binary |= (RegN & 0xf) << ARMII::RegRnShift;
1752 Binary |= ((RegN >> 4) & 1) << ARMII::N_BitShift;
1756 static unsigned encodeNEONRm(const MachineInstr &MI, unsigned OpIdx) {
1757 unsigned RegM = MI.getOperand(OpIdx).getReg();
1758 unsigned Binary = 0;
1759 RegM = getARMRegisterNumbering(RegM);
1760 Binary |= (RegM & 0xf);
1761 Binary |= ((RegM >> 4) & 1) << ARMII::M_BitShift;
1765 /// convertNEONDataProcToThumb - Convert the ARM mode encoding for a NEON
1766 /// data-processing instruction to the corresponding Thumb encoding.
1767 static unsigned convertNEONDataProcToThumb(unsigned Binary) {
1768 assert((Binary & 0xfe000000) == 0xf2000000 &&
1769 "not an ARM NEON data-processing instruction");
1770 unsigned UBit = (Binary >> 24) & 1;
1771 return 0xef000000 | (UBit << 28) | (Binary & 0xffffff);
1774 void ARMCodeEmitter::emitNEONLaneInstruction(const MachineInstr &MI) {
1775 unsigned Binary = getBinaryCodeForInstr(MI);
1777 unsigned RegTOpIdx, RegNOpIdx, LnOpIdx;
1778 const TargetInstrDesc &TID = MI.getDesc();
1779 if ((TID.TSFlags & ARMII::FormMask) == ARMII::NGetLnFrm) {
1783 } else { // ARMII::NSetLnFrm
1789 // Set the conditional execution predicate
1790 Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift;
1792 unsigned RegT = MI.getOperand(RegTOpIdx).getReg();
1793 RegT = getARMRegisterNumbering(RegT);
1794 Binary |= (RegT << ARMII::RegRdShift);
1795 Binary |= encodeNEONRn(MI, RegNOpIdx);
1798 if ((Binary & (1 << 22)) != 0)
1799 LaneShift = 0; // 8-bit elements
1800 else if ((Binary & (1 << 5)) != 0)
1801 LaneShift = 1; // 16-bit elements
1803 LaneShift = 2; // 32-bit elements
1805 unsigned Lane = MI.getOperand(LnOpIdx).getImm() << LaneShift;
1806 unsigned Opc1 = Lane >> 2;
1807 unsigned Opc2 = Lane & 3;
1808 assert((Opc1 & 3) == 0 && "out-of-range lane number operand");
1809 Binary |= (Opc1 << 21);
1810 Binary |= (Opc2 << 5);
1815 void ARMCodeEmitter::emitNEONDupInstruction(const MachineInstr &MI) {
1816 unsigned Binary = getBinaryCodeForInstr(MI);
1818 // Set the conditional execution predicate
1819 Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift;
1821 unsigned RegT = MI.getOperand(1).getReg();
1822 RegT = getARMRegisterNumbering(RegT);
1823 Binary |= (RegT << ARMII::RegRdShift);
1824 Binary |= encodeNEONRn(MI, 0);
1828 void ARMCodeEmitter::emitNEON1RegModImmInstruction(const MachineInstr &MI) {
1829 unsigned Binary = getBinaryCodeForInstr(MI);
1830 // Destination register is encoded in Dd.
1831 Binary |= encodeNEONRd(MI, 0);
1832 // Immediate fields: Op, Cmode, I, Imm3, Imm4
1833 unsigned Imm = MI.getOperand(1).getImm();
1834 unsigned Op = (Imm >> 12) & 1;
1835 unsigned Cmode = (Imm >> 8) & 0xf;
1836 unsigned I = (Imm >> 7) & 1;
1837 unsigned Imm3 = (Imm >> 4) & 0x7;
1838 unsigned Imm4 = Imm & 0xf;
1839 Binary |= (I << 24) | (Imm3 << 16) | (Cmode << 8) | (Op << 5) | Imm4;
1841 Binary = convertNEONDataProcToThumb(Binary);
1845 void ARMCodeEmitter::emitNEON2RegInstruction(const MachineInstr &MI) {
1846 const TargetInstrDesc &TID = MI.getDesc();
1847 unsigned Binary = getBinaryCodeForInstr(MI);
1848 // Destination register is encoded in Dd; source register in Dm.
1850 Binary |= encodeNEONRd(MI, OpIdx++);
1851 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1853 Binary |= encodeNEONRm(MI, OpIdx);
1855 Binary = convertNEONDataProcToThumb(Binary);
1856 // FIXME: This does not handle VDUPfdf or VDUPfqf.
1860 void ARMCodeEmitter::emitNEON3RegInstruction(const MachineInstr &MI) {
1861 const TargetInstrDesc &TID = MI.getDesc();
1862 unsigned Binary = getBinaryCodeForInstr(MI);
1863 // Destination register is encoded in Dd; source registers in Dn and Dm.
1865 Binary |= encodeNEONRd(MI, OpIdx++);
1866 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1868 Binary |= encodeNEONRn(MI, OpIdx++);
1869 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1871 Binary |= encodeNEONRm(MI, OpIdx);
1873 Binary = convertNEONDataProcToThumb(Binary);
1874 // FIXME: This does not handle VMOVDneon or VMOVQ.
1878 #include "ARMGenCodeEmitter.inc"