Esempio n. 1
0
 def _write_source_includes(self):
     code = C.sequence()
     code.extend(_genCommentHeader2('INCLUDES'))
     code.append(C.blank())
     for include in self.includes:
         code.append(C.include(*include))
     code.append(C.blank())
     return code
Esempio n. 2
0
 def _write_source_global_funcs(self):
     code = C.sequence()
     code.extend(_genCommentHeader2('GLOBAL FUNCTIONS'))
     code.append(C.blank())
     for task in sorted(self.os_cfg.tasks, key=lambda x: x.name):
         code.append(C.line('OS_TASK_HANDLER({0.name}, arg)'.format(task)))
         code.append(self._generate_task_body(task))
         code.append(C.blank(2))
     return code
Esempio n. 3
0
 def _genComponentHeader(self, fp, component):
    ws = component.swc.rootWS()
    assert(ws is not None)
    hfile=C.hfile(None, guard='RTE_%s_H'%(component.swc.name.upper()))
    hfile.code.append(C.include('Rte.h'))
    hfile.code.append(C.include('Rte_Type.h'))      
    lines = self._genInitValues(ws, component.swc.requirePorts+component.swc.providePorts)
    if len(lines)>0:
       hfile.code.extend([C.line(x) for x in _genCommentHeader('Init Values')])
       hfile.code.extend(lines)
    
    #Write API
    hfile.code.append(C.blank())
    hfile.code.extend([C.line(x) for x in _genCommentHeader('API Prototypes')])
    for proto in component.clientAPI.get_all():
       hfile.code.append(C.statement(proto.func))
    if len(component.clientAPI.final['read'])>0:
       hfile.code.append(C.blank())
       hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Read_<p>_<d>')])         
       hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['read']])
    if len(component.clientAPI.final['write'])>0:
       hfile.code.append(C.blank())
       hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Write_<p>_<d>')])         
       hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['write']])
    if len(component.clientAPI.final['receive'])>0:
       hfile.code.append(C.blank())
       hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Receive_<p>_<d>')])         
       hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['receive']])
    if len(component.clientAPI.final['send'])>0:
       hfile.code.append(C.blank())
       hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Send_<p>_<d>')])         
       hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['send']])
    if len(component.clientAPI.final['mode'])>0:
       hfile.code.append(C.blank())
       hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Mode_<p>_<d>')])         
       hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['mode']])
    if len(component.clientAPI.final['mode'])>0:
       hfile.code.append(C.blank())
       hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Mode_<mode>')])         
       hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['mode']])
    if len(component.clientAPI.final['calprm'])>0:
       hfile.code.append(C.blank())
       hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Calprm_<name>')])
       hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['calprm']])
    if len(component.clientAPI.final['call'])>0:
       hfile.code.append(C.blank())
       hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Call_<p>_<o> ')])
       hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['call']])         
    if len(component.rte_runnables)>0:
       for name in sorted(component.rte_runnables):
          runnable = component.rte_runnables[name]
          tmp = self._writeRunnableProto(runnable)
          hfile.code.extend(tmp)
    fp.write('\n'.join(hfile.lines()))
    fp.write('\n')
Esempio n. 4
0
 def _writeRunnableProto(self, runnable):
     lines = []
     lines.extend([
         C.line(x) for x in _genCommentHeader('Runnable %s' % runnable.name)
     ])
     lines.append(C.statement(runnable.prototype))
     lines.append(C.blank())
     return lines
Esempio n. 5
0
def main_file(filename):
    f = C.cfile(filename)
    f.code.append(C.sysinclude('stdio.h'))
    f.code.append(C.blank())
    f.code.append(utils._main())
    f.code.append(utils._printf())
    f.code.append(utils._scanf_no_pointer())
    f.code.append(utils._rand())
    for i in range(10000):
        f.code.append(utils._func(f"func{i}"))
    utils._file(filename, str(f))
