Added verilog frontend -ignore_redef option
[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 namespace AST
37 {
38 // all node types, type2str() must be extended
39 // whenever a new node type is added here
40 enum AstNodeType
41 {
42 AST_NONE,
43 AST_DESIGN,
44 AST_MODULE,
45 AST_TASK,
46 AST_FUNCTION,
47
48 AST_WIRE,
49 AST_MEMORY,
50 AST_AUTOWIRE,
51 AST_PARAMETER,
52 AST_LOCALPARAM,
53 AST_DEFPARAM,
54 AST_PARASET,
55 AST_ARGUMENT,
56 AST_RANGE,
57 AST_CONSTANT,
58 AST_CELLTYPE,
59 AST_IDENTIFIER,
60 AST_PREFIX,
61
62 AST_FCALL,
63 AST_TO_SIGNED,
64 AST_TO_UNSIGNED,
65 AST_CONCAT,
66 AST_REPLICATE,
67 AST_BIT_NOT,
68 AST_BIT_AND,
69 AST_BIT_OR,
70 AST_BIT_XOR,
71 AST_BIT_XNOR,
72 AST_REDUCE_AND,
73 AST_REDUCE_OR,
74 AST_REDUCE_XOR,
75 AST_REDUCE_XNOR,
76 AST_REDUCE_BOOL,
77 AST_SHIFT_LEFT,
78 AST_SHIFT_RIGHT,
79 AST_SHIFT_SLEFT,
80 AST_SHIFT_SRIGHT,
81 AST_LT,
82 AST_LE,
83 AST_EQ,
84 AST_NE,
85 AST_GE,
86 AST_GT,
87 AST_ADD,
88 AST_SUB,
89 AST_MUL,
90 AST_DIV,
91 AST_MOD,
92 AST_POW,
93 AST_POS,
94 AST_NEG,
95 AST_LOGIC_AND,
96 AST_LOGIC_OR,
97 AST_LOGIC_NOT,
98 AST_TERNARY,
99 AST_MEMRD,
100 AST_MEMWR,
101
102 AST_TCALL,
103 AST_ASSIGN,
104 AST_CELL,
105 AST_PRIMITIVE,
106 AST_ALWAYS,
107 AST_INITIAL,
108 AST_BLOCK,
109 AST_ASSIGN_EQ,
110 AST_ASSIGN_LE,
111 AST_CASE,
112 AST_COND,
113 AST_DEFAULT,
114 AST_FOR,
115
116 AST_GENVAR,
117 AST_GENFOR,
118 AST_GENIF,
119 AST_GENBLOCK,
120
121 AST_POSEDGE,
122 AST_NEGEDGE,
123 AST_EDGE
124 };
125
126 // convert an node type to a string (e.g. for debug output)
127 std::string type2str(AstNodeType type);
128
129 // The AST is built using instances of this struct
130 struct AstNode
131 {
132 // this nodes type
133 AstNodeType type;
134
135 // the list of child nodes for this node
136 std::vector<AstNode*> children;
137
138 // the list of attributes assigned to this node
139 std::map<RTLIL::IdString, AstNode*> attributes;
140 bool get_bool_attribute(RTLIL::IdString id);
141
142 // node content - most of it is unused in most node types
143 std::string str;
144 std::vector<RTLIL::State> bits;
145 bool is_input, is_output, is_reg, is_signed, range_valid;
146 int port_id, range_left, range_right;
147 uint32_t integer;
148
149 // this is set by simplify and used during RTLIL generation
150 AstNode *id2ast;
151
152 // this is the original sourcecode location that resulted in this AST node
153 // it is automatically set by the constructor using AST::current_filename and
154 // the AST::get_line_num() callback function.
155 std::string filename;
156 int linenum;
157
158 // creating and deleting nodes
159 AstNode(AstNodeType type = AST_NONE, AstNode *child1 = NULL, AstNode *child2 = NULL);
160 AstNode *clone();
161 void cloneInto(AstNode *other);
162 void delete_children();
163 ~AstNode();
164
165 enum mem2reg_flags
166 {
167 /* status flags */
168 MEM2REG_FL_ALL = 0x00000001,
169 MEM2REG_FL_ASYNC = 0x00000002,
170 MEM2REG_FL_INIT = 0x00000004,
171
172 /* candidate flags */
173 MEM2REG_FL_FORCED = 0x00000100,
174 MEM2REG_FL_SET_INIT = 0x00000200,
175 MEM2REG_FL_SET_ELSE = 0x00000400,
176 MEM2REG_FL_SET_ASYNC = 0x00000800,
177 MEM2REG_FL_EQ2 = 0x00001000,
178
179 /* proc flags */
180 MEM2REG_FL_EQ1 = 0x01000000,
181 };
182
183 // simplify() creates a simpler AST by unrolling for-loops, expanding generate blocks, etc.
184 // it also sets the id2ast pointers so that identifier lookups are fast in genRTLIL()
185 bool simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage, int width_hint, bool sign_hint);
186 void expand_genblock(std::string index_var, std::string prefix, std::map<std::string, std::string> &name_map);
187 void replace_ids(std::map<std::string, std::string> &rules);
188 void mem2reg_as_needed_pass1(std::map<AstNode*, std::set<std::string>> &mem2reg_places,
189 std::map<AstNode*, uint32_t> &mem2reg_flags, std::map<AstNode*, uint32_t> &proc_flags, uint32_t &status_flags);
190 void mem2reg_as_needed_pass2(std::set<AstNode*> &mem2reg_set, AstNode *mod, AstNode *block);
191 void meminfo(int &mem_width, int &mem_size, int &addr_bits);
192
193 // create a human-readable text representation of the AST (for debugging)
194 void dumpAst(FILE *f, std::string indent);
195 void dumpVlog(FILE *f, std::string indent);
196
197 // used by genRTLIL() for detecting expression width and sign
198 void detectSignWidthWorker(int &width_hint, bool &sign_hint);
199 void detectSignWidth(int &width_hint, bool &sign_hint);
200
201 // create RTLIL code for this AST node
202 // for expressions the resulting signal vector is returned
203 // all generated cell instances, etc. are written to the RTLIL::Module pointed to by AST_INTERNAL::current_module
204 RTLIL::SigSpec genRTLIL(int width_hint = -1, bool sign_hint = false);
205 RTLIL::SigSpec genWidthRTLIL(int width, RTLIL::SigSpec *subst_from = NULL, RTLIL::SigSpec *subst_to = NULL);
206
207 // compare AST nodes
208 bool operator==(const AstNode &other) const;
209 bool operator!=(const AstNode &other) const;
210 bool contains(const AstNode *other) const;
211
212 // helper functions for creating AST nodes for constants
213 static AstNode *mkconst_int(uint32_t v, bool is_signed, int width = 32);
214 static AstNode *mkconst_bits(const std::vector<RTLIL::State> &v, bool is_signed);
215
216 // helper function for creating sign-extended const objects
217 RTLIL::Const bitsAsConst(int width, bool is_signed);
218 RTLIL::Const bitsAsConst(int width = -1);
219 };
220
221 // process an AST tree (ast must point to an AST_DESIGN node) and generate RTLIL code
222 void process(RTLIL::Design *design, AstNode *ast, bool dump_ast1 = false, bool dump_ast2 = false, bool dump_vlog = false, bool nolatches = false, bool nomem2reg = false, bool mem2reg = false, bool lib = false, bool noopt = false, bool ignore_redef = false);
223
224 // parametric modules are supported directly by the AST library
225 // therfore we need our own derivate of RTLIL::Module with overloaded virtual functions
226 struct AstModule : RTLIL::Module {
227 AstNode *ast;
228 bool nolatches, nomem2reg, mem2reg, lib, noopt;
229 virtual ~AstModule();
230 virtual RTLIL::IdString derive(RTLIL::Design *design, std::map<RTLIL::IdString, RTLIL::Const> parameters, std::set<RTLIL::IdString> signed_parameters);
231 virtual RTLIL::Module *clone() const;
232 };
233
234 // this must be set by the language frontend before parsing the sources
235 // the AstNode constructor then uses current_filename and get_line_num()
236 // to initialize the filename and linenum properties of new nodes
237 extern std::string current_filename;
238 extern void (*set_line_num)(int);
239 extern int (*get_line_num)();
240
241 // set set_line_num and get_line_num to internal dummy functions (done by simplify() and AstModule::derive
242 // to control the filename and linenum properties of new nodes not generated by a frontend parser)
243 void use_internal_line_num();
244 }
245
246 namespace AST_INTERNAL
247 {
248 // internal state variables
249 extern bool flag_dump_ast1, flag_dump_ast2, flag_nolatches, flag_nomem2reg, flag_mem2reg, flag_lib, flag_noopt;
250 extern AST::AstNode *current_ast, *current_ast_mod;
251 extern std::map<std::string, AST::AstNode*> current_scope;
252 extern RTLIL::SigSpec *genRTLIL_subst_from, *genRTLIL_subst_to, ignoreThisSignalsInInitial;
253 extern AST::AstNode *current_top_block, *current_block, *current_block_child;
254 extern AST::AstModule *current_module;
255 struct ProcessGenerator;
256 }
257
258 #endif