def _getInitValue(self, ws, def_name, value, dataType): """ returns a list or sequence """ code = C.sequence() if isinstance(value, autosar.constant.IntegerValue): if dataType.minVal >= 0: suffix = 'u' else: suffix = '' code.append( C.define(def_name, '((%s)%s%s)' % (dataType.name, value.value, suffix))) elif isinstance(value, autosar.constant.StringValue): code.append(C.define(def_name, '"%s"' % (value.value))) elif isinstance(value, autosar.constant.BooleanValue): if value.value: text = '((boolean) TRUE)' else: text = '((boolean) FALSE)' code.append(C.define(def_name, text)) elif isinstance(value, autosar.constant.RecordValue): for element in value.elements: prefix = '%s_%s' % (def_name, element.name) dataType = ws.find(element.typeRef) if dataType is not None: code.extend( self._getInitValue(ws, prefix, element, dataType)) elif isinstance(value, autosar.constant.ArrayValue): pass else: raise NotImplementedError(type(value)) return code
def __init__(self, partition, prefix='Rte', include=None, mode_switch=True, os_enable=True): self.partition = partition self.includes = [ ] #array of tuples, first element is the name of include header, second element is True if this is a sysinclude self.prefix = prefix self.com_component = None self.header_file_name = None self.data_elements = [] self.extra_static_vars = {} self.extra_public_functions = {} self.extra_rte_start = C.sequence() self.mode_switch_enable = mode_switch self.os_enable = os_enable #self.com_access = {'receive': {}, 'send': {}} if include is not None: for elem in include: if isinstance(elem, str) or isinstance(elem, tuple): self.includes.append(elem) else: raise ValueError( "include items must be of type str or tuple(str,boolean)" ) for component in partition.components: if isinstance(component.inner, autosar.bsw.com.ComComponent): if self.com_component is None: self.com_component = component else: raise RuntimeError( "More than one Com component allowed in a partition")
def _write_includes(self, fp): lines = _genCommentHeader('Includes') fp.write('\n'.join(lines)+'\n') code = C.sequence() for include in self.includes: code.append(C.include(*include)) fp.write('\n'.join(code.lines())+'\n\n')
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
def _initDefaultType(self): self.defaultTypes['Boolean'] = C.sequence().extend( [C.statement(C.typedef('boolean', 'Boolean'))]) self.defaultTypes['UInt8'] = C.sequence().extend([ C.statement(C.typedef('uint8', 'UInt8')), C.define('UInt8_LowerLimit', '((UInt8)0u)'), C.define('UInt8_UpperLimit', '((UInt8)255u)') ]) self.defaultTypes['UInt16'] = C.sequence().extend([ C.statement(C.typedef('uint16', 'UInt16')), C.define('UInt16_LowerLimit', '((UInt16)0u)'), C.define('UInt16_UpperLimit', '((UInt16)65535u)') ]) self.defaultTypes['UInt32'] = C.sequence().extend([ C.statement(C.typedef('uint32', 'UInt32')), C.define('UInt32_LowerLimit', '((UInt32)0u)'), C.define('UInt32_UpperLimit', '((UInt32)4294967295u)') ]) self.defaultTypes['SInt8'] = C.sequence().extend([ C.statement(C.typedef('sint8', 'SInt8')), C.define('SInt8_LowerLimit', '((SInt8)-128)'), C.define('SInt8_UpperLimit', '((SInt8)127)') ]) self.defaultTypes['SInt16'] = C.sequence().extend([ C.statement(C.typedef('sint16', 'SInt16')), C.define('SInt16_LowerLimit', '((SInt16)-32768)'), C.define('SInt16_UpperLimit', '((SInt16)32767)') ]) self.defaultTypes['SInt32'] = C.sequence().extend([ C.statement(C.typedef('sint32', 'SInt32')), C.define('SInt32_LowerLimit', '((SInt32)-2147483648)'), C.define('SInt32_UpperLimit', '((SInt32)2147483647)') ])
def generate_on_exit_code(self, event, callback_name): code = C.sequence() else_str = 'else ' if len(self.calls) > 0 else '' code.append(C.line(else_str+'if ( (previousMode == RTE_MODE_{0}) && (newMode != RTE_MODE_{0}) )'.format(event.mode+'_'+event.modeDeclaration))) block = C.block(innerIndent = innerIndentDefault) block.append(C.statement(C.fcall(callback_name))) code.append(block) self.add_event_to_call(event, callback_name) self.body.extend(code)
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
def _generate_event_mask_triggers(self, task): code = C.sequence() for runnable in task.runnables: if runnable.processed: continue matching_runnables = self._find_compatible_runnables( task, runnable) self._generate_runnable_calls(code, matching_runnables) for matching in matching_runnables: matching.processed = True return code
def _write_includes(self, fp): lines = _genCommentHeader('Includes') fp.write('\n'.join(lines) + '\n') code = C.sequence() for include in self.includes: code.append(C.include(*include)) if self.com_component is not None: code.append(C.include(self.com_component.name + '.h')) if self.os_enable: code.append(C.include('os.h')) fp.write('\n'.join(code.lines()) + '\n\n')
def _write_local_vars(self, fp): fp.write('\n'.join(_genCommentHeader('Local Variables')) + '\n') code = C.sequence() for data_element in sorted(self.partition.data_element_map.values(), key=lambda x: x.symbol): var = C.variable(data_element.symbol, data_element.dataType.name, True) code.append(C.statement(var)) for key in sorted(self.extra_static_vars.keys()): code.append(C.statement(self.extra_static_vars[key])) fp.write('\n'.join(code.lines()) + '\n\n')
def _genCommentHeader(comment): code = C.sequence() code.append( C.line( '/*********************************************************************************************************************' )) code.append(C.line('* %s' % comment)) code.append( C.line( '*********************************************************************************************************************/' )) return code
def _rand(): print("Generate rand function...") body = C.block(innerIndent=3) body.append( C.statement(f"{C.variable('var0', 'int')} = {C.fcall('rand')}")) body.append(C.statement('return var0')) head = C.function('f_rand', 'int') func = C.sequence() func.append(head) func.append(body) print(str(func)) return func
def _printf(): print("Generate printf function...") body = C.block(innerIndent=3) body.append(C.statement(C.fcall('printf').add_arg('\"%d\"').add_arg('p0'))) head = C.function( 'f_printf', 'void', ).add_param(C.variable('p0', 'int')) func = C.sequence() func.append(head) func.append(body) print(str(func)) return func
def _main(): print("Generate main function...") func = C.sequence() head = C.function( 'main', 'int', ) body = C.block(innerIndent=3) body.append(C.statement('return 0')) func.append(head) func.append(body) print(str(func)) return func
def _generate_mode_switch_func(self, callback_name, events): code = C.sequence() generated=set() code.append(C.function(callback_name, 'void')) block = C.block(innerIndent = innerIndentDefault) for event in events: task = self.cfg.find_os_task_by_runnable(event.runnable) if task is not None: if (task.name, event.name) not in generated: block.append(C.statement(C.fcall('os_task_setEvent', params=['&m_os_task_%s'%task.name, 'EVENT_MASK_%s_%s'%(task.name, event.name)]))) generated.add((task.name, event.name)) code.append(block) return code
def _genCommentHeader2(comment): """ Same as _genCommentHeader but returns a C sequence instead of raw strings """ code = C.sequence() code.append( C.line( '/*********************************************************************************************************************' )) code.append(C.line('* %s' % comment)) code.append( C.line( '*********************************************************************************************************************/' )) return code
def _scanf_no_pointer(): print("Generate scanf function...") body = C.block(innerIndent=3) body.append(C.statement(C.variable('var0', 'int'))) body.append( C.statement(C.fcall('scanf').add_arg('\"%d\"').add_arg('&var0'))) body.append(C.statement("return var0")) head = C.function( 'f_scanf_nop', 'int', ) func = C.sequence() func.append(head) func.append(body) print(str(func)) return func
def _genInitValues(self, ws, ports): ports = sorted(ports, key=lambda port: port.name) code = C.sequence() for port in ports: for comspec in port.comspec: if isinstance(comspec, autosar.component.DataElementComSpec): if comspec.initValueRef is not None: initValue = ws.find(comspec.initValueRef) if isinstance(initValue, autosar.constant.Constant): #in case the ref is pointing to a Constant (the parent), grab the child instance using .value initValue=initValue.value if initValue is not None: dataType = ws.find(initValue.typeRef) if dataType is not None: prefix = 'Rte_InitValue_%s_%s'%(port.name, comspec.name) code.extend(self._getInitValue(ws, prefix, initValue, dataType)) return code
def _generate_mode_switch_func(self, callback_name, events): code = C.sequence() generated = set() code.append(C.function(callback_name, 'void')) block = C.block(innerIndent=innerIndentDefault) for event in events: task = self.cfg.find_os_task_by_runnable(event.runnable) if task is not None: if (task.name, event.name) not in generated: block.append( C.statement( C.fcall('os_task_setEvent', params=[ '&m_os_task_%s' % task.name, 'EVENT_MASK_%s_%s' % (task.name, event.name) ]))) generated.add((task.name, event.name)) code.append(block) return code
def _func(funcname): print(f"Generate {funcname}...") para_count = random.randint(0, 3) local_count = 3 local_const = 2 body = C.block(innerIndent=3) for i in range(local_count): body.append( C.statement( f"{C.variable(f'var{i}', 'int')} = {C.fcall(random.choice(['f_rand', 'f_scanf_nop']))}" )) for i in range(local_const): body.append( C.statement( f"{C.variable(f'var{i+local_count}', 'int')} = {random.randint(-1000, 1000)}" )) all_vars = [f'var{i}' for i in range(local_const + local_count) ] + [f'p{i}' for i in range(para_count)] op_seq = ['+', '-', '*', '/', '<<', '>>'] # print(all_vars) max_iter = 5 targets = [] for i in range(4): trg = random.choice(all_vars) expr = _expression(all_vars, op_seq, trg) body.append(C.statement(f"{trg} = {expr}")) targets.append(trg) ret_var = random.choice(all_vars) ret_expr = _expression(targets, op_seq, ret_var) body.append(C.statement(f"{ret_var} = {ret_expr}")) body.append(C.statement(f'return {ret_var}')) head = C.function(funcname, 'int') for i in range(para_count): head.add_param(C.variable(f'p{i}', 'int')) func = C.sequence() func.append(head) func.append(body) print(str(func)) return func
def _write_source_local_funcs(self): code = C.sequence() code.extend(_genCommentHeader2('LOCAL FUNCTION PROTOTYPES')) code.append(C.blank()) return code
def _write_source_constants_and_typedefs(self): code = C.sequence() code.extend(_genCommentHeader2('CONSTANTS AND DATA TYPES')) code.append(C.blank()) return code
def _write_local_vars(self, fp): fp.write('\n'.join(_genCommentHeader('Local Variables'))+'\n') code = C.sequence()
def _genCommentHeader(comment): code = C.sequence() code.append(C.line('/*********************************************************************************************************************')) code.append(C.line('* %s'%comment)) code.append(C.line('*********************************************************************************************************************/')) return code