Commit 64c0dbee authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Fixed minor bugs in Ada backend

parent 62fcf81c
...@@ -113,7 +113,7 @@ def _process(process): ...@@ -113,7 +113,7 @@ def _process(process):
LOG.info('Generating Ada code for process ' + str(process_name)) LOG.info('Generating Ada code for process ' + str(process_name))
# In case model has nested states, flatten everything # In case model has nested states, flatten everything
Helper.flatten(process) Helper.flatten(process, sep='_')
# Make an maping {input: {state: transition...}} in order to easily # Make an maping {input: {state: transition...}} in order to easily
# generate the lookup tables for the state machine runtime # generate the lookup tables for the state machine runtime
...@@ -246,6 +246,27 @@ package {process_name} is'''.format(process_name=process_name, ...@@ -246,6 +246,27 @@ package {process_name} is'''.format(process_name=process_name,
continue continue
taste_template.append('when {state} =>'.format(state=state)) taste_template.append('when {state} =>'.format(state=state))
input_def = mapping[signal['name']].get(state) input_def = mapping[signal['name']].get(state)
# Check for nested states to call optional exit procedure
sep = '_'
state_tree = state.split(sep)
context=process
exitlist = []
current = ''
trans = input_def and process.transitions[input_def.transition_id]
while state_tree:
current = current + state_tree.pop(0)
for comp in context.composite_states:
if current == comp.statename.lower():
if comp.exit_procedure:
exitlist.append(current)
context = comp
current = current + sep
break
for each in reversed(exitlist):
if trans and all(each.startswith(trans_st)
for trans_st in trans.possible_states):
taste_template.append(each + '_' + 'exit;')
if input_def: if input_def:
for inp in input_def.parameters: for inp in input_def.parameters:
# Assign the (optional and unique) parameter # Assign the (optional and unique) parameter
......
...@@ -71,7 +71,7 @@ def inner_labels_to_floating(process): ...@@ -71,7 +71,7 @@ def inner_labels_to_floating(process):
process.content.floating_labels.append(new_floating) process.content.floating_labels.append(new_floating)
def flatten(process): def flatten(process, sep='_'):
''' Flatten the nested states: ''' Flatten the nested states:
Rename inner states, procedures, etc. and move them to process level Rename inner states, procedures, etc. and move them to process level
''' '''
...@@ -80,24 +80,19 @@ def flatten(process): ...@@ -80,24 +80,19 @@ def flatten(process):
if term.inputString.lower() in (st.statename.lower() if term.inputString.lower() in (st.statename.lower()
for st in context.composite_states): for st in context.composite_states):
if not term.via: if not term.via:
term.next_id = term.inputString.lower() + '_START' term.next_id = term.inputString.lower() + sep + 'START'
#process.mapping \
# [term.inputString.lower() + '_START']
else: else:
term.next_id = '{term}_{entry}_START'.format( term.next_id = '{term}{sep}{entry}{sep}START'.format(
term=term.inputString, entry=term.entrypoint) term=term.inputString, entry=term.entrypoint, sep=sep)
#process.mapping[term.inputString.lower()
# + '_'
# + term.entrypoint.lower()
# + '_START']
elif term.inputString.strip() == '-': elif term.inputString.strip() == '-':
term.candidate_id = defaultdict(list) term.candidate_id = defaultdict(list)
for each in term.possible_states: for each in term.possible_states:
if each in (st.statename.lower() if each in (st.statename.lower()
for st in context.composite_states): for st in context.composite_states):
term.candidate_id[each + '_START'] = \ term.candidate_id[each + sep + 'START'] = \
[st for st in process.mapping.viewkeys() [st for st in process.mapping.viewkeys()
if st.startswith(each) and not st.endswith('_START')] if st.startswith(each)
and not st.endswith(sep + 'START')]
else: else:
term.candidate_id[-1].append(each) term.candidate_id[-1].append(each)
...@@ -106,8 +101,10 @@ def flatten(process): ...@@ -106,8 +101,10 @@ def flatten(process):
to process, updating indexes, and update terminators to process, updating indexes, and update terminators
''' '''
trans_idx = len(process.transitions) trans_idx = len(process.transitions)
prefix = state.statename + '_' prefix = state.statename + sep
set_terminator_states(state, prefix) set_terminator_states(state, prefix)
set_transition_states(state, prefix)
state.mapping = {prefix + key:state.mapping.pop(key) state.mapping = {prefix + key:state.mapping.pop(key)
for key in state.mapping.keys()} for key in state.mapping.keys()}
process.transitions.extend(state.transitions) process.transitions.extend(state.transitions)
...@@ -119,13 +116,28 @@ def flatten(process): ...@@ -119,13 +116,28 @@ def flatten(process):
for key in state.variables.keys()} for key in state.variables.keys()}
process.variables.update(state.variables) process.variables.update(state.variables)
# Update return transition indices
for each in state.terminators:
if each.kind == 'return':
for idx, trans in enumerate(process.transitions):
if trans == each.next_trans:
each.next_id = idx
break
values = []
for key, value in state.mapping.viewitems(): for key, value in state.mapping.viewitems():
# Update transition indices # Update transition indices
if isinstance(value, int): if isinstance(value, int):
state.mapping[key] = value + trans_idx state.mapping[key] = value + trans_idx
else: else:
for inp in value: values.extend(value)
for inp in set(values):
# values may contain duplicate entries if an input corresponds
# to multiple states. In that case we must update the index of the
# input only once, thus the set().
inp.transition_id += trans_idx inp.transition_id += trans_idx
process.mapping.update(state.mapping) process.mapping.update(state.mapping)
# If composite state has entry procedures, add the call # If composite state has entry procedures, add the call
...@@ -207,7 +219,16 @@ def flatten(process): ...@@ -207,7 +219,16 @@ def flatten(process):
term.possible_states.extend(prefix + name.lower() term.possible_states.extend(prefix + name.lower()
for name in each.statelist) for name in each.statelist)
def set_transition_states(context, prefix=''):
''' Associate state to transitions, needed to process properly
the call to the exit procedure of a nested state '''
for each in context.content.states:
for inp in each.inputs:
inp.transition.possible_states.extend(prefix + name.lower()
for name in each.statelist)
set_terminator_states(process) set_terminator_states(process)
set_transition_states(process)
for each in process.composite_states: for each in process.composite_states:
update_composite_state(each, process) update_composite_state(each, process)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
   
