blob: a4690a9adef99f7b7ef836f60c3fe468997c0624 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Target/TargetAsmInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000025#include "llvm/Target/TargetLoweringObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Target/TargetMachine.h"
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000027#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Support/Compiler.h"
Edwin Török2b331342009-07-08 19:04:27 +000029#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/Mangler.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 {
38 struct VISIBILITY_HIDDEN 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,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000043 const TargetAsmInfo *T, bool V)
44 : 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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 void printOp(const MachineOperand &MO, bool IsCallOp = false);
51 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerae982212009-07-21 18:38:57 +000052 void printBaseOffsetPair(const MachineInstr *MI, int i, bool brackets=true);
53 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 bool runOnMachineFunction(MachineFunction &F);
55 bool doInitialization(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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066#include "AlphaGenAsmWriter.inc"
67
68void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
69{
70 const MachineOperand &MO = MI->getOperand(opNum);
71 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman1e57df32008-02-10 18:45:23 +000072 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
73 "Not physreg??");
Bill Wendling8eeb9792008-02-26 21:11:01 +000074 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000075 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000076 O << MO.getImm();
77 assert(MO.getImm() < (1 << 30));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 } else {
79 printOp(MO);
80 }
81}
82
83
84void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman1e57df32008-02-10 18:45:23 +000085 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086
87 switch (MO.getType()) {
88 case MachineOperand::MO_Register:
Bill Wendling8eeb9792008-02-26 21:11:01 +000089 O << RI.get(MO.getReg()).AsmName;
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 Lattner6017d482007-12-30 23:10:15 +000097 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 return;
99
100 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000101 O << TAI->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:
110 O << Mang->getMangledName(MO.getGlobal());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112
113 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000114 O << TAI->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 Lattner2931fe42009-07-29 05:09:30 +0000141 SwitchToSection(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:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
158 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()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000171 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 O << '\n';
173 }
174 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
175 II != E; ++II) {
176 // Print the assembly for the instruction.
177 ++EmittedInsts;
Chris Lattner7d337492009-08-08 00:05:42 +0000178 printInstruction(II);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 }
180 }
181
182 O << "\t.end " << CurrentFnName << "\n";
183
184 // We didn't modify anything.
185 return false;
186}
187
188bool AlphaAsmPrinter::doInitialization(Module &M)
189{
190 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
191 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
192 else
193 O << "\t.arch ev6\n";
194 O << "\t.set noat\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000195 return AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196}
197
Chris Lattnerae982212009-07-21 18:38:57 +0000198void AlphaAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 const TargetData *TD = TM.getTargetData();
200
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000201 if (!GVar->hasInitializer()) return; // External global require no code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000203 // Check to see if this is a special global used by LLVM, if so, emit it.
204 if (EmitSpecialLLVMGlobal(GVar))
205 return;
206
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000207 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000208 Constant *C = GVar->getInitializer();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000209 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000210 unsigned Align = TD->getPreferredAlignmentLog(GVar);
211
212 // 0: Switch to section
Chris Lattner2931fe42009-07-29 05:09:30 +0000213 SwitchToSection(getObjFileLowering().SectionForGlobal(GVar, Mang, TM));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000214
215 // 1: Check visibility
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000216 printVisibility(name, GVar->getVisibility());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000217
218 // 2: Kind
219 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000220 case GlobalValue::LinkOnceAnyLinkage:
221 case GlobalValue::LinkOnceODRLinkage:
222 case GlobalValue::WeakAnyLinkage:
223 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000224 case GlobalValue::CommonLinkage:
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000225 O << TAI->getWeakRefDirective() << name << '\n';
226 break;
227 case GlobalValue::AppendingLinkage:
228 case GlobalValue::ExternalLinkage:
229 O << TAI->getGlobalDirective() << name << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 break;
231 case GlobalValue::InternalLinkage:
Rafael Espindola3b450b32009-01-15 21:51:46 +0000232 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000233 case GlobalValue::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 break;
235 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000236 llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 }
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000238
239 // 3: Type, Size, Align
240 if (TAI->hasDotTypeDotSizeDirective()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 O << "\t.type\t" << name << ", @object\n";
242 O << "\t.size\t" << name << ", " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 }
244
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000245 EmitAlignment(Align, GVar);
246
247 O << name << ":\n";
248
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000249 EmitGlobalConstant(C);
250 O << '\n';
251}
252
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253/// PrintAsmOperand - Print out an operand for an inline asm expression.
254///
255bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000256 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 const char *ExtraCode) {
258 printOperand(MI, OpNo);
259 return false;
260}
261
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000262bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000264 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 const char *ExtraCode) {
266 if (ExtraCode && ExtraCode[0])
267 return true; // Unknown modifier.
268 O << "0(";
269 printOperand(MI, OpNo);
270 O << ")";
271 return false;
272}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000273
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000274// Force static initialization.
275extern "C" void LLVMInitializeAlphaAsmPrinter() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000276 RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000277}