Laurie Hendren McGill University Leverhulme Visiting Professor Department of Computer Science University of Oxford TexPoint fonts used in EMF Read the TexPoint manual before you delete this box ID: 759242
Download Presentation The PPT/PDF document "Language Extensions for MATLAB" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Language Extensionsfor MATLAB
Laurie Hendren McGill UniversityLeverhulme Visiting Professor Department of Computer ScienceUniversity of Oxford
TexPoint fonts used in EMF.
Read the TexPoint manual before you delete this box.: AAAAAAAAAAAAAAAAAAAAAA
Amina AslamToheed AslamAndrew CaseyMaxime Chevalier- BoisvertJesse DohertyAnton DubrauRahul GargMaja FrydrychowiczNurudeen LameedJun LiSoroush RadpourOlivier Savary
7/1/2011
Intro - 1
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide2Overview
How does one make language extensions for MATLAB using McLab?MetaLexerAspects for MATLAB["Types" for MATLAB]
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Intro - 2
Slide33
McLab Extensible Front-end
Scanner(MetaLexer)
Parser(Beaver)
AST attributes, rewrites(JastAdd)
Attributed AST
XML
Other
AspectMatlab
AspectMatlab
AspectMatlab
.m source
.m source
.m source
MATLAB-to-
Natlab
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide4MetaLexer
Modular Lexer GeneratorM.Sc. thesis, Andrew CaseyAOSD 2011www.sable.mcgill.ca/metalexer
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Intro - 4
Slide55
Given a front-end specification for a language (i.e. MATLAB), current method to implement a front-end for an extension of that language (i.e.
AspectMatlab)?
Grammar rules for extension
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide66
Desired Modular MetaLexer Approach
Grammar rules for extension
Lexical rules for extension
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide77
We also want to be able to combine lexical specifications for diverse languages.
Java + HTML
Java + Aspects (AspectJ) Java + SQL MATLAB + Aspects (AspectMatlab)
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide8Scanning AspectJ
8
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide99
Would like to be able to reuse and extend lexical specification modules
Nested C-style comments Javadoc comments Floating-point constants URL regular expressions …
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide1010
First, let’s understand the traditional lexer tools (lex, flex, jflex).
programmer specifies regular expressions + actions tools generate a finite automaton-based implementation states are used to handle different language contexts
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide11JFlex
Lexing Structure
11
Lexing
rules associated with a state.Changing states associated with action code.
Specification
in one file.
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide1212
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide1313
Current (ugly) method for extending jflex specifications - copy&modify
Principled way of weaving new rules into existing rules.Modular and abstract notion of state and changing between states.
Copy jflex specification.Insert new scanner rules into copy.Order of rules matters! Introduce new states and action logic for converting between states.
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide1414
MetaLexer
Structure
Components define
lexing
rules associated with a state, rules produce meta-tokens.Layout defines transitions between components, state changes by meta-lexer (regular expressions + matching pairs of start/end symbols).
Each component specified in its own file.
Layout specified in its own file.
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide15Example Structure of a
MetaLexer Specification for MATLAB
15
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide16Extending a
MetaLexer Specification for Matlab
16
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide17Sharing component specifications with
MetaLexer
17
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide18Properties
Key
Value
Util_Patterns
Scanning a properties file
18
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide19util_properties.mlc helper component
19
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide20key.mlc
component
20
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide21value.mlc
component
21
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide22properties.mll
layout
22
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide23MetaLexer is implemented and available:www.sable.mcgill.ca/metalexer
23
properties.mll
key.mlc
value.mlc
util_patterns.mlc
MetaLexer
properties.jflex
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide24Key problems to solve:
How to implement the meta-token lexer?How to allow for insertion of new components, replacing of components, adding new embeddings (metalexer transitions).How to insert new patterns into components at specific points.
24
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide2525
Implementing the meta-token
lexer
Recognize the matching suffix.
Recognize a meta-pattern, i.e. when to go to a new component and when to return.
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide26Implementing inheritance (structured weaving).
26
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide277/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
27
Implementing MetaLexer layout inheritance
Layouts can inherit other layouts
%inherit
directive put at the location at which the inherited transition rules (embeddings) should be placed.
each
%inherit
directive can be followed by:
%
unoption
%replace
%
unembed
new embeddings
Slide2828
Implementing
MetaLexer component inheritance
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide29O
Weaving in an inherited component
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
29
Original Component
New Component adds some rules and inherits original component.
Woven output
Slide30Results:Applied to three projects with complex scanners: AspectJ (abc and extensions) Matlab (Annotations and AspectMatlab extensions) MetaLexer
30
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide3131
AspectJ
and Extensions
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide32MetaLexer scanner implemented in MetaLexer
1st version of MetaLexer written in JFlex, one for components and one for layouts.2nd version implemented in MetaLexer, many shared components between the component lexer and the layout lexer.
32
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide33Ad-hoc systems with separate scanner/ LALR parserPolyglotJastAddabcRecursive-descent scanner/parserANTLR and systems using ANTLRScannerless systemsRats! (PEGs)Integrated systemsCopper (modified LALR parser which communicates with DFA-based scanner)
Related Work for MetaLexer
33
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide34Metalexer Conclusions
MetaLexer allows one to specify modular and extensible scanners suitable for any system that works with JFlex.Two main ideas: meta-lexing and component/layout inheritance.Used in large projects such as abc, McLab and MetaLexer itself.Available at: www.sable.mcgill.ca/metalexer
34
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Slide35AspectMatlab
Simple Aspect-Oriented extension to MATLABM.Sc. thesis, Toheed AslamAnalysis by Jesse Doherty, applications by Anton Dubrau, extensions by Olivier Savary-BelangerAOSD 2010www.sable.mcgill.ca/mclab
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Intro - 35
Slide36Why AspectMatlab?
AspectJAspectC++AspectCobol…
McLab, Laurie Hendren, Leverhulme Lecture #2
Test the
McLab framework for extensibility Bring a simple and relevant version of AOP to scientists.
7/1/2011
36
simple language constructs
focus on arrays and loops
Slide37What is an Aspect?
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
37
Event
Observer
Pattern specifying events to match.
Action to do before, after or around the matched events.
Action can use context information from the matched event.
Slide38McLab, Laurie Hendren, Leverhulme Lecture #2
Example: Profiling Array Sparsity
0 0 0 9 0 0 0 0 0 00 0 0 0 0 0 5 0 0 00 1 0 0 0 0 0 3 0 00 0 0 0 4 0 0 0 0 00 0 7 0 0 0 0 0 0 0
7/1/2011
38
Capture the
sparsity
and size at each operation on the whole array.
Capture the number of indexed references to each array.
Print out a summary for each array, allowing the programmer to identify good candidates to implement as sparse arrays.
Slide39Background - MATLAB Class
classdef myClass properties … end methods … endend
McLab, Laurie Hendren, Leverhulme Lecture #2
datacount = 0;
helper functionsfunction x=getCount(this) x = this.count; end
7/1/2011
39
Slide40Aspect Definition
aspect myAspect properties … end methods … end patterns … end actions … endend
McLab, Laurie Hendren, Leverhulme Lecture #2
datacount = 0;
helper functionsfunction x=getCount(this) x = this.count; end
pointcuts foocalls : call(foo);
advice foocounter : before foocalls this.count = this.count + 1; end
7/1/2011
40
Slide41Function and Operator
Patterns
McLab, Laurie Hendren, Leverhulme Lecture #2
patterns pCallFoo : call(foo); pExecBar : execution(bar); pCallFoo2args : call(foo(*,*)); pExecutionMain : mainexecution();end
7/1/2011
41
patterns
plusOp
:
op(+);
timesOp
:
op(.*) || op(*);
matrixOps
: op(matrix);
allButMinus
: op(all) & ~op(-);
end
Slide42patterns
pSetX : set(a); pGetX : get(b); arraySet : set(*); arrayWholeGet : get(*()); arrayIndexedGet : get(*(..));end
Array Patterns
McLab, Laurie Hendren, Leverhulme Lecture #2
Context Info
name
indices
object (value)
line numberlocationfile name
also,new value
a(i) = b(j,k)
7/1/2011
42
Slide43Loop Patterns
McLab, Laurie Hendren, Leverhulme Lecture #2
patterns
pLoopI : loop(i); pLoopHeadI : loophead(i); pLoopBodyI : loopbody(i);end
for
end
i = 1:2:n
…
…
…
t1 = [1,3,5,7,9,…,n];
i = t1(t2);
t2 = 1:numel(t1)
7/1/2011
43
Slide44Scope Patterns
McLab, Laurie Hendren, Leverhulme Lecture #2
patterns
pWithinFoo : within(function, foo); pWithinBar : within(script, bar); pWithinMyClass : within(class, myClass); pWithinLoops : within(loops, *); pWithinAllAbc : within(*, abc);end
7/1/2011
44
Slide45Compound Patterns
Logical combinations of primitive patterns
McLab, Laurie Hendren, Leverhulme Lecture #2
patterns
pCallFoo : call(foo) & within(loops, *); pGetOrSet : (get(*) | set(*)) & within(function, bar);end
7/1/2011
45
Slide46Before & After Actions
McLab, Laurie Hendren, Leverhulme Lecture #2
actions
aCountCall : before pCall this.count = this.count + 1; disp(‘calling a function’); end aExecution : after executionMain total = this.getCount(); disp([‘total calls: ’, num2str(total)]); endend
7/1/2011
46
Slide47Context Exposure
McLab, Laurie Hendren, Leverhulme Lecture #2
actions
aCountCall : before pCall : (name, args) this.count = this.count + 1; disp([‘calling ’,name,‘ with args(’,args, ‘)’]); end aExecution : after executionMain : (file) total = this.getCount(); disp([‘total calls in ’,file,‘: ’,num2str(total)]); endend
7/1/2011
47
Slide48Around Actions
McLab, Laurie Hendren, Leverhulme Lecture #2
actions
actcall : around pCallFoo : (args) % proceed not called, so varargout is set varargout{1} = bar(args{1}, args{2}); endend
actions
actcall :
around
pCallFoo : (args)
disp([‘before foo call with args(’, args , ‘)’]); proceed(); disp([‘after foo call with args(’, args , ‘)’]); endend
7/1/2011
48
Slide49McLab, Laurie Hendren, Leverhulme Lecture #2
Actions Weaving Order
actions before1 : before pCallFoo … around1 : around pCallFoo … after1 : after pCallFoo … before2 : before pCallFoo … around2 : around pCallFoo … after2 : after pCallFoo …end
foo();
before1();
around1();
after1();
before2();
around2();
after2();
7/1/2011
49
Slide50Compiler Structure
McLab, Laurie Hendren, Leverhulme Lecture #2
.m
Front-end
Separator
AST(Matlab+Aspects)
AspectInfo
Matlab AST
Transformations
ResolvedName Set
Simplified AST
Name ResolutionAnalysis
.m
Base
Matlab
Aspects
Matcher & Weaver
Woven AST
Post-processing
Woven
Base Matlab
Matlab Impl.
of Aspects
AspectMatlab Compiler
7/1/2011
50
Slide51Compiler Structure
McLab, Laurie Hendren, Leverhulme Lecture #2
.m
Front-end
Separator
AST(Matlab+Aspects)
AspectInfo
Matlab AST
Transformations
ResolvedName Set
Simplified AST
Name ResolutionAnalysis
.m
Base
Matlab
Aspects
Matcher & Weaver
Woven AST
Post-processing
Woven
Base Matlab
Matlab Impl.
of Aspects
7/1/2011
51
Slide52Name Resolution Analysis
patterns pCallFoo : call(foo); pGetFoo : get(foo);endactions before1 : before pCallFoo … before2 : before pGetFoo …end
McLab, Laurie Hendren, Leverhulme Lecture #2
foo();function
before1();
before2();
foo();variable
foo();unresolved
before1();
before2();
if isFun(foo)
if isVar(foo)
7/1/2011
52
Slide53Scientific Use Cases
Domain-Specific Profiling of ProgramsTracking array sparsityTracking array size-growing operationsCounting floating-point operationsExtending FunctionalityInterpreting loop iteration spaceAdding units to computations
McLab, Laurie Hendren, Leverhulme Lecture #2
7/1/2011
53
Slide54Related Work for AspectMatlab
AspectJ (Kiczales et al., ECOOP '01)abc (The de Moor and Hendren gang, AOSD '05) Array pointcuts (Chen et al., JSES '07)Loop pointcuts (Harbulot et al., AOSD '06)AspectCobol (Lammel et al., AOSD '05)Domain-Specific Aspects in Matlab (Cardoso et al., DSAL workshop held at AOSD '10)
McLab, Laurie Hendren, Leverhulme Lecture #2
7/1/2011
54
Slide55Conclusions
McLab supports extensions to MATLABWe developed MetaLexer to support modular and extensible lexers, and then used it in McLab.We designed and implemented AspectMatlab as an exercise in using McLab for extensions, and also to provide simple and relevant AOP for scientists.
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
55
Slide56Typing Aspects
Types for MATLAB, somewhat in the spirit of aspects.Designed by what programmers might want to say.Checked at run-time, but some static analysis could be done.
7/1/2011
McLab, Laurie Hendren, Leverhulme Lecture #2
Intro - 56
Slide5757
Simple Example MATLAB function
Slide5858
Slide5959
Slide6060
MATLAB prorammers often expect certain types
Slide6161
Slide6262
High-level types in MATLAB
Slide6363
Slide64Simple Example
64
Slide6565
Slide66Capturing reflective information
66
a.type
a.value
a.dims
a.basetype
Slide67Capturing dimensions and basetype
67
<n> can be used as a dimension spec
value of n is instantiated from the runtime dimension
repeated use in same
atype
statement implies equality