HAL  v4.5.0-259-g577210b12
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
utils.cpp
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 // Copyright (c) 2025-2026 Sascha Tommasone. All rights reserved.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
26 
28 
29 #include "hal_core/defines.h"
31 #include "hal_core/netlist/gate.h"
35 
36 #include <charconv>
37 #include <cmath>
38 #include <locale>
39 #include <sstream>
40 
41 namespace hal::cte
42 {
43  bool is_ff( const Gate *gate )
44  {
45  return gate->get_type()->has_property( GateTypeProperty::ff );
46  }
47 
48  bool is_latch( const Gate *gate )
49  {
51  }
52 
53  bool is_buffer( const Gate *gate )
54  {
56  }
57 
58  bool is_inverter( const Gate *gate )
59  {
61  }
62 
63  bool is_delay( const Gate *gate )
64  {
66  }
67 
68  bool is_mux( const Gate *gate )
69  {
71  }
72 
73  bool is_oscillator( const Gate *gate )
74  {
76  }
77 
78  bool is_pll( const Gate *gate )
79  {
80  return gate->get_type()->has_property( GateTypeProperty::pll );
81  }
82 
83  bool is_control_pin( PinType pin_type )
84  {
85  return pin_type == PinType::clock || pin_type == PinType::enable || pin_type == PinType::select
86  || pin_type == PinType::set || pin_type == PinType::reset;
87  }
88 
89  bool is_connected_to_control_pin( const Endpoint *endpoint )
90  {
91  return is_control_pin( endpoint->get_pin()->get_type() );
92  }
93 
94  std::optional<double> read_parameter_coordinate( const Gate *gate, const std::string &name )
95  {
96  const Result<std::string> parameter = gate->get_parameter_value( name );
97  if( parameter.is_error() )
98  {
99  return std::nullopt;
100  }
101 
102  const std::string &value = parameter.get();
103  const std::size_t first = value.find_first_not_of( " \t\r\n" );
104  if( first == std::string::npos )
105  {
106  return std::nullopt;
107  }
108  const std::string text = value.substr( first, value.find_last_not_of( " \t\r\n" ) - first + 1 );
109 
110  // bit vectors are stored in hexadecimal, which not every standard library reads as a double
111  if( text.rfind( "0x", 0 ) == 0 )
112  {
113  u64 coordinate = 0;
114  if( const auto [end, error] = std::from_chars( text.data() + 2, text.data() + text.size(), coordinate, 16 );
115  error != std::errc() || end != text.data() + text.size() )
116  {
117  return std::nullopt;
118  }
119  return static_cast<double>( coordinate );
120  }
121 
122  // read with the classic locale, so a coordinate has a decimal point no matter where the program runs
123  std::istringstream stream( text );
124  stream.imbue( std::locale::classic() );
125 
126  double coordinate = 0.0;
127  stream >> coordinate;
128 
129  // the whole value must be the number, "12 um" is not a coordinate
130  if( stream.fail() || !stream.eof() || !std::isfinite( coordinate ) )
131  {
132  return std::nullopt;
133  }
134  return coordinate;
135  }
136 } // namespace hal::cte
PinType get_type() const
Definition: base_pin.h:150
Result< std::string > get_parameter_value(const std::string &name) const
GatePin * get_pin() const
Definition: endpoint.cpp:28
Definition: gate.h:59
GateType * get_type() const
Definition: gate.cpp:162
bool has_property(GateTypeProperty property) const
Definition: gate_type.cpp:101
const T & get() const
Definition: result.h:166
bool is_error() const
Definition: result.h:153
uint64_t u64
Definition: defines.h:42
bool is_delay(const Gate *gate)
Definition: utils.cpp:63
bool is_mux(const Gate *gate)
Definition: utils.cpp:68
bool is_connected_to_control_pin(const Endpoint *endpoint)
Definition: utils.cpp:89
bool is_control_pin(PinType pin_type)
Definition: utils.cpp:83
bool is_inverter(const Gate *gate)
Definition: utils.cpp:58
bool is_buffer(const Gate *gate)
Definition: utils.cpp:53
bool is_latch(const Gate *gate)
Definition: utils.cpp:48
bool is_pll(const Gate *gate)
Definition: utils.cpp:78
std::optional< double > read_parameter_coordinate(const Gate *gate, const std::string &name)
Definition: utils.cpp:94
bool is_ff(const Gate *gate)
Definition: utils.cpp:43
bool is_oscillator(const Gate *gate)
Definition: utils.cpp:73
PinType
Definition: pin_type.h:36
std::string name