ROSE Tutorial: a Tool for Building Source-To-Source Translators Draft Tutorial (Version 0.9.11.115)
Total Page:16
File Type:pdf, Size:1020Kb
ROSE Tutorial: A Tool for Building Source-to-Source Translators Draft Tutorial (version 0.9.11.115) Daniel Quinlan, Markus Schordan, Richard Vuduc, Qing Yi Thomas Panas, Chunhua Liao, and Jeremiah J. Willcock Lawrence Livermore National Laboratory Livermore, CA 94550 925-423-2668 (office) 925-422-6278 (fax) fdquinlan,panas2,[email protected] [email protected] [email protected] [email protected] [email protected] Project Web Page: www.rosecompiler.org UCRL Number for ROSE User Manual: UCRL-SM-210137-DRAFT UCRL Number for ROSE Tutorial: UCRL-SM-210032-DRAFT UCRL Number for ROSE Source Code: UCRL-CODE-155962 ROSE User Manual (pdf) ROSE Tutorial (pdf) ROSE HTML Reference (html only) September 12, 2019 ii September 12, 2019 Contents 1 Introduction 1 1.1 What is ROSE.....................................1 1.2 Why you should be interested in ROSE.......................2 1.3 Problems that ROSE can address...........................2 1.4 Examples in this ROSE Tutorial...........................3 1.5 ROSE Documentation and Where To Find It.................... 10 1.6 Using the Tutorial................................... 11 1.7 Required Makefile for Tutorial Examples....................... 11 I Working with the ROSE AST 13 2 Identity Translator 15 3 Simple AST Graph Generator 19 4 AST Whole Graph Generator 23 5 Advanced AST Graph Generation 29 6 AST PDF Generator 31 7 Introduction to AST Traversals 35 7.1 Input For Example Traversals............................. 35 7.2 Traversals of the AST Structure............................ 36 7.2.1 Classic Object-Oriented Visitor Pattern for the AST............ 37 7.2.2 Simple Traversal (no attributes)....................... 37 7.2.3 Simple Pre- and Postorder Traversal..................... 37 7.2.4 Inherited Attributes.............................. 38 7.2.5 Synthesized Attributes............................. 44 7.2.6 Accumulator Attributes............................ 47 7.2.7 Inherited and Synthesized Attributes..................... 48 7.2.8 Persistent Attributes.............................. 51 7.2.9 Nested Traversals................................ 54 7.2.10 Combining all Attributes and Using Primitive Types............ 56 iii iv CONTENTS 7.2.11 Combined Traversals.............................. 57 7.2.12 Short-Circuiting Traversals.......................... 63 7.3 Memory Pool Traversals................................ 66 7.3.1 ROSE Memory Pool Visit Traversal..................... 66 7.3.2 Classic Object-Oriented Visitor Pattern for Memory Pool......... 68 7.3.3 ROSE IR Type Traversal (uses Memory Pools)............... 69 8 Graph Processing Tutorial 73 8.1 Traversal Tutorial................................... 73 9 Scopes of Declarations 77 9.1 Input For Examples Showing Scope Information.................. 77 9.2 Generating the code representing any IR node.................... 78 10 AST Query 81 10.1 Simple Queries on the AST.............................. 81 10.2 Nested Query...................................... 81 11 AST File I/O 87 11.1 Source Code for File I/O................................ 87 11.2 Input to Demonstrate File I/O............................ 87 11.3 Output from File I/O................................. 87 11.4 Final Code After Passing Through File I/O..................... 87 12 Debugging Techniques 91 12.1 Input For Examples Showing Debugging Techniques................ 91 12.2 Generating the code from any IR node........................ 92 12.3 Displaying the source code position of any IR node................. 92 II Complex Types 95 13 Type and Declaration Modifiers 97 13.1 Input For Example Showing use of Volatile type modifier............. 97 13.2 Generating the code representing the seeded bug.................. 98 14 Function Parameter Types 101 15 Resolving Overloaded Functions 105 16 Template Parameter Extraction 109 17 Template Support 113 17.1 Example Template Code #1.............................. 113 17.2 Example Template Code #2.............................. 113 CONTENTS v III Program Analyses 115 18 Generic Dataflow Analysis Framework 117 18.1 Basics of DataFlowAnalysis.............................. 117 18.2 ROSE Dataflow Framework.............................. 119 18.2.1 Call and Control-Flow Graphs........................ 119 18.2.2 Analyses..................................... 119 18.2.3 Dataflow.................................... 121 18.2.4 Transferring Information Between Analyses................. 130 18.2.5 CFG Transformations............................. 132 19 Recognizing Loops 135 20 Virtual CFG 139 20.1 CFGNode Index values................................. 139 20.2 Important functions.................................. 140 20.2.1 Node methods................................. 140 20.2.2 Edge methods.................................. 140 20.3 Drawing a graph of the CFG............................. 141 20.4 Robustness to AST changes.............................. 148 20.5 Limitations....................................... 148 20.5.1 Fortran support................................ 148 20.5.2 Exception handling............................... 148 20.5.3 Interprocedural control flow analysis..................... 148 20.6 Node filtering...................................... 148 20.6.1 \Interesting" node filter............................ 148 20.6.2 Arbitrary filtering............................... 149 20.7 Static CFG....................................... 149 20.7.1 Class methods................................. 149 20.7.2 Drawing a graph of the CFG......................... 150 20.8 Static, Interprocedural CFGs............................. 150 21 Generating Control Flow Graphs 153 22 Graph Processing Tutorial 157 22.1 Traversal Tutorial................................... 157 23 Dataflow Analysis 161 23.1 Def-Use Analysis.................................... 161 23.1.1 Def-use Example implementation....................... 161 23.1.2 Accessing the Def-Use Results........................ 162 23.2 Liveness Analysis.................................... 165 23.2.1 Access live variables.............................. 165 24 Generating the Call Graph (CG) 171 25 Dataflow Analysis based Virtual Function Analysis 175 vi CONTENTS 26 Generating the Class Hierarchy Graph 179 27 Database Support 183 27.1 ROSE DB Support for Persistent Analysis...................... 183 27.2 Call Graph for Multi-file Application......................... 183 27.3 Class Hierarchy Graph................................. 183 28 Building Custom Graphs 189 IV Program Transformations and Optimizations 191 29 Generating Unique Names for Declarations 193 29.1 Example Code Showing Generation of Unique Names................ 194 29.2 Input For Examples Showing Unique Name Generation for Variables....... 194 29.3 Example Output Showing Unique Variable Names................. 195 29.4 Input For Examples Showing Unique Name Generation for Functions....... 195 29.5 Example Output Showing Unique Function Names................. 195 30 Command-line Processing Within Translators 201 30.1 Commandline Selection of Files............................ 201 31 Tailoring The Code Generation Format 205 31.1 Source Code for Example that Tailors the Code Generation............ 205 31.2 Input to Demonstrate Tailoring the Code Generation................ 205 31.3 Final Code After Tailoring the Code Generation.................. 205 32 AST Construction 209 32.1 Variable Declarations.................................. 209 32.2 Expressions....................................... 213 32.3 Assignment Statements................................ 215 32.4 Functions........................................ 217 32.5 Function Calls..................................... 222 32.6 Creating a 'struct' for Global Variables........................ 222 33 Parser Building Blocks 233 33.1 Grammar Examples.................................. 234 33.2 AstAttribute to Store results............................. 235 33.3 The AstFromString Namespace............................ 236 33.4 Write your parsers using parser building blocks................... 236 33.5 Limitations....................................... 238 34 Handling Comments, Preprocessor Directives, And Adding Arbitrary Text to Generated Code 239 34.1 How to Access Comments and Preprocessor Directives............... 239 34.1.1 Source Code Showing How to Access Comments and Preprocessor Directives240 34.1.2 Input to example showing how to access comments and CPP directives. 240 CONTENTS vii 34.1.3 Comments and CPP Directives collected from source file (skipping headers)240 34.1.4 Comments and CPP Directives collected from source file and all header files240 34.2 Collecting #define C Preprocessor Directives.................... 240 34.2.1 Source Code Showing How to Collect #define Directives.......... 240 34.2.2 Input to example showing how to access comments and CPP directives. 241 34.2.3 Comments and CPP Directives collected from source file and all header files241 34.3 Automated Generation of Comments......................... 241 34.3.1 Source Code Showing Automated Comment Generation.......... 241 34.3.2 Input to Automated Addition of Comments................. 241 34.3.3 Final Code After Automatically Adding Comments............. 241 34.4 Addition of Arbitrary Text to Unparsed Code Generation............. 242 34.4.1 Source Code Showing Automated Arbitrary Text Generation....... 242 34.4.2 Input to Automated Addition of Arbitrary Text.............. 242 34.4.3 Final Code After Automatically Adding Arbitrary Text.......... 242 35 Partial Redundancy Elimination (PRE) 253 35.1 Source Code for example using PRE........................