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.
25 #include "kernel/log.h"
28 using namespace PASS_DFFLIBMAP
;
30 std::set
<std::string
> LibertyAst::blacklist
;
31 std::set
<std::string
> LibertyAst::whitelist
;
33 LibertyAst::~LibertyAst()
35 for (auto child
: children
)
40 LibertyAst
*LibertyAst::find(std::string name
)
44 for (auto child
: children
)
45 if (child
->id
== name
)
50 void LibertyAst::dump(FILE *f
, std::string indent
, std::string path
, bool path_ok
)
52 if (whitelist
.count(path
+ "/*") > 0)
57 if (blacklist
.count(id
) > 0 || blacklist
.count(path
) > 0)
59 if (whitelist
.size() > 0 && whitelist
.count(id
) == 0 && whitelist
.count(path
) == 0 && !path_ok
) {
60 fprintf(stderr
, "Automatically added to blacklist: %s\n", path
.c_str());
65 fprintf(f
, "%s%s", indent
.c_str(), id
.c_str());
67 for (size_t i
= 0; i
< args
.size(); i
++)
68 fprintf(f
, "%s%s", i
> 0 ? ", " : "(", args
[i
].c_str());
72 fprintf(f
, " : %s", value
.c_str());
73 if (!children
.empty()) {
75 for (size_t i
= 0; i
< children
.size(); i
++)
76 children
[i
]->dump(f
, indent
+ " ", path
, path_ok
);
77 fprintf(f
, "%s}\n", indent
.c_str());
82 int LibertyParer::lexer(std::string
&str
)
88 } while (c
== ' ' || c
== '\t' || c
== '\r');
90 if (('a' <= c
&& c
<= 'z') || ('A' <= c
&& c
<= 'Z') || ('0' <= c
&& c
<= '9') || c
== '_' || c
== '-' || c
== '.') {
94 if (('a' <= c
&& c
<= 'z') || ('A' <= c
&& c
<= 'Z') || ('0' <= c
&& c
<= '9') || c
== '_' || c
== '-' || c
== '.')
100 // fprintf(stderr, "LEX: identifier >>%s<<\n", str.c_str());
114 // fprintf(stderr, "LEX: string >>%s<<\n", str.c_str());
122 while (c
> 0 && (last_c
!= '*' || c
!= '/')) {
131 // fprintf(stderr, "LEX: char >>/<<\n");
150 // if (c >= 32 && c < 255)
151 // fprintf(stderr, "LEX: char >>%c<<\n", c);
153 // fprintf(stderr, "LEX: char %d\n", c);
157 LibertyAst
*LibertyParer::parse()
161 int tok
= lexer(str
);
166 if (tok
== '}' || tok
< 0)
172 LibertyAst
*ast
= new LibertyAst
;
182 if (tok
== ':' && ast
->value
.empty()) {
183 tok
= lexer(ast
->value
);
199 ast
->args
.push_back(arg
);
206 LibertyAst
*child
= parse();
209 ast
->children
.push_back(child
);
222 void LibertyParer::error()
224 log_error("Syntax error in line %d.\n", line
);
229 void LibertyParer::error()
231 fprintf(stderr
, "Syntax error in line %d.\n", line
);
235 /**** BEGIN: http://svn.clifford.at/tools/trunk/examples/check.h ****/
237 // This is to not confuse the VIM syntax highlighting
238 #define CHECK_VAL_OPEN (
239 #define CHECK_VAL_CLOSE )
241 #define CHECK(result, check) \
243 auto _R = (result); \
245 fprintf(stderr, "Error from '%s' (%ld %s) in %s:%d.\n", \
246 #result, (long int)_R, #check, __FILE__, __LINE__); \
252 #define CHECK_NV(result, check) \
254 auto _R = (result); \
256 fprintf(stderr, "Error from '%s' (%ld %s) in %s:%d.\n", \
257 #result, (long int)_R, #check, __FILE__, __LINE__); \
262 #define CHECK_COND(result) \
265 fprintf(stderr, "Error from '%s' in %s:%d.\n", \
266 #result, __FILE__, __LINE__); \
271 /**** END: http://svn.clifford.at/tools/trunk/examples/check.h ****/
273 std::string
func2vl(std::string str
)
275 for (size_t pos
= str
.find_first_of("\" \t"); pos
!= std::string::npos
; pos
= str
.find_first_of("\" \t")) {
276 char c_left
= pos
> 0 ? str
[pos
-1] : ' ';
277 char c_right
= pos
+1 < str
.size() ? str
[pos
+1] : ' ';
278 if (std::string("\" \t*+").find(c_left
) != std::string::npos
)
280 else if (std::string("\" \t*+").find(c_right
) != std::string::npos
)
286 std::vector
<size_t> group_start
;
287 for (size_t pos
= 0; pos
< str
.size(); pos
++) {
289 group_start
.push_back(pos
);
290 if (str
[pos
] == ')' && group_start
.size() > 0) {
291 if (pos
+1 < str
.size() && str
[pos
+1] == '\'') {
292 std::string group
= str
.substr(group_start
.back(), pos
-group_start
.back()+1);
293 str
[group_start
.back()] = '~';
294 str
.replace(group_start
.back()+1, group
.size(), group
);
297 group_start
.pop_back();
299 if (str
[pos
] == '\'' && pos
> 0) {
300 size_t start
= str
.find_last_of("()'*+^&| ", pos
-1)+1;
301 std::string group
= str
.substr(start
, pos
-start
);
303 str
.replace(start
+1, group
.size(), group
);
314 void event2vl(LibertyAst
*ast
, std::string
&edge
, std::string
&expr
)
320 expr
= func2vl(ast
->value
);
321 if (expr
.size() > 0 && expr
[0] == '~')
322 edge
= "negedge " + expr
.substr(1);
324 edge
= "posedge " + expr
;
328 void clear_preset_var(std::string var
, std::string type
)
330 if (type
.find('L') != std::string::npos
) {
331 printf(" %s <= 0;\n", var
.c_str());
334 if (type
.find('H') != std::string::npos
) {
335 printf(" %s <= 1;\n", var
.c_str());
338 if (type
.find('T') != std::string::npos
) {
339 printf(" %s <= ~%s;\n", var
.c_str(), var
.c_str());
342 if (type
.find('X') != std::string::npos
) {
343 printf(" %s <= 'bx;\n", var
.c_str());
348 void gen_verilogsim_cell(LibertyAst
*ast
)
350 if (ast
->find("statetable") != NULL
)
353 CHECK_NV(ast
->args
.size(), == 1);
354 printf("module %s (", ast
->args
[0].c_str());
356 for (auto child
: ast
->children
) {
357 if (child
->id
!= "pin")
359 CHECK_NV(child
->args
.size(), == 1);
360 printf("%s%s", first
? "" : ", ", child
->args
[0].c_str());
365 for (auto child
: ast
->children
) {
366 if (child
->id
!= "ff" && child
->id
!= "latch")
370 for (auto arg
: child
->args
) {
371 printf("%s%s", first
? "" : ", ", arg
.c_str());
377 for (auto child
: ast
->children
) {
378 if (child
->id
!= "pin")
380 CHECK_NV(child
->args
.size(), == 1);
381 LibertyAst
*dir
= CHECK(child
->find("direction"), != NULL
);
382 LibertyAst
*func
= child
->find("function");
383 printf(" %s %s;\n", dir
->value
.c_str(), child
->args
[0].c_str());
385 printf(" assign %s = %s; // %s\n", child
->args
[0].c_str(), func2vl(func
->value
).c_str(), func
->value
.c_str());
388 for (auto child
: ast
->children
)
390 if (child
->id
!= "ff" || child
->args
.size() != 2)
393 std::string iq_var
= child
->args
[0];
394 std::string iqn_var
= child
->args
[1];
396 std::string clock_edge
, clock_expr
;
397 event2vl(child
->find("clocked_on"), clock_edge
, clock_expr
);
399 std::string clear_edge
, clear_expr
;
400 event2vl(child
->find("clear"), clear_edge
, clear_expr
);
402 std::string preset_edge
, preset_expr
;
403 event2vl(child
->find("preset"), preset_edge
, preset_expr
);
405 std::string edge
= "";
406 if (!clock_edge
.empty())
407 edge
+= (edge
.empty() ? "" : ", ") + clock_edge
;
408 if (!clear_edge
.empty())
409 edge
+= (edge
.empty() ? "" : ", ") + clear_edge
;
410 if (!preset_edge
.empty())
411 edge
+= (edge
.empty() ? "" : ", ") + preset_edge
;
416 printf(" always @(%s) begin\n", edge
.c_str());
418 const char *else_prefix
= "";
419 if (!clear_expr
.empty() && !preset_expr
.empty()) {
420 printf(" %sif ((%s) && (%s)) begin\n", else_prefix
, clear_expr
.c_str(), preset_expr
.c_str());
421 clear_preset_var(iq_var
, CHECK(child
->find("clear_preset_var1"), != NULL
)->value
);
422 clear_preset_var(iqn_var
, CHECK(child
->find("clear_preset_var2"), != NULL
)->value
);
424 else_prefix
= "else ";
426 if (!clear_expr
.empty()) {
427 printf(" %sif (%s) begin\n", else_prefix
, clear_expr
.c_str());
428 printf(" %s <= 0;\n", iq_var
.c_str());
429 printf(" %s <= 1;\n", iqn_var
.c_str());
431 else_prefix
= "else ";
433 if (!preset_expr
.empty()) {
434 printf(" %sif (%s) begin\n", else_prefix
, preset_expr
.c_str());
435 printf(" %s <= 1;\n", iq_var
.c_str());
436 printf(" %s <= 0;\n", iqn_var
.c_str());
438 else_prefix
= "else ";
441 printf(" %sbegin\n", else_prefix
);
442 std::string expr
= CHECK(child
->find("next_state"), != NULL
)->value
;
443 printf(" // %s\n", expr
.c_str());
444 printf(" %s <= %s;\n", iq_var
.c_str(), func2vl(expr
).c_str());
445 printf(" %s <= ~(%s);\n", iqn_var
.c_str(), func2vl(expr
).c_str());
452 for (auto child
: ast
->children
)
454 if (child
->id
!= "latch" || child
->args
.size() != 2)
457 std::string iq_var
= child
->args
[0];
458 std::string iqn_var
= child
->args
[1];
460 std::string enable_edge
, enable_expr
;
461 event2vl(child
->find("enable"), enable_edge
, enable_expr
);
463 std::string clear_edge
, clear_expr
;
464 event2vl(child
->find("clear"), clear_edge
, clear_expr
);
466 std::string preset_edge
, preset_expr
;
467 event2vl(child
->find("preset"), preset_edge
, preset_expr
);
469 printf(" always @* begin\n");
471 const char *else_prefix
= "";
472 if (!clear_expr
.empty() && !preset_expr
.empty()) {
473 printf(" %sif ((%s) && (%s)) begin\n", else_prefix
, clear_expr
.c_str(), preset_expr
.c_str());
474 clear_preset_var(iq_var
, CHECK(child
->find("clear_preset_var1"), != NULL
)->value
);
475 clear_preset_var(iqn_var
, CHECK(child
->find("clear_preset_var2"), != NULL
)->value
);
477 else_prefix
= "else ";
479 if (!clear_expr
.empty()) {
480 printf(" %sif (%s) begin\n", else_prefix
, clear_expr
.c_str());
481 printf(" %s <= 0;\n", iq_var
.c_str());
482 printf(" %s <= 1;\n", iqn_var
.c_str());
484 else_prefix
= "else ";
486 if (!preset_expr
.empty()) {
487 printf(" %sif (%s) begin\n", else_prefix
, preset_expr
.c_str());
488 printf(" %s <= 1;\n", iq_var
.c_str());
489 printf(" %s <= 0;\n", iqn_var
.c_str());
491 else_prefix
= "else ";
493 if (!enable_expr
.empty()) {
494 printf(" %sif (%s) begin\n", else_prefix
, enable_expr
.c_str());
495 std::string expr
= CHECK(child
->find("data_in"), != NULL
)->value
;
496 printf(" %s <= %s;\n", iq_var
.c_str(), func2vl(expr
).c_str());
497 printf(" %s <= ~(%s);\n", iqn_var
.c_str(), func2vl(expr
).c_str());
499 else_prefix
= "else ";
505 printf("endmodule\n");
508 void gen_verilogsim(LibertyAst
*ast
)
510 CHECK_COND(ast
->id
== "library");
512 for (auto child
: ast
->children
)
513 if (child
->id
== "cell" && !child
->find("dont_use"))
514 gen_verilogsim_cell(child
);
519 fprintf(stderr
, "Usage: filterlib [rules-file [liberty-file]]\n");
520 fprintf(stderr
, " or: filterlib -verilogsim [liberty-file]\n");
524 int main(int argc
, char **argv
)
526 bool flag_verilogsim
= false;
533 if (!strcmp(argv
[1], "-verilogsim"))
534 flag_verilogsim
= true;
535 if (!strcmp(argv
[1], "-") || !strcmp(argv
[1], "-verilogsim"))
537 LibertyAst::whitelist
.insert("/library");
538 LibertyAst::whitelist
.insert("/library/cell");
539 LibertyAst::whitelist
.insert("/library/cell/area");
540 LibertyAst::whitelist
.insert("/library/cell/cell_footprint");
541 LibertyAst::whitelist
.insert("/library/cell/dont_touch");
542 LibertyAst::whitelist
.insert("/library/cell/dont_use");
543 LibertyAst::whitelist
.insert("/library/cell/ff");
544 LibertyAst::whitelist
.insert("/library/cell/ff/*");
545 LibertyAst::whitelist
.insert("/library/cell/latch");
546 LibertyAst::whitelist
.insert("/library/cell/latch/*");
547 LibertyAst::whitelist
.insert("/library/cell/pin");
548 LibertyAst::whitelist
.insert("/library/cell/pin/clock");
549 LibertyAst::whitelist
.insert("/library/cell/pin/direction");
550 LibertyAst::whitelist
.insert("/library/cell/pin/driver_type");
551 LibertyAst::whitelist
.insert("/library/cell/pin/function");
552 LibertyAst::whitelist
.insert("/library/cell/pin_opposite");
553 LibertyAst::whitelist
.insert("/library/cell/pin/state_function");
554 LibertyAst::whitelist
.insert("/library/cell/pin/three_state");
555 LibertyAst::whitelist
.insert("/library/cell/statetable");
556 LibertyAst::whitelist
.insert("/library/cell/statetable/*");
560 FILE *f
= fopen(argv
[1], "r");
562 fprintf(stderr
, "Can't open rules file `%s'.\n", argv
[1]);
567 while (fgets(buffer
, 1024, f
) != NULL
)
571 for (char *p
= buffer
; *p
; p
++)
573 if (*p
== '-' || *p
== '+') {
579 if (*p
== ' ' || *p
== '\t' || *p
== '\r' || *p
== '\n' || *p
== '#') {
582 LibertyAst::blacklist
.insert(id
);
585 LibertyAst::whitelist
.insert(id
);
598 fprintf(stderr
, "Syntax error in rules file:\n%s", buffer
);
607 f
= fopen(argv
[2], "r");
609 fprintf(stderr
, "Can't open liberty file `%s'.\n", argv
[2]);
614 LibertyParer
parser(f
);
617 gen_verilogsim(parser
.ast
);
619 parser
.ast
->dump(stdout
);