# Resource object code # Resource object code
# #
# Created: Wed May 7 14:56:46 2014 # Created: Sat May 17 07:26:45 2014
# by: The Resource Compiler for PySide (Qt v4.8.6) # by: The Resource Compiler for PySide (Qt v4.8.6)
# #
# WARNING! All changes made in this file will be lost! # WARNING! All changes made in this file will be lost!
...@@ -398,8 +398,11 @@ class Terminator(object): ...@@ -398,8 +398,11 @@ class Terminator(object):
self.entrypoint = None self.entrypoint = None
# some transitions can be chained, when entering/leaving nested states # some transitions can be chained, when entering/leaving nested states
self.next_id = -1 self.next_id = -1
# List of State that can contain the current state # Pointer to the next transition, when using return/connect
self.next_trans = None
# List of State that can lead to this terminator
# There can be several if terminator follows a floating label # There can be several if terminator follows a floating label
# Note, this field is updated by the Helper.flatten function
self.possible_states = [] self.possible_states = []
def __repr__(self): def __repr__(self):
...@@ -469,6 +472,12 @@ class Transition(object): ...@@ -469,6 +472,12 @@ class Transition(object):
self.terminator = None self.terminator = None
# All Terminators of this transition # All Terminators of this transition
self.terminators = [] self.terminators = []
# List of State that can lead to this transition
# There can be several if state has multiple names (e.g. STATE a, b)
# Note, this field is updated by the Helper.flatten function
# and is needed to properly know when to call a nested state exit
# procedure when the model is flattened.
self.possible_states = []
def __repr__(self): def __repr__(self):
''' Debug output: display all actions ''' ''' Debug output: display all actions '''
......
...@@ -2043,7 +2043,7 @@ def connect_part(root, parent, context): ...@@ -2043,7 +2043,7 @@ def connect_part(root, parent, context):
# Retrieve composite state # Retrieve composite state
try: try:
nested, = (comp for comp in context.composite_states nested, = (comp for comp in context.composite_states
if comp.statename == statename) if comp.statename.lower() == statename.lower())
except ValueError: except ValueError:
# Ignore unexisting state - to allow local syntax check # Ignore unexisting state - to allow local syntax check
nested = ogAST.CompositeState() nested = ogAST.CompositeState()
...@@ -2097,8 +2097,8 @@ def connect_part(root, parent, context): ...@@ -2097,8 +2097,8 @@ def connect_part(root, parent, context):
errors.append('No {rs} return statement in nested state {st}' errors.append('No {rs} return statement in nested state {st}'
.format(rs=exitp, st=statename)) .format(rs=exitp, st=statename))
for each in terminators: for each in terminators:
# Set transition ID, referencing process.transitions # Set next transition, exact id to be found in postprocessing
each.next_id = trans_id each.next_trans = trans
# Set list of terminators # Set list of terminators
conn.terminators = list(context.terminators[terms:]) conn.terminators = list(context.terminators[terms:])
# Report errors with symbol coordinates # Report errors with symbol coordinates
......
# $ANTLR 3.1.3 Mar 17, 2009 19:23:44 sdl92.g 2014-05-07 14:56:49 # $ANTLR 3.1.3 Mar 17, 2009 19:23:44 sdl92.g 2014-05-17 07:26:48
import sys import sys
from antlr3 import * from antlr3 import *
......
# $ANTLR 3.1.3 Mar 17, 2009 19:23:44 sdl92.g 2014-05-07 14:56:47 # $ANTLR 3.1.3 Mar 17, 2009 19:23:44 sdl92.g 2014-05-17 07:26:46
   
