Skip to content

Commit

Permalink
Display multiple storage vars at the same key in the same order always.
Browse files Browse the repository at this point in the history
This makes diff easier to read. Also convert more tilde to matcher.
  • Loading branch information
palkeo committed Apr 22, 2020
1 parent 3c79ff2 commit 38df3c8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
53 changes: 29 additions & 24 deletions core/memloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .algebra import add_op, lt_op, le_op, CannotCompare, to_bytes, safe_gt_zero, safe_le_op
from utils.helpers import opcode, cached, before_after, contains, replace, is_array
import logging
from pano.matcher import match, Any

import sys

Expand All @@ -13,16 +14,16 @@

from .masks import find_mask

opcode = opcode

from .algebra import simplify, calc_max, add_ge_zero, minus_op, sub_op, flatten_adds, max_to_add
from .algebra import add_op, bits, mul_op, get_sign, safe_ge_zero, ge_zero, lt_op, safe_lt_op, safe_le_op
from .algebra import simplify_max, le_op, max_op, safe_max_op, safe_min_op, min_op, or_op, neg_mask_op, mask_op
from .algebra import apply_mask_to_storage, apply_mask, try_add, all_concrete
from .algebra import lt_op

def apply_mask_to_range(memloc, size, offset):
assert memloc ~ ('range', :range_pos, :range_len)
assert (m := match(memloc, ('range', ':range_pos', ':range_len')))
range_pos = m.range_pos
range_len = m.range_len

size_bytes, size_bits = to_bytes(size)
offset_bytes, offset_bits = to_bytes(offset)
Expand Down Expand Up @@ -57,14 +58,15 @@ def split_or(value):
if opcode(value) == 'mask_shl':

This comment has been minimized.

Copy link
@jbaryy708

jbaryy708 Jan 3, 2023

E777299FC265DD04793070EB944D35F9AC3DB76A

value = ('or', value)

assert value ~ ('or', *terms)
opcode_, *terms = value
assert opcode_ == 'or'


ret_rows = []

for row in terms:
if row ~ ('bool', :arg):
row = ('mask_shl', 8, 0, 0, ('bool', arg)) # does weird things if size == 1, in loops.activateSafeMode
if m := match(row, ('bool', ':arg')):
row = ('mask_shl', 8, 0, 0, ('bool', m.arg)) # does weird things if size == 1, in loops.activateSafeMode

if row == 'caller':
row = ('mask_shl', 160, 0, 0, 'caller') # does weird things if size == 1, in loops.activateSafeMode
Expand All @@ -73,8 +75,8 @@ def split_or(value):
row = ('mask_shl', 64, 0, 0, 'caller') # does weird things if size == 1, in loops.activateSafeMode


if row ~ ('mul', 1, :val):
row = val
if m := match(row, ('mul', 1, ':val')):
row = m.val

if opcode(row) == 'mask_shl' and all_concrete(row):
row = apply_mask(row[4] if row[4]<256 else 256, row[1], row[2], row[3])
Expand All @@ -84,34 +86,35 @@ def split_or(value):
shl = 0
row = ('mask_shl', size, offset, 0, row)

if row ~ ('mem', :mem_idx):
if opcode(mem_idx) != 'range':
mem_idx = ('range', mem_idx, 32)
if m := match(row, ('mem', ':mem_idx')):
if opcode(m.mem_idx) != 'range':
m.mem_idx = ('range', m.mem_idx, 32)

mem_begin = mem_idx[1]
mem_len = mem_idx[2]
mem_begin = m.mem_idx[1]
mem_len = m.mem_idx[2]
ret_rows.append((bits(mem_len), 0, row))
continue

if row ~ ('storage', :size, :off, :idx):
ret_rows.append((size, 0, row))
if m := match(row, ('storage', ':size', ':off', ':idx')):
ret_rows.append((m.size, 0, row))
continue

if opcode(row) != 'mask_shl':
return [(256,0, value)]

assert row ~ ('mask_shl', :size, :offset, :shl, :value)
assert opcode(row) == 'mask_shl'
_, size, offset, shl, value = row

stor_size = size
stor_offset = add_op(offset, shl)
shl = sub_op(shl, stor_offset)
if type(value) == int:
value = apply_mask(value, size, offset, shl)

