8ae05b75e919d9fe170423a36024738b4d5fdd96
[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 Reg = CSI[i].getReg();
137     unsigned RegNum = MipsRegisterInfo::getRegisterNumbering(Reg);
138     if (Mips::CPURegsRegisterClass->contains(Reg))
139       CPUBitmask |= (1 << RegNum);
140     else
141       FPUBitmask |= (1 << RegNum);
142   }
143
144   // Return Address and Frame registers must also be set in CPUBitmask.
145   if (RI.hasFP(*MF)) 
146     CPUBitmask |= (1 << MipsRegisterInfo::
147                 getRegisterNumbering(RI.getFrameRegister(*MF)));
148   
149   if (MFI->adjustsStack()) 
150     CPUBitmask |= (1 << MipsRegisterInfo::
151                 getRegisterNumbering(RI.getRARegister()));
152
153   // Print CPUBitmask
154   O << "\t.mask \t"; printHex32(CPUBitmask, O);
155   O << ',' << MipsFI->getCPUTopSavedRegOff() << '\n';
156
157   // Print FPUBitmask
158   O << "\t.fmask\t"; printHex32(FPUBitmask, O); O << ","
159     << MipsFI->getFPUTopSavedRegOff() << '\n';
160 }
161
162 // Print a 32 bit hex number with all numbers.
163 void MipsAsmPrinter::printHex32(unsigned Value, raw_ostream &O) {
164   O << "0x";
165   for (int i = 7; i >= 0; i--) 
166     O << utohexstr((Value & (0xF << (i*4))) >> (i*4));
167 }
168
169 //===----------------------------------------------------------------------===//
170 // Frame and Set directives
171 //===----------------------------------------------------------------------===//
172
173 /// Frame Directive
174 void MipsAsmPrinter::emitFrameDirective() {
175   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
176
177   unsigned stackReg  = RI.getFrameRegister(*MF);
178   unsigned returnReg = RI.getRARegister();
179   unsigned stackSize = MF->getFrameInfo()->getStackSize();
180
181   OutStreamer.EmitRawText("\t.frame\t$" +
182                           Twine(LowercaseString(getRegisterName(stackReg))) +
183                           "," + Twine(stackSize) + ",$" +
184                           Twine(LowercaseString(getRegisterName(returnReg))));
185 }
186
187 /// Emit Set directives.
188 const char *MipsAsmPrinter::getCurrentABIString() const { 
189   switch (Subtarget->getTargetABI()) {
190   case MipsSubtarget::O32:  return "abi32";  
191   case MipsSubtarget::O64:  return "abiO64";
192   case MipsSubtarget::N32:  return "abiN32";
193   case MipsSubtarget::N64:  return "abi64";
194   case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
195   default: break;
196   }
197
198   llvm_unreachable("Unknown Mips ABI");
199   return NULL;
200 }  
201
202 void MipsAsmPrinter::EmitFunctionEntryLabel() {
203   OutStreamer.EmitRawText("\t.ent\t" + Twine(CurrentFnSym->getName()));
204   OutStreamer.EmitLabel(CurrentFnSym);
205 }
206
207 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
208 /// the first basic block in the function.
209 void MipsAsmPrinter::EmitFunctionBodyStart() {
210   emitFrameDirective();
211   
212   SmallString<128> Str;
213   raw_svector_ostream OS(Str);
214   printSavedRegsBitmask(OS);
215   OutStreamer.EmitRawText(OS.str());
216 }
217
218 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
219 /// the last basic block in the function.
220 void MipsAsmPrinter::EmitFunctionBodyEnd() {
221   // There are instruction for this macros, but they must
222   // always be at the function end, and we can't emit and
223   // break with BB logic. 
224   OutStreamer.EmitRawText(StringRef("\t.set\tmacro"));
225   OutStreamer.EmitRawText(StringRef("\t.set\treorder"));
226   OutStreamer.EmitRawText("\t.end\t" + Twine(CurrentFnSym->getName()));
227 }
228
229
230 // Print out an operand for an inline asm expression.
231 bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 
232                                      unsigned AsmVariant,const char *ExtraCode,
233                                      raw_ostream &O) {
234   // Does this asm operand have a single letter operand modifier?
235   if (ExtraCode && ExtraCode[0]) 
236     return true; // Unknown modifier.
237
238   printOperand(MI, OpNo, O);
239   return false;
240 }
241
242 void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
243                                   raw_ostream &O) {
244   const MachineOperand &MO = MI->getOperand(opNum);
245   bool closeP = false;
246
247   if (MO.getTargetFlags())
248     closeP = true;
249
250   switch(MO.getTargetFlags()) {
251   case MipsII::MO_GPREL:    O << "%gp_rel("; break;
252   case MipsII::MO_GOT_CALL: O << "%call16("; break;
253   case MipsII::MO_GOT:
254     if (MI->getOpcode() == Mips::LW)
255       O << "%got(";
256     else
257       O << "%lo(";
258     break;
259   case MipsII::MO_ABS_HILO:
260     if (MI->getOpcode() == Mips::LUi)
261       O << "%hi(";
262     else
263       O << "%lo(";     
264     break;
265   }
266
267   switch (MO.getType()) {
268     case MachineOperand::MO_Register:
269       O << '$' << LowercaseString(getRegisterName(MO.getReg()));
270       break;
271
272     case MachineOperand::MO_Immediate:
273       O << (short int)MO.getImm();
274       break;
275
276     case MachineOperand::MO_MachineBasicBlock:
277       O << *MO.getMBB()->getSymbol();
278       return;
279
280     case MachineOperand::MO_GlobalAddress:
281       O << *Mang->getSymbol(MO.getGlobal());
282       break;
283
284     case MachineOperand::MO_ExternalSymbol:
285       O << *GetExternalSymbolSymbol(MO.getSymbolName());
286       break;
287
288     case MachineOperand::MO_JumpTableIndex:
289       O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
290         << '_' << MO.getIndex();
291       break;
292
293     case MachineOperand::MO_ConstantPoolIndex:
294       O << MAI->getPrivateGlobalPrefix() << "CPI"
295         << getFunctionNumber() << "_" << MO.getIndex();
296       if (MO.getOffset())
297         O << "+" << MO.getOffset();
298       break;
299   
300     default:
301       llvm_unreachable("<unknown operand type>");
302   }
303
304   if (closeP) O << ")";
305 }
306
307 void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
308                                       raw_ostream &O) {
309   const MachineOperand &MO = MI->getOperand(opNum);
310   if (MO.isImm())
311     O << (unsigned short int)MO.getImm();
312   else 
313     printOperand(MI, opNum, O);
314 }
315
316 void MipsAsmPrinter::
317 printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
318                 const char *Modifier) {
319   // when using stack locations for not load/store instructions
320   // print the same way as all normal 3 operand instructions.
321   if (Modifier && !strcmp(Modifier, "stackloc")) {
322     printOperand(MI, opNum+1, O);
323     O << ", ";
324     printOperand(MI, opNum, O);
325     return;
326   }
327
328   // Load/Store memory operands -- imm($reg) 
329   // If PIC target the target is loaded as the 
330   // pattern lw $25,%call16($28)
331   printOperand(MI, opNum, O);
332   O << "(";
333   printOperand(MI, opNum+1, O);
334   O << ")";
335 }
336
337 void MipsAsmPrinter::
338 printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
339                 const char *Modifier) {
340   const MachineOperand& MO = MI->getOperand(opNum);
341   O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm()); 
342 }
343
344 void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
345   // FIXME: Use SwitchSection.
346   
347   // Tell the assembler which ABI we are using
348   OutStreamer.EmitRawText("\t.section .mdebug." + Twine(getCurrentABIString()));
349
350   // TODO: handle O64 ABI
351   if (Subtarget->isABI_EABI()) {
352     if (Subtarget->isGP32bit())
353       OutStreamer.EmitRawText(StringRef("\t.section .gcc_compiled_long32"));
354     else
355       OutStreamer.EmitRawText(StringRef("\t.section .gcc_compiled_long64"));
356   }
357
358   // return to previous section
359   OutStreamer.EmitRawText(StringRef("\t.previous")); 
360 }
361
362 // Force static initialization.
363 extern "C" void LLVMInitializeMipsAsmPrinter() { 
364   RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
365   RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
366 }