DrItanium/theoretical-architecture — pull request #7 by ajvondrak (2014-03-25 06:24:19+00:00)
Title: Unparse instructions

OK, so there's a lot going on in this pull request.

High-level feature: `void unparse(char*, ushort)` takes in an instruction and will write a human-friendly string representation. The obvious gotcha here is, of course, that the instruction set isn't really defined, as per issue #4. I just took a crack at it, and tried to write the code modularly enough that you can swap out the little details:
* `mnemonics.c` stores the actual instruction mnemonics (`add`, `load`, etc.). It's essentially a bunch of `switch` statements around `const char*` return values, so it's easy to swap out different mnemonics you might prefer.
* `unparse.c` does the work of stringing together the whole instruction, with the help of `mnemonic.c`. It's slightly hairier, but adopts a few simple syntactic conventions that should be easy to change:
* Immediate values are represented in the raw; if signed and negative, they'll have negative signs (duh).
* Registers are represented as `r`, where `` is a number `0`-`7`.
* Signed registers are spelled `$r`.
* Two registers used as one address are written with a colon like `r0:r1`.

I reused certain mnemonics where I *think* the above syntax rules make the "underlying" instruction unambiguous. E.g., register-to-register moves look like `load r0 r0`, but immediate-to-register moves look like `load r0 0`.

To support the above, a few other things changed. On the minor end, I added Vim modelines, because you seem to use 3 spaces of indentation and I was sick of being caught off guard with my `shiftwidth=2`. More major is the `Makefile` (which is what was giving me the most merge conflicts):
* It uses wildcards now, so you don't have to manually add rules.
* `make iris` builds all top-level files along with `sim.c`, since it's the top-level file with the `main()` function.
* I moved all tests to a `tests/` directory. Each `.c` file in there should define a `main()` function, so that the next change works...
* `make test_blah` will compile `tests/blah.c` for the `main()` function *instead* of `sim.c`.

Bringing it all together, I'd love if you could peruse the output of `make test_unparse && ./iris` to make sure (a) it works and (b) the output makes sense.

I welcome your feedback! Let me know if there's anything I should change.