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/Statistic.h"
33 #include "llvm/ADT/StringExtras.h"
34 #include "llvm/ADT/StringSet.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 StringSet<> GVNonLazyPtrs;
78 /// HiddenGVNonLazyPtrs - Keeps the set of GlobalValues with hidden
79 /// visibility that require non-lazy-pointers for indirect access.
80 StringSet<> HiddenGVNonLazyPtrs;
82 /// FnStubs - Keeps the set of external function GlobalAddresses that the
83 /// asm printer should generate stubs for.
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 if (GV->hasHiddenVisibility())
145 HiddenGVNonLazyPtrs.insert(Name);
147 GVNonLazyPtrs.insert(Name);
148 printSuffixedName(Name, "$non_lazy_ptr");
149 } else if (ACPV->isStub()) {
150 FnStubs.insert(Name);
151 printSuffixedName(Name, "$stub");
154 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
155 if (ACPV->getPCAdjustment() != 0) {
156 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
157 << utostr(ACPV->getLabelId())
158 << "+" << (unsigned)ACPV->getPCAdjustment();
159 if (ACPV->mustAddCurrentAddress())
165 // If the constant pool value is a extern weak symbol, remember to emit
166 // the weak reference.
167 if (GV && GV->hasExternalWeakLinkage())
168 ExtWeakSymbols.insert(GV);
171 void getAnalysisUsage(AnalysisUsage &AU) const {
172 AsmPrinter::getAnalysisUsage(AU);
173 AU.setPreservesAll();
174 AU.addRequired<MachineModuleInfo>();
177 } // end of anonymous namespace
179 #include "ARMGenAsmWriter.inc"
181 /// runOnMachineFunction - This uses the printInstruction()
182 /// method to print assembly for each instruction.
184 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
185 AFI = MF.getInfo<ARMFunctionInfo>();
186 MCP = MF.getConstantPool();
188 SetupMachineFunction(MF);
191 // NOTE: we don't print out constant pools here, they are handled as
195 // Print out labels for the function.
196 const Function *F = MF.getFunction();
197 switch (F->getLinkage()) {
198 default: assert(0 && "Unknown linkage type!");
199 case Function::InternalLinkage:
200 SwitchToTextSection("\t.text", F);
202 case Function::ExternalLinkage:
203 SwitchToTextSection("\t.text", F);
204 O << "\t.globl\t" << CurrentFnName << "\n";
206 case Function::WeakLinkage:
207 case Function::LinkOnceLinkage:
208 if (Subtarget->isTargetDarwin()) {
210 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
211 O << "\t.globl\t" << CurrentFnName << "\n";
212 O << "\t.weak_definition\t" << CurrentFnName << "\n";
214 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
219 printVisibility(CurrentFnName, F->getVisibility());
221 if (AFI->isThumbFunction()) {
222 EmitAlignment(1, F, AFI->getAlign());
223 O << "\t.code\t16\n";
224 O << "\t.thumb_func";
225 if (Subtarget->isTargetDarwin())
226 O << "\t" << CurrentFnName;
232 O << CurrentFnName << ":\n";
233 // Emit pre-function debug information.
234 // FIXME: Dwarf support.
235 //DW.BeginFunction(&MF);
237 if (Subtarget->isTargetDarwin()) {
238 // If the function is empty, then we need to emit *something*. Otherwise,
239 // the function's label might be associated with something that it wasn't
240 // meant to be associated with. We emit a noop in this situation.
241 MachineFunction::iterator I = MF.begin();
243 if (++I == MF.end() && MF.front().empty())
247 // Print out code for the function.
248 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
250 // Print a label for the basic block.
251 if (I != MF.begin()) {
252 printBasicBlockLabel(I, true, true);
255 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
257 // Print the assembly for the instruction.
258 printMachineInstruction(II);
262 if (TAI->hasDotTypeDotSizeDirective())
263 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
265 // Emit post-function debug information.
266 // FIXME: Dwarf support.
274 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
275 const char *Modifier) {
276 const MachineOperand &MO = MI->getOperand(opNum);
277 switch (MO.getType()) {
278 case MachineOperand::MO_Register:
279 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
280 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
282 assert(0 && "not implemented");
284 case MachineOperand::MO_Immediate: {
285 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
288 O << (int)MO.getImm();
291 case MachineOperand::MO_MachineBasicBlock:
292 printBasicBlockLabel(MO.getMBB());
294 case MachineOperand::MO_GlobalAddress: {
295 bool isCallOp = Modifier && !strcmp(Modifier, "call");
296 GlobalValue *GV = MO.getGlobal();
297 std::string Name = Mang->getValueName(GV);
298 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
299 GV->hasLinkOnceLinkage());
300 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
301 TM.getRelocationModel() != Reloc::Static) {
302 printSuffixedName(Name, "$stub");
303 FnStubs.insert(Name);
307 printOffset(MO.getOffset());
309 if (isCallOp && Subtarget->isTargetELF() &&
310 TM.getRelocationModel() == Reloc::PIC_)
312 if (GV->hasExternalWeakLinkage())
313 ExtWeakSymbols.insert(GV);
316 case MachineOperand::MO_ExternalSymbol: {
317 bool isCallOp = Modifier && !strcmp(Modifier, "call");
318 std::string Name(TAI->getGlobalPrefix());
319 Name += MO.getSymbolName();
320 if (isCallOp && Subtarget->isTargetDarwin() &&
321 TM.getRelocationModel() != Reloc::Static) {
322 printSuffixedName(Name, "$stub");
323 FnStubs.insert(Name);
326 if (isCallOp && Subtarget->isTargetELF() &&
327 TM.getRelocationModel() == Reloc::PIC_)
331 case MachineOperand::MO_ConstantPoolIndex:
332 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
333 << '_' << MO.getIndex();
335 case MachineOperand::MO_JumpTableIndex:
336 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
337 << '_' << MO.getIndex();
340 O << "<unknown operand type>"; abort (); break;
344 static void printSOImm(raw_ostream &O, int64_t V, const TargetAsmInfo *TAI) {
345 assert(V < (1 << 12) && "Not a valid so_imm value!");
346 unsigned Imm = ARM_AM::getSOImmValImm(V);
347 unsigned Rot = ARM_AM::getSOImmValRot(V);
349 // Print low-level immediate formation info, per
350 // A5.1.3: "Data-processing operands - Immediate".
352 O << "#" << Imm << ", " << Rot;
353 // Pretty printed version.
354 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
360 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
361 /// immediate in bits 0-7.
362 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
363 const MachineOperand &MO = MI->getOperand(OpNum);
364 assert(MO.isImm() && "Not a valid so_imm value!");
365 printSOImm(O, MO.getImm(), TAI);
368 /// printSOImm2PartOperand - SOImm is broken into two pieces using a 'mov'
369 /// followed by an 'orr' to materialize.
370 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
371 const MachineOperand &MO = MI->getOperand(OpNum);
372 assert(MO.isImm() && "Not a valid so_imm value!");
373 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
374 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
375 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
377 printPredicateOperand(MI, 2);
383 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
386 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
387 // "Addressing Mode 1 - Data-processing operands" forms. This includes:
389 // REG REG 0,SH_OPC - e.g. R5, ROR R3
390 // REG 0 IMM,SH_OPC - e.g. R5, LSL #3
391 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
392 const MachineOperand &MO1 = MI->getOperand(Op);
393 const MachineOperand &MO2 = MI->getOperand(Op+1);
394 const MachineOperand &MO3 = MI->getOperand(Op+2);
396 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
397 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
399 // Print the shift opc.
401 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
405 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
406 O << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
407 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
409 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
413 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
414 const MachineOperand &MO1 = MI->getOperand(Op);
415 const MachineOperand &MO2 = MI->getOperand(Op+1);
416 const MachineOperand &MO3 = MI->getOperand(Op+2);
418 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
419 printOperand(MI, Op);
423 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
426 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
428 << (char)ARM_AM::getAM2Op(MO3.getImm())
429 << ARM_AM::getAM2Offset(MO3.getImm());
435 << (char)ARM_AM::getAM2Op(MO3.getImm())
436 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
438 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
440 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
445 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
446 const MachineOperand &MO1 = MI->getOperand(Op);
447 const MachineOperand &MO2 = MI->getOperand(Op+1);
450 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
451 assert(ImmOffs && "Malformed indexed load / store!");
453 << (char)ARM_AM::getAM2Op(MO2.getImm())
458 O << (char)ARM_AM::getAM2Op(MO2.getImm())
459 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
461 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
463 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
467 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
468 const MachineOperand &MO1 = MI->getOperand(Op);
469 const MachineOperand &MO2 = MI->getOperand(Op+1);
470 const MachineOperand &MO3 = MI->getOperand(Op+2);
472 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
473 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
477 << (char)ARM_AM::getAM3Op(MO3.getImm())
478 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName
483 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
485 << (char)ARM_AM::getAM3Op(MO3.getImm())
490 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
491 const MachineOperand &MO1 = MI->getOperand(Op);
492 const MachineOperand &MO2 = MI->getOperand(Op+1);
495 O << (char)ARM_AM::getAM3Op(MO2.getImm())
496 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
500 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
501 assert(ImmOffs && "Malformed indexed load / store!");
503 << (char)ARM_AM::getAM3Op(MO2.getImm())
507 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
508 const char *Modifier) {
509 const MachineOperand &MO1 = MI->getOperand(Op);
510 const MachineOperand &MO2 = MI->getOperand(Op+1);
511 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
512 if (Modifier && strcmp(Modifier, "submode") == 0) {
513 if (MO1.getReg() == ARM::SP) {
514 bool isLDM = (MI->getOpcode() == ARM::LDM ||
515 MI->getOpcode() == ARM::LDM_RET);
516 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
518 O << ARM_AM::getAMSubModeStr(Mode);
520 printOperand(MI, Op);
521 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
526 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
527 const char *Modifier) {
528 const MachineOperand &MO1 = MI->getOperand(Op);
529 const MachineOperand &MO2 = MI->getOperand(Op+1);
531 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
532 printOperand(MI, Op);
536 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
538 if (Modifier && strcmp(Modifier, "submode") == 0) {
539 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
540 if (MO1.getReg() == ARM::SP) {
541 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
542 MI->getOpcode() == ARM::FLDMS);
543 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
545 O << ARM_AM::getAMSubModeStr(Mode);
547 } else if (Modifier && strcmp(Modifier, "base") == 0) {
548 // Used for FSTM{D|S} and LSTM{D|S} operations.
549 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
550 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
555 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
557 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
559 << (char)ARM_AM::getAM5Op(MO2.getImm())
565 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
566 const char *Modifier) {
567 if (Modifier && strcmp(Modifier, "label") == 0) {
568 printPCLabel(MI, Op+1);
572 const MachineOperand &MO1 = MI->getOperand(Op);
573 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
574 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName << "]";
578 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
579 const MachineOperand &MO1 = MI->getOperand(Op);
580 const MachineOperand &MO2 = MI->getOperand(Op+1);
581 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
582 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).AsmName << "]";
586 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
588 const MachineOperand &MO1 = MI->getOperand(Op);
589 const MachineOperand &MO2 = MI->getOperand(Op+1);
590 const MachineOperand &MO3 = MI->getOperand(Op+2);
592 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
593 printOperand(MI, Op);
597 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
599 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).AsmName;
600 else if (unsigned ImmOffs = MO2.getImm()) {
601 O << ", #" << ImmOffs;
609 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
610 printThumbAddrModeRI5Operand(MI, Op, 1);
613 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
614 printThumbAddrModeRI5Operand(MI, Op, 2);
617 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
618 printThumbAddrModeRI5Operand(MI, Op, 4);
621 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
622 const MachineOperand &MO1 = MI->getOperand(Op);
623 const MachineOperand &MO2 = MI->getOperand(Op+1);
624 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
625 if (unsigned ImmOffs = MO2.getImm())
626 O << ", #" << ImmOffs << " * 4";
630 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int opNum) {
631 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(opNum).getImm();
633 O << ARMCondCodeToString(CC);
636 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int opNum){
637 unsigned Reg = MI->getOperand(opNum).getReg();
639 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
644 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
645 int Id = (int)MI->getOperand(opNum).getImm();
646 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
649 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
651 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
653 if (i != e-1) O << ", ";
658 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
659 const char *Modifier) {
660 assert(Modifier && "This operand only works with a modifier!");
661 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
663 if (!strcmp(Modifier, "label")) {
664 unsigned ID = MI->getOperand(OpNo).getImm();
665 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
666 << '_' << ID << ":\n";
668 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
669 unsigned CPI = MI->getOperand(OpNo).getIndex();
671 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
673 if (MCPE.isMachineConstantPoolEntry()) {
674 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
676 EmitGlobalConstant(MCPE.Val.ConstVal);
677 // remember to emit the weak reference
678 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
679 if (GV->hasExternalWeakLinkage())
680 ExtWeakSymbols.insert(GV);
685 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
686 const MachineOperand &MO1 = MI->getOperand(OpNo);
687 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
688 unsigned JTI = MO1.getIndex();
689 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
690 << '_' << JTI << '_' << MO2.getImm() << ":\n";
692 const char *JTEntryDirective = TAI->getJumpTableDirective();
693 if (!JTEntryDirective)
694 JTEntryDirective = TAI->getData32bitsDirective();
696 const MachineFunction *MF = MI->getParent()->getParent();
697 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
698 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
699 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
700 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
701 std::set<MachineBasicBlock*> JTSets;
702 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
703 MachineBasicBlock *MBB = JTBBs[i];
704 if (UseSet && JTSets.insert(MBB).second)
705 printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
707 O << JTEntryDirective << ' ';
709 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
710 << '_' << JTI << '_' << MO2.getImm()
711 << "_set_" << MBB->getNumber();
712 else if (TM.getRelocationModel() == Reloc::PIC_) {
713 printBasicBlockLabel(MBB, false, false, false);
714 // If the arch uses custom Jump Table directives, don't calc relative to JT
715 if (!TAI->getJumpTableDirective())
716 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
717 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
719 printBasicBlockLabel(MBB, false, false, false);
726 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
727 unsigned AsmVariant, const char *ExtraCode){
728 // Does this asm operand have a single letter operand modifier?
729 if (ExtraCode && ExtraCode[0]) {
730 if (ExtraCode[1] != 0) return true; // Unknown modifier.
732 switch (ExtraCode[0]) {
733 default: return true; // Unknown modifier.
734 case 'c': // Don't print "$" before a global var name or constant.
735 case 'P': // Print a VFP double precision register.
736 printOperand(MI, OpNo);
739 if (TM.getTargetData()->isLittleEndian())
743 if (TM.getTargetData()->isBigEndian())
746 case 'H': // Write second word of DI / DF reference.
747 // Verify that this operand has two consecutive registers.
748 if (!MI->getOperand(OpNo).isReg() ||
749 OpNo+1 == MI->getNumOperands() ||
750 !MI->getOperand(OpNo+1).isReg())
752 ++OpNo; // Return the high-part.
756 printOperand(MI, OpNo);
760 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
763 int Opc = MI->getOpcode();
765 case ARM::CONSTPOOL_ENTRY:
766 if (!InCPMode && AFI->isThumbFunction()) {
772 if (InCPMode && AFI->isThumbFunction())
776 // Call the autogenerated instruction printer routines.
777 printInstruction(MI);
780 bool ARMAsmPrinter::doInitialization(Module &M) {
781 // Emit initial debug information.
782 // FIXME: Dwarf support.
783 //DW.BeginModule(&M);
785 bool Result = AsmPrinter::doInitialization(M);
787 // AsmPrinter::doInitialization should have done this analysis.
788 MMI = getAnalysisToUpdate<MachineModuleInfo>();
790 // FIXME: Dwarf support.
791 //DW.SetModuleInfo(MMI);
793 // Darwin wants symbols to be quoted if they have complex names.
794 if (Subtarget->isTargetDarwin())
795 Mang->setUseQuotes(true);
800 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
801 /// Don't print things like \n or \0.
802 static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
803 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
809 void ARMAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
810 const TargetData *TD = TM.getTargetData();
812 if (!GVar->hasInitializer()) // External global require no code
815 // Check to see if this is a special global used by LLVM, if so, emit it.
817 if (EmitSpecialLLVMGlobal(GVar)) {
818 if (Subtarget->isTargetDarwin() &&
819 TM.getRelocationModel() == Reloc::Static) {
820 if (GVar->getName() == "llvm.global_ctors")
821 O << ".reference .constructors_used\n";
822 else if (GVar->getName() == "llvm.global_dtors")
823 O << ".reference .destructors_used\n";
828 std::string name = Mang->getValueName(GVar);
829 Constant *C = GVar->getInitializer();
830 const Type *Type = C->getType();
831 unsigned Size = TD->getABITypeSize(Type);
832 unsigned Align = TD->getPreferredAlignmentLog(GVar);
833 bool isDarwin = Subtarget->isTargetDarwin();
835 printVisibility(name, GVar->getVisibility());
837 if (Subtarget->isTargetELF())
838 O << "\t.type " << name << ",%object\n";
840 if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal()) {
841 // FIXME: This seems to be pretty darwin-specific
843 if (GVar->hasExternalLinkage()) {
844 SwitchToSection(TAI->SectionForGlobal(GVar));
845 if (const char *Directive = TAI->getZeroFillDirective()) {
846 O << "\t.globl\t" << name << "\n";
847 O << Directive << "__DATA, __common, " << name << ", "
848 << Size << ", " << Align << "\n";
853 if (GVar->hasInternalLinkage() || GVar->mayBeOverridden()) {
854 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
857 if (GVar->hasInternalLinkage()) {
858 O << TAI->getLCOMMDirective() << name << "," << Size
860 } else if (GVar->hasCommonLinkage()) {
861 O << TAI->getCOMMDirective() << name << "," << Size
864 SwitchToSection(TAI->SectionForGlobal(GVar));
865 O << "\t.globl " << name << '\n'
866 << TAI->getWeakDefDirective() << name << '\n';
867 EmitAlignment(Align, GVar);
868 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
869 PrintUnmangledNameSafely(GVar, O);
871 EmitGlobalConstant(C);
874 } else if (TAI->getLCOMMDirective() != NULL) {
875 if (GVar->hasInternalLinkage()) {
876 O << TAI->getLCOMMDirective() << name << "," << Size;
878 O << TAI->getCOMMDirective() << name << "," << Size;
879 if (TAI->getCOMMDirectiveTakesAlignment())
880 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
883 SwitchToSection(TAI->SectionForGlobal(GVar));
884 if (GVar->hasInternalLinkage())
885 O << "\t.local\t" << name << "\n";
886 O << TAI->getCOMMDirective() << name << "," << Size;
887 if (TAI->getCOMMDirectiveTakesAlignment())
888 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
890 O << "\t\t" << TAI->getCommentString() << " ";
891 PrintUnmangledNameSafely(GVar, O);
897 SwitchToSection(TAI->SectionForGlobal(GVar));
898 switch (GVar->getLinkage()) {
899 case GlobalValue::CommonLinkage:
900 case GlobalValue::LinkOnceLinkage:
901 case GlobalValue::WeakLinkage:
903 O << "\t.globl " << name << "\n"
904 << "\t.weak_definition " << name << "\n";
906 O << "\t.weak " << name << "\n";
909 case GlobalValue::AppendingLinkage:
910 // FIXME: appending linkage variables should go into a section of
911 // their name or something. For now, just emit them as external.
912 case GlobalValue::ExternalLinkage:
913 O << "\t.globl " << name << "\n";
915 case GlobalValue::InternalLinkage:
918 assert(0 && "Unknown linkage type!");
922 EmitAlignment(Align, GVar);
923 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
924 PrintUnmangledNameSafely(GVar, O);
926 if (TAI->hasDotTypeDotSizeDirective())
927 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);
940 bool ARMAsmPrinter::doFinalization(Module &M) {
941 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
943 printModuleLevelGV(I);
945 if (Subtarget->isTargetDarwin()) {
946 SwitchToDataSection("");
948 // Output stubs for dynamically-linked functions
949 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
951 if (TM.getRelocationModel() == Reloc::PIC_)
952 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
955 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
959 O << "\t.code\t32\n";
961 const char *p = i->getKeyData();
962 printSuffixedName(p, "$stub");
964 O << "\t.indirect_symbol " << p << "\n";
966 printSuffixedName(p, "$slp");
968 if (TM.getRelocationModel() == Reloc::PIC_) {
969 printSuffixedName(p, "$scv");
971 O << "\tadd ip, pc, ip\n";
973 O << "\tldr pc, [ip, #0]\n";
974 printSuffixedName(p, "$slp");
977 printSuffixedName(p, "$lazy_ptr");
978 if (TM.getRelocationModel() == Reloc::PIC_) {
980 printSuffixedName(p, "$scv");
984 SwitchToDataSection(".lazy_symbol_pointer", 0);
985 printSuffixedName(p, "$lazy_ptr");
987 O << "\t.indirect_symbol " << p << "\n";
988 O << "\t.long\tdyld_stub_binding_helper\n";
992 // Output non-lazy-pointers for external and common global variables.
993 if (!GVNonLazyPtrs.empty()) {
994 SwitchToDataSection("\t.non_lazy_symbol_pointer", 0);
995 for (StringSet<>::iterator i = GVNonLazyPtrs.begin(),
996 e = GVNonLazyPtrs.end(); i != e; ++i) {
997 const char *p = i->getKeyData();
998 printSuffixedName(p, "$non_lazy_ptr");
1000 O << "\t.indirect_symbol " << p << "\n";
1001 O << "\t.long\t0\n";
1005 if (!HiddenGVNonLazyPtrs.empty()) {
1006 SwitchToSection(TAI->getDataSection());
1007 for (StringSet<>::iterator i = HiddenGVNonLazyPtrs.begin(),
1008 e = HiddenGVNonLazyPtrs.end(); i != e; ++i) {
1009 const char *p = i->getKeyData();
1011 printSuffixedName(p, "$non_lazy_ptr");
1013 O << "\t.long " << p << "\n";
1018 // Emit initial debug information.
1019 // FIXME: Dwarf support.
1022 // Funny Darwin hack: This flag tells the linker that no global symbols
1023 // contain code that falls through to other global symbols (e.g. the obvious
1024 // implementation of multiple entry points). If this doesn't occur, the
1025 // linker can safely perform dead code stripping. Since LLVM never
1026 // generates code that does this, it is always safe to set.
1027 O << "\t.subsections_via_symbols\n";
1029 // Emit final debug information for ELF.
1030 // FIXME: Dwarf support.
1034 return AsmPrinter::doFinalization(M);
1037 /// createARMCodePrinterPass - Returns a pass that prints the ARM
1038 /// assembly code for a MachineFunction to the given output stream,
1039 /// using the given target machine description. This should work
1040 /// regardless of whether the function is in SSA form.
1042 FunctionPass *llvm::createARMCodePrinterPass(raw_ostream &o,
1043 ARMTargetMachine &tm) {
1044 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
1048 static struct Register {
1050 ARMTargetMachine::registerAsmPrinter(createARMCodePrinterPass);