52
1 Generators Jun Chen March 2003 University of Colorado at Boulder

SUBJECT

Embed Size (px)

DESCRIPTION

PDF

Citation preview

Page 1: SUBJECT

1

Generators

Jun ChenMarch 2003

University of Colorado at Boulder

Page 2: SUBJECT

2

Agenda

Part I: Generator Technology

Part II: GenVoca

Part III: GenVoca vs. AOP

Page 3: SUBJECT

3

Part I: Generator Technology

What are Generators?Technologies for Building GeneratorsVertical, Horizontal, Oblique TransformationsCompositional vs. Transformational GeneratorsKinds of Transformations• Compiler Transformations• Source-to-Source Transformations

Transformation Systems

Page 4: SUBJECT

4

System Specification

System Implementation

Generator

What are Generators?

Generator: A program that takes a higher-level specification of a piece of software and produces its implementation.

• Check Specification

• Complete Specification

• Perform Optimizations

• Generate Implementation

Page 5: SUBJECT

5

Three Issues

Raising the intentionality of system descriptions• Focus on “what is needed”• Avoid implementation details

Page 6: SUBJECT

6

Three Issues

Raising the intentionality of system descriptionsComputing an efficient implementation• Performance Requirements (e.g. response time) • Complex computation inside generators

Page 7: SUBJECT

7

Three Issues

Raising the intentionality of system descriptionsComputing an efficient implementationAvoiding the library scaling problem• Exponential growth • Factoring libraries into components corresponding to

features + Composing using function calls • Avoid “exponential growth”• Poor performance

• Generators: factoring + eliminating calling overhead

Page 8: SUBJECT

8

Three Ways to Build Generators

Developing generators as stand-alone programs from scratch• Effort-intensive

Using the built-in metaprogramming capabilities of a programming lang. (e.g. c++)

•Advantage: easier to write generators, generators could be part of the library•Disadvantage: limited by the host lang., debugging

Page 9: SUBJECT

9

Three Ways to Build Generators

Using a generator infrastructure• The infrastructure provides basic facilities

• A common format for the internal source representation• Operations for encoding transformations• Input/output facilities• Debugging facilities

• An example: Intentional Programming

Page 10: SUBJECT

10

Vertical, Horizontal, Oblique Transformations

Horizontal Transformation

Oblique Transformation

Vertical Transformation

(Forward Refinement)

Page 11: SUBJECT

11

Compositional Generator: vertical transformation

Transformational Generators: vertical transformation + horizontal transformation

Compositional vs. Transformational Generators

Page 12: SUBJECT

12

Kinds of Transformations

Page 13: SUBJECT

System Implementation

System source in a general-purpose lang.

Compile

System Requirements

Manually Implement

Interactive, automated support

System Implementation

Could generate code in C++ or Java if needed

Compile

System source using domain-specific lang.

abstractions

Compile

High-level system specification

System Requirements

Manually Implement

System Implementation

Could generate code in C++ or Java if needed

Compile

System source using domain-specific lang.

abstractions

Compile

System Requirements

Manually Implement

Automating System Implementation

Compiler Transformations

Source-to-Source Transformations

Page 14: SUBJECT

14

Compiler Transformations

Refinements(vertical) – adds implementation details• Decomposition

• An abstract data type(ADT) -> A number of other ADTs

• Choice of representation• Matrix -> Array? Vector? Hash Table?

• Choice of algorithm• Performance

Page 15: SUBJECT

15

Compiler Transformations

Optimizations(horizontal) – improve performance• Structural changes of the code -> code becomes

hard to understand• Inlining – replace the symbol by its definition• Loop fusion – combine two loops into one if two loops

have a similar structure and can be done in parallel

• Beyond conventional compilers• Domain-specific optimizations (domain knowledge)• Global optimizations (cross multiple locations)

Page 16: SUBJECT

16

Source-to-Source Transformations

Restructuring transformations• Editing transformations

• Mechanize some simple editing operations • Ex. converting a code section into a procedure

• Refactoring transformations• Recognize code• Ex. abstraction and generalization

Page 17: SUBJECT

17

Transformation Systems

Definition • Environments for building transformational

generators• Transformational generators

Page 18: SUBJECT

18

Elements of transformation systems

A common format for the internal program representation• Abstract syntax tree, data and control flow graph

Code analysis facilities• Check the input program and guide transformation

A transformation engine• Apply transformations

Input and output facilities for the internal representation

Page 19: SUBJECT

19

An Example

Parser Transformation Engine

Unparser

Program text e.g., (y+1/y+1)+z AST

