[m-rev.] for review: encode invariants in types when creating constant structures

Zoltan Somogyi zoltan.somogyi at runbox.com
Thu Mar 29 12:40:35 AEDT 2018



On Wed, 14 Mar 2018 11:48:36 +1100, Peter Wang <novalazy at gmail.com> wrote:
> For
> 
>     :- type data
> 	--->    data(int, float, float, int).
> 
>     Data = data(1, 2.2, 3.3, 4),
> 
> we currently generate in hlc grades:
> 
>     #ifdef MR_MSVC
>     #pragma pack(push, MR_BYTES_PER_WORD)
>     #endif
>     struct constdata_scalar_cell_group_1 {
>       MR_Box f1;
>       MR_Float_Aligned f2;
>       MR_Float_Aligned f3;
>       MR_Box f4;
>     };
>     #ifdef MR_MSVC
>     #pragma pack(pop)
>     #endif
> 
>     static /* final */ const struct constdata_scalar_cell_group_1 constdata_scalar_common_1[1] = {
>       /* row 0 */
>       {
> 	((MR_Box) ((MR_Integer) 1)),
> 	(MR_Float) 2.2000000000000002,
> 	(MR_Float) 3.2999999999999998,
> 	((MR_Box) ((MR_Integer) 4))
>       },
>     };
> 
> This diff would and does cause the compiler to abort instead.

You are right.

I have modified the code to leave double word arguments alone,
and added a long comment explaining why it does that.
See the attached updated diff.

With this change, the compiler generates the same .c code
in hlc.gc as the old compiler.

Peter, could you please have a look at the updated diff
and see if it is ok? The only part that has changed
is the deletion of the abort. I apologize for the delay,
I have been sick for the past two weeks.

BTW, I have an unrelated observation about the above code:
the .m file says "3.3", while the .c file says "3.2999999999999998".
This shows that float constants get to go through decimal to binary
conversion *twice*, when once should be enough, and would yield
more accurate results. Maybe we should consider a lossless
representation, such as strings, for floats in the lexer. We could
change the representation of the float_const cons_id in prog_data.m
from float_const(float) to float_const(float, maybe(string)).
The maybe would be yes(...) for constants read in, and "no"
for constants computed by the compiler, but the code that
generates C would use the string version if present.

Zoltan.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: DIFF.const_triples2
Type: application/octet-stream
Size: 30153 bytes
Desc: not available
URL: <http://lists.mercurylang.org/archives/reviews/attachments/20180329/846a0701/attachment-0001.obj>


More information about the reviews mailing list