blob: e902b2d597f66eb774d6c8e2c391d894e9cca42a [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 Lenharth120ab482005-09-29 22:54:56 +000017#include "AlphaTargetMachine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000018#include "llvm/Module.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000019#include "llvm/Type.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000020#include "llvm/Assembly/Writer.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000022#include "llvm/CodeGen/ValueTypes.h"
23#include "llvm/CodeGen/AsmPrinter.h"
24
25#include "llvm/Target/TargetMachine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000026
27#include "llvm/Support/Mangler.h"
28#include "llvm/ADT/Statistic.h"
Andrew Lenharth3ae18292005-04-14 16:24:00 +000029#include "llvm/Support/CommandLine.h"
Andrew Lenharth01269522005-01-24 18:37:48 +000030
Andrew Lenharth304d0f32005-01-22 23:41:55 +000031using namespace llvm;
32
33namespace {
34 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
35
36 struct AlphaAsmPrinter : public AsmPrinter {
37
38 /// Unique incrementer for label values for referencing Global values.
39 ///
40 unsigned LabelNumber;
Misha Brukman4633f1c2005-04-21 23:13:11 +000041
42 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
Andrew Lenharth440e6882005-02-04 14:09:38 +000043 : AsmPrinter(o, tm), LabelNumber(0)
44 {
45 AlignmentIsInBytes = false;
Chris Lattner81a994e2005-11-21 06:51:52 +000046 PrivateGlobalPrefix = "$";
Andrew Lenharth440e6882005-02-04 14:09:38 +000047 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000048
49 /// We name each basic block in a Function with a unique number, so
50 /// that we can consistently refer to them later. This is cleared
51 /// at the beginning of each call to runOnMachineFunction().
52 ///
53 typedef std::map<const Value *, unsigned> ValueMapTy;
54 ValueMapTy NumberForBB;
Andrew Lenharthc24b5372005-04-13 17:17:28 +000055 std::string CurSection;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000056
57 virtual const char *getPassName() const {
58 return "Alpha Assembly Printer";
59 }
60 bool printInstruction(const MachineInstr *MI);
61 void printOp(const MachineOperand &MO, bool IsCallOp = false);
62 void printConstantPool(MachineConstantPool *MCP);
63 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
64 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
65 void printMachineInstruction(const MachineInstr *MI);
Misha Brukman4633f1c2005-04-21 23:13:11 +000066 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000067 bool doInitialization(Module &M);
68 bool doFinalization(Module &M);
Andrew Lenharthc24b5372005-04-13 17:17:28 +000069 void SwitchSection(std::ostream &OS, const char *NewSection);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000070 };
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??");
Andrew Lenharth01269522005-01-24 18:37:48 +000090 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000091 } 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;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000102
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000103 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:
Andrew Lenharth01269522005-01-24 18:37:48 +0000112 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000113 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:
Andrew Lenharth01269522005-01-24 18:37:48 +0000122 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000123 abort();
124 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000125
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000126 case MachineOperand::MO_MachineBasicBlock: {
127 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Andrew Lenhartheee2a882005-06-06 19:03:09 +0000128 O << "$LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000129 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
130 << MBBOp->getBasicBlock()->getName();
131 return;
132 }
133
134 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner81a994e2005-11-21 06:51:52 +0000135 O << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_"
136 << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000137 return;
138
139 case MachineOperand::MO_ExternalSymbol:
140 O << MO.getSymbolName();
141 return;
142
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000143 case MachineOperand::MO_GlobalAddress:
144 //Abuse PCrel to specify pcrel calls
145 //calls are the only thing that use this flag
146 if (MO.isPCRelative())
147 O << "$" << Mang->getValueName(MO.getGlobal()) << "..ng";
148 else
149 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000150 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000151
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000152 default:
153 O << "<unknown operand type: " << MO.getType() << ">";
154 return;
155 }
156}
157
158/// printMachineInstruction -- Print out a single Alpha MI to
159/// the current output stream.
160///
161void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
162 ++EmittedInsts;
163 if (printInstruction(MI))
164 return; // Printer was automatically generated
Misha Brukman4633f1c2005-04-21 23:13:11 +0000165
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000166 assert(0 && "Unhandled instruction in asm writer!");
167 abort();
168 return;
169}
170
171
172/// runOnMachineFunction - This uses the printMachineInstruction()
173/// method to print assembly for each instruction.
174///
175bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
176 setupMachineFunction(MF);
177 O << "\n\n";
178
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000179 // Print out constants referenced by the function
180 printConstantPool(MF.getConstantPool());
181
182 // Print out labels for the function.
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000183 SwitchSection(O, "text");
184 emitAlignment(4);
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000185 O << "\t.globl " << CurrentFnName << "\n";
186 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000187
188 O << CurrentFnName << ":\n";
189
190 // Print out code for the function.
191 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
192 I != E; ++I) {
193 // Print a label for the basic block.
Andrew Lenhartheee2a882005-06-06 19:03:09 +0000194 O << "$LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000195 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
196 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
197 II != E; ++II) {
198 // Print the assembly for the instruction.
199 O << "\t";
200 printMachineInstruction(II);
201 }
202 }
203 ++LabelNumber;
204
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000205 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000206
207 // We didn't modify anything.
208 return false;
209}
210
211
212/// printConstantPool - Print to the current output stream assembly
213/// representations of the constants in the constant pool MCP. This is
214/// used to print out constants which have been "spilled to memory" by
215/// the code generator.
216///
217void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
218 const std::vector<Constant*> &CP = MCP->getConstants();
219 const TargetData &TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000220
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000221 if (CP.empty()) return;
222
Andrew Lenharth21e786b2005-08-12 16:13:43 +0000223 SwitchSection(O, "rodata");
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000224 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000225 // SwitchSection(O, "section .rodata, \"dr\"");
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000226 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
Chris Lattner81a994e2005-11-21 06:51:52 +0000227 O << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_" << i
228 << ":\t\t\t\t\t" << CommentString << *CP[i] << "\n";
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000229 emitGlobalConstant(CP[i]);
230 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000231}
232
233bool AlphaAsmPrinter::doInitialization(Module &M)
234{
235 AsmPrinter::doInitialization(M);
Andrew Lenharth120ab482005-09-29 22:54:56 +0000236 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
237 || TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000238 O << "\t.arch ev6\n";
239 else
240 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000241 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000242 return false;
243}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000244
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000245
246// SwitchSection - Switch to the specified section of the executable if we are
247// not already in it!
248//
Misha Brukman4633f1c2005-04-21 23:13:11 +0000249void AlphaAsmPrinter::SwitchSection(std::ostream &OS, const char *NewSection)
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000250{
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000251 if (CurSection != NewSection) {
252 CurSection = NewSection;
253 if (!CurSection.empty())
Andrew Lenharth21e786b2005-08-12 16:13:43 +0000254 OS << "\t.section ." << NewSection << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000255 }
256}
257
258bool AlphaAsmPrinter::doFinalization(Module &M) {
259 const TargetData &TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000260
Chris Lattnere4d5c442005-03-15 04:54:21 +0000261 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000262 if (I->hasInitializer()) { // External global require no code
263 O << "\n\n";
264 std::string name = Mang->getValueName(I);
265 Constant *C = I->getInitializer();
266 unsigned Size = TD.getTypeSize(C->getType());
267 unsigned Align = TD.getTypeAlignmentShift(C->getType());
268
Misha Brukman4633f1c2005-04-21 23:13:11 +0000269 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000270 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
271 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000272 SwitchSection(O, "data");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000273 if (I->hasInternalLinkage())
274 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000275
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000276 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
277 << "," << (1 << Align);
278 O << "\t\t# ";
279 WriteAsOperand(O, I, true, true, &M);
280 O << "\n";
281 } else {
282 switch (I->getLinkage()) {
283 case GlobalValue::LinkOnceLinkage:
284 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
285 // Nonnull linkonce -> weak
286 O << "\t.weak " << name << "\n";
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000287 SwitchSection(O, "");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000288 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
289 break;
290 case GlobalValue::AppendingLinkage:
291 // FIXME: appending linkage variables should go into a section of
292 // their name or something. For now, just emit them as external.
293 case GlobalValue::ExternalLinkage:
294 // If external or appending, declare as a global symbol
295 O << "\t.globl " << name << "\n";
296 // FALL THROUGH
297 case GlobalValue::InternalLinkage:
298 if (C->isNullValue())
Andrew Lenharth21e786b2005-08-12 16:13:43 +0000299 SwitchSection(O, "bss");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000300 else
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000301 SwitchSection(O, "data");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000302 break;
303 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000304 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000305 abort();
306 }
307
308 emitAlignment(Align);
309 O << "\t.type " << name << ",@object\n";
310 O << "\t.size " << name << "," << Size << "\n";
311 O << name << ":\t\t\t\t# ";
312 WriteAsOperand(O, I, true, true, &M);
313 O << " = ";
314 WriteAsOperand(O, C, false, false, &M);
315 O << "\n";
316 emitGlobalConstant(C);
317 }
318 }
319
320 AsmPrinter::doFinalization(M);
321 return false;
322}