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