forked from The-OpenROAD-Project/OpenSTA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FuncExpr.cc
418 lines (387 loc) · 9.48 KB
/
FuncExpr.cc
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// OpenSTA, Static Timing Analyzer
// Copyright (c) 2019, Parallax Software, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "Machine.hh"
#include "StringUtil.hh"
#include "Liberty.hh"
#include "Network.hh"
#include "FuncExpr.hh"
namespace sta {
FuncExpr *
FuncExpr::makePort(LibertyPort *port)
{
return new FuncExpr(op_port, nullptr, nullptr, port);
}
FuncExpr *
FuncExpr::makeNot(FuncExpr *expr)
{
return new FuncExpr(op_not, expr, nullptr, nullptr);
}
FuncExpr *
FuncExpr::makeAnd(FuncExpr *left,
FuncExpr *right)
{
return new FuncExpr(op_and, left, right, nullptr);
}
FuncExpr *
FuncExpr::makeOr(FuncExpr *left,
FuncExpr *right)
{
return new FuncExpr(op_or, left, right, nullptr);
}
FuncExpr *
FuncExpr::makeXor(FuncExpr *left,
FuncExpr *right)
{
return new FuncExpr(op_xor, left, right, nullptr);
}
FuncExpr *
FuncExpr::makeZero()
{
return new FuncExpr(op_zero, nullptr, nullptr, nullptr);
}
FuncExpr *
FuncExpr::makeOne()
{
return new FuncExpr(op_one, nullptr, nullptr, nullptr);
}
FuncExpr::FuncExpr(Operator op,
FuncExpr *left,
FuncExpr *right,
LibertyPort *port) :
op_(op),
left_(left),
right_(right),
port_(port)
{
}
void
FuncExpr::deleteSubexprs()
{
if (left_)
left_->deleteSubexprs();
if (right_)
right_->deleteSubexprs();
delete this;
}
FuncExpr *
FuncExpr::copy()
{
FuncExpr *left = left_ ? left_->copy() : nullptr;
FuncExpr *right = right_ ? right_->copy() : nullptr;
return new FuncExpr(op_, left, right, port_);
}
LibertyPort *
FuncExpr::port() const
{
if (op_ == op_port)
return port_;
else
return nullptr;
}
// Protect against null sub-expressions caused by unknown port refs.
TimingSense
FuncExpr::portTimingSense(const LibertyPort *port) const
{
TimingSense left_sense, right_sense;
switch (op_) {
case op_port:
if (port == port_)
return TimingSense::positive_unate;
else
return TimingSense::none;
case op_not:
if (left_) {
switch (left_->portTimingSense(port)) {
case TimingSense::positive_unate:
return TimingSense::negative_unate;
case TimingSense::negative_unate:
return TimingSense::positive_unate;
case TimingSense::non_unate:
return TimingSense::non_unate;
case TimingSense::none:
return TimingSense::none;
case TimingSense::unknown:
return TimingSense::unknown;
}
}
return TimingSense::unknown;
case op_or:
case op_and:
left_sense = TimingSense::unknown;
right_sense = TimingSense::unknown;
if (left_)
left_sense = left_->portTimingSense(port);
if (right_)
right_sense = right_->portTimingSense(port);
if (left_sense == right_sense)
return left_sense;
else if (left_sense == TimingSense::non_unate
|| right_sense == TimingSense::non_unate
|| (left_sense == TimingSense::positive_unate
&& right_sense == TimingSense::negative_unate)
|| (left_sense == TimingSense::negative_unate
&& right_sense == TimingSense::positive_unate))
return TimingSense::non_unate;
else if (left_sense == TimingSense::none
|| left_sense == TimingSense::unknown)
return right_sense;
else if (right_sense == TimingSense::none
|| right_sense == TimingSense::unknown)
return left_sense;
else
return TimingSense::unknown;
case op_xor:
left_sense = TimingSense::unknown;
right_sense = TimingSense::unknown;
if (left_)
left_sense = left_->portTimingSense(port);
if (right_)
right_sense = right_->portTimingSense(port);
if (left_sense == TimingSense::positive_unate
|| left_sense == TimingSense::negative_unate
|| left_sense == TimingSense::non_unate
|| right_sense == TimingSense::positive_unate
|| right_sense == TimingSense::negative_unate
|| right_sense == TimingSense::non_unate)
return TimingSense::non_unate;
else
return TimingSense::unknown;
case op_one:
return TimingSense::none;
case op_zero:
return TimingSense::none;
}
// Prevent warnings from lame compilers.
return TimingSense::unknown;
}
const char *
FuncExpr::asString() const
{
return asString(false);
}
const char *
FuncExpr::asString(bool with_parens) const
{
switch (op_) {
case op_port:
return port_->name();
case op_not: {
const char *left = left_->asString(true);
size_t left_length = strlen(left);
size_t length = left_length + 2;
char *result = makeTmpString(length);
char *ptr = result;
*ptr++ = '!';
strcpy(ptr, left);
return result;
}
case op_or:
return asStringSubexpr(with_parens, '+');
case op_and:
return asStringSubexpr(with_parens, '*');
case op_xor:
return asStringSubexpr(with_parens, '^');
case op_one:
return "1";
case op_zero:
return "0";
default:
return "?";
}
}
const char *
FuncExpr::asStringSubexpr(bool with_parens,
char op) const
{
const char *left = left_->asString(true);
const char *right = right_->asString(true);
size_t length = strlen(left) + 1 + strlen(right) + 1;
if (with_parens)
length += 2;
char *result = makeTmpString(length);
char *r = result;
if (with_parens)
*r++= '(';
stringAppend(r, left);
*r++ = op;
stringAppend(r, right);
if (with_parens)
*r++ = ')';
*r = '\0';
return result;
}
FuncExpr *
FuncExpr::bitSubExpr(int bit_offset)
{
switch (op_) {
case op_port:
if (port_->hasMembers()) {
if (port_->size() == 1) {
LibertyPort *port = port_->findLibertyMember(0);
return makePort(port);
}
else {
LibertyPort *port = port_->findLibertyMember(bit_offset);
return makePort(port);
}
}
else
// Always copy so the subexpr doesn't share memory.
return makePort(port_);
case op_not:
return makeNot(left_->bitSubExpr(bit_offset));
case op_or:
return makeOr(left_->bitSubExpr(bit_offset),
right_->bitSubExpr(bit_offset));
case op_and:
return makeAnd(left_->bitSubExpr(bit_offset),
right_->bitSubExpr(bit_offset));
case op_xor:
return makeXor(left_->bitSubExpr(bit_offset),
right_->bitSubExpr(bit_offset));
case op_one:
case op_zero:
return this;
}
// Prevent warnings from lame compilers.
return nullptr;
}
bool
FuncExpr::hasPort(const LibertyPort *port) const
{
switch (op_) {
case op_port:
return (port_ == port);
case op_not:
return left_ && left_->hasPort(port);
case op_or:
case op_and:
case op_xor:
return (left_ && left_->hasPort(port))
|| (right_ && right_->hasPort(port));
case op_one:
case op_zero:
return false;
}
// Prevent warnings from lame compilers.
return false;
}
bool
FuncExpr::checkSize(LibertyPort *port)
{
return checkSize(port->size());
}
bool
FuncExpr::checkSize(size_t size)
{
size_t port_size;
switch (op_) {
case op_port:
port_size = port_->size();
return !(port_size == size
|| port_size == 1);
case op_not:
return left_->checkSize(size);
case op_or:
case op_and:
case op_xor:
return left_->checkSize(size) || right_->checkSize(size);
case op_one:
case op_zero:
return false;
}
// Prevent warnings from lame compilers.
return false;
}
FuncExpr *
funcExprNot(FuncExpr *expr)
{
if (expr->op() == FuncExpr::op_not) {
FuncExpr *not_expr = expr->left();
delete expr;
return not_expr;
}
else
return FuncExpr::makeNot(expr);
}
////////////////////////////////////////////////////////////////
FuncExprPortIterator::FuncExprPortIterator(FuncExpr *expr)
{
findPorts(expr);
iter_.init(ports_);
}
void
FuncExprPortIterator::findPorts(FuncExpr *expr)
{
if (expr) {
if (expr->op() == FuncExpr::op_port)
ports_.insert(expr->port());
else {
findPorts(expr->left());
findPorts(expr->right());
}
}
}
bool
FuncExpr::equiv(const FuncExpr *expr1,
const FuncExpr *expr2)
{
if (expr1 == nullptr && expr2 == nullptr)
return true;
else if (expr1 != nullptr && expr2 != nullptr
&& expr1->op() == expr2->op()) {
switch (expr1->op()) {
case FuncExpr::op_port:
return LibertyPort::equiv(expr1->port(), expr2->port());
case FuncExpr::op_not:
return equiv(expr1->left(), expr2->left());
default:
return equiv(expr1->left(), expr2->left())
&& equiv(expr1->right(), expr2->right());
}
}
else
return false;
}
bool
FuncExpr::less(const FuncExpr *expr1,
const FuncExpr *expr2)
{
if (expr1 != nullptr && expr2 != nullptr) {
Operator op1 = expr1->op();
Operator op2 = expr2->op();
if (op1 == op2) {
switch (expr1->op()) {
case FuncExpr::op_port:
return LibertyPort::less(expr1->port(), expr2->port());
case FuncExpr::op_not:
return less(expr1->left(), expr2->left());
default:
if (equiv(expr1->left(), expr2->left()))
return less(expr1->right(), expr2->right());
else
return less(expr1->left(), expr2->left());
}
}
else
return op1 < op2;
}
else if (expr1 == nullptr && expr2 == nullptr)
return false;
else
return (expr1 == nullptr && expr2 != nullptr);
}
} // namespace