% !TEX encoding = UTF-8
% !TEX spellcheck = en_GB

\documentclass[aspectratio=169,compress]{beamer}

% Basic packages
\usepackage{pgfpages}
\usepackage{listings}
\usepackage[utf8]{inputenc}
\usepackage[spanish,english]{babel}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{soul}

% Bibliography settings
\usepackage[backend=bibtex,style=alphabetic,citestyle=alphabetic-verb]{biblatex}
\bibliography{biblio.bib}

% Graphics to create step-by-step graphs
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

% Beamer theme
% \usetheme{beamertheme-bjeldbak/bjeldbak}
\setbeamertemplate{note page}[plain]
% \setbeameroption{show notes on second screen=right}
\usetheme{-bjeldbak/beamerthemebjeldbak}
% \usetheme{Madrid}
% \usecolortheme{whale}

% Document metadata
% \title[Program slicing with exceptions]{Fragmentación de programas con excepciones}
\title{Program slicing with exception handling}
\date{Dec 18th, 2019}
\author[C. Galindo]{Carlos S. Galindo Jiménez}
\institute[UPV]{
    Universidad Politécnica de Valencia
}

% Escapable listing environment
\lstnewenvironment{java}[1][]
{\lstset{
    language=[LaTeX]TeX}\lstset{escapeinside={(*@}{@*)},
    numbers=left,
    numberstyle=\tiny,
    stepnumber=1,
    numbersep=5pt,
    breaklines=true,
    %firstnumber=last,
    %frame=tblr,
    tabsize=2,
    framesep=5pt,
    basicstyle=\small\ttfamily,
    showstringspaces=false,
    %identifierstyle=\ttfamily,
    stringstyle=\color{maroon},
    commentstyle=\color{black},
    rulecolor=\color{black},
    xleftmargin=0pt,
    xrightmargin=0pt,
    aboveskip=\medskipamount,
    belowskip=\medskipamount,
    backgroundcolor=\color{white}, #1
}}{}

\begin{document}
\input{listings-config}

\maketitle

\begin{frame}
    \frametitle{TODO}

    \begin{itemize}
        \item Proposal no, solution o proposed solution.
        \item \ul{test}
    \end{itemize}
\end{frame}

\begin{frame}
    \frametitle{Table of Contents}
    \tableofcontents
\end{frame}

\section{Introduction}

\subsection{Program slicing}

\begin{frame}[fragile]{Program slicing}

    \begin{columns}
        \column{0.5\textwidth}
            \begin{java}
void f(int x) {
    int sum = 0;
    int prod = 0;
    while (x > 0) {
        sum += x;
        prod *= x;
        x--;
    }
    log("sum: " + sum);
    log("prod: " + (*@\textcolor<2->{blue}{prod}@*));
}
            \end{java}
        \column<3->{0.5\textwidth}
            \begin{java}
void f(int x) {
    (*@\invisible<4->{int sum = 0;}@*)
    int prod = 0;
    while (x > 0) {
        (*@\invisible<4->{sum += x;}@*)
        prod *= x;
        x--;
    }
    (*@\invisible<4->{log("sum: " + sum);}@*)
    log("prod: " + prod);
}
            \end{java}
    \end{columns}
    {\hfill Example adapted from \cite{Tip95}.}
\end{frame}

% TODO: posible addition of examples on the right side.
\begin{frame}{Program slicing}{Applications}
    \begin{itemize}
        \item Debugging
        \item Program specialization
        \item Software maintenance
        \item Code obfuscation
        \item Dead code removal
        \item Program parallelization
    \end{itemize}
\end{frame}

