blob: 21622dbc724a3442ca840a6288886fc1cb192c2f [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"
Torok Edwin804e0fe2009-07-08 19:04:27 +000028#include "llvm/Support/ErrorHandling.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000029#include "llvm/Support/Mangler.h"
David Greene71847812009-07-14 20:18:05 +000030#include "llvm/Support/FormattedStream.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000031#include "llvm/ADT/Statistic.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000032using namespace llvm;
33
Chris Lattner95b2c7d2006-12-19 22:59:26 +000034STATISTIC(EmittedInsts, "Number of machine instrs printed");
35
Andrew Lenharth304d0f32005-01-22 23:41:55 +000036namespace {
Jim Laskey563321a2006-09-06 18:34:40 +000037 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000038 /// Unique incrementer for label values for referencing Global values.
39 ///
Misha Brukman4633f1c2005-04-21 23:13:11 +000040
David Greene71847812009-07-14 20:18:05 +000041 explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000042 const TargetAsmInfo *T, bool V)
43 : AsmPrinter(o, tm, T, 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///
David Greene71847812009-07-14 20:18:05 +000071FunctionPass *llvm::createAlphaCodePrinterPass(formatted_raw_ostream &o,
Bill Wendling57f0db82009-02-24 08:30:20 +000072 TargetMachine &tm,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000073 bool verbose) {
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000074 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000075}
76
77#include "AlphaGenAsmWriter.inc"
78
Nate Begeman391c5d22005-11-30 18:54:35 +000079void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000080{
81 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattner2d90ac72006-05-04 18:05:43 +000082 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000083 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
84 "Not physreg??");
Bill Wendling74ab84c2008-02-26 21:11:01 +000085 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmand735b802008-10-03 15:45:36 +000086 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +000087 O << MO.getImm();
88 assert(MO.getImm() < (1 << 30));
Andrew Lenharth304d0f32005-01-22 23:41:55 +000089 } else {
90 printOp(MO);
91 }
92}
93
94
95void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000096 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Misha Brukman4633f1c2005-04-21 23:13:11 +000097
Andrew Lenharth304d0f32005-01-22 23:41:55 +000098 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +000099 case MachineOperand::MO_Register:
Bill Wendling74ab84c2008-02-26 21:11:01 +0000100 O << RI.get(MO.getReg()).AsmName;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000101 return;
102
Chris Lattner63b3d712006-05-04 17:21:20 +0000103 case MachineOperand::MO_Immediate:
Torok Edwinc23197a2009-07-14 16:55:14 +0000104 llvm_unreachable("printOp() does not handle immediate values");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000105 return;
106
Nate Begeman37efe672006-04-22 18:53:45 +0000107 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000108 printBasicBlockLabel(MO.getMBB());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000109 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000110
111 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000112 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000113 << MO.getIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000114 return;
115
116 case MachineOperand::MO_ExternalSymbol:
117 O << MO.getSymbolName();
118 return;
119
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000120 case MachineOperand::MO_GlobalAddress:
121 O << Mang->getMangledName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000122 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000123
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000124 case MachineOperand::MO_JumpTableIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000125 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000126 << '_' << MO.getIndex();
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000127 return;
128
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000129 default:
130 O << "<unknown operand type: " << MO.getType() << ">";
131 return;
132 }
133}
134
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000135/// runOnMachineFunction - This uses the printMachineInstruction()
136/// method to print assembly for each instruction.
137///
138bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000139 this->MF = &MF;
140
Chris Lattner8b8b9512005-11-21 07:51:23 +0000141 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000142 O << "\n\n";
143
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000144 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000145 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000146
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000147 // Print out jump tables referenced by the function
Chris Lattner1da31ee2006-10-05 03:01:21 +0000148 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000149
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000150 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000151 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000152 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000153
Bill Wendling20c568f2009-06-30 22:38:32 +0000154 EmitAlignment(MF.getAlignment(), F);
Andrew Lenharthfd895432006-02-04 19:13:09 +0000155 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000156 default: llvm_unreachable("Unknown linkage type!");
Andrew Lenharthfd895432006-02-04 19:13:09 +0000157 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola19caec72009-01-15 21:51:46 +0000158 case Function::PrivateLinkage:
Andrew Lenharthfd895432006-02-04 19:13:09 +0000159 break;
160 case Function::ExternalLinkage:
161 O << "\t.globl " << CurrentFnName << "\n";
162 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000163 case Function::WeakAnyLinkage:
164 case Function::WeakODRLinkage:
165 case Function::LinkOnceAnyLinkage:
166 case Function::LinkOnceODRLinkage:
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000167 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
Andrew Lenharthfd895432006-02-04 19:13:09 +0000168 break;
169 }
170
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000171 printVisibility(CurrentFnName, F->getVisibility());
172
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000173 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000174
175 O << CurrentFnName << ":\n";
176
177 // Print out code for the function.
178 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
179 I != E; ++I) {
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000180 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000181 printBasicBlockLabel(I, true, true);
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000182 O << '\n';
183 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000184 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
185 II != E; ++II) {
186 // Print the assembly for the instruction.
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000187 ++EmittedInsts;
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000188 if (!printInstruction(II)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000189 llvm_unreachable("Unhandled instruction in asm writer!");
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000190 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000191 }
192 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000193
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000194 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000195
196 // We didn't modify anything.
197 return false;
198}
199
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000200bool AlphaAsmPrinter::doInitialization(Module &M)
201{
Andrew Lenharth3553d862007-01-24 21:09:16 +0000202 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
203 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000204 else
Andrew Lenharth3553d862007-01-24 21:09:16 +0000205 O << "\t.arch ev6\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000206 O << "\t.set noat\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000207 return AsmPrinter::doInitialization(M);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000208}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000209
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000210void AlphaAsmPrinter::printModuleLevelGV(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 Lattnerb8158ac2009-07-14 18:17:16 +0000219 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000220 Constant *C = GVar->getInitializer();
Devang Patel0f05d222009-06-26 02:26:12 +0000221 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patele4c0c0f2009-06-25 00:47:42 +0000222 return;
Duncan Sands777d2302009-05-09 07:06:46 +0000223 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000224 unsigned Align = TD->getPreferredAlignmentLog(GVar);
225
226 // 0: Switch to section
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000227 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000228
229 // 1: Check visibility
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000230 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000231
232 // 2: Kind
233 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000234 case GlobalValue::LinkOnceAnyLinkage:
235 case GlobalValue::LinkOnceODRLinkage:
236 case GlobalValue::WeakAnyLinkage:
237 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000238 case GlobalValue::CommonLinkage:
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000239 O << TAI->getWeakRefDirective() << name << '\n';
240 break;
241 case GlobalValue::AppendingLinkage:
242 case GlobalValue::ExternalLinkage:
243 O << TAI->getGlobalDirective() << name << "\n";
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000244 break;
245 case GlobalValue::InternalLinkage:
Rafael Espindola19caec72009-01-15 21:51:46 +0000246 case GlobalValue::PrivateLinkage:
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000247 break;
248 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000249 llvm_unreachable("Unknown linkage type!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000250 }
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000251
252 // 3: Type, Size, Align
253 if (TAI->hasDotTypeDotSizeDirective()) {
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000254 O << "\t.type\t" << name << ", @object\n";
255 O << "\t.size\t" << name << ", " << Size << "\n";
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000256 }
Andrew Lenharth913ab052006-12-07 17:39:14 +0000257
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000258 EmitAlignment(Align, GVar);
259
260 O << name << ":\n";
261
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000262 EmitGlobalConstant(C);
263 O << '\n';
264}
265
266bool AlphaAsmPrinter::doFinalization(Module &M) {
267 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
268 I != E; ++I)
269 printModuleLevelGV(I);
270
Dan Gohmanb8275a32007-07-25 19:33:14 +0000271 return AsmPrinter::doFinalization(M);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000272}
Andrew Lenharth17255992006-06-21 13:37:27 +0000273
274/// PrintAsmOperand - Print out an operand for an inline asm expression.
275///
276bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000277 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000278 const char *ExtraCode) {
Andrew Lenharth17255992006-06-21 13:37:27 +0000279 printOperand(MI, OpNo);
280 return false;
281}
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000282
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000283bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000284 unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000285 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000286 const char *ExtraCode) {
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000287 if (ExtraCode && ExtraCode[0])
288 return true; // Unknown modifier.
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000289 O << "0(";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000290 printOperand(MI, OpNo);
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000291 O << ")";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000292 return false;
293}
Douglas Gregor1555a232009-06-16 20:12:29 +0000294
Stuart Hastings2286f8d2009-07-15 17:27:11 +0000295// Force static initialization.
296extern "C" void LLVMInitializeAlphaAsmPrinter() { }
297
Anton Korobeynikove494b9e2009-06-19 19:36:55 +0000298namespace {
299 static struct Register {
300 Register() {
301 AlphaTargetMachine::registerAsmPrinter(createAlphaCodePrinterPass);
302 }
303 } Registrator;
304}