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/Type.h"
21 #include "llvm/Assembly/Writer.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/DwarfWriter.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Target/TargetLoweringObjectFile.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetRegistry.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/Mangler.h"
33 #include "llvm/Support/FormattedStream.h"
34 #include "llvm/ADT/Statistic.h"
37 STATISTIC(EmittedInsts, "Number of machine instrs printed");
40 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
41 /// Unique incrementer for label values for referencing Global values.
44 explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
45 const MCAsmInfo *T, bool V)
46 : AsmPrinter(o, tm, T, V) {}
48 virtual const char *getPassName() const {
49 return "Alpha Assembly Printer";
51 void printInstruction(const MachineInstr *MI);
52 static const char *getRegisterName(unsigned RegNo);
54 void printOp(const MachineOperand &MO, bool IsCallOp = false);
55 void printOperand(const MachineInstr *MI, int opNum);
56 void printBaseOffsetPair(const MachineInstr *MI, int i, bool brackets=true);
57 void PrintGlobalVariable(const GlobalVariable *GVar);
58 bool runOnMachineFunction(MachineFunction &F);
59 void EmitStartOfAsmFile(Module &M);
61 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
62 unsigned AsmVariant, const char *ExtraCode);
63 bool PrintAsmMemoryOperand(const MachineInstr *MI,
66 const char *ExtraCode);
68 } // end of anonymous namespace
70 #include "AlphaGenAsmWriter.inc"
72 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
74 const MachineOperand &MO = MI->getOperand(opNum);
75 if (MO.getType() == MachineOperand::MO_Register) {
76 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
78 O << getRegisterName(MO.getReg());
79 } else if (MO.isImm()) {
81 assert(MO.getImm() < (1 << 30));
88 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
89 switch (MO.getType()) {
90 case MachineOperand::MO_Register:
91 O << getRegisterName(MO.getReg());
94 case MachineOperand::MO_Immediate:
95 llvm_unreachable("printOp() does not handle immediate values");
98 case MachineOperand::MO_MachineBasicBlock:
99 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
102 case MachineOperand::MO_ConstantPoolIndex:
103 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
107 case MachineOperand::MO_ExternalSymbol:
108 O << MO.getSymbolName();
111 case MachineOperand::MO_GlobalAddress:
112 O << Mang->getMangledName(MO.getGlobal());
115 case MachineOperand::MO_JumpTableIndex:
116 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
117 << '_' << MO.getIndex();
121 O << "<unknown operand type: " << MO.getType() << ">";
126 /// runOnMachineFunction - This uses the printMachineInstruction()
127 /// method to print assembly for each instruction.
129 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
132 SetupMachineFunction(MF);
135 // Print out constants referenced by the function
136 EmitConstantPool(MF.getConstantPool());
138 // Print out jump tables referenced by the function
139 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
141 // Print out labels for the function.
142 const Function *F = MF.getFunction();
143 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
145 EmitAlignment(MF.getAlignment(), F);
146 switch (F->getLinkage()) {
147 default: llvm_unreachable("Unknown linkage type!");
148 case Function::InternalLinkage: // Symbols default to internal.
149 case Function::PrivateLinkage:
150 case Function::LinkerPrivateLinkage:
152 case Function::ExternalLinkage:
153 O << "\t.globl " << CurrentFnName << "\n";
155 case Function::WeakAnyLinkage:
156 case Function::WeakODRLinkage:
157 case Function::LinkOnceAnyLinkage:
158 case Function::LinkOnceODRLinkage:
159 O << MAI->getWeakRefDirective() << CurrentFnName << "\n";
163 printVisibility(CurrentFnName, F->getVisibility());
165 O << "\t.ent " << CurrentFnName << "\n";
167 O << CurrentFnName << ":\n";
169 // Print out code for the function.
170 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
172 if (I != MF.begin()) {
173 EmitBasicBlockStart(I);
176 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
178 // Print the assembly for the instruction.
182 printInstruction(II);
184 if (VerboseAsm && !II->getDebugLoc().isUnknown())
190 O << "\t.end " << CurrentFnName << "\n";
192 // We didn't modify anything.
196 void AlphaAsmPrinter::EmitStartOfAsmFile(Module &M) {
197 if (TM.getSubtarget<AlphaSubtarget>().hasCT())
198 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
200 O << "\t.arch ev6\n";
201 O << "\t.set noat\n";
204 void AlphaAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
205 const TargetData *TD = TM.getTargetData();
207 if (!GVar->hasInitializer()) return; // External global require no code
209 // Check to see if this is a special global used by LLVM, if so, emit it.
210 if (EmitSpecialLLVMGlobal(GVar))
213 std::string name = Mang->getMangledName(GVar);
214 Constant *C = GVar->getInitializer();
215 unsigned Size = TD->getTypeAllocSize(C->getType());
216 unsigned Align = TD->getPreferredAlignmentLog(GVar);
218 // 0: Switch to section
219 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
222 // 1: Check visibility
223 printVisibility(name, GVar->getVisibility());
226 switch (GVar->getLinkage()) {
227 case GlobalValue::LinkOnceAnyLinkage:
228 case GlobalValue::LinkOnceODRLinkage:
229 case GlobalValue::WeakAnyLinkage:
230 case GlobalValue::WeakODRLinkage:
231 case GlobalValue::CommonLinkage:
232 O << MAI->getWeakRefDirective() << name << '\n';
234 case GlobalValue::AppendingLinkage:
235 case GlobalValue::ExternalLinkage:
236 O << MAI->getGlobalDirective() << name << "\n";
238 case GlobalValue::InternalLinkage:
239 case GlobalValue::PrivateLinkage:
240 case GlobalValue::LinkerPrivateLinkage:
243 llvm_unreachable("Unknown linkage type!");
246 // 3: Type, Size, Align
247 if (MAI->hasDotTypeDotSizeDirective()) {
248 O << "\t.type\t" << name << ", @object\n";
249 O << "\t.size\t" << name << ", " << Size << "\n";
252 EmitAlignment(Align, GVar);
256 EmitGlobalConstant(C);
260 /// PrintAsmOperand - Print out an operand for an inline asm expression.
262 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
264 const char *ExtraCode) {
265 printOperand(MI, OpNo);
269 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
272 const char *ExtraCode) {
273 if (ExtraCode && ExtraCode[0])
274 return true; // Unknown modifier.
276 printOperand(MI, OpNo);
281 // Force static initialization.
282 extern "C" void LLVMInitializeAlphaAsmPrinter() {
283 RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);