Automatic Code Generation from Stateflow Models

Similar documents
Simulink/Stateflow. June 2008

Specifications Part 1

Clock-directed Modular Code-generation for Synchronous Data-flow Languages

Gene-Auto development status and support

Concurrent Models of Computation

Denotational Semantics. Domain Theory

Modal Models in Ptolemy

Synchronous Specification

Synchronous Statecharts. Christian Motika

Early design phases. Peter Marwedel TU Dortmund, Informatik /10/11. technische universität dortmund. fakultät für informatik informatik 12

States Transitions Connectors Esterel Studio

Prof. Dr. Reinhard von Hanxleden. Christian-Albrechts Universität Kiel Department of Computer Science Real-Time Systems and Embedded Systems Group

Semantics of Statecharts

Hybrid System Modeling: Operational Semantics Issues

SCXML State Chart XML

Modular code generation from synchronous models:

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

Embedded software design with Polychrony

Section 8. The Basic Step Algorithm

CISC836: Models in Software Development: Methods, Techniques and Tools

Compilation of Heterogeneous Models: Motivations and Challenges 1

The Esterel language

Lecture 2. The SCADE Language Data Flow Kernel. Daniel Kästner AbsInt GmbH 2012

Introduction 2 The first synchronous language (early 80 s) Gérard Berry and his team (École des Mines de Paris / INRIA Sophia-Antipolis) Imperative, s

Unified Modeling Language 2

Visual Layout of Graph-Like Models

An Introduction to Lustre

From synchronous models to distributed, asynchronous architectures

challenges in domain-specific modeling raphaël mannadiar august 27, 2009

Distributed Systems Programming (F21DS1) Formal Verification

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Programming Embedded Systems

Modeling Hybrid Systems with Petri Nets

Com S 541. Programming Languages I

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS522 Programming Language Semantics

Embedded Systems. Problem 1: Getting started with STATEFLOW. Starting STATEFLOW

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics

Łabiak G., Miczulski P. (IIE, UZ, Zielona Góra, Poland)

SCXML State Chart XML. Previously, in this course...

Interactive Esterel to SyncCharts Transformation. Christian Motika

CSCE 314 Programming Languages

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

DIVERSITY TG Automatic Test Case Generation from Matlab/Simulink models. Diane Bahrami, Alain Faivre, Arnault Lapitre

Outline. SLD challenges Platform Based Design (PBD) Leveraging state of the art CAD Metropolis. Case study: Wireless Sensor Network

Handout 9: Imperative Programs and State

Alloy: A Lightweight Object Modelling Notation

Tsmart-BIPEX: An Integrated Graphical Design Toolkit for Software Systems

System-level co-modeling AADL and Simulink specifications using Polychrony (and Syndex)

AADL Graphical Editor Design

Software Development with Automatic Code Generation: Observations from Novice Developer Viewpoint

Introduction to Dependable Systems: Meta-modeling and modeldriven

2 nd UML 2 Semantics Symposium: Formal Semantics for UML

7. Introduction to Denotational Semantics. Oscar Nierstrasz

The Synchronous Languages 12 Years Later

Statecharts 1.- INTRODUCTION 1.- INTRODUCTION

Translation validation: from Simulink to C

COMP 763. Eugene Syriani. Ph.D. Student in the Modelling, Simulation and Design Lab School of Computer Science. McGill University

Executing SyncCharts with Ptolemy

Safe Reactive Programming: the FunLoft Proposal

Remote Control. IMMS.de

Overview of Dataflow Languages. Waheed Ahmad

Program generation for schema-based, typed data access

On the Generation of Test Cases for Embedded Software in Avionics or Overview of CESAR

This project has received funding from the European Union s Horizon 2020 research and innovation programme under grant agreement No

Synchronous Estelle: Just Another Synchronous Language?

Relational Algebra and Calculus. Relational Query Languages. Formal Relational Query Languages. Yanlei Diao UMass Amherst Feb 1, 2007

Contemporary Design. Traditional Hardware Design. Traditional Hardware Design. HDL Based Hardware Design User Inputs. Requirements.

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University

Ptolemy II The automotive challenge problems version 4.1

Lecture 16: Hierarchical State Machines II

Toward a Semantic Anchoring Infrastructure for Domain-Specific Modeling Languages

Compiler Construction

This book is licensed under a Creative Commons Attribution 3.0 License

Scenario-based Synthesis of Annotated Class Diagrams in UML

Monads. Mark Hills 6 August Department of Computer Science University of Illinois at Urbana-Champaign

Analysis of the combined use of SCADE and UML 2.x models. Project description. Ramin Hedayati. Member of GRADUIERTENKOLLEG EINGEBETTETE SYSTEME

