Merge pull request #680 from jburgess777/fix-empty-string-back-assert
[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
56 #include <sstream>
57 #include <fstream>
58 #include <istream>
59 #include <ostream>
60 #include <iostream>
61
62 #include <stdarg.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <stdint.h>
66 #include <stdio.h>
67 #include <limits.h>
68
69 #ifndef _YOSYS_
70 # error It looks like you are trying to build Yosys without the config defines set. \
71 When building Yosys with a custom make system, make sure you set all the \
72 defines the Yosys Makefile would set for your build configuration.
73 #endif
74
75 #ifdef YOSYS_ENABLE_TCL
76 # include <tcl.h>
77 # ifdef YOSYS_MXE_HACKS
78 extern Tcl_Command Tcl_CreateCommand(Tcl_Interp *interp, const char *cmdName, Tcl_CmdProc *proc, ClientData clientData, Tcl_CmdDeleteProc *deleteProc);
79 extern Tcl_Interp *Tcl_CreateInterp(void);
80 extern void Tcl_DeleteInterp(Tcl_Interp *interp);
81 extern int Tcl_Eval(Tcl_Interp *interp, const char *script);
82 extern int Tcl_EvalFile(Tcl_Interp *interp, const char *fileName);
83 extern void Tcl_Finalize(void);
84 extern int Tcl_GetCommandInfo(Tcl_Interp *interp, const char *cmdName, Tcl_CmdInfo *infoPtr);
85 extern const char *Tcl_GetStringResult(Tcl_Interp *interp);
86 # endif
87 #endif
88
89 #ifdef _WIN32
90 # undef NOMINMAX
91 # define NOMINMAX 1
92 # undef YY_NO_UNISTD_H
93 # define YY_NO_UNISTD_H 1
94
95 # include <windows.h>
96 # include <io.h>
97 # include <direct.h>
98
99 # define strtok_r strtok_s
100 # define strdup _strdup
101 # define snprintf _snprintf
102 # define getcwd _getcwd
103 # define mkdir _mkdir
104 # define popen _popen
105 # define pclose _pclose
106
107 # ifndef __MINGW32__
108 # define PATH_MAX MAX_PATH
109 # define isatty _isatty
110 # define fileno _fileno
111 # endif
112 #endif
113
114 #ifndef PATH_MAX
115 # define PATH_MAX 4096
116 #endif
117
118 #define PRIVATE_NAMESPACE_BEGIN namespace {
119 #define PRIVATE_NAMESPACE_END }
120 #define YOSYS_NAMESPACE_BEGIN namespace Yosys {
121 #define YOSYS_NAMESPACE_END }
122 #define YOSYS_NAMESPACE_PREFIX Yosys::
123 #define USING_YOSYS_NAMESPACE using namespace Yosys;
124
125 #if __cplusplus >= 201103L
126 # define YS_OVERRIDE override
127 # define YS_FINAL final
128 #else
129 # define YS_OVERRIDE
130 # define YS_FINAL
131 #endif
132
133 #if defined(__GNUC__) || defined(__clang__)
134 # define YS_ATTRIBUTE(...) __attribute__((__VA_ARGS__))
135 # define YS_NORETURN
136 #elif defined(_MSC_VER)
137 # define YS_ATTRIBUTE(...)
138 # define YS_NORETURN __declspec(noreturn)
139 #else
140 # define YS_ATTRIBUTE(...)
141 # define YS_NORETURN
142 #endif
143
144 YOSYS_NAMESPACE_BEGIN
145
146 // Note: All headers included in hashlib.h must be included
147 // outside of YOSYS_NAMESPACE before this or bad things will happen.
148 #ifdef HASHLIB_H
149 # undef HASHLIB_H
150 # include "kernel/hashlib.h"
151 #else
152 # include "kernel/hashlib.h"
153 # undef HASHLIB_H
154 #endif
155
156 using std::vector;
157 using std::string;
158 using std::tuple;
159 using std::pair;
160
161 using std::make_tuple;
162 using std::make_pair;
163 using std::get;
164 using std::min;
165 using std::max;
166
167 // A primitive shared string implementation that does not
168 // move its .c_str() when the object is copied or moved.
169 struct shared_str {
170 std::shared_ptr<string> content;
171 shared_str() { }
172 shared_str(string s) { content = std::shared_ptr<string>(new string(s)); }
173 shared_str(const char *s) { content = std::shared_ptr<string>(new string(s)); }
174 const char *c_str() const { return content->c_str(); }
175 const string &str() const { return *content; }
176 bool operator==(const shared_str &other) const { return *content == *other.content; }
177 unsigned int hash() const { return hashlib::hash_ops<std::string>::hash(*content); }
178 };
179
180 using hashlib::mkhash;
181 using hashlib::mkhash_init;
182 using hashlib::mkhash_add;
183 using hashlib::mkhash_xorshift;
184 using hashlib::hash_ops;
185 using hashlib::hash_cstr_ops;
186 using hashlib::hash_ptr_ops;
187 using hashlib::hash_obj_ops;
188 using hashlib::dict;
189 using hashlib::idict;
190 using hashlib::pool;
191 using hashlib::mfp;
192
193 namespace RTLIL {
194 struct IdString;
195 struct Const;
196 struct SigBit;
197 struct SigSpec;
198 struct Wire;
199 struct Cell;
200 struct Module;
201 struct Design;
202 struct Monitor;
203 }
204
205 namespace AST {
206 struct AstNode;
207 }
208
209 using RTLIL::IdString;
210 using RTLIL::Const;
211 using RTLIL::SigBit;
212 using RTLIL::SigSpec;
213 using RTLIL::Wire;
214 using RTLIL::Cell;
215 using RTLIL::Module;
216 using RTLIL::Design;
217
218 namespace hashlib {
219 template<> struct hash_ops<RTLIL::Wire*> : hash_obj_ops {};
220 template<> struct hash_ops<RTLIL::Cell*> : hash_obj_ops {};
221 template<> struct hash_ops<RTLIL::Module*> : hash_obj_ops {};
222 template<> struct hash_ops<RTLIL::Design*> : hash_obj_ops {};
223 template<> struct hash_ops<RTLIL::Monitor*> : hash_obj_ops {};
224 template<> struct hash_ops<AST::AstNode*> : hash_obj_ops {};
225
226 template<> struct hash_ops<const RTLIL::Wire*> : hash_obj_ops {};
227 template<> struct hash_ops<const RTLIL::Cell*> : hash_obj_ops {};
228 template<> struct hash_ops<const RTLIL::Module*> : hash_obj_ops {};
229 template<> struct hash_ops<const RTLIL::Design*> : hash_obj_ops {};
230 template<> struct hash_ops<const RTLIL::Monitor*> : hash_obj_ops {};
231 template<> struct hash_ops<const AST::AstNode*> : hash_obj_ops {};
232 }
233
234 void memhasher_on();
235 void memhasher_off();
236 void memhasher_do();
237
238 extern bool memhasher_active;
239 inline void memhasher() { if (memhasher_active) memhasher_do(); }
240
241 void yosys_banner();
242 int ceil_log2(int x);
243 std::string stringf(const char *fmt, ...) YS_ATTRIBUTE(format(printf, 1, 2));
244 std::string vstringf(const char *fmt, va_list ap);
245 int readsome(std::istream &f, char *s, int n);
246 std::string next_token(std::string &text, const char *sep = " \t\r\n", bool long_strings = false);
247 std::vector<std::string> split_tokens(const std::string &text, const char *sep = " \t\r\n");
248 bool patmatch(const char *pattern, const char *string);
249 int run_command(const std::string &command, std::function<void(const std::string&)> process_line = std::function<void(const std::string&)>());
250 std::string make_temp_file(std::string template_str = "/tmp/yosys_XXXXXX");
251 std::string make_temp_dir(std::string template_str = "/tmp/yosys_XXXXXX");
252 bool check_file_exists(std::string filename, bool is_exec = false);
253 bool is_absolute_path(std::string filename);
254 void remove_directory(std::string dirname);
255
256 template<typename T> int GetSize(const T &obj) { return obj.size(); }
257 int GetSize(RTLIL::Wire *wire);
258
259 extern int autoidx;
260 extern int yosys_xtrace;
261
262 YOSYS_NAMESPACE_END
263
264 #include "kernel/log.h"
265 #include "kernel/rtlil.h"
266 #include "kernel/register.h"
267
268 YOSYS_NAMESPACE_BEGIN
269
270 using RTLIL::State;
271 using RTLIL::SigChunk;
272 using RTLIL::SigSig;
273
274 namespace hashlib {
275 template<> struct hash_ops<RTLIL::State> : hash_ops<int> {};
276 }
277
278 void yosys_setup();
279 void yosys_shutdown();
280
281 #ifdef YOSYS_ENABLE_TCL
282 Tcl_Interp *yosys_get_tcl_interp();
283 #endif
284
285 extern RTLIL::Design *yosys_design;
286
287 RTLIL::IdString new_id(std::string file, int line, std::string func);
288
289 #define NEW_ID \
290 YOSYS_NAMESPACE_PREFIX new_id(__FILE__, __LINE__, __FUNCTION__)
291
292 #define ID(_str) \
293 ([]() { static YOSYS_NAMESPACE_PREFIX RTLIL::IdString _id(_str); return _id; })()
294
295 RTLIL::Design *yosys_get_design();
296 std::string proc_self_dirname();
297 std::string proc_share_dirname();
298 const char *create_prompt(RTLIL::Design *design, int recursion_counter);
299 std::vector<std::string> glob_filename(const std::string &filename_pattern);
300 void rewrite_filename(std::string &filename);
301
302 void run_pass(std::string command, RTLIL::Design *design = nullptr);
303 void run_frontend(std::string filename, std::string command, std::string *backend_command, std::string *from_to_label = nullptr, RTLIL::Design *design = nullptr);
304 void run_frontend(std::string filename, std::string command, RTLIL::Design *design = nullptr);
305 void run_backend(std::string filename, std::string command, RTLIL::Design *design = nullptr);
306 void shell(RTLIL::Design *design);
307
308 // journal of all input and output files read (for "yosys -E")
309 extern std::set<std::string> yosys_input_files, yosys_output_files;
310
311 // from kernel/version_*.o (cc source generated from Makefile)
312 extern const char *yosys_version_str;
313
314 // from passes/cmds/design.cc
315 extern std::map<std::string, RTLIL::Design*> saved_designs;
316 extern std::vector<RTLIL::Design*> pushed_designs;
317
318 // from passes/cmds/pluginc.cc
319 extern std::map<std::string, void*> loaded_plugins;
320 extern std::map<std::string, std::string> loaded_plugin_aliases;
321 void load_plugin(std::string filename, std::vector<std::string> aliases);
322
323 YOSYS_NAMESPACE_END
324
325 #endif