build_ada_skeletons.c 12.2 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
/* Buildsupport is (c) 2008-2015 European Space Agency
 * contact: maxime.perrotin@esa.int
 * License is LGPL, check LICENSE file */
/* build_ada_skeletons.c

  this program generates empty Ada functions respecting the interfaces defined
  in the interface view. it provides functions to invoke RI.

  Note on the use of the Ada skeletons:
   Generated functions for PI are called by C code with parameters passed as
   pointers. In Ada, pointers are expressed using the "access" keyword instead
   of "in" or "out". To access the variable values, the ".all" postfix can be
   used. And to declare a variable for which an access is necessary, the type
   must be "aliased". Complete example:

     procedure Compute (my_input: access asn1sccT_SEQUENCE) is   -- Provided interface
     my_output: aliased T_INTEGER;   -- Parameter of a Required Interface
     begin
       my_output := my_input.all.x + my_input.all.y;
       required_interface (my_output'access); -- Call Required Interface
     end Compute;
     
  Copyright 2014-2015 IB Krates <info@krates.ee>
       QGenc code generator integration

*/

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

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

static FILE *adb = NULL, *ads = NULL;

/* Adds header to ads and (if new) adb files */
int ada_gw_preamble(FV * fv)
{
    int hasparam = 0;

    /* todo: move the modules list to the FV struct */
    ASN1_Module_list *modules = NULL;

    if (NULL == ads)
        return -1;

    /* a. ads preamble */
    fprintf(ads,
            "-- This file was generated automatically: DO NOT MODIFY IT !\n\n");
    fprintf(ads,
            "-- Declaration of the provided and required interfaces\n\n");
    fprintf(ads, "pragma style_checks (off);\n");
    fprintf(ads, "pragma warnings (off);\n");

    /* Check if any interface needs Asn.1 types and if so, include asn1 modules */

    if (NULL != fv->timer_list) {
       fprintf(ads, "with taste_basictypes;\n"
                    "use taste_basictypes;\n\n");
    }

    FOREACH(i, Interface, fv->interfaces, {
            CheckForAsn1Params(i, &hasparam);}
    );
    if (hasparam) {
        fprintf(ads, "with adaasn1rtl;\n");
        fprintf(ads, "use adaasn1rtl;\n\n");

        /* Build the list of Ada packages to include from the ASN.1 module list */
        FOREACH(i, Interface, fv->interfaces, {
            FOREACH(p, Parameter, i->in, ADD_TO_SET(ASN1_Module, modules, p->asn1_module));
            FOREACH(p, Parameter, i->out, ADD_TO_SET(ASN1_Module, modules, p->asn1_module));
Maxime Perrotin's avatar
Maxime Perrotin committed
76
77
78
79
80
81
82
83
84
            FOREACH(cp, Context_Parameter, fv->context_parameters, {
                if (strcmp (cp->type.name, "Taste-directive") &&
                    strcmp (cp->type.name, "Simulink-Tunable-Parameter") &&
                    strcmp (cp->type.name, "Timer")) {
                    ADD_TO_SET(ASN1_Module,
                               modules,
                               asn2underscore(cp->type.module, strlen(cp->type.module)));
                }
            });
Maxime Perrotin's avatar
Maxime Perrotin committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        });

        FOREACH(m, ASN1_Module, modules, {
                fprintf(ads, "with %s;\nuse %s;\n\n", m, m);}
        );
    }

    if(has_context_param(fv)) {
        fprintf(ads,
                "--  Context Parameters defined in interface view\n"
                "with context_%s;\n\n",
                fv->name);
    }

    fprintf(ads, "package %s is\n", fv->name);

    /* Expose each context parameter - pragma export used by SMP2 */
    FOREACH (cp, Context_Parameter, fv->context_parameters, {
        if (strcmp (cp->type.name, "Taste-directive") &&
            strcmp (cp->type.name, "Simulink-Tunable-Parameter") &&
            strcmp (cp->type.name, "Timer")) {

            fprintf(ads,
                "\t%s : asn1Scc%s := context_%s.%s_ctxt.%s;\n"
                "\tpragma export(C, %s, \"%s_%s\");\n",
                cp->name,
Maxime Perrotin's avatar
Maxime Perrotin committed
111
                asn2underscore(cp->type.name, strlen(cp->type.name)),
Maxime Perrotin's avatar
Maxime Perrotin committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
                fv->name,
                fv->name,
                cp->name,
                cp->name,
                fv->name,
                cp->name);
        }
    });
    fprintf(ads, "\n");


    /* b. adb preamble (if applicable) */
    if (NULL != adb) {
        fprintf(adb, "-- User implementation of the %s function\n",
                fv->name);
        fprintf(adb,
                "-- This file will never be overwritten once edited and modified\n");
        fprintf(adb,
                "-- Only the interface of functions is regenerated (in the .ads file)\n\n");

        fprintf(adb, "pragma style_checks (off);\n");
        fprintf(adb, "pragma warnings (off);\n");

        if (hasparam) {
            fprintf(adb, "with adaasn1rtl;\n");
            fprintf(adb, "use adaasn1rtl;\n\n");
            FOREACH(m, ASN1_Module, modules, {
                    fprintf(adb, "with %s;\nuse %s;\n\n", m, m);}
            );
        }

        if (qgenada == fv->language) {
            /* Add a with and use clause for every QGenAda PI */
            FOREACH(i, Interface, fv->interfaces, {
                switch (i->direction) {
                    case PI: fprintf(adb, "with %s;\nuse %s;\n\n", i->name, i->name); break;
                    case RI: break;
                    default: break;
                }
            });
        }

        fprintf(adb, "package body %s is\n\n", fv->name);

    }
    return 0;
}

