Skip to content

Commit

Permalink
Implement directives
Browse files Browse the repository at this point in the history
  • Loading branch information
hs-apotell committed Oct 27, 2023
1 parent 3b219d2 commit c3ba0f4
Show file tree
Hide file tree
Showing 20 changed files with 989 additions and 84 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
15 changes: 3 additions & 12 deletions grammar/SV3_1aLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,9 @@ String

// A.9.2 Comments

/* one_line_comment ::= // comment_text \n */
One_line_comment: '//' ~[\r\n]* -> channel(COMMENTS);

One_line_comment
: '//' Comment_text '\r'? ('\n' | EOF) -> channel(COMMENTS)
;

// block_comment ::= /* comment_text */

Block_comment: '/*' Comment_text '*/' -> channel(COMMENTS);

/* comment_text ::= { Any_ASCII_character } */

fragment Comment_text: .*?;
Block_comment: '/*' .*? '*/' -> channel(COMMENTS);

ASSOCIATIVE_UNSPECIFIED: '[' [ ]* '*' [ ]* ']';

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

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

TILDA: '~';

Expand Down
61 changes: 42 additions & 19 deletions grammar/SV3_1aParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,16 @@ 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: ;

comment: One_line_comment | Block_comment;

whitespace: White_space;

description
: module_declaration
: Preproc_identifier
| module_declaration
| udp_declaration
| interface_declaration
| program_declaration
Expand All @@ -160,6 +164,8 @@ description
| attribute_instance* (package_item | bind_directive)
| config_declaration
| top_directives
| comment
| whitespace
;

module_nonansi_header
Expand Down Expand Up @@ -248,7 +254,7 @@ program_declaration
;

