Page 1 of 1

Ada and C

Posted: Wed Feb 09, 2011 1:50 am
by kriscrump
Hello OSDev,
I've been working on a basic kernel in Ada. Things are coming along quite fine, except that right now I'm stuck at one line of code.

Here's the C code:

Code: Select all

gdt_ptr.base = (uint32_t) &gdt_entries;
I've been searching for a way to implement this in Ada and haven't had much luck. ;/

gdt_ptr.base is defined as an unsigned 32. I've mimicked this in Ada as well with using Unsigned_32, but I'm at a loss as how to set the variable to a reference to the array of gdt_entries. I come from a heavy C/C++ background, so the way that Ada does pointers and references and such is really giving me a hard time.

Any ideas or suggestions would be very much appreciated! ;)

Regards,
~Kris

Re: Ada and C

Posted: Wed Feb 09, 2011 2:16 am
by Combuster
You shouldn't use a language in which you are not an expert for OS development - and this is why.

The following showed up in a google search:
Yes it does. There are standard packages System.Storage_Elements and
System.Address_To_Access_Conversions, which provide facilities for
converting between access types and addresses, and performing integer
arithmetic on addresses, as well as Interfaces.C and
Interfaces.C.Pointers which enable Ada programs to access C strings,
arrays, and pointers.
I haven't ever tried Ada, but this looks like you need some form of runtime library to do such conversions for you. Do you know how to implement one, or do you know where that information can be found? If not, this is a good moment to stop to prevent further frustration.

Re: Ada and C

Posted: Wed Feb 09, 2011 10:02 am
by Kevin
Combuster wrote:You shouldn't use a language in which you are not an expert for OS development - and this is why.
To be fair, outside OS developement you wouldn't normally deal with addresses in Ada. The 'access' types it has are more like references.

Re: Ada and C

Posted: Wed Feb 09, 2011 11:37 am
by kriscrump
Combuster - I completely agree with you.
The thing about Ada is that packages like that are part of the language itself, and don't require a run-time most times.

using Ada's pragma No_Run_Time, and other pragma Restrictions; actually takes care of letting you know at compile time what is and isn't possible, as well as telling the compiler to remove/not use the run time;

I did find a solution after having looked into the different 'Address and 'Access things.

Code: Select all

procedure Initialize is
        function To_Unsigned_32 is
                new Ada.Unchecked_Conversion (Source => System.Address,
                                              Target => Unsigned_32);
begin
Doing it like this now allows me to do:

Code: Select all

gdt_ptr.Base := To_Unsigned_32 (GDT_Entry (0)'Address);
I know this might not be best Ada practices in the code, but it does in fact hold the Address to the gdt entries now. ;/ I'm open to any other suggestions not utilizing unchecked_conversion if there are any.