blob: 919552cb1f0b13d4978a166da71cdda24cb58ebb [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"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000022#include "llvm/Target/TargetMachine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000023#include "llvm/Support/Mangler.h"
24#include "llvm/ADT/Statistic.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000025#include <iostream>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000026using namespace llvm;
27
28namespace {
29 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
30
31 struct AlphaAsmPrinter : public AsmPrinter {
32
33 /// Unique incrementer for label values for referencing Global values.
34 ///
35 unsigned LabelNumber;
Misha Brukman4633f1c2005-04-21 23:13:11 +000036
37 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
Chris Lattner87744a22005-11-21 07:38:08 +000038 : AsmPrinter(o, tm), LabelNumber(0) {
Andrew Lenharth440e6882005-02-04 14:09:38 +000039 AlignmentIsInBytes = false;
Chris Lattner81a994e2005-11-21 06:51:52 +000040 PrivateGlobalPrefix = "$";
Andrew Lenharth440e6882005-02-04 14:09:38 +000041 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000042
43 /// We name each basic block in a Function with a unique number, so
44 /// that we can consistently refer to them later. This is cleared
45 /// at the beginning of each call to runOnMachineFunction().
46 ///
47 typedef std::map<const Value *, unsigned> ValueMapTy;
48 ValueMapTy NumberForBB;
Andrew Lenharthc24b5372005-04-13 17:17:28 +000049 std::string CurSection;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000050
51 virtual const char *getPassName() const {
52 return "Alpha Assembly Printer";
53 }
54 bool printInstruction(const MachineInstr *MI);
55 void printOp(const MachineOperand &MO, bool IsCallOp = false);
Nate Begeman391c5d22005-11-30 18:54:35 +000056 void printOperand(const MachineInstr *MI, int opNum);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000057 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
58 void printMachineInstruction(const MachineInstr *MI);
Misha Brukman4633f1c2005-04-21 23:13:11 +000059 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000060 bool doInitialization(Module &M);
61 bool doFinalization(Module &M);
Andrew Lenharth17255992006-06-21 13:37:27 +000062
63 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
64 unsigned AsmVariant, const char *ExtraCode);
Andrew Lenharthdf97cc62006-06-21 15:42:36 +000065 bool PrintAsmMemoryOperand(const MachineInstr *MI,
66 unsigned OpNo,
67 unsigned AsmVariant,
68 const char *ExtraCode);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000069 };
70} // end of anonymous namespace
71
72/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
73/// assembly code for a MachineFunction to the given output stream,
74/// using the given target machine description. This should work
75/// regardless of whether the function is in SSA form.
76///
77FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
78 TargetMachine &tm) {
79 return new AlphaAsmPrinter(o, tm);
80}
81
82#include "AlphaGenAsmWriter.inc"
83
Nate Begeman391c5d22005-11-30 18:54:35 +000084void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000085{
86 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattner2d90ac72006-05-04 18:05:43 +000087 if (MO.getType() == MachineOperand::MO_Register) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000088 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000089 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000090 } else if (MO.isImmediate()) {
91 O << MO.getImmedValue();
Andrew Lenharth3aa92e42006-05-17 19:24:31 +000092 assert(MO.getImmedValue() < (1 << 30));
Andrew Lenharth304d0f32005-01-22 23:41:55 +000093 } else {
94 printOp(MO);
95 }
96}
97
98
99void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
100 const MRegisterInfo &RI = *TM.getRegisterInfo();
101 int new_symbol;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000102
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000103 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000104 case MachineOperand::MO_Register:
Andrew Lenharth01269522005-01-24 18:37:48 +0000105 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000106 return;
107
Chris Lattner63b3d712006-05-04 17:21:20 +0000108 case MachineOperand::MO_Immediate:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000109 std::cerr << "printOp() does not handle immediate values\n";
110 abort();
111 return;
112
Nate Begeman37efe672006-04-22 18:53:45 +0000113 case MachineOperand::MO_MachineBasicBlock:
114 printBasicBlockLabel(MO.getMachineBasicBlock());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000115 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000116
117 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner60530102005-11-21 08:29:17 +0000118 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattner81a994e2005-11-21 06:51:52 +0000119 << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000120 return;
121
122 case MachineOperand::MO_ExternalSymbol:
123 O << MO.getSymbolName();
124 return;
125
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000126 case MachineOperand::MO_GlobalAddress:
Chris Lattner34fb2ca2006-05-04 00:49:59 +0000127 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000128 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000129
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000130 default:
131 O << "<unknown operand type: " << MO.getType() << ">";
132 return;
133 }
134}
135
136/// printMachineInstruction -- Print out a single Alpha MI to
137/// the current output stream.
138///
139void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
140 ++EmittedInsts;
141 if (printInstruction(MI))
142 return; // Printer was automatically generated
Misha Brukman4633f1c2005-04-21 23:13:11 +0000143
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000144 assert(0 && "Unhandled instruction in asm writer!");
145 abort();
146 return;
147}
148
149
150/// runOnMachineFunction - This uses the printMachineInstruction()
151/// method to print assembly for each instruction.
152///
153bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000154 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000155 O << "\n\n";
156
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000157 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000158 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000159
160 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000161 const Function *F = MF.getFunction();
Chris Lattner4632d7a2006-05-09 04:59:56 +0000162 SwitchToTextSection(".text", F);
Andrew Lenharthfd895432006-02-04 19:13:09 +0000163 EmitAlignment(4, F);
164 switch (F->getLinkage()) {
165 default: assert(0 && "Unknown linkage type!");
166 case Function::InternalLinkage: // Symbols default to internal.
167 break;
168 case Function::ExternalLinkage:
169 O << "\t.globl " << CurrentFnName << "\n";
170 break;
171 case Function::WeakLinkage:
172 case Function::LinkOnceLinkage:
173 O << "\t.weak " << CurrentFnName << "\n";
174 break;
175 }
176
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000177 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000178
179 O << CurrentFnName << ":\n";
180
181 // Print out code for the function.
182 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
183 I != E; ++I) {
Nate Begemancdf38c42006-05-02 05:37:32 +0000184 printBasicBlockLabel(I, true);
185 O << '\n';
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000186 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
187 II != E; ++II) {
188 // Print the assembly for the instruction.
189 O << "\t";
190 printMachineInstruction(II);
191 }
192 }
193 ++LabelNumber;
194
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000195 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000196
197 // We didn't modify anything.
198 return false;
199}
200
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000201bool AlphaAsmPrinter::doInitialization(Module &M)
202{
203 AsmPrinter::doInitialization(M);
Andrew Lenharth120ab482005-09-29 22:54:56 +0000204 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
205 || TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000206 O << "\t.arch ev6\n";
207 else
208 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000209 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000210 return false;
211}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000212
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000213bool AlphaAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000214 const TargetData *TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000215
Chris Lattnere4d5c442005-03-15 04:54:21 +0000216 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000217 if (I->hasInitializer()) { // External global require no code
Chris Lattner04f96742006-03-09 06:14:35 +0000218 // Check to see if this is a special global used by LLVM, if so, emit it.
219 if (EmitSpecialLLVMGlobal(I))
220 continue;
221
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000222 O << "\n\n";
223 std::string name = Mang->getValueName(I);
224 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000225 unsigned Size = TD->getTypeSize(C->getType());
226 // unsigned Align = TD->getTypeAlignmentShift(C->getType());
Andrew Lenharth054e2a72006-02-06 17:15:17 +0000227 unsigned Align = getPreferredAlignmentLog(I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000228
Misha Brukman4633f1c2005-04-21 23:13:11 +0000229 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000230 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
231 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000232 SwitchToDataSection("\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000233 if (I->hasInternalLinkage())
234 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000235
Owen Andersona69571c2006-05-03 01:29:57 +0000236 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
Jim Laskeydae29982006-02-27 10:29:04 +0000237 << "," << (1 << Align)
238 << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000239 } else {
240 switch (I->getLinkage()) {
241 case GlobalValue::LinkOnceLinkage:
242 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
243 // Nonnull linkonce -> weak
244 O << "\t.weak " << name << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000245 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +0000246 SwitchToDataSection("", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000247 break;
248 case GlobalValue::AppendingLinkage:
249 // FIXME: appending linkage variables should go into a section of
250 // their name or something. For now, just emit them as external.
251 case GlobalValue::ExternalLinkage:
252 // If external or appending, declare as a global symbol
253 O << "\t.globl " << name << "\n";
254 // FALL THROUGH
255 case GlobalValue::InternalLinkage:
Chris Lattner4632d7a2006-05-09 04:59:56 +0000256 SwitchToDataSection(C->isNullValue() ? "\t.section .bss" :
257 "\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000258 break;
259 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000260 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000261 abort();
262 }
263
Chris Lattner8b8b9512005-11-21 07:51:23 +0000264 EmitAlignment(Align);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000265 O << "\t.type " << name << ",@object\n";
266 O << "\t.size " << name << "," << Size << "\n";
Jim Laskeydae29982006-02-27 10:29:04 +0000267 O << name << ":\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000268 EmitGlobalConstant(C);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000269 }
270 }
271
272 AsmPrinter::doFinalization(M);
273 return false;
274}
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,
279 unsigned AsmVariant,
280 const char *ExtraCode) {
281 printOperand(MI, OpNo);
282 return false;
283}
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000284
285bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
286 unsigned OpNo,
287 unsigned AsmVariant,
288 const char *ExtraCode) {
289 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}