Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Xiaoqing Wu, Barrett R. Bryant, Jeff Gray and Suman Roychoudhury University of Alabama at Birmingham Separation of Concerns in Compiler Development Using.

Similar presentations


Presentation on theme: "1 Xiaoqing Wu, Barrett R. Bryant, Jeff Gray and Suman Roychoudhury University of Alabama at Birmingham Separation of Concerns in Compiler Development Using."— Presentation transcript:

1 1 Xiaoqing Wu, Barrett R. Bryant, Jeff Gray and Suman Roychoudhury University of Alabama at Birmingham Separation of Concerns in Compiler Development Using Aspect-Orientation Marjan Mernik University of Maribor

2 2 The Problem: Modularization of Compiler Phases package org.apache.tomcat.session; import org.apache.tomcat.core.*; import org.apache.tomcat.util.StringManager; import java.io.*; import java.net.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; /** * Core implementation of a server session * * @author James Duncan Davidson [duncan@eng.sun.com] * @author James Todd [gonzo@eng.sun.com] */ public class ServerSession { private StringManager sm = StringManager.getManager("org.apache.tomcat.session"); private Hashtable values = new Hashtable(); private Hashtable appSessions = new Hashtable(); private String id; private long creationTime = System.currentTimeMillis();; private long thisAccessTime = creationTime; private int inactiveInterval = -1; ServerSession(String id) { this.id = id; } public String getId() { return id; } public long getCreationTime() { return creationTime; } public ApplicationSession getApplicationSession(Context context, boolean create) { ApplicationSession appSession = (ApplicationSession)appSessions.get(context); if (appSession == null && create) { // XXX // sync to ensure valid? appSession = new ApplicationSession(id, this, context); appSessions.put(context, appSession); } // XXX // make sure that we haven't gone over the end of our // inactive interval -- if so, invalidate and create // a new appSession return appSession; } void removeApplicationSession(Context context) { appSessions.remove(context); } /** * Called by context when request comes in so that accesses and * inactivities can be dealt with accordingly. */ void validate() package org.apache.tomcat.session; import org.apache.tomcat.core.*; import org.apache.tomcat.util.StringManager; import java.io.*; import java.net.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; /** * Core implementation of a server session * * @author James Duncan Davidson [duncan@eng.sun.com] * @author James Todd [gonzo@eng.sun.com] */ public class ServerSession { private StringManager sm = StringManager.getManager("org.apache.tomcat.session"); private Hashtable values = new Hashtable(); private Hashtable appSessions = new Hashtable(); private String id; private long creationTime = System.currentTimeMillis();; private long thisAccessTime = creationTime; private int inactiveInterval = -1; ServerSession(String id) { this.id = id; } public String getId() { return id; } public long getCreationTime() { return creationTime; } public ApplicationSession getApplicationSession(Context context, boolean create) { ApplicationSession appSession = (ApplicationSession)appSessions.get(context); if (appSession == null && create) { // XXX // sync to ensure valid? appSession = new ApplicationSession(id, this, context); appSessions.put(context, appSession); } // XXX // make sure that we haven't gone over the end of our // inactive interval -- if so, invalidate and create // a new appSession return appSession; } void removeApplicationSession(Context context) { appSessions.remove(context); } /** * Called by context when request comes in so that accesses and * inactivities can be dealt with accordingly. */ void validate() package org.apache.tomcat.session; import org.apache.tomcat.core.*; import org.apache.tomcat.util.StringManager; import java.io.*; import java.net.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; /** * Core implementation of a server session * * @author James Duncan Davidson [duncan@eng.sun.com] * @author James Todd [gonzo@eng.sun.com] */ public class ServerSession { private StringManager sm = StringManager.getManager("org.apache.tomcat.session"); private Hashtable values = new Hashtable(); private Hashtable appSessions = new Hashtable(); private String id; private long creationTime = System.currentTimeMillis();; private long thisAccessTime = creationTime; private int inactiveInterval = -1; ServerSession(String id) { this.id = id; } public String getId() { return id; } public long getCreationTime() { return creationTime; } public ApplicationSession getApplicationSession(Context context, boolean create) { ApplicationSession appSession = (ApplicationSession)appSessions.get(context); if (appSession == null && create) { // XXX // sync to ensure valid? appSession = new ApplicationSession(id, this, context); appSessions.put(context, appSession); } // XXX // make sure that we haven't gone over the end of our // inactive interval -- if so, invalidate and create // a new appSession return appSession; } void removeApplicationSession(Context context) { appSessions.remove(context); } /** * Called by context when request comes in so that accesses and * inactivities can be dealt with accordingly. */ void validate() Compiler implementation is often an intricate task due to the complexity and interconnected nature of various stages within the compiler. Type checking static analysis Code generation

3 3 Problems 1. Code scatters all over the classes. 2. Hard to maintain and evolve. Pure Object-Oriented Design

4 4 The Goal: Separation of Concerns The key challenge is to provide exceptional modularity that assists in properly separating several crosscutting concerns, which not only helps the developer to divide-and-conquer the complexity, but also improves the readability, reusability and extensibility of the compiler implementation. Syntax analysis Type checking static analysis Code generation

5 5 Popular Solution: the Visitor Pattern Problems 1. Introduce a lot of extra code. 2. Forces all concrete visitors share the same interface. 3. New semantics are always introduced by traversal of the whole tree. 4. Cannot access private members of a node class.

6 6 New solution: Aspect-Oriented Programming Aspect-Oriented Programming provides special language constructs called aspects to modularize crosscutting concerns in conventional program structures. –Introduction (inter-type declarations) –Interception (join-points) AOP: handling crosscutting concerns Semantic phase: a concern that crosscuts various AST nodes AOP is an excellent programming technology to apply to semantic analysis

7 7 Aspect-Oriented Semantics Implementation Each concern is modularized as an aspect –An independent semantic pass –A group of action codes Semantic pass (i.e., a visitor) –Implemented as introductions of the AST classes Crosscutting actions applied to a group of nodes –Weaved into AST classes as interceptions.

8 8 Introduction

9 9 /*................................ */ parser_code_part : := PARSER CODE CODE_STRING:u ser_code opt_semi {: if (emit.parser_code! =null) lexer.emit_error(" Redundant parser code (skipping)"); else /* save the user included code string */ emit.parser_code = user_code; :} ; /*................................ */ init_code ::= INIT WITH CODE_STRING:u ser_code opt_semi {: if (emit.init_code!=n ull) lexer.emit_error(" Redundant init code (skipping)"); else /* save the user code */ emit.init_code = user_code; :} ; /*................................ */ scan_code ::= SCAN WITH CODE_STRING:u ser_code opt_semi {: /* reset the accumulated multipart name */ multipart_name = new String(); :} SEMI ; /*................................ */ declares_term ::= term_name_list {: /* reset the accumulated multipart name */ multipart_name = new String(); :} SEMI ; /*................................ */ declares_non_term ::= non_term_name_list {: /* reset the accumulated multipart name */ multipart_name = new String(); :} SEMI ; /*................................ */ term_name_list ::= term_name_list COMMA new_term_id | new_term_id; /*................................ */ non_term_name_list ::= non_term_name_list COMMA new_non_term_id | new_non_term_id /*................................ */ precedence_list ::= precedence_l | empty; /*................................ */ precedence_l ::= precedence_l preced | preced; /*................................ */ preced ::= PRECEDENCE LEFT {: update_precedence(assoc.left); :} terminal_list SEMI | PRECEDENCE RIGHT {: update_precedence(assoc.right); :} terminal_list SEMI | PRECEDENCE NONASSOC {: update_precedence(assoc.nonassoc); :} terminal_list SEMI if (emit.scan_code!=null) lexer.emit_error("Redundant scan code (skipping)"); else /* save the user code */ emit.scan_code = user_code; :}; /*................................ */ symbol_list ::= symbol_list symbol | symbol; /*................................ */ symbol ::= TERMINAL type_id declares_term TERMINAL declares_term | non_terminal type_id declares_non_term | non_terminal declares_non_term | /* error recovery productions -- sync on semicolon */ TERMINAL error {: /* reset the accumulated multipart name */ multipart_name = new String(); :} SEMI if (emit.parser_code!=null) lexer.emit_error("Redundant parser code (skipping)"); else /* save the user included code string */ emit.parser_code = user_code; if (emit.init_code!=nul) lexer.emit_error("Redundant init code (skipping)"); else /* save the user code */ emit.init_code = user_code; if (emit.scan_code!=null) lexer.emit_error("Redundant scan code (skipping)"); else /* save the user code */ emit.scan_code = user_code; /* reset the accumulated multipart name */ multipart_name = new String(); /* reset the accumulated multipart name */ multipart_name = new String(); /* reset the accumulated multipart name */ multipart_name = new String(); /* reset the accumulated multipart name */ multipart_name = new String(); update_precedence(assoc.left); update_precedence(assoc.right); :} {: update_precedence(assoc.nonassoc); /*................................ */ parser_code_part ::= PARSER CODE CODE_STRING:user_code opt_semi {: :} ; /*................................ */ init_code ::= INIT WITH CODE_STRING:user_code opt_semi {: :} ; /*................................ */ scan_code ::= SCAN WITH CODE_STRING:user_code opt_semi {: :} ; /*................................ */ symbol_list ::= symbol_list symbol | symbol; /*................................ */ symbol ::= TERMINAL type_id declares_term | TERMINAL declares_term | non_terminal type_id declares_non_term | non_terminal declares_non_term | /* error recovery productions -- sync on semicolon */ TERMINAL error {: :} SEMI | non_terminal error {: :} SEMI ; /*................................ */ declares_term ::= term_name_list {: :} SEMI ; /*................................ */ declares_non_term ::= non_term_name_list {: :} SEMI ; /*................................ */ term_name_list ::= term_name_list COMMA new_term_id | new_term_id; /*................................ */ non_term_name_list :: = non_term_name_list COMMA new_non_term_id | new_non_term_id ; /*................................ */ precedence_list ::= precedence_l | empty; /*................................ */ precedence_l ::= precedence_l preced | preced; /*................................ */ preced ::= PRECEDENCE LEFT {: :} terminal_list SEMI | PRECEDENCE RIGHT {: terminal_list SEMI | PRECEDENCE NONASSOC :} terminal_list SEMI ; parser.cup Interception Action.aj

10 10 Benefits ( I ) Aspect-orientation can isolate crosscutting semantic behavior in an explicit way. –Each semantic aspect can be freely attached to (generated) AST nodes without “polluting” the parser or AST node structure. –Different aspects can be selectively plugged in for different purposes at compile time. –Since each aspect is separated with other aspects, developers can always come back to the previous phase while developing a later phase.

11 11 Benefits ( II ) Inter-type declaration (Introduction) –Defined within class scope, direct access to AST node class members (include private members) –Preserve the object-oriented inheritance relationship. Join-point model (Interception) –Provides flexibility to insert semantic behaviors into AST nodes or parser –Avoid code duplication –Trace facility Introduction + Interception –Tree traversal –Phase combination

12 12 Inter-Type Declaration Example class UnparseVisitor extends Visitor{ protected PrintStream out = System.out; public Object print(Node node){ //... } // Other utility routines //... public Object visit(Node node){ return print(node); } public Object visit( ASTCompilationUnit node){ return print(node); } // same visit methods for another 83 nodes. //... } aspect Unparse{ protected static PrintStream out = System.out; public static void print(Node node){ //... } // other utility routines //... public void Node.unparse(){ Unparse.print(this); }

13 13 Join-Point Model Example pointcut scopeEvaluate(): target(ScopeNode+) && call (* *.evaluate()) ; before() : scopeEvaluate(){ } after() : scopeEvaluate(){ } symTabs.push(currentSymTab); SymbolTable tmp = currentSymTab; currentSymTab = new SymbolTable(); currentSymTab.parentScope = tmp; currentSymTab = (SymbolTable)symTabs.pop(); Executed each time entering a new scope Executed each time leaving a scope Occurred 46 times in a parser!

14 14 … // node is an instance of ScopeNode node.evaluate(); … before() : scopeEvaluate(){ symTabs.push(currentSymTab); SymbolTable tmp = currentSymTab; currentSymTab = new SymbolTable(); currentSymTab.parentScope = tmp; } after() : scopeEvaluate(){ currentSymTab = (SymbolTable)symTabs.pop(); } … // node is an instance of ScopeNode symTabs.push(currentSymTab); SymbolTable tmp = currentSymTab; currentSymTab = new SymbolTable(); currentSymTab.parentScope = tmp; node.evaluate(); currentSymTab = (SymbolTable)symTabs.pop(); … ASPECT WEAVING

15 15 Summary The major difficulty in compiler construction is separation of concerns. Traditional object-oriented design and the Visitor pattern cannot address the problem adequately. Using aspect-orientation to describe semantics supersedes both approaches without generating side- effects. Various AOP language features fulfill the computational needs of tree traversal. The proposed approach improves the overall modularization of the system and provides flexibility for future evolution of the compiler.

16 16 Thank you! http://www.cis.uab.edu/wuxi/ wuxi@cis.uab.edu


Download ppt "1 Xiaoqing Wu, Barrett R. Bryant, Jeff Gray and Suman Roychoudhury University of Alabama at Birmingham Separation of Concerns in Compiler Development Using."

Similar presentations


Ads by Google