Commit Graph

40 Commits

Author SHA1 Message Date
Lei Zhang
aa3cd5abb9 Optional typed literal number should be concrete in BinaryParser. 2015-11-10 15:58:16 -05:00
David Neto
229b90f6f4 Print OpConstant values according to type.
Zero and normal floating point values are printed with enough
enough digits to reproduce all the bits exactly.
Other float values (subnormal, infinity, and NaN) are printed
as hex floats.

Fix a binary parse bug: Count partially filled words in a
typed literal number operand.

TODO: Assembler support for hex numbers, and therefore reading
infinities and NaNs.
2015-11-10 15:57:59 -05:00
David Neto
201caf7001 spv_operand_type_t cleanup.
- Concrete operand types are never optional.
  Split them to make this so, e.g. add SPV_OPERAND_TYPE_IMAGE
  since there was SPV_OPERAND_TYPE_OPTIONAL_IMAGE.
  Similarly for SPV_OPERAND_TYPE_MEMORY_ACCESS.
  This entails duplicating two operand table entries.

- The above, plus some rearranging of enums, allows us to define
  first and last optional operand types, and first and last
  variable operand types.
  This lets us simplify the code for spvOperandIsOptional, and
  spvOperandIsVariable.

- Replace SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER with the
  more accurately named SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER.
  Its special characteristic is that the type of the literal
  number is determined by some previous operand in the instruction.
  This is used for literals in OpSwitch, OpConstant, and OpSpecConstant.
  This lets us refactor operand parsing cases in the assembler.

- Remove the special required-thing-in-optional-tuple in favour of
  the corresponding concrete operand type:
        SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE
    --> SPV_OPERAND_TYPE_ID
        SPV_OPERAND_TYPE_INTEGER_LITERAL_IN_OPTIONAL_TUPLE
    --> SPV_OPERAND_TYPE_INTEGER_LITERAL

- Constrain spvOpeandTypeStr to only have to work for non-variable
  operand types.  Add a test for this.
2015-11-10 15:57:45 -05:00
David Neto
0ca6b59bfd Refactor the binary parser.
The binary parser has a C API, described in binary.h.
Eventually we will make it public in libspirv.h.
The API is event-driven in the sense that a callback is called
when a valid header is parsed, and for each parsed instruction.

Classify some operand types as "concrete".  The binary parser uses
only concrete operand types to describe parsed instructions.

The old disassembler APIs are moved into disassemble.cpp

TODO: Add unit tests for spvBinaryParse.
2015-11-10 15:57:33 -05:00
David Neto
e4945def95 A spv_binary_t points to const code words. 2015-11-10 15:57:22 -05:00
David Neto
ea324e4a23 Move spvBinaryToText into new disassembler file.
This begins the refactoring of the disassembler into
two parts: A binary decoder in binary.cpp, and an
event-driven converter to text in disassemble.cpp
2015-11-10 15:57:19 -05:00
David Neto
aef608c40d Consolidate: spvOpcodeIsType into spvOpcodeGeneratesType
And fix the spvOpcodeGeneratesType: OpTypeForwardPointer
does not generate a new type.
2015-11-10 15:56:54 -05:00
Lei Zhang
1a0334edee Run clang-format to enforce Google style globally.
Note that we are more strict than Google style for one aspect:
pointer/reference indicators are adjacent to their types, not
their variables.

find . -name "*.h" -exec clang-format -i {} \;
find . -name "*.cpp" -exec clang-format -i {} \;
2015-11-10 15:56:47 -05:00
Lei Zhang
b36e704a47 Use spirv.h instead of spirv.hpp.
This is a part of the effort to make libspirv.h C-compatible.
2015-11-02 13:52:20 -05:00
David Neto
db901b622e Move endian utilities to their own file. 2015-11-02 13:52:12 -05:00
David Neto
0b9816813b Hide spvBinaryHeaderSet with its only client.
Also rename it to SetHeader since it's not part of the "binary"
API.
2015-11-02 13:52:09 -05:00
David Neto
fcc7d58353 Move AssemblyGrammar into its own source files 2015-11-02 13:52:05 -05:00
Andrew Woloszyn
157e41bf57 Added type tracking to the disassembler.
TODO: Actually use the tracked types to make sure that we print out
values correctly.
2015-10-26 12:55:33 -04:00
David Neto
445ce4401d Add operand type for extension instruction number
This is required to support extended instructions that
have literal numbers as operands.  An example is OpenCL's
vloadn.

