Skip to content

Commit

Permalink
[TableGen][NFC] Group tokens with same attribute togather
Browse files Browse the repository at this point in the history
So that we can simplify some code and simplify the implmentation
when we want to add new features (like adding new bang operators).

Reviewed By: fpetrogalli

Differential Revision: https://reviews.llvm.org/D156502
  • Loading branch information
wangpc-pp committed Aug 1, 2023
1 parent 11fbdd2 commit ebf394f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 95 deletions.
88 changes: 55 additions & 33 deletions llvm/lib/TableGen/TGLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,59 @@ enum TokKind {
paste, // #
dotdotdot, // ...

// Boolean literals.
TrueVal,
FalseVal,

// Integer value.
IntVal,

// Binary constant. Note that these are sized according to the number of
// bits given.
BinaryIntVal,

// Preprocessing tokens for internal usage by the lexer.
// They are never returned as a result of Lex().
Ifdef,
Ifndef,
Else,
Endif,
Define,

// Reserved keywords. ('ElseKW' is named to distinguish it from the
// existing 'Else' that means the preprocessor #else.)
Assert,
Bit,
Bits,
Class,
Code,
Dag,
Def,
Defm,
Defset,
Defvar,
ElseKW,
FalseKW,
Field,
Foreach,
If,
In,
Include,
Int,
Let,
List,
MultiClass,
String,
Then,
TrueKW,

// Object start tokens.
OBJECT_START_FIRST,
Assert = OBJECT_START_FIRST,
Class,
Def,
Defm,
Defset,
Defvar,
Foreach,
If,
Let,
MultiClass,
OBJECT_START_LAST = MultiClass,

// Bang operators.
XConcat,
BANG_OPERATOR_FIRST,
XConcat = BANG_OPERATOR_FIRST,
XADD,
XSUB,
XMUL,
Expand Down Expand Up @@ -131,33 +155,32 @@ enum TokKind {
XGetDagName,
XSetDagArg,
XSetDagName,

// Boolean literals.
TrueVal,
FalseVal,

// Integer value.
IntVal,

// Binary constant. Note that these are sized according to the number of
// bits given.
BinaryIntVal,
BANG_OPERATOR_LAST = XSetDagName,

// String valued tokens.
Id,
STRING_VALUE_FIRST,
Id = STRING_VALUE_FIRST,
StrVal,
VarName,
CodeFragment,

// Preprocessing tokens for internal usage by the lexer.
// They are never returned as a result of Lex().
Ifdef,
Ifndef,
Else,
Endif,
Define
STRING_VALUE_LAST = CodeFragment,
};

/// isBangOperator - Return true if this is a bang operator.
static inline bool isBangOperator(tgtok::TokKind Kind) {
return tgtok::BANG_OPERATOR_FIRST <= Kind && Kind <= BANG_OPERATOR_LAST;
}

/// isObjectStart - Return true if this is a valid first token for a statement.
static inline bool isObjectStart(tgtok::TokKind Kind) {
return tgtok::OBJECT_START_FIRST <= Kind && Kind <= OBJECT_START_LAST;
}

/// isStringValue - Return true if this is a string value.
static inline bool isStringValue(tgtok::TokKind Kind) {
return tgtok::STRING_VALUE_FIRST <= Kind && Kind <= STRING_VALUE_LAST;
}
} // namespace tgtok

/// TGLexer - TableGen Lexer class.
class TGLexer {
Expand Down Expand Up @@ -197,8 +220,7 @@ class TGLexer {
tgtok::TokKind getCode() const { return CurCode; }

const std::string &getCurStrVal() const {
assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal ||
CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
assert(tgtok::isStringValue(CurCode) &&
"This token doesn't have a string value");
return CurStrVal;
}
Expand Down
70 changes: 8 additions & 62 deletions llvm/lib/TableGen/TGParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,14 +629,6 @@ bool TGParser::resolveArgumentsOfMultiClass(SubstStack &Substs, MultiClass *MC,
// Parser Code
//===----------------------------------------------------------------------===//

/// isObjectStart - Return true if this is a valid first token for a statement.
static bool isObjectStart(tgtok::TokKind K) {
return K == tgtok::Assert || K == tgtok::Class || K == tgtok::Def ||
K == tgtok::Defm || K == tgtok::Defset || K == tgtok::Defvar ||
K == tgtok::Foreach || K == tgtok::If || K == tgtok::Let ||
K == tgtok::MultiClass;
}

bool TGParser::consume(tgtok::TokKind K) {
if (Lex.getCode() == K) {
Lex.Lex();
Expand Down Expand Up @@ -2547,7 +2539,13 @@ Init *TGParser::ParseOperationCond(Record *CurRec, RecTy *ItemType) {
Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
IDParseMode Mode) {
Init *R = nullptr;
switch (Lex.getCode()) {
tgtok::TokKind Code = Lex.getCode();

// Parse bang operators.
if (tgtok::isBangOperator(Code))
return ParseOperation(CurRec, ItemType);

switch (Code) {
default: TokError("Unknown or reserved token when parsing a value"); break;

case tgtok::TrueVal:
Expand Down Expand Up @@ -2802,58 +2800,6 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,

return DagInit::get(Operator, OperatorName, DagArgs);
}

case tgtok::XHead:
case tgtok::XTail:
case tgtok::XSize:
case tgtok::XEmpty:
case tgtok::XCast:
case tgtok::XToLower:
case tgtok::XToUpper:
case tgtok::XGetDagOp: // Value ::= !unop '(' Value ')'
case tgtok::XExists:
case tgtok::XIsA:
case tgtok::XConcat:
case tgtok::XDag:
case tgtok::XADD:
case tgtok::XSUB:
case tgtok::XMUL:
case tgtok::XDIV:
case tgtok::XNOT:
case tgtok::XLOG2:
case tgtok::XAND:
case tgtok::XOR:
case tgtok::XXOR:
case tgtok::XSRA:
case tgtok::XSRL:
case tgtok::XSHL:
case tgtok::XEq:
case tgtok::XNe:
case tgtok::XLe:
case tgtok::XLt:
case tgtok::XGe:
case tgtok::XGt:
case tgtok::XListConcat:
case tgtok::XListSplat:
case tgtok::XListRemove:
case tgtok::XRange:
case tgtok::XStrConcat:
case tgtok::XInterleave:
case tgtok::XGetDagArg:
case tgtok::XGetDagName:
case tgtok::XSetDagOp: // Value ::= !binop '(' Value ',' Value ')'
case tgtok::XSetDagArg:
case tgtok::XSetDagName:
case tgtok::XIf:
case tgtok::XCond:
case tgtok::XFoldl:
case tgtok::XForEach:
case tgtok::XFilter:
case tgtok::XSubst:
case tgtok::XSubstr:
case tgtok::XFind: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
return ParseOperation(CurRec, ItemType);
}
}

return R;
Expand Down Expand Up @@ -4275,7 +4221,7 @@ bool TGParser::ParseObject(MultiClass *MC) {
/// ParseObjectList
/// ObjectList :== Object*
bool TGParser::ParseObjectList(MultiClass *MC) {
while (isObjectStart(Lex.getCode())) {
while (tgtok::isObjectStart(Lex.getCode())) {
if (ParseObject(MC))
return true;
}
Expand Down

0 comments on commit ebf394f

Please sign in to comment.