diff options
| author | Camille Mougey <camille.mougey@cea.fr> | 2014-12-14 18:25:52 +0100 |
|---|---|---|
| committer | Camille Mougey <camille.mougey@cea.fr> | 2014-12-14 18:25:52 +0100 |
| commit | 9c518f0edd4d0dbc1a9be7084015d53673b488fc (patch) | |
| tree | 3d16c611cd5c778625312b74b22590e3626345c3 | |
| parent | 90180500af9736e3943afea2a6ea72b44f9ea469 (diff) | |
| download | miasm-9c518f0edd4d0dbc1a9be7084015d53673b488fc.tar.gz miasm-9c518f0edd4d0dbc1a9be7084015d53673b488fc.zip | |
Example: Add a new example using C & Python translation
| -rw-r--r-- | example/expression/expr_translate.py | 42 | ||||
| -rw-r--r-- | test/test_all.py | 1 |
2 files changed, 43 insertions, 0 deletions
diff --git a/example/expression/expr_translate.py b/example/expression/expr_translate.py new file mode 100644 index 00000000..17663cd2 --- /dev/null +++ b/example/expression/expr_translate.py @@ -0,0 +1,42 @@ +import random + +from miasm2.expression.expression import ExprId +from miasm2.expression.expression_helper import ExprRandom +from miasm2.ir.translators import Translator + + +class ExprRandom_OpSubRange(ExprRandom): + operations_by_args_number = {1: ["-"], + 2: ["<<", ">>",], + "2+": ["+", "*", "&", "|", "^"], + } + + +print "[+] Compute a random expression:" +expr = ExprRandom_OpSubRange.get(depth=8) +print "-> %s" % expr +print "" + +print "[+] Translate in Python:" +exprPython = Translator.to_language("Python").from_expr(expr) +print exprPython +print "" + +print "[+] Translate in C:" +exprC = Translator.to_language("C").from_expr(expr) +print exprC +print "" + +print "[+] Eval in Python:" +def memory(addr, size): + ret = random.randint(0, (1 << size) - 1) + print "Memory access: @0x%x -> 0x%x" % (addr, ret) + return ret + +for expr_id in expr.get_r(mem_read=True): + if isinstance(expr_id, ExprId): + value = random.randint(0, (1 << expr_id.size) - 1) + print "Declare var: %s = 0x%x" % (expr_id.name, value) + globals()[expr_id.name] = value + +print "-> 0x%x" % eval(exprPython) diff --git a/test/test_all.py b/test/test_all.py index 44dd75fa..db4c2c6d 100644 --- a/test/test_all.py +++ b/test/test_all.py @@ -155,6 +155,7 @@ for script in [["symbol_exec.py"], ["expression/expr_grapher.py"], ["expression/simplification_add.py"], ["expression/expr_random.py"], + ["expression/expr_translate.py"], ]: testset += Example(script) ## Jitter |