Skip to content

Commit

Permalink
Add a getStringLiteral utility function.
Browse files Browse the repository at this point in the history
  • Loading branch information
fruffy committed Apr 17, 2024
1 parent 5848a28 commit f058dab
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 26 deletions.
3 changes: 1 addition & 2 deletions backends/p4tools/modules/testgen/core/program_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ void ProgramInfo::produceCopyInOutCall(const IR::Parameter *param, size_t paramI
}
const auto *archPath = new IR::PathExpression(paramType, new IR::Path(archRef));
const auto *paramRef = new IR::PathExpression(paramType, new IR::Path(param->name));
const auto *paramDir =
new IR::StringLiteral(IR::Type_String::get(), directionToString(param->direction));
const auto *paramDir = IR::getStringLiteral(directionToString(param->direction));
if (copyIns != nullptr) {
// This mimicks the copy-in from the architecture environment.
const auto *copyInCall = new IR::MethodCallStatement(Utils::generateInternalMethodCall(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ bool ExprStepper::preorder(const IR::PathExpression *pathExpression) {
// If the path expression is a Type_MatchKind, convert it to a StringLiteral.
if (pathExpression->type->is<IR::Type_MatchKind>()) {
state.replaceTopBody(Continuation::Return(
new IR::StringLiteral(IR::Type_MatchKind::get(), pathExpression->path->name)));
IR::getStringLiteral(pathExpression->path->name, IR::Type_MatchKind::get())));
result->emplace_back(state);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ const IR::Expression *TableStepper::computeHit(TableMatchMap *matches) {

const IR::StringLiteral *TableStepper::getTableActionString(
const IR::MethodCallExpression *actionCall) {
cstring actionName = actionCall->method->toString();
return new IR::StringLiteral(IR::Type_String::get(), actionName);
return IR::getStringLiteral(actionCall->method->toString());
}

const IR::Expression *TableStepper::evalTableConstEntries() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class TableStepper {

/// Sets the action taken by the given table. The arguments in the given MethodCallExpression
/// are assumed to be symbolic values.
const IR::StringLiteral *getTableActionString(const IR::MethodCallExpression *actionCall);
static const IR::StringLiteral *getTableActionString(
const IR::MethodCallExpression *actionCall);

/// A helper function to iteratively resolve table keys into symbolic values.
/// This function returns false, if no key needs to be resolved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ std::vector<Continuation::Command> Bmv2V1ModelProgramInfo::processDeclaration(
std::vector<Continuation::Command> cmds;
// Copy-in.
const auto *copyInCall = new IR::MethodCallStatement(Utils::generateInternalMethodCall(
"copy_in", {new IR::StringLiteral(typeDecl->name)}, IR::Type_Void::get(),
"copy_in", {IR::getStringLiteral(typeDecl->name)}, IR::Type_Void::get(),
new IR::ParameterList(
{new IR::Parameter("blockRef", IR::Direction::In, IR::Type_Unknown::get())})));
cmds.emplace_back(copyInCall);
// Insert the actual pipeline.
cmds.emplace_back(typeDecl);
// Copy-out.
const auto *copyOutCall = new IR::MethodCallStatement(Utils::generateInternalMethodCall(
"copy_out", {new IR::StringLiteral(typeDecl->name)}, IR::Type_Void::get(),
"copy_out", {IR::getStringLiteral(typeDecl->name)}, IR::Type_Void::get(),
new IR::ParameterList(
{new IR::Parameter("blockRef", IR::Direction::In, IR::Type_Unknown::get())})));
cmds.emplace_back(copyOutCall);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ std::vector<Continuation::Command> EBPFProgramInfo::processDeclaration(

// Copy-in.
const auto *copyInCall = new IR::MethodCallStatement(Utils::generateInternalMethodCall(
"copy_in", {new IR::StringLiteral(typeDecl->name)}, IR::Type_Void::get(),
"copy_in", {IR::getStringLiteral(typeDecl->name)}, IR::Type_Void::get(),
new IR::ParameterList(
{new IR::Parameter("blockRef", IR::Direction::In, IR::Type_Unknown::get())})));
cmds.emplace_back(copyInCall);
// Insert the actual pipeline.
cmds.emplace_back(typeDecl);
// Copy-out.
const auto *copyOutCall = new IR::MethodCallStatement(Utils::generateInternalMethodCall(
"copy_out", {new IR::StringLiteral(typeDecl->name)}, IR::Type_Void::get(),
"copy_out", {IR::getStringLiteral(typeDecl->name)}, IR::Type_Void::get(),
new IR::ParameterList(
{new IR::Parameter("blockRef", IR::Direction::In, IR::Type_Unknown::get())})));
cmds.emplace_back(copyOutCall);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "backends/p4tools/common/lib/util.h"
#include "ir/id.h"
#include "ir/ir.h"
#include "ir/irutils.h"
#include "lib/cstring.h"
#include "lib/exceptions.h"

Expand Down Expand Up @@ -69,15 +70,15 @@ std::vector<Continuation::Command> PnaDpdkProgramInfo::processDeclaration(
std::vector<Continuation::Command> cmds;
// Copy-in.
const auto *copyInCall = new IR::MethodCallStatement(Utils::generateInternalMethodCall(
"copy_in", {new IR::StringLiteral(typeDecl->name)}, IR::Type_Void::get(),
"copy_in", {IR::getStringLiteral(typeDecl->name)}, IR::Type_Void::get(),
new IR::ParameterList(
{new IR::Parameter("blockRef", IR::Direction::In, IR::Type_Unknown::get())})));
cmds.emplace_back(copyInCall);
// Insert the actual pipeline.
cmds.emplace_back(typeDecl);
// Copy-out.
const auto *copyOutCall = new IR::MethodCallStatement(Utils::generateInternalMethodCall(
"copy_out", {new IR::StringLiteral(typeDecl->name)}, IR::Type_Void::get(),
"copy_out", {IR::getStringLiteral(typeDecl->name)}, IR::Type_Void::get(),
new IR::ParameterList(
{new IR::Parameter("blockRef", IR::Direction::In, IR::Type_Unknown::get())})));
cmds.emplace_back(copyOutCall);
Expand Down
26 changes: 12 additions & 14 deletions backends/p4tools/modules/testgen/test/z3-solver/constraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,51 +84,49 @@ TEST_F(Z3SolverSatisfiabilityChecks, Strings) {
const auto *fooVar = P4Tools::ToolsVariables::getSymbolicVariable(stringType, "foo");
const auto *barVar = P4Tools::ToolsVariables::getSymbolicVariable(stringType, "bar");
{
auto *expression = new IR::Equ(new IR::StringLiteral(stringType, "dead"),
new IR::StringLiteral(stringType, "dead"));
auto *expression = new IR::Equ(IR::getStringLiteral("dead"), IR::getStringLiteral("dead"));
ConstraintVector inputExpression = {expression};
testCheckSat(inputExpression, true);
}
{
auto *expression = new IR::Equ(new IR::StringLiteral(stringType, "dead"),
new IR::StringLiteral(stringType, "beef"));
auto *expression = new IR::Equ(IR::getStringLiteral("dead"), IR::getStringLiteral("beef"));
ConstraintVector inputExpression = {expression};
testCheckSat(inputExpression, false);
}
{
auto *expression = new IR::Equ(fooVar, new IR::StringLiteral(stringType, "dead"));
auto *expression = new IR::Equ(fooVar, IR::getStringLiteral("dead"));
ConstraintVector inputExpression = {expression};
testCheckSat(inputExpression, true);
}
{
auto *expression = new IR::Equ(fooVar, new IR::StringLiteral(stringType, "dead"));
auto *constraint1 = new IR::Equ(fooVar, new IR::StringLiteral(stringType, "dead"));
auto *expression = new IR::Equ(fooVar, IR::getStringLiteral("dead"));
auto *constraint1 = new IR::Equ(fooVar, IR::getStringLiteral("dead"));
ConstraintVector inputExpression = {expression, constraint1};
testCheckSat(inputExpression, true);
}
{
auto *expression = new IR::Equ(fooVar, barVar);
auto *constraint1 = new IR::Equ(fooVar, new IR::StringLiteral(stringType, "dead"));
auto *constraint1 = new IR::Equ(fooVar, IR::getStringLiteral("dead"));
ConstraintVector inputExpression = {expression, constraint1};
testCheckSat(inputExpression, true);
}
{
auto *expression = new IR::Equ(fooVar, barVar);
auto *constraint1 = new IR::Equ(fooVar, new IR::StringLiteral(stringType, "dead"));
auto *constraint2 = new IR::Equ(barVar, new IR::StringLiteral(stringType, "dead"));
auto *constraint1 = new IR::Equ(fooVar, IR::getStringLiteral("dead"));
auto *constraint2 = new IR::Equ(barVar, IR::getStringLiteral("dead"));
ConstraintVector inputExpression = {expression, constraint1, constraint2};
testCheckSat(inputExpression, true);
}
{
auto *expression = new IR::Equ(fooVar, new IR::StringLiteral(stringType, "dead"));
auto *constraint1 = new IR::Equ(fooVar, new IR::StringLiteral(stringType, "beef"));
auto *expression = new IR::Equ(fooVar, IR::getStringLiteral("dead"));
auto *constraint1 = new IR::Equ(fooVar, IR::getStringLiteral("beef"));
ConstraintVector inputExpression = {expression, constraint1};
testCheckSat(inputExpression, false);
}
{
auto *expression = new IR::Equ(fooVar, barVar);
auto *constraint1 = new IR::Equ(fooVar, new IR::StringLiteral(stringType, "dead"));
auto *constraint2 = new IR::Equ(barVar, new IR::StringLiteral(stringType, "beef"));
auto *constraint1 = new IR::Equ(fooVar, IR::getStringLiteral("dead"));
auto *constraint2 = new IR::Equ(barVar, IR::getStringLiteral("beef"));
ConstraintVector inputExpression = {expression, constraint1, constraint2};
testCheckSat(inputExpression, false);
}
Expand Down
16 changes: 16 additions & 0 deletions ir/irutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ const BoolLiteral *getBoolLiteral(bool value, const Util::SourceInfo &srcInfo) {
return result;
}

const IR::StringLiteral *getStringLiteral(cstring value, const IR::Type *type,
const Util::SourceInfo &srcInfo) {
if (type == nullptr) {
type = Type::String::get();
}
// String literals are interned.
using key_t = std::pair<cstring, const IR::Type *>;
static std::map<key_t, const IR::StringLiteral *> STRINGS;

auto *&result = STRINGS[{value, type}];
if (result == nullptr) {
result = new IR::StringLiteral(srcInfo, type, value);
}
return result;
}

const IR::Constant *convertBoolLiteral(const IR::BoolLiteral *lit) {
return IR::getConstant(IR::getBitType(1), lit->value ? 1 : 0, lit->getSourceInfo());
}
Expand Down
5 changes: 5 additions & 0 deletions ir/irutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace IR {

// Forward-declare some IR classes that are used in function declarations.
class BoolLiteral;
class StringLiteral;
class Constant;
class Expression;
class BaseListExpression;
Expand Down Expand Up @@ -50,6 +51,10 @@ const Constant *getConstant(const Type *type, big_int v, const Util::SourceInfo
/// @returns a bool literal. The value is cached.
const BoolLiteral *getBoolLiteral(bool value, const Util::SourceInfo &srcInfo = {});

/// @returns a string literal. If @param type is nullptr, Type_String is used. The value is cached.
const StringLiteral *getStringLiteral(cstring value, const Type *type = nullptr,
const Util::SourceInfo &srcInfo = {});

/// @returns a constant with the maximum big_int value that can fit into this bit width.
/// Implicitly converts boolean types to a bit vector of width one with value 1.
const IR::Constant *getMaxValueConstant(const Type *t, const Util::SourceInfo &srcInfo = {});
Expand Down

0 comments on commit f058dab

Please sign in to comment.