LLVM 17.0.0git
MCTargetAsmParser.h
Go to the documentation of this file.
1//===- llvm/MC/MCTargetAsmParser.h - Target Assembly Parser -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_MC_MCPARSER_MCTARGETASMPARSER_H
10#define LLVM_MC_MCPARSER_MCTARGETASMPARSER_H
11
12#include "llvm/ADT/StringRef.h"
13#include "llvm/MC/MCExpr.h"
18#include "llvm/Support/SMLoc.h"
19#include <cstdint>
20#include <memory>
21
22namespace llvm {
23
24class MCContext;
25class MCInst;
26class MCInstrInfo;
27class MCRegister;
28class MCStreamer;
29class MCSubtargetInfo;
30class MCSymbol;
31template <typename T> class SmallVectorImpl;
32
34
36 AOK_Align, // Rewrite align as .align.
37 AOK_EVEN, // Rewrite even as .even.
38 AOK_Emit, // Rewrite _emit as .byte.
39 AOK_CallInput, // Rewrite in terms of ${N:P}.
40 AOK_Input, // Rewrite in terms of $N.
41 AOK_Output, // Rewrite in terms of $N.
42 AOK_SizeDirective, // Add a sizing directive (e.g., dword ptr).
43 AOK_Label, // Rewrite local labels.
44 AOK_EndOfStatement, // Add EndOfStatement (e.g., "\n\t").
45 AOK_Skip, // Skip emission (e.g., offset/type operators).
46 AOK_IntelExpr // SizeDirective SymDisp [BaseReg + IndexReg * Scale + ImmDisp]
47};
48
49const char AsmRewritePrecedence [] = {
50 2, // AOK_Align
51 2, // AOK_EVEN
52 2, // AOK_Emit
53 3, // AOK_Input
54 3, // AOK_CallInput
55 3, // AOK_Output
56 5, // AOK_SizeDirective
57 1, // AOK_Label
58 5, // AOK_EndOfStatement
59 2, // AOK_Skip
60 2 // AOK_IntelExpr
61};
62
63// Represent the various parts which make up an intel expression,
64// used for emitting compound intel expressions
65struct IntelExpr {
66 bool NeedBracs = false;
67 int64_t Imm = 0;
71 unsigned Scale = 1;
72
73 IntelExpr() = default;
74 // [BaseReg + IndexReg * ScaleExpression + OFFSET name + ImmediateExpression]
75 IntelExpr(StringRef baseReg, StringRef indexReg, unsigned scale,
76 StringRef offsetName, int64_t imm, bool needBracs)
77 : NeedBracs(needBracs), Imm(imm), BaseReg(baseReg), IndexReg(indexReg),
78 OffsetName(offsetName), Scale(1) {
79 if (scale)
80 Scale = scale;
81 }
82 bool hasBaseReg() const { return !BaseReg.empty(); }
83 bool hasIndexReg() const { return !IndexReg.empty(); }
84 bool hasRegs() const { return hasBaseReg() || hasIndexReg(); }
85 bool hasOffset() const { return !OffsetName.empty(); }
86 // Normally we won't emit immediates unconditionally,
87 // unless we've got no other components
88 bool emitImm() const { return !(hasRegs() || hasOffset()); }
89 bool isValid() const {
90 return (Scale == 1) ||
91 (hasIndexReg() && (Scale == 2 || Scale == 4 || Scale == 8));
92 }
93};
94
95struct AsmRewrite {
98 unsigned Len;
99 bool Done;
100 int64_t Val;
104
105public:
106 AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len = 0, int64_t val = 0,
107 bool Restricted = false)
108 : Kind(kind), Loc(loc), Len(len), Done(false), Val(val) {
109 IntelExpRestricted = Restricted;
110 }
111 AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len, StringRef label)
112 : AsmRewrite(kind, loc, len) { Label = label; }
113 AsmRewrite(SMLoc loc, unsigned len, IntelExpr exp)
114 : AsmRewrite(AOK_IntelExpr, loc, len) { IntelExp = exp; }
115};
116
119
122 : AsmRewrites(rewrites) {}
123};
124
126 MatchOperand_Success, // operand matched successfully
127 MatchOperand_NoMatch, // operand did not match
128 MatchOperand_ParseFail // operand matched but had errors
130
132 Match,
133 NearMatch,
134 NoMatch,
135};
136
137// When an operand is parsed, the assembler will try to iterate through a set of
138// possible operand classes that the operand might match and call the
139// corresponding PredicateMethod to determine that.
140//
141// If there are two AsmOperands that would give a specific diagnostic if there
142// is no match, there is currently no mechanism to distinguish which operand is
143// a closer match. The DiagnosticPredicate distinguishes between 'completely
144// no match' and 'near match', so the assembler can decide whether to give a
145// specific diagnostic, or use 'InvalidOperand' and continue to find a
146// 'better matching' diagnostic.
147//
148// For example:
149// opcode opnd0, onpd1, opnd2
150//
151// where:
152// opnd2 could be an 'immediate of range [-8, 7]'
153// opnd2 could be a 'register + shift/extend'.
154//
155// If opnd2 is a valid register, but with a wrong shift/extend suffix, it makes
156// little sense to give a diagnostic that the operand should be an immediate
157// in range [-8, 7].
158//
159// This is a light-weight alternative to the 'NearMissInfo' approach
160// below which collects *all* possible diagnostics. This alternative
161// is optional and fully backward compatible with existing
162// PredicateMethods that return a 'bool' (match or no match).
165
172
173 operator bool() const { return Type == DiagnosticPredicateTy::Match; }
174 bool isMatch() const { return Type == DiagnosticPredicateTy::Match; }
177};
178
179// When matching of an assembly instruction fails, there may be multiple
180// encodings that are close to being a match. It's often ambiguous which one
181// the programmer intended to use, so we want to report an error which mentions
182// each of these "near-miss" encodings. This struct contains information about
183// one such encoding, and why it did not match the parsed instruction.
185public:
192 };
193
194 // The encoding is valid for the parsed assembly string. This is only used
195 // internally to the table-generated assembly matcher.
196 static NearMissInfo getSuccess() { return NearMissInfo(); }
197
198 // The instruction encoding is not valid because it requires some target
199 // features that are not currently enabled. MissingFeatures has a bit set for
200 // each feature that the encoding needs but which is not enabled.
201 static NearMissInfo getMissedFeature(const FeatureBitset &MissingFeatures) {
202 NearMissInfo Result;
203 Result.Kind = NearMissFeature;
204 Result.Features = MissingFeatures;
205 return Result;
206 }
207
208 // The instruction encoding is not valid because the target-specific
209 // predicate function returned an error code. FailureCode is the
210 // target-specific error code returned by the predicate.
211 static NearMissInfo getMissedPredicate(unsigned FailureCode) {
212 NearMissInfo Result;
213 Result.Kind = NearMissPredicate;
214 Result.PredicateError = FailureCode;
215 return Result;
216 }
217
218 // The instruction encoding is not valid because one (and only one) parsed
219 // operand is not of the correct type. OperandError is the error code
220 // relating to the operand class expected by the encoding. OperandClass is
221 // the type of the expected operand. Opcode is the opcode of the encoding.
222 // OperandIndex is the index into the parsed operand list.
223 static NearMissInfo getMissedOperand(unsigned OperandError,
224 unsigned OperandClass, unsigned Opcode,
225 unsigned OperandIndex) {
226 NearMissInfo Result;
227 Result.Kind = NearMissOperand;
228 Result.MissedOperand.Error = OperandError;
229 Result.MissedOperand.Class = OperandClass;
230 Result.MissedOperand.Opcode = Opcode;
231 Result.MissedOperand.Index = OperandIndex;
232 return Result;
233 }
234
235 // The instruction encoding is not valid because it expects more operands
236 // than were parsed. OperandClass is the class of the expected operand that
237 // was not provided. Opcode is the instruction encoding.
238 static NearMissInfo getTooFewOperands(unsigned OperandClass,
239 unsigned Opcode) {
240 NearMissInfo Result;
241 Result.Kind = NearMissTooFewOperands;
242 Result.TooFewOperands.Class = OperandClass;
243 Result.TooFewOperands.Opcode = Opcode;
244 return Result;
245 }
246
247 operator bool() const { return Kind != NoNearMiss; }
248
249 NearMissKind getKind() const { return Kind; }
250
251 // Feature flags required by the instruction, that the current target does
252 // not have.
253 const FeatureBitset& getFeatures() const {
254 assert(Kind == NearMissFeature);
255 return Features;
256 }
257 // Error code returned by the target predicate when validating this
258 // instruction encoding.
259 unsigned getPredicateError() const {
260 assert(Kind == NearMissPredicate);
261 return PredicateError;
262 }
263 // MatchClassKind of the operand that we expected to see.
264 unsigned getOperandClass() const {
266 return MissedOperand.Class;
267 }
268 // Opcode of the encoding we were trying to match.
269 unsigned getOpcode() const {
271 return MissedOperand.Opcode;
272 }
273 // Error code returned when validating the operand.
274 unsigned getOperandError() const {
275 assert(Kind == NearMissOperand);
276 return MissedOperand.Error;
277 }
278 // Index of the actual operand we were trying to match in the list of parsed
279 // operands.
280 unsigned getOperandIndex() const {
281 assert(Kind == NearMissOperand);
282 return MissedOperand.Index;
283 }
284
285private:
286 NearMissKind Kind;
287
288 // These two structs share a common prefix, so we can safely rely on the fact
289 // that they overlap in the union.
290 struct MissedOpInfo {
291 unsigned Class;
292 unsigned Opcode;
293 unsigned Error;
294 unsigned Index;
295 };
296
297 struct TooFewOperandsInfo {
298 unsigned Class;
299 unsigned Opcode;
300 };
301
302 union {
305 MissedOpInfo MissedOperand;
306 TooFewOperandsInfo TooFewOperands;
307 };
308
309 NearMissInfo() : Kind(NoNearMiss) {}
310};
311
312/// MCTargetAsmParser - Generic interface to target specific assembly parsers.
314public:
323 };
324
325protected: // Can only create subclasses.
327 const MCInstrInfo &MII);
328
329 /// Create a copy of STI and return a non-const reference to it.
331
332 /// AvailableFeatures - The current set of available features.
334
335 /// ParsingMSInlineAsm - Are we parsing ms-style inline assembly?
336 bool ParsingMSInlineAsm = false;
337
338 /// SemaCallback - The Sema callback implementation. Must be set when parsing
339 /// ms-style inline assembly.
341
342 /// Set of options which affects instrumentation of inline assembly.
344
345 /// Current STI.
347
349
350public:
353
355
356 const MCSubtargetInfo &getSTI() const;
357
359 return AvailableFeatures;
360 }
363 }
364
367
369
371 SemaCallback = Callback;
372 }
373
374 // Target-specific parsing of expression.
375 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
376 return getParser().parsePrimaryExpr(Res, EndLoc, nullptr);
377 }
378
379 virtual bool parseRegister(MCRegister &Reg, SMLoc &StartLoc,
380 SMLoc &EndLoc) = 0;
381
382 /// tryParseRegister - parse one register if possible
383 ///
384 /// Check whether a register specification can be parsed at the current
385 /// location, without failing the entire parse if it can't. Must not consume
386 /// tokens if the parse fails.
388 tryParseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) = 0;
389
390 /// ParseInstruction - Parse one assembly instruction.
391 ///
392 /// The parser is positioned following the instruction name. The target
393 /// specific instruction parser should parse the entire instruction and
394 /// construct the appropriate MCInst, or emit an error. On success, the entire
395 /// line should be parsed up to and including the end-of-statement token. On
396 /// failure, the parser is not required to read to the end of the line.
397 //
398 /// \param Name - The instruction name.
399 /// \param NameLoc - The source location of the name.
400 /// \param Operands [out] - The list of parsed operands, this returns
401 /// ownership of them to the caller.
402 /// \return True on failure.
404 SMLoc NameLoc, OperandVector &Operands) = 0;
407 return ParseInstruction(Info, Name, Token.getLoc(), Operands);
408 }
409
410 /// ParseDirective - Parse a target specific assembler directive
411 ///
412 /// The parser is positioned following the directive name. The target
413 /// specific directive parser should parse the entire directive doing or
414 /// recording any target specific work, or return true and do nothing if the
415 /// directive is not target specific. If the directive is specific for
416 /// the target, the entire line is parsed up to and including the
417 /// end-of-statement token and false is returned.
418 ///
419 /// \param DirectiveID - the identifier token of the directive.
420 virtual bool ParseDirective(AsmToken DirectiveID) = 0;
421
422 /// MatchAndEmitInstruction - Recognize a series of operands of a parsed
423 /// instruction as an actual MCInst and emit it to the specified MCStreamer.
424 /// This returns false on success and returns true on failure to match.
425 ///
426 /// On failure, the target parser is responsible for emitting a diagnostic
427 /// explaining the match failure.
428 virtual bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
431 bool MatchingInlineAsm) = 0;
432
433 /// Allows targets to let registers opt out of clobber lists.
434 virtual bool OmitRegisterFromClobberLists(unsigned RegNo) { return false; }
435
436 /// Allow a target to add special case operand matching for things that
437 /// tblgen doesn't/can't handle effectively. For example, literal
438 /// immediates on ARM. TableGen expects a token operand, but the parser
439 /// will recognize them as immediates.
441 unsigned Kind) {
443 }
444
445 /// Validate the instruction match against any complex target predicates
446 /// before rendering any operands to it.
447 virtual unsigned
449 return Match_Success;
450 }
451
452 /// checkTargetMatchPredicate - Validate the instruction match against
453 /// any complex target predicates not expressible via match classes.
454 virtual unsigned checkTargetMatchPredicate(MCInst &Inst) {
455 return Match_Success;
456 }
457
458 virtual void convertToMapAndConstraints(unsigned Kind,
459 const OperandVector &Operands) = 0;
460
461 /// Returns whether two operands are registers and are equal. This is used
462 /// by the tied-operands checks in the AsmMatcher. This method can be
463 /// overridden to allow e.g. a sub- or super-register as the tied operand.
464 virtual bool areEqualRegs(const MCParsedAsmOperand &Op1,
465 const MCParsedAsmOperand &Op2) const {
466 return Op1.isReg() && Op2.isReg() && Op1.getReg() == Op2.getReg();
467 }
468
469 // Return whether this parser uses assignment statements with equals tokens
470 virtual bool equalIsAsmAssignment() { return true; };
471 // Return whether this start of statement identifier is a label
472 virtual bool isLabel(AsmToken &Token) { return true; };
473 // Return whether this parser accept star as start of statement
474 virtual bool starIsStartOfStatement() { return false; };
475
476 virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
478 MCContext &Ctx) {
479 return nullptr;
480 }
481
482 // For actions that have to be performed before a label is emitted
483 virtual void doBeforeLabelEmit(MCSymbol *Symbol, SMLoc IDLoc) {}
484
485 virtual void onLabelParsed(MCSymbol *Symbol) {}
486
487 /// Ensure that all previously parsed instructions have been emitted to the
488 /// output streamer, if the target does not emit them immediately.
490
491 virtual const MCExpr *createTargetUnaryExpr(const MCExpr *E,
492 AsmToken::TokenKind OperatorToken,
493 MCContext &Ctx) {
494 return nullptr;
495 }
496
497 // For any initialization at the beginning of parsing.
498 virtual void onBeginOfFile() {}
499
500 // For any checks or cleanups at the end of parsing.
501 virtual void onEndOfFile() {}
502};
503
504} // end namespace llvm
505
506#endif // LLVM_MC_MCPARSER_MCTARGETASMPARSER_H
static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
std::string Name
mir Rename Register Operands
unsigned Reg
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Target independent representation for an assembler token.
Definition: MCAsmMacro.h:21
SMLoc getLoc() const
Definition: MCAsmLexer.cpp:26
Base class for user error types.
Definition: Error.h:348
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
Container class for subtarget features.
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
Generic Sema callback for assembly parser.
Definition: MCAsmParser.h:108
virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc, AsmTypeInfo *TypeInfo)=0
Parse a primary expression.
Context object for machine code objects.
Definition: MCContext.h:76
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
MCParsedAsmOperand - This abstract class represents a source-level assembly instruction operand.
virtual unsigned getReg() const =0
virtual bool isReg() const =0
isReg - Is this a register operand?
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:24
Streaming machine code generation interface.
Definition: MCStreamer.h:212
Generic base class for all target subtargets.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
MCTargetAsmParser - Generic interface to target specific assembly parsers.
virtual bool ParseDirective(AsmToken DirectiveID)=0
ParseDirective - Parse a target specific assembler directive.
virtual void onLabelParsed(MCSymbol *Symbol)
const FeatureBitset & getAvailableFeatures() const
virtual void convertToMapAndConstraints(unsigned Kind, const OperandVector &Operands)=0
MCTargetOptions MCOptions
Set of options which affects instrumentation of inline assembly.
MCSubtargetInfo & copySTI()
Create a copy of STI and return a non-const reference to it.
bool ParsingMSInlineAsm
ParsingMSInlineAsm - Are we parsing ms-style inline assembly?
virtual bool equalIsAsmAssignment()
virtual bool parseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc)=0
MCAsmParserSemaCallback * SemaCallback
SemaCallback - The Sema callback implementation.
void setParsingMSInlineAsm(bool Value)
virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc)
virtual unsigned checkEarlyTargetMatchPredicate(MCInst &Inst, const OperandVector &Operands)
Validate the instruction match against any complex target predicates before rendering any operands to...
virtual bool starIsStartOfStatement()
virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name, AsmToken Token, OperandVector &Operands)
const MCInstrInfo & MII
virtual void flushPendingInstructions(MCStreamer &Out)
Ensure that all previously parsed instructions have been emitted to the output streamer,...
virtual bool isLabel(AsmToken &Token)
void setAvailableFeatures(const FeatureBitset &Value)
const MCSubtargetInfo & getSTI() const
virtual void doBeforeLabelEmit(MCSymbol *Symbol, SMLoc IDLoc)
FeatureBitset AvailableFeatures
AvailableFeatures - The current set of available features.
virtual const MCExpr * applyModifierToExpr(const MCExpr *E, MCSymbolRefExpr::VariantKind, MCContext &Ctx)
void setSemaCallback(MCAsmParserSemaCallback *Callback)
virtual const MCExpr * createTargetUnaryExpr(const MCExpr *E, AsmToken::TokenKind OperatorToken, MCContext &Ctx)
MCTargetAsmParser(const MCTargetAsmParser &)=delete
virtual bool OmitRegisterFromClobberLists(unsigned RegNo)
Allows targets to let registers opt out of clobber lists.
virtual unsigned validateTargetOperandClass(MCParsedAsmOperand &Op, unsigned Kind)
Allow a target to add special case operand matching for things that tblgen doesn't/can't handle effec...
~MCTargetAsmParser() override
MCTargetAsmParser & operator=(const MCTargetAsmParser &)=delete
virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc, OperandVector &Operands)=0
ParseInstruction - Parse one assembly instruction.
virtual unsigned checkTargetMatchPredicate(MCInst &Inst)
checkTargetMatchPredicate - Validate the instruction match against any complex target predicates not ...
MCTargetOptions getTargetOptions() const
virtual bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, OperandVector &Operands, MCStreamer &Out, uint64_t &ErrorInfo, bool MatchingInlineAsm)=0
MatchAndEmitInstruction - Recognize a series of operands of a parsed instruction as an actual MCInst ...
virtual OperandMatchResultTy tryParseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc)=0
tryParseRegister - parse one register if possible
virtual bool areEqualRegs(const MCParsedAsmOperand &Op1, const MCParsedAsmOperand &Op2) const
Returns whether two operands are registers and are equal.
const MCSubtargetInfo * STI
Current STI.
static NearMissInfo getMissedPredicate(unsigned FailureCode)
unsigned getOperandClass() const
static NearMissInfo getTooFewOperands(unsigned OperandClass, unsigned Opcode)
static NearMissInfo getMissedOperand(unsigned OperandError, unsigned OperandClass, unsigned Opcode, unsigned OperandIndex)
unsigned getOperandIndex() const
const FeatureBitset & getFeatures() const
NearMissKind getKind() const
FeatureBitset Features
static NearMissInfo getMissedFeature(const FeatureBitset &MissingFeatures)
TooFewOperandsInfo TooFewOperands
unsigned getOperandError() const
static NearMissInfo getSuccess()
unsigned getOpcode() const
unsigned getPredicateError() const
MissedOpInfo MissedOperand
Represents a location in source code.
Definition: SMLoc.h:23
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ MatchOperand_NoMatch
@ MatchOperand_ParseFail
@ MatchOperand_Success
@ AOK_EndOfStatement
@ AOK_SizeDirective
const char AsmRewritePrecedence[]
AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len=0, int64_t val=0, bool Restricted=false)
AsmRewriteKind Kind
AsmRewrite(SMLoc loc, unsigned len, IntelExpr exp)
AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len, StringRef label)
DiagnosticPredicate(const DiagnosticPredicate &)=default
DiagnosticPredicate & operator=(const DiagnosticPredicate &)=default
DiagnosticPredicateTy Type
DiagnosticPredicate(DiagnosticPredicateTy T)
IntelExpr()=default
bool hasIndexReg() const
bool hasRegs() const
bool hasOffset() const
IntelExpr(StringRef baseReg, StringRef indexReg, unsigned scale, StringRef offsetName, int64_t imm, bool needBracs)
bool hasBaseReg() const
bool emitImm() const
bool isValid() const
ParseInstructionInfo(SmallVectorImpl< AsmRewrite > *rewrites)
SmallVectorImpl< AsmRewrite > * AsmRewrites