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 #include "ARMAsmPrinter.h"
17 #include "ARMConstantPoolValue.h"
18 #include "ARMFPUName.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMTargetMachine.h"
21 #include "ARMTargetObjectFile.h"
22 #include "InstPrinter/ARMInstPrinter.h"
23 #include "MCTargetDesc/ARMAddressingModes.h"
24 #include "MCTargetDesc/ARMMCExpr.h"
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineJumpTableInfo.h"
29 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DataLayout.h"
32 #include "llvm/IR/DebugInfo.h"
33 #include "llvm/IR/Mangler.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/IR/Type.h"
36 #include "llvm/MC/MCAsmInfo.h"
37 #include "llvm/MC/MCAssembler.h"
38 #include "llvm/MC/MCContext.h"
39 #include "llvm/MC/MCELFStreamer.h"
40 #include "llvm/MC/MCInst.h"
41 #include "llvm/MC/MCInstBuilder.h"
42 #include "llvm/MC/MCObjectStreamer.h"
43 #include "llvm/MC/MCSectionMachO.h"
44 #include "llvm/MC/MCStreamer.h"
45 #include "llvm/MC/MCSymbol.h"
46 #include "llvm/Support/ARMBuildAttributes.h"
47 #include "llvm/Support/COFF.h"
48 #include "llvm/Support/CommandLine.h"
49 #include "llvm/Support/Debug.h"
50 #include "llvm/Support/ELF.h"
51 #include "llvm/Support/ErrorHandling.h"
52 #include "llvm/Support/TargetRegistry.h"
53 #include "llvm/Support/raw_ostream.h"
54 #include "llvm/Target/TargetMachine.h"
58 #define DEBUG_TYPE "asm-printer"
60 void ARMAsmPrinter::EmitFunctionBodyEnd() {
61 // Make sure to terminate any constant pools that were at the end
65 InConstantPool = false;
66 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
69 void ARMAsmPrinter::EmitFunctionEntryLabel() {
70 if (AFI->isThumbFunction()) {
71 OutStreamer.EmitAssemblerFlag(MCAF_Code16);
72 OutStreamer.EmitThumbFunc(CurrentFnSym);
75 OutStreamer.EmitLabel(CurrentFnSym);
78 void ARMAsmPrinter::EmitXXStructor(const Constant *CV) {
79 uint64_t Size = TM.getDataLayout()->getTypeAllocSize(CV->getType());
80 assert(Size && "C++ constructor pointer had zero size!");
82 const GlobalValue *GV = dyn_cast<GlobalValue>(CV->stripPointerCasts());
83 assert(GV && "C++ constructor pointer was not a GlobalValue!");
85 const MCExpr *E = MCSymbolRefExpr::Create(GetARMGVSymbol(GV,
87 (Subtarget->isTargetELF()
88 ? MCSymbolRefExpr::VK_ARM_TARGET1
89 : MCSymbolRefExpr::VK_None),
92 OutStreamer.EmitValue(E, Size);
95 /// runOnMachineFunction - This uses the EmitInstruction()
96 /// method to print assembly for each instruction.
98 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
99 AFI = MF.getInfo<ARMFunctionInfo>();
100 MCP = MF.getConstantPool();
102 SetupMachineFunction(MF);
104 if (Subtarget->isTargetCOFF()) {
105 bool Internal = MF.getFunction()->hasInternalLinkage();
106 COFF::SymbolStorageClass Scl = Internal ? COFF::IMAGE_SYM_CLASS_STATIC
107 : COFF::IMAGE_SYM_CLASS_EXTERNAL;
108 int Type = COFF::IMAGE_SYM_DTYPE_FUNCTION << COFF::SCT_COMPLEX_TYPE_SHIFT;
110 OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
111 OutStreamer.EmitCOFFSymbolStorageClass(Scl);
112 OutStreamer.EmitCOFFSymbolType(Type);
113 OutStreamer.EndCOFFSymbolDef();
116 // Have common code print out the function header with linkage info etc.
117 EmitFunctionHeader();
119 // Emit the rest of the function body.
122 // We didn't modify anything.
126 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
127 raw_ostream &O, const char *Modifier) {
128 const MachineOperand &MO = MI->getOperand(OpNum);
129 unsigned TF = MO.getTargetFlags();
131 switch (MO.getType()) {
132 default: llvm_unreachable("<unknown operand type>");
133 case MachineOperand::MO_Register: {
134 unsigned Reg = MO.getReg();
135 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
136 assert(!MO.getSubReg() && "Subregs should be eliminated!");
137 if(ARM::GPRPairRegClass.contains(Reg)) {
138 const MachineFunction &MF = *MI->getParent()->getParent();
139 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
140 Reg = TRI->getSubReg(Reg, ARM::gsub_0);
142 O << ARMInstPrinter::getRegisterName(Reg);
145 case MachineOperand::MO_Immediate: {
146 int64_t Imm = MO.getImm();
148 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
149 (TF == ARMII::MO_LO16))
151 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
152 (TF == ARMII::MO_HI16))
157 case MachineOperand::MO_MachineBasicBlock:
158 O << *MO.getMBB()->getSymbol();
160 case MachineOperand::MO_GlobalAddress: {
161 const GlobalValue *GV = MO.getGlobal();
162 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
163 (TF & ARMII::MO_LO16))
165 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
166 (TF & ARMII::MO_HI16))
168 O << *GetARMGVSymbol(GV, TF);
170 printOffset(MO.getOffset(), O);
171 if (TF == ARMII::MO_PLT)
175 case MachineOperand::MO_ConstantPoolIndex:
176 O << *GetCPISymbol(MO.getIndex());
181 //===--------------------------------------------------------------------===//
183 MCSymbol *ARMAsmPrinter::
184 GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
185 const DataLayout *DL = TM.getDataLayout();
186 SmallString<60> Name;
187 raw_svector_ostream(Name) << DL->getPrivateGlobalPrefix() << "JTI"
188 << getFunctionNumber() << '_' << uid << '_' << uid2;
189 return OutContext.GetOrCreateSymbol(Name.str());
193 MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel() const {
194 const DataLayout *DL = TM.getDataLayout();
195 SmallString<60> Name;
196 raw_svector_ostream(Name) << DL->getPrivateGlobalPrefix() << "SJLJEH"
197 << getFunctionNumber();
198 return OutContext.GetOrCreateSymbol(Name.str());
201 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
202 unsigned AsmVariant, const char *ExtraCode,
204 // Does this asm operand have a single letter operand modifier?
205 if (ExtraCode && ExtraCode[0]) {
206 if (ExtraCode[1] != 0) return true; // Unknown modifier.
208 switch (ExtraCode[0]) {
210 // See if this is a generic print operand
211 return AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O);
212 case 'a': // Print as a memory address.
213 if (MI->getOperand(OpNum).isReg()) {
215 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
220 case 'c': // Don't print "#" before an immediate operand.
221 if (!MI->getOperand(OpNum).isImm())
223 O << MI->getOperand(OpNum).getImm();
225 case 'P': // Print a VFP double precision register.
226 case 'q': // Print a NEON quad precision register.
227 printOperand(MI, OpNum, O);
229 case 'y': // Print a VFP single precision register as indexed double.
230 if (MI->getOperand(OpNum).isReg()) {
231 unsigned Reg = MI->getOperand(OpNum).getReg();
232 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
233 // Find the 'd' register that has this 's' register as a sub-register,
234 // and determine the lane number.
235 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) {
236 if (!ARM::DPRRegClass.contains(*SR))
238 bool Lane0 = TRI->getSubReg(*SR, ARM::ssub_0) == Reg;
239 O << ARMInstPrinter::getRegisterName(*SR) << (Lane0 ? "[0]" : "[1]");
244 case 'B': // Bitwise inverse of integer or symbol without a preceding #.
245 if (!MI->getOperand(OpNum).isImm())
247 O << ~(MI->getOperand(OpNum).getImm());
249 case 'L': // The low 16 bits of an immediate constant.
250 if (!MI->getOperand(OpNum).isImm())
252 O << (MI->getOperand(OpNum).getImm() & 0xffff);
254 case 'M': { // A register range suitable for LDM/STM.
255 if (!MI->getOperand(OpNum).isReg())
257 const MachineOperand &MO = MI->getOperand(OpNum);
258 unsigned RegBegin = MO.getReg();
259 // This takes advantage of the 2 operand-ness of ldm/stm and that we've
260 // already got the operands in registers that are operands to the
261 // inline asm statement.
263 if (ARM::GPRPairRegClass.contains(RegBegin)) {
264 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
265 unsigned Reg0 = TRI->getSubReg(RegBegin, ARM::gsub_0);
266 O << ARMInstPrinter::getRegisterName(Reg0) << ", ";
267 RegBegin = TRI->getSubReg(RegBegin, ARM::gsub_1);
269 O << ARMInstPrinter::getRegisterName(RegBegin);
271 // FIXME: The register allocator not only may not have given us the
272 // registers in sequence, but may not be in ascending registers. This
273 // will require changes in the register allocator that'll need to be
274 // propagated down here if the operands change.
275 unsigned RegOps = OpNum + 1;
276 while (MI->getOperand(RegOps).isReg()) {
278 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
286 case 'R': // The most significant register of a pair.
287 case 'Q': { // The least significant register of a pair.
290 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
291 if (!FlagsOP.isImm())
293 unsigned Flags = FlagsOP.getImm();
295 // This operand may not be the one that actually provides the register. If
296 // it's tied to a previous one then we should refer instead to that one
297 // for registers and their classes.
299 if (InlineAsm::isUseOperandTiedToDef(Flags, TiedIdx)) {
300 for (OpNum = InlineAsm::MIOp_FirstOperand; TiedIdx; --TiedIdx) {
301 unsigned OpFlags = MI->getOperand(OpNum).getImm();
302 OpNum += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
304 Flags = MI->getOperand(OpNum).getImm();
306 // Later code expects OpNum to be pointing at the register rather than
311 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
313 InlineAsm::hasRegClassConstraint(Flags, RC);
314 if (RC == ARM::GPRPairRegClassID) {
317 const MachineOperand &MO = MI->getOperand(OpNum);
320 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
321 unsigned Reg = TRI->getSubReg(MO.getReg(), ExtraCode[0] == 'Q' ?
322 ARM::gsub_0 : ARM::gsub_1);
323 O << ARMInstPrinter::getRegisterName(Reg);
328 unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1;
329 if (RegOp >= MI->getNumOperands())
331 const MachineOperand &MO = MI->getOperand(RegOp);
334 unsigned Reg = MO.getReg();
335 O << ARMInstPrinter::getRegisterName(Reg);
339 case 'e': // The low doubleword register of a NEON quad register.
340 case 'f': { // The high doubleword register of a NEON quad register.
341 if (!MI->getOperand(OpNum).isReg())
343 unsigned Reg = MI->getOperand(OpNum).getReg();
344 if (!ARM::QPRRegClass.contains(Reg))
346 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
347 unsigned SubReg = TRI->getSubReg(Reg, ExtraCode[0] == 'e' ?
348 ARM::dsub_0 : ARM::dsub_1);
349 O << ARMInstPrinter::getRegisterName(SubReg);
353 // This modifier is not yet supported.
354 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
356 case 'H': { // The highest-numbered register of a pair.
357 const MachineOperand &MO = MI->getOperand(OpNum);
360 const MachineFunction &MF = *MI->getParent()->getParent();
361 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
362 unsigned Reg = MO.getReg();
363 if(!ARM::GPRPairRegClass.contains(Reg))
365 Reg = TRI->getSubReg(Reg, ARM::gsub_1);
366 O << ARMInstPrinter::getRegisterName(Reg);
372 printOperand(MI, OpNum, O);
376 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
377 unsigned OpNum, unsigned AsmVariant,
378 const char *ExtraCode,
380 // Does this asm operand have a single letter operand modifier?
381 if (ExtraCode && ExtraCode[0]) {
382 if (ExtraCode[1] != 0) return true; // Unknown modifier.
384 switch (ExtraCode[0]) {
385 case 'A': // A memory operand for a VLD1/VST1 instruction.
386 default: return true; // Unknown modifier.
387 case 'm': // The base register of a memory operand.
388 if (!MI->getOperand(OpNum).isReg())
390 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
395 const MachineOperand &MO = MI->getOperand(OpNum);
396 assert(MO.isReg() && "unexpected inline asm memory operand");
397 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
401 static bool isThumb(const MCSubtargetInfo& STI) {
402 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
405 void ARMAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
406 const MCSubtargetInfo *EndInfo) const {
407 // If either end mode is unknown (EndInfo == NULL) or different than
408 // the start mode, then restore the start mode.
409 const bool WasThumb = isThumb(StartInfo);
410 if (!EndInfo || WasThumb != isThumb(*EndInfo)) {
411 OutStreamer.EmitAssemblerFlag(WasThumb ? MCAF_Code16 : MCAF_Code32);
415 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
416 if (Subtarget->isTargetMachO()) {
417 Reloc::Model RelocM = TM.getRelocationModel();
418 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
419 // Declare all the text sections up front (before the DWARF sections
420 // emitted by AsmPrinter::doInitialization) so the assembler will keep
421 // them together at the beginning of the object file. This helps
422 // avoid out-of-range branches that are due a fundamental limitation of
423 // the way symbol offsets are encoded with the current Darwin ARM
425 const TargetLoweringObjectFileMachO &TLOFMacho =
426 static_cast<const TargetLoweringObjectFileMachO &>(
427 getObjFileLowering());
429 // Collect the set of sections our functions will go into.
430 SetVector<const MCSection *, SmallVector<const MCSection *, 8>,
431 SmallPtrSet<const MCSection *, 8> > TextSections;
432 // Default text section comes first.
433 TextSections.insert(TLOFMacho.getTextSection());
434 // Now any user defined text sections from function attributes.
435 for (Module::iterator F = M.begin(), e = M.end(); F != e; ++F)
436 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage())
437 TextSections.insert(TLOFMacho.SectionForGlobal(F, *Mang, TM));
438 // Now the coalescable sections.
439 TextSections.insert(TLOFMacho.getTextCoalSection());
440 TextSections.insert(TLOFMacho.getConstTextCoalSection());
442 // Emit the sections in the .s file header to fix the order.
443 for (unsigned i = 0, e = TextSections.size(); i != e; ++i)
444 OutStreamer.SwitchSection(TextSections[i]);
446 if (RelocM == Reloc::DynamicNoPIC) {
447 const MCSection *sect =
448 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
449 MachO::S_SYMBOL_STUBS,
450 12, SectionKind::getText());
451 OutStreamer.SwitchSection(sect);
453 const MCSection *sect =
454 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
455 MachO::S_SYMBOL_STUBS,
456 16, SectionKind::getText());
457 OutStreamer.SwitchSection(sect);
459 const MCSection *StaticInitSect =
460 OutContext.getMachOSection("__TEXT", "__StaticInit",
462 MachO::S_ATTR_PURE_INSTRUCTIONS,
463 SectionKind::getText());
464 OutStreamer.SwitchSection(StaticInitSect);
467 // Compiling with debug info should not affect the code
468 // generation. Ensure the cstring section comes before the
469 // optional __DWARF secion. Otherwise, PC-relative loads would
470 // have to use different instruction sequences at "-g" in order to
471 // reach global data in the same object file.
472 OutStreamer.SwitchSection(getObjFileLowering().getCStringSection());
475 // Use unified assembler syntax.
476 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
478 // Emit ARM Build Attributes
479 if (Subtarget->isTargetELF())
482 if (!M.getModuleInlineAsm().empty() && Subtarget->isThumb())
483 OutStreamer.EmitAssemblerFlag(MCAF_Code16);
487 emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel,
488 MachineModuleInfoImpl::StubValueTy &MCSym) {
490 OutStreamer.EmitLabel(StubLabel);
491 // .indirect_symbol _foo
492 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
495 // External to current translation unit.
496 OutStreamer.EmitIntValue(0, 4/*size*/);
498 // Internal to current translation unit.
500 // When we place the LSDA into the TEXT section, the type info
501 // pointers need to be indirect and pc-rel. We accomplish this by
502 // using NLPs; however, sometimes the types are local to the file.
503 // We need to fill in the value for the NLP in those cases.
504 OutStreamer.EmitValue(
505 MCSymbolRefExpr::Create(MCSym.getPointer(), OutStreamer.getContext()),
510 void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
511 if (Subtarget->isTargetMachO()) {
512 // All darwin targets use mach-o.
513 const TargetLoweringObjectFileMachO &TLOFMacho =
514 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
515 MachineModuleInfoMachO &MMIMacho =
516 MMI->getObjFileInfo<MachineModuleInfoMachO>();
518 // Output non-lazy-pointers for external and common global variables.
519 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
521 if (!Stubs.empty()) {
522 // Switch with ".non_lazy_symbol_pointer" directive.
523 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
526 for (auto &Stub : Stubs)
527 emitNonLazySymbolPointer(OutStreamer, Stub.first, Stub.second);
530 OutStreamer.AddBlankLine();
533 Stubs = MMIMacho.GetHiddenGVStubList();
534 if (!Stubs.empty()) {
535 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
538 for (auto &Stub : Stubs)
539 emitNonLazySymbolPointer(OutStreamer, Stub.first, Stub.second);
542 OutStreamer.AddBlankLine();
545 // Funny Darwin hack: This flag tells the linker that no global symbols
546 // contain code that falls through to other global symbols (e.g. the obvious
547 // implementation of multiple entry points). If this doesn't occur, the
548 // linker can safely perform dead code stripping. Since LLVM never
549 // generates code that does this, it is always safe to set.
550 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
553 // Emit a .data.rel section containing any stubs that were created.
554 if (Subtarget->isTargetELF()) {
555 const TargetLoweringObjectFileELF &TLOFELF =
556 static_cast<const TargetLoweringObjectFileELF &>(getObjFileLowering());
558 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
560 // Output stubs for external and common global variables.
561 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
562 if (!Stubs.empty()) {
563 OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
564 const DataLayout *TD = TM.getDataLayout();
566 for (auto &stub: Stubs) {
567 OutStreamer.EmitLabel(stub.first);
568 OutStreamer.EmitSymbolValue(stub.second.getPointer(),
569 TD->getPointerSize(0));
576 //===----------------------------------------------------------------------===//
577 // Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
579 // The following seem like one-off assembler flags, but they actually need
580 // to appear in the .ARM.attributes section in ELF.
581 // Instead of subclassing the MCELFStreamer, we do the work here.
583 static ARMBuildAttrs::CPUArch getArchForCPU(StringRef CPU,
584 const ARMSubtarget *Subtarget) {
586 return ARMBuildAttrs::v5TEJ;
588 if (Subtarget->hasV8Ops())
589 return ARMBuildAttrs::v8;
590 else if (Subtarget->hasV7Ops()) {
591 if (Subtarget->isMClass() && Subtarget->hasThumb2DSP())
592 return ARMBuildAttrs::v7E_M;
593 return ARMBuildAttrs::v7;
594 } else if (Subtarget->hasV6T2Ops())
595 return ARMBuildAttrs::v6T2;
596 else if (Subtarget->hasV6MOps())
597 return ARMBuildAttrs::v6S_M;
598 else if (Subtarget->hasV6Ops())
599 return ARMBuildAttrs::v6;
600 else if (Subtarget->hasV5TEOps())
601 return ARMBuildAttrs::v5TE;
602 else if (Subtarget->hasV5TOps())
603 return ARMBuildAttrs::v5T;
604 else if (Subtarget->hasV4TOps())
605 return ARMBuildAttrs::v4T;
607 return ARMBuildAttrs::v4;
610 void ARMAsmPrinter::emitAttributes() {
611 MCTargetStreamer &TS = *OutStreamer.getTargetStreamer();
612 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
614 ATS.switchVendor("aeabi");
616 std::string CPUString = Subtarget->getCPUString();
618 // FIXME: remove krait check when GNU tools support krait cpu
619 if (CPUString != "generic" && CPUString != "krait")
620 ATS.emitTextAttribute(ARMBuildAttrs::CPU_name, CPUString);
622 ATS.emitAttribute(ARMBuildAttrs::CPU_arch,
623 getArchForCPU(CPUString, Subtarget));
625 // Tag_CPU_arch_profile must have the default value of 0 when "Architecture
626 // profile is not applicable (e.g. pre v7, or cross-profile code)".
627 if (Subtarget->hasV7Ops()) {
628 if (Subtarget->isAClass()) {
629 ATS.emitAttribute(ARMBuildAttrs::CPU_arch_profile,
630 ARMBuildAttrs::ApplicationProfile);
631 } else if (Subtarget->isRClass()) {
632 ATS.emitAttribute(ARMBuildAttrs::CPU_arch_profile,
633 ARMBuildAttrs::RealTimeProfile);
634 } else if (Subtarget->isMClass()) {
635 ATS.emitAttribute(ARMBuildAttrs::CPU_arch_profile,
636 ARMBuildAttrs::MicroControllerProfile);
640 ATS.emitAttribute(ARMBuildAttrs::ARM_ISA_use, Subtarget->hasARMOps() ?
641 ARMBuildAttrs::Allowed : ARMBuildAttrs::Not_Allowed);
642 if (Subtarget->isThumb1Only()) {
643 ATS.emitAttribute(ARMBuildAttrs::THUMB_ISA_use,
644 ARMBuildAttrs::Allowed);
645 } else if (Subtarget->hasThumb2()) {
646 ATS.emitAttribute(ARMBuildAttrs::THUMB_ISA_use,
647 ARMBuildAttrs::AllowThumb32);
650 if (Subtarget->hasNEON()) {
651 /* NEON is not exactly a VFP architecture, but GAS emit one of
652 * neon/neon-fp-armv8/neon-vfpv4/vfpv3/vfpv2 for .fpu parameters */
653 if (Subtarget->hasFPARMv8()) {
654 if (Subtarget->hasCrypto())
655 ATS.emitFPU(ARM::CRYPTO_NEON_FP_ARMV8);
657 ATS.emitFPU(ARM::NEON_FP_ARMV8);
659 else if (Subtarget->hasVFP4())
660 ATS.emitFPU(ARM::NEON_VFPV4);
662 ATS.emitFPU(ARM::NEON);
663 // Emit Tag_Advanced_SIMD_arch for ARMv8 architecture
664 if (Subtarget->hasV8Ops())
665 ATS.emitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
666 ARMBuildAttrs::AllowNeonARMv8);
668 if (Subtarget->hasFPARMv8())
669 ATS.emitFPU(ARM::FP_ARMV8);
670 else if (Subtarget->hasVFP4())
671 ATS.emitFPU(Subtarget->hasD16() ? ARM::VFPV4_D16 : ARM::VFPV4);
672 else if (Subtarget->hasVFP3())
673 ATS.emitFPU(Subtarget->hasD16() ? ARM::VFPV3_D16 : ARM::VFPV3);
674 else if (Subtarget->hasVFP2())
675 ATS.emitFPU(ARM::VFPV2);
678 if (TM.getRelocationModel() == Reloc::PIC_) {
679 // PIC specific attributes.
680 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RW_data,
681 ARMBuildAttrs::AddressRWPCRel);
682 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RO_data,
683 ARMBuildAttrs::AddressROPCRel);
684 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use,
685 ARMBuildAttrs::AddressGOT);
687 // Allow direct addressing of imported data for all other relocation models.
688 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use,
689 ARMBuildAttrs::AddressDirect);
692 // Signal various FP modes.
693 if (!TM.Options.UnsafeFPMath) {
694 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal, ARMBuildAttrs::Allowed);
695 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
696 ARMBuildAttrs::Allowed);
699 if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath)
700 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
701 ARMBuildAttrs::Allowed);
703 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
704 ARMBuildAttrs::AllowIEE754);
706 // FIXME: add more flags to ARMBuildAttributes.h
707 // 8-bytes alignment stuff.
708 ATS.emitAttribute(ARMBuildAttrs::ABI_align_needed, 1);
709 ATS.emitAttribute(ARMBuildAttrs::ABI_align_preserved, 1);
711 // ABI_HardFP_use attribute to indicate single precision FP.
712 if (Subtarget->isFPOnlySP())
713 ATS.emitAttribute(ARMBuildAttrs::ABI_HardFP_use,
714 ARMBuildAttrs::HardFPSinglePrecision);
716 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
717 if (Subtarget->isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard)
718 ATS.emitAttribute(ARMBuildAttrs::ABI_VFP_args, ARMBuildAttrs::HardFPAAPCS);
720 // FIXME: Should we signal R9 usage?
722 if (Subtarget->hasFP16())
723 ATS.emitAttribute(ARMBuildAttrs::FP_HP_extension, ARMBuildAttrs::AllowHPFP);
725 if (Subtarget->hasMPExtension())
726 ATS.emitAttribute(ARMBuildAttrs::MPextension_use, ARMBuildAttrs::AllowMP);
728 // Hardware divide in ARM mode is part of base arch, starting from ARMv8.
729 // If only Thumb hwdiv is present, it must also be in base arch (ARMv7-R/M).
730 // It is not possible to produce DisallowDIV: if hwdiv is present in the base
731 // arch, supplying -hwdiv downgrades the effective arch, via ClearImpliedBits.
732 // AllowDIVExt is only emitted if hwdiv isn't available in the base arch;
733 // otherwise, the default value (AllowDIVIfExists) applies.
734 if (Subtarget->hasDivideInARMMode() && !Subtarget->hasV8Ops())
735 ATS.emitAttribute(ARMBuildAttrs::DIV_use, ARMBuildAttrs::AllowDIVExt);
738 if (const Module *SourceModule = MMI->getModule()) {
739 // ABI_PCS_wchar_t to indicate wchar_t width
740 // FIXME: There is no way to emit value 0 (wchar_t prohibited).
741 if (auto WCharWidthValue = cast_or_null<ConstantInt>(
742 SourceModule->getModuleFlag("wchar_size"))) {
743 int WCharWidth = WCharWidthValue->getZExtValue();
744 assert((WCharWidth == 2 || WCharWidth == 4) &&
745 "wchar_t width must be 2 or 4 bytes");
746 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_wchar_t, WCharWidth);
749 // ABI_enum_size to indicate enum width
750 // FIXME: There is no way to emit value 0 (enums prohibited) or value 3
751 // (all enums contain a value needing 32 bits to encode).
752 if (auto EnumWidthValue = cast_or_null<ConstantInt>(
753 SourceModule->getModuleFlag("min_enum_size"))) {
754 int EnumWidth = EnumWidthValue->getZExtValue();
755 assert((EnumWidth == 1 || EnumWidth == 4) &&
756 "Minimum enum width must be 1 or 4 bytes");
757 int EnumBuildAttr = EnumWidth == 1 ? 1 : 2;
758 ATS.emitAttribute(ARMBuildAttrs::ABI_enum_size, EnumBuildAttr);
763 if (Subtarget->hasTrustZone() && Subtarget->hasVirtualization())
764 ATS.emitAttribute(ARMBuildAttrs::Virtualization_use,
765 ARMBuildAttrs::AllowTZVirtualization);
766 else if (Subtarget->hasTrustZone())
767 ATS.emitAttribute(ARMBuildAttrs::Virtualization_use,
768 ARMBuildAttrs::AllowTZ);
769 else if (Subtarget->hasVirtualization())
770 ATS.emitAttribute(ARMBuildAttrs::Virtualization_use,
771 ARMBuildAttrs::AllowVirtualization);
773 ATS.finishAttributeSection();
776 //===----------------------------------------------------------------------===//
778 static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
779 unsigned LabelId, MCContext &Ctx) {
781 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
782 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
786 static MCSymbolRefExpr::VariantKind
787 getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
789 case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None;
790 case ARMCP::TLSGD: return MCSymbolRefExpr::VK_TLSGD;
791 case ARMCP::TPOFF: return MCSymbolRefExpr::VK_TPOFF;
792 case ARMCP::GOTTPOFF: return MCSymbolRefExpr::VK_GOTTPOFF;
793 case ARMCP::GOT: return MCSymbolRefExpr::VK_GOT;
794 case ARMCP::GOTOFF: return MCSymbolRefExpr::VK_GOTOFF;
796 llvm_unreachable("Invalid ARMCPModifier!");
799 MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV,
800 unsigned char TargetFlags) {
801 if (Subtarget->isTargetMachO()) {
802 bool IsIndirect = (TargetFlags & ARMII::MO_NONLAZY) &&
803 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
806 return getSymbol(GV);
808 // FIXME: Remove this when Darwin transition to @GOT like syntax.
809 MCSymbol *MCSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
810 MachineModuleInfoMachO &MMIMachO =
811 MMI->getObjFileInfo<MachineModuleInfoMachO>();
812 MachineModuleInfoImpl::StubValueTy &StubSym =
813 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym)
814 : MMIMachO.getGVStubEntry(MCSym);
815 if (!StubSym.getPointer())
816 StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV),
817 !GV->hasInternalLinkage());
819 } else if (Subtarget->isTargetCOFF()) {
820 assert(Subtarget->isTargetWindows() &&
821 "Windows is the only supported COFF target");
823 bool IsIndirect = (TargetFlags & ARMII::MO_DLLIMPORT);
825 return getSymbol(GV);
827 SmallString<128> Name;
829 getNameWithPrefix(Name, GV);
831 return OutContext.GetOrCreateSymbol(Name);
832 } else if (Subtarget->isTargetELF()) {
833 return getSymbol(GV);
835 llvm_unreachable("unexpected target");
839 EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
840 const DataLayout *DL = TM.getDataLayout();
841 int Size = TM.getDataLayout()->getTypeAllocSize(MCPV->getType());
843 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
846 if (ACPV->isLSDA()) {
847 SmallString<128> Str;
848 raw_svector_ostream OS(Str);
849 OS << DL->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
850 MCSym = OutContext.GetOrCreateSymbol(OS.str());
851 } else if (ACPV->isBlockAddress()) {
852 const BlockAddress *BA =
853 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
854 MCSym = GetBlockAddressSymbol(BA);
855 } else if (ACPV->isGlobalValue()) {
856 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
858 // On Darwin, const-pool entries may get the "FOO$non_lazy_ptr" mangling, so
859 // flag the global as MO_NONLAZY.
860 unsigned char TF = Subtarget->isTargetMachO() ? ARMII::MO_NONLAZY : 0;
861 MCSym = GetARMGVSymbol(GV, TF);
862 } else if (ACPV->isMachineBasicBlock()) {
863 const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
864 MCSym = MBB->getSymbol();
866 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
867 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
868 MCSym = GetExternalSymbolSymbol(Sym);
871 // Create an MCSymbol for the reference.
873 MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()),
876 if (ACPV->getPCAdjustment()) {
877 MCSymbol *PCLabel = getPICLabel(DL->getPrivateGlobalPrefix(),
881 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
883 MCBinaryExpr::CreateAdd(PCRelExpr,
884 MCConstantExpr::Create(ACPV->getPCAdjustment(),
887 if (ACPV->mustAddCurrentAddress()) {
888 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
889 // label, so just emit a local label end reference that instead.
890 MCSymbol *DotSym = OutContext.CreateTempSymbol();
891 OutStreamer.EmitLabel(DotSym);
892 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
893 PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext);
895 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
897 OutStreamer.EmitValue(Expr, Size);
900 void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
901 unsigned Opcode = MI->getOpcode();
903 if (Opcode == ARM::BR_JTadd)
905 else if (Opcode == ARM::BR_JTm)
908 const MachineOperand &MO1 = MI->getOperand(OpNum);
909 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
910 unsigned JTI = MO1.getIndex();
912 // Emit a label for the jump table.
913 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
914 OutStreamer.EmitLabel(JTISymbol);
916 // Mark the jump table as data-in-code.
917 OutStreamer.EmitDataRegion(MCDR_DataRegionJT32);
919 // Emit each entry of the table.
920 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
921 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
922 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
924 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
925 MachineBasicBlock *MBB = JTBBs[i];
926 // Construct an MCExpr for the entry. We want a value of the form:
927 // (BasicBlockAddr - TableBeginAddr)
929 // For example, a table with entries jumping to basic blocks BB0 and BB1
932 // .word (LBB0 - LJTI_0_0)
933 // .word (LBB1 - LJTI_0_0)
934 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
936 if (TM.getRelocationModel() == Reloc::PIC_)
937 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
940 // If we're generating a table of Thumb addresses in static relocation
941 // model, we need to add one to keep interworking correctly.
942 else if (AFI->isThumbFunction())
943 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(1,OutContext),
945 OutStreamer.EmitValue(Expr, 4);
947 // Mark the end of jump table data-in-code region.
948 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
951 void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
952 unsigned Opcode = MI->getOpcode();
953 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
954 const MachineOperand &MO1 = MI->getOperand(OpNum);
955 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
956 unsigned JTI = MO1.getIndex();
958 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
959 OutStreamer.EmitLabel(JTISymbol);
961 // Emit each entry of the table.
962 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
963 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
964 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
965 unsigned OffsetWidth = 4;
966 if (MI->getOpcode() == ARM::t2TBB_JT) {
968 // Mark the jump table as data-in-code.
969 OutStreamer.EmitDataRegion(MCDR_DataRegionJT8);
970 } else if (MI->getOpcode() == ARM::t2TBH_JT) {
972 // Mark the jump table as data-in-code.
973 OutStreamer.EmitDataRegion(MCDR_DataRegionJT16);
976 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
977 MachineBasicBlock *MBB = JTBBs[i];
978 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
980 // If this isn't a TBB or TBH, the entries are direct branch instructions.
981 if (OffsetWidth == 4) {
982 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::t2B)
983 .addExpr(MBBSymbolExpr)
988 // Otherwise it's an offset from the dispatch instruction. Construct an
989 // MCExpr for the entry. We want a value of the form:
990 // (BasicBlockAddr - TableBeginAddr) / 2
992 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
995 // .byte (LBB0 - LJTI_0_0) / 2
996 // .byte (LBB1 - LJTI_0_0) / 2
998 MCBinaryExpr::CreateSub(MBBSymbolExpr,
999 MCSymbolRefExpr::Create(JTISymbol, OutContext),
1001 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
1003 OutStreamer.EmitValue(Expr, OffsetWidth);
1005 // Mark the end of jump table data-in-code region. 32-bit offsets use
1006 // actual branch instructions here, so we don't mark those as a data-region
1008 if (OffsetWidth != 4)
1009 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1012 void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
1013 assert(MI->getFlag(MachineInstr::FrameSetup) &&
1014 "Only instruction which are involved into frame setup code are allowed");
1016 MCTargetStreamer &TS = *OutStreamer.getTargetStreamer();
1017 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1018 const MachineFunction &MF = *MI->getParent()->getParent();
1019 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
1020 const ARMFunctionInfo &AFI = *MF.getInfo<ARMFunctionInfo>();
1022 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1023 unsigned Opc = MI->getOpcode();
1024 unsigned SrcReg, DstReg;
1026 if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) {
1027 // Two special cases:
1028 // 1) tPUSH does not have src/dst regs.
1029 // 2) for Thumb1 code we sometimes materialize the constant via constpool
1030 // load. Yes, this is pretty fragile, but for now I don't see better
1032 SrcReg = DstReg = ARM::SP;
1034 SrcReg = MI->getOperand(1).getReg();
1035 DstReg = MI->getOperand(0).getReg();
1038 // Try to figure out the unwinding opcode out of src / dst regs.
1039 if (MI->mayStore()) {
1041 assert(DstReg == ARM::SP &&
1042 "Only stack pointer as a destination reg is supported");
1044 SmallVector<unsigned, 4> RegList;
1045 // Skip src & dst reg, and pred ops.
1046 unsigned StartOp = 2 + 2;
1047 // Use all the operands.
1048 unsigned NumOffset = 0;
1053 llvm_unreachable("Unsupported opcode for unwinding information");
1055 // Special case here: no src & dst reg, but two extra imp ops.
1056 StartOp = 2; NumOffset = 2;
1057 case ARM::STMDB_UPD:
1058 case ARM::t2STMDB_UPD:
1059 case ARM::VSTMDDB_UPD:
1060 assert(SrcReg == ARM::SP &&
1061 "Only stack pointer as a source reg is supported");
1062 for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
1064 const MachineOperand &MO = MI->getOperand(i);
1065 // Actually, there should never be any impdef stuff here. Skip it
1066 // temporary to workaround PR11902.
1067 if (MO.isImplicit())
1069 RegList.push_back(MO.getReg());
1072 case ARM::STR_PRE_IMM:
1073 case ARM::STR_PRE_REG:
1074 case ARM::t2STR_PRE:
1075 assert(MI->getOperand(2).getReg() == ARM::SP &&
1076 "Only stack pointer as a source reg is supported");
1077 RegList.push_back(SrcReg);
1080 if (MAI->getExceptionHandlingType() == ExceptionHandling::ARM)
1081 ATS.emitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
1083 // Changes of stack / frame pointer.
1084 if (SrcReg == ARM::SP) {
1089 llvm_unreachable("Unsupported opcode for unwinding information");
1095 Offset = -MI->getOperand(2).getImm();
1099 Offset = MI->getOperand(2).getImm();
1102 Offset = MI->getOperand(2).getImm()*4;
1106 Offset = -MI->getOperand(2).getImm()*4;
1108 case ARM::tLDRpci: {
1109 // Grab the constpool index and check, whether it corresponds to
1110 // original or cloned constpool entry.
1111 unsigned CPI = MI->getOperand(1).getIndex();
1112 const MachineConstantPool *MCP = MF.getConstantPool();
1113 if (CPI >= MCP->getConstants().size())
1114 CPI = AFI.getOriginalCPIdx(CPI);
1115 assert(CPI != -1U && "Invalid constpool index");
1117 // Derive the actual offset.
1118 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
1119 assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
1120 // FIXME: Check for user, it should be "add" instruction!
1121 Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
1126 if (MAI->getExceptionHandlingType() == ExceptionHandling::ARM) {
1127 if (DstReg == FramePtr && FramePtr != ARM::SP)
1128 // Set-up of the frame pointer. Positive values correspond to "add"
1130 ATS.emitSetFP(FramePtr, ARM::SP, -Offset);
1131 else if (DstReg == ARM::SP) {
1132 // Change of SP by an offset. Positive values correspond to "sub"
1134 ATS.emitPad(Offset);
1136 // Move of SP to a register. Positive values correspond to an "add"
1138 ATS.emitMovSP(DstReg, -Offset);
1141 } else if (DstReg == ARM::SP) {
1143 llvm_unreachable("Unsupported opcode for unwinding information");
1147 llvm_unreachable("Unsupported opcode for unwinding information");
1152 // Simple pseudo-instructions have their lowering (with expansion to real
1153 // instructions) auto-generated.
1154 #include "ARMGenMCPseudoLowering.inc"
1156 void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
1157 const DataLayout *DL = TM.getDataLayout();
1159 // If we just ended a constant pool, mark it as such.
1160 if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) {
1161 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1162 InConstantPool = false;
1165 // Emit unwinding stuff for frame-related instructions
1166 if (Subtarget->isTargetEHABICompatible() &&
1167 MI->getFlag(MachineInstr::FrameSetup))
1168 EmitUnwindingInstruction(MI);
1170 // Do any auto-generated pseudo lowerings.
1171 if (emitPseudoExpansionLowering(OutStreamer, MI))
1174 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
1175 "Pseudo flag setting opcode should be expanded early");
1177 // Check for manual lowerings.
1178 unsigned Opc = MI->getOpcode();
1180 case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass");
1181 case ARM::DBG_VALUE: llvm_unreachable("Should be handled by generic printing");
1183 case ARM::tLEApcrel:
1184 case ARM::t2LEApcrel: {
1185 // FIXME: Need to also handle globals and externals
1186 MCSymbol *CPISymbol = GetCPISymbol(MI->getOperand(1).getIndex());
1187 EmitToStreamer(OutStreamer, MCInstBuilder(MI->getOpcode() ==
1188 ARM::t2LEApcrel ? ARM::t2ADR
1189 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
1191 .addReg(MI->getOperand(0).getReg())
1192 .addExpr(MCSymbolRefExpr::Create(CPISymbol, OutContext))
1193 // Add predicate operands.
1194 .addImm(MI->getOperand(2).getImm())
1195 .addReg(MI->getOperand(3).getReg()));
1198 case ARM::LEApcrelJT:
1199 case ARM::tLEApcrelJT:
1200 case ARM::t2LEApcrelJT: {
1201 MCSymbol *JTIPICSymbol =
1202 GetARMJTIPICJumpTableLabel2(MI->getOperand(1).getIndex(),
1203 MI->getOperand(2).getImm());
1204 EmitToStreamer(OutStreamer, MCInstBuilder(MI->getOpcode() ==
1205 ARM::t2LEApcrelJT ? ARM::t2ADR
1206 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
1208 .addReg(MI->getOperand(0).getReg())
1209 .addExpr(MCSymbolRefExpr::Create(JTIPICSymbol, OutContext))
1210 // Add predicate operands.
1211 .addImm(MI->getOperand(3).getImm())
1212 .addReg(MI->getOperand(4).getReg()));
1215 // Darwin call instructions are just normal call instructions with different
1216 // clobber semantics (they clobber R9).
1217 case ARM::BX_CALL: {
1218 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVr)
1221 // Add predicate operands.
1224 // Add 's' bit operand (always reg0 for this)
1227 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::BX)
1228 .addReg(MI->getOperand(0).getReg()));
1231 case ARM::tBX_CALL: {
1232 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVr)
1235 // Add predicate operands.
1239 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tBX)
1240 .addReg(MI->getOperand(0).getReg())
1241 // Add predicate operands.
1246 case ARM::BMOVPCRX_CALL: {
1247 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVr)
1250 // Add predicate operands.
1253 // Add 's' bit operand (always reg0 for this)
1256 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVr)
1258 .addReg(MI->getOperand(0).getReg())
1259 // Add predicate operands.
1262 // Add 's' bit operand (always reg0 for this)
1266 case ARM::BMOVPCB_CALL: {
1267 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVr)
1270 // Add predicate operands.
1273 // Add 's' bit operand (always reg0 for this)
1276 const MachineOperand &Op = MI->getOperand(0);
1277 const GlobalValue *GV = Op.getGlobal();
1278 const unsigned TF = Op.getTargetFlags();
1279 MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
1280 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1281 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::Bcc)
1283 // Add predicate operands.
1288 case ARM::MOVi16_ga_pcrel:
1289 case ARM::t2MOVi16_ga_pcrel: {
1291 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
1292 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1294 unsigned TF = MI->getOperand(1).getTargetFlags();
1295 const GlobalValue *GV = MI->getOperand(1).getGlobal();
1296 MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
1297 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1299 MCSymbol *LabelSym = getPICLabel(DL->getPrivateGlobalPrefix(),
1300 getFunctionNumber(),
1301 MI->getOperand(2).getImm(), OutContext);
1302 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1303 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
1304 const MCExpr *PCRelExpr =
1305 ARMMCExpr::CreateLower16(MCBinaryExpr::CreateSub(GVSymExpr,
1306 MCBinaryExpr::CreateAdd(LabelSymExpr,
1307 MCConstantExpr::Create(PCAdj, OutContext),
1308 OutContext), OutContext), OutContext);
1309 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1311 // Add predicate operands.
1312 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1313 TmpInst.addOperand(MCOperand::CreateReg(0));
1314 // Add 's' bit operand (always reg0 for this)
1315 TmpInst.addOperand(MCOperand::CreateReg(0));
1316 EmitToStreamer(OutStreamer, TmpInst);
1319 case ARM::MOVTi16_ga_pcrel:
1320 case ARM::t2MOVTi16_ga_pcrel: {
1322 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
1323 ? ARM::MOVTi16 : ARM::t2MOVTi16);
1324 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1325 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1327 unsigned TF = MI->getOperand(2).getTargetFlags();
1328 const GlobalValue *GV = MI->getOperand(2).getGlobal();
1329 MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
1330 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1332 MCSymbol *LabelSym = getPICLabel(DL->getPrivateGlobalPrefix(),
1333 getFunctionNumber(),
1334 MI->getOperand(3).getImm(), OutContext);
1335 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1336 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
1337 const MCExpr *PCRelExpr =
1338 ARMMCExpr::CreateUpper16(MCBinaryExpr::CreateSub(GVSymExpr,
1339 MCBinaryExpr::CreateAdd(LabelSymExpr,
1340 MCConstantExpr::Create(PCAdj, OutContext),
1341 OutContext), OutContext), OutContext);
1342 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1343 // Add predicate operands.
1344 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1345 TmpInst.addOperand(MCOperand::CreateReg(0));
1346 // Add 's' bit operand (always reg0 for this)
1347 TmpInst.addOperand(MCOperand::CreateReg(0));
1348 EmitToStreamer(OutStreamer, TmpInst);
1351 case ARM::tPICADD: {
1352 // This is a pseudo op for a label + instruction sequence, which looks like:
1355 // This adds the address of LPC0 to r0.
1358 OutStreamer.EmitLabel(getPICLabel(DL->getPrivateGlobalPrefix(),
1359 getFunctionNumber(), MI->getOperand(2).getImm(),
1362 // Form and emit the add.
1363 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tADDhirr)
1364 .addReg(MI->getOperand(0).getReg())
1365 .addReg(MI->getOperand(0).getReg())
1367 // Add predicate operands.
1373 // This is a pseudo op for a label + instruction sequence, which looks like:
1376 // This adds the address of LPC0 to r0.
1379 OutStreamer.EmitLabel(getPICLabel(DL->getPrivateGlobalPrefix(),
1380 getFunctionNumber(), MI->getOperand(2).getImm(),
1383 // Form and emit the add.
1384 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::ADDrr)
1385 .addReg(MI->getOperand(0).getReg())
1387 .addReg(MI->getOperand(1).getReg())
1388 // Add predicate operands.
1389 .addImm(MI->getOperand(3).getImm())
1390 .addReg(MI->getOperand(4).getReg())
1391 // Add 's' bit operand (always reg0 for this)
1402 case ARM::PICLDRSH: {
1403 // This is a pseudo op for a label + instruction sequence, which looks like:
1406 // The LCP0 label is referenced by a constant pool entry in order to get
1407 // a PC-relative address at the ldr instruction.
1410 OutStreamer.EmitLabel(getPICLabel(DL->getPrivateGlobalPrefix(),
1411 getFunctionNumber(), MI->getOperand(2).getImm(),
1414 // Form and emit the load
1416 switch (MI->getOpcode()) {
1418 llvm_unreachable("Unexpected opcode!");
1419 case ARM::PICSTR: Opcode = ARM::STRrs; break;
1420 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
1421 case ARM::PICSTRH: Opcode = ARM::STRH; break;
1422 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
1423 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
1424 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
1425 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1426 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1428 EmitToStreamer(OutStreamer, MCInstBuilder(Opcode)
1429 .addReg(MI->getOperand(0).getReg())
1431 .addReg(MI->getOperand(1).getReg())
1433 // Add predicate operands.
1434 .addImm(MI->getOperand(3).getImm())
1435 .addReg(MI->getOperand(4).getReg()));
1439 case ARM::CONSTPOOL_ENTRY: {
1440 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1441 /// in the function. The first operand is the ID# for this instruction, the
1442 /// second is the index into the MachineConstantPool that this is, the third
1443 /// is the size in bytes of this constant pool entry.
1444 /// The required alignment is specified on the basic block holding this MI.
1445 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1446 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
1448 // If this is the first entry of the pool, mark it.
1449 if (!InConstantPool) {
1450 OutStreamer.EmitDataRegion(MCDR_DataRegion);
1451 InConstantPool = true;
1454 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
1456 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1457 if (MCPE.isMachineConstantPoolEntry())
1458 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1460 EmitGlobalConstant(MCPE.Val.ConstVal);
1463 case ARM::t2BR_JT: {
1464 // Lower and emit the instruction itself, then the jump table following it.
1465 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVr)
1467 .addReg(MI->getOperand(0).getReg())
1468 // Add predicate operands.
1472 // Output the data for the jump table itself
1476 case ARM::t2TBB_JT: {
1477 // Lower and emit the instruction itself, then the jump table following it.
1478 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::t2TBB)
1480 .addReg(MI->getOperand(0).getReg())
1481 // Add predicate operands.
1485 // Output the data for the jump table itself
1487 // Make sure the next instruction is 2-byte aligned.
1491 case ARM::t2TBH_JT: {
1492 // Lower and emit the instruction itself, then the jump table following it.
1493 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::t2TBH)
1495 .addReg(MI->getOperand(0).getReg())
1496 // Add predicate operands.
1500 // Output the data for the jump table itself
1506 // Lower and emit the instruction itself, then the jump table following it.
1509 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
1510 ARM::MOVr : ARM::tMOVr;
1511 TmpInst.setOpcode(Opc);
1512 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1513 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1514 // Add predicate operands.
1515 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1516 TmpInst.addOperand(MCOperand::CreateReg(0));
1517 // Add 's' bit operand (always reg0 for this)
1518 if (Opc == ARM::MOVr)
1519 TmpInst.addOperand(MCOperand::CreateReg(0));
1520 EmitToStreamer(OutStreamer, TmpInst);
1522 // Make sure the Thumb jump table is 4-byte aligned.
1523 if (Opc == ARM::tMOVr)
1526 // Output the data for the jump table itself
1531 // Lower and emit the instruction itself, then the jump table following it.
1534 if (MI->getOperand(1).getReg() == 0) {
1536 TmpInst.setOpcode(ARM::LDRi12);
1537 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1538 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1539 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1541 TmpInst.setOpcode(ARM::LDRrs);
1542 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1543 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1544 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1545 TmpInst.addOperand(MCOperand::CreateImm(0));
1547 // Add predicate operands.
1548 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1549 TmpInst.addOperand(MCOperand::CreateReg(0));
1550 EmitToStreamer(OutStreamer, TmpInst);
1552 // Output the data for the jump table itself
1556 case ARM::BR_JTadd: {
1557 // Lower and emit the instruction itself, then the jump table following it.
1558 // add pc, target, idx
1559 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::ADDrr)
1561 .addReg(MI->getOperand(0).getReg())
1562 .addReg(MI->getOperand(1).getReg())
1563 // Add predicate operands.
1566 // Add 's' bit operand (always reg0 for this)
1569 // Output the data for the jump table itself
1574 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1575 // FIXME: Remove this special case when they do.
1576 if (!Subtarget->isTargetMachO()) {
1577 //.long 0xe7ffdefe @ trap
1578 uint32_t Val = 0xe7ffdefeUL;
1579 OutStreamer.AddComment("trap");
1580 OutStreamer.EmitIntValue(Val, 4);
1585 case ARM::TRAPNaCl: {
1586 //.long 0xe7fedef0 @ trap
1587 uint32_t Val = 0xe7fedef0UL;
1588 OutStreamer.AddComment("trap");
1589 OutStreamer.EmitIntValue(Val, 4);
1593 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1594 // FIXME: Remove this special case when they do.
1595 if (!Subtarget->isTargetMachO()) {
1596 //.short 57086 @ trap
1597 uint16_t Val = 0xdefe;
1598 OutStreamer.AddComment("trap");
1599 OutStreamer.EmitIntValue(Val, 2);
1604 case ARM::t2Int_eh_sjlj_setjmp:
1605 case ARM::t2Int_eh_sjlj_setjmp_nofp:
1606 case ARM::tInt_eh_sjlj_setjmp: {
1607 // Two incoming args: GPR:$src, GPR:$val
1610 // str $val, [$src, #4]
1615 unsigned SrcReg = MI->getOperand(0).getReg();
1616 unsigned ValReg = MI->getOperand(1).getReg();
1617 MCSymbol *Label = GetARMSJLJEHLabel();
1618 OutStreamer.AddComment("eh_setjmp begin");
1619 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVr)
1626 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tADDi3)
1636 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tSTRi)
1639 // The offset immediate is #4. The operand value is scaled by 4 for the
1640 // tSTR instruction.
1646 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVi8)
1654 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1655 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tB)
1656 .addExpr(SymbolExpr)
1660 OutStreamer.AddComment("eh_setjmp end");
1661 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVi8)
1669 OutStreamer.EmitLabel(Label);
1673 case ARM::Int_eh_sjlj_setjmp_nofp:
1674 case ARM::Int_eh_sjlj_setjmp: {
1675 // Two incoming args: GPR:$src, GPR:$val
1677 // str $val, [$src, #+4]
1681 unsigned SrcReg = MI->getOperand(0).getReg();
1682 unsigned ValReg = MI->getOperand(1).getReg();
1684 OutStreamer.AddComment("eh_setjmp begin");
1685 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::ADDri)
1692 // 's' bit operand (always reg0 for this).
1695 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::STRi12)
1703 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVi)
1709 // 's' bit operand (always reg0 for this).
1712 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::ADDri)
1719 // 's' bit operand (always reg0 for this).
1722 OutStreamer.AddComment("eh_setjmp end");
1723 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVi)
1729 // 's' bit operand (always reg0 for this).
1733 case ARM::Int_eh_sjlj_longjmp: {
1734 // ldr sp, [$src, #8]
1735 // ldr $scratch, [$src, #4]
1738 unsigned SrcReg = MI->getOperand(0).getReg();
1739 unsigned ScratchReg = MI->getOperand(1).getReg();
1740 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::LDRi12)
1748 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::LDRi12)
1756 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::LDRi12)
1764 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::BX)
1771 case ARM::tInt_eh_sjlj_longjmp: {
1772 // ldr $scratch, [$src, #8]
1774 // ldr $scratch, [$src, #4]
1777 unsigned SrcReg = MI->getOperand(0).getReg();
1778 unsigned ScratchReg = MI->getOperand(1).getReg();
1779 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tLDRi)
1782 // The offset immediate is #8. The operand value is scaled by 4 for the
1783 // tLDR instruction.
1789 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVr)
1796 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tLDRi)
1804 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tLDRi)
1812 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tBX)
1822 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
1824 EmitToStreamer(OutStreamer, TmpInst);
1827 //===----------------------------------------------------------------------===//
1828 // Target Registry Stuff
1829 //===----------------------------------------------------------------------===//
1831 // Force static initialization.
1832 extern "C" void LLVMInitializeARMAsmPrinter() {
1833 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMLETarget);
1834 RegisterAsmPrinter<ARMAsmPrinter> Y(TheARMBETarget);
1835 RegisterAsmPrinter<ARMAsmPrinter> A(TheThumbLETarget);
1836 RegisterAsmPrinter<ARMAsmPrinter> B(TheThumbBETarget);