blob: d08727724c0e7bdeeb6a4ac1b481b6ca0140c77f [file] [log] [blame]
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +00001//===-- SystemZAsmPrinter.cpp - SystemZ LLVM assembly writer ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to the SystemZ assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "asm-printer"
16#include "SystemZ.h"
17#include "SystemZInstrInfo.h"
18#include "SystemZTargetMachine.h"
19#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Module.h"
22#include "llvm/CodeGen/AsmPrinter.h"
23#include "llvm/CodeGen/DwarfWriter.h"
24#include "llvm/CodeGen/MachineModuleInfo.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineConstantPool.h"
27#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/Target/TargetAsmInfo.h"
29#include "llvm/Target/TargetData.h"
30#include "llvm/ADT/Statistic.h"
31#include "llvm/Support/Compiler.h"
32#include "llvm/Support/Mangler.h"
33#include "llvm/Support/raw_ostream.h"
34
35using namespace llvm;
36
37STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
39namespace {
40 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
41 public:
42 SystemZAsmPrinter(raw_ostream &O, SystemZTargetMachine &TM,
43 const TargetAsmInfo *TAI,
44 CodeGenOpt::Level OL, bool V)
45 : AsmPrinter(O, TM, TAI, OL, V) {}
46
47 virtual const char *getPassName() const {
48 return "SystemZ Assembly Printer";
49 }
50
51 void printOperand(const MachineInstr *MI, int OpNum,
52 const char* Modifier = 0);
Anton Korobeynikova58fac92009-07-16 13:43:18 +000053 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
54 const char* Modifier = 0);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +000055 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
56 const char* Modifier = 0);
Anton Korobeynikovbf21bac2009-07-16 14:02:45 +000057 void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
58 O << (int16_t)MI->getOperand(OpNum).getImm();
59 }
60 void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
61 O << (int32_t)MI->getOperand(OpNum).getImm();
62 }
63
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000064 bool printInstruction(const MachineInstr *MI); // autogenerated.
65 void printMachineInstruction(const MachineInstr * MI);
66
67 void emitFunctionHeader(const MachineFunction &MF);
68 bool runOnMachineFunction(MachineFunction &F);
69 bool doInitialization(Module &M);
70 bool doFinalization(Module &M);
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +000071 void printModuleLevelGV(const GlobalVariable* GVar);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000072
73 void getAnalysisUsage(AnalysisUsage &AU) const {
74 AsmPrinter::getAnalysisUsage(AU);
75 AU.setPreservesAll();
76 }
77 };
78} // end of anonymous namespace
79
80#include "SystemZGenAsmWriter.inc"
81
82/// createSystemZCodePrinterPass - Returns a pass that prints the SystemZ
83/// assembly code for a MachineFunction to the given output stream,
84/// using the given target machine description. This should work
85/// regardless of whether the function is in SSA form.
86///
87FunctionPass *llvm::createSystemZCodePrinterPass(raw_ostream &o,
88 SystemZTargetMachine &tm,
89 CodeGenOpt::Level OptLevel,
90 bool verbose) {
91 return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
92}
93
94bool SystemZAsmPrinter::doInitialization(Module &M) {
95 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
96 return false; // success
97}
98
99
100bool SystemZAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000101 // Print out module-level global variables here.
102 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
103 I != E; ++I) {
104 printModuleLevelGV(I);
105
106 // If the global is a extern weak symbol, remember to emit the weak
107 // reference!
108 // FIXME: This is rather hacky, since we'll emit references to ALL weak
109 // stuff, not used. But currently it's the only way to deal with extern weak
110 // initializers hidden deep inside constant expressions.
111 if (I->hasExternalWeakLinkage())
112 ExtWeakSymbols.insert(I);
113 }
114
115 for (Module::const_iterator I = M.begin(), E = M.end();
116 I != E; ++I) {
117 // If the global is a extern weak symbol, remember to emit the weak
118 // reference!
119 // FIXME: This is rather hacky, since we'll emit references to ALL weak
120 // stuff, not used. But currently it's the only way to deal with extern weak
121 // initializers hidden deep inside constant expressions.
122 if (I->hasExternalWeakLinkage())
123 ExtWeakSymbols.insert(I);
124 }
125
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000126 return AsmPrinter::doFinalization(M);
127}
128
129void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
130 const Function *F = MF.getFunction();
131
132 SwitchToSection(TAI->SectionForGlobal(F));
133
134 unsigned FnAlign = 4;
135 if (F->hasFnAttr(Attribute::OptimizeForSize))
136 FnAlign = 1;
137
138 EmitAlignment(FnAlign, F);
139
140 switch (F->getLinkage()) {
141 default: assert(0 && "Unknown linkage type!");
142 case Function::InternalLinkage: // Symbols default to internal.
143 case Function::PrivateLinkage:
144 break;
145 case Function::ExternalLinkage:
146 O << "\t.globl\t" << CurrentFnName << '\n';
147 break;
148 case Function::LinkOnceAnyLinkage:
149 case Function::LinkOnceODRLinkage:
150 case Function::WeakAnyLinkage:
151 case Function::WeakODRLinkage:
152 O << "\t.weak\t" << CurrentFnName << '\n';
153 break;
154 }
155
156 printVisibility(CurrentFnName, F->getVisibility());
157
158 O << "\t.type\t" << CurrentFnName << ",@function\n"
159 << CurrentFnName << ":\n";
160}
161
162bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
163 SetupMachineFunction(MF);
164 O << "\n\n";
165
166 // Print the 'header' of function
167 emitFunctionHeader(MF);
168
169 // Print out code for the function.
170 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
171 I != E; ++I) {
172 // Print a label for the basic block.
173 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
174 // This is an entry block or a block that's only reachable via a
175 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
176 } else {
177 printBasicBlockLabel(I, true, true, VerboseAsm);
178 O << '\n';
179 }
180
181 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
182 II != E; ++II)
183 // Print the assembly for the instruction.
184 printMachineInstruction(II);
185 }
186
187 if (TAI->hasDotTypeDotSizeDirective())
188 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
189
190 O.flush();
191
192 // We didn't modify anything
193 return false;
194}
195
196void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
197 ++EmittedInsts;
198
199 // Call the autogenerated instruction printer routines.
200 if (printInstruction(MI))
201 return;
202
203 assert(0 && "Should not happen");
204}
205
206void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000207 const char* Modifier) {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000208 const MachineOperand &MO = MI->getOperand(OpNum);
209 switch (MO.getType()) {
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000210 case MachineOperand::MO_Register: {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000211 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
212 "Virtual registers should be already mapped!");
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000213 unsigned Reg = MO.getReg();
214 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
215 if (strncmp(Modifier + 7, "even", 4) == 0)
216 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
217 else if (strncmp(Modifier + 7, "odd", 3) == 0)
218 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
219 else
220 assert(0 && "Invalid subreg modifier");
221 }
222
223 O << '%' << TRI->getAsmName(Reg);
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000224 return;
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000225 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000226 case MachineOperand::MO_Immediate:
Anton Korobeynikov8eef4042009-07-16 14:01:10 +0000227 O << MO.getImm();
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000228 return;
229 case MachineOperand::MO_MachineBasicBlock:
230 printBasicBlockLabel(MO.getMBB());
231 return;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000232 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000233 const GlobalValue *GV = MO.getGlobal();
234
235 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000236 assert(MO.getOffset() == 0 && "No offsets allowed!");
237
238 O << Name;
239
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000240 if (GV->hasExternalWeakLinkage())
241 ExtWeakSymbols.insert(GV);
242
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000243 return;
244 }
245 case MachineOperand::MO_ExternalSymbol: {
246 std::string Name(TAI->getGlobalPrefix());
247 Name += MO.getSymbolName();
248 O << Name;
249 return;
250 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000251 default:
252 assert(0 && "Not implemented yet!");
253 }
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000254}
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000255
256void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
257 const char* Modifier) {
258 const MachineOperand &Base = MI->getOperand(OpNum);
259
260 // Print displacement operand.
261 printOperand(MI, OpNum+1);
262
263 // Print base operand (if any)
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000264 if (Base.getReg()) {
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000265 O << '(';
266 printOperand(MI, OpNum);
267 O << ')';
268 }
269}
270
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000271void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
272 const char* Modifier) {
273 const MachineOperand &Base = MI->getOperand(OpNum);
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000274 const MachineOperand &Index = MI->getOperand(OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000275
276 // Print displacement operand.
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000277 printOperand(MI, OpNum+1);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000278
279 // Print base operand (if any)
280 if (Base.getReg()) {
281 O << '(';
282 printOperand(MI, OpNum);
283 if (Index.getReg()) {
284 O << ',';
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000285 printOperand(MI, OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000286 }
287 O << ')';
288 } else
289 assert(!Index.getReg() && "Should allocate base register first!");
290}
291
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000292/// PrintUnmangledNameSafely - Print out the printable characters in the name.
293/// Don't print things like \\n or \\0.
294static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
295 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
296 Name != E; ++Name)
297 if (isprint(*Name))
298 OS << *Name;
299}
300
301void SystemZAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
302 const TargetData *TD = TM.getTargetData();
303
304 if (!GVar->hasInitializer())
305 return; // External global require no code
306
307 // Check to see if this is a special global used by LLVM, if so, emit it.
308 if (EmitSpecialLLVMGlobal(GVar))
309 return;
310
311 std::string name = Mang->getValueName(GVar);
312 Constant *C = GVar->getInitializer();
313 const Type *Type = C->getType();
314 unsigned Size = TD->getTypeAllocSize(Type);
315 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
316
317 printVisibility(name, GVar->getVisibility());
318
319 O << "\t.type\t" << name << ",@object\n";
320
321 SwitchToSection(TAI->SectionForGlobal(GVar));
322
323 if (C->isNullValue() && !GVar->hasSection() &&
324 !GVar->isThreadLocal() &&
325 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
326
327 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
328
329 if (GVar->hasLocalLinkage())
330 O << "\t.local\t" << name << '\n';
331
332 O << TAI->getCOMMDirective() << name << ',' << Size;
333 if (TAI->getCOMMDirectiveTakesAlignment())
334 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
335
336 if (VerboseAsm) {
337 O << "\t\t" << TAI->getCommentString() << ' ';
338 PrintUnmangledNameSafely(GVar, O);
339 }
340 O << '\n';
341 return;
342 }
343
344 switch (GVar->getLinkage()) {
345 case GlobalValue::CommonLinkage:
346 case GlobalValue::LinkOnceAnyLinkage:
347 case GlobalValue::LinkOnceODRLinkage:
348 case GlobalValue::WeakAnyLinkage:
349 case GlobalValue::WeakODRLinkage:
350 O << "\t.weak\t" << name << '\n';
351 break;
352 case GlobalValue::DLLExportLinkage:
353 case GlobalValue::AppendingLinkage:
354 // FIXME: appending linkage variables should go into a section of
355 // their name or something. For now, just emit them as external.
356 case GlobalValue::ExternalLinkage:
357 // If external or appending, declare as a global symbol
358 O << "\t.globl " << name << '\n';
359 // FALL THROUGH
360 case GlobalValue::PrivateLinkage:
361 case GlobalValue::InternalLinkage:
362 break;
363 default:
364 assert(0 && "Unknown linkage type!");
365 }
366
367 // Use 16-bit alignment by default to simplify bunch of stuff
368 EmitAlignment(Align, GVar, 1);
369 O << name << ":";
370 if (VerboseAsm) {
371 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
372 PrintUnmangledNameSafely(GVar, O);
373 }
374 O << '\n';
375 if (TAI->hasDotTypeDotSizeDirective())
376 O << "\t.size\t" << name << ", " << Size << '\n';
377
378 EmitGlobalConstant(C);
379}