blob: 50abd1a6a392f203410d9b698eba76860175ae8d [file] [log] [blame]
Wesley Peck4e9141f2010-10-21 03:57:26 +00001//===-- 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
48using namespace llvm;
49
50namespace {
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
Wesley Peck42e75a32010-11-24 16:32:35 +000063 void printSavedRegsBitmask();
Wesley Peck82dc0402010-11-24 15:39:32 +000064 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
Wesley Peck4e9141f2010-10-21 03:57:26 +000072 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);
Wesley Peck4e9141f2010-10-21 03:57:26 +000080
Wesley Peck0a67d922010-11-08 19:40:01 +000081 void EmitInstruction(const MachineInstr *MI);
Wesley Peck4e9141f2010-10-21 03:57:26 +000082 };
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
Wesley Peck82dc0402010-11-24 15:39:32 +0000118// Print a 32 bit hex number with all numbers.
119static 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.
Wesley Peck42e75a32010-11-24 16:32:35 +0000127void MBlazeAsmPrinter::printSavedRegsBitmask() {
Wesley Peck82dc0402010-11-24 15:39:32 +0000128 const TargetFrameInfo *TFI = TM.getFrameInfo();
129 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Wesley Peck82dc0402010-11-24 15:39:32 +0000130
131 // CPU Saved Registers Bitmasks
132 unsigned int CPUBitmask = 0;
133
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);
142 }
143
144 // Return Address and Frame registers must also be set in CPUBitmask.
145 if (TFI->hasFP(*MF))
146 CPUBitmask |= (1 << MBlazeRegisterInfo::
147 getRegisterNumbering(RI.getFrameRegister(*MF)));
148
149 if (MFI->adjustsStack())
150 CPUBitmask |= (1 << MBlazeRegisterInfo::
151 getRegisterNumbering(RI.getRARegister()));
152
153 // Print CPUBitmask
Wesley Peck42e75a32010-11-24 16:32:35 +0000154 OutStreamer.EmitRawText("\t.mask\t0x" + Twine::utohexstr(CPUBitmask));
Wesley Peck82dc0402010-11-24 15:39:32 +0000155}
156
157/// Frame Directive
158void MBlazeAsmPrinter::emitFrameDirective() {
Wesley Peck42e75a32010-11-24 16:32:35 +0000159 if (!OutStreamer.hasRawTextSupport())
160 return;
Wesley Peck82dc0402010-11-24 15:39:32 +0000161
Wesley Peck42e75a32010-11-24 16:32:35 +0000162 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Wesley Peck82dc0402010-11-24 15:39:32 +0000163 unsigned stkReg = RI.getFrameRegister(*MF);
164 unsigned retReg = RI.getRARegister();
165 unsigned stkSze = MF->getFrameInfo()->getStackSize();
Wesley Peck42e75a32010-11-24 16:32:35 +0000166 if (stkSze < 28 && MF->getFrameInfo()->adjustsStack()) stkSze = 28;
Wesley Peck82dc0402010-11-24 15:39:32 +0000167
168 OutStreamer.EmitRawText("\t.frame\t" +
169 Twine(MBlazeInstPrinter::getRegisterName(stkReg)) +
170 "," + Twine(stkSze) + "," +
171 Twine(MBlazeInstPrinter::getRegisterName(retReg)));
172}
173
174void MBlazeAsmPrinter::EmitFunctionEntryLabel() {
Wesley Peck42e75a32010-11-24 16:32:35 +0000175 if (OutStreamer.hasRawTextSupport())
176 OutStreamer.EmitRawText("\t.ent\t" + Twine(CurrentFnSym->getName()));
Wesley Peck82dc0402010-11-24 15:39:32 +0000177 AsmPrinter::EmitFunctionEntryLabel();
178}
179
180void MBlazeAsmPrinter::EmitFunctionBodyStart() {
Wesley Peck42e75a32010-11-24 16:32:35 +0000181 if (!OutStreamer.hasRawTextSupport())
182 return;
Wesley Peck82dc0402010-11-24 15:39:32 +0000183
Wesley Peck42e75a32010-11-24 16:32:35 +0000184 emitFrameDirective();
185 printSavedRegsBitmask();
186
187 // SmallString<128> Str;
188 // raw_svector_ostream OS(Str);
189 // printSavedRegsBitmask(OS);
190 // OutStreamer.EmitRawText(OS.str());
Wesley Peck82dc0402010-11-24 15:39:32 +0000191}
192
193void MBlazeAsmPrinter::EmitFunctionBodyEnd() {
Wesley Peck42e75a32010-11-24 16:32:35 +0000194 if (OutStreamer.hasRawTextSupport())
195 OutStreamer.EmitRawText("\t.end\t" + Twine(CurrentFnSym->getName()));
Wesley Peck82dc0402010-11-24 15:39:32 +0000196}
197
Wesley Peck4e9141f2010-10-21 03:57:26 +0000198//===----------------------------------------------------------------------===//
199void MBlazeAsmPrinter::EmitInstruction(const MachineInstr *MI) {
200 MBlazeMCInstLower MCInstLowering(OutContext, *Mang, *this);
201
202 MCInst TmpInst;
203 MCInstLowering.Lower(MI, TmpInst);
204 OutStreamer.EmitInstruction(TmpInst);
205}
206
Wesley Peck4e9141f2010-10-21 03:57:26 +0000207// Print out an operand for an inline asm expression.
208bool MBlazeAsmPrinter::
209PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
210 unsigned AsmVariant,const char *ExtraCode, raw_ostream &O) {
211 // Does this asm operand have a single letter operand modifier?
212 if (ExtraCode && ExtraCode[0])
213 return true; // Unknown modifier.
214
215 printOperand(MI, OpNo, O);
216 return false;
217}
218
219void MBlazeAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
220 raw_ostream &O) {
221 const MachineOperand &MO = MI->getOperand(opNum);
222
223 switch (MO.getType()) {
224 case MachineOperand::MO_Register:
225 O << MBlazeInstPrinter::getRegisterName(MO.getReg());
226 break;
227
228 case MachineOperand::MO_Immediate:
Wesley Peck6e749f32010-11-21 21:39:46 +0000229 O << (int32_t)MO.getImm();
Wesley Peck4e9141f2010-10-21 03:57:26 +0000230 break;
231
232 case MachineOperand::MO_FPImmediate: {
233 const ConstantFP *fp = MO.getFPImm();
234 printHex32(fp->getValueAPF().bitcastToAPInt().getZExtValue(), O);
235 O << ";\t# immediate = " << *fp;
236 break;
237 }
238
239 case MachineOperand::MO_MachineBasicBlock:
240 O << *MO.getMBB()->getSymbol();
241 return;
242
243 case MachineOperand::MO_GlobalAddress:
244 O << *Mang->getSymbol(MO.getGlobal());
245 break;
246
247 case MachineOperand::MO_ExternalSymbol:
248 O << *GetExternalSymbolSymbol(MO.getSymbolName());
249 break;
250
251 case MachineOperand::MO_JumpTableIndex:
252 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
253 << '_' << MO.getIndex();
254 break;
255
256 case MachineOperand::MO_ConstantPoolIndex:
257 O << MAI->getPrivateGlobalPrefix() << "CPI"
258 << getFunctionNumber() << "_" << MO.getIndex();
259 if (MO.getOffset())
260 O << "+" << MO.getOffset();
261 break;
262
263 default:
264 llvm_unreachable("<unknown operand type>");
265 }
266}
267
268void MBlazeAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
269 raw_ostream &O) {
270 const MachineOperand &MO = MI->getOperand(opNum);
271 if (MO.isImm())
Wesley Peck6e749f32010-11-21 21:39:46 +0000272 O << (uint32_t)MO.getImm();
Wesley Peck4e9141f2010-10-21 03:57:26 +0000273 else
274 printOperand(MI, opNum, O);
275}
276
277void MBlazeAsmPrinter::printFSLImm(const MachineInstr *MI, int opNum,
278 raw_ostream &O) {
279 const MachineOperand &MO = MI->getOperand(opNum);
280 if (MO.isImm())
281 O << "rfsl" << (unsigned int)MO.getImm();
282 else
283 printOperand(MI, opNum, O);
284}
285
286void MBlazeAsmPrinter::
287printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
288 const char *Modifier) {
Wesley Peck4e9141f2010-10-21 03:57:26 +0000289 printOperand(MI, opNum, O);
Wesley Peck41400da2010-11-12 23:30:17 +0000290 O << ", ";
291 printOperand(MI, opNum+1, O);
Wesley Peck4e9141f2010-10-21 03:57:26 +0000292}
293
Wesley Peck82dc0402010-11-24 15:39:32 +0000294/// isBlockOnlyReachableByFallthough - Return true if the basic block has
295/// exactly one predecessor and the control transfer mechanism between
296/// the predecessor and this block is a fall-through.
297bool MBlazeAsmPrinter::
298isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
299 // If this is a landing pad, it isn't a fall through. If it has no preds,
300 // then nothing falls through to it.
301 if (MBB->isLandingPad() || MBB->pred_empty())
302 return false;
303
304 // If there isn't exactly one predecessor, it can't be a fall through.
305 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
306 ++PI2;
307 if (PI2 != MBB->pred_end())
308 return false;
309
310 // The predecessor has to be immediately before this block.
311 const MachineBasicBlock *Pred = *PI;
312
313 if (!Pred->isLayoutSuccessor(MBB))
314 return false;
315
316 // If the block is completely empty, then it definitely does fall through.
317 if (Pred->empty())
318 return true;
319
320 // Check if the last terminator is an unconditional branch.
321 MachineBasicBlock::const_iterator I = Pred->end();
322 while (I != Pred->begin() && !(--I)->getDesc().isTerminator())
323 ; // Noop
324 return I == Pred->end() || !I->getDesc().isBarrier();
325}
326
Wesley Peck4e9141f2010-10-21 03:57:26 +0000327static MCInstPrinter *createMBlazeMCInstPrinter(const Target &T,
328 unsigned SyntaxVariant,
329 const MCAsmInfo &MAI) {
330 if (SyntaxVariant == 0)
331 return new MBlazeInstPrinter(MAI);
332 return 0;
333}
334
335// Force static initialization.
336extern "C" void LLVMInitializeMBlazeAsmPrinter() {
337 RegisterAsmPrinter<MBlazeAsmPrinter> X(TheMBlazeTarget);
338 TargetRegistry::RegisterMCInstPrinter(TheMBlazeTarget,
339 createMBlazeMCInstPrinter);
340
341}