diff --git a/backends/p4tools/common/CMakeLists.txt b/backends/p4tools/common/CMakeLists.txt index 7438aac353b..480215b13dc 100644 --- a/backends/p4tools/common/CMakeLists.txt +++ b/backends/p4tools/common/CMakeLists.txt @@ -10,6 +10,7 @@ set( options.cpp version.cpp + compiler/compiler_result.cpp compiler/compiler_target.cpp compiler/convert_hs_index.cpp compiler/convert_struct_expr.cpp diff --git a/backends/p4tools/common/compiler/compiler_result.cpp b/backends/p4tools/common/compiler/compiler_result.cpp new file mode 100644 index 00000000000..52e6d33f1ff --- /dev/null +++ b/backends/p4tools/common/compiler/compiler_result.cpp @@ -0,0 +1,9 @@ +#include "backends/p4tools/common/compiler/compiler_result.h" + +namespace P4Tools { + +const IR::P4Program &CompilerResult::getProgram() const { return program; } + +CompilerResult::CompilerResult(const IR::P4Program &program) : program(program) {} + +} // namespace P4Tools diff --git a/backends/p4tools/common/compiler/compiler_result.h b/backends/p4tools/common/compiler/compiler_result.h new file mode 100644 index 00000000000..c9b3c6ee3c5 --- /dev/null +++ b/backends/p4tools/common/compiler/compiler_result.h @@ -0,0 +1,35 @@ +#ifndef BACKENDS_P4TOOLS_COMMON_COMPILER_COMPILER_RESULT_H_ +#define BACKENDS_P4TOOLS_COMMON_COMPILER_COMPILER_RESULT_H_ + +#include +#include + +#include "ir/ir.h" +#include "lib/castable.h" + +namespace P4Tools { + +/// An extensible result object which is returned by the CompilerTarget. +/// In its simplest form, this holds the transformed P4 program after the front- and midend passes. +class CompilerResult : public ICastable { + private: + /// The reference to the input P4 program, after it has been transformed by the compiler. + std::reference_wrapper program; + + public: + explicit CompilerResult(const IR::P4Program &program); + + /// @returns the reference to the input P4 program, after it has been transformed by the + /// compiler. + [[nodiscard]] const IR::P4Program &getProgram() const; + + DECLARE_TYPEINFO(CompilerResult); +}; + +/// P4Tools compilers may return an error instead of a compiler result. +/// This is a convenience definition for the return value. +using CompilerResultOrError = std::optional>; + +} // namespace P4Tools + +#endif /* BACKENDS_P4TOOLS_COMMON_COMPILER_COMPILER_RESULT_H_ */ diff --git a/backends/p4tools/common/compiler/compiler_target.cpp b/backends/p4tools/common/compiler/compiler_target.cpp index 22bdecc6d9a..f9731b2702d 100644 --- a/backends/p4tools/common/compiler/compiler_target.cpp +++ b/backends/p4tools/common/compiler/compiler_target.cpp @@ -18,47 +18,46 @@ namespace P4Tools { -const IR::P4Program &CompilerResult::getProgram() const { return program; } - -CompilerResult::CompilerResult(const IR::P4Program &program) : program(program) {} - -ICompileContext *CompilerTarget::makeContext() { return get().makeContextImpl(); } +ICompileContext *CompilerTarget::makeContext(const std::string &toolName) { + return get(toolName).makeContextImpl(); +} -std::vector *CompilerTarget::initCompiler(int argc, char **argv) { - return get().initCompilerImpl(argc, argv); +std::vector *CompilerTarget::initCompiler(const std::string &toolName, int argc, + char **argv) { + return get(toolName).initCompilerImpl(argc, argv); } -CompilerResultOrError CompilerTarget::runCompiler() { +CompilerResultOrError CompilerTarget::runCompiler(const std::string &toolName) { const auto *program = P4Tools::CompilerTarget::runParser(); if (program == nullptr) { return std::nullopt; } - return runCompiler(program); + return runCompiler(toolName, program); } -CompilerResultOrError CompilerTarget::runCompiler(const std::string &source) { +CompilerResultOrError CompilerTarget::runCompiler(const std::string &toolName, + const std::string &source) { const auto *program = P4::parseP4String(source, P4CContext::get().options().langVersion); if (program == nullptr) { return std::nullopt; } - return runCompiler(program); + return runCompiler(toolName, program); } -CompilerResultOrError CompilerTarget::runCompiler(const IR::P4Program *program) { - return get().runCompilerImpl(program); +CompilerResultOrError CompilerTarget::runCompiler(const std::string &toolName, + const IR::P4Program *program) { + return get(toolName).runCompilerImpl(program); } CompilerResultOrError CompilerTarget::runCompilerImpl(const IR::P4Program *program) const { - const auto &self = get(); - - program = self.runFrontend(program); + program = runFrontend(program); if (program == nullptr) { return std::nullopt; } - program = self.runMidEnd(program); + program = runMidEnd(program); if (program == nullptr) { return std::nullopt; } @@ -119,8 +118,12 @@ const IR::P4Program *CompilerTarget::runMidEnd(const IR::P4Program *program) con return program->apply(midEnd); } -CompilerTarget::CompilerTarget(std::string deviceName, std::string archName) - : Target("compiler", std::move(deviceName), std::move(archName)) {} +CompilerTarget::CompilerTarget(const std::string &toolName, const std::string &deviceName, + const std::string &archName) + : Target(toolName, deviceName, archName) {} + +const CompilerTarget &CompilerTarget::get(const std::string &toolName) { + return Target::get(toolName); +} -const CompilerTarget &CompilerTarget::get() { return Target::get("compiler"); } } // namespace P4Tools diff --git a/backends/p4tools/common/compiler/compiler_target.h b/backends/p4tools/common/compiler/compiler_target.h index d69422b5fcf..d2ccec936ed 100644 --- a/backends/p4tools/common/compiler/compiler_target.h +++ b/backends/p4tools/common/compiler/compiler_target.h @@ -1,69 +1,48 @@ #ifndef BACKENDS_P4TOOLS_COMMON_COMPILER_COMPILER_TARGET_H_ #define BACKENDS_P4TOOLS_COMMON_COMPILER_COMPILER_TARGET_H_ -#include -#include #include #include +#include "backends/p4tools/common/compiler/compiler_result.h" #include "backends/p4tools/common/compiler/midend.h" #include "backends/p4tools/common/core/target.h" #include "frontends/common/options.h" #include "frontends/p4/frontend.h" #include "ir/ir.h" -#include "lib/castable.h" #include "lib/compile_context.h" namespace P4Tools { -/// An extensible result object which is returned by the CompilerTarget. -/// In its simplest form, this holds the transformed P4 program after the front- and midend passes. -class CompilerResult : public ICastable { - private: - /// The reference to the input P4 program, after it has been transformed by the compiler. - std::reference_wrapper program; - - public: - explicit CompilerResult(const IR::P4Program &program); - - /// @returns the reference to the input P4 program, after it has been transformed by the - /// compiler. - [[nodiscard]] const IR::P4Program &getProgram() const; - - DECLARE_TYPEINFO(CompilerResult); -}; - -/// P4Tools compilers may return an error instead of a compiler result. -/// This is a convenience definition for the return value. -using CompilerResultOrError = std::optional>; - /// Encapsulates the details of invoking the P4 compiler for a target device and architecture. class CompilerTarget : public Target { public: /// @returns a new compilation context for the compiler. - static ICompileContext *makeContext(); + static ICompileContext *makeContext(const std::string &toolName); /// Initializes the P4 compiler with the given compiler-specific command-line arguments. /// /// @returns any unprocessed arguments, or nullptr if there was an error. - static std::vector *initCompiler(int argc, char **argv); + static std::vector *initCompiler(const std::string &toolName, int argc, + char **argv); /// Runs the P4 compiler to produce an IR and various other kinds of information on the input /// program. /// /// @returns std::nullopt if an error occurs during compilation. - static CompilerResultOrError runCompiler(); + static CompilerResultOrError runCompiler(const std::string &toolName); /// Runs the P4 compiler to produce an IR and other information for the given source code. /// /// @returns std::nullopt if an error occurs during compilation. - static CompilerResultOrError runCompiler(const std::string &source); + static CompilerResultOrError runCompiler(const std::string &toolName, + const std::string &source); private: /// Runs the front and mid ends on the given parsed program. /// /// @returns std::nullopt if an error occurs during compilation. - static CompilerResultOrError runCompiler(const IR::P4Program *); + static CompilerResultOrError runCompiler(const std::string &toolName, const IR::P4Program *); protected: /// @see @makeContext. @@ -98,11 +77,12 @@ class CompilerTarget : public Target { /// @returns nullptr if an error occurs during compilation. const IR::P4Program *runMidEnd(const IR::P4Program *program) const; - explicit CompilerTarget(std::string deviceName, std::string archName); + explicit CompilerTarget(const std::string &toolName, const std::string &deviceName, + const std::string &archName); private: /// @returns the singleton instance for the current target. - static const CompilerTarget &get(); + static const CompilerTarget &get(const std::string &toolName); }; } // namespace P4Tools diff --git a/backends/p4tools/common/core/target.cpp b/backends/p4tools/common/core/target.cpp index fd0eb1fd5dd..4a31eaf8a5d 100644 --- a/backends/p4tools/common/core/target.cpp +++ b/backends/p4tools/common/core/target.cpp @@ -76,7 +76,8 @@ const IR::Expression *Target::createTargetUninitialized(const IR::Type *type, return IR::getDefaultValue(type); } -Target::Target(std::string toolName, std::string deviceName, std::string archName) +Target::Target(const std::string &toolName, const std::string &deviceName, + const std::string &archName) : toolName(toolName), spec(deviceName, archName) { // Register this instance. BUG_CHECK(!registry[spec].count(toolName), "Already registered %1%/%2% instance for %3%", diff --git a/backends/p4tools/common/core/target.h b/backends/p4tools/common/core/target.h index 74edd758b48..f26db1b448a 100644 --- a/backends/p4tools/common/core/target.h +++ b/backends/p4tools/common/core/target.h @@ -70,7 +70,7 @@ class Target { protected: /// Creates and registers a new Target instance for the given @toolName, @deviceName, and /// @archName. - Target(std::string toolName, std::string deviceName, std::string archName); + Target(const std::string &toolName, const std::string &deviceName, const std::string &archName); /// @returns the target instance for the given tool and active target, as selected by @init. // diff --git a/backends/p4tools/common/options.cpp b/backends/p4tools/common/options.cpp index 8ddd83ab7af..2850d4179c3 100644 --- a/backends/p4tools/common/options.cpp +++ b/backends/p4tools/common/options.cpp @@ -50,12 +50,12 @@ std::optional AbstractP4cToolOptions::process( } // Establish the real compilation context. - auto *compilerContext = P4Tools::CompilerTarget::makeContext(); + auto *compilerContext = P4Tools::CompilerTarget::makeContext(_toolName); AutoCompileContext autoContext(compilerContext); // Initialize the compiler, forwarding any compiler-specific options. std::tie(argc, argv) = convertArgs(compilerArgs); - auto *unprocessedCompilerArgs = P4Tools::CompilerTarget::initCompiler(argc, argv); + auto *unprocessedCompilerArgs = P4Tools::CompilerTarget::initCompiler(_toolName, argc, argv); if ((unprocessedCompilerArgs == nullptr) || ::errorCount() > 0) { return std::nullopt; @@ -108,7 +108,8 @@ struct InheritedCompilerOptionSpec { std::optional> handler; }; -AbstractP4cToolOptions::AbstractP4cToolOptions(cstring message) : Options(message) { +AbstractP4cToolOptions::AbstractP4cToolOptions(const std::string &toolName, cstring message) + : Options(message), _toolName(toolName) { // Register some common options. registerOption( "--help", nullptr, @@ -209,4 +210,8 @@ AbstractP4cToolOptions::AbstractP4cToolOptions(cstring message) : Options(messag } } +bool AbstractP4cToolOptions::validateOptions() const { return true; } + +const std::string &AbstractP4cToolOptions::getToolName() const { return _toolName; } + } // namespace P4Tools diff --git a/backends/p4tools/common/options.h b/backends/p4tools/common/options.h index e4eb2fcda09..1ed1f2ff76a 100644 --- a/backends/p4tools/common/options.h +++ b/backends/p4tools/common/options.h @@ -17,7 +17,12 @@ namespace P4Tools { /// should use the singleton pattern and define a static get() for obtaining the singleton /// instance. class AbstractP4cToolOptions : protected Util::Options { + private: + /// The name of the tool associated with these options. + std::string _toolName; + public: + virtual ~AbstractP4cToolOptions() = default; /// A seed for the PRNG. std::optional seed = std::nullopt; @@ -29,6 +34,11 @@ class AbstractP4cToolOptions : protected Util::Options { /// @returns a compilation context on success, std::nullopt on error. std::optional process(const std::vector &args); + // No copy constructor and no self-assignments. + AbstractP4cToolOptions(const AbstractP4cToolOptions &) = delete; + + AbstractP4cToolOptions &operator=(const AbstractP4cToolOptions &) = delete; + protected: /// Command-line arguments to be sent to the compiler. Populated by @process. std::vector compilerArgs; @@ -38,17 +48,15 @@ class AbstractP4cToolOptions : protected Util::Options { /// Checks if parsed options make sense with respect to each-other. /// @returns true if the validation was successfull and false otherwise. - virtual bool validateOptions() const { return true; } + [[nodiscard]] virtual bool validateOptions() const; + + /// The name of the tool associated with these options. + [[nodiscard]] const std::string &getToolName() const; /// Converts a vector of command-line arguments into the traditional (argc, argv) format. static std::tuple convertArgs(const std::vector &args); - explicit AbstractP4cToolOptions(cstring message); - - // No copy constructor and no self-assignments. - AbstractP4cToolOptions(const AbstractP4cToolOptions &) = delete; - - AbstractP4cToolOptions &operator=(const AbstractP4cToolOptions &) = delete; + explicit AbstractP4cToolOptions(const std::string &toolName, cstring message); }; } // namespace P4Tools diff --git a/backends/p4tools/common/p4ctool.h b/backends/p4tools/common/p4ctool.h index e8efc8a1c3d..1ae0638caa1 100644 --- a/backends/p4tools/common/p4ctool.h +++ b/backends/p4tools/common/p4ctool.h @@ -30,7 +30,7 @@ class AbstractP4cTool { /// @param args /// Contains the path to the executable, followed by the command-line arguments for this /// tool. - int main(const std::vector &args) { + int main(const std::string &toolName, const std::vector &args) { // Register supported compiler targets. registerTarget(); @@ -55,7 +55,7 @@ class AbstractP4cTool { } // Run the compiler to get an IR and invoke the tool. - const auto compilerResult = P4Tools::CompilerTarget::runCompiler(); + const auto compilerResult = P4Tools::CompilerTarget::runCompiler(toolName); if (!compilerResult.has_value()) { return EXIT_FAILURE; } diff --git a/backends/p4tools/modules/testgen/CMakeLists.txt b/backends/p4tools/modules/testgen/CMakeLists.txt index 07875d0e0be..38bf31f3664 100644 --- a/backends/p4tools/modules/testgen/CMakeLists.txt +++ b/backends/p4tools/modules/testgen/CMakeLists.txt @@ -14,7 +14,7 @@ set( options.cpp testgen.cpp - core/compiler_target.cpp + core/compiler_result.cpp core/externs.cpp core/program_info.cpp core/small_step/abstract_stepper.cpp @@ -92,7 +92,6 @@ foreach(ext ${testgen_targets}) message("-- Enabling target ${ext}") add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/targets/${ext}) set(include_statements_var "${include_statements_var}#include \"backends/p4tools/modules/testgen/targets/${ext}/register.h\"\n") - set(compiler_targets_var "${compiler_targets_var} ${ext}RegisterCompilerTarget();\n") set(testgen_targets_var "${testgen_targets_var} ${ext}RegisterTestgenTarget();\n") endif() endif() diff --git a/backends/p4tools/modules/testgen/core/compiler_result.cpp b/backends/p4tools/modules/testgen/core/compiler_result.cpp new file mode 100644 index 00000000000..7059e0fde5c --- /dev/null +++ b/backends/p4tools/modules/testgen/core/compiler_result.cpp @@ -0,0 +1,26 @@ +#include "backends/p4tools/modules/testgen/core/compiler_result.h" + +#include + +#include "backends/p4tools/common/compiler/reachability.h" +#include "midend/coverage.h" + +namespace P4Tools::P4Testgen { + +TestgenCompilerResult::TestgenCompilerResult(CompilerResult compilerResult, + P4::Coverage::CoverageSet coverableNodes, + const NodesCallGraph *callGraph) + : CompilerResult(std::move(compilerResult)), + coverableNodes(std::move(coverableNodes)), + callGraph(callGraph) {} + +const NodesCallGraph &TestgenCompilerResult::getCallGraph() const { + BUG_CHECK(callGraph != nullptr, "The call graph has not been initialized."); + return *callGraph; +} + +const P4::Coverage::CoverageSet &TestgenCompilerResult::getCoverableNodes() const { + return coverableNodes; +} + +} // namespace P4Tools::P4Testgen diff --git a/backends/p4tools/modules/testgen/core/compiler_target.h b/backends/p4tools/modules/testgen/core/compiler_result.h similarity index 70% rename from backends/p4tools/modules/testgen/core/compiler_target.h rename to backends/p4tools/modules/testgen/core/compiler_result.h index ca634b40af5..6e0a66e470f 100644 --- a/backends/p4tools/modules/testgen/core/compiler_target.h +++ b/backends/p4tools/modules/testgen/core/compiler_result.h @@ -1,7 +1,7 @@ -#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_COMPILER_TARGET_H_ -#define BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_COMPILER_TARGET_H_ +#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_COMPILER_RESULT_H_ +#define BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_COMPILER_RESULT_H_ -#include "backends/p4tools/common/compiler/compiler_target.h" +#include "backends/p4tools/common/compiler/compiler_result.h" #include "backends/p4tools/common/compiler/reachability.h" #include "midend/coverage.h" @@ -32,14 +32,6 @@ class TestgenCompilerResult : public CompilerResult { DECLARE_TYPEINFO(TestgenCompilerResult, CompilerResult); }; -class TestgenCompilerTarget : public CompilerTarget { - protected: - explicit TestgenCompilerTarget(std::string deviceName, std::string archName); - - private: - CompilerResultOrError runCompilerImpl(const IR::P4Program *program) const override; -}; - } // namespace P4Tools::P4Testgen -#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_COMPILER_TARGET_H_ */ +#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_COMPILER_RESULT_H_ */ diff --git a/backends/p4tools/modules/testgen/core/compiler_target.cpp b/backends/p4tools/modules/testgen/core/compiler_target.cpp deleted file mode 100644 index d8fd2b339c6..00000000000 --- a/backends/p4tools/modules/testgen/core/compiler_target.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "backends/p4tools/modules/testgen/core/compiler_target.h" - -#include - -#include "backends/p4tools/common/compiler/reachability.h" -#include "midend/coverage.h" - -#include "backends/p4tools/modules/testgen/options.h" - -namespace P4Tools::P4Testgen { - -TestgenCompilerResult::TestgenCompilerResult(CompilerResult compilerResult, - P4::Coverage::CoverageSet coverableNodes, - const NodesCallGraph *callGraph) - : CompilerResult(std::move(compilerResult)), - coverableNodes(std::move(coverableNodes)), - callGraph(callGraph) {} - -const NodesCallGraph &TestgenCompilerResult::getCallGraph() const { - BUG_CHECK(callGraph != nullptr, "The call graph has not been initialized."); - return *callGraph; -} - -const P4::Coverage::CoverageSet &TestgenCompilerResult::getCoverableNodes() const { - return coverableNodes; -} - -TestgenCompilerTarget::TestgenCompilerTarget(std::string deviceName, std::string archName) - : CompilerTarget(std::move(deviceName), std::move(archName)) {} - -CompilerResultOrError TestgenCompilerTarget::runCompilerImpl(const IR::P4Program *program) const { - program = runFrontend(program); - if (program == nullptr) { - return std::nullopt; - } - - program = runMidEnd(program); - if (program == nullptr) { - return std::nullopt; - } - - // Create DCG. - NodesCallGraph *dcg = nullptr; - if (TestgenOptions::get().dcg || !TestgenOptions::get().pattern.empty()) { - dcg = new NodesCallGraph("NodesCallGraph"); - P4ProgramDCGCreator dcgCreator(dcg); - program->apply(dcgCreator); - } - - /// Collect coverage information about the program. - auto coverage = P4::Coverage::CollectNodes(TestgenOptions::get().coverageOptions); - program->apply(coverage); - - return { - *new TestgenCompilerResult(CompilerResult(*program), coverage.getCoverableNodes(), dcg)}; -} - -} // namespace P4Tools::P4Testgen diff --git a/backends/p4tools/modules/testgen/core/program_info.cpp b/backends/p4tools/modules/testgen/core/program_info.cpp index 3175193dd2c..69d751bd200 100644 --- a/backends/p4tools/modules/testgen/core/program_info.cpp +++ b/backends/p4tools/modules/testgen/core/program_info.cpp @@ -9,7 +9,7 @@ #include "lib/exceptions.h" #include "midend/coverage.h" -#include "backends/p4tools/modules/testgen/core/compiler_target.h" +#include "backends/p4tools/modules/testgen/core/compiler_result.h" #include "backends/p4tools/modules/testgen/lib/concolic.h" #include "backends/p4tools/modules/testgen/lib/continuation.h" diff --git a/backends/p4tools/modules/testgen/core/program_info.h b/backends/p4tools/modules/testgen/core/program_info.h index 7355f05d34c..c832af67c9b 100644 --- a/backends/p4tools/modules/testgen/core/program_info.h +++ b/backends/p4tools/modules/testgen/core/program_info.h @@ -3,7 +3,6 @@ #include #include -#include #include "backends/p4tools/common/compiler/reachability.h" #include "backends/p4tools/common/lib/arch_spec.h" @@ -12,7 +11,7 @@ #include "lib/rtti.h" #include "midend/coverage.h" -#include "backends/p4tools/modules/testgen/core/compiler_target.h" +#include "backends/p4tools/modules/testgen/core/compiler_result.h" #include "backends/p4tools/modules/testgen/lib/concolic.h" #include "backends/p4tools/modules/testgen/lib/continuation.h" diff --git a/backends/p4tools/modules/testgen/core/target.cpp b/backends/p4tools/modules/testgen/core/target.cpp index cb479173160..28da41a04c7 100644 --- a/backends/p4tools/modules/testgen/core/target.cpp +++ b/backends/p4tools/modules/testgen/core/target.cpp @@ -1,7 +1,6 @@ #include "backends/p4tools/modules/testgen/core/target.h" #include -#include #include "backends/p4tools/common/compiler/compiler_target.h" #include "backends/p4tools/common/core/target.h" @@ -12,11 +11,12 @@ #include "lib/exceptions.h" #include "backends/p4tools/modules/testgen/core/program_info.h" +#include "backends/p4tools/modules/testgen/toolname.h" namespace P4Tools::P4Testgen { -TestgenTarget::TestgenTarget(std::string deviceName, std::string archName) - : Target("testgen", std::move(deviceName), std::move(archName)) {} +TestgenTarget::TestgenTarget(const std::string &deviceName, const std::string &archName) + : CompilerTarget(TOOL_NAME, deviceName, archName) {} const ProgramInfo *TestgenTarget::produceProgramInfoImpl( const CompilerResult &compilerResult) const { @@ -60,4 +60,31 @@ CmdStepper *TestgenTarget::getCmdStepper(ExecutionState &state, AbstractSolver & return get().getCmdStepperImpl(state, solver, programInfo); } +CompilerResultOrError TestgenTarget::runCompilerImpl(const IR::P4Program *program) const { + program = runFrontend(program); + if (program == nullptr) { + return std::nullopt; + } + + program = runMidEnd(program); + if (program == nullptr) { + return std::nullopt; + } + + // Create DCG. + NodesCallGraph *dcg = nullptr; + if (TestgenOptions::get().dcg || !TestgenOptions::get().pattern.empty()) { + dcg = new NodesCallGraph("NodesCallGraph"); + P4ProgramDCGCreator dcgCreator(dcg); + program->apply(dcgCreator); + } + + /// Collect coverage information about the program. + auto coverage = P4::Coverage::CollectNodes(TestgenOptions::get().coverageOptions); + program->apply(coverage); + + return { + *new TestgenCompilerResult(CompilerResult(*program), coverage.getCoverableNodes(), dcg)}; +} + } // namespace P4Tools::P4Testgen diff --git a/backends/p4tools/modules/testgen/core/target.h b/backends/p4tools/modules/testgen/core/target.h index d7b186757c9..8edb5dbad5c 100644 --- a/backends/p4tools/modules/testgen/core/target.h +++ b/backends/p4tools/modules/testgen/core/target.h @@ -1,12 +1,9 @@ #ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_TARGET_H_ #define BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_TARGET_H_ -#include #include #include "backends/p4tools/common/compiler/compiler_target.h" -#include "backends/p4tools/common/core/target.h" -#include "backends/p4tools/common/lib/arch_spec.h" #include "ir/ir.h" #include "ir/solver.h" @@ -19,7 +16,7 @@ namespace P4Tools::P4Testgen { -class TestgenTarget : public Target { +class TestgenTarget : public CompilerTarget { public: /// @returns the singleton instance for the current target. static const TestgenTarget &get(); @@ -64,7 +61,10 @@ class TestgenTarget : public Target { virtual ExprStepper *getExprStepperImpl(ExecutionState &state, AbstractSolver &solver, const ProgramInfo &programInfo) const = 0; - explicit TestgenTarget(std::string deviceName, std::string archName); + explicit TestgenTarget(const std::string &deviceName, const std::string &archName); + + private: + CompilerResultOrError runCompilerImpl(const IR::P4Program *program) const override; }; } // namespace P4Tools::P4Testgen diff --git a/backends/p4tools/modules/testgen/main.cpp b/backends/p4tools/modules/testgen/main.cpp index a1e473ab083..cd5ca9f868c 100644 --- a/backends/p4tools/modules/testgen/main.cpp +++ b/backends/p4tools/modules/testgen/main.cpp @@ -7,6 +7,7 @@ #include "backends/p4tools/modules/testgen/lib/logging.h" #include "backends/p4tools/modules/testgen/testgen.h" +#include "backends/p4tools/modules/testgen/toolname.h" std::string updateErrorMsg(std::string errorMsg) { for (const std::string_view toReplace : {"Compiler", "compiler"}) { @@ -31,7 +32,7 @@ int main(int argc, char **argv) { int result = EXIT_SUCCESS; try { Util::ScopedTimer timer("P4Testgen Main"); - result = P4Tools::P4Testgen::Testgen().main(args); + result = P4Tools::P4Testgen::Testgen().main(P4Tools::P4Testgen::TOOL_NAME, args); } catch (const Util::CompilerBug &e) { std::cerr << "Internal error: " << updateErrorMsg(e.what()) << "\n"; std::cerr << "Please submit a bug report with your code." << "\n"; diff --git a/backends/p4tools/modules/testgen/options.cpp b/backends/p4tools/modules/testgen/options.cpp index 80d012ac7be..faa37f665d9 100644 --- a/backends/p4tools/modules/testgen/options.cpp +++ b/backends/p4tools/modules/testgen/options.cpp @@ -13,6 +13,7 @@ #include "lib/error.h" #include "backends/p4tools/modules/testgen/lib/logging.h" +#include "backends/p4tools/modules/testgen/toolname.h" namespace P4Tools::P4Testgen { @@ -28,7 +29,7 @@ const char *TestgenOptions::getIncludePath() { const std::set TestgenOptions::SUPPORTED_STOP_METRICS = {"MAX_NODE_COVERAGE"}; TestgenOptions::TestgenOptions() - : AbstractP4cToolOptions("Generate packet tests for a P4 program.") { + : AbstractP4cToolOptions(TOOL_NAME, "Generate packet tests for a P4 program.") { registerOption( "--strict", nullptr, [this](const char *) { diff --git a/backends/p4tools/modules/testgen/register.h.in b/backends/p4tools/modules/testgen/register.h.in index e8efb323ca1..27cef142a5b 100644 --- a/backends/p4tools/modules/testgen/register.h.in +++ b/backends/p4tools/modules/testgen/register.h.in @@ -5,9 +5,6 @@ namespace P4Tools::P4Testgen { -inline void registerCompilerTargets() { -@compiler_targets_var@} - inline void registerTestgenTargets() { @testgen_targets_var@} diff --git a/backends/p4tools/modules/testgen/targets/bmv2/CMakeLists.txt b/backends/p4tools/modules/testgen/targets/bmv2/CMakeLists.txt index 237b7eaf365..769053c775e 100644 --- a/backends/p4tools/modules/testgen/targets/bmv2/CMakeLists.txt +++ b/backends/p4tools/modules/testgen/targets/bmv2/CMakeLists.txt @@ -13,8 +13,8 @@ set(TESTGEN_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/test_backend/metadata.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_backend/ptf.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_backend/stf.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/bmv2.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cmd_stepper.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/compiler_result.cpp ${CMAKE_CURRENT_SOURCE_DIR}/concolic.cpp ${CMAKE_CURRENT_SOURCE_DIR}/constants.cpp ${CMAKE_CURRENT_SOURCE_DIR}/expr_stepper.cpp diff --git a/backends/p4tools/modules/testgen/targets/bmv2/bmv2.cpp b/backends/p4tools/modules/testgen/targets/bmv2/bmv2.cpp deleted file mode 100644 index d0a3d648688..00000000000 --- a/backends/p4tools/modules/testgen/targets/bmv2/bmv2.cpp +++ /dev/null @@ -1,134 +0,0 @@ -#include "backends/p4tools/modules/testgen/targets/bmv2/bmv2.h" - -#include -#include -#include - -#include "backends/bmv2/common/annotations.h" -#include "backends/p4tools/common/compiler/compiler_target.h" -#include "backends/p4tools/common/compiler/midend.h" -#include "frontends/common/options.h" -#include "frontends/common/resolveReferences/referenceMap.h" -#include "frontends/p4/typeChecking/typeChecker.h" -#include "frontends/p4/typeMap.h" -#include "lib/cstring.h" -#include "lib/error.h" -#include "midend/coverage.h" - -#include "backends/p4tools/modules/testgen/core/compiler_target.h" -#include "backends/p4tools/modules/testgen/options.h" -#include "backends/p4tools/modules/testgen/targets/bmv2/map_direct_externs.h" -#include "backends/p4tools/modules/testgen/targets/bmv2/p4_asserts_parser.h" -#include "backends/p4tools/modules/testgen/targets/bmv2/p4_refers_to_parser.h" -#include "backends/p4tools/modules/testgen/targets/bmv2/p4runtime_translation.h" - -namespace P4Tools::P4Testgen::Bmv2 { - -BMv2V1ModelCompilerResult::BMv2V1ModelCompilerResult(TestgenCompilerResult compilerResult, - P4::P4RuntimeAPI p4runtimeApi, - DirectExternMap directExternMap, - ConstraintsVector p4ConstraintsRestrictions) - : TestgenCompilerResult(std::move(compilerResult)), - p4runtimeApi(p4runtimeApi), - directExternMap(std::move(directExternMap)), - p4ConstraintsRestrictions(std::move(p4ConstraintsRestrictions)) {} - -const P4::P4RuntimeAPI &BMv2V1ModelCompilerResult::getP4RuntimeApi() const { return p4runtimeApi; } - -ConstraintsVector BMv2V1ModelCompilerResult::getP4ConstraintsRestrictions() const { - return p4ConstraintsRestrictions; -} - -const DirectExternMap &BMv2V1ModelCompilerResult::getDirectExternMap() const { - return directExternMap; -} - -Bmv2V1ModelCompilerTarget::Bmv2V1ModelCompilerTarget() : TestgenCompilerTarget("bmv2", "v1model") {} - -void Bmv2V1ModelCompilerTarget::make() { - static Bmv2V1ModelCompilerTarget *INSTANCE = nullptr; - if (INSTANCE == nullptr) { - INSTANCE = new Bmv2V1ModelCompilerTarget(); - } -} - -CompilerResultOrError Bmv2V1ModelCompilerTarget::runCompilerImpl( - const IR::P4Program *program) const { - program = runFrontend(program); - if (program == nullptr) { - return std::nullopt; - } - - /// After the front end, get the P4Runtime API for the V1model architecture. - auto p4runtimeApi = P4::P4RuntimeSerializer::get()->generateP4Runtime(program, "v1model"); - - if (::errorCount() > 0) { - return std::nullopt; - } - - program = runMidEnd(program); - if (program == nullptr) { - return std::nullopt; - } - - // Create DCG. - NodesCallGraph *dcg = nullptr; - if (TestgenOptions::get().dcg || !TestgenOptions::get().pattern.empty()) { - dcg = new NodesCallGraph("NodesCallGraph"); - P4ProgramDCGCreator dcgCreator(dcg); - program->apply(dcgCreator); - } - if (::errorCount() > 0) { - return std::nullopt; - } - /// Collect coverage information about the program. - auto coverage = P4::Coverage::CollectNodes(TestgenOptions::get().coverageOptions); - program->apply(coverage); - if (::errorCount() > 0) { - return std::nullopt; - } - - // Parses any @refers_to annotations and converts them into a vector of restrictions. - auto refersToParser = RefersToParser(); - program->apply(refersToParser); - if (::errorCount() > 0) { - return std::nullopt; - } - ConstraintsVector p4ConstraintsRestrictions = refersToParser.getRestrictionsVector(); - - // Defines all "entry_restriction" and then converts restrictions from string to IR - // expressions, and stores them in p4ConstraintsRestrictions to move targetConstraints - // further. - program->apply(AssertsParser(p4ConstraintsRestrictions)); - if (::errorCount() > 0) { - return std::nullopt; - } - // Try to map all instances of direct externs to the table they are attached to. - // Save the map in @var directExternMap. - auto directExternMapper = MapDirectExterns(); - program->apply(directExternMapper); - if (::errorCount() > 0) { - return std::nullopt; - } - - return {*new BMv2V1ModelCompilerResult{ - TestgenCompilerResult(CompilerResult(*program), coverage.getCoverableNodes(), dcg), - p4runtimeApi, directExternMapper.getDirectExternMap(), p4ConstraintsRestrictions}}; -} - -MidEnd Bmv2V1ModelCompilerTarget::mkMidEnd(const CompilerOptions &options) const { - MidEnd midEnd(options); - auto *refMap = midEnd.getRefMap(); - auto *typeMap = midEnd.getTypeMap(); - midEnd.addPasses({ - // Parse BMv2-specific annotations. - new BMV2::ParseAnnotations(), - new P4::TypeChecking(refMap, typeMap, true), - new PropagateP4RuntimeTranslation(*typeMap), - }); - midEnd.addDefaultPasses(); - - return midEnd; -} - -} // namespace P4Tools::P4Testgen::Bmv2 diff --git a/backends/p4tools/modules/testgen/targets/bmv2/compiler_result.cpp b/backends/p4tools/modules/testgen/targets/bmv2/compiler_result.cpp new file mode 100644 index 00000000000..350baf6451c --- /dev/null +++ b/backends/p4tools/modules/testgen/targets/bmv2/compiler_result.cpp @@ -0,0 +1,45 @@ +#include "backends/p4tools/modules/testgen/targets/bmv2/compiler_result.h" + +#include +#include +#include + +#include "backends/bmv2/common/annotations.h" +#include "backends/p4tools/common/compiler/compiler_target.h" +#include "backends/p4tools/common/compiler/midend.h" +#include "frontends/common/options.h" +#include "frontends/common/resolveReferences/referenceMap.h" +#include "frontends/p4/typeChecking/typeChecker.h" +#include "frontends/p4/typeMap.h" +#include "lib/cstring.h" +#include "lib/error.h" +#include "midend/coverage.h" + +#include "backends/p4tools/modules/testgen/options.h" +#include "backends/p4tools/modules/testgen/targets/bmv2/map_direct_externs.h" +#include "backends/p4tools/modules/testgen/targets/bmv2/p4_asserts_parser.h" +#include "backends/p4tools/modules/testgen/targets/bmv2/p4_refers_to_parser.h" +#include "backends/p4tools/modules/testgen/targets/bmv2/p4runtime_translation.h" + +namespace P4Tools::P4Testgen::Bmv2 { + +BMv2V1ModelCompilerResult::BMv2V1ModelCompilerResult(TestgenCompilerResult compilerResult, + P4::P4RuntimeAPI p4runtimeApi, + DirectExternMap directExternMap, + ConstraintsVector p4ConstraintsRestrictions) + : TestgenCompilerResult(std::move(compilerResult)), + p4runtimeApi(p4runtimeApi), + directExternMap(std::move(directExternMap)), + p4ConstraintsRestrictions(std::move(p4ConstraintsRestrictions)) {} + +const P4::P4RuntimeAPI &BMv2V1ModelCompilerResult::getP4RuntimeApi() const { return p4runtimeApi; } + +ConstraintsVector BMv2V1ModelCompilerResult::getP4ConstraintsRestrictions() const { + return p4ConstraintsRestrictions; +} + +const DirectExternMap &BMv2V1ModelCompilerResult::getDirectExternMap() const { + return directExternMap; +} + +} // namespace P4Tools::P4Testgen::Bmv2 diff --git a/backends/p4tools/modules/testgen/targets/bmv2/bmv2.h b/backends/p4tools/modules/testgen/targets/bmv2/compiler_result.h similarity index 73% rename from backends/p4tools/modules/testgen/targets/bmv2/bmv2.h rename to backends/p4tools/modules/testgen/targets/bmv2/compiler_result.h index 49300daf188..e4a44af9a85 100644 --- a/backends/p4tools/modules/testgen/targets/bmv2/bmv2.h +++ b/backends/p4tools/modules/testgen/targets/bmv2/compiler_result.h @@ -1,13 +1,12 @@ -#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_BMV2_H_ -#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_BMV2_H_ +#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_COMPILER_RESULT_H_ +#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_COMPILER_RESULT_H_ -#include "backends/p4tools/common/compiler/compiler_target.h" #include "backends/p4tools/common/compiler/midend.h" #include "backends/p4tools/common/lib/variables.h" #include "control-plane/p4RuntimeSerializer.h" #include "frontends/common/options.h" -#include "backends/p4tools/modules/testgen/core/compiler_target.h" +#include "backends/p4tools/modules/testgen/core/compiler_result.h" #include "backends/p4tools/modules/testgen/targets/bmv2/map_direct_externs.h" #include "backends/p4tools/modules/testgen/targets/bmv2/p4_asserts_parser.h" @@ -45,19 +44,6 @@ class BMv2V1ModelCompilerResult : public TestgenCompilerResult { DECLARE_TYPEINFO(BMv2V1ModelCompilerResult, TestgenCompilerResult); }; -class Bmv2V1ModelCompilerTarget : public TestgenCompilerTarget { - public: - /// Registers this target. - static void make(); - - private: - [[nodiscard]] MidEnd mkMidEnd(const CompilerOptions &options) const override; - - CompilerResultOrError runCompilerImpl(const IR::P4Program *program) const override; - - Bmv2V1ModelCompilerTarget(); -}; - } // namespace P4Tools::P4Testgen::Bmv2 -#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_BMV2_H_ */ +#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_COMPILER_RESULT_H_ */ diff --git a/backends/p4tools/modules/testgen/targets/bmv2/program_info.cpp b/backends/p4tools/modules/testgen/targets/bmv2/program_info.cpp index eae3da95b67..1ba2ffc7aee 100644 --- a/backends/p4tools/modules/testgen/targets/bmv2/program_info.cpp +++ b/backends/p4tools/modules/testgen/targets/bmv2/program_info.cpp @@ -19,13 +19,12 @@ #include "backends/p4tools/modules/testgen//lib/exceptions.h" #include "backends/p4tools/modules/testgen/core/program_info.h" -#include "backends/p4tools/modules/testgen/core/target.h" #include "backends/p4tools/modules/testgen/lib/concolic.h" #include "backends/p4tools/modules/testgen/lib/continuation.h" #include "backends/p4tools/modules/testgen/lib/execution_state.h" #include "backends/p4tools/modules/testgen/lib/packet_vars.h" #include "backends/p4tools/modules/testgen/options.h" -#include "backends/p4tools/modules/testgen/targets/bmv2/bmv2.h" +#include "backends/p4tools/modules/testgen/targets/bmv2/compiler_result.h" #include "backends/p4tools/modules/testgen/targets/bmv2/concolic.h" #include "backends/p4tools/modules/testgen/targets/bmv2/constants.h" diff --git a/backends/p4tools/modules/testgen/targets/bmv2/program_info.h b/backends/p4tools/modules/testgen/targets/bmv2/program_info.h index 1cbac7a922d..2af04731c86 100644 --- a/backends/p4tools/modules/testgen/targets/bmv2/program_info.h +++ b/backends/p4tools/modules/testgen/targets/bmv2/program_info.h @@ -2,17 +2,13 @@ #define BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_PROGRAM_INFO_H_ #include -#include -#include #include "control-plane/p4RuntimeSerializer.h" #include "ir/ir.h" #include "lib/cstring.h" -#include "lib/ordered_map.h" #include "backends/p4tools/modules/testgen/core/program_info.h" -#include "backends/p4tools/modules/testgen/lib/continuation.h" -#include "backends/p4tools/modules/testgen/targets/bmv2/bmv2.h" +#include "backends/p4tools/modules/testgen/targets/bmv2/compiler_result.h" namespace P4Tools::P4Testgen::Bmv2 { diff --git a/backends/p4tools/modules/testgen/targets/bmv2/register.h b/backends/p4tools/modules/testgen/targets/bmv2/register.h index 888db8faf8b..9da926b279c 100644 --- a/backends/p4tools/modules/testgen/targets/bmv2/register.h +++ b/backends/p4tools/modules/testgen/targets/bmv2/register.h @@ -4,15 +4,11 @@ #include "backends/p4tools/common/p4ctool.h" #include "backends/p4tools/modules/testgen/options.h" -#include "backends/p4tools/modules/testgen/targets/bmv2/bmv2.h" #include "backends/p4tools/modules/testgen/targets/bmv2/target.h" #include "backends/p4tools/modules/testgen/testgen.h" namespace P4Tools::P4Testgen { -/// Register the BMv2 compiler target with the tools framework. -inline void bmv2RegisterCompilerTarget() { Bmv2::Bmv2V1ModelCompilerTarget::make(); } - /// Register the BMv2 testgen target with the testgen framework. inline void bmv2RegisterTestgenTarget() { Bmv2::Bmv2V1ModelTestgenTarget::make(); } diff --git a/backends/p4tools/modules/testgen/targets/bmv2/target.cpp b/backends/p4tools/modules/testgen/targets/bmv2/target.cpp index bb279590821..a77168437dd 100644 --- a/backends/p4tools/modules/testgen/targets/bmv2/target.cpp +++ b/backends/p4tools/modules/testgen/targets/bmv2/target.cpp @@ -4,6 +4,7 @@ #include #include +#include "backends/bmv2/common/annotations.h" #include "backends/p4tools/common/lib/util.h" #include "ir/ir.h" #include "ir/solver.h" @@ -15,10 +16,12 @@ #include "backends/p4tools/modules/testgen/core/symbolic_executor/symbolic_executor.h" #include "backends/p4tools/modules/testgen/core/target.h" #include "backends/p4tools/modules/testgen/lib/execution_state.h" -#include "backends/p4tools/modules/testgen/targets/bmv2/bmv2.h" #include "backends/p4tools/modules/testgen/targets/bmv2/cmd_stepper.h" +#include "backends/p4tools/modules/testgen/targets/bmv2/compiler_result.h" #include "backends/p4tools/modules/testgen/targets/bmv2/constants.h" #include "backends/p4tools/modules/testgen/targets/bmv2/expr_stepper.h" +#include "backends/p4tools/modules/testgen/targets/bmv2/p4_refers_to_parser.h" +#include "backends/p4tools/modules/testgen/targets/bmv2/p4runtime_translation.h" #include "backends/p4tools/modules/testgen/targets/bmv2/program_info.h" #include "backends/p4tools/modules/testgen/targets/bmv2/test_backend.h" @@ -37,6 +40,85 @@ void Bmv2V1ModelTestgenTarget::make() { } } +CompilerResultOrError Bmv2V1ModelTestgenTarget::runCompilerImpl( + const IR::P4Program *program) const { + program = runFrontend(program); + if (program == nullptr) { + return std::nullopt; + } + + /// After the front end, get the P4Runtime API for the V1model architecture. + auto p4runtimeApi = P4::P4RuntimeSerializer::get()->generateP4Runtime(program, "v1model"); + + if (::errorCount() > 0) { + return std::nullopt; + } + + program = runMidEnd(program); + if (program == nullptr) { + return std::nullopt; + } + + // Create DCG. + NodesCallGraph *dcg = nullptr; + if (TestgenOptions::get().dcg || !TestgenOptions::get().pattern.empty()) { + dcg = new NodesCallGraph("NodesCallGraph"); + P4ProgramDCGCreator dcgCreator(dcg); + program->apply(dcgCreator); + } + if (::errorCount() > 0) { + return std::nullopt; + } + /// Collect coverage information about the program. + auto coverage = P4::Coverage::CollectNodes(TestgenOptions::get().coverageOptions); + program->apply(coverage); + if (::errorCount() > 0) { + return std::nullopt; + } + + // Parses any @refers_to annotations and converts them into a vector of restrictions. + auto refersToParser = RefersToParser(); + program->apply(refersToParser); + if (::errorCount() > 0) { + return std::nullopt; + } + ConstraintsVector p4ConstraintsRestrictions = refersToParser.getRestrictionsVector(); + + // Defines all "entry_restriction" and then converts restrictions from string to IR + // expressions, and stores them in p4ConstraintsRestrictions to move targetConstraints + // further. + program->apply(AssertsParser(p4ConstraintsRestrictions)); + if (::errorCount() > 0) { + return std::nullopt; + } + // Try to map all instances of direct externs to the table they are attached to. + // Save the map in @var directExternMap. + auto directExternMapper = MapDirectExterns(); + program->apply(directExternMapper); + if (::errorCount() > 0) { + return std::nullopt; + } + + return {*new BMv2V1ModelCompilerResult{ + TestgenCompilerResult(CompilerResult(*program), coverage.getCoverableNodes(), dcg), + p4runtimeApi, directExternMapper.getDirectExternMap(), p4ConstraintsRestrictions}}; +} + +MidEnd Bmv2V1ModelTestgenTarget::mkMidEnd(const CompilerOptions &options) const { + MidEnd midEnd(options); + auto *refMap = midEnd.getRefMap(); + auto *typeMap = midEnd.getTypeMap(); + midEnd.addPasses({ + // Parse BMv2-specific annotations. + new BMV2::ParseAnnotations(), + new P4::TypeChecking(refMap, typeMap, true), + new PropagateP4RuntimeTranslation(*typeMap), + }); + midEnd.addDefaultPasses(); + + return midEnd; +} + const Bmv2V1ModelProgramInfo *Bmv2V1ModelTestgenTarget::produceProgramInfoImpl( const CompilerResult &compilerResult, const IR::Declaration_Instance *mainDecl) const { // The blocks in the main declaration are just the arguments in the constructor call. diff --git a/backends/p4tools/modules/testgen/targets/bmv2/target.h b/backends/p4tools/modules/testgen/targets/bmv2/target.h index 7fa9fb3bc45..05921f9b374 100644 --- a/backends/p4tools/modules/testgen/targets/bmv2/target.h +++ b/backends/p4tools/modules/testgen/targets/bmv2/target.h @@ -1,11 +1,6 @@ #ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_TARGET_H_ #define BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_TARGET_H_ -#include -#include -#include - -#include "backends/p4tools/common/lib/arch_spec.h" #include "ir/ir.h" #include "ir/solver.h" @@ -42,6 +37,10 @@ class Bmv2V1ModelTestgenTarget : public TestgenTarget { private: Bmv2V1ModelTestgenTarget(); + + [[nodiscard]] MidEnd mkMidEnd(const CompilerOptions &options) const override; + + CompilerResultOrError runCompilerImpl(const IR::P4Program *program) const override; }; } // namespace P4Tools::P4Testgen::Bmv2 diff --git a/backends/p4tools/modules/testgen/targets/ebpf/CMakeLists.txt b/backends/p4tools/modules/testgen/targets/ebpf/CMakeLists.txt index 80639881e1f..ae363e560c2 100644 --- a/backends/p4tools/modules/testgen/targets/ebpf/CMakeLists.txt +++ b/backends/p4tools/modules/testgen/targets/ebpf/CMakeLists.txt @@ -9,7 +9,6 @@ set( TESTGEN_SOURCES ${TESTGEN_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/backend/stf/stf.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/ebpf.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cmd_stepper.cpp ${CMAKE_CURRENT_SOURCE_DIR}/concolic.cpp ${CMAKE_CURRENT_SOURCE_DIR}/constants.cpp diff --git a/backends/p4tools/modules/testgen/targets/ebpf/ebpf.cpp b/backends/p4tools/modules/testgen/targets/ebpf/ebpf.cpp deleted file mode 100644 index 81ec041d1c7..00000000000 --- a/backends/p4tools/modules/testgen/targets/ebpf/ebpf.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "backends/p4tools/modules/testgen/targets/ebpf/ebpf.h" - -#include - -#include "backends/p4tools/common/compiler/midend.h" -#include "frontends/common/options.h" - -namespace P4Tools::P4Testgen::EBPF { - -EBPFCompilerTarget::EBPFCompilerTarget() : TestgenCompilerTarget("ebpf", "ebpf") {} - -void EBPFCompilerTarget::make() { - static EBPFCompilerTarget *INSTANCE = nullptr; - if (INSTANCE == nullptr) { - INSTANCE = new EBPFCompilerTarget(); - } -} - -MidEnd EBPFCompilerTarget::mkMidEnd(const CompilerOptions &options) const { - MidEnd midEnd(options); - midEnd.addPasses({}); - midEnd.addDefaultPasses(); - - return midEnd; -} - -} // namespace P4Tools::P4Testgen::EBPF diff --git a/backends/p4tools/modules/testgen/targets/ebpf/ebpf.h b/backends/p4tools/modules/testgen/targets/ebpf/ebpf.h deleted file mode 100644 index 74277a23018..00000000000 --- a/backends/p4tools/modules/testgen/targets/ebpf/ebpf.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef TESTGEN_TARGETS_EBPF_EBPF_H_ -#define TESTGEN_TARGETS_EBPF_EBPF_H_ - -#include "backends/p4tools/common/compiler/midend.h" -#include "frontends/common/options.h" - -#include "backends/p4tools/modules/testgen/core/compiler_target.h" - -namespace P4Tools::P4Testgen::EBPF { - -class EBPFCompilerTarget : public TestgenCompilerTarget { - public: - /// Registers this target. - static void make(); - - private: - MidEnd mkMidEnd(const CompilerOptions &options) const override; - - EBPFCompilerTarget(); -}; - -} // namespace P4Tools::P4Testgen::EBPF - -#endif /* TESTGEN_TARGETS_EBPF_EBPF_H_ */ diff --git a/backends/p4tools/modules/testgen/targets/ebpf/program_info.cpp b/backends/p4tools/modules/testgen/targets/ebpf/program_info.cpp index e2687d95af0..8a1a6931c99 100644 --- a/backends/p4tools/modules/testgen/targets/ebpf/program_info.cpp +++ b/backends/p4tools/modules/testgen/targets/ebpf/program_info.cpp @@ -1,16 +1,13 @@ #include "backends/p4tools/modules/testgen/targets/ebpf/program_info.h" -#include #include #include -#include #include #include #include "backends/p4tools/common/lib/arch_spec.h" #include "backends/p4tools/common/lib/util.h" -#include "backends/p4tools/common/lib/variables.h" #include "ir/id.h" #include "ir/ir.h" #include "ir/irutils.h" @@ -18,9 +15,8 @@ #include "lib/exceptions.h" #include "backends/p4tools/modules/testgen//lib/exceptions.h" -#include "backends/p4tools/modules/testgen/core/compiler_target.h" +#include "backends/p4tools/modules/testgen/core/compiler_result.h" #include "backends/p4tools/modules/testgen/core/program_info.h" -#include "backends/p4tools/modules/testgen/core/target.h" #include "backends/p4tools/modules/testgen/lib/concolic.h" #include "backends/p4tools/modules/testgen/lib/continuation.h" #include "backends/p4tools/modules/testgen/lib/execution_state.h" diff --git a/backends/p4tools/modules/testgen/targets/ebpf/register.h b/backends/p4tools/modules/testgen/targets/ebpf/register.h index ba83458ab38..a9aa07aefab 100644 --- a/backends/p4tools/modules/testgen/targets/ebpf/register.h +++ b/backends/p4tools/modules/testgen/targets/ebpf/register.h @@ -4,15 +4,11 @@ #include "backends/p4tools/common/p4ctool.h" #include "backends/p4tools/modules/testgen/options.h" -#include "backends/p4tools/modules/testgen/targets/ebpf/ebpf.h" #include "backends/p4tools/modules/testgen/targets/ebpf/target.h" #include "backends/p4tools/modules/testgen/testgen.h" namespace P4Tools::P4Testgen { -/// Register the ebpf compiler target with the tools framework. -inline void ebpfRegisterCompilerTarget() { EBPF::EBPFCompilerTarget::make(); } - /// Register the ebpf testgen target with the testgen framework. inline void ebpfRegisterTestgenTarget() { EBPF::EBPFTestgenTarget::make(); } diff --git a/backends/p4tools/modules/testgen/targets/ebpf/target.cpp b/backends/p4tools/modules/testgen/targets/ebpf/target.cpp index 2591e32ac1c..06eea71060b 100644 --- a/backends/p4tools/modules/testgen/targets/ebpf/target.cpp +++ b/backends/p4tools/modules/testgen/targets/ebpf/target.cpp @@ -11,7 +11,7 @@ #include "lib/exceptions.h" #include "lib/ordered_map.h" -#include "backends/p4tools/modules/testgen/core/compiler_target.h" +#include "backends/p4tools/modules/testgen/core/compiler_result.h" #include "backends/p4tools/modules/testgen/core/program_info.h" #include "backends/p4tools/modules/testgen/core/symbolic_executor/symbolic_executor.h" #include "backends/p4tools/modules/testgen/core/target.h" @@ -86,4 +86,12 @@ EBPFExprStepper *EBPFTestgenTarget::getExprStepperImpl(ExecutionState &state, return new EBPFExprStepper(state, solver, programInfo); } +MidEnd EBPFTestgenTarget::mkMidEnd(const CompilerOptions &options) const { + MidEnd midEnd(options); + midEnd.addPasses({}); + midEnd.addDefaultPasses(); + + return midEnd; +} + } // namespace P4Tools::P4Testgen::EBPF diff --git a/backends/p4tools/modules/testgen/targets/ebpf/target.h b/backends/p4tools/modules/testgen/targets/ebpf/target.h index 84a87c321f4..679cb2e6860 100644 --- a/backends/p4tools/modules/testgen/targets/ebpf/target.h +++ b/backends/p4tools/modules/testgen/targets/ebpf/target.h @@ -42,6 +42,8 @@ class EBPFTestgenTarget : public TestgenTarget { private: EBPFTestgenTarget(); + + [[nodiscard]] MidEnd mkMidEnd(const CompilerOptions &options) const override; }; } // namespace P4Tools::P4Testgen::EBPF diff --git a/backends/p4tools/modules/testgen/targets/pna/CMakeLists.txt b/backends/p4tools/modules/testgen/targets/pna/CMakeLists.txt index 1d09c8c5e16..c2c65841f9c 100644 --- a/backends/p4tools/modules/testgen/targets/pna/CMakeLists.txt +++ b/backends/p4tools/modules/testgen/targets/pna/CMakeLists.txt @@ -14,7 +14,6 @@ set( ${CMAKE_CURRENT_SOURCE_DIR}/dpdk/expr_stepper.cpp ${CMAKE_CURRENT_SOURCE_DIR}/dpdk/program_info.cpp ${CMAKE_CURRENT_SOURCE_DIR}/dpdk/table_stepper.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/pna.cpp ${CMAKE_CURRENT_SOURCE_DIR}/concolic.cpp ${CMAKE_CURRENT_SOURCE_DIR}/constants.cpp ${CMAKE_CURRENT_SOURCE_DIR}/shared_cmd_stepper.cpp diff --git a/backends/p4tools/modules/testgen/targets/pna/dpdk/program_info.cpp b/backends/p4tools/modules/testgen/targets/pna/dpdk/program_info.cpp index 66c8ac2d382..68f8ae68bf6 100644 --- a/backends/p4tools/modules/testgen/targets/pna/dpdk/program_info.cpp +++ b/backends/p4tools/modules/testgen/targets/pna/dpdk/program_info.cpp @@ -1,8 +1,6 @@ #include "backends/p4tools/modules/testgen/targets/pna/dpdk/program_info.h" -#include #include -#include #include #include "backends/p4tools/common/lib/arch_spec.h" @@ -12,8 +10,7 @@ #include "lib/cstring.h" #include "lib/exceptions.h" -#include "backends/p4tools/modules/testgen/core/compiler_target.h" -#include "backends/p4tools/modules/testgen/core/target.h" +#include "backends/p4tools/modules/testgen/core/compiler_result.h" #include "backends/p4tools/modules/testgen/lib/concolic.h" #include "backends/p4tools/modules/testgen/lib/continuation.h" #include "backends/p4tools/modules/testgen/lib/exceptions.h" diff --git a/backends/p4tools/modules/testgen/targets/pna/pna.cpp b/backends/p4tools/modules/testgen/targets/pna/pna.cpp deleted file mode 100644 index 398232f4394..00000000000 --- a/backends/p4tools/modules/testgen/targets/pna/pna.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "backends/p4tools/modules/testgen/targets/pna/pna.h" - -#include - -#include "backends/p4tools/common/compiler/midend.h" -#include "frontends/common/options.h" - -namespace P4Tools::P4Testgen::Pna { - -PnaDpdkCompilerTarget::PnaDpdkCompilerTarget() : TestgenCompilerTarget("dpdk", "pna") {} - -void PnaDpdkCompilerTarget::make() { - static PnaDpdkCompilerTarget *INSTANCE = nullptr; - if (INSTANCE == nullptr) { - INSTANCE = new PnaDpdkCompilerTarget(); - } -} - -MidEnd PnaDpdkCompilerTarget::mkMidEnd(const CompilerOptions &options) const { - MidEnd midEnd(options); - midEnd.addPasses({}); - midEnd.addDefaultPasses(); - - return midEnd; -} - -} // namespace P4Tools::P4Testgen::Pna diff --git a/backends/p4tools/modules/testgen/targets/pna/pna.h b/backends/p4tools/modules/testgen/targets/pna/pna.h deleted file mode 100644 index 5cf53f111dd..00000000000 --- a/backends/p4tools/modules/testgen/targets/pna/pna.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_PNA_PNA_H_ -#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_PNA_PNA_H_ - -#include "backends/p4tools/common/compiler/midend.h" -#include "frontends/common/options.h" - -#include "backends/p4tools/modules/testgen/core/compiler_target.h" - -namespace P4Tools::P4Testgen { - -namespace Pna { - -class PnaDpdkCompilerTarget : public TestgenCompilerTarget { - public: - /// Registers this target. - static void make(); - - private: - MidEnd mkMidEnd(const CompilerOptions &options) const override; - - PnaDpdkCompilerTarget(); -}; - -} // namespace Pna - -} // namespace P4Tools::P4Testgen - -#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_PNA_PNA_H_ */ diff --git a/backends/p4tools/modules/testgen/targets/pna/register.h b/backends/p4tools/modules/testgen/targets/pna/register.h index 3a6c58d2bc3..0baa499e2b0 100644 --- a/backends/p4tools/modules/testgen/targets/pna/register.h +++ b/backends/p4tools/modules/testgen/targets/pna/register.h @@ -4,15 +4,11 @@ #include "backends/p4tools/common/p4ctool.h" #include "backends/p4tools/modules/testgen/options.h" -#include "backends/p4tools/modules/testgen/targets/pna/pna.h" #include "backends/p4tools/modules/testgen/targets/pna/target.h" #include "backends/p4tools/modules/testgen/testgen.h" namespace P4Tools::P4Testgen { -/// Register the PNA compiler target with the tools framework. -inline void pnaRegisterCompilerTarget() { Pna::PnaDpdkCompilerTarget::make(); } - /// Register the PNA testgen target with the testgen framework. inline void pnaRegisterTestgenTarget() { Pna::PnaDpdkTestgenTarget::make(); } diff --git a/backends/p4tools/modules/testgen/targets/pna/target.cpp b/backends/p4tools/modules/testgen/targets/pna/target.cpp index 99a134d2c0f..528b0833442 100644 --- a/backends/p4tools/modules/testgen/targets/pna/target.cpp +++ b/backends/p4tools/modules/testgen/targets/pna/target.cpp @@ -10,7 +10,7 @@ #include "lib/exceptions.h" #include "lib/ordered_map.h" -#include "backends/p4tools/modules/testgen/core/compiler_target.h" +#include "backends/p4tools/modules/testgen/core/compiler_result.h" #include "backends/p4tools/modules/testgen/core/program_info.h" #include "backends/p4tools/modules/testgen/core/symbolic_executor/symbolic_executor.h" #include "backends/p4tools/modules/testgen/core/target.h" @@ -78,4 +78,12 @@ PnaDpdkExprStepper *PnaDpdkTestgenTarget::getExprStepperImpl(ExecutionState &sta return new PnaDpdkExprStepper(state, solver, programInfo); } +MidEnd PnaDpdkTestgenTarget::mkMidEnd(const CompilerOptions &options) const { + MidEnd midEnd(options); + midEnd.addPasses({}); + midEnd.addDefaultPasses(); + + return midEnd; +} + } // namespace P4Tools::P4Testgen::Pna diff --git a/backends/p4tools/modules/testgen/targets/pna/target.h b/backends/p4tools/modules/testgen/targets/pna/target.h index 153989c2e30..2674b9b0ca7 100644 --- a/backends/p4tools/modules/testgen/targets/pna/target.h +++ b/backends/p4tools/modules/testgen/targets/pna/target.h @@ -42,6 +42,8 @@ class PnaDpdkTestgenTarget : public TestgenTarget { private: PnaDpdkTestgenTarget(); + + [[nodiscard]] MidEnd mkMidEnd(const CompilerOptions &options) const override; }; } // namespace P4Tools::P4Testgen::Pna diff --git a/backends/p4tools/modules/testgen/test/gtest_utils.cpp b/backends/p4tools/modules/testgen/test/gtest_utils.cpp index feaae806bbc..0070f3957ea 100644 --- a/backends/p4tools/modules/testgen/test/gtest_utils.cpp +++ b/backends/p4tools/modules/testgen/test/gtest_utils.cpp @@ -26,7 +26,7 @@ std::optional P4ToolsTestCase::create( deviceName, archName); // Set up the compilation context and set the source language. - AutoCompileContext autoCompileContext(P4Tools::CompilerTarget::makeContext()); + AutoCompileContext autoCompileContext(P4Tools::CompilerTarget::makeContext("testgen")); P4CContext::get().options().langVersion = langVersion; auto compilerResults = P4Tools::CompilerTarget::runCompiler(source); @@ -61,9 +61,6 @@ void P4ToolsTestCase::ensureInit() { if (INITIALIZED) { return; } - // Register supported compiler targets. - P4Tools::P4Testgen::registerCompilerTargets(); - // Register supported Testgen targets. P4Tools::P4Testgen::registerTestgenTargets(); diff --git a/backends/p4tools/modules/testgen/testgen.cpp b/backends/p4tools/modules/testgen/testgen.cpp index 935d6a052c9..e825727bf8e 100644 --- a/backends/p4tools/modules/testgen/testgen.cpp +++ b/backends/p4tools/modules/testgen/testgen.cpp @@ -14,7 +14,7 @@ #include "lib/cstring.h" #include "lib/error.h" -#include "backends/p4tools/modules/testgen/core/compiler_target.h" +#include "backends/p4tools/modules/testgen/core/compiler_result.h" #include "backends/p4tools/modules/testgen/core/program_info.h" #include "backends/p4tools/modules/testgen/core/symbolic_executor/depth_first.h" #include "backends/p4tools/modules/testgen/core/symbolic_executor/greedy_node_cov.h" @@ -27,6 +27,7 @@ #include "backends/p4tools/modules/testgen/lib/test_framework.h" #include "backends/p4tools/modules/testgen/options.h" #include "backends/p4tools/modules/testgen/register.h" +#include "backends/p4tools/modules/testgen/toolname.h" namespace P4Tools::P4Testgen { @@ -146,12 +147,6 @@ std::optional generateTestsImpl(std::optional generateTestsImpl(std::optional generateTestsImpl(std::optional(); diff --git a/backends/p4tools/modules/testgen/toolname.h b/backends/p4tools/modules/testgen/toolname.h new file mode 100644 index 00000000000..9161f3a5136 --- /dev/null +++ b/backends/p4tools/modules/testgen/toolname.h @@ -0,0 +1,12 @@ +#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TOOLNAME_H +#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TOOLNAME_H + +#include + +namespace P4Tools::P4Testgen { + +static const std::string TOOL_NAME = "testgen"; + +} // namespace P4Tools::P4Testgen + +#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TOOLNAME_H */