-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathutil.cpp
More file actions
356 lines (300 loc) · 9.83 KB
/
Copy pathutil.cpp
File metadata and controls
356 lines (300 loc) · 9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Main authors:
* Mats Carlsson <mats.carlsson@ri.se>
* Roberto Castaneda Lozano <rcas@acm.org>
*
* This file is part of Unison, see http://unison-code.github.io
*
* Copyright (c) 2016, RISE SICS AB
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "util.hpp"
bool overlap(int x, int xw, int y, int yw) {
return (x >= 0) && (y >= 0) && (x + xw > y) && (y + yw > x);
}
vector<register_atom> extend(register_atom ra, int w) {
vector<register_atom> atoms;
for (int era = max(0, ra - w + 1); era <= ra; era++) atoms.push_back(era);
return atoms;
}
bool all_assigned(const IntVarArgs& x) {
for (int i = 0; i < x.size(); i++) if (!x.assigned()) return false;
return true;
}
bool holds(const BoolVar x) {
return x.assigned() && x.val();
}
string show(const int i) {
stringstream s;
s << i;
return s.str();
}
string show(const double d) {
stringstream s;
s << d;
return s.str();
}
string show(const string s) {
return s;
}
string show(const Temporand t) {
stringstream s;
s << t;
return s.str();
}
string show(const UnisonConstraintExpr e) {
return show(vector<string>{show((int)e.id), show(e.data), show(e.children)}, ", ");
}
string show(const PresolverBefore b) {
return show(vector<string>{show(b.p), show(b.q), show(b.d)}, ", ");
}
string show(const PresolverBeforeJSON b) {
return show(vector<string>{show(b.p), show(b.q), show(b.e)}, ", ");
}
string show(const PresolverDominates d) {
return show(vector<string>{show(d.o1), show(d.o2), show(d.ins),
show(d.temps)}, ", ");
}
string show(const PresolverInstrCond ic) {
return show(vector<string>{show(ic.o), show(ic.i), show(ic.q)}, ", ");
}
string show(const PresolverValuePrecedeChain pvc) {
return show(vector<string>{show(pvc.ts), show(pvc.rss)}, ", ");
}
string show(const PresolverInsnClass t) {
return show(vector<string>{show(t.insn), show(t.rclass)}, ", ");
}
string show(const PresolverInsn2Class2 t) {
return show(vector<string>{show(t.insn1), show(t.insn2), show(t.class1),
show(t.class2)}, ", ");
}
string show(const PresolverActiveTable t) {
return show(vector<string>{show(t.os), show(t.tuples)}, ", ");
}
string show(const PresolverCopyTmpTable t) {
return show(vector<string>{show(t.os), show(t.ps), show(t.tuples)}, ", ");
}
string show(const PresolverAcrossJSON x) {
return show(vector<string>{show(x.o), show(x.ras), show(x.as)}, ", ");
}
string show(const PresolverAcrossTuple x) {
return show(vector<string>{show(x.o), show(x.t), show(x.d)}, ", ");
}
string show(const PresolverAcrossItemJSON x) {
return show(vector<string>{show(x.t), show(x.e)}, ", ");
}
string show(const PresolverWCET x) {
return show(vector<string>{show(x.o), show(x.i), show(x.d)}, ", ");
}
string show(const PresolverSetAcross x) {
return show(vector<string>{show(x.o), show(x.ras), show(x.tsets)}, ", ");
}
string show(const PresolverPrecedence x) {
return show(vector<string>{show(x.i), show(x.j), show(x.n), show(x.d)}, ", ");
}
string show(const PrecedenceEdge x) {
return show(vector<string>{show(x.i), show(x.j), show(x.n)}, ", ");
}
string emit_json(const int i) {
stringstream s;
s << i;
return s.str();
}
string emit_json(const bool b) {
stringstream s;
s << (b ? "true" : "false");
return s.str();
}
string emit_json(const double d) {
stringstream s;
s.precision(std::numeric_limits<double>::max_digits10);
s << fixed << d;
return s.str();
}
string emit_json(const string s) {
stringstream ss;
ss << "\"" << s << "\"";
return ss.str();
}
string emit_json(const PresolverActiveTable x) {
return show(vector<string>{emit_json(x.os), emit_json(x.tuples)}, ", ");
}
string emit_json(const PresolverCopyTmpTable x) {
return show(vector<string>{emit_json(x.os), emit_json(x.ps),
emit_json(x.tuples)}, ", ");
}
string emit_json(const PresolverBeforeJSON x) {
return show(vector<string>{emit_json(x.p), emit_json(x.q),
emit_json(x.e)}, ", ");
}
string emit_json(const PresolverAcrossJSON x) {
return show(vector<string>{emit_json(x.o), emit_json(x.ras),
emit_json(x.as)}, ", ");
}
string emit_json(const PresolverAcrossItemJSON x) {
return show(vector<string>{emit_json(x.t), emit_json(x.e)}, ", ");
}
string emit_json(const PresolverSetAcross x) {
return show(vector<string>{emit_json(x.o), emit_json(x.ras),
emit_json(x.tsets)}, ", ");
}
string emit_json(const PresolverDominates x) {
return show(vector<string>{emit_json(x.o1), emit_json(x.o2),
emit_json(x.ins), emit_json(x.temps)}, ", ");
}
string emit_json(const PresolverInstrCond x) {
return show(vector<string>{emit_json(x.o), emit_json(x.i),
emit_json(x.q)}, ", ");
}
string emit_json(const PresolverValuePrecedeChain pvc) {
return show(vector<string>{emit_json(pvc.ts), emit_json(pvc.rss)}, ", ");
}
string emit_json(const PresolverWCET wcet) {
return show(vector<string>{emit_json(wcet.o), emit_json(wcet.i), emit_json(wcet.d)}, ", ");
}
string emit_json(const UnisonConstraintExpr e) {
vector<string> elements;
stringstream ids;
ids << e.id;
elements.push_back(ids.str());
for (int d : e.data)
elements.push_back(show(d));
for (UnisonConstraintExpr e0 : e.children)
elements.push_back(emit_json(e0));
return show(elements, ", ");
}
string show_class(register_class rc, const Parameters * p) {
stringstream s;
s << "rc" << rc << ":" << p->classname[rc];
return s.str();
}
string show_space(register_space rs, const Parameters * p) {
stringstream s;
s << "rs" << rs << ":" << p->spacename[rs];
return s.str();
}
string show_register(register_atom ra, int w, const Parameters * p) {
string raname;
if (ra == NULL_REGISTER) raname = "null";
else {
string fa = p->atomname[ra];
string la = p->atomname[ra + w - 1];
raname = (fa == la ? fa : fa + "-" + la);
}
return raname;
}
string show_instruction(instruction i, operation o, const Parameters * p) {
if (i == NULL_INSTRUCTION) return p->insname[i];
switch (p->type[o]) {
case IN: return "(in)";
case OUT: return "(out)";
case KILL: return "(kill)";
case DEFINE: return "(define)";
case COMBINE: return "(combine)";
case LOW: return "(low)";
case HIGH: return "(high)";
case SPLIT2: return "(split2)";
case SPLIT4: return "(split4)";
default: return p->insname[i];
}
}
string showInstructions(operation o, const IntVar& i, const Parameters * p) {
vector<string> ops;
for (IntVarValues ii(i); ii(); ++ii)
ops.push_back(show_instruction(p->instructions[o][ii.val()], o, p));
return showDomain(ops);
}
int gcd(int a, int b) {
int temp;
while( b != 0) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
void copy_domain(Home h, IntVar s, IntVar d) {
Int::ViewRanges<Int::IntView> sr(s);
Int::IntView dv(d);
dv.narrow_r(h, sr);
}
void copy_domain(Home h, BoolVar s, BoolVar d) {
Int::ViewRanges<Int::BoolView> sr(s);
Int::BoolView dv(d);
dv.narrow_r(h, sr);
}
string init(string s) {
return s.substr(0, s.size() - 1);
}
bool in_block(PresolverActiveTable & ct, block b, const Parameters * input) {
for (operation o : ct.os) if (input->oblock[o] != b) return false;
return true;
}
bool in_block(PresolverCopyTmpTable & ctt, block b, const Parameters * input) {
for (operation o : ctt.os) if (input->oblock[o] != b) return false;
for (operand p : ctt.ps) if (input->pb[p] != b) return false;
return true;
}
bool in_block(PresolverBeforeJSON & bf, block b, const Parameters * input) {
if (input->pb[bf.p] != b) return false;
if (input->pb[bf.q] != b) return false;
return in_block(bf.e, b, input);
}
bool in_block(UnisonConstraintExpr & e, block b, const Parameters * input) {
switch (e.id) {
case OR_EXPR:
case AND_EXPR:
case XOR_EXPR:
case IMPLIES_EXPR:
case NOT_EXPR:
for (UnisonConstraintExpr e0 : e.children)
if (!in_block(e0, b, input)) return false;
return true;
case ACTIVE_EXPR:
case IMPLEMENTS_EXPR:
case DISTANCE_EXPR:
return (input->oblock[e.data[0]] == b);
case CONNECTS_EXPR:
case SHARE_EXPR:
case OPERAND_OVERLAP_EXPR:
case ALLOCATED_EXPR:
return (input->pb[e.data[0]] == b);
case TEMPORARY_OVERLAP_EXPR:
case CALLER_SAVED_EXPR:
return (input->tb[e.data[0]] == b);
case ALIGNED_EXPR:
return (input->pb[e.data[0]] == b);
}
GECODE_NEVER;
return true;
}
vector<int> var_vector(const IntVarArgs & v) {
vector<int> x;
for (int i = 0; i < v.size(); i++) {
x.push_back(v[i].val());
}
return x;
}