Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly --===// |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 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 | // |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "Hexagon.h" |
Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 17 | #include "HexagonAsmPrinter.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "HexagonMachineFunctionInfo.h" |
Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 19 | #include "HexagonSubtarget.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 20 | #include "HexagonTargetMachine.h" |
Colin LeMahieu | ff06261 | 2014-11-20 21:56:35 +0000 | [diff] [blame] | 21 | #include "MCTargetDesc/HexagonInstPrinter.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 22 | #include "MCTargetDesc/HexagonMCInst.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
| 24 | #include "llvm/ADT/SmallVector.h" |
| 25 | #include "llvm/ADT/StringExtras.h" |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/ConstantFolding.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/AsmPrinter.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 29 | #include "llvm/CodeGen/MachineInstr.h" |
| 30 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Constants.h" |
| 33 | #include "llvm/IR/DataLayout.h" |
| 34 | #include "llvm/IR/DerivedTypes.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Mangler.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Module.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCAsmInfo.h" |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 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" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 43 | #include "llvm/MC/MCSymbol.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 44 | #include "llvm/Support/CommandLine.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Debug.h" |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Format.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 48 | #include "llvm/Support/MathExtras.h" |
Craig Topper | b25fda9 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 49 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 50 | #include "llvm/Support/raw_ostream.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 51 | #include "llvm/Target/TargetInstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 52 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 53 | #include "llvm/Target/TargetOptions.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 54 | #include "llvm/Target/TargetRegisterInfo.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 55 | |
| 56 | using namespace llvm; |
| 57 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 58 | #define DEBUG_TYPE "asm-printer" |
| 59 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 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 | |
David Blaikie | 9459832 | 2015-01-18 20:29:04 +0000 | [diff] [blame] | 64 | HexagonAsmPrinter::HexagonAsmPrinter(TargetMachine &TM, |
| 65 | std::unique_ptr<MCStreamer> Streamer) |
Eric Christopher | 8f276db | 2015-02-03 06:40:22 +0000 | [diff] [blame] | 66 | : AsmPrinter(TM, std::move(Streamer)), Subtarget(nullptr) {} |
David Blaikie | 9459832 | 2015-01-18 20:29:04 +0000 | [diff] [blame] | 67 | |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 68 | void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, |
| 69 | raw_ostream &O) { |
| 70 | const MachineOperand &MO = MI->getOperand(OpNo); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 71 | |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 72 | switch (MO.getType()) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 73 | default: llvm_unreachable ("<unknown operand type>"); |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 74 | case MachineOperand::MO_Register: |
| 75 | O << HexagonInstPrinter::getRegisterName(MO.getReg()); |
| 76 | return; |
| 77 | case MachineOperand::MO_Immediate: |
| 78 | O << MO.getImm(); |
| 79 | return; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 80 | case MachineOperand::MO_MachineBasicBlock: |
| 81 | O << *MO.getMBB()->getSymbol(); |
| 82 | return; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 83 | case MachineOperand::MO_ConstantPoolIndex: |
| 84 | O << *GetCPISymbol(MO.getIndex()); |
| 85 | return; |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 86 | case MachineOperand::MO_GlobalAddress: |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 87 | // Computing the address of a global symbol, not calling it. |
Rafael Espindola | 79858aa | 2013-10-29 17:07:16 +0000 | [diff] [blame] | 88 | O << *getSymbol(MO.getGlobal()); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 89 | printOffset(MO.getOffset(), O); |
| 90 | return; |
| 91 | } |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 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, |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 116 | raw_ostream &OS) { |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 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]) { |
Jack Carter | 5e69cff | 2012-06-26 13:49:27 +0000 | [diff] [blame] | 122 | default: |
| 123 | // See if this is a generic print operand |
| 124 | return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 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 |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 163 | llvm_unreachable("Unimplemented"); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 164 | |
| 165 | if (Offset.isImm()) { |
| 166 | if (Offset.getImm()) |
| 167 | O << " + #" << Offset.getImm(); |
| 168 | } |
| 169 | else |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 170 | llvm_unreachable("Unimplemented"); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 171 | |
| 172 | return false; |
| 173 | } |
| 174 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 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) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 180 | if (MI->isBundle()) { |
Colin LeMahieu | 7e9908e | 2014-12-03 20:23:22 +0000 | [diff] [blame] | 181 | std::vector<MachineInstr const *> BundleMIs; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 182 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 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 || |
Colin LeMahieu | 7e9908e | 2014-12-03 20:23:22 +0000 | [diff] [blame] | 190 | MInst->getOpcode() == TargetOpcode::IMPLICIT_DEF) { |
| 191 | IgnoreCount++; |
| 192 | ++MII; |
| 193 | continue; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 194 | } |
Colin LeMahieu | 7e9908e | 2014-12-03 20:23:22 +0000 | [diff] [blame] | 195 | // BundleMIs.push_back(&*MII); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 196 | BundleMIs.push_back(MInst); |
| 197 | ++MII; |
| 198 | } |
| 199 | unsigned Size = BundleMIs.size(); |
Colin LeMahieu | 7e9908e | 2014-12-03 20:23:22 +0000 | [diff] [blame] | 200 | assert((Size + IgnoreCount) == MI->getBundleSize() && "Corrupt Bundle!"); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 201 | for (unsigned Index = 0; Index < Size; Index++) { |
Colin LeMahieu | 5241881 | 2014-11-04 00:14:36 +0000 | [diff] [blame] | 202 | HexagonMCInst MCI; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 203 | |
| 204 | HexagonLowerToMC(BundleMIs[Index], MCI, *this); |
Colin LeMahieu | 7e9908e | 2014-12-03 20:23:22 +0000 | [diff] [blame] | 205 | HexagonMCInst::AppendImplicitOperands(MCI); |
| 206 | MCI.setPacketBegin(Index == 0); |
| 207 | MCI.setPacketEnd(Index == (Size - 1)); |
David Woodhouse | e6c13e4 | 2014-01-28 23:12:42 +0000 | [diff] [blame] | 208 | EmitToStreamer(OutStreamer, MCI); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | else { |
Colin LeMahieu | 5241881 | 2014-11-04 00:14:36 +0000 | [diff] [blame] | 212 | HexagonMCInst MCI; |
Colin LeMahieu | 7e9908e | 2014-12-03 20:23:22 +0000 | [diff] [blame] | 213 | HexagonLowerToMC(MI, MCI, *this); |
| 214 | HexagonMCInst::AppendImplicitOperands(MCI); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 215 | if (MI->getOpcode() == Hexagon::ENDLOOP0) { |
Colin LeMahieu | b4e5be4 | 2014-12-03 17:31:43 +0000 | [diff] [blame] | 216 | MCI.setPacketBegin(true); |
Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 217 | MCI.setPacketEnd(true); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 218 | } |
David Woodhouse | e6c13e4 | 2014-01-28 23:12:42 +0000 | [diff] [blame] | 219 | EmitToStreamer(OutStreamer, MCI); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 220 | } |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 221 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 225 | static MCInstPrinter *createHexagonMCInstPrinter(const Target &T, |
| 226 | unsigned SyntaxVariant, |
| 227 | const MCAsmInfo &MAI, |
| 228 | const MCInstrInfo &MII, |
| 229 | const MCRegisterInfo &MRI, |
| 230 | const MCSubtargetInfo &STI) { |
| 231 | if (SyntaxVariant == 0) |
| 232 | return(new HexagonInstPrinter(MAI, MII, MRI)); |
| 233 | else |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 234 | return nullptr; |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 237 | extern "C" void LLVMInitializeHexagonAsmPrinter() { |
| 238 | RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget); |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 239 | |
| 240 | TargetRegistry::RegisterMCInstPrinter(TheHexagonTarget, |
| 241 | createHexagonMCInstPrinter); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 242 | } |