-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp4_lang.sh
executable file
·288 lines (234 loc) · 6.19 KB
/
p4_lang.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/bash
verbose=0
#set -x
#
# This is a simple heuristical utility that tries to determine if a given
# program is written in P4_16 or P4_14. It works on any file, but the more
# information there is, the more accurate the result is.
# * If you feed it the fully preprocessed program, the correct result is
# pretty much guaranteed
# * If you feed it the top-level file only, the result might be less
# certain (which is why I strongly suggest to start all your P4 code
# with the Emacs modeline /* -*- P4_1[46] -*- */
# * There are some .p4 files that contain nothing but #defines. For these
# (or if you feed programs in other languages), the program might
# output "no-p4"
# * This is not an AI program in bash! Thus it might classify C++ or
# Java code as p4_16, for example.
#
function print_help() {
prog=$0
cat <<EOF
Usage:
$prog [-h] [-v level]
-v level
Specifies verbosity level:
0 -- just output the language (default)
1 -- output total scores
2 -- output scores for individual elements
EOF
}
#
# The function counts how many times a certain pattern has been
# found in a file
#
function score_pattern() {
file="$1"; shift
pattern="$1"; shift
weight="$1" ; shift
comment="$1"; shift
count=`egrep "$pattern" "$file" | wc -l`
score=$(($count * $weight))
if [ $verbose -ge 2 ]; then
printf "%40s: %3d = %d * %d\n" "$comment" $score $count $weight >& 2
fi
echo $score
}
#
# The inline STDIN in this function contains the typical strings that we are
# looking for in a P4_14 program. The function after this one contains the
# definitions for typical P4_16 elements instead
#
# You can add more definitions if you like
#
# Each definition consists of 4 lines:
# --------------
# 1. #include with Angle Brackets
# 2. #include.*<.*>
# 3. -1
# 4.
# -------------
#
# Line 1 contains the explanation what we are looking for (useful for debug)
# Line 2 contains an egrep pattern. Note: for a backslash, you need to use \\\\
# Line 3 contains the weight (importance) of a certain construct. Note how one
# line uses the weight -1 to discount a certain pattern not relevant for
# the detection
# Line 4 is empty or it must contain the word END
#
function p4_14_score()
{
score=0
while true; do
read comment
read pattern
read weight
read end
score=$(( $score + `score_pattern $1 "$pattern" "$weight" "$comment"` ))
if [ x$end == 'xEND' ]; then
break;
fi
done <<EOF
P4_14 Emacs Mode Line
/\\\\* -\\\\*-\ P4_14\ -\\\\*-\ \\\\*/
10
apply() statement invocations
[^._[:alnum:]][[:space:]]*apply[[:space:]]*\\\\(
1
extract() statement invocations
[^._[:alnum:]][[:space:]]*extract *\\\\(
1
return statements in the parser
return[[:space:]]+(select[[:space:]]*\\\\(|ingress*[[:space:]]*;)
2
header and metadata instantiators
(header|metadata)[[:space:]]+[[:alpha:]_][[:alnum:]_]*[[:space:]]+[[:alpha:]_][[:alnum:]_]*(\\\\[[[:digit:]]\\\\])?[[:space:]]*;
2
header_type type declarations
header_type +[[:alpha:]_][[:alnum:]_]*[[:space:]]*{
2
modify_field() primitive invocations
modify_field[[:space:]]*\\\\(
1
table attributes with a brace
(reads|actions)[[:space:]]*{
1
table attributes with a colon
(size,default_action|type)[[:space:]]*:
1
P4_14 standard objects
(counter|meter|register|field_list|field_list_calculation|blackbox_type)[[:space:]]+[[:alpha:]_][[:alnum:]_]*[[:space:]]*{
2
controls and parsers without parameters
(control|parser)[[:space:]]*[[:alpha:]_][[:alnum:]_]*[[:space:]]*[^(]
2
END
EOF
echo $score
}
function p4_16_score()
{
score=0
while true; do
read comment
read pattern
read weight
read end
score=$(( $score + `score_pattern $1 "$pattern" "$weight" "$comment"` ))
if [ x$end == 'xEND' ]; then
break;
fi
done <<EOF
P4_16 Emacs Mode Line
/\\\\* -\\\\*-\ P4_16\ -\\\\*-\ \\\\*/
10
Angle Brackets
<.*>
1
#include with Angle Brackets
#include.*<.*>
-1
Apply method invocations
\\\\.[[:space:]]*apply *\\\\(
1
Apply method definitions
[dht>][[:space:]]+apply *\\\\(
1
Apply blocks
apply[[:space:]]*{
5
extract() method invocations
\\\\.[[:space:]]*extract[[:space:]]*\\\\(
1
emit() method invocations
\\\\.[[:space:]]*emit[[:space:]]*\\\\(
2
transition statements
transition[[:space:]]+(select[[:space:]]*\\\\(|[[:alpha:]_][[:alnum:]_]*[[:space:]]*;)
1
parser/control with parameters
(control|parser)[[:space:]]*[[:alpha:]_][[:alnum:]_]*[[:space:]]*\\\\(
2
header statements as type declarations
(header|struct|header_union)[[:space:]]+[[:alpha:]_][[:alnum:]_]*[[:space:]]*{
1
P4_16 table properties with a brace
[[:space:]]*(keys|actions|size|default_action)[[:space:]]*=
1
P4_16 saturated operations
\\\\|[+-]\\\\|
10
P4_16 bit-slicing operations
\\\\[[[:space:]]*[[:digit:]]+[[:space:]]*:[[:space:]]*[[:digit:]]+[[:space:]]*\\\\]
1
P4_16 binary const with explicit width
[[:digit:]]+[sw]0b[01]+
2
P4_16 decimal const with explicit width
[[:digit:]]+[sw][[:digit:]]+
2
P4_16 hex const with explicit width
[[:digit:]]+[sw]0x[[:xdigit:]]+
2
"main" package instantiation
[[:space:]]*main[[:space:]]*;
5
END
EOF
echo $score
}
function main() {
if [ $verbose -ge 2 ]; then
echo "Calculating P4_14 score" >& 2
fi
p4_14_score=`p4_14_score $1`
if [ $verbose -ge 1 ]; then
echo "P4_14:" $p4_14_score >&2
fi
if [ $verbose -ge 2 ]; then
echo "Calculating P4_16 score" >& 2
fi
p4_16_score=`p4_16_score $1`
if [ $verbose -ge 1 ]; then
echo "P4_16:" $p4_16_score >&2
fi
if [ $p4_14_score -eq 0 -a $p4_16_score -eq 0 ]; then
echo "no-p4"
else
if [ $p4_14_score -gt $p4_16_score ]; then
echo "p4-14"
else
echo "p4_16"
fi
fi
}
#
# Parse Options
#
opts=`getopt -o hv: \
-l help \
-l verbose: \
-- "$@"`
if [ $? != 0 ]; then
print_help
exit 1
fi
eval set -- "$opts"
while true; do
case "$1" in
-h|--help) print_help; exit 0 ;;
-v|--verbose) verbose=$2; shift 2 ;;
--) shift; break
esac
done
main $1