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.
29 #include "kernel/log.h"
32 using namespace Yosys
;
34 std::set
<std::string
> LibertyAst::blacklist
;
35 std::set
<std::string
> LibertyAst::whitelist
;
37 LibertyAst::~LibertyAst()
39 for (auto child
: children
)
44 LibertyAst
*LibertyAst::find(std::string name
)
46 for (auto child
: children
)
47 if (child
->id
== name
)
52 void LibertyAst::dump(FILE *f
, std::string indent
, std::string path
, bool path_ok
)
54 if (whitelist
.count(path
+ "/*") > 0)
59 if (blacklist
.count(id
) > 0 || blacklist
.count(path
) > 0)
61 if (whitelist
.size() > 0 && whitelist
.count(id
) == 0 && whitelist
.count(path
) == 0 && !path_ok
) {
62 fprintf(stderr
, "Automatically added to blacklist: %s\n", path
.c_str());
67 fprintf(f
, "%s%s", indent
.c_str(), id
.c_str());
68 if (!args
.empty() || !children
.empty()) {
70 for (size_t i
= 0; i
< args
.size(); i
++)
71 fprintf(f
, "%s%s", i
> 0 ? ", " : "", args
[i
].c_str());
75 fprintf(f
, " : %s", value
.c_str());
76 if (!children
.empty()) {
78 for (size_t i
= 0; i
< children
.size(); i
++)
79 children
[i
]->dump(f
, indent
+ " ", path
, path_ok
);
80 fprintf(f
, "%s}\n", indent
.c_str());
85 int LibertyParser::lexer(std::string
&str
)
91 } while (c
== ' ' || c
== '\t' || c
== '\r');
93 if (('a' <= c
&& c
<= 'z') || ('A' <= c
&& c
<= 'Z') || ('0' <= c
&& c
<= '9') || c
== '_' || c
== '-' || c
== '+' || c
== '.' || c
== '[' || c
== ']') {
97 if (('a' <= c
&& c
<= 'z') || ('A' <= c
&& c
<= 'Z') || ('0' <= c
&& c
<= '9') || c
== '_' || c
== '-' || c
== '+' || c
== '.' || c
== '[' || c
== ']')
103 if (str
== "+" || str
== "-") {
104 /* Single operator is not an identifier */
105 // fprintf(stderr, "LEX: char >>%s<<\n", str.c_str());
109 // fprintf(stderr, "LEX: identifier >>%s<<\n", str.c_str());
124 // fprintf(stderr, "LEX: string >>%s<<\n", str.c_str());
132 while (c
> 0 && (last_c
!= '*' || c
!= '/')) {
139 } else if (c
== '/') {
140 while (c
> 0 && c
!= '\n')
146 // fprintf(stderr, "LEX: char >>/<<\n");
165 // if (c >= 32 && c < 255)
166 // fprintf(stderr, "LEX: char >>%c<<\n", c);
168 // fprintf(stderr, "LEX: char %d\n", c);
172 LibertyAst
*LibertyParser::parse()
176 int tok
= lexer(str
);
181 if (tok
== '}' || tok
< 0)
187 LibertyAst
*ast
= new LibertyAst
;
200 if (tok
== ':' && ast
->value
.empty()) {
201 tok
= lexer(ast
->value
);
205 while (tok
== '+' || tok
== '-' || tok
== '*' || tok
== '/') {
230 ast
->args
.push_back(arg
);
237 LibertyAst
*child
= parse();
240 ast
->children
.push_back(child
);
253 void LibertyParser::error()
255 log_error("Syntax error in liberty file on line %d.\n", line
);
260 void LibertyParser::error()
262 fprintf(stderr
, "Syntax error in liberty file on line %d.\n", line
);
266 /**** BEGIN: http://svn.clifford.at/tools/trunk/examples/check.h ****/
268 #define CHECK_NV(result, check) \
270 auto _R = (result); \
272 fprintf(stderr, "Error from '%s' (%ld %s) in %s:%d.\n", \
273 #result, (long int)_R, #check, __FILE__, __LINE__); \
278 #define CHECK_COND(result) \
281 fprintf(stderr, "Error from '%s' in %s:%d.\n", \
282 #result, __FILE__, __LINE__); \
287 /**** END: http://svn.clifford.at/tools/trunk/examples/check.h ****/
289 LibertyAst
*find_non_null(LibertyAst
*node
, const char *name
)
291 LibertyAst
*ret
= node
->find(name
);
293 fprintf(stderr
, "Error: expected to find `%s' node.\n", name
);
297 std::string
func2vl(std::string str
)
299 for (size_t pos
= str
.find_first_of("\" \t"); pos
!= std::string::npos
; pos
= str
.find_first_of("\" \t")) {
300 char c_left
= pos
> 0 ? str
[pos
-1] : ' ';
301 char c_right
= pos
+1 < str
.size() ? str
[pos
+1] : ' ';
302 if (std::string("\" \t*+").find(c_left
) != std::string::npos
)
304 else if (std::string("\" \t*+").find(c_right
) != std::string::npos
)
310 std::vector
<size_t> group_start
;
311 for (size_t pos
= 0; pos
< str
.size(); pos
++) {
313 group_start
.push_back(pos
);
314 if (str
[pos
] == ')' && group_start
.size() > 0) {
315 if (pos
+1 < str
.size() && str
[pos
+1] == '\'') {
316 std::string group
= str
.substr(group_start
.back(), pos
-group_start
.back()+1);
317 str
[group_start
.back()] = '~';
318 str
.replace(group_start
.back()+1, group
.size(), group
);
321 group_start
.pop_back();
323 if (str
[pos
] == '\'' && pos
> 0) {
324 size_t start
= str
.find_last_of("()'*+^&| ", pos
-1)+1;
325 std::string group
= str
.substr(start
, pos
-start
);
327 str
.replace(start
+1, group
.size(), group
);
338 void event2vl(LibertyAst
*ast
, std::string
&edge
, std::string
&expr
)
344 expr
= func2vl(ast
->value
);
345 if (expr
.size() > 0 && expr
[0] == '~')
346 edge
= "negedge " + expr
.substr(1);
348 edge
= "posedge " + expr
;
352 void clear_preset_var(std::string var
, std::string type
)
354 if (type
.find('L') != std::string::npos
) {
355 printf(" %s <= 0;\n", var
.c_str());
358 if (type
.find('H') != std::string::npos
) {
359 printf(" %s <= 1;\n", var
.c_str());
362 if (type
.find('T') != std::string::npos
) {
363 printf(" %s <= ~%s;\n", var
.c_str(), var
.c_str());
366 if (type
.find('X') != std::string::npos
) {
367 printf(" %s <= 'bx;\n", var
.c_str());
372 void gen_verilogsim_cell(LibertyAst
*ast
)
374 if (ast
->find("statetable") != NULL
)
377 CHECK_NV(ast
->args
.size(), == 1);
378 printf("module %s (", ast
->args
[0].c_str());
380 for (auto child
: ast
->children
) {
381 if (child
->id
!= "pin")
383 CHECK_NV(child
->args
.size(), == 1);
384 printf("%s%s", first
? "" : ", ", child
->args
[0].c_str());
389 for (auto child
: ast
->children
) {
390 if (child
->id
!= "ff" && child
->id
!= "latch")
394 for (auto arg
: child
->args
) {
395 printf("%s%s", first
? "" : ", ", arg
.c_str());
401 for (auto child
: ast
->children
) {
402 if (child
->id
!= "pin")
404 CHECK_NV(child
->args
.size(), == 1);
405 LibertyAst
*dir
= find_non_null(child
, "direction");
406 LibertyAst
*func
= child
->find("function");
407 printf(" %s %s;\n", dir
->value
.c_str(), child
->args
[0].c_str());
409 printf(" assign %s = %s; // %s\n", child
->args
[0].c_str(), func2vl(func
->value
).c_str(), func
->value
.c_str());
412 for (auto child
: ast
->children
)
414 if (child
->id
!= "ff" || child
->args
.size() != 2)
417 std::string iq_var
= child
->args
[0];
418 std::string iqn_var
= child
->args
[1];
420 std::string clock_edge
, clock_expr
;
421 event2vl(child
->find("clocked_on"), clock_edge
, clock_expr
);
423 std::string clear_edge
, clear_expr
;
424 event2vl(child
->find("clear"), clear_edge
, clear_expr
);
426 std::string preset_edge
, preset_expr
;
427 event2vl(child
->find("preset"), preset_edge
, preset_expr
);
429 std::string edge
= "";
430 if (!clock_edge
.empty())
431 edge
+= (edge
.empty() ? "" : ", ") + clock_edge
;
432 if (!clear_edge
.empty())
433 edge
+= (edge
.empty() ? "" : ", ") + clear_edge
;
434 if (!preset_edge
.empty())
435 edge
+= (edge
.empty() ? "" : ", ") + preset_edge
;
440 printf(" always @(%s) begin\n", edge
.c_str());
442 const char *else_prefix
= "";
443 if (!clear_expr
.empty() && !preset_expr
.empty()) {
444 printf(" %sif ((%s) && (%s)) begin\n", else_prefix
, clear_expr
.c_str(), preset_expr
.c_str());
445 clear_preset_var(iq_var
, find_non_null(child
, "clear_preset_var1")->value
);
446 clear_preset_var(iqn_var
, find_non_null(child
, "clear_preset_var2")->value
);
448 else_prefix
= "else ";
450 if (!clear_expr
.empty()) {
451 printf(" %sif (%s) begin\n", else_prefix
, clear_expr
.c_str());
452 printf(" %s <= 0;\n", iq_var
.c_str());
453 printf(" %s <= 1;\n", iqn_var
.c_str());
455 else_prefix
= "else ";
457 if (!preset_expr
.empty()) {
458 printf(" %sif (%s) begin\n", else_prefix
, preset_expr
.c_str());
459 printf(" %s <= 1;\n", iq_var
.c_str());
460 printf(" %s <= 0;\n", iqn_var
.c_str());
462 else_prefix
= "else ";
465 printf(" %sbegin\n", else_prefix
);
466 std::string expr
= find_non_null(child
, "next_state")->value
;
467 printf(" // %s\n", expr
.c_str());
468 printf(" %s <= %s;\n", iq_var
.c_str(), func2vl(expr
).c_str());
469 printf(" %s <= ~(%s);\n", iqn_var
.c_str(), func2vl(expr
).c_str());
476 for (auto child
: ast
->children
)
478 if (child
->id
!= "latch" || child
->args
.size() != 2)
481 std::string iq_var
= child
->args
[0];
482 std::string iqn_var
= child
->args
[1];
484 std::string enable_edge
, enable_expr
;
485 event2vl(child
->find("enable"), enable_edge
, enable_expr
);
487 std::string clear_edge
, clear_expr
;
488 event2vl(child
->find("clear"), clear_edge
, clear_expr
);
490 std::string preset_edge
, preset_expr
;
491 event2vl(child
->find("preset"), preset_edge
, preset_expr
);
493 printf(" always @* begin\n");
495 const char *else_prefix
= "";
496 if (!clear_expr
.empty() && !preset_expr
.empty()) {
497 printf(" %sif ((%s) && (%s)) begin\n", else_prefix
, clear_expr
.c_str(), preset_expr
.c_str());
498 clear_preset_var(iq_var
, find_non_null(child
, "clear_preset_var1")->value
);
499 clear_preset_var(iqn_var
, find_non_null(child
, "clear_preset_var2")->value
);
501 else_prefix
= "else ";
503 if (!clear_expr
.empty()) {
504 printf(" %sif (%s) begin\n", else_prefix
, clear_expr
.c_str());
505 printf(" %s <= 0;\n", iq_var
.c_str());
506 printf(" %s <= 1;\n", iqn_var
.c_str());
508 else_prefix
= "else ";
510 if (!preset_expr
.empty()) {
511 printf(" %sif (%s) begin\n", else_prefix
, preset_expr
.c_str());
512 printf(" %s <= 1;\n", iq_var
.c_str());
513 printf(" %s <= 0;\n", iqn_var
.c_str());
515 else_prefix
= "else ";
517 if (!enable_expr
.empty()) {
518 printf(" %sif (%s) begin\n", else_prefix
, enable_expr
.c_str());
519 std::string expr
= find_non_null(child
, "data_in")->value
;
520 printf(" %s <= %s;\n", iq_var
.c_str(), func2vl(expr
).c_str());
521 printf(" %s <= ~(%s);\n", iqn_var
.c_str(), func2vl(expr
).c_str());
523 else_prefix
= "else ";
529 printf("endmodule\n");
532 void gen_verilogsim(LibertyAst
*ast
)
534 CHECK_COND(ast
->id
== "library");
536 for (auto child
: ast
->children
)
537 if (child
->id
== "cell" && !child
->find("dont_use"))
538 gen_verilogsim_cell(child
);
543 fprintf(stderr
, "Usage: filterlib [rules-file [liberty-file]]\n");
544 fprintf(stderr
, " or: filterlib -verilogsim [liberty-file]\n");
548 int main(int argc
, char **argv
)
550 bool flag_verilogsim
= false;
557 if (!strcmp(argv
[1], "-verilogsim"))
558 flag_verilogsim
= true;
559 if (!strcmp(argv
[1], "-") || !strcmp(argv
[1], "-verilogsim"))
561 LibertyAst::whitelist
.insert("/library");
562 LibertyAst::whitelist
.insert("/library/cell");
563 LibertyAst::whitelist
.insert("/library/cell/area");
564 LibertyAst::whitelist
.insert("/library/cell/cell_footprint");
565 LibertyAst::whitelist
.insert("/library/cell/dont_touch");
566 LibertyAst::whitelist
.insert("/library/cell/dont_use");
567 LibertyAst::whitelist
.insert("/library/cell/ff");
568 LibertyAst::whitelist
.insert("/library/cell/ff/*");
569 LibertyAst::whitelist
.insert("/library/cell/latch");
570 LibertyAst::whitelist
.insert("/library/cell/latch/*");
571 LibertyAst::whitelist
.insert("/library/cell/pin");
572 LibertyAst::whitelist
.insert("/library/cell/pin/clock");
573 LibertyAst::whitelist
.insert("/library/cell/pin/direction");
574 LibertyAst::whitelist
.insert("/library/cell/pin/driver_type");
575 LibertyAst::whitelist
.insert("/library/cell/pin/function");
576 LibertyAst::whitelist
.insert("/library/cell/pin_opposite");
577 LibertyAst::whitelist
.insert("/library/cell/pin/state_function");
578 LibertyAst::whitelist
.insert("/library/cell/pin/three_state");
579 LibertyAst::whitelist
.insert("/library/cell/statetable");
580 LibertyAst::whitelist
.insert("/library/cell/statetable/*");
584 FILE *f
= fopen(argv
[1], "r");
586 fprintf(stderr
, "Can't open rules file `%s'.\n", argv
[1]);
591 while (fgets(buffer
, 1024, f
) != NULL
)
595 for (char *p
= buffer
; *p
; p
++)
597 if (*p
== '-' || *p
== '+') {
603 if (*p
== ' ' || *p
== '\t' || *p
== '\r' || *p
== '\n' || *p
== '#') {
606 LibertyAst::blacklist
.insert(id
);
609 LibertyAst::whitelist
.insert(id
);
622 fprintf(stderr
, "Syntax error in rules file:\n%s", buffer
);
629 std::istream
*f
= &std::cin
;
632 std::ifstream
*ff
= new std::ifstream
;
636 fprintf(stderr
, "Can't open liberty file `%s'.\n", argv
[2]);
642 LibertyParser
parser(*f
);
645 gen_verilogsim(parser
.ast
);
647 parser
.ast
->dump(stdout
);