hdl.ast: add Value.{as_signed,as_unsigned}.

Before this commit, there was no way to do so besides creating and
assigning an intermediate signal, which could not be extracted into
a helper function due to Module statefulness.

Fixes #292.
This commit is contained in:
whitequark 2020-02-06 18:27:55 +00:00
parent 9301e31b69
commit 27b47faf16
5 changed files with 51 additions and 0 deletions

View file

@ -426,6 +426,9 @@ class _RHSValueCompiler(_ValueCompiler):
if value.operator == "r^":
# Believe it or not, this is the fastest way to compute a sideways XOR in Python.
return f"(format({mask(arg)}, 'b').count('1') % 2)"
if value.operator in ("u", "s"):
# These operators don't change the bit pattern, only its interpretation.
return self(arg)
elif len(value.operands) == 2:
lhs, rhs = value.operands
lhs_mask = (1 << len(lhs)) - 1

View file

@ -454,6 +454,10 @@ class _RHSValueCompiler(_ValueCompiler):
def on_Operator_unary(self, value):
arg, = value.operands
if value.operator in ("u", "s"):
# These operators don't change the bit pattern, only its interpretation.
return self(arg)
arg_bits, arg_sign = arg.shape()
res_bits, res_sign = value.shape()
res = self.s.rtlil.wire(width=res_bits, src=src(value.src_loc))