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/Support/Compiler.h"
28 #include "llvm/Support/Mangler.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/ADT/Statistic.h"
33 STATISTIC(EmittedInsts, "Number of machine instrs printed");
36 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
37 /// Unique incrementer for label values for referencing Global values.
40 explicit AlphaAsmPrinter(raw_ostream &o, TargetMachine &tm,
41 const TargetAsmInfo *T, bool V)
42 : AsmPrinter(o, tm, T, V) {}
44 virtual const char *getPassName() const {
45 return "Alpha Assembly Printer";
47 bool printInstruction(const MachineInstr *MI);
48 void printOp(const MachineOperand &MO, bool IsCallOp = false);
49 void printOperand(const MachineInstr *MI, int opNum);
50 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
51 void printModuleLevelGV(const GlobalVariable* GVar);
52 bool runOnMachineFunction(MachineFunction &F);
53 bool doInitialization(Module &M);
54 bool doFinalization(Module &M);
56 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
57 unsigned AsmVariant, const char *ExtraCode);
58 bool PrintAsmMemoryOperand(const MachineInstr *MI,
61 const char *ExtraCode);
63 } // end of anonymous namespace
65 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
66 /// assembly code for a MachineFunction to the given output stream,
67 /// using the given target machine description. This should work
68 /// regardless of whether the function is in SSA form.
70 FunctionPass *llvm::createAlphaCodePrinterPass(raw_ostream &o,
73 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
76 #include "AlphaGenAsmWriter.inc"
78 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
80 const MachineOperand &MO = MI->getOperand(opNum);
81 if (MO.getType() == MachineOperand::MO_Register) {
82 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
84 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
85 } else if (MO.isImm()) {
87 assert(MO.getImm() < (1 << 30));
94 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
95 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
97 switch (MO.getType()) {
98 case MachineOperand::MO_Register:
99 O << RI.get(MO.getReg()).AsmName;
102 case MachineOperand::MO_Immediate:
103 cerr << "printOp() does not handle immediate values\n";
107 case MachineOperand::MO_MachineBasicBlock:
108 printBasicBlockLabel(MO.getMBB());
111 case MachineOperand::MO_ConstantPoolIndex:
112 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
116 case MachineOperand::MO_ExternalSymbol:
117 O << MO.getSymbolName();
120 case MachineOperand::MO_GlobalAddress: {
121 GlobalValue *GV = MO.getGlobal();
122 O << Mang->getValueName(GV);
126 case MachineOperand::MO_JumpTableIndex:
127 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
128 << '_' << MO.getIndex();
132 O << "<unknown operand type: " << MO.getType() << ">";
137 /// runOnMachineFunction - This uses the printMachineInstruction()
138 /// method to print assembly for each instruction.
140 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
143 SetupMachineFunction(MF);
146 // Print out constants referenced by the function
147 EmitConstantPool(MF.getConstantPool());
149 // Print out jump tables referenced by the function
150 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
152 // Print out labels for the function.
153 const Function *F = MF.getFunction();
154 SwitchToSection(TAI->SectionForGlobal(F));
156 EmitAlignment(MF.getAlignment(), F);
157 switch (F->getLinkage()) {
158 default: assert(0 && "Unknown linkage type!");
159 case Function::InternalLinkage: // Symbols default to internal.
160 case Function::PrivateLinkage:
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 assert(0 && "Unhandled instruction in asm writer!");
197 O << "\t.end " << CurrentFnName << "\n";
199 // We didn't modify anything.
203 bool AlphaAsmPrinter::doInitialization(Module &M)
205 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
206 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
208 O << "\t.arch ev6\n";
209 O << "\t.set noat\n";
210 return AsmPrinter::doInitialization(M);
213 void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
214 const TargetData *TD = TM.getTargetData();
216 if (!GVar->hasInitializer()) return; // External global require no code
218 // Check to see if this is a special global used by LLVM, if so, emit it.
219 if (EmitSpecialLLVMGlobal(GVar))
222 std::string name = Mang->getValueName(GVar);
223 Constant *C = GVar->getInitializer();
224 if (isa<MDNode>(C) || isa<MDString>(C))
226 unsigned Size = TD->getTypeAllocSize(C->getType());
227 unsigned Align = TD->getPreferredAlignmentLog(GVar);
229 // 0: Switch to section
230 SwitchToSection(TAI->SectionForGlobal(GVar));
232 // 1: Check visibility
233 printVisibility(name, GVar->getVisibility());
236 switch (GVar->getLinkage()) {
237 case GlobalValue::LinkOnceAnyLinkage:
238 case GlobalValue::LinkOnceODRLinkage:
239 case GlobalValue::WeakAnyLinkage:
240 case GlobalValue::WeakODRLinkage:
241 case GlobalValue::CommonLinkage:
242 O << TAI->getWeakRefDirective() << name << '\n';
244 case GlobalValue::AppendingLinkage:
245 case GlobalValue::ExternalLinkage:
246 O << TAI->getGlobalDirective() << name << "\n";
248 case GlobalValue::InternalLinkage:
249 case GlobalValue::PrivateLinkage:
252 assert(0 && "Unknown linkage type!");
253 cerr << "Unknown linkage type!\n";
257 // 3: Type, Size, Align
258 if (TAI->hasDotTypeDotSizeDirective()) {
259 O << "\t.type\t" << name << ", @object\n";
260 O << "\t.size\t" << name << ", " << Size << "\n";
263 EmitAlignment(Align, GVar);
267 EmitGlobalConstant(C);
271 bool AlphaAsmPrinter::doFinalization(Module &M) {
272 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
274 printModuleLevelGV(I);
276 return AsmPrinter::doFinalization(M);
279 /// PrintAsmOperand - Print out an operand for an inline asm expression.
281 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
283 const char *ExtraCode) {
284 printOperand(MI, OpNo);
288 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
291 const char *ExtraCode) {
292 if (ExtraCode && ExtraCode[0])
293 return true; // Unknown modifier.
295 printOperand(MI, OpNo);
300 // Force static initialization.
301 extern "C" void LLVMInitializeAlphaAsmPrinter() { }
304 static struct Register {
306 AlphaTargetMachine::registerAsmPrinter(createAlphaCodePrinterPass);