blob: 5f8e94db847c30ba83e6fc3e8c894ee0afd36c5a [file] [log] [blame]
David Neto4f750c02016-10-13 15:17:11 -04001// 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
15// This file contains utility functions for spv_parsed_operand_t.
16
dan sinclaireda2cfb2018-08-03 15:06:09 -040017#include "source/parsed_operand.h"
David Neto4f750c02016-10-13 15:17:11 -040018
19#include <cassert>
dan sinclaireda2cfb2018-08-03 15:06:09 -040020#include "source/util/hex_float.h"
David Neto4f750c02016-10-13 15:17:11 -040021
dan sinclair3dad1cd2018-07-07 09:38:00 -040022namespace spvtools {
David Neto4f750c02016-10-13 15:17:11 -040023
24void EmitNumericLiteral(std::ostream* out, const spv_parsed_instruction_t& inst,
25 const spv_parsed_operand_t& operand) {
Alan Baker983f8f02018-08-08 13:01:57 -040026 if (operand.type != SPV_OPERAND_TYPE_LITERAL_INTEGER &&
Shahbaz Youssefi7fa9e742022-02-02 10:33:18 -050027 operand.type != SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER &&
28 operand.type != SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER &&
29 operand.type != SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER)
Alan Baker983f8f02018-08-08 13:01:57 -040030 return;
31 if (operand.num_words < 1) return;
32 // TODO(dneto): Support more than 64-bits at a time.
33 if (operand.num_words > 2) return;
David Neto4f750c02016-10-13 15:17:11 -040034
35 const uint32_t word = inst.words[operand.offset];
36 if (operand.num_words == 1) {
37 switch (operand.number_kind) {
38 case SPV_NUMBER_SIGNED_INT:
39 *out << int32_t(word);
40 break;
41 case SPV_NUMBER_UNSIGNED_INT:
42 *out << word;
43 break;
44 case SPV_NUMBER_FLOATING:
45 if (operand.number_bit_width == 16) {
dan sinclair76e0bde2018-07-06 13:25:17 -040046 *out << spvtools::utils::FloatProxy<spvtools::utils::Float16>(
David Neto4f750c02016-10-13 15:17:11 -040047 uint16_t(word & 0xFFFF));
48 } else {
49 // Assume 32-bit floats.
dan sinclair76e0bde2018-07-06 13:25:17 -040050 *out << spvtools::utils::FloatProxy<float>(word);
David Neto4f750c02016-10-13 15:17:11 -040051 }
52 break;
53 default:
Alan Baker983f8f02018-08-08 13:01:57 -040054 break;
David Neto4f750c02016-10-13 15:17:11 -040055 }
56 } else if (operand.num_words == 2) {
57 // Multi-word numbers are presented with lower order words first.
58 uint64_t bits =
59 uint64_t(word) | (uint64_t(inst.words[operand.offset + 1]) << 32);
60 switch (operand.number_kind) {
61 case SPV_NUMBER_SIGNED_INT:
62 *out << int64_t(bits);
63 break;
64 case SPV_NUMBER_UNSIGNED_INT:
65 *out << bits;
66 break;
67 case SPV_NUMBER_FLOATING:
68 // Assume only 64-bit floats.
dan sinclair76e0bde2018-07-06 13:25:17 -040069 *out << spvtools::utils::FloatProxy<double>(bits);
David Neto4f750c02016-10-13 15:17:11 -040070 break;
71 default:
Alan Baker983f8f02018-08-08 13:01:57 -040072 break;
David Neto4f750c02016-10-13 15:17:11 -040073 }
David Neto4f750c02016-10-13 15:17:11 -040074 }
75}
dan sinclair3dad1cd2018-07-07 09:38:00 -040076} // namespace spvtools