diff options
author | David Greene <greened@obbligato.org> | 2009-06-08 17:00:34 +0000 |
---|---|---|
committer | David Greene <greened@obbligato.org> | 2009-06-08 17:00:34 +0000 |
commit | 938c8ab0a0f4a52d7d6ddc975abe9de08fee06d5 (patch) | |
tree | 790c1f084b25650896b3b08b8811e9bb0d52d692 /utils/TableGen | |
parent | 06bfa33eefd6c7b56fc354ab640a9175e82bf890 (diff) | |
download | external_llvm-938c8ab0a0f4a52d7d6ddc975abe9de08fee06d5.zip external_llvm-938c8ab0a0f4a52d7d6ddc975abe9de08fee06d5.tar.gz external_llvm-938c8ab0a0f4a52d7d6ddc975abe9de08fee06d5.tar.bz2 |
Add a !regmatch operator to do pattern matching in TableGen.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73074 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'utils/TableGen')
-rw-r--r-- | utils/TableGen/Record.cpp | 33 | ||||
-rw-r--r-- | utils/TableGen/Record.h | 2 | ||||
-rw-r--r-- | utils/TableGen/TGLexer.cpp | 1 | ||||
-rw-r--r-- | utils/TableGen/TGLexer.h | 2 | ||||
-rw-r--r-- | utils/TableGen/TGParser.cpp | 7 |
5 files changed, 43 insertions, 2 deletions
diff --git a/utils/TableGen/Record.cpp b/utils/TableGen/Record.cpp index 45804b9..47cab7b 100644 --- a/utils/TableGen/Record.cpp +++ b/utils/TableGen/Record.cpp @@ -16,6 +16,8 @@ #include "llvm/Support/Streams.h" #include "llvm/ADT/StringExtras.h" #include <ios> +#include <sys/types.h> +#include <regex.h> using namespace llvm; @@ -671,6 +673,36 @@ Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) { } break; } + case REGMATCH: { + StringInit *LHSs = dynamic_cast<StringInit*>(LHS); + StringInit *RHSs = dynamic_cast<StringInit*>(RHS); + if (LHSs && RHSs) { + regex_t compiled; + int err = regcomp (&compiled, LHSs->getValue().c_str(), REG_EXTENDED); + if (err != 0) { + size_t length = regerror (err, &compiled, NULL, 0); + char *buffer = new char[length]; + (void) regerror (err, &compiled, buffer, length); + std::string errmsg = buffer; + delete[] buffer; + regfree(&compiled); + throw errmsg; + } + int result = regexec(&compiled, RHSs->getValue().c_str(), 0, NULL, 0); + if (result == REG_ESPACE) { + size_t length = regerror (err, &compiled, NULL, 0); + char *buffer = new char[length]; + (void) regerror (err, &compiled, buffer, length); + std::string errmsg = buffer; + delete[] buffer; + regfree(&compiled); + throw errmsg; + } + regfree(&compiled); + return new IntInit(result == 0); + } + break; + } case SHL: case SRA: case SRL: { @@ -710,6 +742,7 @@ std::string BinOpInit::getAsString() const { case SRA: Result = "!sra"; break; case SRL: Result = "!srl"; break; case STRCONCAT: Result = "!strconcat"; break; + case REGMATCH: Result = "!regmatch"; break; case NAMECONCAT: Result = "!nameconcat<" + getType()->getAsString() + ">"; break; } diff --git a/utils/TableGen/Record.h b/utils/TableGen/Record.h index 4284cab..3a15ca5 100644 --- a/utils/TableGen/Record.h +++ b/utils/TableGen/Record.h @@ -802,7 +802,7 @@ public: /// class BinOpInit : public OpInit { public: - enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT }; + enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT, REGMATCH }; private: BinaryOp Opc; Init *LHS, *RHS; diff --git a/utils/TableGen/TGLexer.cpp b/utils/TableGen/TGLexer.cpp index 758d499..378bffb 100644 --- a/utils/TableGen/TGLexer.cpp +++ b/utils/TableGen/TGLexer.cpp @@ -447,6 +447,7 @@ tgtok::TokKind TGLexer::LexExclaim() { if (Len == 3 && !memcmp(Start, "shl", 3)) return tgtok::XSHL; if (Len == 9 && !memcmp(Start, "strconcat", 9)) return tgtok::XStrConcat; if (Len == 10 && !memcmp(Start, "nameconcat", 10)) return tgtok::XNameConcat; + if (Len == 8 && !memcmp(Start, "regmatch", 8)) return tgtok::XRegMatch; if (Len == 5 && !memcmp(Start, "subst", 5)) return tgtok::XSubst; if (Len == 7 && !memcmp(Start, "foreach", 7)) return tgtok::XForEach; if (Len == 4 && !memcmp(Start, "cast", 4)) return tgtok::XCast; diff --git a/utils/TableGen/TGLexer.h b/utils/TableGen/TGLexer.h index ac3b984..06f6535 100644 --- a/utils/TableGen/TGLexer.h +++ b/utils/TableGen/TGLexer.h @@ -46,7 +46,7 @@ namespace tgtok { // !keywords. XConcat, XSRA, XSRL, XSHL, XStrConcat, XNameConcat, XCast, XSubst, - XForEach, XCar, XCdr, XNull, XIf, + XForEach, XCar, XCdr, XNull, XIf, XRegMatch, // Integer value. IntVal, diff --git a/utils/TableGen/TGParser.cpp b/utils/TableGen/TGParser.cpp index fc6f29f..b4d448b 100644 --- a/utils/TableGen/TGParser.cpp +++ b/utils/TableGen/TGParser.cpp @@ -791,6 +791,7 @@ Init *TGParser::ParseOperation(Record *CurRec) { case tgtok::XSRL: case tgtok::XSHL: case tgtok::XStrConcat: + case tgtok::XRegMatch: case tgtok::XNameConcat: { // Value ::= !binop '(' Value ',' Value ')' BinOpInit::BinaryOp Code; RecTy *Type = 0; @@ -823,6 +824,11 @@ Init *TGParser::ParseOperation(Record *CurRec) { Code = BinOpInit::STRCONCAT; Type = new StringRecTy(); break; + case tgtok::XRegMatch: + Lex.Lex(); // eat the operation + Code = BinOpInit::REGMATCH; + Type = new IntRecTy(); + break; case tgtok::XNameConcat: Lex.Lex(); // eat the operation Code = BinOpInit::NAMECONCAT; @@ -1174,6 +1180,7 @@ Init *TGParser::ParseSimpleValue(Record *CurRec) { case tgtok::XSRL: case tgtok::XSHL: case tgtok::XStrConcat: + case tgtok::XRegMatch: case tgtok::XNameConcat: // Value ::= !binop '(' Value ',' Value ')' case tgtok::XIf: case tgtok::XForEach: |