Added "yosys -X"
[yosys.git] / kernel / yosys.h
1 /*
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 <vector>
45 #include <string>
46 #include <algorithm>
47 #include <functional>
48 #include <unordered_map>
49 #include <unordered_set>
50 #include <initializer_list>
51 #include <stdexcept>
52
53 #include <sstream>
54 #include <fstream>
55 #include <istream>
56 #include <ostream>
57 #include <iostream>
58
59 #include <stdarg.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <stdint.h>
63 #include <stdio.h>
64
65 #ifndef _YOSYS_
66 # error It looks like you are trying to build Yosys without the config defines set. \
67 When building Yosys with a custom make system, make sure you set all the \
68 defines the Yosys Makefile would set for your build configuration.
69 #endif
70
71 #ifdef YOSYS_ENABLE_TCL
72 # include <tcl.h>
73 #endif
74
75 #ifdef _WIN32
76 # undef NOMINMAX
77 # define NOMINMAX 1
78 # undef YY_NO_UNISTD_H
79 # define YY_NO_UNISTD_H 1
80
81 # include <windows.h>
82 # include <io.h>
83 # include <direct.h>
84
85 # define strtok_r strtok_s
86 # define strdup _strdup
87 # define snprintf _snprintf
88 # define getcwd _getcwd
89 # define mkdir _mkdir
90 # define popen _popen
91 # define pclose _pclose
92 # define PATH_MAX MAX_PATH
93
94 # ifndef __MINGW32__
95 # define isatty _isatty
96 # define fileno _fileno
97 # endif
98 #endif
99
100 #define PRIVATE_NAMESPACE_BEGIN namespace {
101 #define PRIVATE_NAMESPACE_END }
102 #define YOSYS_NAMESPACE_BEGIN namespace Yosys {
103 #define YOSYS_NAMESPACE_END }
104 #define YOSYS_NAMESPACE_PREFIX Yosys::
105 #define USING_YOSYS_NAMESPACE using namespace Yosys;
106
107 #if __cplusplus >= 201103L
108 # define YS_OVERRIDE override
109 # define YS_FINAL final
110 #else
111 # define YS_OVERRIDE
112 # define YS_FINAL
113 #endif
114
115 #if defined(__GNUC__) || defined(__clang__)
116 # define YS_ATTRIBUTE(...) __attribute__((__VA_ARGS__))
117 # define YS_NORETURN
118 #elif defined(_MSC_VER)
119 # define YS_ATTRIBUTE(...)
120 # define YS_NORETURN __declspec(noreturn)
121 #else
122 # define YS_ATTRIBUTE(...)
123 # define YS_NORETURN
124 #endif
125
126 YOSYS_NAMESPACE_BEGIN
127
128 // Note: All headers included in hashlib.h must be included
129 // outside of YOSYS_NAMESPACE before this or bad things will happen.
130 #ifdef HASHLIB_H
131 # undef HASHLIB_H
132 # include "kernel/hashlib.h"
133 #else
134 # include "kernel/hashlib.h"
135 # undef HASHLIB_H
136 #endif
137
138 using std::vector;
139 using std::string;
140 using hashlib::mkhash;
141 using hashlib::mkhash_add;
142 using hashlib::mkhash_xorshift;
143 using hashlib::hash_ops;
144 using hashlib::hash_cstr_ops;
145 using hashlib::hash_ptr_ops;
146 using hashlib::hash_obj_ops;
147 using hashlib::dict;
148 using hashlib::pool;
149
150 namespace RTLIL {
151 struct IdString;
152 struct Const;
153 struct SigBit;
154 struct SigSpec;
155 struct Wire;
156 struct Cell;
157 struct Module;
158 struct Design;
159 struct Monitor;
160 }
161
162 namespace AST {
163 struct AstNode;
164 }
165
166 using RTLIL::IdString;
167 using RTLIL::Const;
168 using RTLIL::SigBit;
169 using RTLIL::SigSpec;
170 using RTLIL::Wire;
171 using RTLIL::Cell;
172 using RTLIL::Module;
173 using RTLIL::Design;
174
175 namespace hashlib {
176 template<> struct hash_ops<RTLIL::Wire*> : hash_obj_ops {};
177 template<> struct hash_ops<RTLIL::Cell*> : hash_obj_ops {};
178 template<> struct hash_ops<RTLIL::Module*> : hash_obj_ops {};
179 template<> struct hash_ops<RTLIL::Design*> : hash_obj_ops {};
180 template<> struct hash_ops<RTLIL::Monitor*> : hash_obj_ops {};
181 template<> struct hash_ops<AST::AstNode*> : hash_obj_ops {};
182
183 template<> struct hash_ops<const RTLIL::Wire*> : hash_obj_ops {};
184 template<> struct hash_ops<const RTLIL::Cell*> : hash_obj_ops {};
185 template<> struct hash_ops<const RTLIL::Module*> : hash_obj_ops {};
186 template<> struct hash_ops<const RTLIL::Design*> : hash_obj_ops {};
187 template<> struct hash_ops<const RTLIL::Monitor*> : hash_obj_ops {};
188 template<> struct hash_ops<const AST::AstNode*> : hash_obj_ops {};
189 }
190
191 void memhasher_on();
192 void memhasher_off();
193 void memhasher_do();
194
195 extern bool memhasher_active;
196 inline void memhasher() { if (memhasher_active) memhasher_do(); }
197
198 std::string stringf(const char *fmt, ...) YS_ATTRIBUTE(format(printf, 1, 2));
199 std::string vstringf(const char *fmt, va_list ap);
200 int readsome(std::istream &f, char *s, int n);
201 std::string next_token(std::string &text, const char *sep);
202 bool patmatch(const char *pattern, const char *string);
203 int run_command(const std::string &command, std::function<void(const std::string&)> process_line = std::function<void(const std::string&)>());
204 std::string make_temp_file(std::string template_str = "/tmp/yosys_XXXXXX");
205 std::string make_temp_dir(std::string template_str = "/tmp/yosys_XXXXXX");
206 bool check_file_exists(std::string filename, bool is_exec = false);
207 void remove_directory(std::string dirname);
208
209 template<typename T> int GetSize(const T &obj) { return obj.size(); }
210 int GetSize(RTLIL::Wire *wire);
211
212 extern int autoidx;
213 extern int yosys_xtrace;
214
215 YOSYS_NAMESPACE_END
216
217 #include "kernel/log.h"
218 #include "kernel/rtlil.h"
219 #include "kernel/register.h"
220
221 YOSYS_NAMESPACE_BEGIN
222
223 void yosys_setup();
224 void yosys_shutdown();
225
226 #ifdef YOSYS_ENABLE_TCL
227 Tcl_Interp *yosys_get_tcl_interp();
228 #endif
229
230 extern RTLIL::Design *yosys_design;
231
232 RTLIL::IdString new_id(std::string file, int line, std::string func);
233
234 #define NEW_ID \
235 YOSYS_NAMESPACE_PREFIX new_id(__FILE__, __LINE__, __FUNCTION__)
236
237 #define ID(_str) \
238 ([]() { static YOSYS_NAMESPACE_PREFIX RTLIL::IdString _id(_str); return _id; })()
239
240 RTLIL::Design *yosys_get_design();
241 std::string proc_self_dirname();
242 std::string proc_share_dirname();
243 const char *create_prompt(RTLIL::Design *design, int recursion_counter);
244
245 void run_frontend(std::string filename, std::string command, RTLIL::Design *design, std::string *backend_command, std::string *from_to_label);
246 void run_pass(std::string command, RTLIL::Design *design);
247 void run_backend(std::string filename, std::string command, RTLIL::Design *design);
248 void shell(RTLIL::Design *design);
249
250 // from kernel/version_*.o (cc source generated from Makefile)
251 extern const char *yosys_version_str;
252
253 // from passes/cmds/design.cc
254 extern std::map<std::string, RTLIL::Design*> saved_designs;
255 extern std::vector<RTLIL::Design*> pushed_designs;
256
257 // from passes/cmds/pluginc.cc
258 extern std::map<std::string, void*> loaded_plugins;
259 extern std::map<std::string, std::string> loaded_plugin_aliases;
260 void load_plugin(std::string filename, std::vector<std::string> aliases);
261
262 YOSYS_NAMESPACE_END
263
264 #endif