parsed_operand.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2016 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // This file contains utility functions for spv_parsed_operand_t.
  15. #include "source/parsed_operand.h"
  16. #include <cassert>
  17. #include "source/util/hex_float.h"
  18. namespace spvtools {
  19. void EmitNumericLiteral(std::ostream* out, const spv_parsed_instruction_t& inst,
  20. const spv_parsed_operand_t& operand) {
  21. if (operand.type != SPV_OPERAND_TYPE_LITERAL_INTEGER &&
  22. operand.type != SPV_OPERAND_TYPE_LITERAL_FLOAT &&
  23. operand.type != SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER &&
  24. operand.type != SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER &&
  25. operand.type != SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER)
  26. return;
  27. if (operand.num_words < 1) return;
  28. // TODO(dneto): Support more than 64-bits at a time.
  29. if (operand.num_words > 2) return;
  30. const uint32_t word = inst.words[operand.offset];
  31. if (operand.num_words == 1) {
  32. switch (operand.number_kind) {
  33. case SPV_NUMBER_SIGNED_INT:
  34. *out << int32_t(word);
  35. break;
  36. case SPV_NUMBER_UNSIGNED_INT:
  37. *out << word;
  38. break;
  39. case SPV_NUMBER_FLOATING:
  40. if (operand.number_bit_width == 16) {
  41. *out << spvtools::utils::FloatProxy<spvtools::utils::Float16>(
  42. uint16_t(word & 0xFFFF));
  43. } else {
  44. // Assume 32-bit floats.
  45. *out << spvtools::utils::FloatProxy<float>(word);
  46. }
  47. break;
  48. default:
  49. break;
  50. }
  51. } else if (operand.num_words == 2) {
  52. // Multi-word numbers are presented with lower order words first.
  53. uint64_t bits =
  54. uint64_t(word) | (uint64_t(inst.words[operand.offset + 1]) << 32);
  55. switch (operand.number_kind) {
  56. case SPV_NUMBER_SIGNED_INT:
  57. *out << int64_t(bits);
  58. break;
  59. case SPV_NUMBER_UNSIGNED_INT:
  60. *out << bits;
  61. break;
  62. case SPV_NUMBER_FLOATING:
  63. // Assume only 64-bit floats.
  64. *out << spvtools::utils::FloatProxy<double>(bits);
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. }
  71. } // namespace spvtools