Merge pull request #592 from japm48/master
[yosys.git] / frontends / ast / ast.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 * This is the AST frontend library.
21 *
22 * The AST frontend library is not a frontend on it's own but provides a
23 * generic abstract syntax tree (AST) abstraction for HDL code and can be
24 * used by HDL frontends. See "ast.h" for an overview of the API and the
25 * Verilog frontend for an usage example.
26 *
27 */
28
29 #ifndef AST_H
30 #define AST_H
31
32 #include "kernel/rtlil.h"
33 #include <stdint.h>
34 #include <set>
35
36 YOSYS_NAMESPACE_BEGIN
37
38 namespace AST
39 {
40 // all node types, type2str() must be extended
41 // whenever a new node type is added here
42 enum AstNodeType
43 {
44 AST_NONE,
45 AST_DESIGN,
46 AST_MODULE,
47 AST_TASK,
48 AST_FUNCTION,
49 AST_DPI_FUNCTION,
50
51 AST_WIRE,
52 AST_MEMORY,
53 AST_AUTOWIRE,
54 AST_PARAMETER,
55 AST_LOCALPARAM,
56 AST_DEFPARAM,
57 AST_PARASET,
58 AST_ARGUMENT,
59 AST_RANGE,
60 AST_MULTIRANGE,
61 AST_CONSTANT,
62 AST_REALVALUE,
63 AST_CELLTYPE,
64 AST_IDENTIFIER,
65 AST_PREFIX,
66 AST_ASSERT,
67 AST_ASSUME,
68 AST_LIVE,
69 AST_FAIR,
70 AST_COVER,
71
72 AST_FCALL,
73 AST_TO_BITS,
74 AST_TO_SIGNED,
75 AST_TO_UNSIGNED,
76 AST_CONCAT,
77 AST_REPLICATE,
78 AST_BIT_NOT,
79 AST_BIT_AND,
80 AST_BIT_OR,
81 AST_BIT_XOR,
82 AST_BIT_XNOR,
83 AST_REDUCE_AND,
84 AST_REDUCE_OR,
85 AST_REDUCE_XOR,
86 AST_REDUCE_XNOR,
87 AST_REDUCE_BOOL,
88 AST_SHIFT_LEFT,
89 AST_SHIFT_RIGHT,
90 AST_SHIFT_SLEFT,
91 AST_SHIFT_SRIGHT,
92 AST_LT,
93 AST_LE,
94 AST_EQ,
95 AST_NE,
96 AST_EQX,
97 AST_NEX,
98 AST_GE,
99 AST_GT,
100 AST_ADD,
101 AST_SUB,
102 AST_MUL,
103 AST_DIV,
104 AST_MOD,
105 AST_POW,
106 AST_POS,
107 AST_NEG,
108 AST_LOGIC_AND,
109 AST_LOGIC_OR,
110 AST_LOGIC_NOT,
111 AST_TERNARY,
112 AST_MEMRD,
113 AST_MEMWR,
114 AST_MEMINIT,
115
116 AST_TCALL,
117 AST_ASSIGN,
118 AST_CELL,
119 AST_PRIMITIVE,
120 AST_CELLARRAY,
121 AST_ALWAYS,
122 AST_INITIAL,
123 AST_BLOCK,
124 AST_ASSIGN_EQ,
125 AST_ASSIGN_LE,
126 AST_CASE,
127 AST_COND,
128 AST_CONDX,
129 AST_CONDZ,
130 AST_DEFAULT,
131 AST_FOR,
132 AST_WHILE,
133 AST_REPEAT,
134
135 AST_GENVAR,
136 AST_GENFOR,
137 AST_GENIF,
138 AST_GENCASE,
139 AST_GENBLOCK,
140
141 AST_POSEDGE,
142 AST_NEGEDGE,
143 AST_EDGE,
144
145 AST_PACKAGE
146 };
147
148 // convert an node type to a string (e.g. for debug output)
149 std::string type2str(AstNodeType type);
150
151 // The AST is built using instances of this struct
152 struct AstNode
153 {
154 // for dict<> and pool<>
155 unsigned int hashidx_;
156 unsigned int hash() const { return hashidx_; }
157
158 // this nodes type
159 AstNodeType type;
160
161 // the list of child nodes for this node
162 std::vector<AstNode*> children;
163
164 // the list of attributes assigned to this node
165 std::map<RTLIL::IdString, AstNode*> attributes;
166 bool get_bool_attribute(RTLIL::IdString id);
167
168 // node content - most of it is unused in most node types
169 std::string str;
170 std::vector<RTLIL::State> bits;
171 bool is_input, is_output, is_reg, is_logic, is_signed, is_string, range_valid, range_swapped, was_checked;
172 int port_id, range_left, range_right;
173 uint32_t integer;
174 double realvalue;
175
176 // if this is a multirange memory then this vector contains offset and length of each dimension
177 std::vector<int> multirange_dimensions;
178
179 // this is set by simplify and used during RTLIL generation
180 AstNode *id2ast;
181
182 // this is used by simplify to detect if basic analysis has been performed already on the node
183 bool basic_prep;
184
185 // this is the original sourcecode location that resulted in this AST node
186 // it is automatically set by the constructor using AST::current_filename and
187 // the AST::get_line_num() callback function.
188 std::string filename;
189 int linenum;
190
191 // creating and deleting nodes
192 AstNode(AstNodeType type = AST_NONE, AstNode *child1 = NULL, AstNode *child2 = NULL, AstNode *child3 = NULL);
193 AstNode *clone() const;
194 void cloneInto(AstNode *other) const;
195 void delete_children();
196 ~AstNode();
197
198 enum mem2reg_flags
199 {
200 /* status flags */
201 MEM2REG_FL_ALL = 0x00000001,
202 MEM2REG_FL_ASYNC = 0x00000002,
203 MEM2REG_FL_INIT = 0x00000004,
204
205 /* candidate flags */
206 MEM2REG_FL_FORCED = 0x00000100,
207 MEM2REG_FL_SET_INIT = 0x00000200,
208 MEM2REG_FL_SET_ELSE = 0x00000400,
209 MEM2REG_FL_SET_ASYNC = 0x00000800,
210 MEM2REG_FL_EQ2 = 0x00001000,
211 MEM2REG_FL_CMPLX_LHS = 0x00002000,
212
213 /* proc flags */
214 MEM2REG_FL_EQ1 = 0x01000000,
215 };
216
217 // simplify() creates a simpler AST by unrolling for-loops, expanding generate blocks, etc.
218 // it also sets the id2ast pointers so that identifier lookups are fast in genRTLIL()
219 bool simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage, int width_hint, bool sign_hint, bool in_param);
220 AstNode *readmem(bool is_readmemh, std::string mem_filename, AstNode *memory, int start_addr, int finish_addr, bool unconditional_init);
221 void expand_genblock(std::string index_var, std::string prefix, std::map<std::string, std::string> &name_map);
222 void replace_ids(const std::string &prefix, const std::map<std::string, std::string> &rules);
223 void mem2reg_as_needed_pass1(dict<AstNode*, pool<std::string>> &mem2reg_places,
224 dict<AstNode*, uint32_t> &mem2reg_flags, dict<AstNode*, uint32_t> &proc_flags, uint32_t &status_flags);
225 bool mem2reg_as_needed_pass2(pool<AstNode*> &mem2reg_set, AstNode *mod, AstNode *block, AstNode *&async_block);
226 bool mem2reg_check(pool<AstNode*> &mem2reg_set);
227 void mem2reg_remove(pool<AstNode*> &mem2reg_set, vector<AstNode*> &delnodes);
228 void meminfo(int &mem_width, int &mem_size, int &addr_bits);
229
230 // additional functionality for evaluating constant functions
231 struct varinfo_t { RTLIL::Const val; int offset; bool is_signed; };
232 bool has_const_only_constructs(bool &recommend_const_eval);
233 void replace_variables(std::map<std::string, varinfo_t> &variables, AstNode *fcall);
234 AstNode *eval_const_function(AstNode *fcall);
235
236 // create a human-readable text representation of the AST (for debugging)
237 void dumpAst(FILE *f, std::string indent) const;
238 void dumpVlog(FILE *f, std::string indent) const;
239
240 // used by genRTLIL() for detecting expression width and sign
241 void detectSignWidthWorker(int &width_hint, bool &sign_hint, bool *found_real = NULL);
242 void detectSignWidth(int &width_hint, bool &sign_hint, bool *found_real = NULL);
243
244 // create RTLIL code for this AST node
245 // for expressions the resulting signal vector is returned
246 // all generated cell instances, etc. are written to the RTLIL::Module pointed to by AST_INTERNAL::current_module
247 RTLIL::SigSpec genRTLIL(int width_hint = -1, bool sign_hint = false);
248 RTLIL::SigSpec genWidthRTLIL(int width, const dict<RTLIL::SigBit, RTLIL::SigBit> *new_subst_ptr = NULL);
249
250 // compare AST nodes
251 bool operator==(const AstNode &other) const;
252 bool operator!=(const AstNode &other) const;
253 bool contains(const AstNode *other) const;
254
255 // helper functions for creating AST nodes for constants
256 static AstNode *mkconst_int(uint32_t v, bool is_signed, int width = 32);
257 static AstNode *mkconst_bits(const std::vector<RTLIL::State> &v, bool is_signed);
258 static AstNode *mkconst_str(const std::vector<RTLIL::State> &v);
259 static AstNode *mkconst_str(const std::string &str);
260
261 // helper function for creating sign-extended const objects
262 RTLIL::Const bitsAsConst(int width, bool is_signed);
263 RTLIL::Const bitsAsConst(int width = -1);
264 RTLIL::Const asAttrConst();
265 RTLIL::Const asParaConst();
266 uint64_t asInt(bool is_signed);
267 bool bits_only_01() const;
268 bool asBool() const;
269
270 // helper functions for real valued const eval
271 int isConst() const; // return '1' for AST_CONSTANT and '2' for AST_REALVALUE
272 double asReal(bool is_signed);
273 RTLIL::Const realAsConst(int width);
274 };
275
276 // process an AST tree (ast must point to an AST_DESIGN node) and generate RTLIL code
277 void process(RTLIL::Design *design, AstNode *ast, bool dump_ast1, bool dump_ast2, bool dump_vlog, bool dump_rtlil, bool nolatches, bool nomeminit,
278 bool nomem2reg, bool mem2reg, bool lib, bool noopt, bool icells, bool nooverwrite, bool overwrite, bool defer, bool autowire);
279
280 // parametric modules are supported directly by the AST library
281 // therefore we need our own derivate of RTLIL::Module with overloaded virtual functions
282 struct AstModule : RTLIL::Module {
283 AstNode *ast;
284 bool nolatches, nomeminit, nomem2reg, mem2reg, lib, noopt, icells, autowire;
285 virtual ~AstModule();
286 virtual RTLIL::IdString derive(RTLIL::Design *design, dict<RTLIL::IdString, RTLIL::Const> parameters, bool mayfail);
287 virtual RTLIL::Module *clone() const;
288 };
289
290 // this must be set by the language frontend before parsing the sources
291 // the AstNode constructor then uses current_filename and get_line_num()
292 // to initialize the filename and linenum properties of new nodes
293 extern std::string current_filename;
294 extern void (*set_line_num)(int);
295 extern int (*get_line_num)();
296
297 // set set_line_num and get_line_num to internal dummy functions (done by simplify() and AstModule::derive
298 // to control the filename and linenum properties of new nodes not generated by a frontend parser)
299 void use_internal_line_num();
300
301 // call a DPI function
302 AstNode *dpi_call(const std::string &rtype, const std::string &fname, const std::vector<std::string> &argtypes, const std::vector<AstNode*> &args);
303 }
304
305 namespace AST_INTERNAL
306 {
307 // internal state variables
308 extern bool flag_dump_ast1, flag_dump_ast2, flag_dump_rtlil, flag_nolatches, flag_nomeminit;
309 extern bool flag_nomem2reg, flag_mem2reg, flag_lib, flag_noopt, flag_icells, flag_autowire;
310 extern AST::AstNode *current_ast, *current_ast_mod;
311 extern std::map<std::string, AST::AstNode*> current_scope;
312 extern const dict<RTLIL::SigBit, RTLIL::SigBit> *genRTLIL_subst_ptr;
313 extern RTLIL::SigSpec ignoreThisSignalsInInitial;
314 extern AST::AstNode *current_always, *current_top_block, *current_block, *current_block_child;
315 extern AST::AstModule *current_module;
316 extern bool current_always_clocked;
317 struct ProcessGenerator;
318 }
319
320 YOSYS_NAMESPACE_END
321
322 #endif