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

Enable generation of pointer variables by eBPF CodeGenInspector #3131

Merged
merged 1 commit into from
Mar 15, 2022
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
7 changes: 6 additions & 1 deletion backends/ebpf/codeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,12 @@ bool CodeGenInspector::preorder(const IR::Member* expression) {
auto ei = P4::EnumInstance::resolve(expression, typeMap);
if (ei == nullptr) {
visit(expression->expr);
builder->append(".");
auto pe = expression->expr->to<IR::PathExpression>();
if (pe != nullptr && isPointerVariable(pe->path->name.name)) {
builder->append("->");
} else {
builder->append(".");
}
}
builder->append(expression->member);
expressionPrecedence = prec;
Expand Down
15 changes: 15 additions & 0 deletions backends/ebpf/codeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class CodeGenInspector : public Inspector {
P4::ReferenceMap* refMap;
P4::TypeMap* typeMap;
std::map<const IR::Parameter*, const IR::Parameter*> substitution;
// asPointerVariables stores the list of string expressions that
// should be emitted as pointer variables.
std::set<cstring> asPointerVariables;

public:
int expressionPrecedence; /// precedence of current IR::Operation
Expand All @@ -65,6 +68,18 @@ class CodeGenInspector : public Inspector {
substitute(s.first, s.second);
}

void useAsPointerVariable(cstring name) {
this->asPointerVariables.insert(name);
}
void copyPointerVariables(CodeGenInspector *other) {
for (auto s : other->asPointerVariables) {
this->asPointerVariables.insert(s);
}
}
bool isPointerVariable(cstring name) {
return asPointerVariables.count(name) > 0;
}

bool notSupported(const IR::Expression* expression)
{ ::error(ErrorType::ERR_UNSUPPORTED,
"%1%: not yet implemented", expression); return false; }
Expand Down
3 changes: 2 additions & 1 deletion backends/ebpf/ebpfControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@ void EBPFControl::emitDeclaration(CodeBuilder* builder, const IR::Declaration* d
auto vd = decl->to<IR::Declaration_Variable>();
auto etype = EBPFTypeFactory::instance->create(vd->type);
builder->emitIndent();
etype->declareInit(builder, vd->name, false);
bool isPointer = codeGen->isPointerVariable(decl->name.name);
etype->declareInit(builder, vd->name, isPointer);
builder->endOfStatement(true);
BUG_CHECK(vd->initializer == nullptr,
"%1%: declarations with initializers not supported", decl);
Expand Down