[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: RFC: Support non-standard extension (call via casted function pointer)



On 26.01.2016 10:42, Andreas Schwab wrote:
> Michael Karcher <debian@mkarcher.dialup.fu-berlin.de> writes:
>> ghc does not have the information yet when it writes the
>> declaration.
> It knows it when it calls the function, so that's a lame excuse.

The excuse is not as lame as it sounds at the first sight. In Cmm, the
following code is legal:

#include "Cmm.h"

import rand;
import malloc;

foo(W_ flag)
{
    W_ i;
    P_ fun, p;
    if(flag != 0) {
        fun = rand;
    } else {
        fun = malloc;
    }
    if(flag != 0) {
        ("ptr" p) = foreign "C" fun(100);
    } else {
        (i) = foreign "C" fun();
    }
}

The (untyped) pointer variable "fun" can take the address of malloc
(returning a pointer) or rand (returning an integer). Only at call time
you have to specify the return type of the native function pointed to by
"fun" (by including or not including "ptr"). There is no generic way for
the Cmm-to-C compiler to find out the correct type for rand or malloc
either at import time, nor at use time (when assigning the address to
"fun").

Regards,
  Michael Karcher


Reply to: