Bdl The B lock D escription L anguage Pierre Weis - Cl´ ement Franchini Carg` ese – 2014 October 19
Motivation Block diagram simulation tools (such as Scicos, Simulink) are routinely used for modeling and simulate dynamical systems. However, seen as modeling languages, they lack a precise defi- nition. Programs correctness is thus difficult to state (if not impossible). Yet, block diagram programs are developped with the aim of generating software embedded into critical systems. 1 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Proposal Design a domain specific language (DSL) to describe the seman- tics of blocks and their connections. High-level language specialized to block description. For sake of safety and efficiency we need: • Strong and static type-checking. • Compilation (in particular static binding discipline). • Semantically sound definition of the language constructs. 2 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Bdl Our DSL is the B lock D escription L anguage (Bdl). Characteristics • Builtin matrix values and matrix types. • Matrix size statically verified. • All variables and matrices are initialized. • Language constructions are side effect free (pure). • Very limited memory side effects (assignments are restricted to variables declared as mutable ). 3 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Static semantic description Type reconstruction, ` a la ML (Damas-Milner algorithm). Compiler automatically finds types and verifies type adequacy. No type annotations in programs (except for interfaces). Polymorphic typing, using type schemes with quantified type pa- rameters. 4 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Polymorphic values and functions [ n, m ] t is the type of a matrix of t values with n rows and m columns. [ 0, 0 ] :: [1, 2] int // An integer vector of size 2 [ 1.1, 2.2; 3.0, 4.0 ] :: [2, 2] float // A 2x2 float matrix Matrix addition has a highly polymorphic type scheme: add_matrix :: for all m n t, function ([m, n] t, [m, n] t) -> [m, n] t 5 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Bdl generic functions Generic functions describe the meaning of high-level operators used in blocks (such as arithmetic operators). The dynamic semantics of generic functions is described in terms of the compiler’s target language basic primitives. A generic definition is not adequately typable, but all applications of a generic function must statically evaluate to a well-typed expression. Static semantics of generic functions is thus given by a combi- nation of partial evaluation and type reconstruction. 6 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Generic addition generic ( + ) (x, y) = assert (type of x == type of y); switch size of x case [1, 1], add_scalar (x, y); otherwise, (m, n) = size of x; mutable res = y; for i = 1 : m do for j = 1 : n do res := [res with i, j = add_scalar (x [i, j], y [i, j])]; end; end; res; end; end; 7 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Generic scalar addition generic add_scalar (x, y) = assert (type of x == type of y); t = type of x; switch t case int8, add_int8 (x, y); case int, add_int (x, y); case float, add_float (x, y); otherwise, partial_evaluation_error ( "no instance of add_scalar for type " ^^ string_of_type (t)); end; end; 8 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Primitive addition Primitive addition for short integers is an external definition (not defined in Bdl). external add_int8 :: function (int8, int8) -> int8 = external "bdl_add_int8"; bdlc will translate any call to “ add int8 ” to a call to the target language primitive addition for eight bits long signed integers. Same definitions for other scalar types (int16, int32, float, ...). 9 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Application of generic addition 1 + 2; generic ( + ) (1, 2) => type of 1 and type of 2 is int => assertion is verified => size of x is [1, 1] => call add_scalar (1, 2) => ... add_int (1, 2) => 3 10 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Generic application type-checking 1.0 + 2; generic ( + ) (1.0, 2) => type of 1.0 is float type of 2 is int => assertion is not verified Static type checking error. 11 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Dynamic semantic description Small step semantics (source code to source code rewriting rules). Dynamic semantics is described by a partial evaluator. Semantics is defined as iteration of partial evaluation up to nor- mal form. 12 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Difficulties • Mixing static typing and partial evaluation is challenging. • Provide a Bdl semantics for blocks that have a fuzzy defini- tion in the first place. 13 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Current state of the compiler The front end is written: - Concrete syntax is defined - YACC parser written The middle end is actively developped: - Scope analysis completed - Type reconstruction is in progress - Sketchy partial evaluation The back end is future work: - The compiler will produce C source code. - Tied to the codegen toolbox for NSP. - Still a joint work with ENPC & ALTAIR. 14 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Conclusion bdlc need to import existing models providing a plausible seman- tics to programs in other modelers (Simulink, Scicos or ScicosPro models). bdlc should be a fast and reliable compiler for block diagram programs written in Bdl. 15 Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
Recommend
More recommend