c_ast_construction.c 33.7 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
76
77
78
79
80
81
82
83
84
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
111
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
/* Buildsupport is (c) 2008-2015 European Space Agency
 * contact: maxime.perrotin@esa.int
 * License is LGPL, check LICENSE file */
/*
 * C AST Construction functions
 * This file contains all the functions used to build
 * the Abstact Syntax Tree (AST) of a system
 */


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

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

/* Data shared by the functions of this module */

/* System AST: */
static System *system_ast = NULL;

/* Temporary AST construction units: */
static FV *fv = NULL;
static Interface *interface = NULL;
static Parameter *parameter = NULL;
static Aplc_binding *binding = NULL;
static char *binding_name = NULL;
static Process *process = NULL;
static Processor *processor = NULL;
static Bus *bus = NULL;
static Device *device = NULL;
static Connection *connection = NULL;
static char *search_name = NULL;
static Device_list *drivers = NULL;

/* Function returning a pointer to the system AST */
System *get_system_ast()
{
    return system_ast;
}

/* Set System name */
void Set_Root_Node(char *name, size_t len)
{
    build_string(&(system_ast->name), name, len);
}

void Set_Native_Encoding()
{
    if (NULL != parameter)
        parameter->encoding = native;
}

void Set_UPER_Encoding()
{
    if (NULL != parameter)
        parameter->encoding = uper;
}

void Set_ACN_Encoding()
{
    if (NULL != parameter)
        parameter->encoding = acn;
}


void Set_ASN1_BasicType_Sequence()
{
    if (NULL != parameter)
        parameter->basic_type = sequence;
}

void Set_ASN1_BasicType_SequenceOf()
{
    if (NULL != parameter)
        parameter->basic_type = sequenceof;
}

void Set_ASN1_BasicType_Enumerated()
{
    if (NULL != parameter)
        parameter->basic_type = enumerated;
}

void Set_ASN1_BasicType_Set()
{
    if (NULL != parameter)
        parameter->basic_type = set;
}

void Set_ASN1_BasicType_SetOf()
{
    if (NULL != parameter)
        parameter->basic_type = setof;
}

void Set_ASN1_BasicType_Integer()
{
    if (NULL != parameter)
        parameter->basic_type = integer;
}

void Set_ASN1_BasicType_Boolean()
{
    if (NULL != parameter)
        parameter->basic_type = boolean;
}

void Set_ASN1_BasicType_Real()
{
    if (NULL != parameter)
        parameter->basic_type = real;
}

void Set_ASN1_BasicType_Choice()
{
    if (NULL != parameter)
        parameter->basic_type = choice;
}

void Set_ASN1_BasicType_String()
{
    if (NULL != parameter)
        parameter->basic_type = string;
}

void Set_ASN1_BasicType_OctetString()
{
    if (NULL != parameter)
        parameter->basic_type = octetstring;
}

void Set_ASN1_BasicType_Unknown()
{
    if (NULL != parameter)
        parameter->basic_type = unknown_type;
}

void Set_Debug_Messages()
{
        if (NULL != (system_ast->context)) {
            system_ast->context->debug = 1;
        }
}


void Set_Language_To_SDL()
{
    if (NULL != fv)
        fv->language = sdl;
}

void Set_Language_To_CPP()
{
    if (NULL != fv)
        fv->language = cpp;
}

165
166
167
168
169
170
void Set_Language_To_VDM()
{
    if (NULL != fv)
        fv->language = vdm;
}

Maxime Perrotin's avatar
Maxime Perrotin committed
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
void Set_Language_To_OpenGEODE()
{
    if (NULL != fv)
        fv->language = opengeode;
}
void Set_Language_To_Simulink()
{
    if (NULL != fv)
        fv->language = simulink;
}

void Set_Language_To_BlackBox_Device()
{
    if (NULL != fv)
        fv->language = blackbox_device;
}

void Set_Language_To_QGenAda()
{
    if (NULL != fv)
        fv->language = qgenada;
}

void Set_Language_To_QGenC()
{
    if (NULL != fv)
        fv->language = qgenc;
}

void Set_Language_To_Other()
{
    if (NULL != fv)
        fv->language = other;
}

void Set_Language_To_C()
{
    if (NULL != fv)
        fv->language = c;
}

void Set_Language_To_RTDS()
{
    if (NULL != fv)
        fv->language = rtds;
}

void Set_Language_To_Rhapsody()
{
    if (NULL != fv)
        fv->language = rhapsody;
}

void Set_Language_To_Scade()
{
    if (NULL != fv)
        fv->language = scade;
}

void Set_Language_To_Ada()
{
    if (NULL != fv)
        fv->language = ada;
}

void Set_Language_To_GUI()
{
    if (NULL != fv)
        fv->language = gui;
}

void Set_Language_To_VHDL()
{
    if (NULL != fv)
        fv->language = vhdl;
}

void Set_Language_To_System_C()
{
    if (NULL != fv)
        fv->language = system_c;
}

254
255
256
257
258
259
void Set_Language_To_MicroPython()
{
    if (NULL != fv)
        fv->language = micropython;
}

Maxime Perrotin's avatar
Maxime Perrotin committed
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
void Set_Zipfile (char *file, size_t len)
{
    assert (NULL != file && 0 < len && NULL != fv);
    build_string (&(fv->zipfile), file, len);
}

void Set_Sync_IF()
{
    if (NULL != interface)
        interface->synchronism = synch;
}

void Set_ASync_IF()
{
    if (NULL != interface)
        interface->synchronism = asynch;
}

void Set_Unknown_IF()
{
    if (NULL != interface)
        interface->synchronism = unknown;
}

void Set_Cyclic_IF()
{
    if (NULL != interface)
        interface->rcm = cyclic;
}

void Set_Sporadic_IF()
{
    if (NULL != interface)
        interface->rcm = sporadic;
}

void Set_Variator_IF()
{
    if (NULL != interface)
        interface->rcm = variator;
}

void Set_Protected_IF()
{
    if (NULL != interface)
        interface->rcm = protected;
}

void Set_Unprotected_IF()
{
    if (NULL != interface)
        interface->rcm = unprotected;
}

void Set_UndefinedKind_IF()
{
    if (NULL != interface)
        interface->rcm = undefined;
}

void Set_Period(long long p)
{
    if (NULL != interface)
        interface->period = p;
}

