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 "ARMTargetMachine.h"
18 #include "ARMAddressingModes.h"
19 #include "ARMConstantPoolValue.h"
20 #include "ARMMachineFunctionInfo.h"
21 #include "llvm/Constants.h"
22 #include "llvm/Module.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/Target/TargetAsmInfo.h"
29 #include "llvm/Target/TargetData.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/ADT/StringExtras.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/Mangler.h"
36 #include "llvm/Support/MathExtras.h"
40 STATISTIC(EmittedInsts, "Number of machine instrs printed");
43 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
44 ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
45 : AsmPrinter(O, TM, T), DW(O, this, T), AFI(NULL), InCPMode(false) {
46 Subtarget = &TM.getSubtarget<ARMSubtarget>();
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 /// We name each basic block in a Function with a unique number, so
60 /// that we can consistently refer to them later. This is cleared
61 /// at the beginning of each call to runOnMachineFunction().
63 typedef std::map<const Value *, unsigned> ValueMapTy;
64 ValueMapTy NumberForBB;
66 /// Keeps the set of GlobalValues that require non-lazy-pointers for
68 std::set<std::string> GVNonLazyPtrs;
70 /// Keeps the set of external function GlobalAddresses that the asm
71 /// printer should generate stubs for.
72 std::set<std::string> FnStubs;
74 /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
77 virtual const char *getPassName() const {
78 return "ARM Assembly Printer";
81 void printOperand(const MachineInstr *MI, int opNum,
82 const char *Modifier = 0);
83 void printSOImmOperand(const MachineInstr *MI, int opNum);
84 void printSOImm2PartOperand(const MachineInstr *MI, int opNum);
85 void printSORegOperand(const MachineInstr *MI, int opNum);
86 void printAddrMode2Operand(const MachineInstr *MI, int OpNo);
87 void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNo);
88 void printAddrMode3Operand(const MachineInstr *MI, int OpNo);
89 void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNo);
90 void printAddrMode4Operand(const MachineInstr *MI, int OpNo,
91 const char *Modifier = 0);
92 void printAddrMode5Operand(const MachineInstr *MI, int OpNo,
93 const char *Modifier = 0);
94 void printAddrModePCOperand(const MachineInstr *MI, int OpNo,
95 const char *Modifier = 0);
96 void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNo);
97 void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNo,
99 void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNo);
100 void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNo);
101 void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNo);
102 void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNo);
103 void printPredicateOperand(const MachineInstr *MI, int opNum);
104 void printSBitModifierOperand(const MachineInstr *MI, int opNum);
105 void printPCLabel(const MachineInstr *MI, int opNum);
106 void printRegisterList(const MachineInstr *MI, int opNum);
107 void printCPInstOperand(const MachineInstr *MI, int opNum,
108 const char *Modifier);
109 void printJTBlockOperand(const MachineInstr *MI, int opNum);
111 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
112 unsigned AsmVariant, const char *ExtraCode);
114 bool printInstruction(const MachineInstr *MI); // autogenerated.
115 void printMachineInstruction(const MachineInstr *MI);
116 bool runOnMachineFunction(MachineFunction &F);
117 bool doInitialization(Module &M);
118 bool doFinalization(Module &M);
120 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
121 printDataDirective(MCPV->getType());
123 ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MCPV;
124 GlobalValue *GV = ACPV->getGV();
125 std::string Name = GV ? Mang->getValueName(GV) : TAI->getGlobalPrefix();
127 Name += ACPV->getSymbol();
128 if (ACPV->isNonLazyPointer()) {
129 GVNonLazyPtrs.insert(Name);
130 O << TAI->getPrivateGlobalPrefix() << Name << "$non_lazy_ptr";
131 } else if (ACPV->isStub()) {
132 FnStubs.insert(Name);
133 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
136 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
137 if (ACPV->getPCAdjustment() != 0) {
138 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
139 << utostr(ACPV->getLabelId())
140 << "+" << (unsigned)ACPV->getPCAdjustment();
141 if (ACPV->mustAddCurrentAddress())
147 // If the constant pool value is a extern weak symbol, remember to emit
148 // the weak reference.
149 if (GV && GV->hasExternalWeakLinkage())
150 ExtWeakSymbols.insert(GV);
153 void getAnalysisUsage(AnalysisUsage &AU) const {
154 AsmPrinter::getAnalysisUsage(AU);
155 AU.setPreservesAll();
156 AU.addRequired<MachineModuleInfo>();
159 } // end of anonymous namespace
161 #include "ARMGenAsmWriter.inc"
163 /// createARMCodePrinterPass - Returns a pass that prints the ARM
164 /// assembly code for a MachineFunction to the given output stream,
165 /// using the given target machine description. This should work
166 /// regardless of whether the function is in SSA form.
168 FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
169 ARMTargetMachine &tm) {
170 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
173 /// runOnMachineFunction - This uses the printInstruction()
174 /// method to print assembly for each instruction.
176 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
177 AFI = MF.getInfo<ARMFunctionInfo>();
179 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
181 SetupMachineFunction(MF);
184 // NOTE: we don't print out constant pools here, they are handled as
188 // Print out labels for the function.
189 const Function *F = MF.getFunction();
190 switch (F->getLinkage()) {
191 default: assert(0 && "Unknown linkage type!");
192 case Function::InternalLinkage:
193 SwitchToTextSection("\t.text", F);
195 case Function::ExternalLinkage:
196 SwitchToTextSection("\t.text", F);
197 O << "\t.globl\t" << CurrentFnName << "\n";
199 case Function::WeakLinkage:
200 case Function::LinkOnceLinkage:
201 if (Subtarget->isTargetDarwin()) {
203 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
204 O << "\t.globl\t" << CurrentFnName << "\n";
205 O << "\t.weak_definition\t" << CurrentFnName << "\n";
207 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
212 const char *VisibilityDirective = NULL;
213 if (F->hasHiddenVisibility())
214 VisibilityDirective = TAI->getHiddenDirective();
215 else if (F->hasProtectedVisibility())
216 VisibilityDirective = TAI->getProtectedDirective();
218 if (VisibilityDirective)
219 O << VisibilityDirective << CurrentFnName << "\n";
221 if (AFI->isThumbFunction()) {
222 EmitAlignment(1, F, AFI->getAlign());
223 O << "\t.code\t16\n";
224 O << "\t.thumb_func";
225 if (Subtarget->isTargetDarwin())
226 O << "\t" << CurrentFnName;
232 O << CurrentFnName << ":\n";
233 // Emit pre-function debug information.
234 DW.BeginFunction(&MF);
236 if (Subtarget->isTargetDarwin()) {
237 // If the function is empty, then we need to emit *something*. Otherwise,
238 // the function's label might be associated with something that it wasn't
239 // meant to be associated with. We emit a noop in this situation.
240 MachineFunction::iterator I = MF.begin();
242 if (++I == MF.end() && MF.front().empty())
246 // Print out code for the function.
247 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
249 // Print a label for the basic block.
250 if (I != MF.begin()) {
251 printBasicBlockLabel(I, true, true);
254 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
256 // Print the assembly for the instruction.
257 printMachineInstruction(II);
261 if (TAI->hasDotTypeDotSizeDirective())
262 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
264 // Emit post-function debug information.
270 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
271 const char *Modifier) {
272 const MachineOperand &MO = MI->getOperand(opNum);
273 switch (MO.getType()) {
274 case MachineOperand::MO_Register:
275 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
276 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
278 assert(0 && "not implemented");
280 case MachineOperand::MO_Immediate: {
281 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
284 O << (int)MO.getImm();
287 case MachineOperand::MO_MachineBasicBlock:
288 printBasicBlockLabel(MO.getMBB());
290 case MachineOperand::MO_GlobalAddress: {
291 bool isCallOp = Modifier && !strcmp(Modifier, "call");
292 GlobalValue *GV = MO.getGlobal();
293 std::string Name = Mang->getValueName(GV);
294 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
295 GV->hasLinkOnceLinkage());
296 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
297 TM.getRelocationModel() != Reloc::Static) {
298 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
299 FnStubs.insert(Name);
303 if (MO.getOffset() > 0)
304 O << '+' << MO.getOffset();
305 else if (MO.getOffset() < 0)
308 if (isCallOp && Subtarget->isTargetELF() &&
309 TM.getRelocationModel() == Reloc::PIC_)
311 if (GV->hasExternalWeakLinkage())
312 ExtWeakSymbols.insert(GV);
315 case MachineOperand::MO_ExternalSymbol: {
316 bool isCallOp = Modifier && !strcmp(Modifier, "call");
317 std::string Name(TAI->getGlobalPrefix());
318 Name += MO.getSymbolName();
319 if (isCallOp && Subtarget->isTargetDarwin() &&
320 TM.getRelocationModel() != Reloc::Static) {
321 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
322 FnStubs.insert(Name);
325 if (isCallOp && Subtarget->isTargetELF() &&
326 TM.getRelocationModel() == Reloc::PIC_)
330 case MachineOperand::MO_ConstantPoolIndex:
331 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
332 << '_' << MO.getIndex();
334 case MachineOperand::MO_JumpTableIndex:
335 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
336 << '_' << MO.getIndex();
339 O << "<unknown operand type>"; abort (); break;
343 static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
344 assert(V < (1 << 12) && "Not a valid so_imm value!");
345 unsigned Imm = ARM_AM::getSOImmValImm(V);
346 unsigned Rot = ARM_AM::getSOImmValRot(V);
348 // Print low-level immediate formation info, per
349 // A5.1.3: "Data-processing operands - Immediate".
351 O << "#" << Imm << ", " << Rot;
352 // Pretty printed version.
353 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
359 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
360 /// immediate in bits 0-7.
361 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
362 const MachineOperand &MO = MI->getOperand(OpNum);
363 assert(MO.isImmediate() && "Not a valid so_imm value!");
364 printSOImm(O, MO.getImm(), TAI);
367 /// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
368 /// followed by a or to materialize.
369 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
370 const MachineOperand &MO = MI->getOperand(OpNum);
371 assert(MO.isImmediate() && "Not a valid so_imm value!");
372 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
373 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
374 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
376 printPredicateOperand(MI, 2);
382 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
385 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
386 // "Addressing Mode 1 - Data-processing operands" forms. This includes:
388 // REG REG 0,SH_OPC - e.g. R5, ROR R3
389 // REG 0 IMM,SH_OPC - e.g. R5, LSL #3
390 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
391 const MachineOperand &MO1 = MI->getOperand(Op);
392 const MachineOperand &MO2 = MI->getOperand(Op+1);
393 const MachineOperand &MO3 = MI->getOperand(Op+2);
395 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
396 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
398 // Print the shift opc.
400 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
404 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
405 O << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
406 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
408 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
412 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
413 const MachineOperand &MO1 = MI->getOperand(Op);
414 const MachineOperand &MO2 = MI->getOperand(Op+1);
415 const MachineOperand &MO3 = MI->getOperand(Op+2);
417 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
418 printOperand(MI, Op);
422 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
425 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
427 << (char)ARM_AM::getAM2Op(MO3.getImm())
428 << ARM_AM::getAM2Offset(MO3.getImm());
434 << (char)ARM_AM::getAM2Op(MO3.getImm())
435 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
437 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
439 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
444 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
445 const MachineOperand &MO1 = MI->getOperand(Op);
446 const MachineOperand &MO2 = MI->getOperand(Op+1);
449 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
450 assert(ImmOffs && "Malformed indexed load / store!");
452 << (char)ARM_AM::getAM2Op(MO2.getImm())
457 O << (char)ARM_AM::getAM2Op(MO2.getImm())
458 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
460 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
462 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
466 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
467 const MachineOperand &MO1 = MI->getOperand(Op);
468 const MachineOperand &MO2 = MI->getOperand(Op+1);
469 const MachineOperand &MO3 = MI->getOperand(Op+2);
471 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
472 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
476 << (char)ARM_AM::getAM3Op(MO3.getImm())
477 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName
482 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
484 << (char)ARM_AM::getAM3Op(MO3.getImm())
489 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
490 const MachineOperand &MO1 = MI->getOperand(Op);
491 const MachineOperand &MO2 = MI->getOperand(Op+1);
494 O << (char)ARM_AM::getAM3Op(MO2.getImm())
495 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
499 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
500 assert(ImmOffs && "Malformed indexed load / store!");
502 << (char)ARM_AM::getAM3Op(MO2.getImm())
506 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
507 const char *Modifier) {
508 const MachineOperand &MO1 = MI->getOperand(Op);
509 const MachineOperand &MO2 = MI->getOperand(Op+1);
510 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
511 if (Modifier && strcmp(Modifier, "submode") == 0) {
512 if (MO1.getReg() == ARM::SP) {
513 bool isLDM = (MI->getOpcode() == ARM::LDM ||
514 MI->getOpcode() == ARM::LDM_RET);
515 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
517 O << ARM_AM::getAMSubModeStr(Mode);
519 printOperand(MI, Op);
520 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
525 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
526 const char *Modifier) {
527 const MachineOperand &MO1 = MI->getOperand(Op);
528 const MachineOperand &MO2 = MI->getOperand(Op+1);
530 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
531 printOperand(MI, Op);
535 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
537 if (Modifier && strcmp(Modifier, "submode") == 0) {
538 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
539 if (MO1.getReg() == ARM::SP) {
540 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
541 MI->getOpcode() == ARM::FLDMS);
542 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
544 O << ARM_AM::getAMSubModeStr(Mode);
546 } else if (Modifier && strcmp(Modifier, "base") == 0) {
547 // Used for FSTM{D|S} and LSTM{D|S} operations.
548 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
549 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
554 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
556 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
558 << (char)ARM_AM::getAM5Op(MO2.getImm())
564 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
565 const char *Modifier) {
566 if (Modifier && strcmp(Modifier, "label") == 0) {
567 printPCLabel(MI, Op+1);
571 const MachineOperand &MO1 = MI->getOperand(Op);
572 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
573 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName << "]";
577 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
578 const MachineOperand &MO1 = MI->getOperand(Op);
579 const MachineOperand &MO2 = MI->getOperand(Op+1);
580 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
581 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).AsmName << "]";
585 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
587 const MachineOperand &MO1 = MI->getOperand(Op);
588 const MachineOperand &MO2 = MI->getOperand(Op+1);
589 const MachineOperand &MO3 = MI->getOperand(Op+2);
591 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
592 printOperand(MI, Op);
596 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
598 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).AsmName;
599 else if (unsigned ImmOffs = MO2.getImm()) {
600 O << ", #" << ImmOffs;
608 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
609 printThumbAddrModeRI5Operand(MI, Op, 1);
612 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
613 printThumbAddrModeRI5Operand(MI, Op, 2);
616 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
617 printThumbAddrModeRI5Operand(MI, Op, 4);
620 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
621 const MachineOperand &MO1 = MI->getOperand(Op);
622 const MachineOperand &MO2 = MI->getOperand(Op+1);
623 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
624 if (unsigned ImmOffs = MO2.getImm())
625 O << ", #" << ImmOffs << " * 4";
629 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int opNum) {
630 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(opNum).getImm();
632 O << ARMCondCodeToString(CC);
635 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int opNum){
636 unsigned Reg = MI->getOperand(opNum).getReg();
638 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
643 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
644 int Id = (int)MI->getOperand(opNum).getImm();
645 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
648 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
650 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
652 if (i != e-1) O << ", ";
657 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
658 const char *Modifier) {
659 assert(Modifier && "This operand only works with a modifier!");
660 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
662 if (!strcmp(Modifier, "label")) {
663 unsigned ID = MI->getOperand(OpNo).getImm();
664 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
665 << '_' << ID << ":\n";
667 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
668 unsigned CPI = MI->getOperand(OpNo).getIndex();
670 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
671 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
673 if (MCPE.isMachineConstantPoolEntry())
674 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
676 EmitGlobalConstant(MCPE.Val.ConstVal);
677 // remember to emit the weak reference
678 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
679 if (GV->hasExternalWeakLinkage())
680 ExtWeakSymbols.insert(GV);
685 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
686 const MachineOperand &MO1 = MI->getOperand(OpNo);
687 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
688 unsigned JTI = MO1.getIndex();
689 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
690 << '_' << JTI << '_' << MO2.getImm() << ":\n";
692 const char *JTEntryDirective = TAI->getJumpTableDirective();
693 if (!JTEntryDirective)
694 JTEntryDirective = TAI->getData32bitsDirective();
696 const MachineFunction *MF = MI->getParent()->getParent();
697 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
698 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
699 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
700 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
701 std::set<MachineBasicBlock*> JTSets;
702 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
703 MachineBasicBlock *MBB = JTBBs[i];
704 if (UseSet && JTSets.insert(MBB).second)
705 printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
707 O << JTEntryDirective << ' ';
709 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
710 << '_' << JTI << '_' << MO2.getImm()
711 << "_set_" << MBB->getNumber();
712 else if (TM.getRelocationModel() == Reloc::PIC_) {
713 printBasicBlockLabel(MBB, false, false, false);
714 // If the arch uses custom Jump Table directives, don't calc relative to JT
715 if (!TAI->getJumpTableDirective())
716 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
717 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
719 printBasicBlockLabel(MBB, false, false, false);
726 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
727 unsigned AsmVariant, const char *ExtraCode){
728 // Does this asm operand have a single letter operand modifier?
729 if (ExtraCode && ExtraCode[0]) {
730 if (ExtraCode[1] != 0) return true; // Unknown modifier.
732 switch (ExtraCode[0]) {
733 default: return true; // Unknown modifier.
734 case 'c': // Don't print "$" before a global var name or constant.
735 case 'P': // Print a VFP double precision register.
736 printOperand(MI, OpNo);
739 if (TM.getTargetData()->isLittleEndian())
743 if (TM.getTargetData()->isBigEndian())
746 case 'H': // Write second word of DI / DF reference.
747 // Verify that this operand has two consecutive registers.
748 if (!MI->getOperand(OpNo).isRegister() ||
749 OpNo+1 == MI->getNumOperands() ||
750 !MI->getOperand(OpNo+1).isRegister())
752 ++OpNo; // Return the high-part.
756 printOperand(MI, OpNo);
760 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
763 int Opc = MI->getOpcode();
765 case ARM::CONSTPOOL_ENTRY:
766 if (!InCPMode && AFI->isThumbFunction()) {
772 if (InCPMode && AFI->isThumbFunction())
793 // Call the autogenerated instruction printer routines.
794 printInstruction(MI);
797 bool ARMAsmPrinter::doInitialization(Module &M) {
798 // Emit initial debug information.
801 bool Result = AsmPrinter::doInitialization(M);
803 // Darwin wants symbols to be quoted if they have complex names.
804 if (Subtarget->isTargetDarwin())
805 Mang->setUseQuotes(true);
810 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
811 /// Don't print things like \n or \0.
812 static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
813 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
819 bool ARMAsmPrinter::doFinalization(Module &M) {
820 const TargetData *TD = TM.getTargetData();
822 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
824 if (!I->hasInitializer()) // External global require no code
827 if (EmitSpecialLLVMGlobal(I)) {
828 if (Subtarget->isTargetDarwin() &&
829 TM.getRelocationModel() == Reloc::Static) {
830 if (I->getName() == "llvm.global_ctors")
831 O << ".reference .constructors_used\n";
832 else if (I->getName() == "llvm.global_dtors")
833 O << ".reference .destructors_used\n";
838 std::string name = Mang->getValueName(I);
839 Constant *C = I->getInitializer();
840 const Type *Type = C->getType();
841 unsigned Size = TD->getABITypeSize(Type);
842 unsigned Align = TD->getPreferredAlignmentLog(I);
844 const char *VisibilityDirective = NULL;
845 if (I->hasHiddenVisibility())
846 VisibilityDirective = TAI->getHiddenDirective();
847 else if (I->hasProtectedVisibility())
848 VisibilityDirective = TAI->getProtectedDirective();
850 if (VisibilityDirective)
851 O << VisibilityDirective << name << "\n";
853 if (Subtarget->isTargetELF())
854 O << "\t.type " << name << ",%object\n";
856 if (C->isNullValue() && !I->hasSection() && !I->isThreadLocal()) {
857 if (I->hasExternalLinkage()) {
858 if (const char *Directive = TAI->getZeroFillDirective()) {
859 O << "\t.globl\t" << name << "\n";
860 O << Directive << "__DATA, __common, " << name << ", "
861 << Size << ", " << Align << "\n";
866 if (I->hasInternalLinkage() || I->hasWeakLinkage() ||
867 I->hasLinkOnceLinkage()) {
868 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
869 if (!NoZerosInBSS && TAI->getBSSSection())
870 SwitchToDataSection(TAI->getBSSSection(), I);
872 SwitchToDataSection(TAI->getDataSection(), I);
873 if (TAI->getLCOMMDirective() != NULL) {
874 if (I->hasInternalLinkage()) {
875 O << TAI->getLCOMMDirective() << name << "," << Size;
876 if (Subtarget->isTargetDarwin())
879 O << TAI->getCOMMDirective() << name << "," << Size;
881 if (I->hasInternalLinkage())
882 O << "\t.local\t" << name << "\n";
883 O << TAI->getCOMMDirective() << name << "," << Size;
884 if (TAI->getCOMMDirectiveTakesAlignment())
885 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
887 O << "\t\t" << TAI->getCommentString() << " ";
888 PrintUnmangledNameSafely(I, O);
894 switch (I->getLinkage()) {
895 case GlobalValue::LinkOnceLinkage:
896 case GlobalValue::WeakLinkage:
897 if (Subtarget->isTargetDarwin()) {
898 O << "\t.globl " << name << "\n"
899 << "\t.weak_definition " << name << "\n";
900 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
902 std::string SectionName("\t.section\t.llvm.linkonce.d." +
904 ",\"aw\",%progbits");
905 SwitchToDataSection(SectionName.c_str(), I);
906 O << "\t.weak " << name << "\n";
909 case GlobalValue::AppendingLinkage:
910 // FIXME: appending linkage variables should go into a section of
911 // their name or something. For now, just emit them as external.
912 case GlobalValue::ExternalLinkage:
913 O << "\t.globl " << name << "\n";
915 case GlobalValue::InternalLinkage: {
916 if (I->isConstant()) {
917 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
918 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
919 SwitchToDataSection(TAI->getCStringSection(), I);
923 // FIXME: special handling for ".ctors" & ".dtors" sections
924 if (I->hasSection() &&
925 (I->getSection() == ".ctors" ||
926 I->getSection() == ".dtors")) {
927 assert(!Subtarget->isTargetDarwin());
928 std::string SectionName = ".section " + I->getSection();
929 SectionName += ",\"aw\",%progbits";
930 SwitchToDataSection(SectionName.c_str());
931 } else if (I->hasSection() && Subtarget->isTargetDarwin()) {
932 // Honor all section names on Darwin; ObjC uses this
933 std::string SectionName = ".section " + I->getSection();
934 SwitchToDataSection(SectionName.c_str());
936 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
937 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
938 TAI->getBSSSection(), I);
939 else if (!I->isConstant())
940 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
941 TAI->getDataSection(), I);
942 else if (I->isThreadLocal())
943 SwitchToDataSection(TAI->getTLSDataSection());
946 bool HasReloc = C->ContainsRelocations();
948 Subtarget->isTargetDarwin() &&
949 TM.getRelocationModel() != Reloc::Static)
950 SwitchToDataSection("\t.const_data\n");
951 else if (!HasReloc && Size == 4 &&
952 TAI->getFourByteConstantSection())
953 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
954 else if (!HasReloc && Size == 8 &&
955 TAI->getEightByteConstantSection())
956 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
957 else if (!HasReloc && Size == 16 &&
958 TAI->getSixteenByteConstantSection())
959 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
960 else if (TAI->getReadOnlySection())
961 SwitchToDataSection(TAI->getReadOnlySection(), I);
963 SwitchToDataSection(TAI->getDataSection(), I);
970 assert(0 && "Unknown linkage type!");
974 EmitAlignment(Align, I);
975 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
976 PrintUnmangledNameSafely(I, O);
978 if (TAI->hasDotTypeDotSizeDirective())
979 O << "\t.size " << name << ", " << Size << "\n";
980 // If the initializer is a extern weak symbol, remember to emit the weak
982 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
983 if (GV->hasExternalWeakLinkage())
984 ExtWeakSymbols.insert(GV);
986 EmitGlobalConstant(C);
990 if (Subtarget->isTargetDarwin()) {
991 SwitchToDataSection("");
993 // Output stubs for dynamically-linked functions
995 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
997 if (TM.getRelocationModel() == Reloc::PIC_)
998 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
1001 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
1005 O << "\t.code\t32\n";
1007 O << "L" << *i << "$stub:\n";
1008 O << "\t.indirect_symbol " << *i << "\n";
1009 O << "\tldr ip, L" << *i << "$slp\n";
1010 if (TM.getRelocationModel() == Reloc::PIC_) {
1011 O << "L" << *i << "$scv:\n";
1012 O << "\tadd ip, pc, ip\n";
1014 O << "\tldr pc, [ip, #0]\n";
1015 O << "L" << *i << "$slp:\n";
1016 if (TM.getRelocationModel() == Reloc::PIC_)
1017 O << "\t.long\tL" << *i << "$lazy_ptr-(L" << *i << "$scv+8)\n";
1019 O << "\t.long\tL" << *i << "$lazy_ptr\n";
1020 SwitchToDataSection(".lazy_symbol_pointer", 0);
1021 O << "L" << *i << "$lazy_ptr:\n";
1022 O << "\t.indirect_symbol " << *i << "\n";
1023 O << "\t.long\tdyld_stub_binding_helper\n";
1027 // Output non-lazy-pointers for external and common global variables.
1028 if (!GVNonLazyPtrs.empty())
1029 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
1030 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
1031 e = GVNonLazyPtrs.end(); i != e; ++i) {
1032 O << "L" << *i << "$non_lazy_ptr:\n";
1033 O << "\t.indirect_symbol " << *i << "\n";
1034 O << "\t.long\t0\n";
1037 // Emit initial debug information.
1040 // Funny Darwin hack: This flag tells the linker that no global symbols
1041 // contain code that falls through to other global symbols (e.g. the obvious
1042 // implementation of multiple entry points). If this doesn't occur, the
1043 // linker can safely perform dead code stripping. Since LLVM never
1044 // generates code that does this, it is always safe to set.
1045 O << "\t.subsections_via_symbols\n";
1047 // Emit final debug information for ELF.
1051 return AsmPrinter::doFinalization(M);