Skip to content

Commit

Permalink
Implement directives
Browse files Browse the repository at this point in the history
  • Loading branch information
hs-apotell committed Mar 28, 2024
1 parent e09c1c8 commit 29d94d9
Show file tree
Hide file tree
Showing 29 changed files with 2,160 additions and 301 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,17 @@ add_custom_command(
${GENDIR}/include/Surelog/SourceCompile/SV3_1aTreeShapeListener.h
${GENDIR}/include/Surelog/SourceCompile/VObjectTypes.h
${GENDIR}/src/SourceCompile/ParseTreeListener.cpp
${GENDIR}/src/SourceCompile/SV3_1aParseTreeListener.cpp
${GENDIR}/src/SourceCompile/SV3_1aPpParseTreeListener.cpp
${GENDIR}/src/SourceCompile/VObjectTypes.cpp
DEPENDS ${GENDIR}/src/parser/generated-parsers.tstamp
${surelog_grammars-GENERATED_SRC}
${PROJECT_SOURCE_DIR}/include/Surelog/SourceCompile/ParseTreeListener.template.hpp
${PROJECT_SOURCE_DIR}/include/Surelog/SourceCompile/ParseTreeTraceListener.template.hpp
${PROJECT_SOURCE_DIR}/scripts/generate_parser_listener.py
${PROJECT_SOURCE_DIR}/src/SourceCompile/ParseTreeListener.template.cxx
${PROJECT_SOURCE_DIR}/src/SourceCompile/SV3_1aParseTreeListener.template.cxx
${PROJECT_SOURCE_DIR}/src/SourceCompile/SV3_1aPpParseTreeListener.template.cxx
${PROJECT_SOURCE_DIR}/src/SourceCompile/SV3_1aPpTreeShapeListener.cpp
${PROJECT_SOURCE_DIR}/src/SourceCompile/SV3_1aTreeShapeListener.cpp
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
Expand All @@ -367,6 +371,8 @@ add_custom_target(GenerateParserListeners DEPENDS
${GENDIR}/include/Surelog/SourceCompile/SV3_1aTreeShapeListener.h
${GENDIR}/include/Surelog/SourceCompile/VObjectTypes.h
${GENDIR}/src/SourceCompile/ParseTreeListener.cpp
${GENDIR}/src/SourceCompile/SV3_1aParseTreeListener.cpp
${GENDIR}/src/SourceCompile/SV3_1aPpParseTreeListener.cpp
${GENDIR}/src/SourceCompile/VObjectTypes.cpp)

if (SURELOG_WITH_PYTHON)
Expand Down Expand Up @@ -534,7 +540,9 @@ set(surelog_generated_SRC

# Derived from parser generation.
${GENDIR}/src/SourceCompile/VObjectTypes.cpp
${GENDIR}/src/SourceCompile/ParseTreeListener.cpp)
${GENDIR}/src/SourceCompile/ParseTreeListener.cpp
${GENDIR}/src/SourceCompile/SV3_1aParseTreeListener.cpp
${GENDIR}/src/SourceCompile/SV3_1aPpParseTreeListener.cpp)

foreach(gen_src ${surelog_generated_SRC})
set_source_files_properties(${gen_src} PROPERTIES GENERATED TRUE)
Expand Down
7 changes: 6 additions & 1 deletion grammar/SV3_1aLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ lexer grammar SV3_1aLexer;

channels {
WHITESPACES,
COMMENTS
COMMENTS,
PREPROC
}

PREPROC_BEGIN: '{!< ' Decimal_digit+ ' !}' -> channel(PREPROC);
PREPROC_END: '{! ' Decimal_digit+ ' >!}' -> channel(PREPROC);

QMARK: '?';

TICK_b0: '\'b0';
Expand Down Expand Up @@ -916,6 +920,7 @@ NETTYPE: 'nettype';
//Escaped_identifier : '^^^' [\\|+a-zA-Z0-9_$:,-/*{}()`~!=;'"<>?.]* '^^^' ;

Escaped_identifier: '#~@' .*? '#~@';
Preproc_identifier: '@~#' Decimal_digit+;

TILDA: '~';

