1 //===-- SparcV8AsmPrinter.cpp - SparcV8 LLVM assembly writer --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 GAS-format Sparc V8 assembly language.
13 //===----------------------------------------------------------------------===//
16 #include "SparcV8InstrInfo.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Module.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Support/Mangler.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/MathExtras.h"
35 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
37 struct SparcV8AsmPrinter : public AsmPrinter {
38 SparcV8AsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) {
39 Data16bitsDirective = "\t.half\t";
40 Data32bitsDirective = "\t.word\t";
41 Data64bitsDirective = 0; // .xword is only supported by V9.
42 ZeroDirective = 0; // no .zero or .space!
44 ConstantPoolSection = "\t.section \".rodata\",#alloc\n";
47 /// We name each basic block in a Function with a unique number, so
48 /// that we can consistently refer to them later. This is cleared
49 /// at the beginning of each call to runOnMachineFunction().
51 typedef std::map<const Value *, unsigned> ValueMapTy;
52 ValueMapTy NumberForBB;
54 virtual const char *getPassName() const {
55 return "SparcV8 Assembly Printer";
58 void printOperand(const MachineInstr *MI, int opNum);
59 void printMemOperand(const MachineInstr *MI, int opNum);
60 bool printInstruction(const MachineInstr *MI); // autogenerated.
61 bool runOnMachineFunction(MachineFunction &F);
62 bool doInitialization(Module &M);
63 bool doFinalization(Module &M);
65 } // end of anonymous namespace
67 #include "SparcV8GenAsmWriter.inc"
69 /// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8
70 /// assembly code for a MachineFunction to the given output stream,
71 /// using the given target machine description. This should work
72 /// regardless of whether the function is in SSA form.
74 FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o,
76 return new SparcV8AsmPrinter(o, tm);
79 /// runOnMachineFunction - This uses the printMachineInstruction()
80 /// method to print assembly for each instruction.
82 bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
83 SetupMachineFunction(MF);
85 // Print out constants referenced by the function
86 EmitConstantPool(MF.getConstantPool());
88 // BBNumber is used here so that a given Printer will never give two
89 // BBs the same name. (If you have a better way, please let me know!)
90 static unsigned BBNumber = 0;
93 // What's my mangled name?
94 CurrentFnName = Mang->getValueName(MF.getFunction());
96 // Print out labels for the function.
99 O << "\t.globl\t" << CurrentFnName << "\n";
100 O << "\t.type\t" << CurrentFnName << ", #function\n";
101 O << CurrentFnName << ":\n";
103 // Number each basic block so that we can consistently refer to them
104 // in PC-relative references.
106 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
108 NumberForBB[I->getBasicBlock()] = BBNumber++;
111 // Print out code for the function.
112 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
114 // Print a label for the basic block.
115 O << ".LBB" << Mang->getValueName(MF.getFunction ())
116 << "_" << I->getNumber () << ":\t! "
117 << I->getBasicBlock ()->getName () << "\n";
118 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
120 // Print the assembly for the instruction.
122 printInstruction(II);
127 // We didn't modify anything.
131 void SparcV8AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
132 const MachineOperand &MO = MI->getOperand (opNum);
133 const MRegisterInfo &RI = *TM.getRegisterInfo();
134 bool CloseParen = false;
135 if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
138 } else if (MI->getOpcode() ==V8::ORri &&!MO.isRegister() &&!MO.isImmediate())
143 switch (MO.getType()) {
144 case MachineOperand::MO_VirtualRegister:
145 if (Value *V = MO.getVRegValueOrNull()) {
146 O << "<" << V->getName() << ">";
150 case MachineOperand::MO_MachineRegister:
151 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
152 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
154 O << "%reg" << MO.getReg();
157 case MachineOperand::MO_SignExtendedImmed:
158 case MachineOperand::MO_UnextendedImmed:
159 O << (int)MO.getImmedValue();
161 case MachineOperand::MO_MachineBasicBlock: {
162 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
163 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
164 << "_" << MBBOp->getNumber () << "\t! "
165 << MBBOp->getBasicBlock ()->getName ();
168 case MachineOperand::MO_PCRelativeDisp:
169 std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs";
172 case MachineOperand::MO_GlobalAddress:
173 O << Mang->getValueName(MO.getGlobal());
175 case MachineOperand::MO_ExternalSymbol:
176 O << MO.getSymbolName();
178 case MachineOperand::MO_ConstantPoolIndex:
179 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
180 << MO.getConstantPoolIndex();
183 O << "<unknown operand type>"; abort (); break;
185 if (CloseParen) O << ")";
188 void SparcV8AsmPrinter::printMemOperand(const MachineInstr *MI, int opNum) {
189 printOperand(MI, opNum);
190 MachineOperand::MachineOperandType OpTy = MI->getOperand(opNum+1).getType();
192 if ((OpTy == MachineOperand::MO_VirtualRegister ||
193 OpTy == MachineOperand::MO_MachineRegister) &&
194 MI->getOperand(opNum+1).getReg() == V8::G0)
195 return; // don't print "+%g0"
196 if ((OpTy == MachineOperand::MO_SignExtendedImmed ||
197 OpTy == MachineOperand::MO_UnextendedImmed) &&
198 MI->getOperand(opNum+1).getImmedValue() == 0)
199 return; // don't print "+0"
202 if (OpTy == MachineOperand::MO_GlobalAddress ||
203 OpTy == MachineOperand::MO_ConstantPoolIndex) {
205 printOperand(MI, opNum+1);
208 printOperand(MI, opNum+1);
213 bool SparcV8AsmPrinter::doInitialization(Module &M) {
214 Mang = new Mangler(M);
215 return false; // success
218 bool SparcV8AsmPrinter::doFinalization(Module &M) {
219 const TargetData &TD = TM.getTargetData();
221 // Print out module-level global variables here.
222 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
223 if (I->hasInitializer()) { // External global require no code
225 std::string name = Mang->getValueName(I);
226 Constant *C = I->getInitializer();
227 unsigned Size = TD.getTypeSize(C->getType());
228 unsigned Align = TD.getTypeAlignment(C->getType());
230 if (C->isNullValue() &&
231 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
232 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
233 SwitchSection(".data", I);
234 if (I->hasInternalLinkage())
235 O << "\t.local " << name << "\n";
237 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
238 << "," << (unsigned)TD.getTypeAlignment(C->getType());
240 WriteAsOperand(O, I, true, true, &M);
243 switch (I->getLinkage()) {
244 case GlobalValue::LinkOnceLinkage:
245 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
246 // Nonnull linkonce -> weak
247 O << "\t.weak " << name << "\n";
248 SwitchSection("", I);
249 O << "\t.section\t\".llvm.linkonce.d." << name
250 << "\",\"aw\",@progbits\n";
253 case GlobalValue::AppendingLinkage:
254 // FIXME: appending linkage variables should go into a section of
255 // their name or something. For now, just emit them as external.
256 case GlobalValue::ExternalLinkage:
257 // If external or appending, declare as a global symbol
258 O << "\t.globl " << name << "\n";
260 case GlobalValue::InternalLinkage:
261 if (C->isNullValue())
262 SwitchSection(".bss", I);
264 SwitchSection(".data", I);
266 case GlobalValue::GhostLinkage:
267 std::cerr << "Should not have any unmaterialized functions!\n";
271 O << "\t.align " << Align << "\n";
272 O << "\t.type " << name << ",#object\n";
273 O << "\t.size " << name << "," << Size << "\n";
274 O << name << ":\t\t\t\t! ";
275 WriteAsOperand(O, I, true, true, &M);
277 WriteAsOperand(O, C, false, false, &M);
279 EmitGlobalConstant(C);
283 AsmPrinter::doFinalization(M);
284 return false; // success