yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ast.h
Go to the documentation of this file.
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 
37 
38 namespace AST
39 {
40  // all node types, type2str() must be extended
41  // whenever a new node type is added here
43  {
50 
67 
110 
127 
133 
137  };
138 
139  // convert an node type to a string (e.g. for debug output)
140  std::string type2str(AstNodeType type);
141 
142  // The AST is built using instances of this struct
143  struct AstNode
144  {
145  // this nodes type
147 
148  // the list of child nodes for this node
149  std::vector<AstNode*> children;
150 
151  // the list of attributes assigned to this node
152  std::map<RTLIL::IdString, AstNode*> attributes;
154 
155  // node content - most of it is unused in most node types
156  std::string str;
157  std::vector<RTLIL::State> bits;
160  uint32_t integer;
161  double realvalue;
162 
163  // if this is a multirange memory then this vector contains offset and length of each dimension
164  std::vector<int> multirange_dimensions;
165 
166  // this is set by simplify and used during RTLIL generation
168 
169  // this is used by simplify to detect if basic analysis has been performed already on the node
171 
172  // this is the original sourcecode location that resulted in this AST node
173  // it is automatically set by the constructor using AST::current_filename and
174  // the AST::get_line_num() callback function.
175  std::string filename;
176  int linenum;
177 
178  // creating and deleting nodes
179  AstNode(AstNodeType type = AST_NONE, AstNode *child1 = NULL, AstNode *child2 = NULL);
180  AstNode *clone();
181  void cloneInto(AstNode *other);
182  void delete_children();
183  ~AstNode();
184 
186  {
187  /* status flags */
188  MEM2REG_FL_ALL = 0x00000001,
189  MEM2REG_FL_ASYNC = 0x00000002,
190  MEM2REG_FL_INIT = 0x00000004,
191 
192  /* candidate flags */
193  MEM2REG_FL_FORCED = 0x00000100,
194  MEM2REG_FL_SET_INIT = 0x00000200,
195  MEM2REG_FL_SET_ELSE = 0x00000400,
196  MEM2REG_FL_SET_ASYNC = 0x00000800,
197  MEM2REG_FL_EQ2 = 0x00001000,
198  MEM2REG_FL_CMPLX_LHS = 0x00002000,
199 
200  /* proc flags */
201  MEM2REG_FL_EQ1 = 0x01000000,
202  };
203 
204  // simplify() creates a simpler AST by unrolling for-loops, expanding generate blocks, etc.
205  // it also sets the id2ast pointers so that identifier lookups are fast in genRTLIL()
206  bool simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage, int width_hint, bool sign_hint, bool in_param);
207  AstNode *readmem(bool is_readmemh, std::string mem_filename, AstNode *memory, int start_addr, int finish_addr);
208  void expand_genblock(std::string index_var, std::string prefix, std::map<std::string, std::string> &name_map);
209  void replace_ids(const std::string &prefix, const std::map<std::string, std::string> &rules);
210  void mem2reg_as_needed_pass1(std::map<AstNode*, std::set<std::string>> &mem2reg_places,
211  std::map<AstNode*, uint32_t> &mem2reg_flags, std::map<AstNode*, uint32_t> &proc_flags, uint32_t &status_flags);
212  void mem2reg_as_needed_pass2(std::set<AstNode*> &mem2reg_set, AstNode *mod, AstNode *block);
213  bool mem2reg_check(std::set<AstNode*> &mem2reg_set);
214  void meminfo(int &mem_width, int &mem_size, int &addr_bits);
215 
216  // additional functionality for evaluating constant functions
217  struct varinfo_t { RTLIL::Const val; int offset; bool is_signed; };
218  bool has_const_only_constructs(bool &recommend_const_eval);
219  void replace_variables(std::map<std::string, varinfo_t> &variables, AstNode *fcall);
221 
222  // create a human-readable text representation of the AST (for debugging)
223  void dumpAst(FILE *f, std::string indent);
224  void dumpVlog(FILE *f, std::string indent);
225 
226  // used by genRTLIL() for detecting expression width and sign
227  void detectSignWidthWorker(int &width_hint, bool &sign_hint, bool *found_real = NULL);
228  void detectSignWidth(int &width_hint, bool &sign_hint, bool *found_real = NULL);
229 
230  // create RTLIL code for this AST node
231  // for expressions the resulting signal vector is returned
232  // all generated cell instances, etc. are written to the RTLIL::Module pointed to by AST_INTERNAL::current_module
233  RTLIL::SigSpec genRTLIL(int width_hint = -1, bool sign_hint = false);
234  RTLIL::SigSpec genWidthRTLIL(int width, const std::map<RTLIL::SigBit, RTLIL::SigBit> *new_subst_ptr = NULL);
235 
236  // compare AST nodes
237  bool operator==(const AstNode &other) const;
238  bool operator!=(const AstNode &other) const;
239  bool contains(const AstNode *other) const;
240 
241  // helper functions for creating AST nodes for constants
242  static AstNode *mkconst_int(uint32_t v, bool is_signed, int width = 32);
243  static AstNode *mkconst_bits(const std::vector<RTLIL::State> &v, bool is_signed);
244  static AstNode *mkconst_str(const std::vector<RTLIL::State> &v);
245  static AstNode *mkconst_str(const std::string &str);
246 
247  // helper function for creating sign-extended const objects
248  RTLIL::Const bitsAsConst(int width, bool is_signed);
249  RTLIL::Const bitsAsConst(int width = -1);
252  uint64_t asInt(bool is_signed);
253  bool bits_only_01();
254  bool asBool();
255 
256  // helper functions for real valued const eval
257  int isConst(); // return '1' for AST_CONSTANT and '2' for AST_REALVALUE
258  double asReal(bool is_signed);
259  RTLIL::Const realAsConst(int width);
260  };
261 
262  // process an AST tree (ast must point to an AST_DESIGN node) and generate RTLIL code
263  void process(RTLIL::Design *design, AstNode *ast, bool dump_ast1, bool dump_ast2, bool dump_vlog, bool nolatches, bool nomem2reg, bool mem2reg, bool lib, bool noopt, bool icells, bool ignore_redef, bool defer, bool autowire);
264 
265  // parametric modules are supported directly by the AST library
266  // therfore we need our own derivate of RTLIL::Module with overloaded virtual functions
270  virtual ~AstModule();
271  virtual RTLIL::IdString derive(RTLIL::Design *design, std::map<RTLIL::IdString, RTLIL::Const> parameters);
272  virtual RTLIL::Module *clone() const;
273  };
274 
275  // this must be set by the language frontend before parsing the sources
276  // the AstNode constructor then uses current_filename and get_line_num()
277  // to initialize the filename and linenum properties of new nodes
278  extern std::string current_filename;
279  extern void (*set_line_num)(int);
280  extern int (*get_line_num)();
281 
282  // set set_line_num and get_line_num to internal dummy functions (done by simplify() and AstModule::derive
283  // to control the filename and linenum properties of new nodes not generated by a frontend parser)
284  void use_internal_line_num();
285 
286  // call a DPI function
287  AstNode *dpi_call(const std::string &rtype, const std::string &fname, const std::vector<std::string> &argtypes, const std::vector<AstNode*> &args);
288 }
289 
290 namespace AST_INTERNAL
291 {
292  // internal state variables
295  extern std::map<std::string, AST::AstNode*> current_scope;
296  extern const std::map<RTLIL::SigBit, RTLIL::SigBit> *genRTLIL_subst_ptr;
300  struct ProcessGenerator;
301 }
302 
304 
305 #endif
mem2reg_flags
Definition: ast.h:185
std::map< std::string, AstNode * > current_scope
Definition: ast.cc:58
RTLIL::Const bitsAsConst(int width, bool is_signed)
Definition: ast.cc:741
bool flag_lib
Definition: ast.cc:56
bool range_valid
Definition: ast.h:158
RTLIL::SigSpec genWidthRTLIL(int width, const std::map< RTLIL::SigBit, RTLIL::SigBit > *new_subst_ptr=NULL)
Definition: genrtlil.cc:1394
static AstNode * mkconst_int(uint32_t v, bool is_signed, int width=32)
Definition: ast.cc:672
AstNode * current_top_block
Definition: ast.cc:61
AstNode * dpi_call(const std::string &rtype, const std::string &fname, const std::vector< std::string > &argtypes, const std::vector< AstNode * > &args)
Definition: dpicall.cc:140
RTLIL::Const val
Definition: ast.h:217
virtual RTLIL::IdString derive(RTLIL::Design *design, std::map< RTLIL::IdString, RTLIL::Const > parameters)
Definition: ast.cc:1021
static AstNode * mkconst_bits(const std::vector< RTLIL::State > &v, bool is_signed)
Definition: ast.cc:688
AstNode * current_block
Definition: ast.cc:61
static AstNode * mkconst_str(const std::vector< RTLIL::State > &v)
Definition: ast.cc:706
RTLIL::Const asParaConst()
Definition: ast.cc:776
#define YOSYS_NAMESPACE_END
Definition: yosys.h:100
void dumpAst(FILE *f, std::string indent)
Definition: ast.cc:250
void mem2reg_as_needed_pass1(std::map< AstNode *, std::set< std::string >> &mem2reg_places, std::map< AstNode *, uint32_t > &mem2reg_flags, std::map< AstNode *, uint32_t > &proc_flags, uint32_t &status_flags)
Definition: simplify.cc:2202
virtual RTLIL::Module * clone() const
Definition: ast.cc:1093
RTLIL::SigSpec genRTLIL(int width_hint=-1, bool sign_hint=false)
Definition: genrtlil.cc:762
void(* set_line_num)(int)
Definition: ast.cc:50
std::map< RTLIL::IdString, AstNode * > attributes
Definition: ast.h:152
void dumpVlog(FILE *f, std::string indent)
Definition: ast.cc:327
bool flag_dump_ast1
Definition: ast.cc:56
bool bits_only_01()
Definition: ast.cc:733
AstNode * id2ast
Definition: ast.h:167
bool mem2reg_check(std::set< AstNode * > &mem2reg_set)
Definition: simplify.cc:2303
AstNode * current_ast
Definition: ast.cc:57
bool get_bool_attribute(RTLIL::IdString id)
Definition: ast.cc:166
bool range_swapped
Definition: ast.h:158
std::string type2str(AstNodeType type)
Definition: ast.cc:66
bool operator==(const AstNode &other) const
Definition: ast.cc:616
bool nolatches
Definition: ast.h:269
void cloneInto(AstNode *other)
Definition: ast.cc:220
RTLIL::SigSpec ignoreThisSignalsInInitial
Definition: ast.cc:60
bool mem2reg
Definition: ast.h:269
double asReal(bool is_signed)
Definition: ast.cc:822
int(* get_line_num)()
Definition: ast.cc:51
int range_right
Definition: ast.h:159
bool flag_autowire
Definition: ast.cc:56
AstNodeType
Definition: ast.h:42
bool is_reg
Definition: ast.h:158
AstNode(AstNodeType type=AST_NONE, AstNode *child1=NULL, AstNode *child2=NULL)
Definition: ast.cc:181
double realvalue
Definition: ast.h:161
bool flag_dump_ast2
Definition: ast.cc:56
void detectSignWidth(int &width_hint, bool &sign_hint, bool *found_real=NULL)
Definition: genrtlil.cc:746
AstNode * current_block_child
Definition: ast.cc:61
virtual ~AstModule()
Definition: ast.cc:1014
bool flag_noopt
Definition: ast.cc:56
list variables
Definition: fsm/generate.py:47
AstNodeType type
Definition: ast.h:146
bool autowire
Definition: ast.h:269
std::vector< int > multirange_dimensions
Definition: ast.h:164
AstNode * eval_const_function(AstNode *fcall)
Definition: simplify.cc:2541
uint32_t integer
Definition: ast.h:160
void detectSignWidthWorker(int &width_hint, bool &sign_hint, bool *found_real=NULL)
Definition: genrtlil.cc:534
int range_left
Definition: ast.h:159
void meminfo(int &mem_width, int &mem_size, int &addr_bits)
Definition: simplify.cc:2479
void expand_genblock(std::string index_var, std::string prefix, std::map< std::string, std::string > &name_map)
Definition: simplify.cc:2113
AstNode * ast
Definition: ast.h:268
bool contains(const AstNode *other) const
Definition: ast.cc:661
std::string current_filename
Definition: ast.cc:49
AstNode * clone()
Definition: ast.cc:208
#define NULL
#define YOSYS_NAMESPACE_BEGIN
Definition: yosys.h:99
bool simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage, int width_hint, bool sign_hint, bool in_param)
Definition: simplify.cc:50
bool nomem2reg
Definition: ast.h:269
std::string filename
Definition: ast.h:175
bool icells
Definition: ast.h:269
void use_internal_line_num()
Definition: ast.cc:1123
AstNode * current_ast_mod
Definition: ast.cc:57
AstNode * readmem(bool is_readmemh, std::string mem_filename, AstNode *memory, int start_addr, int finish_addr)
Definition: simplify.cc:2036
bool is_signed
Definition: ast.h:158
std::string str
Definition: ast.h:156
bool flag_nolatches
Definition: ast.cc:56
std::vector< AstNode * > children
Definition: ast.h:149
void replace_ids(const std::string &prefix, const std::map< std::string, std::string > &rules)
Definition: simplify.cc:2160
bool flag_mem2reg
Definition: ast.cc:56
std::vector< RTLIL::State > bits
Definition: ast.h:157
bool noopt
Definition: ast.h:269
RTLIL::Design * design
Definition: rtlil.h:589
void process(RTLIL::Design *design, AstNode *ast, bool dump_ast1, bool dump_ast2, bool dump_vlog, bool nolatches, bool nomem2reg, bool mem2reg, bool lib, bool noopt, bool icells, bool ignore_redef, bool defer, bool autowire)
Definition: ast.cc:967
bool is_string
Definition: ast.h:158
int linenum
Definition: ast.h:176
const std::map< RTLIL::SigBit, RTLIL::SigBit > * genRTLIL_subst_ptr
Definition: ast.cc:59
AstModule * current_module
Definition: ast.cc:62
~AstNode()
Definition: ast.cc:243
uint64_t asInt(bool is_signed)
Definition: ast.cc:802
bool flag_icells
Definition: ast.cc:56
int isConst()
Definition: ast.cc:793
void replace_variables(std::map< std::string, varinfo_t > &variables, AstNode *fcall)
Definition: simplify.cc:2511
bool basic_prep
Definition: ast.h:170
bool flag_nomem2reg
Definition: ast.cc:56
void delete_children()
Definition: ast.cc:231
RTLIL::Const realAsConst(int width)
Definition: ast.cc:850
void mem2reg_as_needed_pass2(std::set< AstNode * > &mem2reg_set, AstNode *mod, AstNode *block)
Definition: simplify.cc:2315
bool is_output
Definition: ast.h:158
bool lib
Definition: ast.h:269
int port_id
Definition: ast.h:159
bool asBool()
Definition: ast.cc:784
bool has_const_only_constructs(bool &recommend_const_eval)
Definition: simplify.cc:2495
bool operator!=(const AstNode &other) const
Definition: ast.cc:655
bool is_input
Definition: ast.h:158
RTLIL::Const asAttrConst()
Definition: ast.cc:761