SPIRV-Tools/tools
Shahbaz Youssefi 02433568af
tools: Accept hex representation as binary input (#5870)
Sometimes when debugging or logging, SPIR-V may be dumped as a stream of
hex values.  There are tools to convert such a stream to binary
(such as [1]) but they create an inconvenient extra step when for
example the disassembly of that hex stream is needed.

[1]: https://www.khronos.org/spir/visualizer/hexdump.html

In this change, the binary reader used by the tools is enhanced to
detect when the binary is actually a hex stream, and parse that instead.
The following formats are accepted, detected based on how the SPIR-V
magic number is output:

=== Words

If the first token of the hex stream is one of 0x07230203, 0x7230203,
x07230203, or x7230203, the hex stream is expected to consist of 32-bit
hex words prefixed with 0x or x.  For example:

    0x7230203, 0x10400, 0x180001, 0x79, 0x0

is parsed as:

    0x07230203 0x00010400 0x00180001 0x00000079 0x00000000

Note that `,` is optional in the stream, but the hex values are expected
to be delimited by either `,` or whitespace.

=== Bytes With Prefix

If the first token of the hex stream is one of 0x07, 0x7, x07, x7, 0x03,
0x3, x03, or x3, the hex stream is expected to consist of 8-bit hex
bytes prefixed with 0x or x.  If the first token has a value of 7, the
stream is big-endian.  Otherwise it's little-endian.  For example:

    0x3, 0x2, 0x23, 0x7, 0x0, 0x4, 0x1, 0x0, 0x1, 0x0, 0x18, 0x0, 0x79, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0

is parsed as:

    0x07230203 0x00010400 0x00180001 0x00000079 0x00000000

Similar to "Words", `,` is optional in the stream, but the hex values
are expected to be delimited by either `,` or whitespace.

=== Bytes Without Prefix

If the first two characters of the hex stream is 07, or 03, the hex
stream is expected to consist of 8-bit hex bytes of 2 characters each.
If the first token is 07, the stream is big-endian.  Otherwise it's
little-endian.  Unlike the other modes, delimiter is optional (which
automatically handles 32-bit word streams), but no 0-padding is done.
For example, all of the following:

    03, 02, 23, 07, 00, 04, 01, 00, 01, 00, 18, 00, 79, 00, 00, 00, 00, 00, 00, 00
    03 02 23 07 00 04 01 00 01 00 18 00 79 00 00 00 00 00 00 00
    03022307 00040100 01001800 79000000 00000000
    07,23,02,03,00,01,04,00,00,18,00,01,00,00,00,79,00,00,00,00
    07230203, 00010400, 00180001, 00000079, 00000000

are parsed as:

    0x07230203 0x00010400 0x00180001 0x00000079 0x00000000
2024-11-04 09:57:37 -05:00
..
as tools: Move io utils to cpp (#5869) 2024-10-28 13:31:54 -04:00
cfg tools: Move io utils to cpp (#5869) 2024-10-28 13:31:54 -04:00
diff tools: Move io utils to cpp (#5869) 2024-10-28 13:31:54 -04:00
dis tools: Accept hex representation as binary input (#5870) 2024-11-04 09:57:37 -05:00
emacs Define variable to skip installation 2017-07-04 12:24:44 -04:00
fuzz Update proto JSON options call. (#5867) 2024-10-31 21:08:25 -04:00
lesspipe tools/lesspipe: Allow generic shell (#2255) 2018-12-27 15:06:37 -05:00
link tools: Move io utils to cpp (#5869) 2024-10-28 13:31:54 -04:00
lint linter: refactorize linter flag parsing (#5134) 2023-03-02 15:17:34 +01:00
objdump Implement source extraction logic for spirv-objdump (#5150) 2023-03-22 23:57:18 +01:00
opt tools: Move io utils to cpp (#5869) 2024-10-28 13:31:54 -04:00
reduce tools: Move io utils to cpp (#5869) 2024-10-28 13:31:54 -04:00
sva build(deps-dev): bump rollup from 3.23.0 to 3.29.5 in /tools/sva (#5826) 2024-09-26 13:45:37 -04:00
util Add explicit deduction guide for FlagRegistration (#5141) 2023-03-07 16:44:56 -05:00
val tools: Move io utils to cpp (#5869) 2024-10-28 13:31:54 -04:00
CMakeLists.txt tools: Move io utils to cpp (#5869) 2024-10-28 13:31:54 -04:00
io.cpp tools: Accept hex representation as binary input (#5870) 2024-11-04 09:57:37 -05:00
io.h tools: Accept hex representation as binary input (#5870) 2024-11-04 09:57:37 -05:00