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:
161 case Function::ExternalLinkage:
162 O << "\t.globl " << CurrentFnName << "\n";
164 case Function::WeakAnyLinkage:
165 case Function::WeakODRLinkage:
166 case Function::LinkOnceAnyLinkage:
167 case Function::LinkOnceODRLinkage:
168 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
172 printVisibility(CurrentFnName, F->getVisibility());
174 O << "\t.ent " << CurrentFnName << "\n";
176 O << CurrentFnName << ":\n";
178 // Print out code for the function.
179 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
181 if (I != MF.begin()) {
182 printBasicBlockLabel(I, true, true);
185 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
187 // Print the assembly for the instruction.
189 if (!printInstruction(II)) {
190 llvm_unreachable("Unhandled instruction in asm writer!");
195 O << "\t.end " << CurrentFnName << "\n";
197 // We didn't modify anything.
201 bool AlphaAsmPrinter::doInitialization(Module &M)
203 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
204 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
206 O << "\t.arch ev6\n";
207 O << "\t.set noat\n";
208 return AsmPrinter::doInitialization(M);
211 void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
212 const TargetData *TD = TM.getTargetData();
214 if (!GVar->hasInitializer()) return; // External global require no code
216 // Check to see if this is a special global used by LLVM, if so, emit it.
217 if (EmitSpecialLLVMGlobal(GVar))
220 std::string name = Mang->getMangledName(GVar);
221 Constant *C = GVar->getInitializer();
222 if (isa<MDNode>(C) || isa<MDString>(C))
224 unsigned Size = TD->getTypeAllocSize(C->getType());
225 unsigned Align = TD->getPreferredAlignmentLog(GVar);
227 // 0: Switch to section
228 SwitchToSection(TAI->SectionForGlobal(GVar));
230 // 1: Check visibility
231 printVisibility(name, GVar->getVisibility());
234 switch (GVar->getLinkage()) {
235 case GlobalValue::LinkOnceAnyLinkage:
236 case GlobalValue::LinkOnceODRLinkage:
237 case GlobalValue::WeakAnyLinkage:
238 case GlobalValue::WeakODRLinkage:
239 case GlobalValue::CommonLinkage:
240 O << TAI->getWeakRefDirective() << name << '\n';
242 case GlobalValue::AppendingLinkage:
243 case GlobalValue::ExternalLinkage:
244 O << TAI->getGlobalDirective() << name << "\n";
246 case GlobalValue::InternalLinkage:
247 case GlobalValue::PrivateLinkage:
250 llvm_unreachable("Unknown linkage type!");
253 // 3: Type, Size, Align
254 if (TAI->hasDotTypeDotSizeDirective()) {
255 O << "\t.type\t" << name << ", @object\n";
256 O << "\t.size\t" << name << ", " << Size << "\n";
259 EmitAlignment(Align, GVar);
263 EmitGlobalConstant(C);
267 bool AlphaAsmPrinter::doFinalization(Module &M) {
268 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
270 printModuleLevelGV(I);
272 return AsmPrinter::doFinalization(M);
275 /// PrintAsmOperand - Print out an operand for an inline asm expression.
277 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
279 const char *ExtraCode) {
280 printOperand(MI, OpNo);
284 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
287 const char *ExtraCode) {
288 if (ExtraCode && ExtraCode[0])
289 return true; // Unknown modifier.
291 printOperand(MI, OpNo);
296 // Force static initialization.
297 extern "C" void LLVMInitializeAlphaAsmPrinter() {
298 extern Target TheAlphaTarget;
299 TargetRegistry::RegisterAsmPrinter(TheAlphaTarget,
300 createAlphaCodePrinterPass);