Added Verilog/AST support for DPI functions (dpi_call() still unimplemented)
authorClifford Wolf <clifford@clifford.at>
Thu, 21 Aug 2014 10:43:51 +0000 (12:43 +0200)
committerClifford Wolf <clifford@clifford.at>
Thu, 21 Aug 2014 10:43:51 +0000 (12:43 +0200)
frontends/ast/Makefile.inc
frontends/ast/ast.cc
frontends/ast/ast.h
frontends/ast/dpicall.cc [new file with mode: 0644]
frontends/ast/genrtlil.cc
frontends/ast/simplify.cc
frontends/verilog/lexer.l
frontends/verilog/parser.y

index 993ead9285dc68323592327eac5807419dc09eb7..91d917c9169947245fbd335ecfed46317d353da4 100644 (file)
@@ -2,4 +2,5 @@
 OBJS += frontends/ast/ast.o
 OBJS += frontends/ast/simplify.o
 OBJS += frontends/ast/genrtlil.o
+OBJS += frontends/ast/dpicall.o
 
index d59ff1bb640b3f9c084e2bcbf1f51ba3bdb3207d..de38eff6c8bf00322b1fab790be309a9450bd4b7 100644 (file)
@@ -68,6 +68,7 @@ std::string AST::type2str(AstNodeType type)
        X(AST_MODULE)
        X(AST_TASK)
        X(AST_FUNCTION)
+       X(AST_DPI_FUNCTION)
        X(AST_WIRE)
        X(AST_MEMORY)
        X(AST_AUTOWIRE)
index ef54d76f4f1d1a826c83d62214606c2dd3b8453b..88917c64bf6cd54295cdc213db6649a95742309a 100644 (file)
@@ -46,6 +46,7 @@ namespace AST
                AST_MODULE,
                AST_TASK,
                AST_FUNCTION,
+               AST_DPI_FUNCTION,
 
                AST_WIRE,
                AST_MEMORY,
@@ -278,6 +279,9 @@ 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
diff --git a/frontends/ast/dpicall.cc b/frontends/ast/dpicall.cc
new file mode 100644 (file)
index 0000000..a1954da
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ *  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.
+ *
+ */
+
+#include "ast.h"
+
+AST::AstNode *AST::dpi_call(const std::string &rtype, const std::string &fname, const std::vector<std::string> &argtypes, const std::vector<AstNode*> &args)
+{
+       AST::AstNode *newNode = nullptr;
+
+       log("Calling DPI function `%s' and returning `%s':\n", fname.c_str(), rtype.c_str());
+
+       log_assert(SIZE(args) == SIZE(argtypes));
+       for (int i = 0; i < SIZE(args); i++)
+               if (argtypes[i] == "real" || argtypes[i] == "shortreal")
+                       log("  arg %d (%s): %f\n", i, argtypes[i].c_str(), args[i]->asReal(args[i]->is_signed));
+               else
+                       log("  arg %d (%s): %d\n", i, argtypes[i].c_str(), args[i]->bitsAsConst().as_int());
+
+       if (rtype == "real" || rtype == "shortreal") {
+               newNode = new AstNode(AST_REALVALUE);
+               newNode->realvalue = 1234;
+       } else {
+               newNode = AstNode::mkconst_int(1234, false);
+       }
+
+       return newNode;
+}
+
index 1936146b3dac6cf5e74129287c2742f687a728e9..506c2bb259c4ef1aa425ffb1eb4858082b2c6382 100644 (file)
@@ -753,6 +753,7 @@ RTLIL::SigSpec AstNode::genRTLIL(int width_hint, bool sign_hint)
        // and are only accessed here thru this references
        case AST_TASK:
        case AST_FUNCTION:
+       case AST_DPI_FUNCTION:
        case AST_AUTOWIRE:
        case AST_LOCALPARAM:
        case AST_DEFPARAM:
index 2572fa4a93d00e14e11be1d958fdfa6b86778275..19f1d0554474ab8c147e5e8efb01481f83c2dd96 100644 (file)
@@ -226,7 +226,7 @@ bool AstNode::simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage,
                                this_wire_scope[node->str] = node;
                        }
                        if (node->type == AST_PARAMETER || node->type == AST_LOCALPARAM || node->type == AST_WIRE || node->type == AST_AUTOWIRE || node->type == AST_GENVAR ||
-                                       node->type == AST_MEMORY || node->type == AST_FUNCTION || node->type == AST_TASK || node->type == AST_CELL) {
+                                       node->type == AST_MEMORY || node->type == AST_FUNCTION || node->type == AST_TASK || node->type == AST_DPI_FUNCTION || node->type == AST_CELL) {
                                backup_scope[node->str] = current_scope[node->str];
                                current_scope[node->str] = node;
                        }
@@ -646,7 +646,7 @@ bool AstNode::simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage,
                if (current_scope.count(str) == 0) {
                        for (auto node : current_ast_mod->children) {
                                if ((node->type == AST_PARAMETER || node->type == AST_LOCALPARAM || node->type == AST_WIRE || node->type == AST_AUTOWIRE || node->type == AST_GENVAR ||
-                                               node->type == AST_MEMORY || node->type == AST_FUNCTION || node->type == AST_TASK) && str == node->str) {
+                                               node->type == AST_MEMORY || node->type == AST_FUNCTION || node->type == AST_TASK || node->type == AST_DPI_FUNCTION) && str == node->str) {
                                        current_scope[node->str] = node;
                                        break;
                                }
@@ -1442,6 +1442,34 @@ skip_dynamic_range_lvalue_expansion:;
                                goto apply_newNode;
                        }
 
