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 printRIAddrOperand(const MachineInstr *MI, int OpNum,
54 const char* Modifier = 0);
55 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
56 const char* Modifier = 0);
57 void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
58 O << (int16_t)MI->getOperand(OpNum).getImm();
60 void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
61 O << (int32_t)MI->getOperand(OpNum).getImm();
64 bool printInstruction(const MachineInstr *MI); // autogenerated.
65 void printMachineInstruction(const MachineInstr * MI);
67 void emitFunctionHeader(const MachineFunction &MF);
68 bool runOnMachineFunction(MachineFunction &F);
69 bool doInitialization(Module &M);
70 bool doFinalization(Module &M);
71 void printModuleLevelGV(const GlobalVariable* GVar);
73 void getAnalysisUsage(AnalysisUsage &AU) const {
74 AsmPrinter::getAnalysisUsage(AU);
78 } // end of anonymous namespace
80 #include "SystemZGenAsmWriter.inc"
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.
87 FunctionPass *llvm::createSystemZCodePrinterPass(raw_ostream &o,
88 SystemZTargetMachine &tm,
89 CodeGenOpt::Level OptLevel,
91 return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, 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 const Function *F = MF.getFunction();
112 SwitchToSection(TAI->SectionForGlobal(F));
114 unsigned FnAlign = 4;
115 if (F->hasFnAttr(Attribute::OptimizeForSize))
118 EmitAlignment(FnAlign, F);
120 switch (F->getLinkage()) {
121 default: assert(0 && "Unknown linkage type!");
122 case Function::InternalLinkage: // Symbols default to internal.
123 case Function::PrivateLinkage:
125 case Function::ExternalLinkage:
126 O << "\t.globl\t" << CurrentFnName << '\n';
128 case Function::LinkOnceAnyLinkage:
129 case Function::LinkOnceODRLinkage:
130 case Function::WeakAnyLinkage:
131 case Function::WeakODRLinkage:
132 O << "\t.weak\t" << CurrentFnName << '\n';
136 printVisibility(CurrentFnName, F->getVisibility());
138 O << "\t.type\t" << CurrentFnName << ",@function\n"
139 << CurrentFnName << ":\n";
142 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
143 SetupMachineFunction(MF);
146 // Print the 'header' of function
147 emitFunctionHeader(MF);
149 // Print out code for the function.
150 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
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.
157 printBasicBlockLabel(I, true, true, VerboseAsm);
161 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
163 // Print the assembly for the instruction.
164 printMachineInstruction(II);
167 if (TAI->hasDotTypeDotSizeDirective())
168 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
170 // Print out jump tables referenced by the function.
171 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
175 // We didn't modify anything
179 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
182 // Call the autogenerated instruction printer routines.
183 if (printInstruction(MI))
186 assert(0 && "Should not happen");
189 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
190 const char* Modifier) {
191 const MachineOperand &MO = MI->getOperand(OpNum);
192 switch (MO.getType()) {
193 case MachineOperand::MO_Register: {
194 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
195 "Virtual registers should be already mapped!");
196 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);
203 assert(0 && "Invalid subreg modifier");
206 O << '%' << TRI->getAsmName(Reg);
209 case MachineOperand::MO_Immediate:
212 case MachineOperand::MO_MachineBasicBlock:
213 printBasicBlockLabel(MO.getMBB());
215 case MachineOperand::MO_JumpTableIndex:
216 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
220 case MachineOperand::MO_GlobalAddress: {
221 const GlobalValue *GV = MO.getGlobal();
223 std::string Name = Mang->getValueName(GV);
224 assert(MO.getOffset() == 0 && "No offsets allowed!");
230 case MachineOperand::MO_ExternalSymbol: {
231 std::string Name(TAI->getGlobalPrefix());
232 Name += MO.getSymbolName();
237 assert(0 && "Not implemented yet!");
241 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
242 const char* Modifier) {
243 const MachineOperand &Base = MI->getOperand(OpNum);
245 // Print displacement operand.
246 printOperand(MI, OpNum+1);
248 // Print base operand (if any)
251 printOperand(MI, OpNum);
256 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
257 const char* Modifier) {
258 const MachineOperand &Base = MI->getOperand(OpNum);
259 const MachineOperand &Index = MI->getOperand(OpNum+2);
261 // Print displacement operand.
262 printOperand(MI, OpNum+1);
264 // Print base operand (if any)
267 printOperand(MI, OpNum);
268 if (Index.getReg()) {
270 printOperand(MI, OpNum+2);
274 assert(!Index.getReg() && "Should allocate base register first!");
277 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
278 /// Don't print things like \\n or \\0.
279 static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
280 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
286 void SystemZAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
287 const TargetData *TD = TM.getTargetData();
289 if (!GVar->hasInitializer())
290 return; // External global require no code
292 // Check to see if this is a special global used by LLVM, if so, emit it.
293 if (EmitSpecialLLVMGlobal(GVar))
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));
302 printVisibility(name, GVar->getVisibility());
304 O << "\t.type\t" << name << ",@object\n";
306 SwitchToSection(TAI->SectionForGlobal(GVar));
308 if (C->isNullValue() && !GVar->hasSection() &&
309 !GVar->isThreadLocal() &&
310 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
312 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
314 if (GVar->hasLocalLinkage())
315 O << "\t.local\t" << name << '\n';
317 O << TAI->getCOMMDirective() << name << ',' << Size;
318 if (TAI->getCOMMDirectiveTakesAlignment())
319 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
322 O << "\t\t" << TAI->getCommentString() << ' ';
323 PrintUnmangledNameSafely(GVar, O);
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';
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';
345 case GlobalValue::PrivateLinkage:
346 case GlobalValue::InternalLinkage:
349 assert(0 && "Unknown linkage type!");
352 // Use 16-bit alignment by default to simplify bunch of stuff
353 EmitAlignment(Align, GVar, 1);
356 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
357 PrintUnmangledNameSafely(GVar, O);
360 if (TAI->hasDotTypeDotSizeDirective())
361 O << "\t.size\t" << name << ", " << Size << '\n';
363 EmitGlobalConstant(C);