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"
38 #include "llvm/Support/raw_ostream.h"
42 STATISTIC(EmittedInsts, "Number of machine instrs printed");
45 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
46 ARMAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
47 : AsmPrinter(O, TM, T), DW(O, this, T), MMI(NULL), AFI(NULL), MCP(NULL),
49 Subtarget = &TM.getSubtarget<ARMSubtarget>();
53 MachineModuleInfo *MMI;
55 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
56 /// make the right decision when printing asm code for different targets.
57 const ARMSubtarget *Subtarget;
59 /// AFI - Keep a pointer to ARMFunctionInfo for the current
63 /// MCP - Keep a pointer to constantpool entries of the current
65 const MachineConstantPool *MCP;
67 /// We name each basic block in a Function with a unique number, so
68 /// that we can consistently refer to them later. This is cleared
69 /// at the beginning of each call to runOnMachineFunction().
71 typedef std::map<const Value *, unsigned> ValueMapTy;
72 ValueMapTy NumberForBB;
74 /// GVNonLazyPtrs - Keeps the set of GlobalValues that require
75 /// non-lazy-pointers for indirect access.
76 std::set<std::string> GVNonLazyPtrs;
78 /// FnStubs - Keeps the set of external function GlobalAddresses that the
79 /// asm printer should generate stubs for.
80 std::set<std::string> FnStubs;
82 /// PCRelGVs - Keeps the set of GlobalValues used in pc relative
84 SmallPtrSet<const GlobalValue*, 8> PCRelGVs;
86 /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
89 virtual const char *getPassName() const {
90 return "ARM Assembly Printer";
93 void printOperand(const MachineInstr *MI, int opNum,
94 const char *Modifier = 0);
95 void printSOImmOperand(const MachineInstr *MI, int opNum);
96 void printSOImm2PartOperand(const MachineInstr *MI, int opNum);
97 void printSORegOperand(const MachineInstr *MI, int opNum);
98 void printAddrMode2Operand(const MachineInstr *MI, int OpNo);
99 void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNo);
100 void printAddrMode3Operand(const MachineInstr *MI, int OpNo);
101 void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNo);
102 void printAddrMode4Operand(const MachineInstr *MI, int OpNo,
103 const char *Modifier = 0);
104 void printAddrMode5Operand(const MachineInstr *MI, int OpNo,
105 const char *Modifier = 0);
106 void printAddrModePCOperand(const MachineInstr *MI, int OpNo,
107 const char *Modifier = 0);
108 void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNo);
109 void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNo,
111 void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNo);
112 void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNo);
113 void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNo);
114 void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNo);
115 void printPredicateOperand(const MachineInstr *MI, int opNum);
116 void printSBitModifierOperand(const MachineInstr *MI, int opNum);
117 void printPCLabel(const MachineInstr *MI, int opNum);
118 void printRegisterList(const MachineInstr *MI, int opNum);
119 void printCPInstOperand(const MachineInstr *MI, int opNum,
120 const char *Modifier);
121 void printJTBlockOperand(const MachineInstr *MI, int opNum);
123 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
124 unsigned AsmVariant, const char *ExtraCode);
126 void printModuleLevelGV(const GlobalVariable* GVar);
127 bool printInstruction(const MachineInstr *MI); // autogenerated.
128 void printMachineInstruction(const MachineInstr *MI);
129 bool runOnMachineFunction(MachineFunction &F);
130 bool doInitialization(Module &M);
131 bool doFinalization(Module &M);
133 /// EmitMachineConstantPoolValue - Print a machine constantpool value to
135 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
136 printDataDirective(MCPV->getType());
138 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
139 GlobalValue *GV = ACPV->getGV();
140 std::string Name = GV ? Mang->getValueName(GV) : TAI->getGlobalPrefix();
142 Name += ACPV->getSymbol();
143 if (ACPV->isNonLazyPointer()) {
144 GVNonLazyPtrs.insert(Name);
145 printSuffixedName(Name, "$non_lazy_ptr");
146 } else if (ACPV->isStub()) {
147 FnStubs.insert(Name);
148 printSuffixedName(Name, "$stub");
151 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
152 if (ACPV->getPCAdjustment() != 0) {
153 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
154 << utostr(ACPV->getLabelId())
155 << "+" << (unsigned)ACPV->getPCAdjustment();
156 if (ACPV->mustAddCurrentAddress())
162 // If the constant pool value is a extern weak symbol, remember to emit
163 // the weak reference.
164 if (GV && GV->hasExternalWeakLinkage())
165 ExtWeakSymbols.insert(GV);
168 void getAnalysisUsage(AnalysisUsage &AU) const {
169 AsmPrinter::getAnalysisUsage(AU);
170 AU.setPreservesAll();
171 AU.addRequired<MachineModuleInfo>();
174 } // end of anonymous namespace
176 #include "ARMGenAsmWriter.inc"
178 /// runOnMachineFunction - This uses the printInstruction()
179 /// method to print assembly for each instruction.
181 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
182 AFI = MF.getInfo<ARMFunctionInfo>();
183 MCP = MF.getConstantPool();
185 SetupMachineFunction(MF);
188 // NOTE: we don't print out constant pools here, they are handled as
192 // Print out labels for the function.
193 const Function *F = MF.getFunction();
194 switch (F->getLinkage()) {
195 default: assert(0 && "Unknown linkage type!");
196 case Function::InternalLinkage:
197 SwitchToTextSection("\t.text", F);
199 case Function::ExternalLinkage:
200 SwitchToTextSection("\t.text", F);
201 O << "\t.globl\t" << CurrentFnName << "\n";
203 case Function::WeakLinkage:
204 case Function::LinkOnceLinkage:
205 if (Subtarget->isTargetDarwin()) {
207 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
208 O << "\t.globl\t" << CurrentFnName << "\n";
209 O << "\t.weak_definition\t" << CurrentFnName << "\n";
211 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
216 printVisibility(CurrentFnName, F->getVisibility());
218 if (AFI->isThumbFunction()) {
219 EmitAlignment(1, F, AFI->getAlign());
220 O << "\t.code\t16\n";
221 O << "\t.thumb_func";
222 if (Subtarget->isTargetDarwin())
223 O << "\t" << CurrentFnName;
229 O << CurrentFnName << ":\n";
230 // Emit pre-function debug information.
231 // FIXME: Dwarf support.
232 //DW.BeginFunction(&MF);
234 if (Subtarget->isTargetDarwin()) {
235 // If the function is empty, then we need to emit *something*. Otherwise,
236 // the function's label might be associated with something that it wasn't
237 // meant to be associated with. We emit a noop in this situation.
238 MachineFunction::iterator I = MF.begin();
240 if (++I == MF.end() && MF.front().empty())
244 // Print out code for the function.
245 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
247 // Print a label for the basic block.
248 if (I != MF.begin()) {
249 printBasicBlockLabel(I, true, true);
252 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
254 // Print the assembly for the instruction.
255 printMachineInstruction(II);
259 if (TAI->hasDotTypeDotSizeDirective())
260 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
262 // Emit post-function debug information.
263 // FIXME: Dwarf support.
271 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
272 const char *Modifier) {
273 const MachineOperand &MO = MI->getOperand(opNum);
274 switch (MO.getType()) {
275 case MachineOperand::MO_Register:
276 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
277 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
279 assert(0 && "not implemented");
281 case MachineOperand::MO_Immediate: {
282 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
285 O << (int)MO.getImm();
288 case MachineOperand::MO_MachineBasicBlock:
289 printBasicBlockLabel(MO.getMBB());
291 case MachineOperand::MO_GlobalAddress: {
292 bool isCallOp = Modifier && !strcmp(Modifier, "call");
293 GlobalValue *GV = MO.getGlobal();
294 std::string Name = Mang->getValueName(GV);
295 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
296 GV->hasLinkOnceLinkage());
297 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
298 TM.getRelocationModel() != Reloc::Static) {
299 printSuffixedName(Name, "$stub");
300 FnStubs.insert(Name);
304 printOffset(MO.getOffset());
306 if (isCallOp && Subtarget->isTargetELF() &&
307 TM.getRelocationModel() == Reloc::PIC_)
309 if (GV->hasExternalWeakLinkage())
310 ExtWeakSymbols.insert(GV);
313 case MachineOperand::MO_ExternalSymbol: {
314 bool isCallOp = Modifier && !strcmp(Modifier, "call");
315 std::string Name(TAI->getGlobalPrefix());
316 Name += MO.getSymbolName();
317 if (isCallOp && Subtarget->isTargetDarwin() &&
318 TM.getRelocationModel() != Reloc::Static) {
319 printSuffixedName(Name, "$stub");
320 FnStubs.insert(Name);
323 if (isCallOp && Subtarget->isTargetELF() &&
324 TM.getRelocationModel() == Reloc::PIC_)
328 case MachineOperand::MO_ConstantPoolIndex:
329 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
330 << '_' << MO.getIndex();
332 case MachineOperand::MO_JumpTableIndex:
333 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
334 << '_' << MO.getIndex();
337 O << "<unknown operand type>"; abort (); break;
341 static void printSOImm(raw_ostream &O, int64_t V, const TargetAsmInfo *TAI) {
342 assert(V < (1 << 12) && "Not a valid so_imm value!");
343 unsigned Imm = ARM_AM::getSOImmValImm(V);
344 unsigned Rot = ARM_AM::getSOImmValRot(V);
346 // Print low-level immediate formation info, per
347 // A5.1.3: "Data-processing operands - Immediate".
349 O << "#" << Imm << ", " << Rot;
350 // Pretty printed version.
351 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
357 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
358 /// immediate in bits 0-7.
359 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
360 const MachineOperand &MO = MI->getOperand(OpNum);
361 assert(MO.isImm() && "Not a valid so_imm value!");
362 printSOImm(O, MO.getImm(), TAI);
365 /// printSOImm2PartOperand - SOImm is broken into two pieces using a 'mov'
366 /// followed by an 'orr' to materialize.
367 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
368 const MachineOperand &MO = MI->getOperand(OpNum);
369 assert(MO.isImm() && "Not a valid so_imm value!");
370 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
371 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
372 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
374 printPredicateOperand(MI, 2);
380 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
383 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
384 // "Addressing Mode 1 - Data-processing operands" forms. This includes:
386 // REG REG 0,SH_OPC - e.g. R5, ROR R3
387 // REG 0 IMM,SH_OPC - e.g. R5, LSL #3
388 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
389 const MachineOperand &MO1 = MI->getOperand(Op);
390 const MachineOperand &MO2 = MI->getOperand(Op+1);
391 const MachineOperand &MO3 = MI->getOperand(Op+2);
393 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
394 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
396 // Print the shift opc.
398 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
402 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
403 O << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
404 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
406 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
410 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
411 const MachineOperand &MO1 = MI->getOperand(Op);
412 const MachineOperand &MO2 = MI->getOperand(Op+1);
413 const MachineOperand &MO3 = MI->getOperand(Op+2);
415 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
416 printOperand(MI, Op);
420 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
423 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
425 << (char)ARM_AM::getAM2Op(MO3.getImm())
426 << ARM_AM::getAM2Offset(MO3.getImm());
432 << (char)ARM_AM::getAM2Op(MO3.getImm())
433 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
435 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
437 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
442 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
443 const MachineOperand &MO1 = MI->getOperand(Op);
444 const MachineOperand &MO2 = MI->getOperand(Op+1);
447 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
448 assert(ImmOffs && "Malformed indexed load / store!");
450 << (char)ARM_AM::getAM2Op(MO2.getImm())
455 O << (char)ARM_AM::getAM2Op(MO2.getImm())
456 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
458 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
460 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
464 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
465 const MachineOperand &MO1 = MI->getOperand(Op);
466 const MachineOperand &MO2 = MI->getOperand(Op+1);
467 const MachineOperand &MO3 = MI->getOperand(Op+2);
469 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
470 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
474 << (char)ARM_AM::getAM3Op(MO3.getImm())
475 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName
480 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
482 << (char)ARM_AM::getAM3Op(MO3.getImm())
487 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
488 const MachineOperand &MO1 = MI->getOperand(Op);
489 const MachineOperand &MO2 = MI->getOperand(Op+1);
492 O << (char)ARM_AM::getAM3Op(MO2.getImm())
493 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
497 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
498 assert(ImmOffs && "Malformed indexed load / store!");
500 << (char)ARM_AM::getAM3Op(MO2.getImm())
504 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
505 const char *Modifier) {
506 const MachineOperand &MO1 = MI->getOperand(Op);
507 const MachineOperand &MO2 = MI->getOperand(Op+1);
508 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
509 if (Modifier && strcmp(Modifier, "submode") == 0) {
510 if (MO1.getReg() == ARM::SP) {
511 bool isLDM = (MI->getOpcode() == ARM::LDM ||
512 MI->getOpcode() == ARM::LDM_RET);
513 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
515 O << ARM_AM::getAMSubModeStr(Mode);
517 printOperand(MI, Op);
518 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
523 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
524 const char *Modifier) {
525 const MachineOperand &MO1 = MI->getOperand(Op);
526 const MachineOperand &MO2 = MI->getOperand(Op+1);
528 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
529 printOperand(MI, Op);
533 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
535 if (Modifier && strcmp(Modifier, "submode") == 0) {
536 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
537 if (MO1.getReg() == ARM::SP) {
538 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
539 MI->getOpcode() == ARM::FLDMS);
540 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
542 O << ARM_AM::getAMSubModeStr(Mode);
544 } else if (Modifier && strcmp(Modifier, "base") == 0) {
545 // Used for FSTM{D|S} and LSTM{D|S} operations.
546 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
547 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
552 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
554 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
556 << (char)ARM_AM::getAM5Op(MO2.getImm())
562 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
563 const char *Modifier) {
564 if (Modifier && strcmp(Modifier, "label") == 0) {
565 printPCLabel(MI, Op+1);
569 const MachineOperand &MO1 = MI->getOperand(Op);
570 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
571 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName << "]";
575 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
576 const MachineOperand &MO1 = MI->getOperand(Op);
577 const MachineOperand &MO2 = MI->getOperand(Op+1);
578 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
579 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).AsmName << "]";
583 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
585 const MachineOperand &MO1 = MI->getOperand(Op);
586 const MachineOperand &MO2 = MI->getOperand(Op+1);
587 const MachineOperand &MO3 = MI->getOperand(Op+2);
589 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
590 printOperand(MI, Op);
594 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
596 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).AsmName;
597 else if (unsigned ImmOffs = MO2.getImm()) {
598 O << ", #" << ImmOffs;
606 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
607 printThumbAddrModeRI5Operand(MI, Op, 1);
610 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
611 printThumbAddrModeRI5Operand(MI, Op, 2);
614 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
615 printThumbAddrModeRI5Operand(MI, Op, 4);
618 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
619 const MachineOperand &MO1 = MI->getOperand(Op);
620 const MachineOperand &MO2 = MI->getOperand(Op+1);
621 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
622 if (unsigned ImmOffs = MO2.getImm())
623 O << ", #" << ImmOffs << " * 4";
627 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int opNum) {
628 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(opNum).getImm();
630 O << ARMCondCodeToString(CC);
633 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int opNum){
634 unsigned Reg = MI->getOperand(opNum).getReg();
636 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
641 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
642 int Id = (int)MI->getOperand(opNum).getImm();
643 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
646 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
648 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
650 if (i != e-1) O << ", ";
655 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
656 const char *Modifier) {
657 assert(Modifier && "This operand only works with a modifier!");
658 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
660 if (!strcmp(Modifier, "label")) {
661 unsigned ID = MI->getOperand(OpNo).getImm();
662 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
663 << '_' << ID << ":\n";
665 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
666 unsigned CPI = MI->getOperand(OpNo).getIndex();
668 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
670 if (MCPE.isMachineConstantPoolEntry()) {
671 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
672 ARMConstantPoolValue *ACPV =
673 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
674 if (ACPV->getPCAdjustment() != 0) {
675 const GlobalValue *GV = ACPV->getGV();
679 EmitGlobalConstant(MCPE.Val.ConstVal);
680 // remember to emit the weak reference
681 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
682 if (GV->hasExternalWeakLinkage())
683 ExtWeakSymbols.insert(GV);
688 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
689 const MachineOperand &MO1 = MI->getOperand(OpNo);
690 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
691 unsigned JTI = MO1.getIndex();
692 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
693 << '_' << JTI << '_' << MO2.getImm() << ":\n";
695 const char *JTEntryDirective = TAI->getJumpTableDirective();
696 if (!JTEntryDirective)
697 JTEntryDirective = TAI->getData32bitsDirective();
699 const MachineFunction *MF = MI->getParent()->getParent();
700 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
701 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
702 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
703 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
704 std::set<MachineBasicBlock*> JTSets;
705 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
706 MachineBasicBlock *MBB = JTBBs[i];
707 if (UseSet && JTSets.insert(MBB).second)
708 printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
710 O << JTEntryDirective << ' ';
712 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
713 << '_' << JTI << '_' << MO2.getImm()
714 << "_set_" << MBB->getNumber();
715 else if (TM.getRelocationModel() == Reloc::PIC_) {
716 printBasicBlockLabel(MBB, false, false, false);
717 // If the arch uses custom Jump Table directives, don't calc relative to JT
718 if (!TAI->getJumpTableDirective())
719 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
720 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
722 printBasicBlockLabel(MBB, false, false, false);
729 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
730 unsigned AsmVariant, const char *ExtraCode){
731 // Does this asm operand have a single letter operand modifier?
732 if (ExtraCode && ExtraCode[0]) {
733 if (ExtraCode[1] != 0) return true; // Unknown modifier.
735 switch (ExtraCode[0]) {
736 default: return true; // Unknown modifier.
737 case 'c': // Don't print "$" before a global var name or constant.
738 case 'P': // Print a VFP double precision register.
739 printOperand(MI, OpNo);
742 if (TM.getTargetData()->isLittleEndian())
746 if (TM.getTargetData()->isBigEndian())
749 case 'H': // Write second word of DI / DF reference.
750 // Verify that this operand has two consecutive registers.
751 if (!MI->getOperand(OpNo).isReg() ||
752 OpNo+1 == MI->getNumOperands() ||
753 !MI->getOperand(OpNo+1).isReg())
755 ++OpNo; // Return the high-part.
759 printOperand(MI, OpNo);
763 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
766 int Opc = MI->getOpcode();
768 case ARM::CONSTPOOL_ENTRY:
769 if (!InCPMode && AFI->isThumbFunction()) {
775 if (InCPMode && AFI->isThumbFunction())
779 // Call the autogenerated instruction printer routines.
780 printInstruction(MI);
783 bool ARMAsmPrinter::doInitialization(Module &M) {
784 // Emit initial debug information.
785 // FIXME: Dwarf support.
786 //DW.BeginModule(&M);
788 bool Result = AsmPrinter::doInitialization(M);
790 // AsmPrinter::doInitialization should have done this analysis.
791 MMI = getAnalysisToUpdate<MachineModuleInfo>();
793 // FIXME: Dwarf support.
794 //DW.SetModuleInfo(MMI);
796 // Darwin wants symbols to be quoted if they have complex names.
797 if (Subtarget->isTargetDarwin())
798 Mang->setUseQuotes(true);
803 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
804 /// Don't print things like \n or \0.
805 static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
806 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
812 void ARMAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
813 const TargetData *TD = TM.getTargetData();
815 if (!GVar->hasInitializer()) // External global require no code
818 // Check to see if this is a special global used by LLVM, if so, emit it.
820 if (EmitSpecialLLVMGlobal(GVar)) {
821 if (Subtarget->isTargetDarwin() &&
822 TM.getRelocationModel() == Reloc::Static) {
823 if (GVar->getName() == "llvm.global_ctors")
824 O << ".reference .constructors_used\n";
825 else if (GVar->getName() == "llvm.global_dtors")
826 O << ".reference .destructors_used\n";
831 std::string name = Mang->getValueName(GVar);
832 Constant *C = GVar->getInitializer();
833 const Type *Type = C->getType();
834 unsigned Size = TD->getABITypeSize(Type);
835 unsigned Align = TD->getPreferredAlignmentLog(GVar);
837 printVisibility(name, GVar->getVisibility());
839 if (Subtarget->isTargetELF())
840 O << "\t.type " << name << ",%object\n";
842 SwitchToSection(TAI->SectionForGlobal(GVar));
844 if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal()) {
845 // FIXME: This seems to be pretty darwin-specific
847 if (GVar->hasExternalLinkage()) {
848 if (const char *Directive = TAI->getZeroFillDirective()) {
849 O << "\t.globl\t" << name << "\n";
850 O << Directive << "__DATA, __common, " << name << ", "
851 << Size << ", " << Align << "\n";
856 if (GVar->hasInternalLinkage() || GVar->mayBeOverridden()) {
857 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
859 if (TAI->getLCOMMDirective() != NULL) {
860 if (PCRelGVs.count(GVar) || GVar->hasInternalLinkage()) {
861 O << TAI->getLCOMMDirective() << name << "," << Size;
862 if (Subtarget->isTargetDarwin())
865 O << TAI->getCOMMDirective() << name << "," << Size;
867 if (GVar->hasInternalLinkage())
868 O << "\t.local\t" << name << "\n";
869 O << TAI->getCOMMDirective() << name << "," << Size;
870 if (TAI->getCOMMDirectiveTakesAlignment())
871 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
873 O << "\t\t" << TAI->getCommentString() << " ";
874 PrintUnmangledNameSafely(GVar, O);
880 switch (GVar->getLinkage()) {
881 case GlobalValue::LinkOnceLinkage:
882 case GlobalValue::WeakLinkage:
883 if (Subtarget->isTargetDarwin()) {
884 O << "\t.globl " << name << "\n"
885 << "\t.weak_definition " << name << "\n";
887 O << "\t.weak " << name << "\n";
890 case GlobalValue::AppendingLinkage:
891 // FIXME: appending linkage variables should go into a section of
892 // their name or something. For now, just emit them as external.
893 case GlobalValue::ExternalLinkage:
894 O << "\t.globl " << name << "\n";
896 case GlobalValue::InternalLinkage:
899 assert(0 && "Unknown linkage type!");
903 EmitAlignment(Align, GVar);
904 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
905 PrintUnmangledNameSafely(GVar, O);
907 if (TAI->hasDotTypeDotSizeDirective())
908 O << "\t.size " << name << ", " << Size << "\n";
910 // If the initializer is a extern weak symbol, remember to emit the weak
912 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
913 if (GV->hasExternalWeakLinkage())
914 ExtWeakSymbols.insert(GV);
916 EmitGlobalConstant(C);
921 bool ARMAsmPrinter::doFinalization(Module &M) {
922 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
924 printModuleLevelGV(I);
926 if (Subtarget->isTargetDarwin()) {
927 SwitchToDataSection("");
929 // Output stubs for dynamically-linked functions
931 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
933 if (TM.getRelocationModel() == Reloc::PIC_)
934 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
937 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
941 O << "\t.code\t32\n";
944 printSuffixedName(p, "$stub");
946 O << "\t.indirect_symbol " << *i << "\n";
948 printSuffixedName(p, "$slp");
950 if (TM.getRelocationModel() == Reloc::PIC_) {
951 printSuffixedName(p, "$scv");
953 O << "\tadd ip, pc, ip\n";
955 O << "\tldr pc, [ip, #0]\n";
956 printSuffixedName(p, "$slp");
959 printSuffixedName(p, "$lazy_ptr");
960 if (TM.getRelocationModel() == Reloc::PIC_) {
962 printSuffixedName(p, "$scv");
966 SwitchToDataSection(".lazy_symbol_pointer", 0);
967 printSuffixedName(p, "$lazy_ptr");
969 O << "\t.indirect_symbol " << *i << "\n";
970 O << "\t.long\tdyld_stub_binding_helper\n";
974 // Output non-lazy-pointers for external and common global variables.
975 if (!GVNonLazyPtrs.empty())
976 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
977 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
978 e = GVNonLazyPtrs.end(); i != e; ++i) {
980 printSuffixedName(p, "$non_lazy_ptr");
982 O << "\t.indirect_symbol " << *i << "\n";
986 // Emit initial debug information.
987 // FIXME: Dwarf support.
990 // Funny Darwin hack: This flag tells the linker that no global symbols
991 // contain code that falls through to other global symbols (e.g. the obvious
992 // implementation of multiple entry points). If this doesn't occur, the
993 // linker can safely perform dead code stripping. Since LLVM never
994 // generates code that does this, it is always safe to set.
995 O << "\t.subsections_via_symbols\n";
997 // Emit final debug information for ELF.
998 // FIXME: Dwarf support.
1002 return AsmPrinter::doFinalization(M);
1005 /// createARMCodePrinterPass - Returns a pass that prints the ARM
1006 /// assembly code for a MachineFunction to the given output stream,
1007 /// using the given target machine description. This should work
1008 /// regardless of whether the function is in SSA form.
1010 FunctionPass *llvm::createARMCodePrinterPass(raw_ostream &o,
1011 ARMTargetMachine &tm) {
1012 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
1016 static struct Register {
1018 ARMTargetMachine::registerAsmPrinter(createARMCodePrinterPass);