replace printBasicBlockLabel with EmitBasicBlockStart,
[llvm/avr.git] / lib / Target / SystemZ / AsmPrinter / SystemZAsmPrinter.cpp
blob8289010254465202527e811f98f886f428539ad9
1 //===-- SystemZAsmPrinter.cpp - SystemZ LLVM assembly writer ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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"
16 #include "SystemZ.h"
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"
40 using namespace llvm;
42 STATISTIC(EmittedInsts, "Number of machine instrs printed");
44 namespace {
45 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
46 public:
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 void printMachineInstruction(const MachineInstr * MI);
72 void emitFunctionHeader(const MachineFunction &MF);
73 bool runOnMachineFunction(MachineFunction &F);
74 void PrintGlobalVariable(const GlobalVariable* GVar);
76 void getAnalysisUsage(AnalysisUsage &AU) const {
77 AsmPrinter::getAnalysisUsage(AU);
78 AU.setPreservesAll();
81 } // end of anonymous namespace
83 #include "SystemZGenAsmWriter.inc"
85 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
86 unsigned FnAlign = MF.getAlignment();
87 const Function *F = MF.getFunction();
89 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
91 EmitAlignment(FnAlign, F);
93 switch (F->getLinkage()) {
94 default: assert(0 && "Unknown linkage type!");
95 case Function::InternalLinkage: // Symbols default to internal.
96 case Function::PrivateLinkage:
97 case Function::LinkerPrivateLinkage:
98 break;
99 case Function::ExternalLinkage:
100 O << "\t.globl\t" << CurrentFnName << '\n';
101 break;
102 case Function::LinkOnceAnyLinkage:
103 case Function::LinkOnceODRLinkage:
104 case Function::WeakAnyLinkage:
105 case Function::WeakODRLinkage:
106 O << "\t.weak\t" << CurrentFnName << '\n';
107 break;
110 printVisibility(CurrentFnName, F->getVisibility());
112 O << "\t.type\t" << CurrentFnName << ",@function\n"
113 << CurrentFnName << ":\n";
116 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
117 SetupMachineFunction(MF);
118 O << "\n\n";
120 // Print out constants referenced by the function
121 EmitConstantPool(MF.getConstantPool());
123 // Print the 'header' of function
124 emitFunctionHeader(MF);
126 // Print out code for the function.
127 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
128 I != E; ++I) {
129 // Print a label for the basic block.
130 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
131 // This is an entry block or a block that's only reachable via a
132 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
133 } else {
134 EmitBasicBlockStart(I);
135 O << '\n';
138 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
139 II != E; ++II)
140 // Print the assembly for the instruction.
141 printMachineInstruction(II);
144 if (MAI->hasDotTypeDotSizeDirective())
145 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
147 // Print out jump tables referenced by the function.
148 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
150 // We didn't modify anything
151 return false;
154 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
155 ++EmittedInsts;
157 processDebugLoc(MI->getDebugLoc());
159 // Call the autogenerated instruction printer routines.
160 printInstruction(MI);
162 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
163 EmitComments(*MI);
164 O << '\n';
167 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
168 const MachineOperand &MO = MI->getOperand(OpNum);
169 switch (MO.getType()) {
170 case MachineOperand::MO_Immediate:
171 O << MO.getImm();
172 return;
173 case MachineOperand::MO_MachineBasicBlock:
174 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
175 return;
176 case MachineOperand::MO_GlobalAddress: {
177 const GlobalValue *GV = MO.getGlobal();
178 std::string Name = Mang->getMangledName(GV);
180 O << Name;
182 // Assemble calls via PLT for externally visible symbols if PIC.
183 if (TM.getRelocationModel() == Reloc::PIC_ &&
184 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
185 !GV->hasLocalLinkage())
186 O << "@PLT";
188 printOffset(MO.getOffset());
189 return;
191 case MachineOperand::MO_ExternalSymbol: {
192 std::string Name(MAI->getGlobalPrefix());
193 Name += MO.getSymbolName();
194 O << Name;
196 if (TM.getRelocationModel() == Reloc::PIC_)
197 O << "@PLT";
199 return;
201 default:
202 assert(0 && "Not implemented yet!");
207 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
208 const char* Modifier) {
209 const MachineOperand &MO = MI->getOperand(OpNum);
210 switch (MO.getType()) {
211 case MachineOperand::MO_Register: {
212 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
213 "Virtual registers should be already mapped!");
214 unsigned Reg = MO.getReg();
215 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
216 if (strncmp(Modifier + 7, "even", 4) == 0)
217 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
218 else if (strncmp(Modifier + 7, "odd", 3) == 0)
219 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
220 else
221 assert(0 && "Invalid subreg modifier");
224 O << '%' << TRI->getAsmName(Reg);
225 return;
227 case MachineOperand::MO_Immediate:
228 O << MO.getImm();
229 return;
230 case MachineOperand::MO_MachineBasicBlock:
231 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
232 return;
233 case MachineOperand::MO_JumpTableIndex:
234 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
235 << MO.getIndex();
237 return;
238 case MachineOperand::MO_ConstantPoolIndex:
239 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
240 << MO.getIndex();
242 printOffset(MO.getOffset());
243 break;
244 case MachineOperand::MO_GlobalAddress: {
245 const GlobalValue *GV = MO.getGlobal();
246 std::string Name = Mang->getMangledName(GV);
248 O << Name;
249 break;
251 case MachineOperand::MO_ExternalSymbol: {
252 std::string Name(MAI->getGlobalPrefix());
253 Name += MO.getSymbolName();
254 O << Name;
255 break;
257 default:
258 assert(0 && "Not implemented yet!");
261 switch (MO.getTargetFlags()) {
262 default:
263 llvm_unreachable("Unknown target flag on GV operand");
264 case SystemZII::MO_NO_FLAG:
265 break;
266 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
267 case SystemZII::MO_PLT: O << "@PLT"; break;
270 printOffset(MO.getOffset());
273 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
274 const char* Modifier) {
275 const MachineOperand &Base = MI->getOperand(OpNum);
277 // Print displacement operand.
278 printOperand(MI, OpNum+1);
280 // Print base operand (if any)
281 if (Base.getReg()) {
282 O << '(';
283 printOperand(MI, OpNum);
284 O << ')';
288 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
289 const char* Modifier) {
290 const MachineOperand &Base = MI->getOperand(OpNum);
291 const MachineOperand &Index = MI->getOperand(OpNum+2);
293 // Print displacement operand.
294 printOperand(MI, OpNum+1);
296 // Print base operand (if any)
297 if (Base.getReg()) {
298 O << '(';
299 printOperand(MI, OpNum);
300 if (Index.getReg()) {
301 O << ',';
302 printOperand(MI, OpNum+2);
304 O << ')';
305 } else
306 assert(!Index.getReg() && "Should allocate base register first!");
309 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
310 const TargetData *TD = TM.getTargetData();
312 if (!GVar->hasInitializer())
313 return; // External global require no code
315 // Check to see if this is a special global used by LLVM, if so, emit it.
316 if (EmitSpecialLLVMGlobal(GVar))
317 return;
319 std::string name = Mang->getMangledName(GVar);
320 Constant *C = GVar->getInitializer();
321 unsigned Size = TD->getTypeAllocSize(C->getType());
322 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
324 printVisibility(name, GVar->getVisibility());
326 O << "\t.type\t" << name << ",@object\n";
328 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
329 TM));
331 if (C->isNullValue() && !GVar->hasSection() &&
332 !GVar->isThreadLocal() &&
333 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
335 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
337 if (GVar->hasLocalLinkage())
338 O << "\t.local\t" << name << '\n';
340 O << MAI->getCOMMDirective() << name << ',' << Size;
341 if (MAI->getCOMMDirectiveTakesAlignment())
342 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
344 if (VerboseAsm) {
345 O << "\t\t" << MAI->getCommentString() << ' ';
346 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
348 O << '\n';
349 return;
352 switch (GVar->getLinkage()) {
353 case GlobalValue::CommonLinkage:
354 case GlobalValue::LinkOnceAnyLinkage:
355 case GlobalValue::LinkOnceODRLinkage:
356 case GlobalValue::WeakAnyLinkage:
357 case GlobalValue::WeakODRLinkage:
358 O << "\t.weak\t" << name << '\n';
359 break;
360 case GlobalValue::DLLExportLinkage:
361 case GlobalValue::AppendingLinkage:
362 // FIXME: appending linkage variables should go into a section of
363 // their name or something. For now, just emit them as external.
364 case GlobalValue::ExternalLinkage:
365 // If external or appending, declare as a global symbol
366 O << "\t.globl " << name << '\n';
367 // FALL THROUGH
368 case GlobalValue::PrivateLinkage:
369 case GlobalValue::LinkerPrivateLinkage:
370 case GlobalValue::InternalLinkage:
371 break;
372 default:
373 assert(0 && "Unknown linkage type!");
376 // Use 16-bit alignment by default to simplify bunch of stuff
377 EmitAlignment(Align, GVar, 1);
378 O << name << ":";
379 if (VerboseAsm) {
380 O << "\t\t\t\t" << MAI->getCommentString() << ' ';
381 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
383 O << '\n';
384 if (MAI->hasDotTypeDotSizeDirective())
385 O << "\t.size\t" << name << ", " << Size << '\n';
387 EmitGlobalConstant(C);
390 // Force static initialization.
391 extern "C" void LLVMInitializeSystemZAsmPrinter() {
392 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);