blob: f0b7fd7f615b5a016b172757d32bc544d4cd8fc3 [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);
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);
57 bool doInitialization(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??");
Bill Wendling8eeb9792008-02-26 21:11:01 +000076 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
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 Gohman1e57df32008-02-10 18:45:23 +000087 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088
89 switch (MO.getType()) {
90 case MachineOperand::MO_Register:
Bill Wendling8eeb9792008-02-26 21:11:01 +000091 O << RI.get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 return;
93
94 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +000095 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 return;
97
98 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +000099 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 return;
101
102 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000103 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000104 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 return;
106
107 case MachineOperand::MO_ExternalSymbol:
108 O << MO.getSymbolName();
109 return;
110
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000111 case MachineOperand::MO_GlobalAddress:
112 O << Mang->getMangledName(MO.getGlobal());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114
115 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000116 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000117 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 return;
119
120 default:
121 O << "<unknown operand type: " << MO.getType() << ">";
122 return;
123 }
124}
125
126/// runOnMachineFunction - This uses the printMachineInstruction()
127/// method to print assembly for each instruction.
128///
129bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000130 this->MF = &MF;
131
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 SetupMachineFunction(MF);
133 O << "\n\n";
134
135 // Print out constants referenced by the function
136 EmitConstantPool(MF.getConstantPool());
137
138 // Print out jump tables referenced by the function
139 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
140
141 // Print out labels for the function.
142 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000143 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000144
Bill Wendling25a8ae32009-06-30 22:38:32 +0000145 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000147 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola3b450b32009-01-15 21:51:46 +0000149 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000150 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 break;
152 case Function::ExternalLinkage:
153 O << "\t.globl " << CurrentFnName << "\n";
154 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000155 case Function::WeakAnyLinkage:
156 case Function::WeakODRLinkage:
157 case Function::LinkOnceAnyLinkage:
158 case Function::LinkOnceODRLinkage:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000159 O << MAI->getWeakRefDirective() << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 break;
161 }
162
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000163 printVisibility(CurrentFnName, F->getVisibility());
164
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 O << "\t.ent " << CurrentFnName << "\n";
166
167 O << CurrentFnName << ":\n";
168
169 // Print out code for the function.
170 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
171 I != E; ++I) {
172 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000173 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 O << '\n';
175 }
176 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
177 II != E; ++II) {
178 // Print the assembly for the instruction.
179 ++EmittedInsts;
Chris Lattner32d4cc72009-09-09 23:14:36 +0000180 processDebugLoc(II->getDebugLoc());
181
Chris Lattner7d337492009-08-08 00:05:42 +0000182 printInstruction(II);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000183
184 if (VerboseAsm && !II->getDebugLoc().isUnknown())
185 EmitComments(*II);
186 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 }
188 }
189
190 O << "\t.end " << CurrentFnName << "\n";
191
192 // We didn't modify anything.
193 return false;
194}
195
196bool AlphaAsmPrinter::doInitialization(Module &M)
197{
198 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
199 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
200 else
201 O << "\t.arch ev6\n";
202 O << "\t.set noat\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000203 return AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204}
205
Chris Lattnerae982212009-07-21 18:38:57 +0000206void AlphaAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 const TargetData *TD = TM.getTargetData();
208
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000209 if (!GVar->hasInitializer()) return; // External global require no code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000211 // Check to see if this is a special global used by LLVM, if so, emit it.
212 if (EmitSpecialLLVMGlobal(GVar))
213 return;
214
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000215 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000216 Constant *C = GVar->getInitializer();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000217 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000218 unsigned Align = TD->getPreferredAlignmentLog(GVar);
219
220 // 0: Switch to section
Chris Lattner73266f92009-08-19 05:49:37 +0000221 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
222 TM));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000223
224 // 1: Check visibility
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000225 printVisibility(name, GVar->getVisibility());
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000226
227 // 2: Kind
228 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000229 case GlobalValue::LinkOnceAnyLinkage:
230 case GlobalValue::LinkOnceODRLinkage:
231 case GlobalValue::WeakAnyLinkage:
232 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000233 case GlobalValue::CommonLinkage:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000234 O << MAI->getWeakRefDirective() << name << '\n';
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000235 break;
236 case GlobalValue::AppendingLinkage:
237 case GlobalValue::ExternalLinkage:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000238 O << MAI->getGlobalDirective() << name << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 break;
240 case GlobalValue::InternalLinkage:
Rafael Espindola3b450b32009-01-15 21:51:46 +0000241 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000242 case GlobalValue::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 break;
244 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000245 llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 }
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000247
248 // 3: Type, Size, Align
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000249 if (MAI->hasDotTypeDotSizeDirective()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 O << "\t.type\t" << name << ", @object\n";
251 O << "\t.size\t" << name << ", " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 }
253
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000254 EmitAlignment(Align, GVar);
255
256 O << name << ":\n";
257
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000258 EmitGlobalConstant(C);
259 O << '\n';
260}
261
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262/// PrintAsmOperand - Print out an operand for an inline asm expression.
263///
264bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000265 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 const char *ExtraCode) {
267 printOperand(MI, OpNo);
268 return false;
269}
270
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000271bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000273 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 const char *ExtraCode) {
275 if (ExtraCode && ExtraCode[0])
276 return true; // Unknown modifier.
277 O << "0(";
278 printOperand(MI, OpNo);
279 O << ")";
280 return false;
281}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000282
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000283// Force static initialization.
284extern "C" void LLVMInitializeAlphaAsmPrinter() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000285 RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000286}