Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add porting rule for assert 0; #212

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions py/dml/ctree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions py/dml/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions py/port_dml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions test/1.2/misc/porting.dml
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ event ev2 is (evt) {
parameter timebase = "seconds";
}

parameter zero = 0;

method init() {
local int1 i1;

Expand Down Expand Up @@ -428,4 +430,9 @@ method init() {
$c.signal.signal_raise();
}
}
if (1) {
assert $zero;
Copy link
Contributor

@lwaern-intel lwaern-intel Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since PZEROCOND covers both assert 0; and assert 1;, test assert 1; too. Also, because of that, you may want to consider renaming it to PICONSTCOND -- as in, Integer Constant Condition.

} else {
assert 0;
}
}
7 changes: 7 additions & 0 deletions test/1.4/misc/porting.dml
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ template evt is event {
event ev2 is (custom_time_event, evt) {
}

param zero = 0;

method init() {
local uint1 i1;

Expand Down Expand Up @@ -455,4 +457,9 @@ method init() {
c.signal.signal_raise();
}
}
if (true) {
assert zero != 0;
} else {
assert false;
}
}
1 change: 1 addition & 0 deletions test/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,7 @@ def test(self):
'PABSTRACT_TEMPLATE',
'PCHANGE_INARGS',
'PBITNEQ',
'PZEROCOND',
'PVAL',
'PNODOLLAR',
'PDOLLAR_QUALIFY',
Expand Down