Semantic Subtyping. Alain Frisch (ENS Paris) Giuseppe Castagna (ENS Paris) Véronique Benzaken (LRI U Paris Sud)

Databases = Categories

A Tabular Expression Toolbox for Matlab/Simulink

Zélus, a Synchronous Language with ODEs 1

Synchronous Kahn Networks (ten years later)

The essence of dataflow programming. Teooriapäevad Kokel 2,

1 Statecharts language and Statemate Magnum

Functional Logic Programming Language Curry

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

Concurrent Models of Computation

The design of a programming language for provably correct programs: success and failure

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

The Montana Toolset: OSATE Plugins for Analysis and Code Generation

ECL: A SPECIFICATION ENVIRONMENT FOR SYSTEM-LEVEL DESIGN

Chapter 15 Functional Programming Languages

Formal Systems and their Applications

Synoptic: a DSML for On-Board Real-Time Software Design

LECTURE 16. Functional Programming

Modal Logic: Implications for Design of a Language for Distributed Computation p.1/53

NOTES ON OBJECT-ORIENTED MODELING AND DESIGN

CA314 Object Oriented Analysis & Design - 7. File name: CA314_Section_07_Ver01 Author: L Tuohey No. of pages: 16

JVM ByteCode Interpreter

Cover Page. The handle holds various files of this Leiden University dissertation

Defining and translating a safe subset of Simulink/Stateflow into Lustre

Transcription:

Automatic Code Generation from Stateflow Models Andres Toom IB Krates OÜ / Institute of Cybernetics at TUT Based on the Master s Thesis 05.2007 Supervisors: Tõnu Näks, Tarmo Uustalu TUT Department of Computer Control and the Gene-Auto Project Theory Days, Vanaõue 2007

Outline Introduction The Gene-Auto Project Model Based System Design Declarative style Imperative style Stateflow Informal introduction Modelling considerations Formal specification of Stateflow Code generation from Stateflow Demo Conclusions 28-30.09.2007 Andres Toom - Teooriapäevad 2007 2

Introduction 28-30.09.2007 Andres Toom - Teooriapäevad 2007 3

The Gene-Auto project 28-30.09.2007 Andres Toom - Teooriapäevad 2007 4

The Gene-Auto project (contd.) Motivations Increasing complexity of embedded real-time systems Increasing demands for safety and reliability Shorter time-to-market development pressure Existing closed proprietary systems lack in flexibility and their vendors deny any liability for using their products. Aims Develop an open source code generator from mathematical style systems modelling languages (e.g Simulink/Scicos, Stateflow) Full qualification of the code generator according to the industry standards Integrate formal methods, as much as possible to reduce the amount of classical testing Initial target language is (platform independent) C Current work Provide a code generator prototype for the Stateflow language to explore and refine the functionality and semantics of Stateflow. 28-30.09.2007 Andres Toom - Teooriapäevad 2007 5

Specifying dynamic/reactive systems Two styles: Declarative ~ data-flow Imperative ~ automata Synchronous vs. asynchronous models Synchronous: Synchronicity hypotheses - computation instants are instantaneous and atomic, time passes only between the computations. Simpler to handle. Both, declarative and imperative variants exist: Lustre, Signal, synchronous data-flow Esterel, StateMate, synchronous automata Asynchronous Computations take time and are non-atomic More general, more complex GALS Globally Asynchronous Locally Synchronous 28-30.09.2007 Andres Toom - Teooriapäevad 2007 6

Declarative style of modelling dynamic/reactive systems Functional modelling, (mostly) data-flow oriented. Well suited for expressing systems represented as a set of differencial or difference equations. Examples: y(n) = K 1 x(n) + K 2 x(n-1) y(n) = x(n) + K 1 x(n-m 1 ) - K 2 y(n-m 2 ) 28-30.09.2007 Andres Toom - Teooriapäevad 2007 7

Declarative style of modelling dynamic/reactive systems (contd.) Many visual modelling tools exist Simulink, Scicos, Scade (Lustre), Sildex (Signal), Polychrony (Signal), Synchronous data-flow languages provide a rigorous formalism for specifying many systems Operate on (infinite) sequences of values over time Formal methods, e.g. model checking, can be applied on such models See for example, N. Halbwachs EWSCS 06. Simulink Most widely used mathematical modelling tool in practice Background in modelling continuous systems No rigorous formalism underneath. The semantics of the modelled system is defined by its behaviour during the simulation. Complete semantics more complex and powerful than that of synchronous data-flow languages. 28-30.09.2007 Andres Toom - Teooriapäevad 2007 8

