1 //===-- MipsAsmPrinter.cpp - Mips LLVM assembly writer --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Bruno Cardoso Lopes and is distributed under the
6 // 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 MIPS assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "mips-asm-printer"
18 #include "MipsInstrInfo.h"
19 #include "MipsTargetMachine.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Module.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/Target/TargetAsmInfo.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Support/Mangler.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/ADT/StringExtras.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/MathExtras.h"
39 STATISTIC(EmittedInsts, "Number of machine instrs printed");
42 struct VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter {
43 MipsAsmPrinter(std::ostream &O, MipsTargetMachine &TM,
44 const TargetAsmInfo *T):
45 AsmPrinter(O, TM, T) {}
47 virtual const char *getPassName() const {
48 return "Mips Assembly Printer";
51 void printOperand(const MachineInstr *MI, int opNum);
52 void printMemOperand(const MachineInstr *MI, int opNum,
53 const char *Modifier = 0);
55 bool printInstruction(const MachineInstr *MI); // autogenerated.
56 bool runOnMachineFunction(MachineFunction &F);
57 bool doInitialization(Module &M);
58 bool doFinalization(Module &M);
60 } // end of anonymous namespace
62 #include "MipsGenAsmWriter.inc"
64 /// createMipsCodePrinterPass - Returns a pass that prints the MIPS
65 /// assembly code for a MachineFunction to the given output stream,
66 /// using the given target machine description. This should work
67 /// regardless of whether the function is in SSA form.
68 FunctionPass *llvm::createMipsCodePrinterPass(std::ostream &o,
69 MipsTargetMachine &tm)
71 return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo());
74 /// runOnMachineFunction - This uses the printMachineInstruction()
75 /// method to print assembly for each instruction.
77 runOnMachineFunction(MachineFunction &MF)
79 SetupMachineFunction(MF);
81 // Print out constants referenced by the function
82 EmitConstantPool(MF.getConstantPool());
86 // What's my mangled name?
87 CurrentFnName = Mang->getValueName(MF.getFunction());
89 // Print out the label for the function.
90 const Function *F = MF.getFunction();
91 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
93 // On Mips GAS if .align #n is present, #n means the number of bits
94 // to be cleared to align. So, if we want 4 byte alignment, we must
97 // add gas ".mask" and ".fmask"
99 O << "\t.globl\t" << CurrentFnName << "\n";
100 O << "\t.ent\t" << CurrentFnName << "\n";
101 O << "\t.type\t" << CurrentFnName << ", @function\n";
102 O << CurrentFnName << ":\n";
104 // Print out code for the function.
105 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
108 // Print a label for the basic block.
109 if (I != MF.begin()) {
110 printBasicBlockLabel(I, true);
114 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
116 // Print the assembly for the instruction.
118 printInstruction(II);
123 // close function with asm directive
124 O << "\t.end\t" << CurrentFnName << "\n";
126 // We didn't modify anything.
130 void MipsAsmPrinter::
131 printOperand(const MachineInstr *MI, int opNum)
133 const MachineOperand &MO = MI->getOperand(opNum);
134 const MRegisterInfo &RI = *TM.getRegisterInfo();
137 // %hi and %lo used on mips gas to break large constants
138 if (MI->getOpcode() == Mips::LUi && !MO.isRegister()
139 && !MO.isImmediate()) {
142 } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isRegister()
143 && !MO.isImmediate()) {
148 switch (MO.getType())
150 case MachineOperand::MO_Register:
151 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
152 O << "$" << LowercaseString (RI.get(MO.getReg()).Name);
154 O << "$" << MO.getReg();
157 case MachineOperand::MO_Immediate:
158 if ((MI->getOpcode() == Mips::SLTiu) || (MI->getOpcode() == Mips::ORi) ||
159 (MI->getOpcode() == Mips::LUi) || (MI->getOpcode() == Mips::ANDi))
160 O << (unsigned int)MO.getImmedValue();
162 O << (int)MO.getImmedValue();
165 case MachineOperand::MO_MachineBasicBlock:
166 printBasicBlockLabel(MO.getMachineBasicBlock());
169 case MachineOperand::MO_GlobalAddress:
170 O << Mang->getValueName(MO.getGlobal());
173 case MachineOperand::MO_ExternalSymbol:
174 O << MO.getSymbolName();
177 case MachineOperand::MO_ConstantPoolIndex:
178 O << TAI->getPrivateGlobalPrefix() << "CPI"
179 << getFunctionNumber() << "_" << MO.getConstantPoolIndex();
183 O << "<unknown operand type>"; abort (); break;
186 if (closeP) O << ")";
189 void MipsAsmPrinter::
190 printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier)
192 // lw/sw $reg, MemOperand
194 // lw/sw $reg, imm($reg)
195 printOperand(MI, opNum);
197 printOperand(MI, opNum+1);
201 bool MipsAsmPrinter::
202 doInitialization(Module &M)
204 Mang = new Mangler(M);
205 return false; // success
208 bool MipsAsmPrinter::
209 doFinalization(Module &M)
211 const TargetData *TD = TM.getTargetData();
213 // Print out module-level global variables here.
214 for (Module::const_global_iterator I = M.global_begin(),
215 E = M.global_end(); I != E; ++I)
217 // External global require no code
218 if (I->hasInitializer()) {
220 // Check to see if this is a special global
221 // used by LLVM, if so, emit it.
222 if (EmitSpecialLLVMGlobal(I))
226 std::string name = Mang->getValueName(I);
227 Constant *C = I->getInitializer();
228 unsigned Size = TD->getTypeSize(C->getType());
229 unsigned Align = TD->getPrefTypeAlignment(C->getType());
231 if (C->isNullValue() && (I->hasLinkOnceLinkage() ||
232 I->hasInternalLinkage() || I->hasWeakLinkage()
233 /* FIXME: Verify correct */)) {
235 SwitchToDataSection(".data", I);
236 if (I->hasInternalLinkage())
237 O << "\t.local " << name << "\n";
239 O << "\t.comm " << name << ","
240 << TD->getTypeSize(C->getType())
241 << "," << Align << "\n";
245 switch (I->getLinkage())
247 case GlobalValue::LinkOnceLinkage:
248 case GlobalValue::WeakLinkage:
249 // FIXME: Verify correct for weak.
250 // Nonnull linkonce -> weak
251 O << "\t.weak " << name << "\n";
252 SwitchToDataSection("", I);
253 O << "\t.section\t\".llvm.linkonce.d." << name
254 << "\",\"aw\",@progbits\n";
256 case GlobalValue::AppendingLinkage:
257 // FIXME: appending linkage variables
258 // should go into a section of their name or
259 // something. For now, just emit them as external.
260 case GlobalValue::ExternalLinkage:
261 // If external or appending, declare as a global symbol
262 O << "\t.globl " << name << "\n";
263 case GlobalValue::InternalLinkage:
264 if (C->isNullValue())
265 SwitchToDataSection(".bss", I);
267 SwitchToDataSection(".data", I);
269 case GlobalValue::GhostLinkage:
270 cerr << "Should not have any"
271 << "unmaterialized functions!\n";
273 case GlobalValue::DLLImportLinkage:
274 cerr << "DLLImport linkage is"
275 << "not supported by this target!\n";
277 case GlobalValue::DLLExportLinkage:
278 cerr << "DLLExport linkage is"
279 << "not supported by this target!\n";
282 assert(0 && "Unknown linkage type!");
284 O << "\t.align " << Align << "\n";
285 O << "\t.type " << name << ",@object\n";
286 O << "\t.size " << name << "," << Size << "\n";
288 EmitGlobalConstant(C);
292 AsmPrinter::doFinalization(M);
293 return false; // success