llvm.org GIT mirror llvm / 2c6d713
Don't use stack unwinding to provide the location information for SetTheory, but pass down the location explicitly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@166629 91177308-0d34-0410-b5e6-96231b3b80d8 Joerg Sonnenberger 8 years ago
4 changed file(s) with 78 addition(s) and 70 deletion(s). Raw diff Collapse all Expand all
599599 unsigned Length = ~0u;
600600 SmallVector Lists(Dim);
601601 for (unsigned i = 0; i != Dim; ++i) {
602 ST.evaluate(SubRegs->getElement(i), Lists[i]);
602 ST.evaluate(SubRegs->getElement(i), Lists[i], Def->getLoc());
603603 Length = std::min(Length, unsigned(Lists[i].size()));
604604 }
605605
727727 // Alternative allocation orders may be subsets.
728728 SetTheory::RecSet Order;
729729 for (unsigned i = 0, e = AltOrders->size(); i != e; ++i) {
730 RegBank.getSets().evaluate(AltOrders->getElement(i), Order);
730 RegBank.getSets().evaluate(AltOrders->getElement(i), Order, R->getLoc());
731731 Orders[1 + i].append(Order.begin(), Order.end());
732732 // Verify that all altorder members are regclass members.
733733 while (!Order.empty()) {
3737
3838 // (instrs a, b, ...) Evaluate and union all arguments. Identical to AddOp.
3939 struct InstrsOp : public SetTheory::Operator {
40 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts) {
41 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts);
40 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
41 ArrayRef Loc) {
42 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
4243 }
4344 };
4445
5455 const CodeGenTarget &Target;
5556 InstRegexOp(const CodeGenTarget &t): Target(t) {}
5657
57 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts) {
58 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
59 ArrayRef Loc) {
5860 SmallVector RegexList;
5961 for (DagInit::const_arg_iterator
6062 AI = Expr->arg_begin(), AE = Expr->arg_end(); AI != AE; ++AI) {
6163 StringInit *SI = dyn_cast(*AI);
6264 if (!SI)
63 throw "instregex requires pattern string: " + Expr->getAsString();
65 throw TGError(Loc, "instregex requires pattern string: "
66 + Expr->getAsString());
6467 std::string pat = SI->getValue();
6568 // Implement a python-style prefix match.
6669 if (pat[0] != '^') {
2626
2727 // (add a, b, ...) Evaluate and union all arguments.
2828 struct AddOp : public SetTheory::Operator {
29 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
30 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts);
29 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts, ArrayRef Loc) {
30 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
3131 }
3232 };
3333
3434 // (sub Add, Sub, ...) Set difference.
3535 struct SubOp : public SetTheory::Operator {
36 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
36 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts, ArrayRef Loc) {
3737 if (Expr->arg_size() < 2)
38 throw "Set difference needs at least two arguments: " +
39 Expr->getAsString();
38 throw TGError(Loc, "Set difference needs at least two arguments: " +
39 Expr->getAsString());
4040 RecSet Add, Sub;
41 ST.evaluate(*Expr->arg_begin(), Add);
42 ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Sub);
41 ST.evaluate(*Expr->arg_begin(), Add, Loc);
42 ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Sub, Loc);
4343 for (RecSet::iterator I = Add.begin(), E = Add.end(); I != E; ++I)
4444 if (!Sub.count(*I))
4545 Elts.insert(*I);
4848
4949 // (and S1, S2) Set intersection.
5050 struct AndOp : public SetTheory::Operator {
51 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
51 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts, ArrayRef Loc) {
5252 if (Expr->arg_size() != 2)
53 throw "Set intersection requires two arguments: " + Expr->getAsString();
53 throw TGError(Loc, "Set intersection requires two arguments: " +
54 Expr->getAsString());
5455 RecSet S1, S2;
55 ST.evaluate(Expr->arg_begin()[0], S1);
56 ST.evaluate(Expr->arg_begin()[1], S2);
56 ST.evaluate(Expr->arg_begin()[0], S1, Loc);
57 ST.evaluate(Expr->arg_begin()[1], S2, Loc);
5758 for (RecSet::iterator I = S1.begin(), E = S1.end(); I != E; ++I)
5859 if (S2.count(*I))
5960 Elts.insert(*I);
6465 struct SetIntBinOp : public SetTheory::Operator {
6566 virtual void apply2(SetTheory &ST, DagInit *Expr,
6667 RecSet &Set, int64_t N,
67 RecSet &Elts) =0;
68
69 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
68 RecSet &Elts, ArrayRef Loc) =0;
69
70 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts, ArrayRef Loc) {
7071 if (Expr->arg_size() != 2)
71 throw "Operator requires (Op Set, Int) arguments: " + Expr->getAsString();
72 throw TGError(Loc, "Operator requires (Op Set, Int) arguments: " +
73 Expr->getAsString());
7274 RecSet Set;
73 ST.evaluate(Expr->arg_begin()[0], Set);
75 ST.evaluate(Expr->arg_begin()[0], Set, Loc);
7476 IntInit *II = dyn_cast(Expr->arg_begin()[1]);
7577 if (!II)
76 throw "Second argument must be an integer: " + Expr->getAsString();
77 apply2(ST, Expr, Set, II->getValue(), Elts);
78 throw TGError(Loc, "Second argument must be an integer: " +
79 Expr->getAsString());
80 apply2(ST, Expr, Set, II->getValue(), Elts, Loc);
7881 }
7982 };
8083
8285 struct ShlOp : public SetIntBinOp {
8386 void apply2(SetTheory &ST, DagInit *Expr,
8487 RecSet &Set, int64_t N,
85 RecSet &Elts) {
88 RecSet &Elts, ArrayRef Loc) {
8689 if (N < 0)
87 throw "Positive shift required: " + Expr->getAsString();
90 throw TGError(Loc, "Positive shift required: " +
91 Expr->getAsString());
8892 if (unsigned(N) < Set.size())
8993 Elts.insert(Set.begin() + N, Set.end());
9094 }
9498 struct TruncOp : public SetIntBinOp {
9599 void apply2(SetTheory &ST, DagInit *Expr,
96100 RecSet &Set, int64_t N,
97 RecSet &Elts) {
101 RecSet &Elts, ArrayRef Loc) {
98102 if (N < 0)
99 throw "Positive length required: " + Expr->getAsString();
103 throw TGError(Loc, "Positive length required: " +
104 Expr->getAsString());
100105 if (unsigned(N) > Set.size())
101106 N = Set.size();
102107 Elts.insert(Set.begin(), Set.begin() + N);
111116
112117 void apply2(SetTheory &ST, DagInit *Expr,
113118 RecSet &Set, int64_t N,
114 RecSet &Elts) {
119 RecSet &Elts, ArrayRef Loc) {
115120 if (Reverse)
116121 N = -N;
117122 // N > 0 -> rotate left, N < 0 -> rotate right.
130135 struct DecimateOp : public SetIntBinOp {
131136 void apply2(SetTheory &ST, DagInit *Expr,
132137 RecSet &Set, int64_t N,
133 RecSet &Elts) {
138 RecSet &Elts, ArrayRef Loc) {
134139 if (N <= 0)
135 throw "Positive stride required: " + Expr->getAsString();
140 throw TGError(Loc, "Positive stride required: " +
141 Expr->getAsString());
136142 for (unsigned I = 0; I < Set.size(); I += N)
137143 Elts.insert(Set[I]);
138144 }
140146
141147 // (interleave S1, S2, ...) Interleave elements of the arguments.
142148 struct InterleaveOp : public SetTheory::Operator {
143 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
149 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts, ArrayRef Loc) {
144150 // Evaluate the arguments individually.
145151 SmallVector Args(Expr->getNumArgs());
146152 unsigned MaxSize = 0;
147153 for (unsigned i = 0, e = Expr->getNumArgs(); i != e; ++i) {
148 ST.evaluate(Expr->getArg(i), Args[i]);
154 ST.evaluate(Expr->getArg(i), Args[i], Loc);
149155 MaxSize = std::max(MaxSize, unsigned(Args[i].size()));
150156 }
151157 // Interleave arguments into Elts.
158164
159165 // (sequence "Format", From, To) Generate a sequence of records by name.
160166 struct SequenceOp : public SetTheory::Operator {
161 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
167 void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts, ArrayRef Loc) {
162168 int Step = 1;
163169 if (Expr->arg_size() > 4)
164 throw "Bad args to (sequence \"Format\", From, To): " +
165 Expr->getAsString();
170 throw TGError(Loc, "Bad args to (sequence \"Format\", From, To): " +
171 Expr->getAsString());
166172 else if (Expr->arg_size() == 4) {
167173 if (IntInit *II = dyn_cast(Expr->arg_begin()[3])) {
168174 Step = II->getValue();
169175 } else
170 throw "Stride must be an integer: " + Expr->getAsString();
176 throw TGError(Loc, "Stride must be an integer: " + Expr->getAsString());
171177 }
172178
173179 std::string Format;
174180 if (StringInit *SI = dyn_cast(Expr->arg_begin()[0]))
175181 Format = SI->getValue();
176182 else
177 throw "Format must be a string: " + Expr->getAsString();
183 throw TGError(Loc, "Format must be a string: " + Expr->getAsString());
178184
179185 int64_t From, To;
180186 if (IntInit *II = dyn_cast(Expr->arg_begin()[1]))
181187 From = II->getValue();
182188 else
183 throw "From must be an integer: " + Expr->getAsString();
189 throw TGError(Loc, "From must be an integer: " + Expr->getAsString());
184190 if (From < 0 || From >= (1 << 30))
185 throw "From out of range";
191 throw TGError(Loc, "From out of range");
186192
187193 if (IntInit *II = dyn_cast(Expr->arg_begin()[2]))
188194 To = II->getValue();
189195 else
190 throw "From must be an integer: " + Expr->getAsString();
196 throw TGError(Loc, "From must be an integer: " + Expr->getAsString());
191197 if (To < 0 || To >= (1 << 30))
192 throw "To out of range";
198 throw TGError(Loc, "To out of range");
193199
194200 RecordKeeper &Records =
195201 cast(Expr->getOperator())->getDef()->getRecords();
205211 OS << format(Format.c_str(), unsigned(From));
206212 Record *Rec = Records.getDef(OS.str());
207213 if (!Rec)
208 throw "No def named '" + Name + "': " + Expr->getAsString();
214 throw TGError(Loc, "No def named '" + Name + "': " +
215 Expr->getAsString());
209216 // Try to reevaluate Rec in case it is a set.
210217 if (const RecVec *Result = ST.expand(Rec))
211218 Elts.insert(Result->begin(), Result->end());
224231 FieldExpander(StringRef fn) : FieldName(fn) {}
225232
226233 void expand(SetTheory &ST, Record *Def, RecSet &Elts) {
227 ST.evaluate(Def->getValueInit(FieldName), Elts);
234 ST.evaluate(Def->getValueInit(FieldName), Elts, Def->getLoc());
228235 }
229236 };
230237 } // end anonymous namespace
258265 addExpander(ClassName, new FieldExpander(FieldName));
259266 }
260267
261 void SetTheory::evaluate(Init *Expr, RecSet &Elts) {
268 void SetTheory::evaluate(Init *Expr, RecSet &Elts, ArrayRef Loc) {
262269 // A def in a list can be a just an element, or it may expand.
263270 if (DefInit *Def = dyn_cast(Expr)) {
264271 if (const RecVec *Result = expand(Def->getDef()))
269276
270277 // Lists simply expand.
271278 if (ListInit *LI = dyn_cast(Expr))
272 return evaluate(LI->begin(), LI->end(), Elts);
279 return evaluate(LI->begin(), LI->end(), Elts, Loc);
273280
274281 // Anything else must be a DAG.
275282 DagInit *DagExpr = dyn_cast(Expr);
276283 if (!DagExpr)
277 throw "Invalid set element: " + Expr->getAsString();
284 throw TGError(Loc, "Invalid set element: " + Expr->getAsString());
278285 DefInit *OpInit = dyn_cast(DagExpr->getOperator());
279286 if (!OpInit)
280 throw "Bad set expression: " + Expr->getAsString();
287 throw TGError(Loc, "Bad set expression: " + Expr->getAsString());
281288 Operator *Op = Operators.lookup(OpInit->getDef()->getName());
282289 if (!Op)
283 throw "Unknown set operator: " + Expr->getAsString();
284 Op->apply(*this, DagExpr, Elts);
290 throw TGError(Loc, "Unknown set operator: " + Expr->getAsString());
291 Op->apply(*this, DagExpr, Elts, Loc);
285292 }
286293
287294 const RecVec *SetTheory::expand(Record *Set) {
291298 return &I->second;
292299
293300 // This is the first time we see Set. Find a suitable expander.
294 try {
295 const std::vector &SC = Set->getSuperClasses();
296 for (unsigned i = 0, e = SC.size(); i != e; ++i) {
297 // Skip unnamed superclasses.
298 if (!dyn_cast(SC[i]->getNameInit()))
299 continue;
300 if (Expander *Exp = Expanders.lookup(SC[i]->getName())) {
301 // This breaks recursive definitions.
302 RecVec &EltVec = Expansions[Set];
303 RecSet Elts;
304 Exp->expand(*this, Set, Elts);
305 EltVec.assign(Elts.begin(), Elts.end());
306 return &EltVec;
307 }
301 const std::vector &SC = Set->getSuperClasses();
302 for (unsigned i = 0, e = SC.size(); i != e; ++i) {
303 // Skip unnamed superclasses.
304 if (!dyn_cast(SC[i]->getNameInit()))
305 continue;
306 if (Expander *Exp = Expanders.lookup(SC[i]->getName())) {
307 // This breaks recursive definitions.
308 RecVec &EltVec = Expansions[Set];
309 RecSet Elts;
310 Exp->expand(*this, Set, Elts);
311 EltVec.assign(Elts.begin(), Elts.end());
312 return &EltVec;
308313 }
309 } catch (const std::string &Error) {
310 throw TGError(Set->getLoc(), Error);
311314 }
312315
313316 // Set is not expandable.
4848
4949 #include "llvm/ADT/StringMap.h"
5050 #include "llvm/ADT/SetVector.h"
51 #include "llvm/Support/SourceMgr.h"
5152 #include
5253 #include
5354
7172
7273 /// apply - Apply this operator to Expr's arguments and insert the result
7374 /// in Elts.
74 virtual void apply(SetTheory&, DagInit *Expr, RecSet &Elts) =0;
75 virtual void apply(SetTheory&, DagInit *Expr, RecSet &Elts,
76 ArrayRef Loc) =0;
7577 };
7678
7779 /// Expander - A callback function that can transform a Record representing a
118120 void addOperator(StringRef Name, Operator*);
119121
120122 /// evaluate - Evaluate Expr and append the resulting set to Elts.
121 void evaluate(Init *Expr, RecSet &Elts);
123 void evaluate(Init *Expr, RecSet &Elts, ArrayRef Loc);
122124
123125 /// evaluate - Evaluate a sequence of Inits and append to Elts.
124126 template
125 void evaluate(Iter begin, Iter end, RecSet &Elts) {
127 void evaluate(Iter begin, Iter end, RecSet &Elts, ArrayRef Loc) {
126128 while (begin != end)
127 evaluate(*begin++, Elts);
129 evaluate(*begin++, Elts, Loc);
128130 }
129131
130132 /// expand - Expand a record into a set of elements if possible. Return a