llvm.org GIT mirror llvm / e1b4691
Make IntInits and ListInits typed. This helps deduce types of !if and other operators. For the rare cases where a list type cannot be deduced, provide a []<type> syntax, where <type> is the list element type. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73078 91177308-0d34-0410-b5e6-96231b3b80d8 David Greene 10 years ago
7 changed file(s) with 385 addition(s) and 48 deletion(s). Raw diff Collapse all Expand all
227227 SDTypeProfile TypeProfile = typeprof;
228228 }
229229
230 // Special TableGen-recognized dag nodes
230231 def set;
231232 def implicit;
232233 def parallel;
0 // RUN: tblgen %s | grep {[(set VR128:$dst, (int_x86_sse2_add_pd VR128:$src1, VR128:$src2))]} | count 1
1 // RUN: tblgen %s | grep {[(set VR128:$dst, (int_x86_sse2_add_ps VR128:$src1, VR128:$src2))]} | count 2
2
3 class ValueType {
4 int Size = size;
5 int Value = value;
6 }
7
8 def f32 : ValueType<32, 1>; // 2 x i64 vector value
9
10 class Intrinsic {
11 string Name = name;
12 }
13
14 class Inst opcode, dag oopnds, dag iopnds, string asmstr,
15 list pattern> {
16 bits<8> Opcode = opcode;
17 dag OutOperands = oopnds;
18 dag InOperands = iopnds;
19 string AssemblyString = asmstr;
20 list Pattern = pattern;
21 }
22
23 def ops;
24 def outs;
25 def ins;
26
27 def set;
28
29 // Define registers
30 class Register {
31 string Name = n;
32 }
33
34 class RegisterClass regTypes, list regList> {
35 list RegTypes = regTypes;
36 list MemberList = regList;
37 }
38
39 def XMM0: Register<"xmm0">;
40 def XMM1: Register<"xmm1">;
41 def XMM2: Register<"xmm2">;
42 def XMM3: Register<"xmm3">;
43 def XMM4: Register<"xmm4">;
44 def XMM5: Register<"xmm5">;
45 def XMM6: Register<"xmm6">;
46 def XMM7: Register<"xmm7">;
47 def XMM8: Register<"xmm8">;
48 def XMM9: Register<"xmm9">;
49 def XMM10: Register<"xmm10">;
50 def XMM11: Register<"xmm11">;
51 def XMM12: Register<"xmm12">;
52 def XMM13: Register<"xmm13">;
53 def XMM14: Register<"xmm14">;
54 def XMM15: Register<"xmm15">;
55
56 def FR32 : RegisterClass<[f32],
57 [XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
58 XMM8, XMM9, XMM10, XMM11,
59 XMM12, XMM13, XMM14, XMM15]>;
60
61 class SDNode {}
62 def not : SDNode;
63
64 multiclass scalar opcode, string asmstr = "", list> patterns = []> {
65 def SSrr : Inst
66 !strconcat(asmstr, "\t$dst, $src"),
67 !if(!null(patterns),[],patterns[0])>;
68 def SSrm : Inst
69 !strconcat(asmstr, "\t$dst, $src"),
70 !if(!null(patterns),[],!if(!null(!cdr(patterns)),patterns[0],patterns[1]))>;
71 }
72
73 multiclass vscalar opcode, string asmstr = "", list> patterns = []> {
74 def V#NAME#SSrr : Inst
75 !strconcat(asmstr, "\t$dst, $src"),
76 !if(!null(patterns),[],patterns[0])>;
77 def V#NAME#SSrm : Inst
78 !strconcat(asmstr, "\t$dst, $src"),
79 !if(!null(patterns),[],!if(!null(!cdr(patterns)),patterns[0],patterns[1]))>;
80 }
81
82 multiclass myscalar opcode, string asmstr = "", list> patterns = []> :
83 scalar,
84 vscalar;
85
86 defm NOT : myscalar<0x10, "not", [[], [(set FR32:$dst, (f32 (not FR32:$src)))]]>;
20062006 Pattern = new TreePattern(Patterns[i], Tree, true, *this);
20072007 else {
20082008 std::vector Values;
2009 for (unsigned j = 0, ee = Tree->getNumArgs(); j != ee; ++j)
2009 RecTy *ListTy = 0;
2010 for (unsigned j = 0, ee = Tree->getNumArgs(); j != ee; ++j) {
20102011 Values.push_back(Tree->getArg(j));
2011 ListInit *LI = new ListInit(Values);
2012 TypedInit *TArg = dynamic_cast(Tree->getArg(j));
2013 if (TArg == 0) {
2014 cerr << "In dag: " << Tree->getAsString();
2015 cerr << " -- Untyped argument in pattern\n";
2016 assert(0 && "Untyped argument in pattern");
2017 }
2018 if (ListTy != 0) {
2019 ListTy = resolveTypes(ListTy, TArg->getType());
2020 if (ListTy == 0) {
2021 cerr << "In dag: " << Tree->getAsString();
2022 cerr << " -- Incompatible types in pattern arguments\n";
2023 assert(0 && "Incompatible types in pattern arguments");
2024 }
2025 }
2026 else {
2027 ListTy - TArg->getType();
2028 }
2029 }
2030 ListInit *LI = new ListInit(Values, new ListRecTy(ListTy));
20122031 Pattern = new TreePattern(Patterns[i], LI, true, *this);
20132032 }
20142033
190190 else
191191 return 0;
192192
193 return new ListInit(Elements);
193 ListRecTy *LType = dynamic_cast(LI->getType());
194 if (LType == 0) {
195 return 0;
196 }
197
198 return new ListInit(Elements, new ListRecTy(Ty));
194199 }
195200
196201 Init *ListRecTy::convertValue(TypedInit *TI) {
271276 }
272277
273278
279 /// resolveTypes - Find a common type that T1 and T2 convert to.
280 /// Return 0 if no such type exists.
281 ///
282 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
283 if (!T1->typeIsConvertibleTo(T2)) {
284 if (!T2->typeIsConvertibleTo(T1)) {
285 // If one is a Record type, check superclasses
286 RecordRecTy *RecTy1 = dynamic_cast(T1);
287 if (RecTy1) {
288 // See if T2 inherits from a type T1 also inherits from
289 const std::vector &T1SuperClasses = RecTy1->getRecord()->getSuperClasses();
290 for(std::vector::const_iterator i = T1SuperClasses.begin(),
291 iend = T1SuperClasses.end();
292 i != iend;
293 ++i) {
294 RecordRecTy *SuperRecTy1 = new RecordRecTy(*i);
295 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
296 if (NewType1 != 0) {
297 if (NewType1 != SuperRecTy1) {
298 delete SuperRecTy1;
299 }
300 return NewType1;
301 }
302 }
303 }
304 RecordRecTy *RecTy2 = dynamic_cast(T2);
305 if (RecTy2) {
306 // See if T1 inherits from a type T2 also inherits from
307 const std::vector &T2SuperClasses = RecTy2->getRecord()->getSuperClasses();
308 for(std::vector::const_iterator i = T2SuperClasses.begin(),
309 iend = T2SuperClasses.end();
310 i != iend;
311 ++i) {
312 RecordRecTy *SuperRecTy2 = new RecordRecTy(*i);
313 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
314 if (NewType2 != 0) {
315 if (NewType2 != SuperRecTy2) {
316 delete SuperRecTy2;
317 }
318 return NewType2;
319 }
320 }
321 }
322 return 0;
323 }
324 return T2;
325 }
326 return T1;
327 }
328
329
274330 //===----------------------------------------------------------------------===//
275331 // Initializer implementations
276332 //===----------------------------------------------------------------------===//
399455 return 0;
400456 Vals.push_back(getElement(Elements[i]));
401457 }
402 return new ListInit(Vals);
458 return new ListInit(Vals, getType());
403459 }
404460
405461 Record *ListInit::getElementAsRecord(unsigned i) const {
427483 }
428484
429485 if (Changed)
430 return new ListInit(Resolved);
486 return new ListInit(Resolved, getType());
431487 return this;
488 }
489
490 Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
491 unsigned Elt) {
492 if (Elt >= getSize())
493 return 0; // Out of range reference.
494 Init *E = getElement(Elt);
495 if (!dynamic_cast(E)) // If the element is set
496 return E; // Replace the VarListElementInit with it.
497 return 0;
432498 }
433499
434500 std::string ListInit::getAsString() const {
539605 assert(0 && "Empty list in cdr");
540606 return 0;
541607 }
542 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end());
608 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(), LHSl->getType());
543609 return Result;
544610 }
545611 break;
554620 return new IntInit(0);
555621 }
556622 }
623 StringInit *LHSs = dynamic_cast(LHS);
624 if (LHSs) {
625 if (LHSs->getValue().empty()) {
626 return new IntInit(1);
627 }
628 else {
629 return new IntInit(0);
630 }
631 }
632
557633 break;
558634 }
559635 }
666742 if (Record *D = Records.getDef(Name))
667743 return new DefInit(D);
668744
669 cerr << "Variable not defined: '" + Name + "'\n";
670 assert(0 && "Variable not found");
745 cerr << "Variable not defined in !nameconcat: '" + Name + "'\n";
746 assert(0 && "Variable not found in !nameconcat");
671747 return 0;
672748 }
673749 break;
880956 delete NewOp;
881957 }
882958 }
883 return new ListInit(NewList);
959 return new ListInit(NewList, MHSl->getType());
884960 }
885961 }
886962 return 0;
10261102 ListInits.reserve(Elements.size());
10271103 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
10281104 ListInits.push_back(new VarListElementInit(this, Elements[i]));
1029 return new ListInit(ListInits);
1105 return new ListInit(ListInits, T);
10301106 }
10311107
10321108
441441 virtual bool baseClassOf(const RecordRecTy *RHS) const;
442442 };
443443
444
444 /// resolveTypes - Find a common type that T1 and T2 convert to.
445 /// Return 0 if no such type exists.
446 ///
447 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
445448
446449 //===----------------------------------------------------------------------===//
447450 // Initializer Classes
617620
618621 /// IntInit - 7 - Represent an initalization by a literal integer value.
619622 ///
620 class IntInit : public Init {
623 class IntInit : public TypedInit {
621624 int64_t Value;
622625 public:
623 explicit IntInit(int64_t V) : Value(V) {}
626 explicit IntInit(int64_t V) : TypedInit(new IntRecTy), Value(V) {}
624627
625628 int64_t getValue() const { return Value; }
626629
630633 virtual Init *convertInitializerBitRange(const std::vector &Bits);
631634
632635 virtual std::string getAsString() const;
636
637 /// resolveBitReference - This method is used to implement
638 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
639 /// simply return the resolved value, otherwise we return null.
640 ///
641 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
642 unsigned Bit) {
643 assert(0 && "Illegal bit reference off int");
644 return 0;
645 }
646
647 /// resolveListElementReference - This method is used to implement
648 /// VarListElementInit::resolveReferences. If the list element is resolvable
649 /// now, we return the resolved value, otherwise we return null.
650 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
651 unsigned Elt) {
652 assert(0 && "Illegal element reference off int");
653 return 0;
654 }
633655 };
634656
635657
687709
688710 /// ListInit - [AL, AH, CL] - Represent a list of defs
689711 ///
690 class ListInit : public Init {
712 class ListInit : public TypedInit {
691713 std::vector Values;
692714 public:
693715 typedef std::vector::iterator iterator;
694716 typedef std::vector::const_iterator const_iterator;
695717
696 explicit ListInit(std::vector &Vs) {
718 explicit ListInit(std::vector &Vs, RecTy *EltTy)
719 : TypedInit(new ListRecTy(EltTy)) {
697720 Values.swap(Vs);
698721 }
699 explicit ListInit(iterator Start, iterator End)
700 : Values(Start, End) {}
722 explicit ListInit(iterator Start, iterator End, RecTy *EltTy)
723 : TypedInit(new ListRecTy(EltTy)), Values(Start, End) {}
701724
702725 unsigned getSize() const { return Values.size(); }
703726 Init *getElement(unsigned i) const {
729752
730753 inline size_t size () const { return Values.size(); }
731754 inline bool empty() const { return Values.empty(); }
755
756 /// resolveBitReference - This method is used to implement
757 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
758 /// simply return the resolved value, otherwise we return null.
759 ///
760 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
761 unsigned Bit) {
762 assert(0 && "Illegal bit reference off list");
763 return 0;
764 }
765
766 /// resolveListElementReference - This method is used to implement
767 /// VarListElementInit::resolveReferences. If the list element is resolvable
768 /// now, we return the resolved value, otherwise we return null.
769 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
770 unsigned Elt);
732771 };
733772
734773
1111 //===----------------------------------------------------------------------===//
1212
1313 #include
14 #include
1415
1516 #include "TGParser.h"
1617 #include "Record.h"
395396 return Result;
396397 }
397398
398 Result.TemplateArgs = ParseValueList(CurRec);
399 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
399400 if (Result.TemplateArgs.empty()) {
400401 Result.Rec = 0; // Error parsing value list.
401402 return Result;
437438 return Result;
438439 }
439440
440 Result.TemplateArgs = ParseValueList(&CurMC->Rec);
441 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
441442 if (Result.TemplateArgs.empty()) {
442443 Result.MC = 0; // Error parsing value list.
443444 return Result;
727728 || Code == UnOpInit::CDR
728729 || Code == UnOpInit::LNULL) {
729730 ListInit *LHSl = dynamic_cast(LHS);
731 StringInit *LHSs = dynamic_cast(LHS);
730732 TypedInit *LHSt = dynamic_cast(LHS);
731 if (LHSl == 0 && LHSt == 0) {
732 TokError("expected list type argument in unary operator");
733 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
734 TokError("expected list or string type argument in unary operator");
733735 return 0;
734736 }
735737 if (LHSt) {
736738 ListRecTy *LType = dynamic_cast(LHSt->getType());
737 if (LType == 0) {
739 StringRecTy *SType = dynamic_cast(LHSt->getType());
740 if (LType == 0 && SType == 0) {
741 TokError("expected list or string type argumnet in unary operator");
742 return 0;
743 }
744 }
745
746 if (Code == UnOpInit::CAR
747 || Code == UnOpInit::CDR) {
748 if (LHSl == 0 && LHSt == 0) {
738749 TokError("expected list type argumnet in unary operator");
739750 return 0;
740751 }
741 }
742
743 if (Code == UnOpInit::CAR
744 || Code == UnOpInit::CDR) {
752
745753 if (LHSl && LHSl->getSize() == 0) {
746754 TokError("empty list argument in unary operator");
747755 return 0;
10161024 /// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
10171025 /// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
10181026 ///
1019 Init *TGParser::ParseSimpleValue(Record *CurRec) {
1027 Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) {
10201028 Init *R = 0;
10211029 switch (Lex.getCode()) {
10221030 default: TokError("Unknown token when parsing a value"); break;
10481056 TokError("expected non-empty value list");
10491057 return 0;
10501058 }
1051 std::vector ValueList = ParseValueList(CurRec);
1052 if (ValueList.empty()) return 0;
1053
1054 if (Lex.getCode() != tgtok::greater) {
1055 TokError("expected '>' at end of value list");
1056 return 0;
1057 }
1058 Lex.Lex(); // eat the '>'
1059
1059
10601060 // This is a CLASS expression. This is supposed to synthesize
10611061 // a new anonymous definition, deriving from CLASS with no
10621062 // body.
10651065 Error(NameLoc, "Expected a class name, got '" + Name + "'");
10661066 return 0;
10671067 }
1068
1069 std::vector ValueList = ParseValueList(CurRec, Class);
1070 if (ValueList.empty()) return 0;
1071
1072 if (Lex.getCode() != tgtok::greater) {
1073 TokError("expected '>' at end of value list");
1074 return 0;
1075 }
1076 Lex.Lex(); // eat the '>'
10681077
10691078 // Create the new record, set it as CurRec temporarily.
10701079 static unsigned AnonCounter = 0;
11131122 Lex.Lex(); // eat the '['
11141123 std::vector Vals;
11151124
1125 RecTy *DeducedEltTy = 0;
1126 ListRecTy *GivenListTy = 0;
1127
1128 if (ItemType != 0) {
1129 ListRecTy *ListType = dynamic_cast(ItemType);
1130 if (ListType == 0) {
1131 std::stringstream s;
1132 s << "Type mismatch for list, expected list type, got "
1133 << ItemType->getAsString();
1134 TokError(s.str());
1135 }
1136 GivenListTy = ListType;
1137 }
1138
11161139 if (Lex.getCode() != tgtok::r_square) {
1117 Vals = ParseValueList(CurRec);
1140 Vals = ParseValueList(CurRec, 0, GivenListTy ? GivenListTy->getElementType() : 0);
11181141 if (Vals.empty()) return 0;
11191142 }
11201143 if (Lex.getCode() != tgtok::r_square) {
11221145 return 0;
11231146 }
11241147 Lex.Lex(); // eat the ']'
1125 return new ListInit(Vals);
1148
1149 RecTy *GivenEltTy = 0;
1150 if (Lex.getCode() == tgtok::less) {
1151 // Optional list element type
1152 Lex.Lex(); // eat the '<'
1153
1154 GivenEltTy = ParseType();
1155 if (GivenEltTy == 0) {
1156 // Couldn't parse element type
1157 return 0;
1158 }
1159
1160 if (Lex.getCode() != tgtok::greater) {
1161 TokError("expected '>' at end of list element type");
1162 return 0;
1163 }
1164 Lex.Lex(); // eat the '>'
1165 }
1166
1167 // Check elements
1168 RecTy *EltTy = 0;
1169 for (std::vector::iterator i = Vals.begin(), ie = Vals.end();
1170 i != ie;
1171 ++i) {
1172 TypedInit *TArg = dynamic_cast(*i);
1173 if (TArg == 0) {
1174 TokError("Untyped list element");
1175 return 0;
1176 }
1177 if (EltTy != 0) {
1178 EltTy = resolveTypes(EltTy, TArg->getType());
1179 if (EltTy == 0) {
1180 TokError("Incompatible types in list elements");
1181 return 0;
1182 }
1183 }
1184 else {
1185 EltTy = TArg->getType();
1186 }
1187 }
1188
1189 if (GivenEltTy != 0) {
1190 if (EltTy != 0) {
1191 // Verify consistency
1192 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1193 TokError("Incompatible types in list elements");
1194 return 0;
1195 }
1196 }
1197 EltTy = GivenEltTy;
1198 }
1199
1200 if (EltTy == 0) {
1201 if (ItemType == 0) {
1202 TokError("No type for list");
1203 return 0;
1204 }
1205 DeducedEltTy = GivenListTy->getElementType();
1206 }
1207 else {
1208 // Make sure the deduced type is compatible with the given type
1209 if (GivenListTy) {
1210 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1211 TokError("Element type mismatch for list");
1212 return 0;
1213 }
1214 }
1215 DeducedEltTy = EltTy;
1216 }
1217
1218 return new ListInit(Vals, DeducedEltTy);
11261219 }
11271220 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
11281221 Lex.Lex(); // eat the '('
11991292 /// ValueSuffix ::= '[' BitList ']'
12001293 /// ValueSuffix ::= '.' ID
12011294 ///
1202 Init *TGParser::ParseValue(Record *CurRec) {
1203 Init *Result = ParseSimpleValue(CurRec);
1295 Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) {
1296 Init *Result = ParseSimpleValue(CurRec, ItemType);
12041297 if (Result == 0) return 0;
12051298
12061299 // Parse the suffixes now if present.
13051398 ///
13061399 /// ValueList ::= Value (',' Value)
13071400 ///
1308 std::vector TGParser::ParseValueList(Record *CurRec) {
1401 std::vector TGParser::ParseValueList(Record *CurRec, Record *ArgsRec, RecTy *EltTy) {
13091402 std::vector Result;
1310 Result.push_back(ParseValue(CurRec));
1403 RecTy *ItemType = EltTy;
1404 int ArgN = 0;
1405 if (ArgsRec != 0 && EltTy == 0) {
1406 const std::vector &TArgs = ArgsRec->getTemplateArgs();
1407 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1408 assert(RV && "Template argument record not found??");
1409 ItemType = RV->getType();
1410 ++ArgN;
1411 }
1412 Result.push_back(ParseValue(CurRec, ItemType));
13111413 if (Result.back() == 0) return std::vector();
13121414
13131415 while (Lex.getCode() == tgtok::comma) {
13141416 Lex.Lex(); // Eat the comma
13151417
1316 Result.push_back(ParseValue(CurRec));
1418 if (ArgsRec != 0 && EltTy == 0) {
1419 const std::vector &TArgs = ArgsRec->getTemplateArgs();
1420 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1421 assert(RV && "Template argument record not found??");
1422 ItemType = RV->getType();
1423 ++ArgN;
1424 }
1425 Result.push_back(ParseValue(CurRec, ItemType));
13171426 if (Result.back() == 0) return std::vector();
13181427 }
13191428
13681477 if (Lex.getCode() == tgtok::equal) {
13691478 Lex.Lex();
13701479 TGLoc ValLoc = Lex.getLoc();
1371 Init *Val = ParseValue(CurRec);
1480 Init *Val = ParseValue(CurRec, Type);
13721481 if (Val == 0 ||
13731482 SetValue(CurRec, ValLoc, DeclName, std::vector(), Val))
13741483 return "";
14461555 return TokError("expected '=' in let expression");
14471556 Lex.Lex(); // eat the '='.
14481557
1449 Init *Val = ParseValue(CurRec);
1558 RecordVal *Field = CurRec->getValue(FieldName);
1559 if (Field == 0)
1560 return TokError("Value '" + FieldName + "' unknown!");
1561
1562 RecTy *Type = Field->getType();
1563
1564 Init *Val = ParseValue(CurRec, Type);
14501565 if (Val == 0) return true;
14511566
14521567 if (Lex.getCode() != tgtok::semi)
9292
9393 Init *ParseIDValue(Record *CurRec);
9494 Init *ParseIDValue(Record *CurRec, const std::string &Name, TGLoc NameLoc);
95 Init *ParseSimpleValue(Record *CurRec);
96 Init *ParseValue(Record *CurRec);
97 std::vector ParseValueList(Record *CurRec);
95 Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0);
96 Init *ParseValue(Record *CurRec, RecTy *ItemType = 0);
97 std::vector ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0);
9898 std::vector > ParseDagArgList(Record *);
9999 bool ParseOptionalRangeList(std::vector &Ranges);
100100 bool ParseOptionalBitList(std::vector &Ranges);