% TODO: remove one of the notations in the examples.
% TODO: maybe use example 'block'?
\begin{frame}{Program slicing}{Metrics}
    \begin{description}
        \item[\textbf{Completeness.}] The slice includes all instructions that are necessary.
        \item[\textbf{Correctness.}] The slice excludes all instructions that are not necessary.
    \end{description}

    \vfill
    \definecolor{minimalcolor}{rgb}{0.8588235294117647,0.3803921568627451,0.0784313725490196}
    \definecolor{programcolor}{rgb}{0.08235294117647059,0.396078431372549,0.7529411764705882}
    \definecolor{slicecolor}{rgb}{0,0,0}

    \pause
    \begin{columns}
        \column{0.25\textwidth}
            Correct and complete: \\
            \begin{tikzpicture}[line cap=round,line join=round,>=triangle 45]
                \draw [line width=2pt,color=programcolor,fill=programcolor,fill opacity=0.25] (0,0) circle (4em);
                \draw [line width=2pt,color=minimalcolor,fill=minimalcolor,fill opacity=0.4] (0,0) circle (2em);
                \visible<3->{\draw [line width=1pt,dotted,color=slicecolor,fill=slicecolor,fill opacity=0.2] (0, 0) circle (2em);}
            \end{tikzpicture}
            $$c_m \wedge c_r$$
        % \includegraphics[width=\textwidth]{}
        \column{0.25\textwidth}
            Correct and incomplete: \\
            \begin{tikzpicture}[line cap=round,line join=round,>=triangle 45]
                \draw [line width=2pt,color=programcolor,fill=programcolor,fill opacity=0.25] (0,0) circle (4em);
                \draw [line width=2pt,color=minimalcolor,fill=minimalcolor,fill opacity=0.4] (0,0) circle (2em);
                \visible<4->{\draw [line width=1pt,dotted,color=slicecolor,fill=slicecolor,fill opacity=0.2] (0, 0) circle (1.5em);}
            \end{tikzpicture}
            $$\neg c_m \wedge c_r$$
        % \includegraphics[width=\textwidth]{}
        \column{0.25\textwidth}
            Incorrect and complete: \\
            \begin{tikzpicture}[line cap=round,line join=round,>=triangle 45]
                \draw [line width=2pt,color=programcolor,fill=programcolor,fill opacity=0.25] (0,0) circle (4em);
                \draw [line width=2pt,color=minimalcolor,fill=minimalcolor,fill opacity=0.4] (0,0) circle (2em);
                \visible<5->{\draw [line width=1pt,dotted,color=slicecolor,fill=slicecolor,fill opacity=0.2] (0, 0) circle (2.5em);}
            \end{tikzpicture}
            $$c_m \wedge \neg c_r$$
        % \includegraphics[width=\textwidth]{}
        \column{0.25\textwidth}
            Incorrect and incomplete: \\
            \begin{tikzpicture}[line cap=round,line join=round,>=triangle 45]
                \draw [line width=2pt,color=programcolor,fill=programcolor,fill opacity=0.25] (0,0) circle (4em);
                \draw [line width=2pt,color=minimalcolor,fill=minimalcolor,fill opacity=0.4] (0,0) circle (2em);
                \visible<6->{\draw [line width=1pt,dotted,color=slicecolor,fill=slicecolor,fill opacity=0.2] (1em, 1em) circle (2em);}
            \end{tikzpicture}
            $$\neg c_m \wedge \neg c_r$$
        % \includegraphics[width=\textwidth]{}
    \end{columns}
\end{frame}

\subsection{Exception handling}

\begin{frame}[fragile]{Exception handling systems}{Java}
    \begin{columns}
        \column{0.5\textwidth}
        \begin{java}[tabsize=2]
void main() {
  (*@\textcolor<4->{blue}{try}@*) {
    f();
  } (*@\textcolor<5->{blue}{catch}@*) ((*@\textcolor<6>{blue}{Exception e}@*)) {
    log("caught exception");
  } (*@\textcolor<5->{blue}{catch}@*) ((*@\textcolor<6>{blue}{Throwable e}@*)) {
    log("caught throwable");
  }
}
        \end{java}
        \column{0.5\textwidth}
        \begin{java}[firstnumber=10,tabsize=2]
