1 //===-- MBlazeAsmPrinter.cpp - MBlaze LLVM assembly writer ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to GAS-format MBlaze assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "mblaze-asm-printer"
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"
51 class MBlazeAsmPrinter
: public AsmPrinter
{
52 const MBlazeSubtarget
*Subtarget
;
54 explicit MBlazeAsmPrinter(TargetMachine
&TM
, MCStreamer
&Streamer
)
55 : AsmPrinter(TM
, Streamer
) {
56 Subtarget
= &TM
.getSubtarget
<MBlazeSubtarget
>();
59 virtual const char *getPassName() const {
60 return "MBlaze Assembly Printer";
63 void printSavedRegsBitmask();
64 void emitFrameDirective();
65 virtual void EmitFunctionBodyStart();
66 virtual void EmitFunctionBodyEnd();
67 virtual void EmitFunctionEntryLabel();
69 virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock
*MBB
)
72 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
73 unsigned AsmVariant
, const char *ExtraCode
,
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);
81 void EmitInstruction(const MachineInstr
*MI
);
83 } // end of anonymous namespace
85 // #include "MBlazeGenAsmWriter.inc"
87 //===----------------------------------------------------------------------===//
89 // MBlaze Asm Directives
91 // -- Frame directive "frame Stackpointer, Stacksize, RARegister"
92 // Describe the stack frame.
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
102 // Consider the following function prologue:
105 // .mask 0xc0000000,-8
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.
116 //===----------------------------------------------------------------------===//
118 // Print a 32 bit hex number with all numbers.
119 static void printHex32(unsigned int Value
, raw_ostream
&O
) {
121 for (int i
= 7; i
>= 0; i
--)
122 O
<< utohexstr((Value
& (0xF << (i
*4))) >> (i
*4));
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() {
128 const TargetFrameLowering
*TFI
= TM
.getFrameLowering();
129 const TargetRegisterInfo
&RI
= *TM
.getRegisterInfo();
131 // CPU Saved Registers Bitmasks
132 unsigned int CPUBitmask
= 0;
134 // Set the CPU Bitmasks
135 const MachineFrameInfo
*MFI
= MF
->getFrameInfo();
136 const std::vector
<CalleeSavedInfo
> &CSI
= MFI
->getCalleeSavedInfo();
137 for (unsigned i
= 0, e
= CSI
.size(); i
!= e
; ++i
) {
138 unsigned Reg
= CSI
[i
].getReg();
139 unsigned RegNum
= MBlazeRegisterInfo::getRegisterNumbering(Reg
);
140 if (MBlaze::GPRRegisterClass
->contains(Reg
))
141 CPUBitmask
|= (1 << RegNum
);
144 // Return Address and Frame registers must also be set in CPUBitmask.
146 CPUBitmask
|= (1 << MBlazeRegisterInfo::
147 getRegisterNumbering(RI
.getFrameRegister(*MF
)));
149 if (MFI
->adjustsStack())
150 CPUBitmask
|= (1 << MBlazeRegisterInfo::
151 getRegisterNumbering(RI
.getRARegister()));
154 OutStreamer
.EmitRawText("\t.mask\t0x" + Twine::utohexstr(CPUBitmask
));
158 void MBlazeAsmPrinter::emitFrameDirective() {
159 if (!OutStreamer
.hasRawTextSupport())
162 const TargetRegisterInfo
&RI
= *TM
.getRegisterInfo();
163 unsigned stkReg
= RI
.getFrameRegister(*MF
);
164 unsigned retReg
= RI
.getRARegister();
165 unsigned stkSze
= MF
->getFrameInfo()->getStackSize();
167 OutStreamer
.EmitRawText("\t.frame\t" +
168 Twine(MBlazeInstPrinter::getRegisterName(stkReg
)) +
169 "," + Twine(stkSze
) + "," +
170 Twine(MBlazeInstPrinter::getRegisterName(retReg
)));
173 void MBlazeAsmPrinter::EmitFunctionEntryLabel() {
174 if (OutStreamer
.hasRawTextSupport())
175 OutStreamer
.EmitRawText("\t.ent\t" + Twine(CurrentFnSym
->getName()));
176 AsmPrinter::EmitFunctionEntryLabel();
179 void MBlazeAsmPrinter::EmitFunctionBodyStart() {
180 if (!OutStreamer
.hasRawTextSupport())
183 emitFrameDirective();
184 printSavedRegsBitmask();
187 void MBlazeAsmPrinter::EmitFunctionBodyEnd() {
188 if (OutStreamer
.hasRawTextSupport())
189 OutStreamer
.EmitRawText("\t.end\t" + Twine(CurrentFnSym
->getName()));
192 //===----------------------------------------------------------------------===//
193 void MBlazeAsmPrinter::EmitInstruction(const MachineInstr
*MI
) {
194 MBlazeMCInstLower
MCInstLowering(OutContext
, *Mang
, *this);
197 MCInstLowering
.Lower(MI
, TmpInst
);
198 OutStreamer
.EmitInstruction(TmpInst
);
201 // Print out an operand for an inline asm expression.
202 bool MBlazeAsmPrinter::
203 PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
204 unsigned AsmVariant
,const char *ExtraCode
, raw_ostream
&O
) {
205 // Does this asm operand have a single letter operand modifier?
206 if (ExtraCode
&& ExtraCode
[0])
207 return true; // Unknown modifier.
209 printOperand(MI
, OpNo
, O
);
213 void MBlazeAsmPrinter::printOperand(const MachineInstr
*MI
, int opNum
,
215 const MachineOperand
&MO
= MI
->getOperand(opNum
);
217 switch (MO
.getType()) {
218 case MachineOperand::MO_Register
:
219 O
<< MBlazeInstPrinter::getRegisterName(MO
.getReg());
222 case MachineOperand::MO_Immediate
:
223 O
<< (int32_t)MO
.getImm();
226 case MachineOperand::MO_FPImmediate
: {
227 const ConstantFP
*fp
= MO
.getFPImm();
228 printHex32(fp
->getValueAPF().bitcastToAPInt().getZExtValue(), O
);
229 O
<< ";\t# immediate = " << *fp
;
233 case MachineOperand::MO_MachineBasicBlock
:
234 O
<< *MO
.getMBB()->getSymbol();
237 case MachineOperand::MO_GlobalAddress
:
238 O
<< *Mang
->getSymbol(MO
.getGlobal());
241 case MachineOperand::MO_ExternalSymbol
:
242 O
<< *GetExternalSymbolSymbol(MO
.getSymbolName());
245 case MachineOperand::MO_JumpTableIndex
:
246 O
<< MAI
->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
247 << '_' << MO
.getIndex();
250 case MachineOperand::MO_ConstantPoolIndex
:
251 O
<< MAI
->getPrivateGlobalPrefix() << "CPI"
252 << getFunctionNumber() << "_" << MO
.getIndex();
254 O
<< "+" << MO
.getOffset();
258 llvm_unreachable("<unknown operand type>");
262 void MBlazeAsmPrinter::printUnsignedImm(const MachineInstr
*MI
, int opNum
,
264 const MachineOperand
&MO
= MI
->getOperand(opNum
);
266 O
<< (uint32_t)MO
.getImm();
268 printOperand(MI
, opNum
, O
);
271 void MBlazeAsmPrinter::printFSLImm(const MachineInstr
*MI
, int opNum
,
273 const MachineOperand
&MO
= MI
->getOperand(opNum
);
275 O
<< "rfsl" << (unsigned int)MO
.getImm();
277 printOperand(MI
, opNum
, O
);
280 void MBlazeAsmPrinter::
281 printMemOperand(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
,
282 const char *Modifier
) {
283 printOperand(MI
, opNum
, O
);
285 printOperand(MI
, opNum
+1, O
);
288 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
289 /// exactly one predecessor and the control transfer mechanism between
290 /// the predecessor and this block is a fall-through.
291 bool MBlazeAsmPrinter::
292 isBlockOnlyReachableByFallthrough(const MachineBasicBlock
*MBB
) const {
293 // If this is a landing pad, it isn't a fall through. If it has no preds,
294 // then nothing falls through to it.
295 if (MBB
->isLandingPad() || MBB
->pred_empty())
298 // If there isn't exactly one predecessor, it can't be a fall through.
299 MachineBasicBlock::const_pred_iterator PI
= MBB
->pred_begin(), PI2
= PI
;
301 if (PI2
!= MBB
->pred_end())
304 // The predecessor has to be immediately before this block.
305 const MachineBasicBlock
*Pred
= *PI
;
307 if (!Pred
->isLayoutSuccessor(MBB
))
310 // If the block is completely empty, then it definitely does fall through.
314 // Check if the last terminator is an unconditional branch.
315 MachineBasicBlock::const_iterator I
= Pred
->end();
316 while (I
!= Pred
->begin() && !(--I
)->getDesc().isTerminator())
318 return I
== Pred
->end() || !I
->getDesc().isBarrier();
321 static MCInstPrinter
*createMBlazeMCInstPrinter(const Target
&T
,
323 unsigned SyntaxVariant
,
324 const MCAsmInfo
&MAI
) {
325 if (SyntaxVariant
== 0)
326 return new MBlazeInstPrinter(TM
, MAI
);
330 // Force static initialization.
331 extern "C" void LLVMInitializeMBlazeAsmPrinter() {
332 RegisterAsmPrinter
<MBlazeAsmPrinter
> X(TheMBlazeTarget
);
333 TargetRegistry::RegisterMCInstPrinter(TheMBlazeTarget
,
334 createMBlazeMCInstPrinter
);