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/Assembly/Writer.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/Target/TargetData.h"
33 #include "llvm/Target/TargetLoweringObjectFile.h"
34 #include "llvm/Target/TargetRegistry.h"
35 #include "llvm/ADT/Statistic.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/FormattedStream.h"
38 #include "llvm/Support/Mangler.h"
42 STATISTIC(EmittedInsts
, "Number of machine instrs printed");
45 class VISIBILITY_HIDDEN SystemZAsmPrinter
: public AsmPrinter
{
47 SystemZAsmPrinter(formatted_raw_ostream
&O
, TargetMachine
&TM
,
48 const MCAsmInfo
*MAI
, bool V
)
49 : AsmPrinter(O
, TM
, MAI
, V
) {}
51 virtual const char *getPassName() const {
52 return "SystemZ Assembly Printer";
55 void printOperand(const MachineInstr
*MI
, int OpNum
,
56 const char* Modifier
= 0);
57 void printPCRelImmOperand(const MachineInstr
*MI
, int OpNum
);
58 void printRIAddrOperand(const MachineInstr
*MI
, int OpNum
,
59 const char* Modifier
= 0);
60 void printRRIAddrOperand(const MachineInstr
*MI
, int OpNum
,
61 const char* Modifier
= 0);
62 void printS16ImmOperand(const MachineInstr
*MI
, int OpNum
) {
63 O
<< (int16_t)MI
->getOperand(OpNum
).getImm();
65 void printS32ImmOperand(const MachineInstr
*MI
, int OpNum
) {
66 O
<< (int32_t)MI
->getOperand(OpNum
).getImm();
69 void printInstruction(const MachineInstr
*MI
); // autogenerated.
70 static const char *getRegisterName(unsigned RegNo
);
72 void printMachineInstruction(const MachineInstr
* MI
);
74 void emitFunctionHeader(const MachineFunction
&MF
);
75 bool runOnMachineFunction(MachineFunction
&F
);
76 void PrintGlobalVariable(const GlobalVariable
* GVar
);
78 void getAnalysisUsage(AnalysisUsage
&AU
) const {
79 AsmPrinter::getAnalysisUsage(AU
);
83 } // end of anonymous namespace
85 #include "SystemZGenAsmWriter.inc"
87 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction
&MF
) {
88 unsigned FnAlign
= MF
.getAlignment();
89 const Function
*F
= MF
.getFunction();
91 OutStreamer
.SwitchSection(getObjFileLowering().SectionForGlobal(F
, Mang
, TM
));
93 EmitAlignment(FnAlign
, F
);
95 switch (F
->getLinkage()) {
96 default: assert(0 && "Unknown linkage type!");
97 case Function::InternalLinkage
: // Symbols default to internal.
98 case Function::PrivateLinkage
:
99 case Function::LinkerPrivateLinkage
:
101 case Function::ExternalLinkage
:
102 O
<< "\t.globl\t" << CurrentFnName
<< '\n';
104 case Function::LinkOnceAnyLinkage
:
105 case Function::LinkOnceODRLinkage
:
106 case Function::WeakAnyLinkage
:
107 case Function::WeakODRLinkage
:
108 O
<< "\t.weak\t" << CurrentFnName
<< '\n';
112 printVisibility(CurrentFnName
, F
->getVisibility());
114 O
<< "\t.type\t" << CurrentFnName
<< ",@function\n"
115 << CurrentFnName
<< ":\n";
118 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction
&MF
) {
119 SetupMachineFunction(MF
);
122 // Print out constants referenced by the function
123 EmitConstantPool(MF
.getConstantPool());
125 // Print the 'header' of function
126 emitFunctionHeader(MF
);
128 // Print out code for the function.
129 for (MachineFunction::const_iterator I
= MF
.begin(), E
= MF
.end();
131 // Print a label for the basic block.
132 if (!VerboseAsm
&& (I
->pred_empty() || I
->isOnlyReachableByFallthrough())) {
133 // This is an entry block or a block that's only reachable via a
134 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
136 EmitBasicBlockStart(I
);
140 for (MachineBasicBlock::const_iterator II
= I
->begin(), E
= I
->end();
142 // Print the assembly for the instruction.
143 printMachineInstruction(II
);
146 if (MAI
->hasDotTypeDotSizeDirective())
147 O
<< "\t.size\t" << CurrentFnName
<< ", .-" << CurrentFnName
<< '\n';
149 // Print out jump tables referenced by the function.
150 EmitJumpTableInfo(MF
.getJumpTableInfo(), MF
);
152 // We didn't modify anything
156 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr
*MI
) {
159 processDebugLoc(MI
->getDebugLoc());
161 // Call the autogenerated instruction printer routines.
162 printInstruction(MI
);
164 if (VerboseAsm
&& !MI
->getDebugLoc().isUnknown())
169 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr
*MI
, int OpNum
){
170 const MachineOperand
&MO
= MI
->getOperand(OpNum
);
171 switch (MO
.getType()) {
172 case MachineOperand::MO_Immediate
:
175 case MachineOperand::MO_MachineBasicBlock
:
176 GetMBBSymbol(MO
.getMBB()->getNumber())->print(O
, MAI
);
178 case MachineOperand::MO_GlobalAddress
: {
179 const GlobalValue
*GV
= MO
.getGlobal();
180 std::string Name
= Mang
->getMangledName(GV
);
184 // Assemble calls via PLT for externally visible symbols if PIC.
185 if (TM
.getRelocationModel() == Reloc::PIC_
&&
186 !GV
->hasHiddenVisibility() && !GV
->hasProtectedVisibility() &&
187 !GV
->hasLocalLinkage())
190 printOffset(MO
.getOffset());
193 case MachineOperand::MO_ExternalSymbol
: {
194 std::string
Name(MAI
->getGlobalPrefix());
195 Name
+= MO
.getSymbolName();
198 if (TM
.getRelocationModel() == Reloc::PIC_
)
204 assert(0 && "Not implemented yet!");
209 void SystemZAsmPrinter::printOperand(const MachineInstr
*MI
, int OpNum
,
210 const char* Modifier
) {
211 const MachineOperand
&MO
= MI
->getOperand(OpNum
);
212 switch (MO
.getType()) {
213 case MachineOperand::MO_Register
: {
214 assert (TargetRegisterInfo::isPhysicalRegister(MO
.getReg()) &&
215 "Virtual registers should be already mapped!");
216 unsigned Reg
= MO
.getReg();
217 if (Modifier
&& strncmp(Modifier
, "subreg", 6) == 0) {
218 if (strncmp(Modifier
+ 7, "even", 4) == 0)
219 Reg
= TRI
->getSubReg(Reg
, SystemZ::SUBREG_EVEN
);
220 else if (strncmp(Modifier
+ 7, "odd", 3) == 0)
221 Reg
= TRI
->getSubReg(Reg
, SystemZ::SUBREG_ODD
);
223 assert(0 && "Invalid subreg modifier");
226 O
<< '%' << getRegisterName(Reg
);
229 case MachineOperand::MO_Immediate
:
232 case MachineOperand::MO_MachineBasicBlock
:
233 GetMBBSymbol(MO
.getMBB()->getNumber())->print(O
, MAI
);
235 case MachineOperand::MO_JumpTableIndex
:
236 O
<< MAI
->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
240 case MachineOperand::MO_ConstantPoolIndex
:
241 O
<< MAI
->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
244 printOffset(MO
.getOffset());
246 case MachineOperand::MO_GlobalAddress
: {
247 const GlobalValue
*GV
= MO
.getGlobal();
248 std::string Name
= Mang
->getMangledName(GV
);
253 case MachineOperand::MO_ExternalSymbol
: {
254 std::string
Name(MAI
->getGlobalPrefix());
255 Name
+= MO
.getSymbolName();
260 assert(0 && "Not implemented yet!");
263 switch (MO
.getTargetFlags()) {
265 llvm_unreachable("Unknown target flag on GV operand");
266 case SystemZII::MO_NO_FLAG
:
268 case SystemZII::MO_GOTENT
: O
<< "@GOTENT"; break;
269 case SystemZII::MO_PLT
: O
<< "@PLT"; break;
272 printOffset(MO
.getOffset());
275 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr
*MI
, int OpNum
,
276 const char* Modifier
) {
277 const MachineOperand
&Base
= MI
->getOperand(OpNum
);
279 // Print displacement operand.
280 printOperand(MI
, OpNum
+1);
282 // Print base operand (if any)
285 printOperand(MI
, OpNum
);
290 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr
*MI
, int OpNum
,
291 const char* Modifier
) {
292 const MachineOperand
&Base
= MI
->getOperand(OpNum
);
293 const MachineOperand
&Index
= MI
->getOperand(OpNum
+2);
295 // Print displacement operand.
296 printOperand(MI
, OpNum
+1);
298 // Print base operand (if any)
301 printOperand(MI
, OpNum
);
302 if (Index
.getReg()) {
304 printOperand(MI
, OpNum
+2);
308 assert(!Index
.getReg() && "Should allocate base register first!");
311 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable
* GVar
) {
312 const TargetData
*TD
= TM
.getTargetData();
314 if (!GVar
->hasInitializer())
315 return; // External global require no code
317 // Check to see if this is a special global used by LLVM, if so, emit it.
318 if (EmitSpecialLLVMGlobal(GVar
))
321 std::string name
= Mang
->getMangledName(GVar
);
322 Constant
*C
= GVar
->getInitializer();
323 unsigned Size
= TD
->getTypeAllocSize(C
->getType());
324 unsigned Align
= std::max(1U, TD
->getPreferredAlignmentLog(GVar
));
326 printVisibility(name
, GVar
->getVisibility());
328 O
<< "\t.type\t" << name
<< ",@object\n";
330 OutStreamer
.SwitchSection(getObjFileLowering().SectionForGlobal(GVar
, Mang
,
333 if (C
->isNullValue() && !GVar
->hasSection() &&
334 !GVar
->isThreadLocal() &&
335 (GVar
->hasLocalLinkage() || GVar
->isWeakForLinker())) {
337 if (Size
== 0) Size
= 1; // .comm Foo, 0 is undefined, avoid it.
339 if (GVar
->hasLocalLinkage())
340 O
<< "\t.local\t" << name
<< '\n';
342 O
<< MAI
->getCOMMDirective() << name
<< ',' << Size
;
343 if (MAI
->getCOMMDirectiveTakesAlignment())
344 O
<< ',' << (MAI
->getAlignmentIsInBytes() ? (1 << Align
) : Align
);
347 O
<< "\t\t" << MAI
->getCommentString() << ' ';
348 WriteAsOperand(O
, GVar
, /*PrintType=*/false, GVar
->getParent());
354 switch (GVar
->getLinkage()) {
355 case GlobalValue::CommonLinkage
:
356 case GlobalValue::LinkOnceAnyLinkage
:
357 case GlobalValue::LinkOnceODRLinkage
:
358 case GlobalValue::WeakAnyLinkage
:
359 case GlobalValue::WeakODRLinkage
:
360 O
<< "\t.weak\t" << name
<< '\n';
362 case GlobalValue::DLLExportLinkage
:
363 case GlobalValue::AppendingLinkage
:
364 // FIXME: appending linkage variables should go into a section of
365 // their name or something. For now, just emit them as external.
366 case GlobalValue::ExternalLinkage
:
367 // If external or appending, declare as a global symbol
368 O
<< "\t.globl " << name
<< '\n';
370 case GlobalValue::PrivateLinkage
:
371 case GlobalValue::LinkerPrivateLinkage
:
372 case GlobalValue::InternalLinkage
:
375 assert(0 && "Unknown linkage type!");
378 // Use 16-bit alignment by default to simplify bunch of stuff
379 EmitAlignment(Align
, GVar
, 1);
382 O
<< "\t\t\t\t" << MAI
->getCommentString() << ' ';
383 WriteAsOperand(O
, GVar
, /*PrintType=*/false, GVar
->getParent());
386 if (MAI
->hasDotTypeDotSizeDirective())
387 O
<< "\t.size\t" << name
<< ", " << Size
<< '\n';
389 EmitGlobalConstant(C
);
392 // Force static initialization.
393 extern "C" void LLVMInitializeSystemZAsmPrinter() {
394 RegisterAsmPrinter
<SystemZAsmPrinter
> X(TheSystemZTarget
);