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 if (ARM::GPRPairRegClass.contains(RegBegin)) {
469 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
470 unsigned Reg0 = TRI->getSubReg(RegBegin, ARM::gsub_0);
471 O << ARMInstPrinter::getRegisterName(Reg0) << ", ";;
472 RegBegin = TRI->getSubReg(RegBegin, ARM::gsub_1);
474 O << ARMInstPrinter::getRegisterName(RegBegin);
476 // FIXME: The register allocator not only may not have given us the
477 // registers in sequence, but may not be in ascending registers. This
478 // will require changes in the register allocator that'll need to be
479 // propagated down here if the operands change.
480 unsigned RegOps = OpNum + 1;
481 while (MI->getOperand(RegOps).isReg()) {
483 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
491 case 'R': // The most significant register of a pair.
492 case 'Q': { // The least significant register of a pair.
495 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
496 if (!FlagsOP.isImm())
498 unsigned Flags = FlagsOP.getImm();
500 // This operand may not be the one that actually provides the register. If
501 // it's tied to a previous one then we should refer instead to that one
502 // for registers and their classes.
504 if (InlineAsm::isUseOperandTiedToDef(Flags, TiedIdx)) {
505 for (OpNum = InlineAsm::MIOp_FirstOperand; TiedIdx; --TiedIdx) {
506 unsigned OpFlags = MI->getOperand(OpNum).getImm();
507 OpNum += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
509 Flags = MI->getOperand(OpNum).getImm();
511 // Later code expects OpNum to be pointing at the register rather than
516 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
518 InlineAsm::hasRegClassConstraint(Flags, RC);
519 if (RC == ARM::GPRPairRegClassID) {
522 const MachineOperand &MO = MI->getOperand(OpNum);
525 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
526 unsigned Reg = TRI->getSubReg(MO.getReg(), ExtraCode[0] == 'Q' ?
527 ARM::gsub_0 : ARM::gsub_1);
528 O << ARMInstPrinter::getRegisterName(Reg);
533 unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1;
534 if (RegOp >= MI->getNumOperands())
536 const MachineOperand &MO = MI->getOperand(RegOp);
539 unsigned Reg = MO.getReg();
540 O << ARMInstPrinter::getRegisterName(Reg);
544 case 'e': // The low doubleword register of a NEON quad register.
545 case 'f': { // The high doubleword register of a NEON quad register.
546 if (!MI->getOperand(OpNum).isReg())
548 unsigned Reg = MI->getOperand(OpNum).getReg();
549 if (!ARM::QPRRegClass.contains(Reg))
551 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
552 unsigned SubReg = TRI->getSubReg(Reg, ExtraCode[0] == 'e' ?
553 ARM::dsub_0 : ARM::dsub_1);
554 O << ARMInstPrinter::getRegisterName(SubReg);
558 // This modifier is not yet supported.
559 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
561 case 'H': { // The highest-numbered register of a pair.
562 const MachineOperand &MO = MI->getOperand(OpNum);
565 const MachineFunction &MF = *MI->getParent()->getParent();
566 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
567 unsigned Reg = MO.getReg();
568 if(!ARM::GPRPairRegClass.contains(Reg))
570 Reg = TRI->getSubReg(Reg, ARM::gsub_1);
571 O << ARMInstPrinter::getRegisterName(Reg);
577 printOperand(MI, OpNum, O);
581 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
582 unsigned OpNum, unsigned AsmVariant,
583 const char *ExtraCode,
585 // Does this asm operand have a single letter operand modifier?
586 if (ExtraCode && ExtraCode[0]) {
587 if (ExtraCode[1] != 0) return true; // Unknown modifier.
589 switch (ExtraCode[0]) {
590 case 'A': // A memory operand for a VLD1/VST1 instruction.
591 default: return true; // Unknown modifier.
592 case 'm': // The base register of a memory operand.
593 if (!MI->getOperand(OpNum).isReg())
595 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
600 const MachineOperand &MO = MI->getOperand(OpNum);
601 assert(MO.isReg() && "unexpected inline asm memory operand");
602 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
606 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
607 if (Subtarget->isTargetDarwin()) {
608 Reloc::Model RelocM = TM.getRelocationModel();
609 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
610 // Declare all the text sections up front (before the DWARF sections
611 // emitted by AsmPrinter::doInitialization) so the assembler will keep
612 // them together at the beginning of the object file. This helps
613 // avoid out-of-range branches that are due a fundamental limitation of
614 // the way symbol offsets are encoded with the current Darwin ARM
616 const TargetLoweringObjectFileMachO &TLOFMacho =
617 static_cast<const TargetLoweringObjectFileMachO &>(
618 getObjFileLowering());
620 // Collect the set of sections our functions will go into.
621 SetVector<const MCSection *, SmallVector<const MCSection *, 8>,
622 SmallPtrSet<const MCSection *, 8> > TextSections;
623 // Default text section comes first.
624 TextSections.insert(TLOFMacho.getTextSection());
625 // Now any user defined text sections from function attributes.
626 for (Module::iterator F = M.begin(), e = M.end(); F != e; ++F)
627 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage())
628 TextSections.insert(TLOFMacho.SectionForGlobal(F, Mang, TM));
629 // Now the coalescable sections.
630 TextSections.insert(TLOFMacho.getTextCoalSection());
631 TextSections.insert(TLOFMacho.getConstTextCoalSection());
633 // Emit the sections in the .s file header to fix the order.
634 for (unsigned i = 0, e = TextSections.size(); i != e; ++i)
635 OutStreamer.SwitchSection(TextSections[i]);
637 if (RelocM == Reloc::DynamicNoPIC) {
638 const MCSection *sect =
639 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
640 MCSectionMachO::S_SYMBOL_STUBS,
641 12, SectionKind::getText());
642 OutStreamer.SwitchSection(sect);
644 const MCSection *sect =
645 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
646 MCSectionMachO::S_SYMBOL_STUBS,
647 16, SectionKind::getText());
648 OutStreamer.SwitchSection(sect);
650 const MCSection *StaticInitSect =
651 OutContext.getMachOSection("__TEXT", "__StaticInit",
652 MCSectionMachO::S_REGULAR |
653 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
654 SectionKind::getText());
655 OutStreamer.SwitchSection(StaticInitSect);
659 // Use unified assembler syntax.
660 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
662 // Emit ARM Build Attributes
663 if (Subtarget->isTargetELF())
668 void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
669 if (Subtarget->isTargetDarwin()) {
670 // All darwin targets use mach-o.
671 const TargetLoweringObjectFileMachO &TLOFMacho =
672 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
673 MachineModuleInfoMachO &MMIMacho =
674 MMI->getObjFileInfo<MachineModuleInfoMachO>();
676 // Output non-lazy-pointers for external and common global variables.
677 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
679 if (!Stubs.empty()) {
680 // Switch with ".non_lazy_symbol_pointer" directive.
681 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
683 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
685 OutStreamer.EmitLabel(Stubs[i].first);
686 // .indirect_symbol _foo
687 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
688 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
691 // External to current translation unit.
692 OutStreamer.EmitIntValue(0, 4/*size*/);
694 // Internal to current translation unit.
696 // When we place the LSDA into the TEXT section, the type info
697 // pointers need to be indirect and pc-rel. We accomplish this by
698 // using NLPs; however, sometimes the types are local to the file.
699 // We need to fill in the value for the NLP in those cases.
700 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
706 OutStreamer.AddBlankLine();
709 Stubs = MMIMacho.GetHiddenGVStubList();
710 if (!Stubs.empty()) {
711 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
713 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
715 OutStreamer.EmitLabel(Stubs[i].first);
717 OutStreamer.EmitValue(MCSymbolRefExpr::
718 Create(Stubs[i].second.getPointer(),
724 OutStreamer.AddBlankLine();
727 // Funny Darwin hack: This flag tells the linker that no global symbols
728 // contain code that falls through to other global symbols (e.g. the obvious
729 // implementation of multiple entry points). If this doesn't occur, the
730 // linker can safely perform dead code stripping. Since LLVM never
731 // generates code that does this, it is always safe to set.
732 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
734 // FIXME: This should eventually end up somewhere else where more
735 // intelligent flag decisions can be made. For now we are just maintaining
736 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
737 if (MCELFStreamer *MES = dyn_cast<MCELFStreamer>(&OutStreamer))
738 MES->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
741 //===----------------------------------------------------------------------===//
742 // Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
744 // The following seem like one-off assembler flags, but they actually need
745 // to appear in the .ARM.attributes section in ELF.
746 // Instead of subclassing the MCELFStreamer, we do the work here.
748 void ARMAsmPrinter::emitAttributes() {
750 emitARMAttributeSection();
752 /* GAS expect .fpu to be emitted, regardless of VFP build attribute */
753 bool emitFPU = false;
754 AttributeEmitter *AttrEmitter;
755 if (OutStreamer.hasRawTextSupport()) {
756 AttrEmitter = new AsmAttributeEmitter(OutStreamer);
759 MCObjectStreamer &O = static_cast<MCObjectStreamer&>(OutStreamer);
760 AttrEmitter = new ObjectAttributeEmitter(O);
763 AttrEmitter->MaybeSwitchVendor("aeabi");
765 std::string CPUString = Subtarget->getCPUString();
767 if (CPUString == "cortex-a8" ||
768 Subtarget->isCortexA8()) {
769 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::CPU_name, "cortex-a8");
770 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7);
771 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch_profile,
772 ARMBuildAttrs::ApplicationProfile);
773 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
774 ARMBuildAttrs::Allowed);
775 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
776 ARMBuildAttrs::AllowThumb32);
777 // Fixme: figure out when this is emitted.
778 //AttrEmitter->EmitAttribute(ARMBuildAttrs::WMMX_arch,
779 // ARMBuildAttrs::AllowWMMXv1);
782 /// ADD additional Else-cases here!
783 } else if (CPUString == "xscale") {
784 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5TEJ);
785 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
786 ARMBuildAttrs::Allowed);
787 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
788 ARMBuildAttrs::Allowed);
789 } else if (Subtarget->hasV8Ops())
790 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v8);
791 else if (Subtarget->hasV7Ops()) {
792 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7);
793 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
794 ARMBuildAttrs::AllowThumb32);
795 } else if (Subtarget->hasV6T2Ops())
796 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v6T2);
797 else if (Subtarget->hasV6Ops())
798 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v6);
799 else if (Subtarget->hasV5TEOps())
800 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5TE);
801 else if (Subtarget->hasV5TOps())
802 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5T);
803 else if (Subtarget->hasV4TOps())
804 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4T);
806 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4);
808 if (Subtarget->hasNEON() && emitFPU) {
809 /* NEON is not exactly a VFP architecture, but GAS emit one of
810 * neon/neon-fp-armv8/neon-vfpv4/vfpv3/vfpv2 for .fpu parameters */
811 if (Subtarget->hasV8FP())
812 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
814 else if (Subtarget->hasVFP4())
815 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
818 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch, "neon");
819 /* If emitted for NEON, omit from VFP below, since you can have both
820 * NEON and VFP in build attributes but only one .fpu */
825 if (Subtarget->hasV8FP()) {
826 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
827 ARMBuildAttrs::AllowV8FPA);
829 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "fp-armv8");
831 } else if (Subtarget->hasVFP4()) {
832 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
833 ARMBuildAttrs::AllowFPv4A);
835 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv4");
838 } else if (Subtarget->hasVFP3()) {
839 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
840 ARMBuildAttrs::AllowFPv3A);
842 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv3");
845 } else if (Subtarget->hasVFP2()) {
846 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
847 ARMBuildAttrs::AllowFPv2);
849 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv2");
852 /* TODO: ARMBuildAttrs::Allowed is not completely accurate,
853 * since NEON can have 1 (allowed) or 2 (MAC operations) */
854 if (Subtarget->hasNEON()) {
855 if (Subtarget->hasV8Ops())
856 AttrEmitter->EmitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
857 ARMBuildAttrs::AllowedNeonV8);
859 AttrEmitter->EmitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
860 ARMBuildAttrs::Allowed);
863 // Signal various FP modes.
864 if (!TM.Options.UnsafeFPMath) {
865 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_denormal,
866 ARMBuildAttrs::Allowed);
867 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
868 ARMBuildAttrs::Allowed);
871 if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath)
872 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
873 ARMBuildAttrs::Allowed);
875 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
876 ARMBuildAttrs::AllowIEE754);
878 // FIXME: add more flags to ARMBuildAttrs.h
879 // 8-bytes alignment stuff.
880 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
881 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
883 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
884 if (Subtarget->isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard) {
885 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3);
886 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_VFP_args, 1);
888 // FIXME: Should we signal R9 usage?
890 if (Subtarget->hasDivide())
891 AttrEmitter->EmitAttribute(ARMBuildAttrs::DIV_use, 1);
893 AttrEmitter->Finish();
897 void ARMAsmPrinter::emitARMAttributeSection() {
899 // [ <section-length> "vendor-name"
900 // [ <file-tag> <size> <attribute>*
901 // | <section-tag> <size> <section-number>* 0 <attribute>*
902 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
906 if (OutStreamer.hasRawTextSupport())
909 const ARMElfTargetObjectFile &TLOFELF =
910 static_cast<const ARMElfTargetObjectFile &>
911 (getObjFileLowering());
913 OutStreamer.SwitchSection(TLOFELF.getAttributesSection());
916 OutStreamer.EmitIntValue(0x41, 1);
919 //===----------------------------------------------------------------------===//
921 static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
922 unsigned LabelId, MCContext &Ctx) {
924 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
925 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
929 static MCSymbolRefExpr::VariantKind
930 getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
932 case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None;
933 case ARMCP::TLSGD: return MCSymbolRefExpr::VK_ARM_TLSGD;
934 case ARMCP::TPOFF: return MCSymbolRefExpr::VK_ARM_TPOFF;
935 case ARMCP::GOTTPOFF: return MCSymbolRefExpr::VK_ARM_GOTTPOFF;
936 case ARMCP::GOT: return MCSymbolRefExpr::VK_ARM_GOT;
937 case ARMCP::GOTOFF: return MCSymbolRefExpr::VK_ARM_GOTOFF;
939 llvm_unreachable("Invalid ARMCPModifier!");
942 MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV) {
943 bool isIndirect = Subtarget->isTargetDarwin() &&
944 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
946 return Mang->getSymbol(GV);
948 // FIXME: Remove this when Darwin transition to @GOT like syntax.
949 MCSymbol *MCSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
950 MachineModuleInfoMachO &MMIMachO =
951 MMI->getObjFileInfo<MachineModuleInfoMachO>();
952 MachineModuleInfoImpl::StubValueTy &StubSym =
953 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym) :
954 MMIMachO.getGVStubEntry(MCSym);
955 if (StubSym.getPointer() == 0)
956 StubSym = MachineModuleInfoImpl::
957 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
962 EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
963 int Size = TM.getDataLayout()->getTypeAllocSize(MCPV->getType());
965 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
968 if (ACPV->isLSDA()) {
969 SmallString<128> Str;
970 raw_svector_ostream OS(Str);
971 OS << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
972 MCSym = OutContext.GetOrCreateSymbol(OS.str());
973 } else if (ACPV->isBlockAddress()) {
974 const BlockAddress *BA =
975 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
976 MCSym = GetBlockAddressSymbol(BA);
977 } else if (ACPV->isGlobalValue()) {
978 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
979 MCSym = GetARMGVSymbol(GV);
980 } else if (ACPV->isMachineBasicBlock()) {
981 const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
982 MCSym = MBB->getSymbol();
984 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
985 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
986 MCSym = GetExternalSymbolSymbol(Sym);
989 // Create an MCSymbol for the reference.
991 MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()),
994 if (ACPV->getPCAdjustment()) {
995 MCSymbol *PCLabel = getPICLabel(MAI->getPrivateGlobalPrefix(),
999 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
1001 MCBinaryExpr::CreateAdd(PCRelExpr,
1002 MCConstantExpr::Create(ACPV->getPCAdjustment(),
1005 if (ACPV->mustAddCurrentAddress()) {
1006 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
1007 // label, so just emit a local label end reference that instead.
1008 MCSymbol *DotSym = OutContext.CreateTempSymbol();
1009 OutStreamer.EmitLabel(DotSym);
1010 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
1011 PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext);
1013 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
1015 OutStreamer.EmitValue(Expr, Size);
1018 void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
1019 unsigned Opcode = MI->getOpcode();
1021 if (Opcode == ARM::BR_JTadd)
1023 else if (Opcode == ARM::BR_JTm)
1026 const MachineOperand &MO1 = MI->getOperand(OpNum);
1027 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
1028 unsigned JTI = MO1.getIndex();
1030 // Emit a label for the jump table.
1031 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
1032 OutStreamer.EmitLabel(JTISymbol);
1034 // Mark the jump table as data-in-code.
1035 OutStreamer.EmitDataRegion(MCDR_DataRegionJT32);
1037 // Emit each entry of the table.
1038 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1039 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1040 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1042 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1043 MachineBasicBlock *MBB = JTBBs[i];
1044 // Construct an MCExpr for the entry. We want a value of the form:
1045 // (BasicBlockAddr - TableBeginAddr)
1047 // For example, a table with entries jumping to basic blocks BB0 and BB1
1050 // .word (LBB0 - LJTI_0_0)
1051 // .word (LBB1 - LJTI_0_0)
1052 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
1054 if (TM.getRelocationModel() == Reloc::PIC_)
1055 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
1058 // If we're generating a table of Thumb addresses in static relocation
1059 // model, we need to add one to keep interworking correctly.
1060 else if (AFI->isThumbFunction())
1061 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(1,OutContext),
1063 OutStreamer.EmitValue(Expr, 4);
1065 // Mark the end of jump table data-in-code region.
1066 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1069 void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
1070 unsigned Opcode = MI->getOpcode();
1071 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
1072 const MachineOperand &MO1 = MI->getOperand(OpNum);
1073 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
1074 unsigned JTI = MO1.getIndex();
1076 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
1077 OutStreamer.EmitLabel(JTISymbol);
1079 // Emit each entry of the table.
1080 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1081 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1082 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1083 unsigned OffsetWidth = 4;
1084 if (MI->getOpcode() == ARM::t2TBB_JT) {
1086 // Mark the jump table as data-in-code.
1087 OutStreamer.EmitDataRegion(MCDR_DataRegionJT8);
1088 } else if (MI->getOpcode() == ARM::t2TBH_JT) {
1090 // Mark the jump table as data-in-code.
1091 OutStreamer.EmitDataRegion(MCDR_DataRegionJT16);
1094 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1095 MachineBasicBlock *MBB = JTBBs[i];
1096 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
1098 // If this isn't a TBB or TBH, the entries are direct branch instructions.
1099 if (OffsetWidth == 4) {
1100 OutStreamer.EmitInstruction(MCInstBuilder(ARM::t2B)
1101 .addExpr(MBBSymbolExpr)
1106 // Otherwise it's an offset from the dispatch instruction. Construct an
1107 // MCExpr for the entry. We want a value of the form:
1108 // (BasicBlockAddr - TableBeginAddr) / 2
1110 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
1113 // .byte (LBB0 - LJTI_0_0) / 2
1114 // .byte (LBB1 - LJTI_0_0) / 2
1115 const MCExpr *Expr =
1116 MCBinaryExpr::CreateSub(MBBSymbolExpr,
1117 MCSymbolRefExpr::Create(JTISymbol, OutContext),
1119 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
1121 OutStreamer.EmitValue(Expr, OffsetWidth);
1123 // Mark the end of jump table data-in-code region. 32-bit offsets use
1124 // actual branch instructions here, so we don't mark those as a data-region
1126 if (OffsetWidth != 4)
1127 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1130 void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
1131 assert(MI->getFlag(MachineInstr::FrameSetup) &&
1132 "Only instruction which are involved into frame setup code are allowed");
1134 const MachineFunction &MF = *MI->getParent()->getParent();
1135 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
1136 const ARMFunctionInfo &AFI = *MF.getInfo<ARMFunctionInfo>();
1138 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1139 unsigned Opc = MI->getOpcode();
1140 unsigned SrcReg, DstReg;
1142 if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) {
1143 // Two special cases:
1144 // 1) tPUSH does not have src/dst regs.
1145 // 2) for Thumb1 code we sometimes materialize the constant via constpool
1146 // load. Yes, this is pretty fragile, but for now I don't see better
1148 SrcReg = DstReg = ARM::SP;
1150 SrcReg = MI->getOperand(1).getReg();
1151 DstReg = MI->getOperand(0).getReg();
1154 // Try to figure out the unwinding opcode out of src / dst regs.
1155 if (MI->mayStore()) {
1157 assert(DstReg == ARM::SP &&
1158 "Only stack pointer as a destination reg is supported");
1160 SmallVector<unsigned, 4> RegList;
1161 // Skip src & dst reg, and pred ops.
1162 unsigned StartOp = 2 + 2;
1163 // Use all the operands.
1164 unsigned NumOffset = 0;
1169 llvm_unreachable("Unsupported opcode for unwinding information");
1171 // Special case here: no src & dst reg, but two extra imp ops.
1172 StartOp = 2; NumOffset = 2;
1173 case ARM::STMDB_UPD:
1174 case ARM::t2STMDB_UPD:
1175 case ARM::VSTMDDB_UPD:
1176 assert(SrcReg == ARM::SP &&
1177 "Only stack pointer as a source reg is supported");
1178 for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
1180 const MachineOperand &MO = MI->getOperand(i);
1181 // Actually, there should never be any impdef stuff here. Skip it
1182 // temporary to workaround PR11902.
1183 if (MO.isImplicit())
1185 RegList.push_back(MO.getReg());
1188 case ARM::STR_PRE_IMM:
1189 case ARM::STR_PRE_REG:
1190 case ARM::t2STR_PRE:
1191 assert(MI->getOperand(2).getReg() == ARM::SP &&
1192 "Only stack pointer as a source reg is supported");
1193 RegList.push_back(SrcReg);
1196 OutStreamer.EmitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
1198 // Changes of stack / frame pointer.
1199 if (SrcReg == ARM::SP) {
1204 llvm_unreachable("Unsupported opcode for unwinding information");
1210 Offset = -MI->getOperand(2).getImm();
1214 Offset = MI->getOperand(2).getImm();
1217 Offset = MI->getOperand(2).getImm()*4;
1221 Offset = -MI->getOperand(2).getImm()*4;
1223 case ARM::tLDRpci: {
1224 // Grab the constpool index and check, whether it corresponds to
1225 // original or cloned constpool entry.
1226 unsigned CPI = MI->getOperand(1).getIndex();
1227 const MachineConstantPool *MCP = MF.getConstantPool();
1228 if (CPI >= MCP->getConstants().size())
1229 CPI = AFI.getOriginalCPIdx(CPI);
1230 assert(CPI != -1U && "Invalid constpool index");
1232 // Derive the actual offset.
1233 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
1234 assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
1235 // FIXME: Check for user, it should be "add" instruction!
1236 Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
1241 if (DstReg == FramePtr && FramePtr != ARM::SP)
1242 // Set-up of the frame pointer. Positive values correspond to "add"
1244 OutStreamer.EmitSetFP(FramePtr, ARM::SP, -Offset);
1245 else if (DstReg == ARM::SP) {
1246 // Change of SP by an offset. Positive values correspond to "sub"
1248 OutStreamer.EmitPad(Offset);
1251 llvm_unreachable("Unsupported opcode for unwinding information");
1253 } else if (DstReg == ARM::SP) {
1254 // FIXME: .movsp goes here
1256 llvm_unreachable("Unsupported opcode for unwinding information");
1260 llvm_unreachable("Unsupported opcode for unwinding information");
1265 extern cl::opt<bool> EnableARMEHABI;
1267 // Simple pseudo-instructions have their lowering (with expansion to real
1268 // instructions) auto-generated.
1269 #include "ARMGenMCPseudoLowering.inc"
1271 void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
1272 // If we just ended a constant pool, mark it as such.
1273 if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) {
1274 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1275 InConstantPool = false;
1278 // Emit unwinding stuff for frame-related instructions
1279 if (EnableARMEHABI && MI->getFlag(MachineInstr::FrameSetup))
1280 EmitUnwindingInstruction(MI);
1282 // Do any auto-generated pseudo lowerings.
1283 if (emitPseudoExpansionLowering(OutStreamer, MI))
1286 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
1287 "Pseudo flag setting opcode should be expanded early");
1289 // Check for manual lowerings.
1290 unsigned Opc = MI->getOpcode();
1292 case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass");
1293 case ARM::DBG_VALUE: llvm_unreachable("Should be handled by generic printing");
1295 case ARM::tLEApcrel:
1296 case ARM::t2LEApcrel: {
1297 // FIXME: Need to also handle globals and externals
1298 MCSymbol *CPISymbol = GetCPISymbol(MI->getOperand(1).getIndex());
1299 OutStreamer.EmitInstruction(MCInstBuilder(MI->getOpcode() ==
1300 ARM::t2LEApcrel ? ARM::t2ADR
1301 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
1303 .addReg(MI->getOperand(0).getReg())
1304 .addExpr(MCSymbolRefExpr::Create(CPISymbol, OutContext))
1305 // Add predicate operands.
1306 .addImm(MI->getOperand(2).getImm())
1307 .addReg(MI->getOperand(3).getReg()));
1310 case ARM::LEApcrelJT:
1311 case ARM::tLEApcrelJT:
1312 case ARM::t2LEApcrelJT: {
1313 MCSymbol *JTIPICSymbol =
1314 GetARMJTIPICJumpTableLabel2(MI->getOperand(1).getIndex(),
1315 MI->getOperand(2).getImm());
1316 OutStreamer.EmitInstruction(MCInstBuilder(MI->getOpcode() ==
1317 ARM::t2LEApcrelJT ? ARM::t2ADR
1318 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
1320 .addReg(MI->getOperand(0).getReg())
1321 .addExpr(MCSymbolRefExpr::Create(JTIPICSymbol, OutContext))
1322 // Add predicate operands.
1323 .addImm(MI->getOperand(3).getImm())
1324 .addReg(MI->getOperand(4).getReg()));
1327 // Darwin call instructions are just normal call instructions with different
1328 // clobber semantics (they clobber R9).
1329 case ARM::BX_CALL: {
1330 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr)
1333 // Add predicate operands.
1336 // Add 's' bit operand (always reg0 for this)
1339 OutStreamer.EmitInstruction(MCInstBuilder(ARM::BX)
1340 .addReg(MI->getOperand(0).getReg()));
1343 case ARM::tBX_CALL: {
1344 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr)
1347 // Add predicate operands.
1351 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tBX)
1352 .addReg(MI->getOperand(0).getReg())
1353 // Add predicate operands.
1358 case ARM::BMOVPCRX_CALL: {
1359 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr)
1362 // Add predicate operands.
1365 // Add 's' bit operand (always reg0 for this)
1368 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr)
1370 .addReg(MI->getOperand(0).getReg())
1371 // Add predicate operands.
1374 // Add 's' bit operand (always reg0 for this)
1378 case ARM::BMOVPCB_CALL: {
1379 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr)
1382 // Add predicate operands.
1385 // Add 's' bit operand (always reg0 for this)
1388 const GlobalValue *GV = MI->getOperand(0).getGlobal();
1389 MCSymbol *GVSym = Mang->getSymbol(GV);
1390 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1391 OutStreamer.EmitInstruction(MCInstBuilder(ARM::Bcc)
1393 // Add predicate operands.
1398 case ARM::MOVi16_ga_pcrel:
1399 case ARM::t2MOVi16_ga_pcrel: {
1401 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
1402 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1404 unsigned TF = MI->getOperand(1).getTargetFlags();
1405 bool isPIC = TF == ARMII::MO_LO16_NONLAZY_PIC;
1406 const GlobalValue *GV = MI->getOperand(1).getGlobal();
1407 MCSymbol *GVSym = GetARMGVSymbol(GV);
1408 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1410 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1411 getFunctionNumber(),
1412 MI->getOperand(2).getImm(), OutContext);
1413 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1414 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
1415 const MCExpr *PCRelExpr =
1416 ARMMCExpr::CreateLower16(MCBinaryExpr::CreateSub(GVSymExpr,
1417 MCBinaryExpr::CreateAdd(LabelSymExpr,
1418 MCConstantExpr::Create(PCAdj, OutContext),
1419 OutContext), OutContext), OutContext);
1420 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1422 const MCExpr *RefExpr= ARMMCExpr::CreateLower16(GVSymExpr, OutContext);
1423 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1426 // Add predicate operands.
1427 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1428 TmpInst.addOperand(MCOperand::CreateReg(0));
1429 // Add 's' bit operand (always reg0 for this)
1430 TmpInst.addOperand(MCOperand::CreateReg(0));
1431 OutStreamer.EmitInstruction(TmpInst);
1434 case ARM::MOVTi16_ga_pcrel:
1435 case ARM::t2MOVTi16_ga_pcrel: {
1437 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
1438 ? ARM::MOVTi16 : ARM::t2MOVTi16);
1439 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1440 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1442 unsigned TF = MI->getOperand(2).getTargetFlags();
1443 bool isPIC = TF == ARMII::MO_HI16_NONLAZY_PIC;
1444 const GlobalValue *GV = MI->getOperand(2).getGlobal();
1445 MCSymbol *GVSym = GetARMGVSymbol(GV);
1446 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1448 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1449 getFunctionNumber(),
1450 MI->getOperand(3).getImm(), OutContext);
1451 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1452 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
1453 const MCExpr *PCRelExpr =
1454 ARMMCExpr::CreateUpper16(MCBinaryExpr::CreateSub(GVSymExpr,
1455 MCBinaryExpr::CreateAdd(LabelSymExpr,
1456 MCConstantExpr::Create(PCAdj, OutContext),
1457 OutContext), OutContext), OutContext);
1458 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1460 const MCExpr *RefExpr= ARMMCExpr::CreateUpper16(GVSymExpr, OutContext);
1461 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1463 // Add predicate operands.
1464 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1465 TmpInst.addOperand(MCOperand::CreateReg(0));
1466 // Add 's' bit operand (always reg0 for this)
1467 TmpInst.addOperand(MCOperand::CreateReg(0));
1468 OutStreamer.EmitInstruction(TmpInst);
1471 case ARM::tPICADD: {
1472 // This is a pseudo op for a label + instruction sequence, which looks like:
1475 // This adds the address of LPC0 to r0.
1478 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1479 getFunctionNumber(), MI->getOperand(2).getImm(),
1482 // Form and emit the add.
1483 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tADDhirr)
1484 .addReg(MI->getOperand(0).getReg())
1485 .addReg(MI->getOperand(0).getReg())
1487 // Add predicate operands.
1493 // This is a pseudo op for a label + instruction sequence, which looks like:
1496 // This adds the address of LPC0 to r0.
1499 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1500 getFunctionNumber(), MI->getOperand(2).getImm(),
1503 // Form and emit the add.
1504 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDrr)
1505 .addReg(MI->getOperand(0).getReg())
1507 .addReg(MI->getOperand(1).getReg())
1508 // Add predicate operands.
1509 .addImm(MI->getOperand(3).getImm())
1510 .addReg(MI->getOperand(4).getReg())
1511 // Add 's' bit operand (always reg0 for this)
1522 case ARM::PICLDRSH: {
1523 // This is a pseudo op for a label + instruction sequence, which looks like:
1526 // The LCP0 label is referenced by a constant pool entry in order to get
1527 // a PC-relative address at the ldr instruction.
1530 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1531 getFunctionNumber(), MI->getOperand(2).getImm(),
1534 // Form and emit the load
1536 switch (MI->getOpcode()) {
1538 llvm_unreachable("Unexpected opcode!");
1539 case ARM::PICSTR: Opcode = ARM::STRrs; break;
1540 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
1541 case ARM::PICSTRH: Opcode = ARM::STRH; break;
1542 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
1543 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
1544 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
1545 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1546 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1548 OutStreamer.EmitInstruction(MCInstBuilder(Opcode)
1549 .addReg(MI->getOperand(0).getReg())
1551 .addReg(MI->getOperand(1).getReg())
1553 // Add predicate operands.
1554 .addImm(MI->getOperand(3).getImm())
1555 .addReg(MI->getOperand(4).getReg()));
1559 case ARM::CONSTPOOL_ENTRY: {
1560 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1561 /// in the function. The first operand is the ID# for this instruction, the
1562 /// second is the index into the MachineConstantPool that this is, the third
1563 /// is the size in bytes of this constant pool entry.
1564 /// The required alignment is specified on the basic block holding this MI.
1565 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1566 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
1568 // If this is the first entry of the pool, mark it.
1569 if (!InConstantPool) {
1570 OutStreamer.EmitDataRegion(MCDR_DataRegion);
1571 InConstantPool = true;
1574 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
1576 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1577 if (MCPE.isMachineConstantPoolEntry())
1578 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1580 EmitGlobalConstant(MCPE.Val.ConstVal);
1583 case ARM::t2BR_JT: {
1584 // Lower and emit the instruction itself, then the jump table following it.
1585 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr)
1587 .addReg(MI->getOperand(0).getReg())
1588 // Add predicate operands.
1592 // Output the data for the jump table itself
1596 case ARM::t2TBB_JT: {
1597 // Lower and emit the instruction itself, then the jump table following it.
1598 OutStreamer.EmitInstruction(MCInstBuilder(ARM::t2TBB)
1600 .addReg(MI->getOperand(0).getReg())
1601 // Add predicate operands.
1605 // Output the data for the jump table itself
1607 // Make sure the next instruction is 2-byte aligned.
1611 case ARM::t2TBH_JT: {
1612 // Lower and emit the instruction itself, then the jump table following it.
1613 OutStreamer.EmitInstruction(MCInstBuilder(ARM::t2TBH)
1615 .addReg(MI->getOperand(0).getReg())
1616 // Add predicate operands.
1620 // Output the data for the jump table itself
1626 // Lower and emit the instruction itself, then the jump table following it.
1629 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
1630 ARM::MOVr : ARM::tMOVr;
1631 TmpInst.setOpcode(Opc);
1632 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1633 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1634 // Add predicate operands.
1635 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1636 TmpInst.addOperand(MCOperand::CreateReg(0));
1637 // Add 's' bit operand (always reg0 for this)
1638 if (Opc == ARM::MOVr)
1639 TmpInst.addOperand(MCOperand::CreateReg(0));
1640 OutStreamer.EmitInstruction(TmpInst);
1642 // Make sure the Thumb jump table is 4-byte aligned.
1643 if (Opc == ARM::tMOVr)
1646 // Output the data for the jump table itself
1651 // Lower and emit the instruction itself, then the jump table following it.
1654 if (MI->getOperand(1).getReg() == 0) {
1656 TmpInst.setOpcode(ARM::LDRi12);
1657 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1658 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1659 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1661 TmpInst.setOpcode(ARM::LDRrs);
1662 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1663 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1664 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1665 TmpInst.addOperand(MCOperand::CreateImm(0));
1667 // Add predicate operands.
1668 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1669 TmpInst.addOperand(MCOperand::CreateReg(0));
1670 OutStreamer.EmitInstruction(TmpInst);
1672 // Output the data for the jump table itself
1676 case ARM::BR_JTadd: {
1677 // Lower and emit the instruction itself, then the jump table following it.
1678 // add pc, target, idx
1679 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDrr)
1681 .addReg(MI->getOperand(0).getReg())
1682 .addReg(MI->getOperand(1).getReg())
1683 // Add predicate operands.
1686 // Add 's' bit operand (always reg0 for this)
1689 // Output the data for the jump table itself
1694 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1695 // FIXME: Remove this special case when they do.
1696 if (!Subtarget->isTargetDarwin()) {
1697 //.long 0xe7ffdefe @ trap
1698 uint32_t Val = 0xe7ffdefeUL;
1699 OutStreamer.AddComment("trap");
1700 OutStreamer.EmitIntValue(Val, 4);
1705 case ARM::TRAPNaCl: {
1706 //.long 0xe7fedef0 @ trap
1707 uint32_t Val = 0xe7fedef0UL;
1708 OutStreamer.AddComment("trap");
1709 OutStreamer.EmitIntValue(Val, 4);
1713 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1714 // FIXME: Remove this special case when they do.
1715 if (!Subtarget->isTargetDarwin()) {
1716 //.short 57086 @ trap
1717 uint16_t Val = 0xdefe;
1718 OutStreamer.AddComment("trap");
1719 OutStreamer.EmitIntValue(Val, 2);
1724 case ARM::t2Int_eh_sjlj_setjmp:
1725 case ARM::t2Int_eh_sjlj_setjmp_nofp:
1726 case ARM::tInt_eh_sjlj_setjmp: {
1727 // Two incoming args: GPR:$src, GPR:$val
1730 // str $val, [$src, #4]
1735 unsigned SrcReg = MI->getOperand(0).getReg();
1736 unsigned ValReg = MI->getOperand(1).getReg();
1737 MCSymbol *Label = GetARMSJLJEHLabel();
1738 OutStreamer.AddComment("eh_setjmp begin");
1739 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr)
1746 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tADDi3)
1756 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tSTRi)
1759 // The offset immediate is #4. The operand value is scaled by 4 for the
1760 // tSTR instruction.
1766 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVi8)
1774 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1775 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tB)
1776 .addExpr(SymbolExpr)
1780 OutStreamer.AddComment("eh_setjmp end");
1781 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVi8)
1789 OutStreamer.EmitLabel(Label);
1793 case ARM::Int_eh_sjlj_setjmp_nofp:
1794 case ARM::Int_eh_sjlj_setjmp: {
1795 // Two incoming args: GPR:$src, GPR:$val
1797 // str $val, [$src, #+4]
1801 unsigned SrcReg = MI->getOperand(0).getReg();
1802 unsigned ValReg = MI->getOperand(1).getReg();
1804 OutStreamer.AddComment("eh_setjmp begin");
1805 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDri)
1812 // 's' bit operand (always reg0 for this).
1815 OutStreamer.EmitInstruction(MCInstBuilder(ARM::STRi12)
1823 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVi)
1829 // 's' bit operand (always reg0 for this).
1832 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDri)
1839 // 's' bit operand (always reg0 for this).
1842 OutStreamer.AddComment("eh_setjmp end");
1843 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVi)
1849 // 's' bit operand (always reg0 for this).
1853 case ARM::Int_eh_sjlj_longjmp: {
1854 // ldr sp, [$src, #8]
1855 // ldr $scratch, [$src, #4]
1858 unsigned SrcReg = MI->getOperand(0).getReg();
1859 unsigned ScratchReg = MI->getOperand(1).getReg();
1860 OutStreamer.EmitInstruction(MCInstBuilder(ARM::LDRi12)
1868 OutStreamer.EmitInstruction(MCInstBuilder(ARM::LDRi12)
1876 OutStreamer.EmitInstruction(MCInstBuilder(ARM::LDRi12)
1884 OutStreamer.EmitInstruction(MCInstBuilder(ARM::BX)
1891 case ARM::tInt_eh_sjlj_longjmp: {
1892 // ldr $scratch, [$src, #8]
1894 // ldr $scratch, [$src, #4]
1897 unsigned SrcReg = MI->getOperand(0).getReg();
1898 unsigned ScratchReg = MI->getOperand(1).getReg();
1899 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tLDRi)
1902 // The offset immediate is #8. The operand value is scaled by 4 for the
1903 // tLDR instruction.
1909 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr)
1916 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tLDRi)
1924 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tLDRi)
1932 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tBX)
1942 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
1944 OutStreamer.EmitInstruction(TmpInst);
1947 //===----------------------------------------------------------------------===//
1948 // Target Registry Stuff
1949 //===----------------------------------------------------------------------===//
1951 // Force static initialization.
1952 extern "C" void LLVMInitializeARMAsmPrinter() {
1953 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1954 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);