diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..1d268fd7 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: c +services: + - docker +before_install: + - docker pull berezun/cw-2020 + - docker run -d -it --name cw-2020 -v $(pwd):/usr/share/compiler-2020 berezun/cw-2020 +script: + docker exec -it cw-2020 sh test.sh \ No newline at end of file diff --git a/lectures/08.aux b/lectures/08.aux new file mode 100644 index 00000000..4e0e8176 --- /dev/null +++ b/lectures/08.aux @@ -0,0 +1,21 @@ +\relax +\providecommand\hyper@newdestlabel[2]{} +\providecommand\HyperFirstAtBeginDocument{\AtBeginDocument} +\HyperFirstAtBeginDocument{\ifx\hyper@anchor\@undefined +\global\let\oldcontentsline\contentsline +\gdef\contentsline#1#2#3#4{\oldcontentsline{#1}{#2}{#3}} +\global\let\oldnewlabel\newlabel +\gdef\newlabel#1#2{\newlabelxx{#1}#2} +\gdef\newlabelxx#1#2#3#4#5#6{\oldnewlabel{#1}{{#2}{#3}}} +\AtEndDocument{\ifx\hyper@anchor\@undefined +\let\contentsline\oldcontentsline +\let\newlabel\oldnewlabel +\fi} +\fi} +\global\let\hyper@last\relax +\gdef\HyperFirstAtBeginDocument#1{#1} +\providecommand*\HyPL@Entry[1]{} +\HyPL@Entry{0<>} +\@writefile{toc}{\contentsline {section}{\numberline {1}Arrays}{1}{section.1}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {2}Operational Semantics}{1}{section.2}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {3}Stack Machine}{2}{section.3}\protected@file@percent } diff --git a/lectures/08.log b/lectures/08.log new file mode 100644 index 00000000..a76bb638 --- /dev/null +++ b/lectures/08.log @@ -0,0 +1,728 @@ +This is XeTeX, Version 3.14159265-2.6-0.999992 (TeX Live 2020/Arch Linux) (preloaded format=xelatex 2020.11.8) 16 NOV 2020 09:19 +entering extended mode + restricted \write18 enabled. + %&-line parsing enabled. +**08.tex +(./08.tex +LaTeX2e <2020-02-02> patch level 5 +L3 programming layer <2020-06-03> +(/usr/share/texmf-dist/tex/latex/base/article.cls +Document Class: article 2019/12/20 v1.4l Standard LaTeX document class +(/usr/share/texmf-dist/tex/latex/base/size10.clo +File: size10.clo 2019/12/20 v1.4l Standard LaTeX file (size option) +) +\c@part=\count163 +\c@section=\count164 +\c@subsection=\count165 +\c@subsubsection=\count166 +\c@paragraph=\count167 +\c@subparagraph=\count168 +\c@figure=\count169 +\c@table=\count170 +\abovecaptionskip=\skip47 +\belowcaptionskip=\skip48 +\bibindent=\dimen134 +) +(/usr/share/texmf-dist/tex/latex/amsfonts/amssymb.sty +Package: amssymb 2013/01/14 v3.01 AMS font symbols + +(/usr/share/texmf-dist/tex/latex/amsfonts/amsfonts.sty +Package: amsfonts 2013/01/14 v3.01 Basic AMSFonts support +\@emptytoks=\toks15 +\symAMSa=\mathgroup4 +\symAMSb=\mathgroup5 +LaTeX Font Info: Redeclaring math symbol \hbar on input line 98. +LaTeX Font Info: Overwriting math alphabet `\mathfrak' in version `bold' +(Font) U/euf/m/n --> U/euf/b/n on input line 106. +)) +(/usr/share/texmf-dist/tex/latex/amsmath/amsmath.sty +Package: amsmath 2020/01/20 v2.17e AMS math features +\@mathmargin=\skip49 + +For additional information on amsmath, use the `?' option. +(/usr/share/texmf-dist/tex/latex/amsmath/amstext.sty +Package: amstext 2000/06/29 v2.01 AMS text + +(/usr/share/texmf-dist/tex/latex/amsmath/amsgen.sty +File: amsgen.sty 1999/11/30 v2.0 generic functions +\@emptytoks=\toks16 +\ex@=\dimen135 +)) +(/usr/share/texmf-dist/tex/latex/amsmath/amsbsy.sty +Package: amsbsy 1999/11/29 v1.2d Bold Symbols +\pmbraise@=\dimen136 +) +(/usr/share/texmf-dist/tex/latex/amsmath/amsopn.sty +Package: amsopn 2016/03/08 v2.02 operator names +) +\inf@bad=\count171 +LaTeX Info: Redefining \frac on input line 227. +\uproot@=\count172 +\leftroot@=\count173 +LaTeX Info: Redefining \overline on input line 389. +\classnum@=\count174 +\DOTSCASE@=\count175 +LaTeX Info: Redefining \ldots on input line 486. +LaTeX Info: Redefining \dots on input line 489. +LaTeX Info: Redefining \cdots on input line 610. +\Mathstrutbox@=\box45 +\strutbox@=\box46 +\big@size=\dimen137 +LaTeX Font Info: Redeclaring font encoding OML on input line 733. +LaTeX Font Info: Redeclaring font encoding OMS on input line 734. +\macc@depth=\count176 +\c@MaxMatrixCols=\count177 +\dotsspace@=\muskip16 +\c@parentequation=\count178 +\dspbrk@lvl=\count179 +\tag@help=\toks17 +\row@=\count180 +\column@=\count181 +\maxfields@=\count182 +\andhelp@=\toks18 +\eqnshift@=\dimen138 +\alignsep@=\dimen139 +\tagshift@=\dimen140 +\tagwidth@=\dimen141 +\totwidth@=\dimen142 +\lineht@=\dimen143 +\@envbody=\toks19 +\multlinegap=\skip50 +\multlinetaggap=\skip51 +\mathdisplay@stack=\toks20 +LaTeX Info: Redefining \[ on input line 2859. +LaTeX Info: Redefining \] on input line 2860. +) +(/usr/share/texmf-dist/tex/latex/base/alltt.sty +Package: alltt 1997/06/16 v2.0g defines alltt environment +) +(/usr/share/texmf-dist/tex/latex/pslatex/pslatex.sty +Package: pslatex 1996/07/24 v1.2 pslatex emulation (DPC) +LaTeX Font Info: Redeclaring symbol font `operators' on input line 65. +LaTeX Font Info: Overwriting symbol font `operators' in version `normal' +(Font) OT1/cmr/m/n --> OT1/ptmcm/m/n on input line 65. +LaTeX Font Info: Overwriting symbol font `operators' in version `bold' +(Font) OT1/cmr/bx/n --> OT1/ptmcm/m/n on input line 65. +LaTeX Font Info: Redeclaring symbol font `letters' on input line 66. +LaTeX Font Info: Overwriting symbol font `letters' in version `normal' +(Font) OML/cmm/m/it --> OML/ptmcm/m/it on input line 66. +LaTeX Font Info: Overwriting symbol font `letters' in version `bold' +(Font) OML/cmm/b/it --> OML/ptmcm/m/it on input line 66. +LaTeX Font Info: Redeclaring symbol font `symbols' on input line 67. +LaTeX Font Info: Overwriting symbol font `symbols' in version `normal' +(Font) OMS/cmsy/m/n --> OMS/pzccm/m/n on input line 67. +LaTeX Font Info: Overwriting symbol font `symbols' in version `bold' +(Font) OMS/cmsy/b/n --> OMS/pzccm/m/n on input line 67. +LaTeX Font Info: Redeclaring symbol font `largesymbols' on input line 68. +LaTeX Font Info: Overwriting symbol font `largesymbols' in version `normal' +(Font) OMX/cmex/m/n --> OMX/psycm/m/n on input line 68. +LaTeX Font Info: Overwriting symbol font `largesymbols' in version `bold' +(Font) OMX/cmex/m/n --> OMX/psycm/m/n on input line 68. +\symbold=\mathgroup6 +\symitalic=\mathgroup7 +LaTeX Font Info: Redeclaring math alphabet \mathbf on input line 74. +LaTeX Font Info: Overwriting math alphabet `\mathbf' in version `normal' +(Font) OT1/cmr/bx/n --> OT1/ptm/bx/n on input line 74. +LaTeX Font Info: Overwriting math alphabet `\mathbf' in version `bold' +(Font) OT1/cmr/bx/n --> OT1/ptm/bx/n on input line 74. +LaTeX Font Info: Redeclaring math alphabet \mathit on input line 75. +LaTeX Font Info: Overwriting math alphabet `\mathit' in version `normal' +(Font) OT1/cmr/m/it --> OT1/ptm/m/it on input line 75. +LaTeX Font Info: Overwriting math alphabet `\mathit' in version `bold' +(Font) OT1/cmr/bx/it --> OT1/ptm/m/it on input line 75. + + +LaTeX Warning: Font shape declaration has incorrect series value `mc'. + It should not contain an `m'! Please correct it. + Found on input line 226. + + +LaTeX Warning: Font shape declaration has incorrect series value `mc'. + It should not contain an `m'! Please correct it. + Found on input line 230. + + +LaTeX Warning: Font shape declaration has incorrect series value `mc'. + It should not contain an `m'! Please correct it. + Found on input line 234. + + +LaTeX Warning: Font shape declaration has incorrect series value `mc'. + It should not contain an `m'! Please correct it. + Found on input line 243. + + +LaTeX Warning: Font shape declaration has incorrect series value `mc'. + It should not contain an `m'! Please correct it. + Found on input line 384. + + +LaTeX Warning: Font shape declaration has incorrect series value `mc'. + It should not contain an `m'! Please correct it. + Found on input line 388. + + +LaTeX Warning: Font shape declaration has incorrect series value `mc'. + It should not contain an `m'! Please correct it. + Found on input line 392. + + +LaTeX Warning: Font shape declaration has incorrect series value `mc'. + It should not contain an `m'! Please correct it. + Found on input line 401. + +LaTeX Font Info: Trying to load font information for TU+ptm on input line 45 +6. +LaTeX Font Info: No file TUptm.fd. on input line 456. + +LaTeX Font Warning: Font shape `TU/ptm/m/n' undefined +(Font) using `TU/lmr/m/n' instead on input line 456. + +) (/usr/share/texmf-dist/tex/latex/epigraph/epigraph.sty +Package: epigraph 2020/01/02 v1.5e typesetting epigraphs + +(/usr/share/texmf-dist/tex/latex/nextpage/nextpage.sty +Package: nextpage 2009/09/03 v1.1a additional page commands +) +\beforeepigraphskip=\skip52 +\afterepigraphskip=\skip53 +\epigraphwidth=\skip54 +\epigraphrule=\skip55 +) +(/usr/share/texmf-dist/tex/latex/tools/verbatim.sty +Package: verbatim 2019/11/10 v1.5r LaTeX2e package for verbatim enhancements +\every@verbatim=\toks21 +\verbatim@line=\toks22 +\verbatim@in@stream=\read2 +) +(/usr/share/texmf-dist/tex/latex/base/latexsym.sty +Package: latexsym 1998/08/17 v2.2e Standard LaTeX package (lasy symbols) +\symlasy=\mathgroup8 +LaTeX Font Info: Overwriting symbol font `lasy' in version `bold' +(Font) U/lasy/m/n --> U/lasy/b/n on input line 52. +) +(/usr/share/texmf-dist/tex/latex/tools/array.sty +Package: array 2019/08/31 v2.4l Tabular extension package (FMi) +\col@sep=\dimen144 +\ar@mcellbox=\box47 +\extrarowheight=\dimen145 +\NC@list=\toks23 +\extratabsurround=\skip56 +\backup@length=\skip57 +\ar@cellbox=\box48 +) +(/usr/share/texmf-dist/tex/latex/comment/comment.sty +\CommentStream=\write3 + +Excluding comment 'comment') (/usr/share/texmf-dist/tex/latex/base/makeidx.sty +Package: makeidx 2014/09/29 v1.0m Standard LaTeX package +) (/usr/share/texmf-dist/tex/latex/listings/listings.sty +(/usr/share/texmf-dist/tex/latex/graphics/keyval.sty +Package: keyval 2014/10/28 v1.15 key=value parser (DPC) +\KV@toks@=\toks24 +) +\lst@mode=\count183 +\lst@gtempboxa=\box49 +\lst@token=\toks25 +\lst@length=\count184 +\lst@currlwidth=\dimen146 +\lst@column=\count185 +\lst@pos=\count186 +\lst@lostspace=\dimen147 +\lst@width=\dimen148 +\lst@newlines=\count187 +\lst@lineno=\count188 +\lst@maxwidth=\dimen149 + +(/usr/share/texmf-dist/tex/latex/listings/lstmisc.sty +File: lstmisc.sty 2020/03/24 1.8d (Carsten Heinz) +\c@lstnumber=\count189 +\lst@skipnumbers=\count190 +\lst@framebox=\box50 +) +(/usr/share/texmf-dist/tex/latex/listings/listings.cfg +File: listings.cfg 2020/03/24 1.8d listings configuration +)) +Package: listings 2020/03/24 1.8d (Carsten Heinz) + +(/usr/share/texmf-dist/tex/latex/tools/indentfirst.sty +Package: indentfirst 1995/11/23 v1.03 Indent first paragraph (DPC) +) +(/usr/share/texmf-dist/tex/latex/graphics/color.sty +Package: color 2019/11/23 v1.2a Standard LaTeX Color (DPC) + +(/usr/share/texmf-dist/tex/latex/graphics-cfg/color.cfg +File: color.cfg 2016/01/02 v1.6 sample color configuration +) +Package color Info: Driver file: xetex.def on input line 147. + +(/usr/share/texmf-dist/tex/latex/graphics-def/xetex.def +File: xetex.def 2017/06/24 v5.0h Graphics/color driver for xetex +)) +(/usr/share/texmf-dist/tex/latex/url/url.sty +\Urlmuskip=\muskip17 +Package: url 2013/09/16 ver 3.4 Verb mode for urls, etc. +) +(/usr/share/texmf-dist/tex/latex/tools/xspace.sty +Package: xspace 2014/10/28 v1.13 Space after command names (DPC,MH) +) +(/usr/share/texmf-dist/tex/latex/hyperref/hyperref.sty +Package: hyperref 2020-05-15 v7.00e Hypertext links for LaTeX + +(/usr/share/texmf-dist/tex/generic/ltxcmds/ltxcmds.sty +Package: ltxcmds 2019/12/15 v1.24 LaTeX kernel commands for general use (HO) +) +(/usr/share/texmf-dist/tex/generic/iftex/iftex.sty +Package: iftex 2020/03/06 v1.0d TeX engine tests +) +(/usr/share/texmf-dist/tex/latex/pdftexcmds/pdftexcmds.sty +Package: pdftexcmds 2020-06-04 v0.32 Utility functions of pdfTeX for LuaTeX (HO +) + +(/usr/share/texmf-dist/tex/generic/infwarerr/infwarerr.sty +Package: infwarerr 2019/12/03 v1.5 Providing info/warning/error messages (HO) +) +Package pdftexcmds Info: \pdf@primitive is available. +Package pdftexcmds Info: \pdf@ifprimitive is available. +Package pdftexcmds Info: \pdfdraftmode not found. +) +(/usr/share/texmf-dist/tex/generic/kvsetkeys/kvsetkeys.sty +Package: kvsetkeys 2019/12/15 v1.18 Key value parser (HO) +) +(/usr/share/texmf-dist/tex/generic/kvdefinekeys/kvdefinekeys.sty +Package: kvdefinekeys 2019-12-19 v1.6 Define keys (HO) +) +(/usr/share/texmf-dist/tex/generic/pdfescape/pdfescape.sty +Package: pdfescape 2019/12/09 v1.15 Implements pdfTeX's escape features (HO) +) +(/usr/share/texmf-dist/tex/latex/hycolor/hycolor.sty +Package: hycolor 2020-01-27 v1.10 Color options for hyperref/bookmark (HO) +) +(/usr/share/texmf-dist/tex/latex/letltxmacro/letltxmacro.sty +Package: letltxmacro 2019/12/03 v1.6 Let assignment for LaTeX macros (HO) +) +(/usr/share/texmf-dist/tex/latex/auxhook/auxhook.sty +Package: auxhook 2019-12-17 v1.6 Hooks for auxiliary files (HO) +) +(/usr/share/texmf-dist/tex/latex/kvoptions/kvoptions.sty +Package: kvoptions 2019/11/29 v3.13 Key value format for package options (HO) +) +\@linkdim=\dimen150 +\Hy@linkcounter=\count191 +\Hy@pagecounter=\count192 + +(/usr/share/texmf-dist/tex/latex/hyperref/pd1enc.def +File: pd1enc.def 2020-05-15 v7.00e Hyperref: PDFDocEncoding definition (HO) +) +(/usr/share/texmf-dist/tex/generic/intcalc/intcalc.sty +Package: intcalc 2019/12/15 v1.3 Expandable calculations with integers (HO) +) +(/usr/share/texmf-dist/tex/generic/etexcmds/etexcmds.sty +Package: etexcmds 2019/12/15 v1.7 Avoid name clashes with e-TeX commands (HO) +) +\Hy@SavedSpaceFactor=\count193 +Package hyperref Info: Hyper figures OFF on input line 4464. +Package hyperref Info: Link nesting OFF on input line 4469. +Package hyperref Info: Hyper index ON on input line 4472. +Package hyperref Info: Plain pages OFF on input line 4479. +Package hyperref Info: Backreferencing OFF on input line 4484. +Package hyperref Info: Implicit mode ON; LaTeX internals redefined. +Package hyperref Info: Bookmarks ON on input line 4717. +\c@Hy@tempcnt=\count194 +LaTeX Info: Redefining \url on input line 5076. +\XeTeXLinkMargin=\dimen151 + +(/usr/share/texmf-dist/tex/generic/bitset/bitset.sty +Package: bitset 2019/12/09 v1.3 Handle bit-vector datatype (HO) + +(/usr/share/texmf-dist/tex/generic/bigintcalc/bigintcalc.sty +Package: bigintcalc 2019/12/15 v1.5 Expandable calculations on big integers (HO +) +)) +\Fld@menulength=\count195 +\Field@Width=\dimen152 +\Fld@charsize=\dimen153 +Package hyperref Info: Hyper figures OFF on input line 6347. +Package hyperref Info: Link nesting OFF on input line 6352. +Package hyperref Info: Hyper index ON on input line 6355. +Package hyperref Info: backreferencing OFF on input line 6362. +Package hyperref Info: Link coloring OFF on input line 6367. +Package hyperref Info: Link coloring with OCG OFF on input line 6372. +Package hyperref Info: PDF/A mode OFF on input line 6377. +LaTeX Info: Redefining \ref on input line 6417. +LaTeX Info: Redefining \pageref on input line 6421. + +(/usr/share/texmf-dist/tex/generic/atbegshi/atbegshi.sty +Package: atbegshi 2019/12/05 v1.19 At begin shipout hook (HO) +) +\Hy@abspage=\count196 +\c@Item=\count197 +\c@Hfootnote=\count198 +) +Package hyperref Info: Driver (autodetected): hxetex. + +(/usr/share/texmf-dist/tex/latex/hyperref/hxetex.def +File: hxetex.def 2020-05-15 v7.00e Hyperref driver for XeTeX + +(/usr/share/texmf-dist/tex/latex/hyperref/puenc.def +File: puenc.def 2020-05-15 v7.00e Hyperref: PDF Unicode definition (HO) +) +(/usr/share/texmf-dist/tex/generic/stringenc/stringenc.sty +Package: stringenc 2019/11/29 v1.12 Convert strings between diff. encodings (HO +) +) +\pdfm@box=\box51 +\c@Hy@AnnotLevel=\count199 +\HyField@AnnotCount=\count266 +\Fld@listcount=\count267 +\c@bookmark@seq@number=\count268 + +(/usr/share/texmf-dist/tex/latex/rerunfilecheck/rerunfilecheck.sty +Package: rerunfilecheck 2019/12/05 v1.9 Rerun checks for auxiliary files (HO) + +(/usr/share/texmf-dist/tex/latex/atveryend/atveryend.sty +Package: atveryend 2019-12-11 v1.11 Hooks at the very end of document (HO) +Package atveryend Info: \enddocument detected (standard20110627). +) +(/usr/share/texmf-dist/tex/generic/uniquecounter/uniquecounter.sty +Package: uniquecounter 2019/12/15 v1.4 Provide unlimited unique counter (HO) +) +Package uniquecounter Info: New unique counter `rerunfilecheck' on input line 2 +86. +) +\Hy@SectionHShift=\skip58 +) +(/usr/share/texmf-dist/tex/latex/stmaryrd/stmaryrd.sty +Package: stmaryrd 1994/03/03 St Mary's Road symbol package +\symstmry=\mathgroup9 +LaTeX Font Info: Overwriting symbol font `stmry' in version `bold' +(Font) U/stmry/m/n --> U/stmry/b/n on input line 89. +) +(/usr/share/texmf-dist/tex/latex/amscls/amsthm.sty +Package: amsthm 2020/05/29 v2.20.6 +\thm@style=\toks26 +\thm@bodyfont=\toks27 +\thm@headfont=\toks28 +\thm@notefont=\toks29 +\thm@headpunct=\toks30 +\thm@preskip=\skip59 +\thm@postskip=\skip60 +\thm@headsep=\skip61 +\dth@everypar=\toks31 +) +(/usr/share/texmf-dist/tex/latex/graphics/graphicx.sty +Package: graphicx 2019/11/30 v1.2a Enhanced LaTeX Graphics (DPC,SPQR) + +(/usr/share/texmf-dist/tex/latex/graphics/graphics.sty +Package: graphics 2019/11/30 v1.4a Standard LaTeX Graphics (DPC,SPQR) + +(/usr/share/texmf-dist/tex/latex/graphics/trig.sty +Package: trig 2016/01/03 v1.10 sin cos tan (DPC) +) +(/usr/share/texmf-dist/tex/latex/graphics-cfg/graphics.cfg +File: graphics.cfg 2016/06/04 v1.11 sample graphics configuration +) +Package graphics Info: Driver file: xetex.def on input line 105. +) +\Gin@req@height=\dimen154 +\Gin@req@width=\dimen155 +) +(/usr/share/texmf-dist/tex/latex/amsfonts/euscript.sty +Package: euscript 2009/06/22 v3.00 Euler Script fonts +LaTeX Font Info: Overwriting math alphabet `\EuScript' in version `bold' +(Font) U/eus/m/n --> U/eus/b/n on input line 33. +) +(/usr/share/texmf-dist/tex/latex/mathtools/mathtools.sty +Package: mathtools 2020/03/24 v1.24 mathematical typesetting tools + +(/usr/share/texmf-dist/tex/latex/tools/calc.sty +Package: calc 2017/05/25 v4.3 Infix arithmetic (KKT,FJ) +\calc@Acount=\count269 +\calc@Bcount=\count270 +\calc@Adimen=\dimen156 +\calc@Bdimen=\dimen157 +\calc@Askip=\skip62 +\calc@Bskip=\skip63 +LaTeX Info: Redefining \setlength on input line 80. +LaTeX Info: Redefining \addtolength on input line 81. +\calc@Ccount=\count271 +\calc@Cskip=\skip64 +) +(/usr/share/texmf-dist/tex/latex/mathtools/mhsetup.sty +Package: mhsetup 2017/03/31 v1.3 programming setup (MH) +) +LaTeX Info: Thecontrolsequence`\('isalreadyrobust on input line 130. +LaTeX Info: Thecontrolsequence`\)'isalreadyrobust on input line 130. +LaTeX Info: Thecontrolsequence`\['isalreadyrobust on input line 130. +LaTeX Info: Thecontrolsequence`\]'isalreadyrobust on input line 130. +\g_MT_multlinerow_int=\count272 +\l_MT_multwidth_dim=\dimen158 +\origjot=\skip65 +\l_MT_shortvdotswithinadjustabove_dim=\dimen159 +\l_MT_shortvdotswithinadjustbelow_dim=\dimen160 +\l_MT_above_intertext_sep=\dimen161 +\l_MT_below_intertext_sep=\dimen162 +\l_MT_above_shortintertext_sep=\dimen163 +\l_MT_below_shortintertext_sep=\dimen164 +\xmathstrut@box=\box52 +\xmathstrut@dim=\dimen165 +) +(/usr/share/texmf-dist/tex/latex/jknapltx/mathrsfs.sty +Package: mathrsfs 1996/01/01 Math RSFS package v1.0 (jk) +\symrsfs=\mathgroup10 +) +(/usr/share/texmf-dist/tex/latex/multirow/multirow.sty +Package: multirow 2019/05/31 v2.5 Span multiple rows of a table +\multirow@colwidth=\skip66 +\multirow@cntb=\count273 +\multirow@dima=\skip67 +\bigstrutjot=\dimen166 +) +(/usr/share/texmf-dist/tex/latex/multirow/bigdelim.sty +Package: bigdelim 2019/05/31 v2.5 Create big delimiters in tabular or array +) +(/usr/share/texmf-dist/tex/latex/caption/subcaption.sty +Package: subcaption 2020/01/22 v1.3d Sub-captions (AR) + +(/usr/share/texmf-dist/tex/latex/caption/caption.sty +Package: caption 2020/05/10 v3.4j Customizing captions (AR) + +(/usr/share/texmf-dist/tex/latex/caption/caption3.sty +Package: caption3 2020/05/10 v1.11 caption3 kernel (AR) +Package caption3 Info: TeX engine: e-TeX on input line 61. +\captionmargin=\dimen167 +\captionmargin@=\dimen168 +\captionwidth=\dimen169 +\caption@tempdima=\dimen170 +\caption@indent=\dimen171 +\caption@parindent=\dimen172 +\caption@hangindent=\dimen173 +Package caption Info: Standard document class detected. +) +\c@caption@flags=\count274 +\c@continuedfloat=\count275 +Package caption Info: hyperref package is loaded. +Package caption Info: listings package is loaded. +) +\c@subfigure=\count276 +\c@subtable=\count277 +) +(/usr/share/texmf-dist/tex/latex/placeins/placeins.sty +Package: placeins 2005/04/18 v 2.2 +) +\c@lemma=\count278 +\c@theorem=\count279 + +(/usr/share/texmf-dist/tex/latex/l3backend/l3backend-xdvipdfmx.def +File: l3backend-xdvipdfmx.def 2020-06-03 L3 backend support: xdvipdfmx +\g__graphics_track_int=\count280 +\l__pdf_internal_box=\box53 +\g__pdf_backend_object_int=\count281 +\g__pdf_backend_annotation_int=\count282 +) (./08.aux) +LaTeX Font Info: Checking defaults for OML/cmm/m/it on input line 114. +LaTeX Font Info: ... okay on input line 114. +LaTeX Font Info: Checking defaults for OMS/pzccm/m/n on input line 114. +LaTeX Font Info: Trying to load font information for OMS+pzccm on input line + 114. + +(/usr/share/texmf-dist/tex/latex/psnfss/omspzccm.fd +File: omspzccm.fd 2000/01/03 Fontinst v1.801 font definitions for OMS/pzccm. +) +LaTeX Font Info: ... okay on input line 114. +LaTeX Font Info: Checking defaults for OT1/cmr/m/n on input line 114. +LaTeX Font Info: ... okay on input line 114. +LaTeX Font Info: Checking defaults for T1/cmr/m/n on input line 114. +LaTeX Font Info: ... okay on input line 114. +LaTeX Font Info: Checking defaults for TS1/cmr/m/n on input line 114. +LaTeX Font Info: Trying to load font information for TS1+cmr on input line 1 +14. + +(/usr/share/texmf-dist/tex/latex/base/ts1cmr.fd +File: ts1cmr.fd 2019/12/16 v2.5j Standard LaTeX font definitions +) +LaTeX Font Info: ... okay on input line 114. +LaTeX Font Info: Checking defaults for TU/lmr/m/n on input line 114. +LaTeX Font Info: ... okay on input line 114. +LaTeX Font Info: Checking defaults for OMX/cmex/m/n on input line 114. +LaTeX Font Info: ... okay on input line 114. +LaTeX Font Info: Checking defaults for U/cmr/m/n on input line 114. +LaTeX Font Info: ... okay on input line 114. +LaTeX Font Info: Checking defaults for PD1/pdf/m/n on input line 114. +LaTeX Font Info: ... okay on input line 114. +LaTeX Font Info: Checking defaults for PU/pdf/m/n on input line 114. +LaTeX Font Info: ... okay on input line 114. +\c@lstlisting=\count283 +\AtBeginShipoutBox=\box54 +Package hyperref Info: Link coloring OFF on input line 114. + +(/usr/share/texmf-dist/tex/latex/hyperref/nameref.sty +Package: nameref 2019/09/16 v2.46 Cross-referencing by name of section + +(/usr/share/texmf-dist/tex/latex/refcount/refcount.sty +Package: refcount 2019/12/15 v3.6 Data extraction from label references (HO) +) +(/usr/share/texmf-dist/tex/generic/gettitlestring/gettitlestring.sty +Package: gettitlestring 2019/12/15 v1.6 Cleanup title references (HO) +) +\c@section@level=\count284 +) +LaTeX Info: Redefining \ref on input line 114. +LaTeX Info: Redefining \pageref on input line 114. +LaTeX Info: Redefining \nameref on input line 114. + +(./08.out) (./08.out) +\@outlinefile=\write4 +Package caption Info: Begin \AtBeginDocument code. +Package caption Info: End \AtBeginDocument code. + + +LaTeX Font Warning: Font shape `TU/ptm/b/n' undefined +(Font) using `TU/ptm/m/n' instead on input line 116. + +(/usr/share/texmf-dist/tex/generic/stringenc/se-ascii-print.def +File: se-ascii-print.def 2019/11/29 v1.12 stringenc: Printable ASCII characters + +) +LaTeX Font Info: Trying to load font information for OT1+ptmcm on input line + 120. + +(/usr/share/texmf-dist/tex/latex/psnfss/ot1ptmcm.fd +File: ot1ptmcm.fd 2000/01/03 Fontinst v1.801 font definitions for OT1/ptmcm. +) +LaTeX Font Info: Trying to load font information for OML+ptmcm on input line + 120. + +(/usr/share/texmf-dist/tex/latex/psnfss/omlptmcm.fd +File: omlptmcm.fd 2000/01/03 Fontinst v1.801 font definitions for OML/ptmcm. +) +LaTeX Font Info: Trying to load font information for OMX+psycm on input line + 120. + +(/usr/share/texmf-dist/tex/latex/psnfss/omxpsycm.fd +File: omxpsycm.fd 2000/01/03 Fontinst v1.801 font definitions for OMX/psycm. +) +LaTeX Font Info: Trying to load font information for U+msa on input line 120 +. + +(/usr/share/texmf-dist/tex/latex/amsfonts/umsa.fd +File: umsa.fd 2013/01/14 v3.01 AMS symbols A +) +LaTeX Font Info: Trying to load font information for U+msb on input line 120 +. + +(/usr/share/texmf-dist/tex/latex/amsfonts/umsb.fd +File: umsb.fd 2013/01/14 v3.01 AMS symbols B +) +LaTeX Font Info: Font shape `OT1/ptm/bx/n' in size <10> not available +(Font) Font shape `OT1/ptm/b/n' tried instead on input line 120. +LaTeX Font Info: Font shape `OT1/ptm/bx/n' in size <7.4> not available +(Font) Font shape `OT1/ptm/b/n' tried instead on input line 120. +LaTeX Font Info: Font shape `OT1/ptm/bx/n' in size <6> not available +(Font) Font shape `OT1/ptm/b/n' tried instead on input line 120. +LaTeX Font Info: Trying to load font information for U+lasy on input line 12 +0. + +(/usr/share/texmf-dist/tex/latex/base/ulasy.fd +File: ulasy.fd 1998/08/17 v2.2e LaTeX symbol font definitions +) +LaTeX Font Info: Trying to load font information for U+stmry on input line 1 +20. + +(/usr/share/texmf-dist/tex/latex/stmaryrd/Ustmry.fd) +LaTeX Font Info: Trying to load font information for U+rsfs on input line 12 +0. + +(/usr/share/texmf-dist/tex/latex/jknapltx/ursfs.fd +File: ursfs.fd 1998/03/24 rsfs font definition file (jk) +) +LaTeX Font Info: Font shape `OT1/ptm/bx/n' in size <9> not available +(Font) Font shape `OT1/ptm/b/n' tried instead on input line 123. +LaTeX Font Info: Font shape `OT1/ptm/bx/n' in size <7> not available +(Font) Font shape `OT1/ptm/b/n' tried instead on input line 123. +LaTeX Font Info: Font shape `OT1/ptm/bx/n' in size <5> not available +(Font) Font shape `OT1/ptm/b/n' tried instead on input line 123. +LaTeX Font Info: Trying to load font information for TU+pcr on input line 12 +4. +LaTeX Font Info: No file TUpcr.fd. on input line 124. + + +LaTeX Font Warning: Font shape `TU/pcr/m/n' undefined +(Font) using `TU/lmr/m/n' instead on input line 124. + + +LaTeX Font Warning: Font shape `TU/pcr/b/n' undefined +(Font) using `TU/pcr/m/n' instead on input line 124. + + +LaTeX Font Warning: Font shape `TU/ptm/m/it' undefined +(Font) using `TU/ptm/m/n' instead on input line 171. + +[1 + +] + +LaTeX Font Warning: Command \scriptsize invalid in math mode on input line 206. + + + +LaTeX Font Warning: Font shape `TU/ptm/m/sc' undefined +(Font) using `TU/ptm/m/n' instead on input line 206. + + +LaTeX Font Warning: Command \scriptsize invalid in math mode on input line 210. + + + +LaTeX Font Warning: Command \scriptsize invalid in math mode on input line 214. + + + +LaTeX Font Warning: Command \scriptsize invalid in math mode on input line 218. + + + +LaTeX Font Warning: Command \scriptsize invalid in math mode on input line 225. + + + +LaTeX Font Warning: Command \scriptsize invalid in math mode on input line 247. + + +[2] + +LaTeX Font Warning: Command \scriptsize invalid in math mode on input line 251. + + + +LaTeX Font Warning: Command \scriptsize invalid in math mode on input line 255. + + +Package atveryend Info: Empty hook `BeforeClearDocument' on input line 257. +[3] +Package atveryend Info: Empty hook `AfterLastShipout' on input line 257. + (./08.aux) +Package atveryend Info: Empty hook `AtVeryEndDocument' on input line 257. +Package atveryend Info: Executing hook `AtEndAfterFileList' on input line 257. +Package rerunfilecheck Info: File `08.out' has not changed. +(rerunfilecheck) Checksum: 94B461501762BD2435B940A339C1067F;151. + + +LaTeX Font Warning: Size substitutions with differences +(Font) up to 1.3pt have occurred. + + +LaTeX Font Warning: Some font shapes were not available, defaults substituted. + +Package atveryend Info: Empty hook `AtVeryVeryEnd' on input line 257. + ) +Here is how much of TeX's memory you used: + 13465 strings out of 479284 + 196344 string characters out of 5886220 + 451721 words of memory out of 5000000 + 30874 multiletter control sequences out of 15000+600000 + 560678 words of font info for 115 fonts, out of 8000000 for 9000 + 1348 hyphenation exceptions out of 8191 + 45i,19n,47p,274b,1298s stack positions out of 5000i,500n,10000p,200000b,80000s + +Output written on 08.pdf (3 pages). diff --git a/lectures/08.out b/lectures/08.out new file mode 100644 index 00000000..d7b3695d --- /dev/null +++ b/lectures/08.out @@ -0,0 +1,3 @@ +\BOOKMARK [1][-]{section.1}{Arrays}{}% 1 +\BOOKMARK [1][-]{section.2}{Operational\040Semantics}{}% 2 +\BOOKMARK [1][-]{section.3}{Stack\040Machine}{}% 3 diff --git a/lectures/08.pdf b/lectures/08.pdf new file mode 100644 index 00000000..652e05ad Binary files /dev/null and b/lectures/08.pdf differ diff --git a/runtime/runtime.c b/runtime/runtime.c index e44136e7..a8597a0e 100644 --- a/runtime/runtime.c +++ b/runtime/runtime.c @@ -1,93 +1,103 @@ -# include -# include -# include -# include +#include +#include +#include +#include -# define UNBOXED(x) (((int) (x)) & 0x0001) -# define UNBOX(x) (((int) (x)) >> 1) -# define BOX(x) ((((int) (x)) << 1) | 0x0001) +#define UNBOXED(x) (((int)(x)) & 0x0001) +#define UNBOX(x) (((int)(x)) >> 1) +#define BOX(x) ((((int)(x)) << 1) | 0x0001) -# define STRING_TAG 0x00000001 -# define ARRAY_TAG 0x00000003 +#define STRING_TAG 0x00000001 +#define ARRAY_TAG 0x00000003 -# define LEN(x) ((x & 0xFFFFFFF8) >> 3) -# define TAG(x) (x & 0x00000007) +#define LEN(x) ((x & 0xFFFFFFF8) >> 3) +#define TAG(x) (x & 0x00000007) -# define TO_DATA(x) ((data*)((char*)(x)-sizeof(int))) +#define TO_DATA(x) ((data *)((char *)(x) - sizeof(int))) -typedef struct { - int tag; +typedef struct +{ + int tag; char contents[0]; -} data; +} data; -int Blength (void *p) { +int Blength(void *p) +{ data *a = TO_DATA(p); return BOX(LEN(a->tag)); } -void* Barray (int n0, ...) { - int n = UNBOX(n0); - va_list args; - int i, ai; - data *r; +void *Barray(int n0, ...) +{ + int n = UNBOX(n0); + va_list args; + int i, ai; + data *r; - r = (data*) malloc (sizeof(int) * (n+1)); + r = (data *)malloc(sizeof(int) * (n + 1)); r->tag = ARRAY_TAG | (n << 3); - + va_start(args, n); - - for (i = 0; icontents)[i] = ai; + ((int *)r->contents)[i] = ai; } - + va_end(args); return r->contents; } -void* Bstring (void *p) { - int n = strlen (p); +void *Bstring(void *p) +{ + int n = strlen(p); data *s; - - s = (data*) malloc (n + 1 + sizeof (int)); + + s = (data *)malloc(n + 1 + sizeof(int)); s->tag = STRING_TAG | (n << 3); - strncpy (s->contents, p, n + 1); + strncpy(s->contents, p, n + 1); return s->contents; } -void* Belem (void *p, int i0) { +void *Belem(void *p, int i0) +{ int i = UNBOX(i0); data *a = TO_DATA(p); - - if (TAG(a->tag) == STRING_TAG) { - return (void*) BOX(a->contents[i]); + + if (TAG(a->tag) == STRING_TAG) + { + return (void *)BOX(a->contents[i]); } - - return (void*) ((int*) a->contents)[i]; + + return (void *)((int *)a->contents)[i]; } -void* Bsta (int i0, void *v, void *x) { - int i = UNBOX (i0); - - if (TAG(TO_DATA(x)->tag) == STRING_TAG) - ((char*) x)[i] = UNBOX((int) v); - else ((int*) x)[i] = (int) v; +void *Bsta(int i0, void *v, void *x) +{ + int i = UNBOX(i0); + + if (TAG(TO_DATA(x)->tag) == STRING_TAG) + ((char *)x)[i] = UNBOX((int)v); + else + ((int *)x)[i] = (int)v; return v; } -void Lwrite (int x) { - printf ("%d\n", UNBOX (x)); +void Lwrite(int x) +{ + printf("%d\n", UNBOX(x)); } -int Lread () { +int Lread() +{ int result; - scanf ("%d", &result); + scanf("%d", &result); return BOX(result); } - diff --git a/src/Expr.lama b/src/Expr.lama index 0dc1a5de..ad802531 100644 --- a/src/Expr.lama +++ b/src/Expr.lama @@ -99,11 +99,103 @@ fun evalList (c, exprs) { esac } -(* Assignment *) +fun evalSeq(c, exprs) { + foldl (fun ([c, _], e) { eval (c, e) }, [c, Error], exprs) +} + fun eval (c@[s, w], expr) { - failure ("evalExpr not implemented\n") + case expr of + Assn (tgt, val) -> + case evalList(c, {tgt, val}) of [[s, w], {tgt, val}] -> + case tgt of + Ref (tgt) -> [[s <- [tgt, val], w], val] + | ElemRef (arr, idx) -> arr[idx] := val; [[s, w], val] + esac + esac + | Seq (e1, e2) -> evalSeq (c, {e1, e2}) + | Skip -> [c, Unit] + | If (cond, thn, els) -> + case eval (c, cond) of + [c, 0] -> eval (c, els) + | [c, _] -> eval (c, thn) + esac + | whl@While (cond, body) -> + case eval (c, cond) of + [c, 0] -> [c, Unit] + | [c, _] -> + case evalList (c, {body, whl}) of + [c, _] -> [c, Unit] + esac + esac + | rpt@Repeat (body, cond) -> + case evalSeq (c, {body, cond}) of + [c, 0] -> eval (c, rpt) + | [c, _] -> [c, Unit] + esac + | Var (id) -> [c, lookup (s, id)] + | Ref (id) -> [c, Ref (id)] + | Const (val) -> [c, val] + | String (str) -> [c, str] + | Array (xs) -> + case evalList (c, xs) of [c, xs] -> + [c, listArray(xs)] + esac + | Elem (arr, idx) -> + case evalList (c, {arr, idx}) of [c, {arr, idx}] -> + [c, arr[idx]] + esac + | ElemRef (arr, idx) -> + case evalList (c, {arr, idx}) of [c, {arr, idx}] -> + [c, ElemRef (arr, idx)] + esac + | Binop (op, lhs, rhs) -> + case evalList (c, {lhs, rhs}) of + [c, {lhs, rhs}] -> [c, evalOp (op, lhs, rhs)] + esac + | Scope (defs, e) -> + case defs of + {} -> eval (c, e) -- LocalScope + | defs -> { + local ls = foldl( + fun (s, def) { + case def of + Fun (name, params, body) -> addFunction (s, name, params, body) + | Local (names) -> addNames (s, names) + esac + }, + enterScope (s), + defs + ); + case eval ([ls, w], e) of [[s, w], val] -> [[leaveScope (s), w], val] esac + } + esac + | Ignore (e) -> + case eval (c, e) of + [c, _] -> [c, Unit] + esac + | Call (name, args) -> + case lookupFun (s, name) of Fun (params, body) -> + case evalList (c, args) of [[s, w], args] -> + case body of + External -> case evalBuiltin(name, args, w) of [retval, w] -> + [[s, w], retval] + esac + | _ -> case eval ([foldl (fun (s, [name, value]) { + addName(s, name, value) + }, enterFunction(s), zip(params, args)), w], body) of [[s_, w], retval] -> + [[leaveFunction(s, getGlobal(s_)), w], retval] + esac + esac + esac + esac + | Builtin (name, args) -> + case evalList (c, args) of [[s, w], args] -> + case evalBuiltin(name, args, w) of [retval, w] -> + [[s, w], retval] + esac + esac + esac } -(* End *) -- Evaluates a program with a given input and returns an output public fun evalExpr (input, expr) { diff --git a/src/Parser.lama b/src/Parser.lama index b95f37ae..5e0dad97 100644 --- a/src/Parser.lama +++ b/src/Parser.lama @@ -129,9 +129,38 @@ local primary = memo $ eta syntax ( | Some (args) -> assertValue (a, Call (x, args)) esac }} | -(* Assignment *) - $(failure ("the rest of primary parsing in not implemented\n"))), -(* End *) + inbr[s("("), exp, s(")")] | + inbr[s("{"), scopeExpr, s("}")] | + loc=pos kSkip {fun (a) { assertVoid(a, Skip, loc) }} | + -kIf ifNoKw | + loc=pos kWhile cond=exp kDo body=scopeExpr kOd {fun (a) { assertVoid(a, While (cond (Val), body (Void)), loc) }} | + loc=pos kFor init=scopeExpr s[","] cond=exp s[","] step=exp kDo body=scopeExpr kOd {fun (a) { + local whl = distributeScope(init (Void), fun (i) { + Seq (i, While (cond (Val), Seq (body (Void), step (Void)))) + }); + assertVoid(a, whl, loc) + }} | + loc=pos kRepeat body=scopeExpr kUntil cond=basic {fun (a) { + local rpt = distributeScope(body (Void), fun (b) { + Repeat (b, cond (Val)) + }); + assertVoid(a, rpt, loc) + }} + ), + ifNoKw = memo $ eta syntax ( + cond=exp kThen thn=scopeExpr els=( + -kElse scopeExpr -kFi | + -kElif ifNoKw | + loc=pos kFi {fun (a) { assertVoid(a, Skip, loc) }} + ) {fun (a) { If (cond (Val), thn (a), els (a)) }} + ), + defItem = memo $ eta syntax ( + kFun fname=lident params=inbr[s("("), list0(lident), s(")")] + body=inbr[s("{"), scopeExpr, s("}")] { Fun (fname, params, body (Weak)) } | + kLocal defs=list[syntax( + id=lident val=(-s["="] v=basic {v (Val)})? { [id, val] } + )] s[";"] { Local (defs) } + ), basic = memo $ eta (expr ({[Right, singleton ([s (":="), fun (l, loc, r) { fun (a) {assertValue (a, Assn (l (Ref), r (Val)), loc)} @@ -142,7 +171,7 @@ local primary = memo $ eta syntax ( [Left , map (binop, {"+", "-"})], [Left , map (binop, {"*", "/", "%"})] }, - postfix)(id)), + postfix)), postfix = memo $ eta syntax (loc=pos e=primary ps=(i=inbr[s("["), exp, s("]")] {Index (i (Val))} | -s["."] kLength {Length} )* {fun (a) {foldl (fun (e, p) { diff --git a/src/SM.lama b/src/SM.lama index 0af2a8de..47169855 100644 --- a/src/SM.lama +++ b/src/SM.lama @@ -123,9 +123,51 @@ fun eval (env, w, insns) { -- Core interpreter: takes a configuration and a program, returns a configuration (* Assignment *) fun eval (c@[st, cst, s, w], insns) { - failure ("SM interpreter is not implemented\n") + case insns of + {} -> c + | ins : insns_ -> + eval (case ins of + CONST (n) -> [ n : st, cst, s, w ] + | STRING (x) -> [ x : st, cst, s, w ] + | ARRAY (n) -> case take (st, n) of [st, arr] -> [listArray (arr) : st, cst, s, w] esac + | BINOP (op) -> case st of a : b : rest -> [ evalOp (op, a, b) : rest, cst, s, w ] esac + | LD (x) -> [ lookup(s, x) : st, cst, s, w ] + | LDA (x) -> [ Ref (x) : st, cst, s, w ] + | ST (x) -> case st of a : rest -> assign(s, x, a); [ rest, cst, s, w ] esac + | STI -> case st of a : Ref (x) : rest -> assign (s, x, a); [ a : rest, cst, s, w ] esac + | STA -> case st of a : idx : arr : rest -> arr[idx] := a; [ a : rest, cst, s, w ] esac + | ELEM -> case st of idx : arr : rest -> [ arr[idx] : rest, cst, s, w ] esac + | DUP -> case st of a : rest -> [ a : a : rest, cst, s, w ] esac + | DROP -> case st of a : rest -> [ rest, cst, s, w ] esac + | CJMP (cond, where) -> case st of a : rest -> [ rest, cst, s, w ] esac + | CALL (func, argc) -> [st, [s, insns_] : cst, s, w] + | BUILTIN (name, argc) -> + case take (st, argc) of [st, argv] -> + case evalBuiltin (name, argv, w) of [retval, w] -> + [ retval : st, cst, s, w ] + esac + esac + | GLOBAL (x) -> assign (s, Glb (x), 0); [st, cst, s, w] + | BEGIN (func, argc, varc) -> + case take (st, argc) of [st, argv] -> + [st, cst, foldl (fun ([s, i], arg) { + assign (s, Arg (i), arg); + [s, i + 1] + }, [makeState (argc, varc), 0], argv)[0], w] + esac + | END -> case cst of [s, _] : cst -> [st, cst, s, w] | _ -> c esac + | _ -> c + esac, + case ins of + JMP (where) -> fromLabel (env, where) + | CALL (func, argc) -> fromLabel (env, func) + | CJMP (cond, where) -> case st of a : rest -> + if (a == (case cond of "z" -> 0 | _ -> 1 esac)) then fromLabel (env, where) else insns_ fi esac + | END -> case cst of [_, ret] : cst -> ret | _ -> {} esac + | _ -> insns_ + esac) + esac } - (* End *) eval ([{}, {}, makeState (0, 0), w], insns) [3].getOutput } @@ -200,9 +242,13 @@ fun makeCompEnv (nLabels, scopeDepth, state, nLocals, nArgs, functions) { -- Lookups a name of a function fun lookupFun (name) { - case lookup (state, name) of - x@Fun (_, _) -> x - | _ -> error (sprintf ("the name ""%s"" does not designate a function", name), getLoc (name)) + case name of + "read" -> Builtin + | "write" -> Builtin + | _ -> case lookup (state, name) of + x@Fun (_, _) -> x + | _ -> error (sprintf ("the name ""%s"" does not designate a function", name), getLoc (name)) + esac esac } @@ -352,7 +398,182 @@ fun addVars (env, names) { -- environment (use env.beginFun) and register arguments -- (use env.addArg). -public fun compileSM (expr) { - failure ("compileSM not implemented\n") +fun compileImpl (env, stmt) { + case stmt of + Assn (tgt, e) -> + case compileImpl (env, e) of [env, smVal] -> + case tgt of + ElemRef (arr, idx) -> + case compileImpl (env, arr) of [env, arr] -> + case compileImpl (env, idx) of [env, idx] -> + [env, {arr, idx, smVal, STA}] + esac + esac + | _ -> + case compileImpl(env, tgt) of [env, tgt] -> + [env, {tgt, smVal, STI}] + esac + esac + esac + | Seq (e1, e2) -> + case compileImpl (env, e1) of [env, sm1] -> + case compileImpl (env, e2) of [env, sm2] -> + [env, {sm1, sm2}] + esac + esac + | Skip -> [env, {}] + | If (cond, thn, els) -> + case compileImpl (env, cond) of [env, smCond] -> + case genLabels (env, 2) of [labelEls, labelEnd, env] -> + case compileImpl (env, thn) of [env, smThn] -> + case compileImpl (env, els) of [env, smEls] -> + [env, + { smCond + , CJMP ("z", labelEls) + , smThn + , JMP (labelEnd) + , LABEL (labelEls) + , smEls + , LABEL (labelEnd) + } + ] + esac + esac + esac + esac + | While (cond, body) -> + case compileImpl (env, cond) of [env, smCond] -> + case genLabels (env, 2) of [labelBody, labelCond, env] -> + case compileImpl (env, body) of [env, smBody] -> + [env, + { JMP (labelCond) + , LABEL (labelBody) + , smBody + , LABEL (labelCond) + , smCond + , CJMP ("nz", labelBody) + } + ] + esac + esac + esac + | Repeat (body, cond) -> + case compileImpl (env, cond) of [env, smCond] -> + case genLabels (env, 1) of [labelBody, env] -> + case compileImpl (env, body) of [env, smBody] -> + [env, + { LABEL (labelBody) + , smBody + , smCond + , CJMP ("z", labelBody) + } + ] + esac + esac + esac + | Var (id) -> [env, singleton (LD (lookupVar(env, id)))] + | Ref (id) -> [env, singleton (LDA (lookupVar(env, id)))] + | Const (n) -> [env, singleton (CONST (n))] + | String (x) -> [env, singleton (STRING (x))] + | Array (xs) -> + case foldl (fun ([env, xs], x) { + case compileImpl (env, x) of [env, t] -> + [env, {xs, t}] + esac + }, [env, {}], xs) of [env, ts] -> + [env, {ts, ARRAY (size(xs))}] + esac + | Elem (arr, idx) -> + case compileImpl (env, arr) of [env, arr] -> + case compileImpl (env, idx) of [env, idx] -> + [env, {arr, idx, ELEM}] + esac + esac + | Binop (op, lhs, rhs) -> + case compileImpl (env, lhs) of [env, smLhs] -> + case compileImpl (env, rhs) of [env, smRhs] -> + [env, {smRhs, smLhs, BINOP (op)}] + esac + esac + | Ignore (e) -> + case compileImpl (env, e) of [env, sm] -> + [env, {sm, DROP}] + esac + | Scope (defs, x) -> + case foldl (fun ([env, globals], def) { + case def of + Local (names) -> [addVars (env, names), + if isGlobal (env) then + map(fun (x) { GLOBAL (x) }, names) +++ globals + else globals fi + ] | + Fun (name, params, body) -> + case genFunLabel (env, name) of [label, env] -> + [addFun (env, name, label, params.size), globals] + esac + esac + }, [beginScope(env), {}], defs) of [env, globals] -> + case foldl (fun (env, def) { + case def of Fun (name, params, body) -> + rememberFun (env, lookupFun (env, name)[0], params, body) + | _ -> env + esac + }, env, defs) of env -> + case compileImpl (env, x) of [env, t] -> + [endScope (env), { globals, t }] + esac + esac + esac + | Call (name, args) -> + case foldl (fun ([env, res], arg) { + case compileImpl (env, arg) of [env, t] -> + [env, {res, t}] + esac + }, [env, {}], args) of [env, res] -> case lookupFun (env, name) of Fun (label, n) -> + [env, {res, CALL (label, n)}] + | Builtin -> [env, {res, BUILTIN (name, size(args))}] + esac + esac + | Builtin (name, args) -> + case foldl (fun ([env, res], arg) { + case compileImpl (env, arg) of [env, t] -> + [env, {res, t}] + esac + }, [env, {}], args) of [env, res] -> + [env, {res, BUILTIN (name, size(args))}] + esac + esac +} + +fun compileSMFun (env, sm) { + case getFuns (env) of [funs, env] -> + case funs of {} -> [env, sm] | _ -> + case foldl (fun ([env, sm], Fun (name, params, body, st)) { + case addArgs(beginFun (env, st), params) of env -> + case compileImpl(env, body) of [env, bodySM] -> + [env, { + sm, + LABEL (name), + BEGIN (name, params.size, getLocals (env)), + bodySM, + END + }] + esac + esac + }, [env, sm], funs) of [env, sm] -> + compileSMFun (env, sm) + esac + esac + esac +} + +public fun compileSM (stmt) { + case compileImpl (beginScope (initCompEnv ()), stmt) of [env, sm] -> + deepFlatten(compileSMFun (env, { + LABEL ("main"), + BEGIN ("main", 0, getLocals (env)), + sm, + END + })[1]) + esac } -(* End *) diff --git a/src/X86.lama b/src/X86.lama index a66e5e2f..9a816445 100644 --- a/src/X86.lama +++ b/src/X86.lama @@ -488,7 +488,7 @@ fun call (env, fLabel, nA) { [env, pushArgs] -> case env.allocate of [y, env] -> - [env, listBuffer (flatten $ {pushRegs, + [env, listBuffer (deepFlatten $ {pushRegs, reverse (pushArgs), case extraArg of Some (n) -> Push (L (box $ n)) | _ -> {} esac, Call (fLabel), @@ -510,34 +510,163 @@ fun compile (env, code) { fun ([env, scode], i) { local code = scode <+ Meta ("# " ++ showSMInsn (i) ++ "\n"); case i of - READ -> + CONST (n) -> case env.allocate of - [s, env] -> [env, code <+ Call ("Lread") <+ Mov (eax, s)] - esac - | WRITE -> - case env.pop of - [s, env] -> [env, code <+ Push (s) <+ Call ("Lwrite") <+ Pop (eax)] + [s, env] -> [env, code <+ Mov (L (box(n)), s)] esac - (* Assignment *) - - -- Some guidelines for generating function calls: - -- - -- 1. generate instructions to save live registers on the X86 stack (use - -- env.liveRegisters (number of arguments); - -- 2. generate instructions to move actual parameters from the symbolic - -- stack to the hardware one; - -- 3. generate the call itself; - -- 4. discard the actual parameters from the stack; - -- 5. restore saved live registers. - -- - -- Some guidelines for generating functions: - -- - -- 1. generate proper prologue for BEGIN instruction (use "prologue" helper); use - -- env.enterFunction to create a proper environment; - -- 2. generate epilogue for END instruction. - - | _ -> failure ("codegeneration for instruction %s is not yet implemented\n", i.string) - (* End *) + | STRING (s) -> + case env.addString (s) of [env, name] -> + case env.allocate of [y, env] -> + case call (env, "Bstring", 1) of [env, x] -> + [env, code <+> move (M ("$" ++ name), y) <+> x] + esac + esac + esac + | ARRAY (n) -> + case call (env, "Barray", n) of [env, x] -> + [env, code <+> x] + esac + | STA -> + case call (env, "Bsta", 2) of [env, x] -> + [env, code <+> x] + esac + | ELEM -> + case call (env, "Belem", 2) of [env, x] -> + [env, code <+> x] + esac + | BUILTIN (name, argc) -> + case name of "read" -> + case call (env, "Lread", argc) of [env, x] -> + [env, code <+> x] + esac + | "write" -> + case call (env, "Lwrite", argc) of [env, x] -> + [env, code <+> x] + esac + | _ -> + case call (env, sprintf("B%s", name), argc) of [env, x] -> + [env, code <+> x] + esac + esac + | LD (x) -> + case env.allocate of + [s, env] -> [env, code <+> move (env.loc (x), s)] + esac + | ST (x) -> + case env.pop of + [s, env] -> [env, code <+> move (s, env.loc (x))] + esac + | LABEL (s) -> [if isBarrier (env) then retrieveStack (env, s) else env fi, code <+ Label (s)] + | JMP (l) -> [setBarrier (setStack (env, l)), code <+ Jmp (l)] + | CJMP (c, l) -> + case env.pop of [s, env] -> + [setStack (env, l), code + <+ Binop ("cmp", L (box(0)), s) + <+ CJmp (c, l) + ] + esac + | BINOP (op) -> + case env.pop2 of + [l, r, env] -> + case env.allocate of + [s, env] -> { + fun oneOf (x, lst) { + case find (fun (v) { compare (x, v) == 0 }, lst) of + Some (_) -> true + | _ -> false + esac + } + if op.oneOf ({"+", "-"}) then + [env, code + <+ Dec (case op of "+" -> l | "-" -> r esac) + <+ Mov (l, eax) + <+ Binop (op, r, eax) + <+ Mov (eax, s) + ] + elif op.oneOf ({"*"}) then + [env, code + <+ Sar1 (l) + <+ Sar1 (r) + <+ Mov (l, eax) + <+ Binop(op, r, eax) + <+ Sal1 (eax) + <+ Or1 (eax) + <+ Mov (eax, s) + ] + elif op.oneOf ({"/", "%"}) then + [env, code + <+ Sar1(l) + <+ Sar1(r) + <+ Mov (l, eax) + <+ Cltd + <+ IDiv (r) + <+ Sal1 (case op of "/" -> eax | "%" -> edx esac) + <+ Or1 (case op of "/" -> eax | "%" -> edx esac) + <+ Mov (case op of "/" -> eax | "%" -> edx esac, s) + ] + elif op.oneOf ({"==", "!=", "<", "<=", ">", ">="}) then + [env, code + <+ Mov (l, eax) + <+ Binop ("cmp", r, eax) + <+ Mov (L (0), eax) + <+ Set (suffix (op), "%al") + <+ Sal1 (eax) + <+ Or1 (eax) + <+ Mov (eax, s) + ] + elif op.oneOf ({"&&", "!!"}) then + [env, code + <+ Sar1 (l) + <+ Sar1 (r) + <+ Mov (L (0), eax) + <+ Binop ("cmp", L (0), l) + <+ Set ("nz", "%al") + <+ Mov (L (0), edx) + <+ Binop ("cmp", L (0), r) + <+ Set ("nz", "%dl") + <+ Binop (op, edx, eax) + <+ Sal1 (eax) + <+ Or1 (eax) + <+ Mov (eax, s) + ] + else + failure ("unexpected operator in codegeneration: %s\n", op) + fi + } + esac + esac + | LDA (x) -> + case env.allocate of [s, env] -> + [env, code <+ Lea (env.loc(x), eax) + <+ Mov (eax, s) + ] + esac + | STI -> + case env.pop of [s, env] -> + case env.peek of ref -> + [env, code <+> move (s, I (0, ref)) <+> move (s, ref)] + esac + esac + | DROP -> + case env.pop of [_, env] -> + [env, code] + esac + | BEGIN (name, argc, varc) -> [ + enterFunction (env, name, varc), + code <+> prologue(name) + ] + | END -> + case epilogue(env) of [env, x] -> + [env, code <+> x] + esac + | GLOBAL (x) -> [ + addGlobal (env, x), + code + ] + | CALL (name, argc) -> + case call(env, name, argc) of [env, x] -> + [env, code <+> x] + esac esac }, [env, emptyBuffer ()], code) }