scade6_B_mapper.py 29.1 KB
Newer Older
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
#
# (C) Semantix Information Technologies.
#
# Semantix Information Technologies is licensing the code of the
# Data Modelling Tools (DMT) in the following dual-license mode:
#
# Commercial Developer License:
#       The DMT Commercial Developer License is the suggested version
# to use for the development of proprietary and/or commercial software.
# This version is for developers/companies who do not want to comply
# with the terms of the GNU Lesser General Public License version 2.1.
#
# GNU LGPL v. 2.1:
#       This version of DMT is the one to use for the development of
# applications, when you are willing to comply with the terms of the
# GNU Lesser General Public License version 2.1.
#
# Note that in both cases, there are no charges (royalties) for the
# generated code.
#
'''
This is the code generator for the SCADE6 code mappers.
This backend is called by aadl2glueC, when a SCADE5 or SCADE6
subprogram is identified in the input concurrency view.

This code generator supports both UPER/ACN and Native encodings,
and also supports UPER/ACN using both ASN1SCC and Nokalva.

SCADE is a member of the synchronous "club" (Simulink, etc) ;
The subsystem developer (or rather, the APLC developer) is
building a model in SCADE, and the generated code is offered
in the form of a "function" that does all the work.
To that end, we create "glue" functions for input and output
parameters, which have C callable interfaces. The necessary
stubs (to allow calling from the VM side) are also generated.
'''

38
39
from typing import List

40
from ..commonPy.utility import panic, panicWithCallStack
41
from ..commonPy.asnAST import (
42
43
44
    isSequenceVariable, sourceSequenceLimit, AsnInt, AsnBool, AsnReal, AsnBasicNode,
    AsnEnumerated, AsnOctetString, AsnChoice, AsnSequenceOrSet, AsnSequenceOrSetOf,
    AsnNode, AsnSet, AsnSequence, AsnSetOf, AsnSequenceOf)
45
from ..commonPy.asnParser import AST_Lookup, AST_Leaftypes
46
from ..commonPy.aadlAST import AadlPort, AadlParameter, ApLevelContainer, Param
47

48
from ..commonPy.recursiveMapper import RecursiveMapper
49
50
51
52
53
54
from .synchronousTool import SynchronousToolGlueGenerator

isAsynchronous = False
scadeBackend = None


