blob: d92317e9204ee2bf178663e55fb989ea7a573c4c [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- AlphaAsmPrinter.cpp - Alpha LLVM assembly writer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format Alpha assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "asm-printer"
16#include "Alpha.h"
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"
Owen Anderson847b99b2008-08-21 00:14:44 +000027#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/ADT/Statistic.h"
29using namespace llvm;
30
31STATISTIC(EmittedInsts, "Number of machine instrs printed");
32
33namespace {
34 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
35
36 /// Unique incrementer for label values for referencing Global values.
37 ///
38
Owen Anderson847b99b2008-08-21 00:14:44 +000039 AlphaAsmPrinter(raw_ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 : AsmPrinter(o, tm, T) {
41 }
42
43 virtual const char *getPassName() const {
44 return "Alpha Assembly Printer";
45 }
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);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000050 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 bool runOnMachineFunction(MachineFunction &F);
52 bool doInitialization(Module &M);
53 bool doFinalization(Module &M);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000054
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
56 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000057 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +000059 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 const char *ExtraCode);
61 };
62} // end of anonymous namespace
63
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.
68///
Owen Anderson847b99b2008-08-21 00:14:44 +000069FunctionPass *llvm::createAlphaCodePrinterPass(raw_ostream &o,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 TargetMachine &tm) {
71 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
72}
73
74#include "AlphaGenAsmWriter.inc"
75
76void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
77{
78 const MachineOperand &MO = MI->getOperand(opNum);
79 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman1e57df32008-02-10 18:45:23 +000080 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
81 "Not physreg??");
Bill Wendling8eeb9792008-02-26 21:11:01 +000082 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 } else if (MO.isImmediate()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000084 O << MO.getImm();
85 assert(MO.getImm() < (1 << 30));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 } else {
87 printOp(MO);
88 }
89}
90
91
92void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman1e57df32008-02-10 18:45:23 +000093 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094
95 switch (MO.getType()) {
96 case MachineOperand::MO_Register:
Bill Wendling8eeb9792008-02-26 21:11:01 +000097 O << RI.get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 return;
99
100 case MachineOperand::MO_Immediate:
101 cerr << "printOp() does not handle immediate values\n";
102 abort();
103 return;
104
105 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000106 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 return;
108
109 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000110 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000111 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 return;
113
114 case MachineOperand::MO_ExternalSymbol:
115 O << MO.getSymbolName();
116 return;
117
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);
123 return;
124 }
125
126 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000127 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000128 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 return;
130
131 default:
132 O << "<unknown operand type: " << MO.getType() << ">";
133 return;
134 }
135}
136
137/// runOnMachineFunction - This uses the printMachineInstruction()
138/// method to print assembly for each instruction.
139///
140bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
141 SetupMachineFunction(MF);
142 O << "\n\n";
143
144 // Print out constants referenced by the function
145 EmitConstantPool(MF.getConstantPool());
146
147 // Print out jump tables referenced by the function
148 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
149
150 // Print out labels for the function.
151 const Function *F = MF.getFunction();
152 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000153
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 EmitAlignment(4, F);
155 switch (F->getLinkage()) {
156 default: assert(0 && "Unknown linkage type!");
157 case Function::InternalLinkage: // Symbols default to internal.
158 break;
159 case Function::ExternalLinkage:
160 O << "\t.globl " << CurrentFnName << "\n";
161 break;
162 case Function::WeakLinkage:
163 case Function::LinkOnceLinkage:
164 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
165 break;
166 }
167
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000168 printVisibility(CurrentFnName, F->getVisibility());
169
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 O << "\t.ent " << CurrentFnName << "\n";
171
172 O << CurrentFnName << ":\n";
173
174 // Print out code for the function.
175 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
176 I != E; ++I) {
177 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000178 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 O << '\n';
180 }
181 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
182 II != E; ++II) {
183 // Print the assembly for the instruction.
184 ++EmittedInsts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 if (!printInstruction(II)) {
186 assert(0 && "Unhandled instruction in asm writer!");
187 abort();
188 }
189 }
190 }
191
192 O << "\t.end " << CurrentFnName << "\n";
193
194 // We didn't modify anything.
195 return false;
196}
197
198bool AlphaAsmPrinter::doInitialization(Module &M)
199{
200 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
201 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
202 else
203 O << "\t.arch ev6\n";
204 O << "\t.set noat\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000205 return AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206}
207
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000208void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 const TargetData *TD = TM.getTargetData();
210
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000211 if (!GVar->hasInitializer()) return; // External global require no code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000213 // Check to see if this is a special global used by LLVM, if so, emit it.
214 if (EmitSpecialLLVMGlobal(GVar))
215 return;
216
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);
222
223 // 0: Switch to section
224 SwitchToDataSection(SectionName.c_str());
225
226 // 1: Check visibility
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000227 printVisibility(name, GVar->getVisibility());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000228
229 // 2: Kind
230 switch (GVar->getLinkage()) {
231 case GlobalValue::LinkOnceLinkage:
232 case GlobalValue::WeakLinkage:
233 case GlobalValue::CommonLinkage:
234 O << TAI->getWeakRefDirective() << name << '\n';
235 break;
236 case GlobalValue::AppendingLinkage:
237 case GlobalValue::ExternalLinkage:
238 O << TAI->getGlobalDirective() << name << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 break;
240 case GlobalValue::InternalLinkage:
241 break;
242 default:
243 assert(0 && "Unknown linkage type!");
244 cerr << "Unknown linkage type!\n";
245 abort();
246 }
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000247
248 // 3: Type, Size, Align
249 if (TAI->hasDotTypeDotSizeDirective()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 O << "\t.type\t" << name << ", @object\n";
251 O << "\t.size\t" << name << ", " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 }
253
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000254 EmitAlignment(Align, GVar);
255
256 O << name << ":\n";
257
258 // If the initializer is a extern weak symbol, remember to emit the weak
259 // reference!
260 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
261 if (GV->hasExternalWeakLinkage())
262 ExtWeakSymbols.insert(GV);
263
264 EmitGlobalConstant(C);
265 O << '\n';
266}
267
268bool AlphaAsmPrinter::doFinalization(Module &M) {
269 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
270 I != E; ++I)
271 printModuleLevelGV(I);
272
Dan Gohman4a558a32007-07-25 19:33:14 +0000273 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274}
275
276/// PrintAsmOperand - Print out an operand for an inline asm expression.
277///
278bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000279 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 const char *ExtraCode) {
281 printOperand(MI, OpNo);
282 return false;
283}
284
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000285bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000287 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 const char *ExtraCode) {
289 if (ExtraCode && ExtraCode[0])
290 return true; // Unknown modifier.
291 O << "0(";
292 printOperand(MI, OpNo);
293 O << ")";
294 return false;
295}