Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #516 #522

Merged
merged 3 commits into from
Apr 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backends/p4test/run-p4-sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def process_file(options, argv):
os.makedirs(expected_dirname)

# We rely on the fact that these keys are in alphabetical order.
rename = { "FrontEnd_12_SimplifyControlFlow": "first",
rename = { "FrontEnd_13_SimplifyControlFlow": "first",
"FrontEndLast": "frontend",
"MidEndLast": "midend" }

Expand Down
46 changes: 27 additions & 19 deletions frontends/p4/fromv1.0/programStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,18 @@ const IR::Expression* ProgramStructure::paramReference(const IR::Parameter* para
return result;
}

const IR::AssignmentStatement* ProgramStructure::assign(
Util::SourceInfo srcInfo, const IR::Expression* left,
const IR::Expression* right, const IR::Type* type) {
const IR::Expression* cast;
if (type != nullptr)
cast = new IR::Cast(type, right);
else
cast = right;
auto result = new IR::AssignmentStatement(srcInfo, left, cast);
return result;
}

const IR::Statement* ProgramStructure::convertParserStatement(const IR::Expression* expr) {
ExpressionConverter conv(this);

Expand Down Expand Up @@ -304,8 +316,7 @@ const IR::Statement* ProgramStructure::convertParserStatement(const IR::Expressi

auto dest = conv.convert(primitive->operands.at(0));
auto src = conv.convert(primitive->operands.at(1));
auto result = new IR::AssignmentStatement(primitive->srcInfo, dest, src);
return result;
return assign(primitive->srcInfo, dest, src, primitive->operands.at(0)->type);
}
}
BUG("Unhandled expression %1%", expr);
Expand Down Expand Up @@ -831,7 +842,7 @@ const IR::Statement* ProgramStructure::sliceAssign(
auto l = new IR::Constant(range.lowIndex);
left = new IR::Slice(left->srcInfo, left, h, l);
right = new IR::Slice(right->srcInfo, right, h, l);
return new IR::AssignmentStatement(srcInfo, left, right);
return assign(srcInfo, left, right, nullptr);
}
// else value is too complex for a slice
}
Expand All @@ -844,7 +855,7 @@ const IR::Statement* ProgramStructure::sliceAssign(
auto op1 = new IR::BAnd(left->srcInfo, left, new IR::Cmpl(mask));
auto op2 = new IR::BAnd(right->srcInfo, right, mask);
auto result = new IR::BOr(mask->srcInfo, op1, op2);
return new IR::AssignmentStatement(srcInfo, left, result);
return assign(srcInfo, left, result, type);
}

