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(getSymbol(GV),
86 (Subtarget->isTargetELF()
87 ? MCSymbolRefExpr::VK_ARM_TARGET1
88 : MCSymbolRefExpr::VK_None),
91 OutStreamer.EmitValue(E, Size);
94 /// runOnMachineFunction - This uses the EmitInstruction()
95 /// method to print assembly for each instruction.
97 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
98 AFI = MF.getInfo<ARMFunctionInfo>();
99 MCP = MF.getConstantPool();
101 SetupMachineFunction(MF);
103 if (Subtarget->isTargetCOFF()) {
104 bool Internal = MF.getFunction()->hasInternalLinkage();
105 COFF::SymbolStorageClass Scl = Internal ? COFF::IMAGE_SYM_CLASS_STATIC
106 : COFF::IMAGE_SYM_CLASS_EXTERNAL;
107 int Type = COFF::IMAGE_SYM_DTYPE_FUNCTION << COFF::SCT_COMPLEX_TYPE_SHIFT;
109 OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
110 OutStreamer.EmitCOFFSymbolStorageClass(Scl);
111 OutStreamer.EmitCOFFSymbolType(Type);
112 OutStreamer.EndCOFFSymbolDef();
115 // Have common code print out the function header with linkage info etc.
116 EmitFunctionHeader();
118 // Emit the rest of the function body.
121 // We didn't modify anything.
125 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
126 raw_ostream &O, const char *Modifier) {
127 const MachineOperand &MO = MI->getOperand(OpNum);
128 unsigned TF = MO.getTargetFlags();
130 switch (MO.getType()) {
131 default: llvm_unreachable("<unknown operand type>");
132 case MachineOperand::MO_Register: {
133 unsigned Reg = MO.getReg();
134 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
135 assert(!MO.getSubReg() && "Subregs should be eliminated!");
136 if(ARM::GPRPairRegClass.contains(Reg)) {
137 const MachineFunction &MF = *MI->getParent()->getParent();
138 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
139 Reg = TRI->getSubReg(Reg, ARM::gsub_0);
141 O << ARMInstPrinter::getRegisterName(Reg);
144 case MachineOperand::MO_Immediate: {
145 int64_t Imm = MO.getImm();
147 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
148 (TF == ARMII::MO_LO16))
150 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
151 (TF == ARMII::MO_HI16))
156 case MachineOperand::MO_MachineBasicBlock:
157 O << *MO.getMBB()->getSymbol();
159 case MachineOperand::MO_GlobalAddress: {
160 const GlobalValue *GV = MO.getGlobal();
161 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
162 (TF & ARMII::MO_LO16))
164 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
165 (TF & ARMII::MO_HI16))
169 printOffset(MO.getOffset(), O);
170 if (TF == ARMII::MO_PLT)
174 case MachineOperand::MO_ConstantPoolIndex:
175 O << *GetCPISymbol(MO.getIndex());
180 //===--------------------------------------------------------------------===//
182 MCSymbol *ARMAsmPrinter::
183 GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
184 const DataLayout *DL = TM.getDataLayout();
185 SmallString<60> Name;
186 raw_svector_ostream(Name) << DL->getPrivateGlobalPrefix() << "JTI"
187 << getFunctionNumber() << '_' << uid << '_' << uid2;
188 return OutContext.GetOrCreateSymbol(Name.str());
192 MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel() const {
193 const DataLayout *DL = TM.getDataLayout();
194 SmallString<60> Name;
195 raw_svector_ostream(Name) << DL->getPrivateGlobalPrefix() << "SJLJEH"
196 << getFunctionNumber();
197 return OutContext.GetOrCreateSymbol(Name.str());
200 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
201 unsigned AsmVariant, const char *ExtraCode,
203 // Does this asm operand have a single letter operand modifier?
204 if (ExtraCode && ExtraCode[0]) {
205 if (ExtraCode[1] != 0) return true; // Unknown modifier.
207 switch (ExtraCode[0]) {
209 // See if this is a generic print operand
210 return AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O);
211 case 'a': // Print as a memory address.
212 if (MI->getOperand(OpNum).isReg()) {
214 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
219 case 'c': // Don't print "#" before an immediate operand.
220 if (!MI->getOperand(OpNum).isImm())
222 O << MI->getOperand(OpNum).getImm();
224 case 'P': // Print a VFP double precision register.
225 case 'q': // Print a NEON quad precision register.
226 printOperand(MI, OpNum, O);
228 case 'y': // Print a VFP single precision register as indexed double.
229 if (MI->getOperand(OpNum).isReg()) {
230 unsigned Reg = MI->getOperand(OpNum).getReg();
231 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
232 // Find the 'd' register that has this 's' register as a sub-register,
233 // and determine the lane number.
234 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) {
235 if (!ARM::DPRRegClass.contains(*SR))
237 bool Lane0 = TRI->getSubReg(*SR, ARM::ssub_0) == Reg;
238 O << ARMInstPrinter::getRegisterName(*SR) << (Lane0 ? "[0]" : "[1]");
243 case 'B': // Bitwise inverse of integer or symbol without a preceding #.
244 if (!MI->getOperand(OpNum).isImm())
246 O << ~(MI->getOperand(OpNum).getImm());
248 case 'L': // The low 16 bits of an immediate constant.
249 if (!MI->getOperand(OpNum).isImm())
251 O << (MI->getOperand(OpNum).getImm() & 0xffff);
253 case 'M': { // A register range suitable for LDM/STM.
254 if (!MI->getOperand(OpNum).isReg())
256 const MachineOperand &MO = MI->getOperand(OpNum);
257 unsigned RegBegin = MO.getReg();
258 // This takes advantage of the 2 operand-ness of ldm/stm and that we've
259 // already got the operands in registers that are operands to the
260 // inline asm statement.
262 if (ARM::GPRPairRegClass.contains(RegBegin)) {
263 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
264 unsigned Reg0 = TRI->getSubReg(RegBegin, ARM::gsub_0);
265 O << ARMInstPrinter::getRegisterName(Reg0) << ", ";
266 RegBegin = TRI->getSubReg(RegBegin, ARM::gsub_1);
268 O << ARMInstPrinter::getRegisterName(RegBegin);
270 // FIXME: The register allocator not only may not have given us the
271 // registers in sequence, but may not be in ascending registers. This
272 // will require changes in the register allocator that'll need to be
273 // propagated down here if the operands change.
274 unsigned RegOps = OpNum + 1;
275 while (MI->getOperand(RegOps).isReg()) {
277 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
285 case 'R': // The most significant register of a pair.
286 case 'Q': { // The least significant register of a pair.
289 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
290 if (!FlagsOP.isImm())
292 unsigned Flags = FlagsOP.getImm();
294 // This operand may not be the one that actually provides the register. If
295 // it's tied to a previous one then we should refer instead to that one
296 // for registers and their classes.
298 if (InlineAsm::isUseOperandTiedToDef(Flags, TiedIdx)) {
299 for (OpNum = InlineAsm::MIOp_FirstOperand; TiedIdx; --TiedIdx) {
300 unsigned OpFlags = MI->getOperand(OpNum).getImm();
301 OpNum += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
303 Flags = MI->getOperand(OpNum).getImm();
305 // Later code expects OpNum to be pointing at the register rather than
310 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
312 InlineAsm::hasRegClassConstraint(Flags, RC);
313 if (RC == ARM::GPRPairRegClassID) {
316 const MachineOperand &MO = MI->getOperand(OpNum);
319 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
320 unsigned Reg = TRI->getSubReg(MO.getReg(), ExtraCode[0] == 'Q' ?
321 ARM::gsub_0 : ARM::gsub_1);
322 O << ARMInstPrinter::getRegisterName(Reg);
327 unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1;
328 if (RegOp >= MI->getNumOperands())
330 const MachineOperand &MO = MI->getOperand(RegOp);
333 unsigned Reg = MO.getReg();
334 O << ARMInstPrinter::getRegisterName(Reg);
338 case 'e': // The low doubleword register of a NEON quad register.
339 case 'f': { // The high doubleword register of a NEON quad register.
340 if (!MI->getOperand(OpNum).isReg())
342 unsigned Reg = MI->getOperand(OpNum).getReg();
343 if (!ARM::QPRRegClass.contains(Reg))
345 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
346 unsigned SubReg = TRI->getSubReg(Reg, ExtraCode[0] == 'e' ?
347 ARM::dsub_0 : ARM::dsub_1);
348 O << ARMInstPrinter::getRegisterName(SubReg);
352 // This modifier is not yet supported.
353 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
355 case 'H': { // The highest-numbered register of a pair.
356 const MachineOperand &MO = MI->getOperand(OpNum);
359 const MachineFunction &MF = *MI->getParent()->getParent();
360 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
361 unsigned Reg = MO.getReg();
362 if(!ARM::GPRPairRegClass.contains(Reg))
364 Reg = TRI->getSubReg(Reg, ARM::gsub_1);
365 O << ARMInstPrinter::getRegisterName(Reg);
371 printOperand(MI, OpNum, O);
375 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
376 unsigned OpNum, unsigned AsmVariant,
377 const char *ExtraCode,
379 // Does this asm operand have a single letter operand modifier?
380 if (ExtraCode && ExtraCode[0]) {
381 if (ExtraCode[1] != 0) return true; // Unknown modifier.
383 switch (ExtraCode[0]) {
384 case 'A': // A memory operand for a VLD1/VST1 instruction.
385 default: return true; // Unknown modifier.
386 case 'm': // The base register of a memory operand.
387 if (!MI->getOperand(OpNum).isReg())
389 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
394 const MachineOperand &MO = MI->getOperand(OpNum);
395 assert(MO.isReg() && "unexpected inline asm memory operand");
396 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
400 static bool isThumb(const MCSubtargetInfo& STI) {
401 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
404 void ARMAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
405 const MCSubtargetInfo *EndInfo) const {
406 // If either end mode is unknown (EndInfo == NULL) or different than
407 // the start mode, then restore the start mode.
408 const bool WasThumb = isThumb(StartInfo);
409 if (!EndInfo || WasThumb != isThumb(*EndInfo)) {
410 OutStreamer.EmitAssemblerFlag(WasThumb ? MCAF_Code16 : MCAF_Code32);
414 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
415 if (Subtarget->isTargetMachO()) {
416 Reloc::Model RelocM = TM.getRelocationModel();
417 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
418 // Declare all the text sections up front (before the DWARF sections
419 // emitted by AsmPrinter::doInitialization) so the assembler will keep
420 // them together at the beginning of the object file. This helps
421 // avoid out-of-range branches that are due a fundamental limitation of
422 // the way symbol offsets are encoded with the current Darwin ARM
424 const TargetLoweringObjectFileMachO &TLOFMacho =
425 static_cast<const TargetLoweringObjectFileMachO &>(
426 getObjFileLowering());
428 // Collect the set of sections our functions will go into.
429 SetVector<const MCSection *, SmallVector<const MCSection *, 8>,
430 SmallPtrSet<const MCSection *, 8> > TextSections;
431 // Default text section comes first.
432 TextSections.insert(TLOFMacho.getTextSection());
433 // Now any user defined text sections from function attributes.
434 for (Module::iterator F = M.begin(), e = M.end(); F != e; ++F)
435 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage())
436 TextSections.insert(TLOFMacho.SectionForGlobal(F, *Mang, TM));
437 // Now the coalescable sections.
438 TextSections.insert(TLOFMacho.getTextCoalSection());
439 TextSections.insert(TLOFMacho.getConstTextCoalSection());
441 // Emit the sections in the .s file header to fix the order.
442 for (unsigned i = 0, e = TextSections.size(); i != e; ++i)
443 OutStreamer.SwitchSection(TextSections[i]);
445 if (RelocM == Reloc::DynamicNoPIC) {
446 const MCSection *sect =
447 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
448 MachO::S_SYMBOL_STUBS,
449 12, SectionKind::getText());
450 OutStreamer.SwitchSection(sect);
452 const MCSection *sect =
453 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
454 MachO::S_SYMBOL_STUBS,
455 16, SectionKind::getText());
456 OutStreamer.SwitchSection(sect);
458 const MCSection *StaticInitSect =
459 OutContext.getMachOSection("__TEXT", "__StaticInit",
461 MachO::S_ATTR_PURE_INSTRUCTIONS,
462 SectionKind::getText());
463 OutStreamer.SwitchSection(StaticInitSect);
466 // Compiling with debug info should not affect the code
467 // generation. Ensure the cstring section comes before the
468 // optional __DWARF secion. Otherwise, PC-relative loads would
469 // have to use different instruction sequences at "-g" in order to
470 // reach global data in the same object file.
471 OutStreamer.SwitchSection(getObjFileLowering().getCStringSection());
474 // Use unified assembler syntax.
475 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
477 // Emit ARM Build Attributes
478 if (Subtarget->isTargetELF())
483 emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel,
484 MachineModuleInfoImpl::StubValueTy &MCSym) {
486 OutStreamer.EmitLabel(StubLabel);
487 // .indirect_symbol _foo
488 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
491 // External to current translation unit.
492 OutStreamer.EmitIntValue(0, 4/*size*/);
494 // Internal to current translation unit.
496 // When we place the LSDA into the TEXT section, the type info
497 // pointers need to be indirect and pc-rel. We accomplish this by
498 // using NLPs; however, sometimes the types are local to the file.
499 // We need to fill in the value for the NLP in those cases.
500 OutStreamer.EmitValue(
501 MCSymbolRefExpr::Create(MCSym.getPointer(), OutStreamer.getContext()),
506 void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
507 if (Subtarget->isTargetMachO()) {
508 // All darwin targets use mach-o.
509 const TargetLoweringObjectFileMachO &TLOFMacho =
510 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
511 MachineModuleInfoMachO &MMIMacho =
512 MMI->getObjFileInfo<MachineModuleInfoMachO>();
514 // Output non-lazy-pointers for external and common global variables.
515 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
517 if (!Stubs.empty()) {
518 // Switch with ".non_lazy_symbol_pointer" directive.
519 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
522 for (auto &Stub : Stubs)
523 emitNonLazySymbolPointer(OutStreamer, Stub.first, Stub.second);
526 OutStreamer.AddBlankLine();
529 Stubs = MMIMacho.GetHiddenGVStubList();
530 if (!Stubs.empty()) {
531 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
534 for (auto &Stub : Stubs)
535 emitNonLazySymbolPointer(OutStreamer, Stub.first, Stub.second);
538 OutStreamer.AddBlankLine();
541 // Funny Darwin hack: This flag tells the linker that no global symbols
542 // contain code that falls through to other global symbols (e.g. the obvious
543 // implementation of multiple entry points). If this doesn't occur, the
544 // linker can safely perform dead code stripping. Since LLVM never
545 // generates code that does this, it is always safe to set.
546 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
549 // Emit a .data.rel section containing any stubs that were created.
550 if (Subtarget->isTargetELF()) {
551 const TargetLoweringObjectFileELF &TLOFELF =
552 static_cast<const TargetLoweringObjectFileELF &>(getObjFileLowering());
554 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
556 // Output stubs for external and common global variables.
557 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
558 if (!Stubs.empty()) {
559 OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
560 const DataLayout *TD = TM.getDataLayout();
562 for (auto &stub: Stubs) {
563 OutStreamer.EmitLabel(stub.first);
564 OutStreamer.EmitSymbolValue(stub.second.getPointer(),
565 TD->getPointerSize(0));
572 //===----------------------------------------------------------------------===//
573 // Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
575 // The following seem like one-off assembler flags, but they actually need
576 // to appear in the .ARM.attributes section in ELF.
577 // Instead of subclassing the MCELFStreamer, we do the work here.
579 static ARMBuildAttrs::CPUArch getArchForCPU(StringRef CPU,
580 const ARMSubtarget *Subtarget) {
582 return ARMBuildAttrs::v5TEJ;
584 if (Subtarget->hasV8Ops())
585 return ARMBuildAttrs::v8;
586 else if (Subtarget->hasV7Ops()) {
587 if (Subtarget->isMClass() && Subtarget->hasThumb2DSP())
588 return ARMBuildAttrs::v7E_M;
589 return ARMBuildAttrs::v7;
590 } else if (Subtarget->hasV6T2Ops())
591 return ARMBuildAttrs::v6T2;
592 else if (Subtarget->hasV6MOps())
593 return ARMBuildAttrs::v6S_M;
594 else if (Subtarget->hasV6Ops())
595 return ARMBuildAttrs::v6;
596 else if (Subtarget->hasV5TEOps())
597 return ARMBuildAttrs::v5TE;
598 else if (Subtarget->hasV5TOps())
599 return ARMBuildAttrs::v5T;
600 else if (Subtarget->hasV4TOps())
601 return ARMBuildAttrs::v4T;
603 return ARMBuildAttrs::v4;
606 void ARMAsmPrinter::emitAttributes() {
607 MCTargetStreamer &TS = *OutStreamer.getTargetStreamer();
608 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
610 ATS.switchVendor("aeabi");
612 std::string CPUString = Subtarget->getCPUString();
614 // FIXME: remove krait check when GNU tools support krait cpu
615 if (CPUString != "generic" && CPUString != "krait")
616 ATS.emitTextAttribute(ARMBuildAttrs::CPU_name, CPUString);
618 ATS.emitAttribute(ARMBuildAttrs::CPU_arch,
619 getArchForCPU(CPUString, Subtarget));
621 // Tag_CPU_arch_profile must have the default value of 0 when "Architecture
622 // profile is not applicable (e.g. pre v7, or cross-profile code)".
623 if (Subtarget->hasV7Ops()) {
624 if (Subtarget->isAClass()) {
625 ATS.emitAttribute(ARMBuildAttrs::CPU_arch_profile,
626 ARMBuildAttrs::ApplicationProfile);
627 } else if (Subtarget->isRClass()) {
628 ATS.emitAttribute(ARMBuildAttrs::CPU_arch_profile,
629 ARMBuildAttrs::RealTimeProfile);
630 } else if (Subtarget->isMClass()) {
631 ATS.emitAttribute(ARMBuildAttrs::CPU_arch_profile,
632 ARMBuildAttrs::MicroControllerProfile);
636 ATS.emitAttribute(ARMBuildAttrs::ARM_ISA_use, Subtarget->hasARMOps() ?
637 ARMBuildAttrs::Allowed : ARMBuildAttrs::Not_Allowed);
638 if (Subtarget->isThumb1Only()) {
639 ATS.emitAttribute(ARMBuildAttrs::THUMB_ISA_use,
640 ARMBuildAttrs::Allowed);
641 } else if (Subtarget->hasThumb2()) {
642 ATS.emitAttribute(ARMBuildAttrs::THUMB_ISA_use,
643 ARMBuildAttrs::AllowThumb32);
646 if (Subtarget->hasNEON()) {
647 /* NEON is not exactly a VFP architecture, but GAS emit one of
648 * neon/neon-fp-armv8/neon-vfpv4/vfpv3/vfpv2 for .fpu parameters */
649 if (Subtarget->hasFPARMv8()) {
650 if (Subtarget->hasCrypto())
651 ATS.emitFPU(ARM::CRYPTO_NEON_FP_ARMV8);
653 ATS.emitFPU(ARM::NEON_FP_ARMV8);
655 else if (Subtarget->hasVFP4())
656 ATS.emitFPU(ARM::NEON_VFPV4);
658 ATS.emitFPU(ARM::NEON);
659 // Emit Tag_Advanced_SIMD_arch for ARMv8 architecture
660 if (Subtarget->hasV8Ops())
661 ATS.emitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
662 ARMBuildAttrs::AllowNeonARMv8);
664 if (Subtarget->hasFPARMv8())
665 ATS.emitFPU(ARM::FP_ARMV8);
666 else if (Subtarget->hasVFP4())
667 ATS.emitFPU(Subtarget->hasD16() ? ARM::VFPV4_D16 : ARM::VFPV4);
668 else if (Subtarget->hasVFP3())
669 ATS.emitFPU(Subtarget->hasD16() ? ARM::VFPV3_D16 : ARM::VFPV3);
670 else if (Subtarget->hasVFP2())
671 ATS.emitFPU(ARM::VFPV2);
674 if (TM.getRelocationModel() == Reloc::PIC_) {
675 // PIC specific attributes.
676 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RW_data,
677 ARMBuildAttrs::AddressRWPCRel);
678 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RO_data,
679 ARMBuildAttrs::AddressROPCRel);
680 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use,
681 ARMBuildAttrs::AddressGOT);
683 // Allow direct addressing of imported data for all other relocation models.
684 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use,
685 ARMBuildAttrs::AddressDirect);
688 // Signal various FP modes.
689 if (!TM.Options.UnsafeFPMath) {
690 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal, ARMBuildAttrs::Allowed);
691 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
692 ARMBuildAttrs::Allowed);
695 if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath)
696 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
697 ARMBuildAttrs::Allowed);
699 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
700 ARMBuildAttrs::AllowIEE754);
702 // FIXME: add more flags to ARMBuildAttributes.h
703 // 8-bytes alignment stuff.
704 ATS.emitAttribute(ARMBuildAttrs::ABI_align_needed, 1);
705 ATS.emitAttribute(ARMBuildAttrs::ABI_align_preserved, 1);
707 // ABI_HardFP_use attribute to indicate single precision FP.
708 if (Subtarget->isFPOnlySP())
709 ATS.emitAttribute(ARMBuildAttrs::ABI_HardFP_use,
710 ARMBuildAttrs::HardFPSinglePrecision);
712 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
713 if (Subtarget->isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard)
714 ATS.emitAttribute(ARMBuildAttrs::ABI_VFP_args, ARMBuildAttrs::HardFPAAPCS);
716 // FIXME: Should we signal R9 usage?
718 if (Subtarget->hasFP16())
719 ATS.emitAttribute(ARMBuildAttrs::FP_HP_extension, ARMBuildAttrs::AllowHPFP);
721 if (Subtarget->hasMPExtension())
722 ATS.emitAttribute(ARMBuildAttrs::MPextension_use, ARMBuildAttrs::AllowMP);
724 // Hardware divide in ARM mode is part of base arch, starting from ARMv8.
725 // If only Thumb hwdiv is present, it must also be in base arch (ARMv7-R/M).
726 // It is not possible to produce DisallowDIV: if hwdiv is present in the base
727 // arch, supplying -hwdiv downgrades the effective arch, via ClearImpliedBits.
728 // AllowDIVExt is only emitted if hwdiv isn't available in the base arch;
729 // otherwise, the default value (AllowDIVIfExists) applies.
730 if (Subtarget->hasDivideInARMMode() && !Subtarget->hasV8Ops())
731 ATS.emitAttribute(ARMBuildAttrs::DIV_use, ARMBuildAttrs::AllowDIVExt);
734 if (const Module *SourceModule = MMI->getModule()) {
735 // ABI_PCS_wchar_t to indicate wchar_t width
736 // FIXME: There is no way to emit value 0 (wchar_t prohibited).
737 if (auto WCharWidthValue = cast_or_null<ConstantInt>(
738 SourceModule->getModuleFlag("wchar_size"))) {
739 int WCharWidth = WCharWidthValue->getZExtValue();
740 assert((WCharWidth == 2 || WCharWidth == 4) &&
741 "wchar_t width must be 2 or 4 bytes");
742 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_wchar_t, WCharWidth);
745 // ABI_enum_size to indicate enum width
746 // FIXME: There is no way to emit value 0 (enums prohibited) or value 3
747 // (all enums contain a value needing 32 bits to encode).
748 if (auto EnumWidthValue = cast_or_null<ConstantInt>(
749 SourceModule->getModuleFlag("min_enum_size"))) {
750 int EnumWidth = EnumWidthValue->getZExtValue();
751 assert((EnumWidth == 1 || EnumWidth == 4) &&
752 "Minimum enum width must be 1 or 4 bytes");
753 int EnumBuildAttr = EnumWidth == 1 ? 1 : 2;
754 ATS.emitAttribute(ARMBuildAttrs::ABI_enum_size, EnumBuildAttr);
759 if (Subtarget->hasTrustZone() && Subtarget->hasVirtualization())
760 ATS.emitAttribute(ARMBuildAttrs::Virtualization_use,
761 ARMBuildAttrs::AllowTZVirtualization);
762 else if (Subtarget->hasTrustZone())
763 ATS.emitAttribute(ARMBuildAttrs::Virtualization_use,
764 ARMBuildAttrs::AllowTZ);
765 else if (Subtarget->hasVirtualization())
766 ATS.emitAttribute(ARMBuildAttrs::Virtualization_use,
767 ARMBuildAttrs::AllowVirtualization);
769 ATS.finishAttributeSection();
772 //===----------------------------------------------------------------------===//
774 static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
775 unsigned LabelId, MCContext &Ctx) {
777 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
778 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
782 static MCSymbolRefExpr::VariantKind
783 getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
785 case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None;
786 case ARMCP::TLSGD: return MCSymbolRefExpr::VK_TLSGD;
787 case ARMCP::TPOFF: return MCSymbolRefExpr::VK_TPOFF;
788 case ARMCP::GOTTPOFF: return MCSymbolRefExpr::VK_GOTTPOFF;
789 case ARMCP::GOT: return MCSymbolRefExpr::VK_GOT;
790 case ARMCP::GOTOFF: return MCSymbolRefExpr::VK_GOTOFF;
792 llvm_unreachable("Invalid ARMCPModifier!");
795 MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV,
796 unsigned char TargetFlags) {
797 bool isIndirect = Subtarget->isTargetMachO() &&
798 (TargetFlags & ARMII::MO_NONLAZY) &&
799 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
801 return getSymbol(GV);
803 // FIXME: Remove this when Darwin transition to @GOT like syntax.
804 MCSymbol *MCSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
805 MachineModuleInfoMachO &MMIMachO =
806 MMI->getObjFileInfo<MachineModuleInfoMachO>();
807 MachineModuleInfoImpl::StubValueTy &StubSym =
808 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym) :
809 MMIMachO.getGVStubEntry(MCSym);
810 if (!StubSym.getPointer())
811 StubSym = MachineModuleInfoImpl::
812 StubValueTy(getSymbol(GV), !GV->hasInternalLinkage());
817 EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
818 const DataLayout *DL = TM.getDataLayout();
819 int Size = TM.getDataLayout()->getTypeAllocSize(MCPV->getType());
821 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
824 if (ACPV->isLSDA()) {
825 SmallString<128> Str;
826 raw_svector_ostream OS(Str);
827 OS << DL->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
828 MCSym = OutContext.GetOrCreateSymbol(OS.str());
829 } else if (ACPV->isBlockAddress()) {
830 const BlockAddress *BA =
831 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
832 MCSym = GetBlockAddressSymbol(BA);
833 } else if (ACPV->isGlobalValue()) {
834 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
836 // On Darwin, const-pool entries may get the "FOO$non_lazy_ptr" mangling, so
837 // flag the global as MO_NONLAZY.
838 unsigned char TF = Subtarget->isTargetMachO() ? ARMII::MO_NONLAZY : 0;
839 MCSym = GetARMGVSymbol(GV, TF);
840 } else if (ACPV->isMachineBasicBlock()) {
841 const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
842 MCSym = MBB->getSymbol();
844 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
845 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
846 MCSym = GetExternalSymbolSymbol(Sym);
849 // Create an MCSymbol for the reference.
851 MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()),
854 if (ACPV->getPCAdjustment()) {
855 MCSymbol *PCLabel = getPICLabel(DL->getPrivateGlobalPrefix(),
859 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
861 MCBinaryExpr::CreateAdd(PCRelExpr,
862 MCConstantExpr::Create(ACPV->getPCAdjustment(),
865 if (ACPV->mustAddCurrentAddress()) {
866 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
867 // label, so just emit a local label end reference that instead.
868 MCSymbol *DotSym = OutContext.CreateTempSymbol();
869 OutStreamer.EmitLabel(DotSym);
870 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
871 PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext);
873 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
875 OutStreamer.EmitValue(Expr, Size);
878 void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
879 unsigned Opcode = MI->getOpcode();
881 if (Opcode == ARM::BR_JTadd)
883 else if (Opcode == ARM::BR_JTm)
886 const MachineOperand &MO1 = MI->getOperand(OpNum);
887 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
888 unsigned JTI = MO1.getIndex();
890 // Emit a label for the jump table.
891 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
892 OutStreamer.EmitLabel(JTISymbol);
894 // Mark the jump table as data-in-code.
895 OutStreamer.EmitDataRegion(MCDR_DataRegionJT32);
897 // Emit each entry of the table.
898 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
899 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
900 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
902 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
903 MachineBasicBlock *MBB = JTBBs[i];
904 // Construct an MCExpr for the entry. We want a value of the form:
905 // (BasicBlockAddr - TableBeginAddr)
907 // For example, a table with entries jumping to basic blocks BB0 and BB1
910 // .word (LBB0 - LJTI_0_0)
911 // .word (LBB1 - LJTI_0_0)
912 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
914 if (TM.getRelocationModel() == Reloc::PIC_)
915 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
918 // If we're generating a table of Thumb addresses in static relocation
919 // model, we need to add one to keep interworking correctly.
920 else if (AFI->isThumbFunction())
921 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(1,OutContext),
923 OutStreamer.EmitValue(Expr, 4);
925 // Mark the end of jump table data-in-code region.
926 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
929 void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
930 unsigned Opcode = MI->getOpcode();
931 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
932 const MachineOperand &MO1 = MI->getOperand(OpNum);
933 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
934 unsigned JTI = MO1.getIndex();
936 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
937 OutStreamer.EmitLabel(JTISymbol);
939 // Emit each entry of the table.
940 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
941 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
942 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
943 unsigned OffsetWidth = 4;
944 if (MI->getOpcode() == ARM::t2TBB_JT) {
946 // Mark the jump table as data-in-code.
947 OutStreamer.EmitDataRegion(MCDR_DataRegionJT8);
948 } else if (MI->getOpcode() == ARM::t2TBH_JT) {
950 // Mark the jump table as data-in-code.
951 OutStreamer.EmitDataRegion(MCDR_DataRegionJT16);
954 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
955 MachineBasicBlock *MBB = JTBBs[i];
956 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
958 // If this isn't a TBB or TBH, the entries are direct branch instructions.
959 if (OffsetWidth == 4) {
960 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::t2B)
961 .addExpr(MBBSymbolExpr)
966 // Otherwise it's an offset from the dispatch instruction. Construct an
967 // MCExpr for the entry. We want a value of the form:
968 // (BasicBlockAddr - TableBeginAddr) / 2
970 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
973 // .byte (LBB0 - LJTI_0_0) / 2
974 // .byte (LBB1 - LJTI_0_0) / 2
976 MCBinaryExpr::CreateSub(MBBSymbolExpr,
977 MCSymbolRefExpr::Create(JTISymbol, OutContext),
979 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
981 OutStreamer.EmitValue(Expr, OffsetWidth);
983 // Mark the end of jump table data-in-code region. 32-bit offsets use
984 // actual branch instructions here, so we don't mark those as a data-region
986 if (OffsetWidth != 4)
987 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
990 void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
991 assert(MI->getFlag(MachineInstr::FrameSetup) &&
992 "Only instruction which are involved into frame setup code are allowed");
994 MCTargetStreamer &TS = *OutStreamer.getTargetStreamer();
995 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
996 const MachineFunction &MF = *MI->getParent()->getParent();
997 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
998 const ARMFunctionInfo &AFI = *MF.getInfo<ARMFunctionInfo>();
1000 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1001 unsigned Opc = MI->getOpcode();
1002 unsigned SrcReg, DstReg;
1004 if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) {
1005 // Two special cases:
1006 // 1) tPUSH does not have src/dst regs.
1007 // 2) for Thumb1 code we sometimes materialize the constant via constpool
1008 // load. Yes, this is pretty fragile, but for now I don't see better
1010 SrcReg = DstReg = ARM::SP;
1012 SrcReg = MI->getOperand(1).getReg();
1013 DstReg = MI->getOperand(0).getReg();
1016 // Try to figure out the unwinding opcode out of src / dst regs.
1017 if (MI->mayStore()) {
1019 assert(DstReg == ARM::SP &&
1020 "Only stack pointer as a destination reg is supported");
1022 SmallVector<unsigned, 4> RegList;
1023 // Skip src & dst reg, and pred ops.
1024 unsigned StartOp = 2 + 2;
1025 // Use all the operands.
1026 unsigned NumOffset = 0;
1031 llvm_unreachable("Unsupported opcode for unwinding information");
1033 // Special case here: no src & dst reg, but two extra imp ops.
1034 StartOp = 2; NumOffset = 2;
1035 case ARM::STMDB_UPD:
1036 case ARM::t2STMDB_UPD:
1037 case ARM::VSTMDDB_UPD:
1038 assert(SrcReg == ARM::SP &&
1039 "Only stack pointer as a source reg is supported");
1040 for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
1042 const MachineOperand &MO = MI->getOperand(i);
1043 // Actually, there should never be any impdef stuff here. Skip it
1044 // temporary to workaround PR11902.
1045 if (MO.isImplicit())
1047 RegList.push_back(MO.getReg());
1050 case ARM::STR_PRE_IMM:
1051 case ARM::STR_PRE_REG:
1052 case ARM::t2STR_PRE:
1053 assert(MI->getOperand(2).getReg() == ARM::SP &&
1054 "Only stack pointer as a source reg is supported");
1055 RegList.push_back(SrcReg);
1058 if (MAI->getExceptionHandlingType() == ExceptionHandling::ARM)
1059 ATS.emitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
1061 // Changes of stack / frame pointer.
1062 if (SrcReg == ARM::SP) {
1067 llvm_unreachable("Unsupported opcode for unwinding information");
1073 Offset = -MI->getOperand(2).getImm();
1077 Offset = MI->getOperand(2).getImm();
1080 Offset = MI->getOperand(2).getImm()*4;
1084 Offset = -MI->getOperand(2).getImm()*4;
1086 case ARM::tLDRpci: {
1087 // Grab the constpool index and check, whether it corresponds to
1088 // original or cloned constpool entry.
1089 unsigned CPI = MI->getOperand(1).getIndex();
1090 const MachineConstantPool *MCP = MF.getConstantPool();
1091 if (CPI >= MCP->getConstants().size())
1092 CPI = AFI.getOriginalCPIdx(CPI);
1093 assert(CPI != -1U && "Invalid constpool index");
1095 // Derive the actual offset.
1096 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
1097 assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
1098 // FIXME: Check for user, it should be "add" instruction!
1099 Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
1104 if (MAI->getExceptionHandlingType() == ExceptionHandling::ARM) {
1105 if (DstReg == FramePtr && FramePtr != ARM::SP)
1106 // Set-up of the frame pointer. Positive values correspond to "add"
1108 ATS.emitSetFP(FramePtr, ARM::SP, -Offset);
1109 else if (DstReg == ARM::SP) {
1110 // Change of SP by an offset. Positive values correspond to "sub"
1112 ATS.emitPad(Offset);
1114 // Move of SP to a register. Positive values correspond to an "add"
1116 ATS.emitMovSP(DstReg, -Offset);
1119 } else if (DstReg == ARM::SP) {
1121 llvm_unreachable("Unsupported opcode for unwinding information");
1125 llvm_unreachable("Unsupported opcode for unwinding information");
1130 // Simple pseudo-instructions have their lowering (with expansion to real
1131 // instructions) auto-generated.
1132 #include "ARMGenMCPseudoLowering.inc"
1134 void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
1135 const DataLayout *DL = TM.getDataLayout();
1137 // If we just ended a constant pool, mark it as such.
1138 if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) {
1139 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1140 InConstantPool = false;
1143 // Emit unwinding stuff for frame-related instructions
1144 if (Subtarget->isTargetEHABICompatible() &&
1145 MI->getFlag(MachineInstr::FrameSetup))
1146 EmitUnwindingInstruction(MI);
1148 // Do any auto-generated pseudo lowerings.
1149 if (emitPseudoExpansionLowering(OutStreamer, MI))
1152 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
1153 "Pseudo flag setting opcode should be expanded early");
1155 // Check for manual lowerings.
1156 unsigned Opc = MI->getOpcode();
1158 case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass");
1159 case ARM::DBG_VALUE: llvm_unreachable("Should be handled by generic printing");
1161 case ARM::tLEApcrel:
1162 case ARM::t2LEApcrel: {
1163 // FIXME: Need to also handle globals and externals
1164 MCSymbol *CPISymbol = GetCPISymbol(MI->getOperand(1).getIndex());
1165 EmitToStreamer(OutStreamer, MCInstBuilder(MI->getOpcode() ==
1166 ARM::t2LEApcrel ? ARM::t2ADR
1167 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
1169 .addReg(MI->getOperand(0).getReg())
1170 .addExpr(MCSymbolRefExpr::Create(CPISymbol, OutContext))
1171 // Add predicate operands.
1172 .addImm(MI->getOperand(2).getImm())
1173 .addReg(MI->getOperand(3).getReg()));
1176 case ARM::LEApcrelJT:
1177 case ARM::tLEApcrelJT:
1178 case ARM::t2LEApcrelJT: {
1179 MCSymbol *JTIPICSymbol =
1180 GetARMJTIPICJumpTableLabel2(MI->getOperand(1).getIndex(),
1181 MI->getOperand(2).getImm());
1182 EmitToStreamer(OutStreamer, MCInstBuilder(MI->getOpcode() ==
1183 ARM::t2LEApcrelJT ? ARM::t2ADR
1184 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
1186 .addReg(MI->getOperand(0).getReg())
1187 .addExpr(MCSymbolRefExpr::Create(JTIPICSymbol, OutContext))
1188 // Add predicate operands.
1189 .addImm(MI->getOperand(3).getImm())
1190 .addReg(MI->getOperand(4).getReg()));
1193 // Darwin call instructions are just normal call instructions with different
1194 // clobber semantics (they clobber R9).
1195 case ARM::BX_CALL: {
1196 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVr)
1199 // Add predicate operands.
1202 // Add 's' bit operand (always reg0 for this)
1205 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::BX)
1206 .addReg(MI->getOperand(0).getReg()));
1209 case ARM::tBX_CALL: {
1210 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVr)
1213 // Add predicate operands.
1217 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tBX)
1218 .addReg(MI->getOperand(0).getReg())
1219 // Add predicate operands.
1224 case ARM::BMOVPCRX_CALL: {
1225 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVr)
1228 // Add predicate operands.
1231 // Add 's' bit operand (always reg0 for this)
1234 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVr)
1236 .addReg(MI->getOperand(0).getReg())
1237 // Add predicate operands.
1240 // Add 's' bit operand (always reg0 for this)
1244 case ARM::BMOVPCB_CALL: {
1245 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVr)
1248 // Add predicate operands.
1251 // Add 's' bit operand (always reg0 for this)
1254 const GlobalValue *GV = MI->getOperand(0).getGlobal();
1255 MCSymbol *GVSym = getSymbol(GV);
1256 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1257 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::Bcc)
1259 // Add predicate operands.
1264 case ARM::MOVi16_ga_pcrel:
1265 case ARM::t2MOVi16_ga_pcrel: {
1267 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
1268 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1270 unsigned TF = MI->getOperand(1).getTargetFlags();
1271 const GlobalValue *GV = MI->getOperand(1).getGlobal();
1272 MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
1273 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1275 MCSymbol *LabelSym = getPICLabel(DL->getPrivateGlobalPrefix(),
1276 getFunctionNumber(),
1277 MI->getOperand(2).getImm(), OutContext);
1278 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1279 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
1280 const MCExpr *PCRelExpr =
1281 ARMMCExpr::CreateLower16(MCBinaryExpr::CreateSub(GVSymExpr,
1282 MCBinaryExpr::CreateAdd(LabelSymExpr,
1283 MCConstantExpr::Create(PCAdj, OutContext),
1284 OutContext), OutContext), OutContext);
1285 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1287 // Add predicate operands.
1288 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1289 TmpInst.addOperand(MCOperand::CreateReg(0));
1290 // Add 's' bit operand (always reg0 for this)
1291 TmpInst.addOperand(MCOperand::CreateReg(0));
1292 EmitToStreamer(OutStreamer, TmpInst);
1295 case ARM::MOVTi16_ga_pcrel:
1296 case ARM::t2MOVTi16_ga_pcrel: {
1298 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
1299 ? ARM::MOVTi16 : ARM::t2MOVTi16);
1300 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1301 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1303 unsigned TF = MI->getOperand(2).getTargetFlags();
1304 const GlobalValue *GV = MI->getOperand(2).getGlobal();
1305 MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
1306 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1308 MCSymbol *LabelSym = getPICLabel(DL->getPrivateGlobalPrefix(),
1309 getFunctionNumber(),
1310 MI->getOperand(3).getImm(), OutContext);
1311 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1312 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
1313 const MCExpr *PCRelExpr =
1314 ARMMCExpr::CreateUpper16(MCBinaryExpr::CreateSub(GVSymExpr,
1315 MCBinaryExpr::CreateAdd(LabelSymExpr,
1316 MCConstantExpr::Create(PCAdj, OutContext),
1317 OutContext), OutContext), OutContext);
1318 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1319 // Add predicate operands.
1320 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1321 TmpInst.addOperand(MCOperand::CreateReg(0));
1322 // Add 's' bit operand (always reg0 for this)
1323 TmpInst.addOperand(MCOperand::CreateReg(0));
1324 EmitToStreamer(OutStreamer, TmpInst);
1327 case ARM::tPICADD: {
1328 // This is a pseudo op for a label + instruction sequence, which looks like:
1331 // This adds the address of LPC0 to r0.
1334 OutStreamer.EmitLabel(getPICLabel(DL->getPrivateGlobalPrefix(),
1335 getFunctionNumber(), MI->getOperand(2).getImm(),
1338 // Form and emit the add.
1339 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tADDhirr)
1340 .addReg(MI->getOperand(0).getReg())
1341 .addReg(MI->getOperand(0).getReg())
1343 // Add predicate operands.
1349 // This is a pseudo op for a label + instruction sequence, which looks like:
1352 // This adds the address of LPC0 to r0.
1355 OutStreamer.EmitLabel(getPICLabel(DL->getPrivateGlobalPrefix(),
1356 getFunctionNumber(), MI->getOperand(2).getImm(),
1359 // Form and emit the add.
1360 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::ADDrr)
1361 .addReg(MI->getOperand(0).getReg())
1363 .addReg(MI->getOperand(1).getReg())
1364 // Add predicate operands.
1365 .addImm(MI->getOperand(3).getImm())
1366 .addReg(MI->getOperand(4).getReg())
1367 // Add 's' bit operand (always reg0 for this)
1378 case ARM::PICLDRSH: {
1379 // This is a pseudo op for a label + instruction sequence, which looks like:
1382 // The LCP0 label is referenced by a constant pool entry in order to get
1383 // a PC-relative address at the ldr instruction.
1386 OutStreamer.EmitLabel(getPICLabel(DL->getPrivateGlobalPrefix(),
1387 getFunctionNumber(), MI->getOperand(2).getImm(),
1390 // Form and emit the load
1392 switch (MI->getOpcode()) {
1394 llvm_unreachable("Unexpected opcode!");
1395 case ARM::PICSTR: Opcode = ARM::STRrs; break;
1396 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
1397 case ARM::PICSTRH: Opcode = ARM::STRH; break;
1398 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
1399 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
1400 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
1401 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1402 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1404 EmitToStreamer(OutStreamer, MCInstBuilder(Opcode)
1405 .addReg(MI->getOperand(0).getReg())
1407 .addReg(MI->getOperand(1).getReg())
1409 // Add predicate operands.
1410 .addImm(MI->getOperand(3).getImm())
1411 .addReg(MI->getOperand(4).getReg()));
1415 case ARM::CONSTPOOL_ENTRY: {
1416 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1417 /// in the function. The first operand is the ID# for this instruction, the
1418 /// second is the index into the MachineConstantPool that this is, the third
1419 /// is the size in bytes of this constant pool entry.
1420 /// The required alignment is specified on the basic block holding this MI.
1421 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1422 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
1424 // If this is the first entry of the pool, mark it.
1425 if (!InConstantPool) {
1426 OutStreamer.EmitDataRegion(MCDR_DataRegion);
1427 InConstantPool = true;
1430 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
1432 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1433 if (MCPE.isMachineConstantPoolEntry())
1434 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1436 EmitGlobalConstant(MCPE.Val.ConstVal);
1439 case ARM::t2BR_JT: {
1440 // Lower and emit the instruction itself, then the jump table following it.
1441 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVr)
1443 .addReg(MI->getOperand(0).getReg())
1444 // Add predicate operands.
1448 // Output the data for the jump table itself
1452 case ARM::t2TBB_JT: {
1453 // Lower and emit the instruction itself, then the jump table following it.
1454 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::t2TBB)
1456 .addReg(MI->getOperand(0).getReg())
1457 // Add predicate operands.
1461 // Output the data for the jump table itself
1463 // Make sure the next instruction is 2-byte aligned.
1467 case ARM::t2TBH_JT: {
1468 // Lower and emit the instruction itself, then the jump table following it.
1469 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::t2TBH)
1471 .addReg(MI->getOperand(0).getReg())
1472 // Add predicate operands.
1476 // Output the data for the jump table itself
1482 // Lower and emit the instruction itself, then the jump table following it.
1485 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
1486 ARM::MOVr : ARM::tMOVr;
1487 TmpInst.setOpcode(Opc);
1488 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1489 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1490 // Add predicate operands.
1491 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1492 TmpInst.addOperand(MCOperand::CreateReg(0));
1493 // Add 's' bit operand (always reg0 for this)
1494 if (Opc == ARM::MOVr)
1495 TmpInst.addOperand(MCOperand::CreateReg(0));
1496 EmitToStreamer(OutStreamer, TmpInst);
1498 // Make sure the Thumb jump table is 4-byte aligned.
1499 if (Opc == ARM::tMOVr)
1502 // Output the data for the jump table itself
1507 // Lower and emit the instruction itself, then the jump table following it.
1510 if (MI->getOperand(1).getReg() == 0) {
1512 TmpInst.setOpcode(ARM::LDRi12);
1513 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1514 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1515 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1517 TmpInst.setOpcode(ARM::LDRrs);
1518 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1519 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1520 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1521 TmpInst.addOperand(MCOperand::CreateImm(0));
1523 // Add predicate operands.
1524 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1525 TmpInst.addOperand(MCOperand::CreateReg(0));
1526 EmitToStreamer(OutStreamer, TmpInst);
1528 // Output the data for the jump table itself
1532 case ARM::BR_JTadd: {
1533 // Lower and emit the instruction itself, then the jump table following it.
1534 // add pc, target, idx
1535 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::ADDrr)
1537 .addReg(MI->getOperand(0).getReg())
1538 .addReg(MI->getOperand(1).getReg())
1539 // Add predicate operands.
1542 // Add 's' bit operand (always reg0 for this)
1545 // Output the data for the jump table itself
1550 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1551 // FIXME: Remove this special case when they do.
1552 if (!Subtarget->isTargetMachO()) {
1553 //.long 0xe7ffdefe @ trap
1554 uint32_t Val = 0xe7ffdefeUL;
1555 OutStreamer.AddComment("trap");
1556 OutStreamer.EmitIntValue(Val, 4);
1561 case ARM::TRAPNaCl: {
1562 //.long 0xe7fedef0 @ trap
1563 uint32_t Val = 0xe7fedef0UL;
1564 OutStreamer.AddComment("trap");
1565 OutStreamer.EmitIntValue(Val, 4);
1569 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1570 // FIXME: Remove this special case when they do.
1571 if (!Subtarget->isTargetMachO()) {
1572 //.short 57086 @ trap
1573 uint16_t Val = 0xdefe;
1574 OutStreamer.AddComment("trap");
1575 OutStreamer.EmitIntValue(Val, 2);
1580 case ARM::t2Int_eh_sjlj_setjmp:
1581 case ARM::t2Int_eh_sjlj_setjmp_nofp:
1582 case ARM::tInt_eh_sjlj_setjmp: {
1583 // Two incoming args: GPR:$src, GPR:$val
1586 // str $val, [$src, #4]
1591 unsigned SrcReg = MI->getOperand(0).getReg();
1592 unsigned ValReg = MI->getOperand(1).getReg();
1593 MCSymbol *Label = GetARMSJLJEHLabel();
1594 OutStreamer.AddComment("eh_setjmp begin");
1595 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVr)
1602 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tADDi3)
1612 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tSTRi)
1615 // The offset immediate is #4. The operand value is scaled by 4 for the
1616 // tSTR instruction.
1622 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVi8)
1630 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1631 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tB)
1632 .addExpr(SymbolExpr)
1636 OutStreamer.AddComment("eh_setjmp end");
1637 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVi8)
1645 OutStreamer.EmitLabel(Label);
1649 case ARM::Int_eh_sjlj_setjmp_nofp:
1650 case ARM::Int_eh_sjlj_setjmp: {
1651 // Two incoming args: GPR:$src, GPR:$val
1653 // str $val, [$src, #+4]
1657 unsigned SrcReg = MI->getOperand(0).getReg();
1658 unsigned ValReg = MI->getOperand(1).getReg();
1660 OutStreamer.AddComment("eh_setjmp begin");
1661 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::ADDri)
1668 // 's' bit operand (always reg0 for this).
1671 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::STRi12)
1679 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVi)
1685 // 's' bit operand (always reg0 for this).
1688 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::ADDri)
1695 // 's' bit operand (always reg0 for this).
1698 OutStreamer.AddComment("eh_setjmp end");
1699 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVi)
1705 // 's' bit operand (always reg0 for this).
1709 case ARM::Int_eh_sjlj_longjmp: {
1710 // ldr sp, [$src, #8]
1711 // ldr $scratch, [$src, #4]
1714 unsigned SrcReg = MI->getOperand(0).getReg();
1715 unsigned ScratchReg = MI->getOperand(1).getReg();
1716 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::LDRi12)
1724 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::LDRi12)
1732 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::LDRi12)
1740 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::BX)
1747 case ARM::tInt_eh_sjlj_longjmp: {
1748 // ldr $scratch, [$src, #8]
1750 // ldr $scratch, [$src, #4]
1753 unsigned SrcReg = MI->getOperand(0).getReg();
1754 unsigned ScratchReg = MI->getOperand(1).getReg();
1755 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tLDRi)
1758 // The offset immediate is #8. The operand value is scaled by 4 for the
1759 // tLDR instruction.
1765 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVr)
1772 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tLDRi)
1780 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tLDRi)
1788 EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tBX)
1798 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
1800 EmitToStreamer(OutStreamer, TmpInst);
1803 //===----------------------------------------------------------------------===//
1804 // Target Registry Stuff
1805 //===----------------------------------------------------------------------===//
1807 // Force static initialization.
1808 extern "C" void LLVMInitializeARMAsmPrinter() {
1809 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMLETarget);
1810 RegisterAsmPrinter<ARMAsmPrinter> Y(TheARMBETarget);
1811 RegisterAsmPrinter<ARMAsmPrinter> A(TheThumbLETarget);
1812 RegisterAsmPrinter<ARMAsmPrinter> B(TheThumbBETarget);