blob: 265f8ae4a21e660be2ddc606c3bf95a76eb233c6 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
15#include "Alpha.h"
16#include "AlphaInstrInfo.h"
Andrew Lenharth120ab482005-09-29 22:54:56 +000017#include "AlphaTargetMachine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000018#include "llvm/Module.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000019#include "llvm/Type.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000020#include "llvm/Assembly/Writer.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000021#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey563321a2006-09-06 18:34:40 +000022#include "llvm/Target/TargetAsmInfo.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000023#include "llvm/Target/TargetMachine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000024#include "llvm/Support/Mangler.h"
25#include "llvm/ADT/Statistic.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000026#include <iostream>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000027using namespace llvm;
28
29namespace {
30 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
Jim Laskey563321a2006-09-06 18:34:40 +000031
Jim Laskey563321a2006-09-06 18:34:40 +000032 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000033
34 /// Unique incrementer for label values for referencing Global values.
35 ///
36 unsigned LabelNumber;
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)
Jim Laskey563321a2006-09-06 18:34:40 +000039 : AsmPrinter(o, tm, T), LabelNumber(0) {
Andrew Lenharth440e6882005-02-04 14:09:38 +000040 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000041
42 /// We name each basic block in a Function with a unique number, so
43 /// that we can consistently refer to them later. This is cleared
44 /// at the beginning of each call to runOnMachineFunction().
45 ///
46 typedef std::map<const Value *, unsigned> ValueMapTy;
47 ValueMapTy NumberForBB;
Andrew Lenharthc24b5372005-04-13 17:17:28 +000048 std::string CurSection;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000049
50 virtual const char *getPassName() const {
51 return "Alpha Assembly Printer";
52 }
53 bool printInstruction(const MachineInstr *MI);
54 void printOp(const MachineOperand &MO, bool IsCallOp = false);
Nate Begeman391c5d22005-11-30 18:54:35 +000055 void printOperand(const MachineInstr *MI, int opNum);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000056 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
57 void printMachineInstruction(const MachineInstr *MI);
Misha Brukman4633f1c2005-04-21 23:13:11 +000058 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000059 bool doInitialization(Module &M);
60 bool doFinalization(Module &M);
Chris Lattner6e796292006-10-05 02:47:13 +000061
Andrew Lenharth17255992006-06-21 13:37:27 +000062 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
63 unsigned AsmVariant, const char *ExtraCode);
Andrew Lenharthdf97cc62006-06-21 15:42:36 +000064 bool PrintAsmMemoryOperand(const MachineInstr *MI,
65 unsigned OpNo,
66 unsigned AsmVariant,
67 const char *ExtraCode);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000068 };
69} // end of anonymous namespace
70
71/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
72/// assembly code for a MachineFunction to the given output stream,
73/// using the given target machine description. This should work
74/// regardless of whether the function is in SSA form.
75///
Chris Lattner6e796292006-10-05 02:47:13 +000076FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
77 TargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +000078 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
Andrew Lenharth304d0f32005-01-22 23:41:55 +000079}
80
81#include "AlphaGenAsmWriter.inc"
82
Nate Begeman391c5d22005-11-30 18:54:35 +000083void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000084{
85 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattner2d90ac72006-05-04 18:05:43 +000086 if (MO.getType() == MachineOperand::MO_Register) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000087 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000088 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000089 } else if (MO.isImmediate()) {
90 O << MO.getImmedValue();
Andrew Lenharth3aa92e42006-05-17 19:24:31 +000091 assert(MO.getImmedValue() < (1 << 30));
Andrew Lenharth304d0f32005-01-22 23:41:55 +000092 } else {
93 printOp(MO);
94 }
95}
96
97
98void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
99 const MRegisterInfo &RI = *TM.getRegisterInfo();
100 int new_symbol;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000101
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000102 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000103 case MachineOperand::MO_Register:
Andrew Lenharth01269522005-01-24 18:37:48 +0000104 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000105 return;
106
Chris Lattner63b3d712006-05-04 17:21:20 +0000107 case MachineOperand::MO_Immediate:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000108 std::cerr << "printOp() does not handle immediate values\n";
109 abort();
110 return;
111
Nate Begeman37efe672006-04-22 18:53:45 +0000112 case MachineOperand::MO_MachineBasicBlock:
113 printBasicBlockLabel(MO.getMachineBasicBlock());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000114 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000115
116 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskey563321a2006-09-06 18:34:40 +0000117 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner81a994e2005-11-21 06:51:52 +0000118 << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000119 return;
120
121 case MachineOperand::MO_ExternalSymbol:
122 O << MO.getSymbolName();
123 return;
124
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000125 case MachineOperand::MO_GlobalAddress:
Chris Lattner34fb2ca2006-05-04 00:49:59 +0000126 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000127 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000128
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000129 case MachineOperand::MO_JumpTableIndex:
130 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
131 << '_' << MO.getJumpTableIndex();
132 return;
133
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000134 default:
135 O << "<unknown operand type: " << MO.getType() << ">";
136 return;
137 }
138}
139
140/// printMachineInstruction -- Print out a single Alpha MI to
141/// the current output stream.
142///
143void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
144 ++EmittedInsts;
145 if (printInstruction(MI))
146 return; // Printer was automatically generated
Misha Brukman4633f1c2005-04-21 23:13:11 +0000147
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000148 assert(0 && "Unhandled instruction in asm writer!");
149 abort();
150 return;
151}
152
153
154/// runOnMachineFunction - This uses the printMachineInstruction()
155/// method to print assembly for each instruction.
156///
157bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000158 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000159 O << "\n\n";
160
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000161 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000162 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000163
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000164 // Print out jump tables referenced by the function
165 EmitJumpTableInfo(MF.getJumpTableInfo());
166
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000167 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000168 const Function *F = MF.getFunction();
Chris Lattner6e796292006-10-05 02:47:13 +0000169 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
170
Andrew Lenharthfd895432006-02-04 19:13:09 +0000171 EmitAlignment(4, F);
172 switch (F->getLinkage()) {
173 default: assert(0 && "Unknown linkage type!");
174 case Function::InternalLinkage: // Symbols default to internal.
175 break;
176 case Function::ExternalLinkage:
177 O << "\t.globl " << CurrentFnName << "\n";
178 break;
179 case Function::WeakLinkage:
180 case Function::LinkOnceLinkage:
181 O << "\t.weak " << CurrentFnName << "\n";
182 break;
183 }
184
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000185 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000186
187 O << CurrentFnName << ":\n";
188
189 // Print out code for the function.
190 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
191 I != E; ++I) {
Nate Begemancdf38c42006-05-02 05:37:32 +0000192 printBasicBlockLabel(I, true);
193 O << '\n';
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000194 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
195 II != E; ++II) {
196 // Print the assembly for the instruction.
197 O << "\t";
198 printMachineInstruction(II);
199 }
200 }
201 ++LabelNumber;
202
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000203 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000204
205 // We didn't modify anything.
206 return false;
207}
208
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000209bool AlphaAsmPrinter::doInitialization(Module &M)
210{
211 AsmPrinter::doInitialization(M);
Andrew Lenharth120ab482005-09-29 22:54:56 +0000212 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
213 || TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000214 O << "\t.arch ev6\n";
215 else
216 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000217 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000218 return false;
219}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000220
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000221bool AlphaAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000222 const TargetData *TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000223
Chris Lattnere4d5c442005-03-15 04:54:21 +0000224 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000225 if (I->hasInitializer()) { // External global require no code
Chris Lattner04f96742006-03-09 06:14:35 +0000226 // Check to see if this is a special global used by LLVM, if so, emit it.
227 if (EmitSpecialLLVMGlobal(I))
228 continue;
229
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000230 O << "\n\n";
231 std::string name = Mang->getValueName(I);
232 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000233 unsigned Size = TD->getTypeSize(C->getType());
234 // unsigned Align = TD->getTypeAlignmentShift(C->getType());
Andrew Lenharth054e2a72006-02-06 17:15:17 +0000235 unsigned Align = getPreferredAlignmentLog(I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000236
Misha Brukman4633f1c2005-04-21 23:13:11 +0000237 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000238 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
239 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000240 SwitchToDataSection("\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000241 if (I->hasInternalLinkage())
242 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000243
Owen Andersona69571c2006-05-03 01:29:57 +0000244 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
Jim Laskeydae29982006-02-27 10:29:04 +0000245 << "," << (1 << Align)
246 << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000247 } else {
248 switch (I->getLinkage()) {
249 case GlobalValue::LinkOnceLinkage:
250 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
251 // Nonnull linkonce -> weak
252 O << "\t.weak " << name << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000253 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +0000254 SwitchToDataSection("", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000255 break;
256 case GlobalValue::AppendingLinkage:
257 // FIXME: appending linkage variables should go into a section of
258 // their name or something. For now, just emit them as external.
259 case GlobalValue::ExternalLinkage:
260 // If external or appending, declare as a global symbol
261 O << "\t.globl " << name << "\n";
262 // FALL THROUGH
263 case GlobalValue::InternalLinkage:
Chris Lattner4632d7a2006-05-09 04:59:56 +0000264 SwitchToDataSection(C->isNullValue() ? "\t.section .bss" :
265 "\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000266 break;
267 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000268 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000269 abort();
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000270 case GlobalValue::DLLImportLinkage:
271 std::cerr << "DLLImport linkage is not supported by this target!\n";
272 abort();
273 case GlobalValue::DLLExportLinkage:
274 std::cerr << "DLLExport linkage is not supported by this target!\n";
275 abort();
276 default:
277 assert(0 && "Unknown linkage type!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000278 }
279
Chris Lattner8b8b9512005-11-21 07:51:23 +0000280 EmitAlignment(Align);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000281 O << "\t.type " << name << ",@object\n";
282 O << "\t.size " << name << "," << Size << "\n";
Jim Laskeydae29982006-02-27 10:29:04 +0000283 O << name << ":\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000284 EmitGlobalConstant(C);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000285 }
286 }
287
288 AsmPrinter::doFinalization(M);
289 return false;
290}
Andrew Lenharth17255992006-06-21 13:37:27 +0000291
292/// PrintAsmOperand - Print out an operand for an inline asm expression.
293///
294bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
295 unsigned AsmVariant,
296 const char *ExtraCode) {
297 printOperand(MI, OpNo);
298 return false;
299}
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000300
301bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
302 unsigned OpNo,
303 unsigned AsmVariant,
304 const char *ExtraCode) {
305 if (ExtraCode && ExtraCode[0])
306 return true; // Unknown modifier.
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000307 O << "0(";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000308 printOperand(MI, OpNo);
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000309 O << ")";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000310 return false;
311}