build_c_glue.c 23.6 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1
/* Buildsupport is (c) 2008-2016 European Space Agency
Maxime Perrotin's avatar
Maxime Perrotin committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * contact: maxime.perrotin@esa.int
 * License is LGPL, check LICENSE file */
/* 
 * build_c_glue.c
 * Generate vm_if.c/h and invoke_ri.c
 * (functions interfacing with the middleware)
 */


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <sys/stat.h>

#include "my_types.h"
#include "practical_functions.h"
#include "c_ast_construction.h"

Maxime Perrotin's avatar
Maxime Perrotin committed
22
23
24
static FILE *vm_if     = NULL,
            *invoke_ri = NULL,
            *vm_if_h   = NULL;
Maxime Perrotin's avatar
Maxime Perrotin committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

static int count_calling_threads = 0;

/* Adds header to vm_if files */
void c_preamble(FV * fv)
{
    int hasparam = 0;

    /* Check if any interface needs ASN.1 types */
    FOREACH(i, Interface, fv->interfaces, {
            CheckForAsn1Params(i, &hasparam);});

    fprintf(vm_if, "%s", do_not_modify_warning);

    fprintf(vm_if_h, "%s", do_not_modify_warning);

    fprintf(vm_if_h, "#ifndef VM_IF_%s\n", fv->name);
    fprintf(vm_if_h, "#define VM_IF_%s\n\n", fv->name);

    fprintf(vm_if, "#include <stdlib.h>\n" "#include <stdio.h>\n\n");

    fprintf(vm_if_h, "#ifdef __cplusplus\n"
            "extern \"C\" {\n" "#endif\n\n");

    fprintf(vm_if, "#include \"%s_vm_if.h\"\n\n", fv->name);

    if (c == fv->language || cpp == fv->language) {
        fprintf(vm_if, "#include \"%s.h\"\n\n", fv->name);
    }

    if (rtds == fv->language) {
        fprintf(vm_if, "#include \"glue_%s.h\"\n\n", fv->name);
    }

    if (hasparam) {
        fprintf(vm_if, "#include \"C_ASN1_Types.h\"\n\n");
        fprintf(vm_if_h, "#include \"C_ASN1_Types.h\"\n\n");

    }

    fprintf(vm_if, "void init_%s()\n{\n", fv->name);

    fprintf(vm_if_h,
            "/*\n * Function initialization:\n"
            " * Calls all dependent user (or GUI) startup code - including sychronous RI\n"
            "*/\n" "void init_%s();\n\n", fv->name);

    /* 
     * Define the main C initialization function
     * It calls the "startup()" user-code function only once
     * There is a protection (init static variable) that is needed
     * when using Po-Hi-C to make sure that the function is executed
     * only once. In Ada, it is not necessary because this function is
     * called only during the elaboration phase of the Ada wrapper, and
     * elaboration is done only once.
     * EDIT (MP 10/01/11): In Ada when using PO-HI-C we must call adainit()
     * which is generated by gnatmake in orchestration phase
     * EDIT (MP 08/02/11): Commented the if (USE_PO_HI_C) that prevented
     * the barrier from being generated with POHIAda. The init function is
     * not called during the elaboration of the Ada wrapper! It is called
     * at the thread startup.
     * EDIT (MP 19/11/12) Removed the barrier that prevented from calling
     * the init function of the remote FVs if the language was Ada.
     */
Maxime Perrotin's avatar
Maxime Perrotin committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    fprintf(vm_if, "    static int init = 0;\n\n"
                   "    if (!init) {\n"
                   "        init = 1;\n");

    /* Call the user-defined startup function (or GUI startup)
     * Except for Ada functions - that have startup in elaboration */
    if (ada != fv->language && qgenada != fv->language && qgenc != fv->language) {
        fprintf(vm_if, "        %s_startup();\n", fv->name);
    }

    /* Add a call to QGen init function, if it exists in generated code*/
    FOREACH(i, Interface, fv->interfaces, {
        if ((RI == i->direction) && (i->distant_qgen != NULL)) {
            if (NULL != i->distant_qgen->qgen_init)
                fprintf(vm_if, "        %s;\n", i->distant_qgen->qgen_init);
Maxime Perrotin's avatar
Maxime Perrotin committed
104
        }
Maxime Perrotin's avatar
Maxime Perrotin committed
105
    });
Maxime Perrotin's avatar
Maxime Perrotin committed
106

Maxime Perrotin's avatar
Maxime Perrotin committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    /*
     * If this function is a thread and we are using Po-Hi-C, we
     * must call the initialization function of all dependent
     * synchronous functions. Several threads can be connected
     * to a given sync function, but multiple execution of their
     * startup() function is prevented by the "init" barrier
     * (see just above).
     */
    /*
     * Edit (MP 27/06/11):
     * This must NOT be limited to threads - sync functions
     * must also call the init function of their offsprings.
     * (Auto-generated threads do not have any init_ function)
     */
    if (USE_PO_HI_C(fv)) {
Maxime Perrotin's avatar
Maxime Perrotin committed
122

Maxime Perrotin's avatar
Maxime Perrotin committed
123
124
125
126
127
128
129
130
131
132
        FOREACH(i, Interface, fv->interfaces, {
                if (RI == i->direction && synch == i->synchronism) {
                    fprintf(vm_if,
                        "        void init_%s();"
                        "        init_%s();\n",
                        i->distant_fv, 
                        i->distant_fv);}
                }
        );
    }
Maxime Perrotin's avatar
Maxime Perrotin committed
133

Maxime Perrotin's avatar
Maxime Perrotin committed
134
    fprintf(vm_if, "    }\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
135
136
137

    if (ada == fv->language || qgenada == fv->language) {
        if (USE_PO_HI_C(fv)) {
Maxime Perrotin's avatar
Maxime Perrotin committed
138
139
            fprintf(vm_if, "    /* Calling Ada initialization code */\n"
                           "    adainit();\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
140
141
142
143
144
145
146
147
148
149
150
151
152
            fprintf(vm_if_h, "extern void adainit();\n");
        }
    }

    fprintf(vm_if, "}\n\n");
}

/* Creates vm_if.c  */
int Init_C_Glue_Backend(FV * fv)
{
    char *path = NULL;
    char *filename = NULL;

Maxime Perrotin's avatar
Maxime Perrotin committed
153
    if (NULL != fv->system_ast->context->output) {
Maxime Perrotin's avatar
Maxime Perrotin committed
154
155
        build_string(&path, fv->system_ast->context->output,
                     strlen(fv->system_ast->context->output));
Maxime Perrotin's avatar
Maxime Perrotin committed
156
157
    }

Maxime Perrotin's avatar
Maxime Perrotin committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    build_string(&path, fv->name, strlen(fv->name));

    filename = make_string("%s_vm_if.h", fv->name);

    create_file(path, filename, &vm_if_h);
    filename[strlen(filename) - 1] = 'c';
    create_file(path, filename, &vm_if);


    free(path);
    assert(NULL != vm_if && NULL != vm_if_h);

    c_preamble(fv);

    return 0;
}


void close_c_gluefiles()
{
    close_file(&vm_if);
    if (NULL != vm_if_h) {
        fprintf(vm_if_h,
                "#ifdef __cplusplus\n" 
                "}\n"
                "#endif\n\n"
                "#endif\n");

    }
    close_file(&vm_if_h);
    close_file(&invoke_ri);
}

/* Add a Provided interface. Can contain in and out parameters */
void add_PI_to_c_vm_if(Interface * i)
{
    char *fcn_proto = NULL;
    bool comma = false;

    if (NULL == vm_if)
        return;

    /* a. function name */
    fcn_proto = make_string("void %s_%s (", i->parent_fv->name, i->name);

    /* b. for each IN param, add "void *pmy_inparam, size_t size_my_inparam" */
    FOREACH(p, Parameter, i->in, {
            fcn_proto = make_string("%s%svoid *pmy_%s, size_t size_my_%s",
                                    fcn_proto,
                                    (p != i->in->value) ? ", " : "",
                                    p->name, p->name);}
    );

    /* c. for each OUT param, add "void *pmy_outparam, size_t *size_my_outparam" */
    FOREACH(p, Parameter, i->out, {
Maxime Perrotin's avatar
Maxime Perrotin committed
213
214
215
216
217
218
        fcn_proto = make_string("%s%svoid *pmy_%s, size_t *psize_my_%s",
                                fcn_proto,
                                (p != i->out->value || (p == i->out->value
                                    && NULL != i->in)) ? ", " : "",
                                p->name,
                                p->name);}
Maxime Perrotin's avatar
Maxime Perrotin committed
219
220
221
222
223
224
    );

    fprintf(vm_if, "%s)\n{\n", fcn_proto);
    fprintf(vm_if_h, "%s);\n", fcn_proto);
    free(fcn_proto);

Maxime Perrotin's avatar
Maxime Perrotin committed
225
226
227
    /* d. For each IN param, declare a variable to put the DECODED data.
     *    Variable is static for all interface but unprotected ones.
     */
Maxime Perrotin's avatar
Maxime Perrotin committed
228
229
230

    if (NULL != i->in) {
        fprintf(vm_if,
Maxime Perrotin's avatar
Maxime Perrotin committed
231
                "    /* Decoded input variable(s): developer can use them */\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
232
233
234
    }

    FOREACH(p, Parameter, i->in, {
Maxime Perrotin's avatar
Maxime Perrotin committed
235
236
        fprintf(vm_if, "    %sasn1Scc%s IN_%s;\n",
                       unprotected != i->rcm? "static ": "",
Maxime Perrotin's avatar
Maxime Perrotin committed
237
238
239
                       p->type,
                       p->name);
        }
Maxime Perrotin's avatar
Maxime Perrotin committed
240
241
    );

Maxime Perrotin's avatar
Maxime Perrotin committed
242
243
244
    /* e. For each OUT param, declare a variable that the user can fill
     *    prior to encoding. Variable is static except for unprotected
     *    interfaces (that would lead to race conditions) */
Maxime Perrotin's avatar
Maxime Perrotin committed
245
246
    if (NULL != i->out) {
        fprintf(vm_if,
Maxime Perrotin's avatar
Maxime Perrotin committed
247
                "\n    /* Output variable(s): developer has to fill them */\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
248
249
250
    }

    FOREACH(p, Parameter, i->out, {
Maxime Perrotin's avatar
Maxime Perrotin committed
251
252
        fprintf(vm_if, "    %sasn1Scc%s OUT_%s;\n",
                       unprotected != i->rcm? "static ": "",
Maxime Perrotin's avatar
Maxime Perrotin committed
253
254
255
                       p->type,
                       p->name);
        }
Maxime Perrotin's avatar
Maxime Perrotin committed
256
257
    );

Maxime Perrotin's avatar
Maxime Perrotin committed
258
259
260
261
    /* f. In case of linux platform, memset to 0 all parameters
     *    !! This is only needed to avoid valgrind warnings !!
     *    Unnecessary for flight platform because it consumes CPU for nothing
    */
Maxime Perrotin's avatar
Maxime Perrotin committed
262
263
264
    if (NULL != i->in || NULL != i->out) {
        fprintf(vm_if, "\n#ifdef __unix__\n");
        FOREACH(p, Parameter, i->in, {
Maxime Perrotin's avatar
Maxime Perrotin committed
265
                fprintf(vm_if, "    asn1Scc%s_Initialize(&IN_%s);\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
266
267
268
269
270
                        p->type, p->name);
                }
        );
        FOREACH(p, Parameter, i->out, {
                fprintf(vm_if,
Maxime Perrotin's avatar
Maxime Perrotin committed
271
                        "    asn1Scc%s_Initialize(&OUT_%s);\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
272
273
274
275
276
277
278
279
280
                        p->type, p->name);
                }
        );

        fprintf(vm_if, "#endif\n");
    }

    /* g. Decode each IN param */
    if (NULL != i->in) {
Maxime Perrotin's avatar
Maxime Perrotin committed
281
        fprintf(vm_if, "\n    /* Decode each input parameter */\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
282
283
284
285
    }

    FOREACH(p, Parameter, i->in, {
            fprintf(vm_if,
Maxime Perrotin's avatar
Maxime Perrotin committed
286
287
288
289
                    "    if (0 != Decode_%s_%s (&IN_%s, pmy_%s, size_my_%s)) {\n"
                    "        printf(\"\\nError Decoding %s\\n\");\n"
                    "        return;\n"
                    "    }\n\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
290
291
292
293
294
295
                    BINARY_ENCODING(p),
                    p->type, p->name, p->name, p->name, p->type);}
    );

    if (qgenc == i->parent_fv->language) {
        /* h. Add a call to the user-defined function passing the decoded input as parameter */
Maxime Perrotin's avatar
Maxime Perrotin committed
296
297
        fprintf(vm_if, "    /* Call to QGenc comp function */\n");
        fprintf(vm_if, "    %s_comp (", i->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
298
299
300
301
302

        /* Add the external QGenc comp function declaration in the header file */
        fprintf(vm_if_h, "extern void %s_comp (", i->name);
    } else {
        /* h. Add a call to the user-defined function passing the decoded input as parameter */
Maxime Perrotin's avatar
Maxime Perrotin committed
303
304
        fprintf(vm_if, "    /* Call to User-defined function */\n");
        fprintf(vm_if, "    %s_PI_%s (", i->parent_fv->name, i->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
305
306
307
308
309

        /* Add the external PI function declaration in the header file */
        fprintf(vm_if_h, "extern void %s_PI_%s (", i->parent_fv->name, i->name);
    }

Maxime Perrotin's avatar
Maxime Perrotin committed
310
    comma = false;
Maxime Perrotin's avatar
Maxime Perrotin committed
311
    FOREACH (p, Parameter, i->in, {
Maxime Perrotin's avatar
Maxime Perrotin committed
312
        fprintf(vm_if_h, "%sconst asn1Scc%s *",
Maxime Perrotin's avatar
Maxime Perrotin committed
313
                         comma ? ", " : "",
Maxime Perrotin's avatar
Maxime Perrotin committed
314
                         p->type);
Maxime Perrotin's avatar
Maxime Perrotin committed
315
        comma = true;
Maxime Perrotin's avatar
Maxime Perrotin committed
316
317
    });
    FOREACH (p, Parameter, i->out, {
Maxime Perrotin's avatar
Maxime Perrotin committed
318
        fprintf(vm_if_h, "%sasn1Scc%s *",
Maxime Perrotin's avatar
Maxime Perrotin committed
319
                         comma ? ", " : "",
Maxime Perrotin's avatar
Maxime Perrotin committed
320
                         p->type);
Maxime Perrotin's avatar
Maxime Perrotin committed
321
        comma = true;
Maxime Perrotin's avatar
Maxime Perrotin committed
322
323
324
325
326
    });
    fprintf(vm_if_h, ");\n");

    comma = false;
    FOREACH(p, Parameter, i->in, {
Maxime Perrotin's avatar
Maxime Perrotin committed
327
328
329
        fprintf(vm_if, "%s&IN_%s",
                (true == comma) ? ", " : "", p->name); comma = true;
        }
Maxime Perrotin's avatar
Maxime Perrotin committed
330
331
332
333
    );

    comma = false;
    FOREACH(p, Parameter, i->out, {
Maxime Perrotin's avatar
Maxime Perrotin committed
334
335
336
337
        if (NULL != i->in) comma = true;
        fprintf(vm_if, "%s&OUT_%s",
                (true == comma) ? ", " : "", p->name); comma = true;
        }
Maxime Perrotin's avatar
Maxime Perrotin committed
338
339
340
341
342
343
    );

    fprintf(vm_if, ");\n\n");

    /* i. Encode each OUT param */
    if (NULL != i->out) {
Maxime Perrotin's avatar
Maxime Perrotin committed
344
        fprintf(vm_if, "    /* Encode each output parameter */\n\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
345
346
347
    }

    FOREACH(p, Parameter, i->out, {
Maxime Perrotin's avatar
Maxime Perrotin committed
348
349
350
351
352
353
354
355
356
357
358
359
360
        fprintf(vm_if,
                "    *psize_my_%s = Encode_%s_%s (pmy_%s, %sasn1Scc%s%s, &OUT_%s);\n",
                p->name,
                BINARY_ENCODING(p),
                p->type,
                p->name,
                (native == p->encoding) ? "sizeof (" : "",
                p->type,
                (native == p->encoding) ? ")" :
                (uper == p->encoding) ?
                "_REQUIRED_BYTES_FOR_ENCODING" :
                "_REQUIRED_BYTES_FOR_ACN_ENCODING", p->name);
        }
Maxime Perrotin's avatar
Maxime Perrotin committed
361
362
363
364
365
366
367
368
369
370
371
372
373
    );

    fprintf(vm_if, "}\n");
}

/* Generation of interface functions to allow developer to call RI */
void add_RI_to_c_invoke_ri(Interface * i)
{
    if (NULL == invoke_ri) {
        char *path = NULL;
        int hasparam = 0;

        FOREACH(interface, Interface, i->parent_fv->interfaces, {
Maxime Perrotin's avatar
Maxime Perrotin committed
374
375
                CheckForAsn1Params(interface, &hasparam);
        });
Maxime Perrotin's avatar
Maxime Perrotin committed
376
377
378
379
380

        if (NULL != i->parent_fv->system_ast->context->output) {
            build_string(&path, get_context()->output,
                         strlen(get_context()->output));
        }
Maxime Perrotin's avatar
Maxime Perrotin committed
381

Maxime Perrotin's avatar
Maxime Perrotin committed
382
383
384
385
386
387
        build_string(&path, i->parent_fv->name,
                     strlen(i->parent_fv->name));

        if (-1 == create_file(path, "invoke_ri.c", &invoke_ri)) {
            free(path);
            return;
Maxime Perrotin's avatar
Maxime Perrotin committed
388
389
        }
        else {
Maxime Perrotin's avatar
Maxime Perrotin committed
390
            fprintf(invoke_ri,
Maxime Perrotin's avatar
Maxime Perrotin committed
391
392
393
394
                    "/* This file was generated automatically: DO NOT MODIFY IT ! */\n\n"
                    "#include <stdlib.h>\n"
                    "#ifdef __unix__\n"
                    "#include <stdio.h>\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
395
            if (hasparam) {
Maxime Perrotin's avatar
Maxime Perrotin committed
396
397
398
                fprintf(invoke_ri,
                        "#include \"PrintTypesAsASN1.h\"\n"
                        "#include \"timeInMS.h\"\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
399
400
401
402
403
            }
            fprintf(invoke_ri, "#endif\n\n");
            if (hasparam)
                fprintf(invoke_ri, "#include \"C_ASN1_Types.h\"\n");
            if (get_context()->polyorb_hi_c) {
Maxime Perrotin's avatar
Maxime Perrotin committed
404
405
406
                fprintf(invoke_ri,
                        "#include \"%s_polyorb_interface.h\"\n\n",
                        i->parent_fv->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
407
408
            }
            if (qgenc == i->distant_qgen->language)
Maxime Perrotin's avatar
Maxime Perrotin committed
409
410
411
                fprintf(invoke_ri,
                        "#include \"%s.h\"\n\n",
                        string_to_lower(i->distant_name));
Maxime Perrotin's avatar
Maxime Perrotin committed
412
413
414
415
416
417
418
        }
        free(path);
    }


    /* a. function name */

Maxime Perrotin's avatar
Maxime Perrotin committed
419
420
421
422
423
    fprintf(invoke_ri,
            "void %s_RI_%s(",
            i->parent_fv->name,
            i->name);

Maxime Perrotin's avatar
Maxime Perrotin committed
424
425
426
427
428
429
430
431
432
433
    /* Add the IN and OUT parameters: */
    FOREACH(p, Parameter, i->in, {
            List_C_Types_And_Params_With_Pointers(p, &invoke_ri);}
    );
    FOREACH(p, Parameter, i->out, {
            List_C_Types_And_Params_With_Pointers(p, &invoke_ri);}
    );

    fprintf(invoke_ri, ")\n{\n");

Maxime Perrotin's avatar
Maxime Perrotin committed
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
    /* MSC Tracer-related code */
    FV *receiver_FV = NULL;
    char *receiver_id = NULL, *sender_id = NULL;

    fprintf(invoke_ri, "#ifdef __unix__\n");

    fprintf(invoke_ri, "    static int innerMsc = -1;\n");

    /* Sender or receiver can be in a FV that was created during VT
     * (their name may be computed: fv_FV_pi) - in that case this is not
     * what we want to see in the MSC - we have to retreive the original
     * name, as it was entered by the user in the interface view */
    if (NULL != i->distant_fv) {
        receiver_FV = FindFV(i->distant_fv);

        if (true == receiver_FV->artificial) {
            receiver_id = receiver_FV->original_name;
        }
        else {
            receiver_id = receiver_FV->name;
        }
        if (true == i->parent_fv->artificial) {
            sender_id = i->parent_fv->original_name;
        }
        else {
            sender_id = i->parent_fv->name;
        }
        fprintf(invoke_ri,
                "    if (-1 == innerMsc)\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
463
464
                "        innerMsc = (NULL != getenv(\"TASTE_INNER_MSC\"))?1:0;\n"
                "    if (1 == innerMsc) {\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
465
466
467
                "        long long msc_time = getTimeInMilliseconds();\n\n");
        FOREACH(p, Parameter, i->in, {
            fprintf(invoke_ri,
Maxime Perrotin's avatar
Maxime Perrotin committed
468
469
470
                    "        {\n"
                    "            PrintASN1%s (\"INNERDATA: %s::%s::%s\", IN_%s);\n"
                    "        }\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
471
472
473
474
475
476
477
                    p->type,
                    i->name,
                    p->type,
                    p->name,
                    p->name);
        });
        fprintf(invoke_ri,
Maxime Perrotin's avatar
Maxime Perrotin committed
478
479
480
                "        printf (\"\\nINNER: %s,%s,%s,%%lld\\n\", msc_time);\n"
                "        fflush(stdout);\n"
                "    }\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
481
482
483
484
485
486
                sender_id, receiver_id, i->name);

        fprintf(invoke_ri, "#endif\n\n");
        /* End MSC Tracer-related code */
    }

Maxime Perrotin's avatar
Maxime Perrotin committed
487
488
489
    if (qgenada == i->distant_qgen->language) {

        /* Add a call to the vm callback function */
Maxime Perrotin's avatar
Maxime Perrotin committed
490
        fprintf(invoke_ri, "\n    /* Call to VM callback function */\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
491

Maxime Perrotin's avatar
Maxime Perrotin committed
492
        fprintf(invoke_ri, "    vm_%s%s_%s(",
Maxime Perrotin's avatar
Maxime Perrotin committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
                asynch == i->synchronism ? "async_" : "", i->parent_fv->name,
                i->name);

        /* Create the list of parameters */
        FOREACH(p, Parameter, i->in, {
                List_QGen_Param_Names(p, &invoke_ri, i->distant_qgen->language);}
        );

        FOREACH(p, Parameter, i->out, {
                List_QGen_Param_Names(p, &invoke_ri, i->distant_qgen->language);}
        );

        fprintf(invoke_ri, ");\n\n");

    } else if (qgenc == i->distant_qgen->language){

        /* Add a call to the QGenc comp function */
Maxime Perrotin's avatar
Maxime Perrotin committed
510
        fprintf(invoke_ri, "\n    /* Call QGenc function */\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
511

Maxime Perrotin's avatar
Maxime Perrotin committed
512
        fprintf(invoke_ri, "    %s_comp(", i->distant_name);
Maxime Perrotin's avatar
Maxime Perrotin committed
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527

        /* Create the list of parameters */
        FOREACH(p, Parameter, i->in, {
                List_QGen_Param_Names(p, &invoke_ri, i->distant_qgen->language);}
        );

        FOREACH(p, Parameter, i->out, {
                List_QGen_Param_Names(p, &invoke_ri, i->distant_qgen->language);}
        );

        fprintf(invoke_ri, ");\n\n");

    } else {

        /* d. For each IN and OUT params, declare a static buffer to put encoded data */
Maxime Perrotin's avatar
Maxime Perrotin committed
528
        if (NULL != i->in) {
Maxime Perrotin's avatar
Maxime Perrotin committed
529
            fprintf(invoke_ri,
Maxime Perrotin's avatar
Maxime Perrotin committed
530
                    "    /* Buffer(s) to put the encoded input parameter(s) */\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
531
        }
Maxime Perrotin's avatar
Maxime Perrotin committed
532
        FOREACH(p, Parameter, i->in, {
Maxime Perrotin's avatar
Maxime Perrotin committed
533
            fprintf(invoke_ri,
Maxime Perrotin's avatar
Maxime Perrotin committed
534
                    "    static char IN_buf_%s[%sasn1Scc%s%s] = {0};\n    int size_IN_buf_%s=0;\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
535
536
537
538
539
540
541
542
                    p->name,
                    (native == p->encoding) ? "sizeof(" : "",
                    p->type,
                    (native == p->encoding) ? ")" :
                    (uper == p->encoding) ? "_REQUIRED_BYTES_FOR_ENCODING" :
                    "_REQUIRED_BYTES_FOR_ACN_ENCODING",
                    p->name);
        });
Maxime Perrotin's avatar
Maxime Perrotin committed
543

Maxime Perrotin's avatar
Maxime Perrotin committed
544
        if (NULL != i->out) {
Maxime Perrotin's avatar
Maxime Perrotin committed
545
            fprintf(invoke_ri,
Maxime Perrotin's avatar
Maxime Perrotin committed
546
                    "\n    /* Buffer(s) for the output parameter(s) */\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
547
        }
Maxime Perrotin's avatar
Maxime Perrotin committed
548
        FOREACH(p, Parameter, i->out, {
Maxime Perrotin's avatar
Maxime Perrotin committed
549
            fprintf(invoke_ri,
Maxime Perrotin's avatar
Maxime Perrotin committed
550
                    "    static char OUT_buf_%s[%sasn1Scc%s%s];\n    int size_OUT_buf_%s=0;\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
551
552
553
554
555
556
557
558
                    p->name,
                    (native == p->encoding) ? "sizeof(" : "",
                    p->type,
                    (native == p->encoding) ? ")" :
                    (uper == p->encoding) ? "_REQUIRED_BYTES_FOR_ENCODING" :
                    "_REQUIRED_BYTES_FOR_ACN_ENCODING",
                    p->name);
        });
Maxime Perrotin's avatar
Maxime Perrotin committed
559
560

        /* e. Encode each IN param */
Maxime Perrotin's avatar
Maxime Perrotin committed
561
        if (NULL != i->in) {
Maxime Perrotin's avatar
Maxime Perrotin committed
562
            fprintf(invoke_ri, "\n    /* Encode each input parameter */\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
563
564
        }

Maxime Perrotin's avatar
Maxime Perrotin committed
565
        FOREACH(p, Parameter, i->in, {
Maxime Perrotin's avatar
Maxime Perrotin committed
566
            fprintf(invoke_ri,
Maxime Perrotin's avatar
Maxime Perrotin committed
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
                    "    size_IN_buf_%s = Encode_%s_%s(IN_buf_%s, %sasn1Scc%s%s, IN_%s);\n"
                    "    if (-1 == size_IN_buf_%s) {\n"
                    "#ifdef __unix__\n"
                    "        printf (\"** Encoding error in %s_RI_%s!!\\n\");\n"
                    "#endif\n"
                    "        /* Major error, we must stop the application and let the FDIR/Watchdogs recover */\n"
                    "        exit (-1);\n"
                    "    }\n",
                    p->name,
                    BINARY_ENCODING(p),
                    p->type, p->name,
                    (native == p->encoding) ? "sizeof(" : "",
                    p->type,
                    (native == p->encoding) ? ")" :
                    (uper == p->encoding) ? "_REQUIRED_BYTES_FOR_ENCODING" :
                    "_REQUIRED_BYTES_FOR_ACN_ENCODING",
                    p->name,
                    p->name,
                    i->parent_fv->name,
                    i->name);
        });
Maxime Perrotin's avatar
Maxime Perrotin committed
588

Maxime Perrotin's avatar
Maxime Perrotin committed
589
590
591
592
593
594

        /* f. Add a call to the vm callback function passing the encoded
         *  inputs as parameters */
        fprintf(invoke_ri, "\n    /* Call to VM callback function */\n");
        char *name = make_string("vm_%s%s_%s",
                asynch == i->synchronism ? "async_" : "", i->parent_fv->name,
Maxime Perrotin's avatar
Maxime Perrotin committed
595
596
                i->name);

Maxime Perrotin's avatar
Maxime Perrotin committed
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
        /* Declare external function (may be in Ada, so no file to include */
        fprintf(invoke_ri, "    extern void %s(", name);
        bool comma = false;
        FOREACH(_, Parameter, i->in, {
                (void) _;
                fprintf(invoke_ri, "%svoid *, size_t", comma? ", ": "");
                comma = true;
        });
        FOREACH(_, Parameter, i->out, {
                (void) _;
                fprintf(invoke_ri, "%svoid *, size_t *", comma? ", ": "");
                comma = true;
        });
        fprintf(invoke_ri, ");\n\n");

        fprintf(invoke_ri, "    %s(", name);

        free(name);

Maxime Perrotin's avatar
Maxime Perrotin committed
616
617
618
619
620
621
622
623
624
625
626
        /* Add the IN and OUT parameters */
        FOREACH(p, Parameter, i->in, {
                List_C_Params_And_Size(p, &invoke_ri);}
        );
        FOREACH(p, Parameter, i->out, {
                List_C_Params_And_Size(p, &invoke_ri);}
        );

        fprintf(invoke_ri, ");\n\n");

        /* g. Decode each OUT param */
Maxime Perrotin's avatar
Maxime Perrotin committed
627
        if (NULL != i->out) {
Maxime Perrotin's avatar
Maxime Perrotin committed
628
            fprintf(invoke_ri, "    /* Decode each output parameter */\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
629
630
        }

Maxime Perrotin's avatar
Maxime Perrotin committed
631
        FOREACH(p, Parameter, i->out, {
Maxime Perrotin's avatar
Maxime Perrotin committed
632
            fprintf(invoke_ri,
Maxime Perrotin's avatar
Maxime Perrotin committed
633
634
635
636
637
638
639
640
641
642
643
644
645
                    "    if (0 != Decode_%s_%s(OUT_%s, OUT_buf_%s, size_OUT_buf_%s)) {\n"\
                    "#ifdef __unix__\n"
                    "        printf(\"\\nError Decoding %s\\n\");\n"
                    "#endif\n"
                    "        return;\n"
                    "    }\n\n",
                    BINARY_ENCODING(p),
                    p->type,
                    p->name,
                    p->name,
                    p->name,
                    p->type);
        });
Maxime Perrotin's avatar
Maxime Perrotin committed
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724

    }

    fprintf(invoke_ri, "}\n\n");
}

void End_C_Glue_Backend()
{
        close_c_gluefiles();
}

/* Function to process a PI for C language */
void GLUE_C_ProvidedInterface(Interface * i)
{
    if (NULL == i)
        return;
    if (PI == i->direction && !(qgenada == i->parent_fv->language || qgenc == i->parent_fv->language))
        add_PI_to_c_vm_if(i);
}

/* Function to process a RI for non-ObjectGEODE languages */
void GLUE_C_InvokeRI(Interface * i)
{
    if (NULL == i)
        return;
    if (RI == i->direction) {
        /* Discard duplicate RI (with different synchronism) - keep only the sync one */
        if (asynch == i->synchronism) {
            FOREACH(interface, Interface, i->parent_fv->interfaces, {
                    if (RI == interface->direction &&
                        !strcmp(interface->name, i->name) &&
                        synch == interface->synchronism) {
                    return;}
                    }
            );
        }
        add_RI_to_c_invoke_ri(i);
    }
}

/* External interface (the one and unique) */
void GLUE_C_Backend(FV * fv)
{
    count_calling_threads = 0;

    if (fv->system_ast->context->onlycv)
        return;

    /* Check, if it's a passive FV, if it can be called by more than one thread.
       This is done in order to generate the appropriate call to vm_... function  */
    if (passive_runtime == fv->runtime_nature) {

        FOREACH(ct, FV, fv->calling_threads, {
                (void) ct; count_calling_threads++;
                }
        );
    }


    if (c == fv->language || gui == fv->language || ada == fv->language
        || qgenada == fv->language || qgenc == fv->language
        || rtds == fv->language || cpp == fv->language || opengeode == fv->language) {
        Init_C_Glue_Backend(fv);
        FOREACH(i, Interface, fv->interfaces, {
                GLUE_C_ProvidedInterface(i);
                }
        );
    }

    /* for all languages except ObjectGeode (sdl) and Blackbox devices, generate invoke_ri.c */
    if (sdl != fv->language && blackbox_device != fv->language) {
        FOREACH(i, Interface, fv->interfaces, {
                GLUE_C_InvokeRI(i);
                }
        );
    }

    End_C_Glue_Backend();
}