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 PASS_DFFLIBMAP
;
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
)
48 for (auto child
: children
)
49 if (child
->id
== name
)
54 void LibertyAst::dump(FILE *f
, std::string indent
, std::string path
, bool path_ok
)
56 if (whitelist
.count(path
+ "/*") > 0)
61 if (blacklist
.count(id
) > 0 || blacklist
.count(path
) > 0)
63 if (whitelist
.size() > 0 && whitelist
.count(id
) == 0 && whitelist
.count(path
) == 0 && !path_ok
) {
64 fprintf(stderr
, "Automatically added to blacklist: %s\n", path
.c_str());
69 fprintf(f
, "%s%s", indent
.c_str(), id
.c_str());
70 if (!args
.empty() || !children
.empty()) {
72 for (size_t i
= 0; i
< args
.size(); i
++)
73 fprintf(f
, "%s%s", i
> 0 ? ", " : "", args
[i
].c_str());
77 fprintf(f
, " : %s", value
.c_str());
78 if (!children
.empty()) {
80 for (size_t i
= 0; i
< children
.size(); i
++)
81 children
[i
]->dump(f
, indent
+ " ", path
, path_ok
);
82 fprintf(f
, "%s}\n", indent
.c_str());
87 int LibertyParser::lexer(std::string
&str
)
93 } while (c
== ' ' || c
== '\t' || c
== '\r');
95 if (('a' <= c
&& c
<= 'z') || ('A' <= c
&& c
<= 'Z') || ('0' <= c
&& c
<= '9') || c
== '_' || c
== '-' || c
== '+' || c
== '.') {
99 if (('a' <= c
&& c
<= 'z') || ('A' <= c
&& c
<= 'Z') || ('0' <= c
&& c
<= '9') || c
== '_' || c
== '-' || c
== '+' || c
== '.')
105 // fprintf(stderr, "LEX: identifier >>%s<<\n", str.c_str());
119 // fprintf(stderr, "LEX: string >>%s<<\n", str.c_str());
127 while (c
> 0 && (last_c
!= '*' || c
!= '/')) {
134 } else if (c
== '/') {
135 while (c
> 0 && c
!= '\n')
141 // fprintf(stderr, "LEX: char >>/<<\n");
160 // if (c >= 32 && c < 255)
161 // fprintf(stderr, "LEX: char >>%c<<\n", c);
163 // fprintf(stderr, "LEX: char %d\n", c);
167 LibertyAst
*LibertyParser::parse()
171 int tok
= lexer(str
);
176 if (tok
== '}' || tok
< 0)
182 LibertyAst
*ast
= new LibertyAst
;
192 if (tok
== ':' && ast
->value
.empty()) {
193 tok
= lexer(ast
->value
);
209 ast
->args
.push_back(arg
);
216 LibertyAst
*child
= parse();
219 ast
->children
.push_back(child
);
232 void LibertyParser::error()
234 log_error("Syntax error in line %d.\n", line
);
239 void LibertyParser::error()
241 fprintf(stderr
, "Syntax error in line %d.\n", line
);
245 /**** BEGIN: http://svn.clifford.at/tools/trunk/examples/check.h ****/
247 // This is to not confuse the VIM syntax highlighting
248 #define CHECK_VAL_OPEN (
249 #define CHECK_VAL_CLOSE )
251 #define CHECK(result, check) \
253 auto _R = (result); \
255 fprintf(stderr, "Error from '%s' (%ld %s) in %s:%d.\n", \
256 #result, (long int)_R, #check, __FILE__, __LINE__); \
262 #define CHECK_NV(result, check) \
264 auto _R = (result); \
266 fprintf(stderr, "Error from '%s' (%ld %s) in %s:%d.\n", \
267 #result, (long int)_R, #check, __FILE__, __LINE__); \
272 #define CHECK_COND(result) \
275 fprintf(stderr, "Error from '%s' in %s:%d.\n", \
276 #result, __FILE__, __LINE__); \
281 /**** END: http://svn.clifford.at/tools/trunk/examples/check.h ****/
283 std::string
func2vl(std::string str
)
285 for (size_t pos
= str
.find_first_of("\" \t"); pos
!= std::string::npos
; pos
= str
.find_first_of("\" \t")) {
286 char c_left
= pos
> 0 ? str
[pos
-1] : ' ';
287 char c_right
= pos
+1 < str
.size() ? str
[pos
+1] : ' ';
288 if (std::string("\" \t*+").find(c_left
) != std::string::npos
)
290 else if (std::string("\" \t*+").find(c_right
) != std::string::npos
)
296 std::vector
<size_t> group_start
;
297 for (size_t pos
= 0; pos
< str
.size(); pos
++) {
299 group_start
.push_back(pos
);
300 if (str
[pos
] == ')' && group_start
.size() > 0) {
301 if (pos
+1 < str
.size() && str
[pos
+1] == '\'') {
302 std::string group
= str
.substr(group_start
.back(), pos
-group_start
.back()+1);
303 str
[group_start
.back()] = '~';
304 str
.replace(group_start
.back()+1, group
.size(), group
);
307 group_start
.pop_back();
309 if (str
[pos
] == '\'' && pos
> 0) {
310 size_t start
= str
.find_last_of("()'*+^&| ", pos
-1)+1;
311 std::string group
= str
.substr(start
, pos
-start
);
313 str
.replace(start
+1, group
.size(), group
);
324 void event2vl(LibertyAst
*ast
, std::string
&edge
, std::string
&expr
)
330 expr
= func2vl(ast
->value
);
331 if (expr
.size() > 0 && expr
[0] == '~')
332 edge
= "negedge " + expr
.substr(1);
334 edge
= "posedge " + expr
;
338 void clear_preset_var(std::string var
, std::string type
)
340 if (type
.find('L') != std::string::npos
) {
341 printf(" %s <= 0;\n", var
.c_str());
344 if (type
.find('H') != std::string::npos
) {
345 printf(" %s <= 1;\n", var
.c_str());
348 if (type
.find('T') != std::string::npos
) {
349 printf(" %s <= ~%s;\n", var
.c_str(), var
.c_str());
352 if (type
.find('X') != std::string::npos
) {
353 printf(" %s <= 'bx;\n", var
.c_str());
358 void gen_verilogsim_cell(LibertyAst
*ast
)
360 if (ast
->find("statetable") != NULL
)
363 CHECK_NV(ast
->args
.size(), == 1);
364 printf("module %s (", ast
->args
[0].c_str());
366 for (auto child
: ast
->children
) {
367 if (child
->id
!= "pin")
369 CHECK_NV(child
->args
.size(), == 1);
370 printf("%s%s", first
? "" : ", ", child
->args
[0].c_str());
375 for (auto child
: ast
->children
) {
376 if (child
->id
!= "ff" && child
->id
!= "latch")
380 for (auto arg
: child
->args
) {
381 printf("%s%s", first
? "" : ", ", arg
.c_str());
387 for (auto child
: ast
->children
) {
388 if (child
->id
!= "pin")
390 CHECK_NV(child
->args
.size(), == 1);
391 LibertyAst
*dir
= CHECK(child
->find("direction"), != NULL
);
392 LibertyAst
*func
= child
->find("function");
393 printf(" %s %s;\n", dir
->value
.c_str(), child
->args
[0].c_str());
395 printf(" assign %s = %s; // %s\n", child
->args
[0].c_str(), func2vl(func
->value
).c_str(), func
->value
.c_str());
398 for (auto child
: ast
->children
)
400 if (child
->id
!= "ff" || child
->args
.size() != 2)
403 std::string iq_var
= child
->args
[0];
404 std::string iqn_var
= child
->args
[1];
406 std::string clock_edge
, clock_expr
;
407 event2vl(child
->find("clocked_on"), clock_edge
, clock_expr
);
409 std::string clear_edge
, clear_expr
;
410 event2vl(child
->find("clear"), clear_edge
, clear_expr
);
412 std::string preset_edge
, preset_expr
;
413 event2vl(child
->find("preset"), preset_edge
, preset_expr
);
415 std::string edge
= "";
416 if (!clock_edge
.empty())
417 edge
+= (edge
.empty() ? "" : ", ") + clock_edge
;
418 if (!clear_edge
.empty())
419 edge
+= (edge
.empty() ? "" : ", ") + clear_edge
;
420 if (!preset_edge
.empty())
421 edge
+= (edge
.empty() ? "" : ", ") + preset_edge
;
426 printf(" always @(%s) begin\n", edge
.c_str());
428 const char *else_prefix
= "";
429 if (!clear_expr
.empty() && !preset_expr
.empty()) {
430 printf(" %sif ((%s) && (%s)) begin\n", else_prefix
, clear_expr
.c_str(), preset_expr
.c_str());
431 clear_preset_var(iq_var
, CHECK(child
->find("clear_preset_var1"), != NULL
)->value
);
432 clear_preset_var(iqn_var
, CHECK(child
->find("clear_preset_var2"), != NULL
)->value
);
434 else_prefix
= "else ";
436 if (!clear_expr
.empty()) {
437 printf(" %sif (%s) begin\n", else_prefix
, clear_expr
.c_str());
438 printf(" %s <= 0;\n", iq_var
.c_str());
439 printf(" %s <= 1;\n", iqn_var
.c_str());
441 else_prefix
= "else ";
443 if (!preset_expr
.empty()) {
444 printf(" %sif (%s) begin\n", else_prefix
, preset_expr
.c_str());
445 printf(" %s <= 1;\n", iq_var
.c_str());
446 printf(" %s <= 0;\n", iqn_var
.c_str());
448 else_prefix
= "else ";
451 printf(" %sbegin\n", else_prefix
);
452 std::string expr
= CHECK(child
->find("next_state"), != NULL
)->value
;
453 printf(" // %s\n", expr
.c_str());
454 printf(" %s <= %s;\n", iq_var
.c_str(), func2vl(expr
).c_str());
455 printf(" %s <= ~(%s);\n", iqn_var
.c_str(), func2vl(expr
).c_str());
462 for (auto child
: ast
->children
)
464 if (child
->id
!= "latch" || child
->args
.size() != 2)
467 std::string iq_var
= child
->args
[0];
468 std::string iqn_var
= child
->args
[1];
470 std::string enable_edge
, enable_expr
;
471 event2vl(child
->find("enable"), enable_edge
, enable_expr
);
473 std::string clear_edge
, clear_expr
;
474 event2vl(child
->find("clear"), clear_edge
, clear_expr
);
476 std::string preset_edge
, preset_expr
;
477 event2vl(child
->find("preset"), preset_edge
, preset_expr
);
479 printf(" always @* begin\n");
481 const char *else_prefix
= "";
482 if (!clear_expr
.empty() && !preset_expr
.empty()) {
483 printf(" %sif ((%s) && (%s)) begin\n", else_prefix
, clear_expr
.c_str(), preset_expr
.c_str());
484 clear_preset_var(iq_var
, CHECK(child
->find("clear_preset_var1"), != NULL
)->value
);
485 clear_preset_var(iqn_var
, CHECK(child
->find("clear_preset_var2"), != NULL
)->value
);
487 else_prefix
= "else ";
489 if (!clear_expr
.empty()) {
490 printf(" %sif (%s) begin\n", else_prefix
, clear_expr
.c_str());
491 printf(" %s <= 0;\n", iq_var
.c_str());
492 printf(" %s <= 1;\n", iqn_var
.c_str());
494 else_prefix
= "else ";
496 if (!preset_expr
.empty()) {
497 printf(" %sif (%s) begin\n", else_prefix
, preset_expr
.c_str());
498 printf(" %s <= 1;\n", iq_var
.c_str());
499 printf(" %s <= 0;\n", iqn_var
.c_str());
501 else_prefix
= "else ";
503 if (!enable_expr
.empty()) {
504 printf(" %sif (%s) begin\n", else_prefix
, enable_expr
.c_str());
505 std::string expr
= CHECK(child
->find("data_in"), != NULL
)->value
;
506 printf(" %s <= %s;\n", iq_var
.c_str(), func2vl(expr
).c_str());
507 printf(" %s <= ~(%s);\n", iqn_var
.c_str(), func2vl(expr
).c_str());
509 else_prefix
= "else ";
515 printf("endmodule\n");
518 void gen_verilogsim(LibertyAst
*ast
)
520 CHECK_COND(ast
->id
== "library");
522 for (auto child
: ast
->children
)
523 if (child
->id
== "cell" && !child
->find("dont_use"))
524 gen_verilogsim_cell(child
);
529 fprintf(stderr
, "Usage: filterlib [rules-file [liberty-file]]\n");
530 fprintf(stderr
, " or: filterlib -verilogsim [liberty-file]\n");
534 int main(int argc
, char **argv
)
536 bool flag_verilogsim
= false;
543 if (!strcmp(argv
[1], "-verilogsim"))
544 flag_verilogsim
= true;
545 if (!strcmp(argv
[1], "-") || !strcmp(argv
[1], "-verilogsim"))
547 LibertyAst::whitelist
.insert("/library");
548 LibertyAst::whitelist
.insert("/library/cell");
549 LibertyAst::whitelist
.insert("/library/cell/area");
550 LibertyAst::whitelist
.insert("/library/cell/cell_footprint");
551 LibertyAst::whitelist
.insert("/library/cell/dont_touch");
552 LibertyAst::whitelist
.insert("/library/cell/dont_use");
553 LibertyAst::whitelist
.insert("/library/cell/ff");
554 LibertyAst::whitelist
.insert("/library/cell/ff/*");
555 LibertyAst::whitelist
.insert("/library/cell/latch");
556 LibertyAst::whitelist
.insert("/library/cell/latch/*");
557 LibertyAst::whitelist
.insert("/library/cell/pin");
558 LibertyAst::whitelist
.insert("/library/cell/pin/clock");
559 LibertyAst::whitelist
.insert("/library/cell/pin/direction");
560 LibertyAst::whitelist
.insert("/library/cell/pin/driver_type");
561 LibertyAst::whitelist
.insert("/library/cell/pin/function");
562 LibertyAst::whitelist
.insert("/library/cell/pin_opposite");
563 LibertyAst::whitelist
.insert("/library/cell/pin/state_function");
564 LibertyAst::whitelist
.insert("/library/cell/pin/three_state");
565 LibertyAst::whitelist
.insert("/library/cell/statetable");
566 LibertyAst::whitelist
.insert("/library/cell/statetable/*");
570 FILE *f
= fopen(argv
[1], "r");
572 fprintf(stderr
, "Can't open rules file `%s'.\n", argv
[1]);
577 while (fgets(buffer
, 1024, f
) != NULL
)
581 for (char *p
= buffer
; *p
; p
++)
583 if (*p
== '-' || *p
== '+') {
589 if (*p
== ' ' || *p
== '\t' || *p
== '\r' || *p
== '\n' || *p
== '#') {
592 LibertyAst::blacklist
.insert(id
);
595 LibertyAst::whitelist
.insert(id
);
608 fprintf(stderr
, "Syntax error in rules file:\n%s", buffer
);
615 std::istream
*f
= &std::cin
;
618 std::ifstream
*ff
= new std::ifstream
;
622 fprintf(stderr
, "Can't open liberty file `%s'.\n", argv
[2]);
628 LibertyParser
parser(*f
);
631 gen_verilogsim(parser
.ast
);
633 parser
.ast
->dump(stdout
);