On 12/07/2012 08:48 AM, BartC wrote:
>
>
> "James Kuyper" <
james...@verizon.net> wrote in message
> news:k9sqi4$do2$1...@dont-email.me...
>> On 12/07/2012 07:54 AM, BartC wrote:
>> ...
>>> Look, I have this compiler project from a couple of months back. That
>>> language also doesn't have lvalue casts (it wasn't too hot on casts, but
>>> it
>>> *does* have the 'equivalence' feature which C doesn't have, which is what
>>> is
>>> used instead, and is better IMO).
>>
>> C does have a feature with the same semantics as your "equivalence"
>> feature, just different syntax. That feature is called a union.
>
> Unions are much more limited. For example, if you have:
>
> int A[25];
> double X;
>
> you can't 'equivalence' X to A[16] without some difficulty. (Actually X
> might span both A[16] and A[17].)
Your earlier description of how "equivalence" works in your language
didn't mention that it could be used in that fashion. Yes, that would be
a bit harder to do in C. If _Alignof(int) != _Alignof(double), make sure
that a[16] is correctly aligned to be used as a place to store a double
could, in principle, be problematic. The fact that you chose a power of
2 as a subscript makes this unlikely to be an issue on most machines,
but I presume your language also allows X to be equivalenced with A[17]?
If there was only one 'double' object equivalenced to A[17], you could
adjust the location of A to make sure that A[17] was correctly aligned
for a double. However, that approach wouldn't work if multiple
incompatible equivalences were specified. How does your language deal
with that? Can it only be implemented on platforms with no alignment
restrictions?
> A might also be external, limiting the options further.
In C, if A were external, code compiled to work with A could be
optimized to assume that no pointer to double ever aliases any part of
A. The union approach removes permission to make such optimizations, at
least for code within the scope of the union, but if the declaration of
A is outside your control, that's not an option.
Allowing anything in C that's similar to your language's "equivalence"
would require disabling such optimizations. Allowing it for arbitrary
types would require disabling all such optimizations; I presume that
such optimizations are prohibited in your language? Or perhaps code
which would interact badly with such optimizations is prohibited? That
would be the equivalent of restrict-qualifying most pointer declarations
in C.
> It's also harder to refer to A and X entirely independently; you might need
> to use U.A and U.X (if you can even get that far).
>
> (BTW 'equivalence' is a (now-deprecated) feature of Fortran. I thought
> people might be familiar with it. My version just uses @, for example:
> double X @ A[16]; )
Its been a couple of decades since the last time I wrote much Fortran
code, and I don't think I ever used that feature, but I was aware of it.
If it has the same capability you describe above, I didn't remember that
fact, which isn't particularly surprising.