Imperative style of modelling Synchronous language Esterel SyncCharts, Safe State Machines (SSM) Statecharts A visual formalism for specifying the behaviour of dynamic systems. Extends the classical finite state machine formalism, by adding: depth (hierarchy) orthogonality (parallel states) broadcast communication. Informal semantics proposed by David Harel in 1987 (1). Formal semantics, called the Statemate semantics of Statecharts, presented in 1987 (2) and 1996 by D. Harel et al. By 1994 over 20 variants of Statecharts existed that tried to refine some aspect of it. 28-30.09.2007 Andres Toom - Teooriapäevad 2007 9

Stateflow 28-30.09.2007 Andres Toom - Teooriapäevad 2007 10

Stateflow Based on the Statecharts formalism. Designed by the Mathworks Inc, part of the Matlab/Simulink toolset. Several unique additions. Combines StateCharts, flow-charts and truthtables in a unique way. A complex transition and action mechanism. Very expressive, but with caveats for the modeller. 28-30.09.2007 Andres Toom - Teooriapäevad 2007 11

Simulink/Stateflow - Example 28-30.09.2007 Andres Toom - Teooriapäevad 2007 12

Stateflow Modelling caveats Puzzling semantics 28-30.09.2007 Andres Toom - Teooriapäevad 2007 13

Stateflow Modelling caveats (contd.) Non-termination 28-30.09.2007 Andres Toom - Teooriapäevad 2007 14

Modelling restrictions! Complex semantics can easily lead to misestimating the exact run-time behaviour. Possibilities for non-termination of the computation exist. Such constructs are specifically forbidden in some other languages: e.g. Esterel, Safe Sate Machines. 28-30.09.2007 Andres Toom - Teooriapäevad 2007 15

Formal specification of Stateflow 28-30.09.2007 Andres Toom - Teooriapäevad 2007 16

The Stateflow Language Informally defined by the Mathworks. Reference manual is over 900 pages. The de facto semantics is defined by the simulation. Formal definition of a subset of Stateflow. Operational semantics - G. Hamon and J. Rushby (2004). Denotational semantics - G. Hamon (2005). 28-30.09.2007 Andres Toom - Teooriapäevad 2007 17

Stateflow syntax (Gene-Auto SF Metamodel) 28-30.09.2007 Andres Toom - Teooriapäevad 2007 18

