blob: b13f544843e106f7d64ddb5ab2aa7ff519b5c0a6 [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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 bool runOnMachineFunction(MachineFunction &F);
Bob Wilsonb5f835e2009-09-30 22:06:26 +000056 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000057
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
59 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikova99c6362008-08-07 09:53:57 +000060 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +000062 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 const char *ExtraCode);
64 };
65} // end of anonymous namespace
66
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067#include "AlphaGenAsmWriter.inc"
68
69void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
70{
71 const MachineOperand &MO = MI->getOperand(opNum);
72 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman1e57df32008-02-10 18:45:23 +000073 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
74 "Not physreg??");
Chris Lattnerf0a25de2009-09-13 20:31:40 +000075 O << getRegisterName(MO.getReg());
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000076 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000077 O << MO.getImm();
78 assert(MO.getImm() < (1 << 30));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 } else {
80 printOp(MO);
81 }
82}
83
84
85void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 switch (MO.getType()) {
87 case MachineOperand::MO_Register:
Chris Lattnerf0a25de2009-09-13 20:31:40 +000088 O << getRegisterName(MO.getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 return;
90
91 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +000092 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 return;
94
95 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerce409842010-01-17 21:43:43 +000096 O << *GetMBBSymbol(MO.getMBB()->getNumber());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 return;
98
99 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000100 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000101 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 return;
103
104 case MachineOperand::MO_ExternalSymbol:
105 O << MO.getSymbolName();
106 return;
107
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000108 case MachineOperand::MO_GlobalAddress:
Chris Lattnerce409842010-01-17 21:43:43 +0000109 O << *GetGlobalValueSymbol(MO.getGlobal());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111
112 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000113 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000114 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 return;
116
117 default:
118 O << "<unknown operand type: " << MO.getType() << ">";
119 return;
120 }
121}
122
123/// runOnMachineFunction - This uses the printMachineInstruction()
124/// method to print assembly for each instruction.
125///
126bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000127 this->MF = &MF;
128
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 SetupMachineFunction(MF);
130 O << "\n\n";
131
132 // Print out constants referenced by the function
133 EmitConstantPool(MF.getConstantPool());
134
135 // Print out jump tables referenced by the function
136 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
137
138 // Print out labels for the function.
139 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000140 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000141
Bill Wendling25a8ae32009-06-30 22:38:32 +0000142 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000144 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola3b450b32009-01-15 21:51:46 +0000146 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000147 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 break;
Chris Lattner651adf32010-01-16 00:21:18 +0000149 case Function::ExternalLinkage:
Chris Lattnerce409842010-01-17 21:43:43 +0000150 O << "\t.globl " << *CurrentFnSym << '\n';
Chris Lattner651adf32010-01-16 00:21:18 +0000151 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000152 case Function::WeakAnyLinkage:
153 case Function::WeakODRLinkage:
154 case Function::LinkOnceAnyLinkage:
155 case Function::LinkOnceODRLinkage:
Chris Lattnerce409842010-01-17 21:43:43 +0000156 O << MAI->getWeakRefDirective() << *CurrentFnSym << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 break;
158 }
159
Chris Lattner651adf32010-01-16 00:21:18 +0000160 printVisibility(CurrentFnSym, F->getVisibility());
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000161
Chris Lattnerce409842010-01-17 21:43:43 +0000162 O << "\t.ent " << *CurrentFnSym << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163
Chris Lattnerce409842010-01-17 21:43:43 +0000164 O << *CurrentFnSym << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165
166 // Print out code for the function.
167 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
168 I != E; ++I) {
Chris Lattnerce409842010-01-17 21:43:43 +0000169 if (I != MF.begin())
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000170 EmitBasicBlockStart(I);
Chris Lattnerce409842010-01-17 21:43:43 +0000171
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
173 II != E; ++II) {
174 // Print the assembly for the instruction.
175 ++EmittedInsts;
Devang Patel5450fc12009-10-06 02:19:11 +0000176 processDebugLoc(II, true);
Chris Lattner7d337492009-08-08 00:05:42 +0000177 printInstruction(II);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000178
David Greeneca9b04b2009-11-13 21:34:57 +0000179 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000180 EmitComments(*II);
181 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000182 processDebugLoc(II, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 }
184 }
185
Chris Lattnerce409842010-01-17 21:43:43 +0000186 O << "\t.end " << *CurrentFnSym << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187
188 // We didn't modify anything.
189 return false;
190}
191
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000192void AlphaAsmPrinter::EmitStartOfAsmFile(Module &M) {
193 if (TM.getSubtarget<AlphaSubtarget>().hasCT())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
195 else
196 O << "\t.arch ev6\n";
197 O << "\t.set noat\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198}
199
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200/// PrintAsmOperand - Print out an operand for an inline asm expression.
201///
202bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000203 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 const char *ExtraCode) {
205 printOperand(MI, OpNo);
206 return false;
207}
208
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000209bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 unsigned OpNo,
Anton Korobeynikova99c6362008-08-07 09:53:57 +0000211 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 const char *ExtraCode) {
213 if (ExtraCode && ExtraCode[0])
214 return true; // Unknown modifier.
215 O << "0(";
216 printOperand(MI, OpNo);
217 O << ")";
218 return false;
219}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000220
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000221// Force static initialization.
222extern "C" void LLVMInitializeAlphaAsmPrinter() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000223 RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000224}