\part{ACSE}

% SUMMARY
\begin{slide}{Overview}
\begin{itemize}
 \item The Advanced Compiler System for Education
 \item Compilation process
 \item The Language for Compiler Education
 \item Modules and libraries
\end{itemize}
\end{slide}

\begin{slide}{Compilation process}
\centering
\resizebox{1.1\textwidth}{!}
{\includegraphics{acse/compiler}}
\end{slide}

\begin{slide}{Compilation process: Front-End}
\begin{itemize}
 \item Source file tokenized by the \emph{scanner} (or lexer) 
		\begin{itemize}
 			\item The scanner is generated using \texttt{Flex}
		\end{itemize}
 \item String of tokens processed by the \emph{parser}
		\begin{itemize}
 			\item \texttt{ACSE} parser: LALR(1), generated with \texttt{Bison}
		\end{itemize}
 \item Syntax Directed Translation 
		\begin{itemize}
 			\item Check the tokenized input for syntactic correctness 
			\item Execute semantic actions for each recognized grammar rule
		\end{itemize}
\end{itemize}
\end{slide}

\begin{slide}{Compilation process: Back-End}
\begin{itemize}
 \item Transform tokenized input to assembly statements for the target machine
		\begin{itemize}
 			\item The assembly produced uses an unlimited number of registers!
		\end{itemize}
 \item Liveness Analysis and Register Allocation steps 
		\begin{itemize}
 			\item The assembly produced now uses a limited number of registers!
		\end{itemize}
 \item The assembly code is now written out
 \item The Assembler must now be invoked to produce an object file
\end{itemize}
\end{slide}


\begin{slide}{Source Language: \texttt{LanCE}}
\begin{itemize}
 \item The Language for Compiler Education (\texttt{LanCE})
 \item Simple C-based syntax:
		\begin{itemize}
 			\item Standard set of arithmetic/logic operations
			\item Reduced control flow (while/do-while, if)
			\item A single type: integer
		\end{itemize}
\end{itemize}
\end{slide}
 

\begin{slide}{\texttt{LanCE}: Tokens}
\centering
\resizebox{!}{0.5\textheight}{
\begin{tabular}{l|l|l|l}
\hline
Token  &Regular Expression  &Token  &Regular Expression  \\
\hline
DIGIT     &[0-9]  &ID       &[a-zA-Z\_][a-zA-Z0-9\_]* \\
LBRACE    &\{     &RBRACE   &\} \\
LSQUARE   &[      &RSQUARE  &] \\
LPAR      &(      &RPAR     &) \\
SEMI      &;      &COLON    &: \\
PLUS      &+      &MINUS    &- \\
MUL\_OP   &*      &DIV\_OP  &/ \\
MOD\_OP   &\%     &AND\_OP  &\& \\
OR\_OP    &$\mid$ &NOT\_OP  &! \\
ASSIGN    &=      &LT       &< \\
GT        &>      &SHL\_OP  &$\ll$ \\
SHR\_OP   &$\gg$  &EQ       &== \\
NOTEQ     &!=     &LTEQ     &$<=$ \\
GTEQ      &$>=$   &ANDAND   &\&\& \\
OROR      &$\parallel$ &COMMA    &, \\
DO        &do     &ELSE     &else \\
FOR       &for    &IF       &if \\
TYPE      &int    &WHILE    &while \\
RETURN    &return &READ     &read \\
WRITE     &write  &         &\\
\hline
\end{tabular}
}
\end{slide}


\begin{slide}{\texttt{LanCE}: Grammar (1)}
\resizebox{!}{0.25\textheight}{
\begin{tabular}{lp{0.8\textwidth}}
\hline
\emph{\textbf{\texttt{program}}}  : &\emph{var\_declarations} \emph{statements} \\
\emph{\textbf{\texttt{var\_declarations}}}  : &\emph{var\_declarations} \emph{var\_declaration} \\
   & $\mid$ $\varepsilon$ \\
\emph{\textbf{\texttt{var\_declaration}}}  : &TYPE \emph{declaration\_list} SEMI \\
\emph{\textbf{\texttt{declaration\_list}}}  : &\emph{declaration\_list} COMMA \emph{declaration} \\
   & $\mid$ \emph{declaration} \\
\emph{\textbf{\texttt{declaration}}} : &IDENTIFIER ASSIGN NUMBER \\
\emph{\textbf{\texttt{statements}}}  : &\emph{statements statement} \\
   & $\mid$  \emph{statement} \\
\end{tabular}
}\end{slide}


