Next: , Previous: Running gnatbind, Up: Binding Using gnatbind

4.2 Generating the Binder Program in C

In most normal usage, the default mode of gnatbind which is to generate the main package in Ada, as described in the previous section. In particular, this means that any Ada programmer can read and understand the generated main program. It can also be debugged just like any other Ada code provided the -g switch is used for gnatbind and gnatlink.

However for some purposes it may be convenient to generate the main program in C rather than Ada. This may for example be helpful when you are generating a mixed language program with the main program in C. The GNAT compiler itself is an example. The use of the -C switch for both gnatbind and gnatlink will cause the program to be generated in C (and compiled using the gnu C compiler). The following shows the C code generated for the same "Hello World" program:

     #ifdef __STDC__
     #define PARAMS(paramlist) paramlist
     #define PARAMS(paramlist) ()
     extern void __gnat_set_globals
      PARAMS ((int, int, int, int, int, int,
               void (*) PARAMS ((void)), int, int));
     extern void adafinal PARAMS ((void));
     extern void adainit PARAMS ((void));
     extern void system__standard_library__adafinal PARAMS ((void));
     extern int main PARAMS ((int, char **, char **));
     extern void exit PARAMS ((int));
     extern void __gnat_break_start PARAMS ((void));
     extern void _ada_hello PARAMS ((void));
     extern void __gnat_initialize PARAMS ((void));
     extern void __gnat_finalize PARAMS ((void));
     extern void ada__exceptions___elabs PARAMS ((void));
     extern void system__exceptions___elabs PARAMS ((void));
     extern void interfaces__c_streams___elabs PARAMS ((void));
     extern void system__exception_table___elabb PARAMS ((void));
     extern void ada__io_exceptions___elabs PARAMS ((void));
     extern void system__stack_checking___elabs PARAMS ((void));
     extern void system__soft_links___elabb PARAMS ((void));
     extern void system__secondary_stack___elabb PARAMS ((void));
     extern void ada__tags___elabs PARAMS ((void));
     extern void ada__tags___elabb PARAMS ((void));
     extern void ada__streams___elabs PARAMS ((void));
     extern void system__finalization_root___elabs PARAMS ((void));
     extern void ada__exceptions___elabb PARAMS ((void));
     extern void system__finalization_implementation___elabs PARAMS ((void));
     extern void system__finalization_implementation___elabb PARAMS ((void));
     extern void ada__finalization___elabs PARAMS ((void));
     extern void ada__finalization__list_controller___elabs PARAMS ((void));
     extern void system__file_control_block___elabs PARAMS ((void));
     extern void system__file_io___elabb PARAMS ((void));
     extern void ada__text_io___elabs PARAMS ((void));
     extern void ada__text_io___elabb PARAMS ((void));
     extern int __gnat_inside_elab_final_code;
     extern int gnat_argc;
     extern char **gnat_argv;
     extern char **gnat_envp;
     extern int gnat_exit_status;
     char __gnat_version[] = "GNAT Version: 3.15w (20010315)";
     void adafinal () {
        system__standard_library__adafinal ();
     void adainit ()
        extern char ada__exceptions_E;
        extern char system__exceptions_E;
        extern char interfaces__c_streams_E;
        extern char system__exception_table_E;
        extern char ada__io_exceptions_E;
        extern char system__secondary_stack_E;
        extern char system__stack_checking_E;
        extern char system__soft_links_E;
        extern char ada__tags_E;
        extern char ada__streams_E;
        extern char system__finalization_root_E;
        extern char system__finalization_implementation_E;
        extern char ada__finalization_E;
        extern char ada__finalization__list_controller_E;
        extern char system__file_control_block_E;
        extern char system__file_io_E;
        extern char ada__text_io_E;
        extern void *__gnat_hello__SDP;
        extern void *__gnat_ada__text_io__SDP;
        extern void *__gnat_ada__exceptions__SDP;
        extern void *__gnat_gnat__heap_sort_a__SDP;
        extern void *__gnat_system__exception_table__SDP;
        extern void *__gnat_system__machine_state_operations__SDP;
        extern void *__gnat_system__secondary_stack__SDP;
        extern void *__gnat_system__parameters__SDP;
        extern void *__gnat_system__soft_links__SDP;
        extern void *__gnat_system__stack_checking__SDP;
        extern void *__gnat_system__traceback__SDP;
        extern void *__gnat_ada__streams__SDP;
        extern void *__gnat_ada__tags__SDP;
        extern void *__gnat_system__string_ops__SDP;
        extern void *__gnat_interfaces__c_streams__SDP;
        extern void *__gnat_system__file_io__SDP;
        extern void *__gnat_ada__finalization__SDP;
        extern void *__gnat_system__finalization_root__SDP;
        extern void *__gnat_system__finalization_implementation__SDP;
        extern void *__gnat_system__string_ops_concat_3__SDP;
        extern void *__gnat_system__stream_attributes__SDP;
        extern void *__gnat_system__file_control_block__SDP;
        extern void *__gnat_ada__finalization__list_controller__SDP;
        void **st[23] = {
        extern void ada__exceptions___elabs ();
        extern void system__exceptions___elabs ();
        extern void interfaces__c_streams___elabs ();
        extern void system__exception_table___elabb ();
        extern void ada__io_exceptions___elabs ();
        extern void system__stack_checking___elabs ();
        extern void system__soft_links___elabb ();
        extern void system__secondary_stack___elabb ();
        extern void ada__tags___elabs ();
        extern void ada__tags___elabb ();
        extern void ada__streams___elabs ();
        extern void system__finalization_root___elabs ();
        extern void ada__exceptions___elabb ();
        extern void system__finalization_implementation___elabs ();
        extern void system__finalization_implementation___elabb ();
        extern void ada__finalization___elabs ();
        extern void ada__finalization__list_controller___elabs ();
        extern void system__file_control_block___elabs ();
        extern void system__file_io___elabb ();
        extern void ada__text_io___elabs ();
        extern void ada__text_io___elabb ();
        void (*ea[23]) () = {
        __gnat_SDP_Table_Build (&st, 23, ea, 23);
        __gnat_set_globals (
           -1,      /* Main_Priority              */
           -1,      /* Time_Slice_Value           */
           'b',     /* WC_Encoding                */
           ' ',     /* Locking_Policy             */
           ' ',     /* Queuing_Policy             */
           ' ',     /* Tasking_Dispatching_Policy */
           0,       /* Finalization routine address, not used anymore */
           0,       /* Unreserve_All_Interrupts */
           0);      /* Exception_Tracebacks */
        __gnat_inside_elab_final_code = 1;
        if (ada__exceptions_E == 0) {
           ada__exceptions___elabs ();
        if (system__exceptions_E == 0) {
           system__exceptions___elabs ();
        if (interfaces__c_streams_E == 0) {
           interfaces__c_streams___elabs ();
        interfaces__c_streams_E = 1;
        if (system__exception_table_E == 0) {
           system__exception_table___elabb ();
        if (ada__io_exceptions_E == 0) {
           ada__io_exceptions___elabs ();
        if (system__stack_checking_E == 0) {
           system__stack_checking___elabs ();
        if (system__soft_links_E == 0) {
           system__soft_links___elabb ();
        system__stack_checking_E = 1;
        if (system__secondary_stack_E == 0) {
           system__secondary_stack___elabb ();
        if (ada__tags_E == 0) {
           ada__tags___elabs ();
        if (ada__tags_E == 0) {
           ada__tags___elabb ();
        if (ada__streams_E == 0) {
           ada__streams___elabs ();
        if (system__finalization_root_E == 0) {
           system__finalization_root___elabs ();
        system__finalization_root_E = 1;
        if (ada__exceptions_E == 0) {
           ada__exceptions___elabb ();
        if (system__finalization_implementation_E == 0) {
           system__finalization_implementation___elabs ();
        if (system__finalization_implementation_E == 0) {
           system__finalization_implementation___elabb ();
        if (ada__finalization_E == 0) {
           ada__finalization___elabs ();
        ada__finalization_E = 1;
        if (ada__finalization__list_controller_E == 0) {
           ada__finalization__list_controller___elabs ();
        ada__finalization__list_controller_E = 1;
        if (system__file_control_block_E == 0) {
           system__file_control_block___elabs ();
        if (system__file_io_E == 0) {
           system__file_io___elabb ();
        if (ada__text_io_E == 0) {
           ada__text_io___elabs ();
        if (ada__text_io_E == 0) {
           ada__text_io___elabb ();
        __gnat_inside_elab_final_code = 0;
     int main (argc, argv, envp)
         int argc;
         char **argv;
         char **envp;
        gnat_argc = argc;
        gnat_argv = argv;
        gnat_envp = envp;
        __gnat_initialize ();
        adainit ();
        __gnat_break_start ();
        _ada_hello ();
        system__standard_library__adafinal ();
        __gnat_finalize ();
        exit (gnat_exit_status);
     unsigned helloB = 0x7880BEB3;
     unsigned system__standard_libraryB = 0x0D24CBD0;
     unsigned system__standard_libraryS = 0x3283DBEB;
     unsigned adaS = 0x2359F9ED;
     unsigned ada__text_ioB = 0x47C85FC4;
     unsigned ada__text_ioS = 0x496FE45C;
     unsigned ada__exceptionsB = 0x74F50187;
     unsigned ada__exceptionsS = 0x6736945B;
     unsigned gnatS = 0x156A40CF;
     unsigned gnat__heap_sort_aB = 0x033DABE0;
     unsigned gnat__heap_sort_aS = 0x6AB38FEA;
     unsigned systemS = 0x0331C6FE;
     unsigned system__exceptionsS = 0x20C9ECA4;
     unsigned system__exception_tableB = 0x68A22947;
     unsigned system__exception_tableS = 0x394BADD5;
     unsigned gnat__htableB = 0x08258E1B;
     unsigned gnat__htableS = 0x367D5222;
     unsigned system__machine_state_operationsB = 0x4F3B7492;
     unsigned system__machine_state_operationsS = 0x182F5CF4;
     unsigned system__storage_elementsB = 0x2F1EB794;
     unsigned system__storage_elementsS = 0x102C83C7;
     unsigned system__secondary_stackB = 0x1574B6E9;
     unsigned system__secondary_stackS = 0x708E260A;
     unsigned system__parametersB = 0x56D770CD;
     unsigned system__parametersS = 0x237E39BE;
     unsigned system__soft_linksB = 0x08AB6B2C;
     unsigned system__soft_linksS = 0x1E2491F3;
     unsigned system__stack_checkingB = 0x476457A0;
     unsigned system__stack_checkingS = 0x5299FCED;
     unsigned system__tracebackB = 0x2971EBDE;
     unsigned system__tracebackS = 0x2E9C3122;
     unsigned ada__streamsS = 0x7C25DE96;
     unsigned ada__tagsB = 0x39ADFFA2;
     unsigned ada__tagsS = 0x769A0464;
     unsigned system__string_opsB = 0x5EB646AB;
     unsigned system__string_opsS = 0x63CED018;
     unsigned interfacesS = 0x0357E00A;
     unsigned interfaces__c_streamsB = 0x3784FB72;
     unsigned interfaces__c_streamsS = 0x2E723019;
     unsigned system__file_ioB = 0x623358EA;
     unsigned system__file_ioS = 0x31F873E6;
     unsigned ada__finalizationB = 0x6843F68A;
     unsigned ada__finalizationS = 0x63305874;
     unsigned system__finalization_rootB = 0x31E56CE1;
     unsigned system__finalization_rootS = 0x23169EF3;
     unsigned system__finalization_implementationB = 0x6CCBA70E;
     unsigned system__finalization_implementationS = 0x604AA587;
     unsigned system__string_ops_concat_3B = 0x572E3F58;
     unsigned system__string_ops_concat_3S = 0x01F57876;
     unsigned system__stream_attributesB = 0x1D4F93E8;
     unsigned system__stream_attributesS = 0x30B2EC3D;
     unsigned ada__io_exceptionsS = 0x34054F96;
     unsigned system__unsigned_typesS = 0x7B9E7FE3;
     unsigned system__file_control_blockS = 0x2FF876A8;
     unsigned ada__finalization__list_controllerB = 0x5760634A;
     unsigned ada__finalization__list_controllerS = 0x5D851835;
     ada (spec)
     gnat (spec)
     gnat.heap_sort_a (spec)
     gnat.htable (spec)
     gnat.htable (body)
     interfaces (spec)
     system (spec)
     system.parameters (spec)
     system.standard_library (spec)
     ada.exceptions (spec)
     system.exceptions (spec)
     system.parameters (body)
     gnat.heap_sort_a (body)
     interfaces.c_streams (spec)
     interfaces.c_streams (body)
     system.exception_table (spec)
     system.exception_table (body)
     ada.io_exceptions (spec)
     system.storage_elements (spec)
     system.storage_elements (body)
     system.machine_state_operations (spec)
     system.machine_state_operations (body)
     system.secondary_stack (spec)
     system.stack_checking (spec)
     system.soft_links (spec)
     system.soft_links (body)
     system.stack_checking (body)
     system.secondary_stack (body)
     system.standard_library (body)
     system.string_ops (spec)
     system.string_ops (body)
     ada.tags (spec)
     ada.tags (body)
     ada.streams (spec)
     system.finalization_root (spec)
     system.finalization_root (body)
     system.string_ops_concat_3 (spec)
     system.string_ops_concat_3 (body)
     system.traceback (spec)
     system.traceback (body)
     ada.exceptions (body)
     system.unsigned_types (spec)
     system.stream_attributes (spec)
     system.stream_attributes (body)
     system.finalization_implementation (spec)
     system.finalization_implementation (body)
     ada.finalization (spec)
     ada.finalization (body)
     ada.finalization.list_controller (spec)
     ada.finalization.list_controller (body)
     system.file_control_block (spec)
     system.file_io (spec)
     system.file_io (body)
     ada.text_io (spec)
     ada.text_io (body)
     hello (body)
     /* BEGIN Object file/option list
        END Object file/option list */

Here again, the C code is exactly what is generated by the binder. The functions of the various parts of this code correspond in an obvious manner with the commented Ada code shown in the example in the previous section.