The previous code in the assembler assumed that *any* literal
number argument in any part of an OpExtInst must be the name
of the extended instruction.  That's true only for the first
literal number argument.
2015-10-26 12:55:33 -04:00
Andrew Woloszyn
ccc210b4cc Some minor clean-ups to binary.{h,cpp}.
Removed spvBinaryDecodeOpcode and spvBinaryDecodeOperand from the public
interface since they were only ever used in binary.cpp.

Replaced the usage of spv_operand_table_t and it's ilk with the
AssemblyGrammar to reduce the number of passed parameters.

Fixed typo in comment.
2015-10-26 12:55:33 -04:00
Lei Zhang
6483bd7132 Enforce suitable literal number operands to be unsigned integers.
Except for OpConstant and OpSpecConstant, all other literal number
operands are indeed unsigned integers. So,

* Rename all *LITERAL_NUMBER* operand types to *LITERAL_INTEGER*.
* Expect unsigned integers for *LITERAL_INTEGER* operands.
* Keep MULITPLE_WORD_LITERAL untouched since it is only used by
  OpConstant and OpSpecConstant.

And we want to provide the capability to specify floating-point
numbers after !<integer> in the alternate parsing mode. So,
OPTIONAL_LITERAL_NUMBER is reserved for OPTIONAL_CIV.
2015-10-26 12:55:33 -04:00
David Neto
980b7cb95e Avoid strlen in a loop, for speed. 2015-10-26 12:55:33 -04:00
Andrew Woloszyn
e59e6b72f9 Added proper string escaping to the disassembler.
The disassembler now generates correct string when encountering
quotes and slashes.
2015-10-26 12:55:33 -04:00
Andrew Woloszyn
537e77663d Added tracking to types and validated certain instructions.
We need to know how to generate correct SPIRV for cases like
OpConstant %int64 42 since the current parser will encode the 42 as a
32-bit value incorrectly.

This change is the first of a pair. This one tracks types, and makes
sure that OpConstant and OpSpecConstant are only ever called with
Integer or Float types, and OpSwitch is only called with integer
generating values.
2015-10-26 12:55:33 -04:00
David Neto
b5dc8fcd5d Support strings with up to 65535 chars, and null.
Move the definition of spv_instruction_t to an internal
header file, since it now depends on C++ and is not
used by the external interface.

Use a std::vector<uint32_t> in spv_instruction_t
instead of a fixed size array.
2015-10-26 12:55:33 -04:00
Andrew Woloszyn
71fc05587b Refactored dynamic and static state out of text processing.
This reduces the number of arguments required to be passed
to every single function. This is in preparation for adding
id tracking.
2015-10-26 12:55:33 -04:00
David Neto
561dc4e975 Remove SPV_OPERAND_TYPE_LITERAL
All uses of OptionalLiteral by the SPIR-V spec are used
for literal numbers.

Also rename:
- SPV_OPERAND_TYPE_OPTIONAL_LITERAL to
  SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER.
- SPV_OPERAND_TYPE_VARIABLE_LITERAL to
  SPV_OPERAND_TYPE_VARIABLE_LITERAL_NUMBER.
- SPV_OPERAND_TYPE_VARIABLE_LITERAL_ID to
  SPV_OPERAND_TYPE_VARIABLE_LITERAL_NUMBER_ID.
- SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL to
  SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_NUMBER.
- SPV_OPERAND_TYPE_LITERAL_IN_OPTIONAL_TUPLE to
  SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE.
