• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly --===//
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 Hexagon assembly language. This printer is
12 // the output mechanism used by `llc'.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "Hexagon.h"
17 #include "HexagonAsmPrinter.h"
18 #include "HexagonMachineFunctionInfo.h"
19 #include "HexagonSubtarget.h"
20 #include "HexagonTargetMachine.h"
21 #include "MCTargetDesc/HexagonInstPrinter.h"
22 #include "MCTargetDesc/HexagonMCInstrInfo.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"
55 
56 using namespace llvm;
57 
58 #define DEBUG_TYPE "asm-printer"
59 
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"));
63 
HexagonAsmPrinter(TargetMachine & TM,std::unique_ptr<MCStreamer> Streamer)64 HexagonAsmPrinter::HexagonAsmPrinter(TargetMachine &TM,
65                                      std::unique_ptr<MCStreamer> Streamer)
66     : AsmPrinter(TM, std::move(Streamer)), Subtarget(nullptr) {}
67 
printOperand(const MachineInstr * MI,unsigned OpNo,raw_ostream & O)68 void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
69                                     raw_ostream &O) {
70   const MachineOperand &MO = MI->getOperand(OpNo);
71 
72   switch (MO.getType()) {
73   default: llvm_unreachable ("<unknown operand type>");
74   case MachineOperand::MO_Register:
75     O << HexagonInstPrinter::getRegisterName(MO.getReg());
76     return;
77   case MachineOperand::MO_Immediate:
78     O << MO.getImm();
79     return;
80   case MachineOperand::MO_MachineBasicBlock:
81     O << *MO.getMBB()->getSymbol();
82     return;
83   case MachineOperand::MO_ConstantPoolIndex:
84     O << *GetCPISymbol(MO.getIndex());
85     return;
86   case MachineOperand::MO_GlobalAddress:
87     // Computing the address of a global symbol, not calling it.
88     O << *getSymbol(MO.getGlobal());
89     printOffset(MO.getOffset(), O);
90     return;
91   }
92 }
93 
94 //
95 // isBlockOnlyReachableByFallthrough - We need to override this since the
96 // default AsmPrinter does not print labels for any basic block that
97 // is only reachable by a fall through. That works for all cases except
98 // for the case in which the basic block is reachable by a fall through but
99 // through an indirect from a jump table. In this case, the jump table
100 // will contain a label not defined by AsmPrinter.
101 //
102 bool HexagonAsmPrinter::
isBlockOnlyReachableByFallthrough(const MachineBasicBlock * MBB) const103 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
104   if (MBB->hasAddressTaken()) {
105     return false;
106   }
107   return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
108 }
109 
110 
111 /// PrintAsmOperand - Print out an operand for an inline asm expression.
112 ///
PrintAsmOperand(const MachineInstr * MI,unsigned OpNo,unsigned AsmVariant,const char * ExtraCode,raw_ostream & OS)113 bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
114                                         unsigned AsmVariant,
115                                         const char *ExtraCode,
116                                         raw_ostream &OS) {
117   // Does this asm operand have a single letter operand modifier?
118   if (ExtraCode && ExtraCode[0]) {
119     if (ExtraCode[1] != 0) return true; // Unknown modifier.
120 
121     switch (ExtraCode[0]) {
122     default:
123       // See if this is a generic print operand
124       return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
125     case 'c': // Don't print "$" before a global var name or constant.
126       // Hexagon never has a prefix.
127       printOperand(MI, OpNo, OS);
128       return false;
129     case 'L': // Write second word of DImode reference.
130       // Verify that this operand has two consecutive registers.
131       if (!MI->getOperand(OpNo).isReg() ||
132           OpNo+1 == MI->getNumOperands() ||
133           !MI->getOperand(OpNo+1).isReg())
134         return true;
135       ++OpNo;   // Return the high-part.
136       break;
137     case 'I':
138       // Write 'i' if an integer constant, otherwise nothing.  Used to print
139       // addi vs add, etc.
140       if (MI->getOperand(OpNo).isImm())
141         OS << "i";
142       return false;
143     }
144   }
145 
146   printOperand(MI, OpNo, OS);
147   return false;
148 }
149 
PrintAsmMemoryOperand(const MachineInstr * MI,unsigned OpNo,unsigned AsmVariant,const char * ExtraCode,raw_ostream & O)150 bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
151                                             unsigned OpNo, unsigned AsmVariant,
152                                             const char *ExtraCode,
153                                             raw_ostream &O) {
154   if (ExtraCode && ExtraCode[0])
155     return true; // Unknown modifier.
156 
157   const MachineOperand &Base  = MI->getOperand(OpNo);
158   const MachineOperand &Offset = MI->getOperand(OpNo+1);
159 
160   if (Base.isReg())
161     printOperand(MI, OpNo, O);
162   else
163     llvm_unreachable("Unimplemented");
164 
165   if (Offset.isImm()) {
166     if (Offset.getImm())
167       O << " + #" << Offset.getImm();
168   }
169   else
170     llvm_unreachable("Unimplemented");
171 
172   return false;
173 }
174 
175 
176 /// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
177 /// the current output stream.
178 ///
EmitInstruction(const MachineInstr * MI)179 void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
180   if (MI->isBundle()) {
181     std::vector<MachineInstr const *> BundleMIs;
182 
183     const MachineBasicBlock *MBB = MI->getParent();
184     MachineBasicBlock::const_instr_iterator MII = MI;
185     ++MII;
186     unsigned int IgnoreCount = 0;
187     while (MII != MBB->end() && MII->isInsideBundle()) {
188       const MachineInstr *MInst = MII;
189       if (MInst->getOpcode() == TargetOpcode::DBG_VALUE ||
190         MInst->getOpcode() == TargetOpcode::IMPLICIT_DEF) {
191         IgnoreCount++;
192         ++MII;
193         continue;
194       }
195       // BundleMIs.push_back(&*MII);
196       BundleMIs.push_back(MInst);
197       ++MII;
198     }
199     unsigned Size = BundleMIs.size();
200     assert((Size + IgnoreCount) == MI->getBundleSize() && "Corrupt Bundle!");
201     for (unsigned Index = 0; Index < Size; Index++) {
202       MCInst MCI;
203 
204       HexagonLowerToMC(BundleMIs[Index], MCI, *this);
205       HexagonMCInstrInfo::AppendImplicitOperands(MCI);
206       HexagonMCInstrInfo::setPacketBegin(MCI, Index == 0);
207       HexagonMCInstrInfo::setPacketEnd(MCI, Index == (Size - 1));
208       EmitToStreamer(OutStreamer, MCI);
209     }
210   }
211   else {
212     MCInst MCI;
213     HexagonLowerToMC(MI, MCI, *this);
214     HexagonMCInstrInfo::AppendImplicitOperands(MCI);
215     if (MI->getOpcode() == Hexagon::ENDLOOP0) {
216       HexagonMCInstrInfo::setPacketBegin(MCI, true);
217       HexagonMCInstrInfo::setPacketEnd(MCI, true);
218     }
219     EmitToStreamer(OutStreamer, MCI);
220   }
221 
222   return;
223 }
224 
LLVMInitializeHexagonAsmPrinter()225 extern "C" void LLVMInitializeHexagonAsmPrinter() {
226   RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
227 }
228