abc the Aspect Bench Compiler for Aspect J

abc : the Aspect. Bench Compiler for Aspect. J Oege de Moor Programming Tools Group University of Oxford Joint work with: Chris Allan, Pavel Avgustinov, Sascha Kuzins, Neil Ongkingco, Damien Sereni, Ganesh Sittampalam, Julian Tibble (Oxford), Laurie Hendren, Jennifer Lhoták, Ondřej Lhoták, Bruno Dufour, Christopher Goard, Clark Verbrugge (Mc. Gill), Aske Simon Christensen (Aarhus)

What is Aspect. J? Disciplined Metaprogramming

Bluffer’s guide to aspect-lingo Static: Dynamic: inject new members into existing classes at compile-time aspects observe composite events in base program; run extra code at begin/end of certain events joinpoint = composite event = node in generalised dynamic call graph pointcut = pattern of events = set of nodes in call graph advice = extra code shadow = program point that corresponds to joinpoint

Events, joinpoints and advice Program: int x; void f() { x = x+1; } execute: … f(); … events: begin/end joinpoints: boxes begin call f(); begin execution f(); begin get x; end get x; begin set x; end execution f(); end call f(); Advice can be run: before a joinpoint (immediately following “begin” event) after a joinpoint (immediately preceding “end” event) around a joinpoint (replacing whole contents of box between begin/end)

Example aspects Aspect. J: extension of Java

No allocations with “new” in inner loop aspect No. New. In. Round { private int allocations; before() : call(* World. play(. . )) { allocations = 0; } before() : cflow(call(* World. play(. . ))) && call(*. new(. . )) && !within(No. New. In. Round) { System. err. println("alloc at: "+ this. Join. Point. get. Source. Location()); allocations++; } after() : call(* World. play(. . )) { if (allocations > 0) System. err. println("allocations per game "+allocations); } }

Memoisation aspect Memo { Hashtable; pointcut to. Memo() : call(Integer ackermann(Integer)); before() : to. Memo() && !cflowbelow(to. Memo()) { table = new Hashtable(); } Integer around(Integer n) : to. Memo() && args(n) { Integer entry = (Integer) table. get(n); if (entry == null) { entry = proceed(n); table. put(n, entry); } return entry; } }

Observing an oblivious subject aspect Observe { List Subject. observers = new Array. List(); after(Subject s) returning(Observer o) : call(Observer. new(. . )) && args(s) { s. observers. add(o); } after(Subject s) : call(* Subject. update(. . )) && target(s) { for (Iterator obsit = s. observers. iterator(); obsit. hasnext(); ) { Observer o = (Observer) obsit. next(); o. refresh. View(); } } }

AOP languages: summary Conceptual model: traces of (composite) events at runtime Pointcuts: query language for events Advice: run extra code before/after/around selected events

Compiling Aspect. J Match events at compile time whenever possible: “weaving”

Aspect. J compilers ajc: • de facto standard • developed at Xerox, then IBM • extends Eclipse compiler • integrated with Eclipse IDE abc: • research compiler • extensible for experiments in language design • aggressive optimisation • no IDE integration

The Aspect. Bench Compiler Polyglot-based frontend . class . java parsing, type-checking Aspect. J AST separator Soot-based backend Java AST code generation + static weaving Jimple IR advice weaving + postprocessing bytecode Aspect Info

Close-up of the advice weaver Jimple IR for bytecode Shadow finder IR for pointcuts Shadows Matcher Jimple is a typed, stackless 3 -address intermediate representation of Java bytecode Weaving instructions Optimiser Weaver Analysis results Woven Jimple Analyser Bytecode generator

Does this work? cflow benchmarks (1)

cflow benchmarks (2)

Sample language extension Tracematches: match regular patterns on sequences of begin/end events

Failsafe enumeration over vectors public aspect Fail. Safe. Enum { pointcut vector_update() : call(* Vector. add*(. . )) || … ; tracematch(Vector ds, Enumeration e) { && args(ds); target(e); sym create_enum after returning(e) : call(Enumeration+. new(. . )) sym call_next before : call(Object Enumeration. next. Element()) && sym update_source after : vector_update() && target(ds); create_enum call_next* update_source+ call_next { throw new Concurrent. Modification. Exception(); } } }

Database connection pooling public aspect DBConnection. Pooling. TM { … Connection tracematch(Connection connection, String url, String uid, String password) { sym get_connection 1 after returning(connection) : connection. Creation(url, uid, password); sym get_connection 2 around (url, uid, password): connection. Creation(url, uid, password); sym release_connection before: connection. Release(connection); get_connection 1 release_connection get_connection 2 { return connection; } } void around() : connection. Release(*) { } }

Anatomy of an abc extension No abc. tm glue for new, extended compiler ast 17 new AST node classes new AST node factory parse new lexer and parser rules visit one new pass to translate tracematches to advice weaving aspect. Info IR for tracematches (3 classes) matching compile-time state machines (5 classes) weaver Jimple code generation (4 classes) ge an ch o t er ev so at wh e as cb ab de co

Performance of generated code Memory usage of animations in JHot. Draw + Fail. Safe. Enum: NO leaks! Running time of dbpooling: (seconds) Pure Java without pooling: 6. 0 with hand-coded pooling aspect: 1. 0 with tracematch 1. 2

Sample of other abc extensions n Bruno Harbulot: Loops. AJ loop joinpoints for parallelisation n Aotani & Masuhara: SCo. PE static evaluation of conditional pointcuts n Bodden & Stolz: J-LO run-time checking of LTL properties

abc goes shopping at GPCE What typical GPCE technologies might be used in a tool like abc?

Frontend shopping list n Automated pass ordering demand-driven attribute evaluation n Extending AST nodes in middle of hierarchy virtual classes; nested inheritance n Rewrite rules and strategies ) n a m k : r d E fo an al) t ke din r et r a m (He isse e th dd (V In st. A go Ja rate St

Backend shopping list n Quotation for Jimple n Guarantees that generated Jimple is well-formed a r : e r bo o f t n al) e e k v r t a a r e B m ( g n e g a th Bor Hu n I ( a t n Me fege Sa V d n r) e iss

Aspect. J design shopping list n n In n se th m em an a tic rk s! et fo r: n Virtual classes in lieu of intertype declarations Regular query language (Datalog? ) for traces and static structure Hiding (and exposure) of implementation detail to various degrees of “pure” advice (TR abc-2005 -2) Interaction of all this with generics

Further information: http: //aspectbench. org Papers, dissertations, talks Downloads Bug reports Mailing lists

Oxford Centre for Metacomputation Oxford University Computing Laboratory A new EPSRC-funded venture led by: Samson Abramsky, Tom Melham, Oege de Moor, and Luke Ong types for reflection, termination analysis, compositional model checking of higher-order programs, games semantics for aspects 4 -year postdoc position available further postdocs for a shorter period ask Oege for further information
- Slides: 27