blob: 4d70c1d66a71c3215a7c611329b430f1ff59ddd7 [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/ValueTypes.h"
22#include "llvm/CodeGen/AsmPrinter.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000023#include "llvm/Target/TargetMachine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000024#include "llvm/Support/Mangler.h"
25#include "llvm/ADT/Statistic.h"
Andrew Lenharth01269522005-01-24 18:37:48 +000026
Andrew Lenharth304d0f32005-01-22 23:41:55 +000027using namespace llvm;
28
29namespace {
30 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
31
32 struct AlphaAsmPrinter : public AsmPrinter {
33
34 /// Unique incrementer for label values for referencing Global values.
35 ///
36 unsigned LabelNumber;
Misha Brukman4633f1c2005-04-21 23:13:11 +000037
38 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
Chris Lattner87744a22005-11-21 07:38:08 +000039 : AsmPrinter(o, tm), LabelNumber(0) {
Andrew Lenharth440e6882005-02-04 14:09:38 +000040 AlignmentIsInBytes = false;
Chris Lattner81a994e2005-11-21 06:51:52 +000041 PrivateGlobalPrefix = "$";
Andrew Lenharth440e6882005-02-04 14:09:38 +000042 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000043
44 /// We name each basic block in a Function with a unique number, so
45 /// that we can consistently refer to them later. This is cleared
46 /// at the beginning of each call to runOnMachineFunction().
47 ///
48 typedef std::map<const Value *, unsigned> ValueMapTy;
49 ValueMapTy NumberForBB;
Andrew Lenharthc24b5372005-04-13 17:17:28 +000050 std::string CurSection;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000051
52 virtual const char *getPassName() const {
53 return "Alpha Assembly Printer";
54 }
55 bool printInstruction(const MachineInstr *MI);
56 void printOp(const MachineOperand &MO, bool IsCallOp = false);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000057 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
58 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
59 void printMachineInstruction(const MachineInstr *MI);
Misha Brukman4633f1c2005-04-21 23:13:11 +000060 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000061 bool doInitialization(Module &M);
62 bool doFinalization(Module &M);
63 };
64} // end of anonymous namespace
65
66/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
67/// assembly code for a MachineFunction to the given output stream,
68/// using the given target machine description. This should work
69/// regardless of whether the function is in SSA form.
70///
71FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
72 TargetMachine &tm) {
73 return new AlphaAsmPrinter(o, tm);
74}
75
76#include "AlphaGenAsmWriter.inc"
77
78void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
79{
80 const MachineOperand &MO = MI->getOperand(opNum);
81 if (MO.getType() == MachineOperand::MO_MachineRegister) {
82 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000083 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000084 } else if (MO.isImmediate()) {
85 O << MO.getImmedValue();
86 } else {
87 printOp(MO);
88 }
89}
90
91
92void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
93 const MRegisterInfo &RI = *TM.getRegisterInfo();
94 int new_symbol;
Misha Brukman4633f1c2005-04-21 23:13:11 +000095
Andrew Lenharth304d0f32005-01-22 23:41:55 +000096 switch (MO.getType()) {
97 case MachineOperand::MO_VirtualRegister:
98 if (Value *V = MO.getVRegValueOrNull()) {
99 O << "<" << V->getName() << ">";
100 return;
101 }
102 // FALLTHROUGH
103 case MachineOperand::MO_MachineRegister:
104 case MachineOperand::MO_CCRegister:
Andrew Lenharth01269522005-01-24 18:37:48 +0000105 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000106 return;
107
108 case MachineOperand::MO_SignExtendedImmed:
109 case MachineOperand::MO_UnextendedImmed:
110 std::cerr << "printOp() does not handle immediate values\n";
111 abort();
112 return;
113
114 case MachineOperand::MO_PCRelativeDisp:
Andrew Lenharth01269522005-01-24 18:37:48 +0000115 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000116 abort();
117 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000118
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000119 case MachineOperand::MO_MachineBasicBlock: {
120 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner87744a22005-11-21 07:38:08 +0000121 O << PrivateGlobalPrefix << "LBB"
122 << Mang->getValueName(MBBOp->getParent()->getFunction())
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000123 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
124 << MBBOp->getBasicBlock()->getName();
125 return;
126 }
127
128 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner60530102005-11-21 08:29:17 +0000129 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattner81a994e2005-11-21 06:51:52 +0000130 << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000131 return;
132
133 case MachineOperand::MO_ExternalSymbol:
134 O << MO.getSymbolName();
135 return;
136
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000137 case MachineOperand::MO_GlobalAddress:
138 //Abuse PCrel to specify pcrel calls
139 //calls are the only thing that use this flag
140 if (MO.isPCRelative())
Chris Lattner87744a22005-11-21 07:38:08 +0000141 O << PrivateGlobalPrefix << Mang->getValueName(MO.getGlobal()) << "..ng";
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000142 else
143 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000144 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000145
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000146 default:
147 O << "<unknown operand type: " << MO.getType() << ">";
148 return;
149 }
150}
151
152/// printMachineInstruction -- Print out a single Alpha MI to
153/// the current output stream.
154///
155void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
156 ++EmittedInsts;
157 if (printInstruction(MI))
158 return; // Printer was automatically generated
Misha Brukman4633f1c2005-04-21 23:13:11 +0000159
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000160 assert(0 && "Unhandled instruction in asm writer!");
161 abort();
162 return;
163}
164
165
166/// runOnMachineFunction - This uses the printMachineInstruction()
167/// method to print assembly for each instruction.
168///
169bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000170 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000171 O << "\n\n";
172
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000173 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000174 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000175
176 // Print out labels for the function.
Chris Lattner0a70a492005-11-21 07:30:28 +0000177 SwitchSection("\t.section .text", MF.getFunction());
Chris Lattner8b8b9512005-11-21 07:51:23 +0000178 EmitAlignment(4);
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000179 O << "\t.globl " << CurrentFnName << "\n";
180 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000181
182 O << CurrentFnName << ":\n";
183
184 // Print out code for the function.
185 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
186 I != E; ++I) {
187 // Print a label for the basic block.
Chris Lattner87744a22005-11-21 07:38:08 +0000188 O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_" << I->getNumber()
189 << ":\t" << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000190 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
191 II != E; ++II) {
192 // Print the assembly for the instruction.
193 O << "\t";
194 printMachineInstruction(II);
195 }
196 }
197 ++LabelNumber;
198
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000199 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000200
201 // We didn't modify anything.
202 return false;
203}
204
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000205bool AlphaAsmPrinter::doInitialization(Module &M)
206{
207 AsmPrinter::doInitialization(M);
Andrew Lenharth120ab482005-09-29 22:54:56 +0000208 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
209 || TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000210 O << "\t.arch ev6\n";
211 else
212 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000213 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000214 return false;
215}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000216
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000217bool AlphaAsmPrinter::doFinalization(Module &M) {
218 const TargetData &TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000219
Chris Lattnere4d5c442005-03-15 04:54:21 +0000220 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000221 if (I->hasInitializer()) { // External global require no code
222 O << "\n\n";
223 std::string name = Mang->getValueName(I);
224 Constant *C = I->getInitializer();
225 unsigned Size = TD.getTypeSize(C->getType());
226 unsigned Align = TD.getTypeAlignmentShift(C->getType());
227
Misha Brukman4633f1c2005-04-21 23:13:11 +0000228 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000229 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
230 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner0a70a492005-11-21 07:30:28 +0000231 SwitchSection("\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000232 if (I->hasInternalLinkage())
233 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000234
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000235 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
236 << "," << (1 << Align);
237 O << "\t\t# ";
238 WriteAsOperand(O, I, true, true, &M);
239 O << "\n";
240 } else {
241 switch (I->getLinkage()) {
242 case GlobalValue::LinkOnceLinkage:
243 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
244 // Nonnull linkonce -> weak
245 O << "\t.weak " << name << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000246 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
Chris Lattner0a70a492005-11-21 07:30:28 +0000247 SwitchSection("", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000248 break;
249 case GlobalValue::AppendingLinkage:
250 // FIXME: appending linkage variables should go into a section of
251 // their name or something. For now, just emit them as external.
252 case GlobalValue::ExternalLinkage:
253 // If external or appending, declare as a global symbol
254 O << "\t.globl " << name << "\n";
255 // FALL THROUGH
256 case GlobalValue::InternalLinkage:
Chris Lattner0a70a492005-11-21 07:30:28 +0000257 SwitchSection(C->isNullValue() ? "\t.section .bss" :
258 "\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000259 break;
260 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000261 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000262 abort();
263 }
264
Chris Lattner8b8b9512005-11-21 07:51:23 +0000265 EmitAlignment(Align);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000266 O << "\t.type " << name << ",@object\n";
267 O << "\t.size " << name << "," << Size << "\n";
268 O << name << ":\t\t\t\t# ";
269 WriteAsOperand(O, I, true, true, &M);
270 O << " = ";
271 WriteAsOperand(O, C, false, false, &M);
272 O << "\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000273 EmitGlobalConstant(C);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000274 }
275 }
276
277 AsmPrinter::doFinalization(M);
278 return false;
279}