mirror of
				https://github.com/YosysHQ/yosys
				synced 2025-10-30 19:22:31 +00:00 
			
		
		
		
	Allow for gzip magic in stat
This commit is contained in:
		
							parent
							
								
									2a8575abb2
								
							
						
					
					
						commit
						261bc561fa
					
				
					 1 changed files with 101 additions and 6 deletions
				
			
		|  | @ -25,6 +25,65 @@ | ||||||
| #include "kernel/cost.h" | #include "kernel/cost.h" | ||||||
| #include "libs/json11/json11.hpp" | #include "libs/json11/json11.hpp" | ||||||
| 
 | 
 | ||||||
|  | #ifdef YOSYS_ENABLE_ZLIB | ||||||
|  | #include <zlib.h> | ||||||
|  | 
 | ||||||
|  | PRIVATE_NAMESPACE_BEGIN | ||||||
|  | #define GZ_BUFFER_SIZE 8192 | ||||||
|  | void decompress_gzip(const std::string &filename, std::stringstream &out) | ||||||
|  | { | ||||||
|  | 	char buffer[GZ_BUFFER_SIZE]; | ||||||
|  | 	int bytes_read; | ||||||
|  | 	gzFile gzf = gzopen(filename.c_str(), "rb"); | ||||||
|  | 	while(!gzeof(gzf)) { | ||||||
|  | 		bytes_read = gzread(gzf, reinterpret_cast<void *>(buffer), GZ_BUFFER_SIZE); | ||||||
|  | 		out.write(buffer, bytes_read); | ||||||
|  | 	} | ||||||
|  | 	gzclose(gzf); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /*
 | ||||||
|  | An output stream that uses a stringbuf to buffer data internally, | ||||||
|  | using zlib to write gzip-compressed data every time the stream is flushed. | ||||||
|  | */ | ||||||
|  | class gzip_ostream : public std::ostream  { | ||||||
|  | public: | ||||||
|  | 	gzip_ostream() : std::ostream(nullptr) | ||||||
|  | 	{ | ||||||
|  | 		rdbuf(&outbuf); | ||||||
|  | 	} | ||||||
|  | 	bool open(const std::string &filename) | ||||||
|  | 	{ | ||||||
|  | 		return outbuf.open(filename); | ||||||
|  | 	} | ||||||
|  | private: | ||||||
|  | 	class gzip_streambuf : public std::stringbuf { | ||||||
|  | 	public: | ||||||
|  | 		gzip_streambuf() { }; | ||||||
|  | 		bool open(const std::string &filename) | ||||||
|  | 		{ | ||||||
|  | 			gzf = gzopen(filename.c_str(), "wb"); | ||||||
|  | 			return gzf != nullptr; | ||||||
|  | 		} | ||||||
|  | 		virtual int sync() override | ||||||
|  | 		{ | ||||||
|  | 			gzwrite(gzf, reinterpret_cast<const void *>(str().c_str()), unsigned(str().size())); | ||||||
|  | 			str(""); | ||||||
|  | 			return 0; | ||||||
|  | 		} | ||||||
|  | 		virtual ~gzip_streambuf() | ||||||
|  | 		{ | ||||||
|  | 			sync(); | ||||||
|  | 			gzclose(gzf); | ||||||
|  | 		} | ||||||
|  | 	private: | ||||||
|  | 		gzFile gzf = nullptr; | ||||||
|  | 	} outbuf; | ||||||
|  | }; | ||||||
|  | PRIVATE_NAMESPACE_END | ||||||
|  | 
 | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
| USING_YOSYS_NAMESPACE | USING_YOSYS_NAMESPACE | ||||||
| PRIVATE_NAMESPACE_BEGIN | PRIVATE_NAMESPACE_BEGIN | ||||||
| 
 | 
 | ||||||
|  | @ -350,13 +409,49 @@ statdata_t hierarchy_worker(std::map<RTLIL::IdString, statdata_t> &mod_stat, RTL | ||||||
| 
 | 
 | ||||||
| void read_liberty_cellarea(dict<IdString, cell_area_t> &cell_area, string liberty_file) | void read_liberty_cellarea(dict<IdString, cell_area_t> &cell_area, string liberty_file) | ||||||
| { | { | ||||||
| 	std::ifstream f; | 	std::istream *f; | ||||||
| 	f.open(liberty_file.c_str()); | 	std::ifstream *ff = new std::ifstream; | ||||||
|  | 	ff->open(liberty_file.c_str(), (liberty_file.substr(liberty_file.size() - 3) == ".gz") ? std::ifstream::binary : std::ifstream::in); | ||||||
| 	yosys_input_files.insert(liberty_file); | 	yosys_input_files.insert(liberty_file); | ||||||
| 	if (f.fail()) | 	if (ff->fail()) { | ||||||
| 		log_cmd_error("Can't open liberty file `%s': %s\n", liberty_file.c_str(), strerror(errno)); | 		delete ff; | ||||||
| 	LibertyParser libparser(f); | 		ff = nullptr; | ||||||
| 	f.close(); | 	} | ||||||
|  | 	f = ff; | ||||||
|  | 	if (f != NULL) { | ||||||
|  | 		// Check for gzip magic
 | ||||||
|  | 		unsigned char magic[3]; | ||||||
|  | 		int n = 0; | ||||||
|  | 		while (n < 3) | ||||||
|  | 		{ | ||||||
|  | 			int c = ff->get(); | ||||||
|  | 			if (c != EOF) { | ||||||
|  | 				magic[n] = (unsigned char) c; | ||||||
|  | 			} | ||||||
|  | 			n++; | ||||||
|  | 		} | ||||||
|  | 		if (n == 3 && magic[0] == 0x1f && magic[1] == 0x8b) { | ||||||
|  | #ifdef YOSYS_ENABLE_ZLIB | ||||||
|  | 			// log("Found gzip magic in file `%s', decompressing using zlib.\n", liberty_file.c_str());
 | ||||||
|  | 			if (magic[2] != 8) | ||||||
|  | 				log_cmd_error("gzip file `%s' uses unsupported compression type %02x\n", | ||||||
|  | 					liberty_file.c_str(), unsigned(magic[2])); | ||||||
|  | 			delete ff; | ||||||
|  | 			std::stringstream *df = new std::stringstream(); | ||||||
|  | 			decompress_gzip(liberty_file, *df); | ||||||
|  | 			f = df; | ||||||
|  | #else | ||||||
|  | 			log_cmd_error("File `%s' is a gzip file, but Yosys is compiled without zlib.\n", liberty_file.c_str()); | ||||||
|  | #endif | ||||||
|  | 		} else { | ||||||
|  | 			ff->clear(); | ||||||
|  | 			ff->seekg(0, std::ios::beg); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	if (f == NULL) | ||||||
|  | 		log_cmd_error("Can't open input file `%s' for reading: %s\n", liberty_file.c_str(), strerror(errno)); | ||||||
|  | 
 | ||||||
|  | 	LibertyParser libparser(*f); | ||||||
| 
 | 
 | ||||||
| 	for (auto cell : libparser.ast->children) | 	for (auto cell : libparser.ast->children) | ||||||
| 	{ | 	{ | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue