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 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "asm-printer" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "Hexagon.h" |
Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 18 | #include "HexagonAsmPrinter.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "HexagonMachineFunctionInfo.h" |
Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 20 | #include "HexagonSubtarget.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 21 | #include "HexagonTargetMachine.h" |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 22 | #include "InstPrinter/HexagonInstPrinter.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 23 | #include "MCTargetDesc/HexagonMCInst.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
| 25 | #include "llvm/ADT/SmallVector.h" |
| 26 | #include "llvm/ADT/StringExtras.h" |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/ConstantFolding.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/AsmPrinter.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 30 | #include "llvm/CodeGen/MachineInstr.h" |
| 31 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Constants.h" |
| 34 | #include "llvm/IR/DataLayout.h" |
| 35 | #include "llvm/IR/DerivedTypes.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Mangler.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Module.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCAsmInfo.h" |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 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" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 44 | #include "llvm/MC/MCSymbol.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 45 | #include "llvm/Support/CommandLine.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Debug.h" |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Format.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 49 | #include "llvm/Support/MathExtras.h" |
Craig Topper | b25fda9 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 50 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 51 | #include "llvm/Support/raw_ostream.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 52 | #include "llvm/Target/TargetInstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 53 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 54 | #include "llvm/Target/TargetOptions.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 55 | #include "llvm/Target/TargetRegisterInfo.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 56 | |
| 57 | using namespace llvm; |
| 58 | |
| 59 | static cl::opt<bool> AlignCalls( |
| 60 | "hexagon-align-calls", cl::Hidden, cl::init(true), |
| 61 | cl::desc("Insert falign after call instruction for Hexagon target")); |
| 62 | |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 63 | void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, |
| 64 | raw_ostream &O) { |
| 65 | const MachineOperand &MO = MI->getOperand(OpNo); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 66 | |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 67 | switch (MO.getType()) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 68 | default: llvm_unreachable ("<unknown operand type>"); |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 69 | case MachineOperand::MO_Register: |
| 70 | O << HexagonInstPrinter::getRegisterName(MO.getReg()); |
| 71 | return; |
| 72 | case MachineOperand::MO_Immediate: |
| 73 | O << MO.getImm(); |
| 74 | return; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 75 | case MachineOperand::MO_MachineBasicBlock: |
| 76 | O << *MO.getMBB()->getSymbol(); |
| 77 | return; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 78 | case MachineOperand::MO_ConstantPoolIndex: |
| 79 | O << *GetCPISymbol(MO.getIndex()); |
| 80 | return; |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 81 | case MachineOperand::MO_GlobalAddress: |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 82 | // Computing the address of a global symbol, not calling it. |
Rafael Espindola | 79858aa | 2013-10-29 17:07:16 +0000 | [diff] [blame] | 83 | O << *getSymbol(MO.getGlobal()); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 84 | printOffset(MO.getOffset(), O); |
| 85 | return; |
| 86 | } |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 89 | // |
| 90 | // isBlockOnlyReachableByFallthrough - We need to override this since the |
| 91 | // default AsmPrinter does not print labels for any basic block that |
| 92 | // is only reachable by a fall through. That works for all cases except |
| 93 | // for the case in which the basic block is reachable by a fall through but |
| 94 | // through an indirect from a jump table. In this case, the jump table |
| 95 | // will contain a label not defined by AsmPrinter. |
| 96 | // |
| 97 | bool HexagonAsmPrinter:: |
| 98 | isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const { |
| 99 | if (MBB->hasAddressTaken()) { |
| 100 | return false; |
| 101 | } |
| 102 | return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /// PrintAsmOperand - Print out an operand for an inline asm expression. |
| 107 | /// |
| 108 | bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 109 | unsigned AsmVariant, |
| 110 | const char *ExtraCode, |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 111 | raw_ostream &OS) { |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 112 | // Does this asm operand have a single letter operand modifier? |
| 113 | if (ExtraCode && ExtraCode[0]) { |
| 114 | if (ExtraCode[1] != 0) return true; // Unknown modifier. |
| 115 | |
| 116 | switch (ExtraCode[0]) { |
Jack Carter | 5e69cff | 2012-06-26 13:49:27 +0000 | [diff] [blame] | 117 | default: |
| 118 | // See if this is a generic print operand |
| 119 | return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 120 | case 'c': // Don't print "$" before a global var name or constant. |
| 121 | // Hexagon never has a prefix. |
| 122 | printOperand(MI, OpNo, OS); |
| 123 | return false; |
| 124 | case 'L': // Write second word of DImode reference. |
| 125 | // Verify that this operand has two consecutive registers. |
| 126 | if (!MI->getOperand(OpNo).isReg() || |
| 127 | OpNo+1 == MI->getNumOperands() || |
| 128 | !MI->getOperand(OpNo+1).isReg()) |
| 129 | return true; |
| 130 | ++OpNo; // Return the high-part. |
| 131 | break; |
| 132 | case 'I': |
| 133 | // Write 'i' if an integer constant, otherwise nothing. Used to print |
| 134 | // addi vs add, etc. |
| 135 | if (MI->getOperand(OpNo).isImm()) |
| 136 | OS << "i"; |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | printOperand(MI, OpNo, OS); |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, |
| 146 | unsigned OpNo, unsigned AsmVariant, |
| 147 | const char *ExtraCode, |
| 148 | raw_ostream &O) { |
| 149 | if (ExtraCode && ExtraCode[0]) |
| 150 | return true; // Unknown modifier. |
| 151 | |
| 152 | const MachineOperand &Base = MI->getOperand(OpNo); |
| 153 | const MachineOperand &Offset = MI->getOperand(OpNo+1); |
| 154 | |
| 155 | if (Base.isReg()) |
| 156 | printOperand(MI, OpNo, O); |
| 157 | else |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 158 | llvm_unreachable("Unimplemented"); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 159 | |
| 160 | if (Offset.isImm()) { |
| 161 | if (Offset.getImm()) |
| 162 | O << " + #" << Offset.getImm(); |
| 163 | } |
| 164 | else |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 165 | llvm_unreachable("Unimplemented"); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 166 | |
| 167 | return false; |
| 168 | } |
| 169 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 170 | |
| 171 | /// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to |
| 172 | /// the current output stream. |
| 173 | /// |
| 174 | void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 175 | if (MI->isBundle()) { |
| 176 | std::vector<const MachineInstr*> BundleMIs; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 177 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 178 | const MachineBasicBlock *MBB = MI->getParent(); |
| 179 | MachineBasicBlock::const_instr_iterator MII = MI; |
| 180 | ++MII; |
| 181 | unsigned int IgnoreCount = 0; |
| 182 | while (MII != MBB->end() && MII->isInsideBundle()) { |
| 183 | const MachineInstr *MInst = MII; |
| 184 | if (MInst->getOpcode() == TargetOpcode::DBG_VALUE || |
| 185 | MInst->getOpcode() == TargetOpcode::IMPLICIT_DEF) { |
| 186 | IgnoreCount++; |
| 187 | ++MII; |
| 188 | continue; |
| 189 | } |
| 190 | //BundleMIs.push_back(&*MII); |
| 191 | BundleMIs.push_back(MInst); |
| 192 | ++MII; |
| 193 | } |
| 194 | unsigned Size = BundleMIs.size(); |
| 195 | assert((Size+IgnoreCount) == MI->getBundleSize() && "Corrupt Bundle!"); |
| 196 | for (unsigned Index = 0; Index < Size; Index++) { |
| 197 | HexagonMCInst MCI; |
Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 198 | MCI.setPacketStart(Index == 0); |
| 199 | MCI.setPacketEnd(Index == (Size-1)); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 200 | |
| 201 | HexagonLowerToMC(BundleMIs[Index], MCI, *this); |
David Woodhouse | e6c13e4 | 2014-01-28 23:12:42 +0000 | [diff] [blame^] | 202 | EmitToStreamer(OutStreamer, MCI); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | else { |
| 206 | HexagonMCInst MCI; |
| 207 | if (MI->getOpcode() == Hexagon::ENDLOOP0) { |
Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 208 | MCI.setPacketStart(true); |
| 209 | MCI.setPacketEnd(true); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 210 | } |
| 211 | HexagonLowerToMC(MI, MCI, *this); |
David Woodhouse | e6c13e4 | 2014-01-28 23:12:42 +0000 | [diff] [blame^] | 212 | EmitToStreamer(OutStreamer, MCI); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 213 | } |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 214 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 218 | static MCInstPrinter *createHexagonMCInstPrinter(const Target &T, |
| 219 | unsigned SyntaxVariant, |
| 220 | const MCAsmInfo &MAI, |
| 221 | const MCInstrInfo &MII, |
| 222 | const MCRegisterInfo &MRI, |
| 223 | const MCSubtargetInfo &STI) { |
| 224 | if (SyntaxVariant == 0) |
| 225 | return(new HexagonInstPrinter(MAI, MII, MRI)); |
| 226 | else |
| 227 | return NULL; |
| 228 | } |
| 229 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 230 | extern "C" void LLVMInitializeHexagonAsmPrinter() { |
| 231 | RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget); |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 232 | |
| 233 | TargetRegistry::RegisterMCInstPrinter(TheHexagonTarget, |
| 234 | createHexagonMCInstPrinter); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 235 | } |