\documentclass[11pt]{article}
\usepackage{palatino}
\usepackage{latexsym}
\usepackage{verbatim}
\usepackage{alltt}
\usepackage{amsmath,proof,amsthm,amssymb,enumerate}
\usepackage{china2e} % to get \Integer, \Natural
\usepackage{math-cmds}

\newcommand{\definition}[2]
  {\bigskip
   \begin{tabular}{p{1.5in}p{4.0in}}
        \textbf{#1} & #2 \\
        \end{tabular}
  }

\def\prop{\textsf{\,\,prop}}
\def\thm{\textsf{\,\,thm}}
\def\implies{\Rightarrow}

\title{Lecture Notes:\\
The \textsc{While} Language and \textsc{While3Addr} Representation}
\author{15-819O: Program Analysis \\
        Jonathan Aldrich \\
		{\tt jonathan.aldrich@cs.cmu.edu}}
\date{Lecture 2}

\begin{document}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}

\maketitle

\section{The \textsc{While} Language}

In this course, we will study the theory of analyses using a simple
programming language called \textsc{While}, along with various extensions.
The \textsc{While} language is at least as old as Hoare's 1969 paper on
a logic for proving program properties (to be discussed in a later lecture).
It is a simple imperative language, with assignment to local variables,
if statements, while loops, and simple integer and boolean expressions.

We will use the following metavariables to describe several different
categories of syntax.  The letter on the left will be used as a variable
representing a piece of a program, while on the right, we describe the kind
of program piece that variable represents:

\[
\begin{array}{ll}
S   & \mbox{statements}\\
a   & \mbox{arithmetic expressions}\\
x,y & \mbox{program variables}\\
n   & \mbox{number literals}\\
P   & \mbox{boolean predicates}\\
\end{array}
\]

The syntax of \textsc{While} is shown below.  Statements $S$ can be an
assignment $x := a$, a skip statement which does nothing (similar to
a lone semicolon or open/close bracket in C or Java), and if and while
statements whose condition is a boolean predicate $P$.  Arithmetic
expressions $a$ include variables $x$, numbers $n$, and one of several
arithmetic operators, abstractly represented by $op_a$.  Boolean
expressions include true, false, the negation of another boolean
expression, boolean operators $op_b$ applied to other boolean
expressions, and relational operators $op_r$ applied to arithmetic
expressions.

\[
\begin{array}{lll}
S & \bnfdef & x := a \\
  & \bnfalt & \mbox{skip} \\
  & \bnfalt & S_1;~ S_2 \\
  & \bnfalt & \mbox{if}~ P ~\mbox{then}~ S_1 ~\mbox{else}~ S_2 \\
  & \bnfalt & \mbox{while}~ P ~\mbox{do}~ S \\[1ex]

a & \bnfdef & x \\
  & \bnfalt & n \\
  & \bnfalt & a_1 ~op_a~ a_2 \\[1ex]

op_a & \bnfdef & + \bnfalt - \bnfalt * \bnfalt /\\[1ex]

P & \bnfdef & \mbox{true} \\
  & \bnfalt & \mbox{false} \\
  & \bnfalt & \mbox{not}~ P \\
  & \bnfalt & P_1 ~op_b~ P_2 \\
  & \bnfalt & a_1 ~op_r~ a_2 \\[1ex]

op_b & \bnfdef & \mbox{and} \bnfalt \mbox{or} \\[1ex]

op_r & \bnfdef & <~ \bnfalt ~\le~ \bnfalt ~=~ \bnfalt ~>~ \bnfalt ~\ge~

\end{array}
\]


\section{\textsc{While3Addr}: A Representation for Analysis}

We could define the semantics of \textsc{While} directly---and indeed we will do so when studying Hoare Logic.\footnote{The supplemental Nielson et al. text also defines the semantics of the \textsc{While} language given here.}  For program analysis, however, the source-like definition of \textsc{While} is somewhat inconvenient.  Even a simple language such as \textsc{While} can be complex to define.  For example, \textsc{While} has three separate syntactic forms---statements, arithmetic expressions, and boolean predicates---and we would have to define the semantics of each separately.  A simpler and more regular representation of programs will help make our formalism simpler.

As a starting point, we will eliminate recursive arithmetic and boolean expressions and replace them with simple atomic statement forms, which are called instructions after the assembly language instructions that they resemble.  For example, an assignment statement of the form $w = x*y+z$ will be rewritten as a multiply instruction followed by an add instruction.  The multiply assigns to a temporary variable $t_1$, which is then used in the subsequent add:

\[
\begin{array}{l}
t_1 = x * y \\
w = t_1 + z
\end{array}
\]

As the translation from expressions to instructions suggests, program analysis is typically studied using a representation of programs that is not only simpler, but also lower-level than the source \textsc{While} language.  Typically high-level languages come with features that are numerous and complex, but can be reduced into a smaller set of simpler primitives.  Working at the lower level of abstraction thus also supports simplicity in the compiler.

Control flow constructs such as if and while are similarly translated into simpler goto and conditional branch constructs that jump to a particular (numbered) instruction.  For example, a statement of the form if $P$ then $S_1$ else $S_2$ would be translated into:

\[
\begin{array}{ll}
1: & \mbox{if}~P~\mbox{then goto}~4\\
2: & S_2\\
3: & \mbox{goto}~5\\
4: & S_1\\
5: & \textit{rest of program...}\
\end{array}
\]

The translation of a statement of the form while $P$ do $S$ is similar:

\[
\begin{array}{ll}
1: & \mbox{if not}~P~\mbox{goto}~4\\
2: & S\\
3: & \mbox{goto}~1\\
4: & \textit{rest of program...}\
\end{array}
\]

This form of code is often called 3-address code, because every instruction is of a simple form with at most two source operands and one result operand.  We now define the syntax for 3-address code produce from the \textsc{While} language, which we will call \textsc{While3Addr}.  This language consists of a set of simple instructions that load a constant into a variable, copy from one variable to another, compute the value of a variable from two others, or jump (possibly conditionally) to a new address $n$.  A program is just a map from addresses to instructions:

\[
\begin{array}{lll}
I & \bnfdef & x := n \\
  & \bnfalt & x := y \\
  & \bnfalt & x := y ~op~ z \\
  & \bnfalt & \mbox{goto}~n \\
  & \bnfalt & \mbox{if}~x~op_r~0~\mbox{goto}~n \\[1ex]
  
op & \bnfdef & + \bnfalt - \bnfalt * \bnfalt / \\[1ex]

op_r & \bnfdef & <~ \bnfalt ~=~  \\[1ex]

P & \in & \Natural \rightharpoonup I

\end{array}
\]

Formally defining a translation from a source language such as \textsc{While} to a lower-level intermediate language such as \textsc{While3Addr} is possible, but it is more appropriate for the scope of a compilers course.  For the purposes of this course, the examples above should suffice as intuition.  We will proceed by first formalizing program analysis in \textsc{While3Addr},
then having a closer look at its semantics in order to verify the correctness
of the program analysis.

\end{document}