Skip to content

Commit

Permalink
sv: par block, close #98
Browse files Browse the repository at this point in the history
  • Loading branch information
Nic30 committed Mar 31, 2020
1 parent eb53a35 commit 1f12e20
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 25 deletions.
4 changes: 2 additions & 2 deletions hdlConvertor/hdlAst/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
HdlDirection, HdlIntValue, HdlName, HdlOthers, HdlTypeAuto, HdlTypeType,
iHdlExpr)
from hdlConvertor.hdlAst._statements import (HdlImport, HdlStmAssign,
HdlStmBlock, HdlStmBreak, HdlStmCase, HdlStmContinue, HdlStmFor,
HdlStmForIn, HdlStmIf, HdlStmProcess, HdlStmReturn, HdlStmWait,
HdlStmBlockJoinType, HdlStmBlock, HdlStmBreak, HdlStmCase, HdlStmContinue,
HdlStmFor, HdlStmForIn, HdlStmIf, HdlStmProcess, HdlStmReturn, HdlStmWait,
HdlStmWhile)
from hdlConvertor.hdlAst._structural import (HdlComponentInst, HdlContext,
HdlModuleDec, HdlModuleDef, HdlNamespace, HdlLibrary)
Expand Down
19 changes: 15 additions & 4 deletions hdlConvertor/hdlAst/_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from hdlConvertor.hdlAst._bases import iHdlStatement
from hdlConvertor.hdlAst._expr import iHdlExpr
from enum import Enum


class HdlImport(iHdlStatement):
Expand All @@ -26,17 +27,27 @@ class HdlStmNop():
__slots__ = []


class HdlStmBlockJoinType(Enum):
"""
Python equivalent of C++ hdlConvertor::hdlObjects::HdlStmBlockJoinType
"""
(
SEQ, # sequential block
PAR_JOIN, # parallel block where we are wainting on all threads
PAR_JOIN_ANY, # parallel block where we are wainting on first finished thread
PAR_JOIN_NONE, # parallel block where we are are not waiting on any thread
) = range(4)


class HdlStmBlock(iHdlStatement):
"""
Block of statements in HDL
"""
__slots__ = ["is_parallel", "join_any", "join_none", "body", ]
__slots__ = ["join_t", "body", ]

def __init__(self):
super(HdlStmBlock, self).__init__()
self.is_parallel = False
self.join_any = False
self.join_none = False
self.join_t = HdlStmBlockJoinType.SEQ # type: HdlStmBlockJoinType
self.body = [] # type: List[iHdlObj]


