[m-rev.] for discussion: undefined behaviours in int.m

Mark Brown mark at mercurylang.org
Fri Oct 21 17:17:17 AEDT 2016


On Fri, Oct 21, 2016 at 11:06 AM, Julien Fischer <jfischer at opturion.com> wrote:
> On Fri, 21 Oct 2016, Mark Brown wrote:
>> I'd like to see this in the Mercury standard library, even if literals
>> aren't immediately available.
>
> I assume that's in addition to the 'uint' (i.e. word sized unsigned
> integer) type, rather than instead of?

Yes, in addition to.

On Fri, Oct 21, 2016 at 3:21 PM, Julien Fischer <jfischer at opturion.com> wrote:
>
> Hi Zoltan,
>
> On Fri, 21 Oct 2016, Zoltan Somogyi wrote:
>
>> On Fri, 21 Oct 2016 11:06:43 +1100 (AEDT), Julien Fischer
>> <jfischer at opturion.com> wrote:
>>>
>>> It depends on what you (and indeed the compiler) are doing.  Defining
>>> a constant via a function call would be undesirable in speed critical
>>> code.

Ok, the function I gave is unlikely to be inlined.

>>
>>
>> That is correct if the function call actually gets executed at runtime.
>> However, it is possible to add code to compiler to guarantee its
>> evaluation
>> at compile time. We already do this in (some of) the code generators
>> for operations such as multiplications by constants, and during the
>> simplification pass at the end of semantic analysis for operations
>> such as string.format.
>
>
> Of course, my point is that if you define these types as solely as
> library types (as now) then that can't happen.  You require some
> compiler / language support.
>

Backend compilers are likely to be able to statically evaluate expressions like

    (123 * 1000 + 456) * 1000 + 789

to get the number 123456789. Can we get enough inlining of the library
functions to generate code like this, or at least something that
backend compilers can statically evaluate? For example, this program:

    main(!IO) :-
        write_line(k(123,456,789), !IO).

    :- func k(int, int, int) = int.
    k(A, B, C) = (A * 1000 + B) * 1000 + C.

compiles to the following assembler (x86_64, gcc 5.2.1, hlc.gc):

    movl    $123456789, %esi
    movl    $mercury__builtin__builtin__type_ctor_info_int_0, %edi
    jmp     mercury__io__write_line_3_p_0

so the answer to my question is yes in this case. How about other platforms?

Mark


More information about the reviews mailing list