Skip to content

Commit d82b16c

Browse files
committed
[Odin]: adding getbline to read BLIFs according to line styles (getline or fgets with '\')
Signed-off-by: Seyed Alireza Damghani <[email protected]>
1 parent 041a6fd commit d82b16c

File tree

2 files changed

+78
-18
lines changed

2 files changed

+78
-18
lines changed

ODIN_II/SRC/BLIFReader.cc

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ void* BLIF::Reader::__read() {
104104
// A cache of hard block models indexed by name. As each one is read, it's stored here to be used again.
105105
hard_block_models* models = create_hard_block_models();
106106
printf("\n");
107-
char buffer[READ_BLIF_BUFFER];
108-
while (vtr::fgets(buffer, READ_BLIF_BUFFER, file) && read_tokens(buffer, models)) { // Print a progress bar indicating completeness.
107+
char* buffer = NULL;
108+
while (getbline(buffer, READ_BLIF_BUFFER, file) && read_tokens(buffer, models)) { // Print a progress bar indicating completeness.
109109
position = print_progress_bar((++line_count) / (double)num_lines, position, 50, wall_time() - time);
110110
}
111111
free_hard_block_models(models);
@@ -121,9 +121,12 @@ void* BLIF::Reader::__read() {
121121
// delete output_nets_hash;
122122
fclose(file);
123123

124-
printf("Elaborating the netlist created from the input BLIF file to make it compatible with ODIN_II partial mapping\n");
124+
printf("Elaborating the netlist created from the input BLIF file\n");
125125
blif_elaborate_top(blif_netlist);
126126

127+
/* clean up */
128+
vtr::free(buffer);
129+
127130
return static_cast<void*>(blif_netlist);
128131
}
129132

@@ -187,8 +190,8 @@ void BLIF::Reader::find_top_module() {
187190

188191
bool found = false;
189192
while (!found) {
190-
char buffer[READ_BLIF_BUFFER];
191-
vtr::fgets(buffer, READ_BLIF_BUFFER, file);
193+
char* buffer = NULL;
194+
getbline(buffer, READ_BLIF_BUFFER, file);
192195
my_location.line += 1;
193196

194197
// not sure if this is needed
@@ -197,9 +200,12 @@ void BLIF::Reader::find_top_module() {
197200

198201
char* token = vtr::strtok(buffer, TOKENS, file, buffer);
199202
if (token && !strcmp(token, ".model")) {
200-
top_module = vtr::strtok(NULL, TOKENS, file, buffer);
203+
top_module = vtr::strdup(vtr::strtok(NULL, TOKENS, file, buffer));
201204
found = true;
202205
}
206+
207+
/* clean up */
208+
vtr::free(buffer);
203209
}
204210

205211
if (!found) {
@@ -762,8 +768,8 @@ hard_block_model* BLIF::Reader::read_hard_block_model(char* name_subckt, operati
762768
model = NULL;
763769

764770
// Search the file for .model followed buy the subcircuit name.
765-
char buffer[READ_BLIF_BUFFER];
766-
while (vtr::fgets(buffer, READ_BLIF_BUFFER, file)) {
771+
char* buffer = NULL;
772+
while (getbline(buffer, READ_BLIF_BUFFER, file)) {
767773
my_location.line += 1;
768774
char* token = vtr::strtok(buffer, TOKENS, file, buffer);
769775
// match .model followed by the subcircuit name.
@@ -779,7 +785,7 @@ hard_block_model* BLIF::Reader::read_hard_block_model(char* name_subckt, operati
779785
model->outputs->names = NULL;
780786

781787
// Read the inputs and outputs.
782-
while (vtr::fgets(buffer, READ_BLIF_BUFFER, file)) {
788+
while (getbline(buffer, READ_BLIF_BUFFER, file)) {
783789
char* first_word = vtr::strtok(buffer, TOKENS, file, buffer);
784790
if (first_word) {
785791
if (!strcmp(first_word, ".inputs")) {
@@ -803,6 +809,9 @@ hard_block_model* BLIF::Reader::read_hard_block_model(char* name_subckt, operati
803809
}
804810
}
805811

812+
/* clean up */
813+
vtr::free(buffer);
814+
806815
if (!model || feof(file)) {
807816
if (configuration.coarsen)
808817
model = create_hard_block_model(name_subckt, type, ports);
@@ -991,10 +1000,10 @@ operation_list BLIF::Reader::read_bit_map_find_unknown_gate(int input_count, nno
9911000
char** bit_map = NULL;
9921001
char* output_bit_map = NULL; // to distinguish whether for the bit_map output is 1 or 0
9931002
int line_count_bitmap = 0; //stores the number of lines in a particular bit map
994-
char buffer[READ_BLIF_BUFFER];
1003+
char* buffer = NULL;
9951004

9961005
if (!input_count) {
997-
vtr::fgets(buffer, READ_BLIF_BUFFER, file);
1006+
getbline(buffer, READ_BLIF_BUFFER, file);
9981007
my_location.line += 1;
9991008

10001009
char* ptr = vtr::strtok(buffer, "\t\n", file, buffer);
@@ -1009,7 +1018,7 @@ operation_list BLIF::Reader::read_bit_map_find_unknown_gate(int input_count, nno
10091018
}
10101019
} else {
10111020
while (1) {
1012-
vtr::fgets(buffer, READ_BLIF_BUFFER, file);
1021+
getbline(buffer, READ_BLIF_BUFFER, file);
10131022
my_location.line += 1;
10141023

10151024
if (!(buffer[0] == '0' || buffer[0] == '1' || buffer[0] == '-'))
@@ -1209,6 +1218,9 @@ operation_list BLIF::Reader::read_bit_map_find_unknown_gate(int input_count, nno
12091218
to_return = GENERIC;
12101219
}
12111220
}
1221+
/* clean up */
1222+
vtr::free(buffer);
1223+
12121224
if (output_bit_map) {
12131225
vtr::free(output_bit_map);
12141226
}
@@ -1345,8 +1357,8 @@ char* BLIF::Reader::search_clock_name() {
13451357
int input_names_count = 0;
13461358
int found = 0;
13471359
while (!found) {
1348-
char buffer[READ_BLIF_BUFFER];
1349-
vtr::fgets(buffer, READ_BLIF_BUFFER, file);
1360+
char* buffer = NULL;
1361+
getbline(buffer, READ_BLIF_BUFFER, file);
13501362
my_location.line += 1;
13511363

13521364
// not sure if this is needed
@@ -1378,6 +1390,9 @@ char* BLIF::Reader::search_clock_name() {
13781390
found = 1;
13791391
}
13801392
}
1393+
1394+
/* clean up */
1395+
vtr::free(buffer);
13811396
}
13821397
my_location.line = last_line;
13831398
fsetpos(file, &pos);
@@ -1635,13 +1650,18 @@ hard_block_models* BLIF::Reader::create_hard_block_models() {
16351650
*/
16361651
int BLIF::Reader::count_blif_lines() {
16371652
int local_num_lines = 0;
1638-
char buffer[READ_BLIF_BUFFER];
1639-
while (vtr::fgets(buffer, READ_BLIF_BUFFER, file)) {
1653+
char* buffer = NULL;
1654+
while (getbline(buffer, READ_BLIF_BUFFER, file)) {
16401655
if (strstr(buffer, ".end"))
16411656
break;
16421657
local_num_lines++;
16431658
}
1659+
16441660
rewind(file);
1661+
1662+
/* clean up */
1663+
vtr::free(buffer);
1664+
16451665
return local_num_lines;
16461666
}
16471667

@@ -1969,11 +1989,11 @@ void BLIF::Reader::hard_block_sensitivities(const char* subckt_name, nnode_t* ne
19691989
fgetpos(file, &pos);
19701990

19711991
char* ptr;
1972-
char buffer[READ_BLIF_BUFFER];
1992+
char* buffer = NULL;
19731993
attr_t* attributes = new_node->attributes;
19741994

19751995
if (need_params(yosys_subckt_str[subckt_name])) {
1976-
while (vtr::fgets(buffer, READ_BLIF_BUFFER, file)) {
1996+
while (getbline(buffer, READ_BLIF_BUFFER, file)) {
19771997
my_location.line += 1;
19781998
ptr = vtr::strtok(buffer, TOKENS, file, buffer);
19791999

@@ -2101,6 +2121,9 @@ void BLIF::Reader::hard_block_sensitivities(const char* subckt_name, nnode_t* ne
21012121
}
21022122
}
21032123

2124+
/* clean up */
2125+
vtr::free(buffer);
2126+
21042127
// Restore the original position in the file.
21052128
my_location.line = last_line;
21062129
fsetpos(file, &pos);

ODIN_II/SRC/include/BLIF.hh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#include "odin_types.h"
3434
#include "Hashtable.hpp"
3535

36+
#include "vtr_util.h"
37+
#include "vtr_memory.h"
38+
3639
#define TOKENS " \t\n"
3740
#define YOSYS_TOKENS "[]"
3841
#define YOSYS_ID_FIRST_DELIMITER "\\\\"
@@ -88,6 +91,40 @@ extern bool insert_global_clock;
8891

8992
extern FILE* file;
9093

94+
/**
95+
*---------------------------------------------------------------------------------------------
96+
* (function: getbline)
97+
*
98+
* @brief reading blif file lines according to the blif file type.
99+
* For instance, in yosys blif we must read lines since there is
100+
* no '\' usage. For arbitrary long lines fgets does not work due
101+
* to size limit. However, the scenario in Odin is on the flipside
102+
* and we ought to use fgets to pass '\' and the line length are
103+
* always fix and less than a const value
104+
*
105+
* @param buf buffer pointer
106+
* @param size buffer size (in the case of using fgets)
107+
* @param fd file stream
108+
*
109+
* @return null pointer if end of the file, otherwise the read line
110+
*---------------------------------------------------------------------------------------------*/
111+
inline char* getbline(char* &buf, size_t size, FILE* fd) {
112+
char* retval = NULL;
113+
if (buf) {
114+
vtr::free(buf);
115+
buf = NULL;
116+
}
117+
/* decide whether need to read line or using fgets */
118+
if (configuration.coarsen) {
119+
retval = vtr::getline(buf, fd);
120+
} else {
121+
buf = (char*)vtr::malloc(READ_BLIF_BUFFER * sizeof(char));
122+
retval = vtr::fgets(buf, size, fd);
123+
}
124+
125+
return (retval);
126+
}
127+
91128
/**
92129
* @brief A class to provide the general object of an input BLIF file reader
93130
*/

0 commit comments

Comments
 (0)