More hashtable finetuning
[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
52 #include <sstream>
53 #include <fstream>
54 #include <istream>
55 #include <ostream>
56 #include <iostream>
57
58 #include <stdarg.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <stdint.h>
62 #include <stdio.h>
63
64 #ifndef _YOSYS_
65 # error It looks like you are trying to build Yosys without the config defines set. \
66 When building Yosys with a custom make system, make sure you set all the \
67 defines the Yosys Makefile would set for your build configuration.
68 #endif
69
70 #ifdef YOSYS_ENABLE_TCL
71 # include <tcl.h>
72 #endif
73
74 #ifdef _WIN32
75 # undef NOMINMAX
76 # define NOMINMAX 1
77 # undef YY_NO_UNISTD_H
78 # define YY_NO_UNISTD_H 1
79
80 # include <windows.h>
81 # include <io.h>
82 # include <direct.h>
83
84 # define strtok_r strtok_s
85 # define strdup _strdup
86 # define snprintf _snprintf
87 # define getcwd _getcwd
88 # define mkdir _mkdir
89 # define popen _popen
90 # define pclose _pclose
91 # define PATH_MAX MAX_PATH
92
93 # ifndef __MINGW32__
94 # define isatty _isatty
95 # define fileno _fileno
96 # endif
97 #endif
98
99 #define PRIVATE_NAMESPACE_BEGIN namespace {
100 #define PRIVATE_NAMESPACE_END }
101 #define YOSYS_NAMESPACE_BEGIN namespace Yosys {
102 #define YOSYS_NAMESPACE_END }
103 #define YOSYS_NAMESPACE_PREFIX Yosys::
104 #define USING_YOSYS_NAMESPACE using namespace Yosys;
105
106 #if __cplusplus >= 201103L
107 # define YS_OVERRIDE override
108 # define YS_FINAL final
109 #else
110 # define YS_OVERRIDE
111 # define YS_FINAL
112 #endif
113
114 #if defined(__GNUC__) || defined(__clang__)
115 # define YS_ATTRIBUTE(...) __attribute__((__VA_ARGS__))
116 # define YS_NORETURN
117 #elif defined(_MSC_VER)
118 # define YS_ATTRIBUTE(...)
119 # define YS_NORETURN __declspec(noreturn)
120 #else
121 # define YS_ATTRIBUTE(...)
122 # define YS_NORETURN
123 #endif
124
125 YOSYS_NAMESPACE_BEGIN
126
127 #include "kernel/hashmap.h"
128 using std::vector;
129
130 namespace RTLIL {
131 struct IdString;
132 struct SigBit;
133 struct SigSpec;
134 struct Wire;
135 struct Cell;
136 }
137
138 std::string stringf(const char *fmt, ...) YS_ATTRIBUTE(format(printf, 1, 2));
139 std::string vstringf(const char *fmt, va_list ap);
140 int readsome(std::istream &f, char *s, int n);
141 std::string next_token(std::string &text, const char *sep);
142 bool patmatch(const char *pattern, const char *string);
143 int run_command(const std::string &command, std::function<void(const std::string&)> process_line = std::function<void(const std::string&)>());
144 std::string make_temp_file(std::string template_str = "/tmp/yosys_XXXXXX");
145 std::string make_temp_dir(std::string template_str = "/tmp/yosys_XXXXXX");
146 bool check_file_exists(std::string filename, bool is_exec = false);
147 void remove_directory(std::string dirname);
148
149 template<typename T> int GetSize(const T &obj) { return obj.size(); }
150 int GetSize(RTLIL::Wire *wire);
151
152 extern int autoidx;
153
154 YOSYS_NAMESPACE_END
155
156 #include "kernel/log.h"
157 #include "kernel/rtlil.h"
158 #include "kernel/register.h"
159
160 YOSYS_NAMESPACE_BEGIN
161
162 void yosys_setup();
163 void yosys_shutdown();
164
165 #ifdef YOSYS_ENABLE_TCL
166 Tcl_Interp *yosys_get_tcl_interp();
167 #endif
168
169 extern RTLIL::Design *yosys_design;
170
171 RTLIL::IdString new_id(std::string file, int line, std::string func);
172
173 #define NEW_ID \
174 YOSYS_NAMESPACE_PREFIX new_id(__FILE__, __LINE__, __FUNCTION__)
175
176 #define ID(_str) \
177 ([]() { static YOSYS_NAMESPACE_PREFIX RTLIL::IdString _id(_str); return _id; })()
178
179 RTLIL::Design *yosys_get_design();
180 std::string proc_self_dirname();
181 std::string proc_share_dirname();
182 const char *create_prompt(RTLIL::Design *design, int recursion_counter);
183
184 void run_frontend(std::string filename, std::string command, RTLIL::Design *design, std::string *backend_command, std::string *from_to_label);
185 void run_pass(std::string command, RTLIL::Design *design);
186 void run_backend(std::string filename, std::string command, RTLIL::Design *design);
187 void shell(RTLIL::Design *design);
188
189 // from kernel/version_*.o (cc source generated from Makefile)
190 extern const char *yosys_version_str;
191
192 // from passes/cmds/design.cc
193 extern std::map<std::string, RTLIL::Design*> saved_designs;
194 extern std::vector<RTLIL::Design*> pushed_designs;
195
196 // from passes/cmds/pluginc.cc
197 extern std::map<std::string, void*> loaded_plugins;
198 extern std::map<std::string, std::string> loaded_plugin_aliases;
199 void load_plugin(std::string filename, std::vector<std::string> aliases);
200
201 YOSYS_NAMESPACE_END
202
203 #endif