How to model a C function pointer?

If I model it as ptr I can’t make indirect call off it. The only way I can
think of is to create a C function call_ptr(ptr, …) that wraps around the
function pointer and does the actual call. I can then pass the function
pointer to the call_ptr function as a regular ptr. This is in the context
of DLL calling.

I was referring to a function pointer such as “int (*fptr) ()” that can be
returned by a C function such as dlsym (DLL symbol look up). Once I have
the pointer I would then like to call it. In C I can simply do “fptr();”. I
assume I can’t do something like that in ATS? I wrapped the indirect call
in C and pass the pointer around in ATS using the opaque ptr type and that
works.On Thursday, January 23, 2014 5:36:03 PM UTC-8, gmhwxi wrote:

Not sure what you need precisely.

Say you have

int foo (char *) ;

Then you can have

fun foo (string): int

in ATS.

On Thursday, January 23, 2014 7:34:08 PM UTC-5, H Zhang wrote:

If I model it as ptr I can’t make indirect call off it. The only way I
can think of is to create a C function call_ptr(ptr, …) that wraps around
the function pointer and does the actual call. I can then pass the function
pointer to the call_ptr function as a regular ptr. This is in the context
of DLL calling.

That is really cool! Thanks!On Thursday, January 23, 2014 6:36:55 PM UTC-8, gmhwxi wrote:

By the way, dlsym is available in ${PATSHOME}/libc/SATS/dlfcn.sats.

On Thursday, January 23, 2014 9:26:24 PM UTC-5, gmhwxi wrote:

It should be:

let
val fp = dlsym (…)
typedef fp_t = () → int
val () = assertloc (fp > 0)
val fp = $UN.cast{fp_t}(fp)
val res = fp ()
in
,…
end

On Thursday, January 23, 2014 9:24:14 PM UTC-5, gmhwxi wrote:

Then you can do something like:

let
val fp = dlsym (…)
typedef fp_t = () → int
val fp = $UN.cast{fp_t}(fp)
val res = fp ()
in
,…
end

Thanks to ATS and C sharing the same run-time semantics :slight_smile:

On Thursday, January 23, 2014 9:16:09 PM UTC-5, H Zhang wrote:

I was referring to a function pointer such as “int (*fptr) ()” that can
be returned by a C function such as dlsym (DLL symbol look up). Once I have
the pointer I would then like to call it. In C I can simply do “fptr();”. I
assume I can’t do something like that in ATS? I wrapped the indirect call
in C and pass the pointer around in ATS using the opaque ptr type and that
works.

On Thursday, January 23, 2014 5:36:03 PM UTC-8, gmhwxi wrote:

Not sure what you need precisely.

Say you have

int foo (char *) ;

Then you can have

fun foo (string): int

in ATS.

On Thursday, January 23, 2014 7:34:08 PM UTC-5, H Zhang wrote:

If I model it as ptr I can’t make indirect call off it. The only way
I can think of is to create a C function call_ptr(ptr, …) that wraps
around the function pointer and does the actual call. I can then pass the
function pointer to the call_ptr function as a regular ptr. This is in the
context of DLL calling.

It should be:

let
val fp = dlsym (…)
typedef fp_t = () → int
val () = assertloc (fp > 0)
val fp = $UN.cast{fp_t}(fp)
val res = fp ()
in
,…
endOn Thursday, January 23, 2014 9:24:14 PM UTC-5, gmhwxi wrote:

Then you can do something like:

let
val fp = dlsym (…)
typedef fp_t = () → int
val fp = $UN.cast{fp_t}(fp)
val res = fp ()
in
,…
end

Thanks to ATS and C sharing the same run-time semantics :slight_smile:

On Thursday, January 23, 2014 9:16:09 PM UTC-5, H Zhang wrote:

I was referring to a function pointer such as “int (*fptr) ()” that can
be returned by a C function such as dlsym (DLL symbol look up). Once I have
the pointer I would then like to call it. In C I can simply do “fptr();”. I
assume I can’t do something like that in ATS? I wrapped the indirect call
in C and pass the pointer around in ATS using the opaque ptr type and that
works.

On Thursday, January 23, 2014 5:36:03 PM UTC-8, gmhwxi wrote:

Not sure what you need precisely.

Say you have

int foo (char *) ;

Then you can have

fun foo (string): int

in ATS.

On Thursday, January 23, 2014 7:34:08 PM UTC-5, H Zhang wrote:

If I model it as ptr I can’t make indirect call off it. The only way I
can think of is to create a C function call_ptr(ptr, …) that wraps around
the function pointer and does the actual call. I can then pass the function
pointer to the call_ptr function as a regular ptr. This is in the context
of DLL calling.

Not sure what you need precisely.

Say you have

int foo (char *) ;

Then you can have

fun foo (string): int

in ATS.On Thursday, January 23, 2014 7:34:08 PM UTC-5, H Zhang wrote:

If I model it as ptr I can’t make indirect call off it. The only way I can
think of is to create a C function call_ptr(ptr, …) that wraps around the
function pointer and does the actual call. I can then pass the function
pointer to the call_ptr function as a regular ptr. This is in the context
of DLL calling.

By the way, dlsym is available in ${PATSHOME}/libc/SATS/dlfcn.sats.On Thursday, January 23, 2014 9:26:24 PM UTC-5, gmhwxi wrote:

It should be:

let
val fp = dlsym (…)
typedef fp_t = () → int
val () = assertloc (fp > 0)
val fp = $UN.cast{fp_t}(fp)
val res = fp ()
in
,…
end

On Thursday, January 23, 2014 9:24:14 PM UTC-5, gmhwxi wrote:

Then you can do something like:

let
val fp = dlsym (…)
typedef fp_t = () → int
val fp = $UN.cast{fp_t}(fp)
val res = fp ()
in
,…
end

Thanks to ATS and C sharing the same run-time semantics :slight_smile:

On Thursday, January 23, 2014 9:16:09 PM UTC-5, H Zhang wrote:

I was referring to a function pointer such as “int (*fptr) ()” that can
be returned by a C function such as dlsym (DLL symbol look up). Once I have
the pointer I would then like to call it. In C I can simply do “fptr();”. I
assume I can’t do something like that in ATS? I wrapped the indirect call
in C and pass the pointer around in ATS using the opaque ptr type and that
works.

On Thursday, January 23, 2014 5:36:03 PM UTC-8, gmhwxi wrote:

Not sure what you need precisely.

Say you have

int foo (char *) ;

Then you can have

fun foo (string): int

in ATS.

On Thursday, January 23, 2014 7:34:08 PM UTC-5, H Zhang wrote:

If I model it as ptr I can’t make indirect call off it. The only way I
can think of is to create a C function call_ptr(ptr, …) that wraps around
the function pointer and does the actual call. I can then pass the function
pointer to the call_ptr function as a regular ptr. This is in the context
of DLL calling.

Then you can do something like:

let
val fp = dlsym (…)
typedef fp_t = () → int
val fp = $UN.cast{fp_t}(fp)
val res = fp ()
in
,…
end

Thanks to ATS and C sharing the same run-time semantics :)On Thursday, January 23, 2014 9:16:09 PM UTC-5, H Zhang wrote:

I was referring to a function pointer such as “int (*fptr) ()” that can be
returned by a C function such as dlsym (DLL symbol look up). Once I have
the pointer I would then like to call it. In C I can simply do “fptr();”. I
assume I can’t do something like that in ATS? I wrapped the indirect call
in C and pass the pointer around in ATS using the opaque ptr type and that
works.

On Thursday, January 23, 2014 5:36:03 PM UTC-8, gmhwxi wrote:

Not sure what you need precisely.

Say you have

int foo (char *) ;

Then you can have

fun foo (string): int

in ATS.

On Thursday, January 23, 2014 7:34:08 PM UTC-5, H Zhang wrote:

If I model it as ptr I can’t make indirect call off it. The only way I
can think of is to create a C function call_ptr(ptr, …) that wraps around
the function pointer and does the actual call. I can then pass the function
pointer to the call_ptr function as a regular ptr. This is in the context
of DLL calling.