1 // The LLVM Compiler Infrastructure
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file contains a printer that converts from our internal representation
9 // of machine-dependent LLVM code to GAS-format ARM assembly language.
11 //===----------------------------------------------------------------------===//
13 #define DEBUG_TYPE "asm-printer"
15 #include "ARMBuildAttrs.h"
16 #include "ARMTargetMachine.h"
17 #include "ARMAddressingModes.h"
18 #include "ARMConstantPoolValue.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Module.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineJumpTableInfo.h"
28 #include "llvm/MC/MCSectionMachO.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/Target/TargetData.h"
33 #include "llvm/Target/TargetLoweringObjectFile.h"
34 #include "llvm/Target/TargetMachine.h"
35 #include "llvm/Target/TargetOptions.h"
36 #include "llvm/Target/TargetRegistry.h"
37 #include "llvm/ADT/SmallPtrSet.h"
38 #include "llvm/ADT/SmallString.h"
39 #include "llvm/ADT/Statistic.h"
40 #include "llvm/ADT/StringSet.h"
41 #include "llvm/Support/Compiler.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/Mangler.h"
44 #include "llvm/Support/MathExtras.h"
45 #include "llvm/Support/FormattedStream.h"
49 STATISTIC(EmittedInsts, "Number of machine instrs printed");
52 class VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
55 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
56 /// make the right decision when printing asm code for different targets.
57 const ARMSubtarget *Subtarget;
59 /// AFI - Keep a pointer to ARMFunctionInfo for the current
63 /// MCP - Keep a pointer to constantpool entries of the current
65 const MachineConstantPool *MCP;
67 /// We name each basic block in a Function with a unique number, so
68 /// that we can consistently refer to them later. This is cleared
69 /// at the beginning of each call to runOnMachineFunction().
71 typedef std::map<const Value *, unsigned> ValueMapTy;
72 ValueMapTy NumberForBB;
74 /// GVNonLazyPtrs - Keeps the set of GlobalValues that require
75 /// non-lazy-pointers for indirect access.
76 StringMap<std::string> GVNonLazyPtrs;
78 /// HiddenGVNonLazyPtrs - Keeps the set of GlobalValues with hidden
79 /// visibility that require non-lazy-pointers for indirect access.
80 StringMap<std::string> HiddenGVNonLazyPtrs;
82 /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
85 explicit ARMAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
86 const MCAsmInfo *T, bool V)
87 : AsmPrinter(O, TM, T, V), DW(0), AFI(NULL), MCP(NULL),
89 Subtarget = &TM.getSubtarget<ARMSubtarget>();
92 virtual const char *getPassName() const {
93 return "ARM Assembly Printer";
96 void printOperand(const MachineInstr *MI, int OpNum,
97 const char *Modifier = 0);
98 void printSOImmOperand(const MachineInstr *MI, int OpNum);
99 void printSOImm2PartOperand(const MachineInstr *MI, int OpNum);
100 void printSORegOperand(const MachineInstr *MI, int OpNum);
101 void printAddrMode2Operand(const MachineInstr *MI, int OpNum);
102 void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNum);
103 void printAddrMode3Operand(const MachineInstr *MI, int OpNum);
104 void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNum);
105 void printAddrMode4Operand(const MachineInstr *MI, int OpNum,
106 const char *Modifier = 0);
107 void printAddrMode5Operand(const MachineInstr *MI, int OpNum,
108 const char *Modifier = 0);
109 void printAddrMode6Operand(const MachineInstr *MI, int OpNum);
110 void printAddrModePCOperand(const MachineInstr *MI, int OpNum,
111 const char *Modifier = 0);
112 void printBitfieldInvMaskImmOperand (const MachineInstr *MI, int OpNum);
114 void printThumbITMask(const MachineInstr *MI, int OpNum);
115 void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNum);
116 void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNum,
118 void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNum);
119 void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNum);
120 void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNum);
121 void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNum);
123 void printT2SOOperand(const MachineInstr *MI, int OpNum);
124 void printT2AddrModeImm12Operand(const MachineInstr *MI, int OpNum);
125 void printT2AddrModeImm8Operand(const MachineInstr *MI, int OpNum);
126 void printT2AddrModeImm8s4Operand(const MachineInstr *MI, int OpNum);
127 void printT2AddrModeImm8OffsetOperand(const MachineInstr *MI, int OpNum);
128 void printT2AddrModeSoRegOperand(const MachineInstr *MI, int OpNum);
130 void printPredicateOperand(const MachineInstr *MI, int OpNum);
131 void printSBitModifierOperand(const MachineInstr *MI, int OpNum);
132 void printPCLabel(const MachineInstr *MI, int OpNum);
133 void printRegisterList(const MachineInstr *MI, int OpNum);
134 void printCPInstOperand(const MachineInstr *MI, int OpNum,
135 const char *Modifier);
136 void printJTBlockOperand(const MachineInstr *MI, int OpNum);
137 void printJT2BlockOperand(const MachineInstr *MI, int OpNum);
138 void printTBAddrMode(const MachineInstr *MI, int OpNum);
139 void printNoHashImmediate(const MachineInstr *MI, int OpNum);
141 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
142 unsigned AsmVariant, const char *ExtraCode);
143 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
145 const char *ExtraCode);
147 void PrintGlobalVariable(const GlobalVariable* GVar);
148 void printInstruction(const MachineInstr *MI); // autogenerated.
149 static const char *getRegisterName(unsigned RegNo);
151 void printMachineInstruction(const MachineInstr *MI);
152 bool runOnMachineFunction(MachineFunction &F);
153 bool doInitialization(Module &M);
154 bool doFinalization(Module &M);
156 /// EmitMachineConstantPoolValue - Print a machine constantpool value to
158 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
159 printDataDirective(MCPV->getType());
161 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
162 GlobalValue *GV = ACPV->getGV();
165 if (ACPV->isLSDA()) {
166 SmallString<16> LSDAName;
167 raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() <<
168 "_LSDA_" << getFunctionNumber();
169 Name = LSDAName.str();
171 bool isIndirect = Subtarget->isTargetDarwin() &&
172 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
174 Name = Mang->getMangledName(GV);
176 // FIXME: Remove this when Darwin transition to @GOT like syntax.
177 std::string SymName = Mang->getMangledName(GV);
178 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
179 if (GV->hasHiddenVisibility())
180 HiddenGVNonLazyPtrs[SymName] = Name;
182 GVNonLazyPtrs[SymName] = Name;
185 Name = Mang->makeNameProper(ACPV->getSymbol());
188 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
189 if (ACPV->getPCAdjustment() != 0) {
190 O << "-(" << MAI->getPrivateGlobalPrefix() << "PC"
191 << ACPV->getLabelId()
192 << "+" << (unsigned)ACPV->getPCAdjustment();
193 if (ACPV->mustAddCurrentAddress())
200 void getAnalysisUsage(AnalysisUsage &AU) const {
201 AsmPrinter::getAnalysisUsage(AU);
202 AU.setPreservesAll();
203 AU.addRequired<MachineModuleInfo>();
204 AU.addRequired<DwarfWriter>();
207 } // end of anonymous namespace
209 #include "ARMGenAsmWriter.inc"
211 /// runOnMachineFunction - This uses the printInstruction()
212 /// method to print assembly for each instruction.
214 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
217 AFI = MF.getInfo<ARMFunctionInfo>();
218 MCP = MF.getConstantPool();
220 SetupMachineFunction(MF);
223 // NOTE: we don't print out constant pools here, they are handled as
228 // Print out labels for the function.
229 const Function *F = MF.getFunction();
230 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
232 switch (F->getLinkage()) {
233 default: llvm_unreachable("Unknown linkage type!");
234 case Function::PrivateLinkage:
235 case Function::InternalLinkage:
237 case Function::ExternalLinkage:
238 O << "\t.globl\t" << CurrentFnName << "\n";
240 case Function::LinkerPrivateLinkage:
241 case Function::WeakAnyLinkage:
242 case Function::WeakODRLinkage:
243 case Function::LinkOnceAnyLinkage:
244 case Function::LinkOnceODRLinkage:
245 if (Subtarget->isTargetDarwin()) {
246 O << "\t.globl\t" << CurrentFnName << "\n";
247 O << "\t.weak_definition\t" << CurrentFnName << "\n";
249 O << MAI->getWeakRefDirective() << CurrentFnName << "\n";
254 printVisibility(CurrentFnName, F->getVisibility());
256 if (AFI->isThumbFunction()) {
257 EmitAlignment(MF.getAlignment(), F, AFI->getAlign());
258 O << "\t.code\t16\n";
259 O << "\t.thumb_func";
260 if (Subtarget->isTargetDarwin())
261 O << "\t" << CurrentFnName;
265 EmitAlignment(MF.getAlignment(), F);
268 O << CurrentFnName << ":\n";
269 // Emit pre-function debug information.
270 DW->BeginFunction(&MF);
272 if (Subtarget->isTargetDarwin()) {
273 // If the function is empty, then we need to emit *something*. Otherwise,
274 // the function's label might be associated with something that it wasn't
275 // meant to be associated with. We emit a noop in this situation.
276 MachineFunction::iterator I = MF.begin();
278 if (++I == MF.end() && MF.front().empty())
282 // Print out code for the function.
283 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
285 // Print a label for the basic block.
286 if (I != MF.begin()) {
287 EmitBasicBlockStart(I);
290 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
292 // Print the assembly for the instruction.
293 printMachineInstruction(II);
297 if (MAI->hasDotTypeDotSizeDirective())
298 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
300 // Emit post-function debug information.
301 DW->EndFunction(&MF);
306 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
307 const char *Modifier) {
308 const MachineOperand &MO = MI->getOperand(OpNum);
309 switch (MO.getType()) {
310 case MachineOperand::MO_Register: {
311 unsigned Reg = MO.getReg();
312 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
313 if (Modifier && strcmp(Modifier, "dregpair") == 0) {
314 unsigned DRegLo = TRI->getSubReg(Reg, 5); // arm_dsubreg_0
315 unsigned DRegHi = TRI->getSubReg(Reg, 6); // arm_dsubreg_1
317 << getRegisterName(DRegLo) << ',' << getRegisterName(DRegHi)
319 } else if (Modifier && strcmp(Modifier, "lane") == 0) {
320 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(Reg);
321 unsigned DReg = TRI->getMatchingSuperReg(Reg, RegNum & 1 ? 2 : 1,
322 &ARM::DPR_VFP2RegClass);
323 O << getRegisterName(DReg) << '[' << (RegNum & 1) << ']';
325 O << getRegisterName(Reg);
328 llvm_unreachable("not implemented");
331 case MachineOperand::MO_Immediate: {
332 int64_t Imm = MO.getImm();
334 if (strcmp(Modifier, "lo16") == 0)
335 Imm = Imm & 0xffffLL;
336 else if (strcmp(Modifier, "hi16") == 0)
337 Imm = (Imm & 0xffff0000LL) >> 16;
342 case MachineOperand::MO_MachineBasicBlock:
343 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
345 case MachineOperand::MO_GlobalAddress: {
346 bool isCallOp = Modifier && !strcmp(Modifier, "call");
347 GlobalValue *GV = MO.getGlobal();
348 O << Mang->getMangledName(GV);
350 printOffset(MO.getOffset());
352 if (isCallOp && Subtarget->isTargetELF() &&
353 TM.getRelocationModel() == Reloc::PIC_)
357 case MachineOperand::MO_ExternalSymbol: {
358 bool isCallOp = Modifier && !strcmp(Modifier, "call");
359 std::string Name = Mang->makeNameProper(MO.getSymbolName());
362 if (isCallOp && Subtarget->isTargetELF() &&
363 TM.getRelocationModel() == Reloc::PIC_)
367 case MachineOperand::MO_ConstantPoolIndex:
368 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
369 << '_' << MO.getIndex();
371 case MachineOperand::MO_JumpTableIndex:
372 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
373 << '_' << MO.getIndex();
376 O << "<unknown operand type>"; abort (); break;
380 static void printSOImm(formatted_raw_ostream &O, int64_t V, bool VerboseAsm,
381 const MCAsmInfo *MAI) {
382 // Break it up into two parts that make up a shifter immediate.
383 V = ARM_AM::getSOImmVal(V);
384 assert(V != -1 && "Not a valid so_imm value!");
386 unsigned Imm = ARM_AM::getSOImmValImm(V);
387 unsigned Rot = ARM_AM::getSOImmValRot(V);
389 // Print low-level immediate formation info, per
390 // A5.1.3: "Data-processing operands - Immediate".
392 O << "#" << Imm << ", " << Rot;
393 // Pretty printed version.
395 O << ' ' << MAI->getCommentString()
396 << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
402 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
403 /// immediate in bits 0-7.
404 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
405 const MachineOperand &MO = MI->getOperand(OpNum);
406 assert(MO.isImm() && "Not a valid so_imm value!");
407 printSOImm(O, MO.getImm(), VerboseAsm, MAI);
410 /// printSOImm2PartOperand - SOImm is broken into two pieces using a 'mov'
411 /// followed by an 'orr' to materialize.
412 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
413 const MachineOperand &MO = MI->getOperand(OpNum);
414 assert(MO.isImm() && "Not a valid so_imm value!");
415 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
416 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
417 printSOImm(O, V1, VerboseAsm, MAI);
419 printPredicateOperand(MI, 2);
425 printSOImm(O, V2, VerboseAsm, MAI);
428 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
429 // "Addressing Mode 1 - Data-processing operands" forms. This includes:
431 // REG REG 0,SH_OPC - e.g. R5, ROR R3
432 // REG 0 IMM,SH_OPC - e.g. R5, LSL #3
433 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
434 const MachineOperand &MO1 = MI->getOperand(Op);
435 const MachineOperand &MO2 = MI->getOperand(Op+1);
436 const MachineOperand &MO3 = MI->getOperand(Op+2);
438 O << getRegisterName(MO1.getReg());
440 // Print the shift opc.
442 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
446 O << getRegisterName(MO2.getReg());
447 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
449 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
453 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
454 const MachineOperand &MO1 = MI->getOperand(Op);
455 const MachineOperand &MO2 = MI->getOperand(Op+1);
456 const MachineOperand &MO3 = MI->getOperand(Op+2);
458 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
459 printOperand(MI, Op);
463 O << "[" << getRegisterName(MO1.getReg());
466 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
468 << (char)ARM_AM::getAM2Op(MO3.getImm())
469 << ARM_AM::getAM2Offset(MO3.getImm());
475 << (char)ARM_AM::getAM2Op(MO3.getImm())
476 << getRegisterName(MO2.getReg());
478 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
480 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
485 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
486 const MachineOperand &MO1 = MI->getOperand(Op);
487 const MachineOperand &MO2 = MI->getOperand(Op+1);
490 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
491 assert(ImmOffs && "Malformed indexed load / store!");
493 << (char)ARM_AM::getAM2Op(MO2.getImm())
498 O << (char)ARM_AM::getAM2Op(MO2.getImm())
499 << getRegisterName(MO1.getReg());
501 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
503 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
507 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
508 const MachineOperand &MO1 = MI->getOperand(Op);
509 const MachineOperand &MO2 = MI->getOperand(Op+1);
510 const MachineOperand &MO3 = MI->getOperand(Op+2);
512 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
513 O << "[" << getRegisterName(MO1.getReg());
517 << (char)ARM_AM::getAM3Op(MO3.getImm())
518 << getRegisterName(MO2.getReg())
523 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
525 << (char)ARM_AM::getAM3Op(MO3.getImm())
530 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
531 const MachineOperand &MO1 = MI->getOperand(Op);
532 const MachineOperand &MO2 = MI->getOperand(Op+1);
535 O << (char)ARM_AM::getAM3Op(MO2.getImm())
536 << getRegisterName(MO1.getReg());
540 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
541 assert(ImmOffs && "Malformed indexed load / store!");
543 << (char)ARM_AM::getAM3Op(MO2.getImm())
547 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
548 const char *Modifier) {
549 const MachineOperand &MO1 = MI->getOperand(Op);
550 const MachineOperand &MO2 = MI->getOperand(Op+1);
551 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
552 if (Modifier && strcmp(Modifier, "submode") == 0) {
553 if (MO1.getReg() == ARM::SP) {
555 bool isLDM = (MI->getOpcode() == ARM::LDM ||
556 MI->getOpcode() == ARM::LDM_RET ||
557 MI->getOpcode() == ARM::t2LDM ||
558 MI->getOpcode() == ARM::t2LDM_RET);
559 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
561 O << ARM_AM::getAMSubModeStr(Mode);
562 } else if (Modifier && strcmp(Modifier, "wide") == 0) {
563 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
564 if (Mode == ARM_AM::ia)
567 printOperand(MI, Op);
568 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
573 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
574 const char *Modifier) {
575 const MachineOperand &MO1 = MI->getOperand(Op);
576 const MachineOperand &MO2 = MI->getOperand(Op+1);
578 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
579 printOperand(MI, Op);
583 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
585 if (Modifier && strcmp(Modifier, "submode") == 0) {
586 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
587 if (MO1.getReg() == ARM::SP) {
588 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
589 MI->getOpcode() == ARM::FLDMS);
590 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
592 O << ARM_AM::getAMSubModeStr(Mode);
594 } else if (Modifier && strcmp(Modifier, "base") == 0) {
595 // Used for FSTM{D|S} and LSTM{D|S} operations.
596 O << getRegisterName(MO1.getReg());
597 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
602 O << "[" << getRegisterName(MO1.getReg());
604 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
606 << (char)ARM_AM::getAM5Op(MO2.getImm())
612 void ARMAsmPrinter::printAddrMode6Operand(const MachineInstr *MI, int Op) {
613 const MachineOperand &MO1 = MI->getOperand(Op);
614 const MachineOperand &MO2 = MI->getOperand(Op+1);
615 const MachineOperand &MO3 = MI->getOperand(Op+2);
617 // FIXME: No support yet for specifying alignment.
618 O << "[" << getRegisterName(MO1.getReg()) << "]";
620 if (ARM_AM::getAM6WBFlag(MO3.getImm())) {
621 if (MO2.getReg() == 0)
624 O << ", " << getRegisterName(MO2.getReg());
628 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
629 const char *Modifier) {
630 if (Modifier && strcmp(Modifier, "label") == 0) {
631 printPCLabel(MI, Op+1);
635 const MachineOperand &MO1 = MI->getOperand(Op);
636 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
637 O << "[pc, +" << getRegisterName(MO1.getReg()) << "]";
641 ARMAsmPrinter::printBitfieldInvMaskImmOperand(const MachineInstr *MI, int Op) {
642 const MachineOperand &MO = MI->getOperand(Op);
643 uint32_t v = ~MO.getImm();
644 int32_t lsb = CountTrailingZeros_32(v);
645 int32_t width = (32 - CountLeadingZeros_32 (v)) - lsb;
646 assert(MO.isImm() && "Not a valid bf_inv_mask_imm value!");
647 O << "#" << lsb << ", #" << width;
650 //===--------------------------------------------------------------------===//
653 ARMAsmPrinter::printThumbITMask(const MachineInstr *MI, int Op) {
654 // (3 - the number of trailing zeros) is the number of then / else.
655 unsigned Mask = MI->getOperand(Op).getImm();
656 unsigned NumTZ = CountTrailingZeros_32(Mask);
657 assert(NumTZ <= 3 && "Invalid IT mask!");
658 for (unsigned Pos = 3, e = NumTZ; Pos > e; --Pos) {
659 bool T = (Mask & (1 << Pos)) == 0;
668 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
669 const MachineOperand &MO1 = MI->getOperand(Op);
670 const MachineOperand &MO2 = MI->getOperand(Op+1);
671 O << "[" << getRegisterName(MO1.getReg());
672 O << ", " << getRegisterName(MO2.getReg()) << "]";
676 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
678 const MachineOperand &MO1 = MI->getOperand(Op);
679 const MachineOperand &MO2 = MI->getOperand(Op+1);
680 const MachineOperand &MO3 = MI->getOperand(Op+2);
682 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
683 printOperand(MI, Op);
687 O << "[" << getRegisterName(MO1.getReg());
689 O << ", " << getRegisterName(MO3.getReg());
690 else if (unsigned ImmOffs = MO2.getImm()) {
691 O << ", #" << ImmOffs;
699 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
700 printThumbAddrModeRI5Operand(MI, Op, 1);
703 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
704 printThumbAddrModeRI5Operand(MI, Op, 2);
707 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
708 printThumbAddrModeRI5Operand(MI, Op, 4);
711 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
712 const MachineOperand &MO1 = MI->getOperand(Op);
713 const MachineOperand &MO2 = MI->getOperand(Op+1);
714 O << "[" << getRegisterName(MO1.getReg());
715 if (unsigned ImmOffs = MO2.getImm())
716 O << ", #" << ImmOffs << " * 4";
720 //===--------------------------------------------------------------------===//
722 // Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
723 // register with shift forms.
725 // REG IMM, SH_OPC - e.g. R5, LSL #3
726 void ARMAsmPrinter::printT2SOOperand(const MachineInstr *MI, int OpNum) {
727 const MachineOperand &MO1 = MI->getOperand(OpNum);
728 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
730 unsigned Reg = MO1.getReg();
731 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
732 O << getRegisterName(Reg);
734 // Print the shift opc.
736 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO2.getImm()))
739 assert(MO2.isImm() && "Not a valid t2_so_reg value!");
740 O << "#" << ARM_AM::getSORegOffset(MO2.getImm());
743 void ARMAsmPrinter::printT2AddrModeImm12Operand(const MachineInstr *MI,
745 const MachineOperand &MO1 = MI->getOperand(OpNum);
746 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
748 O << "[" << getRegisterName(MO1.getReg());
750 unsigned OffImm = MO2.getImm();
751 if (OffImm) // Don't print +0.
752 O << ", #+" << OffImm;
756 void ARMAsmPrinter::printT2AddrModeImm8Operand(const MachineInstr *MI,
758 const MachineOperand &MO1 = MI->getOperand(OpNum);
759 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
761 O << "[" << getRegisterName(MO1.getReg());
763 int32_t OffImm = (int32_t)MO2.getImm();
766 O << ", #-" << -OffImm;
768 O << ", #+" << OffImm;
772 void ARMAsmPrinter::printT2AddrModeImm8s4Operand(const MachineInstr *MI,
774 const MachineOperand &MO1 = MI->getOperand(OpNum);
775 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
777 O << "[" << getRegisterName(MO1.getReg());
779 int32_t OffImm = (int32_t)MO2.getImm() / 4;
782 O << ", #-" << -OffImm << " * 4";
784 O << ", #+" << OffImm << " * 4";
788 void ARMAsmPrinter::printT2AddrModeImm8OffsetOperand(const MachineInstr *MI,
790 const MachineOperand &MO1 = MI->getOperand(OpNum);
791 int32_t OffImm = (int32_t)MO1.getImm();
794 O << "#-" << -OffImm;
799 void ARMAsmPrinter::printT2AddrModeSoRegOperand(const MachineInstr *MI,
801 const MachineOperand &MO1 = MI->getOperand(OpNum);
802 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
803 const MachineOperand &MO3 = MI->getOperand(OpNum+2);
805 O << "[" << getRegisterName(MO1.getReg());
807 assert(MO2.getReg() && "Invalid so_reg load / store address!");
808 O << ", " << getRegisterName(MO2.getReg());
810 unsigned ShAmt = MO3.getImm();
812 assert(ShAmt <= 3 && "Not a valid Thumb2 addressing mode!");
813 O << ", lsl #" << ShAmt;
819 //===--------------------------------------------------------------------===//
821 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int OpNum) {
822 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
824 O << ARMCondCodeToString(CC);
827 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int OpNum){
828 unsigned Reg = MI->getOperand(OpNum).getReg();
830 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
835 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int OpNum) {
836 int Id = (int)MI->getOperand(OpNum).getImm();
837 O << MAI->getPrivateGlobalPrefix() << "PC" << Id;
840 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int OpNum) {
842 for (unsigned i = OpNum, e = MI->getNumOperands(); i != e; ++i) {
843 if (MI->getOperand(i).isImplicit())
845 if ((int)i != OpNum) O << ", ";
851 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNum,
852 const char *Modifier) {
853 assert(Modifier && "This operand only works with a modifier!");
854 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
856 if (!strcmp(Modifier, "label")) {
857 unsigned ID = MI->getOperand(OpNum).getImm();
858 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
859 << '_' << ID << ":\n";
861 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
862 unsigned CPI = MI->getOperand(OpNum).getIndex();
864 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
866 if (MCPE.isMachineConstantPoolEntry()) {
867 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
869 EmitGlobalConstant(MCPE.Val.ConstVal);
874 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNum) {
875 assert(!Subtarget->isThumb2() && "Thumb2 should use double-jump jumptables!");
877 const MachineOperand &MO1 = MI->getOperand(OpNum);
878 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
879 unsigned JTI = MO1.getIndex();
880 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
881 << '_' << JTI << '_' << MO2.getImm() << ":\n";
883 const char *JTEntryDirective = MAI->getData32bitsDirective();
885 const MachineFunction *MF = MI->getParent()->getParent();
886 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
887 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
888 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
889 bool UseSet= MAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
890 SmallPtrSet<MachineBasicBlock*, 8> JTSets;
891 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
892 MachineBasicBlock *MBB = JTBBs[i];
893 bool isNew = JTSets.insert(MBB);
896 printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
898 O << JTEntryDirective << ' ';
900 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
901 << '_' << JTI << '_' << MO2.getImm()
902 << "_set_" << MBB->getNumber();
903 else if (TM.getRelocationModel() == Reloc::PIC_) {
904 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
905 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
906 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
908 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
915 void ARMAsmPrinter::printJT2BlockOperand(const MachineInstr *MI, int OpNum) {
916 const MachineOperand &MO1 = MI->getOperand(OpNum);
917 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
918 unsigned JTI = MO1.getIndex();
919 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
920 << '_' << JTI << '_' << MO2.getImm() << ":\n";
922 const MachineFunction *MF = MI->getParent()->getParent();
923 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
924 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
925 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
926 bool ByteOffset = false, HalfWordOffset = false;
927 if (MI->getOpcode() == ARM::t2TBB)
929 else if (MI->getOpcode() == ARM::t2TBH)
930 HalfWordOffset = true;
932 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
933 MachineBasicBlock *MBB = JTBBs[i];
935 O << MAI->getData8bitsDirective();
936 else if (HalfWordOffset)
937 O << MAI->getData16bitsDirective();
938 if (ByteOffset || HalfWordOffset) {
940 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
941 O << "-" << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
942 << '_' << JTI << '_' << MO2.getImm() << ")/2";
945 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
951 // Make sure the instruction that follows TBB is 2-byte aligned.
952 // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
953 if (ByteOffset && (JTBBs.size() & 1)) {
959 void ARMAsmPrinter::printTBAddrMode(const MachineInstr *MI, int OpNum) {
960 O << "[pc, " << getRegisterName(MI->getOperand(OpNum).getReg());
961 if (MI->getOpcode() == ARM::t2TBH)
966 void ARMAsmPrinter::printNoHashImmediate(const MachineInstr *MI, int OpNum) {
967 O << MI->getOperand(OpNum).getImm();
970 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
971 unsigned AsmVariant, const char *ExtraCode){
972 // Does this asm operand have a single letter operand modifier?
973 if (ExtraCode && ExtraCode[0]) {
974 if (ExtraCode[1] != 0) return true; // Unknown modifier.
976 switch (ExtraCode[0]) {
977 default: return true; // Unknown modifier.
978 case 'a': // Print as a memory address.
979 if (MI->getOperand(OpNum).isReg()) {
980 O << "[" << getRegisterName(MI->getOperand(OpNum).getReg()) << "]";
984 case 'c': // Don't print "#" before an immediate operand.
985 if (!MI->getOperand(OpNum).isImm())
987 printNoHashImmediate(MI, OpNum);
989 case 'P': // Print a VFP double precision register.
990 printOperand(MI, OpNum);
993 if (TM.getTargetData()->isLittleEndian())
997 if (TM.getTargetData()->isBigEndian())
1000 case 'H': // Write second word of DI / DF reference.
1001 // Verify that this operand has two consecutive registers.
1002 if (!MI->getOperand(OpNum).isReg() ||
1003 OpNum+1 == MI->getNumOperands() ||
1004 !MI->getOperand(OpNum+1).isReg())
1006 ++OpNum; // Return the high-part.
1010 printOperand(MI, OpNum);
1014 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
1015 unsigned OpNum, unsigned AsmVariant,
1016 const char *ExtraCode) {
1017 if (ExtraCode && ExtraCode[0])
1018 return true; // Unknown modifier.
1019 printAddrMode2Operand(MI, OpNum);
1023 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
1026 int Opc = MI->getOpcode();
1028 case ARM::CONSTPOOL_ENTRY:
1029 if (!InCPMode && AFI->isThumbFunction()) {
1035 if (InCPMode && AFI->isThumbFunction())
1039 // Call the autogenerated instruction printer routines.
1040 processDebugLoc(MI->getDebugLoc());
1041 printInstruction(MI);
1042 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
1047 bool ARMAsmPrinter::doInitialization(Module &M) {
1049 bool Result = AsmPrinter::doInitialization(M);
1050 DW = getAnalysisIfAvailable<DwarfWriter>();
1052 // Use unified assembler syntax mode for Thumb.
1053 if (Subtarget->isThumb())
1054 O << "\t.syntax unified\n";
1056 // Emit ARM Build Attributes
1057 if (Subtarget->isTargetELF()) {
1059 std::string CPUString = Subtarget->getCPUString();
1060 if (CPUString != "generic")
1061 O << "\t.cpu " << CPUString << '\n';
1063 // FIXME: Emit FPU type
1064 if (Subtarget->hasVFP2())
1065 O << "\t.eabi_attribute " << ARMBuildAttrs::VFP_arch << ", 2\n";
1067 // Signal various FP modes.
1069 O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_denormal << ", 1\n"
1070 << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_exceptions << ", 1\n";
1072 if (FiniteOnlyFPMath())
1073 O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 1\n";
1075 O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 3\n";
1077 // 8-bytes alignment stuff.
1078 O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_needed << ", 1\n"
1079 << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_preserved << ", 1\n";
1081 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
1082 if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard)
1083 O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_HardFP_use << ", 3\n"
1084 << "\t.eabi_attribute " << ARMBuildAttrs::ABI_VFP_args << ", 1\n";
1086 // FIXME: Should we signal R9 usage?
1092 void ARMAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
1093 const TargetData *TD = TM.getTargetData();
1095 if (!GVar->hasInitializer()) // External global require no code
1098 // Check to see if this is a special global used by LLVM, if so, emit it.
1100 if (EmitSpecialLLVMGlobal(GVar)) {
1101 if (Subtarget->isTargetDarwin() &&
1102 TM.getRelocationModel() == Reloc::Static) {
1103 if (GVar->getName() == "llvm.global_ctors")
1104 O << ".reference .constructors_used\n";
1105 else if (GVar->getName() == "llvm.global_dtors")
1106 O << ".reference .destructors_used\n";
1111 std::string name = Mang->getMangledName(GVar);
1112 Constant *C = GVar->getInitializer();
1113 const Type *Type = C->getType();
1114 unsigned Size = TD->getTypeAllocSize(Type);
1115 unsigned Align = TD->getPreferredAlignmentLog(GVar);
1116 bool isDarwin = Subtarget->isTargetDarwin();
1118 printVisibility(name, GVar->getVisibility());
1120 if (Subtarget->isTargetELF())
1121 O << "\t.type " << name << ",%object\n";
1123 const MCSection *TheSection =
1124 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
1125 OutStreamer.SwitchSection(TheSection);
1127 // FIXME: get this stuff from section kind flags.
1128 if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal() &&
1129 // Don't put things that should go in the cstring section into "comm".
1130 !TheSection->getKind().isMergeableCString()) {
1131 if (GVar->hasExternalLinkage()) {
1132 if (const char *Directive = MAI->getZeroFillDirective()) {
1133 O << "\t.globl\t" << name << "\n";
1134 O << Directive << "__DATA, __common, " << name << ", "
1135 << Size << ", " << Align << "\n";
1140 if (GVar->hasLocalLinkage() || GVar->isWeakForLinker()) {
1141 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
1144 if (GVar->hasLocalLinkage()) {
1145 O << MAI->getLCOMMDirective() << name << "," << Size
1147 } else if (GVar->hasCommonLinkage()) {
1148 O << MAI->getCOMMDirective() << name << "," << Size
1151 OutStreamer.SwitchSection(TheSection);
1152 O << "\t.globl " << name << '\n'
1153 << MAI->getWeakDefDirective() << name << '\n';
1154 EmitAlignment(Align, GVar);
1157 O << "\t\t\t\t" << MAI->getCommentString() << ' ';
1158 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1161 EmitGlobalConstant(C);
1164 } else if (MAI->getLCOMMDirective() != NULL) {
1165 if (GVar->hasLocalLinkage()) {
1166 O << MAI->getLCOMMDirective() << name << "," << Size;
1168 O << MAI->getCOMMDirective() << name << "," << Size;
1169 if (MAI->getCOMMDirectiveTakesAlignment())
1170 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
1173 if (GVar->hasLocalLinkage())
1174 O << "\t.local\t" << name << "\n";
1175 O << MAI->getCOMMDirective() << name << "," << Size;
1176 if (MAI->getCOMMDirectiveTakesAlignment())
1177 O << "," << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
1180 O << "\t\t" << MAI->getCommentString() << " ";
1181 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1188 switch (GVar->getLinkage()) {
1189 case GlobalValue::CommonLinkage:
1190 case GlobalValue::LinkOnceAnyLinkage:
1191 case GlobalValue::LinkOnceODRLinkage:
1192 case GlobalValue::WeakAnyLinkage:
1193 case GlobalValue::WeakODRLinkage:
1194 case GlobalValue::LinkerPrivateLinkage:
1196 O << "\t.globl " << name << "\n"
1197 << "\t.weak_definition " << name << "\n";
1199 O << "\t.weak " << name << "\n";
1202 case GlobalValue::AppendingLinkage:
1203 // FIXME: appending linkage variables should go into a section of
1204 // their name or something. For now, just emit them as external.
1205 case GlobalValue::ExternalLinkage:
1206 O << "\t.globl " << name << "\n";
1208 case GlobalValue::PrivateLinkage:
1209 case GlobalValue::InternalLinkage:
1212 llvm_unreachable("Unknown linkage type!");
1215 EmitAlignment(Align, GVar);
1218 O << "\t\t\t\t" << MAI->getCommentString() << " ";
1219 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1222 if (MAI->hasDotTypeDotSizeDirective())
1223 O << "\t.size " << name << ", " << Size << "\n";
1225 EmitGlobalConstant(C);
1230 bool ARMAsmPrinter::doFinalization(Module &M) {
1231 if (Subtarget->isTargetDarwin()) {
1232 // All darwin targets use mach-o.
1233 TargetLoweringObjectFileMachO &TLOFMacho =
1234 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1238 // Output non-lazy-pointers for external and common global variables.
1239 if (!GVNonLazyPtrs.empty()) {
1240 // Switch with ".non_lazy_symbol_pointer" directive.
1241 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
1243 for (StringMap<std::string>::iterator I = GVNonLazyPtrs.begin(),
1244 E = GVNonLazyPtrs.end(); I != E; ++I) {
1245 O << I->second << ":\n";
1246 O << "\t.indirect_symbol " << I->getKeyData() << "\n";
1247 O << "\t.long\t0\n";
1251 if (!HiddenGVNonLazyPtrs.empty()) {
1252 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
1254 for (StringMap<std::string>::iterator I = HiddenGVNonLazyPtrs.begin(),
1255 E = HiddenGVNonLazyPtrs.end(); I != E; ++I) {
1256 O << I->second << ":\n";
1257 O << "\t.long " << I->getKeyData() << "\n";
1261 // Funny Darwin hack: This flag tells the linker that no global symbols
1262 // contain code that falls through to other global symbols (e.g. the obvious
1263 // implementation of multiple entry points). If this doesn't occur, the
1264 // linker can safely perform dead code stripping. Since LLVM never
1265 // generates code that does this, it is always safe to set.
1266 O << "\t.subsections_via_symbols\n";
1269 return AsmPrinter::doFinalization(M);
1272 // Force static initialization.
1273 extern "C" void LLVMInitializeARMAsmPrinter() {
1274 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1275 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);