\chapter{C Frontend}

\section{Overview}


\section{Scanning and Parsing}

\begin{figure}[h]
\centering
\includegraphics[width=1.0\textwidth]{ctool}
\caption{From C-file to IRep}
\label{fig:cToIrep}
\end{figure}

The sources of the C frontend are located in the "ansi-c/" directory. It currently 
uses the CTool library for scanning and parsing the files.
CTool produces a tree representation of the input program. The class
\textit{convert\_ct} declared in "ansi-c/convert-c.h" is used for 
translating CTool IR into IRep. IRep is the language-independant tree
representation used in the the frontend.

The following code illustrates how to use the frontend for parsing files and
for translating them into IRep. A call to \textit{parse} generates the CTool
tree of the program. The conversion to IRep is done during typechecking,
which is done by an object of the class \textit{convert\_ct}.  The IRep
trees for the type and initial values are constructed for every symbol and
are stored within the {\it symbol\_t} data structure.

\begin{lgrind}
\input{parse.cpp.tex}
\end{lgrind}

\section{IRep}

The implementation of the IRep data structure is based on the class
\textit{irept}. Its declaration and definiton
can be found in files "util/irep.h" and "util/irep.cpp".
The code below shows some details of class \textit{irept}:

\begin{lgrind}
\input{irep.cpp.tex}
\end{lgrind}

A tree node is an object of class \textit{irept}. Each node has a pointer
to an object of class \textit{dt}. The \textit{dt} objects are used for
storing the actual content of nodes.

Objects of class \textit{dt}  are dynamically allocated and can 
be shared between nodes. A reference-counter  mechansim is 
implemented to  automatically  free unreachable \textit{dt} objects.

The field \textit{data} of class \textit{dt} is a string representing the 
label of the nodes. The fields \textit{named\_sub}, \textit{comments}
and \textit{sub} are links to childs. Edges are either labeled
with a string or ordered. The string-labeled edges are stored in the
map \textit{comments} if their first
character  is '\#'. Otherwise, they are stored in the map \textit{named\_sub}.
The labels of edges are unique for a given node.
The field \textit{sub} is a vector of nodes that is used for storing the ordered
childs. The order of the edges is preserved during copy.

\begin{figure}[h]
\centering
\includegraphics[width=0.7\textwidth]{ireptree}
\caption{Tree for expression \textit{a+b} with \textit{int a; char b;}.}
\label{fig:ireptree}
\end{figure}

\subsection{Public interface of class \textit{irept}}

\subsubsection{is\_nil and is\_not\_nil}
\begin{verbatim}
virtual bool is_nil() const;
virtual bool is_not_nil() const;
\end{verbatim}

The first  method returns true if the label of the node is equal to  "nil".
The second method returns false if the label of the node is equal to "nil".

\subsubsection{id}
\begin{verbatim}
const irep_data_string &id();
void id(const irep_data_string &_data);
\end{verbatim}
The first method returns a constant reference to the label of the node.
The second method sets the label of the node.

\subsubsection{find, add and get}
\begin{verbatim}
const irept &find(const irep_name_string &name) const;
irept &add(const irep_name_string &name);
const irep_data_string &get(const irep_name_string &name) const;
\end{verbatim}
\begin{itemize}
\item The first method looks for an edge with label \textit{name}
and returns the corresponding child. If no edge with label \textit{name}
is found, then \textit{nil\_rep} is returned.

\item The second method does the same as the first except that if
no edge with label \textit{name} if found, then a new child is created
and returned.

\item The third method does the same as the first except that the label
of the child is returned (instead of a reference).
If no edge with label \textit{name} is found, then an empty
string is returned.
\end{itemize}
\subsubsection{set}
\begin{verbatim}
void set(const irep_name_string &name, const irep_data_string &value);
void set(const irep_name_string &name, const long value);
void set(const irep_name_string &name, const irept &irep);
\end{verbatim}

These methods create a new edge with label \textit{name}.

If the second argument is an object of class \textit{irept} then it is assigned
to the new child.

If the second argument is a string, then it is set as node-label of the new child.

If the second argument is a number then it is converted to a
string and set as node-label of the new child.

\subsubsection{remove}
\begin{verbatim}
void remove(const irep_name_string &name);
\end{verbatim}
This method looks for an edge with label \textit{name}
and removes it.