55
# noinspection PyListCreation
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
56
# pylint: disable=no-self-use
57
class FromSCADEtoASN1SCC(RecursiveMapper):
58
    def MapInteger(self, srcScadeMacro: str, destVar: str, _: AsnInt, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
59
60
        return ["%s = (asn1SccSint) %s;\n" % (destVar, srcScadeMacro)]

61
    def MapReal(self, srcScadeMacro: str, destVar: str, _: AsnReal, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
62
63
        return ["%s = (double) %s;\n" % (destVar, srcScadeMacro)]

64
    def MapBoolean(self, srcScadeMacro: str, destVar: str, _: AsnBool, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
65
66
        return ["%s = (int)%s;\n" % (destVar, srcScadeMacro)]

67
    def MapOctetString(self, srcScadeMacro: str, destVar: str, node: AsnOctetString, _: AST_Leaftypes, __: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
68
        lines = []  # type: List[str]
69
        lines.append("{\n")
70
        if not node._range:
71
72
73
74
75
76
77
78
79
80
            panicWithCallStack(
                "OCTET STRING (in %s) must have a SIZE constraint "  # pragma: no cover
                "inside ASN.1,\nor else SCADE can't generate C code!" % node.Location())  # pragma: no cover
        for i in range(0, node._range[-1]):
            lines.append("    %s.arr[%d] = %s[%d];\n" % (destVar, i, srcScadeMacro, i))
        if isSequenceVariable(node):
            lines.append("    %s.nCount = %d;\n" % (destVar, node._range[-1]))
        lines.append("}\n")
        return lines

81
    def MapEnumerated(self, srcScadeMacro: str, destVar: str, _: AsnEnumerated, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
82
83
        return ["%s = %s;\n" % (destVar, srcScadeMacro)]

84
    def MapSequence(self, srcScadeMacro: str, destVar: str, node: AsnSequenceOrSet, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
85
        lines = []  # type: List[str]
86
87
88
89
90
91
92
93
94
95
        for child in node._members:
            lines.extend(
                self.Map(
                    "%s.%s" % (srcScadeMacro, self.CleanName(child[0])),
                    destVar + "." + self.CleanName(child[0]),
                    child[1],
                    leafTypeDict,
                    names))
        return lines

96
    def MapSet(self, srcScadeMacro: str, destVar: str, node: AsnSequenceOrSet, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
97
98
        return self.MapSequence(srcScadeMacro, destVar, node, leafTypeDict, names)  # pragma: nocover  # pragma: nocover

99
    def MapChoice(self, srcScadeMacro: str, destVar: str, node: AsnChoice, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
100
        lines = []  # type: List[str]
101
102
103
104
105
        childNo = 0
        for child in node._members:
            childNo += 1
            lines.append("%sif (%s.choiceIdx == %d) {\n" %
                         (self.maybeElse(childNo), srcScadeMacro, childNo))
106
            lines.extend(
107
                ['    ' + x
108
109
110
111
112
113
                 for x in self.Map(
                     "%s.%s" % (srcScadeMacro, self.CleanName(child[0])),
                     destVar + ".u." + self.CleanName(child[0]),
                     child[1],
                     leafTypeDict,
                     names)])
114
115
116
117
            lines.append("    %s.kind = %s;\n" % (destVar, self.CleanName(child[2])))
            lines.append("}")
        return lines

118
    def MapSequenceOf(self, srcScadeMacro: str, destVar: str, node: AsnSequenceOrSetOf, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
119
        if not node._range:
120
121
122
            panicWithCallStack(
                "A SIZE constraint is required, or else SCADE can't generate C code (%s)!\n" %   # pragma: no cover
                node.Location())  # pragma: no cover
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
123
        lines = []  # type: List[str]
124
        for i in range(0, node._range[-1]):
125
126
            lines.extend(
                self.Map("%s[%d]" % (srcScadeMacro, i),
127
128
129
130
131
132
133
134
                         "%s.arr[%d]" % (destVar, i),
                         node._containedType,
                         leafTypeDict,
                         names))
        if isSequenceVariable(node):
            lines.append("%s.nCount = %d;\n" % (destVar, node._range[-1]))
        return lines

135
    def MapSetOf(self, srcScadeMacro: str, destVar: str, node: AsnSequenceOrSetOf, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
136
137
138
        return self.MapSequenceOf(srcScadeMacro, destVar, node, leafTypeDict, names)  # pragma: nocover


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
139
# pylint: disable=no-self-use
140
class FromASN1SCCtoSCADE(RecursiveMapper):
141
    def __init__(self) -> None:
142
143
        self._seqIndex = 1

144
    def MapInteger(self, srcVar: str, dstScadeMacro: str, _: AsnInt, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
145
146
        return ["%s = %s;\n" % (dstScadeMacro, srcVar)]

147
    def MapReal(self, srcVar: str, dstScadeMacro: str, _: AsnReal, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
148
149
        return ["%s = %s;\n" % (dstScadeMacro, srcVar)]

150
    def MapBoolean(self, srcVar: str, dstScadeMacro: str, _: AsnBool, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
151
152
        return ["%s = (%s)?1:0;\n" % (dstScadeMacro, srcVar)]

153
    def MapOctetString(self, srcVar: str, dstScadeMacro: str, node: AsnOctetString, _: AST_Leaftypes, __: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
154
        if not node._range:
155
156
157
            panicWithCallStack(
                "OCTET STRING (in %s) must have a SIZE constraint "  # pragma: no cover
                "inside ASN.1,\nor else SCADE can't generate C code!" % node.Location())  # pragma: no cover
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
158
        lines = []  # type: List[str]
159
        limit = sourceSequenceLimit(node, srcVar)
160
161
        # for i in xrange(0, node._range[-1]):
        #     lines.append("%s[%d] = %s->buf[%d];\n" % (dstScadeMacro, i, srcVar, i))
162
163
164
165
166
167
168
        lines.append("{\n")
        lines.append("    int i;\n")
        lines.append("    for(i=0; i<%s; i++) %s[i] = %s.arr[i];\n" % (limit, dstScadeMacro, srcVar))
        lines.append("    while(i<%d) { %s[i]=0; i++; }\n" % (node._range[-1], dstScadeMacro))
        lines.append("}\n")
        return lines

169
    def MapEnumerated(self, srcVar: str, dstScadeMacro: str, _: AsnEnumerated, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
170
171
        return ["%s = %s;\n" % (dstScadeMacro, srcVar)]

172
    def MapSequence(self, srcVar: str, dstScadeMacro: str, node: AsnSequenceOrSet, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
173
        lines = []  # type: List[str]
174
175
176
177
178
179
180
181
182
183
        for child in node._members:
            lines.extend(
                self.Map(
                    srcVar + "." + self.CleanName(child[0]),
                    "%s.%s" % (dstScadeMacro, self.CleanName(child[0])),
                    child[1],
                    leafTypeDict,
                    names))
        return lines

184
    def MapSet(self, srcVar: str, dstScadeMacro: str, node: AsnSequenceOrSet, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
185
186
        return self.MapSequence(srcVar, dstScadeMacro, node, leafTypeDict, names)  # pragma: nocover

187
    def MapChoice(self, srcVar: str, dstScadeMacro: str, node: AsnChoice, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
188
        lines = []  # type: List[str]
189
190
191
192
193
        childNo = 0
        for child in node._members:
            childNo += 1
            lines.append("%sif (%s.kind == %s) {\n" %
                         (self.maybeElse(childNo), srcVar, self.CleanName(child[2])))
194
            lines.extend(
195
                ['    ' + x
196
197
198
199
200
201
                 for x in self.Map(
                     srcVar + ".u." + self.CleanName(child[0]),
                     "%s.%s" % (dstScadeMacro, self.CleanName(child[0])),
                     child[1],
                     leafTypeDict,
                     names)])
202
203
204
205
            lines.append("    %s.choiceIdx = %d;\n" % (dstScadeMacro, childNo))
            lines.append("}\n")
        return lines

206
    def MapSequenceOf(self, srcVar: str, dstScadeMacro: str, node: AsnSequenceOrSetOf, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
207
        if not node._range:
208
209
210
            panicWithCallStack(
                "A SIZE constraint is required or else SCADE can't generate C code (%s)!\n" %   # pragma: no cover
                node.Location())  # pragma: no cover
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
211
        lines = []  # type: List[str]
212
213
214
215
216
217
218
        seqIndex = self._seqIndex
        self._seqIndex += 1
        lines.append("{\n")
        lines.append("    int i%d;\n" % seqIndex)
        # Bugfix for Astrium/Nicolas Gillet: always use the array max conf
        # lines.append("    for(i%d=0; i%d<%s.nCount; i%d++) {\n" % (seqIndex, seqIndex, srcVar, seqIndex))
        lines.append("    for(i%d=0; i%d<%d; i%d++) {\n" % (seqIndex, seqIndex, node._range[-1], seqIndex))
219
        lines.extend(
220
            ["        " + x
221
222
223
224
225
226
             for x in self.Map(
                 srcVar + ".arr[i%d]" % seqIndex,
                 "%s[i%d]" % (dstScadeMacro, seqIndex),
                 node._containedType,
                 leafTypeDict,
                 names)])
227
228
229
230
231
        lines.append("    }\n")
        # How to reset the remaining elements in SCADE? No idea...
        lines.append("}\n")
        return lines

232
    def MapSetOf(self, srcVar: str, dstScadeMacro: str, node: AsnSequenceOrSetOf, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
233
234
235
        return self.MapSequenceOf(srcVar, dstScadeMacro, node, leafTypeDict, names)  # pragma: nocover


236
# noinspection PyListCreation
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
237
# pylint: disable=no-self-use
238
class FromSCADEtoOSS(RecursiveMapper):
239
    def MapInteger(self, srcScadeMacro: str, destVar: str, _: AsnInt, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
240
241
        return ["%s = %s;\n" % (destVar, srcScadeMacro)]

242
    def MapReal(self, srcScadeMacro: str, destVar: str, _: AsnReal, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
243
244
        return ["%s = %s;\n" % (destVar, srcScadeMacro)]

245
    def MapBoolean(self, srcScadeMacro: str, destVar: str, _: AsnBool, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
246
247
        return ["%s = (char)%s;\n" % (destVar, srcScadeMacro)]

248
    def MapOctetString(self, srcScadeMacro: str, destVar: str, node: AsnOctetString, _: AST_Leaftypes, __: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
249
        lines = []  # type: List[str]
250
        lines.append("{\n")
251
        if not node._range:
252
253
254
255
256
257
258
259
260
            panicWithCallStack(
                "OCTET STRING (in %s) must have a SIZE constraint "  # pragma: no cover
                "inside ASN.1,\nor else SCADE can't generate C code!" % node.Location())  # pragma: no cover
        for i in range(0, node._range[-1]):
            lines.append("    %s.value[%d] = %s[%d];\n" % (destVar, i, srcScadeMacro, i))
        lines.append("    %s.length = %d;\n" % (destVar, node._range[-1]))
        lines.append("}\n")
        return lines

261
    def MapEnumerated(self, srcScadeMacro: str, destVar: str, _: AsnEnumerated, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
262
263
        return ["%s = %s;\n" % (destVar, srcScadeMacro)]

264
    def MapSequence(self, srcScadeMacro: str, destVar: str, node: AsnSequenceOrSet, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
265
        lines = []  # type: List[str]
266
267
268
269
270
271
272
273
274
275
        for child in node._members:
            lines.extend(
                self.Map(
                    "%s.%s" % (srcScadeMacro, self.CleanName(child[0])),
                    destVar + "." + self.CleanName(child[0]),
                    child[1],
                    leafTypeDict,
                    names))
        return lines

276
    def MapSet(self, srcScadeMacro: str, destVar: str, node: AsnSequenceOrSet, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
277
278
        return self.MapSequence(srcScadeMacro, destVar, node, leafTypeDict, names)  # pragma: nocover

279
    def MapChoice(self, srcScadeMacro: str, destVar: str, node: AsnChoice, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
280
        lines = []  # type: List[str]
281
282
283
284
285
        childNo = 0
        for child in node._members:
            childNo += 1
            lines.append("%sif (%s.choiceIdx == %d) {\n" %
                         (self.maybeElse(childNo), srcScadeMacro, childNo))
286
            lines.extend(
287
                ['    ' + x
288
289
290
291
292
293
                 for x in self.Map(
                     "%s.%s" % (srcScadeMacro, self.CleanName(child[0])),
                     destVar + ".u." + self.CleanName(child[0]),
                     child[1],
                     leafTypeDict,
                     names)])
294
295
296
297
            lines.append("    %s.choice = OSS_%s_chosen;\n" % (destVar, self.CleanName(child[0])))
            lines.append("}")
        return lines

298
    def MapSequenceOf(self, srcScadeMacro: str, destVar: str, node: AsnSequenceOrSetOf, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
299
        if not node._range:
300
301
302
            panicWithCallStack(
                "A SIZE constraint is required, or else SCADE can't generate C code (%s)!\n" %   # pragma: no cover
                node.Location())  # pragma: no cover
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
303
        lines = []  # type: List[str]
304
        for i in range(0, node._range[-1]):
305
306
            lines.extend(
                self.Map("%s[%d]" % (srcScadeMacro, i),
307
308
309
310
311
312
313
                         "%s.value[%d]" % (destVar, i),
                         node._containedType,
                         leafTypeDict,
                         names))
        lines.append("%s.count = %d;\n" % (destVar, node._range[-1]))
        return lines

314
    def MapSetOf(self, srcScadeMacro: str, destVar: str, node: AsnSequenceOrSetOf, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
315
316
317
        return self.MapSequenceOf(srcScadeMacro, destVar, node, leafTypeDict, names)  # pragma: nocover


318
# noinspection PyListCreation
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
319
# pylint: disable=no-self-use
320
class FromOSStoSCADE(RecursiveMapper):
321
    def __init__(self) -> None:
322
323
        self._seqIndex = 1

324
    def MapInteger(self, srcVar: str, dstScadeMacro: str, _: AsnInt, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
325
326
        return ["%s = %s;\n" % (dstScadeMacro, srcVar)]

327
    def MapReal(self, srcVar: str, dstScadeMacro: str, _: AsnReal, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
328
329
        return ["%s = %s;\n" % (dstScadeMacro, srcVar)]

330
    def MapBoolean(self, srcVar: str, dstScadeMacro: str, _: AsnBool, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
331
332
        return ["%s = (%s)?1:0;\n" % (dstScadeMacro, srcVar)]

333
    def MapOctetString(self, srcVar: str, dstScadeMacro: str, node: AsnOctetString, _: AST_Leaftypes, __: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
334
        if not node._range:
335
336
337
            panicWithCallStack(
                "OCTET STRING (in %s) must have a SIZE constraint "  # pragma: no cover
                "inside ASN.1,\nor else SCADE can't generate C code!" % node.Location())  # pragma: no cover
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
338
        lines = []  # type: List[str]
339
340
341
342
343
344
345
        lines.append("{\n")
        lines.append("    int i;\n")
        lines.append("    for(i=0; i<%s.length; i++) %s[i] = %s.value[i];\n" % (srcVar, dstScadeMacro, srcVar))
        lines.append("    while(i<%d) { %s[i]=0; i++; }\n" % (node._range[-1], dstScadeMacro))
        lines.append("}\n")
        return lines

346
    def MapEnumerated(self, srcVar: str, dstScadeMacro: str, _: AsnEnumerated, __: AST_Leaftypes, ___: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
347
348
        return ["%s = %s;\n" % (dstScadeMacro, srcVar)]

349
    def MapSequence(self, srcVar: str, dstScadeMacro: str, node: AsnSequenceOrSet, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
350
        lines = []  # type: List[str]
351
352
353
354
355
356
357
358
359
360
        for child in node._members:
            lines.extend(
                self.Map(
                    srcVar + "." + self.CleanName(child[0]),
                    "%s.%s" % (dstScadeMacro, self.CleanName(child[0])),
                    child[1],
                    leafTypeDict,
                    names))
        return lines

361
    def MapSet(self, srcVar: str, dstScadeMacro: str, node: AsnSequenceOrSet, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
362
363
        return self.MapSequence(srcVar, dstScadeMacro, node, leafTypeDict, names)  # pragma: nocover

364
    def MapChoice(self, srcVar: str, dstScadeMacro: str, node: AsnChoice, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
365
        lines = []  # type: List[str]
366
367
368
369
370
        childNo = 0
        for child in node._members:
            childNo += 1
            lines.append("%sif (%s.choice == OSS_%s_chosen) {\n" %
                         (self.maybeElse(childNo), srcVar, self.CleanName(child[0])))
371
            lines.extend(
372
                ['    ' + x
373
374
375
376
377
378
                 for x in self.Map(
                     srcVar + ".u." + self.CleanName(child[0]),
                     "%s.%s" % (dstScadeMacro, self.CleanName(child[0])),
                     child[1],
                     leafTypeDict,
                     names)])
379
380
381
382
            lines.append("    %s.choiceIdx = %d;\n" % (dstScadeMacro, childNo))
            lines.append("}\n")
        return lines

383
    def MapSequenceOf(self, srcVar: str, dstScadeMacro: str, node: AsnSequenceOrSetOf, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
384
        if not node._range:
385
386
387
            panicWithCallStack(
                "A SIZE constraint is required or else SCADE can't generate C code (%s)!\n" %   # pragma: no cover
                node.Location())  # pragma: no cover
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
388
        lines = []  # type: List[str]
389
390
391
392
393
        seqIndex = self._seqIndex
        self._seqIndex += 1
        lines.append("{\n")
        lines.append("    int i%d;\n" % seqIndex)
        lines.append("    for(i%d=0; i%d<%s.count; i%d++) {\n" % (seqIndex, seqIndex, srcVar, seqIndex))
394
        # for i in xrange(0, node._range[-1]):
395
        lines.extend(
396
            ["        " + x
397
398
399
400
401
402
             for x in self.Map(
                 srcVar + ".value[i%d]" % seqIndex,
                 "%s[i%d]" % (dstScadeMacro, seqIndex),
                 node._containedType,
                 leafTypeDict,
                 names)])
403
404
405
406
407
        lines.append("    }\n")
        # How to reset the remaining elements in SCADE? No idea...
        lines.append("}\n")
        return lines

408
    def MapSetOf(self, srcVar: str, dstScadeMacro: str, node: AsnSequenceOrSetOf, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> List[str]:  # pylint: disable=invalid-sequence-index
409
410
411
412
        return self.MapSequenceOf(srcVar, dstScadeMacro, node, leafTypeDict, names)  # pragma: nocover


class ScadeGlueGenerator(SynchronousToolGlueGenerator):
413
    def Version(self) -> None:
414
415
        print("Code generator: " + "$Id: scade6_B_mapper.py 2390 2012-07-19 12:39:17Z ttsiodras $")  # pragma: no cover

416
    def FromToolToASN1SCC(self) -> RecursiveMapper:
417
418
        return FromSCADEtoASN1SCC()

419
    def FromToolToOSS(self) -> RecursiveMapper:
420
421
        return FromSCADEtoOSS()

422
    def FromASN1SCCtoTool(self) -> RecursiveMapper:
423
424
        return FromASN1SCCtoSCADE()

425
    def FromOSStoTool(self) -> RecursiveMapper:
426
427
        return FromOSStoSCADE()

428
    def HeadersOnStartup(self, unused_modelingLanguage: str, unused_asnFile: str, subProgram: ApLevelContainer, unused_subProgramImplementation: str, unused_outputDir: str, maybeFVname: str) -> None:
429
430
431
432
433
434
435
436
437
        if self.useOSS:
            self.C_SourceFile.write(
                "#include \"%s.oss.h\" // OSS generated\n" % self.asn_name)
            self.C_SourceFile.write("extern OssGlobal *g_world;\n\n")
        else:
            self.C_SourceFile.write(
                "#include \"%s.h\" // Space certified compiler generated\n\n" % self.asn_name)
        self.C_SourceFile.write("#include \"%s\"\n\n" % (self.CleanNameAsToolWants(maybeFVname) + ".h"))
        # Declare and define staging inputs and outputs
438
439
440
441
        # self.C_SourceFile.write("AADL2SCADE_%s_%s_DECLARE(var_%s_%s);\n\n" % \
        #     (subProgram._id, subProgramImplementation, subProgram._id, subProgramImplementation))
        # self.C_SourceFile.write("AADL2SCADE_%s_%s_DEFINE(var_%s_%s);\n\n" % \
        #     (subProgram._id, subProgramImplementation, subProgram._id, subProgramImplementation))
442
443
444
445
        for param in subProgram._params:
            self.C_SourceFile.write("%s %s;\n" % (self.CleanNameAsToolWants(param._signal._asnNodename), param._id))
        self.C_SourceFile.write("\n")

446
    def SourceVar(self, unused_nodeTypename: str, unused_encoding: str, unused_node: AsnNode, unused_subProgram: ApLevelContainer, unused_subProgramImplementation: str, param: Param, unused_leafTypeDict: AST_Leaftypes, unused_names: AST_Lookup) -> str:
447
448
        if isinstance(param._sourceElement, AadlPort):  # Both AadlPort and AadlEventDataPort
            panic("Unsupported old construct")  # pragma: no cover
449
450
451
452
            # srcScadeMacro = "AADL2SCADE_OUTPUT_DATA_PORT(var_%s, %s, %s)" % \
            #    (self.CleanNameAsToolWants(subProgram._id + "_" + subProgramImplementation),
            #     self.CleanNameAsToolWants(subProgram._id + "_" + subProgramImplementation),
            #     self.CleanNameAsToolWants(param._id).upper())
453
        elif isinstance(param._sourceElement, AadlParameter):
454
455
456
457
            # srcScadeMacro = "AADL2SCADE_OUTPUT_PARAMETER(var_%s, %s, %s)" % \
            #    (self.CleanNameAsToolWants(subProgram._id + "_" + subProgramImplementation),
            #     self.CleanNameAsToolWants(subProgram._id + "_" + subProgramImplementation),
            #     self.CleanNameAsToolWants(param._id).upper())
458
459
460
461
462
            srcScadeMacro = self.CleanNameAsToolWants(param._id)
        else:  # pragma: no cover
            panic(str(self.__class__) + ": %s not supported (yet?)\n" % str(param._sourceElement))  # pragma: no cover
        return srcScadeMacro

463
    def TargetVar(self, unused_nodeTypename: str, unused_encoding: str, unused_node: AsnNode, unused_subProgram: ApLevelContainer, unused_subProgramImplementation: str, param: Param, unused_leafTypeDict: AST_Leaftypes, unused_names: AST_Lookup) -> str:
464
465
        if isinstance(param._sourceElement, AadlPort):  # Both AadlPort and AadlEventDataPort
            panic("Unsupported old construct")  # pragma: no cover
466
467
468
469
            # dstScadeMacro = "AADL2SCADE_INPUT_DATA_PORT(var_%s, %s, %s)" % \
            #    (self.CleanNameAsToolWants(subProgram._id + "_" + subProgramImplementation),
            #     self.CleanNameAsToolWants(subProgram._id + "_" + subProgramImplementation),
            #     self.CleanNameAsToolWants(param._id).upper())
470
        elif isinstance(param._sourceElement, AadlParameter):
471
472
473
474
            # dstScadeMacro = "AADL2SCADE_INPUT_PARAMETER(var_%s, %s, %s)" % \
            #    (self.CleanNameAsToolWants(subProgram._id + "_" + subProgramImplementation),
            #     self.CleanNameAsToolWants(subProgram._id + "_" + subProgramImplementation),
            #     self.CleanNameAsToolWants(param._id).upper())
475
476
477
478
479
            dstScadeMacro = self.CleanNameAsToolWants(param._id)
        else:  # pragma: no cover
            panic(str(self.__class__) + ": %s not supported (yet?)\n" % str(param._sourceElement))  # pragma: no cover
        return dstScadeMacro

480
    def InitializeBlock(self, unused_modelingLanguage: str, unused_asnFile: str, sp: ApLevelContainer, unused_subProgramImplementation: str, unused_maybeFVname: str) -> None:
481
482
        self.C_SourceFile.write("    %s_reset();\n" % (self.CleanNameAsToolWants(sp._id)))

483
    def ExecuteBlock(self, unused_modelingLanguage: str, unused_asnFile: str, sp: ApLevelContainer, unused_subProgramImplementation: str, unused_maybeFVname: str) -> None:
484
485
486
        self.C_SourceFile.write("    %s();\n" % (self.CleanNameAsToolWants(sp._id)))


487
def OnStartup(modelingLanguage: str, asnFile: str, subProgram: ApLevelContainer, subProgramImplementation: str, outputDir: str, maybeFVname: str, useOSS: bool) -> None:
488
489
490
491
492
    global scadeBackend
    scadeBackend = ScadeGlueGenerator()
    scadeBackend.OnStartup(modelingLanguage, asnFile, subProgram, subProgramImplementation, outputDir, maybeFVname, useOSS)


493
def OnBasic(nodeTypename: str, node: AsnBasicNode, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
494
495
496
    scadeBackend.OnBasic(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)


497
def OnSequence(nodeTypename: str, node: AsnSequence, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
498
499
500
    scadeBackend.OnSequence(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)


501
def OnSet(nodeTypename: str, node: AsnSet, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
502
503
504
    scadeBackend.OnSet(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)  # pragma: nocover


505
def OnEnumerated(nodeTypename: str, node: AsnEnumerated, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
506
507
508
    scadeBackend.OnEnumerated(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)


509
def OnSequenceOf(nodeTypename: str, node: AsnSequenceOf, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
510
511
512
    scadeBackend.OnSequenceOf(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)


513
def OnSetOf(nodeTypename: str, node: AsnSetOf, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
514
515
516
    scadeBackend.OnSetOf(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)  # pragma: nocover


517
def OnChoice(nodeTypename: str, node: AsnChoice, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
518
519
520
    scadeBackend.OnChoice(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)


521
def OnShutdown(modelingLanguage: str, asnFile: str, sp: ApLevelContainer, subProgramImplementation: str, maybeFVname: str) -> None:
522
    scadeBackend.OnShutdown(modelingLanguage, asnFile, sp, subProgramImplementation, maybeFVname)