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->getPCAdjustment() != 0)
137 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
138 << utostr(ACPV->getLabelId())
139 << "+" << (unsigned)ACPV->getPCAdjustment() << ")";
142 // If the constant pool value is a extern weak symbol, remember to emit
143 // the weak reference.
144 if (GV && GV->hasExternalWeakLinkage())
145 ExtWeakSymbols.insert(GV);
148 void getAnalysisUsage(AnalysisUsage &AU) const {
149 AU.setPreservesAll();
150 AU.addRequired<MachineModuleInfo>();
153 } // end of anonymous namespace
155 #include "ARMGenAsmWriter.inc"
157 /// createARMCodePrinterPass - Returns a pass that prints the ARM
158 /// assembly code for a MachineFunction to the given output stream,
159 /// using the given target machine description. This should work
160 /// regardless of whether the function is in SSA form.
162 FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
163 ARMTargetMachine &tm) {
164 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
167 /// runOnMachineFunction - This uses the printInstruction()
168 /// method to print assembly for each instruction.
170 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
171 AFI = MF.getInfo<ARMFunctionInfo>();
173 if (Subtarget->isTargetDarwin()) {
174 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
177 SetupMachineFunction(MF);
180 // NOTE: we don't print out constant pools here, they are handled as
184 // Print out labels for the function.
185 const Function *F = MF.getFunction();
186 switch (F->getLinkage()) {
187 default: assert(0 && "Unknown linkage type!");
188 case Function::InternalLinkage:
189 SwitchToTextSection("\t.text", F);
191 case Function::ExternalLinkage:
192 SwitchToTextSection("\t.text", F);
193 O << "\t.globl\t" << CurrentFnName << "\n";
195 case Function::WeakLinkage:
196 case Function::LinkOnceLinkage:
197 if (Subtarget->isTargetDarwin()) {
199 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
200 O << "\t.globl\t" << CurrentFnName << "\n";
201 O << "\t.weak_definition\t" << CurrentFnName << "\n";
203 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
208 if (F->hasHiddenVisibility())
209 if (const char *Directive = TAI->getHiddenDirective())
210 O << Directive << CurrentFnName << "\n";
212 if (AFI->isThumbFunction()) {
214 O << "\t.code\t16\n";
215 O << "\t.thumb_func";
216 if (Subtarget->isTargetDarwin())
217 O << "\t" << CurrentFnName;
223 O << CurrentFnName << ":\n";
224 if (Subtarget->isTargetDarwin()) {
225 // Emit pre-function debug information.
226 DW.BeginFunction(&MF);
229 // Print out code for the function.
230 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
232 // Print a label for the basic block.
233 if (I != MF.begin()) {
234 printBasicBlockLabel(I, true);
237 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
239 // Print the assembly for the instruction.
240 printMachineInstruction(II);
244 if (TAI->hasDotTypeDotSizeDirective())
245 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
247 if (Subtarget->isTargetDarwin()) {
248 // Emit post-function debug information.
255 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
256 const char *Modifier) {
257 const MachineOperand &MO = MI->getOperand(opNum);
258 switch (MO.getType()) {
259 case MachineOperand::MO_Register:
260 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
261 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
263 assert(0 && "not implemented");
265 case MachineOperand::MO_Immediate: {
266 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
269 O << (int)MO.getImmedValue();
272 case MachineOperand::MO_MachineBasicBlock:
273 printBasicBlockLabel(MO.getMachineBasicBlock());
275 case MachineOperand::MO_GlobalAddress: {
276 bool isCallOp = Modifier && !strcmp(Modifier, "call");
277 GlobalValue *GV = MO.getGlobal();
278 std::string Name = Mang->getValueName(GV);
279 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
280 GV->hasLinkOnceLinkage());
281 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
282 TM.getRelocationModel() != Reloc::Static) {
283 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
284 FnStubs.insert(Name);
288 if (GV->hasExternalWeakLinkage())
289 ExtWeakSymbols.insert(GV);
292 case MachineOperand::MO_ExternalSymbol: {
293 bool isCallOp = Modifier && !strcmp(Modifier, "call");
294 std::string Name(TAI->getGlobalPrefix());
295 Name += MO.getSymbolName();
296 if (isCallOp && Subtarget->isTargetDarwin() &&
297 TM.getRelocationModel() != Reloc::Static) {
298 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
299 FnStubs.insert(Name);
304 case MachineOperand::MO_ConstantPoolIndex:
305 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
306 << '_' << MO.getConstantPoolIndex();
308 case MachineOperand::MO_JumpTableIndex:
309 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
310 << '_' << MO.getJumpTableIndex();
313 O << "<unknown operand type>"; abort (); break;
317 static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
318 assert(V < (1 << 12) && "Not a valid so_imm value!");
319 unsigned Imm = ARM_AM::getSOImmValImm(V);
320 unsigned Rot = ARM_AM::getSOImmValRot(V);
322 // Print low-level immediate formation info, per
323 // A5.1.3: "Data-processing operands - Immediate".
325 O << "#" << Imm << ", " << Rot;
326 // Pretty printed version.
327 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
333 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
334 /// immediate in bits 0-7.
335 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
336 const MachineOperand &MO = MI->getOperand(OpNum);
337 assert(MO.isImmediate() && "Not a valid so_imm value!");
338 printSOImm(O, MO.getImmedValue(), TAI);
341 /// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
342 /// followed by a or to materialize.
343 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
344 const MachineOperand &MO = MI->getOperand(OpNum);
345 assert(MO.isImmediate() && "Not a valid so_imm value!");
346 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImmedValue());
347 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImmedValue());
348 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
354 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
357 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
358 // "Addressing Mode 1 - Data-processing operands" forms. This includes:
360 // REG REG 0,SH_OPC - e.g. R5, ROR R3
361 // REG 0 IMM,SH_OPC - e.g. R5, LSL #3
362 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
363 const MachineOperand &MO1 = MI->getOperand(Op);
364 const MachineOperand &MO2 = MI->getOperand(Op+1);
365 const MachineOperand &MO3 = MI->getOperand(Op+2);
367 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
368 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
370 // Print the shift opc.
372 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImmedValue()))
376 assert(MRegisterInfo::isPhysicalRegister(MO2.getReg()));
377 O << TM.getRegisterInfo()->get(MO2.getReg()).Name;
378 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
380 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
384 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
385 const MachineOperand &MO1 = MI->getOperand(Op);
386 const MachineOperand &MO2 = MI->getOperand(Op+1);
387 const MachineOperand &MO3 = MI->getOperand(Op+2);
389 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
390 printOperand(MI, Op);
394 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
397 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
399 << (char)ARM_AM::getAM2Op(MO3.getImm())
400 << ARM_AM::getAM2Offset(MO3.getImm());
406 << (char)ARM_AM::getAM2Op(MO3.getImm())
407 << TM.getRegisterInfo()->get(MO2.getReg()).Name;
409 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
411 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImmedValue()))
416 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
417 const MachineOperand &MO1 = MI->getOperand(Op);
418 const MachineOperand &MO2 = MI->getOperand(Op+1);
421 if (ARM_AM::getAM2Offset(MO2.getImm())) // Don't print +0.
423 << (char)ARM_AM::getAM2Op(MO2.getImm())
424 << ARM_AM::getAM2Offset(MO2.getImm());
428 O << (char)ARM_AM::getAM2Op(MO2.getImm())
429 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
431 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
433 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImmedValue()))
437 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
438 const MachineOperand &MO1 = MI->getOperand(Op);
439 const MachineOperand &MO2 = MI->getOperand(Op+1);
440 const MachineOperand &MO3 = MI->getOperand(Op+2);
442 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
443 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
447 << (char)ARM_AM::getAM3Op(MO3.getImm())
448 << TM.getRegisterInfo()->get(MO2.getReg()).Name
453 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
455 << (char)ARM_AM::getAM3Op(MO3.getImm())
460 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
461 const MachineOperand &MO1 = MI->getOperand(Op);
462 const MachineOperand &MO2 = MI->getOperand(Op+1);
465 O << (char)ARM_AM::getAM3Op(MO2.getImm())
466 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
470 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
472 << (char)ARM_AM::getAM3Op(MO2.getImm())
476 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
477 const char *Modifier) {
478 const MachineOperand &MO1 = MI->getOperand(Op);
479 const MachineOperand &MO2 = MI->getOperand(Op+1);
480 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
481 if (Modifier && strcmp(Modifier, "submode") == 0) {
482 if (MO1.getReg() == ARM::SP) {
483 bool isLDM = (MI->getOpcode() == ARM::LDM ||
484 MI->getOpcode() == ARM::LDM_RET);
485 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
487 O << ARM_AM::getAMSubModeStr(Mode);
489 printOperand(MI, Op);
490 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
495 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
496 const char *Modifier) {
497 const MachineOperand &MO1 = MI->getOperand(Op);
498 const MachineOperand &MO2 = MI->getOperand(Op+1);
500 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
501 printOperand(MI, Op);
505 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
507 if (Modifier && strcmp(Modifier, "submode") == 0) {
508 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
509 if (MO1.getReg() == ARM::SP) {
510 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
511 MI->getOpcode() == ARM::FLDMS);
512 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
514 O << ARM_AM::getAMSubModeStr(Mode);
516 } else if (Modifier && strcmp(Modifier, "base") == 0) {
517 // Used for FSTM{D|S} and LSTM{D|S} operations.
518 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
519 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
524 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
526 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
528 << (char)ARM_AM::getAM5Op(MO2.getImm())
534 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
535 const char *Modifier) {
536 if (Modifier && strcmp(Modifier, "label") == 0) {
537 printPCLabel(MI, Op+1);
541 const MachineOperand &MO1 = MI->getOperand(Op);
542 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
543 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).Name << "]";
547 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
548 const MachineOperand &MO1 = MI->getOperand(Op);
549 const MachineOperand &MO2 = MI->getOperand(Op+1);
550 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
551 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).Name << "]";
555 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
557 const MachineOperand &MO1 = MI->getOperand(Op);
558 const MachineOperand &MO2 = MI->getOperand(Op+1);
559 const MachineOperand &MO3 = MI->getOperand(Op+2);
561 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
562 printOperand(MI, Op);
566 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
568 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).Name;
569 else if (unsigned ImmOffs = MO2.getImm()) {
570 O << ", #" << ImmOffs;
578 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
579 printThumbAddrModeRI5Operand(MI, Op, 1);
582 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
583 printThumbAddrModeRI5Operand(MI, Op, 2);
586 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
587 printThumbAddrModeRI5Operand(MI, Op, 4);
590 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
591 const MachineOperand &MO1 = MI->getOperand(Op);
592 const MachineOperand &MO2 = MI->getOperand(Op+1);
593 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
594 if (unsigned ImmOffs = MO2.getImm())
595 O << ", #" << ImmOffs << " * 4";
599 void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
600 int CC = (int)MI->getOperand(opNum).getImmedValue();
601 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
604 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
605 int Id = (int)MI->getOperand(opNum).getImmedValue();
606 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
609 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
611 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
613 if (i != e-1) O << ", ";
618 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
619 const char *Modifier) {
620 assert(Modifier && "This operand only works with a modifier!");
621 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
623 if (!strcmp(Modifier, "label")) {
624 unsigned ID = MI->getOperand(OpNo).getImm();
625 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
626 << '_' << ID << ":\n";
628 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
629 unsigned CPI = MI->getOperand(OpNo).getConstantPoolIndex();
631 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
632 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
634 if (MCPE.isMachineConstantPoolEntry())
635 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
637 EmitGlobalConstant(MCPE.Val.ConstVal);
641 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
642 const MachineOperand &MO1 = MI->getOperand(OpNo);
643 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
644 unsigned JTI = MO1.getJumpTableIndex();
645 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
646 << '_' << JTI << '_' << MO2.getImmedValue() << ":\n";
648 const char *JTEntryDirective = TAI->getJumpTableDirective();
649 if (!JTEntryDirective)
650 JTEntryDirective = TAI->getData32bitsDirective();
652 const MachineFunction *MF = MI->getParent()->getParent();
653 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
654 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
655 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
656 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
657 std::set<MachineBasicBlock*> JTSets;
658 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
659 MachineBasicBlock *MBB = JTBBs[i];
660 if (UseSet && JTSets.insert(MBB).second)
661 printSetLabel(JTI, MO2.getImmedValue(), MBB);
663 O << JTEntryDirective << ' ';
665 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
666 << '_' << JTI << '_' << MO2.getImmedValue()
667 << "_set_" << MBB->getNumber();
668 else if (TM.getRelocationModel() == Reloc::PIC_) {
669 printBasicBlockLabel(MBB, false, false);
670 // If the arch uses custom Jump Table directives, don't calc relative to JT
671 if (!TAI->getJumpTableDirective())
672 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
673 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImmedValue();
675 printBasicBlockLabel(MBB, false, false);
682 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
683 unsigned AsmVariant, const char *ExtraCode){
684 // Does this asm operand have a single letter operand modifier?
685 if (ExtraCode && ExtraCode[0]) {
686 if (ExtraCode[1] != 0) return true; // Unknown modifier.
688 switch (ExtraCode[0]) {
689 default: return true; // Unknown modifier.
690 case 'c': // Don't print "$" before a global var name or constant.
691 printOperand(MI, OpNo);
694 if (TM.getTargetData()->isLittleEndian())
698 if (TM.getTargetData()->isBigEndian())
701 case 'H': // Write second word of DI / DF reference.
702 // Verify that this operand has two consecutive registers.
703 if (!MI->getOperand(OpNo).isRegister() ||
704 OpNo+1 == MI->getNumOperands() ||
705 !MI->getOperand(OpNo+1).isRegister())
707 ++OpNo; // Return the high-part.
711 printOperand(MI, OpNo);
715 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
718 int Opc = MI->getOpcode();
720 case ARM::CONSTPOOL_ENTRY:
721 if (!InCPMode && AFI->isThumbFunction()) {
727 if (InCPMode && AFI->isThumbFunction())
740 // Call the autogenerated instruction printer routines.
741 printInstruction(MI);
744 bool ARMAsmPrinter::doInitialization(Module &M) {
745 if (Subtarget->isTargetDarwin()) {
746 // Emit initial debug information.
750 return AsmPrinter::doInitialization(M);
753 bool ARMAsmPrinter::doFinalization(Module &M) {
754 const TargetData *TD = TM.getTargetData();
756 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
758 if (!I->hasInitializer()) // External global require no code
761 if (EmitSpecialLLVMGlobal(I)) {
762 if (Subtarget->isTargetDarwin() &&
763 TM.getRelocationModel() == Reloc::Static) {
764 if (I->getName() == "llvm.global_ctors")
765 O << ".reference .constructors_used\n";
766 else if (I->getName() == "llvm.global_dtors")
767 O << ".reference .destructors_used\n";
772 std::string name = Mang->getValueName(I);
773 Constant *C = I->getInitializer();
774 const Type *Type = C->getType();
775 unsigned Size = TD->getTypeSize(Type);
776 unsigned Align = TD->getPreferredAlignmentLog(I);
778 if (I->hasHiddenVisibility())
779 if (const char *Directive = TAI->getHiddenDirective())
780 O << Directive << name << "\n";
781 if (Subtarget->isTargetELF())
782 O << "\t.type " << name << ",%object\n";
784 if (C->isNullValue()) {
785 if (I->hasExternalLinkage()) {
786 if (const char *Directive = TAI->getZeroFillDirective()) {
787 O << "\t.globl\t" << name << "\n";
788 O << Directive << "__DATA__, __common, " << name << ", "
789 << Size << ", " << Align << "\n";
794 if (!I->hasSection() &&
795 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
796 I->hasLinkOnceLinkage())) {
797 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
798 if (!NoZerosInBSS && TAI->getBSSSection())
799 SwitchToDataSection(TAI->getBSSSection(), I);
801 SwitchToDataSection(TAI->getDataSection(), I);
802 if (TAI->getLCOMMDirective() != NULL) {
803 if (I->hasInternalLinkage()) {
804 O << TAI->getLCOMMDirective() << name << "," << Size;
805 if (Subtarget->isTargetDarwin())
808 O << TAI->getCOMMDirective() << name << "," << Size;
810 if (I->hasInternalLinkage())
811 O << "\t.local\t" << name << "\n";
812 O << TAI->getCOMMDirective() << name << "," << Size;
813 if (TAI->getCOMMDirectiveTakesAlignment())
814 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
816 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
821 switch (I->getLinkage()) {
822 case GlobalValue::LinkOnceLinkage:
823 case GlobalValue::WeakLinkage:
824 if (Subtarget->isTargetDarwin()) {
825 O << "\t.globl " << name << "\n"
826 << "\t.weak_definition " << name << "\n";
827 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
829 std::string SectionName("\t.section\t.llvm.linkonce.d." +
831 ",\"aw\",%progbits");
832 SwitchToDataSection(SectionName.c_str(), I);
833 O << "\t.weak " << name << "\n";
836 case GlobalValue::AppendingLinkage:
837 // FIXME: appending linkage variables should go into a section of
838 // their name or something. For now, just emit them as external.
839 case GlobalValue::ExternalLinkage:
840 O << "\t.globl " << name << "\n";
842 case GlobalValue::InternalLinkage: {
843 if (I->isConstant()) {
844 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
845 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
846 SwitchToDataSection(TAI->getCStringSection(), I);
850 // FIXME: special handling for ".ctors" & ".dtors" sections
851 if (I->hasSection() &&
852 (I->getSection() == ".ctors" ||
853 I->getSection() == ".dtors")) {
854 assert(!Subtarget->isTargetDarwin());
855 std::string SectionName = ".section " + I->getSection();
856 SectionName += ",\"aw\",%progbits";
857 SwitchToDataSection(SectionName.c_str());
859 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
860 SwitchToDataSection(TAI->getBSSSection(), I);
861 else if (!I->isConstant())
862 SwitchToDataSection(TAI->getDataSection(), I);
865 bool HasReloc = C->ContainsRelocations();
867 Subtarget->isTargetDarwin() &&
868 TM.getRelocationModel() != Reloc::Static)
869 SwitchToDataSection("\t.const_data\n");
870 else if (!HasReloc && Size == 4 &&
871 TAI->getFourByteConstantSection())
872 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
873 else if (!HasReloc && Size == 8 &&
874 TAI->getEightByteConstantSection())
875 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
876 else if (!HasReloc && Size == 16 &&
877 TAI->getSixteenByteConstantSection())
878 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
879 else if (TAI->getReadOnlySection())
880 SwitchToDataSection(TAI->getReadOnlySection(), I);
882 SwitchToDataSection(TAI->getDataSection(), I);
889 assert(0 && "Unknown linkage type!");
893 EmitAlignment(Align, I);
894 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
896 if (TAI->hasDotTypeDotSizeDirective())
897 O << "\t.size " << name << ", " << Size << "\n";
898 // If the initializer is a extern weak symbol, remember to emit the weak
900 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
901 if (GV->hasExternalWeakLinkage())
902 ExtWeakSymbols.insert(GV);
904 EmitGlobalConstant(C);
908 if (Subtarget->isTargetDarwin()) {
909 SwitchToDataSection("");
911 // Output stubs for dynamically-linked functions
913 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
915 if (TM.getRelocationModel() == Reloc::PIC_)
916 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
919 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
923 O << "\t.code\t32\n";
925 O << "L" << *i << "$stub:\n";
926 O << "\t.indirect_symbol " << *i << "\n";
927 O << "\tldr ip, L" << *i << "$slp\n";
928 if (TM.getRelocationModel() == Reloc::PIC_) {
929 O << "L" << *i << "$scv:\n";
930 O << "\tadd ip, pc, ip\n";
932 O << "\tldr pc, [ip, #0]\n";
933 O << "L" << *i << "$slp:\n";
934 if (TM.getRelocationModel() == Reloc::PIC_)
935 O << "\t.long\tL" << *i << "$lazy_ptr-(L" << *i << "$scv+8)\n";
937 O << "\t.long\tL" << *i << "$lazy_ptr\n";
938 SwitchToDataSection(".lazy_symbol_pointer", 0);
939 O << "L" << *i << "$lazy_ptr:\n";
940 O << "\t.indirect_symbol " << *i << "\n";
941 O << "\t.long\tdyld_stub_binding_helper\n";
945 // Output non-lazy-pointers for external and common global variables.
946 if (GVNonLazyPtrs.begin() != GVNonLazyPtrs.end())
947 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
948 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
949 e = GVNonLazyPtrs.end(); i != e; ++i) {
950 O << "L" << *i << "$non_lazy_ptr:\n";
951 O << "\t.indirect_symbol " << *i << "\n";
955 // Emit initial debug information.
958 // Funny Darwin hack: This flag tells the linker that no global symbols
959 // contain code that falls through to other global symbols (e.g. the obvious
960 // implementation of multiple entry points). If this doesn't occur, the
961 // linker can safely perform dead code stripping. Since LLVM never
962 // generates code that does this, it is always safe to set.
963 O << "\t.subsections_via_symbols\n";
966 AsmPrinter::doFinalization(M);
967 return false; // success