blob: 536e5920f383eb672855d2c05575e25a1203c7ea [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"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000023#include "llvm/CodeGen/DwarfWriter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Target/TargetAsmInfo.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Support/Compiler.h"
27#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000028#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/ADT/Statistic.h"
30using namespace llvm;
31
32STATISTIC(EmittedInsts, "Number of machine instrs printed");
33
34namespace {
35 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 /// Unique incrementer for label values for referencing Global values.
37 ///
38
Bill Wendling4f405312009-02-24 08:30:20 +000039 AlphaAsmPrinter(raw_ostream &o, TargetMachine &tm,
40 const TargetAsmInfo *T, bool F)
41 : AsmPrinter(o, tm, T, F) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042
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,
Bill Wendling4f405312009-02-24 08:30:20 +000070 TargetMachine &tm,
71 bool fast) {
72 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), fast);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073}
74
75#include "AlphaGenAsmWriter.inc"
76
77void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
78{
79 const MachineOperand &MO = MI->getOperand(opNum);
80 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman1e57df32008-02-10 18:45:23 +000081 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
82 "Not physreg??");
Bill Wendling8eeb9792008-02-26 21:11:01 +000083 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000084 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000085 O << MO.getImm();
86 assert(MO.getImm() < (1 << 30));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 } else {
88 printOp(MO);
89 }
90}
91
92
93void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman1e57df32008-02-10 18:45:23 +000094 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095
96 switch (MO.getType()) {
97 case MachineOperand::MO_Register:
Bill Wendling8eeb9792008-02-26 21:11:01 +000098 O << RI.get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 return;
100
101 case MachineOperand::MO_Immediate:
102 cerr << "printOp() does not handle immediate values\n";
103 abort();
104 return;
105
106 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000107 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 return;
109
110 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000111 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000112 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 return;
114
115 case MachineOperand::MO_ExternalSymbol:
116 O << MO.getSymbolName();
117 return;
118
119 case MachineOperand::MO_GlobalAddress: {
120 GlobalValue *GV = MO.getGlobal();
121 O << Mang->getValueName(GV);
122 if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
123 ExtWeakSymbols.insert(GV);
124 return;
125 }
126
127 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000128 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000129 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 return;
131
132 default:
133 O << "<unknown operand type: " << MO.getType() << ">";
134 return;
135 }
136}
137
138/// runOnMachineFunction - This uses the printMachineInstruction()
139/// method to print assembly for each instruction.
140///
141bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000142 this->MF = &MF;
143
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 SetupMachineFunction(MF);
145 O << "\n\n";
146
147 // Print out constants referenced by the function
148 EmitConstantPool(MF.getConstantPool());
149
150 // Print out jump tables referenced by the function
151 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
152
153 // Print out labels for the function.
154 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000155 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000156
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 EmitAlignment(4, F);
158 switch (F->getLinkage()) {
159 default: assert(0 && "Unknown linkage type!");
160 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola3b450b32009-01-15 21:51:46 +0000161 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 break;
163 case Function::ExternalLinkage:
164 O << "\t.globl " << CurrentFnName << "\n";
165 break;
166 case Function::WeakLinkage:
167 case Function::LinkOnceLinkage:
168 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
169 break;
170 }
171
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000172 printVisibility(CurrentFnName, F->getVisibility());
173
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 O << "\t.ent " << CurrentFnName << "\n";
175
176 O << CurrentFnName << ":\n";
177
178 // Print out code for the function.
179 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
180 I != E; ++I) {
181 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000182 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 O << '\n';
184 }
185 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
186 II != E; ++II) {
187 // Print the assembly for the instruction.
188 ++EmittedInsts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 if (!printInstruction(II)) {
190 assert(0 && "Unhandled instruction in asm writer!");
191 abort();
192 }
193 }
194 }
195
196 O << "\t.end " << CurrentFnName << "\n";
197
198 // We didn't modify anything.
199 return false;
200}
201
202bool AlphaAsmPrinter::doInitialization(Module &M)
203{
204 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
205 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
206 else
207 O << "\t.arch ev6\n";
208 O << "\t.set noat\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000209 return AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210}
211
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000212void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 const TargetData *TD = TM.getTargetData();
214
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000215 if (!GVar->hasInitializer()) return; // External global require no code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000217 // Check to see if this is a special global used by LLVM, if so, emit it.
218 if (EmitSpecialLLVMGlobal(GVar))
219 return;
220
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000221 std::string name = Mang->getValueName(GVar);
222 Constant *C = GVar->getInitializer();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000223 unsigned Size = TD->getTypePaddedSize(C->getType());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000224 unsigned Align = TD->getPreferredAlignmentLog(GVar);
225
226 // 0: Switch to section
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000227 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000228
229 // 1: Check visibility
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000230 printVisibility(name, GVar->getVisibility());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000231
232 // 2: Kind
233 switch (GVar->getLinkage()) {
234 case GlobalValue::LinkOnceLinkage:
235 case GlobalValue::WeakLinkage:
236 case GlobalValue::CommonLinkage:
237 O << TAI->getWeakRefDirective() << name << '\n';
238 break;
239 case GlobalValue::AppendingLinkage:
240 case GlobalValue::ExternalLinkage:
241 O << TAI->getGlobalDirective() << name << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 break;
243 case GlobalValue::InternalLinkage:
Rafael Espindola3b450b32009-01-15 21:51:46 +0000244 case GlobalValue::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 break;
246 default:
247 assert(0 && "Unknown linkage type!");
248 cerr << "Unknown linkage type!\n";
249 abort();
250 }
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000251
252 // 3: Type, Size, Align
253 if (TAI->hasDotTypeDotSizeDirective()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 O << "\t.type\t" << name << ", @object\n";
255 O << "\t.size\t" << name << ", " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 }
257
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000258 EmitAlignment(Align, GVar);
259
260 O << name << ":\n";
261
262 // If the initializer is a extern weak symbol, remember to emit the weak
263 // reference!
264 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
265 if (GV->hasExternalWeakLinkage())
266 ExtWeakSymbols.insert(GV);
267
268 EmitGlobalConstant(C);
269 O << '\n';
270}
271
272bool AlphaAsmPrinter::doFinalization(Module &M) {
273 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
274 I != E; ++I)
275 printModuleLevelGV(I);
276
Dan Gohman4a558a32007-07-25 19:33:14 +0000277 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278}
279
280/// PrintAsmOperand - Print out an operand for an inline asm expression.
281///
282bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000283 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 const char *ExtraCode) {
285 printOperand(MI, OpNo);
286 return false;
287}
288
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000289bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000291 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 const char *ExtraCode) {
293 if (ExtraCode && ExtraCode[0])
294 return true; // Unknown modifier.
295 O << "0(";
296 printOperand(MI, OpNo);
297 O << ")";
298 return false;
299}