[comp.lang.ada] Compiling Ada into C

neff@Shasta.STANFORD.EDU (Randy Neff) (02/06/88)

Why do people want to compile Ada into C?   Because it would appear to a
naive person that this would be a lot less work than compiling into 
assembly language or binary relocatable format.  When, in fact, the C language
features (deficiencies) actually get in the way.

So here is a short list of the some of the problems that appear when compiling
to C, even assuming that you have a fully semanticized abstract syntax tree 
available.

1.  You have to design, write, and validate a runtime tasking
system.   This requires complete understanding of LRM ch 9, the language
maintance committee notes, the implementers' guide, and the folklore.

2.  C does not support nested routines.  The name space is broken into
'local' and 'global' plus included files.   Ada has a very nested name
space.   Fully qualified names will be too long for most C compilers.
Ada has overloaded names, C doesn't.   What to do and still be able to use
a symbolic debugger?   C names are case sensitive.

3.  C does not support nested routines.  Therefore it does not generate any
code to handle a 'display' in order to access objects on the stack that are
not local and not global.  The generated C code must explicitly manage a
display on the stack in the procedure frame for intermediate addressing.

4.  Exceptions and exception handlers are defined in Ada.  The generated
C code must explicitly manage the visiblity of exception handlers on the stack.
It would be nice if the default exception handler could report the 
call stack with line numbers from the point of the exception being raised.
In any case, the debugger should know about exceptions and be able to
catch any at the point of raise, even when there are explicit handlers.

5.  C arrays are not Ada arrays: no bounds information is stored.  Generated
code must explicitly index and check bounds on all array actions.   Also
for strings.   C strings are terminated by ASCII.NUL.  No C library routines 
that use string parameters can be used directly.

6.  All of the LRM required checking must be explicitly generated:  bounds
for ranges and arrays;  null pointer dereferencing;  using fields in a
discriminent (variant) record;  checking that a package has been initialized;
checking assignment sizes;  etc.   C is an unchecked (unsafe) language.

7.  The semantics of parameter passing are different:  C is call by value
(requiring explicit passing of addresses to pointers for out parameters);
Ada is copy in and copy out for scalar parameters, and either copy in and out
or reference for arrays, records, or task types.

8.  Do even the simple arithmetic and boolean operators have the same meaning?
Maybe, maybe not.  Ada permits the redefinition of operators for the standard
types.   With representation and length clauses and pragma PACKED, it is 
possible to generate Ada types that have no corresponding C types.

9.  A lot of the power of C comes from the library of routines rather than
the language.   Most of the routines don't match Ada semantics and are
unusable.

wes@obie.UUCP (Barnacle Wes) (02/13/88)

In article <2460@Shasta.STANFORD.EDU>, neff@Shasta.STANFORD.EDU (Randy Neff) writes:
> Why do people want to compile Ada into C?

Your nine observations that follow this question are correct.  They
are also obvious.  If you were writing an Ada compiler that compiled
to assembly code, or directly to object code, you would also have to
take all of this into account.  The simple fact is C is much more
portable than any assembler.  If you write an Ada compiler that
compiles to Rolm 1666 assembler, the compiler can only generate code
for the 1666 and similar processors.  If you write it to produces C,
you can run it on many processors, you just have to port the
executable (probably written in C itself) to the target machine.
-- 
    /\              - " Against Stupidity,  -    {backbones}!
   /\/\  .    /\    -  The Gods Themselves  -  utah-cs!utah-gr!
  /    \/ \/\/  \   -   Contend in Vain."   -  uplherc!sp7040!
 / U i n T e c h \  -        Isaac Asimov   -     obie!wes

mack@inco.UUCP (Dave Mack) (02/19/88)

In article <49@obie.UUCP> wes@obie.UUCP (Barnacle Wes) writes:
>In article <2460@Shasta.STANFORD.EDU>, neff@Shasta.STANFORD.EDU (Randy Neff) writes:
>> Why do people want to compile Ada into C?
>
>Your nine observations that follow this question are correct.  They
>are also obvious.  If you were writing an Ada compiler that compiled
>to assembly code, or directly to object code, you would also have to
>take all of this into account.  The simple fact is C is much more
>portable than any assembler.  If you write an Ada compiler that
>compiles to Rolm 1666 assembler, the compiler can only generate code
>for the 1666 and similar processors.  If you write it to produces C,
>you can run it on many processors, you just have to port the
>executable (probably written in C itself) to the target machine.

I have been trying (now and then) to write an Ada-to-C translator
for the last couple of years, for the reasons you mention.

This is, shall we say, somewhat nontrivial. Has anyone done
this successfully? 


>    /\              - " Against Stupidity,  -    {backbones}!
>   /\/\  .    /\    -  The Gods Themselves  -  utah-cs!utah-gr!
>  /    \/ \/\/  \   -   Contend in Vain."   -  uplherc!sp7040!
> / U i n T e c h \  -        Isaac Asimov   -     obie!wes
                       ^^^^^^^^^^^^^^^^^^^^
BTW, Asimov was quoting Schiller.
-- 
  		Dave Mack	Master of BizZen
  McDonnell Douglas-Inco, Inc. 		DISCLAIMER: The opinions expressed
  8201 Greensboro Drive                 are my own and in no way reflect the
  McLean, VA 22102			views of McDonnell Douglas or its
  (703)883-3911				subsidiaries.
  {uunet | sundc | rlgvax | netxcom | decuac}!hadron!inco!mack
  Support your local Turing Police. Delete GREG/NOWAK.