Silence various build warnings from Hexagon backend that show up in release builds...
[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 #define DEBUG_TYPE "asm-printer"
17 #include "Hexagon.h"
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"
57 #include <map>
58
59 using namespace llvm;
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 void HexagonAsmPrinter::EmitAlignment(unsigned NumBits,
66                                       const GlobalValue *GV) const {
67   // For basic block level alignment, use ".falign".
68   if (!GV) {
69     OutStreamer.EmitRawText(StringRef("\t.falign"));
70     return;
71   }
72
73   AsmPrinter::EmitAlignment(NumBits, GV);
74 }
75
76 void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
77                                     raw_ostream &O) {
78   const MachineOperand &MO = MI->getOperand(OpNo);
79
80   switch (MO.getType()) {
81   default: llvm_unreachable("<unknown operand type>");
82   case MachineOperand::MO_Register:
83     O << HexagonInstPrinter::getRegisterName(MO.getReg());
84     return;
85   case MachineOperand::MO_Immediate:
86     O << MO.getImm();
87     return;
88   case MachineOperand::MO_MachineBasicBlock:
89     O << *MO.getMBB()->getSymbol();
90     return;
91   case MachineOperand::MO_JumpTableIndex:
92     O << *GetJTISymbol(MO.getIndex());
93     // FIXME: PIC relocation model.
94     return;
95   case MachineOperand::MO_ConstantPoolIndex:
96     O << *GetCPISymbol(MO.getIndex());
97     return;
98   case MachineOperand::MO_ExternalSymbol:
99     O << *GetExternalSymbolSymbol(MO.getSymbolName());
100     return;
101   case MachineOperand::MO_GlobalAddress:
102     // Computing the address of a global symbol, not calling it.
103     O << *Mang->getSymbol(MO.getGlobal());
104     printOffset(MO.getOffset(), O);
105     return;
106   }
107 }
108
109 //
110 // isBlockOnlyReachableByFallthrough - We need to override this since the
111 // default AsmPrinter does not print labels for any basic block that
112 // is only reachable by a fall through. That works for all cases except
113 // for the case in which the basic block is reachable by a fall through but
114 // through an indirect from a jump table. In this case, the jump table
115 // will contain a label not defined by AsmPrinter.
116 //
117 bool HexagonAsmPrinter::
118 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
119   if (MBB->hasAddressTaken()) {
120     return false;
121   }
122   return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
123 }
124
125
126 /// PrintAsmOperand - Print out an operand for an inline asm expression.
127 ///
128 bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
129                                         unsigned AsmVariant,
130                                         const char *ExtraCode,
131                                         raw_ostream &OS) {
132   // Does this asm operand have a single letter operand modifier?
133   if (ExtraCode && ExtraCode[0]) {
134     if (ExtraCode[1] != 0) return true; // Unknown modifier.
135
136     switch (ExtraCode[0]) {
137     default: return true;  // Unknown modifier.
138     case 'c': // Don't print "$" before a global var name or constant.
139       // Hexagon never has a prefix.
140       printOperand(MI, OpNo, OS);
141       return false;
142     case 'L': // Write second word of DImode reference.
143       // Verify that this operand has two consecutive registers.
144       if (!MI->getOperand(OpNo).isReg() ||
145           OpNo+1 == MI->getNumOperands() ||
146           !MI->getOperand(OpNo+1).isReg())
147         return true;
148       ++OpNo;   // Return the high-part.
149       break;
150     case 'I':
151       // Write 'i' if an integer constant, otherwise nothing.  Used to print
152       // addi vs add, etc.
153       if (MI->getOperand(OpNo).isImm())
154         OS << "i";
155       return false;
156     }
157   }
158
159   printOperand(MI, OpNo, OS);
160   return false;
161 }
162
163 bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
164                                             unsigned OpNo, unsigned AsmVariant,
165                                             const char *ExtraCode,
166                                             raw_ostream &O) {
167   if (ExtraCode && ExtraCode[0])
168     return true; // Unknown modifier.
169
170   const MachineOperand &Base  = MI->getOperand(OpNo);
171   const MachineOperand &Offset = MI->getOperand(OpNo+1);
172
173   if (Base.isReg())
174     printOperand(MI, OpNo, O);
175   else
176     llvm_unreachable("Unimplemented");
177
178   if (Offset.isImm()) {
179     if (Offset.getImm())
180       O << " + #" << Offset.getImm();
181   }
182   else
183     llvm_unreachable("Unimplemented");
184
185   return false;
186 }
187
188 void HexagonAsmPrinter::printPredicateOperand(const MachineInstr *MI,
189                                               unsigned OpNo,
190                                               raw_ostream &O) {
191   llvm_unreachable("Unimplemented");
192 }
193
194
195 /// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
196 /// the current output stream.
197 ///
198 void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
199   if (MI->isBundle()) {
200     std::vector<const MachineInstr*> BundleMIs;
201
202     const MachineBasicBlock *MBB = MI->getParent();
203     MachineBasicBlock::const_instr_iterator MII = MI;
204     ++MII;
205     unsigned int IgnoreCount = 0;
206     while (MII != MBB->end() && MII->isInsideBundle()) {
207       const MachineInstr *MInst = MII;
208       if (MInst->getOpcode() == TargetOpcode::DBG_VALUE ||
209           MInst->getOpcode() == TargetOpcode::IMPLICIT_DEF) {
210           IgnoreCount++;
211           ++MII;
212           continue;
213       }
214       //BundleMIs.push_back(&*MII);
215       BundleMIs.push_back(MInst);
216       ++MII;
217     }
218     unsigned Size = BundleMIs.size();
219     assert((Size+IgnoreCount) == MI->getBundleSize() && "Corrupt Bundle!");
220     for (unsigned Index = 0; Index < Size; Index++) {
221       HexagonMCInst MCI;
222       MCI.setStartPacket(Index == 0);
223       MCI.setEndPacket(Index == (Size-1));
224
225       HexagonLowerToMC(BundleMIs[Index], MCI, *this);
226       OutStreamer.EmitInstruction(MCI);
227     }
228   }
229   else {
230     HexagonMCInst MCI;
231     if (MI->getOpcode() == Hexagon::ENDLOOP0) {
232       MCI.setStartPacket(true);
233       MCI.setEndPacket(true);
234     }
235     HexagonLowerToMC(MI, MCI, *this);
236     OutStreamer.EmitInstruction(MCI);
237   }
238
239   return;
240 }
241
242 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
243 /// Don't print things like \n or \0.
244 // static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
245 //   for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
246 //        Name != E; ++Name)
247 //     if (isprint(*Name))
248 //       OS << *Name;
249 // }
250
251
252 void HexagonAsmPrinter::printAddrModeBasePlusOffset(const MachineInstr *MI,
253                                                     int OpNo, raw_ostream &O) {
254   const MachineOperand &MO1 = MI->getOperand(OpNo);
255   const MachineOperand &MO2 = MI->getOperand(OpNo+1);
256
257   O << HexagonInstPrinter::getRegisterName(MO1.getReg())
258     << " + #"
259     << MO2.getImm();
260 }
261
262
263 void HexagonAsmPrinter::printGlobalOperand(const MachineInstr *MI, int OpNo,
264                                            raw_ostream &O) {
265   const MachineOperand &MO = MI->getOperand(OpNo);
266   assert( (MO.getType() == MachineOperand::MO_GlobalAddress) &&
267          "Expecting global address");
268
269   O << *Mang->getSymbol(MO.getGlobal());
270   if (MO.getOffset() != 0) {
271     O << " + ";
272     O << MO.getOffset();
273   }
274 }
275
276 void HexagonAsmPrinter::printJumpTable(const MachineInstr *MI, int OpNo,
277                                        raw_ostream &O) {
278   const MachineOperand &MO = MI->getOperand(OpNo);
279   assert( (MO.getType() == MachineOperand::MO_JumpTableIndex) &&
280     "Expecting jump table index");
281
282   // Hexagon_TODO: Do we need name mangling?
283   O << *GetJTISymbol(MO.getIndex());
284 }
285
286 void HexagonAsmPrinter::printConstantPool(const MachineInstr *MI, int OpNo,
287                                        raw_ostream &O) {
288   const MachineOperand &MO = MI->getOperand(OpNo);
289   assert( (MO.getType() == MachineOperand::MO_ConstantPoolIndex) &&
290    "Expecting constant pool index");
291
292   // Hexagon_TODO: Do we need name mangling?
293   O << *GetCPISymbol(MO.getIndex());
294 }
295
296 static MCInstPrinter *createHexagonMCInstPrinter(const Target &T,
297                                                  unsigned SyntaxVariant,
298                                                  const MCAsmInfo &MAI,
299                                                  const MCInstrInfo &MII,
300                                                  const MCRegisterInfo &MRI,
301                                                  const MCSubtargetInfo &STI) {
302   if (SyntaxVariant == 0)
303     return(new HexagonInstPrinter(MAI, MII, MRI));
304   else
305    return NULL;
306 }
307
308 extern "C" void LLVMInitializeHexagonAsmPrinter() {
309   RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
310
311   TargetRegistry::RegisterMCInstPrinter(TheHexagonTarget,
312                                         createHexagonMCInstPrinter);
313 }