blob: 338057b453158bec4aa49cf8e8433726b4ae3be9 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/Support/Mangler.h"
David Greene302008d2009-07-14 20:18:05 +000032#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/ADT/Statistic.h"
34using namespace llvm;
35
36STATISTIC(EmittedInsts, "Number of machine instrs printed");
37
38namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000039 struct AlphaAsmPrinter : public AsmPrinter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 /// Unique incrementer for label values for referencing Global values.
41 ///
42
David Greene302008d2009-07-14 20:18:05 +000043 explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +000044 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +000045 : AsmPrinter(o, tm, T, V) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046
47 virtual const char *getPassName() const {
48 return "Alpha Assembly Printer";
49 }
Chris Lattnerddb259a2009-08-08 01:32:19 +000050 void printInstruction(const MachineInstr *MI);
Chris Lattner213703c2009-09-13 20:19:22 +000051 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +000052
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 void printOp(const MachineOperand &MO, bool IsCallOp = false);
54 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerae982212009-07-21 18:38:57 +000055 void printBaseOffsetPair(const MachineInstr *MI, int i, bool brackets=true);
56 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 bool runOnMachineFunction(MachineFunction &F);
Bob Wilsonb5f835e2009-09-30 22:06:26 +000058 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000059
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
61 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000062 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +000064 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 const char *ExtraCode);
66 };
67} // end of anonymous namespace
68
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069#include "AlphaGenAsmWriter.inc"
70
71void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
72{
73 const MachineOperand &MO = MI->getOperand(opNum);
74 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman1e57df32008-02-10 18:45:23 +000075 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
76 "Not physreg??");
Chris Lattnerf0a25de2009-09-13 20:31:40 +000077 O << getRegisterName(MO.getReg());
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000078 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000079 O << MO.getImm();
80 assert(MO.getImm() < (1 << 30));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 } else {
82 printOp(MO);
83 }
84}
85
86
87void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 switch (MO.getType()) {
89 case MachineOperand::MO_Register:
Chris Lattnerf0a25de2009-09-13 20:31:40 +000090 O << getRegisterName(MO.getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 return;
92
93 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +000094 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 return;
96
97 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +000098 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 return;
100
101 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000102 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000103 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 return;
105
106 case MachineOperand::MO_ExternalSymbol:
107 O << MO.getSymbolName();
108 return;
109
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000110 case MachineOperand::MO_GlobalAddress:
111 O << Mang->getMangledName(MO.getGlobal());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113
114 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000115 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000116 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 return;
118
119 default:
120 O << "<unknown operand type: " << MO.getType() << ">";
121 return;
122 }
123}
124
125/// runOnMachineFunction - This uses the printMachineInstruction()
126/// method to print assembly for each instruction.
127///
128bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000129 this->MF = &MF;
130
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 SetupMachineFunction(MF);
132 O << "\n\n";
133
134 // Print out constants referenced by the function
135 EmitConstantPool(MF.getConstantPool());
136
137 // Print out jump tables referenced by the function
138 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
139
140 // Print out labels for the function.
141 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000142 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000143
Bill Wendling25a8ae32009-06-30 22:38:32 +0000144 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000146 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola3b450b32009-01-15 21:51:46 +0000148 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000149 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 break;
151 case Function::ExternalLinkage:
152 O << "\t.globl " << CurrentFnName << "\n";
153 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000154 case Function::WeakAnyLinkage:
155 case Function::WeakODRLinkage:
156 case Function::LinkOnceAnyLinkage:
157 case Function::LinkOnceODRLinkage:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000158 O << MAI->getWeakRefDirective() << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 break;
160 }
161
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000162 printVisibility(CurrentFnName, F->getVisibility());
163
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 O << "\t.ent " << CurrentFnName << "\n";
165
166 O << CurrentFnName << ":\n";
167
168 // Print out code for the function.
169 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
170 I != E; ++I) {
171 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000172 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 }
174 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
175 II != E; ++II) {
176 // Print the assembly for the instruction.
177 ++EmittedInsts;
Devang Patel5450fc12009-10-06 02:19:11 +0000178 processDebugLoc(II, true);
Chris Lattner7d337492009-08-08 00:05:42 +0000179 printInstruction(II);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000180
David Greeneca9b04b2009-11-13 21:34:57 +0000181 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000182 EmitComments(*II);
183 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000184 processDebugLoc(II, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 }
186 }
187
188 O << "\t.end " << CurrentFnName << "\n";
189
190 // We didn't modify anything.
191 return false;
192}
193
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000194void AlphaAsmPrinter::EmitStartOfAsmFile(Module &M) {
195 if (TM.getSubtarget<AlphaSubtarget>().hasCT())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
197 else
198 O << "\t.arch ev6\n";
199 O << "\t.set noat\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200}
201
Chris Lattnerae982212009-07-21 18:38:57 +0000202void AlphaAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 const TargetData *TD = TM.getTargetData();
204
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000205 if (!GVar->hasInitializer()) return; // External global require no code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000207 // Check to see if this is a special global used by LLVM, if so, emit it.
208 if (EmitSpecialLLVMGlobal(GVar))
209 return;
210
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000211 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000212 Constant *C = GVar->getInitializer();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000213 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000214 unsigned Align = TD->getPreferredAlignmentLog(GVar);
215
216 // 0: Switch to section
Chris Lattner73266f92009-08-19 05:49:37 +0000217 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
218 TM));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000219
220 // 1: Check visibility
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000221 printVisibility(name, GVar->getVisibility());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000222
223 // 2: Kind
224 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000225 case GlobalValue::LinkOnceAnyLinkage:
226 case GlobalValue::LinkOnceODRLinkage:
227 case GlobalValue::WeakAnyLinkage:
228 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000229 case GlobalValue::CommonLinkage:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000230 O << MAI->getWeakRefDirective() << name << '\n';
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000231 break;
232 case GlobalValue::AppendingLinkage:
233 case GlobalValue::ExternalLinkage:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000234 O << MAI->getGlobalDirective() << name << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 break;
236 case GlobalValue::InternalLinkage:
Rafael Espindola3b450b32009-01-15 21:51:46 +0000237 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000238 case GlobalValue::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 break;
240 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000241 llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 }
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000243
244 // 3: Type, Size, Align
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000245 if (MAI->hasDotTypeDotSizeDirective()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 O << "\t.type\t" << name << ", @object\n";
247 O << "\t.size\t" << name << ", " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 }
249
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000250 EmitAlignment(Align, GVar);
251
252 O << name << ":\n";
253
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000254 EmitGlobalConstant(C);
255 O << '\n';
256}
257
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258/// PrintAsmOperand - Print out an operand for an inline asm expression.
259///
260bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000261 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 const char *ExtraCode) {
263 printOperand(MI, OpNo);
264 return false;
265}
266
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000267bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000269 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 const char *ExtraCode) {
271 if (ExtraCode && ExtraCode[0])
272 return true; // Unknown modifier.
273 O << "0(";
274 printOperand(MI, OpNo);
275 O << ")";
276 return false;
277}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000278
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000279// Force static initialization.
280extern "C" void LLVMInitializeAlphaAsmPrinter() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000281 RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000282}