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