Skip to content

Commit

Permalink
Fix parser symbolic interpreter to evaluate StringLiteral
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Havranek <[email protected]>
  • Loading branch information
jhavrane committed Oct 3, 2024
1 parent 7dec522 commit b9eeb04
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 1 deletion.
38 changes: 37 additions & 1 deletion midend/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,33 @@ bool SymbolicInteger::equals(const SymbolicValue *other) const {
return true;
}

bool SymbolicString::merge(const SymbolicValue *other) {
BUG_CHECK(other->is<SymbolicString>(), "%1%: expected a string", other);
auto so = other->to<SymbolicString>();
auto saveState = state;
state = mergeState(so->state);
if (state == ValueState::Constant && string->value != so->string->value) {
state = ValueState::NotConstant;
string = nullptr;
}
return (state != saveState);
}

void SymbolicString::assign(const SymbolicValue *other) {
BUG_CHECK(other->is<SymbolicString>(), "%1%: expected a string", other);
auto so = other->to<SymbolicString>();
state = so->state;
string = so->string;
}

bool SymbolicString::equals(const SymbolicValue *other) const {
if (!other->is<SymbolicString>()) return false;
auto ab = other->to<SymbolicString>();
if (state != ab->state) return false;
if (isKnown()) return string->value == ab->string->value;
return true;
}

bool SymbolicVarbit::merge(const SymbolicValue *other) {
BUG_CHECK(other->is<SymbolicVarbit>(), "%1%: expected a varbit", other);
auto vo = other->to<SymbolicVarbit>();
Expand Down Expand Up @@ -657,6 +684,8 @@ const IR::Expression *getConstant(const ScalarValue *constant) {
return new IR::BoolLiteral(IR::Type::Boolean::get(), constant->to<SymbolicBool>()->value);
} else if (constant->is<SymbolicInteger>()) {
return constant->to<SymbolicInteger>()->constant;
} else if (constant->is<SymbolicString>()) {
return constant->to<SymbolicString>()->string;
}
BUG("Unimplemented structure for expression evaluation %1%", constant);
}
Expand All @@ -669,8 +698,11 @@ void ExpressionEvaluator::checkResult(const IR::Expression *expression,
} else if (result->is<IR::BoolLiteral>()) {
set(expression, new SymbolicBool(result->to<IR::BoolLiteral>()->value));
return;
} else if (result->is<IR::StringLiteral>()) {
set(expression, new SymbolicString(result->to<IR::StringLiteral>()));
return;
}
BUG("%1% : expected a constant/bool literal", result);
BUG("%1% : expected a constant/bool/string literal", result);
}

void ExpressionEvaluator::setNonConstant(const IR::Expression *expression) {
Expand Down Expand Up @@ -829,6 +861,10 @@ void ExpressionEvaluator::postorder(const IR::Constant *expression) {
set(expression, new SymbolicInteger(expression));
}

void ExpressionEvaluator::postorder(const IR::StringLiteral *expression) {
set(expression, new SymbolicString(expression));
}

void ExpressionEvaluator::postorder(const IR::ListExpression *expression) {
auto type = typeMap->getType(expression, true);
auto result = new SymbolicTuple(type->to<IR::Type_Tuple>());
Expand Down
30 changes: 30 additions & 0 deletions midend/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class ExpressionEvaluator : public Inspector {

void postorder(const IR::Constant *expression) override;
void postorder(const IR::BoolLiteral *expression) override;
void postorder(const IR::StringLiteral *expression) override;
void postorder(const IR::Operation_Ternary *expression) override;
void postorder(const IR::Operation_Binary *expression) override;
void postorder(const IR::Operation_Relation *expression) override;
Expand Down Expand Up @@ -346,6 +347,35 @@ class SymbolicInteger final : public ScalarValue {
DECLARE_TYPEINFO(SymbolicInteger, ScalarValue);
};

class SymbolicString final : public ScalarValue {
public:
const IR::StringLiteral *string;
explicit SymbolicString(const IR::Type_String *type)
: ScalarValue(ScalarValue::ValueState::Uninitialized, type), string(nullptr) {}
SymbolicString(ScalarValue::ValueState state, const IR::Type_String *type)
: ScalarValue(state, type), string(nullptr) {}
explicit SymbolicString(const IR::StringLiteral *string)
: ScalarValue(ScalarValue::ValueState::Constant, string->type), string(string) {
CHECK_NULL(string);
}
SymbolicString(const SymbolicString &other) = default;
void dbprint(std::ostream &out) const override {
ScalarValue::dbprint(out);
if (isKnown()) out << string->value;
}
SymbolicValue *clone() const override {
auto result = new SymbolicString(type->to<IR::Type_String>());
result->state = state;
result->string = string;
return result;
}
void assign(const SymbolicValue *other) override;
bool merge(const SymbolicValue *other) override;
bool equals(const SymbolicValue *other) const override;

DECLARE_TYPEINFO(SymbolicString, ScalarValue);
};

class SymbolicVarbit final : public ScalarValue {
public:
explicit SymbolicVarbit(const IR::Type_Varbits *type)
Expand Down
7 changes: 7 additions & 0 deletions test/gtest/parser_unroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,11 @@ TEST_F(P4CParserUnroll, header_union) {
ASSERT_EQ(parsers.first->states.size(), parsers.second->states.size());
}

TEST_F(P4CParserUnroll, stringTypes) {
auto parsers = loadExample("issue4932.p4");
ASSERT_TRUE(parsers.first);
ASSERT_TRUE(parsers.second);
ASSERT_EQ(parsers.first->states.size(), parsers.second->states.size());
}

} // namespace P4::Test
30 changes: 30 additions & 0 deletions testdata/p4_16_samples/issue4932.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2024 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

extern void log(string msg);

parser SimpleParser();
package SimpleArch(SimpleParser p);

parser ParserImpl()
{
state start {
log("Log message" ++ " text");
transition accept;
}
}

SimpleArch(ParserImpl()) main;
11 changes: 11 additions & 0 deletions testdata/p4_16_samples_outputs/issue4932-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern void log(string msg);
parser SimpleParser();
package SimpleArch(SimpleParser p);
parser ParserImpl() {
state start {
log("Log message text");
transition accept;
}
}

SimpleArch(ParserImpl()) main;
11 changes: 11 additions & 0 deletions testdata/p4_16_samples_outputs/issue4932-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern void log(string msg);
parser SimpleParser();
package SimpleArch(SimpleParser p);
parser ParserImpl() {
state start {
log("Log message text");
transition accept;
}
}

SimpleArch(ParserImpl()) main;
11 changes: 11 additions & 0 deletions testdata/p4_16_samples_outputs/issue4932-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern void log(string msg);
parser SimpleParser();
package SimpleArch(SimpleParser p);
parser ParserImpl() {
state start {
log("Log message text");
transition accept;
}
}

SimpleArch(ParserImpl()) main;
11 changes: 11 additions & 0 deletions testdata/p4_16_samples_outputs/issue4932.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern void log(string msg);
parser SimpleParser();
package SimpleArch(SimpleParser p);
parser ParserImpl() {
state start {
log("Log message" ++ " text");
transition accept;
}
}

SimpleArch(ParserImpl()) main;
Empty file.

0 comments on commit b9eeb04

Please sign in to comment.