Skip to content

Commit bc33fac

Browse files
committed
Add porting rule for assert 0;
This tends to be fairly common in DML 1.2 code
1 parent cd3368b commit bc33fac

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed

py/dml/ctree.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -984,10 +984,15 @@ def as_bool(e):
984984
if isinstance(t, TBool):
985985
return e
986986
elif t.is_int and t.bits == 1:
987-
if logging.show_porting and (isinstance(e, NodeRef)
988-
or isinstance(e, LocalVariable)):
989-
report(PBITNEQ(dmlparse.start_site(e.site),
990-
dmlparse.end_site(e.site)))
987+
if logging.show_porting:
988+
if e.constant:
989+
report(PZEROCOND(dmlparse.start_site(e.site),
990+
dmlparse.end_site(e.site),
991+
'true' if e.value else 'false'))
992+
elif (isinstance(e, NodeRef)
993+
or isinstance(e, LocalVariable)):
994+
report(PBITNEQ(dmlparse.start_site(e.site),
995+
dmlparse.end_site(e.site)))
991996
return mkFlag(e.site, e)
992997
elif isinstance(t, TPtr):
993998
return mkNotEquals(e.site, e,

py/dml/messages.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,11 @@ class PBITNEQ(PortingMessage):
23462346
field values are 64 bit, and thus require an explicit `!= 0`"""
23472347
fmt = ""
23482348

2349+
class PZEROCOND(PortingMessage):
2350+
"""DML 1.2 permits using the literals `0` and `1` as booleans. In DML 1.4,
2351+
this should be replaced with `false` and `true`, respectively"""
2352+
fmt = ""
2353+
23492354
class PVAL(PortingMessage):
23502355
"""The value of a `register`, `field` or `attribute`
23512356
object, and the interface struct of a `interface` object, is now

py/port_dml.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,10 @@ def apply(self, f):
802802
start_offs = self.offset(f)
803803
[end_site] = self.params
804804
end_offs = self.offset(f, end_site)
805+
self.insert_zero_compare(f, start_offs, end_offs)
806+
807+
@staticmethod
808+
def insert_zero_compare(f, start_offs, end_offs):
805809
(pad, token, kind) = next(f.read_tokens(end_offs))
806810
from dml import dmllex14
807811
if kind in ('ID', 'RBRACKET', 'THIS', 'REGISTER', 'SIGNED',
@@ -817,6 +821,17 @@ def apply(self, f):
817821
# f => f != 0
818822
f.edit(end_offs, 0, ' != 0')
819823

824+
class PZEROCOND(Transformation):
825+
def apply(self, f):
826+
start_offs = self.offset(f)
827+
[end_site, value] = self.params
828+
end_offs = self.offset(f, end_site)
829+
(pad, token, kind) = next(f.read_tokens(end_offs))
830+
if end_offs == start_offs and kind == 'ICONST':
831+
f.edit(start_offs, token, value)
832+
else:
833+
PBITNEQ.insert_zero_compare(f, start_offs, end_offs)
834+
820835
class PVAL(Transformation):
821836
# must happen after PBITNEQ
822837
phase = 1
@@ -1005,6 +1020,7 @@ def report(f, dest):
10051020
'PIMPORT_DML12COMPAT': PIMPORT_DML12COMPAT,
10061021
'PCHANGE_INARGS': PCHANGE_INARGS,
10071022
'PBITNEQ': PBITNEQ,
1023+
'PZEROCOND': PZEROCOND,
10081024
'PVAL': PVAL,
10091025
'PNODOLLAR': PNODOLLAR,
10101026
'PDOLLAR_QUALIFY': Replace,
@@ -1087,7 +1103,7 @@ def main(argv):
10871103
already_added.add(key)
10881104
t = tags[tag](loc, ast.literal_eval(params))
10891105
transformations.setdefault(t.phase, []).append((t, lineno, line))
1090-
except:
1106+
except Exception:
10911107
sys.stderr.write("Unexpected error on this porting tag:\n")
10921108
sys.stderr.write(line)
10931109
sys.stderr.write("%s:%d: found here\n" % (tagfilename, lineno))
@@ -1114,7 +1130,7 @@ def main(argv):
11141130
loc, '' if e.tag is None else ' ' + e.tag, e))
11151131
sys.stderr.write("%s:%d: found here\n" % (tagfilename, lineno))
11161132
errors += 1
1117-
except:
1133+
except Exception:
11181134
sys.stderr.write("Unexpected error on this porting tag:\n")
11191135
sys.stderr.write(line)
11201136
sys.stderr.write("%s:%d: found here\n" % (tagfilename, lineno))

test/1.2/misc/porting.dml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ event ev2 is (evt) {
393393
parameter timebase = "seconds";
394394
}
395395

396+
parameter zero = 0;
397+
396398
method init() {
397399
local int1 i1;
398400

@@ -428,4 +430,9 @@ method init() {
428430
$c.signal.signal_raise();
429431
}
430432
}
433+
if (1) {
434+
assert $zero;
435+
} else {
436+
assert 0;
437+
}
431438
}

test/1.4/misc/porting.dml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ template evt is event {
418418
event ev2 is (custom_time_event, evt) {
419419
}
420420

421+
param zero = 0;
422+
421423
method init() {
422424
local uint1 i1;
423425

@@ -455,4 +457,9 @@ method init() {
455457
c.signal.signal_raise();
456458
}
457459
}
460+
if (true) {
461+
assert zero != 0;
462+
} else {
463+
assert false;
464+
}
458465
}

test/tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,7 @@ def test(self):
13881388
'PABSTRACT_TEMPLATE',
13891389
'PCHANGE_INARGS',
13901390
'PBITNEQ',
1391+
'PZEROCOND',
13911392
'PVAL',
13921393
'PNODOLLAR',
13931394
'PDOLLAR_QUALIFY',

0 commit comments

Comments
 (0)