1 //===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to GAS-format ARM assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
16 #include "ARMAsmPrinter.h"
18 #include "ARMBuildAttrs.h"
19 #include "ARMConstantPoolValue.h"
20 #include "ARMFPUName.h"
21 #include "ARMMachineFunctionInfo.h"
22 #include "ARMTargetMachine.h"
23 #include "ARMTargetObjectFile.h"
24 #include "InstPrinter/ARMInstPrinter.h"
25 #include "MCTargetDesc/ARMAddressingModes.h"
26 #include "MCTargetDesc/ARMMCExpr.h"
27 #include "llvm/ADT/SetVector.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/Assembly/Writer.h"
30 #include "llvm/CodeGen/MachineFunctionPass.h"
31 #include "llvm/CodeGen/MachineJumpTableInfo.h"
32 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
33 #include "llvm/DebugInfo.h"
34 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/DataLayout.h"
36 #include "llvm/IR/Module.h"
37 #include "llvm/IR/Type.h"
38 #include "llvm/MC/MCAsmInfo.h"
39 #include "llvm/MC/MCAssembler.h"
40 #include "llvm/MC/MCContext.h"
41 #include "llvm/MC/MCELFStreamer.h"
42 #include "llvm/MC/MCInst.h"
43 #include "llvm/MC/MCInstBuilder.h"
44 #include "llvm/MC/MCObjectStreamer.h"
45 #include "llvm/MC/MCSectionMachO.h"
46 #include "llvm/MC/MCStreamer.h"
47 #include "llvm/MC/MCSymbol.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/Mangler.h"
55 #include "llvm/Target/TargetMachine.h"
59 /// EmitDwarfRegOp - Emit dwarf register operation.
60 void ARMAsmPrinter::EmitDwarfRegOp(const MachineLocation &MLoc,
61 bool Indirect) const {
62 const TargetRegisterInfo *RI = TM.getRegisterInfo();
63 if (RI->getDwarfRegNum(MLoc.getReg(), false) != -1) {
64 AsmPrinter::EmitDwarfRegOp(MLoc, Indirect);
67 assert(MLoc.isReg() && !Indirect &&
68 "This doesn't support offset/indirection - implement it if needed");
69 unsigned Reg = MLoc.getReg();
70 if (Reg >= ARM::S0 && Reg <= ARM::S31) {
71 assert(ARM::S0 + 31 == ARM::S31 && "Unexpected ARM S register numbering");
72 // S registers are described as bit-pieces of a register
73 // S[2x] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 0)
74 // S[2x+1] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 32)
76 unsigned SReg = Reg - ARM::S0;
77 bool odd = SReg & 0x1;
78 unsigned Rx = 256 + (SReg >> 1);
80 OutStreamer.AddComment("DW_OP_regx for S register");
81 EmitInt8(dwarf::DW_OP_regx);
83 OutStreamer.AddComment(Twine(SReg));
87 OutStreamer.AddComment("DW_OP_bit_piece 32 32");
88 EmitInt8(dwarf::DW_OP_bit_piece);
92 OutStreamer.AddComment("DW_OP_bit_piece 32 0");
93 EmitInt8(dwarf::DW_OP_bit_piece);
97 } else if (Reg >= ARM::Q0 && Reg <= ARM::Q15) {
98 assert(ARM::Q0 + 15 == ARM::Q15 && "Unexpected ARM Q register numbering");
99 // Q registers Q0-Q15 are described by composing two D registers together.
100 // Qx = DW_OP_regx(256+2x) DW_OP_piece(8) DW_OP_regx(256+2x+1)
103 unsigned QReg = Reg - ARM::Q0;
104 unsigned D1 = 256 + 2 * QReg;
105 unsigned D2 = D1 + 1;
107 OutStreamer.AddComment("DW_OP_regx for Q register: D1");
108 EmitInt8(dwarf::DW_OP_regx);
110 OutStreamer.AddComment("DW_OP_piece 8");
111 EmitInt8(dwarf::DW_OP_piece);
114 OutStreamer.AddComment("DW_OP_regx for Q register: D2");
115 EmitInt8(dwarf::DW_OP_regx);
117 OutStreamer.AddComment("DW_OP_piece 8");
118 EmitInt8(dwarf::DW_OP_piece);
123 void ARMAsmPrinter::EmitFunctionBodyEnd() {
124 // Make sure to terminate any constant pools that were at the end
128 InConstantPool = false;
129 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
132 void ARMAsmPrinter::EmitFunctionEntryLabel() {
133 if (AFI->isThumbFunction()) {
134 OutStreamer.EmitAssemblerFlag(MCAF_Code16);
135 OutStreamer.EmitThumbFunc(CurrentFnSym);
138 OutStreamer.EmitLabel(CurrentFnSym);
141 void ARMAsmPrinter::EmitXXStructor(const Constant *CV) {
142 uint64_t Size = TM.getDataLayout()->getTypeAllocSize(CV->getType());
143 assert(Size && "C++ constructor pointer had zero size!");
145 const GlobalValue *GV = dyn_cast<GlobalValue>(CV->stripPointerCasts());
146 assert(GV && "C++ constructor pointer was not a GlobalValue!");
148 const MCExpr *E = MCSymbolRefExpr::Create(getSymbol(GV),
149 (Subtarget->isTargetDarwin()
150 ? MCSymbolRefExpr::VK_None
151 : MCSymbolRefExpr::VK_ARM_TARGET1),
154 OutStreamer.EmitValue(E, Size);
157 /// runOnMachineFunction - This uses the EmitInstruction()
158 /// method to print assembly for each instruction.
160 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
161 AFI = MF.getInfo<ARMFunctionInfo>();
162 MCP = MF.getConstantPool();
164 return AsmPrinter::runOnMachineFunction(MF);
167 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
168 raw_ostream &O, const char *Modifier) {
169 const MachineOperand &MO = MI->getOperand(OpNum);
170 unsigned TF = MO.getTargetFlags();
172 switch (MO.getType()) {
173 default: llvm_unreachable("<unknown operand type>");
174 case MachineOperand::MO_Register: {
175 unsigned Reg = MO.getReg();
176 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
177 assert(!MO.getSubReg() && "Subregs should be eliminated!");
178 if(ARM::GPRPairRegClass.contains(Reg)) {
179 const MachineFunction &MF = *MI->getParent()->getParent();
180 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
181 Reg = TRI->getSubReg(Reg, ARM::gsub_0);
183 O << ARMInstPrinter::getRegisterName(Reg);
186 case MachineOperand::MO_Immediate: {
187 int64_t Imm = MO.getImm();
189 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
190 (TF == ARMII::MO_LO16))
192 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
193 (TF == ARMII::MO_HI16))
198 case MachineOperand::MO_MachineBasicBlock:
199 O << *MO.getMBB()->getSymbol();
201 case MachineOperand::MO_GlobalAddress: {
202 const GlobalValue *GV = MO.getGlobal();
203 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
204 (TF & ARMII::MO_LO16))
206 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
207 (TF & ARMII::MO_HI16))
211 printOffset(MO.getOffset(), O);
212 if (TF == ARMII::MO_PLT)
216 case MachineOperand::MO_ExternalSymbol: {
217 O << *GetExternalSymbolSymbol(MO.getSymbolName());
218 if (TF == ARMII::MO_PLT)
222 case MachineOperand::MO_ConstantPoolIndex:
223 O << *GetCPISymbol(MO.getIndex());
225 case MachineOperand::MO_JumpTableIndex:
226 O << *GetJTISymbol(MO.getIndex());
231 //===--------------------------------------------------------------------===//
233 MCSymbol *ARMAsmPrinter::
234 GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
235 SmallString<60> Name;
236 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
237 << getFunctionNumber() << '_' << uid << '_' << uid2;
238 return OutContext.GetOrCreateSymbol(Name.str());
242 MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel() const {
243 SmallString<60> Name;
244 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
245 << getFunctionNumber();
246 return OutContext.GetOrCreateSymbol(Name.str());
249 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
250 unsigned AsmVariant, const char *ExtraCode,
252 // Does this asm operand have a single letter operand modifier?
253 if (ExtraCode && ExtraCode[0]) {
254 if (ExtraCode[1] != 0) return true; // Unknown modifier.
256 switch (ExtraCode[0]) {
258 // See if this is a generic print operand
259 return AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O);
260 case 'a': // Print as a memory address.
261 if (MI->getOperand(OpNum).isReg()) {
263 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
268 case 'c': // Don't print "#" before an immediate operand.
269 if (!MI->getOperand(OpNum).isImm())
271 O << MI->getOperand(OpNum).getImm();
273 case 'P': // Print a VFP double precision register.
274 case 'q': // Print a NEON quad precision register.
275 printOperand(MI, OpNum, O);
277 case 'y': // Print a VFP single precision register as indexed double.
278 if (MI->getOperand(OpNum).isReg()) {
279 unsigned Reg = MI->getOperand(OpNum).getReg();
280 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
281 // Find the 'd' register that has this 's' register as a sub-register,
282 // and determine the lane number.
283 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) {
284 if (!ARM::DPRRegClass.contains(*SR))
286 bool Lane0 = TRI->getSubReg(*SR, ARM::ssub_0) == Reg;
287 O << ARMInstPrinter::getRegisterName(*SR) << (Lane0 ? "[0]" : "[1]");
292 case 'B': // Bitwise inverse of integer or symbol without a preceding #.
293 if (!MI->getOperand(OpNum).isImm())
295 O << ~(MI->getOperand(OpNum).getImm());
297 case 'L': // The low 16 bits of an immediate constant.
298 if (!MI->getOperand(OpNum).isImm())
300 O << (MI->getOperand(OpNum).getImm() & 0xffff);
302 case 'M': { // A register range suitable for LDM/STM.
303 if (!MI->getOperand(OpNum).isReg())
305 const MachineOperand &MO = MI->getOperand(OpNum);
306 unsigned RegBegin = MO.getReg();
307 // This takes advantage of the 2 operand-ness of ldm/stm and that we've
308 // already got the operands in registers that are operands to the
309 // inline asm statement.
311 if (ARM::GPRPairRegClass.contains(RegBegin)) {
312 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
313 unsigned Reg0 = TRI->getSubReg(RegBegin, ARM::gsub_0);
314 O << ARMInstPrinter::getRegisterName(Reg0) << ", ";;
315 RegBegin = TRI->getSubReg(RegBegin, ARM::gsub_1);
317 O << ARMInstPrinter::getRegisterName(RegBegin);
319 // FIXME: The register allocator not only may not have given us the
320 // registers in sequence, but may not be in ascending registers. This
321 // will require changes in the register allocator that'll need to be
322 // propagated down here if the operands change.
323 unsigned RegOps = OpNum + 1;
324 while (MI->getOperand(RegOps).isReg()) {
326 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
334 case 'R': // The most significant register of a pair.
335 case 'Q': { // The least significant register of a pair.
338 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
339 if (!FlagsOP.isImm())
341 unsigned Flags = FlagsOP.getImm();
343 // This operand may not be the one that actually provides the register. If
344 // it's tied to a previous one then we should refer instead to that one
345 // for registers and their classes.
347 if (InlineAsm::isUseOperandTiedToDef(Flags, TiedIdx)) {
348 for (OpNum = InlineAsm::MIOp_FirstOperand; TiedIdx; --TiedIdx) {
349 unsigned OpFlags = MI->getOperand(OpNum).getImm();
350 OpNum += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
352 Flags = MI->getOperand(OpNum).getImm();
354 // Later code expects OpNum to be pointing at the register rather than
359 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
361 InlineAsm::hasRegClassConstraint(Flags, RC);
362 if (RC == ARM::GPRPairRegClassID) {
365 const MachineOperand &MO = MI->getOperand(OpNum);
368 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
369 unsigned Reg = TRI->getSubReg(MO.getReg(), ExtraCode[0] == 'Q' ?
370 ARM::gsub_0 : ARM::gsub_1);
371 O << ARMInstPrinter::getRegisterName(Reg);
376 unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1;
377 if (RegOp >= MI->getNumOperands())
379 const MachineOperand &MO = MI->getOperand(RegOp);
382 unsigned Reg = MO.getReg();
383 O << ARMInstPrinter::getRegisterName(Reg);
387 case 'e': // The low doubleword register of a NEON quad register.
388 case 'f': { // The high doubleword register of a NEON quad register.
389 if (!MI->getOperand(OpNum).isReg())
391 unsigned Reg = MI->getOperand(OpNum).getReg();
392 if (!ARM::QPRRegClass.contains(Reg))
394 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
395 unsigned SubReg = TRI->getSubReg(Reg, ExtraCode[0] == 'e' ?
396 ARM::dsub_0 : ARM::dsub_1);
397 O << ARMInstPrinter::getRegisterName(SubReg);
401 // This modifier is not yet supported.
402 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
404 case 'H': { // The highest-numbered register of a pair.
405 const MachineOperand &MO = MI->getOperand(OpNum);
408 const MachineFunction &MF = *MI->getParent()->getParent();
409 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
410 unsigned Reg = MO.getReg();
411 if(!ARM::GPRPairRegClass.contains(Reg))
413 Reg = TRI->getSubReg(Reg, ARM::gsub_1);
414 O << ARMInstPrinter::getRegisterName(Reg);
420 printOperand(MI, OpNum, O);
424 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
425 unsigned OpNum, unsigned AsmVariant,
426 const char *ExtraCode,
428 // Does this asm operand have a single letter operand modifier?
429 if (ExtraCode && ExtraCode[0]) {
430 if (ExtraCode[1] != 0) return true; // Unknown modifier.
432 switch (ExtraCode[0]) {
433 case 'A': // A memory operand for a VLD1/VST1 instruction.
434 default: return true; // Unknown modifier.
435 case 'm': // The base register of a memory operand.
436 if (!MI->getOperand(OpNum).isReg())
438 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
443 const MachineOperand &MO = MI->getOperand(OpNum);
444 assert(MO.isReg() && "unexpected inline asm memory operand");
445 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
449 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
450 if (Subtarget->isTargetDarwin()) {
451 Reloc::Model RelocM = TM.getRelocationModel();
452 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
453 // Declare all the text sections up front (before the DWARF sections
454 // emitted by AsmPrinter::doInitialization) so the assembler will keep
455 // them together at the beginning of the object file. This helps
456 // avoid out-of-range branches that are due a fundamental limitation of
457 // the way symbol offsets are encoded with the current Darwin ARM
459 const TargetLoweringObjectFileMachO &TLOFMacho =
460 static_cast<const TargetLoweringObjectFileMachO &>(
461 getObjFileLowering());
463 // Collect the set of sections our functions will go into.
464 SetVector<const MCSection *, SmallVector<const MCSection *, 8>,
465 SmallPtrSet<const MCSection *, 8> > TextSections;
466 // Default text section comes first.
467 TextSections.insert(TLOFMacho.getTextSection());
468 // Now any user defined text sections from function attributes.
469 for (Module::iterator F = M.begin(), e = M.end(); F != e; ++F)
470 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage())
471 TextSections.insert(TLOFMacho.SectionForGlobal(F, Mang, TM));
472 // Now the coalescable sections.
473 TextSections.insert(TLOFMacho.getTextCoalSection());
474 TextSections.insert(TLOFMacho.getConstTextCoalSection());
476 // Emit the sections in the .s file header to fix the order.
477 for (unsigned i = 0, e = TextSections.size(); i != e; ++i)
478 OutStreamer.SwitchSection(TextSections[i]);
480 if (RelocM == Reloc::DynamicNoPIC) {
481 const MCSection *sect =
482 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
483 MCSectionMachO::S_SYMBOL_STUBS,
484 12, SectionKind::getText());
485 OutStreamer.SwitchSection(sect);
487 const MCSection *sect =
488 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
489 MCSectionMachO::S_SYMBOL_STUBS,
490 16, SectionKind::getText());
491 OutStreamer.SwitchSection(sect);
493 const MCSection *StaticInitSect =
494 OutContext.getMachOSection("__TEXT", "__StaticInit",
495 MCSectionMachO::S_REGULAR |
496 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
497 SectionKind::getText());
498 OutStreamer.SwitchSection(StaticInitSect);
502 // Use unified assembler syntax.
503 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
505 // Emit ARM Build Attributes
506 if (Subtarget->isTargetELF())
511 void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
512 if (Subtarget->isTargetDarwin()) {
513 // All darwin targets use mach-o.
514 const TargetLoweringObjectFileMachO &TLOFMacho =
515 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
516 MachineModuleInfoMachO &MMIMacho =
517 MMI->getObjFileInfo<MachineModuleInfoMachO>();
519 // Output non-lazy-pointers for external and common global variables.
520 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
522 if (!Stubs.empty()) {
523 // Switch with ".non_lazy_symbol_pointer" directive.
524 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
526 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
528 OutStreamer.EmitLabel(Stubs[i].first);
529 // .indirect_symbol _foo
530 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
531 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
534 // External to current translation unit.
535 OutStreamer.EmitIntValue(0, 4/*size*/);
537 // Internal to current translation unit.
539 // When we place the LSDA into the TEXT section, the type info
540 // pointers need to be indirect and pc-rel. We accomplish this by
541 // using NLPs; however, sometimes the types are local to the file.
542 // We need to fill in the value for the NLP in those cases.
543 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
549 OutStreamer.AddBlankLine();
552 Stubs = MMIMacho.GetHiddenGVStubList();
553 if (!Stubs.empty()) {
554 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
556 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
558 OutStreamer.EmitLabel(Stubs[i].first);
560 OutStreamer.EmitValue(MCSymbolRefExpr::
561 Create(Stubs[i].second.getPointer(),
567 OutStreamer.AddBlankLine();
570 // Funny Darwin hack: This flag tells the linker that no global symbols
571 // contain code that falls through to other global symbols (e.g. the obvious
572 // implementation of multiple entry points). If this doesn't occur, the
573 // linker can safely perform dead code stripping. Since LLVM never
574 // generates code that does this, it is always safe to set.
575 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
579 //===----------------------------------------------------------------------===//
580 // Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
582 // The following seem like one-off assembler flags, but they actually need
583 // to appear in the .ARM.attributes section in ELF.
584 // Instead of subclassing the MCELFStreamer, we do the work here.
586 static ARMBuildAttrs::CPUArch getArchForCPU(StringRef CPU,
587 const ARMSubtarget *Subtarget) {
589 return ARMBuildAttrs::v5TEJ;
591 if (Subtarget->hasV8Ops())
592 return ARMBuildAttrs::v8;
593 else if (Subtarget->hasV7Ops()) {
594 if (Subtarget->isMClass() && Subtarget->hasThumb2DSP())
595 return ARMBuildAttrs::v7E_M;
596 return ARMBuildAttrs::v7;
597 } else if (Subtarget->hasV6T2Ops())
598 return ARMBuildAttrs::v6T2;
599 else if (Subtarget->hasV6MOps())
600 return ARMBuildAttrs::v6S_M;
601 else if (Subtarget->hasV6Ops())
602 return ARMBuildAttrs::v6;
603 else if (Subtarget->hasV5TEOps())
604 return ARMBuildAttrs::v5TE;
605 else if (Subtarget->hasV5TOps())
606 return ARMBuildAttrs::v5T;
607 else if (Subtarget->hasV4TOps())
608 return ARMBuildAttrs::v4T;
610 return ARMBuildAttrs::v4;
613 void ARMAsmPrinter::emitAttributes() {
614 MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
615 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
617 ATS.switchVendor("aeabi");
619 std::string CPUString = Subtarget->getCPUString();
621 if (CPUString != "generic")
622 ATS.emitTextAttribute(ARMBuildAttrs::CPU_name, CPUString);
624 ATS.emitAttribute(ARMBuildAttrs::CPU_arch,
625 getArchForCPU(CPUString, Subtarget));
627 if (Subtarget->isAClass()) {
628 ATS.emitAttribute(ARMBuildAttrs::CPU_arch_profile,
629 ARMBuildAttrs::ApplicationProfile);
630 } else if (Subtarget->isRClass()) {
631 ATS.emitAttribute(ARMBuildAttrs::CPU_arch_profile,
632 ARMBuildAttrs::RealTimeProfile);
633 } else if (Subtarget->isMClass()){
634 ATS.emitAttribute(ARMBuildAttrs::CPU_arch_profile,
635 ARMBuildAttrs::MicroControllerProfile);
638 ATS.emitAttribute(ARMBuildAttrs::ARM_ISA_use, Subtarget->hasARMOps() ?
639 ARMBuildAttrs::Allowed : ARMBuildAttrs::Not_Allowed);
640 if (Subtarget->isThumb1Only()) {
641 ATS.emitAttribute(ARMBuildAttrs::THUMB_ISA_use,
642 ARMBuildAttrs::Allowed);
643 } else if (Subtarget->hasThumb2()) {
644 ATS.emitAttribute(ARMBuildAttrs::THUMB_ISA_use,
645 ARMBuildAttrs::AllowThumb32);
648 if (Subtarget->hasNEON()) {
649 /* NEON is not exactly a VFP architecture, but GAS emit one of
650 * neon/neon-fp-armv8/neon-vfpv4/vfpv3/vfpv2 for .fpu parameters */
651 if (Subtarget->hasFPARMv8()) {
652 if (Subtarget->hasCrypto())
653 ATS.emitFPU(ARM::CRYPTO_NEON_FP_ARMV8);
655 ATS.emitFPU(ARM::NEON_FP_ARMV8);
657 else if (Subtarget->hasVFP4())
658 ATS.emitFPU(ARM::NEON_VFPV4);
660 ATS.emitFPU(ARM::NEON);
661 // Emit Tag_Advanced_SIMD_arch for ARMv8 architecture
662 if (Subtarget->hasV8Ops())
663 ATS.emitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
664 ARMBuildAttrs::AllowNeonARMv8);
666 if (Subtarget->hasFPARMv8())
667 ATS.emitFPU(ARM::FP_ARMV8);
668 else if (Subtarget->hasVFP4())
669 ATS.emitFPU(Subtarget->hasD16() ? ARM::VFPV4_D16 : ARM::VFPV4);
670 else if (Subtarget->hasVFP3())
671 ATS.emitFPU(Subtarget->hasD16() ? ARM::VFPV3_D16 : ARM::VFPV3);
672 else if (Subtarget->hasVFP2())
673 ATS.emitFPU(ARM::VFPV2);
676 // Signal various FP modes.
677 if (!TM.Options.UnsafeFPMath) {
678 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal, ARMBuildAttrs::Allowed);
679 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
680 ARMBuildAttrs::Allowed);
683 if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath)
684 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
685 ARMBuildAttrs::Allowed);
687 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
688 ARMBuildAttrs::AllowIEE754);
690 // FIXME: add more flags to ARMBuildAttrs.h
691 // 8-bytes alignment stuff.
692 ATS.emitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
693 ATS.emitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
695 // ABI_HardFP_use attribute to indicate single precision FP.
696 if (Subtarget->isFPOnlySP())
697 ATS.emitAttribute(ARMBuildAttrs::ABI_HardFP_use,
698 ARMBuildAttrs::HardFPSinglePrecision);
700 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
701 if (Subtarget->isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard)
702 ATS.emitAttribute(ARMBuildAttrs::ABI_VFP_args, ARMBuildAttrs::HardFPAAPCS);
704 // FIXME: Should we signal R9 usage?
706 if (Subtarget->hasDivide()) {
707 // Check if hardware divide is only available in thumb2 or ARM as well.
708 ATS.emitAttribute(ARMBuildAttrs::DIV_use,
709 Subtarget->hasDivideInARMMode() ? ARMBuildAttrs::AllowDIVExt :
710 ARMBuildAttrs::AllowDIVIfExists);
713 ATS.finishAttributeSection();
716 void ARMAsmPrinter::emitARMAttributeSection() {
718 // [ <section-length> "vendor-name"
719 // [ <file-tag> <size> <attribute>*
720 // | <section-tag> <size> <section-number>* 0 <attribute>*
721 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
725 if (OutStreamer.hasRawTextSupport())
728 const ARMElfTargetObjectFile &TLOFELF =
729 static_cast<const ARMElfTargetObjectFile &>
730 (getObjFileLowering());
732 OutStreamer.SwitchSection(TLOFELF.getAttributesSection());
735 OutStreamer.EmitIntValue(0x41, 1);
738 //===----------------------------------------------------------------------===//
740 static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
741 unsigned LabelId, MCContext &Ctx) {
743 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
744 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
748 static MCSymbolRefExpr::VariantKind
749 getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
751 case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None;
752 case ARMCP::TLSGD: return MCSymbolRefExpr::VK_ARM_TLSGD;
753 case ARMCP::TPOFF: return MCSymbolRefExpr::VK_ARM_TPOFF;
754 case ARMCP::GOTTPOFF: return MCSymbolRefExpr::VK_ARM_GOTTPOFF;
755 case ARMCP::GOT: return MCSymbolRefExpr::VK_ARM_GOT;
756 case ARMCP::GOTOFF: return MCSymbolRefExpr::VK_ARM_GOTOFF;
758 llvm_unreachable("Invalid ARMCPModifier!");
761 MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV) {
762 bool isIndirect = Subtarget->isTargetDarwin() &&
763 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
765 return getSymbol(GV);
767 // FIXME: Remove this when Darwin transition to @GOT like syntax.
768 MCSymbol *MCSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
769 MachineModuleInfoMachO &MMIMachO =
770 MMI->getObjFileInfo<MachineModuleInfoMachO>();
771 MachineModuleInfoImpl::StubValueTy &StubSym =
772 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym) :
773 MMIMachO.getGVStubEntry(MCSym);
774 if (StubSym.getPointer() == 0)
775 StubSym = MachineModuleInfoImpl::
776 StubValueTy(getSymbol(GV), !GV->hasInternalLinkage());
781 EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
782 int Size = TM.getDataLayout()->getTypeAllocSize(MCPV->getType());
784 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
787 if (ACPV->isLSDA()) {
788 SmallString<128> Str;
789 raw_svector_ostream OS(Str);
790 OS << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
791 MCSym = OutContext.GetOrCreateSymbol(OS.str());
792 } else if (ACPV->isBlockAddress()) {
793 const BlockAddress *BA =
794 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
795 MCSym = GetBlockAddressSymbol(BA);
796 } else if (ACPV->isGlobalValue()) {
797 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
798 MCSym = GetARMGVSymbol(GV);
799 } else if (ACPV->isMachineBasicBlock()) {
800 const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
801 MCSym = MBB->getSymbol();
803 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
804 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
805 MCSym = GetExternalSymbolSymbol(Sym);
808 // Create an MCSymbol for the reference.
810 MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()),
813 if (ACPV->getPCAdjustment()) {
814 MCSymbol *PCLabel = getPICLabel(MAI->getPrivateGlobalPrefix(),
818 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
820 MCBinaryExpr::CreateAdd(PCRelExpr,
821 MCConstantExpr::Create(ACPV->getPCAdjustment(),
824 if (ACPV->mustAddCurrentAddress()) {
825 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
826 // label, so just emit a local label end reference that instead.
827 MCSymbol *DotSym = OutContext.CreateTempSymbol();
828 OutStreamer.EmitLabel(DotSym);
829 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
830 PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext);
832 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
834 OutStreamer.EmitValue(Expr, Size);
837 void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
838 unsigned Opcode = MI->getOpcode();
840 if (Opcode == ARM::BR_JTadd)
842 else if (Opcode == ARM::BR_JTm)
845 const MachineOperand &MO1 = MI->getOperand(OpNum);
846 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
847 unsigned JTI = MO1.getIndex();
849 // Emit a label for the jump table.
850 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
851 OutStreamer.EmitLabel(JTISymbol);
853 // Mark the jump table as data-in-code.
854 OutStreamer.EmitDataRegion(MCDR_DataRegionJT32);
856 // Emit each entry of the table.
857 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
858 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
859 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
861 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
862 MachineBasicBlock *MBB = JTBBs[i];
863 // Construct an MCExpr for the entry. We want a value of the form:
864 // (BasicBlockAddr - TableBeginAddr)
866 // For example, a table with entries jumping to basic blocks BB0 and BB1
869 // .word (LBB0 - LJTI_0_0)
870 // .word (LBB1 - LJTI_0_0)
871 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
873 if (TM.getRelocationModel() == Reloc::PIC_)
874 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
877 // If we're generating a table of Thumb addresses in static relocation
878 // model, we need to add one to keep interworking correctly.
879 else if (AFI->isThumbFunction())
880 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(1,OutContext),
882 OutStreamer.EmitValue(Expr, 4);
884 // Mark the end of jump table data-in-code region.
885 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
888 void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
889 unsigned Opcode = MI->getOpcode();
890 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
891 const MachineOperand &MO1 = MI->getOperand(OpNum);
892 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
893 unsigned JTI = MO1.getIndex();
895 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
896 OutStreamer.EmitLabel(JTISymbol);
898 // Emit each entry of the table.
899 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
900 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
901 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
902 unsigned OffsetWidth = 4;
903 if (MI->getOpcode() == ARM::t2TBB_JT) {
905 // Mark the jump table as data-in-code.
906 OutStreamer.EmitDataRegion(MCDR_DataRegionJT8);
907 } else if (MI->getOpcode() == ARM::t2TBH_JT) {
909 // Mark the jump table as data-in-code.
910 OutStreamer.EmitDataRegion(MCDR_DataRegionJT16);
913 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
914 MachineBasicBlock *MBB = JTBBs[i];
915 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
917 // If this isn't a TBB or TBH, the entries are direct branch instructions.
918 if (OffsetWidth == 4) {
919 OutStreamer.EmitInstruction(MCInstBuilder(ARM::t2B)
920 .addExpr(MBBSymbolExpr)
925 // Otherwise it's an offset from the dispatch instruction. Construct an
926 // MCExpr for the entry. We want a value of the form:
927 // (BasicBlockAddr - TableBeginAddr) / 2
929 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
932 // .byte (LBB0 - LJTI_0_0) / 2
933 // .byte (LBB1 - LJTI_0_0) / 2
935 MCBinaryExpr::CreateSub(MBBSymbolExpr,
936 MCSymbolRefExpr::Create(JTISymbol, OutContext),
938 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
940 OutStreamer.EmitValue(Expr, OffsetWidth);
942 // Mark the end of jump table data-in-code region. 32-bit offsets use
943 // actual branch instructions here, so we don't mark those as a data-region
945 if (OffsetWidth != 4)
946 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
949 void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
950 assert(MI->getFlag(MachineInstr::FrameSetup) &&
951 "Only instruction which are involved into frame setup code are allowed");
953 MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
954 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
955 const MachineFunction &MF = *MI->getParent()->getParent();
956 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
957 const ARMFunctionInfo &AFI = *MF.getInfo<ARMFunctionInfo>();
959 unsigned FramePtr = RegInfo->getFrameRegister(MF);
960 unsigned Opc = MI->getOpcode();
961 unsigned SrcReg, DstReg;
963 if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) {
964 // Two special cases:
965 // 1) tPUSH does not have src/dst regs.
966 // 2) for Thumb1 code we sometimes materialize the constant via constpool
967 // load. Yes, this is pretty fragile, but for now I don't see better
969 SrcReg = DstReg = ARM::SP;
971 SrcReg = MI->getOperand(1).getReg();
972 DstReg = MI->getOperand(0).getReg();
975 // Try to figure out the unwinding opcode out of src / dst regs.
976 if (MI->mayStore()) {
978 assert(DstReg == ARM::SP &&
979 "Only stack pointer as a destination reg is supported");
981 SmallVector<unsigned, 4> RegList;
982 // Skip src & dst reg, and pred ops.
983 unsigned StartOp = 2 + 2;
984 // Use all the operands.
985 unsigned NumOffset = 0;
990 llvm_unreachable("Unsupported opcode for unwinding information");
992 // Special case here: no src & dst reg, but two extra imp ops.
993 StartOp = 2; NumOffset = 2;
995 case ARM::t2STMDB_UPD:
996 case ARM::VSTMDDB_UPD:
997 assert(SrcReg == ARM::SP &&
998 "Only stack pointer as a source reg is supported");
999 for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
1001 const MachineOperand &MO = MI->getOperand(i);
1002 // Actually, there should never be any impdef stuff here. Skip it
1003 // temporary to workaround PR11902.
1004 if (MO.isImplicit())
1006 RegList.push_back(MO.getReg());
1009 case ARM::STR_PRE_IMM:
1010 case ARM::STR_PRE_REG:
1011 case ARM::t2STR_PRE:
1012 assert(MI->getOperand(2).getReg() == ARM::SP &&
1013 "Only stack pointer as a source reg is supported");
1014 RegList.push_back(SrcReg);
1017 ATS.emitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
1019 // Changes of stack / frame pointer.
1020 if (SrcReg == ARM::SP) {
1025 llvm_unreachable("Unsupported opcode for unwinding information");
1031 Offset = -MI->getOperand(2).getImm();
1035 Offset = MI->getOperand(2).getImm();
1038 Offset = MI->getOperand(2).getImm()*4;
1042 Offset = -MI->getOperand(2).getImm()*4;
1044 case ARM::tLDRpci: {
1045 // Grab the constpool index and check, whether it corresponds to
1046 // original or cloned constpool entry.
1047 unsigned CPI = MI->getOperand(1).getIndex();
1048 const MachineConstantPool *MCP = MF.getConstantPool();
1049 if (CPI >= MCP->getConstants().size())
1050 CPI = AFI.getOriginalCPIdx(CPI);
1051 assert(CPI != -1U && "Invalid constpool index");
1053 // Derive the actual offset.
1054 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
1055 assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
1056 // FIXME: Check for user, it should be "add" instruction!
1057 Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
1062 if (DstReg == FramePtr && FramePtr != ARM::SP)
1063 // Set-up of the frame pointer. Positive values correspond to "add"
1065 ATS.emitSetFP(FramePtr, ARM::SP, -Offset);
1066 else if (DstReg == ARM::SP) {
1067 // Change of SP by an offset. Positive values correspond to "sub"
1069 ATS.emitPad(Offset);
1072 llvm_unreachable("Unsupported opcode for unwinding information");
1074 } else if (DstReg == ARM::SP) {
1075 // FIXME: .movsp goes here
1077 llvm_unreachable("Unsupported opcode for unwinding information");
1081 llvm_unreachable("Unsupported opcode for unwinding information");
1086 extern cl::opt<bool> EnableARMEHABI;
1088 // Simple pseudo-instructions have their lowering (with expansion to real
1089 // instructions) auto-generated.
1090 #include "ARMGenMCPseudoLowering.inc"
1092 void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
1093 // If we just ended a constant pool, mark it as such.
1094 if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) {
1095 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1096 InConstantPool = false;
1099 // Emit unwinding stuff for frame-related instructions
1100 if (EnableARMEHABI && MI->getFlag(MachineInstr::FrameSetup))
1101 EmitUnwindingInstruction(MI);
1103 // Do any auto-generated pseudo lowerings.
1104 if (emitPseudoExpansionLowering(OutStreamer, MI))
1107 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
1108 "Pseudo flag setting opcode should be expanded early");
1110 // Check for manual lowerings.
1111 unsigned Opc = MI->getOpcode();
1113 case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass");
1114 case ARM::DBG_VALUE: llvm_unreachable("Should be handled by generic printing");
1116 case ARM::tLEApcrel:
1117 case ARM::t2LEApcrel: {
1118 // FIXME: Need to also handle globals and externals
1119 MCSymbol *CPISymbol = GetCPISymbol(MI->getOperand(1).getIndex());
1120 OutStreamer.EmitInstruction(MCInstBuilder(MI->getOpcode() ==
1121 ARM::t2LEApcrel ? ARM::t2ADR
1122 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
1124 .addReg(MI->getOperand(0).getReg())
1125 .addExpr(MCSymbolRefExpr::Create(CPISymbol, OutContext))
1126 // Add predicate operands.
1127 .addImm(MI->getOperand(2).getImm())
1128 .addReg(MI->getOperand(3).getReg()));
1131 case ARM::LEApcrelJT:
1132 case ARM::tLEApcrelJT:
1133 case ARM::t2LEApcrelJT: {
1134 MCSymbol *JTIPICSymbol =
1135 GetARMJTIPICJumpTableLabel2(MI->getOperand(1).getIndex(),
1136 MI->getOperand(2).getImm());
1137 OutStreamer.EmitInstruction(MCInstBuilder(MI->getOpcode() ==
1138 ARM::t2LEApcrelJT ? ARM::t2ADR
1139 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
1141 .addReg(MI->getOperand(0).getReg())
1142 .addExpr(MCSymbolRefExpr::Create(JTIPICSymbol, OutContext))
1143 // Add predicate operands.
1144 .addImm(MI->getOperand(3).getImm())
1145 .addReg(MI->getOperand(4).getReg()));
1148 // Darwin call instructions are just normal call instructions with different
1149 // clobber semantics (they clobber R9).
1150 case ARM::BX_CALL: {
1151 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr)
1154 // Add predicate operands.
1157 // Add 's' bit operand (always reg0 for this)
1160 OutStreamer.EmitInstruction(MCInstBuilder(ARM::BX)
1161 .addReg(MI->getOperand(0).getReg()));
1164 case ARM::tBX_CALL: {
1165 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr)
1168 // Add predicate operands.
1172 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tBX)
1173 .addReg(MI->getOperand(0).getReg())
1174 // Add predicate operands.
1179 case ARM::BMOVPCRX_CALL: {
1180 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr)
1183 // Add predicate operands.
1186 // Add 's' bit operand (always reg0 for this)
1189 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr)
1191 .addReg(MI->getOperand(0).getReg())
1192 // Add predicate operands.
1195 // Add 's' bit operand (always reg0 for this)
1199 case ARM::BMOVPCB_CALL: {
1200 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr)
1203 // Add predicate operands.
1206 // Add 's' bit operand (always reg0 for this)
1209 const GlobalValue *GV = MI->getOperand(0).getGlobal();
1210 MCSymbol *GVSym = getSymbol(GV);
1211 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1212 OutStreamer.EmitInstruction(MCInstBuilder(ARM::Bcc)
1214 // Add predicate operands.
1219 case ARM::MOVi16_ga_pcrel:
1220 case ARM::t2MOVi16_ga_pcrel: {
1222 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
1223 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1225 unsigned TF = MI->getOperand(1).getTargetFlags();
1226 bool isPIC = TF == ARMII::MO_LO16_NONLAZY_PIC;
1227 const GlobalValue *GV = MI->getOperand(1).getGlobal();
1228 MCSymbol *GVSym = GetARMGVSymbol(GV);
1229 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1231 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1232 getFunctionNumber(),
1233 MI->getOperand(2).getImm(), OutContext);
1234 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1235 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
1236 const MCExpr *PCRelExpr =
1237 ARMMCExpr::CreateLower16(MCBinaryExpr::CreateSub(GVSymExpr,
1238 MCBinaryExpr::CreateAdd(LabelSymExpr,
1239 MCConstantExpr::Create(PCAdj, OutContext),
1240 OutContext), OutContext), OutContext);
1241 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1243 const MCExpr *RefExpr= ARMMCExpr::CreateLower16(GVSymExpr, OutContext);
1244 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1247 // Add predicate operands.
1248 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1249 TmpInst.addOperand(MCOperand::CreateReg(0));
1250 // Add 's' bit operand (always reg0 for this)
1251 TmpInst.addOperand(MCOperand::CreateReg(0));
1252 OutStreamer.EmitInstruction(TmpInst);
1255 case ARM::MOVTi16_ga_pcrel:
1256 case ARM::t2MOVTi16_ga_pcrel: {
1258 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
1259 ? ARM::MOVTi16 : ARM::t2MOVTi16);
1260 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1261 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1263 unsigned TF = MI->getOperand(2).getTargetFlags();
1264 bool isPIC = TF == ARMII::MO_HI16_NONLAZY_PIC;
1265 const GlobalValue *GV = MI->getOperand(2).getGlobal();
1266 MCSymbol *GVSym = GetARMGVSymbol(GV);
1267 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1269 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1270 getFunctionNumber(),
1271 MI->getOperand(3).getImm(), OutContext);
1272 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1273 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
1274 const MCExpr *PCRelExpr =
1275 ARMMCExpr::CreateUpper16(MCBinaryExpr::CreateSub(GVSymExpr,
1276 MCBinaryExpr::CreateAdd(LabelSymExpr,
1277 MCConstantExpr::Create(PCAdj, OutContext),
1278 OutContext), OutContext), OutContext);
1279 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1281 const MCExpr *RefExpr= ARMMCExpr::CreateUpper16(GVSymExpr, OutContext);
1282 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1284 // Add predicate operands.
1285 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1286 TmpInst.addOperand(MCOperand::CreateReg(0));
1287 // Add 's' bit operand (always reg0 for this)
1288 TmpInst.addOperand(MCOperand::CreateReg(0));
1289 OutStreamer.EmitInstruction(TmpInst);
1292 case ARM::tPICADD: {
1293 // This is a pseudo op for a label + instruction sequence, which looks like:
1296 // This adds the address of LPC0 to r0.
1299 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1300 getFunctionNumber(), MI->getOperand(2).getImm(),
1303 // Form and emit the add.
1304 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tADDhirr)
1305 .addReg(MI->getOperand(0).getReg())
1306 .addReg(MI->getOperand(0).getReg())
1308 // Add predicate operands.
1314 // This is a pseudo op for a label + instruction sequence, which looks like:
1317 // This adds the address of LPC0 to r0.
1320 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1321 getFunctionNumber(), MI->getOperand(2).getImm(),
1324 // Form and emit the add.
1325 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDrr)
1326 .addReg(MI->getOperand(0).getReg())
1328 .addReg(MI->getOperand(1).getReg())
1329 // Add predicate operands.
1330 .addImm(MI->getOperand(3).getImm())
1331 .addReg(MI->getOperand(4).getReg())
1332 // Add 's' bit operand (always reg0 for this)
1343 case ARM::PICLDRSH: {
1344 // This is a pseudo op for a label + instruction sequence, which looks like:
1347 // The LCP0 label is referenced by a constant pool entry in order to get
1348 // a PC-relative address at the ldr instruction.
1351 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1352 getFunctionNumber(), MI->getOperand(2).getImm(),
1355 // Form and emit the load
1357 switch (MI->getOpcode()) {
1359 llvm_unreachable("Unexpected opcode!");
1360 case ARM::PICSTR: Opcode = ARM::STRrs; break;
1361 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
1362 case ARM::PICSTRH: Opcode = ARM::STRH; break;
1363 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
1364 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
1365 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
1366 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1367 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1369 OutStreamer.EmitInstruction(MCInstBuilder(Opcode)
1370 .addReg(MI->getOperand(0).getReg())
1372 .addReg(MI->getOperand(1).getReg())
1374 // Add predicate operands.
1375 .addImm(MI->getOperand(3).getImm())
1376 .addReg(MI->getOperand(4).getReg()));
1380 case ARM::CONSTPOOL_ENTRY: {
1381 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1382 /// in the function. The first operand is the ID# for this instruction, the
1383 /// second is the index into the MachineConstantPool that this is, the third
1384 /// is the size in bytes of this constant pool entry.
1385 /// The required alignment is specified on the basic block holding this MI.
1386 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1387 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
1389 // If this is the first entry of the pool, mark it.
1390 if (!InConstantPool) {
1391 OutStreamer.EmitDataRegion(MCDR_DataRegion);
1392 InConstantPool = true;
1395 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
1397 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1398 if (MCPE.isMachineConstantPoolEntry())
1399 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1401 EmitGlobalConstant(MCPE.Val.ConstVal);
1404 case ARM::t2BR_JT: {
1405 // Lower and emit the instruction itself, then the jump table following it.
1406 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr)
1408 .addReg(MI->getOperand(0).getReg())
1409 // Add predicate operands.
1413 // Output the data for the jump table itself
1417 case ARM::t2TBB_JT: {
1418 // Lower and emit the instruction itself, then the jump table following it.
1419 OutStreamer.EmitInstruction(MCInstBuilder(ARM::t2TBB)
1421 .addReg(MI->getOperand(0).getReg())
1422 // Add predicate operands.
1426 // Output the data for the jump table itself
1428 // Make sure the next instruction is 2-byte aligned.
1432 case ARM::t2TBH_JT: {
1433 // Lower and emit the instruction itself, then the jump table following it.
1434 OutStreamer.EmitInstruction(MCInstBuilder(ARM::t2TBH)
1436 .addReg(MI->getOperand(0).getReg())
1437 // Add predicate operands.
1441 // Output the data for the jump table itself
1447 // Lower and emit the instruction itself, then the jump table following it.
1450 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
1451 ARM::MOVr : ARM::tMOVr;
1452 TmpInst.setOpcode(Opc);
1453 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1454 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1455 // Add predicate operands.
1456 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1457 TmpInst.addOperand(MCOperand::CreateReg(0));
1458 // Add 's' bit operand (always reg0 for this)
1459 if (Opc == ARM::MOVr)
1460 TmpInst.addOperand(MCOperand::CreateReg(0));
1461 OutStreamer.EmitInstruction(TmpInst);
1463 // Make sure the Thumb jump table is 4-byte aligned.
1464 if (Opc == ARM::tMOVr)
1467 // Output the data for the jump table itself
1472 // Lower and emit the instruction itself, then the jump table following it.
1475 if (MI->getOperand(1).getReg() == 0) {
1477 TmpInst.setOpcode(ARM::LDRi12);
1478 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1479 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1480 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1482 TmpInst.setOpcode(ARM::LDRrs);
1483 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1484 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1485 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1486 TmpInst.addOperand(MCOperand::CreateImm(0));
1488 // Add predicate operands.
1489 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1490 TmpInst.addOperand(MCOperand::CreateReg(0));
1491 OutStreamer.EmitInstruction(TmpInst);
1493 // Output the data for the jump table itself
1497 case ARM::BR_JTadd: {
1498 // Lower and emit the instruction itself, then the jump table following it.
1499 // add pc, target, idx
1500 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDrr)
1502 .addReg(MI->getOperand(0).getReg())
1503 .addReg(MI->getOperand(1).getReg())
1504 // Add predicate operands.
1507 // Add 's' bit operand (always reg0 for this)
1510 // Output the data for the jump table itself
1515 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1516 // FIXME: Remove this special case when they do.
1517 if (!Subtarget->isTargetDarwin()) {
1518 //.long 0xe7ffdefe @ trap
1519 uint32_t Val = 0xe7ffdefeUL;
1520 OutStreamer.AddComment("trap");
1521 OutStreamer.EmitIntValue(Val, 4);
1526 case ARM::TRAPNaCl: {
1527 //.long 0xe7fedef0 @ trap
1528 uint32_t Val = 0xe7fedef0UL;
1529 OutStreamer.AddComment("trap");
1530 OutStreamer.EmitIntValue(Val, 4);
1534 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1535 // FIXME: Remove this special case when they do.
1536 if (!Subtarget->isTargetDarwin()) {
1537 //.short 57086 @ trap
1538 uint16_t Val = 0xdefe;
1539 OutStreamer.AddComment("trap");
1540 OutStreamer.EmitIntValue(Val, 2);
1545 case ARM::t2Int_eh_sjlj_setjmp:
1546 case ARM::t2Int_eh_sjlj_setjmp_nofp:
1547 case ARM::tInt_eh_sjlj_setjmp: {
1548 // Two incoming args: GPR:$src, GPR:$val
1551 // str $val, [$src, #4]
1556 unsigned SrcReg = MI->getOperand(0).getReg();
1557 unsigned ValReg = MI->getOperand(1).getReg();
1558 MCSymbol *Label = GetARMSJLJEHLabel();
1559 OutStreamer.AddComment("eh_setjmp begin");
1560 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr)
1567 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tADDi3)
1577 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tSTRi)
1580 // The offset immediate is #4. The operand value is scaled by 4 for the
1581 // tSTR instruction.
1587 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVi8)
1595 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1596 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tB)
1597 .addExpr(SymbolExpr)
1601 OutStreamer.AddComment("eh_setjmp end");
1602 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVi8)
1610 OutStreamer.EmitLabel(Label);
1614 case ARM::Int_eh_sjlj_setjmp_nofp:
1615 case ARM::Int_eh_sjlj_setjmp: {
1616 // Two incoming args: GPR:$src, GPR:$val
1618 // str $val, [$src, #+4]
1622 unsigned SrcReg = MI->getOperand(0).getReg();
1623 unsigned ValReg = MI->getOperand(1).getReg();
1625 OutStreamer.AddComment("eh_setjmp begin");
1626 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDri)
1633 // 's' bit operand (always reg0 for this).
1636 OutStreamer.EmitInstruction(MCInstBuilder(ARM::STRi12)
1644 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVi)
1650 // 's' bit operand (always reg0 for this).
1653 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDri)
1660 // 's' bit operand (always reg0 for this).
1663 OutStreamer.AddComment("eh_setjmp end");
1664 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVi)
1670 // 's' bit operand (always reg0 for this).
1674 case ARM::Int_eh_sjlj_longjmp: {
1675 // ldr sp, [$src, #8]
1676 // ldr $scratch, [$src, #4]
1679 unsigned SrcReg = MI->getOperand(0).getReg();
1680 unsigned ScratchReg = MI->getOperand(1).getReg();
1681 OutStreamer.EmitInstruction(MCInstBuilder(ARM::LDRi12)
1689 OutStreamer.EmitInstruction(MCInstBuilder(ARM::LDRi12)
1697 OutStreamer.EmitInstruction(MCInstBuilder(ARM::LDRi12)
1705 OutStreamer.EmitInstruction(MCInstBuilder(ARM::BX)
1712 case ARM::tInt_eh_sjlj_longjmp: {
1713 // ldr $scratch, [$src, #8]
1715 // ldr $scratch, [$src, #4]
1718 unsigned SrcReg = MI->getOperand(0).getReg();
1719 unsigned ScratchReg = MI->getOperand(1).getReg();
1720 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tLDRi)
1723 // The offset immediate is #8. The operand value is scaled by 4 for the
1724 // tLDR instruction.
1730 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr)
1737 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tLDRi)
1745 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tLDRi)
1753 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tBX)
1763 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
1765 OutStreamer.EmitInstruction(TmpInst);
1768 //===----------------------------------------------------------------------===//
1769 // Target Registry Stuff
1770 //===----------------------------------------------------------------------===//
1772 // Force static initialization.
1773 extern "C" void LLVMInitializeARMAsmPrinter() {
1774 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1775 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);