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);
72 void EmitInstruction(const MachineInstr
*MI
);
74 } // end of anonymous namespace
76 // #include "MBlazeGenAsmWriter.inc"
78 //===----------------------------------------------------------------------===//
80 // MBlaze Asm Directives
82 // -- Frame directive "frame Stackpointer, Stacksize, RARegister"
83 // Describe the stack frame.
85 // -- Mask directives "mask bitmask, offset"
86 // Tells the assembler which registers are saved and where.
87 // bitmask - contain a little endian bitset indicating which registers are
88 // saved on function prologue (e.g. with a 0x80000000 mask, the
89 // assembler knows the register 31 (RA) is saved at prologue.
90 // offset - the position before stack pointer subtraction indicating where
91 // the first saved register on prologue is located. (e.g. with a
93 // Consider the following function prologue:
96 // .mask 0xc0000000,-8
101 // With a 0xc0000000 mask, the assembler knows the register 15 (R15) and
102 // 19 (R19) are saved at prologue. As the save order on prologue is from
103 // left to right, R15 is saved first. A -8 offset means that after the
104 // stack pointer subtration, the first register in the mask (R15) will be
105 // saved at address 48-8=40.
107 //===----------------------------------------------------------------------===//
109 //===----------------------------------------------------------------------===//
110 void MBlazeAsmPrinter::EmitInstruction(const MachineInstr
*MI
) {
111 MBlazeMCInstLower
MCInstLowering(OutContext
, *Mang
, *this);
114 MCInstLowering
.Lower(MI
, TmpInst
);
115 OutStreamer
.EmitInstruction(TmpInst
);
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));
126 // Print out an operand for an inline asm expression.
127 bool MBlazeAsmPrinter::
128 PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
129 unsigned AsmVariant
,const char *ExtraCode
, raw_ostream
&O
) {
130 // Does this asm operand have a single letter operand modifier?
131 if (ExtraCode
&& ExtraCode
[0])
132 return true; // Unknown modifier.
134 printOperand(MI
, OpNo
, O
);
138 void MBlazeAsmPrinter::printOperand(const MachineInstr
*MI
, int opNum
,
140 const MachineOperand
&MO
= MI
->getOperand(opNum
);
142 switch (MO
.getType()) {
143 case MachineOperand::MO_Register
:
144 O
<< MBlazeInstPrinter::getRegisterName(MO
.getReg());
147 case MachineOperand::MO_Immediate
:
148 O
<< (int)MO
.getImm();
151 case MachineOperand::MO_FPImmediate
: {
152 const ConstantFP
*fp
= MO
.getFPImm();
153 printHex32(fp
->getValueAPF().bitcastToAPInt().getZExtValue(), O
);
154 O
<< ";\t# immediate = " << *fp
;
158 case MachineOperand::MO_MachineBasicBlock
:
159 O
<< *MO
.getMBB()->getSymbol();
162 case MachineOperand::MO_GlobalAddress
:
163 O
<< *Mang
->getSymbol(MO
.getGlobal());
166 case MachineOperand::MO_ExternalSymbol
:
167 O
<< *GetExternalSymbolSymbol(MO
.getSymbolName());
170 case MachineOperand::MO_JumpTableIndex
:
171 O
<< MAI
->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
172 << '_' << MO
.getIndex();
175 case MachineOperand::MO_ConstantPoolIndex
:
176 O
<< MAI
->getPrivateGlobalPrefix() << "CPI"
177 << getFunctionNumber() << "_" << MO
.getIndex();
179 O
<< "+" << MO
.getOffset();
183 llvm_unreachable("<unknown operand type>");
187 void MBlazeAsmPrinter::printUnsignedImm(const MachineInstr
*MI
, int opNum
,
189 const MachineOperand
&MO
= MI
->getOperand(opNum
);
191 O
<< (unsigned int)MO
.getImm();
193 printOperand(MI
, opNum
, O
);
196 void MBlazeAsmPrinter::printFSLImm(const MachineInstr
*MI
, int opNum
,
198 const MachineOperand
&MO
= MI
->getOperand(opNum
);
200 O
<< "rfsl" << (unsigned int)MO
.getImm();
202 printOperand(MI
, opNum
, O
);
205 void MBlazeAsmPrinter::
206 printMemOperand(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
,
207 const char *Modifier
) {
208 printOperand(MI
, opNum
+1, O
);
210 printOperand(MI
, opNum
, O
);
213 static MCInstPrinter
*createMBlazeMCInstPrinter(const Target
&T
,
214 unsigned SyntaxVariant
,
215 const MCAsmInfo
&MAI
) {
216 if (SyntaxVariant
== 0)
217 return new MBlazeInstPrinter(MAI
);
221 // Force static initialization.
222 extern "C" void LLVMInitializeMBlazeAsmPrinter() {
223 RegisterAsmPrinter
<MBlazeAsmPrinter
> X(TheMBlazeTarget
);
224 TargetRegistry::RegisterMCInstPrinter(TheMBlazeTarget
,
225 createMBlazeMCInstPrinter
);