2015-10-26 12:55:33 -04:00
David Neto
b14a727a30 Execution scope, memory semantics operands are IDs
They shouldn't be parsed or printed as masks.
2015-10-26 12:55:33 -04:00
David Neto
619db2612e Disassembler support for mask expressions. 2015-10-26 12:55:33 -04:00
Lei Zhang
b41d150b7f Support multiple word literal numbers as constants.
Add a new operand type SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER
to represent those operands that can expand into multiple words.
Now only OpConstant and OpSpecConstant have such kind of operand.
2015-10-26 12:55:33 -04:00
Lei Zhang
97afd5c03c Remove useless variable. 2015-10-26 12:55:33 -04:00
Lei Zhang
41bf0733c6 Simplify code for handling literals in disassembler. 2015-10-26 12:55:33 -04:00
Lei Zhang
4005670363 Limit the use of spvCheck and spvCheckReturn to validator.
spvCheck is indeed just an if-statement. Defining such a macro
doesn't help much.
2015-10-26 12:55:33 -04:00
Lei Zhang
29e667eb6c Add library function parameter to set assembly format for disassembler. 2015-10-26 12:55:33 -04:00
Lei Zhang
e78a7c19ff Run clang-format. 2015-10-26 12:54:39 -04:00
Andrew Woloszyn
4b4acded77 Added missing diagnostic when disassembling empty file.
Added diagnostic messages for what should be an internal failure
that never happens. I figure if we return "failed" for something the
user cannot control we should print a message for it.
2015-10-26 12:54:39 -04:00
Pyry Haulos
26b3b00887 Fix build on MSC
Visual C++ compiler doesn't allow ternary select where type depends on
condition even if there is a user-defined conversion operator to a common
type.
2015-10-26 12:54:39 -04:00
Andrew Woloszyn
cfeac48a37 Removed the ambiguity in the API for cleaning input vs output data.
Previous the api used spv_text_t and spv_binary_t for both input
and output, but depending on the usage, you either MUST
call spvBinaryDestroy or you MUST NOT call spvBinaryDestroy on the
pointer.
2015-10-26 12:54:39 -04:00
David Neto
78c3b43774 Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types.  (Internally, it is a deque,
for readability.)  Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.

The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
  extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
  operands.  These are expanded lazily during the parse.

Adds spv::OperandClass from the SPIR-V specification generator.

Modifies spv_operand_desc_t:
 - adds hasResult, hasType, and operandClass array to the opcode
description type.
 - "wordCount" is replaced with "numTypes", which counts the number
   of entries in operandTypes.  And each of those describes a
   *logical* operand, including the type id for the instruction,
   and the result id for the instruction.  A logical operand could be
   variable-width, such as a literal string.

Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)

Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH.  For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.

The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode.  So now, "OpenCL" does not look like
an opcode name.

In assembly, the EntryPoint name field is mandatory, but can be
an empty string.

Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-10-26 12:52:01 -04:00
David Neto
4799482787 Fix spelling of SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO 2015-10-26 12:52:01 -04:00
Lei Zhang
8a37520908 Disassemble in the format of "<result-id> = <opcode> <operand>..". 2015-10-26 12:52:01 -04:00
Lei Zhang
abafd5e674 Only use '%' as variable name prefix.
Since now we can distinguish between def and use according to
the variable's location, there is no need to keep two variable
prefixes.

Also reformat tests to use the value generating instruction
format ("<result-id> = <opcode> <operand>..").
2015-08-24 15:05:11 -04:00
Andrew Woloszyn
0d350b5992 Make the disassembler print the Extended instruction name.
This allows the disassembled output to more closely follow the
original assembly.
2015-08-24 15:04:55 -04:00
Kenneth Benzie (Benie)
81d7d49a20 Change binary header generator word to Khronos 2015-06-01 09:50:46 -07:00
Kenneth Benzie (Benie)
83e5a29b06 Code drop of the Codeplay spirv-tools source.
This commit contains the source for the SPIRV static library, spirv-as,
spirv-dis, and spirv-val tools.
2015-05-22 18:26:19 +01:00