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"
42 STATISTIC(EmittedInsts, "Number of machine instrs printed");
45 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
46 ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
47 : AsmPrinter(O, TM, T), DW(O, this, T), AFI(NULL), InCPMode(false) {
48 Subtarget = &TM.getSubtarget<ARMSubtarget>();
53 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
54 /// make the right decision when printing asm code for different targets.
55 const ARMSubtarget *Subtarget;
57 /// AFI - Keep a pointer to ARMFunctionInfo for the current
61 /// We name each basic block in a Function with a unique number, so
62 /// that we can consistently refer to them later. This is cleared
63 /// at the beginning of each call to runOnMachineFunction().
65 typedef std::map<const Value *, unsigned> ValueMapTy;
66 ValueMapTy NumberForBB;
68 /// Keeps the set of GlobalValues that require non-lazy-pointers for
70 std::set<std::string> GVNonLazyPtrs;
72 /// Keeps the set of external function GlobalAddresses that the asm
73 /// printer should generate stubs for.
74 std::set<std::string> FnStubs;
76 /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
79 virtual const char *getPassName() const {
80 return "ARM Assembly Printer";
83 void printOperand(const MachineInstr *MI, int opNum,
84 const char *Modifier = 0);
85 void printSOImmOperand(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 = Mang->getValueName(GV);
126 if (ACPV->isNonLazyPointer()) {
127 GVNonLazyPtrs.insert(Name);
128 O << TAI->getPrivateGlobalPrefix() << Name << "$non_lazy_ptr";
131 if (ACPV->getPCAdjustment() != 0)
132 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
133 << utostr(ACPV->getLabelId())
134 << "+" << (unsigned)ACPV->getPCAdjustment() << ")";
137 // If the constant pool value is a extern weak symbol, remember to emit
138 // the weak reference.
139 if (GV->hasExternalWeakLinkage())
140 ExtWeakSymbols.insert(GV);
143 void getAnalysisUsage(AnalysisUsage &AU) const {
144 AU.setPreservesAll();
145 AU.addRequired<MachineModuleInfo>();
148 } // end of anonymous namespace
150 #include "ARMGenAsmWriter.inc"
152 /// createARMCodePrinterPass - Returns a pass that prints the ARM
153 /// assembly code for a MachineFunction to the given output stream,
154 /// using the given target machine description. This should work
155 /// regardless of whether the function is in SSA form.
157 FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
158 ARMTargetMachine &tm) {
159 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
162 /// runOnMachineFunction - This uses the printInstruction()
163 /// method to print assembly for each instruction.
165 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
166 AFI = MF.getInfo<ARMFunctionInfo>();
168 if (Subtarget->isTargetDarwin()) {
169 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
172 SetupMachineFunction(MF);
175 // NOTE: we don't print out constant pools here, they are handled as
179 // Print out labels for the function.
180 const Function *F = MF.getFunction();
181 switch (F->getLinkage()) {
182 default: assert(0 && "Unknown linkage type!");
183 case Function::InternalLinkage:
184 SwitchToTextSection("\t.text", F);
186 case Function::ExternalLinkage:
187 SwitchToTextSection("\t.text", F);
188 O << "\t.globl\t" << CurrentFnName << "\n";
190 case Function::WeakLinkage:
191 case Function::LinkOnceLinkage:
192 if (Subtarget->isTargetDarwin()) {
194 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
195 O << "\t.globl\t" << CurrentFnName << "\n";
196 O << "\t.weak_definition\t" << CurrentFnName << "\n";
198 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
203 if (AFI->isThumbFunction()) {
205 O << "\t.code\t16\n";
206 O << "\t.thumb_func\t" << CurrentFnName << "\n";
211 O << CurrentFnName << ":\n";
212 if (Subtarget->isTargetDarwin()) {
213 // Emit pre-function debug information.
214 DW.BeginFunction(&MF);
217 // Print out code for the function.
218 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
220 // Print a label for the basic block.
221 if (I != MF.begin()) {
222 printBasicBlockLabel(I, true);
225 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
227 // Print the assembly for the instruction.
228 printMachineInstruction(II);
232 if (TAI->hasDotTypeDotSizeDirective())
233 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
235 if (Subtarget->isTargetDarwin()) {
236 // Emit post-function debug information.
243 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
244 const char *Modifier) {
245 const MachineOperand &MO = MI->getOperand(opNum);
246 switch (MO.getType()) {
247 case MachineOperand::MO_Register:
248 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
249 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
251 assert(0 && "not implemented");
253 case MachineOperand::MO_Immediate: {
254 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
257 O << (int)MO.getImmedValue();
260 case MachineOperand::MO_MachineBasicBlock:
261 printBasicBlockLabel(MO.getMachineBasicBlock());
263 case MachineOperand::MO_GlobalAddress: {
264 bool isCallOp = Modifier && !strcmp(Modifier, "call");
265 GlobalValue *GV = MO.getGlobal();
266 std::string Name = Mang->getValueName(GV);
267 bool isExt = (GV->isExternal() || GV->hasWeakLinkage() ||
268 GV->hasLinkOnceLinkage());
269 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
270 TM.getRelocationModel() != Reloc::Static) {
271 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
272 FnStubs.insert(Name);
276 if (GV->hasExternalWeakLinkage())
277 ExtWeakSymbols.insert(GV);
280 case MachineOperand::MO_ExternalSymbol: {
281 bool isCallOp = Modifier && !strcmp(Modifier, "call");
282 std::string Name(TAI->getGlobalPrefix());
283 Name += MO.getSymbolName();
284 if (isCallOp && Subtarget->isTargetDarwin() &&
285 TM.getRelocationModel() != Reloc::Static) {
286 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
287 FnStubs.insert(Name);
292 case MachineOperand::MO_ConstantPoolIndex:
293 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
294 << '_' << MO.getConstantPoolIndex();
296 case MachineOperand::MO_JumpTableIndex:
297 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
298 << '_' << MO.getJumpTableIndex();
301 O << "<unknown operand type>"; abort (); break;
305 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
306 /// immediate in bits 0-7.
307 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
308 const MachineOperand &MO = MI->getOperand(OpNum);
309 assert(MO.isImmediate() && (MO.getImmedValue() < (1 << 12)) &&
310 "Not a valid so_imm value!");
311 unsigned Imm = ARM_AM::getSOImmValImm(MO.getImmedValue());
312 unsigned Rot = ARM_AM::getSOImmValRot(MO.getImmedValue());
314 // Print low-level immediate formation info, per
315 // A5.1.3: "Data-processing operands - Immediate".
317 O << "#" << Imm << ", " << Rot;
318 // Pretty printed version.
319 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
325 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
326 // "Addressing Mode 1 - Data-processing operands" forms. This includes:
328 // REG REG 0,SH_OPC - e.g. R5, ROR R3
329 // REG 0 IMM,SH_OPC - e.g. R5, LSL #3
330 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
331 const MachineOperand &MO1 = MI->getOperand(Op);
332 const MachineOperand &MO2 = MI->getOperand(Op+1);
333 const MachineOperand &MO3 = MI->getOperand(Op+2);
335 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
336 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
338 // Print the shift opc.
340 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImmedValue()))
344 assert(MRegisterInfo::isPhysicalRegister(MO2.getReg()));
345 O << TM.getRegisterInfo()->get(MO2.getReg()).Name;
346 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
348 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
352 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
353 const MachineOperand &MO1 = MI->getOperand(Op);
354 const MachineOperand &MO2 = MI->getOperand(Op+1);
355 const MachineOperand &MO3 = MI->getOperand(Op+2);
357 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
358 printOperand(MI, Op);
362 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
365 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
367 << (char)ARM_AM::getAM2Op(MO3.getImm())
368 << ARM_AM::getAM2Offset(MO3.getImm());
374 << (char)ARM_AM::getAM2Op(MO3.getImm())
375 << TM.getRegisterInfo()->get(MO2.getReg()).Name;
377 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
379 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImmedValue()))
384 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
385 const MachineOperand &MO1 = MI->getOperand(Op);
386 const MachineOperand &MO2 = MI->getOperand(Op+1);
389 if (ARM_AM::getAM2Offset(MO2.getImm())) // Don't print +0.
391 << (char)ARM_AM::getAM2Op(MO2.getImm())
392 << ARM_AM::getAM2Offset(MO2.getImm());
396 O << (char)ARM_AM::getAM2Op(MO2.getImm())
397 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
399 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
401 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImmedValue()))
405 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
406 const MachineOperand &MO1 = MI->getOperand(Op);
407 const MachineOperand &MO2 = MI->getOperand(Op+1);
408 const MachineOperand &MO3 = MI->getOperand(Op+2);
410 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
411 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
415 << (char)ARM_AM::getAM3Op(MO3.getImm())
416 << TM.getRegisterInfo()->get(MO2.getReg()).Name
421 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
423 << (char)ARM_AM::getAM3Op(MO3.getImm())
428 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
429 const MachineOperand &MO1 = MI->getOperand(Op);
430 const MachineOperand &MO2 = MI->getOperand(Op+1);
433 O << (char)ARM_AM::getAM3Op(MO2.getImm())
434 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
438 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
440 << (char)ARM_AM::getAM3Op(MO2.getImm())
444 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
445 const char *Modifier) {
446 const MachineOperand &MO1 = MI->getOperand(Op);
447 const MachineOperand &MO2 = MI->getOperand(Op+1);
448 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
449 if (Modifier && strcmp(Modifier, "submode") == 0) {
450 if (MO1.getReg() == ARM::SP) {
451 bool isLDM = (MI->getOpcode() == ARM::LDM ||
452 MI->getOpcode() == ARM::LDM_RET);
453 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
455 O << ARM_AM::getAMSubModeStr(Mode);
457 printOperand(MI, Op);
458 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
463 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
464 const char *Modifier) {
465 const MachineOperand &MO1 = MI->getOperand(Op);
466 const MachineOperand &MO2 = MI->getOperand(Op+1);
468 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
469 printOperand(MI, Op);
473 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
475 if (Modifier && strcmp(Modifier, "submode") == 0) {
476 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
477 if (MO1.getReg() == ARM::SP) {
478 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
479 MI->getOpcode() == ARM::FLDMS);
480 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
482 O << ARM_AM::getAMSubModeStr(Mode);
484 } else if (Modifier && strcmp(Modifier, "base") == 0) {
485 // Used for FSTM{D|S} and LSTM{D|S} operations.
486 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
487 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
492 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
494 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
496 << (char)ARM_AM::getAM5Op(MO2.getImm())
502 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
503 const char *Modifier) {
504 if (Modifier && strcmp(Modifier, "label") == 0) {
505 printPCLabel(MI, Op+1);
509 const MachineOperand &MO1 = MI->getOperand(Op);
510 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
511 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).Name << "]";
515 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
516 const MachineOperand &MO1 = MI->getOperand(Op);
517 const MachineOperand &MO2 = MI->getOperand(Op+1);
518 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
519 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).Name << "]";
523 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
525 const MachineOperand &MO1 = MI->getOperand(Op);
526 const MachineOperand &MO2 = MI->getOperand(Op+2);
528 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
529 printOperand(MI, Op);
533 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
534 if (unsigned ImmOffs = MO2.getImm()) {
535 O << ", #" << ImmOffs;
543 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
544 if (MI->getOperand(Op+1).getReg())
545 printThumbAddrModeRROperand(MI, Op);
547 printThumbAddrModeRI5Operand(MI, Op, 1);
550 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
551 if (MI->getOperand(Op+1).getReg())
552 printThumbAddrModeRROperand(MI, Op);
554 printThumbAddrModeRI5Operand(MI, Op, 2);
557 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
558 if (MI->getOperand(Op+1).getReg())
559 printThumbAddrModeRROperand(MI, Op);
561 printThumbAddrModeRI5Operand(MI, Op, 4);
564 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
565 const MachineOperand &MO1 = MI->getOperand(Op);
566 const MachineOperand &MO2 = MI->getOperand(Op+1);
567 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
568 if (unsigned ImmOffs = MO2.getImm())
569 O << ", #" << ImmOffs << " * 4";
573 void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
574 int CC = (int)MI->getOperand(opNum).getImmedValue();
575 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
578 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
579 int Id = (int)MI->getOperand(opNum).getImmedValue();
580 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
583 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
585 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
587 if (i != e-1) O << ", ";
592 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
593 const char *Modifier) {
594 assert(Modifier && "This operand only works with a modifier!");
595 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
597 if (!strcmp(Modifier, "label")) {
598 unsigned ID = MI->getOperand(OpNo).getImm();
599 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
600 << '_' << ID << ":\n";
602 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
603 unsigned CPI = MI->getOperand(OpNo).getConstantPoolIndex();
605 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
606 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
608 if (MCPE.isMachineConstantPoolEntry())
609 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
611 EmitGlobalConstant(MCPE.Val.ConstVal);
615 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
616 const MachineOperand &MO1 = MI->getOperand(OpNo);
617 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
618 unsigned JTI = MO1.getJumpTableIndex();
619 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
620 << '_' << JTI << '_' << MO2.getImmedValue() << ":\n";
622 const char *JTEntryDirective = TAI->getJumpTableDirective();
623 if (!JTEntryDirective)
624 JTEntryDirective = TAI->getData32bitsDirective();
626 const MachineFunction *MF = MI->getParent()->getParent();
627 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
628 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
629 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
630 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
631 std::set<MachineBasicBlock*> JTSets;
632 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
633 MachineBasicBlock *MBB = JTBBs[i];
634 if (UseSet && JTSets.insert(MBB).second)
635 printSetLabel(JTI, MO2.getImmedValue(), MBB);
637 O << JTEntryDirective << ' ';
639 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
640 << '_' << JTI << '_' << MO2.getImmedValue()
641 << "_set_" << MBB->getNumber();
642 else if (TM.getRelocationModel() == Reloc::PIC_) {
643 printBasicBlockLabel(MBB, false, false);
644 // If the arch uses custom Jump Table directives, don't calc relative to JT
645 if (!TAI->getJumpTableDirective())
646 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
647 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImmedValue();
649 printBasicBlockLabel(MBB, false, false);
656 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
657 unsigned AsmVariant, const char *ExtraCode){
658 // Does this asm operand have a single letter operand modifier?
659 if (ExtraCode && ExtraCode[0]) {
660 if (ExtraCode[1] != 0) return true; // Unknown modifier.
662 switch (ExtraCode[0]) {
663 default: return true; // Unknown modifier.
665 if (TM.getTargetData()->isLittleEndian())
669 if (TM.getTargetData()->isBigEndian())
672 case 'H': // Write second word of DI / DF reference.
673 // Verify that this operand has two consecutive registers.
674 if (!MI->getOperand(OpNo).isRegister() ||
675 OpNo+1 == MI->getNumOperands() ||
676 !MI->getOperand(OpNo+1).isRegister())
678 ++OpNo; // Return the high-part.
682 printOperand(MI, OpNo);
686 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
689 if (MI->getOpcode() == ARM::CONSTPOOL_ENTRY) {
690 if (!InCPMode && AFI->isThumbFunction()) {
695 if (InCPMode && AFI->isThumbFunction()) {
702 // Call the autogenerated instruction printer routines.
703 printInstruction(MI);
706 bool ARMAsmPrinter::doInitialization(Module &M) {
707 if (Subtarget->isTargetDarwin()) {
708 // Emit initial debug information.
712 return AsmPrinter::doInitialization(M);
715 bool ARMAsmPrinter::doFinalization(Module &M) {
716 const TargetData *TD = TM.getTargetData();
718 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
720 if (!I->hasInitializer()) // External global require no code
723 if (EmitSpecialLLVMGlobal(I))
726 std::string name = Mang->getValueName(I);
727 Constant *C = I->getInitializer();
728 unsigned Size = TD->getTypeSize(C->getType());
729 unsigned Align = TD->getPreferredAlignmentLog(I);
731 if (I->hasHiddenVisibility())
732 if (const char *Directive = TAI->getHiddenDirective())
733 O << Directive << name << "\n";
734 if (Subtarget->isTargetELF())
735 O << "\t.type " << name << ",%object\n";
737 if (C->isNullValue()) {
738 if (I->hasExternalLinkage()) {
739 if (const char *Directive = TAI->getZeroFillDirective()) {
740 O << "\t.globl\t" << name << "\n";
741 O << Directive << "__DATA__, __common, " << name << ", "
742 << Size << ", " << Align << "\n";
747 if (!I->hasSection() &&
748 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
749 I->hasLinkOnceLinkage())) {
750 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
751 if (!NoZerosInBSS && TAI->getBSSSection())
752 SwitchToDataSection(TAI->getBSSSection(), I);
754 SwitchToDataSection(TAI->getDataSection(), I);
755 if (TAI->getLCOMMDirective() != NULL) {
756 if (I->hasInternalLinkage()) {
757 O << TAI->getLCOMMDirective() << name << "," << Size;
758 if (Subtarget->isTargetDarwin())
761 O << TAI->getCOMMDirective() << name << "," << Size;
763 if (I->hasInternalLinkage())
764 O << "\t.local\t" << name << "\n";
765 O << TAI->getCOMMDirective() << name << "," << Size;
766 if (TAI->getCOMMDirectiveTakesAlignment())
767 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
769 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
774 switch (I->getLinkage()) {
775 case GlobalValue::LinkOnceLinkage:
776 case GlobalValue::WeakLinkage:
777 if (Subtarget->isTargetDarwin()) {
778 O << "\t.globl " << name << "\n"
779 << "\t.weak_definition " << name << "\n";
780 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
782 std::string SectionName("\t.section\t.llvm.linkonce.d." +
784 ",\"aw\",%progbits");
785 SwitchToDataSection(SectionName.c_str(), I);
786 O << "\t.weak " << name << "\n";
789 case GlobalValue::AppendingLinkage:
790 // FIXME: appending linkage variables should go into a section of
791 // their name or something. For now, just emit them as external.
792 case GlobalValue::ExternalLinkage:
793 O << "\t.globl " << name << "\n";
795 case GlobalValue::InternalLinkage: {
796 if (I->isConstant()) {
797 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
798 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
799 SwitchToDataSection(TAI->getCStringSection(), I);
803 // FIXME: special handling for ".ctors" & ".dtors" sections
804 if (I->hasSection() &&
805 (I->getSection() == ".ctors" ||
806 I->getSection() == ".dtors")) {
807 assert(!Subtarget->isTargetDarwin());
808 std::string SectionName = ".section " + I->getSection();
809 SectionName += ",\"aw\",%progbits";
810 SwitchToDataSection(SectionName.c_str());
812 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
813 SwitchToDataSection(TAI->getBSSSection(), I);
815 SwitchToDataSection(TAI->getDataSection(), I);
821 assert(0 && "Unknown linkage type!");
825 EmitAlignment(Align, I);
826 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
828 if (TAI->hasDotTypeDotSizeDirective())
829 O << "\t.size " << name << ", " << Size << "\n";
830 // If the initializer is a extern weak symbol, remember to emit the weak
832 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
833 if (GV->hasExternalWeakLinkage())
834 ExtWeakSymbols.insert(GV);
836 EmitGlobalConstant(C);
840 if (Subtarget->isTargetDarwin()) {
841 SwitchToDataSection("");
843 // Output stubs for dynamically-linked functions
845 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
847 if (TM.getRelocationModel() == Reloc::PIC_)
848 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
851 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
855 O << "\t.code\t32\n";
857 O << "L" << *i << "$stub:\n";
858 O << "\t.indirect_symbol " << *i << "\n";
859 O << "\tldr ip, L" << *i << "$slp\n";
860 if (TM.getRelocationModel() == Reloc::PIC_) {
861 O << "L" << *i << "$scv:\n";
862 O << "\tadd ip, pc, ip\n";
864 O << "\tldr pc, [ip, #0]\n";
865 O << "L" << *i << "$slp:\n";
866 if (TM.getRelocationModel() == Reloc::PIC_)
867 O << "\t.long\tL" << *i << "$lazy_ptr-(L" << *i << "$scv+8)\n";
869 O << "\t.long\tL" << *i << "$lazy_ptr\n";
870 SwitchToDataSection(".lazy_symbol_pointer", 0);
871 O << "L" << *i << "$lazy_ptr:\n";
872 O << "\t.indirect_symbol " << *i << "\n";
873 O << "\t.long\tdyld_stub_binding_helper\n";
877 // Output non-lazy-pointers for external and common global variables.
878 if (GVNonLazyPtrs.begin() != GVNonLazyPtrs.end())
879 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
880 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
881 e = GVNonLazyPtrs.end(); i != e; ++i) {
882 O << "L" << *i << "$non_lazy_ptr:\n";
883 O << "\t.indirect_symbol " << *i << "\n";
887 // Emit initial debug information.
890 // Funny Darwin hack: This flag tells the linker that no global symbols
891 // contain code that falls through to other global symbols (e.g. the obvious
892 // implementation of multiple entry points). If this doesn't occur, the
893 // linker can safely perform dead code stripping. Since LLVM never
894 // generates code that does this, it is always safe to set.
895 O << "\t.subsections_via_symbols\n";
898 AsmPrinter::doFinalization(M);
899 return false; // success