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
This commit fixes the following build error when the source code is build
with MinGW gcc 12 and compiler switch '-Werror=maybe-uninitialized' is
added:
'file.{anonymous}::OutputFile::old_mode_' may be used uninitialized
in this function [-Werror=maybe-uninitialized]
We currently do not set stdout to binary mode when writing a binary
file on Windows. This cause some extra characters to be written that
mess up the output. We try to fix that.
Fixes#4793
When a tool (tested with spirv-dis and spirv-as) runs on a file that
does not exist, it notifies the user and tries to close the stream that
was never successfully opened.
* Use standard function to get stdin to binary mode.
The fisrt implementation to try to change stdin to binary mode is not
protable. Using freopen has a lot of implementation defined behaviour,
and the code relied on that working.
Looking into the MSVC documentation, I do not see a protable way of
doing this. I've implemented a Windows specific method and do nothing
for other systems. In general, UNIX based systems do make a distinction
between text and binary files, so nothing needs to be done for them.
Fixes#2518
* Split ReadFile into two different functions.
We want to remove the flag parameter. So the original function has been
replaced with ReadBinaryFile and ReadTextFile. This should make the
code more readable.
* Change name of CorrectlyReadFile to avoid confusion.
[val] Add extra context to error messages.
This CL extends the error messages produced by the validator to output the
disassembly of the errored line.
The validation_id messages have also been updated to print the line number of
the error instead of the word number. Note, the error number is from the start
of the SPIR-V, it does not include any headers printed in the disassembled code.
Fixes#670, #1581