Skip to content

Commit

Permalink
indirect action call plumbing
Browse files Browse the repository at this point in the history
  • Loading branch information
rcgoodfellow committed Sep 22, 2024
1 parent 2c3b80c commit 9fffa1a
Show file tree
Hide file tree
Showing 7 changed files with 427 additions and 106 deletions.
13 changes: 10 additions & 3 deletions codegen/htq/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use p4::{ast::ControlParameter, hlir::Hlir};

use crate::{
error::CodegenError, p4_type_to_htq_type, statement::emit_statement,
AsyncFlagAllocator, P4Context, RegisterAllocator,
AsyncFlagAllocator, P4Context, RegisterAllocator, TableContext,
};

pub(crate) fn emit_control_functions(
Expand All @@ -33,6 +33,7 @@ fn emit_control(
) -> Result<Vec<htq::ast::Function>, CodegenError> {
let mut result = Vec::default();
let mut psub = HashMap::<ControlParameter, Vec<Register>>::default();
let mut table_context = TableContext::default();

let mut parameters = Vec::new();
let mut apply_return_signature = Vec::new();
Expand All @@ -48,12 +49,12 @@ fn emit_control(
//variant
apply_return_signature.push(htq::ast::Type::Unsigned(16));

let args_size = control
let info = control
.resolve_lookup_result_args_size(&x.name, ast)
.ok_or(CodegenError::LookupResultArgSize(x.clone()))?;

apply_return_signature
.push(htq::ast::Type::Unsigned(args_size));
.push(htq::ast::Type::Unsigned(info.max_arg_size));

if x.ty.is_sync() {
//async flag
Expand All @@ -79,6 +80,7 @@ fn emit_control(
&apply_return_signature,
afa,
&mut psub,
&mut table_context,
)?);

for action in &control.actions {
Expand All @@ -91,6 +93,7 @@ fn emit_control(
&action_return_signature,
afa,
&mut psub,
&mut table_context,
)?);
}
Ok(result)
Expand All @@ -104,6 +107,7 @@ fn emit_control_apply(
return_signature: &[htq::ast::Type],
afa: &mut AsyncFlagAllocator,
psub: &mut HashMap<ControlParameter, Vec<Register>>,
table_context: &mut TableContext,
) -> Result<htq::ast::Function, CodegenError> {
let mut ra = RegisterAllocator::default();
let mut names = control.names();
Expand All @@ -127,6 +131,7 @@ fn emit_control_apply(
&mut ra,
afa,
psub,
table_context,
)?;
statements.extend(stmts);
blocks.extend(blks);
Expand Down Expand Up @@ -180,6 +185,7 @@ fn emit_control_action(
return_signature: &[htq::ast::Type],
afa: &mut AsyncFlagAllocator,
psub: &mut HashMap<ControlParameter, Vec<Register>>,
table_context: &mut TableContext,
) -> Result<htq::ast::Function, CodegenError> {
let mut ra = RegisterAllocator::default();
let mut names = control.names();
Expand Down Expand Up @@ -218,6 +224,7 @@ fn emit_control_action(
&mut ra,
afa,
psub,
table_context,
)?;
statements.extend(stmts);
blocks.extend(blks);
Expand Down
11 changes: 11 additions & 0 deletions codegen/htq/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use p4::{
};
use thiserror::Error;

use crate::{RegisterAllocator, TableContext};

#[derive(Error, Debug)]
pub enum FlagAllocationError {
#[error("flag overflow: count exceeds 128")]
Expand Down Expand Up @@ -100,6 +102,15 @@ pub enum CodegenError {

#[error("missing register for lvalue, this is a compiler bug \n{0:#?}\ncurrent registers: \n{1:#?}")]
MissingRegisterForLvalue(Lvalue, Vec<Register>),

#[error("table not found in context \nlvalue:\n{0:#?}\ncontext:\n{1:#?}")]
TableNotFoundInContext(Lvalue, TableContext),

#[error("indirect action call in parser for \n{0:#?}")]
IndirectActionCallInParser(Lvalue),

#[error("no register for parameter {0}\nregisters:\n{1:#?}")]
NoRegisterForParameter(String, RegisterAllocator),
}

#[derive(Error, Debug)]
Expand Down
Loading

0 comments on commit 9fffa1a

Please sign in to comment.