blob: 74d398b2724e2edb3457603da242d66bfc5b4859 [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"
Chris Lattner73266f92009-08-19 05:49:37 +000024#include "llvm/MC/MCStreamer.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000025#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc6f802d2009-09-13 17:14:04 +000026#include "llvm/MC/MCSymbol.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000027#include "llvm/Target/TargetLoweringObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Target/TargetMachine.h"
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000029#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/Compiler.h"
Edwin Török2b331342009-07-08 19:04:27 +000031#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Support/Mangler.h"
David Greene302008d2009-07-14 20:18:05 +000033#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/ADT/Statistic.h"
35using namespace llvm;
36
37STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
39namespace {
40 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 /// Unique incrementer for label values for referencing Global values.
42 ///
43
David Greene302008d2009-07-14 20:18:05 +000044 explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +000045 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +000046 : AsmPrinter(o, tm, T, V) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
48 virtual const char *getPassName() const {
49 return "Alpha Assembly Printer";
50 }
Chris Lattnerddb259a2009-08-08 01:32:19 +000051 void printInstruction(const MachineInstr *MI);
Chris Lattner92221692009-09-13 20:08:00 +000052 const char *getRegisterName(unsigned RegNo) const;
53
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 void printOp(const MachineOperand &MO, bool IsCallOp = false);
55 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerae982212009-07-21 18:38:57 +000056 void printBaseOffsetPair(const MachineInstr *MI, int i, bool brackets=true);
57 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 bool runOnMachineFunction(MachineFunction &F);
59 bool doInitialization(Module &M);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000060
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
62 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000063 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +000065 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 const char *ExtraCode);
67 };
68} // end of anonymous namespace
69
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070#include "AlphaGenAsmWriter.inc"
71
72void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
73{
74 const MachineOperand &MO = MI->getOperand(opNum);
75 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman1e57df32008-02-10 18:45:23 +000076 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
77 "Not physreg??");
Bill Wendling8eeb9792008-02-26 21:11:01 +000078 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000079 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000080 O << MO.getImm();
81 assert(MO.getImm() < (1 << 30));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 } else {
83 printOp(MO);
84 }
85}
86
87
88void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman1e57df32008-02-10 18:45:23 +000089 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090
91 switch (MO.getType()) {
92 case MachineOperand::MO_Register:
Bill Wendling8eeb9792008-02-26 21:11:01 +000093 O << RI.get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 return;
95
96 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +000097 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 return;
99
100 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000101 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 return;
103
104 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000105 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000106 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 return;
108
109 case MachineOperand::MO_ExternalSymbol:
110 O << MO.getSymbolName();
111 return;
112
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000113 case MachineOperand::MO_GlobalAddress:
114 O << Mang->getMangledName(MO.getGlobal());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116
117 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000118 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000119 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 return;
121
122 default:
123 O << "<unknown operand type: " << MO.getType() << ">";
124 return;
125 }
126}
127
128/// runOnMachineFunction - This uses the printMachineInstruction()
129/// method to print assembly for each instruction.
130///
131bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000132 this->MF = &MF;
133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 SetupMachineFunction(MF);
135 O << "\n\n";
136
137 // Print out constants referenced by the function
138 EmitConstantPool(MF.getConstantPool());
139
140 // Print out jump tables referenced by the function
141 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
142
143 // Print out labels for the function.
144 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000145 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000146
Bill Wendling25a8ae32009-06-30 22:38:32 +0000147 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000149 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola3b450b32009-01-15 21:51:46 +0000151 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000152 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 break;
154 case Function::ExternalLinkage:
155 O << "\t.globl " << CurrentFnName << "\n";
156 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000157 case Function::WeakAnyLinkage:
158 case Function::WeakODRLinkage:
159 case Function::LinkOnceAnyLinkage:
160 case Function::LinkOnceODRLinkage:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000161 O << MAI->getWeakRefDirective() << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 break;
163 }
164
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000165 printVisibility(CurrentFnName, F->getVisibility());
166
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 O << "\t.ent " << CurrentFnName << "\n";
168
169 O << CurrentFnName << ":\n";
170
171 // Print out code for the function.
172 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
173 I != E; ++I) {
174 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000175 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 O << '\n';
177 }
178 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
179 II != E; ++II) {
180 // Print the assembly for the instruction.
181 ++EmittedInsts;
Chris Lattner32d4cc72009-09-09 23:14:36 +0000182 processDebugLoc(II->getDebugLoc());
183
Chris Lattner7d337492009-08-08 00:05:42 +0000184 printInstruction(II);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000185
186 if (VerboseAsm && !II->getDebugLoc().isUnknown())
187 EmitComments(*II);
188 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 }
190 }
191
192 O << "\t.end " << CurrentFnName << "\n";
193
194 // We didn't modify anything.
195 return false;
196}
197
198bool AlphaAsmPrinter::doInitialization(Module &M)
199{
200 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
201 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
202 else
203 O << "\t.arch ev6\n";
204 O << "\t.set noat\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000205 return AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206}
207
Chris Lattnerae982212009-07-21 18:38:57 +0000208void AlphaAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 const TargetData *TD = TM.getTargetData();
210
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000211 if (!GVar->hasInitializer()) return; // External global require no code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000213 // Check to see if this is a special global used by LLVM, if so, emit it.
214 if (EmitSpecialLLVMGlobal(GVar))
215 return;
216
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000217 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000218 Constant *C = GVar->getInitializer();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000219 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000220 unsigned Align = TD->getPreferredAlignmentLog(GVar);
221
222 // 0: Switch to section
Chris Lattner73266f92009-08-19 05:49:37 +0000223 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
224 TM));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000225
226 // 1: Check visibility
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000227 printVisibility(name, GVar->getVisibility());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000228
229 // 2: Kind
230 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000231 case GlobalValue::LinkOnceAnyLinkage:
232 case GlobalValue::LinkOnceODRLinkage:
233 case GlobalValue::WeakAnyLinkage:
234 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000235 case GlobalValue::CommonLinkage:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000236 O << MAI->getWeakRefDirective() << name << '\n';
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000237 break;
238 case GlobalValue::AppendingLinkage:
239 case GlobalValue::ExternalLinkage:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000240 O << MAI->getGlobalDirective() << name << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 break;
242 case GlobalValue::InternalLinkage:
Rafael Espindola3b450b32009-01-15 21:51:46 +0000243 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000244 case GlobalValue::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 break;
246 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000247 llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 }
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000249
250 // 3: Type, Size, Align
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000251 if (MAI->hasDotTypeDotSizeDirective()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 O << "\t.type\t" << name << ", @object\n";
253 O << "\t.size\t" << name << ", " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 }
255
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000256 EmitAlignment(Align, GVar);
257
258 O << name << ":\n";
259
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000260 EmitGlobalConstant(C);
261 O << '\n';
262}
263
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264/// PrintAsmOperand - Print out an operand for an inline asm expression.
265///
266bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000267 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 const char *ExtraCode) {
269 printOperand(MI, OpNo);
270 return false;
271}
272
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000273bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000275 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 const char *ExtraCode) {
277 if (ExtraCode && ExtraCode[0])
278 return true; // Unknown modifier.
279 O << "0(";
280 printOperand(MI, OpNo);
281 O << ")";
282 return false;
283}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000284
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000285// Force static initialization.
286extern "C" void LLVMInitializeAlphaAsmPrinter() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000287 RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000288}