A simple x86 assembler C++ template metaprogram
blog.mattbierner.com
blog.mattbierner.com
The tests suite is neat (imho). At runtime it walks a data structure built at compile time to randomly exercise each of the possible encoding clauses of the instruction and compare the output to Nasm. Writing that was a very "lisp is for building organisms" moment for me.
As an aside, x86 makes a lot more sense than people give it credit for.
Do you use it? Is anyone aware of a python way of this?
Later, we did one in Ruby, which allowed our assembler syntax to almost mirror AT&T assembler while still being pure Ruby.
These are easy libraries to write; people should just write them themselves.
Well, if you say so! Seriously though, isn't x86/64 opcode set gargantuan in size? Not to mention the maintenance and what to do with parallel constructs. It doesn't sound easy to me, apart from trivial text emit experiments. I haven't tried writing one though, so I wouldn't know. Also, what's easy for someone isn't easy for others. I find image processing and rendering relatively easy for example.
(b) You don't need even most of the instructions to get to a useful, fun place.
I was surprised by how easy it was to get to that place. Take my word for this. Read this:
http://reocities.com/SiliconValley/heights/7052/opcode.txt
and just start coding.
https://github.com/pflanze/hasm
I implemented a number of higher level features for structured programming. Example input file: https://github.com/pflanze/hasm/blob/master/examples/fact.sc...
It doesn't use Scheme macros but expands assembly macros using custom code (because the bottom layer isn't Scheme anyway, just S-expressions, and so that I could let the macro expanders access their context).
Of course the difficult part would be to grow the higher level features in a way that they are safe, and I'm not sure how useful they really are when allocating registers manually. Once you build in a register allocator, you'd probably call it a compiler.
I miss that tool. Lost it in a triple HD failure along with the others. Closest thing I've seen to my approach is iMatix's combo of DSL's and mini-4GL-for-C for more productive, correct, C-language applications. Racket has potential to exceed anything I did and for more languages if applied wisely. It's main option if I try to rebuild my tool.
Btw, what would I call a tool that combines LISP macros, 4GL features (eg DSL's, generators), C or C++ data-types, and auto-generation of C code? It doesn't necessarily need LISP syntax: one prototype used Tcl style & final looked like BASIC w/ compatibility for BASIC tools. It fits as a systems language, a 4GL, a LISP, and a C/C++ superset all at once.
To "unbury" that fact some more, I'll again link to the message I think you're referring to...
http://reocities.com/SiliconValley/heights/7052/opcode.txt
(Google still refuses to index that link for some reason. IIRC it used to have a very high ranking, but disappeared from search results within the last year or so.)
> http://www.dabo.de/ccc99/www.camp.ccc.de/radio/help.txt
Some bugs were corrected June, the 20th, 1997 by S.Klose (sven@devcon.net)
(minor bugs in 32bit effective addresses and opcode typoes)Then this could be used for on-the-fly code generation like you'd see in a jit-emulator like qemu.
you should find that it fails in most modern execution environments...
in legacy/desktop windows you can virtualalloc things to be executable but the same is not true in the general case, and this code doesn't do that.
even a long time ago (before windows 8, ios etc.) i wasn't able to just execute things on the stack or heap reliably and had to write code that explicitly allocated executable memory.