Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 1 | //===- MIParser.cpp - Machine instructions parser implementation ----------===// |
| 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 implements the parsing of machine instructions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "MIParser.h" |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 15 | #include "MILexer.h" |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringMap.h" |
| 17 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 18 | #include "llvm/CodeGen/MachineFunction.h" |
| 19 | #include "llvm/CodeGen/MachineInstr.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | #include "llvm/Support/SourceMgr.h" |
| 22 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 23 | #include "llvm/Target/TargetInstrInfo.h" |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | class MIParser { |
| 30 | SourceMgr &SM; |
| 31 | MachineFunction &MF; |
| 32 | SMDiagnostic &Error; |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 33 | StringRef Source, CurrentSource; |
| 34 | MIToken Token; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 35 | /// Maps from basic block numbers to MBBs. |
| 36 | const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots; |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 37 | /// Maps from instruction names to op codes. |
| 38 | StringMap<unsigned> Names2InstrOpCodes; |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 39 | /// Maps from register names to registers. |
| 40 | StringMap<unsigned> Names2Regs; |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 41 | |
| 42 | public: |
| 43 | MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error, |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 44 | StringRef Source, |
| 45 | const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 46 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 47 | void lex(); |
| 48 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 49 | /// Report an error at the current location with the given message. |
| 50 | /// |
| 51 | /// This function always return true. |
| 52 | bool error(const Twine &Msg); |
| 53 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 54 | /// Report an error at the given location with the given message. |
| 55 | /// |
| 56 | /// This function always return true. |
| 57 | bool error(StringRef::iterator Loc, const Twine &Msg); |
| 58 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 59 | MachineInstr *parse(); |
| 60 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 61 | bool parseRegister(unsigned &Reg); |
| 62 | bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false); |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 63 | bool parseImmediateOperand(MachineOperand &Dest); |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 64 | bool parseMBBOperand(MachineOperand &Dest); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 65 | bool parseMachineOperand(MachineOperand &Dest); |
| 66 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 67 | private: |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 68 | /// Convert the integer literal in the current token into an unsigned integer. |
| 69 | /// |
| 70 | /// Return true if an error occurred. |
| 71 | bool getUnsigned(unsigned &Result); |
| 72 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 73 | void initNames2InstrOpCodes(); |
| 74 | |
| 75 | /// Try to convert an instruction name to an opcode. Return true if the |
| 76 | /// instruction name is invalid. |
| 77 | bool parseInstrName(StringRef InstrName, unsigned &OpCode); |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 78 | |
| 79 | bool parseInstruction(unsigned &OpCode); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 80 | |
| 81 | void initNames2Regs(); |
| 82 | |
| 83 | /// Try to convert a register name to a register number. Return true if the |
| 84 | /// register name is invalid. |
| 85 | bool getRegisterByName(StringRef RegName, unsigned &Reg); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // end anonymous namespace |
| 89 | |
| 90 | MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error, |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 91 | StringRef Source, |
| 92 | const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 93 | : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source), |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 94 | Token(MIToken::Error, StringRef()), MBBSlots(MBBSlots) {} |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 95 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 96 | void MIParser::lex() { |
| 97 | CurrentSource = lexMIToken( |
| 98 | CurrentSource, Token, |
| 99 | [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); }); |
| 100 | } |
| 101 | |
| 102 | bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); } |
| 103 | |
| 104 | bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) { |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 105 | // TODO: Get the proper location in the MIR file, not just a location inside |
| 106 | // the string. |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 107 | assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size())); |
| 108 | Error = SMDiagnostic( |
| 109 | SM, SMLoc(), |
| 110 | SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1, |
| 111 | Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 112 | return true; |
| 113 | } |
| 114 | |
| 115 | MachineInstr *MIParser::parse() { |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 116 | lex(); |
| 117 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 118 | // Parse any register operands before '=' |
| 119 | // TODO: Allow parsing of multiple operands before '=' |
| 120 | MachineOperand MO = MachineOperand::CreateImm(0); |
| 121 | SmallVector<MachineOperand, 8> Operands; |
| 122 | if (Token.isRegister()) { |
| 123 | if (parseRegisterOperand(MO, /*IsDef=*/true)) |
| 124 | return nullptr; |
| 125 | Operands.push_back(MO); |
| 126 | if (Token.isNot(MIToken::equal)) { |
| 127 | error("expected '='"); |
| 128 | return nullptr; |
| 129 | } |
| 130 | lex(); |
| 131 | } |
| 132 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 133 | unsigned OpCode; |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 134 | if (Token.isError() || parseInstruction(OpCode)) |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 135 | return nullptr; |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 136 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 137 | // TODO: Parse the instruction flags and memory operands. |
| 138 | |
| 139 | // Parse the remaining machine operands. |
| 140 | while (Token.isNot(MIToken::Eof)) { |
| 141 | if (parseMachineOperand(MO)) |
| 142 | return nullptr; |
| 143 | Operands.push_back(MO); |
| 144 | if (Token.is(MIToken::Eof)) |
| 145 | break; |
| 146 | if (Token.isNot(MIToken::comma)) { |
| 147 | error("expected ',' before the next machine operand"); |
| 148 | return nullptr; |
| 149 | } |
| 150 | lex(); |
| 151 | } |
| 152 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 153 | const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 154 | |
| 155 | // Verify machine operands. |
| 156 | if (!MCID.isVariadic()) { |
| 157 | for (size_t I = 0, E = Operands.size(); I < E; ++I) { |
| 158 | if (I < MCID.getNumOperands()) |
| 159 | continue; |
| 160 | // Mark this register as implicit to prevent an assertion when it's added |
| 161 | // to an instruction. This is a temporary workaround until the implicit |
| 162 | // register flag can be parsed. |
| 163 | Operands[I].setImplicit(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // TODO: Determine the implicit behaviour when implicit register flags are |
| 168 | // parsed. |
| 169 | auto *MI = MF.CreateMachineInstr(MCID, DebugLoc(), /*NoImplicit=*/true); |
| 170 | for (const auto &Operand : Operands) |
| 171 | MI->addOperand(MF, Operand); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 172 | return MI; |
| 173 | } |
| 174 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 175 | bool MIParser::parseInstruction(unsigned &OpCode) { |
| 176 | if (Token.isNot(MIToken::Identifier)) |
| 177 | return error("expected a machine instruction"); |
| 178 | StringRef InstrName = Token.stringValue(); |
| 179 | if (parseInstrName(InstrName, OpCode)) |
| 180 | return error(Twine("unknown machine instruction name '") + InstrName + "'"); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 181 | lex(); |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | bool MIParser::parseRegister(unsigned &Reg) { |
| 186 | switch (Token.kind()) { |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 187 | case MIToken::underscore: |
| 188 | Reg = 0; |
| 189 | break; |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 190 | case MIToken::NamedRegister: { |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 191 | StringRef Name = Token.stringValue(); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 192 | if (getRegisterByName(Name, Reg)) |
| 193 | return error(Twine("unknown register name '") + Name + "'"); |
| 194 | break; |
| 195 | } |
| 196 | // TODO: Parse other register kinds. |
| 197 | default: |
| 198 | llvm_unreachable("The current token should be a register"); |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) { |
| 204 | unsigned Reg; |
| 205 | // TODO: Parse register flags. |
| 206 | if (parseRegister(Reg)) |
| 207 | return true; |
| 208 | lex(); |
| 209 | // TODO: Parse subregister. |
| 210 | Dest = MachineOperand::CreateReg(Reg, IsDef); |
| 211 | return false; |
| 212 | } |
| 213 | |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 214 | bool MIParser::parseImmediateOperand(MachineOperand &Dest) { |
| 215 | assert(Token.is(MIToken::IntegerLiteral)); |
| 216 | const APSInt &Int = Token.integerValue(); |
| 217 | if (Int.getMinSignedBits() > 64) |
| 218 | // TODO: Replace this with an error when we can parse CIMM Machine Operands. |
| 219 | llvm_unreachable("Can't parse large integer literals yet!"); |
| 220 | Dest = MachineOperand::CreateImm(Int.getExtValue()); |
| 221 | lex(); |
| 222 | return false; |
| 223 | } |
| 224 | |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 225 | bool MIParser::getUnsigned(unsigned &Result) { |
| 226 | assert(Token.hasIntegerValue() && "Expected a token with an integer value"); |
| 227 | const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1; |
| 228 | uint64_t Val64 = Token.integerValue().getLimitedValue(Limit); |
| 229 | if (Val64 == Limit) |
| 230 | return error("expected 32-bit integer (too large)"); |
| 231 | Result = Val64; |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | bool MIParser::parseMBBOperand(MachineOperand &Dest) { |
| 236 | assert(Token.is(MIToken::MachineBasicBlock)); |
| 237 | unsigned Number; |
| 238 | if (getUnsigned(Number)) |
| 239 | return true; |
| 240 | auto MBBInfo = MBBSlots.find(Number); |
| 241 | if (MBBInfo == MBBSlots.end()) |
| 242 | return error(Twine("use of undefined machine basic block #") + |
| 243 | Twine(Number)); |
| 244 | MachineBasicBlock *MBB = MBBInfo->second; |
| 245 | if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName()) |
| 246 | return error(Twine("the name of machine basic block #") + Twine(Number) + |
| 247 | " isn't '" + Token.stringValue() + "'"); |
| 248 | Dest = MachineOperand::CreateMBB(MBB); |
| 249 | lex(); |
| 250 | return false; |
| 251 | } |
| 252 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 253 | bool MIParser::parseMachineOperand(MachineOperand &Dest) { |
| 254 | switch (Token.kind()) { |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 255 | case MIToken::underscore: |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 256 | case MIToken::NamedRegister: |
| 257 | return parseRegisterOperand(Dest); |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 258 | case MIToken::IntegerLiteral: |
| 259 | return parseImmediateOperand(Dest); |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 260 | case MIToken::MachineBasicBlock: |
| 261 | return parseMBBOperand(Dest); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 262 | case MIToken::Error: |
| 263 | return true; |
| 264 | default: |
| 265 | // TODO: parse the other machine operands. |
| 266 | return error("expected a machine operand"); |
| 267 | } |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 268 | return false; |
| 269 | } |
| 270 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 271 | void MIParser::initNames2InstrOpCodes() { |
| 272 | if (!Names2InstrOpCodes.empty()) |
| 273 | return; |
| 274 | const auto *TII = MF.getSubtarget().getInstrInfo(); |
| 275 | assert(TII && "Expected target instruction info"); |
| 276 | for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I) |
| 277 | Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I)); |
| 278 | } |
| 279 | |
| 280 | bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) { |
| 281 | initNames2InstrOpCodes(); |
| 282 | auto InstrInfo = Names2InstrOpCodes.find(InstrName); |
| 283 | if (InstrInfo == Names2InstrOpCodes.end()) |
| 284 | return true; |
| 285 | OpCode = InstrInfo->getValue(); |
| 286 | return false; |
| 287 | } |
| 288 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 289 | void MIParser::initNames2Regs() { |
| 290 | if (!Names2Regs.empty()) |
| 291 | return; |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 292 | // The '%noreg' register is the register 0. |
| 293 | Names2Regs.insert(std::make_pair("noreg", 0)); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 294 | const auto *TRI = MF.getSubtarget().getRegisterInfo(); |
| 295 | assert(TRI && "Expected target register info"); |
| 296 | for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) { |
| 297 | bool WasInserted = |
| 298 | Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I)) |
| 299 | .second; |
| 300 | (void)WasInserted; |
| 301 | assert(WasInserted && "Expected registers to be unique case-insensitively"); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) { |
| 306 | initNames2Regs(); |
| 307 | auto RegInfo = Names2Regs.find(RegName); |
| 308 | if (RegInfo == Names2Regs.end()) |
| 309 | return true; |
| 310 | Reg = RegInfo->getValue(); |
| 311 | return false; |
| 312 | } |
| 313 | |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 314 | MachineInstr * |
| 315 | llvm::parseMachineInstr(SourceMgr &SM, MachineFunction &MF, StringRef Src, |
| 316 | const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots, |
| 317 | SMDiagnostic &Error) { |
| 318 | return MIParser(SM, MF, Error, Src, MBBSlots).parse(); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 319 | } |