void f() throws Exception {
  switch(x) {
  case A:
    log("no error");
    break;
  case B:
    (*@\textcolor<2-3>{blue}{throw}@*) (*@\textcolor<3>{blue}{new Exception()}@*);
  default:
    (*@\textcolor<2-3>{blue}{throw}@*) (*@\textcolor<3>{blue}{new Throwable()}@*);
  }
}
        \end{java}
    \end{columns}
\end{frame}

% TODO: extend with examples or replace by Exception hierarchy
\begin{frame}{Exception handling systems}{Java}
    \begin{columns}
        \column{0.5\textwidth}
        \begin{center}
            \includegraphics[height=0.85\textheight]{figs/exception-hierarchy}
        \end{center}
        \column{0.5\textwidth}
        \begin{itemize}
            \item \textit{Checked} (solid): must be caught (\texttt{try-catch}) or declared (\texttt{f() throws T}).
            \item \textit{Unchecked} (dashed).
        \end{itemize}
    \end{columns}
\end{frame}

\begin{frame}{Exception handling systems}{Other programming languages}
    \begin{table}
        \begin{minipage}{0.6\linewidth}
        \centering
        \begin{tabular}{r | r }
            \textbf{Language}     & $\%$ usage \\ \hline
            JavaScript            & 69.7 \\ \hline
            \textcolor<2->{blue}{HTML/CSS}              & 63.1 \\ \hline
            \textcolor<2->{blue}{SQL}                   & 56.5 \\ \hline
            Python                & 39.4 \\ \hline
            Java                  & 39.2 \\ \hline
            \textcolor<2->{blue}{Bash/Shell}/PowerShell & 37.9 \\ \hline
            C\#                   & 31.9 \\ \hline
            PHP                   & 25.8 \\ \hline
            TypeScript            & 23.5 \\ \hline
            C++                   & 20.4 \\ \hline
        \end{tabular}
        \end{minipage}
        \begin{minipage}{0.39\linewidth}
        \begin{tabular}{r | r }
            \textbf{Language}     & $\%$ usage \\ \hline
            \textcolor<2->{blue}{C}                    & 17.3 \\ \hline
            Ruby                  &  8.9 \\ \hline
            \textcolor<2->{blue}{Go}                  &  8.8 \\ \hline
            Swift                 &  6.8 \\ \hline
            Kotlin                &  6.6 \\ \hline
            R                     &  5.6 \\ \hline
            \textcolor<2->{blue}{VBA}                  &  5.5 \\ \hline
            Objective-C           &  5.2 \\ \hline
            \textcolor<2->{blue}{Assembly}              &  5.0 \\ \hline
        \end{tabular}
        \end{minipage}
        % The caption has a weird structure due to the fact that there's a footnote
        % inside of it.
        \caption[Commonly used programming languages]{Programming language by usage in the software industry (StackOverflow's 2019 Developer Survey)}
        \label{tab:popular-languages}
    \end{table}
\end{frame}

\subsection{The System Dependence Graph}

\begin{frame}[fragile]{The System Dependence Graph}{Creation}
%     \item Código a un lado
%     \item Aparecen nodos que corresponden a statements
%     \item Aparecen arcos (control y data o por pasos).
%     \item Añadir in-out
%     \item No añadir la llamada
\begin{columns}
    \column{0.20\textwidth}
    \begin{java}[basicstyle=\scriptsize\ttfamily,numberstyle=\tiny]
void f(int x) {
  int sum = 0;
  int prod = 0;
  while (x>0) {
    sum += sum;
    prod*=prod;
    x--;
  }
  log(sum);
  log(prod);
}
    \end{java}
    \column{0.8\textwidth}
    \centering
    \includegraphics<1>[width=1.1\linewidth]{figs/example-sdg-1}
    \includegraphics<2>[width=1.1\linewidth]{figs/example-sdg-2}
    \includegraphics<3>[width=1.1\linewidth]{figs/example-sdg-3}
    \includegraphics<4>[width=1.1\linewidth]{figs/example-sdg-4}
    \includegraphics<5>[width=1.1\linewidth]{figs/example-sdg-5}
    \includegraphics<6>[width=1.1\linewidth]{figs/example-sdg-6}
\end{columns}
\end{frame}

\begin{frame}[fragile]{The System Dependence Graph}{Traversal}
    \begin{columns}
        \column{0.20\textwidth}
        \definecolor{light-gray}{gray}{0.8}
        \begin{java}[basicstyle=\scriptsize\ttfamily\color<2->{light-gray},numberstyle=\tiny\color{black}]
(*@\textcolor<4->{black}{void f(}@*)(*@\textcolor<8->{black}{int x}@*)(*@\textcolor<4->{black}{) \{}@*)
  int sum = 0;
  (*@\textcolor<4->{black}{int prod = 0;}@*)
  (*@\textcolor<6->{black}{while (x>0) \{}@*)
    sum += sum;
    (*@\textcolor<4->{black}{prod*=prod;}@*)
    (*@\textcolor<8->{black}{x--;}@*)
  (*@\textcolor<6->{black}{\}}@*)
  log(sum);
  (*@\textcolor<2->{black}{log(prod);}@*)
(*@\textcolor<4->{black}{\}}@*)
        \end{java}
%   (*@\only<-11>{int sum = 0;}@*)
%   (*@\textcolor<4-10>{blue}{int prod = 0;}@*)
%   (*@\textcolor<6-10>{blue}{while (x>0) \{}@*)
%     (*@\only<-11>sum += sum;}@*)
%     (*@\textcolor<4-10>{blue}{prod*=prod;}@*)
%     (*@\textcolor<8-10>{blue}{x--;}@*)
%   (*@\textcolor<6-10>{blue}{\}}@*)
%   (*@\only<-11>{log(sum);}@*)
%   (*@\textcolor<2-10>{blue}{log(prod);}@*)
% }
%         \end{java}
        \column{0.8\textwidth}
        \centering
        \includegraphics<1>[width=1.1\linewidth]{figs/example-sdg} % original graph
        \includegraphics<2>[width=1.1\linewidth]{figs/example-slice-1} % SC marked
        \includegraphics<3>[width=1.1\linewidth]{figs/example-slice-2} % edges
        \includegraphics<4>[width=1.1\linewidth]{figs/example-slice-3} % add 1, 3, 6
        \includegraphics<5>[width=1.1\linewidth]{figs/example-slice-4} % edges
        \includegraphics<6>[width=1.1\linewidth]{figs/example-slice-5} % add 4
        \includegraphics<7>[width=1.1\linewidth]{figs/example-slice-6} % edges
        \includegraphics<8>[width=1.1\linewidth]{figs/example-slice-7} % add 7, x
        \includegraphics<9>[width=1.1\linewidth]{figs/example-slice-8} % edges
        \includegraphics<10->[width=1.1\linewidth]{figs/example-slice} % final slice
    \end{columns}
