about summary refs log tree commit diff stats
path: root/example/expression
diff options
context:
space:
mode:
authorCamille Mougey <camille.mougey@cea.fr>2014-12-14 18:25:52 +0100
committerCamille Mougey <camille.mougey@cea.fr>2014-12-14 18:25:52 +0100
commit9c518f0edd4d0dbc1a9be7084015d53673b488fc (patch)
tree3d16c611cd5c778625312b74b22590e3626345c3 /example/expression
parent90180500af9736e3943afea2a6ea72b44f9ea469 (diff)
downloadmiasm-9c518f0edd4d0dbc1a9be7084015d53673b488fc.tar.gz
miasm-9c518f0edd4d0dbc1a9be7084015d53673b488fc.zip
Example: Add a new example using C & Python translation
Diffstat (limited to 'example/expression')
-rw-r--r--example/expression/expr_translate.py42
1 files changed, 42 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)