aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-constexpr.h
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-01-13 22:01:59 +0000
committerPhilip Herron <philip.herron@embecosm.com>2022-01-14 12:32:40 +0000
commit52780af6602763fac6297f7983878b38e0188bb9 (patch)
tree41a8d89d018f527b3f20159da1279f67aac71f40 /gcc/rust/backend/rust-constexpr.h
parent06c2a74f557ec98896c9f71ba666bd969c4735d2 (diff)
downloadgcc-52780af6602763fac6297f7983878b38e0188bb9.zip
gcc-52780af6602763fac6297f7983878b38e0188bb9.tar.gz
gcc-52780af6602763fac6297f7983878b38e0188bb9.tar.bz2
Redesign constant folding from the typechecking pass to the backend
In Rust the ArrayType has a constant capacity constraint, this means it allows for bounds checking at compile time as no variable length arrays are allowed. In order to typecheck this case we had a constant folding pass as part of the typechecking system which generated gcc tree's for the IR and enforced the constant checking along the way. Also after doing some testing GCC with optimizations turned on is capable of constant folding/propogating the compilation unit fully. Which meant we need a method of doing with regardless of optimization level to be able to be on par with what the Rust language expects we need a full proof method. Turns out the CPP front-end already does this via its constexpr mechanism to ensure that these _do_ fold correclty. Another major reason to do this change is that the original const fold pass was a striped down copy of what the backend is _already_ doing which is creating a duplication of the code generation pass. With this all unified into the code generation pass all we need to do is port over gcc/cp/constexpr.c to enforce the const rules fully but at the GCC tree level not at the typed HIR level. Now that we have unified the pass when we hit a const function we can simply emit a normal GCC function and outside of const expressions GCC will simply emit a normal CallExpr and depending on optimization level fully optimize this. If we are in a ConstDecl we will follow the rust-constexpr.cc and fold the values or error_mark_node with an apropriate error. By reusing the CPP constexpr code we _know_ it works so reusing it as much as possible is a good idea in general for this front-end. Addresses #799
Diffstat (limited to 'gcc/rust/backend/rust-constexpr.h')
-rw-r--r--gcc/rust/backend/rust-constexpr.h46
1 files changed, 46 insertions, 0 deletions
diff --git a/gcc/rust/backend/rust-constexpr.h b/gcc/rust/backend/rust-constexpr.h
new file mode 100644
index 0000000..40d9159
--- /dev/null
+++ b/gcc/rust/backend/rust-constexpr.h
@@ -0,0 +1,46 @@
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#ifndef RUST_CONSTEXPR
+#define RUST_CONSTEXPR
+
+#include "rust-system.h"
+#include "tree.h"
+
+namespace Rust {
+namespace Compile {
+
+class ConstCtx
+{
+public:
+ static tree fold (tree);
+
+ tree constexpr_expression (tree);
+ tree eval_binary_expression (tree);
+ tree eval_call_expression (tree);
+ tree constexpr_fn_retval (tree);
+ tree eval_store_expression (tree);
+
+private:
+ ConstCtx ();
+
+ HOST_WIDE_INT constexpr_ops_count;
+};
+
+} // namespace Compile
+} // namespace Rust
+
+#endif // RUST_CONSTEXPR