Newsgroups: | comp.compilers |
From: | Douglas M. Pase <pase@orville.nas.nasa.gov> |
In-Reply-To: | <9009051416.AA29612@uvacs.cs.Virginia.EDU> |
Keywords: | design, polymorphism |
Organization: | NASA Ames Research Center, Moffett Field, CA |
References: | <9008310419.AA06194@karakorum.berkeley.edu> <4c98dcd0b.001a92c@caen.engin.umich.edu> |
Date: | Thu, 6 Sep 90 21:38:23 -0700 |
In article <4c98dcd0b.001a92c@caen.engin.umich.edu> plph@caen.engin.umich.edu (Mark Montague) writes:
>Are the square braces in C polymorphic or overloaded? Which term should be
>used?
>[In the C compilers I've seen subscripts are turned into the intermediate code
>equivalent of *(a+b) very early in the compiler, typically as the intermediate
>form is being built. -John]
Yes, well, it is important to note that a[b] => *(a+b). So what is the type
signature of this function? You can look at it several ways. In the first
case it depends on the type of a. (It also depends on whether a[b] occurs on
the rhs or lhs of an assignment, but I will ignore that additional
complication.) Given the address that a represents, the amount added to that
address will depend on the size of its elements. For example, in a byte
oriented 32-bit machine,
char a[MAX];
... a[b] ...
gives something like
LDW,r6 b ; load the contents of b
ADD,r6 r6,a ; add the address a
LDB,r7 r6 ; load the byte contents of address in r6
but
int a[MAX];
... a[b] ...
gives
LDW,r6 b ; load the contents of b
MUL,6 r6,4 ; multiply b by the size of an int
ADD,r6 r6,a ; add the address a
LDW,r7 r6 ; load an int located at the byte address in reg 7
Judging from this, it seems that the type signatures are
[] : array of char x int -> char
: array of int x int -> int
...
This seems to suggest that [] is overloaded, and represents an infinite family
of functions, all differing in the size of the elements of a. Because []
seems to fit the definition, one could say [] is overloaded. It's hard to
believe, though, that much useful information is gained by looking at it in
this way.
On the other hand, it might also be said that it is universal polymorphic.
Its type signature would be
[] : array of *a x int -> *a
Looking at it in this way assumes either that it is a primitive polymorphic
function, and is therefore polymorphic almost by definition, or that it is
constructed from functions which are. That means loads, sizeofs, etc., must
be universal polymorphic. The use of the "+" function doesn't give us any
problem as long as only one definition of "+" is used, namely
+ : byte_address x int -> byte_address
If the "+" somehow depended on the type of "a", [] wouldn't be universal
polymorphic. It would be ad hoc polymorphic (i.e., overloaded). Assuming
that the above mentioned restrictions are met, it seems much simpler and
more useful to view [] as universal polymorphic.
--
Dr. Douglas M. Pase, Computer Sciences Corporation
NASA Ames Research Center MS 258-6, Moffett Field, CA 94035
(415) 604-6394, pase@orville.nas.nasa.gov
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.