From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ca15935e4fb21334 X-Google-Attributes: gid103376,public From: Marin David Condic Subject: Re: Storage space question Date: 1998/12/11 Message-ID: <3671463B.33E291@pwfl.com>#1/1 X-Deja-AN: 421223032 Content-Transfer-Encoding: 7bit Sender: condicma@bogon.pwfl.com References: <366FE278.FAF73497@pwfl.com> <74pfg4$3s6$1@nnrp1.dejanews.com> <36704ADB.73FA308D@pwfl.com> Content-Type: text/plain; charset=us-ascii Organization: Pratt & Whitney Mime-Version: 1.0 Reply-To: diespammer@pwfl.com Newsgroups: comp.lang.ada Date: 1998-12-11T00:00:00+00:00 List-Id: Matthew Heaney wrote: > > Marin David Condic 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