Skip to content

Commit

Permalink
Fix bitwise operator evaluation when built-in consts/vars are involved (
Browse files Browse the repository at this point in the history
#424)

Closes #423
  • Loading branch information
mkruselj authored Dec 22, 2023
1 parent f1f43af commit 821b29c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compiler/ksp_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def __init__(self, lexinfo, name, indices=None, functions=None, alias_varref=Non
functions = []

# result := alias_varref
functions.append(FunctionDef(lexinfo, ID(lexinfo, 'get'),parameters=indices, return_value=ID(lexinfo, 'result'),
functions.append(FunctionDef(lexinfo, ID(lexinfo, 'get'), parameters=indices, return_value=ID(lexinfo, 'result'),
lines=[AssignStmt(lexinfo, VarRef(lexinfo, ID(lexinfo, 'result')), alias_varref)]))
# alias_varref := value_to_set
functions.append(FunctionDef(lexinfo, ID(lexinfo, 'set'), parameters=indices + [ID(lexinfo, 'value_to_set')], return_value=None,
Expand Down
6 changes: 5 additions & 1 deletion compiler/ksp_compiler_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def move_on_init_first(module):
module.blocks.insert(0, on_init)

def toint(i, bits=32):
' converts to a signed integer with bits bits '
''' converts to a signed integer with "bits" bits '''

i &= (1 << bits) - 1 # get last "bits" bits, as unsigned

if i & (1 << (bits - 1)): # is negative in N-bit 2's comp
Expand Down Expand Up @@ -128,6 +129,9 @@ def evaluate_expression(expr):
elif op == '#':
return a != b
elif op in ['.and.', '.or.', '.xor.']:
assert_numeric(a)
assert_numeric(b)

a, b = toint(a), toint(b)

if op == '.and.':
Expand Down

0 comments on commit 821b29c

Please sign in to comment.