Esempio n. 6
0
    def _createHeaderLines(self, filepath):
        hfile = C.hfile(filepath)
        code = hfile.code
        code.extend([C.line(x) for x in _genCommentHeader('Includes')])
        code.append(C.include("Std_Types.h"))
        code.append(C.include("Rte_Type.h"))
        code.append(C.include("Rte.h"))
        code.append(C.blank())
        code.extend(_genCommentHeader('Constants and Types'))
        for key in sorted(self.typedefs.keys()):
            code.append(C.statement(self.typedefs[key]))
        code.append(C.blank())
        code.extend([
            C.line(x)
            for x in _genCommentHeader('Public Function Declarations')
        ])
        code.append(C.blank())

        code.append(
            C.statement(C.function('%s_Start' % self.api_prefix, 'void')))
        for func in sorted(self.partition.upperLayerAPI.final['get'],
                           key=lambda x: x.shortname):
            assert func.proto is not None
            hfile.code.append(C.statement(func.proto))
        for func in sorted(self.partition.upperLayerAPI.final['setReadData'],
                           key=lambda x: x.shortname):
            assert func.proto is not None
            hfile.code.append(C.statement(func.proto))
        for func in sorted(self.partition.upperLayerAPI.final['setReadResult'],
                           key=lambda x: x.shortname):
            assert func.proto is not None
            hfile.code.append(C.statement(func.proto))
        for func in sorted(self.extra_public_functions.values(),
                           key=lambda x: x.shortname):
            assert func.proto is not None
            hfile.code.append(C.statement(func.proto))
        hfile.code.append(C.blank())
        return hfile.lines()
Esempio n. 7
0
 def _generate_event_cfg_header(self, dest_dir, file_name='os_event_cfg.h'):      
    header = C.hfile(os.path.join(dest_dir, file_name))
    code = header.code
    code.extend(_genCommentHeader('INCLUDES'))
    code.append(C.include('PlatForm_Types.h'))
    code.append('')
    code.extend(_genCommentHeader('PUBLIC CONSTANTS AND DATA TYPES'))
    for os_task in self.cfg.tasks:
       for event_mask in os_task.event_masks:
          code.append(event_mask)
       code.append(C.define('OS_NUM_ALARMS_%s'%os_task.name, str(len(os_task.timer_events))))
       code.append(C.blank())
    with io.open(header.path, 'w', newline='\n') as fp:
       for line in header.lines():
          fp.write(line+'\n')
Esempio n. 8
0
File: generator.py Progetto: ncs1/as
 def _generate_event_cfg_header(self, dest_dir, file_name='os_event_cfg.h'):
     header = C.hfile(os.path.join(dest_dir, file_name))
     code = header.code
     code.extend(_genCommentHeader('INCLUDES'))
     code.append(C.include('PlatForm_Types.h'))
     code.append('')
     code.extend(_genCommentHeader('PUBLIC CONSTANTS AND DATA TYPES'))
     for os_task in self.cfg.tasks:
         for event_mask in os_task.event_masks:
             code.append(event_mask)
         code.append(
             C.define('OS_NUM_ALARMS_%s' % os_task.name,
                      str(len(os_task.timer_events))))
         code.append(C.blank())
     with io.open(header.path, 'w', newline='\n') as fp:
         for line in header.lines():
             fp.write(line + '\n')
Esempio n. 9
0
 def generateEventCfg(self, destdir, destfile='os_event_cfg.h'):
     self.event_id = 1
     self.num_timer_events = 0
     file = C.hfile(os.path.join(destdir, destfile))
     code = file.code
     with io.open(file.path, 'w', newline='\n') as fp:
         for os_task in self.cfg.tasks:
             for event_name in sorted(os_task.event_map.keys()):
                 rte_event = os_task.event_map[event_name]
                 if isinstance(rte_event, autosar.rte.TimerEvent):
                     self.num_timer_events += 1
                 code.append(
                     C.define(
                         event_name,
                         str(self.event_id) + ' ' +
                         str(C.linecomment(rte_event.rte_runnable.symbol)),
                         align=60))
                 self.event_id += 1
             code.append(C.blank())
         code.append(
             C.define('OS_NUM_TIMER_EVENTS', str(self.num_timer_events)))
         for line in file.lines():
             fp.write(line + '\n')
