blob: 292a380595f23286a948aef3daf68ff865ed955d [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 Wendling58ed5d22009-04-29 00:15:41 +000039 explicit AlphaAsmPrinter(raw_ostream &o, TargetMachine &tm,
40 const TargetAsmInfo *T, unsigned OL, bool V)
41 : AsmPrinter(o, tm, T, OL, V) {}
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,
Bill Wendling58ed5d22009-04-29 00:15:41 +000071 unsigned OptLevel,
72 bool verbose) {
73 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074}
75
76#include "AlphaGenAsmWriter.inc"
77
78void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
79{
80 const MachineOperand &MO = MI->getOperand(opNum);
81 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman1e57df32008-02-10 18:45:23 +000082 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
83 "Not physreg??");
Bill Wendling8eeb9792008-02-26 21:11:01 +000084 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000085 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000086 O << MO.getImm();
87 assert(MO.getImm() < (1 << 30));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 } else {
89 printOp(MO);
90 }
91}
92
93
94void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman1e57df32008-02-10 18:45:23 +000095 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
97 switch (MO.getType()) {
98 case MachineOperand::MO_Register:
Bill Wendling8eeb9792008-02-26 21:11:01 +000099 O << RI.get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 return;
101
102 case MachineOperand::MO_Immediate:
103 cerr << "printOp() does not handle immediate values\n";
104 abort();
105 return;
106
107 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000108 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 return;
110
111 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000112 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000113 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 return;
115
116 case MachineOperand::MO_ExternalSymbol:
117 O << MO.getSymbolName();
118 return;
119
120 case MachineOperand::MO_GlobalAddress: {
121 GlobalValue *GV = MO.getGlobal();
122 O << Mang->getValueName(GV);
123 if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
124 ExtWeakSymbols.insert(GV);
125 return;
126 }
127
128 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000129 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000130 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 return;
132
133 default:
134 O << "<unknown operand type: " << MO.getType() << ">";
135 return;
136 }
137}
138
139/// runOnMachineFunction - This uses the printMachineInstruction()
140/// method to print assembly for each instruction.
141///
142bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000143 this->MF = &MF;
144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 SetupMachineFunction(MF);
146 O << "\n\n";
147
148 // Print out constants referenced by the function
149 EmitConstantPool(MF.getConstantPool());
150
151 // Print out jump tables referenced by the function
152 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
153
154 // Print out labels for the function.
155 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000156 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 EmitAlignment(4, F);
159 switch (F->getLinkage()) {
160 default: assert(0 && "Unknown linkage type!");
161 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola3b450b32009-01-15 21:51:46 +0000162 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 break;
164 case Function::ExternalLinkage:
165 O << "\t.globl " << CurrentFnName << "\n";
166 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000167 case Function::WeakAnyLinkage:
168 case Function::WeakODRLinkage:
169 case Function::LinkOnceAnyLinkage:
170 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
172 break;
173 }
174
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000175 printVisibility(CurrentFnName, F->getVisibility());
176
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 O << "\t.ent " << CurrentFnName << "\n";
178
179 O << CurrentFnName << ":\n";
180
181 // Print out code for the function.
182 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
183 I != E; ++I) {
184 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000185 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 O << '\n';
187 }
188 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
189 II != E; ++II) {
190 // Print the assembly for the instruction.
191 ++EmittedInsts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 if (!printInstruction(II)) {
193 assert(0 && "Unhandled instruction in asm writer!");
194 abort();
195 }
196 }
197 }
198
199 O << "\t.end " << CurrentFnName << "\n";
200
201 // We didn't modify anything.
202 return false;
203}
204
205bool AlphaAsmPrinter::doInitialization(Module &M)
206{
207 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
208 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
209 else
210 O << "\t.arch ev6\n";
211 O << "\t.set noat\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000212 return AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213}
214
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000215void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 const TargetData *TD = TM.getTargetData();
217
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000218 if (!GVar->hasInitializer()) return; // External global require no code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000220 // Check to see if this is a special global used by LLVM, if so, emit it.
221 if (EmitSpecialLLVMGlobal(GVar))
222 return;
223
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000224 std::string name = Mang->getValueName(GVar);
225 Constant *C = GVar->getInitializer();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000226 unsigned Size = TD->getTypePaddedSize(C->getType());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000227 unsigned Align = TD->getPreferredAlignmentLog(GVar);
228
229 // 0: Switch to section
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000230 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000231
232 // 1: Check visibility
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000233 printVisibility(name, GVar->getVisibility());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000234
235 // 2: Kind
236 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000237 case GlobalValue::LinkOnceAnyLinkage:
238 case GlobalValue::LinkOnceODRLinkage:
239 case GlobalValue::WeakAnyLinkage:
240 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000241 case GlobalValue::CommonLinkage:
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}