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 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
64 unsigned AsmVariant, const char *ExtraCode,
66 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
67 void printUnsignedImm(const MachineInstr *MI, int opNum, raw_ostream &O);
68 void printFSLImm(const MachineInstr *MI, int opNum, raw_ostream &O);
69 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
70 const char *Modifier = 0);
71 void printSavedRegsBitmask(raw_ostream &OS);
73 void emitFrameDirective();
75 void EmitInstruction(const MachineInstr *MI);
76 virtual void EmitFunctionBodyStart();
77 virtual void EmitFunctionBodyEnd();
79 virtual void EmitFunctionEntryLabel();
81 } // end of anonymous namespace
83 // #include "MBlazeGenAsmWriter.inc"
85 //===----------------------------------------------------------------------===//
87 // MBlaze Asm Directives
89 // -- Frame directive "frame Stackpointer, Stacksize, RARegister"
90 // Describe the stack frame.
92 // -- Mask directives "mask bitmask, offset"
93 // Tells the assembler which registers are saved and where.
94 // bitmask - contain a little endian bitset indicating which registers are
95 // saved on function prologue (e.g. with a 0x80000000 mask, the
96 // assembler knows the register 31 (RA) is saved at prologue.
97 // offset - the position before stack pointer subtraction indicating where
98 // the first saved register on prologue is located. (e.g. with a
100 // Consider the following function prologue:
103 // .mask 0xc0000000,-8
108 // With a 0xc0000000 mask, the assembler knows the register 15 (R15) and
109 // 19 (R19) are saved at prologue. As the save order on prologue is from
110 // left to right, R15 is saved first. A -8 offset means that after the
111 // stack pointer subtration, the first register in the mask (R15) will be
112 // saved at address 48-8=40.
114 //===----------------------------------------------------------------------===//
116 //===----------------------------------------------------------------------===//
117 void MBlazeAsmPrinter::EmitInstruction(const MachineInstr *MI) {
118 MBlazeMCInstLower MCInstLowering(OutContext, *Mang, *this);
121 MCInstLowering.Lower(MI, TmpInst);
122 OutStreamer.EmitInstruction(TmpInst);
125 //===----------------------------------------------------------------------===//
127 //===----------------------------------------------------------------------===//
129 // Print a 32 bit hex number with all numbers.
130 static void printHex32(unsigned int Value, raw_ostream &O) {
132 for (int i = 7; i >= 0; i--)
133 O << utohexstr((Value & (0xF << (i*4))) >> (i*4));
137 // Create a bitmask with all callee saved registers for CPU or Floating Point
138 // registers. For CPU registers consider RA, GP and FP for saving if necessary.
139 void MBlazeAsmPrinter::printSavedRegsBitmask(raw_ostream &O) {
140 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
141 const MBlazeFunctionInfo *MBlazeFI = MF->getInfo<MBlazeFunctionInfo>();
143 // CPU Saved Registers Bitmasks
144 unsigned int CPUBitmask = 0;
146 // Set the CPU Bitmasks
147 const MachineFrameInfo *MFI = MF->getFrameInfo();
148 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
149 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
150 unsigned Reg = CSI[i].getReg();
151 unsigned RegNum = MBlazeRegisterInfo::getRegisterNumbering(Reg);
152 if (MBlaze::CPURegsRegisterClass->contains(Reg))
153 CPUBitmask |= (1 << RegNum);
156 // Return Address and Frame registers must also be set in CPUBitmask.
158 CPUBitmask |= (1 << MBlazeRegisterInfo::
159 getRegisterNumbering(RI.getFrameRegister(*MF)));
161 if (MFI->adjustsStack())
162 CPUBitmask |= (1 << MBlazeRegisterInfo::
163 getRegisterNumbering(RI.getRARegister()));
166 O << "\t.mask \t"; printHex32(CPUBitmask, O);
167 O << ',' << MBlazeFI->getCPUTopSavedRegOff() << '\n';
170 //===----------------------------------------------------------------------===//
171 // Frame and Set directives
172 //===----------------------------------------------------------------------===//
175 void MBlazeAsmPrinter::emitFrameDirective() {
176 // const TargetRegisterInfo &RI = *TM.getRegisterInfo();
178 // unsigned stackReg = RI.getFrameRegister(*MF);
179 // unsigned returnReg = RI.getRARegister();
180 // unsigned stackSize = MF->getFrameInfo()->getStackSize();
184 OutStreamer.EmitRawText("\t.frame\t" +
185 Twine(MBlazeInstPrinter::getRegisterName(stackReg)) +
186 "," + Twine(stackSize) + "," +
187 Twine(MBlazeInstPrinter::getRegisterName(returnReg)));
191 void MBlazeAsmPrinter::EmitFunctionEntryLabel() {
192 // OutStreamer.EmitRawText("\t.ent\t" + Twine(CurrentFnSym->getName()));
193 OutStreamer.EmitLabel(CurrentFnSym);
196 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
197 /// the first basic block in the function.
198 void MBlazeAsmPrinter::EmitFunctionBodyStart() {
199 // emitFrameDirective();
201 // SmallString<128> Str;
202 // raw_svector_ostream OS(Str);
203 // printSavedRegsBitmask(OS);
204 // OutStreamer.EmitRawText(OS.str());
207 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
208 /// the last basic block in the function.
209 void MBlazeAsmPrinter::EmitFunctionBodyEnd() {
210 // OutStreamer.EmitRawText("\t.end\t" + Twine(CurrentFnSym->getName()));
213 // Print out an operand for an inline asm expression.
214 bool MBlazeAsmPrinter::
215 PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
216 unsigned AsmVariant,const char *ExtraCode, raw_ostream &O) {
217 // Does this asm operand have a single letter operand modifier?
218 if (ExtraCode && ExtraCode[0])
219 return true; // Unknown modifier.
221 printOperand(MI, OpNo, O);
225 void MBlazeAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
227 const MachineOperand &MO = MI->getOperand(opNum);
229 switch (MO.getType()) {
230 case MachineOperand::MO_Register:
231 O << MBlazeInstPrinter::getRegisterName(MO.getReg());
234 case MachineOperand::MO_Immediate:
235 O << (int)MO.getImm();
238 case MachineOperand::MO_FPImmediate: {
239 const ConstantFP *fp = MO.getFPImm();
240 printHex32(fp->getValueAPF().bitcastToAPInt().getZExtValue(), O);
241 O << ";\t# immediate = " << *fp;
245 case MachineOperand::MO_MachineBasicBlock:
246 O << *MO.getMBB()->getSymbol();
249 case MachineOperand::MO_GlobalAddress:
250 O << *Mang->getSymbol(MO.getGlobal());
253 case MachineOperand::MO_ExternalSymbol:
254 O << *GetExternalSymbolSymbol(MO.getSymbolName());
257 case MachineOperand::MO_JumpTableIndex:
258 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
259 << '_' << MO.getIndex();
262 case MachineOperand::MO_ConstantPoolIndex:
263 O << MAI->getPrivateGlobalPrefix() << "CPI"
264 << getFunctionNumber() << "_" << MO.getIndex();
266 O << "+" << MO.getOffset();
270 llvm_unreachable("<unknown operand type>");
274 void MBlazeAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
276 const MachineOperand &MO = MI->getOperand(opNum);
278 O << (unsigned int)MO.getImm();
280 printOperand(MI, opNum, O);
283 void MBlazeAsmPrinter::printFSLImm(const MachineInstr *MI, int opNum,
285 const MachineOperand &MO = MI->getOperand(opNum);
287 O << "rfsl" << (unsigned int)MO.getImm();
289 printOperand(MI, opNum, O);
292 void MBlazeAsmPrinter::
293 printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
294 const char *Modifier) {
295 printOperand(MI, opNum+1, O);
297 printOperand(MI, opNum, O);
300 static MCInstPrinter *createMBlazeMCInstPrinter(const Target &T,
301 unsigned SyntaxVariant,
302 const MCAsmInfo &MAI) {
303 if (SyntaxVariant == 0)
304 return new MBlazeInstPrinter(MAI);
308 // Force static initialization.
309 extern "C" void LLVMInitializeMBlazeAsmPrinter() {
310 RegisterAsmPrinter<MBlazeAsmPrinter> X(TheMBlazeTarget);
311 TargetRegistry::RegisterMCInstPrinter(TheMBlazeTarget,
312 createMBlazeMCInstPrinter);