llvm.org GIT mirror llvm / e6c27de
Implement !cast. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@71794 91177308-0d34-0410-b5e6-96231b3b80d8 David Greene 10 years ago
7 changed file(s) with 270 addition(s) and 179 deletion(s). Raw diff Collapse all Expand all
397397
!strconcat(a, b)
398398
A string value that is the result of concatenating the 'a' and 'b'
399399 strings.
400
!cast(a)
401
A symbol of type type obtained by looking up the string 'a' in
402 the symbol table. If the type of 'a' does not match type, TableGen
403 aborts with an error.
400404
!nameconcat<type>(a, b)
401
A value that is the result of concatenating the 'a' and 'b'
402 strings and looking up the resulting name in the symbol table. The symbol type
403 determines the type of the resulting value. If the symbol is not found
404 or the symbol type does not match 'type,' TableGen emits an error and aborts.
405
Shorthand for !cast(!strconcat(a, b))
405406
406407
407408

Note that all of the values have rules specifying how they convert to values

0 // RUN: tblgen %s | grep {add_ps} | count 3
1
2 class ValueType {
3 int Size = size;
4 int Value = value;
5 }
6
7 def v2i64 : ValueType<128, 22>; // 2 x i64 vector value
8 def v2f64 : ValueType<128, 28>; // 2 x f64 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 VR128 : RegisterClass<[v2i64, v2f64],
57 [XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
58 XMM8, XMM9, XMM10, XMM11,
59 XMM12, XMM13, XMM14, XMM15]>;
60
61 // Define intrinsics
62 def int_x86_sse2_add_ps : Intrinsic<"addps">;
63 def int_x86_sse2_add_pd : Intrinsic<"addpd">;
64
65 multiclass arith opcode, string asmstr, string Intr> {
66 def PS : Inst
67 !strconcat(asmstr, "\t$dst, $src1, $src2"),
68 [(set VR128:$dst, (!cast(!strconcat(Intr, "_ps")) VR128:$src1, VR128:$src2))]>;
69
70 def PD : Inst
71 !strconcat(asmstr, "\t$dst, $src1, $src2"),
72 [(set VR128:$dst, (!cast(!strconcat(Intr, "_pd")) VR128:$src1, VR128:$src2))]>;
73 }
74
75 defm ADD : arith<0x58, "add", "int_x86_sse2_add">;
76
77 class IntInst opcode, string asmstr, Intrinsic Intr> :
78 Inst
79 !strconcat(asmstr, "\t$dst, $src1, $src2"),
80 [(set VR128:$dst, (Intr VR128:$src1, VR128:$src2))]>;
81
82
83 multiclass arith_int opcode, string asmstr, string Intr> {
84 def PS_Int : IntInst(!strconcat(Intr, "_ps"))>;
85
86 def PD_Int : IntInst(!strconcat(Intr, "_pd"))>;
87 }
88
89 defm ADD : arith_int<0x58, "add", "int_x86_sse2_add">;
131131 return 0;
132132 }
133133
134 // Init *StringRecTy::convertValue(UnOpInit *BO) {
135 // if (BO->getOpcode() == UnOpInit::CAST) {
136 // Init *L = BO->getOperand()->convertInitializerTo(this);
137 // if (L == 0) return 0;
138 // if (L != BO->getOperand())
139 // return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
140 // return BO;
141 // }
142
143 // return convertValue((TypedInit*)BO);
144 // }
134 Init *StringRecTy::convertValue(UnOpInit *BO) {
135 if (BO->getOpcode() == UnOpInit::CAST) {
136 Init *L = BO->getOperand()->convertInitializerTo(this);
137 if (L == 0) return 0;
138 if (L != BO->getOperand())
139 return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
140 return BO;
141 }
142
143 return convertValue((TypedInit*)BO);
144 }
145145
146146 Init *StringRecTy::convertValue(BinOpInit *BO) {
147147 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
211211 return 0;
212212 }
213213
214 // Init *DagRecTy::convertValue(UnOpInit *BO) {
215 // if (BO->getOpcode() == UnOpInit::CAST) {
216 // Init *L = BO->getOperand()->convertInitializerTo(this);
217 // if (L == 0) return 0;
218 // if (L != BO->getOperand())
219 // return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
220 // return BO;
221 // }
222 // return 0;
223 // }
214 Init *DagRecTy::convertValue(UnOpInit *BO) {
215 if (BO->getOpcode() == UnOpInit::CAST) {
216 Init *L = BO->getOperand()->convertInitializerTo(this);
217 if (L == 0) return 0;
218 if (L != BO->getOperand())
219 return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
220 return BO;
221 }
222 return 0;
223 }
224224
225225 Init *DagRecTy::convertValue(BinOpInit *BO) {
226226 if (BO->getOpcode() == BinOpInit::CONCAT) {
466466 return 0;
467467 }
468468
469 // Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
470 // switch (getOpcode()) {
471 // default: assert(0 && "Unknown unop");
472 // case CAST: {
473 // StringInit *LHSs = dynamic_cast(LHS);
474 // if (LHSs) {
475 // std::string Name = LHSs->getValue();
476
477 // // From TGParser::ParseIDValue
478 // if (CurRec) {
479 // if (const RecordVal *RV = CurRec->getValue(Name)) {
480 // if (RV->getType() != getType()) {
481 // throw "type mismatch in nameconcat";
482 // }
483 // return new VarInit(Name, RV->getType());
484 // }
469 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
470 switch (getOpcode()) {
471 default: assert(0 && "Unknown unop");
472 case CAST: {
473 StringInit *LHSs = dynamic_cast(LHS);
474 if (LHSs) {
475 std::string Name = LHSs->getValue();
476
477 // From TGParser::ParseIDValue
478 if (CurRec) {
479 if (const RecordVal *RV = CurRec->getValue(Name)) {
480 if (RV->getType() != getType()) {
481 throw "type mismatch in nameconcat";
482 }
483 return new VarInit(Name, RV->getType());
484 }
485485
486 // std::string TemplateArgName = CurRec->getName()+":"+Name;
487 // if (CurRec->isTemplateArg(TemplateArgName)) {
488 // const RecordVal *RV = CurRec->getValue(TemplateArgName);
489 // assert(RV && "Template arg doesn't exist??");
490
491 // if (RV->getType() != getType()) {
492 // throw "type mismatch in nameconcat";
493 // }
494
495 // return new VarInit(TemplateArgName, RV->getType());
496 // }
497 // }
498
499 // if (CurMultiClass) {
500 // std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
501 // if (CurMultiClass->Rec.isTemplateArg(MCName)) {
502 // const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
503 // assert(RV && "Template arg doesn't exist??");
504
505 // if (RV->getType() != getType()) {
506 // throw "type mismatch in nameconcat";
507 // }
486 std::string TemplateArgName = CurRec->getName()+":"+Name;
487 if (CurRec->isTemplateArg(TemplateArgName)) {
488 const RecordVal *RV = CurRec->getValue(TemplateArgName);
489 assert(RV && "Template arg doesn't exist??");
490
491 if (RV->getType() != getType()) {
492 throw "type mismatch in nameconcat";
493 }
494
495 return new VarInit(TemplateArgName, RV->getType());
496 }
497 }
498
499 if (CurMultiClass) {
500 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
501 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
502 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
503 assert(RV && "Template arg doesn't exist??");
504
505 if (RV->getType() != getType()) {
506 throw "type mismatch in nameconcat";
507 }
508508
509 // return new VarInit(MCName, RV->getType());
510 // }
511 // }
512
513 // if (Record *D = Records.getDef(Name))
514 // return new DefInit(D);
515
516 // cerr << "Variable not defined: '" + Name + "'\n";
517 // assert(0 && "Variable not found");
518 // return 0;
519 // }
520 // break;
521 // }
522 // }
523 // return this;
524 // }
525
526 // Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
527 // Init *lhs = LHS->resolveReferences(R, RV);
509 return new VarInit(MCName, RV->getType());
510 }
511 }
512
513 if (Record *D = Records.getDef(Name))
514 return new DefInit(D);
515
516 cerr << "Variable not defined: '" + Name + "'\n";
517 assert(0 && "Variable not found");
518 return 0;
519 }
520 break;
521 }
522 }
523 return this;
524 }
525
526 Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
527 Init *lhs = LHS->resolveReferences(R, RV);
528528
529 // if (LHS != lhs)
530 // return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
531 // return Fold(&R, 0);
532 // }
533
534 // std::string UnOpInit::getAsString() const {
535 // std::string Result;
536 // switch (Opc) {
537 // case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
538 // }
539 // return Result + "(" + LHS->getAsString() + ")";
540 // }
529 if (LHS != lhs)
530 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
531 return Fold(&R, 0);
532 }
533
534 std::string UnOpInit::getAsString() const {
535 std::string Result;
536 switch (Opc) {
537 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
538 }
539 return Result + "(" + LHS->getAsString() + ")";
540 }
541541
542542 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
543543 switch (getOpcode()) {
4040 class StringInit;
4141 class CodeInit;
4242 class ListInit;
43 //class UnOpInit;
43 class UnOpInit;
4444 class BinOpInit;
4545 //class TernOpInit;
4646 class DefInit;
7878 virtual Init *convertValue( IntInit *II) { return 0; }
7979 virtual Init *convertValue(StringInit *SI) { return 0; }
8080 virtual Init *convertValue( ListInit *LI) { return 0; }
81 // virtual Init *convertValue( UnOpInit *UI) {
82 // return convertValue((TypedInit*)UI);
83 // }
81 virtual Init *convertValue( UnOpInit *UI) {
82 return convertValue((TypedInit*)UI);
83 }
8484 virtual Init *convertValue( BinOpInit *UI) {
8585 return convertValue((TypedInit*)UI);
8686 }
132132 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
133133 virtual Init *convertValue( DefInit *DI) { return 0; }
134134 virtual Init *convertValue( DagInit *DI) { return 0; }
135 // virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
135 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
136136 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
137137 //virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
138138 virtual Init *convertValue( TypedInit *TI);
176176 virtual Init *convertValue(VarBitInit *VB) { return 0; }
177177 virtual Init *convertValue( DefInit *DI) { return 0; }
178178 virtual Init *convertValue( DagInit *DI) { return 0; }
179 //virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
179 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
180180 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
181181 //virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
182182 virtual Init *convertValue( TypedInit *TI);
216216 virtual Init *convertValue(VarBitInit *VB) { return 0; }
217217 virtual Init *convertValue( DefInit *DI) { return 0; }
218218 virtual Init *convertValue( DagInit *DI) { return 0; }
219 //virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
219 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
220220 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
221221 //virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
222222 virtual Init *convertValue( TypedInit *TI);
250250 virtual Init *convertValue( IntInit *II) { return 0; }
251251 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
252252 virtual Init *convertValue( ListInit *LI) { return 0; }
253 //virtual Init *convertValue( UnOpInit *BO);
253 virtual Init *convertValue( UnOpInit *BO);
254254 virtual Init *convertValue( BinOpInit *BO);
255255 //virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
256256
300300 virtual Init *convertValue(VarBitInit *VB) { return 0; }
301301 virtual Init *convertValue( DefInit *DI) { return 0; }
302302 virtual Init *convertValue( DagInit *DI) { return 0; }
303 //virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
303 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
304304 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
305305 //virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
306306 virtual Init *convertValue( TypedInit *TI);
339339 virtual Init *convertValue(VarBitInit *VB) { return 0; }
340340 virtual Init *convertValue( DefInit *DI) { return 0; }
341341 virtual Init *convertValue( DagInit *DI) { return 0; }
342 //virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
342 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
343343 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
344344 //virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
345345 virtual Init *convertValue( TypedInit *TI);
374374 virtual Init *convertValue( CodeInit *CI) { return 0; }
375375 virtual Init *convertValue(VarBitInit *VB) { return 0; }
376376 virtual Init *convertValue( DefInit *DI) { return 0; }
377 //virtual Init *convertValue( UnOpInit *BO);
377 virtual Init *convertValue( UnOpInit *BO);
378378 virtual Init *convertValue( BinOpInit *BO);
379379 //virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
380380 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
417417 virtual Init *convertValue( ListInit *LI) { return 0; }
418418 virtual Init *convertValue( CodeInit *CI) { return 0; }
419419 virtual Init *convertValue(VarBitInit *VB) { return 0; }
420 //virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
420 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
421421 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
422422 //virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
423423 virtual Init *convertValue( DefInit *DI);
739739
740740 /// UnOpInit - !op (X) - Transform an init.
741741 ///
742 // class UnOpInit : public OpInit {
743 // public:
744 // enum UnaryOp { CAST };
745 // private:
746 // UnaryOp Opc;
747 // Init *LHS;
748 // public:
749 // UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) :
750 // OpInit(Type), Opc(opc), LHS(lhs) {
751 // }
752
753 // // Clone - Clone this operator, replacing arguments with the new list
754 // virtual OpInit *clone(std::vector &Operands) {
755 // assert(Operands.size() == 1 && "Wrong number of operands for unary operation");
756 // return new UnOpInit(getOpcode(), *Operands.begin(), getType());
757 // }
758
759 // int getNumOperands(void) const { return 1; }
760 // Init *getOperand(int i) {
761 // assert(i == 0 && "Invalid operand id for unary operator");
762 // return getOperand();
763 // }
764
765 // UnaryOp getOpcode() const { return Opc; }
766 // Init *getOperand() const { return LHS; }
767
768 // // Fold - If possible, fold this to a simpler init. Return this if not
769 // // possible to fold.
770 // Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
771
772 // virtual Init *resolveReferences(Record &R, const RecordVal *RV);
773
774 // virtual std::string getAsString() const;
775 // };
742 class UnOpInit : public OpInit {
743 public:
744 enum UnaryOp { CAST };
745 private:
746 UnaryOp Opc;
747 Init *LHS;
748 public:
749 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) :
750 OpInit(Type), Opc(opc), LHS(lhs) {
751 }
752
753 // Clone - Clone this operator, replacing arguments with the new list
754 virtual OpInit *clone(std::vector &Operands) {
755 assert(Operands.size() == 1 && "Wrong number of operands for unary operation");
756 return new UnOpInit(getOpcode(), *Operands.begin(), getType());
757 }
758
759 int getNumOperands(void) const { return 1; }
760 Init *getOperand(int i) {
761 assert(i == 0 && "Invalid operand id for unary operator");
762 return getOperand();
763 }
764
765 UnaryOp getOpcode() const { return Opc; }
766 Init *getOperand() const { return LHS; }
767
768 // Fold - If possible, fold this to a simpler init. Return this if not
769 // possible to fold.
770 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
771
772 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
773
774 virtual std::string getAsString() const;
775 };
776776
777777 /// BinOpInit - !op (X, Y) - Combine two inits.
778778 ///
448448 if (Len == 10 && !memcmp(Start, "nameconcat", 10)) return tgtok::XNameConcat;
449449 // if (Len == 5 && !memcmp(Start, "subst", 5)) return tgtok::XSubst;
450450 // if (Len == 7 && !memcmp(Start, "foreach", 7)) return tgtok::XForEach;
451 // if (Len == 4 && !memcmp(Start, "cast", 4)) return tgtok::XCast;
451 if (Len == 4 && !memcmp(Start, "cast", 4)) return tgtok::XCast;
452452
453453 return ReturnError(Start-1, "Unknown operator");
454454 }
4444 MultiClass, String,
4545
4646 // !keywords.
47 XConcat, XSRA, XSRL, XSHL, XStrConcat, XNameConcat, //XSubst, XCast,
47 XConcat, XSRA, XSRL, XSHL, XStrConcat, XNameConcat, XCast, // XSubst,
4848 //XForEach,
4949
5050 // Integer value.
679679 TokError("unknown operation");
680680 return 0;
681681 break;
682 // case tgtok::XCast: { // Value ::= !unop '(' Value ')'
683 // UnOpInit::UnaryOp Code;
684 // RecTy *Type = 0;
685
686 // switch (Lex.getCode()) {
687 // default: assert(0 && "Unhandled code!");
688 // case tgtok::XCast:
689 // Lex.Lex(); // eat the operation
690 // Code = UnOpInit::CAST;
691
692 // Type = ParseOperatorType();
693
694 // if (Type == 0) {
695 // TokError("did not get type for binary operator");
696 // return 0;
697 // }
698
699 // break;
700 // }
701 // if (Lex.getCode() != tgtok::l_paren) {
702 // TokError("expected '(' after unary operator");
703 // return 0;
704 // }
705 // Lex.Lex(); // eat the '('
706
707 // Init *LHS = ParseValue(CurRec);
708 // if (LHS == 0) return 0;
709
710 // if (Lex.getCode() != tgtok::r_paren) {
711 // TokError("expected ')' in unary operator");
712 // return 0;
713 // }
714 // Lex.Lex(); // eat the ')'
715 // return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
716 // }
682 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
683 UnOpInit::UnaryOp Code;
684 RecTy *Type = 0;
685
686 switch (Lex.getCode()) {
687 default: assert(0 && "Unhandled code!");
688 case tgtok::XCast:
689 Lex.Lex(); // eat the operation
690 Code = UnOpInit::CAST;
691
692 Type = ParseOperatorType();
693
694 if (Type == 0) {
695 TokError("did not get type for binary operator");
696 return 0;
697 }
698
699 break;
700 }
701 if (Lex.getCode() != tgtok::l_paren) {
702 TokError("expected '(' after unary operator");
703 return 0;
704 }
705 Lex.Lex(); // eat the '('
706
707 Init *LHS = ParseValue(CurRec);
708 if (LHS == 0) return 0;
709
710 if (Lex.getCode() != tgtok::r_paren) {
711 TokError("expected ')' in unary operator");
712 return 0;
713 }
714 Lex.Lex(); // eat the ')'
715 return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
716 }
717717
718718 case tgtok::XConcat:
719719 case tgtok::XSRA:
10281028 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
10291029 Lex.Lex(); // eat the '('
10301030 if (Lex.getCode() != tgtok::Id
1031 // && Lex.getCode() != tgtok::XCast
1031 && Lex.getCode() != tgtok::XCast
10321032 && Lex.getCode() != tgtok::XNameConcat) {
10331033 TokError("expected identifier in dag init");
10341034 return 0;
10711071 break;
10721072 }
10731073
1074 // case tgtok::XCast: // Value ::= !unop '(' Value ')'
1074 case tgtok::XCast: // Value ::= !unop '(' Value ')'
10751075 case tgtok::XConcat:
10761076 case tgtok::XSRA:
10771077 case tgtok::XSRL: