blob: 19db81935a5053f749315f32e467e7194b271d9b [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 Lenharth304d0f32005-01-22 23:41:55 +000017#include "llvm/Module.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000018#include "llvm/Type.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000019#include "llvm/Assembly/Writer.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000021#include "llvm/CodeGen/ValueTypes.h"
22#include "llvm/CodeGen/AsmPrinter.h"
23
24#include "llvm/Target/TargetMachine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000025
26#include "llvm/Support/Mangler.h"
27#include "llvm/ADT/Statistic.h"
Andrew Lenharth3ae18292005-04-14 16:24:00 +000028#include "llvm/Support/CommandLine.h"
Andrew Lenharth01269522005-01-24 18:37:48 +000029
Andrew Lenharth304d0f32005-01-22 23:41:55 +000030using namespace llvm;
31
Andrew Lenharth3ae18292005-04-14 16:24:00 +000032namespace llvm {
33 extern cl::opt<bool> EnableAlphaFTOI;
Andrew Lenharth59009192005-05-04 19:12:09 +000034 extern cl::opt<bool> EnableAlphaCT;
Andrew Lenharth3ae18292005-04-14 16:24:00 +000035}
36
Andrew Lenharth304d0f32005-01-22 23:41:55 +000037namespace {
38 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
39
40 struct AlphaAsmPrinter : public AsmPrinter {
41
42 /// Unique incrementer for label values for referencing Global values.
43 ///
44 unsigned LabelNumber;
Misha Brukman4633f1c2005-04-21 23:13:11 +000045
46 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
Andrew Lenharth440e6882005-02-04 14:09:38 +000047 : AsmPrinter(o, tm), LabelNumber(0)
48 {
49 AlignmentIsInBytes = false;
50 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000051
52 /// We name each basic block in a Function with a unique number, so
53 /// that we can consistently refer to them later. This is cleared
54 /// at the beginning of each call to runOnMachineFunction().
55 ///
56 typedef std::map<const Value *, unsigned> ValueMapTy;
57 ValueMapTy NumberForBB;
Andrew Lenharthc24b5372005-04-13 17:17:28 +000058 std::string CurSection;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000059
60 virtual const char *getPassName() const {
61 return "Alpha Assembly Printer";
62 }
63 bool printInstruction(const MachineInstr *MI);
64 void printOp(const MachineOperand &MO, bool IsCallOp = false);
65 void printConstantPool(MachineConstantPool *MCP);
66 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
67 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
68 void printMachineInstruction(const MachineInstr *MI);
Misha Brukman4633f1c2005-04-21 23:13:11 +000069 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000070 bool doInitialization(Module &M);
71 bool doFinalization(Module &M);
Andrew Lenharthc24b5372005-04-13 17:17:28 +000072 void SwitchSection(std::ostream &OS, const char *NewSection);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000073 };
74} // end of anonymous namespace
75
76/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
77/// assembly code for a MachineFunction to the given output stream,
78/// using the given target machine description. This should work
79/// regardless of whether the function is in SSA form.
80///
81FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
82 TargetMachine &tm) {
83 return new AlphaAsmPrinter(o, tm);
84}
85
86#include "AlphaGenAsmWriter.inc"
87
88void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
89{
90 const MachineOperand &MO = MI->getOperand(opNum);
91 if (MO.getType() == MachineOperand::MO_MachineRegister) {
92 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000093 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000094 } else if (MO.isImmediate()) {
95 O << MO.getImmedValue();
96 } else {
97 printOp(MO);
98 }
99}
100
101
102void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
103 const MRegisterInfo &RI = *TM.getRegisterInfo();
104 int new_symbol;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000105
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000106 switch (MO.getType()) {
107 case MachineOperand::MO_VirtualRegister:
108 if (Value *V = MO.getVRegValueOrNull()) {
109 O << "<" << V->getName() << ">";
110 return;
111 }
112 // FALLTHROUGH
113 case MachineOperand::MO_MachineRegister:
114 case MachineOperand::MO_CCRegister:
Andrew Lenharth01269522005-01-24 18:37:48 +0000115 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000116 return;
117
118 case MachineOperand::MO_SignExtendedImmed:
119 case MachineOperand::MO_UnextendedImmed:
120 std::cerr << "printOp() does not handle immediate values\n";
121 abort();
122 return;
123
124 case MachineOperand::MO_PCRelativeDisp:
Andrew Lenharth01269522005-01-24 18:37:48 +0000125 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000126 abort();
127 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000128
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000129 case MachineOperand::MO_MachineBasicBlock: {
130 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Andrew Lenhartheee2a882005-06-06 19:03:09 +0000131 O << "$LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000132 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
133 << MBBOp->getBasicBlock()->getName();
134 return;
135 }
136
137 case MachineOperand::MO_ConstantPoolIndex:
Andrew Lenharth98f0eee2005-06-27 16:29:54 +0000138 O << "$CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000139 return;
140
141 case MachineOperand::MO_ExternalSymbol:
142 O << MO.getSymbolName();
143 return;
144
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000145 case MachineOperand::MO_GlobalAddress:
146 //Abuse PCrel to specify pcrel calls
147 //calls are the only thing that use this flag
148 if (MO.isPCRelative())
149 O << "$" << Mang->getValueName(MO.getGlobal()) << "..ng";
150 else
151 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000152 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000153
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000154 default:
155 O << "<unknown operand type: " << MO.getType() << ">";
156 return;
157 }
158}
159
160/// printMachineInstruction -- Print out a single Alpha MI to
161/// the current output stream.
162///
163void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
164 ++EmittedInsts;
165 if (printInstruction(MI))
166 return; // Printer was automatically generated
Misha Brukman4633f1c2005-04-21 23:13:11 +0000167
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000168 assert(0 && "Unhandled instruction in asm writer!");
169 abort();
170 return;
171}
172
173
174/// runOnMachineFunction - This uses the printMachineInstruction()
175/// method to print assembly for each instruction.
176///
177bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
178 setupMachineFunction(MF);
179 O << "\n\n";
180
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000181 // Print out constants referenced by the function
182 printConstantPool(MF.getConstantPool());
183
184 // Print out labels for the function.
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000185 SwitchSection(O, "text");
186 emitAlignment(4);
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000187 O << "\t.globl " << CurrentFnName << "\n";
188 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000189
190 O << CurrentFnName << ":\n";
191
192 // Print out code for the function.
193 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
194 I != E; ++I) {
195 // Print a label for the basic block.
Andrew Lenhartheee2a882005-06-06 19:03:09 +0000196 O << "$LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000197 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
198 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
199 II != E; ++II) {
200 // Print the assembly for the instruction.
201 O << "\t";
202 printMachineInstruction(II);
203 }
204 }
205 ++LabelNumber;
206
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000207 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000208
209 // We didn't modify anything.
210 return false;
211}
212
213
214/// printConstantPool - Print to the current output stream assembly
215/// representations of the constants in the constant pool MCP. This is
216/// used to print out constants which have been "spilled to memory" by
217/// the code generator.
218///
219void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
220 const std::vector<Constant*> &CP = MCP->getConstants();
221 const TargetData &TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000222
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000223 if (CP.empty()) return;
224
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000225 SwitchSection(O, "section .rodata");
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000226 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000227 // SwitchSection(O, "section .rodata, \"dr\"");
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000228 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
Andrew Lenharth98f0eee2005-06-27 16:29:54 +0000229 O << "$CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000230 << *CP[i] << "\n";
231 emitGlobalConstant(CP[i]);
232 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000233}
234
235bool AlphaAsmPrinter::doInitialization(Module &M)
236{
237 AsmPrinter::doInitialization(M);
Andrew Lenharth59009192005-05-04 19:12:09 +0000238 if(EnableAlphaFTOI || EnableAlphaCT)
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000239 O << "\t.arch ev6\n";
240 else
241 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000242 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000243 return false;
244}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000245
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000246
247// SwitchSection - Switch to the specified section of the executable if we are
248// not already in it!
249//
Misha Brukman4633f1c2005-04-21 23:13:11 +0000250void AlphaAsmPrinter::SwitchSection(std::ostream &OS, const char *NewSection)
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000251{
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000252 if (CurSection != NewSection) {
253 CurSection = NewSection;
254 if (!CurSection.empty())
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000255 OS << "\t." << NewSection << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000256 }
257}
258
259bool AlphaAsmPrinter::doFinalization(Module &M) {
260 const TargetData &TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000261
Chris Lattnere4d5c442005-03-15 04:54:21 +0000262 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000263 if (I->hasInitializer()) { // External global require no code
264 O << "\n\n";
265 std::string name = Mang->getValueName(I);
266 Constant *C = I->getInitializer();
267 unsigned Size = TD.getTypeSize(C->getType());
268 unsigned Align = TD.getTypeAlignmentShift(C->getType());
269
Misha Brukman4633f1c2005-04-21 23:13:11 +0000270 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000271 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
272 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000273 SwitchSection(O, "data");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000274 if (I->hasInternalLinkage())
275 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000276
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000277 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
278 << "," << (1 << Align);
279 O << "\t\t# ";
280 WriteAsOperand(O, I, true, true, &M);
281 O << "\n";
282 } else {
283 switch (I->getLinkage()) {
284 case GlobalValue::LinkOnceLinkage:
285 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
286 // Nonnull linkonce -> weak
287 O << "\t.weak " << name << "\n";
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000288 SwitchSection(O, "");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000289 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
290 break;
291 case GlobalValue::AppendingLinkage:
292 // FIXME: appending linkage variables should go into a section of
293 // their name or something. For now, just emit them as external.
294 case GlobalValue::ExternalLinkage:
295 // If external or appending, declare as a global symbol
296 O << "\t.globl " << name << "\n";
297 // FALL THROUGH
298 case GlobalValue::InternalLinkage:
299 if (C->isNullValue())
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000300 SwitchSection(O, "bss"); //was .bss
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000301 else
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000302 SwitchSection(O, "data");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000303 break;
304 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000305 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000306 abort();
307 }
308
309 emitAlignment(Align);
310 O << "\t.type " << name << ",@object\n";
311 O << "\t.size " << name << "," << Size << "\n";
312 O << name << ":\t\t\t\t# ";
313 WriteAsOperand(O, I, true, true, &M);
314 O << " = ";
315 WriteAsOperand(O, C, false, false, &M);
316 O << "\n";
317 emitGlobalConstant(C);
318 }
319 }
320
321 AsmPrinter::doFinalization(M);
322 return false;
323}