diff --git a/frontends/common/constantFolding.cpp b/frontends/common/constantFolding.cpp index f05d6d1dfc..432673aadf 100644 --- a/frontends/common/constantFolding.cpp +++ b/frontends/common/constantFolding.cpp @@ -808,6 +808,16 @@ const IR::Node *DoConstantFolding::postorder(IR::Cast *e) { } else { return e; } + } else if (etype->is()) { + if (const auto *constant = expr->to()) { + const auto *ctype = constant->type; + if (!ctype->is() && !ctype->is()) { + ::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()) { if (expr->is()) return expr; if (expr->is()) { diff --git a/testdata/p4_16_samples/constant-fold-infint.p4 b/testdata/p4_16_samples/constant-fold-infint.p4 new file mode 100644 index 0000000000..a275e3d36c --- /dev/null +++ b/testdata/p4_16_samples/constant-fold-infint.p4 @@ -0,0 +1,9 @@ +const bit<4> a = 0b0101; +const int b = (int)a; // 5 +const bit<7> c = (bit<7>)b; // 5 + +const int<4> d = -1; +const int e = (int)d; // -1 +const bit<7> f = (bit<7>)e; // 0b1111111 = 127 +const int<7> g = (int<7>)e; // 0b1111111 = -1 +const int h = (int)g; // -1 diff --git a/testdata/p4_16_samples_outputs/constant-fold-infint-first.p4 b/testdata/p4_16_samples_outputs/constant-fold-infint-first.p4 new file mode 100644 index 0000000000..28f1deba6a --- /dev/null +++ b/testdata/p4_16_samples_outputs/constant-fold-infint-first.p4 @@ -0,0 +1,8 @@ +const bit<4> a = 4w0b101; +const int b = 0b101; +const bit<7> c = 7w0b101; +const int<4> d = -4s1; +const int e = -1; +const bit<7> f = 7w127; +const int<7> g = -7s1; +const int h = -1; diff --git a/testdata/p4_16_samples_outputs/constant-fold-infint-frontend.p4 b/testdata/p4_16_samples_outputs/constant-fold-infint-frontend.p4 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testdata/p4_16_samples_outputs/constant-fold-infint.p4 b/testdata/p4_16_samples_outputs/constant-fold-infint.p4 new file mode 100644 index 0000000000..c1545fbef7 --- /dev/null +++ b/testdata/p4_16_samples_outputs/constant-fold-infint.p4 @@ -0,0 +1,8 @@ +const bit<4> a = 0b101; +const int b = (int)a; +const bit<7> c = (bit<7>)b; +const int<4> d = -1; +const int e = (int)d; +const bit<7> f = (bit<7>)e; +const int<7> g = (int<7>)e; +const int h = (int)g; diff --git a/testdata/p4_16_samples_outputs/constant-fold-infint.p4-stderr b/testdata/p4_16_samples_outputs/constant-fold-infint.p4-stderr new file mode 100644 index 0000000000..3ca1c35cc6 --- /dev/null +++ b/testdata/p4_16_samples_outputs/constant-fold-infint.p4-stderr @@ -0,0 +1,4 @@ +constant-fold-infint.p4(6): [--Wwarn=mismatch] warning: -7w1: negative value with unsigned type +const int e = (int)d; // -1 + ^^^^^^ +[--Wwarn=missing] warning: Program does not contain a `main' module diff --git a/testdata/p4_16_samples_outputs/issue2444-first.p4 b/testdata/p4_16_samples_outputs/issue2444-first.p4 index 37592f5e91..f91215342c 100644 --- a/testdata/p4_16_samples_outputs/issue2444-first.p4 +++ b/testdata/p4_16_samples_outputs/issue2444-first.p4 @@ -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; diff --git a/testdata/p4_16_samples_outputs/issue3219-first.p4 b/testdata/p4_16_samples_outputs/issue3219-first.p4 index 644e103d45..8ffbb0ef22 100644 --- a/testdata/p4_16_samples_outputs/issue3219-first.p4 +++ b/testdata/p4_16_samples_outputs/issue3219-first.p4 @@ -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) { diff --git a/testdata/p4_16_samples_outputs/issue3219-frontend.p4 b/testdata/p4_16_samples_outputs/issue3219-frontend.p4 index c9c712e20d..3c0c8eea0e 100644 --- a/testdata/p4_16_samples_outputs/issue3219-frontend.p4 +++ b/testdata/p4_16_samples_outputs/issue3219-frontend.p4 @@ -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; } diff --git a/testdata/p4_16_samples_outputs/issue3219-midend.p4 b/testdata/p4_16_samples_outputs/issue3219-midend.p4 index 78f78f8caa..efabe485e1 100644 --- a/testdata/p4_16_samples_outputs/issue3219-midend.p4 +++ b/testdata/p4_16_samples_outputs/issue3219-midend.p4 @@ -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 = {