Commit 62fcf81c authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Refactoring - using Helper

parent 39c9516f
...@@ -115,6 +115,10 @@ def _process(process): ...@@ -115,6 +115,10 @@ def _process(process):
# In case model has nested states, flatten everything # In case model has nested states, flatten everything
Helper.flatten(process) Helper.flatten(process)
# Make an maping {input: {state: transition...}} in order to easily
# generate the lookup tables for the state machine runtime
mapping = Helper.map_input_state(process)
VARIABLES.update(process.variables) VARIABLES.update(process.variables)
INNER_PROCEDURES.extend(process.content.inner_procedures) INNER_PROCEDURES.extend(process.content.inner_procedures)
...@@ -160,20 +164,6 @@ def _process(process): ...@@ -160,20 +164,6 @@ def _process(process):
start_transition = ['begin', start_transition = ['begin',
'runTransition(0);'] 'runTransition(0);']
mapping = {}
# Generate the code for the transitions in a mapping input-state
input_signals = [sig['name'] for sig in process.input_signals]
# Add timers to the mapping
input_signals.extend(process.timers)
for input_signal in input_signals:
mapping[input_signal] = {}
for state_name, input_symbols in process.mapping.viewitems():
if isinstance(input_symbols, list):
# Start symbols have no list of inputs
for i in input_symbols:
if input_signal.lower() in (inp.lower() for
inp in i.inputlist):
mapping[input_signal][state_name] = i
# Generate the TASTE template # Generate the TASTE template
try: try:
......
...@@ -33,6 +33,7 @@ import logging ...@@ -33,6 +33,7 @@ import logging
from singledispatch import singledispatch from singledispatch import singledispatch
import ogAST import ogAST
import Helper
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
...@@ -66,7 +67,12 @@ def expression(expr): ...@@ -66,7 +67,12 @@ def expression(expr):
@generate.register(ogAST.Process) @generate.register(ogAST.Process)
def _process(process): def _process(process):
''' Generate the code for a complete process (AST Top level) ''' ''' Generate the code for a complete process (AST Top level) '''
pass # In case model has nested states, flatten everything
Helper.flatten(process)
# Make an maping {input: {state: transition...}} in order to easily
# generate the lookup tables for the state machine runtime
mapping = Helper.map_input_state(process)
def write_statement(param, newline): def write_statement(param, newline):
''' Generate the code for the special "write" operator ''' ''' Generate the code for the special "write" operator '''
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
flatten(ast) : transform a model with nested states to a flat model flatten(ast) : transform a model with nested states to a flat model
rename_everything(ast, from_name, to_name) : rename symbols rename_everything(ast, from_name, to_name) : rename symbols
inner_labels_to_floating(process) : remove labels from transitions inner_labels_to_floating(process) : remove labels from transitions
map_input_state(process) -> mapping: create a mapping
input-state-transition
Copyright (c) 2012-2014 European Space Agency Copyright (c) 2012-2014 European Space Agency
...@@ -27,7 +29,25 @@ import ogAST ...@@ -27,7 +29,25 @@ import ogAST
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
__all__ = ['flatten', 'rename_everything', 'inner_labels_to_floating'] __all__ = ['flatten', 'rename_everything', 'inner_labels_to_floating',
'map_input_state']
def map_input_state(process):
''' Create a mapping dict {input1: {state1: transition, ...}, ...} '''
mapping = defaultdict(dict)
input_signals = [sig['name'] for sig in process.input_signals]
# Add timers to the mapping
input_signals.extend(process.timers)
for input_signal in input_signals:
for state_name, input_symbols in process.mapping.viewitems():
if isinstance(input_symbols, list):
# Start symbols have no list of inputs
for i in input_symbols:
if input_signal.lower() in (inp.lower() for
inp in i.inputlist):
mapping[input_signal][state_name] = i
return mapping
def inner_labels_to_floating(process): def inner_labels_to_floating(process):
......
...@@ -22,6 +22,7 @@ from singledispatch import singledispatch ...@@ -22,6 +22,7 @@ from singledispatch import singledispatch
from llvm import core, passes, ee from llvm import core, passes, ee
import ogAST import ogAST
import Helper
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
...@@ -56,6 +57,13 @@ def _process(process): ...@@ -56,6 +57,13 @@ def _process(process):
process_name = process.processName process_name = process.processName
LOG.info('Generating LLVM IR code for process ' + str(process_name)) LOG.info('Generating LLVM IR code for process ' + str(process_name))
# In case model has nested states, flatten everything
Helper.flatten(process)
# Make an maping {input: {state: transition...}} in order to easily
# generate the lookup tables for the state machine runtime
mapping = Helper.map_input_state(process)
# Initialise LLVM global structure # Initialise LLVM global structure
LLVM['module'] = core.Module.new(str(process_name)) LLVM['module'] = core.Module.new(str(process_name))
LLVM['pass_manager'] = passes.FunctionPassManager.new(LLVM['module']) LLVM['pass_manager'] = passes.FunctionPassManager.new(LLVM['module'])
...@@ -136,14 +144,29 @@ def _task_forloop(task): ...@@ -136,14 +144,29 @@ def _task_forloop(task):
''' '''
pass pass
# ------ expressions --------
@singledispatch
def expression(expr):
''' Generate the code for Expression-classes, returning 3 things:
- list of statements
- useable string corresponding to the evaluation of the expression,
- list of local declarations
(API can differ depending on the backend)
'''
_ = expr
raise TypeError('Unsupported expression: ' + str(expr))
#return [], '', []
@generate.register(ogAST.PrimVariable) @expression.register(ogAST.PrimVariable)
def _primary_variable(prim): def _primary_variable(prim):
''' Single variable reference ''' ''' Single variable reference '''
pass pass
@generate.register(ogAST.PrimPath) @expression.register(ogAST.PrimPath)
def _prim_path(primaryId): def _prim_path(primaryId):
''' '''
Return the string of an element list (path) Return the string of an element list (path)
...@@ -157,107 +180,107 @@ def _prim_path(primaryId): ...@@ -157,107 +180,107 @@ def _prim_path(primaryId):
pass pass
@generate.register(ogAST.ExprPlus) @expression.register(ogAST.ExprPlus)
@generate.register(ogAST.ExprMul) @expression.register(ogAST.ExprMul)
@generate.register(ogAST.ExprMinus) @expression.register(ogAST.ExprMinus)
@generate.register(ogAST.ExprEq) @expression.register(ogAST.ExprEq)
@generate.register(ogAST.ExprNeq) @expression.register(ogAST.ExprNeq)
@generate.register(ogAST.ExprGt) @expression.register(ogAST.ExprGt)
@generate.register(ogAST.ExprGe) @expression.register(ogAST.ExprGe)
@generate.register(ogAST.ExprLt) @expression.register(ogAST.ExprLt)
@generate.register(ogAST.ExprLe) @expression.register(ogAST.ExprLe)
@generate.register(ogAST.ExprDiv) @expression.register(ogAST.ExprDiv)
@generate.register(ogAST.ExprMod) @expression.register(ogAST.ExprMod)
@generate.register(ogAST.ExprRem) @expression.register(ogAST.ExprRem)
@generate.register(ogAST.ExprAssign) @expression.register(ogAST.ExprAssign)
def _basic_operators(expr): def _basic_operators(expr):
''' Expressions with two sides ''' ''' Expressions with two sides '''
pass pass
@generate.register(ogAST.ExprOr) @expression.register(ogAST.ExprOr)
@generate.register(ogAST.ExprAnd) @expression.register(ogAST.ExprAnd)
@generate.register(ogAST.ExprXor) @expression.register(ogAST.ExprXor)
def _bitwise_operators(expr): def _bitwise_operators(expr):
''' Logical operators ''' ''' Logical operators '''
pass pass
@generate.register(ogAST.ExprAppend) @expression.register(ogAST.ExprAppend)
def _append(expr): def _append(expr):
''' Generate code for the APPEND construct: a // b ''' ''' Generate code for the APPEND construct: a // b '''
pass pass
@generate.register(ogAST.ExprIn) @expression.register(ogAST.ExprIn)
def _expr_in(expr): def _expr_in(expr):
''' IN expressions: check if item is in a SEQUENCE OF ''' ''' IN expressions: check if item is in a SEQUENCE OF '''
pass pass
@generate.register(ogAST.PrimEnumeratedValue) @expression.register(ogAST.PrimEnumeratedValue)
def _enumerated_value(primary): def _enumerated_value(primary):
''' Generate code for an enumerated value ''' ''' Generate code for an enumerated value '''
pass pass
@generate.register(ogAST.PrimChoiceDeterminant) @expression.register(ogAST.PrimChoiceDeterminant)
def _choice_determinant(primary): def _choice_determinant(primary):
''' Generate code for a choice determinant (enumerated) ''' ''' Generate code for a choice determinant (enumerated) '''
pass pass
@generate.register(ogAST.PrimInteger) @expression.register(ogAST.PrimInteger)
@generate.register(ogAST.PrimReal) @expression.register(ogAST.PrimReal)
@generate.register(ogAST.PrimBoolean) @expression.register(ogAST.PrimBoolean)
def _integer(primary): def _integer(primary):
''' Generate code for a raw integer/real/boolean value ''' ''' Generate code for a raw integer/real/boolean value '''
pass pass
@generate.register(ogAST.PrimEmptyString) @expression.register(ogAST.PrimEmptyString)
def _empty_string(primary): def _empty_string(primary):
''' Generate code for an empty SEQUENCE OF: {} ''' ''' Generate code for an empty SEQUENCE OF: {} '''
pass pass
@generate.register(ogAST.PrimStringLiteral) @expression.register(ogAST.PrimStringLiteral)
def _string_literal(primary): def _string_literal(primary):
''' Generate code for a string (Octet String) ''' ''' Generate code for a string (Octet String) '''
pass pass
@generate.register(ogAST.PrimConstant) @expression.register(ogAST.PrimConstant)
def _constant(primary): def _constant(primary):
''' Generate code for a reference to an ASN.1 constant ''' ''' Generate code for a reference to an ASN.1 constant '''
pass pass
@generate.register(ogAST.PrimMantissaBaseExp) @expression.register(ogAST.PrimMantissaBaseExp)
def _mantissa_base_exp(primary): def _mantissa_base_exp(primary):
''' Generate code for a Real with Mantissa-base-Exponent representation ''' ''' Generate code for a Real with Mantissa-base-Exponent representation '''
pass pass
@generate.register(ogAST.PrimIfThenElse) @expression.register(ogAST.PrimIfThenElse)
def _if_then_else(ifThenElse): def _if_then_else(ifThenElse):
''' Return string and statements for ternary operator ''' ''' Return string and statements for ternary operator '''
pass pass
@generate.register(ogAST.PrimSequence) @expression.register(ogAST.PrimSequence)
def _sequence(seq): def _sequence(seq):
''' Return Ada string for an ASN.1 SEQUENCE ''' ''' Return Ada string for an ASN.1 SEQUENCE '''
pass pass
@generate.register(ogAST.PrimSequenceOf) @expression.register(ogAST.PrimSequenceOf)
def _sequence_of(seqof): def _sequence_of(seqof):
''' Return Ada string for an ASN.1 SEQUENCE OF ''' ''' Return Ada string for an ASN.1 SEQUENCE OF '''
pass pass
@generate.register(ogAST.PrimChoiceItem) @expression.register(ogAST.PrimChoiceItem)
def _choiceitem(choice): def _choiceitem(choice):
''' Return the Ada code for a CHOICE expression ''' ''' Return the Ada code for a CHOICE expression '''
pass pass
......
...@@ -29,8 +29,8 @@ D ada.ads 20070406091342 3ffc8e18 ...@@ -29,8 +29,8 @@ D ada.ads 20070406091342 3ffc8e18
D a-charac.ads 20070912115821 2d3ec45b D a-charac.ads 20070912115821 2d3ec45b
D a-chlat1.ads 20070912115821 2825df20 D a-chlat1.ads 20070912115821 2825df20
D a-unccon.ads 20070406091342 f9eb8f06 D a-unccon.ads 20070406091342 f9eb8f06
D adaasn1rtl.ads 20140513071215 989f6c09 D adaasn1rtl.ads 20140513113246 989f6c09
D adaasn1rtl.adb 20140513071215 3a622529 D adaasn1rtl.adb 20140513113246 3a622529
D interfac.ads 20110906094621 129923ea D interfac.ads 20110906094621 129923ea
D system.ads 20140212114732 bd760655 D system.ads 20140212114732 bd760655
D s-exnllf.ads 20121004090553 81841eef D s-exnllf.ads 20121004090553 81841eef
......
...@@ -14,12 +14,12 @@ W adaasn1rtl%s adaasn1rtl.adb adaasn1rtl.ali ...@@ -14,12 +14,12 @@ W adaasn1rtl%s adaasn1rtl.adb adaasn1rtl.ali
D ada.ads 20070406091342 3ffc8e18 D ada.ads 20070406091342 3ffc8e18
D a-charac.ads 20070912115821 2d3ec45b D a-charac.ads 20070912115821 2d3ec45b
D a-chlat1.ads 20070912115821 2825df20 D a-chlat1.ads 20070912115821 2825df20
D adaasn1rtl.ads 20140513071215 989f6c09 D adaasn1rtl.ads 20140513113246 989f6c09
D interfac.ads 20110906094621 129923ea D interfac.ads 20110906094621 129923ea
D system.ads 20140212114732 bd760655 D system.ads 20140212114732 bd760655
D s-unstyp.ads 20090409150019 6ae15c76 D s-unstyp.ads 20090409150019 6ae15c76
D taste_basictypes.ads 20140513071215 72ec04b4 D taste_basictypes.ads 20140513113246 72ec04b4
D taste_basictypes.adb 20140513071215 ad8ecfb4 D taste_basictypes.adb 20140513113246 ad8ecfb4
X 4 adaasn1rtl.ads X 4 adaasn1rtl.ads
7K9*adaasn1rtl 908e15 8|2w6 2r27 2r57 2r85 11r27 21r75 29r28 39r77 47r26 7K9*adaasn1rtl 908e15 8|2w6 2r27 2r57 2r85 11r27 21r75 29r28 39r77 47r26
. 57r73 65r27 75r75 83r29 9|2w6 3r10 4r10 5r10 12r36 21r75 23r11 31r12 47r77 . 57r73 65r27 75r75 83r29 9|2w6 3r10 4r10 5r10 12r36 21r75 23r11 31r12 47r77
......
...@@ -14,13 +14,13 @@ W taste_basictypes%s taste_basictypes.adb taste_basictypes.ali ...@@ -14,13 +14,13 @@ W taste_basictypes%s taste_basictypes.adb taste_basictypes.ali
D ada.ads 20070406091342 3ffc8e18 D ada.ads 20070406091342 3ffc8e18
D a-charac.ads 20070912115821 2d3ec45b D a-charac.ads 20070912115821 2d3ec45b
D a-chlat1.ads 20070912115821 2825df20 D a-chlat1.ads 20070912115821 2825df20
D adaasn1rtl.ads 20140513071215 989f6c09 D adaasn1rtl.ads 20140513113246 989f6c09
D interfac.ads 20110906094621 129923ea D interfac.ads 20110906094621 129923ea
D system.ads 20140212114732 bd760655 D system.ads 20140212114732 bd760655
D s-unstyp.ads 20090409150019 6ae15c76 D s-unstyp.ads 20090409150019 6ae15c76
D taste_basictypes.ads 20140513071215 72ec04b4 D taste_basictypes.ads 20140513113246 72ec04b4
D taste_dataview.ads 20140513071215 a40837cf D taste_dataview.ads 20140513113246 a40837cf
D taste_dataview.adb 20140513071215 051f7d26 D taste_dataview.adb 20140513113246 051f7d26
X 4 adaasn1rtl.ads X 4 adaasn1rtl.ads
7K9*adaasn1rtl 908e15 9|2w6 2r27 2r57 2r85 10|2w6 4r10 5r10 6r10 7K9*adaasn1rtl 908e15 9|2w6 2r27 2r57 2r85 10|2w6 4r10 5r10 6r10
20M10*BIT 9|2r96 10|6r21 20M10*BIT 9|2r96 10|6r21
......
...@@ -21,15 +21,15 @@ W taste_dataview%s taste_dataview.adb taste_dataview.ali ...@@ -21,15 +21,15 @@ W taste_dataview%s taste_dataview.adb taste_dataview.ali
D ada.ads 20070406091342 3ffc8e18 D ada.ads 20070406091342 3ffc8e18
D a-charac.ads 20070912115821 2d3ec45b D a-charac.ads 20070912115821 2d3ec45b
D a-chlat1.ads 20070912115821 2825df20 D a-chlat1.ads 20070912115821 2825df20
D adaasn1rtl.ads 20140513071215 989f6c09 D adaasn1rtl.ads 20140513113246 989f6c09
D interfac.ads 20110906094621 129923ea D interfac.ads 20110906094621 129923ea
D system.ads 20140212114732 bd760655 D system.ads 20140212114732 bd760655
D s-io.ads 20090409150019 9e42f74f D s-io.ads 20090409150019 9e42f74f
D s-unstyp.ads 20090409150019 6ae15c76 D s-unstyp.ads 20090409150019 6ae15c76
D taste_basictypes.ads 20140513071215 72ec04b4 D taste_basictypes.ads 20140513113246 72ec04b4
D taste_dataview.ads 20140513071215 a40837cf D taste_dataview.ads 20140513113246 a40837cf
D trafficlight.ads 20140513071214 76d3ced9 D trafficlight.ads 20140513113246 76d3ced9
D trafficlight.adb 20140513071214 95fc595b D trafficlight.adb 20140513113246 95fc595b
X 4 adaasn1rtl.ads X 4 adaasn1rtl.ads
7K9*adaasn1rtl 908e15 12|11w6 12r5 7K9*adaasn1rtl 908e15 12|11w6 12r5
28I13*Asn1Int{5|52I9} 28I13*Asn1Int{5|52I9}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment