blob: 03ed3d6b68f1a179219ed2000b9ff7ab660fdaea [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- AlphaAsmPrinter.cpp - Alpha LLVM assembly writer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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#define DEBUG_TYPE "asm-printer"
16#include "Alpha.h"
17#include "AlphaInstrInfo.h"
18#include "AlphaTargetMachine.h"
19#include "llvm/Module.h"
20#include "llvm/Type.h"
21#include "llvm/Assembly/Writer.h"
22#include "llvm/CodeGen/AsmPrinter.h"
23#include "llvm/Target/TargetAsmInfo.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Support/Compiler.h"
26#include "llvm/Support/Mangler.h"
27#include "llvm/ADT/Statistic.h"
28using namespace llvm;
29
30STATISTIC(EmittedInsts, "Number of machine instrs printed");
31
32namespace {
33 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
34
35 /// Unique incrementer for label values for referencing Global values.
36 ///
37
38 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
39 : AsmPrinter(o, tm, T) {
40 }
41
42 virtual const char *getPassName() const {
43 return "Alpha Assembly Printer";
44 }
45 bool printInstruction(const MachineInstr *MI);
46 void printOp(const MachineOperand &MO, bool IsCallOp = false);
47 void printOperand(const MachineInstr *MI, int opNum);
48 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
49 bool runOnMachineFunction(MachineFunction &F);
50 bool doInitialization(Module &M);
51 bool doFinalization(Module &M);
52
53 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
54 unsigned AsmVariant, const char *ExtraCode);
55 bool PrintAsmMemoryOperand(const MachineInstr *MI,
56 unsigned OpNo,
57 unsigned AsmVariant,
58 const char *ExtraCode);
59 };
60} // end of anonymous namespace
61
62/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
63/// assembly code for a MachineFunction to the given output stream,
64/// using the given target machine description. This should work
65/// regardless of whether the function is in SSA form.
66///
67FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
68 TargetMachine &tm) {
69 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
70}
71
72#include "AlphaGenAsmWriter.inc"
73
74void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
75{
76 const MachineOperand &MO = MI->getOperand(opNum);
77 if (MO.getType() == MachineOperand::MO_Register) {
78 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
79 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
80 } else if (MO.isImmediate()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000081 O << MO.getImm();
82 assert(MO.getImm() < (1 << 30));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 } else {
84 printOp(MO);
85 }
86}
87
88
89void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
90 const MRegisterInfo &RI = *TM.getRegisterInfo();
91
92 switch (MO.getType()) {
93 case MachineOperand::MO_Register:
94 O << RI.get(MO.getReg()).Name;
95 return;
96
97 case MachineOperand::MO_Immediate:
98 cerr << "printOp() does not handle immediate values\n";
99 abort();
100 return;
101
102 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000103 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 return;
105
106 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000107 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000108 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 return;
110
111 case MachineOperand::MO_ExternalSymbol:
112 O << MO.getSymbolName();
113 return;
114
115 case MachineOperand::MO_GlobalAddress: {
116 GlobalValue *GV = MO.getGlobal();
117 O << Mang->getValueName(GV);
118 if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
119 ExtWeakSymbols.insert(GV);
120 return;
121 }
122
123 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000124 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000125 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 return;
127
128 default:
129 O << "<unknown operand type: " << MO.getType() << ">";
130 return;
131 }
132}
133
134/// runOnMachineFunction - This uses the printMachineInstruction()
135/// method to print assembly for each instruction.
136///
137bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
138 SetupMachineFunction(MF);
139 O << "\n\n";
140
141 // Print out constants referenced by the function
142 EmitConstantPool(MF.getConstantPool());
143
144 // Print out jump tables referenced by the function
145 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
146
147 // Print out labels for the function.
148 const Function *F = MF.getFunction();
149 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
150
151 EmitAlignment(4, F);
152 switch (F->getLinkage()) {
153 default: assert(0 && "Unknown linkage type!");
154 case Function::InternalLinkage: // Symbols default to internal.
155 break;
156 case Function::ExternalLinkage:
157 O << "\t.globl " << CurrentFnName << "\n";
158 break;
159 case Function::WeakLinkage:
160 case Function::LinkOnceLinkage:
161 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
162 break;
163 }
164
165 O << "\t.ent " << CurrentFnName << "\n";
166
167 O << CurrentFnName << ":\n";
168
169 // Print out code for the function.
170 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
171 I != E; ++I) {
172 if (I != MF.begin()) {
173 printBasicBlockLabel(I, true);
174 O << '\n';
175 }
176 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
177 II != E; ++II) {
178 // Print the assembly for the instruction.
179 ++EmittedInsts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 if (!printInstruction(II)) {
181 assert(0 && "Unhandled instruction in asm writer!");
182 abort();
183 }
184 }
185 }
186
187 O << "\t.end " << CurrentFnName << "\n";
188
189 // We didn't modify anything.
190 return false;
191}
192
193bool AlphaAsmPrinter::doInitialization(Module &M)
194{
195 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
196 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
197 else
198 O << "\t.arch ev6\n";
199 O << "\t.set noat\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000200 return AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201}
202
203bool AlphaAsmPrinter::doFinalization(Module &M) {
204 const TargetData *TD = TM.getTargetData();
205
206 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) {
207
208 if (!I->hasInitializer()) continue; // External global require no code
209
210 // Check to see if this is a special global used by LLVM, if so, emit it.
211 if (EmitSpecialLLVMGlobal(I))
212 continue;
213
214 std::string name = Mang->getValueName(I);
215 Constant *C = I->getInitializer();
Duncan Sands8157ef42007-11-05 00:04:43 +0000216 unsigned Size = TD->getABITypeSize(C->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 unsigned Align = TD->getPreferredAlignmentLog(I);
218
219 //1: hidden?
220 if (I->hasHiddenVisibility())
221 O << TAI->getHiddenDirective() << name << "\n";
222
223 //2: kind
224 switch (I->getLinkage()) {
225 case GlobalValue::LinkOnceLinkage:
226 case GlobalValue::WeakLinkage:
227 O << TAI->getWeakRefDirective() << name << '\n';
228 break;
229 case GlobalValue::AppendingLinkage:
230 case GlobalValue::ExternalLinkage:
231 O << "\t.globl " << name << "\n";
232 break;
233 case GlobalValue::InternalLinkage:
234 break;
235 default:
236 assert(0 && "Unknown linkage type!");
237 cerr << "Unknown linkage type!\n";
238 abort();
239 }
240
241 //3: Section (if changed)
242 if (I->hasSection() &&
243 (I->getSection() == ".ctors" ||
244 I->getSection() == ".dtors")) {
245 std::string SectionName = ".section\t" + I->getSection()
246 + ",\"aw\",@progbits";
247 SwitchToDataSection(SectionName.c_str());
248 } else {
249 if (C->isNullValue())
250 SwitchToDataSection("\t.section\t.bss", I);
251 else
252 SwitchToDataSection("\t.section\t.data", I);
253 }
254
255 //4: Type, Size, Align
256 O << "\t.type\t" << name << ", @object\n";
257 O << "\t.size\t" << name << ", " << Size << "\n";
258 EmitAlignment(Align, I);
259
260 O << name << ":\n";
261
262 // If the initializer is a extern weak symbol, remember to emit the weak
263 // reference!
264 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
265 if (GV->hasExternalWeakLinkage())
266 ExtWeakSymbols.insert(GV);
267
268 EmitGlobalConstant(C);
269 O << '\n';
270 }
271
Dan Gohman4a558a32007-07-25 19:33:14 +0000272 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273}
274
275/// PrintAsmOperand - Print out an operand for an inline asm expression.
276///
277bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
278 unsigned AsmVariant,
279 const char *ExtraCode) {
280 printOperand(MI, OpNo);
281 return false;
282}
283
284bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
285 unsigned OpNo,
286 unsigned AsmVariant,
287 const char *ExtraCode) {
288 if (ExtraCode && ExtraCode[0])
289 return true; // Unknown modifier.
290 O << "0(";
291 printOperand(MI, OpNo);
292 O << ")";
293 return false;
294}