d269153ef0e76eefbfcfe2374245635fba7224a8
[oota-llvm.git] / lib / Target / Mips / AsmPrinter / MipsAsmPrinter.cpp
1 //===-- MipsAsmPrinter.cpp - Mips LLVM assembly writer --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to GAS-format MIPS assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "mips-asm-printer"
16 #include "Mips.h"
17 #include "MipsSubtarget.h"
18 #include "MipsInstrInfo.h"
19 #include "MipsTargetMachine.h"
20 #include "MipsMachineFunction.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/Target/Mangler.h"
30 #include "llvm/Target/TargetData.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h" 
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/Target/TargetOptions.h"
34 #include "llvm/Target/TargetRegistry.h"
35 #include "llvm/ADT/SmallString.h"
36 #include "llvm/ADT/StringExtras.h"
37 #include "llvm/ADT/Twine.h"
38 #include "llvm/Support/raw_ostream.h"
39 using namespace llvm;
40
41 namespace {
42   class MipsAsmPrinter : public AsmPrinter {
43     const MipsSubtarget *Subtarget;
44   public:
45     explicit MipsAsmPrinter(TargetMachine &TM,  MCStreamer &Streamer)
46       : AsmPrinter(TM, Streamer) {
47       Subtarget = &TM.getSubtarget<MipsSubtarget>();
48     }
49
50     virtual const char *getPassName() const {
51       return "Mips Assembly Printer";
52     }
53
54     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 
55                          unsigned AsmVariant, const char *ExtraCode,
56                          raw_ostream &O);
57     void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
58     void printUnsignedImm(const MachineInstr *MI, int opNum, raw_ostream &O);
59     void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O, 
60                          const char *Modifier = 0);
61     void printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O, 
62                          const char *Modifier = 0);
63     void printSavedRegsBitmask(raw_ostream &O);
64     void printHex32(unsigned int Value, raw_ostream &O);
65
66     const char *getCurrentABIString() const;
67     void emitFrameDirective();
68
69     void printInstruction(const MachineInstr *MI, raw_ostream &O); // autogen'd.
70     void EmitInstruction(const MachineInstr *MI) {
71       SmallString<128> Str;
72       raw_svector_ostream OS(Str);
73       printInstruction(MI, OS);
74       OutStreamer.EmitRawText(OS.str());
75     }
76     virtual void EmitFunctionBodyStart();
77     virtual void EmitFunctionBodyEnd();
78     static const char *getRegisterName(unsigned RegNo);
79
80     virtual void EmitFunctionEntryLabel();
81     void EmitStartOfAsmFile(Module &M);
82   };
83 } // end of anonymous namespace
84
85 #include "MipsGenAsmWriter.inc"
86
87 //===----------------------------------------------------------------------===//
88 //
89 //  Mips Asm Directives
90 //
91 //  -- Frame directive "frame Stackpointer, Stacksize, RARegister"
92 //  Describe the stack frame.
93 //
94 //  -- Mask directives "(f)mask  bitmask, offset" 
95 //  Tells the assembler which registers are saved and where.
96 //  bitmask - contain a little endian bitset indicating which registers are 
97 //            saved on function prologue (e.g. with a 0x80000000 mask, the 
98 //            assembler knows the register 31 (RA) is saved at prologue.
99 //  offset  - the position before stack pointer subtraction indicating where 
100 //            the first saved register on prologue is located. (e.g. with a
101 //
102 //  Consider the following function prologue:
103 //
104 //    .frame  $fp,48,$ra
105 //    .mask   0xc0000000,-8
106 //       addiu $sp, $sp, -48
107 //       sw $ra, 40($sp)
108 //       sw $fp, 36($sp)
109 //
110 //    With a 0xc0000000 mask, the assembler knows the register 31 (RA) and 
111 //    30 (FP) are saved at prologue. As the save order on prologue is from 
112 //    left to right, RA is saved first. A -8 offset means that after the 
113 //    stack pointer subtration, the first register in the mask (RA) will be
114 //    saved at address 48-8=40.
115 //
116 //===----------------------------------------------------------------------===//
117
118 //===----------------------------------------------------------------------===//
119 // Mask directives
120 //===----------------------------------------------------------------------===//
121
122 // Create a bitmask with all callee saved registers for CPU or Floating Point 
123 // registers. For CPU registers consider RA, GP and FP for saving if necessary.
124 void MipsAsmPrinter::printSavedRegsBitmask(raw_ostream &O) {
125   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
126   const MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
127              
128   // CPU and FPU Saved Registers Bitmasks
129   unsigned int CPUBitmask = 0;
130   unsigned int FPUBitmask = 0;
131
132   // Set the CPU and FPU Bitmasks
133   const MachineFrameInfo *MFI = MF->getFrameInfo();
134   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
135   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
136     unsigned RegNum = MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg());
137     if (CSI[i].getRegClass() == Mips::CPURegsRegisterClass)
138       CPUBitmask |= (1 << RegNum);
139     else
140       FPUBitmask |= (1 << RegNum);
141   }
142
143   // Return Address and Frame registers must also be set in CPUBitmask.
144   if (RI.hasFP(*MF)) 
145     CPUBitmask |= (1 << MipsRegisterInfo::
146                 getRegisterNumbering(RI.getFrameRegister(*MF)));
147   
148   if (MFI->hasCalls()) 
149     CPUBitmask |= (1 << MipsRegisterInfo::
150                 getRegisterNumbering(RI.getRARegister()));
151
152   // Print CPUBitmask
153   O << "\t.mask \t"; printHex32(CPUBitmask, O);
154   O << ',' << MipsFI->getCPUTopSavedRegOff() << '\n';
155
156   // Print FPUBitmask
157   O << "\t.fmask\t"; printHex32(FPUBitmask, O); O << ","
158     << MipsFI->getFPUTopSavedRegOff() << '\n';
159 }
160
161 // Print a 32 bit hex number with all numbers.
162 void MipsAsmPrinter::printHex32(unsigned Value, raw_ostream &O) {
163   O << "0x";
164   for (int i = 7; i >= 0; i--) 
165     O << utohexstr((Value & (0xF << (i*4))) >> (i*4));
166 }
167
168 //===----------------------------------------------------------------------===//
169 // Frame and Set directives
170 //===----------------------------------------------------------------------===//
171
172 /// Frame Directive
173 void MipsAsmPrinter::emitFrameDirective() {
174   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
175
176   unsigned stackReg  = RI.getFrameRegister(*MF);
177   unsigned returnReg = RI.getRARegister();
178   unsigned stackSize = MF->getFrameInfo()->getStackSize();
179
180   OutStreamer.EmitRawText("\t.frame\t$" +
181                           Twine(LowercaseString(getRegisterName(stackReg))) +
182                           "," + Twine(stackSize) + ",$" +
183                           Twine(LowercaseString(getRegisterName(returnReg))));
184 }
185
186 /// Emit Set directives.
187 const char *MipsAsmPrinter::getCurrentABIString() const { 
188   switch (Subtarget->getTargetABI()) {
189   case MipsSubtarget::O32:  return "abi32";  
190   case MipsSubtarget::O64:  return "abiO64";
191   case MipsSubtarget::N32:  return "abiN32";
192   case MipsSubtarget::N64:  return "abi64";
193   case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
194   default: break;
195   }
196
197   llvm_unreachable("Unknown Mips ABI");
198   return NULL;
199 }  
200
201 void MipsAsmPrinter::EmitFunctionEntryLabel() {
202   OutStreamer.EmitRawText("\t.ent\t" + Twine(CurrentFnSym->getName()));
203   OutStreamer.EmitLabel(CurrentFnSym);
204 }
205
206 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
207 /// the first basic block in the function.
208 void MipsAsmPrinter::EmitFunctionBodyStart() {
209   emitFrameDirective();
210   
211   SmallString<128> Str;
212   raw_svector_ostream OS(Str);
213   printSavedRegsBitmask(OS);
214   OutStreamer.EmitRawText(OS.str());
215 }
216
217 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
218 /// the last basic block in the function.
219 void MipsAsmPrinter::EmitFunctionBodyEnd() {
220   // There are instruction for this macros, but they must
221   // always be at the function end, and we can't emit and
222   // break with BB logic. 
223   OutStreamer.EmitRawText(StringRef("\t.set\tmacro"));
224   OutStreamer.EmitRawText(StringRef("\t.set\treorder"));
225   OutStreamer.EmitRawText("\t.end\t" + Twine(CurrentFnSym->getName()));
226 }
227
228
229 // Print out an operand for an inline asm expression.
230 bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 
231                                      unsigned AsmVariant,const char *ExtraCode,
232                                      raw_ostream &O) {
233   // Does this asm operand have a single letter operand modifier?
234   if (ExtraCode && ExtraCode[0]) 
235     return true; // Unknown modifier.
236
237   printOperand(MI, OpNo, O);
238   return false;
239 }
240
241 void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
242                                   raw_ostream &O) {
243   const MachineOperand &MO = MI->getOperand(opNum);
244   bool closeP = false;
245
246   if (MO.getTargetFlags())
247     closeP = true;
248
249   switch(MO.getTargetFlags()) {
250   case MipsII::MO_GPREL:    O << "%gp_rel("; break;
251   case MipsII::MO_GOT_CALL: O << "%call16("; break;
252   case MipsII::MO_GOT:
253     if (MI->getOpcode() == Mips::LW)
254       O << "%got(";
255     else
256       O << "%lo(";
257     break;
258   case MipsII::MO_ABS_HILO:
259     if (MI->getOpcode() == Mips::LUi)
260       O << "%hi(";
261     else
262       O << "%lo(";     
263     break;
264   }
265
266   switch (MO.getType()) {
267     case MachineOperand::MO_Register:
268       O << '$' << LowercaseString(getRegisterName(MO.getReg()));
269       break;
270
271     case MachineOperand::MO_Immediate:
272       O << (short int)MO.getImm();
273       break;
274
275     case MachineOperand::MO_MachineBasicBlock:
276       O << *MO.getMBB()->getSymbol();
277       return;
278
279     case MachineOperand::MO_GlobalAddress:
280       O << *Mang->getSymbol(MO.getGlobal());
281       break;
282
283     case MachineOperand::MO_ExternalSymbol:
284       O << *GetExternalSymbolSymbol(MO.getSymbolName());
285       break;
286
287     case MachineOperand::MO_JumpTableIndex:
288       O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
289         << '_' << MO.getIndex();
290       break;
291
292     case MachineOperand::MO_ConstantPoolIndex:
293       O << MAI->getPrivateGlobalPrefix() << "CPI"
294         << getFunctionNumber() << "_" << MO.getIndex();
295       if (MO.getOffset())
296         O << "+" << MO.getOffset();
297       break;
298   
299     default:
300       llvm_unreachable("<unknown operand type>");
301   }
302
303   if (closeP) O << ")";
304 }
305
306 void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
307                                       raw_ostream &O) {
308   const MachineOperand &MO = MI->getOperand(opNum);
309   if (MO.isImm())
310     O << (unsigned short int)MO.getImm();
311   else 
312     printOperand(MI, opNum, O);
313 }
314
315 void MipsAsmPrinter::
316 printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
317                 const char *Modifier) {
318   // when using stack locations for not load/store instructions
319   // print the same way as all normal 3 operand instructions.
320   if (Modifier && !strcmp(Modifier, "stackloc")) {
321     printOperand(MI, opNum+1, O);
322     O << ", ";
323     printOperand(MI, opNum, O);
324     return;
325   }
326
327   // Load/Store memory operands -- imm($reg) 
328   // If PIC target the target is loaded as the 
329   // pattern lw $25,%call16($28)
330   printOperand(MI, opNum, O);
331   O << "(";
332   printOperand(MI, opNum+1, O);
333   O << ")";
334 }
335
336 void MipsAsmPrinter::
337 printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
338                 const char *Modifier) {
339   const MachineOperand& MO = MI->getOperand(opNum);
340   O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm()); 
341 }
342
343 void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
344   // FIXME: Use SwitchSection.
345   
346   // Tell the assembler which ABI we are using
347   OutStreamer.EmitRawText("\t.section .mdebug." + Twine(getCurrentABIString()));
348
349   // TODO: handle O64 ABI
350   if (Subtarget->isABI_EABI()) {
351     if (Subtarget->isGP32bit())
352       OutStreamer.EmitRawText(StringRef("\t.section .gcc_compiled_long32"));
353     else
354       OutStreamer.EmitRawText(StringRef("\t.section .gcc_compiled_long64"));
355   }
356
357   // return to previous section
358   OutStreamer.EmitRawText(StringRef("\t.previous")); 
359 }
360
361 // Force static initialization.
362 extern "C" void LLVMInitializeMipsAsmPrinter() { 
363   RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
364   RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
365 }