blob: 9581c0c532e26b218c7c4d581d12a3bbb480f264 [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;
46 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000047
48 /// We name each basic block in a Function with a unique number, so
49 /// that we can consistently refer to them later. This is cleared
50 /// at the beginning of each call to runOnMachineFunction().
51 ///
52 typedef std::map<const Value *, unsigned> ValueMapTy;
53 ValueMapTy NumberForBB;
Andrew Lenharthc24b5372005-04-13 17:17:28 +000054 std::string CurSection;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000055
56 virtual const char *getPassName() const {
57 return "Alpha Assembly Printer";
58 }
59 bool printInstruction(const MachineInstr *MI);
60 void printOp(const MachineOperand &MO, bool IsCallOp = false);
61 void printConstantPool(MachineConstantPool *MCP);
62 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
63 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
64 void printMachineInstruction(const MachineInstr *MI);
Misha Brukman4633f1c2005-04-21 23:13:11 +000065 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000066 bool doInitialization(Module &M);
67 bool doFinalization(Module &M);
Andrew Lenharthc24b5372005-04-13 17:17:28 +000068 void SwitchSection(std::ostream &OS, const char *NewSection);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000069 };
70} // end of anonymous namespace
71
72/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
73/// assembly code for a MachineFunction to the given output stream,
74/// using the given target machine description. This should work
75/// regardless of whether the function is in SSA form.
76///
77FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
78 TargetMachine &tm) {
79 return new AlphaAsmPrinter(o, tm);
80}
81
82#include "AlphaGenAsmWriter.inc"
83
84void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
85{
86 const MachineOperand &MO = MI->getOperand(opNum);
87 if (MO.getType() == MachineOperand::MO_MachineRegister) {
88 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000089 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000090 } else if (MO.isImmediate()) {
91 O << MO.getImmedValue();
92 } else {
93 printOp(MO);
94 }
95}
96
97
98void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
99 const MRegisterInfo &RI = *TM.getRegisterInfo();
100 int new_symbol;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000101
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000102 switch (MO.getType()) {
103 case MachineOperand::MO_VirtualRegister:
104 if (Value *V = MO.getVRegValueOrNull()) {
105 O << "<" << V->getName() << ">";
106 return;
107 }
108 // FALLTHROUGH
109 case MachineOperand::MO_MachineRegister:
110 case MachineOperand::MO_CCRegister:
Andrew Lenharth01269522005-01-24 18:37:48 +0000111 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000112 return;
113
114 case MachineOperand::MO_SignExtendedImmed:
115 case MachineOperand::MO_UnextendedImmed:
116 std::cerr << "printOp() does not handle immediate values\n";
117 abort();
118 return;
119
120 case MachineOperand::MO_PCRelativeDisp:
Andrew Lenharth01269522005-01-24 18:37:48 +0000121 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000122 abort();
123 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000124
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000125 case MachineOperand::MO_MachineBasicBlock: {
126 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Andrew Lenhartheee2a882005-06-06 19:03:09 +0000127 O << "$LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000128 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
129 << MBBOp->getBasicBlock()->getName();
130 return;
131 }
132
133 case MachineOperand::MO_ConstantPoolIndex:
Andrew Lenharth98f0eee2005-06-27 16:29:54 +0000134 O << "$CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000135 return;
136
137 case MachineOperand::MO_ExternalSymbol:
138 O << MO.getSymbolName();
139 return;
140
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000141 case MachineOperand::MO_GlobalAddress:
142 //Abuse PCrel to specify pcrel calls
143 //calls are the only thing that use this flag
144 if (MO.isPCRelative())
145 O << "$" << Mang->getValueName(MO.getGlobal()) << "..ng";
146 else
147 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000148 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000149
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000150 default:
151 O << "<unknown operand type: " << MO.getType() << ">";
152 return;
153 }
154}
155
156/// printMachineInstruction -- Print out a single Alpha MI to
157/// the current output stream.
158///
159void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
160 ++EmittedInsts;
161 if (printInstruction(MI))
162 return; // Printer was automatically generated
Misha Brukman4633f1c2005-04-21 23:13:11 +0000163
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000164 assert(0 && "Unhandled instruction in asm writer!");
165 abort();
166 return;
167}
168
169
170/// runOnMachineFunction - This uses the printMachineInstruction()
171/// method to print assembly for each instruction.
172///
173bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
174 setupMachineFunction(MF);
175 O << "\n\n";
176
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000177 // Print out constants referenced by the function
178 printConstantPool(MF.getConstantPool());
179
180 // Print out labels for the function.
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000181 SwitchSection(O, "text");
182 emitAlignment(4);
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000183 O << "\t.globl " << CurrentFnName << "\n";
184 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000185
186 O << CurrentFnName << ":\n";
187
188 // Print out code for the function.
189 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
190 I != E; ++I) {
191 // Print a label for the basic block.
Andrew Lenhartheee2a882005-06-06 19:03:09 +0000192 O << "$LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000193 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
194 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
195 II != E; ++II) {
196 // Print the assembly for the instruction.
197 O << "\t";
198 printMachineInstruction(II);
199 }
200 }
201 ++LabelNumber;
202
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000203 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000204
205 // We didn't modify anything.
206 return false;
207}
208
209
210/// printConstantPool - Print to the current output stream assembly
211/// representations of the constants in the constant pool MCP. This is
212/// used to print out constants which have been "spilled to memory" by
213/// the code generator.
214///
215void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
216 const std::vector<Constant*> &CP = MCP->getConstants();
217 const TargetData &TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000218
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000219 if (CP.empty()) return;
220
Andrew Lenharth21e786b2005-08-12 16:13:43 +0000221 SwitchSection(O, "rodata");
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000222 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000223 // SwitchSection(O, "section .rodata, \"dr\"");
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000224 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
Andrew Lenharth98f0eee2005-06-27 16:29:54 +0000225 O << "$CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
Andrew Lenharthf61ed952005-02-01 20:38:53 +0000226 << *CP[i] << "\n";
227 emitGlobalConstant(CP[i]);
228 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000229}
230
231bool AlphaAsmPrinter::doInitialization(Module &M)
232{
233 AsmPrinter::doInitialization(M);
Andrew Lenharth120ab482005-09-29 22:54:56 +0000234 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
235 || TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000236 O << "\t.arch ev6\n";
237 else
238 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000239 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000240 return false;
241}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000242
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000243
244// SwitchSection - Switch to the specified section of the executable if we are
245// not already in it!
246//
Misha Brukman4633f1c2005-04-21 23:13:11 +0000247void AlphaAsmPrinter::SwitchSection(std::ostream &OS, const char *NewSection)
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000248{
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000249 if (CurSection != NewSection) {
250 CurSection = NewSection;
251 if (!CurSection.empty())
Andrew Lenharth21e786b2005-08-12 16:13:43 +0000252 OS << "\t.section ." << NewSection << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000253 }
254}
255
256bool AlphaAsmPrinter::doFinalization(Module &M) {
257 const TargetData &TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000258
Chris Lattnere4d5c442005-03-15 04:54:21 +0000259 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000260 if (I->hasInitializer()) { // External global require no code
261 O << "\n\n";
262 std::string name = Mang->getValueName(I);
263 Constant *C = I->getInitializer();
264 unsigned Size = TD.getTypeSize(C->getType());
265 unsigned Align = TD.getTypeAlignmentShift(C->getType());
266
Misha Brukman4633f1c2005-04-21 23:13:11 +0000267 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000268 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
269 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000270 SwitchSection(O, "data");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000271 if (I->hasInternalLinkage())
272 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000273
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000274 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
275 << "," << (1 << Align);
276 O << "\t\t# ";
277 WriteAsOperand(O, I, true, true, &M);
278 O << "\n";
279 } else {
280 switch (I->getLinkage()) {
281 case GlobalValue::LinkOnceLinkage:
282 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
283 // Nonnull linkonce -> weak
284 O << "\t.weak " << name << "\n";
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000285 SwitchSection(O, "");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000286 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
287 break;
288 case GlobalValue::AppendingLinkage:
289 // FIXME: appending linkage variables should go into a section of
290 // their name or something. For now, just emit them as external.
291 case GlobalValue::ExternalLinkage:
292 // If external or appending, declare as a global symbol
293 O << "\t.globl " << name << "\n";
294 // FALL THROUGH
295 case GlobalValue::InternalLinkage:
296 if (C->isNullValue())
Andrew Lenharth21e786b2005-08-12 16:13:43 +0000297 SwitchSection(O, "bss");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000298 else
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000299 SwitchSection(O, "data");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000300 break;
301 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000302 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000303 abort();
304 }
305
306 emitAlignment(Align);
307 O << "\t.type " << name << ",@object\n";
308 O << "\t.size " << name << "," << Size << "\n";
309 O << name << ":\t\t\t\t# ";
310 WriteAsOperand(O, I, true, true, &M);
311 O << " = ";
312 WriteAsOperand(O, C, false, false, &M);
313 O << "\n";
314 emitGlobalConstant(C);
315 }
316 }
317
318 AsmPrinter::doFinalization(M);
319 return false;
320}