blob: 3b820c3cdee9a8f7a9e845ae4bbb5b527093efb2 [file] [log] [blame]
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001//===- 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 Lorenz91370c52015-06-22 20:37:46 +000015#include "MILexer.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000016#include "llvm/ADT/StringMap.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000017#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000018#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000021#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000023#include "llvm/IR/Instructions.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000024#include "llvm/IR/Module.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000025#include "llvm/Support/raw_ostream.h"
26#include "llvm/Support/SourceMgr.h"
27#include "llvm/Target/TargetSubtargetInfo.h"
28#include "llvm/Target/TargetInstrInfo.h"
29
30using namespace llvm;
31
32namespace {
33
Alex Lorenz36962cd2015-07-07 02:08:46 +000034/// A wrapper struct around the 'MachineOperand' struct that includes a source
35/// range.
36struct MachineOperandWithLocation {
37 MachineOperand Operand;
38 StringRef::iterator Begin;
39 StringRef::iterator End;
40
41 MachineOperandWithLocation(const MachineOperand &Operand,
42 StringRef::iterator Begin, StringRef::iterator End)
43 : Operand(Operand), Begin(Begin), End(End) {}
44};
45
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000046class MIParser {
47 SourceMgr &SM;
48 MachineFunction &MF;
49 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000050 StringRef Source, CurrentSource;
51 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000052 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000053 /// Maps from indices to unnamed global values and metadata nodes.
54 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000055 /// Maps from instruction names to op codes.
56 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000057 /// Maps from register names to registers.
58 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000059 /// Maps from register mask names to register masks.
60 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000061 /// Maps from subregister names to subregister indices.
62 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000063
64public:
65 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000066 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000067 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000068
Alex Lorenz91370c52015-06-22 20:37:46 +000069 void lex();
70
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000071 /// Report an error at the current location with the given message.
72 ///
73 /// This function always return true.
74 bool error(const Twine &Msg);
75
Alex Lorenz91370c52015-06-22 20:37:46 +000076 /// Report an error at the given location with the given message.
77 ///
78 /// This function always return true.
79 bool error(StringRef::iterator Loc, const Twine &Msg);
80
Alex Lorenz3708a642015-06-30 17:47:50 +000081 bool parse(MachineInstr *&MI);
Alex Lorenzf09df002015-06-30 18:16:42 +000082 bool parseMBB(MachineBasicBlock *&MBB);
Alex Lorenz9fab3702015-07-14 21:24:41 +000083 bool parseNamedRegister(unsigned &Reg);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000084
Alex Lorenzf3db51de2015-06-23 16:35:26 +000085 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +000086 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +000087 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000088 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +000089 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +000090 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +000091 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000092 bool parseStackObjectOperand(MachineOperand &Dest);
93 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +000094 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +000095 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000096 bool parseMachineOperand(MachineOperand &Dest);
97
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000098private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +000099 /// Convert the integer literal in the current token into an unsigned integer.
100 ///
101 /// Return true if an error occurred.
102 bool getUnsigned(unsigned &Result);
103
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000104 void initNames2InstrOpCodes();
105
106 /// Try to convert an instruction name to an opcode. Return true if the
107 /// instruction name is invalid.
108 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000109
Alex Lorenze5a44662015-07-17 00:24:15 +0000110 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000111
Alex Lorenz36962cd2015-07-07 02:08:46 +0000112 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
113 const MCInstrDesc &MCID);
114
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000115 void initNames2Regs();
116
117 /// Try to convert a register name to a register number. Return true if the
118 /// register name is invalid.
119 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000120
121 void initNames2RegMasks();
122
123 /// Check if the given identifier is a name of a register mask.
124 ///
125 /// Return null if the identifier isn't a register mask.
126 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000127
128 void initNames2SubRegIndices();
129
130 /// Check if the given identifier is a name of a subregister index.
131 ///
132 /// Return 0 if the name isn't a subregister index class.
133 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000134};
135
136} // end anonymous namespace
137
138MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000139 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000140 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000141 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000142 Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000143
Alex Lorenz91370c52015-06-22 20:37:46 +0000144void MIParser::lex() {
145 CurrentSource = lexMIToken(
146 CurrentSource, Token,
147 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
148}
149
150bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
151
152bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000153 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
154 Error = SMDiagnostic(
155 SM, SMLoc(),
156 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
157 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000158 return true;
159}
160
Alex Lorenz3708a642015-06-30 17:47:50 +0000161bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000162 lex();
163
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000164 // Parse any register operands before '='
165 // TODO: Allow parsing of multiple operands before '='
166 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000167 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000168 if (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000169 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000170 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000171 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000172 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz3708a642015-06-30 17:47:50 +0000173 if (Token.isNot(MIToken::equal))
174 return error("expected '='");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000175 lex();
176 }
177
Alex Lorenze5a44662015-07-17 00:24:15 +0000178 unsigned OpCode, Flags = 0;
179 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000180 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000181
Alex Lorenze5a44662015-07-17 00:24:15 +0000182 // TODO: Parse the bundle instruction flags and memory operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000183
184 // Parse the remaining machine operands.
185 while (Token.isNot(MIToken::Eof)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000186 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000187 if (parseMachineOperand(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000188 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000189 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000190 if (Token.is(MIToken::Eof))
191 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000192 if (Token.isNot(MIToken::comma))
193 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000194 lex();
195 }
196
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000197 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000198 if (!MCID.isVariadic()) {
199 // FIXME: Move the implicit operand verification to the machine verifier.
200 if (verifyImplicitOperands(Operands, MCID))
201 return true;
202 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000203
Alex Lorenzcb268d42015-07-06 23:07:26 +0000204 // TODO: Check for extraneous machine operands.
Alex Lorenz3708a642015-06-30 17:47:50 +0000205 MI = MF.CreateMachineInstr(MCID, DebugLoc(), /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000206 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000207 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000208 MI->addOperand(MF, Operand.Operand);
Alex Lorenz3708a642015-06-30 17:47:50 +0000209 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000210}
211
Alex Lorenzf09df002015-06-30 18:16:42 +0000212bool MIParser::parseMBB(MachineBasicBlock *&MBB) {
213 lex();
214 if (Token.isNot(MIToken::MachineBasicBlock))
215 return error("expected a machine basic block reference");
216 if (parseMBBReference(MBB))
217 return true;
218 lex();
219 if (Token.isNot(MIToken::Eof))
220 return error(
221 "expected end of string after the machine basic block reference");
222 return false;
223}
224
Alex Lorenz9fab3702015-07-14 21:24:41 +0000225bool MIParser::parseNamedRegister(unsigned &Reg) {
226 lex();
227 if (Token.isNot(MIToken::NamedRegister))
228 return error("expected a named register");
229 if (parseRegister(Reg))
230 return 0;
231 lex();
232 if (Token.isNot(MIToken::Eof))
233 return error("expected end of string after the register reference");
234 return false;
235}
236
Alex Lorenz36962cd2015-07-07 02:08:46 +0000237static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
238 assert(MO.isImplicit());
239 return MO.isDef() ? "implicit-def" : "implicit";
240}
241
242static std::string getRegisterName(const TargetRegisterInfo *TRI,
243 unsigned Reg) {
244 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
245 return StringRef(TRI->getName(Reg)).lower();
246}
247
248bool MIParser::verifyImplicitOperands(
249 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
250 if (MCID.isCall())
251 // We can't verify call instructions as they can contain arbitrary implicit
252 // register and register mask operands.
253 return false;
254
255 // Gather all the expected implicit operands.
256 SmallVector<MachineOperand, 4> ImplicitOperands;
257 if (MCID.ImplicitDefs)
258 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
259 ImplicitOperands.push_back(
260 MachineOperand::CreateReg(*ImpDefs, true, true));
261 if (MCID.ImplicitUses)
262 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
263 ImplicitOperands.push_back(
264 MachineOperand::CreateReg(*ImpUses, false, true));
265
266 const auto *TRI = MF.getSubtarget().getRegisterInfo();
267 assert(TRI && "Expected target register info");
268 size_t I = ImplicitOperands.size(), J = Operands.size();
269 while (I) {
270 --I;
271 if (J) {
272 --J;
273 const auto &ImplicitOperand = ImplicitOperands[I];
274 const auto &Operand = Operands[J].Operand;
275 if (ImplicitOperand.isIdenticalTo(Operand))
276 continue;
277 if (Operand.isReg() && Operand.isImplicit()) {
278 return error(Operands[J].Begin,
279 Twine("expected an implicit register operand '") +
280 printImplicitRegisterFlag(ImplicitOperand) + " %" +
281 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
282 }
283 }
284 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
285 // insead of reporting an error at this location:
286 // %eax = MOV32r0
287 // ^
288 // report the error at the following location:
289 // %eax = MOV32r0
290 // ^
291 return error(J < Operands.size() ? Operands[J].End : Token.location(),
292 Twine("missing implicit register operand '") +
293 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
294 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
295 }
296 return false;
297}
298
Alex Lorenze5a44662015-07-17 00:24:15 +0000299bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
300 if (Token.is(MIToken::kw_frame_setup)) {
301 Flags |= MachineInstr::FrameSetup;
302 lex();
303 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000304 if (Token.isNot(MIToken::Identifier))
305 return error("expected a machine instruction");
306 StringRef InstrName = Token.stringValue();
307 if (parseInstrName(InstrName, OpCode))
308 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000309 lex();
310 return false;
311}
312
313bool MIParser::parseRegister(unsigned &Reg) {
314 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000315 case MIToken::underscore:
316 Reg = 0;
317 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000318 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000319 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000320 if (getRegisterByName(Name, Reg))
321 return error(Twine("unknown register name '") + Name + "'");
322 break;
323 }
Alex Lorenz53464512015-07-10 22:51:20 +0000324 case MIToken::VirtualRegister: {
325 unsigned ID;
326 if (getUnsigned(ID))
327 return true;
328 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
329 if (RegInfo == PFS.VirtualRegisterSlots.end())
330 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
331 "'");
332 Reg = RegInfo->second;
333 break;
334 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000335 // TODO: Parse other register kinds.
336 default:
337 llvm_unreachable("The current token should be a register");
338 }
339 return false;
340}
341
Alex Lorenzcb268d42015-07-06 23:07:26 +0000342bool MIParser::parseRegisterFlag(unsigned &Flags) {
343 switch (Token.kind()) {
344 case MIToken::kw_implicit:
345 Flags |= RegState::Implicit;
346 break;
347 case MIToken::kw_implicit_define:
348 Flags |= RegState::ImplicitDefine;
349 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000350 case MIToken::kw_dead:
351 Flags |= RegState::Dead;
352 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000353 case MIToken::kw_killed:
354 Flags |= RegState::Kill;
355 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000356 case MIToken::kw_undef:
357 Flags |= RegState::Undef;
358 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000359 // TODO: report an error when we specify the same flag more than once.
360 // TODO: parse the other register flags.
361 default:
362 llvm_unreachable("The current token should be a register flag");
363 }
364 lex();
365 return false;
366}
367
Alex Lorenz2eacca82015-07-13 23:24:34 +0000368bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
369 assert(Token.is(MIToken::colon));
370 lex();
371 if (Token.isNot(MIToken::Identifier))
372 return error("expected a subregister index after ':'");
373 auto Name = Token.stringValue();
374 SubReg = getSubRegIndex(Name);
375 if (!SubReg)
376 return error(Twine("use of unknown subregister index '") + Name + "'");
377 lex();
378 return false;
379}
380
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000381bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
382 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000383 unsigned Flags = IsDef ? RegState::Define : 0;
384 while (Token.isRegisterFlag()) {
385 if (parseRegisterFlag(Flags))
386 return true;
387 }
388 if (!Token.isRegister())
389 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000390 if (parseRegister(Reg))
391 return true;
392 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000393 unsigned SubReg = 0;
394 if (Token.is(MIToken::colon)) {
395 if (parseSubRegisterIndex(SubReg))
396 return true;
397 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000398 Dest = MachineOperand::CreateReg(
399 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000400 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
401 /*isEarlyClobber=*/false, SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000402 return false;
403}
404
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000405bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
406 assert(Token.is(MIToken::IntegerLiteral));
407 const APSInt &Int = Token.integerValue();
408 if (Int.getMinSignedBits() > 64)
409 // TODO: Replace this with an error when we can parse CIMM Machine Operands.
410 llvm_unreachable("Can't parse large integer literals yet!");
411 Dest = MachineOperand::CreateImm(Int.getExtValue());
412 lex();
413 return false;
414}
415
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000416bool MIParser::getUnsigned(unsigned &Result) {
417 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
418 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
419 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
420 if (Val64 == Limit)
421 return error("expected 32-bit integer (too large)");
422 Result = Val64;
423 return false;
424}
425
Alex Lorenzf09df002015-06-30 18:16:42 +0000426bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000427 assert(Token.is(MIToken::MachineBasicBlock));
428 unsigned Number;
429 if (getUnsigned(Number))
430 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000431 auto MBBInfo = PFS.MBBSlots.find(Number);
432 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000433 return error(Twine("use of undefined machine basic block #") +
434 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000435 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000436 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
437 return error(Twine("the name of machine basic block #") + Twine(Number) +
438 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000439 return false;
440}
441
442bool MIParser::parseMBBOperand(MachineOperand &Dest) {
443 MachineBasicBlock *MBB;
444 if (parseMBBReference(MBB))
445 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000446 Dest = MachineOperand::CreateMBB(MBB);
447 lex();
448 return false;
449}
450
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000451bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
452 assert(Token.is(MIToken::StackObject));
453 unsigned ID;
454 if (getUnsigned(ID))
455 return true;
456 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
457 if (ObjectInfo == PFS.StackObjectSlots.end())
458 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
459 "'");
460 StringRef Name;
461 if (const auto *Alloca =
462 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
463 Name = Alloca->getName();
464 if (!Token.stringValue().empty() && Token.stringValue() != Name)
465 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
466 "' isn't '" + Token.stringValue() + "'");
467 lex();
468 Dest = MachineOperand::CreateFI(ObjectInfo->second);
469 return false;
470}
471
472bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
473 assert(Token.is(MIToken::FixedStackObject));
474 unsigned ID;
475 if (getUnsigned(ID))
476 return true;
477 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
478 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
479 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
480 Twine(ID) + "'");
481 lex();
482 Dest = MachineOperand::CreateFI(ObjectInfo->second);
483 return false;
484}
485
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000486bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
487 switch (Token.kind()) {
488 case MIToken::NamedGlobalValue: {
489 auto Name = Token.stringValue();
490 const Module *M = MF.getFunction()->getParent();
491 if (const auto *GV = M->getNamedValue(Name)) {
492 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
493 break;
494 }
495 return error(Twine("use of undefined global value '@") + Name + "'");
496 }
497 case MIToken::GlobalValue: {
498 unsigned GVIdx;
499 if (getUnsigned(GVIdx))
500 return true;
501 if (GVIdx >= IRSlots.GlobalValues.size())
502 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
503 "'");
504 Dest = MachineOperand::CreateGA(IRSlots.GlobalValues[GVIdx],
505 /*Offset=*/0);
506 break;
507 }
508 default:
509 llvm_unreachable("The current token should be a global value");
510 }
511 // TODO: Parse offset and target flags.
512 lex();
513 return false;
514}
515
Alex Lorenz31d70682015-07-15 23:38:35 +0000516bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
517 assert(Token.is(MIToken::JumpTableIndex));
518 unsigned ID;
519 if (getUnsigned(ID))
520 return true;
521 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
522 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
523 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
524 lex();
525 // TODO: Parse target flags.
526 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
527 return false;
528}
529
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000530bool MIParser::parseMachineOperand(MachineOperand &Dest) {
531 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000532 case MIToken::kw_implicit:
533 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000534 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000535 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000536 case MIToken::kw_undef:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000537 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000538 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000539 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000540 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000541 case MIToken::IntegerLiteral:
542 return parseImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000543 case MIToken::MachineBasicBlock:
544 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000545 case MIToken::StackObject:
546 return parseStackObjectOperand(Dest);
547 case MIToken::FixedStackObject:
548 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000549 case MIToken::GlobalValue:
550 case MIToken::NamedGlobalValue:
551 return parseGlobalAddressOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000552 case MIToken::JumpTableIndex:
553 return parseJumpTableIndexOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000554 case MIToken::Error:
555 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000556 case MIToken::Identifier:
557 if (const auto *RegMask = getRegMask(Token.stringValue())) {
558 Dest = MachineOperand::CreateRegMask(RegMask);
559 lex();
560 break;
561 }
562 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000563 default:
564 // TODO: parse the other machine operands.
565 return error("expected a machine operand");
566 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000567 return false;
568}
569
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000570void MIParser::initNames2InstrOpCodes() {
571 if (!Names2InstrOpCodes.empty())
572 return;
573 const auto *TII = MF.getSubtarget().getInstrInfo();
574 assert(TII && "Expected target instruction info");
575 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
576 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
577}
578
579bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
580 initNames2InstrOpCodes();
581 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
582 if (InstrInfo == Names2InstrOpCodes.end())
583 return true;
584 OpCode = InstrInfo->getValue();
585 return false;
586}
587
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000588void MIParser::initNames2Regs() {
589 if (!Names2Regs.empty())
590 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +0000591 // The '%noreg' register is the register 0.
592 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000593 const auto *TRI = MF.getSubtarget().getRegisterInfo();
594 assert(TRI && "Expected target register info");
595 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
596 bool WasInserted =
597 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
598 .second;
599 (void)WasInserted;
600 assert(WasInserted && "Expected registers to be unique case-insensitively");
601 }
602}
603
604bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
605 initNames2Regs();
606 auto RegInfo = Names2Regs.find(RegName);
607 if (RegInfo == Names2Regs.end())
608 return true;
609 Reg = RegInfo->getValue();
610 return false;
611}
612
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000613void MIParser::initNames2RegMasks() {
614 if (!Names2RegMasks.empty())
615 return;
616 const auto *TRI = MF.getSubtarget().getRegisterInfo();
617 assert(TRI && "Expected target register info");
618 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
619 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
620 assert(RegMasks.size() == RegMaskNames.size());
621 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
622 Names2RegMasks.insert(
623 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
624}
625
626const uint32_t *MIParser::getRegMask(StringRef Identifier) {
627 initNames2RegMasks();
628 auto RegMaskInfo = Names2RegMasks.find(Identifier);
629 if (RegMaskInfo == Names2RegMasks.end())
630 return nullptr;
631 return RegMaskInfo->getValue();
632}
633
Alex Lorenz2eacca82015-07-13 23:24:34 +0000634void MIParser::initNames2SubRegIndices() {
635 if (!Names2SubRegIndices.empty())
636 return;
637 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
638 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
639 Names2SubRegIndices.insert(
640 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
641}
642
643unsigned MIParser::getSubRegIndex(StringRef Name) {
644 initNames2SubRegIndices();
645 auto SubRegInfo = Names2SubRegIndices.find(Name);
646 if (SubRegInfo == Names2SubRegIndices.end())
647 return 0;
648 return SubRegInfo->getValue();
649}
650
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000651bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
652 MachineFunction &MF, StringRef Src,
653 const PerFunctionMIParsingState &PFS,
654 const SlotMapping &IRSlots, SMDiagnostic &Error) {
655 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000656}
Alex Lorenzf09df002015-06-30 18:16:42 +0000657
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000658bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
659 MachineFunction &MF, StringRef Src,
660 const PerFunctionMIParsingState &PFS,
661 const SlotMapping &IRSlots, SMDiagnostic &Error) {
662 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000663}
Alex Lorenz9fab3702015-07-14 21:24:41 +0000664
665bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
666 MachineFunction &MF, StringRef Src,
667 const PerFunctionMIParsingState &PFS,
668 const SlotMapping &IRSlots,
669 SMDiagnostic &Error) {
670 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseNamedRegister(Reg);
671}