1 //===-- ARMAsmPrinter.cpp - ARM LLVM assembly writer ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the "Instituto Nokia de Tecnologia" and
6 // is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // This file contains a printer that converts from our internal representation
12 // of machine-dependent LLVM code to GAS-format ARM assembly language.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "asm-printer"
18 #include "ARMTargetMachine.h"
19 #include "ARMAddressingModes.h"
20 #include "ARMConstantPoolValue.h"
21 #include "ARMMachineFunctionInfo.h"
22 #include "llvm/Constants.h"
23 #include "llvm/Module.h"
24 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/CodeGen/DwarfWriter.h"
26 #include "llvm/CodeGen/MachineModuleInfo.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineJumpTableInfo.h"
29 #include "llvm/Target/TargetAsmInfo.h"
30 #include "llvm/Target/TargetData.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Target/TargetOptions.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), AFI(NULL), InCPMode(false) {
47 Subtarget = &TM.getSubtarget<ARMSubtarget>();
52 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
53 /// make the right decision when printing asm code for different targets.
54 const ARMSubtarget *Subtarget;
56 /// AFI - Keep a pointer to ARMFunctionInfo for the current
60 /// We name each basic block in a Function with a unique number, so
61 /// that we can consistently refer to them later. This is cleared
62 /// at the beginning of each call to runOnMachineFunction().
64 typedef std::map<const Value *, unsigned> ValueMapTy;
65 ValueMapTy NumberForBB;
67 /// Keeps the set of GlobalValues that require non-lazy-pointers for
69 std::set<std::string> GVNonLazyPtrs;
71 /// Keeps the set of external function GlobalAddresses that the asm
72 /// printer should generate stubs for.
73 std::set<std::string> FnStubs;
75 /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
78 virtual const char *getPassName() const {
79 return "ARM Assembly Printer";
82 void printOperand(const MachineInstr *MI, int opNum,
83 const char *Modifier = 0);
84 void printSOImmOperand(const MachineInstr *MI, int opNum);
85 void printSOImm2PartOperand(const MachineInstr *MI, int opNum);
86 void printSORegOperand(const MachineInstr *MI, int opNum);
87 void printAddrMode2Operand(const MachineInstr *MI, int OpNo);
88 void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNo);
89 void printAddrMode3Operand(const MachineInstr *MI, int OpNo);
90 void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNo);
91 void printAddrMode4Operand(const MachineInstr *MI, int OpNo,
92 const char *Modifier = 0);
93 void printAddrMode5Operand(const MachineInstr *MI, int OpNo,
94 const char *Modifier = 0);
95 void printAddrModePCOperand(const MachineInstr *MI, int OpNo,
96 const char *Modifier = 0);
97 void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNo);
98 void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNo,
100 void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNo);
101 void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNo);
102 void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNo);
103 void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNo);
104 void printCCOperand(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 AU.setPreservesAll();
155 AU.addRequired<MachineModuleInfo>();
158 } // end of anonymous namespace
160 #include "ARMGenAsmWriter.inc"
162 /// createARMCodePrinterPass - Returns a pass that prints the ARM
163 /// assembly code for a MachineFunction to the given output stream,
164 /// using the given target machine description. This should work
165 /// regardless of whether the function is in SSA form.
167 FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
168 ARMTargetMachine &tm) {
169 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
172 /// runOnMachineFunction - This uses the printInstruction()
173 /// method to print assembly for each instruction.
175 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
176 AFI = MF.getInfo<ARMFunctionInfo>();
178 if (Subtarget->isTargetDarwin()) {
179 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
182 SetupMachineFunction(MF);
185 // NOTE: we don't print out constant pools here, they are handled as
189 // Print out labels for the function.
190 const Function *F = MF.getFunction();
191 switch (F->getLinkage()) {
192 default: assert(0 && "Unknown linkage type!");
193 case Function::InternalLinkage:
194 SwitchToTextSection("\t.text", F);
196 case Function::ExternalLinkage:
197 SwitchToTextSection("\t.text", F);
198 O << "\t.globl\t" << CurrentFnName << "\n";
200 case Function::WeakLinkage:
201 case Function::LinkOnceLinkage:
202 if (Subtarget->isTargetDarwin()) {
204 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
205 O << "\t.globl\t" << CurrentFnName << "\n";
206 O << "\t.weak_definition\t" << CurrentFnName << "\n";
208 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
213 const char *VisibilityDirective = NULL;
214 if (F->hasHiddenVisibility())
215 VisibilityDirective = TAI->getHiddenDirective();
216 else if (F->hasProtectedVisibility())
217 VisibilityDirective = TAI->getProtectedDirective();
219 if (VisibilityDirective)
220 O << VisibilityDirective << CurrentFnName << "\n";
222 if (AFI->isThumbFunction()) {
223 EmitAlignment(AFI->getAlign(), F);
224 O << "\t.code\t16\n";
225 O << "\t.thumb_func";
226 if (Subtarget->isTargetDarwin())
227 O << "\t" << CurrentFnName;
233 O << CurrentFnName << ":\n";
234 if (Subtarget->isTargetDarwin()) {
235 // Emit pre-function debug information.
236 DW.BeginFunction(&MF);
239 // Print out code for the function.
240 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
242 // Print a label for the basic block.
243 if (I != MF.begin()) {
244 printBasicBlockLabel(I, true);
247 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
249 // Print the assembly for the instruction.
250 printMachineInstruction(II);
254 if (TAI->hasDotTypeDotSizeDirective())
255 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
257 if (Subtarget->isTargetDarwin()) {
258 // Emit post-function debug information.
265 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
266 const char *Modifier) {
267 const MachineOperand &MO = MI->getOperand(opNum);
268 switch (MO.getType()) {
269 case MachineOperand::MO_Register:
270 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
271 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
273 assert(0 && "not implemented");
275 case MachineOperand::MO_Immediate: {
276 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
279 O << (int)MO.getImmedValue();
282 case MachineOperand::MO_MachineBasicBlock:
283 printBasicBlockLabel(MO.getMachineBasicBlock());
285 case MachineOperand::MO_GlobalAddress: {
286 bool isCallOp = Modifier && !strcmp(Modifier, "call");
287 GlobalValue *GV = MO.getGlobal();
288 std::string Name = Mang->getValueName(GV);
289 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
290 GV->hasLinkOnceLinkage());
291 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
292 TM.getRelocationModel() != Reloc::Static) {
293 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
294 FnStubs.insert(Name);
297 if (isCallOp && Subtarget->isTargetELF() &&
298 TM.getRelocationModel() == Reloc::PIC_)
300 if (GV->hasExternalWeakLinkage())
301 ExtWeakSymbols.insert(GV);
304 case MachineOperand::MO_ExternalSymbol: {
305 bool isCallOp = Modifier && !strcmp(Modifier, "call");
306 std::string Name(TAI->getGlobalPrefix());
307 Name += MO.getSymbolName();
308 if (isCallOp && Subtarget->isTargetDarwin() &&
309 TM.getRelocationModel() != Reloc::Static) {
310 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
311 FnStubs.insert(Name);
314 if (isCallOp && Subtarget->isTargetELF() &&
315 TM.getRelocationModel() == Reloc::PIC_)
319 case MachineOperand::MO_ConstantPoolIndex:
320 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
321 << '_' << MO.getConstantPoolIndex();
323 case MachineOperand::MO_JumpTableIndex:
324 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
325 << '_' << MO.getJumpTableIndex();
328 O << "<unknown operand type>"; abort (); break;
332 static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
333 assert(V < (1 << 12) && "Not a valid so_imm value!");
334 unsigned Imm = ARM_AM::getSOImmValImm(V);
335 unsigned Rot = ARM_AM::getSOImmValRot(V);
337 // Print low-level immediate formation info, per
338 // A5.1.3: "Data-processing operands - Immediate".
340 O << "#" << Imm << ", " << Rot;
341 // Pretty printed version.
342 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
348 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
349 /// immediate in bits 0-7.
350 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
351 const MachineOperand &MO = MI->getOperand(OpNum);
352 assert(MO.isImmediate() && "Not a valid so_imm value!");
353 printSOImm(O, MO.getImmedValue(), TAI);
356 /// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
357 /// followed by a or to materialize.
358 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
359 const MachineOperand &MO = MI->getOperand(OpNum);
360 assert(MO.isImmediate() && "Not a valid so_imm value!");
361 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImmedValue());
362 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImmedValue());
363 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
369 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
372 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
373 // "Addressing Mode 1 - Data-processing operands" forms. This includes:
375 // REG REG 0,SH_OPC - e.g. R5, ROR R3
376 // REG 0 IMM,SH_OPC - e.g. R5, LSL #3
377 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
378 const MachineOperand &MO1 = MI->getOperand(Op);
379 const MachineOperand &MO2 = MI->getOperand(Op+1);
380 const MachineOperand &MO3 = MI->getOperand(Op+2);
382 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
383 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
385 // Print the shift opc.
387 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImmedValue()))
391 assert(MRegisterInfo::isPhysicalRegister(MO2.getReg()));
392 O << TM.getRegisterInfo()->get(MO2.getReg()).Name;
393 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
395 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
399 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
400 const MachineOperand &MO1 = MI->getOperand(Op);
401 const MachineOperand &MO2 = MI->getOperand(Op+1);
402 const MachineOperand &MO3 = MI->getOperand(Op+2);
404 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
405 printOperand(MI, Op);
409 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
412 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
414 << (char)ARM_AM::getAM2Op(MO3.getImm())
415 << ARM_AM::getAM2Offset(MO3.getImm());
421 << (char)ARM_AM::getAM2Op(MO3.getImm())
422 << TM.getRegisterInfo()->get(MO2.getReg()).Name;
424 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
426 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImmedValue()))
431 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
432 const MachineOperand &MO1 = MI->getOperand(Op);
433 const MachineOperand &MO2 = MI->getOperand(Op+1);
436 if (ARM_AM::getAM2Offset(MO2.getImm())) // Don't print +0.
438 << (char)ARM_AM::getAM2Op(MO2.getImm())
439 << ARM_AM::getAM2Offset(MO2.getImm());
443 O << (char)ARM_AM::getAM2Op(MO2.getImm())
444 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
446 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
448 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImmedValue()))
452 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
453 const MachineOperand &MO1 = MI->getOperand(Op);
454 const MachineOperand &MO2 = MI->getOperand(Op+1);
455 const MachineOperand &MO3 = MI->getOperand(Op+2);
457 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
458 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
462 << (char)ARM_AM::getAM3Op(MO3.getImm())
463 << TM.getRegisterInfo()->get(MO2.getReg()).Name
468 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
470 << (char)ARM_AM::getAM3Op(MO3.getImm())
475 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
476 const MachineOperand &MO1 = MI->getOperand(Op);
477 const MachineOperand &MO2 = MI->getOperand(Op+1);
480 O << (char)ARM_AM::getAM3Op(MO2.getImm())
481 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
485 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
487 << (char)ARM_AM::getAM3Op(MO2.getImm())
491 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
492 const char *Modifier) {
493 const MachineOperand &MO1 = MI->getOperand(Op);
494 const MachineOperand &MO2 = MI->getOperand(Op+1);
495 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
496 if (Modifier && strcmp(Modifier, "submode") == 0) {
497 if (MO1.getReg() == ARM::SP) {
498 bool isLDM = (MI->getOpcode() == ARM::LDM ||
499 MI->getOpcode() == ARM::LDM_RET);
500 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
502 O << ARM_AM::getAMSubModeStr(Mode);
504 printOperand(MI, Op);
505 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
510 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
511 const char *Modifier) {
512 const MachineOperand &MO1 = MI->getOperand(Op);
513 const MachineOperand &MO2 = MI->getOperand(Op+1);
515 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
516 printOperand(MI, Op);
520 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
522 if (Modifier && strcmp(Modifier, "submode") == 0) {
523 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
524 if (MO1.getReg() == ARM::SP) {
525 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
526 MI->getOpcode() == ARM::FLDMS);
527 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
529 O << ARM_AM::getAMSubModeStr(Mode);
531 } else if (Modifier && strcmp(Modifier, "base") == 0) {
532 // Used for FSTM{D|S} and LSTM{D|S} operations.
533 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
534 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
539 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
541 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
543 << (char)ARM_AM::getAM5Op(MO2.getImm())
549 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
550 const char *Modifier) {
551 if (Modifier && strcmp(Modifier, "label") == 0) {
552 printPCLabel(MI, Op+1);
556 const MachineOperand &MO1 = MI->getOperand(Op);
557 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
558 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).Name << "]";
562 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
563 const MachineOperand &MO1 = MI->getOperand(Op);
564 const MachineOperand &MO2 = MI->getOperand(Op+1);
565 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
566 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).Name << "]";
570 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
572 const MachineOperand &MO1 = MI->getOperand(Op);
573 const MachineOperand &MO2 = MI->getOperand(Op+1);
574 const MachineOperand &MO3 = MI->getOperand(Op+2);
576 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
577 printOperand(MI, Op);
581 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
583 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).Name;
584 else if (unsigned ImmOffs = MO2.getImm()) {
585 O << ", #" << ImmOffs;
593 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
594 printThumbAddrModeRI5Operand(MI, Op, 1);
597 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
598 printThumbAddrModeRI5Operand(MI, Op, 2);
601 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
602 printThumbAddrModeRI5Operand(MI, Op, 4);
605 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
606 const MachineOperand &MO1 = MI->getOperand(Op);
607 const MachineOperand &MO2 = MI->getOperand(Op+1);
608 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
609 if (unsigned ImmOffs = MO2.getImm())
610 O << ", #" << ImmOffs << " * 4";
614 void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
615 int CC = (int)MI->getOperand(opNum).getImmedValue();
616 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
619 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
620 int Id = (int)MI->getOperand(opNum).getImmedValue();
621 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
624 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
626 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
628 if (i != e-1) O << ", ";
633 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
634 const char *Modifier) {
635 assert(Modifier && "This operand only works with a modifier!");
636 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
638 if (!strcmp(Modifier, "label")) {
639 unsigned ID = MI->getOperand(OpNo).getImm();
640 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
641 << '_' << ID << ":\n";
643 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
644 unsigned CPI = MI->getOperand(OpNo).getConstantPoolIndex();
646 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
647 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
649 if (MCPE.isMachineConstantPoolEntry())
650 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
652 EmitGlobalConstant(MCPE.Val.ConstVal);
653 // remember to emit the weak reference
654 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
655 if (GV->hasExternalWeakLinkage())
656 ExtWeakSymbols.insert(GV);
661 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
662 const MachineOperand &MO1 = MI->getOperand(OpNo);
663 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
664 unsigned JTI = MO1.getJumpTableIndex();
665 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
666 << '_' << JTI << '_' << MO2.getImmedValue() << ":\n";
668 const char *JTEntryDirective = TAI->getJumpTableDirective();
669 if (!JTEntryDirective)
670 JTEntryDirective = TAI->getData32bitsDirective();
672 const MachineFunction *MF = MI->getParent()->getParent();
673 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
674 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
675 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
676 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
677 std::set<MachineBasicBlock*> JTSets;
678 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
679 MachineBasicBlock *MBB = JTBBs[i];
680 if (UseSet && JTSets.insert(MBB).second)
681 printSetLabel(JTI, MO2.getImmedValue(), MBB);
683 O << JTEntryDirective << ' ';
685 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
686 << '_' << JTI << '_' << MO2.getImmedValue()
687 << "_set_" << MBB->getNumber();
688 else if (TM.getRelocationModel() == Reloc::PIC_) {
689 printBasicBlockLabel(MBB, false, false);
690 // If the arch uses custom Jump Table directives, don't calc relative to JT
691 if (!TAI->getJumpTableDirective())
692 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
693 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImmedValue();
695 printBasicBlockLabel(MBB, false, false);
702 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
703 unsigned AsmVariant, const char *ExtraCode){
704 // Does this asm operand have a single letter operand modifier?
705 if (ExtraCode && ExtraCode[0]) {
706 if (ExtraCode[1] != 0) return true; // Unknown modifier.
708 switch (ExtraCode[0]) {
709 default: return true; // Unknown modifier.
710 case 'c': // Don't print "$" before a global var name or constant.
711 case 'P': // Print a VFP double precision register.
712 printOperand(MI, OpNo);
715 if (TM.getTargetData()->isLittleEndian())
719 if (TM.getTargetData()->isBigEndian())
722 case 'H': // Write second word of DI / DF reference.
723 // Verify that this operand has two consecutive registers.
724 if (!MI->getOperand(OpNo).isRegister() ||
725 OpNo+1 == MI->getNumOperands() ||
726 !MI->getOperand(OpNo+1).isRegister())
728 ++OpNo; // Return the high-part.
732 printOperand(MI, OpNo);
736 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
739 int Opc = MI->getOpcode();
741 case ARM::CONSTPOOL_ENTRY:
742 if (!InCPMode && AFI->isThumbFunction()) {
748 if (InCPMode && AFI->isThumbFunction())
761 // Call the autogenerated instruction printer routines.
762 printInstruction(MI);
765 bool ARMAsmPrinter::doInitialization(Module &M) {
766 if (Subtarget->isTargetDarwin()) {
767 // Emit initial debug information.
771 return AsmPrinter::doInitialization(M);
774 bool ARMAsmPrinter::doFinalization(Module &M) {
775 const TargetData *TD = TM.getTargetData();
777 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
779 if (!I->hasInitializer()) // External global require no code
782 if (EmitSpecialLLVMGlobal(I)) {
783 if (Subtarget->isTargetDarwin() &&
784 TM.getRelocationModel() == Reloc::Static) {
785 if (I->getName() == "llvm.global_ctors")
786 O << ".reference .constructors_used\n";
787 else if (I->getName() == "llvm.global_dtors")
788 O << ".reference .destructors_used\n";
793 std::string name = Mang->getValueName(I);
794 Constant *C = I->getInitializer();
795 const Type *Type = C->getType();
796 unsigned Size = TD->getTypeSize(Type);
797 unsigned Align = TD->getPreferredAlignmentLog(I);
799 const char *VisibilityDirective = NULL;
800 if (I->hasHiddenVisibility())
801 VisibilityDirective = TAI->getHiddenDirective();
802 else if (I->hasProtectedVisibility())
803 VisibilityDirective = TAI->getProtectedDirective();
805 if (VisibilityDirective)
806 O << VisibilityDirective << name << "\n";
808 if (Subtarget->isTargetELF())
809 O << "\t.type " << name << ",%object\n";
811 if (C->isNullValue()) {
812 if (I->hasExternalLinkage()) {
813 if (const char *Directive = TAI->getZeroFillDirective()) {
814 O << "\t.globl\t" << name << "\n";
815 O << Directive << "__DATA__, __common, " << name << ", "
816 << Size << ", " << Align << "\n";
821 if (!I->hasSection() &&
822 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
823 I->hasLinkOnceLinkage())) {
824 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
825 if (!NoZerosInBSS && TAI->getBSSSection())
826 SwitchToDataSection(TAI->getBSSSection(), I);
828 SwitchToDataSection(TAI->getDataSection(), I);
829 if (TAI->getLCOMMDirective() != NULL) {
830 if (I->hasInternalLinkage()) {
831 O << TAI->getLCOMMDirective() << name << "," << Size;
832 if (Subtarget->isTargetDarwin())
835 O << TAI->getCOMMDirective() << name << "," << Size;
837 if (I->hasInternalLinkage())
838 O << "\t.local\t" << name << "\n";
839 O << TAI->getCOMMDirective() << name << "," << Size;
840 if (TAI->getCOMMDirectiveTakesAlignment())
841 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
843 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
848 switch (I->getLinkage()) {
849 case GlobalValue::LinkOnceLinkage:
850 case GlobalValue::WeakLinkage:
851 if (Subtarget->isTargetDarwin()) {
852 O << "\t.globl " << name << "\n"
853 << "\t.weak_definition " << name << "\n";
854 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
856 std::string SectionName("\t.section\t.llvm.linkonce.d." +
858 ",\"aw\",%progbits");
859 SwitchToDataSection(SectionName.c_str(), I);
860 O << "\t.weak " << name << "\n";
863 case GlobalValue::AppendingLinkage:
864 // FIXME: appending linkage variables should go into a section of
865 // their name or something. For now, just emit them as external.
866 case GlobalValue::ExternalLinkage:
867 O << "\t.globl " << name << "\n";
869 case GlobalValue::InternalLinkage: {
870 if (I->isConstant()) {
871 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
872 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
873 SwitchToDataSection(TAI->getCStringSection(), I);
877 // FIXME: special handling for ".ctors" & ".dtors" sections
878 if (I->hasSection() &&
879 (I->getSection() == ".ctors" ||
880 I->getSection() == ".dtors")) {
881 assert(!Subtarget->isTargetDarwin());
882 std::string SectionName = ".section " + I->getSection();
883 SectionName += ",\"aw\",%progbits";
884 SwitchToDataSection(SectionName.c_str());
886 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
887 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
888 TAI->getBSSSection(), I);
889 else if (!I->isConstant())
890 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
891 TAI->getDataSection(), I);
892 else if (I->isThreadLocal())
893 SwitchToDataSection(TAI->getTLSDataSection());
896 bool HasReloc = C->ContainsRelocations();
898 Subtarget->isTargetDarwin() &&
899 TM.getRelocationModel() != Reloc::Static)
900 SwitchToDataSection("\t.const_data\n");
901 else if (!HasReloc && Size == 4 &&
902 TAI->getFourByteConstantSection())
903 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
904 else if (!HasReloc && Size == 8 &&
905 TAI->getEightByteConstantSection())
906 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
907 else if (!HasReloc && Size == 16 &&
908 TAI->getSixteenByteConstantSection())
909 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
910 else if (TAI->getReadOnlySection())
911 SwitchToDataSection(TAI->getReadOnlySection(), I);
913 SwitchToDataSection(TAI->getDataSection(), I);
920 assert(0 && "Unknown linkage type!");
924 EmitAlignment(Align, I);
925 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
927 if (TAI->hasDotTypeDotSizeDirective())
928 O << "\t.size " << name << ", " << Size << "\n";
929 // If the initializer is a extern weak symbol, remember to emit the weak
931 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
932 if (GV->hasExternalWeakLinkage())
933 ExtWeakSymbols.insert(GV);
935 EmitGlobalConstant(C);
939 if (Subtarget->isTargetDarwin()) {
940 SwitchToDataSection("");
942 // Output stubs for dynamically-linked functions
944 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
946 if (TM.getRelocationModel() == Reloc::PIC_)
947 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
950 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
954 O << "\t.code\t32\n";
956 O << "L" << *i << "$stub:\n";
957 O << "\t.indirect_symbol " << *i << "\n";
958 O << "\tldr ip, L" << *i << "$slp\n";
959 if (TM.getRelocationModel() == Reloc::PIC_) {
960 O << "L" << *i << "$scv:\n";
961 O << "\tadd ip, pc, ip\n";
963 O << "\tldr pc, [ip, #0]\n";
964 O << "L" << *i << "$slp:\n";
965 if (TM.getRelocationModel() == Reloc::PIC_)
966 O << "\t.long\tL" << *i << "$lazy_ptr-(L" << *i << "$scv+8)\n";
968 O << "\t.long\tL" << *i << "$lazy_ptr\n";
969 SwitchToDataSection(".lazy_symbol_pointer", 0);
970 O << "L" << *i << "$lazy_ptr:\n";
971 O << "\t.indirect_symbol " << *i << "\n";
972 O << "\t.long\tdyld_stub_binding_helper\n";
976 // Output non-lazy-pointers for external and common global variables.
977 if (GVNonLazyPtrs.begin() != GVNonLazyPtrs.end())
978 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
979 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
980 e = GVNonLazyPtrs.end(); i != e; ++i) {
981 O << "L" << *i << "$non_lazy_ptr:\n";
982 O << "\t.indirect_symbol " << *i << "\n";
986 // Emit initial debug information.
989 // Funny Darwin hack: This flag tells the linker that no global symbols
990 // contain code that falls through to other global symbols (e.g. the obvious
991 // implementation of multiple entry points). If this doesn't occur, the
992 // linker can safely perform dead code stripping. Since LLVM never
993 // generates code that does this, it is always safe to set.
994 O << "\t.subsections_via_symbols\n";
997 AsmPrinter::doFinalization(M);
998 return false; // success