comp.lang.ada
 help / color / mirror / Atom feed
* Flags in Ada?
@ 1994-10-25 10:36 Andre Spiegel
  1994-10-25 10:07 ` David Emery
                   ` (3 more replies)
  0 siblings, 4 replies; 33+ messages in thread
From: Andre Spiegel @ 1994-10-25 10:36 UTC (permalink / raw)


I am trying to use UNIX system calls from Ada, which has been working
fine so far, i.e. the `translation' into Ada looks rather neat and
secure (enumeration types with representation clauses for C #define'd
constants, etc.), but how would I go about representing *flags* --
i.e. words, in which the individual bits have names, and can be set,
cleared, or'ed, xor'ed, and the like?

I seem to recall that all of this is realized very comfortably in Ada
9X, but how can I do it in Ada 83?

Another question: UNIX returns a pointer to some data structure, which
I want to read and possibly modify from within Ada. What would be the
adequate way to do this? Off the cuff, I can think of two
possibilities:

  (1) declare the operating system routine to be returning an access
      value, pointing to an Ada record, which resembles the C
      structure.

	type Data is record
                       ...
	             end record;

	type Data_Access is access Data;

        function OS_Routine (...) return Data_Access;

      Disadvantage: As far as I know, access types do not necessarily 
      have to be implemented as pointers. I have read (recently in
      this group), that GNAT, for example, doesn't always do that.
      Also, the Sparc Alsys compiler (which I'm using) seems to use
      the numeric address *one* to indicate a null access value...

  (2) declare the operating system routine to be returning
      System.Address, and use an address clause to interpret the
      memory area pointed to as the corresponding structure.

        function OS_Routine (...) return System.Address;

        Data_Pointer : System.Address;

	...

        Data_Pointer := OS_Routine (...);

        -- check for null address here, using Unchecked_Conversion
        -- to convert Data_Pointer to Integer?

	declare
          Result : Data;
	  for Result use at Data_Pointer;
        begin
          -- process Result...
        end;
 
      Questions: Is this acceptable? Why does RM83 13.5(8) forbid to
      use address clauses to achieve overlays, then? How do compilers 
      usually implement this -- does the declare statement force 
      data allocation on the stack, or is it optimized away, yielding
      simply a bypass of the type checking system, just like a `call'
      to Unchecked_Conversion should simply translate into _nothing_,
      if I'm not all wrong?

In either of these possibilities, what if `Data' is a record with
variants (forced to the right representation with a rep clause)? Might
Ada get into trouble when interpreting it, or even refuse to work with
a record value it has not assigned and checked itself?

Again, I'm primarily interested in the `83 solutions, but would also
like to learn how everything will be much simpler and more 
straightforward in `9X :-). 

BTW, I'm sure all of this has already been asked and discussed at
length. Forgive me if this is just an FAQ.

Regards,

--
Andre Spiegel                     |  This life is a test. It is only a test.
                                  |  Had this been an actual life, you would
University of Stuttgart, Germany  |  have received further instructions as to
Computer Science                  |  where to go and what to do.
                                                            -- Author unknown

	   email: spiegel@bruessel.informatik.uni-stuttgart.de

      
       
 
      



^ permalink raw reply	[flat|nested] 33+ messages in thread
* Re: Flags in Ada?
@ 1994-10-25 16:22 tmoran
  0 siblings, 0 replies; 33+ messages in thread
From: tmoran @ 1994-10-25 16:22 UTC (permalink / raw)


>Look at IEEE Std 1003.5-1992 (soon to be an ISO standard, too) for the
>Ada interface to POSIX.  The rationale is very good on the design
>...
>I wrote a paper for Tri-Ada '90 on my implementation experience for
Is either document ftp-able?



^ permalink raw reply	[flat|nested] 33+ messages in thread
* Re: Flags in Ada?
@ 1994-10-27  5:05 tmoran
  1994-10-27 13:29 ` Robert Dewar
  1994-10-27 17:15 ` Norman H. Cohen
  0 siblings, 2 replies; 33+ messages in thread
From: tmoran @ 1994-10-27  5:05 UTC (permalink / raw)


>As I noted in a previous message, pragma pack should work just fine on
>records, it is NOT necessary to give record representation clauses unless
>they are needed to match an external specification. In Ada 9X in particular,
>one would assume that even in the latter case, the use of:
>
> pragma COnvention (C, rectype);

>should be adequate to ensure a match up with the C type. In GNAT, convention
>C is the default (i.e. for datatypes that match, convention Ada and
>convention C are identical), so it works even in the absnece of this
>pragma.

Does 'C' specify how the bit fields are arranged?
My copy of 'The ANSI C Programming Language' (by K&R) says 'Almost
everything about bit fields is implementation dependent.  ....  Fields
are assigned left to right on some machines and right to left on others.'
And in its appendix titled 'ANSI implementation-specific standards' my
Borland C++ 3.1 manual has an entry specifying 'The order of allocation
of bit fields within an int.'



^ permalink raw reply	[flat|nested] 33+ messages in thread
* Re: Flags in Ada?
@ 1994-10-27  5:06 tmoran
  1994-10-27 13:47 ` Robert Dewar
  0 siblings, 1 reply; 33+ messages in thread
