blob: 7b73bb302c38e70edd857d28a9ed30c45f45c150 [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"
Jim Laskey563321a2006-09-06 18:34:40 +000024#include "llvm/Target/TargetAsmInfo.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner11fd2f12006-12-06 18:19:53 +000026#include "llvm/Support/Compiler.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000027#include "llvm/Support/Mangler.h"
Owen Andersoncb371882008-08-21 00:14:44 +000028#include "llvm/Support/raw_ostream.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000029#include "llvm/ADT/Statistic.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000030using namespace llvm;
31
Chris Lattner95b2c7d2006-12-19 22:59:26 +000032STATISTIC(EmittedInsts, "Number of machine instrs printed");
33
Andrew Lenharth304d0f32005-01-22 23:41:55 +000034namespace {
Jim Laskey563321a2006-09-06 18:34:40 +000035 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000036 /// Unique incrementer for label values for referencing Global values.
37 ///
Misha Brukman4633f1c2005-04-21 23:13:11 +000038
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000039 explicit AlphaAsmPrinter(raw_ostream &o, TargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +000040 const TargetAsmInfo *T, CodeGenOpt::Level OL,
41 bool V)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000042 : AsmPrinter(o, tm, T, OL, V) {}
Andrew Lenharth304d0f32005-01-22 23:41:55 +000043
Andrew Lenharth304d0f32005-01-22 23:41:55 +000044 virtual const char *getPassName() const {
45 return "Alpha Assembly Printer";
46 }
47 bool printInstruction(const MachineInstr *MI);
48 void printOp(const MachineOperand &MO, bool IsCallOp = false);
Nate Begeman391c5d22005-11-30 18:54:35 +000049 void printOperand(const MachineInstr *MI, int opNum);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000050 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000051 void printModuleLevelGV(const GlobalVariable* GVar);
Misha Brukman4633f1c2005-04-21 23:13:11 +000052 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000053 bool doInitialization(Module &M);
54 bool doFinalization(Module &M);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000055
Andrew Lenharth17255992006-06-21 13:37:27 +000056 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
57 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000058 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +000059 unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +000060 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +000061 const char *ExtraCode);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000062 };
63} // end of anonymous namespace
64
65/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
66/// assembly code for a MachineFunction to the given output stream,
67/// using the given target machine description. This should work
68/// regardless of whether the function is in SSA form.
69///
Owen Andersoncb371882008-08-21 00:14:44 +000070FunctionPass *llvm::createAlphaCodePrinterPass(raw_ostream &o,
Bill Wendling57f0db82009-02-24 08:30:20 +000071 TargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +000072 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000073 bool verbose) {
74 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, 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:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000104 cerr << "printOp() does not handle immediate values\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000105 abort();
106 return;
107
Nate Begeman37efe672006-04-22 18:53:45 +0000108 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000109 printBasicBlockLabel(MO.getMBB());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000110 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000111
112 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000113 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000114 << MO.getIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000115 return;
116
117 case MachineOperand::MO_ExternalSymbol:
118 O << MO.getSymbolName();
119 return;
120
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000121 case MachineOperand::MO_GlobalAddress: {
122 GlobalValue *GV = MO.getGlobal();
123 O << Mang->getValueName(GV);
124 if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
125 ExtWeakSymbols.insert(GV);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000126 return;
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000127 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000128
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000129 case MachineOperand::MO_JumpTableIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000130 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000131 << '_' << MO.getIndex();
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000132 return;
133
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000134 default:
135 O << "<unknown operand type: " << MO.getType() << ">";
136 return;
137 }
138}
139
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000140/// runOnMachineFunction - This uses the printMachineInstruction()
141/// method to print assembly for each instruction.
142///
143bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000144 this->MF = &MF;
145
Chris Lattner8b8b9512005-11-21 07:51:23 +0000146 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 O << "\n\n";
148
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000149 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000150 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000151
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000152 // Print out jump tables referenced by the function
Chris Lattner1da31ee2006-10-05 03:01:21 +0000153 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000154
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000155 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000156 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000157 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000158
Andrew Lenharthfd895432006-02-04 19:13:09 +0000159 EmitAlignment(4, F);
160 switch (F->getLinkage()) {
161 default: assert(0 && "Unknown linkage type!");
162 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola19caec72009-01-15 21:51:46 +0000163 case Function::PrivateLinkage:
Andrew Lenharthfd895432006-02-04 19:13:09 +0000164 break;
165 case Function::ExternalLinkage:
166 O << "\t.globl " << CurrentFnName << "\n";
167 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000168 case Function::WeakAnyLinkage:
169 case Function::WeakODRLinkage:
170 case Function::LinkOnceAnyLinkage:
171 case Function::LinkOnceODRLinkage:
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000172 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
Andrew Lenharthfd895432006-02-04 19:13:09 +0000173 break;
174 }
175
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000176 printVisibility(CurrentFnName, F->getVisibility());
177
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000178 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000179
180 O << CurrentFnName << ":\n";
181
182 // Print out code for the function.
183 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
184 I != E; ++I) {
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000185 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000186 printBasicBlockLabel(I, true, true);
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000187 O << '\n';
188 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000189 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
190 II != E; ++II) {
191 // Print the assembly for the instruction.
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000192 ++EmittedInsts;
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000193 if (!printInstruction(II)) {
194 assert(0 && "Unhandled instruction in asm writer!");
195 abort();
196 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000197 }
198 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000199
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000200 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000201
202 // We didn't modify anything.
203 return false;
204}
205
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000206bool AlphaAsmPrinter::doInitialization(Module &M)
207{
Andrew Lenharth3553d862007-01-24 21:09:16 +0000208 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
209 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000210 else
Andrew Lenharth3553d862007-01-24 21:09:16 +0000211 O << "\t.arch ev6\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000212 O << "\t.set noat\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000213 return AsmPrinter::doInitialization(M);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000214}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000215
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000216void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Owen Andersona69571c2006-05-03 01:29:57 +0000217 const TargetData *TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000218
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000219 if (!GVar->hasInitializer()) return; // External global require no code
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000220
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000221 // Check to see if this is a special global used by LLVM, if so, emit it.
222 if (EmitSpecialLLVMGlobal(GVar))
223 return;
224
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000225 std::string name = Mang->getValueName(GVar);
226 Constant *C = GVar->getInitializer();
Duncan Sands777d2302009-05-09 07:06:46 +0000227 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000228 unsigned Align = TD->getPreferredAlignmentLog(GVar);
229
230 // 0: Switch to section
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000231 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000232
233 // 1: Check visibility
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000234 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000235
236 // 2: Kind
237 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000238 case GlobalValue::LinkOnceAnyLinkage:
239 case GlobalValue::LinkOnceODRLinkage:
240 case GlobalValue::WeakAnyLinkage:
241 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000242 case GlobalValue::CommonLinkage:
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000243 O << TAI->getWeakRefDirective() << name << '\n';
244 break;
245 case GlobalValue::AppendingLinkage:
246 case GlobalValue::ExternalLinkage:
247 O << TAI->getGlobalDirective() << name << "\n";
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000248 break;
249 case GlobalValue::InternalLinkage:
Rafael Espindola19caec72009-01-15 21:51:46 +0000250 case GlobalValue::PrivateLinkage:
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000251 break;
252 default:
253 assert(0 && "Unknown linkage type!");
254 cerr << "Unknown linkage type!\n";
255 abort();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000256 }
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000257
258 // 3: Type, Size, Align
259 if (TAI->hasDotTypeDotSizeDirective()) {
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000260 O << "\t.type\t" << name << ", @object\n";
261 O << "\t.size\t" << name << ", " << Size << "\n";
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000262 }
Andrew Lenharth913ab052006-12-07 17:39:14 +0000263
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000264 EmitAlignment(Align, GVar);
265
266 O << name << ":\n";
267
268 // If the initializer is a extern weak symbol, remember to emit the weak
269 // reference!
270 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
271 if (GV->hasExternalWeakLinkage())
272 ExtWeakSymbols.insert(GV);
273
274 EmitGlobalConstant(C);
275 O << '\n';
276}
277
278bool AlphaAsmPrinter::doFinalization(Module &M) {
279 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
280 I != E; ++I)
281 printModuleLevelGV(I);
282
Dan Gohmanb8275a32007-07-25 19:33:14 +0000283 return AsmPrinter::doFinalization(M);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000284}
Andrew Lenharth17255992006-06-21 13:37:27 +0000285
286/// PrintAsmOperand - Print out an operand for an inline asm expression.
287///
288bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000289 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000290 const char *ExtraCode) {
Andrew Lenharth17255992006-06-21 13:37:27 +0000291 printOperand(MI, OpNo);
292 return false;
293}
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000294
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000295bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000296 unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000297 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000298 const char *ExtraCode) {
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000299 if (ExtraCode && ExtraCode[0])
300 return true; // Unknown modifier.
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000301 O << "0(";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000302 printOperand(MI, OpNo);
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000303 O << ")";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000304 return false;
305}
Douglas Gregor1555a232009-06-16 20:12:29 +0000306
307// Force static initialization when called from
308// llvm/InitializeAllAsmPrinters.h
309namespace llvm {
310 void InitializeAlphaAsmPrinter() { }
311}
Anton Korobeynikove494b9e2009-06-19 19:36:55 +0000312
313namespace {
314 static struct Register {
315 Register() {
316 AlphaTargetMachine::registerAsmPrinter(createAlphaCodePrinterPass);
317 }
318 } Registrator;
319}