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 "MipsMachineFunction.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Module.h"
24 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/Target/TargetAsmInfo.h"
30 #include "llvm/Target/TargetData.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Support/Mangler.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/ADT/SetVector.h"
35 #include "llvm/ADT/StringExtras.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/MathExtras.h"
43 STATISTIC(EmittedInsts, "Number of machine instrs printed");
46 struct VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter {
47 MipsAsmPrinter(std::ostream &O, MipsTargetMachine &TM,
48 const TargetAsmInfo *T):
49 AsmPrinter(O, TM, T) {}
51 virtual const char *getPassName() const {
52 return "Mips Assembly Printer";
55 enum SetDirectiveFlags {
56 REORDER, // enables instruction reordering.
57 NOREORDER, // disables instruction reordering.
58 MACRO, // enables GAS macros.
59 NOMACRO // disables GAS macros.
62 void printOperand(const MachineInstr *MI, int opNum);
63 void printMemOperand(const MachineInstr *MI, int opNum,
64 const char *Modifier = 0);
66 unsigned int getSavedRegsBitmask(bool isFloat, MachineFunction &MF);
67 void printHex32(unsigned int Value);
69 void emitFunctionStart(MachineFunction &MF);
70 void emitFunctionEnd();
71 void emitFrameDirective(MachineFunction &MF);
72 void emitMaskDirective(MachineFunction &MF);
73 void emitFMaskDirective(MachineFunction &MF);
74 void emitSetDirective(SetDirectiveFlags Flag);
76 bool printInstruction(const MachineInstr *MI); // autogenerated.
77 bool runOnMachineFunction(MachineFunction &F);
78 bool doInitialization(Module &M);
79 bool doFinalization(Module &M);
81 } // end of anonymous namespace
83 #include "MipsGenAsmWriter.inc"
85 /// createMipsCodePrinterPass - Returns a pass that prints the MIPS
86 /// assembly code for a MachineFunction to the given output stream,
87 /// using the given target machine description. This should work
88 /// regardless of whether the function is in SSA form.
89 FunctionPass *llvm::createMipsCodePrinterPass(std::ostream &o,
90 MipsTargetMachine &tm)
92 return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo());
95 //===----------------------------------------------------------------------===//
97 // Mips Asm Directives
99 // -- Frame directive "frame Stackpointer, Stacksize, RARegister"
100 // Describe the stack frame.
102 // -- Mask directives "(f)mask bitmask, offset"
103 // Tells the assembler which registers are saved and where.
104 // bitmask - contain a little endian bitset indicating which registers are
105 // saved on function prologue (e.g. with a 0x80000000 mask, the
106 // assembler knows the register 31 (RA) is saved at prologue.
107 // offset - the position before stack pointer subtraction indicating where
108 // the first saved register on prologue is located. (e.g. with a
110 // Consider the following function prologue:
113 // .mask 0xc0000000,-8
114 // addiu $sp, $sp, -48
118 // With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
119 // 30 (FP) are saved at prologue. As the save order on prologue is from
120 // left to right, RA is saved first. A -8 offset means that after the
121 // stack pointer subtration, the first register in the mask (RA) will be
122 // saved at address 48-8=40.
124 //===----------------------------------------------------------------------===//
126 /// Mask directive for GPR
127 void MipsAsmPrinter::
128 emitMaskDirective(MachineFunction &MF)
130 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
132 int StackSize = MF.getFrameInfo()->getStackSize();
133 int Offset = (!MipsFI->getTopSavedRegOffset()) ? 0 :
134 (-(StackSize-MipsFI->getTopSavedRegOffset()));
137 DOUT << "--> emitMaskDirective" << "\n";
138 DOUT << "StackSize : " << StackSize << "\n";
139 DOUT << "getTopSavedReg : " << MipsFI->getTopSavedRegOffset() << "\n";
140 DOUT << "Offset : " << Offset << "\n\n";
143 unsigned int Bitmask = getSavedRegsBitmask(false, MF);
146 O << "," << Offset << "\n";
149 /// TODO: Mask Directive for Float Point
150 void MipsAsmPrinter::
151 emitFMaskDirective(MachineFunction &MF)
153 unsigned int Bitmask = getSavedRegsBitmask(true, MF);
161 void MipsAsmPrinter::
162 emitFrameDirective(MachineFunction &MF)
164 const MRegisterInfo &RI = *TM.getRegisterInfo();
166 unsigned stackReg = RI.getFrameRegister(MF);
167 unsigned returnReg = RI.getRARegister();
168 unsigned stackSize = MF.getFrameInfo()->getStackSize();
171 O << "\t.frame\t" << "$" << LowercaseString(RI.get(stackReg).Name)
172 << "," << stackSize << ","
173 << "$" << LowercaseString(RI.get(returnReg).Name)
177 /// Emit Set directives.
178 void MipsAsmPrinter::
179 emitSetDirective(SetDirectiveFlags Flag)
183 case REORDER: O << "reorder" << "\n"; break;
184 case NOREORDER: O << "noreorder" << "\n"; break;
185 case MACRO: O << "macro" << "\n"; break;
186 case NOMACRO: O << "nomacro" << "\n"; break;
191 // Create a bitmask with all callee saved registers for CPU
192 // or Float Point registers. For CPU registers consider RA,
193 // GP and FP for saving if necessary.
194 unsigned int MipsAsmPrinter::
195 getSavedRegsBitmask(bool isFloat, MachineFunction &MF)
197 const MRegisterInfo &RI = *TM.getRegisterInfo();
199 // Float Point Registers, TODO
204 unsigned int Bitmask = 0;
206 MachineFrameInfo *MFI = MF.getFrameInfo();
207 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
208 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
209 Bitmask |= (1 << MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg()));
212 Bitmask |= (1 << MipsRegisterInfo::getRegisterNumbering(RI.getFrameRegister(MF)));
214 if (MF.getFrameInfo()->hasCalls())
215 Bitmask |= (1 << MipsRegisterInfo::getRegisterNumbering(RI.getRARegister()));
220 // Print a 32 bit hex number with all numbers.
221 void MipsAsmPrinter::
222 printHex32(unsigned int Value)
224 O << "0x" << std::hex;
225 for (int i = 7; i >= 0; i--)
226 O << std::hex << ( (Value & (0xF << (i*4))) >> (i*4) );
230 /// Emit the directives used by GAS on the start of functions
231 void MipsAsmPrinter::
232 emitFunctionStart(MachineFunction &MF)
234 // Print out the label for the function.
235 const Function *F = MF.getFunction();
236 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
238 // On Mips GAS, if .align #n is present, #n means the number of bits
239 // to be cleared. So, if we want 4 byte alignment, we must have .align 2
242 O << "\t.globl\t" << CurrentFnName << "\n";
243 O << "\t.ent\t" << CurrentFnName << "\n";
244 O << "\t.type\t" << CurrentFnName << ", @function\n";
245 O << CurrentFnName << ":\n";
247 emitFrameDirective(MF);
248 emitMaskDirective(MF);
249 emitFMaskDirective(MF);
250 emitSetDirective(NOREORDER);
251 emitSetDirective(NOMACRO);
255 /// Emit the directives used by GAS on the end of functions
256 void MipsAsmPrinter::
258 emitSetDirective(MACRO);
259 emitSetDirective(REORDER);
260 O << "\t.end\t" << CurrentFnName << "\n";
263 /// runOnMachineFunction - This uses the printMachineInstruction()
264 /// method to print assembly for each instruction.
265 bool MipsAsmPrinter::
266 runOnMachineFunction(MachineFunction &MF)
268 SetupMachineFunction(MF);
270 // Print out constants referenced by the function
271 EmitConstantPool(MF.getConstantPool());
275 // What's my mangled name?
276 CurrentFnName = Mang->getValueName(MF.getFunction());
278 // Emit the function start directives
279 emitFunctionStart(MF);
281 // Print out code for the function.
282 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
285 // Print a label for the basic block.
286 if (I != MF.begin()) {
287 printBasicBlockLabel(I, true);
291 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
293 // Print the assembly for the instruction.
295 printInstruction(II);
300 // Emit function end directives
303 // We didn't modify anything.
307 void MipsAsmPrinter::
308 printOperand(const MachineInstr *MI, int opNum)
310 const MachineOperand &MO = MI->getOperand(opNum);
311 const MRegisterInfo &RI = *TM.getRegisterInfo();
314 // %hi and %lo used on mips gas to break large constants
315 if (MI->getOpcode() == Mips::LUi && !MO.isRegister()
316 && !MO.isImmediate()) {
319 } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isRegister()
320 && !MO.isImmediate()) {
325 switch (MO.getType())
327 case MachineOperand::MO_Register:
328 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
329 O << "$" << LowercaseString (RI.get(MO.getReg()).Name);
331 O << "$" << MO.getReg();
334 case MachineOperand::MO_Immediate:
335 if ((MI->getOpcode() == Mips::SLTiu) || (MI->getOpcode() == Mips::ORi) ||
336 (MI->getOpcode() == Mips::LUi) || (MI->getOpcode() == Mips::ANDi))
337 O << (unsigned short int)MO.getImmedValue();
339 O << (short int)MO.getImmedValue();
342 case MachineOperand::MO_MachineBasicBlock:
343 printBasicBlockLabel(MO.getMachineBasicBlock());
346 case MachineOperand::MO_GlobalAddress:
347 O << Mang->getValueName(MO.getGlobal());
350 case MachineOperand::MO_ExternalSymbol:
351 O << MO.getSymbolName();
354 case MachineOperand::MO_ConstantPoolIndex:
355 O << TAI->getPrivateGlobalPrefix() << "CPI"
356 << getFunctionNumber() << "_" << MO.getConstantPoolIndex();
360 O << "<unknown operand type>"; abort (); break;
363 if (closeP) O << ")";
366 void MipsAsmPrinter::
367 printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier)
369 // lw/sw $reg, MemOperand
371 // lw/sw $reg, imm($reg)
372 printOperand(MI, opNum);
374 printOperand(MI, opNum+1);
378 bool MipsAsmPrinter::
379 doInitialization(Module &M)
381 Mang = new Mangler(M);
382 return false; // success
385 bool MipsAsmPrinter::
386 doFinalization(Module &M)
388 const TargetData *TD = TM.getTargetData();
390 // Print out module-level global variables here.
391 for (Module::const_global_iterator I = M.global_begin(),
392 E = M.global_end(); I != E; ++I)
394 // External global require no code
395 if (I->hasInitializer()) {
397 // Check to see if this is a special global
398 // used by LLVM, if so, emit it.
399 if (EmitSpecialLLVMGlobal(I))
403 std::string name = Mang->getValueName(I);
404 Constant *C = I->getInitializer();
405 unsigned Size = TD->getTypeSize(C->getType());
406 unsigned Align = TD->getPrefTypeAlignment(C->getType());
408 if (C->isNullValue() && (I->hasLinkOnceLinkage() ||
409 I->hasInternalLinkage() || I->hasWeakLinkage()
410 /* FIXME: Verify correct */)) {
412 SwitchToDataSection(".data", I);
413 if (I->hasInternalLinkage())
414 O << "\t.local " << name << "\n";
416 O << "\t.comm " << name << ","
417 << TD->getTypeSize(C->getType())
418 << "," << Align << "\n";
422 switch (I->getLinkage())
424 case GlobalValue::LinkOnceLinkage:
425 case GlobalValue::WeakLinkage:
426 // FIXME: Verify correct for weak.
427 // Nonnull linkonce -> weak
428 O << "\t.weak " << name << "\n";
429 SwitchToDataSection("", I);
430 O << "\t.section\t\".llvm.linkonce.d." << name
431 << "\",\"aw\",@progbits\n";
433 case GlobalValue::AppendingLinkage:
434 // FIXME: appending linkage variables
435 // should go into a section of their name or
436 // something. For now, just emit them as external.
437 case GlobalValue::ExternalLinkage:
438 // If external or appending, declare as a global symbol
439 O << "\t.globl " << name << "\n";
440 case GlobalValue::InternalLinkage:
441 if (C->isNullValue())
442 SwitchToDataSection(".bss", I);
444 SwitchToDataSection(".data", I);
446 case GlobalValue::GhostLinkage:
447 cerr << "Should not have any"
448 << "unmaterialized functions!\n";
450 case GlobalValue::DLLImportLinkage:
451 cerr << "DLLImport linkage is"
452 << "not supported by this target!\n";
454 case GlobalValue::DLLExportLinkage:
455 cerr << "DLLExport linkage is"
456 << "not supported by this target!\n";
459 assert(0 && "Unknown linkage type!");
462 O << "\t.align " << Align << "\n";
463 O << "\t.type " << name << ",@object\n";
464 O << "\t.size " << name << "," << Size << "\n";
466 EmitGlobalConstant(C);
470 return AsmPrinter::doFinalization(M);