1 //===-- AlphaAsmPrinter.cpp - Alpha 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 Alpha assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
17 #include "AlphaInstrInfo.h"
18 #include "AlphaTargetMachine.h"
19 #include "llvm/Module.h"
20 #include "llvm/MDNode.h"
21 #include "llvm/Type.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/Target/TargetAsmInfo.h"
26 #include "llvm/Target/TargetLoweringObjectFile.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Target/TargetRegistry.h"
29 #include "llvm/Support/Compiler.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/Mangler.h"
32 #include "llvm/Support/FormattedStream.h"
33 #include "llvm/ADT/Statistic.h"
36 STATISTIC(EmittedInsts, "Number of machine instrs printed");
39 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
40 /// Unique incrementer for label values for referencing Global values.
43 explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
44 const TargetAsmInfo *T, bool V)
45 : AsmPrinter(o, tm, T, V) {}
47 virtual const char *getPassName() const {
48 return "Alpha Assembly Printer";
50 bool printInstruction(const MachineInstr *MI);
51 void printOp(const MachineOperand &MO, bool IsCallOp = false);
52 void printOperand(const MachineInstr *MI, int opNum);
53 void printBaseOffsetPair(const MachineInstr *MI, int i, bool brackets=true);
54 void PrintGlobalVariable(const GlobalVariable *GVar);
55 bool runOnMachineFunction(MachineFunction &F);
56 bool doInitialization(Module &M);
58 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
59 unsigned AsmVariant, const char *ExtraCode);
60 bool PrintAsmMemoryOperand(const MachineInstr *MI,
63 const char *ExtraCode);
65 } // end of anonymous namespace
67 #include "AlphaGenAsmWriter.inc"
69 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
71 const MachineOperand &MO = MI->getOperand(opNum);
72 if (MO.getType() == MachineOperand::MO_Register) {
73 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
75 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
76 } else if (MO.isImm()) {
78 assert(MO.getImm() < (1 << 30));
85 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
86 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
88 switch (MO.getType()) {
89 case MachineOperand::MO_Register:
90 O << RI.get(MO.getReg()).AsmName;
93 case MachineOperand::MO_Immediate:
94 llvm_unreachable("printOp() does not handle immediate values");
97 case MachineOperand::MO_MachineBasicBlock:
98 printBasicBlockLabel(MO.getMBB());
101 case MachineOperand::MO_ConstantPoolIndex:
102 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
106 case MachineOperand::MO_ExternalSymbol:
107 O << MO.getSymbolName();
110 case MachineOperand::MO_GlobalAddress:
111 O << Mang->getMangledName(MO.getGlobal());
114 case MachineOperand::MO_JumpTableIndex:
115 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
116 << '_' << MO.getIndex();
120 O << "<unknown operand type: " << MO.getType() << ">";
125 /// runOnMachineFunction - This uses the printMachineInstruction()
126 /// method to print assembly for each instruction.
128 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
131 SetupMachineFunction(MF);
134 // Print out constants referenced by the function
135 EmitConstantPool(MF.getConstantPool());
137 // Print out jump tables referenced by the function
138 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
140 // Print out labels for the function.
141 const Function *F = MF.getFunction();
142 SwitchToSection(getObjFileLowering().SectionForGlobal(F, TM));
144 EmitAlignment(MF.getAlignment(), F);
145 switch (F->getLinkage()) {
146 default: llvm_unreachable("Unknown linkage type!");
147 case Function::InternalLinkage: // Symbols default to internal.
148 case Function::PrivateLinkage:
149 case Function::LinkerPrivateLinkage:
151 case Function::ExternalLinkage:
152 O << "\t.globl " << CurrentFnName << "\n";
154 case Function::WeakAnyLinkage:
155 case Function::WeakODRLinkage:
156 case Function::LinkOnceAnyLinkage:
157 case Function::LinkOnceODRLinkage:
158 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
162 printVisibility(CurrentFnName, F->getVisibility());
164 O << "\t.ent " << CurrentFnName << "\n";
166 O << CurrentFnName << ":\n";
168 // Print out code for the function.
169 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
171 if (I != MF.begin()) {
172 printBasicBlockLabel(I, true, true);
175 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
177 // Print the assembly for the instruction.
179 if (!printInstruction(II)) {
180 llvm_unreachable("Unhandled instruction in asm writer!");
185 O << "\t.end " << CurrentFnName << "\n";
187 // We didn't modify anything.
191 bool AlphaAsmPrinter::doInitialization(Module &M)
193 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
194 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
196 O << "\t.arch ev6\n";
197 O << "\t.set noat\n";
198 return AsmPrinter::doInitialization(M);
201 void AlphaAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
202 const TargetData *TD = TM.getTargetData();
204 if (!GVar->hasInitializer()) return; // External global require no code
206 // Check to see if this is a special global used by LLVM, if so, emit it.
207 if (EmitSpecialLLVMGlobal(GVar))
210 std::string name = Mang->getMangledName(GVar);
211 Constant *C = GVar->getInitializer();
212 if (isa<MDNode>(C) || isa<MDString>(C))
214 unsigned Size = TD->getTypeAllocSize(C->getType());
215 unsigned Align = TD->getPreferredAlignmentLog(GVar);
217 // 0: Switch to section
218 SwitchToSection(getObjFileLowering().SectionForGlobal(GVar, TM));
220 // 1: Check visibility
221 printVisibility(name, GVar->getVisibility());
224 switch (GVar->getLinkage()) {
225 case GlobalValue::LinkOnceAnyLinkage:
226 case GlobalValue::LinkOnceODRLinkage:
227 case GlobalValue::WeakAnyLinkage:
228 case GlobalValue::WeakODRLinkage:
229 case GlobalValue::CommonLinkage:
230 O << TAI->getWeakRefDirective() << name << '\n';
232 case GlobalValue::AppendingLinkage:
233 case GlobalValue::ExternalLinkage:
234 O << TAI->getGlobalDirective() << name << "\n";
236 case GlobalValue::InternalLinkage:
237 case GlobalValue::PrivateLinkage:
238 case GlobalValue::LinkerPrivateLinkage:
241 llvm_unreachable("Unknown linkage type!");
244 // 3: Type, Size, Align
245 if (TAI->hasDotTypeDotSizeDirective()) {
246 O << "\t.type\t" << name << ", @object\n";
247 O << "\t.size\t" << name << ", " << Size << "\n";
250 EmitAlignment(Align, GVar);
254 EmitGlobalConstant(C);
258 /// PrintAsmOperand - Print out an operand for an inline asm expression.
260 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
262 const char *ExtraCode) {
263 printOperand(MI, OpNo);
267 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
270 const char *ExtraCode) {
271 if (ExtraCode && ExtraCode[0])
272 return true; // Unknown modifier.
274 printOperand(MI, OpNo);
279 // Force static initialization.
280 extern "C" void LLVMInitializeAlphaAsmPrinter() {
281 RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);