Skip to content

Commit

Permalink
Move static helpers from program info andargumentsToTypeDeclarations …
Browse files Browse the repository at this point in the history
…to common/lib/util
  • Loading branch information
vlstill committed Nov 30, 2023
1 parent adc8327 commit 08c7fd0
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 111 deletions.
64 changes: 64 additions & 0 deletions backends/p4tools/common/lib/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,68 @@ const IR::MethodCallExpression *Utils::generateInternalMethodCall(
args);
}

std::vector<const IR::Type_Declaration *> argumentsToTypeDeclarations(
const IR::IGeneralNamespace *ns, const IR::Vector<IR::Argument> *inputArgs) {
std::vector<const IR::Type_Declaration *> resultDecls;
for (const auto *arg : *inputArgs) {
const auto *expr = arg->expression;

const IR::Type_Declaration *declType = nullptr;

if (const auto *ctorCall = expr->to<IR::ConstructorCallExpression>()) {
const auto *constructedTypeName = ctorCall->constructedType->checkedTo<IR::Type_Name>();

// Find the corresponding type declaration in the top-level namespace.
declType =
findProgramDecl(ns, constructedTypeName->path)->checkedTo<IR::Type_Declaration>();
} else if (const auto *pathExpr = expr->to<IR::PathExpression>()) {
// Look up the path expression in the top-level namespace and expect to find a
// declaration instance.
const auto *declInstance =
findProgramDecl(ns, pathExpr->path)->checkedTo<IR::Declaration_Instance>();
declType = declInstance->type->checkedTo<IR::Type_Declaration>();
} else {
BUG("Unexpected main-declaration argument node type: %1%", expr->node_type_name());
}

// The constructor's parameter list should be empty, since the compiler should have
// substituted the constructor arguments for us.
if (const auto *iApply = declType->to<IR::IContainer>()) {
const IR::ParameterList *ctorParams = iApply->getConstructorParameters();
BUG_CHECK(ctorParams->empty(), "Compiler did not eliminate constructor parameters: %1%",
ctorParams);
} else {
BUG("Does not instantiate an IContainer: %1%", expr);
}

resultDecls.emplace_back(declType);
}
return resultDecls;
}

const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns, const IR::Path *path) {
auto name = path->name.name;
const auto *decls = ns->getDeclsByName(name)->toVector();
if (!decls->empty()) {
// TODO: Figure out what to do with multiple results. Maybe return all of them and
// let the caller sort it out?
BUG_CHECK(decls->size() == 1, "Handling of overloaded names not implemented");
return decls->at(0);
}
BUG("Variable %1% not found in the available namespaces.", path);
}

const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns,
const IR::PathExpression *pathExpr) {
return findProgramDecl(ns, pathExpr->path);
}

const IR::Type_Declaration *resolveProgramType(const IR::IGeneralNamespace *ns,
const IR::Type_Name *type) {
const auto *path = type->path;
const auto *decl = findProgramDecl(ns, path)->to<IR::Type_Declaration>();
BUG_CHECK(decl, "Not a type: %1%", path);
return decl;
}

} // namespace P4Tools
17 changes: 17 additions & 0 deletions backends/p4tools/common/lib/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ class Utils {
}
};

/// Converts the list of arguments @inputArgs to a list of type declarations. Any names appearing in
/// the arguments are resolved with @ns.
/// This is mainly useful for inspecting package instances.
std::vector<const IR::Type_Declaration *> argumentsToTypeDeclarations(
const IR::IGeneralNamespace *ns, const IR::Vector<IR::Argument> *inputArgs);

/// Looks up a declaration from a path. A BUG occurs if no declaration is found.
const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns, const IR::Path *path);

/// Looks up a declaration from a path expression. A BUG occurs if no declaration is found.
const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns,
const IR::PathExpression *pathExpr);

/// Resolves a Type_Name in the top-level namespace.
const IR::Type_Declaration *resolveProgramType(const IR::IGeneralNamespace *ns,
const IR::Type_Name *type);

} // namespace P4Tools

#endif /* BACKENDS_P4TOOLS_COMMON_LIB_UTIL_H_ */
1 change: 0 additions & 1 deletion backends/p4tools/modules/testgen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ set(
lib/test_backend.cpp
lib/test_framework.cpp
lib/test_spec.cpp
lib/util.cpp
)

# GTest source files for p4testgen.
Expand Down
30 changes: 0 additions & 30 deletions backends/p4tools/modules/testgen/core/program_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,6 @@ ProgramInfo::ProgramInfo(const IR::P4Program *program) : concolicMethodImpls({})
coverableNodes.insert(coveredNodes.begin(), coveredNodes.end());
}

