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, CodeGenOpt::Level OL,
43 : AsmPrinter(o, tm, T, OL, V) {}
45 virtual const char *getPassName() const {
46 return "Alpha Assembly Printer";
48 bool printInstruction(const MachineInstr *MI);
49 void printOp(const MachineOperand &MO, bool IsCallOp = false);
50 void printOperand(const MachineInstr *MI, int opNum);
51 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
52 void printModuleLevelGV(const GlobalVariable* GVar);
53 bool runOnMachineFunction(MachineFunction &F);
54 bool doInitialization(Module &M);
55 bool doFinalization(Module &M);
57 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
58 unsigned AsmVariant, const char *ExtraCode);
59 bool PrintAsmMemoryOperand(const MachineInstr *MI,
62 const char *ExtraCode);
64 } // end of anonymous namespace
66 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
67 /// assembly code for a MachineFunction to the given output stream,
68 /// using the given target machine description. This should work
69 /// regardless of whether the function is in SSA form.
71 FunctionPass *llvm::createAlphaCodePrinterPass(raw_ostream &o,
73 CodeGenOpt::Level OptLevel,
75 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, 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 cerr << "printOp() does not handle immediate values\n";
109 case MachineOperand::MO_MachineBasicBlock:
110 printBasicBlockLabel(MO.getMBB());
113 case MachineOperand::MO_ConstantPoolIndex:
114 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
118 case MachineOperand::MO_ExternalSymbol:
119 O << MO.getSymbolName();
122 case MachineOperand::MO_GlobalAddress: {
123 GlobalValue *GV = MO.getGlobal();
124 O << Mang->getValueName(GV);
128 case MachineOperand::MO_JumpTableIndex:
129 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
130 << '_' << MO.getIndex();
134 O << "<unknown operand type: " << MO.getType() << ">";
139 /// runOnMachineFunction - This uses the printMachineInstruction()
140 /// method to print assembly for each instruction.
142 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
145 SetupMachineFunction(MF);
148 // Print out constants referenced by the function
149 EmitConstantPool(MF.getConstantPool());
151 // Print out jump tables referenced by the function
152 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
154 // Print out labels for the function.
155 const Function *F = MF.getFunction();
156 SwitchToSection(TAI->SectionForGlobal(F));
159 switch (F->getLinkage()) {
160 default: assert(0 && "Unknown linkage type!");
161 case Function::InternalLinkage: // Symbols default to internal.
162 case Function::PrivateLinkage:
164 case Function::ExternalLinkage:
165 O << "\t.globl " << CurrentFnName << "\n";
167 case Function::WeakAnyLinkage:
168 case Function::WeakODRLinkage:
169 case Function::LinkOnceAnyLinkage:
170 case Function::LinkOnceODRLinkage:
171 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
175 printVisibility(CurrentFnName, F->getVisibility());
177 O << "\t.ent " << CurrentFnName << "\n";
179 O << CurrentFnName << ":\n";
181 // Print out code for the function.
182 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
184 if (I != MF.begin()) {
185 printBasicBlockLabel(I, true, true);
188 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
190 // Print the assembly for the instruction.
192 if (!printInstruction(II)) {
193 assert(0 && "Unhandled instruction in asm writer!");
199 O << "\t.end " << CurrentFnName << "\n";
201 // We didn't modify anything.
205 bool AlphaAsmPrinter::doInitialization(Module &M)
207 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
208 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
210 O << "\t.arch ev6\n";
211 O << "\t.set noat\n";
212 return AsmPrinter::doInitialization(M);
215 void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
216 const TargetData *TD = TM.getTargetData();
218 if (!GVar->hasInitializer()) return; // External global require no code
220 // Check to see if this is a special global used by LLVM, if so, emit it.
221 if (EmitSpecialLLVMGlobal(GVar))
224 std::string name = Mang->getValueName(GVar);
225 Constant *C = GVar->getInitializer();
228 unsigned Size = TD->getTypeAllocSize(C->getType());
229 unsigned Align = TD->getPreferredAlignmentLog(GVar);
231 // 0: Switch to section
232 SwitchToSection(TAI->SectionForGlobal(GVar));
234 // 1: Check visibility
235 printVisibility(name, GVar->getVisibility());
238 switch (GVar->getLinkage()) {
239 case GlobalValue::LinkOnceAnyLinkage:
240 case GlobalValue::LinkOnceODRLinkage:
241 case GlobalValue::WeakAnyLinkage:
242 case GlobalValue::WeakODRLinkage:
243 case GlobalValue::CommonLinkage:
244 O << TAI->getWeakRefDirective() << name << '\n';
246 case GlobalValue::AppendingLinkage:
247 case GlobalValue::ExternalLinkage:
248 O << TAI->getGlobalDirective() << name << "\n";
250 case GlobalValue::InternalLinkage:
251 case GlobalValue::PrivateLinkage:
254 assert(0 && "Unknown linkage type!");
255 cerr << "Unknown linkage type!\n";
259 // 3: Type, Size, Align
260 if (TAI->hasDotTypeDotSizeDirective()) {
261 O << "\t.type\t" << name << ", @object\n";
262 O << "\t.size\t" << name << ", " << Size << "\n";
265 EmitAlignment(Align, GVar);
269 EmitGlobalConstant(C);
273 bool AlphaAsmPrinter::doFinalization(Module &M) {
274 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
276 printModuleLevelGV(I);
278 return AsmPrinter::doFinalization(M);
281 /// PrintAsmOperand - Print out an operand for an inline asm expression.
283 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
285 const char *ExtraCode) {
286 printOperand(MI, OpNo);
290 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
293 const char *ExtraCode) {
294 if (ExtraCode && ExtraCode[0])
295 return true; // Unknown modifier.
297 printOperand(MI, OpNo);
302 // Force static initialization.
303 extern "C" void LLVMInitializeAlphaAsmPrinter() { }
306 static struct Register {
308 AlphaTargetMachine::registerAsmPrinter(createAlphaCodePrinterPass);