An open API service indexing awesome lists of open source software.

https://github.com/turkeymcmac/instruction-switch

Generate code for handling complex switches based on bit patterns.
https://github.com/turkeymcmac/instruction-switch

assembly codegen

Last synced: about 1 month ago
JSON representation

Generate code for handling complex switches based on bit patterns.

Awesome Lists containing this project

README

          

# instruction-switch
Generate code for handling complex switches based on bit patterns. The name was
given because I hope to use it for decoding machine code instructions. The
program generates the body of a C function which you can `#include` in another
file.

## Installation
To make the executable usable, do the following on the command line:
```
make install
```

If you can't use make and/or awk, here are the manual steps:
1. copy `src/main.rb` to the file `instrswitch`
2. Put the shebang or whatever at the file beginning: `#!/usr/bin/env ruby`
3. Look through the new file and replace all file names which are arguments to
`load` statements with the absolute paths to those files (they are all in
the `src` directory)
4. Make the new file executable using `chmod +x instrswitch` or something
5. Copy `instrswitch` to a directory in your PATH

## Usage
### Generation
Call the executable with the form
```
instrswitch -i > -o
```
where the instruction table is a TSV file in the following format:
```
nameformat

```

Each instruction format is a sequence of up to 64 characters. 0s and 1s indicate
a bit which is invariant for that instruction. Other characters indicate a bit
which can vary. All other characters except '_' or ' ' also correspond to an
argument of the instruction, and will be recorded.

The generated code is minified, so run it through `indent` before viewing it.

### Including in C Code
Two macros must be defined above where the autogenerated file is included:
* `DO_INSTR_(name, arg_count, args)`: This takes the name of the matching
instruction as a symbol and the comma-separated argument list within
parentheses. `arg_count` is the number of arguments to the instruction.
* `DO_ERROR_`: This token is used in the case that none of the possible
instructions match the pattern.

## Format Overlap
The instruction formats given must be completely unambiguous. For example, this
is legal:
```
add0aaabbb1
sub1aaabbb1
```
But it would be illegal to add:
```
neg1aaa1111
```
Because their is no way to tell between the `sub` and `neg` instructions.

## Examples
After you have built the program, there is an example of its usage in
`examples/utf8`.