llvm.org GIT mirror llvm / ffc0ab6
Revert 73074 and 73099 because Windows doesn't have POSIX regular expressions. We will add an OpenBSD implementation and re-apply ASAP. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73138 91177308-0d34-0410-b5e6-96231b3b80d8 David Greene 10 years ago
9 changed file(s) with 4 addition(s) and 153 deletion(s). Raw diff Collapse all Expand all
416416
!subst(a, b, c)
417417
If 'a' and 'b' are of string type or are symbol references, substitute
418418 'b' for 'a' in 'c.' This operation is analogous to $(subst) in GNU make.
419
!patsubst(a, b, c)
420
patch regular expression 'a' against string 'c' and substitute string 'b' on
421 a match. 'b' may contain placeholders of the form $<digit>, where
422 <digit> is a number 1-9.
423
!regmatch(a, b)
424
An integer {0,1} indicating whether string 'b' matched regular expression
425 'a.'
426419
!foreach(a, b, c)
427420
For each member 'b' of dag or list 'a' apply operator 'c.' 'b' is a
428421 dummy variable that should be declared as a member variable of an instantiated
None // RUN: tblgen %s | grep {Match1 = "v4f32"} | count 1
1 // RUN: tblgen %s | grep {Match2 = "v2f64"} | count 1
2 // RUN: tblgen %s | grep {Match3 = "v4f32 add"} | count 1
3 // RUN: tblgen %s | grep {Match4 = "v2f64 add"} | count 1
4
5 class Foo {
6 string Value = v;
7 string Match1 = !patsubst(".*ps$", "v4f32", v);
8 string Match2 = !patsubst(".*pd$", "v2f64", v);
9 string Match3 = !patsubst("(.*)ps$", "v4f32 $1", v);
10 string Match4 = !patsubst("(.*)pd$", "v2f64 $1", v);
11 }
12
13 def Bar : Foo<"addps">;
14 def Baz : Foo<"addpd">;
None // RUN: tblgen %s | grep {Match1 = 1} | count 1
1 // RUN: tblgen %s | grep {Match2 = 1} | count 1
2
3 class Foo {
4 string Value = v;
5 int Match1 = !regmatch(".*ps$", v);
6 int Match2 = !regmatch(".*pd$", v);
7 }
8
9 def Bar : Foo<"addps">;
10 def Baz : Foo<"addpd">;
20242024 }
20252025 }
20262026 else {
2027 ListTy = TArg->getType();
2027 ListTy - TArg->getType();
20282028 }
20292029 }
20302030 ListInit *LI = new ListInit(Values, new ListRecTy(ListTy));
1515 #include "llvm/Support/Streams.h"
1616 #include "llvm/ADT/StringExtras.h"
1717 #include
18 #include
19 #include
20 #include
2118
2219 using namespace llvm;
2320
749746 }
750747 break;
751748 }
752 case REGMATCH: {
753 StringInit *LHSs = dynamic_cast(LHS);
754 StringInit *RHSs = dynamic_cast(RHS);
755 if (LHSs && RHSs) {
756 regex_t compiled;
757 int err = regcomp (&compiled, LHSs->getValue().c_str(), REG_EXTENDED);
758 if (err != 0) {
759 size_t length = regerror (err, &compiled, NULL, 0);
760 char *buffer = new char[length];
761 (void) regerror (err, &compiled, buffer, length);
762 std::string errmsg = buffer;
763 delete[] buffer;
764 regfree(&compiled);
765 throw errmsg;
766 }
767 int result = regexec(&compiled, RHSs->getValue().c_str(), 0, NULL, 0);
768 if (result == REG_ESPACE) {
769 size_t length = regerror (err, &compiled, NULL, 0);
770 char *buffer = new char[length];
771 (void) regerror (err, &compiled, buffer, length);
772 std::string errmsg = buffer;
773 delete[] buffer;
774 regfree(&compiled);
775 throw errmsg;
776 }
777 regfree(&compiled);
778 return new IntInit(result == 0);
779 }
780 break;
781 }
782749 case SHL:
783750 case SRA:
784751 case SRL: {
818785 case SRA: Result = "!sra"; break;
819786 case SRL: Result = "!srl"; break;
820787 case STRCONCAT: Result = "!strconcat"; break;
821 case REGMATCH: Result = "!regmatch"; break;
822788 case NAMECONCAT:
823789 Result = "!nameconcat<" + getType()->getAsString() + ">"; break;
824790 }
10341000 }
10351001 break;
10361002 }
1037
1038 case PATSUBST: {
1039 StringInit *LHSs = dynamic_cast(LHS);
1040 StringInit *MHSs = dynamic_cast(MHS);
1041 StringInit *RHSs = dynamic_cast(RHS);
1042
1043 if (LHSs && MHSs && RHSs) {
1044 regex_t compiled;
1045 int err = regcomp (&compiled, LHSs->getValue().c_str(), REG_EXTENDED);
1046 if (err != 0) {
1047 size_t length = regerror (err, &compiled, NULL, 0);
1048 char *buffer = new char[length];
1049 (void) regerror (err, &compiled, buffer, length);
1050 std::string errmsg = buffer;
1051 delete[] buffer;
1052 regfree(&compiled);
1053 throw errmsg;
1054 }
1055 regmatch_t matches[10];
1056 int result = regexec(&compiled, RHSs->getValue().c_str(), 10, matches, 0);
1057 if (result == REG_ESPACE) {
1058 size_t length = regerror (err, &compiled, NULL, 0);
1059 char *buffer = new char[length];
1060 (void) regerror (err, &compiled, buffer, length);
1061 std::string errmsg = buffer;
1062 delete[] buffer;
1063 regfree(&compiled);
1064 throw errmsg;
1065 }
1066 regfree(&compiled);
1067 if (result == 0) {
1068 // Parse the substitution string looking for $1, $2, etc. and
1069 // substitute strings. If there are no $1, etc. just replace
1070 // the whole string.
1071 std::string replacement = MHSs->getValue();
1072 size_t pos = replacement.find("$");
1073 while (pos != std::string::npos && pos+1 < replacement.size()) {
1074 if (std::isdigit(replacement[pos+1])) {
1075 std::string sidx(&replacement[pos+1], 1);
1076 std::istringstream str(sidx);
1077 int idx;
1078 if (str >> idx) {
1079 replacement.replace(pos, 2, RHSs->getValue(), matches[idx].rm_so,
1080 matches[idx].rm_eo - matches[idx].rm_so);
1081 }
1082 else {
1083 throw "unexpected failure in patsubst index calculation";
1084 }
1085 }
1086 else if (replacement[pos+1] == '$') {
1087 replacement.replace(pos, 2, "$");
1088 }
1089 pos = replacement.find("$", pos+1);
1090 }
1091 return new StringInit(replacement);
1092 }
1093 else {
1094 // No match, just pass the string through
1095 return RHSs;
1096 }
1097 }
1098 break;
1099 }
11001003 }
11011004
11021005 return this;
11321035 std::string Result;
11331036 switch (Opc) {
11341037 case SUBST: Result = "!subst"; break;
1135 case PATSUBST: Result = "!patsubst"; break;
11361038 case FOREACH: Result = "!foreach"; break;
11371039 case IF: Result = "!if"; break;
11381040 }
840840 ///
841841 class BinOpInit : public OpInit {
842842 public:
843 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT, REGMATCH };
843 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT };
844844 private:
845845 BinaryOp Opc;
846846 Init *LHS, *RHS;
884884 ///
885885 class TernOpInit : public OpInit {
886886 public:
887 enum TernaryOp { SUBST, FOREACH, IF, PATSUBST };
887 enum TernaryOp { SUBST, FOREACH, IF };
888888 private:
889889 TernaryOp Opc;
890890 Init *LHS, *MHS, *RHS;
446446 if (Len == 3 && !memcmp(Start, "shl", 3)) return tgtok::XSHL;
447447 if (Len == 9 && !memcmp(Start, "strconcat", 9)) return tgtok::XStrConcat;
448448 if (Len == 10 && !memcmp(Start, "nameconcat", 10)) return tgtok::XNameConcat;
449 if (Len == 8 && !memcmp(Start, "regmatch", 8)) return tgtok::XRegMatch;
450449 if (Len == 5 && !memcmp(Start, "subst", 5)) return tgtok::XSubst;
451 if (Len == 8 && !memcmp(Start, "patsubst", 8)) return tgtok::XPatSubst;
452450 if (Len == 7 && !memcmp(Start, "foreach", 7)) return tgtok::XForEach;
453451 if (Len == 4 && !memcmp(Start, "cast", 4)) return tgtok::XCast;
454452 if (Len == 3 && !memcmp(Start, "car", 3)) return tgtok::XCar;
4545
4646 // !keywords.
4747 XConcat, XSRA, XSRL, XSHL, XStrConcat, XNameConcat, XCast, XSubst,
48 XForEach, XCar, XCdr, XNull, XIf, XRegMatch, XPatSubst,
48 XForEach, XCar, XCdr, XNull, XIf,
4949
5050 // Integer value.
5151 IntVal,
798798 case tgtok::XSRL:
799799 case tgtok::XSHL:
800800 case tgtok::XStrConcat:
801 case tgtok::XRegMatch:
802801 case tgtok::XNameConcat: { // Value ::= !binop '(' Value ',' Value ')'
803802 BinOpInit::BinaryOp Code;
804803 RecTy *Type = 0;
831830 Code = BinOpInit::STRCONCAT;
832831 Type = new StringRecTy();
833832 break;
834 case tgtok::XRegMatch:
835 Lex.Lex(); // eat the operation
836 Code = BinOpInit::REGMATCH;
837 Type = new IntRecTy();
838 break;
839833 case tgtok::XNameConcat:
840834 Lex.Lex(); // eat the operation
841835 Code = BinOpInit::NAMECONCAT;
877871
878872 case tgtok::XIf:
879873 case tgtok::XForEach:
880 case tgtok::XPatSubst:
881874 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
882875 TernOpInit::TernaryOp Code;
883876 RecTy *Type = 0;
896889 case tgtok::XSubst:
897890 Code = TernOpInit::SUBST;
898891 break;
899 case tgtok::XPatSubst:
900 Code = TernOpInit::PATSUBST;
901 break;
902892 }
903893 if (Lex.getCode() != tgtok::l_paren) {
904894 TokError("expected '(' after ternary operator");
972962 Type = RHSt->getType();
973963 break;
974964 }
975 case tgtok::XPatSubst: {
976 Type = new StringRecTy;
977 break;
978 }
979965 }
980966 return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec, CurMultiClass);
981967 }
12801266 case tgtok::XSRL:
12811267 case tgtok::XSHL:
12821268 case tgtok::XStrConcat:
1283 case tgtok::XRegMatch:
12841269 case tgtok::XNameConcat: // Value ::= !binop '(' Value ',' Value ')'
12851270 case tgtok::XIf:
12861271 case tgtok::XForEach:
1287 case tgtok::XPatSubst:
12881272 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
12891273 return ParseOperation(CurRec);
12901274 break;