blob: 212996bfaf58a99ab3e694fbbd1220c41fe99221 [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 Lorenzf4baeb52015-07-21 22:28:27 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000024#include "llvm/IR/Instructions.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000025#include "llvm/IR/Module.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000026#include "llvm/Support/raw_ostream.h"
27#include "llvm/Support/SourceMgr.h"
28#include "llvm/Target/TargetSubtargetInfo.h"
29#include "llvm/Target/TargetInstrInfo.h"
30
31using namespace llvm;
32
33namespace {
34
Alex Lorenzb29554d2015-07-20 20:31:01 +000035struct StringValueUtility {
36 StringRef String;
37 std::string UnescapedString;
38
39 StringValueUtility(const MIToken &Token) {
40 if (Token.isStringValueQuoted()) {
41 Token.unescapeQuotedStringValue(UnescapedString);
42 String = UnescapedString;
43 return;
44 }
45 String = Token.stringValue();
46 }
47
48 operator StringRef() const { return String; }
49};
50
Alex Lorenz36962cd2015-07-07 02:08:46 +000051/// A wrapper struct around the 'MachineOperand' struct that includes a source
52/// range.
53struct MachineOperandWithLocation {
54 MachineOperand Operand;
55 StringRef::iterator Begin;
56 StringRef::iterator End;
57
58 MachineOperandWithLocation(const MachineOperand &Operand,
59 StringRef::iterator Begin, StringRef::iterator End)
60 : Operand(Operand), Begin(Begin), End(End) {}
61};
62
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000063class MIParser {
64 SourceMgr &SM;
65 MachineFunction &MF;
66 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000067 StringRef Source, CurrentSource;
68 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000069 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000070 /// Maps from indices to unnamed global values and metadata nodes.
71 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000072 /// Maps from instruction names to op codes.
73 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000074 /// Maps from register names to registers.
75 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000076 /// Maps from register mask names to register masks.
77 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000078 /// Maps from subregister names to subregister indices.
79 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000080
81public:
82 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000083 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000084 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000085
Alex Lorenz91370c52015-06-22 20:37:46 +000086 void lex();
87
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000088 /// Report an error at the current location with the given message.
89 ///
90 /// This function always return true.
91 bool error(const Twine &Msg);
92
Alex Lorenz91370c52015-06-22 20:37:46 +000093 /// Report an error at the given location with the given message.
94 ///
95 /// This function always return true.
96 bool error(StringRef::iterator Loc, const Twine &Msg);
97
Alex Lorenz3708a642015-06-30 17:47:50 +000098 bool parse(MachineInstr *&MI);
Alex Lorenzf09df002015-06-30 18:16:42 +000099 bool parseMBB(MachineBasicBlock *&MBB);
Alex Lorenz9fab3702015-07-14 21:24:41 +0000100 bool parseNamedRegister(unsigned &Reg);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000101
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000102 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000103 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000104 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000105 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000106 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000107 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000108 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000109 bool parseStackObjectOperand(MachineOperand &Dest);
110 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000111 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000112 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000113 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000114 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +0000115 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000116 bool parseCFIOffset(int &Offset);
117 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000118 bool parseMachineOperand(MachineOperand &Dest);
119
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000120private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000121 /// Convert the integer literal in the current token into an unsigned integer.
122 ///
123 /// Return true if an error occurred.
124 bool getUnsigned(unsigned &Result);
125
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000126 void initNames2InstrOpCodes();
127
128 /// Try to convert an instruction name to an opcode. Return true if the
129 /// instruction name is invalid.
130 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000131
Alex Lorenze5a44662015-07-17 00:24:15 +0000132 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000133
Alex Lorenz36962cd2015-07-07 02:08:46 +0000134 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
135 const MCInstrDesc &MCID);
136
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000137 void initNames2Regs();
138
139 /// Try to convert a register name to a register number. Return true if the
140 /// register name is invalid.
141 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000142
143 void initNames2RegMasks();
144
145 /// Check if the given identifier is a name of a register mask.
146 ///
147 /// Return null if the identifier isn't a register mask.
148 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000149
150 void initNames2SubRegIndices();
151
152 /// Check if the given identifier is a name of a subregister index.
153 ///
154 /// Return 0 if the name isn't a subregister index class.
155 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000156};
157
158} // end anonymous namespace
159
160MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000161 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000162 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000163 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000164 Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000165
Alex Lorenz91370c52015-06-22 20:37:46 +0000166void MIParser::lex() {
167 CurrentSource = lexMIToken(
168 CurrentSource, Token,
169 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
170}
171
172bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
173
174bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000175 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
176 Error = SMDiagnostic(
177 SM, SMLoc(),
178 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
179 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000180 return true;
181}
182
Alex Lorenz3708a642015-06-30 17:47:50 +0000183bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000184 lex();
185
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000186 // Parse any register operands before '='
187 // TODO: Allow parsing of multiple operands before '='
188 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000189 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000190 if (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000191 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000192 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000193 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000194 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz3708a642015-06-30 17:47:50 +0000195 if (Token.isNot(MIToken::equal))
196 return error("expected '='");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000197 lex();
198 }
199
Alex Lorenze5a44662015-07-17 00:24:15 +0000200 unsigned OpCode, Flags = 0;
201 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000202 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000203
Alex Lorenze5a44662015-07-17 00:24:15 +0000204 // TODO: Parse the bundle instruction flags and memory operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000205
206 // Parse the remaining machine operands.
207 while (Token.isNot(MIToken::Eof)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000208 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000209 if (parseMachineOperand(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000210 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000211 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000212 if (Token.is(MIToken::Eof))
213 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000214 if (Token.isNot(MIToken::comma))
215 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000216 lex();
217 }
218
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000219 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000220 if (!MCID.isVariadic()) {
221 // FIXME: Move the implicit operand verification to the machine verifier.
222 if (verifyImplicitOperands(Operands, MCID))
223 return true;
224 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000225
Alex Lorenzcb268d42015-07-06 23:07:26 +0000226 // TODO: Check for extraneous machine operands.
Alex Lorenz3708a642015-06-30 17:47:50 +0000227 MI = MF.CreateMachineInstr(MCID, DebugLoc(), /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000228 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000229 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000230 MI->addOperand(MF, Operand.Operand);
Alex Lorenz3708a642015-06-30 17:47:50 +0000231 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000232}
233
Alex Lorenzf09df002015-06-30 18:16:42 +0000234bool MIParser::parseMBB(MachineBasicBlock *&MBB) {
235 lex();
236 if (Token.isNot(MIToken::MachineBasicBlock))
237 return error("expected a machine basic block reference");
238 if (parseMBBReference(MBB))
239 return true;
240 lex();
241 if (Token.isNot(MIToken::Eof))
242 return error(
243 "expected end of string after the machine basic block reference");
244 return false;
245}
246
Alex Lorenz9fab3702015-07-14 21:24:41 +0000247bool MIParser::parseNamedRegister(unsigned &Reg) {
248 lex();
249 if (Token.isNot(MIToken::NamedRegister))
250 return error("expected a named register");
251 if (parseRegister(Reg))
252 return 0;
253 lex();
254 if (Token.isNot(MIToken::Eof))
255 return error("expected end of string after the register reference");
256 return false;
257}
258
Alex Lorenz36962cd2015-07-07 02:08:46 +0000259static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
260 assert(MO.isImplicit());
261 return MO.isDef() ? "implicit-def" : "implicit";
262}
263
264static std::string getRegisterName(const TargetRegisterInfo *TRI,
265 unsigned Reg) {
266 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
267 return StringRef(TRI->getName(Reg)).lower();
268}
269
270bool MIParser::verifyImplicitOperands(
271 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
272 if (MCID.isCall())
273 // We can't verify call instructions as they can contain arbitrary implicit
274 // register and register mask operands.
275 return false;
276
277 // Gather all the expected implicit operands.
278 SmallVector<MachineOperand, 4> ImplicitOperands;
279 if (MCID.ImplicitDefs)
280 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
281 ImplicitOperands.push_back(
282 MachineOperand::CreateReg(*ImpDefs, true, true));
283 if (MCID.ImplicitUses)
284 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
285 ImplicitOperands.push_back(
286 MachineOperand::CreateReg(*ImpUses, false, true));
287
288 const auto *TRI = MF.getSubtarget().getRegisterInfo();
289 assert(TRI && "Expected target register info");
290 size_t I = ImplicitOperands.size(), J = Operands.size();
291 while (I) {
292 --I;
293 if (J) {
294 --J;
295 const auto &ImplicitOperand = ImplicitOperands[I];
296 const auto &Operand = Operands[J].Operand;
297 if (ImplicitOperand.isIdenticalTo(Operand))
298 continue;
299 if (Operand.isReg() && Operand.isImplicit()) {
300 return error(Operands[J].Begin,
301 Twine("expected an implicit register operand '") +
302 printImplicitRegisterFlag(ImplicitOperand) + " %" +
303 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
304 }
305 }
306 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
307 // insead of reporting an error at this location:
308 // %eax = MOV32r0
309 // ^
310 // report the error at the following location:
311 // %eax = MOV32r0
312 // ^
313 return error(J < Operands.size() ? Operands[J].End : Token.location(),
314 Twine("missing implicit register operand '") +
315 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
316 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
317 }
318 return false;
319}
320
Alex Lorenze5a44662015-07-17 00:24:15 +0000321bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
322 if (Token.is(MIToken::kw_frame_setup)) {
323 Flags |= MachineInstr::FrameSetup;
324 lex();
325 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000326 if (Token.isNot(MIToken::Identifier))
327 return error("expected a machine instruction");
328 StringRef InstrName = Token.stringValue();
329 if (parseInstrName(InstrName, OpCode))
330 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000331 lex();
332 return false;
333}
334
335bool MIParser::parseRegister(unsigned &Reg) {
336 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000337 case MIToken::underscore:
338 Reg = 0;
339 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000340 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000341 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000342 if (getRegisterByName(Name, Reg))
343 return error(Twine("unknown register name '") + Name + "'");
344 break;
345 }
Alex Lorenz53464512015-07-10 22:51:20 +0000346 case MIToken::VirtualRegister: {
347 unsigned ID;
348 if (getUnsigned(ID))
349 return true;
350 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
351 if (RegInfo == PFS.VirtualRegisterSlots.end())
352 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
353 "'");
354 Reg = RegInfo->second;
355 break;
356 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000357 // TODO: Parse other register kinds.
358 default:
359 llvm_unreachable("The current token should be a register");
360 }
361 return false;
362}
363
Alex Lorenzcb268d42015-07-06 23:07:26 +0000364bool MIParser::parseRegisterFlag(unsigned &Flags) {
365 switch (Token.kind()) {
366 case MIToken::kw_implicit:
367 Flags |= RegState::Implicit;
368 break;
369 case MIToken::kw_implicit_define:
370 Flags |= RegState::ImplicitDefine;
371 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000372 case MIToken::kw_dead:
373 Flags |= RegState::Dead;
374 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000375 case MIToken::kw_killed:
376 Flags |= RegState::Kill;
377 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000378 case MIToken::kw_undef:
379 Flags |= RegState::Undef;
380 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000381 // TODO: report an error when we specify the same flag more than once.
382 // TODO: parse the other register flags.
383 default:
384 llvm_unreachable("The current token should be a register flag");
385 }
386 lex();
387 return false;
388}
389
Alex Lorenz2eacca82015-07-13 23:24:34 +0000390bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
391 assert(Token.is(MIToken::colon));
392 lex();
393 if (Token.isNot(MIToken::Identifier))
394 return error("expected a subregister index after ':'");
395 auto Name = Token.stringValue();
396 SubReg = getSubRegIndex(Name);
397 if (!SubReg)
398 return error(Twine("use of unknown subregister index '") + Name + "'");
399 lex();
400 return false;
401}
402
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000403bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
404 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000405 unsigned Flags = IsDef ? RegState::Define : 0;
406 while (Token.isRegisterFlag()) {
407 if (parseRegisterFlag(Flags))
408 return true;
409 }
410 if (!Token.isRegister())
411 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000412 if (parseRegister(Reg))
413 return true;
414 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000415 unsigned SubReg = 0;
416 if (Token.is(MIToken::colon)) {
417 if (parseSubRegisterIndex(SubReg))
418 return true;
419 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000420 Dest = MachineOperand::CreateReg(
421 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000422 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
423 /*isEarlyClobber=*/false, SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000424 return false;
425}
426
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000427bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
428 assert(Token.is(MIToken::IntegerLiteral));
429 const APSInt &Int = Token.integerValue();
430 if (Int.getMinSignedBits() > 64)
431 // TODO: Replace this with an error when we can parse CIMM Machine Operands.
432 llvm_unreachable("Can't parse large integer literals yet!");
433 Dest = MachineOperand::CreateImm(Int.getExtValue());
434 lex();
435 return false;
436}
437
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000438bool MIParser::getUnsigned(unsigned &Result) {
439 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
440 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
441 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
442 if (Val64 == Limit)
443 return error("expected 32-bit integer (too large)");
444 Result = Val64;
445 return false;
446}
447
Alex Lorenzf09df002015-06-30 18:16:42 +0000448bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000449 assert(Token.is(MIToken::MachineBasicBlock));
450 unsigned Number;
451 if (getUnsigned(Number))
452 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000453 auto MBBInfo = PFS.MBBSlots.find(Number);
454 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000455 return error(Twine("use of undefined machine basic block #") +
456 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000457 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000458 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
459 return error(Twine("the name of machine basic block #") + Twine(Number) +
460 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000461 return false;
462}
463
464bool MIParser::parseMBBOperand(MachineOperand &Dest) {
465 MachineBasicBlock *MBB;
466 if (parseMBBReference(MBB))
467 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000468 Dest = MachineOperand::CreateMBB(MBB);
469 lex();
470 return false;
471}
472
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000473bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
474 assert(Token.is(MIToken::StackObject));
475 unsigned ID;
476 if (getUnsigned(ID))
477 return true;
478 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
479 if (ObjectInfo == PFS.StackObjectSlots.end())
480 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
481 "'");
482 StringRef Name;
483 if (const auto *Alloca =
484 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
485 Name = Alloca->getName();
486 if (!Token.stringValue().empty() && Token.stringValue() != Name)
487 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
488 "' isn't '" + Token.stringValue() + "'");
489 lex();
490 Dest = MachineOperand::CreateFI(ObjectInfo->second);
491 return false;
492}
493
494bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
495 assert(Token.is(MIToken::FixedStackObject));
496 unsigned ID;
497 if (getUnsigned(ID))
498 return true;
499 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
500 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
501 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
502 Twine(ID) + "'");
503 lex();
504 Dest = MachineOperand::CreateFI(ObjectInfo->second);
505 return false;
506}
507
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000508bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
509 switch (Token.kind()) {
Alex Lorenzb29554d2015-07-20 20:31:01 +0000510 case MIToken::NamedGlobalValue:
511 case MIToken::QuotedNamedGlobalValue: {
512 StringValueUtility Name(Token);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000513 const Module *M = MF.getFunction()->getParent();
514 if (const auto *GV = M->getNamedValue(Name)) {
515 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
516 break;
517 }
Alex Lorenzb29554d2015-07-20 20:31:01 +0000518 return error(Twine("use of undefined global value '@") +
519 Token.rawStringValue() + "'");
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000520 }
521 case MIToken::GlobalValue: {
522 unsigned GVIdx;
523 if (getUnsigned(GVIdx))
524 return true;
525 if (GVIdx >= IRSlots.GlobalValues.size())
526 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
527 "'");
528 Dest = MachineOperand::CreateGA(IRSlots.GlobalValues[GVIdx],
529 /*Offset=*/0);
530 break;
531 }
532 default:
533 llvm_unreachable("The current token should be a global value");
534 }
535 // TODO: Parse offset and target flags.
536 lex();
537 return false;
538}
539
Alex Lorenzab980492015-07-20 20:51:18 +0000540bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
541 assert(Token.is(MIToken::ConstantPoolItem));
542 unsigned ID;
543 if (getUnsigned(ID))
544 return true;
545 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
546 if (ConstantInfo == PFS.ConstantPoolSlots.end())
547 return error("use of undefined constant '%const." + Twine(ID) + "'");
548 lex();
549 // TODO: Parse offset and target flags.
550 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
551 return false;
552}
553
Alex Lorenz31d70682015-07-15 23:38:35 +0000554bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
555 assert(Token.is(MIToken::JumpTableIndex));
556 unsigned ID;
557 if (getUnsigned(ID))
558 return true;
559 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
560 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
561 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
562 lex();
563 // TODO: Parse target flags.
564 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
565 return false;
566}
567
Alex Lorenz6ede3742015-07-21 16:59:53 +0000568bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
569 assert(Token.is(MIToken::ExternalSymbol) ||
570 Token.is(MIToken::QuotedExternalSymbol));
571 StringValueUtility Name(Token);
572 const char *Symbol = MF.createExternalSymbolName(Name);
573 lex();
574 // TODO: Parse the target flags.
575 Dest = MachineOperand::CreateES(Symbol);
576 return false;
577}
578
Alex Lorenz35e44462015-07-22 17:58:46 +0000579bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
580 assert(Token.is(MIToken::exclaim));
581 auto Loc = Token.location();
582 lex();
583 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
584 return error("expected metadata id after '!'");
585 unsigned ID;
586 if (getUnsigned(ID))
587 return true;
588 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
589 if (NodeInfo == IRSlots.MetadataNodes.end())
590 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
591 lex();
592 Dest = MachineOperand::CreateMetadata(NodeInfo->second.get());
593 return false;
594}
595
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000596bool MIParser::parseCFIOffset(int &Offset) {
597 if (Token.isNot(MIToken::IntegerLiteral))
598 return error("expected a cfi offset");
599 if (Token.integerValue().getMinSignedBits() > 32)
600 return error("expected a 32 bit integer (the cfi offset is too large)");
601 Offset = (int)Token.integerValue().getExtValue();
602 lex();
603 return false;
604}
605
606bool MIParser::parseCFIOperand(MachineOperand &Dest) {
607 // TODO: Parse the other CFI operands.
608 assert(Token.is(MIToken::kw_cfi_def_cfa_offset));
609 lex();
610 int Offset;
611 if (parseCFIOffset(Offset))
612 return true;
613 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
614 Dest = MachineOperand::CreateCFIIndex(MF.getMMI().addFrameInst(
615 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset)));
616 return false;
617}
618
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000619bool MIParser::parseMachineOperand(MachineOperand &Dest) {
620 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000621 case MIToken::kw_implicit:
622 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000623 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000624 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000625 case MIToken::kw_undef:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000626 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000627 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000628 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000629 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000630 case MIToken::IntegerLiteral:
631 return parseImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000632 case MIToken::MachineBasicBlock:
633 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000634 case MIToken::StackObject:
635 return parseStackObjectOperand(Dest);
636 case MIToken::FixedStackObject:
637 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000638 case MIToken::GlobalValue:
639 case MIToken::NamedGlobalValue:
Alex Lorenzb29554d2015-07-20 20:31:01 +0000640 case MIToken::QuotedNamedGlobalValue:
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000641 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000642 case MIToken::ConstantPoolItem:
643 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000644 case MIToken::JumpTableIndex:
645 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000646 case MIToken::ExternalSymbol:
647 case MIToken::QuotedExternalSymbol:
648 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +0000649 case MIToken::exclaim:
650 return parseMetadataOperand(Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000651 case MIToken::kw_cfi_def_cfa_offset:
652 return parseCFIOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000653 case MIToken::Error:
654 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000655 case MIToken::Identifier:
656 if (const auto *RegMask = getRegMask(Token.stringValue())) {
657 Dest = MachineOperand::CreateRegMask(RegMask);
658 lex();
659 break;
660 }
661 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000662 default:
663 // TODO: parse the other machine operands.
664 return error("expected a machine operand");
665 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000666 return false;
667}
668
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000669void MIParser::initNames2InstrOpCodes() {
670 if (!Names2InstrOpCodes.empty())
671 return;
672 const auto *TII = MF.getSubtarget().getInstrInfo();
673 assert(TII && "Expected target instruction info");
674 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
675 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
676}
677
678bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
679 initNames2InstrOpCodes();
680 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
681 if (InstrInfo == Names2InstrOpCodes.end())
682 return true;
683 OpCode = InstrInfo->getValue();
684 return false;
685}
686
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000687void MIParser::initNames2Regs() {
688 if (!Names2Regs.empty())
689 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +0000690 // The '%noreg' register is the register 0.
691 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000692 const auto *TRI = MF.getSubtarget().getRegisterInfo();
693 assert(TRI && "Expected target register info");
694 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
695 bool WasInserted =
696 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
697 .second;
698 (void)WasInserted;
699 assert(WasInserted && "Expected registers to be unique case-insensitively");
700 }
701}
702
703bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
704 initNames2Regs();
705 auto RegInfo = Names2Regs.find(RegName);
706 if (RegInfo == Names2Regs.end())
707 return true;
708 Reg = RegInfo->getValue();
709 return false;
710}
711
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000712void MIParser::initNames2RegMasks() {
713 if (!Names2RegMasks.empty())
714 return;
715 const auto *TRI = MF.getSubtarget().getRegisterInfo();
716 assert(TRI && "Expected target register info");
717 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
718 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
719 assert(RegMasks.size() == RegMaskNames.size());
720 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
721 Names2RegMasks.insert(
722 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
723}
724
725const uint32_t *MIParser::getRegMask(StringRef Identifier) {
726 initNames2RegMasks();
727 auto RegMaskInfo = Names2RegMasks.find(Identifier);
728 if (RegMaskInfo == Names2RegMasks.end())
729 return nullptr;
730 return RegMaskInfo->getValue();
731}
732
Alex Lorenz2eacca82015-07-13 23:24:34 +0000733void MIParser::initNames2SubRegIndices() {
734 if (!Names2SubRegIndices.empty())
735 return;
736 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
737 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
738 Names2SubRegIndices.insert(
739 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
740}
741
742unsigned MIParser::getSubRegIndex(StringRef Name) {
743 initNames2SubRegIndices();
744 auto SubRegInfo = Names2SubRegIndices.find(Name);
745 if (SubRegInfo == Names2SubRegIndices.end())
746 return 0;
747 return SubRegInfo->getValue();
748}
749
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000750bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
751 MachineFunction &MF, StringRef Src,
752 const PerFunctionMIParsingState &PFS,
753 const SlotMapping &IRSlots, SMDiagnostic &Error) {
754 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000755}
Alex Lorenzf09df002015-06-30 18:16:42 +0000756
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000757bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
758 MachineFunction &MF, StringRef Src,
759 const PerFunctionMIParsingState &PFS,
760 const SlotMapping &IRSlots, SMDiagnostic &Error) {
761 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000762}
Alex Lorenz9fab3702015-07-14 21:24:41 +0000763
764bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
765 MachineFunction &MF, StringRef Src,
766 const PerFunctionMIParsingState &PFS,
767 const SlotMapping &IRSlots,
768 SMDiagnostic &Error) {
769 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseNamedRegister(Reg);
770}