111 points · by mmastrac · 10y ago · 55 comments
http://research.microsoft.com/en-us/um/people/nick/coqasm.pd...
https://www.cs.cornell.edu/talc/
And Vx86 used in Microsoft hypervisor:
http://swt.informatik.uni-freiburg.de/staff/maus/dissemain.p...
Note: Microsoft also used a TAL, though can't recall if same as Cornell, in their VerveOS that was verified down to assembler. Combined with SLAM, it's safe to say their researchers are kicking serious behind on practical, cutting-edge verification. I wouldn't have believed it 10+ years ago if someone told me haha.
This is using a system known as "macro_rules" which are a system of "expanding" -- in some ways it's a fancier version of the C preprocessor, but also resembling the Lisp/Racket macro system to some degree too.
Let's take a random little piece of that code from the post and talk about it:
macro_rules! codelen {
() => { 0 };
( $($c:expr),+ ) => { [$($c),+].len() };
}
This declares a new macro substitution rule called `codelen`.
Without going into too much depth, that's defining rules (on the left hand of the `=>`) and saying what code to expand them into, on the other side. The first replaces `()` with `{0};`, just a simple substitution. In the second rule, the `$($variable),+ is the essential construct here, letting you operate on sequences of input stuff.It's a little hard to read -- writing your own macros in Rust is a little bit complicated and I don't think it's actually something that should be done often. You can write little DSLs with it for sure. This variety is going to stick around and will keep working. It is called macro_rules! and not just macro! to leave that name free for a future, enhanced way to write macros, though.
> Is this a "procedural" macro?
This is not a procedural macro, it's a regular-old macro. > Does Rust specifically target writing DSLs using its macro system?
I would say that "specifically" is a bit strong, but they are one application of them, yes. > Can we expect macros like this to continue working for a long time?
This functionality runs on Rust stable, and therefore should continue working forever.The post you're referring to is talking about procedural macros/syntax extensions/compiler plugins, which are not stable and will see lots of change.
We also may end up with a new macro system as well, but it will not conflict with the existing macro system if we do.
(also, 0x syntax is quite unusual, but I assume $ couldn't have been implemented for some reason)
http://www.downloads.reactivemicro.com/Public/Users/David_Cr...
Code is straight from the Red Book listings. Search for ROR - accumulator (shown explicitly), indexed, you name it. 4 modes each.
What you name is the value to be operated on, and that is indexed, zpage, immediate, etc... the accumulator is not named. There are two or three bytes, one instruction, one or two for value or address.
Same for things like ADC, the accumulator is where it will happen, the things you name are values and addresses.
LSL, LSR can just be stated, one byte instructions that operate on the accumulator, which does not need to be specified.
6502 is really basic. The accumulator is the working register for many things. The index registers are for indexing. One does not do LDX $c000, A for example.
Benefit (?): you can now have a label called "A".
This reminded me of https://github.com/kingcons/cl-6502 where the opcodes are defined using a defasm macro https://github.com/kingcons/cl-6502/blob/master/src/cpu.lisp... and implemented here https://github.com/kingcons/cl-6502/blob/master/src/opcodes....
Are Lisp macros pretty much on par with Rust macros?
This is getting to be a big problem with Rust. The language, or maybe its community, encourages cutesy stuff like this. Remember "Diesel", the Rust compiler for SQL, from a few days ago? Just because you can doesn't mean you should. Too much fancy template stuff is being baked into the low level libraries. This may not end well.
C++ went down this road. The template system turned out to be more powerful than intended. Then people started writing cool stuff as fancy templates. Then the C++ committee added features to support the fancy templates better. Overly elaborate templates damaged C++; nobody could figure out what was going on. Use of C++ for new work declined.
All of this comes from smart people with good intentions. But when there's too much of this stuff, the mental load required to keep up with all of it becomes excessive. Especially if it changes a lot.
Maybe even in Rust, although I have not tried it yet.