\section{Examples of Transformation}
\label(examples)

\subsection{cgatoi}

\subsubsection{source code}

\begin{verbatim}
cgatoi: using (cgInternal)
process (Q: atoiQ)
declare
  args: atoi;
  sign: integer;
  scopy: charstring;
  zcode: integer;
  ten: integer;
begin
  receive args from Q;
  args.i := 0;
  scopy := args.s;
  zcode <- convert of char#'0';
  ten <- 10;
  block declare
    c: char;
  begin
    remove c from scopy[0];
    select c
    where ('+')
      sign <- 1;
    where ('-')
      sign <- -1;
    otherwise
      sign <- 1;
      args.i <- (convert of c) - zcode;
    end select;
  end block;
  for c in scopy[] inspect
    args.i <- args.i * ten;
    args.i <- args.i + ((convert of c) - zcode);
  end for;
  args.i <- args.i * sign;
  return args;  
end process
\end{verbatim}

\subsubsection{{\bf li}-code before transformation}

\begin{verbatim}
using ( predefined inferredtype common terminalio objectio
        load cwd stdenv positions interpform listuff coercions
        annotate cginternal )
process cgatoi cginternal!atoiq 31
          linking ( )
/*LI0*/   _block , handlers others cgatoi48;
          _receive 3 0 
          _ilit 4 , 0
          _move 3.1 4 
          _copy 5 3.0 
/*LI5*/   _ordenum_lit 6 , 48
          _oecvt_int 7 6 
          _move 8 7 
          _ilit 9 , 10
          _move 10 9 
/*LI10*/  _privatize 5 
          _ilit 12 , 0
          _lookup_at 11 5 12 
          _remove_at 13 5 12 
          _ordenum_lit 14 , 45
/*LI15*/  _equal 15 13 14 
          _ordenum_lit 16 , 43
          _equal 17 13 16 
          _select * 15 * 17 , selectarms cgatoi20 cgatoi23 ;
          _branch , cgatoi26
cgatoi20: _ilit 18 , -1
          _move 19 18 
          _branch , cgatoi31
cgatoi23: _ilit 20 , 1
          _move 19 20 
/*LI25*/  _branch , cgatoi31
cgatoi26: _ilit 21 , 1
          _move 19 21 
          _oecvt_int 22 13 
          _isubtract 23 22 8 
/*LI30*/  _move 3.1 23 
cgatoi31: _copy 24 5 
          _initget 25 24 , pair 0 0
cgatoi33: _get_or_goto 25 24 , cgatoi41
          _imultiply 26 3.1 10 
/*LI35*/  _move 3.1 26 
          _oecvt_int 27 25 
          _iadd 28 3.1 27 
          _isubtract 29 28 8 
          _move 3.1 29 
/*LI40*/  _branch , cgatoi33
cgatoi41: _endget 25 24 
          _discard 24 
          _imultiply 30 3.1 19 
          _move 3.1 30 
/*LI45*/  _return 3 
          _endblock 
          _branch , cgatoi48
cgatoi48: _endprocess 
end
\end{verbatim}

\subsubsection{{\bf li}-code after transformation}

\begin{verbatim}
using ( predefined inferredtype common terminalio objectio
        load cwd stdenv positions interpform listuff coercions
        annotate cginternal )
process cgatoi cginternal!atoiq 13
          linking ( )
/*LI0*/   _escape , 0
          _escape , 1
          _escape , 2
          _escape , 3
          _escape , 4
end
\end{verbatim}

\subsubsection{{\bf C}-code after transformation}

The code has been edited to combine include files with the generated
code, and to exclude some irrelevant declarations.  The original
operand identifiers have been retained, and the comments suitably
edited and combined.  These changes should improve the clarity of the
generated {\bf C}-code for the human reader, and make it easier to
relate to the original {\bf li}-code.

\begin{verbatim}
\#include "li.h"
\#include "accessors.h"
\#include "interpform.cd"
\#include "predefined.cd"

