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 | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 35 | /// Maps from instruction names to op codes. |
| 36 | StringMap<unsigned> Names2InstrOpCodes; |
| 37 | |
| 38 | public: |
| 39 | MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error, |
| 40 | StringRef Source); |
| 41 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame^] | 42 | void lex(); |
| 43 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 44 | /// Report an error at the current location with the given message. |
| 45 | /// |
| 46 | /// This function always return true. |
| 47 | bool error(const Twine &Msg); |
| 48 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame^] | 49 | /// Report an error at the given location with the given message. |
| 50 | /// |
| 51 | /// This function always return true. |
| 52 | bool error(StringRef::iterator Loc, const Twine &Msg); |
| 53 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 54 | MachineInstr *parse(); |
| 55 | |
| 56 | private: |
| 57 | void initNames2InstrOpCodes(); |
| 58 | |
| 59 | /// Try to convert an instruction name to an opcode. Return true if the |
| 60 | /// instruction name is invalid. |
| 61 | bool parseInstrName(StringRef InstrName, unsigned &OpCode); |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame^] | 62 | |
| 63 | bool parseInstruction(unsigned &OpCode); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | } // end anonymous namespace |
| 67 | |
| 68 | MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error, |
| 69 | StringRef Source) |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame^] | 70 | : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source), |
| 71 | Token(MIToken::Error, StringRef()) {} |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 72 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame^] | 73 | void MIParser::lex() { |
| 74 | CurrentSource = lexMIToken( |
| 75 | CurrentSource, Token, |
| 76 | [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); }); |
| 77 | } |
| 78 | |
| 79 | bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); } |
| 80 | |
| 81 | bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) { |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 82 | // TODO: Get the proper location in the MIR file, not just a location inside |
| 83 | // the string. |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame^] | 84 | assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size())); |
| 85 | Error = SMDiagnostic( |
| 86 | SM, SMLoc(), |
| 87 | SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1, |
| 88 | Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 89 | return true; |
| 90 | } |
| 91 | |
| 92 | MachineInstr *MIParser::parse() { |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame^] | 93 | lex(); |
| 94 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 95 | unsigned OpCode; |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame^] | 96 | if (Token.isError() || parseInstruction(OpCode)) |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 97 | return nullptr; |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 98 | |
| 99 | // TODO: Parse the rest of instruction - machine operands, etc. |
| 100 | const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode); |
| 101 | auto *MI = MF.CreateMachineInstr(MCID, DebugLoc()); |
| 102 | return MI; |
| 103 | } |
| 104 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame^] | 105 | bool MIParser::parseInstruction(unsigned &OpCode) { |
| 106 | if (Token.isNot(MIToken::Identifier)) |
| 107 | return error("expected a machine instruction"); |
| 108 | StringRef InstrName = Token.stringValue(); |
| 109 | if (parseInstrName(InstrName, OpCode)) |
| 110 | return error(Twine("unknown machine instruction name '") + InstrName + "'"); |
| 111 | return false; |
| 112 | } |
| 113 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 114 | void MIParser::initNames2InstrOpCodes() { |
| 115 | if (!Names2InstrOpCodes.empty()) |
| 116 | return; |
| 117 | const auto *TII = MF.getSubtarget().getInstrInfo(); |
| 118 | assert(TII && "Expected target instruction info"); |
| 119 | for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I) |
| 120 | Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I)); |
| 121 | } |
| 122 | |
| 123 | bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) { |
| 124 | initNames2InstrOpCodes(); |
| 125 | auto InstrInfo = Names2InstrOpCodes.find(InstrName); |
| 126 | if (InstrInfo == Names2InstrOpCodes.end()) |
| 127 | return true; |
| 128 | OpCode = InstrInfo->getValue(); |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | MachineInstr *llvm::parseMachineInstr(SourceMgr &SM, MachineFunction &MF, |
| 133 | StringRef Src, SMDiagnostic &Error) { |
| 134 | return MIParser(SM, MF, Error, Src).parse(); |
| 135 | } |