Skip to content

Commit 656d459

Browse files
committed
Lazy generation of the lable table and some enum stuff.
1 parent 1903c1f commit 656d459

File tree

5 files changed

+122
-129
lines changed

5 files changed

+122
-129
lines changed

cil-semantics/cil-decl.k

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,8 @@ module CIL-DECL
112112
<k>
113113
defineFun(F:Ptr, T:Type, Ps:Params, FB:FunBody)
114114
=> lower-break-continue-to-goto-control(F)
115-
~> make-goto-table-control(F)
116115
//~> propagate-constants(F)
117116
...</k>
118-
// <frame-fun-id> _ => F </frame-fun-id>
119117
<funs>...
120118
.Bag
121119
=>
@@ -285,116 +283,6 @@ module CIL-DECL
285283
rule normLabels(FB:FunBody)
286284
=> #visit(FB, 'normLabelsVisitor, 0, 'needsLabelNorm)::FunBody
287285

288-
/*
289-
* Compute the goto table (Map{Label, Stmt})
290-
*/
291-
syntax K ::= "make-goto-table-control" "(" Ptr ")"
292-
rule
293-
<k> make-goto-table-control(F:Ptr) ...</k>
294-
<fun>...
295-
<fun-id> F </fun-id>
296-
<body> { _:VarDecls { Ss:Stmts } } </body>
297-
...</fun>
298-
<make-goto-table>
299-
<make-goto-table-enabled> false => true </make-goto-table-enabled>
300-
<make-goto-table-tasks>
301-
(.Bag =>
302-
<make-goto-table-task>
303-
<make-goto-table-stmts> Ss </make-goto-table-stmts>
304-
<make-goto-table-cont> .Stmts </make-goto-table-cont>
305-
</make-goto-table-task>
306-
)
307-
</make-goto-table-tasks>
308-
</make-goto-table>
309-
[structural]
310-
rule
311-
<make-goto-table-tasks>...
312-
<make-goto-table-task>...
313-
<make-goto-table-stmts> .Stmts </make-goto-table-stmts>
314-
...</make-goto-table-task>
315-
=> .Bag
316-
...</make-goto-table-tasks>
317-
[structural]
318-
rule
319-
<k> make-goto-table-control(_:Ptr) => . ...</k>
320-
<make-goto-table-enabled> true => false </make-goto-table-enabled>
321-
<make-goto-table-tasks> .Bag </make-goto-table-tasks>
322-
[structural]
323-
324-
rule
325-
<k> make-goto-table-control(F:Ptr) ...</k>
326-
<fun>...
327-
<fun-id> F </fun-id>
328-
<labels>... .Map => L |-> (S Ss1) @Stmts Cont ...</labels>
329-
...</fun>
330-
<make-goto-table-task>
331-
<make-goto-table-stmts>
332-
L:Label : S:Stmt Ss1:Stmts => S Ss1
333-
</make-goto-table-stmts>
334-
<make-goto-table-cont>
335-
Cont:Stmts
336-
</make-goto-table-cont>
337-
</make-goto-table-task>
338-
[structural]
339-
340-
rule
341-
<make-goto-table-tasks>...
342-
<make-goto-table-task>
343-
<make-goto-table-stmts>
344-
(if ( _ ) S1:Stmt else S2:Stmt) Ss:Stmts => Ss
345-
</make-goto-table-stmts>
346-
<make-goto-table-cont>
347-
Cont:Stmts
348-
</make-goto-table-cont>
349-
</make-goto-table-task>
350-
(.Bag =>
351-
<make-goto-table-task>
352-
<make-goto-table-stmts> S1 (.Stmts) </make-goto-table-stmts>
353-
<make-goto-table-cont> Ss @Stmts Cont </make-goto-table-cont>
354-
</make-goto-table-task>
355-
<make-goto-table-task>
356-
<make-goto-table-stmts> S2 (.Stmts) </make-goto-table-stmts>
357-
<make-goto-table-cont> Ss @Stmts Cont </make-goto-table-cont>
358-
</make-goto-table-task>
359-
)
360-
...</make-goto-table-tasks>
361-
[structural]
362-
rule
363-
<make-goto-table-tasks>...
364-
<make-goto-table-task>
365-
<make-goto-table-stmts>
366-
while (E:Exp) S:Stmt Ss:Stmts => Ss
367-
</make-goto-table-stmts>
368-
<make-goto-table-cont>
369-
Cont:Stmts
370-
</make-goto-table-cont>
371-
</make-goto-table-task>
372-
(.Bag =>
373-
<make-goto-table-task>
374-
<make-goto-table-stmts> S (.Stmts) </make-goto-table-stmts>
375-
<make-goto-table-cont>
376-
(while (E) S Ss) @Stmts Cont
377-
</make-goto-table-cont>
378-
</make-goto-table-task>
379-
)
380-
...</make-goto-table-tasks>
381-
[structural]
382-
rule
383-
<make-goto-table-stmts>
384-
{ Ss1:Stmts } Ss2:Stmts => Ss1 @Stmts Ss2
385-
</make-goto-table-stmts>
386-
[structural]
387-
rule
388-
<make-goto-table-stmts>
389-
KLabel:KLabel(KList:KList) Ss:Stmts => Ss
390-
</make-goto-table-stmts>
391-
when isStmt(KLabel:KLabel(KList:KList))
392-
andBool KLabel =/=KLabel '_:_
393-
andBool KLabel =/=KLabel 'if`(_`)_else_
394-
andBool KLabel =/=KLabel 'while`(_`)_
395-
andBool KLabel =/=KLabel '`{_`}
396-
[structural]
397-
398286
/*
399287
* Local variable declarations (without initializers).
400288
*/

