blob: 1a911dbc1d68b4ce68d23ebb70e39ef47bd5ade8 [file] [log] [blame]
Anton Korobeynikov4403b932009-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 Korobeynikov9e4816e2009-07-16 13:43:18 +000053 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
54 const char* Modifier = 0);
Anton Korobeynikov3360da92009-07-16 13:44:00 +000055 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
56 const char* Modifier = 0);
Anton Korobeynikovd3ba2f22009-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 Korobeynikov4403b932009-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 Korobeynikov70f717f2009-07-16 14:04:22 +000071 void printModuleLevelGV(const GlobalVariable* GVar);
Anton Korobeynikov4403b932009-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 Korobeynikov70f717f2009-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();
Anton Korobeynikov78085ee2009-07-16 14:12:00 +0000103 I != E; ++I)
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000104 printModuleLevelGV(I);
105
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000106 return AsmPrinter::doFinalization(M);
107}
108
109void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
110 const Function *F = MF.getFunction();
111
112 SwitchToSection(TAI->SectionForGlobal(F));
113
114 unsigned FnAlign = 4;
115 if (F->hasFnAttr(Attribute::OptimizeForSize))
116 FnAlign = 1;
117
118 EmitAlignment(FnAlign, F);
119
120 switch (F->getLinkage()) {
121 default: assert(0 && "Unknown linkage type!");
122 case Function::InternalLinkage: // Symbols default to internal.
123 case Function::PrivateLinkage:
124 break;
125 case Function::ExternalLinkage:
126 O << "\t.globl\t" << CurrentFnName << '\n';
127 break;
128 case Function::LinkOnceAnyLinkage:
129 case Function::LinkOnceODRLinkage:
130 case Function::WeakAnyLinkage:
131 case Function::WeakODRLinkage:
132 O << "\t.weak\t" << CurrentFnName << '\n';
133 break;
134 }
135
136 printVisibility(CurrentFnName, F->getVisibility());
137
138 O << "\t.type\t" << CurrentFnName << ",@function\n"
139 << CurrentFnName << ":\n";
140}
141
142bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
143 SetupMachineFunction(MF);
144 O << "\n\n";
145
146 // Print the 'header' of function
147 emitFunctionHeader(MF);
148
149 // Print out code for the function.
150 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
151 I != E; ++I) {
152 // Print a label for the basic block.
153 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
154 // This is an entry block or a block that's only reachable via a
155 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
156 } else {
157 printBasicBlockLabel(I, true, true, VerboseAsm);
158 O << '\n';
159 }
160
161 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
162 II != E; ++II)
163 // Print the assembly for the instruction.
164 printMachineInstruction(II);
165 }
166
167 if (TAI->hasDotTypeDotSizeDirective())
168 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
169
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000170 // Print out jump tables referenced by the function.
171 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
172
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000173 O.flush();
174
175 // We didn't modify anything
176 return false;
177}
178
179void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
180 ++EmittedInsts;
181
182 // Call the autogenerated instruction printer routines.
183 if (printInstruction(MI))
184 return;
185
186 assert(0 && "Should not happen");
187}
188
189void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000190 const char* Modifier) {
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000191 const MachineOperand &MO = MI->getOperand(OpNum);
192 switch (MO.getType()) {
Anton Korobeynikoved002122009-07-16 14:04:01 +0000193 case MachineOperand::MO_Register: {
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000194 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
195 "Virtual registers should be already mapped!");
Anton Korobeynikoved002122009-07-16 14:04:01 +0000196 unsigned Reg = MO.getReg();
197 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
198 if (strncmp(Modifier + 7, "even", 4) == 0)
199 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
200 else if (strncmp(Modifier + 7, "odd", 3) == 0)
201 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
202 else
203 assert(0 && "Invalid subreg modifier");
204 }
205
206 O << '%' << TRI->getAsmName(Reg);
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000207 return;
Anton Korobeynikoved002122009-07-16 14:04:01 +0000208 }
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000209 case MachineOperand::MO_Immediate:
Anton Korobeynikov4cc7ce02009-07-16 14:01:10 +0000210 O << MO.getImm();
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000211 return;
212 case MachineOperand::MO_MachineBasicBlock:
213 printBasicBlockLabel(MO.getMBB());
214 return;
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000215 case MachineOperand::MO_JumpTableIndex:
216 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
217 << MO.getIndex();
218
219 return;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000220 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000221 const GlobalValue *GV = MO.getGlobal();
222
223 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000224 assert(MO.getOffset() == 0 && "No offsets allowed!");
225
226 O << Name;
227
228 return;
229 }
230 case MachineOperand::MO_ExternalSymbol: {
231 std::string Name(TAI->getGlobalPrefix());
232 Name += MO.getSymbolName();
233 O << Name;
234 return;
235 }
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000236 default:
237 assert(0 && "Not implemented yet!");
238 }
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000239}
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000240
241void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
242 const char* Modifier) {
243 const MachineOperand &Base = MI->getOperand(OpNum);
244
245 // Print displacement operand.
246 printOperand(MI, OpNum+1);
247
248 // Print base operand (if any)
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000249 if (Base.getReg()) {
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000250 O << '(';
251 printOperand(MI, OpNum);
252 O << ')';
253 }
254}
255
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000256void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
257 const char* Modifier) {
258 const MachineOperand &Base = MI->getOperand(OpNum);
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000259 const MachineOperand &Index = MI->getOperand(OpNum+2);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000260
261 // Print displacement operand.
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000262 printOperand(MI, OpNum+1);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000263
264 // Print base operand (if any)
265 if (Base.getReg()) {
266 O << '(';
267 printOperand(MI, OpNum);
268 if (Index.getReg()) {
269 O << ',';
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000270 printOperand(MI, OpNum+2);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000271 }
272 O << ')';
273 } else
274 assert(!Index.getReg() && "Should allocate base register first!");
275}
276
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000277/// PrintUnmangledNameSafely - Print out the printable characters in the name.
278/// Don't print things like \\n or \\0.
279static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
280 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
281 Name != E; ++Name)
282 if (isprint(*Name))
283 OS << *Name;
284}
285
286void SystemZAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
287 const TargetData *TD = TM.getTargetData();
288
289 if (!GVar->hasInitializer())
290 return; // External global require no code
291
292 // Check to see if this is a special global used by LLVM, if so, emit it.
293 if (EmitSpecialLLVMGlobal(GVar))
294 return;
295
296 std::string name = Mang->getValueName(GVar);
297 Constant *C = GVar->getInitializer();
298 const Type *Type = C->getType();
299 unsigned Size = TD->getTypeAllocSize(Type);
300 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
301
302 printVisibility(name, GVar->getVisibility());
303
304 O << "\t.type\t" << name << ",@object\n";
305
306 SwitchToSection(TAI->SectionForGlobal(GVar));
307
308 if (C->isNullValue() && !GVar->hasSection() &&
309 !GVar->isThreadLocal() &&
310 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
311
312 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
313
314 if (GVar->hasLocalLinkage())
315 O << "\t.local\t" << name << '\n';
316
317 O << TAI->getCOMMDirective() << name << ',' << Size;
318 if (TAI->getCOMMDirectiveTakesAlignment())
319 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
320
321 if (VerboseAsm) {
322 O << "\t\t" << TAI->getCommentString() << ' ';
323 PrintUnmangledNameSafely(GVar, O);
324 }
325 O << '\n';
326 return;
327 }
328
329 switch (GVar->getLinkage()) {
330 case GlobalValue::CommonLinkage:
331 case GlobalValue::LinkOnceAnyLinkage:
332 case GlobalValue::LinkOnceODRLinkage:
333 case GlobalValue::WeakAnyLinkage:
334 case GlobalValue::WeakODRLinkage:
335 O << "\t.weak\t" << name << '\n';
336 break;
337 case GlobalValue::DLLExportLinkage:
338 case GlobalValue::AppendingLinkage:
339 // FIXME: appending linkage variables should go into a section of
340 // their name or something. For now, just emit them as external.
341 case GlobalValue::ExternalLinkage:
342 // If external or appending, declare as a global symbol
343 O << "\t.globl " << name << '\n';
344 // FALL THROUGH
345 case GlobalValue::PrivateLinkage:
346 case GlobalValue::InternalLinkage:
347 break;
348 default:
349 assert(0 && "Unknown linkage type!");
350 }
351
352 // Use 16-bit alignment by default to simplify bunch of stuff
353 EmitAlignment(Align, GVar, 1);
354 O << name << ":";
355 if (VerboseAsm) {
356 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
357 PrintUnmangledNameSafely(GVar, O);
358 }
359 O << '\n';
360 if (TAI->hasDotTypeDotSizeDirective())
361 O << "\t.size\t" << name << ", " << Size << '\n';
362
363 EmitGlobalConstant(C);
364}