Jlm
type.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 Helge Bahmann <hcb@chaoticmind.net>
3  * Copyright 2011 2012 2013 2014 2015 Nico Reißmann <nico.reissmann@gmail.com>
4  * See COPYING for terms of redistribution.
5  */
6 
8 #include <jlm/rvsdg/graph.hpp>
9 #include <jlm/util/Hash.hpp>
10 #include <jlm/util/strfmt.hpp>
11 
12 namespace jlm::rvsdg
13 {
14 
15 BitType::~BitType() noexcept = default;
16 
17 std::string
18 BitType::debug_string() const
19 {
20  return jlm::util::strfmt("bit", nbits());
21 }
22 
23 bool
24 BitType::operator==(const Type & other) const noexcept
25 {
26  auto type = dynamic_cast<const BitType *>(&other);
27  return type != nullptr && this->nbits() == type->nbits();
28 }
29 
30 std::size_t
31 BitType::ComputeHash() const noexcept
32 {
33  auto typeHash = typeid(BitType).hash_code();
34  auto numBitsHash = std::hash<size_t>()(nbits_);
35  return util::CombineHashes(typeHash, numBitsHash);
36 }
37 
39 BitType::Kind() const noexcept
40 {
41  return TypeKind::Value;
42 }
43 
44 std::shared_ptr<const BitType>
45 BitType::Create(std::size_t nbits)
46 {
47  static const BitType static_instances[65] = {
48  BitType(0), BitType(1), BitType(2), BitType(3), BitType(4), BitType(5), BitType(6),
49  BitType(7), BitType(8), BitType(9), BitType(10), BitType(11), BitType(12), BitType(13),
50  BitType(14), BitType(15), BitType(16), BitType(17), BitType(18), BitType(19), BitType(20),
51  BitType(21), BitType(22), BitType(23), BitType(24), BitType(25), BitType(26), BitType(27),
52  BitType(28), BitType(29), BitType(30), BitType(31), BitType(32), BitType(33), BitType(34),
53  BitType(35), BitType(36), BitType(37), BitType(38), BitType(39), BitType(40), BitType(41),
54  BitType(42), BitType(43), BitType(44), BitType(45), BitType(46), BitType(47), BitType(48),
55  BitType(49), BitType(50), BitType(51), BitType(52), BitType(53), BitType(54), BitType(55),
56  BitType(56), BitType(57), BitType(58), BitType(59), BitType(60), BitType(61), BitType(62),
57  BitType(63), BitType(64)
58  };
59 
60  if (nbits <= 64)
61  {
62  if (nbits == 0)
63  {
64  throw util::Error("Number of bits must be greater than zero.");
65  }
66 
67  return std::shared_ptr<const BitType>(std::shared_ptr<void>(), &static_instances[nbits]);
68  }
69  else
70  {
71  return std::make_shared<BitType>(nbits);
72  }
73 }
74 
75 }
bool operator==(const jlm::rvsdg::Type &other) const noexcept override
Definition: type.cpp:24
static std::shared_ptr< const BitType > Create(std::size_t nbits)
Creates bit type of specified width.
Definition: type.cpp:45
TypeKind Kind() const noexcept override
Return the kind of this type.
Definition: type.cpp:39
size_t nbits() const noexcept
Definition: type.hpp:26
std::size_t ComputeHash() const noexcept override
Definition: type.cpp:31
~BitType() noexcept override
constexpr BitType(const size_t nbits)
Definition: type.hpp:21
static std::string type(const Node *n)
Definition: view.cpp:255
TypeKind
The kinds of types supported in rvsdg.
Definition: type.hpp:22
@ Value
Designate a value type.
static std::string strfmt(Args... args)
Definition: strfmt.hpp:35
std::size_t CombineHashes(std::size_t hash, Args... args)
Definition: Hash.hpp:63