blob: 06232712da47956470e020f9da9912099dae53c0 [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,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000041 const TargetAsmInfo *T, CodeGenOpt::Level OL,
42 bool V)
Bill Wendling58ed5d22009-04-29 00:15:41 +000043 : AsmPrinter(o, tm, T, OL, V) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044
45 virtual const char *getPassName() const {
46 return "Alpha Assembly Printer";
47 }
48 bool printInstruction(const MachineInstr *MI);
49 void printOp(const MachineOperand &MO, bool IsCallOp = false);
50 void printOperand(const MachineInstr *MI, int opNum);
51 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000052 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 bool runOnMachineFunction(MachineFunction &F);
54 bool doInitialization(Module &M);
55 bool doFinalization(Module &M);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000056
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
58 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000059 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +000061 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 const char *ExtraCode);
63 };
64} // end of anonymous namespace
65
66/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
67/// assembly code for a MachineFunction to the given output stream,
68/// using the given target machine description. This should work
69/// regardless of whether the function is in SSA form.
70///
Owen Anderson847b99b2008-08-21 00:14:44 +000071FunctionPass *llvm::createAlphaCodePrinterPass(raw_ostream &o,
Bill Wendling4f405312009-02-24 08:30:20 +000072 TargetMachine &tm,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000073 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +000074 bool verbose) {
75 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, 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:
105 cerr << "printOp() does not handle immediate values\n";
106 abort();
107 return;
108
109 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000110 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 return;
112
113 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000114 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000115 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 return;
117
118 case MachineOperand::MO_ExternalSymbol:
119 O << MO.getSymbolName();
120 return;
121
122 case MachineOperand::MO_GlobalAddress: {
123 GlobalValue *GV = MO.getGlobal();
124 O << Mang->getValueName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 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
Bill Wendling25a8ae32009-06-30 22:38:32 +0000158 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 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();
Devang Patel880595f2009-06-26 02:26:12 +0000226 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000227 return;
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000228 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000229 unsigned Align = TD->getPreferredAlignmentLog(GVar);
230
231 // 0: Switch to section
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000232 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000233
234 // 1: Check visibility
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000235 printVisibility(name, GVar->getVisibility());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000236
237 // 2: Kind
238 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000239 case GlobalValue::LinkOnceAnyLinkage:
240 case GlobalValue::LinkOnceODRLinkage:
241 case GlobalValue::WeakAnyLinkage:
242 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000243 case GlobalValue::CommonLinkage:
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000244 O << TAI->getWeakRefDirective() << name << '\n';
245 break;
246 case GlobalValue::AppendingLinkage:
247 case GlobalValue::ExternalLinkage:
248 O << TAI->getGlobalDirective() << name << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 break;
250 case GlobalValue::InternalLinkage:
Rafael Espindola3b450b32009-01-15 21:51:46 +0000251 case GlobalValue::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 break;
253 default:
254 assert(0 && "Unknown linkage type!");
255 cerr << "Unknown linkage type!\n";
256 abort();
257 }
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000258
259 // 3: Type, Size, Align
260 if (TAI->hasDotTypeDotSizeDirective()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 O << "\t.type\t" << name << ", @object\n";
262 O << "\t.size\t" << name << ", " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 }
264
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000265 EmitAlignment(Align, GVar);
266
267 O << name << ":\n";
268
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000269 EmitGlobalConstant(C);
270 O << '\n';
271}
272
273bool AlphaAsmPrinter::doFinalization(Module &M) {
274 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
275 I != E; ++I)
276 printModuleLevelGV(I);
277
Dan Gohman4a558a32007-07-25 19:33:14 +0000278 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279}
280
281/// PrintAsmOperand - Print out an operand for an inline asm expression.
282///
283bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000284 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 const char *ExtraCode) {
286 printOperand(MI, OpNo);
287 return false;
288}
289
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000290bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000292 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 const char *ExtraCode) {
294 if (ExtraCode && ExtraCode[0])
295 return true; // Unknown modifier.
296 O << "0(";
297 printOperand(MI, OpNo);
298 O << ")";
299 return false;
300}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000301
Bob Wilsonebbc1c42009-06-23 23:59:40 +0000302// Force static initialization.
303extern "C" void LLVMInitializeAlphaAsmPrinter() { }
Anton Korobeynikovbab832e2009-06-19 19:36:55 +0000304
305namespace {
306 static struct Register {
307 Register() {
308 AlphaTargetMachine::registerAsmPrinter(createAlphaCodePrinterPass);
309 }
310 } Registrator;
311}