22#define P4MLIR_DIALECT_BMv2IR_BMv2IR_OPS_TD
33
44include "mlir/IR/OpBase.td"
5+ include "mlir/IR/SymbolInterfaces.td"
6+
57
68include "p4mlir/Dialect/BMv2IR/BMv2IR_Dialect.td"
79include "p4mlir/Dialect/BMv2IR/BMv2IR_Types.td"
10+ include "p4mlir/Dialect/BMv2IR/BMv2IR_Attrs.td"
11+ include "p4mlir/Dialect/BMv2IR/BMv2IR_OpInterfaces.td"
812
913//===----------------------------------------------------------------------===//
1014// Base BMv2IR operation definition.
@@ -13,4 +17,181 @@ include "p4mlir/Dialect/BMv2IR/BMv2IR_Types.td"
1317class BMv2IR_Op<string mnemonic, list<Trait> traits = []> :
1418 Op<BMv2IR_Dialect, mnemonic, traits>;
1519
20+ def BMv2IR_ParserOp : BMv2IR_Op<"parser", [
21+ Symbol, SymbolTable,
22+ SingleBlock, NoTerminator
23+ ]> {
24+ let summary = "P4 parser definition";
25+ let description = [{
26+ Represents a P4 parser with an initial state and a set of parse states.
27+
28+ Example:
29+ ```mlir
30+ bmv2.parser @my_parser init_state @start {
31+ // parse states here
32+ }
33+ ```
34+ }];
35+
36+ let arguments = (ins
37+ SymbolNameAttr:$sym_name,
38+ SymbolRefAttr:$init_state
39+ );
40+
41+ let regions = (region SizedRegion<1>:$body);
42+
43+ let assemblyFormat = [{
44+ $sym_name `init_state` $init_state $body attr-dict
45+ }];
46+ }
47+
48+ def BMv2IR_ParserStateOp : BMv2IR_Op<"state",
49+ [HasParent<"ParserOp">,
50+ Symbol, NoTerminator]> {
51+ let arguments = (ins
52+ SymbolNameAttr:$sym_name
53+ );
54+ let description = [{
55+ Represents a BMv2 parse state.
56+ The op has 3 regions, which match the JSON format for parse states:
57+ - parser_ops contains the operations performed by the state to parse the header
58+ - transition_keys contains the keys from select transitions
59+ - transitions contains the actual transitions
60+ }];
61+
62+ let regions = (region
63+ SizedRegion<1>:$parser_ops,
64+ SizedRegion<1>:$transitions,
65+ SizedRegion<1>:$transition_keys);
66+
67+ let assemblyFormat = [{
68+ $sym_name
69+ `\n``transition_key` $transition_keys
70+ `\n``transitions` $transitions
71+ `\n``parser_ops` $parser_ops attr-dict
72+ }];
73+
74+ let hasVerifier = 1;
75+ }
76+
77+ def BMv2IR_TransitionOp : BMv2IR_Op<"transition", [HasParent<"ParserStateOp">]> {
78+ let summary = "Parser transition";
79+ let description = [{
80+ Represents a single transition from a parse state.
81+
82+ Types:
83+ - default: default transition (no value/mask)
84+ - hexstr: value-based transition with hexstring
85+ - parse_vset: parse value-set based transition
86+
87+ The value and mask are hexstrings in the format described in the BMv2 JSON
88+ spec (concatenation of byte-padded fields). For example, if the transition
89+ key has a 12-bit field and a 2-bit field, values use 3 bytes (0x0aba03).
90+
91+ `next_state` can be null for the last state in the parser.
92+ `value` is the hexstr value or vset name (null if default)
93+ `mask` is the mask (if needed) for the hexstr
94+ }];
95+
96+ // TODO: refine value and mask fields
97+ let arguments = (ins
98+ BMv2IR_TransitionKindAttr:$type,
99+ OptionalAttr<SymbolRefAttr>:$next_state,
100+ OptionalAttr<AnyAttr>:$value,
101+ OptionalAttr<AnyAttr>:$mask
102+ );
103+
104+ let assemblyFormat = "`type`$type (`value` $value^)? (`mask` $mask^)? (`next_state` $next_state^)? attr-dict";
105+ }
106+
107+
108+ def BMv2_ExtractOp : BMv2IR_Op<"extract", [HasParent<"ParserStateOp">, AllowedParserOp]> {
109+ let summary = "Extract header operation";
110+ let description = [{
111+ Extracts the field of a header instance, header stack, or union stack element.
112+
113+ Types:
114+ - regular: extraction to a regular header instance
115+ - stack: extraction to the end of a header stack
116+ - union_stack: extraction to the end of a header union stack
117+ }];
118+
119+ let arguments = (ins
120+ BMv2IR_ExtractKindAttr:$extract_type,
121+ StrAttr:$value,
122+ OptionalAttr<StrAttr>:$union_member
123+ );
124+
125+ let assemblyFormat = [{
126+ $extract_type $value (`union_member` $union_member^)? attr-dict
127+ }];
128+ }
129+
130+ //===-------------------------------------------------------------------------------------------===//
131+ // BMv2 type-value fields
132+ // see https://github.com/p4lang/behavioral-model/blob/main/docs/JSON_format.md#the-type-value-object
133+ //===-------------------------------------------------------------------------------------------===//
134+
135+ def BMv2IR_FieldOp : BMv2IR_Op<"field", [AllowedTransitionKey]> {
136+ let summary = "BMv2 header field access";
137+ let description = [{
138+ Represents access to a field within a header instance.
139+ Contains a 2-tuple: (header_instance_name, field_member_name)
140+ }];
141+
142+ let arguments = (ins
143+ StrAttr:$headerInstance,
144+ StrAttr:$fieldMember
145+ );
146+
147+ let assemblyFormat = "`<` $headerInstance `,` $fieldMember `>` attr-dict";
148+ }
149+
150+ def BMv2_StackFieldOp : BMv2IR_Op<"stack_field", [AllowedTransitionKey]> {
151+ let summary = "BMv2 header stack field access";
152+ let description = [{
153+ Represents access to a field in the last valid header instance in a stack.
154+ Contains a 2-tuple: (header_stack_name, field_member_name)
155+ }];
156+
157+ let arguments = (ins
158+ StrAttr:$headerStack,
159+ StrAttr:$fieldMember
160+ );
161+
162+ let assemblyFormat = "`<` $headerStack `,` $fieldMember `>` attr-dict";
163+ }
164+
165+
166+ def BMv2IR_LookaheadOp : BMv2IR_Op<"lookahead", [AllowedTransitionKey]> {
167+ let summary = "BMv2 parser lookahead";
168+ let description = [{
169+ Represents a lookahead operation in a parser.
170+ Contains a 2-tuple: (bit_offset, bitwidth)
171+ }];
172+
173+ let arguments = (ins
174+ I32Attr:$bitOffset,
175+ I32Attr:$bitwidth
176+ );
177+
178+ let assemblyFormat = "`<` $bitOffset `,` $bitwidth `>` attr-dict";
179+ }
180+
181+ def BMv2_UnionStackFieldOp : BMv2IR_Op<"union_stack_field", [AllowedTransitionKey]> {
182+ let summary = "BMv2 header union stack field access";
183+ let description = [{
184+ Represents access to a field in the last valid union instance in a stack.
185+ Contains a 3-tuple: (header_union_stack_name, union_member_name, field_member_name)
186+ }];
187+
188+ let arguments = (ins
189+ StrAttr:$headerUnionStack,
190+ StrAttr:$unionMember,
191+ StrAttr:$fieldMember
192+ );
193+
194+ let assemblyFormat = "`<` $headerUnionStack `,` $unionMember `,` $fieldMember `>` attr-dict";
195+ }
196+
16197#endif // P4MLIR_DIALECT_BMv2IR_BMv2IR_OPS_TD
0 commit comments