+                       if (current_scope.count(str) != 0 && current_scope[str]->type == AST_DPI_FUNCTION)
+                       {
+                               AstNode *dpi_decl = current_scope[str];
+
+                               std::string rtype, fname;
+                               std::vector<std::string> argtypes;
+                               std::vector<AstNode*> args;
+
+                               rtype = RTLIL::unescape_id(dpi_decl->children.at(0)->str);
+                               fname = RTLIL::unescape_id(dpi_decl->str);
+
+                               for (int i = 1; i < SIZE(dpi_decl->children); i++)
+                               {
+                                       if (i-1 >= SIZE(children))
+                                               log_error("Insufficient number of arguments in DPI function call at %s:%d.\n", filename.c_str(), linenum);
+
+                                       argtypes.push_back(RTLIL::unescape_id(dpi_decl->children.at(i)->str));
+                                       args.push_back(children.at(i-1)->clone());
+                                       while (args.back()->simplify(true, false, false, stage, -1, false, true)) { }
+
+                                       if (args.back()->type != AST_CONSTANT && args.back()->type != AST_REALVALUE)
+                                               log_error("Failed to evaluate DPI function with non-constant argument at %s:%d.\n", filename.c_str(), linenum);
+                               }
+
+                               newNode = dpi_call(rtype, fname, argtypes, args);
+                               goto apply_newNode;
+                       }
+
                        if (current_scope.count(str) == 0 || current_scope[str]->type != AST_FUNCTION)
                                log_error("Can't resolve function name `%s' at %s:%d.\n", str.c_str(), filename.c_str(), linenum);
                }
index fdb9bb02cad85a25c4e38f5cbeddc67f80f60b82..cf51aac82c3c33434ccf4ad7d67fdcb995d26e0b 100644 (file)
@@ -74,6 +74,7 @@ YOSYS_NAMESPACE_END
 %x STRING
 %x SYNOPSYS_TRANSLATE_OFF
 %x SYNOPSYS_FLAGS
+%x IMPORT_DPI
 
 %%
 
@@ -274,6 +275,27 @@ supply1 { return TOK_SUPPLY1; }
 <SYNOPSYS_FLAGS>. /* ignore everything else */
 <SYNOPSYS_FLAGS>"*/" { BEGIN(0); }
 
+import[ \t\r\n]+\"(DPI|DPI-C)\"[ \t\r\n]+function[ \t\r\n]+ {
+       BEGIN(IMPORT_DPI);
+       return TOK_DPI_FUNCTION;
+}
+
+<IMPORT_DPI>[(),] {
+       return *yytext;
+}
+
+<IMPORT_DPI>[a-zA-Z_$][a-zA-Z0-9_$]* {
+       frontend_verilog_yylval.string = new std::string(std::string("\\") + yytext);
+       return TOK_ID;
+}
+
+<IMPORT_DPI>[ \t\r\n] /* ignore whitespaces */
+
+<IMPORT_DPI>";" {
+       BEGIN(0);
+       return *yytext;
+}
+
 "\\"[^ \t\r\n]+ {
        frontend_verilog_yylval.string = new std::string(yytext);
        return TOK_ID;
index acd904e55d985667e73f25ca6c2f6ec44a6179ee..1d62bc3bed8dff03d973c41736b3efd44df90d94 100644 (file)
@@ -104,7 +104,7 @@ static void free_attr(std::map<std::string, AstNode*> *al)
 %token TOK_INPUT TOK_OUTPUT TOK_INOUT TOK_WIRE TOK_REG
 %token TOK_INTEGER TOK_SIGNED TOK_ASSIGN TOK_ALWAYS TOK_INITIAL
 %token TOK_BEGIN TOK_END TOK_IF TOK_ELSE TOK_FOR TOK_WHILE TOK_REPEAT
-%token TOK_POSEDGE TOK_NEGEDGE TOK_OR
+%token TOK_DPI_FUNCTION TOK_POSEDGE TOK_NEGEDGE TOK_OR
 %token TOK_CASE TOK_CASEX TOK_CASEZ TOK_ENDCASE TOK_DEFAULT
 %token TOK_FUNCTION TOK_ENDFUNCTION TOK_TASK TOK_ENDTASK
 %token TOK_GENERATE TOK_ENDGENERATE TOK_GENVAR TOK_REAL
@@ -415,6 +415,16 @@ module_body_stmt:
        always_stmt | TOK_GENERATE module_gen_body TOK_ENDGENERATE | defattr | assert_property;
 
 task_func_decl:
+       attr TOK_DPI_FUNCTION TOK_ID TOK_ID {
+               current_function_or_task = new AstNode(AST_DPI_FUNCTION, AstNode::mkconst_str(*$3));
+               current_function_or_task->str = *$4;
+               append_attr(current_function_or_task, $1);
+               ast_stack.back()->children.push_back(current_function_or_task);
+               delete $3;
+               delete $4;
+       } opt_dpi_function_args ';' {
+               current_function_or_task = NULL;
+       } |
        attr TOK_TASK TOK_ID ';' {
                current_function_or_task = new AstNode(AST_TASK);
                current_function_or_task->str = *$3;
@@ -449,6 +459,27 @@ task_func_decl:
                ast_stack.pop_back();
        };
 
+dpi_function_arg:
+       TOK_ID TOK_ID {
+               current_function_or_task->children.push_back(AstNode::mkconst_str(*$1));
+               delete $1;
+               delete $2;
+       } |
+       TOK_ID {
+               current_function_or_task->children.push_back(AstNode::mkconst_str(*$1));
+               delete $1;
+       };
+
+opt_dpi_function_args:
+       '(' dpi_function_args ')' |
+       /* empty */;
+
+dpi_function_args:
+       dpi_function_args ',' dpi_function_arg |
+       dpi_function_args ',' |
+       dpi_function_arg |
+       /* empty */;
+
 opt_signed:
        TOK_SIGNED {
                $$ = true;