\section{Structure of the C-coded Module}
\label{structure}

The module consists of a single {\bf C}-function.

\begin{verbatim}
            <global_definitions_and_includes>

            void <Name of Process>(current, sched, cop)

            { <declarations and initialization>

              select: switch (SELECTOR) {
                case 0: goto <First C-code sequence>;
                    ...
                case n: goto <Last C-code sequence>;
              }

              <First C-coded sequence>
                    ...
              <Last C-coded sequence>

              <labeled code to raise exceptions>

              <conditional_re_select>
            }
\end{verbatim}

\subsection{Global Declarations}
The {\tt <global{\uline}definitions{\uline}and{\uline}includes>} are
specified in {\tt hermes/chcode/chdecl.h}.  It defines a few
pre-processor symbols and includes the relevant {\tt .h} files of the
interpreter and of UNIX.  These declarations are essential to all {\bf
C}-coded text files.

\subsection{Function Declaration}
The {\tt <Name of Process>} is the same as the {\tt name} field of the
{\tt prog} record which contains the residual {\bf li}-code, prefixed
by the string ``CH\uline''.\footnote{
This prefix eliminates conflict with reserved or predefined names -
e.g. {\tt typedef}.}
The arguments are the interface to the interpreter.

\begin{itemize}

\item {\tt current} is the address of the Process Control Block of this
process.  It contains {\tt ip}, the {\em instruction pointer}, and
provides access to the variables and qualifiers of the process.

\item {\tt sched} is the address of the head of the interpreter's list
of scheduling blocks.

\item {\tt cop} addresses the current {\bf li}-code instruction,
the {\tt escape} instruction which caused the interpreter to call the
module.\footnote{
It is included as an optimization, since the interpreter must calculate
it in any case.  However, it is not needed if the entire {\bf li}-coded
program consists of {\tt escape} instructions, since in that case it is
redundant with {\tt current->ip}.}
\end{itemize}

\subsection{Local Declaration}

Local variables are:

\begin{itemize}

\item {\tt data} is the address of the process variable address list.

\item {\tt qualifiers} references the pooled, ordered table of compound
Hermes objects needed by some of the transformed instructions (see
{\tt interpform} definitions.)

\item {\tt arguments[]} is used by the {\tt call} and {\tt select}
instructions and some of the table-access functions to construct a list
of operands.\footnote{
See Appendix~\ref{descriptors} for examples.}

\item The other local variables are auxiliary operands for various
operations; for example, {\tt ch{\uline}returncode} holds the
exception code value returned by many of the interpreter functions.

\item Many interface declarations of interpreter functions also appear
in the local declarations.

\end{itemize}

The initialization code prepares for access to the variable list and
to the qualifiers, by initializing the local variables {\tt data} and
{\tt qualifiers}.

With the exception of two {\tt \#define}d symbols, all of the above
declarations and code are common to all {\bf C}-coded text files.  They
are specified in file {\tt hermes/ccode/chhead.h}.  The two symbols
are:
\begin{itemize}
\item INTERPRET is defined when the residual {\bf li}-code includes
interpreted operations other than {\tt escape}.
\item ARGUMENTS is defined as the maximum size of the {\tt arguments}
array.
\end{itemize}

The {\tt switch} statement selects the {\bf C}-coded sequence which is
to begin executed at each entry.  {\tt SELECTOR} is either {\tt
current->ip} (the {\em instruction pointer}) or the integer value of
the Hermes {\em qualifier} of the {\tt escape} instruction\footnote{
They agree if there are no interpreted instructions in the residual
{\bf li}-code.  } -the latter if INTERPRET is defined. The {\tt
switch} statement is iterated to implement computed branches,
particulary Hermes exceptions and exits.

\subsection{{\tt Cblocks}}

Each {\bf C}-coded sequence, called a {\tt cblock}, begins with one or
more labels.  The last label is referenced by a {\tt goto} of the {\tt
switch} statement.  A {\em blocking} instruction is one of {\tt receive},
{\tt select}, {\tt call}.  If the {\tt cblock} consists of one {\em blocking}
instruction, the last label begins with the letters ``cb''.  Otherwise,
it begins with the letter ``c''.  The rest of a {\tt cblock} label is the
original ordinal of the first {\bf li}-code instruction to be included in
the {\tt cblock}.

A {\em blocking} instruction occupies a {\tt cblock} by itself, since
the interpreter may suspend it, in which case it must begin a {\tt
cblock} when it is resumed, or the interpreter may resume the process
at the following instruction, which must therefor begin a {\tt cblock}
as well.  If the {\em blocking} instruction is accessible other than
via an {\tt escape}, the last label of its {\tt cblock} is immediately
preceded by an assignment of the process's {\em instruction pointer} -
this guarantees the accuracy of the {\em instruction pointer} whenever
the process might block.  The code generated for a {\em blocking}
instruction is always terminated by a {\bf C} {\tt return} statement
or, for {\tt select}, conditional iteration of the {\tt switch}
statement; i.e. execution {\underline never} continues sequentially
following a {\em blocking} instruction - the next instruction is
executed via the {\tt switch}.

An {\tt cblock} may be followed by another {\tt cblock} or by an
interpreted instruction.  In the latter case, if the flow of execution
would normally continue sequentially, following the last {\bf C}-coded
instruction of the {\tt cblock}, the {\tt cblock} is terminated with a
{\bf C} {\tt return} statement, to return control to the interpreter.
The framing code of each {\tt cblock} is generated by the process {\tt
chencode} during Phase 3 of the transformation.

\subsection{Exception handling and iteration}

The file {\tt hermes/ccode/chtail.h} provides canned code for handling
built-in exceptions, from {\tt raise} instructions and from many
interpreter functions, and for routing {\tt exit} instructions.  A new
value of the {\em instruction pointer} is computed by the interpreter
function {\tt raise{\uline}exception}.

When the corresponding residual {\bf li}-code instruction is an {\tt
escape}, a {\tt goto select} is executed, to iterate the {\tt switch}
statement.  When the instruction is interpreted, a {\bf C} {\tt
return} is executed instead.