import sys import sys
from antlr3 import * from antlr3 import *
...@@ -762,7 +762,7 @@ class sdl92Parser(Parser): ...@@ -762,7 +762,7 @@ class sdl92Parser(Parser):
stream_end.add(end10.tree) stream_end.add(end10.tree)
   
# AST Rewrite # AST Rewrite
# elements: entity_in_system, system_name, SYSTEM # elements: SYSTEM, entity_in_system, system_name
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -890,7 +890,7 @@ class sdl92Parser(Parser): ...@@ -890,7 +890,7 @@ class sdl92Parser(Parser):
stream_end.add(end14.tree) stream_end.add(end14.tree)
   
# AST Rewrite # AST Rewrite
# elements: package_name, USE, use_asn1 # elements: USE, package_name, use_asn1
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -1174,7 +1174,7 @@ class sdl92Parser(Parser): ...@@ -1174,7 +1174,7 @@ class sdl92Parser(Parser):
stream_end.add(end23.tree) stream_end.add(end23.tree)
   
# AST Rewrite # AST Rewrite
# elements: SIGNAL, paramnames, signal_id, input_params # elements: signal_id, input_params, SIGNAL, paramnames
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -1328,7 +1328,7 @@ class sdl92Parser(Parser): ...@@ -1328,7 +1328,7 @@ class sdl92Parser(Parser):
stream_end.add(end28.tree) stream_end.add(end28.tree)
   
# AST Rewrite # AST Rewrite
# elements: route, channel_id, CHANNEL # elements: CHANNEL, channel_id, route
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -1498,7 +1498,7 @@ class sdl92Parser(Parser): ...@@ -1498,7 +1498,7 @@ class sdl92Parser(Parser):
stream_end.add(end37.tree) stream_end.add(end37.tree)
   
# AST Rewrite # AST Rewrite
# elements: dest_id, signal_id, source_id # elements: dest_id, source_id, signal_id
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -1648,7 +1648,7 @@ class sdl92Parser(Parser): ...@@ -1648,7 +1648,7 @@ class sdl92Parser(Parser):
stream_end.add(end43.tree) stream_end.add(end43.tree)
   
# AST Rewrite # AST Rewrite
# elements: BLOCK, block_id, entity_in_block # elements: entity_in_block, block_id, BLOCK
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -2058,7 +2058,7 @@ class sdl92Parser(Parser): ...@@ -2058,7 +2058,7 @@ class sdl92Parser(Parser):
stream_end.add(end56.tree) stream_end.add(end56.tree)
   
# AST Rewrite # AST Rewrite
# elements: channel_id, route_id # elements: route_id, channel_id
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -2220,7 +2220,7 @@ class sdl92Parser(Parser): ...@@ -2220,7 +2220,7 @@ class sdl92Parser(Parser):
stream_end.add(end61.tree) stream_end.add(end61.tree)
   