cil-semantics/cil-implementation.k

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ require "cil-common.k"
1010
module CIL-IMPLEMENTATION
1111
imports CIL-COMMON
1212

13+
// "each enumerated type shall be compatible with char, a signed integer
14+
// type, or an unsigned integer type"
15+
rule enum X:CId => int [macro]
16+
1317
/*
1418
* FIXME: it would be faster to treat each basic type individually, rather
1519
* that us byteWidthMacro

cil-semantics/cil-stmt.k

Lines changed: 113 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,143 @@ module CIL-STMT
1313
rule
1414
<k> goto L:Label; ~> _ => Ss </k>
1515
<frame-fun-id> F:Ptr </frame-fun-id>
16-
<fun>...
17-
<fun-id> F </fun-id>
18-
<labels>... L |-> Ss:Stmts ...</labels>
19-
...</fun>
16+
<fun-id> F </fun-id>
17+
<labels>... L |-> Ss:Stmts ...</labels>
2018
[computational]
2119

22-
rule <k> (. => eval-case-labels)
23-
~> goto $caseLabel(_, _) ;
24-
...</k>
20+
rule
21+
<k> (. => make-goto-table ~> eval-case-labels) ~> goto _:Label ; ...</k>
2522
<frame-fun-id> F:Ptr </frame-fun-id>
2623
<fun-id> F </fun-id>
27-
<labels-evaluated> false </labels-evaluated>
24+
<labels-generated> false => true </labels-generated>
2825
[structural]
2926

3027
rule <k> goto $caseLabel(I:Int, V:TypedValue) ;
3128
=> goto $defaultLabel(I) ; ...</k>
3229
<frame-fun-id> F:Ptr </frame-fun-id>
3330
<fun-id> F </fun-id>
3431
<labels> Lbls:Map </labels>
35-
<labels-evaluated> true </labels-evaluated>
32+
<labels-generated> true </labels-generated>
3633
when notBool ($caseLabel(I, V) in keys Lbls)
3734
[structural]
3835

36+
/*
37+
* Compute the goto table (Map{Label, Stmt}). We do this at "runtime" to
38+
* improve the performance of the interpreter.
39+
*/
40+
syntax K ::= "make-goto-table"
41+
rule
42+
<k> make-goto-table ...</k>
43+
<frame-fun-id> F:Ptr </frame-fun-id>
44+
<fun-id> F </fun-id>
45+
<body> { _:VarDecls { Ss:Stmts } } </body>
46+
<make-goto-table-enabled> false => true </make-goto-table-enabled>
47+
<make-goto-table-tasks>
48+
(.Bag =>
49+
<make-goto-table-task>
50+
<make-goto-table-stmts> Ss </make-goto-table-stmts>
51+
<make-goto-table-cont> .Stmts </make-goto-table-cont>
52+
</make-goto-table-task>
53+
)
54+
</make-goto-table-tasks>
55+
[structural]
56+
rule
57+
<make-goto-table-tasks>...
58+
<make-goto-table-task>...
59+
<make-goto-table-stmts> .Stmts </make-goto-table-stmts>
60+
...</make-goto-table-task>
61+
=> .Bag
62+
...</make-goto-table-tasks>
63+
[structural]
64+
rule
65+
<k> make-goto-table => . ...</k>
66+
<make-goto-table-enabled> true => false </make-goto-table-enabled>
67+
<make-goto-table-tasks> .Bag </make-goto-table-tasks>
68+
[structural]
69+
70+
rule
71+
<frame-fun-id> F:Ptr </frame-fun-id>
72+
<fun-id> F </fun-id>
73+
<labels>... .Map => L |-> (S Ss1) @Stmts Cont ...</labels>
74+
<make-goto-table-task>
75+
<make-goto-table-stmts>
76+
L:Label : S:Stmt Ss1:Stmts => S Ss1
77+
</make-goto-table-stmts>
78+
<make-goto-table-cont>
79+
Cont:Stmts
80+
</make-goto-table-cont>
81+
</make-goto-table-task>
82+
[structural]
83+
84+
rule
85+
<make-goto-table-tasks>...
86+
<make-goto-table-task>
87+
<make-goto-table-stmts>
88+
(if ( _ ) S1:Stmt else S2:Stmt) Ss:Stmts => Ss
89+
</make-goto-table-stmts>
90+
<make-goto-table-cont>
91+
Cont:Stmts
92+
</make-goto-table-cont>
93+
</make-goto-table-task>
94+
(.Bag =>
95+
<make-goto-table-task>
96+
<make-goto-table-stmts> S1 (.Stmts) </make-goto-table-stmts>
97+
<make-goto-table-cont> Ss @Stmts Cont </make-goto-table-cont>
98+
</make-goto-table-task>
99+
<make-goto-table-task>
100+
<make-goto-table-stmts> S2 (.Stmts) </make-goto-table-stmts>
101+
<make-goto-table-cont> Ss @Stmts Cont </make-goto-table-cont>
102+
</make-goto-table-task>
103+
)
104+
...</make-goto-table-tasks>
105+
[structural]
106+
rule
107+
<make-goto-table-tasks>...
108+
<make-goto-table-task>
109+
<make-goto-table-stmts>
110+
while (E:Exp) S:Stmt Ss:Stmts => Ss
111+
</make-goto-table-stmts>
112+
<make-goto-table-cont>
113+
Cont:Stmts
114+
</make-goto-table-cont>
115+
</make-goto-table-task>
116+
(.Bag =>
117+
<make-goto-table-task>
118+
<make-goto-table-stmts> S (.Stmts) </make-goto-table-stmts>
119+
<make-goto-table-cont>
120+
(while (E) S Ss) @Stmts Cont
121+
</make-goto-table-cont>
122+
</make-goto-table-task>
123+
)
124+
...</make-goto-table-tasks>
125+
[structural]
126+
rule
127+
<make-goto-table-stmts>
128+
{ Ss1:Stmts } Ss2:Stmts => Ss1 @Stmts Ss2
129+
</make-goto-table-stmts>
130+
[structural]
131+
rule
132+
<make-goto-table-stmts>
133+
KLabel:KLabel(KList:KList) Ss:Stmts => Ss
134+
</make-goto-table-stmts>
135+
when isStmt(KLabel:KLabel(KList:KList))
136+
andBool KLabel =/=KLabel '_:_
137+
andBool KLabel =/=KLabel 'if`(_`)_else_
138+
andBool KLabel =/=KLabel 'while`(_`)_
139+
andBool KLabel =/=KLabel '`{_`}
140+
[structural]
141+
39142
// This is a somewhat hackish method for evaluating the constant expressions
40143
// in the case labels of switch statements. It seems to slow things down
41144
// quite a bit too.
42145
syntax K ::= "eval-case-labels"
43146
| "eval-case-labels" "(" Map "," Map ")"
44147
| "case-label-freezer" "(" Int "," Stmts ")"
45-
rule <k> eval-case-labels => . ...</k>
46-
<frame-fun-id> F:Ptr </frame-fun-id>
47-
<fun-id> F </fun-id>
48-
<labels-evaluated> true </labels-evaluated>
148+
49149
rule <k> eval-case-labels => eval-case-labels(Lbls, .Map) ...</k>
50150
<frame-fun-id> F:Ptr </frame-fun-id>
51151
<fun-id> F </fun-id>
52152
<labels> Lbls:Map </labels>
53-
<labels-evaluated> false </labels-evaluated>
54153

