HAL
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 {
47  {
48  public:
49  LibertyParser() = default;
50  ~LibertyParser() = default;
51 
68  Result<std::unique_ptr<GateLibrary>> parse(const std::filesystem::path& file_path) override;
69 
70  private:
71  struct type_group
72  {
73  u32 line_number;
74  std::string name;
75  bool ascending = false;
76  u32 start_index = 0;
77  u32 width;
78  };
79 
80  struct pin_group
81  {
82  u32 line_number;
83  std::vector<std::string> pin_names;
86  std::string function;
87  std::string x_function;
88  std::string z_function;
89  bool clock = false;
90  bool power = false;
91  bool ground = false;
92  };
93 
94  struct bus_group
95  {
96  u32 line_number;
97  std::string name;
99  std::vector<std::string> pin_names;
100  std::vector<pin_group> pins;
101  bool ascending = false;
102  u32 start_index = 0;
103  std::unordered_map<u32, std::string> index_to_pin;
104  };
105 
106  struct ff_group
107  {
108  u32 line_number;
109  std::string state1, state2;
110  std::string clocked_on;
111  std::string next_state;
112  std::string clear;
113  std::string preset;
114  AsyncSetResetBehavior special_behavior_var1 = AsyncSetResetBehavior::undef;
115  AsyncSetResetBehavior special_behavior_var2 = AsyncSetResetBehavior::undef;
116  };
117 
118  struct latch_group
119  {
120  u32 line_number;
121  std::string state1, state2;
122  std::string enable;
123  std::string data_in;
124  std::string clear;
125  std::string preset;
126  AsyncSetResetBehavior special_behavior_var1 = AsyncSetResetBehavior::undef;
127  AsyncSetResetBehavior special_behavior_var2 = AsyncSetResetBehavior::undef;
128  };
129 
130  struct lut_group
131  {
132  u32 line_number;
133  std::string name;
134  std::string data_category;
135  std::string data_identifier;
136  std::string data_direction;
137  };
138 
139  struct cell_group
140  {
141  u32 line_number;
142  std::string name;
143  std::set<GateTypeProperty> properties;
144  std::optional<ff_group> ff;
145  std::optional<latch_group> latch;
146  std::optional<lut_group> lut;
147  std::vector<pin_group> pins;
148  std::map<std::string, bus_group> buses;
149  std::set<std::string> pin_names;
150  };
151 
152  std::unique_ptr<GateLibrary> m_gate_lib;
153  std::stringstream m_fs;
154  std::filesystem::path m_path;
155 
156  TokenStream<std::string> m_token_stream;
157  std::map<std::string, type_group> m_bus_types;
158  std::set<std::string> m_cell_names;
159 
160  void tokenize();
161  Result<std::monostate> parse_tokens();
162 
163  Result<cell_group> parse_cell(TokenStream<std::string>& library_stream);
164  Result<type_group> parse_type(TokenStream<std::string>& str);
165  Result<pin_group> parse_pin(TokenStream<std::string>& str, cell_group& cell, PinDirection direction = PinDirection::none, const std::string& external_pin_name = "");
166  Result<pin_group> parse_pg_pin(TokenStream<std::string>& str, cell_group& cell);
167  Result<bus_group> parse_bus(TokenStream<std::string>& str, cell_group& cell);
168  Result<ff_group> parse_ff(TokenStream<std::string>& str);
169  Result<latch_group> parse_latch(TokenStream<std::string>& str);
170  Result<std::monostate> construct_gate_type(cell_group&& cell);
171 
172  void remove_comments(std::string& line, bool& multi_line_comment);
173  std::vector<std::string> tokenize_function(const std::string& function);
174  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);
175  std::string prepare_pin_function(const std::map<std::string, bus_group>& buses, const std::string& function);
176  Result<std::unordered_map<std::string, BooleanFunction>> construct_bus_functions(const cell_group& cell);
177  };
178 } // namespace hal
#define NETLIST_API
Definition: arch_linux.h:30
LibertyParser()=default
~LibertyParser()=default
std::unique_ptr< GateLibrary > parse(std::filesystem::path file_path)
PinDirection
Definition: pin_direction.h:36
PinType
Definition: pin_type.h:36
quint32 u32
PinType type
std::vector< PinInformation > pins
u32 start_index
bool ascending
PinDirection direction
std::string name