Skip to content

Commit

Permalink
Minot tweaks
Browse files Browse the repository at this point in the history
- Expression::toString() generate actual expression strings
- LocalCopyProp typechecking arg optional
- json factor common code
  • Loading branch information
Chris Dodd committed Aug 14, 2023
1 parent a73a7fc commit dc72f90
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
8 changes: 7 additions & 1 deletion ir/expression.def
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ abstract Operation_Unary : Operation {
if (!srcInfo && expr) srcInfo = expr->srcInfo;
if (type->is<Type::Unknown>() && expr) type = expr->type; }
precedence = DBPrint::Prec_Prefix;
toString{ return getStringOp() + expr->toString(); }
}

class Neg : Operation_Unary {
Expand All @@ -37,6 +38,11 @@ abstract Operation_Binary : Operation {
if (!srcInfo && left && right) srcInfo = left->srcInfo + right->srcInfo;
if (type->is<Type::Unknown>() && left && right && left->type == right->type)
type = left->type; }
toString {
std::stringstream tmp;
tmp << DBPrint::Prec_Low << *this;
return tmp.str();
}
}

abstract Operation_Ternary : Operation {
Expand Down Expand Up @@ -374,7 +380,7 @@ class Cast : Operation_Unary {
/// type, and 'type' will only be updated later when type inferencing occurs
precedence = DBPrint::Prec_Prefix;
stringOp = "(cast)";
toString{ return "cast"; }
toString{ return "(" + destType->toString() + ")" + expr->toString(); }
validate{ BUG_CHECK(!destType->is<Type_Unknown>(), "%1%: Cannot cast to unknown type", this); }
}

Expand Down
24 changes: 1 addition & 23 deletions ir/json_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,29 +195,7 @@ class JSONGenerator {

void generate(cstring v) {
if (v) {
out << "\"";
for (auto ch : v) {
switch (ch) {
case '\n':
out << "\\n";
break;
case '\r':
out << "\\r";
break;
case '\t':
out << "\\t";
break;
case '\"':
out << "\\\"";
break;
case '\\':
out << "\\\\";
break;
default:
out << ch;
}
}
out << "\"";
out << "\"" << v.escapeJson() << "\"";
} else {
out << "null";
}
Expand Down
3 changes: 3 additions & 0 deletions midend/local_copyprop.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ class LocalCopyPropagation : public PassManager {
passes.push_back(typeChecking);
passes.push_back(new DoLocalCopyPropagation(refMap, typeMap, policy, elimUnusedTables));
}
LocalCopyPropagation(ReferenceMap *refMap, TypeMap *typeMap,
std::function<bool(const Context *, const IR::Expression *)> policy)
: LocalCopyPropagation(refMap, typeMap, nullptr, policy) {}
};

} // namespace P4
Expand Down

0 comments on commit dc72f90

Please sign in to comment.