3. Part I: Generator Technology
What are Generators?
Technologies for Building Generators
Vertical, Horizontal, Oblique Transformations
Compositional vs. Transformational Generators
Kinds of Transformations
• Compiler Transformations
• Source-to-Source Transformations
Transformation Systems
3
4. What are Generators?
Generator: A program that takes a higher-
level specification of a piece of software and
produces its implementation.
• Check Specification
System System
• Complete Specification
Specification Implementation
• Perform Optimizations
• Generate Implementation
Generator
4
5. Three Issues
Raising the intentionality of system
descriptions
• Focus on “what is needed”
• Avoid implementation details
5
6. Three Issues
Raising the intentionality of system
descriptions
Computing an efficient implementation
• Performance Requirements (e.g. response time)
• Complex computation inside generators
6
7. Three Issues
Raising the intentionality of system descriptions
Computing an efficient implementation
Avoiding 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
7
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
8
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
9
13. Automating System Implementation
System
System System
Requirements
Requirements Requirements
Manually
Manually Manually
Implement
Implement Implement
High-level system
specification
Source-to-Source Interactive,
Transformations automated support
Compiler System source using System source using
Transformations domain-specific lang. domain-specific lang.
abstractions abstractions
Compile Compile
System source Could generate Could generate
in a general- code in C++ or code in C++ or
purpose lang. Java if needed Java if needed
Compile Compile Compile
System
System System
Implementation
Implementation Implementation
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
14
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)
15
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
16
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
18
19. An Example
Program text Transformed
AST
e.g., (y+1/y+1)+z 1+z
AST
Transformation
Parser Unparser
Engine
+ +
/ z z
1
+ +
/
y1y 1
1
x x
Rewrite Rule
19
20. Summary: Generator Technology
What are Generators?
Technologies for Building Generators
Vertical, Horizontal, Oblique Transformations
Compositional vs. Transformational Generators
Kinds of Transformations
• Compiler Transformations
• Source-to-Source Transformations
Transformation Systems
20
21. Part II: GenVoca
GenVoca Approach
GenVoca Model
Implementing GenVoca in C++
Composition Validation
21
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
22
23. Transforming a framework into
a GenVoca model (step 1)
Identify layers of abstractions in a framework
C11 C13
C21 C22
Most
C31 C32 C33
Refined
23
24. Transforming a framework into
a GenVoca model (step 2)
Turn the hierarchy of layers upside down
Most
C31 C32 C33
Refined
C21 C22
C11 C13
24
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
25
26. Transforming a framework into
a GenVoca model (step 4)
Provide families of alternative, parameterized
layers
26
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 feature
Example:
S = { a, b, c }
T = { d[S], e[S], f[S] }
W = { n[W], m[W], p, q[T,S] }
27
28. GenVoca Model
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
28
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]
29
30. GenVoca Model
Grammar
Example:
S = { a, b, c }
T = { d[S], e[S], f[S] }
W = { n[W], m[W], p, q[T,S] }
S := a | b | c
T := d[S] | e[S] | f[S]
W:= n[W] | m[W] | p | q[T,S]
30
31. GenVoca Model
Type Expressions
• describing layer composition, used to model
software system
Example:
S := a | b | c
T := 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]
31
32. GenVoca Model
Families of Systems
• The set of all component compositions
Scalability
• Few components, large families of systems
Design Rules and Domain Models
S = { 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
32
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
33
34. An Example – Booch Data Structure
Bag[Concurrent[Size_of[Unbounded[Managed[heap]]]]]
Formal
Bag
parameter
Concurrent Multiple Parameters
Size_of
Unbounded
Managed
Used as a
heap
parameter of
34
35. Implementing GenVoca Layers
in C++
Implement GenVoca layers as class templates
containing member classes
Example: LayerA
35
36. Template <class LowerLayer> Forwarding Implementation
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 {…};
}
37. Forwarding Static Wrapper
Template <class Component>
Class Wrapper
{ public:
void operationA() //refine operationA() Problem:
{ …//wrapper-specific work
propagation of operations
component.operationA();
Solution: Inheritance
…//wrapper-specific work
};
void operationB() { component.operationB(); }; //forward operationB
private:
Component component;
}
37
38. Inheritance-based Static Wrapper
Template <class Component>
Class Wrapper : public Component
{ public:
void operationA() //refine operationA()
{ …//wrapper-specific work
Component :: operationA();
…//wrapper-specific work
};
}
38
39. Template <class LowerLayer> Inheritance-based Implementation
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
{…};
}
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
40
41. An Example
Accessing R2
and R3 Possible R1: A
R1: A R2: B,C
Config
R2: B,C
export:
R3: D,E Global1, Global2
ConfigA::HorizA
Accessing R3 ConfigB::HorizB
Possible
41
42. Downward Type Propagation
Idea: put the whole layer hierarchy into the
configuration repository.
Access R1
R1: A
possible
R2: B
Config
export:
R1, R2
42
44. A model – Conditions & Restrictions
Upward and downward propagation of
attributes that represent constraints on layers or
properties of layers
Conditions: constraints propagated downwards
Restrictions: constraints propagated upwards
Postrestrictions Preconditions
Layer A
Prerestrictions Postconditions
44
45. Summary : GenVoca
GenVoca Approach
GenVoca Model
Implementing GenVoca in C++
Composition Validation
45
46. Part III: GenVoca vs. AOP
Richard 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.
46
49. Implementation
AOP
• Aspect language
• Weaver (preprocessor)
GenVoca
• Type expression
• Generator (preprocessor)
49
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
50
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
51
52. Sources
“Generative Programming” textbook
Don 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
52