Home | History | Annotate | Download | only in source
      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 
     15 // This file contains utility functions for spv_parsed_operand_t.
     16 
     17 #include "source/parsed_operand.h"
     18 
     19 #include <cassert>
     20 #include "source/util/hex_float.h"
     21 
     22 namespace spvtools {
     23 
     24 void EmitNumericLiteral(std::ostream* out, const spv_parsed_instruction_t& inst,
     25                         const spv_parsed_operand_t& operand) {
     26   if (operand.type != SPV_OPERAND_TYPE_LITERAL_INTEGER &&
     27       operand.type != SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER)
     28     return;
     29   if (operand.num_words < 1) return;
     30   // TODO(dneto): Support more than 64-bits at a time.
     31   if (operand.num_words > 2) return;
     32 
     33   const uint32_t word = inst.words[operand.offset];
     34   if (operand.num_words == 1) {
     35     switch (operand.number_kind) {
     36       case SPV_NUMBER_SIGNED_INT:
     37         *out << int32_t(word);
     38         break;
     39       case SPV_NUMBER_UNSIGNED_INT:
     40         *out << word;
     41         break;
     42       case SPV_NUMBER_FLOATING:
     43         if (operand.number_bit_width == 16) {
     44           *out << spvtools::utils::FloatProxy<spvtools::utils::Float16>(
     45               uint16_t(word & 0xFFFF));
     46         } else {
     47           // Assume 32-bit floats.
     48           *out << spvtools::utils::FloatProxy<float>(word);
     49         }
     50         break;
     51       default:
     52         break;
     53     }
     54   } else if (operand.num_words == 2) {
     55     // Multi-word numbers are presented with lower order words first.
     56     uint64_t bits =
     57         uint64_t(word) | (uint64_t(inst.words[operand.offset + 1]) << 32);
     58     switch (operand.number_kind) {
     59       case SPV_NUMBER_SIGNED_INT:
     60         *out << int64_t(bits);
     61         break;
     62       case SPV_NUMBER_UNSIGNED_INT:
     63         *out << bits;
     64         break;
     65       case SPV_NUMBER_FLOATING:
     66         // Assume only 64-bit floats.
     67         *out << spvtools::utils::FloatProxy<double>(bits);
     68         break;
     69       default:
     70         break;
     71     }
     72   }
     73 }
     74 }  // namespace spvtools
     75