2013-01-05 04:13:26 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* This is the AST frontend library.
|
|
|
|
*
|
|
|
|
* The AST frontend library is not a frontend on it's own but provides a
|
|
|
|
* generic abstract syntax tree (AST) abstraction for HDL code and can be
|
|
|
|
* used by HDL frontends. See "ast.h" for an overview of the API and the
|
|
|
|
* Verilog frontend for an usage example.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AST_H
|
|
|
|
#define AST_H
|
|
|
|
|
|
|
|
#include "kernel/rtlil.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
namespace AST
|
|
|
|
{
|
|
|
|
// all node types, type2str() must be extended
|
|
|
|
// whenever a new node type is added here
|
|
|
|
enum AstNodeType
|
|
|
|
{
|
|
|
|
AST_NONE,
|
|
|
|
AST_DESIGN,
|
|
|
|
AST_MODULE,
|
|
|
|
AST_TASK,
|
|
|
|
AST_FUNCTION,
|
|
|
|
|
|
|
|
AST_WIRE,
|
|
|
|
AST_MEMORY,
|
|
|
|
AST_AUTOWIRE,
|
|
|
|
AST_PARAMETER,
|
|
|
|
AST_LOCALPARAM,
|
2013-07-04 07:12:33 -05:00
|
|
|
AST_DEFPARAM,
|
2013-01-05 04:13:26 -06:00
|
|
|
AST_PARASET,
|
|
|
|
AST_ARGUMENT,
|
|
|
|
AST_RANGE,
|
|
|
|
AST_CONSTANT,
|
2014-06-13 04:29:23 -05:00
|
|
|
AST_REALVALUE,
|
2013-01-05 04:13:26 -06:00
|
|
|
AST_CELLTYPE,
|
|
|
|
AST_IDENTIFIER,
|
2013-02-26 06:18:22 -06:00
|
|
|
AST_PREFIX,
|
2014-01-18 21:18:22 -06:00
|
|
|
AST_ASSERT,
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
AST_FCALL,
|
2014-02-01 06:50:23 -06:00
|
|
|
AST_TO_BITS,
|
2013-01-05 04:13:26 -06:00
|
|
|
AST_TO_SIGNED,
|
|
|
|
AST_TO_UNSIGNED,
|
|
|
|
AST_CONCAT,
|
|
|
|
AST_REPLICATE,
|
|
|
|
AST_BIT_NOT,
|
|
|
|
AST_BIT_AND,
|
|
|
|
AST_BIT_OR,
|
|
|
|
AST_BIT_XOR,
|
|
|
|
AST_BIT_XNOR,
|
|
|
|
AST_REDUCE_AND,
|
|
|
|
AST_REDUCE_OR,
|
|
|
|
AST_REDUCE_XOR,
|
|
|
|
AST_REDUCE_XNOR,
|
|
|
|
AST_REDUCE_BOOL,
|
|
|
|
AST_SHIFT_LEFT,
|
|
|
|
AST_SHIFT_RIGHT,
|
|
|
|
AST_SHIFT_SLEFT,
|
|
|
|
AST_SHIFT_SRIGHT,
|
|
|
|
AST_LT,
|
|
|
|
AST_LE,
|
|
|
|
AST_EQ,
|
|
|
|
AST_NE,
|
2013-12-27 06:50:08 -06:00
|
|
|
AST_EQX,
|
|
|
|
AST_NEX,
|
2013-01-05 04:13:26 -06:00
|
|
|
AST_GE,
|
|
|
|
AST_GT,
|
|
|
|
AST_ADD,
|
|
|
|
AST_SUB,
|
|
|
|
AST_MUL,
|
|
|
|
AST_DIV,
|
|
|
|
AST_MOD,
|
|
|
|
AST_POW,
|
|
|
|
AST_POS,
|
|
|
|
AST_NEG,
|
|
|
|
AST_LOGIC_AND,
|
|
|
|
AST_LOGIC_OR,
|
|
|
|
AST_LOGIC_NOT,
|
|
|
|
AST_TERNARY,
|
|
|
|
AST_MEMRD,
|
|
|
|
AST_MEMWR,
|
|
|
|
|
|
|
|
AST_TCALL,
|
|
|
|
AST_ASSIGN,
|
|
|
|
AST_CELL,
|
|
|
|
AST_PRIMITIVE,
|
2014-06-07 04:48:50 -05:00
|
|
|
AST_CELLARRAY,
|
2013-01-05 04:13:26 -06:00
|
|
|
AST_ALWAYS,
|
2013-03-31 04:19:11 -05:00
|
|
|
AST_INITIAL,
|
2013-01-05 04:13:26 -06:00
|
|
|
AST_BLOCK,
|
|
|
|
AST_ASSIGN_EQ,
|
|
|
|
AST_ASSIGN_LE,
|
|
|
|
AST_CASE,
|
|
|
|
AST_COND,
|
|
|
|
AST_DEFAULT,
|
|
|
|
AST_FOR,
|
2014-02-14 13:33:22 -06:00
|
|
|
AST_WHILE,
|
2014-06-06 10:40:04 -05:00
|
|
|
AST_REPEAT,
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
AST_GENVAR,
|
|
|
|
AST_GENFOR,
|
|
|
|
AST_GENIF,
|
2013-12-04 14:06:54 -06:00
|
|
|
AST_GENCASE,
|
2013-01-05 04:13:26 -06:00
|
|
|
AST_GENBLOCK,
|
|
|
|
|
|
|
|
AST_POSEDGE,
|
|
|
|
AST_NEGEDGE,
|
|
|
|
AST_EDGE
|
|
|
|
};
|
|
|
|
|
|
|
|
// convert an node type to a string (e.g. for debug output)
|
|
|
|
std::string type2str(AstNodeType type);
|
|
|
|
|
|
|
|
// The AST is built using instances of this struct
|
|
|
|
struct AstNode
|
|
|
|
{
|
|
|
|
// this nodes type
|
|
|
|
AstNodeType type;
|
|
|
|
|
|
|
|
// the list of child nodes for this node
|
|
|
|
std::vector<AstNode*> children;
|
|
|
|
|
|
|
|
// the list of attributes assigned to this node
|
|
|
|
std::map<RTLIL::IdString, AstNode*> attributes;
|
2013-10-24 04:20:13 -05:00
|
|
|
bool get_bool_attribute(RTLIL::IdString id);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
// node content - most of it is unused in most node types
|
|
|
|
std::string str;
|
|
|
|
std::vector<RTLIL::State> bits;
|
2013-12-05 06:26:17 -06:00
|
|
|
bool is_input, is_output, is_reg, is_signed, is_string, range_valid;
|
2013-01-05 04:13:26 -06:00
|
|
|
int port_id, range_left, range_right;
|
|
|
|
uint32_t integer;
|
2014-06-13 04:29:23 -05:00
|
|
|
double realvalue;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
// this is set by simplify and used during RTLIL generation
|
|
|
|
AstNode *id2ast;
|
|
|
|
|
2014-01-20 13:25:20 -06:00
|
|
|
// this is used by simplify to detect if basic analysis has been performed already on the node
|
|
|
|
bool basic_prep;
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
// this is the original sourcecode location that resulted in this AST node
|
|
|
|
// it is automatically set by the constructor using AST::current_filename and
|
|
|
|
// the AST::get_line_num() callback function.
|
|
|
|
std::string filename;
|
|
|
|
int linenum;
|
|
|
|
|
|
|
|
// creating and deleting nodes
|
|
|
|
AstNode(AstNodeType type = AST_NONE, AstNode *child1 = NULL, AstNode *child2 = NULL);
|
|
|
|
AstNode *clone();
|
|
|
|
void cloneInto(AstNode *other);
|
|
|
|
void delete_children();
|
|
|
|
~AstNode();
|
|
|
|
|
2013-11-21 06:49:00 -06:00
|
|
|
enum mem2reg_flags
|
|
|
|
{
|
|
|
|
/* status flags */
|
|
|
|
MEM2REG_FL_ALL = 0x00000001,
|
|
|
|
MEM2REG_FL_ASYNC = 0x00000002,
|
|
|
|
MEM2REG_FL_INIT = 0x00000004,
|
|
|
|
|
|
|
|
/* candidate flags */
|
|
|
|
MEM2REG_FL_FORCED = 0x00000100,
|
|
|
|
MEM2REG_FL_SET_INIT = 0x00000200,
|
|
|
|
MEM2REG_FL_SET_ELSE = 0x00000400,
|
|
|
|
MEM2REG_FL_SET_ASYNC = 0x00000800,
|
|
|
|
MEM2REG_FL_EQ2 = 0x00001000,
|
2014-06-17 14:39:25 -05:00
|
|
|
MEM2REG_FL_CMPLX_LHS = 0x00002000,
|
2013-11-21 06:49:00 -06:00
|
|
|
|
|
|
|
/* proc flags */
|
|
|
|
MEM2REG_FL_EQ1 = 0x01000000,
|
|
|
|
};
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
// simplify() creates a simpler AST by unrolling for-loops, expanding generate blocks, etc.
|
|
|
|
// it also sets the id2ast pointers so that identifier lookups are fast in genRTLIL()
|
2014-02-14 12:56:44 -06:00
|
|
|
bool simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage, int width_hint, bool sign_hint, bool in_param);
|
2013-01-05 04:13:26 -06:00
|
|
|
void expand_genblock(std::string index_var, std::string prefix, std::map<std::string, std::string> &name_map);
|
|
|
|
void replace_ids(std::map<std::string, std::string> &rules);
|
2013-11-21 06:49:00 -06:00
|
|
|
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);
|
2013-03-25 11:13:14 -05:00
|
|
|
void mem2reg_as_needed_pass2(std::set<AstNode*> &mem2reg_set, AstNode *mod, AstNode *block);
|
2013-01-05 04:13:26 -06:00
|
|
|
void meminfo(int &mem_width, int &mem_size, int &addr_bits);
|
|
|
|
|
2014-02-14 12:56:44 -06:00
|
|
|
// additional functionality for evaluating constant functions
|
|
|
|
struct varinfo_t { RTLIL::Const val; int offset; bool is_signed; };
|
2014-06-06 17:02:05 -05:00
|
|
|
bool has_const_only_constructs(bool &recommend_const_eval);
|
2014-02-14 12:56:44 -06:00
|
|
|
void replace_variables(std::map<std::string, varinfo_t> &variables, AstNode *fcall);
|
|
|
|
AstNode *eval_const_function(AstNode *fcall);
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
// create a human-readable text representation of the AST (for debugging)
|
2013-08-19 12:49:14 -05:00
|
|
|
void dumpAst(FILE *f, std::string indent);
|
2013-01-05 04:13:26 -06:00
|
|
|
void dumpVlog(FILE *f, std::string indent);
|
|
|
|
|
2013-07-09 07:31:57 -05:00
|
|
|
// used by genRTLIL() for detecting expression width and sign
|
2014-06-16 08:00:57 -05:00
|
|
|
void detectSignWidthWorker(int &width_hint, bool &sign_hint, bool *found_real = NULL);
|
|
|
|
void detectSignWidth(int &width_hint, bool &sign_hint, bool *found_real = NULL);
|
2013-07-09 07:31:57 -05:00
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
// create RTLIL code for this AST node
|
|
|
|
// for expressions the resulting signal vector is returned
|
|
|
|
// all generated cell instances, etc. are written to the RTLIL::Module pointed to by AST_INTERNAL::current_module
|
2013-07-09 07:31:57 -05:00
|
|
|
RTLIL::SigSpec genRTLIL(int width_hint = -1, bool sign_hint = false);
|
2013-01-05 04:13:26 -06:00
|
|
|
RTLIL::SigSpec genWidthRTLIL(int width, RTLIL::SigSpec *subst_from = NULL, RTLIL::SigSpec *subst_to = NULL);
|
|
|
|
|
|
|
|
// compare AST nodes
|
|
|
|
bool operator==(const AstNode &other) const;
|
|
|
|
bool operator!=(const AstNode &other) const;
|
|
|
|
bool contains(const AstNode *other) const;
|
|
|
|
|
|
|
|
// helper functions for creating AST nodes for constants
|
|
|
|
static AstNode *mkconst_int(uint32_t v, bool is_signed, int width = 32);
|
|
|
|
static AstNode *mkconst_bits(const std::vector<RTLIL::State> &v, bool is_signed);
|
2013-12-05 06:26:17 -06:00
|
|
|
static AstNode *mkconst_str(const std::vector<RTLIL::State> &v);
|
2013-12-05 05:53:49 -06:00
|
|
|
static AstNode *mkconst_str(const std::string &str);
|
2013-11-02 07:00:17 -05:00
|
|
|
|
|
|
|
// helper function for creating sign-extended const objects
|
|
|
|
RTLIL::Const bitsAsConst(int width, bool is_signed);
|
|
|
|
RTLIL::Const bitsAsConst(int width = -1);
|
2013-12-04 07:14:05 -06:00
|
|
|
RTLIL::Const asAttrConst();
|
|
|
|
RTLIL::Const asParaConst();
|
2013-12-04 14:06:54 -06:00
|
|
|
bool asBool();
|
2014-06-14 01:51:22 -05:00
|
|
|
|
|
|
|
// helper functions for real valued const eval
|
|
|
|
int isConst(); // return '1' for AST_CONSTANT and '2' for AST_REALVALUE
|
|
|
|
double asReal(bool is_signed);
|
2014-06-14 13:38:05 -05:00
|
|
|
RTLIL::Const realAsConst(int width);
|
2013-01-05 04:13:26 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// process an AST tree (ast must point to an AST_DESIGN node) and generate RTLIL code
|
2014-02-17 07:28:52 -06:00
|
|
|
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);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
// parametric modules are supported directly by the AST library
|
|
|
|
// therfore we need our own derivate of RTLIL::Module with overloaded virtual functions
|
|
|
|
struct AstModule : RTLIL::Module {
|
|
|
|
AstNode *ast;
|
2014-02-17 07:28:52 -06:00
|
|
|
bool nolatches, nomem2reg, mem2reg, lib, noopt, icells, autowire;
|
2013-01-05 04:13:26 -06:00
|
|
|
virtual ~AstModule();
|
2013-12-04 07:24:44 -06:00
|
|
|
virtual RTLIL::IdString derive(RTLIL::Design *design, std::map<RTLIL::IdString, RTLIL::Const> parameters);
|
2013-07-27 07:27:51 -05:00
|
|
|
virtual RTLIL::Module *clone() const;
|
2013-01-05 04:13:26 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// this must be set by the language frontend before parsing the sources
|
|
|
|
// the AstNode constructor then uses current_filename and get_line_num()
|
|
|
|
// to initialize the filename and linenum properties of new nodes
|
|
|
|
extern std::string current_filename;
|
|
|
|
extern void (*set_line_num)(int);
|
|
|
|
extern int (*get_line_num)();
|
|
|
|
|
2013-11-24 10:29:11 -06:00
|
|
|
// set set_line_num and get_line_num to internal dummy functions (done by simplify() and AstModule::derive
|
|
|
|
// to control the filename and linenum properties of new nodes not generated by a frontend parser)
|
2013-01-05 04:13:26 -06:00
|
|
|
void use_internal_line_num();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace AST_INTERNAL
|
|
|
|
{
|
|
|
|
// internal state variables
|
2014-02-17 07:28:52 -06:00
|
|
|
extern bool flag_dump_ast1, flag_dump_ast2, flag_nolatches, flag_nomem2reg, flag_mem2reg, flag_lib, flag_noopt, flag_icells, flag_autowire;
|
2013-01-05 04:13:26 -06:00
|
|
|
extern AST::AstNode *current_ast, *current_ast_mod;
|
|
|
|
extern std::map<std::string, AST::AstNode*> current_scope;
|
2013-03-31 04:51:12 -05:00
|
|
|
extern RTLIL::SigSpec *genRTLIL_subst_from, *genRTLIL_subst_to, ignoreThisSignalsInInitial;
|
2013-01-05 04:13:26 -06:00
|
|
|
extern AST::AstNode *current_top_block, *current_block, *current_block_child;
|
|
|
|
extern AST::AstModule *current_module;
|
|
|
|
struct ProcessGenerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|