2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
30 #include "kernel/log.h"
33 using namespace Yosys
;
35 std::set
<std::string
> LibertyAst::blacklist
;
36 std::set
<std::string
> LibertyAst::whitelist
;
38 LibertyAst::~LibertyAst()
40 for (auto child
: children
)
45 LibertyAst
*LibertyAst::find(std::string name
)
47 for (auto child
: children
)
48 if (child
->id
== name
)
53 void LibertyAst::dump(FILE *f
, std::string indent
, std::string path
, bool path_ok
)
55 if (whitelist
.count(path
+ "/*") > 0)
60 if (blacklist
.count(id
) > 0 || blacklist
.count(path
) > 0)
62 if (whitelist
.size() > 0 && whitelist
.count(id
) == 0 && whitelist
.count(path
) == 0 && !path_ok
) {
63 fprintf(stderr
, "Automatically added to blacklist: %s\n", path
.c_str());
68 fprintf(f
, "%s%s", indent
.c_str(), id
.c_str());
69 if (!args
.empty() || !children
.empty()) {
71 for (size_t i
= 0; i
< args
.size(); i
++)
72 fprintf(f
, "%s%s", i
> 0 ? ", " : "", args
[i
].c_str());
76 fprintf(f
, " : %s", value
.c_str());
77 if (!children
.empty()) {
79 for (size_t i
= 0; i
< children
.size(); i
++)
80 children
[i
]->dump(f
, indent
+ " ", path
, path_ok
);
81 fprintf(f
, "%s}\n", indent
.c_str());
86 int LibertyParser::lexer(std::string
&str
)
93 } while (c
== ' ' || c
== '\t' || c
== '\r');
95 // search for identifiers, numbers, plus or minus.
96 if (('a' <= c
&& c
<= 'z') || ('A' <= c
&& c
<= 'Z') || ('0' <= c
&& c
<= '9') || c
== '_' || c
== '-' || c
== '+' || c
== '.') {
100 if (('a' <= c
&& c
<= 'z') || ('A' <= c
&& c
<= 'Z') || ('0' <= c
&& c
<= '9') || c
== '_' || c
== '-' || c
== '+' || c
== '.')
106 if (str
== "+" || str
== "-") {
107 /* Single operator is not an identifier */
108 // fprintf(stderr, "LEX: char >>%s<<\n", str.c_str());
112 // fprintf(stderr, "LEX: identifier >>%s<<\n", str.c_str());
117 // if it wasn't an identifer, number of array range,
118 // maybe it's a string?
129 // fprintf(stderr, "LEX: string >>%s<<\n", str.c_str());
133 // if it wasn't a string, perhaps it's a comment or a forward slash?
136 if (c
== '*') { // start of '/*' block comment
138 while (c
> 0 && (last_c
!= '*' || c
!= '/')) {
145 } else if (c
== '/') { // start of '//' line comment
146 while (c
> 0 && c
!= '\n')
152 // fprintf(stderr, "LEX: char >>/<<\n");
153 return '/'; // a single '/' charater.
156 // check for a backslash
167 // check for a new line
173 // anything else, such as ';' will get passed
174 // through as literal items.
176 // if (c >= 32 && c < 255)
177 // fprintf(stderr, "LEX: char >>%c<<\n", c);
179 // fprintf(stderr, "LEX: char %d\n", c);
183 LibertyAst
*LibertyParser::parse()
187 int tok
= lexer(str
);
192 if (tok
== '}' || tok
< 0)
198 LibertyAst
*ast
= new LibertyAst
;
205 // allow both ';' and new lines to
206 // terminate a statement.
207 if ((tok
== ';') || (tok
== 'n'))
210 if (tok
== ':' && ast
->value
.empty()) {
211 tok
= lexer(ast
->value
);
215 while (tok
== '+' || tok
== '-' || tok
== '*' || tok
== '/') {
224 // In a liberty file, all key : value pairs should end in ';'
225 // However, there are some liberty files in the wild that
226 // just have a newline. We'll be kind and accept a newline
227 // instead of the ';' too..
228 if ((tok
== ';') || (tok
== 'n'))
244 // FIXME: the AST needs to be extended to store
245 // these vector ranges.
248 // parse vector range [A] or [A:B]
253 // expected a vector array index
254 error("Expected a number.");
258 // fixme: check for number A
261 // optionally check for : in case of [A:B]
262 // if it isn't we just expect ']'
269 // expected a vector array index
270 error("Expected a number.");
274 // fixme: check for number B
278 // expect a closing bracket of array range
281 error("Expected ']' on array range.");
287 ast
->args
.push_back(arg
);
294 LibertyAst
*child
= parse();
297 ast
->children
.push_back(child
);
310 void LibertyParser::error()
312 log_error("Syntax error in liberty file on line %d.\n", line
);
315 void LibertyParser::error(const std::string
&str
)
317 std::stringstream ss
;
318 ss
<< "Syntax error in liberty file on line " << line
<< ".\n";
319 ss
<< " " << str
<< "\n";
320 log_error("%s", ss
.str().c_str());
325 void LibertyParser::error()
327 fprintf(stderr
, "Syntax error in liberty file on line %d.\n", line
);
331 void LibertyParser::error(const std::string
&str
)
333 std::stringstream ss
;
334 ss
<< "Syntax error in liberty file on line " << line
<< ".\n";
335 ss
<< " " << str
<< "\n";
336 printf("%s", ss
.str().c_str());
340 /**** BEGIN: http://svn.clifford.at/tools/trunk/examples/check.h ****/
342 #define CHECK_NV(result, check) \
344 auto _R = (result); \
346 fprintf(stderr, "Error from '%s' (%ld %s) in %s:%d.\n", \
347 #result, (long int)_R, #check, __FILE__, __LINE__); \
352 #define CHECK_COND(result) \
355 fprintf(stderr, "Error from '%s' in %s:%d.\n", \
356 #result, __FILE__, __LINE__); \
361 /**** END: http://svn.clifford.at/tools/trunk/examples/check.h ****/
363 LibertyAst
*find_non_null(LibertyAst
*node
, const char *name
)
365 LibertyAst
*ret
= node
->find(name
);
367 fprintf(stderr
, "Error: expected to find `%s' node.\n", name
);
371 std::string
func2vl(std::string str
)
373 for (size_t pos
= str
.find_first_of("\" \t"); pos
!= std::string::npos
; pos
= str
.find_first_of("\" \t")) {
374 char c_left
= pos
> 0 ? str
[pos
-1] : ' ';
375 char c_right
= pos
+1 < str
.size() ? str
[pos
+1] : ' ';
376 if (std::string("\" \t*+").find(c_left
) != std::string::npos
)
378 else if (std::string("\" \t*+").find(c_right
) != std::string::npos
)
384 std::vector
<size_t> group_start
;
385 for (size_t pos
= 0; pos
< str
.size(); pos
++) {
387 group_start
.push_back(pos
);
388 if (str
[pos
] == ')' && group_start
.size() > 0) {
389 if (pos
+1 < str
.size() && str
[pos
+1] == '\'') {
390 std::string group
= str
.substr(group_start
.back(), pos
-group_start
.back()+1);
391 str
[group_start
.back()] = '~';
392 str
.replace(group_start
.back()+1, group
.size(), group
);
395 group_start
.pop_back();
397 if (str
[pos
] == '\'' && pos
> 0) {
398 size_t start
= str
.find_last_of("()'*+^&| ", pos
-1)+1;
399 std::string group
= str
.substr(start
, pos
-start
);
401 str
.replace(start
+1, group
.size(), group
);
412 void event2vl(LibertyAst
*ast
, std::string
&edge
, std::string
&expr
)
418 expr
= func2vl(ast
->value
);
419 if (expr
.size() > 0 && expr
[0] == '~')
420 edge
= "negedge " + expr
.substr(1);
422 edge
= "posedge " + expr
;
426 void clear_preset_var(std::string var
, std::string type
)
428 if (type
.find('L') != std::string::npos
) {
429 printf(" %s <= 0;\n", var
.c_str());
432 if (type
.find('H') != std::string::npos
) {
433 printf(" %s <= 1;\n", var
.c_str());
436 if (type
.find('T') != std::string::npos
) {
437 printf(" %s <= ~%s;\n", var
.c_str(), var
.c_str());
440 if (type
.find('X') != std::string::npos
) {
441 printf(" %s <= 'bx;\n", var
.c_str());
446 void gen_verilogsim_cell(LibertyAst
*ast
)
448 if (ast
->find("statetable") != NULL
)
451 CHECK_NV(ast
->args
.size(), == 1);
452 printf("module %s (", ast
->args
[0].c_str());
454 for (auto child
: ast
->children
) {
455 if (child
->id
!= "pin")
457 CHECK_NV(child
->args
.size(), == 1);
458 printf("%s%s", first
? "" : ", ", child
->args
[0].c_str());
463 for (auto child
: ast
->children
) {
464 if (child
->id
!= "ff" && child
->id
!= "latch")
468 for (auto arg
: child
->args
) {
469 printf("%s%s", first
? "" : ", ", arg
.c_str());
475 for (auto child
: ast
->children
) {
476 if (child
->id
!= "pin")
478 CHECK_NV(child
->args
.size(), == 1);
479 LibertyAst
*dir
= find_non_null(child
, "direction");
480 LibertyAst
*func
= child
->find("function");
481 printf(" %s %s;\n", dir
->value
.c_str(), child
->args
[0].c_str());
483 printf(" assign %s = %s; // %s\n", child
->args
[0].c_str(), func2vl(func
->value
).c_str(), func
->value
.c_str());
486 for (auto child
: ast
->children
)
488 if (child
->id
!= "ff" || child
->args
.size() != 2)
491 std::string iq_var
= child
->args
[0];
492 std::string iqn_var
= child
->args
[1];
494 std::string clock_edge
, clock_expr
;
495 event2vl(child
->find("clocked_on"), clock_edge
, clock_expr
);
497 std::string clear_edge
, clear_expr
;
498 event2vl(child
->find("clear"), clear_edge
, clear_expr
);
500 std::string preset_edge
, preset_expr
;
501 event2vl(child
->find("preset"), preset_edge
, preset_expr
);
503 std::string edge
= "";
504 if (!clock_edge
.empty())
505 edge
+= (edge
.empty() ? "" : ", ") + clock_edge
;
506 if (!clear_edge
.empty())
507 edge
+= (edge
.empty() ? "" : ", ") + clear_edge
;
508 if (!preset_edge
.empty())
509 edge
+= (edge
.empty() ? "" : ", ") + preset_edge
;
514 printf(" always @(%s) begin\n", edge
.c_str());
516 const char *else_prefix
= "";
517 if (!clear_expr
.empty() && !preset_expr
.empty()) {
518 printf(" %sif ((%s) && (%s)) begin\n", else_prefix
, clear_expr
.c_str(), preset_expr
.c_str());
519 clear_preset_var(iq_var
, find_non_null(child
, "clear_preset_var1")->value
);
520 clear_preset_var(iqn_var
, find_non_null(child
, "clear_preset_var2")->value
);
522 else_prefix
= "else ";
524 if (!clear_expr
.empty()) {
525 printf(" %sif (%s) begin\n", else_prefix
, clear_expr
.c_str());
526 printf(" %s <= 0;\n", iq_var
.c_str());
527 printf(" %s <= 1;\n", iqn_var
.c_str());
529 else_prefix
= "else ";
531 if (!preset_expr
.empty()) {
532 printf(" %sif (%s) begin\n", else_prefix
, preset_expr
.c_str());
533 printf(" %s <= 1;\n", iq_var
.c_str());
534 printf(" %s <= 0;\n", iqn_var
.c_str());
536 else_prefix
= "else ";
539 printf(" %sbegin\n", else_prefix
);
540 std::string expr
= find_non_null(child
, "next_state")->value
;
541 printf(" // %s\n", expr
.c_str());
542 printf(" %s <= %s;\n", iq_var
.c_str(), func2vl(expr
).c_str());
543 printf(" %s <= ~(%s);\n", iqn_var
.c_str(), func2vl(expr
).c_str());
550 for (auto child
: ast
->children
)
552 if (child
->id
!= "latch" || child
->args
.size() != 2)
555 std::string iq_var
= child
->args
[0];
556 std::string iqn_var
= child
->args
[1];
558 std::string enable_edge
, enable_expr
;
559 event2vl(child
->find("enable"), enable_edge
, enable_expr
);
561 std::string clear_edge
, clear_expr
;
562 event2vl(child
->find("clear"), clear_edge
, clear_expr
);
564 std::string preset_edge
, preset_expr
;
565 event2vl(child
->find("preset"), preset_edge
, preset_expr
);
567 printf(" always @* begin\n");
569 const char *else_prefix
= "";
570 if (!clear_expr
.empty() && !preset_expr
.empty()) {
571 printf(" %sif ((%s) && (%s)) begin\n", else_prefix
, clear_expr
.c_str(), preset_expr
.c_str());
572 clear_preset_var(iq_var
, find_non_null(child
, "clear_preset_var1")->value
);
573 clear_preset_var(iqn_var
, find_non_null(child
, "clear_preset_var2")->value
);
575 else_prefix
= "else ";
577 if (!clear_expr
.empty()) {
578 printf(" %sif (%s) begin\n", else_prefix
, clear_expr
.c_str());
579 printf(" %s <= 0;\n", iq_var
.c_str());
580 printf(" %s <= 1;\n", iqn_var
.c_str());
582 else_prefix
= "else ";
584 if (!preset_expr
.empty()) {
585 printf(" %sif (%s) begin\n", else_prefix
, preset_expr
.c_str());
586 printf(" %s <= 1;\n", iq_var
.c_str());
587 printf(" %s <= 0;\n", iqn_var
.c_str());
589 else_prefix
= "else ";
591 if (!enable_expr
.empty()) {
592 printf(" %sif (%s) begin\n", else_prefix
, enable_expr
.c_str());
593 std::string expr
= find_non_null(child
, "data_in")->value
;
594 printf(" %s <= %s;\n", iq_var
.c_str(), func2vl(expr
).c_str());
595 printf(" %s <= ~(%s);\n", iqn_var
.c_str(), func2vl(expr
).c_str());
597 else_prefix
= "else ";
603 printf("endmodule\n");
606 void gen_verilogsim(LibertyAst
*ast
)
608 CHECK_COND(ast
->id
== "library");
610 for (auto child
: ast
->children
)
611 if (child
->id
== "cell" && !child
->find("dont_use"))
612 gen_verilogsim_cell(child
);
617 fprintf(stderr
, "Usage: filterlib [rules-file [liberty-file]]\n");
618 fprintf(stderr
, " or: filterlib -verilogsim [liberty-file]\n");
622 int main(int argc
, char **argv
)
624 bool flag_verilogsim
= false;
631 if (!strcmp(argv
[1], "-verilogsim"))
632 flag_verilogsim
= true;
633 if (!strcmp(argv
[1], "-") || !strcmp(argv
[1], "-verilogsim"))
635 LibertyAst::whitelist
.insert("/library");
636 LibertyAst::whitelist
.insert("/library/cell");
637 LibertyAst::whitelist
.insert("/library/cell/area");
638 LibertyAst::whitelist
.insert("/library/cell/cell_footprint");
639 LibertyAst::whitelist
.insert("/library/cell/dont_touch");
640 LibertyAst::whitelist
.insert("/library/cell/dont_use");
641 LibertyAst::whitelist
.insert("/library/cell/ff");
642 LibertyAst::whitelist
.insert("/library/cell/ff/*");
643 LibertyAst::whitelist
.insert("/library/cell/latch");
644 LibertyAst::whitelist
.insert("/library/cell/latch/*");
645 LibertyAst::whitelist
.insert("/library/cell/pin");
646 LibertyAst::whitelist
.insert("/library/cell/pin/clock");
647 LibertyAst::whitelist
.insert("/library/cell/pin/direction");
648 LibertyAst::whitelist
.insert("/library/cell/pin/driver_type");
649 LibertyAst::whitelist
.insert("/library/cell/pin/function");
650 LibertyAst::whitelist
.insert("/library/cell/pin_opposite");
651 LibertyAst::whitelist
.insert("/library/cell/pin/state_function");
652 LibertyAst::whitelist
.insert("/library/cell/pin/three_state");
653 LibertyAst::whitelist
.insert("/library/cell/statetable");
654 LibertyAst::whitelist
.insert("/library/cell/statetable/*");
658 FILE *f
= fopen(argv
[1], "r");
660 fprintf(stderr
, "Can't open rules file `%s'.\n", argv
[1]);
665 while (fgets(buffer
, 1024, f
) != NULL
)
669 for (char *p
= buffer
; *p
; p
++)
671 if (*p
== '-' || *p
== '+') {
677 if (*p
== ' ' || *p
== '\t' || *p
== '\r' || *p
== '\n' || *p
== '#') {
680 LibertyAst::blacklist
.insert(id
);
683 LibertyAst::whitelist
.insert(id
);
696 fprintf(stderr
, "Syntax error in rules file:\n%s", buffer
);
703 std::istream
*f
= &std::cin
;
706 std::ifstream
*ff
= new std::ifstream
;
710 fprintf(stderr
, "Can't open liberty file `%s'.\n", argv
[2]);
716 LibertyParser
parser(*f
);
719 gen_verilogsim(parser
.ast
);
721 parser
.ast
->dump(stdout
);