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 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(formatted_raw_ostream &o,
91 return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
94 bool SystemZAsmPrinter::doInitialization(Module &M) {
95 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
96 return false; // success
100 bool SystemZAsmPrinter::doFinalization(Module &M) {
101 // Print out module-level global variables here.
102 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
104 printModuleLevelGV(I);
106 return AsmPrinter::doFinalization(M);
109 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
110 unsigned FnAlign = MF.getAlignment();
111 const Function *F = MF.getFunction();
113 SwitchToSection(TAI->SectionForGlobal(F));
115 EmitAlignment(FnAlign, F);
117 switch (F->getLinkage()) {
118 default: assert(0 && "Unknown linkage type!");
119 case Function::InternalLinkage: // Symbols default to internal.
120 case Function::PrivateLinkage:
121 case Function::LinkerPrivateLinkage:
123 case Function::ExternalLinkage:
124 O << "\t.globl\t" << CurrentFnName << '\n';
126 case Function::LinkOnceAnyLinkage:
127 case Function::LinkOnceODRLinkage:
128 case Function::WeakAnyLinkage:
129 case Function::WeakODRLinkage:
130 O << "\t.weak\t" << CurrentFnName << '\n';
134 printVisibility(CurrentFnName, F->getVisibility());
136 O << "\t.type\t" << CurrentFnName << ",@function\n"
137 << CurrentFnName << ":\n";
140 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
141 SetupMachineFunction(MF);
144 // Print out constants referenced by the function
145 EmitConstantPool(MF.getConstantPool());
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 llvm_unreachable("Unreachable!");
190 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
191 const MachineOperand &MO = MI->getOperand(OpNum);
192 switch (MO.getType()) {
193 case MachineOperand::MO_Immediate:
196 case MachineOperand::MO_MachineBasicBlock:
197 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
199 case MachineOperand::MO_GlobalAddress: {
200 const GlobalValue *GV = MO.getGlobal();
201 std::string Name = Mang->getMangledName(GV);
205 // Assemble calls via PLT for externally visible symbols if PIC.
206 if (TM.getRelocationModel() == Reloc::PIC_ &&
207 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
208 !GV->hasLocalLinkage())
211 printOffset(MO.getOffset());
214 case MachineOperand::MO_ExternalSymbol: {
215 std::string Name(TAI->getGlobalPrefix());
216 Name += MO.getSymbolName();
219 if (TM.getRelocationModel() == Reloc::PIC_)
225 assert(0 && "Not implemented yet!");
230 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
231 const char* Modifier) {
232 const MachineOperand &MO = MI->getOperand(OpNum);
233 switch (MO.getType()) {
234 case MachineOperand::MO_Register: {
235 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
236 "Virtual registers should be already mapped!");
237 unsigned Reg = MO.getReg();
238 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
239 if (strncmp(Modifier + 7, "even", 4) == 0)
240 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
241 else if (strncmp(Modifier + 7, "odd", 3) == 0)
242 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
244 assert(0 && "Invalid subreg modifier");
247 O << '%' << TRI->getAsmName(Reg);
250 case MachineOperand::MO_Immediate:
253 case MachineOperand::MO_MachineBasicBlock:
254 printBasicBlockLabel(MO.getMBB());
256 case MachineOperand::MO_JumpTableIndex:
257 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
261 case MachineOperand::MO_ConstantPoolIndex:
262 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
265 printOffset(MO.getOffset());
267 case MachineOperand::MO_GlobalAddress: {
268 const GlobalValue *GV = MO.getGlobal();
269 std::string Name = Mang->getMangledName(GV);
274 case MachineOperand::MO_ExternalSymbol: {
275 std::string Name(TAI->getGlobalPrefix());
276 Name += MO.getSymbolName();
281 assert(0 && "Not implemented yet!");
284 switch (MO.getTargetFlags()) {
286 llvm_unreachable("Unknown target flag on GV operand");
287 case SystemZII::MO_NO_FLAG:
289 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
290 case SystemZII::MO_PLT: O << "@PLT"; break;
293 printOffset(MO.getOffset());
296 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
297 const char* Modifier) {
298 const MachineOperand &Base = MI->getOperand(OpNum);
300 // Print displacement operand.
301 printOperand(MI, OpNum+1);
303 // Print base operand (if any)
306 printOperand(MI, OpNum);
311 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
312 const char* Modifier) {
313 const MachineOperand &Base = MI->getOperand(OpNum);
314 const MachineOperand &Index = MI->getOperand(OpNum+2);
316 // Print displacement operand.
317 printOperand(MI, OpNum+1);
319 // Print base operand (if any)
322 printOperand(MI, OpNum);
323 if (Index.getReg()) {
325 printOperand(MI, OpNum+2);
329 assert(!Index.getReg() && "Should allocate base register first!");
332 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
333 /// Don't print things like \\n or \\0.
334 static void PrintUnmangledNameSafely(const Value *V, formatted_raw_ostream &OS) {
335 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
341 void SystemZAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
342 const TargetData *TD = TM.getTargetData();
344 if (!GVar->hasInitializer())
345 return; // External global require no code
347 // Check to see if this is a special global used by LLVM, if so, emit it.
348 if (EmitSpecialLLVMGlobal(GVar))
351 std::string name = Mang->getMangledName(GVar);
352 Constant *C = GVar->getInitializer();
353 unsigned Size = TD->getTypeAllocSize(C->getType());
354 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
356 printVisibility(name, GVar->getVisibility());
358 O << "\t.type\t" << name << ",@object\n";
360 SwitchToSection(TAI->SectionForGlobal(GVar));
362 if (C->isNullValue() && !GVar->hasSection() &&
363 !GVar->isThreadLocal() &&
364 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
366 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
368 if (GVar->hasLocalLinkage())
369 O << "\t.local\t" << name << '\n';
371 O << TAI->getCOMMDirective() << name << ',' << Size;
372 if (TAI->getCOMMDirectiveTakesAlignment())
373 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
376 O << "\t\t" << TAI->getCommentString() << ' ';
377 PrintUnmangledNameSafely(GVar, O);
383 switch (GVar->getLinkage()) {
384 case GlobalValue::CommonLinkage:
385 case GlobalValue::LinkOnceAnyLinkage:
386 case GlobalValue::LinkOnceODRLinkage:
387 case GlobalValue::WeakAnyLinkage:
388 case GlobalValue::WeakODRLinkage:
389 O << "\t.weak\t" << name << '\n';
391 case GlobalValue::DLLExportLinkage:
392 case GlobalValue::AppendingLinkage:
393 // FIXME: appending linkage variables should go into a section of
394 // their name or something. For now, just emit them as external.
395 case GlobalValue::ExternalLinkage:
396 // If external or appending, declare as a global symbol
397 O << "\t.globl " << name << '\n';
399 case GlobalValue::PrivateLinkage:
400 case GlobalValue::LinkerPrivateLinkage:
401 case GlobalValue::InternalLinkage:
404 assert(0 && "Unknown linkage type!");
407 // Use 16-bit alignment by default to simplify bunch of stuff
408 EmitAlignment(Align, GVar, 1);
411 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
412 PrintUnmangledNameSafely(GVar, O);
415 if (TAI->hasDotTypeDotSizeDirective())
416 O << "\t.size\t" << name << ", " << Size << '\n';
418 EmitGlobalConstant(C);
421 // Force static initialization.
422 extern "C" void LLVMInitializeSystemZAsmPrinter() {
423 TargetRegistry::RegisterAsmPrinter(TheSystemZTarget,
424 createSystemZCodePrinterPass);