Transformed AST 1+z

z

/

x x1

Rewrite Rule

+

y 1 y 1

+

/+ +

1 z

Page 20: SUBJECT

20

Summary: Generator Technology

What are Generators?Technologies for Building GeneratorsVertical, Horizontal, Oblique TransformationsCompositional vs. Transformational GeneratorsKinds of Transformations• Compiler Transformations• Source-to-Source Transformations

Transformation Systems

Page 21: SUBJECT

21

Part II: GenVoca

GenVoca ApproachGenVoca ModelImplementing GenVoca in C++Composition Validation

Page 22: SUBJECT

22

GenVoca Approach

Build software system generators based on composing object-oriented layers of abstraction.• Stacked layers• Layer above refines the layer below it. Ex.

add new classes

OO framework v.s. GenVoca model

Page 23: SUBJECT

23

Transforming a framework into a GenVoca model (step 1)

Identify layers of abstractions in a framework

C31 C32 C33

C21 C22

C11 C13

Most Refined

Page 24: SUBJECT

24

Transforming a framework into a GenVoca model (step 2)

Turn the hierarchy of layers upside down

C31 C32 C33

C21 C22

C11 C13

Most Refined

Page 25: SUBJECT

25

Transforming a framework into a GenVoca model (step 3)

Treat “the layer below” as a parameter of “the layer” above

C31 C32 C33

C21 C22

C11 C13

Page 26: SUBJECT

26

Transforming a framework into a GenVoca model (step 4)

Provide families of alternative, parameterized layers

Page 27: SUBJECT

27

GenVoca Model

Components and Realms• Component (or layer): an implementation of

abstract data type or feature• Realm: all components that implement the

same abstract data type or featureExample:

S = { a, b, c }T = { d[S], e[S], f[S] }W = { n[W], m[W], p, q[T,S] }

Page 28: SUBJECT

28

Parameters & Transformations• A component has a realm parameter for every

realm that it imports.Example:

S = { a, b, c }T = { d[S], e[S], f[S] }W = { n[W], m[W], p, q[T,S] }

Vertical v.s. Horizontal Parameters• Vertical parameters: layer parameters• Horizontal parameters: other parameters

GenVoca Model

Page 29: SUBJECT

29

GenVoca Model

Symmetric Components• Exports the same interface as it imports

Example:S = { a, b, c }T = { d[S], e[S], f[S] }W = { n[W], m[W], p, q[T,S] }

n[W], m[W]

Page 30: SUBJECT

30

GenVoca Model

GrammarExample:

S = { a, b, c }T = { d[S], e[S], f[S] }W = { n[W], m[W], p, q[T,S] }

S := a | b | cT := d[S] | e[S] | f[S]W:= n[W] | m[W] | p | q[T,S]

Page 31: SUBJECT

31

GenVoca Model

Type Expressions• describing layer composition, used to model

software systemExample:

S := a | b | cT := d[S] | e[S] | f[S]W:= n[W] | m[W] | p | q[T,S]

System_1 = d [b]System_2 = q [d[a], c]

Page 32: SUBJECT

32

GenVoca Model

Families of Systems• The set of all component compositions

Scalability• Few components, large families of systems

Design Rules and Domain ModelsS = { a, b, c }T = { d[S], e[S], f[S] }d[a], d[b], d[c], e[a], e[b], e[c], f[a], f[b], f[c]

• Type expression, syntax correct, semantics wrong• Domain-specific constrains -> Design rules• Domain model: Realms of components + Design rules

Page 33: SUBJECT

33

An Example – Booch Data Structure

Data Structure Families• Bag: unordered collection of objects• Queue: ordered sequence of objects with FIFO semantics

Data Structure Features for Every Family• Unbounded: No upper bound on total number of objects• Concurrent: A multithread environment, read/write serialized• Managed: Free objects are stored on a list for subsequent

reuse.

Data Structure Features only for Queue• Priority: Objects are sorted based on some priority function

Page 34: SUBJECT

34

An Example – Booch Data Structure

Bag[Concurrent[Size_of[Unbounded[Managed[heap]]]]]Bag

Concurrent

Size_of

Unbounded

Managed

heap Used as a parameter of

Formal parameter

Multiple Parameters

Page 35: SUBJECT

35

Implementing GenVoca Layers in C++

Implement GenVoca layers as class templates containing member classes

Example: LayerA

Page 36: SUBJECT

Template <class LowerLayer>

Class LayerA

