/
Language Extensions for MATLAB Language Extensions for MATLAB

Language Extensions for MATLAB - PowerPoint Presentation

mitsue-stanley
mitsue-stanley . @mitsue-stanley
Follow
342 views
Uploaded On 2019-06-20

Language Extensions for MATLAB - PPT Presentation

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

Share:

Link:

Embed:

Download Presentation from below link

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.


Presentation Transcript

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

Slide2

Overview

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

Slide3

3

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

Slide4

MetaLexer

Modular Lexer GeneratorM.Sc. thesis, Andrew CaseyAOSD 2011www.sable.mcgill.ca/metalexer

7/1/2011

McLab, Laurie Hendren, Leverhulme Lecture #2

Intro - 4

Slide5

5

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

Slide6

6

Desired Modular MetaLexer Approach

Grammar rules for extension

Lexical rules for extension

7/1/2011

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide7

7

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

Slide8

Scanning AspectJ

8

7/1/2011

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide9

9

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

Slide10

10

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

Slide11

JFlex

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

Slide12

12

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide13

13

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

Slide14

14

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

Slide15

Example Structure of a

MetaLexer Specification for MATLAB

15

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide16

Extending a

MetaLexer Specification for Matlab

16

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide17

Sharing component specifications with

MetaLexer

17

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide18

Properties

Key

Value

Util_Patterns

Scanning a properties file

18

7/1/2011

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide19

util_properties.mlc helper component

19

7/1/2011

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide20

key.mlc

component

20

7/1/2011

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide21

value.mlc

component

21

7/1/2011

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide22

properties.mll

layout

22

7/1/2011

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide23

MetaLexer 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

Slide24

Key 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

Slide25

25

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

Slide26

Implementing inheritance (structured weaving).

26

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide27

7/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

Slide28

28

Implementing

MetaLexer component inheritance

7/1/2011

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide29

O

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

Slide30

Results: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

Slide31

31

AspectJ

and Extensions

7/1/2011

McLab, Laurie Hendren, Leverhulme Lecture #2

Slide32

MetaLexer 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

Slide33

Ad-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

Slide34

Metalexer 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

Slide35

AspectMatlab

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

Slide36

Why 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

Slide37

What 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.

Slide38

McLab, 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.

Slide39

Background - 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

Slide40

Aspect 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

Slide41

Function 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

Slide42

patterns

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

Slide43

Loop 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

Slide44

Scope 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

Slide45

Compound 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

Slide46

Before & 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

Slide47

Context 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

Slide48

Around 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

Slide49

McLab, 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

Slide50

Compiler 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

Slide51

Compiler 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

Slide52

Name 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

Slide53

Scientific 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

Slide54

Related 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

Slide55

Conclusions

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

Slide56

Typing 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

Slide57

57

Simple Example MATLAB function

Slide58

58

Slide59

59

Slide60

60

MATLAB prorammers often expect certain types

Slide61

61

Slide62

62

High-level types in MATLAB

Slide63

63

Slide64

Simple Example

64

Slide65

65

Slide66

Capturing reflective information

66

a.type

a.value

a.dims

a.basetype

Slide67

Capturing 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