\end{frame}

\section{Problems and proposals}
\subsection{Problem 1: incorrect slices with nested unconditional jumps}
% TODO añadir nombres a los problemas

\begin{frame}[fragile]{Problem 1}{The subsumption correctness error}
    \begin{minipage}{0.39\linewidth}
    \begin{lstlisting}
public void f() {
    while (X) {
        if (Y) {
            if (Z) {
                A;
                break;
            }
            B;
            break;
        }
        C;
    }
    D;
}
    \end{lstlisting}
    \end{minipage}
    \begin{minipage}{0.59\linewidth}
        \includegraphics[width=0.8\linewidth]{figs/problem1}
    \end{minipage}
\end{frame}

\begin{frame}{Problem 1: proposed solution}{The subsumption correctness error}
    TODO: show graph here, label the edges caused by each jump and then remove some.
\end{frame}

\subsection{Problem 2: incorrect weak slices}

\begin{frame}[fragile]{Problem 2}{Unnecessary instructions in weak slicing}
    \begin{minipage}{0.33\linewidth}
		\begin{lstlisting}
void g() {
	int x = 0;
	while (x > 0) {
		if (x > 10)
			break;
		x++;
	}
	log(x);
}
		\end{lstlisting}
    \end{minipage}
    \begin{minipage}{0.66\linewidth}
        \centering
        \includegraphics[width=0.6\linewidth]{figs/problem2}
    \end{minipage}
