blob: c1ae4502ad55f0235d42b63ace6dd87e447b69cc [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
63 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
64 unsigned AsmVariant, const char *ExtraCode,
65 raw_ostream &O);
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);
72
73 void emitFrameDirective();
74
75 void EmitInstruction(const MachineInstr *MI);
76 virtual void EmitFunctionBodyStart();
77 virtual void EmitFunctionBodyEnd();
78
79 virtual void EmitFunctionEntryLabel();
80 };
81} // end of anonymous namespace
82
83// #include "MBlazeGenAsmWriter.inc"
84
85//===----------------------------------------------------------------------===//
86//
87// MBlaze Asm Directives
88//
89// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
90// Describe the stack frame.
91//
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
99//
100// Consider the following function prologue:
101//
102// .frame R19,48,R15
103// .mask 0xc0000000,-8
104// addiu R1, R1, -48
105// sw R15, 40(R1)
106// sw R19, 36(R1)
107//
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.
113//
114//===----------------------------------------------------------------------===//
115
116//===----------------------------------------------------------------------===//
117void MBlazeAsmPrinter::EmitInstruction(const MachineInstr *MI) {
118 MBlazeMCInstLower MCInstLowering(OutContext, *Mang, *this);
119
120 MCInst TmpInst;
121 MCInstLowering.Lower(MI, TmpInst);
122 OutStreamer.EmitInstruction(TmpInst);
123}
124
125//===----------------------------------------------------------------------===//
126// Mask directives
127//===----------------------------------------------------------------------===//
128
129// Print a 32 bit hex number with all numbers.
130static void printHex32(unsigned int Value, raw_ostream &O) {
131 O << "0x";
132 for (int i = 7; i >= 0; i--)
133 O << utohexstr((Value & (0xF << (i*4))) >> (i*4));
134}
135
136
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.
139void MBlazeAsmPrinter::printSavedRegsBitmask(raw_ostream &O) {
140 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
141 const MBlazeFunctionInfo *MBlazeFI = MF->getInfo<MBlazeFunctionInfo>();
142
143 // CPU Saved Registers Bitmasks
144 unsigned int CPUBitmask = 0;
145
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);
154 }
155
156 // Return Address and Frame registers must also be set in CPUBitmask.
157 if (RI.hasFP(*MF))
158 CPUBitmask |= (1 << MBlazeRegisterInfo::
159 getRegisterNumbering(RI.getFrameRegister(*MF)));
160
161 if (MFI->adjustsStack())
162 CPUBitmask |= (1 << MBlazeRegisterInfo::
163 getRegisterNumbering(RI.getRARegister()));
164
165 // Print CPUBitmask
166 O << "\t.mask \t"; printHex32(CPUBitmask, O);
167 O << ',' << MBlazeFI->getCPUTopSavedRegOff() << '\n';
168}
169
170//===----------------------------------------------------------------------===//
171// Frame and Set directives
172//===----------------------------------------------------------------------===//
173
174/// Frame Directive
175void MBlazeAsmPrinter::emitFrameDirective() {
176 // const TargetRegisterInfo &RI = *TM.getRegisterInfo();
177
178 // unsigned stackReg = RI.getFrameRegister(*MF);
179 // unsigned returnReg = RI.getRARegister();
180 // unsigned stackSize = MF->getFrameInfo()->getStackSize();
181
182
183 /*
184 OutStreamer.EmitRawText("\t.frame\t" +
185 Twine(MBlazeInstPrinter::getRegisterName(stackReg)) +
186 "," + Twine(stackSize) + "," +
187 Twine(MBlazeInstPrinter::getRegisterName(returnReg)));
188 */
189}
190
191void MBlazeAsmPrinter::EmitFunctionEntryLabel() {
192 // OutStreamer.EmitRawText("\t.ent\t" + Twine(CurrentFnSym->getName()));
193 OutStreamer.EmitLabel(CurrentFnSym);
194}
195
196/// EmitFunctionBodyStart - Targets can override this to emit stuff before
197/// the first basic block in the function.
198void MBlazeAsmPrinter::EmitFunctionBodyStart() {
199 // emitFrameDirective();
200
201 // SmallString<128> Str;
202 // raw_svector_ostream OS(Str);
203 // printSavedRegsBitmask(OS);
204 // OutStreamer.EmitRawText(OS.str());
205}
206
207/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
208/// the last basic block in the function.
209void MBlazeAsmPrinter::EmitFunctionBodyEnd() {
210 // OutStreamer.EmitRawText("\t.end\t" + Twine(CurrentFnSym->getName()));
211}
212
213// Print out an operand for an inline asm expression.
214bool MBlazeAsmPrinter::
215PrintAsmOperand(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.
220
221 printOperand(MI, OpNo, O);
222 return false;
223}
224
225void MBlazeAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
226 raw_ostream &O) {
227 const MachineOperand &MO = MI->getOperand(opNum);
228
229 switch (MO.getType()) {
230 case MachineOperand::MO_Register:
231 O << MBlazeInstPrinter::getRegisterName(MO.getReg());
232 break;
233
234 case MachineOperand::MO_Immediate:
235 O << (int)MO.getImm();
236 break;
237
238 case MachineOperand::MO_FPImmediate: {
239 const ConstantFP *fp = MO.getFPImm();
240 printHex32(fp->getValueAPF().bitcastToAPInt().getZExtValue(), O);
241 O << ";\t# immediate = " << *fp;
242 break;
243 }
244
245 case MachineOperand::MO_MachineBasicBlock:
246 O << *MO.getMBB()->getSymbol();
247 return;
248
249 case MachineOperand::MO_GlobalAddress:
250 O << *Mang->getSymbol(MO.getGlobal());
251 break;
252
253 case MachineOperand::MO_ExternalSymbol:
254 O << *GetExternalSymbolSymbol(MO.getSymbolName());
255 break;
256
257 case MachineOperand::MO_JumpTableIndex:
258 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
259 << '_' << MO.getIndex();
260 break;
261
262 case MachineOperand::MO_ConstantPoolIndex:
263 O << MAI->getPrivateGlobalPrefix() << "CPI"
264 << getFunctionNumber() << "_" << MO.getIndex();
265 if (MO.getOffset())
266 O << "+" << MO.getOffset();
267 break;
268
269 default:
270 llvm_unreachable("<unknown operand type>");
271 }
272}
273
274void MBlazeAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
275 raw_ostream &O) {
276 const MachineOperand &MO = MI->getOperand(opNum);
277 if (MO.isImm())
278 O << (unsigned int)MO.getImm();
279 else
280 printOperand(MI, opNum, O);
281}
282
283void MBlazeAsmPrinter::printFSLImm(const MachineInstr *MI, int opNum,
284 raw_ostream &O) {
285 const MachineOperand &MO = MI->getOperand(opNum);
286 if (MO.isImm())
287 O << "rfsl" << (unsigned int)MO.getImm();
288 else
289 printOperand(MI, opNum, O);
290}
291
292void MBlazeAsmPrinter::
293printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
294 const char *Modifier) {
295 printOperand(MI, opNum+1, O);
296 O << ", ";
297 printOperand(MI, opNum, O);
298}
299
300static MCInstPrinter *createMBlazeMCInstPrinter(const Target &T,
301 unsigned SyntaxVariant,
302 const MCAsmInfo &MAI) {
303 if (SyntaxVariant == 0)
304 return new MBlazeInstPrinter(MAI);
305 return 0;
306}
307
308// Force static initialization.
309extern "C" void LLVMInitializeMBlazeAsmPrinter() {
310 RegisterAsmPrinter<MBlazeAsmPrinter> X(TheMBlazeTarget);
311 TargetRegistry::RegisterMCInstPrinter(TheMBlazeTarget,
312 createMBlazeMCInstPrinter);
313
314}