Research funded by SSF RISE-PARACOMP, CUGS Graduate School, LiU Ceniit Proc. ICSE-2007 Minneapolis Joint work with Mikhail Chalabine IDA / PELAB Linköpings universitet, Sweden Christoph Kessler in Static Aspect Weaving and Transformations Automated Roundtrip Engineering time Aspect source WovenCode C. Kessler, IDA, Linköpings universitet. Core source WovenCode 2 ARE (Automatic Roundtrip Engineering) Problem: How to trace back and consistently commit (manual) changes in transformed code to the right source of origin? Sequences of (syntactic) program transformations (weaving steps) Interactive (manual) editing Rule-based source-to-source transformation C macro expansion Static Aspect Weaving ... Woven Core’’ Woven Core’ Woven Core weave weave weave Application Core Static Aspect Weaving ... and other program transformations Manual Editing Aspect Advice Aspect Advice NT(v1)…NT(vk) T C. Kessler, IDA, Linköpings universitet. 3 Syntactic substitutability of Tw only with tree (fragment) of type compatible to NT(w) Generic CST / tree fragment: CST with ”open” leaves (= non-expanded nonterminals) Syntactic Type of CST: NT( root ) Production NT(w) Inner node w with children v1,…,vk: Leaf node b annotated with terminal: NT(b) Inner nodes w annotated with nonterminal: NT(w) Including whitespaces, semicolons, comments etc. CST: Parse tree / Derivation tree Formalisation: Concrete Syntax Tree (CST) Leaf_word (Tw ) Tw w v w D C. Kessler, IDA, Linköpings universitet. Notation: C TE TB T TD C TA A B T T E Γ T’ F ΓF Γ 4 G ΓG m r A v w F C C G T’ E T’ T’ F T’ G T’ A T’ E Syntactically correct if NT(w) = NT(r) and NT(v) = NT(m) (1,1)-Double-Grafting-Transformation: Formalisation (2) Double-Grafting Transformation on CSTs v w D C. Kessler, IDA, Linköpings universitet. C TE TB TC TD TA A B T E F ΓF Γ 5 G ΓG m r A v w F C G T’ E T’ T’ F T’ C G T’ A T’ With missing proceed()-pseudocall (= no m-node): (1,0)-Double-Grafting Transformation Application of an advice at a join point (shadow) is a (1,1)-Double-Grafting-Transformation Static Aspects (before, after, around join point) (e.g. early version of Aspect-J) Example 1: Static Aspect Weaving E C. Kessler, IDA, Linköpings universitet. AOP Example 6 The woven code is in Aspect-J not visible for the programmer! (Weaving is done at bytecode level …) v w TE D C. Kessler, IDA, Linköpings universitet. C TB TC TD TA A B T E F ΓF Γ 7 G ΓG m r A v w F C G T’ E T’ T’ F T’ C G T’ A T’ Editor coupled with CST search structure – Clicking / Cursor on program point locates CST-node Program again syntactically correct after editing Requirements: Delete, replace: (1,0)-Double-Grafting-Transformation Insert: (1,1)-Double-Grafting-Transformation Example 2: Editing E vk Dk E vi l F C. Kessler, IDA, Linköpings universitet. 8 A F r G1 w ml vi 1 G1 m1 C i1 where vi j in { v1, …, vk } for j = 1, …, l, and NT(mj) = NT(vi j) vi 1 , …, mk C1 D1 C2 D2 Ck v2 + Mapping: m1 A B v1 w ( k, l )-Double-Grafting-Transformation C il vi l Gl Gl E MAX( X, Y ) vk C. Kessler, IDA, Linköpings universitet. A B C1 D1 C2 D2 Ck Dk E v1 v2 w F 9 G1 m1 ml r Gl A F C i1 G1 vi 1 C il vi l w Gl ( (X) > (Y)? X : Y ) is a (2,4)-Double-Grafting-Transformation … = MAX( u+1, 23 ); #define Macro Expansion Example 3: C - Preprocessor E vk C. Kessler, IDA, Linköpings universitet. A B C1 D1 C2 D2 Ck Dk E v1 v2 w F 10 G1 m1 ml r Gl A F C i1 G1 vi 1 2 * id:1 is a (2,1)-Double-Grafting-Transformation @expr: @id:1 + @id:1 Generic Transformation Rule Example 4: Rule-based Source-toSource Transformation Systems C il vi l w Gl E time 11 Aspect source WovenCode C. Kessler, IDA, Linköpings universitet. Core source WovenCode ARE (Automatic Roundtrip Engineering) Problem: How to trace back and consistently commit (manual) changes in transformed code to the right source of origin? Sequences of (syntactic) program transformations (weaving steps) Interactive (manual) editing Rule-based source-to-source transformation C macro expansion Static Aspect Weaving ... Woven Core’’ Woven Core’ Woven Core weave weave weave Application Core Static Aspect Weaving ... and other program transformations Manual Editing Aspect Advice Aspect Advice C. Kessler, IDA, Linköpings universitet. Commit update consistently? Commit Ignore Commit Ignore 12 Thismodification modificationapplies appliestotoaspect aspect This secureTrans,advice advicebefore before(line (line8) 8). secureTrans, which also crosscuts in linesource? 10. Commit update to aspect ARENotice: Notice: ARE ARE Example T1 Γ2 T2 Γ3 T3 … Γn Tn Method: Store relative coordinate transformation to each Double-Grafting-Transformation in the weaving sequence Transposition tree C. Kessler, IDA, Linköpings universitet. 13 either in the core T0 or in one of the Γi, i = 1…n Compute: Source-File + Coordinate of p in Origin of p Given: Program point coordinate w.r.t. Tn (row r, column c) in Editor or, linear index p in Lexeme Sequence (leaf word) T0 Γ1 Given: Weaving sequence (History) Problem: Find Origin of a Program Point v w D C. Kessler, IDA, Linköpings universitet. ? C TE TB T TD C TA A B T E F ΓF Γ 14 G ΓG m r ? A v w F p C C G q T’ E T’ T’ F T’ G T’ A T’ As example: for a (1,1)-Double-Grafting-Transformation Transposition Tree Construction (1) E 15 fposT (v) + |A| - |B| + |F| + |C| - |D| + |G| if v in T’E fposΓ (v) + |A| - |B| + |F| + |C| if v in T’G fposT (v) + |A| - |B| + |F| if v in T’C fposΓ (v) + |A| if v in T’F fposT (v) if v in T’A C. Kessler, IDA, Linköpings universitet. fposT’ (v) = fsrcT’ (v) = fsrcT (v) if v in TA, TC, TE fsrcΓ (v) if v in TF, TG undefined, if v in TB, TD Forward-Transposition: Transposition Tree Construction (2) A +0 A F B C. Kessler, IDA, Linköpings universitet. Ttn(T’) Ttn(T) C -|F|+|B| C G D E 16 E F -|A|-|C| -|F|-|G|+|B|+|D| -|A| Ttn(Γ) Nodes / Edges in Transposition tree Backward transposition stored blockwise All coordinates in the same segment (A, B, …) are mapped (transposed) in the same way G Transposition Tree Construction (3) e1,1 A F F B H e2,3 C e1,3 C p E e1,5 I G e2,5 D E C. Kessler, IDA, Linköpings universitet. 17 Search backwards in time O(n) Data structure needs O(n) space (Constant depends on k, l) … Ttn(T2 ) A e2,1 Ttn(T1 ) A Ttn(T0 ) e1,4 G E e2,4 H F I G Input: Coordinate p v Ttn ( Tn ); // Ttn for current CST for t from n downto 1 do find interval i of p in v (Binary search) p Backwardtransp[ et,i ] ( p ); v target ( et,i ); if v is Ttn of an aspect Γ break od e2,2 Ttn(Γ2 ) e1,2 Ttn(Γ1 ) Incremental construction yields tree of depth <= n Transposition Tree Construction (4) 2 Options (User dialog): 3 Options (User dialog): C. Kessler, IDA, Linköpings universitet. 18 Store and commit edit as local patch Γn+1 Patch edit directly in Advice (as Γ0 ), then replay Γ1,…,Γn Commit after Γt for some 0<t<n, then replay Γt+1,…,Γn Origin in Aspect-Advice Store and commit edit as local patch Γn+1 of Tn Patch edit directly in the core (as Γ0 ), then replay Γ1,…,Γn Origin in core T0 At a change (e.g. edit): Undo Γn by replaying Γ1,…,Γn-1 on T0 Weaving sequenz can always be replayed Propagating Changes (1) Γ2 Γ3 T1 T2 T’3 … Γ3’ T3 … C. Kessler, IDA, Linköpings universitet. 19 Limit scope of propagating changes, undo option Encapsulate inconsistent intermediate configurations Remedy: Weaving Transactions T0 Γ1 Γn Γn’ T’ n’ Tn Change can lead to disruptive changes in the replay – code segments created ”later” may disappear, new ones appear… Paradox (cf. ”Back to the future II”) Not all Tt are (semantically) correct programs Problems: Propagating Changes (2) C. Kessler, IDA, Linköpings universitet. 20 Co-Evolution of application core and woven-in parallelisation code Sequential application core remains intact Manual parallelisation (forall, sh, $ivdep, $distribute …) Semiautomatic transformations (Skeleton expansion) Rule-based automatic transformations Planned: Interactive Parallelisation Tool Application? C. Kessler, IDA, Linköpings universitet. 21 ☺ Space-efficient data structure ☺ Need not know reverse transformation to each step User dialog for consistent change propagation Replay with transposition tree find origin of a program point Store weaving sequence Models static aspect weaving, Macro expansion, Edits … (k, l )-Double-Grafting-Transformation Summary C. Kessler, IDA, Linköpings universitet. 22 Mikhail Chalabine, Christoph Kessler: A Formal Framework for Automated Round-trip Software Engineering in Static Aspect Weaving and Transformations, Proc. of the 29th IEEE / ACM SIGSOFT Int. Conference on Software Engineering (ICSE 2007), Minneapolis, MN, USA, May 2007 Mikhail Chalabine, Christoph Kessler, Peter Bunus: Automated Round-trip Software Engineering in Aspect Weaving Systems (Short paper), Proc. of the 21st IEEE / ACM International Conference on Automated Software Engineering (ASE 2006), Tokyo, Japan, September 2006 Uwe Assmann: Automatic Roundtrip Engineering. Electronic Lecture Notes in Theoretical Computer Science 85(5), 2003 Uwe Assmann, Andreas Ludwig: Aspect Weaving by Graph Rewriting. Proc. Generative Component-Based Software Engineering (GCSE), 1999 References