1 //===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains a printer that converts from our internal representation
10 // of machine-dependent LLVM code to GAS-format ARM assembly language.
12 //===----------------------------------------------------------------------===//
14 #include "ARMAsmPrinter.h"
16 #include "ARMConstantPoolValue.h"
17 #include "ARMMachineFunctionInfo.h"
18 #include "ARMTargetMachine.h"
19 #include "ARMTargetObjectFile.h"
20 #include "MCTargetDesc/ARMInstPrinter.h"
21 #include "MCTargetDesc/ARMMCExpr.h"
22 #include "TargetInfo/ARMTargetInfo.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/BinaryFormat/COFF.h"
25 #include "llvm/CodeGen/MachineJumpTableInfo.h"
26 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/Mangler.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/IR/Type.h"
32 #include "llvm/MC/MCAsmInfo.h"
33 #include "llvm/MC/MCAssembler.h"
34 #include "llvm/MC/MCContext.h"
35 #include "llvm/MC/MCELFStreamer.h"
36 #include "llvm/MC/MCInst.h"
37 #include "llvm/MC/MCInstBuilder.h"
38 #include "llvm/MC/MCObjectStreamer.h"
39 #include "llvm/MC/MCStreamer.h"
40 #include "llvm/MC/MCSymbol.h"
41 #include "llvm/MC/TargetRegistry.h"
42 #include "llvm/Support/ARMBuildAttributes.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include "llvm/Target/TargetMachine.h"
49 #define DEBUG_TYPE "asm-printer"
51 ARMAsmPrinter::ARMAsmPrinter(TargetMachine
&TM
,
52 std::unique_ptr
<MCStreamer
> Streamer
)
53 : AsmPrinter(TM
, std::move(Streamer
)), Subtarget(nullptr), AFI(nullptr),
54 MCP(nullptr), InConstantPool(false), OptimizationGoals(-1) {}
56 void ARMAsmPrinter::emitFunctionBodyEnd() {
57 // Make sure to terminate any constant pools that were at the end
61 InConstantPool
= false;
62 OutStreamer
->emitDataRegion(MCDR_DataRegionEnd
);
65 void ARMAsmPrinter::emitFunctionEntryLabel() {
66 if (AFI
->isThumbFunction()) {
67 OutStreamer
->emitAssemblerFlag(MCAF_Code16
);
68 OutStreamer
->emitThumbFunc(CurrentFnSym
);
70 OutStreamer
->emitAssemblerFlag(MCAF_Code32
);
73 // Emit symbol for CMSE non-secure entry point
74 if (AFI
->isCmseNSEntryFunction()) {
76 OutContext
.getOrCreateSymbol("__acle_se_" + CurrentFnSym
->getName());
77 emitLinkage(&MF
->getFunction(), S
);
78 OutStreamer
->emitSymbolAttribute(S
, MCSA_ELF_TypeFunction
);
79 OutStreamer
->emitLabel(S
);
81 AsmPrinter::emitFunctionEntryLabel();
84 void ARMAsmPrinter::emitXXStructor(const DataLayout
&DL
, const Constant
*CV
) {
85 uint64_t Size
= getDataLayout().getTypeAllocSize(CV
->getType());
86 assert(Size
&& "C++ constructor pointer had zero size!");
88 const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(CV
->stripPointerCasts());
89 assert(GV
&& "C++ constructor pointer was not a GlobalValue!");
91 const MCExpr
*E
= MCSymbolRefExpr::create(GetARMGVSymbol(GV
,
93 (Subtarget
->isTargetELF()
94 ? MCSymbolRefExpr::VK_ARM_TARGET1
95 : MCSymbolRefExpr::VK_None
),
98 OutStreamer
->emitValue(E
, Size
);
101 void ARMAsmPrinter::emitGlobalVariable(const GlobalVariable
*GV
) {
102 if (PromotedGlobals
.count(GV
))
103 // The global was promoted into a constant pool. It should not be emitted.
105 AsmPrinter::emitGlobalVariable(GV
);
108 /// runOnMachineFunction - This uses the emitInstruction()
109 /// method to print assembly for each instruction.
111 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction
&MF
) {
112 AFI
= MF
.getInfo
<ARMFunctionInfo
>();
113 MCP
= MF
.getConstantPool();
114 Subtarget
= &MF
.getSubtarget
<ARMSubtarget
>();
116 SetupMachineFunction(MF
);
117 const Function
&F
= MF
.getFunction();
118 const TargetMachine
& TM
= MF
.getTarget();
120 // Collect all globals that had their storage promoted to a constant pool.
121 // Functions are emitted before variables, so this accumulates promoted
122 // globals from all functions in PromotedGlobals.
123 for (const auto *GV
: AFI
->getGlobalsPromotedToConstantPool())
124 PromotedGlobals
.insert(GV
);
126 // Calculate this function's optimization goal.
127 unsigned OptimizationGoal
;
129 // For best debugging illusion, speed and small size sacrificed
130 OptimizationGoal
= 6;
131 else if (F
.hasMinSize())
132 // Aggressively for small size, speed and debug illusion sacrificed
133 OptimizationGoal
= 4;
134 else if (F
.hasOptSize())
135 // For small size, but speed and debugging illusion preserved
136 OptimizationGoal
= 3;
137 else if (TM
.getOptLevel() == CodeGenOptLevel::Aggressive
)
138 // Aggressively for speed, small size and debug illusion sacrificed
139 OptimizationGoal
= 2;
140 else if (TM
.getOptLevel() > CodeGenOptLevel::None
)
141 // For speed, but small size and good debug illusion preserved
142 OptimizationGoal
= 1;
143 else // TM.getOptLevel() == CodeGenOptLevel::None
144 // For good debugging, but speed and small size preserved
145 OptimizationGoal
= 5;
147 // Combine a new optimization goal with existing ones.
148 if (OptimizationGoals
== -1) // uninitialized goals
149 OptimizationGoals
= OptimizationGoal
;
150 else if (OptimizationGoals
!= (int)OptimizationGoal
) // conflicting goals
151 OptimizationGoals
= 0;
153 if (Subtarget
->isTargetCOFF()) {
154 bool Local
= F
.hasLocalLinkage();
155 COFF::SymbolStorageClass Scl
=
156 Local
? COFF::IMAGE_SYM_CLASS_STATIC
: COFF::IMAGE_SYM_CLASS_EXTERNAL
;
157 int Type
= COFF::IMAGE_SYM_DTYPE_FUNCTION
<< COFF::SCT_COMPLEX_TYPE_SHIFT
;
159 OutStreamer
->beginCOFFSymbolDef(CurrentFnSym
);
160 OutStreamer
->emitCOFFSymbolStorageClass(Scl
);
161 OutStreamer
->emitCOFFSymbolType(Type
);
162 OutStreamer
->endCOFFSymbolDef();
165 // Emit the rest of the function body.
168 // Emit the XRay table for this function.
171 // If we need V4T thumb mode Register Indirect Jump pads, emit them.
172 // These are created per function, rather than per TU, since it's
173 // relatively easy to exceed the thumb branch range within a TU.
174 if (! ThumbIndirectPads
.empty()) {
175 OutStreamer
->emitAssemblerFlag(MCAF_Code16
);
176 emitAlignment(Align(2));
177 for (std::pair
<unsigned, MCSymbol
*> &TIP
: ThumbIndirectPads
) {
178 OutStreamer
->emitLabel(TIP
.second
);
179 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tBX
)
181 // Add predicate operands.
185 ThumbIndirectPads
.clear();
188 // We didn't modify anything.
192 void ARMAsmPrinter::PrintSymbolOperand(const MachineOperand
&MO
,
194 assert(MO
.isGlobal() && "caller should check MO.isGlobal");
195 unsigned TF
= MO
.getTargetFlags();
196 if (TF
& ARMII::MO_LO16
)
198 else if (TF
& ARMII::MO_HI16
)
200 else if (TF
& ARMII::MO_LO_0_7
)
202 else if (TF
& ARMII::MO_LO_8_15
)
204 else if (TF
& ARMII::MO_HI_0_7
)
206 else if (TF
& ARMII::MO_HI_8_15
)
209 GetARMGVSymbol(MO
.getGlobal(), TF
)->print(O
, MAI
);
210 printOffset(MO
.getOffset(), O
);
213 void ARMAsmPrinter::printOperand(const MachineInstr
*MI
, int OpNum
,
215 const MachineOperand
&MO
= MI
->getOperand(OpNum
);
217 switch (MO
.getType()) {
218 default: llvm_unreachable("<unknown operand type>");
219 case MachineOperand::MO_Register
: {
220 Register Reg
= MO
.getReg();
221 assert(Reg
.isPhysical());
222 assert(!MO
.getSubReg() && "Subregs should be eliminated!");
223 if(ARM::GPRPairRegClass
.contains(Reg
)) {
224 const MachineFunction
&MF
= *MI
->getParent()->getParent();
225 const TargetRegisterInfo
*TRI
= MF
.getSubtarget().getRegisterInfo();
226 Reg
= TRI
->getSubReg(Reg
, ARM::gsub_0
);
228 O
<< ARMInstPrinter::getRegisterName(Reg
);
231 case MachineOperand::MO_Immediate
: {
233 unsigned TF
= MO
.getTargetFlags();
234 if (TF
== ARMII::MO_LO16
)
236 else if (TF
== ARMII::MO_HI16
)
238 else if (TF
== ARMII::MO_LO_0_7
)
240 else if (TF
== ARMII::MO_LO_8_15
)
242 else if (TF
== ARMII::MO_HI_0_7
)
244 else if (TF
== ARMII::MO_HI_8_15
)
249 case MachineOperand::MO_MachineBasicBlock
:
250 MO
.getMBB()->getSymbol()->print(O
, MAI
);
252 case MachineOperand::MO_GlobalAddress
: {
253 PrintSymbolOperand(MO
, O
);
256 case MachineOperand::MO_ConstantPoolIndex
:
257 if (Subtarget
->genExecuteOnly())
258 llvm_unreachable("execute-only should not generate constant pools");
259 GetCPISymbol(MO
.getIndex())->print(O
, MAI
);
264 MCSymbol
*ARMAsmPrinter::GetCPISymbol(unsigned CPID
) const {
265 // The AsmPrinter::GetCPISymbol superclass method tries to use CPID as
266 // indexes in MachineConstantPool, which isn't in sync with indexes used here.
267 const DataLayout
&DL
= getDataLayout();
268 return OutContext
.getOrCreateSymbol(Twine(DL
.getPrivateGlobalPrefix()) +
269 "CPI" + Twine(getFunctionNumber()) + "_" +
273 //===--------------------------------------------------------------------===//
275 MCSymbol
*ARMAsmPrinter::
276 GetARMJTIPICJumpTableLabel(unsigned uid
) const {
277 const DataLayout
&DL
= getDataLayout();
278 SmallString
<60> Name
;
279 raw_svector_ostream(Name
) << DL
.getPrivateGlobalPrefix() << "JTI"
280 << getFunctionNumber() << '_' << uid
;
281 return OutContext
.getOrCreateSymbol(Name
);
284 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNum
,
285 const char *ExtraCode
, raw_ostream
&O
) {
286 // Does this asm operand have a single letter operand modifier?
287 if (ExtraCode
&& ExtraCode
[0]) {
288 if (ExtraCode
[1] != 0) return true; // Unknown modifier.
290 switch (ExtraCode
[0]) {
292 // See if this is a generic print operand
293 return AsmPrinter::PrintAsmOperand(MI
, OpNum
, ExtraCode
, O
);
294 case 'P': // Print a VFP double precision register.
295 case 'q': // Print a NEON quad precision register.
296 printOperand(MI
, OpNum
, O
);
298 case 'y': // Print a VFP single precision register as indexed double.
299 if (MI
->getOperand(OpNum
).isReg()) {
300 MCRegister Reg
= MI
->getOperand(OpNum
).getReg().asMCReg();
301 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
302 // Find the 'd' register that has this 's' register as a sub-register,
303 // and determine the lane number.
304 for (MCPhysReg SR
: TRI
->superregs(Reg
)) {
305 if (!ARM::DPRRegClass
.contains(SR
))
307 bool Lane0
= TRI
->getSubReg(SR
, ARM::ssub_0
) == Reg
;
308 O
<< ARMInstPrinter::getRegisterName(SR
) << (Lane0
? "[0]" : "[1]");
313 case 'B': // Bitwise inverse of integer or symbol without a preceding #.
314 if (!MI
->getOperand(OpNum
).isImm())
316 O
<< ~(MI
->getOperand(OpNum
).getImm());
318 case 'L': // The low 16 bits of an immediate constant.
319 if (!MI
->getOperand(OpNum
).isImm())
321 O
<< (MI
->getOperand(OpNum
).getImm() & 0xffff);
323 case 'M': { // A register range suitable for LDM/STM.
324 if (!MI
->getOperand(OpNum
).isReg())
326 const MachineOperand
&MO
= MI
->getOperand(OpNum
);
327 Register RegBegin
= MO
.getReg();
328 // This takes advantage of the 2 operand-ness of ldm/stm and that we've
329 // already got the operands in registers that are operands to the
330 // inline asm statement.
332 if (ARM::GPRPairRegClass
.contains(RegBegin
)) {
333 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
334 Register Reg0
= TRI
->getSubReg(RegBegin
, ARM::gsub_0
);
335 O
<< ARMInstPrinter::getRegisterName(Reg0
) << ", ";
336 RegBegin
= TRI
->getSubReg(RegBegin
, ARM::gsub_1
);
338 O
<< ARMInstPrinter::getRegisterName(RegBegin
);
340 // FIXME: The register allocator not only may not have given us the
341 // registers in sequence, but may not be in ascending registers. This
342 // will require changes in the register allocator that'll need to be
343 // propagated down here if the operands change.
344 unsigned RegOps
= OpNum
+ 1;
345 while (MI
->getOperand(RegOps
).isReg()) {
347 << ARMInstPrinter::getRegisterName(MI
->getOperand(RegOps
).getReg());
355 case 'R': // The most significant register of a pair.
356 case 'Q': { // The least significant register of a pair.
359 const MachineOperand
&FlagsOP
= MI
->getOperand(OpNum
- 1);
360 if (!FlagsOP
.isImm())
362 InlineAsm::Flag
F(FlagsOP
.getImm());
364 // This operand may not be the one that actually provides the register. If
365 // it's tied to a previous one then we should refer instead to that one
366 // for registers and their classes.
368 if (F
.isUseOperandTiedToDef(TiedIdx
)) {
369 for (OpNum
= InlineAsm::MIOp_FirstOperand
; TiedIdx
; --TiedIdx
) {
370 unsigned OpFlags
= MI
->getOperand(OpNum
).getImm();
371 const InlineAsm::Flag
F(OpFlags
);
372 OpNum
+= F
.getNumOperandRegisters() + 1;
374 F
= InlineAsm::Flag(MI
->getOperand(OpNum
).getImm());
376 // Later code expects OpNum to be pointing at the register rather than
381 const unsigned NumVals
= F
.getNumOperandRegisters();
384 const ARMBaseTargetMachine
&ATM
=
385 static_cast<const ARMBaseTargetMachine
&>(TM
);
387 // 'Q' should correspond to the low order register and 'R' to the high
388 // order register. Whether this corresponds to the upper or lower half
389 // depends on the endianess mode.
390 if (ExtraCode
[0] == 'Q')
391 FirstHalf
= ATM
.isLittleEndian();
393 // ExtraCode[0] == 'R'.
394 FirstHalf
= !ATM
.isLittleEndian();
395 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
396 if (F
.hasRegClassConstraint(RC
) &&
397 ARM::GPRPairRegClass
.hasSubClassEq(TRI
->getRegClass(RC
))) {
400 const MachineOperand
&MO
= MI
->getOperand(OpNum
);
403 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
405 TRI
->getSubReg(MO
.getReg(), FirstHalf
? ARM::gsub_0
: ARM::gsub_1
);
406 O
<< ARMInstPrinter::getRegisterName(Reg
);
411 unsigned RegOp
= FirstHalf
? OpNum
: OpNum
+ 1;
412 if (RegOp
>= MI
->getNumOperands())
414 const MachineOperand
&MO
= MI
->getOperand(RegOp
);
417 Register Reg
= MO
.getReg();
418 O
<< ARMInstPrinter::getRegisterName(Reg
);
422 case 'e': // The low doubleword register of a NEON quad register.
423 case 'f': { // The high doubleword register of a NEON quad register.
424 if (!MI
->getOperand(OpNum
).isReg())
426 Register Reg
= MI
->getOperand(OpNum
).getReg();
427 if (!ARM::QPRRegClass
.contains(Reg
))
429 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
431 TRI
->getSubReg(Reg
, ExtraCode
[0] == 'e' ? ARM::dsub_0
: ARM::dsub_1
);
432 O
<< ARMInstPrinter::getRegisterName(SubReg
);
436 // This modifier is not yet supported.
437 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
439 case 'H': { // The highest-numbered register of a pair.
440 const MachineOperand
&MO
= MI
->getOperand(OpNum
);
443 const MachineFunction
&MF
= *MI
->getParent()->getParent();
444 const TargetRegisterInfo
*TRI
= MF
.getSubtarget().getRegisterInfo();
445 Register Reg
= MO
.getReg();
446 if(!ARM::GPRPairRegClass
.contains(Reg
))
448 Reg
= TRI
->getSubReg(Reg
, ARM::gsub_1
);
449 O
<< ARMInstPrinter::getRegisterName(Reg
);
455 printOperand(MI
, OpNum
, O
);
459 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr
*MI
,
460 unsigned OpNum
, const char *ExtraCode
,
462 // Does this asm operand have a single letter operand modifier?
463 if (ExtraCode
&& ExtraCode
[0]) {
464 if (ExtraCode
[1] != 0) return true; // Unknown modifier.
466 switch (ExtraCode
[0]) {
467 case 'A': // A memory operand for a VLD1/VST1 instruction.
468 default: return true; // Unknown modifier.
469 case 'm': // The base register of a memory operand.
470 if (!MI
->getOperand(OpNum
).isReg())
472 O
<< ARMInstPrinter::getRegisterName(MI
->getOperand(OpNum
).getReg());
477 const MachineOperand
&MO
= MI
->getOperand(OpNum
);
478 assert(MO
.isReg() && "unexpected inline asm memory operand");
479 O
<< "[" << ARMInstPrinter::getRegisterName(MO
.getReg()) << "]";
483 static bool isThumb(const MCSubtargetInfo
& STI
) {
484 return STI
.hasFeature(ARM::ModeThumb
);
487 void ARMAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo
&StartInfo
,
488 const MCSubtargetInfo
*EndInfo
) const {
489 // If either end mode is unknown (EndInfo == NULL) or different than
490 // the start mode, then restore the start mode.
491 const bool WasThumb
= isThumb(StartInfo
);
492 if (!EndInfo
|| WasThumb
!= isThumb(*EndInfo
)) {
493 OutStreamer
->emitAssemblerFlag(WasThumb
? MCAF_Code16
: MCAF_Code32
);
497 void ARMAsmPrinter::emitStartOfAsmFile(Module
&M
) {
498 const Triple
&TT
= TM
.getTargetTriple();
499 // Use unified assembler syntax.
500 OutStreamer
->emitAssemblerFlag(MCAF_SyntaxUnified
);
502 // Emit ARM Build Attributes
503 if (TT
.isOSBinFormatELF())
506 // Use the triple's architecture and subarchitecture to determine
507 // if we're thumb for the purposes of the top level code16 assembler
509 if (!M
.getModuleInlineAsm().empty() && TT
.isThumb())
510 OutStreamer
->emitAssemblerFlag(MCAF_Code16
);
514 emitNonLazySymbolPointer(MCStreamer
&OutStreamer
, MCSymbol
*StubLabel
,
515 MachineModuleInfoImpl::StubValueTy
&MCSym
) {
517 OutStreamer
.emitLabel(StubLabel
);
518 // .indirect_symbol _foo
519 OutStreamer
.emitSymbolAttribute(MCSym
.getPointer(), MCSA_IndirectSymbol
);
522 // External to current translation unit.
523 OutStreamer
.emitIntValue(0, 4/*size*/);
525 // Internal to current translation unit.
527 // When we place the LSDA into the TEXT section, the type info
528 // pointers need to be indirect and pc-rel. We accomplish this by
529 // using NLPs; however, sometimes the types are local to the file.
530 // We need to fill in the value for the NLP in those cases.
531 OutStreamer
.emitValue(
532 MCSymbolRefExpr::create(MCSym
.getPointer(), OutStreamer
.getContext()),
537 void ARMAsmPrinter::emitEndOfAsmFile(Module
&M
) {
538 const Triple
&TT
= TM
.getTargetTriple();
539 if (TT
.isOSBinFormatMachO()) {
540 // All darwin targets use mach-o.
541 const TargetLoweringObjectFileMachO
&TLOFMacho
=
542 static_cast<const TargetLoweringObjectFileMachO
&>(getObjFileLowering());
543 MachineModuleInfoMachO
&MMIMacho
=
544 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
546 // Output non-lazy-pointers for external and common global variables.
547 MachineModuleInfoMachO::SymbolListTy Stubs
= MMIMacho
.GetGVStubList();
549 if (!Stubs
.empty()) {
550 // Switch with ".non_lazy_symbol_pointer" directive.
551 OutStreamer
->switchSection(TLOFMacho
.getNonLazySymbolPointerSection());
552 emitAlignment(Align(4));
554 for (auto &Stub
: Stubs
)
555 emitNonLazySymbolPointer(*OutStreamer
, Stub
.first
, Stub
.second
);
558 OutStreamer
->addBlankLine();
561 Stubs
= MMIMacho
.GetThreadLocalGVStubList();
562 if (!Stubs
.empty()) {
563 // Switch with ".non_lazy_symbol_pointer" directive.
564 OutStreamer
->switchSection(TLOFMacho
.getThreadLocalPointerSection());
565 emitAlignment(Align(4));
567 for (auto &Stub
: Stubs
)
568 emitNonLazySymbolPointer(*OutStreamer
, Stub
.first
, Stub
.second
);
571 OutStreamer
->addBlankLine();
574 // Funny Darwin hack: This flag tells the linker that no global symbols
575 // contain code that falls through to other global symbols (e.g. the obvious
576 // implementation of multiple entry points). If this doesn't occur, the
577 // linker can safely perform dead code stripping. Since LLVM never
578 // generates code that does this, it is always safe to set.
579 OutStreamer
->emitAssemblerFlag(MCAF_SubsectionsViaSymbols
);
582 // The last attribute to be emitted is ABI_optimization_goals
583 MCTargetStreamer
&TS
= *OutStreamer
->getTargetStreamer();
584 ARMTargetStreamer
&ATS
= static_cast<ARMTargetStreamer
&>(TS
);
586 if (OptimizationGoals
> 0 &&
587 (Subtarget
->isTargetAEABI() || Subtarget
->isTargetGNUAEABI() ||
588 Subtarget
->isTargetMuslAEABI()))
589 ATS
.emitAttribute(ARMBuildAttrs::ABI_optimization_goals
, OptimizationGoals
);
590 OptimizationGoals
= -1;
592 ATS
.finishAttributeSection();
595 //===----------------------------------------------------------------------===//
596 // Helper routines for emitStartOfAsmFile() and emitEndOfAsmFile()
598 // The following seem like one-off assembler flags, but they actually need
599 // to appear in the .ARM.attributes section in ELF.
600 // Instead of subclassing the MCELFStreamer, we do the work here.
602 // Returns true if all functions have the same function attribute value.
603 // It also returns true when the module has no functions.
604 static bool checkFunctionsAttributeConsistency(const Module
&M
, StringRef Attr
,
606 return !any_of(M
, [&](const Function
&F
) {
607 return F
.getFnAttribute(Attr
).getValueAsString() != Value
;
610 // Returns true if all functions have the same denormal mode.
611 // It also returns true when the module has no functions.
612 static bool checkDenormalAttributeConsistency(const Module
&M
,
614 DenormalMode Value
) {
615 return !any_of(M
, [&](const Function
&F
) {
616 StringRef AttrVal
= F
.getFnAttribute(Attr
).getValueAsString();
617 return parseDenormalFPAttribute(AttrVal
) != Value
;
621 void ARMAsmPrinter::emitAttributes() {
622 MCTargetStreamer
&TS
= *OutStreamer
->getTargetStreamer();
623 ARMTargetStreamer
&ATS
= static_cast<ARMTargetStreamer
&>(TS
);
625 ATS
.emitTextAttribute(ARMBuildAttrs::conformance
, "2.09");
627 ATS
.switchVendor("aeabi");
629 // Compute ARM ELF Attributes based on the default subtarget that
630 // we'd have constructed. The existing ARM behavior isn't LTO clean
632 // FIXME: For ifunc related functions we could iterate over and look
633 // for a feature string that doesn't match the default one.
634 const Triple
&TT
= TM
.getTargetTriple();
635 StringRef CPU
= TM
.getTargetCPU();
636 StringRef FS
= TM
.getTargetFeatureString();
637 std::string ArchFS
= ARM_MC::ParseARMTriple(TT
, CPU
);
640 ArchFS
= (Twine(ArchFS
) + "," + FS
).str();
642 ArchFS
= std::string(FS
);
644 const ARMBaseTargetMachine
&ATM
=
645 static_cast<const ARMBaseTargetMachine
&>(TM
);
646 const ARMSubtarget
STI(TT
, std::string(CPU
), ArchFS
, ATM
,
647 ATM
.isLittleEndian());
649 // Emit build attributes for the available hardware.
650 ATS
.emitTargetAttributes(STI
);
652 // RW data addressing.
653 if (isPositionIndependent()) {
654 ATS
.emitAttribute(ARMBuildAttrs::ABI_PCS_RW_data
,
655 ARMBuildAttrs::AddressRWPCRel
);
656 } else if (STI
.isRWPI()) {
657 // RWPI specific attributes.
658 ATS
.emitAttribute(ARMBuildAttrs::ABI_PCS_RW_data
,
659 ARMBuildAttrs::AddressRWSBRel
);
662 // RO data addressing.
663 if (isPositionIndependent() || STI
.isROPI()) {
664 ATS
.emitAttribute(ARMBuildAttrs::ABI_PCS_RO_data
,
665 ARMBuildAttrs::AddressROPCRel
);
669 if (isPositionIndependent()) {
670 ATS
.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use
,
671 ARMBuildAttrs::AddressGOT
);
673 ATS
.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use
,
674 ARMBuildAttrs::AddressDirect
);
678 if (checkDenormalAttributeConsistency(*MMI
->getModule(), "denormal-fp-math",
679 DenormalMode::getPreserveSign()))
680 ATS
.emitAttribute(ARMBuildAttrs::ABI_FP_denormal
,
681 ARMBuildAttrs::PreserveFPSign
);
682 else if (checkDenormalAttributeConsistency(*MMI
->getModule(),
684 DenormalMode::getPositiveZero()))
685 ATS
.emitAttribute(ARMBuildAttrs::ABI_FP_denormal
,
686 ARMBuildAttrs::PositiveZero
);
687 else if (!TM
.Options
.UnsafeFPMath
)
688 ATS
.emitAttribute(ARMBuildAttrs::ABI_FP_denormal
,
689 ARMBuildAttrs::IEEEDenormals
);
691 if (!STI
.hasVFP2Base()) {
692 // When the target doesn't have an FPU (by design or
693 // intention), the assumptions made on the software support
694 // mirror that of the equivalent hardware support *if it
695 // existed*. For v7 and better we indicate that denormals are
696 // flushed preserving sign, and for V6 we indicate that
697 // denormals are flushed to positive zero.
699 ATS
.emitAttribute(ARMBuildAttrs::ABI_FP_denormal
,
700 ARMBuildAttrs::PreserveFPSign
);
701 } else if (STI
.hasVFP3Base()) {
702 // In VFPv4, VFPv4U, VFPv3, or VFPv3U, it is preserved. That is,
703 // the sign bit of the zero matches the sign bit of the input or
704 // result that is being flushed to zero.
705 ATS
.emitAttribute(ARMBuildAttrs::ABI_FP_denormal
,
706 ARMBuildAttrs::PreserveFPSign
);
708 // For VFPv2 implementations it is implementation defined as
709 // to whether denormals are flushed to positive zero or to
710 // whatever the sign of zero is (ARM v7AR ARM 2.7.5). Historically
711 // LLVM has chosen to flush this to positive zero (most likely for
712 // GCC compatibility), so that's the chosen value here (the
713 // absence of its emission implies zero).
716 // Set FP exceptions and rounding
717 if (checkFunctionsAttributeConsistency(*MMI
->getModule(),
718 "no-trapping-math", "true") ||
719 TM
.Options
.NoTrappingFPMath
)
720 ATS
.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions
,
721 ARMBuildAttrs::Not_Allowed
);
722 else if (!TM
.Options
.UnsafeFPMath
) {
723 ATS
.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions
, ARMBuildAttrs::Allowed
);
725 // If the user has permitted this code to choose the IEEE 754
726 // rounding at run-time, emit the rounding attribute.
727 if (TM
.Options
.HonorSignDependentRoundingFPMathOption
)
728 ATS
.emitAttribute(ARMBuildAttrs::ABI_FP_rounding
, ARMBuildAttrs::Allowed
);
731 // TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath is the
732 // equivalent of GCC's -ffinite-math-only flag.
733 if (TM
.Options
.NoInfsFPMath
&& TM
.Options
.NoNaNsFPMath
)
734 ATS
.emitAttribute(ARMBuildAttrs::ABI_FP_number_model
,
735 ARMBuildAttrs::Allowed
);
737 ATS
.emitAttribute(ARMBuildAttrs::ABI_FP_number_model
,
738 ARMBuildAttrs::AllowIEEE754
);
740 // FIXME: add more flags to ARMBuildAttributes.h
741 // 8-bytes alignment stuff.
742 ATS
.emitAttribute(ARMBuildAttrs::ABI_align_needed
, 1);
743 ATS
.emitAttribute(ARMBuildAttrs::ABI_align_preserved
, 1);
745 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
746 if (STI
.isAAPCS_ABI() && TM
.Options
.FloatABIType
== FloatABI::Hard
)
747 ATS
.emitAttribute(ARMBuildAttrs::ABI_VFP_args
, ARMBuildAttrs::HardFPAAPCS
);
749 // FIXME: To support emitting this build attribute as GCC does, the
750 // -mfp16-format option and associated plumbing must be
751 // supported. For now the __fp16 type is exposed by default, so this
752 // attribute should be emitted with value 1.
753 ATS
.emitAttribute(ARMBuildAttrs::ABI_FP_16bit_format
,
754 ARMBuildAttrs::FP16FormatIEEE
);
756 if (const Module
*SourceModule
= MMI
->getModule()) {
757 // ABI_PCS_wchar_t to indicate wchar_t width
758 // FIXME: There is no way to emit value 0 (wchar_t prohibited).
759 if (auto WCharWidthValue
= mdconst::extract_or_null
<ConstantInt
>(
760 SourceModule
->getModuleFlag("wchar_size"))) {
761 int WCharWidth
= WCharWidthValue
->getZExtValue();
762 assert((WCharWidth
== 2 || WCharWidth
== 4) &&
763 "wchar_t width must be 2 or 4 bytes");
764 ATS
.emitAttribute(ARMBuildAttrs::ABI_PCS_wchar_t
, WCharWidth
);
767 // ABI_enum_size to indicate enum width
768 // FIXME: There is no way to emit value 0 (enums prohibited) or value 3
769 // (all enums contain a value needing 32 bits to encode).
770 if (auto EnumWidthValue
= mdconst::extract_or_null
<ConstantInt
>(
771 SourceModule
->getModuleFlag("min_enum_size"))) {
772 int EnumWidth
= EnumWidthValue
->getZExtValue();
773 assert((EnumWidth
== 1 || EnumWidth
== 4) &&
774 "Minimum enum width must be 1 or 4 bytes");
775 int EnumBuildAttr
= EnumWidth
== 1 ? 1 : 2;
776 ATS
.emitAttribute(ARMBuildAttrs::ABI_enum_size
, EnumBuildAttr
);
779 auto *PACValue
= mdconst::extract_or_null
<ConstantInt
>(
780 SourceModule
->getModuleFlag("sign-return-address"));
781 if (PACValue
&& PACValue
->isOne()) {
782 // If "+pacbti" is used as an architecture extension,
783 // Tag_PAC_extension is emitted in
784 // ARMTargetStreamer::emitTargetAttributes().
785 if (!STI
.hasPACBTI()) {
786 ATS
.emitAttribute(ARMBuildAttrs::PAC_extension
,
787 ARMBuildAttrs::AllowPACInNOPSpace
);
789 ATS
.emitAttribute(ARMBuildAttrs::PACRET_use
, ARMBuildAttrs::PACRETUsed
);
792 auto *BTIValue
= mdconst::extract_or_null
<ConstantInt
>(
793 SourceModule
->getModuleFlag("branch-target-enforcement"));
794 if (BTIValue
&& BTIValue
->isOne()) {
795 // If "+pacbti" is used as an architecture extension,
796 // Tag_BTI_extension is emitted in
797 // ARMTargetStreamer::emitTargetAttributes().
798 if (!STI
.hasPACBTI()) {
799 ATS
.emitAttribute(ARMBuildAttrs::BTI_extension
,
800 ARMBuildAttrs::AllowBTIInNOPSpace
);
802 ATS
.emitAttribute(ARMBuildAttrs::BTI_use
, ARMBuildAttrs::BTIUsed
);
806 // We currently do not support using R9 as the TLS pointer.
808 ATS
.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use
,
809 ARMBuildAttrs::R9IsSB
);
810 else if (STI
.isR9Reserved())
811 ATS
.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use
,
812 ARMBuildAttrs::R9Reserved
);
814 ATS
.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use
,
815 ARMBuildAttrs::R9IsGPR
);
818 //===----------------------------------------------------------------------===//
820 static MCSymbol
*getBFLabel(StringRef Prefix
, unsigned FunctionNumber
,
821 unsigned LabelId
, MCContext
&Ctx
) {
823 MCSymbol
*Label
= Ctx
.getOrCreateSymbol(Twine(Prefix
)
824 + "BF" + Twine(FunctionNumber
) + "_" + Twine(LabelId
));
828 static MCSymbol
*getPICLabel(StringRef Prefix
, unsigned FunctionNumber
,
829 unsigned LabelId
, MCContext
&Ctx
) {
831 MCSymbol
*Label
= Ctx
.getOrCreateSymbol(Twine(Prefix
)
832 + "PC" + Twine(FunctionNumber
) + "_" + Twine(LabelId
));
836 static MCSymbolRefExpr::VariantKind
837 getModifierVariantKind(ARMCP::ARMCPModifier Modifier
) {
839 case ARMCP::no_modifier
:
840 return MCSymbolRefExpr::VK_None
;
842 return MCSymbolRefExpr::VK_TLSGD
;
844 return MCSymbolRefExpr::VK_TPOFF
;
845 case ARMCP::GOTTPOFF
:
846 return MCSymbolRefExpr::VK_GOTTPOFF
;
848 return MCSymbolRefExpr::VK_ARM_SBREL
;
849 case ARMCP::GOT_PREL
:
850 return MCSymbolRefExpr::VK_ARM_GOT_PREL
;
852 return MCSymbolRefExpr::VK_SECREL
;
854 llvm_unreachable("Invalid ARMCPModifier!");
857 MCSymbol
*ARMAsmPrinter::GetARMGVSymbol(const GlobalValue
*GV
,
858 unsigned char TargetFlags
) {
859 if (Subtarget
->isTargetMachO()) {
861 (TargetFlags
& ARMII::MO_NONLAZY
) && Subtarget
->isGVIndirectSymbol(GV
);
864 return getSymbol(GV
);
866 // FIXME: Remove this when Darwin transition to @GOT like syntax.
867 MCSymbol
*MCSym
= getSymbolWithGlobalValueBase(GV
, "$non_lazy_ptr");
868 MachineModuleInfoMachO
&MMIMachO
=
869 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
870 MachineModuleInfoImpl::StubValueTy
&StubSym
=
871 GV
->isThreadLocal() ? MMIMachO
.getThreadLocalGVStubEntry(MCSym
)
872 : MMIMachO
.getGVStubEntry(MCSym
);
874 if (!StubSym
.getPointer())
875 StubSym
= MachineModuleInfoImpl::StubValueTy(getSymbol(GV
),
876 !GV
->hasInternalLinkage());
878 } else if (Subtarget
->isTargetCOFF()) {
879 assert(Subtarget
->isTargetWindows() &&
880 "Windows is the only supported COFF target");
883 (TargetFlags
& (ARMII::MO_DLLIMPORT
| ARMII::MO_COFFSTUB
));
885 return getSymbol(GV
);
887 SmallString
<128> Name
;
888 if (TargetFlags
& ARMII::MO_DLLIMPORT
)
890 else if (TargetFlags
& ARMII::MO_COFFSTUB
)
892 getNameWithPrefix(Name
, GV
);
894 MCSymbol
*MCSym
= OutContext
.getOrCreateSymbol(Name
);
896 if (TargetFlags
& ARMII::MO_COFFSTUB
) {
897 MachineModuleInfoCOFF
&MMICOFF
=
898 MMI
->getObjFileInfo
<MachineModuleInfoCOFF
>();
899 MachineModuleInfoImpl::StubValueTy
&StubSym
=
900 MMICOFF
.getGVStubEntry(MCSym
);
902 if (!StubSym
.getPointer())
903 StubSym
= MachineModuleInfoImpl::StubValueTy(getSymbol(GV
), true);
907 } else if (Subtarget
->isTargetELF()) {
908 return getSymbolPreferLocal(*GV
);
910 llvm_unreachable("unexpected target");
913 void ARMAsmPrinter::emitMachineConstantPoolValue(
914 MachineConstantPoolValue
*MCPV
) {
915 const DataLayout
&DL
= getDataLayout();
916 int Size
= DL
.getTypeAllocSize(MCPV
->getType());
918 ARMConstantPoolValue
*ACPV
= static_cast<ARMConstantPoolValue
*>(MCPV
);
920 if (ACPV
->isPromotedGlobal()) {
921 // This constant pool entry is actually a global whose storage has been
922 // promoted into the constant pool. This global may be referenced still
923 // by debug information, and due to the way AsmPrinter is set up, the debug
924 // info is immutable by the time we decide to promote globals to constant
925 // pools. Because of this, we need to ensure we emit a symbol for the global
926 // with private linkage (the default) so debug info can refer to it.
928 // However, if this global is promoted into several functions we must ensure
929 // we don't try and emit duplicate symbols!
930 auto *ACPC
= cast
<ARMConstantPoolConstant
>(ACPV
);
931 for (const auto *GV
: ACPC
->promotedGlobals()) {
932 if (!EmittedPromotedGlobalLabels
.count(GV
)) {
933 MCSymbol
*GVSym
= getSymbol(GV
);
934 OutStreamer
->emitLabel(GVSym
);
935 EmittedPromotedGlobalLabels
.insert(GV
);
938 return emitGlobalConstant(DL
, ACPC
->getPromotedGlobalInit());
942 if (ACPV
->isLSDA()) {
943 MCSym
= getMBBExceptionSym(MF
->front());
944 } else if (ACPV
->isBlockAddress()) {
945 const BlockAddress
*BA
=
946 cast
<ARMConstantPoolConstant
>(ACPV
)->getBlockAddress();
947 MCSym
= GetBlockAddressSymbol(BA
);
948 } else if (ACPV
->isGlobalValue()) {
949 const GlobalValue
*GV
= cast
<ARMConstantPoolConstant
>(ACPV
)->getGV();
951 // On Darwin, const-pool entries may get the "FOO$non_lazy_ptr" mangling, so
952 // flag the global as MO_NONLAZY.
953 unsigned char TF
= Subtarget
->isTargetMachO() ? ARMII::MO_NONLAZY
: 0;
954 MCSym
= GetARMGVSymbol(GV
, TF
);
955 } else if (ACPV
->isMachineBasicBlock()) {
956 const MachineBasicBlock
*MBB
= cast
<ARMConstantPoolMBB
>(ACPV
)->getMBB();
957 MCSym
= MBB
->getSymbol();
959 assert(ACPV
->isExtSymbol() && "unrecognized constant pool value");
960 auto Sym
= cast
<ARMConstantPoolSymbol
>(ACPV
)->getSymbol();
961 MCSym
= GetExternalSymbolSymbol(Sym
);
964 // Create an MCSymbol for the reference.
966 MCSymbolRefExpr::create(MCSym
, getModifierVariantKind(ACPV
->getModifier()),
969 if (ACPV
->getPCAdjustment()) {
971 getPICLabel(DL
.getPrivateGlobalPrefix(), getFunctionNumber(),
972 ACPV
->getLabelId(), OutContext
);
973 const MCExpr
*PCRelExpr
= MCSymbolRefExpr::create(PCLabel
, OutContext
);
975 MCBinaryExpr::createAdd(PCRelExpr
,
976 MCConstantExpr::create(ACPV
->getPCAdjustment(),
979 if (ACPV
->mustAddCurrentAddress()) {
980 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
981 // label, so just emit a local label end reference that instead.
982 MCSymbol
*DotSym
= OutContext
.createTempSymbol();
983 OutStreamer
->emitLabel(DotSym
);
984 const MCExpr
*DotExpr
= MCSymbolRefExpr::create(DotSym
, OutContext
);
985 PCRelExpr
= MCBinaryExpr::createSub(PCRelExpr
, DotExpr
, OutContext
);
987 Expr
= MCBinaryExpr::createSub(Expr
, PCRelExpr
, OutContext
);
989 OutStreamer
->emitValue(Expr
, Size
);
992 void ARMAsmPrinter::emitJumpTableAddrs(const MachineInstr
*MI
) {
993 const MachineOperand
&MO1
= MI
->getOperand(1);
994 unsigned JTI
= MO1
.getIndex();
996 // Make sure the Thumb jump table is 4-byte aligned. This will be a nop for
998 emitAlignment(Align(4));
1000 // Emit a label for the jump table.
1001 MCSymbol
*JTISymbol
= GetARMJTIPICJumpTableLabel(JTI
);
1002 OutStreamer
->emitLabel(JTISymbol
);
1004 // Mark the jump table as data-in-code.
1005 OutStreamer
->emitDataRegion(MCDR_DataRegionJT32
);
1007 // Emit each entry of the table.
1008 const MachineJumpTableInfo
*MJTI
= MF
->getJumpTableInfo();
1009 const std::vector
<MachineJumpTableEntry
> &JT
= MJTI
->getJumpTables();
1010 const std::vector
<MachineBasicBlock
*> &JTBBs
= JT
[JTI
].MBBs
;
1012 for (MachineBasicBlock
*MBB
: JTBBs
) {
1013 // Construct an MCExpr for the entry. We want a value of the form:
1014 // (BasicBlockAddr - TableBeginAddr)
1016 // For example, a table with entries jumping to basic blocks BB0 and BB1
1019 // .word (LBB0 - LJTI_0_0)
1020 // .word (LBB1 - LJTI_0_0)
1021 const MCExpr
*Expr
= MCSymbolRefExpr::create(MBB
->getSymbol(), OutContext
);
1023 if (isPositionIndependent() || Subtarget
->isROPI())
1024 Expr
= MCBinaryExpr::createSub(Expr
, MCSymbolRefExpr::create(JTISymbol
,
1027 // If we're generating a table of Thumb addresses in static relocation
1028 // model, we need to add one to keep interworking correctly.
1029 else if (AFI
->isThumbFunction())
1030 Expr
= MCBinaryExpr::createAdd(Expr
, MCConstantExpr::create(1,OutContext
),
1032 OutStreamer
->emitValue(Expr
, 4);
1034 // Mark the end of jump table data-in-code region.
1035 OutStreamer
->emitDataRegion(MCDR_DataRegionEnd
);
1038 void ARMAsmPrinter::emitJumpTableInsts(const MachineInstr
*MI
) {
1039 const MachineOperand
&MO1
= MI
->getOperand(1);
1040 unsigned JTI
= MO1
.getIndex();
1042 // Make sure the Thumb jump table is 4-byte aligned. This will be a nop for
1044 emitAlignment(Align(4));
1046 // Emit a label for the jump table.
1047 MCSymbol
*JTISymbol
= GetARMJTIPICJumpTableLabel(JTI
);
1048 OutStreamer
->emitLabel(JTISymbol
);
1050 // Emit each entry of the table.
1051 const MachineJumpTableInfo
*MJTI
= MF
->getJumpTableInfo();
1052 const std::vector
<MachineJumpTableEntry
> &JT
= MJTI
->getJumpTables();
1053 const std::vector
<MachineBasicBlock
*> &JTBBs
= JT
[JTI
].MBBs
;
1055 for (MachineBasicBlock
*MBB
: JTBBs
) {
1056 const MCExpr
*MBBSymbolExpr
= MCSymbolRefExpr::create(MBB
->getSymbol(),
1058 // If this isn't a TBB or TBH, the entries are direct branch instructions.
1059 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::t2B
)
1060 .addExpr(MBBSymbolExpr
)
1066 void ARMAsmPrinter::emitJumpTableTBInst(const MachineInstr
*MI
,
1067 unsigned OffsetWidth
) {
1068 assert((OffsetWidth
== 1 || OffsetWidth
== 2) && "invalid tbb/tbh width");
1069 const MachineOperand
&MO1
= MI
->getOperand(1);
1070 unsigned JTI
= MO1
.getIndex();
1072 if (Subtarget
->isThumb1Only())
1073 emitAlignment(Align(4));
1075 MCSymbol
*JTISymbol
= GetARMJTIPICJumpTableLabel(JTI
);
1076 OutStreamer
->emitLabel(JTISymbol
);
1078 // Emit each entry of the table.
1079 const MachineJumpTableInfo
*MJTI
= MF
->getJumpTableInfo();
1080 const std::vector
<MachineJumpTableEntry
> &JT
= MJTI
->getJumpTables();
1081 const std::vector
<MachineBasicBlock
*> &JTBBs
= JT
[JTI
].MBBs
;
1083 // Mark the jump table as data-in-code.
1084 OutStreamer
->emitDataRegion(OffsetWidth
== 1 ? MCDR_DataRegionJT8
1085 : MCDR_DataRegionJT16
);
1087 for (auto *MBB
: JTBBs
) {
1088 const MCExpr
*MBBSymbolExpr
= MCSymbolRefExpr::create(MBB
->getSymbol(),
1090 // Otherwise it's an offset from the dispatch instruction. Construct an
1091 // MCExpr for the entry. We want a value of the form:
1092 // (BasicBlockAddr - TBBInstAddr + 4) / 2
1094 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
1097 // .byte (LBB0 - (LCPI0_0 + 4)) / 2
1098 // .byte (LBB1 - (LCPI0_0 + 4)) / 2
1099 // where LCPI0_0 is a label defined just before the TBB instruction using
1101 MCSymbol
*TBInstPC
= GetCPISymbol(MI
->getOperand(0).getImm());
1102 const MCExpr
*Expr
= MCBinaryExpr::createAdd(
1103 MCSymbolRefExpr::create(TBInstPC
, OutContext
),
1104 MCConstantExpr::create(4, OutContext
), OutContext
);
1105 Expr
= MCBinaryExpr::createSub(MBBSymbolExpr
, Expr
, OutContext
);
1106 Expr
= MCBinaryExpr::createDiv(Expr
, MCConstantExpr::create(2, OutContext
),
1108 OutStreamer
->emitValue(Expr
, OffsetWidth
);
1110 // Mark the end of jump table data-in-code region. 32-bit offsets use
1111 // actual branch instructions here, so we don't mark those as a data-region
1113 OutStreamer
->emitDataRegion(MCDR_DataRegionEnd
);
1115 // Make sure the next instruction is 2-byte aligned.
1116 emitAlignment(Align(2));
1119 std::tuple
<const MCSymbol
*, uint64_t, const MCSymbol
*,
1120 codeview::JumpTableEntrySize
>
1121 ARMAsmPrinter::getCodeViewJumpTableInfo(int JTI
,
1122 const MachineInstr
*BranchInstr
,
1123 const MCSymbol
*BranchLabel
) const {
1124 codeview::JumpTableEntrySize EntrySize
;
1125 const MCSymbol
*BaseLabel
;
1126 uint64_t BaseOffset
= 0;
1127 switch (BranchInstr
->getOpcode()) {
1131 // Word relative to the jump table address.
1132 EntrySize
= codeview::JumpTableEntrySize::UInt32
;
1133 BaseLabel
= GetARMJTIPICJumpTableLabel(JTI
);
1137 // half-word shifted left, relative to *after* the branch instruction.
1138 EntrySize
= codeview::JumpTableEntrySize::UInt16ShiftLeft
;
1139 BranchLabel
= GetCPISymbol(BranchInstr
->getOperand(3).getImm());
1140 BaseLabel
= BranchLabel
;
1145 // byte shifted left, relative to *after* the branch instruction.
1146 EntrySize
= codeview::JumpTableEntrySize::UInt8ShiftLeft
;
1147 BranchLabel
= GetCPISymbol(BranchInstr
->getOperand(3).getImm());
1148 BaseLabel
= BranchLabel
;
1153 BaseLabel
= nullptr;
1154 EntrySize
= codeview::JumpTableEntrySize::Pointer
;
1157 llvm_unreachable("Unknown jump table instruction");
1160 return std::make_tuple(BaseLabel
, BaseOffset
, BranchLabel
, EntrySize
);
1163 void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr
*MI
) {
1164 assert(MI
->getFlag(MachineInstr::FrameSetup
) &&
1165 "Only instruction which are involved into frame setup code are allowed");
1167 MCTargetStreamer
&TS
= *OutStreamer
->getTargetStreamer();
1168 ARMTargetStreamer
&ATS
= static_cast<ARMTargetStreamer
&>(TS
);
1169 const MachineFunction
&MF
= *MI
->getParent()->getParent();
1170 const TargetRegisterInfo
*TargetRegInfo
=
1171 MF
.getSubtarget().getRegisterInfo();
1172 const MachineRegisterInfo
&MachineRegInfo
= MF
.getRegInfo();
1174 Register FramePtr
= TargetRegInfo
->getFrameRegister(MF
);
1175 unsigned Opc
= MI
->getOpcode();
1176 unsigned SrcReg
, DstReg
;
1180 // special case: tPUSH does not have src/dst regs.
1181 SrcReg
= DstReg
= ARM::SP
;
1185 case ARM::t2MOVTi16
:
1190 // 1) for Thumb1 code we sometimes materialize the constant via constpool
1192 // 2) for Thumb1 execute only code we materialize the constant via the
1193 // following pattern:
1194 // movs r3, #:upper8_15:<const>
1196 // adds r3, #:upper0_7:<const>
1198 // adds r3, #:lower8_15:<const>
1200 // adds r3, #:lower0_7:<const>
1201 // So we need to special-case MOVS, ADDS and LSLS, and keep track of
1202 // where we are in the sequence with the simplest of state machines.
1203 // 3) for Thumb2 execute only code we materialize the constant via
1204 // immediate constants in 2 separate instructions (MOVW/MOVT).
1206 DstReg
= MI
->getOperand(0).getReg();
1209 SrcReg
= MI
->getOperand(1).getReg();
1210 DstReg
= MI
->getOperand(0).getReg();
1214 // Try to figure out the unwinding opcode out of src / dst regs.
1215 if (MI
->mayStore()) {
1217 assert(DstReg
== ARM::SP
&&
1218 "Only stack pointer as a destination reg is supported");
1220 SmallVector
<MCRegister
, 4> RegList
;
1221 // Skip src & dst reg, and pred ops.
1222 unsigned StartOp
= 2 + 2;
1223 // Use all the operands.
1224 unsigned NumOffset
= 0;
1225 // Amount of SP adjustment folded into a push, before the
1226 // registers are stored (pad at higher addresses).
1227 unsigned PadBefore
= 0;
1228 // Amount of SP adjustment folded into a push, after the
1229 // registers are stored (pad at lower addresses).
1230 unsigned PadAfter
= 0;
1235 llvm_unreachable("Unsupported opcode for unwinding information");
1237 // Special case here: no src & dst reg, but two extra imp ops.
1238 StartOp
= 2; NumOffset
= 2;
1240 case ARM::STMDB_UPD
:
1241 case ARM::t2STMDB_UPD
:
1242 case ARM::VSTMDDB_UPD
:
1243 assert(SrcReg
== ARM::SP
&&
1244 "Only stack pointer as a source reg is supported");
1245 for (unsigned i
= StartOp
, NumOps
= MI
->getNumOperands() - NumOffset
;
1247 const MachineOperand
&MO
= MI
->getOperand(i
);
1248 // Actually, there should never be any impdef stuff here. Skip it
1249 // temporary to workaround PR11902.
1250 if (MO
.isImplicit())
1252 // Registers, pushed as a part of folding an SP update into the
1253 // push instruction are marked as undef and should not be
1254 // restored when unwinding, because the function can modify the
1255 // corresponding stack slots.
1257 assert(RegList
.empty() &&
1258 "Pad registers must come before restored ones");
1260 TargetRegInfo
->getRegSizeInBits(MO
.getReg(), MachineRegInfo
) / 8;
1264 // Check for registers that are remapped (for a Thumb1 prologue that
1265 // saves high registers).
1266 Register Reg
= MO
.getReg();
1267 if (unsigned RemappedReg
= AFI
->EHPrologueRemappedRegs
.lookup(Reg
))
1269 RegList
.push_back(Reg
);
1272 case ARM::STR_PRE_IMM
:
1273 case ARM::STR_PRE_REG
:
1274 case ARM::t2STR_PRE
:
1275 assert(MI
->getOperand(2).getReg() == ARM::SP
&&
1276 "Only stack pointer as a source reg is supported");
1277 if (unsigned RemappedReg
= AFI
->EHPrologueRemappedRegs
.lookup(SrcReg
))
1278 SrcReg
= RemappedReg
;
1280 RegList
.push_back(SrcReg
);
1282 case ARM::t2STRD_PRE
:
1283 assert(MI
->getOperand(3).getReg() == ARM::SP
&&
1284 "Only stack pointer as a source reg is supported");
1285 SrcReg
= MI
->getOperand(1).getReg();
1286 if (unsigned RemappedReg
= AFI
->EHPrologueRemappedRegs
.lookup(SrcReg
))
1287 SrcReg
= RemappedReg
;
1288 RegList
.push_back(SrcReg
);
1289 SrcReg
= MI
->getOperand(2).getReg();
1290 if (unsigned RemappedReg
= AFI
->EHPrologueRemappedRegs
.lookup(SrcReg
))
1291 SrcReg
= RemappedReg
;
1292 RegList
.push_back(SrcReg
);
1293 PadBefore
= -MI
->getOperand(4).getImm() - 8;
1296 if (MAI
->getExceptionHandlingType() == ExceptionHandling::ARM
) {
1298 ATS
.emitPad(PadBefore
);
1299 ATS
.emitRegSave(RegList
, Opc
== ARM::VSTMDDB_UPD
);
1300 // Account for the SP adjustment, folded into the push.
1302 ATS
.emitPad(PadAfter
);
1305 // Changes of stack / frame pointer.
1306 if (SrcReg
== ARM::SP
) {
1311 llvm_unreachable("Unsupported opcode for unwinding information");
1313 // Used to restore LR in a prologue which uses it as a temporary, has
1314 // no effect on unwind tables.
1322 case ARM::t2ADDri12
:
1323 case ARM::t2ADDspImm
:
1324 case ARM::t2ADDspImm12
:
1325 Offset
= -MI
->getOperand(2).getImm();
1329 case ARM::t2SUBri12
:
1330 case ARM::t2SUBspImm
:
1331 case ARM::t2SUBspImm12
:
1332 Offset
= MI
->getOperand(2).getImm();
1335 Offset
= MI
->getOperand(2).getImm()*4;
1339 Offset
= -MI
->getOperand(2).getImm()*4;
1343 -AFI
->EHPrologueOffsetInRegs
.lookup(MI
->getOperand(2).getReg());
1347 if (MAI
->getExceptionHandlingType() == ExceptionHandling::ARM
) {
1348 if (DstReg
== FramePtr
&& FramePtr
!= ARM::SP
)
1349 // Set-up of the frame pointer. Positive values correspond to "add"
1351 ATS
.emitSetFP(FramePtr
, ARM::SP
, -Offset
);
1352 else if (DstReg
== ARM::SP
) {
1353 // Change of SP by an offset. Positive values correspond to "sub"
1355 ATS
.emitPad(Offset
);
1357 // Move of SP to a register. Positive values correspond to an "add"
1359 ATS
.emitMovSP(DstReg
, -Offset
);
1362 } else if (DstReg
== ARM::SP
) {
1364 llvm_unreachable("Unsupported opcode for unwinding information");
1369 // If a Thumb1 function spills r8-r11, we copy the values to low
1370 // registers before pushing them. Record the copy so we can emit the
1371 // correct ".save" later.
1372 AFI
->EHPrologueRemappedRegs
[DstReg
] = SrcReg
;
1374 case ARM::tLDRpci
: {
1375 // Grab the constpool index and check, whether it corresponds to
1376 // original or cloned constpool entry.
1377 unsigned CPI
= MI
->getOperand(1).getIndex();
1378 const MachineConstantPool
*MCP
= MF
.getConstantPool();
1379 if (CPI
>= MCP
->getConstants().size())
1380 CPI
= AFI
->getOriginalCPIdx(CPI
);
1381 assert(CPI
!= -1U && "Invalid constpool index");
1383 // Derive the actual offset.
1384 const MachineConstantPoolEntry
&CPE
= MCP
->getConstants()[CPI
];
1385 assert(!CPE
.isMachineConstantPoolEntry() && "Invalid constpool entry");
1386 Offset
= cast
<ConstantInt
>(CPE
.Val
.ConstVal
)->getSExtValue();
1387 AFI
->EHPrologueOffsetInRegs
[DstReg
] = Offset
;
1391 Offset
= MI
->getOperand(1).getImm();
1392 AFI
->EHPrologueOffsetInRegs
[DstReg
] = Offset
;
1394 case ARM::t2MOVTi16
:
1395 Offset
= MI
->getOperand(2).getImm();
1396 AFI
->EHPrologueOffsetInRegs
[DstReg
] |= (Offset
<< 16);
1399 Offset
= MI
->getOperand(2).getImm();
1400 AFI
->EHPrologueOffsetInRegs
[DstReg
] = Offset
;
1403 assert(MI
->getOperand(3).getImm() == 8 &&
1404 "The shift amount is not equal to 8");
1405 assert(MI
->getOperand(2).getReg() == MI
->getOperand(0).getReg() &&
1406 "The source register is not equal to the destination register");
1407 AFI
->EHPrologueOffsetInRegs
[DstReg
] <<= 8;
1410 assert(MI
->getOperand(2).getReg() == MI
->getOperand(0).getReg() &&
1411 "The source register is not equal to the destination register");
1412 Offset
= MI
->getOperand(3).getImm();
1413 AFI
->EHPrologueOffsetInRegs
[DstReg
] += Offset
;
1417 AFI
->EHPrologueRemappedRegs
[ARM::R12
] = ARM::RA_AUTH_CODE
;
1421 llvm_unreachable("Unsupported opcode for unwinding information");
1427 // Simple pseudo-instructions have their lowering (with expansion to real
1428 // instructions) auto-generated.
1429 #include "ARMGenMCPseudoLowering.inc"
1431 void ARMAsmPrinter::emitInstruction(const MachineInstr
*MI
) {
1432 // TODOD FIXME: Enable feature predicate checks once all the test pass.
1433 // ARM_MC::verifyInstructionPredicates(MI->getOpcode(),
1434 // getSubtargetInfo().getFeatureBits());
1436 const DataLayout
&DL
= getDataLayout();
1437 MCTargetStreamer
&TS
= *OutStreamer
->getTargetStreamer();
1438 ARMTargetStreamer
&ATS
= static_cast<ARMTargetStreamer
&>(TS
);
1440 // If we just ended a constant pool, mark it as such.
1441 if (InConstantPool
&& MI
->getOpcode() != ARM::CONSTPOOL_ENTRY
) {
1442 OutStreamer
->emitDataRegion(MCDR_DataRegionEnd
);
1443 InConstantPool
= false;
1446 // Emit unwinding stuff for frame-related instructions
1447 if (Subtarget
->isTargetEHABICompatible() &&
1448 MI
->getFlag(MachineInstr::FrameSetup
))
1449 EmitUnwindingInstruction(MI
);
1451 // Do any auto-generated pseudo lowerings.
1452 if (MCInst OutInst
; lowerPseudoInstExpansion(MI
, OutInst
)) {
1453 EmitToStreamer(*OutStreamer
, OutInst
);
1457 assert(!convertAddSubFlagsOpcode(MI
->getOpcode()) &&
1458 "Pseudo flag setting opcode should be expanded early");
1460 // Check for manual lowerings.
1461 unsigned Opc
= MI
->getOpcode();
1463 case ARM::t2MOVi32imm
: llvm_unreachable("Should be lowered by thumb2it pass");
1464 case ARM::DBG_VALUE
: llvm_unreachable("Should be handled by generic printing");
1466 case ARM::tLEApcrel
:
1467 case ARM::t2LEApcrel
: {
1468 // FIXME: Need to also handle globals and externals
1469 MCSymbol
*CPISymbol
= GetCPISymbol(MI
->getOperand(1).getIndex());
1470 EmitToStreamer(*OutStreamer
, MCInstBuilder(MI
->getOpcode() ==
1471 ARM::t2LEApcrel
? ARM::t2ADR
1472 : (MI
->getOpcode() == ARM::tLEApcrel
? ARM::tADR
1474 .addReg(MI
->getOperand(0).getReg())
1475 .addExpr(MCSymbolRefExpr::create(CPISymbol
, OutContext
))
1476 // Add predicate operands.
1477 .addImm(MI
->getOperand(2).getImm())
1478 .addReg(MI
->getOperand(3).getReg()));
1481 case ARM::LEApcrelJT
:
1482 case ARM::tLEApcrelJT
:
1483 case ARM::t2LEApcrelJT
: {
1484 MCSymbol
*JTIPICSymbol
=
1485 GetARMJTIPICJumpTableLabel(MI
->getOperand(1).getIndex());
1486 EmitToStreamer(*OutStreamer
, MCInstBuilder(MI
->getOpcode() ==
1487 ARM::t2LEApcrelJT
? ARM::t2ADR
1488 : (MI
->getOpcode() == ARM::tLEApcrelJT
? ARM::tADR
1490 .addReg(MI
->getOperand(0).getReg())
1491 .addExpr(MCSymbolRefExpr::create(JTIPICSymbol
, OutContext
))
1492 // Add predicate operands.
1493 .addImm(MI
->getOperand(2).getImm())
1494 .addReg(MI
->getOperand(3).getReg()));
1497 // Darwin call instructions are just normal call instructions with different
1498 // clobber semantics (they clobber R9).
1499 case ARM::BX_CALL
: {
1500 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::MOVr
)
1503 // Add predicate operands.
1506 // Add 's' bit operand (always reg0 for this)
1509 assert(Subtarget
->hasV4TOps());
1510 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::BX
)
1511 .addReg(MI
->getOperand(0).getReg()));
1514 case ARM::tBX_CALL
: {
1515 if (Subtarget
->hasV5TOps())
1516 llvm_unreachable("Expected BLX to be selected for v5t+");
1518 // On ARM v4t, when doing a call from thumb mode, we need to ensure
1519 // that the saved lr has its LSB set correctly (the arch doesn't
1521 // So here we generate a bl to a small jump pad that does bx rN.
1522 // The jump pads are emitted after the function body.
1524 Register TReg
= MI
->getOperand(0).getReg();
1525 MCSymbol
*TRegSym
= nullptr;
1526 for (std::pair
<unsigned, MCSymbol
*> &TIP
: ThumbIndirectPads
) {
1527 if (TIP
.first
== TReg
) {
1528 TRegSym
= TIP
.second
;
1534 TRegSym
= OutContext
.createTempSymbol();
1535 ThumbIndirectPads
.push_back(std::make_pair(TReg
, TRegSym
));
1538 // Create a link-saving branch to the Reg Indirect Jump Pad.
1539 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tBL
)
1540 // Predicate comes first here.
1541 .addImm(ARMCC::AL
).addReg(0)
1542 .addExpr(MCSymbolRefExpr::create(TRegSym
, OutContext
)));
1545 case ARM::BMOVPCRX_CALL
: {
1546 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::MOVr
)
1549 // Add predicate operands.
1552 // Add 's' bit operand (always reg0 for this)
1555 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::MOVr
)
1557 .addReg(MI
->getOperand(0).getReg())
1558 // Add predicate operands.
1561 // Add 's' bit operand (always reg0 for this)
1565 case ARM::BMOVPCB_CALL
: {
1566 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::MOVr
)
1569 // Add predicate operands.
1572 // Add 's' bit operand (always reg0 for this)
1575 const MachineOperand
&Op
= MI
->getOperand(0);
1576 const GlobalValue
*GV
= Op
.getGlobal();
1577 const unsigned TF
= Op
.getTargetFlags();
1578 MCSymbol
*GVSym
= GetARMGVSymbol(GV
, TF
);
1579 const MCExpr
*GVSymExpr
= MCSymbolRefExpr::create(GVSym
, OutContext
);
1580 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::Bcc
)
1582 // Add predicate operands.
1587 case ARM::MOVi16_ga_pcrel
:
1588 case ARM::t2MOVi16_ga_pcrel
: {
1590 TmpInst
.setOpcode(Opc
== ARM::MOVi16_ga_pcrel
? ARM::MOVi16
: ARM::t2MOVi16
);
1591 TmpInst
.addOperand(MCOperand::createReg(MI
->getOperand(0).getReg()));
1593 unsigned TF
= MI
->getOperand(1).getTargetFlags();
1594 const GlobalValue
*GV
= MI
->getOperand(1).getGlobal();
1595 MCSymbol
*GVSym
= GetARMGVSymbol(GV
, TF
);
1596 const MCExpr
*GVSymExpr
= MCSymbolRefExpr::create(GVSym
, OutContext
);
1598 MCSymbol
*LabelSym
=
1599 getPICLabel(DL
.getPrivateGlobalPrefix(), getFunctionNumber(),
1600 MI
->getOperand(2).getImm(), OutContext
);
1601 const MCExpr
*LabelSymExpr
= MCSymbolRefExpr::create(LabelSym
, OutContext
);
1602 unsigned PCAdj
= (Opc
== ARM::MOVi16_ga_pcrel
) ? 8 : 4;
1603 const MCExpr
*PCRelExpr
=
1604 ARMMCExpr::createLower16(MCBinaryExpr::createSub(GVSymExpr
,
1605 MCBinaryExpr::createAdd(LabelSymExpr
,
1606 MCConstantExpr::create(PCAdj
, OutContext
),
1607 OutContext
), OutContext
), OutContext
);
1608 TmpInst
.addOperand(MCOperand::createExpr(PCRelExpr
));
1610 // Add predicate operands.
1611 TmpInst
.addOperand(MCOperand::createImm(ARMCC::AL
));
1612 TmpInst
.addOperand(MCOperand::createReg(0));
1613 // Add 's' bit operand (always reg0 for this)
1614 TmpInst
.addOperand(MCOperand::createReg(0));
1615 EmitToStreamer(*OutStreamer
, TmpInst
);
1618 case ARM::MOVTi16_ga_pcrel
:
1619 case ARM::t2MOVTi16_ga_pcrel
: {
1621 TmpInst
.setOpcode(Opc
== ARM::MOVTi16_ga_pcrel
1622 ? ARM::MOVTi16
: ARM::t2MOVTi16
);
1623 TmpInst
.addOperand(MCOperand::createReg(MI
->getOperand(0).getReg()));
1624 TmpInst
.addOperand(MCOperand::createReg(MI
->getOperand(1).getReg()));
1626 unsigned TF
= MI
->getOperand(2).getTargetFlags();
1627 const GlobalValue
*GV
= MI
->getOperand(2).getGlobal();
1628 MCSymbol
*GVSym
= GetARMGVSymbol(GV
, TF
);
1629 const MCExpr
*GVSymExpr
= MCSymbolRefExpr::create(GVSym
, OutContext
);
1631 MCSymbol
*LabelSym
=
1632 getPICLabel(DL
.getPrivateGlobalPrefix(), getFunctionNumber(),
1633 MI
->getOperand(3).getImm(), OutContext
);
1634 const MCExpr
*LabelSymExpr
= MCSymbolRefExpr::create(LabelSym
, OutContext
);
1635 unsigned PCAdj
= (Opc
== ARM::MOVTi16_ga_pcrel
) ? 8 : 4;
1636 const MCExpr
*PCRelExpr
=
1637 ARMMCExpr::createUpper16(MCBinaryExpr::createSub(GVSymExpr
,
1638 MCBinaryExpr::createAdd(LabelSymExpr
,
1639 MCConstantExpr::create(PCAdj
, OutContext
),
1640 OutContext
), OutContext
), OutContext
);
1641 TmpInst
.addOperand(MCOperand::createExpr(PCRelExpr
));
1642 // Add predicate operands.
1643 TmpInst
.addOperand(MCOperand::createImm(ARMCC::AL
));
1644 TmpInst
.addOperand(MCOperand::createReg(0));
1645 // Add 's' bit operand (always reg0 for this)
1646 TmpInst
.addOperand(MCOperand::createReg(0));
1647 EmitToStreamer(*OutStreamer
, TmpInst
);
1655 // This is a Branch Future instruction.
1657 const MCExpr
*BranchLabel
= MCSymbolRefExpr::create(
1658 getBFLabel(DL
.getPrivateGlobalPrefix(), getFunctionNumber(),
1659 MI
->getOperand(0).getIndex(), OutContext
),
1662 auto MCInst
= MCInstBuilder(Opc
).addExpr(BranchLabel
);
1663 if (MI
->getOperand(1).isReg()) {
1665 MCInst
.addReg(MI
->getOperand(1).getReg());
1667 // For BFi/BFLi/BFic
1668 const MCExpr
*BranchTarget
;
1669 if (MI
->getOperand(1).isMBB())
1670 BranchTarget
= MCSymbolRefExpr::create(
1671 MI
->getOperand(1).getMBB()->getSymbol(), OutContext
);
1672 else if (MI
->getOperand(1).isGlobal()) {
1673 const GlobalValue
*GV
= MI
->getOperand(1).getGlobal();
1674 BranchTarget
= MCSymbolRefExpr::create(
1675 GetARMGVSymbol(GV
, MI
->getOperand(1).getTargetFlags()), OutContext
);
1676 } else if (MI
->getOperand(1).isSymbol()) {
1677 BranchTarget
= MCSymbolRefExpr::create(
1678 GetExternalSymbolSymbol(MI
->getOperand(1).getSymbolName()),
1681 llvm_unreachable("Unhandled operand kind in Branch Future instruction");
1683 MCInst
.addExpr(BranchTarget
);
1686 if (Opc
== ARM::t2BFic
) {
1687 const MCExpr
*ElseLabel
= MCSymbolRefExpr::create(
1688 getBFLabel(DL
.getPrivateGlobalPrefix(), getFunctionNumber(),
1689 MI
->getOperand(2).getIndex(), OutContext
),
1691 MCInst
.addExpr(ElseLabel
);
1692 MCInst
.addImm(MI
->getOperand(3).getImm());
1694 MCInst
.addImm(MI
->getOperand(2).getImm())
1695 .addReg(MI
->getOperand(3).getReg());
1698 EmitToStreamer(*OutStreamer
, MCInst
);
1701 case ARM::t2BF_LabelPseudo
: {
1702 // This is a pseudo op for a label used by a branch future instruction
1705 OutStreamer
->emitLabel(getBFLabel(DL
.getPrivateGlobalPrefix(),
1706 getFunctionNumber(),
1707 MI
->getOperand(0).getIndex(), OutContext
));
1710 case ARM::tPICADD
: {
1711 // This is a pseudo op for a label + instruction sequence, which looks like:
1714 // This adds the address of LPC0 to r0.
1717 OutStreamer
->emitLabel(getPICLabel(DL
.getPrivateGlobalPrefix(),
1718 getFunctionNumber(),
1719 MI
->getOperand(2).getImm(), OutContext
));
1721 // Form and emit the add.
1722 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tADDhirr
)
1723 .addReg(MI
->getOperand(0).getReg())
1724 .addReg(MI
->getOperand(0).getReg())
1726 // Add predicate operands.
1732 // This is a pseudo op for a label + instruction sequence, which looks like:
1735 // This adds the address of LPC0 to r0.
1738 OutStreamer
->emitLabel(getPICLabel(DL
.getPrivateGlobalPrefix(),
1739 getFunctionNumber(),
1740 MI
->getOperand(2).getImm(), OutContext
));
1742 // Form and emit the add.
1743 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::ADDrr
)
1744 .addReg(MI
->getOperand(0).getReg())
1746 .addReg(MI
->getOperand(1).getReg())
1747 // Add predicate operands.
1748 .addImm(MI
->getOperand(3).getImm())
1749 .addReg(MI
->getOperand(4).getReg())
1750 // Add 's' bit operand (always reg0 for this)
1761 case ARM::PICLDRSH
: {
1762 // This is a pseudo op for a label + instruction sequence, which looks like:
1765 // The LCP0 label is referenced by a constant pool entry in order to get
1766 // a PC-relative address at the ldr instruction.
1769 OutStreamer
->emitLabel(getPICLabel(DL
.getPrivateGlobalPrefix(),
1770 getFunctionNumber(),
1771 MI
->getOperand(2).getImm(), OutContext
));
1773 // Form and emit the load
1775 switch (MI
->getOpcode()) {
1777 llvm_unreachable("Unexpected opcode!");
1778 case ARM::PICSTR
: Opcode
= ARM::STRrs
; break;
1779 case ARM::PICSTRB
: Opcode
= ARM::STRBrs
; break;
1780 case ARM::PICSTRH
: Opcode
= ARM::STRH
; break;
1781 case ARM::PICLDR
: Opcode
= ARM::LDRrs
; break;
1782 case ARM::PICLDRB
: Opcode
= ARM::LDRBrs
; break;
1783 case ARM::PICLDRH
: Opcode
= ARM::LDRH
; break;
1784 case ARM::PICLDRSB
: Opcode
= ARM::LDRSB
; break;
1785 case ARM::PICLDRSH
: Opcode
= ARM::LDRSH
; break;
1787 EmitToStreamer(*OutStreamer
, MCInstBuilder(Opcode
)
1788 .addReg(MI
->getOperand(0).getReg())
1790 .addReg(MI
->getOperand(1).getReg())
1792 // Add predicate operands.
1793 .addImm(MI
->getOperand(3).getImm())
1794 .addReg(MI
->getOperand(4).getReg()));
1798 case ARM::CONSTPOOL_ENTRY
: {
1799 if (Subtarget
->genExecuteOnly())
1800 llvm_unreachable("execute-only should not generate constant pools");
1802 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1803 /// in the function. The first operand is the ID# for this instruction, the
1804 /// second is the index into the MachineConstantPool that this is, the third
1805 /// is the size in bytes of this constant pool entry.
1806 /// The required alignment is specified on the basic block holding this MI.
1807 unsigned LabelId
= (unsigned)MI
->getOperand(0).getImm();
1808 unsigned CPIdx
= (unsigned)MI
->getOperand(1).getIndex();
1810 // If this is the first entry of the pool, mark it.
1811 if (!InConstantPool
) {
1812 OutStreamer
->emitDataRegion(MCDR_DataRegion
);
1813 InConstantPool
= true;
1816 OutStreamer
->emitLabel(GetCPISymbol(LabelId
));
1818 const MachineConstantPoolEntry
&MCPE
= MCP
->getConstants()[CPIdx
];
1819 if (MCPE
.isMachineConstantPoolEntry())
1820 emitMachineConstantPoolValue(MCPE
.Val
.MachineCPVal
);
1822 emitGlobalConstant(DL
, MCPE
.Val
.ConstVal
);
1825 case ARM::JUMPTABLE_ADDRS
:
1826 emitJumpTableAddrs(MI
);
1828 case ARM::JUMPTABLE_INSTS
:
1829 emitJumpTableInsts(MI
);
1831 case ARM::JUMPTABLE_TBB
:
1832 case ARM::JUMPTABLE_TBH
:
1833 emitJumpTableTBInst(MI
, MI
->getOpcode() == ARM::JUMPTABLE_TBB
? 1 : 2);
1835 case ARM::t2BR_JT
: {
1836 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tMOVr
)
1838 .addReg(MI
->getOperand(0).getReg())
1839 // Add predicate operands.
1845 case ARM::t2TBH_JT
: {
1846 unsigned Opc
= MI
->getOpcode() == ARM::t2TBB_JT
? ARM::t2TBB
: ARM::t2TBH
;
1847 // Lower and emit the PC label, then the instruction itself.
1848 OutStreamer
->emitLabel(GetCPISymbol(MI
->getOperand(3).getImm()));
1849 EmitToStreamer(*OutStreamer
, MCInstBuilder(Opc
)
1850 .addReg(MI
->getOperand(0).getReg())
1851 .addReg(MI
->getOperand(1).getReg())
1852 // Add predicate operands.
1858 case ARM::tTBH_JT
: {
1860 bool Is8Bit
= MI
->getOpcode() == ARM::tTBB_JT
;
1861 Register Base
= MI
->getOperand(0).getReg();
1862 Register Idx
= MI
->getOperand(1).getReg();
1863 assert(MI
->getOperand(1).isKill() && "We need the index register as scratch!");
1865 // Multiply up idx if necessary.
1867 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tLSLri
)
1872 // Add predicate operands.
1876 if (Base
== ARM::PC
) {
1877 // TBB [base, idx] =
1878 // ADDS idx, idx, base
1879 // LDRB idx, [idx, #4] ; or LDRH if TBH
1883 // When using PC as the base, it's important that there is no padding
1884 // between the last ADDS and the start of the jump table. The jump table
1885 // is 4-byte aligned, so we ensure we're 4 byte aligned here too.
1887 // FIXME: Ideally we could vary the LDRB index based on the padding
1888 // between the sequence and jump table, however that relies on MCExprs
1889 // for load indexes which are currently not supported.
1890 OutStreamer
->emitCodeAlignment(Align(4), &getSubtargetInfo());
1891 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tADDhirr
)
1895 // Add predicate operands.
1899 unsigned Opc
= Is8Bit
? ARM::tLDRBi
: ARM::tLDRHi
;
1900 EmitToStreamer(*OutStreamer
, MCInstBuilder(Opc
)
1903 .addImm(Is8Bit
? 4 : 2)
1904 // Add predicate operands.
1908 // TBB [base, idx] =
1909 // LDRB idx, [base, idx] ; or LDRH if TBH
1913 unsigned Opc
= Is8Bit
? ARM::tLDRBr
: ARM::tLDRHr
;
1914 EmitToStreamer(*OutStreamer
, MCInstBuilder(Opc
)
1918 // Add predicate operands.
1923 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tLSLri
)
1928 // Add predicate operands.
1932 OutStreamer
->emitLabel(GetCPISymbol(MI
->getOperand(3).getImm()));
1933 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tADDhirr
)
1937 // Add predicate operands.
1946 unsigned Opc
= MI
->getOpcode() == ARM::BR_JTr
?
1947 ARM::MOVr
: ARM::tMOVr
;
1948 TmpInst
.setOpcode(Opc
);
1949 TmpInst
.addOperand(MCOperand::createReg(ARM::PC
));
1950 TmpInst
.addOperand(MCOperand::createReg(MI
->getOperand(0).getReg()));
1951 // Add predicate operands.
1952 TmpInst
.addOperand(MCOperand::createImm(ARMCC::AL
));
1953 TmpInst
.addOperand(MCOperand::createReg(0));
1954 // Add 's' bit operand (always reg0 for this)
1955 if (Opc
== ARM::MOVr
)
1956 TmpInst
.addOperand(MCOperand::createReg(0));
1957 EmitToStreamer(*OutStreamer
, TmpInst
);
1960 case ARM::BR_JTm_i12
: {
1963 TmpInst
.setOpcode(ARM::LDRi12
);
1964 TmpInst
.addOperand(MCOperand::createReg(ARM::PC
));
1965 TmpInst
.addOperand(MCOperand::createReg(MI
->getOperand(0).getReg()));
1966 TmpInst
.addOperand(MCOperand::createImm(MI
->getOperand(2).getImm()));
1967 // Add predicate operands.
1968 TmpInst
.addOperand(MCOperand::createImm(ARMCC::AL
));
1969 TmpInst
.addOperand(MCOperand::createReg(0));
1970 EmitToStreamer(*OutStreamer
, TmpInst
);
1973 case ARM::BR_JTm_rs
: {
1976 TmpInst
.setOpcode(ARM::LDRrs
);
1977 TmpInst
.addOperand(MCOperand::createReg(ARM::PC
));
1978 TmpInst
.addOperand(MCOperand::createReg(MI
->getOperand(0).getReg()));
1979 TmpInst
.addOperand(MCOperand::createReg(MI
->getOperand(1).getReg()));
1980 TmpInst
.addOperand(MCOperand::createImm(MI
->getOperand(2).getImm()));
1981 // Add predicate operands.
1982 TmpInst
.addOperand(MCOperand::createImm(ARMCC::AL
));
1983 TmpInst
.addOperand(MCOperand::createReg(0));
1984 EmitToStreamer(*OutStreamer
, TmpInst
);
1987 case ARM::BR_JTadd
: {
1988 // add pc, target, idx
1989 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::ADDrr
)
1991 .addReg(MI
->getOperand(0).getReg())
1992 .addReg(MI
->getOperand(1).getReg())
1993 // Add predicate operands.
1996 // Add 's' bit operand (always reg0 for this)
2001 OutStreamer
->emitZeros(MI
->getOperand(1).getImm());
2004 // Non-Darwin binutils don't yet support the "trap" mnemonic.
2005 // FIXME: Remove this special case when they do.
2006 if (!Subtarget
->isTargetMachO()) {
2007 uint32_t Val
= 0xe7ffdefeUL
;
2008 OutStreamer
->AddComment("trap");
2014 case ARM::TRAPNaCl
: {
2015 uint32_t Val
= 0xe7fedef0UL
;
2016 OutStreamer
->AddComment("trap");
2021 // Non-Darwin binutils don't yet support the "trap" mnemonic.
2022 // FIXME: Remove this special case when they do.
2023 if (!Subtarget
->isTargetMachO()) {
2024 uint16_t Val
= 0xdefe;
2025 OutStreamer
->AddComment("trap");
2026 ATS
.emitInst(Val
, 'n');
2031 case ARM::t2Int_eh_sjlj_setjmp
:
2032 case ARM::t2Int_eh_sjlj_setjmp_nofp
:
2033 case ARM::tInt_eh_sjlj_setjmp
: {
2034 // Two incoming args: GPR:$src, GPR:$val
2037 // str $val, [$src, #4]
2042 Register SrcReg
= MI
->getOperand(0).getReg();
2043 Register ValReg
= MI
->getOperand(1).getReg();
2044 MCSymbol
*Label
= OutContext
.createTempSymbol("SJLJEH");
2045 OutStreamer
->AddComment("eh_setjmp begin");
2046 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tMOVr
)
2053 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tADDi3
)
2063 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tSTRi
)
2066 // The offset immediate is #4. The operand value is scaled by 4 for the
2067 // tSTR instruction.
2073 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tMOVi8
)
2081 const MCExpr
*SymbolExpr
= MCSymbolRefExpr::create(Label
, OutContext
);
2082 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tB
)
2083 .addExpr(SymbolExpr
)
2087 OutStreamer
->AddComment("eh_setjmp end");
2088 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tMOVi8
)
2096 OutStreamer
->emitLabel(Label
);
2100 case ARM::Int_eh_sjlj_setjmp_nofp
:
2101 case ARM::Int_eh_sjlj_setjmp
: {
2102 // Two incoming args: GPR:$src, GPR:$val
2104 // str $val, [$src, #+4]
2108 Register SrcReg
= MI
->getOperand(0).getReg();
2109 Register ValReg
= MI
->getOperand(1).getReg();
2111 OutStreamer
->AddComment("eh_setjmp begin");
2112 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::ADDri
)
2119 // 's' bit operand (always reg0 for this).
2122 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::STRi12
)
2130 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::MOVi
)
2136 // 's' bit operand (always reg0 for this).
2139 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::ADDri
)
2146 // 's' bit operand (always reg0 for this).
2149 OutStreamer
->AddComment("eh_setjmp end");
2150 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::MOVi
)
2156 // 's' bit operand (always reg0 for this).
2160 case ARM::Int_eh_sjlj_longjmp
: {
2161 // ldr sp, [$src, #8]
2162 // ldr $scratch, [$src, #4]
2165 Register SrcReg
= MI
->getOperand(0).getReg();
2166 Register ScratchReg
= MI
->getOperand(1).getReg();
2167 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::LDRi12
)
2175 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::LDRi12
)
2183 const MachineFunction
&MF
= *MI
->getParent()->getParent();
2184 const ARMSubtarget
&STI
= MF
.getSubtarget
<ARMSubtarget
>();
2186 if (STI
.isTargetDarwin() || STI
.isTargetWindows()) {
2187 // These platforms always use the same frame register
2188 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::LDRi12
)
2189 .addReg(STI
.getFramePointerReg())
2196 // If the calling code might use either R7 or R11 as
2197 // frame pointer register, restore it into both.
2198 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::LDRi12
)
2205 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::LDRi12
)
2214 assert(Subtarget
->hasV4TOps());
2215 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::BX
)
2222 case ARM::tInt_eh_sjlj_longjmp
: {
2223 // ldr $scratch, [$src, #8]
2225 // ldr $scratch, [$src, #4]
2228 Register SrcReg
= MI
->getOperand(0).getReg();
2229 Register ScratchReg
= MI
->getOperand(1).getReg();
2231 const MachineFunction
&MF
= *MI
->getParent()->getParent();
2232 const ARMSubtarget
&STI
= MF
.getSubtarget
<ARMSubtarget
>();
2234 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tLDRi
)
2237 // The offset immediate is #8. The operand value is scaled by 4 for the
2238 // tLDR instruction.
2244 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tMOVr
)
2251 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tLDRi
)
2259 if (STI
.isTargetDarwin() || STI
.isTargetWindows()) {
2260 // These platforms always use the same frame register
2261 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tLDRi
)
2262 .addReg(STI
.getFramePointerReg())
2269 // If the calling code might use either R7 or R11 as
2270 // frame pointer register, restore it into both.
2271 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tLDRi
)
2278 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tLDRi
)
2287 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::tBX
)
2294 case ARM::tInt_WIN_eh_sjlj_longjmp
: {
2295 // ldr.w r11, [$src, #0]
2296 // ldr.w sp, [$src, #8]
2297 // ldr.w pc, [$src, #4]
2299 Register SrcReg
= MI
->getOperand(0).getReg();
2301 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::t2LDRi12
)
2308 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::t2LDRi12
)
2315 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::t2LDRi12
)
2324 case ARM::PATCHABLE_FUNCTION_ENTER
:
2325 LowerPATCHABLE_FUNCTION_ENTER(*MI
);
2327 case ARM::PATCHABLE_FUNCTION_EXIT
:
2328 LowerPATCHABLE_FUNCTION_EXIT(*MI
);
2330 case ARM::PATCHABLE_TAIL_CALL
:
2331 LowerPATCHABLE_TAIL_CALL(*MI
);
2333 case ARM::SpeculationBarrierISBDSBEndBB
: {
2334 // Print DSB SYS + ISB
2336 TmpInstDSB
.setOpcode(ARM::DSB
);
2337 TmpInstDSB
.addOperand(MCOperand::createImm(0xf));
2338 EmitToStreamer(*OutStreamer
, TmpInstDSB
);
2340 TmpInstISB
.setOpcode(ARM::ISB
);
2341 TmpInstISB
.addOperand(MCOperand::createImm(0xf));
2342 EmitToStreamer(*OutStreamer
, TmpInstISB
);
2345 case ARM::t2SpeculationBarrierISBDSBEndBB
: {
2346 // Print DSB SYS + ISB
2348 TmpInstDSB
.setOpcode(ARM::t2DSB
);
2349 TmpInstDSB
.addOperand(MCOperand::createImm(0xf));
2350 TmpInstDSB
.addOperand(MCOperand::createImm(ARMCC::AL
));
2351 TmpInstDSB
.addOperand(MCOperand::createReg(0));
2352 EmitToStreamer(*OutStreamer
, TmpInstDSB
);
2354 TmpInstISB
.setOpcode(ARM::t2ISB
);
2355 TmpInstISB
.addOperand(MCOperand::createImm(0xf));
2356 TmpInstISB
.addOperand(MCOperand::createImm(ARMCC::AL
));
2357 TmpInstISB
.addOperand(MCOperand::createReg(0));
2358 EmitToStreamer(*OutStreamer
, TmpInstISB
);
2361 case ARM::SpeculationBarrierSBEndBB
: {
2364 TmpInstSB
.setOpcode(ARM::SB
);
2365 EmitToStreamer(*OutStreamer
, TmpInstSB
);
2368 case ARM::t2SpeculationBarrierSBEndBB
: {
2371 TmpInstSB
.setOpcode(ARM::t2SB
);
2372 EmitToStreamer(*OutStreamer
, TmpInstSB
);
2376 case ARM::SEH_StackAlloc
:
2377 ATS
.emitARMWinCFIAllocStack(MI
->getOperand(0).getImm(),
2378 MI
->getOperand(1).getImm());
2381 case ARM::SEH_SaveRegs
:
2382 case ARM::SEH_SaveRegs_Ret
:
2383 ATS
.emitARMWinCFISaveRegMask(MI
->getOperand(0).getImm(),
2384 MI
->getOperand(1).getImm());
2387 case ARM::SEH_SaveSP
:
2388 ATS
.emitARMWinCFISaveSP(MI
->getOperand(0).getImm());
2391 case ARM::SEH_SaveFRegs
:
2392 ATS
.emitARMWinCFISaveFRegs(MI
->getOperand(0).getImm(),
2393 MI
->getOperand(1).getImm());
2396 case ARM::SEH_SaveLR
:
2397 ATS
.emitARMWinCFISaveLR(MI
->getOperand(0).getImm());
2401 case ARM::SEH_Nop_Ret
:
2402 ATS
.emitARMWinCFINop(MI
->getOperand(0).getImm());
2405 case ARM::SEH_PrologEnd
:
2406 ATS
.emitARMWinCFIPrologEnd(/*Fragment=*/false);
2409 case ARM::SEH_EpilogStart
:
2410 ATS
.emitARMWinCFIEpilogStart(ARMCC::AL
);
2413 case ARM::SEH_EpilogEnd
:
2414 ATS
.emitARMWinCFIEpilogEnd();
2419 LowerARMMachineInstrToMCInst(MI
, TmpInst
, *this);
2421 EmitToStreamer(*OutStreamer
, TmpInst
);
2424 //===----------------------------------------------------------------------===//
2425 // Target Registry Stuff
2426 //===----------------------------------------------------------------------===//
2428 // Force static initialization.
2429 extern "C" LLVM_EXTERNAL_VISIBILITY
void LLVMInitializeARMAsmPrinter() {
2430 RegisterAsmPrinter
<ARMAsmPrinter
> X(getTheARMLETarget());
2431 RegisterAsmPrinter
<ARMAsmPrinter
> Y(getTheARMBETarget());
2432 RegisterAsmPrinter
<ARMAsmPrinter
> A(getTheThumbLETarget());
2433 RegisterAsmPrinter
<ARMAsmPrinter
> B(getTheThumbBETarget());