55154
rule eval-case-labels(
56155
_:Map (L:Label |-> Ss:Stmts => .Map),
@@ -70,7 +169,6 @@ module CIL-STMT
70169
<frame-fun-id> F:Ptr </frame-fun-id>
71170
<fun-id> F </fun-id>
72171
<labels> _ => Lbls </labels>
73-
<labels-evaluated> false => true </labels-evaluated>
74172
[structural]
75173

76174
syntax K ::= "$exp2bool" "(" Exp ")" [strict]

cil-semantics/cil-syntax.k

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module CIL-SYNTAX
1414
| "union" CId "{" VarDecls "}" ";"
1515
| "struct" CId ";" // forward decls
1616
| "union" CId ";"
17+
| "enum" CId ";"
1718

1819
syntax TypedefDecl ::= "typedef" DeclSpecs Declarator ";"
1920

@@ -24,6 +25,7 @@ module CIL-SYNTAX
2425
// It appears that CIL substitutes constants for the enum tags, but leaves
2526
// the declaration, so I think it can be safely elided.
2627
rule enum _:CId { _:EnumInits } ; => . [macro]
28+
rule enum _:CId ; => . [macro]
2729

2830
syntax Type ::= ParamDecl // TODO(chathhorn)
2931
// syntax Type ::= Void
@@ -386,6 +388,7 @@ module CIL-SYNTAX
386388
syntax AggTypeSpec ::=
387389
"struct" CId
388390
| "union" CId
391+
| "enum" CId
389392

390393
// atomic-type-specifier
391394
// _Atomic ( type-name )

cil-semantics/cil.k

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ module CIL
3434
<formals> .Params </formals>
3535
<body> .K </body> // FunBody
3636
<labels> .Map </labels> // Map{Label, Stmt}
37-
// For lazy evaluation of constant expressions in case labels.
38-
<labels-evaluated> false </labels-evaluated>
37+
// For lazy label generation.
38+
<labels-generated> false </labels-generated>
3939
</fun>
4040
</funs>
4141
<global-env> .Map </global-env> // Map{CId, Ptr}

0 commit comments

Comments
 (0)