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/Target/TargetAsmInfo.h"
30 #include "llvm/Target/TargetData.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include "llvm/Target/TargetRegistry.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/FormattedStream.h"
36 #include "llvm/Support/Mangler.h"
40 STATISTIC(EmittedInsts
, "Number of machine instrs printed");
43 class VISIBILITY_HIDDEN SystemZAsmPrinter
: public AsmPrinter
{
45 SystemZAsmPrinter(formatted_raw_ostream
&O
, TargetMachine
&TM
,
46 const TargetAsmInfo
*TAI
, bool V
)
47 : AsmPrinter(O
, TM
, TAI
, V
) {}
49 virtual const char *getPassName() const {
50 return "SystemZ Assembly Printer";
53 void printOperand(const MachineInstr
*MI
, int OpNum
,
54 const char* Modifier
= 0);
55 void printPCRelImmOperand(const MachineInstr
*MI
, int OpNum
);
56 void printRIAddrOperand(const MachineInstr
*MI
, int OpNum
,
57 const char* Modifier
= 0);
58 void printRRIAddrOperand(const MachineInstr
*MI
, int OpNum
,
59 const char* Modifier
= 0);
60 void printS16ImmOperand(const MachineInstr
*MI
, int OpNum
) {
61 O
<< (int16_t)MI
->getOperand(OpNum
).getImm();
63 void printS32ImmOperand(const MachineInstr
*MI
, int OpNum
) {
64 O
<< (int32_t)MI
->getOperand(OpNum
).getImm();
67 void printInstruction(const MachineInstr
*MI
); // autogenerated.
68 void printMachineInstruction(const MachineInstr
* MI
);
70 void emitFunctionHeader(const MachineFunction
&MF
);
71 bool runOnMachineFunction(MachineFunction
&F
);
72 void PrintGlobalVariable(const GlobalVariable
* GVar
);
74 void getAnalysisUsage(AnalysisUsage
&AU
) const {
75 AsmPrinter::getAnalysisUsage(AU
);
79 } // end of anonymous namespace
81 #include "SystemZGenAsmWriter.inc"
83 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction
&MF
) {
84 unsigned FnAlign
= MF
.getAlignment();
85 const Function
*F
= MF
.getFunction();
87 SwitchToSection(getObjFileLowering().SectionForGlobal(F
, Mang
, TM
));
89 EmitAlignment(FnAlign
, F
);
91 switch (F
->getLinkage()) {
92 default: assert(0 && "Unknown linkage type!");
93 case Function::InternalLinkage
: // Symbols default to internal.
94 case Function::PrivateLinkage
:
95 case Function::LinkerPrivateLinkage
:
97 case Function::ExternalLinkage
:
98 O
<< "\t.globl\t" << CurrentFnName
<< '\n';
100 case Function::LinkOnceAnyLinkage
:
101 case Function::LinkOnceODRLinkage
:
102 case Function::WeakAnyLinkage
:
103 case Function::WeakODRLinkage
:
104 O
<< "\t.weak\t" << CurrentFnName
<< '\n';
108 printVisibility(CurrentFnName
, F
->getVisibility());
110 O
<< "\t.type\t" << CurrentFnName
<< ",@function\n"
111 << CurrentFnName
<< ":\n";
114 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction
&MF
) {
115 SetupMachineFunction(MF
);
118 // Print out constants referenced by the function
119 EmitConstantPool(MF
.getConstantPool());
121 // Print the 'header' of function
122 emitFunctionHeader(MF
);
124 // Print out code for the function.
125 for (MachineFunction::const_iterator I
= MF
.begin(), E
= MF
.end();
127 // Print a label for the basic block.
128 if (!VerboseAsm
&& (I
->pred_empty() || I
->isOnlyReachableByFallthrough())) {
129 // This is an entry block or a block that's only reachable via a
130 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
132 printBasicBlockLabel(I
, true, true, VerboseAsm
);
136 for (MachineBasicBlock::const_iterator II
= I
->begin(), E
= I
->end();
138 // Print the assembly for the instruction.
139 printMachineInstruction(II
);
142 if (TAI
->hasDotTypeDotSizeDirective())
143 O
<< "\t.size\t" << CurrentFnName
<< ", .-" << CurrentFnName
<< '\n';
145 // Print out jump tables referenced by the function.
146 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 printInstruction(MI
);
159 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr
*MI
, int OpNum
) {
160 const MachineOperand
&MO
= MI
->getOperand(OpNum
);
161 switch (MO
.getType()) {
162 case MachineOperand::MO_Immediate
:
165 case MachineOperand::MO_MachineBasicBlock
:
166 printBasicBlockLabel(MO
.getMBB(), false, false, VerboseAsm
);
168 case MachineOperand::MO_GlobalAddress
: {
169 const GlobalValue
*GV
= MO
.getGlobal();
170 std::string Name
= Mang
->getMangledName(GV
);
174 // Assemble calls via PLT for externally visible symbols if PIC.
175 if (TM
.getRelocationModel() == Reloc::PIC_
&&
176 !GV
->hasHiddenVisibility() && !GV
->hasProtectedVisibility() &&
177 !GV
->hasLocalLinkage())
180 printOffset(MO
.getOffset());
183 case MachineOperand::MO_ExternalSymbol
: {
184 std::string
Name(TAI
->getGlobalPrefix());
185 Name
+= MO
.getSymbolName();
188 if (TM
.getRelocationModel() == Reloc::PIC_
)
194 assert(0 && "Not implemented yet!");
199 void SystemZAsmPrinter::printOperand(const MachineInstr
*MI
, int OpNum
,
200 const char* Modifier
) {
201 const MachineOperand
&MO
= MI
->getOperand(OpNum
);
202 switch (MO
.getType()) {
203 case MachineOperand::MO_Register
: {
204 assert (TargetRegisterInfo::isPhysicalRegister(MO
.getReg()) &&
205 "Virtual registers should be already mapped!");
206 unsigned Reg
= MO
.getReg();
207 if (Modifier
&& strncmp(Modifier
, "subreg", 6) == 0) {
208 if (strncmp(Modifier
+ 7, "even", 4) == 0)
209 Reg
= TRI
->getSubReg(Reg
, SystemZ::SUBREG_EVEN
);
210 else if (strncmp(Modifier
+ 7, "odd", 3) == 0)
211 Reg
= TRI
->getSubReg(Reg
, SystemZ::SUBREG_ODD
);
213 assert(0 && "Invalid subreg modifier");
216 O
<< '%' << TRI
->getAsmName(Reg
);
219 case MachineOperand::MO_Immediate
:
222 case MachineOperand::MO_MachineBasicBlock
:
223 printBasicBlockLabel(MO
.getMBB());
225 case MachineOperand::MO_JumpTableIndex
:
226 O
<< TAI
->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
230 case MachineOperand::MO_ConstantPoolIndex
:
231 O
<< TAI
->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
234 printOffset(MO
.getOffset());
236 case MachineOperand::MO_GlobalAddress
: {
237 const GlobalValue
*GV
= MO
.getGlobal();
238 std::string Name
= Mang
->getMangledName(GV
);
243 case MachineOperand::MO_ExternalSymbol
: {
244 std::string
Name(TAI
->getGlobalPrefix());
245 Name
+= MO
.getSymbolName();
250 assert(0 && "Not implemented yet!");
253 switch (MO
.getTargetFlags()) {
255 llvm_unreachable("Unknown target flag on GV operand");
256 case SystemZII::MO_NO_FLAG
:
258 case SystemZII::MO_GOTENT
: O
<< "@GOTENT"; break;
259 case SystemZII::MO_PLT
: O
<< "@PLT"; break;
262 printOffset(MO
.getOffset());
265 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr
*MI
, int OpNum
,
266 const char* Modifier
) {
267 const MachineOperand
&Base
= MI
->getOperand(OpNum
);
269 // Print displacement operand.
270 printOperand(MI
, OpNum
+1);
272 // Print base operand (if any)
275 printOperand(MI
, OpNum
);
280 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr
*MI
, int OpNum
,
281 const char* Modifier
) {
282 const MachineOperand
&Base
= MI
->getOperand(OpNum
);
283 const MachineOperand
&Index
= MI
->getOperand(OpNum
+2);
285 // Print displacement operand.
286 printOperand(MI
, OpNum
+1);
288 // Print base operand (if any)
291 printOperand(MI
, OpNum
);
292 if (Index
.getReg()) {
294 printOperand(MI
, OpNum
+2);
298 assert(!Index
.getReg() && "Should allocate base register first!");
301 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable
* GVar
) {
302 const TargetData
*TD
= TM
.getTargetData();
304 if (!GVar
->hasInitializer())
305 return; // External global require no code
307 // Check to see if this is a special global used by LLVM, if so, emit it.
308 if (EmitSpecialLLVMGlobal(GVar
))
311 std::string name
= Mang
->getMangledName(GVar
);
312 Constant
*C
= GVar
->getInitializer();
313 unsigned Size
= TD
->getTypeAllocSize(C
->getType());
314 unsigned Align
= std::max(1U, TD
->getPreferredAlignmentLog(GVar
));
316 printVisibility(name
, GVar
->getVisibility());
318 O
<< "\t.type\t" << name
<< ",@object\n";
320 SwitchToSection(getObjFileLowering().SectionForGlobal(GVar
, Mang
, TM
));
322 if (C
->isNullValue() && !GVar
->hasSection() &&
323 !GVar
->isThreadLocal() &&
324 (GVar
->hasLocalLinkage() || GVar
->isWeakForLinker())) {
326 if (Size
== 0) Size
= 1; // .comm Foo, 0 is undefined, avoid it.
328 if (GVar
->hasLocalLinkage())
329 O
<< "\t.local\t" << name
<< '\n';
331 O
<< TAI
->getCOMMDirective() << name
<< ',' << Size
;
332 if (TAI
->getCOMMDirectiveTakesAlignment())
333 O
<< ',' << (TAI
->getAlignmentIsInBytes() ? (1 << Align
) : Align
);
336 O
<< "\t\t" << TAI
->getCommentString() << ' ';
337 WriteAsOperand(O
, GVar
, /*PrintType=*/false, GVar
->getParent());
343 switch (GVar
->getLinkage()) {
344 case GlobalValue::CommonLinkage
:
345 case GlobalValue::LinkOnceAnyLinkage
:
346 case GlobalValue::LinkOnceODRLinkage
:
347 case GlobalValue::WeakAnyLinkage
:
348 case GlobalValue::WeakODRLinkage
:
349 O
<< "\t.weak\t" << name
<< '\n';
351 case GlobalValue::DLLExportLinkage
:
352 case GlobalValue::AppendingLinkage
:
353 // FIXME: appending linkage variables should go into a section of
354 // their name or something. For now, just emit them as external.
355 case GlobalValue::ExternalLinkage
:
356 // If external or appending, declare as a global symbol
357 O
<< "\t.globl " << name
<< '\n';
359 case GlobalValue::PrivateLinkage
:
360 case GlobalValue::LinkerPrivateLinkage
:
361 case GlobalValue::InternalLinkage
:
364 assert(0 && "Unknown linkage type!");
367 // Use 16-bit alignment by default to simplify bunch of stuff
368 EmitAlignment(Align
, GVar
, 1);
371 O
<< "\t\t\t\t" << TAI
->getCommentString() << ' ';
372 WriteAsOperand(O
, GVar
, /*PrintType=*/false, GVar
->getParent());
375 if (TAI
->hasDotTypeDotSizeDirective())
376 O
<< "\t.size\t" << name
<< ", " << Size
<< '\n';
378 EmitGlobalConstant(C
);
381 // Force static initialization.
382 extern "C" void LLVMInitializeSystemZAsmPrinter() {
383 RegisterAsmPrinter
<SystemZAsmPrinter
> X(TheSystemZTarget
);