Skip to content

Commit 8796177

Browse files
committed
Squashed 'libs/EXTERNAL/libblifparse/' content from commit c4ba9de
git-subtree-dir: libs/EXTERNAL/libblifparse git-subtree-split: c4ba9de9bff58e2bb7bb0bec012c7cb211212907
0 parents  commit 8796177

22 files changed

+1304
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
build
2+
blifparse_test
3+
libblifparse.a
4+
obj/
5+
src/blif_lexer.gen.c
6+
src/blif_lexer.gen.h
7+
src/blif_parser.gen.c
8+
src/blif_parser.gen.h
9+
src/stack.hh

CMakeLists.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
cmake_minimum_required(VERSION 2.8.12)
2+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
3+
include(HeadersToIncludeDirs)
4+
5+
project("libblifparse")
6+
7+
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
8+
#Only set compiler settings if we are not a sub-project
9+
set(WARN_FLAGS "-Wall -Wextra -Wpedantic -Wcast-qual -Wcast-align -Wshadow -Wformat=2 -Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wredundant-decls -Wswitch-default -Wundef -Wunused-variable -Wdisabled-optimization -Wnoexcept -Woverloaded-virtual -Wctor-dtor-privacy -Wnon-virtual-dtor")
10+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11 ${WARN_FLAGS}")
11+
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize=leak -fsanitize=undefined")
12+
set(FLEX_BISON_WARN_SUPPRESS_FLAGS "-Wno-switch-default -Wno-unused-parameter -Wno-missing-declarations")
13+
endif()
14+
15+
#Flex and Bison are used to generate the parser
16+
find_package(BISON REQUIRED 3.0)
17+
find_package(FLEX REQUIRED)
18+
19+
file(GLOB_RECURSE LIB_SOURCES src/blif*.cpp)
20+
file(GLOB_RECURSE LIB_HEADERS src/blif*.hpp)
21+
headers_to_include_dirs(LIB_HEADERS LIB_INCLUDE_DIRS)
22+
23+
#Find the flex and bison input files
24+
file(GLOB_RECURSE LEXER_SOURCES src/blif*.l)
25+
file(GLOB_RECURSE PARSER_SOURCES src/blif*.y)
26+
27+
#Make the flex and bison targets
28+
flex_target(BlifLexer ${LEXER_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/blif_lexer.gen.cpp
29+
COMPILE_FLAGS --header-file=${CMAKE_CURRENT_BINARY_DIR}/blif_lexer.gen.hpp)
30+
bison_target(BlifParser ${PARSER_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/blif_parser.gen.cpp)
31+
add_flex_bison_dependency(BlifLexer BlifParser)
32+
33+
#Suppress warnings in Flex/Bison generated files
34+
if(FLEX_BISON_WARN_SUPPRESS_FLAGS)
35+
set_source_files_properties(${FLEX_BlifLexer_OUTPUTS} ${BISON_BlifParser_OUTPUT_SOURCE}
36+
PROPERTIES COMPILE_FLAGS ${FLEX_BISON_WARN_SUPPRESS_FLAGS})
37+
endif()
38+
39+
#Create the library
40+
add_library(libblifparse STATIC
41+
${LIB_HEADERS}
42+
${LIB_SOURCES}
43+
${FLEX_BlifLexer_OUTPUTS}
44+
${BISON_BlifParser_OUTPUT_SOURCE})
45+
target_include_directories(libblifparse PUBLIC ${LIB_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
46+
set_target_properties(libblifparse PROPERTIES PREFIX "") #Avoid extra 'lib' prefix
47+
48+
#Create the test executable
49+
add_executable(blifparse_test src/main.cpp)
50+
target_link_libraries(blifparse_test libblifparse)
51+
52+
install(TARGETS libblifparse blifparse_test DESTINATION bin)

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Kevin E. Murray
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#Tools
2+
CXX = g++
3+
AR = ar
4+
LEXER_GEN = flex
5+
PARSER_GEN = bison
6+
7+
#Whether this is a debug (symbols, no opt) or
8+
# release (no symbols, opt) build. May be
9+
# inherited from build environment. Can
10+
# override by defining below.
11+
#
12+
# Can be 'debug' or 'release'
13+
#BUILD_TYPE = release
14+
15+
# How verbose we want the make file
16+
# 0: print nothing
17+
# 1: print high-level message
18+
# 2: print high-level message + detailed command
19+
VERBOSITY = 1
20+
21+
22+
#Verbosity Configuration
23+
vecho_0 = true
24+
vecho_1 = echo
25+
vecho_2 = echo
26+
vecho = $(vecho_$(VERBOSITY))
27+
28+
AT_0 := @
29+
AT_1 := @
30+
AT_2 :=
31+
AT = $(AT_$(VERBOSITY))
32+
33+
#Final output files
34+
EXE=blifparse_test
35+
STATIC_LIB=libblifparse.a
36+
37+
#Flags
38+
WARN_FLAGS = -Wall -Wpointer-arith -Wcast-qual -D__USE_FIXED_PROTOTYPES__ -ansi -pedantic -Wshadow -Wcast-align -D_POSIX_SOURCE -Wno-write-strings
39+
40+
DEP_FLAGS = -MMD -MP
41+
42+
DEBUG_FLAGS = -g -ggdb3 -g3 -O0 -fno-inline
43+
44+
OPT_FLAGS = -O3
45+
46+
ifneq (,$(findstring debug, $(BUILD_TYPE)))
47+
DEBUG_OPT_FLAGS := $(DEBUG_FLAGS)
48+
else
49+
DEBUG_OPT_FLAGS := $(OPT_FLAGS)
50+
endif
51+
52+
INC_FLAGS = -I$(SRC_DIR) -I$(OBJ_DIR)
53+
54+
CFLAGS = $(DEP_FLAGS) $(WARN_FLAGS) $(DEBUG_OPT_FLAGS) $(INC_FLAGS) -std=c++11
55+
56+
#Objects
57+
#.SUFFIXES: .o .c .y .l
58+
SRC_DIR = src
59+
OBJ_DIR = obj
60+
61+
main_src = $(SRC_DIR)/main.cpp
62+
main_obj := $(patsubst %.cpp, %.o, $(main_src))
63+
main_obj := $(foreach obj, $(main_obj), $(patsubst $(SRC_DIR)/%.o, $(OBJ_DIR)/%.o, $(obj)))
64+
65+
lib_src = $(wildcard $(SRC_DIR)/blif*.cpp)
66+
lib_obj := $(patsubst %.cpp, %.o, $(lib_src))
67+
lib_obj := $(foreach obj, $(lib_obj), $(patsubst $(SRC_DIR)/%.o, $(OBJ_DIR)/%.o, $(obj)))
68+
69+
LEXER_SRC = $(SRC_DIR)/blif_lexer.l
70+
LEXER_GEN_SRC = $(SRC_DIR)/blif_lexer.gen.c
71+
LEXER_GEN_HDR = $(SRC_DIR)/blif_lexer.gen.h
72+
LEXER_GEN_OBJ := $(LEXER_GEN_SRC:.c=.o)
73+
LEXER_GEN_OBJ := $(foreach obj, $(LEXER_GEN_OBJ), $(patsubst $(SRC_DIR)/%.o, $(OBJ_DIR)/%.o, $(obj)))
74+
75+
PARSER_SRC = $(SRC_DIR)/blif_parser.y
76+
PARSER_GEN_SRC = $(SRC_DIR)/blif_parser.gen.c
77+
PARSER_GEN_HDR = $(SRC_DIR)/blif_parser.gen.h
78+
PARSER_GEN_OBJ := $(PARSER_GEN_SRC:.c=.o)
79+
PARSER_GEN_OBJ := $(foreach obj, $(PARSER_GEN_OBJ), $(patsubst $(SRC_DIR)/%.o, $(OBJ_DIR)/%.o, $(obj)))
80+
81+
OBJECTS_LIB = $(lib_obj) $(LEXER_GEN_OBJ) $(PARSER_GEN_OBJ)
82+
OBJECTS_EXE = $(main_obj) $(OBJECTS_LIB)
83+
84+
#Dependancies
85+
DEP = $(OBJECTS_EXE:.o=.d)
86+
87+
#Rules
88+
.PHONY: clean
89+
90+
all: $(OBJ_DIR) $(STATIC_LIB) $(EXE)
91+
92+
-include $(DEP)
93+
94+
$(EXE): $(OBJECTS_EXE)
95+
@$(vecho) "Linking executable: $@"
96+
$(AT) $(CXX) $(CFLAGS) -o $@ $(OBJECTS_EXE)
97+
98+
$(LEXER_GEN_SRC) $(LEXER_GEN_HDR): $(LEXER_SRC)
99+
@$(vecho) "Generating Lexer $(LEXER_SRC)..."
100+
@#Avoid including "unistd.h" since it is unavailable on windows
101+
$(AT) $(LEXER_GEN) --nounistd --header-file=$(LEXER_GEN_HDR) -o $(LEXER_GEN_SRC) $(LEXER_SRC)
102+
103+
$(PARSER_GEN_SRC) $(PARSER_GEN_HDR): $(PARSER_SRC)
104+
@$(vecho) "Generating Parser $(PARSER_SRC)..."
105+
$(AT) $(PARSER_GEN) -d -o $(PARSER_GEN_SRC) $(PARSER_SRC)
106+
107+
$(STATIC_LIB): $(OBJECTS_LIB)
108+
@$(vecho) "Linking static library: $@"
109+
$(AT) $(AR) rcs $@ $(OBJECTS_LIB)
110+
111+
$(LEXER_GEN_OBJ): $(LEXER_GEN_SRC) $(PARSER_GEN_SRC)
112+
@$(vecho) "Compiling Lexer $<..."
113+
@#Suppress warning about unused function produced by flex
114+
$(AT) $(CXX) $(CFLAGS) -Wno-unused-function -c $< -o $@
115+
116+
$(PARSER_GEN_OBJ): $(PARSER_GEN_SRC)
117+
@$(vecho) "Compiling Parser $<..."
118+
$(AT) $(CXX) $(CFLAGS) -c $< -o $@
119+
120+
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(LEXER_GEN_HDR) $(PARSER_GEN_HDR) $(OBJ_DIR)
121+
@$(vecho) "Compiling Source $<..."
122+
$(AT) $(CXX) $(CFLAGS) -c $< -o $@
123+
124+
$(OBJ_DIR):
125+
@ mkdir -p $@
126+
127+
clean:
128+
rm -f $(OBJECTS_EXE)
129+
rm -f $(DEP)
130+
rm -rf $(OBJ_DIR)
131+
rm -f $(EXE)
132+
rm -f $(STATIC_LIB)
133+
rm -f $(LEXER_GEN_SRC) $(LEXER_GEN_HDR)
134+
rm -f $(PARSER_GEN_SRC) $(PARSER_GEN_HDR) $(SRC_DIR)/stack.hh

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
libblifparse
2+
----------------------------------
3+
4+
This library provides a parser for a Berkely Logic Interchange Format (BLIF) files.
5+
See comments at the top of 'src/blifparse.hpp' for more detailed information and usage.
6+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function(headers_to_include_dirs header_file_list include_directory_list)
2+
#Extract the directories for each header file
3+
foreach(header ${${header_file_list}})
4+
get_filename_component(incl_dir ${header} DIRECTORY)
5+
list(APPEND dir_list ${incl_dir})
6+
endforeach()
7+
8+
#Remove any duplicates
9+
list(LENGTH "${dir_list}" length)
10+
if(${length} GREATER 1) #Avoid error with zero-length lists
11+
list(REMOVE_DUPLICATES ${dir_list})
12+
endif()
13+
14+
#Set the second argument in the caller's scope
15+
set(${include_directory_list} ${dir_list} PARENT_SCOPE)
16+
endfunction(headers_to_include_dirs)

doc/blif.pdf

53 KB
Binary file not shown.

src/blif_common.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <cstdlib>
2+
#include <cstring>
3+
4+
#include "blif_common.hpp"
5+
6+
namespace blifparse {
7+
8+
} //namespace

src/blif_common.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef BLIF_COMMON_HPP
2+
#define BLIF_COMMON_HPP
3+
4+
#include "blifparse.hpp"
5+
6+
namespace blifparse {
7+
8+
/*
9+
* Function Declarations
10+
*/
11+
12+
struct Names {
13+
std::vector<std::string> nets;
14+
std::vector<std::vector<LogicValue>> so_cover;
15+
};
16+
17+
struct SubCkt {
18+
std::string model;
19+
std::vector<std::string> ports;
20+
std::vector<std::string> nets;
21+
};
22+
23+
} //namespace
24+
#endif

src/blif_error.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <cstdarg>
2+
#include <cassert>
3+
#include "blif_error.hpp"
4+
#include "blifparse.hpp"
5+
6+
namespace blifparse {
7+
8+
std::string escape_string(const std::string& near_text);
9+
10+
//We wrap the actual blif_error to issolate custom handlers from vaargs
11+
void blif_error_wrap(Callback& callback, const int line_no, const std::string& near_text, const char* fmt, ...) {
12+
va_list args;
13+
va_start(args, fmt);
14+
15+
//We need to copy the args so we don't change them before the true formating
16+
va_list args_copy;
17+
va_copy(args_copy, args);
18+
19+
//Determine the formatted length using a copy of the args
20+
int len = std::vsnprintf(nullptr, 0, fmt, args_copy);
21+
22+
va_end(args_copy); //Clean-up
23+
24+
//Negative if there is a problem with the format string
25+
assert(len >= 0 && "Problem decoding format string");
26+
27+
size_t buf_size = len + 1; //For terminator
28+
29+
//Allocate a buffer
30+
// unique_ptr will free buffer automatically
31+
std::unique_ptr<char[]> buf(new char[buf_size]);
32+
33+
//Format into the buffer using the original args
34+
len = std::vsnprintf(buf.get(), buf_size, fmt, args);
35+
36+
va_end(args); //Clean-up
37+
38+
assert(len >= 0 && "Problem decoding format string");
39+
assert(static_cast<size_t>(len) == buf_size - 1);
40+
41+
//Build the string from the buffer
42+
std::string msg(buf.get(), len);
43+
44+
//TODO: escape near_text
45+
std::string escaped_near_text = escape_string(near_text);
46+
47+
//Call the error handler
48+
callback.parse_error(line_no, escaped_near_text, msg);
49+
}
50+
51+
std::string escape_string(const std::string& near_text) {
52+
std::string escaped_text;
53+
54+
for(char c : near_text) {
55+
56+
if(c == '\n') {
57+
escaped_text += "\\n";
58+
} else if(c == '\r') {
59+
escaped_text += "\\r";
60+
} else {
61+
escaped_text += c;
62+
}
63+
}
64+
65+
return escaped_text;
66+
}
67+
68+
69+
}

src/blif_error.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef BLIF_ERROR_H
2+
#define BLIF_ERROR_H
3+
#include <functional>
4+
#include "blifparse.hpp"
5+
namespace blifparse {
6+
7+
void blif_error_wrap(Callback& callback, const int line_no, const std::string& near_text, const char* fmt, ...);
8+
}
9+
#endif

src/blif_lexer.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "blif_lexer.hpp"
2+
3+
//Windows doesn't have unistd.h, so we set '%option nounistd'
4+
//in blif_lexer.l, but flex still includes it in the generated
5+
//header unless YY_NO_UNISTD_H is defined to 1
6+
#define YY_NO_UNISTD_H 1
7+
#include "blif_lexer.gen.hpp" //For blifparse_lex_*()
8+
9+
extern YY_DECL; //For blifparse_lex()
10+
11+
namespace blifparse {
12+
13+
Lexer::Lexer(FILE* file, Callback& callback)
14+
: callback_(callback) {
15+
blifparse_lex_init(&state_);
16+
blifparse_set_in(file, state_);
17+
}
18+
19+
Lexer::~Lexer() {
20+
blifparse_lex_destroy(state_);
21+
}
22+
23+
Parser::symbol_type Lexer::next_token() {
24+
return blifparse_lex(state_, callback_);
25+
}
26+
27+
const char* Lexer::text() const {
28+
return blifparse_get_text(state_);
29+
}
30+
31+
int Lexer::lineno() const {
32+
return blifparse_get_lineno(state_);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)