/* Creates FV.ads, and if necessary FV.adb (if it did not already exist) */
int Init_Ada_GW_Backend(FV * fv)
{
    char *path = NULL;
    char *filename = NULL;

    if (NULL != fv->system_ast->context->output) {
        build_string(&path, fv->system_ast->context->output,
                     strlen(fv->system_ast->context->output));
    }

    build_string(&path, fv->name, strlen(fv->name));

    build_string(&filename, fv->name, strlen(fv->name));
    build_string(&filename, ".ads", strlen(".ads"));
    create_file(path, filename, &ads);

    filename[strlen(filename) - 1] = 'b';       /* change from ads to adb */

    if (!file_exists(path, filename)) {
        create_file(path, filename, &adb);
        assert(NULL != adb);
    } else
        INFO ("** Information: User code not overwritten for function %s\n",
               fv->name);

    free(path);
    free(filename);

    assert(NULL != ads);

    ada_gw_preamble(fv);

    return 0;
}


void close_ada_gw_files(FV * fv)
{
    if (NULL != ads) {
        fprintf(ads, "\nend %s;\n", fv->name);
        close_file(&ads);
    }
    if (NULL != adb) {
        fprintf(adb, "\nend %s;\n", fv->name);
        close_file(&adb);
    }
}

/* Create a string in the form "name: access type",
 * as expected by Ada for list of parameters*/
void Create_Ada_Access_Param(Parameter * p, char **result)
{
    if (NULL != *result) {
        build_string(result, "; ", 2);
    }
    build_string(result, p->name, strlen(p->name));
    build_string(result, ": access asn1scc", strlen(": access asn1scc"));
    build_string(result, p->type, strlen(p->type));
}

/*
   Add a Provided interface. Can contain in and out parameters
   Write in ads the declaration of the user functions
   and if adb is new, copy these declarations there too.
*/
void add_PI_to_Ada_gw(Interface * i)
{
    char *ada_params = NULL;

    if (NULL == ads)
        return;

    fprintf(ads,
            "\t---------------------------------------------------------\n"
            "\t-- Provided interface \"%s\"\n"
            "\t---------------------------------------------------------\n"
            "\tprocedure %s",
            i->name,
            i->name);

    /* Create the list of parameters
     * (e.g. "param1: in type1; param2: out type2") */
    FOREACH(p, Parameter, i->in, {
            Create_Ada_Access_Param(p, &ada_params);}
    );

    FOREACH(p, Parameter, i->out, {
            Create_Ada_Access_Param(p, &ada_params);}
    );

    if (NULL != ada_params) {
        fprintf(ads, "(");
        fprintf(ads, "%s", ada_params);
        fprintf(ads, ")");
    }

    fprintf(ads, ";\n");
    fprintf(ads, "\tpragma export(C, %s, \"%s_PI_%s\");\n\n", i->name,
            i->parent_fv->name, i->name);

    if (NULL != adb) {
        fprintf(adb,
                "\t---------------------------------------------------------\n"
                "\t-- Provided interface \"%s\"\n"
                "\t---------------------------------------------------------\n"
                "\tprocedure %s",
                i->name,
                i->name);
    }

    if (NULL != ada_params && NULL != adb) {
        fprintf(adb, "(");
        fprintf(adb, "%s", ada_params);
        fprintf(adb, ")");
    }

    if (NULL != adb) {
        fprintf(adb,
            " is\n\tpragma suppress (all_checks);"
            "\n\tbegin\n\n");

        fprintf(adb,
            "\t\tnull;"
            " -- Replace \"null\" with your own code!\n\n\tend %s;\n\n",
            i->name);
    }

    if (NULL != ada_params) {
        free(ada_params);
        ada_params = NULL;
    }
}

