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/Target/TargetAsmInfo.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/Mangler.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/ADT/Statistic.h"
32 STATISTIC(EmittedInsts, "Number of machine instrs printed");
35 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
36 /// Unique incrementer for label values for referencing Global values.
39 explicit AlphaAsmPrinter(raw_ostream &o, TargetMachine &tm,
40 const TargetAsmInfo *T, unsigned OL, bool V)
41 : AsmPrinter(o, tm, T, OL, V) {}
43 virtual const char *getPassName() const {
44 return "Alpha Assembly Printer";
46 bool printInstruction(const MachineInstr *MI);
47 void printOp(const MachineOperand &MO, bool IsCallOp = false);
48 void printOperand(const MachineInstr *MI, int opNum);
49 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
50 void printModuleLevelGV(const GlobalVariable* GVar);
51 bool runOnMachineFunction(MachineFunction &F);
52 bool doInitialization(Module &M);
53 bool doFinalization(Module &M);
55 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
56 unsigned AsmVariant, const char *ExtraCode);
57 bool PrintAsmMemoryOperand(const MachineInstr *MI,
60 const char *ExtraCode);
62 } // end of anonymous namespace
64 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
65 /// assembly code for a MachineFunction to the given output stream,
66 /// using the given target machine description. This should work
67 /// regardless of whether the function is in SSA form.
69 FunctionPass *llvm::createAlphaCodePrinterPass(raw_ostream &o,
73 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, 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);
123 if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
124 ExtWeakSymbols.insert(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();
226 unsigned Size = TD->getTypePaddedSize(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 // If the initializer is a extern weak symbol, remember to emit the weak
269 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
270 if (GV->hasExternalWeakLinkage())
271 ExtWeakSymbols.insert(GV);
273 EmitGlobalConstant(C);
277 bool AlphaAsmPrinter::doFinalization(Module &M) {
278 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
280 printModuleLevelGV(I);
282 return AsmPrinter::doFinalization(M);
285 /// PrintAsmOperand - Print out an operand for an inline asm expression.
287 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
289 const char *ExtraCode) {
290 printOperand(MI, OpNo);
294 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
297 const char *ExtraCode) {
298 if (ExtraCode && ExtraCode[0])
299 return true; // Unknown modifier.
301 printOperand(MI, OpNo);