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/TargetRegistry.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/Support/Compiler.h"
33 #include "llvm/Support/FormattedStream.h"
34 #include "llvm/Support/Mangler.h"
38 STATISTIC(EmittedInsts, "Number of machine instrs printed");
41 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
43 SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
44 const TargetAsmInfo *TAI, bool V)
45 : AsmPrinter(O, TM, TAI, 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 void PrintGlobalVariable(const GlobalVariable* GVar);
72 void getAnalysisUsage(AnalysisUsage &AU) const {
73 AsmPrinter::getAnalysisUsage(AU);
77 } // end of anonymous namespace
79 #include "SystemZGenAsmWriter.inc"
81 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
82 unsigned FnAlign = MF.getAlignment();
83 const Function *F = MF.getFunction();
85 SwitchToSection(TAI->SectionForGlobal(F));
87 EmitAlignment(FnAlign, F);
89 switch (F->getLinkage()) {
90 default: assert(0 && "Unknown linkage type!");
91 case Function::InternalLinkage: // Symbols default to internal.
92 case Function::PrivateLinkage:
93 case Function::LinkerPrivateLinkage:
95 case Function::ExternalLinkage:
96 O << "\t.globl\t" << CurrentFnName << '\n';
98 case Function::LinkOnceAnyLinkage:
99 case Function::LinkOnceODRLinkage:
100 case Function::WeakAnyLinkage:
101 case Function::WeakODRLinkage:
102 O << "\t.weak\t" << CurrentFnName << '\n';
106 printVisibility(CurrentFnName, F->getVisibility());
108 O << "\t.type\t" << CurrentFnName << ",@function\n"
109 << CurrentFnName << ":\n";
112 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
113 SetupMachineFunction(MF);
116 // Print out constants referenced by the function
117 EmitConstantPool(MF.getConstantPool());
119 // Print the 'header' of function
120 emitFunctionHeader(MF);
122 // Print out code for the function.
123 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
125 // Print a label for the basic block.
126 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
127 // This is an entry block or a block that's only reachable via a
128 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
130 printBasicBlockLabel(I, true, true, VerboseAsm);
134 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
136 // Print the assembly for the instruction.
137 printMachineInstruction(II);
140 if (TAI->hasDotTypeDotSizeDirective())
141 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
143 // Print out jump tables referenced by the function.
144 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
148 // We didn't modify anything
152 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
155 // Call the autogenerated instruction printer routines.
156 if (printInstruction(MI))
159 llvm_unreachable("Unreachable!");
162 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
163 const MachineOperand &MO = MI->getOperand(OpNum);
164 switch (MO.getType()) {
165 case MachineOperand::MO_Immediate:
168 case MachineOperand::MO_MachineBasicBlock:
169 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
171 case MachineOperand::MO_GlobalAddress: {
172 const GlobalValue *GV = MO.getGlobal();
173 std::string Name = Mang->getMangledName(GV);
177 // Assemble calls via PLT for externally visible symbols if PIC.
178 if (TM.getRelocationModel() == Reloc::PIC_ &&
179 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
180 !GV->hasLocalLinkage())
183 printOffset(MO.getOffset());
186 case MachineOperand::MO_ExternalSymbol: {
187 std::string Name(TAI->getGlobalPrefix());
188 Name += MO.getSymbolName();
191 if (TM.getRelocationModel() == Reloc::PIC_)
197 assert(0 && "Not implemented yet!");
202 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
203 const char* Modifier) {
204 const MachineOperand &MO = MI->getOperand(OpNum);
205 switch (MO.getType()) {
206 case MachineOperand::MO_Register: {
207 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
208 "Virtual registers should be already mapped!");
209 unsigned Reg = MO.getReg();
210 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
211 if (strncmp(Modifier + 7, "even", 4) == 0)
212 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
213 else if (strncmp(Modifier + 7, "odd", 3) == 0)
214 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
216 assert(0 && "Invalid subreg modifier");
219 O << '%' << TRI->getAsmName(Reg);
222 case MachineOperand::MO_Immediate:
225 case MachineOperand::MO_MachineBasicBlock:
226 printBasicBlockLabel(MO.getMBB());
228 case MachineOperand::MO_JumpTableIndex:
229 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
233 case MachineOperand::MO_ConstantPoolIndex:
234 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
237 printOffset(MO.getOffset());
239 case MachineOperand::MO_GlobalAddress: {
240 const GlobalValue *GV = MO.getGlobal();
241 std::string Name = Mang->getMangledName(GV);
246 case MachineOperand::MO_ExternalSymbol: {
247 std::string Name(TAI->getGlobalPrefix());
248 Name += MO.getSymbolName();
253 assert(0 && "Not implemented yet!");
256 switch (MO.getTargetFlags()) {
258 llvm_unreachable("Unknown target flag on GV operand");
259 case SystemZII::MO_NO_FLAG:
261 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
262 case SystemZII::MO_PLT: O << "@PLT"; break;
265 printOffset(MO.getOffset());
268 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
269 const char* Modifier) {
270 const MachineOperand &Base = MI->getOperand(OpNum);
272 // Print displacement operand.
273 printOperand(MI, OpNum+1);
275 // Print base operand (if any)
278 printOperand(MI, OpNum);
283 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
284 const char* Modifier) {
285 const MachineOperand &Base = MI->getOperand(OpNum);
286 const MachineOperand &Index = MI->getOperand(OpNum+2);
288 // Print displacement operand.
289 printOperand(MI, OpNum+1);
291 // Print base operand (if any)
294 printOperand(MI, OpNum);
295 if (Index.getReg()) {
297 printOperand(MI, OpNum+2);
301 assert(!Index.getReg() && "Should allocate base register first!");
304 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
305 /// Don't print things like \\n or \\0.
306 static void PrintUnmangledNameSafely(const Value *V, formatted_raw_ostream &OS) {
307 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
313 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
314 const TargetData *TD = TM.getTargetData();
316 if (!GVar->hasInitializer())
317 return; // External global require no code
319 // Check to see if this is a special global used by LLVM, if so, emit it.
320 if (EmitSpecialLLVMGlobal(GVar))
323 std::string name = Mang->getMangledName(GVar);
324 Constant *C = GVar->getInitializer();
325 unsigned Size = TD->getTypeAllocSize(C->getType());
326 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
328 printVisibility(name, GVar->getVisibility());
330 O << "\t.type\t" << name << ",@object\n";
332 SwitchToSection(TAI->SectionForGlobal(GVar));
334 if (C->isNullValue() && !GVar->hasSection() &&
335 !GVar->isThreadLocal() &&
336 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
338 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
340 if (GVar->hasLocalLinkage())
341 O << "\t.local\t" << name << '\n';
343 O << TAI->getCOMMDirective() << name << ',' << Size;
344 if (TAI->getCOMMDirectiveTakesAlignment())
345 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
348 O << "\t\t" << TAI->getCommentString() << ' ';
349 PrintUnmangledNameSafely(GVar, O);
355 switch (GVar->getLinkage()) {
356 case GlobalValue::CommonLinkage:
357 case GlobalValue::LinkOnceAnyLinkage:
358 case GlobalValue::LinkOnceODRLinkage:
359 case GlobalValue::WeakAnyLinkage:
360 case GlobalValue::WeakODRLinkage:
361 O << "\t.weak\t" << name << '\n';
363 case GlobalValue::DLLExportLinkage:
364 case GlobalValue::AppendingLinkage:
365 // FIXME: appending linkage variables should go into a section of
366 // their name or something. For now, just emit them as external.
367 case GlobalValue::ExternalLinkage:
368 // If external or appending, declare as a global symbol
369 O << "\t.globl " << name << '\n';
371 case GlobalValue::PrivateLinkage:
372 case GlobalValue::LinkerPrivateLinkage:
373 case GlobalValue::InternalLinkage:
376 assert(0 && "Unknown linkage type!");
379 // Use 16-bit alignment by default to simplify bunch of stuff
380 EmitAlignment(Align, GVar, 1);
383 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
384 PrintUnmangledNameSafely(GVar, O);
387 if (TAI->hasDotTypeDotSizeDirective())
388 O << "\t.size\t" << name << ", " << Size << '\n';
390 EmitGlobalConstant(C);
393 // Force static initialization.
394 extern "C" void LLVMInitializeSystemZAsmPrinter() {
395 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);