/* =============================================================================================
* Namespaces and declarations
* ============================================================================================= */

const IR::IDeclaration *ProgramInfo::findProgramDecl(const IR::IGeneralNamespace *ns,
const IR::Path *path) {
auto name = path->name.name;
const auto *decls = ns->getDeclsByName(name)->toVector();
if (!decls->empty()) {
// TODO: Figure out what to do with multiple results. Maybe return all of them and
// let the caller sort it out?
BUG_CHECK(decls->size() == 1, "Handling of overloaded names not implemented");
return decls->at(0);
}
BUG("Variable %1% not found in the available namespaces.", path);
}

const IR::IDeclaration *ProgramInfo::findProgramDecl(const IR::IGeneralNamespace *ns,
const IR::PathExpression *pathExpr) {
return findProgramDecl(ns, pathExpr->path);
}

const IR::Type_Declaration *ProgramInfo::resolveProgramType(const IR::IGeneralNamespace *ns,
const IR::Type_Name *type) {
const auto *path = type->path;
const auto *decl = findProgramDecl(ns, path)->to<IR::Type_Declaration>();
BUG_CHECK(decl, "Not a type: %1%", path);
return decl;
}

const IR::Expression *ProgramInfo::createTargetUninitialized(const IR::Type *type,
bool forceTaint) const {
if (forceTaint) {
Expand Down
12 changes: 0 additions & 12 deletions backends/p4tools/modules/testgen/core/program_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,6 @@ class ProgramInfo : public ICastable {
// @returns the width of the parser error for this specific target.
[[nodiscard]] virtual const IR::Type_Bits *getParserErrorType() const = 0;

/// Looks up a declaration from a path. A BUG occurs if no declaration is found.
static const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns,
const IR::Path *path);

/// Looks up a declaration from a path expression. A BUG occurs if no declaration is found.
static const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns,
const IR::PathExpression *pathExpr);

/// Resolves a Type_Name in the top-level namespace.
static const IR::Type_Declaration *resolveProgramType(const IR::IGeneralNamespace *ns,
const IR::Type_Name *type);

/// @returns the canonical name of the program block that is passed in.
/// Throws a BUG, if the name can not be found.
[[nodiscard]] cstring getCanonicalBlockName(cstring programBlockName) const;
Expand Down
46 changes: 0 additions & 46 deletions backends/p4tools/modules/testgen/lib/util.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions backends/p4tools/modules/testgen/lib/util.h

This file was deleted.

2 changes: 1 addition & 1 deletion backends/p4tools/modules/testgen/targets/bmv2/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
#include "lib/exceptions.h"
#include "lib/ordered_map.h"

#include "backends/p4tools/common/lib/util.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"
#include "backends/p4tools/modules/testgen/lib/execution_state.h"
#include "backends/p4tools/modules/testgen/lib/util.h"
#include "backends/p4tools/modules/testgen/targets/bmv2/cmd_stepper.h"
#include "backends/p4tools/modules/testgen/targets/bmv2/constants.h"
#include "backends/p4tools/modules/testgen/targets/bmv2/expr_stepper.h"
Expand Down
2 changes: 1 addition & 1 deletion backends/p4tools/modules/testgen/targets/ebpf/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#include "lib/exceptions.h"
#include "lib/ordered_map.h"

#include "backends/p4tools/common/lib/util.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"
#include "backends/p4tools/modules/testgen/lib/execution_state.h"
#include "backends/p4tools/modules/testgen/lib/util.h"
#include "backends/p4tools/modules/testgen/options.h"
#include "backends/p4tools/modules/testgen/targets/ebpf/cmd_stepper.h"
#include "backends/p4tools/modules/testgen/targets/ebpf/expr_stepper.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "lib/cstring.h"
#include "lib/null.h"

#include "backends/p4tools/common/lib/util.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"
Expand Down
2 changes: 1 addition & 1 deletion backends/p4tools/modules/testgen/targets/pna/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#include "lib/exceptions.h"
#include "lib/ordered_map.h"

#include "backends/p4tools/common/lib/util.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"
#include "backends/p4tools/modules/testgen/lib/execution_state.h"
#include "backends/p4tools/modules/testgen/lib/util.h"
#include "backends/p4tools/modules/testgen/targets/pna/dpdk/cmd_stepper.h"
#include "backends/p4tools/modules/testgen/targets/pna/dpdk/expr_stepper.h"
#include "backends/p4tools/modules/testgen/targets/pna/dpdk/program_info.h"
Expand Down

0 comments on commit 08c7fd0

Please sign in to comment.