Skip to content

Commit ce7db66

Browse files
authored
Added cast to type support (#4284)
1 parent 8e1e2b9 commit ce7db66

File tree

4 files changed

+573
-2
lines changed

4 files changed

+573
-2
lines changed

frontends/ast/ast.cc

+15-1
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@ void AstNode::dumpVlog(FILE *f, std::string indent) const
478478
fprintf(f, ";\n");
479479
break;
480480

481+
case AST_WIRETYPE:
482+
fprintf(f, "%s", id2vl(str).c_str());
483+
break;
484+
481485
case AST_MEMORY:
482486
fprintf(f, "%s" "memory", indent.c_str());
483487
if (is_signed)
@@ -694,7 +698,17 @@ void AstNode::dumpVlog(FILE *f, std::string indent) const
694698
break;
695699

696700
case AST_CAST_SIZE:
697-
children[0]->dumpVlog(f, "");
701+
switch (children[0]->type)
702+
{
703+
case AST_WIRE:
704+
if (children[0]->children.size() > 0)
705+
children[0]->children[0]->dumpVlog(f, "");
706+
else
707+
fprintf(f, "%d'", children[0]->range_left - children[0]->range_right + 1);
708+
break;
709+
default:
710+
children[0]->dumpVlog(f, "");
711+
}
698712
fprintf(f, "'(");
699713
children[1]->dumpVlog(f, "");
700714
fprintf(f, ")");

frontends/ast/simplify.cc

+59-1
Original file line numberDiff line numberDiff line change
@@ -1500,11 +1500,69 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin
15001500
}
15011501
break;
15021502

1503+
case AST_CAST_SIZE: {
1504+
int width = 1;
1505+
AstNode *node;
1506+
AstNode *child = children[0];
1507+
1508+
if (child->type == AST_WIRE) {
1509+
if (child->children.size() == 0) {
1510+
// Base type (e.g., int)
1511+
width = child->range_left - child->range_right +1;
1512+
node = mkconst_int(width, child->is_signed);
1513+
} else {
1514+
// User defined type
1515+
log_assert(child->children[0]->type == AST_WIRETYPE);
1516+
1517+
const std::string &type_name = child->children[0]->str;
1518+
if (!current_scope.count(type_name))
1519+
input_error("Unknown identifier `%s' used as type name\n", type_name.c_str());
1520+
AstNode *resolved_type_node = current_scope.at(type_name);
1521+
if (resolved_type_node->type != AST_TYPEDEF)
1522+
input_error("`%s' does not name a type\n", type_name.c_str());
1523+
log_assert(resolved_type_node->children.size() == 1);
1524+
AstNode *template_node = resolved_type_node->children[0];
1525+
1526+
// Ensure typedef itself is fully simplified
1527+
while (template_node->simplify(const_fold, stage, width_hint, sign_hint)) {};
1528+
1529+
switch (template_node->type)
1530+
{
1531+
case AST_WIRE: {
1532+
if (template_node->children.size() > 0 && template_node->children[0]->type == AST_RANGE)
1533+
width = range_width(this, template_node->children[0]);
1534+
child->delete_children();
1535+
node = mkconst_int(width, true);
1536+
break;
1537+
}
1538+
1539+
case AST_STRUCT:
1540+
case AST_UNION: {
1541+
child->delete_children();
1542+
width = size_packed_struct(template_node, 0);
1543+
node = mkconst_int(width, false);
1544+
break;
1545+
}
1546+
1547+
default:
1548+
log_error("Don't know how to translate static cast of type %s\n", type2str(template_node->type).c_str());
1549+
}
1550+
}
1551+
1552+
delete child;
1553+
children.erase(children.begin());
1554+
children.insert(children.begin(), node);
1555+
}
1556+
1557+
detect_width_simple = true;
1558+
children_are_self_determined = true;
1559+
break;
1560+
}
1561+
15031562
case AST_TO_BITS:
15041563
case AST_TO_SIGNED:
15051564
case AST_TO_UNSIGNED:
15061565
case AST_SELFSZ:
1507-
case AST_CAST_SIZE:
15081566
case AST_CONCAT:
15091567
case AST_REPLICATE:
15101568
case AST_REDUCE_AND:

frontends/verilog/verilog_parser.y

+6
Original file line numberDiff line numberDiff line change
@@ -3506,6 +3506,12 @@ basic_expr:
35063506
$$ = new AstNode(AST_CAST_SIZE, $1, $4);
35073507
SET_AST_NODE_LOC($$, @1, @4);
35083508
} |
3509+
typedef_base_type OP_CAST '(' expr ')' {
3510+
if (!sv_mode)
3511+
frontend_verilog_yyerror("Static cast is only supported in SystemVerilog mode.");
3512+
$$ = new AstNode(AST_CAST_SIZE, $1, $4);
3513+
SET_AST_NODE_LOC($$, @1, @4);
3514+
} |
35093515
'(' expr '=' expr ')' {
35103516
ensureAsgnExprAllowed();
35113517
AstNode *node = new AstNode(AST_ASSIGN_EQ, $2, $4);

0 commit comments

Comments
 (0)