cea-sec/miasm — pull request #23 by commial (2014-12-14 17:40:31+00:00)
Title: Introduce IR Translators

IR Translators are a set of `Translator` subclasses.
They aims to make the translation part between Miasm IR and others languages independent of others modules.

For instance, the C or LLVM translation previously implemented in the Jitter part should be available for others purposes, such as producing code or using LLVM simplification passes.

The `ir.translators` sub-module exposes the parent class, `Translator`.
This class provides methods to retrieve lang-specific translators:
```Python
Translator.available_languages() -> ["C", "Python"]
Translator.to_language("C") -> TranslatorC
Translator.to_language("Python") -> TranslatorPython
```

An example (`example/expression/expr_translate.py`) is provided:
```
$ python expr_translate.py
[+] Compute a random expression:
-> @32[@1[{{{@4[@12[0xAD]],0,4},0,4, 0xF782[2:7],4,9, QhulF[5:10],9,14, (0x5|iUwgR|0x2),14,17, FHFGW[8:19],17,28},0,28, (0x5|iUwgR|0x2),28,31, {0x1,0,1},31,32}[13:30]]]

[+] Translate in Python:
memory(memory(((((((((((memory(memory(0xAD, 0x1), 0x0) & 0xf) << 0)) & 0xf) << 0) | (((0xF782 & 0x1f) & 0x1f) << 4) | (((QhulF & 0x1f) & 0x1f) << 9) | (((0x5 | iUwgR | 0x2 & 0x7) & 0x7) << 14) | (((FHFGW & 0x7ff) & 0x7ff) << 17)) & 0xfffffff) << 0) | (((0x5 | iUwgR | 0x2 & 0x7) & 0x7) << 28) | (((((0x1 & 0x1) << 0)) & 0x1) << 31)) & 0x1ffff), 0x0), 0x4)

[+] Translate in C:
MEM_LOOKUP_32(vm_mngr, MEM_LOOKUP_01(vm_mngr, ((((((uint32_t)(((((uint28_t)(((((uint4_t)(MEM_LOOKUP_04(vm_mngr, MEM_LOOKUP_12(vm_mngr, 0xad)) & 0xF)) << 0)) & 0xF)) << 0) | (((uint28_t)(((0xf782>>2) & 0x1F) & 0x1F)) << 4) | (((uint28_t)(((QhulF>>5) & 0x1F) & 0x1F)) << 9) | (((uint28_t)((((0x5&0x7)|(iUwgR&0x7)|(0x2&0x7))&0x7) & 0x7)) << 14) | (((uint28_t)(((FHFGW>>8) & 0x7FF) & 0x7FF)) << 17)) & 0xFFFFFFF)) << 0) | (((uint32_t)((((0x5&0x7)|(iUwgR&0x7)|(0x2&0x7))&0x7) & 0x7)) << 28) | (((uint32_t)(((((uint1_t)(0x1 & 0x1)) << 0)) & 0x1)) << 31))>>13) & 0x1FFFF)))

[+] Eval in Python:
Declare var: QhulF = 0x1e16
Declare var: FHFGW = 0x3323b2
Declare var: iUwgR = 0x1
Memory access: @0xad -> 0x1
Memory access: @0x1 -> 0x0
Memory access: @0x1ec20 -> 0x0
Memory access: @0x0 -> 0xf
-> 0xf
```

In the future, Translators may be used to implement jitters by sub-classing the targeted language translator and implementing jitter-specific parts.