comp.lang.ada
 help / color / mirror / Atom feed
From: Marin David Condic <condicma@bogon.pwfl.com>
Subject: Re: Storage space question
Date: 1998/12/11
Date: 1998-12-11T00:00:00+00:00	[thread overview]
Message-ID: <3671463B.33E291@pwfl.com> (raw)
In-Reply-To: m31zm7o4lm.fsf@mheaney.ni.net

Matthew Heaney wrote:
> 
> Marin David Condic <condicma@bogon.pwfl.com> writes:
> 
> > I've done exactly this in any number of situations. Enumerations are
> > definitely "The Ada Way" of solving this problem, so naturally you want
> > to try to do this.
> 
> I don't necessarily agree.  Giving enumeration types a representation
> clause seems to be of dubious value.

I was referring to using enumerations in general to represent values for
fields in a register - not necessarily with a representation clause.
Fields in special registers are quite often naturally reflected by an
enumeration. (Off, Standby, On) or even op-codes in machine instructions
are examples. Other times, fields represent some sort of count -
Watchdog_Timer_Resets => 0..7 might be an example. If the bit-patterns
are labeled with some mnemonic in the hardware documentation, then
enumerations are a very natural way of representing that. If they are
contiguous starting at zero - save your rep clause work. If not
contiguous, then the rep clause suits it well. As long as your compiler
implementation is reasonably efficient about it, I see no reason to
resort to named numbers or constants since they are going to be less
type-safe. 

> 
> > If you can't find an answer in the compiler-specific documentation, you
> > can always get there through the
> > less-desirable-but-no-worse-than-C-does-it method of declaring a bunch
> > of named numbers:
> >
> > Some_Name : constant := 2#0101# ;
> 
> But that's going too far, because you've turned off all the type
> checks.

Sure. I wouldn't normally adopt this method at all because all of the
embedded compilers I've got for my real time hardware do a reasonable
job of implementing enumerations. My point was that if compiler A does a
lousy job with enumerations or you can't find out how to tell compiler A
to do a better job _you_are_no_worse_off_than_you_are_with_C_. You can
always resort to a C sort of implementation, so there should be no
reason to claim "I can't do this as efficiently as I can with C". Of
course, there will be issues of constant folding with named numbers
which may be undesirable and you'll want to go with a constant
declaration and some flavor of pragma Volatile (Ada83 doesn't have it
but lots of vendors found a way to give it to us.)

> 
> A middle position between named numbers and enumeration types is
> constants of a integer type:
> 
>   type Robot_Control is range 0 .. 3;
> 
>   Run_Robot : constant Robot_Control := 0;
> 
>   Stop_Robot : constant Robot_Control := 1;
> 
>   Turn_Robot : constant Robot_Control := 3;
> 
>   type Control_Register is
>     record
>       ...
>       Control : Robot_Control;
>       ...
>    end record;
> 
> As long as you only use the constants (not the literals), you get all
> the type safety that an enumeration type buys you.
> 
> Which is why rep clauses for enumeration types is overkill, that adds
> unnecessary complexity to the language.

Well, not quite all the type safety. Suppose that the field occupies 3
bits but only the values 0, 1 and 2 are currently valid. Or worse, only
the values 2#001#, 2#010# and 2#100# are valid. With an integer type,
there is no way of eliminating the invalid values. With enumerations,
you can't accidentally get an invalid bit pattern assigned to the
register field.

Granted, an integer type restricted to the field size & range is better
than nothing. But I still think enumerations are more often the natural
expression of this unless the field represents a count of something.

MDC
-- 
Marin David Condic
Real Time & Embedded Systems, Propulsion Systems Analysis
United Technologies, Pratt & Whitney, Large Military Engines
M/S 731-95, P.O.B. 109600, West Palm Beach, FL, 33410-9600
Ph: 561.796.8997         Fx: 561.796.4669
***To reply, remove "bogon" from the domain name.***

"Transported to a surreal landscape, a young girl kills the first woman
she meets and then teams up with three complete strangers to kill
again."

        -- TV listing for the Wizard of Oz




  reply	other threads:[~1998-12-11  0:00 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-12-10  0:00 Storage space question Craig Allen
1998-12-10  0:00 ` dennison
1998-12-10  0:00 ` Marin David Condic
1998-12-10  0:00   ` Pat Rogers
1998-12-10  0:00     ` dennison
1998-12-10  0:00       ` Pat Rogers
1998-12-10  0:00   ` Tucker Taft
1998-12-10  0:00     ` callen
1998-12-10  0:00       ` Marin David Condic
1998-12-10  0:00         ` Tucker Taft
1998-12-11  0:00           ` dennison
1998-12-11  0:00         ` Matthew Heaney
1998-12-11  0:00           ` Marin David Condic [this message]
1998-12-12  0:00             ` Matthew Heaney
1998-12-12  0:00               ` David C. Hoos, Sr.
1998-12-12  0:00                 ` Matthew Heaney
1998-12-14  0:00                 ` dennison
1998-12-10  0:00       ` dennison
1998-12-11  0:00       ` Matthew Heaney
1998-12-10  0:00   ` Matthew Heaney
1998-12-10  0:00     ` dennison
replies disabled

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