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/Target/TargetAsmInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/Mangler.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/ADT/Statistic.h"
31 STATISTIC(EmittedInsts, "Number of machine instrs printed");
34 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
36 /// Unique incrementer for label values for referencing Global values.
39 AlphaAsmPrinter(raw_ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
40 : AsmPrinter(o, tm, T) {
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,
71 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
74 #include "AlphaGenAsmWriter.inc"
76 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
78 const MachineOperand &MO = MI->getOperand(opNum);
79 if (MO.getType() == MachineOperand::MO_Register) {
80 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
82 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
83 } else if (MO.isImmediate()) {
85 assert(MO.getImm() < (1 << 30));
92 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
93 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
95 switch (MO.getType()) {
96 case MachineOperand::MO_Register:
97 O << RI.get(MO.getReg()).AsmName;
100 case MachineOperand::MO_Immediate:
101 cerr << "printOp() does not handle immediate values\n";
105 case MachineOperand::MO_MachineBasicBlock:
106 printBasicBlockLabel(MO.getMBB());
109 case MachineOperand::MO_ConstantPoolIndex:
110 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
114 case MachineOperand::MO_ExternalSymbol:
115 O << MO.getSymbolName();
118 case MachineOperand::MO_GlobalAddress: {
119 GlobalValue *GV = MO.getGlobal();
120 O << Mang->getValueName(GV);
121 if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
122 ExtWeakSymbols.insert(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) {
141 SetupMachineFunction(MF);
144 // Print out constants referenced by the function
145 EmitConstantPool(MF.getConstantPool());
147 // Print out jump tables referenced by the function
148 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
150 // Print out labels for the function.
151 const Function *F = MF.getFunction();
152 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
155 switch (F->getLinkage()) {
156 default: assert(0 && "Unknown linkage type!");
157 case Function::InternalLinkage: // Symbols default to internal.
159 case Function::ExternalLinkage:
160 O << "\t.globl " << CurrentFnName << "\n";
162 case Function::WeakLinkage:
163 case Function::LinkOnceLinkage:
164 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
168 printVisibility(CurrentFnName, F->getVisibility());
170 O << "\t.ent " << CurrentFnName << "\n";
172 O << CurrentFnName << ":\n";
174 // Print out code for the function.
175 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
177 if (I != MF.begin()) {
178 printBasicBlockLabel(I, true, true);
181 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
183 // Print the assembly for the instruction.
185 if (!printInstruction(II)) {
186 assert(0 && "Unhandled instruction in asm writer!");
192 O << "\t.end " << CurrentFnName << "\n";
194 // We didn't modify anything.
198 bool AlphaAsmPrinter::doInitialization(Module &M)
200 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
201 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
203 O << "\t.arch ev6\n";
204 O << "\t.set noat\n";
205 return AsmPrinter::doInitialization(M);
208 void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
209 const TargetData *TD = TM.getTargetData();
211 if (!GVar->hasInitializer()) return; // External global require no code
213 // Check to see if this is a special global used by LLVM, if so, emit it.
214 if (EmitSpecialLLVMGlobal(GVar))
217 std::string SectionName = TAI->SectionForGlobal(GVar);
218 std::string name = Mang->getValueName(GVar);
219 Constant *C = GVar->getInitializer();
220 unsigned Size = TD->getABITypeSize(C->getType());
221 unsigned Align = TD->getPreferredAlignmentLog(GVar);
223 // 0: Switch to section
224 SwitchToDataSection(SectionName.c_str());
226 // 1: Check visibility
227 printVisibility(name, GVar->getVisibility());
230 switch (GVar->getLinkage()) {
231 case GlobalValue::LinkOnceLinkage:
232 case GlobalValue::WeakLinkage:
233 case GlobalValue::CommonLinkage:
234 O << TAI->getWeakRefDirective() << name << '\n';
236 case GlobalValue::AppendingLinkage:
237 case GlobalValue::ExternalLinkage:
238 O << TAI->getGlobalDirective() << name << "\n";
240 case GlobalValue::InternalLinkage:
243 assert(0 && "Unknown linkage type!");
244 cerr << "Unknown linkage type!\n";
248 // 3: Type, Size, Align
249 if (TAI->hasDotTypeDotSizeDirective()) {
250 O << "\t.type\t" << name << ", @object\n";
251 O << "\t.size\t" << name << ", " << Size << "\n";
254 EmitAlignment(Align, GVar);
258 // If the initializer is a extern weak symbol, remember to emit the weak
260 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
261 if (GV->hasExternalWeakLinkage())
262 ExtWeakSymbols.insert(GV);
264 EmitGlobalConstant(C);
268 bool AlphaAsmPrinter::doFinalization(Module &M) {
269 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
271 printModuleLevelGV(I);
273 return AsmPrinter::doFinalization(M);
276 /// PrintAsmOperand - Print out an operand for an inline asm expression.
278 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
280 const char *ExtraCode) {
281 printOperand(MI, OpNo);
285 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
288 const char *ExtraCode) {
289 if (ExtraCode && ExtraCode[0])
290 return true; // Unknown modifier.
292 printOperand(MI, OpNo);