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"
40 // all node types, type2str() must be extended
41 // whenever a new node type is added here
153 AST_INTERFACEPORTTYPE
,
165 struct AstSrcLocType
{
166 unsigned int first_line
, last_line
;
167 unsigned int first_column
, last_column
;
168 AstSrcLocType() : first_line(0), last_line(0), first_column(0), last_column(0) {}
169 AstSrcLocType(int _first_line
, int _first_column
, int _last_line
, int _last_column
) : first_line(_first_line
), last_line(_last_line
), first_column(_first_column
), last_column(_last_column
) {}
172 // convert an node type to a string (e.g. for debug output)
173 std::string
type2str(AstNodeType type
);
175 // The AST is built using instances of this struct
178 // for dict<> and pool<>
179 unsigned int hashidx_
;
180 unsigned int hash() const { return hashidx_
; }
185 // the list of child nodes for this node
186 std::vector
<AstNode
*> children
;
188 // the list of attributes assigned to this node
189 std::map
<RTLIL::IdString
, AstNode
*> attributes
;
190 bool get_bool_attribute(RTLIL::IdString id
);
192 // node content - most of it is unused in most node types
194 std::vector
<RTLIL::State
> bits
;
195 bool is_input
, is_output
, is_reg
, is_logic
, is_signed
, is_string
, is_wand
, is_wor
, range_valid
, range_swapped
, was_checked
, is_unsized
, is_custom_type
;
196 int port_id
, range_left
, range_right
;
199 // set for IDs typed to an enumeration, not used
202 // if this is a multirange memory then this vector contains offset and length of each dimension
203 std::vector
<int> multirange_dimensions
;
205 // this is set by simplify and used during RTLIL generation
208 // this is used by simplify to detect if basic analysis has been performed already on the node
211 // this is used for ID references in RHS expressions that should use the "new" value for non-blocking assignments
214 // this is the original sourcecode location that resulted in this AST node
215 // it is automatically set by the constructor using AST::current_filename and
216 // the AST::get_line_num() callback function.
217 std::string filename
;
218 AstSrcLocType location
;
220 // creating and deleting nodes
221 AstNode(AstNodeType type
= AST_NONE
, AstNode
*child1
= NULL
, AstNode
*child2
= NULL
, AstNode
*child3
= NULL
);
222 AstNode
*clone() const;
223 void cloneInto(AstNode
*other
) const;
224 void delete_children();
230 MEM2REG_FL_ALL
= 0x00000001,
231 MEM2REG_FL_ASYNC
= 0x00000002,
232 MEM2REG_FL_INIT
= 0x00000004,
234 /* candidate flags */
235 MEM2REG_FL_FORCED
= 0x00000100,
236 MEM2REG_FL_SET_INIT
= 0x00000200,
237 MEM2REG_FL_SET_ELSE
= 0x00000400,
238 MEM2REG_FL_SET_ASYNC
= 0x00000800,
239 MEM2REG_FL_EQ2
= 0x00001000,
240 MEM2REG_FL_CMPLX_LHS
= 0x00002000,
241 MEM2REG_FL_CONST_LHS
= 0x00004000,
242 MEM2REG_FL_VAR_LHS
= 0x00008000,
245 MEM2REG_FL_EQ1
= 0x01000000,
248 // simplify() creates a simpler AST by unrolling for-loops, expanding generate blocks, etc.
249 // it also sets the id2ast pointers so that identifier lookups are fast in genRTLIL()
250 bool simplify(bool const_fold
, bool at_zero
, bool in_lvalue
, int stage
, int width_hint
, bool sign_hint
, bool in_param
);
251 AstNode
*readmem(bool is_readmemh
, std::string mem_filename
, AstNode
*memory
, int start_addr
, int finish_addr
, bool unconditional_init
);
252 void expand_genblock(std::string index_var
, std::string prefix
, std::map
<std::string
, std::string
> &name_map
);
253 void replace_ids(const std::string
&prefix
, const std::map
<std::string
, std::string
> &rules
);
254 void mem2reg_as_needed_pass1(dict
<AstNode
*, pool
<std::string
>> &mem2reg_places
,
255 dict
<AstNode
*, uint32_t> &mem2reg_flags
, dict
<AstNode
*, uint32_t> &proc_flags
, uint32_t &status_flags
);
256 bool mem2reg_as_needed_pass2(pool
<AstNode
*> &mem2reg_set
, AstNode
*mod
, AstNode
*block
, AstNode
*&async_block
);
257 bool mem2reg_check(pool
<AstNode
*> &mem2reg_set
);
258 void mem2reg_remove(pool
<AstNode
*> &mem2reg_set
, vector
<AstNode
*> &delnodes
);
259 void meminfo(int &mem_width
, int &mem_size
, int &addr_bits
);
261 // additional functionality for evaluating constant functions
262 struct varinfo_t
{ RTLIL::Const val
; int offset
; bool is_signed
; };
263 bool has_const_only_constructs(bool &recommend_const_eval
);
264 void replace_variables(std::map
<std::string
, varinfo_t
> &variables
, AstNode
*fcall
);
265 AstNode
*eval_const_function(AstNode
*fcall
);
266 bool is_simple_const_expr();
267 std::string
process_format_str(const std::string
&sformat
, int next_arg
, int stage
, int width_hint
, bool sign_hint
);
269 // create a human-readable text representation of the AST (for debugging)
270 void dumpAst(FILE *f
, std::string indent
) const;
271 void dumpVlog(FILE *f
, std::string indent
) const;
273 // used by genRTLIL() for detecting expression width and sign
274 void detectSignWidthWorker(int &width_hint
, bool &sign_hint
, bool *found_real
= NULL
);
275 void detectSignWidth(int &width_hint
, bool &sign_hint
, bool *found_real
= NULL
);
277 // create RTLIL code for this AST node
278 // for expressions the resulting signal vector is returned
279 // all generated cell instances, etc. are written to the RTLIL::Module pointed to by AST_INTERNAL::current_module
280 RTLIL::SigSpec
genRTLIL(int width_hint
= -1, bool sign_hint
= false);
281 RTLIL::SigSpec
genWidthRTLIL(int width
, const dict
<RTLIL::SigBit
, RTLIL::SigBit
> *new_subst_ptr
= NULL
);
284 bool operator==(const AstNode
&other
) const;
285 bool operator!=(const AstNode
&other
) const;
286 bool contains(const AstNode
*other
) const;
288 // helper functions for creating AST nodes for constants
289 static AstNode
*mkconst_int(uint32_t v
, bool is_signed
, int width
= 32);
290 static AstNode
*mkconst_bits(const std::vector
<RTLIL::State
> &v
, bool is_signed
, bool is_unsized
);
291 static AstNode
*mkconst_bits(const std::vector
<RTLIL::State
> &v
, bool is_signed
);
292 static AstNode
*mkconst_str(const std::vector
<RTLIL::State
> &v
);
293 static AstNode
*mkconst_str(const std::string
&str
);
295 // helper function for creating sign-extended const objects
296 RTLIL::Const
bitsAsConst(int width
, bool is_signed
);
297 RTLIL::Const
bitsAsConst(int width
= -1);
298 RTLIL::Const
bitsAsUnsizedConst(int width
);
299 RTLIL::Const
asAttrConst();
300 RTLIL::Const
asParaConst();
301 uint64_t asInt(bool is_signed
);
302 bool bits_only_01() const;
305 // helper functions for real valued const eval
306 int isConst() const; // return '1' for AST_CONSTANT and '2' for AST_REALVALUE
307 double asReal(bool is_signed
);
308 RTLIL::Const
realAsConst(int width
);
311 void allocateDefaultEnumValues();
312 void annotateTypedEnums(AstNode
*template_node
);
315 // process an AST tree (ast must point to an AST_DESIGN node) and generate RTLIL code
316 void process(RTLIL::Design
*design
, AstNode
*ast
, bool dump_ast1
, bool dump_ast2
, bool no_dump_ptr
, bool dump_vlog1
, bool dump_vlog2
, bool dump_rtlil
, bool nolatches
, bool nomeminit
,
317 bool nomem2reg
, bool mem2reg
, bool noblackbox
, bool lib
, bool nowb
, bool noopt
, bool icells
, bool pwires
, bool nooverwrite
, bool overwrite
, bool defer
, bool autowire
);
319 // parametric modules are supported directly by the AST library
320 // therefore we need our own derivate of RTLIL::Module with overloaded virtual functions
321 struct AstModule
: RTLIL::Module
{
323 bool nolatches
, nomeminit
, nomem2reg
, mem2reg
, noblackbox
, lib
, nowb
, noopt
, icells
, pwires
, autowire
;
324 ~AstModule() YS_OVERRIDE
;
325 RTLIL::IdString
derive(RTLIL::Design
*design
, const dict
<RTLIL::IdString
, RTLIL::Const
> ¶meters
, bool mayfail
) YS_OVERRIDE
;
326 RTLIL::IdString
derive(RTLIL::Design
*design
, const dict
<RTLIL::IdString
, RTLIL::Const
> ¶meters
, const dict
<RTLIL::IdString
, RTLIL::Module
*> &interfaces
, const dict
<RTLIL::IdString
, RTLIL::IdString
> &modports
, bool mayfail
) YS_OVERRIDE
;
327 std::string
derive_common(RTLIL::Design
*design
, const dict
<RTLIL::IdString
, RTLIL::Const
> ¶meters
, AstNode
**new_ast_out
, bool quiet
= false);
328 void reprocess_module(RTLIL::Design
*design
, const dict
<RTLIL::IdString
, RTLIL::Module
*> &local_interfaces
) YS_OVERRIDE
;
329 RTLIL::Module
*clone() const YS_OVERRIDE
;
330 void loadconfig() const;
333 // this must be set by the language frontend before parsing the sources
334 // the AstNode constructor then uses current_filename and get_line_num()
335 // to initialize the filename and linenum properties of new nodes
336 extern std::string current_filename
;
337 extern void (*set_line_num
)(int);
338 extern int (*get_line_num
)();
340 // set set_line_num and get_line_num to internal dummy functions (done by simplify() and AstModule::derive
341 // to control the filename and linenum properties of new nodes not generated by a frontend parser)
342 void use_internal_line_num();
344 // call a DPI function
345 AstNode
*dpi_call(const std::string
&rtype
, const std::string
&fname
, const std::vector
<std::string
> &argtypes
, const std::vector
<AstNode
*> &args
);
347 // Helper functions related to handling SystemVerilog interfaces
348 std::pair
<std::string
,std::string
> split_modport_from_type(std::string name_type
);
349 AstNode
* find_modport(AstNode
*intf
, std::string name
);
350 void explode_interface_port(AstNode
*module_ast
, RTLIL::Module
* intfmodule
, std::string intfname
, AstNode
*modport
);
353 namespace AST_INTERNAL
355 // internal state variables
356 extern bool flag_dump_ast1
, flag_dump_ast2
, flag_no_dump_ptr
, flag_dump_rtlil
, flag_nolatches
, flag_nomeminit
;
357 extern bool flag_nomem2reg
, flag_mem2reg
, flag_lib
, flag_noopt
, flag_icells
, flag_pwires
, flag_autowire
;
358 extern AST::AstNode
*current_ast
, *current_ast_mod
;
359 extern std::map
<std::string
, AST::AstNode
*> current_scope
;
360 extern const dict
<RTLIL::SigBit
, RTLIL::SigBit
> *genRTLIL_subst_ptr
;
361 extern RTLIL::SigSpec ignoreThisSignalsInInitial
;
362 extern AST::AstNode
*current_always
, *current_top_block
, *current_block
, *current_block_child
;
363 extern AST::AstModule
*current_module
;
364 extern bool current_always_clocked
;
365 struct LookaheadRewriter
;
366 struct ProcessGenerator
;