\subsubsection{move\_to\_sub and move\_to\_named\_sub}
\begin{verbatim}
void move_to_sub(irept &irep);
void move_to_named_sub(const irep_name_string &name, irept &irep);
\end{verbatim}
The first method creates a new ordered edge with a
child equal  to \textit{irep}. Then it sets \textit{irep} to\textit{ nil}.
The index of the edge is equal to the size of vector \textit{sub} before the call.

The second method does the same but for labeled edges.


\subsubsection{swap}
\begin{verbatim}
void swap(irept &irep);
\end{verbatim}
Exchange the content of the invoked node with the one of \textit{irep}.


\subsubsection{make\_nil}
\begin{verbatim}
void make_nil();
\end{verbatim}
Set the label of the node to "nil" and remove all the edges.


\subsubsection{get\_sub and get\_named\_sub and get\_comments }
\begin{verbatim}
const subt &get_sub();
const named_subt &get_named_sub();
const named_subt &get_comments();
\end{verbatim}
Return a constant reference to respectively
\textit{sub}, \textit{named\_sub} and \textit{comments}

\subsection{Expression}
Class \textit{exprt} inherits from class \textit{irept}. Expressions
have operands and a type. This is modeled with two edges labeled
respectively "operands" and "type". Class \textit{exprt} only adds to
the interface of \textit{irept}, specialized methods for accessing
operands and type informations.


\begin{figure}[h]
\centering
\includegraphics[width=0.3\textwidth]{expr}
\caption{A binary expression}
\label{fig:exprtree}
\end{figure}

\subsubsection{type}
\begin{verbatim}
const typet &type() const;
typet &type();
\end{verbatim}
Return a reference to the 'type' node

\subsubsection{has\_operands}
\begin{verbatim}
bool has_operands() const;
\end{verbatim}
Return true if the expression has operands.


\subsubsection{operands}
\begin{verbatim}
const operandst &operands() const;
\end{verbatim}
Return a reference to the vector of operands.

\subsubsection{op}
\begin{verbatim}
const exprt &op0();
const exprt &op1();
const exprt &op2();
const exprt &op3();
exprt &op0();
exprt &op1();
exprt &op2();
exprt &op3();
\end{verbatim}
Return a reference to the diverse operands.

\subsection{Type}
Class \textit{typet} inherits from \textit{irept}. Types
may have subtypes. This is modeled with two edges named
"subtype" and "subtypes".
Class \textit{typet} only adds to the interface of \textit{irept},
specialized methods for accessing the subtype information.

\begin{figure}[h]
\centering
\includegraphics[width=0.3\textwidth]{typetree}
\caption{A type tree}
\label{fig:typetree}
\end{figure}

\subsubsection{has\_subtype and has\_subtypes}
\begin{verbatim}
bool has_subtype() const;
bool has_subtypes() const;
\end{verbatim}
The first method returns true if the a subtype node exists.
is not \textit{nil}.
The second method returns true is a subtypes node exists.


\subsubsection{subtype and subtypes}
\begin{verbatim}
typet &subtype();
typest &subtypes();
\end{verbatim}
The first method returns a reference to the 'subtype' node.
The second method returns a reference to the vector of subtypes.


\subsection{Location}
The class \textit{locationt} inherits from the class \textit{irept}. It adds
specialized methods to manipulate the edges named: "file", "line", "column", "function"

\begin{figure}[h]
\centering
\includegraphics[width=0.3\textwidth]{locationtree}
\caption{A location tree}
\label{fig:locationtree}
\end{figure}

\section{Symbol and Symbol Table}

\subsection{Symbol}
A symbol is an object of class \textit{symbolt}. This class
is declared in "util/symbol.h". The code below shows a partial
declaration of the interface:


\begin{lgrind}
\input{symb.cpp.tex}
\end{lgrind}


Symbol names are unique. Scopes are handled by adding prefixes
to symbols:


\begin{lgrind}
\input{symbpref.c.tex}
\end{lgrind}

\subsection{Symbol Table}
A symbol table is an object of class \textit{contextt}. This class
is declared in "util/context.h". The code below shows a partial
declaration of the interface:


\begin{lgrind}
\input{context.cpp.tex}
\end{lgrind}

\section{Goto-program}
