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