1 //===-- ARMAsmPrinter.cpp - ARM LLVM assembly writer ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to GAS-format ARM assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
17 #include "ARMBuildAttrs.h"
18 #include "ARMTargetMachine.h"
19 #include "ARMAddressingModes.h"
20 #include "ARMConstantPoolValue.h"
21 #include "ARMMachineFunctionInfo.h"
22 #include "llvm/Constants.h"
23 #include "llvm/Module.h"
24 #include "llvm/MDNode.h"
25 #include "llvm/CodeGen/AsmPrinter.h"
26 #include "llvm/CodeGen/DwarfWriter.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineJumpTableInfo.h"
30 #include "llvm/Target/TargetAsmInfo.h"
31 #include "llvm/Target/TargetData.h"
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/Target/TargetOptions.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/ADT/StringExtras.h"
36 #include "llvm/ADT/StringSet.h"
37 #include "llvm/Support/Compiler.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/Mangler.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Support/raw_ostream.h"
45 STATISTIC(EmittedInsts, "Number of machine instrs printed");
48 class VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
51 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
52 /// make the right decision when printing asm code for different targets.
53 const ARMSubtarget *Subtarget;
55 /// AFI - Keep a pointer to ARMFunctionInfo for the current
59 /// MCP - Keep a pointer to constantpool entries of the current
61 const MachineConstantPool *MCP;
63 /// We name each basic block in a Function with a unique number, so
64 /// that we can consistently refer to them later. This is cleared
65 /// at the beginning of each call to runOnMachineFunction().
67 typedef std::map<const Value *, unsigned> ValueMapTy;
68 ValueMapTy NumberForBB;
70 /// GVNonLazyPtrs - Keeps the set of GlobalValues that require
71 /// non-lazy-pointers for indirect access.
72 StringSet<> GVNonLazyPtrs;
74 /// HiddenGVNonLazyPtrs - Keeps the set of GlobalValues with hidden
75 /// visibility that require non-lazy-pointers for indirect access.
76 StringSet<> HiddenGVNonLazyPtrs;
78 /// FnStubs - Keeps the set of external function GlobalAddresses that the
79 /// asm printer should generate stubs for.
82 /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
85 explicit ARMAsmPrinter(raw_ostream &O, TargetMachine &TM,
86 const TargetAsmInfo *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);
138 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
139 unsigned AsmVariant, const char *ExtraCode);
140 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
142 const char *ExtraCode);
144 void printModuleLevelGV(const GlobalVariable* GVar);
145 bool printInstruction(const MachineInstr *MI); // autogenerated.
146 void printMachineInstruction(const MachineInstr *MI);
147 bool runOnMachineFunction(MachineFunction &F);
148 bool doInitialization(Module &M);
149 bool doFinalization(Module &M);
151 /// EmitMachineConstantPoolValue - Print a machine constantpool value to
153 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
154 printDataDirective(MCPV->getType());
156 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
157 GlobalValue *GV = ACPV->getGV();
160 Name = Mang->getMangledName(GV);
162 Name = std::string(TAI->getGlobalPrefix()) + ACPV->getSymbol();
163 if (ACPV->isNonLazyPointer()) {
164 if (GV->hasHiddenVisibility())
165 HiddenGVNonLazyPtrs.insert(Name);
167 GVNonLazyPtrs.insert(Name);
168 printSuffixedName(Name, "$non_lazy_ptr");
169 } else if (ACPV->isStub()) {
170 FnStubs.insert(Name);
171 printSuffixedName(Name, "$stub");
174 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
175 if (ACPV->getPCAdjustment() != 0) {
176 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
177 << utostr(ACPV->getLabelId())
178 << "+" << (unsigned)ACPV->getPCAdjustment();
179 if (ACPV->mustAddCurrentAddress())
186 void getAnalysisUsage(AnalysisUsage &AU) const {
187 AsmPrinter::getAnalysisUsage(AU);
188 AU.setPreservesAll();
189 AU.addRequired<MachineModuleInfo>();
190 AU.addRequired<DwarfWriter>();
193 } // end of anonymous namespace
195 #include "ARMGenAsmWriter.inc"
197 /// runOnMachineFunction - This uses the printInstruction()
198 /// method to print assembly for each instruction.
200 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
203 AFI = MF.getInfo<ARMFunctionInfo>();
204 MCP = MF.getConstantPool();
206 SetupMachineFunction(MF);
209 // NOTE: we don't print out constant pools here, they are handled as
213 // Print out labels for the function.
214 const Function *F = MF.getFunction();
215 switch (F->getLinkage()) {
216 default: LLVM_UNREACHABLE("Unknown linkage type!");
217 case Function::PrivateLinkage:
218 case Function::InternalLinkage:
219 SwitchToTextSection("\t.text", F);
221 case Function::ExternalLinkage:
222 SwitchToTextSection("\t.text", F);
223 O << "\t.globl\t" << CurrentFnName << "\n";
225 case Function::WeakAnyLinkage:
226 case Function::WeakODRLinkage:
227 case Function::LinkOnceAnyLinkage:
228 case Function::LinkOnceODRLinkage:
229 if (Subtarget->isTargetDarwin()) {
231 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
232 O << "\t.globl\t" << CurrentFnName << "\n";
233 O << "\t.weak_definition\t" << CurrentFnName << "\n";
235 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
240 printVisibility(CurrentFnName, F->getVisibility());
242 if (AFI->isThumbFunction()) {
243 EmitAlignment(MF.getAlignment(), F, AFI->getAlign());
244 O << "\t.code\t16\n";
245 O << "\t.thumb_func";
246 if (Subtarget->isTargetDarwin())
247 O << "\t" << CurrentFnName;
251 EmitAlignment(MF.getAlignment(), F);
254 O << CurrentFnName << ":\n";
255 // Emit pre-function debug information.
256 DW->BeginFunction(&MF);
258 if (Subtarget->isTargetDarwin()) {
259 // If the function is empty, then we need to emit *something*. Otherwise,
260 // the function's label might be associated with something that it wasn't
261 // meant to be associated with. We emit a noop in this situation.
262 MachineFunction::iterator I = MF.begin();
264 if (++I == MF.end() && MF.front().empty())
268 // Print out code for the function.
269 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
271 // Print a label for the basic block.
272 if (I != MF.begin()) {
273 printBasicBlockLabel(I, true, true, VerboseAsm);
276 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
278 // Print the assembly for the instruction.
279 printMachineInstruction(II);
283 if (TAI->hasDotTypeDotSizeDirective())
284 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
286 // Emit post-function debug information.
287 DW->EndFunction(&MF);
294 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
295 const char *Modifier) {
296 const MachineOperand &MO = MI->getOperand(OpNum);
297 switch (MO.getType()) {
298 case MachineOperand::MO_Register: {
299 unsigned Reg = MO.getReg();
300 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
301 if (Modifier && strcmp(Modifier, "dregpair") == 0) {
302 unsigned DRegLo = TRI->getSubReg(Reg, 5); // arm_dsubreg_0
303 unsigned DRegHi = TRI->getSubReg(Reg, 6); // arm_dsubreg_1
305 << TRI->getAsmName(DRegLo) << ',' << TRI->getAsmName(DRegHi)
307 } else if (Modifier && strcmp(Modifier, "dregsingle") == 0) {
308 O << '{' << TRI->getAsmName(Reg) << '}';
310 O << TRI->getAsmName(Reg);
313 LLVM_UNREACHABLE("not implemented");
316 case MachineOperand::MO_Immediate: {
317 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
323 case MachineOperand::MO_MachineBasicBlock:
324 printBasicBlockLabel(MO.getMBB());
326 case MachineOperand::MO_GlobalAddress: {
327 bool isCallOp = Modifier && !strcmp(Modifier, "call");
328 GlobalValue *GV = MO.getGlobal();
329 std::string Name = Mang->getMangledName(GV);
330 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
331 GV->hasLinkOnceLinkage());
332 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
333 TM.getRelocationModel() != Reloc::Static) {
334 printSuffixedName(Name, "$stub");
335 FnStubs.insert(Name);
339 printOffset(MO.getOffset());
341 if (isCallOp && Subtarget->isTargetELF() &&
342 TM.getRelocationModel() == Reloc::PIC_)
346 case MachineOperand::MO_ExternalSymbol: {
347 bool isCallOp = Modifier && !strcmp(Modifier, "call");
348 std::string Name(TAI->getGlobalPrefix());
349 Name += MO.getSymbolName();
350 if (isCallOp && Subtarget->isTargetDarwin() &&
351 TM.getRelocationModel() != Reloc::Static) {
352 printSuffixedName(Name, "$stub");
353 FnStubs.insert(Name);
356 if (isCallOp && Subtarget->isTargetELF() &&
357 TM.getRelocationModel() == Reloc::PIC_)
361 case MachineOperand::MO_ConstantPoolIndex:
362 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
363 << '_' << MO.getIndex();
365 case MachineOperand::MO_JumpTableIndex:
366 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
367 << '_' << MO.getIndex();
370 O << "<unknown operand type>"; abort (); break;
374 static void printSOImm(raw_ostream &O, int64_t V, bool VerboseAsm,
375 const TargetAsmInfo *TAI) {
376 // Break it up into two parts that make up a shifter immediate.
377 V = ARM_AM::getSOImmVal(V);
378 assert(V != -1 && "Not a valid so_imm value!");
380 unsigned Imm = ARM_AM::getSOImmValImm(V);
381 unsigned Rot = ARM_AM::getSOImmValRot(V);
383 // Print low-level immediate formation info, per
384 // A5.1.3: "Data-processing operands - Immediate".
386 O << "#" << Imm << ", " << Rot;
387 // Pretty printed version.
389 O << ' ' << TAI->getCommentString()
390 << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
396 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
397 /// immediate in bits 0-7.
398 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
399 const MachineOperand &MO = MI->getOperand(OpNum);
400 assert(MO.isImm() && "Not a valid so_imm value!");
401 printSOImm(O, MO.getImm(), VerboseAsm, TAI);
404 /// printSOImm2PartOperand - SOImm is broken into two pieces using a 'mov'
405 /// followed by an 'orr' to materialize.
406 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
407 const MachineOperand &MO = MI->getOperand(OpNum);
408 assert(MO.isImm() && "Not a valid so_imm value!");
409 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
410 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
411 printSOImm(O, V1, VerboseAsm, TAI);
413 printPredicateOperand(MI, 2);
419 printSOImm(O, V2, VerboseAsm, TAI);
422 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
423 // "Addressing Mode 1 - Data-processing operands" forms. This includes:
425 // REG REG 0,SH_OPC - e.g. R5, ROR R3
426 // REG 0 IMM,SH_OPC - e.g. R5, LSL #3
427 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
428 const MachineOperand &MO1 = MI->getOperand(Op);
429 const MachineOperand &MO2 = MI->getOperand(Op+1);
430 const MachineOperand &MO3 = MI->getOperand(Op+2);
432 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
433 O << TRI->getAsmName(MO1.getReg());
435 // Print the shift opc.
437 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
441 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
442 O << TRI->getAsmName(MO2.getReg());
443 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
445 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
449 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
450 const MachineOperand &MO1 = MI->getOperand(Op);
451 const MachineOperand &MO2 = MI->getOperand(Op+1);
452 const MachineOperand &MO3 = MI->getOperand(Op+2);
454 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
455 printOperand(MI, Op);
459 O << "[" << TRI->getAsmName(MO1.getReg());
462 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
464 << (char)ARM_AM::getAM2Op(MO3.getImm())
465 << ARM_AM::getAM2Offset(MO3.getImm());
471 << (char)ARM_AM::getAM2Op(MO3.getImm())
472 << TRI->getAsmName(MO2.getReg());
474 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
476 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
481 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
482 const MachineOperand &MO1 = MI->getOperand(Op);
483 const MachineOperand &MO2 = MI->getOperand(Op+1);
486 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
487 assert(ImmOffs && "Malformed indexed load / store!");
489 << (char)ARM_AM::getAM2Op(MO2.getImm())
494 O << (char)ARM_AM::getAM2Op(MO2.getImm())
495 << TRI->getAsmName(MO1.getReg());
497 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
499 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
503 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
504 const MachineOperand &MO1 = MI->getOperand(Op);
505 const MachineOperand &MO2 = MI->getOperand(Op+1);
506 const MachineOperand &MO3 = MI->getOperand(Op+2);
508 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
509 O << "[" << TRI->getAsmName(MO1.getReg());
513 << (char)ARM_AM::getAM3Op(MO3.getImm())
514 << TRI->getAsmName(MO2.getReg())
519 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
521 << (char)ARM_AM::getAM3Op(MO3.getImm())
526 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
527 const MachineOperand &MO1 = MI->getOperand(Op);
528 const MachineOperand &MO2 = MI->getOperand(Op+1);
531 O << (char)ARM_AM::getAM3Op(MO2.getImm())
532 << TRI->getAsmName(MO1.getReg());
536 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
537 assert(ImmOffs && "Malformed indexed load / store!");
539 << (char)ARM_AM::getAM3Op(MO2.getImm())
543 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
544 const char *Modifier) {
545 const MachineOperand &MO1 = MI->getOperand(Op);
546 const MachineOperand &MO2 = MI->getOperand(Op+1);
547 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
548 if (Modifier && strcmp(Modifier, "submode") == 0) {
549 if (MO1.getReg() == ARM::SP) {
550 bool isLDM = (MI->getOpcode() == ARM::LDM ||
551 MI->getOpcode() == ARM::LDM_RET);
552 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
554 O << ARM_AM::getAMSubModeStr(Mode);
556 printOperand(MI, Op);
557 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
562 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
563 const char *Modifier) {
564 const MachineOperand &MO1 = MI->getOperand(Op);
565 const MachineOperand &MO2 = MI->getOperand(Op+1);
567 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
568 printOperand(MI, Op);
572 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
574 if (Modifier && strcmp(Modifier, "submode") == 0) {
575 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
576 if (MO1.getReg() == ARM::SP) {
577 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
578 MI->getOpcode() == ARM::FLDMS);
579 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
581 O << ARM_AM::getAMSubModeStr(Mode);
583 } else if (Modifier && strcmp(Modifier, "base") == 0) {
584 // Used for FSTM{D|S} and LSTM{D|S} operations.
585 O << TRI->getAsmName(MO1.getReg());
586 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
591 O << "[" << TRI->getAsmName(MO1.getReg());
593 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
595 << (char)ARM_AM::getAM5Op(MO2.getImm())
601 void ARMAsmPrinter::printAddrMode6Operand(const MachineInstr *MI, int Op) {
602 const MachineOperand &MO1 = MI->getOperand(Op);
603 const MachineOperand &MO2 = MI->getOperand(Op+1);
604 const MachineOperand &MO3 = MI->getOperand(Op+2);
606 // FIXME: No support yet for specifying alignment.
607 O << "[" << TRI->getAsmName(MO1.getReg()) << "]";
609 if (ARM_AM::getAM6WBFlag(MO3.getImm())) {
610 if (MO2.getReg() == 0)
613 O << ", " << TRI->getAsmName(MO2.getReg());
617 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
618 const char *Modifier) {
619 if (Modifier && strcmp(Modifier, "label") == 0) {
620 printPCLabel(MI, Op+1);
624 const MachineOperand &MO1 = MI->getOperand(Op);
625 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
626 O << "[pc, +" << TRI->getAsmName(MO1.getReg()) << "]";
630 ARMAsmPrinter::printBitfieldInvMaskImmOperand(const MachineInstr *MI, int Op) {
631 const MachineOperand &MO = MI->getOperand(Op);
632 uint32_t v = ~MO.getImm();
633 int32_t lsb = CountTrailingZeros_32(v);
634 int32_t width = (32 - CountLeadingZeros_32 (v)) - lsb;
635 assert(MO.isImm() && "Not a valid bf_inv_mask_imm value!");
636 O << "#" << lsb << ", #" << width;
639 //===--------------------------------------------------------------------===//
642 ARMAsmPrinter::printThumbITMask(const MachineInstr *MI, int Op) {
643 // (3 - the number of trailing zeros) is the number of then / else.
644 unsigned Mask = MI->getOperand(Op).getImm();
645 unsigned NumTZ = CountTrailingZeros_32(Mask);
646 assert(NumTZ <= 3 && "Invalid IT mask!");
647 for (unsigned Pos = 3, e = NumTZ; Pos > e; --Pos) {
648 bool T = (Mask & (1 << Pos)) != 0;
657 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
658 const MachineOperand &MO1 = MI->getOperand(Op);
659 const MachineOperand &MO2 = MI->getOperand(Op+1);
660 O << "[" << TRI->getAsmName(MO1.getReg());
661 O << ", " << TRI->getAsmName(MO2.getReg()) << "]";
665 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
667 const MachineOperand &MO1 = MI->getOperand(Op);
668 const MachineOperand &MO2 = MI->getOperand(Op+1);
669 const MachineOperand &MO3 = MI->getOperand(Op+2);
671 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
672 printOperand(MI, Op);
676 O << "[" << TRI->getAsmName(MO1.getReg());
678 O << ", " << TRI->getAsmName(MO3.getReg());
679 else if (unsigned ImmOffs = MO2.getImm()) {
680 O << ", #" << ImmOffs;
688 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
689 printThumbAddrModeRI5Operand(MI, Op, 1);
692 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
693 printThumbAddrModeRI5Operand(MI, Op, 2);
696 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
697 printThumbAddrModeRI5Operand(MI, Op, 4);
700 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
701 const MachineOperand &MO1 = MI->getOperand(Op);
702 const MachineOperand &MO2 = MI->getOperand(Op+1);
703 O << "[" << TRI->getAsmName(MO1.getReg());
704 if (unsigned ImmOffs = MO2.getImm())
705 O << ", #" << ImmOffs << " * 4";
709 //===--------------------------------------------------------------------===//
711 // Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
712 // register with shift forms.
714 // REG IMM, SH_OPC - e.g. R5, LSL #3
715 void ARMAsmPrinter::printT2SOOperand(const MachineInstr *MI, int OpNum) {
716 const MachineOperand &MO1 = MI->getOperand(OpNum);
717 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
719 unsigned Reg = MO1.getReg();
720 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
721 O << TRI->getAsmName(Reg);
723 // Print the shift opc.
725 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO2.getImm()))
728 assert(MO2.isImm() && "Not a valid t2_so_reg value!");
729 O << "#" << ARM_AM::getSORegOffset(MO2.getImm());
732 void ARMAsmPrinter::printT2AddrModeImm12Operand(const MachineInstr *MI,
734 const MachineOperand &MO1 = MI->getOperand(OpNum);
735 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
737 O << "[" << TRI->getAsmName(MO1.getReg());
739 unsigned OffImm = MO2.getImm();
740 if (OffImm) // Don't print +0.
741 O << ", #+" << OffImm;
745 void ARMAsmPrinter::printT2AddrModeImm8Operand(const MachineInstr *MI,
747 const MachineOperand &MO1 = MI->getOperand(OpNum);
748 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
750 O << "[" << TRI->getAsmName(MO1.getReg());
752 int32_t OffImm = (int32_t)MO2.getImm();
755 O << ", #-" << -OffImm;
757 O << ", #+" << OffImm;
761 void ARMAsmPrinter::printT2AddrModeImm8s4Operand(const MachineInstr *MI,
763 const MachineOperand &MO1 = MI->getOperand(OpNum);
764 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
766 O << "[" << TRI->getAsmName(MO1.getReg());
768 int32_t OffImm = (int32_t)MO2.getImm() / 4;
771 O << ", #-" << -OffImm << " * 4";
773 O << ", #+" << OffImm << " * 4";
777 void ARMAsmPrinter::printT2AddrModeImm8OffsetOperand(const MachineInstr *MI,
779 const MachineOperand &MO1 = MI->getOperand(OpNum);
780 int32_t OffImm = (int32_t)MO1.getImm();
783 O << "#-" << -OffImm;
788 void ARMAsmPrinter::printT2AddrModeSoRegOperand(const MachineInstr *MI,
790 const MachineOperand &MO1 = MI->getOperand(OpNum);
791 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
792 const MachineOperand &MO3 = MI->getOperand(OpNum+2);
794 O << "[" << TRI->getAsmName(MO1.getReg());
797 O << ", +" << TRI->getAsmName(MO2.getReg());
799 unsigned ShAmt = MO3.getImm();
801 assert(ShAmt <= 3 && "Not a valid Thumb2 addressing mode!");
802 O << ", lsl #" << ShAmt;
809 //===--------------------------------------------------------------------===//
811 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int OpNum) {
812 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
814 O << ARMCondCodeToString(CC);
817 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int OpNum){
818 unsigned Reg = MI->getOperand(OpNum).getReg();
820 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
825 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int OpNum) {
826 int Id = (int)MI->getOperand(OpNum).getImm();
827 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
830 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int OpNum) {
832 for (unsigned i = OpNum, e = MI->getNumOperands(); i != e; ++i) {
834 if (i != e-1) O << ", ";
839 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNum,
840 const char *Modifier) {
841 assert(Modifier && "This operand only works with a modifier!");
842 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
844 if (!strcmp(Modifier, "label")) {
845 unsigned ID = MI->getOperand(OpNum).getImm();
846 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
847 << '_' << ID << ":\n";
849 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
850 unsigned CPI = MI->getOperand(OpNum).getIndex();
852 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
854 if (MCPE.isMachineConstantPoolEntry()) {
855 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
857 EmitGlobalConstant(MCPE.Val.ConstVal);
862 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNum) {
863 const MachineOperand &MO1 = MI->getOperand(OpNum);
864 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
865 unsigned JTI = MO1.getIndex();
866 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
867 << '_' << JTI << '_' << MO2.getImm() << ":\n";
869 const char *JTEntryDirective = TAI->getJumpTableDirective();
870 if (!JTEntryDirective)
871 JTEntryDirective = TAI->getData32bitsDirective();
873 const MachineFunction *MF = MI->getParent()->getParent();
874 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
875 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
876 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
877 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
878 std::set<MachineBasicBlock*> JTSets;
879 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
880 MachineBasicBlock *MBB = JTBBs[i];
881 if (UseSet && JTSets.insert(MBB).second)
882 printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
884 O << JTEntryDirective << ' ';
886 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
887 << '_' << JTI << '_' << MO2.getImm()
888 << "_set_" << MBB->getNumber();
889 else if (TM.getRelocationModel() == Reloc::PIC_) {
890 printBasicBlockLabel(MBB, false, false, false);
891 // If the arch uses custom Jump Table directives, don't calc relative to JT
892 if (!TAI->getJumpTableDirective())
893 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
894 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
896 printBasicBlockLabel(MBB, false, false, false);
903 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
904 unsigned AsmVariant, const char *ExtraCode){
905 // Does this asm operand have a single letter operand modifier?
906 if (ExtraCode && ExtraCode[0]) {
907 if (ExtraCode[1] != 0) return true; // Unknown modifier.
909 switch (ExtraCode[0]) {
910 default: return true; // Unknown modifier.
911 case 'a': // Print as a memory address.
912 if (MI->getOperand(OpNum).isReg()) {
913 O << "[" << TRI->getAsmName(MI->getOperand(OpNum).getReg()) << "]";
917 case 'c': // Don't print "#" before an immediate operand.
918 printOperand(MI, OpNum, "no_hash");
920 case 'P': // Print a VFP double precision register.
921 printOperand(MI, OpNum);
924 if (TM.getTargetData()->isLittleEndian())
928 if (TM.getTargetData()->isBigEndian())
931 case 'H': // Write second word of DI / DF reference.
932 // Verify that this operand has two consecutive registers.
933 if (!MI->getOperand(OpNum).isReg() ||
934 OpNum+1 == MI->getNumOperands() ||
935 !MI->getOperand(OpNum+1).isReg())
937 ++OpNum; // Return the high-part.
941 printOperand(MI, OpNum);
945 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
946 unsigned OpNum, unsigned AsmVariant,
947 const char *ExtraCode) {
948 if (ExtraCode && ExtraCode[0])
949 return true; // Unknown modifier.
950 printAddrMode2Operand(MI, OpNum);
954 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
957 int Opc = MI->getOpcode();
959 case ARM::CONSTPOOL_ENTRY:
960 if (!InCPMode && AFI->isThumbFunction()) {
966 if (InCPMode && AFI->isThumbFunction())
970 // Call the autogenerated instruction printer routines.
971 printInstruction(MI);
974 bool ARMAsmPrinter::doInitialization(Module &M) {
976 bool Result = AsmPrinter::doInitialization(M);
977 DW = getAnalysisIfAvailable<DwarfWriter>();
979 // Use unified assembler syntax mode for Thumb.
980 if (Subtarget->isThumb())
981 O << "\t.syntax unified\n";
983 // Emit ARM Build Attributes
984 if (Subtarget->isTargetELF()) {
986 std::string CPUString = Subtarget->getCPUString();
987 if (CPUString != "generic")
988 O << "\t.cpu " << CPUString << '\n';
990 // FIXME: Emit FPU type
991 if (Subtarget->hasVFP2())
992 O << "\t.eabi_attribute " << ARMBuildAttrs::VFP_arch << ", 2\n";
994 // Signal various FP modes.
996 O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_denormal << ", 1\n"
997 << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_exceptions << ", 1\n";
999 if (FiniteOnlyFPMath())
1000 O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 1\n";
1002 O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 3\n";
1004 // 8-bytes alignment stuff.
1005 O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_needed << ", 1\n"
1006 << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_preserved << ", 1\n";
1008 // FIXME: Should we signal R9 usage?
1014 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
1015 /// Don't print things like \\n or \\0.
1016 static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
1017 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
1023 void ARMAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
1024 const TargetData *TD = TM.getTargetData();
1026 if (!GVar->hasInitializer()) // External global require no code
1029 // Check to see if this is a special global used by LLVM, if so, emit it.
1031 if (EmitSpecialLLVMGlobal(GVar)) {
1032 if (Subtarget->isTargetDarwin() &&
1033 TM.getRelocationModel() == Reloc::Static) {
1034 if (GVar->getName() == "llvm.global_ctors")
1035 O << ".reference .constructors_used\n";
1036 else if (GVar->getName() == "llvm.global_dtors")
1037 O << ".reference .destructors_used\n";
1042 std::string name = Mang->getMangledName(GVar);
1043 Constant *C = GVar->getInitializer();
1044 if (isa<MDNode>(C) || isa<MDString>(C))
1046 const Type *Type = C->getType();
1047 unsigned Size = TD->getTypeAllocSize(Type);
1048 unsigned Align = TD->getPreferredAlignmentLog(GVar);
1049 bool isDarwin = Subtarget->isTargetDarwin();
1051 printVisibility(name, GVar->getVisibility());
1053 if (Subtarget->isTargetELF())
1054 O << "\t.type " << name << ",%object\n";
1056 if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal() &&
1058 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
1059 // FIXME: This seems to be pretty darwin-specific
1061 if (GVar->hasExternalLinkage()) {
1062 SwitchToSection(TAI->SectionForGlobal(GVar));
1063 if (const char *Directive = TAI->getZeroFillDirective()) {
1064 O << "\t.globl\t" << name << "\n";
1065 O << Directive << "__DATA, __common, " << name << ", "
1066 << Size << ", " << Align << "\n";
1071 if (GVar->hasLocalLinkage() || GVar->isWeakForLinker()) {
1072 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
1075 if (GVar->hasLocalLinkage()) {
1076 O << TAI->getLCOMMDirective() << name << "," << Size
1078 } else if (GVar->hasCommonLinkage()) {
1079 O << TAI->getCOMMDirective() << name << "," << Size
1082 SwitchToSection(TAI->SectionForGlobal(GVar));
1083 O << "\t.globl " << name << '\n'
1084 << TAI->getWeakDefDirective() << name << '\n';
1085 EmitAlignment(Align, GVar);
1088 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
1089 PrintUnmangledNameSafely(GVar, O);
1092 EmitGlobalConstant(C);
1095 } else if (TAI->getLCOMMDirective() != NULL) {
1096 if (GVar->hasLocalLinkage()) {
1097 O << TAI->getLCOMMDirective() << name << "," << Size;
1099 O << TAI->getCOMMDirective() << name << "," << Size;
1100 if (TAI->getCOMMDirectiveTakesAlignment())
1101 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
1104 SwitchToSection(TAI->SectionForGlobal(GVar));
1105 if (GVar->hasLocalLinkage())
1106 O << "\t.local\t" << name << "\n";
1107 O << TAI->getCOMMDirective() << name << "," << Size;
1108 if (TAI->getCOMMDirectiveTakesAlignment())
1109 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
1112 O << "\t\t" << TAI->getCommentString() << " ";
1113 PrintUnmangledNameSafely(GVar, O);
1120 SwitchToSection(TAI->SectionForGlobal(GVar));
1121 switch (GVar->getLinkage()) {
1122 case GlobalValue::CommonLinkage:
1123 case GlobalValue::LinkOnceAnyLinkage:
1124 case GlobalValue::LinkOnceODRLinkage:
1125 case GlobalValue::WeakAnyLinkage:
1126 case GlobalValue::WeakODRLinkage:
1128 O << "\t.globl " << name << "\n"
1129 << "\t.weak_definition " << name << "\n";
1131 O << "\t.weak " << name << "\n";
1134 case GlobalValue::AppendingLinkage:
1135 // FIXME: appending linkage variables should go into a section of
1136 // their name or something. For now, just emit them as external.
1137 case GlobalValue::ExternalLinkage:
1138 O << "\t.globl " << name << "\n";
1140 case GlobalValue::PrivateLinkage:
1141 case GlobalValue::InternalLinkage:
1144 LLVM_UNREACHABLE("Unknown linkage type!");
1147 EmitAlignment(Align, GVar);
1150 O << "\t\t\t\t" << TAI->getCommentString() << " ";
1151 PrintUnmangledNameSafely(GVar, O);
1154 if (TAI->hasDotTypeDotSizeDirective())
1155 O << "\t.size " << name << ", " << Size << "\n";
1157 EmitGlobalConstant(C);
1162 bool ARMAsmPrinter::doFinalization(Module &M) {
1163 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1165 printModuleLevelGV(I);
1167 if (Subtarget->isTargetDarwin()) {
1168 SwitchToDataSection("");
1170 // Output stubs for dynamically-linked functions
1171 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1173 if (TM.getRelocationModel() == Reloc::PIC_)
1174 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
1177 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
1181 O << "\t.code\t32\n";
1183 const char *p = i->getKeyData();
1184 printSuffixedName(p, "$stub");
1186 O << "\t.indirect_symbol " << p << "\n";
1188 printSuffixedName(p, "$slp");
1190 if (TM.getRelocationModel() == Reloc::PIC_) {
1191 printSuffixedName(p, "$scv");
1193 O << "\tadd ip, pc, ip\n";
1195 O << "\tldr pc, [ip, #0]\n";
1196 printSuffixedName(p, "$slp");
1199 printSuffixedName(p, "$lazy_ptr");
1200 if (TM.getRelocationModel() == Reloc::PIC_) {
1202 printSuffixedName(p, "$scv");
1206 SwitchToDataSection(".lazy_symbol_pointer", 0);
1207 printSuffixedName(p, "$lazy_ptr");
1209 O << "\t.indirect_symbol " << p << "\n";
1210 O << "\t.long\tdyld_stub_binding_helper\n";
1214 // Output non-lazy-pointers for external and common global variables.
1215 if (!GVNonLazyPtrs.empty()) {
1216 SwitchToDataSection("\t.non_lazy_symbol_pointer", 0);
1217 for (StringSet<>::iterator i = GVNonLazyPtrs.begin(),
1218 e = GVNonLazyPtrs.end(); i != e; ++i) {
1219 const char *p = i->getKeyData();
1220 printSuffixedName(p, "$non_lazy_ptr");
1222 O << "\t.indirect_symbol " << p << "\n";
1223 O << "\t.long\t0\n";
1227 if (!HiddenGVNonLazyPtrs.empty()) {
1228 SwitchToSection(TAI->getDataSection());
1229 for (StringSet<>::iterator i = HiddenGVNonLazyPtrs.begin(),
1230 e = HiddenGVNonLazyPtrs.end(); i != e; ++i) {
1231 const char *p = i->getKeyData();
1233 printSuffixedName(p, "$non_lazy_ptr");
1235 O << "\t.long " << p << "\n";
1240 // Funny Darwin hack: This flag tells the linker that no global symbols
1241 // contain code that falls through to other global symbols (e.g. the obvious
1242 // implementation of multiple entry points). If this doesn't occur, the
1243 // linker can safely perform dead code stripping. Since LLVM never
1244 // generates code that does this, it is always safe to set.
1245 O << "\t.subsections_via_symbols\n";
1248 return AsmPrinter::doFinalization(M);
1251 /// createARMCodePrinterPass - Returns a pass that prints the ARM
1252 /// assembly code for a MachineFunction to the given output stream,
1253 /// using the given target machine description. This should work
1254 /// regardless of whether the function is in SSA form.
1256 FunctionPass *llvm::createARMCodePrinterPass(raw_ostream &o,
1257 ARMBaseTargetMachine &tm,
1259 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
1263 static struct Register {
1265 ARMBaseTargetMachine::registerAsmPrinter(createARMCodePrinterPass);
1270 // Force static initialization.
1271 extern "C" void LLVMInitializeARMAsmPrinter() { }