\#define DEF(X) & data[X]
\#define REF(X) data[X]
\#define DOT(X) .value.record->data[X]
\#define QUAL(I) dot(get_elem(qualifiers,I),VARIANT_COMPONENT)

\#define GET TRUE

extern datarep dr_boolean;
extern datarep dr_bottom;
extern datarep dr_integer;
extern datarep dr_table;
extern datarep *qdatarepmap[];

void CH_cgatoi(current, sched, cop)
     pcb *current;
     schedblock *sched;
     valcell cop;
{
  register objectp data;
  valcell qualifiers;

  register dfd_table *ch_t;
  dfd_enumeration ch_e;
  valcell ch_val;
  predef_exception ch_returncode;

  pcb *ch_caller;

  predef_exception che_block();
  predef_exception che_endblock();
  predef_exception che_endprocess();
  predef_exception che_receive();
  predef_exception cp_table();
  predef_exception get_selected_object();
  predef_exception init_selector();
  predef_exception remove_at_func();

  int chf_iadd();
  int chf_imultiply();
  int chf_isubtract();

  void end_selector();
  void rem_return();

  pcb *do_return();  

  data = current->ep.h->data;
  qualifiers = dot(dcdot(current->prog,program__LI_PROGRAM),prog__pool);

  switch (current->ip) {
    case 0: goto c0;
    case 1: goto cb1;
    case 2: goto c2;
    case 3: goto c33;
    case 4: goto c48;  }

c0:/* block , 0 */

  if((ch_returncode = che_block(QUAL(0),sched)) != Normal)
     goto ch_raise_exception;
  current->ip = 1;

cb1:/* receive 3 0 */

  if ((ch_returncode = che_receive(DEF(3),DEF(0),sched)) != Normal)
     goto ch_raise_exception;
  return;

c2:/* ilit 4 , 0 
      move 3.1 4 */

  REF(3)DOT(1).value.integer = 0;
  REF(3)DOT(1).tsdr = & dr_integer;

/* copy 5 3.0 */

  if((ch_returncode = cp_table(& ch_val, REF(3)DOT(0).value)) != Normal)
     goto ch_raise_exception;
  (*(REF(5).tsdr)->finalize)(REF(5).value,F_DISCARD,sched);
  REF(5).value = ch_val;
  REF(5).tsdr = & dr_table;

    /* ordenum_lit 6 , 48
       oecvt_int 7 6 
       move 8 7 */

    /* ilit 9 , 10
       move 10 9 */

    /* privatize 5 */

  if (REF(5).value.table->refcount != 1)
    if ((ch_returncode = doCopyOnWrite(DEF(5))) != Normal)
       goto ch_raise_exception;

    /* ilit 12 , 0
       lookup_at 11 5 12 */

  if ( !((ch_t = REF(5).value.table)->size) ) goto ch_notfound;
  (*(REF(11).tsdr)->finalize)(REF(11).value,F_DISCARD,sched);
  (*(ch_t->tbls[ORDER_TBL].des->lookup_at))
    (ch_t, ORDER_TBL, & REF(11).value,0);
  REF(11).tsdr = qdatarepmap[ch_t->tsdr->number];

    /* remove_at 13 5 12 */

  if ((ch_returncode = remove_at_func(DEF(13),DEF(5),0)) != Normal)
     goto ch_raise_exception;

    /* ordenum_lit 14 , 45
       equal 15 13 14 */

  if ( REF(13).value.ord_enum == 45 ) goto c20;

    /* ordenum_lit 16 , 43
       equal 17 13 16 */

  if ( REF(13).value.ord_enum == 43 ) goto c23;

    /* select * 15 * 17 , selectarms cgatoi20 cgatoi23
       branch , 26 */

   goto c26;

c20:/* ilit 18 , -1
       move 19 18 */

  REF(19).value.integer = -1;
  REF(19).tsdr = & dr_integer;

    /* branch , 31 */

   goto c31;

c23:/* ilit 20 , 1
       move 19 20 */

  REF(19).value.integer = 1;
  REF(19).tsdr = & dr_integer;

    /* branch , 31 */

   goto c31;

c26:/* ilit 21 , 1
       move 19 21 */

  REF(19).value.integer = 1;
  REF(19).tsdr = & dr_integer;

    /* oecvt_int 22 13 */

  REF(22).value.integer = REF(13).value.ord_enum;
  REF(13).tsdr = & dr_integer;

   /* isubtract 23 22 8
      move 3.1 23 */

  if (!chf_isubtract(DEF(3)DOT(1),REF(22).value.integer,48))
    goto ch_depletion;

c31:/* copy 24 5 */

  if((ch_returncode = cp_table(& ch_val, REF(5).value)) != Normal)
     goto ch_raise_exception;
  (*(REF(24).tsdr)->finalize)(REF(24).value,F_DISCARD,sched);
  REF(24).value = ch_val;
  REF(24).tsdr = & dr_table;

    /* initget 25 24 , pair 0 0 */

  (*(REF(25).tsdr)->finalize)(REF(25).value,F_DISCARD,sched);
  if ((ch_returncode = init_selector(DEF(25),DEF(24),nil,sched,GET,0,0
    )) != Normal) goto ch_raise_exception;

c33:/* get_or_goto 25 24 , 41 */

  if ((ch_returncode = get_selected_object(DEF(25),DEF(24),sched))
	 != Normal)
     goto c41;

    /* imultiply 26 3.1 10
       move 3.1 26 */

  if (!chf_imultiply(DEF(3)DOT(1),REF(3)DOT(1).value.integer,10))
     goto ch_depletion;

    /* oecvt_int 27 25 */

  REF(27).value.integer = REF(25).value.ord_enum;
  REF(27).tsdr = & dr_integer;

    /* iadd 28 3.1 27 */

  if (!chf_iadd(DEF(28),REF(3)DOT(1).value.integer,REF(27).value.integer))
     goto ch_depletion;

    /* isubtract 29 28 8
       move 3.1 29 */

  if (!chf_isubtract(DEF(3)DOT(1),REF(28).value.integer,48))
     goto ch_depletion;

    /* branch , 33 */

   goto c33;

c41:/* endget 25 24 */

  end_selector(DEF(25),DEF(24),sched,GET);

    /* discard 24 */

  (*(REF(24).tsdr)->finalize)(REF(24).value,F_DISCARD,sched);
  REF(24).tsdr = & dr_bottom;

    /* imultiply 30 3.1 19
       move 3.1 30 */

  if (!chf_imultiply(DEF(3)DOT(1),REF(3)DOT(1).value.integer,
    REF(19).value.integer)) goto ch_depletion;

    /* return 3 */

  if (ch_caller = do_return(REF(3).value)) {
    if (ch_caller->type < CProcess) ch_caller->ip++;
      sched->wakeup_proc(sched, ch_caller);}
  else rem_return(REF(3).value);
  REF(3).tsdr = & dr_bottom;

    /* endblock */

  che_endblock(sched);

c48:/* endprocess */

  che_endprocess(sched); return;

ch_depletion :
  ch_val.enumeration = Depletion;
  goto ch_builtin_handler;

ch_notfound :
  ch_val.enumeration = NotFound;
  goto ch_builtin_handler;

ch_raise_exception :
  ch_val.enumeration = ch_returncode;

ch_builtin_handler :
  ch_e = handler_type__builtin;

ch_exception :
  set_error(current, ch_e, ch_val);
  current->ip = raise_exception(current);
  goto select;

}
\end{verbatim}

{\tt QUAL(0)} (see {\tt block} operation following switch) is a {\tt
valcell}, where {\tt QUAL(0).table} is the address of a table
consisting of one entry, a {\tt block{\uline}handler} variant with {\tt
handler} in case {'others'} and {\tt label} = 4, corresponding to the
last {\tt escape} instruction in the residual {\bf li}-code table (see
{\tt interpform} and {\tt predefined} definitions).
