blob: e592e80a760d73570f4244f9922937d1d0fa3677 [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"
Jim Laskey563321a2006-09-06 18:34:40 +000023#include "llvm/Target/TargetAsmInfo.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000024#include "llvm/Target/TargetMachine.h"
Chris Lattner11fd2f12006-12-06 18:19:53 +000025#include "llvm/Support/Compiler.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000026#include "llvm/Support/Mangler.h"
27#include "llvm/ADT/Statistic.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000028using namespace llvm;
29
Chris Lattner95b2c7d2006-12-19 22:59:26 +000030STATISTIC(EmittedInsts, "Number of machine instrs printed");
31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000032namespace {
Jim Laskey563321a2006-09-06 18:34:40 +000033 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000034
35 /// Unique incrementer for label values for referencing Global values.
36 ///
Misha Brukman4633f1c2005-04-21 23:13:11 +000037
Jim Laskeya0f3d172006-09-07 22:06:40 +000038 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
Andrew Lenharth0fb25902006-12-07 23:55:55 +000039 : AsmPrinter(o, tm, T) {
Andrew Lenharth440e6882005-02-04 14:09:38 +000040 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000041
Andrew Lenharth304d0f32005-01-22 23:41:55 +000042 virtual const char *getPassName() const {
43 return "Alpha Assembly Printer";
44 }
45 bool printInstruction(const MachineInstr *MI);
46 void printOp(const MachineOperand &MO, bool IsCallOp = false);
Nate Begeman391c5d22005-11-30 18:54:35 +000047 void printOperand(const MachineInstr *MI, int opNum);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000048 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000049 void printModuleLevelGV(const GlobalVariable* GVar);
Misha Brukman4633f1c2005-04-21 23:13:11 +000050 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000051 bool doInitialization(Module &M);
52 bool doFinalization(Module &M);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000053
Andrew Lenharth17255992006-06-21 13:37:27 +000054 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
55 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000056 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +000057 unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +000058 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +000059 const char *ExtraCode);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000060 };
61} // end of anonymous namespace
62
63/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
64/// assembly code for a MachineFunction to the given output stream,
65/// using the given target machine description. This should work
66/// regardless of whether the function is in SSA form.
67///
Chris Lattner6e796292006-10-05 02:47:13 +000068FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
69 TargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +000070 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
Andrew Lenharth304d0f32005-01-22 23:41:55 +000071}
72
73#include "AlphaGenAsmWriter.inc"
74
Nate Begeman391c5d22005-11-30 18:54:35 +000075void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000076{
77 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattner2d90ac72006-05-04 18:05:43 +000078 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000079 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
80 "Not physreg??");
Bill Wendling74ab84c2008-02-26 21:11:01 +000081 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000082 } else if (MO.isImmediate()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +000083 O << MO.getImm();
84 assert(MO.getImm() < (1 << 30));
Andrew Lenharth304d0f32005-01-22 23:41:55 +000085 } else {
86 printOp(MO);
87 }
88}
89
90
91void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000092 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Misha Brukman4633f1c2005-04-21 23:13:11 +000093
Andrew Lenharth304d0f32005-01-22 23:41:55 +000094 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +000095 case MachineOperand::MO_Register:
Bill Wendling74ab84c2008-02-26 21:11:01 +000096 O << RI.get(MO.getReg()).AsmName;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000097 return;
98
Chris Lattner63b3d712006-05-04 17:21:20 +000099 case MachineOperand::MO_Immediate:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000100 cerr << "printOp() does not handle immediate values\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000101 abort();
102 return;
103
Nate Begeman37efe672006-04-22 18:53:45 +0000104 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000105 printBasicBlockLabel(MO.getMBB());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000106 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000107
108 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000109 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000110 << MO.getIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000111 return;
112
113 case MachineOperand::MO_ExternalSymbol:
114 O << MO.getSymbolName();
115 return;
116
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000117 case MachineOperand::MO_GlobalAddress: {
118 GlobalValue *GV = MO.getGlobal();
119 O << Mang->getValueName(GV);
120 if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
121 ExtWeakSymbols.insert(GV);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000122 return;
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000123 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000124
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000125 case MachineOperand::MO_JumpTableIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000126 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000127 << '_' << MO.getIndex();
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000128 return;
129
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000130 default:
131 O << "<unknown operand type: " << MO.getType() << ">";
132 return;
133 }
134}
135
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000136/// runOnMachineFunction - This uses the printMachineInstruction()
137/// method to print assembly for each instruction.
138///
139bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000140 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000141 O << "\n\n";
142
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000143 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000144 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000145
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000146 // Print out jump tables referenced by the function
Chris Lattner1da31ee2006-10-05 03:01:21 +0000147 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000148
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000149 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000150 const Function *F = MF.getFunction();
Chris Lattner6e796292006-10-05 02:47:13 +0000151 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000152
Andrew Lenharthfd895432006-02-04 19:13:09 +0000153 EmitAlignment(4, F);
154 switch (F->getLinkage()) {
155 default: assert(0 && "Unknown linkage type!");
156 case Function::InternalLinkage: // Symbols default to internal.
157 break;
158 case Function::ExternalLinkage:
159 O << "\t.globl " << CurrentFnName << "\n";
160 break;
161 case Function::WeakLinkage:
162 case Function::LinkOnceLinkage:
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000163 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
Andrew Lenharthfd895432006-02-04 19:13:09 +0000164 break;
165 }
166
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000167 printVisibility(CurrentFnName, F->getVisibility());
168
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000169 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000170
171 O << CurrentFnName << ":\n";
172
173 // Print out code for the function.
174 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
175 I != E; ++I) {
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000176 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000177 printBasicBlockLabel(I, true, true);
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000178 O << '\n';
179 }
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;
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000184 if (!printInstruction(II)) {
185 assert(0 && "Unhandled instruction in asm writer!");
186 abort();
187 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000188 }
189 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000190
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000191 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000192
193 // We didn't modify anything.
194 return false;
195}
196
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000197bool AlphaAsmPrinter::doInitialization(Module &M)
198{
Andrew Lenharth3553d862007-01-24 21:09:16 +0000199 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
200 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000201 else
Andrew Lenharth3553d862007-01-24 21:09:16 +0000202 O << "\t.arch ev6\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000203 O << "\t.set noat\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000204 return AsmPrinter::doInitialization(M);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000205}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000206
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000207void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Owen Andersona69571c2006-05-03 01:29:57 +0000208 const TargetData *TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000209
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000210 if (!GVar->hasInitializer()) return; // External global require no code
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000211
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000212 // Check to see if this is a special global used by LLVM, if so, emit it.
213 if (EmitSpecialLLVMGlobal(GVar))
214 return;
215
216 std::string SectionName = TAI->SectionForGlobal(GVar);
217 std::string name = Mang->getValueName(GVar);
218 Constant *C = GVar->getInitializer();
219 unsigned Size = TD->getABITypeSize(C->getType());
220 unsigned Align = TD->getPreferredAlignmentLog(GVar);
221
222 // 0: Switch to section
223 SwitchToDataSection(SectionName.c_str());
224
225 // 1: Check visibility
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000226 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000227
228 // 2: Kind
229 switch (GVar->getLinkage()) {
230 case GlobalValue::LinkOnceLinkage:
231 case GlobalValue::WeakLinkage:
232 case GlobalValue::CommonLinkage:
233 O << TAI->getWeakRefDirective() << name << '\n';
234 break;
235 case GlobalValue::AppendingLinkage:
236 case GlobalValue::ExternalLinkage:
237 O << TAI->getGlobalDirective() << name << "\n";
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000238 break;
239 case GlobalValue::InternalLinkage:
240 break;
241 default:
242 assert(0 && "Unknown linkage type!");
243 cerr << "Unknown linkage type!\n";
244 abort();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000245 }
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000246
247 // 3: Type, Size, Align
248 if (TAI->hasDotTypeDotSizeDirective()) {
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000249 O << "\t.type\t" << name << ", @object\n";
250 O << "\t.size\t" << name << ", " << Size << "\n";
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000251 }
Andrew Lenharth913ab052006-12-07 17:39:14 +0000252
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000253 EmitAlignment(Align, GVar);
254
255 O << name << ":\n";
256
257 // If the initializer is a extern weak symbol, remember to emit the weak
258 // reference!
259 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
260 if (GV->hasExternalWeakLinkage())
261 ExtWeakSymbols.insert(GV);
262
263 EmitGlobalConstant(C);
264 O << '\n';
265}
266
267bool AlphaAsmPrinter::doFinalization(Module &M) {
268 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
269 I != E; ++I)
270 printModuleLevelGV(I);
271
Dan Gohmanb8275a32007-07-25 19:33:14 +0000272 return AsmPrinter::doFinalization(M);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000273}
Andrew Lenharth17255992006-06-21 13:37:27 +0000274
275/// 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}