Skip to content

Commit

Permalink
Prepare old greedy algorithm for testing and debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcere committed Jan 16, 2025
1 parent bd1ab12 commit 63a46dd
Show file tree
Hide file tree
Showing 3 changed files with 343 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/greedy/greedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
var_instr_map_T = Dict[var_T, instr_T]
opid_instr_map_T = Dict[var_T, instr_T]

VERBOSE = 0
VERBOSE = 3


def get_ops_map(instructions: List[Dict[str, Any]], op: id_T) -> Dict[var_T, id_T]:
Expand Down Expand Up @@ -1715,10 +1715,11 @@ def greedy_from_json(json_data: Dict[str, Any], verb=False) -> Tuple[
# print(name, encoding._b0, encoding._b0)
error = 0
except Exception as e:
# print(e)
if VERBOSE > 0:
print(e)
# print(json_data)
# _, _, tb = sys.exc_info()
# traceback.print_tb(tb)
_, _, tb = sys.exc_info()
traceback.print_tb(tb)
# print("Error")
res = None
resids = None
Expand Down Expand Up @@ -1760,6 +1761,13 @@ def greedy_standalone(sms: Dict) -> Tuple[str, float, List[str]]:
return optimization_outcome, usage_stop.ru_utime + usage_stop.ru_stime - usage_start.ru_utime - usage_start.ru_stime, seq_ids


def greedy_from_file(filename: str):
with open(filename, "r") as f:
sfs = json.load(f)
outcome, time, ids = greedy_standalone(sfs)
return sfs, ids, outcome


if __name__ == "__main__":
with open(sys.argv[1]) as f:
json_read = json.load(f)
Expand All @@ -1774,7 +1782,7 @@ def greedy_standalone(sms: Dict) -> Tuple[str, float, List[str]]:
name = name[p + 1:]

json_info, encod, rs, rsids, error = greedy_from_json(json_read) # ,True) if verbose
print("Solution", rsids)
print(f"Solution {len(rsids)} instrs: ", rsids)

# if error == 0:
# print(name, "m:", minst, "g:", len(rs), "e:", error)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
{
"original_instrs": "",
"yul_expressions": "c35 = push[0xffffffffffffffff]\nc34 = push[0x40]\nc33 = push[0x20]\nc32 = push[0x1f]\nv84_f9_0 = not(c32)\nv85_f9_0 = add(v20,c32)\nv86_f9_0 = and(v85_f9_0,v84_f9_0)\nv54 = add(v86_f9_0,c33)\nv50_f8_0 = mload(c34)\nv84_f9_1 = not(c32)\nv85_f9_1 = add(v54,c32)\nv86_f9_1 = and(v85_f9_1,v84_f9_1)\nv72 = add(v50_f8_0,v86_f9_1)\nv73 = lt(v72,v50_f8_0)\nv74 = gt(v72,c35)\nv75 = or(v74,v73)\n7 = PUSH [tag]\nJUMPI(7,v75)",
"src_ws": [
"v20",
"v21",
"v63"
],
"tgt_ws": [
"7",
"v75",
"v72",
"v50_f8_0",
"v20",
"v21",
"v63"
],
"user_instrs": [
{
"id": "PUSH_0",
"opcode": "60",
"disasm": "PUSH",
"inpt_sk": [],
"value": [
18446744073709551615
],
"outpt_sk": [
"c35"
],
"gas": 3,
"commutative": false,
"push": true,
"storage": false,
"size": 9
},
{
"id": "PUSH_1",
"opcode": "60",
"disasm": "PUSH",
"inpt_sk": [],
"value": [
64
],
"outpt_sk": [
"c34"
],
"gas": 3,
"commutative": false,
"push": true,
"storage": false,
"size": 2
},
{
"id": "PUSH_2",
"opcode": "60",
"disasm": "PUSH",
"inpt_sk": [],
"value": [
32
],
"outpt_sk": [
"c33"
],
"gas": 3,
"commutative": false,
"push": true,
"storage": false,
"size": 2
},
{
"id": "PUSH_3",
"opcode": "60",
"disasm": "PUSH",
"inpt_sk": [],
"value": [
31
],
"outpt_sk": [
"c32"
],
"gas": 3,
"commutative": false,
"push": true,
"storage": false,
"size": 2
},
{
"id": "NOT_0",
"opcode": "19",
"disasm": "NOT",
"inpt_sk": [
"c32"
],
"outpt_sk": [
"v84_f9_0"
],
"gas": 3,
"commutative": false,
"push": false,
"storage": false,
"size": 1
},
{
"id": "ADD_0",
"opcode": "01",
"disasm": "ADD",
"inpt_sk": [
"v20",
"c32"
],
"outpt_sk": [
"v85_f9_0"
],
"gas": 3,
"commutative": true,
"push": false,
"storage": false,
"size": 1
},
{
"id": "AND_0",
"opcode": "16",
"disasm": "AND",
"inpt_sk": [
"v85_f9_0",
"v84_f9_0"
],
"outpt_sk": [
"v86_f9_0"
],
"gas": 3,
"commutative": true,
"push": false,
"storage": false,
"size": 1
},
{
"id": "ADD_1",
"opcode": "01",
"disasm": "ADD",
"inpt_sk": [
"v86_f9_0",
"c33"
],
"outpt_sk": [
"v54"
],
"gas": 3,
"commutative": true,
"push": false,
"storage": false,
"size": 1
},
{
"id": "MLOAD_0",
"opcode": "51",
"disasm": "MLOAD",
"inpt_sk": [
"c34"
],
"outpt_sk": [
"v50_f8_0"
],
"gas": 3,
"commutative": false,
"push": false,
"storage": false,
"size": 1
},
{
"id": "ADD_2",
"opcode": "01",
"disasm": "ADD",
"inpt_sk": [
"v54",
"c32"
],
"outpt_sk": [
"v85_f9_1"
],
"gas": 3,
"commutative": true,
"push": false,
"storage": false,
"size": 1
},
{
"id": "AND_1",
"opcode": "16",
"disasm": "AND",
"inpt_sk": [
"v85_f9_1",
"v84_f9_0"
],
"outpt_sk": [
"v86_f9_1"
],
"gas": 3,
"commutative": true,
"push": false,
"storage": false,
"size": 1
},
{
"id": "ADD_3",
"opcode": "01",
"disasm": "ADD",
"inpt_sk": [
"v50_f8_0",
"v86_f9_1"
],
"outpt_sk": [
"v72"
],
"gas": 3,
"commutative": true,
"push": false,
"storage": false,
"size": 1
},
{
"id": "LT_0",
"opcode": "10",
"disasm": "LT",
"inpt_sk": [
"v72",
"v50_f8_0"
],
"outpt_sk": [
"v73"
],
"gas": 3,
"commutative": false,
"push": false,
"storage": false,
"size": 1
},
{
"id": "GT_0",
"opcode": "11",
"disasm": "GT",
"inpt_sk": [
"v72",
"c35"
],
"outpt_sk": [
"v74"
],
"gas": 3,
"commutative": false,
"push": false,
"storage": false,
"size": 1
},
{
"id": "OR_0",
"opcode": "17",
"disasm": "OR",
"inpt_sk": [
"v74",
"v73"
],
"outpt_sk": [
"v75"
],
"gas": 3,
"commutative": true,
"push": false,
"storage": false,
"size": 1
},
{
"id": "PUSH [TAG]_0",
"opcode": "00",
"disasm": "PUSH [TAG]",
"inpt_sk": [],
"outpt_sk": [
"7"
],
"gas": 3,
"commutative": false,
"push": false,
"storage": false,
"size": 3
}
],
"variables": [
"7",
"v73",
"v75",
"v74",
"v54",
"v85_f9_0",
"v72",
"v20",
"c35",
"v84_f9_1",
"c32",
"v85_f9_1",
"v86_f9_1",
"v86_f9_0",
"v50_f8_0",
"c34",
"c33",
"v84_f9_0"
],
"memory_dependences": [],
"storage_dependences": [],
"init_progr_len": 0,
"max_progr_len": 0,
"min_length_instrs": 0,
"min_length_bounds": 0,
"min_length": 0,
"rules": ""
}
15 changes: 15 additions & 0 deletions tests/test_greedy_old.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import glob

import pytest
from typing import Dict
from parser.cfg_block import CFGBlock
from parser.cfg_instruction import build_instr_spec, build_verbatim_spec, build_push_spec, CFGInstruction
from greedy.greedy import SMSgreedy, greedy_from_file
import json


class TestGreedyOld:

def test_array_allocation(self):
_, _, error = greedy_from_file("greedy_old/array_allocation_size_t_bytes_memory_ptr_Block2_split_0.json")
assert error != "error", "Falla test"

0 comments on commit 63a46dd

Please sign in to comment.