Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Introduction Templight Debugger Visualizer Conclusion
Visualization of C++ Template Metaprograms1
Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Dept. Programming Languages and CompilersEötvös Loránd University, Budapest, Hungary
SCAM 2010
1TÁMOP-4.2.1/B-09/1/KMR-2010-0003Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Contents
1 Introduction
2 Templight
3 Debugger
4 Visualizer
5 Conclusion
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
C++ Templates
Different from Java / C# genericsJava / C#: type erasureC++: instantitation
Mainly used for libraries: STL, etc.Templates are skeletons, code generated on demandPossibility for specialisationRecursive templates are ok
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
C++ Template Metaprogram - example
template <int N>struct Factorial{enum { Value = N * Factorial<N-1>::Value};
};template <>struct Factorial<0>{enum { Value = 1 };
};
int main(){std::cout << Factorial<5>::Value;
}
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
C++ Template Metaprogram features
Executed at compilation-timeFunctional paradigmWhy we used them:
optimalizations of runtime programs, expression templatesstatic interface checking, concept checkingcompile-time code adoption, active librariesembedding DSLs
Turing complete
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Motivation
Metaprogramming is side effect of template constructTemplate syntax is not helpfulCompiler interprets metaprograms at compilation-timeNo user input, trivial printouts, etc.Maintenance is hopeless
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Motivation
Metaprogramming is side effect of template constructTemplate syntax is not helpfulCompiler interprets metaprograms at compilation-timeNo user input, trivial printouts, etc.Maintenance is hopeless
C++ template metaprogram code comprehension tools areessential
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Templight
Lightweight parser using boost wave and spiritInstruments template classes/functions injecting begin/endmarkersMarkers emit compilation warnings on instantiationCollects warnings generating a "stack-trace"Post-mortem wayTake advantage of compiler dependent implementationdetails (e.g. memoization)
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Debugger
Based on TemplightGUI is based on QTImplements "usual" debugger features:
Breakpoints, continueStep in/out/overLocals, watch
Backward execution
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Screenshot
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Visualizer
Based on TemplightTransform the instantiation chain into a directed graph:
nodes: types generated from templatesedges show the instantiation requests
Show corresponding codeFilter out irrevelant nodesExport to png, jpg etc,
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Unruh Example Demonstration
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Unruh Example Demonstration
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Unruh Example Demonstration
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Unruh Example Demonstration
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Unruh Example Demonstration
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Unruh Example Demonstration
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Unruh Example Demonstration
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Unruh Example Demonstration
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Unruh Example Demonstration
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Unruh Example Demonstration
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Unruh Example Demonstration
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Conclusion
It is hard to understand and maintain C++ templatemetaprogramsVisualization of programs is essentialWe have created a basic framework called TemplightWe have developed a graphical user interfacedpost-mortem debuggerWe have implemented a tool to visualize the C++ templatemetaprograms as graphs
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Controversial
template <int p, int i>struct is_prime {enum {prim = (p==2) ||
(p%i) &&is_prime<(i>2?p:0),i-1>::prim
};};template<>struct is_prime<0,0> {enum {prim=1};
};template<>struct is_prime<0,1> {enum {prim=1};
};Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Controversial
C++ source is the assembly of template metaprogram.
We have to use high level functional programming languages,like Haskell, to write metaprograms, and generate C++ source.
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb
Introduction Templight Debugger Visualizer Conclusion
Controversial
C++ source is the assembly of template metaprogram.
We have to use high level functional programming languages,like Haskell, to write metaprograms, and generate C++ source.
Thank you for attention
Visualization of C++ Template Metaprograms Zoltán Borók-Nagy, Viktor Májer, József Mihalicza,Norbert Pataki, Zoltán Porkoláb