Expand Down
15 changes: 10 additions & 5 deletions grammar/SV3_1aParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ include_statement: INCLUDE file_path_spec SEMICOLON;

source_text: timeunits_declaration? description*;

null_rule
:
; // Placeholder rule that create the "0" VObject, DO NOT REMOVE
// Placeholder rule that create the "0" VObject, DO NOT REMOVE
null_rule: ;

description
: module_declaration
Expand Down Expand Up @@ -308,7 +307,10 @@ parameter_port_declaration
| TYPE list_of_type_assignments
;

list_of_ports: OPEN_PARENS port (COMMA port)* CLOSE_PARENS;
list_of_ports
: OPEN_PARENS CLOSE_PARENS
| OPEN_PARENS port (COMMA port)* CLOSE_PARENS
;

list_of_port_declarations
: OPEN_PARENS (
Expand Down Expand Up @@ -384,7 +386,10 @@ module_common_item
| system_task
;

module_item: port_declaration SEMICOLON | non_port_module_item;
module_item
: port_declaration SEMICOLON
| non_port_module_item
;

module_or_generate_item
: attribute_instance* (
Expand Down
7 changes: 2 additions & 5 deletions grammar/SV3_1aPpParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ description
escaped_identifier: ESCAPED_IDENTIFIER;

macro_instance
: (Macro_identifier | Macro_Escaped_identifier) Spaces* OPEN_PARENS macro_actual_args CLOSE_PARENS
: (Macro_identifier | Macro_Escaped_identifier) Spaces* OPEN_PARENS macro_actual_args CLOSE_PARENS
# MacroInstanceWithArgs
| (Macro_identifier | Macro_Escaped_identifier) # MacroInstanceNoArgs
;
Expand Down Expand Up @@ -352,10 +352,7 @@ multiline_args_macro_definition

simple_no_args_macro_definition
: TICK_DEFINE Spaces (Simple_identifier | ESCAPED_IDENTIFIER) Spaces simple_macro_definition_body
(
CR
| One_line_comment
)
(CR | One_line_comment)
| TICK_DEFINE Spaces (Simple_identifier | ESCAPED_IDENTIFIER) Spaces* CR
;

Expand Down
3 changes: 3 additions & 0 deletions include/Surelog/CommandLine/CommandLineParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ class CommandLineParser final {
bool sepComp() const { return m_sepComp; }
bool link() const { return m_link; }
bool gc() const { return m_gc; }
bool parseTree() const { return m_parseTree; }
void setParse(bool val) { m_parse = val; }
void setParseTree(bool val) { m_parseTree = val; }
void setParseOnly(bool val) { m_parseOnly = val; }
void setLowMem(bool val) { m_lowMem = val; }
void setCompile(bool val) { m_compile = val; }
Expand Down Expand Up @@ -311,6 +313,7 @@ class CommandLineParser final {
bool m_parametersubstitution;
bool m_letexprsubstitution;
bool m_diffCompMode;
bool m_parseTree;
bool m_help;
bool m_cacheAllowed;
bool m_writeCache;
Expand Down
20 changes: 14 additions & 6 deletions include/Surelog/Design/FileContent.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <Surelog/Design/DesignComponent.h>
#include <Surelog/Design/VObject.h>

#include <ostream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -101,12 +102,6 @@ class FileContent : public DesignComponent {
bool isInstance() const override { return false; }
std::string_view getName() const override;
NodeId getRootNode() const;
std::string printObjects() const; // The whole file content
std::string printSubTree(
NodeId parentIndex) const; // Print subtree from parent
std::string printObject(NodeId noedId) const; // Only print that object
std::vector<std::string> collectSubTree(
NodeId uniqueId) const; // Helper function
SymbolTable* getSymbolTable() const { return m_symbolTable; }
void setSymbolTable(SymbolTable* table) { m_symbolTable = table; }
PathId getFileId(NodeId id) const;
Expand Down Expand Up @@ -215,6 +210,19 @@ class FileContent : public DesignComponent {
void populateCoreMembers(NodeId startIndex, NodeId endIndex,
UHDM::any* instance) const;

void sortTree();
bool validate() const;

std::string printObjects() const; // The whole file content
std::string printObject(NodeId nodeId) const; // Only print that object

void printTree(std::ostream& strm) const;
void printTree(std::ostream& strm, NodeId id, size_t indent = 0) const;

private:
void sortTree(NodeId parentId);
bool validate(NodeId parentId) const;

protected:
std::vector<DesignElement*> m_elements;
std::map<std::string, DesignElement*, StringViewCompare> m_elementMap;
Expand Down
19 changes: 14 additions & 5 deletions include/Surelog/SourceCompile/CommonListenerHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <Surelog/SourceCompile/VObjectTypes.h>

#include <map>
#include <regex>
#include <string>

namespace antlr4 {
Expand All @@ -46,14 +47,21 @@ namespace SURELOG {
class FileContent;
class VObject;

static constexpr char EscapeSequence[] = "#~@";
static constexpr char kEscapeSequence[] = "#~@";

static constexpr std::string_view kPreprocBeginPrefix = "{!< ";
static constexpr std::string_view kPreprocBeginSuffix = " !}";

static constexpr std::string_view kPreprocEndPrefix = "{! ";
static constexpr std::string_view kPreprocEndSuffix = " >!}";

class CommonListenerHelper {
public:
virtual ~CommonListenerHelper() = default;

public:
FileContent* getFileContent() { return m_fileContent; }
const FileContent* getFileContent() const { return m_fileContent; }

protected:
virtual SymbolId registerSymbol(std::string_view symbol) = 0;
Expand Down Expand Up @@ -85,8 +93,7 @@ class CommonListenerHelper {

NodeId getObjectId(antlr4::ParserRuleContext* ctx) const;

virtual std::tuple<PathId, uint32_t, uint16_t, uint32_t,
uint16_t>
virtual std::tuple<PathId, uint32_t, uint16_t, uint32_t, uint16_t>
getFileLine(antlr4::ParserRuleContext* ctx, antlr4::Token* token) const = 0;

NodeId& MutableChild(NodeId index);
Expand All @@ -95,8 +102,7 @@ class CommonListenerHelper {

protected:
CommonListenerHelper(FileContent* file_content,
antlr4::CommonTokenStream* tokens)
: m_fileContent(file_content), m_tokens(tokens) {}
antlr4::CommonTokenStream* tokens);

// These should be *const, but they are still set in some places.
// TODO: fix these places.
Expand All @@ -105,6 +111,9 @@ class CommonListenerHelper {

typedef std::map<const antlr4::tree::ParseTree*, NodeId> ContextToObjectMap;
ContextToObjectMap m_contextToObjectMap;

const std::regex m_escSeqReplaceRegex;
const std::regex m_escSeqSearchRegex;
};

} // namespace SURELOG
Expand Down
12 changes: 8 additions & 4 deletions include/Surelog/SourceCompile/ParseTreeListener.template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <Surelog/SourceCompile/VObjectTypes.h>

#include <cstdint>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -98,6 +100,7 @@ class ParseTreeListener {
ParseTreeListener() = default;
virtual ~ParseTreeListener() = default;

virtual bool shouldWalkSourceFile(PathId fileId) { return true; }
virtual void enterSourceFile(PathId fileId) {}
virtual void leaveSourceFile(PathId fileId) {}

Expand All @@ -114,15 +117,16 @@ class ParseTreeListener {
// clang-format on

void listen(const ParseTreeNode& node);
void listenChildren(const ParseTreeNode& node, bool ordered);
void listenSiblings(const ParseTreeNode& node, bool ordered);
void listenChildren(const ParseTreeNode& node);
void listenSiblings(const ParseTreeNode& node);

void listen(PathId fileId, const VObject* objects, size_t count,
const SymbolTable* symbolTable);

VObjectType getNodeType(const ParseTreeNode& node) const;
ParseTreeNode getRootNode() const;
bool getNodeText(const ParseTreeNode& node, std::string& text) const;
bool getNodeText(const ParseTreeNode& node, std::string_view& text) const;
bool getNodeFileId(const ParseTreeNode& node, PathId& fileId) const;
bool getNodeStartLocation(const ParseTreeNode& node, int32_t& line,
int32_t& column) const;
Expand All @@ -134,9 +138,9 @@ class ParseTreeListener {
ParseTreeNode getNodeParent(const ParseTreeNode& node) const;
ParseTreeNode getNodePrevSibling(const ParseTreeNode& node) const;
ParseTreeNode getNodeNextSibling(const ParseTreeNode& node) const;
bool getNodeChildren(const ParseTreeNode& node, bool ordered,
bool getNodeChildren(const ParseTreeNode& node,
parsetreenode_vector_t& children) const;
bool getNodeSiblings(const ParseTreeNode& node, bool ordered,
bool getNodeSiblings(const ParseTreeNode& node,
parsetreenode_vector_t& siblings) const;

private:
Expand Down
98 changes: 98 additions & 0 deletions include/Surelog/SourceCompile/SV3_1aParseTreeListener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Copyright 2019 Alain Dargelas
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/*
* File: SV3_1aParseTreeListener.h
* Author: hs
*
* Created on January 31, 2023, 12:00 PM
*/

#ifndef SURELOG_SV3_1APARSETREELISTENER_H
#define SURELOG_SV3_1APARSETREELISTENER_H
#pragma once

#include <Surelog/SourceCompile/SV3_1aTreeShapeHelper.h>
#include <parser/SV3_1aParserBaseListener.h>

#include <map>
#include <optional>
#include <regex>
#include <tuple>
#include <vector>

namespace SURELOG {
class SV3_1aParseTreeListener final : public SV3_1aParserBaseListener,
public SV3_1aTreeShapeHelper {
typedef std::vector<VObject> vobjects_t;

typedef std::vector<antlr4::ParserRuleContext*> rule_callstack_t;
typedef std::set<const antlr4::Token*> visited_tokens_t;
typedef std::multimap<antlr4::ParserRuleContext*, NodeId> orphan_objects_t;
typedef std::vector<antlr4::Token*> preproc_begin_statck_t;

typedef std::tuple<uint16_t, int32_t> column_offset_t;
typedef std::multimap<uint32_t, column_offset_t> offsets_t;

public:
SV3_1aParseTreeListener(ParseFile* pf, antlr4::CommonTokenStream* tokens,
uint32_t lineOffset, FileContent* ppFileContent);
~SV3_1aParseTreeListener() final = default;

void enterString_value(SV3_1aParser::String_valueContext* ctx) final;

void enterEveryRule(antlr4::ParserRuleContext* ctx) final;
void exitEveryRule(antlr4::ParserRuleContext* ctx) final;
void visitTerminal(antlr4::tree::TerminalNode* node) final;
void visitErrorNode(antlr4::tree::ErrorNode* node) final;

private:
using SV3_1aTreeShapeHelper::addVObject;
NodeId addVObject(antlr4::ParserRuleContext* ctx, antlr4::Token* token,
VObjectType objectType);
NodeId addVObject(antlr4::tree::TerminalNode* node, VObjectType objectType);

NodeId mergeObjectTree(NodeId ppNodeId);
std::optional<bool> isUnaryOperator(
const antlr4::tree::TerminalNode* node) const;

void sortChildren(vobjects_t& objects, NodeId id) const;
void applyLocationOffsets();
void visitPreprocBegin(antlr4::Token* token);
void visitPreprocEnd(antlr4::Token* token, NodeId ppNodeId);
void processPendingTokens(antlr4::ParserRuleContext* ctx,
size_t endTokenIndex);
void processOrphanObjects(antlr4::ParserRuleContext* ctx, NodeId parentId);

typedef std::map<uint32_t, std::pair<uint16_t, uint16_t>> line_ends_t;
void collectLineRanges(NodeId ppParentId, line_ends_t &ends) const;
void collectLineRanges(const antlr4::Token* begin, const antlr4::Token* end,
line_ends_t& ends) const;

private:
FileContent* const m_ppFileContent = nullptr;
offsets_t m_offsets;

rule_callstack_t m_ruleCallstack;
visited_tokens_t m_visitedTokens;
orphan_objects_t m_orphanObjects;
preproc_begin_statck_t m_preprocBeginStack;

size_t m_lastVisitedTokenIndex = 0;
int32_t m_paused = 0;
};
} // namespace SURELOG
#endif // SURELOG_SV3_1APARSETREELISTENER_H
Loading

0 comments on commit 29d94d9

Please sign in to comment.