Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 336 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variadic function in Ada (C - Ada binding)?

#1
I am working on a project which uses **C - Ada language binding**. A function in C will call a function in Ada side. I want to make a variadic function in Ada which can receive a variable number of arguments sent from the C function. I also wanted to send different types of args at the same time like int, char, enums, etc at the same time. Is it possible to have this type of mechanism?
Reply

#2
You cannot create a variadic function in Ada. You can simulate a variadic function is a couple of ways.

1. Ada allows you to specify default values for functions and procedures. One need not always specify the values of the default parameters if you want to use the default values.
2. You can define one or more of the parameters as a variant record.
Reply

#3
You can use 'address or the package System.Address_To_Access_Conversions and 'access (or 'unchecked_access) to generate addresses of each item you want to pass.

type Address_Array is array (positive range <>) of System.address;
function C_Caller(Params : Address_Array) return Integer is begin return 0; end;
X, Y, Z, Result : Integer;
begin
result := C_Caller(Address_Array'(x'address, y'address, z'address));

...then you'll need to pragma import the actual function.
Reply

#4
**It is not possible** to call any C variadic function from Ada in a portable way!

One of the reason - some ABIs use special ways/registers to pass float values. This mean C compiler will use this registers, due to it's unknown in advance whether argument is float or not. Ada compiler will not use this registers (since you can't put float parameter in Ada wrapper function declaration). As result you will get crash, stack corruption or any other undefined behavior.

Particularly [AMD64 ABI][1] specifies:
> %rax - with variable arguments passes information
> about the number of vector registers used
>
> %xmm0–%xmm1 - used to pass and return floating
> point arguments

The only portable solution is using C wrapper with fixed number of parameters, then bind it as usual.


[1]:
Reply

#5
The forthcoming Ada standard Ada 202x is planning to provide support for calling C variadic functions.

You would then be able to write;
<!-- language: ada-lang -->

package C renames Interfaces.C;

procedure Printf (Format : in C.char_array)
with Import => True, Convention => C_Variadic_1, External_Name => "printf";
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through