\begin{slide}{\texttt{LanCE}: Grammar (2)}
\resizebox{!}{0.5\textheight}{
\begin{tabular}{lp{0.8\textwidth}}
\emph{\textbf{\texttt{statement}}} : &IDENTIFIER LSQUARE \emph{exp} RSQUARE ASSIGN \emph{exp} SEMI \\
   & $\mid$ IDENTIFIER ASSIGN \emph{exp} SEMI \\
   & $\mid$ \emph{if\_statements} \\
   & $\mid$ \emph{if\_statements} ELSE \emph{code\_block} \\
   & $\mid$ \emph{while\_statements}  \\
   & $\mid$ \emph{do\_while\_statements} SEMI \\
   & $\mid$ SEMI \\
   & $\mid$ RETURN SEMI \\
   & $\mid$ READ LPAR IDENTIFIER RPAR SEMI \\
   & $\mid$ WRITE LPAR \emph{exp} RPAR SEMI \\
\emph{\textbf{\texttt{code\_block}}}  : &\emph{statement} \\
   & $\mid$ LBRACE \emph{statements} RBRACE \\
\emph{\textbf{\texttt{while\_statements}}}  : &WHILE LPAR \emph{exp} RPAR \emph{code\_block} \\
\emph{\textbf{\texttt{do\_while\_statements}}}  : &DO \emph{code\_block} WHILE LPAR \emph{exp} RPAR \\
\emph{\textbf{\texttt{if\_statements}}}  :  &IF LPAR \emph{exp} RPAR \emph{code\_block} \\
\end{tabular}
}\end{slide}


\begin{slide}{\texttt{LanCE}: Grammar (3)}
\begin{tabular}{cc}
\resizebox{!}{0.4\textheight}{
\begin{tabular}{lp{0.8\textwidth}}
\emph{\textbf{\texttt{\emph{exp}}}} : &NUMBER  \\
   & $\mid$ IDENTIFIER \\
   & $\mid$ IDENTIFIER LSQUARE \emph{exp} RSQUARE \\
   & $\mid$ NOT\_OP NUMBER \\
   & $\mid$ NOT\_OP IDENTIFIER \\
   & $\mid$ \emph{exp} AND\_OP \emph{exp} \\
   & $\mid$ \emph{exp} OR\_OP \emph{exp} \\
   & $\mid$ \emph{exp} PLUS \emph{exp} \\
   & $\mid$ \emph{exp} MINUS \emph{exp} \\
   & $\mid$ \emph{exp} MUL\_OP \emph{exp} \\
   & $\mid$ \emph{exp} DIV\_OP \emph{exp}  \\
   & $\mid$ \emph{exp} LT \emph{exp}  \\
   & $\mid$ \emph{exp} GT \emph{exp}  \\
\end{tabular}
} & \resizebox{!}{0.4\textheight}{
\begin{tabular}{lp{0.8\textwidth}}
   & $\mid$ \emph{exp} GT \emph{exp}  \\
   & $\mid$ \emph{exp} EQ \emph{exp}  \\
   & $\mid$ \emph{exp} NOTEQ \emph{exp}  \\
   & $\mid$ \emph{exp} LTEQ \emph{exp}  \\
   & $\mid$ \emph{exp} GTEQ \emph{exp}  \\
   & $\mid$ \emph{exp} SHL\_OP \emph{exp}  \\
   & $\mid$ \emph{exp} SHR\_OP \emph{exp}  \\
   & $\mid$ \emph{exp} ANDAND \emph{exp}  \\
   & $\mid$ \emph{exp} OROR \emph{exp}  \\
   & $\mid$ LPAR \emph{exp} RPAR \\
   & $\mid$ MINUS \emph{exp} \\
\end{tabular}
}
\end{tabular}
\end{slide}