# AST Rewrite # AST Rewrite
# elements: number_of_instances, PROCESS, process_id, REFERENCED # elements: number_of_instances, PROCESS, REFERENCED, process_id
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -2422,7 +2422,7 @@ class sdl92Parser(Parser): ...@@ -2422,7 +2422,7 @@ class sdl92Parser(Parser):
stream_end.add(end73.tree) stream_end.add(end73.tree)
   
# AST Rewrite # AST Rewrite
# elements: composite_state, PROCESS, text_area, processBody, procedure, number_of_instances, cif, process_id # elements: process_id, composite_state, cif, processBody, PROCESS, procedure, number_of_instances, text_area
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -2747,7 +2747,7 @@ class sdl92Parser(Parser): ...@@ -2747,7 +2747,7 @@ class sdl92Parser(Parser):
stream_end.add(end85.tree) stream_end.add(end85.tree)
   
# AST Rewrite # AST Rewrite
# elements: processBody, text_area, fpar, procedure_id, cif, PROCEDURE, procedure, end, EXTERNAL # elements: processBody, procedure, fpar, end, procedure_id, EXTERNAL, PROCEDURE, text_area, cif
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -3084,7 +3084,7 @@ class sdl92Parser(Parser): ...@@ -3084,7 +3084,7 @@ class sdl92Parser(Parser):
stream_sort.add(sort96.tree) stream_sort.add(sort96.tree)
   
# AST Rewrite # AST Rewrite
# elements: IN, INOUT, sort, variable_id # elements: sort, variable_id, INOUT, IN
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -3226,7 +3226,7 @@ class sdl92Parser(Parser): ...@@ -3226,7 +3226,7 @@ class sdl92Parser(Parser):
stream_cif_end_text.add(cif_end_text99.tree) stream_cif_end_text.add(cif_end_text99.tree)
   
# AST Rewrite # AST Rewrite
# elements: cif_end_text, content, cif # elements: cif, cif_end_text, content
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -3388,7 +3388,7 @@ class sdl92Parser(Parser): ...@@ -3388,7 +3388,7 @@ class sdl92Parser(Parser):
break #loop30 break #loop30
   
# AST Rewrite # AST Rewrite
# elements: timer_declaration, variable_definition, procedure, fpar # elements: timer_declaration, variable_definition, fpar, procedure
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -3841,7 +3841,7 @@ class sdl92Parser(Parser): ...@@ -3841,7 +3841,7 @@ class sdl92Parser(Parser):
   
   
# AST Rewrite # AST Rewrite
# elements: ground_expression, variable_id, sort # elements: variable_id, sort, ground_expression
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -4049,7 +4049,7 @@ class sdl92Parser(Parser): ...@@ -4049,7 +4049,7 @@ class sdl92Parser(Parser):
stream_R_PAREN.add(char_literal123) stream_R_PAREN.add(char_literal123)
   
# AST Rewrite # AST Rewrite
# elements: maximum_number, initial_number # elements: initial_number, maximum_number
# token labels: maximum_number, initial_number # token labels: maximum_number, initial_number
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -4326,7 +4326,7 @@ class sdl92Parser(Parser): ...@@ -4326,7 +4326,7 @@ class sdl92Parser(Parser):
   
   
# AST Rewrite # AST Rewrite
# elements: end, cif, START, transition, hyperlink, name # elements: START, name, hyperlink, end, transition, cif
# token labels: # token labels:
# rule labels: retval, name # rule labels: retval, name
# token list labels: # token list labels:
...@@ -4563,7 +4563,7 @@ class sdl92Parser(Parser): ...@@ -4563,7 +4563,7 @@ class sdl92Parser(Parser):
stream_SEMI.add(SEMI140) stream_SEMI.add(SEMI140)
   
# AST Rewrite # AST Rewrite
# elements: cif, transition, connector_name, hyperlink # elements: hyperlink, connector_name, cif, transition
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -4787,7 +4787,7 @@ class sdl92Parser(Parser): ...@@ -4787,7 +4787,7 @@ class sdl92Parser(Parser):
stream_end.add(f.tree) stream_end.add(f.tree)
   
