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 //===----------------------------------------------------------------------===//
17 #include "HexagonAsmPrinter.h"
18 #include "HexagonMachineFunctionInfo.h"
19 #include "HexagonSubtarget.h"
20 #include "HexagonTargetMachine.h"
21 #include "MCTargetDesc/HexagonInstPrinter.h"
22 #include "MCTargetDesc/HexagonMCInst.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/Analysis/ConstantFolding.h"
27 #include "llvm/CodeGen/AsmPrinter.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/MachineModuleInfo.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/DataLayout.h"
34 #include "llvm/IR/DerivedTypes.h"
35 #include "llvm/IR/Mangler.h"
36 #include "llvm/IR/Module.h"
37 #include "llvm/MC/MCAsmInfo.h"
38 #include "llvm/MC/MCContext.h"
39 #include "llvm/MC/MCExpr.h"
40 #include "llvm/MC/MCInst.h"
41 #include "llvm/MC/MCSection.h"
42 #include "llvm/MC/MCStreamer.h"
43 #include "llvm/MC/MCSymbol.h"
44 #include "llvm/Support/CommandLine.h"
45 #include "llvm/Support/Compiler.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Support/Format.h"
48 #include "llvm/Support/MathExtras.h"
49 #include "llvm/Support/TargetRegistry.h"
50 #include "llvm/Support/raw_ostream.h"
51 #include "llvm/Target/TargetInstrInfo.h"
52 #include "llvm/Target/TargetLoweringObjectFile.h"
53 #include "llvm/Target/TargetOptions.h"
54 #include "llvm/Target/TargetRegisterInfo.h"
58 #define DEBUG_TYPE "asm-printer"
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 HexagonAsmPrinter::HexagonAsmPrinter(TargetMachine &TM,
65 std::unique_ptr<MCStreamer> Streamer)
66 : AsmPrinter(TM, std::move(Streamer)) {
67 Subtarget = &TM.getSubtarget<HexagonSubtarget>();
70 void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
72 const MachineOperand &MO = MI->getOperand(OpNo);
74 switch (MO.getType()) {
75 default: llvm_unreachable ("<unknown operand type>");
76 case MachineOperand::MO_Register:
77 O << HexagonInstPrinter::getRegisterName(MO.getReg());
79 case MachineOperand::MO_Immediate:
82 case MachineOperand::MO_MachineBasicBlock:
83 O << *MO.getMBB()->getSymbol();
85 case MachineOperand::MO_ConstantPoolIndex:
86 O << *GetCPISymbol(MO.getIndex());
88 case MachineOperand::MO_GlobalAddress:
89 // Computing the address of a global symbol, not calling it.
90 O << *getSymbol(MO.getGlobal());
91 printOffset(MO.getOffset(), O);
97 // isBlockOnlyReachableByFallthrough - We need to override this since the
98 // default AsmPrinter does not print labels for any basic block that
99 // is only reachable by a fall through. That works for all cases except
100 // for the case in which the basic block is reachable by a fall through but
101 // through an indirect from a jump table. In this case, the jump table
102 // will contain a label not defined by AsmPrinter.
104 bool HexagonAsmPrinter::
105 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
106 if (MBB->hasAddressTaken()) {
109 return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
113 /// PrintAsmOperand - Print out an operand for an inline asm expression.
115 bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
117 const char *ExtraCode,
119 // Does this asm operand have a single letter operand modifier?
120 if (ExtraCode && ExtraCode[0]) {
121 if (ExtraCode[1] != 0) return true; // Unknown modifier.
123 switch (ExtraCode[0]) {
125 // See if this is a generic print operand
126 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
127 case 'c': // Don't print "$" before a global var name or constant.
128 // Hexagon never has a prefix.
129 printOperand(MI, OpNo, OS);
131 case 'L': // Write second word of DImode reference.
132 // Verify that this operand has two consecutive registers.
133 if (!MI->getOperand(OpNo).isReg() ||
134 OpNo+1 == MI->getNumOperands() ||
135 !MI->getOperand(OpNo+1).isReg())
137 ++OpNo; // Return the high-part.
140 // Write 'i' if an integer constant, otherwise nothing. Used to print
142 if (MI->getOperand(OpNo).isImm())
148 printOperand(MI, OpNo, OS);
152 bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
153 unsigned OpNo, unsigned AsmVariant,
154 const char *ExtraCode,
156 if (ExtraCode && ExtraCode[0])
157 return true; // Unknown modifier.
159 const MachineOperand &Base = MI->getOperand(OpNo);
160 const MachineOperand &Offset = MI->getOperand(OpNo+1);
163 printOperand(MI, OpNo, O);
165 llvm_unreachable("Unimplemented");
167 if (Offset.isImm()) {
169 O << " + #" << Offset.getImm();
172 llvm_unreachable("Unimplemented");
178 /// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
179 /// the current output stream.
181 void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
182 if (MI->isBundle()) {
183 std::vector<MachineInstr const *> BundleMIs;
185 const MachineBasicBlock *MBB = MI->getParent();
186 MachineBasicBlock::const_instr_iterator MII = MI;
188 unsigned int IgnoreCount = 0;
189 while (MII != MBB->end() && MII->isInsideBundle()) {
190 const MachineInstr *MInst = MII;
191 if (MInst->getOpcode() == TargetOpcode::DBG_VALUE ||
192 MInst->getOpcode() == TargetOpcode::IMPLICIT_DEF) {
197 // BundleMIs.push_back(&*MII);
198 BundleMIs.push_back(MInst);
201 unsigned Size = BundleMIs.size();
202 assert((Size + IgnoreCount) == MI->getBundleSize() && "Corrupt Bundle!");
203 for (unsigned Index = 0; Index < Size; Index++) {
206 HexagonLowerToMC(BundleMIs[Index], MCI, *this);
207 HexagonMCInst::AppendImplicitOperands(MCI);
208 MCI.setPacketBegin(Index == 0);
209 MCI.setPacketEnd(Index == (Size - 1));
210 EmitToStreamer(OutStreamer, MCI);
215 HexagonLowerToMC(MI, MCI, *this);
216 HexagonMCInst::AppendImplicitOperands(MCI);
217 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
218 MCI.setPacketBegin(true);
219 MCI.setPacketEnd(true);
221 EmitToStreamer(OutStreamer, MCI);
227 static MCInstPrinter *createHexagonMCInstPrinter(const Target &T,
228 unsigned SyntaxVariant,
229 const MCAsmInfo &MAI,
230 const MCInstrInfo &MII,
231 const MCRegisterInfo &MRI,
232 const MCSubtargetInfo &STI) {
233 if (SyntaxVariant == 0)
234 return(new HexagonInstPrinter(MAI, MII, MRI));
239 extern "C" void LLVMInitializeHexagonAsmPrinter() {
240 RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
242 TargetRegistry::RegisterMCInstPrinter(TheHexagonTarget,
243 createHexagonMCInstPrinter);