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/SmallPtrSet.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/ADT/StringExtras.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/Mangler.h"
37 #include "llvm/Support/MathExtras.h"
41 STATISTIC(EmittedInsts, "Number of machine instrs printed");
44 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
45 ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
46 : AsmPrinter(O, TM, T), DW(O, this, T), MMI(NULL), AFI(NULL),
48 Subtarget = &TM.getSubtarget<ARMSubtarget>();
52 MachineModuleInfo *MMI;
54 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
55 /// make the right decision when printing asm code for different targets.
56 const ARMSubtarget *Subtarget;
58 /// AFI - Keep a pointer to ARMFunctionInfo for the current
62 /// We name each basic block in a Function with a unique number, so
63 /// that we can consistently refer to them later. This is cleared
64 /// at the beginning of each call to runOnMachineFunction().
66 typedef std::map<const Value *, unsigned> ValueMapTy;
67 ValueMapTy NumberForBB;
69 /// GVNonLazyPtrs - Keeps the set of GlobalValues that require
70 /// non-lazy-pointers for indirect access.
71 std::set<std::string> GVNonLazyPtrs;
73 /// FnStubs - Keeps the set of external function GlobalAddresses that the
74 /// asm printer should generate stubs for.
75 std::set<std::string> FnStubs;
77 /// PCRelGVs - Keeps the set of GlobalValues used in pc relative
79 SmallPtrSet<const GlobalValue*, 8> PCRelGVs;
81 /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
84 virtual const char *getPassName() const {
85 return "ARM Assembly Printer";
88 void printOperand(const MachineInstr *MI, int opNum,
89 const char *Modifier = 0);
90 void printSOImmOperand(const MachineInstr *MI, int opNum);
91 void printSOImm2PartOperand(const MachineInstr *MI, int opNum);
92 void printSORegOperand(const MachineInstr *MI, int opNum);
93 void printAddrMode2Operand(const MachineInstr *MI, int OpNo);
94 void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNo);
95 void printAddrMode3Operand(const MachineInstr *MI, int OpNo);
96 void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNo);
97 void printAddrMode4Operand(const MachineInstr *MI, int OpNo,
98 const char *Modifier = 0);
99 void printAddrMode5Operand(const MachineInstr *MI, int OpNo,
100 const char *Modifier = 0);
101 void printAddrModePCOperand(const MachineInstr *MI, int OpNo,
102 const char *Modifier = 0);
103 void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNo);
104 void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNo,
106 void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNo);
107 void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNo);
108 void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNo);
109 void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNo);
110 void printPredicateOperand(const MachineInstr *MI, int opNum);
111 void printSBitModifierOperand(const MachineInstr *MI, int opNum);
112 void printPCLabel(const MachineInstr *MI, int opNum);
113 void printRegisterList(const MachineInstr *MI, int opNum);
114 void printCPInstOperand(const MachineInstr *MI, int opNum,
115 const char *Modifier);
116 void printJTBlockOperand(const MachineInstr *MI, int opNum);
118 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
119 unsigned AsmVariant, const char *ExtraCode);
121 void printModuleLevelGV(const GlobalVariable* GVar);
122 bool printInstruction(const MachineInstr *MI); // autogenerated.
123 void printMachineInstruction(const MachineInstr *MI);
124 bool runOnMachineFunction(MachineFunction &F);
125 bool doInitialization(Module &M);
126 bool doFinalization(Module &M);
128 /// getSectionForFunction - Return the section that we should emit the
129 /// specified function body into.
130 virtual std::string getSectionForFunction(const Function &F) const;
132 /// EmitMachineConstantPoolValue - Print a machine constantpool value to
134 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
135 printDataDirective(MCPV->getType());
137 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
138 GlobalValue *GV = ACPV->getGV();
139 std::string Name = GV ? Mang->getValueName(GV) : TAI->getGlobalPrefix();
141 Name += ACPV->getSymbol();
142 if (ACPV->isNonLazyPointer()) {
143 GVNonLazyPtrs.insert(Name);
144 printSuffixedName(Name, "$non_lazy_ptr");
145 } else if (ACPV->isStub()) {
146 FnStubs.insert(Name);
147 printSuffixedName(Name, "$stub");
150 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
151 if (ACPV->getPCAdjustment() != 0) {
152 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
153 << utostr(ACPV->getLabelId())
154 << "+" << (unsigned)ACPV->getPCAdjustment();
155 if (ACPV->mustAddCurrentAddress())
161 // If the constant pool value is a extern weak symbol, remember to emit
162 // the weak reference.
163 if (GV && GV->hasExternalWeakLinkage())
164 ExtWeakSymbols.insert(GV);
167 void getAnalysisUsage(AnalysisUsage &AU) const {
168 AsmPrinter::getAnalysisUsage(AU);
169 AU.setPreservesAll();
170 AU.addRequired<MachineModuleInfo>();
173 } // end of anonymous namespace
175 #include "ARMGenAsmWriter.inc"
177 /// createARMCodePrinterPass - Returns a pass that prints the ARM
178 /// assembly code for a MachineFunction to the given output stream,
179 /// using the given target machine description. This should work
180 /// regardless of whether the function is in SSA form.
182 FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
183 ARMTargetMachine &tm) {
184 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
187 // Substitute old hook with new one temporary
188 std::string ARMAsmPrinter::getSectionForFunction(const Function &F) const {
189 return TAI->SectionForGlobal(&F);
192 /// runOnMachineFunction - This uses the printInstruction()
193 /// method to print assembly for each instruction.
195 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
196 AFI = MF.getInfo<ARMFunctionInfo>();
198 SetupMachineFunction(MF);
201 // NOTE: we don't print out constant pools here, they are handled as
205 // Print out labels for the function.
206 const Function *F = MF.getFunction();
207 switch (F->getLinkage()) {
208 default: assert(0 && "Unknown linkage type!");
209 case Function::InternalLinkage:
210 SwitchToTextSection("\t.text", F);
212 case Function::ExternalLinkage:
213 SwitchToTextSection("\t.text", F);
214 O << "\t.globl\t" << CurrentFnName << "\n";
216 case Function::WeakLinkage:
217 case Function::LinkOnceLinkage:
218 if (Subtarget->isTargetDarwin()) {
220 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
221 O << "\t.globl\t" << CurrentFnName << "\n";
222 O << "\t.weak_definition\t" << CurrentFnName << "\n";
224 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
229 printVisibility(CurrentFnName, F->getVisibility());
231 if (AFI->isThumbFunction()) {
232 EmitAlignment(1, F, AFI->getAlign());
233 O << "\t.code\t16\n";
234 O << "\t.thumb_func";
235 if (Subtarget->isTargetDarwin())
236 O << "\t" << CurrentFnName;
242 O << CurrentFnName << ":\n";
243 // Emit pre-function debug information.
244 DW.BeginFunction(&MF);
246 if (Subtarget->isTargetDarwin()) {
247 // If the function is empty, then we need to emit *something*. Otherwise,
248 // the function's label might be associated with something that it wasn't
249 // meant to be associated with. We emit a noop in this situation.
250 MachineFunction::iterator I = MF.begin();
252 if (++I == MF.end() && MF.front().empty())
256 // Print out code for the function.
257 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
259 // Print a label for the basic block.
260 if (I != MF.begin()) {
261 printBasicBlockLabel(I, true, true);
264 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
266 // Print the assembly for the instruction.
267 printMachineInstruction(II);
271 if (TAI->hasDotTypeDotSizeDirective())
272 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
274 // Emit post-function debug information.
280 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
281 const char *Modifier) {
282 const MachineOperand &MO = MI->getOperand(opNum);
283 switch (MO.getType()) {
284 case MachineOperand::MO_Register:
285 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
286 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
288 assert(0 && "not implemented");
290 case MachineOperand::MO_Immediate: {
291 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
294 O << (int)MO.getImm();
297 case MachineOperand::MO_MachineBasicBlock:
298 printBasicBlockLabel(MO.getMBB());
300 case MachineOperand::MO_GlobalAddress: {
301 bool isCallOp = Modifier && !strcmp(Modifier, "call");
302 GlobalValue *GV = MO.getGlobal();
303 std::string Name = Mang->getValueName(GV);
304 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
305 GV->hasLinkOnceLinkage());
306 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
307 TM.getRelocationModel() != Reloc::Static) {
308 printSuffixedName(Name, "$stub");
309 FnStubs.insert(Name);
313 if (MO.getOffset() > 0)
314 O << '+' << MO.getOffset();
315 else if (MO.getOffset() < 0)
318 if (isCallOp && Subtarget->isTargetELF() &&
319 TM.getRelocationModel() == Reloc::PIC_)
321 if (GV->hasExternalWeakLinkage())
322 ExtWeakSymbols.insert(GV);
325 case MachineOperand::MO_ExternalSymbol: {
326 bool isCallOp = Modifier && !strcmp(Modifier, "call");
327 std::string Name(TAI->getGlobalPrefix());
328 Name += MO.getSymbolName();
329 if (isCallOp && Subtarget->isTargetDarwin() &&
330 TM.getRelocationModel() != Reloc::Static) {
331 printSuffixedName(Name, "$stub");
332 FnStubs.insert(Name);
335 if (isCallOp && Subtarget->isTargetELF() &&
336 TM.getRelocationModel() == Reloc::PIC_)
340 case MachineOperand::MO_ConstantPoolIndex:
341 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
342 << '_' << MO.getIndex();
344 case MachineOperand::MO_JumpTableIndex:
345 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
346 << '_' << MO.getIndex();
349 O << "<unknown operand type>"; abort (); break;
353 static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
354 assert(V < (1 << 12) && "Not a valid so_imm value!");
355 unsigned Imm = ARM_AM::getSOImmValImm(V);
356 unsigned Rot = ARM_AM::getSOImmValRot(V);
358 // Print low-level immediate formation info, per
359 // A5.1.3: "Data-processing operands - Immediate".
361 O << "#" << Imm << ", " << Rot;
362 // Pretty printed version.
363 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
369 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
370 /// immediate in bits 0-7.
371 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
372 const MachineOperand &MO = MI->getOperand(OpNum);
373 assert(MO.isImmediate() && "Not a valid so_imm value!");
374 printSOImm(O, MO.getImm(), TAI);
377 /// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
378 /// followed by a or to materialize.
379 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
380 const MachineOperand &MO = MI->getOperand(OpNum);
381 assert(MO.isImmediate() && "Not a valid so_imm value!");
382 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
383 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
384 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
386 printPredicateOperand(MI, 2);
392 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
395 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
396 // "Addressing Mode 1 - Data-processing operands" forms. This includes:
398 // REG REG 0,SH_OPC - e.g. R5, ROR R3
399 // REG 0 IMM,SH_OPC - e.g. R5, LSL #3
400 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
401 const MachineOperand &MO1 = MI->getOperand(Op);
402 const MachineOperand &MO2 = MI->getOperand(Op+1);
403 const MachineOperand &MO3 = MI->getOperand(Op+2);
405 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
406 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
408 // Print the shift opc.
410 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
414 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
415 O << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
416 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
418 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
422 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
423 const MachineOperand &MO1 = MI->getOperand(Op);
424 const MachineOperand &MO2 = MI->getOperand(Op+1);
425 const MachineOperand &MO3 = MI->getOperand(Op+2);
427 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
428 printOperand(MI, Op);
432 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
435 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
437 << (char)ARM_AM::getAM2Op(MO3.getImm())
438 << ARM_AM::getAM2Offset(MO3.getImm());
444 << (char)ARM_AM::getAM2Op(MO3.getImm())
445 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
447 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
449 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
454 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
455 const MachineOperand &MO1 = MI->getOperand(Op);
456 const MachineOperand &MO2 = MI->getOperand(Op+1);
459 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
460 assert(ImmOffs && "Malformed indexed load / store!");
462 << (char)ARM_AM::getAM2Op(MO2.getImm())
467 O << (char)ARM_AM::getAM2Op(MO2.getImm())
468 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
470 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
472 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
476 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
477 const MachineOperand &MO1 = MI->getOperand(Op);
478 const MachineOperand &MO2 = MI->getOperand(Op+1);
479 const MachineOperand &MO3 = MI->getOperand(Op+2);
481 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
482 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
486 << (char)ARM_AM::getAM3Op(MO3.getImm())
487 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName
492 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
494 << (char)ARM_AM::getAM3Op(MO3.getImm())
499 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
500 const MachineOperand &MO1 = MI->getOperand(Op);
501 const MachineOperand &MO2 = MI->getOperand(Op+1);
504 O << (char)ARM_AM::getAM3Op(MO2.getImm())
505 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
509 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
510 assert(ImmOffs && "Malformed indexed load / store!");
512 << (char)ARM_AM::getAM3Op(MO2.getImm())
516 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
517 const char *Modifier) {
518 const MachineOperand &MO1 = MI->getOperand(Op);
519 const MachineOperand &MO2 = MI->getOperand(Op+1);
520 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
521 if (Modifier && strcmp(Modifier, "submode") == 0) {
522 if (MO1.getReg() == ARM::SP) {
523 bool isLDM = (MI->getOpcode() == ARM::LDM ||
524 MI->getOpcode() == ARM::LDM_RET);
525 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
527 O << ARM_AM::getAMSubModeStr(Mode);
529 printOperand(MI, Op);
530 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
535 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
536 const char *Modifier) {
537 const MachineOperand &MO1 = MI->getOperand(Op);
538 const MachineOperand &MO2 = MI->getOperand(Op+1);
540 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
541 printOperand(MI, Op);
545 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
547 if (Modifier && strcmp(Modifier, "submode") == 0) {
548 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
549 if (MO1.getReg() == ARM::SP) {
550 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
551 MI->getOpcode() == ARM::FLDMS);
552 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
554 O << ARM_AM::getAMSubModeStr(Mode);
556 } else if (Modifier && strcmp(Modifier, "base") == 0) {
557 // Used for FSTM{D|S} and LSTM{D|S} operations.
558 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
559 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
564 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
566 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
568 << (char)ARM_AM::getAM5Op(MO2.getImm())
574 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
575 const char *Modifier) {
576 if (Modifier && strcmp(Modifier, "label") == 0) {
577 printPCLabel(MI, Op+1);
581 const MachineOperand &MO1 = MI->getOperand(Op);
582 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
583 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName << "]";
587 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
588 const MachineOperand &MO1 = MI->getOperand(Op);
589 const MachineOperand &MO2 = MI->getOperand(Op+1);
590 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
591 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).AsmName << "]";
595 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
597 const MachineOperand &MO1 = MI->getOperand(Op);
598 const MachineOperand &MO2 = MI->getOperand(Op+1);
599 const MachineOperand &MO3 = MI->getOperand(Op+2);
601 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
602 printOperand(MI, Op);
606 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
608 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).AsmName;
609 else if (unsigned ImmOffs = MO2.getImm()) {
610 O << ", #" << ImmOffs;
618 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
619 printThumbAddrModeRI5Operand(MI, Op, 1);
622 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
623 printThumbAddrModeRI5Operand(MI, Op, 2);
626 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
627 printThumbAddrModeRI5Operand(MI, Op, 4);
630 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
631 const MachineOperand &MO1 = MI->getOperand(Op);
632 const MachineOperand &MO2 = MI->getOperand(Op+1);
633 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
634 if (unsigned ImmOffs = MO2.getImm())
635 O << ", #" << ImmOffs << " * 4";
639 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int opNum) {
640 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(opNum).getImm();
642 O << ARMCondCodeToString(CC);
645 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int opNum){
646 unsigned Reg = MI->getOperand(opNum).getReg();
648 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
653 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
654 int Id = (int)MI->getOperand(opNum).getImm();
655 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
658 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
660 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
662 if (i != e-1) O << ", ";
667 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
668 const char *Modifier) {
669 assert(Modifier && "This operand only works with a modifier!");
670 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
672 if (!strcmp(Modifier, "label")) {
673 unsigned ID = MI->getOperand(OpNo).getImm();
674 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
675 << '_' << ID << ":\n";
677 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
678 unsigned CPI = MI->getOperand(OpNo).getIndex();
680 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
681 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
683 if (MCPE.isMachineConstantPoolEntry()) {
684 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
685 ARMConstantPoolValue *ACPV =
686 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
687 if (ACPV->getPCAdjustment() != 0) {
688 const GlobalValue *GV = ACPV->getGV();
692 EmitGlobalConstant(MCPE.Val.ConstVal);
693 // remember to emit the weak reference
694 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
695 if (GV->hasExternalWeakLinkage())
696 ExtWeakSymbols.insert(GV);
701 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
702 const MachineOperand &MO1 = MI->getOperand(OpNo);
703 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
704 unsigned JTI = MO1.getIndex();
705 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
706 << '_' << JTI << '_' << MO2.getImm() << ":\n";
708 const char *JTEntryDirective = TAI->getJumpTableDirective();
709 if (!JTEntryDirective)
710 JTEntryDirective = TAI->getData32bitsDirective();
712 const MachineFunction *MF = MI->getParent()->getParent();
713 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
714 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
715 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
716 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
717 std::set<MachineBasicBlock*> JTSets;
718 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
719 MachineBasicBlock *MBB = JTBBs[i];
720 if (UseSet && JTSets.insert(MBB).second)
721 printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
723 O << JTEntryDirective << ' ';
725 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
726 << '_' << JTI << '_' << MO2.getImm()
727 << "_set_" << MBB->getNumber();
728 else if (TM.getRelocationModel() == Reloc::PIC_) {
729 printBasicBlockLabel(MBB, false, false, false);
730 // If the arch uses custom Jump Table directives, don't calc relative to JT
731 if (!TAI->getJumpTableDirective())
732 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
733 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
735 printBasicBlockLabel(MBB, false, false, false);
742 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
743 unsigned AsmVariant, const char *ExtraCode){
744 // Does this asm operand have a single letter operand modifier?
745 if (ExtraCode && ExtraCode[0]) {
746 if (ExtraCode[1] != 0) return true; // Unknown modifier.
748 switch (ExtraCode[0]) {
749 default: return true; // Unknown modifier.
750 case 'c': // Don't print "$" before a global var name or constant.
751 case 'P': // Print a VFP double precision register.
752 printOperand(MI, OpNo);
755 if (TM.getTargetData()->isLittleEndian())
759 if (TM.getTargetData()->isBigEndian())
762 case 'H': // Write second word of DI / DF reference.
763 // Verify that this operand has two consecutive registers.
764 if (!MI->getOperand(OpNo).isRegister() ||
765 OpNo+1 == MI->getNumOperands() ||
766 !MI->getOperand(OpNo+1).isRegister())
768 ++OpNo; // Return the high-part.
772 printOperand(MI, OpNo);
776 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
779 int Opc = MI->getOpcode();
781 case ARM::CONSTPOOL_ENTRY:
782 if (!InCPMode && AFI->isThumbFunction()) {
788 if (InCPMode && AFI->isThumbFunction())
809 // Call the autogenerated instruction printer routines.
810 printInstruction(MI);
813 bool ARMAsmPrinter::doInitialization(Module &M) {
814 // Emit initial debug information.
817 bool Result = AsmPrinter::doInitialization(M);
819 // AsmPrinter::doInitialization should have done this analysis.
820 MMI = getAnalysisToUpdate<MachineModuleInfo>();
822 DW.SetModuleInfo(MMI);
824 // Darwin wants symbols to be quoted if they have complex names.
825 if (Subtarget->isTargetDarwin())
826 Mang->setUseQuotes(true);
831 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
832 /// Don't print things like \n or \0.
833 static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
834 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
840 void ARMAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
841 const TargetData *TD = TM.getTargetData();
843 if (!GVar->hasInitializer()) // External global require no code
846 // Check to see if this is a special global used by LLVM, if so, emit it.
848 if (EmitSpecialLLVMGlobal(GVar)) {
849 if (Subtarget->isTargetDarwin() &&
850 TM.getRelocationModel() == Reloc::Static) {
851 if (GVar->getName() == "llvm.global_ctors")
852 O << ".reference .constructors_used\n";
853 else if (GVar->getName() == "llvm.global_dtors")
854 O << ".reference .destructors_used\n";
859 std::string SectionName = TAI->SectionForGlobal(GVar);
860 std::string name = Mang->getValueName(GVar);
861 Constant *C = GVar->getInitializer();
862 const Type *Type = C->getType();
863 unsigned Size = TD->getABITypeSize(Type);
864 unsigned Align = TD->getPreferredAlignmentLog(GVar);
866 printVisibility(name, GVar->getVisibility());
868 if (Subtarget->isTargetELF())
869 O << "\t.type " << name << ",%object\n";
871 SwitchToDataSection(SectionName.c_str());
873 if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal()) {
874 // FIXME: This seems to be pretty darwin-specific
876 if (GVar->hasExternalLinkage()) {
877 if (const char *Directive = TAI->getZeroFillDirective()) {
878 O << "\t.globl\t" << name << "\n";
879 O << Directive << "__DATA, __common, " << name << ", "
880 << Size << ", " << Align << "\n";
885 if (GVar->hasInternalLinkage() || GVar->isWeakForLinker()) {
886 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
888 if (TAI->getLCOMMDirective() != NULL) {
889 if (PCRelGVs.count(GVar) || GVar->hasInternalLinkage()) {
890 O << TAI->getLCOMMDirective() << name << "," << Size;
891 if (Subtarget->isTargetDarwin())
894 O << TAI->getCOMMDirective() << name << "," << Size;
896 if (GVar->hasInternalLinkage())
897 O << "\t.local\t" << name << "\n";
898 O << TAI->getCOMMDirective() << name << "," << Size;
899 if (TAI->getCOMMDirectiveTakesAlignment())
900 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
902 O << "\t\t" << TAI->getCommentString() << " ";
903 PrintUnmangledNameSafely(GVar, O);
909 switch (GVar->getLinkage()) {
910 case GlobalValue::LinkOnceLinkage:
911 case GlobalValue::WeakLinkage:
912 if (Subtarget->isTargetDarwin()) {
913 O << "\t.globl " << name << "\n"
914 << "\t.weak_definition " << name << "\n";
916 O << "\t.weak " << name << "\n";
919 case GlobalValue::AppendingLinkage:
920 // FIXME: appending linkage variables should go into a section of
921 // their name or something. For now, just emit them as external.
922 case GlobalValue::ExternalLinkage:
923 O << "\t.globl " << name << "\n";
925 case GlobalValue::InternalLinkage:
928 assert(0 && "Unknown linkage type!");
932 EmitAlignment(Align, GVar);
933 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
934 PrintUnmangledNameSafely(GVar, O);
936 if (TAI->hasDotTypeDotSizeDirective())
937 O << "\t.size " << name << ", " << Size << "\n";
939 // If the initializer is a extern weak symbol, remember to emit the weak
941 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
942 if (GV->hasExternalWeakLinkage())
943 ExtWeakSymbols.insert(GV);
945 EmitGlobalConstant(C);
950 bool ARMAsmPrinter::doFinalization(Module &M) {
951 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
953 printModuleLevelGV(I);
955 if (Subtarget->isTargetDarwin()) {
956 SwitchToDataSection("");
958 // Output stubs for dynamically-linked functions
960 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
962 if (TM.getRelocationModel() == Reloc::PIC_)
963 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
966 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
970 O << "\t.code\t32\n";
973 printSuffixedName(p, "$stub");
975 O << "\t.indirect_symbol " << *i << "\n";
977 printSuffixedName(p, "$slp");
979 if (TM.getRelocationModel() == Reloc::PIC_) {
980 printSuffixedName(p, "$scv");
982 O << "\tadd ip, pc, ip\n";
984 O << "\tldr pc, [ip, #0]\n";
985 printSuffixedName(p, "$slp");
988 printSuffixedName(p, "$lazy_ptr");
989 if (TM.getRelocationModel() == Reloc::PIC_) {
991 printSuffixedName(p, "$scv");
995 SwitchToDataSection(".lazy_symbol_pointer", 0);
996 printSuffixedName(p, "$lazy_ptr");
998 O << "\t.indirect_symbol " << *i << "\n";
999 O << "\t.long\tdyld_stub_binding_helper\n";
1003 // Output non-lazy-pointers for external and common global variables.
1004 if (!GVNonLazyPtrs.empty())
1005 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
1006 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
1007 e = GVNonLazyPtrs.end(); i != e; ++i) {
1009 printSuffixedName(p, "$non_lazy_ptr");
1011 O << "\t.indirect_symbol " << *i << "\n";
1012 O << "\t.long\t0\n";
1015 // Emit initial debug information.
1018 // Funny Darwin hack: This flag tells the linker that no global symbols
1019 // contain code that falls through to other global symbols (e.g. the obvious
1020 // implementation of multiple entry points). If this doesn't occur, the
1021 // linker can safely perform dead code stripping. Since LLVM never
1022 // generates code that does this, it is always safe to set.
1023 O << "\t.subsections_via_symbols\n";
1025 // Emit final debug information for ELF.
1029 return AsmPrinter::doFinalization(M);