{ public:

class ClassA

{ public:

void operationA() //refine operationA()

{ …//LayerA-specific work

lower.operationA();

…//LayerA-specific work

};

void operationB() { lower.operationB(); }; //forward operationB

private:

typedef typename LowerLayer::ClassA LowerLayerClassA;

LowerLayerClassA lower;

};

class ClassB {…};

}

Forwarding Implementation

Page 37: SUBJECT

37

Forwarding Static WrapperTemplate <class Component>

Class Wrapper

{ public:

void operationA() //refine operationA()

{ …//wrapper-specific work

component.operationA();

…//wrapper-specific work

};

void operationB() { component.operationB(); }; //forward operationB

private:

Component component;

}

Problem:

propagation of operations

Solution: Inheritance

Page 38: SUBJECT

38

Inheritance-based Static WrapperTemplate <class Component>

Class Wrapper : public Component

{ public:

void operationA() //refine operationA()

{ …//wrapper-specific work

Component :: operationA();

…//wrapper-specific work

};

}

Page 39: SUBJECT

Template <class LowerLayer>

Class LayerA

{ private:

typedef typename LowerLayer::ClassA LowerLayerClassA;

typedef typename LowerLayer::ClassB LowerLayerClassB;

public:

class ClassA : Public LowerLayerA

{ public:

void operationA() //refine operationA()

{ …//LayerA-specific work

LowerLayerClassA :: operationA();

…//LayerA-specific work

};

};

class ClassB : Public LowerLayerClassB

{…};

}

Inheritance-based Implementation

Page 40: SUBJECT

40

Upward Type Propagation

Scope operator, ::typedef Problem: the layer might have to explicitly pass types they are not interested for themselves.Solution: configuration repository• an “envelop” containing all the layers, types and

constants

Page 41: SUBJECT

41

An Example

R1: A

R2: B,C

R3: D,E

Accessing R3 Possible

Accessing R2 and R3 Possible R1: A

R2: B,C

Config

export:

Global1, Global2

ConfigA::HorizA

ConfigB::HorizB

Page 42: SUBJECT

42

Downward Type Propagation

Idea: put the whole layer hierarchy into the configuration repository.

R1: A

R2: B

Config

export:

R1, R2

Access R1 possible

Page 43: SUBJECT

43

Composition Validation

Problem: syntactically correct composition might be wrong in semantics.Example:

DS_Size: guarded[DS_Size] | concurrent[DS_Size]multiple[DS_Size] | size_of[DS] | …

DS : bounded | unbounded[Memory] | …

guarded[guarded[multiple[concurrent[guarded[concurrent[size_of[bounded]]]]]]]

Solution: Configuration Rules

Page 44: SUBJECT

44

A model – Conditions & Restrictions

Upward and downward propagation of attributes that represent constraints on layers or properties of layersConditions: constraints propagated downwardsRestrictions: constraints propagated upwards

Layer A

Postrestrictions

Prerestrictions

Preconditions

Postconditions

Page 45: SUBJECT

45

Summary : GenVoca

GenVoca ApproachGenVoca ModelImplementing GenVoca in C++Composition Validation

Page 46: SUBJECT

46

Part III: GenVoca vs. AOPRichard Cardone

AOP: a meta-programming model that promotes code reuse by localizing the implementation of design features that cut across multiple functional unit.

GenVoca: a model of hierarchical software construction that enables customized applications to be assembled from interchangeable and reusable components.

Page 47: SUBJECT

47

Difference & Similarity

Difference• Starting point• Focus• Style

Similarity• Basic concepts• Effort required for implementation

Page 48: SUBJECT

48

Fundamental Concepts

AOP• Components, Aspects, Joint Points

GenVoca• Components, Realms, Parameters

Page 49: SUBJECT

49

Implementation

AOP• Aspect language• Weaver (preprocessor)

GenVoca• Type expression• Generator (preprocessor)

Page 50: SUBJECT

50

Focus

AOP• For any existing code base• Support code reuse by applying new features in a

controlled and localized way

Genvoca• Require the definition of standardized realm

interfaces, domain analysis needed• Provide techniques to decompose applications into

reusable and composable components

Page 51: SUBJECT

51

Conclusion

Generators• Automate system implementation• Various levels: compiler, source-to-source• Depending on the needs• Transformation systems

GenVoca• A systematic approach• Issues:

• composition validation (distance)• how to get effective component composition• component changes

Page 52: SUBJECT

52

Sources

“Generative Programming” textbookDon Batory, http://www.cs.utexas.edu/users/dsb/• “Composition Validation and Subjectivity in GenVoca

Generators”, 1997

“On the Relationship of Aspect-Oriented Programming and GenVoca”, Richard Cardone