blob: 85feb9d53184a12eb95b6ff814bce8f03b9e25ae [file] [log] [blame]
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001//===-- AlphaAsmPrinter.cpp - Alpha LLVM assembly writer --------------===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Module.h"
20#include "llvm/Assembly/Writer.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineConstantPool.h"
23#include "llvm/CodeGen/MachineInstr.h"
24#include "llvm/CodeGen/ValueTypes.h"
25#include "llvm/CodeGen/AsmPrinter.h"
26
27#include "llvm/Target/TargetMachine.h"
28#include "llvm/Target/MRegisterInfo.h"
29#include "llvm/Target/TargetInstrInfo.h"
30
31#include "llvm/Support/Mangler.h"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/StringExtras.h"
34#include "llvm/Support/CommandLine.h"
35#include <cctype>
36using namespace llvm;
37
38namespace {
39 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
40
41 struct AlphaAsmPrinter : public AsmPrinter {
42
43 /// Unique incrementer for label values for referencing Global values.
44 ///
45 unsigned LabelNumber;
46
47 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
48 : AsmPrinter(o, tm), LabelNumber(0)
49 { }
50
51 /// We name each basic block in a Function with a unique number, so
52 /// that we can consistently refer to them later. This is cleared
53 /// at the beginning of each call to runOnMachineFunction().
54 ///
55 typedef std::map<const Value *, unsigned> ValueMapTy;
56 ValueMapTy NumberForBB;
57
58 virtual const char *getPassName() const {
59 return "Alpha Assembly Printer";
60 }
61 bool printInstruction(const MachineInstr *MI);
62 void printOp(const MachineOperand &MO, bool IsCallOp = false);
63 void printConstantPool(MachineConstantPool *MCP);
64 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
65 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
66 void printMachineInstruction(const MachineInstr *MI);
67 bool runOnMachineFunction(MachineFunction &F);
68 bool doInitialization(Module &M);
69 bool doFinalization(Module &M);
70 };
71} // end of anonymous namespace
72
73/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
74/// assembly code for a MachineFunction to the given output stream,
75/// using the given target machine description. This should work
76/// regardless of whether the function is in SSA form.
77///
78FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
79 TargetMachine &tm) {
80 return new AlphaAsmPrinter(o, tm);
81}
82
83#include "AlphaGenAsmWriter.inc"
84
85void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
86{
87 const MachineOperand &MO = MI->getOperand(opNum);
88 if (MO.getType() == MachineOperand::MO_MachineRegister) {
89 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
90 O << LowercaseString(TM.getRegisterInfo()->get(MO.getReg()).Name);
91 } else if (MO.isImmediate()) {
92 O << MO.getImmedValue();
93 } 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;
102
103 switch (MO.getType()) {
104 case MachineOperand::MO_VirtualRegister:
105 if (Value *V = MO.getVRegValueOrNull()) {
106 O << "<" << V->getName() << ">";
107 return;
108 }
109 // FALLTHROUGH
110 case MachineOperand::MO_MachineRegister:
111 case MachineOperand::MO_CCRegister:
112 O << LowercaseString(RI.get(MO.getReg()).Name);
113 return;
114
115 case MachineOperand::MO_SignExtendedImmed:
116 case MachineOperand::MO_UnextendedImmed:
117 std::cerr << "printOp() does not handle immediate values\n";
118 abort();
119 return;
120
121 case MachineOperand::MO_PCRelativeDisp:
122 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
123 abort();
124 return;
125
126 case MachineOperand::MO_MachineBasicBlock: {
127 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
128 O << "$LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
129 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
130 << MBBOp->getBasicBlock()->getName();
131 return;
132 }
133
134 case MachineOperand::MO_ConstantPoolIndex:
135 O << "$CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
136 return;
137
138 case MachineOperand::MO_ExternalSymbol:
139 O << MO.getSymbolName();
140 return;
141
142 case MachineOperand::MO_GlobalAddress:
143 //std::cerr << "Global Addresses? Are you kidding?\n"
144 //abort();
145 O << Mang->getValueName(MO.getGlobal());
146 return;
147
148 default:
149 O << "<unknown operand type: " << MO.getType() << ">";
150 return;
151 }
152}
153
154/// printMachineInstruction -- Print out a single Alpha MI to
155/// the current output stream.
156///
157void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
158 ++EmittedInsts;
159 if (printInstruction(MI))
160 return; // Printer was automatically generated
161
162 assert(0 && "Unhandled instruction in asm writer!");
163 abort();
164 return;
165}
166
167
168/// runOnMachineFunction - This uses the printMachineInstruction()
169/// method to print assembly for each instruction.
170///
171bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
172 setupMachineFunction(MF);
173 O << "\n\n";
174
175 if (CurrentFnName.compare("main") == 0)
176 {
177 O << "\n\n#HACK\n\t.text\n\t.ent __main\n__main:\n\tret $31,($26),1\n\t.end __main\n#ENDHACK\n\n";
178 }
179
180 // Print out constants referenced by the function
181 printConstantPool(MF.getConstantPool());
182
183 // Print out labels for the function.
184 O << "\t.text\n";
185 emitAlignment(2);
186 O << "\t.globl\t" << CurrentFnName << "\n";
187 O << "\t.ent\t" << CurrentFnName << "\n";
188
189 O << CurrentFnName << ":\n";
190
191 // Print out code for the function.
192 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
193 I != E; ++I) {
194 // Print a label for the basic block.
195 O << "$LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
196 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
197 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
198 II != E; ++II) {
199 // Print the assembly for the instruction.
200 O << "\t";
201 printMachineInstruction(II);
202 }
203 }
204 ++LabelNumber;
205
206 O << "\t.end\t" << CurrentFnName << "\n";
207
208 // We didn't modify anything.
209 return false;
210}
211
212
213/// printConstantPool - Print to the current output stream assembly
214/// representations of the constants in the constant pool MCP. This is
215/// used to print out constants which have been "spilled to memory" by
216/// the code generator.
217///
218void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
219 const std::vector<Constant*> &CP = MCP->getConstants();
220 const TargetData &TD = TM.getTargetData();
221
222 if (CP.empty()) return;
223
224 abort();
225// for (unsigned i = 0, e = CP.size(); i != e; ++i) {
226// O << "\t.section\t.rodata\n";
227// emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
228// O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
229// << *CP[i] << "\n";
230// //emitGlobalConstant(CP[i]);
231// }
232}
233
234bool AlphaAsmPrinter::doInitialization(Module &M)
235{
236 AsmPrinter::doInitialization(M);
237 O << "\t.arch ev56\n";
238 return false;
239}
240
241
242// SwitchSection - Switch to the specified section of the executable if we are
243// not already in it!
244//
245static void SwitchSection(std::ostream &OS, std::string &CurSection,
246 const char *NewSection) {
247 if (CurSection != NewSection) {
248 CurSection = NewSection;
249 if (!CurSection.empty())
250 OS << "\t" << NewSection << "\n";
251 }
252}
253
254bool AlphaAsmPrinter::doFinalization(Module &M) {
255 const TargetData &TD = TM.getTargetData();
256 std::string CurSection;
257
258 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
259 if (I->hasInitializer()) { // External global require no code
260 O << "\n\n";
261 std::string name = Mang->getValueName(I);
262 Constant *C = I->getInitializer();
263 unsigned Size = TD.getTypeSize(C->getType());
264 unsigned Align = TD.getTypeAlignmentShift(C->getType());
265
266 if (C->isNullValue() &&
267 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
268 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
269 SwitchSection(O, CurSection, ".data");
270 if (I->hasInternalLinkage())
271 O << "\t.local " << name << "\n";
272
273 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
274 << "," << (1 << Align);
275 O << "\t\t# ";
276 WriteAsOperand(O, I, true, true, &M);
277 O << "\n";
278 } else {
279 switch (I->getLinkage()) {
280 case GlobalValue::LinkOnceLinkage:
281 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
282 // Nonnull linkonce -> weak
283 O << "\t.weak " << name << "\n";
284 SwitchSection(O, CurSection, "");
285 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
286 break;
287 case GlobalValue::AppendingLinkage:
288 // FIXME: appending linkage variables should go into a section of
289 // their name or something. For now, just emit them as external.
290 case GlobalValue::ExternalLinkage:
291 // If external or appending, declare as a global symbol
292 O << "\t.globl " << name << "\n";
293 // FALL THROUGH
294 case GlobalValue::InternalLinkage:
295 if (C->isNullValue())
296 SwitchSection(O, CurSection, ".bss");
297 else
298 SwitchSection(O, CurSection, ".data");
299 break;
300 case GlobalValue::GhostLinkage:
301 std::cerr << "GhostLinkage cannot appear in X86AsmPrinter!\n";
302 abort();
303 }
304
305 emitAlignment(Align);
306 O << "\t.type " << name << ",@object\n";
307 O << "\t.size " << name << "," << Size << "\n";
308 O << name << ":\t\t\t\t# ";
309 WriteAsOperand(O, I, true, true, &M);
310 O << " = ";
311 WriteAsOperand(O, C, false, false, &M);
312 O << "\n";
313 emitGlobalConstant(C);
314 }
315 }
316
317 AsmPrinter::doFinalization(M);
318 return false;
319}