blob: e280a457b7dae577dc485ae07df2910d1033bffa [file] [log] [blame]
Andrew Lenharth01269522005-01-24 18:37:48 +00001//===-- AlphaAsmPrinter.cpp - Alpha LLVM assembly writer ------------------===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
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
Chris Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000016#include "Alpha.h"
17#include "AlphaInstrInfo.h"
Andrew Lenharth120ab482005-09-29 22:54:56 +000018#include "AlphaTargetMachine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000019#include "llvm/Module.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000020#include "llvm/Type.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000021#include "llvm/Assembly/Writer.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000022#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000023#include "llvm/CodeGen/DwarfWriter.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000024#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000025#include "llvm/MC/MCAsmInfo.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000026#include "llvm/MC/MCSymbol.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000027#include "llvm/Target/TargetLoweringObjectFile.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000028#include "llvm/Target/TargetMachine.h"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000029#include "llvm/Target/TargetRegistry.h"
Torok Edwin804e0fe2009-07-08 19:04:27 +000030#include "llvm/Support/ErrorHandling.h"
David Greene71847812009-07-14 20:18:05 +000031#include "llvm/Support/FormattedStream.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000032#include "llvm/ADT/Statistic.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000033using namespace llvm;
34
Chris Lattner95b2c7d2006-12-19 22:59:26 +000035STATISTIC(EmittedInsts, "Number of machine instrs printed");
36
Andrew Lenharth304d0f32005-01-22 23:41:55 +000037namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000038 struct AlphaAsmPrinter : public AsmPrinter {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000039 /// Unique incrementer for label values for referencing Global values.
40 ///
Misha Brukman4633f1c2005-04-21 23:13:11 +000041
David Greene71847812009-07-14 20:18:05 +000042 explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattneraf76e592009-08-22 20:48:53 +000043 const MCAsmInfo *T, bool V)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000044 : AsmPrinter(o, tm, T, V) {}
Andrew Lenharth304d0f32005-01-22 23:41:55 +000045
Andrew Lenharth304d0f32005-01-22 23:41:55 +000046 virtual const char *getPassName() const {
47 return "Alpha Assembly Printer";
48 }
Chris Lattner41aefdc2009-08-08 01:32:19 +000049 void printInstruction(const MachineInstr *MI);
Chris Lattnerd95148f2009-09-13 20:19:22 +000050 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000051
Andrew Lenharth304d0f32005-01-22 23:41:55 +000052 void printOp(const MachineOperand &MO, bool IsCallOp = false);
Nate Begeman391c5d22005-11-30 18:54:35 +000053 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattner40bbebd2009-07-21 18:38:57 +000054 void printBaseOffsetPair(const MachineInstr *MI, int i, bool brackets=true);
55 void PrintGlobalVariable(const GlobalVariable *GVar);
Misha Brukman4633f1c2005-04-21 23:13:11 +000056 bool runOnMachineFunction(MachineFunction &F);
Bob Wilson812209a2009-09-30 22:06:26 +000057 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000058
Andrew Lenharth17255992006-06-21 13:37:27 +000059 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
60 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000061 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +000062 unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +000063 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +000064 const char *ExtraCode);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000065 };
66} // end of anonymous namespace
67
Andrew Lenharth304d0f32005-01-22 23:41:55 +000068#include "AlphaGenAsmWriter.inc"
69
Nate Begeman391c5d22005-11-30 18:54:35 +000070void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000071{
72 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattner2d90ac72006-05-04 18:05:43 +000073 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000074 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
75 "Not physreg??");
Chris Lattner762ccea2009-09-13 20:31:40 +000076 O << getRegisterName(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +000077 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +000078 O << MO.getImm();
79 assert(MO.getImm() < (1 << 30));
Andrew Lenharth304d0f32005-01-22 23:41:55 +000080 } else {
81 printOp(MO);
82 }
83}
84
85
86void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000087 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +000088 case MachineOperand::MO_Register:
Chris Lattner762ccea2009-09-13 20:31:40 +000089 O << getRegisterName(MO.getReg());
Andrew Lenharth304d0f32005-01-22 23:41:55 +000090 return;
91
Chris Lattner63b3d712006-05-04 17:21:20 +000092 case MachineOperand::MO_Immediate:
Torok Edwinc23197a2009-07-14 16:55:14 +000093 llvm_unreachable("printOp() does not handle immediate values");
Andrew Lenharth304d0f32005-01-22 23:41:55 +000094 return;
95
Nate Begeman37efe672006-04-22 18:53:45 +000096 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner325d3dc2009-09-13 17:14:04 +000097 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000098 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000099
100 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000101 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000102 << MO.getIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000103 return;
104
105 case MachineOperand::MO_ExternalSymbol:
106 O << MO.getSymbolName();
107 return;
108
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000109 case MachineOperand::MO_GlobalAddress:
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000110 GetGlobalValueSymbol(MO.getGlobal())->print(O, MAI);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000111 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000112
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000113 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000114 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000115 << '_' << MO.getIndex();
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000116 return;
117
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000118 default:
119 O << "<unknown operand type: " << MO.getType() << ">";
120 return;
121 }
122}
123
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000124/// runOnMachineFunction - This uses the printMachineInstruction()
125/// method to print assembly for each instruction.
126///
127bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000128 this->MF = &MF;
129
Chris Lattner8b8b9512005-11-21 07:51:23 +0000130 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000131 O << "\n\n";
132
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000133 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000134 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000135
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000136 // Print out jump tables referenced by the function
Chris Lattner1da31ee2006-10-05 03:01:21 +0000137 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000138
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000139 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000140 const Function *F = MF.getFunction();
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000141 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000142
Bill Wendling20c568f2009-06-30 22:38:32 +0000143 EmitAlignment(MF.getAlignment(), F);
Andrew Lenharthfd895432006-02-04 19:13:09 +0000144 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000145 default: llvm_unreachable("Unknown linkage type!");
Andrew Lenharthfd895432006-02-04 19:13:09 +0000146 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola19caec72009-01-15 21:51:46 +0000147 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000148 case Function::LinkerPrivateLinkage:
Andrew Lenharthfd895432006-02-04 19:13:09 +0000149 break;
Chris Lattner12164412010-01-16 00:21:18 +0000150 case Function::ExternalLinkage:
151 O << "\t.globl ";
152 CurrentFnSym->print(O, MAI);
153 O << "\n";
154 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000155 case Function::WeakAnyLinkage:
156 case Function::WeakODRLinkage:
157 case Function::LinkOnceAnyLinkage:
158 case Function::LinkOnceODRLinkage:
Chris Lattner12164412010-01-16 00:21:18 +0000159 O << MAI->getWeakRefDirective();
160 CurrentFnSym->print(O, MAI);
161 O << "\n";
Andrew Lenharthfd895432006-02-04 19:13:09 +0000162 break;
163 }
164
Chris Lattner12164412010-01-16 00:21:18 +0000165 printVisibility(CurrentFnSym, F->getVisibility());
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000166
Chris Lattner12164412010-01-16 00:21:18 +0000167 O << "\t.ent ";
168 CurrentFnSym->print(O, MAI);
169 O << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000170
Chris Lattner12164412010-01-16 00:21:18 +0000171 CurrentFnSym->print(O, MAI);
172 O << ":\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000173
174 // Print out code for the function.
175 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
176 I != E; ++I) {
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000177 if (I != MF.begin()) {
Chris Lattner70a54c02009-09-13 18:25:37 +0000178 EmitBasicBlockStart(I);
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000179 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000180 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
181 II != E; ++II) {
182 // Print the assembly for the instruction.
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000183 ++EmittedInsts;
Devang Patelaf0e2722009-10-06 02:19:11 +0000184 processDebugLoc(II, true);
Chris Lattner2698cb62009-08-08 00:05:42 +0000185 printInstruction(II);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000186
David Greene1924aab2009-11-13 21:34:57 +0000187 if (VerboseAsm)
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000188 EmitComments(*II);
189 O << '\n';
Devang Patelaf0e2722009-10-06 02:19:11 +0000190 processDebugLoc(II, false);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000191 }
192 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000193
Chris Lattner12164412010-01-16 00:21:18 +0000194 O << "\t.end ";
195 CurrentFnSym->print(O, MAI);
196 O << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000197
198 // We didn't modify anything.
199 return false;
200}
201
Bob Wilson812209a2009-09-30 22:06:26 +0000202void AlphaAsmPrinter::EmitStartOfAsmFile(Module &M) {
203 if (TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3553d862007-01-24 21:09:16 +0000204 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000205 else
Andrew Lenharth3553d862007-01-24 21:09:16 +0000206 O << "\t.arch ev6\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000207 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000208}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000209
Chris Lattner40bbebd2009-07-21 18:38:57 +0000210void AlphaAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Owen Andersona69571c2006-05-03 01:29:57 +0000211 const TargetData *TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000212
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000213 if (!GVar->hasInitializer()) return; // External global require no code
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000214
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000215 // Check to see if this is a special global used by LLVM, if so, emit it.
216 if (EmitSpecialLLVMGlobal(GVar))
217 return;
218
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000219 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000220 Constant *C = GVar->getInitializer();
Duncan Sands777d2302009-05-09 07:06:46 +0000221 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000222 unsigned Align = TD->getPreferredAlignmentLog(GVar);
223
224 // 0: Switch to section
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000225 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
226 TM));
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000227
228 // 1: Check visibility
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000229 printVisibility(GVarSym, GVar->getVisibility());
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000230
231 // 2: Kind
232 switch (GVar->getLinkage()) {
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000233 case GlobalValue::LinkOnceAnyLinkage:
234 case GlobalValue::LinkOnceODRLinkage:
235 case GlobalValue::WeakAnyLinkage:
236 case GlobalValue::WeakODRLinkage:
237 case GlobalValue::CommonLinkage:
238 O << MAI->getWeakRefDirective();
239 GVarSym->print(O, MAI);
240 O << '\n';
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000241 break;
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000242 case GlobalValue::AppendingLinkage:
243 case GlobalValue::ExternalLinkage:
244 O << MAI->getGlobalDirective();
245 GVarSym->print(O, MAI);
246 O << '\n';
247 break;
248 case GlobalValue::InternalLinkage:
249 case GlobalValue::PrivateLinkage:
250 case GlobalValue::LinkerPrivateLinkage:
251 break;
252 default:
253 llvm_unreachable("Unknown linkage type!");
254 }
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000255
256 // 3: Type, Size, Align
Chris Lattner33adcfb2009-08-22 21:43:10 +0000257 if (MAI->hasDotTypeDotSizeDirective()) {
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000258 O << "\t.type\t";
259 GVarSym->print(O, MAI);
260 O << ", @object\n";
261 O << "\t.size\t";
262 GVarSym->print(O, MAI);
263 O << ", " << Size << "\n";
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000264 }
Andrew Lenharth913ab052006-12-07 17:39:14 +0000265
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000266 EmitAlignment(Align, GVar);
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000267
268 GVarSym->print(O, MAI);
269 O << ":\n";
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000270
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000271 EmitGlobalConstant(C);
272 O << '\n';
273}
274
Andrew Lenharth17255992006-06-21 13:37:27 +0000275/// PrintAsmOperand - Print out an operand for an inline asm expression.
276///
277bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000278 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000279 const char *ExtraCode) {
Andrew Lenharth17255992006-06-21 13:37:27 +0000280 printOperand(MI, OpNo);
281 return false;
282}
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000283
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000284bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000285 unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000286 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000287 const char *ExtraCode) {
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000288 if (ExtraCode && ExtraCode[0])
289 return true; // Unknown modifier.
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000290 O << "0(";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000291 printOperand(MI, OpNo);
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000292 O << ")";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000293 return false;
294}
Douglas Gregor1555a232009-06-16 20:12:29 +0000295
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000296// Force static initialization.
297extern "C" void LLVMInitializeAlphaAsmPrinter() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000298 RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000299}