# AST Rewrite # AST Rewrite
# elements: statelist, state_part, e, hyperlink, STATE, cif # elements: state_part, cif, statelist, hyperlink, e, STATE
# token labels: # token labels:
# rule labels: retval, e # rule labels: retval, e
# token list labels: # token list labels:
...@@ -5341,7 +5341,7 @@ class sdl92Parser(Parser): ...@@ -5341,7 +5341,7 @@ class sdl92Parser(Parser):
stream_end.add(f.tree) stream_end.add(f.tree)
   
# AST Rewrite # AST Rewrite
# elements: body, e, connection_points, statename # elements: connection_points, e, body, statename
# token labels: # token labels:
# rule labels: body, retval, e # rule labels: body, retval, e
# token list labels: # token list labels:
...@@ -5490,7 +5490,7 @@ class sdl92Parser(Parser): ...@@ -5490,7 +5490,7 @@ class sdl92Parser(Parser):
stream_end.add(end166.tree) stream_end.add(end166.tree)
   
# AST Rewrite # AST Rewrite
# elements: IN, end, state_entry_exit_points # elements: end, state_entry_exit_points, IN
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -5547,7 +5547,7 @@ class sdl92Parser(Parser): ...@@ -5547,7 +5547,7 @@ class sdl92Parser(Parser):
stream_end.add(end169.tree) stream_end.add(end169.tree)
   
# AST Rewrite # AST Rewrite
# elements: end, OUT, state_entry_exit_points # elements: state_entry_exit_points, OUT, end
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -6141,7 +6141,7 @@ class sdl92Parser(Parser): ...@@ -6141,7 +6141,7 @@ class sdl92Parser(Parser):
   
   
# AST Rewrite # AST Rewrite
# elements: transition, connect_list, cif, CONNECT, end, hyperlink # elements: transition, connect_list, hyperlink, CONNECT, cif, end
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -6491,7 +6491,7 @@ class sdl92Parser(Parser): ...@@ -6491,7 +6491,7 @@ class sdl92Parser(Parser):
stream_transition.add(transition202.tree) stream_transition.add(transition202.tree)
   
# AST Rewrite # AST Rewrite
# elements: hyperlink, cif, transition # elements: cif, transition, hyperlink
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -6746,7 +6746,7 @@ class sdl92Parser(Parser): ...@@ -6746,7 +6746,7 @@ class sdl92Parser(Parser):
stream_transition.add(transition211.tree) stream_transition.add(transition211.tree)
   
# AST Rewrite # AST Rewrite
# elements: transition, integer_literal_name, expression, PROVIDED # elements: expression, transition, integer_literal_name, PROVIDED
# token labels: integer_literal_name # token labels: integer_literal_name
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -6855,7 +6855,7 @@ class sdl92Parser(Parser): ...@@ -6855,7 +6855,7 @@ class sdl92Parser(Parser):
stream_end.add(end214.tree) stream_end.add(end214.tree)
   
# AST Rewrite # AST Rewrite
# elements: SAVE, save_list # elements: save_list, SAVE
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -7362,7 +7362,7 @@ class sdl92Parser(Parser): ...@@ -7362,7 +7362,7 @@ class sdl92Parser(Parser):
   
   
# AST Rewrite # AST Rewrite
# elements: enabling_condition, end, transition, hyperlink, INPUT, inputlist, cif # elements: cif, enabling_condition, inputlist, INPUT, hyperlink, transition, end
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -7913,7 +7913,7 @@ class sdl92Parser(Parser): ...@@ -7913,7 +7913,7 @@ class sdl92Parser(Parser):
   
   
# AST Rewrite # AST Rewrite
# elements: terminator_statement, action, label # elements: action, terminator_statement, label
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -8317,7 +8317,7 @@ class sdl92Parser(Parser): ...@@ -8317,7 +8317,7 @@ class sdl92Parser(Parser):
stream_end.add(end261.tree) stream_end.add(end261.tree)
   
# AST Rewrite # AST Rewrite
# elements: EXPORT, variable_id # elements: variable_id, EXPORT
# token labels: # token labels:
# rule labels: retval # rule labels: retval
# token list labels: # token list labels:
...@@ -8471,7 +8471,7 @@ class sdl92Parser(Parser): ...@@ -8471,7 +8471,7 @@ class sdl92Parser(Parser):
stream_end.add(end266.tree) stream_end.add(end266.tree)
<