Initial implementation of elaboration system tasks
authorUdi Finkelstein <github@udifink.com>
Fri, 3 May 2019 00:10:43 +0000 (03:10 +0300)
committerUdi Finkelstein <github@udifink.com>
Fri, 3 May 2019 00:10:43 +0000 (03:10 +0300)
(IEEE1800-2017 section 20.11)
This PR allows us to use $info/$warning/$error/$fatal **at elaboration time** within a generate block.
This is very useful to stop a synthesis of a parametrized block when an
illegal combination of parameters is chosen.

frontends/ast/ast.cc
frontends/ast/ast.h
frontends/ast/genrtlil.cc
frontends/ast/simplify.cc
frontends/verilog/verilog_lexer.l
frontends/verilog/verilog_parser.y
kernel/log.cc
kernel/log.h
tests/various/elab_sys_tasks.sv [new file with mode: 0644]
tests/various/elab_sys_tasks.ys [new file with mode: 0644]

index 9f88b08c1243d5fb82f6c777c4285c99df70c161..d283455036320a08f7d6a73c8ff1b6e5d20932b0 100644 (file)
@@ -154,6 +154,7 @@ std::string AST::type2str(AstNodeType type)
        X(AST_GENIF)
        X(AST_GENCASE)
        X(AST_GENBLOCK)
+       X(AST_TECALL)
        X(AST_POSEDGE)
        X(AST_NEGEDGE)
        X(AST_EDGE)
index 281cbe0865ba8987fed475e9c7d71944c3ab9d14..b3e6fbaa054bef34d70b09c2ca6a0c2918ea8b44 100644 (file)
@@ -137,7 +137,8 @@ namespace AST
                AST_GENIF,
                AST_GENCASE,
                AST_GENBLOCK,
-
+               AST_TECALL,
+               
                AST_POSEDGE,
                AST_NEGEDGE,
                AST_EDGE,
@@ -233,6 +234,7 @@ namespace AST
                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);
+               bool check_elab_tasks(void);
 
                // additional functionality for evaluating constant functions
                struct varinfo_t { RTLIL::Const val; int offset; bool is_signed; };
index b3a2a84be51e99e6e45c7b05cdcb7b7620b4bde8..d5e7d94ab2bce961c0bb6b56da1676f487a6a3cd 100644 (file)
@@ -856,6 +856,7 @@ RTLIL::SigSpec AstNode::genRTLIL(int width_hint, bool sign_hint)
        case AST_GENVAR:
        case AST_GENFOR:
        case AST_GENBLOCK:
+       case AST_TECALL:
        case AST_GENIF:
        case AST_GENCASE:
        case AST_PACKAGE:
index 4d4b9dfe11fc70019349df17905d87ab82aedee3..e6132995dc1f6ecd3cb390f5444c19179f368f75 100644 (file)
@@ -1146,7 +1146,8 @@ bool AstNode::simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage,
 
                        if (type == AST_GENFOR) {
                                for (size_t i = 0; i < buf->children.size(); i++) {
-                                       buf->children[i]->simplify(false, false, false, stage, -1, false, false);
+                                       if (!buf->children[i]->check_elab_tasks())
+                                               buf->children[i]->simplify(false, false, false, stage, -1, false, false);
                                        current_ast_mod->children.push_back(buf->children[i]);
                                }
                        } else {
@@ -1261,7 +1262,8 @@ bool AstNode::simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage,
                        }
 
                        for (size_t i = 0; i < buf->children.size(); i++) {
-                               buf->children[i]->simplify(false, false, false, stage, -1, false, false);
+                               if (!buf->children[i]->check_elab_tasks())
+                                       buf->children[i]->simplify(false, false, false, stage, -1, false, false);
                                current_ast_mod->children.push_back(buf->children[i]);
                        }
 
@@ -1340,7 +1342,8 @@ bool AstNode::simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage,
                        }
 
                        for (size_t i = 0; i < buf->children.size(); i++) {
-                               buf->children[i]->simplify(false, false, false, stage, -1, false, false);
+                               if (!buf->children[i]->check_elab_tasks())
+                                       buf->children[i]->simplify(false, false, false, stage, -1, false, false);
                                current_ast_mod->children.push_back(buf->children[i]);
                        }
 
@@ -2969,6 +2972,44 @@ static void mark_memories_assign_lhs_complex(dict<AstNode*, pool<std::string>> &
        }
 }
 
+// handle $info(), $warning(), $error(), $fatal()
+// we don't do that in simplify() because we don't know 
+bool AstNode::check_elab_tasks(void)
+{
+       if (type == AST_TECALL) {
+               int sz = children.size();
+               if (str == "$info") {
+                       if (sz > 0)
+                               log_file_info(filename, linenum, "%s.\n", children[0]->str.c_str());
+                       else
+                               log_file_info(filename, linenum, "\n");
+               } else if (str == "$warning") {
+                       if (sz > 0)
+                               log_file_warning(filename, linenum, "%s.\n", children[0]->str.c_str());
+                       else
+                               log_file_warning(filename, linenum, "\n");
+               } else if (str == "$error") {
+                       if (sz > 0)
+                               log_file_error(filename, linenum, "%s.\n", children[0]->str.c_str());
+                       else
+                               log_file_error(filename, linenum, "\n");
+               } else if (str == "$fatal") {
+                       // TODO: 1st parameter, if exists, is 0,1 or 2, and passed to $finish()
+                       // if no parameter is given, default value is 1
+                       // dollar_finish(sz ? children[0] : 1);
+                       // perhaps create & use log_file_fatal()
+                       if (sz > 0)
+                               log_file_error(filename, linenum, "FATAL: %s.\n", children[0]->str.c_str());
+                       else
+                               log_file_error(filename, linenum, "FATAL.\n");
+               } else {
+                       log_file_error(filename, linenum, "Unknown elabortoon system task '%s'.\n", str.c_str());
+               }
+               return true;
+       }
+       return false;
+
+}
 // find memories that should be replaced by registers
 void AstNode::mem2reg_as_needed_pass1(dict<AstNode*, pool<std::string>> &mem2reg_places,
                dict<AstNode*, uint32_t> &mem2reg_candidates, dict<AstNode*, uint32_t> &proc_flags, uint32_t &flags)