\begin{slide}{\texttt{LanCE}: Example}
\begin{scriptsize}
\begin{verbatim}
int value, fact;      /* variables declarations */
read(value);          /* read from standard input the
                       * value of 'value' */
if (value < 0) {      /* invalid input */
   write(-1);        
   return;           
}
fact = 1;             /* initialize 'fact' */
while(value > 0) {    /* compute the factorial of value */
   fact = value * fact;
   value = value - 1;
}
write(fact);          /* write the result to stdout */
\end{verbatim}
\end{scriptsize}
\end{slide}


\begin{slide}{ACSE SDT: Overview}
\begin{itemize}
  \item Goal: gather all useful information about each statement of the source program
	\item Each instruction is typically translated into one or more assembly instructions
	\item Global compiler environment	\texttt{t\_program\_infos}:
   \begin{itemize}
		\item Used to store the collected information, including the assembly instructions
		\item Defined in \texttt{axe\_engine.h}.
   \end{itemize}
	\item Data structures for instructions and directives are defined in \texttt{axe\_struct.h}
\end{itemize}
\end{slide}


\begin{slide}{ACSE SDT: Program Information}
\texttt{t\_program\_infos} contains:
\begin{itemize}
   \item An instance of a symbol table
   \item An instance of label manager
   \item A list of program variables
   \item A list of instruction and assembler directives.
\end{itemize}

\end{slide}