/* Declaration of the RI in the ads file */
void add_RI_to_Ada_gw(Interface * i)
{
    char *ada_params = NULL;

    if (NULL == ads)
        return;

    fprintf(ads,
            "\t---------------------------------------------------------\n"
            "\t-- Required interface \"%s\"\n"
            "\t---------------------------------------------------------\n"
            "\tprocedure %s",
            i->name,
            i->name);

    /* Create the list of parameters
     * (e.g. "param1: in type1; param2: out type2") */
    FOREACH(p, Parameter, i->in, {
            Create_Ada_Access_Param(p, &ada_params);}
    );

    FOREACH(p, Parameter, i->out, {
            Create_Ada_Access_Param(p, &ada_params);}
    );

    if (NULL != ada_params) {
        fprintf(ads, "(");
        fprintf(ads, "%s", ada_params);
        fprintf(ads, ")");
    }

    free(ada_params);
    ada_params = NULL;

    fprintf(ads, ";\n");

    fprintf(ads, "\tpragma import(C, %s, \"%s_RI_%s\");\n\n", i->name,
            i->parent_fv->name, i->name);
}

/* Add timer declarations to the Ada code skeletons */
void Ada_Add_timers (FV *fv)
{
    if (NULL != fv->timer_list) {
        fprintf (ads, 
                "\t---------------------------------------------------------\n"
                "\t--                   Timers management                 --\n"
                "\t---------------------------------------------------------\n"
                "\n\n");
    }
    FOREACH(timer, String, fv->timer_list, {
        fprintf(ads,
            "\t--  This function is called when the timer \"%s\" expires\n"
            "\tprocedure %s;\n"
            "\tpragma export(C, %s, \"%s_PI_%s\");\n\n"
            "\t-- Call this function to set (enable) the timer\n"
            "\t-- Value is in milliseconds, and must be a multiple of 100\n"
            "\tprocedure SET_%s(val: access asn1sccT_UInt32);\n\n"
            "\tpragma import(C, SET_%s, \"%s_RI_SET_%s\");\n\n"
            "\t-- Call this function to reset (disable) the timer\n"
            "\tprocedure RESET_%s;\n\n"
            "\tpragma import(C, RESET_%s, \"%s_RI_RESET_%s\");\n\n",
            timer,
            timer,
            timer,
            fv->name,
            timer,
            timer,
            timer,
            fv->name,
            timer,
            timer,
            timer,
            fv->name,
            timer);
        if (NULL != adb) {
            fprintf(adb,
              "\t-- This function is called when the timer \"%s\" expires \n"
              "\tprocedure %s is\n"
              "\tbegin\n"
              "\t    null;  --  Replace \"null\" with your own code!\n"
              "\tend;\n\n",
              timer,
              timer);
        }
    });
}

/* External interface */
void GW_Ada_Backend(FV * fv)
{
    if (fv->system_ast->context->onlycv)
        return;

    if (ada == fv->language || qgenada == fv->language) {

        /* Create the files and add headers */
        Init_Ada_GW_Backend(fv);

        /* Process each interface of the function */
        FOREACH(i, Interface, fv->interfaces, {
            switch (i->direction) {
                case PI: add_PI_to_Ada_gw(i); break;
                case RI: add_RI_to_Ada_gw(i); break;
                default: break;
            }
        });

        // If any timers, add declarations in code skeleton
        Ada_Add_timers(fv);

        /* Write footers and close files */
        close_ada_gw_files(fv);
    }
}