oxidecomputer / p4

A P4 compiler
Mozilla Public License 2.0
111 stars 5 forks source link

use qualified table names everywhere #11

Closed rcgoodfellow closed 2 years ago

rcgoodfellow commented 2 years ago

Fixes #8

The Fix

This fixes the problems described in #8 by making sure we are using qualified table names everywhere in generated rust code.

Any time a table is referenced, it is qualified relative to the control it is being referenced in. For example if a table muffin lives in the control baz, which is instantiated in control bar which is instantiated in control foo. Then

This disambiguates tables that have the same name, or control blocks that are instantiated multiple times.

The dot-delimited names above are used for the string-based table identifiers in the Pipeline trait. For Rust code generation purposes, underscore-delimited names are used.

Follow Ups

This fix is primarily necessary because the compiler currently constructs pipeline objects such that all tables are constructed up front and passed into control blocks as parameters. This makes it straightforward to implement a control API that modifies all tables. However, it does lose some of the hierarchical structuring intrinsic to P4 programs. It's possible that generated Rust could benefit from a more hierarchical organization.

Doing table name qualification was rather awkward and feels very bolted on. Most of the complexity was shouldered by the code generation stage, which does not seem right. Seems like we're missing intermediate data structures similar to the HLIR data structures that would make code generation a more straightforward process instead of taking on the role of calculating qualified names by traversing controller chains all over the place.