blob: 5238b79d9d201f17d76e2e43bbda91cfa60cc0ac [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;
Duncan Sands19d161f2009-03-07 15:45:40 +0000166 case Function::WeakAnyLinkage:
167 case Function::WeakODRLinkage:
168 case Function::LinkOnceAnyLinkage:
169 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
171 break;
172 }
173
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000174 printVisibility(CurrentFnName, F->getVisibility());
175
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 O << "\t.ent " << CurrentFnName << "\n";
177
178 O << CurrentFnName << ":\n";
179
180 // Print out code for the function.
181 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
182 I != E; ++I) {
183 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000184 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 O << '\n';
186 }
187 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
188 II != E; ++II) {
189 // Print the assembly for the instruction.
190 ++EmittedInsts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 if (!printInstruction(II)) {
192 assert(0 && "Unhandled instruction in asm writer!");
193 abort();
194 }
195 }
196 }
197
198 O << "\t.end " << CurrentFnName << "\n";
199
200 // We didn't modify anything.
201 return false;
202}
203
204bool AlphaAsmPrinter::doInitialization(Module &M)
205{
206 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
207 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
208 else
209 O << "\t.arch ev6\n";
210 O << "\t.set noat\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000211 return AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212}
213
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000214void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 const TargetData *TD = TM.getTargetData();
216
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000217 if (!GVar->hasInitializer()) return; // External global require no code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000219 // Check to see if this is a special global used by LLVM, if so, emit it.
220 if (EmitSpecialLLVMGlobal(GVar))
221 return;
222
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000223 std::string name = Mang->getValueName(GVar);
224 Constant *C = GVar->getInitializer();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000225 unsigned Size = TD->getTypePaddedSize(C->getType());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000226 unsigned Align = TD->getPreferredAlignmentLog(GVar);
227
228 // 0: Switch to section
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000229 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000230
231 // 1: Check visibility
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000232 printVisibility(name, GVar->getVisibility());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000233
234 // 2: Kind
235 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000236 case GlobalValue::LinkOnceAnyLinkage:
237 case GlobalValue::LinkOnceODRLinkage:
238 case GlobalValue::WeakAnyLinkage:
239 case GlobalValue::WeakODRLinkage:
240 case GlobalValue::CommonAnyLinkage:
241 case GlobalValue::CommonODRLinkage:
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000242 O << TAI->getWeakRefDirective() << name << '\n';
243 break;
244 case GlobalValue::AppendingLinkage:
245 case GlobalValue::ExternalLinkage:
246 O << TAI->getGlobalDirective() << name << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 break;
248 case GlobalValue::InternalLinkage:
Rafael Espindola3b450b32009-01-15 21:51:46 +0000249 case GlobalValue::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 break;
251 default:
252 assert(0 && "Unknown linkage type!");
253 cerr << "Unknown linkage type!\n";
254 abort();
255 }
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000256
257 // 3: Type, Size, Align
258 if (TAI->hasDotTypeDotSizeDirective()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 O << "\t.type\t" << name << ", @object\n";
260 O << "\t.size\t" << name << ", " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 }
262
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000263 EmitAlignment(Align, GVar);
264
265 O << name << ":\n";
266
267 // If the initializer is a extern weak symbol, remember to emit the weak
268 // reference!
269 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
270 if (GV->hasExternalWeakLinkage())
271 ExtWeakSymbols.insert(GV);
272
273 EmitGlobalConstant(C);
274 O << '\n';
275}
276
277bool AlphaAsmPrinter::doFinalization(Module &M) {
278 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
279 I != E; ++I)
280 printModuleLevelGV(I);
281
Dan Gohman4a558a32007-07-25 19:33:14 +0000282 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283}
284
285/// PrintAsmOperand - Print out an operand for an inline asm expression.
286///
287bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000288 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 const char *ExtraCode) {
290 printOperand(MI, OpNo);
291 return false;
292}
293
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000294bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000296 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 const char *ExtraCode) {
298 if (ExtraCode && ExtraCode[0])
299 return true; // Unknown modifier.
300 O << "0(";
301 printOperand(MI, OpNo);
302 O << ")";
303 return false;
304}