#define OPS_CK(primitive, n) BUG_CHECK((primitive)->operands.size() == n, \
Expand Down Expand Up @@ -884,7 +895,7 @@ const IR::Statement* ProgramStructure::convertPrimitive(const IR::Primitive* pri
if (primitive->operands.size() == 2) {
auto left = conv.convert(primitive->operands.at(0));
auto right = conv.convert(primitive->operands.at(1));
return new IR::AssignmentStatement(primitive->srcInfo, left, right);
return assign(primitive->srcInfo, left, right, primitive->operands.at(0)->type);
} else if (primitive->operands.size() == 3) {
auto left = conv.convert(primitive->operands.at(0));
auto right = conv.convert(primitive->operands.at(1));
Expand Down Expand Up @@ -946,17 +957,15 @@ const IR::Statement* ProgramStructure::convertPrimitive(const IR::Primitive* pri
op = new IR::Shl(dest->srcInfo, left, right);
else if (primitive->name == "shift_right")
op = new IR::Shr(dest->srcInfo, left, right);
auto result = new IR::AssignmentStatement(primitive->srcInfo, dest, op);
return result;
return assign(primitive->srcInfo, dest, op, primitive->operands.at(0)->type);
} else if (primitive->name == "bit_not") {
OPS_CK(primitive, 2);
auto dest = conv.convert(primitive->operands.at(0));
auto right = conv.convert(primitive->operands.at(1));
IR::Expression* op = nullptr;
if (primitive->name == "bit_not")
op = new IR::Cmpl(right);
auto result = new IR::AssignmentStatement(primitive->srcInfo, dest, op);
return result;
return assign(primitive->srcInfo, dest, op, primitive->operands.at(0)->type);
} else if (primitive->name == "no_op") {
return new IR::EmptyStatement();
} else if (primitive->name == "add_to_field" || primitive->name == "subtract_from_field") {
Expand All @@ -970,7 +979,7 @@ const IR::Statement* ProgramStructure::convertPrimitive(const IR::Primitive* pri
op = new IR::Add(primitive->srcInfo, left, right);
else
op = new IR::Sub(primitive->srcInfo, left, right);
return new IR::AssignmentStatement(primitive->srcInfo, left2, op);
return assign(primitive->srcInfo, left2, op, primitive->operands.at(0)->type);
} else if (primitive->name == "remove_header") {
OPS_CK(primitive, 1);
auto hdr = conv.convert(primitive->operands.at(0));
Expand Down Expand Up @@ -1048,16 +1057,15 @@ const IR::Statement* ProgramStructure::convertPrimitive(const IR::Primitive* pri
auto mc = new IR::MethodCallExpression(primitive->srcInfo, random, args);
auto call = new IR::MethodCallStatement(primitive->srcInfo, mc);
auto block = new IR::BlockStatement;
const IR::Statement* assign;
if (mask != nullptr) {
assign = sliceAssign(primitive->srcInfo, field,
const IR::Statement* assgn;
if (mask != nullptr)
assgn = sliceAssign(primitive->srcInfo, field,
new IR::PathExpression(IR::ID(tmpvar)), mask);
} else {
assign = new IR::AssignmentStatement(field, new IR::PathExpression(tmpvar));
}
else
assgn = new IR::AssignmentStatement(field, new IR::PathExpression(tmpvar));
block->push_back(decl);
block->push_back(call);
block->push_back(assign);
block->push_back(assgn);
return block;
} else if (primitive->name == "modify_field_rng_uniform") {
BUG_CHECK(primitive->operands.size() == 3, "Expected 3 operands for %1%", primitive);
Expand Down Expand Up @@ -1182,7 +1190,7 @@ const IR::Statement* ProgramStructure::convertPrimitive(const IR::Primitive* pri
if (!cond->type->is<IR::Type::Boolean>())
cond = new IR::Neq(cond, new IR::Constant(0));
src = new IR::Mux(primitive->srcInfo, cond, src, dest);
return new IR::AssignmentStatement(primitive->srcInfo, dest, src);
return assign(primitive->srcInfo, dest, src, primitive->operands.at(0)->type);
} else if (primitive->name == "generate_digest") {
OPS_CK(primitive, 2);
auto args = new IR::Vector<IR::Expression>();
Expand Down Expand Up @@ -1273,7 +1281,7 @@ const IR::Statement* ProgramStructure::convertPrimitive(const IR::Primitive* pri
auto lo = conv.convert(primitive->operands.at(2));
auto shift = conv.convert(primitive->operands.at(3));
auto src = new IR::Shr(new IR::Concat(hi, lo), shift);
return new IR::AssignmentStatement(primitive->srcInfo, dest, src);
return assign(primitive->srcInfo, dest, src, primitive->operands.at(0)->type);
}

// If everything else failed maybe we are invoking an action
Expand Down
2 changes: 2 additions & 0 deletions frontends/p4/fromv1.0/programStructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ class ProgramStructure {
void createMain();

public:
const IR::AssignmentStatement* assign(Util::SourceInfo srcInfo, const IR::Expression* left,
const IR::Expression* right, const IR::Type* type);
const IR::Expression* paramReference(const IR::Parameter* param);
void tablesReferred(const IR::V1Control* control, std::vector<const IR::V1Table*> &out);
bool isHeader(const IR::ConcreteHeaderRef* nhr) const;
Expand Down
10 changes: 0 additions & 10 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,16 +643,6 @@ TypeInference::assignment(const IR::Node* errorPosition, const IR::Type* destTyp
if (initType == destType)
return sourceExpression;

if (canCastBetween(destType, initType)) {
LOG2("Inserting cast in " << sourceExpression);
bool isConst = isCompileTimeConstant(sourceExpression);
sourceExpression = new IR::Cast(sourceExpression->srcInfo, destType, sourceExpression);
setType(sourceExpression, destType);
if (isConst)
setCompileTimeConstant(sourceExpression);
return sourceExpression;
}

auto tvs = unify(errorPosition, destType, initType, true);
if (tvs == nullptr)
// error already signalled
Expand Down
8 changes: 4 additions & 4 deletions testdata/p4_14_samples_outputs/01-BigMatch.p4
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
@name(".nop") action nop() {
}
@name(".set_f1") action set_f1(bit<1024> f1) {
meta.ing_metadata.f1 = f1;
meta.ing_metadata.f1 = (bit<1024>)f1;
}
@name(".set_f2") action set_f2(bit<512> f2) {
meta.ing_metadata.f2 = f2;
meta.ing_metadata.f2 = (bit<512>)f2;
}
@name(".set_f3") action set_f3(bit<256> f3) {
meta.ing_metadata.f3 = f3;
meta.ing_metadata.f3 = (bit<256>)f3;
}
@name(".set_f4") action set_f4(bit<128> f4) {
meta.ing_metadata.f4 = f4;
meta.ing_metadata.f4 = (bit<128>)f4;
}
@name("i_t1") table i_t1 {
actions = {
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_14_samples_outputs/01-DeadMetadata1.p4
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t

control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
@name(".a1") action a1() {
meta.m.f1 = 32w1;
meta.m.f1 = (bit<32>)32w1;
}
@name("t1") table t1 {
actions = {
Expand Down
Loading