elif value ~ ('mem', :idx) and add_op(offset, shl) == 0:
new_memloc = apply_mask_to_range(idx, size, offset)
elif (m := match(value, ('mem', ':idx'))) and add_op(offset, shl) == 0:
new_memloc = apply_mask_to_range(m.idx, size, offset)
value = ('mem', new_memloc)

else:
value = mask_op(value, size=size, offset=offset, shl=shl)

Expand All @@ -132,10 +135,12 @@ def split_or(value):
f_size, f_off, f_val = first
s_size, s_off, s_val = second

if f_off == 0 \
and s_off ~ ('add', 256, ('mul', -1, ('mask_shl', 253, 0, 3, ('add', 32, ('mul', -1, ('mask_shl', 5, 0, 0, f_size[4])))))):
assert s_size ~ ('mask_shl', _, _, _, ('add', 32, ('mul', -1, ...))), s_size
return ret_rows
try:
if f_off == 0 and s_off == ('add', 256, ('mul', -1, ('mask_shl', 253, 0, 3, ('add', 32, ('mul', -1, ('mask_shl', 5, 0, 0, f_size[4])))))):
assert match(s_size, ('mask_shl', Any, Any, Any, ('add', 32, ('mul', -1, ...))))
return ret_rows
except TypeError:
pass

try:
ret_rows.sort(key = lambda row: cmp_to_key(row[1])) # sort by offsets, descending
Expand Down Expand Up @@ -480,7 +485,7 @@ def fill_mem(exp, mem_idx, mem_val):

f = _fill_mem(exp, mem_idx, mem_val)
return f



def _fill_mem(exp, split, split_val):
Expand Down
33 changes: 17 additions & 16 deletions pano/sparser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding: tilde

from pano.prettify import pprint_trace, pretty_stor
from pano.matcher import match, Any

from utils.helpers import opcode, find_f, replace, replace_f, tuplify, replace_lines, get_op, hashable, to_exp2

Expand Down Expand Up @@ -30,11 +31,11 @@ def f(exp):
# don't follow this one when looking for loc
return None

elif exp ~ ('loc', :num):
return num
elif m := match(exp, ('loc', ':num')):
return m.num

elif exp ~ ('name', _, :num):
return num
elif m := match(exp, ('name', Any, ':num')):
return m.num

else:
for e in exp:
Expand All @@ -43,21 +44,21 @@ def f(exp):

return None

if exp ~ ('type', _, ('field', _, :m_idx)):
exp = m_idx

if exp ~ ('storage', _, _, :e):
exp = e
if m := match(exp, ('type', Any, ('field', Any, ':m_idx'))):
exp = m.m_idx

if exp ~ ('stor', _, _, :e):
exp = e
if m := match(exp, ('storage', Any, Any, ':e')):
exp = m.e

if exp ~ ('stor', :e):
exp = e
if m := match(exp, ('stor', Any, Any, ':e')):
exp = m.e

if m := match(exp, ('stor', ':e')):
exp = m.e

return f(exp)


def get_name_full(exp):
def f(exp):
if type(exp) != tuple:
Expand Down Expand Up @@ -204,12 +205,12 @@ def get_type(stordefs):

try:
sorted_keys = sorted(stordefs.keys())
except:
except Exception:
logger.warn('unusual storage location')
sorted_keys = stordefs.keys()

for loc in sorted_keys:
for l in stordefs[loc]:
for l in sorted(stordefs[loc]):
if (l ~ ('stor', int, int, ('loc', _))) or \
(l ~ ('stor', int, int, ('name', ...))):
pass
Expand All @@ -231,7 +232,7 @@ def get_type(stordefs):
break
else:
# all stor references are not arrays/maps, let's just print them out
for l in stordefs[loc]:
for l in sorted(stordefs[loc]):
name = get_name(l)

if name is None:
Expand Down
2 changes: 0 additions & 2 deletions panoramix.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,6 @@ def dec():
if len(contract.stor_defs) > 0:
print(f"{C.green}def {C.end}storage:")

storage = {}

for s in contract.stor_defs:
print(pretty_type(s))

Expand Down

0 comments on commit 38df3c8

Please sign in to comment.