一種自動化生成z3變量的方法 (基於Python)

from z3 import *


var_allocate={}


def IRtoZ3expr(var): # if var is number , return a number , else return a z3expr
    x = str(var)
    if ("0x" in x):
        return int(x, 16)
    else:
        if (x not in var_allocate):
            strcmd = "%s = Int('%s')" % (x, x)
            exec strcmd
            var_allocate["%s" % x] = eval(x)
            return eval(x)
        else:
            return var_allocate["%s" % x]
            1


a = IRtoZ3expr('a')
b = IRtoZ3expr('a')
c = IRtoZ3expr('a')
print a.__hash__()
print b.__hash__()

print c.__hash__()


from z3 import *

var_allocate={}

def IRtoZ3expr(var): # if var is number , return a number , else return a z3expr
    x = str(var)
    if ("0x" in x):
        return int(x, 16)
    else:
        if (x not in var_allocate):
            strcmd = "%s = Int('%s')" % (x, x)
            exec strcmd
            var_allocate["%s" % x] = eval(x)
            return eval(x)
        else:
            return var_allocate["%s" % x]
            1

a = IRtoZ3expr('a')
b = IRtoZ3expr('a')
c = IRtoZ3expr('a')
print a.__hash__()
print b.__hash__()
print c.__hash__()

輸出 的 變量hash值相同,證明我們生成名字爲 a 的 Int 值是同一個,沒有被覆蓋!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章