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/ErrorHandling.h"
31 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/ADT/Statistic.h"
35 STATISTIC(EmittedInsts, "Number of machine instrs printed");
38 struct AlphaAsmPrinter : public AsmPrinter {
39 /// Unique incrementer for label values for referencing Global values.
42 explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
43 const MCAsmInfo *T, bool V)
44 : AsmPrinter(o, tm, T, V) {}
46 virtual const char *getPassName() const {
47 return "Alpha Assembly Printer";
49 void printInstruction(const MachineInstr *MI);
50 static const char *getRegisterName(unsigned RegNo);
52 void printOp(const MachineOperand &MO, bool IsCallOp = false);
53 void printOperand(const MachineInstr *MI, int opNum);
54 void printBaseOffsetPair(const MachineInstr *MI, int i, bool brackets=true);
55 bool runOnMachineFunction(MachineFunction &F);
56 void EmitStartOfAsmFile(Module &M);
58 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
59 unsigned AsmVariant, const char *ExtraCode);
60 bool PrintAsmMemoryOperand(const MachineInstr *MI,
63 const char *ExtraCode);
65 } // end of anonymous namespace
67 #include "AlphaGenAsmWriter.inc"
69 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
71 const MachineOperand &MO = MI->getOperand(opNum);
72 if (MO.getType() == MachineOperand::MO_Register) {
73 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
75 O << getRegisterName(MO.getReg());
76 } else if (MO.isImm()) {
78 assert(MO.getImm() < (1 << 30));
85 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
86 switch (MO.getType()) {
87 case MachineOperand::MO_Register:
88 O << getRegisterName(MO.getReg());
91 case MachineOperand::MO_Immediate:
92 llvm_unreachable("printOp() does not handle immediate values");
95 case MachineOperand::MO_MachineBasicBlock:
96 O << *MO.getMBB()->getSymbol(OutContext);
99 case MachineOperand::MO_ConstantPoolIndex:
100 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
104 case MachineOperand::MO_ExternalSymbol:
105 O << MO.getSymbolName();
108 case MachineOperand::MO_GlobalAddress:
109 O << *GetGlobalValueSymbol(MO.getGlobal());
112 case MachineOperand::MO_JumpTableIndex:
113 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
114 << '_' << MO.getIndex();
118 O << "<unknown operand type: " << MO.getType() << ">";
123 /// runOnMachineFunction - This uses the printMachineInstruction()
124 /// method to print assembly for each instruction.
126 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
127 SetupMachineFunction(MF);
130 // Print out constants referenced by the function
131 EmitConstantPool(MF.getConstantPool());
133 // Print out jump tables referenced by the function
134 EmitJumpTableInfo(MF);
136 // Print out labels for the function.
137 const Function *F = MF.getFunction();
138 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
140 EmitAlignment(MF.getAlignment(), F);
141 switch (F->getLinkage()) {
142 default: llvm_unreachable("Unknown linkage type!");
143 case Function::InternalLinkage: // Symbols default to internal.
144 case Function::PrivateLinkage:
145 case Function::LinkerPrivateLinkage:
147 case Function::ExternalLinkage:
148 O << "\t.globl " << *CurrentFnSym << '\n';
150 case Function::WeakAnyLinkage:
151 case Function::WeakODRLinkage:
152 case Function::LinkOnceAnyLinkage:
153 case Function::LinkOnceODRLinkage:
154 O << MAI->getWeakRefDirective() << *CurrentFnSym << '\n';
158 printVisibility(CurrentFnSym, F->getVisibility());
160 O << "\t.ent " << *CurrentFnSym << "\n";
162 O << *CurrentFnSym << ":\n";
164 // Print out code for the function.
165 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
168 EmitBasicBlockStart(I);
170 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
172 // Print the assembly for the instruction.
174 processDebugLoc(II, true);
175 printInstruction(II);
180 processDebugLoc(II, false);
184 O << "\t.end " << *CurrentFnSym << "\n";
186 // We didn't modify anything.
190 void AlphaAsmPrinter::EmitStartOfAsmFile(Module &M) {
191 if (TM.getSubtarget<AlphaSubtarget>().hasCT())
192 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
194 O << "\t.arch ev6\n";
195 O << "\t.set noat\n";
198 /// PrintAsmOperand - Print out an operand for an inline asm expression.
200 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
202 const char *ExtraCode) {
203 printOperand(MI, OpNo);
207 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
210 const char *ExtraCode) {
211 if (ExtraCode && ExtraCode[0])
212 return true; // Unknown modifier.
214 printOperand(MI, OpNo);
219 // Force static initialization.
220 extern "C" void LLVMInitializeAlphaAsmPrinter() {
221 RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);