Skip to content

Commit

Permalink
Enable generation of pointer variables by eBPF codeGen (#3131)
Browse files Browse the repository at this point in the history
Co-authored-by: Mateusz Kossakowski <[email protected]>
Co-authored-by: Jan Palimąka <[email protected]>
  • Loading branch information
3 people authored Mar 15, 2022
1 parent ad1376e commit 319cb3e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
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

0 comments on commit 319cb3e

Please sign in to comment.