blob: abfc16cd38bfa488895149741b54f5c96f77a017 [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;
34}
35
Andrew Lenharth304d0f32005-01-22 23:41:55 +000036namespace {
37 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
38
39 struct AlphaAsmPrinter : public AsmPrinter {
40
41 /// Unique incrementer for label values for referencing Global values.
42 ///
43 unsigned LabelNumber;
Misha Brukman4633f1c2005-04-21 23:13:11 +000044
45 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
Andrew Lenharth440e6882005-02-04 14:09:38 +000046 : AsmPrinter(o, tm), LabelNumber(0)
47 {
48 AlignmentIsInBytes = false;
49 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000050
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;
Andrew Lenharthc24b5372005-04-13 17:17:28 +000057 std::string CurSection;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000058
59 virtual const char *getPassName() const {
60 return "Alpha Assembly Printer";
61 }
62 bool printInstruction(const MachineInstr *MI);
63 void printOp(const MachineOperand &MO, bool IsCallOp = false);
64 void printConstantPool(MachineConstantPool *MCP);
65 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
66 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
67 void printMachineInstruction(const MachineInstr *MI);
Misha Brukman4633f1c2005-04-21 23:13:11 +000068 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000069 bool doInitialization(Module &M);
70 bool doFinalization(Module &M);
Andrew Lenharthc24b5372005-04-13 17:17:28 +000071 void SwitchSection(std::ostream &OS, const char *NewSection);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000072 };
73} // end of anonymous namespace
74
75/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
76/// assembly code for a MachineFunction to the given output stream,
77/// using the given target machine description. This should work
78/// regardless of whether the function is in SSA form.
79///
80FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
81 TargetMachine &tm) {
82 return new AlphaAsmPrinter(o, tm);
83}
84
85#include "AlphaGenAsmWriter.inc"
86
87void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
88{
89 const MachineOperand &MO = MI->getOperand(opNum);
90 if (MO.getType() == MachineOperand::MO_MachineRegister) {
91 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000092 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000093 } else if (MO.isImmediate()) {
94 O << MO.getImmedValue();
95 } else {
96 printOp(MO);
97 }
98}
99
100
101void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
102 const MRegisterInfo &RI = *TM.getRegisterInfo();
103 int new_symbol;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000104
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000105 switch (MO.getType()) {
106 case MachineOperand::MO_VirtualRegister:
107 if (Value *V = MO.getVRegValueOrNull()) {
108 O << "<" << V->getName() << ">";
109 return;
110 }
111 // FALLTHROUGH
112 case MachineOperand::MO_MachineRegister:
113 case MachineOperand::MO_CCRegister:
Andrew Lenharth01269522005-01-24 18:37:48 +0000114 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000115 return;
116
117 case MachineOperand::MO_SignExtendedImmed:
118 case MachineOperand::MO_UnextendedImmed:
119 std::cerr << "printOp() does not handle immediate values\n";
120 abort();
121 return;
122
123 case MachineOperand::MO_PCRelativeDisp:
Andrew Lenharth01269522005-01-24 18:37:48 +0000124 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000125 abort();
126 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000127
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000128 case MachineOperand::MO_MachineBasicBlock: {
129 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000130 O << "LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000131 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
132 << MBBOp->getBasicBlock()->getName();
133 return;
134 }
135
136 case MachineOperand::MO_ConstantPoolIndex:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000137 O << "CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000138 return;
139
140 case MachineOperand::MO_ExternalSymbol:
141 O << MO.getSymbolName();
142 return;
143
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000144 case MachineOperand::MO_GlobalAddress:
145 //Abuse PCrel to specify pcrel calls
146 //calls are the only thing that use this flag
147 if (MO.isPCRelative())
148 O << "$" << Mang->getValueName(MO.getGlobal()) << "..ng";
149 else
150 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000151 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000152
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000153 default:
154 O << "<unknown operand type: " << MO.getType() << ">";
155 return;
156 }
157}
158
159/// printMachineInstruction -- Print out a single Alpha MI to
160/// the current output stream.
161///
162void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
163 ++EmittedInsts;
164 if (printInstruction(MI))
165 return; // Printer was automatically generated
Misha Brukman4633f1c2005-04-21 23:13:11 +0000166
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000167 assert(0 && "Unhandled instruction in asm writer!");
168 abort();
169 return;
170}
171
172
173/// runOnMachineFunction - This uses the printMachineInstruction()
174/// method to print assembly for each instruction.
175///
176bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
177 setupMachineFunction(MF);
178 O << "\n\n";
179
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000180 // Print out constants referenced by the function
181 printConstantPool(MF.getConstantPool());
182
183 // Print out labels for the function.
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000184 SwitchSection(O, "text");
185 emitAlignment(4);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000186 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.
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000195 O << "LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000196 << 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
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000206 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000207
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();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000221
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000222 if (CP.empty()) return;
223
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000224 SwitchSection(O, "section .rodata");
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000225 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000226 // SwitchSection(O, "section .rodata, \"dr\"");
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000227 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000228 O << "CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000229 << *CP[i] << "\n";
230 emitGlobalConstant(CP[i]);
231 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000232}
233
234bool AlphaAsmPrinter::doInitialization(Module &M)
235{
236 AsmPrinter::doInitialization(M);
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000237 if(EnableAlphaFTOI)
238 O << "\t.arch ev6\n";
239 else
240 O << "\t.arch ev56\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000241 return false;
242}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000243
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000244
245// SwitchSection - Switch to the specified section of the executable if we are
246// not already in it!
247//
Misha Brukman4633f1c2005-04-21 23:13:11 +0000248void AlphaAsmPrinter::SwitchSection(std::ostream &OS, const char *NewSection)
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000249{
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000250 if (CurSection != NewSection) {
251 CurSection = NewSection;
252 if (!CurSection.empty())
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000253 OS << "\t." << NewSection << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000254 }
255}
256
257bool AlphaAsmPrinter::doFinalization(Module &M) {
258 const TargetData &TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000259
Chris Lattnere4d5c442005-03-15 04:54:21 +0000260 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000261 if (I->hasInitializer()) { // External global require no code
262 O << "\n\n";
263 std::string name = Mang->getValueName(I);
264 Constant *C = I->getInitializer();
265 unsigned Size = TD.getTypeSize(C->getType());
266 unsigned Align = TD.getTypeAlignmentShift(C->getType());
267
Misha Brukman4633f1c2005-04-21 23:13:11 +0000268 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000269 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
270 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000271 SwitchSection(O, "data");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000272 if (I->hasInternalLinkage())
273 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000274
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000275 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
276 << "," << (1 << Align);
277 O << "\t\t# ";
278 WriteAsOperand(O, I, true, true, &M);
279 O << "\n";
280 } else {
281 switch (I->getLinkage()) {
282 case GlobalValue::LinkOnceLinkage:
283 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
284 // Nonnull linkonce -> weak
285 O << "\t.weak " << name << "\n";
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000286 SwitchSection(O, "");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000287 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
288 break;
289 case GlobalValue::AppendingLinkage:
290 // FIXME: appending linkage variables should go into a section of
291 // their name or something. For now, just emit them as external.
292 case GlobalValue::ExternalLinkage:
293 // If external or appending, declare as a global symbol
294 O << "\t.globl " << name << "\n";
295 // FALL THROUGH
296 case GlobalValue::InternalLinkage:
297 if (C->isNullValue())
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000298 SwitchSection(O, "bss"); //was .bss
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000299 else
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000300 SwitchSection(O, "data");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000301 break;
302 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000303 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000304 abort();
305 }
306
307 emitAlignment(Align);
308 O << "\t.type " << name << ",@object\n";
309 O << "\t.size " << name << "," << Size << "\n";
310 O << name << ":\t\t\t\t# ";
311 WriteAsOperand(O, I, true, true, &M);
312 O << " = ";
313 WriteAsOperand(O, C, false, false, &M);
314 O << "\n";
315 emitGlobalConstant(C);
316 }
317 }
318
319 AsmPrinter::doFinalization(M);
320 return false;
321}