forked from B-Lang-org/bsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSV_lang.tex
12882 lines (10512 loc) · 475 KB
/
BSV_lang.tex
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
% $Id$
%
\documentclass[twoside,letterpaper]{article}
% \usepackage{a4}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{latexsym}
\usepackage{makeidx}
\usepackage{verbatim}
\usepackage{moreverb}
\usepackage{index}
\usepackage{dingbat}
\usepackage{fancyhdr}
\usepackage{ifpdf}
% \newif\ifpdf
% \ifx\pdfoutput\undefined
% \else
% \ifx\pdfoutput\relax
% \else
% \ifcase\pdfoutput
% \else
% \pdftrue
% \fi
% \fi
% \fi
\ifpdf
\usepackage[pdftex,colorlinks=true,bookmarksopen, pdfstartview=FitH,
linkcolor=blue, citecolor=blue, urlcolor=blue]{hyperref}
\pdfcompresslevel=9
\usepackage[pdftex]{graphicx}
\else
\usepackage[dvips]{graphicx}
\fi
\usepackage{ae}
\usepackage{aecompl}
\usepackage{hyperref}
% HORIZONTAL MARGINS
% Left margin, odd pages: 1.25 inch (0.25 + 1)
\setlength{\oddsidemargin}{0.25in}
% Left margin, even pages: 1.25 inch (0 + 1)
\setlength{\evensidemargin}{0.25in}
% Text width 6 inch (so other margin is 1.25 inch).
\setlength{\textwidth}{6in}
% ----------------
% VERTICAL MARGINS
% Top margin 0.5 inch (-0.5 + 1)
\setlength{\topmargin}{-0.5in}
% Head height 0.25 inch (where page headers go)
\setlength{\headheight}{0.25in}
% Head separation 0.25 inch (between header and top line of text)
\setlength{\headsep}{0.25in}
% Text height 9 inch (so bottom margin 1 in)
\setlength{\textheight}{9in}
% ----------------
% PARAGRAPH INDENTATION
\setlength{\parindent}{0in}
% SPACE BETWEEN PARAGRAPHS
\setlength{\parskip}{\medskipamount}
% ----------------
% STRUTS
% HORIZONTAL STRUT. One argument (width).
\newcommand{\hstrut}[1]{\hspace*{#1}}
% VERTICAL STRUT. Two arguments (offset from baseline, height).
\newcommand{\vstrut}[2]{\rule[#1]{0in}{#2}}
% ----------------
% HORIZONTAL LINE ACROSS PAGE:
\newcommand{\hdivider}{\noindent\mbox{}\hrulefill\mbox{}}
% ----------------
% EMPTY BOXES OF VARIOUS WIDTHS, FOR INDENTATION
\newcommand{\hm}{\hspace*{1em}}
\newcommand{\hmm}{\hspace*{2em}}
\newcommand{\hmmm}{\hspace*{3em}}
\newcommand{\hmmmm}{\hspace*{4em}}
% ----------------
% ``TIGHTLIST'' ENVIRONMENT (no para space betwee items, small indent)
\newenvironment{tightlist}%
{\begin{list}{$\bullet$}{%
\setlength{\topsep}{0in}
\setlength{\partopsep}{0in}
\setlength{\itemsep}{0in}
\setlength{\parsep}{0in}
\setlength{\leftmargin}{1.5em}
\setlength{\rightmargin}{0in}
\setlength{\itemindent}{0in}
}
}%
{\end{list}
}
% ----------------
\newcommand{\ie}{\emph{i.e.,}}
%----------------
% Added to get special characters in index
\newcommand{\Caret}{\char94}
\newcommand{\Tilde}{\char126}
% ----------------
% BSV names
\newcommand{\LibRefGuide}{\emph{Libraries Reference Guide}}
\newcommand{\LibRefGuideFullName}{\emph{Bluespec Compiler (BSC) Libraries Reference Guide}}
\newcommand{\BSCUserGuide}{\emph{BSC User Guide}}
\newcommand{\BSCUserGuideFullName}{\emph{Bluespec Compiler (BSC) User Guide}}
\newcommand{\BS}{Bluespec}
\newcommand{\BSInc}{Bluespec, Inc.}
\newcommand{\BH}{BH}
\newcommand{\BSVFull}{Bluespec SystemVerilog}
\newcommand{\BSV}{BSV}
\newcommand{\bsc}{\emph{bsc}}
\newcommand{\SV}{SystemVerilog}
\newcommand{\SVThreeOneA}{SystemVerilog 3.1a}
\newcommand{\SC}{SystemC}
\newcommand{\V}{Verilog}
\newcommand{\veri}{Verilog}
\newcommand{\VOrig}{Verilog 1995}
\newcommand{\VTwoK}{Verilog 2001}
% ----------------------------------------------------------------
% CODE DISPLAYS.
% Bluespec code displays are enclosed between \BBS and \EBS
% Most characters are taken verbatim, in typewriter font,
% Except:
% Commands are still available (beginning with \)
% but use ` and ' instead of { and }
% Math mode is still available (beginning with $)
% but use ~ and ! for ^ and _
\outer\def\BBS{%
\begin{list}{$\bullet$}{%
\setlength{\topsep}{0in}
\setlength{\partopsep}{0in}
\setlength{\itemsep}{0in}
\setlength{\parsep}{0in}
\setlength{\leftmargin}{1em}
\setlength{\rightmargin}{0in}
\setlength{\itemindent}{0in}
}\item[]
% \catcode`\{=12
% \catcode`\}=12
\catcode`\&=12
\catcode`\#=12
\catcode`\%=12
\catcode`\~=12
% \catcode`\_=12
\catcode`\^=12
% \catcode`\~=7
% \catcode`\!=7 % superscript
% \catcode`\'=2
% \catcode`\`=1
\obeyspaces
\obeylines \tt}
\outer\def\EBS{%
\end{list}
}
{\obeyspaces\gdef {\ }}
% ----------------------------------------------------------------
% The following hack is from Mark Tuttle
\newcommand{\ttsymbol}[1]{%
% print character at position #1 in the tt font family in current font size
\begingroup\fontfamily{cmtt}\selectfont\symbol{#1}\endgroup
}
% ----------------
% HASH (tt font hash), can be used inside \fbox environment
\newcommand{\HASH}{\ttsymbol{35}}
% ----------------
% BSL (tt font backslash), can be used inside \fbox environment
\newcommand{\BSL}{\ttsymbol{92}}
% ----------------
% HAT (tt font hat), can be used inside \fbox environment
\newcommand{\HAT}{\ttsymbol{94}}
% ----------------
% UNDERSCORE of standard char width (normal tt font \_ is narrower)
\newcommand{\US}{\ttsymbol{95}}
% ----------------
% TILDE (tt font tilde), can be used inside \fbox environment
\newcommand{\TILDE}{\ttsymbol{126}}
% ----------------
% LBRACE (tt font left brace), can be used inside \fbox environment
\newcommand{\LBRACE}{\ttsymbol{123}}
% ----------------
% RBRACE (tt font right brace), can be used inside \fbox environment
\newcommand{\RBRACE}{\ttsymbol{125}}
% ----------------------------------------------------------------
% Library environment. Used by generated code.
\newenvironment{libverbatim}
{\vspace*{-1.0em}
\verbatim}
{\endverbatim
}
\newenvironment{smcenterboxverbatim}
{\center
\small
\boxedverbatim}
{\endboxedverbatim
{\endcenter} }
\newenvironment{centerboxverbatim}
{\center
\boxedverbatim}
{\endboxedverbatim
{\endcenter }}
% ----------------------------------------------------------------
\newcommand\lineup{\vspace*{-0.6em}}
\newcommand\com[1]{}
\newcommand{\te}[1]{\texttt{#1}}
\newcommand{\nterm}[1]{\emph{#1}}
\newcommand{\term}[1]{\texttt{#1}}
\newcommand{\many}[1]{\{ #1 \}}
\newcommand{\opt}[1]{[ #1 ]}
\newcommand{\alt}{{$\mid$}}
\newcommand{\gram}[2]{ \hm\makebox[10em][l]{\it #1}\makebox[1.5em][l]{::=} #2}
\newcommand{\grammore}[1]{\hm\makebox[10em][l]{ }\makebox[1.5em][l]{} #1}
\newcommand{\gramalt}[1]{ \hm\makebox[10em][l]{ }\makebox[1.5em][l]{\alt} #1}
\newcommand{\tbd}[1]{{\sf TBD: #1}}
\newcommand{\note}[1]{\vspace*{2mm}{\sf {\large \bf Note\\} #1}\vspace{2mm}}
\newcommand{\begindescrlist}[1]{
\begin{list}{\arabic{enumi}}{
\settowidth{\labelwidth}{#1}
\setlength{\leftmargin}{\labelwidth} % {#1}
\addtolength{\leftmargin}{\labelsep}
\setlength{\parsep}{0ex}
\setlength{\itemsep}{0ex}
\usecounter{enumi}
}
}
\newcommand{\litem}[1]{\item[#1\hfill]}
% ``Quoted'' inline bluespec
\newcommand{\qbs}[1]{``\mbox{\te{#1}}''}
\newcommand{\obsolete}[1]{}
\makeindex
\title{
\resizebox{2in}{!}{\includegraphics[width=\textwidth]{../common/B-Lang}} \\
\vspace{0.3in}
{\BS}$^{\rm{TM}}$ {\SV} \\
Language Reference Guide \\
\vspace*{1in}
\vspace*{1in}
\mbox{}
}
% Revision id, major copyrights
\input{version}
\ifpdf
\hypersetup{
pdfauthor = {Bluespec, Inc.},
pdftitle = {Bluespec(TM) Reference Guide},
pdfsubject = {Bluespec},
pdfkeywords = {Bluespec},
pdfcreator = {Bluespec}}
\else
\fi
\begin{document}
% ----------------
\maketitle
% ================================================================
\pagestyle{fancy}
\lhead[Reference Guide]{BSV}
\rhead[BSV]{Reference Guide}
%\lfoot[\thepage]{}
\cfoot{\thepage}
%\rfoot[]{\thepage}
% ----------------
\newpage
{\large\bf Trademarks and copyrights}
Verilog is a trademark of IEEE (the Institute of Electrical and
Electronics Engineers). The Verilog standard is copyrighted, owned
and maintained by IEEE.
VHDL is a trademark of IEEE (the Institute of Electrical and
Electronics Engineers). The VHDL standard is copyrighted, owned and
maintained by IEEE.
SystemVerilog is a trademark of IEEE. The SystemVerilog standard is
owned and maintained by IEEE.
SystemC is a trademark of IEEE. The SystemC standard is owned and
maintained by IEEE.
Bluespec is a trademark of Bluespec, Inc.
% ================================================================
\newpage
\clearpage
\phantomsection
\addcontentsline{toc}{section}{Table of Contents}
\tableofcontents
% The following two commands are a work-around for some bug
% seemingly introduced by the fancyhdr package. Without this,
% the entries on the last page of the table of are spread
% vertically across the page, i.e., the linespacing is
% screwed up. This work-around seems to fix it.
\vfill
\hm
\pagebreak
% ================================================================
\section{Introduction}
\vspace*{3ex}
{\BSV} ({\BSVFull}) is aimed at hardware designers who are using or
expect to use Verilog {\cite{IEEEVerilog2005a}}, VHDL
{\cite{IEEEVHDL2002}}, {\SV} {\cite{IEEESystemVerilog2012a}}, or
SystemC {\cite{IEEESystemC2011a}} to design ASICs or FPGAs. It is also
aimed at people creating \emph{synthesizable} models, transactors, and
verification components to run on FPGA emulation platforms. {\BSV}
substantially extends the design subset of {\SV}, including {\SV}
types, modules, module instantiation, interfaces, interface
instantiation, parameterization, static elaboration, and ``generate''
elaboration. {\BSV} can significantly improve the hardware designer's
productivity with some key innovations:
\begin{itemize}
\item
It expresses synthesizable behavior with \emph{Rules} instead of
synchronous \texttt{always} blocks. Rules are powerful concepts for
achieving \emph{correct} concurrency and eliminating race conditions.
Each rule can be viewed as a declarative assertion expressing a
potential \emph{atomic} state transition. Although rules are
expressed in a modular fashion, a rule may span multiple modules,
i.e., it can test and affect the state in multiple modules. Rules
need not be disjoint, i.e., two rules can read and write common state
elements. The {\BSV} compiler produces efficient RTL code that
manages all the potential interactions between rules by inserting
appropriate arbitration and scheduling logic, logic that would
otherwise have to be designed and coded manually. The atomicity of
rules gives a scalable way to avoid unwanted concurrency (races) in
large designs.
\item
It enables more powerful generate-like elaboration. This is made
possible because in BSV, actions, rules, modules, interfaces and
functions are all first-class objects. {\BSV} also has more general
type parameterization (polymorphism). These enable the designer to
``compute with design fragments,'' i.e., to reuse designs and to glue
them together in much more flexible ways. This leads to much greater
succinctness and correctness.
\item
It provides formal semantics, enabling formal verification and formal
design-by-refinement. {\BSV} rules are based on Term Rewriting
Systems, a clean formalism supported by decades of theoretical
research in the computer science community {\cite{Terese2003}}. This,
together with a judicious choice of a design subset of {\SV}, makes
programs in {\BSV} amenable to formal reasoning.
\end{itemize}
This reference guide is meant to be a stand-alone reference for
{\BSV}, i.e., it fully describes the subset of {\V} and {\SV} used in
{\BSV}. It is not intended to be a tutorial for the beginner. A
reader with a working knowledge of {\V} 1995 or {\V} 2001 should be
able to read this manual easily. Prior knowledge of {\SV} is not
required.
% ----------------------------------------------------------------
\subsection{Libraries and the Standard Prelude}
\index{Prelude|see{Standard Prelude}}
\index{Standard Prelude}
This reference guide focuses on the {\BSV} \emph{language} (syntax and
semantics). However, as with most languages, that is only part of the
story; the utility of a language depends equally on the
\emph{libraries} that come with it.
A separate document,
{\LibRefGuideFullName}~\cite{BLangLibsDoc},\footnote{
\url{https://github.com/B-Lang-org/bsc/tree/main/doc/libraries_ref_guide}}
describes libraries ({\BSV} packages) that come with {\bsc} and are
useful across a broad range of hardware designs. A part of those
libraries, called the Standard Prelude, is automatically imported into
every {\BSV} design because it contains universally useful basic
definitions.
The {\LibRefGuide} is extensive (over 300 pages) and ever-growing as
new libraries are added to the repository. The libraries include many
useful data types, many kinds of registers, wires and FIFOs; register
files, BRAMs and memory interaces; Vectors; math functions;
pseudo-random number generators; counters; interconnects; facilities
for multiple Clock and Reset domains, and more.
Another repository, \texttt{bsc-contrib}\footnote{
\url{https://github.com/B-Lang-org/bsc-contrib/tree/main/Libraries}},
contains more libraries contributed by various people. These are
offered ``as-is'', i.e., they are not part of {\bsc}'s
continuous-integration regression tests.
% ----------------------------------------------------------------
\subsection{{\BSV} and {\BH}}
{\BSV} (Bluespec SystemVerilog) and {\BH} (Bluespec Haskell, or
Bluespec Classic) are actually just two different syntaxes for the
same language (exactly the same semantics; just two alternative
parsers in the {\bsc} compiler).\footnote{ See
Appendix~\ref{sec-history} for a brief history of how this came to
be.} A design project can freely mix packages from the two
languages (\texttt{.bsv} and \texttt{.bs} files, respectively).
{\BSV} code is ``SystemVerilog-ish'' and {\BH} code is ``Haskell-ish''
in flavor; choosing between them is largely a matter of personal
preference. Note however, there are currently (Spring 2022) a few
language features that are only available in {\BSV} and not yet in
{\BH}; we hope to eliminate this gap over time.
% ----------------------------------------------------------------
\subsection{Meta notation for grammar}
\index{grammar}
\index{meta notation|see{grammar}}
The grammar in this document is given using an extended BNF
(Backus-Naur Form). Grammar alternatives are separated by a vertical
bar (``{\alt}''). Items enclosed in square brackets ({``\opt{}''})
are optional. Items enclosed in curly braces (``{\many{}}'') can be
repeated zero or more times.
Another BNF extension is parameterization. For example, a
\nterm{moduleStmt} can be a \nterm{moduleIf}, and an
\nterm{actionStmt} can be an \nterm{actionIf}. A \nterm{moduleIf} and
an \nterm{actionIf} are almost identical; the only difference is that
the former can contain (recursively) \nterm{moduleStmt}s whereas the
latter can contain \nterm{actionStmt}s. Instead of tediously
repeating the grammar for \nterm{moduleIf} and \nterm{actionIf}, we
parameterize it by giving a single grammar for \nterm{<ctxt>If}, where
\nterm{<ctxt>} is either \nterm{module} or \nterm{action}. In the
productions for \nterm{<ctxt>If}, we call for \nterm{<ctxt>Stmt}
which, therefore, either represents a \nterm{moduleStmt} or an
\nterm{actionStmt}, depending on the context in which it is used.
% ----------------------------------------------------------------
\subsection{Overview of Program Structure}
The sections that follow in this Reference Guide are organized
according to the structure of the grammar of {\BSV}. For a newcomer
to {\BSV}, that organization may not quickly convey an intuition or
mental model of program structure and where each part fits. In this
section we provide a top-down overview of {\BSV} program structure so
that the reader can locate where each grammatical construct may appear
within a complete {\BSV} program.
A complete {\BSV} program is a collection of files, where each file
contains one {\BSV} \emph{package}. One package may import another,
making the top-level identifiers of the latter visible and usable in
the former. Figure~\ref{fig_OverallStructure}
\begin{figure}[htbp]
\centerline{\includegraphics[width=\textwidth]{Figures/fig_Overall_Structure}}
\caption{%
\label{fig_OverallStructure}
Overall structure of a {\BSV} program.
}
\end{figure}
shows the structure of a sample program.
Figure~\ref{fig_Contents_Package}
\begin{figure}[htbp]
\centerline{\includegraphics[height=3.5in]{Figures/fig_Contents_Package}}
\caption{%
\label{fig_Contents_Package}
Contents of a {\BSV} package.
}
\end{figure}
shows the kind of top-level constructs one may find in a {\BSV}
package. Section~\ref{sec-packages} describes packages, and
Section~\ref{sec-scopes} provides more detail about scopes,
controlling the import and export of names, and resolving name
clashes.
Figure~\ref{fig_Contents_Interface_Decl}
\begin{figure}[htbp]
\centerline{\includegraphics[height=2.5in]{Figures/fig_Contents_Interface_Decl}}
\caption{%
\label{fig_Contents_Interface_Decl}
Contents of a {\BSV} interface declaration.
}
\end{figure}
shows what goes into an \emph{interface} declaration
(Section~\ref{sec-interface-decl}). These are \emph{method}
declarations and sub-interface declarations (since interfaces can be
nested hierarchically).
Figure~\ref{fig_Contents_Module_Decl}
\begin{figure}[htbp]
\centerline{\includegraphics[height=3.5in]{Figures/fig_Contents_Module_Decl}}
\caption{%
\label{fig_Contents_Module_Decl}
Contents of a {\BSV} module declaration.
}
\end{figure}
shows what goes into a \emph{module} declaration
(Section~\ref{sec-module-decl}). These are value, function and
(sub)-module declarations and definitions, (sub-)module
instantiations, rules, and definitions of methods and sub-interfaces
implemented (offered) by the module.
Figure~\ref{fig_Contents_Rule}
\begin{figure}[htbp]
\centerline{\includegraphics[height=2in]{Figures/fig_Contents_Rule}}
\caption{%
\label{fig_Contents_Rule}
Contents of a {\BSV} rule.
}
\end{figure}
shows what goes into a \emph{rule}
(Section~\ref{sec-rules-in-modules}). Each rule has a \emph{rule
condition} and a \emph{rule body} containing local definitions and
\emph{actions} which perform the semantic actions of the rule.
Actions are typically invocations of methods in other modules.
Figure~\ref{fig_Contents_Method_Def}
\begin{figure}[htbp]
\centerline{\includegraphics[height=1.75in]{Figures/fig_Contents_Method_Def}}
\caption{%
\label{fig_Contents_Method_Def}
Contents of a {\BSV} method definition (in a module).
}
\end{figure}
shows what goes into an interface method definition
(Section~\ref{sec-interface-definition}). These are similar in
structure to rules containing a condition and a body (in fact, a
method definition is semantically a fragment of a rule that invokes
it).
All of the above describe the \emph{textual} structure of a {\BSV}
program. When compiled to hardware, it undergoes \emph{static
elaboration}:
\index{static elaboration}
an instance of the top-level module instantiates its
sub-modules, in turn, instantiate their sub-modules, and so on,
recursively, forming a tree structure (a nesting structure). This
struucture is illustrated in Figure~\ref{fig_Contents_Elaborated}
\begin{figure}[htbp]
\centerline{\includegraphics[height=3in]{Figures/fig_Contents_Elaborated}}
\caption{%
\label{fig_Contents_Elaborated}
The (fixed) hardware strucuture of a {\BSV} program after static elaboration.
}
\end{figure}
% ================================================================
\section{Lexical elements}
{\BSV} has the same basic lexical elements as {\V}.
% ----------------------------------------------------------------
\subsection{Whitespace and comments}
Spaces, tabs, newlines, formfeeds, and carriage returns all constitute
whitespace. They may be used freely between all lexical tokens.
A \emph{comment} is treated as whitespace (it can only occur between,
and never within, any lexical token). A one-line comment
\index{comment!one-line}
\index{//@\te{//} (one-line comment)}
starts with \texttt{//} and ends with a newline. A block comment
\index{comment!block}
\index{/*@\te{/*} (open block comment)}
\index{*/@\te{*/} (close nested comment)}
begins with
\texttt{/*} and ends with \texttt{*/} and may span any number of lines.
Comments do not nest. In a one-line comment, the character sequences
\texttt{//}, \texttt{/*} and \texttt{*/} have no special significance. In a
block comment, the character sequences \texttt{//} and \texttt{/*} have no
special significance.
% ----------------------------------------------------------------
\subsection{Identifiers and keywords}
\index{identifiers}
An identifier in {\BSV} consists of any
sequence of letters, digits, dollar signs \texttt{\$} and underscore
characters (\texttt{\_}).
\index{underscore|see{\_}}
\index{_@\$ (character in identifiers)}
\index{_@\te{\_} (character in identifiers)}
Identifiers are case-sensitive: {\tt{glurph}}, {\tt{gluRph}} and
{\tt{Glurph}} are three distinct identifiers. The first character
cannot be a digit.
{\BSV} currently requires a certain capitalization convention for the
first letter in an identifier. Identifiers used for package names,
type names, enumeration labels, union members and type classes must
begin with a capital letter. In the syntax, we use the non-terminal
{\nterm{Identifier}} to refer to these. Other identifiers (including
names of variables, modules, interfaces, etc.) must begin with a
lowercase letter and, in the syntax, we use the non-terminal
{\nterm{identifier}} to refer to
\index{identifiers!case sensitivity}
\index{identifier@{\nterm{identifier}} (grammar terminal)}
\index{identifier@{\nterm{Identifier}} (grammar terminal)}
these.
As in {\V}, identifiers whose first character is \term{\$}
\index{identifiers!with \$ as first letter}
are reserved for so-called \emph{system tasks and functions}
(see Section \ref{sec-system-tasks-and-functions}).
If the first character of an instance name is an
underscore, (\te{\_}), the compiler will not generate this instance
in the Verilog hierarchy name. This
can be useful for removing submodules from the hierarchical naming.
There are a number of \emph{keywords} that are essentially reserved
identifiers, i.e., they cannot be used by the programmer as
identifiers. Keywords generally do not use uppercase letters (the
only exception is the keyword \texttt{valueOf}). {\BSV} includes all
keywords in {\SV}. All keywords are listed in Appendix
\ref{sec-keywords}.
The types \texttt{Action} and \texttt{ActionValue} are special, and cannot
be redefined.
% ----------------------------------------------------------------
\subsection{Integer literals}
\label{sec-integer-literals}
\index{Literals!Integer}
\index{Integer literals}
Integer literals are written with the usual Verilog and C notations:
\gram{intLiteral}{ \term{'0} {\alt} \term{'1}}\\
\gramalt { \nterm{sizedIntLiteral}}\\
\gramalt { \nterm{unsizedIntLiteral} }
\gram{sizedIntLiteral}{ \nterm{bitWidth} \nterm{baseLiteral}}
\gram{unsizedIntLiteral}{ {\opt{\nterm{sign}} \nterm{baseLiteral}}}\\
\gramalt { {\opt{\nterm{sign}} \nterm{decNum}}}
\gram{baseLiteral} {(\term{'d} {\alt} \term{'D}) \nterm{decDigitsUnderscore}}\\
\gramalt {(\term{'h} {\alt} \term{'H}) \nterm{hexDigitsUnderscore}}\\
\gramalt {(\term{'o} {\alt} \term{'O}) \nterm{octDigitsUnderscore}}\\
\gramalt {(\term{'b} {\alt} \term{'B}) \nterm{binDigitsUnderscore}}\\
\gram{decNum} {\nterm{decDigits} \opt{\nterm{decDigitsUnderscore}}}\\
\gram{bitWidth}{ \nterm{decDigits} }
\gram{sign}{\term{+} {\alt} \term{-}}\\
\gram{decDigits}{\many{\texttt{0}...\texttt{9}}} \\
\gram{decDigitsUnderscore} {\many{\texttt{0}...\texttt{9}, \te{\_}}}\\
\gram{hexDigitsUnderscore}{\many{\texttt{0}...\texttt{9}, \te{a...f},
\te{A...F}, \te{\_}}} \\
\gram{octDigitsUnderscore}{\many{\texttt{0}...\texttt{7}, \te{\_}}}\\
\gram{binDigitsUnderscore}{\many{\texttt{0},\texttt{1}, \te{\_}}}
An integer literal is a sized integer literal if a specific
\nterm{bitWidth} is given (e.g., {\tt 8'o255}).
There is no leading \nterm{sign} (\te{+} or \te{-}) in the syntax for sized integer
literals; instead we provide unary prefix \texttt{+} or \texttt{-}
operators that can be used in front of any integer expression,
including literals (see Section \ref{sec-exprs}). An optional sign (\te{+} or \te{-}) is
part of the syntax for unsized literals so that it is possible to construct
negative constants whose negation is not in the range of the type being constructed
(e.g. \texttt{Int\#(4) x = -8;} since \texttt{8} is not a valid \texttt{Int\#(4)},
but \texttt{-8} is).
Examples:
\begin{verbatim}
125
-16
'h48454a
32'h48454a
8'o255
12'b101010
32'h_FF_FF_FF_FF
\end{verbatim}
% ----------------
\subsubsection{Type conversion of integer literals}
\label{sec-typeconv-integer-literals}
Integer literals can be used to specify values for various integer
types and even for user-defined types. {\BSV} uses its systematic
overloading resolution mechanism to perform these type conversions.
Overloading resolution is described in more detail in Section
{\ref{sec-overloading}}.
An integer literal is a sized literal if a specific \nterm{bitWidth}
is given (e.g., {\tt 8'o255}), in which case the literal is assumed to
have type \texttt{bit~[$w-1$:0]}. The compiler implicitly applies the
function \texttt{fromSizedInteger}
\index{fromSizedInteger@\te{fromSizedInteger} (\te{SizedLiteral} class
method)} to the literal to convert it to the type required by the
context. Thus, sized literals can be used for any type on which the
overloaded function \texttt{fromSizedInteger} is defined, i.e., for
the types \te{Bit}, \te{UInt} and \te{Int}. The function
\te{fromSizedInteger} is part of the \te{SizedLiteral} typeclass,
described in {\LibRefGuide}.
If the literal is an unsized integer literal (a specific
\nterm{bitWidth} is not given), the literal is assumed to have type
\texttt{Integer}. The compiler implicitly applies the overloaded
function \texttt{fromInteger} \index{fromInteger@\te{fromInteger}
(\te{Literal} class method)} to the literal to convert it to the
type required by the context. Thus, unsized literals can be used for
any type on which the overloaded function \texttt{fromInteger} is
defined. The function \te{fromInteger} is part of the \te{Literal}
typeclass, described in {\LibRefGuide}.
The literal \texttt{'0} just stands for 0. The literal \texttt{'1} stands
for a value in which all bits are 1 (the width depends on the
context).
% ----------------------------------------------------------------
\subsection{Real literals}
\label{sec-real-literals}
\index{Literals!Real}
\index{Real literals}
% Support for \texttt{real} ({\VTwoK}) and \texttt{shortreal} ({\SV}) will be
% added to {\BSV} in the future.
Real number literals are written with the usual Verilog notation:
\gram{realLiteral}{\nterm{decNum}{\opt{\term{.}\nterm{decDigitsUnderscore}}} \nterm{exp}
\opt{\nterm{sign}} \nterm{decDigitsUnderscore}}\\
\gramalt{\nterm{decNum}\term{.}\nterm{decDigitsUnderscore}}
\gram{sign}{\term{+} {\alt} \term{-}}
\gram{exp}{\term{e} {\alt} \term{E}}
\gram{decNum} {\nterm{decDigits} \opt{\nterm{decDigitsUnderscore}}}
\gram{decDigits}{\many{\texttt{0}...\texttt{9}}} \\
\gram{decDigitsUnderscore} {\many{\texttt{0}...\texttt{9}, \te{\_}}}\\
There is no leading sign (\te{+} or \te{-}) in the syntax for real
literals. Instead, we provide the unary prefix \te{+} and \te{-}
operators that can be used in front of any expression, including real
literals (Section \ref{sec-exprs}).
If the real literal contains a decimal point, there must be digits following
the decimal point. An exponent can start with either an \te{E} or
an \te{e}, followed by an optional
sign (\te{+} or \te{-}), followed by digits. There cannot be an
exponent or a sign without any digits. Any of the numeric components
may include an underscore, but an underscore cannot be the first digit
of the real literal.
Unlike integer literals, real literals are of limited precision. They are
represented as IEEE floating point numbers of 64 bit length, as defined by
the IEEE standard.
% The \te{Real} type is used for static elaboration only; all values must be
% resolved at compile time.
Examples:
\begin{verbatim}
1.2
0.6
2.4E10 // exponent can be e or E
5e-3
325.761_452_e-10 // underscores are ignored
9.2e+4
\end{verbatim}
\subsubsection{Type conversion of real literals}
Real literals can be used to specify values for real types. By
default, real literals are assumed to have the type \te{Real}. {\BSV}
uses its systematic overloading resolution mechanism to perform these
type conversions. Overloading resolution is described in more detail
in Section {\ref{sec-overloading}}. There are additional functions
defined for \te{Real} types, provided in the \te{Real} package
described in {\LibRefGuide}.
The function \te{fromReal} (described in {\LibRefGuide}) converts a
value of type \te{Real} into a value of another datatype. Whenever
you write a real literal in BSV (such as \te{3.14}), there is an
implied \te{fromReal} applied to it, which turns the real into the
specified type. By defining an instance of \te{RealLiteral} for a
datatype, you can create values of that type from real literals.
The type \te{FixedPoint}, defined in the \te{FixedPoint} package,
defines a type for representing fixed point numbers. The
\te{FixedPoint} type has an instance of \te{RealLiteral} defined for
it and contains functions for operating on fixed-point real numbers.
% ----------------------------------------------------------------
\subsection{String literals}
\label{sec-string-literals}
\index{Literals!String}
\index{String literals}
String literals are written enclosed in double quotes \texttt{"$\cdots$"} and must
be contained on a single source line.
\gram{stringLiteral}{ \term{"} $\cdots$ string characters $\cdots$ \term{"} }
Special characters may be inserted in string literals with the
following backslash escape sequences:
\begin{tabbing}
\texttt{{\BSL}n} \hmmmm \= newline \\
\texttt{{\BSL}t} \> tab \\
\texttt{{\BSL}{\BSL}} \> backslash \\
\texttt{{\BSL}"} \> double quote \\
\texttt{{\BSL}v} \> vertical tab \\
\texttt{{\BSL}f} \> form feed \\
\texttt{{\BSL}a} \> bell \\
\texttt{{\BSL}$OOO$} \> exactly 3 octal digits (8-bit character code) \\
\texttt{{\BSL}x$HH$} \> exactly 2 hexadecimal digits (8-bit character code)
\end{tabbing}
Example - printing characters using form feed.
\begin{libverbatim}
module mkPrinter (Empty);
String display_value;
display_value = "a\nb\nc"; //prints a
// b
// c repeatedly
rule every;
$display(display_value);
endrule
endmodule
\end{libverbatim}
% TODO: fixup to the following text after Mieszko has fixed the compiler
% If fewer than three octal digits are used in the octal character code
% escape sequence, they must be followed by a non-octal digit or by the
% end of the string.
% If fewer than two hexadecimal digits are used in the hexadecimal
% character code escape sequence, they must be followed by a
% non-hexadecimal digit or by the end of the string.
\subsubsection{Type conversion of string literals}
String literals are used to specify values for string types. BSV uses
its systematic overloading resolution mechanism to perform these type
conversions. Overloading resolution is described in more detail in
Section {\ref{sec-overloading}}.
Whenever you write a string literal in BSV there is an implicit
\te{fromString} applied to it, which defaults to type \te{String}.
% ----------------------------------------------------------------
\subsection{Don't-care values}
A lone question mark \texttt{?} is treated as a special don't-care value.
For example, one may return \texttt{?} from an arm of a case statement
that is known to be unreachable.
\index{?@\texttt{?} (don't-care expression)}
\index{don't-care expression|see {?}}
Example - Using \te{?} as a don't-care value
\begin{libverbatim}
module mkExample (Empty);
Reg#(Bit#(8)) r <- mkReg(?); // don't-care is used for the
rule every; // reset value of the Reg
$display("value is %h", r); // the value of r is displayed
endrule
endmodule
\end{libverbatim}
% ----------------------------------------------------------------
\subsection{Compiler directives}
\index{compiler directives}
\index{compiler directives}
\index{`@\te{`}|see{compiler directives}}
The following compiler directives permit file inclusion, macro
definition and substitution, and conditional compilation. They follow
the specifications given in the {\VTwoK} LRM plus the extensions given
in the {\SVThreeOneA} LRM.
In general, these compiler directives can appear anywhere in the
source text. In particular, they do not need to be on lines by
themselves, and they need not begin in the first column. Of course,
they should not be inside strings or comments, where the text remains
uninterpreted.
% ----------------
\subsubsection{File inclusion: `include and `line}
\index{include@\te{`include} (compiler directive)}
\gram{compilerDirective}{ \term{`include "}\nterm{filename}\term{"} } \\
\gramalt { \term{`include <}\nterm{filename}\term{>} } \\
\gramalt { \term{`include} \nterm{macroInvocation} }
In an \texttt{`include} directive, the contents of the named file are
inserted in place of this line. The included files may themselves
contain compiler directives. Currently there is no difference between
the \te{"..."} and \te{<...>} forms. A \nterm{macroInvocation} should
expand to one of the other two forms. The file name may be absolute,
or relative to the current directory.
\gram{compilerDirective}{ \term{`line} \nterm{lineNumber} \term{"}\nterm{filename}\term{"} \nterm{level} } \\
\gram{lineNumber}{ \nterm{decLiteral} } \\
\gram{level}{ \term{0} {\alt} \term{1} {\alt} \term{2} }
\index{line@\te{`line} (compiler directive)}
A \texttt{`line} directive is terminated by a newline, i.e., it cannot
have any other source text after the \nterm{level}. The compiler
automatically keeps track of the source file name and line number for
every line of source text (including from included source files), so
that error messages can be properly correlated to the source. This
directive effectively overrides the compiler's internal tracking
mechanism, forcing it to regard the next line onwards as coming from
the given source file and line number. It is generally not necessary
to use this directive explicitly; it is mainly intended to be
generated by other preprocessors that may themselves need to alter the
source files before passing them through the {\BSV} compiler; this
mechanism allows proper references to the original source.
The \nterm{level} specifier is either 0, 1 or 2:
\begin{itemize}
\item
1 indicates that an include file has just been entered
\item
2 indicates that an include file has just been exited
\item
0 is used in all other cases
\end{itemize}
% ----------------
\label{sec-macrodef}