forked from kframework/c-semantics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.k
309 lines (261 loc) · 10.9 KB
/
syntax.k
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
module C-SYNTAX
imports BASIC-K
imports STRING-SYNTAX
imports INT-SYNTAX
imports BOOL-SYNTAX
imports LIST
imports C-DYNAMIC-SYNTAX
syntax KBott ::= "(" K ")" [bracket]
syntax KResult ::= SpecifierElem
syntax C ::= CabsLoc
syntax C ::= CId
syntax C ::= CVSpecifier
syntax C ::= Expression
syntax C ::= SpecifierElem
syntax C ::= Statement
syntax C ::= Storage
syntax CId ::= Identifier(String) // new
syntax CId ::= "#NoName" // new
syntax CId ::= #NoName(Int) // new
syntax Statement ::= Block(Int, K) [avoid]
/*
type cabsloc = {
lineno : int;
filename: string;
lineOffsetStart: int;
lineOffsetEnd : int;
}
*/
syntax CabsLoc ::= CabsLoc(String, Int, Int, Int)
/*
and statement =
NOP of cabsloc
| COMPUTATION of expression * cabsloc
| BLOCK of block * cabsloc
| SEQUENCE of statement * statement * cabsloc
*/
syntax Statement ::= "Nop"
syntax Statement ::= Computation(K) [latex({{#1}};)]
context Computation(HOLE:KItem => reval(HOLE)) [result(RValue)]
/*
and storage =
NO_STORAGE | AUTO | STATIC | EXTERN | REGISTER
*/
// this will be used as the actual atomic modifier
syntax Storage ::= "Atomic"
syntax Storage ::= "NoStorage"
syntax Storage ::= "Auto"
syntax Storage ::= "Static"
syntax Storage ::= "Extern"
syntax Storage ::= "Register"
syntax Storage ::= "ThreadLocal"
/*
and cvspec =
CV_CONST | CV_VOLATILE | CV_RESTRICT
*/
syntax CVSpecifier ::= "Const"
syntax CVSpecifier ::= "Volatile"
syntax CVSpecifier ::= "Restrict"
/*
and spec_elem =
SpecTypedef
| SpecCV of cvspec (* const/volatile *)
| SpecAttr of attribute (* __attribute__ *)
| SpecStorage of storage
| SpecInline
*/
syntax SpecifierElem ::= "SpecTypedef"
// the following are because I flattened SpecifierElem
syntax SpecifierElem ::= CVSpecifier
syntax SpecifierElem ::= Storage
syntax SpecifierElem ::= "Inline"
syntax SpecifierElem ::= "Noreturn"
/*
| IF of expression * statement * statement * cabsloc
| WHILE of expression * statement * cabsloc
| DOWHILE of expression * statement * cabsloc
| FOR of for_clause * expression * expression * statement * cabsloc
*/
syntax Statement ::= IfThenElse(K, K, K)
context IfThenElse((HOLE:KItem => reval(HOLE)), _, _) [result(RValue)]
syntax Statement ::= While(K, K)
syntax Statement ::= DoWhile(K, K)
// Condition, post expression, body
syntax Statement ::= For(K, K, K)
/*
| BREAK of cabsloc
| CONTINUE of cabsloc
| RETURN of expression * cabsloc
*/
syntax Statement ::= "Break"
syntax Statement ::= "Continue"
syntax Statement ::= Return(K)
context Return(HOLE:KItem => reval(HOLE)) [result(RValue)]
/*
| SWITCH of expression * statement * cabsloc
| CASERANGE of expression * expression * statement * cabsloc
*/
// unique switch id
syntax Statement ::= Switch(K, K, K)
context Switch(_, (HOLE:KItem => reval(HOLE)), _) [result(RValue)]
/*
| CASE of expression * statement * cabsloc
| DEFAULT of statement * cabsloc
*/
// unique switch id, unique case id, exp, statement
syntax Statement ::= Case(Int, Int, K, K)
// unique switch id, statement
syntax Statement ::= Default(Int, K)
/*
| LABEL of String * statement * cabsloc
*/
syntax Statement ::= Label(CId, K) [avoid]
/*
| GOTO of String * cabsloc
*/
syntax Statement ::= Goto(CId) // CId
/*
| UNARY of unary_operator * expression
and unary_operator =
MINUS | PLUS | NOT | BNOT | MEMOF | ADDROF
| PREINCR | PREDECR | POSINCR | POSDECR
*/
syntax Expression ::= "-" K [prec(22)]
| "+" K [prec(22)]
| "!" K [prec(22)]
| "~" K [prec(22)]
| "*" K [prec(22)]
| "&" K [prec(22), strict]
| "++" K [prec(22), prefer, strict]
| "--" K [prec(22), prefer, strict, latex(\terminal{-{}-}{#1})]
| K "++" [prefer, strict]
| K "--" [prefer, strict, latex({#1}\terminal{-{}-})]
/* | BINARY of binary_operator * expression * expression */
> K "*" K [prec(31), left]
| K "/" K [prec(31), left]
| K "%" K [prec(31), left]
> K "+" K [prec(33), left]
| K "-" K [prec(33), left]
> K "<<" K [prec(35), left, latex({{#1}}\ll{{#2}})]
| K ">>" K [prec(35), left, latex({{#1}}\gg{{#2}})]
> K "<" K [prec(37), left]
| K "<=" K [prec(37), left]
| K ">" K [prec(37), left]
| K ">=" K [prec(37), left]
> K "==" K [prec(39), left]
| K "!=" K [prec(39), left]
> K "&" K [prec(41), left]
> K "^" K [prec(43), left]
> K "|" K [prec(45), left]
> K "&&" K [prec(47), prefer, left]
> K "||" K [prec(49), left]
/* | QUESTION of expression * expression * expression */
// Ternary operator is right-associative.
> K "?" K ":" K
[prec(51), right, gather(e & E)]
> K "*=" K
[prec(53)]
| K "/=" K
[prec(53)]
| K "%=" K
[prec(53)]
| K "+=" K
[prec(53)]
| K "-=" K
[prec(53)]
| K "<<=" K
[prec(53)]
| K "&=" K
[prec(53)]
| K "^=" K
[prec(53)]
| K "|=" K
[prec(53)]
| K ">>=" K
[prec(53), latex({{#1}}\terminal{$\ll$=}{{#2}})]
| K ":=" K [prec(53)]
context - (HOLE:KItem => reval(HOLE)) [result(RValue)]
context + (HOLE:KItem => reval(HOLE)) [result(RValue)]
context ! (HOLE:KItem => reval(HOLE)) [result(RValue)]
context ~ (HOLE:KItem => reval(HOLE)) [result(RValue)]
context * (HOLE:KItem => reval(HOLE)) [result(RValue)]
context (HOLE:KItem => reval(HOLE)) * _ [ndheat, result(RValue)]
context _ * (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) / _ [ndheat, result(RValue)]
context _ / (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) % _ [ndheat, result(RValue)]
context _ % (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) + _ [ndheat, result(RValue)]
context _ + (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) - _ [ndheat, result(RValue)]
context _ - (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) << _ [ndheat, result(RValue)]
context _ << (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) >> _ [ndheat, result(RValue)]
context _ >> (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) < _ [ndheat, result(RValue)]
context _ < (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) <= _ [ndheat, result(RValue)]
context _ <= (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) > _ [ndheat, result(RValue)]
context _ > (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) >= _ [ndheat, result(RValue)]
context _ >= (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) == _ [ndheat, result(RValue)]
context _ == (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) != _ [ndheat, result(RValue)]
context _ != (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) & _ [ndheat, result(RValue)]
context _ & (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) ^ _ [ndheat, result(RValue)]
context _ ^ (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context (HOLE:KItem => reval(HOLE)) | _ [ndheat, result(RValue)]
context _ | (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
context HOLE:KItem *= _
context _ *= (HOLE:KItem => reval(HOLE)) [result(RValue)]
context HOLE:KItem /= _
context _ /= (HOLE:KItem => reval(HOLE)) [result(RValue)]
context HOLE:KItem %= _
context _ %= (HOLE:KItem => reval(HOLE)) [result(RValue)]
context HOLE:KItem += _
context _ += (HOLE:KItem => reval(HOLE)) [result(RValue)]
context HOLE:KItem -= _
context _ -= (HOLE:KItem => reval(HOLE)) [result(RValue)]
context HOLE:KItem <<= _
context _ <<= (HOLE:KItem => reval(HOLE)) [result(RValue)]
context HOLE:KItem &= _
context _ &= (HOLE:KItem => reval(HOLE)) [result(RValue)]
context HOLE:KItem ^= _
context _ ^= (HOLE:KItem => reval(HOLE)) [result(RValue)]
context HOLE:KItem |= _
context _ |= (HOLE:KItem => reval(HOLE)) [result(RValue)]
context HOLE:KItem >>= _
context _ >>= (HOLE:KItem => reval(HOLE)) [result(RValue)]
context HOLE:KItem := _ [ndheat]
context _ := (HOLE:KItem => reval(HOLE)) [ndheat, result(RValue)]
syntax Expression ::= Cast(K, K) [klabel(Cast2)]
context Cast(HOLE:KItem, _)
context Cast(_, (HOLE:KItem => reval(HOLE))) [result(RValue)]
/*
(* There is a special form of CALL in which the function called
__builtin_va_arg and the second argument is sizeof(T). This
should be printed as just T *)
| CALL of expression * expression list
*/
syntax Expression ::= Call(K, K)
context Call((HOLE:KItem => reval(HOLE)), _) [ndheat, result(RValue)]
/*
| MEMBEROF of expression * string
*/
syntax Expression ::= K "." CId [left, strict(1)]
/*
and init_expression =
| NO_INIT
*/
syntax KItem ::= "NoInit"
/*
and attribute = String * expression list
*/
// String, List
syntax C ::= Attribute(String, K)
endmodule