Revert r239095 incorrect test tree.
[oota-llvm.git] / lib / Target / Hexagon / HexagonAsmPrinter.cpp
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
64 HexagonAsmPrinter::HexagonAsmPrinter(TargetMachine &TM,
65                                      std::unique_ptr<MCStreamer> Streamer)
66     : AsmPrinter(TM, std::move(Streamer)), Subtarget(nullptr) {}
67
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::
103 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 ///
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
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 ///
179 void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
180   MCInst MCB;
181   MCB.setOpcode(Hexagon::BUNDLE);
182   MCB.addOperand(MCOperand::createImm(0));
183
184   if (MI->isBundle()) {
185     const MachineBasicBlock* MBB = MI->getParent();
186     MachineBasicBlock::const_instr_iterator MII = MI;
187     unsigned IgnoreCount = 0;
188
189     for (++MII; MII != MBB->end() && MII->isInsideBundle(); ++MII) {
190       if (MII->getOpcode() == TargetOpcode::DBG_VALUE ||
191           MII->getOpcode() == TargetOpcode::IMPLICIT_DEF)
192         ++IgnoreCount;
193       else {
194         HexagonLowerToMC(MII, MCB, *this);
195       }
196     }
197   }
198   else {
199     HexagonLowerToMC(MI, MCB, *this);
200     HexagonMCInstrInfo::padEndloop(MCB);
201   }
202   EmitToStreamer(*OutStreamer, MCB);
203
204   return;
205 }
206
207 extern "C" void LLVMInitializeHexagonAsmPrinter() {
208   RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
209 }