comp.lang.ada
 help / color / mirror / Atom feed
From: adam@irvine.com (Adam Beneschan)
Subject: Re: some questions re. Ada/GNAT from a C++/GCC user
Date: 1996/03/29
Date: 1996-03-29T00:00:00+00:00	[thread overview]
Message-ID: <4jht4r$tpo@krusty.irvine.com> (raw)
In-Reply-To: 315AC5E7.3A77@escmail.orl.mmc.com

Ted Dennison <dennison@escmail.orl.mmc.com> writes:
 
 >> When I make two different instantiations of a generic package with the
 >> same arguments, I understand the compiler treats them formally as two
 >> different packages, which is OK with me.  However, I'd appreciate
 >> knowing the compiler wouldn't actually output two redundant copies of
 >> the corresponding (identical?) machine code, but instead share the
 >> code.  I saw somewhere that the compiler is given considerable freedom
 >> to share one instantiation between several arguments if it thinks it's
 >> appropriate, which is also OK with me.  However, I haven't seen any
 >> guarantee that the compiler won't output redundant copies for
 >> instantiations with identical arguments.  Is there such a guarantee?
 >
 >If you want a guarantee, put the code you want shared in a non-generic
 >package or procedure, and call it from your generic package or
 >procedure.

I don't think this will always work, at least not cleanly.  Suppose
you have a generic whose only generic formal parameter is a private
type:
        
        generic
            type T is private;
        package GENERIC_PACKAGE is
            ... 
            procedure Do_Something (X : out T);
            procedure Do_Something_Else (X : in T);
            ...
        end GENERIC_PACKAGE;

About the only thing the generic package body can do with objects of
type T is to move them around.  So, ignoring for the moment strange
cases like unconstrained arrays, it should be easy for a compiler to
generate one copy of Do_Something and Do_Something_Else (and similarly
any procedure declared in the package body) for all instantiations of
GENERIC_PACKAGE.  Each procedure would take the size of T as an extra
parameter, and whenever the procedure needs to move objects of type T
around, it just does a raw byte copy, using the size parameter.  This
should run only slightly slower than if the compiler generated a
separate copy of Do_Something for every instantiation, and each copy
were able to treat the size as a known constant.

But suppose you have a compiler that always generates a separate copy
of all procedures for each instantiation.  How would you write a
non-generic shared procedure that you could call from the generic
package? 

    procedure Non_Generic_Do_Something (---well, what?)

What types would the parameters of this routine be?  The only
possibilities I can see would be to have this routine take a
SYSTEM.ADDRESS and a size parameter, or a byte array type, or some
other low-level type.  No matter how you do it, the code in
GENERIC_PACKAGE, in order to call Non_Generic_Do_Something, would have
to use UNCHECKED_CONVERSION or something like that (which doesn't fit
my definition of "cleanly").

Even if all you want is to make sure that only one copy of the code
is generated when the generic parameters are identical, how can you do
this by writing a shared non-generic routine?  If, in four different
places, you say:

    package GP1 is new GENERIC_PACKAGE (RECORD_TYPE_1);

    package GP2 is new GENERIC_PACKAGE (RECORD_TYPE_2);

    package GP3 is new GENERIC_PACKAGE (RECORD_TYPE_1);

    package GP4 is new GENERIC_PACKAGE (RECORD_TYPE_2);

How could you write the Non_Generic_Do_Something routine, and the
generic Do_Something procedure, so that just two versions of the
Do_Something code (one to handle RECORD_TYPE_1 and one to handle
RECORD_TYPE_2) are generated by the compiler?

                                -- Adam




  reply	other threads:[~1996-03-29  0:00 UTC|newest]

