2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
21 // *** NOTE TO THE READER ***
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! ;)
26 // If you want to know how the design is represented by Yosys in the memory,
27 // you should read "kernel/rtlil.h".
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".
33 // This header is very boring. It just defines some general things that
34 // belong nowhere else and includes the interesting headers.
36 // Find more information in the "CodingReadme" file.
49 #include <unordered_map>
50 #include <unordered_set>
51 #include <initializer_list>
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.
80 #ifdef YOSYS_ENABLE_TCL
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
);
101 # undef YY_NO_UNISTD_H
102 # define YY_NO_UNISTD_H 1
104 # include <windows.h>
108 # define strtok_r strtok_s
109 # define strdup _strdup
110 # define snprintf _snprintf
111 # define getcwd _getcwd
112 # define mkdir _mkdir
113 # define popen _popen
114 # define pclose _pclose
117 # define PATH_MAX MAX_PATH
118 # define isatty _isatty
119 # define fileno _fileno
124 # define PATH_MAX 4096
127 #define YOSYS_NAMESPACE Yosys
128 #define PRIVATE_NAMESPACE_BEGIN namespace {
129 #define PRIVATE_NAMESPACE_END }
130 #define YOSYS_NAMESPACE_BEGIN namespace Yosys {
131 #define YOSYS_NAMESPACE_END }
132 #define YOSYS_NAMESPACE_PREFIX Yosys::
133 #define USING_YOSYS_NAMESPACE using namespace Yosys;
135 #if __cplusplus >= 201103L
136 # define YS_OVERRIDE override
137 # define YS_FINAL final
143 #if defined(__GNUC__) || defined(__clang__)
144 # define YS_ATTRIBUTE(...) __attribute__((__VA_ARGS__))
146 #elif defined(_MSC_VER)
147 # define YS_ATTRIBUTE(...)
148 # define YS_NORETURN __declspec(noreturn)
150 # define YS_ATTRIBUTE(...)
154 YOSYS_NAMESPACE_BEGIN
156 // Note: All headers included in hashlib.h must be included
157 // outside of YOSYS_NAMESPACE before this or bad things will happen.
160 # include "kernel/hashlib.h"
162 # include "kernel/hashlib.h"
171 using std::make_tuple
;
172 using std::make_pair
;
177 // A primitive shared string implementation that does not
178 // move its .c_str() when the object is copied or moved.
180 std::shared_ptr
<string
> content
;
182 shared_str(string s
) { content
= std::shared_ptr
<string
>(new string(s
)); }
183 shared_str(const char *s
) { content
= std::shared_ptr
<string
>(new string(s
)); }
184 const char *c_str() const { return content
->c_str(); }
185 const string
&str() const { return *content
; }
186 bool operator==(const shared_str
&other
) const { return *content
== *other
.content
; }
187 unsigned int hash() const { return hashlib::hash_ops
<std::string
>::hash(*content
); }
190 using hashlib::mkhash
;
191 using hashlib::mkhash_init
;
192 using hashlib::mkhash_add
;
193 using hashlib::mkhash_xorshift
;
194 using hashlib::hash_ops
;
195 using hashlib::hash_cstr_ops
;
196 using hashlib::hash_ptr_ops
;
197 using hashlib::hash_obj_ops
;
199 using hashlib::idict
;
219 using RTLIL::IdString
;
222 using RTLIL::SigSpec
;
229 template<> struct hash_ops
<RTLIL::Wire
*> : hash_obj_ops
{};
230 template<> struct hash_ops
<RTLIL::Cell
*> : hash_obj_ops
{};
231 template<> struct hash_ops
<RTLIL::Module
*> : hash_obj_ops
{};
232 template<> struct hash_ops
<RTLIL::Design
*> : hash_obj_ops
{};
233 template<> struct hash_ops
<RTLIL::Monitor
*> : hash_obj_ops
{};
234 template<> struct hash_ops
<AST::AstNode
*> : hash_obj_ops
{};
236 template<> struct hash_ops
<const RTLIL::Wire
*> : hash_obj_ops
{};
237 template<> struct hash_ops
<const RTLIL::Cell
*> : hash_obj_ops
{};
238 template<> struct hash_ops
<const RTLIL::Module
*> : hash_obj_ops
{};
239 template<> struct hash_ops
<const RTLIL::Design
*> : hash_obj_ops
{};
240 template<> struct hash_ops
<const RTLIL::Monitor
*> : hash_obj_ops
{};
241 template<> struct hash_ops
<const AST::AstNode
*> : hash_obj_ops
{};
245 void memhasher_off();
248 extern bool memhasher_active
;
249 inline void memhasher() { if (memhasher_active
) memhasher_do(); }
252 int ceil_log2(int x
) YS_ATTRIBUTE(const);
253 std::string
stringf(const char *fmt
, ...) YS_ATTRIBUTE(format(printf
, 1, 2));
254 std::string
vstringf(const char *fmt
, va_list ap
);
255 int readsome(std::istream
&f
, char *s
, int n
);
256 std::string
next_token(std::string
&text
, const char *sep
= " \t\r\n", bool long_strings
= false);
257 std::vector
<std::string
> split_tokens(const std::string
&text
, const char *sep
= " \t\r\n");
258 bool patmatch(const char *pattern
, const char *string
);
259 int run_command(const std::string
&command
, std::function
<void(const std::string
&)> process_line
= std::function
<void(const std::string
&)>());
260 std::string
make_temp_file(std::string template_str
= "/tmp/yosys_XXXXXX");
261 std::string
make_temp_dir(std::string template_str
= "/tmp/yosys_XXXXXX");
262 bool check_file_exists(std::string filename
, bool is_exec
= false);
263 bool is_absolute_path(std::string filename
);
264 void remove_directory(std::string dirname
);
265 std::string
escape_filename_spaces(const std::string
& filename
);
267 template<typename T
> int GetSize(const T
&obj
) { return obj
.size(); }
268 int GetSize(RTLIL::Wire
*wire
);
271 extern int yosys_xtrace
;
275 #include "kernel/log.h"
276 #include "kernel/rtlil.h"
277 #include "kernel/register.h"
279 YOSYS_NAMESPACE_BEGIN
282 using RTLIL::SigChunk
;
286 template<> struct hash_ops
<RTLIL::State
> : hash_ops
<int> {};
292 bool yosys_already_setup();
295 void yosys_shutdown();
297 #ifdef YOSYS_ENABLE_TCL
298 Tcl_Interp
*yosys_get_tcl_interp();
301 extern RTLIL::Design
*yosys_design
;
303 RTLIL::IdString
new_id(std::string file
, int line
, std::string func
);
306 YOSYS_NAMESPACE_PREFIX new_id(__FILE__, __LINE__, __FUNCTION__)
308 // Create a statically allocated IdString object, using for example ID(A) or ID($add).
310 // Recipe for Converting old code that is using conversion of strings like "\\A" and
311 // "$add" for creating IdStrings: Run below SED command on the .cc file and then use for
312 // example "meld foo.cc foo.cc.orig" to manually compile errors, if necessary.
314 // sed -i.orig -r 's/"\\\\([a-zA-Z0-9_]+)"/ID(\1)/g; s/"(\$[a-zA-Z0-9_]+)"/ID(\1)/g;' <filename>
316 #define ID(_id) ([]() { const char *p = "\\" #_id, *q = p[1] == '$' ? p+1 : p; \
317 static const YOSYS_NAMESPACE_PREFIX RTLIL::IdString id(q); return id; })()
318 namespace ID
= RTLIL::ID
;
320 RTLIL::Design
*yosys_get_design();
321 std::string
proc_self_dirname();
322 std::string
proc_share_dirname();
323 const char *create_prompt(RTLIL::Design
*design
, int recursion_counter
);
324 std::vector
<std::string
> glob_filename(const std::string
&filename_pattern
);
325 void rewrite_filename(std::string
&filename
);
327 void run_pass(std::string command
, RTLIL::Design
*design
= nullptr);
328 void run_frontend(std::string filename
, std::string command
, std::string
*backend_command
, std::string
*from_to_label
= nullptr, RTLIL::Design
*design
= nullptr);
329 void run_frontend(std::string filename
, std::string command
, RTLIL::Design
*design
= nullptr);
330 void run_backend(std::string filename
, std::string command
, RTLIL::Design
*design
= nullptr);
331 void shell(RTLIL::Design
*design
);
333 // journal of all input and output files read (for "yosys -E")
334 extern std::set
<std::string
> yosys_input_files
, yosys_output_files
;
336 // from kernel/version_*.o (cc source generated from Makefile)
337 extern const char *yosys_version_str
;
339 // from passes/cmds/design.cc
340 extern std::map
<std::string
, RTLIL::Design
*> saved_designs
;
341 extern std::vector
<RTLIL::Design
*> pushed_designs
;
343 // from passes/cmds/pluginc.cc
344 extern std::map
<std::string
, void*> loaded_plugins
;
346 extern std::map
<std::string
, void*> loaded_python_plugins
;
348 extern std::map
<std::string
, std::string
> loaded_plugin_aliases
;
349 void load_plugin(std::string filename
, std::vector
<std::string
> aliases
);