From a202c6bf698fafe159e14f8b61664c1dff8f42e5 Mon Sep 17 00:00:00 2001 From: James Cherry Date: Wed, 10 Mar 2021 09:45:17 -0700 Subject: [PATCH] read_liberty gzip'd files --- liberty/LibertyLex.ll | 8 +++---- liberty/LibertyParser.cc | 47 ++++++++++++++++++++++++++++++---------- liberty/LibertyParser.hh | 17 ++++++++++++++- 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/liberty/LibertyLex.ll b/liberty/LibertyLex.ll index aa1b0c36..9d4a8d75 100644 --- a/liberty/LibertyLex.ll +++ b/liberty/LibertyLex.ll @@ -132,11 +132,9 @@ EOL \r?\n while (isspace(filename_end[-1]) && filename_end > filename) filename_end--; *filename_end = '\0'; - FILE *stream = sta::libertyIncludeBegin(filename); - if (stream) { - yypush_buffer_state(yy_create_buffer(stream, YY_BUF_SIZE)); - BEGIN(INITIAL); - } + sta::libertyIncludeBegin(filename); + yypush_buffer_state(yy_create_buffer(nullptr, YY_BUF_SIZE)); + BEGIN(INITIAL); } } #else diff --git a/liberty/LibertyParser.cc b/liberty/LibertyParser.cc index 715fcda8..3a2ba4a5 100644 --- a/liberty/LibertyParser.cc +++ b/liberty/LibertyParser.cc @@ -27,18 +27,18 @@ int LibertyParse_parse(); -extern FILE *LibertyLex_in; namespace sta { typedef Vector LibertyGroupSeq; static const char *liberty_filename; +static gzFile liberty_stream; static int liberty_line; // Previous lex reader state for include files. static const char *liberty_filename_prev; static int liberty_line_prev; -static FILE *liberty_stream_prev; +static gzFile liberty_stream_prev; static LibertyGroupVisitor *liberty_group_visitor; static LibertyGroupSeq liberty_group_stack; @@ -59,8 +59,8 @@ parseLibertyFile(const char *filename, LibertyGroupVisitor *library_visitor, Report *report) { - LibertyLex_in = fopen(filename, "r"); - if (LibertyLex_in) { + liberty_stream = gzopen(filename, "r"); + if (liberty_stream) { liberty_group_visitor = library_visitor; liberty_group_stack.clear(); liberty_filename = filename; @@ -69,12 +69,36 @@ parseLibertyFile(const char *filename, liberty_line = 1; liberty_report = report; LibertyParse_parse(); - fclose(LibertyLex_in); + gzclose(liberty_stream); } else throw FileNotReadable(filename); } +void +libertyGetChars(char *buf, + int &result, + size_t max_size) +{ + char *status = gzgets(liberty_stream, buf, max_size); + if (status == Z_NULL) + result = 0; // YY_nullptr + else + result = strlen(buf); +} + +void +libertyGetChars(char *buf, + size_t &result, + size_t max_size) +{ + char *status = gzgets(liberty_stream, buf, max_size); + if (status == Z_NULL) + result = 0; // YY_nullptr + else + result = strlen(buf); +} + void libertyGroupBegin(const char *type, LibertyAttrValueSeq *params, @@ -510,30 +534,31 @@ libertyInInclude() return liberty_filename_prev != nullptr; } -FILE * +void libertyIncludeBegin(const char *filename) { - FILE *stream = fopen(filename, "r" ); + gzFile stream = gzopen(filename, "r" ); if (stream) { + liberty_stream_prev = liberty_stream; liberty_filename_prev = liberty_filename; liberty_line_prev = liberty_line; - liberty_stream_prev = LibertyLex_in; + liberty_stream = stream; liberty_filename = filename; liberty_line = 1; } else libertyParseError("cannot open include file %s.", filename); - return stream; } void libertyIncludeEnd() { - fclose(LibertyLex_in); + gzclose(liberty_stream); + liberty_stream = liberty_stream_prev; liberty_filename = liberty_filename_prev; liberty_line = liberty_line_prev; - LibertyLex_in = liberty_stream_prev; + liberty_filename_prev = nullptr; liberty_stream_prev = nullptr; } diff --git a/liberty/LibertyParser.hh b/liberty/LibertyParser.hh index 6c959e17..98c64272 100644 --- a/liberty/LibertyParser.hh +++ b/liberty/LibertyParser.hh @@ -17,6 +17,7 @@ #pragma once #include "DisallowCopyAssign.hh" +#include "Zlib.hh" #include "Vector.hh" #include "Map.hh" #include "Set.hh" @@ -51,6 +52,20 @@ enum class LibertyAttrType { attr_string, attr_int, attr_double, enum class LibertyGroupType { library, cell, pin, timing, unknown }; +// flex YY_INPUT yy_n_chars arg changed definition from int to size_t, +// so provide both forms. +void +libertyGetChars(char *buf, + size_t &result, + size_t max_size); +void +libertyGetChars(char *buf, + int &result, + size_t max_size); + +#define YY_INPUT(buf,result,max_size) \ + sta::libertyGetChars(buf, result, max_size) + // Abstract base class for liberty statements. class LibertyStmt { @@ -300,7 +315,7 @@ private: DISALLOW_COPY_AND_ASSIGN(LibertyGroupVisitor); }; -FILE * +void libertyIncludeBegin(const char *filename); void libertyIncludeEnd();