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 << *GetMBBSymbol(MO.getMBB()->getNumber());
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) {
129 SetupMachineFunction(MF);
132 // Print out constants referenced by the function
133 EmitConstantPool(MF.getConstantPool());
135 // Print out jump tables referenced by the function
136 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
138 // Print out labels for the function.
139 const Function *F = MF.getFunction();
140 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
142 EmitAlignment(MF.getAlignment(), F);
143 switch (F->getLinkage()) {
144 default: llvm_unreachable("Unknown linkage type!");
145 case Function::InternalLinkage: // Symbols default to internal.
146 case Function::PrivateLinkage:
147 case Function::LinkerPrivateLinkage:
149 case Function::ExternalLinkage:
150 O << "\t.globl " << *CurrentFnSym << '\n';
152 case Function::WeakAnyLinkage:
153 case Function::WeakODRLinkage:
154 case Function::LinkOnceAnyLinkage:
155 case Function::LinkOnceODRLinkage:
156 O << MAI->getWeakRefDirective() << *CurrentFnSym << '\n';
160 printVisibility(CurrentFnSym, F->getVisibility());
162 O << "\t.ent " << *CurrentFnSym << "\n";
164 O << *CurrentFnSym << ":\n";
166 // Print out code for the function.
167 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
170 EmitBasicBlockStart(I);
172 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
174 // Print the assembly for the instruction.
176 processDebugLoc(II, true);
177 printInstruction(II);
182 processDebugLoc(II, false);
186 O << "\t.end " << *CurrentFnSym << "\n";
188 // We didn't modify anything.
192 void AlphaAsmPrinter::EmitStartOfAsmFile(Module &M) {
193 if (TM.getSubtarget<AlphaSubtarget>().hasCT())
194 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
196 O << "\t.arch ev6\n";
197 O << "\t.set noat\n";
200 /// PrintAsmOperand - Print out an operand for an inline asm expression.
202 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
204 const char *ExtraCode) {
205 printOperand(MI, OpNo);
209 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
212 const char *ExtraCode) {
213 if (ExtraCode && ExtraCode[0])
214 return true; // Unknown modifier.
216 printOperand(MI, OpNo);
221 // Force static initialization.
222 extern "C" void LLVMInitializeAlphaAsmPrinter() {
223 RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);