1 //===-- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly --===//
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 Hexagon assembly language. This printer is
12 // the output mechanism used by `llc'.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "asm-printer"
18 #include "HexagonAsmPrinter.h"
19 #include "HexagonMachineFunctionInfo.h"
20 #include "HexagonMCInst.h"
21 #include "HexagonTargetMachine.h"
22 #include "HexagonSubtarget.h"
23 #include "InstPrinter/HexagonInstPrinter.h"
24 #include "llvm/Constants.h"
25 #include "llvm/DerivedTypes.h"
26 #include "llvm/Module.h"
27 #include "llvm/Analysis/ConstantFolding.h"
28 #include "llvm/Assembly/Writer.h"
29 #include "llvm/CodeGen/AsmPrinter.h"
30 #include "llvm/CodeGen/MachineModuleInfo.h"
31 #include "llvm/CodeGen/MachineFunctionPass.h"
32 #include "llvm/CodeGen/MachineInstr.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/MC/MCAsmInfo.h"
35 #include "llvm/MC/MCContext.h"
36 #include "llvm/MC/MCExpr.h"
37 #include "llvm/MC/MCInst.h"
38 #include "llvm/MC/MCSection.h"
39 #include "llvm/MC/MCStreamer.h"
40 #include "llvm/MC/MCSymbol.h"
41 #include "llvm/Support/MathExtras.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/Compiler.h"
45 #include "llvm/Support/Format.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Support/TargetRegistry.h"
48 #include "llvm/Target/Mangler.h"
49 #include "llvm/Target/TargetData.h"
50 #include "llvm/Target/TargetLoweringObjectFile.h"
51 #include "llvm/Target/TargetRegisterInfo.h"
52 #include "llvm/Target/TargetInstrInfo.h"
53 #include "llvm/Target/TargetOptions.h"
54 #include "llvm/ADT/SmallString.h"
55 #include "llvm/ADT/SmallVector.h"
56 #include "llvm/ADT/StringExtras.h"
60 static cl::opt<bool> AlignCalls(
61 "hexagon-align-calls", cl::Hidden, cl::init(true),
62 cl::desc("Insert falign after call instruction for Hexagon target"));
64 void HexagonAsmPrinter::EmitAlignment(unsigned NumBits,
65 const GlobalValue *GV) const {
66 // For basic block level alignment, use ".falign".
68 OutStreamer.EmitRawText(StringRef("\t.falign"));
72 AsmPrinter::EmitAlignment(NumBits, GV);
75 void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
77 const MachineOperand &MO = MI->getOperand(OpNo);
79 switch (MO.getType()) {
80 default: llvm_unreachable ("<unknown operand type>");
81 case MachineOperand::MO_Register:
82 O << HexagonInstPrinter::getRegisterName(MO.getReg());
84 case MachineOperand::MO_Immediate:
87 case MachineOperand::MO_MachineBasicBlock:
88 O << *MO.getMBB()->getSymbol();
90 case MachineOperand::MO_JumpTableIndex:
91 O << *GetJTISymbol(MO.getIndex());
92 // FIXME: PIC relocation model.
94 case MachineOperand::MO_ConstantPoolIndex:
95 O << *GetCPISymbol(MO.getIndex());
97 case MachineOperand::MO_ExternalSymbol:
98 O << *GetExternalSymbolSymbol(MO.getSymbolName());
100 case MachineOperand::MO_GlobalAddress:
101 // Computing the address of a global symbol, not calling it.
102 O << *Mang->getSymbol(MO.getGlobal());
103 printOffset(MO.getOffset(), O);
109 // isBlockOnlyReachableByFallthrough - We need to override this since the
110 // default AsmPrinter does not print labels for any basic block that
111 // is only reachable by a fall through. That works for all cases except
112 // for the case in which the basic block is reachable by a fall through but
113 // through an indirect from a jump table. In this case, the jump table
114 // will contain a label not defined by AsmPrinter.
116 bool HexagonAsmPrinter::
117 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
118 if (MBB->hasAddressTaken()) {
121 return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
125 /// PrintAsmOperand - Print out an operand for an inline asm expression.
127 bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
129 const char *ExtraCode,
131 // Does this asm operand have a single letter operand modifier?
132 if (ExtraCode && ExtraCode[0]) {
133 if (ExtraCode[1] != 0) return true; // Unknown modifier.
135 switch (ExtraCode[0]) {
137 // See if this is a generic print operand
138 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
139 case 'c': // Don't print "$" before a global var name or constant.
140 // Hexagon never has a prefix.
141 printOperand(MI, OpNo, OS);
143 case 'L': // Write second word of DImode reference.
144 // Verify that this operand has two consecutive registers.
145 if (!MI->getOperand(OpNo).isReg() ||
146 OpNo+1 == MI->getNumOperands() ||
147 !MI->getOperand(OpNo+1).isReg())
149 ++OpNo; // Return the high-part.
152 // Write 'i' if an integer constant, otherwise nothing. Used to print
154 if (MI->getOperand(OpNo).isImm())
160 printOperand(MI, OpNo, OS);
164 bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
165 unsigned OpNo, unsigned AsmVariant,
166 const char *ExtraCode,
168 if (ExtraCode && ExtraCode[0])
169 return true; // Unknown modifier.
171 const MachineOperand &Base = MI->getOperand(OpNo);
172 const MachineOperand &Offset = MI->getOperand(OpNo+1);
175 printOperand(MI, OpNo, O);
177 llvm_unreachable("Unimplemented");
179 if (Offset.isImm()) {
181 O << " + #" << Offset.getImm();
184 llvm_unreachable("Unimplemented");
189 void HexagonAsmPrinter::printPredicateOperand(const MachineInstr *MI,
192 llvm_unreachable("Unimplemented");
196 /// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
197 /// the current output stream.
199 void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
200 if (MI->isBundle()) {
201 std::vector<const MachineInstr*> BundleMIs;
203 const MachineBasicBlock *MBB = MI->getParent();
204 MachineBasicBlock::const_instr_iterator MII = MI;
206 unsigned int IgnoreCount = 0;
207 while (MII != MBB->end() && MII->isInsideBundle()) {
208 const MachineInstr *MInst = MII;
209 if (MInst->getOpcode() == TargetOpcode::DBG_VALUE ||
210 MInst->getOpcode() == TargetOpcode::IMPLICIT_DEF) {
215 //BundleMIs.push_back(&*MII);
216 BundleMIs.push_back(MInst);
219 unsigned Size = BundleMIs.size();
220 assert((Size+IgnoreCount) == MI->getBundleSize() && "Corrupt Bundle!");
221 for (unsigned Index = 0; Index < Size; Index++) {
223 MCI.setStartPacket(Index == 0);
224 MCI.setEndPacket(Index == (Size-1));
226 HexagonLowerToMC(BundleMIs[Index], MCI, *this);
227 OutStreamer.EmitInstruction(MCI);
232 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
233 MCI.setStartPacket(true);
234 MCI.setEndPacket(true);
236 HexagonLowerToMC(MI, MCI, *this);
237 OutStreamer.EmitInstruction(MCI);
243 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
244 /// Don't print things like \n or \0.
245 // static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
246 // for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
247 // Name != E; ++Name)
248 // if (isprint(*Name))
253 void HexagonAsmPrinter::printAddrModeBasePlusOffset(const MachineInstr *MI,
254 int OpNo, raw_ostream &O) {
255 const MachineOperand &MO1 = MI->getOperand(OpNo);
256 const MachineOperand &MO2 = MI->getOperand(OpNo+1);
258 O << HexagonInstPrinter::getRegisterName(MO1.getReg())
264 void HexagonAsmPrinter::printGlobalOperand(const MachineInstr *MI, int OpNo,
266 const MachineOperand &MO = MI->getOperand(OpNo);
267 assert( (MO.getType() == MachineOperand::MO_GlobalAddress) &&
268 "Expecting global address");
270 O << *Mang->getSymbol(MO.getGlobal());
271 if (MO.getOffset() != 0) {
277 void HexagonAsmPrinter::printJumpTable(const MachineInstr *MI, int OpNo,
279 const MachineOperand &MO = MI->getOperand(OpNo);
280 assert( (MO.getType() == MachineOperand::MO_JumpTableIndex) &&
281 "Expecting jump table index");
283 // Hexagon_TODO: Do we need name mangling?
284 O << *GetJTISymbol(MO.getIndex());
287 void HexagonAsmPrinter::printConstantPool(const MachineInstr *MI, int OpNo,
289 const MachineOperand &MO = MI->getOperand(OpNo);
290 assert( (MO.getType() == MachineOperand::MO_ConstantPoolIndex) &&
291 "Expecting constant pool index");
293 // Hexagon_TODO: Do we need name mangling?
294 O << *GetCPISymbol(MO.getIndex());
297 static MCInstPrinter *createHexagonMCInstPrinter(const Target &T,
298 unsigned SyntaxVariant,
299 const MCAsmInfo &MAI,
300 const MCInstrInfo &MII,
301 const MCRegisterInfo &MRI,
302 const MCSubtargetInfo &STI) {
303 if (SyntaxVariant == 0)
304 return(new HexagonInstPrinter(MAI, MII, MRI));
309 extern "C" void LLVMInitializeHexagonAsmPrinter() {
310 RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
312 TargetRegistry::RegisterMCInstPrinter(TheHexagonTarget,
313 createHexagonMCInstPrinter);