Created by: eliotmoss
We have encountered an interesting issue in developing the C client, namely how to deal with union types. Our thought was to define a separate struct type for each union variant, and then to cast to the appropriate struct type when accessing a particular variant. (Note that this requires structs to be heap or alloca allocated, which I think is ok -- C does not treat them as single values that can go into a register, etc., as I recall.)
The problem we have is that because Mu defines the detailed layout of a struct on a given target, we cannot determine the sizes of the structs, and thus we cannot determine the maximum size, something we need in order to allocate an instance of a union type.
We observe that Mu gives no way to as the size of a type (or to get the offset of a field in a struct or an element of an array). While such information may not be used for typical accesses, we now see that it has at least one important use case. Given that C programs are typical way-ahead-of-time compiled, we do not consider it appropriate to generate Mu for C code only at the last minute.
We suggest that Mu provide means to determine sizes and perhaps to do simple load-time (if that is the right word) computations over these constants. Here is some possible syntax (admitting that I have not thought about it long or deeply yet):
.sizeof name type
Define name to be the constant that is the number of bytes needed for type.
.sizeof name op t1 t2 ... tn
Define name to be the sizes of t1 through tn combined with operator op, where op can be at least max and sum.
Alternatively, we could define names for the sizes of each type, and a more general constant-computing form:
.define name op e1 ... en
This would define name to be op applied to the ei. We could provide a suitable range of operators.
For offsets we could have:
.offset name struct or array type idx
This would define name to be the constant giving the offset of the idx'th field/element of the given struct or array type.
The point is to allow target-dependent computations over constants to be written in a target independent (symbolic) way. I believe this would meet the needs of C.