index 6ef38252a1f03beeee65e0fa3fb6da9f300cf3b9..1c6810b47927f7840d71a46ab67b62683d4678d8 100644 (file)
@@ -301,6 +301,11 @@ supply1 { return TOK_SUPPLY1; }
        return TOK_ID;
 }
 
+"$"(info|warning|error|fatal) {
+       frontend_verilog_yylval.string = new std::string(yytext);
+       return TOK_ELAB_TASK;
+}
+
 "$signed"   { return TOK_TO_SIGNED; }
 "$unsigned" { return TOK_TO_UNSIGNED; }
 
index 40968d17a12bcaf7052b00b0ca53b62e1b1a3b89..1965f090e499c76c66254c0ee77b347d1b608a1e 100644 (file)
@@ -105,7 +105,7 @@ static void free_attr(std::map<std::string, AstNode*> *al)
        bool boolean;
 }
 
-%token <string> TOK_STRING TOK_ID TOK_CONSTVAL TOK_REALVAL TOK_PRIMITIVE TOK_SVA_LABEL
+%token <string> TOK_STRING TOK_ID TOK_CONSTVAL TOK_REALVAL TOK_PRIMITIVE TOK_SVA_LABEL TOK_ELAB_TASK
 %token TOK_ASSERT TOK_ASSUME TOK_RESTRICT TOK_COVER
 %token ATTR_BEGIN ATTR_END DEFATTR_BEGIN DEFATTR_END
 %token TOK_MODULE TOK_ENDMODULE TOK_PARAMETER TOK_LOCALPARAM TOK_DEFPARAM
@@ -1869,6 +1869,15 @@ gen_stmt:
                if ($6 != NULL)
                        delete $6;
                ast_stack.pop_back();
+       } |
+       TOK_ELAB_TASK {
+               AstNode *node = new AstNode(AST_TECALL);
+               node->str = *$1;
+               delete $1;
+               ast_stack.back()->children.push_back(node);
+               ast_stack.push_back(node);
+       } opt_arg_list ';'{
+               ast_stack.pop_back();           
        };
 
 gen_stmt_block:
index 9a9104e265e379ac19652e3c42414d4727eb18aa..d2a661bb0bdc114673f5297f1616f37d3e21f060 100644 (file)
@@ -278,6 +278,17 @@ void log_file_warning(const std::string &filename, int lineno,
        va_end(ap);
 }
 
+void log_file_info(const std::string &filename, int lineno,
+                      const char *format, ...)
+{
+       va_list ap;
+       va_start(ap, format);
+       std::string prefix = stringf("%s:%d: Info: ",
+                                    filename.c_str(), lineno);
+       logv_warning_with_prefix(prefix.c_str(), format, ap);
+       va_end(ap);
+}
+
 YS_ATTRIBUTE(noreturn)
 static void logv_error_with_prefix(const char *prefix,
                                    const char *format, va_list ap)
index e6afae716a3cb0e88054437be7f99382fe242b8f..3e1facae87c0353dcfb754c130a0b914ce348374 100644 (file)
@@ -80,6 +80,7 @@ void log_warning(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2));
 
 // Log with filename to report a problem in a source file.
 void log_file_warning(const std::string &filename, int lineno, const char *format, ...) YS_ATTRIBUTE(format(printf, 3, 4));
+void log_file_info(const std::string &filename, int lineno, const char *format, ...) YS_ATTRIBUTE(format(printf, 3, 4));
 
 void log_warning_noprefix(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2));
 YS_NORETURN void log_error(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2), noreturn);
diff --git a/tests/various/elab_sys_tasks.sv b/tests/various/elab_sys_tasks.sv
new file mode 100644 (file)
index 0000000..774d85b
--- /dev/null
@@ -0,0 +1,30 @@
+module test;
+localparam X=1;
+genvar i;
+generate
+if (X == 1)
+  $info("X is 1");
+if (X == 1)
+  $warning("X is 1");
+else
+  $error("X is not 1");
+case (X)
+  1: $info("X is 1 in a case statement");
+endcase
+//case (X-1)
+//  1: $warn("X is 2");
+//  default: $warn("X might be anything in a case statement");
+//endcase
+for (i = 0; i < 3; i = i + 1)
+begin
+  case(i)
+    0: $info;
+    1: $warning;
+    default: $info("default case statemnent");
+  endcase
+end
+
+$info("This is a standalone $info(). Next $info has no parameters");
+$info;
+endgenerate
+endmodule
diff --git a/tests/various/elab_sys_tasks.ys b/tests/various/elab_sys_tasks.ys
new file mode 100644 (file)
index 0000000..45bee3a
--- /dev/null
@@ -0,0 +1 @@
+read_verilog -sv elab_sys_tasks.sv