[m-users.] Float array initialisation

Julien Fischer jfischer at opturion.com
Thu May 6 01:23:36 AEST 2021


Hi Fabrice,

On Wed, 5 May 2021, Fabrice Nicol wrote:

> Aaah OK (light bulb icon). 
> So passing T to the C FFI is not quite the same as passing array(T) or
> list(T), *despite the fact* that all of them are actually passed to C
> as MR_Word (which is what misled me). 

That's not quite right either.  The rules for this are in the "C data
passing conventions" section of the reference manual.

array(T) is passed to C foreig_procs as MR_ArrayPtr. That is because it
is defined using a foreign_type pragma, namely:

     :- pragma foreign_type("C", array(T), "MR_ArrayPtr")
        where equality is array.array_equal,
        comparison is array.array_compare.

(Other Mercury developers: why does the above definition not have
'can_pass_as_mercury_type' attribute?)

T and list(T), not being primitive types, tuples or defined using
foreign_type pragmas are covered by this:

      Mercury variables of any other type are passed as a MR_Word

> underlying boxing operation that is managed by the compiler (if I
> understood you well),

The compiler will emit code to box floats / int64s /
uint64s at the point those values are created (if required).

> and in the T structure case you are on you own with the macros. 

Mercury's data representation is essentially untyped at the C level.
That's why we recommend minimising the amount of manipulation of Mercury
values that you do in foreign_procs.  (Having written quite a lot of
bindings to C and C++ libraries in my time, I realise that's not always
an option however ... so you may just have to tread carefully.)

Julien.


More information about the users mailing list