Expand Down
2 changes: 2 additions & 0 deletions hdlConvertor/toPy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ToPy::ToPy() {
import(HdlStmBreakCls, "HdlStmBreak");
import(HdlStmContinueCls, "HdlStmContinue");
import(HdlStmWaitCls, "HdlStmWait");
import(HdlStmBlockJoinTypeCls, "HdlStmBlockJoinType");
import(HdlStmBlockCls, "HdlStmBlock");
import(HdlLibraryCls, "HdlLibrary");
import(HdlImportCls, "HdlImport");
Expand Down Expand Up @@ -457,6 +458,7 @@ ToPy::~ToPy() {
Py_XDECREF(HdlImportCls);
Py_XDECREF(HdlLibraryCls);
Py_XDECREF(HdlStmBlockCls);
Py_XDECREF(HdlStmBlockJoinTypeCls);
Py_XDECREF(HdlStmWaitCls);
Py_XDECREF(HdlStmContinueCls);
Py_XDECREF(HdlStmBreakCls);
Expand Down
2 changes: 2 additions & 0 deletions hdlConvertor/toPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ToPy {
PyObject *HdlStmContinueCls;
PyObject *HdlStmWaitCls;
PyObject *HdlStmBlockCls;
PyObject *HdlStmBlockJoinTypeCls;
PyObject *HdlImportCls;
PyObject *HdlLibraryCls;
PyObject *HdlComponentInstCls;
Expand Down Expand Up @@ -159,6 +160,7 @@ class ToPy {
PyObject* toPy(const hdlObjects::HdlVariableDef *o);
PyObject* toPy(const hdlObjects::HdlStmExpr *o);
PyObject* toPy(const hdlObjects::HdlStmIf *o);
PyObject* toPy(const hdlObjects::HdlStmBlockJoinType o);
PyObject* toPy(const hdlObjects::HdlStmBlock *o);
PyObject* toPy(const hdlObjects::HdlStmCase *o);
PyObject* toPy(const hdlObjects::HdlStmFor *o);
Expand Down
23 changes: 23 additions & 0 deletions hdlConvertor/toPy_statements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,34 @@ PyObject* ToPy::toPy(const HdlStmIf *o) {
return py_inst;
}

PyObject* ToPy::toPy(const HdlStmBlockJoinType o) {
const char *name;
try {
name = HdlStmBlockJoinType_toString(o);
} catch (const std::runtime_error &e) {
PyErr_SetString(PyExc_ValueError, e.what());
return nullptr;
}
return PyObject_GetAttrString(HdlStmBlockJoinTypeCls, name);
}

PyObject* ToPy::toPy(const HdlStmBlock *o) {
auto py_inst = PyObject_CallObject(HdlStmBlockCls, NULL);
if (!py_inst) {
return nullptr;
}
auto join_t = toPy(o->join_t);
if (!join_t) {
Py_DECREF(py_inst);
return nullptr;
}
int e = PyObject_SetAttrString(py_inst, "join_t", join_t);
if (e) {
Py_DECREF(join_t);
Py_DECREF(py_inst);
return nullptr;
}

if (toPy_arr(py_inst, "body", o->statements))
return nullptr;

Expand Down
10 changes: 10 additions & 0 deletions include/hdlConvertor/hdlObjects/hdlStmBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@
namespace hdlConvertor {
namespace hdlObjects {

enum HdlStmBlockJoinType {
SEQ, // sequential block
PAR_JOIN, // parallel block where we are wainting on all threads
PAR_JOIN_ANY, // parallel block where we are wainting on first finished thread
PAR_JOIN_NONE, // parallel block where we are are not waiting on any thread
};

/*
* HDL AST node for block statement (begin-end in SV)
* */
class HdlStmBlock: public iHdlStatement {
public:
// [todo] rename to obs
HdlStmBlockJoinType join_t;
std::vector<std::unique_ptr<iHdlObj>> statements;

HdlStmBlock();
Expand All @@ -21,5 +29,7 @@ class HdlStmBlock: public iHdlStatement {
virtual ~HdlStmBlock() override;
};

const char * HdlStmBlockJoinType_toString(HdlStmBlockJoinType o);

}
}
26 changes: 26 additions & 0 deletions include/hdlConvertor/svConvertor/statementParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <hdlConvertor/hdlObjects/hdlStmBlock.h>
#include <hdlConvertor/hdlObjects/hdlVariableDef.h>
#include <hdlConvertor/svConvertor/commentParser.h>
#include <hdlConvertor/svConvertor/exprParser.h>
#include <hdlConvertor/createObject.h>


namespace hdlConvertor {
namespace sv {
Expand Down Expand Up @@ -53,8 +56,31 @@ class VerStatementParser {
std::vector<std::unique_ptr<hdlObjects::iHdlStatement>> &res);
std::unique_ptr<hdlObjects::HdlStmAssign> visitNonblocking_assignment(
sv2017Parser::Nonblocking_assignmentContext *ctx);
template<typename BLOCK_T>
std::unique_ptr<hdlObjects::HdlStmBlock> visit_block(BLOCK_T* ctx) {
// X ( COLON identifier | {_input->LA(1) != COLON}? )
// ( block_item_declaration )* ( statement_or_null )*
// X (COLON identifier | {_input->LA(1) != COLON}?);
auto _label = ctx->identifier(0);
std::vector<std::unique_ptr<hdlObjects::iHdlObj>> items;
for (auto bid : ctx->block_item_declaration()) {
visitBlock_item_declaration(bid, items);
}
for (auto stm : ctx->statement_or_null()) {
auto i = visitStatement_or_null(stm);
items.push_back(move(i));
}
auto b = create_object<hdlObjects::HdlStmBlock>(ctx, items);
if (_label) {
VerExprParser ep(commentParser);
b->labels.push_back(ep.getIdentifierStr(_label));
}
return b;
}
std::unique_ptr<hdlObjects::HdlStmBlock> visitSeq_block(
sv2017Parser::Seq_blockContext *ctx);
std::unique_ptr<hdlObjects::HdlStmBlock> visitPar_block(
sv2017Parser::Par_blockContext *ctx);
void visitBlock_item_declaration(
sv2017Parser::Block_item_declarationContext *ctx,
std::vector<std::unique_ptr<hdlObjects::iHdlObj>> &res);
Expand Down
21 changes: 18 additions & 3 deletions src/hdlObjects/hdlStmBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,35 @@ namespace hdlConvertor {
namespace hdlObjects {

HdlStmBlock::HdlStmBlock() :
iHdlStatement() {
iHdlStatement(), join_t(HdlStmBlockJoinType::SEQ) {
}

HdlStmBlock::HdlStmBlock(std::vector<std::unique_ptr<iHdlObj>> &_statements) :
iHdlStatement(), statements(move(_statements)) {
iHdlStatement(), join_t(HdlStmBlockJoinType::SEQ), statements(move(_statements)) {
}

HdlStmBlock::HdlStmBlock(std::unique_ptr<iHdlObj> obj) :
iHdlStatement() {
HdlStmBlock() {
statements.push_back(move(obj));
}

HdlStmBlock::~HdlStmBlock() {
}

const char* HdlStmBlockJoinType_toString(HdlStmBlockJoinType o) {
switch (o) {
case HdlStmBlockJoinType::SEQ:
return "SEQ";
case HdlStmBlockJoinType::PAR_JOIN:
return "PAR_JOIN";
case HdlStmBlockJoinType::PAR_JOIN_ANY:
return "PAR_JOIN_ANY";
case HdlStmBlockJoinType::PAR_JOIN_NONE:
return "PAR_JOIN_NONE";
default:
throw std::runtime_error("invalid value for HdlStmBlockJoinType");
}
}

}
}
44 changes: 28 additions & 16 deletions src/svConvertor/statementParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,7 @@ unique_ptr<iHdlStatement> VerStatementParser::visitStatement_item(
}
auto pb = ctx->par_block();
if (pb) {
NotImplementedLogger::print(
"VerStatementParser.visitStatement_item.par_block", pb);
return create_object<HdlStmNop>(ctx);
return visitPar_block(pb);
}
auto ptcs = ctx->procedural_timing_control_statement();
if (ptcs) {
Expand Down Expand Up @@ -587,23 +585,37 @@ unique_ptr<HdlStmBlock> VerStatementParser::visitSeq_block(
// KW_BEGIN ( COLON identifier | {_input->LA(1) != COLON}? )
// ( block_item_declaration )* ( statement_or_null )*
// KW_END (COLON identifier | {_input->LA(1) != COLON}?);
auto _label = ctx->identifier(0);
vector<unique_ptr<iHdlObj>> items;
for (auto bid : ctx->block_item_declaration()) {
visitBlock_item_declaration(bid, items);
}
for (auto stm : ctx->statement_or_null()) {
auto i = visitStatement_or_null(stm);
items.push_back(move(i));
}
auto b = create_object<HdlStmBlock>(ctx, items);
if (_label) {
VerExprParser ep(commentParser);
b->labels.push_back(ep.getIdentifierStr(_label));
return visit_block(ctx);
}

HdlStmBlockJoinType visitJoin_keyword(sv2017Parser::Join_keywordContext * ctx) {
// join_keyword:
// KW_JOIN
// | KW_JOIN_ANY
// | KW_JOIN_NONE
// ;
if (ctx->KW_JOIN())
return HdlStmBlockJoinType::PAR_JOIN;
else if (ctx->KW_JOIN_ANY())
return HdlStmBlockJoinType::PAR_JOIN_ANY;
else {
assert(ctx->KW_JOIN_NONE());
return HdlStmBlockJoinType::PAR_JOIN_NONE;
}
}

unique_ptr<HdlStmBlock> VerStatementParser::visitPar_block(
sv2017Parser::Par_blockContext *ctx) {
// par_block:
// KW_FORK ( COLON identifier | {_input->LA(1) != COLON}? )
// ( block_item_declaration )* ( statement_or_null )*
// join_keyword ( COLON identifier | {_input->LA(1) != COLON}? );
auto b = visit_block(ctx);
b->join_t = visitJoin_keyword(ctx->join_keyword());
return b;
}


void VerStatementParser::visitBlock_item_declaration(
sv2017Parser::Block_item_declarationContext *ctx,
std::vector<unique_ptr<iHdlObj>> &res) {
Expand Down

0 comments on commit 1f12e20

Please sign in to comment.