@@ -37,6 +37,7 @@ static map<int, std::pair<std::string, int>> line_file_map;
37
37
static IR::V1Program *global;
38
38
static void yyerror(const char *fmt);
39
39
static IR::Constant *constantFold(IR::Expression *);
40
+ static IR::Vector<IR::Expression> make_expr_list(const IR::NameList *);
40
41
41
42
#define YYLTYPE Util::SourceInfo
42
43
#define YYLLOC_DEFAULT(Cur, Rhs, N) \
@@ -62,19 +63,26 @@ static const IR::Annotations *getPragmas() {
62
63
IR::ActionFunction *ActionFunction;
63
64
IR::ActionProfile *ActionProfile;
64
65
IR::ActionSelector *ActionSelector;
66
+ IR::Annotations *Annotations;
65
67
IR::Apply *Apply;
68
+ IR::Attribute *Attribute;
66
69
IR::CalculatedField *CalculatedField;
67
70
IR::CaseEntry *CaseEntry;
68
71
IR::Vector<IR::CaseEntry> *CaseEntryList;
69
72
IR::Constant *Constant;
70
73
IR::Counter *Counter;
74
+ IR::Declaration_Instance *BBox;
75
+ IR::Direction Direction;
71
76
IR::Expression *Expression;
72
77
IR::Vector<IR::Expression> *ExpressionList;
73
78
IR::FieldList *FieldList;
74
79
IR::FieldListCalculation *FieldListCalculation;
75
80
IR::NameList *NameList;
76
81
IR::Member *Member;
77
82
IR::Meter *Meter;
83
+ IR::Parameter *Parameter;
84
+ IR::ParameterList *ParameterList;
85
+ IR::IndexedVector<IR::Parameter> *ParameterVector;
78
86
IR::V1Parser *Parser;
79
87
IR::Register *Register;
80
88
IR::V1Table *Table;
@@ -83,21 +91,26 @@ static const IR::Annotations *getPragmas() {
83
91
IR::Vector<IR::Annotation> *annotations;
84
92
IR::IndexedVector<IR::StructField> *fields;
85
93
} HeaderType;
94
+ struct {
95
+ IR::Vector<IR::Method> *methods;
96
+ IR::NameMap<IR::Attribute, ordered_map> *attribs;
97
+ } BBoxType;
86
98
}
87
99
88
100
%token<str> ACTION ACTIONS ACTION_PROFILE ACTION_SELECTOR ALGORITHM APPLY
89
- ATTRIBUTE ATTRIBUTES BIT BLACKBOX BLACKBOX_TYPE
101
+ ATTRIBUTE ATTRIBUTES BIT BLACKBOX BLACKBOX_TYPE BLOCK
90
102
CALCULATED_FIELD CONTROL COUNTER CURRENT DEFAULT DEFAULT_ACTION
91
- DIRECT DROP DYNAMIC_ACTION_SELECTION ELSE EXTRACT FALSE
103
+ DIRECT DROP DYNAMIC_ACTION_SELECTION ELSE EXTRACT EXPRESSION
104
+ EXPRESSION_LOCAL_VARIABLES FALSE
92
105
FIELD_LIST FIELD_LIST_CALCULATION FIELDS HEADER HEADER_TYPE
93
- IF IMPLEMENTATION INPUT INSTANCE_COUNT INT LATEST LAYOUT LENGTH MASK
106
+ IF IMPLEMENTATION IN INPUT INSTANCE_COUNT INT LATEST LAYOUT LENGTH MASK
94
107
MAX_LENGTH MAX_SIZE MAX_WIDTH METADATA METER METHOD MIN_SIZE MIN_WIDTH NOT
95
- OUTPUT_WIDTH PARSE_ERROR PARSER PARSER_EXCEPTION PAYLOAD PRAGMA PREFIX
96
- PRE_COLOR PRIMITIVE_ACTION READS REGISTER RESULT RETURN SATURATING
108
+ OPTIONAL OUT OUTPUT_WIDTH PARSE_ERROR PARSER PARSER_EXCEPTION PAYLOAD PRAGMA
109
+ PREFIX PRE_COLOR PRIMITIVE_ACTION READS REGISTER RESULT RETURN SATURATING
97
110
SELECT SELECTION_KEY SELECTION_MODE SELECTION_TYPE SET_METADATA
98
- SIGNED SIZE STATIC TABLE TRUE TYPE UPDATE VALID VERIFY WIDTH WRITES
111
+ SIGNED SIZE STATIC STRING TABLE TRUE TYPE UPDATE VALID VERIFY WIDTH WRITES
99
112
100
- %token<str> IDENTIFIER STRING
113
+ %token<str> IDENTIFIER STRING_LITERAL
101
114
%token<Constant> INTEGER
102
115
103
116
%printer { fprintf(yyoutput, "\"%s\"", $$.c_str()); } <str>
@@ -123,12 +136,17 @@ static const IR::Annotations *getPragmas() {
123
136
%type<ActionFunction> action_function_body action_statement_list
124
137
%type<ActionProfile> action_profile_body
125
138
%type<ActionSelector> action_selector_body
139
+ %type<Annotations> blackbox_method
126
140
%type<Apply> apply_case_list
141
+ %type<Attribute> blackbox_attribute
142
+ %type<BBox> blackbox_config
143
+ %type<BBoxType> blackbox_body
127
144
%type<CalculatedField> update_verify_spec_list
128
145
%type<CaseEntry> case_value_list
129
146
%type<CaseEntryList> case_entry_list
130
147
%type<Constant> const_expression
131
148
%type<Counter> counter_spec_list
149
+ %type<Direction> inout
132
150
%type<Expression> expression header_ref header_or_field_ref field_or_masked_ref opt_condition
133
151
pragma_operand
134
152
%type<ExpressionList> expression_list opt_expression_list control_statement_list opt_else
@@ -139,6 +157,9 @@ static const IR::Annotations *getPragmas() {
139
157
%type<HeaderType> header_dec_body field_declarations
140
158
%type<NameList> action_list name_list opt_name_list field_list_list
141
159
%type<Meter> meter_spec_list
160
+ %type<Parameter> argument
161
+ %type<ParameterList> opt_argument_list
162
+ %type<ParameterVector> argument_list
142
163
%type<Parser> parser_statement_list
143
164
%type<Register> register_spec_list
144
165
%type<Table> table_body
@@ -239,14 +260,24 @@ opt_max_length: /* epsilon */
239
260
IR::Vector<IR::Expression>($3)); }
240
261
;
241
262
242
- type: INT '<' INTEGER '>'
263
+ type: BIT { $$ = IR::Type::Bits::get(@1, 1); }
264
+ | BIT '<' INTEGER '>'
243
265
{ if (!$3->fitsInt())
244
266
BUG("%1$: Value too large", @3);
245
267
$$ = IR::Type::Bits::get(@3, $3->asInt()); }
246
- | BIT '<' INTEGER '>'
268
+ | BLOCK { $$ = IR::Type_Block::get(); }
269
+ | COUNTER { $$ = IR::Type_Counter::get(); }
270
+ | EXPRESSION { $$ = IR::Type_Expression::get(); }
271
+ | FIELD_LIST_CALCULATION { $$ = IR::Type_FieldListCalculation::get(); }
272
+ | INT { $$ = new IR::Type_InfInt; }
273
+ | INT '<' INTEGER '>'
247
274
{ if (!$3->fitsInt())
248
275
BUG("%1$: Value too large", @3);
249
- $$ = IR::Type::Bits::get(@3, $3->asInt()); }
276
+ $$ = IR::Type::Bits::get(@3, $3->asInt(), true); }
277
+ | METER { $$ = IR::Type_Meter::get(); }
278
+ | STRING { $$ = IR::Type_String::get(); }
279
+ | REGISTER { $$ = IR::Type_Register::get(); }
280
+ | TABLE { $$ = IR::Type_AnyTable::get(); }
250
281
;
251
282
252
283
/************************************/
@@ -603,7 +634,7 @@ table_body: /* epsilon */ { $$ = new IR::V1Table(getPragmas()); }
603
634
$$->default_action_args = $6; }
604
635
| table_body IDENTIFIER ':' expression ';'
605
636
{ auto exp = new IR::ExpressionValue(@4, $4);
606
- auto prop = new IR::TableProperty (@2, IR::ID(@2, $2), IR::Annotations::empty, exp, false);
637
+ auto prop = new IR::Property (@2, IR::ID(@2, $2), IR::Annotations::empty, exp, false);
607
638
$1->addProperty(prop);
608
639
$$ = $1; }
609
640
| table_body error ';' { $$=$1; }
@@ -679,47 +710,83 @@ apply_case_list: /* epsilon */ { $$ = new IR::Apply; }
679
710
/************/
680
711
681
712
blackbox_type_declaration: BLACKBOX_TYPE name '{' blackbox_body '}'
682
- { current_pragmas.clear(); }
713
+ { global->add($2, new IR::Type_Extern(@1+@5, IR::ID(@2, $2), $4.methods,
714
+ *$4.attribs, getPragmas())); }
683
715
;
684
716
685
- blackbox_body: /* epsilon */
717
+ blackbox_body: /* epsilon */ {
718
+ $$.methods = new IR::Vector<IR::Method>;
719
+ $$.attribs = new IR::NameMap<IR::Attribute, ordered_map>; }
686
720
| blackbox_body ATTRIBUTE name '{' blackbox_attribute '}'
721
+ { ($$=$1).attribs->addUnique($3, $5); }
687
722
| blackbox_body METHOD name '(' opt_argument_list ')' ';'
723
+ { ($$=$1).methods->push_back(new IR::Method(@2+@3, $3,
724
+ new IR::Type_Method(@4+@6, IR::Type::Void::get(), $5))); }
688
725
| blackbox_body METHOD name '(' opt_argument_list ')' '{' blackbox_method '}'
726
+ { ($$=$1).methods->push_back(new IR::Method(@2+@3, $3,
727
+ new IR::Type_Method(@4+@6, IR::Type::Void::get(), $5), $8)); }
689
728
| blackbox_body error
690
729
;
691
730
692
- blackbox_attribute: /* epsilon */
693
- | blackbox_attribute name ':' name ';'
694
- | blackbox_attribute name ':' type ';'
695
- | blackbox_attribute name '{' opt_name_list '}'
696
- | blackbox_attribute name ';'
731
+ blackbox_attribute: /* epsilon */ { $$ = new IR::Attribute(@-1, $<str>-1); }
732
+ | blackbox_attribute TYPE ':' type ';' { ($$=$1)->type = $4; }
733
+ | blackbox_attribute EXPRESSION_LOCAL_VARIABLES '{' opt_name_list '}'
734
+ { ($$=$1)->locals = $4; }
735
+ | blackbox_attribute OPTIONAL ';'
736
+ { ($$=$1)->optional = true; }
697
737
| blackbox_attribute error ';'
698
738
| blackbox_attribute error
699
739
;
700
740
701
- blackbox_method: /* epsilon */
741
+ blackbox_method: { $$ = new IR::Annotations; }
702
742
| blackbox_method READS '{' opt_name_list '}'
743
+ { ($$=$1)->add(new IR::Annotation(@2, $2, make_expr_list($4))); }
703
744
| blackbox_method WRITES '{' opt_name_list '}'
745
+ { ($$=$1)->add(new IR::Annotation(@2, $2, make_expr_list($4))); }
704
746
;
705
747
706
- opt_argument_list: /* epsilon */ | argument_list ;
748
+ opt_argument_list:
749
+ /* epsilon */ { $$ = new IR::ParameterList(new IR::IndexedVector<IR::Parameter>); }
750
+ | argument_list { $$ = new IR::ParameterList($1); }
751
+ ;
707
752
708
- argument_list: argument | argument_list ',' argument ;
753
+ argument_list:
754
+ argument { $$ = new IR::IndexedVector<IR::Parameter>($1); }
755
+ | argument_list ',' argument { ($$=$1)->push_back($3); }
756
+ ;
709
757
710
- argument: name | type | argument name | argument type;
758
+ argument:
759
+ inout type name
760
+ { $$ = new IR::Parameter(@3, $3, $1, $2); }
761
+ | OPTIONAL argument
762
+ { ($$ = $2)->annotations = $2->annotations->add(new IR::Annotation(@1, $1, {})); }
763
+ ;
764
+
765
+ inout: IN { $$ = IR::Direction::In; } | OUT { $$ = IR::Direction::Out; } ;
711
766
712
767
blackbox_instantiation:
713
768
BLACKBOX name name ';'
714
- { current_pragmas.clear(); }
715
- | BLACKBOX name name '{' blackbox_config '}'
716
- { current_pragmas.clear(); }
769
+ { global->add($3, new IR::Declaration_Instance(@3, $3, getPragmas(),
770
+ new IR::Type_Name($2), new IR::Vector<IR::Expression>)); }
771
+ | BLACKBOX name name '{'
772
+ { $<BBox>$ = new IR::Declaration_Instance(@3, $3, getPragmas(),
773
+ new IR::Type_Name($2), new IR::Vector<IR::Expression>); }
774
+ blackbox_config '}'
775
+ { global->add($3, $6); }
717
776
;
718
777
719
- blackbox_config: /* epsilon */
778
+ blackbox_config: /* epsilon */ { $$ = $<BBox>0; }
720
779
| blackbox_config name ':' expressions ';'
780
+ { const IR::PropertyValue *pv;
781
+ if ($4->size() == 1)
782
+ pv = new IR::ExpressionValue($4->front());
783
+ else
784
+ pv = new IR::ExpressionListValue(std::move(*$4));
785
+ ($$=$1)->properties.add($2, new IR::Property(@2+@4, $2, pv, false)); }
721
786
| blackbox_config name '{' expressions '}'
722
- ;
787
+ { auto *pv = new IR::ExpressionListValue(std::move(*$4));
788
+ ($$=$1)->properties.add($2, new IR::Property(@2+@4, $2, pv, false)); }
789
+ ;
723
790
724
791
expressions: /* epsilon */
725
792
{ $$ = new IR::Vector<IR::Expression>; }
@@ -739,9 +806,9 @@ pragma_operands: /* epsilon */
739
806
740
807
pragma_operand:
741
808
INTEGER { $$ = $1; }
742
- | STRING { $$ = new IR::StringLiteral($1); }
743
- | name { $$ = new IR::StringLiteral($1); }
744
- | name '.' name { $$ = new IR::StringLiteral($1 + '.' + $3); }
809
+ | STRING_LITERAL { $$ = new IR::StringLiteral(@1, $1); }
810
+ | name { $$ = new IR::StringLiteral(@1, $1); }
811
+ | name '.' name { $$ = new IR::StringLiteral(@1+@3, $1 + '.' + $3); }
745
812
;
746
813
747
814
/***************/
@@ -813,15 +880,16 @@ opt_expression_list: /* epsilon */ { $$ = nullptr; } | expression_list
813
880
814
881
name: IDENTIFIER
815
882
| ACTION | ACTIONS | ACTION_PROFILE | ACTION_SELECTOR | ALGORITHM
816
- | ATTRIBUTE | ATTRIBUTES | BIT | BLACKBOX | BLACKBOX_TYPE
883
+ | ATTRIBUTE | ATTRIBUTES | BIT | BLACKBOX | BLACKBOX_TYPE | BLOCK
817
884
| CALCULATED_FIELD | CONTROL | COUNTER | DEFAULT_ACTION
818
- | DIRECT | DROP | DYNAMIC_ACTION_SELECTION | EXTRACT | FIELD_LIST
819
- | FIELD_LIST_CALCULATION | FIELDS | HEADER | HEADER_TYPE | IMPLEMENTATION
885
+ | DIRECT | DROP | DYNAMIC_ACTION_SELECTION | EXPRESSION | EXPRESSION_LOCAL_VARIABLES
886
+ | EXTRACT | FIELD_LIST
887
+ | FIELD_LIST_CALCULATION | FIELDS | HEADER | HEADER_TYPE | IMPLEMENTATION | IN
820
888
| INPUT | INSTANCE_COUNT | INT | LAYOUT | LENGTH | MASK | MAX_LENGTH | MAX_SIZE
821
- | MAX_WIDTH | METADATA | METER | METHOD | MIN_SIZE | MIN_WIDTH | OUTPUT_WIDTH
822
- | PARSER | PARSER_EXCEPTION | PRE_COLOR | PRIMITIVE_ACTION | READS
889
+ | MAX_WIDTH | METADATA | METER | METHOD | MIN_SIZE | MIN_WIDTH | OPTIONAL | OUT
890
+ | OUTPUT_WIDTH | PARSER | PARSER_EXCEPTION | PRE_COLOR | PRIMITIVE_ACTION | READS
823
891
| REGISTER | RESULT | RETURN | SATURATING | SELECTION_KEY | SELECTION_MODE
824
- | SELECTION_TYPE | SET_METADATA | SIGNED | SIZE | STATIC | TABLE | TYPE
892
+ | SELECTION_TYPE | SET_METADATA | SIGNED | SIZE | STATIC | STRING | TABLE | TYPE
825
893
| UPDATE | VERIFY | WIDTH | WRITES
826
894
;
827
895
@@ -867,3 +935,10 @@ static IR::Constant *constantFold(IR::Expression *a) {
867
935
auto rv = exp->apply(P4::DoConstantFolding(nullptr, nullptr))->to<IR::Constant>();
868
936
return rv ? new IR::Constant(rv->type, rv->value, rv->base) : nullptr;
869
937
}
938
+
939
+ static IR::Vector<IR::Expression> make_expr_list(const IR::NameList *list) {
940
+ IR::Vector<IR::Expression> rv;
941
+ for (auto &name :list->names)
942
+ rv.push_back(new IR::StringLiteral(name));
943
+ return rv;
944
+ }
0 commit comments