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