131d157ae1ac2cd9c787dc3c1d28e64c682803844Jia Liu//===-- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly --===//
2b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
3b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//                     The LLVM Compiler Infrastructure
4b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
5b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// This file is distributed under the University of Illinois Open Source
6b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// License. See LICENSE.TXT for details.
7b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
8b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//===----------------------------------------------------------------------===//
9b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
10b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// This file contains a printer that converts from our internal representation
11b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// of machine-dependent LLVM code to Hexagon assembly language. This printer is
12b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// the output mechanism used by `llc'.
13b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
14b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//===----------------------------------------------------------------------===//
15b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
16b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#define DEBUG_TYPE "asm-printer"
17d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "Hexagon.h"
18d6c98ae63824854ea2175b362a10985cac7cfb32Jyotsna Verma#include "HexagonAsmPrinter.h"
19d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "HexagonMachineFunctionInfo.h"
20d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "HexagonTargetMachine.h"
21d6c98ae63824854ea2175b362a10985cac7cfb32Jyotsna Verma#include "HexagonSubtarget.h"
22d6c98ae63824854ea2175b362a10985cac7cfb32Jyotsna Verma#include "MCTargetDesc/HexagonMCInst.h"
23e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes#include "InstPrinter/HexagonInstPrinter.h"
24d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/SmallString.h"
25d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/SmallVector.h"
26d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/StringExtras.h"
27e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes#include "llvm/Analysis/ConstantFolding.h"
28b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/Assembly/Writer.h"
29b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/CodeGen/AsmPrinter.h"
30b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/CodeGen/MachineFunctionPass.h"
31b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/CodeGen/MachineInstr.h"
32b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/CodeGen/MachineInstrBuilder.h"
33d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/CodeGen/MachineModuleInfo.h"
340b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h"
350b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h"
360b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
370b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Module.h"
38b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/MC/MCAsmInfo.h"
39e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes#include "llvm/MC/MCContext.h"
40e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes#include "llvm/MC/MCExpr.h"
41e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes#include "llvm/MC/MCInst.h"
42e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes#include "llvm/MC/MCSection.h"
43e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes#include "llvm/MC/MCStreamer.h"
44b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/MC/MCSymbol.h"
45b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/Support/CommandLine.h"
46b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/Support/Compiler.h"
47d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/Debug.h"
48e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes#include "llvm/Support/Format.h"
49d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/MathExtras.h"
5079aa3417eb6f58d668aadfedf075240a41d35a26Craig Topper#include "llvm/Support/TargetRegistry.h"
51d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/raw_ostream.h"
52b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/Target/Mangler.h"
53b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/Target/TargetInstrInfo.h"
54d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetLoweringObjectFile.h"
55b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/Target/TargetOptions.h"
56d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetRegisterInfo.h"
57b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
58b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumusing namespace llvm;
59b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
60b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumstatic cl::opt<bool> AlignCalls(
61b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum         "hexagon-align-calls", cl::Hidden, cl::init(true),
62b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          cl::desc("Insert falign after call instruction for Hexagon target"));
63b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
64b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumvoid HexagonAsmPrinter::EmitAlignment(unsigned NumBits,
65b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                      const GlobalValue *GV) const {
66e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes  // For basic block level alignment, use ".falign".
67b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (!GV) {
68b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    OutStreamer.EmitRawText(StringRef("\t.falign"));
69b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return;
70b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
71b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
72b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  AsmPrinter::EmitAlignment(NumBits, GV);
73b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
74b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
75e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezesvoid HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
76e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes                                    raw_ostream &O) {
77e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes  const MachineOperand &MO = MI->getOperand(OpNo);
78e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes
79b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  switch (MO.getType()) {
8026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  default: llvm_unreachable ("<unknown operand type>");
81e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes  case MachineOperand::MO_Register:
82e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes    O << HexagonInstPrinter::getRegisterName(MO.getReg());
83e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes    return;
84b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case MachineOperand::MO_Immediate:
85e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes    O << MO.getImm();
86e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes    return;
87b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case MachineOperand::MO_MachineBasicBlock:
88b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    O << *MO.getMBB()->getSymbol();
89b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return;
90b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case MachineOperand::MO_JumpTableIndex:
91b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    O << *GetJTISymbol(MO.getIndex());
92b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    // FIXME: PIC relocation model.
93b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return;
94b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case MachineOperand::MO_ConstantPoolIndex:
95b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    O << *GetCPISymbol(MO.getIndex());
96b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return;
97b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case MachineOperand::MO_ExternalSymbol:
98b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    O << *GetExternalSymbolSymbol(MO.getSymbolName());
99b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return;
100e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes  case MachineOperand::MO_GlobalAddress:
101b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    // Computing the address of a global symbol, not calling it.
102b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    O << *Mang->getSymbol(MO.getGlobal());
103b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    printOffset(MO.getOffset(), O);
104b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return;
105b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
106b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
107b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
108b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
109b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// isBlockOnlyReachableByFallthrough - We need to override this since the
110b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// default AsmPrinter does not print labels for any basic block that
111b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// is only reachable by a fall through. That works for all cases except
112b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// for the case in which the basic block is reachable by a fall through but
113b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// through an indirect from a jump table. In this case, the jump table
114b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// will contain a label not defined by AsmPrinter.
115b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
116b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonAsmPrinter::
117b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumisBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
118b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (MBB->hasAddressTaken()) {
119b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return false;
120b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
121b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
122b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
123b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
124b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
125b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// PrintAsmOperand - Print out an operand for an inline asm expression.
126b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum///
127b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
128b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                        unsigned AsmVariant,
129b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                        const char *ExtraCode,
130e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes                                        raw_ostream &OS) {
131b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Does this asm operand have a single letter operand modifier?
132b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (ExtraCode && ExtraCode[0]) {
133b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    if (ExtraCode[1] != 0) return true; // Unknown modifier.
134b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
135b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    switch (ExtraCode[0]) {
1360518fca843ff87d069ecb07fc00d306c1f587d58Jack Carter    default:
1370518fca843ff87d069ecb07fc00d306c1f587d58Jack Carter      // See if this is a generic print operand
1380518fca843ff87d069ecb07fc00d306c1f587d58Jack Carter      return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
139b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case 'c': // Don't print "$" before a global var name or constant.
140b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      // Hexagon never has a prefix.
141b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      printOperand(MI, OpNo, OS);
142b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return false;
143b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case 'L': // Write second word of DImode reference.
144b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      // Verify that this operand has two consecutive registers.
145b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      if (!MI->getOperand(OpNo).isReg() ||
146b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          OpNo+1 == MI->getNumOperands() ||
147b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          !MI->getOperand(OpNo+1).isReg())
148b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        return true;
149b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      ++OpNo;   // Return the high-part.
150b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      break;
151b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case 'I':
152b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      // Write 'i' if an integer constant, otherwise nothing.  Used to print
153b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      // addi vs add, etc.
154b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      if (MI->getOperand(OpNo).isImm())
155b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        OS << "i";
156b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return false;
157b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    }
158b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
159b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
160b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  printOperand(MI, OpNo, OS);
161b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return false;
162b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
163b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
164b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
165b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                            unsigned OpNo, unsigned AsmVariant,
166b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                            const char *ExtraCode,
167b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                            raw_ostream &O) {
168b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (ExtraCode && ExtraCode[0])
169b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return true; // Unknown modifier.
170b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
171b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  const MachineOperand &Base  = MI->getOperand(OpNo);
172b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  const MachineOperand &Offset = MI->getOperand(OpNo+1);
173b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
174b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (Base.isReg())
175b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    printOperand(MI, OpNo, O);
176b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  else
177bc2198133a1836598b54b943420748e75d5dea94Craig Topper    llvm_unreachable("Unimplemented");
178b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
179b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (Offset.isImm()) {
180b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    if (Offset.getImm())
181b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      O << " + #" << Offset.getImm();
182b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
183b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  else
184bc2198133a1836598b54b943420748e75d5dea94Craig Topper    llvm_unreachable("Unimplemented");
185b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
186b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return false;
187b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
188b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
189b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumvoid HexagonAsmPrinter::printPredicateOperand(const MachineInstr *MI,
190b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                              unsigned OpNo,
191b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                              raw_ostream &O) {
192bc2198133a1836598b54b943420748e75d5dea94Craig Topper  llvm_unreachable("Unimplemented");
193b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
194b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
195b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
196b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
197b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// the current output stream.
198b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum///
199b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumvoid HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
20026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  if (MI->isBundle()) {
20126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    std::vector<const MachineInstr*> BundleMIs;
20226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande
20326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    const MachineBasicBlock *MBB = MI->getParent();
20426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    MachineBasicBlock::const_instr_iterator MII = MI;
20526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    ++MII;
20626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    unsigned int IgnoreCount = 0;
20726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    while (MII != MBB->end() && MII->isInsideBundle()) {
20826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      const MachineInstr *MInst = MII;
20926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      if (MInst->getOpcode() == TargetOpcode::DBG_VALUE ||
21026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande          MInst->getOpcode() == TargetOpcode::IMPLICIT_DEF) {
21126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande          IgnoreCount++;
21226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande          ++MII;
21326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande          continue;
21426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      }
21526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      //BundleMIs.push_back(&*MII);
21626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      BundleMIs.push_back(MInst);
21726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      ++MII;
21826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    }
21926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    unsigned Size = BundleMIs.size();
22026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    assert((Size+IgnoreCount) == MI->getBundleSize() && "Corrupt Bundle!");
22126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    for (unsigned Index = 0; Index < Size; Index++) {
22226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      HexagonMCInst MCI;
223d6c98ae63824854ea2175b362a10985cac7cfb32Jyotsna Verma      MCI.setPacketStart(Index == 0);
224d6c98ae63824854ea2175b362a10985cac7cfb32Jyotsna Verma      MCI.setPacketEnd(Index == (Size-1));
22526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande
22626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      HexagonLowerToMC(BundleMIs[Index], MCI, *this);
22726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      OutStreamer.EmitInstruction(MCI);
22826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    }
22926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  }
23026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  else {
23126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    HexagonMCInst MCI;
23226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    if (MI->getOpcode() == Hexagon::ENDLOOP0) {
233d6c98ae63824854ea2175b362a10985cac7cfb32Jyotsna Verma      MCI.setPacketStart(true);
234d6c98ae63824854ea2175b362a10985cac7cfb32Jyotsna Verma      MCI.setPacketEnd(true);
23526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    }
23626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    HexagonLowerToMC(MI, MCI, *this);
23726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    OutStreamer.EmitInstruction(MCI);
23826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  }
239b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
240b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return;
241b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
242b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
243b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// PrintUnmangledNameSafely - Print out the printable characters in the name.
244b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// Don't print things like \n or \0.
245b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
246b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//   for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
247b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//        Name != E; ++Name)
248b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//     if (isprint(*Name))
249b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//       OS << *Name;
250b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// }
251b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
252b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
253b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumvoid HexagonAsmPrinter::printAddrModeBasePlusOffset(const MachineInstr *MI,
254b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                                    int OpNo, raw_ostream &O) {
255b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  const MachineOperand &MO1 = MI->getOperand(OpNo);
256b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  const MachineOperand &MO2 = MI->getOperand(OpNo+1);
257b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
258e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes  O << HexagonInstPrinter::getRegisterName(MO1.getReg())
259b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    << " + #"
260b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    << MO2.getImm();
261b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
262b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
263b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
264b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumvoid HexagonAsmPrinter::printGlobalOperand(const MachineInstr *MI, int OpNo,
265b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                           raw_ostream &O) {
266b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  const MachineOperand &MO = MI->getOperand(OpNo);
267b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  assert( (MO.getType() == MachineOperand::MO_GlobalAddress) &&
268b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum         "Expecting global address");
269b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
270b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  O << *Mang->getSymbol(MO.getGlobal());
271b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (MO.getOffset() != 0) {
272b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    O << " + ";
273b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    O << MO.getOffset();
274b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
275b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
276b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
277b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumvoid HexagonAsmPrinter::printJumpTable(const MachineInstr *MI, int OpNo,
278b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                       raw_ostream &O) {
279b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  const MachineOperand &MO = MI->getOperand(OpNo);
28026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  assert( (MO.getType() == MachineOperand::MO_JumpTableIndex) &&
28126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande           "Expecting jump table index");
282b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
283b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Hexagon_TODO: Do we need name mangling?
284b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  O << *GetJTISymbol(MO.getIndex());
285b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
286b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
287e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezesvoid HexagonAsmPrinter::printConstantPool(const MachineInstr *MI, int OpNo,
28826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande                                       raw_ostream &O) {
289e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes  const MachineOperand &MO = MI->getOperand(OpNo);
290e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes  assert( (MO.getType() == MachineOperand::MO_ConstantPoolIndex) &&
291d410eaba04211d53a523a518a5e315eb24c1072fChandler Carruth          "Expecting constant pool index");
292e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes
293e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes  // Hexagon_TODO: Do we need name mangling?
294e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes  O << *GetCPISymbol(MO.getIndex());
295e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes}
296e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes
297e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezesstatic MCInstPrinter *createHexagonMCInstPrinter(const Target &T,
298e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes                                                 unsigned SyntaxVariant,
299e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes                                                 const MCAsmInfo &MAI,
300e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes                                                 const MCInstrInfo &MII,
301e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes                                                 const MCRegisterInfo &MRI,
302e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes                                                 const MCSubtargetInfo &STI) {
303e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes  if (SyntaxVariant == 0)
304e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes    return(new HexagonInstPrinter(MAI, MII, MRI));
305e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes  else
306e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes   return NULL;
307e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes}
308e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes
309b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumextern "C" void LLVMInitializeHexagonAsmPrinter() {
310b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
311e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes
312e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes  TargetRegistry::RegisterMCInstPrinter(TheHexagonTarget,
313e5041e6fa8fa74a26e031b7487be1912257c87f1Evandro Menezes                                        createHexagonMCInstPrinter);
314b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
315