Merge pull request #1226 from YosysHQ/dave/gzip
[yosys.git] / kernel / yosys.h
1 /* -*- c++ -*-
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20
21 // *** NOTE TO THE READER ***
22 //
23 // Maybe you have just opened this file in the hope to learn more about the
24 // Yosys API. Let me congratulate you on this great decision! ;)
25 //
26 // If you want to know how the design is represented by Yosys in the memory,
27 // you should read "kernel/rtlil.h".
28 //
29 // If you want to know how to register a command with Yosys, you could read
30 // "kernel/register.h", but it would be easier to just look at a simple
31 // example instead. A simple one would be "passes/cmds/log.cc".
32 //
33 // This header is very boring. It just defines some general things that
34 // belong nowhere else and includes the interesting headers.
35 //
36 // Find more information in the "CodingReadme" file.
37
38
39 #ifndef YOSYS_H
40 #define YOSYS_H
41
42 #include <map>
43 #include <set>
44 #include <tuple>
45 #include <vector>
46 #include <string>
47 #include <algorithm>
48 #include <functional>
49 #include <unordered_map>
50 #include <unordered_set>
51 #include <initializer_list>
52 #include <stdexcept>
53 #include <memory>
54 #include <cmath>
55 #include <cstddef>
56
57 #include <sstream>
58 #include <fstream>
59 #include <istream>
60 #include <ostream>
61 #include <iostream>
62
63 #include <stdarg.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <stdint.h>
67 #include <stdio.h>
68 #include <limits.h>
69
70 #ifdef WITH_PYTHON
71 #include <Python.h>
72 #endif
73
74 #ifndef _YOSYS_
75 # error It looks like you are trying to build Yosys without the config defines set. \
76 When building Yosys with a custom make system, make sure you set all the \
77 defines the Yosys Makefile would set for your build configuration.
78 #endif
79
80 #ifdef YOSYS_ENABLE_TCL
81 # include <tcl.h>
82 # ifdef YOSYS_MXE_HACKS
83 extern Tcl_Command Tcl_CreateCommand(Tcl_Interp *interp, const char *cmdName, Tcl_CmdProc *proc, ClientData clientData, Tcl_CmdDeleteProc *deleteProc);
84 extern Tcl_Interp *Tcl_CreateInterp(void);
85 extern void Tcl_DeleteInterp(Tcl_Interp *interp);
86 extern int Tcl_Eval(Tcl_Interp *interp, const char *script);
87 extern int Tcl_EvalFile(Tcl_Interp *interp, const char *fileName);
88 extern void Tcl_Finalize(void);
89 extern int Tcl_GetCommandInfo(Tcl_Interp *interp, const char *cmdName, Tcl_CmdInfo *infoPtr);
90 extern const char *Tcl_GetStringResult(Tcl_Interp *interp);
91 # endif
92 #endif
93
94 #ifdef _WIN32
95 # undef NOMINMAX
96 # define NOMINMAX 1
97 # undef YY_NO_UNISTD_H
98 # define YY_NO_UNISTD_H 1
99
100 # include <windows.h>
101 # include <io.h>
102 # include <direct.h>
103
104 # define strtok_r strtok_s
105 # define strdup _strdup
106 # define snprintf _snprintf
107 # define getcwd _getcwd
108 # define mkdir _mkdir
109 # define popen _popen
110 # define pclose _pclose
111
112 # ifndef __MINGW32__
113 # define PATH_MAX MAX_PATH
114 # define isatty _isatty
115 # define fileno _fileno
116 # endif
117 #endif
118
119 #ifndef PATH_MAX
120 # define PATH_MAX 4096
121 #endif
122
123 #define YOSYS_NAMESPACE Yosys
124 #define PRIVATE_NAMESPACE_BEGIN namespace {
125 #define PRIVATE_NAMESPACE_END }
126 #define YOSYS_NAMESPACE_BEGIN namespace Yosys {
127 #define YOSYS_NAMESPACE_END }
128 #define YOSYS_NAMESPACE_PREFIX Yosys::
129 #define USING_YOSYS_NAMESPACE using namespace Yosys;
130
131 #if __cplusplus >= 201103L
132 # define YS_OVERRIDE override
133 # define YS_FINAL final
134 #else
135 # define YS_OVERRIDE
136 # define YS_FINAL
137 #endif
138
139 #if defined(__GNUC__) || defined(__clang__)
140 # define YS_ATTRIBUTE(...) __attribute__((__VA_ARGS__))
141 # define YS_NORETURN
142 #elif defined(_MSC_VER)
143 # define YS_ATTRIBUTE(...)
144 # define YS_NORETURN __declspec(noreturn)
145 #else
146 # define YS_ATTRIBUTE(...)
147 # define YS_NORETURN
148 #endif
149
150 YOSYS_NAMESPACE_BEGIN
151
152 // Note: All headers included in hashlib.h must be included
153 // outside of YOSYS_NAMESPACE before this or bad things will happen.
154 #ifdef HASHLIB_H
155 # undef HASHLIB_H
156 # include "kernel/hashlib.h"
157 #else
158 # include "kernel/hashlib.h"
159 # undef HASHLIB_H
160 #endif
161
162 using std::vector;
163 using std::string;
164 using std::tuple;
165 using std::pair;
166
167 using std::make_tuple;
168 using std::make_pair;
169 using std::get;
170 using std::min;
171 using std::max;
172
173 // A primitive shared string implementation that does not
174 // move its .c_str() when the object is copied or moved.
175 struct shared_str {
176 std::shared_ptr<string> content;
177 shared_str() { }
178 shared_str(string s) { content = std::shared_ptr<string>(new string(s)); }
179 shared_str(const char *s) { content = std::shared_ptr<string>(new string(s)); }
180 const char *c_str() const { return content->c_str(); }
181 const string &str() const { return *content; }
182 bool operator==(const shared_str &other) const { return *content == *other.content; }
183 unsigned int hash() const { return hashlib::hash_ops<std::string>::hash(*content); }
184 };
185
186 using hashlib::mkhash;
187 using hashlib::mkhash_init;
188 using hashlib::mkhash_add;
189 using hashlib::mkhash_xorshift;
190 using hashlib::hash_ops;
191 using hashlib::hash_cstr_ops;
192 using hashlib::hash_ptr_ops;
193 using hashlib::hash_obj_ops;
194 using hashlib::dict;
195 using hashlib::idict;
196 using hashlib::pool;
197 using hashlib::mfp;
198
199 namespace RTLIL {
200 struct IdString;
201 struct Const;
202 struct SigBit;
203 struct SigSpec;
204 struct Wire;
205 struct Cell;
206 struct Module;
207 struct Design;
208 struct Monitor;
209 }
210
211 namespace AST {
212 struct AstNode;
213 }
214
215 using RTLIL::IdString;
216 using RTLIL::Const;
217 using RTLIL::SigBit;
218 using RTLIL::SigSpec;
219 using RTLIL::Wire;
220 using RTLIL::Cell;
221 using RTLIL::Module;
222 using RTLIL::Design;
223
224 namespace hashlib {
225 template<> struct hash_ops<RTLIL::Wire*> : hash_obj_ops {};
226 template<> struct hash_ops<RTLIL::Cell*> : hash_obj_ops {};
227 template<> struct hash_ops<RTLIL::Module*> : hash_obj_ops {};
228 template<> struct hash_ops<RTLIL::Design*> : hash_obj_ops {};
229 template<> struct hash_ops<RTLIL::Monitor*> : hash_obj_ops {};
230 template<> struct hash_ops<AST::AstNode*> : hash_obj_ops {};
231
232 template<> struct hash_ops<const RTLIL::Wire*> : hash_obj_ops {};
233 template<> struct hash_ops<const RTLIL::Cell*> : hash_obj_ops {};
234 template<> struct hash_ops<const RTLIL::Module*> : hash_obj_ops {};
235 template<> struct hash_ops<const RTLIL::Design*> : hash_obj_ops {};
236 template<> struct hash_ops<const RTLIL::Monitor*> : hash_obj_ops {};
237 template<> struct hash_ops<const AST::AstNode*> : hash_obj_ops {};
238 }
239
240 void memhasher_on();
241 void memhasher_off();
242 void memhasher_do();
243
244 extern bool memhasher_active;
245 inline void memhasher() { if (memhasher_active) memhasher_do(); }
246
247 void yosys_banner();
248 int ceil_log2(int x) YS_ATTRIBUTE(const);
249 std::string stringf(const char *fmt, ...) YS_ATTRIBUTE(format(printf, 1, 2));
250 std::string vstringf(const char *fmt, va_list ap);
251 int readsome(std::istream &f, char *s, int n);
252 std::string next_token(std::string &text, const char *sep = " \t\r\n", bool long_strings = false);
253 std::vector<std::string> split_tokens(const std::string &text, const char *sep = " \t\r\n");
254 bool patmatch(const char *pattern, const char *string);
255 int run_command(const std::string &command, std::function<void(const std::string&)> process_line = std::function<void(const std::string&)>());
256 std::string make_temp_file(std::string template_str = "/tmp/yosys_XXXXXX");
257 std::string make_temp_dir(std::string template_str = "/tmp/yosys_XXXXXX");
258 bool check_file_exists(std::string filename, bool is_exec = false);
259 bool is_absolute_path(std::string filename);
260 void remove_directory(std::string dirname);
261 std::string escape_filename_spaces(const std::string& filename);
262
263 template<typename T> int GetSize(const T &obj) { return obj.size(); }
264 int GetSize(RTLIL::Wire *wire);
265
266 extern int autoidx;
267 extern int yosys_xtrace;
268
269 YOSYS_NAMESPACE_END
270
271 #include "kernel/log.h"
272 #include "kernel/rtlil.h"
273 #include "kernel/register.h"
274
275 YOSYS_NAMESPACE_BEGIN
276
277 using RTLIL::State;
278 using RTLIL::SigChunk;
279 using RTLIL::SigSig;
280
281 namespace hashlib {
282 template<> struct hash_ops<RTLIL::State> : hash_ops<int> {};
283 }
284
285 void yosys_setup();
286
287 #ifdef WITH_PYTHON
288 bool yosys_already_setup();
289 #endif
290
291 void yosys_shutdown();
292
293 #ifdef YOSYS_ENABLE_TCL
294 Tcl_Interp *yosys_get_tcl_interp();
295 #endif
296
297 extern RTLIL::Design *yosys_design;
298
299 RTLIL::IdString new_id(std::string file, int line, std::string func);
300
301 #define NEW_ID \
302 YOSYS_NAMESPACE_PREFIX new_id(__FILE__, __LINE__, __FUNCTION__)
303
304 #define ID(_str) \
305 ([]() { static YOSYS_NAMESPACE_PREFIX RTLIL::IdString _id(_str); return _id; })()
306
307 RTLIL::Design *yosys_get_design();
308 std::string proc_self_dirname();
309 std::string proc_share_dirname();
310 const char *create_prompt(RTLIL::Design *design, int recursion_counter);
311 std::vector<std::string> glob_filename(const std::string &filename_pattern);
312 void rewrite_filename(std::string &filename);
313
314 void run_pass(std::string command, RTLIL::Design *design = nullptr);
315 void run_frontend(std::string filename, std::string command, std::string *backend_command, std::string *from_to_label = nullptr, RTLIL::Design *design = nullptr);
316 void run_frontend(std::string filename, std::string command, RTLIL::Design *design = nullptr);
317 void run_backend(std::string filename, std::string command, RTLIL::Design *design = nullptr);
318 void shell(RTLIL::Design *design);
319
320 // journal of all input and output files read (for "yosys -E")
321 extern std::set<std::string> yosys_input_files, yosys_output_files;
322
323 // from kernel/version_*.o (cc source generated from Makefile)
324 extern const char *yosys_version_str;
325
326 // from passes/cmds/design.cc
327 extern std::map<std::string, RTLIL::Design*> saved_designs;
328 extern std::vector<RTLIL::Design*> pushed_designs;
329
330 // from passes/cmds/pluginc.cc
331 extern std::map<std::string, void*> loaded_plugins;
332 #ifdef WITH_PYTHON
333 extern std::map<std::string, void*> loaded_python_plugins;
334 #endif
335 extern std::map<std::string, std::string> loaded_plugin_aliases;
336 void load_plugin(std::string filename, std::vector<std::string> aliases);
337
338 YOSYS_NAMESPACE_END
339
340 #endif