blob: 982ef5e851948838e192f391219d434472dc0da2 [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"
Devang Patelf667ab42009-06-25 00:47:42 +000020#include "llvm/MDNode.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Type.h"
22#include "llvm/Assembly/Writer.h"
23#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000024#include "llvm/CodeGen/DwarfWriter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Target/TargetAsmInfo.h"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/Compiler.h"
28#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/ADT/Statistic.h"
31using namespace llvm;
32
33STATISTIC(EmittedInsts, "Number of machine instrs printed");
34
35namespace {
36 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037 /// Unique incrementer for label values for referencing Global values.
38 ///
39
Bill Wendling58ed5d22009-04-29 00:15:41 +000040 explicit AlphaAsmPrinter(raw_ostream &o, TargetMachine &tm,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000041 const TargetAsmInfo *T, bool V)
42 : AsmPrinter(o, tm, T, V) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043
44 virtual const char *getPassName() const {
45 return "Alpha Assembly Printer";
46 }
47 bool printInstruction(const MachineInstr *MI);
48 void printOp(const MachineOperand &MO, bool IsCallOp = false);
49 void printOperand(const MachineInstr *MI, int opNum);
50 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000051 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 bool runOnMachineFunction(MachineFunction &F);
53 bool doInitialization(Module &M);
54 bool doFinalization(Module &M);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000055
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
57 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000058 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +000060 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 const char *ExtraCode);
62 };
63} // end of anonymous namespace
64
65/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
66/// assembly code for a MachineFunction to the given output stream,
67/// using the given target machine description. This should work
68/// regardless of whether the function is in SSA form.
69///
Owen Anderson847b99b2008-08-21 00:14:44 +000070FunctionPass *llvm::createAlphaCodePrinterPass(raw_ostream &o,
Bill Wendling4f405312009-02-24 08:30:20 +000071 TargetMachine &tm,
Bill Wendling58ed5d22009-04-29 00:15:41 +000072 bool verbose) {
Daniel Dunbarb10d2222009-07-01 01:48:54 +000073 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), 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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 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) {
Bill Wendling4f405312009-02-24 08:30:20 +0000141 this->MF = &MF;
142
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 SetupMachineFunction(MF);
144 O << "\n\n";
145
146 // Print out constants referenced by the function
147 EmitConstantPool(MF.getConstantPool());
148
149 // Print out jump tables referenced by the function
150 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
151
152 // Print out labels for the function.
153 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000154 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000155
Bill Wendling25a8ae32009-06-30 22:38:32 +0000156 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 switch (F->getLinkage()) {
158 default: assert(0 && "Unknown linkage type!");
159 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola3b450b32009-01-15 21:51:46 +0000160 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 break;
162 case Function::ExternalLinkage:
163 O << "\t.globl " << CurrentFnName << "\n";
164 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000165 case Function::WeakAnyLinkage:
166 case Function::WeakODRLinkage:
167 case Function::LinkOnceAnyLinkage:
168 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
170 break;
171 }
172
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000173 printVisibility(CurrentFnName, F->getVisibility());
174
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 O << "\t.ent " << CurrentFnName << "\n";
176
177 O << CurrentFnName << ":\n";
178
179 // Print out code for the function.
180 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
181 I != E; ++I) {
182 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000183 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 O << '\n';
185 }
186 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
187 II != E; ++II) {
188 // Print the assembly for the instruction.
189 ++EmittedInsts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 if (!printInstruction(II)) {
191 assert(0 && "Unhandled instruction in asm writer!");
192 abort();
193 }
194 }
195 }
196
197 O << "\t.end " << CurrentFnName << "\n";
198
199 // We didn't modify anything.
200 return false;
201}
202
203bool AlphaAsmPrinter::doInitialization(Module &M)
204{
205 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
206 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
207 else
208 O << "\t.arch ev6\n";
209 O << "\t.set noat\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000210 return AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211}
212
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000213void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 const TargetData *TD = TM.getTargetData();
215
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000216 if (!GVar->hasInitializer()) return; // External global require no code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000218 // Check to see if this is a special global used by LLVM, if so, emit it.
219 if (EmitSpecialLLVMGlobal(GVar))
220 return;
221
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000222 std::string name = Mang->getValueName(GVar);
223 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000224 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000225 return;
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000226 unsigned Size = TD->getTypeAllocSize(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
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000267 EmitGlobalConstant(C);
268 O << '\n';
269}
270
271bool AlphaAsmPrinter::doFinalization(Module &M) {
272 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
273 I != E; ++I)
274 printModuleLevelGV(I);
275
Dan Gohman4a558a32007-07-25 19:33:14 +0000276 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277}
278
279/// PrintAsmOperand - Print out an operand for an inline asm expression.
280///
281bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000282 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 const char *ExtraCode) {
284 printOperand(MI, OpNo);
285 return false;
286}
287
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000288bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000290 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 const char *ExtraCode) {
292 if (ExtraCode && ExtraCode[0])
293 return true; // Unknown modifier.
294 O << "0(";
295 printOperand(MI, OpNo);
296 O << ")";
297 return false;
298}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000299
Bob Wilsonebbc1c42009-06-23 23:59:40 +0000300// Force static initialization.
301extern "C" void LLVMInitializeAlphaAsmPrinter() { }
Anton Korobeynikovbab832e2009-06-19 19:36:55 +0000302
303namespace {
304 static struct Register {
305 Register() {
306 AlphaTargetMachine::registerAsmPrinter(createAlphaCodePrinterPass);
307 }
308 } Registrator;
309}