Skip to content

Commit 7ddad22

Browse files
authored
Merge pull request #703 from os-fpga/pln_checker_support_escaping_NL
pln blif-checker: support escaping NL (line continuation)
2 parents d4d2526 + ccf1cba commit 7ddad22

File tree

4 files changed

+126
-8
lines changed

4 files changed

+126
-8
lines changed

stars/src/file_readers/pinc_Fio.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,108 @@ bool MMapReader::makeLines(bool cutComments, bool cutNL) noexcept {
867867
return true;
868868
}
869869

870+
vector<char*> MMapReader::get_backslashed_block(size_t fromLine) noexcept {
871+
if (!sz_ || !fsz_) return {};
872+
if (!buf_) return {};
873+
if (!hasLines()) return {};
874+
875+
size_t numLines = lines_.size();
876+
if (numLines <= 3)
877+
return {};
878+
if (fromLine >= numLines - 3)
879+
return {};
880+
881+
vector<char*> V;
882+
size_t li = 0;
883+
884+
for (li = fromLine; li < numLines - 3; li++) {
885+
char* line = lines_[li];
886+
if (!line)
887+
break;
888+
size_t len = ::strlen(line);
889+
if (len <= 2)
890+
break;
891+
if (line[len - 1] != '\\')
892+
break;
893+
894+
line[len - 1] = ' '; // erase backslash
895+
V.push_back(line);
896+
}
897+
898+
// add non-backslashed line at the end of block:
899+
if (li < numLines - 1) {
900+
char* line = lines_[li];
901+
if (line and line[0])
902+
V.push_back(line);
903+
}
904+
905+
return V;
906+
}
907+
908+
// '\' screens new-line '\n' like in tcl and cpp.
909+
// returns number of processed '\' symbols.
910+
size_t MMapReader::escapeNL() noexcept {
911+
if (!sz_ || !fsz_) return 0;
912+
if (!buf_) return 0;
913+
if (!hasLines()) return 0;
914+
915+
size_t numLines = lines_.size();
916+
if (numLines <= 3)
917+
return 0;
918+
919+
size_t cnt = 0;
920+
vector<char*> bs_block;
921+
922+
for (size_t li = 1; li < numLines - 1; li++) {
923+
char* line = lines_[li];
924+
if (!line)
925+
continue;
926+
size_t len = ::strlen(line);
927+
if (len <= 2)
928+
continue;
929+
if (line[len - 1] != '\\')
930+
continue;
931+
line[len - 1] = ' '; // erase backslash
932+
933+
char* nextLine = lines_[li + 1];
934+
if (!nextLine)
935+
continue;
936+
size_t nextLen = ::strlen(nextLine);
937+
if (nextLen <= 1)
938+
continue;
939+
940+
bs_block = get_backslashed_block(li + 1);
941+
if (bs_block.empty())
942+
continue;
943+
944+
// lprintf("\t ............. bs_block.size()= %zu\n", bs_block.size());
945+
946+
// replace 'line' by allocated and contatenated string:
947+
size_t newSize = len;
948+
for (char* s : bs_block) {
949+
assert(s);
950+
newSize += ::strlen(s);
951+
}
952+
char* replacement = (char*) ::calloc(newSize + 2, 1);
953+
954+
void* next = ::mempcpy(replacement, line, len);
955+
for (char* s : bs_block) {
956+
size_t slen = ::strlen(s);
957+
next = ::mempcpy(next, s, slen);
958+
cnt++;
959+
}
960+
961+
// blank bs_block lines:
962+
for (char* s : bs_block) {
963+
s[0] = 0;
964+
}
965+
966+
lines_[li] = replacement;
967+
}
968+
969+
return cnt;
970+
}
971+
870972
char* MMapReader::skipLine(char* curL) noexcept {
871973
assert(sz_);
872974
assert(buf_);

stars/src/file_readers/pinc_Fio.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ class MMapReader : public Fio
232232

233233
virtual bool makeLines(bool cutComments, bool cutNL) noexcept override;
234234

235+
size_t escapeNL() noexcept; // by '\'
236+
235237
int64_t countLines() const noexcept;
236238

237239
size_t setNumLines() noexcept {
@@ -261,6 +263,8 @@ class MMapReader : public Fio
261263
private:
262264
bool is_text_file(string& label) const noexcept;
263265

266+
vector<char*> get_backslashed_block(size_t fromLine) noexcept;
267+
264268
}; // MMapReader
265269

266270

stars/src/file_readers/pln_blif_file.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ bool BLIF_file::readBlif() noexcept {
146146
return false;
147147
}
148148

149-
ok = ::memmem(buf_, sz_, ".end", 4);
150-
if (!ok) {
151-
err_msg_ = ".end not found";
152-
return false;
153-
}
149+
//ok = ::memmem(buf_, sz_, ".end", 4);
150+
//if (!ok) {
151+
// err_msg_ = ".end not found";
152+
// return false;
153+
//}
154154

155155
ok = MMapReader::makeLines(true, true);
156156
if (!ok) {
@@ -164,6 +164,11 @@ bool BLIF_file::readBlif() noexcept {
164164
return false;
165165
}
166166

167+
size_t num_escaped = escapeNL();
168+
if (trace_ >= 4) {
169+
lprintf(" ....\\ ... num_escaped= %zu\n", num_escaped);
170+
}
171+
167172
inputs_lnum_ = outputs_lnum_ = 0;
168173
err_lnum_ = 0;
169174
for (size_t i = 1; i < lsz; i++) {
@@ -189,7 +194,7 @@ bool BLIF_file::readBlif() noexcept {
189194
return false;
190195
}
191196

192-
if (trace() >= 3) {
197+
if (trace_ >= 3) {
193198
lprintf("\t .... inputs_lnum_= %zu outputs_lnum_= %zu\n", inputs_lnum_, outputs_lnum_);
194199
}
195200

@@ -636,7 +641,14 @@ bool BLIF_file::linkNodes() noexcept {
636641
for (Node* fab_nd : fabricNodes_) {
637642
assert(fab_nd);
638643
Node& nd = *fab_nd;
639-
assert(!nd.out_.empty());
644+
if (nd.out_.empty()) {
645+
err_msg_ = str::concat("incomplete fabric cell: ", nd.kw_);
646+
if (!nd.data_.empty()) {
647+
err_msg_ += str::concat(" ", nd.data_.front());
648+
}
649+
err_lnum_ = nd.lnum_;
650+
return false;
651+
}
640652
Node* port = findOutputPort(nd.out_);
641653
if (port) {
642654
link(*port, nd);

stars/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
static const char* _pln_VERSION_STR = "pln0218";
1+
static const char* _pln_VERSION_STR = "pln0221";
22

33
#include "RS/rsEnv.h"
44
#include "util/pln_log.h"

0 commit comments

Comments
 (0)