blob: 235b16da0633a5a87cbef823f27cdaa72612ab4b [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"
Edwin Török2b331342009-07-08 19:04:27 +000030#include "llvm/Support/ErrorHandling.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 {
Nick Lewycky492d06e2009-10-25 06:33:48 +000038 struct 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,
Chris Lattner621c44d2009-08-22 20:48:53 +000043 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +000044 : 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 }
Chris Lattnerddb259a2009-08-08 01:32:19 +000049 void printInstruction(const MachineInstr *MI);
Chris Lattner213703c2009-09-13 20:19:22 +000050 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +000051
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 void printOp(const MachineOperand &MO, bool IsCallOp = false);
53 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerae982212009-07-21 18:38:57 +000054 void printBaseOffsetPair(const MachineInstr *MI, int i, bool brackets=true);
55 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 bool runOnMachineFunction(MachineFunction &F);
Bob Wilsonb5f835e2009-09-30 22:06:26 +000057 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000058
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
60 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000061 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +000063 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 const char *ExtraCode);
65 };
66} // end of anonymous namespace
67
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068#include "AlphaGenAsmWriter.inc"
69
70void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
71{
72 const MachineOperand &MO = MI->getOperand(opNum);
73 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman1e57df32008-02-10 18:45:23 +000074 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
75 "Not physreg??");
Chris Lattnerf0a25de2009-09-13 20:31:40 +000076 O << getRegisterName(MO.getReg());
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000077 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000078 O << MO.getImm();
79 assert(MO.getImm() < (1 << 30));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 } else {
81 printOp(MO);
82 }
83}
84
85
86void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 switch (MO.getType()) {
88 case MachineOperand::MO_Register:
Chris Lattnerf0a25de2009-09-13 20:31:40 +000089 O << getRegisterName(MO.getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 return;
91
92 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +000093 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 return;
95
96 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +000097 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 return;
99
100 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000101 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000102 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 return;
104
105 case MachineOperand::MO_ExternalSymbol:
106 O << MO.getSymbolName();
107 return;
108
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000109 case MachineOperand::MO_GlobalAddress:
Chris Lattner8d656d92010-01-15 23:55:16 +0000110 GetGlobalValueSymbol(MO.getGlobal())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112
113 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000114 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000115 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 return;
117
118 default:
119 O << "<unknown operand type: " << MO.getType() << ">";
120 return;
121 }
122}
123
124/// runOnMachineFunction - This uses the printMachineInstruction()
125/// method to print assembly for each instruction.
126///
127bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000128 this->MF = &MF;
129
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 SetupMachineFunction(MF);
131 O << "\n\n";
132
133 // Print out constants referenced by the function
134 EmitConstantPool(MF.getConstantPool());
135
136 // Print out jump tables referenced by the function
137 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
138
139 // Print out labels for the function.
140 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000141 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000142
Bill Wendling25a8ae32009-06-30 22:38:32 +0000143 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000145 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola3b450b32009-01-15 21:51:46 +0000147 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000148 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 break;
150 case Function::ExternalLinkage:
151 O << "\t.globl " << CurrentFnName << "\n";
152 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000153 case Function::WeakAnyLinkage:
154 case Function::WeakODRLinkage:
155 case Function::LinkOnceAnyLinkage:
156 case Function::LinkOnceODRLinkage:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000157 O << MAI->getWeakRefDirective() << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 break;
159 }
160
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000161 printVisibility(CurrentFnName, F->getVisibility());
162
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 O << "\t.ent " << CurrentFnName << "\n";
164
165 O << CurrentFnName << ":\n";
166
167 // Print out code for the function.
168 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
169 I != E; ++I) {
170 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000171 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 }
173 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
174 II != E; ++II) {
175 // Print the assembly for the instruction.
176 ++EmittedInsts;
Devang Patel5450fc12009-10-06 02:19:11 +0000177 processDebugLoc(II, true);
Chris Lattner7d337492009-08-08 00:05:42 +0000178 printInstruction(II);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000179
David Greeneca9b04b2009-11-13 21:34:57 +0000180 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000181 EmitComments(*II);
182 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000183 processDebugLoc(II, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 }
185 }
186
187 O << "\t.end " << CurrentFnName << "\n";
188
189 // We didn't modify anything.
190 return false;
191}
192
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000193void AlphaAsmPrinter::EmitStartOfAsmFile(Module &M) {
194 if (TM.getSubtarget<AlphaSubtarget>().hasCT())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
196 else
197 O << "\t.arch ev6\n";
198 O << "\t.set noat\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199}
200
Chris Lattnerae982212009-07-21 18:38:57 +0000201void AlphaAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 const TargetData *TD = TM.getTargetData();
203
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000204 if (!GVar->hasInitializer()) return; // External global require no code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000206 // Check to see if this is a special global used by LLVM, if so, emit it.
207 if (EmitSpecialLLVMGlobal(GVar))
208 return;
209
Chris Lattner8d656d92010-01-15 23:55:16 +0000210 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000211 Constant *C = GVar->getInitializer();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000212 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000213 unsigned Align = TD->getPreferredAlignmentLog(GVar);
214
215 // 0: Switch to section
Chris Lattner73266f92009-08-19 05:49:37 +0000216 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
217 TM));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000218
219 // 1: Check visibility
Chris Lattner8d656d92010-01-15 23:55:16 +0000220 printVisibility(GVarSym, GVar->getVisibility());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000221
222 // 2: Kind
223 switch (GVar->getLinkage()) {
Chris Lattner8d656d92010-01-15 23:55:16 +0000224 case GlobalValue::LinkOnceAnyLinkage:
225 case GlobalValue::LinkOnceODRLinkage:
226 case GlobalValue::WeakAnyLinkage:
227 case GlobalValue::WeakODRLinkage:
228 case GlobalValue::CommonLinkage:
229 O << MAI->getWeakRefDirective();
230 GVarSym->print(O, MAI);
231 O << '\n';
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000232 break;
Chris Lattner8d656d92010-01-15 23:55:16 +0000233 case GlobalValue::AppendingLinkage:
234 case GlobalValue::ExternalLinkage:
235 O << MAI->getGlobalDirective();
236 GVarSym->print(O, MAI);
237 O << '\n';
238 break;
239 case GlobalValue::InternalLinkage:
240 case GlobalValue::PrivateLinkage:
241 case GlobalValue::LinkerPrivateLinkage:
242 break;
243 default:
244 llvm_unreachable("Unknown linkage type!");
245 }
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000246
247 // 3: Type, Size, Align
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000248 if (MAI->hasDotTypeDotSizeDirective()) {
Chris Lattner8d656d92010-01-15 23:55:16 +0000249 O << "\t.type\t";
250 GVarSym->print(O, MAI);
251 O << ", @object\n";
252 O << "\t.size\t";
253 GVarSym->print(O, MAI);
254 O << ", " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 }
256
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000257 EmitAlignment(Align, GVar);
Chris Lattner8d656d92010-01-15 23:55:16 +0000258
259 GVarSym->print(O, MAI);
260 O << ":\n";
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000261
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000262 EmitGlobalConstant(C);
263 O << '\n';
264}
265
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266/// PrintAsmOperand - Print out an operand for an inline asm expression.
267///
268bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000269 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 const char *ExtraCode) {
271 printOperand(MI, OpNo);
272 return false;
273}
274
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000275bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000277 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 const char *ExtraCode) {
279 if (ExtraCode && ExtraCode[0])
280 return true; // Unknown modifier.
281 O << "0(";
282 printOperand(MI, OpNo);
283 O << ")";
284 return false;
285}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000286
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000287// Force static initialization.
288extern "C" void LLVMInitializeAlphaAsmPrinter() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000289 RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000290}