Skip to content

Commit

Permalink
adding is_loop
Browse files Browse the repository at this point in the history
  • Loading branch information
PetarMax committed Sep 25, 2024
1 parent 8115309 commit 465966a
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions kevm-pyk/src/kevm_pyk/kevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
from pyk.kast.manip import abstract_term_safely, flatten_label, set_cell
from pyk.kast.pretty import paren
from pyk.kcfg.kcfg import Step
from pyk.kcfg.semantics import KCFGSemantics
from pyk.kcfg.semantics import DefaultSemantics
from pyk.kcfg.show import NodePrinter
from pyk.ktool.kprove import KProve
from pyk.ktool.krun import KRun
from pyk.prelude.bytes import BYTES, pretty_bytes
from pyk.prelude.kbool import notBool
from pyk.prelude.kint import INT, eqInt, gtInt, intToken, ltInt
from pyk.prelude.kint import INT, gtInt, intToken, ltInt
from pyk.prelude.ml import mlEqualsFalse, mlEqualsTrue
from pyk.prelude.string import stringToken
from pyk.prelude.utils import token
Expand All @@ -50,7 +49,7 @@
# KEVM class


class KEVMSemantics(KCFGSemantics):
class KEVMSemantics(DefaultSemantics):
auto_abstract_gas: bool
allow_symbolic_program: bool
_cached_subst: Subst | None
Expand Down Expand Up @@ -98,6 +97,12 @@ def is_terminal(self, cterm: CTerm) -> bool:

return False

def is_loop(self, cterm: CTerm) -> bool:
jumpi_pattern = KEVM.jumpi_applied(KVariable('###PCOUNT'), KVariable('###COND'))
pc_next_pattern = KEVM.pc_applied(KEVM.jumpi())
branch_pattern = KSequence([jumpi_pattern, pc_next_pattern, KEVM.sharp_execute(), KVariable('###CONTINUATION')])
return branch_pattern.match(cterm.cell('K_CELL')) is not None

def same_loop(self, cterm1: CTerm, cterm2: CTerm) -> bool:
# In the same program, at the same calldepth, at the same program counter
for cell in ['PC_CELL', 'CALLDEPTH_CELL', 'PROGRAM_CELL']:
Expand All @@ -116,20 +121,6 @@ def same_loop(self, cterm1: CTerm, cterm2: CTerm) -> bool:
return True
return False

def extract_branches(self, cterm: CTerm) -> list[KInner]:
k_cell = cterm.cell('K_CELL')
jumpi_pattern = KEVM.jumpi_applied(KVariable('###PCOUNT'), KVariable('###COND'))
pc_next_pattern = KEVM.pc_applied(KEVM.jumpi())
branch_pattern = KSequence([jumpi_pattern, pc_next_pattern, KEVM.sharp_execute(), KVariable('###CONTINUATION')])
if subst := branch_pattern.match(k_cell):
cond = subst['###COND']
if cond_subst := KEVM.bool_2_word(KVariable('###BOOL_2_WORD')).match(cond):
cond = cond_subst['###BOOL_2_WORD']
else:
cond = eqInt(cond, intToken(0))
return [mlEqualsTrue(cond), mlEqualsTrue(notBool(cond))]
return []

def abstract_node(self, cterm: CTerm) -> CTerm:
if not self.auto_abstract_gas:
return cterm
Expand Down

0 comments on commit 465966a

Please sign in to comment.