From: tmoran @ 1994-10-27  5:06 UTC (permalink / raw)


>Most of the techniques that have been proposed for this use packed
>boolean and representation specs within the visibile part of the
>package spec.  My experience is that this can be a dangerous approach,
>particularly for portability.
>
  ...
> package POSIX is
>  ...
>    type Option_Set is private;

Where is the conceptual focus?  If the important object is the set of
flags (Option_Set here), then certainly it should be dealt with as an
entity - either hiding the implementation details as per your example
or exposing it as an array of booleans (packed if interfacing or
other reasons so indicate).  But if the 'flags' are in fact logically
heterogenous data, that just happen to be tossed together to match
some external interface, then, like all heterogeneous data, they should
be in a record, not an array.  If it's just an interfacing requirement
that names and places the bits, it seems to me that like all system
dependencies their specification should be buried inside a package which
exports a clean, logical, and portable view of their relevant information.



^ permalink raw reply	[flat|nested] 33+ messages in thread
* Re: Flags in Ada?
@ 1994-10-28  3:59 tmoran
  1994-10-28 13:43 ` Robert Dewar
  0 siblings, 1 reply; 33+ messages in thread
From: tmoran @ 1994-10-28  3:59 UTC (permalink / raw)


Some downsides of using 9X modular types:

 Console.Control := Start_Read_Bit;
may look correct, but perhaps what you really wanted was
 Console.Control := Console.Control or Start_Read_Bit;

Using records, those two are done with less ambiguity with
 Console.Control := Start_Read_OP_Code;
 Console.Control.Start_Read := True;

>...  a measure of safety.  The compiler will complain if you
>use a Status_Byte mask on the Control register, or vice-versa.

But    Console.Control := Console.Control*3;   is legal, though
perhaps not what you meant to code.  And there is a temptation
to do arithmetic (shift, for instance) to accomplish things.
In some circumstances that's fine, and even a good reason to use
modular types rather than records, but it clearly locks you
into a certain bit representation.  There are plenty of programmers
in the world who will do 'clever' arithmetic to juggle bits. :(



^ permalink raw reply	[flat|nested] 33+ messages in thread

end of thread, other threads:[~1994-11-03 23:08 UTC | newest]

Thread overview: 33+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1994-10-25 10:36 Flags in Ada? Andre Spiegel
1994-10-25 10:07 ` David Emery
1994-10-25 16:19 ` Norman H. Cohen
1994-10-26  3:19   ` tmoran
1994-10-26  9:59     ` David Emery
1994-10-26 22:32       ` Robert Dewar
1994-10-27 13:24         ` Norman H. Cohen
1994-10-27 15:15         ` John Volan
1994-10-31  9:29         ` David Emery
1994-10-27 22:34       ` Henry G. Baker
1994-10-26 14:33     ` Robert Dewar
1994-10-26 17:43     ` Norman H. Cohen
1994-10-26 15:54   ` Andre Spiegel
1994-10-26  0:36 ` Dale Stanbrough
1994-10-26 11:01   ` Robert Dewar
1994-10-27  8:23 ` Henri Altarac
1994-10-27 23:00   ` Robert Dewar
1994-10-31  9:32     ` David Emery
  -- strict thread matches above, loose matches on Subject: below --
1994-10-25 16:22 tmoran
1994-10-27  5:05 tmoran
1994-10-27 13:29 ` Robert Dewar
1994-10-27 17:15 ` Norman H. Cohen
1994-10-28  3:51   ` Robert Dewar
1994-10-27  5:06 tmoran
1994-10-27 13:47 ` Robert Dewar
1994-10-28  2:41   ` Tucker Taft
1994-10-30 13:31     ` Robert Dewar
1994-10-28  3:59 tmoran
1994-10-28 13:43 ` Robert Dewar
1994-10-31 14:19   ` Norman H. Cohen
1994-11-02 14:06     ` Mats Weber
1994-11-03 23:08       ` Robert Dewar
1994-11-03 11:26     ` Robert Dewar

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