
PeachPy: Assembly Code Generation in High-Level Python - shagunsodhani
https://github.com/Maratyszcza/PeachPy
======
struct
I remember coming across ARM's VIXL
([https://github.com/armvixl/vixl](https://github.com/armvixl/vixl)), which is
a similar concept but for C++, and just being blown away by how easy it was to
twiddle the stack and assemble new functions on the fly - yee haw!

------
lukego
Cool! I have been doing this kind of programming with dynasm [*] and finding
it addictive. I wonder if assembler is going to make a come back :).

[https://github.com/luapower/dynasm/blob/master/dynasm.md](https://github.com/luapower/dynasm/blob/master/dynasm.md)

~~~
csl
_I wonder if assembler is going to make a come back :)._

I'm certainly seeing a lot of cool projects that make assembly more
accessible, not to mention a lot of good guides on the web. As for myself,
I've recently made Python ctypes bindings for GNU Lightning, which lets you
write architecture agnostic RISC assembly, then JIT to native code on x86,
ARM, etc. See
[https://github.com/cslarsen/lyn/](https://github.com/cslarsen/lyn/)

It's pretty light-weight and JITs fast. The point is that it's very fun and
addictive to do assembly from a language like Python.

~~~
dr_zoidberg
_It 's pretty light-weight and JITs fast. The point is that it's very fun and
addictive to do assembly from a language like Python._

Because you skip the boring boilerplate, and go straight to the fun parts!

~~~
lfowles
My biggest problem with assembly was coming up with names for all of the
labels :)

One of the hardest problems of CS... naming!

How does PeachPY handle loops? I'm looking for examples, but will it generate
a label for me and then let me specify the contents only?

Edit:
[https://github.com/Maratyszcza/PeachPy/blob/master/examples/...](https://github.com/Maratyszcza/PeachPy/blob/master/examples/make/matmul-
opt.py) !

(Add a bit more, give it some static typing and soon we'll be coding C in
Python)

~~~
pbsd
That's how you unroll a loop, not how you write one. As far as I can tell in
PeachPy you can do something similar with anonymous labels:

    
    
      counter = GeneralPurposeRegister64()
      MOV(counter, 64)
      with Loop() as loop:
        ...
        DEC(counter)
        JNZ(loop.begin)

~~~
lfowles
Thanks for the clarification, that's still a decent improvement.

------
rmcpherson
Great tool. It supports assembly generation for the golang Plan 9 toolchain as
well. The author, Marat Dukhan, gave a great talk on this last night at the
Atlanta Go Meetup. Slides are at
[https://docs.google.com/presentation/d/1MYg8PyhEf0oIvZ9YU2pa...](https://docs.google.com/presentation/d/1MYg8PyhEf0oIvZ9YU2panNkVXsKt5UQBl_vGEaCeB1k/edit#slide=id.p)

------
milin
Debugging would be a nightmare when you have 300+ line function.
[https://github.com/Maratyszcza/PeachPy/blob/master/codegen/x...](https://github.com/Maratyszcza/PeachPy/blob/master/codegen/x86_64.py#L213)

------
nickpsecurity
This is pretty cool stuff. You get the RAD advantage of Python, probably easy
to extend w/ new features, customization of real assembly, and then the
assembling part itself. Nice tool. Assembly hasn't been this interesting since
Microsoft started coding it in a proof assistant [1]. I like this better as
Python is a bit more accessible. ;)

[1] [http://research.microsoft.com/en-
us/um/people/nick/coqasm.pd...](http://research.microsoft.com/en-
us/um/people/nick/coqasm.pdf)

------
ant6n
I've been looking for a way to assemble ARM instruction by instruction in
python; unfortunately this only seems to do x86.

~~~
ycnews
A brief look at the source reveals ARM support also.

~~~
Marat_Dukhan
This is a re-write of an older version of PeachPy (you will find it at
[https://bitbucket.org/MDukhan/peachpy](https://bitbucket.org/MDukhan/peachpy)),
which supported x86-64 and ARM, but didn't generate object files directly. The
new PeachPy currently only supports x86-64, albeit there are remnants of old
code for ARM.

~~~
ant6n
I didn't get the example in section "PeachPy as Instruction Encoder" to work
for arm on the linked github version, the version currently on pypi, or the
older bitbucket version. If the module loads at all, it seems to always
complain about lacking function context.

~~~
Marat_Dukhan
Probably I didn't make it clear enough:

\- The old version of PeachPy (on BitBucket and PyPI) supports ARM, but can't
directly generate machine code.

\- The new version of PeachPy (on GitHub) can directly encode instructions
into machine code, but currently only supports x86-64. The files in
peachpy.arm module are merely non-working remnants of the old code.

------
baldfat
In the early-mid 1980s there were several projects claiming this high level
language to Assembly Code or P-Code (Machine not Microsoft's abandoned
p-code). They never really worked out right and due to weird optimizations
could be just as fast or 5 times slower.

------
rthomas6
How is an assembly code generator different than a compiler?

~~~
z92
Assembly code then has to be compiled into machine code to create the
executable.

~~~
lfowles
To expand a bit in terrible pseudocode that I sort of remember from Atmel ucs:

Assembly: mov 0x1,reg2 # put 1 in register 2

Machine Code: 0xAC12 # Move an immediate value into register 2

Assembly: mov *reg2,reg1 # put contents of address in reg2 in reg1

Machine Code: 0xBF201 # Move an indirect value, offset 0, address in reg2 to
reg1

It's a much more straightforward transformation than the one from C->ASM
though.

Edit: This link might make more sense... the difference is mainly between the
mnemonic and the encoding:
[http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html](http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html)

You'll also notice there are explicit instructions for immediate ops, but no
two assembly schemes seem to share the same level of abstraction.

------
dang
Url changed from [http://lambda-the-ultimate.org/node/5251](http://lambda-the-
ultimate.org/node/5251), which points to this.

~~~
shagunsodhani
Thanks!