\end{frame}

\begin{frame}{Problem 2: proposed solution}{Unnecessary instructions in weak slicing}
    TODO: animate the multi-pass solution.
    \begin{enumerate}
        \item Remove all forward-jumping unconditional jump's nodes.
        \item Traverse the graph.
        \item Re-add all nodes that fulfil the condition of having something selected after them.
        \item Goto 2.
    \end{enumerate}
\end{frame}

\subsection{Problem 3: incomplete \texttt{catch} treatment}

\begin{frame}[fragile]{Problem 3}{The lack of dependencies of \texttt{catch} statements}
\begin{minipage}{0.29\linewidth}
    \begin{lstlisting}
void main() throws Exception {
    try {
        f();
    } catch (Exception e) {
        log("error");
    }
    x = 0;
    f();
}
    \end{lstlisting}
\end{minipage}
\begin{minipage}{0.7\linewidth}
    \includegraphics[width=\linewidth]{figs/problem3}
\end{minipage}
\end{frame}

\begin{frame}{Problem 3: proposed solution}{The lack of dependencies of \texttt{catch} statements}
    TODO: animate the conversion to the new dependencies and a slice.
    \begin{enumerate}
        \item Organize \texttt{catch} statements in a tree.
        \item Add a path to the exit on the root.
        \item Maybe reference parallelism with the treatment of unconditional statements.
    \end{enumerate}
\end{frame}

\section{Conclusions}

\begin{frame}{Conclusions}
    \begin{itemize}
        \item Program slicing: a powerful technique, not yet complete for commonly used programming languages.
        \item Any proposal exchanges efficiency for completeness/correctness.
        \item Complete is required, correct is nice.
        \item Unconditional jumps are complete: let's correct them.
        \item Results
        \begin{itemize}
            \item 3 problems and proposed solutions.
            \item NOMBRE PAPER, submitted to BLA BLA.
            \item OTHER WORK, to be submitted to BLA BLA.
        \end{itemize}
    \end{itemize}
\end{frame}

\begin{frame}{Future work}
    \begin{itemize}
        \item Improve correctness of \texttt{try-catch}.
        \item Implement into existing software tools, benchmark against state of the art.
        \item Adapt to other variants of program slicing.
        \item Redefine control dependence: Danicic TODO (nº cita) tried, \textit{execution dependence} vs. \textit{presence dependence}.
    \end{itemize}
\end{frame}

\begin{frame}{The End}
\end{frame}


\appendix
% \section*{Extra slides}

\begin{frame}[noframenumbering]{Related work}
    Change to state of the art and move to introduction
    \begin{itemize}
        \item SinH98 and SinH99 introduce the field slicing + exceptions
        \item AllH03 improves the existing techniques
        \item HorwitzRB88 introduces the current handling of unconditional jumps
        \item JoC04 presents an alternative way to compute the CFG, but does not demonstrate the effects for program slicing.
        \item JiaZSJ06 propose a solution in C++, ... TODO
        \item PraMB11 also work in C++, no notable improvement TODO
        \item JieS11 introduces OO + Exception SDG, but treats exceptions similar to Horwitz, leaving the original problems unresolved.
    \end{itemize}
\end{frame}

\begin{frame}[noframenumbering]{Program slicing}{Strong and weak slices}
    Slide with definitions + examples (code and table w/ sequences).
\end{frame}

\begin{frame}
    \frametitle{Bibliography}
    \printbibliography
\end{frame}

\end{document}