void Set_Context_Variable (char *name, size_t len1, char *type, size_t len2,
                           char *def, size_t len3, char *mod, size_t len4,
                           char *file, size_t len5, char *fullNameWithCase)
{
 Context_Parameter *cp = NULL;
 unsigned int i = 0;
Maxime Perrotin's avatar
Maxime Perrotin committed
332

Maxime Perrotin's avatar
Maxime Perrotin committed
333
334
335
336
337
338
339
340
341
342
343
344
345
 cp = (Context_Parameter *) malloc (sizeof (Context_Parameter));

 assert (NULL != cp);

 cp->name = NULL;
 cp->fullNameWithCase = NULL;
 cp->type.name = NULL;
 cp->type.module = NULL;
 cp->type.asn1_filename = NULL;
 cp->value = NULL;

 /* First check the validity of the ASN.1 module name */
 if (!strncmp (mod, "nomodule", len4)) {
346
347
     ERROR ("[ERROR] The dataview you are using was generated using an old version\n");
     ERROR ("        of the toolchain. You must update it by calling taste-update-data-view\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
     exit (-1);
 }
 /* Temporary for the SMP2 support until version of taste-IV withtout obj suffixes */
 size_t lenWithSuffix = len1;
 /* Remove any _objXXX suffix on the name field */
 len1 = remove_objXXX_suffix(name, len1);

 build_string (&(cp->name), name, len1);
 build_string (&(cp->fullNameWithCase), fullNameWithCase, lenWithSuffix);  // temp: remove when objXX disappears
 build_string (&(cp->type.name), type, len2);
 build_string (&(cp->value), def, len3);
 build_string (&(cp->type.module), mod, len4);
 build_string (&(cp->type.asn1_filename), file, len5);

 /* Convert '_' to '-' to be ASN.1-compliant */
 for (i=0; i<len4; i++) if ('_' == cp->type.module[i]) cp->type.module[i] = '-';
 
 APPEND_TO_LIST (Context_Parameter, fv->context_parameters, cp);
}

/* build the WCET string used in VT. */
void Set_Compute_Time(uint64_t lower, char *unitlower, size_t len2,
                      uint64_t upper, char *unitupper, size_t len4)
{
    if (NULL == interface) {
        return;
    }

    interface->wcet_low = lower;
    interface->wcet_high = upper;
    build_string(&(interface->wcet_low_unit), unitlower, len2);
    build_string(&(interface->wcet_high_unit), unitupper, len4);
}

/* Context-related functions */

/* Function returning a pointer to the current system context */
Context *get_context()
{
    return system_ast->context;
}

void Set_Interfaceview (char *name, size_t len)
{
    if (NULL != (system_ast->context) && 0 < len && NULL != name) {
        build_string(&(system_ast->context)->ifview, name, len);
    }

}

void Set_Dataview (char *name, size_t len)
{
    if (NULL != (system_ast->context) && 0 < len && NULL != name) {
        build_string (&(system_ast->context)->dataview, name, len);
    }
}

void Set_Glue()
{
    if (NULL != (system_ast->context)) {
        system_ast->context->glue = 1;
    }
}

void Set_SMP2()
{
    if (NULL != (system_ast->context)) {
        system_ast->context->smp2 = true;
    }
}

void Set_Gateway()
{
    if (NULL != (system_ast->context)) {
        system_ast->context->gw = 1;
    }
}

void Set_keep_case ()
{
    if (NULL != (system_ast->context)) {
        system_ast->context->keep_case = 1;
    }
}

void Set_OnlyCV()
{
    if (NULL != (system_ast->context)) {
        system_ast->context->onlycv = 1;
    }
}

void Set_AADLV2()
{
    if (NULL != (system_ast->context)) {
        system_ast->context->aadlv2 = 1;
    }
}

void Set_Test()
{
    if (NULL != (system_ast->context)) {
        system_ast->context->test = 1;
    }
}

454
455
456
457
458
459
void Set_Timer_Resolution(char *val, size_t len) {
    errno = 0;
    if (NULL != (system_ast->context)) {
        char *str = make_string("%.*s", len, val);
        int time_res = (int) strtol(str, (char **)NULL, 10);
        if (0 != errno) {
460
            ERROR("[ERROR] Timer resolution must be a valid number\n");
461
462
463
464
465
466
467
468
469
        }
        else {
            printf("[INFO] Set timer resolution to %d\n", time_res);
            system_ast->context->timer_resolution = time_res;
        }
    }
}


Maxime Perrotin's avatar
Maxime Perrotin committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/* Set Future - Flag that can be used for temporary purposes,
 * like when migrating from one TASTE version to another one when all
 * tools are not ready.
 */
void Set_Future()
{
    if (NULL != (system_ast->context)) {
        system_ast->context->future = 1;
    }
}

void Set_PolyorbHI_C()
{
    if (NULL != (system_ast->context)) {
        system_ast->context->polyorb_hi_c = 1;
    }
}

/* Set the root output directory in the context vector of the AST
 * and create the directory if possible */
void Set_OutDir(char *o, size_t len)
{
    if (NULL == (system_ast->context))
        return;

    build_string(&(system_ast->context)->output, o, len);

    if (((system_ast->context)->output)[len - 1] != '/'
        && ((system_ast->context)->output)[len - 1] != '\\') {
        build_string(&(system_ast->context)->output, "/", 1);
    }

    /* create the root output directory */
    if (-1 == mkdir((system_ast->context)->output, 0700)
        && EEXIST != errno) {
505
506
        ERROR("[ERROR] output directory could not be created!\n");
        ERROR("        Current directory will be used instead...\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
507
508
509
510
511
512
513
514
515
516
517
518
519
        free((system_ast->context)->output);
        (system_ast->context)->output = NULL;
    }
}

/* Set the stack size used in generated AADL files */
void Set_Stack(char *val, size_t len)
{
    if (NULL == (system_ast->context))
        return;
    build_string(&(system_ast->context->stacksize), val, len);
}

520
521
522
523
524
525
526
527
528
529
530
void Set_Instance_Of(char *component, size_t len)
{
    assert (0 < len && NULL != fv);
    build_string (&(fv->instance_of), component, len);
}

void Set_Is_Component_Type()
{
    assert (NULL != fv);
    fv->is_component_type = true;
}
Maxime Perrotin's avatar
Maxime Perrotin committed
531
532
533
534
535
536
537
538
539
540
541
542

/* End of context-related functions */


/* Process-related functions */

/* Define a new process (partition in the deployment view) */
void New_Process(char *procname,
                 size_t length,
                 char *procid,
                 size_t lenid,
                 char *node,
Maxime Perrotin's avatar
Maxime Perrotin committed
543
544
                 size_t lennode,
                 bool coverage)
Maxime Perrotin's avatar
Maxime Perrotin committed
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
{
    Create_Process(&process);

    if (NULL != process) {
        build_string(&(process->name), procname, length);
        build_string(&(process->identifier), procid, lenid);
        build_string(&(process->processor_board_name), node, lennode);
        if (processor != NULL) {
            process->cpu = processor;
            /* Append the node name to the CPU name */
            char *old_cpu_name = process->cpu->name;
            process->cpu->name = make_string ("%s_%s",
                                              process->processor_board_name,
                                              old_cpu_name);
            //free(old_cpu_name);
        }
Maxime Perrotin's avatar
Maxime Perrotin committed
561
        process->coverage = coverage;
Maxime Perrotin's avatar
Maxime Perrotin committed
562
563
564
    }
}

565
void New_Processor (char *name,       size_t name_length,
Maxime Perrotin's avatar
Maxime Perrotin committed
566
                    char *classifier, size_t classifier_length,
567
568
569
570
                    char* platform,   size_t platform_length,
                    char *envvars,    size_t envvars_length,
                    char *cflags,     size_t cflags_length,
                    char *ldflags,    size_t ldflags_length)
Maxime Perrotin's avatar
Maxime Perrotin committed
571
572
573
574
{
    processor = malloc(sizeof(Processor));
    assert(NULL != processor);

575
576
    processor->name          = NULL;
    processor->classifier    = NULL;
577
    processor->platform_name = NULL;
578
579
580
581
    processor->envvars       = NULL;
    processor->user_cflags   = NULL;
    processor->user_ldflags  = NULL;

582
583
584
    build_string(&(processor->name), name, name_length);
    build_string(&(processor->classifier), classifier,
                 classifier_length);
585
586
587
588
589
590
591
592
    if (platform_length > 0) {
        build_string(&(processor->platform_name), platform, platform_length);
    }
    if (envvars_length > 0) {
        build_string(&(processor->envvars), envvars, envvars_length);
    }
    if (cflags_length > 0) {
        build_string(&(processor->user_cflags), cflags, cflags_length);
593
    }
594
595
    if (ldflags_length > 0) {
        build_string(&(processor->user_ldflags), ldflags, ldflags_length);
596
    }
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
}


void New_Bus(char *name, size_t name_length, char *classifier,
                   size_t classifier_length)
{
    bus = malloc(sizeof(Bus));
    assert (NULL != bus);

        bus->name = NULL;
        bus->classifier = NULL;
        build_string(&(bus->name), name, name_length);
        build_string(&(bus->classifier), classifier,
                     classifier_length);
}

/* Connections in the deployment view */
void New_Connection(char *src_system, size_t src_system_length,
                    char *src_port, size_t src_port_length,
                    char *busname, size_t bus_length,
                    char *dst_system, size_t dst_system_length,
                    char *dst_port, size_t dst_port_length)
{

   src_port_length = remove_objXXX_suffix (src_port, src_port_length);
   dst_port_length = remove_objXXX_suffix (dst_port, dst_port_length);

   /* If the first 3 characters of the dst_port name are "obj" we suppose
    * it is a name generated randomly by TASTE-IV. In that case we replace
    * the port name with the name of the source port.
    */
    if (dst_port_length >= 4 && !strncmp (dst_port, "obj", 3)) {
                  dst_port = src_port;
                  dst_port_length = src_port_length;
    }

   connection = malloc(sizeof(Connection));
   assert (NULL != connection);

   connection->src_system       = NULL;
   connection->src_port         = NULL;
   connection->bus              = NULL;
   connection->dst_system       = NULL;
   connection->dst_port         = NULL;

   build_string(&(connection->src_system), src_system, 
                src_system_length);
   build_string(&(connection->src_port), src_port, 
                src_port_length);
   build_string(&(connection->bus), busname, bus_length);
   build_string(&(connection->dst_system), dst_system, 
                dst_system_length);
   build_string(&(connection->dst_port), dst_port, 
                dst_port_length);
}


void New_Drivers_Section()
{
    drivers = NULL;
}

void End_Drivers_Section()
{
    if (NULL == process) return;
    process->drivers = drivers;
    APPEND_TO_LIST (Process, (system_ast->processes), process);
    drivers = NULL;
    process = NULL;
}

void New_Device   (char *name,
                  size_t   name_length,
                  char  *classifier,
                  size_t   classifier_length,
                  char  *associated_processor,
                  size_t   associated_processor_length,
                  char  *configuration,
                  size_t   configuration_length,
                  char  *accessed_bus,
                  size_t   accessed_bus_length,
                  char  *access_port,
                  size_t   access_port_length,
                  char  *asn1_filename,
                  size_t   asn1_filename_length,
                  char  *asn1_typename,
                  size_t   asn1_typename_length, 
                  char  *asn1_modulename,
                  size_t   asn1_modulename_length)
{
    device = malloc(sizeof(Device));
    assert (NULL != device);

    device->name                    = NULL;
    device->classifier              = NULL;
    device->associated_processor    = NULL;
    device->accessed_bus            = NULL;
    device->configuration           = NULL;
    device->access_port             = NULL;
    device->asn1_filename           = NULL;
    device->asn1_typename           = NULL;
    device->asn1_modulename         = NULL;

    /* Prefix the driver name with the name of the node */
    device->name = make_string ("%s_%.*s",
                                process->processor_board_name,
                                name_length,
                                name);

    if (strncmp (configuration, "noconf", configuration_length)) {
            build_string (&(device->configuration), configuration, 
                    configuration_length);
    }

    build_string (&(device->classifier), classifier,
                    classifier_length);
    /* Prefix CPU name with the name of the node */
    device->associated_processor = make_string ("%s_%.*s",
            process->processor_board_name,
            associated_processor_length,
            associated_processor);
    build_string (&(device->accessed_bus), accessed_bus,
                    accessed_bus_length);
    build_string (&(device->access_port), access_port,
                    access_port_length);

    if (strncmp (asn1_filename, "noconf", asn1_filename_length)) {
            build_string (&(device->asn1_filename), asn1_filename,
                    asn1_filename_length);
    }

    if (strncmp (asn1_typename, "notype", asn1_typename_length)) {
            build_string (&(device->asn1_typename), asn1_typename,
                    asn1_typename_length);
    }

    if (strncmp (asn1_modulename, "nomod", asn1_typename_length)) {
            build_string (&(device->asn1_modulename), asn1_modulename,
                    asn1_modulename_length);
    }
}



/* Packages added when parsing the deployment view
 * Used to generate list of WITH statements in process.aadl
*/
void Add_Package(char *pkg_name, size_t len)
{
   Package *pack = (Package *) make_string("%.*s", len, pkg_name);
   assert (NULL != pack);
   ADD_TO_SET(Package, system_ast->packages, pack);
}

void End_Process()
{
    // Nothing to do: The process can be added to the list only after
    // all its drivers have been processed (-> in End_Drivers_Section())
}


void End_Bus()
{
    APPEND_TO_LIST (Bus, (system_ast->buses), bus);
}


void End_Device()
{
    APPEND_TO_LIST (Device, drivers, device);
}

Connection *Get_Connection()
{
    return connection;
}

void End_Connection()
{
    APPEND_TO_LIST (Connection, (system_ast->connections), connection);
}



/* Set Current Process variable in order to give the possibility
 * to add/remove some bindings to it
*/
void Set_Current_Process(Process * p)
{
    process = p;
}

/* Set Current FV variable in order to give the possibility
 * to add/remove Interfaces to it */
void Set_Current_FV(FV * f)
{
    fv = f;
}


void PrintBinding(Aplc_binding * b)
{
    printf("\t%s\n", b->fv->name);
}


void Dump_Bindings()
{
    printf("process \"%s\" bindings: \n", process->name);
    FOREACH(b, Aplc_binding, process->bindings, {
            printf("\t%s\n", b->fv->name);})
}

void Add_Binding(char *b, size_t length)
{
    FV *result_fv = NULL;
    Create_Aplc_binding(&binding);

    if (NULL == binding) {
816
        ERROR ("[ERROR] in Add_Binding->Create_Aplc_binding\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
        return;
    }

    /* Keep compatibility with 1.2 - remove objXXX suffix */
    length = remove_objXXX_suffix (b, length);

    binding_name = NULL;
    build_string(&binding_name, b, length);

    result_fv = FindFV(binding_name);

    /* Add the binding to the list (only for active functions
     * i.e. not an unprotected/protected one) */
    if (NULL != result_fv) {

        binding->fv = result_fv;
        APPEND_TO_LIST(Aplc_binding, process->bindings, binding);
        result_fv->process = process;
    } else {
836
        ERROR ("[ERROR] Binding references a non-existing FV...(%s)\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
               binding_name);
        add_error();
    }

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

/* End of process-related functions */

/* FV-related functions */

/* Create a new functional view (starting point) */
FV *New_FV(char *fv_name, size_t length, char *caseSensitive)
{
    Create_FV(&fv);
    assert (NULL != fv);

    build_string(&(fv->name), fv_name, length);
    build_string(&(fv->nameWithCase), caseSensitive, length);

    return fv;
}

void Set_Interface_Queue_Size (const unsigned long long s)
{
   if (interface != NULL) {
      interface->queue_size = s;
   }
}

/* New interface: set the name and distant FV to which it is connected */
Maxime Perrotin's avatar
Maxime Perrotin committed
871
872
void New_Interface(char *name,         size_t length,
                   char *dist_fv,      size_t distant_length,
Maxime Perrotin's avatar
Maxime Perrotin committed
873
874
875
876
877
                   char *distant_name, size_t dist_name_length,
                   IF_type direction)
{
    interface = NULL;
    Create_Interface(&interface);
Maxime Perrotin's avatar
Maxime Perrotin committed
878
879
880
    assert (NULL != interface);

    build_string(&(interface->name), name, length);
Maxime Perrotin's avatar
Maxime Perrotin committed
881

Maxime Perrotin's avatar
Maxime Perrotin committed
882
883
884
885
886
887
888
889
    /* RI can have a local identifier which may be different from
     * the corresponding PI it is connected to. In that case we set
     * the "distant_name" field of the interface to make sure that
     *  the connection will be properly handled
     */
    if (NULL != distant_name) {
        build_string (&(interface->distant_name),
                      distant_name, dist_name_length);
Maxime Perrotin's avatar
Maxime Perrotin committed
890
    }
Maxime Perrotin's avatar
Maxime Perrotin committed
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915

    /* Set the port name as the interface name, so that if later
     * the interface name is changed, we still know to which port we
     * have to connect it (in case of distributed systems */
    build_string (&(interface->port_name), name, length);

    if (distant_length > 0) {
        build_string (&(interface->distant_fv), dist_fv, distant_length);
    }
    else {
        interface->distant_fv = NULL;
    }
    interface->direction      = direction;
    interface->wcet_low       = 0;
    interface->wcet_high      = 0;
    interface->wcet_low_unit  = NULL;
    interface->wcet_high_unit = NULL;
    interface->queue_size     = 1;

    /* ignore params will be kept to true if all callers of a given PI
     * are located in the same node (binary) - in that case the parameters
     * will not be copied from the sender to the receiver through the orb
     * but made available directly through global pointers.
     */
    interface->ignore_params = true;
Maxime Perrotin's avatar
Maxime Perrotin committed
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
}

/* New Provided interface */
void Add_PI(char *pi, size_t length)
{
   length = remove_objXXX_suffix (pi, length);

   /* Parameters 3 is NULL because a PI's distant FV is irrelevant
    * (there can be several) and parameter 5 is NULL because a PI's
    *  distant name is also irrelevant since there can also
    *  be several callers (thus several different distant names).
   */
   New_Interface(pi, length, NULL, 0, NULL, 0, PI);
}

/* New Required Interface */
void Add_RI(char *ri, size_t length,
            char *dist_fv, size_t distant_length,
            char *dist_name, size_t dist_name_length)
{
Maxime Perrotin's avatar
Maxime Perrotin committed
936
937
   size_t tmp             = 0; 
   char   *local_name     = ri;
Maxime Perrotin's avatar
Maxime Perrotin committed
938
939
940
941
942

   assert (NULL != ri);
   assert (NULL != dist_fv);
   assert (NULL != dist_name);

Maxime Perrotin's avatar
Maxime Perrotin committed
943
   tmp = remove_objXXX_suffix (ri, length);
Maxime Perrotin's avatar
Maxime Perrotin committed
944
945
946
947
948
   dist_name_length = remove_objXXX_suffix (dist_name, dist_name_length);

   /* If the first 3 characters of the local RI name are "obj" we suppose
    * it is a name generated randomly by TASTE-IV. In that case we replace
    * the local RI name with the name of the remote PI.
Maxime Perrotin's avatar
Maxime Perrotin committed
949
950
    * This is kept only for backward compatibility
    */
Maxime Perrotin's avatar
Maxime Perrotin committed
951
952
953
954
955
956
957
958
959
960
961
962
   if (tmp >= 4 && !strncmp (ri, "obj", 3)) {
        local_name = dist_name;
        length = dist_name_length;
   }
   else length = tmp;

   New_Interface(local_name, length,
                 dist_fv, distant_length,
                 dist_name, dist_name_length,
                 RI);
}

Maxime Perrotin's avatar
Maxime Perrotin committed
963
/* add an IN parameter to the list (name respects the case) */
Maxime Perrotin's avatar
Maxime Perrotin committed
964
965
966
void Add_In_Param(char *name,     size_t l1,
                  char *type,     size_t l2,
                  char *module,   size_t l3,
Maxime Perrotin's avatar
Maxime Perrotin committed
967
968
969
970
971
972
                  char *filename, size_t l4)
{
    type = asn2underscore(type, l2);

    Create_Parameter(&parameter);
    assert (NULL != parameter && NULL != interface);
Maxime Perrotin's avatar
Maxime Perrotin committed
973
    char *param_name = name;
Maxime Perrotin's avatar
Maxime Perrotin committed
974

Maxime Perrotin's avatar
Maxime Perrotin committed
975
976
977
978
979
980
    if (0 == system_ast->context->keep_case) {
        // user does not want to keep case of the parameters
        param_name = string_to_lower(name);
    }

    build_string(&(parameter->name), param_name, l1);
Maxime Perrotin's avatar
Maxime Perrotin committed
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
    build_string(&(parameter->type), type, l2);
    build_string(&(parameter->asn1_module), module, l3);
    build_string(&(parameter->asn1_filename), filename, l4);
    parameter->interface = interface;
    parameter->param_direction = param_in;

    Add_Param(&parameter, &(interface->in));
}

/* add an OUT parameter to the list */
void Add_Out_Param(char *name, size_t l1, 
                   char *type, size_t l2, 
                   char *module, size_t l3,
                   char *filename, size_t l4)
{
    type = asn2underscore(type, l2);

    Create_Parameter(&parameter);
    assert (NULL != parameter && NULL != interface);
Maxime Perrotin's avatar
Maxime Perrotin committed
1000
1001
1002
1003
1004
1005
    char *param_name = name;

    if (0 == system_ast->context->keep_case) {
        // user does not want to keep case of the parameters
        param_name = string_to_lower(name);
    }
Maxime Perrotin's avatar
Maxime Perrotin committed
1006

Maxime Perrotin's avatar
Maxime Perrotin committed
1007
    build_string(&(parameter->name), param_name, l1);
Maxime Perrotin's avatar
Maxime Perrotin committed
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
    build_string(&(parameter->type), type, l2);
    build_string(&(parameter->asn1_module), module, l3);
    build_string(&(parameter->asn1_filename), filename, l4);
    parameter->interface = interface;
    parameter->param_direction = param_out;

    Add_Param(&parameter, &(interface->out));
}

void End_IF()
{
    if (NULL == fv || NULL == interface)
        return;
    /*
       Determine if the interface is immediate or deffered
       It is deduced fromm the RCMOperationKind attribute
     */

    if (unknown == interface->synchronism) {
        switch (interface->rcm) {
        case cyclic:
        case sporadic:
        case variator:
            interface->synchronism = asynch;
            break;
        case protected:
        case unprotected:
            interface->synchronism = synch;
            break;
        default:
            if (NULL == interface->out) {
                interface->synchronism = asynch;
            } else {
                interface->synchronism = synch;
            }
            break;
        }
    }

    interface->parent_fv = fv;

    /* Check if this is the first interface of this FV
     * If not, and if op_kind=sporadic, then change it to variator
     * MP 20/04/10: variators are deprecated with the new VT rules
     * that create a thread per PI as soon as there is more than
     * one "active" (spo, var, cyc) PI in a function.
     */
    if (NULL != fv->interfaces) {
        if (sporadic == interface->rcm && PI == interface->direction) {
            interface->rcm = variator;
        }
    }

    /* Add new interface to the list contained in the Function definition */
    APPEND_TO_LIST (Interface, fv->interfaces, interface);
}

/* Last function to call: add the new FV to the System AST */
void End_FV()
{
    assert (NULL != fv);

    /* Check if FV has timers - store in list */
    fv->timer_list = timers(fv);

    fv->system_ast = system_ast;

    /* Determine if the function is a thread or a passive function
     * (thread if at least 1 active PI) */
    fv->runtime_nature = passive_runtime;
    FOREACH(i, Interface, fv->interfaces, {
            if (PI == i->direction
                && (sporadic == i->rcm || cyclic == i->rcm
                    || variator == i->rcm)) fv->runtime_nature =
            thread_runtime;}
    )

    APPEND_TO_LIST(FV, system_ast->functions, fv);

    fv = NULL;
}

/* End of FV-related functions */


/* AST helper functions */


/*  Functions to find a FV or an Interface based on a char string */
void SetSearchName(char *name)
{
    search_name = name;
}

/* Find FV functions */
void CompareFVname(FV * fv_local, FV ** result)
{
    if (NULL == fv_local->name || NULL == search_name)
        return;

    if (!strcmp(fv_local->name, search_name)) {
        *result = fv_local;
        SetSearchName(NULL);
    }
}

Maxime Perrotin's avatar
Maxime Perrotin committed
1114
/* main function to be called */
Maxime Perrotin's avatar
Maxime Perrotin committed
1115
FV *FindFV(char *fv_name)
Maxime Perrotin's avatar
Maxime Perrotin committed
1116
{
Maxime Perrotin's avatar
Maxime Perrotin committed
1117
1118
1119
    FV *result_fv = NULL;
    SetSearchName(fv_name);
    FOREACH(f, FV, system_ast->functions, {
Maxime Perrotin's avatar
Maxime Perrotin committed
1120
1121
1122
        CompareFVname(f, &result_fv);
    });
    SetSearchName(NULL);
Maxime Perrotin's avatar
Maxime Perrotin committed
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151

    return result_fv;
}

/* End Find FV functions */

/* FindInterface of a given FV */
void CompareIFname(Interface * i, Interface ** result)
{
    if (NULL == i->name || NULL == search_name)
        return;

    if (!strcmp(i->name, search_name)) {
        *result = i;
        SetSearchName(NULL);
    }
}

Interface *FindInterface(FV * function, char *interface_name)
{
    Interface *result = NULL;
    SetSearchName(interface_name);
    FOREACH(i, Interface, function->interfaces, {
            CompareIFname(i, &result);})
        SetSearchName(NULL);

    return result;
}

1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
/* FindParameter of a given Interface */
void CompareParName(Parameter * p, Parameter ** result)
{
    if (NULL == p->name || NULL == search_name)
        return;

    if (!strcmp(p->name, search_name)) {
        *result = p;
        SetSearchName(NULL);
    }
}

Parameter *FindInParameter(Interface *i, char *param_name)
{
    Parameter *result = NULL;
    SetSearchName(param_name);
    FOREACH(p, Parameter, i->in, {
            CompareParName(p, &result);})
        SetSearchName(NULL);

    return result;
}

Parameter *FindOutParameter(Interface * i, char *param_name)
{
    Parameter *result = NULL;
    SetSearchName(param_name);
    FOREACH(p, Parameter, i->out, {
            CompareParName(p, &result);})
        SetSearchName(NULL);

    return result;
}

Maxime Perrotin's avatar
Maxime Perrotin committed
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
/* Given a PI (from any function) and a known caller, find the corresponding
 * RI in the remote function
*/
Interface *FindCorrespondingRI(FV *remote, Interface *pi)
{
    FOREACH (i, Interface, remote->interfaces, {
        if (RI == i->direction &&
            !strcmp (i->distant_name, pi->name) &&
            !strcmp (i->distant_fv, pi->parent_fv->name)) {
            return i;
        }
    });
    return NULL;
}

Maxime Perrotin's avatar
Maxime Perrotin committed
1201
1202
/* End Find Interface functions */

Maxime Perrotin's avatar
Maxime Perrotin committed
1203
/* Return the list of FV calling a given PI
Maxime Perrotin's avatar
Maxime Perrotin committed
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
   the conditions are, for each RI of each FV that :
        - RI's distant name = PI's name
        - RI's distant FV = PI's parent FV
*/
FV_list *Find_All_Calling_FV(Interface * i)
{
    FV_list *result = NULL;
    bool match = false;

    FOREACH (function, FV, system_ast->functions, {
        match = false;
        FOREACH(iface, Interface, function->interfaces, {
            if (false == match) {
                if (RI == iface->direction) {
                    if (iface->distant_name == NULL ||
                       iface->distant_fv == NULL) {
                        printf("[ERROR] Required Interface %s in function %s is not "
                               "connected. Glue code cannot be generated.\n",
                               iface->name, function->name);
                        add_error();
                    }
                }
1226
1227
1228
                if (NULL != iface->distant_name && NULL != i->name &&
                    NULL != iface->distant_fv && NULL != i->parent_fv &&
                    RI == iface->direction &&
Maxime Perrotin's avatar
Maxime Perrotin committed
1229
1230
1231
1232
1233
1234
1235
1236
1237
                    !strcmp (iface->distant_name, i->name) &&
                    !strcmp (iface->distant_fv, i->parent_fv->name))
                        match = true;
            }
        })
        if (true == match) {
            APPEND_TO_LIST (FV, result, function);
        }
    })
Maxime Perrotin's avatar
Maxime Perrotin committed
1238
    if (get_context()->test && NULL != result) {
Maxime Perrotin's avatar
Maxime Perrotin committed
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
        printf("\n[Interface %s in FV %s] is called by:\n",
                i->name,
                i->parent_fv->name);
        PRINT_LIST (FV, result);
    }

    return result;
}

/* End Find Calling PI functions */

/* First function to call to initialize the AST */
void C_Init()
{
    Create_System(&system_ast);
}

/* Remove the current system AST from memory */
void Delete_System_AST()
{
    Clear_System(system_ast);
}

Maxime Perrotin's avatar
Maxime Perrotin committed
1262
1263
1264
/* Get dataview file name based (same as dataview.aadl
 * passed with -d but replace with .asn extension
*/
Maxime Perrotin's avatar
Maxime Perrotin committed
1265
1266
1267
1268
1269
char *getASN1DataView()
{
    char *p = strrchr (get_context()->dataview,'/');
    if (NULL == p) p = get_context()->dataview - 1;

Maxime Perrotin's avatar
Maxime Perrotin committed
1270
    return make_string ("%.*s.asn", strlen (p + 1) - strlen (".aadl"), p + 1);
Maxime Perrotin's avatar
Maxime Perrotin committed
1271
}
Maxime Perrotin's avatar
Maxime Perrotin committed
1272

Maxime Perrotin's avatar
Maxime Perrotin committed
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
/* Get dataView path */
char *getDataViewPath()
{
    char *p = strrchr (get_context()->dataview,'/');

    if (NULL == p) return make_string (".");

    else return make_string
                ("%.*s",
                p - get_context()->dataview,
                get_context()->dataview);
}

/* Debug functions */
void Print_Interface(Interface * i)
{
    printf("*** %s interface \"%s\"\n",
           (PI == i->direction) ? "provided" : "required", i->name);
    printf("\tparent_fv = %s\n", i->parent_fv->name);
    printf("\tsynchronism = %s\n",
           (synch == i->synchronism) ? "sync" : "async");
    printf("\trcm = ");
    switch (i->rcm) {
    case cyclic:
        printf("cyclic\n");
        break;
    case sporadic:
        printf("sporadic\n");
        break;
    case variator:
        printf("variator\n");
        break;
    case protected:
        printf("protected\n");
        break;
    case unprotected:
        printf("unprotected\n");
        break;
    case undefined:
        printf("undefined\n");
        break;
    }
    printf("\tperiod/miat = %lld\n", i->period);
    printf("\tdistant_fv = %s\n", i->distant_fv);
    printf("\tdistant_name = %s\n", i->distant_name);
Maxime Perrotin's avatar
Maxime Perrotin committed
1318
    printf("\twcet = %llu %s .. %llu %s\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
1319
1320
1321
1322
           (unsigned long long) i->wcet_low,
           i->wcet_low_unit,
           (unsigned long long) i->wcet_high,
           i->wcet_high_unit);
Maxime Perrotin's avatar
Maxime Perrotin committed
1323
   printf("\tignore params = %s\n\n", i->ignore_params ? "true": "false");
Maxime Perrotin's avatar
Maxime Perrotin committed
1324
1325
1326
1327
1328
1329
1330
1331
}

void Dump_Interfaces(Interface_list * l)
{
    FOREACH(i, Interface, l, {
            Print_Interface(i);})
}
/* End AST builder functions */