blob: e592e80a760d73570f4244f9922937d1d0fa3677 [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"
27#include "llvm/ADT/Statistic.h"
28using namespace llvm;
29
30STATISTIC(EmittedInsts, "Number of machine instrs printed");
31
32namespace {
33 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
34
35 /// Unique incrementer for label values for referencing Global values.
36 ///
37
38 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
39 : AsmPrinter(o, tm, T) {
40 }
41
42 virtual const char *getPassName() const {
43 return "Alpha Assembly Printer";
44 }
45 bool printInstruction(const MachineInstr *MI);
46 void printOp(const MachineOperand &MO, bool IsCallOp = false);
47 void printOperand(const MachineInstr *MI, int opNum);
48 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000049 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 bool runOnMachineFunction(MachineFunction &F);
51 bool doInitialization(Module &M);
52 bool doFinalization(Module &M);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000053
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
55 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000056 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +000058 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 const char *ExtraCode);
60 };
61} // end of anonymous namespace
62
63/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
64/// assembly code for a MachineFunction to the given output stream,
65/// using the given target machine description. This should work
66/// regardless of whether the function is in SSA form.
67///
68FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
69 TargetMachine &tm) {
70 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
71}
72
73#include "AlphaGenAsmWriter.inc"
74
75void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
76{
77 const MachineOperand &MO = MI->getOperand(opNum);
78 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman1e57df32008-02-10 18:45:23 +000079 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
80 "Not physreg??");
Bill Wendling8eeb9792008-02-26 21:11:01 +000081 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 } else if (MO.isImmediate()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000083 O << MO.getImm();
84 assert(MO.getImm() < (1 << 30));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 } else {
86 printOp(MO);
87 }
88}
89
90
91void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman1e57df32008-02-10 18:45:23 +000092 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
94 switch (MO.getType()) {
95 case MachineOperand::MO_Register:
Bill Wendling8eeb9792008-02-26 21:11:01 +000096 O << RI.get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 return;
98
99 case MachineOperand::MO_Immediate:
100 cerr << "printOp() does not handle immediate values\n";
101 abort();
102 return;
103
104 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000105 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 return;
107
108 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000109 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000110 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 return;
112
113 case MachineOperand::MO_ExternalSymbol:
114 O << MO.getSymbolName();
115 return;
116
117 case MachineOperand::MO_GlobalAddress: {
118 GlobalValue *GV = MO.getGlobal();
119 O << Mang->getValueName(GV);
120 if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
121 ExtWeakSymbols.insert(GV);
122 return;
123 }
124
125 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000126 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000127 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 return;
129
130 default:
131 O << "<unknown operand type: " << MO.getType() << ">";
132 return;
133 }
134}
135
136/// runOnMachineFunction - This uses the printMachineInstruction()
137/// method to print assembly for each instruction.
138///
139bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
140 SetupMachineFunction(MF);
141 O << "\n\n";
142
143 // Print out constants referenced by the function
144 EmitConstantPool(MF.getConstantPool());
145
146 // Print out jump tables referenced by the function
147 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
148
149 // Print out labels for the function.
150 const Function *F = MF.getFunction();
151 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000152
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 EmitAlignment(4, F);
154 switch (F->getLinkage()) {
155 default: assert(0 && "Unknown linkage type!");
156 case Function::InternalLinkage: // Symbols default to internal.
157 break;
158 case Function::ExternalLinkage:
159 O << "\t.globl " << CurrentFnName << "\n";
160 break;
161 case Function::WeakLinkage:
162 case Function::LinkOnceLinkage:
163 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
164 break;
165 }
166
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000167 printVisibility(CurrentFnName, F->getVisibility());
168
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 O << "\t.ent " << CurrentFnName << "\n";
170
171 O << CurrentFnName << ":\n";
172
173 // Print out code for the function.
174 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
175 I != E; ++I) {
176 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000177 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 O << '\n';
179 }
180 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
181 II != E; ++II) {
182 // Print the assembly for the instruction.
183 ++EmittedInsts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 if (!printInstruction(II)) {
185 assert(0 && "Unhandled instruction in asm writer!");
186 abort();
187 }
188 }
189 }
190
191 O << "\t.end " << CurrentFnName << "\n";
192
193 // We didn't modify anything.
194 return false;
195}
196
197bool AlphaAsmPrinter::doInitialization(Module &M)
198{
199 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
200 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
201 else
202 O << "\t.arch ev6\n";
203 O << "\t.set noat\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000204 return AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205}
206
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000207void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 const TargetData *TD = TM.getTargetData();
209
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000210 if (!GVar->hasInitializer()) return; // External global require no code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000212 // Check to see if this is a special global used by LLVM, if so, emit it.
213 if (EmitSpecialLLVMGlobal(GVar))
214 return;
215
216 std::string SectionName = TAI->SectionForGlobal(GVar);
217 std::string name = Mang->getValueName(GVar);
218 Constant *C = GVar->getInitializer();
219 unsigned Size = TD->getABITypeSize(C->getType());
220 unsigned Align = TD->getPreferredAlignmentLog(GVar);
221
222 // 0: Switch to section
223 SwitchToDataSection(SectionName.c_str());
224
225 // 1: Check visibility
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000226 printVisibility(name, GVar->getVisibility());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000227
228 // 2: Kind
229 switch (GVar->getLinkage()) {
230 case GlobalValue::LinkOnceLinkage:
231 case GlobalValue::WeakLinkage:
232 case GlobalValue::CommonLinkage:
233 O << TAI->getWeakRefDirective() << name << '\n';
234 break;
235 case GlobalValue::AppendingLinkage:
236 case GlobalValue::ExternalLinkage:
237 O << TAI->getGlobalDirective() << name << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 break;
239 case GlobalValue::InternalLinkage:
240 break;
241 default:
242 assert(0 && "Unknown linkage type!");
243 cerr << "Unknown linkage type!\n";
244 abort();
245 }
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000246
247 // 3: Type, Size, Align
248 if (TAI->hasDotTypeDotSizeDirective()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 O << "\t.type\t" << name << ", @object\n";
250 O << "\t.size\t" << name << ", " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 }
252
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000253 EmitAlignment(Align, GVar);
254
255 O << name << ":\n";
256
257 // If the initializer is a extern weak symbol, remember to emit the weak
258 // reference!
259 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
260 if (GV->hasExternalWeakLinkage())
261 ExtWeakSymbols.insert(GV);
262
263 EmitGlobalConstant(C);
264 O << '\n';
265}
266
267bool AlphaAsmPrinter::doFinalization(Module &M) {
268 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
269 I != E; ++I)
270 printModuleLevelGV(I);
271
Dan Gohman4a558a32007-07-25 19:33:14 +0000272 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273}
274
275/// PrintAsmOperand - Print out an operand for an inline asm expression.
276///
277bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000278 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 const char *ExtraCode) {
280 printOperand(MI, OpNo);
281 return false;
282}
283
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000284bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000286 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 const char *ExtraCode) {
288 if (ExtraCode && ExtraCode[0])
289 return true; // Unknown modifier.
290 O << "0(";
291 printOperand(MI, OpNo);
292 O << ")";
293 return false;
294}