blob: f43a3941230748862cba04cd1800182ea4568018 [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"
Edwin Török2b331342009-07-08 19:04:27 +000028#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000030#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/ADT/Statistic.h"
32using namespace llvm;
33
34STATISTIC(EmittedInsts, "Number of machine instrs printed");
35
36namespace {
37 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038 /// Unique incrementer for label values for referencing Global values.
39 ///
40
Bill Wendling58ed5d22009-04-29 00:15:41 +000041 explicit AlphaAsmPrinter(raw_ostream &o, TargetMachine &tm,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000042 const TargetAsmInfo *T, bool V)
43 : AsmPrinter(o, tm, T, 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 Wendling58ed5d22009-04-29 00:15:41 +000073 bool verbose) {
Daniel Dunbarb10d2222009-07-01 01:48:54 +000074 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075}
76
77#include "AlphaGenAsmWriter.inc"
78
79void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
80{
81 const MachineOperand &MO = MI->getOperand(opNum);
82 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman1e57df32008-02-10 18:45:23 +000083 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
84 "Not physreg??");
Bill Wendling8eeb9792008-02-26 21:11:01 +000085 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000086 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000087 O << MO.getImm();
88 assert(MO.getImm() < (1 << 30));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 } else {
90 printOp(MO);
91 }
92}
93
94
95void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman1e57df32008-02-10 18:45:23 +000096 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097
98 switch (MO.getType()) {
99 case MachineOperand::MO_Register:
Bill Wendling8eeb9792008-02-26 21:11:01 +0000100 O << RI.get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 return;
102
103 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000104 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 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
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000120 case MachineOperand::MO_GlobalAddress:
121 O << Mang->getMangledName(MO.getGlobal());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123
124 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000125 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000126 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 return;
128
129 default:
130 O << "<unknown operand type: " << MO.getType() << ">";
131 return;
132 }
133}
134
135/// runOnMachineFunction - This uses the printMachineInstruction()
136/// method to print assembly for each instruction.
137///
138bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000139 this->MF = &MF;
140
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 SetupMachineFunction(MF);
142 O << "\n\n";
143
144 // Print out constants referenced by the function
145 EmitConstantPool(MF.getConstantPool());
146
147 // Print out jump tables referenced by the function
148 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
149
150 // Print out labels for the function.
151 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000152 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000153
Bill Wendling25a8ae32009-06-30 22:38:32 +0000154 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000156 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola3b450b32009-01-15 21:51:46 +0000158 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 break;
160 case Function::ExternalLinkage:
161 O << "\t.globl " << CurrentFnName << "\n";
162 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000163 case Function::WeakAnyLinkage:
164 case Function::WeakODRLinkage:
165 case Function::LinkOnceAnyLinkage:
166 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
168 break;
169 }
170
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000171 printVisibility(CurrentFnName, F->getVisibility());
172
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 O << "\t.ent " << CurrentFnName << "\n";
174
175 O << CurrentFnName << ":\n";
176
177 // Print out code for the function.
178 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
179 I != E; ++I) {
180 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000181 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 O << '\n';
183 }
184 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
185 II != E; ++II) {
186 // Print the assembly for the instruction.
187 ++EmittedInsts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 if (!printInstruction(II)) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000189 llvm_unreachable("Unhandled instruction in asm writer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 }
191 }
192 }
193
194 O << "\t.end " << CurrentFnName << "\n";
195
196 // We didn't modify anything.
197 return false;
198}
199
200bool AlphaAsmPrinter::doInitialization(Module &M)
201{
202 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
203 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
204 else
205 O << "\t.arch ev6\n";
206 O << "\t.set noat\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000207 return AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208}
209
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000210void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 const TargetData *TD = TM.getTargetData();
212
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000213 if (!GVar->hasInitializer()) return; // External global require no code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000215 // Check to see if this is a special global used by LLVM, if so, emit it.
216 if (EmitSpecialLLVMGlobal(GVar))
217 return;
218
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000219 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000220 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000221 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000222 return;
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000223 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000224 unsigned Align = TD->getPreferredAlignmentLog(GVar);
225
226 // 0: Switch to section
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000227 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000228
229 // 1: Check visibility
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000230 printVisibility(name, GVar->getVisibility());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000231
232 // 2: Kind
233 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000234 case GlobalValue::LinkOnceAnyLinkage:
235 case GlobalValue::LinkOnceODRLinkage:
236 case GlobalValue::WeakAnyLinkage:
237 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000238 case GlobalValue::CommonLinkage:
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000239 O << TAI->getWeakRefDirective() << name << '\n';
240 break;
241 case GlobalValue::AppendingLinkage:
242 case GlobalValue::ExternalLinkage:
243 O << TAI->getGlobalDirective() << name << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 break;
245 case GlobalValue::InternalLinkage:
Rafael Espindola3b450b32009-01-15 21:51:46 +0000246 case GlobalValue::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 break;
248 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000249 llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 }
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000251
252 // 3: Type, Size, Align
253 if (TAI->hasDotTypeDotSizeDirective()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 O << "\t.type\t" << name << ", @object\n";
255 O << "\t.size\t" << name << ", " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 }
257
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000258 EmitAlignment(Align, GVar);
259
260 O << name << ":\n";
261
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000262 EmitGlobalConstant(C);
263 O << '\n';
264}
265
266bool AlphaAsmPrinter::doFinalization(Module &M) {
267 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
268 I != E; ++I)
269 printModuleLevelGV(I);
270
Dan Gohman4a558a32007-07-25 19:33:14 +0000271 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272}
273
274/// PrintAsmOperand - Print out an operand for an inline asm expression.
275///
276bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000277 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 const char *ExtraCode) {
279 printOperand(MI, OpNo);
280 return false;
281}
282
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000283bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000285 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 const char *ExtraCode) {
287 if (ExtraCode && ExtraCode[0])
288 return true; // Unknown modifier.
289 O << "0(";
290 printOperand(MI, OpNo);
291 O << ")";
292 return false;
293}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000294
Bob Wilsonebbc1c42009-06-23 23:59:40 +0000295// Force static initialization.
296extern "C" void LLVMInitializeAlphaAsmPrinter() { }
Anton Korobeynikovbab832e2009-06-19 19:36:55 +0000297
298namespace {
299 static struct Register {
300 Register() {
301 AlphaTargetMachine::registerAsmPrinter(createAlphaCodePrinterPass);
302 }
303 } Registrator;
304}