blob: ce07225030e8dca1dc4bf5a0401f0b395d46de40 [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"
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000027#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Support/Compiler.h"
Edwin Török2b331342009-07-08 19:04:27 +000029#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/Mangler.h"
David Greene302008d2009-07-14 20:18:05 +000031#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/ADT/Statistic.h"
33using namespace llvm;
34
35STATISTIC(EmittedInsts, "Number of machine instrs printed");
36
37namespace {
38 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 /// Unique incrementer for label values for referencing Global values.
40 ///
41
David Greene302008d2009-07-14 20:18:05 +000042 explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000043 const TargetAsmInfo *T, bool V)
44 : AsmPrinter(o, tm, T, V) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045
46 virtual const char *getPassName() const {
47 return "Alpha Assembly Printer";
48 }
49 bool printInstruction(const MachineInstr *MI);
50 void printOp(const MachineOperand &MO, bool IsCallOp = false);
51 void printOperand(const MachineInstr *MI, int opNum);
52 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000053 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 bool runOnMachineFunction(MachineFunction &F);
55 bool doInitialization(Module &M);
56 bool doFinalization(Module &M);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000057
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
59 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000060 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +000062 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 const char *ExtraCode);
64 };
65} // end of anonymous namespace
66
67/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
68/// assembly code for a MachineFunction to the given output stream,
69/// using the given target machine description. This should work
70/// regardless of whether the function is in SSA form.
71///
David Greene302008d2009-07-14 20:18:05 +000072FunctionPass *llvm::createAlphaCodePrinterPass(formatted_raw_ostream &o,
Bill Wendling4f405312009-02-24 08:30:20 +000073 TargetMachine &tm,
Bill Wendling58ed5d22009-04-29 00:15:41 +000074 bool verbose) {
Daniel Dunbarb10d2222009-07-01 01:48:54 +000075 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076}
77
78#include "AlphaGenAsmWriter.inc"
79
80void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
81{
82 const MachineOperand &MO = MI->getOperand(opNum);
83 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman1e57df32008-02-10 18:45:23 +000084 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
85 "Not physreg??");
Bill Wendling8eeb9792008-02-26 21:11:01 +000086 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000087 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000088 O << MO.getImm();
89 assert(MO.getImm() < (1 << 30));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 } else {
91 printOp(MO);
92 }
93}
94
95
96void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman1e57df32008-02-10 18:45:23 +000097 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098
99 switch (MO.getType()) {
100 case MachineOperand::MO_Register:
Bill Wendling8eeb9792008-02-26 21:11:01 +0000101 O << RI.get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 return;
103
104 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000105 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 return;
107
108 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000109 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 return;
111
112 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000113 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000114 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 return;
116
117 case MachineOperand::MO_ExternalSymbol:
118 O << MO.getSymbolName();
119 return;
120
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000121 case MachineOperand::MO_GlobalAddress:
122 O << Mang->getMangledName(MO.getGlobal());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124
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) {
Bill Wendling4f405312009-02-24 08:30:20 +0000140 this->MF = &MF;
141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 SetupMachineFunction(MF);
143 O << "\n\n";
144
145 // Print out constants referenced by the function
146 EmitConstantPool(MF.getConstantPool());
147
148 // Print out jump tables referenced by the function
149 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
150
151 // Print out labels for the function.
152 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000153 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000154
Bill Wendling25a8ae32009-06-30 22:38:32 +0000155 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000157 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola3b450b32009-01-15 21:51:46 +0000159 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000160 case Function::LinkerPrivateLinkage:
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)) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000191 llvm_unreachable("Unhandled instruction in asm writer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 }
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
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000221 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000222 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000223 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000224 return;
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000225 unsigned Size = TD->getTypeAllocSize(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:
Duncan Sandsb95df792009-03-11 20:14:15 +0000240 case GlobalValue::CommonLinkage:
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000241 O << TAI->getWeakRefDirective() << name << '\n';
242 break;
243 case GlobalValue::AppendingLinkage:
244 case GlobalValue::ExternalLinkage:
245 O << TAI->getGlobalDirective() << name << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 break;
247 case GlobalValue::InternalLinkage:
Rafael Espindola3b450b32009-01-15 21:51:46 +0000248 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000249 case GlobalValue::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 break;
251 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000252 llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 }
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000254
255 // 3: Type, Size, Align
256 if (TAI->hasDotTypeDotSizeDirective()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 O << "\t.type\t" << name << ", @object\n";
258 O << "\t.size\t" << name << ", " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 }
260
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000261 EmitAlignment(Align, GVar);
262
263 O << name << ":\n";
264
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000265 EmitGlobalConstant(C);
266 O << '\n';
267}
268
269bool AlphaAsmPrinter::doFinalization(Module &M) {
270 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
271 I != E; ++I)
272 printModuleLevelGV(I);
273
Dan Gohman4a558a32007-07-25 19:33:14 +0000274 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275}
276
277/// PrintAsmOperand - Print out an operand for an inline asm expression.
278///
279bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000280 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 const char *ExtraCode) {
282 printOperand(MI, OpNo);
283 return false;
284}
285
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000286bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000288 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 const char *ExtraCode) {
290 if (ExtraCode && ExtraCode[0])
291 return true; // Unknown modifier.
292 O << "0(";
293 printOperand(MI, OpNo);
294 O << ")";
295 return false;
296}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000297
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000298// Force static initialization.
299extern "C" void LLVMInitializeAlphaAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000300 TargetRegistry::RegisterAsmPrinter(TheAlphaTarget,
301 createAlphaCodePrinterPass);
302}