Denotational semantics of Stateflow Approach from G. Hamon (2005) Environment Contains bindings of user variables and chart s statevariables to values type Env = (Maybe (Array SFDataId SimVal), (Array SFLocId LocState) Continuation environment Not used in the current implementation Defunctionalizing the continuation environment yields just SFChart SF language semantics is kept separate from the input model s structure Continuations to express the transition semantics Success: type Kplus = Env Dest Env Failure: type Kminus = Env Env 28-30.09.2007 Andres Toom - Teooriapäevad 2007 19

Success and fail continuations Continuations A mathematical formalism, capable of handling full jumps in computer programs (i.e. gotos ) Intuition - a way to formally deal with the rest of the program C. Strachey, C. P. Wadsworth 28-30.09.2007 Andres Toom - Teooriapäevad 2007 20

Success and fail continuations Continuations to express the transition semantics Success: type Kplus = Env Dest Env Failure: type Kminus = Env Env 28-30.09.2007 Andres Toom - Teooriapäevad 2007 21

Revised success continuation Continuations of type: Env Dest Env Are insufficient to correctly build the evaluation sequence of actions/activites Need a different approach, 28-30.09.2007 Andres Toom - Teooriapäevad 2007 22

Revised success continuation (contd.) Second problem: What to do, when terminal junctions appear together with states? Need a third continuation type: type KTerm = Env Env And Distinguishing between pure flow-graph networks and flow-graphs networks mixed with states. 28-30.09.2007 Andres Toom - Teooriapäevad 2007 23

Revised success continuation (contd.) Revised success continuation type: data KPos = KPosFG KTerm KPosOuter SFStateId [SFAct] KTerm KPosInner SFParentId [SFAct] Kterm KPosDefault SFParentId [SFAct] KTerm (Defunctionalized) 28-30.09.2007 Andres Toom - Teooriapäevad 2007 24

Semantical functions Evaluating a chart Entering a chart Entering a composition Entering a state 28-30.09.2007 Andres Toom - Teooriapäevad 2007 25

Semantical functions (contd.) Evaluating a transition 28-30.09.2007 Andres Toom - Teooriapäevad 2007 26

Semantical functions (contd.) Evaluating a transition list 28-30.09.2007 Andres Toom - Teooriapäevad 2007 27

Code generation from Stateflow 28-30.09.2007 Andres Toom - Teooriapäevad 2007 28

Code generation via partial evaluation of the semantics The semantic function for evaluating the chart: runchart :: SFChart Env EventId Env Result of partial evaluation against the SFChart: runchart :: Env EventId Env 28-30.09.2007 Andres Toom - Teooriapäevad 2007 29

Specific considerations with partial evaluation Inlining amount Need a way to control evaluation. First, we don t want to evaluate everything, because: run-time computations must not get evaluated during the code generation we might not want to give a specification of primitive functions One solution Supply a list of abstract or primitive functions to the evaluator: [(Identifier, Arity)] 28-30.09.2007 Andres Toom - Teooriapäevad 2007 30

Specific considerations with partial evaluation Inlining amount (contd.) Second, a naive evaluation would inline still too much. For example, consider following action statements: udata[1] = udata[2] = udata[1] + 1 udata[3] = udata[1] + 10 A straightforward evaluation would recreate the evaluation sequence of udata[1] several times. Goal Inline/evaluate only the meta-actions actions related to evaluating the chart s semantics. Do not evaluate the actions that have intended effects on the environment. One solution Augment the specification language with a special construct. 28-30.09.2007 Andres Toom - Teooriapäevad 2007 31

Specific considerations with partial evaluation Inlining amount (contd.) A local keyword is introduced. Defined in Haskell as follows: Used as an indicator for the partial evaluator to preserve a local let-binding. Example: Automatic alternatives are possible. 28-30.09.2007 Andres Toom - Teooriapäevad 2007 32

Specific considerations with partial evaluation Loops Loops in the evaluated program. Flow-graph loops in Stateflow correspond to loops in traditional imperative programs and in general may not terminate. The partial evaluator needs to a criterion to stop. 28-30.09.2007 Andres Toom - Teooriapäevad 2007 33

Specific considerations with partial evaluation Loops (contd.) A lbl keyword is introduced. Defined in Haskell as follows: A special meaning for the partial evaluator: a label has to be generated the first time a lbl with a new number is seen and a goto any other time. The rest of the expression is evaluated only the first time. In Stateflow this also solves the issue of evaluating joining paths. Certain jumps can be transformed to whiles, fors or ifs later. 28-30.09.2007 Andres Toom - Teooriapäevad 2007 34

The HTr partial evaluator Developed for the purpose of the current project. Input language Haskell. Supported constructs: pattern matching function and constructor application (incl. infix application) lambda abstraction if and case expressions local let binding tuple and list expressions and list construction Introduced additional language constructs for specifiying primitives, maintaining locality and dealing with loops Generic, does not know Stateflow Implemented also in Haskell. 28-30.09.2007 Andres Toom - Teooriapäevad 2007 35

Tool architecture with a partial evaluator class SF code generator Input Code generator Output SF codegen:: Model parser SF codegen:: Partial ev aluator SF codegen:: PostProcessor takes as input produces as output takes as input produces as output takes as input produces as output SLInterfaces:: MDLFile GALanguage::GA (SF) Model takes as input SF codegen:: Abstract code SF codegen::c Code SF codegen:: Specification parser takes as input produces as output SF codegen:: Formal specification of Stateflow SF codegen:: Abstract specification of Stateflow 28-30.09.2007 Andres Toom - Teooriapäevad 2007 36

Code generation via manual transformation of the semantics Main idea Keep the form of the original executable specification. Rewrite it so that instead of outputting a modified environment it will output an expression that does that. The semantic function for evaluating the chart: runchart :: SFChart Env EventId Env Transformed function: runchart :: SFChart CmStmt 28-30.09.2007 Andres Toom - Teooriapäevad 2007 37

Manual transformation of the semantics. Example Original semantic function 28-30.09.2007 Andres Toom - Teooriapäevad 2007 38

Manual transformation of the semantics. Example (contd.) Transformed semantic function 28-30.09.2007 Andres Toom - Teooriapäevad 2007 39

Tool architecture with manually transformed semantics class SF code generator (manual transf.) Input Code generator Output SF codegen:: Formal specification of Stateflow «is manually transformed to» SF codegen::code generator core (2) SF codegen:: Model parser SF codegen:: PostProcessor (2) takes as input produces as output takes as input produces as output takes as input produces as output SLInterfaces:: MDLFile GALanguage::GA (SF) Model SF codegen:: Abstract code (2) SF codegen::c Code 28-30.09.2007 Andres Toom - Teooriapäevad 2007 40

Demo 28-30.09.2007 Andres Toom - Teooriapäevad 2007 41

Results A refined version of formal semantics of Stateflow has been specified. A code generator prototype based on that semantics has been created. Small-scale tests show conformance with the de facto Stateflow semantics. Some secondary features remain to be implemented to enable testing on real industrial test-cases. Creating a qualified version of the tool and using the results presented here in creating a formally validated code generator remain subjects for future work. 28-30.09.2007 Andres Toom - Teooriapäevad 2007 42