class_declaration
: VIRTUAL? CLASS lifetime? identifier parameter_port_list? (
: VIRTUAL? CLASS lifetime? identifier parameter_port_list? Preproc_identifier? (
EXTENDS class_type (
OPEN_PARENS list_of_arguments CLOSE_PARENS
)?
Expand All @@ -260,13 +266,14 @@ class_declaration
interface_class_type: ps_identifier parameter_value_assignment?;

interface_class_declaration
: INTERFACE CLASS identifier parameter_port_list? (
: INTERFACE CLASS identifier parameter_port_list? Preproc_identifier? (
EXTENDS interface_class_type (COMMA interface_class_type)*
)? SEMICOLON interface_class_item* ENDCLASS (COLON identifier)?
;

interface_class_item
: type_declaration
: Preproc_identifier
| type_declaration
| attribute_instance* interface_class_method SEMICOLON
| local_parameter_declaration SEMICOLON
| parameter_declaration SEMICOLON
Expand Down Expand Up @@ -308,7 +315,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 +394,11 @@ module_common_item
| system_task
;

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

module_or_generate_item
: attribute_instance* (
Expand All @@ -405,7 +419,8 @@ module_or_generate_item_declaration
;

non_port_module_item
: generate_region
: Preproc_identifier
| generate_region
| module_or_generate_item
| specify_block
| attribute_instance* specparam_declaration
Expand Down Expand Up @@ -529,7 +544,8 @@ checker_generate_item
;

class_item
: attribute_instance* (
: Preproc_identifier
| attribute_instance* (
class_property
| class_method
| class_constraint
Expand Down Expand Up @@ -597,7 +613,7 @@ super_dot_new: SUPER DOT NEW;
class_constructor_declaration
: FUNCTION class_scope? NEW (
OPEN_PARENS tf_port_list? CLOSE_PARENS
)? SEMICOLON block_item_declaration* (
)? SEMICOLON Preproc_identifier* block_item_declaration* (
super_dot_new (OPEN_PARENS list_of_arguments CLOSE_PARENS)? SEMICOLON
)? function_statement_or_null* ENDFUNCTION (COLON NEW)?
;
Expand Down Expand Up @@ -665,7 +681,8 @@ extern_constraint_declaration
identifier_list: identifier (COMMA identifier)*;

package_item
: package_or_generate_item_declaration
: Preproc_identifier
| package_or_generate_item_declaration
| specparam_declaration
| anonymous_program
| package_export_declaration
Expand Down Expand Up @@ -1286,7 +1303,8 @@ task_prototype

block_item_declaration
: attribute_instance* (
data_declaration
Preproc_identifier
| data_declaration
| local_parameter_declaration SEMICOLON
| parameter_declaration SEMICOLON
| overload_declaration
Expand Down Expand Up @@ -2355,7 +2373,8 @@ join_none_keyword: JOIN_NONE;
statement_or_null: statement | attribute_instance* SEMICOLON;

statement
: (identifier COLON)? attribute_instance* statement_item
: Preproc_identifier
| (identifier COLON)? attribute_instance* statement_item
;

statement_item
Expand Down Expand Up @@ -3240,7 +3259,8 @@ inc_or_dec_expression
;

constant_expression
: constant_primary
: Preproc_identifier
| constant_primary
| (
PLUS
| MINUS
Expand Down Expand Up @@ -3307,7 +3327,8 @@ constant_mintypmax_expression
;

constant_param_expression
: constant_mintypmax_expression
: Preproc_identifier
| constant_mintypmax_expression
| data_type
| DOLLAR
;
Expand Down Expand Up @@ -3421,7 +3442,8 @@ constant_indexed_range
*/

expression
: primary
: Preproc_identifier
| primary
| OPEN_PARENS expression CLOSE_PARENS
| (
PLUS
Expand Down Expand Up @@ -3759,7 +3781,8 @@ hierarchical_identifier
;

identifier
: Simple_identifier
: Preproc_identifier
| Simple_identifier
| Escaped_identifier
| THIS // System Verilog keyword
| RANDOMIZE // System Verilog keyword
Expand Down
15 changes: 7 additions & 8 deletions grammar/SV3_1aPpParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ 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 CR?
# MacroInstanceWithArgs
| (Macro_identifier | Macro_Escaped_identifier) # MacroInstanceNoArgs
| (Macro_identifier | Macro_Escaped_identifier) CR? # MacroInstanceNoArgs
;

unterminated_string: DOUBLE_QUOTE string_blob* CR;
Expand Down Expand Up @@ -352,17 +352,16 @@ 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
)
| TICK_DEFINE Spaces (Simple_identifier | ESCAPED_IDENTIFIER) Spaces* CR
(CR | One_line_comment)
| TICK_DEFINE Spaces (Simple_identifier | ESCAPED_IDENTIFIER) Spaces*
(CR | One_line_comment)
;

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

identifier_in_macro_body: (Simple_identifier TICK_TICK?)*;
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
6 changes: 6 additions & 0 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 @@ -215,6 +216,11 @@ class FileContent : public DesignComponent {
void populateCoreMembers(NodeId startIndex, NodeId endIndex,
UHDM::any* instance) const;

void printTree(std::ostream& strm) const;

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

protected:
std::vector<DesignElement*> m_elements;
std::map<std::string, DesignElement*, StringViewCompare> m_elementMap;
Expand Down
70 changes: 70 additions & 0 deletions include/Surelog/SourceCompile/SV3_1aParseTreeListener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
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 <optional>
#include <regex>
#include <vector>

namespace SURELOG {
class SV3_1aParseTreeListener final : public SV3_1aParserBaseListener,
public SV3_1aTreeShapeHelper {
public:
SV3_1aParseTreeListener(FileContent* ppFileContent,
ParseFile* pf, antlr4::CommonTokenStream* tokens,
unsigned int lineOffset);
~SV3_1aParseTreeListener() final = default;

void enterString_value(SV3_1aParser::String_valueContext* 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 objtype);
NodeId addVObject(antlr4::tree::TerminalNode* node, VObjectType objtype);

NodeId mergeObjectTree(const VObject& object);
std::optional<bool> isUnaryOperator(const antlr4::tree::TerminalNode* node) const;
void applyColumnOffsets(NodeId nodeId) const;

private:
typedef std::map<int32_t, std::vector<std::pair<int32_t, int32_t>>>
column_offsets_t;

FileContent* const m_ppFileContent;
std::set<const antlr4::Token*> m_visited;
const std::regex m_escapeSequenceRegex;
column_offsets_t m_columnOffsets;
};
} // namespace SURELOG
#endif // SURELOG_SV3_1APARSETREELISTENER_H
Loading

0 comments on commit c3ba0f4

Please sign in to comment.