HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
liberty_parser.h
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 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 #include "hal_core/defines.h"
35 
36 #include <filesystem>
37 #include <map>
38 #include <optional>
39 #include <unordered_map>
40 
41 namespace hal
42 {
49  {
50  public:
51  LibertyParser() = default;
52  ~LibertyParser() = default;
53 
72  Result<std::unique_ptr<GateLibrary>> parse(const std::filesystem::path& file_path) override;
73 
74  private:
78  struct type_group
79  {
80  u32 line_number;
81  std::string name;
82  bool ascending = false;
83  u32 start_index = 0;
84  u32 width;
85  };
86 
90  struct pin_group
91  {
92  u32 line_number;
93  std::vector<std::string> pin_names;
96  std::string function;
97  std::string x_function;
98  std::string z_function;
99  bool clock = false;
100  bool power = false;
101  bool ground = false;
102  };
103 
107  struct bus_group
108  {
109  u32 line_number;
110  std::string name;
112  std::vector<std::string> pin_names;
113  std::vector<pin_group> pins;
114  bool ascending = false;
115  u32 start_index = 0;
116  std::unordered_map<u32, std::string> index_to_pin;
117  };
118 
122  struct ff_group
123  {
124  u32 line_number;
125  std::string state1, state2;
126  std::string clocked_on;
127  std::string next_state;
128  std::string clear;
129  std::string preset;
130  AsyncSetResetBehavior special_behavior_var1 = AsyncSetResetBehavior::undef;
131  AsyncSetResetBehavior special_behavior_var2 = AsyncSetResetBehavior::undef;
132  };
133 
137  struct latch_group
138  {
139  u32 line_number;
140  std::string state1, state2;
141  std::string enable;
142  std::string data_in;
143  std::string clear;
144  std::string preset;
145  AsyncSetResetBehavior special_behavior_var1 = AsyncSetResetBehavior::undef;
146  AsyncSetResetBehavior special_behavior_var2 = AsyncSetResetBehavior::undef;
147  };
148 
152  struct lut_group
153  {
154  u32 line_number;
155  std::string name;
156  std::string data_category;
157  std::string data_identifier;
158  std::string data_direction;
159  };
160 
164  struct cell_group
165  {
166  u32 line_number;
167  std::string name;
168  std::set<GateTypeProperty> properties;
169  std::optional<ff_group> ff;
170  std::optional<latch_group> latch;
171  std::optional<lut_group> lut;
172  std::vector<pin_group> pins;
173  std::map<std::string, bus_group> buses;
174  std::set<std::string> pin_names;
175  };
176 
177  std::unique_ptr<GateLibrary> m_gate_lib;
178  std::stringstream m_fs;
179  std::filesystem::path m_path;
180 
181  TokenStream<std::string> m_token_stream;
182  std::map<std::string, type_group> m_bus_types;
183  std::set<std::string> m_cell_names;
184 
185  void tokenize();
186  Result<std::monostate> parse_tokens();
187 
188  Result<cell_group> parse_cell(TokenStream<std::string>& library_stream);
189  Result<type_group> parse_type(TokenStream<std::string>& str);
190  Result<pin_group> parse_pin(TokenStream<std::string>& str, cell_group& cell, PinDirection direction = PinDirection::none, const std::string& external_pin_name = "");
191  Result<pin_group> parse_pg_pin(TokenStream<std::string>& str, cell_group& cell);
192  Result<bus_group> parse_bus(TokenStream<std::string>& str, cell_group& cell);
193  Result<ff_group> parse_ff(TokenStream<std::string>& str);
194  Result<latch_group> parse_latch(TokenStream<std::string>& str);
195  Result<std::monostate> construct_gate_type(cell_group&& cell);
196 
197  void remove_comments(std::string& line, bool& multi_line_comment);
198  std::vector<std::string> tokenize_function(const std::string& function);
199  std::map<std::string, std::string> expand_bus_function(const std::map<std::string, bus_group>& buses, const std::vector<std::string>& pin_names, const std::string& function);
200  std::string prepare_pin_function(const std::map<std::string, bus_group>& buses, const std::string& function);
201  Result<std::unordered_map<std::string, BooleanFunction>> construct_bus_functions(const cell_group& cell);
202  };
203 } // namespace hal
#define NETLIST_API
Definition: arch_linux.h:30
LibertyParser()=default
~LibertyParser()=default
uint32_t u32
Definition: defines.h:41
std::unique_ptr< GateLibrary > parse(std::filesystem::path file_path)
Definition: defines.h:45
PinDirection
Definition: pin_direction.h:36
PinType
Definition: pin_type.h:36
PinType type
std::vector< PinInformation > pins
u32 start_index
bool ascending
PinDirection direction
std::string name