Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow constant-folding of arbitrary-precision integer casts #4325

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions frontends/common/constantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,16 @@ const IR::Node *DoConstantFolding::postorder(IR::Cast *e) {
} else {
return e;
}
} else if (etype->is<IR::Type_InfInt>()) {
if (const auto *constant = expr->to<IR::Constant>()) {
const auto *ctype = constant->type;
if (!ctype->is<IR::Type_Bits>() && !ctype->is<IR::Type_InfInt>()) {
::error(ErrorType::ERR_INVALID, "%1%: Cannot cast %1% to arbitrary presion integer",
ctype);
return e;
}
return new IR::Constant(e->srcInfo, etype, constant->value, constant->base);
}
} else if (etype->is<IR::Type_Boolean>()) {
if (expr->is<IR::BoolLiteral>()) return expr;
if (expr->is<IR::Constant>()) {
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/issue2444-first.p4
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const int a = 1;
const bool b = true;
const int z = (int)2w1;
const bool w = (bool)(int)2w1;
const int z = 1;
const bool w = true;
const int z1 = 1;
const bool w1 = true;
const int z2 = 3;
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/issue3219-first.p4
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
bit<4> func1() {
bit<4> t = (bit<4>)(int)1;
bit<4> t = 4w1;
return t;
}
bit<4> func2() {
bit<4> t = (bit<4>)(int)4w1;
bit<4> t = 4w1;
return t;
}
control c(out bit<4> result) {
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/issue3219-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ control c(out bit<4> result) {
@name("c.retval") bit<4> retval;
@name("c.t") bit<4> t_0;
apply {
t_0 = (bit<4>)(int)1;
t_0 = 4w1;
retval = t_0;
result = retval;
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/issue3219-midend.p4
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
control c(out bit<4> result) {
@hidden action issue3219l13() {
result = (bit<4>)(int)1;
result = 4w1;
}
@hidden table tbl_issue3219l13 {
actions = {
Expand Down
Loading