blob: a46a64cebd9490e3991072a988c1f011a94cd9e5 [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) {
Dan Gohman1e57df32008-02-10 18:45:23 +000078 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
79 "Not physreg??");
Bill Wendling8eeb9792008-02-26 21:11:01 +000080 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 } else if (MO.isImmediate()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000082 O << MO.getImm();
83 assert(MO.getImm() < (1 << 30));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 } else {
85 printOp(MO);
86 }
87}
88
89
90void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman1e57df32008-02-10 18:45:23 +000091 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
93 switch (MO.getType()) {
94 case MachineOperand::MO_Register:
Bill Wendling8eeb9792008-02-26 21:11:01 +000095 O << RI.get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 return;
97
98 case MachineOperand::MO_Immediate:
99 cerr << "printOp() does not handle immediate values\n";
100 abort();
101 return;
102
103 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000104 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 return;
106
107 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000108 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000109 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 return;
111
112 case MachineOperand::MO_ExternalSymbol:
113 O << MO.getSymbolName();
114 return;
115
116 case MachineOperand::MO_GlobalAddress: {
117 GlobalValue *GV = MO.getGlobal();
118 O << Mang->getValueName(GV);
119 if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
120 ExtWeakSymbols.insert(GV);
121 return;
122 }
123
124 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000125 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000126 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 return;
128
129 default:
130 O << "<unknown operand type: " << MO.getType() << ">";
131 return;
132 }
133}
134
135/// runOnMachineFunction - This uses the printMachineInstruction()
136/// method to print assembly for each instruction.
137///
138bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
139 SetupMachineFunction(MF);
140 O << "\n\n";
141
142 // Print out constants referenced by the function
143 EmitConstantPool(MF.getConstantPool());
144
145 // Print out jump tables referenced by the function
146 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
147
148 // Print out labels for the function.
149 const Function *F = MF.getFunction();
150 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
151
152 EmitAlignment(4, F);
153 switch (F->getLinkage()) {
154 default: assert(0 && "Unknown linkage type!");
155 case Function::InternalLinkage: // Symbols default to internal.
156 break;
157 case Function::ExternalLinkage:
158 O << "\t.globl " << CurrentFnName << "\n";
159 break;
160 case Function::WeakLinkage:
161 case Function::LinkOnceLinkage:
162 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
163 break;
164 }
165
166 O << "\t.ent " << CurrentFnName << "\n";
167
168 O << CurrentFnName << ":\n";
169
170 // Print out code for the function.
171 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
172 I != E; ++I) {
173 if (I != MF.begin()) {
174 printBasicBlockLabel(I, true);
175 O << '\n';
176 }
177 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
178 II != E; ++II) {
179 // Print the assembly for the instruction.
180 ++EmittedInsts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 if (!printInstruction(II)) {
182 assert(0 && "Unhandled instruction in asm writer!");
183 abort();
184 }
185 }
186 }
187
188 O << "\t.end " << CurrentFnName << "\n";
189
190 // We didn't modify anything.
191 return false;
192}
193
194bool AlphaAsmPrinter::doInitialization(Module &M)
195{
196 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
197 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
198 else
199 O << "\t.arch ev6\n";
200 O << "\t.set noat\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000201 return AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202}
203
204bool AlphaAsmPrinter::doFinalization(Module &M) {
205 const TargetData *TD = TM.getTargetData();
206
207 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) {
208
209 if (!I->hasInitializer()) continue; // External global require no code
210
211 // Check to see if this is a special global used by LLVM, if so, emit it.
212 if (EmitSpecialLLVMGlobal(I))
213 continue;
214
215 std::string name = Mang->getValueName(I);
216 Constant *C = I->getInitializer();
Duncan Sands8157ef42007-11-05 00:04:43 +0000217 unsigned Size = TD->getABITypeSize(C->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 unsigned Align = TD->getPreferredAlignmentLog(I);
219
220 //1: hidden?
221 if (I->hasHiddenVisibility())
222 O << TAI->getHiddenDirective() << name << "\n";
223
224 //2: kind
225 switch (I->getLinkage()) {
226 case GlobalValue::LinkOnceLinkage:
227 case GlobalValue::WeakLinkage:
228 O << TAI->getWeakRefDirective() << name << '\n';
229 break;
230 case GlobalValue::AppendingLinkage:
231 case GlobalValue::ExternalLinkage:
232 O << "\t.globl " << name << "\n";
233 break;
234 case GlobalValue::InternalLinkage:
235 break;
236 default:
237 assert(0 && "Unknown linkage type!");
238 cerr << "Unknown linkage type!\n";
239 abort();
240 }
241
242 //3: Section (if changed)
243 if (I->hasSection() &&
244 (I->getSection() == ".ctors" ||
245 I->getSection() == ".dtors")) {
246 std::string SectionName = ".section\t" + I->getSection()
247 + ",\"aw\",@progbits";
248 SwitchToDataSection(SectionName.c_str());
249 } else {
250 if (C->isNullValue())
251 SwitchToDataSection("\t.section\t.bss", I);
252 else
253 SwitchToDataSection("\t.section\t.data", I);
254 }
255
256 //4: Type, Size, Align
257 O << "\t.type\t" << name << ", @object\n";
258 O << "\t.size\t" << name << ", " << Size << "\n";
259 EmitAlignment(Align, I);
260
261 O << name << ":\n";
262
263 // If the initializer is a extern weak symbol, remember to emit the weak
264 // reference!
265 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
266 if (GV->hasExternalWeakLinkage())
267 ExtWeakSymbols.insert(GV);
268
269 EmitGlobalConstant(C);
270 O << '\n';
271 }
272
Dan Gohman4a558a32007-07-25 19:33:14 +0000273 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274}
275
276/// PrintAsmOperand - Print out an operand for an inline asm expression.
277///
278bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
279 unsigned AsmVariant,
280 const char *ExtraCode) {
281 printOperand(MI, OpNo);
282 return false;
283}
284
285bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
286 unsigned OpNo,
287 unsigned AsmVariant,
288 const char *ExtraCode) {
289 if (ExtraCode && ExtraCode[0])
290 return true; // Unknown modifier.
291 O << "0(";
292 printOperand(MI, OpNo);
293 O << ")";
294 return false;
295}