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 = "\t.xword\t";
42 ZeroDirective = 0; // no .zero or .space!
45 /// We name each basic block in a Function with a unique number, so
46 /// that we can consistently refer to them later. This is cleared
47 /// at the beginning of each call to runOnMachineFunction().
49 typedef std::map<const Value *, unsigned> ValueMapTy;
50 ValueMapTy NumberForBB;
52 virtual const char *getPassName() const {
53 return "SparcV8 Assembly Printer";
56 void printOperand(const MachineInstr *MI, int opNum);
57 bool printInstruction(const MachineInstr *MI); // autogenerated.
58 bool runOnMachineFunction(MachineFunction &F);
59 bool doInitialization(Module &M);
60 bool doFinalization(Module &M);
62 } // end of anonymous namespace
64 #include "SparcV8GenAsmWriter.inc"
66 /// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8
67 /// assembly code for a MachineFunction to the given output stream,
68 /// using the given target machine description. This should work
69 /// regardless of whether the function is in SSA form.
71 FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o,
73 return new SparcV8AsmPrinter(o, tm);
76 /// runOnMachineFunction - This uses the printMachineInstruction()
77 /// method to print assembly for each instruction.
79 bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
80 SetupMachineFunction(MF);
82 // Print out constants referenced by the function
83 EmitConstantPool(MF.getConstantPool());
85 // BBNumber is used here so that a given Printer will never give two
86 // BBs the same name. (If you have a better way, please let me know!)
87 static unsigned BBNumber = 0;
90 // What's my mangled name?
91 CurrentFnName = Mang->getValueName(MF.getFunction());
93 // Print out labels for the function.
96 O << "\t.globl\t" << CurrentFnName << "\n";
97 O << "\t.type\t" << CurrentFnName << ", #function\n";
98 O << CurrentFnName << ":\n";
100 // Number each basic block so that we can consistently refer to them
101 // in PC-relative references.
103 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
105 NumberForBB[I->getBasicBlock()] = BBNumber++;
108 // Print out code for the function.
109 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
111 // Print a label for the basic block.
112 O << ".LBB" << Mang->getValueName(MF.getFunction ())
113 << "_" << I->getNumber () << ":\t! "
114 << I->getBasicBlock ()->getName () << "\n";
115 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
117 // Print the assembly for the instruction.
119 printInstruction(II);
124 // We didn't modify anything.
128 void SparcV8AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
129 const MachineOperand &MO = MI->getOperand (opNum);
130 const MRegisterInfo &RI = *TM.getRegisterInfo();
131 bool CloseParen = false;
132 if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
135 } else if (MI->getOpcode() ==V8::ORri &&!MO.isRegister() &&!MO.isImmediate())
140 switch (MO.getType()) {
141 case MachineOperand::MO_VirtualRegister:
142 if (Value *V = MO.getVRegValueOrNull()) {
143 O << "<" << V->getName() << ">";
147 case MachineOperand::MO_MachineRegister:
148 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
149 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
151 O << "%reg" << MO.getReg();
154 case MachineOperand::MO_SignExtendedImmed:
155 case MachineOperand::MO_UnextendedImmed:
156 O << (int)MO.getImmedValue();
158 case MachineOperand::MO_MachineBasicBlock: {
159 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
160 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
161 << "_" << MBBOp->getNumber () << "\t! "
162 << MBBOp->getBasicBlock ()->getName ();
165 case MachineOperand::MO_PCRelativeDisp:
166 std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs";
169 case MachineOperand::MO_GlobalAddress:
170 O << Mang->getValueName(MO.getGlobal());
172 case MachineOperand::MO_ExternalSymbol:
173 O << MO.getSymbolName();
175 case MachineOperand::MO_ConstantPoolIndex:
176 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
177 << MO.getConstantPoolIndex();
180 O << "<unknown operand type>"; abort (); break;
182 if (CloseParen) O << ")";
185 bool SparcV8AsmPrinter::doInitialization(Module &M) {
186 Mang = new Mangler(M);
187 return false; // success
190 bool SparcV8AsmPrinter::doFinalization(Module &M) {
191 const TargetData &TD = TM.getTargetData();
193 // Print out module-level global variables here.
194 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
195 if (I->hasInitializer()) { // External global require no code
197 std::string name = Mang->getValueName(I);
198 Constant *C = I->getInitializer();
199 unsigned Size = TD.getTypeSize(C->getType());
200 unsigned Align = TD.getTypeAlignment(C->getType());
202 if (C->isNullValue() &&
203 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
204 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
205 SwitchSection(".data", I);
206 if (I->hasInternalLinkage())
207 O << "\t.local " << name << "\n";
209 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
210 << "," << (unsigned)TD.getTypeAlignment(C->getType());
212 WriteAsOperand(O, I, true, true, &M);
215 switch (I->getLinkage()) {
216 case GlobalValue::LinkOnceLinkage:
217 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
218 // Nonnull linkonce -> weak
219 O << "\t.weak " << name << "\n";
220 SwitchSection("", I);
221 O << "\t.section\t\".llvm.linkonce.d." << name
222 << "\",\"aw\",@progbits\n";
225 case GlobalValue::AppendingLinkage:
226 // FIXME: appending linkage variables should go into a section of
227 // their name or something. For now, just emit them as external.
228 case GlobalValue::ExternalLinkage:
229 // If external or appending, declare as a global symbol
230 O << "\t.globl " << name << "\n";
232 case GlobalValue::InternalLinkage:
233 if (C->isNullValue())
234 SwitchSection(".bss", I);
236 SwitchSection(".data", I);
238 case GlobalValue::GhostLinkage:
239 std::cerr << "Should not have any unmaterialized functions!\n";
243 O << "\t.align " << Align << "\n";
244 O << "\t.type " << name << ",#object\n";
245 O << "\t.size " << name << "," << Size << "\n";
246 O << name << ":\t\t\t\t! ";
247 WriteAsOperand(O, I, true, true, &M);
249 WriteAsOperand(O, C, false, false, &M);
251 EmitGlobalConstant(C);
255 AsmPrinter::doFinalization(M);
256 return false; // success