\begin{slide}{Assembly instructions}
\begin{scriptsize}
An assembly instruction is described by:
\begin{itemize}
   \item An operation identifier (for example: `SUB');
   \item A set of instruction parameters which depend on the instruction type;
   \item A user comment (optional);
   \item A label identifier (optional).
\end{itemize}

Valid instruction parameters are:
\begin{itemize}
      \item Register identifiers;
      \item Immediate values (signed integer values);
      \item Addresses (for example: label identifiers).
\end{itemize}
\end{scriptsize}
\end{slide}


\begin{slide}{Assembly instructions: Example (1)}
\begin{scriptsize}
\begin{itemize}
  \item Assembly ADD instruction: ternary instruction \texttt{ADD R3 R1 R2}
		\begin{itemize}
      \item Sum \texttt{`R1'} and \texttt{`R2'}
			\item Stores the result in \texttt{`R3'}
			\item The keyword ``\texttt{ADD}'' identifies the operation
		\end{itemize}
	\item Number of parameters depends on instruction type
		\begin{itemize}
      \item Ternary instructions accept only registers identifiers
			\item Register identifier: alias for a machine general-purpose register
		\end{itemize}
	\item Additional information:
		\begin{itemize}
      \item User comments: for debugging purposes
			\item Labels: \texttt{L1: ADD R3 R1 R2}
		\end{itemize}
\end{itemize}
\end{scriptsize}
\end{slide}


\begin{slide}{Assembly directives}
A Assembler directive is defined as follows:
\begin{itemize}
   \item Directive type identifier (e.g.: \texttt{.WORD})
   \item Value associated with the directive
   \item Optional label identifier
\end{itemize}

Example of assembler directive:
\texttt{.WORD 0}
\end{slide}


\begin{slide}{Axe Variables}
\begin{itemize}
  \item ``Variable'' item: \texttt{t\_axe\_variable}
	\item Symbol Table: within \texttt{t\_program\_infos}
\end{itemize}

\texttt{t\_axe\_variable} defines:
\begin{itemize}   
   \item A data type (for example: INTEGER );
   \item An Array Size (defined only if the variable is an array);
   \item An initial value;
   \item A variable identifier;
\end{itemize}
\end{slide}


\begin{slide}{Axe Variable example}
\begin{scriptsize}
\begin{itemize}
	\item Source program: variable ``\texttt{var}'' as integer with initial value `100'
		\begin{itemize}
			\item \texttt{int var = 100;}
		\end{itemize}
	\item \texttt{t\_axe\_variable} for ``var'':
		\begin{itemize}	
		 	\item \texttt{INTEGER} as data type;
		 	\item \texttt{100} as initial value;
		 	\item the string ``\texttt{var}'' as variable identifier.
		\end{itemize}
	\item \texttt{t\_axe\_variable} instance used to produce \texttt{.WORD} and/or \texttt{.SPACE} assembler directives
		\begin{itemize}	
		 	\item The \texttt{t\_axe\_variable} defined for ``var'' produces: \texttt{.WORD 100} 
		\end{itemize}
\end{itemize}
\end{scriptsize}
\end{slide}


\begin{slide}{Symbol Table}
\begin{itemize}
 \item \emph{Symbol table}: a data structure used at translation time to keep track of source program variables.
 \item The Symbol Table contains, for each variable
  \begin{itemize}
   \item ID: variable identifier
   \item Type: data type of the variable 
   \item A Register identifier.
  \end{itemize}
 \item The register identifier refers to the register location where the variable is currently stored.
\end{itemize}
\end{slide}


\begin{slide}{Symbol Table: Example}
\centering
\resizebox{!}{0.5\textheight}{
\begin{tabular}{|l|ll|l|}
\hline
Source Program & &Assembly &Comments \\
\hline
int value, fact;        &    &.DATA      & /* variables declarations */ \\
                        &L0: \ \ &.WORD 0    & /* initialize 4 bytes of data to 0 */\\
                        &L1: \ \ &.WORD 0    & /* initialize 4 bytes of data to 0 */\\
read(value);            &    &.TEXT      & /* start of a block of code */ \\
                        &    &READ R1 0  & /* read from standard input */ \\
if (value $<$ 0) \{     &    &SUBI R3 R1 \#0 & /* sub immediate */ \\
                        &    &SLT R3 0   & /* set R3 on less than zero */ \\
                        &    &BEQ L2     & /* `branch on equal' to label L2 */ \\
write(-1);              &    &ADDI R4 R0 \#-1 & /* add immediate */ \\
                        &    &WRITE R4 0 & /* write R4 to standard output */ \\
return; \}              &    &HALT       & /* stop the program execution */ \\
fact = 1;               &L2: \ \ &ADDI R2 R0 \#1 & \\
while(value $>$ 0) \{   &L3: \ \ &SUBI R5 R1 \#0 & \\
                        &    &SGT R5 0  & /* set R3 on `less than zero' */ \\
                        &    &BEQ L4  & /* `branch on equal' to label L4 */ \\
fact = value * fact;    &    &MUL R6 R1 R2 & /* binary mult. operation */\\
                        &    &ADDI R2 R6 \#0 &\\
value = value - 1; \}   &    &SUBI R7 R1 \#1 &\\
                        &    &ADDI R1 R7 \#0 &\\
                        &    &BT L3  & /* `branch true' to label `L3' */ \\
write(fact);            &L4: \ \ &WRITE R2 0 &/* write R2 to standard output */ \\
                        &    &HALT & /* stop the program execution */ \\
\hline
\end{tabular}
}
\end{slide}

\begin{slide}{Symbol Table: Example}
Content of the Symbol Table for the example:
\begin{scriptsize}
\begin{tabular}{|l|l|l|}
\hline
Variable Identifier &Type &Register Location \\
\hline
value      &INTEGER       &R1 \\
fact       &INTEGER       &R2 \\
\hline
\end{tabular}
\end{scriptsize}
\end{slide}


\begin{slide}{AXE API: Symbol Table}
\begin{itemize}
 \item \textbf{\texttt{symbol\_table.h}}: functions to manipulate the symbol table
  \begin{itemize}
   \item look up a symbol
   \item define and insert a new symbol 
   \item set the register location information of a symbol
   \item retrieve the register location associated with a symbol
  \end{itemize}
 \item \textbf{\texttt{axe\_struct.h}}: data structures used by the parser (defined in \texttt{Acse.y})
\end{itemize}
\end{slide}


\begin{slide}{AXE API: Label Manager}
\begin{itemize}
 \item Label Manager (\textbf{\texttt{axe\_labels.h}}): functions to work with labels
	\begin{itemize}
   \item \texttt{reserveLabelID}: user code requires the creation of a new label
   \item \texttt{fixLabelID}: assign a given label to an instruction
	\end{itemize}
\end{itemize}
\end{slide}


\begin{slide}{AXE API: Code Generation}
\begin{scriptsize}
\begin{itemize}
	\item \texttt{axe\_gencode.h}: functions to generate assembly instructions
	\item E.g.: \texttt{gen\_add\_instruction} is used to create an ADD
 	\item \texttt{axe\_array.h}: functions to generate load/store instructions from/to array elements
	\item E.g.: \texttt{loadArrayElement} that takes as input:
		\begin{itemize}
  	  \item Input: an array variable identifier
 		  \item Input: an array subscript identifying an array element.
			\item Output: a register location identifier that holds the value of the specified array element
		\end{itemize}
\end{itemize}
\end{scriptsize}
\end{slide}


\begin{slide}{AXE API: Miscellanea}
\begin{scriptsize}
\texttt{axe\_engine.h} defines the \texttt{t\_program\_infos} data structure, plus functions to
\begin{itemize}
   \item initialize an instance of \texttt{t\_program\_infos}
   \item add an assembly instruction to a \texttt{t\_program\_infos}
   \item create a variable and assign it to a \texttt{t\_program\_infos}
   \item request for a free register location (\texttt{getNewRegister})
   \item write an assembly file as output (\texttt{writeAssembly})
\end{itemize}
\end{scriptsize}
\end{slide}


\begin{slide}{AXE API: Expressions}
\begin{scriptsize}
\begin{itemize}
	\item \texttt{axe\_structs.h}: expression type \texttt{t\_axe\_expression}, with the following fields:
		\begin{itemize}
  		 \item A value: a register identifier or an immediate value
   		\item An expression type: either ``register'' or ``immediate value''
	 	\end{itemize}
	\item Functions used to generate instructions for expressions:
		\begin{itemize}
   		\item \texttt{perform\_binary\_comparison}: comparison expressions
   		\item \texttt{perform\_bin\_numeric\_op}: arithmetic expressions
	 		\item Both take two expressions as input and return as expression
		\end{itemize}
	\item \texttt{axe\_utils.h} 
		\begin{itemize}
   		\item \texttt{get\_symbol\_location}: wrapper for the functions in \texttt{symbol\_table.h}
			\item Look up the register for a variable, if not present get a new one
		\end{itemize}
\end{itemize}
\end{scriptsize}
\end{slide}


\begin{slide}{Semantic Actions Examples}
Three examples of bison semantic actions:
\begin{itemize}
	\item Arithmetic expression
	\item Comparison expression
	\item \texttt{do-while} statement
\end{itemize}
\end{slide}

\begin{slide}{Arithmetics Expression (1)}
\begin{itemize}
	\item LanCE Grammar rule:
\begin{tabular}{lp{0.6\textwidth}}
\emph{\textbf{\texttt{\emph{exp}}}} : &\\
   & $\mid$ \emph{exp} AND\_OP \emph{exp} \\
\end{tabular}
	\item Source code: \texttt{``a \& b''}
	\item Semantic action: \texttt{\$\$ = perform\_bin\_numeric\_op (program, \$1, \$3, ANDB);}
\end{itemize}
\end{slide}

\begin{slide}{Arithmetics Expression (2)}
\begin{itemize}
	\item Query the symbol table to retrieve the register locations associated with both \texttt{a} and \texttt{b}.
		\begin{itemize}
			\item \texttt{get\_symbol\_location} 
		\end{itemize}
	\item Generate an assembly \texttt{ANDB} instruction
		\begin{itemize}
			\item \texttt{gen\_andb\_instruction}
		\end{itemize}
	\item \texttt{perform\_bin\_numeric\_op} can be used to perform all these operations
\end{itemize}
\end{slide}

\begin{slide}{Arithmetics Expression (3)}
\begin{scriptsize}
\texttt{gen\_andb\_instruction} requires five parameters:
\begin{itemize}
   \item Pointer to \texttt{t\_program\_infos};
   \item A destination register identifier;
   \item Two register identifiers as parameters for the \texttt{ANDB} instruction;
   \item The addressing mode:
   \begin{itemize}
      \item \texttt{CG\_DIRECT\_ALL}
      \item \texttt{CG\_INDIRECT\_ALL}
      \item \texttt{CG\_INDIRECT\_DEST}
      \item \texttt{CG\_INDIRECT\_SOURCE}
   \end{itemize}
\end{itemize}
\end{scriptsize}
\end{slide}

\begin{slide}{Arithmetics Expression (3)}
\begin{scriptsize}
\begin{itemize}
	\item \texttt{perform\_bin\_numeric\_op} takes the following parameters:
		\begin{itemize}
	  	 \item Pointer to \texttt{t\_program\_infos}
	  	 \item Two \texttt{t\_axe\_expression}, one for each operand
	  	 \item An operation identifier (e.g.: \texttt{ANDB})
		\end{itemize}
	\item Valid binary operation identifiers:
		\begin{itemize}
  	 \item \texttt{ADD}
  	 \item \texttt{ANDB}
  	 \item \texttt{ORB}
  	 \item \texttt{SUB}
  	 \item \texttt{MUL}
  	 \item \texttt{DIV}
	\end{itemize}
\end{itemize}
\end{scriptsize}
\end{slide}


\begin{slide}{Comparison Expression (1)}
\begin{itemize}
	\item LanCE Grammar rule:
\begin{tabular}{lp{0.6\textwidth}}
\emph{\textbf{\texttt{\emph{exp}}}} : &\\
   & $\mid$ \emph{exp} LT \emph{exp}  \\
\end{tabular}
	\item Source code: \texttt{``a $<$ b''}
	\item Semantic action: \texttt{\$\$ = perform\_binary\_comparison (program, \$1, \$3, \_LT\_);}
\end{itemize}
\end{slide}


\begin{slide}{Comparison Expression (2)}
\begin{scriptsize}
\begin{itemize}
 \item \texttt{perform\_binary\_comparison} is used to implement the semantic action
 \item \texttt{perform\_binary\_comparison} takes the following parameters:
	\begin{itemize}
		 \item Pointer to \texttt{t\_program\_infos}
	   \item Two \texttt{t\_axe\_expression}, one for each operand
	   \item A condition code (e.g.: \texttt{\_LT\_})
	\end{itemize}
 \item Valid condition codes:
  \begin{itemize}
   \item \texttt{\_LT\_} 
   \item \texttt{\_GT\_} 
   \item \texttt{\_EQ\_} 
   \item \texttt{\_NOTEQ\_} 
   \item \texttt{\_LTEQ\_} 
   \item \texttt{\_GTEQ\_} 
  \end{itemize}
\end{itemize}
\end{scriptsize}
\end{slide}


\begin{slide}{Do-while Statement (1)}
\begin{scriptsize}
\begin{verbatim}
do_while_statements  : DO
          {
            $1 = reserveLabel(program);
            fixLabel(program, $1);
          }
          code_block WHILE LPAR exp RPAR
          {
            gen_bne_instruction (program, $1, 0);
          };                    
\end{verbatim}
\end{scriptsize}
\end{slide}


\begin{slide}{Do-while Statement (1)}
\begin{itemize}
\item Assign a label to the loop body first instruction via \texttt{reserveLabel} and \texttt{fixLabel}
\item Check if \texttt{exp} is different from zero
	\begin{itemize}
		\item If so, the control jumps back to the first instruction of the loop body (\texttt{code\_block})
		\item Otherwise the control get out from the loop.
	\end{itemize}
\item Use \texttt{gen\_bne\_instruction} to generate a conditional branch instruction
\end{itemize}
\end{slide}
