Skip to content

Commit

Permalink
Merge pull request #1051 from alainmarcel/alainmarcel-patch-1
Browse files Browse the repository at this point in the history
Rewrite rule for for_loop
  • Loading branch information
alaindargelas authored Nov 5, 2023
2 parents b69b728 + 8442bc5 commit 91616db
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
44 changes: 44 additions & 0 deletions templates/SynthSubset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,48 @@ bool SynthSubset::reportedParent(const any* object) {
return false;
}

void SynthSubset::leaveFor_stmt(const for_stmt* object, vpiHandle handle) {
if (const expr* cond = object->VpiCondition()) {
// Rewrite rule for Yosys (Cannot handle non-constant expression in for loop condition besides loop var)
// Transforms:
// for (int i=0; i<32 && found==0; i++) begin
// end
// Into:
// for (int i=0; i<32; i++) begin
// if (found==0) break;
// end
//

if (cond->UhdmType() == uhdmoperation) {
operation* topOp = (operation*) cond;
if (topOp->VpiOpType() == vpiLogAndOp) {
VectorOfany* operands = topOp->Operands();
// Assumes lhs is comprator over loop var
// rhs is any expression
any* lhs = operands->at(0);
any* rhs = operands->at(1);
((for_stmt*)object)->VpiCondition((expr*)lhs);
VectorOfany* stlist = nullptr;
if (const any* stmt = object->VpiStmt()) {
if (stmt->UhdmType() == uhdmbegin) {
begin* st = (begin*) stmt;
stlist = st->Stmts();
} else if (stmt->UhdmType() == uhdmnamed_begin) {
named_begin* st = (named_begin*) stmt;
stlist = st->Stmts();
}
if (stlist) {
if_stmt* ifstmt = serializer_->MakeIf_stmt();
stlist->insert(stlist->begin(), ifstmt);
ifstmt->VpiCondition((expr*)rhs);
break_stmt* brk = serializer_->MakeBreak_stmt();
ifstmt->VpiStmt(brk);
}
}
}
}
}
}


} // namespace UHDM
3 changes: 3 additions & 0 deletions templates/SynthSubset.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class SynthSubset final : public VpiListener {

void leaveClass_var(const class_var* object, vpiHandle handle) override;

// Apply some rewrite rule for Yosys limitations
void leaveFor_stmt(const for_stmt* object, vpiHandle handle) override;

void reportError(const any* object);
void mark(const any* object);
bool reportedParent(const any* object);
Expand Down

0 comments on commit 91616db

Please sign in to comment.