Skip to content

[WiP] Add an P4HIR-to-P4 syntax exporter.#168

Draft
fruffy wants to merge 6 commits intomainfrom
fruffy/top4
Draft

[WiP] Add an P4HIR-to-P4 syntax exporter.#168
fruffy wants to merge 6 commits intomainfrom
fruffy/top4

Conversation

@fruffy
Copy link
Copy Markdown
Collaborator

@fruffy fruffy commented May 4, 2025

Not ready yet for review. Currently intended to check CI.

This PR adds a utility to export P4HIR generated by the translate pass back to P4. The purpose is threefold. First, check that the current P4HIR representation is semantically equivalent to the original P4 source program and retains all the information needed for a valid P4 program. Second, provide a utility similar to ToP4, which can export P4 code for manual inspection and as reference. Third, once more front ends are available, serve as a transpiler back end to convert a particular source language (C/Python...) to P4.

The current approach to the exporter is as follows.

  1. First, collect all complex types (headers, enums, errors) in the program and declare them.
  2. Iterate over all top-level ops (externs, functions, actions, controls, parsers, packages) in the module and declare/define them as needed.

For each block that corresponds to a P4 block we hoist all variable declarations. All other Ops are handled in order.

We currently use two checks to ensure that this pass works. First, we use references files for each test P4 program and we compare against these references. That also makes reviewing changes easier. Second, we feed the generated program to p4test to make sure it compiles. This ensures that we always generate a valid P4 program with this exporter.

Tasks left.

  • Figure out a way to correctly declare controls and parsers. Currently we are missing type and direction information for parameters. As long as this feature is missing we can not export real programs.
  • Method calls are currently scope operators with several operators. This can be difficult to convert back into a method call. In particular, when these are part of a table action list.
  • Make sure to emit annotations everywhere where possible. Including types.
  • Comments are all over the place. Needs to be cleaned up.
  • General clean up (smaller function, remove unnecessary code, etc).
  • Clean up error reporting.
  • Biggest TODO: Make sure all current example programs pass.

Signed-off-by: fruffy <fruffy@nyu.edu>
Signed-off-by: fruffy <fruffy@nyu.edu>
Signed-off-by: fruffy <fruffy@nyu.edu>
Signed-off-by: fruffy <fruffy@nyu.edu>
Signed-off-by: fruffy <fruffy@nyu.edu>
Signed-off-by: fruffy <fruffy@nyu.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant