From bc33fac52d05ef8c80c4eb152b361e6ce2ba3216 Mon Sep 17 00:00:00 2001 From: Erik Carstensen Date: Thu, 28 Sep 2023 15:05:11 +0200 Subject: [PATCH] Add porting rule for `assert 0;` This tends to be fairly common in DML 1.2 code --- py/dml/ctree.py | 13 +++++++++---- py/dml/messages.py | 5 +++++ py/port_dml.py | 20 ++++++++++++++++++-- test/1.2/misc/porting.dml | 7 +++++++ test/1.4/misc/porting.dml | 7 +++++++ test/tests.py | 1 + 6 files changed, 47 insertions(+), 6 deletions(-) diff --git a/py/dml/ctree.py b/py/dml/ctree.py index 5d9fad091..d9f42b2ba 100644 --- a/py/dml/ctree.py +++ b/py/dml/ctree.py @@ -984,10 +984,15 @@ def as_bool(e): if isinstance(t, TBool): return e elif t.is_int and t.bits == 1: - if logging.show_porting and (isinstance(e, NodeRef) - or isinstance(e, LocalVariable)): - report(PBITNEQ(dmlparse.start_site(e.site), - dmlparse.end_site(e.site))) + if logging.show_porting: + if e.constant: + report(PZEROCOND(dmlparse.start_site(e.site), + dmlparse.end_site(e.site), + 'true' if e.value else 'false')) + elif (isinstance(e, NodeRef) + or isinstance(e, LocalVariable)): + report(PBITNEQ(dmlparse.start_site(e.site), + dmlparse.end_site(e.site))) return mkFlag(e.site, e) elif isinstance(t, TPtr): return mkNotEquals(e.site, e, diff --git a/py/dml/messages.py b/py/dml/messages.py index 22ad1c369..2d256279c 100644 --- a/py/dml/messages.py +++ b/py/dml/messages.py @@ -2346,6 +2346,11 @@ class PBITNEQ(PortingMessage): field values are 64 bit, and thus require an explicit `!= 0`""" fmt = "" +class PZEROCOND(PortingMessage): + """DML 1.2 permits using the literals `0` and `1` as booleans. In DML 1.4, + this should be replaced with `false` and `true`, respectively""" + fmt = "" + class PVAL(PortingMessage): """The value of a `register`, `field` or `attribute` object, and the interface struct of a `interface` object, is now diff --git a/py/port_dml.py b/py/port_dml.py index e36468f35..cba59cf4c 100644 --- a/py/port_dml.py +++ b/py/port_dml.py @@ -802,6 +802,10 @@ def apply(self, f): start_offs = self.offset(f) [end_site] = self.params end_offs = self.offset(f, end_site) + self.insert_zero_compare(f, start_offs, end_offs) + + @staticmethod + def insert_zero_compare(f, start_offs, end_offs): (pad, token, kind) = next(f.read_tokens(end_offs)) from dml import dmllex14 if kind in ('ID', 'RBRACKET', 'THIS', 'REGISTER', 'SIGNED', @@ -817,6 +821,17 @@ def apply(self, f): # f => f != 0 f.edit(end_offs, 0, ' != 0') +class PZEROCOND(Transformation): + def apply(self, f): + start_offs = self.offset(f) + [end_site, value] = self.params + end_offs = self.offset(f, end_site) + (pad, token, kind) = next(f.read_tokens(end_offs)) + if end_offs == start_offs and kind == 'ICONST': + f.edit(start_offs, token, value) + else: + PBITNEQ.insert_zero_compare(f, start_offs, end_offs) + class PVAL(Transformation): # must happen after PBITNEQ phase = 1 @@ -1005,6 +1020,7 @@ def report(f, dest): 'PIMPORT_DML12COMPAT': PIMPORT_DML12COMPAT, 'PCHANGE_INARGS': PCHANGE_INARGS, 'PBITNEQ': PBITNEQ, + 'PZEROCOND': PZEROCOND, 'PVAL': PVAL, 'PNODOLLAR': PNODOLLAR, 'PDOLLAR_QUALIFY': Replace, @@ -1087,7 +1103,7 @@ def main(argv): already_added.add(key) t = tags[tag](loc, ast.literal_eval(params)) transformations.setdefault(t.phase, []).append((t, lineno, line)) - except: + except Exception: sys.stderr.write("Unexpected error on this porting tag:\n") sys.stderr.write(line) sys.stderr.write("%s:%d: found here\n" % (tagfilename, lineno)) @@ -1114,7 +1130,7 @@ def main(argv): loc, '' if e.tag is None else ' ' + e.tag, e)) sys.stderr.write("%s:%d: found here\n" % (tagfilename, lineno)) errors += 1 - except: + except Exception: sys.stderr.write("Unexpected error on this porting tag:\n") sys.stderr.write(line) sys.stderr.write("%s:%d: found here\n" % (tagfilename, lineno)) diff --git a/test/1.2/misc/porting.dml b/test/1.2/misc/porting.dml index edf2fc643..6f60c2e0a 100644 --- a/test/1.2/misc/porting.dml +++ b/test/1.2/misc/porting.dml @@ -393,6 +393,8 @@ event ev2 is (evt) { parameter timebase = "seconds"; } +parameter zero = 0; + method init() { local int1 i1; @@ -428,4 +430,9 @@ method init() { $c.signal.signal_raise(); } } + if (1) { + assert $zero; + } else { + assert 0; + } } diff --git a/test/1.4/misc/porting.dml b/test/1.4/misc/porting.dml index ced145384..545fac410 100644 --- a/test/1.4/misc/porting.dml +++ b/test/1.4/misc/porting.dml @@ -418,6 +418,8 @@ template evt is event { event ev2 is (custom_time_event, evt) { } +param zero = 0; + method init() { local uint1 i1; @@ -455,4 +457,9 @@ method init() { c.signal.signal_raise(); } } + if (true) { + assert zero != 0; + } else { + assert false; + } } diff --git a/test/tests.py b/test/tests.py index cc0fe7d02..1851a550b 100644 --- a/test/tests.py +++ b/test/tests.py @@ -1388,6 +1388,7 @@ def test(self): 'PABSTRACT_TEMPLATE', 'PCHANGE_INARGS', 'PBITNEQ', + 'PZEROCOND', 'PVAL', 'PNODOLLAR', 'PDOLLAR_QUALIFY',