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 O << "\t.ent " << CurrentFnName << "\n";
169 O << CurrentFnName << ":\n";
171 // Print out code for the function.
172 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
174 if (I != MF.begin()) {
175 printBasicBlockLabel(I, true, true);
178 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
180 // Print the assembly for the instruction.
182 if (!printInstruction(II)) {
183 assert(0 && "Unhandled instruction in asm writer!");
189 O << "\t.end " << CurrentFnName << "\n";
191 // We didn't modify anything.
195 bool AlphaAsmPrinter::doInitialization(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";
202 return AsmPrinter::doInitialization(M);
205 void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
206 const TargetData *TD = TM.getTargetData();
208 if (!GVar->hasInitializer()) return; // External global require no code
210 // Check to see if this is a special global used by LLVM, if so, emit it.
211 if (EmitSpecialLLVMGlobal(GVar))
214 std::string SectionName = TAI->SectionForGlobal(GVar);
215 std::string name = Mang->getValueName(GVar);
216 Constant *C = GVar->getInitializer();
217 unsigned Size = TD->getABITypeSize(C->getType());
218 unsigned Align = TD->getPreferredAlignmentLog(GVar);
220 // 0: Switch to section
221 SwitchToDataSection(SectionName.c_str());
223 // 1: Check visibility
224 if (GVar->hasHiddenVisibility())
225 O << TAI->getHiddenDirective() << name << "\n";
228 switch (GVar->getLinkage()) {
229 case GlobalValue::LinkOnceLinkage:
230 case GlobalValue::WeakLinkage:
231 case GlobalValue::CommonLinkage:
232 O << TAI->getWeakRefDirective() << name << '\n';
234 case GlobalValue::AppendingLinkage:
235 case GlobalValue::ExternalLinkage:
236 O << TAI->getGlobalDirective() << name << "\n";
238 case GlobalValue::InternalLinkage:
241 assert(0 && "Unknown linkage type!");
242 cerr << "Unknown linkage type!\n";
246 // 3: Type, Size, Align
247 if (TAI->hasDotTypeDotSizeDirective()) {
248 O << "\t.type\t" << name << ", @object\n";
249 O << "\t.size\t" << name << ", " << Size << "\n";
252 EmitAlignment(Align, GVar);
256 // If the initializer is a extern weak symbol, remember to emit the weak
258 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
259 if (GV->hasExternalWeakLinkage())
260 ExtWeakSymbols.insert(GV);
262 EmitGlobalConstant(C);
266 bool AlphaAsmPrinter::doFinalization(Module &M) {
267 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
269 printModuleLevelGV(I);
271 return AsmPrinter::doFinalization(M);
274 /// PrintAsmOperand - Print out an operand for an inline asm expression.
276 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
278 const char *ExtraCode) {
279 printOperand(MI, OpNo);
283 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
286 const char *ExtraCode) {
287 if (ExtraCode && ExtraCode[0])
288 return true; // Unknown modifier.
290 printOperand(MI, OpNo);