blob: 3e7d5e7bbcbf5de2624adde3addaf52736df21a7 [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;
Andrew Lenharthc24b5372005-04-13 17:17:28 +000052 std::string CurSection;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000053
54 virtual const char *getPassName() const {
55 return "Alpha Assembly Printer";
56 }
57 bool printInstruction(const MachineInstr *MI);
58 void printOp(const MachineOperand &MO, bool IsCallOp = false);
59 void printConstantPool(MachineConstantPool *MCP);
60 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
61 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
62 void printMachineInstruction(const MachineInstr *MI);
63 bool runOnMachineFunction(MachineFunction &F);
64 bool doInitialization(Module &M);
65 bool doFinalization(Module &M);
Andrew Lenharthc24b5372005-04-13 17:17:28 +000066 void SwitchSection(std::ostream &OS, const char *NewSection);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000067 };
68} // end of anonymous namespace
69
70/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
71/// assembly code for a MachineFunction to the given output stream,
72/// using the given target machine description. This should work
73/// regardless of whether the function is in SSA form.
74///
75FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
76 TargetMachine &tm) {
77 return new AlphaAsmPrinter(o, tm);
78}
79
80#include "AlphaGenAsmWriter.inc"
81
82void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
83{
84 const MachineOperand &MO = MI->getOperand(opNum);
85 if (MO.getType() == MachineOperand::MO_MachineRegister) {
86 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000087 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000088 } else if (MO.isImmediate()) {
89 O << MO.getImmedValue();
90 } else {
91 printOp(MO);
92 }
93}
94
95
96void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
97 const MRegisterInfo &RI = *TM.getRegisterInfo();
98 int new_symbol;
99
100 switch (MO.getType()) {
101 case MachineOperand::MO_VirtualRegister:
102 if (Value *V = MO.getVRegValueOrNull()) {
103 O << "<" << V->getName() << ">";
104 return;
105 }
106 // FALLTHROUGH
107 case MachineOperand::MO_MachineRegister:
108 case MachineOperand::MO_CCRegister:
Andrew Lenharth01269522005-01-24 18:37:48 +0000109 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000110 return;
111
112 case MachineOperand::MO_SignExtendedImmed:
113 case MachineOperand::MO_UnextendedImmed:
114 std::cerr << "printOp() does not handle immediate values\n";
115 abort();
116 return;
117
118 case MachineOperand::MO_PCRelativeDisp:
Andrew Lenharth01269522005-01-24 18:37:48 +0000119 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000120 abort();
121 return;
122
123 case MachineOperand::MO_MachineBasicBlock: {
124 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000125 O << "LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000126 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
127 << MBBOp->getBasicBlock()->getName();
128 return;
129 }
130
131 case MachineOperand::MO_ConstantPoolIndex:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000132 O << "CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000133 return;
134
135 case MachineOperand::MO_ExternalSymbol:
136 O << MO.getSymbolName();
137 return;
138
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000139 case MachineOperand::MO_GlobalAddress:
140 //Abuse PCrel to specify pcrel calls
141 //calls are the only thing that use this flag
142 if (MO.isPCRelative())
143 O << "$" << Mang->getValueName(MO.getGlobal()) << "..ng";
144 else
145 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000146 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
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000175 // Print out constants referenced by the function
176 printConstantPool(MF.getConstantPool());
177
178 // Print out labels for the function.
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000179 SwitchSection(O, "text");
180 emitAlignment(4);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000181 O << "\t.globl\t" << CurrentFnName << "\n";
182 O << "\t.ent\t" << CurrentFnName << "\n";
183
184 O << CurrentFnName << ":\n";
185
186 // Print out code for the function.
187 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
188 I != E; ++I) {
189 // Print a label for the basic block.
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000190 O << "LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000191 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
192 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
193 II != E; ++II) {
194 // Print the assembly for the instruction.
195 O << "\t";
196 printMachineInstruction(II);
197 }
198 }
199 ++LabelNumber;
200
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000201 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000202
203 // We didn't modify anything.
204 return false;
205}
206
207
208/// printConstantPool - Print to the current output stream assembly
209/// representations of the constants in the constant pool MCP. This is
210/// used to print out constants which have been "spilled to memory" by
211/// the code generator.
212///
213void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
214 const std::vector<Constant*> &CP = MCP->getConstants();
215 const TargetData &TD = TM.getTargetData();
216
217 if (CP.empty()) return;
218
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000219 SwitchSection(O, "section .rodata");
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000220 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000221 // SwitchSection(O, "section .rodata, \"dr\"");
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000222 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000223 O << "CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000224 << *CP[i] << "\n";
225 emitGlobalConstant(CP[i]);
226 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000227}
228
229bool AlphaAsmPrinter::doInitialization(Module &M)
230{
231 AsmPrinter::doInitialization(M);
232 O << "\t.arch ev56\n";
233 return false;
234}
235
236
237// SwitchSection - Switch to the specified section of the executable if we are
238// not already in it!
239//
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000240void AlphaAsmPrinter::SwitchSection(std::ostream &OS, const char *NewSection)
241{
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000242 if (CurSection != NewSection) {
243 CurSection = NewSection;
244 if (!CurSection.empty())
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000245 OS << "\t." << NewSection << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000246 }
247}
248
249bool AlphaAsmPrinter::doFinalization(Module &M) {
250 const TargetData &TD = TM.getTargetData();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000251
Chris Lattnere4d5c442005-03-15 04:54:21 +0000252 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000253 if (I->hasInitializer()) { // External global require no code
254 O << "\n\n";
255 std::string name = Mang->getValueName(I);
256 Constant *C = I->getInitializer();
257 unsigned Size = TD.getTypeSize(C->getType());
258 unsigned Align = TD.getTypeAlignmentShift(C->getType());
259
260 if (C->isNullValue() &&
261 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
262 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000263 SwitchSection(O, "data");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000264 if (I->hasInternalLinkage())
265 O << "\t.local " << name << "\n";
266
267 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
268 << "," << (1 << Align);
269 O << "\t\t# ";
270 WriteAsOperand(O, I, true, true, &M);
271 O << "\n";
272 } else {
273 switch (I->getLinkage()) {
274 case GlobalValue::LinkOnceLinkage:
275 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
276 // Nonnull linkonce -> weak
277 O << "\t.weak " << name << "\n";
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000278 SwitchSection(O, "");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000279 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
280 break;
281 case GlobalValue::AppendingLinkage:
282 // FIXME: appending linkage variables should go into a section of
283 // their name or something. For now, just emit them as external.
284 case GlobalValue::ExternalLinkage:
285 // If external or appending, declare as a global symbol
286 O << "\t.globl " << name << "\n";
287 // FALL THROUGH
288 case GlobalValue::InternalLinkage:
289 if (C->isNullValue())
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000290 SwitchSection(O, "bss"); //was .bss
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000291 else
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000292 SwitchSection(O, "data");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000293 break;
294 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000295 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000296 abort();
297 }
298
299 emitAlignment(Align);
300 O << "\t.type " << name << ",@object\n";
301 O << "\t.size " << name << "," << Size << "\n";
302 O << name << ":\t\t\t\t# ";
303 WriteAsOperand(O, I, true, true, &M);
304 O << " = ";
305 WriteAsOperand(O, C, false, false, &M);
306 O << "\n";
307 emitGlobalConstant(C);
308 }
309 }
310
311 AsmPrinter::doFinalization(M);
312 return false;
313}