Implement the missing bits corresponding to .mips_hack_elf_flags.
[oota-llvm.git] / lib / Target / Mips / MipsAsmPrinter.cpp
1 //===-- MipsAsmPrinter.cpp - Mips LLVM Assembly Printer -------------------===//
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 "InstPrinter/MipsInstPrinter.h"
17 #include "MCTargetDesc/MipsBaseInfo.h"
18 #include "Mips.h"
19 #include "MipsAsmPrinter.h"
20 #include "MipsInstrInfo.h"
21 #include "MipsMCInstLower.h"
22 #include "MipsTargetStreamer.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/CodeGen/MachineMemOperand.h"
31 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/DataLayout.h"
33 #include "llvm/IR/InlineAsm.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/Mangler.h"
36 #include "llvm/MC/MCAsmInfo.h"
37 #include "llvm/MC/MCELFStreamer.h"
38 #include "llvm/MC/MCInst.h"
39 #include "llvm/MC/MCSymbol.h"
40 #include "llvm/Support/ELF.h"
41 #include "llvm/Support/TargetRegistry.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Target/TargetLoweringObjectFile.h"
44 #include "llvm/Target/TargetOptions.h"
45
46 using namespace llvm;
47
48 MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() {
49   return static_cast<MipsTargetStreamer &>(*OutStreamer.getTargetStreamer());
50 }
51
52 bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
53   // Initialize TargetLoweringObjectFile.
54   if (Subtarget->allowMixed16_32())
55     const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
56       .Initialize(OutContext, TM);
57   MipsFI = MF.getInfo<MipsFunctionInfo>();
58   MCP = MF.getConstantPool();
59   AsmPrinter::runOnMachineFunction(MF);
60   return true;
61 }
62
63 bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
64   MCOp = MCInstLowering.LowerOperand(MO);
65   return MCOp.isValid();
66 }
67
68 #include "MipsGenMCPseudoLowering.inc"
69
70 void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
71   if (MI->isDebugValue()) {
72     SmallString<128> Str;
73     raw_svector_ostream OS(Str);
74
75     PrintDebugValueComment(MI, OS);
76     return;
77   }
78
79   // If we just ended a constant pool, mark it as such.
80   if (InConstantPool && MI->getOpcode() != Mips::CONSTPOOL_ENTRY) {
81     OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
82     InConstantPool = false;
83   }
84   if (MI->getOpcode() == Mips::CONSTPOOL_ENTRY) {
85     // CONSTPOOL_ENTRY - This instruction represents a floating
86     //constant pool in the function.  The first operand is the ID#
87     // for this instruction, the second is the index into the
88     // MachineConstantPool that this is, the third is the size in
89     // bytes of this constant pool entry.
90     // The required alignment is specified on the basic block holding this MI.
91     //
92     unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
93     unsigned CPIdx   = (unsigned)MI->getOperand(1).getIndex();
94
95     // If this is the first entry of the pool, mark it.
96     if (!InConstantPool) {
97       OutStreamer.EmitDataRegion(MCDR_DataRegion);
98       InConstantPool = true;
99     }
100
101     OutStreamer.EmitLabel(GetCPISymbol(LabelId));
102
103     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
104     if (MCPE.isMachineConstantPoolEntry())
105       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
106     else
107       EmitGlobalConstant(MCPE.Val.ConstVal);
108     return;
109   }
110
111
112   MachineBasicBlock::const_instr_iterator I = MI;
113   MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
114
115   do {
116     // Do any auto-generated pseudo lowerings.
117     if (emitPseudoExpansionLowering(OutStreamer, &*I))
118       continue;
119
120     // The inMips16Mode() test is not permanent.
121     // Some instructions are marked as pseudo right now which
122     // would make the test fail for the wrong reason but
123     // that will be fixed soon. We need this here because we are
124     // removing another test for this situation downstream in the
125     // callchain.
126     //
127     if (I->isPseudo() && !Subtarget->inMips16Mode())
128       llvm_unreachable("Pseudo opcode found in EmitInstruction()");
129
130     MCInst TmpInst0;
131     MCInstLowering.Lower(I, TmpInst0);
132     OutStreamer.EmitInstruction(TmpInst0);
133   } while ((++I != E) && I->isInsideBundle()); // Delay slot check
134 }
135
136 //===----------------------------------------------------------------------===//
137 //
138 //  Mips Asm Directives
139 //
140 //  -- Frame directive "frame Stackpointer, Stacksize, RARegister"
141 //  Describe the stack frame.
142 //
143 //  -- Mask directives "(f)mask  bitmask, offset"
144 //  Tells the assembler which registers are saved and where.
145 //  bitmask - contain a little endian bitset indicating which registers are
146 //            saved on function prologue (e.g. with a 0x80000000 mask, the
147 //            assembler knows the register 31 (RA) is saved at prologue.
148 //  offset  - the position before stack pointer subtraction indicating where
149 //            the first saved register on prologue is located. (e.g. with a
150 //
151 //  Consider the following function prologue:
152 //
153 //    .frame  $fp,48,$ra
154 //    .mask   0xc0000000,-8
155 //       addiu $sp, $sp, -48
156 //       sw $ra, 40($sp)
157 //       sw $fp, 36($sp)
158 //
159 //    With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
160 //    30 (FP) are saved at prologue. As the save order on prologue is from
161 //    left to right, RA is saved first. A -8 offset means that after the
162 //    stack pointer subtration, the first register in the mask (RA) will be
163 //    saved at address 48-8=40.
164 //
165 //===----------------------------------------------------------------------===//
166
167 //===----------------------------------------------------------------------===//
168 // Mask directives
169 //===----------------------------------------------------------------------===//
170
171 // Create a bitmask with all callee saved registers for CPU or Floating Point
172 // registers. For CPU registers consider RA, GP and FP for saving if necessary.
173 void MipsAsmPrinter::printSavedRegsBitmask(raw_ostream &O) {
174   // CPU and FPU Saved Registers Bitmasks
175   unsigned CPUBitmask = 0, FPUBitmask = 0;
176   int CPUTopSavedRegOff, FPUTopSavedRegOff;
177
178   // Set the CPU and FPU Bitmasks
179   const MachineFrameInfo *MFI = MF->getFrameInfo();
180   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
181   // size of stack area to which FP callee-saved regs are saved.
182   unsigned CPURegSize = Mips::GPR32RegClass.getSize();
183   unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
184   unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
185   bool HasAFGR64Reg = false;
186   unsigned CSFPRegsSize = 0;
187   unsigned i, e = CSI.size();
188
189   // Set FPU Bitmask.
190   for (i = 0; i != e; ++i) {
191     unsigned Reg = CSI[i].getReg();
192     if (Mips::GPR32RegClass.contains(Reg))
193       break;
194
195     unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
196     if (Mips::AFGR64RegClass.contains(Reg)) {
197       FPUBitmask |= (3 << RegNum);
198       CSFPRegsSize += AFGR64RegSize;
199       HasAFGR64Reg = true;
200       continue;
201     }
202
203     FPUBitmask |= (1 << RegNum);
204     CSFPRegsSize += FGR32RegSize;
205   }
206
207   // Set CPU Bitmask.
208   for (; i != e; ++i) {
209     unsigned Reg = CSI[i].getReg();
210     unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
211     CPUBitmask |= (1 << RegNum);
212   }
213
214   // FP Regs are saved right below where the virtual frame pointer points to.
215   FPUTopSavedRegOff = FPUBitmask ?
216     (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
217
218   // CPU Regs are saved below FP Regs.
219   CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
220
221   // Print CPUBitmask
222   O << "\t.mask \t"; printHex32(CPUBitmask, O);
223   O << ',' << CPUTopSavedRegOff << '\n';
224
225   // Print FPUBitmask
226   O << "\t.fmask\t"; printHex32(FPUBitmask, O);
227   O << "," << FPUTopSavedRegOff << '\n';
228 }
229
230 // Print a 32 bit hex number with all numbers.
231 void MipsAsmPrinter::printHex32(unsigned Value, raw_ostream &O) {
232   O << "0x";
233   for (int i = 7; i >= 0; i--)
234     O.write_hex((Value & (0xF << (i*4))) >> (i*4));
235 }
236
237 //===----------------------------------------------------------------------===//
238 // Frame and Set directives
239 //===----------------------------------------------------------------------===//
240
241 /// Frame Directive
242 void MipsAsmPrinter::emitFrameDirective() {
243   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
244
245   unsigned stackReg  = RI.getFrameRegister(*MF);
246   unsigned returnReg = RI.getRARegister();
247   unsigned stackSize = MF->getFrameInfo()->getStackSize();
248
249   if (OutStreamer.hasRawTextSupport())
250     OutStreamer.EmitRawText("\t.frame\t$" +
251            StringRef(MipsInstPrinter::getRegisterName(stackReg)).lower() +
252            "," + Twine(stackSize) + ",$" +
253            StringRef(MipsInstPrinter::getRegisterName(returnReg)).lower());
254 }
255
256 /// Emit Set directives.
257 const char *MipsAsmPrinter::getCurrentABIString() const {
258   switch (Subtarget->getTargetABI()) {
259   case MipsSubtarget::O32:  return "abi32";
260   case MipsSubtarget::N32:  return "abiN32";
261   case MipsSubtarget::N64:  return "abi64";
262   case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
263   default: llvm_unreachable("Unknown Mips ABI");
264   }
265 }
266
267 void MipsAsmPrinter::EmitFunctionEntryLabel() {
268   MipsTargetStreamer &TS = getTargetStreamer();
269   if (Subtarget->inMicroMipsMode())
270     TS.emitDirectiveSetMicroMips();
271   // leave out until FSF available gas has micromips changes
272   //  else
273   //    TS.emitDirectiveSetNoMicroMips();
274
275   if (Subtarget->inMips16Mode())
276     TS.emitDirectiveSetMips16();
277   else
278     TS.emitDirectiveSetNoMips16();
279
280   TS.emitDirectiveEnt(*CurrentFnSym);
281   OutStreamer.EmitLabel(CurrentFnSym);
282 }
283
284 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
285 /// the first basic block in the function.
286 void MipsAsmPrinter::EmitFunctionBodyStart() {
287   MipsTargetStreamer &TS = getTargetStreamer();
288
289   MCInstLowering.Initialize(&MF->getContext());
290
291   bool IsNakedFunction =
292     MF->getFunction()->
293       getAttributes().hasAttribute(AttributeSet::FunctionIndex,
294                                    Attribute::Naked);
295   if (!IsNakedFunction)
296     emitFrameDirective();
297
298   if (OutStreamer.hasRawTextSupport()) {
299     SmallString<128> Str;
300     raw_svector_ostream OS(Str);
301     if (!IsNakedFunction)
302       printSavedRegsBitmask(OS);
303     OutStreamer.EmitRawText(OS.str());
304   }
305   if (!Subtarget->inMips16Mode()) {
306     TS.emitDirectiveSetNoReorder();
307     TS.emitDirectiveSetNoMacro();
308     TS.emitDirectiveSetNoAt();
309   }
310 }
311
312 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
313 /// the last basic block in the function.
314 void MipsAsmPrinter::EmitFunctionBodyEnd() {
315   MipsTargetStreamer &TS = getTargetStreamer();
316
317   // There are instruction for this macros, but they must
318   // always be at the function end, and we can't emit and
319   // break with BB logic.
320   if (!Subtarget->inMips16Mode()) {
321     TS.emitDirectiveSetAt();
322     TS.emitDirectiveSetMacro();
323     TS.emitDirectiveSetReorder();
324   }
325   TS.emitDirectiveEnd(CurrentFnSym->getName());
326   // Make sure to terminate any constant pools that were at the end
327   // of the function.
328   if (!InConstantPool)
329     return;
330   InConstantPool = false;
331   OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
332 }
333
334 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
335 /// exactly one predecessor and the control transfer mechanism between
336 /// the predecessor and this block is a fall-through.
337 bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
338                                                        MBB) const {
339   // The predecessor has to be immediately before this block.
340   const MachineBasicBlock *Pred = *MBB->pred_begin();
341
342   // If the predecessor is a switch statement, assume a jump table
343   // implementation, so it is not a fall through.
344   if (const BasicBlock *bb = Pred->getBasicBlock())
345     if (isa<SwitchInst>(bb->getTerminator()))
346       return false;
347
348   // If this is a landing pad, it isn't a fall through.  If it has no preds,
349   // then nothing falls through to it.
350   if (MBB->isLandingPad() || MBB->pred_empty())
351     return false;
352
353   // If there isn't exactly one predecessor, it can't be a fall through.
354   MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
355   ++PI2;
356
357   if (PI2 != MBB->pred_end())
358     return false;
359
360   // The predecessor has to be immediately before this block.
361   if (!Pred->isLayoutSuccessor(MBB))
362     return false;
363
364   // If the block is completely empty, then it definitely does fall through.
365   if (Pred->empty())
366     return true;
367
368   // Otherwise, check the last instruction.
369   // Check if the last terminator is an unconditional branch.
370   MachineBasicBlock::const_iterator I = Pred->end();
371   while (I != Pred->begin() && !(--I)->isTerminator()) ;
372
373   return !I->isBarrier();
374 }
375
376 // Print out an operand for an inline asm expression.
377 bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
378                                      unsigned AsmVariant,const char *ExtraCode,
379                                      raw_ostream &O) {
380   // Does this asm operand have a single letter operand modifier?
381   if (ExtraCode && ExtraCode[0]) {
382     if (ExtraCode[1] != 0) return true; // Unknown modifier.
383
384     const MachineOperand &MO = MI->getOperand(OpNum);
385     switch (ExtraCode[0]) {
386     default:
387       // See if this is a generic print operand
388       return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
389     case 'X': // hex const int
390       if ((MO.getType()) != MachineOperand::MO_Immediate)
391         return true;
392       O << "0x" << StringRef(utohexstr(MO.getImm())).lower();
393       return false;
394     case 'x': // hex const int (low 16 bits)
395       if ((MO.getType()) != MachineOperand::MO_Immediate)
396         return true;
397       O << "0x" << StringRef(utohexstr(MO.getImm() & 0xffff)).lower();
398       return false;
399     case 'd': // decimal const int
400       if ((MO.getType()) != MachineOperand::MO_Immediate)
401         return true;
402       O << MO.getImm();
403       return false;
404     case 'm': // decimal const int minus 1
405       if ((MO.getType()) != MachineOperand::MO_Immediate)
406         return true;
407       O << MO.getImm() - 1;
408       return false;
409     case 'z': {
410       // $0 if zero, regular printing otherwise
411       if (MO.getType() != MachineOperand::MO_Immediate)
412         return true;
413       int64_t Val = MO.getImm();
414       if (Val)
415         O << Val;
416       else
417         O << "$0";
418       return false;
419     }
420     case 'D': // Second part of a double word register operand
421     case 'L': // Low order register of a double word register operand
422     case 'M': // High order register of a double word register operand
423     {
424       if (OpNum == 0)
425         return true;
426       const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
427       if (!FlagsOP.isImm())
428         return true;
429       unsigned Flags = FlagsOP.getImm();
430       unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
431       // Number of registers represented by this operand. We are looking
432       // for 2 for 32 bit mode and 1 for 64 bit mode.
433       if (NumVals != 2) {
434         if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
435           unsigned Reg = MO.getReg();
436           O << '$' << MipsInstPrinter::getRegisterName(Reg);
437           return false;
438         }
439         return true;
440       }
441
442       unsigned RegOp = OpNum;
443       if (!Subtarget->isGP64bit()){
444         // Endianess reverses which register holds the high or low value
445         // between M and L.
446         switch(ExtraCode[0]) {
447         case 'M':
448           RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
449           break;
450         case 'L':
451           RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
452           break;
453         case 'D': // Always the second part
454           RegOp = OpNum + 1;
455         }
456         if (RegOp >= MI->getNumOperands())
457           return true;
458         const MachineOperand &MO = MI->getOperand(RegOp);
459         if (!MO.isReg())
460           return true;
461         unsigned Reg = MO.getReg();
462         O << '$' << MipsInstPrinter::getRegisterName(Reg);
463         return false;
464       }
465     }
466     case 'w':
467       // Print MSA registers for the 'f' constraint
468       // In LLVM, the 'w' modifier doesn't need to do anything.
469       // We can just call printOperand as normal.
470       break;
471     }
472   }
473
474   printOperand(MI, OpNum, O);
475   return false;
476 }
477
478 bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
479                                            unsigned OpNum, unsigned AsmVariant,
480                                            const char *ExtraCode,
481                                            raw_ostream &O) {
482   int Offset = 0;
483   // Currently we are expecting either no ExtraCode or 'D'
484   if (ExtraCode) {
485     if (ExtraCode[0] == 'D')
486       Offset = 4;
487     else
488       return true; // Unknown modifier.
489   }
490
491   const MachineOperand &MO = MI->getOperand(OpNum);
492   assert(MO.isReg() && "unexpected inline asm memory operand");
493   O << Offset << "($" << MipsInstPrinter::getRegisterName(MO.getReg()) << ")";
494
495   return false;
496 }
497
498 void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
499                                   raw_ostream &O) {
500   const DataLayout *DL = TM.getDataLayout();
501   const MachineOperand &MO = MI->getOperand(opNum);
502   bool closeP = false;
503
504   if (MO.getTargetFlags())
505     closeP = true;
506
507   switch(MO.getTargetFlags()) {
508   case MipsII::MO_GPREL:    O << "%gp_rel("; break;
509   case MipsII::MO_GOT_CALL: O << "%call16("; break;
510   case MipsII::MO_GOT:      O << "%got(";    break;
511   case MipsII::MO_ABS_HI:   O << "%hi(";     break;
512   case MipsII::MO_ABS_LO:   O << "%lo(";     break;
513   case MipsII::MO_TLSGD:    O << "%tlsgd(";  break;
514   case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
515   case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
516   case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
517   case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
518   case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
519   case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
520   case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
521   case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
522   }
523
524   switch (MO.getType()) {
525     case MachineOperand::MO_Register:
526       O << '$'
527         << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
528       break;
529
530     case MachineOperand::MO_Immediate:
531       O << MO.getImm();
532       break;
533
534     case MachineOperand::MO_MachineBasicBlock:
535       O << *MO.getMBB()->getSymbol();
536       return;
537
538     case MachineOperand::MO_GlobalAddress:
539       O << *getSymbol(MO.getGlobal());
540       break;
541
542     case MachineOperand::MO_BlockAddress: {
543       MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
544       O << BA->getName();
545       break;
546     }
547
548     case MachineOperand::MO_ConstantPoolIndex:
549       O << DL->getPrivateGlobalPrefix() << "CPI"
550         << getFunctionNumber() << "_" << MO.getIndex();
551       if (MO.getOffset())
552         O << "+" << MO.getOffset();
553       break;
554
555     default:
556       llvm_unreachable("<unknown operand type>");
557   }
558
559   if (closeP) O << ")";
560 }
561
562 void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
563                                       raw_ostream &O) {
564   const MachineOperand &MO = MI->getOperand(opNum);
565   if (MO.isImm())
566     O << (unsigned short int)MO.getImm();
567   else
568     printOperand(MI, opNum, O);
569 }
570
571 void MipsAsmPrinter::printUnsignedImm8(const MachineInstr *MI, int opNum,
572                                        raw_ostream &O) {
573   const MachineOperand &MO = MI->getOperand(opNum);
574   if (MO.isImm())
575     O << (unsigned short int)(unsigned char)MO.getImm();
576   else
577     printOperand(MI, opNum, O);
578 }
579
580 void MipsAsmPrinter::
581 printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
582   // Load/Store memory operands -- imm($reg)
583   // If PIC target the target is loaded as the
584   // pattern lw $25,%call16($28)
585   printOperand(MI, opNum+1, O);
586   O << "(";
587   printOperand(MI, opNum, O);
588   O << ")";
589 }
590
591 void MipsAsmPrinter::
592 printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
593   // when using stack locations for not load/store instructions
594   // print the same way as all normal 3 operand instructions.
595   printOperand(MI, opNum, O);
596   O << ", ";
597   printOperand(MI, opNum+1, O);
598   return;
599 }
600
601 void MipsAsmPrinter::
602 printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
603                 const char *Modifier) {
604   const MachineOperand &MO = MI->getOperand(opNum);
605   O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
606 }
607
608 void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
609   // FIXME: Use SwitchSection.
610
611   // TODO: Need to add -mabicalls and -mno-abicalls flags.
612   // Currently we assume that -mabicalls is the default.
613   getTargetStreamer().emitDirectiveAbiCalls();
614   Reloc::Model RM = Subtarget->getRelocationModel();
615   if (RM == Reloc::Static && !Subtarget->hasMips64())
616     getTargetStreamer().emitDirectiveOptionPic0();
617
618   // Tell the assembler which ABI we are using
619   if (OutStreamer.hasRawTextSupport())
620     OutStreamer.EmitRawText("\t.section .mdebug." +
621                             Twine(getCurrentABIString()));
622
623   // TODO: handle O64 ABI
624   if (OutStreamer.hasRawTextSupport()) {
625     if (Subtarget->isABI_EABI()) {
626       if (Subtarget->isGP32bit())
627         OutStreamer.EmitRawText(StringRef("\t.section .gcc_compiled_long32"));
628       else
629         OutStreamer.EmitRawText(StringRef("\t.section .gcc_compiled_long64"));
630     }
631   }
632
633   // return to previous section
634   if (OutStreamer.hasRawTextSupport())
635     OutStreamer.EmitRawText(StringRef("\t.previous"));
636
637 }
638
639 void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
640   // Emit Mips ELF register info
641   Subtarget->getMReginfo().emitMipsReginfoSectionCG(
642              OutStreamer, getObjFileLowering(), *Subtarget);
643 }
644
645 void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
646                                            raw_ostream &OS) {
647   // TODO: implement
648 }
649
650 // Force static initialization.
651 extern "C" void LLVMInitializeMipsAsmPrinter() {
652   RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
653   RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
654   RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
655   RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
656 }