1 //===-- SystemZAsmPrinter.cpp - SystemZ LLVM assembly writer ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to the SystemZ assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
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"
37 STATISTIC(EmittedInsts, "Number of machine instrs printed");
40 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
42 SystemZAsmPrinter(raw_ostream &O, SystemZTargetMachine &TM,
43 const TargetAsmInfo *TAI,
44 CodeGenOpt::Level OL, bool V)
45 : AsmPrinter(O, TM, TAI, OL, V) {}
47 virtual const char *getPassName() const {
48 return "SystemZ Assembly Printer";
51 void printOperand(const MachineInstr *MI, int OpNum,
52 const char* Modifier = 0);
53 void printPCRelImmOperand(const MachineInstr *MI, int OpNum);
54 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
55 const char* Modifier = 0);
56 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
57 const char* Modifier = 0);
58 void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
59 O << (int16_t)MI->getOperand(OpNum).getImm();
61 void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
62 O << (int32_t)MI->getOperand(OpNum).getImm();
65 bool printInstruction(const MachineInstr *MI); // autogenerated.
66 void printMachineInstruction(const MachineInstr * MI);
68 void emitFunctionHeader(const MachineFunction &MF);
69 bool runOnMachineFunction(MachineFunction &F);
70 bool doInitialization(Module &M);
71 bool doFinalization(Module &M);
72 void printModuleLevelGV(const GlobalVariable* GVar);
74 void getAnalysisUsage(AnalysisUsage &AU) const {
75 AsmPrinter::getAnalysisUsage(AU);
79 } // end of anonymous namespace
81 #include "SystemZGenAsmWriter.inc"
83 /// createSystemZCodePrinterPass - Returns a pass that prints the SystemZ
84 /// assembly code for a MachineFunction to the given output stream,
85 /// using the given target machine description. This should work
86 /// regardless of whether the function is in SSA form.
88 FunctionPass *llvm::createSystemZCodePrinterPass(raw_ostream &o,
89 SystemZTargetMachine &tm,
90 CodeGenOpt::Level OptLevel,
92 return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
95 bool SystemZAsmPrinter::doInitialization(Module &M) {
96 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
97 return false; // success
101 bool SystemZAsmPrinter::doFinalization(Module &M) {
102 // Print out module-level global variables here.
103 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
105 printModuleLevelGV(I);
107 return AsmPrinter::doFinalization(M);
110 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
111 const Function *F = MF.getFunction();
113 SwitchToSection(TAI->SectionForGlobal(F));
115 unsigned FnAlign = 4;
116 if (F->hasFnAttr(Attribute::OptimizeForSize))
119 EmitAlignment(FnAlign, F);
121 switch (F->getLinkage()) {
122 default: assert(0 && "Unknown linkage type!");
123 case Function::InternalLinkage: // Symbols default to internal.
124 case Function::PrivateLinkage:
126 case Function::ExternalLinkage:
127 O << "\t.globl\t" << CurrentFnName << '\n';
129 case Function::LinkOnceAnyLinkage:
130 case Function::LinkOnceODRLinkage:
131 case Function::WeakAnyLinkage:
132 case Function::WeakODRLinkage:
133 O << "\t.weak\t" << CurrentFnName << '\n';
137 printVisibility(CurrentFnName, F->getVisibility());
139 O << "\t.type\t" << CurrentFnName << ",@function\n"
140 << CurrentFnName << ":\n";
143 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
144 SetupMachineFunction(MF);
147 // Print the 'header' of function
148 emitFunctionHeader(MF);
150 // Print out code for the function.
151 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
153 // Print a label for the basic block.
154 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
155 // This is an entry block or a block that's only reachable via a
156 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
158 printBasicBlockLabel(I, true, true, VerboseAsm);
162 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
164 // Print the assembly for the instruction.
165 printMachineInstruction(II);
168 if (TAI->hasDotTypeDotSizeDirective())
169 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
171 // Print out jump tables referenced by the function.
172 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
176 // We didn't modify anything
180 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
183 // Call the autogenerated instruction printer routines.
184 if (printInstruction(MI))
187 assert(0 && "Should not happen");
190 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
191 const MachineOperand &MO = MI->getOperand(OpNum);
192 switch (MO.getType()) {
193 case MachineOperand::MO_GlobalAddress: {
194 const GlobalValue *GV = MO.getGlobal();
195 std::string Name = Mang->getValueName(GV);
199 // Assemble calls via PLT for externally visible symbols if PIC.
200 if (TM.getRelocationModel() == Reloc::PIC_ &&
201 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
202 !GV->hasLocalLinkage())
205 printOffset(MO.getOffset());
208 case MachineOperand::MO_ExternalSymbol: {
209 std::string Name(TAI->getGlobalPrefix());
210 Name += MO.getSymbolName();
213 if (TM.getRelocationModel() == Reloc::PIC_)
219 assert(0 && "Not implemented yet!");
224 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
225 const char* Modifier) {
226 const MachineOperand &MO = MI->getOperand(OpNum);
227 switch (MO.getType()) {
228 case MachineOperand::MO_Register: {
229 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
230 "Virtual registers should be already mapped!");
231 unsigned Reg = MO.getReg();
232 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
233 if (strncmp(Modifier + 7, "even", 4) == 0)
234 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
235 else if (strncmp(Modifier + 7, "odd", 3) == 0)
236 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
238 assert(0 && "Invalid subreg modifier");
241 O << '%' << TRI->getAsmName(Reg);
244 case MachineOperand::MO_Immediate:
247 case MachineOperand::MO_MachineBasicBlock:
248 printBasicBlockLabel(MO.getMBB());
250 case MachineOperand::MO_JumpTableIndex:
251 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
255 case MachineOperand::MO_GlobalAddress: {
256 const GlobalValue *GV = MO.getGlobal();
257 std::string Name = Mang->getValueName(GV);
262 case MachineOperand::MO_ExternalSymbol: {
263 std::string Name(TAI->getGlobalPrefix());
264 Name += MO.getSymbolName();
269 assert(0 && "Not implemented yet!");
272 switch (MO.getTargetFlags()) {
274 assert(0 && "Unknown target flag on GV operand");
275 case SystemZII::MO_NO_FLAG:
277 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
278 case SystemZII::MO_PLT: O << "@PLT"; break;
281 printOffset(MO.getOffset());
284 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
285 const char* Modifier) {
286 const MachineOperand &Base = MI->getOperand(OpNum);
288 // Print displacement operand.
289 printOperand(MI, OpNum+1);
291 // Print base operand (if any)
294 printOperand(MI, OpNum);
299 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
300 const char* Modifier) {
301 const MachineOperand &Base = MI->getOperand(OpNum);
302 const MachineOperand &Index = MI->getOperand(OpNum+2);
304 // Print displacement operand.
305 printOperand(MI, OpNum+1);
307 // Print base operand (if any)
310 printOperand(MI, OpNum);
311 if (Index.getReg()) {
313 printOperand(MI, OpNum+2);
317 assert(!Index.getReg() && "Should allocate base register first!");
320 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
321 /// Don't print things like \\n or \\0.
322 static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
323 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
329 void SystemZAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
330 const TargetData *TD = TM.getTargetData();
332 if (!GVar->hasInitializer())
333 return; // External global require no code
335 // Check to see if this is a special global used by LLVM, if so, emit it.
336 if (EmitSpecialLLVMGlobal(GVar))
339 std::string name = Mang->getValueName(GVar);
340 Constant *C = GVar->getInitializer();
341 const Type *Type = C->getType();
342 unsigned Size = TD->getTypeAllocSize(Type);
343 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
345 printVisibility(name, GVar->getVisibility());
347 O << "\t.type\t" << name << ",@object\n";
349 SwitchToSection(TAI->SectionForGlobal(GVar));
351 if (C->isNullValue() && !GVar->hasSection() &&
352 !GVar->isThreadLocal() &&
353 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
355 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
357 if (GVar->hasLocalLinkage())
358 O << "\t.local\t" << name << '\n';
360 O << TAI->getCOMMDirective() << name << ',' << Size;
361 if (TAI->getCOMMDirectiveTakesAlignment())
362 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
365 O << "\t\t" << TAI->getCommentString() << ' ';
366 PrintUnmangledNameSafely(GVar, O);
372 switch (GVar->getLinkage()) {
373 case GlobalValue::CommonLinkage:
374 case GlobalValue::LinkOnceAnyLinkage:
375 case GlobalValue::LinkOnceODRLinkage:
376 case GlobalValue::WeakAnyLinkage:
377 case GlobalValue::WeakODRLinkage:
378 O << "\t.weak\t" << name << '\n';
380 case GlobalValue::DLLExportLinkage:
381 case GlobalValue::AppendingLinkage:
382 // FIXME: appending linkage variables should go into a section of
383 // their name or something. For now, just emit them as external.
384 case GlobalValue::ExternalLinkage:
385 // If external or appending, declare as a global symbol
386 O << "\t.globl " << name << '\n';
388 case GlobalValue::PrivateLinkage:
389 case GlobalValue::InternalLinkage:
392 assert(0 && "Unknown linkage type!");
395 // Use 16-bit alignment by default to simplify bunch of stuff
396 EmitAlignment(Align, GVar, 1);
399 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
400 PrintUnmangledNameSafely(GVar, O);
403 if (TAI->hasDotTypeDotSizeDirective())
404 O << "\t.size\t" << name << ", " << Size << '\n';
406 EmitGlobalConstant(C);