Thread overview: 80+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-03-27  0:00 some questions re. Ada/GNAT from a C++/GCC user Bill Newman
1996-03-27  0:00 ` Robert Dewar
1996-03-28  0:00   ` Brian Rogoff
1996-03-29  0:00     ` John G. Volan
1996-03-30  0:00       ` Mike Young
1996-03-30  0:00         ` Ted Dennison
1996-03-31  0:00           ` Mike Young
1996-03-30  0:00       ` Robert A Duff
1996-03-31  0:00         ` John G. Volan
1996-03-31  0:00           ` Mike Young
1996-04-02  0:00             ` Glenn H. Porter
1996-04-02  0:00               ` Jonas Nygren
1996-04-02  0:00               ` Robert Dewar
1996-04-03  0:00               ` Geert Bosch
1996-04-03  0:00                 ` Robert Dewar
1996-04-01  0:00           ` Bruce.Conroy
1996-04-01  0:00           ` Robert A Duff
1996-04-03  0:00             ` Scott Leschke
1996-04-04  0:00               ` AdaWorks
1996-03-31  0:00         ` Robert Dewar
1996-04-01  0:00           ` Norman H. Cohen
1996-04-01  0:00       ` Norman H. Cohen
1996-04-01  0:00         ` Mike Young
1996-04-02  0:00           ` Norman H. Cohen
1996-04-02  0:00           ` David Shochat
1996-04-02  0:00             ` Mike Young
1996-04-02  0:00           ` Robert Dewar
1996-04-01  0:00         ` Robert A Duff
1996-04-01  0:00           ` Mike Young
1996-04-02  0:00             ` Robert A Duff
1996-04-02  0:00             ` Norman H. Cohen
1996-03-28  0:00   ` Norman H. Cohen
1996-03-28  0:00 ` Scott Leschke
1996-03-29  0:00   ` Robert I. Eachus
1996-03-29  0:00   ` Robert A Duff
1996-03-30  0:00     ` Richard Pitre
1996-03-30  0:00       ` Robert A Duff
1996-03-31  0:00         ` AdaWorks
1996-04-01  0:00           ` Robert A Duff
1996-04-01  0:00             ` AdaWorks
1996-04-01  0:00               ` Mike Young
1996-04-02  0:00                 ` Robert Dewar
1996-04-02  0:00                 ` AdaWorks
1996-04-01  0:00             ` Norman H. Cohen
1996-04-01  0:00             ` Ken Garlington
1996-04-01  0:00               ` Robert A Duff
1996-04-02  0:00                 ` Ken Garlington
1996-04-02  0:00                   ` Robert A Duff
1996-04-02  0:00                     ` Ken Garlington
1996-04-02  0:00                       ` Robert A Duff
1996-04-03  0:00                         ` Ken Garlington
1996-04-09  0:00                           ` Matt Kennel
1996-04-03  0:00                         ` David Emery
1996-04-02  0:00                 ` Tucker Taft
1996-04-02  0:00                   ` Felaco
1996-04-02  0:00                     ` Robert Dewar
1996-04-03  0:00                     ` Mark A Biggar
1996-04-01  0:00         ` Robert Dewar
1996-04-01  0:00         ` Richard A. O'Keefe
1996-04-01  0:00           ` Robert A Duff
1996-04-02  0:00       ` Robert I. Eachus
1996-03-29  0:00   ` Bill Newman
1996-03-28  0:00 ` Ted Dennison
1996-03-29  0:00   ` Adam Beneschan [this message]
1996-03-29  0:00 ` Robert A Duff
1996-03-29  0:00   ` Brian Rogoff
1996-04-01  0:00     ` Mark A Biggar
1996-04-01  0:00       ` Robert A Duff
1996-03-30  0:00   ` Iterators (was Re: some questions re. Ada/GNAT from a C++/GCC user) Robert I. Eachus
1996-03-31  0:00     ` Mike Young
1996-03-31  0:00       ` Fergus Henderson
     [not found]   ` <4jlj79$h1k@Nntp1.mcs.net>
1996-04-01  0:00     ` some questions re. Ada/GNAT from a C++/GCC user Robert A Duff
1996-04-02  0:00       ` Kevin Cline
1996-04-02  0:00         ` Robert A Duff
1996-04-01  0:00   ` Iterators (was Re: some questions re. Ada/GNAT from a C++/GCC user) Robert I. Eachus
1996-04-04  0:00   ` some questions re. Ada/GNAT from a C++/GCC user Jon S Anthony
1996-03-30  0:00 ` Simon Wright
1996-04-01  0:00 ` Laurent Guerby
1996-04-01  0:00   ` Robert A Duff
  -- strict thread matches above, loose matches on Subject: below --
1996-03-28  0:00 Simon Johnston
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox