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

ternary type checking limitation #370

Open
pataei opened this issue Oct 14, 2022 · 1 comment
Open

ternary type checking limitation #370

pataei opened this issue Oct 14, 2022 · 1 comment
Labels
typechecker unsupported vs.P4 Petr4 limitation compared to P4

Comments

@pataei
Copy link
Collaborator

pataei commented Oct 14, 2022

Petr4 doesn't support the case where both sub-expressions of e1 ? e2 : e3 have the infinite precision integer type.
Code can be found in checker.ml:

type_ternary env ctx cond tru fls : Prog.Expression.t =
  let cond_typed = type_expression env ctx cond in
  let open Expression in 
  assert_bool (tags cond) cond_typed.typ |> ignore;
  let fls_typed = type_expression env ctx fls in
  let tru_typed = type_expression env ctx tru in
  assert_same_type env (tags tru) (tags fls) tru_typed.typ fls_typed.typ |> ignore;
  match tru_typed.typ with
  | Type.Integer ->
     (* TODO this is allowed if cond is compile-time known *)
     failwith "Conditional expressions may not have arbitrary width integer type"
  | t ->
     { expr = Ternary { cond = cond_typed; tru = tru_typed; fls = fls_typed; tags = Info.dummy };
       typ = t;
       dir = Directionless;
       tags = Info.dummy}

However, P4 spec states that:
"The first sub-expression e1 must have Boolean type (i.e. “bit” or "bit<1>"), and the second and third sub-expressions must have the same type, which cannot both be infinite-precision integers unless the condition itself can be evaluated at compilation time. This restriction is designed to ensure that the width of the result of the conditional expression can be inferred statically at compile time."

@pataei pataei added typechecker unsupported vs.P4 Petr4 limitation compared to P4 labels Oct 14, 2022
@pataei
Copy link
Collaborator Author

pataei commented Oct 18, 2022

Simple solution: add compile-time-known check to the condition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typechecker unsupported vs.P4 Petr4 limitation compared to P4
Projects
None yet
Development

No branches or pull requests

1 participant