blob: 06232712da47956470e020f9da9912099dae53c0 [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"
Devang Patele4c0c0f2009-06-25 00:47:42 +000020#include "llvm/MDNode.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000021#include "llvm/Type.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000022#include "llvm/Assembly/Writer.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000023#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000024#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey563321a2006-09-06 18:34:40 +000025#include "llvm/Target/TargetAsmInfo.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000026#include "llvm/Target/TargetMachine.h"
Chris Lattner11fd2f12006-12-06 18:19:53 +000027#include "llvm/Support/Compiler.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000028#include "llvm/Support/Mangler.h"
Owen Andersoncb371882008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000030#include "llvm/ADT/Statistic.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000031using namespace llvm;
32
Chris Lattner95b2c7d2006-12-19 22:59:26 +000033STATISTIC(EmittedInsts, "Number of machine instrs printed");
34
Andrew Lenharth304d0f32005-01-22 23:41:55 +000035namespace {
Jim Laskey563321a2006-09-06 18:34:40 +000036 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000037 /// Unique incrementer for label values for referencing Global values.
38 ///
Misha Brukman4633f1c2005-04-21 23:13:11 +000039
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000040 explicit AlphaAsmPrinter(raw_ostream &o, TargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +000041 const TargetAsmInfo *T, CodeGenOpt::Level OL,
42 bool V)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000043 : AsmPrinter(o, tm, T, OL, V) {}
Andrew Lenharth304d0f32005-01-22 23:41:55 +000044
Andrew Lenharth304d0f32005-01-22 23:41:55 +000045 virtual const char *getPassName() const {
46 return "Alpha Assembly Printer";
47 }
48 bool printInstruction(const MachineInstr *MI);
49 void printOp(const MachineOperand &MO, bool IsCallOp = false);
Nate Begeman391c5d22005-11-30 18:54:35 +000050 void printOperand(const MachineInstr *MI, int opNum);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000051 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000052 void printModuleLevelGV(const GlobalVariable* GVar);
Misha Brukman4633f1c2005-04-21 23:13:11 +000053 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000054 bool doInitialization(Module &M);
55 bool doFinalization(Module &M);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000056
Andrew Lenharth17255992006-06-21 13:37:27 +000057 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
58 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000059 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +000060 unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +000061 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +000062 const char *ExtraCode);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000063 };
64} // end of anonymous namespace
65
66/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
67/// assembly code for a MachineFunction to the given output stream,
68/// using the given target machine description. This should work
69/// regardless of whether the function is in SSA form.
70///
Owen Andersoncb371882008-08-21 00:14:44 +000071FunctionPass *llvm::createAlphaCodePrinterPass(raw_ostream &o,
Bill Wendling57f0db82009-02-24 08:30:20 +000072 TargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +000073 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000074 bool verbose) {
75 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000076}
77
78#include "AlphaGenAsmWriter.inc"
79
Nate Begeman391c5d22005-11-30 18:54:35 +000080void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000081{
82 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattner2d90ac72006-05-04 18:05:43 +000083 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000084 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
85 "Not physreg??");
Bill Wendling74ab84c2008-02-26 21:11:01 +000086 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmand735b802008-10-03 15:45:36 +000087 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +000088 O << MO.getImm();
89 assert(MO.getImm() < (1 << 30));
Andrew Lenharth304d0f32005-01-22 23:41:55 +000090 } else {
91 printOp(MO);
92 }
93}
94
95
96void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000097 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Misha Brukman4633f1c2005-04-21 23:13:11 +000098
Andrew Lenharth304d0f32005-01-22 23:41:55 +000099 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000100 case MachineOperand::MO_Register:
Bill Wendling74ab84c2008-02-26 21:11:01 +0000101 O << RI.get(MO.getReg()).AsmName;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000102 return;
103
Chris Lattner63b3d712006-05-04 17:21:20 +0000104 case MachineOperand::MO_Immediate:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000105 cerr << "printOp() does not handle immediate values\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000106 abort();
107 return;
108
Nate Begeman37efe672006-04-22 18:53:45 +0000109 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000110 printBasicBlockLabel(MO.getMBB());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000111 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000112
113 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000114 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000115 << MO.getIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000116 return;
117
118 case MachineOperand::MO_ExternalSymbol:
119 O << MO.getSymbolName();
120 return;
121
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000122 case MachineOperand::MO_GlobalAddress: {
123 GlobalValue *GV = MO.getGlobal();
124 O << Mang->getValueName(GV);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000125 return;
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000126 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000127
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000128 case MachineOperand::MO_JumpTableIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000129 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000130 << '_' << MO.getIndex();
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000131 return;
132
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000133 default:
134 O << "<unknown operand type: " << MO.getType() << ">";
135 return;
136 }
137}
138
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000139/// runOnMachineFunction - This uses the printMachineInstruction()
140/// method to print assembly for each instruction.
141///
142bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000143 this->MF = &MF;
144
Chris Lattner8b8b9512005-11-21 07:51:23 +0000145 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000146 O << "\n\n";
147
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000148 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000149 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000150
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000151 // Print out jump tables referenced by the function
Chris Lattner1da31ee2006-10-05 03:01:21 +0000152 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000153
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000154 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000155 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000156 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000157
Bill Wendling20c568f2009-06-30 22:38:32 +0000158 EmitAlignment(MF.getAlignment(), F);
Andrew Lenharthfd895432006-02-04 19:13:09 +0000159 switch (F->getLinkage()) {
160 default: assert(0 && "Unknown linkage type!");
161 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola19caec72009-01-15 21:51:46 +0000162 case Function::PrivateLinkage:
Andrew Lenharthfd895432006-02-04 19:13:09 +0000163 break;
164 case Function::ExternalLinkage:
165 O << "\t.globl " << CurrentFnName << "\n";
166 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000167 case Function::WeakAnyLinkage:
168 case Function::WeakODRLinkage:
169 case Function::LinkOnceAnyLinkage:
170 case Function::LinkOnceODRLinkage:
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000171 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
Andrew Lenharthfd895432006-02-04 19:13:09 +0000172 break;
173 }
174
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000175 printVisibility(CurrentFnName, F->getVisibility());
176
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000177 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000178
179 O << CurrentFnName << ":\n";
180
181 // Print out code for the function.
182 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
183 I != E; ++I) {
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000184 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000185 printBasicBlockLabel(I, true, true);
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000186 O << '\n';
187 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000188 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
189 II != E; ++II) {
190 // Print the assembly for the instruction.
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000191 ++EmittedInsts;
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000192 if (!printInstruction(II)) {
193 assert(0 && "Unhandled instruction in asm writer!");
194 abort();
195 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000196 }
197 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000198
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000199 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000200
201 // We didn't modify anything.
202 return false;
203}
204
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000205bool AlphaAsmPrinter::doInitialization(Module &M)
206{
Andrew Lenharth3553d862007-01-24 21:09:16 +0000207 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
208 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000209 else
Andrew Lenharth3553d862007-01-24 21:09:16 +0000210 O << "\t.arch ev6\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000211 O << "\t.set noat\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000212 return AsmPrinter::doInitialization(M);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000213}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000214
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000215void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Owen Andersona69571c2006-05-03 01:29:57 +0000216 const TargetData *TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000217
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000218 if (!GVar->hasInitializer()) return; // External global require no code
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000219
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000220 // Check to see if this is a special global used by LLVM, if so, emit it.
221 if (EmitSpecialLLVMGlobal(GVar))
222 return;
223
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000224 std::string name = Mang->getValueName(GVar);
225 Constant *C = GVar->getInitializer();
Devang Patel0f05d222009-06-26 02:26:12 +0000226 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patele4c0c0f2009-06-25 00:47:42 +0000227 return;
Duncan Sands777d2302009-05-09 07:06:46 +0000228 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000229 unsigned Align = TD->getPreferredAlignmentLog(GVar);
230
231 // 0: Switch to section
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000232 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000233
234 // 1: Check visibility
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000235 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000236
237 // 2: Kind
238 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000239 case GlobalValue::LinkOnceAnyLinkage:
240 case GlobalValue::LinkOnceODRLinkage:
241 case GlobalValue::WeakAnyLinkage:
242 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000243 case GlobalValue::CommonLinkage:
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000244 O << TAI->getWeakRefDirective() << name << '\n';
245 break;
246 case GlobalValue::AppendingLinkage:
247 case GlobalValue::ExternalLinkage:
248 O << TAI->getGlobalDirective() << name << "\n";
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000249 break;
250 case GlobalValue::InternalLinkage:
Rafael Espindola19caec72009-01-15 21:51:46 +0000251 case GlobalValue::PrivateLinkage:
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000252 break;
253 default:
254 assert(0 && "Unknown linkage type!");
255 cerr << "Unknown linkage type!\n";
256 abort();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000257 }
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000258
259 // 3: Type, Size, Align
260 if (TAI->hasDotTypeDotSizeDirective()) {
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000261 O << "\t.type\t" << name << ", @object\n";
262 O << "\t.size\t" << name << ", " << Size << "\n";
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000263 }
Andrew Lenharth913ab052006-12-07 17:39:14 +0000264
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000265 EmitAlignment(Align, GVar);
266
267 O << name << ":\n";
268
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000269 EmitGlobalConstant(C);
270 O << '\n';
271}
272
273bool AlphaAsmPrinter::doFinalization(Module &M) {
274 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
275 I != E; ++I)
276 printModuleLevelGV(I);
277
Dan Gohmanb8275a32007-07-25 19:33:14 +0000278 return AsmPrinter::doFinalization(M);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000279}
Andrew Lenharth17255992006-06-21 13:37:27 +0000280
281/// PrintAsmOperand - Print out an operand for an inline asm expression.
282///
283bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000284 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000285 const char *ExtraCode) {
Andrew Lenharth17255992006-06-21 13:37:27 +0000286 printOperand(MI, OpNo);
287 return false;
288}
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000289
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000290bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000291 unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000292 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000293 const char *ExtraCode) {
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000294 if (ExtraCode && ExtraCode[0])
295 return true; // Unknown modifier.
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000296 O << "0(";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000297 printOperand(MI, OpNo);
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000298 O << ")";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000299 return false;
300}
Douglas Gregor1555a232009-06-16 20:12:29 +0000301
Bob Wilsona96751f2009-06-23 23:59:40 +0000302// Force static initialization.
303extern "C" void LLVMInitializeAlphaAsmPrinter() { }
Anton Korobeynikove494b9e2009-06-19 19:36:55 +0000304
305namespace {
306 static struct Register {
307 Register() {
308 AlphaTargetMachine::registerAsmPrinter(createAlphaCodePrinterPass);
309 }
310 } Registrator;
311}