Merge pull request #3067 from YosysHQ/aki/ci_update
[yosys.git] / kernel / yosys.h
1 /* -*- c++ -*-
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
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 "guidelines/GettingStarted" 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 extern Tcl_Obj *Tcl_NewStringObj(const char *bytes, int length);
92 extern Tcl_Obj *Tcl_NewIntObj(int intValue);
93 extern Tcl_Obj *Tcl_NewListObj(int objc, Tcl_Obj *const objv[]);
94 extern Tcl_Obj *Tcl_ObjSetVar2(Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr, Tcl_Obj *newValuePtr, int flags);
95 # endif
96 # undef CONST
97 # undef INLINE
98 #endif
99
100 #ifdef _WIN32
101 # undef NOMINMAX
102 # define NOMINMAX 1
103 # undef YY_NO_UNISTD_H
104 # define YY_NO_UNISTD_H 1
105
106 # include <windows.h>
107 # include <io.h>
108 # include <direct.h>
109
110 # define strtok_r strtok_s
111 # define strdup _strdup
112 # define snprintf _snprintf
113 # define getcwd _getcwd
114 # define mkdir _mkdir
115 # define popen _popen
116 # define pclose _pclose
117
118 # ifndef __MINGW32__
119 # define PATH_MAX MAX_PATH
120 # define isatty _isatty
121 # define fileno _fileno
122 # endif
123
124 // The following defines conflict with our identifiers:
125 # undef CONST
126 // `wingdi.h` defines a TRANSPARENT macro that conflicts with X(TRANSPARENT) entry in kernel/constids.inc
127 # undef TRANSPARENT
128 #endif
129
130 #ifndef PATH_MAX
131 # define PATH_MAX 4096
132 #endif
133
134 #define YOSYS_NAMESPACE Yosys
135 #define PRIVATE_NAMESPACE_BEGIN namespace {
136 #define PRIVATE_NAMESPACE_END }
137 #define YOSYS_NAMESPACE_BEGIN namespace Yosys {
138 #define YOSYS_NAMESPACE_END }
139 #define YOSYS_NAMESPACE_PREFIX Yosys::
140 #define USING_YOSYS_NAMESPACE using namespace Yosys;
141
142 #if defined(__GNUC__) || defined(__clang__)
143 # define YS_ATTRIBUTE(...) __attribute__((__VA_ARGS__))
144 #elif defined(_MSC_VER)
145 # define YS_ATTRIBUTE(...)
146 #else
147 # define YS_ATTRIBUTE(...)
148 #endif
149
150 #if __cplusplus >= 201703L
151 # define YS_MAYBE_UNUSED [[maybe_unused]]
152 #elif defined(__GNUC__) || defined(__clang__)
153 # define YS_MAYBE_UNUSED __attribute__((__unused__))
154 #else
155 # define YS_MAYBE_UNUSED
156 #endif
157
158 #if __cplusplus >= 201703L
159 # define YS_FALLTHROUGH [[fallthrough]];
160 #elif defined(__clang__)
161 # define YS_FALLTHROUGH [[clang::fallthrough]];
162 #elif defined(__GNUC__)
163 # define YS_FALLTHROUGH [[gnu::fallthrough]];
164 #else
165 # define YS_FALLTHROUGH
166 #endif
167
168 YOSYS_NAMESPACE_BEGIN
169
170 // Note: All headers included in hashlib.h must be included
171 // outside of YOSYS_NAMESPACE before this or bad things will happen.
172 #ifdef HASHLIB_H
173 # undef HASHLIB_H
174 # include "kernel/hashlib.h"
175 #else
176 # include "kernel/hashlib.h"
177 # undef HASHLIB_H
178 #endif
179
180 using std::vector;
181 using std::string;
182 using std::tuple;
183 using std::pair;
184
185 using std::make_tuple;
186 using std::make_pair;
187 using std::get;
188 using std::min;
189 using std::max;
190
191 // A primitive shared string implementation that does not
192 // move its .c_str() when the object is copied or moved.
193 struct shared_str {
194 std::shared_ptr<string> content;
195 shared_str() { }
196 shared_str(string s) { content = std::shared_ptr<string>(new string(s)); }
197 shared_str(const char *s) { content = std::shared_ptr<string>(new string(s)); }
198 const char *c_str() const { return content->c_str(); }
199 const string &str() const { return *content; }
200 bool operator==(const shared_str &other) const { return *content == *other.content; }
201 unsigned int hash() const { return hashlib::hash_ops<std::string>::hash(*content); }
202 };
203
204 using hashlib::mkhash;
205 using hashlib::mkhash_init;
206 using hashlib::mkhash_add;
207 using hashlib::mkhash_xorshift;
208 using hashlib::hash_ops;
209 using hashlib::hash_cstr_ops;
210 using hashlib::hash_ptr_ops;
211 using hashlib::hash_obj_ops;
212 using hashlib::dict;
213 using hashlib::idict;
214 using hashlib::pool;
215 using hashlib::mfp;
216
217 namespace RTLIL {
218 struct IdString;
219 struct Const;
220 struct SigBit;
221 struct SigSpec;
222 struct Wire;
223 struct Cell;
224 struct Memory;
225 struct Process;
226 struct Module;
227 struct Design;
228 struct Monitor;
229 enum State : unsigned char;
230 }
231
232 namespace AST {
233 struct AstNode;
234 }
235
236 using RTLIL::IdString;
237 using RTLIL::Const;
238 using RTLIL::SigBit;
239 using RTLIL::SigSpec;
240 using RTLIL::Wire;
241 using RTLIL::Cell;
242 using RTLIL::Module;
243 using RTLIL::Design;
244
245 namespace hashlib {
246 template<> struct hash_ops<RTLIL::Wire*> : hash_obj_ops {};
247 template<> struct hash_ops<RTLIL::Cell*> : hash_obj_ops {};
248 template<> struct hash_ops<RTLIL::Memory*> : hash_obj_ops {};
249 template<> struct hash_ops<RTLIL::Process*> : hash_obj_ops {};
250 template<> struct hash_ops<RTLIL::Module*> : hash_obj_ops {};
251 template<> struct hash_ops<RTLIL::Design*> : hash_obj_ops {};
252 template<> struct hash_ops<RTLIL::Monitor*> : hash_obj_ops {};
253 template<> struct hash_ops<AST::AstNode*> : hash_obj_ops {};
254
255 template<> struct hash_ops<const RTLIL::Wire*> : hash_obj_ops {};
256 template<> struct hash_ops<const RTLIL::Cell*> : hash_obj_ops {};
257 template<> struct hash_ops<const RTLIL::Memory*> : hash_obj_ops {};
258 template<> struct hash_ops<const RTLIL::Process*> : hash_obj_ops {};
259 template<> struct hash_ops<const RTLIL::Module*> : hash_obj_ops {};
260 template<> struct hash_ops<const RTLIL::Design*> : hash_obj_ops {};
261 template<> struct hash_ops<const RTLIL::Monitor*> : hash_obj_ops {};
262 template<> struct hash_ops<const AST::AstNode*> : hash_obj_ops {};
263 }
264
265 void memhasher_on();
266 void memhasher_off();
267 void memhasher_do();
268
269 extern bool memhasher_active;
270 inline void memhasher() { if (memhasher_active) memhasher_do(); }
271
272 void yosys_banner();
273 int ceil_log2(int x) YS_ATTRIBUTE(const);
274 std::string stringf(const char *fmt, ...) YS_ATTRIBUTE(format(printf, 1, 2));
275 std::string vstringf(const char *fmt, va_list ap);
276 int readsome(std::istream &f, char *s, int n);
277 std::string next_token(std::string &text, const char *sep = " \t\r\n", bool long_strings = false);
278 std::vector<std::string> split_tokens(const std::string &text, const char *sep = " \t\r\n");
279 bool patmatch(const char *pattern, const char *string);
280 #if !defined(YOSYS_DISABLE_SPAWN)
281 int run_command(const std::string &command, std::function<void(const std::string&)> process_line = std::function<void(const std::string&)>());
282 #endif
283 std::string make_temp_file(std::string template_str = "/tmp/yosys_XXXXXX");
284 std::string make_temp_dir(std::string template_str = "/tmp/yosys_XXXXXX");
285 bool check_file_exists(std::string filename, bool is_exec = false);
286 bool is_absolute_path(std::string filename);
287 void remove_directory(std::string dirname);
288 std::string escape_filename_spaces(const std::string& filename);
289
290 template<typename T> int GetSize(const T &obj) { return obj.size(); }
291 int GetSize(RTLIL::Wire *wire);
292
293 extern int autoidx;
294 extern int yosys_xtrace;
295
296 YOSYS_NAMESPACE_END
297
298 #include "kernel/log.h"
299 #include "kernel/rtlil.h"
300 #include "kernel/register.h"
301
302 YOSYS_NAMESPACE_BEGIN
303
304 using RTLIL::State;
305 using RTLIL::SigChunk;
306 using RTLIL::SigSig;
307
308 namespace hashlib {
309 template<> struct hash_ops<RTLIL::State> : hash_ops<int> {};
310 }
311
312 void yosys_setup();
313
314 #ifdef WITH_PYTHON
315 bool yosys_already_setup();
316 #endif
317
318 void yosys_shutdown();
319
320 #ifdef YOSYS_ENABLE_TCL
321 Tcl_Interp *yosys_get_tcl_interp();
322 #endif
323
324 extern RTLIL::Design *yosys_design;
325
326 RTLIL::IdString new_id(std::string file, int line, std::string func);
327
328 #define NEW_ID \
329 YOSYS_NAMESPACE_PREFIX new_id(__FILE__, __LINE__, __FUNCTION__)
330
331 // Create a statically allocated IdString object, using for example ID::A or ID($add).
332 //
333 // Recipe for Converting old code that is using conversion of strings like ID::A and
334 // "$add" for creating IdStrings: Run below SED command on the .cc file and then use for
335 // example "meld foo.cc foo.cc.orig" to manually compile errors, if necessary.
336 //
337 // sed -i.orig -r 's/"\\\\([a-zA-Z0-9_]+)"/ID(\1)/g; s/"(\$[a-zA-Z0-9_]+)"/ID(\1)/g;' <filename>
338 //
339 #define ID(_id) ([]() { const char *p = "\\" #_id, *q = p[1] == '$' ? p+1 : p; \
340 static const YOSYS_NAMESPACE_PREFIX RTLIL::IdString id(q); return id; })()
341 namespace ID = RTLIL::ID;
342
343 RTLIL::Design *yosys_get_design();
344 std::string proc_self_dirname();
345 std::string proc_share_dirname();
346 std::string proc_program_prefix();
347 const char *create_prompt(RTLIL::Design *design, int recursion_counter);
348 std::vector<std::string> glob_filename(const std::string &filename_pattern);
349 void rewrite_filename(std::string &filename);
350
351 void run_pass(std::string command, RTLIL::Design *design = nullptr);
352 void run_frontend(std::string filename, std::string command, std::string *backend_command, std::string *from_to_label = nullptr, RTLIL::Design *design = nullptr);
353 void run_frontend(std::string filename, std::string command, RTLIL::Design *design = nullptr);
354 void run_backend(std::string filename, std::string command, RTLIL::Design *design = nullptr);
355 void shell(RTLIL::Design *design);
356
357 // journal of all input and output files read (for "yosys -E")
358 extern std::set<std::string> yosys_input_files, yosys_output_files;
359
360 // from kernel/version_*.o (cc source generated from Makefile)
361 extern const char *yosys_version_str;
362
363 // from passes/cmds/design.cc
364 extern std::map<std::string, RTLIL::Design*> saved_designs;
365 extern std::vector<RTLIL::Design*> pushed_designs;
366
367 // from passes/cmds/pluginc.cc
368 extern std::map<std::string, void*> loaded_plugins;
369 #ifdef WITH_PYTHON
370 extern std::map<std::string, void*> loaded_python_plugins;
371 #endif
372 extern std::map<std::string, std::string> loaded_plugin_aliases;
373 void load_plugin(std::string filename, std::vector<std::string> aliases);
374
375 extern std::string yosys_share_dirname;
376 extern std::string yosys_abc_executable;
377
378 YOSYS_NAMESPACE_END
379
380 #endif