1 //===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
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 a printer that converts from our internal representation
11 // of machine-dependent LLVM code to GAS-format ARM assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
16 #include "ARMAsmPrinter.h"
18 #include "ARMBuildAttrs.h"
19 #include "ARMConstantPoolValue.h"
20 #include "ARMMachineFunctionInfo.h"
21 #include "ARMTargetMachine.h"
22 #include "ARMTargetObjectFile.h"
23 #include "InstPrinter/ARMInstPrinter.h"
24 #include "MCTargetDesc/ARMAddressingModes.h"
25 #include "MCTargetDesc/ARMMCExpr.h"
26 #include "llvm/ADT/SetVector.h"
27 #include "llvm/ADT/SmallString.h"
28 #include "llvm/Assembly/Writer.h"
29 #include "llvm/CodeGen/MachineFunctionPass.h"
30 #include "llvm/CodeGen/MachineJumpTableInfo.h"
31 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
32 #include "llvm/DebugInfo.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/DataLayout.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/MC/MCAsmInfo.h"
38 #include "llvm/MC/MCAssembler.h"
39 #include "llvm/MC/MCContext.h"
40 #include "llvm/MC/MCELFStreamer.h"
41 #include "llvm/MC/MCInst.h"
42 #include "llvm/MC/MCInstBuilder.h"
43 #include "llvm/MC/MCObjectStreamer.h"
44 #include "llvm/MC/MCSectionMachO.h"
45 #include "llvm/MC/MCStreamer.h"
46 #include "llvm/MC/MCSymbol.h"
47 #include "llvm/Support/CommandLine.h"
48 #include "llvm/Support/Debug.h"
49 #include "llvm/Support/ELF.h"
50 #include "llvm/Support/ErrorHandling.h"
51 #include "llvm/Support/TargetRegistry.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include "llvm/Target/Mangler.h"
54 #include "llvm/Target/TargetMachine.h"
60 // Per section and per symbol attributes are not supported.
61 // To implement them we would need the ability to delay this emission
62 // until the assembly file is fully parsed/generated as only then do we
63 // know the symbol and section numbers.
64 class AttributeEmitter {
66 virtual void MaybeSwitchVendor(StringRef Vendor) = 0;
67 virtual void EmitAttribute(unsigned Attribute, unsigned Value) = 0;
68 virtual void EmitTextAttribute(unsigned Attribute, StringRef String) = 0;
69 virtual void Finish() = 0;
70 virtual ~AttributeEmitter() {}
73 class AsmAttributeEmitter : public AttributeEmitter {
77 AsmAttributeEmitter(MCStreamer &Streamer_) : Streamer(Streamer_) {}
78 void MaybeSwitchVendor(StringRef Vendor) { }
80 void EmitAttribute(unsigned Attribute, unsigned Value) {
81 Streamer.EmitRawText("\t.eabi_attribute " +
82 Twine(Attribute) + ", " + Twine(Value));
85 void EmitTextAttribute(unsigned Attribute, StringRef String) {
87 default: llvm_unreachable("Unsupported Text attribute in ASM Mode");
88 case ARMBuildAttrs::CPU_name:
89 Streamer.EmitRawText(StringRef("\t.cpu ") + String.lower());
91 /* GAS requires .fpu to be emitted regardless of EABI attribute */
92 case ARMBuildAttrs::Advanced_SIMD_arch:
93 case ARMBuildAttrs::VFP_arch:
94 Streamer.EmitRawText(StringRef("\t.fpu ") + String.lower());
101 class ObjectAttributeEmitter : public AttributeEmitter {
102 // This structure holds all attributes, accounting for
103 // their string/numeric value, so we can later emmit them
104 // in declaration order, keeping all in the same vector
105 struct AttributeItemType {
113 StringRef StringValue;
116 MCObjectStreamer &Streamer;
117 StringRef CurrentVendor;
118 SmallVector<AttributeItemType, 64> Contents;
120 // Account for the ULEB/String size of each item,
121 // not just the number of items
123 // FIXME: this should be in a more generic place, but
124 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
125 size_t getULEBSize(int Value) {
129 Size += sizeof(int8_t); // Is this really necessary?
135 ObjectAttributeEmitter(MCObjectStreamer &Streamer_) :
136 Streamer(Streamer_), CurrentVendor(""), ContentsSize(0) { }
138 void MaybeSwitchVendor(StringRef Vendor) {
139 assert(!Vendor.empty() && "Vendor cannot be empty.");
141 if (CurrentVendor.empty())
142 CurrentVendor = Vendor;
143 else if (CurrentVendor == Vendor)
148 CurrentVendor = Vendor;
150 assert(Contents.size() == 0);
153 void EmitAttribute(unsigned Attribute, unsigned Value) {
154 AttributeItemType attr = {
155 AttributeItemType::NumericAttribute,
160 ContentsSize += getULEBSize(Attribute);
161 ContentsSize += getULEBSize(Value);
162 Contents.push_back(attr);
165 void EmitTextAttribute(unsigned Attribute, StringRef String) {
166 AttributeItemType attr = {
167 AttributeItemType::TextAttribute,
172 ContentsSize += getULEBSize(Attribute);
174 ContentsSize += String.size()+1;
176 Contents.push_back(attr);
180 // Vendor size + Vendor name + '\0'
181 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
184 const size_t TagHeaderSize = 1 + 4;
186 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
187 Streamer.EmitBytes(CurrentVendor);
188 Streamer.EmitIntValue(0, 1); // '\0'
190 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
191 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
193 // Size should have been accounted for already, now
194 // emit each field as its type (ULEB or String)
195 for (unsigned int i=0; i<Contents.size(); ++i) {
196 AttributeItemType item = Contents[i];
197 Streamer.EmitULEB128IntValue(item.Tag);
199 default: llvm_unreachable("Invalid attribute type");
200 case AttributeItemType::NumericAttribute:
201 Streamer.EmitULEB128IntValue(item.IntValue);
203 case AttributeItemType::TextAttribute:
204 Streamer.EmitBytes(item.StringValue.upper());
205 Streamer.EmitIntValue(0, 1); // '\0'
214 } // end of anonymous namespace
216 /// EmitDwarfRegOp - Emit dwarf register operation.
217 void ARMAsmPrinter::EmitDwarfRegOp(const MachineLocation &MLoc,
218 bool Indirect) const {
219 const TargetRegisterInfo *RI = TM.getRegisterInfo();
220 if (RI->getDwarfRegNum(MLoc.getReg(), false) != -1) {
221 AsmPrinter::EmitDwarfRegOp(MLoc, Indirect);
224 assert(MLoc.isReg() && !Indirect &&
225 "This doesn't support offset/indirection - implement it if needed");
226 unsigned Reg = MLoc.getReg();
227 if (Reg >= ARM::S0 && Reg <= ARM::S31) {
228 assert(ARM::S0 + 31 == ARM::S31 && "Unexpected ARM S register numbering");
229 // S registers are described as bit-pieces of a register
230 // S[2x] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 0)
231 // S[2x+1] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 32)
233 unsigned SReg = Reg - ARM::S0;
234 bool odd = SReg & 0x1;
235 unsigned Rx = 256 + (SReg >> 1);
237 OutStreamer.AddComment("DW_OP_regx for S register");
238 EmitInt8(dwarf::DW_OP_regx);
240 OutStreamer.AddComment(Twine(SReg));
244 OutStreamer.AddComment("DW_OP_bit_piece 32 32");
245 EmitInt8(dwarf::DW_OP_bit_piece);
249 OutStreamer.AddComment("DW_OP_bit_piece 32 0");
250 EmitInt8(dwarf::DW_OP_bit_piece);
254 } else if (Reg >= ARM::Q0 && Reg <= ARM::Q15) {
255 assert(ARM::Q0 + 15 == ARM::Q15 && "Unexpected ARM Q register numbering");
256 // Q registers Q0-Q15 are described by composing two D registers together.
257 // Qx = DW_OP_regx(256+2x) DW_OP_piece(8) DW_OP_regx(256+2x+1)
260 unsigned QReg = Reg - ARM::Q0;
261 unsigned D1 = 256 + 2 * QReg;
262 unsigned D2 = D1 + 1;
264 OutStreamer.AddComment("DW_OP_regx for Q register: D1");
265 EmitInt8(dwarf::DW_OP_regx);
267 OutStreamer.AddComment("DW_OP_piece 8");
268 EmitInt8(dwarf::DW_OP_piece);
271 OutStreamer.AddComment("DW_OP_regx for Q register: D2");
272 EmitInt8(dwarf::DW_OP_regx);
274 OutStreamer.AddComment("DW_OP_piece 8");
275 EmitInt8(dwarf::DW_OP_piece);
280 void ARMAsmPrinter::EmitFunctionBodyEnd() {
281 // Make sure to terminate any constant pools that were at the end
285 InConstantPool = false;
286 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
289 void ARMAsmPrinter::EmitFunctionEntryLabel() {
290 if (AFI->isThumbFunction()) {
291 OutStreamer.EmitAssemblerFlag(MCAF_Code16);
292 OutStreamer.EmitThumbFunc(CurrentFnSym);
295 OutStreamer.EmitLabel(CurrentFnSym);
298 void ARMAsmPrinter::EmitXXStructor(const Constant *CV) {
299 uint64_t Size = TM.getDataLayout()->getTypeAllocSize(CV->getType());
300 assert(Size && "C++ constructor pointer had zero size!");
302 const GlobalValue *GV = dyn_cast<GlobalValue>(CV->stripPointerCasts());
303 assert(GV && "C++ constructor pointer was not a GlobalValue!");
305 const MCExpr *E = MCSymbolRefExpr::Create(Mang->getSymbol(GV),
306 (Subtarget->isTargetDarwin()
307 ? MCSymbolRefExpr::VK_None
308 : MCSymbolRefExpr::VK_ARM_TARGET1),
311 OutStreamer.EmitValue(E, Size);
314 /// runOnMachineFunction - This uses the EmitInstruction()
315 /// method to print assembly for each instruction.
317 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
318 AFI = MF.getInfo<ARMFunctionInfo>();
319 MCP = MF.getConstantPool();
321 return AsmPrinter::runOnMachineFunction(MF);
324 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
325 raw_ostream &O, const char *Modifier) {
326 const MachineOperand &MO = MI->getOperand(OpNum);
327 unsigned TF = MO.getTargetFlags();
329 switch (MO.getType()) {
330 default: llvm_unreachable("<unknown operand type>");
331 case MachineOperand::MO_Register: {
332 unsigned Reg = MO.getReg();
333 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
334 assert(!MO.getSubReg() && "Subregs should be eliminated!");
335 if(ARM::GPRPairRegClass.contains(Reg)) {
336 const MachineFunction &MF = *MI->getParent()->getParent();
337 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
338 Reg = TRI->getSubReg(Reg, ARM::gsub_0);
340 O << ARMInstPrinter::getRegisterName(Reg);
343 case MachineOperand::MO_Immediate: {
344 int64_t Imm = MO.getImm();
346 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
347 (TF == ARMII::MO_LO16))
349 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
350 (TF == ARMII::MO_HI16))
355 case MachineOperand::MO_MachineBasicBlock:
356 O << *MO.getMBB()->getSymbol();
358 case MachineOperand::MO_GlobalAddress: {
359 const GlobalValue *GV = MO.getGlobal();
360 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
361 (TF & ARMII::MO_LO16))
363 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
364 (TF & ARMII::MO_HI16))
366 O << *Mang->getSymbol(GV);
368 printOffset(MO.getOffset(), O);
369 if (TF == ARMII::MO_PLT)
373 case MachineOperand::MO_ExternalSymbol: {
374 O << *GetExternalSymbolSymbol(MO.getSymbolName());
375 if (TF == ARMII::MO_PLT)
379 case MachineOperand::MO_ConstantPoolIndex:
380 O << *GetCPISymbol(MO.getIndex());
382 case MachineOperand::MO_JumpTableIndex:
383 O << *GetJTISymbol(MO.getIndex());
388 //===--------------------------------------------------------------------===//
390 MCSymbol *ARMAsmPrinter::
391 GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
392 SmallString<60> Name;
393 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
394 << getFunctionNumber() << '_' << uid << '_' << uid2;
395 return OutContext.GetOrCreateSymbol(Name.str());
399 MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel() const {
400 SmallString<60> Name;
401 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
402 << getFunctionNumber();
403 return OutContext.GetOrCreateSymbol(Name.str());
406 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
407 unsigned AsmVariant, const char *ExtraCode,
409 // Does this asm operand have a single letter operand modifier?
410 if (ExtraCode && ExtraCode[0]) {
411 if (ExtraCode[1] != 0) return true; // Unknown modifier.
413 switch (ExtraCode[0]) {
415 // See if this is a generic print operand
416 return AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O);
417 case 'a': // Print as a memory address.
418 if (MI->getOperand(OpNum).isReg()) {
420 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
425 case 'c': // Don't print "#" before an immediate operand.
426 if (!MI->getOperand(OpNum).isImm())
428 O << MI->getOperand(OpNum).getImm();
430 case 'P': // Print a VFP double precision register.
431 case 'q': // Print a NEON quad precision register.
432 printOperand(MI, OpNum, O);
434 case 'y': // Print a VFP single precision register as indexed double.
435 if (MI->getOperand(OpNum).isReg()) {
436 unsigned Reg = MI->getOperand(OpNum).getReg();
437 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
438 // Find the 'd' register that has this 's' register as a sub-register,
439 // and determine the lane number.
440 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) {
441 if (!ARM::DPRRegClass.contains(*SR))
443 bool Lane0 = TRI->getSubReg(*SR, ARM::ssub_0) == Reg;
444 O << ARMInstPrinter::getRegisterName(*SR) << (Lane0 ? "[0]" : "[1]");
449 case 'B': // Bitwise inverse of integer or symbol without a preceding #.
450 if (!MI->getOperand(OpNum).isImm())
452 O << ~(MI->getOperand(OpNum).getImm());
454 case 'L': // The low 16 bits of an immediate constant.
455 if (!MI->getOperand(OpNum).isImm())
457 O << (MI->getOperand(OpNum).getImm() & 0xffff);
459 case 'M': { // A register range suitable for LDM/STM.
460 if (!MI->getOperand(OpNum).isReg())
462 const MachineOperand &MO = MI->getOperand(OpNum);
463 unsigned RegBegin = MO.getReg();
464 // This takes advantage of the 2 operand-ness of ldm/stm and that we've
465 // already got the operands in registers that are operands to the
466 // inline asm statement.
468 O << "{" << ARMInstPrinter::getRegisterName(RegBegin);
470 // FIXME: The register allocator not only may not have given us the
471 // registers in sequence, but may not be in ascending registers. This
472 // will require changes in the register allocator that'll need to be
473 // propagated down here if the operands change.
474 unsigned RegOps = OpNum + 1;
475 while (MI->getOperand(RegOps).isReg()) {
477 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
485 case 'R': // The most significant register of a pair.
486 case 'Q': { // The least significant register of a pair.
489 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
490 if (!FlagsOP.isImm())
492 unsigned Flags = FlagsOP.getImm();
493 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
496 unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1;
497 if (RegOp >= MI->getNumOperands())
499 const MachineOperand &MO = MI->getOperand(RegOp);
502 unsigned Reg = MO.getReg();
503 O << ARMInstPrinter::getRegisterName(Reg);
507 case 'e': // The low doubleword register of a NEON quad register.
508 case 'f': { // The high doubleword register of a NEON quad register.
509 if (!MI->getOperand(OpNum).isReg())
511 unsigned Reg = MI->getOperand(OpNum).getReg();
512 if (!ARM::QPRRegClass.contains(Reg))
514 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
515 unsigned SubReg = TRI->getSubReg(Reg, ExtraCode[0] == 'e' ?
516 ARM::dsub_0 : ARM::dsub_1);
517 O << ARMInstPrinter::getRegisterName(SubReg);
521 // This modifier is not yet supported.
522 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
524 case 'H': { // The highest-numbered register of a pair.
525 const MachineOperand &MO = MI->getOperand(OpNum);
528 const MachineFunction &MF = *MI->getParent()->getParent();
529 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
530 unsigned Reg = MO.getReg();
531 if(!ARM::GPRPairRegClass.contains(Reg))
533 Reg = TRI->getSubReg(Reg, ARM::gsub_1);
534 O << ARMInstPrinter::getRegisterName(Reg);
540 printOperand(MI, OpNum, O);
544 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
545 unsigned OpNum, unsigned AsmVariant,
546 const char *ExtraCode,
548 // Does this asm operand have a single letter operand modifier?
549 if (ExtraCode && ExtraCode[0]) {
550 if (ExtraCode[1] != 0) return true; // Unknown modifier.
552 switch (ExtraCode[0]) {
553 case 'A': // A memory operand for a VLD1/VST1 instruction.
554 default: return true; // Unknown modifier.
555 case 'm': // The base register of a memory operand.
556 if (!MI->getOperand(OpNum).isReg())
558 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
563 const MachineOperand &MO = MI->getOperand(OpNum);
564 assert(MO.isReg() && "unexpected inline asm memory operand");
565 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
569 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
570 if (Subtarget->isTargetDarwin()) {
571 Reloc::Model RelocM = TM.getRelocationModel();
572 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
573 // Declare all the text sections up front (before the DWARF sections
574 // emitted by AsmPrinter::doInitialization) so the assembler will keep
575 // them together at the beginning of the object file. This helps
576 // avoid out-of-range branches that are due a fundamental limitation of
577 // the way symbol offsets are encoded with the current Darwin ARM
579 const TargetLoweringObjectFileMachO &TLOFMacho =
580 static_cast<const TargetLoweringObjectFileMachO &>(
581 getObjFileLowering());
583 // Collect the set of sections our functions will go into.
584 SetVector<const MCSection *, SmallVector<const MCSection *, 8>,
585 SmallPtrSet<const MCSection *, 8> > TextSections;
586 // Default text section comes first.
587 TextSections.insert(TLOFMacho.getTextSection());
588 // Now any user defined text sections from function attributes.
589 for (Module::iterator F = M.begin(), e = M.end(); F != e; ++F)
590 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage())
591 TextSections.insert(TLOFMacho.SectionForGlobal(F, Mang, TM));
592 // Now the coalescable sections.
593 TextSections.insert(TLOFMacho.getTextCoalSection());
594 TextSections.insert(TLOFMacho.getConstTextCoalSection());
596 // Emit the sections in the .s file header to fix the order.
597 for (unsigned i = 0, e = TextSections.size(); i != e; ++i)
598 OutStreamer.SwitchSection(TextSections[i]);
600 if (RelocM == Reloc::DynamicNoPIC) {
601 const MCSection *sect =
602 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
603 MCSectionMachO::S_SYMBOL_STUBS,
604 12, SectionKind::getText());
605 OutStreamer.SwitchSection(sect);
607 const MCSection *sect =
608 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
609 MCSectionMachO::S_SYMBOL_STUBS,
610 16, SectionKind::getText());
611 OutStreamer.SwitchSection(sect);
613 const MCSection *StaticInitSect =
614 OutContext.getMachOSection("__TEXT", "__StaticInit",
615 MCSectionMachO::S_REGULAR |
616 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
617 SectionKind::getText());
618 OutStreamer.SwitchSection(StaticInitSect);
622 // Use unified assembler syntax.
623 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
625 // Emit ARM Build Attributes
626 if (Subtarget->isTargetELF())
631 void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
632 if (Subtarget->isTargetDarwin()) {
633 // All darwin targets use mach-o.
634 const TargetLoweringObjectFileMachO &TLOFMacho =
635 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
636 MachineModuleInfoMachO &MMIMacho =
637 MMI->getObjFileInfo<MachineModuleInfoMachO>();
639 // Output non-lazy-pointers for external and common global variables.
640 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
642 if (!Stubs.empty()) {
643 // Switch with ".non_lazy_symbol_pointer" directive.
644 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
646 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
648 OutStreamer.EmitLabel(Stubs[i].first);
649 // .indirect_symbol _foo
650 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
651 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
654 // External to current translation unit.
655 OutStreamer.EmitIntValue(0, 4/*size*/);
657 // Internal to current translation unit.
659 // When we place the LSDA into the TEXT section, the type info
660 // pointers need to be indirect and pc-rel. We accomplish this by
661 // using NLPs; however, sometimes the types are local to the file.
662 // We need to fill in the value for the NLP in those cases.
663 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
669 OutStreamer.AddBlankLine();
672 Stubs = MMIMacho.GetHiddenGVStubList();
673 if (!Stubs.empty()) {
674 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
676 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
678 OutStreamer.EmitLabel(Stubs[i].first);
680 OutStreamer.EmitValue(MCSymbolRefExpr::
681 Create(Stubs[i].second.getPointer(),
687 OutStreamer.AddBlankLine();
690 // Funny Darwin hack: This flag tells the linker that no global symbols
691 // contain code that falls through to other global symbols (e.g. the obvious
692 // implementation of multiple entry points). If this doesn't occur, the
693 // linker can safely perform dead code stripping. Since LLVM never
694 // generates code that does this, it is always safe to set.
695 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
697 // FIXME: This should eventually end up somewhere else where more
698 // intelligent flag decisions can be made. For now we are just maintaining
699 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
700 if (MCELFStreamer *MES = dyn_cast<MCELFStreamer>(&OutStreamer))
701 MES->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
704 //===----------------------------------------------------------------------===//
705 // Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
707 // The following seem like one-off assembler flags, but they actually need
708 // to appear in the .ARM.attributes section in ELF.
709 // Instead of subclassing the MCELFStreamer, we do the work here.
711 void ARMAsmPrinter::emitAttributes() {
713 emitARMAttributeSection();
715 /* GAS expect .fpu to be emitted, regardless of VFP build attribute */
716 bool emitFPU = false;
717 AttributeEmitter *AttrEmitter;
718 if (OutStreamer.hasRawTextSupport()) {
719 AttrEmitter = new AsmAttributeEmitter(OutStreamer);
722 MCObjectStreamer &O = static_cast<MCObjectStreamer&>(OutStreamer);
723 AttrEmitter = new ObjectAttributeEmitter(O);
726 AttrEmitter->MaybeSwitchVendor("aeabi");
728 std::string CPUString = Subtarget->getCPUString();
730 if (CPUString == "cortex-a8" ||
731 Subtarget->isCortexA8()) {
732 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::CPU_name, "cortex-a8");
733 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7);
734 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch_profile,
735 ARMBuildAttrs::ApplicationProfile);
736 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
737 ARMBuildAttrs::Allowed);
738 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
739 ARMBuildAttrs::AllowThumb32);
740 // Fixme: figure out when this is emitted.
741 //AttrEmitter->EmitAttribute(ARMBuildAttrs::WMMX_arch,
742 // ARMBuildAttrs::AllowWMMXv1);
745 /// ADD additional Else-cases here!
746 } else if (CPUString == "xscale") {
747 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5TEJ);
748 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
749 ARMBuildAttrs::Allowed);
750 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
751 ARMBuildAttrs::Allowed);
752 } else if (Subtarget->hasV8Ops())
753 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v8);
754 else if (Subtarget->hasV7Ops()) {
755 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7);
756 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
757 ARMBuildAttrs::AllowThumb32);
758 } else if (Subtarget->hasV6T2Ops())
759 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v6T2);
760 else if (Subtarget->hasV6Ops())
761 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v6);
762 else if (Subtarget->hasV5TEOps())
763 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5TE);
764 else if (Subtarget->hasV5TOps())
765 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5T);
766 else if (Subtarget->hasV4TOps())
767 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4T);
769 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4);
771 if (Subtarget->hasNEON() && emitFPU) {
772 /* NEON is not exactly a VFP architecture, but GAS emit one of
773 * neon/neon-vfpv4/vfpv3/vfpv2 for .fpu parameters */
774 if (Subtarget->hasVFP4())
775 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
778 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch, "neon");
779 /* If emitted for NEON, omit from VFP below, since you can have both
780 * NEON and VFP in build attributes but only one .fpu */
785 if (Subtarget->hasVFP4()) {
786 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
787 ARMBuildAttrs::AllowFPv4A);
789 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv4");
792 } else if (Subtarget->hasVFP3()) {
793 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
794 ARMBuildAttrs::AllowFPv3A);
796 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv3");
799 } else if (Subtarget->hasVFP2()) {
800 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
801 ARMBuildAttrs::AllowFPv2);
803 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv2");
806 /* TODO: ARMBuildAttrs::Allowed is not completely accurate,
807 * since NEON can have 1 (allowed) or 2 (MAC operations) */
808 if (Subtarget->hasNEON()) {
809 AttrEmitter->EmitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
810 ARMBuildAttrs::Allowed);
813 // Signal various FP modes.
814 if (!TM.Options.UnsafeFPMath) {
815 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_denormal,
816 ARMBuildAttrs::Allowed);
817 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
818 ARMBuildAttrs::Allowed);
821 if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath)
822 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
823 ARMBuildAttrs::Allowed);
825 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
826 ARMBuildAttrs::AllowIEE754);
828 // FIXME: add more flags to ARMBuildAttrs.h
829 // 8-bytes alignment stuff.
830 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
831 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
833 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
834 if (Subtarget->isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard) {
835 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3);
836 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_VFP_args, 1);
838 // FIXME: Should we signal R9 usage?
840 if (Subtarget->hasDivide())
841 AttrEmitter->EmitAttribute(ARMBuildAttrs::DIV_use, 1);
843 AttrEmitter->Finish();
847 void ARMAsmPrinter::emitARMAttributeSection() {
849 // [ <section-length> "vendor-name"
850 // [ <file-tag> <size> <attribute>*
851 // | <section-tag> <size> <section-number>* 0 <attribute>*
852 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
856 if (OutStreamer.hasRawTextSupport())
859 const ARMElfTargetObjectFile &TLOFELF =
860 static_cast<const ARMElfTargetObjectFile &>
861 (getObjFileLowering());
863 OutStreamer.SwitchSection(TLOFELF.getAttributesSection());
866 OutStreamer.EmitIntValue(0x41, 1);
869 //===----------------------------------------------------------------------===//
871 static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
872 unsigned LabelId, MCContext &Ctx) {
874 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
875 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
879 static MCSymbolRefExpr::VariantKind
880 getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
882 case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None;
883 case ARMCP::TLSGD: return MCSymbolRefExpr::VK_ARM_TLSGD;
884 case ARMCP::TPOFF: return MCSymbolRefExpr::VK_ARM_TPOFF;
885 case ARMCP::GOTTPOFF: return MCSymbolRefExpr::VK_ARM_GOTTPOFF;
886 case ARMCP::GOT: return MCSymbolRefExpr::VK_ARM_GOT;
887 case ARMCP::GOTOFF: return MCSymbolRefExpr::VK_ARM_GOTOFF;
889 llvm_unreachable("Invalid ARMCPModifier!");
892 MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV) {
893 bool isIndirect = Subtarget->isTargetDarwin() &&
894 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
896 return Mang->getSymbol(GV);
898 // FIXME: Remove this when Darwin transition to @GOT like syntax.
899 MCSymbol *MCSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
900 MachineModuleInfoMachO &MMIMachO =
901 MMI->getObjFileInfo<MachineModuleInfoMachO>();
902 MachineModuleInfoImpl::StubValueTy &StubSym =
903 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym) :
904 MMIMachO.getGVStubEntry(MCSym);
905 if (StubSym.getPointer() == 0)
906 StubSym = MachineModuleInfoImpl::
907 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
912 EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
913 int Size = TM.getDataLayout()->getTypeAllocSize(MCPV->getType());
915 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
918 if (ACPV->isLSDA()) {
919 SmallString<128> Str;
920 raw_svector_ostream OS(Str);
921 OS << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
922 MCSym = OutContext.GetOrCreateSymbol(OS.str());
923 } else if (ACPV->isBlockAddress()) {
924 const BlockAddress *BA =
925 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
926 MCSym = GetBlockAddressSymbol(BA);
927 } else if (ACPV->isGlobalValue()) {
928 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
929 MCSym = GetARMGVSymbol(GV);
930 } else if (ACPV->isMachineBasicBlock()) {
931 const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
932 MCSym = MBB->getSymbol();
934 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
935 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
936 MCSym = GetExternalSymbolSymbol(Sym);
939 // Create an MCSymbol for the reference.
941 MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()),
944 if (ACPV->getPCAdjustment()) {
945 MCSymbol *PCLabel = getPICLabel(MAI->getPrivateGlobalPrefix(),
949 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
951 MCBinaryExpr::CreateAdd(PCRelExpr,
952 MCConstantExpr::Create(ACPV->getPCAdjustment(),
955 if (ACPV->mustAddCurrentAddress()) {
956 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
957 // label, so just emit a local label end reference that instead.
958 MCSymbol *DotSym = OutContext.CreateTempSymbol();
959 OutStreamer.EmitLabel(DotSym);
960 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
961 PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext);
963 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
965 OutStreamer.EmitValue(Expr, Size);
968 void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
969 unsigned Opcode = MI->getOpcode();
971 if (Opcode == ARM::BR_JTadd)
973 else if (Opcode == ARM::BR_JTm)
976 const MachineOperand &MO1 = MI->getOperand(OpNum);
977 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
978 unsigned JTI = MO1.getIndex();
980 // Emit a label for the jump table.
981 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
982 OutStreamer.EmitLabel(JTISymbol);
984 // Mark the jump table as data-in-code.
985 OutStreamer.EmitDataRegion(MCDR_DataRegionJT32);
987 // Emit each entry of the table.
988 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
989 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
990 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
992 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
993 MachineBasicBlock *MBB = JTBBs[i];
994 // Construct an MCExpr for the entry. We want a value of the form:
995 // (BasicBlockAddr - TableBeginAddr)
997 // For example, a table with entries jumping to basic blocks BB0 and BB1
1000 // .word (LBB0 - LJTI_0_0)
1001 // .word (LBB1 - LJTI_0_0)
1002 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
1004 if (TM.getRelocationModel() == Reloc::PIC_)
1005 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
1008 // If we're generating a table of Thumb addresses in static relocation
1009 // model, we need to add one to keep interworking correctly.
1010 else if (AFI->isThumbFunction())
1011 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(1,OutContext),
1013 OutStreamer.EmitValue(Expr, 4);
1015 // Mark the end of jump table data-in-code region.
1016 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1019 void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
1020 unsigned Opcode = MI->getOpcode();
1021 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
1022 const MachineOperand &MO1 = MI->getOperand(OpNum);
1023 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
1024 unsigned JTI = MO1.getIndex();
1026 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
1027 OutStreamer.EmitLabel(JTISymbol);
1029 // Emit each entry of the table.
1030 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1031 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1032 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1033 unsigned OffsetWidth = 4;
1034 if (MI->getOpcode() == ARM::t2TBB_JT) {
1036 // Mark the jump table as data-in-code.
1037 OutStreamer.EmitDataRegion(MCDR_DataRegionJT8);
1038 } else if (MI->getOpcode() == ARM::t2TBH_JT) {
1040 // Mark the jump table as data-in-code.
1041 OutStreamer.EmitDataRegion(MCDR_DataRegionJT16);
1044 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1045 MachineBasicBlock *MBB = JTBBs[i];
1046 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
1048 // If this isn't a TBB or TBH, the entries are direct branch instructions.
1049 if (OffsetWidth == 4) {
1050 OutStreamer.EmitInstruction(MCInstBuilder(ARM::t2B)
1051 .addExpr(MBBSymbolExpr)
1056 // Otherwise it's an offset from the dispatch instruction. Construct an
1057 // MCExpr for the entry. We want a value of the form:
1058 // (BasicBlockAddr - TableBeginAddr) / 2
1060 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
1063 // .byte (LBB0 - LJTI_0_0) / 2
1064 // .byte (LBB1 - LJTI_0_0) / 2
1065 const MCExpr *Expr =
1066 MCBinaryExpr::CreateSub(MBBSymbolExpr,
1067 MCSymbolRefExpr::Create(JTISymbol, OutContext),
1069 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
1071 OutStreamer.EmitValue(Expr, OffsetWidth);
1073 // Mark the end of jump table data-in-code region. 32-bit offsets use
1074 // actual branch instructions here, so we don't mark those as a data-region
1076 if (OffsetWidth != 4)
1077 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1080 void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
1081 assert(MI->getFlag(MachineInstr::FrameSetup) &&
1082 "Only instruction which are involved into frame setup code are allowed");
1084 const MachineFunction &MF = *MI->getParent()->getParent();
1085 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
1086 const ARMFunctionInfo &AFI = *MF.getInfo<ARMFunctionInfo>();
1088 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1089 unsigned Opc = MI->getOpcode();
1090 unsigned SrcReg, DstReg;
1092 if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) {
1093 // Two special cases:
1094 // 1) tPUSH does not have src/dst regs.
1095 // 2) for Thumb1 code we sometimes materialize the constant via constpool
1096 // load. Yes, this is pretty fragile, but for now I don't see better
1098 SrcReg = DstReg = ARM::SP;
1100 SrcReg = MI->getOperand(1).getReg();
1101 DstReg = MI->getOperand(0).getReg();
1104 // Try to figure out the unwinding opcode out of src / dst regs.
1105 if (MI->mayStore()) {
1107 assert(DstReg == ARM::SP &&
1108 "Only stack pointer as a destination reg is supported");
1110 SmallVector<unsigned, 4> RegList;
1111 // Skip src & dst reg, and pred ops.
1112 unsigned StartOp = 2 + 2;
1113 // Use all the operands.
1114 unsigned NumOffset = 0;
1119 llvm_unreachable("Unsupported opcode for unwinding information");
1121 // Special case here: no src & dst reg, but two extra imp ops.
1122 StartOp = 2; NumOffset = 2;
1123 case ARM::STMDB_UPD:
1124 case ARM::t2STMDB_UPD:
1125 case ARM::VSTMDDB_UPD:
1126 assert(SrcReg == ARM::SP &&
1127 "Only stack pointer as a source reg is supported");
1128 for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
1130 const MachineOperand &MO = MI->getOperand(i);
1131 // Actually, there should never be any impdef stuff here. Skip it
1132 // temporary to workaround PR11902.
1133 if (MO.isImplicit())
1135 RegList.push_back(MO.getReg());
1138 case ARM::STR_PRE_IMM:
1139 case ARM::STR_PRE_REG:
1140 case ARM::t2STR_PRE:
1141 assert(MI->getOperand(2).getReg() == ARM::SP &&
1142 "Only stack pointer as a source reg is supported");
1143 RegList.push_back(SrcReg);
1146 OutStreamer.EmitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
1148 // Changes of stack / frame pointer.
1149 if (SrcReg == ARM::SP) {
1154 llvm_unreachable("Unsupported opcode for unwinding information");
1160 Offset = -MI->getOperand(2).getImm();
1164 Offset = MI->getOperand(2).getImm();
1167 Offset = MI->getOperand(2).getImm()*4;
1171 Offset = -MI->getOperand(2).getImm()*4;
1173 case ARM::tLDRpci: {
1174 // Grab the constpool index and check, whether it corresponds to
1175 // original or cloned constpool entry.
1176 unsigned CPI = MI->getOperand(1).getIndex();
1177 const MachineConstantPool *MCP = MF.getConstantPool();
1178 if (CPI >= MCP->getConstants().size())
1179 CPI = AFI.getOriginalCPIdx(CPI);
1180 assert(CPI != -1U && "Invalid constpool index");
1182 // Derive the actual offset.
1183 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
1184 assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
1185 // FIXME: Check for user, it should be "add" instruction!
1186 Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
1191 if (DstReg == FramePtr && FramePtr != ARM::SP)
1192 // Set-up of the frame pointer. Positive values correspond to "add"
1194 OutStreamer.EmitSetFP(FramePtr, ARM::SP, -Offset);
1195 else if (DstReg == ARM::SP) {
1196 // Change of SP by an offset. Positive values correspond to "sub"
1198 OutStreamer.EmitPad(Offset);
1201 llvm_unreachable("Unsupported opcode for unwinding information");
1203 } else if (DstReg == ARM::SP) {
1204 // FIXME: .movsp goes here
1206 llvm_unreachable("Unsupported opcode for unwinding information");
1210 llvm_unreachable("Unsupported opcode for unwinding information");
1215 extern cl::opt<bool> EnableARMEHABI;
1217 // Simple pseudo-instructions have their lowering (with expansion to real
1218 // instructions) auto-generated.
1219 #include "ARMGenMCPseudoLowering.inc"
1221 void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
1222 // If we just ended a constant pool, mark it as such.
1223 if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) {
1224 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1225 InConstantPool = false;
1228 // Emit unwinding stuff for frame-related instructions
1229 if (EnableARMEHABI && MI->getFlag(MachineInstr::FrameSetup))
1230 EmitUnwindingInstruction(MI);
1232 // Do any auto-generated pseudo lowerings.
1233 if (emitPseudoExpansionLowering(OutStreamer, MI))
1236 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
1237 "Pseudo flag setting opcode should be expanded early");
1239 // Check for manual lowerings.
1240 unsigned Opc = MI->getOpcode();
1242 case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass");
1243 case ARM::DBG_VALUE: llvm_unreachable("Should be handled by generic printing");
1245 case ARM::tLEApcrel:
1246 case ARM::t2LEApcrel: {
1247 // FIXME: Need to also handle globals and externals
1248 MCSymbol *CPISymbol = GetCPISymbol(MI->getOperand(1).getIndex());
1249 OutStreamer.EmitInstruction(MCInstBuilder(MI->getOpcode() ==
1250 ARM::t2LEApcrel ? ARM::t2ADR
1251 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
1253 .addReg(MI->getOperand(0).getReg())
1254 .addExpr(MCSymbolRefExpr::Create(CPISymbol, OutContext))
1255 // Add predicate operands.
1256 .addImm(MI->getOperand(2).getImm())
1257 .addReg(MI->getOperand(3).getReg()));
1260 case ARM::LEApcrelJT:
1261 case ARM::tLEApcrelJT:
1262 case ARM::t2LEApcrelJT: {
1263 MCSymbol *JTIPICSymbol =
1264 GetARMJTIPICJumpTableLabel2(MI->getOperand(1).getIndex(),
1265 MI->getOperand(2).getImm());
1266 OutStreamer.EmitInstruction(MCInstBuilder(MI->getOpcode() ==
1267 ARM::t2LEApcrelJT ? ARM::t2ADR
1268 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
1270 .addReg(MI->getOperand(0).getReg())
1271 .addExpr(MCSymbolRefExpr::Create(JTIPICSymbol, OutContext))
1272 // Add predicate operands.
1273 .addImm(MI->getOperand(3).getImm())
1274 .addReg(MI->getOperand(4).getReg()));
1277 // Darwin call instructions are just normal call instructions with different
1278 // clobber semantics (they clobber R9).
1279 case ARM::BX_CALL: {
1280 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr)
1283 // Add predicate operands.
1286 // Add 's' bit operand (always reg0 for this)
1289 OutStreamer.EmitInstruction(MCInstBuilder(ARM::BX)
1290 .addReg(MI->getOperand(0).getReg()));
1293 case ARM::tBX_CALL: {
1294 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr)
1297 // Add predicate operands.
1301 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tBX)
1302 .addReg(MI->getOperand(0).getReg())
1303 // Add predicate operands.
1308 case ARM::BMOVPCRX_CALL: {
1309 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr)
1312 // Add predicate operands.
1315 // Add 's' bit operand (always reg0 for this)
1318 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr)
1320 .addReg(MI->getOperand(0).getReg())
1321 // Add predicate operands.
1324 // Add 's' bit operand (always reg0 for this)
1328 case ARM::BMOVPCB_CALL: {
1329 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr)
1332 // Add predicate operands.
1335 // Add 's' bit operand (always reg0 for this)
1338 const GlobalValue *GV = MI->getOperand(0).getGlobal();
1339 MCSymbol *GVSym = Mang->getSymbol(GV);
1340 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1341 OutStreamer.EmitInstruction(MCInstBuilder(ARM::Bcc)
1343 // Add predicate operands.
1348 case ARM::MOVi16_ga_pcrel:
1349 case ARM::t2MOVi16_ga_pcrel: {
1351 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
1352 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1354 unsigned TF = MI->getOperand(1).getTargetFlags();
1355 bool isPIC = TF == ARMII::MO_LO16_NONLAZY_PIC;
1356 const GlobalValue *GV = MI->getOperand(1).getGlobal();
1357 MCSymbol *GVSym = GetARMGVSymbol(GV);
1358 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1360 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1361 getFunctionNumber(),
1362 MI->getOperand(2).getImm(), OutContext);
1363 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1364 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
1365 const MCExpr *PCRelExpr =
1366 ARMMCExpr::CreateLower16(MCBinaryExpr::CreateSub(GVSymExpr,
1367 MCBinaryExpr::CreateAdd(LabelSymExpr,
1368 MCConstantExpr::Create(PCAdj, OutContext),
1369 OutContext), OutContext), OutContext);
1370 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1372 const MCExpr *RefExpr= ARMMCExpr::CreateLower16(GVSymExpr, OutContext);
1373 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1376 // Add predicate operands.
1377 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1378 TmpInst.addOperand(MCOperand::CreateReg(0));
1379 // Add 's' bit operand (always reg0 for this)
1380 TmpInst.addOperand(MCOperand::CreateReg(0));
1381 OutStreamer.EmitInstruction(TmpInst);
1384 case ARM::MOVTi16_ga_pcrel:
1385 case ARM::t2MOVTi16_ga_pcrel: {
1387 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
1388 ? ARM::MOVTi16 : ARM::t2MOVTi16);
1389 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1390 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1392 unsigned TF = MI->getOperand(2).getTargetFlags();
1393 bool isPIC = TF == ARMII::MO_HI16_NONLAZY_PIC;
1394 const GlobalValue *GV = MI->getOperand(2).getGlobal();
1395 MCSymbol *GVSym = GetARMGVSymbol(GV);
1396 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1398 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1399 getFunctionNumber(),
1400 MI->getOperand(3).getImm(), OutContext);
1401 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1402 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
1403 const MCExpr *PCRelExpr =
1404 ARMMCExpr::CreateUpper16(MCBinaryExpr::CreateSub(GVSymExpr,
1405 MCBinaryExpr::CreateAdd(LabelSymExpr,
1406 MCConstantExpr::Create(PCAdj, OutContext),
1407 OutContext), OutContext), OutContext);
1408 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1410 const MCExpr *RefExpr= ARMMCExpr::CreateUpper16(GVSymExpr, OutContext);
1411 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1413 // Add predicate operands.
1414 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1415 TmpInst.addOperand(MCOperand::CreateReg(0));
1416 // Add 's' bit operand (always reg0 for this)
1417 TmpInst.addOperand(MCOperand::CreateReg(0));
1418 OutStreamer.EmitInstruction(TmpInst);
1421 case ARM::tPICADD: {
1422 // This is a pseudo op for a label + instruction sequence, which looks like:
1425 // This adds the address of LPC0 to r0.
1428 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1429 getFunctionNumber(), MI->getOperand(2).getImm(),
1432 // Form and emit the add.
1433 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tADDhirr)
1434 .addReg(MI->getOperand(0).getReg())
1435 .addReg(MI->getOperand(0).getReg())
1437 // Add predicate operands.
1443 // This is a pseudo op for a label + instruction sequence, which looks like:
1446 // This adds the address of LPC0 to r0.
1449 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1450 getFunctionNumber(), MI->getOperand(2).getImm(),
1453 // Form and emit the add.
1454 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDrr)
1455 .addReg(MI->getOperand(0).getReg())
1457 .addReg(MI->getOperand(1).getReg())
1458 // Add predicate operands.
1459 .addImm(MI->getOperand(3).getImm())
1460 .addReg(MI->getOperand(4).getReg())
1461 // Add 's' bit operand (always reg0 for this)
1472 case ARM::PICLDRSH: {
1473 // This is a pseudo op for a label + instruction sequence, which looks like:
1476 // The LCP0 label is referenced by a constant pool entry in order to get
1477 // a PC-relative address at the ldr instruction.
1480 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1481 getFunctionNumber(), MI->getOperand(2).getImm(),
1484 // Form and emit the load
1486 switch (MI->getOpcode()) {
1488 llvm_unreachable("Unexpected opcode!");
1489 case ARM::PICSTR: Opcode = ARM::STRrs; break;
1490 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
1491 case ARM::PICSTRH: Opcode = ARM::STRH; break;
1492 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
1493 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
1494 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
1495 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1496 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1498 OutStreamer.EmitInstruction(MCInstBuilder(Opcode)
1499 .addReg(MI->getOperand(0).getReg())
1501 .addReg(MI->getOperand(1).getReg())
1503 // Add predicate operands.
1504 .addImm(MI->getOperand(3).getImm())
1505 .addReg(MI->getOperand(4).getReg()));
1509 case ARM::CONSTPOOL_ENTRY: {
1510 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1511 /// in the function. The first operand is the ID# for this instruction, the
1512 /// second is the index into the MachineConstantPool that this is, the third
1513 /// is the size in bytes of this constant pool entry.
1514 /// The required alignment is specified on the basic block holding this MI.
1515 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1516 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
1518 // If this is the first entry of the pool, mark it.
1519 if (!InConstantPool) {
1520 OutStreamer.EmitDataRegion(MCDR_DataRegion);
1521 InConstantPool = true;
1524 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
1526 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1527 if (MCPE.isMachineConstantPoolEntry())
1528 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1530 EmitGlobalConstant(MCPE.Val.ConstVal);
1533 case ARM::t2BR_JT: {
1534 // Lower and emit the instruction itself, then the jump table following it.
1535 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr)
1537 .addReg(MI->getOperand(0).getReg())
1538 // Add predicate operands.
1542 // Output the data for the jump table itself
1546 case ARM::t2TBB_JT: {
1547 // Lower and emit the instruction itself, then the jump table following it.
1548 OutStreamer.EmitInstruction(MCInstBuilder(ARM::t2TBB)
1550 .addReg(MI->getOperand(0).getReg())
1551 // Add predicate operands.
1555 // Output the data for the jump table itself
1557 // Make sure the next instruction is 2-byte aligned.
1561 case ARM::t2TBH_JT: {
1562 // Lower and emit the instruction itself, then the jump table following it.
1563 OutStreamer.EmitInstruction(MCInstBuilder(ARM::t2TBH)
1565 .addReg(MI->getOperand(0).getReg())
1566 // Add predicate operands.
1570 // Output the data for the jump table itself
1576 // Lower and emit the instruction itself, then the jump table following it.
1579 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
1580 ARM::MOVr : ARM::tMOVr;
1581 TmpInst.setOpcode(Opc);
1582 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1583 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1584 // Add predicate operands.
1585 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1586 TmpInst.addOperand(MCOperand::CreateReg(0));
1587 // Add 's' bit operand (always reg0 for this)
1588 if (Opc == ARM::MOVr)
1589 TmpInst.addOperand(MCOperand::CreateReg(0));
1590 OutStreamer.EmitInstruction(TmpInst);
1592 // Make sure the Thumb jump table is 4-byte aligned.
1593 if (Opc == ARM::tMOVr)
1596 // Output the data for the jump table itself
1601 // Lower and emit the instruction itself, then the jump table following it.
1604 if (MI->getOperand(1).getReg() == 0) {
1606 TmpInst.setOpcode(ARM::LDRi12);
1607 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1608 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1609 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1611 TmpInst.setOpcode(ARM::LDRrs);
1612 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1613 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1614 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1615 TmpInst.addOperand(MCOperand::CreateImm(0));
1617 // Add predicate operands.
1618 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1619 TmpInst.addOperand(MCOperand::CreateReg(0));
1620 OutStreamer.EmitInstruction(TmpInst);
1622 // Output the data for the jump table itself
1626 case ARM::BR_JTadd: {
1627 // Lower and emit the instruction itself, then the jump table following it.
1628 // add pc, target, idx
1629 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDrr)
1631 .addReg(MI->getOperand(0).getReg())
1632 .addReg(MI->getOperand(1).getReg())
1633 // Add predicate operands.
1636 // Add 's' bit operand (always reg0 for this)
1639 // Output the data for the jump table itself
1644 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1645 // FIXME: Remove this special case when they do.
1646 if (!Subtarget->isTargetDarwin()) {
1647 //.long 0xe7ffdefe @ trap
1648 uint32_t Val = 0xe7ffdefeUL;
1649 OutStreamer.AddComment("trap");
1650 OutStreamer.EmitIntValue(Val, 4);
1655 case ARM::TRAPNaCl: {
1656 //.long 0xe7fedef0 @ trap
1657 uint32_t Val = 0xe7fedef0UL;
1658 OutStreamer.AddComment("trap");
1659 OutStreamer.EmitIntValue(Val, 4);
1663 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1664 // FIXME: Remove this special case when they do.
1665 if (!Subtarget->isTargetDarwin()) {
1666 //.short 57086 @ trap
1667 uint16_t Val = 0xdefe;
1668 OutStreamer.AddComment("trap");
1669 OutStreamer.EmitIntValue(Val, 2);
1674 case ARM::t2Int_eh_sjlj_setjmp:
1675 case ARM::t2Int_eh_sjlj_setjmp_nofp:
1676 case ARM::tInt_eh_sjlj_setjmp: {
1677 // Two incoming args: GPR:$src, GPR:$val
1680 // str $val, [$src, #4]
1685 unsigned SrcReg = MI->getOperand(0).getReg();
1686 unsigned ValReg = MI->getOperand(1).getReg();
1687 MCSymbol *Label = GetARMSJLJEHLabel();
1688 OutStreamer.AddComment("eh_setjmp begin");
1689 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr)
1696 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tADDi3)
1706 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tSTRi)
1709 // The offset immediate is #4. The operand value is scaled by 4 for the
1710 // tSTR instruction.
1716 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVi8)
1724 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1725 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tB)
1726 .addExpr(SymbolExpr)
1730 OutStreamer.AddComment("eh_setjmp end");
1731 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVi8)
1739 OutStreamer.EmitLabel(Label);
1743 case ARM::Int_eh_sjlj_setjmp_nofp:
1744 case ARM::Int_eh_sjlj_setjmp: {
1745 // Two incoming args: GPR:$src, GPR:$val
1747 // str $val, [$src, #+4]
1751 unsigned SrcReg = MI->getOperand(0).getReg();
1752 unsigned ValReg = MI->getOperand(1).getReg();
1754 OutStreamer.AddComment("eh_setjmp begin");
1755 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDri)
1762 // 's' bit operand (always reg0 for this).
1765 OutStreamer.EmitInstruction(MCInstBuilder(ARM::STRi12)
1773 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVi)
1779 // 's' bit operand (always reg0 for this).
1782 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDri)
1789 // 's' bit operand (always reg0 for this).
1792 OutStreamer.AddComment("eh_setjmp end");
1793 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVi)
1799 // 's' bit operand (always reg0 for this).
1803 case ARM::Int_eh_sjlj_longjmp: {
1804 // ldr sp, [$src, #8]
1805 // ldr $scratch, [$src, #4]
1808 unsigned SrcReg = MI->getOperand(0).getReg();
1809 unsigned ScratchReg = MI->getOperand(1).getReg();
1810 OutStreamer.EmitInstruction(MCInstBuilder(ARM::LDRi12)
1818 OutStreamer.EmitInstruction(MCInstBuilder(ARM::LDRi12)
1826 OutStreamer.EmitInstruction(MCInstBuilder(ARM::LDRi12)
1834 OutStreamer.EmitInstruction(MCInstBuilder(ARM::BX)
1841 case ARM::tInt_eh_sjlj_longjmp: {
1842 // ldr $scratch, [$src, #8]
1844 // ldr $scratch, [$src, #4]
1847 unsigned SrcReg = MI->getOperand(0).getReg();
1848 unsigned ScratchReg = MI->getOperand(1).getReg();
1849 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tLDRi)
1852 // The offset immediate is #8. The operand value is scaled by 4 for the
1853 // tLDR instruction.
1859 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr)
1866 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tLDRi)
1874 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tLDRi)
1882 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tBX)
1892 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
1894 OutStreamer.EmitInstruction(TmpInst);
1897 //===----------------------------------------------------------------------===//
1898 // Target Registry Stuff
1899 //===----------------------------------------------------------------------===//
1901 // Force static initialization.
1902 extern "C" void LLVMInitializeARMAsmPrinter() {
1903 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1904 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);