blob: 067a8efb3ce3998821561a8c5807cfea12cf2d68 [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);
Misha Brukman4633f1c2005-04-21 23:13:11 +000049 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000050 bool doInitialization(Module &M);
51 bool doFinalization(Module &M);
Chris Lattner6e796292006-10-05 02:47:13 +000052
Andrew Lenharth17255992006-06-21 13:37:27 +000053 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
54 unsigned AsmVariant, const char *ExtraCode);
Andrew Lenharthdf97cc62006-06-21 15:42:36 +000055 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +000056 unsigned OpNo,
57 unsigned AsmVariant,
58 const char *ExtraCode);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000059 };
60} // end of anonymous namespace
61
62/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
63/// assembly code for a MachineFunction to the given output stream,
64/// using the given target machine description. This should work
65/// regardless of whether the function is in SSA form.
66///
Chris Lattner6e796292006-10-05 02:47:13 +000067FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
68 TargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +000069 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
Andrew Lenharth304d0f32005-01-22 23:41:55 +000070}
71
72#include "AlphaGenAsmWriter.inc"
73
Nate Begeman391c5d22005-11-30 18:54:35 +000074void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000075{
76 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattner2d90ac72006-05-04 18:05:43 +000077 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000078 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
79 "Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000080 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000081 } else if (MO.isImmediate()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +000082 O << MO.getImm();
83 assert(MO.getImm() < (1 << 30));
Andrew Lenharth304d0f32005-01-22 23:41:55 +000084 } else {
85 printOp(MO);
86 }
87}
88
89
90void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000091 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Misha Brukman4633f1c2005-04-21 23:13:11 +000092
Andrew Lenharth304d0f32005-01-22 23:41:55 +000093 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +000094 case MachineOperand::MO_Register:
Andrew Lenharth01269522005-01-24 18:37:48 +000095 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000096 return;
97
Chris Lattner63b3d712006-05-04 17:21:20 +000098 case MachineOperand::MO_Immediate:
Bill Wendlingf5da1332006-12-07 22:21:48 +000099 cerr << "printOp() does not handle immediate values\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000100 abort();
101 return;
102
Nate Begeman37efe672006-04-22 18:53:45 +0000103 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000104 printBasicBlockLabel(MO.getMBB());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000105 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000106
107 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000108 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000109 << MO.getIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000110 return;
111
112 case MachineOperand::MO_ExternalSymbol:
113 O << MO.getSymbolName();
114 return;
115
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000116 case MachineOperand::MO_GlobalAddress: {
117 GlobalValue *GV = MO.getGlobal();
118 O << Mang->getValueName(GV);
119 if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
120 ExtWeakSymbols.insert(GV);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000121 return;
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000122 }
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) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000139 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000140 O << "\n\n";
141
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000142 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000143 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000144
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000145 // Print out jump tables referenced by the function
Chris Lattner1da31ee2006-10-05 03:01:21 +0000146 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000147
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000148 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000149 const Function *F = MF.getFunction();
Chris Lattner6e796292006-10-05 02:47:13 +0000150 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
151
Andrew Lenharthfd895432006-02-04 19:13:09 +0000152 EmitAlignment(4, F);
153 switch (F->getLinkage()) {
154 default: assert(0 && "Unknown linkage type!");
155 case Function::InternalLinkage: // Symbols default to internal.
156 break;
157 case Function::ExternalLinkage:
158 O << "\t.globl " << CurrentFnName << "\n";
159 break;
160 case Function::WeakLinkage:
161 case Function::LinkOnceLinkage:
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000162 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
Andrew Lenharthfd895432006-02-04 19:13:09 +0000163 break;
164 }
165
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000166 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000167
168 O << CurrentFnName << ":\n";
169
170 // Print out code for the function.
171 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
172 I != E; ++I) {
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000173 if (I != MF.begin()) {
174 printBasicBlockLabel(I, true);
175 O << '\n';
176 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000177 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
178 II != E; ++II) {
179 // Print the assembly for the instruction.
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000180 ++EmittedInsts;
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000181 if (!printInstruction(II)) {
182 assert(0 && "Unhandled instruction in asm writer!");
183 abort();
184 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000185 }
186 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000187
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000188 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000189
190 // We didn't modify anything.
191 return false;
192}
193
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000194bool AlphaAsmPrinter::doInitialization(Module &M)
195{
Andrew Lenharth3553d862007-01-24 21:09:16 +0000196 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
197 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000198 else
Andrew Lenharth3553d862007-01-24 21:09:16 +0000199 O << "\t.arch ev6\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000200 O << "\t.set noat\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000201 return AsmPrinter::doInitialization(M);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000202}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000203
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000204bool AlphaAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000205 const TargetData *TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000206
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000207 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000208
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000209 if (!I->hasInitializer()) continue; // External global require no code
210
211 // Check to see if this is a special global used by LLVM, if so, emit it.
212 if (EmitSpecialLLVMGlobal(I))
213 continue;
214
215 std::string name = Mang->getValueName(I);
216 Constant *C = I->getInitializer();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000217 unsigned Size = TD->getABITypeSize(C->getType());
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000218 unsigned Align = TD->getPreferredAlignmentLog(I);
219
220 //1: hidden?
221 if (I->hasHiddenVisibility())
222 O << TAI->getHiddenDirective() << name << "\n";
223
224 //2: kind
225 switch (I->getLinkage()) {
226 case GlobalValue::LinkOnceLinkage:
227 case GlobalValue::WeakLinkage:
228 O << TAI->getWeakRefDirective() << name << '\n';
229 break;
230 case GlobalValue::AppendingLinkage:
231 case GlobalValue::ExternalLinkage:
232 O << "\t.globl " << name << "\n";
233 break;
234 case GlobalValue::InternalLinkage:
235 break;
236 default:
237 assert(0 && "Unknown linkage type!");
238 cerr << "Unknown linkage type!\n";
239 abort();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000240 }
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000241
242 //3: Section (if changed)
243 if (I->hasSection() &&
244 (I->getSection() == ".ctors" ||
245 I->getSection() == ".dtors")) {
246 std::string SectionName = ".section\t" + I->getSection()
247 + ",\"aw\",@progbits";
248 SwitchToDataSection(SectionName.c_str());
249 } else {
250 if (C->isNullValue())
251 SwitchToDataSection("\t.section\t.bss", I);
252 else
253 SwitchToDataSection("\t.section\t.data", I);
Andrew Lenharth913ab052006-12-07 17:39:14 +0000254 }
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000255
256 //4: Type, Size, Align
257 O << "\t.type\t" << name << ", @object\n";
258 O << "\t.size\t" << name << ", " << Size << "\n";
259 EmitAlignment(Align, I);
260
261 O << name << ":\n";
262
263 // If the initializer is a extern weak symbol, remember to emit the weak
264 // reference!
265 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
266 if (GV->hasExternalWeakLinkage())
267 ExtWeakSymbols.insert(GV);
268
269 EmitGlobalConstant(C);
270 O << '\n';
271 }
Andrew Lenharth913ab052006-12-07 17:39:14 +0000272
Dan Gohmanb8275a32007-07-25 19:33:14 +0000273 return AsmPrinter::doFinalization(M);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000274}
Andrew Lenharth17255992006-06-21 13:37:27 +0000275
276/// PrintAsmOperand - Print out an operand for an inline asm expression.
277///
278bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000279 unsigned AsmVariant,
280 const char *ExtraCode) {
Andrew Lenharth17255992006-06-21 13:37:27 +0000281 printOperand(MI, OpNo);
282 return false;
283}
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000284
285bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000286 unsigned OpNo,
287 unsigned AsmVariant,
288 const char *ExtraCode) {
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000289 if (ExtraCode && ExtraCode[0])
290 return true; // Unknown modifier.
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000291 O << "0(";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000292 printOperand(MI, OpNo);
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000293 O << ")";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000294 return false;
295}