verified-network-toolchain / petr4

Petr4: Formal Semantics for P4
Apache License 2.0
74 stars 20 forks source link

Expression typing rules + types typing rules based on P4 spec #373

Closed pataei closed 1 year ago

pataei commented 1 year ago

This is an ongoing PR (it's not ready for review yet). This PR contains the formalization of expression typing rules based on P4 spec and a syntactic categorization of types.

Notation

We should agree on the judgment forms:

Syntactic categorization of types

I considered multiple syntax categories for major types:

Keep the following points in mind:

Furthermore, there are two other syntactic categories for argument types and function return types.

Well-formed types

Type equality/unification

Expression typing rules

The issues regarding typing of expressions are in order of which P4 spec discusses expressions.

Enums

Ternary/conditional

Unary operation

Binary operations

Bitstring access/slice:

Questions to answer:

Reminder: a slice expression looks like exp[L:R]. E.g., is the following slice expression well-typed?

int x = 8;
enum int<x> dummy {
  d1 = 8s1,
  d2 = 8s2
}

typedef bit<8> u8;
enum u8 dummmy {
  d1 = 8w1,
  d2 = 8w2
}

bit<16> y;
y[dummy.d1:dummmy.d1]

P4 spec (v1.2.3) states: Extraction of a set of contiguous bits, also known as a slice, denoted by [L:R], where L and R must be expressions that evaluate to non-negative compile-time known values, and L >= R must be true. The types of L and R (which do not need to be identical) must be one of the following:

Types that have an equality instance (fyi for parisa)

Right now, the formalization only has equality for expressions of types (this list might get longer as I go through P4 spec):

However, Petr4 has equality for expressions of types in addition to the ones above:

Implicit cast insertion

Explicit cast

Declaration typing

In parallel to formalizing expression typing, I'm writing declaration typing since declared types are used by expressions.

Enums

Both of these are explained in issue #383.

pataei commented 1 year ago

This PR isn't done yet but I want Harim to have the latest version.