Esempio n. 10
0
 def _write_source_local_funcs(self):
     code = C.sequence()
     code.extend(_genCommentHeader2('LOCAL FUNCTION PROTOTYPES'))
     code.append(C.blank())
     return code
Esempio n. 11
0
 def _write_source_constants_and_typedefs(self):
     code = C.sequence()
     code.extend(_genCommentHeader2('CONSTANTS AND DATA TYPES'))
     code.append(C.blank())
     return code
Esempio n. 12
0
    def generate(self, dest_dir='.', file_name='Rte_Type.h'):
        """
      Generates Rte_Type.h
      Note: The last argument has been deprecated and is no longer in use
      """
        if self.partition.isFinalized == False:
            self.partition.finalize()
        file_path = os.path.join(dest_dir, file_name)
        with io.open(file_path, 'w', newline='\n') as fp:
            hfile = C.hfile(file_name)
            hfile.code.extend(
                [C.line(x) for x in _genCommentHeader('Includes')])
            hfile.code.append(C.include("Std_Types.h"))
            hfile.code.append(C.blank())
            (basicTypes, complexTypes,
             modeTypes) = self.partition.types.getTypes()
            hfile.code.extend([
                C.line(x) for x in _genCommentHeader('Data Type Definitions')
            ])
            hfile.code.append(C.blank())
            ws = self.partition.ws
            unusedDefaultTypes = self._findUnusedDefaultTypes(ws, basicTypes)

            first = True
            for ref in sorted(basicTypes) + sorted(complexTypes):
                dataType = ws.find(ref)
                if dataType is not None:
                    typedef = None
                    if first:
                        first = False
                    else:
                        hfile.code.append(C.blank())
                    hfile.code.append('#define Rte_TypeDef_%s' % dataType.name)
                    if isinstance(dataType, autosar.datatype.BooleanDataType):
                        typedef = C.typedef('boolean', dataType.name)
                        hfile.code.append(C.statement(typedef))
                    elif isinstance(dataType,
                                    autosar.datatype.IntegerDataType):
                        valrange = dataType.maxVal - dataType.minVal
                        bitcount = valrange.bit_length()
                        typename = dataType.name
                        basetype = self._typename(bitcount, dataType.minVal)
                        typedef = C.typedef(basetype, typename)
                        hfile.code.append(C.statement(typedef))
                        isUnsigned = True if basetype in ('uint8', 'uint16',
                                                          'uint32') else False
                        if isUnsigned:
                            minval = str(dataType.minVal) + 'u'
                            maxval = str(dataType.maxVal) + 'u'
                        else:
                            minval = str(dataType.minVal)
                            maxval = str(dataType.maxVal)
                        hfile.code.append('#define %s_LowerLimit ((%s)%s)' %
                                          (typename, typename, minval))
                        hfile.code.append('#define %s_UpperLimit ((%s)%s)' %
                                          (typename, typename, maxval))
                        if dataType.compuMethodRef is not None:
                            compuMethod = ws.find(dataType.compuMethodRef)
                            if compuMethod is not None:
                                lines1 = []
                                lines2 = []
                                if isinstance(
                                        compuMethod,
                                        autosar.datatype.CompuMethodConst):
                                    for elem in compuMethod.elements:
                                        if isUnsigned:
                                            value = str(elem.upperLimit) + 'u'
                                        else:
                                            value = str(elem.upperLimit)
                                        lines1.append(
                                            '#define RTE_CONST_%s (%s)' %
                                            (elem.textValue, value))
                                        lines2.append(
                                            '#define %s ((%s)%s)' %
                                            (elem.textValue, typename, value))
                                if len(lines2) > 0:
                                    tmp = lines1 + [C.blank()] + lines2
                                else:
                                    tmp = lines1
                                for line in tmp:
                                    hfile.code.append(line)
                            else:
                                raise ValueError(dataType.compuMethodRef)
                    elif isinstance(dataType, autosar.datatype.RecordDataType):
                        body = C.block(innerIndent=innerIndentDefault)
                        for elem in dataType.elements:
                            childType = ws.find(elem.typeRef, role='DataType')
                            body.append(
                                C.statement(
                                    C.variable(elem.name, childType.name)))
                        struct = C.struct(None, body, typedef=dataType.name)
                        hfile.code.append(C.statement(struct))
                    elif isinstance(dataType, autosar.datatype.StringDataType):
                        hfile.code.append('typedef uint8 %s[%d];' %
                                          (dataType.name, dataType.length + 1))
                    elif isinstance(dataType, autosar.datatype.ArrayDataType):
                        childType = ws.find(dataType.typeRef, role='DataType')
                        if childType is None:
                            raise ValueError('invalid type reference: ' +
                                             dataType.typeRef)
                        hfile.code.append(
                            'typedef %s %s[%d];' %
                            (childType.name, dataType.name, dataType.length))
                    elif isinstance(dataType, autosar.datatype.RealDataType):
                        if dataType.encoding == 'DOUBLE':
                            platform_typename = 'float64'
                        else:
                            platform_typename = 'float32'
                        hfile.code.append('typedef %s %s;' %
                                          (platform_typename, dataType.name))
                    else:
                        raise NotImplementedError(type(dataType))
                        #sys.stderr.write('not implemented: %s\n'%str(type(dataType)))
                else:
                    raise ValueError(ref)

            if len(modeTypes) > 0:
                lines = _genCommentHeader('Mode Types')
                tmp = []
                hfile.code.extend(lines)
                first = True
                for ref in modeTypes:
                    if first:
                        first = False
                    else:
                        tmp.append(C.blank())
                    modeType = ws.find(ref)
                    hfile.code.append(
                        C.statement(
                            C.typedef('uint8',
                                      'Rte_ModeType_' + modeType.name)))

                    for i, elem in enumerate(modeType.modeDeclarations):
                        # define RTE_MODE_EcuM_Mode_POST_RUN ((Rte_ModeType_EcuM_Mode)0)
                        tmp.append(
                            C.define(
                                'RTE_MODE_%s_%s' % (modeType.name, elem.name),
                                '((Rte_ModeType_EcuM_Mode)%d)' % i))

                hfile.code.append(C.blank())
                hfile.code.extend(tmp)
            if len(unusedDefaultTypes) > 0:
                hfile.code.append(C.blank(2))
                hfile.code.append(
                    C.line('#ifndef RTE_SUPPRESS_UNUSED_DATATYPES'))
                for name in sorted(unusedDefaultTypes):
                    hfile.code.append(C.blank())
                    hfile.code.extend(self.defaultTypes[name])
                hfile.code.append(C.blank())
                hfile.code.append(C.line('#endif'))
            fp.write('\n'.join(hfile.lines()))
            fp.write('\n')
Esempio n. 13
0
import cfile as C
hello = C.cfile('hello.c')
hello.code.append(C.sysinclude('stdio.h'))
hello.code.append(C.blank())
hello.code.append(
    C.function(
        'main',
        'int',
    ).add_param(C.variable('argc', 'int')).add_param(
        C.variable('argv', 'char', pointer=2)))
body = C.block(innerIndent=3)
body.append(C.statement(C.fcall('printf').add_arg(r'"Hello World!\n"')))
body.append(C.statement('return 0'))
hello.code.append(body)
print(str(hello))
Esempio n. 14
0
import cfile as C
test = C.cfile('test.c')
test.code.append(C.sysinclude('stdio.h'))
test.code.append(C.blank())
test.code.append(
    C.function(
        'main',
        'int',
    ).add_arg(C.variable('argc',
                         'int')).add_arg(C.variable('argv', 'char',
                                                    pointer=2)))
body = C.block(indent=3)
body.append(C.statement(C.fcall('printf').add_param(r'"Hello World!\n"')))
body.append(C.statement('return 0'))
test.code.append(body)
print(str(test))