Merge pull request #591 from hzeller/virtual-override
[yosys.git] / frontends / ast / ast.h
index 6c15c03ab736b41269c8aad76acd82fe02f227fb..d94199643ccb37ddb93bc7cb99d3f3464a35adfa 100644 (file)
@@ -1,12 +1,12 @@
-/*
+/* -*- c++ -*-
  *  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
@@ -33,6 +33,8 @@
 #include <stdint.h>
 #include <set>
 
+YOSYS_NAMESPACE_BEGIN
+
 namespace AST
 {
        // all node types, type2str() must be extended
@@ -44,6 +46,7 @@ namespace AST
                AST_MODULE,
                AST_TASK,
                AST_FUNCTION,
+               AST_DPI_FUNCTION,
 
                AST_WIRE,
                AST_MEMORY,
@@ -54,12 +57,17 @@ namespace AST
                AST_PARASET,
                AST_ARGUMENT,
                AST_RANGE,
+               AST_MULTIRANGE,
                AST_CONSTANT,
                AST_REALVALUE,
                AST_CELLTYPE,
                AST_IDENTIFIER,
                AST_PREFIX,
                AST_ASSERT,
+               AST_ASSUME,
+               AST_LIVE,
+               AST_FAIR,
+               AST_COVER,
 
                AST_FCALL,
                AST_TO_BITS,
@@ -103,6 +111,7 @@ namespace AST
                AST_TERNARY,
                AST_MEMRD,
                AST_MEMWR,
+               AST_MEMINIT,
 
                AST_TCALL,
                AST_ASSIGN,
@@ -116,6 +125,8 @@ namespace AST
                AST_ASSIGN_LE,
                AST_CASE,
                AST_COND,
+               AST_CONDX,
+               AST_CONDZ,
                AST_DEFAULT,
                AST_FOR,
                AST_WHILE,
@@ -129,7 +140,9 @@ namespace AST
 
                AST_POSEDGE,
                AST_NEGEDGE,
-               AST_EDGE
+               AST_EDGE,
+
+               AST_PACKAGE
        };
 
        // convert an node type to a string (e.g. for debug output)
@@ -138,6 +151,10 @@ namespace AST
        // The AST is built using instances of this struct
        struct AstNode
        {
+               // for dict<> and pool<>
+               unsigned int hashidx_;
+               unsigned int hash() const { return hashidx_; }
+
                // this nodes type
                AstNodeType type;
 
@@ -151,11 +168,14 @@ namespace AST
                // node content - most of it is unused in most node types
                std::string str;
                std::vector<RTLIL::State> bits;
-               bool is_input, is_output, is_reg, is_signed, is_string, range_valid, range_swapped;
+               bool is_input, is_output, is_reg, is_logic, is_signed, is_string, range_valid, range_swapped, was_checked;
                int port_id, range_left, range_right;
                uint32_t integer;
                double realvalue;
 
+               // if this is a multirange memory then this vector contains offset and length of each dimension
+               std::vector<int> multirange_dimensions;
+
                // this is set by simplify and used during RTLIL generation
                AstNode *id2ast;
 
@@ -169,9 +189,9 @@ namespace AST
                int linenum;
 
                // creating and deleting nodes
-               AstNode(AstNodeType type = AST_NONE, AstNode *child1 = NULL, AstNode *child2 = NULL);
-               AstNode *clone();
-               void cloneInto(AstNode *other);
+               AstNode(AstNodeType type = AST_NONE, AstNode *child1 = NULL, AstNode *child2 = NULL, AstNode *child3 = NULL);
+               AstNode *clone() const;
+               void cloneInto(AstNode *other) const;
                void delete_children();
                ~AstNode();
 
@@ -197,11 +217,14 @@ namespace AST
                // 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()
                bool simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage, int width_hint, bool sign_hint, bool in_param);
+               AstNode *readmem(bool is_readmemh, std::string mem_filename, AstNode *memory, int start_addr, int finish_addr, bool unconditional_init);
                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);
-               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);
-               void mem2reg_as_needed_pass2(std::set<AstNode*> &mem2reg_set, AstNode *mod, AstNode *block);
+               void replace_ids(const std::string &prefix, const std::map<std::string, std::string> &rules);
+               void mem2reg_as_needed_pass1(dict<AstNode*, pool<std::string>> &mem2reg_places,
+                               dict<AstNode*, uint32_t> &mem2reg_flags, dict<AstNode*, uint32_t> &proc_flags, uint32_t &status_flags);
+               bool mem2reg_as_needed_pass2(pool<AstNode*> &mem2reg_set, AstNode *mod, AstNode *block, AstNode *&async_block);
+               bool mem2reg_check(pool<AstNode*> &mem2reg_set);
+               void mem2reg_remove(pool<AstNode*> &mem2reg_set, vector<AstNode*> &delnodes);
                void meminfo(int &mem_width, int &mem_size, int &addr_bits);
 
                // additional functionality for evaluating constant functions
@@ -211,8 +234,8 @@ namespace AST
                AstNode *eval_const_function(AstNode *fcall);
 
                // create a human-readable text representation of the AST (for debugging)
-               void dumpAst(FILE *f, std::string indent);
-               void dumpVlog(FILE *f, std::string indent);
+               void dumpAst(FILE *f, std::string indent) const;
+               void dumpVlog(FILE *f, std::string indent) const;
 
                // used by genRTLIL() for detecting expression width and sign
                void detectSignWidthWorker(int &width_hint, bool &sign_hint, bool *found_real = NULL);
@@ -222,7 +245,7 @@ namespace AST
                // 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
                RTLIL::SigSpec genRTLIL(int width_hint = -1, bool sign_hint = false);
-               RTLIL::SigSpec genWidthRTLIL(int width, RTLIL::SigSpec *subst_from = NULL,  RTLIL::SigSpec *subst_to = NULL);
+               RTLIL::SigSpec genWidthRTLIL(int width, const dict<RTLIL::SigBit, RTLIL::SigBit> *new_subst_ptr = NULL);
 
                // compare AST nodes
                bool operator==(const AstNode &other) const;
@@ -240,25 +263,28 @@ namespace AST
                RTLIL::Const bitsAsConst(int width = -1);
                RTLIL::Const asAttrConst();
                RTLIL::Const asParaConst();
-               bool asBool();
+               uint64_t asInt(bool is_signed);
+               bool bits_only_01() const;
+               bool asBool() const;
 
                // helper functions for real valued const eval
-               int isConst(); // return '1' for AST_CONSTANT and '2' for AST_REALVALUE
+               int isConst() const; // return '1' for AST_CONSTANT and '2' for AST_REALVALUE
                double asReal(bool is_signed);
                RTLIL::Const realAsConst(int width);
        };
 
        // process an AST tree (ast must point to an AST_DESIGN node) and generate RTLIL code
-       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);
+       void process(RTLIL::Design *design, AstNode *ast, bool dump_ast1, bool dump_ast2, bool dump_vlog, bool dump_rtlil, bool nolatches, bool nomeminit,
+                       bool nomem2reg, bool mem2reg, bool lib, bool noopt, bool icells, bool nooverwrite, bool overwrite, bool defer, bool autowire);
 
        // parametric modules are supported directly by the AST library
-       // therfore we need our own derivate of RTLIL::Module with overloaded virtual functions
+       // therefore we need our own derivate of RTLIL::Module with overloaded virtual functions
        struct AstModule : RTLIL::Module {
                AstNode *ast;
-               bool nolatches, nomem2reg, mem2reg, lib, noopt, icells, autowire;
-               virtual ~AstModule();
-               virtual RTLIL::IdString derive(RTLIL::Design *design, std::map<RTLIL::IdString, RTLIL::Const> parameters);
-               virtual RTLIL::Module *clone() const;
+               bool nolatches, nomeminit, nomem2reg, mem2reg, lib, noopt, icells, autowire;
+               ~AstModule() YS_OVERRIDE;
+               RTLIL::IdString derive(RTLIL::Design *design, dict<RTLIL::IdString, RTLIL::Const> parameters, bool mayfail) YS_OVERRIDE;
+               RTLIL::Module *clone() const YS_OVERRIDE;
        };
 
        // this must be set by the language frontend before parsing the sources
@@ -271,18 +297,26 @@ namespace AST
        // 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)
        void use_internal_line_num();
+
+       // call a DPI function
+       AstNode *dpi_call(const std::string &rtype, const std::string &fname, const std::vector<std::string> &argtypes, const std::vector<AstNode*> &args);
 }
 
 namespace AST_INTERNAL
 {
        // internal state variables
-       extern bool flag_dump_ast1, flag_dump_ast2, flag_nolatches, flag_nomem2reg, flag_mem2reg, flag_lib, flag_noopt, flag_icells, flag_autowire;
+       extern bool flag_dump_ast1, flag_dump_ast2, flag_dump_rtlil, flag_nolatches, flag_nomeminit;
+       extern bool flag_nomem2reg, flag_mem2reg, flag_lib, flag_noopt, flag_icells, flag_autowire;
        extern AST::AstNode *current_ast, *current_ast_mod;
        extern std::map<std::string, AST::AstNode*> current_scope;
-       extern RTLIL::SigSpec *genRTLIL_subst_from, *genRTLIL_subst_to, ignoreThisSignalsInInitial;
-       extern AST::AstNode *current_top_block, *current_block, *current_block_child;
+       extern const dict<RTLIL::SigBit, RTLIL::SigBit> *genRTLIL_subst_ptr;
+       extern RTLIL::SigSpec ignoreThisSignalsInInitial;
+       extern AST::AstNode *current_always, *current_top_block, *current_block, *current_block_child;
        extern AST::AstModule *current_module;
+       extern bool current_always_clocked;
        struct ProcessGenerator;
 }
 
+YOSYS_NAMESPACE_END
+
 #endif