[comp.lang.pascal] UNtagged variant records and JPAM

g_harrison@vger.nsu.edu (11/10/90)

Untagged variant records have been used for years to do special things like bit
twiddling and demonstrating the implementation of data types - - as in

const IntSize = ....... {size of an INTEGER}
type
	FunnyRecord = record
			case BOOLEAN of
			  TRUE : ( BITS: packed array [0..IntSize-1] of 0..1 );
			  FALSE : ( INT : INTEGER )
		      end;
var
	Fun : FunnyRecord;

....

Fun.INT := 17;

Writing out Fun.BITS gives the bit representation of Fun.INT -- or does it?
I've seen some Pascal compilers give some strange data here.


1.	Although I can't find it in Wirth and Jansen (ISO STANDARD), is there 
a guarantee that this will give the bit implementation of the integer?  


2.	It works great on VAX Pascal for all appropriate data types.  I can 
also get POINTER values this way -- all in ISO STANDARD:

type Pointer = record
		case BOOLEAN of 
			TRUE : (INT : INTEGER);
			FALSE : (P : ^INTEGER)
                end;
var PTR : Pointer;
....
NEW(PTR.P);
WRITE(PTR.INT)  {writes the pointer (or address offset) value}

Are all the texts that say that one can't write out the value of a pointer in
standard Pascal in error?


2.	Besides BitTwiddling and making interesting demonstrations to students,
are there other uses for untagged variant records?


3.	Virtually every Pascal text WARNS against using these records.  It
almost seems like an ERROR on Wirth's part or the ISO Standard's committee
part.  Was it a boo boo that became "popular" (like CREAT in Uniques)?


4.	I first saw some of this stuff in the Journal of Pascal, Ada and
Modula-2 (JPAM).  I haven't received my JPAM in almost a year - and I had about
a year to go on my subscription and have received no renewal notice.  Does
anyone know if it still exists?

George......

--------------------------------------------------------------------------
-- George C. Harrison                 INTERNET: g_harrison@vger.nsu.EDU --
-- Professor of Computer Science               Subliminal Address:  adA --
-- Norfolk State University                                             --
--   The views expressed here are not necessarily those of              --
--   the Univesity, my wife, or my children.                            --
--------------------------------------------------------------------------

reagan@hiyall.enet.dec.com (John R. Reagan) (11/13/90)

In article <225.273b14fa@vger.nsu.edu>, g_harrison@vger.nsu.edu writes...
> 
>1.	Although I can't find it in Wirth and Jansen (ISO STANDARD), is there 
>a guarantee that this will give the bit implementation of the integer?  

First of all J&W is not the same as the ISO 7185 unextended Pascal
standard.  Several things are different (procedural parameters, conformant
arrays, etc.)  Second, the standard does not guarantee this functionality.
You have an invalid program.  It tends to work for most compilers due to
popular allocation schemes to save on memory and the fact that detecting
the use of undefined variants is expensive to implement.  Since most compilers
don't check for this error (including VAX Pascal), you get lucky most of the
time.  The standard does not say how a compiler should allocate fields
in a variant record.  There are cases in VAX Pascal where variant parts
do not overlap (those which contain file variables).  The standard is
even flexible enough for a compiler to not allocate fields that are
never referenced (if it can determine such a fact).

Untagged variants have much the same uses as tagged variants.  They can
describe "generic" records which can mutate forms (variants allocated
with the NEW(p,c1,...cn) form cannot mutate) during the program execution.

---
John Reagan, VAX Pascal Project Leader and X3J9 Secretary
Digital Equipment Corporation
reagan@hiyall.enet.dec.com
Disclaimer:  The opinions and statements expressed by me are not
             necessarily those of Digital Equipment Corporation.
---