Handle ARMv6-J as an alias, instead of fake architecture
[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 "MCTargetDesc/HexagonMCShuffler.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/Analysis/ConstantFolding.h"
28 #include "llvm/CodeGen/AsmPrinter.h"
29 #include "llvm/CodeGen/MachineFunctionPass.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineModuleInfo.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/DataLayout.h"
35 #include "llvm/IR/DerivedTypes.h"
36 #include "llvm/IR/Mangler.h"
37 #include "llvm/IR/Module.h"
38 #include "llvm/MC/MCAsmInfo.h"
39 #include "llvm/MC/MCContext.h"
40 #include "llvm/MC/MCExpr.h"
41 #include "llvm/MC/MCInst.h"
42 #include "llvm/MC/MCSection.h"
43 #include "llvm/MC/MCStreamer.h"
44 #include "llvm/MC/MCSymbol.h"
45 #include "llvm/Support/CommandLine.h"
46 #include "llvm/Support/Compiler.h"
47 #include "llvm/Support/Debug.h"
48 #include "llvm/Support/Format.h"
49 #include "llvm/Support/MathExtras.h"
50 #include "llvm/Support/TargetRegistry.h"
51 #include "llvm/Support/raw_ostream.h"
52 #include "llvm/Target/TargetInstrInfo.h"
53 #include "llvm/Target/TargetLoweringObjectFile.h"
54 #include "llvm/Target/TargetOptions.h"
55 #include "llvm/Target/TargetRegisterInfo.h"
56
57 using namespace llvm;
58
59 #define DEBUG_TYPE "asm-printer"
60
61 static cl::opt<bool> AlignCalls(
62          "hexagon-align-calls", cl::Hidden, cl::init(true),
63           cl::desc("Insert falign after call instruction for Hexagon target"));
64
65 HexagonAsmPrinter::HexagonAsmPrinter(TargetMachine &TM,
66                                      std::unique_ptr<MCStreamer> Streamer)
67     : AsmPrinter(TM, std::move(Streamer)), Subtarget(nullptr) {}
68
69 void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
70                                     raw_ostream &O) {
71   const MachineOperand &MO = MI->getOperand(OpNo);
72
73   switch (MO.getType()) {
74   default: llvm_unreachable ("<unknown operand type>");
75   case MachineOperand::MO_Register:
76     O << HexagonInstPrinter::getRegisterName(MO.getReg());
77     return;
78   case MachineOperand::MO_Immediate:
79     O << MO.getImm();
80     return;
81   case MachineOperand::MO_MachineBasicBlock:
82     MO.getMBB()->getSymbol()->print(O, MAI);
83     return;
84   case MachineOperand::MO_ConstantPoolIndex:
85     GetCPISymbol(MO.getIndex())->print(O, MAI);
86     return;
87   case MachineOperand::MO_GlobalAddress:
88     // Computing the address of a global symbol, not calling it.
89     getSymbol(MO.getGlobal())->print(O, MAI);
90     printOffset(MO.getOffset(), O);
91     return;
92   }
93 }
94
95 //
96 // isBlockOnlyReachableByFallthrough - We need to override this since the
97 // default AsmPrinter does not print labels for any basic block that
98 // is only reachable by a fall through. That works for all cases except
99 // for the case in which the basic block is reachable by a fall through but
100 // through an indirect from a jump table. In this case, the jump table
101 // will contain a label not defined by AsmPrinter.
102 //
103 bool HexagonAsmPrinter::
104 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
105   if (MBB->hasAddressTaken()) {
106     return false;
107   }
108   return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
109 }
110
111
112 /// PrintAsmOperand - Print out an operand for an inline asm expression.
113 ///
114 bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
115                                         unsigned AsmVariant,
116                                         const char *ExtraCode,
117                                         raw_ostream &OS) {
118   // Does this asm operand have a single letter operand modifier?
119   if (ExtraCode && ExtraCode[0]) {
120     if (ExtraCode[1] != 0) return true; // Unknown modifier.
121
122     switch (ExtraCode[0]) {
123     default:
124       // See if this is a generic print operand
125       return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
126     case 'c': // Don't print "$" before a global var name or constant.
127       // Hexagon never has a prefix.
128       printOperand(MI, OpNo, OS);
129       return false;
130     case 'L': // Write second word of DImode reference.
131       // Verify that this operand has two consecutive registers.
132       if (!MI->getOperand(OpNo).isReg() ||
133           OpNo+1 == MI->getNumOperands() ||
134           !MI->getOperand(OpNo+1).isReg())
135         return true;
136       ++OpNo;   // Return the high-part.
137       break;
138     case 'I':
139       // Write 'i' if an integer constant, otherwise nothing.  Used to print
140       // addi vs add, etc.
141       if (MI->getOperand(OpNo).isImm())
142         OS << "i";
143       return false;
144     }
145   }
146
147   printOperand(MI, OpNo, OS);
148   return false;
149 }
150
151 bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
152                                             unsigned OpNo, unsigned AsmVariant,
153                                             const char *ExtraCode,
154                                             raw_ostream &O) {
155   if (ExtraCode && ExtraCode[0])
156     return true; // Unknown modifier.
157
158   const MachineOperand &Base  = MI->getOperand(OpNo);
159   const MachineOperand &Offset = MI->getOperand(OpNo+1);
160
161   if (Base.isReg())
162     printOperand(MI, OpNo, O);
163   else
164     llvm_unreachable("Unimplemented");
165
166   if (Offset.isImm()) {
167     if (Offset.getImm())
168       O << " + #" << Offset.getImm();
169   }
170   else
171     llvm_unreachable("Unimplemented");
172
173   return false;
174 }
175
176
177 /// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
178 /// the current output stream.
179 ///
180 void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
181   MCInst MCB = HexagonMCInstrInfo::createBundle();
182
183   if (MI->isBundle()) {
184     const MachineBasicBlock* MBB = MI->getParent();
185     MachineBasicBlock::const_instr_iterator MII = MI->getIterator();
186     unsigned IgnoreCount = 0;
187
188     for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII) {
189       if (MII->getOpcode() == TargetOpcode::DBG_VALUE ||
190           MII->getOpcode() == TargetOpcode::IMPLICIT_DEF)
191         ++IgnoreCount;
192       else {
193         HexagonLowerToMC(&*MII, MCB, *this);
194       }
195     }
196   }
197   else {
198     HexagonLowerToMC(MI, MCB, *this);
199     HexagonMCInstrInfo::padEndloop(OutStreamer->getContext(), MCB);
200   }
201   // Examine the packet and try to find instructions that can be converted
202   // to compounds.
203   HexagonMCInstrInfo::tryCompound(*Subtarget->getInstrInfo(),
204                                   OutStreamer->getContext(), MCB);
205   // Examine the packet and convert pairs of instructions to duplex
206   // instructions when possible.
207   SmallVector<DuplexCandidate, 8> possibleDuplexes;
208   possibleDuplexes = HexagonMCInstrInfo::getDuplexPossibilties(
209       *Subtarget->getInstrInfo(), MCB);
210   HexagonMCShuffle(*Subtarget->getInstrInfo(), *Subtarget,
211                    OutStreamer->getContext(), MCB, possibleDuplexes);
212   EmitToStreamer(*OutStreamer, MCB);
213 }
214
215 extern "C" void LLVMInitializeHexagonAsmPrinter() {
216   RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
217 }