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/Target/TargetAsmInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/Mangler.h"
27 #include "llvm/ADT/Statistic.h"
30 STATISTIC(EmittedInsts, "Number of machine instrs printed");
33 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
35 /// Unique incrementer for label values for referencing Global values.
38 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
39 : AsmPrinter(o, tm, T) {
42 virtual const char *getPassName() const {
43 return "Alpha Assembly Printer";
45 bool printInstruction(const MachineInstr *MI);
46 void printOp(const MachineOperand &MO, bool IsCallOp = false);
47 void printOperand(const MachineInstr *MI, int opNum);
48 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
49 void printModuleLevelGV(const GlobalVariable* GVar);
50 bool runOnMachineFunction(MachineFunction &F);
51 bool doInitialization(Module &M);
52 bool doFinalization(Module &M);
54 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
55 unsigned AsmVariant, const char *ExtraCode);
56 bool PrintAsmMemoryOperand(const MachineInstr *MI,
59 const char *ExtraCode);
61 } // end of anonymous namespace
63 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
64 /// assembly code for a MachineFunction to the given output stream,
65 /// using the given target machine description. This should work
66 /// regardless of whether the function is in SSA form.
68 FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
70 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
73 #include "AlphaGenAsmWriter.inc"
75 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
77 const MachineOperand &MO = MI->getOperand(opNum);
78 if (MO.getType() == MachineOperand::MO_Register) {
79 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
81 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
82 } else if (MO.isImmediate()) {
84 assert(MO.getImm() < (1 << 30));
91 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
92 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
94 switch (MO.getType()) {
95 case MachineOperand::MO_Register:
96 O << RI.get(MO.getReg()).AsmName;
99 case MachineOperand::MO_Immediate:
100 cerr << "printOp() does not handle immediate values\n";
104 case MachineOperand::MO_MachineBasicBlock:
105 printBasicBlockLabel(MO.getMBB());
108 case MachineOperand::MO_ConstantPoolIndex:
109 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
113 case MachineOperand::MO_ExternalSymbol:
114 O << MO.getSymbolName();
117 case MachineOperand::MO_GlobalAddress: {
118 GlobalValue *GV = MO.getGlobal();
119 O << Mang->getValueName(GV);
120 if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
121 ExtWeakSymbols.insert(GV);
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) {
140 SetupMachineFunction(MF);
143 // Print out constants referenced by the function
144 EmitConstantPool(MF.getConstantPool());
146 // Print out jump tables referenced by the function
147 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
149 // Print out labels for the function.
150 const Function *F = MF.getFunction();
151 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
154 switch (F->getLinkage()) {
155 default: assert(0 && "Unknown linkage type!");
156 case Function::InternalLinkage: // Symbols default to internal.
158 case Function::ExternalLinkage:
159 O << "\t.globl " << CurrentFnName << "\n";
161 case Function::WeakLinkage:
162 case Function::LinkOnceLinkage:
163 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
167 printVisibility(CurrentFnName, F->getVisibility());
169 O << "\t.ent " << CurrentFnName << "\n";
171 O << CurrentFnName << ":\n";
173 // Print out code for the function.
174 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
176 if (I != MF.begin()) {
177 printBasicBlockLabel(I, true, true);
180 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
182 // Print the assembly for the instruction.
184 if (!printInstruction(II)) {
185 assert(0 && "Unhandled instruction in asm writer!");
191 O << "\t.end " << CurrentFnName << "\n";
193 // We didn't modify anything.
197 bool AlphaAsmPrinter::doInitialization(Module &M)
199 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
200 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
202 O << "\t.arch ev6\n";
203 O << "\t.set noat\n";
204 return AsmPrinter::doInitialization(M);
207 void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
208 const TargetData *TD = TM.getTargetData();
210 if (!GVar->hasInitializer()) return; // External global require no code
212 // Check to see if this is a special global used by LLVM, if so, emit it.
213 if (EmitSpecialLLVMGlobal(GVar))
216 std::string SectionName = TAI->SectionForGlobal(GVar);
217 std::string name = Mang->getValueName(GVar);
218 Constant *C = GVar->getInitializer();
219 unsigned Size = TD->getABITypeSize(C->getType());
220 unsigned Align = TD->getPreferredAlignmentLog(GVar);
222 // 0: Switch to section
223 SwitchToDataSection(SectionName.c_str());
225 // 1: Check visibility
226 printVisibility(name, GVar->getVisibility());
229 switch (GVar->getLinkage()) {
230 case GlobalValue::LinkOnceLinkage:
231 case GlobalValue::WeakLinkage:
232 case GlobalValue::CommonLinkage:
233 O << TAI->getWeakRefDirective() << name << '\n';
235 case GlobalValue::AppendingLinkage:
236 case GlobalValue::ExternalLinkage:
237 O << TAI->getGlobalDirective() << name << "\n";
239 case GlobalValue::InternalLinkage:
242 assert(0 && "Unknown linkage type!");
243 cerr << "Unknown linkage type!\n";
247 // 3: Type, Size, Align
248 if (TAI->hasDotTypeDotSizeDirective()) {
249 O << "\t.type\t" << name << ", @object\n";
250 O << "\t.size\t" << name << ", " << Size << "\n";
253 EmitAlignment(Align, GVar);
257 // If the initializer is a extern weak symbol, remember to emit the weak
259 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
260 if (GV->hasExternalWeakLinkage())
261 ExtWeakSymbols.insert(GV);
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);