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.
20 * This is the AST frontend library.
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.
32 #include "kernel/rtlil.h"
38 // all node types, type2str() must be extended
39 // whenever a new node type is added here
126 // convert an node type to a string (e.g. for debug output)
127 std::string
type2str(AstNodeType type
);
129 // The AST is built using instances of this struct
135 // the list of child nodes for this node
136 std::vector
<AstNode
*> children
;
138 // the list of attributes assigned to this node
139 std::map
<RTLIL::IdString
, AstNode
*> attributes
;
141 // node content - most of it is unused in most node types
143 std::vector
<RTLIL::State
> bits
;
144 bool is_input
, is_output
, is_reg
, is_signed
, range_valid
;
145 int port_id
, range_left
, range_right
;
148 // this is set by simplify and used during RTLIL generation
151 // this is the original sourcecode location that resulted in this AST node
152 // it is automatically set by the constructor using AST::current_filename and
153 // the AST::get_line_num() callback function.
154 std::string filename
;
157 // creating and deleting nodes
158 AstNode(AstNodeType type
= AST_NONE
, AstNode
*child1
= NULL
, AstNode
*child2
= NULL
);
160 void cloneInto(AstNode
*other
);
161 void delete_children();
164 // simplify() creates a simpler AST by unrolling for-loops, expanding generate blocks, etc.
165 // it also sets the id2ast pointers so that identifier lookups are fast in genRTLIL()
166 bool simplify(bool const_fold
, bool at_zero
, bool in_lvalue
, int stage
);
167 void expand_genblock(std::string index_var
, std::string prefix
, std::map
<std::string
, std::string
> &name_map
);
168 void replace_ids(std::map
<std::string
, std::string
> &rules
);
169 void mem2reg_as_needed_pass1(std::set
<AstNode
*> &mem2reg_set
, std::set
<AstNode
*> &mem2reg_candidates
, bool sync_proc
, bool async_proc
, bool force_mem2reg
);
170 void mem2reg_as_needed_pass2(std::set
<AstNode
*> &mem2reg_set
, AstNode
*mod
, AstNode
*block
);
171 void meminfo(int &mem_width
, int &mem_size
, int &addr_bits
);
173 // create a human-readable text representation of the AST (for debugging)
174 void dumpAst(FILE *f
, std::string indent
, AstNode
*other
= NULL
);
175 void dumpVlog(FILE *f
, std::string indent
);
177 // used by genRTLIL() for detecting expression width and sign
178 void detectSignWidthWorker(int &width_hint
, bool &sign_hint
);
179 void detectSignWidth(int &width_hint
, bool &sign_hint
);
181 // create RTLIL code for this AST node
182 // for expressions the resulting signal vector is returned
183 // all generated cell instances, etc. are written to the RTLIL::Module pointed to by AST_INTERNAL::current_module
184 RTLIL::SigSpec
genRTLIL(int width_hint
= -1, bool sign_hint
= false);
185 RTLIL::SigSpec
genWidthRTLIL(int width
, RTLIL::SigSpec
*subst_from
= NULL
, RTLIL::SigSpec
*subst_to
= NULL
);
188 bool operator==(const AstNode
&other
) const;
189 bool operator!=(const AstNode
&other
) const;
190 bool contains(const AstNode
*other
) const;
192 // helper functions for creating AST nodes for constants
193 static AstNode
*mkconst_int(uint32_t v
, bool is_signed
, int width
= 32);
194 static AstNode
*mkconst_bits(const std::vector
<RTLIL::State
> &v
, bool is_signed
);
197 // process an AST tree (ast must point to an AST_DESIGN node) and generate RTLIL code
198 void process(RTLIL::Design
*design
, AstNode
*ast
, bool dump_ast
= false, bool dump_ast_diff
= false, bool dump_vlog
= false, bool nolatches
= false, bool nomem2reg
= false, bool mem2reg
= false, bool lib
= false, bool noopt
= false);
200 // parametric modules are supported directly by the AST library
201 // therfore we need our own derivate of RTLIL::Module with overloaded virtual functions
202 struct AstModule
: RTLIL::Module
{
204 bool nolatches
, nomem2reg
, mem2reg
, lib
, noopt
;
205 virtual ~AstModule();
206 virtual RTLIL::IdString
derive(RTLIL::Design
*design
, std::map
<RTLIL::IdString
, RTLIL::Const
> parameters
);
207 virtual void update_auto_wires(std::map
<RTLIL::IdString
, int> auto_sizes
);
208 virtual RTLIL::Module
*clone() const;
211 // this must be set by the language frontend before parsing the sources
212 // the AstNode constructor then uses current_filename and get_line_num()
213 // to initialize the filename and linenum properties of new nodes
214 extern std::string current_filename
;
215 extern void (*set_line_num
)(int);
216 extern int (*get_line_num
)();
218 // set set_line_num and get_line_num to internal dummy functions
219 // (done by simplify(), AstModule::derive and AstModule::update_auto_wires to control
220 // the filename and linenum properties of new nodes not generated by a frontend parser)
221 void use_internal_line_num();
224 namespace AST_INTERNAL
226 // internal state variables
227 extern bool flag_dump_ast
, flag_dump_ast_diff
, flag_nolatches
, flag_nomem2reg
, flag_mem2reg
, flag_lib
, flag_noopt
;
228 extern AST::AstNode
*current_ast
, *current_ast_mod
;
229 extern std::map
<std::string
, AST::AstNode
*> current_scope
;
230 extern RTLIL::SigSpec
*genRTLIL_subst_from
, *genRTLIL_subst_to
, ignoreThisSignalsInInitial
;
231 extern AST::AstNode
*current_top_block
, *current_block
, *current_block_child
;
232 extern AST::AstModule
*current_module
;
233 struct ProcessGenerator
;