Browse Source

fixes for x86 abstract syntax

Peter Thiemann 3 years ago
parent
commit
578c37f436
2 changed files with 29 additions and 9 deletions
  1. 25 7
      book.tex
  2. 4 2
      defs.tex

+ 25 - 7
book.tex

@@ -2526,6 +2526,11 @@ programs, so we define an abstract syntax for x86 in
 Figure~\ref{fig:x86-int-ast}. We refer to this language as
 Figure~\ref{fig:x86-int-ast}. We refer to this language as
 \LangXInt{}.
 \LangXInt{}.
 %
 %
+{\if\edition\pythonEd%
+  The main difference compared to the concrete syntax of \LangXInt{}
+  (Figure~\ref{fig:x86-int-concrete}) is that labels, instruction
+  names, and register names are explicitly represented by strings. 
+\fi} %
 {\if\edition\racketEd  
 {\if\edition\racketEd  
 The main difference compared to the concrete syntax of \LangXInt{}
 The main difference compared to the concrete syntax of \LangXInt{}
 (Figure~\ref{fig:x86-int-concrete}) is that labels are not allowed in
 (Figure~\ref{fig:x86-int-concrete}) is that labels are not allowed in
@@ -2545,6 +2550,12 @@ node includes an integer for representing the arity of the function,
 i.e., the number of arguments, which is helpful to know during
 i.e., the number of arguments, which is helpful to know during
 register allocation (Chapter~\ref{ch:register-allocation-Lvar}).
 register allocation (Chapter~\ref{ch:register-allocation-Lvar}).
 
 
+\newcommand{\allastregisters}{\skey{rsp} \MID \skey{rbp} \MID \skey{rax} \MID \skey{rbx} \MID \skey{rcx}
+              \MID \skey{rdx} \MID \skey{rsi} \MID \skey{rdi} \MID \\
+              && \skey{r8} \MID \skey{r9} \MID \skey{r10}
+              \MID \skey{r11} \MID \skey{r12} \MID \skey{r13}
+              \MID \skey{r14} \MID \skey{r15}}
+
 \begin{figure}[tp]
 \begin{figure}[tp]
 \fbox{
 \fbox{
 \begin{minipage}{0.98\textwidth}
 \begin{minipage}{0.98\textwidth}
@@ -2569,15 +2580,15 @@ register allocation (Chapter~\ref{ch:register-allocation-Lvar}).
 {\if\edition\pythonEd
 {\if\edition\pythonEd
 \[
 \[
 \begin{array}{lcl}
 \begin{array}{lcl}
-\Reg &::=& \allregisters{} \\
+\Reg &::=& \allastregisters{} \\
 \Arg &::=&  \IMM{\Int} \MID \REG{\Reg}
 \Arg &::=&  \IMM{\Int} \MID \REG{\Reg}
    \MID \DEREF{\Reg}{\Int} \\
    \MID \DEREF{\Reg}{\Int} \\
-\Instr &::=& \BININSTR{\code{addq}}{\Arg}{\Arg} 
-       \MID \BININSTR{\code{subq}}{\Arg}{\Arg} \\
-       &\MID& \BININSTR{\code{movq}}{\Arg}{\Arg}
-       \MID \UNIINSTR{\code{negq}}{\Arg}\\
-       &\MID& \PUSHQ{\Arg} \MID \POPQ{\Arg}\\
-       &\MID& \CALLQ{\itm{label}}{\itm{int}} \MID \RETQ{} \MID \JMP{\itm{label}} \\
+\Instr &::=& \BININSTR{\scode{addq}}{\Arg}{\Arg} 
+       \MID \BININSTR{\scode{subq}}{\Arg}{\Arg} \\
+       &\MID& \BININSTR{\scode{movq}}{\Arg}{\Arg}
+       \MID \UNIINSTR{\scode{negq}}{\Arg}\\
+       &\MID&  \PUSHQ{\Arg} \MID \POPQ{\Arg} \\
+       &\MID& \CALLQ{\itm{label}}{\itm{int}} \MID \RETQ{}  \MID \JMP{\itm{label}} \\
 \LangXIntM{} &::= & \XPROGRAM{}{\Instr^{*}}{}
 \LangXIntM{} &::= & \XPROGRAM{}{\Instr^{*}}{}
 \end{array}
 \end{array}
 \]
 \]
@@ -3565,6 +3576,13 @@ For $\SEQ{s}{t}$, you can translate the statement $s$ and tail $t$
 recursively and then append the resulting instructions.
 recursively and then append the resulting instructions.
 \fi}
 \fi}
 
 
+{\if\edition\pythonEd
+We recommend that you use the function \code{utils.label\_name()} to
+transform a string into an label argument suitably suitable for, e.g.,
+the target of the \code{callq} instruction. This practice makes your
+compiler portable across Linus and Mac OS X, which requires an underscore prefixed to
+all labels.
+\fi}
 \begin{exercise}
 \begin{exercise}
   \normalfont
   \normalfont
 {\if\edition\racketEd
 {\if\edition\racketEd

+ 4 - 2
defs.tex

@@ -108,7 +108,9 @@
 \newcommand{\Var}{\itm{var}}
 \newcommand{\Var}{\itm{var}}
 \newcommand{\Op}{\itm{op}}
 \newcommand{\Op}{\itm{op}}
 \newcommand{\key}[1]{\texttt{#1}}
 \newcommand{\key}[1]{\texttt{#1}}
+\newcommand{\skey}[1]{\texttt{'#1'}}
 \newcommand{\code}[1]{\texttt{#1}}
 \newcommand{\code}[1]{\texttt{#1}}
+\newcommand{\scode}[1]{\texttt{'#1'}}
 
 
 \newcommand{\LP}{\key{(}}
 \newcommand{\LP}{\key{(}}
 \newcommand{\RP}{\key{)}}
 \newcommand{\RP}{\key{)}}
@@ -325,8 +327,8 @@
 \newcommand{\BININSTR}[3]{\key{Instr}\LP #1 \key{,} \LS #2 \key{,} #3 \RS \RP}
 \newcommand{\BININSTR}[3]{\key{Instr}\LP #1 \key{,} \LS #2 \key{,} #3 \RS \RP}
 \newcommand{\UNIINSTR}[2]{\key{Instr}\LP #1 \key{,} \LS #2 \RS \RP}
 \newcommand{\UNIINSTR}[2]{\key{Instr}\LP #1 \key{,} \LS #2 \RS \RP}
 \newcommand{\CALLQ}[2]{\key{Callq}\LP #1 \key{,} #2 \RP}
 \newcommand{\CALLQ}[2]{\key{Callq}\LP #1 \key{,} #2 \RP}
-\newcommand{\PUSHQ}[1]{\key{Instr}\LP \code{pushq} \key{,} \LS #1 \RS \RP}
-\newcommand{\POPQ}[1]{\key{Instr}\LP \code{popq} \key{,} \LS #1 \RS \RP}
+\newcommand{\PUSHQ}[1]{\key{Instr}\LP \scode{pushq} \key{,} \LS #1 \RS \RP}
+\newcommand{\POPQ}[1]{\key{Instr}\LP \scode{popq} \key{,} \LS #1 \RS \RP}
 \newcommand{\JMP}[1]{\key{Jump}\LP #1 \RP}
 \newcommand{\JMP}[1]{\key{Jump}\LP #1 \RP}
 \newcommand{\JMPIF}[2]{\key{JumpIf}\LP #1 \key{,} #2 \RP}
 \newcommand{\JMPIF}[2]{\key{JumpIf}\LP #1 \key{,} #2 \RP}
 \newcommand{\RETQ}{\key{Retq}\LP\RP}
 \newcommand{\RETQ}{\key{Retq}\LP\RP}