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/TargetMachine.h"
27 #include "llvm/Target/TargetRegistry.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/Mangler.h"
31 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/ADT/Statistic.h"
35 STATISTIC(EmittedInsts, "Number of machine instrs printed");
38 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
39 /// Unique incrementer for label values for referencing Global values.
42 explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
43 const TargetAsmInfo *T, bool V)
44 : AsmPrinter(o, tm, T, V) {}
46 virtual const char *getPassName() const {
47 return "Alpha Assembly Printer";
49 bool printInstruction(const MachineInstr *MI);
50 void printOp(const MachineOperand &MO, bool IsCallOp = false);
51 void printOperand(const MachineInstr *MI, int opNum);
52 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
53 void printModuleLevelGV(const GlobalVariable* GVar);
54 bool runOnMachineFunction(MachineFunction &F);
55 bool doInitialization(Module &M);
56 bool doFinalization(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 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
68 /// assembly code for a MachineFunction to the given output stream,
69 /// using the given target machine description. This should work
70 /// regardless of whether the function is in SSA form.
72 FunctionPass *llvm::createAlphaCodePrinterPass(formatted_raw_ostream &o,
75 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
78 #include "AlphaGenAsmWriter.inc"
80 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
82 const MachineOperand &MO = MI->getOperand(opNum);
83 if (MO.getType() == MachineOperand::MO_Register) {
84 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
86 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
87 } else if (MO.isImm()) {
89 assert(MO.getImm() < (1 << 30));
96 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
97 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
99 switch (MO.getType()) {
100 case MachineOperand::MO_Register:
101 O << RI.get(MO.getReg()).AsmName;
104 case MachineOperand::MO_Immediate:
105 llvm_unreachable("printOp() does not handle immediate values");
108 case MachineOperand::MO_MachineBasicBlock:
109 printBasicBlockLabel(MO.getMBB());
112 case MachineOperand::MO_ConstantPoolIndex:
113 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
117 case MachineOperand::MO_ExternalSymbol:
118 O << MO.getSymbolName();
121 case MachineOperand::MO_GlobalAddress:
122 O << Mang->getMangledName(MO.getGlobal());
125 case MachineOperand::MO_JumpTableIndex:
126 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
127 << '_' << MO.getIndex();
131 O << "<unknown operand type: " << MO.getType() << ">";
136 /// runOnMachineFunction - This uses the printMachineInstruction()
137 /// method to print assembly for each instruction.
139 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
142 SetupMachineFunction(MF);
145 // Print out constants referenced by the function
146 EmitConstantPool(MF.getConstantPool());
148 // Print out jump tables referenced by the function
149 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
151 // Print out labels for the function.
152 const Function *F = MF.getFunction();
153 SwitchToSection(TAI->SectionForGlobal(F));
155 EmitAlignment(MF.getAlignment(), F);
156 switch (F->getLinkage()) {
157 default: llvm_unreachable("Unknown linkage type!");
158 case Function::InternalLinkage: // Symbols default to internal.
159 case Function::PrivateLinkage:
160 case Function::LinkerPrivateLinkage:
162 case Function::ExternalLinkage:
163 O << "\t.globl " << CurrentFnName << "\n";
165 case Function::WeakAnyLinkage:
166 case Function::WeakODRLinkage:
167 case Function::LinkOnceAnyLinkage:
168 case Function::LinkOnceODRLinkage:
169 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
173 printVisibility(CurrentFnName, F->getVisibility());
175 O << "\t.ent " << CurrentFnName << "\n";
177 O << CurrentFnName << ":\n";
179 // Print out code for the function.
180 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
182 if (I != MF.begin()) {
183 printBasicBlockLabel(I, true, true);
186 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
188 // Print the assembly for the instruction.
190 if (!printInstruction(II)) {
191 llvm_unreachable("Unhandled instruction in asm writer!");
196 O << "\t.end " << CurrentFnName << "\n";
198 // We didn't modify anything.
202 bool AlphaAsmPrinter::doInitialization(Module &M)
204 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
205 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
207 O << "\t.arch ev6\n";
208 O << "\t.set noat\n";
209 return AsmPrinter::doInitialization(M);
212 void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
213 const TargetData *TD = TM.getTargetData();
215 if (!GVar->hasInitializer()) return; // External global require no code
217 // Check to see if this is a special global used by LLVM, if so, emit it.
218 if (EmitSpecialLLVMGlobal(GVar))
221 std::string name = Mang->getMangledName(GVar);
222 Constant *C = GVar->getInitializer();
223 if (isa<MDNode>(C) || isa<MDString>(C))
225 unsigned Size = TD->getTypeAllocSize(C->getType());
226 unsigned Align = TD->getPreferredAlignmentLog(GVar);
228 // 0: Switch to section
229 SwitchToSection(TAI->SectionForGlobal(GVar));
231 // 1: Check visibility
232 printVisibility(name, GVar->getVisibility());
235 switch (GVar->getLinkage()) {
236 case GlobalValue::LinkOnceAnyLinkage:
237 case GlobalValue::LinkOnceODRLinkage:
238 case GlobalValue::WeakAnyLinkage:
239 case GlobalValue::WeakODRLinkage:
240 case GlobalValue::CommonLinkage:
241 O << TAI->getWeakRefDirective() << name << '\n';
243 case GlobalValue::AppendingLinkage:
244 case GlobalValue::ExternalLinkage:
245 O << TAI->getGlobalDirective() << name << "\n";
247 case GlobalValue::InternalLinkage:
248 case GlobalValue::PrivateLinkage:
249 case GlobalValue::LinkerPrivateLinkage:
252 llvm_unreachable("Unknown linkage type!");
255 // 3: Type, Size, Align
256 if (TAI->hasDotTypeDotSizeDirective()) {
257 O << "\t.type\t" << name << ", @object\n";
258 O << "\t.size\t" << name << ", " << Size << "\n";
261 EmitAlignment(Align, GVar);
265 EmitGlobalConstant(C);
269 bool AlphaAsmPrinter::doFinalization(Module &M) {
270 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
272 printModuleLevelGV(I);
274 return AsmPrinter::doFinalization(M);
277 /// PrintAsmOperand - Print out an operand for an inline asm expression.
279 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
281 const char *ExtraCode) {
282 printOperand(MI, OpNo);
286 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
289 const char *ExtraCode) {
290 if (ExtraCode && ExtraCode[0])
291 return true; // Unknown modifier.
293 printOperand(MI, OpNo);
298 // Force static initialization.
299 extern "C" void LLVMInitializeAlphaAsmPrinter() {
300 TargetRegistry::RegisterAsmPrinter(TheAlphaTarget,
301 createAlphaCodePrinterPass);