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/Target/TargetLoweringObjectFile.h"
31 #include "llvm/Target/TargetRegistry.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/FormattedStream.h"
35 #include "llvm/Support/Mangler.h"
39 STATISTIC(EmittedInsts, "Number of machine instrs printed");
42 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
44 SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
45 const TargetAsmInfo *TAI, bool V)
46 : AsmPrinter(O, TM, TAI, V) {}
48 virtual const char *getPassName() const {
49 return "SystemZ Assembly Printer";
52 void printOperand(const MachineInstr *MI, int OpNum,
53 const char* Modifier = 0);
54 void printPCRelImmOperand(const MachineInstr *MI, int OpNum);
55 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
56 const char* Modifier = 0);
57 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
58 const char* Modifier = 0);
59 void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
60 O << (int16_t)MI->getOperand(OpNum).getImm();
62 void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
63 O << (int32_t)MI->getOperand(OpNum).getImm();
66 bool printInstruction(const MachineInstr *MI); // autogenerated.
67 void printMachineInstruction(const MachineInstr * MI);
69 void emitFunctionHeader(const MachineFunction &MF);
70 bool runOnMachineFunction(MachineFunction &F);
71 void PrintGlobalVariable(const GlobalVariable* GVar);
73 void getAnalysisUsage(AnalysisUsage &AU) const {
74 AsmPrinter::getAnalysisUsage(AU);
78 } // end of anonymous namespace
80 #include "SystemZGenAsmWriter.inc"
82 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
83 unsigned FnAlign = MF.getAlignment();
84 const Function *F = MF.getFunction();
86 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
88 EmitAlignment(FnAlign, F);
90 switch (F->getLinkage()) {
91 default: assert(0 && "Unknown linkage type!");
92 case Function::InternalLinkage: // Symbols default to internal.
93 case Function::PrivateLinkage:
94 case Function::LinkerPrivateLinkage:
96 case Function::ExternalLinkage:
97 O << "\t.globl\t" << CurrentFnName << '\n';
99 case Function::LinkOnceAnyLinkage:
100 case Function::LinkOnceODRLinkage:
101 case Function::WeakAnyLinkage:
102 case Function::WeakODRLinkage:
103 O << "\t.weak\t" << CurrentFnName << '\n';
107 printVisibility(CurrentFnName, F->getVisibility());
109 O << "\t.type\t" << CurrentFnName << ",@function\n"
110 << CurrentFnName << ":\n";
113 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
114 SetupMachineFunction(MF);
117 // Print out constants referenced by the function
118 EmitConstantPool(MF.getConstantPool());
120 // Print the 'header' of function
121 emitFunctionHeader(MF);
123 // Print out code for the function.
124 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
126 // Print a label for the basic block.
127 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
128 // This is an entry block or a block that's only reachable via a
129 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
131 printBasicBlockLabel(I, true, true, VerboseAsm);
135 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
137 // Print the assembly for the instruction.
138 printMachineInstruction(II);
141 if (TAI->hasDotTypeDotSizeDirective())
142 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
144 // Print out jump tables referenced by the function.
145 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
147 // We didn't modify anything
151 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
154 // Call the autogenerated instruction printer routines.
155 printInstruction(MI);
158 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
159 const MachineOperand &MO = MI->getOperand(OpNum);
160 switch (MO.getType()) {
161 case MachineOperand::MO_Immediate:
164 case MachineOperand::MO_MachineBasicBlock:
165 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
167 case MachineOperand::MO_GlobalAddress: {
168 const GlobalValue *GV = MO.getGlobal();
169 std::string Name = Mang->getMangledName(GV);
173 // Assemble calls via PLT for externally visible symbols if PIC.
174 if (TM.getRelocationModel() == Reloc::PIC_ &&
175 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
176 !GV->hasLocalLinkage())
179 printOffset(MO.getOffset());
182 case MachineOperand::MO_ExternalSymbol: {
183 std::string Name(TAI->getGlobalPrefix());
184 Name += MO.getSymbolName();
187 if (TM.getRelocationModel() == Reloc::PIC_)
193 assert(0 && "Not implemented yet!");
198 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
199 const char* Modifier) {
200 const MachineOperand &MO = MI->getOperand(OpNum);
201 switch (MO.getType()) {
202 case MachineOperand::MO_Register: {
203 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
204 "Virtual registers should be already mapped!");
205 unsigned Reg = MO.getReg();
206 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
207 if (strncmp(Modifier + 7, "even", 4) == 0)
208 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
209 else if (strncmp(Modifier + 7, "odd", 3) == 0)
210 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
212 assert(0 && "Invalid subreg modifier");
215 O << '%' << TRI->getAsmName(Reg);
218 case MachineOperand::MO_Immediate:
221 case MachineOperand::MO_MachineBasicBlock:
222 printBasicBlockLabel(MO.getMBB());
224 case MachineOperand::MO_JumpTableIndex:
225 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
229 case MachineOperand::MO_ConstantPoolIndex:
230 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
233 printOffset(MO.getOffset());
235 case MachineOperand::MO_GlobalAddress: {
236 const GlobalValue *GV = MO.getGlobal();
237 std::string Name = Mang->getMangledName(GV);
242 case MachineOperand::MO_ExternalSymbol: {
243 std::string Name(TAI->getGlobalPrefix());
244 Name += MO.getSymbolName();
249 assert(0 && "Not implemented yet!");
252 switch (MO.getTargetFlags()) {
254 llvm_unreachable("Unknown target flag on GV operand");
255 case SystemZII::MO_NO_FLAG:
257 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
258 case SystemZII::MO_PLT: O << "@PLT"; break;
261 printOffset(MO.getOffset());
264 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
265 const char* Modifier) {
266 const MachineOperand &Base = MI->getOperand(OpNum);
268 // Print displacement operand.
269 printOperand(MI, OpNum+1);
271 // Print base operand (if any)
274 printOperand(MI, OpNum);
279 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
280 const char* Modifier) {
281 const MachineOperand &Base = MI->getOperand(OpNum);
282 const MachineOperand &Index = MI->getOperand(OpNum+2);
284 // Print displacement operand.
285 printOperand(MI, OpNum+1);
287 // Print base operand (if any)
290 printOperand(MI, OpNum);
291 if (Index.getReg()) {
293 printOperand(MI, OpNum+2);
297 assert(!Index.getReg() && "Should allocate base register first!");
300 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
301 /// Don't print things like \\n or \\0.
302 static void PrintUnmangledNameSafely(const Value *V,
303 formatted_raw_ostream &OS) {
304 for (StringRef::iterator it = V->getName().begin(),
305 ie = V->getName().end(); it != ie; ++it)
310 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
311 const TargetData *TD = TM.getTargetData();
313 if (!GVar->hasInitializer())
314 return; // External global require no code
316 // Check to see if this is a special global used by LLVM, if so, emit it.
317 if (EmitSpecialLLVMGlobal(GVar))
320 std::string name = Mang->getMangledName(GVar);
321 Constant *C = GVar->getInitializer();
322 unsigned Size = TD->getTypeAllocSize(C->getType());
323 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
325 printVisibility(name, GVar->getVisibility());
327 O << "\t.type\t" << name << ",@object\n";
329 SwitchToSection(getObjFileLowering().SectionForGlobal(GVar, Mang, TM));
331 if (C->isNullValue() && !GVar->hasSection() &&
332 !GVar->isThreadLocal() &&
333 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
335 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
337 if (GVar->hasLocalLinkage())
338 O << "\t.local\t" << name << '\n';
340 O << TAI->getCOMMDirective() << name << ',' << Size;
341 if (TAI->getCOMMDirectiveTakesAlignment())
342 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
345 O << "\t\t" << TAI->getCommentString() << ' ';
346 PrintUnmangledNameSafely(GVar, O);
352 switch (GVar->getLinkage()) {
353 case GlobalValue::CommonLinkage:
354 case GlobalValue::LinkOnceAnyLinkage:
355 case GlobalValue::LinkOnceODRLinkage:
356 case GlobalValue::WeakAnyLinkage:
357 case GlobalValue::WeakODRLinkage:
358 O << "\t.weak\t" << name << '\n';
360 case GlobalValue::DLLExportLinkage:
361 case GlobalValue::AppendingLinkage:
362 // FIXME: appending linkage variables should go into a section of
363 // their name or something. For now, just emit them as external.
364 case GlobalValue::ExternalLinkage:
365 // If external or appending, declare as a global symbol
366 O << "\t.globl " << name << '\n';
368 case GlobalValue::PrivateLinkage:
369 case GlobalValue::LinkerPrivateLinkage:
370 case GlobalValue::InternalLinkage:
373 assert(0 && "Unknown linkage type!");
376 // Use 16-bit alignment by default to simplify bunch of stuff
377 EmitAlignment(Align, GVar, 1);
380 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
381 PrintUnmangledNameSafely(GVar, O);
384 if (TAI->hasDotTypeDotSizeDirective())
385 O << "\t.size\t" << name << ", " << Size << '\n';
387 EmitGlobalConstant(C);
390 // Force static initialization.
391 extern "C" void LLVMInitializeSystemZAsmPrinter() {
392 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);