1 //===-- BlackfinAsmPrinter.cpp - Blackfin 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 BLACKFIN assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
17 #include "BlackfinInstrInfo.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Module.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCContext.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/Target/Mangler.h"
30 #include "llvm/Target/TargetData.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include "llvm/Target/TargetRegistry.h"
33 #include "llvm/ADT/SmallString.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
39 class BlackfinAsmPrinter
: public AsmPrinter
{
41 BlackfinAsmPrinter(TargetMachine
&TM
, MCStreamer
&Streamer
)
42 : AsmPrinter(TM
, Streamer
) {}
44 virtual const char *getPassName() const {
45 return "Blackfin Assembly Printer";
48 void printOperand(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
);
49 void printMemoryOperand(const MachineInstr
*MI
, int opNum
, raw_ostream
&O
);
50 void printInstruction(const MachineInstr
*MI
, raw_ostream
&O
);// autogen'd.
51 static const char *getRegisterName(unsigned RegNo
);
53 void EmitInstruction(const MachineInstr
*MI
) {
55 raw_svector_ostream
OS(Str
);
56 printInstruction(MI
, OS
);
57 OutStreamer
.EmitRawText(OS
.str());
59 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
60 unsigned AsmVariant
, const char *ExtraCode
,
62 bool PrintAsmMemoryOperand(const MachineInstr
*MI
, unsigned OpNo
,
63 unsigned AsmVariant
, const char *ExtraCode
,
66 } // end of anonymous namespace
68 #include "BlackfinGenAsmWriter.inc"
70 extern "C" void LLVMInitializeBlackfinAsmPrinter() {
71 RegisterAsmPrinter
<BlackfinAsmPrinter
> X(TheBlackfinTarget
);
74 void BlackfinAsmPrinter::printOperand(const MachineInstr
*MI
, int opNum
,
76 const MachineOperand
&MO
= MI
->getOperand(opNum
);
77 switch (MO
.getType()) {
78 case MachineOperand::MO_Register
:
79 assert(TargetRegisterInfo::isPhysicalRegister(MO
.getReg()) &&
80 "Virtual registers should be already mapped!");
81 O
<< getRegisterName(MO
.getReg());
84 case MachineOperand::MO_Immediate
:
87 case MachineOperand::MO_MachineBasicBlock
:
88 O
<< *MO
.getMBB()->getSymbol();
90 case MachineOperand::MO_GlobalAddress
:
91 O
<< *Mang
->getSymbol(MO
.getGlobal());
92 printOffset(MO
.getOffset(), O
);
94 case MachineOperand::MO_ExternalSymbol
:
95 O
<< *GetExternalSymbolSymbol(MO
.getSymbolName());
97 case MachineOperand::MO_ConstantPoolIndex
:
98 O
<< MAI
->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
101 case MachineOperand::MO_JumpTableIndex
:
102 O
<< MAI
->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
103 << '_' << MO
.getIndex();
106 llvm_unreachable("<unknown operand type>");
111 void BlackfinAsmPrinter::printMemoryOperand(const MachineInstr
*MI
, int opNum
,
113 printOperand(MI
, opNum
, O
);
115 if (MI
->getOperand(opNum
+1).isImm() && MI
->getOperand(opNum
+1).getImm() == 0)
119 printOperand(MI
, opNum
+1, O
);
122 /// PrintAsmOperand - Print out an operand for an inline asm expression.
124 bool BlackfinAsmPrinter::PrintAsmOperand(const MachineInstr
*MI
,
125 unsigned OpNo
, unsigned AsmVariant
,
126 const char *ExtraCode
,
128 if (ExtraCode
&& ExtraCode
[0]) {
129 if (ExtraCode
[1] != 0) return true; // Unknown modifier.
131 switch (ExtraCode
[0]) {
132 default: return true; // Unknown modifier.
138 printOperand(MI
, OpNo
, O
);
143 bool BlackfinAsmPrinter::PrintAsmMemoryOperand(const MachineInstr
*MI
,
146 const char *ExtraCode
,
148 if (ExtraCode
&& ExtraCode
[0])
149 return true; // Unknown modifier
152 printOperand(MI
, OpNo
, O
);