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"
36 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
38 struct SparcV8AsmPrinter : public AsmPrinter {
39 SparcV8AsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) {
40 Data16bitsDirective = "\t.half\t";
41 Data32bitsDirective = "\t.word\t";
42 Data64bitsDirective = 0; // .xword is only supported by V9.
43 ZeroDirective = 0; // no .zero or .space!
45 ConstantPoolSection = "\t.section \".rodata\",#alloc\n";
48 /// We name each basic block in a Function with a unique number, so
49 /// that we can consistently refer to them later. This is cleared
50 /// at the beginning of each call to runOnMachineFunction().
52 typedef std::map<const Value *, unsigned> ValueMapTy;
53 ValueMapTy NumberForBB;
55 virtual const char *getPassName() const {
56 return "SparcV8 Assembly Printer";
59 void printOperand(const MachineInstr *MI, int opNum);
60 void printMemOperand(const MachineInstr *MI, int opNum);
61 void printV8CCOperand(const MachineInstr *MI, int opNum);
63 bool printInstruction(const MachineInstr *MI); // autogenerated.
64 bool runOnMachineFunction(MachineFunction &F);
65 bool doInitialization(Module &M);
66 bool doFinalization(Module &M);
68 } // end of anonymous namespace
70 #include "SparcV8GenAsmWriter.inc"
72 /// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8
73 /// assembly code for a MachineFunction to the given output stream,
74 /// using the given target machine description. This should work
75 /// regardless of whether the function is in SSA form.
77 FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o,
79 return new SparcV8AsmPrinter(o, tm);
82 /// runOnMachineFunction - This uses the printMachineInstruction()
83 /// method to print assembly for each instruction.
85 bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
86 SetupMachineFunction(MF);
88 // Print out constants referenced by the function
89 EmitConstantPool(MF.getConstantPool());
91 // BBNumber is used here so that a given Printer will never give two
92 // BBs the same name. (If you have a better way, please let me know!)
93 static unsigned BBNumber = 0;
96 // What's my mangled name?
97 CurrentFnName = Mang->getValueName(MF.getFunction());
99 // Print out labels for the function.
101 O << "\t.align 16\n";
102 O << "\t.globl\t" << CurrentFnName << "\n";
103 O << "\t.type\t" << CurrentFnName << ", #function\n";
104 O << CurrentFnName << ":\n";
106 // Number each basic block so that we can consistently refer to them
107 // in PC-relative references.
109 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
111 NumberForBB[I->getBasicBlock()] = BBNumber++;
114 // Print out code for the function.
115 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
117 // Print a label for the basic block.
119 O << ".LBB" << Mang->getValueName(MF.getFunction ())
120 << "_" << I->getNumber () << ":\t! "
121 << I->getBasicBlock ()->getName () << "\n";
122 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
124 // Print the assembly for the instruction.
126 printInstruction(II);
131 // We didn't modify anything.
135 void SparcV8AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
136 const MachineOperand &MO = MI->getOperand (opNum);
137 const MRegisterInfo &RI = *TM.getRegisterInfo();
138 bool CloseParen = false;
139 if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
142 } else if ((MI->getOpcode() == V8::ORri || MI->getOpcode() == V8::ADDri)
143 && !MO.isRegister() && !MO.isImmediate()) {
147 switch (MO.getType()) {
148 case MachineOperand::MO_VirtualRegister:
149 if (Value *V = MO.getVRegValueOrNull()) {
150 O << "<" << V->getName() << ">";
154 case MachineOperand::MO_MachineRegister:
155 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
156 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
158 O << "%reg" << MO.getReg();
161 case MachineOperand::MO_SignExtendedImmed:
162 case MachineOperand::MO_UnextendedImmed:
163 O << (int)MO.getImmedValue();
165 case MachineOperand::MO_MachineBasicBlock: {
166 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
167 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
168 << "_" << MBBOp->getNumber () << "\t! "
169 << MBBOp->getBasicBlock ()->getName ();
172 case MachineOperand::MO_PCRelativeDisp:
173 std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs";
176 case MachineOperand::MO_GlobalAddress:
177 O << Mang->getValueName(MO.getGlobal());
179 case MachineOperand::MO_ExternalSymbol:
180 O << MO.getSymbolName();
182 case MachineOperand::MO_ConstantPoolIndex:
183 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
184 << MO.getConstantPoolIndex();
187 O << "<unknown operand type>"; abort (); break;
189 if (CloseParen) O << ")";
192 void SparcV8AsmPrinter::printMemOperand(const MachineInstr *MI, int opNum) {
193 printOperand(MI, opNum);
194 MachineOperand::MachineOperandType OpTy = MI->getOperand(opNum+1).getType();
196 if ((OpTy == MachineOperand::MO_VirtualRegister ||
197 OpTy == MachineOperand::MO_MachineRegister) &&
198 MI->getOperand(opNum+1).getReg() == V8::G0)
199 return; // don't print "+%g0"
200 if ((OpTy == MachineOperand::MO_SignExtendedImmed ||
201 OpTy == MachineOperand::MO_UnextendedImmed) &&
202 MI->getOperand(opNum+1).getImmedValue() == 0)
203 return; // don't print "+0"
206 if (OpTy == MachineOperand::MO_GlobalAddress ||
207 OpTy == MachineOperand::MO_ConstantPoolIndex) {
209 printOperand(MI, opNum+1);
212 printOperand(MI, opNum+1);
216 void SparcV8AsmPrinter::printV8CCOperand(const MachineInstr *MI, int opNum) {
217 int CC = (int)MI->getOperand(opNum).getImmedValue();
218 O << SPARCCondCodeToString((V8CC::CondCodes)CC);
223 bool SparcV8AsmPrinter::doInitialization(Module &M) {
224 Mang = new Mangler(M);
225 return false; // success
228 bool SparcV8AsmPrinter::doFinalization(Module &M) {
229 const TargetData &TD = TM.getTargetData();
231 // Print out module-level global variables here.
232 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
233 if (I->hasInitializer()) { // External global require no code
235 std::string name = Mang->getValueName(I);
236 Constant *C = I->getInitializer();
237 unsigned Size = TD.getTypeSize(C->getType());
238 unsigned Align = TD.getTypeAlignment(C->getType());
240 if (C->isNullValue() &&
241 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
242 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
243 SwitchSection(".data", I);
244 if (I->hasInternalLinkage())
245 O << "\t.local " << name << "\n";
247 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
248 << "," << (unsigned)TD.getTypeAlignment(C->getType());
250 WriteAsOperand(O, I, true, true, &M);
253 switch (I->getLinkage()) {
254 case GlobalValue::LinkOnceLinkage:
255 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
256 // Nonnull linkonce -> weak
257 O << "\t.weak " << name << "\n";
258 SwitchSection("", I);
259 O << "\t.section\t\".llvm.linkonce.d." << name
260 << "\",\"aw\",@progbits\n";
263 case GlobalValue::AppendingLinkage:
264 // FIXME: appending linkage variables should go into a section of
265 // their name or something. For now, just emit them as external.
266 case GlobalValue::ExternalLinkage:
267 // If external or appending, declare as a global symbol
268 O << "\t.globl " << name << "\n";
270 case GlobalValue::InternalLinkage:
271 if (C->isNullValue())
272 SwitchSection(".bss", I);
274 SwitchSection(".data", I);
276 case GlobalValue::GhostLinkage:
277 std::cerr << "Should not have any unmaterialized functions!\n";
281 O << "\t.align " << Align << "\n";
282 O << "\t.type " << name << ",#object\n";
283 O << "\t.size " << name << "," << Size << "\n";
284 O << name << ":\t\t\t\t! ";
285 WriteAsOperand(O, I, true, true, &M);
287 WriteAsOperand(O, C, false, false, &M);
289 EmitGlobalConstant(C);
293 AsmPrinter::doFinalization(M);
294 return false; // success