-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathStatements.tex
More file actions
2256 lines (1855 loc) · 77 KB
/
Statements.tex
File metadata and controls
2256 lines (1855 loc) · 77 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\chapter{Statements\label{chap:Statements}}
Statements update storage elements and determine the flow of control in a subprogram.
\ExampleDef{Statements}
\listingref{Statements} shows some examples of statements.
\ASLListing{Examples of statements}{Statements}{\definitiontests/Statements.asl}
\ChapterOutline
\begin{itemize}
\item \FormalRelationsRef{Statements} defines the formal relations used for statements;
\item \secref{PassStatements} defines pass statements;
\item \secref{AssignmentStatements} defines assignment statements;
\item \secref{SetterAssignmentStatements} defines setter assignment statements;
\item \secref{DeclarationStatements} defines declaration statements;
\item \secref{DeclarationStatementsElidedParameter} defines declaration statements with an elided parameter;
\item \secref{SequencingStatement} defines sequencing statements;
\item \secref{CallStatements} defines call statements;
\item \secref{ConditionalStatements} defines conditional statements;
\item \secref{CaseStatements} defines case statements;
\item \secref{AssertionStatements} defines assertion statements;
\item \secref{WhileStatements} defines while statements;
\item \secref{RepeatStatements} defines repeat statements;
\item \secref{ForStatements} defines for-looping statements;
\item \secref{ThrowStatements} defines throw statements;
\item \secref{TryStatements} defines try statements;
\item \secref{ReturnStatements} defines return statements;
\item \secref{PrintStatements} defines print statements;
\item \secref{UnreachableStatement} defines unreachable statements;
\item \secref{PragmaStatements} defines pragma statements.
\end{itemize}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\FormalRelationsDef{Statements}
\paragraph{Syntax:} Statements are grammatically derived from $\Nstmt$.
\paragraph{Abstract Syntax:} Statements are derived in the abstract syntax from $\stmt$
and generated by $\buildstmt$.
\NotImportedToASLSpecYet{
\hypertarget{build-stmt}{}
The function
\[
\buildstmt(\overname{\parsenode{\Nstmt}}{\vparsednode}) \;\aslto\; \overname{\stmt}{\vastnode}
\]
transforms a statement parse node $\vparsednode$ into a statement AST node $\vastnode$.
} % END_OF_UNIMPORTED_RELATION
\paragraph{Typing:} Statements are annotated by $\annotatestmt$.
\RenderRelation{annotate_stmt}
\paragraph{Semantics:} Statements are evaluated by $\evalstmt$.
\RenderRelation{eval_stmt}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Pass Statements\label{sec:PassStatements}}
\hypertarget{def-passstatementterm}{}
\ASLListing{A \texttt{pass} statement}{semantics-spass}{\semanticstests/SemanticsRule.SPass.asl}
\subsection{Syntax}
\begin{flalign*}
\Nstmt \derives \ & \Tpass \parsesep \Tsemicolon &
\end{flalign*}
\subsection{Abstract Syntax}
\RenderTypes[remove_hypertargets]{stmt_pass}
\ASTRuleDef{SPass}
\begin{mathpar}
\inferrule{}{
\buildstmt(\overname{\Nstmt(\Tpass, \Tsemicolon)}{\vparsednode})
\astarrow
\overname{\SPass}{\vastnode}
}
\end{mathpar}
\subsection{Typing}
\TypingRuleDef{SPass}
\ExampleDef{Typing a Pass Statement}
Annotating the \passstatementterm{} in \listingref{semantics-spass} does
not modify the \staticenvironmentterm{}.
\RenderProseAndFormally{annotate_stmt_SPass}
\CodeSubsection{\SPassBegin}{\SPassEnd}{../Typing.ml}
\subsection{Semantics}
\SemanticsRuleDef{SPass}
\ExampleDef{Evaluation of Pass Statements}
In \listingref{semantics-spass}, \texttt{pass;} does not modify the environment.
\RenderProseAndFormally{eval_stmt_SPass}
\CodeSubsection{\EvalSPassBegin}{\EvalSPassEnd}{../Interpreter.ml}
\hypertarget{def-assignmentstatementterm}{}
\section{Assignment Statements\label{sec:AssignmentStatements}}
\subsection{Syntax}
\begin{flalign*}
\Nstmt \derives \ & \Nlexpr \parsesep \Teq \parsesep \Nexpr \parsesep \Tsemicolon &
\end{flalign*}
\subsection{Abstract Syntax}
\RenderTypes[remove_hypertargets]{stmt_assign}
\ASTRuleDef{SAssign}
\begin{mathpar}
\inferrule{}{
\buildstmt(\overname{\Nstmt(\punnode{\Nlexpr}, \Teq, \punnode{\Nexpr}, \Tsemicolon)}{\vparsednode})
\astarrow
\overname{\SAssign(\astof{\vlexpr}, \astof{\vexpr})}{\vastnode}
}
\end{mathpar}
\subsection{Typing}
\TypingRuleDef{SAssign}
\ExampleDef{Typing an Assignment Statement}
In \listingref{semantics-sassign}, the assignment \verb|x = 3;|
is well-typed.
\RenderProseAndFormally{annotate_stmt_SAssign}
\CodeSubsection{\SAssignBegin}{\SAssignEnd}{../Typing.ml}
\subsection{Semantics}
There are two rules for evaluating assignments:
\begin{itemize}
\item \SemanticsRuleRef{SAssignCall} handles assignments where the \rhsexpression{}
is a \callexpressionterm{} and the left-hand-side expression is a tuple.
\item \SemanticsRuleRef{SAssign} handles all other assignments.
\end{itemize}
Although the sequential semantics of both types of statements is the same,
their concurrent semantics are different.
\SemanticsRuleRef{SAssignCall} generates a different execution graph ---
one where each value of the left-hand-side tuple depends on the \executiongraphterm{}
for the corresponding (that is, at the same position) value returned from the \callexpressionterm{}.
In contrast, the \executiongraphterm{} generated by \SemanticsRuleRef{SAssign}
creates dependencies between the entire \executiongraphterm{} for the evaluated right-hand-side
expression and the entire \executiongraphterm{} for the left-hand-side \executiongraphterm{}.
The rules for assignments first produce a value for the right-hand side expression
and then complete the update to the environment via an appropriate rule for evaluating the
\assignableexpression{} on the left-hand-side of the assignment.
The rules for updating the \assignableexpression{} handle variables, tuples, bitvectors, etc.
\SemanticsRuleDef{SAssignCall}
\ExampleDef{Evaluation of Multi-variable Assignment from Subprogram Calls}
In \listingref{assigncallsemantics}, given that the function call \texttt{f(1)} returns a triple of values ---
$\nvint(1)$, $\nvint(2)$, and $\nvint(3)$
(each with its own associated execution graph),
the statement \texttt{(a,b,-) = f(1)} assigns the value $\nvint(1)$ to the mutable variable \texttt{a},
$\nvint(2)$ to the mutable variable~\texttt{b}, and discards $\nvint(3)$.
\ASLListing{Assignment from a call expression.}{assigncallsemantics}{\semanticstests/SemanticsRule.SAssignCall.asl}
\RenderProseAndFormally{eval_stmt_SAssignCall}
\CodeSubsection{\EvalSAssignCallBegin}{\EvalSAssignCallEnd}{../Interpreter.ml}
\SemanticsRuleDef{SAssign}
\ExampleDef{Evaluation of Assignment Statements}
In \listingref{semantics-sassign},
\texttt{x = 3;} binds \texttt{x} to $\nvint(3)$ in the environment where \texttt{x} is bound to
$\nvint(42)$, and $\newenv$ is such that \texttt{x} is bound to $\nvint(3)$.
\ASLListing{Evaluating an assignment}{semantics-sassign}{\semanticstests/SemanticsRule.SAssign.asl}
\RenderProseAndFormally{eval_stmt_SAssign}
\CodeSubsection{\EvalSAssignBegin}{\EvalSAssignEnd}{../Interpreter.ml}
\SemanticsRuleDef{LexprIsVar}
\RenderRelation{lexpr_is_var}
In \ExampleRef{Evaluation of Multi-variable Assignment from Subprogram Calls},
the \assignableexpressions{} \verb|a|, \verb|b| are variables,
and \verb|-| is a discarded \assignableexpression{}.
\RenderProseAndFormally{lexpr_is_var}
\section{Setter Assignment Statements\label{sec:SetterAssignmentStatements}}
\subsection{Syntax}
\begin{flalign*}
\Nstmt \derives \
& \Ncall \parsesep \Nsetteraccess \parsesep \Teq \parsesep \Nexpr \parsesep \Tsemicolon &\\
|\ & \Ncall \parsesep \Nsetteraccess \parsesep \Nslices \parsesep \Teq \parsesep \Nexpr \parsesep \Tsemicolon &\\
|\ & \Ncall \parsesep \Tdot \parsesep \Tlbracket \parsesep \Clisttwo{{\Tidentifier}} \parsesep \Trbracket \parsesep \Teq \parsesep \Nexpr \parsesep \Tsemicolon &\\
\Nsetteraccess \derives \
& \emptysentence &\\
|\ & \Tdot \parsesep \Tidentifier \parsesep \Nsetteraccess &
\end{flalign*}
\SyntacticSugarDef{SetterFieldAssignment}
If a setter is followed by a slice or field access, then the assignment to that bitslice is
implemented using the following Read-Modify-Write (RMW) behaviour:
\begin{itemize}
\item invoking the getter of the same name as the setter, with the same actual
arguments as the setter invocation, excluding the right-hand side of the assignment;
\item performing the assignment to the bitslice or field of the result of the
getter invocation; and
\item invoking the setter to assign the resulting value.
\end{itemize}
This is implemented by desugaring via \ASTRuleRef{DesugarSetter}.
%
See \ExampleRef{A Setter Call as a Read-Modify-Write}.
\ASTRuleDef{MakeSetter}
\NotImportedToASLSpecYet{
\hypertarget{def-makesetter}{}
The function
\[
\makesetter(
\overname{\call}{\vcall} \aslsep
\overname{\expr}{\varg}) \aslto \overname{\call}{\vcallp}
\]
constructs a setter call $\vcallp$ using a base call $\vcall$ and right-hand side $\varg$.
} % END_OF_UNIMPORTED_RELATION
\begin{mathpar}
\inferrule{}{
\makesetter(\vcall, \varg) \aslto
\overname{\left\{
\begin{array}{rcl}
\callname &:& \vcall.\callname,\\
\callparams &:& \vcall.\callparams,\\
\callargs &:& [\varg]\concat\vcall.\callargs,\\
\callcalltype &:& \STSetter
\end{array}
\right\}}{\vcallp}
}
\end{mathpar}
\ASTRuleDef{DesugarSetter}
\NotImportedToASLSpecYet{
\hypertarget{def-desugarsetter}{}
The function
\[
\desugarsetter(
\overname{\call}{\vcall} \aslsep
\overname{\lhsaccess}{\vlhsaccess} \aslsep
\overname{\expr}{\rhs}) \aslto \overname{\stmt}{\news}
\]
builds a statement $\news$ from an assignment of expression $\rhs$ to a setter invocation $\vcall.\callname$ with accesses given by $\vlhsaccess$.
} % END_OF_UNIMPORTED_RELATION
\begin{mathpar}
\inferrule[empty]{
\vlhsaccess.\lhsaccessaccess = \emptylist \\
\vlhsaccess.\lhsaccessslices = \emptylist \hva\\\\
\makesetter(\vcall, \rhs) \aslto \vcallp
}{
\desugarsetter(\vcall, \vlhsaccess, \rhs)
\astarrow
\SCall(\vcallp)
}
\end{mathpar}
\begin{mathpar}
\inferrule[non\_empty]{
\vlhsaccess.\lhsaccessaccess \neq \emptylist \lor
\vlhsaccess.\lhsaccessslices \neq \emptylist \hva\\\\
\vx \in \Identifier \text{ is fresh} \hva\\\\
\desugarlhsaccess(\vx, \vlhsaccess) \astarrow \lhs \\
\vmodify \eqdef \SAssign(\lhs, \rhs) \\
\readmodifywrite(\vcall, \vx, \vmodify) \astarrow \vcallp
}{
\desugarsetter(\vcall, \vlhsaccess, \rhs)
\astarrow
\vcallp
}
\end{mathpar}
\ASTRuleDef{DesugarSetterSetfields}
\NotImportedToASLSpecYet{
\hypertarget{def-desugarsettersetfields}{}
The function
\[
\desugarsettersetfields(
\overname{\call}{\vcall} \aslsep
\overname{\KleeneStar{\Identifier}}{\vfields} \aslsep
\overname{\expr}{\rhs}) \aslto \overname{\stmt}{\news}
\]
builds a statement $\news$ from an assignment of $\rhs$ to a setter invocation $\vcall.\callname$ with concatenated field accesses given by $\vfields$.
} % END_OF_UNIMPORTED_RELATION
\begin{mathpar}
\inferrule{
\vx \in \Identifier \text{ is fresh} \hva\\\\
\lhs \eqdef \LESetFields(\LEVar(\vx), \vfields) \\
\vmodify \eqdef \SAssign(\lhs, \rhs) \\
\readmodifywrite(\vcall, \vx, \vmodify) \astarrow \vcallp
}{
\desugarsettersetfields(\vcall, \fields, \rhs)
\astarrow
\vcallp
}
\end{mathpar}
\ASTRuleDef{ReadModifyWrite}
\NotImportedToASLSpecYet{
\hypertarget{def-readmodifywrite}{}
The function
\[
\readmodifywrite(
\overname{\call}{\vcall} \aslsep
\overname{\Identifier}{\vx} \aslsep
\overname{\stmt}{\vmodify}) \aslto \overname{\stmt}{\news}
\]
builds a sequence of statements to read from the getter invocation $\vcall.\callname$, modify the resulting value using $\vmodify$, and write it to a setter invocation $\vcall.\callname$.
} % END_OF_UNIMPORTED_RELATION
\ExampleDef{A Setter Call as a Read-Modify-Write}
\listingref{ReadModifyWriteSlice} shows how a setter call followed by a slice is
\desugared.
\ASLListing{A Setter followed by a Slice}{ReadModifyWriteSlice}{\syntaxtests/ASTRule.ReadModifyWriteSlice.asl}
\listingref{ReadModifyWriteField} shows how a setter call followed by a field access is
\desugared.
\ASLListing{A Setter followed by a Field Access}{ReadModifyWriteField}{\syntaxtests/ASTRule.ReadModifyWriteField.asl}
\begin{mathpar}
\inferrule{
\setcalltype(\vcall, \STGetter) \aslto \vgetter \hva\\\\
\vread \eqdef \SDecl(\LDKVar, \LDIVar(\vx), \none, \some{\ECall(\vgetter)}) \hva\\\\
\makesetter(\vcall, \EVar(\vx)) \aslto \vsetter\\
\news \eqdef \SSeq (\SSeq(\vread, \vmodify), \SCall(\vsetter))
}{
\readmodifywrite(\vcall, \vx, \vmodify) \astarrow \news
}
\end{mathpar}
\ASTRuleDef{SetterAssign}
\begin{mathpar}
\inferrule[no\_slices]{
\buildaccess(\vsetteraccess) \astarrow \vaccess \hva\\\\
{
\vlhsaccess \eqdef
\left\{
\begin{array}{rcl}
\lhsaccessaccess &:& \vaccess,\\
\lhsaccessslices &:& \emptylist
\end{array}
\right\}
} \\
\desugarsetter(\astof{\vcall}, \vlhsaccess, \astof{\vexpr}) \astarrow \vastnode
}{
\buildstmt(\overname{\Nstmt(\punnode{\Ncall}, \namednode{\vsetteraccess}{\Nsetteraccess}, \Teq, \punnode{\Nexpr}, \Tsemicolon
)}{\vparsednode})
\astarrow \vastnode
}
\end{mathpar}
\begin{mathpar}
\inferrule[slices]{
\buildaccess(\vsetteraccess) \astarrow \vaccess \hva\\\\
{
\vlhsaccess \eqdef
\left\{
\begin{array}{rcl}
\lhsaccessaccess &:& \vaccess,\\
\lhsaccessslices &:& \astof{\vslices}
\end{array}
\right\}
} \\
\desugarsetter(\astof{\vcall}, \vlhsaccess, \astof{\vexpr}) \astarrow \vastnode
}{
{
\begin{array}{r}
\buildstmt(\overname{\Nstmt(\punnode{\Ncall}, \namednode{\vsetteraccess}{\Nsetteraccess}, \punnode{\Nslices}, \Teq, \punnode{\Nexpr}, \Tsemicolon
)}{\vparsednode})
\astarrow \\ \vastnode
\end{array}
}
}
\end{mathpar}
\begin{mathpar}
\inferrule[setfields]{
\buildclist[\buildidentity](\vfields) \astarrow \vfieldasts \\
\desugarsettersetfields(\astof{\vcall}, \vfieldasts, \astof{\vexpr}) \astarrow \vastnode
}{
\buildstmt(\overname{\Nstmt(\punnode{\Ncall}, \Tdot,
\Tlbracket, \namednode{\vfields}{\Clisttwo{\Tidentifier}}, \Trbracket, \Teq, \punnode{\Nexpr}, \Tsemicolon
)}{\vparsednode})
\astarrow \vastnode
}
\end{mathpar}
\subsection{Typing and semantics}
As given by applying the relevant rules to the desugared AST.
\hypertarget{def-declarationstatementterm}{}
\section{Declaration Statements\label{sec:DeclarationStatements}}
\subsection{Syntax}
\begin{flalign*}
\Nstmt \derives \ & \Nlocaldeclkeyword \parsesep \Ndeclitem \parsesep \option{\Nasty} \parsesep \Teq \parsesep \Nexpr \parsesep \Tsemicolon &\\
|\ & \Tvar \parsesep \Ndeclitem \parsesep \Nasty \parsesep \Tsemicolon &\\
|\ & \Tvar \parsesep \Clisttwo{\Tidentifier} \parsesep \Nasty \parsesep \Tsemicolon &\\
\end{flalign*}
\subsection{Abstract Syntax}
\RenderTypes[remove_hypertargets]{stmt_decl}
\ASTRuleDef{SDecl}
\begin{mathpar}
\inferrule[let\_constant]{
{
\begin{array}{l}
\vparsednode =\\ \Nstmt(\Nlocaldeclkeyword, \Ndeclitem, \namednode{\vt}{\option{\Nasty}}, \Teq, \punnode{\Nexpr}, \Tsemicolon)\\
\end{array}
}\hva\\
\buildoption[\buildasty](\vt) \astarrow \astversion{\vt}\\
\vastnode \eqdef \SDecl(\astof{\vlocaldeclkeyword}, \astof{\vdeclitem}, \astversion{\vt}, \some{\astof{\vexpr}})
}{
\buildstmt(\vparsednode) \astarrow \vastnode
}
\end{mathpar}
\begin{mathpar}
\inferrule[uninit\_var]{}{
{
\begin{array}{r}
\buildstmt(\overname{\Nstmt(\Tvar, \Ndeclitem, \punnode{\Nasty}, \Tsemicolon)}{\vparsednode})
\astarrow \\
\overname{\SDecl(\LDKVar, \astof{\vdeclitem}, \astof{\vasty}, \none)}{\vastnode}
\end{array}
}
}
\end{mathpar}
\begin{mathpar}
\inferrule[multi\_var]{
\buildclist[\buildidentity](\vids) \astarrow \astversion{\vids}\\
\buildasty(\vt) \astarrow \astversion{\vt} \hva\\
\vstmts \eqdef [\vx\in\astversion{\vids}: \SDecl(\LDKVar, \vx, \astversion{\vt}, \none)]\\
\stmtfromlist(\vstmts) \astarrow \vastnode
}{
\buildstmt(\overname{\Nstmt(\Tvar, \namednode{\vids}{\Clisttwo{\Tidentifier}}, \namednode{\vt}{\Nasty}, \Tsemicolon)}{\vparsednode})
\astarrow
\vastnode
}
\end{mathpar}
\subsection{Typing}
\TypingRuleDef{SDecl}
\ExampleDef{Typing Declaration Statements}
\listingref{typing-sdecl} shows well-typed declaration statements.
\ASLListing{Typing declaration statements}{typing-sdecl}{\typingtests/TypingRule.SDecl.asl}
The specifications in \listingref{typing-sdecl-bad1} ill-typed,
since local constant storage elements are not allowed.
\ASLListing{A (illegal) local constant declaration}{typing-sdecl-bad1}{\typingtests/TypingRule.SDecl.bad1.asl}
The specification in \listingref{typing-sdecl-bad2} is ill-typed,
since immutable local storage elements require an initializing expression.
\ASLListing{An uninitialized immutable local storage declaration}{typing-sdecl-bad2}{\typingtests/TypingRule.SDecl.bad2.asl}
\RenderProseAndFormally{annotate_stmt_SDecl}
\CodeSubsection{\SDeclegin}{\SDeclEnd}{../Typing.ml}
\TypingRuleDef{AnnotateLocalDeclTypeAnnot}
\RenderRelation{annotate_local_decl_type_annot}
See \ExampleRef{Typing Declaration Statements}.
\RenderProseAndFormally{annotate_local_decl_type_annot}
\TypingRuleDef{InheritIntegerConstraints}
\RenderRelation{inherit_integer_constraints}
\listingref{typing-pendingconstrained} shows examples of pending-constrained
integer types.
\ExampleDef{Pending-constrained integer type vs. unconstrained integer type}
\listingref{pending-constrained-unconstrained} corresponds to the \typingerrorterm{}
in the \CaseName{int} below.
\ASLListing{An ill-typed pending-constrained integer type}{pending-constrained-unconstrained}
{\typingtests/TypingRule.InheritIntegerConstraints.unconstrained.bad.asl}
\RenderProseAndFormally{inherit_integer_constraints}
\TypingRuleDef{CheckNoPrecisionLoss}
\RenderRelation{check_no_precision_loss}
\ExampleDef{Rejected Declaration Because of Precision Loss}
In \listingref{typing-imprecisetype}, the statement \verb|var b = a * a;|
corresponds to the \typingerrorterm{} raised in the \CaseName{Well-Constrained}
below.
The type of the right-hand-side (\texttt{a * a}) is imprecise because the multiplication of two
constant types would result in more than $\maxconstraintsize$ elements, see
\TypingRuleRef{ApplyBinopTypes}.
\ASLListing{Type-checking a declaration with an imprecise type}{typing-imprecisetype}{\typingtests/TypingRule.CheckNoPrecisionLoss.asl}
\RenderProseAndFormally{check_no_precision_loss}
\CodeSubsection{\CheckNoPrecisionLossBegin}{\CheckNoPreciisonLossEnd}{../Typing.ml}
\TypingRuleDef{CheckCanBeInitializedWith}
\RenderRelation{check_can_be_initialized_with}
See \ExampleRef{Type-satisfaction Examples}.
\RenderProseAndFormally{check_can_be_initialized_with}
\subsection{Semantics}
\SemanticsRuleDef{SDecl}
Notice that \TypingRuleRef{SDecl} ensures that a typed \declarationstatementterm{}
has an initializing expression.
% The implementation still handles the case where there is no initializing expression,
% for cases where type-checking is skipped.
\ExampleDef{Declaration With an Initializing Value}
In \listingref{semantics-sdeclsome},
\texttt{let x = 3;} binds \texttt{x} to $\nvint(3)$ in the empty environment.
\ASLListing{Evaluating a declaration with a given initial value}{semantics-sdeclsome}{\semanticstests/SemanticsRule.SDeclSome.asl}
\ExampleDef{Declaration Without an Initializing Value}
In \listingref{semantics-sdeclnone},
\texttt{var x : integer;} binds \texttt{x} in $\env$ to the base value of \texttt{integer}.
\ASLListing{Evaluating a declaration without a given initial value}{semantics-sdeclnone}{\semanticstests/SemanticsRule.SDeclNone.asl}
\RenderProseAndFormally{eval_stmt_SDecl}
\CodeSubsection{\EvalSDeclBegin}{\EvalSDeclEnd}{../Interpreter.ml}
\section{Declaration Statements with an Elided Parameter\label{sec:DeclarationStatementsElidedParameter}}
\subsection{Syntax}
\begin{flalign*}
\Nstmt \derives \
& \Nlocaldeclkeyword \parsesep \Ndeclitem \parsesep \Nasty \parsesep \Teq \\
& \wrappedline\ \Nelidedparamcall \parsesep \Tsemicolon &\\
\end{flalign*}
\begin{flalign*}
\Nelidedparamcall \derives \
& \Tidentifier \parsesep \Tlbrace \parsesep \Trbrace &\\
|\ & \Tidentifier \parsesep \Tlbrace \parsesep \Trbrace \parsesep \PlistZero{\Nexpr} &\\
|\ & \Tidentifier \parsesep \Tlbrace \parsesep \Tcomma \parsesep \ClistOne{\Nexpr} \parsesep \Trbrace &\\
|\ & \Tidentifier \parsesep \Tlbrace \parsesep \Tcomma \parsesep \ClistOne{\Nexpr} \parsesep \Trbrace \parsesep \PlistZero{\Nexpr}&
\end{flalign*}
\SyntacticSugarDef{ParameterElision}
In a call to a parameterized subprogram, a parameter can be elided,
if it can be inferred from the left-hand-side type annotation,
as detailed by \ASTRuleRef{ElidedParamCall} (See also \secref{ParameterElision}).
\ASTRuleDef{ElidedParamCall}
\hypertarget{build-elided-param-call}{}
The function $\buildelidedparamcall$ builds a $\call$ from a parsed $\Nelidedparamcall$.
\begin{mathpar}
\inferrule{
\buildcall(\Ncall(\Tidentifier(\id), \Tlpar, \Trpar))
\astarrow \vastnode
}{
\buildelidedparamcall(\Nelidedparamcall (\Tidentifier(\id), \Tlbrace, \Trbrace))
\astarrow \vastnode
}
\end{mathpar}
\begin{mathpar}
\inferrule{
\vparsednode = \Nelidedparamcall(\Tidentifier(\id), \Tlbrace, \Trbrace, \namednode{\vargs}{\PlistZero{\Nexpr}})\\
\buildcall(\Ncall(\Tidentifier(\id), \vargs))
\astarrow \vastnode
}{
\buildelidedparamcall(\vparsednode) \astarrow \vastnode
}
\end{mathpar}
\begin{mathpar}
\inferrule{
\vparsednode = \Nelidedparamcall(\Tidentifier(\id), \Tlbrace, \Tcomma, \namednode{\vparams}{\ClistOne{\Nexpr}}, \Trbrace)\\
\buildcall(\Ncall(\Tidentifier(\id), \Tlbrace, \vparams, \Trbrace))
\astarrow \vastnode
}{
\buildelidedparamcall(\vparsednode) \astarrow \vastnode
}
\end{mathpar}
\begin{mathpar}
\inferrule{
{
\vparsednode = \Nelidedparamcall\left(
\begin{array}{l}
\Tidentifier(\id), \Tlbrace, \Tcomma, \namednode{\vparams}{\ClistOne{\Nexpr}}, \Trbrace, \\
\wrappedline\ \namednode{\vargs}{\PlistZero{\Nexpr}}
\end{array}
\right)
}\hva\\
\buildcall(\Ncall(\Tidentifier(\id), \Tlbrace, \vparams, \Trbrace, \vargs))
\astarrow \vastnode
}{
\buildelidedparamcall(\vparsednode) \astarrow \vastnode
}
\end{mathpar}
\ASTRuleDef{DesugarElidedParameter}
\NotImportedToASLSpecYet{
\hypertarget{def-desugarelidedparameter}{}
The function
\[
\desugarelidedparameter(
\overname{\ty}{\vt} \aslsep
\overname{\call}{\vcall})
\aslto
\overname{\Option{\expr}}{\vexpr}
\cup\ \overname{\TBuildError}{\ParseErrorConfig}
\]
builds an expression $\vexpr$ from an assignment of the call $\vcall$ to the left-hand side with type annotation $\vt$, where the call has an elided parameter.
Otherwise, the result is a parse error.
} % END_OF_UNIMPORTED_RELATION
\ExampleDef{Desugaring Parameter Elision Based on Declared Type Annotation}
\listingref{DesugarElidedParameter} shows examples of how parameters can be elided
if they can be copied from the type annotation in declaration statements.
\ASLListing{Desugaring parameter elision based on declared type annotation}{DesugarElidedParameter}{\syntaxtests/ASTRule.DesugarElidedParameter.asl}
\begin{mathpar}
\inferrule{
\becheck(\vt = \TBits(\Ignore, \Ignore), \ParseError) \typearrow \True \;\terminateas \ParseErrorConfig \\
\vt \reverseeqdef \TBits(\ve, \Ignore) \\
\vcallp \eqdef \vcall[\callparams\mapsto [\ve] \concat \vcall.\callparams ]
}{
\desugarelidedparameter(\ldk, \ldi, \vt, \vcall)
\astarrow
\some{\ECall(\vcallp)}
}
\end{mathpar}
\ASTRuleDef{ElidedParamDecl}
\begin{mathpar}
\inferrule{
\buildelidedparamcall (\vcall) \astarrow \astversion{\vcall} \\
\desugarelidedparameter(\astof{\vasty}, \astversion{\vcall}) \astarrow \vexpr
}{
{
\begin{array}{r}
\buildstmt\left(
\Nstmt\left(
\begin{array}{l}
\punnode{\Nlocaldeclkeyword}, \\
\wrappedline\ \punnode{\Ndeclitem}, \punnode{\Nasty}, \Teq, \\
\wrappedline\ \namednode{\vcall}{\Nelidedparamcall}, \Tsemicolon
\end{array}
\right)
\right) \astarrow \\ \overname{\SDecl(\astof{\vlocaldeclkeyword}, \astof{\vdeclitem}, \some{\vasty}, \vexpr)}{\vastnode}
\end{array}
}
}
\end{mathpar}
\subsection{Typing and semantics}
As given by the applying the relevant rules to the desugared AST (see \secref{DeclarationStatements}).
\section{Sequencing Statements\label{sec:SequencingStatement}}
\hypertarget{def-sequencestatementterm}{}
\ASLListing{A sequence of statements}{semantics-sseq}{\semanticstests/SemanticsRule.SSeq.asl}
\subsection{Syntax}
\begin{flalign*}
\Nstmtlist \derives \ & \ListOne{\Nstmt} &
\end{flalign*}
\subsection{Abstract Syntax}
\RenderTypes[remove_hypertargets]{stmt_seq}
\ASTRuleDef{StmtList}
\NotImportedToASLSpecYet{
\hypertarget{build-stmtlist}{}
The function
\[
\buildstmtlist(\overname{\parsenode{\Nstmtlist}}{\vparsednode}) \;\aslto\; \overname{\stmt}{\vastnode}
\]
transforms a parse node $\vparsednode$ into an AST node $\vastnode$.
} % END_OF_UNIMPORTED_RELATION
\begin{mathpar}
\inferrule{
\buildlist[\Nstmt](\vstmts) \astarrow \vstmtlist\\
\stmtfromlist(\vstmtlist) \astarrow \vastnode
}{
\buildstmtlist(\Nstmtlist(\namednode{\vstmts}{\ListOne{\Nstmt}})) \astarrow \vastnode
}
\end{mathpar}
\ASTRuleDef{StmtFromList}
\NotImportedToASLSpecYet{
\hypertarget{def-stmtfromlist}{}
The function
\[
\stmtfromlist(\overname{\KleeneStar{\stmt}}{\vstmts}) \aslto \overname{\stmt}{\news}
\]
builds a statement $\news$ from a possibly-empty list of statements $\vstmts$.
} % END_OF_UNIMPORTED_RELATION
\begin{mathpar}
\inferrule[empty]{
}{
\stmtfromlist(\overname{\emptylist}{\vstmts}) \astarrow \overname{\SPass}{\news}
}
\hva\and
\inferrule[non\_empty]{
\stmtfromlist(\vstmtsone) \astarrow \vsone\\
\sequencestmts(\vs, \vsone) \astarrow \news
}{
\stmtfromlist(\overname{[\vs] \concat \vstmtsone}{\vstmts}) \astarrow \news
}
\end{mathpar}
\ASTRuleDef{SequenceStmts}
\NotImportedToASLSpecYet{
\hypertarget{def-sequencestmts}{}
The function
\[
\sequencestmts(\overname{\stmt}{\vsone}, \overname{\stmt}{\vstwo}) \aslto \overname{\stmt}{\news}
\]
Combines the statement $\vsone$ with $\vstwo$ into the statement $\news$, while filtering away
instances of $\SPass$.
} % END_OF_UNIMPORTED_RELATION
\begin{mathpar}
\inferrule[s1\_spass]{}{
\sequencestmts(\overname{\SPass}{\vsone}, \vstwo) \astarrow \overname{\vstwo}{\news}
}
\hva\and
\inferrule[s2\_spass]{
\vsone \neq \SPass
}{
\sequencestmts(\vsone, \overname{\SPass}{\vstwo}) \astarrow \overname{\vsone}{\news}
}
\hva\and
\inferrule[no\_spass]{
\vsone \neq \SPass\\
\vstwo \neq \SPass
}{
\sequencestmts(\vsone, \vstwo) \astarrow \overname{\SSeq(\vsone, \vstwo)}{\news}
}
\end{mathpar}
\subsection{Typing}
\TypingRuleDef{SSeq}
\ExampleDef{Typing Sequencing Statements}
In \listingref{semantics-sseq}, the statement \verb|let x=3;| is
annotated first in the \staticenvironmentterm{} where the \localstaticenvironmentterm{} is empty.
Then, the statement \verb|let y = x + 1;|
is annotated in the \staticenvironmentterm{} where
\verb|x| has been declared and associated with the type \verb|integer{3}|,
and also recorded to be equivalent to $\LInt(3)$ (in the $\exprequiv$ map).
\RenderProseAndFormally{annotate_stmt_SSeq}
\CodeSubsection{\SSeqBegin}{\SSeqEnd}{../Typing.ml}
\subsection{Semantics}
\SemanticsRuleDef{SSeq}
\ExampleDef{Evaluation of Sequencing Statements}
In \listingref{semantics-sseq},
the evaluation of \texttt{let x = 3; let y = x + 1} first evaluates \texttt{let x = 3} and only then
evaluates \texttt{let y = x + 1}.
\RenderProseAndFormally{eval_stmt_SSeq}
\CodeSubsection{\EvalSSeqBegin}{\EvalSSeqEnd}{../Interpreter.ml}
\section{Call Statements\label{sec:CallStatements}}
\hypertarget{def-callstatementterm}{}
Call statements are used to invoke procedures and setters.
\ASLListing{Call Statements}{scall}{\semanticstests/SemanticsRule.SCall.asl}
\subsection{Syntax}
\begin{flalign*}
\Nstmt \derives \ & \Ncall \Tsemicolon &
\end{flalign*}
\subsection{Abstract Syntax}
\RenderTypes[remove_hypertargets]{stmt_call}
\ASTRuleDef{SCall}
\begin{mathpar}
\inferrule{
\buildcall(\vcall) \astarrow \astversion{\vcall} \\
\setcalltype(\astversion{\vcall}) \aslto \vcallp
}{
\buildstmt(
\overname{\Nstmt(\namednode{\vcall}{\Ncall}, \Tsemicolon)}{\vparsednode})
\astarrow
\overname{\SCall(\vcallp)}{\vastnode}
}
\end{mathpar}
\ASTRuleDef{SetCallType}
\hypertarget{def-setcalltype}{}
In \ASTRuleRef{Call}, $\STFunction$ is inserted as a default call type for any parsed $\call$.
The function
\[
\setcalltype(\overname{\call}{\vcall} \aslsep \overname{\subprogramtype}{\calltype}) \aslto \overname{\call}{\vcallp}
\]
changes the call type of $\vcall$ to $\calltype$.
\begin{mathpar}
\inferrule{}{
\setcalltype(\vcall, \calltype) \aslto
\overname{\vcall[\callcalltype\mapsto\calltype]}{\vcallp}
}
\end{mathpar}
\subsection{Typing}
\TypingRuleDef{SCall}
In \listingref{scall}, the call statement \verb|catenate_into_g{4, 3}(x, y, TRUE);|
is well-typed.
\ExampleDef{Ill-defined Call to a Function}
In \listingref{scall-bad}, the call statement \verb|zero();| is ill-typed,
since call statements are only allowed for procedures, not functions.
\ASLListing{An ill-defined call statement}{scall-bad}{\typingtests/TypingRule.SCall.bad.asl}
\RenderProseAndFormally{annotate_stmt_SCall}
\CodeSubsection{\SCallBegin}{\SCallEnd}{../Typing.ml}
\subsection{Semantics}
\SemanticsRuleDef{SCall}
\ExampleDef{Evaluation of Call Statements}
In \listingref{scall}, the call statement \verb|catenate_into_g{4, 3}(x, y, TRUE);|
assigns \verb|'1101 111'| to the global variable \verb|g|.
\RenderProseAndFormally{eval_stmt_SCall}
\CodeSubsection{\EvalSCallBegin}{\EvalSCallEnd}{../Interpreter.ml}
\hypertarget{def-conditionalstatementterm}{}
\section{Conditional Statements\label{sec:ConditionalStatements}}
A conditional statement evaluates its \texttt{then} statement list if the
condition expression evaluates to $\True$. If the condition expression
evaluates to $\False$ each \texttt{elsif} condition expression is evaluated
sequentially until an \texttt{elsif} condition expression evaluates to $\True$;
the conditional statement evaluates the corresponding \texttt{elsif}
statement list. If no \texttt{elsif} condition expression evaluates to $\True$, the
conditional statement evaluates the \texttt{else} statement list.
\subsection{Syntax}
\begin{flalign*}
\Nstmt \derives \ & \Tif \parsesep \Nexpr \parsesep \Tthen \parsesep \Nstmtlist \parsesep \Nselse \parsesep \Tend \parsesep \Tsemicolon &\\
\Nselse \derives\ & \Telseif \parsesep \Nexpr \parsesep \Tthen \parsesep \Nstmtlist \parsesep \Nselse &\\
|\ & \Telse \parsesep \Nstmtlist &\\
|\ & \emptysentence &
\end{flalign*}
\subsection{Abstract Syntax}
\RenderTypes[remove_hypertargets]{stmt_cond}
\ASTRuleDef{SCond}
\begin{mathpar}
\inferrule{}{
{
\begin{array}{r}
\buildstmt(\overname{\Nstmt(\Tif, \punnode{\Nexpr}, \Tthen, \punnode{\Nstmtlist}, \punnode{\Nselse}, \Tend, \Tsemicolon)}{\vparsednode})
\astarrow \\
\overname{\SCond(\astof{\vexpr}, \astof{\vstmtlist}, \astof{\velse})}{\vastnode}
\end{array}
}
}
\end{mathpar}
\ASTRuleDef{SElse}
\NotImportedToASLSpecYet{
\hypertarget{build-selse}{}
The function
\[
\buildselse(\overname{\parsenode{\Nselse}}{\vparsednode}) \;\aslto\; \overname{\stmt}{\vastnode}
\]
transforms a parse node $\vparsednode$ into an AST node $\vastnode$.
} % END_OF_UNIMPORTED_RELATION
\begin{mathpar}
\inferrule[elseif]{}{
{
\begin{array}{r}
\buildselse(\Nselse(\Telseif, \Nexpr, \Tthen, \Nstmtlist, \Nselse)) \astarrow \\
\overname{\SCond(\astof{\vexpr}, \astof{\vstmtlist}, \astof{\vselse})}{\vastnode}
\end{array}
}
}
\end{mathpar}
\begin{mathpar}
\inferrule[pass]{}{
\buildselse(\Nselse(\emptysentence)) \astarrow \overname{\SPass}{\vastnode}
}
\end{mathpar}
\begin{mathpar}
\inferrule[else]{}{
\buildselse(\Nselse(\Telse, \punnode{\Nstmtlist})) \astarrow \overname{\astof{\vstmtlist}}{\vastnode}
}
\end{mathpar}
\subsection{Typing}
\TypingRuleDef{SCond}
The specifications in \listingref{semantics-scond},
\listingref{semantics-scond2},
\listingref{semantics-scond3}, and
\listingref{semantics-scond4} are all well-typed.
\ExampleDef{Typing Conditional Statements with Two Bitvector Types}
The specification in \listingref{SCond} is well-typed
and shows examples of how \bitvectortypesterm{} either \typesatisfyterm{}
the return type or do not \typesatisfyterm{} the return type and require
an \atcexpressionterm{}.
\ASLListing{Typing conditional statements with two bitvector types}{SCond}{\typingtests/TypingRule.SCond.asl}
\RenderProseAndFormally{annotate_stmt_SCond}
\CodeSubsection{\SCondBegin}{\SCondEnd}{../Typing.ml}
\subsection{Semantics}
\SemanticsRuleDef{SCond}
\ExampleDef{Conditional Statements}
The specification in \listingref{semantics-scond} does not result in any Assertion Error.
\ASLListing{Evaluating a conditional statement}{semantics-scond}{\semanticstests/SemanticsRule.SCond.asl}
The specification in \listingref{semantics-scond2}
does not result in any error.
\ASLListing{Evaluating a condition statement with \texttt{elsif}}{semantics-scond2}{\semanticstests/SemanticsRule.SCond2.asl}
The specification in \listingref{semantics-scond3}
results in an Assertion Error.
\ASLListing{Evaluating a condition statement that results in an Assertion Error}{semantics-scond3}{\semanticstests/SemanticsRule.SCond3.asl}
The specification in \listingref{semantics-scond4} does not result in any error.
\ASLListing{Evaluating a condition statement with only a \texttt{then} branch}{semantics-scond4}{\semanticstests/SemanticsRule.SCond4.asl}
\RenderProseAndFormally{eval_stmt_SCond}
\CodeSubsection{\EvalSCondBegin}{\EvalSCondEnd}{../Interpreter.ml}
\hypertarget{def-casestatementterm}{}
\section{Case Statements\label{sec:CaseStatements}}
Case statements allow executing different statements, based on which
condition an expression satisfies.
\listingref{case-discriminant} shows an example of a \casestatementterm{}
and the output to a console when it is evaluated.
\ASLListing{A side-effecting case discriminant}{case-discriminant}{\definitiontests/CaseStatement.discriminant.asl}
% CONSOLE_BEGIN aslref \definitiontests/CaseStatement.discriminant.asl
\begin{Verbatim}[fontsize=\footnotesize, frame=single]
num_tests: 0
selected case x=51
\end{Verbatim}
% CONSOLE_END
\listingref{case-discriminant} shows an example of a \casestatementterm{}
and the output to a console when it is evaluated.
%
\listingref{CaseStatement.where} shows an example of a \casestatementterm{}
with a \texttt{where} guard expression.
\hypertarget{def-casediscriminantterm}{}
\hypertarget{def-casealternativeterm}{}
\hypertarget{def-otherwisecaseterm}{}
The expression following the \Tcase\ keyword is called the \emph{\casediscriminantterm}.
The list following the \Tof\ keyword consists of \emph{\casealternativesterm},
optionally ending with an \emph{\otherwisecaseterm}, which follows the \Totherwise\ keyword.
Case statements obey the following requirements:
\RequirementDef{CaseDiscriminant} The \casediscriminantterm\ of a \texttt{case}
statement should be evaluated only once each time the case statement is evaluated.
%
\listingref{case-discriminant} demonstrates how the \casediscriminantterm{}
is evaluated only once.
\RequirementDef{CaseAlternatives} The \casealternativesterm\ are examined
one after another, in the order they are listed.
If any of the patterns match the \casediscriminantterm{} (and the guard