e179a236e97e3e46f9e1962cb60b2f26a5a450c5
[oota-llvm.git] / lib / Target / MBlaze / MBlazeAsmPrinter.cpp
1 //===-- MBlazeAsmPrinter.cpp - MBlaze 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 MBlaze assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "mblaze-asm-printer"
16
17 #include "MBlaze.h"
18 #include "MBlazeSubtarget.h"
19 #include "MBlazeInstrInfo.h"
20 #include "MBlazeTargetMachine.h"
21 #include "MBlazeMachineFunction.h"
22 #include "MBlazeMCInstLower.h"
23 #include "InstPrinter/MBlazeInstPrinter.h"
24 #include "llvm/Constants.h"
25 #include "llvm/DerivedTypes.h"
26 #include "llvm/Module.h"
27 #include "llvm/CodeGen/AsmPrinter.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineConstantPool.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineInstr.h"
32 #include "llvm/MC/MCInst.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/MC/MCAsmInfo.h"
35 #include "llvm/MC/MCSymbol.h"
36 #include "llvm/Target/Mangler.h"
37 #include "llvm/Target/TargetData.h"
38 #include "llvm/Target/TargetLoweringObjectFile.h"
39 #include "llvm/Target/TargetMachine.h"
40 #include "llvm/Target/TargetOptions.h"
41 #include "llvm/Target/TargetRegistry.h"
42 #include "llvm/ADT/SmallString.h"
43 #include "llvm/ADT/StringExtras.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include <cctype>
47
48 using namespace llvm;
49
50 namespace {
51   class MBlazeAsmPrinter : public AsmPrinter {
52     const MBlazeSubtarget *Subtarget;
53   public:
54     explicit MBlazeAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
55       : AsmPrinter(TM, Streamer) {
56       Subtarget = &TM.getSubtarget<MBlazeSubtarget>();
57     }
58
59     virtual const char *getPassName() const {
60       return "MBlaze Assembly Printer";
61     }
62
63     void printSavedRegsBitmask(raw_ostream &O);
64     void emitFrameDirective();
65     virtual void EmitFunctionBodyStart();
66     virtual void EmitFunctionBodyEnd();
67     virtual void EmitFunctionEntryLabel();
68
69     virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB)
70       const;
71
72     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
73                          unsigned AsmVariant, const char *ExtraCode,
74                          raw_ostream &O);
75     void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
76     void printUnsignedImm(const MachineInstr *MI, int opNum, raw_ostream &O);
77     void printFSLImm(const MachineInstr *MI, int opNum, raw_ostream &O);
78     void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
79                          const char *Modifier = 0);
80
81     void EmitInstruction(const MachineInstr *MI);
82   };
83 } // end of anonymous namespace
84
85 // #include "MBlazeGenAsmWriter.inc"
86
87 //===----------------------------------------------------------------------===//
88 //
89 //  MBlaze Asm Directives
90 //
91 //  -- Frame directive "frame Stackpointer, Stacksize, RARegister"
92 //  Describe the stack frame.
93 //
94 //  -- Mask directives "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  R19,48,R15
105 //    .mask   0xc0000000,-8
106 //       addiu R1, R1, -48
107 //       sw R15, 40(R1)
108 //       sw R19, 36(R1)
109 //
110 //    With a 0xc0000000 mask, the assembler knows the register 15 (R15) and
111 //    19 (R19) are saved at prologue. As the save order on prologue is from
112 //    left to right, R15 is saved first. A -8 offset means that after the
113 //    stack pointer subtration, the first register in the mask (R15) will be
114 //    saved at address 48-8=40.
115 //
116 //===----------------------------------------------------------------------===//
117
118 // Print a 32 bit hex number with all numbers.
119 static void printHex32(unsigned int Value, raw_ostream &O) {
120   O << "0x";
121   for (int i = 7; i >= 0; i--)
122     O << utohexstr((Value & (0xF << (i*4))) >> (i*4));
123 }
124
125 // Create a bitmask with all callee saved registers for CPU or Floating Point
126 // registers. For CPU registers consider RA, GP and FP for saving if necessary.
127 void MBlazeAsmPrinter::printSavedRegsBitmask(raw_ostream &O) {
128   const TargetFrameInfo *TFI = TM.getFrameInfo();
129   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
130   const MBlazeFunctionInfo *MBlazeFI = MF->getInfo<MBlazeFunctionInfo>();
131
132   // CPU Saved Registers Bitmasks
133   unsigned int CPUBitmask = 0;
134
135   // Set the CPU Bitmasks
136   const MachineFrameInfo *MFI = MF->getFrameInfo();
137   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
138   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
139     unsigned Reg = CSI[i].getReg();
140     unsigned RegNum = MBlazeRegisterInfo::getRegisterNumbering(Reg);
141     if (MBlaze::GPRRegisterClass->contains(Reg))
142       CPUBitmask |= (1 << RegNum);
143   }
144
145   // Return Address and Frame registers must also be set in CPUBitmask.
146   if (TFI->hasFP(*MF))
147     CPUBitmask |= (1 << MBlazeRegisterInfo::
148                 getRegisterNumbering(RI.getFrameRegister(*MF)));
149
150   if (MFI->adjustsStack())
151     CPUBitmask |= (1 << MBlazeRegisterInfo::
152                 getRegisterNumbering(RI.getRARegister()));
153
154   // Print CPUBitmask
155   O << "\t.mask \t"; printHex32(CPUBitmask, O); O << ','
156     << MBlazeFI->getCPUTopSavedRegOff() << '\n';
157 }
158
159 /// Frame Directive
160 void MBlazeAsmPrinter::emitFrameDirective() {
161   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
162
163   unsigned stkReg = RI.getFrameRegister(*MF);
164   unsigned retReg = RI.getRARegister();
165   unsigned stkSze = MF->getFrameInfo()->getStackSize();
166
167   OutStreamer.EmitRawText("\t.frame\t" +
168                           Twine(MBlazeInstPrinter::getRegisterName(stkReg)) +
169                           "," + Twine(stkSze) + "," +
170                           Twine(MBlazeInstPrinter::getRegisterName(retReg)));
171 }
172
173 void MBlazeAsmPrinter::EmitFunctionEntryLabel() {
174   OutStreamer.EmitRawText("\t.ent\t" + Twine(CurrentFnSym->getName()));
175   AsmPrinter::EmitFunctionEntryLabel();
176 }
177
178 void MBlazeAsmPrinter::EmitFunctionBodyStart() {
179   emitFrameDirective();
180
181   SmallString<128> Str;
182   raw_svector_ostream OS(Str);
183   printSavedRegsBitmask(OS);
184   OutStreamer.EmitRawText(OS.str());
185 }
186
187 void MBlazeAsmPrinter::EmitFunctionBodyEnd() {
188   OutStreamer.EmitRawText("\t.end\t" + Twine(CurrentFnSym->getName()));
189 }
190
191 //===----------------------------------------------------------------------===//
192 void MBlazeAsmPrinter::EmitInstruction(const MachineInstr *MI) {
193   MBlazeMCInstLower MCInstLowering(OutContext, *Mang, *this);
194
195   MCInst TmpInst;
196   MCInstLowering.Lower(MI, TmpInst);
197   OutStreamer.EmitInstruction(TmpInst);
198 }
199
200 // Print out an operand for an inline asm expression.
201 bool MBlazeAsmPrinter::
202 PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
203                 unsigned AsmVariant,const char *ExtraCode, raw_ostream &O) {
204   // Does this asm operand have a single letter operand modifier?
205   if (ExtraCode && ExtraCode[0])
206     return true; // Unknown modifier.
207
208   printOperand(MI, OpNo, O);
209   return false;
210 }
211
212 void MBlazeAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
213                                     raw_ostream &O) {
214   const MachineOperand &MO = MI->getOperand(opNum);
215
216   switch (MO.getType()) {
217   case MachineOperand::MO_Register:
218     O << MBlazeInstPrinter::getRegisterName(MO.getReg());
219     break;
220
221   case MachineOperand::MO_Immediate:
222     O << (int32_t)MO.getImm();
223     break;
224
225   case MachineOperand::MO_FPImmediate: {
226     const ConstantFP *fp = MO.getFPImm();
227     printHex32(fp->getValueAPF().bitcastToAPInt().getZExtValue(), O);
228     O << ";\t# immediate = " << *fp;
229     break;
230   }
231
232   case MachineOperand::MO_MachineBasicBlock:
233     O << *MO.getMBB()->getSymbol();
234     return;
235
236   case MachineOperand::MO_GlobalAddress:
237     O << *Mang->getSymbol(MO.getGlobal());
238     break;
239
240   case MachineOperand::MO_ExternalSymbol:
241     O << *GetExternalSymbolSymbol(MO.getSymbolName());
242     break;
243
244   case MachineOperand::MO_JumpTableIndex:
245     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
246       << '_' << MO.getIndex();
247     break;
248
249   case MachineOperand::MO_ConstantPoolIndex:
250     O << MAI->getPrivateGlobalPrefix() << "CPI"
251       << getFunctionNumber() << "_" << MO.getIndex();
252     if (MO.getOffset())
253       O << "+" << MO.getOffset();
254     break;
255
256   default:
257     llvm_unreachable("<unknown operand type>");
258   }
259 }
260
261 void MBlazeAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
262                                         raw_ostream &O) {
263   const MachineOperand &MO = MI->getOperand(opNum);
264   if (MO.isImm())
265     O << (uint32_t)MO.getImm();
266   else
267     printOperand(MI, opNum, O);
268 }
269
270 void MBlazeAsmPrinter::printFSLImm(const MachineInstr *MI, int opNum,
271                                    raw_ostream &O) {
272   const MachineOperand &MO = MI->getOperand(opNum);
273   if (MO.isImm())
274     O << "rfsl" << (unsigned int)MO.getImm();
275   else
276     printOperand(MI, opNum, O);
277 }
278
279 void MBlazeAsmPrinter::
280 printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
281                 const char *Modifier) {
282   printOperand(MI, opNum, O);
283   O << ", ";
284   printOperand(MI, opNum+1, O);
285 }
286
287 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
288 /// exactly one predecessor and the control transfer mechanism between
289 /// the predecessor and this block is a fall-through.
290 bool MBlazeAsmPrinter::
291 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
292   // If this is a landing pad, it isn't a fall through.  If it has no preds,
293   // then nothing falls through to it.
294   if (MBB->isLandingPad() || MBB->pred_empty())
295     return false;
296
297   // If there isn't exactly one predecessor, it can't be a fall through.
298   MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
299   ++PI2;
300   if (PI2 != MBB->pred_end())
301     return false;
302
303   // The predecessor has to be immediately before this block.
304   const MachineBasicBlock *Pred = *PI;
305
306   if (!Pred->isLayoutSuccessor(MBB))
307     return false;
308
309   // If the block is completely empty, then it definitely does fall through.
310   if (Pred->empty())
311     return true;
312
313   // Check if the last terminator is an unconditional branch.
314   MachineBasicBlock::const_iterator I = Pred->end();
315   while (I != Pred->begin() && !(--I)->getDesc().isTerminator())
316     ; // Noop
317   return I == Pred->end() || !I->getDesc().isBarrier();
318 }
319
320 static MCInstPrinter *createMBlazeMCInstPrinter(const Target &T,
321                                                 unsigned SyntaxVariant,
322                                                 const MCAsmInfo &MAI) {
323   if (SyntaxVariant == 0)
324     return new MBlazeInstPrinter(MAI);
325   return 0;
326 }
327
328 // Force static initialization.
329 extern "C" void LLVMInitializeMBlazeAsmPrinter() {
330   RegisterAsmPrinter<MBlazeAsmPrinter> X(TheMBlazeTarget);
331   TargetRegistry::RegisterMCInstPrinter(TheMBlazeTarget,
332                                         createMBlazeMCInstPrinter);
333
334 }