ROSE-Tutorial

Document Sample
ROSE-Tutorial Powered By Docstoc
					              ROSE Tutorial:
            A Tool for Building
        Source-to-Source Translators
               Draft Tutorial
              (version 0.9.5a)


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)
               {dquinlan,panas2,liao6}@llnl.gov
                markus@complang.tuwien.ac.at
                      qingyi@cs.utsa.edu
                     richie@cc.gatech.edu
                      jewillco@osl.iu.edu
           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)

                    January 31, 2012
ii

     January 31, 2012
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 . . . . . . . . . . . . . . . . .                    .   .   .   .   .   .   .   .   .   .   .   .   45
       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) . . . . . .                                   .   .   .   .   .   .   .   .   .    70

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                                                                          111
   17.1 Example Template Code #1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
   17.2 Example Template Code #2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
CONTENTS                                                                                                                                            v

III    Program Analyses                                                                                                                           113
18 Recognizing Loops                                                                                                                              115

19 Virtual CFG                                                                                                                                    119
   19.1 CFGNode Index values . . . . . . . . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   119
   19.2 Important functions . . . . . . . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   120
        19.2.1 Node methods . . . . . . . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   120
        19.2.2 Edge methods . . . . . . . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   120
   19.3 Drawing a graph of the CFG . . . . . . . .            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   121
   19.4 Robustness to AST changes . . . . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   128
   19.5 Limitations . . . . . . . . . . . . . . . . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   128
        19.5.1 Fortran support . . . . . . . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   128
        19.5.2 Exception handling . . . . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   128
        19.5.3 Interprocedural control flow analysis           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   128
   19.6 Node filtering . . . . . . . . . . . . . . . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   128
        19.6.1 “Interesting” node filter . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   128
        19.6.2 Arbitrary filtering . . . . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   129
   19.7 Static CFG . . . . . . . . . . . . . . . . . .        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   129
        19.7.1 Class methods . . . . . . . . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   129
        19.7.2 Drawing a graph of the CFG . . . .             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   130
   19.8 Static, Interprocedural CFGs . . . . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   130

20 Generating Control Flow Graphs                                                                                                                 133

21 Graph Processing Tutorial                                                                     137
   21.1 Traversal Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

22 Dataflow Analysis                                                                                                                               141
   22.1 Def-Use Analysis . . . . . . . . . . . . .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   141
        22.1.1 Def-use Example implementation         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   141
        22.1.2 Accessing the Def-Use Results .        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   143
   22.2 Liveness Analysis . . . . . . . . . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   145
        22.2.1 Access live variables . . . . . . .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   145

23 Generating the Call Graph (CG)                                                                                                                 151

24 Dataflow Analysis based Virtual Function Analysis                                                                                               155

25 Generating the Class Hierarchy Graph                                                                                                           159

26 Database Support                                                                             163
   26.1 ROSE DB Support for Persistent Analysis . . . . . . . . . . . . . . . . . . . . . . 163
   26.2 Call Graph for Multi-file Application . . . . . . . . . . . . . . . . . . . . . . . . . 163
   26.3 Class Hierarchy Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

27 Building Custom Graphs                                                                                                                         169
vi                                                                                                                            CONTENTS

IV    Program Transformations and Optimizations                                                                                                   171
28 Generating Unique Names for Declarations                                                                                                       173
   28.1 Example Code Showing Generation of Unique Names . . . . . . . . .                                             .   .   .   .   .   .   .   174
   28.2 Input For Examples Showing Unique Name Generation for Variables                                               .   .   .   .   .   .   .   174
   28.3 Example Output Showing Unique Variable Names . . . . . . . . . .                                              .   .   .   .   .   .   .   175
   28.4 Input For Examples Showing Unique Name Generation for Functions                                               .   .   .   .   .   .   .   175
   28.5 Example Output Showing Unique Function Names . . . . . . . . . .                                              .   .   .   .   .   .   .   175

29 Command-line Processing Within Translators                                                  181
   29.1 Commandline Selection of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

30 Tailoring The Code Generation Format                                                                                                           185
   30.1 Source Code for Example that Tailors the Code Generation . . . . . . . . . . . .                                                          185
   30.2 Input to Demonstrate Tailoring the Code Generation . . . . . . . . . . . . . . . .                                                        185
   30.3 Final Code After Tailoring the Code Generation . . . . . . . . . . . . . . . . . .                                                        185

31 AST Construction                                                                                                                               189
   31.1 Variable Declarations . . . . . . . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   189
   31.2 Expressions . . . . . . . . . . . . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   193
   31.3 Assignment Statements . . . . . . . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   195
   31.4 Functions . . . . . . . . . . . . . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   197
   31.5 Function Calls . . . . . . . . . . . . . .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   202
   31.6 Creating a ’struct’ for Global Variables .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   202

32 Parser Building Blocks                                                                                                                         215
   32.1 Grammar Examples . . . . . . . . . . . . . . .                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   216
   32.2 AstAttribute to Store results . . . . . . . . . .             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   217
   32.3 The AstFromString Namespace . . . . . . . . .                 .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   218
   32.4 Write your parsers using parser building blocks               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   218
   32.5 Limitations . . . . . . . . . . . . . . . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   220

33 Handling Comments, Preprocessor Directives, And Adding Arbitrary Text to
   Generated Code                                                                           221
   33.1 How to Access Comments and Preprocessor Directives . . . . . . . . . . . . . . . 221
        33.1.1 Source Code Showing How to Access Comments and Preprocessor Directives222
        33.1.2 Input to example showing how to access comments and CPP directives . 222
        33.1.3 Comments and CPP Directives collected from source file (skipping headers)222
        33.1.4 Comments and CPP Directives collected from source file and all header files222
   33.2 Collecting #define C Preprocessor Directives . . . . . . . . . . . . . . . . . . . . 222
        33.2.1 Source Code Showing How to Collect #define Directives . . . . . . . . . . 222
        33.2.2 Input to example showing how to access comments and CPP directives . 224
        33.2.3 Comments and CPP Directives collected from source file and all header files225
   33.3 Automated Generation of Comments . . . . . . . . . . . . . . . . . . . . . . . . . 225
        33.3.1 Source Code Showing Automated Comment Generation . . . . . . . . . . 226
        33.3.2 Input to Automated Addition of Comments . . . . . . . . . . . . . . . . . 226
CONTENTS                                                                                                                     vii

         33.3.3 Final Code After Automatically Adding Comments . . . . . .                      .   .   .   .   .   .   .   226
    33.4 Addition of Arbitrary Text to Unparsed Code Generation . . . . . .                     .   .   .   .   .   .   .   226
         33.4.1 Source Code Showing Automated Arbitrary Text Generation                         .   .   .   .   .   .   .   226
         33.4.2 Input to Automated Addition of Arbitrary Text . . . . . . .                     .   .   .   .   .   .   .   227
         33.4.3 Final Code After Automatically Adding Arbitrary Text . . .                      .   .   .   .   .   .   .   227

34 Partial Redundancy Elimination (PRE)                                                     235
   34.1 Source Code for example using PRE . . . . . . . . . . . . . . . . . . . . . . . . . 235
   34.2 Input to Example Demonstrating PRE . . . . . . . . . . . . . . . . . . . . . . . . 236
   34.3 Final Code After PRE Transformation . . . . . . . . . . . . . . . . . . . . . . . . 237

35 Calling the Inliner                                                                          239
   35.1 Source Code for Inliner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
   35.2 Input to Demonstrate Function Inlining . . . . . . . . . . . . . . . . . . . . . . . 239
   35.3 Final Code After Function Inlining . . . . . . . . . . . . . . . . . . . . . . . . . . 239

36 Using the AST Outliner                                                                                                   243
   36.1 An Outlining Example . . . . . . . . . . . . . . . . . . . .    .   .   .   .   .   .   .   .   .   .   .   .   .   243
   36.2 Limitations of the Outliner . . . . . . . . . . . . . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   244
   36.3 User-Directed Outlining via Pragmas . . . . . . . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   246
   36.4 Outlining via Abstract Handles . . . . . . . . . . . . . . .    .   .   .   .   .   .   .   .   .   .   .   .   .   246
   36.5 Calling Outliner Directly on AST Nodes . . . . . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   248
        36.5.1 Selecting the outlineable if statements . . . . . . .    .   .   .   .   .   .   .   .   .   .   .   .   .   249
        36.5.2 Properly ordering statements for in-place outlining      .   .   .   .   .   .   .   .   .   .   .   .   .   249
   36.6 Outliner’s Preprocessing Phase . . . . . . . . . . . . . . .    .   .   .   .   .   .   .   .   .   .   .   .   .   253

37 Loop Optimization                                                                                                        259
   37.1 Example Loop Optimizer . . . . . . . . . . . . . . . . . . . . . . . . .                    .   .   .   .   .   .   259
   37.2 Matrix Multiply Example . . . . . . . . . . . . . . . . . . . . . . . . .                   .   .   .   .   .   .   262
   37.3 Loop Fusion Example . . . . . . . . . . . . . . . . . . . . . . . . . . .                   .   .   .   .   .   .   264
   37.4 Example Loop Processor (LoopProcessor.C) . . . . . . . . . . . . . . .                      .   .   .   .   .   .   264
   37.5 Matrix Multiplication Example (mm.C) . . . . . . . . . . . . . . . . .                      .   .   .   .   .   .   267
   37.6 Matrix Multiplication Example Using Linearized Matrices (dgemm.C)                           .   .   .   .   .   .   269
   37.7 LU Factorization Example (lufac.C) . . . . . . . . . . . . . . . . . . .                    .   .   .   .   .   .   271
   37.8 Loop Fusion Example (tridvpk.C) . . . . . . . . . . . . . . . . . . . .                     .   .   .   .   .   .   273

38 Parameterized Code Translation                                                                 275
   38.1 Loop Unrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
   38.2 Loop Interchange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
   38.3 Loop Tiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280


V    Correctness Checking                                                                                                   281
39 Code Coverage                                                                                                            283
viii                                                                                                                     CONTENTS

40 Bug Seeding                                                                              291
   40.1 Input For Examples Showing Bug Seeding . . . . . . . . . . . . . . . . . . . . . . 291
   40.2 Generating the code representing the seeded bug . . . . . . . . . . . . . . . . . . 292


VI     Binary Support                                                                                                                        295

41 Instruction Semantics                                                                        297
   41.1 The FindConstantsPolicy Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
   41.2 Sample Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
   41.3 Building on Instruction Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . 303

42 Binary Analysis                                                                                                                           305
   42.1 The ControlFlowGraph . . . . . . . . . . . . . . . . . . . . . . . .                                 .   .   .   .   .   .   .   .   305
   42.2 DataFlow Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . .                              .   .   .   .   .   .   .   .   305
        42.2.1 Def-Use Analysis . . . . . . . . . . . . . . . . . . . . . . . .                              .   .   .   .   .   .   .   .   305
        42.2.2 Variable Analysis . . . . . . . . . . . . . . . . . . . . . . . .                             .   .   .   .   .   .   .   .   307
   42.3 Dynamic Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   307
   42.4 Analysis and Transformations on Binaries . . . . . . . . . . . . . .                                 .   .   .   .   .   .   .   .   308
        42.4.1 Source-to-source transformations to introduce NOPs . . . .                                    .   .   .   .   .   .   .   .   308
        42.4.2 Detection of NOP sequences in the binary AST . . . . . . .                                    .   .   .   .   .   .   .   .   310
        42.4.3 Transformations on the NOP sequences in the binary AST                                        .   .   .   .   .   .   .   .   311
        42.4.4 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . .                             .   .   .   .   .   .   .   .   311

43 Binary Construction                                                                                                                       315
   43.1 Constructors . . . . . . . . . . . . . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   315
   43.2 Read-Only Data Members . . . . . . . . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   315
   43.3 Constructing the Executable File Container       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   316
   43.4 Constructing the ELF File Header . . . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   316
   43.5 Constructing the ELF Segment Table . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   317
   43.6 Constructing the .text Section . . . . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   317
   43.7 Constructing a LOAD Segment . . . . . . .        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   319
   43.8 Constructing a PAX Segment . . . . . . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   320
   43.9 Constructing a String Table . . . . . . . . .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   320
   43.10Constructing an ELF Section Table . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   320
   43.11Allocating Space . . . . . . . . . . . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   321
   43.12Produce a Debugging Dump . . . . . . . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   321
   43.13Produce the Executable File . . . . . . . . .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   321

44 Dwarf Debug Support                                                                     323
   44.1 ROSE AST of Dwarf IR nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
   44.2 Source Position to Instruction Address Mapping . . . . . . . . . . . . . . . . . . 324
CONTENTS                                                                                                                                        ix

VII     Interacting with Other Tools                                                                                                           329
45 Abstract Handles to Language Constructs                                                                                                     331
   45.1 Use Case . . . . . . . . . . . . . . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   332
   45.2 Syntax . . . . . . . . . . . . . . . . . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   332
   45.3 Examples . . . . . . . . . . . . . . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   333
   45.4 Reference Implementation . . . . . . . . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   334
        45.4.1 Connecting to ROSE . . . . . . . . .        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   334
        45.4.2 Connecting to External Tools . . . .        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   340
   45.5 Summary . . . . . . . . . . . . . . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   343

46 ROSE-HPCToolKit Interface                                                                                                                   345
   46.1 An HPCToolkit Example Run . . . . . . . . . .              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   345
   46.2 Attaching HPCToolkit Data to the ROSE AST                  .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   352
        46.2.1 Calling ROSE-HPCT . . . . . . . . . .               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   352
        46.2.2 Retrieving the attribute values . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   352
        46.2.3 Metric propagation . . . . . . . . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   352
   46.3 Working with GNU gprof . . . . . . . . . . . .             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   353
   46.4 Command-line options . . . . . . . . . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   354

47 TAU Instrumentation                                                                       359
   47.1 Input For Examples Showing Information using Tau . . . . . . . . . . . . . . . . 359
   47.2 Generating the code representing any IR node . . . . . . . . . . . . . . . . . . . . 359

48 The Haskell Interface                                                                           363
   48.1 Traversals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
   48.2 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364


VIII     Parallelism                                                                                                                           367
49 Shared-Memory Parallel Traversals                                                                                                           369

50 Distributed-Memory Parallel Traversals                                                                                                      373

51 Parallel Checker                                                                            377
   51.1 Different Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
   51.2 Running through PSUB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377

52 Reduction Recognition                                                                                                                       379


IX     Tutorial Summary                                                                                                                        381
53 Tutorial Wrap-up                                                                                                                            383
x                                                                                    CONTENTS

Appendix                                                                                        385
  53.1 Location of To Do List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
  53.2 Abstract Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385

Glossary                                                                                      393
List of Figures

 1.1   Example Makefile showing how to use an installed version of ROSE (generated
       by make install). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .       12

 2.1   Source code for translator to read an input program and generate an object code
       (with no translation). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      16
 2.2   Example source code used as input to identity translator. . . . . . . . . . . . . .           16
 2.3   Generated code, from ROSE identity translator, sent to the backend (vendor)
       compiler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     17

 3.1   Example source code to read an input program and generate an AST graph. . . .                 20
 3.2   Example source code used as input to generate the AST graph. . . . . . . . . . .              20
 3.3   AST representing the source code file: inputCode ASTGraphGenerator.C. . . . .                  21

 4.1   Example source code to read an input program and generate a whole AST graph.                  24
 4.2   Example tiny source code used as input to generate the small AST graph with
       attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   24
 4.3   AST representing the tiny source code file: inputCode wholeAST 1.C. This graphs
       shows types, symbols, and other attributes that are defined on the attributed AST.             25
 4.4   Example source code used as input to generate a larger AST graph with attributes.             26
 4.5   AST representing the small source code file: inputCode wholeAST 2.C. This
       graph shows the significantly greater internal complexity of a slightly larger input
       source code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .    27

 6.1   Example source code to read an input program and generate a PDF file to repre-
       sent the AST. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     31
 6.2   Example source code used as input to generate the PDF file of the AST. . . . . .               32
 6.3   Example output from translator which outputs PDF representation of AST. The
       generated PDF file makes use of the bookmark mechanism to expand and collapse
       parts of the AST. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     33

 7.1   Example source code used as input to program in traversals shown in this chapter.             36
 7.2   Example source showing simple visitor pattern. . . . . . . . . . . . . . . . . . . .          38
 7.3   Output of input file to the visitor pattern traversal over the memory pools. . . .             40
 7.4   Example source showing simple visitor pattern. . . . . . . . . . . . . . . . . . . .          41

                                                xi
xii                                                                                    LIST OF FIGURES

      7.5    Output of input file to the visitor traversal. . . . . . . . . . . . . . . . . . . . . .       41
      7.6    Example source showing simple pre- and postorder pattern. . . . . . . . . . . . .             42
      7.7    Output of input file to the pre- and postorder traversal. . . . . . . . . . . . . . .          42
      7.8    Example source code showing use of inherited attributes (passing context infor-
             mation down the AST. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .          43
      7.9    Output of input file to the inherited attribute traversal. . . . . . . . . . . . . . .         44
      7.10   Example source code showing use of synthesized attributed (passing analysis in-
             formation up the AST). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .        45
      7.11   Output of input file to the synthesized attribute traversal. . . . . . . . . . . . . .         46
      7.12   Example source code showing use of accumulator attributes (typically to count
             things in the AST). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     47
      7.13   Output of input file to the accumulator attribute traversal. . . . . . . . . . . . .           48
      7.14   Example source code showing use of both inherited and synthesized attributes
             working together (part 1). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      49
      7.15   Output of input file to the inherited and synthesized attribute traversal. . . . . .           50
      7.16   Example source code showing use of persistent attributes used to pass information
             across multiple passes over the AST. . . . . . . . . . . . . . . . . . . . . . . . . .        52
      7.17   Output of input file to the persistent attribute traversal showing the passing of
             information from one AST traversal to a second AST traversal. . . . . . . . . . .             53
      7.18   Example source code showing use nested traversals. . . . . . . . . . . . . . . . . .          54
      7.19   Output of input file to the nested traversal example. . . . . . . . . . . . . . . . .          55
      7.20   Input code with nested loops for nesting info processing . . . . . . . . . . . . . .          56
      7.21   Example source code showing use of inherited, synthesized, accumulator, and
             persistent attributes (part 1). . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     58
      7.22   Example source code showing use of inherited, synthesized, accumulator, and
             persistent attributes (part 2). . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     59
      7.23   Output code showing the result of using inherited, synthesized, and accumulator
             attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   60
      7.24   Example source showing the combination of traversals. . . . . . . . . . . . . . . .           61
      7.25   Output of input file to the combined traversals. Note that the order of outputs
             changes as execution of several analyzers is interleaved. . . . . . . . . . . . . . .         62
      7.26   Input code with used to demonstrate the traversal short-circuit mechanism. . . .              63
      7.27   Example source code showing use of short-circuit mechanism to avoid traversal of
             full AST. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     64
      7.28   Output code showing the result of short-circuiting the traversal. . . . . . . . . .           65
      7.29   Example source showing simple visit traversal over the memory pools. . . . . . .              67
      7.30   Output of input file to the visitor traversal over the memory pool. . . . . . . . .            67
      7.31   Example source showing simple visitor pattern. . . . . . . . . . . . . . . . . . . .          68
      7.32   Output of input file to the visitor pattern traversal over the memory pools. . . .             69
      7.33   Example source showing simple visit traversal over each type of IR node (one
             only) in the memory pools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .        71
      7.34   Output of input file to the IR Type traversal over the memory pool. . . . . . . .              72
      7.35   Example of output using -rose:verbose 2 (memory use report for AST). . . . . . .              72

      8.1    Source CFG Traversal Example . . . . . . . . . . . . . . . . . . . . . . . . . . . .          74
      8.2    Binary CFG Traversal Example . . . . . . . . . . . . . . . . . . . . . . . . . . . .          75
LIST OF FIGURES                                                                                        xiii

  9.1    Example source code used as input to program in codes used in this chapter. . .                78
  9.2    Example source code showing how to get scope information for each IR node. .                   79
  9.3    Output of input code using scopeInformation.C . . . . . . . . . . . . . . . . . . .            80

  10.1 Example source code for translator to read an input program and generate a list
       of functions in the AST (queryLibraryExample.C). . . . . . . . . . . . . . . . . .               82
  10.2 Example source code used as input to program in figure 10.1 (queryLibraryEx-
       ample.C). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .        83
  10.3 Output of input file to the AST query processor (queryLibraryExample.C). . . .                    84
  10.4 Example source code for translator to read an input program and generate a list
       of access functions in the AST (nestedQueryExample.C). . . . . . . . . . . . . .                 85
  10.5 Example source code used as input to program in figure 10.4 (nestedQueryExam-
       ple.C). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      86
  10.6 Output of input file to the AST query processor (nestedQueryExample.C). . . .                     86

  11.1   Example source code showing how to use the AST file I/O support. . . . . .             .   .    88
  11.2   Example source code used as input to demonstrate the AST file I/O support.             .   .    89
  11.3   Output of input code after inlining transformations. . . . . . . . . . . . . . .      .   .    89
  11.4   Output of input code after file I/O. . . . . . . . . . . . . . . . . . . . . . . . .   .   .    90

  12.1 Example source code used as input to program in codes showing debugging tech-
       niques shown in this section. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .        91
  12.2 Example source code showing the output of the string from an IR node. The
       string represents the code associated with the subtree of the target IR node. . . .              93
  12.3 Output of input code using debuggingIRnodeToString.C . . . . . . . . . . . . . .                 93
  12.4 Example source code showing the output of the string from an IR node. The
       string represents the code associated with the subtree of the target IR node. . . .              94
  12.5 Output of input code using debuggingSourceCodePositionInformation.C . . . . .                    94

  13.1 Example source code used as input to program in codes used in this chapter. . .                  97
  13.2 Example source code showing how to detect volatile modifier. . . . . . . . . . .                  98
  13.3 Output of input code using volatileTypeModifier.C . . . . . . . . . . . . . . . . .               99

  14.1 Example source code showing how to get type information from function parameters.102
  14.2 Example source code used as input to typeInfoFromFunctionParameters.C. . . . 103
  14.3 Output of input to typeInfoFromFunctionParameters.C. . . . . . . . . . . . . . . 104

  15.1 Example source code showing mapping of function calls to overloaded function
       declarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
  15.2 Example source code used as input to resolveOverloadedFunction.C. . . . . . . . 107
  15.3 Output of input to resolveOverloadedFunction.C. . . . . . . . . . . . . . . . . . . 107

  16.1 Example source code used to extract template parameter information. . . . . . . 109
  16.2 Example source code used as input to templateParameter.C. . . . . . . . . . . . 110
  16.3 Output of input to templateParameter.C. . . . . . . . . . . . . . . . . . . . . . . 110

  17.1 Example source code showing use of a C++ template. . . . . . . . . . . . . . . . 111
xiv                                                                                 LIST OF FIGURES

      17.2 Example source code after processing using identityTranslator (shown in figure 2.1).112
      17.3 Example source code showing use of a C++ template. . . . . . . . . . . . . . . . 112
      17.4 Example source code after processing using identityTranslator (shown in figure 2.1).112

      18.1   Example source code showing loop recognition (part 1). . . . . .       .   .   .   .   .   .   .   .   .   116
      18.2   Example source code showing loop recognition (part 2). . . . . .       .   .   .   .   .   .   .   .   .   117
      18.3   Example source code used as input to loop recognition processor.       .   .   .   .   .   .   .   .   .   117
      18.4   Output of input to loop recognition processor. . . . . . . . . . . .   .   .   .   .   .   .   .   .   .   118

      19.1 Example source code showing visualization of virtual control flow graph. . . . . .                            122
      19.2 Example source code used as input to build virtual control graphs. . . . . . . . .                           123
      19.3 The debug virtual control flow graph for function main() shows all virtual CFG
           nodes and edges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                      124
      19.4 The virtual control flow graph for function main() shows only interesting vir-
           tual CFG nodes and edges. Each CFGNode’s caption tells associated source line
           number and CFGNode index value (@line-num:index-value) . . . . . . . . . . . .                               125
      19.5 The debug virtual control flow graph for function testIf() shows all virtual CFG
           nodes and edges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                      126
      19.6 The virtual control flow graph for function testIf() shows only interesting vir-
           tual CFG nodes and edges. Each CFGNode’s caption tells associated source line
           number and CFGNode index value (@line-num:index-value) . . . . . . . . . . . .                               127
      19.7 Example source code showing visualization of static control flow graph. . . . . .                             130

      20.1 Example source code showing visualization of control flow graph. . . . . . . . . . 134
      20.2 Example source code used as input to build control flow graph. . . . . . . . . . . 135
      20.3 Control flow graph for function in input code file: inputCode 1.C. . . . . . . . . 135

      21.1 Source CFG Traversal Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
      21.2 Binary CFG Traversal Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

      22.1   Example input code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .            .   .   .   .   141
      22.2   Example source code using def use analysis . . . . . . . . . . . . . . . . .               .   .   .   .   142
      22.3   Output of the program . . . . . . . . . . . . . . . . . . . . . . . . . . . . .            .   .   .   .   142
      22.4   Def-Use graph for example program. . . . . . . . . . . . . . . . . . . . . .               .   .   .   .   144
      22.5   Example source code using liveness analysis . . . . . . . . . . . . . . . . .              .   .   .   .   146
      22.6   Example input code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .            .   .   .   .   147
      22.7   Control flow graph annotated with live variables for example program. . .                   .   .   .   .   148
      22.8   Example code retrieving live variables based on virtual control flow graph                  .   .   .   .   149

      23.1 Example source code showing visualization of call graph. . . . . . . . . . . . . . . 152
      23.2 Example source code used as input to build call graph. . . . . . . . . . . . . . . . 153
      23.3 Call graph for function in input code file: inputCode BuildCG.C. . . . . . . . . . 154

      24.1 Source code to perform virtual function analysis . . . . . . . . . . . . . . . . . . 156
      24.2 Example source code used as input for Virtual Function Analysis. . . . . . . . . . 157
      24.3 Call graph generated by Call Graph Analysis for input code in inputCode vfa.C. 157
LIST OF FIGURES                                                                                       xv

  24.4 Call graph resulted from Virtual Function Analysis for input code in input-
       Code vfa.C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

  25.1 Example source code showing visualization of class hierarchy graph. . . . . . . . 159
  25.2 Example source code used as input to build class hierarchy graph. . . . . . . . . 160
  25.3 Class hierarchy graph in input code file: inputCode ClassHierarchyGraph.C. . . . 161

  26.1   Example translator (part 1) using database connection to store function names. .             164
  26.2   Example translator (part 2) using database connection to store function names. .             165
  26.3   Example source code used as input to database example. . . . . . . . . . . . . . .           166
  26.4   Output from processing input code through database example dataBaseTransla-
         tor26.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   167

  28.1 Example source code showing the output of mangled name. The string represents
       the code associated with the subtree of the target IR node. . . . . . . . . . . . .            176
  28.2 Example source code used as input to program in codes showing debugging tech-
       niques shown in this section. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      177
  28.3 Output of input code using generatingUniqueNamesFromDeclaration.C . . . . .                    178
  28.4 Example source code used as input to program in codes showing debugging tech-
       niques shown in this section. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      179
  28.5 Output of input code using generatingUniqueNamesFromDeclaration.C . . . . .                    180

  29.1 Example source code showing simple command-line processing within ROSE trans-
       lator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   182
  29.2 Output of input code using commandlineProcessing.C . . . . . . . . . . . . . . .               182
  29.3 Example source code showing simple command-line processing within ROSE trans-
       lator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   183
  29.4 Output of input code using commandlineProcessing.C . . . . . . . . . . . . . . .               183

  30.1 Example source code showing how to tailor the code generation format. . . . . . 186
  30.2 Example source code used as input to program to the tailor the code generation. 187
  30.3 Output of input code after changing the format of the generated code. . . . . . . 188

  31.1 AST construction and insertion for a variable using the high level interfaces . . .            190
  31.2 Example source code to read an input program and add a new variable declaration
       at the top of each block. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      191
  31.3 Example source code used as input to the translators adding new variable. . . . .              192
  31.4 Output of input to the translators adding new variable. . . . . . . . . . . . . . .            192
  31.5 Example translator to add expressions . . . . . . . . . . . . . . . . . . . . . . . .          193
  31.6 Example source code used as input . . . . . . . . . . . . . . . . . . . . . . . . . .          194
  31.7 Output of the input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .        194
  31.8 Example source code to add an assignment statement . . . . . . . . . . . . . . .               195
  31.9 Example source code used as input . . . . . . . . . . . . . . . . . . . . . . . . . .          195
  31.10Output of the input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .        196
  31.11Addition of function to global scope using high level interfaces . . . . . . . . . .           197
  31.12Addition of function to global scope using high level interfaces and a scope stack             198
  31.13Example source code shows addition of function to global scope (part 1). . . . .               199
xvi                                                                                 LIST OF FIGURES

      31.14Example source code shows addition of function to global scope (part 2). .           . .   .   200
      31.15Example source code used as input to translator adding new function. . . .           . .   .   201
      31.16Output of input to translator adding new function. . . . . . . . . . . . . . .       . .   .   201
      31.17Example source code to instrument any input program. . . . . . . . . . . .           . .   .   203
      31.18Example source code using the high level interfaces . . . . . . . . . . . . . .      . .   .   204
      31.19Example source code used as input to instrumenting translator. . . . . . . .         . .   .   205
      31.20Output of input to instrumenting translator. . . . . . . . . . . . . . . . . .       . .   .   205
      31.21Example source code instrumenting end of functions . . . . . . . . . . . . .         . .   .   206
      31.22Example input code of the instrumenting translator for end of functions. . .         . .   .   206
      31.23Output of instrumenting translator for end of functions. . . . . . . . . . . .       . .   .   207
      31.24Example source code shows repackaging of global variables to a struct (part          1).   .   208
      31.25Example source code shows repackaging of global variables to a struct (part          2).   .   209
      31.26Example source code shows repackaging of global variables to a struct (part          3).   .   210
      31.27Example source code shows repackaging of global variables to a struct (part          4).   .   211
      31.28Example source code shows repackaging of global variables to a struct (part          5).   .   212
      31.29Example source code used as input to translator adding new function. . . .           . .   .   213
      31.30Output of input to translator adding new function. . . . . . . . . . . . . . .       . .   .   213

      33.1 Example source code showing how to access comments. . . . . . . . . . . . . . .                223
      33.2 Example source code used as input to collection of comments and CPP directives.                224
      33.3 Output from collection of comments and CPP directives on the input source file
           only. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .    224
      33.4 Output from collection of comments and CPP directives on the input source file
           and all header files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     225
      33.5 Example source code showing how to access comments. . . . . . . . . . . . . . .                228
      33.6 Example source code used as input to collection of comments and CPP directives.                229
      33.7 Output from collection of comments and CPP directives on the input source file
           and all header files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     229
      33.8 Example source code showing how automate comments. . . . . . . . . . . . . . .                 230
      33.9 Example source code used as input to automate generation of comments. . . . .                  231
      33.10Output of input code after automating generation of comments. . . . . . . . . . .              231
      33.11Example source code showing how automate the introduction of arbitrary text.                   232
      33.12Example source code used as input to automate generation of arbitrary text. . .                232
      33.13Output of input code after automating generation of arbitrary text. . . . . . . .              233

      34.1 Example source code showing how use Partial Redundancy Elimination (PRE).             235
      34.2 Example source code used as input to program to the Partial Redundancy Elim-
           ination (PRE) transformation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
      34.3 Output of input code after Partial Redundancy Elimination (PRE) transformation.238

      35.1 Example source code showing how to instrument using Tau. . . . . . . . . . . . 240
      35.2 Example source code used as input to program to the inlining transformation. . . 241
      35.3 Output of input code after inlining transformations. . . . . . . . . . . . . . . . . 242

      36.1 inputCode OutlineLoop.cc: Sample input program. The #pragma directive marks
           the nested for loop for outlining. . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
LIST OF FIGURES                                                                                   xvii

  36.2 rose outlined-inputCode OutlineLoop.cc: The nested for loop of Figure 36.1 has
       been outlined. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
  36.3 outline.cc: A basic outlining translator, which generates Figure 36.2 from Fig-
       ure 36.1. This outliner relies on the high-level driver, Outliner::outlineAll(),
       which scans the AST for outlining pragma directives (#pragma rose outline)
       that mark outline targets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
  36.4 inputCode OutlineLoop2.c: Sample input program without pragmas. . . . . . . . 247
  36.5 rose inputCode OutlineLoop2.c: The loop at line 12 of Figure 36.12 has been
       outlined. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
  36.6 rose inputCode OutlineLoop2b.c: The 2nd loop within a function named initial-
       izefrom Figure 36.12 has been outlined. . . . . . . . . . . . . . . . . . . . . . . . 249
  36.7 outlineIfs.cc: A lower-level outlining translator, which calls Outliner::outline()
       directly on SgStatement nodes. This particular translator outlines all SgIfStmt
       nodes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
  36.8 inputCode Ifs.cc: Sample input program, without explicit outline targets specified
       using #pragma rose outline, as in Figures 36.1 and 36.12. . . . . . . . . . . . . 251
  36.9 rose inputCode Ifs.cc: Figure 36.8, after outlining using the translator in Fig-
       ure 36.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
  36.10outlinePreproc.cc: The basic translator of Figure 36.3, modified to execute the
       Outliner’s preprocessing phase only. In particular, the original call to Outliner::outlineAll()
       has been replaced by a call to Outliner::preprocessAll(). . . . . . . . . . . . 253
  36.11rose outlined pp-inputCode OutlineLoop.cc: Figure 36.1 after outline preprocess-
       ing only, i.e., specifying -rose:outline:preproc-only as an option to the trans-
       lator of Figure 36.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
  36.12inputCode OutlineNonLocalJumps.cc: Sample input program, with an outlining
       target that contains two non-local jumps (here, break statements). . . . . . . . . 255
  36.13rose outlined pp-inputCode OutlineNonLocalJumps.cc: The non-local jump ex-
       ample of Figure 36.12 after outliner preprocessing, but before the actual outlin-
       ing. The non-local jump is handled by an additional flag, EXIT TAKEN , which
       indicates what non-local jump is to be taken. . . . . . . . . . . . . . . . . . . . . 256
  36.14rose outlined-inputCode OutlineNonLocalJumps.cc: Figure 36.12 after outlining. 257

  37.1 Example source code showing use of loop optimization mechanisms. . . . . . . .             261
  37.2 Example source code used as input to loop optimization processor. . . . . . . . .          262
  37.3 Output of loop optimization processor showing matrix multiply optimization (us-
       ing options: -bk1 -fs0). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   263
  37.4 Example source code used as input to loop optimization processor. . . . . . . . .          264
  37.5 Output of loop optimization processor showing loop fusion (using options: -fs2).           264
  37.6 Detailed example source code showing use of loop optimization mechanisms (loop-
       Processor.C part 1). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   265
  37.7 loopProcessor.C source code (Part 2). . . . . . . . . . . . . . . . . . . . . . . . .      266
  37.8 Example source code used as input to loopProcessor, show in figure 37.6. . . . .            267
  37.9 Output of loopProcessor using input from figure 37.8 (using options: -bk1 -fs0).            268
  37.10Example source code used as input to loopProcessor, show in figure 37.6. . . . .            269
  37.11Output of loopProcessor using input from figure 37.10 (using options: -bk1
       -unroll nvar 16). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .    270
xviii                                                                              LIST OF FIGURES

   37.12Example source code used as input to loopProcessor, show in figure 37.6. . . . .                           271
   37.13Output of loopProcessor using input from figure 37.12 (using options: -bk1 -fs0
        -splitloop -annotation). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                      272
   37.14Example source code used as input to loopProcessor, show in figure 37.6. . . . .                           273
   37.15Output of loopProcessor input from figure 37.14 (using options: -fs2 -ic1 -opt
        1 ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                274

   38.1   Example source code used as input to loopUnrolling . . . . . . .         . . . .    .   .   .   .   .   276
   38.2   Output for a unrolling factor which can divide the iteration space       evenly     .   .   .   .   .   277
   38.3   Output for the case when divisibility is unknown at compile-time         . . . .    .   .   .   .   .   278
   38.4   Example source code used as input to loopInterchange . . . . . .         . . . .    .   .   .   .   .   279
   38.5   Output for loop interchange . . . . . . . . . . . . . . . . . . . . .    . . . .    .   .   .   .   .   279
   38.6   Example source code used as input to loopTiling . . . . . . . . .        . . . .    .   .   .   .   .   280
   38.7   Output for loop tiling . . . . . . . . . . . . . . . . . . . . . . . .   . . . .    .   .   .   .   .   280

   39.1 Example source code shows instrumentation to call a test function from                the top
        of each function body in the application (part 1). . . . . . . . . . . . . .          . . . . .           286
   39.2 Example source code shows instrumentation to call a test function from                the top
        of each function body in the application (part 2). . . . . . . . . . . . . .          . . . . .           287
   39.3 Example source code shows instrumentation to call a test function from                the top
        of each function body in the application (part 3). . . . . . . . . . . . . .          . . . . .           288
   39.4 Example source code used as input to translator adding new function. .                . . . . .           289
   39.5 Output of input to translator adding new function. . . . . . . . . . . . .            . . . . .           290

   40.1 Example source code used as input to program in codes used in this chapter. . . 291
   40.2 Example source code showing how to seed bugs. . . . . . . . . . . . . . . . . . . 293
   40.3 Output of input code using seedBugsExample arrayIndexing.C . . . . . . . . . . 294

   42.1 Dataflowflow graph for example program. . . . . . . . . . . . . . . . . . . . . . .                         306
   42.2 Source-to-source transformation to introduce NOP assemble instructions in the
        generated binary executable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                    309
   42.3 Header file for the traversal used to identify the NOP sequences in the binary
        executable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                 310
   42.4 Example code to identify the NOP sequences in the binary executable. . . . . . .                          312
   42.5 Main program using the traversal to identify the NOP sequences in the binary
        executable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                 313
   42.6 Example code showing the transformation of the binary executable. . . . . . . .                           314

   44.1   Example source code used to generate Dwarf AST for analysis. . . .          .   .   .   .   .   .   .   323
   44.2   Dwarf AST (subset of ROSE binary AST). . . . . . . . . . . . . . .          .   .   .   .   .   .   .   325
   44.3   Example source code (typical for reading in a binary or source file).        .   .   .   .   .   .   .   326
   44.4   Example source code (typical for reading in a binary or source file).        .   .   .   .   .   .   .   327

   45.1 Example 1: Generated handles for loops: using constructors with             or without a
        specified handle type. . . . . . . . . . . . . . . . . . . . . . . . . .     . . . . . . . . 335
   45.2 Example 1: Example source code with some loops, used as input. .            . . . . . . . . 336
   45.3 Example 1: Abstract handles generated for loops. . . . . . . . . . .        . . . . . . . . 337
LIST OF FIGURES                                                                                          xix

  45.4   Example     2: Generated handles from strings representing handle items. . . . . . .            338
  45.5   Example     2: Source code with some language constructs. . . . . . . . . . . . . . .           339
  45.6   Example     2: Handles generated from string and their language constructs. . . . .             339
  45.7   Example     3: A simple data structure used to represent a loop in an arbitrary tool.           340
  45.8   Example     3: A test program for simple loops’ abstract handles. . . . . . . . . . .           341
  45.9   Example     3: Output of the test program for simple loops’ abstract handles (as
         strings).   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   342

  46.1 profiled.c (part 1 of 2): Sample input program, profiled using the HPCToolkit. .                    347
  46.2 profiled.c (part 2 of 2): Sample input program, profiled using the HPCToolkit. .                    348
  46.3 XML schema for HPCToolkit data files: This schema, prepended to each of the
       HPCToolkit-generated XML files, describes the format of the profiling data. This
       particular schema was generated by HPCToolkit 1.0.4. . . . . . . . . . . . . . . .                349
  46.4 PAPI TOT CYC.xml: Sample cycle counts observed during profiling, generated
       from running the HPCToolkit on profiled.c (Figures 46.1–46.2.) These lines would
       appear after the schema shown in Figure 46.3. . . . . . . . . . . . . . . . . . . . .             350
  46.5 PAPI FP OPS.xml: Sample flop counts observed during profiling, generated from
       running the HPCToolkit on profiled.c (Figures 46.1–46.2.) These lines would
       appear after the schema shown in Figure 46.3. . . . . . . . . . . . . . . . . . . . .             351
  46.6 attachMetrics.cc: Sample translator to attach HPCToolkit metrics to the AST. .                    356
  46.7 Sample output, when running attachMetrics.cc (Figure 46.6) with the XML inputs
       in Figures 46.4–46.5. Here, we only show the output sent to standard output (i.e.,
       cout and not cerr). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .         357
  46.8 Sample PDF showing attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . .              357

  47.1 Example source code used as input to program in codes used in this chapter. . . 360
  47.2 Example source code showing how to instrument using Tau. . . . . . . . . . . . 361
  47.3 Output of input code using tauInstrumenter.C . . . . . . . . . . . . . . . . . . . 362

  48.1 Haskell version of identity translator. . . . . . . . . . . . . . . . . . . . . . . . . . 363
  48.2 Haskell version of constant folding transformation. . . . . . . . . . . . . . . . . . 365

  49.1 Example source showing the shared-memory parallel execution of traversals. . . . 370
  49.2 Output of input file to the shared-memory parallel traversals. Output may be
       garbled depending on the multi-threaded behavior of the underlying I/O libraries. 371

  50.1 Example source demonstrating the use of the distributed-memory parallel analysis
       framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
  50.2 Example output of a distributed-memory analysis running on four processors. . . 376

  52.1 Example source code showing reduction recognition. . . . . . . . . . . . . . . . . 379
  52.2 Example source code used as input to loop reduction recognition processor. . . . 380
  52.3 Output of input to reduction recognition processor. . . . . . . . . . . . . . . . . . 380
xx   LIST OF FIGURES
Chapter 1

Introduction

1.1     What is ROSE
ROSE is an open source compiler infrastructure for building tools that can read and write source
code in multiple languages (C/C++/Fortran) and/or analyze binary executables (using the x86,
Power-PC, and ARM instruction sets). The target audience for ROSE is people building tools
for the analysis and transformation of software generally as well as code generation tools. ROSE
provides a library (librose) that can be used to support the universal requirements of tools that
do custom analysis and/or transformations on source code and custom analysis of binary forms
of software. ROSE is portable across and expanding range of operating systems and work with
an growing number of compilers.
    ROSE provides a common level of infrastructure support to user-defined tools, so that they
need not implement the complex support required for software analysis and transformation op-
erations. For source code based tools these include parsing, common forms of compiler analysis,
common transformations, and code generation. For binary analysis based tools these include
disassembly, function boundary detection, and common forms of analysis. User defined tools
may also mix source code and binary analysis to form more interesting tools for specialized
purposes. ROSE is part of research work to unify the analysis of both source code and binaries
within general compiler research and define mixed forms of static and dynamic analysis.
    ROSE works by reading the source code and/or binary and generating an Abstract Syntax
Tree (AST). The AST forms a graph representing the structure of the source code and/or
binary executable and is held in memory to provide the fastest possible means of operating on
the graph. The nodes used to define the AST graph are an intermediate representation (IR);
common within compiler research as a way of representing the structure of software absent
syntax details (commas, semi-colons, white-space, etc.). ROSE provides mechanisms to traverse
and manipulate the AST. Finally, in the case of source code, ROSE provides mechanisms to
regenerate source code from the AST.
    As a trivial example, if the input source code program contains a variable declaration for an
integer, all of this information will be available in the AST generated from the input code passed
on the command line to any tool built using ROSE. Similarly, an automated transformation of
the variable declaration held in the AST would be expressed using a traversal over the AST

                                                1
2                                                              CHAPTER 1. INTRODUCTION

and code semantic actions to mutate the AST. Then the transformed source code would be
generated (unparsed) from the AST. In the case of binaries (including executables, object files,
and libraries), the AST will represent the structure of the binary. The AST for a binary also
includes the binary file format (symbol table, debug format, import tables, etc.), disassembled
instructions, all instruction operands, etc.
    ROSE provides a rich set of tools to support the analysis of software including the support for
users to build their own forms of analysis and specialized transformations. As an example, ROSE
includes a full OpenMP compiler built using the internal ROSE infrastructure for analysis and
transformation. A wide assortment of AST traversals are provided to express both analysis and
transformations of the AST. A set of common forms of analysis are provided (call graph, control
flow, etc.) most work uniformly on both source code and binary executables. Visualization
support in included to help users understand and debug their tools. GUI support is available
to support building professional level tools using ROSE. ROSE is actively supported by a small
group at LLNL and is used as a basis for compiler research work within DOE at LLNL.
    Technically, ROSE is designed to build what are called translators, ROSE uses a source-to-
source approach to define such translators. Note that translators are significantly more sophis-
ticated than preprocessors but the terms are frequently confused. A translator must understand
the source code at a fundamentally deeper level using a grammar for the whole language and on
the whole source code, where as a preprocessor only understands the source code using a simpler
grammar and on a subset of the source code. It is loosely the difference between any language
compiler and the C preprocessor (cpp).


1.2     Why you should be interested in ROSE
ROSE is a tool for building source-to-source translators. You should be interested in ROSE
if you want to understand or improve any aspect of your software. ROSE makes it easy to
build tools that read and operate on source code from large scale applications (millions of lines).
Whole projects may be analyzed and even optimized using tools built using ROSE. For example,
ROSE is itself analyzed nightly using ROSE.
    To get started immediately consult the ROSE User Manual, chapter Getting Started for
details).


1.3     Problems that ROSE can address
ROSE is a mechanism to build source-to-source analysis or optimization tools that operate
directly on the source code of large scale applications. Example tools that have been built
include:
    • OpenMP translator,
    • Array class abstraction optimizer,
    • Source-to-source instrumenter,
    • Loop analyzer,
    • Symbolic complexity analyzer,
1.4. EXAMPLES IN THIS ROSE TUTORIAL                                                           3

   • Inliner and outliner,
   • Code coverage tools,
   • and many more...
Example tools that can be built include:
   • Custom optimization tools,
   • Custom documentation generators,
   • Custom analysis tools,
   • Code pattern recognition tools,
   • Security analysis tools,
   • and many more...


1.4     Examples in this ROSE Tutorial
This tutorial lays out a set of progressively complex example programs (located in <ROSE SOURCE>/tutorial/*)
that serve as a tutorial for the use of ROSE. Translators built using ROSE can either just ana-
lyze (and output results) or compile the input programs just like a compiler (generating object
files or executables). Many of the examples in this tutorial just do simple analysis of the input
source code, and a few show the full compilation of the input source code. Where the trans-
lators generate either object files of executables, the vendor’s compiler is used to compile the
final ROSE-generated code. Within ROSE, the call to generate source code from the AST and
call the vendor’s compiler is referred to as the backend processing. The specification of the ven-
dor’s compiler as a backend is done within the configuration step within ROSE (see options for
configure in the ROSE User Manual).
    Within the example programs below, the user can provide alternative input programs for
more complex evaluation of the tutorial examples and ROSE. The end of the chapter, section 1.7,
shows the makefiles required to compile the tutorial programs using an installed version of ROSE
(compiled using make install). This example makefile is run as part of the testing using the
make installcheck rule.
    Chapters are organized in topics including simple ROSE AST visualization, dealing with
complex data types, program analysis, program transformation and optimization, correctness
checking, binary support, interacting with other tools, and parallelism. We hope readers can
easily find the information they want.
    Specific chapters in this tutorial include:                                                     FIXME: We should constantly
                                                                                                                    update this
   • Introduction

       1. Introduction (this chapter)
       2. Problems that ROSE can address
       3. Getting Started
          This chapter covers where to find ROSE documentation and how to install ROSE.
4                                                            CHAPTER 1. INTRODUCTION

       4. Example Makefiles demonstrating the command lines to compile and link the example
          translators in this tutorial are found in <ROSE Compile Tree>/tutorial/exampleMakefile.

    • Working with the ROSE AST:

       1. Identity Translator
          This example translator reads a C or C++ application, builds the AST internally, gen-
          erates source code from the AST (unparsing), and calls the backend vendor compiler
          to compile the generated C or C++ application code. Thus the translator acts like
          and can be used to replace any compiler since it takes in source code and outputs an
          object code (or executable). This example also shows that the output generated from
          and ROSE translator is a close reproduction of the input; preserving all comments,
          preprocessor control structure, and most formating.
       2. Scopes of Declarations (scopeInformation.C)
          This example shows the scopes represented by different IR nodes in the AST.
       3. AST Graph Generator
          This translator reads a C or C++ application code and builds the AST, internally.
          The translator does not regenerate code from the AST and so does not call the backend
          vendor’s compiler. This shows how simple it could be to build source code analysis
          tools; the code calls an internal ROSE function to generate a dot graph of the AST,
          the makefile has the details of converting the dot graph into a postscript file (also
          shown).
       4. AST PDF Generator
          This translator reads an C or C++ application code builds the AST internally. The
          translator does not regenerate code from the AST and so does not call the backend
          vendor’s compiler. This shows how simple it could be to build source code analysis
          tools, the code calls an internal ROSE function to generate a pdf file with bookmarks
          representing the AST. The pdf file show as output is in this case a previously generated
          figure of a screen shot obtained by viewing the output pdf file using acroread.
       5. Introduction to AST Traversals and Attributes
          This collection of examples show the use of the simple visitor pattern for the traver-
          sal of the AST within ROSE. The simple visitor pattern permits operations to be
          programmed which will be invoked on different nodes within the AST. To handle
          communication of context information down into the AST and permit communica-
          tion of analysis information up the AST, we have provided inherited and synthesized
          attributes (respectively). Note that an AST is most often represented as a tree with
          extra edges and with shared IR nodes that make the full graph (representing all edges)
          not a tree. We present two styles of traversal, one over the tree representing the AST
          (which excludes some types of IR nodes) and one over the full AST with all extra
          nodes and shared nodes. Extra nodes are nodes such as SgType and SgSymbol IR
          nodes.
          (a) AST traversals
              These traversals visit each node of the tree embedded within the AST (excluding
              shared SgType and SgSymbol IR nodes). These traversals visit the IR nodes is
1.4. EXAMPLES IN THIS ROSE TUTORIAL                                                         5

           an order dependent upon the structure of the AST (the source code from which
           the AST is built).
              i. Classic Object-Oriented Visitor Patterns
                 This example, classicObjectOrientedVisitorPatternMemoryPoolTraversal.C,
                 show the use of a classic visitor patterns. At the moment this example uses
                 the AST’s memory pools as a basis but it is identical to a future traversal.
                 The ROSE visitor Pattern (below) is generally more useful. The classic visitor
                 pattern traversals are provided for completeness.
             ii. Visitor Traversal (visitorTraversal.C)
                 Conventional visitor patterns without no attributes. This pattern can ex-
                 plicitly access global variables to provide the effect of accumulator attributes
                 (using static data members we later show the handling of accumulator at-
                 tributes).
           iii. Inherited Attributes (inheritedAttributeTraversal.C)
                 Inherited attributes are used to communicate the context of any location
                 within the AST in terms of other parent AST nodes.
            iv. Synthesized Attributes (synthesizedAttributeTraversal.C)
                 Synthesized attributes are used to pass analysis results from the leaves of the
                 AST to the parents (all the way to the root of the AST if required).
             v. Accumulator Attributes (accumulatorAttributeTraversal.C)
                 Accumulator attributes permit the interaction of data within inherited at-
                 tributes with data in synthesized attributes. In our example program we will
                 show the use of accumulator attributes implemented as static data members.
                 Accumulator attributes are a fancy name for what is essentially global vari-
                 ables (or equivalently a data structure passed by reference to all the IR nodes
                 in the AST).
            vi. Inherited and Synthesized Attributes (inheritedAndSynthesizedAttributeTraver-
                 sal.C)
                 The combination of using inherited and synthesized attributes permits more
                 complex analysis and is often required to compute analysis results on the AST
                 within a specific context (e.g. number of loop nests of specific depth).
           vii. Persistent Attributes (persistantAttributes.C)
                 Persistent attributes may be added the AST for access to stored results for
                 later traversals of the AST. The user controls the lifetime of these persistent
                 attributes.
          viii. Nested traversals
                 Complex operations upon the AST can require many subordinate operations.
                 Such subordinate operations can be accommodated using nested traversals.
                 All traversals can operate on any subtree of the AST, and may even be nested
                 arbitrarily. Interestingly, ROSE traversals may also be applied recursively
                 (though care should be take using recursive traversals using accumulator at-
                 tributes to avoid over accumulation).
       (b) Memory Pool traversals
           These traversals visit all IR nodes (including shared IR nodes such as SgTypes and
           SgSymbols). By design this traversal can visit ALL IR nodes without the worry
6                                                              CHAPTER 1. INTRODUCTION

              of getting into cycles. These traversals are mostly useful for building specialized
              tools that operate on the AST.
                i. Visit Traversal on Memory Pools
                   This is a similar traversal as to the Visitor Traversal over the tree in the AST.
               ii. Classic Object-Oriented Visitor Pattern on Memory Pools
                   This is similar to the Classic Object-Oriented Visitor Pattern on the AST.
              iii. IR node Type Traversal on Memory Pools
                   This is a specialized traversal which visits each type of IR node, but one one
                   of each type of IR nodes. This specialized traversal is useful for building tools
                   that call static member functions on each type or IR node. A number of
                   memory based tools for ROSE are built using this traversal.
       6. AST Query Library
          This example translator shows the use of the AST query library to generate a list of
          function declarations for any input program (and output the list of function names).
          It can be trivially modified to return a list of any IR node type (C or C++ language
          construct).
       7. Symbol Table Handling (symbolTableHandling.C)
          This example shows how to use the symbol tables held within the AST for each scope.
       8. AST File I/O (astFileIO GenerateBinaryFile.C)
          This example demonstrates the file I/O for AST. This is part of ROSE support for
          whole program analysis.
       9. Debugging Tips
          There are numerous methods ROSE provides to help debug the development of spe-
          cialized source-to-source translators. This section shows some of the techniques for
          getting information from IR nodes and displaying it. Show how to use the PDF
          generator for AST’s. This section may contain several subsections.
          (a) Generating the code representing any IR node
          (b) Displaying the source code position of any IR node
    • Complex Types
       1. Getting the type parameters in function declaration (functionParameterTypes.C)
          This example translator builds a list to record the types used in each function. It
          shows an example of the sort of type information present within the AST. ROSE
          specifically maintains all type information.
       2. Resolving overloaded functions (resolvingOverloadedFunctions.C – C++ specific)
          The AST has all type information pre-evaluated, particularly important for C++ ap-
          plications where type resolution is required for determining function invocation. This
          example translator builds a list of functions called within each function, showing that
          overloaded function are fully resolved within the AST. Thus the user is not required
          to compute the type resolution required to identify which over loaded functions are
          called.
       3. Getting template parameters to a templated class (templateParameters.C – C++
          specific)
1.4. EXAMPLES IN THIS ROSE TUTORIAL                                                          7

        All template information is saved within the AST. Templated classes and functions are
        separately instantiated as specializations, as such they can be transformed separately
        depending upon their template values. This example code shows the template types
        used the instantiate a specific templated class.

  • Program Analysis

     1. Recognizing loops within applications (loopRecognition.C)
        This example program shows the use of inherited and synthesized attributes form
        a list of loop nests and report their depth. The inherited attributes are required
        to record when the traversal is within outer loop and the synthesized attributes are
        required to pass the list of loop nests back up of the AST.
     2. Generating a CFG (buildCFG.C)
        This example shows the generation of a control flow graph within ROSE. The example
        is intended to be simple. Many other graphs can be built, we need to show them as
        well.
     3. Generating a CG (buildCallGraph.C)
        This example shows the generation of a call graph within ROSE.
     4. Generating a CH (classHierarchyGraph.C)
        This example shows the generation of a class hierarchy graph within ROSE.
     5. Building custom graphs of program information
        The mechanisms used internally to build different graphs of program data is also made
        externally available. This section shows how new graphs of program information can
        be built or existing graphs customized.
     6. Database Support (dataBaseUsage.C)
        This example shows how to use the optional (see configure --help) SQLite database
        to hold persistent program analysis results across the compilation of multiple files.
        This mechanism may become less critical as the only mechanism to support global
        analysis once we can support whole program analysis more generally within ROSE.

  • Program Transformations and Optimizations

     1. Generating Unique Names for Declarations (generatingUniqueNamesFromDeclara-
        tion.C)
        A recurring issue in the development of many tools and program analysis is the repre-
        sentation of unique strings from language constructs (functions, variable declarations,
        etc.). This example demonstrated support in ROSE for the generation of unique
        names. Names are unique across different ROSE tools and compilation of different
        files.
     2. Command-line processing
        ROSE includes mechanism to simplify the processing of command-line arguments so
        that translators using ROSE can trivially replace compilers within makefiles. This
        example shows some of the many command-line handling options within ROSE and
        the ways in which customized options may be added.
        (a) Recognizing custom command-line options
                           8                                                             CHAPTER 1. INTRODUCTION

                                      (b) Adding options to internal ROSE command-line driven mechanisms
                                   3. Tailoring the code generation format: how to indent the generated source code and
                                      others.
                                   4. AST construction: how to build AST pieces from scratch and attach them to the
                                      existing AST tree.
                                      (a) Adding a variable declaration (addingVariableDeclaration.C)
                                          Here we show how to add a variable declaration to the input application. Perhaps
                                          we should show this in two ways to make it clear. This is a particularly simple use
                                          of the AST IR nodes to build an AST fragment and add it to the application’s
                                          AST.
                                      (b) Adding a function (addingFunctionDeclaration.C)
                                          This example program shows the addition of a new function to the global scope.
                                          This example is a bit more involved than the previous example.
                                      (c) Simple Instrumentor Translator (simpleInstrumentor.C)
                                          This example modifies an input application to place new code at the top and
                                          bottom of each block. The output is show with the instrumentation in place in
                                          the generated code.
                                      (d) Other examples for creating expressions, structures and so on.
                                   5. Handling source comments, preprocessor directives.
                                   6. Calling the inliner (inlinerExample.C)
                                      This example shows the use of the inliner mechanism within ROSE. The function to
                                      be inlined in specified and the transformation upon the AST is done to inline the
                                      function where it is called and clean up the resulting code.
                                   7. Calling the outliner (outlinerExample.C)
                                      This example shows the use of the outliner mechanism within ROSE. A segment of
                                      code is selected and a function is generated to hold the resulting code. Any required
                                      variables (including global variables) are passed through the generated function’s in-
                                      terface. The outliner is a useful part of the empirical optimization mechanisms being
                                      developed within ROSE.
                                   8. Call loop optimizer on set of loops (loopOptimization.C)
                                      This example program shows the optimization of a loop in C. This section contains
                                      several subsections each of which shows different sorts of optimizations. There are a
                                      large number of loop optimizations only two are shown here, we need to add more.
                                      (a) Optimization of Matrix Multiply
                                      (b) Loop Fusion Optimizations
                                   9. Parameterized code translation: How to use command line options and abstract han-
                                      dles to have the translations you want, the order you want, and the behaviors you
Does this tutorial still              want.
                exist?
                                  10. Program slicing (programSlicingExample.C)
                                      This example shows the interface to the program slicing mechanism within ROSE.
                                      Program slicing has been implemented to two ways within ROSE.
                               • Correctness Checking
1.4. EXAMPLES IN THIS ROSE TUTORIAL                                                             9

       1. Code Coverage Analysis (codeCoverage.C)
          Code coverage is a useful tool by itself, but is particularly useful when combined with
          automated detection of bugs in programs. This is part of work with IBM, Haifa.
       2. Bug seeding: how to purposely inject bugs into source code.

   • Binary Support

       1. Instruction semantics
       2. Binary Analysis
       3. Binary construction
       4. DWarf debug support

   • Interacting with Other Tools

       1. Abstract handles: uniform ways of specifying language constructs.
       2. ROSE-HPCT interface: How to annotate AST with performance metrics generated
          by third-party performance tools.
       3. Tau Performance Analysis Instrumentation (tauInstrumenter.C)
          Tau currently uses an automate mechanism that modified the source code text file.
          This example shows the modification of the AST and the generation of the correctly
          instrumented files (which can otherwise be a problem when macros are used). This is
          part of collaborations with the Tau project.
       4. The Haskell interface: interacting with a function programming language.

   • Parallelism

       1. Shared-memory parallel traversals
       2. Distributed-memory parallel traversals
       3. Parallel checker
       4. Reduction variable recognition

    Other examples included come specifically from external collaborations and are more prac-
tically oriented. Each is useful as an example because each solves a specific technical problem.
More of these will be included over time.                                                       FIXME: The following tutorials
                                                                                                                no longer exist?
  1. Fortran promotion of constants to double precision (typeTransformation.C)
     Fortran constants are by default singe precision, and must be modified to be double preci-
     sion. This is a common problem in older Fortran applications. This is part of collaborations
     with LANL to eventually automatically update/modify older Fortran applications.

  2. Automated Runtime Library Support (charmSupport.C)
     Getting research runtime libraries into use within large scale applications requires automate
     mechanism to make minor changes to large amounts of code. This is part of collaborations
     with the Charm++ team (UIUC).
                          10                                                            CHAPTER 1. INTRODUCTION

                                  (a) Shared Threaded Variable Detection Instrumentation (interveneAtVariables.C)
                                      Instrumentation support for variables, required to support detection of threaded bugs
                                      in applications.
                                  (b) Automated Modification of Function Parameters (changeFunction.C)
                                      This example program addresses a common problem where an applications function
                                      must be modified to include additional information. In this case each function in
                                      a threaded library is modified to include additional information to a corresponding
                                      wrapper library which instruments the library’s use.
Add make installcheck
e.am to build example
ors using the installed
             libraries.   1.5       ROSE Documentation and Where To Find It
                          There are three forms of documentation for ROSE, and also a ROSE web Page and email lists.
                          For more detailed information on getting started, see the ROSE User Manual, chapter Getting
                          Started for more details).

                               1. ROSE User Manual
                                  The User Manual presents how to get started with ROSE and documents features of the
                                  ROSE infrastructure. The User Manual is found in ROSE/docs/Rose directory, or at:
                                  ROSE User Manual (pdf version, relative link)

                               2. ROSE Tutorial
                                  The ROSE Tutorial presents a collection of examples of how to use ROSE (found in the
                                  ROSE/tutorial directory). The ROSE Tutorial documentation is found in ROSE/doc-
                                  s/Rose/Tutorial directory. The tutorial documentation is built in the following steps:

                                  (a) actual source code for each example translator in the ROSE/tutorial directory is
                                      included into the tutorial documentation
                                  (b) each example is compiled
                                  (c) inputs to the examples are taken from the ROSE/tutorial directory
                                  (d) output generated from running each example is placed into the tutorial documentation

                                 Thus the ROSE/tutorial directory contains the exact examples in the tutorial and each
                                 example may be modified (changing either the example translators or the inputs to the
                                 examples). The ROSE Tutorial can also be found in the ROSE/docs/Rose/Tutorial di-
                                 rectory (the LaTeX document; ps or pdf file)
                                 : ROSE Tutorial (pdf version, relative link),

                               3. ROSE HTML Reference: Intermediate Representation (IR) documentation
                                  This web documentation presents the detail interfaces for each IR nodes (documentation
                                  generated by Doxygen). The HTML IR documentation is found in ROSE/docs/Rose di-
                                  rectory (available as html only):
                                  ROSE HTML Reference (relative link)

                               4. ROSE Web Page
                                  The ROSE web pages are located at: http://www.rosecompiler.org
1.6. USING THE TUTORIAL                                                                    11

  5. ROSE Email List
     The ROSE project maintains an external mailing list (see information at: www.roseCompiler.org
     and click on the Mailing Lists link for how to join).


1.6     Using the Tutorial
First install ROSE (see ROSE User Manual, chapter Getting Started for details). Within the
ROSE distribution at the top level is the tutorial directory. All of the examples in this
documentation are represented there with Makefiles and sample input codes to the example
translators.


1.7     Required Makefile for Tutorial Examples
                                                                                                 FIXME: The exampleMakefile
This section shows an example makefile 1.1 required for the compilation of many of the tutorial    needs to be modified to include
example programs using the installed libraries (assumed to be generated from make install).      the support for compiling all of
                                                                                                           the tutorial examples.
The build process can be tested by running make installcheck from within the ROSE compile
tree. This makefile can be found in the compile tree (not the source tree) for ROSE in the
tutorial directory.
 12                                                                         CHAPTER 1. INTRODUCTION


 1    #   Example Makefile for ROSE users
 2    #   This makefile is provided as an example of how to use ROSE when ROSE is
 3    #   installed (using "make install"). This makefile is tested as part of the
 4    #   "make distcheck" rule (run as part of tests before any SVN checkin).
 5    #   The test of this makefile can also be run by using the "make installcheck"
 6    #   rule (run as part of "make distcheck").
 7
 8
 9    # Location of include directory after "make install"
10    ROSE_INCLUDE_DIR = /home/hudson-rose/.hudson/tempInstall/include
11
12    # Location of Boost include directory
13    BOOST_CPPFLAGS = -pthread -I/export/tmp.hudson-rose/opt/boost_1_40_0-inst/include
14
15    # Location of Dwarf include and lib (if ROSE is configured to use Dwarf)
16    ROSE_DWARF_INCLUDES =
17    ROSE_DWARF_LIBS_WITH_PATH =
18
19    # Location of library directory after "make install"
20    ROSE_LIB_DIR = /home/hudson-rose/.hudson/tempInstall/lib
21
22    CC                      = gcc
23    CXX                     = g++
24    CPPFLAGS                =
25    #CXXCPPFLAGS              = @CXXCPPFLAGS@
26    CXXFLAGS                = -g -Wall
27    LDFLAGS                 =
28
29    ROSE_LIBS = $(ROSE_LIB_DIR)/librose.la
30
31    # Location of source code
32    ROSE_SOURCE_DIR = \
33       ../../tutorial
34
35    executableFiles = identityTranslator ASTGraphGenerator \
36                   visitorTraversal inheritedAttributeTraversal \
37                   synthesizedAttributeTraversal \
38                   inheritedAndSynthesizedAttributeTraversal \
39                   accumulatorAttributeTraversal persistantAttributes \
40                   queryLibraryExample nestedTraversal \
41                   loopRecognition \
42                   typeInfoFromFunctionParameters \
43                   resolveOverloadedFunction templateParameter \
44                   instrumentationExample addVariableDeclaration \
45                   addFunctionDeclaration loopOptimization \
46                   buildCFG debuggingIRnodeToString \
47                   debuggingSourceCodePositionInformation \
48                   commandlineProcessing \
49                   loopNestingInfoProcessing
50
51    # Default make rule to use
52    all: $(executableFiles)
53            @if [ x$${ROSE_IN_BUILD_TREE:+present} = xpresent ]; then echo "ROSE_IN_BUILD_TREE should not be set" >&2; exit 1; fi
54
55    # Example of how to use ROSE (linking to dynamic library, which is must faster
56    # and smaller than linking to static libraries). Dynamic linking requires the
57    # use of the "-L$(ROSE_LIB_DIR) -Wl,-rpath" syntax if the LD_LIBRARY_PATH is not
58    # modified to use ROSE_LIB_DIR. We provide two example of this; one using only
59    # the "-lrose -ledg" libraries, and one using the many separate ROSE libraries.
60    $(executableFiles):
61    #       g++ -I$(ROSE_INCLUDE_DIR) -o $@ $(ROSE_SOURCE_DIR)/$@.C -L$(ROSE_LIB_DIR) -Wl,-rpath $(ROSE_LIB_DIR) $(ROSE_LIBS)
62    #       g++ -I$(ROSE_INCLUDE_DIR) -o $@ $(ROSE_SOURCE_DIR)/$@.C $(LIBS_WITH_RPATH) $(ROSE_LIBS)
63    #       /bin/sh ..//libtool --mode=link $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -I$(ROSE_INCLUDE_DIR) $(BOOST_CPPFLAGS) -o $@ $(ROSE_SOU
64            /bin/sh ..//libtool --mode=link $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -I$(ROSE_INCLUDE_DIR) $(BOOST_CPPFLAGS) $(ROSE_DWARF_INC
65
66


 Figure 1.1: Example Makefile showing how to use an installed version of ROSE (generated by
 make install).
                                   Part I

     Working with the ROSE AST


Getting familiar with the ROSE AST is the basis for any advanced usage of ROSE.
This part of tutorial collects examples for AST visualization, traversal, query, and
                                    debugging.




                                        13
Chapter 2

Identity Translator

What To Learn From This Example This example shows a trivial ROSE translator which
does not transformation, but effectively wraps the the backend vendor compiler in an extra layer
of indirection.


    Using the input code in Figure 2.2 we show a translator which builds the AST (calling
frontend()), generates the source code from the AST, and compiles the generated code using
the backend vendor compiler1 . Figure 2.1 shows the source code for this translator. The AST
graph is generated by the call to the frontend() using the standard argc and argv parameters
from the C/C++ main() function. In this example code, the variable project represents the
root of the AST2 . The source code also shows what is an optional call to check the integrity of
the AST (calling function AstTests::runAllTests()); this function has no side-effects on the
AST. The source code generation and compilation to generate the object file or executable are
done within the call to backend().
    The identity translator (identityTranslator ) is probably the simplest translator built using
ROSE. It is built by default and can be found in
ROSE BUILD/exampleTranslators/documentedExamples/simpleTranslatorExamples or
ROSE INSTALL/bin. It is often used to test if ROSE can compile input applications. Typing
identityTranslator –help will give you more information about how to use the translator.
    Figure 2.3 shows the generated code from the processing of the identityTranslator build
using ROSE and using the input file shown in figure 2.2. This example also shows that the
output generated from and ROSE translator is a close reproduction of the input; preserving
all comments, preprocessor control structure, and most formating. Note that all macros are
expanded in the generated code.
    In this trivial case of a program in a single file, the translator compiles the application to
build an executable (since -c was not specified on the command-line).


   1 Note: that the backend vendor compiler is selected at configuration time.
   2 The AST is technically a tree with additional attributes that are represented by edges and additional nodes,
so the AST is a tree and the AST with attributes is a more general graph containing edges that would make it
technically not a tree.


                                                       15
     16                                                                         CHAPTER 2. IDENTITY TRANSLATOR




 1   // Example ROSE T r a n s l a t o r : u s e d f o r        t e s t i n g ROSE i n f r a s t r u c t u r e
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 6        {
 7       // B u i l d t h e AST u s e d by ROSE
 8          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
 9
10        // Run i n t e r n a l c o n s i s t e n c y t e s t s on AST
11           AstTests : : runAllTests ( p r o j e c t ) ;
12
13        // I n s e r t your own m a n i p u l a t i o n o f t h e AST h e r e . . .
14
15        // G e n e r a t e s o u r c e c o d e from AST and c a l l       t h e vendor ’ s c o m p i l e r
16           r e t u r n backend ( p r o j e c t ) ;
17         }



     Figure 2.1: Source code for translator to read an input program and generate an object code
     (with no translation).




 1   // Example i n p u t f i l e        f o r ROSE t u t o r i a l
 2   #i n c l u d e <i o s t r e a m >
 3
 4   typedef       f l o a t R ea l ;
 5
 6   // Main f u n c t i o n
 7   i n t main ( )
 8        {
 9          int x = 0;
10          bool value = f a l s e ;
11
12        // f o r l o o p
13           f o r ( i n t i =0; i < 4 ;       i ++)
14                {
15                   int x ;
16                }
17
18             return 0;
19         }



                         Figure 2.2: Example source code used as input to identity translator.
                                                                                              17




 1   // Example i n p u t f i l e        f o r ROSE t u t o r i a l
 2   #i n c l u d e <i o s t r e a m >
 3   t y p e d e f f l o a t Real ;
 4   // Main f u n c t i o n
 5
 6   i n t main ( )
 7   {
 8       int x = 0;
 9       bool value = f a l s e ;
10   // f o r l o o p
11       for ( int i = 0; i < 4;             i ++) {
12         int x ;
13       }
14       return 0;
15   }



     Figure 2.3: Generated code, from ROSE identity translator, sent to the backend (vendor) com-
     piler.
18   CHAPTER 2. IDENTITY TRANSLATOR
Chapter 3

Simple AST Graph Generator

What To Learn From This Example This example shows how to generate a DOT file to
visualize a simplified AST from any input program.


    DOT is a graphics file format from the AT&T GraphViz project used to visualize moderate
sized graphs. It is one of the visualization tools used within the ROSE project. More inforamtion
can be readily found at www.graphviz.org/. We have found the zgrviewer to be an especially
useful program for visualizing graphs generated in the DOT file format (see chapter 5 for more
information on zgrviewer).
    Each node of the graph in figure 3.3 shows a node of the Intermediate Representation (IR);
the graphs demonstrates that the AST is formally a tree. Each edge shows the connection of the
IR nodes in memory. The generated graph shows the connection of different IR nodes that form
the AST for an input program source code. Binary executables can similarly be vizualized using
DOT files. The generation of such graphs is appropriate for small input programs, chapter 6
shows a mechanism using PDF files that is more appropriate to larger programs (e.g. 100K lines
of code). More information about generation of specialized AST graphs can be found in 5 and
custom graph generation in 27.
    Note that a similar utility program named dotGenerator already exists within ROSE/exam-
pleTranslators/DOTGenerator. It is also installed to ROSE INS/bin.
    The program in figure 3.1 calls an internal ROSE function that traverses the AST and
generates an ASCII file in dot format. Figure 3.2 shows an input code which is processed to
generate a graph of the AST, generating a dot file. The dot file is then processed using dot to
generate a postscript file 3.3 (within the Makefile). Figure 3.3 (../../..//tutorial/test.ps) can
be found in the compile tree (in the tutorial directory) and viewed directly using ghostview or
any postscript viewer to see more detail.
    Figure 3.3 displays the individual C++ nodes in ROSE’s intermediate representation (IR).
Each circle represents a single IR node, the name of the C++ construct appears in the center of
the circle, with the edge numbers of the traversal on top and the number of child nodes appearing
below. Internal processing to build the graph generates unique values for each IR node, a pointer
address, which is displays at the bottom of each circle. The IR nodes are connected for form a
tree, and abstract syntax tree (AST). Each IR node is a C++ class, see SAGE III reference for

                                               19
                           20                                                         CHAPTER 3. SIMPLE AST GRAPH GENERATOR


                     1     // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
                     2
                     3     #i n c l u d e ” r o s e . h”
                     4
                     5     i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
                     6          {
                     7         // B u i l d t h e AST u s e d by ROSE
                     8            SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
                     9
                    10          // G e n e r a t e a DOT f i l e t o u s e i n      v i s u a l i z i n g t h e AST graph .
                    11             generateDOT ( ∗ p r o j e c t ) ;
                    12
                    13                return 0;
                    14           }



                                 Figure 3.1: Example source code to read an input program and generate an AST graph.

                     1
                     2     // Templated c l a s s d e c l a r a t i o n u s e d i n t e m p l a t e p a r a m e t e r example c o d e
                     3     t e m p l a t e <typename T>
                     4     c l a s s templateClass
                     5           {
                     6               public :
                     7                      int x ;
                     8
                     9                      void foo ( i n t ) ;
                    10                      void foo ( double ) ;
                    11           };
                    12
                    13     // O v e r l o a d e d f u n c t i o n s   for   t e s t i n g overloaded function       resolution
                    14     void foo ( i n t ) ;
                    15     void foo ( double )
                    16        {
                    17           int x = 1;
                    18           int y ;
                    19
                    20          // Added t o a l l o w non− t r i v i a l CFG
                    21             i f (x)
                    22                 y = 2;
                    23             else
                    24                 y = 3;
                    25           }



                                           Figure 3.2: Example source code used as input to generate the AST graph.


                       details, the edges represent the values of data members in the class (pointers which connect the
                       IR nodes to other IR nodes). The edges are labeled with the names of the data members in the
 Use this first example classes representing the IR nodes.
 e to explain the use of
ader files (config.h and
 d the code to build the
       SgProject object.
                                                                                                                                                                                                                                                                            21




                                             0:57
                                          SgSourceFile
                                               1
                                        0x2b46e0903010


                                                 globalScope


                                             1:56
                                           SgGlobal
                                             424
                                        0x2b46e095f010


                                  *[421]         *[422]             *[423]



                                               4:9                                           10:55
         2:3
                                      SgFunctionDeclaration                          SgFunctionDeclaration
SgTemplateDeclaration
                                               foo                                            foo
     !isForward
                                           isForward                                      !isForward
          0
                                                3                                              3
  0x2b46e0c0b2a0
                                        0x2b46e0a68be0                                 0x2b46e0a68f70



                           parameterList decoratorList definition                       parameterList decoratorList    definition



           5:8                                                               11:14
                                                                                                                                   15:54
 SgFunctionParameterList                                            SgFunctionParameterList
                                                                                                                            SgFunctionDefinition
       !isForward                                                         !isForward
                                                                                                                                     1
            1                                                                  1
                                                                                                                             0x2b46e0cac010
    0x2b46e0b23900                                                     0x2b46e0b23b30



             *[0]                                                             *[0]                                                        body



             6:7                                                            12:13
                                                                                                                                          16:53
     SgInitializedName                                                SgInitializedName
                                                                                                                                      SgBasicBlock
                                                                                                                                            3
           1                                                                1
                                                                                                                                     0x2b46e0cf5010
     0x2b46e0ba8438                                                   0x2b46e0ba8560



             initptr                                                          initptr                                        *[0]             *[1]          *[2]



                                                                                                      17:24                                 25:28
                                                                                                                                                                             29:52
                                                                                              SgVariableDeclaration                 SgVariableDeclaration
                                                                                                                                                                           SgIfStmt
                                                                                                   !isForward                            !isForward
                                                                                                                                                                               3
                                                                                                        2                                     2
                                                                                                                                                                        0x2b46e0e5a010
                                                                                                0x2b46e0d38010                        0x2b46e0d38288



                                                                      baseTypeDefiningDeclaration *[1]                baseTypeDefiningDeclaration *[1]                       conditional      true_body            false_body



                                                                                              18:23                                         26:27
                                                                                                                                                                        30:35                         36:43                             44:51
                                                                                        SgInitializedName                             SgInitializedName
                                                                                                                                                                   SgExprStatement               SgExprStatement                   SgExprStatement
                                                                                                 x                                             y
                                                                                                                                                                          1                             1                                 1
                                                                                                 1                                             1
                                                                                                                                                                     0x1076e200                    0x1076e258                        0x1076e2b0
                                                                                        0x2b46e0ba8688                                0x2b46e0ba87b0



                                                                                                 initptr                                         initptr                expression                        expression                       expression


                                                                                             19:22                                                              31:34                                 37:42                               45:50
                                                                                        SgAssignInitializer                                                   SgCastExp                            SgAssignOp                          SgAssignOp
                                                                                               1                                                                  1                                     2                                   2
                                                                                          0x106fa3a0                                                         0x10737570                            0x10783ae0                          0x10783b50


                                                                                                 operand_i                                                            operand_i                      lhs_operand_i rhs_operand_i             lhs_operand_i rhs_operand_i



                                                                                             20:21                                                              32:33                         38:39                    40:41              46:47                  48:49
                                                                                            SgIntVal                                                         SgVarRefExp                   SgVarRefExp                SgIntVal         SgVarRefExp              SgIntVal
                                                                                            value = 1                                                          name = x                      name = y                 value = 2          name = y               value = 3
                                                                                               0                                                                  0                             0                        0                  0                      0
                                                                                          0x106e0d10                                                         0x10754b50                    0x10754bb8               0x106e0d78         0x10754c20             0x106e0de0




      Figure 3.3: AST representing the source code file: inputCode ASTGraphGenerator.C.
22   CHAPTER 3. SIMPLE AST GRAPH GENERATOR
Chapter 4

AST Whole Graph Generator

What To Learn From This Example This example shows how to generate and visualize
the AST from any input program. This view of the AST includes all additional IR nodes and
edges that form attributes to the AST, as a result this graph is not a tree. These graphs are
more complex but show significantly more detail about the AST and its additional edges and
attributes. Each node of the graph in figure ?? shows a node of the Intermediate Representation
(IR). Each edge shows the connection of the IR nodes in memory. The generated graph shows
the connection of different IR nodes to form the AST and its additional attributes (e.g types,
modifiers, etc). The generation of such graphs is appropriate for very small input programs,
chapter 6 shows a mechanism using PDF files that is more appropriate to larger programs (e.g.
100K lines of code). More information about generation of specialized AST graphs can be found
in 5 and custom graph generation in 27.
    Again, a utility program, called dotGeneratorWholeASTGraph is provided within ROSE to
generate detailed dot graph for input code. It is available from ROSE BUILD/exampleTranslators/DOTGenerator
or ROSE INS/bin. A set of options is available to further customize what types of AST nodes
to be shown or hidden. Please consult the screen output of dotGeneratorWholeASTGraph -help
for details.
    Viewing these dot files is best done using: zgrviewer at http://zvtm.sourceforge.net/zgrviewer.html.
This tool permits zooming in and out and viewing isolated parts of even very large graphs.
Zgrviewer permits a more natural way of understanding the AST and its additional IR nodes
than the pdf file displayed in these pages. The few lines of code used to generate the graphs can
be used on any input code to better understand how the AST represents different languages and
their constructs.
    The program in figure 4.1 calls an internal ROSE function that traverses the AST and gener-
ates an ASCII file in dot format. Figure ?? shows an tiny input code which is processed to gen-
erate a graph of the AST with its attributes, generating a dot file. The dot file is then processed
using dot to generate a pdf file 4.3 (within the Makefile). Note that a similar utility program
already exists within ROSE/exampleTranslators (and includes a utility to output an alternative
PDF representation (suitable for larger ASTs) as well). Figure ?? (../../..//tutorial/test.ps) can
be found in the compile tree (in the tutorial directory) and viewed directly using any pdf or dot
viewer to see more detail (zgrviewer working with the dot file directly is strongly advised).
    Note that AST’s can get very large, and that the additional IR nodes required to represent

                                            23
     24                                                            CHAPTER 4. AST WHOLE GRAPH GENERATOR


 1   // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 6        {
 7       // B u i l d t h e AST u s e d by ROSE
 8          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
 9
10        //   B u i l d t h e DOT f i l e t o v i s u a l i z e t h e AST w i t h a t t r i b u t e s ( t y p e s , symbols ,         etc . ) .
11        //   To p r o t e c t a g a i n s t b u i l d i n g g r a p h s t h a t a r e t o o l a r g e an o p t i o n i s
12        //   p r o v i d e d t o bound t h e number o f IR n o d e s f o r which a graph w i l l be
13        //   generated .             The l a y o u t o f l a r g e r g r a p h s i s p r o h i b i t i v e l y e x p e n s i v e .
14             c o n s t i n t MAX NUMBER OF IR NODES = 2 0 0 0 ;
15             g e n e r a t e A s t G r a p h ( p r o j e c t , MAX NUMBER OF IR NODES ) ;
16
17             return 0;
18         }



      Figure 4.1: Example source code to read an input program and generate a whole AST graph.


     the types, modifiers, etc, can generate visually complex graphs. ROSE contains the mechanisms
     to traverse these graphs and do analysis on them. In one case the number of IR nodes exceeded
     27 million, an analysis was done through a traversal of the graph in 10 seconds on a desktop x86
     machine (the memory requirements were 6 Gig). ROSE organizes the IR in ways that permit
     analysis of programs that can represent rather large ASTs.

 1   // T r i v i a l f u n c t i o n u s e d t o g e n e r a t e graph o f AST
 2   // w i t h a l l t y p e s and a d d i t i o n a l e d g e s shown .
 3   // Graphs o f t h i s s o r t a r e l a r g e , and can be
 4   // v i e w e d u s i n g ” z g r v i e w e r ” f o r d o t f i l e s .
 5   int foo ()
 6      {
 7           return 0;
 8      }



     Figure 4.2: Example tiny source code used as input to generate the small AST graph with
     attributes.

          Figure 4.3 displays the individual C++ nodes in ROSE’s intermediate representation (IR).
     Colors and shapes are used to represent different types or IR nodes. Although only visible using
     zgrviewer the name of the C++ construct appears in the center of each node in the graph,
     with the names of the data members in each IR node as edge labels. Unique pointer values are
     includes and printed next to the IR node name. These graphs are the single best way to develop
     an intuitive understanding how language constructs are organized in the AST. In these graphs,
     the color yellow is used for types (SgType IR nodes), the color green is used for expressions
     (SgExpression IR nodes), and statements are a number of different colors and shapes to make
     them more recognizable.
          Figure 4.5 shows a graph similar to the previous graph but larger and more complex because
     it is from a larger code. Larger graphs of this sort are still very useful in understanding how more
     significant language constructs are organized and reference each other in the AST. Tools such
     as zgrviewer are essential to reviewing and understanding these graphs. Although such graphs
25
     26                                                     CHAPTER 4. AST WHOLE GRAPH GENERATOR


 1   // L a r g e r f u n c t i o n u s e d t o g e n e r a t e graph o f AST
 2   // w i t h a l l t y p e s and a d d i t i o n a l e d g e s shown .
 3   // Graphs o f t h i s s o r t a r e l a r g e , and can be
 4   // v i e w e d u s i n g ” z g r v i e w e r ” f o r d o t f i l e s .
 5   int foo ( int x ) ;
 6
 7   int globalVar = 42;
 8
 9   void foobar A ( )
10      {
11        int a = 4;
12        int b = a + 2;
13        int c = b ∗ globalVar ;
14        int x ;
15        x = foo ( c ) ;
16        int y = x + 2;
17        int z = globalVar ∗ y ;
18      }
19
20
21
22   void foobar B ()
23      {
24        int p;
25        int i = 4;
26        i n t k = globalVar ∗ ( i +2);
27        p = foo (k ) ;
28        i n t r = ( p+2) ∗ g l o b a l V a r ;
29      }



     Figure 4.4: Example source code used as input to generate a larger AST graph with attributes.


     can be visualized, in practice this is only useful for debugging small codes in the construction
     of custom analysis and transformation tools. The graphs for real million line applications would
     never be visualized. Using ROSE one can build automated tools to operate on the AST for large
     scale applications where visualization would not be possible.
27
28   CHAPTER 4. AST WHOLE GRAPH GENERATOR
Chapter 5

Advanced AST Graph Generation
                                                                                                       FIXME: This chapter brings
What To Learn From This Example This example shows a maximally complete represen-                      more confusions. I suggest to
tation of the AST (often in more detail that is useful).                                             remove it until it is done right .
    Where chapter 3 presented a ROSE-based translator which presented the AST as a tree, this                                     -Leo
chapter presents the more general representation of the graph in which the AST is embedded.
The AST may be thought of as a subset of a more general graph or equivalently as an AST (a
tree in a formal sense) with annotations (extra edges and information), sometimes referred to as
a ‘decorated AST.
    We present tools for seeing all the IR nodes in the graph containing the AST, including all
types (SgType nodes), symbols (SgSymbol nodes), compiler generated IR nodes, and supporting
IR nodes. In general it is a specific filtering of this larger graph which is more useful to com-
municating how the AST is designed and internally connected. We use these graphs for internal
debugging (typically on small problems where the graphs are reasonable in size). The graphs
presented using these graph mechanism present all back-edges, and demonstrate what IR nodes
are shared internally (typically SgType IR nodes).
    First a few names, we will call the AST those nodes in the IR that are specified by a traversal
using the ROSE traversal (SgSimpleTraversal, etc.). We will call the graph of all IR nodes the
Graph of all IR nodes. the AST is embedded in the Graph of all IR nodes. The AST is a tree,
while the graph of all IR nodes typically not a tree (in a Graph Theory sense) since it typically
contains cycles.
    We cover the visualization of both the AST and the Graph of all IR nodes.

   • AST graph
     These techniques define ways of visualizing the AST and filtering IR nodes from being
     represented.

        – Simple AST graphs
        – Colored AST graphs
        – Filtering the graph
          The AST graph may be generated for any subtree of the AST (not possible for the
          graphs of all IR nodes). Additionally runtime options permit null pointers to be
          ignored. .                                                                                          FIXME: Is this true?


                                               29
                         30                                  CHAPTER 5. ADVANCED AST GRAPH GENERATION

                              • Graph of all IR nodes
                                These techniques define the ways of visualizing the whole graph of IR nodes and is based on
                                the memory pool traversal as a means to access all IR nodes. Even disconnected portions
                                of the AST will be presented.

                                  – Simple graphs
                                  – Colored graphs
                                  – Filtering the graph
Removed this example
 newer mechanism for
the whole AST graphs
needs to be presented.
     Chapter 6

     AST PDF Generator

     What To Learn From This Example This example demonstrates a mechanism for gener-
     ating a visualization of the AST using pdf files. A pdf file is generated and can be viewed using
     acroread. The format is suitable for much larger input programs than the example shown in
     previous chapters using dot format 4. This mechanism can support the visualization of input
     files around 100K lines of code.

 1   // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 6        {
 7       // B u i l d t h e AST u s e d by ROSE
 8          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
 9
10      // G e n e r a t e a PDF f i l e f o r i n t e r a c t i v e   e x p l o r a t i o n o f t h e AST .
11         generatePDF ( ∗ p r o j e c t ) ;
12
13            return 0;
14        }



     Figure 6.1: Example source code to read an input program and generate a PDF file to represent
     the AST.

         The program in figure 6.1 calls an internal ROSE function that traverses the AST and
     generates an ASCI file in dot format. Figure 3.2 shows an input code which is processed to
     generate a graph of the AST, generating a pdf file. The pdf file is then processed using acroread
     to generate a GUI for viewing the AST.
         A standalone utility tool, called pdfGenerator is provided within ROSE. It is available from
     ROSE BUILD/exampleTranslators/PDFGenerator or ROSE INS/bin. Users can use it to gen-
     erate AST in a pdf format from an input code.
         Figure 6.3 displays on the left hand side the individual C++ nodes in ROSE’s intermediate
     representation (IR). The page on the right hand side shows that IR nodes member data. Pointers
     in boxes can be clicked on to navigate the AST (or nodes in the tree hierarchy can be clicked on
     jump to any location in the AST. This representation shows only the IR nodes that are traversed

                                                                         31
     32                                                                        CHAPTER 6. AST PDF GENERATOR


 1
 2   // Templated c l a s s d e c l a r a t i o n u s e d i n t e m p l a t e p a r a m e t e r example c o d e
 3   t e m p l a t e <typename T>
 4   c l a s s templateClass
 5         {
 6             public :
 7                    int x ;
 8
 9                    void foo ( i n t ) ;
10                    void foo ( double ) ;
11         };
12
13   // O v e r l o a d e d f u n c t i o n s   for   t e s t i n g overloaded function   resolution
14   void foo ( i n t ) ;
15   void foo ( double )
16      {
17         int x = 1;
18         int y ;
19
20        // Added t o a l l o w non− t r i v i a l CFG
21           i f (x)
22               y = 2;
23           else
24               y = 3;
25         }



                Figure 6.2: Example source code used as input to generate the PDF file of the AST.


     by the standard traversal (no SgSymbol or SgType IR nodes are presented in this view of the
     AST).
          The output of this translator is shown in figure 6.3. The left hand side of the screen is a tree
     with click-able nodes to expand/collapse the subtrees. The right hand side of the screen is a
     description of the data at a particular node in the AST (the node where the user has clicked the
     left mouse button). This relatively simple view of the AST is useful for debugging transformation
     and finding information in the AST required by specific sorts of analysis. It is also useful for
     developing an intuitive feel for what information is in the AST, how it is organized, and where
     it is stored.
                                                                                      33




Figure 6.3: Example output from translator which outputs PDF representation of AST. The
generated PDF file makes use of the bookmark mechanism to expand and collapse parts of the
AST.
34   CHAPTER 6. AST PDF GENERATOR
Chapter 7

Introduction to AST Traversals
                                                                                                     FIXME: Add What to learn
An essential operation in the analysis and construction of ASTs is the definition of traversals       from this example paragraph
                                                                                                                  to each example.
upon the AST to gather information and modify targeted internal representation (IR) nodes.
                                                                                                           FIXME: Add What is
ROSE includes different sorts of traversals to address the different requirements of numerous              different from previous
program analysis and transformation operations. This section demonstrates the different types            example paragraph to each
of traversals that are possible using ROSE.                                                                             example.
    ROSE translators most commonly introduce transformations and analysis through a traversal          FIXME: Add a table and/or
over the AST. Alternatives could be to generate a simpler IR that is more suitable to a specific      graph at the end of this chapter
                                                                                                        to summarize the traversals.
transformation and either convert modification to that transformation specific IR into changes to
the AST or generate source code from the transformation specific IR directly. These approaches
are more complex than introducing changes to the AST directly, but may be better for specific
transformations.
    Traversals represent an essential operation on the AST and there are a number of different
types of traversals. The suggested traversals for users are explained in Section 7.2. Section 7.3
introduces specialized traversals (that traverse the AST in different orders and traverse types and
symbols), typically not appropriate for most translators (but perhaps appropriate for specialized
tools, often internal tools within ROSE).
    See the ROSE User Manual for a more complete introduction to the different types of traver-
sals. The purpose of this tutorial is to present examples, but we focus less on the background
and philosophy here than in the ROSE User Manual.
    This chapter presents a number of ways of traversing the AST of any input source code.
These traversals permit operations on the AST, which may either read or modify the AST in
place. Modifications to the AST will be reflected in the source code generated when the AST is
unparsed; the code generation phase of the source-to-source process defined by ROSE. Note that
for all examples, the input code described in section 7.1 is used to generate all outputs shown
with each translator.


7.1     Input For Example Traversals
The code shown in figure 7.1 shows the input code that will be used to demonstrate the traversals
in this chapter. It may be modified by the user to experiment with the use of the traversals on

                                               35
     36                                                        CHAPTER 7. INTRODUCTION TO AST TRAVERSALS


 1
 2   // Templated c l a s s d e c l a r a t i o n u s e d i n t e m p l a t e p a r a m e t e r example c o d e
 3   t e m p l a t e <typename T>
 4   c l a s s templateClass
 5         {
 6             public :
 7                    int x ;
 8                    void foo ( i n t ) ;
 9                    void foo ( double ) ;
10         };
11
12   // O v e r l o a d e d f u n c t i o n s    for    t e s t i n g overloaded function   resolution
13   void foo ( i n t ) ;
14
15   void foo ( double )
16      {
17        int x = 1;
18        int y ;
19
20             f o r ( i n t i =0; i < 4 ;           i ++)
21                  {
22                     int x ;
23                  }
24
25        // Added t o a l l o w non− t r i v i a l CFG
26           i f (x)
27               y = 2;
28           else
29               y = 3;
30         }
31
32   i n t main ( )
33        {
34          foo (42);
35          foo (3.14159265);
36
37             t e m p l a t e C l a s s <char> i n s t a n t i a t e d C l a s s ;
38             instantiatedClass . foo ( 7 ) ;
39             instantiatedClass . foo ( 7 . 0 ) ;
40
41             f o r ( i n t i =0; i < 4 ;           i ++)
42                  {
43                     int x ;
44                  }
45
46             return 0;
47         }



     Figure 7.1: Example source code used as input to program in traversals shown in this chapter.


     alternative input codes.


     7.2           Traversals of the AST Structure
     This collection of traversals operates on the AST in an order which matches the structure of the
     AST and the associated source code. These types of traversals are the most common traversals
     for users to use. A subsequent section of this chapter demonstrated more specialized traversals
     over all IR nodes (more than just those IR nodes in the AST representing the structure of the
     source code) that are suitable for some tools, mostly tools built internally within ROSE.
7.2. TRAVERSALS OF THE AST STRUCTURE                                                               37

    Because the traversals in this section traverse the structure of the source code (see the AST
graph presented in the first tutorial example) they are more appropriate for most transformations
of the source code. We suggest that the user focus on these traversals which represent the
interface we promote for analysis and transformation of the AST, instead of the memory pools
traversals which are suitable mostly for highly specialized internal tools. The simple traversals
of both kinds have the same interface so the user may easily switch between them with out
significant difficulty.

7.2.1     Classic Object-Oriented Visitor Pattern for the AST
We show this example first, but it is rarely used in practice, and more useful traversals follow. It is
however most closely similar to traversals that are available in other compiler infrastructures, and
so a concept with which many people will be familar. In this case because this implementation
is based on the memory pool infrstructure it will visit all node and not in any order based on the
AST. The ASTSimpleProcessing traversal in section 7.2.2 is closer to a common visitor pattern
that visits the IR nodes in the order in which they appear in the AST.
    Figure 7.2 shows the source code for a translator using the classic object-oriented visitor
pattern to traverse the AST. This visitor pattern is only implemented for the memory pool based
traversal. Thus it works on the whole of the attributed AST and does not work on a restricted
subset of the AST (e.g. a subtree of the unattributed AST). Figure 7.3 shows the output from
this traversal using the example input source from figure 7.1.

7.2.2     Simple Traversal (no attributes)
Figure 7.4 shows the source code for a translator which traverses the AST. The traversal object is
from the type visitorTraversal derived from AstSimpleProcessing. The visit() function is
required to be defined because it is defined as a pure virutal funciton in the AstSimpleProcessing
base class. The member function traverseInputFiles() of AstSimpleProcessing is called to
traverse the AST and call the visit() function on each IR node. Note that the function
traverse() (not used) would visit each IR nodes while traverseInputFiles() will only visit
those IR nodes that originate in the input source code (thus skipping all header files).
    For each node where the visit() function is called a SgNode pointer is to the node is passed
into the visit function. Note that using this simple traversal the only context information
available to the visit function is what is stored in its member variables (though access to other
nodes is possible along any edges in the attributed AST graph). The only option is to traverse
the AST in either pre-order or postorder. The atTraversalEnd() function may be defined by
the user to do final processing after all nodes have been visited (or to perform preparations
before the nodes are visited, in the case of the corresponding atTraversalStart() function).
Figure 7.5 shows the output from this traversal using the example input source from figure 7.1.

7.2.3     Simple Pre- and Postorder Traversal
Figure 7.6 shows the source code for a translator that traverses the AST without attributes
(like the one in the previous subsection), but visiting each node twice, once in preorder (before
its children) and once in postorder (after all children). Figure 7.7 shows the output from this
traversal using the example input source from figure 7.1.
     38                                                   CHAPTER 7. INTRODUCTION TO AST TRAVERSALS


 1
 2   #i n c l u d e ” r o s e . h”
 3
 4   // C l a s s i c V i s i t o r P a t t e r n i n ROSE ( implemented u s i n g t h e t r a v e r s a l o v e r
 5   // t h e e l e m e n t s s t o r e d i n t h e memory p o o l s s o i t ha s no c y c l e s and v i s i t s
 6   // ALL IR n o d e s ( i n c l u d i n g a l l S g F i l e I n f o , SgSymbols , SgTypes , and t h e
 7   // s t a t i c b u i l t i n SgTypes ) .
 8   c l a s s C l a s s i c V i s i t o r : p u b l i c ROSE VisitorPattern
 9         {
10            public :
11              // O v e r r i d e v i r t u r a l f u n c t i o n d e f i n e d i n b a s e c l a s s
12                    void v i s i t ( SgGlobal ∗ g l o b a l S c o p e )
13                           {
14                              p r i n t f ( ” Found t h e S g G l o b a l IR node \n ” ) ;
15                           }
16
17                      void v i s i t ( SgFunctionDeclaration ∗ functionDeclaration )
18                            {
19                              p r i n t f ( ” Found a S g F u n c t i o n D e c l a r a t i o n IR node \n ” ) ;
20                            }
21                      v o i d v i s i t ( SgTypeInt ∗ i n t T y p e )
22                            {
23                              p r i n t f ( ” Found a SgTypeInt IR node \n ” ) ;
24                            }
25
26                      v o i d v i s i t ( SgTypeDouble ∗ doubleType )
27                            {
28                              p r i n t f ( ” Found a SgTypeDouble IR node \n ” ) ;
29                            }
30         };
31
32
33   int
34   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
35       {
36         SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
37         ROSE ASSERT ( p r o j e c t != NULL ) ;
38
39        // C l a s s i c v i s i t o r p a t t e r n o v e r t h e memory p o o l o f IR n o d e s
40           ClassicVisitor visitor A ;
41           traverseMemoryPoolVisitorPattern ( v i s i t o r A ) ;
42
43              r e t u r n backend ( p r o j e c t ) ;
44         }



                                  Figure 7.2: Example source showing simple visitor pattern.


     7.2.4           Inherited Attributes
     Figure 7.8 shows the use of inherited attributes associated with each IR node. Within this
     traversal the attributes are managed by the traversal and exist on the stack. Thus the lifetime
     of the attributes is only as long as the processing of the IR node and its subtree. Attributes
     such as this are used to communicate context information down the AST and called Inherited
     attributes.
         In the example the class Inherited Attribute is used to represent inherited attribute.
     Each instance of the class represents an attribute value. When the AST is traversed we obtain
     as output the loop nest depth at each point in the AST. The output uses the example input
     source from figure 7.1.
         Note that inherited attributes are passed by-value down the AST. In very rare cases you
7.2. TRAVERSALS OF THE AST STRUCTURE                                                      39

might want to pass a pointer to dynamically allocated memory as an inherited attribute. In
this case you can define the virtual member function void destroyInheritedValue(SgNode
*n, InheritedAttribute inheritedValue) which is called after the last use of the inherited
attribute computed at this node, i. e. after all children have been visited. You can use this
function to free the memory allocated for this inherited attribute.
     40                                           CHAPTER 7. INTRODUCTION TO AST TRAVERSALS


 1   Found   a SgTypeInt IR node
 2   Found   a SgTypeDouble IR node
 3   Found   t h e S g G l o b a l IR node
 4   Found   a SgFunctionDeclaration         IR   node
 5   Found   a SgFunctionDeclaration         IR   node
 6   Found   a SgFunctionDeclaration         IR   node
 7   Found   a SgFunctionDeclaration         IR   node
 8   Found   a SgFunctionDeclaration         IR   node
 9   Found   a SgFunctionDeclaration         IR   node
10   Found   a SgFunctionDeclaration         IR   node
11   Found   a SgFunctionDeclaration         IR   node
12   Found   a SgFunctionDeclaration         IR   node
13   Found   a SgFunctionDeclaration         IR   node
14   Found   a SgFunctionDeclaration         IR   node
15   Found   a SgFunctionDeclaration         IR   node
16   Found   a SgFunctionDeclaration         IR   node
17   Found   a SgFunctionDeclaration         IR   node
18   Found   a SgFunctionDeclaration         IR   node
19   Found   a SgFunctionDeclaration         IR   node
20   Found   a SgFunctionDeclaration         IR   node
21   Found   a SgFunctionDeclaration         IR   node
22   Found   a SgFunctionDeclaration         IR   node
23   Found   a SgFunctionDeclaration         IR   node
24   Found   a SgFunctionDeclaration         IR   node
25   Found   a SgFunctionDeclaration         IR   node
26   Found   a SgFunctionDeclaration         IR   node
27   Found   a SgFunctionDeclaration         IR   node
28   Found   a SgFunctionDeclaration         IR   node
29   Found   a SgFunctionDeclaration         IR   node
30   Found   a SgFunctionDeclaration         IR   node
31   Found   a SgFunctionDeclaration         IR   node
32   Found   a SgFunctionDeclaration         IR   node
33   Found   a SgFunctionDeclaration         IR   node
34   Found   a SgFunctionDeclaration         IR   node
35   Found   a SgFunctionDeclaration         IR   node
36   Found   a SgFunctionDeclaration         IR   node
37   Found   a SgFunctionDeclaration         IR   node
38   Found   a SgFunctionDeclaration         IR   node
39   Found   a SgFunctionDeclaration         IR   node
40   Found   a SgFunctionDeclaration         IR   node
41   Found   a SgFunctionDeclaration         IR   node
42   Found   a SgFunctionDeclaration         IR   node
43   Found   a SgFunctionDeclaration         IR   node
44   Found   a SgFunctionDeclaration         IR   node
45   Found   a SgFunctionDeclaration         IR   node
46   Found   a SgFunctionDeclaration         IR   node
47   Found   a SgFunctionDeclaration         IR   node
48   Found   a SgFunctionDeclaration         IR   node
49   Found   a SgFunctionDeclaration         IR   node
50   Found   a SgFunctionDeclaration         IR   node
51   Found   a SgFunctionDeclaration         IR   node
52   Found   a SgFunctionDeclaration         IR   node
53   Found   a SgFunctionDeclaration         IR   node
54   Found   a SgFunctionDeclaration         IR   node
55   Found   a SgFunctionDeclaration         IR   node
56   Found   a SgFunctionDeclaration         IR   node
57   Found   a SgFunctionDeclaration         IR   node
58   Found   a SgFunctionDeclaration         IR   node
59   Found   a SgFunctionDeclaration         IR   node
60   Found   a SgFunctionDeclaration         IR   node
61   Found   a SgFunctionDeclaration         IR   node
62   Found   a SgFunctionDeclaration         IR   node
63   Found   a SgFunctionDeclaration         IR   node
64   Found   a SgFunctionDeclaration         IR   node
65   Found   a SgFunctionDeclaration         IR   node
66   Found   a SgFunctionDeclaration         IR   node
67   Found   a SgFunctionDeclaration         IR   node
68   Found   a SgFunctionDeclaration         IR   node
69   Found   a SgFunctionDeclaration         IR   node
70   Found   a SgFunctionDeclaration         IR   node
71   Found   a SgFunctionDeclaration         IR   node
72   Found   a SgFunctionDeclaration         IR   node
73   Found   a SgFunctionDeclaration         IR   node
74   Found   a SgFunctionDeclaration         IR   node
75   Found   a SgFunctionDeclaration         IR   node
76   Found   a SgFunctionDeclaration         IR   node
77   Found   a SgFunctionDeclaration         IR   node
78   Found   a SgFunctionDeclaration         IR   node
79   Found   a SgFunctionDeclaration         IR   node
80   Found   a SgFunctionDeclaration         IR   node
81   Found   a SgFunctionDeclaration         IR   node
82   Found   a SgFunctionDeclaration         IR   node
83   Found   a SgFunctionDeclaration         IR   node
     7.2. TRAVERSALS OF THE AST STRUCTURE                                                                                              41



 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an example p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   // r o s e . C : Example ( d e f a u l t ) ROSE P r e p r o c e s s o r : u s e d f o r t e s t i n g ROSE i n f r a s t r u c t u r e
 3
 4   #i n c l u d e ” r o s e . h”
 5
 6   class      visitorTraversal           :   public AstSimpleProcessing
 7      {
 8             public :
 9                  visitorTraversal ();
10                  v i r t u a l v o i d v i s i t ( SgNode∗ n ) ;
11                  v i r t u a l void atTraversalEnd ( ) ;
12        };
13
14   visitorTraversal : : visitorTraversal ()
15      {
16      }
17
18   v o i d v i s i t o r T r a v e r s a l : : v i s i t ( SgNode∗ n )
19         {
20           i f ( i s S g F o r S t a t e m e n t ( n ) != NULL)
21                 {
22                      p r i n t f ( ” Found a f o r l o o p . . . \n ” ) ;
23                 }
24         }
25
26   void v i s i t o r T r a v e r s a l : : atTraversalEnd ( )
27      {
28        p r i n t f ( ” T r a v e r s a l e n d s h e r e . \n ” ) ;
29      }
30
31   int
32   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
33       {
34         i f ( SgProject : : g e t v e r b o s e ( ) > 0)
35                p r i n t f ( ” I n v i s i t o r T r a v e r s a l . C : main ( ) \n ” ) ;
36
37             SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
38             ROSE ASSERT ( p r o j e c t != NULL ) ;
39
40      // B u i l d t h e t r a v e r s a l o b j e c t
41         v i s i t o r T r a v e r s a l exampleTraversal ;
42
43      // C a l l t h e t r a v e r s a l f u n c t i o n ( member f u n c t i o n o f A s t S i m p l e P r o c e s s i n g )
44      // s t a r t i n g a t t h e p r o j e c t node o f t h e AST, u s i n g a p r e o r d e r t r a v e r s a l .
45         exampleTraversal . t r a v e r s e I n p u t F i l e s ( project , preorder ) ;
46
47             return 0;
48        }



                                 Figure 7.4: Example source showing simple visitor pattern.




 1   Found a f o r l o o p . . .
 2   Found a f o r l o o p . . .
 3   T r a v e r s a l ends here .



                                     Figure 7.5: Output of input file to the visitor traversal.
     42                                                       CHAPTER 7. INTRODUCTION TO AST TRAVERSALS




 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an example p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   // r o s e . C : Example ( d e f a u l t ) ROSE P r e p r o c e s s o r : u s e d f o r t e s t i n g ROSE i n f r a s t r u c t u r e
 3
 4   #i n c l u d e ” r o s e . h”
 5
 6   c l a s s PreAndPostOrderTraversal : p u b l i c AstPrePostProcessing
 7         {
 8            public :
 9                 v i r t u a l v o i d p r e O r d e r V i s i t ( SgNode∗ n ) ;
10                 v i r t u a l v o i d p o s t O r d e r V i s i t ( SgNode∗ n ) ;
11         };
12
13   v o i d P r e A n d P o s t O r d e r T r a v e r s a l : : p r e O r d e r V i s i t ( SgNode∗ n )
14         {
15           i f ( i s S g F o r S t a t e m e n t ( n ) != NULL)
16                {
17                    p r i n t f ( ” E n t e r i n g f o r l o o p . . . \n ” ) ;
18                }
19         }
20
21   v o i d P r e A n d P o s t O r d e r T r a v e r s a l : : p o s t O r d e r V i s i t ( SgNode∗ n )
22         {
23           i f ( i s S g F o r S t a t e m e n t ( n ) != NULL)
24                {
25                    p r i n t f ( ” L e a v i n g f o r l o o p . . . \n ” ) ;
26                }
27         }
28
29   int
30   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
31       {
32         i f ( SgProject : : g e t v e r b o s e ( ) > 0)
33                p r i n t f ( ” I n p r e P o s t T r a v e r s a l . C : main ( ) \n ” ) ;
34
35             SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
36             ROSE ASSERT ( p r o j e c t != NULL ) ;
37
38        // B u i l d t h e t r a v e r s a l o b j e c t
39           PreAndPostOrderTraversal exampleTraversal ;
40
41        // C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t node o f t h e AST
42           exampleTraversal . t r a v e r s e I n p u t F i l e s ( p r o j e c t ) ;
43
44             return 0;
45         }



                        Figure 7.6: Example source showing simple pre- and postorder pattern.




 1   Entering f o r loop . . .
 2   Leaving f o r loop . . .
 3   Entering f o r loop . . .
 4   Leaving f o r loop . . .



                           Figure 7.7: Output of input file to the pre- and postorder traversal.
     7.2. TRAVERSALS OF THE AST STRUCTURE                                                                                                                          43



 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an example p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   // r o s e . C : Example ( d e f a u l t ) ROSE P r e p r o c e s s o r : u s e d f o r t e s t i n g ROSE i n f r a s t r u c t u r e
 3
 4   #i n c l u d e ” r o s e . h”
 5
 6   // B u i l d an i n h e r i t e d a t t r i b u t e         f o r the t r e e        t r a v e r s a l t o t e s t t h e r e w r i t e mechanism
 7   class InheritedAttribute
 8      {
 9         public :
10           // Depth i n AST
11                i n t depth ;
12                i n t maxLinesOfOutput ;
13
14                // S p e c i f i c c o n s t r u c t o r s a r e r e q u i r e d
15                   I n h e r i t e d A t t r i b u t e ( i n t x ) : depth ( x ) , maxLinesOfOutput ( 2 0 ) { } ;
16                   I n h e r i t e d A t t r i b u t e ( c o n s t I n h e r i t e d A t t r i b u t e & X ) : depth (X . depth ) , maxLinesOfOutput ( 2 0 ) { } ;
17        };
18
19   class      visitorTraversal                :   p u b l i c AstTopDownProcessing<I n h e r i t e d A t t r i b u t e >
20      {
21             public :
22               // v i r t u a l      f u n c t i o n must be d e f i n e d
23                  virtual            I n h e r i t e d A t t r i b u t e e v a l u a t e I n h e r i t e d A t t r i b u t e ( SgNode∗ n ,     InheritedAttribute                    inheritedAttribute );
24        };
25
26   InheritedAttribute
27   v i s i t o r T r a v e r s a l : : e v a l u a t e I n h e r i t e d A t t r i b u t e ( SgNode∗ n , I n h e r i t e d A t t r i b u t e i n h e r i t e d A t t r i b u t e )
28         {
29              s t a t i c i nt linesOfOutput = 0;
30              i f ( l i n e s O f O u t p u t++ < i n h e r i t e d A t t r i b u t e . maxLinesOfOutput )
31                        p r i n t f ( ” Depth i n AST a t %s = %d \n ” , n− a g e c l a s s n a m e ( ) , i n h e r i t e d A t t r i b u t e . depth ) ;
                                                                                                   >s
32
33             r e t u r n I n h e r i t e d A t t r i b u t e ( i n h e r i t e d A t t r i b u t e . depth + 1 ) ;
34        }
35
36   int
37   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
38       {
39         SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
40         ROSE ASSERT ( p r o j e c t != NULL ) ;
41
42      // DQ ( 1 / 1 8 / 2 0 0 6 ) : P a r t o f d e b u g g i n g
43         SgFile & l o c a l F i l e = project − g e t f i l e ( 0 ) ;
                                                             >
44         l o c a l F i l e . g e t f i l e i n f o ()−> d i s p l a y ( ” l o c a l F i l e        information ”);
45
46      // B u i l d t h e i n h e r i t e d a t t r i b u t e
47         InheritedAttribute inheritedAttribute (0);
48
49      // B u i l d t h e t r a v e r s a l o b j e c t
50         v i s i t o r T r a v e r s a l exampleTraversal ;
51
52      // C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t node o f t h e AST
53         exampleTraversal . t r a v e r s e I n p u t F i l e s ( project , i n h e r i t e d A t t r i b u t e ) ;
54
55      // Or t h e t r a v e r s a l o v e r a l l AST IR n o d e s can be c a l l e d !
56         exampleTraversal . t r a v e r s e ( project , i n h e r i t e d A t t r i b u t e ) ;
57
58             return 0;
59        }



     Figure 7.8: Example source code showing use of inherited attributes (passing context information
     down the AST.
     44                                                 CHAPTER 7. INTRODUCTION TO AST TRAVERSALS




 1   Inside of Sg File Info : : display ( l o c a l F i l e information )
 2        isTransformation                                              = false
 3        isCompilerGenerated                                           = false
 4        isOutputInCodeGeneration                                      = false
 5        isShared                                                      = false
 6        isFrontendSpecific                                            = false
 7        isSourcePositionUnavailableInFrontend = f a l s e
 8        isCommentOrDirective                                          = false
 9        isToken                                                       = false
10                                                                                                     −d
          f i l e n a m e = / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t u t o r i a l / i n p
11        line            = 1 column = 1
12   Depth i n AST a t S g S o u r c e F i l e = 0
13   Depth i n AST a t S g G l o b a l = 1
14   Depth i n AST a t S g T e m p l a t e D e c l a r a t i o n = 2
15   Depth i n AST a t S g F u n c t i o n D e c l a r a t i o n = 2
16   Depth i n AST a t S g F u n c t i o n P a r a m e t e r L i s t = 3
17   Depth i n AST a t S g I n i t i a l i z e d N a m e = 4
18   Depth i n AST a t S g F u n c t i o n D e c l a r a t i o n = 2
19   Depth i n AST a t S g F u n c t i o n P a r a m e t e r L i s t = 3
20   Depth i n AST a t S g I n i t i a l i z e d N a m e = 4
21   Depth i n AST a t S g F u n c t i o n D e f i n i t i o n = 3
22   Depth i n AST a t S g B a s i c B l o c k = 4
23   Depth i n AST a t S g V a r i a b l e D e c l a r a t i o n = 5
24   Depth i n AST a t S g I n i t i a l i z e d N a m e = 6
25   Depth i n AST a t S g A s s i g n I n i t i a l i z e r = 7
26   Depth i n AST a t S g I n t V a l = 8
27   Depth i n AST a t S g V a r i a b l e D e c l a r a t i o n = 5
28   Depth i n AST a t S g I n i t i a l i z e d N a m e = 6
29   Depth i n AST a t Sg Fo r St at ement = 5
30   Depth i n AST a t S g F o r I n i t S t a t e m e n t = 6
31   Depth i n AST a t S g V a r i a b l e D e c l a r a t i o n = 7



                         Figure 7.9: Output of input file to the inherited attribute traversal.
     7.2. TRAVERSALS OF THE AST STRUCTURE                                                                                                                          45

     7.2.5          Synthesized Attributes

 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an example p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   // r o s e . C : Example ( d e f a u l t ) ROSE P r e p r o c e s s o r : u s e d f o r t e s t i n g ROSE i n f r a s t r u c t u r e
 3
 4   #i n c l u d e ” r o s e . h”
 5
 6   #i n c l u d e <a l g o r i t h m >
 7   #i n c l u d e <f u n c t i o n a l >
 8   #i n c l u d e <numeric>
 9
10   typedef bool SynthesizedAttribute ;
11
12   class      visitorTraversal               :   p u b l i c AstBottomUpProcessing<S y n t h e s i z e d A t t r i b u t e >
13      {
14             public :
15               // v i r t u a l    f u n c t i o n must be d e f i n e d
16                  virtual          SynthesizedAttribute evaluateSynthesizedAttribute (
17                                             SgNode∗ n , S y n t h e s i z e d A t t r i b u t e s L i s t c h i l d A t t r i b u t e s      );
18        };
19
20   SynthesizedAttribute
21   v i s i t o r T r a v e r s a l : : e v a l u a t e S y n t h e s i z e d A t t r i b u t e ( SgNode∗ n , S y n t h e s i z e d A t t r i b u t e s L i s t    childAttributes )
22         {
23       // Fold up t h e l i s t o f c h i l d a t t r i b u t e s u s i n g l o g i c a l or , i . e . t h e l o c a l
24       // r e s u l t w i l l be t r u e i f f one o f t h e c h i l d a t t r i b u t e s i s t r u e .
25              SynthesizedAttribute localResult =
26                     s t d : : a c c u m u l a t e ( c h i l d A t t r i b u t e s . b e g i n ( ) , c h i l d A t t r i b u t e s . end ( ) ,
27                                                     f a l s e , s t d : : l o g i c a l o r <b o o l > ( ) ) ;
28
29             if   ( i s S g F o r S t a t e m e n t ( n ) != NULL)
30                  {
31                       p r i n t f ( ” Found a f o r l o o p . . .          \n ” ) ;
32                       localResult = true ;
33                  }
34
35             return localResult ;
36        }
37
38   int
39   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
40       {
41         SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
42         ROSE ASSERT ( p r o j e c t != NULL ) ;
43
44      // B u i l d t h e t r a v e r s a l o b j e c t
45         v i s i t o r T r a v e r s a l exampleTraversal ;
46
47      // C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t node o f t h e AST
48         SynthesizedAttribute r e s u l t = exampleTraversal . t r a v e r s e ( p r o j e c t ) ;
49
50             if   ( r e s u l t == t r u e )
51                  {
52                      p r i n t f ( ” The program c o n t a i n s a t l e a s t one l o o p ! \ n ” ) ;
53                  }
54
55             return 0;
56        }



     Figure 7.10: Example source code showing use of synthesized attributed (passing analysis infor-
     mation up the AST).

          Figure 7.10 shows the use of attributes to pass information up the AST. The lifetime of the
    46                                           CHAPTER 7. INTRODUCTION TO AST TRAVERSALS


1   Found a f o r l o o p . . .
2   Found a f o r l o o p . . .
3   The program c o n t a i n s a t l e a s t one l o o p !



                   Figure 7.11: Output of input file to the synthesized attribute traversal.


    attributes are similar as for inherited attributes. Attributes such as these are called synthesized
    attributes.
        This code shows the code for a translator which does an analysis of an input source code
    to determine the presence of loops. It returns true if a loop exists in the input code and false
    otherwise. The list of synthesized attributes representing the information passed up the AST
    from a node’s children is of type SynthesizedAttributesList, which is a type that behaves
    very similarly to vector<SynthesizedAttribute> (it supports iterators, can be indexed, and
    can be used with STL algorithms).
        The example determines the existence of loops for a given program.
     7.2. TRAVERSALS OF THE AST STRUCTURE                                                                                                                           47

     7.2.6           Accumulator Attributes

 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an example p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   // r o s e . C : Example ( d e f a u l t ) ROSE P r e p r o c e s s o r : u s e d f o r t e s t i n g ROSE i n f r a s t r u c t u r e
 3
 4   #i n c l u d e ” r o s e . h”
 5
 6   // B u i l d an a c c u m u l a t o r a t t r i b u t e , f a n c y name f o r what i s                       essentially a global                  v a r i a b l e : −).
 7   c l a s s AccumulatorAttribute
 8         {
 9            public :
10                 i n t forLoopCounter ;
11
12                // S p e c i f i c c o n s t r u c t o r s a r e o p t i o n a l
13                   AccumulatorAttribute ( ) { forLoopCounter = 0 ; }
14                   A c c u m u l a t o r A t t r i b u t e ( c o n s t A c c u m u l a t o r A t t r i b u t e & X ) {}
15                   A c c u m u l a t o r A t t r i b u t e & o p e r a t o r= ( c o n s t A c c u m u l a t o r A t t r i b u t e & X ) { r e t u r n ∗ t h i s ; }
16        };
17
18   class      visitorTraversal                 :    public AstSimpleProcessing
19      {
20             public :
21                  s t a t i c AccumulatorAttribute accumulatorAttribute ;
22                  v i r t u a l v o i d v i s i t ( SgNode∗ n ) ;
23        };
24
25   // d e c l a r a t i o n r e q u i r e d f o r s t a t i c d a t a member
26   AccumulatorAttribute v i s i t o r T r a v e r s a l : : accumulatorAttribute ;
27
28   v o i d v i s i t o r T r a v e r s a l : : v i s i t ( SgNode∗ n )
29         {
30           i f ( i s S g F o r S t a t e m e n t ( n ) != NULL)
31                 {
32                      p r i n t f ( ” Found a f o r l o o p . . . \n ” ) ;
33                      a c c u m u l a t o r A t t r i b u t e . f o r L o o p C o u n t e r ++;
34                 }
35         }
36
37   int
38   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
39       {
40         SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
41         ROSE ASSERT ( p r o j e c t != NULL ) ;
42
43      // B u i l d t h e t r a v e r s a l o b j e c t
44         v i s i t o r T r a v e r s a l exampleTraversal ;
45
46      // C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t node o f t h e AST
47      // can be s p e c i f i e d t o be p r e o r d e r o r p o s t o r d e r ) .
48         exampleTraversal . t r a v e r s e I n p u t F i l e s ( project , preorder ) ;
49
50             p r i n t f ( ” Number o f            f o r l o o p s i n i n p u t a p p l i c a t i o n = %d \n ” , e x a m p l e T r a v e r s a l . a c c u m u l a t o r A t t r i b u t e . f o r L o o p C o u n t e r ) ;
51
52             return 0;
53        }



     Figure 7.12: Example source code showing use of accumulator attributes (typically to count
     things in the AST).

         Figure 7.12 shows the use of a different sort of attribute. This attribute has a lifetime equal
     to the lifetime of the traversal object (much longer than the traversal of any subset of IR nodes).
     The same attribute is accessible from each IR node. Such attributes are called accumulator
    48                                              CHAPTER 7. INTRODUCTION TO AST TRAVERSALS


1   Found a f o r l o o p . . .
2   Found a f o r l o o p . . .
3   Number o f f o r l o o p s i n i n p u t a p p l i c a t i o n = 2



                   Figure 7.13: Output of input file to the accumulator attribute traversal.


    attributes and are semantically equivalent to a global variable. Accumulator attributes act as
    global variables which can easily be used to count application specific properties within the AST.
        Note that due to the limitation that the computation of inherited attributes cannot be made
    dependent on the values of synthesized attributes, counting operations cannot be implemented
    by combining these attributes as is usually done in attribute grammars. However, the use of
    accumulator attributes serves well for this purpose. Therefore all counting-like operations should
    be implemented using accumulator attributes (= member variables of traversal or processing
    classes).
        Although not shown in this tutorial explicitly, accumulator attributes may be easily mixed
    with inherited and/or synthesized attributes.
        In this example we count the number of for-loops in an input program.

    7.2.7       Inherited and Synthesized Attributes
    Figure 7.14 shows the combined use of inherited and synthesized attributes. The example source
    code shows the mixed use of such attributes to list the functions containing loop. Inherited
    attributes are used to communicate that the traversal is in a function, which the synthesized
    attributes are used to pass back the existence of loops deeper within the subtrees associated with
    each function.
        List of functions containing loops.
     7.2. TRAVERSALS OF THE AST STRUCTURE                                                                                                                                          49


 1   // ROSE i s         a   tool      for    building        preprocessors ,            this      file     is    an e x a m p l e    preprocessor        built   w i t h ROSE .
 2
 3   #i n c l u d e   ” r o s e . h”
 4
 5   #i n c l u d e <a l g o r i t h m >
 6   #i n c l u d e <f u n c t i o n a l >
 7   #i n c l u d e <n u m e r i c>
 8
 9   typedef          bool   InheritedAttribute ;
10   typedef          bool   SynthesizedAttribute ;
11
12   class      Traversal         :    public     SgTopDownBottomUpProcessing<I n h e r i t e d A t t r i b u t e , S y n t h e s i z e d A t t r i b u t e >
13      {
14             public :
15               // F u n c t i o n s r e q u i r e d
16                  InheritedAttribute evaluateInheritedAttribute                                           (
17                       SgNode ∗ a s t N o d e ,
18                       InheritedAttribute inheritedAttribute );
19
20                      SynthesizedAttribute evaluateSynthesizedAttribute (
21                         SgNode ∗ a s t N o d e ,
22                         InheritedAttribute inheritedAttribute ,
23                         SubTreeSynthesizedAttributes synthesizedAttributeList                                                );
24        };
25
26   InheritedAttribute
27   Traversal : : evaluateInheritedAttribute (
28        SgNode ∗ a s t N o d e ,
29        InheritedAttribute inheritedAttribute )
30      {
31        i f ( i s S g F u n c t i o n D e f i n i t i o n ( astNode ) )
32            {
33           // The i n h e r i t e d a t t r i b u t e i s t r u e i f f                we a r e     inside       a   function .
34                return true ;
35            }
36        return inheritedAttribute ;
37      }
38
39   SynthesizedAttribute
40   Traversal : : evaluateSynthesizedAttribute (
41        SgNode ∗ a s t N o d e ,
42        InheritedAttribute inheritedAttribute ,
43        SynthesizedAttributesList childAttributes )
44      {
45        i f ( i n h e r i t e d A t t r i b u t e == f a l s e )
46            {
47           // The i n h e r i t e d a t t r i b u t e i s f a l s e , i . e . we a r e n o t i n s i d e any
48           // f u n c t i o n , s o t h e r e c a n be no l o o p s h e r e .
49                return f a l s e ;
50            }
51        else
52            {
53           // F o l d up t h e l i s t o f c h i l d a t t r i b u t e s u s i n g l o g i c a l o r , i . e . t h e                         local
54           // r e s u l t w i l l be t r u e i f f o n e o f t h e c h i l d a t t r i b u t e s i s t r u e .
55                SynthesizedAttribute localResult =
56                s t d : : a c c u m u l a t e ( c h i l d A t t r i b u t e s . b e g i n ( ) , c h i l d A t t r i b u t e s . end ( ) ,
57                                                f a l s e , s t d : : l o g i c a l o r <b o o l > ( ) ) ;
58                i f ( i s S g F u n c t i o n D e f i n i t i o n ( a s t N o d e ) && l o c a l R e s u l t == t r u e )
59                     {
60                           p r i n t f ( ” Found a f u n c t i o n c o n t a i n i n g a f o r l o o p . . . \ n ” ) ;
61                     }
62                i f ( isSgForStatement ( astNode ) )
63                     {
64                           localResult = true ;
65                     }
66                return localResult ;
67            }
68      }
69
70   int
71   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
72       {
73     // B u i l d t h e a b s t r a c t s y n t a x t r e e
74         SgProject ∗ p r o j e c t = f r o n t e n d ( argc , argv ) ;
75         ROSE ASSERT ( p r o j e c t != NULL ) ;
76
77       //    Build the i n h e r i t e d a t t r i b u t e
78             InheritedAttribute inheritedAttribute = false ;
79
80       //    Define the t r a v e r s a l
81             T r a v e r s a l myTraversal ;
82
83       //    C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t ( r o o t ) node o f t h e AST
84             myTraversal . t r a v e r s e I n p u t F i l e s ( p r o j e c t , i n h e r i t e d A t t r i b u t e ) ;
85
86       //    T h i s program         only    does     analysis ,         so   it    need     not    call       the   backend        to      generate   code .
87             return 0;
88        }




     Figure 7.14: Example source code showing use of both inherited and synthesized attributes
     working together (part 1).
    50                                                CHAPTER 7. INTRODUCTION TO AST TRAVERSALS




1   Found a f u n c t i o n c o n t a i n i n g a f o r l o o p   ...
2   Found a f u n c t i o n c o n t a i n i n g a f o r l o o p   ...



           Figure 7.15: Output of input file to the inherited and synthesized attribute traversal.
7.2. TRAVERSALS OF THE AST STRUCTURE                                                           51

7.2.8    Persistent Attributes
Figure 7.16 shows the use of another form of attribute. This attribute has a lifetime which is
controlled explicitly by the user; it lives on the heap typically. These attributes are explicitly
attached to the IR nodes and are not managed directly by the traversal. There attributes are
called persistent attributes and are not required to be associated with any traversal. Persistent
attributes are useful for storing information across multiple traversals (or permanently within
the AST) for later traversal passes.
    Persistent attributes may be used at any time and combined with other traversals (similar to
accumulator attributes). Traversals may combine any or all of the types of attributes within in
ROSE as needed to store, gather, or propagate information within the AST for complex program
analysis.
     52                                                         CHAPTER 7. INTRODUCTION TO AST TRAVERSALS


 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an example p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   // r o s e . C : Example ( d e f a u l t ) ROSE P r e p r o c e s s o r : u s e d f o r t e s t i n g ROSE i n f r a s t r u c t u r e
 3
 4   #i n c l u d e ” r o s e . h”
 5
 6   class       persistantAttribute                 :   public AstAttribute
 7      {
 8              public :
 9                   int value ;
10                   p e r s i s t a n t A t t r i b u t e ( i n t v ) : v a l u e ( v ) {}
11         };
12
13   class       visitorTraversalSetAttribute                         :       public AstSimpleProcessing
14      {
15              public :
16                   v i r t u a l void         v i s i t ( SgNode∗ n ) ;
17         };
18
19   v o i d v i s i t o r T r a v e r s a l S e t A t t r i b u t e : : v i s i t ( SgNode∗ n )
20         {
21           i f ( i s S g F o r S t a t e m e n t ( n ) != NULL)
22                 {
23                      p r i n t f ( ” Found a f o r l o o p ( s e t t h e a t t r i b u t e )             ...     \n ” ) ;
24
25                 // B u i l d an a t t r i b u t e ( on t h e heap )
26                    A s t A t t r i b u t e ∗ n e w A t t r i b u t e = new p e r s i s t a n t A t t r i b u t e ( 5 ) ;
27                    ROSE ASSERT( n e w A t t r i b u t e != NULL ) ;
28
29                 // Add i t t o t h e AST ( s o i t can be f o u nd l a t e r i n a n o t h e r p a s s o v e r t h e AST)
30                    n−>addNewAttribute ( ” MyNewAttribute ” , n e w A t t r i b u t e ) ;
31                  }
32         }
33
34   class       visitorTraversalReadAttribute                            :    public AstSimpleProcessing
35      {
36              public :
37                   v i r t u a l void         v i s i t ( SgNode∗ n ) ;
38         };
39
40   v o i d v i s i t o r T r a v e r s a l R e a d A t t r i b u t e : : v i s i t ( SgNode∗ n )
41         {
42           i f ( i s S g F o r S t a t e m e n t ( n ) != NULL)
43                 {
44                      p r i n t f ( ” Found a f o r l o o p ( r e a d t h e a t t r i b u t e )             ...    \n ” ) ;
45
46                 //   Add i t t o t h e AST ( s o i t can be f o u nd l a t e r i n a n o t h e r p a s s o v e r t h e AST)
47                 //   A s t A t t r i b u t e ∗ e x i s t i n g A t t r i b u t e = n− t t r i b u t e [ ” MyNewAttribute ” ] ;
                                                                                             >a
48                 //   DQ ( 1 / 2 / 2 0 0 6 ) : Added s u p p o r t f o r new a t t r i b u t e i n t e r f a c e .
49                 //   p r i n t f ( ” v i s i t o r T r a v e r s a l R e a d A t t r i b u t e : : v i s i t ( ) : u s i n g new a t t r i b u t e i n t e r f a c e \n ” ) ;
50                 //   A s t A t t r i b u t e ∗ e x i s t i n g A t t r i b u t e = n− t t r i b u t e ( ) [ ” MyNewAttribute ” ] ;
                                                                                             >a
51                      A s t A t t r i b u t e ∗ e x i s t i n g A t t r i b u t e = n− e t A t t r i b u t e ( ” MyNewAttribute ” ) ;
                                                                                             >g
52                      ROSE ASSERT( e x i s t i n g A t t r i b u t e != NULL ) ;
53
54                      p r i n t f (” Existing          a t t r i b u t e a t %p v a l u e = %d \n ” , n , d y n a m i c c a s t <p e r s i s t a n t A t t r i b u t e ∗>( e x i s t i n g A t t r i b
55                  }
56         }
57
58   int
59   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
60       {
61         SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
62         ROSE ASSERT ( p r o j e c t != NULL ) ;
63
64        // B u i l d t h e t r a v e r s a l o b j e c t t o s e t p e r s i s t a n t AST a t t r i b u t e s
65           visitorTraversalSetAttribute exampleTraversalSettingAttribute ;
66
67        // C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t node o f t h e AST
68           exampleTraversalSettingAttribute . traverseInputFiles ( project , preorder ) ;
69
70        // B u i l d t h e t r a v e r s a l o b j e c t t o r e a d any e x i s t i n g AST a t t r i b u t e s
71           visitorTraversalReadAttribute exampleTraversalReadingAtribute ;
72
73        // C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t node o f t h e AST
74           exampleTraversalReadingAtribute . t r a v e r s e I n p u t F i l e s ( project , preorder ) ;
75
76              return 0;
77         }



     Figure 7.16: Example source code showing use of persistent attributes used to pass information
     across multiple passes over the AST.
    7.2. TRAVERSALS OF THE AST STRUCTURE                                                      53




1   Found a    f o r loop ( s e t the a t t r i b u t e ) . . .
2   Found a    f o r loop ( s e t the a t t r i b u t e ) . . .
3   Found a    f o r loop ( read the a t t r i b u t e ) . . .
4   Existing     a t t r i b u t e at 0 x2abb4cdfd010 value = 5
5   Found a    f o r loop ( read the a t t r i b u t e ) . . .
6   Existing     a t t r i b u t e at 0 x2abb4cdfd130 value = 5



    Figure 7.17: Output of input file to the persistent attribute traversal showing the passing of
    information from one AST traversal to a second AST traversal.
     54                                                     CHAPTER 7. INTRODUCTION TO AST TRAVERSALS

     7.2.9          Nested Traversals

 1   // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   class       visitorTraversal            :   public AstSimpleProcessing
 6      {
 7              public :
 8                   v i r t u a l void      v i s i t ( SgNode∗ n ) ;
 9         };
10
11   class       nestedVisitorTraversal                :   public AstSimpleProcessing
12      {
13              public :
14                   v i r t u a l void      v i s i t ( SgNode∗ n ) ;
15         };
16
17   v o i d v i s i t o r T r a v e r s a l : : v i s i t ( SgNode∗ n )
18         {
19           i f ( i s S g F u n c t i o n D e c l a r a t i o n ( n ) != NULL)
20                 {
21                      p r i n t f ( ” Found a f u n c t i o n d e c l a r a t i o n    ...   \n ” ) ;
22
23                // B u i l d t h e n e s t e d t r a v e r s a l o b j e c t
24                   ne st ed V i s i t o r T r a v e r sa l exampleTraversal ;
25
26                // C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t node o f t h e AST ( t r a v e r s e i n p o s t o r d e r j u s t t o be d i f f e r e
27                // Note t h a t we c a l l t h e t r a v e r s e f u n c t i o n i n s t e a d o f t r a v e r s e I n p u t F i l e s , b e c a u s e we a r e n o t s t a r t i n g a
28                // t h e AST r o o t .
29                   exampleTraversal . t r a v e r s e (n , postorder ) ;
30                 }
31         }
32
33   v o i d n e s t e d V i s i t o r T r a v e r s a l : : v i s i t ( SgNode∗ n )
34         {
35           i f ( i s S g F u n c t i o n D e f i n i t i o n ( n ) != NULL)
36                {
37                     p r i n t f ( ” Found a f u n c t i o n d e f i n i t i o n      within the f u n c t i o n   declaration       ...   \n ” ) ;
38                }
39         }
40
41   int
42   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
43       {
44         i f ( SgProject : : g e t v e r b o s e ( ) > 0)
45                p r i n t f ( ” I n v i s i t o r T r a v e r s a l . C : main ( ) \n ” ) ;
46
47              SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
48              ROSE ASSERT ( p r o j e c t != NULL ) ;
49
50        // B u i l d t h e t r a v e r s a l o b j e c t
51           v i s i t o r T r a v e r s a l exampleTraversal ;
52
53        // C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t node o f t h e AST
54           exampleTraversal . t r a v e r s e I n p u t F i l e s ( project , preorder ) ;
55
56              return 0;
57         }



                             Figure 7.18: Example source code showing use nested traversals.

           Figure 7.18 shows the use of multiple traversals in composition. Figure 7.19 shows the output
    7.2. TRAVERSALS OF THE AST STRUCTURE                                                        55


1   Found   a   function   declaration . . .
2   Found   a   function   declaration . . .
3   Found   a   function   d e f i n i t i o n within the f u n c t i o n   declaration   ...
4   Found   a   function   declaration . . .
5   Found   a   function   declaration . . .
6   Found   a   function   declaration . . .
7   Found   a   function   declaration . . .
8   Found   a   function   d e f i n i t i o n within the f u n c t i o n   declaration   ...



                     Figure 7.19: Output of input file to the nested traversal example.


    of the nested traversal.
     56                                                 CHAPTER 7. INTRODUCTION TO AST TRAVERSALS

     7.2.10           Combining all Attributes and Using Primitive Types

 1   i n t main ( ) {
 2       i n t x =1;
 3       f o r ( i n t i =1; i <10; i ++)
 4           f o r ( i n t j=i ; j <10; j ++)
 5               f o r ( i n t k=i ; k <10; k++)
 6                   f o r ( i n t l=i ; l <10; l ++)
 7                                      =i
                         f o r ( i n t m ;m<10;m++)
 8                           x++;
 9
10       i n t i =5 , j =7;
11       w h i l e ( i >0) {
12           w h i l e ( j >0) {
13               x++;
14               j −−;
15               i −−;
16           }
17       }
18
19       i =10;
20       do {
21          x++;
22          i −−;
23       } w h i l e ( i >0);
24
25        return x ;
26   }



                         Figure 7.20: Input code with nested loops for nesting info processing

         The previous examples have shown cases where attributes were classes, alternatively at-
     tributes can be any primitive type (int, bool, etc.). This example demonstrates how to use
     AstTopDownBottomUpProcessing to compute inherited and synthesized attributes, generate
     pdf and dot output, how to accumulate information, and how to attach attributes to AST nodes
     in the same pass.
         The attributes are used to compute the nesting level and the nesting depth of for/while/do-
     while loops: The nesting level is computed using an inherited attribute. It holds that nesting −
     level(innerloop) = nesting − level(outerloop) + 1 starting with 1 at the outer most loop. The
     nesting depth is computed using a synthesized attribute. It holds that nesting−depth(innerloop) =
     nesting − level(outerloop) − 1 starting with 1 at the inner most loop.
         To compute the values we use a primitive type (unsigned int). This example also shows how
     to use defaultSynthesizedAttribute to initialize a synthesized attribute of primitive type. The
     values of the attributes are attached to the AST using AstAttribute and the AST node attribute
     mechanism available at every AST node (which can be accessed with node->attribute). (see
     loopNestingInfoProcessing.C)
         For the entire program the maximum nesting level (= max nesting depth) is computed as
     accumulated value using member variable _maxNestingLevel of class LoopNestingInfoProcess-
     ing. We also demonstrate how to customize an AstAttribute such that the value of the attribute
     is printed in a pdf output. (by overriding toString, see LoopNestingInfo class)
         In the generated pdf file (for some C++ input file) the values of the attributes can be viewed
     for each node (see printLoopInfo implementation). Further more we also generate a dot file, to
     visualize the tree using the graph visualization tool dot. The generated file can be converted to
7.2. TRAVERSALS OF THE AST STRUCTURE                                                  57

postscript (using dot) and viewed with gv.

7.2.11    Combined Traversals
Performing a large number of program analyses as separate traversals of the AST can be some-
what inefficient as there is some overhead associated with visiting every node several times.
ROSE therefore provides a mechanism for combining traversal objects of the same base type and
evaluating them in a single traversal of the AST. This is entirely transparent to the individual
traversal object, so existing code can be reused with the combination mechanism, and analyzers
can be developed and tested in isolation and combined when needed.
    The one requirement that is placed on traversals to be combined is that they be independent
of each other; in particular, this means that they should not modify the AST or any shared
global data. Any output produced by the analyzers will be interleaved.
    Figure 7.24 shows the source code for a translator that combines three different analyzers into
one traversal, each one counting the occurrences of a different type of AST node (as determined
by a VariantT value). First three traversals are run after each other, as usual; then three traversal
objects are passed (by pointer) to an object of type AstCombinedSimpleProcessing using its
addTraversal method. One then invokes one of the usual traverse methods on this combined
object with the same effect as if it had been called for each of the traversal objects individually.
    Any operation on the list of analyzers is possible using the get traversalPtrListRef
method of the combined processing class that returns a reference to its internal list of ana-
lyzers (an object of type vector<AstSimpleProcessing *>). Any changes made through this
reference will be reflected in further traversals.
    In addition to AstCombinedSimpleProcessing, there is also a combined class for each of the
other types of traversals discussed above: AstCombinedTopDownProcessing, AstCombinedBottomUpProcessing,
etc. Where traversals using attributes are combined, all of the combined traversals must have
the same attribute types (i. e. the same template parameters). Attributes are passed to and
returned from the combined traversal as a vector.
     58                                                       CHAPTER 7. INTRODUCTION TO AST TRAVERSALS


 1   // Author : Markus Schordan , Vienna U n i v e r s i t y o f Technology , 2 0 0 4 .
 2   // $ I d : l o o p N e s t i n g I n f o P r o c e s s i n g . C, v 1 . 1 2 0 0 6 / 0 4 / 2 4 0 0 : 2 2 : 0 0 d q u i n l a n Exp $
 3
 4   // #i n c l u d e <s t r i n g >
 5   // #i n c l u d e <i o s t r e a m >
 6
 7   #i n c l u d e ” r o s e . h”
 8
 9   u s i n g namespace s t d ;
10
11   typedef        unsigned i n t         NestingLevel ;
12   typedef        unsigned i n t         NestingDepth ;
13   typedef        NestingLevel           InhNestingLevel ;
14   typedef        NestingDepth           SynNestingDepth ;
15
16   / ∗ ! T h i s c l a s s i s u s e d t o a t t a c h i n f o r m a t i o n t o AST n o d e s .
17           Method ’ t o S t r i n g ’ i s o v e r r i d d e n and
18           c a l l e d when a p d f f i l e i s g e n e r a t e d . T h i s a l l o w s t o d i s p l a y
19           t h e v a l u e o f an AST node a t t r i b u t e ( a n n o t a t i o n ) i n a p d f f i l e .
20   ∗/
21   c l a s s NestingLevelAnnotation : public AstAttribute {
22   public :
23       NestingLevelAnnotation ( N e s t i n g L e v e l n , NestingDepth d )
24           :     n e s t i n g L e v e l ( n ) , n e s t i n g D e p t h ( d ) {}
25       NestingLevel getNestingLevel () { return                                   nestingLevel ; }
26       NestingDepth getNestingDepth ( ) { r e t u r n nestingDepth ; }
27       string toString () {
28                                                  <
             o s t r i n g s t r e a m s s ; s s < n e s t i n g L e v e l <<”,”<< n e s t i n g D e p t h ;
29           return ss . str ( ) ;
30       }
31   private :
32       NestingLevel                nestingLevel ;
33       NestingDepth nestingDepth ;
34   };
35
36   /∗ !
37   The l o o p n e s t i n g l e v e l and n e s t i n g depth f o r e a c h w h i l e / d o w h i l e / f o r
38   l o o p n e s t i s computed . I t i s a t t a c h e d t o t h e AST a s a n n o t a t i o n and
39                                                  >a
     can be a c c e s s e d a s node− t t r i b u t e [ ” l o o p N e s t i n g I n f o ” ]
40   a f t e r t h e p r o c e s s i n g ha s been p e r f o r m e d .
41   The maximum n e s t i n g l e v e l o f t h e whole AST i s computed a s
42   ” a c c u m u l a t e d ” v a l u e i n a member v a r i a b l e and can be a c c e s s e d w i t h
43   getMaxNestingLevel ( ) .
44   ∗/
45   c l a s s L o o p L e v e l P r o c e s s i n g : p u b l i c AstTopDownBottomUpProcessing<I n h N e s t i n g L e v e l , SynNestingDepth> {
46   public :
47       L o o p L e v e l P r o c e s s i n g ( ) : m a x N e s t i n g L e v e l ( 0 ) {}
48
49        / ∗ ! P e r f o r m s a t r a v e r s a l o f t h e AST and computes l o o p −n e s t i n g i n f o r m a t i o n by u s i n g
50               i n h e r i t e d and s y n t h e s i z e d a t t r i b u t e s . The r e s u l t s a r e a t t a c h e d t o t h e AST a s
51               annotation .
52        ∗/
53        v o i d a t t a c h L o o p N e s t i n g A n n o t a t o n ( S g P r o j e c t ∗ node ) { t r a v e r s e I n p u t F i l e s ( node , 0 ) ; }
54
55        / ∗ ! R e t u r n s t h e maximum n e s t i n g l e v e l o f t h e e n t i r e AST ( o f t h e i n p u t                  file ).
56              R e q u i r e s a t t a c h L o o p N e s t i n g A n n o t a t i o n ( t o be c a l l e d b e f o r e )
57        ∗/
58        NestingLevel getMaxNestingLevel ( ) { return maxNestingLevel ; }
59
60   protected :
61      / / ! computes t h e n e s t i n g l e v e l
62      I n h N e s t i n g L e v e l e v a l u a t e I n h e r i t e d A t t r i b u t e ( SgNode ∗ , I n h N e s t i n g L e v e l ) ;
63      / / ! computes t h e n e s t i n g depth
64      SynNestingDepth e v a l u a t e S y n t h e s i z e d A t t r i b u t e ( SgNode ∗ , I n h N e s t i n g L e v e l , S y n t h e s i z e d A t t r i b u t e s L i s t ) ;
65      / / ! p r o v i d e s t h e d e f a u l t v a l u e 0 f o r t h e n e s t i n g depth
66      SynNestingDepth d e f a u l t S y n t h e s i z e d A t t r i b u t e ( I n h N e s t i n g L e v e l i n h ) ;
67   private :
68      NestingLevel maxNestingLevel ;
69   };
70
71
72   NestingLevel
73   L o o p L e v e l P r o c e s s i n g : : e v a l u a t e I n h e r i t e d A t t r i b u t e ( SgNode∗ node ,   NestingLevel loopNestingLevel ) {
74
75        / / ! compute maximum n e s t i n g l e v e l o f e n t i r e program i n a c c u m u l a t o r ( member v a r i a b l e )
76        i f ( loopNestingLevel > maxNestingLevel )
77             m a x N e s t i n g L e v e l=l o o p N e s t i n g L e v e l ;
78
79        s w i t c h ( node− a r i a n t T ( ) ) {
                             >v
80        c a s e V SgGotoStatement :
     7.2. TRAVERSALS OF THE AST STRUCTURE                                                                                                                            59


 1            c o u t << ”WARNING: Goto s t a t e m e n t f o u n d . We do n o t c o n s i d e r g o t o l o o p s . \ n ” ;
 2       // DQ ( 1 1 / 1 7 / 2 0 0 5 ) : Added r e t u r n s t a t m e n t t o a v o i d g++ w a r n i n g : c o n t r o l r e a c h e s end o f non−v o i d f u n c t i o n
 3            return loopNestingLevel ;
 4            break ;
 5        c a s e V SgDoWhileStmt :
 6        c a s e V SgForStatement :
 7        c a s e V SgWhileStmt :
 8            r e t u r n l o o p N e s t i n g L e v e l +1;
 9        default :
10            return loopNestingLevel ;
11        }
12   }
13
14   SynNestingDepth
15   LoopLevelProcessing : : d e f a u l t S y n t h e s i z e d A t t r i b u t e ( InhNestingLevel inh ) {
16     / ∗ ! we do no t need t h e i n h e r i t e d a t t r i b u t e h e r e
17           a s d e f a u l t v a l u e f o r s y n t h e s i z e d a t t r i b u t e we s e t 0 , r e p r e s e n t i n g n e s t i n g depth 0 .
18     ∗/
19     return 0;
20   }
21
22   SynNestingDepth
23   L o o p L e v e l P r o c e s s i n g : : e v a l u a t e S y n t h e s i z e d A t t r i b u t e ( SgNode∗ node , I n h N e s t i n g L e v e l n e s t i n g L e v e l , S y n t h e s i z e d A t t r i b u t e s L i s t   l)
24        {
25            i f ( nestingLevel > maxNestingLevel )
26                       m a x N e s t i n g L e v e l=n e s t i n g L e v e l ;
27
28        // compute maximum n e s t i n g depth o f s y n t h e s i z e d a t t r i b u t e s
29           SynNestingDepth n e s t i n g D e p t h =0;
30           f o r ( S y n t h e s i z e d A t t r i b u t e s L i s t : : i t e r a t o r i=l . b e g i n ( ) ; i != l . end ( ) ; i ++)
31                 {
32                     i f ( ∗ i >n e s t i n g D e p t h ) n e s t i n g D e p t h=∗ i ;
33                 }
34
35                                  >v
               s w i t c h ( node− a r i a n t T ( ) )
36                   {
37                       c a s e V SgDoWhileStmt :
38                       c a s e V SgForStatement :
39                       c a s e V SgWhileStmt :
40                             {
41                               n e s t i n g D e p t h ++;
42                               c o u t << ” N e s t i n g l e v e l : ” << n e s t i n g L e v e l << ” , n e s t i n g depth : ” << n e s t i n g D e p t h << e n d l ;
43                               break ;
44                             }
45
46                       default :
47                          {
48                         // DQ ( 1 1 / 1 7 / 2 0 0 5 ) : Nothing t o do h e r e , but e x p l i c i t                        default in switch avoids                        lots     of warnings .
49                          }
50                   }
51
52        // add l o o p n e s t i n g l e v e l a s a n n o t a t i o n t o AST
53           N e s t i n g L e v e l A n n o t a t i o n ∗ n l a = new N e s t i n g L e v e l A n n o t a t i o n ( n e s t i n g L e v e l , n e s t i n g D e p t h ) ;
54           ROSE ASSERT( n l a != NULL ) ;
55
56      // DQ ( 1 / 2 / 2 0 0 6 ) : Added s u p p o r t f o r new a t t r i b u t e i n t e r f a c e .
57      // p r i n t f ( ” L o o p L e v e l P r o c e s s i n g : : e v a l u a t e S y n t h e s i z e d A t t r i b u t e ( ) : u s i n g new a t t r i b u t e        i n t e r f a c e \n ” ) ;
58   #i f 0
59            i f ( node−   >g e t a t t r i b u t e ( ) == NULL)
60                {
61                   A s t A t t r i b u t e M e c h a n i s m ∗ a t t r i b u t e P t r = new A s t A t t r i b u t e M e c h a n i s m ( ) ;
62                   ROSE ASSERT( a t t r i b u t e P t r != NULL ) ;
63                           >s
                     node− e t a t t r i b u t e ( a t t r i b u t e P t r ) ;
64                }
65   #e n d i f
66                >a
     // node− t t r i b u t e . add ( ” l o o p N e s t i n g I n f o ” , n l a ) ;
67                >a
     // node− t t r i b u t e ( ) . add ( ” l o o p N e s t i n g I n f o ” , n l a ) ;
68       node−    >addNewAttribute ( ” l o o p N e s t i n g I n f o ” , n l a ) ;
69
70        / / ! r e t u r n t h e maximum n e s t i n g depth a s s y n t h e s i z e d                    attribute
71        return nestingDepth ;
72   }
73
74   i n t main ( i n t a r g c , c h a r ∗∗ a r g v ) {
75
76          // command l i n e p a r a m e t e r s a r e p a s s e d t o EDG
77          // non−      EDG p a r a m e t e r s a r e p a s s e d ( t h r o u g h ) t o ROSE ( and t h e v e n d o r c o m p i l e r )
78          S g P r o j e c t ∗ r o o t=f r o n t e n d ( a r g c , a r g v ) ;
79          LoopLevelProcessing t ;



     Figure 7.22: Example source code showing use of inherited, synthesized, accumulator, and per-
     60                                                   CHAPTER 7. INTRODUCTION TO AST TRAVERSALS




 1
 2   Output :
 3   Nesting       level :5 ,   n e s t i n g depth : 1
 4   Nesting       level :4 ,   n e s t i n g depth : 2
 5   Nesting       level :3 ,   n e s t i n g depth : 3
 6   Nesting       level :2 ,   n e s t i n g depth : 4
 7   Nesting       level :1 ,   n e s t i n g depth : 5
 8   Nesting       level :2 ,   n e s t i n g depth : 1
 9   Nesting       level :1 ,   n e s t i n g depth : 2
10   Nesting       level :1 ,   n e s t i n g depth : 1
11   Max l o o p    nesting     level : 5



     Figure 7.23: Output code showing the result of using inherited, synthesized, and accumulator
     attributes.
     7.2. TRAVERSALS OF THE AST STRUCTURE                                                                                                              61




 1   #i n c l u d e <r o s e . h>
 2
 3   c l a s s NodeTypeCounter : p u b l i c A s t S i m p l e P r o c e s s i n g {
 4   public :
 5           NodeTypeCounter ( enum VariantT v a r i a n t , s t d : : s t r i n g typeName )
 6               : myVariant ( v a r i a n t ) , typeName ( typeName ) , c o u n t ( 0 ) {
 7           }
 8
 9   protected :
10       v i r t u a l v o i d v i s i t ( SgNode ∗ node ) {
11               i f ( node− a r i a n t T ( ) == myVariant ) {
                                >v
12                     s t d : : c o u t << ”Found ” << typeName << s t d : : e n d l ;
13                     c o u n t++;
14               }
15       }
16
17         v i r t u a l void atTraversalEnd ( ) {
18                 s t d : : c o u t << typeName << ” t o t a l : ” << c o u n t << s t d : : e n d l ;
19         }
20
21   private :
22       enum VariantT myVariant ;
23       s t d : : s t r i n g typeName ;
24       unsigned i n t count ;
25   };
26
27   i n t main ( i n t a r g c , c h a r ∗∗ a r g v ) {
28         SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
29
30         s t d : : c o u t << ” s e q u e n t i a l e x e c u t i o n o f t r a v e r s a l s ” << s t d : : e n d l ;
31         NodeTypeCounter f o r S t a t e m e n t C o u n t e r ( V SgForStatement , ” f o r l o o p ” ) ;
32         NodeTypeCounter i n t V a l u e C o u n t e r ( V SgIntVal , ” i n t c o n s t a n t ” ) ;
33         NodeTypeCounter v a r D e c l C o u n t e r ( V S g V a r i a b l e D e c l a r a t i o n , ” v a r i a b l e d e c l a r a t i o n ” ) ;
34         // t h r e e c a l l s t o t r a v e r s e , e x e c u t e d s e q u e n t i a l l y
35         forStatementCounter . t r a v e r s e I n p u t F i l e s ( project , preorder ) ;
36         intValueCounter . t r a v e r s e I n p u t F i l e s ( project , preorder ) ;
37         varDeclCounter . t r a v e r s e I n p u t F i l e s ( p r o j e ct , preorder ) ;
38         s t d : : c o u t << s t d : : e n d l ;
39
40         s t d : : c o u t << ” combined e x e c u t i o n o f t r a v e r s a l s ” << s t d : : e n d l ;
41         AstCombinedSimpleProcessing combinedTraversal ;
42         c o m b i n e d T r a v e r s a l . a d d T r a v e r s a l ( new NodeTypeCounter ( V SgForStatement , ” f o r l o o p ” ) ) ;
43         c o m b i n e d T r a v e r s a l . a d d T r a v e r s a l ( new NodeTypeCounter ( V SgIntVal , ” i n t c o n s t a n t ” ) ) ;
44         c o m b i n e d T r a v e r s a l . a d d T r a v e r s a l ( new NodeTypeCounter ( V S g V a r i a b l e D e c l a r a t i o n , ” v a r i a b l e   declaration ”));
45         // one c a l l t o t r a v e r s e , e x e c u t i o n i s i n t e r l e a v e d
46         combinedTraversal . t r a v e r s e I n p u t F i l e s ( project , preorder ) ;
47   }



                         Figure 7.24: Example source showing the combination of traversals.
     62                                                   CHAPTER 7. INTRODUCTION TO AST TRAVERSALS




 1   sequential execution of traversals
 2   Found f o r l o o p
 3   Found f o r l o o p
 4   f o r loop t o t a l : 2
 5   Found i n t c o n s t a n t
 6   Found i n t c o n s t a n t
 7   Found i n t c o n s t a n t
 8   Found i n t c o n s t a n t
 9   Found i n t c o n s t a n t
10   Found i n t c o n s t a n t
11   Found i n t c o n s t a n t
12   Found i n t c o n s t a n t
13   Found i n t c o n s t a n t
14   Found i n t c o n s t a n t
15   i n t c o n s t a n t t o t a l : 10
16   Found v a r i a b l e d e c l a r a t i o n
17   Found v a r i a b l e d e c l a r a t i o n
18   Found v a r i a b l e d e c l a r a t i o n
19   Found v a r i a b l e d e c l a r a t i o n
20   Found v a r i a b l e d e c l a r a t i o n
21   Found v a r i a b l e d e c l a r a t i o n
22   Found v a r i a b l e d e c l a r a t i o n
23   Found v a r i a b l e d e c l a r a t i o n
24   variable declaration total : 8
25
26   combined e x e c u t i o n o f t r a v e r s a l s
27   Found v a r i a b l e d e c l a r a t i o n
28   Found i n t c o n s t a n t
29   Found v a r i a b l e d e c l a r a t i o n
30   Found f o r l o o p
31   Found v a r i a b l e d e c l a r a t i o n
32   Found i n t c o n s t a n t
33   Found i n t c o n s t a n t
34   Found v a r i a b l e d e c l a r a t i o n
35   Found i n t c o n s t a n t
36   Found i n t c o n s t a n t
37   Found v a r i a b l e d e c l a r a t i o n
38   Found i n t c o n s t a n t
39   Found v a r i a b l e d e c l a r a t i o n
40   Found i n t c o n s t a n t
41   Found f o r l o o p
42   Found v a r i a b l e d e c l a r a t i o n
43   Found i n t c o n s t a n t
44   Found i n t c o n s t a n t
45   Found v a r i a b l e d e c l a r a t i o n
46   Found i n t c o n s t a n t
47   f o r loop t o t a l : 2
48   i n t c o n s t a n t t o t a l : 10
49   variable declaration total : 8



     Figure 7.25: Output of input file to the combined traversals. Note that the order of outputs
     changes as execution of several analyzers is interleaved.
     7.2. TRAVERSALS OF THE AST STRUCTURE                                                                                        63

     7.2.12         Short-Circuiting Traversals
     The traversal short-circuit mechanism is a simple way to cut short the traversal of a large AST
     once specific information has been obtained. It is purely an optimization mechanism, and a bit
     of a hack, but common within the C++ Boost community. Since the technique works we present
     it as a way of permitting users to avoid the full traversal of an AST that they might deam
     to be redundant of inappropriate. We don’t expect that this mechanism will be particularly
     useful to most users and we don’t recommend it. It may even at some point not be supported.
     However, we present it because it is a common technique used in the C++ Boost community
     and it happens to work (at one point it didn’t work and so we have no idea what we fixed that
     permitted it to work now). We have regarded this technique as a rather ugly hack. It is presented
     in case you really need it. It is, we think, better than the direct use of lower level mechanisms
     that are used to support the AST traversal.

 1
 2   // I n p u t f o r   t r a n s l a t o r t o show e x c e p t i o n −b a s e d e x i t i n g from a t r a n s l a t o r .
 3
 4   namespace A
 5   {
 6      int    go  ;
 7      struct B
 8      {
 9         static int           stop      ;
10      };
11   };
12
13   void foo ( void )
14   {
15     e x t e r n void bar ( i n t ) ;
16     b a r (A : :     go  );
17     b a r (A : : B : :  stop   );
18   }



         Figure 7.26: Input code with used to demonstrate the traversal short-circuit mechanism.

         Figure 7.27 shows the example code demonstrating a traversal setup to support the short-
     circuit mechanism (a conventional mechanism used often within the C++ Boost community).
     The input code shown in figure 7.26 is compiled using the example translator, the output is
     shown in figure 7.28.
         The output shown in figure 7.28 demonstrates the initiation of a traversal over the AST and
     that traversal being short-circuited after a specific point in the evaluation. The result is that
     there is no further traversal of the AST after that point where it is short-circuited.
     64                                                CHAPTER 7. INTRODUCTION TO AST TRAVERSALS


 1   // Example o f an AST t r a v e r s a l t h a t u s e s t h e Boost i d i o m o f t h r o w i n g
 2   // an e x c e p t i o n t o e x i t t h e t r a v e r s a l e a r l y .
 3
 4   #i n c l u d e <r o s e . h>
 5   #i n c l u d e <s t r i n g >
 6   #i n c l u d e <i o s t r e a m >
 7
 8   u s i n g namespace s t d ;
 9
10   // E x c e p t i o n t o i n d i c a t e an e a r l y e x i t from a t r a v e r s a l a t some node .
11   c l a s s StopEarly
12   {
13   public :
14       S t o p E a r l y ( c o n s t SgNode∗ n ) : e x i t n o d e          ( n ) {}
15       S t o p E a r l y ( c o n s t S t o p E a r l y& e ) : e x i t n o d e    ( e . e x i t n o d e ) {}
16
17      // P r i n t s i n f o r m a t i o n about t h e e x i t node .
18      v o i d p r i n t ( o s t r e a m& o ) c o n s t
19      {
20          i f ( exit node ) {
21             o << ’ \ t ’ << ( c o n s t v o i d ∗ ) e x i t n o d e << ” : ” << e x i t n o d e − l a s s n a m e ( ) << e n d l ;
                                                                                                        >c
22             c o n s t SgLocatedNode ∗ l o c n = i s S g L o c a t e d N o d e ( e x i t n o d e ) ;
23             i f ( loc n ) {
24                 const S g F i l e I n f o ∗ info = loc n− et startOfConstruct ( ) ;
                                                                    >g
25                ROSE ASSERT ( i n f o ) ;
26                 o << ”\ tAt ” << i n f o − e t f i l e n a m e ( ) << ” : ” << i n f o −
                                                    >g                                            >g e t l i n e ( ) << e n d l ;
27             }
28         }
29      }
30
31   private :
32      c o n s t SgNode∗ e x i t n o d e ; // Node a t e a r l y         e x i t from t r a v e r s a l
33   };
34
35   // P r e o r d e r t r a v e r s a l t o f i n d t h e f i r s t SgVarRefExp o f a p a r t i c u l a r name .
36   c l a s s VarRefFinderTraversal : public AstSimpleProcessing
37   {
38   public :
39       // I n i t i a t e t r a v e r s a l t o f i n d ’ t a r g e t ’ i n ’ p r o j ’ .
40       v o i d f i n d ( S g P r o j e c t ∗ p r o j , c o n s t s t r i n g& t a r g e t )
41       {
42           target = target ;
43           t r a v e r s e I n p u t F i l e s ( proj , preorder ) ;
44       }
45
46      v o i d v i s i t ( SgNode∗ node )
47      {
48          c o n s t SgVarRefExp ∗ r e f = isSgVarRefExp ( node ) ;
49          if ( ref ) {
50              c o n s t S g V a r i a b l e S y m b o l ∗ sym = r e f − e t s y m b o l ( ) ;
                                                                         >g
51             ROSE ASSERT ( sym ) ;
52              c o u t << ” V i s i t i n g SgVarRef ’ ” << sym−           >get name ( ) . s t r ( ) << ” ’ ” << e n d l ;
53              i f ( sym−   >get name ( ) . s t r ( ) == t a r g e t ) // E a r l y e x i t a t f i r s t match .
54                  throw S t o p E a r l y ( r e f ) ;
55         }
56      }
57
58   private :
59      string       t a r g e t ; // Symbol r e f e r e n c e name t o f i n d .
60   };
61
62   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
63   {
64       SgProject ∗ p r o j = frontend ( argc , argv ) ;
65       VarRefFinderTraversal f i n d e r ;
66
67      // Look f o r a r e f e r e n c e t o ”        stop     ”.
68      try {
69        f i n d e r . f i n d ( proj , ” s t o p     ”);
70        c o u t << ”∗∗∗ R e f e r e n c e t o a      symbol    ’   stop     ’ n o t f o u n d . ∗∗∗” << e n d l ;
71      } c a t c h ( S t o p E a r l y& s t o p ) {
72        c o u t << ”∗∗∗ R e f e r e n c e t o a      symbol    ’   stop     ’ f o u n d . ∗∗∗” << e n d l ;
73        stop . p r i n t ( cout ) ;
74      }
75
76      // Look f o r a r e f e r e n c e t o ” g o   ”.
77      try {
78        f i n d e r . f i n d ( proj , ” g o  ”);
79        c o u t << ”∗∗∗ R e f e r e n c e t o a symbol         ’   go     ’ n o t f o u n d . ∗∗∗” << e n d l ;
80      } c a t c h ( S t o p E a r l y& go ) {
81        c o u t << ”∗∗∗ R e f e r e n c e t o a symbol         ’   go     ’ f o u n d . ∗∗∗” << e n d l ;
82        go . p r i n t ( c o u t ) ;
83      }
    7.2. TRAVERSALS OF THE AST STRUCTURE                                                                                                         65




1   V i s i t i n g SgVarRef ’ g o           ’
2   V i s i t i n g SgVarRef ’ s t o p         ’
3   ∗∗∗ R e f e r e n c e t o a symbol ’ s t o p            ’ f o u n d . ∗∗∗
4                  0 xd47d4e8 : SgVarRefExp
5                                                                                                −d
                   At / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t u t o r i a l / i n p u t C o d e t r a v e r s a l
6   V i s i t i n g SgVarRef ’ g o           ’
7   ∗∗∗ R e f e r e n c e t o a symbol ’ g o            ’ f o u n d . ∗∗∗
8                  0 xd47d480 : SgVarRefExp
9                                                                                                −d
                   At / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t u t o r i a l / i n p u t C o d e t r a v e r s a l



                 Figure 7.28: Output code showing the result of short-circuiting the traversal.
66                                  CHAPTER 7. INTRODUCTION TO AST TRAVERSALS

7.3       Memory Pool Traversals
Allocation of IR nodes in ROSE is made more efficient through the use of specialized alloca-
tors implemented at member function new operators for each class of the IR in Sage III. Such
specialized memory allocators avoid significant fragmentation of memory, provide more efficient
packing of memory, improve performance of allocation of memory and IR node access, and ad-
ditionally provide a secondary mechanism to accessing all the IR nodes. Each IR node has a
memory pool which is an STL vector of blocks (a fixed or variable sized array of contiguously
stored IR nodes).
    The three types of traversals are:

     1. ROSE Memory Pool Visit Traversal
        This traversal is similar to the one provided by the SimpleProcessing Class (using the
        visit() function and no inherited or synthesized attributes).

     2. Classic Object-Oriented Visitor Pattern for Memory Pool
        This is a classic object-oriented visitor pattern.

     3. IR node type traversal, visits one type of IR node for all IR types in the AST. This is
        useful for building specialized tools.

7.3.1      ROSE Memory Pool Visit Traversal
Figure 7.29 shows the source code for a translator which traverses the memory pool containing the
AST. At each node the visit() function is called using only the input information represented
by the current node. Note that using this simple traversal no context information is available
to the visit function. All the IR nodes for a given memory pool are iterated over at one time.
The order of the traversal of the different memory pools is random but fixed. Thus the order
of the traversal of the IR nodes is in no way connected to the structure of the AST (unlike the
previous non-memory pool traversals that were very much tied to the structure of the AST and
which matched the structure of the original input source code being compiled).
     7.3. MEMORY POOL TRAVERSALS                                                                                                             67




 1
 2   #i n c l u d e ” r o s e . h”
 3
 4   // ROSE V i s i t T r a v e r s a l ( s i m i l a r i n t e r f a c e a s Markus ’ s v i s i t t r a v e r s a l )
 5   // i n ROSE ( implemented u s i n g t h e t r a v e r s a l o v e r
 6   // t h e e l e m e n t s s t o r e d i n t h e memory p o o l s s o i t ha s no c y c l e s and v i s i t s
 7   // ALL IR n o d e s ( i n c l u d i n g a l l S g F i l e I n f o , SgSymbols , SgTypes , and t h e
 8   // s t a t i c b u i l t i n SgTypes ) .
 9   c l a s s RoseVisitor : public ROSE VisitTraversal
10         {
11            public :
12                  int counter ;
13                  v o i d v i s i t ( SgNode∗ node ) ;
14
15                     RoseVisitor ()         : c o u n t e r ( 0 ) {}
16        };
17
18
19   v o i d R o s e V i s i t o r : : v i s i t ( SgNode∗ node )
20         {
21      // p r i n t f ( ” r o s e V i s i t o r : : v i s i t : c o u n t e r %4d node = %s \n ” , c o u n t e r , node− l a s s n a m e ( ) . c s t r ( ) ) ;
                                                                                                                         >c
22           c o u n t e r ++;
23         }
24
25   int
26   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
27       {
28         SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
29         ROSE ASSERT ( p r o j e c t != NULL ) ;
30
31      // ROSE v i s i t t r a v e r s a l
32         RoseVisitor v i s i t o r ;
33         v i s i t o r . traverseMemoryPool ( ) ;
34
35             p r i n t f ( ” Number o f IR n o d e s i n AST = %d \n ” , v i s i t o r . c o u n t e r ) ;
36
37             r e t u r n backend ( p r o j e c t ) ;
38        }



               Figure 7.29: Example source showing simple visit traversal over the memory pools.




 1   Number o f IR n o d e s i n AST = 7705



                  Figure 7.30: Output of input file to the visitor traversal over the memory pool.
     68                                                   CHAPTER 7. INTRODUCTION TO AST TRAVERSALS

     7.3.2           Classic Object-Oriented Visitor Pattern for Memory Pool
     Figure 7.31 shows the source code for a translator which traverses the memory pools containing
     the AST. At each node the visit() function is called using only the input information rep-
     resented by the current node. Note that using this simple traversal no context information is
     available to the visit function. The traversal order is the same as in the 7.29.

 1
 2   #i n c l u d e ” r o s e . h”
 3
 4   // C l a s s i c V i s i t o r P a t t e r n i n ROSE ( implemented u s i n g t h e t r a v e r s a l o v e r
 5   // t h e e l e m e n t s s t o r e d i n t h e memory p o o l s s o i t ha s no c y c l e s and v i s i t s
 6   // ALL IR n o d e s ( i n c l u d i n g a l l S g F i l e I n f o , SgSymbols , SgTypes , and t h e
 7   // s t a t i c b u i l t i n SgTypes ) .
 8   c l a s s C l a s s i c V i s i t o r : p u b l i c ROSE VisitorPattern
 9         {
10            public :
11              // O v e r r i d e v i r t u r a l f u n c t i o n d e f i n e d i n b a s e c l a s s
12                    void v i s i t ( SgGlobal ∗ g l o b a l S c o p e )
13                           {
14                              p r i n t f ( ” Found t h e S g G l o b a l IR node \n ” ) ;
15                           }
16
17                      void v i s i t ( SgFunctionDeclaration ∗ functionDeclaration )
18                            {
19                              p r i n t f ( ” Found a S g F u n c t i o n D e c l a r a t i o n IR node \n ” ) ;
20                            }
21                      v o i d v i s i t ( SgTypeInt ∗ i n t T y p e )
22                            {
23                              p r i n t f ( ” Found a SgTypeInt IR node \n ” ) ;
24                            }
25
26                      v o i d v i s i t ( SgTypeDouble ∗ doubleType )
27                            {
28                              p r i n t f ( ” Found a SgTypeDouble IR node \n ” ) ;
29                            }
30         };
31
32
33   int
34   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
35       {
36         SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
37         ROSE ASSERT ( p r o j e c t != NULL ) ;
38
39        // C l a s s i c v i s i t o r p a t t e r n o v e r t h e memory p o o l o f IR n o d e s
40           ClassicVisitor visitor A ;
41           traverseMemoryPoolVisitorPattern ( v i s i t o r A ) ;
42
43              r e t u r n backend ( p r o j e c t ) ;
44         }



                                 Figure 7.31: Example source showing simple visitor pattern.
     7.3. MEMORY POOL TRAVERSALS                         69


 1   Found   a SgTypeInt IR node
 2   Found   a SgTypeDouble IR node
 3   Found   t h e S g G l o b a l IR node
 4   Found   a SgFunctionDeclaration         IR   node
 5   Found   a SgFunctionDeclaration         IR   node
 6   Found   a SgFunctionDeclaration         IR   node
 7   Found   a SgFunctionDeclaration         IR   node
 8   Found   a SgFunctionDeclaration         IR   node
 9   Found   a SgFunctionDeclaration         IR   node
10   Found   a SgFunctionDeclaration         IR   node
11   Found   a SgFunctionDeclaration         IR   node
12   Found   a SgFunctionDeclaration         IR   node
13   Found   a SgFunctionDeclaration         IR   node
14   Found   a SgFunctionDeclaration         IR   node
15   Found   a SgFunctionDeclaration         IR   node
16   Found   a SgFunctionDeclaration         IR   node
17   Found   a SgFunctionDeclaration         IR   node
18   Found   a SgFunctionDeclaration         IR   node
19   Found   a SgFunctionDeclaration         IR   node
20   Found   a SgFunctionDeclaration         IR   node
21   Found   a SgFunctionDeclaration         IR   node
22   Found   a SgFunctionDeclaration         IR   node
23   Found   a SgFunctionDeclaration         IR   node
24   Found   a SgFunctionDeclaration         IR   node
25   Found   a SgFunctionDeclaration         IR   node
26   Found   a SgFunctionDeclaration         IR   node
27   Found   a SgFunctionDeclaration         IR   node
28   Found   a SgFunctionDeclaration         IR   node
29   Found   a SgFunctionDeclaration         IR   node
30   Found   a SgFunctionDeclaration         IR   node
31   Found   a SgFunctionDeclaration         IR   node
32   Found   a SgFunctionDeclaration         IR   node
33   Found   a SgFunctionDeclaration         IR   node
34   Found   a SgFunctionDeclaration         IR   node
35   Found   a SgFunctionDeclaration         IR   node
36   Found   a SgFunctionDeclaration         IR   node
37   Found   a SgFunctionDeclaration         IR   node
38   Found   a SgFunctionDeclaration         IR   node
39   Found   a SgFunctionDeclaration         IR   node
40   Found   a SgFunctionDeclaration         IR   node
41   Found   a SgFunctionDeclaration         IR   node
42   Found   a SgFunctionDeclaration         IR   node
43   Found   a SgFunctionDeclaration         IR   node
44   Found   a SgFunctionDeclaration         IR   node
45   Found   a SgFunctionDeclaration         IR   node
46   Found   a SgFunctionDeclaration         IR   node
47   Found   a SgFunctionDeclaration         IR   node
48   Found   a SgFunctionDeclaration         IR   node
49   Found   a SgFunctionDeclaration         IR   node
50   Found   a SgFunctionDeclaration         IR   node
51   Found   a SgFunctionDeclaration         IR   node
52   Found   a SgFunctionDeclaration         IR   node
53   Found   a SgFunctionDeclaration         IR   node
54   Found   a SgFunctionDeclaration         IR   node
55   Found   a SgFunctionDeclaration         IR   node
56   Found   a SgFunctionDeclaration         IR   node
57   Found   a SgFunctionDeclaration         IR   node
58   Found   a SgFunctionDeclaration         IR   node
59   Found   a SgFunctionDeclaration         IR   node
60   Found   a SgFunctionDeclaration         IR   node
61   Found   a SgFunctionDeclaration         IR   node
62   Found   a SgFunctionDeclaration         IR   node
63   Found   a SgFunctionDeclaration         IR   node
64   Found   a SgFunctionDeclaration         IR   node
65   Found   a SgFunctionDeclaration         IR   node
66   Found   a SgFunctionDeclaration         IR   node
67   Found   a SgFunctionDeclaration         IR   node
68   Found   a SgFunctionDeclaration         IR   node
69   Found   a SgFunctionDeclaration         IR   node
70   Found   a SgFunctionDeclaration         IR   node
71   Found   a SgFunctionDeclaration         IR   node
72   Found   a SgFunctionDeclaration         IR   node
73   Found   a SgFunctionDeclaration         IR   node
74   Found   a SgFunctionDeclaration         IR   node
75   Found   a SgFunctionDeclaration         IR   node
76   Found   a SgFunctionDeclaration         IR   node
77   Found   a SgFunctionDeclaration         IR   node
78   Found   a SgFunctionDeclaration         IR   node
79   Found   a SgFunctionDeclaration         IR   node
80   Found   a SgFunctionDeclaration         IR   node
81   Found   a SgFunctionDeclaration         IR   node
82   Found   a SgFunctionDeclaration         IR   node
83   Found   a SgFunctionDeclaration         IR   node
70                                  CHAPTER 7. INTRODUCTION TO AST TRAVERSALS

7.3.3    ROSE IR Type Traversal (uses Memory Pools)
Figure 7.33 shows the source code for a translator which traverses only one type of IR node
using the memory pool containing the AST. This traversal is useful for building specialized tools
(often tools which only call static functions on each type of IR node).
    This example shows the use of an alternative traversal which traverses a representative of
each type or IR node just one, but only if it exists in the AST (memory pools). This sort of
traversal is useful for building tools that need only operate on static member functions of the IR
nodes or need only sample one of each type or IR node present in the AST. this specific example
also appears in: ROSE/src/midend/astDiagnostics/AstStatistics.C.
    The user’s use of the traversal is the same as for other ROSE AST traversals except that
the ROSE VisitTraversal::traverseRepresentativeIRnodes() member function is called instead of
ROSE VisitTraversal::traverseMemoryPool().
    This mechanism can be used to generate more complete reports of the memory consumption
of the AST, which is reported on if -rose:verbose 2 is used. Figure 7.35 shows a partial snapshot
of current IR node frequency and memory consumption for a moderate 40,000 line source code
file (one file calling a number of header files), sorted by memory consumption. The AST con-
tains approximately 280K IR nodes. Note that the Sg File Info IR nodes is most frequent and
consumes the greatest amount of memory, this reflects our bias toward preserving significant
information about the mapping of language constructs back to the positions in the source file to
support a rich set of source-to-source functionality. Note: more complete information about the
memory use of the AST in in the ROSE User Manual appendix.
     7.3. MEMORY POOL TRAVERSALS                                                                                                                           71


 1   // T h i s example c o d e shows t h e t r a v e r s a l                    o f IR t y p e s n o t a v a i l a b l e u s i n g t h e o t h e r t r a v e r s a l mechanism .
 2   #i n c l u d e ” r o s e . h”
 3   u s i n g namespace s t d ;
 4
 5   // CPP Macro t o implement c a s e f o r e a c h IR node ( we c o u l d a l t e r n a t i v e l y u s e a v i s i t o r p a t t e r n and a f u n c t i o n t e m p l a t e , ma
 6   #d e f i n e IR NODE VISIT CASE (X) \
 7                   c a s e V ## : \
                                    X
 8                         { \
 9                           X∗ c a s t N o d e = i s## node ) ; \
                                                          X(
10                           i n t numberOfNodes             = castNode− >numberOfNodes ( ) ; \
11                           i n t memoryFootprint = castNode−           >memoryUsage ( ) ; \
12                           p r i n t f ( ” c o u n t = %7d , memory u s e = %7d b y t e s , node name = %s \n ” , numberOfNodes , memoryFootprint , castNode−                    >c
13                           break ; \
14                         }
15
16   c l a s s RoseIRnodeVisitor : public ROSE VisitTraversal {
17            public :
18                 int counter ;
19                 v o i d v i s i t ( SgNode∗ node ) ;
20                 R o s e I R n o d e V i s i t o r ( ) : c o u n t e r ( 0 ) {}
21         };
22
23   void     RoseIRnodeVisitor : : v i s i t               ( SgNode∗ node )
24      {
25     //     Using a c l a s s i c v i s i t o r p a t t e r n s h o u l d a v o i d a l l t h i s c a s t i n g ,
26     //     but e a c h f u n c t i o n must be c r e a t e d s e p a r a t e l y ( s o i t i s wash i f
27     //     we want t o do a l l IR nodes , a s we do h e r e ) .
28                               >v
              s w i t c h ( node− a r i a n t T ( ) )
29                  {
30                      IR NODE VISIT CASE ( S g F i l e I n f o )
31                      IR NODE VISIT CASE ( S g P a r t i a l F u n c t i o n T y p e )
32                      IR NODE VISIT CASE ( SgFunctionType )
33                      IR NODE VISIT CASE ( S g P o i n t e r T y p e )
34                      IR NODE VISIT CASE ( S g F u n c t i o n D e c l a r a t i o n )
35                      IR NODE VISIT CASE ( SgFunctionSymbol )
36                      IR NODE VISIT CASE ( SgSymbolTable )
37                      IR NODE VISIT CASE ( S g I n i t i a l i z e d N a m e )
38                      IR NODE VISIT CASE ( S g S t o r a g e M o d i f i e r )
39                      IR NODE VISIT CASE ( Sg Fo rS ta t eme nt )
40                      IR NODE VISIT CASE ( S g F o r I n i t S t a t e m e n t )
41                      IR NODE VISIT CASE ( S g C t o r I n i t i a l i z e r L i s t )
42                      IR NODE VISIT CASE ( S g I f S t m t )
43                      IR NODE VISIT CASE ( SgExprStatement )
44                      IR NODE VISIT CASE ( S g T e m p l a t e D e c l a r a t i o n )
45                      IR NODE VISIT CASE ( S g T e m p l a t e I n s t a n t i a t i o n D e c l )
46                      IR NODE VISIT CASE ( S g T e m p l a t e I n s t a n t i a t i o n D e f n )
47                      IR NODE VISIT CASE ( S g T e m p l a t e I n s t a n t i a t i o n M e m b e r F u n c t i o n D e c l )
48                      IR NODE VISIT CASE ( SgClassSymbol )
49                      IR NODE VISIT CASE ( SgTemplateSymbol )
50                      IR NODE VISIT CASE ( SgMemberFunctionSymbol )
51
52                      default :
53                         {
54   #i f 0
55                               p r i n t f ( ” Case n o t h a n d l e d : %s \n ” , node− l a s s n a m e ( ) . c s t r ( ) ) ;
                                                                                           >c
56   #e n d i f
57                           }
58                  }
59        }
60
61
62   int
63   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
64       {
65     // ROSE v i s i t t r a v e r s a l
66         SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
67         ROSE ASSERT ( p r o j e c t != NULL ) ;
68
69       // ROSE v i s i t t r a v e r s a l
70          RoseIRnodeVisitor v i s i t o r ;
71          v i s i t o r . traverseRepresentativeIRnodes ( ) ;
72          p r i n t f ( ” Number o f t y p e s o f IR n o d e s ( a f t e r                  b u i l d i n g AST) = %d \n ” , v i s i t o r . c o u n t e r ) ;
73
74   #i f 1
75      // IR n o d e s s t a t i s t i c s
76            i f ( project − e t v e r b o s e ( ) > 1)
                               >g
77                   c o u t << A s t N o d e S t a t i s t i c s : : I R n o d e U s a g e S t a t i s t i c s ( ) ;
78   #e n d i f
79
80            i n t errorCode = 0 ;
81            e r r o r C o d e = backend ( p r o j e c t ) ;
82
83            return errorCode ;
     72                                                        CHAPTER 7. INTRODUCTION TO AST TRAVERSALS


 1   count =                2 4 , memory u s e =                3840 b y t e s        , node name = SgSymbolTable
 2   count =              7 5 0 , memory u s e =              30000 b y t e s         , node name = S g S t o r a g e M o d i f i e r
 3   count =            3 4 2 4 , memory u s e = 246528 b y t e s                     , node name = S g F i l e I n f o
 4   No r e p r e s e n t a t i v e f o r S g P a r t i a l F u n c t i o n T y p e    f o u n d i n memory p o o l s
 5   count =              3 9 9 , memory u s e =              47880 b y t e s         , node name = SgFunctionType
 6   count =                1 2 , memory u s e =                1152 b y t e s        , node name = S g P o i n t e r T y p e
 7   count =                  2 , memory u s e =                  576 b y t e s       , node name = Sg Fo rS ta t ement
 8   count =                  2 , memory u s e =                  208 b y t e s       , node name = S g F o r I n i t S t a t e m e n t
 9   count =                  4 , memory u s e =                2240 b y t e s        , node name = S g C t o r I n i t i a l i z e r L i s t
10   count =                  1 , memory u s e =                  296 b y t e s       , node name = S g I f S t m t
11   count =                  9 , memory u s e =                  792 b y t e s       , node name = SgExprStatement
12   count =                  6 , memory u s e =                3936 b y t e s        , node name = S g T e m p l a t e D e c l a r a t i o n
13   count =                  5 , memory u s e =                3760 b y t e s        , node name = S g T e m p l a t e I n s t a n t i a t i o n D e c l
14   count =                  1 , memory u s e =                  296 b y t e s       , node name = S g T e m p l a t e I n s t a n t i a t i o n D e f n
15   count =                  3 , memory u s e =                3024 b y t e s        , node name = S g T e m p l a t e I n s t a n t i a t i o n M e m b e r F u n c t i o n D e c l
16   count =              4 2 4 , memory u s e = 386688 b y t e s                     , node name = S g F u n c t i o n D e c l a r a t i o n
17   count =                  1 , memory u s e =                    48 b y t e s      , node name = SgClassSymbol
18   count =                  3 , memory u s e =                  144 b y t e s       , node name = SgTemplateSymbol
19   count =                  2 , memory u s e =                    96 b y t e s      , node name = SgMemberFunctionSymbol
20   count =              3 9 9 , memory u s e =              19152 b y t e s         , node name = SgFunctionSymbol
21   count =              7 5 0 , memory u s e = 222000 b y t e s                     , node name = S g I n i t i a l i z e d N a m e
22   Number o f t y p e s o f IR n o d e s ( a f t e r b u i l d i n g                 AST) = 0



                Figure 7.34: Output of input file to the IR Type traversal over the memory pool.

     AST   Memory   Pool   Statistics:      numberOfNodes       = 114081 memory consumption = 5019564 node = Sg_File_Info
     AST   Memory   Pool   Statistics:      numberOfNodes       = 31403 memory consumption = 628060 node = SgTypedefSeq
     AST   Memory   Pool   Statistics:      numberOfNodes       = 14254 memory consumption = 285080 node = SgStorageModifier
     AST   Memory   Pool   Statistics:      numberOfNodes       = 14254 memory consumption = 1140320 node = SgInitializedName
     AST   Memory   Pool   Statistics:      numberOfNodes       =   8458 memory consumption = 169160 node = SgFunctionParameterTypeList
     AST   Memory   Pool   Statistics:      numberOfNodes       =   7868 memory consumption = 1101520 node = SgModifierType
     AST   Memory   Pool   Statistics:      numberOfNodes       =   7657 memory consumption = 398164 node = SgClassType
     AST   Memory   Pool   Statistics:      numberOfNodes       =   7507 memory consumption = 2071932 node = SgClassDeclaration
     AST   Memory   Pool   Statistics:      numberOfNodes       =   7060 memory consumption = 282400 node = SgTemplateArgument
     AST   Memory   Pool   Statistics:      numberOfNodes       =   6024 memory consumption = 385536 node = SgPartialFunctionType
     AST   Memory   Pool   Statistics:      numberOfNodes       =   5985 memory consumption = 1388520 node = SgFunctionParameterList
     AST   Memory   Pool   Statistics:      numberOfNodes       =   4505 memory consumption = 1477640 node = SgTemplateInstantiationDecl
     AST   Memory   Pool   Statistics:      numberOfNodes       =   3697 memory consumption = 162668 node = SgReferenceType
     AST   Memory   Pool   Statistics:      numberOfNodes       =   3270 memory consumption = 758640 node = SgCtorInitializerList
     AST   Memory   Pool   Statistics:      numberOfNodes       =   3178 memory consumption =   76272 node = SgMemberFunctionSymbol
     AST   Memory   Pool   Statistics:      numberOfNodes       =   2713 memory consumption = 119372 node = SgPointerType
     AST   Memory   Pool   Statistics:      numberOfNodes       =   2688 memory consumption = 161280 node = SgThrowOp
     AST   Memory   Pool   Statistics:      numberOfNodes       =   2503 memory consumption =   60072 node = SgFunctionSymbol
     AST   Memory   Pool   Statistics:      numberOfNodes       =   2434 memory consumption = 107096 node = SgFunctionTypeSymbol
     AST   Memory   Pool   Statistics:      numberOfNodes       =   2418 memory consumption = 831792 node = SgFunctionDeclaration
     AST   Memory   Pool   Statistics:      numberOfNodes       =   2304 memory consumption =   55296 node = SgVariableSymbol
     AST   Memory   Pool   Statistics:      numberOfNodes       =   2298 memory consumption = 101112 node = SgVarRefExp
     AST   Memory   Pool   Statistics:      numberOfNodes       =   2195 memory consumption = 114140 node = SgSymbolTable
     AST   Memory   Pool   Statistics:      numberOfNodes       =   2072 memory consumption = 721056 node = SgMemberFunctionDeclaration
     AST   Memory   Pool   Statistics:      numberOfNodes       =   1668 memory consumption = 400320 node = SgVariableDeclaration
     AST   Memory   Pool   Statistics:      numberOfNodes       =   1667 memory consumption = 393412 node = SgVariableDefinition
     AST   Memory   Pool   Statistics:      numberOfNodes       =   1579 memory consumption = 101056 node = SgMemberFunctionType
     AST   Memory   Pool   Statistics:      numberOfNodes       =   1301 memory consumption =   31224 node = SgTemplateSymbol
     AST   Memory   Pool   Statistics:      numberOfNodes       =   1300 memory consumption = 364000 node = SgTemplateDeclaration
     AST   Memory   Pool   Statistics:      numberOfNodes       =   1198 memory consumption = 455240 node = SgTemplateInstantiationMemberFunctionDecl
     AST   Memory   Pool   Statistics:      numberOfNodes       =   1129 memory consumption =   54192 node = SgIntVal
     AST   Memory   Pool   Statistics:      numberOfNodes       =   1092 memory consumption =   56784 node = SgAssignInitializer
     AST   Memory   Pool   Statistics:      numberOfNodes       =   1006 memory consumption =   52312 node = SgExpressionRoot

     Truncated results presented ...



              Figure 7.35: Example of output using -rose:verbose 2 (memory use report for AST).
Chapter 8

Graph Processing Tutorial

8.1     Traversal Tutorial
ROSE can collect and analyze paths in both source and binary CFGs. At moment it doesn’t
attempt to save paths because if you save them directly the space necessary is extremely large,
as paths grow 2n with successive if statements and even faster when for loops are involved.
Currently a path can only cannot complete the same loop twice. However it is possible for a
graph such that 1 -¿ 2 , 2-¿3, 3-¿1, 3-¿5, has paths, 1,2,3,1,2,3,5 and 1,2,3,5 because the loop
1,2,3,1 is not repeated.
    The tutorial example works as such:




                                              73
     74                                                                     CHAPTER 8. GRAPH PROCESSING TUTORIAL


 1   #include <i o s t r e a m >
 2   #include <f s t r e a m >
 3   //#i n c l u d e <r o s e . h>
 4   #include <s t r i n g >
 5   #include <e r r . h>
 6   #include ” SgGraphTemplate . h”
 7   #include ” g r a p h P r o c e s s i n g . h”
 8
 9   #include ” staticCFG . h”
10   #include ” i n t e r p r o c e d u r a l C F G . h”
11   /∗ T e s t i n g t h e graph t r a v e r s a l mechanism now i mple menting i n A s t P r o c e s s i n g . h ( i n s i d e s r c /midend/ a s t P r o c e s s i n g /
12   #include <s y s / t i m e . h>
13   #include <s y s / r e s o u r c e . h>
14   using namespace s t d ;
15   using namespace b o o s t ;
16
17
18
19
20
21
22
23   typedef myGraph CFGforT ;
24
25
26
27
28
29
30   c l a s s v i s i t o r T r a v e r s a l : public S g G r a p h T r a v e r s a l <CFGforT>
31         {
32            public :
33                      int paths ;
34                     void a n a l y z e P a t h ( v e c t o r <VertexID>& pth ) ;
35         };
36
37
38   void v i s i t o r T r a v e r s a l : : a n a l y z e P a t h ( v e c t o r <VertexID>& pth ) {
39             #pragma omp a t o m i c
40              p a t h s ++;
41
42   }
43
44
45   i n t main ( i n t a r g c , char ∗ a r g v [ ] ) {
46
47       SgProject ∗ p r o j = frontend ( argc , argv ) ;
48       ROSE ASSERT ( p r o j != NULL ) ;
49
50        S g F u n c t i o n D e c l a r a t i o n ∗ mainDefDecl = S a g e I n t e r f a c e : : f i n d M a i n ( p r o j ) ;
51
52        S g F u n c t i o n D e f i n i t i o n ∗ mainDef = mainDefDecl−             >g e t d e f i n i t i o n ( ) ;
53          v i s i t o r T r a v e r s a l ∗ v i s = new v i s i t o r T r a v e r s a l ( ) ;
54           StaticCFG : : I n t e r p r o c e d u r a l C F G c f g ( mainDef ) ;
55           stringstream ss ;
56           S g I n c i d e n c e D i r e c t e d G r a p h ∗ g = new S g I n c i d e n c e D i r e c t e d G r a p h ( ) ;
57           g = c f g . getGraph ( ) ;
58           myGraph∗ mg = new myGraph ( ) ;
59           mg = i n s t a n t i a t e G r a p h ( g , c f g , mainDef ) ;
60           vis− ltnodes = 0;
                      >t
61           vis − aths = 0 ;
                      >p
62           // v i s − i r s t P r e p G r a p h ( c o n s t c f g ) ;
                          >f
63           v i s − o n s t r u c t P a t h A n a l y z e r (mg , true , 0 , 0 , true ) ;
                      >c
64           s t d : : c o u t << ” t o o k : ” << t i m e D i f f e r e n c e ( t2 , t 1 ) << s t d : : e n d l ;
65           // c f g . clearNodesAndEdges ( ) ;
66           s t d : : c o u t << ” f i n i s h e d ” << s t d : : e n d l ;
67           s t d : : c o u t << ” p a t h s : ” << v i s − a t h s << s t d : : e n d l ;
                                                                     >p
68           delete v i s ;
69   }



                                                 Figure 8.1: Source CFG Traversal Example
     8.1. TRAVERSAL TUTORIAL                                                                                                                                75


 1   #include <i o s t r e a m >
 2   #include <f s t r e a m >
 3   #include <r o s e . h>
 4   //#i n c l u d e ” interproceduralCFG . h”
 5   #include <s t r i n g >
 6   #include <e r r . h>
 7   #include ” g r a p h P r o c e s s i n g . h”
 8   #include ” B i n a r y C o n t r o l F l o w . h”
 9   #include ” B i n a r y L o a d e r . h”
10   /∗ T e s t i n g t h e graph t r a v e r s a l mechanism now i mple menting i n g r a p h P r o c e s s i n g . h ( i n s i d e s r c /midend/ a s t P r o c e s s i n g /) ∗/
11
12   using namespace s t d ;
13   using namespace b o o s t ;
14
15
16   typedef b o o s t : : g r a p h t r a i t s <B i n a r y A n a l y s i s : : C o n t r o l F l o w : : Graph > : : v e r t e x d e s c r i p t o r V e r t e x ;
     /∗∗< Graph v e r t e x t y p e . ∗/
17   typedef b o o s t : : g r a p h t r a i t s <B i n a r y A n a l y s i s : : C o n t r o l F l o w : : Graph > : : e d g e d e s c r i p t o r
     Edge ;     /∗∗< Graph ed g e t y p e . ∗/
18
19
20
21
22   c l a s s v i s i t o r T r a v e r s a l : public S g G r a p h T r a v e r s a l <B i n a r y A n a l y s i s : : C o n t r o l F l o w : : Graph>
23         {
24            public :
25                   long i n t p t h s ;
26                   long i n t t l t n o d e s ;
27
28                     v i r t u a l void a n a l y z e P a t h ( v e c t o r <Vertex>& pth ) ;
29
30
31        };
32
33
34
35   void v i s i t o r T r a v e r s a l : : a n a l y z e P a t h ( v e c t o r <Vertex>& pth ) {
36       // t l t n o d e s += p t h . s i z e ( ) ;
37       #pragma omp a t o m i c
38       p t h s ++;
39   }
40
41
42   i n t main ( i n t a r g c , char ∗ a r g v [ ] ) {
43
44          /∗ Parse t h e b i n a r y f i l e ∗/
45          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
46          s t d : : v e c t o r <S g A s m I n t e r p r e t a t i o n∗> i n t e r p s = S a g e I n t e r f a c e : : querySubTree<S g A s m I n t e r p r e t a t i o n >( p r o j e c t ) ;
47          i f ( i n t e r p s . empty ( ) ) {
48                  f p r i n t f ( s t d e r r , ” no b i n a r y i n t e r p r e t a t i o n s f o u n d \n” ) ;
49                  exit (1);
50          }
51
52          /∗ C a l c u l a t e p l a i n o l d CFG. ∗/
53              BinaryAnalysis : : ControlFlow c f g a n a l y z e r ;
54              B i n a r y A n a l y s i s : : C o n t r o l F l o w : : Graph∗ c f g = new B i n a r y A n a l y s i s : : C o n t r o l F l o w : : Graph ;
55
56                 c f g a n a l y z e r . b u i l d c f g f r o m a s t ( i n t e r p s . back ( ) , ∗ c f g ) ;
57                 s t d : : o f s t r e a m mf ;
58                 mf . open ( ” a n a l y s i s . d o t ” ) ;
59                 v i s i t o r T r a v e r s a l ∗ v i s = new v i s i t o r T r a v e r s a l ;
60                 vis− ltnodes = 0;
                           >t
61                 vis − ths = 0 ;
                           >p
62                 v i s − o n s t r u c t P a t h A n a l y z e r ( c f g , true , 0 , 0 , f a l s e ) ;
                           >c
63                 s t d : : c o u t << ” p t h s : ” << v i s − t h s << s t d : : e n d l ;
                                                                         >p
64                 s t d : : c o u t << ” t l t n o d e s : ” << v i s − l t n o d e s << s t d : : e n d l ;
                                                                              >t
65   }



                                               Figure 8.2: Binary CFG Traversal Example
76   CHAPTER 8. GRAPH PROCESSING TUTORIAL
Chapter 9

Scopes of Declarations

The scope of an IR node may be either stored explicitly in the IR node or obtained through
computation through its parent information in the AST. Figure X shows an example where the
variable definition for a variable is the scope of namespace X. The declaration for variable a is
in the namespace X. In a more common way, the function foo is a member function of B with a
declaration appearing in class B, but with a function definition in global scope.
namespace X{
    extern int a;
}
int X::a = 0;
class B
  {
     void foo();
  };
void B::foo() {}

    In C++, using name qualification the scope of a declaration can be independent of it struc-
tural location in the AST. The get parent() member function (available on most IR nodes)
communicates the structural information of the original source code (also represented in the
AST). The scope information must at times be stored explicitly when it can not be interpreted
structurally.
    The example in this chapter show how to find the scope of each C++ construct . Note that
SgExpression IR nodes can take their scope from that of the statement where they are found.
SgStatement and SgInitializedName IR nodes are the interesting IR nodes from the point of
scope.
    The SgInitializedName and all SgStatement IR nodes have a member function get scope()
which returns the scope of the associated IR nodes. The example code in this chapter traverses
the AST and reports the scope of any SgInitializedName and all SgStatement IR nodes. It
is intended to provide a simple intuition about what the scope can be expected to be in an
application. The example code is also useful as a simple means of exploring the scopes of any
other input application.


9.1       Input For Examples Showing Scope Information
Figure 9.1 shows the input example code form this tutorial example.

                                              77
     78                                             CHAPTER 9. SCOPES OF DECLARATIONS


 1
 2   i n t xyz ;
 3
 4   void foo ( i n t   x)
 5      {
 6        int y ;
 7        for ( int     i =0; i < 1 0 ;   i ++)
 8           {
 9              int     z;
10              z =     42;
11           }
12      }



          Figure 9.1: Example source code used as input to program in codes used in this chapter.


     9.2       Generating the code representing any IR node
     The following code traverses each IR node and for a SgInitializedName of SgStatement outputs
     the scope information. The input code is shown in figure 9.1; the output of this code is shown
     in figure 9.3.
     9.2. GENERATING THE CODE REPRESENTING ANY IR NODE                                                                                            79


 1   // T h i s example shows t h e s c o p e o f e a c h s t a t e m e n t and name ( v a r i a b l e names , b a s e c l a s s names ,                         etc . ) .
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   class      visitorTraversal            :   public AstSimpleProcessing
 6      {
 7             public :
 8                  v i r t u a l void      v i s i t ( SgNode∗ n ) ;
 9        };
10
11   v o i d v i s i t o r T r a v e r s a l : : v i s i t ( SgNode∗ n )
12         {
13      // There a r e t h r e e t y p e s i r IR n o d e s t h a t can be q u e r i e d f o r s c o p e :
14      //      − SgStatement , and
15      //      − SgInitializedName
16           SgStat ement ∗ s t a t e m e n t = i s S g S t a t e m e n t ( n ) ;
17           i f ( s t a t e m e n t != NULL)
18                 {
19                      SgScopeStatement ∗ scope = statement− e t s c o p e ( ) ;
                                                                               >g
20                     ROSE ASSERT( s c o p e != NULL ) ;
21                      p r i n t f ( ” S gS ta t ement              = %12p = %30 s h as s c o p e = %12p = %s ( t o t a l number = %d ) \n ” ,
22                                statement , statement− lass name ( ) . c s t r ( ) ,
                                                                   >c
23                                scope , scope− lass name ( ) . c s t r ( ) , ( i n t ) scope−
                                                        >c                                        >numberOfNodes ( ) ) ;
24                 }
25
26             SgInitializedName ∗ initializedName = isSgInitializedName (n ) ;
27             i f ( i n i t i a l i z e d N a m e != NULL)
28                 {
29                     SgScopeStatement ∗ scope = i n i t i a l i z e d N a m e − e t s c o p e ( ) ;
                                                                                         >g
30                     ROSE ASSERT( s c o p e != NULL ) ;
31                     p r i n t f ( ” S g I n i t i a l i z e d N a m e = %12p = %30 s h as s c o p e = %12p = %s ( t o t a l number = %d ) \ n ” ,
32                                 initializedName , initializedName−              >get name ( ) . s t r ( ) ,
33                                 scope , scope− lass name ( ) . c s t r ( ) , ( i n t ) scope−
                                                        >c                                            >numberOfNodes ( ) ) ;
34                 }
35        }
36
37   int
38   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
39       {
40         SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
41         ROSE ASSERT ( p r o j e c t != NULL ) ;
42
43      // B u i l d t h e t r a v e r s a l o b j e c t
44         v i s i t o r T r a v e r s a l exampleTraversal ;
45
46      // C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t node o f t h e AST
47         exampleTraversal . t r a v e r s e I n p u t F i l e s ( project , preorder ) ;
48
49             p r i n t f ( ” Number o f s c o p e s ( S g S c o p e S t a t e m e n t ) = %d \n ” , ( i n t ) S g S c o p e S t a t e m e n t : : numberOfNodes ( ) ) ;
50             p r i n t f ( ” Number o f s c o p e s ( S g B a s i c B l o c k )         = %d \n ” , ( i n t ) S g B a s i c B l o c k : : numberOfNodes ( ) ) ;
51
52   #i f 0
53            p r i n t f ( ” \ n\n ” ) ;
54            p r i n t f ( ”Now o u t p u t a l l t h e s y m b o l s i n e a c h symbol t a b l e \n ” ) ;
55            S a g e I n t e r f a c e : : outputLocalSymbolTables ( p r o j e c t ) ;
56            p r i n t f ( ” \ n\n ” ) ;
57   #e n d i f
58
59             return 0;
60        }



         Figure 9.2: Example source code showing how to get scope information for each IR node.
     80                                                                        CHAPTER 9. SCOPES OF DECLARATIONS




 1   SgStatement                       = 0 x2ab0395cf010 =                                                  S g G l o b a l ha s s c o p e   = 0 x2ab0395cf010 = SgGlobal ( t o t
 2   SgStatement                       = 0 x2ab03987b010 =                       S g V a r i a b l e D e c l a r a t i o n ha s s c o p e    = 0 x2ab0395cf010 = SgGlobal ( t o t
 3   S g I n i t i a l i z e d N a m e = 0 x 2a b0 39 8 18 4 38 =
     xyz has s c o p e = 0 x 2 a b 0 3 9 5 c f 0 1 0 = S g G l o b a l ( t o t a l number = 0 )
 4   SgStatement                       = 0 x2ab0396d8be0 =                       S g F u n c t i o n D e c l a r a t i o n ha s s c o p e    = 0 x2ab0395cf010 = SgGlobal ( t o t
 5   SgStatement                       = 0 x 2a b0 39 7 93 9 00 =            S g F u n c t i o n P a r a m e t e r L i s t ha s s c o p e    = 0 x2ab0395cf010 = SgGlobal ( t o t
 6   S g I n i t i a l i z e d N a m e = 0 x 2a b0 39 8 18 5 60 =
     x has s c o p e = 0 x2ab03999d010 = S g F u n c t i o n D e f i n i t i o n ( t o t a l number = 0 )
 7   SgStatement                       = 0 x2ab03999d010 =                         S g F u n c t i o n D e f i n i t i o n ha s s c o p e    = 0 x2ab0395cf010 = SgGlobal ( t o t
 8   SgStatement                       = 0 x2ab0399e6010 =                                         S g B a s i c B l o c k ha s s c o p e    = 0 x2ab03999d010 = S g F u n c t i o n D e
 9   SgStatement                       = 0 x2ab03987b288 =                       S g V a r i a b l e D e c l a r a t i o n ha s s c o p e    = 0 x2ab0399e6010 = SgBasicBlock
10   S g I n i t i a l i z e d N a m e = 0 x2 a b03 98 1 86 8 8 =
     y has s c o p e = 0 x 2 a b 0 3 9 9 e 6 0 1 0 = S g B a s i c B l o c k ( t o t a l number = 0 )
11   SgStatemen t                      = 0 x2 a b03 9a 2 90 1 0 =                              Sg Fo r Sta t ement ha s s c o p e            = 0 x2ab0399e6010 = SgBasicBlock
12   SgStatemen t                      =    0 x1f110080 =                          S g F o r I n i t S t a t e m e n t h as s c o p e =      0 x2 a b03 9a 2 9010 = SgForStatement
13   SgStatemen t                      = 0 x2ab03987b500 =                       S g V a r i a b l e D e c l a r a t i o n ha s s c o p e    = 0 x2 ab0 3 9a29010 = SgForStatemen
14   S g I n i t i a l i z e d N a m e = 0 x2ab0398187b0 =
     i has s c o p e = 0 x 2a b0 39 a 29 01 0 = Sg Fo rS ta t ement ( t o t a l number = 0 )
15   SgStatemen t                      =    0 x1f191550 =                               SgExprStatement has s c o p e =                      0 x2 a b03 9 a2 9010 = SgForStatement
16   SgStatemen t                      = 0 x2ab0399e6120 =                                         S g B a s i c B l o c k ha s s c o p e    = 0 x2 ab0 3 9a 29010 = SgForStatemen
17   SgStatemen t                      = 0 x2ab03987b778 =                       S g V a r i a b l e D e c l a r a t i o n ha s s c o p e    = 0 x2ab0399e6120 = SgBasicBlock
18   S g I n i t i a l i z e d N a m e = 0 x2ab0398188d8 =
     z has s c o p e = 0 x 2 a b 0 3 9 9 e 6 1 2 0 = S g B a s i c B l o c k ( t o t a l number = 0 )
19   SgStatemen t                      =    0 x1f1915a8 =                               SgExprStatement has s c o p e =                      0 x2ab0399e6120 = SgBasicBlock ( t
20   Number o f s c o p e s ( S g S c o p e S t a t e m e n t ) = 0
21   Number o f s c o p e s ( S g B a s i c B l o c k )          = 2



                                 Figure 9.3: Output of input code using scopeInformation.C
Chapter 10

AST Query

This chapter presents a mechanism for simple queries on the AST. Such queries are typically
a single line of code, instead of the class that must be declared and defined when using the
traversal mechanism. While the traversal mechanism is more sophisticated and more powerful,
the AST Query mechanism is particularly simple to use.


10.1      Simple Queries on the AST
This section demonstrates a simple query on the AST.
   The program in figure 10.1 calls an internal ROSE Query Library. Queries of the AST using
the query library are particularly simple and often are useful as nested queries within more
complex analysis. More information of the ROSE AST Query Library is available within ROSE
User Manual.
   Using the input program in figure 10.2 the translator processes the code and generates the
output in figure 10.3.                                                                                  FIXME: Put an example of
                                                                                                    composition of AST queries into
                                                                                                             the example input code.
10.2      Nested Query
This section demonstrates a nested AST query, showing how to use composition in the construc-
tion of more elaborate queries from simple ones.
    The number of traversals of the AST can be reduced by using nested queries. Nested queries
permits queries on the result from a NodeQuery. Another advantage is that nested (combined)
queries can be formed to query for information without writing new query, the nested query is
a new query.
    The program in figure 10.4 calls an internal ROSE Query Library. Two different queries are
performed to find all access functions within the AST. The first query is nested, the returned list
from a query is used in a traversal, and the second query queries the AST for the same nodes.
    Using the input program in figure 10.5 the translator processes the code and generates the
output in figure 10.6.




                                               81
     82                                                                                                      CHAPTER 10. AST QUERY




 1   // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   u s i n g namespace s t d ;
 6
 7   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 8        {
 9       // B u i l d t h e AST u s e d by ROSE
10          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
11          ROSE ASSERT( p r o j e c t != NULL ) ;
12
13        // B u i l d a l i s t o f f u n c t i o n s w i t h i n t h e AST
14           R o s e S T L C o n t a i n e r<SgNode∗> f u n c t i o n D e c l a r a t i o n L i s t = NodeQuery : : querySubTree ( p r o j e c t , V S g F u n c t i o n D e c l a r
15
16             int counter = 0;
17             f o r ( R o s e S T L C o n t a i n e r<SgNode ∗ > : : i t e r a t o r i = f u n c t i o n D e c l a r a t i o n L i s t . b e g i n ( ) ; i != f u n c t i o n D e c l a r a t i o n L
18                  {
19                 // B u i l d a p o i n t e r t o t h e c u r r e n t t y p e s o t h a t we can c a l l t h e get name ( ) member f u n c t i o n .
20                     SgFunctionDeclaration ∗ f u n c t i o n D e c l a r a t i o n = i sS gFu ncti o nDecl a ra ti o n (∗ i ) ;
21                    ROSE ASSERT( f u n c t i o n D e c l a r a t i o n != NULL ) ;
22
23                // DQ ( 3 / 5 / 2 0 0 6 ) : Only o u t p u t t h e non−c o m p i l e r g e n e r a t e d IR n o d e s
24                   i f ( ( ∗ i )−> g e t f i l e i n f o ()−> i s C o m p i l e r G e n e r a t e d ( ) == f a l s e )
25                       {
26                      // o u t p u t t h e f u n c t i o n number and t h e name o f t h e f u n c t i o n
27                          p r i n t f ( ” F u n c t i o n #%2d name i s %s a t l i n e %d \n ” ,
28                                    c o u n t e r ++, f u n c t i o n D e c l a r a t i o n −>get name ( ) . s t r ( ) ,
29                                    f u n c t i o n D e c l a r a t i o n − g e t f i l e i n f o ()−> g e t l i n e ( ) ) ;
                                                                             >
30                       }
31                      else
32                       {
33                      // Output s o m e t h i n g about t h e c o m p i l e r −g e n e r a t e d b u i l t i n f u n c t i o n s
34                          p r i n t f ( ” Compiler−g e n e r a t e d ( b u i l t i n ) f u n c t i o n #%2d name i s %s \n ” ,
35                                    c o u n t e r ++, f u n c t i o n D e c l a r a t i o n −>get name ( ) . s t r ( ) ) ;
36                       }
37                 }
38
39        // Note : Show c o m p o s i t i o n o f AST q u e r i e s
40
41             return 0;
42         }



     Figure 10.1: Example source code for translator to read an input program and generate a list of
     functions in the AST (queryLibraryExample.C).
     10.2. NESTED QUERY                                                                                           83




 1
 2   // Templated c l a s s d e c l a r a t i o n u s e d i n t e m p l a t e p a r a m e t e r example c o d e
 3   t e m p l a t e <typename T>
 4   c l a s s templateClass
 5         {
 6             public :
 7                    int x ;
 8
 9                      void foo ( i n t ) ;
10                      void foo ( double ) ;
11        };
12
13   // O v e r l o a d e d f u n c t i o n s    for    t e s t i n g overloaded function   resolution
14   void foo ( i n t ) ;
15   void foo ( double )
16      {
17         int x = 1;
18         int y ;
19
20      // Added t o a l l o w non− t r i v i a l CFG
21         i f (x)
22             y = 2;
23         else
24             y = 3;
25       }
26
27   i n t main ( )
28        {
29          foo (42);
30          foo (3.14159265);
31
32             t e m p l a t e C l a s s <char> i n s t a n t i a t e d C l a s s ;
33             instantiatedClass . foo ( 7 ) ;
34             instantiatedClass . foo ( 7 . 0 ) ;
35
36             f o r ( i n t i =0; i < 4 ;           i ++)
37                  {
38                     int x ;
39                  }
40
41             return 0;
42        }



     Figure 10.2: Example source code used as input to program in figure 10.1 (queryLibraryExam-
     ple.C).
     84                                                                                 CHAPTER 10. AST QUERY


 1   Compiler−g e n e r a t e d   (   builtin   )   function   # 0   name   is   builtin copysign
 2   Compiler−g e n e r a t e d   (   builtin   )   function   # 1   name   is   builtin copysignf
 3   Compiler−g e n e r a t e d   (   builtin   )   function   # 2   name   is   builtin copysignl
 4   Compiler−g e n e r a t e d   (   builtin   )   function   # 3   name   is   builtin acosf
 5   Compiler−g e n e r a t e d   (   builtin   )   function   # 4   name   is   builtin acosl
 6   Compiler−g e n e r a t e d   (   builtin   )   function   # 5   name   is   builtin asinf
 7   Compiler−g e n e r a t e d   (   builtin   )   function   # 6   name   is   builtin asinl
 8   Compiler−g e n e r a t e d   (   builtin   )   function   # 7   name   is   builtin atanf
 9   Compiler−g e n e r a t e d   (   builtin   )   function   # 8   name   is   builtin atanl
10   Compiler−g e n e r a t e d   (   builtin   )   function   # 9   name   is   builtin atan2f
11   Compiler−g e n e r a t e d   (   builtin   )   function   #10   name   is   builtin atan2l
12   Compiler−g e n e r a t e d   (   builtin   )   function   #11   name   is   builtin ceilf
13   Compiler−g e n e r a t e d   (   builtin   )   function   #12   name   is   builtin ceill
14   Compiler−g e n e r a t e d   (   builtin   )   function   #13   name   is   builtin coshf
15   Compiler−g e n e r a t e d   (   builtin   )   function   #14   name   is   builtin coshl
16   Compiler−g e n e r a t e d   (   builtin   )   function   #15   name   is   builtin floorf
17   Compiler−g e n e r a t e d   (   builtin   )   function   #16   name   is   builtin floorl
18   Compiler−g e n e r a t e d   (   builtin   )   function   #17   name   is   builtin fmodf
19   Compiler−g e n e r a t e d   (   builtin   )   function   #18   name   is   builtin fmodl
20   Compiler−g e n e r a t e d   (   builtin   )   function   #19   name   is   builtin frexpf
21   Compiler−g e n e r a t e d   (   builtin   )   function   #20   name   is   builtin frexpl
22   Compiler−g e n e r a t e d   (   builtin   )   function   #21   name   is   builtin ldexpf
23   Compiler−g e n e r a t e d   (   builtin   )   function   #22   name   is   builtin ldexpl
24   Compiler−g e n e r a t e d   (   builtin   )   function   #23   name   is   builtin log10f
25   Compiler−g e n e r a t e d   (   builtin   )   function   #24   name   is   builtin log10l
26   Compiler−g e n e r a t e d   (   builtin   )   function   #25   name   is   builtin modff
27   Compiler−g e n e r a t e d   (   builtin   )   function   #26   name   is   builtin modfl
28   Compiler−g e n e r a t e d   (   builtin   )   function   #27   name   is   builtin powf
29   Compiler−g e n e r a t e d   (   builtin   )   function   #28   name   is   builtin powl
30   Compiler−g e n e r a t e d   (   builtin   )   function   #29   name   is   builtin sinhf
31   Compiler−g e n e r a t e d   (   builtin   )   function   #30   name   is   builtin sinhl
32   Compiler−g e n e r a t e d   (   builtin   )   function   #31   name   is   builtin tanf
33   Compiler−g e n e r a t e d   (   builtin   )   function   #32   name   is   builtin tanl
34   Compiler−g e n e r a t e d   (   builtin   )   function   #33   name   is   builtin tanhf
35   Compiler−g e n e r a t e d   (   builtin   )   function   #34   name   is   builtin tanhl
36   Compiler−g e n e r a t e d   (   builtin   )   function   #35   name   is   builtin powil
37   Compiler−g e n e r a t e d   (   builtin   )   function   #36   name   is   builtin powi
38   Compiler−g e n e r a t e d   (   builtin   )   function   #37   name   is   builtin powif
39   Compiler−g e n e r a t e d   (   builtin   )   function   #38   name   is   builtin strchr
40   Compiler−g e n e r a t e d   (   builtin   )   function   #39   name   is   builtin strrchr
41   Compiler−g e n e r a t e d   (   builtin   )   function   #40   name   is   builtin strpbrk
42   Compiler−g e n e r a t e d   (   builtin   )   function   #41   name   is   builtin strstr
43   Compiler−g e n e r a t e d   (   builtin   )   function   #42   name   is   builtin nansf
44   Compiler−g e n e r a t e d   (   builtin   )   function   #43   name   is   builtin nans
45   Compiler−g e n e r a t e d   (   builtin   )   function   #44   name   is   builtin nansl
46   Compiler−g e n e r a t e d   (   builtin   )   function   #45   name   is   builtin fabs
47   Compiler−g e n e r a t e d   (   builtin   )   function   #46   name   is   builtin fabsf
48   Compiler−g e n e r a t e d   (   builtin   )   function   #47   name   is   builtin fabsl
49   Compiler−g e n e r a t e d   (   builtin   )   function   #48   name   is   builtin cosf
50   Compiler−g e n e r a t e d   (   builtin   )   function   #49   name   is   builtin cosl
51   Compiler−g e n e r a t e d   (   builtin   )   function   #50   name   is   builtin sinf
52   Compiler−g e n e r a t e d   (   builtin   )   function   #51   name   is   builtin sinl
53   Compiler−g e n e r a t e d   (   builtin   )   function   #52   name   is   builtin sqrtf
54   Compiler−g e n e r a t e d   (   builtin   )   function   #53   name   is   builtin sqrtl
55   Compiler−g e n e r a t e d   (   builtin   )   function   #54   name   is   builtin fpclassify
56   Compiler−g e n e r a t e d   (   builtin   )   function   #55   name   is   builtin return address
57   Compiler−g e n e r a t e d   (   builtin   )   function   #56   name   is   builtin frame address
58   Compiler−g e n e r a t e d   (   builtin   )   function   #57   name   is   builtin expect
59   Compiler−g e n e r a t e d   (   builtin   )   function   #58   name   is   builtin prefetch
60   Compiler−g e n e r a t e d   (   builtin   )   function   #59   name   is   builtin huge val
61   Compiler−g e n e r a t e d   (   builtin   )   function   #60   name   is   builtin huge valf
62   Compiler−g e n e r a t e d   (   builtin   )   function   #61   name   is   builtin huge vall
63   Compiler−g e n e r a t e d   (   builtin   )   function   #62   name   is   builtin inf
64   Compiler−g e n e r a t e d   (   builtin   )   function   #63   name   is   builtin inff
65   Compiler−g e n e r a t e d   (   builtin   )   function   #64   name   is   builtin infl
66   Compiler−g e n e r a t e d   (   builtin   )   function   #65   name   is   builtin nan
67   Compiler−g e n e r a t e d   (   builtin   )   function   #66   name   is   builtin nanf
68   Compiler−g e n e r a t e d   (   builtin   )   function   #67   name   is   builtin nanl
69   Compiler−g e n e r a t e d   (   builtin   )   function   #68   name   is   builtin nans
70   Compiler−g e n e r a t e d   (   builtin   )   function   #69   name   is   builtin nansf
71   Compiler−g e n e r a t e d   (   builtin   )   function   #70   name   is   builtin nansl
72   Compiler−g e n e r a t e d   (   builtin   )   function   #71   name   is   builtin clz
73   Compiler−g e n e r a t e d   (   builtin   )   function   #72   name   is   builtin ctz
74   Compiler−g e n e r a t e d   (   builtin   )   function   #73   name   is   builtin popcount
75   Compiler−g e n e r a t e d   (   builtin   )   function   #74   name   is   builtin parity
76   Compiler−g e n e r a t e d   (   builtin   )   function   #75   name   is   builtin ffsl
77   Compiler−g e n e r a t e d   (   builtin   )   function   #76   name   is   builtin clzl
78   Compiler−g e n e r a t e d   (   builtin   )   function   #77   name   is   builtin ctzl
79   Compiler−g e n e r a t e d   (   builtin   )   function   #78   name   is   builtin popcountl
80   Compiler−g e n e r a t e d   (   builtin   )   function   #79   name   is   builtin parityl
81   Compiler−g e n e r a t e d   (   builtin   )   function   #80   name   is   builtin ffsll
82   Compiler−g e n e r a t e d   (   builtin   )   function   #81   name   is   builtin clzll
83   Compiler−g e n e r a t e d   (   builtin   )   function   #82   name   is   builtin ctzll
     10.2. NESTED QUERY                                                                                                                                        85


 1   // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   u s i n g namespace s t d ;
 6
 7
 8    // F u n c t i o n q u e r y S o l v e r A c c e s s F u n c t i o n s ( )
 9    // f i n d a c c e s s f u n c t i o n s ( f u n c t i o n name s t a r t s w i t h ” g e t ” o r ” s e t ” )
10   NodeQuerySynthesizedAttributeType
11   q u e r y S o l v e r A c c e s s F u n c t i o n s ( SgNode ∗ astNode )
12        {
13            ROSE ASSERT ( astNode != 0 ) ;
14            NodeQuerySynthesizedAttributeType returnNodeList ;
15
16            S g F u n c t i o n D e c l a r a t i o n ∗ f u n c D e c l = i s S g F u n c t i o n D e c l a r a t i o n ( astNode ) ;
17
18            if    ( f u n c D e c l != NULL)
19                  {
20                      s t r i n g functionName = f u n c D e c l −             >get name ( ) . s t r ( ) ;
21                      i f ( ( functionName . l e n g t h ( ) >= 4 ) && ( ( functionName . s u b s t r ( 0 , 4 ) == ” g e t ” )                               ||    ( functionName . s u b s t r ( 0 , 4 ) == ” s e t
22                                r e t u r n N o d e L i s t . p u s h b a c k ( astNode ) ;
23                  }
24
25            return returnNodeList ;
26        }
27
28   // F u n c t i o n p r i n t F u n c t i o n D e c l a r a t i o n L i s t w i l l p r i n t a l l f u n c t i o n names i n t h e l i s t
29   v o i d p r i n t F u n c t i o n D e c l a r a t i o n L i s t ( R o s e S T L C o n t a i n e r<SgNode∗> f u n c t i o n D e c l a r a t i o n L i s t )
30         {
31           int counter = 0;
32           f o r ( R o s e S T L C o n t a i n e r<SgNode ∗ > : : i t e r a t o r i = f u n c t i o n D e c l a r a t i o n L i s t . b e g i n ( ) ; i != f u n c t i o n D e c l a r a t i o n L i s t . end ( ) ;
33                 {
34               // B u i l d a p o i n t e r t o t h e c u r r e n t t y p e s o t h a t we can c a l l t h e get name ( ) member f u n c t i o n .
35                     SgFunctionDeclaration ∗ f u n c t i o n D e c l a r a t i o n = i sS gFu ncti o nDecl a ra ti o n (∗ i ) ;
36                     ROSE ASSERT( f u n c t i o n D e c l a r a t i o n != NULL ) ;
37
38                 // o u t p u t t h e f u n c t i o n number and t h e name o f t h e f u n c t i o n
39                    p r i n t f ( ” f u n c t i o n name #%d i s %s a t l i n e %d \n ” ,
40                              c o u n t e r ++, f u n c t i o n D e c l a r a t i o n −>get name ( ) . s t r ( ) ,
41                              f u n c t i o n D e c l a r a t i o n − g e t f i l e i n f o ()−> g e t l i n e ( ) ) ;
                                                                       >
42                  }
43        }
44
45   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
46        {
47       // B u i l d t h e AST u s e d by ROSE
48          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
49          ROSE ASSERT( p r o j e c t != NULL ) ;
50
51      // B u i l d a l i s t o f f u n c t i o n s w i t h i n t h e AST and f i n d                   all    access       functions
52      // ( f u n c t i o n name s t a r t s w i t h ” g e t ” o r ” s e t ” )
53
54      // B u i l d l i s t u s i n g a q u e r y o f t h e whole AST
55         R o s e S T L C o n t a i n e r<SgNode∗> f u n c t i o n D e c l a r a t i o n L i s t = NodeQuery : : querySubTree ( p r o j e c t , V S g F u n c t i o n D e c l a r a t i o n ) ;
56
57      // B u i l d l i s t u s i n g n e s t e d Q u e r i e s ( o p e r a t i n g on r e t u r n r e s u l t o f p r e v i o u s q u e r y )
58         R o s e S T L C o n t a i n e r<SgNode∗> a c c e s s F u n c t i o n s L i s t ;
59         a c c e s s F u n c t i o n s L i s t = NodeQuery : : q u e r y N o d e L i s t ( f u n c t i o n D e c l a r a t i o n L i s t ,& q u e r y S o l v e r A c c e s s F u n c t i o n s ) ;
60         printFunctionDeclarationList ( accessFunctionsList );
61
62      // A l t e r n a t i v e form o f same q u e r y b u i l d i n g t h e l i s t u s i n g a q u e r y o f t h e whole AST
63         a c c e s s F u n c t i o n s L i s t = NodeQuery : : querySubTree ( p r o j e c t ,& q u e r y S o l v e r A c c e s s F u n c t i o n s ) ;
64         printFunctionDeclarationList ( accessFunctionsList );
65
66      // Another way t o q u e r y f o r c o l l e c t i o n s o f IR n o d e s
67         V a r i a n t V e c t o r vv1 = V S g C l a s s D e f i n i t i o n ;
68         s t d : : c o u t << ”Number o f c l a s s d e f i n i t i o n s i n t h e memory p o o l                         i s : ” << NodeQuery : : queryMemoryPool ( vv1 ) . s i z e ( ) << s t d :
69
70      // Another way t o q u e r y f o r c o l l e c t i o n s o f m u l t i p l e IR n o d e s .
71      // V a r i a n t V e c t o r ( V SgType ) i s i n t e r n a l l y expanded t o a l l IR n o d e s d e r i v e d from SgType .
72         V a r i a n t V e c t o r vv2 = V a r i a n t V e c t o r ( V S g C l a s s D e f i n i t i o n ) + V a r i a n t V e c t o r ( V SgType ) ;
73         s t d : : c o u t << ”Number o f c l a s s d e f i n i t i o n s AND t y p e s i n t h e memory p o o l i s : ” << NodeQuery : : queryMemoryPool ( vv2 ) . s i z e
74
75      // Note : Show c o m p o s i t i o n o f AST q u e r i e s
76
77            return 0;
78        }



     Figure 10.4: Example source code for translator to read an input program and generate a list of
     access functions in the AST (nestedQueryExample.C).
     86                                                                                             CHAPTER 10. AST QUERY



 1
 2   // Templated c l a s s d e c l a r a t i o n u s e d i n t e m p l a t e p a r a m e t e r example c o d e
 3   t e m p l a t e <typename T>
 4   c l a s s templateClass
 5         {
 6             public :
 7                    int x ;
 8
 9                       void foo ( i n t ) ;
10                       void foo ( double ) ;
11         };
12
13   // O v e r l o a d e d f u n c t i o n s     for    t e s t i n g overloaded function       resolution
14   void foo ( i n t ) ;
15   void foo ( double )
16      {
17         int x = 1;
18         int y ;
19
20        // Added t o a l l o w non− t r i v i a l CFG
21           i f (x)
22               y = 2;
23           else
24               y = 3;
25         }
26
27   i n t main ( )
28        {
29          foo (42);
30          foo (3.14159265);
31
32              t e m p l a t e C l a s s <char> i n s t a n t i a t e d C l a s s ;
33              instantiatedClass . foo ( 7 ) ;
34              instantiatedClass . foo ( 7 . 0 ) ;
35
36              f o r ( i n t i =0; i < 4 ;           i ++)
37                   {
38                      int x ;
39                   }
40
41              return 0;
42         }



     Figure 10.5: Example source code used as input to program in figure 10.4 (nestedQueryExam-
     ple.C).



 1   f u n c t i o n name #0 i s g e t f o o            at l i n e 0
 2   f u n c t i o n name #1 i s s e t f o o            at l i n e 0
 3   f u n c t i o n name #2 i s g e t f o o            a t l i n e 28
 4   f u n c t i o n name #3 i s s e t f o o            a t l i n e 29
 5   f u n c t i o n name #0 i s g e t f o o            at l i n e 0
 6   f u n c t i o n name #1 i s s e t f o o            at l i n e 0
 7   f u n c t i o n name #2 i s g e t f o o            a t l i n e 28
 8   f u n c t i o n name #3 i s s e t f o o            a t l i n e 29
 9   Number o f c l a s s d e f i n i t i o n s         i n t h e memory p o o l i s : 1
10   Number o f c l a s s d e f i n i t i o n s         AND t y p e s i n t h e memory p o o l    i s : 436



           Figure 10.6: Output of input file to the AST query processor (nestedQueryExample.C).
Chapter 11

AST File I/O

Figure 11.1 shows an example of how to use the AST File I/O mechanism. This chapter presents
an example translator to write out an AST to a file and then read it back in.


11.1      Source Code for File I/O
Figure 11.1 shows an example translator which reads an input application, forms the AST, writes
out the AST to a file, then deletes the AST and reads the AST from the previously written file.
   The input code is shown in figure 11.2, the output of this code is shown in figure 11.3.


11.2      Input to Demonstrate File I/O
Figure 11.2 shows the example input used for demonstration of the AST file I/O. In this case
we are reusing the example used in the inlining example.


11.3      Output from File I/O
Figure 11.3 shows the output from the example file I/O tutorial example.


11.4      Final Code After Passing Through File I/O
Figure 11.4 shows the same file as the input demonstrating that the file I/O didn’t change
the resulting generated code. Much more sophisticated tests are applied internally to verify the
correctness of the AST after AST file I/O.




                                              87
     88                                                                                                 CHAPTER 11. AST FILE I/O


 1   // Example d e m o n s t r a t i n g f u n c t i o n       i n l i n i n g ( maximal i n l i n i n g , up t o p r e s e t number o f              inlinings ).
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   u s i n g namespace s t d ;
 6
 7   // T h i s i s a f u n c t i o n i n Qing ’ s AST i n t e r f a c e
 8   v o i d F i x S g P r o j e c t ( S g P r o j e c t& p r o j ) ;
 9
10   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
11        {
12       // B u i l d t h e p r o j e c t o b j e c t (AST) which we w i l l f i l l up w i t h m u l t i p l e f i l e s and u s e a s a
13       // h a n d l e f o r a l l p r o c e s s i n g o f t h e AST( s ) a s s o c i a t e d w i t h one o r more s o u r c e f i l e s .
14          S g P r o j e c t ∗ p r o j e c t = new S g P r o j e c t ( a r g c , a r g v ) ;
15
16        // DQ ( 7 / 2 0 / 2 0 0 4 ) : Added i n t e r n a l c o n s i s t a n c y     t e s t s on AST
17           AstTests : : runAllTests ( p r o j e c t ) ;
18
19             b o o l modifiedAST = t r u e ;
20             int     count   = 0;
21
22        // I n l i n e one c a l l a t a t i m e u n t i l         a l l have been i n l i n e d .          Loops on r e c u r s i v e c o d e .
23           do {
24                     modifiedAST = f a l s e ;
25
26                // B u i l d a l i s t o f f u n c t i o n s w i t h i n t h e AST
27                   R o s e S T L C o n t a i n e r<SgNode∗> f u n c t i o n C a l l L i s t = NodeQuery : : querySubTree ( p r o j e c t , V S g F u n c t i o n C a l l E x p )
28
29                // Loop o v e r a l l f u n c t i o n c a l l s
30                // f o r ( l i s t <SgNode ∗ > : : i t e r a t o r i = f u n c t i o n C a l l L i s t . b e g i n ( ) ; i != f u n c t i o n C a l l L i s t . end ( ) ;   i ++)
31                   R o s e S T L C o n t a i n e r<SgNode ∗ > : : i t e r a t o r i = f u n c t i o n C a l l L i s t . b e g i n ( ) ;
32                   w h i l e ( modifiedAST == f a l s e && i != f u n c t i o n C a l l L i s t . end ( ) )
33                        {
34                            SgFunctionCallExp ∗ f u n c t i o n C a l l = isSgFunctionCallExp (∗ i ) ;
35                            ROSE ASSERT( f u n c t i o n C a l l != NULL ) ;
36
37                        // Not a l l f u n c t i o n c a l l s can be i n l i n e d i n C++, s o r e p o r t                 if   successful .
38                           bool s u c e s s f u l l y I n l i n e d = doInline ( functionCall ) ;
39
40                              if    ( s u c e s s f u l l y I n l i n e d == t r u e )
41                                    {
42                                   // As s o o n a s t h e AST i s m o d i f i e d recompute t h e l i s t o f f u n c t i o n
43                                   // c a l l s ( and r e s t a r t t h e i t e r a t i o n s o v e r t h e m o d i f i e d l i s t )
44                                        modifiedAST = t r u e ;
45                                    }
46                                   else
47                                    {
48                                        modifiedAST = f a l s e ;
49                                    }
50
51                        // I n c r e m e n t t h e     list   iterator
52                           i ++;
53                         }
54
55                // Q u i t e when we have c e a s e d t o do any i n l i n e t r a n s f o r m a t i o n s
56                // and o n l y do a p r e d e f i n e d number o f i n l i n e t r a n s f o r m a t i o n s
57                       c o u n t++;
58                  }
59             w h i l e ( modifiedAST == t r u e && c o u n t < 1 0 ) ;
60
61        // C a l l f u n c t i o n t o p o s t p r o c e s s t h e AST and f i x u p symbol t a b l e s
62           FixSgProject (∗ p r o j e c t ) ;
63
64        // Rename e a c h v a r i a b l e d e c l a r a t i o n
65           renameVariables ( p r o j e c t ) ;
66
67        // Fold up b l o c k s
68           flattenBlocks ( project );
69
70        // Clean up i n l i n e r −g e n e r a t e d c o d e
71           cleanupInlinedCode ( project ) ;
72
73        // Change members t o p u b l i c
74           changeAllMembersToPublic ( p r o j e c t ) ;
75
76        // DQ ( 3 / 1 1 / 2 0 0 6 ) : T h i s f a i l s s o t h e i n l i n i n g , o r t h e AST I n t e r f a c e
77        // s u p p o r t , n e e d s more work even though i t g e n e r a t e d good c o d e .
78        // A s t T e s t s : : r u n A l l T e s t s ( p r o j e c t ) ;
79
80             r e t u r n backend ( p r o j e c t ) ;
81         }
     11.4. FINAL CODE AFTER PASSING THROUGH FILE I/O                                                                          89




 1   // T h i s t e s t c o d e i s a c o m b i n a t i o n o f p a s s 1 and p a s s 7 , s e l e c t e d somewhat randomly
 2   // from Jeremiah ’ s t e s t c o d e o f h i s i n l i n i n g t r a n s f o r m a t i o n from summer 2 0 0 4 .
 3
 4   int x = 0;
 5
 6   // F u n c t i o n i t i n c r e m e n t ”x”
 7   v o i d incrementX ( )
 8         {
 9           x++;
10         }
11
12   int foo ()
13      {
14        int a = 0;
15        while ( a < 5)
16            {
17              ++a ;
18            }
19
20           return a + 3;
21       }
22
23   i n t main ( i n t , c h a r ∗ ∗ )
24        {
25       // Two t r i v a l f u n c t i o n   calls   to i n l i n e
26          incrementX ( ) ;
27          incrementX ( ) ;
28
29      // Somthing more i n t e r e s t i n g t o i n l i n e
30         for ( ; foo () < 7;)
31            {
32              x++;
33            }
34
35           return x ;
36       }



        Figure 11.2: Example source code used as input to demonstrate the AST file I/O support.




                           Figure 11.3: Output of input code after inlining transformations.
     90                                                                                  CHAPTER 11. AST FILE I/O




 1   // T h i s t e s t c o d e i s a c o m b i n a t i o n o f p a s s 1 and p a s s 7 , s e l e c t e d somewhat randomly
 2   // from Jeremiah ’ s t e s t c o d e o f h i s i n l i n i n g t r a n s f o r m a t i o n from summer 2 0 0 4 .
 3   int x = 0;
 4   // F u n c t i o n i t i n c r e m e n t ”x”
 5
 6   v o i d incrementX ( )
 7   {
 8      x++;
 9   }
10
11   int foo ()
12   {
13     int a 0 = 0;
14     while ( a 0 < 5){
15       ++a 0 ;
16     }
17     return a 0 + 3;
18   }
19
20   i n t main ( i n t , c h a r ∗ ∗ )
21   {
22       x++;
23       x++;
24   // Somthing more i n t e r e s t i n g t o i n l i n e
25       for ( ; true ; ) {
26         int a 1 = 0;
27         while ( a 1 < 5){
28            ++a 1 ;
29         }
30         int rose temp 7 0 = a 1 + 3;
31         bool rose temp 2 = ( bool )( rose temp                    7   0 < 7);
32         i f (! rose temp 2 ) {
33            break ;
34         }
35         else {
36         }
37         x++;
38       }
39       return x ;
40   }



                                    Figure 11.4: Output of input code after file I/O.
     Chapter 12

     Debugging Techniques

     There are numerous methods ROSE provides to help debug the development of specialized
     source-to-source translators. This section shows some of the techniques for getting information
     from IR nodes and displaying it. More information about generation of specialized AST graphs
     to support debugging can be found in chapter 5 and custom graph generation in section 27.


     12.1           Input For Examples Showing Debugging Techniques
     Figure 12.1 shows the input code used for the example translators that report useful debugging
     information in this chapter.

 1   // Example program show ing m a t r i x m u l t i p l y
 2   // ( f o r u s e w i t h l o o p o p t i m i z a t i o n t u t o r i a l example )
 3
 4   #d e f i n e N 50
 5
 6   i n t main ( )
 7        {
 8          int i , j , k ;
 9          d o u b l e a [ N ] [ N] , b [ N ] [ N ] , c [ N ] [ N ] ;
10
11           f o r ( i = 0 ; i <= N−1; i +=1)
12                {
13                   f o r ( j = 0 ; j <= N−1; j +=1)
14                        {
15                           f o r ( k = 0 ; k <= N−1; k+=1)
16                                {
17                                   c [ i ][ j ] = c[ i ][ j ] + a[ i ][ k] ∗ b[k ][ j ];
18                                }
19                        }
20                }
21
22           return 0;
23       }



     Figure 12.1: Example source code used as input to program in codes showing debugging tech-
     niques shown in this section.


                                                                         91
92                                                 CHAPTER 12. DEBUGGING TECHNIQUES

12.2       Generating the code from any IR node
Any IR node may be converted to the string that represents its subtree within the AST. If it is a
type, then the string will be the value of the type; if it is a statement, the value will be the source
code associated with that statement, including any sub-statements. To support the generation
for strings from IR nodes we use the unparseToString() member function. This function strips
comments and preprocessor control structure. The resulting string is useful for both debugging
and when forming larger strings associated with the specification of transformations using the
string-based rewrite mechanism. Using ROSE, IR nodes may be converted to strings, and strings
converted to AST fragments of IR nodes.
    Note that unparsing associated with generating source code for the backend vendor compiler
is more than just calling the unparseToString member function, since it introduces comments,
preprocessor control structure and formating.
    Figure 12.2 shows a translator which generates a string for a number of predefined IR nodes.
Figure 12.1 shows the sample input code and figure 12.5 shows the output from the translator
when using the example input application.


12.3       Displaying the source code position of any IR node
This example shows how to obtain information about the position of any IR node relative to
where it appeared in the original source code. New IR nodes (or subtrees) that are added to the
AST as part of a transformation will be marked as part of a transformation and have no position
in the source code. Shared IR nodes (as generated by the AST merge mechanism are marked
as shared explicitly (other IR nodes that are shared by definition don’t have a SgFileInfo object
and are thus not marked explicitly as shared.
    The example translator to output the source code position is shown in figure 12.4. Using the
input code in figure 12.1 the output code is shown in figure 12.5.
     12.3. DISPLAYING THE SOURCE CODE POSITION OF ANY IR NODE                                                                                 93


 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an example p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   // r o s e . C : Example ( d e f a u l t ) ROSE P r e p r o c e s s o r : u s e d f o r t e s t i n g ROSE i n f r a s t r u c t u r e
 3
 4   #i n c l u d e ” r o s e . h”
 5
 6   u s i n g namespace s t d ;
 7
 8   int
 9   main ( i n t a r g c , c h a r ∗ a r g v [ ]           )
10       {
11         ios : : sync with stdio ();                           // Syncs C++ and C I /O s u b s y s t e m s !
12
13            if   ( SgProject : : g e t v e r b o s e ( ) > 0)
14                    p r i n t f ( ” I n p r e p r o c e s s o r . C : main ( ) \n ” ) ;
15
16            SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
17            ROSE ASSERT ( p r o j e c t != NULL ) ;
18
19      // AST d i a g n o s t i c t e s t s
20         A s t T e s t s : : r u n A l l T e s t s ( c o n s t c a s t <S g P r o j e c t ∗>( p r o j e c t ) ) ;
21
22      // t e s t s t a t i s t i c s
23         i f ( project − e t v e r b o s e ( ) > 1)
                                >g
24               {
25                 c o u t << A s t N o d e S t a t i s t i c s : : t r a v e r s a l S t a t i s t i c s ( p r o j e c t ) ;
26                 c o u t << A s t N o d e S t a t i s t i c s : : I R n o d e U s a g e S t a t i s t i c s ( ) ;
27               }
28
29            if ( project − e t v e r b o s e ( ) > 0)
                                >g
30                  p r i n t f ( ” G e n e r a t e t h e p d f o u t p u t o f t h e SAGE I I I AST \n ” ) ;
31            generatePDF ( ∗ p r o j e c t ) ;
32
33            if ( project − e t v e r b o s e ( ) > 0)
                                >g
34                  p r i n t f ( ” G e n e r a t e t h e DOT o u t p u t o f t h e SAGE I I I AST \n ” ) ;
35            generateDOT ( ∗ p r o j e c t ) ;
36
37         R o s e S T L C o n t a i n e r<SgNode∗> n o d e L i s t ;
38      // n o d e L i s t = NodeQuery : : querySubTree ( p r o j e c t , V SgType , NodeQuery : : E x t r a c t T y p e s ) ;
39         n o d e L i s t = NodeQuery : : querySubTree ( p r o j e c t , V SgForStatement ) ;
40         p r i n t f ( ” \ n n o d e L i s t . s i z e ( ) = %zu \n ” , n o d e L i s t . s i z e ( ) ) ;
41
42            R o s e S T L C o n t a i n e r<SgNode ∗ > : : i t e r a t o r i = n o d e L i s t . b e g i n ( ) ;
43            w h i l e ( i != n o d e L i s t . end ( ) )
44                 {
45                     p r i n t f ( ” Query node = %p = %s = %s \n ” , ∗ i , ( ∗ i )−> s a g e c l a s s n a m e ( ) , ( ∗ i )−>u n p a r s e T o S t r i n g ( ) . c s t r ( ) ) ;
46                     i ++;
47                 }
48
49            return 0;
50        }



     Figure 12.2: Example source code showing the output of the string from an IR node. The string
     represents the code associated with the subtree of the target IR node.


 1
 2   nodeList . s i z e () = 3
 3   Query node = 0 x 2 b 0 f 0 6 8 4 9 0 1 0 = SgF or St a temen t = f o r ( i = 0 ; i <= 50 − 1 ; i += 1 ) { f o r ( j = 0 ; j <= 50 − 1 ; j += 1 ) { f o r ( k = 0 ; k <
 4   Query node = 0 x 2 b 0 f 0 6 8 4 9 1 3 0 = SgF or St a temen t = f o r ( j = 0 ; j <= 50 − 1 ; j += 1 ) { f o r ( k = 0 ; k <= 50 − 1 ; k += 1 ) { c [ i ] [ j ] =( c [
 5   Query node = 0 x 2 b 0 f 0 6 8 4 9 2 5 0 = SgF or St a temen t = f o r ( k = 0 ; k <= 50 − 1 ; k += 1 ) { c [ i ] [ j ] =( c [ i ] [ j ] +(a [ i ] [ k ] ∗ b [ k ] [ j ] ) ) ; }



                          Figure 12.3: Output of input code using debuggingIRnodeToString.C
     94                                                                    CHAPTER 12. DEBUGGING TECHNIQUES




 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an example p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   // r o s e . C : Example ( d e f a u l t ) ROSE P r e p r o c e s s o r : u s e d f o r t e s t i n g ROSE i n f r a s t r u c t u r e
 3
 4   #i n c l u d e ” r o s e . h”
 5
 6   u s i n g namespace s t d ;
 7
 8   int
 9   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
10       {
11         i f ( SgProject : : g e t v e r b o s e ( ) > 0)
12                p r i n t f ( ” I n p r e p r o c e s s o r . C : main ( ) \n ” ) ;
13
14            SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
15            ROSE ASSERT ( p r o j e c t != NULL ) ;
16
17            R o s e S T L C o n t a i n e r<SgNode∗> n o d e L i s t ;
18            n o d e L i s t = NodeQuery : : querySubTree ( p r o j e c t , V SgForStatement ) ;
19            p r i n t f ( ” \ n n o d e L i s t . s i z e ( ) = %zu \n ” , n o d e L i s t . s i z e ( ) ) ;
20
21            R o s e S T L C o n t a i n e r<SgNode ∗ > : : i t e r a t o r i = n o d e L i s t . b e g i n ( ) ;
22            w h i l e ( i != n o d e L i s t . end ( ) )
23                 {
24                     S g F i l e I n f o & f i l e I n f o = ∗ ( ( ∗ i )−> g e t f i l e i n f o ( ) ) ;
25                     p r i n t f ( ” Query node = %p = %s i n %s \n −−−−− a t l i n e %d on column %d \n ” ,
26                            ∗ i , ( ∗ i )−> s a g e c l a s s n a m e ( ) , f i l e I n f o . g e t f i l e n a m e ( ) ,
27                               f i l e I n f o . get line () , f i l e I n f o . get col ());
28                     i ++;
29                 }
30
31            if   ( project − e t v e r b o s e ( ) > 0)
                                  >g
32                    p r i n t f ( ” C a l l i n g t h e backend ( ) \n ” ) ;
33
34            return 0;
35        }



     Figure 12.4: Example source code showing the output of the string from an IR node. The string
     represents the code associated with the subtree of the target IR node.




 1
 2   nodeList . s i z e () = 3
 3   Query node = 0 x 2 b 2 b 9 7 0 c f 0 1 0                                                                                                         −d
                                                 = SgF or St a temen t i n / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s
 4   −−−−− a t l i n e 11 on column              6
 5   Query node = 0 x 2 b 2 b 9 7 0 c f 1 3 0                                                                                                         −d
                                                 = SgF orSt a temen t i n / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s
 6   −−−−− a t l i n e 13 on column              11
 7   Query node = 0 x 2 b 2 b 9 7 0 c f 2 5 0                                                                                                         −d
                                                 = SgF orSt a temen t i n / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s
 8   −−−−− a t l i n e 15 on column              16

     hared IR nodes (as generated by the AST merge mechanism are mar


              Figure 12.5: Output of input code using debuggingSourceCodePositionInformation.C
                           Part II

                Complex Types


This part elaborates some details for handling complex types in ROSE.




                                 95
     Chapter 13

     Type and Declaration Modifiers

     Most languages support the general concept of modifiers to types, declarations, etc. The keyword
     volatile for example is a modifier to the type where it is used in a declaration. Searching for
     the modifiers for types and declarations,however, can be confusing. They are often not where
     one would expect, and most often because of corner cases in the language that force them to be
     handled in specific ways.
         This example tutorial code is a demonstration of a how to access the volatile modifier used
     in the declaration of types for variables. We demonstrate that the modifier is not present in the
     SgVariableDeclaration or the SgVariableDefinitoon, but is located in the SgModifierType used
     to wrap the type returned from the SgInitializedName (the variable in the variable declaration).




     13.1          Input For Example Showing use of Volatile type mod-
                   ifier
     Figure 13.1 shows the example input used for demonstration of test for the volatile type modifier.


 1   // I n p u t example o f u s e o f ” v o l a t i l e ” t y p e m o d i f i e r
 2
 3   volatile      int a ,∗b ;
 4
 5   void foo ()
 6      {
 7        for ( volatile          i n t y = 0 ; y < 1 0 ; y++)
 8            {
 9            }
10      }



        Figure 13.1: Example source code used as input to program in codes used in this chapter.



                                                                      97
     98                                                                     CHAPTER 13. TYPE AND DECLARATION MODIFIERS

     13.2                    Generating the code representing the seeded bug
     Figure 13.2 shows a code that traverses each IR node and for and SgInitializedName IR node
     checks its type. The input code is shown in figure 13.1, the output of this code is shown in
     figure 13.3.

 1   #i n c l u d e       ” r o s e . h”
 2
 3   using       namespace            std ;
 4
 5   class       visitorTraversal                  :   public       AstSimpleProcessing
 6      {
 7              public :
 8                   void            v i s i t ( SgNode ∗ n ) ;
 9         };
10
11   v o i d v i s i t o r T r a v e r s a l : : v i s i t ( SgNode ∗ n )
12         {
13      // The ” v o l a t i l e ” m a d i f i e r i s i n t h e t y p e o f t h e S g I n i t i a l i z e d N a m e
14           SgInitializedName∗ initializedName = isSgInitializedName (n ) ;
15           i f ( i n i t i a l i z e d N a m e != NULL)
16                 {
17                      p r i n t f ( ” Found a S g I n i t i a l i z e d N a m e = %s \n ” , i n i t i a l i z e d N a m e −>g e t n a m e ( ) . s t r ( ) ) ;
18                     SgType ∗ t y p e = i n i t i a l i z e d N a m e −   >g e t t y p e ( ) ;
19
20                         p r i n t f (”         i n i t i a l i z e d N a m e : t y p e = %p = %s \n ” , t y p e , t y p e−             >c l a s s n a m e ( ) . c s t r ( ) ) ;
21                         SgModifierType ∗ modifierType = isSgModifierType ( type ) ;
22                         i f ( m o d i f i e r T y p e != NULL)
23                               {
24                                   bool i s V o l a t i l e = modifierType−                   >g e t t y p e M o d i f i e r ( ) . g e t c o n s t V o l a t i l e M o d i f i e r ( ) . i s V o l a t i l e ( ) ;
25                                   p r i n t f (”          i n i t i a l i z e d N a m e : S g M o d i f i e r T y p e : i s V o l a t i l e = %s \n ” , ( i s V o l a t i l e == t r u e ) ? ” t r u e ”            :   ” false ”);
26                               }
27
28                         S g M o d i f i e r N o d e s ∗ m o d i f i e r N o d e s = t y p e−     >g e t m o d i f i e r s ( ) ;
29                         p r i n t f (”          i n i t i a l i z e d N a m e : m o d i f i e r N o d e s = %p \n ” , m o d i f i e r N o d e s ) ;
30                         i f ( m o d i f i e r N o d e s != NULL)
31                               {
32                                   SgModifierTypePtrVector m o d i f i e r L i s t = modifierNodes−                                 >g e t n o d e s ( ) ;
33                                   f o r ( S g M o d i f i e r T y p e P t r V e c t o r : : i t e r a t o r i = m o d i f i e r L i s t . b e g i n ( ) ; i != m o d i f i e r L i s t . end ( ) ;        i ++)
34                                         {
35                                             p r i n t f ( ” i n i t i a l i z e d N a m e : m o d i f i e r s : i = %s \n ” , ( ∗ i )−> c l a s s n a m e ( ) . c s t r ( ) ) ;
36                                         }
37                               }
38                    }
39
40       // Note t h a t t h e ” v o l a t i l e ” m a d i f i e r i s n o t i n t h e S g V a r i a b l e D e c l a r a t i o n n o r t h e S g V a r i a b l e D e f i n i t i o n
41          SgVariableDeclaration∗ variableDeclaration = isSgVariableDeclaration (n ) ;
42          i f ( v a r i a b l e D e c l a r a t i o n != NULL)
43              {
44                 bool i s V o l a t i l e = variableDeclaration −                        >g e t d e c l a r a t i o n M o d i f i e r ( ) . g e t t y p e M o d i f i e r ( ) . g e t c o n s t V o l a t i l e M o d i f i e r ( ) . i s V o l a t
45                  p r i n t f ( ” S g V a r i a b l e D e c l a r a t i o n : i s V o l a t i l e = %s \n ” , ( i s V o l a t i l e == t r u e ) ? ” t r u e ” : ” f a l s e ” ) ;
46                 SgVariableDefinition∗ variableDefinition = variableDeclaration−                                                       >g e t d e f i n i t i o n ( ) ;
47             // p r i n t f ( ” v a r i a b l e D e f i n i t i o n = %p \n ” , v a r i a b l e D e f i n i t i o n ) ;
48                  i f ( v a r i a b l e D e f i n i t i o n != NULL)
49                        {
50                            bool i s V o l a t i l e = v a r i a b l e D e f i n i t i o n −     >g e t d e c l a r a t i o n M o d i f i e r ( ) . g e t t y p e M o d i f i e r ( ) . g e t c o n s t V o l a t i l e M o d i f i e r ( ) . i s V
51                            p r i n t f ( ” S g V a r i a b l e D e f i n i t i o n : i s V o l a t i l e = %s \n ” , ( i s V o l a t i l e == t r u e ) ? ” t r u e ” : ” f a l s e ” ) ;
52                        }
53              }
54        }
55
56   // must h a v e a r g c and a r g v h e r e ! !
57   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
58        {
59          SgProject ∗ project = frontend                               ( argc ,     argv ) ;
60
61              visitorTraversal myvisitor ;
62              myvisitor . traverseInputFiles ( project , preorder ) ;
63
64              return         backend ( p r o j e c t ) ;
65        }




                            Figure 13.2: Example source code showing how to detect volatile modifier.
    13.2. GENERATING THE CODE REPRESENTING THE SEEDED BUG                                99




1   // I n p u t example o f u s e o f ” v o l a t i l e ” t y p e m o d i f i e r
2   volatile int a ;
3   v o l a t i l e i n t ∗b ;
4
5   void foo ()
6   {
7     for ( volatile        i n t y = 0 ; y < 1 0 ; y++) {
8     }
9   }



                         Figure 13.3: Output of input code using volatileTypeModifier.C
100   CHAPTER 13. TYPE AND DECLARATION MODIFIERS
Chapter 14

Function Parameter Types

The analysis of functions often requires the query of the function types. This tutorial example
shows how to obtain the function parameter types for any function. Note that functions also
have a type which is based on their signature, a combination of their return type and functions
parameter types. Any functions sharing the same return type and function parameter types have
the same function type (the function type, a SgFunctionType IR node, will be shared between
such functions).
    Figure 14.1 shows a translator which reads an application (shown in figure 14.2) and outputs
information about the function parameter types for each function, shown in figure 14.3. This
information includes the order of the function declaration in the global scope, and name of the
function, and the types of each parameter declared in the function declaration.
    Note that there are a number of builtin functions defined as part of the GNU g++ and gcc
compatibility and these are output as well. These are marked as compiler generated functions
within ROSE. The code shows how to differentiate between the two different types. Notice also
that instantiated template functions are classified as compiler generated.




                                             101
     102                                                               CHAPTER 14. FUNCTION PARAMETER TYPES




 1   // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   u s i n g namespace s t d ;
 6
 7   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 8        {
 9       // B u i l d t h e AST u s e d by ROSE
10          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
11          ROSE ASSERT( p r o j e c t != NULL ) ;
12
13      // B u i l d a l i s t o f f u n c t i o n s w i t h i n t h e AST
14         R o s e S T L C o n t a i n e r<SgNode∗> f u n c t i o n D e c l a r a t i o n L i s t = NodeQuery : : querySubTree ( p r o j e c t , V S g F u n c t i o n D e c l a r
15
16            int functionCounter = 0;
17            f o r ( R o s e S T L C o n t a i n e r<SgNode ∗ > : : i t e r a t o r i = f u n c t i o n D e c l a r a t i o n L i s t . b e g i n ( ) ; i != f u n c t i o n D e c l a r a t i o n L
18                 {
19                // B u i l d a p o i n t e r t o t h e c u r r e n t t y p e s o t h a t we can c a l l t h e get name ( ) member f u n c t i o n .
20                    SgFunctionDeclaration ∗ f u n c t i o n D e c l a r a t i o n = i sS gFu ncti o nDecl a ra ti o n (∗ i ) ;
21                   ROSE ASSERT( f u n c t i o n D e c l a r a t i o n != NULL ) ;
22
23               // DQ ( 3 / 5 / 2 0 0 6 ) : Only o u t p u t t h e non−c o m p i l e r g e n e r a t e d IR n o d e s
24                  i f ( ( ∗ i )−> g e t f i l e i n f o ()−> i s C o m p i l e r G e n e r a t e d ( ) == f a l s e )
25                      {
26                         SgFunctionParameterList ∗ functionParameters = functionDeclaration − et parameterList ( ) ;  >g
27                        ROSE ASSERT( f u n c t i o n D e c l a r a t i o n != NULL ) ;
28
29                       // o u t p u t t h e f u n c t i o n number and t h e name o f t h e f u n c t i o n
30                          p r i n t f ( ” Non−c o m p i l e r g e n e r a t e d f u n c t i o n name #%3d i s %s \n ” , f u n c t i o n C o u n t e r ++, f u n c t i o n D e c l a r a t
31
32                             SgInitializedNamePtrList & parameterList = functionParameters− e t a r g s ( ) ;                                    >g
33                             i n t parameterCounter = 0 ;
34                             f o r ( S g I n i t i a l i z e d N a m e P t r L i s t : : i t e r a t o r j = p a r a m e t e r L i s t . b e g i n ( ) ; j != p a r a m e t e r L i s t . end ( ) ; j +
35                                  {
36                                     SgType∗ parameterType = ( ∗ j )−> g e t t y p e ( ) ;
37                                     p r i n t f (”            parameterType #%2d = %s \n ” , p a r a m e t e r C o u n t e r ++,parameterType− n p a r s e T o S t r i n g (
                                                                                                                                                                          >u
38                                  }
39                         }
40                        else
41                         {
42                           p r i n t f ( ” C o m p i l e r g e n e r a t e d f u n c t i o n name #%3d i s %s \n ” , f u n c t i o n C o u n t e r ++, f u n c t i o n D e c l a r a t i o n −
43                         }
44                 }
45
46            return 0;
47        }



     Figure 14.1: Example source code showing how to get type information from function parameters.
                                                                                                                  103




 1
 2   // Templated c l a s s d e c l a r a t i o n u s e d i n t e m p l a t e p a r a m e t e r example c o d e
 3   t e m p l a t e <typename T>
 4   c l a s s templateClass
 5         {
 6             public :
 7                    int x ;
 8
 9                      void foo ( i n t ) ;
10                      void foo ( double ) ;
11        };
12
13   // O v e r l o a d e d f u n c t i o n s    for    t e s t i n g overloaded function   resolution
14   void foo ( i n t ) ;
15   void foo ( double )
16      {
17         int x = 1;
18         int y ;
19
20      // Added t o a l l o w non− t r i v i a l CFG
21         i f (x)
22             y = 2;
23         else
24             y = 3;
25       }
26
27   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ]            )
28        {
29          foo (42);
30          foo (3.14159265);
31
32             t e m p l a t e C l a s s <char> i n s t a n t i a t e d C l a s s ;
33             instantiatedClass . foo ( 7 ) ;
34             instantiatedClass . foo ( 7 . 0 ) ;
35
36             f o r ( i n t i =0; i < 4 ;           i ++)
37                  {
38                     int x ;
39                  }
40
41             return 0;
42        }



          Figure 14.2: Example source code used as input to typeInfoFromFunctionParameters.C.
     104                                                     CHAPTER 14. FUNCTION PARAMETER TYPES


 1   Compiler         generated   function   name   #    0   is   builtin copysign
 2   C o mp i l e r   generated   function   name   #    1   is   builtin copysignf
 3   C o mp i l e r   generated   function   name   #    2   is   builtin copysignl
 4   C o mp i l e r   generated   function   name   #    3   is   builtin acosf
 5   C o mp i l e r   generated   function   name   #    4   is   builtin acosl
 6   C o mp i l e r   generated   function   name   #    5   is   builtin asinf
 7   C o mp i l e r   generated   function   name   #    6   is   builtin asinl
 8   C o mp i l e r   generated   function   name   #    7   is   builtin atanf
 9   C o mp i l e r   generated   function   name   #    8   is   builtin atanl
10   Compiler         generated   function   name   #    9   is   builtin atan2f
11   Compiler         generated   function   name   #   10   is   builtin atan2l
12   Compiler         generated   function   name   #   11   is   builtin ceilf
13   Compiler         generated   function   name   #   12   is   builtin ceill
14   Compiler         generated   function   name   #   13   is   builtin coshf
15   Compiler         generated   function   name   #   14   is   builtin coshl
16   Compiler         generated   function   name   #   15   is   builtin floorf
17   Compiler         generated   function   name   #   16   is   builtin floorl
18   Compiler         generated   function   name   #   17   is   builtin fmodf
19   Compiler         generated   function   name   #   18   is   builtin fmodl
20   Compiler         generated   function   name   #   19   is   builtin frexpf
21   Compiler         generated   function   name   #   20   is   builtin frexpl
22   Compiler         generated   function   name   #   21   is   builtin ldexpf
23   Compiler         generated   function   name   #   22   is   builtin ldexpl
24   Compiler         generated   function   name   #   23   is   builtin log10f
25   Compiler         generated   function   name   #   24   is   builtin log10l
26   Compiler         generated   function   name   #   25   is   builtin modff
27   Compiler         generated   function   name   #   26   is   builtin modfl
28   Compiler         generated   function   name   #   27   is   builtin powf
29   Compiler         generated   function   name   #   28   is   builtin powl
30   Compiler         generated   function   name   #   29   is   builtin sinhf
31   Compiler         generated   function   name   #   30   is   builtin sinhl
32   Compiler         generated   function   name   #   31   is   builtin tanf
33   Compiler         generated   function   name   #   32   is   builtin tanl
34   Compiler         generated   function   name   #   33   is   builtin tanhf
35   Compiler         generated   function   name   #   34   is   builtin tanhl
36   Compiler         generated   function   name   #   35   is   builtin powil
37   Compiler         generated   function   name   #   36   is   builtin powi
38   Compiler         generated   function   name   #   37   is   builtin powif
39   Compiler         generated   function   name   #   38   is   builtin strchr
40   Compiler         generated   function   name   #   39   is   builtin strrchr
41   Compiler         generated   function   name   #   40   is   builtin strpbrk
42   Compiler         generated   function   name   #   41   is   builtin strstr
43   Compiler         generated   function   name   #   42   is   builtin nansf
44   Compiler         generated   function   name   #   43   is   builtin nans
45   Compiler         generated   function   name   #   44   is   builtin nansl
46   Compiler         generated   function   name   #   45   is   builtin fabs
47   Compiler         generated   function   name   #   46   is   builtin fabsf
48   Compiler         generated   function   name   #   47   is   builtin fabsl
49   Compiler         generated   function   name   #   48   is   builtin cosf
50   Compiler         generated   function   name   #   49   is   builtin cosl
51   Compiler         generated   function   name   #   50   is   builtin sinf
52   Compiler         generated   function   name   #   51   is   builtin sinl
53   Compiler         generated   function   name   #   52   is   builtin sqrtf
54   Compiler         generated   function   name   #   53   is   builtin sqrtl
55   Compiler         generated   function   name   #   54   is   builtin fpclassify
56   Compiler         generated   function   name   #   55   is   builtin return address
57   Compiler         generated   function   name   #   56   is   builtin frame address
58   Compiler         generated   function   name   #   57   is   builtin expect
59   Compiler         generated   function   name   #   58   is   builtin prefetch
60   Compiler         generated   function   name   #   59   is   builtin huge val
61   Compiler         generated   function   name   #   60   is   builtin huge valf
62   Compiler         generated   function   name   #   61   is   builtin huge vall
63   Compiler         generated   function   name   #   62   is   builtin inf
64   Compiler         generated   function   name   #   63   is   builtin inff
65   Compiler         generated   function   name   #   64   is   builtin infl
66   Compiler         generated   function   name   #   65   is   builtin nan
67   Compiler         generated   function   name   #   66   is   builtin nanf
68   Compiler         generated   function   name   #   67   is   builtin nanl
69   Compiler         generated   function   name   #   68   is   builtin nans
70   Compiler         generated   function   name   #   69   is   builtin nansf
71   Compiler         generated   function   name   #   70   is   builtin nansl
72   Compiler         generated   function   name   #   71   is   builtin clz
73   Compiler         generated   function   name   #   72   is   builtin ctz
74   Compiler         generated   function   name   #   73   is   builtin popcount
75   Compiler         generated   function   name   #   74   is   builtin parity
76   Compiler         generated   function   name   #   75   is   builtin ffsl
77   Compiler         generated   function   name   #   76   is   builtin clzl
78   Compiler         generated   function   name   #   77   is   builtin ctzl
79   Compiler         generated   function   name   #   78   is   builtin popcountl
80   Compiler         generated   function   name   #   79   is   builtin parityl
81   Compiler         generated   function   name   #   80   is   builtin ffsll
82   Compiler         generated   function   name   #   81   is   builtin clzll
83   Compiler         generated   function   name   #   82   is   builtin ctzll
Chapter 15

Resolving Overloaded Functions

Figure 15.1 shows a translator which reads an application and reposts on the mapping between
function calls and function declarations. This is trivial since all overloaded function resolution
is done within the frontend and so need not be computed (this is because all type resolution
is done in the frontend and stored in the AST explicitly). Other compiler infrastructures often
require this to be figured out from the AST, when type resolution is unavailable, and while not
too hard for C, this is particularly complex for C++ (due to overloading and type promotion
within function arguments).
    Figure 15.2 shows the input code used to get the translator. Figure 15.3 shows the resulting
output.




                                               105
     106                                                CHAPTER 15. RESOLVING OVERLOADED FUNCTIONS




 1   // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   u s i n g namespace s t d ;
 6
 7   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 8        {
 9       // B u i l d t h e AST u s e d by ROSE
10          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
11          ROSE ASSERT( p r o j e c t != NULL ) ;
12
13      // B u i l d a l i s t o f f u n c t i o n s w i t h i n t h e AST
14         R o s e S T L C o n t a i n e r<SgNode∗> f u n c t i o n C a l l L i s t = NodeQuery : : querySubTree ( p r o j e c t , V S g F u n c t i o n C a l l E x p ) ;
15
16            int functionCounter = 0;
17            f o r ( R o s e S T L C o n t a i n e r<SgNode ∗ > : : i t e r a t o r i = f u n c t i o n C a l l L i s t . b e g i n ( ) ; i != f u n c t i o n C a l l L i s t . end ( ) ;   i ++
18                 {
19                    S g E x p r e s s i o n ∗ f u n c t i o n E x p r e s s i o n = i s S g F u n c t i o n C a l l E x p ( ∗ i )−> g e t f u n c t i o n ( ) ;
20                   ROSE ASSERT( f u n c t i o n E x p r e s s i o n != NULL ) ;
21
22                    SgFunctionRefExp ∗ f u n c t i o n R e f E x p = i s S g F u n c t i o n R e f E x p ( f u n c t i o n E x p r e s s i o n ) ;
23
24                    SgFunctionSymbol ∗ f u n c t i o n S y m b o l = NULL ;
25                    i f ( f u n c t i o n R e f E x p != NULL)
26                        {
27                       // Case o f non−member f u n c t i o n
28                            functionSymbol = functionRefExp− et symbol ( ) ;
                                                                          >g
29                        }
30                       else
31                        {
32                       // Case o f member f u n c t i o n ( h i d d e n i n r h s o f b i n a r y d o t o p e r a t o r e x p r e s s i o n )
33                            SgDotExp∗ dotExp = isSgDotExp ( f u n c t i o n E x p r e s s i o n ) ;
34                            ROSE ASSERT( dotExp != NULL ) ;
35
36                                                                          >g
                               f u n c t i o n E x p r e s s i o n = dotExp− e t r h s o p e r a n d ( ) ;
37                             SgMemberFunctionRefExp ∗ memberFunctionRefExp = isSgMemberFunctionRefExp ( f u n c t i o n E x p r e s s i o n ) ;
38                             ROSE ASSERT( memberFunctionRefExp != NULL ) ;
39
40                                                                                >g
                               f u n c t i o n S y m b o l = memberFunctionRefExp− e t s y m b o l ( ) ;
41                         }
42
43                    ROSE ASSERT( f u n c t i o n S y m b o l != NULL ) ;
44
45                    SgFunctionDeclaration ∗ f u n c t i o n D e c l a r a t i o n = functionSymbol− e t d e c l a r a t i o n ( ) ;
                                                                                                     >g
46                    ROSE ASSERT( f u n c t i o n D e c l a r a t i o n != NULL ) ;
47
48               // Output mapping o f f u n c t i o n c a l l s t o f u n c t i o n d e c l a r a t i o n s
49                  p r i n t f ( ” L o c a t i o n o f f u n c t i o n c a l l #%d a t l i n e %d r e s o l v e d by o v e r l o a d e d f u n c t i o n d e c l a r e d a t l i n e %
50                            f u n c t i o n C o u n t e r ++,
51                            i s S g F u n c t i o n C a l l E x p ( ∗ i )−> g e t f i l e i n f o ()−> g e t l i n e ( ) ,
52                            f u n c t i o n D e c l a r a t i o n − g e t f i l e i n f o ()−> g e t l i n e ( ) ) ;
                                                                     >
53                }
54
55            return 0;
56        }



     Figure 15.1: Example source code showing mapping of function calls to overloaded function
     declarations.
                                                                                                                                      107




 1
 2   // Templated c l a s s d e c l a r a t i o n u s e d i n t e m p l a t e p a r a m e t e r example c o d e
 3   t e m p l a t e <typename T>
 4   c l a s s templateClass
 5         {
 6             public :
 7                    int x ;
 8
 9                      void foo ( i n t ) ;
10                      void foo ( double ) ;
11        };
12
13   // O v e r l o a d e d f u n c t i o n s    for     t e s t i n g overloaded function        resolution
14   void foo ( i n t ) ;
15   void foo ( double )
16      {
17         int x = 1;
18         int y ;
19
20      // Added t o a l l o w non− t r i v i a l CFG
21         i f (x)
22             y = 2;
23         else
24             y = 3;
25       }
26
27   i n t main ( )
28        {
29          foo (42);
30          foo (3.14159265);
31
32             t e m p l a t e C l a s s <char> i n s t a n t i a t e d C l a s s ;
33             instantiatedClass . foo ( 7 ) ;
34             instantiatedClass . foo ( 7 . 0 ) ;
35
36             f o r ( i n t i =0; i < 4 ;           i ++)
37                  {
38                     int x ;
39                  }
40
41             return 0;
42        }



                 Figure 15.2: Example source code used as input to resolveOverloadedFunction.C.




 1   Location         of   function         call    #0    at    line    29    resolved   by   overloaded   function   declared   at   line   14
 2   Location         of   function         call    #1    at    line    30    resolved   by   overloaded   function   declared   at   line   15
 3   Location         of   function         call    #2    at    line    33    resolved   by   overloaded   function   declared   at   line   0
 4   Location         of   function         call    #3    at    line    34    resolved   by   overloaded   function   declared   at   line   0



                                Figure 15.3: Output of input to resolveOverloadedFunction.C.
108   CHAPTER 15. RESOLVING OVERLOADED FUNCTIONS
     Chapter 16

     Template Parameter Extraction


 1   // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   u s i n g namespace s t d ;
 6
 7   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 8        {
 9       // B u i l d t h e AST u s e d by ROSE
10          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
11          ROSE ASSERT( p r o j e c t != NULL ) ;
12
13      // B u i l d a l i s t o f f u n c t i o n s w i t h i n t h e AST
14         R o s e S T L C o n t a i n e r<SgNode∗> t e m p l a t e I n s t a n t i a t i o n D e c l L i s t =
15                  NodeQuery : : querySubTree ( p r o j e c t , V S g T e m p l a t e I n s t a n t i a t i o n D e c l ) ;
16
17            i n t classTemplateCounter = 0 ;
18            f o r ( R o s e S T L C o n t a i n e r<SgNode ∗ > : : i t e r a t o r i = t e m p l a t e I n s t a n t i a t i o n D e c l L i s t . b e g i n ( ) ;
19                    i != t e m p l a t e I n s t a n t i a t i o n D e c l L i s t . end ( ) ; i ++)
20                 {
21                    SgTemplateInstantiationDecl ∗ instantiatedTemplateClass = i s S g T e m p l a t e I n s t a n t i a t i o n D e c l (∗ i ) ;
22                   ROSE ASSERT( i n s t a n t i a t e d T e m p l a t e C l a s s != NULL ) ;
23
24               // o u t p u t t h e f u n c t i o n number and t h e name o f t h e f u n c t i o n
25                  p r i n t f ( ” C l a s s name #%d i s %s \n ” ,
26                            c l a s s T e m p l a t e C o u n t e r ++,
27                            i n s t a n t i a t e d T e m p l a t e C l a s s − et templateName ( ) . s t r ( ) ) ;
                                                                                 >g
28
29                     c o n s t SgTemplateArgumentPtrList& t e m p l a t e P a r a m e t e r L i s t = i n s t a n t i a t e d T e m p l a t e C l a s s − e t t e m p l a t e A r g u m e n t s ( ) ;
                                                                                                                                                           >g
30                     i n t parameterCounter = 0 ;
31                     f o r ( SgTemplateArgumentPtrList : : c o n s t i t e r a t o r j = t e m p l a t e P a r a m e t e r L i s t . b e g i n ( ) ;
32                              j != t e m p l a t e P a r a m e t e r L i s t . end ( ) ; j ++)
33                          {
34                              p r i n t f (”     TemplateArgument #%d = %s \n ” , p a r a m e t e r C o u n t e r ++,(∗ j )−>u n p a r s e T o S t r i n g ( ) . c s t r ( ) ) ;
35                          }
36                 }
37
38            return 0;
39        }



              Figure 16.1: Example source code used to extract template parameter information.

                                                                           109
     110                                                      CHAPTER 16. TEMPLATE PARAMETER EXTRACTION

        Figure 16.1 shows a translator which reads an application and gathers a list of loop nests.
     At the end of the traversal it reports information about each instantiated template, including
     the template arguments.
        Figure 16.2 shows the input code used to get the translator. Figure 16.3 shows the resulting
     output.

 1
 2   // Templated c l a s s d e c l a r a t i o n u s e d i n t e m p l a t e p a r a m e t e r example c o d e
 3   t e m p l a t e <typename T>
 4   c l a s s templateClass
 5         {
 6             public :
 7                    int x ;
 8
 9                       void foo ( i n t ) ;
10                       void foo ( double ) ;
11         };
12
13
14   i n t main ( )
15        {
16          t e m p l a t e C l a s s <char> i n s t a n t i a t e d C l a s s ;
17          instantiatedClass . foo ( 7 ) ;
18          instantiatedClass . foo ( 7 . 0 ) ;
19
20              t e m p l a t e C l a s s <i n t > i n s t a n t i a t e d C l a s s I n t ;
21              t e m p l a t e C l a s s <f l o a t > i n s t a n t i a t e d C l a s s F l o a t ;
22              t e m p l a t e C l a s s <t e m p l a t e C l a s s <char> > i n s t a n t i a t e d C l a s s N e s t e d C h a r ;
23
24              f o r ( i n t i =0; i < 4 ;           i ++)
25                   {
26                      int x ;
27                   }
28
29              return 0;
30        }



                        Figure 16.2: Example source code used as input to templateParameter.C.


 1   C l a s s name #0 i s t e m p l a t e C l a s s
 2        TemplateArgument #0 = c h a r
 3   C l a s s name #1 i s t e m p l a t e C l a s s
 4        TemplateArgument #0 = i n t
 5   C l a s s name #2 i s t e m p l a t e C l a s s
 6        TemplateArgument #0 = f l o a t
 7   C l a s s name #3 i s t e m p l a t e C l a s s
 8        TemplateArgument #0 = t e m p l a t e C l a s s < c h a r >



                                        Figure 16.3: Output of input to templateParameter.C.
     Chapter 17

     Template Support

     This chapter is specific to demonstrating the C++ template support in ROSE. This section is
     not an introduction to the general subject of C++ templates. ROSE provides special handling
     for C++ templates because template instantiation must be controlled by the compiler.
         Templates that require instantiation are instantiated by ROSE and can be seen in the traver-
     sal of the AST (and transformed). Any templates that can be instantiated by the backend
     compiler and are not transformed are not output within the code generation phase.                  FIXME: Provide a list of when
                                                                                                                 templates are generated
                                                                                                        internally in the AST and when
                                                                                                             template instantiations are
     17.1         Example Template Code #1                                                                                       output.

     This section presents figure 17.4, a simple C++ source code using a template. It is used as a
     basis for showing how template instantiations are handled within ROSE.

 1   t e m p l a t e <typename T>
 2   class X
 3        {
 4             public :
 5                   void foo ( ) ;
 6        };
 7
 8    <i
     X nt> x ;
 9
10         <i
     void X nt >:: foo ()
11      {
12      }



                       Figure 17.1: Example source code showing use of a C++ template.



     17.2         Example Template Code #2
     This section presents figure 17.4, a simple C++ source code using a template function. It is used
     as a basis for showing how template instantiations are handled within ROSE.


                                                    111
     112                                                       CHAPTER 17. TEMPLATE SUPPORT




 1   t e m p l a t e < typename T >
 2   class X
 3        {
 4             public :
 5                   void foo ( ) ;
 6        };
 7                <
     class X int > x ;



     Figure 17.2: Example source code after processing using identityTranslator (shown in figure 2.1).




 1   // t e m p l a t e f u n c t i o n
 2   t e m p l a t e <typename T>
 3   void foo ( T t )
 4        {
 5        }
 6
 7   // S p e c i a l i z a t i o n from u s e r
 8   t e m p l a t e < v o i d f o o <i n t >( i n t x ) {}
                      >
 9
10   i n t main ( )
11        {
12          foo ( 1 ) ;
13        }



                          Figure 17.3: Example source code showing use of a C++ template.




 1   // t e m p l a t e f u n c t i o n
 2   t e m p l a t e < typename T >
 3   void foo ( T t )
 4        {
 5        }
 6   // S p e c i a l i z a t i o n from u s e r
 7
 8            >
     template< void foo < i n t > ( i n t x )
 9   {
10   }
11
12   i n t main ( )
13   {
14       foo< i n t > ( 1 ) ;
15       return 0;
16   }



     Figure 17.4: Example source code after processing using identityTranslator (shown in figure 2.1).
                           Part III

              Program Analyses


This part exemplifies the use of existing ROSE analyses and how to build
                   customized analyses using ROSE.




                                  113
Chapter 18

Recognizing Loops

Figures 18.1 and 18.2 show a translator which reads an application and gathers a list of loop
nests. At the end of the traversal it reports information about each loop nest, including the
function where it occurred and the depth of the loop nest.                                    FIXME: This example program
   Using this translator we can compile the code shown in figure 18.3. The output is shown in is unfinished. It will output a list
                                                                                                        of objects representing
figure 18.4.                                                                                        information about perfectly
                                                                                                                     nested loops.




                                              115
     116                                                                                                         CHAPTER 18. RECOGNIZING LOOPS




 1   // ROSE i s             a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an e x a m p l e p r e p r o c e s s o r b u i l t   w i t h ROSE .
 2   // r o s e . C :        Example ( d e f a u l t ) ROSE P r e p r o c e s s o r : u s e d f o r t e s t i n g ROSE i n f r a s t r u c t u r e
 3
 4   #i n c l u d e       ” r o s e . h”
 5
 6   class      InheritedAttribute
 7      {
 8             public :
 9                  int              loopNestDepth ;
10
11                          InheritedAttribute                ( ) : loopNestDepth ( 0 ) {};
12                          InheritedAttribute                ( const InheritedAttribute & X )                         {};
13        };
14
15   class      SynthesizedAttribute
16      {
17             public :
18                 SynthesizedAttribute ()                         {};
19        };
20
21   class      Traversal              :   public      SgTopDownBottomUpProcessing<I n h e r i t e d A t t r i b u t e , S y n t h e s i z e d A t t r i b u t e >
22      {
23             public :
24               // F u n c t i o n s r e q u i r e d
25                  InheritedAttribute evaluateInheritedAttribute                                            (
26                       SgNode ∗ a s t N o d e ,
27                       InheritedAttribute inheritedAttribute );
28
29                          SynthesizedAttribute evaluateSynthesizedAttribute (
30                             SgNode ∗ a s t N o d e ,
31                             InheritedAttribute inheritedAttribute ,
32                             SubTreeSynthesizedAttributes synthesizedAttributeList                                         );
33        };
34
35   InheritedAttribute
36   Traversal : : evaluateInheritedAttribute (
37        SgNode ∗ a s t N o d e ,
38        InheritedAttribute inheritedAttribute )
39      {
40        s w i t c h ( a s t N o d e− >v a r i a n t T ( ) )
41              {
42                  c a s e V SgForStatement :
43                        {
44                            p r i n t f ( ” Found a S g F o r S t a t e m e n t \n ” ) ;
45
46                             //     This l o o p i s one d e e p p e r than t h e depth                   of   the   parent ’ s   inherited      attribute
47                                    i n h e r i t e d A t t r i b u t e . l o o p N e s t D e p t h ++;
48
49                                    break ;
50                               }
51
52                          default :
53                             {
54                            // g++ n e e d s         a   block     here
55                             }
56                    }
57
58             return          inheritedAttribute ;
59        }
60
61   SynthesizedAttribute
62   Traversal : : evaluateSynthesizedAttribute (
63        SgNode ∗ a s t N o d e ,
64        InheritedAttribute inheritedAttribute ,




                                     Figure 18.1: Example source code showing loop recognition (part 1).
                                                                                                                            117



 1            SubTreeSynthesizedAttributes                          synthesizedAttributeList               )
 2       {
 3            SynthesizedAttribute                     returnAttribute ;
 4
 5            s w i t c h ( a s t N o d e−>v a r i a n t T ( ) )
 6                  {
 7                      c a s e V SgForStatement :
 8                            {
 9
10                              break ;
11                          }
12
13                      default :
14                         {
15                        // g++ n e e d s         a    block      here
16                         }
17                 }
18
19            return       returnAttribute ;
20       }
21
22
23
24   int
25   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
26       {
27         SgProject ∗ p r o j e c t = f r o n t e n d ( argc , argv ) ;
28         ROSE ASSERT ( p r o j e c t != NULL ) ;
29
30     //     Build the i n h e r i t e d a t t r i b u t e
31            InheritedAttribute inheritedAttribute ;
32
33            Traversal         myTraversal ;
34
35     //     C a l l t h e t r a v e r s a l s t a r t i n g a t t h e s a g e P r o j e c t node o f t h e AST
36            myTraversal . t r a v e r s e I n p u t F i l e s ( p r o j e c t , i n h e r i t e d A t t r i b u t e ) ;
37
38            return       0;
39       }




                                Figure 18.2: Example source code showing loop recognition (part 2).




 1
 2   i n t main ( )
 3        {
 4          int x [ 4 ] ;
 5          int y [ 4 ] [ 4 ] ;
 6
 7             f o r ( i n t i =0; i < 4 ;                    i ++)
 8                  {
 9                     x [ i ] = 7;
10                  }
11
12             f o r ( i n t i =0; i < 4 ; i ++)
13                  {
14                     f o r ( i n t j =0; j < 4 ; j ++)
15                          {
16                             y [ i ] [ j ] = 42;
17                          }
18                  }
19
20             return 0;
21        }



                       Figure 18.3: Example source code used as input to loop recognition processor.
    118                                                   CHAPTER 18. RECOGNIZING LOOPS




1   Found a Sg Fo r Sta t ement
2   Found a Sg Fo r Sta t ement
3   Found a Sg Fo r Sta t ement



                        Figure 18.4: Output of input to loop recognition processor.
Chapter 19

Virtual CFG

The ROSE virtual control flow graph interface provides a higher level of detail than ROSE’s other
control flow graph interfaces. It expresses control flow even within expressions, and handles short-
circuited logical and conditional operators properly1 . The interface is referred to as “virtual”
because no explicit graph is ever created: only the particular CFG nodes and edges used in a
given program ever exist. CFG nodes and edges are value classes (they are copied around by
value, reducing the need for explicit memory management).
   A CFG node consists of two components: an AST node pointer, and an index of a particular
CFG node within that AST node. There can be several CFG nodes corresponding to a given
AST node, and thus the AST node pointers cannot be used to index CFG nodes. The particular
index values for the different AST node types are explained in Section 19.1.




19.1        CFGNode Index values
To facilitate traversal and represent sufficient details, each eligible ROSE AST node (expression,
statement and SgInitializedName) has several corresponding CFGNodes in virtual CFG. These
CFGNodes have indices from 0 to n. CFGNode of index value of 0 is used to represent the
beginning CFG node for an AST node, while the CFGNode of index n is the end CFGNode for
the AST node. The beginning node represents the point in the control flow immediately before
the construct starts to execute, and the ending node represents the point immediately after the
construct has finished executing. Note that these two nodes do not dominate the other CFG
nodes in the construct due to goto statements and labels.
   Reimplementation of S gNode::cfgIndexForEnd() returns the index value for n of each eligible
SgNode type. See source file src/frontend/SageIII/virtualCFG/memberFunctions.C for valid
index values for each type of eligible SgNode.

  1 It assumes operands of expressions are computed in left-to-right order, unlike the actual language semantics,

however.


                                                      119
120                                                               CHAPTER 19. VIRTUAL CFG

19.2         Important functions
The main body of the virtual CFG interface is in virtualCFG.h; the source code is in src/frontend/SageIII/virtualCFG/
and is linked into librose . The filtered CFG interface explained below is in filteredCFG.h, and
functions for converting the CFG to a graph in Dot format are in cfgToDot.h.
    Two functions provide the basic way of converting from AST nodes to CFG nodes. Each
SgNode has two methods, cfgForBeginning() and cfgForEnd(), to generate the corresponding
beginning and end CFG nodes, respectively. These functions require that the AST node is
either an expression, a statement, or a SgInitializedName.

19.2.1       Node methods
      • CFGNode(SgNode∗ node, unsigned int index): Build a CFG node from the given AST
        node and index. Valid index values are in Section 19.1.

      • toString(): Produce a string representing the information in the node.

      • toStringForDebugging(): Similar, but with more internal debugging information.

      • id(): A C identifier representing the node.

      • getNode(): Get the underlying AST node.

      • getIndex(): Get the index (as explained in Section 19.1) for this CFG node within its
        underlying AST node.

      • outEdges(): Return a vector of outgoing CFG edges from this node. This function inter-
        nally calls cfgOutEdges(unsigned int idx) to generate out edges for each CFGNode of a
        given index value.

      • inEdges(): Return a vector of CFG edges coming into this node (note that the sources
        and targets of the edges are not reversed, and so each in edge has its target as the current
        node). This function internally calls cfgInEdges(unsigned int idx) to generate in edges for
        each CFGNode of a given index value.

      • isInteresting (): See Section 19.6.1.

      • Nodes are also comparable using the operators ==, !=, and <.

19.2.2       Edge methods
      • toString(): Produce a string representing the information in the node.

      • toStringForDebugging(): Similar, but with more internal debugging information.

      • id(): A C identifier representing the node.

      • source(): The starting CFG node for this edge.

      • target (): The ending CFG node for this edge.
19.3. DRAWING A GRAPH OF THE CFG                                                                121

   • condition(): When there are multiple CFG edges from the same starting node, each of
     them is taken under certain conditions. The condition() method returns the condition, of
     type EdgeConditionKind. The possible return values are:

        – eckUnconditional: An edge that is always taken.
        – eckTrue: True case of a two-way branch (either an if statement or a loop
        – eckFalse: False case of a two-way branch
        – eckCaseLabel: Case label in a switch statement (key is given by caseLabel())
        – eckDefault: Default label of a switch statement
        – eckDoConditionPassed: Enter Fortran do loop body
        – eckDoConditionFailed: Fortran do loop finished
        – eckForallIndicesInRange: Start testing forall mask
        – eckForallIndicesNotInRange: End of forall loop
        – eckComputedGotoCaseLabel: Case in computed goto – number needs to be computed
          separately
        – eckArithmeticIfLess: Edge for the arithmetic if expression being less than zero
        – eckArithmeticIfEqual: Edge for the arithmetic if expression being equal to zero
        – eckArithmeticIfGreater: Edge for the arithmetic if expression being greater than zero

   • caseLabel(): For an edge with condition eckCaseLabel, an expression representing the key
     for the case label.

   • computedGotoCaseIndex(): The index of this edge’s case within a Fortran computed goto
     (an edge of kind eckComputedGotoCaseLabel).

   • conditionBasedOn(): The test expression or switch expression that is tested by this edge.

   • scopesBeingExited(), scopesBeingEntered(): Variables leaving and entering scope during
     this edge. This information has not been extensively verified, and should not be relied
     upon.

   • Edges can also be compared using the operators == and !=. They are not ordered to
     avoid dependencies on pointer comparison on different computers.


19.3      Drawing a graph of the CFG
Fig. 19.3 shows a translator to dump full (debug) virtual control flow graphs for all functions
within input source files. It also dumps a simplified version (interesting) version of virtual control
flow graphs. A standalone tool named virtualCFG is installed under ROSE INSTALL TREE/bin
for users to generate both debug and interesting dot files of virtual CFGs.
    The example input code is given in Fig. 19.3. Debug and interesting virtualCFG of function
main() are shown in Fig. 19.3 and Fig. 19.4, respectively. Debug and interesting virtualCFG of
function testIf () are shown in Fig. 19.5 and Fig. 19.6, respectively.
     122                                                                                                CHAPTER 19. VIRTUAL CFG


 1   // Example t r a n s l a t o r t o g e n e r a t e d o t           files     of    virtual      c o n t r o l flow graphs
 2   #i n c l u d e ” r o s e . h”
 3   #i n c l u d e <s t r i n g >
 4   u s i n g namespace s t d ;
 5
 6   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 7   {
 8       // B u i l d t h e AST u s e d by ROSE
 9       SgProject ∗ s a g e P r o j e c t = fro ntend ( argc , argv ) ;
10
11       // P r o c e s s a l l f u n c t i o n d e f i n i t i o n b o d i e s f o r v i r t u a l c o n t r o l f l o w graph g e n e r a t i o n
12       R o s e S T L C o n t a i n e r<SgNode∗> f u n c t i o n s = NodeQuery : : querySubTree ( s a g e P r o j e c t , V S g F u n c t i o n D e f i n i t i o n ) ;
13       f o r ( R o s e S T L C o n t a i n e r<SgNode ∗ > : : c o n s t i t e r a t o r i = f u n c t i o n s . b e g i n ( ) ; i != f u n c t i o n s . end ( ) ; ++i )
14       {
15           SgFunctionDefinition ∗ proc = i s S g F u n c t i o n D e f i n i t i o n (∗ i ) ;
16          ROSE ASSERT ( p r o c != NULL ) ;
17                                                                                                                  >
             s t r i n g f i l e N a m e= S t r i n g U t i l i t y : : st r i p Pa t hF r o m Fi l eN a m e ( proc− g e t f i l e i n f o ()−> g e t f i l e n a m e S t r i n g ( ) ) ;
18           s t r i n g dotFileName1=f i l e N a m e +”.”+ proc− e t d e c l a r a t i o n ()−> get name ( ) +”. debug . d o t ” ;
                                                                                     >g
19                                                                                   >g
             s t r i n g dotFileName2=f i l e N a m e +”.”+ proc− e t d e c l a r a t i o n ()−> get name ( ) +”. i n t e r e s t i n g . do t ” ;
20
21           // Dump o u t t h e f u l l CFG, i n c l u d i n g b o o k k e e p i n g n o d e s
22           VirtualCFG : : cfgToDotForDebugging ( proc , dotFileName1 ) ;
23
24           // Dump o u t o n l y t h o s e n o d e s which a r e ” i n t e r e s t i n g ” f o r              analyses
25           VirtualCFG : : i n t e r e s t i n g C f g T o D o t ( proc , dotFileName2 ) ;
26       }
27
28       return 0;
29   }



             Figure 19.1: Example source code showing visualization of virtual control flow graph.


         As we can see in Fig. 19.5, the debug dot graph has all CFGNodes for each eligible SgNode.
     For example, there are three CFGNodes for SgIfStmt, with indices from 0 to 2. The interest-
     ing CFGNode of SgIfStmt has solid line oval while non-essential CFGNodes have dashed line
     ovals in the graph. The caption of each node has a format of <SgNode-Enum-value>@line-
     number:CFGNode-index-value. It is obvious from the graph that SgIfStmt’s interesting CFGN-
     ode has an index value of 1. In comparison, Fig. 19.6 only shows interesting CFGNodes with
     solid line ovals. Again, the captions tells line numbers and CFGNode’s index values for each
     CFGNode.
     19.3. DRAWING A GRAPH OF THE CFG                                                              123




 1   #i n c l u d e <s t d i o . h>
 2   #i n c l u d e < s t r i n g . h>
 3   #i n c l u d e < a s s e r t . h>
 4
 5   size t i ;
 6   char b u f f e r [ 1 0 ] ;
 7   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 8   {
 9       f o r ( i =0; i < s t r l e n ( a r g v [ 1 ] ) ;   i ++)
10       {
11           b u f f e r [ i ] = argv [ 1 ] [ i ] ;
12       }
13       return 0;
14   }
15
16   int t e s t I f ( int i )
17   {
18     int rt ;
19     i f ( i %2 ==0)
20        r t =0;
21     else
22        r t =1;
23
24       return rt ;
25   }



                 Figure 19.2: Example source code used as input to build virtual control graphs.
124                                                                                                                                                                                CHAPTER 19. VIRTUAL CFG

                                                                                                                                                      Start(::main)
                                                                                                                                          <SgFunctionDefinition> @line=7 :idx=0




                                                                                                                                                  main_parameter_list_
                                                                                                                                        <SgFunctionParameterList> @line=7 :idx=0




                                                                                                                                                     initialized_name_argc
                                                                                                                                                <SgInitializedName> argc :idx=0




                                                                                                                                                  main_parameter_list_
                                                                                                                                        <SgFunctionParameterList> @line=7 :idx=1




                                                                                                                                                     initialized_name_argv
                                                                                                                                                <SgInitializedName> argv :idx=0




                                                                                                                                                  main_parameter_list_
                                                                                                                                        <SgFunctionParameterList> @line=7 :idx=2




                                                                                                                                                After parameters(::main)
                                                                                                                                          <SgFunctionDefinition> @line=7 :idx=1




                                                                                                                                              After pre-initialization(::main)
                                                                                                                                          <SgFunctionDefinition> @line=7 :idx=2




                                                                                                                                                      0x2b9fec474010
                                                                                                                                                <SgBasicBlock> @line=8 :idx=0




                                                                                                                                                      0x2b9fec4b7010
                                                                                                                                               <SgForStatement> @line=9 :idx=0




                                                                                                                                                   SgForInitStatement
                                                                                                                                           <SgForInitStatement> @line=9 :idx=0




                                                                                                                                                       SgExprStatement
                                                                                                                                               <SgExprStatement> @line=9 :idx=0




                                                                                                                                                   SgAssignOp_undef_name
                                                                                                                                                 <SgAssignOp> @line=9 :idx=0




                                                                                                                                                        var_ref_of_i
                                                                                                                                                <SgVarRefExp> @line=9 :idx=0




                                                                                                                                                   SgAssignOp_undef_name
                                                                                                                                                 <SgAssignOp> @line=9 :idx=1




                                                                                                                                                   SgCastExp_undef_name
                                                                                                                                                 <SgCastExp> @line=0 :idx=0




                                                                                                                                                    integer_value_exp_0
                                                                                                                                                  <SgIntVal> @line=9 :idx=0




                                                                                                                                                    integer_value_exp_0
                                                                                                                                                  <SgIntVal> @line=9 :idx=1




                                                                                                                                                   SgCastExp_undef_name
                                                                                                                                                 <SgCastExp> @line=0 :idx=1




                                                                                                                                                   SgAssignOp_undef_name
                                                                                                                                                 <SgAssignOp> @line=9 :idx=2




                                                                                                                                                       SgExprStatement
                                                                                                                                               <SgExprStatement> @line=9 :idx=1




                                                                                                                                                   SgForInitStatement
                                                                                                                                           <SgForInitStatement> @line=9 :idx=1




                                                                                                                                                      0x2b9fec4b7010
                                                                                                                                               <SgForStatement> @line=9 :idx=1




                                                                                                                               SgExprStatement
                                                                                                                       <SgExprStatement> @line=9 :idx=0




                                                                                                                          SgLessThanOp_undef_name
                                                                                                                        <SgLessThanOp> @line=9 :idx=0




                                                                                                                                 var_ref_of_i
                                                                                                                         <SgVarRefExp> @line=9 :idx=0




                                                                                                                         SgLessThanOp_undef_name
                                                                                                                       <SgLessThanOp> @line=9 :idx=1




                                                                                                                       function_call_function_ref_strlen
                                                                                                                     <SgFunctionCallExp> @line=9 :idx=0




                                                                                                                             function_ref_strlen
                                                                                                                     <SgFunctionRefExp> @line=9 :idx=0




                                                                                                                      function_call_function_ref_strlen
                                                                                                                    <SgFunctionCallExp> @line=9 :idx=1




                                                                                                                  expr_list_exp_SgCastExp_undef_name
                                                                                                                    <SgExprListExp> @line=9 :idx=0




                                                                                                                      SgCastExp_undef_name
                                                                                                                    <SgCastExp> @line=0 :idx=0




                                                                                               array_ref_of_var_ref_of_argv_at_integer_value_exp_1
                                                                                                        <SgPntrArrRefExp> @line=9 :idx=0




                                                                                                               var_ref_of_argv
                                                                                                         <SgVarRefExp> @line=9 :idx=0




                                                                                               array_ref_of_var_ref_of_argv_at_integer_value_exp_1
                                                                                                        <SgPntrArrRefExp> @line=9 :idx=1




                                                                                                              integer_value_exp_1
                                                                                                            <SgIntVal> @line=9 :idx=0




                                                                                                              integer_value_exp_1
                                                                                                            <SgIntVal> @line=9 :idx=1




                                                                                               array_ref_of_var_ref_of_argv_at_integer_value_exp_1
                                                                                                        <SgPntrArrRefExp> @line=9 :idx=2




                                                                                                             SgCastExp_undef_name
                                                                                                           <SgCastExp> @line=0 :idx=1




                                                                                                      expr_list_exp_SgCastExp_undef_name
                                                                                                        <SgExprListExp> @line=9 :idx=1




                                                                                                         function_call_function_ref_strlen
                                                                                                       <SgFunctionCallExp> @line=9 :idx=2




                                                                                                         function_call_function_ref_strlen
                                                                                                       <SgFunctionCallExp> @line=9 :idx=3




                                                                                                           SgLessThanOp_undef_name
                                                                                                         <SgLessThanOp> @line=9 :idx=2




                                                                                                                SgExprStatement
                                                                                                        <SgExprStatement> @line=9 :idx=1




                                                                                                               0x2b9fec4b7010
                                                                                                        <SgForStatement> @line=9 :idx=2


                                                                                                        key(true)                 key(false)


                                                                        0x2b9fec474120                                         0x2b9fec4b7010
                                                                 <SgBasicBlock> @line=10 :idx=0                         <SgForStatement> @line=9 :idx=4




                                                                       SgExprStatement                                         0x2b9fec474010
                                                               <SgExprStatement> @line=11 :idx=0                         <SgBasicBlock> @line=8 :idx=1




                                                                SgAssignOp_undef_name                                             SgReturnStmt
                                                              <SgAssignOp> @line=11 :idx=0                               <SgReturnStmt> @line=13 :idx=0




                                                  array_ref_of_var_ref_of_buffer_at_var_ref_of_i                              integer_value_exp_0
                                                       <SgPntrArrRefExp> @line=11 :idx=0                                   <SgIntVal> @line=13 :idx=0




                                            var_ref_of_buffer                                                                 integer_value_exp_0
                                       <SgVarRefExp> @line=0 :idx=0                                                        <SgIntVal> @line=13 :idx=1




 array_ref_of_var_ref_of_buffer_at_var_ref_of_i                             0x2b9fec474010                                        SgReturnStmt
      <SgPntrArrRefExp> @line=11 :idx=1                               <SgBasicBlock> @line=8 :idx=2                      <SgReturnStmt> @line=13 :idx=1




                                                 var_ref_of_i                                                        End(::main)
                                         <SgVarRefExp> @line=11 :idx=0                                   <SgFunctionDefinition> @line=7 :idx=3




                                                                 array_ref_of_var_ref_of_buffer_at_var_ref_of_i
                                                                       <SgPntrArrRefExp> @line=11 :idx=2




                                                                                     SgAssignOp_undef_name
                                                                                   <SgAssignOp> @line=11 :idx=1




                                                           array_ref_of_array_ref_of_var_ref_of_argv_at_integer_value_exp_1_at_var_ref_of_i
                                                                                  <SgPntrArrRefExp> @line=11 :idx=0




                                                                           array_ref_of_var_ref_of_argv_at_integer_value_exp_1
                                                                                    <SgPntrArrRefExp> @line=11 :idx=0




                                                                                            var_ref_of_argv
                                                                                     <SgVarRefExp> @line=11 :idx=0




                                                                           array_ref_of_var_ref_of_argv_at_integer_value_exp_1
                                                                                    <SgPntrArrRefExp> @line=11 :idx=1




                                                                                          integer_value_exp_1
                                                                                       <SgIntVal> @line=11 :idx=0




                                                                                          integer_value_exp_1
                                                                                       <SgIntVal> @line=11 :idx=1




                                                                           array_ref_of_var_ref_of_argv_at_integer_value_exp_1
                                                                                    <SgPntrArrRefExp> @line=11 :idx=2




                                                           array_ref_of_array_ref_of_var_ref_of_argv_at_integer_value_exp_1_at_var_ref_of_i
                                                                                  <SgPntrArrRefExp> @line=11 :idx=1




                                                                                             var_ref_of_i
                                                                                     <SgVarRefExp> @line=11 :idx=0




                                                           array_ref_of_array_ref_of_var_ref_of_argv_at_integer_value_exp_1_at_var_ref_of_i
                                                                                  <SgPntrArrRefExp> @line=11 :idx=2




                                                                                                               SgAssignOp_undef_name
                                                                                                             <SgAssignOp> @line=11 :idx=2




                                                                                                                            SgExprStatement
                                                                                                                    <SgExprStatement> @line=11 :idx=1




                                                                                                                              0x2b9fec474120
                                                                                                                       <SgBasicBlock> @line=10 :idx=1




                                                                                                                               0x2b9fec4b7010
                                                                                                                        <SgForStatement> @line=9 :idx=3




                                                                                                                            SgPlusPlusOp_undef_name
                                                                                                                          <SgPlusPlusOp> @line=9 :idx=0




                                                                                                                                  var_ref_of_i
                                                                                                                          <SgVarRefExp> @line=9 :idx=0




                                                                                                                                                  SgPlusPlusOp_undef_name
                                                                                                                                                <SgPlusPlusOp> @line=9 :idx=1




Figure 19.3: The debug virtual control flow graph for function main() shows all virtual CFG
nodes and edges
19.3. DRAWING A GRAPH OF THE CFG                                                                                                            125

                                                                                                              Start(::main)
                                                                                                  <SgFunctionDefinition> @line=7 :idx=0




                                                                                                            initialized_name_argc
                                                                                                       <SgInitializedName> argc :idx=0




                                                                                                            initialized_name_argv
                                                                                                       <SgInitializedName> argv :idx=0




                                                                                                           main_parameter_list_
                                                                                                 <SgFunctionParameterList> @line=7 :idx=2




                                                                                                               var_ref_of_i
                                                                                                       <SgVarRefExp> @line=9 :idx=0




                                                                                                           integer_value_exp_0
                                                                                                         <SgIntVal> @line=9 :idx=1




                                                                                                          SgCastExp_undef_name
                                                                                                        <SgCastExp> @line=0 :idx=1




                                                                                                         SgAssignOp_undef_name
                                                                                                       <SgAssignOp> @line=9 :idx=2




                                                                                                            SgExprStatement
                                                                                                    <SgExprStatement> @line=9 :idx=1




                                                                                                           SgForInitStatement
                                                                                                   <SgForInitStatement> @line=9 :idx=1




                                                                                                               var_ref_of_i
                                                                                                       <SgVarRefExp> @line=9 :idx=0




                                                                                    function_ref_strlen
                                                                            <SgFunctionRefExp> @line=9 :idx=0




                                                                                   var_ref_of_argv
                                                                             <SgVarRefExp> @line=9 :idx=0




                                                                             integer_value_exp_1
                                                                           <SgIntVal> @line=9 :idx=1




                                                      array_ref_of_var_ref_of_argv_at_integer_value_exp_1
                                                               <SgPntrArrRefExp> @line=9 :idx=2




                                                                    SgCastExp_undef_name
                                                                  <SgCastExp> @line=0 :idx=1




                                                             expr_list_exp_SgCastExp_undef_name
                                                               <SgExprListExp> @line=9 :idx=1




                                                                function_call_function_ref_strlen
                                                              <SgFunctionCallExp> @line=9 :idx=3




                                                                 SgLessThanOp_undef_name
                                                               <SgLessThanOp> @line=9 :idx=2




                                                                       SgExprStatement
                                                               <SgExprStatement> @line=9 :idx=1




                                                                      0x2b9fec4b7010
                                                               <SgForStatement> @line=9 :idx=2


                                                              key(true)             key(false)


                          var_ref_of_buffer                               integer_value_exp_0
                     <SgVarRefExp> @line=0 :idx=0                      <SgIntVal> @line=13 :idx=1




                           var_ref_of_i                                             SgReturnStmt
                   <SgVarRefExp> @line=11 :idx=0                           <SgReturnStmt> @line=13 :idx=1




  array_ref_of_var_ref_of_buffer_at_var_ref_of_i                                      End(::main)
        <SgPntrArrRefExp> @line=11 :idx=2                                 <SgFunctionDefinition> @line=7 :idx=3




                                  var_ref_of_argv
                           <SgVarRefExp> @line=11 :idx=0




                                             integer_value_exp_1
                                          <SgIntVal> @line=11 :idx=1




                                 array_ref_of_var_ref_of_argv_at_integer_value_exp_1
                                          <SgPntrArrRefExp> @line=11 :idx=2




                                                    var_ref_of_i
                                            <SgVarRefExp> @line=11 :idx=0




                   array_ref_of_array_ref_of_var_ref_of_argv_at_integer_value_exp_1_at_var_ref_of_i
                                          <SgPntrArrRefExp> @line=11 :idx=2




                                                                SgAssignOp_undef_name
                                                              <SgAssignOp> @line=11 :idx=2




                                                                                   SgExprStatement
                                                                           <SgExprStatement> @line=11 :idx=1




                                                                                         var_ref_of_i
                                                                                 <SgVarRefExp> @line=9 :idx=0




                                                                                                         SgPlusPlusOp_undef_name
                                                                                                       <SgPlusPlusOp> @line=9 :idx=1




Figure 19.4: The virtual control flow graph for function main() shows only interesting virtual
CFG nodes and edges. Each CFGNode’s caption tells associated source line number and CFGN-
ode index value (@line-num:index-value)
126                                                                                                                            CHAPTER 19. VIRTUAL CFG


                                     Start(::testIf)
                         <SgFunctionDefinition> @line=16 :idx=0




                                 testIf_parameter_list_
                       <SgFunctionParameterList> @line=16 :idx=0




                                      initialized_name_i
                                 <SgInitializedName> i :idx=0




                                 testIf_parameter_list_
                       <SgFunctionParameterList> @line=16 :idx=1




                               After parameters(::testIf)
                         <SgFunctionDefinition> @line=16 :idx=1




                             After pre-initialization(::testIf)
                         <SgFunctionDefinition> @line=16 :idx=2




                                   0x2b9fec474230
                            <SgBasicBlock> @line=17 :idx=0




                                 _variable_declaration_rt
                         <SgVariableDeclaration> @line=18 :idx=0




                                      initialized_name_rt
                                 <SgInitializedName> rt :idx=0




                                 _variable_declaration_rt
                         <SgVariableDeclaration> @line=18 :idx=1




                                   0x2b9fec474230
                            <SgBasicBlock> @line=17 :idx=1




                                      0x2b9fec4fe010
                                 <SgIfStmt> @line=19 :idx=0




                                   SgExprStatement
                           <SgExprStatement> @line=19 :idx=0




                               SgEqualityOp_undef_name
                            <SgEqualityOp> @line=19 :idx=0




                                SgModOp_undef_name
                              <SgModOp> @line=19 :idx=0




                                    var_ref_of_i
                            <SgVarRefExp> @line=19 :idx=0




                                SgModOp_undef_name
                              <SgModOp> @line=19 :idx=1




                                    integer_value_exp_2
                                 <SgIntVal> @line=19 :idx=0




                                    integer_value_exp_2
                                 <SgIntVal> @line=19 :idx=1




                                SgModOp_undef_name
                              <SgModOp> @line=19 :idx=2




                               SgEqualityOp_undef_name
                            <SgEqualityOp> @line=19 :idx=1




                                    integer_value_exp_0
                                 <SgIntVal> @line=19 :idx=0




                                    integer_value_exp_0
                                 <SgIntVal> @line=19 :idx=1




                              SgEqualityOp_undef_name
                            <SgEqualityOp> @line=19 :idx=2




                                   SgExprStatement
                           <SgExprStatement> @line=19 :idx=1




                                      0x2b9fec4fe010
                                 <SgIfStmt> @line=19 :idx=1


                                   key(true)                     key(false)


         SgExprStatement                                          SgExprStatement
 <SgExprStatement> @line=20 :idx=0                        <SgExprStatement> @line=22 :idx=0




     SgAssignOp_undef_name                                    SgAssignOp_undef_name
   <SgAssignOp> @line=20 :idx=0                             <SgAssignOp> @line=22 :idx=0




          var_ref_of_rt                                             var_ref_of_rt
  <SgVarRefExp> @line=20 :idx=0                             <SgVarRefExp> @line=22 :idx=0




     SgAssignOp_undef_name                                    SgAssignOp_undef_name
   <SgAssignOp> @line=20 :idx=1                             <SgAssignOp> @line=22 :idx=1




       integer_value_exp_0                                          integer_value_exp_1
    <SgIntVal> @line=20 :idx=0                                   <SgIntVal> @line=22 :idx=0




       integer_value_exp_0                                          integer_value_exp_1
    <SgIntVal> @line=20 :idx=1                                   <SgIntVal> @line=22 :idx=1




     SgAssignOp_undef_name                                    SgAssignOp_undef_name
   <SgAssignOp> @line=20 :idx=2                             <SgAssignOp> @line=22 :idx=2




         SgExprStatement                                          SgExprStatement
 <SgExprStatement> @line=20 :idx=1                        <SgExprStatement> @line=22 :idx=1




                                             0x2b9fec4fe010
                                        <SgIfStmt> @line=19 :idx=2




                                            0x2b9fec474230
                                     <SgBasicBlock> @line=17 :idx=2




                                              SgReturnStmt
                                     <SgReturnStmt> @line=24 :idx=0




                                             var_ref_of_rt
                                     <SgVarRefExp> @line=24 :idx=0




                                              SgReturnStmt                                           0x2b9fec474230
                                     <SgReturnStmt> @line=24 :idx=1                           <SgBasicBlock> @line=17 :idx=3




                                                                                      End(::testIf)
                                                                          <SgFunctionDefinition> @line=16 :idx=3




Figure 19.5: The debug virtual control flow graph for function testIf() shows all virtual CFG
nodes and edges
19.3. DRAWING A GRAPH OF THE CFG                                                              127

                                      Start(::testIf)
                          <SgFunctionDefinition> @line=16 :idx=0




                                     initialized_name_i
                                <SgInitializedName> i :idx=0




                                  testIf_parameter_list_
                        <SgFunctionParameterList> @line=16 :idx=1




                                    initialized_name_rt
                               <SgInitializedName> rt :idx=0




                                  _variable_declaration_rt
                          <SgVariableDeclaration> @line=18 :idx=1




                                     var_ref_of_i
                             <SgVarRefExp> @line=19 :idx=0




                                   integer_value_exp_2
                                <SgIntVal> @line=19 :idx=1




                                 SgModOp_undef_name
                               <SgModOp> @line=19 :idx=2




                                   integer_value_exp_0
                                <SgIntVal> @line=19 :idx=1




                               SgEqualityOp_undef_name
                             <SgEqualityOp> @line=19 :idx=2




                                    SgExprStatement
                            <SgExprStatement> @line=19 :idx=1




                                    0x2b9fec4fe010
                               <SgIfStmt> @line=19 :idx=1


                                    key(true)                   key(false)


             var_ref_of_rt                                        var_ref_of_rt
     <SgVarRefExp> @line=20 :idx=0                        <SgVarRefExp> @line=22 :idx=0




         integer_value_exp_0                                       integer_value_exp_1
      <SgIntVal> @line=20 :idx=1                                <SgIntVal> @line=22 :idx=1




       SgAssignOp_undef_name                                     SgAssignOp_undef_name
     <SgAssignOp> @line=20 :idx=2                              <SgAssignOp> @line=22 :idx=2




           SgExprStatement                                        SgExprStatement
   <SgExprStatement> @line=20 :idx=1                      <SgExprStatement> @line=22 :idx=1




                                               var_ref_of_rt
                                       <SgVarRefExp> @line=24 :idx=0




                                                SgReturnStmt
                                       <SgReturnStmt> @line=24 :idx=1




                                               End(::testIf)
                                   <SgFunctionDefinition> @line=16 :idx=3




Figure 19.6: The virtual control flow graph for function testIf() shows only interesting virtual
CFG nodes and edges. Each CFGNode’s caption tells associated source line number and CFGN-
ode index value (@line-num:index-value)
128                                                            CHAPTER 19. VIRTUAL CFG

19.4      Robustness to AST changes
Control flow graph nodes and edges can be kept (i.e., are not invalidated) in many cases when
the underlying AST changes. However, there are some limitations to this capability. Changing
the AST node that is pointed to by a given CFG node is not safe. CFG nodes for deleted AST
nodes are of course invalid, as are those pointing to AST nodes whose parent pointers become
invalid.


19.5      Limitations
Although workable for intraprocedural analysis of C code, the virtual CFG code has several
limitations for other languages and uses.


19.5.1     Fortran support
The virtual control flow graph includes support for many Fortran constructs, but that support
is fairly limited and not well tested. It is not recommended for production use.


19.5.2     Exception handling
The virtual CFG interface does not support control flow due to exceptions or the setjmp/longjmp
constructs. It does, however, support break, continue, goto, and early returns from functions.


19.5.3     Interprocedural control flow analysis
In virtual CFGs, interprocedural control flow analysis is disabled by default. It can be enabled
by setting the parameter virtualInteproceduralControlFlowGraphs in SgNode::cfgInEdges,
SgNode::cfgOutEdges, and their subclasses’ definitions. Interprocedural edges are labeled with
the eckInterprocedural EdgeConditionKind.
    In cases where the flow of control cannot be determined statically (calls of virtual functions,
function pointers, or functors), the interprocedural control flow graph contains all possible op-
tions. In keeping with the ‘virtual’ nature of ROSE’s control flow graphs, the set of options is
computed on-the-fly; therefore, changes to the AST will be reflected in subsequent interaction
with the control flow graph.


19.6      Node filtering
FIXME


19.6.1     “Interesting” node filter
To simplify the virtual CFG, non-essential CFGNodes, such as the beginning and the end CFGN-
odes for each AST node, can be filtered out. Each eligible SgNode type has a most important
CFGNode out of its all CFGNodes. The interesting CFGNode’s index value for each Node type is
19.7. STATIC CFG                                                                            129

returned by calling the derived implementation of virtual bool SgNode::cfgIsIndexInteresting(int
idx).

19.6.2     Arbitrary filtering

19.7      Static CFG
Since a virtual CFG does not produce any real graph, it is quite inefficient to traverse a virtual
CFG frequently. It is necessary to build a static CFG which may improve the performance of
some specific operations.
    A SgGraph object (actually, it’s a SgIncidenceDirectedGraph object) is created to store the
static CFG. Each node in the graph is a SgGraphNode object. In a virtual CFG, each node
contains two members: node and index. A SgGraphNode already holds a pointer to SgNode, and
we have to add the other property “index” to our SgGraphNode. This can be done by adding
the corresponding attribute to SgGraphNode.

19.7.1     Class methods
   • CFG(): The default constructor.

   • CFG(SgNode∗ node, bool isFiltered = false): Initialize a static CFG with the start node
     to build from and a flag indicating if the CFG is a full or filtered one.

   • setStart (SgNode∗ node): Set the start node for building a static CFG. Note that the
     argument has to be an object of any of the following classes: SgProject, SgStatement,
     SgExpression, and SgInitializedName. If a SgProject object is passed in, several graphs are
     built for every function definition.

   • isFilteredCFG(): Return the isFiltered flag.

   • setFiltered (bool flag ): Set the isFiltered flag.

   • buildCFG(): Build a full or filtered CFG according to the isFiltered flag.

   • buildFullCFG(): Build a full CFG for debugging.

   • buildFilteredCFG(): Build a filtered CFG which only contains interesting nodes.

   • getOutEdges(SgGraphNode∗ node): Return a vector of outgoing CFG edges (SgDirected-
     GraphEdge objects) from the given node.

   • getInEdges(SgGraphNode∗ node): Return a vector of CFG edges coming into the given
     node.

   • cfgForBeginning(SgNode∗ node): Return the CFG node for just before this AST node.

   • cfgForEnd(SgNode∗ node): Return the CFG node for just after this AST node.

   • getIndex(SgGraphNode∗ node): Return the index of the given CFG node.
     130                                                                                                CHAPTER 19. VIRTUAL CFG

             • cfgToDot(SgNode∗ node, const std::string& filename): Generate a DOT file for the current
               CFG. Note that the start node to be drawn can be indicated which is not necessary to be
               the start node of the CFG.

     19.7.2            Drawing a graph of the CFG
     Figure 19.7.2 shows a translator to dump full (debug) static control flow graphs for all functions
     within input source files. It also dumps a simplified version (interesting) version of static control
     flow graphs.

 1   // Example t r a n s l a t o r t o g e n e r a t e d o t           files     of    static      c o n t r o l flow graphs
 2   #i n c l u d e ” r o s e . h”
 3   #i n c l u d e <s t r i n g >
 4   u s i n g namespace s t d ;
 5
 6   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 7   {
 8       // B u i l d t h e AST u s e d by ROSE
 9       SgProject ∗ s a g e P r o j e c t = fro ntend ( argc , argv ) ;
10
11       // P r o c e s s a l l f u n c t i o n d e f i n i t i o n b o d i e s f o r s t a t i c c o n t r o l f l o w graph g e n e r a t i o n
12       R o s e S T L C o n t a i n e r<SgNode∗> f u n c t i o n s = NodeQuery : : querySubTree ( s a g e P r o j e c t , V S g F u n c t i o n D e f i n i t i o n ) ;
13       f o r ( R o s e S T L C o n t a i n e r<SgNode ∗ > : : c o n s t i t e r a t o r i = f u n c t i o n s . b e g i n ( ) ; i != f u n c t i o n s . end ( ) ; ++i )
14       {
15           SgFunctionDefinition ∗ proc = i s S g F u n c t i o n D e f i n i t i o n (∗ i ) ;
16          ROSE ASSERT ( p r o c != NULL ) ;
17                                                                                                                  >
             s t r i n g f i l e N a m e= S t r i n g U t i l i t y : : st r i p Pa t hF r o m Fi l eN a m e ( proc− g e t f i l e i n f o ()−> g e t f i l e n a m e S t r i n g ( ) ) ;
18                                                                                   >g
             s t r i n g dotFileName1=f i l e N a m e +”.”+ proc− e t d e c l a r a t i o n ()−> get name ( ) +”. debug . d o t ” ;
19                                                                                   >g
             s t r i n g dotFileName2=f i l e N a m e +”.”+ proc− e t d e c l a r a t i o n ()−> get name ( ) +”. i n t e r e s t i n g . do t ” ;
20
21            StaticCFG : : CFG c f g ( p r o c ) ;
22
23            // Dump o u t t h e f u l l CFG, i n c l u d i n g b o o k k e e p i n g n o d e s
24            c f g . buildFullCFG ( ) ;
25            c f g . cfgToDot ( proc , dotFileName1 ) ;
26
27            // Dump o u t o n l y t h o s e n o d e s which a r e ” i n t e r e s t i n g ” f o r             analyses
28            c f g . buildFilteredCFG ( ) ;
29            c f g . cfgToDot ( proc , dotFileName2 ) ;
30       }
31
32       return 0;
33   }



               Figure 19.7: Example source code showing visualization of static control flow graph.

         The example input code is given in Fig. 19.3. Debug and interesting static CFG are shown
     in Fig. 19.5 and Fig. 19.6, respectively.


     19.8             Static, Interprocedural CFGs
     ROSE supports construction of interprocedural control flow graphs using the Interprocedural-
     CFG class, a subclass of StaticCFG. Like the StaticCFG, the InterproceduralCFG can be con-
     structed from any SgNode that affects control flow. If an InterproceduralCFG is constructed
     from a given node, it will contain all possible paths of execution from that point. Edges between
     procedures will be labelled with the ‘eckInterprocedural’ EdgeConditionKind.
19.8. STATIC, INTERPROCEDURAL CFGS                                                      131

   In cases where a function call cannot be statically resolved to a function definition, the
InterproceduralCFG includes edges from the call node to all possible definitions, which are
determined by ROSE’s CallGraph.
132   CHAPTER 19. VIRTUAL CFG
Chapter 20

Generating Control Flow Graphs

The control flow of a program is broken into basic blocks as nodes with control flow forming
edges between the basic blocks. Thus the control flow forms a graph which often labeled edges
(true and false), and basic blocks representing sequentially executed code. This chapter presents
the Control Flow Graph (CFG) and the ROSE application code for generating such graphs for
any function in an input code. The CFG forms a fundamental building block for more complex
forms of program analysis.
    Figure 20.1 shows the code required to generate the control flow graph for each function of
an application. Using the input code shown in figure 20.2 the first function’s control flow graph
is shown in figure 20.3.
    Figure 20.3 shows the control flow graph for the function in the input code in figure 20.2.




                                              133
     134                                                       CHAPTER 20. GENERATING CONTROL FLOW GRAPHS


 1   // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
 2
 3   #i n c l u d e   ” r o s e . h”
 4   #i n c l u d e   <GraphUpdate . h>
 5   #i n c l u d e   ”CFGImpl . h”
 6   #i n c l u d e   ” GraphDotOutput . h”
 7   #i n c l u d e   ” c o n t r o l F l o w G r a p h . h”
 8   #i n c l u d e   ”CommandOptions . h”
 9
10   u s i n g namespace s t d ;
11
12   // Use t h e ControlFlowGraph i s d e f i n e d i n both PRE
13   // and t h e D o m i n a t o r T r ee s A n d D o m i n a n c e F r o n t i e r s namespaces .
14   // We want t o u s e t h e one i n t h e PRE namespace .
15   u s i n g namespace PRE ;
16
17   class        visitorTraversal               :   public AstSimpleProcessing
18      {
19              public :
20                   v i r t u a l void          v i s i t ( SgNode∗ n ) ;
21         };
22
23   v o i d v i s i t o r T r a v e r s a l : : v i s i t ( SgNode∗ n )
24         {
25           SgFunctionDeclaration ∗ functionDeclaration = isSgFunctionDeclaration (n ) ;
26           i f ( f u n c t i o n D e c l a r a t i o n != NULL)
27                 {
28                      SgFunctionDefinition ∗ functionDefinition = functionDeclaration −                     >g e t d e f i n i t i o n ( ) ;
29                      i f ( f u n c t i o n D e f i n i t i o n != NULL)
30                           {
31                               SgBasicBlock ∗ functionBody = f u n c t i o n D e f i n i t i o n − et body ( ) ;
                                                                                                    >g
32                              ROSE ASSERT( f u n c t i o n B o d y != NULL ) ;
33
34                               ControlFlowGraph c o n t r o l f l o w ;
35
36                          // The CFG can o n l y be c a l l e d on a f u n c t i o n d e f i n i t i o n ( a t p r e s e n t )
37                             makeCfg ( f u n c t i o n D e f i n i t i o n , c o n t r o l f l o w ) ;
38                             s t r i n g fileName = functionDeclaration −                         >get name ( ) . s t r ( ) ;
39                             f i l e N a m e += ” . d o t ” ;
40                             ofstream d o t f i l e ( fileName . c s t r ( ) ) ;
41                             printCfgAsDot ( d o t f i l e , c o n t r o l f l o w ) ;
42                           }
43                    }
44        }
45
46   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
47        {
48       // B u i l d t h e AST u s e d by ROSE
49          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
50
51              CmdOptions : : G e t I n s t a n c e ()−> S e t O p t i o n s ( a r g c , a r g v ) ;
52
53       // B u i l d t h e t r a v e r s a l o b j e c t
54          v i s i t o r T r a v e r s a l exampleTraversal ;
55
56       // C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t node o f t h e AST
57          exampleTraversal . t r a v e r s e I n p u t F i l e s ( project , preorder ) ;
58
59              return 0;
60        }



                      Figure 20.1: Example source code showing visualization of control flow graph.
                                                                                                     135




 1   #i n c l u d e <s t d i o . h>
 2   #i n c l u d e < s t r i n g . h>
 3   #i n c l u d e < a s s e r t . h>
 4
 5   size t i ;
 6   char b u f f e r [ 1 0 ] ;
 7   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 8   {
 9       f o r ( i =0; i < s t r l e n ( a r g v [ 1 ] ) ;   i ++)
10       {
11           b u f f e r [ i ] = argv [ 1 ] [ i ] ;
12       }
13       return 0;
14   }
15
16   int t e s t I f ( int i )
17   {
18     int rt ;
19     i f ( i %2 ==0)
20        r t =0;
21     else
22        r t =1;
23
24        return rt ;
25   }



                      Figure 20.2: Example source code used as input to build control flow graph.




                                   1
                     i = 0; in SgForInitStatement



                                  4
                i < strlen(argv[1]) in SgForStatement



             2                     3
         return 0;      buffer[i] = argv[1][i];



            0                              5
                                 i++ in SgBasicBlock


                     Figure 20.3: Control flow graph for function in input code file: inputCode 1.C.
136   CHAPTER 20. GENERATING CONTROL FLOW GRAPHS
Chapter 21

Graph Processing Tutorial

21.1      Traversal Tutorial
ROSE can collect and analyze paths in both source and binary CFGs. At moment it doesn’t
attempt to save paths because if you save them directly the space necessary is extremely large,
as paths grow 2n with successive if statements and even faster when for loops are involved.
Currently a path can only cannot complete the same loop twice. However it is possible for a
graph such that 1 -¿ 2 , 2-¿3, 3-¿1, 3-¿5, has paths, 1,2,3,1,2,3,5 and 1,2,3,5 because the loop
1,2,3,1 is not repeated.
    The tutorial example works as such:




                                              137
     138                                                                 CHAPTER 21. GRAPH PROCESSING TUTORIAL


 1   #include <i o s t r e a m >
 2   #include <f s t r e a m >
 3   //#i n c l u d e <r o s e . h>
 4   #include <s t r i n g >
 5   #include <e r r . h>
 6   #include ” SgGraphTemplate . h”
 7   #include ” g r a p h P r o c e s s i n g . h”
 8
 9   #include ” staticCFG . h”
10   #include ” i n t e r p r o c e d u r a l C F G . h”
11   /∗ T e s t i n g t h e graph t r a v e r s a l mechanism now i mple menting i n A s t P r o c e s s i n g . h ( i n s i d e s r c /midend/ a s t P r o c e s s i n g /
12   #include <s y s / t i m e . h>
13   #include <s y s / r e s o u r c e . h>
14   using namespace s t d ;
15   using namespace b o o s t ;
16
17
18
19
20
21
22
23   typedef myGraph CFGforT ;
24
25
26
27
28
29
30   c l a s s v i s i t o r T r a v e r s a l : public S g G r a p h T r a v e r s a l <CFGforT>
31         {
32            public :
33                      int paths ;
34                     void a n a l y z e P a t h ( v e c t o r <VertexID>& pth ) ;
35         };
36
37
38   void v i s i t o r T r a v e r s a l : : a n a l y z e P a t h ( v e c t o r <VertexID>& pth ) {
39             #pragma omp a t o m i c
40              p a t h s ++;
41
42   }
43
44
45   i n t main ( i n t a r g c , char ∗ a r g v [ ] ) {
46
47       SgProject ∗ p r o j = frontend ( argc , argv ) ;
48       ROSE ASSERT ( p r o j != NULL ) ;
49
50       S g F u n c t i o n D e c l a r a t i o n ∗ mainDefDecl = S a g e I n t e r f a c e : : f i n d M a i n ( p r o j ) ;
51
52       S g F u n c t i o n D e f i n i t i o n ∗ mainDef = mainDefDecl−             >g e t d e f i n i t i o n ( ) ;
53         v i s i t o r T r a v e r s a l ∗ v i s = new v i s i t o r T r a v e r s a l ( ) ;
54          StaticCFG : : I n t e r p r o c e d u r a l C F G c f g ( mainDef ) ;
55          stringstream ss ;
56          S g I n c i d e n c e D i r e c t e d G r a p h ∗ g = new S g I n c i d e n c e D i r e c t e d G r a p h ( ) ;
57          g = c f g . getGraph ( ) ;
58          myGraph∗ mg = new myGraph ( ) ;
59          mg = i n s t a n t i a t e G r a p h ( g , c f g , mainDef ) ;
60          vis− ltnodes = 0;
                     >t
61          vis − aths = 0 ;
                     >p
62          // v i s − i r s t P r e p G r a p h ( c o n s t c f g ) ;
                         >f
63          v i s − o n s t r u c t P a t h A n a l y z e r (mg , true , 0 , 0 , true ) ;
                     >c
64          s t d : : c o u t << ” t o o k : ” << t i m e D i f f e r e n c e ( t2 , t 1 ) << s t d : : e n d l ;
65          // c f g . clearNodesAndEdges ( ) ;
66          s t d : : c o u t << ” f i n i s h e d ” << s t d : : e n d l ;
67          s t d : : c o u t << ” p a t h s : ” << v i s − a t h s << s t d : : e n d l ;
                                                                    >p
68          delete v i s ;
69   }



                                               Figure 21.1: Source CFG Traversal Example
     21.1. TRAVERSAL TUTORIAL                                                                                                                             139


 1   #include <i o s t r e a m >
 2   #include <f s t r e a m >
 3   #include <r o s e . h>
 4   //#i n c l u d e ” interproceduralCFG . h”
 5   #include <s t r i n g >
 6   #include <e r r . h>
 7   #include ” g r a p h P r o c e s s i n g . h”
 8   #include ” B i n a r y C o n t r o l F l o w . h”
 9   #include ” B i n a r y L o a d e r . h”
10   /∗ T e s t i n g t h e graph t r a v e r s a l mechanism now i mple menting i n g r a p h P r o c e s s i n g . h ( i n s i d e s r c /midend/ a s t P r o c e s s i n g /) ∗/
11
12   using namespace s t d ;
13   using namespace b o o s t ;
14
15
16   typedef b o o s t : : g r a p h t r a i t s <B i n a r y A n a l y s i s : : C o n t r o l F l o w : : Graph > : : v e r t e x d e s c r i p t o r V e r t e x ;
     /∗∗< Graph v e r t e x t y p e . ∗/
17   typedef b o o s t : : g r a p h t r a i t s <B i n a r y A n a l y s i s : : C o n t r o l F l o w : : Graph > : : e d g e d e s c r i p t o r
     Edge ;     /∗∗< Graph ed g e t y p e . ∗/
18
19
20
21
22   c l a s s v i s i t o r T r a v e r s a l : public S g G r a p h T r a v e r s a l <B i n a r y A n a l y s i s : : C o n t r o l F l o w : : Graph>
23         {
24            public :
25                   long i n t p t h s ;
26                   long i n t t l t n o d e s ;
27
28                     v i r t u a l void a n a l y z e P a t h ( v e c t o r <Vertex>& pth ) ;
29
30
31        };
32
33
34
35   void v i s i t o r T r a v e r s a l : : a n a l y z e P a t h ( v e c t o r <Vertex>& pth ) {
36       // t l t n o d e s += p t h . s i z e ( ) ;
37       #pragma omp a t o m i c
38       p t h s ++;
39   }
40
41
42   i n t main ( i n t a r g c , char ∗ a r g v [ ] ) {
43
44          /∗ Parse t h e b i n a r y f i l e ∗/
45          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
46          s t d : : v e c t o r <S g A s m I n t e r p r e t a t i o n∗> i n t e r p s = S a g e I n t e r f a c e : : querySubTree<S g A s m I n t e r p r e t a t i o n >( p r o j e c t ) ;
47          i f ( i n t e r p s . empty ( ) ) {
48                  f p r i n t f ( s t d e r r , ” no b i n a r y i n t e r p r e t a t i o n s f o u n d \n” ) ;
49                  exit (1);
50          }
51
52          /∗ C a l c u l a t e p l a i n o l d CFG. ∗/
53              BinaryAnalysis : : ControlFlow c f g a n a l y z e r ;
54              B i n a r y A n a l y s i s : : C o n t r o l F l o w : : Graph∗ c f g = new B i n a r y A n a l y s i s : : C o n t r o l F l o w : : Graph ;
55
56                 c f g a n a l y z e r . b u i l d c f g f r o m a s t ( i n t e r p s . back ( ) , ∗ c f g ) ;
57                 s t d : : o f s t r e a m mf ;
58                 mf . open ( ” a n a l y s i s . d o t ” ) ;
59                 v i s i t o r T r a v e r s a l ∗ v i s = new v i s i t o r T r a v e r s a l ;
60                 vis− ltnodes = 0;
                           >t
61                 vis − ths = 0 ;
                           >p
62                 v i s − o n s t r u c t P a t h A n a l y z e r ( c f g , true , 0 , 0 , f a l s e ) ;
                           >c
63                 s t d : : c o u t << ” p t h s : ” << v i s − t h s << s t d : : e n d l ;
                                                                         >p
64                 s t d : : c o u t << ” t l t n o d e s : ” << v i s − l t n o d e s << s t d : : e n d l ;
                                                                              >t
65   }



                                              Figure 21.2: Binary CFG Traversal Example
140   CHAPTER 21. GRAPH PROCESSING TUTORIAL
    Chapter 22

    Dataflow Analysis

    The dataflow analysis in Rose is based on the control flow graph (CFG). One type of dataflow
    analysis is called def-use analysis, which is explained next.


    22.1       Def-Use Analysis
    The definition-usage (def-use) analysis allows to query the definition and usage for each control
    flow node (CFN). Any statement or expression within ROSE is represented as a sequence of
    CFN’s. For instance, the CFG for the following program


1   i n t main ( )
2   {
3       int x = 9;
4       x = x + 1;
5   }



                                    Figure 22.1: Example input code.

       is illustrated in Figure 22.4.


    22.1.1     Def-use Example implementation
    Fig. 22.2 shows an example program of how the def-use analysis is called. It generates a dot
    graph showing def/use information within a control flow graph. It also outputs reaching def-
    inition information for each variable references of an input code. This program (named as
    defuseAnalysis) is installed under ROSE INST/bin as a standalone tool for users to experiment
    the def/use analysis of ROSE.
        Figure 22.3 shows the screen output of the code(Fig. 22.2) running on the input code(Fig. 22.1).
    Each variable reference in the input code has at least one reaching definition node. The associated
    definition statement is also printed out.

                                                   141
     142                                                                              CHAPTER 22. DATAFLOW ANALYSIS


 1   #i n c l u d e ” r o s e . h”
 2   #i n c l u d e ” D e f U s e A n a l y s i s . h”
 3   #i n c l u d e <s t r i n g >
 4   #i n c l u d e <i o s t r e a m >
 5   u s i n g namespace s t d ;
 6
 7   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 8   {
 9       v e c t o r <s t r i n g > a r g v L i s t ( argv , a r g v + a r g c ) ;
10       SgProject ∗ project = frontend ( argvList ) ;
11
12       // C a l l t h e Def−Use A n a l y s i s
13       DFAnalysis ∗ d e f u s e = new D e f U s e A n a l y s i s ( p r o j e c t ) ;
14       b o o l debug = f a l s e ;
15       defuse−   >run ( debug ) ;
16       // Output d e f u s e a n a l y s i s r e s u l t s i n t o a d o t f i l e
17       defuse−   >dfaToDOT ( ) ;
18
19       // Find a l l v a r i a b l e r e f e r e n c e s
20       N o d e Q u e r y S y n t h e s i z e d A t t r i b u t e T y p e v a r s = NodeQuery : : querySubTree ( p r o j e c t , V SgVarRefExp ) ;
21       NodeQuerySynthesizedAttributeType : : c o n s t i t e r a t o r i = vars . begin ( ) ;
22       f o r ( ; i != v a r s . end ();++ i )
23       {
24           SgVarRefExp ∗ v a r R e f = isSgVarRefExp ( ∗ i ) ;
25           S g I n i t i a l i z e d N a m e ∗ initName = i s S g I n i t i a l i z e d N a m e ( var Ref− e t s y m b o l ()−> g e t d e c l a r a t i o n ( ) ) ;
                                                                                                            >g
26                                                                  >g
             s t d : : s t r i n g name = initName− e t q u a l i f i e d n a m e ( ) . s t r ( ) ;
27           // Find r e a c h i n g d e f i n i t i o n o f initName a t t h e c o n t r o l f l o w node v a r R e f
28           v e c t o r <SgNode∗ > v e c = d e f u s e − e t D e f F o r ( v ar Ref , initName ) ;
                                                                        >g
29          ROSE ASSERT ( v e c . s i z e ( ) >0 ) ; // e a c h v a r i a b l e r e f e r e n c e must have a d e f i n i t i o n somewhere
30
31          // Output e a c h r e a c h i n g d e f i n i t i o n node and t h e c o r r e s p o n d i n g s t a t e m e n t .
32                            <” − − −− − − − − −− − − − − − − − − − − − − − <
            s t d : : cout< − − − − − − − − − − − − − − − − − − − − − −” <s t d : : e n d l ;
33                                                                                                                                    >u
            s t d : : c o u t << v e c . s i z e ( ) << ” d e f i n i t i o n e n t r y / e n t r i e s f o r ” << va rR ef− n p a r s e T o S t r i n g ( ) <<
34                                              >
            ” @ l i n e ” << va rR ef− g e t f i l e i n f o ()−> g e t l i n e ()<<”:”<< va rR ef− g e t f i l e i n f o ()−> g e t c o l ( )
                                                                                                                        >
35          << s t d : : e n d l ;
36          f o r ( s i z e t j =0; j <v e c . s i z e ( ) ; j ++)
37          {
38                       <v
                cout< e c [ j ]−> c l a s s n a m e ()<<” ”<<v e c [ j ]<< e n d l ;
39              SgSt at emen t ∗ d e f s t m t = S a g e I n t e r f a c e : : g e t E n c l o s i n g S t a t e m e n t ( v e c [ j ] ) ;
40              ROSE ASSERT( d e f s t m t ) ;
41              cout< e f s t m t − n p a r s e T o S t r i n g ()<<” @ l i n e ”<<d e f s t m t − g e t f i l e i n f o ()−> g e t l i n e ( )
                         <d             >u                                                                       >
42                 <<”:”<< d e f s t m t − g e t f i l e i n f o ()−> g e t c o l ( ) <<e n d l ;
                                               >
43          }
44       }
45       return 0;
46   }



                                     Figure 22.2: Example source code using def use analysis


 1    − − − − − − − − − − − − − − − − − − − − − −
     − − − − − − − − − − − − − − − − − − − − − − −
 2   1 d e f i n i t i o n entry / e n t r i e s fo r x @ l i n e 4:5
 3   S g A s s i g n I n i t i a l i z e r 0 x103993d0
 4   int x = 9; @ line 3:3
 5    − − − − − − − − − − − − − − − − − − − − − −
     − − − − − − − − − − − − − − − − − − − − − − −
 6   1 d e f i n i t i o n entry / e n t r i e s fo r x @ l i n e 4:7
 7   S g A s s i g n I n i t i a l i z e r 0 x103993d0
 8   int x = 9; @ line 3:3



                                                         Figure 22.3: Output of the program
22.1. DEF-USE ANALYSIS                                                                       143

22.1.2     Accessing the Def-Use Results
For each CFN in the CFG, the definition and usage for variable references can be determined
with the public function calls:

vector <SgNode*> getDefFor(SgNode*, SgInitializedName*)
vector <SgNode*> getUseFor(SgNode*, SgInitializedName*)

    where SgNode* represents any control flow node and SgInitializedName any variable (be-
ing used or defined at that CFN). The result is a vector of possible CFN’s that either define
(getDefFor) or use (getUseFor) a specific variable.
    Figure 22.4 shows how the variable x is being declared and defined in CFN’s between node
1 and 6. Note that the definition is annotated along the edge. For instance at node 6, the edge
reads (6) DEF: x (3) = 5. This means that variable x was declared at CFN 3 but defined at
CFN 5.
    The second statement x=x+1 is represented by CFN’s from 7 to 12. One can see in the figure
that x is being re-defined at CFN 11. However, the definition of x within the same statement
happens at CFN 8. Hence, the definition of the right hand side x in the statement is at CFN 5
: (8) DEF: x (3) = 5.
    Another usage of the def-use analysis is to determine which variables actually are defined at
each CFN. The following function allows to query a CFN for all its variables (SgInitializedNames)
and the positions those variables are defined (SgNode):

std::multimap <SgInitializedName*, SgNode*> getDefMultiMapFor(SgNode*)
std::multimap <SgInitializedName*, SgNode*> getUseMultiMapFor(SgNode*)

   All public functions are described in DefuseAnalysis.h. To use the def-use analysis, one needs
to create an object of the class DefUseAnalysis and execute the run function. After that, the
described functions above help to evaluate definition and usage for each CFN.
144                                                                                   CHAPTER 22. DATAFLOW ANALYSIS


        ::main : ( 2 ) - [0x2b094efdb010] <SgFunctionDefinition> @line=1 :idx=0


                                            DEF: x ( 4 ) - 12


          : ( 3 ) - [0x2b094eef3900] <SgFunctionParameterList> @line=1 :idx=0




            : ( 4 ) - [0x2b094ef78438] initVar : x<SgInitializedName> x :idx=0


                                            DEF: x ( 4 ) - 4


                    : ( 5 ) - [0x1037fd40] <SgIntVal> @line=3 :idx=1


                                            DEF: x ( 4 ) - 4


               : ( 6 ) - [0x103993d0] <SgAssignInitializer> @line=3 :idx=1


                                            DEF: x ( 4 ) - 6


      : ( 7 ) - [0x2b094f067010] varDecl : x,<SgVariableDeclaration> @line=3 :idx=1


                                            DEF: x ( 4 ) - 6


            : ( 8 ) - [0x103be820] varRef : x<SgVarRefExp> @line=4 :idx=0


                                            DEF: x ( 4 ) - 6


            : ( 9 ) - [0x103be888] varRef : x<SgVarRefExp> @line=4 :idx=0


                                            DEF: x ( 4 ) - 6
                                            USE: x ( 4 ) - 9


                   : ( 10 ) - [0x1037fda8] <SgIntVal> @line=4 :idx=1


                                            DEF: x ( 4 ) - 6
                                            USE: x ( 4 ) - 9


                  : ( 11 ) - [0x103d8080] <SgAddOp> @line=4 :idx=2


                                            DEF: x ( 4 ) - 6
                                            USE: x ( 4 ) - 9


                 : ( 12 ) - [0x103f3610] <SgAssignOp> @line=4 :idx=2


                                            DEF: x ( 4 ) - 12


               : ( 13 ) - [0x1040ec00] <SgExprStatement> @line=4 :idx=1


                                            DEF: x ( 4 ) - 12


                   : ( 14 ) - [0x1037fe10] <SgIntVal> @line=5 :idx=1


                                            DEF: x ( 4 ) - 12


                 : ( 15 ) - [0x104243d0] <SgReturnStmt> @line=0 :idx=1


                                            DEF: x ( 4 ) - 12


        ::main : ( 2 ) - [0x2b094efdb010] <SgFunctionDefinition> @line=1 :idx=3




                                Figure 22.4: Def-Use graph for example program.
22.2. LIVENESS ANALYSIS                                                                           145

22.2       Liveness Analysis
Liveness analysis is a classic data flow analysis performed by compilers to calculate for each
program point the variables that may be potentially read before their next write (re-definition).
A variable is live at a point in a program’s execution path if it holds a value that may be needed
in the future.
    Fig. 22.5 shows an example program of how the liveness analysis is called in a ROSE-based
translator. It generates a dot graph showing def/use information within a control flow graph,
alone with live-in and live-out variables. This program (named as livenessAnalysis) is installed
under ROSE INST/bin as a standalone tool for users to experiment the liveness analysis of
ROSE.
    Figure 22.7 shows control flow graph with live variable information for the code(Fig. 22.5)
running on the input code(Fig. 22.6).

22.2.1     Access live variables
After calling liveness analysis, one can access live-in and live-out variables from a translator based
on the virtual control flow graph node. Figure 22.8 shows an example function which retrieves
the live-in and live-out variables for a for loop. The code accesses the CFG node (showing in
Figure 22.7) of a for statement and retrieve live-in variables of the true edge’s target node as
the live-in variables of the loop body. Similarly, the live-out variables of the loop are obtained
by getting the live-in variables of the node right after the loop (target node of the false edge).
     146                                                                                     CHAPTER 22. DATAFLOW ANALYSIS




 1   #i n c l u d e ” r o s e . h”
 2   #i n c l u d e <s t r i n g >
 3   #i n c l u d e <i o s t r e a m >
 4   #i n c l u d e <f s t r e a m >
 5   u s i n g namespace s t d ;
 6
 7   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 8   {
 9       v e c t o r <s t r i n g > a r g v L i s t ( argv , a r g v + a r g c ) ;
10       SgProject ∗ project = frontend ( argvList ) ;
11       i f ( p r o j e c t − g e t f i l e L i s t ( ) . s i z e ( ) ==0)
                                >
12           return 0;
13       // P r e p a r e t h e Def−Use A n a l y s i s
14       DFAnalysis ∗ d e f u s e = new D e f U s e A n a l y s i s ( p r o j e c t ) ;
15       b o o l debug = f a l s e ;
16       defuse−      >run ( debug ) ;
17       i f ( debug )
18           defuse−     >dfaToDOT ( ) ;
19
20       // P r e p a r e l i v e n e s s a n a l y s i s
21       L i v e n e s s A n a l y s i s ∗ l i v = new L i v e n e s s A n a l y s i s ( debug , ( D e f U s e A n a l y s i s ∗ ) d e f u s e ) ;
22       ROSE ASSERT ( l i v != NULL ) ;
23
24       // Find a l l f u n c t i o n d e f i n i t i o n s
25       R o s e S T L C o n t a i n e r<SgNode∗> n o d e L i s t= NodeQuery : : querySubTree ( p r o j e c t , V S g F u n c t i o n D e f i n i t i o n ) ;
26       s t d : : v e c t o r <FilteredCFGNode < I s D F A F i l t e r > > d f a F u n c t i o n s ;
27       R o s e S T L C o n t a i n e r<SgNode ∗ > : : c o n s t i t e r a t o r i = n o d e L i s t . b e g i n ( ) ;
28       b o o l abortme= f a l s e ;
29       f o r ( ; i != n o d e L i s t . end ();++ i )
30       {
31           SgFunctionDefinition ∗ func = i s S g F u n c t i o n D e f i n i t i o n (∗ i ) ;
32           // run l i v e n e s s a n a l y s i s
33           FilteredCFGNode <I s D F A F i l t e r > r e m s o u r c e = l i v −      >run ( f u n c , abortme ) ;
34           i f ( abortme ) {
35               c e r r <<”E r r o r : L i v e n e s s a n a l y s i s i s ABORTING . ” << e n d l ;
36              ROSE ASSERT( f a l s e ) ;
37           }
38           i f ( r e m s o u r c e . getNode ( ) ! =NULL)
39               dfaFunctions . push back ( rem source ) ;
40       }
41
42       SgFilePtrList f i l e l i s t = project− g e t f i l e L i s t ( ) ;
                                                                       >
43       s t d : : s t r i n g f i r s t F i l e N a m e = S t r i n g U t i l i t y : : st r i p Pa t hF r o m Fi l eN a m e ( f i l e l i s t [0] − > g e t F i l e N a m e ( ) ) ;
44       s t d : : s t r i n g f i l e N a m e = f i r s t F i l e N a m e +” l i v e n e s s . d o t ” ;
45       std : : ofstream f s ( fileName . c s t r ( ) ) ;
46       dfaToDot ( f s , s t r i n g ( ” v a r ” ) , d f a F u n c t i o n s , ( D e f U s e A n a l y s i s ∗ ) d e f u s e , l i v ) ;
47       fs . close ();
48       return 0;
49   }



                                     Figure 22.5: Example source code using liveness analysis
     22.2. LIVENESS ANALYSIS                                            147




 1   int a [100];
 2
 3   void foo2 ()
 4   {
 5     int i ;
 6     i n t tmp ;
 7     tmp = 1 0 ;
 8     f o r ( i =0; i <100; i ++)
 9     {
10         a [ i ] = tmp ;
11         tmp =a [ i ]+ i ;
12     }
13       a[0] = 1 ;
14   }



                                     Figure 22.6: Example input code.
148                                                                                                                                                                                                                                     CHAPTER 22. DATAFLOW ANALYSIS


                                                                                                                                                          ::foo2 : ( 3 ) - [0x2af6830b5010] <SgFunctionDefinition> @line=3 :idx=0


                                                                                                                                                                                                       out :
                                                                                                                                                                                                      visited : 3
                                                                                                                                                                                                        in :


                                                                                                                                                                : ( 4 ) - [0x2af682eab4a0] <SgFunctionParameterList> @line=3 :idx=0


                                                                                                                                                                                                       out :
                                                                                                                                                                                                      visited : 1
                                                                                                                                                                                                        in :


                                                                                                                                                                  : ( 5 ) - [0x2af682f301e8] initVar : i<SgInitializedName> i :idx=0


                                                                                                                                                                                                        out :
                                                                                                                                                                                                       visited : 1
                                                                                                                                                                                                         in :
                                                                                                                                                                                                    DEF: i ( 5 ) - 5


                                                                                                                                                        : ( 6 ) - [0x2af682f93288] varDecl : i,<SgVariableDeclaration> @line=5 :idx=1


                                                                                                                                                                                                      out :
                                                                                                                                                                                                     visited : 2
                                                                                                                                                                                                       in :


                                                                                                                                                            : ( 7 ) - [0x2af682f30310] initVar : tmp<SgInitializedName> tmp :idx=0


                                                                                                                                                                                                          out :
                                                                                                                                                                                                         visited : 2
                                                                                                                                                                                                           in :
                                                                                                                                                                                                    DEF: tmp ( 7 ) - 7


                                                                                                                                                      : ( 8 ) - [0x2af682f93500] varDecl : tmp,<SgVariableDeclaration> @line=6 :idx=1


                                                                                                                                                                                                      out :
                                                                                                                                                                                                     visited : 2
                                                                                                                                                                                                       in :


                                                                                                                                                                 : ( 9 ) - [0x1d4f77e0] varRef : tmp<SgVarRefExp> @line=7 :idx=0


                                                                                                                                                                                                      out :
                                                                                                                                                                                                     visited : 2
                                                                                                                                                                                                       in :


                                                                                                                                                                         : ( 10 ) - [0x1d510f00] <SgIntVal> @line=7 :idx=1


                                                                                                                                                                                                      out :
                                                                                                                                                                                                     visited : 2
                                                                                                                                                                                                       in :


                                                                                                                                                                       : ( 11 ) - [0x1d52a5b0] <SgAssignOp> @line=7 :idx=2


                                                                                                                                                                                                        out : tmp,
                                                                                                                                                                                                          visited : 2
                                                                                                                                                                                                          in : tmp,
                                                                                                                                                                                                    DEF: tmp ( 7 ) - 11


                                                                                                                                                                     : ( 12 ) - [0x1d545b40] <SgExprStatement> @line=7 :idx=1


                                                                                                                                                                                                     out : tmp,
                                                                                                                                                                                                      visited : 2
                                                                                                                                                                                                      in : tmp,


                                                                                                                                                                  : ( 13 ) - [0x1d4f7848] varRef : i<SgVarRefExp> @line=8 :idx=0


                                                                                                                                                                                                     out : tmp,
                                                                                                                                                                                                      visited : 2
                                                                                                                                                                                                      in : tmp,


                                                                                                                                                                         : ( 14 ) - [0x1d510f68] <SgIntVal> @line=8 :idx=1


                                                                                                                                                                                                     out : tmp,
                                                                                                                                                                                                      visited : 2
                                                                                                                                                                                                      in : tmp,


                                                                                                                                                                       : ( 15 ) - [0x1d52a620] <SgAssignOp> @line=8 :idx=2


                                                                                                                                                                                                      out : tmp,i,
                                                                                                                                                                                                        visited : 2
                                                                                                                                                                                                       in : tmp,i,
                                                                                                                                                                                                    DEF: i ( 5 ) - 15


                                                                                                                                                                     : ( 16 ) - [0x1d545b98] <SgExprStatement> @line=8 :idx=1


                                                                                                                                                                                                    out : tmp,i,
                                                                                                                                                                                                      visited : 2
                                                                                                                                                                                                     in : tmp,i,


                                                                                                                                                                   : ( 17 ) - [0x1d55d590] <SgForInitStatement> @line=8 :idx=1


                                                                                                                                                                                                    out : tmp,i,
                                                                                                                                                                                                      visited : 3
                                                                                                                                                                                                     in : tmp,i,


                                                                                                                                                                  : ( 18 ) - [0x1d4f78b0] varRef : i<SgVarRefExp> @line=8 :idx=0


                                                                                                                                                                                  out : tmp,i,
                                                                                                                                                                                    visited : 3
                                                                                                                                                                                   in : tmp,i,
                                                                                                                                                                                USE: i ( 5 ) - 18


                                                                                                                     : ( 19 ) - [0x1d510fd0] <SgIntVal> @line=8 :idx=1


                                                                                                                                                out : tmp,i,
                                                                                                                                                  visited : 3
                                                                                                                                                 in : tmp,i,


                                                                                                        : ( 20 ) - [0x1d577050] <SgLessThanOp> @line=8 :idx=2


                                                                                                                                    out : tmp,i,
                                                                                                                                      visited : 2
                                                                                                                                     in : tmp,i,


                                                                                                    : ( 21 ) - [0x1d545bf0] <SgExprStatement> @line=8 :idx=1


                                                                                                                                 out : tmp,i,
                                                                                                                                   visited : 2
                                                                                                                                  in : tmp,i,


                                                                                                  : ( 22 ) - [0x2af683141010] <SgForStatement> @line=8 :idx=2


                                                                                                   out : tmp,i,                  out : tmp,i,
                                                                                                     visited : 2                  visited : 2
                                                                                                    in : tmp,i,                      in :


       : ( 23 ) - [0x1d4f79e8] varRef : a<SgVarRefExp> @line=0 :idx=0                           : ( 24 ) - [0x1d4f7d90] varRef : a<SgVarRefExp> @line=0 :idx=0


                                      out : tmp,i,                                                                                 out :
                                        visited : 1                                                                               visited : 2
                                       in : tmp,i,                                                                                  in :


    : ( 25 ) - [0x1d4f7a50] varRef : i<SgVarRefExp> @line=10 :idx=0                                    : ( 26 ) - [0x1d511038] <SgIntVal> @line=13 :idx=1


                                     out : tmp,i,
                                                                                                                                   out :
                                       visited : 1
                                                                                                                                  visited : 2
                                      in : tmp,i,
                                                                                                                                    in :
                                   USE: i ( 5 ) - 25


      : ( 27 ) - [0x1d5ae160] <SgPntrArrRefExp> @line=10 :idx=2                                    : ( 28 ) - [0x1d5ae240] <SgPntrArrRefExp> @line=13 :idx=2


                                   out : tmp,i,                                                                                    out :
                                     visited : 2                                                                                  visited : 2
                                    in : tmp,i,                                                                                     in :


  : ( 29 ) - [0x1d4f7ab8] varRef : tmp<SgVarRefExp> @line=10 :idx=0                                    : ( 30 ) - [0x1d5110a0] <SgIntVal> @line=13 :idx=1


                                        out : i,
                                                                                                                                   out :
                                       visited : 2
                                                                                                                                  visited : 2
                                         in : i,
                                                                                                                                    in :
                                  USE: tmp ( 7 ) - 29


        : ( 31 ) - [0x1d52a690] <SgAssignOp> @line=10 :idx=2                                         : ( 32 ) - [0x1d52a770] <SgAssignOp> @line=13 :idx=2


                                       out : i,a,                                                                                     out :
                                      visited : 2                                                                                   visited : 2
                                        in : i,a,                                                                                      in :
                                  DEF: ::a ( 2 ) - 31                                                                           DEF: ::a ( 2 ) - 32

                                                                                                                                                                                                      out : tmp,i,
                                                                                                                                                                                                        visited : 3
      : ( 33 ) - [0x1d545c48] <SgExprStatement> @line=10 :idx=1                                    : ( 34 ) - [0x1d545cf8] <SgExprStatement> @line=13 :idx=1
                                                                                                                                                                                                       in : tmp,i,
                                                                                                                                                                                                    DEF: i ( 5 ) - 44

                                    out : i,a,                                                                                     out :
                                    visited : 2                                                                                   visited : 2
                                     in : i,a,                                                                                      in :


  : ( 35 ) - [0x1d4f7b20] varRef : tmp<SgVarRefExp> @line=11 :idx=0                          ::foo2 : ( 3 ) - [0x2af6830b5010] <SgFunctionDefinition> @line=3 :idx=3


                                                                               out : i,a,
                                                                               visited : 2
                                                                                in : i,a,


                                                                        : ( 36 ) - [0x1d4f7bf0] varRef : a<SgVarRefExp> @line=0 :idx=0


                                                                                                                              out : i,
                                                                                                                            visited : 2
                                                                                                                               in : i,
                                                                                                                        USE: ::a ( 2 ) - 36


                                                                                             : ( 37 ) - [0x1d4f7c58] varRef : i<SgVarRefExp> @line=11 :idx=0


                                                                                                                                    out : i,
                                                                                                                                   visited : 2
                                                                                                                                      in : i,
                                                                                                                                USE: i ( 5 ) - 37


                                                                                                    : ( 38 ) - [0x1d5ae1d0] <SgPntrArrRefExp> @line=11 :idx=2


                                                                                                                                       out : i,
                                                                                                                                      visited : 2
                                                                                                                                        in : i,


                                                                                                    : ( 39 ) - [0x1d4f7cc0] varRef : i<SgVarRefExp> @line=11 :idx=0


                                                                                                                                         out : i,
                                                                                                                                        visited : 2
                                                                                                                                           in : i,
                                                                                                                                     USE: i ( 5 ) - 39


                                                                                                            : ( 40 ) - [0x1d5c9a90] <SgAddOp> @line=11 :idx=2


                                                                                                                                        out : i,
                                                                                                                                       visited : 2
                                                                                                                                         in : i,


                                                                                                           : ( 41 ) - [0x1d52a700] <SgAssignOp> @line=11 :idx=2


                                                                                                                                         out : tmp,i,
                                                                                                                                           visited : 2
                                                                                                                                          in : tmp,i,
                                                                                                                                     DEF: tmp ( 7 ) - 41


                                                                                                        : ( 42 ) - [0x1d545ca0] <SgExprStatement> @line=11 :idx=1


                                                                                                                                      out : tmp,i,
                                                                                                                                        visited : 2
                                                                                                                                       in : tmp,i,


                                                                                                     : ( 43 ) - [0x1d4f7918] varRef : i<SgVarRefExp> @line=8 :idx=0


                                                                                                                                                                          out : tmp,
                                                                                                                                                                           visited : 3
                                                                                                                                                                           in : tmp,
                                                                                                                                                                       USE: i ( 5 ) - 43


                                                                                                                                                : ( 44 ) - [0x1d592600] <SgPlusPlusOp> @line=8 :idx=1




                       Figure 22.7: Control flow graph annotated with live variables for example program.
     22.2. LIVENESS ANALYSIS                                                                                                        149




 1   v o i d g e t L i v e V a r i a b l e s ( L i v e n e s s A n a l y s i s ∗ l i v , S gF or St at ement ∗ f o r s t m t )
 2   {
 3      ROSE ASSERT( l i v != NULL ) ;
 4      ROSE ASSERT( f o r s t m t != NULL ) ;
 5       s t d : : v e c t o r <S g I n i t i a l i z e d N a m e ∗> l i v e I n s , l i v e O u t s ;
 6
 7       // For SgForStatement , v i r t u a l CFG node which i s i n t e r e s t i n g ha s an i n d e x number o f 2 ,
 8       // a s shown i n i t s d o t graph ’ s node c a p t i o n .
 9       // ”<SgForStatement> @ 8 : 2” means t h i s node i s f o r a f o r s t a t e m e n t a t s o u r c e l i n e 8 , w i t h an i n d e x 2 .
10       CFGNode c f g n o d e ( f o r s t m t , 2 ) ;
11       FilteredCFGNode<I s D F A F i l t e r > f i l t e r n o d e= FilteredCFGNode<I s D F A F i l t e r > ( c f g n o d e ) ;
12
13       // Check e d g e s
14       v e c t o r <FilteredCFGEdge < I s D F A F i l t e r > > o u t e d g e s = f i l t e r n o d e . out Edges ( ) ;
15       ROSE ASSERT( o u t e d g e s . s i z e ( ) == 2 ) ;
16       v e c t o r <FilteredCFGEdge < I s D F A F i l t e r > > : : i t e r a t o r i t e r = o u t e d g e s . b e g i n ( ) ;
17
18       f o r ( ; i t e r != o u t e d g e s . end ( ) ; i t e r ++)
19       {
20           FilteredCFGEdge < I s D F A F i l t e r > e d g e= ∗ i t e r ;
21           // one t r u e e d g e g o i n g i n t o t h e l o o p body
22           // x . Live−i n ( l o o p ) = l i v e −i n ( f i r s t −stmt−i n −l o o p )
23           i f ( e d g e . c o n d i t i o n ()== eckTrue )
24           {
25               SgNode∗ f i r s t n o d e= e d g e . t a r g e t ( ) . getNode ( ) ;
26               l i v e I n s = liv − etIn ( firstnode ) ;
                                           >g
27           }
28           // one f a l s e e d g e g o i n g o u t o f l o o p
29           // x . l i v e −o u t ( l o o p ) = l i v e −i n ( f i r s t −stmt−a f t e r −l o o p )
30           e l s e i f ( e d g e . c o n d i t i o n ()== e c k F a l s e )
31           {
32               SgNode∗ f i r s t n o d e= e d g e . t a r g e t ( ) . getNode ( ) ;
33               liveOuts0 = liv − etIn ( firstnode ) ;
                                              >g
34           }
35           else
36           {
37               c e r r <<”Unexpected CFG o u t e d g e t y p e f o r SgForStmt!”<< e n d l ;
38               ROSE ASSERT( f a l s e ) ;
39           }
40       } // end f o r ( e d g e s )
41   }



            Figure 22.8: Example code retrieving live variables based on virtual control flow graph
150   CHAPTER 22. DATAFLOW ANALYSIS
Chapter 23

Generating the Call Graph (CG)

The formal definition of a call graph is:

’A diagram that identifies the modules in a system or computer program and shows which
modules call one another.’ IEEE

A call graph shows all function call paths of an arbitrary code. These paths are found by
following all function calls in a function, where a function in the graph is represented by a node
and each possible function call by an edge (arrow). To make a call graph this process is redone
for every called function until all edges are followed and there are no ungraphed functions. ROSE
has an in-build mechanism for generating call graphs.
    ROSE provides support for generating call graphs, as defined in src/midend/programAnal-
ysis/CallGraphAnalysis/CallGraph.h. Figure 23 shows the code required to generate the call
graph for each function of an application. Using the input code shown in figure 23 the first func-
tion’s call graph is shown in figure 23.3. A standalone tool named buildCallGraph is installed
under ROSE INSTALL/bin so users can use it to generate call graphs in dot format.




                                               151
     152                                                          CHAPTER 23. GENERATING THE CALL GRAPH (CG)




 1   #i n c l u d e ” r o s e . h”
 2   #i n c l u d e <CallGraph . h>
 3   #i n c l u d e <i o s t r e a m >
 4   u s i n g namespace s t d ;
 5
 6   // A F u n c t i o n o b j e c t u s e d a s a p r e d i c a t e t h a t d e t e r m i n e s which f u n c t i o n s a r e
 7   // t o be r e p r e s e n t e d i n t h e c a l l graph .
 8   s t r u c t k e e p F u n c t i o n : p u b l i c u n a r y f u n c t i o n <b o o l , S g F u n c t i o n D e c l a r a t i o n ∗>{
 9       public :
10           bool operator ( ) ( SgFunctionDeclaration ∗ funcDecl ){
11              bool returnValue = true ;
12              ROSE ASSERT( f u n c D e c l != NULL ) ;
13              s t r i n g f i l e n a m e = f u n c D e c l − g e t f i l e i n f o ()−> g e t f i l e n a m e ( ) ;
                                                               >
14
15                  // F i l t e r o u t f u n c t i o n s from t h e ROSE p r e i n c l u d e h e a d e r f i l e
16                  i f ( f i l e n a m e . f i n d ( ” r o s e e d g r e q u i r e d m a c r o s a n d f u n c t i o n s ” ) ! = s t r i n g : : npos )
17                      returnValue = f a l s e ;
18
19                  // F i l t e r o u t c o m p i l e r g e n e r a t e d f u n c t i o n s
20                  i f ( f u n c D e c l − g e t f i l e i n f o ()−> i s C o m p i l e r G e n e r a t e d ()== t r u e )
                                           >
21                      r e t u r n V a l u e= f a l s e ;
22
23                  return returnValue ;
24             }
25   };
26
27   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
28   {
29       S g P r o j e c t ∗ p r o j e c t = new S g P r o j e c t ( a r g c , a r g v ) ;
30      ROSE ASSERT ( p r o j e c t != NULL ) ;
31
32        if       ( p r o j e c t − g e t f i l e L i s t ( ) . s i z e ( ) >=1)
                                    >
33        {
34             // C o n s t r u c t a c a l l Graph
35             C a l l G r a p h B u i l d e r CGBuilder ( p r o j e c t ) ;
36             CGBuilder . b u i l d C a l l G r a p h ( k e e p F u n c t i o n ( ) ) ;
37
38             // Output t o a d o t f i l e
39             AstDOTGeneration d o t g e n ;
40             SgFilePtrList f i l e l i s t = project− g e t f i l e L i s t ( ) ;
                                                                                  >
41             s t d : : s t r i n g f i r s t F i l e N a m e = S t r i n g U t i l i t y : : s t r i p Pa t h F r o mF i l e N a me ( f i l e l i s t [0] − > g e t F i l e N a m e ( ) ) ;
42             d o t g e n . w r i t e I n c i d e n c e G r a p h T o D O T F i l e ( CGBuilder . getGraph ( ) , f i r s t F i l e N a m e +” c a l l G r a p h . d o t ” ) ;
43        }
44
45        r e t u r n 0 ; // backend ( p r o j e c t ) ;
46   }



                              Figure 23.1: Example source code showing visualization of call graph.
                                                                                                                                      153




 1   // s i m p l e (   t r i v i a l ) example c o d e u s e d t o d e m o n s t r a t e t h e   c a l l graph g e n e r a t i o n
 2   class A
 3   {
 4   public :
 5                int     f1 () { return 0;}
 6                int     f 2 ( ) { p f = &A : : f 1 ;   r e t u r n ( t h i s −>∗p f ) ( ) ; }
 7                int     (A : : ∗ p f ) ( ) ;
 8   };
 9
10   void    foo1   ();
11   void    foo2   ();
12   void    foo3   ();
13   void    foo4   ();
14
15   void foo1 ()
16      {
17        foo1 ( ) ;
18        foo2 ( ) ;
19        foo3 ( ) ;
20        foo4 ( ) ;
21      }
22
23   void foo2 ()
24      {
25        foo1 ( ) ;
26        foo2 ( ) ;
27        foo3 ( ) ;
28        foo4 ( ) ;
29      }
30
31   void foo3 ()
32      {
33        foo1 ( ) ;
34        foo2 ( ) ;
35        foo3 ( ) ;
36        foo4 ( ) ;
37      }
38
39   void foo4 ()
40      {
41        foo1 ( ) ;
42        foo2 ( ) ;
43        foo3 ( ) ;
44        foo4 ( ) ;
45      }
46
47   i n t main ( )
48        {
49          foo1 ( ) ;
50          foo2 ( ) ;
51          foo3 ( ) ;
52          foo4 ( ) ;
53
54           return 0;
55       }



                          Figure 23.2: Example source code used as input to build call graph.
154                            CHAPTER 23. GENERATING THE CALL GRAPH (CG)




                    ::main                              ::A::f2
                       0                                   0
                  0x1e9e7b0                           0x1e9e890




                    ::foo1                              ::A::f1
                       0                                   0
                  0x1e9e5f0                           0x1e9e820




        ::foo2
           0
      0x1e9e660




                        ::foo4
                           0
                      0x1e9e740




                    ::foo3
                       0
                  0x1e9e6d0



      Figure 23.3: Call graph for function in input code file: inputCode BuildCG.C.
Chapter 24

Dataflow Analysis based Virtual
Function Analysis

C++ Virtual function provides polymorphism to the developer but makes it difficult for compil-
ers to do optimizations. Virtual functions are usually resolved at runtime from the vtable. It’s
very difficult for a compiler to know which functions will be called at compile time. ROSE pro-
vides a flow sensitive dataflow analysis based approach to cut down the set of possible function
calls. The code for Virtual Function Analysis is located in src/midend/programAnalysis/Vir-
tualFunctionAnalysis/VirtualFunctionAnalysis.h. It also provides a mechanism to resolve any
function calls. It’s a whole program analysis and supposed to be expensive. It memorizes all the
resolved function calls for any call site, so that subsequent calls are resolved faster.
    Figure 24.1 shows the code required to generate the pruned call graph. Using the input code
shown in figure 24 Call Graph Analysis generates call graph shown in figure 24.3. Executing
dataflow analysis to resolve virtual function calls resulted in the figure 24.4.




                                              155
     156          CHAPTER 24. DATAFLOW ANALYSIS BASED VIRTUAL FUNCTION ANALYSIS



 1   #i n c l u d e ” s a g e 3 b a s i c . h”
 2
 3   #i n c l u d e <s t r i n g >
 4   #i n c l u d e <i o s t r e a m >
 5
 6   #i n c l u d e ” V i r t u a l F u n c t i o n A n a l y s i s . h”
 7   u s i n g namespace b o o s t ;
 8
 9
10   u s i n g namespace s t d ;
11   u s i n g namespace b o o s t ;
12   // A F u n c t i o n o b j e c t u s e d a s a p r e d i c a t e t h a t d e t e r m i n e s which f u n c t i o n s a r e
13   // t o be r e p r e s e n t e d i n t h e c a l l graph .
14   s t r u c t k e e p F u n c t i o n : p u b l i c u n a r y f u n c t i o n <b o o l , S g F u n c t i o n D e c l a r a t i o n ∗>{
15       public :
16           bool operator ( ) ( SgFunctionDeclaration ∗ funcDecl ){
17              bool returnValue = true ;
18              ROSE ASSERT( f u n c D e c l != NULL ) ;
19              s t r i n g f i l e n a m e = f u n c D e c l − g e t f i l e i n f o ()−> g e t f i l e n a m e ( ) ;
                                                               >
20
21               // F i l t e r o u t f u n c t i o n s from t h e ROSE p r e i n c l u d e h e a d e r f i l e
22               i f ( f i l e n a m e . f i n d ( ” r o s e e d g r e q u i r e d m a c r o s a n d f u n c t i o n s ” ) ! = s t r i n g : : npos )
23                   returnValue = f a l s e ;
24
25               // F i l t e r o u t c o m p i l e r g e n e r a t e d f u n c t i o n s
26               i f ( f u n c D e c l − g e t f i l e i n f o ()−> i s C o m p i l e r G e n e r a t e d ()== t r u e )
                                        >
27                   r e t u r n V a l u e= f a l s e ;
28
29                return returnValue ;
30          }
31   };
32
33   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] ) {
34
35          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
36          ROSE ASSERT( p r o j e c t != NULL ) ;
37
38          CallGraphBuilder b u i l d e r ( p r o j e c t ) ;
39          b u i l d e r . buildCallGraph ( keepFunction ( ) ) ;
40          // G e n e r a t e c a l l graph i n d o t f o r m a t
41          AstDOTGeneration d o t g e n ;
42          d o t g e n . w r i t e I n c i d e n c e G r a p h T o D O T F i l e ( b u i l d e r . getGraph ( ) , ” o r i g i n a l c a l l g r a p h . d o t ” ) ;
43
44           S g F u n c t i o n D e c l a r a t i o n ∗ mainDecl = S a g e I n t e r f a c e : : f i n d M a i n ( p r o j e c t ) ;
45           i f ( mainDecl == NULL) {
46                          s t d : : c e r r < ”Can ’ t e x e c u t e V i r t u a l F u n c t i o n A n a l y s i s w i t h o u t main f u n c t i o n \n ” ;
                                                <
47                           return 0;
48             }
49
50           V i r t u a l F u n c t i o n A n a l y s i s ∗ a n a l = new V i r t u a l F u n c t i o n A n a l y s i s ( p r o j e c t ) ;
51           anal−    >run ( ) ;
52
53           anal− runeCallGraph ( b u i l d e r ) ;
                  >p
54
55              AstDOTGeneration d o t g e n 2 ;
56              d o t g e n 2 . w r i t e I n c i d e n c e G r a p h T o D O T F i l e ( b u i l d e r . getGraph ( ) , ” p r u n e d c a l l g r a p h . d o t ” ) ;
57
58           d e l e t e anal ;
59
60
61           return 0;
62   }



                                   Figure 24.1: Source code to perform virtual function analysis
                                                                                                 157


 1   c l a s s Animal {
 2     public :
 3         v i r t u a l v o i d s h o u t ( ) {}
 4   };
 5
 6   c l a s s dog : p u b l i c Animal {
 7       public :
 8       v i r t u a l v o i d s h o u t ( ) {}
 9   };
10   c l a s s t e r r i e r : p u b l i c dog {
11       public :
12       v i r t u a l v o i d s h o u t ( ) {}
13   };
14   class yterrier : public t e r r i e r {
15       public :
16       v i r t u a l v o i d s h o u t ( ) {}
17   };
18
19   i n t main ( v o i d ) {
20
21   Animal ∗∗p , ∗∗ q ;
22   dog ∗x , d ;
23   t e r r i e r ∗y ;
24
25   y = new y t e r r i e r ;
26   x = &d ;
27   p = ( Animal ∗∗)&x ;
28   q = p;
29   ∗p = y ;
30
31     >s
     x− h o u t ( ) ;
32
33   return 0;
34
35   }



                 Figure 24.2: Example source code used as input for Virtual Function Analysis.




                                                 ::main         ::Animal::shout
                                                    0                 0
                                               0x16c30c0          0x16c3130




           ::dog::shout                      ::terrier::shout    ::yterrier::shout
                0                                    0                   0
           0x16c31a0                           0x16c3210           0x16c3280



      Figure 24.3: Call graph generated by Call Graph Analysis for input code in inputCode vfa.C.
158      CHAPTER 24. DATAFLOW ANALYSIS BASED VIRTUAL FUNCTION ANALYSIS




         ::main           ::Animal::shout    ::dog::shout       ::terrier::shout
            0                   0                 0                     0
       0x1ebe0c0            0x1ebe130        0x1ebe1a0            0x1ebe210




      ::yterrier::shout
              0
        0x1ebe280



Figure 24.4: Call graph resulted from Virtual Function Analysis for input code in input-
Code vfa.C.
     Chapter 25

     Generating the Class Hierarchy
     Graph

 1   #i n c l u d e ” r o s e . h”
 2   #i n c l u d e ” CallGraph . h”
 3   #i n c l u d e <b o o s t / f o r e a c h . hpp>
 4
 5   #d e f i n e f o r e a c h BOOST FOREACH
 6
 7   u s i n g namespace s t d ;
 8
 9   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
10   {
11              S g P r o j e c t ∗ p r o j e c t = new S g P r o j e c t ( a r g c , a r g v ) ;
12
13                 // C o n s t r u c t c l a s s h i e r a r c h y graph
14                 ClassHierarchyWrapper h i e r ( p r o j e c t ) ;
15
16                // D i s p l a y t h e a n c e s t o r s o f e a c h c l a s s
17                v e c t o r <S g C l a s s D e f i n i t i o n ∗> a l l C l a s s e s = S a g e I n t e r f a c e : : querySubTree<S g C l a s s D e f i n i t i o n >( p r o j e c t ,   V SgClassDefinitio
18                foreach ( SgClassDefinition ∗ classDef , a l l C l a s se s )
19                {
20                               p r i n t f ( ” \ n%s s u b c l a s s e s : ” , c l a s s D e f − e t d e c l a r a t i o n ()−> get name ( ) . s t r ( ) ) ;
                                                                                                   >g
21                               foreach ( SgClassDefinition ∗ subclass , hier . getSubclasses ( classDef ))
22                               {
23                                               p r i n t f (”% s , ” , s u b c l a s s − e t d e c l a r a t i o n ()−> get name ( ) . s t r ( ) ) ;
                                                                                          >g
24                               }
25                }
26
27                 return 0;
28   }



                Figure 25.1: Example source code showing visualization of class hierarchy graph.

        For C++, because of multiple inheritance, a class hierarchy graph is a directed graph with
     pointers from a class to a superclass. A superclass is a class which does not inherit from any
     other class. A class may inherit from a superclass by inheriting from another class which does
     rather than by a direct inheritance.
        Figure 25 shows the code required to generate the class hierarchy graph for each class of an

                                                                             159
    160                                     CHAPTER 25. GENERATING THE CLASS HIERARCHY GRAPH

    application. Using the input code shown in figure 25 the first function’s call graph is shown in
    figure 25.3.

1   c l a s s A{ } ;
2
3   class B :          p u b l i c A{ } ;
4
5   class C :          p u b l i c B{ } ;



                Figure 25.2: Example source code used as input to build class hierarchy graph.

          Figure 25.3 shows the class hierarchy graph for the classes in the input code in figure 25.
                                                                                     161




Figure 25.3: Class hierarchy graph in input code file: inputCode ClassHierarchyGraph.C.
162   CHAPTER 25. GENERATING THE CLASS HIERARCHY GRAPH
Chapter 26

Database Support

This chapter is specific to support in ROSE for persistent storage. ROSE uses the SQLite
database and makes it simple to store data in the database for retrieval in later phases of
processing large multiple file projects.                                                     FIXME: Need more information
                                                                                                                             here.

26.1      ROSE DB Support for Persistent Analysis
This section presents figure 26.3, a simple C++ source code using a template. It is used as a
basis for showing how template instantiations are handled within ROSE. An example translator
using a database connection to store function information is shown in Fig.26.1 and Fig.26.2. The
output by the translator operating on the C++ source code is shown in Fig. 26.4.


26.2      Call Graph for Multi-file Application
This section shows an example of the use of the ROSE Database mechanism where information
is stored after processing each file as part of generating the call graph for a project consisting
of multiple files. The separate files are show in figures 26.3 and ??. These files are processed
using the translator in figure ?? to generate the final project call graph shown in figure ??.            FIXME: This example still
                                                                                                    needs to be implemented to use
                                                                                                          the new ROSE call graph
                                                                                                                        generator.
26.3      Class Hierarchy Graph
This section presents a translator in figure ??, to generate the class hierarchy graph of the
example shown in figure ??. The input is a multi-file application show in figure ?? and figure ??.
This example is incomplete.                                                                         FIXME: This example is still
                                                                                                                   incomplete.




                                              163
     164                                                                                   CHAPTER 26. DATABASE SUPPORT


 1   // Example ROSE T r a n s l a t o r : u s e d f o r              t e s t i n g ROSE i n f r a s t r u c t u r e
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   u s i n g namespace s t d ;
 6
 7   // DQ ( 9 / 9 / 2 0 0 5 ) : Don ’ t i n c l u d e t h e d a t a b a s e by d e f a u l t
 8   // TPS ( 0 1 Dec2008 ) : Enabled mysql and t h i s f a i l s .
 9   // seems l i k e i t i s n o t s u p p o s e d t o be i n c l u d e d
10   #i f 0
11   //# i f d e f HAVE MYSQL
12       #i n c l u d e ” GlobalDatabaseConnectionMYSQL . h”
13   #e n d i f
14
15   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
16        {
17   // TPS ( 0 1 Dec2008 ) : Enabled mysql and t h i s f a i l s .
18   // seems l i k e i t i s n o t s u p p o s e d t o be i n c l u d e d
19   #i f 0
20          //# i f d e f HAVE MYSQL
21       // B u i l d t h e Data b a s e
22          G l o b a l D a t a b a s e C o n n e c t i o n ∗gDB ;
23          gDB = new G l o b a l D a t a b a s e C o n n e c t i o n ( ” functionNameDataBase ” ) ;
24                  >
            gDB− i n i t i a l i z e ( ) ;
25          s t r i n g command = ” ” ;
26          command = command + ”CREATE TABLE F u n c t i o n s ( name TEXT, c o u n t e r                                      );”;
27
28           Query ∗q = gDB−      >getQuery ( ) ;
29             >s
             q− e t ( command ) ;
30             >e
             q− x e c u t e ( ) ;
31
32            if        >s
                 ( q− u c c e s s ( ) != 0 )
33                                                                                  >e
                     c o u t << ” E r r o r c r e a t i n g schema : ” << q− r r o r ( ) << ”\n ” ;
34      // A l t e r n a t i v e s y n t a x , but d o e s n o t p e r m i t a c c e s s t o e r r o r m e s s a g e s and e x i t c o d e s
35                >e
        // gDB− x e c u t e ( command . c s t r ( ) ) ;
36   #e n d i f
37
38      // B u i l d t h e AST u s e d by ROSE
39         SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
40
41      // Run i n t e r n a l c o n s i s t e n c y t e s t s on AST
42         AstTests : : runAllTests ( p r o j e c t ) ;
43
44      // B u i l d a l i s t o f f u n c t i o n s w i t h i n t h e AST
45         R o s e S T L C o n t a i n e r<SgNode∗> f u n c t i o n D e c l a r a t i o n L i s t =
46                  NodeQuery : : querySubTree ( p r o j e c t , V S g F u n c t i o n D e c l a r a t i o n ) ;
47
48            int counter = 0;
49            f o r ( R o s e S T L C o n t a i n e r<SgNode ∗ > : : i t e r a t o r i = f u n c t i o n D e c l a r a t i o n L i s t . b e g i n ( ) ;
50                              i != f u n c t i o n D e c l a r a t i o n L i s t . end ( ) ; i ++)
51                 {
52                // B u i l d a p o i n t e r t o t h e c u r r e n t t y p e s o t h a t we can c a l l
53                // t h e get name ( ) member f u n c t i o n .
54                    SgFunctionDeclaration ∗ f u n c t i o n D e c l a r a t i o n = i sS gFu ncti o nDecl a ra ti o n (∗ i ) ;
55                   ROSE ASSERT( f u n c t i o n D e c l a r a t i o n != NULL ) ;
56
57                 SgName func name = f u n c t i o n D e c l a r a t i o n −          >get name ( ) ;
58             // S k i p b u i l t i n f u n c t i o n s f o r s h o r t e r output , L i a o 4 / 2 8 / 2 0 0 8
59                  i f ( func name . g e t S t r i n g ( ) . f i n d ( ”     b u i l t i n ” ,0)==0)
60                      continue ;
61
62                 // o u t p u t t h e f u n c t i o n number and t h e name o f t h e f u n c t i o n
63                    p r i n t f ( ” f u n c t i o n name #%d i s %s a t l i n e %d \n ” ,
64                              c o u n t e r ++,func name . s t r ( ) ,



       Figure 26.1: Example translator (part 1) using database connection to store function names.
     26.3. CLASS HIERARCHY GRAPH                                                                                                              165




 1                            f u n c t i o n D e c l a r a t i o n − g e t f i l e i n f o ()−> g e t l i n e ( ) ) ;
                                                                     >
 2
 3                    s t r i n g functionName = f u n c t i o n D e c l a r a t i o n − e t q u a l i f i e d n a m e ( ) . s t r ( ) ;
                                                                                        >g
 4
 5   // TPS ( 0 1 Dec2008 ) : Enabled mysql and t h i s f a i l s .
 6   // seems l i k e i t i s n o t s u p p o s e d t o be i n c l u d e d
 7   #i f 0
 8                 //# i f d e f HAVE MYSQL
 9                 command = ”INSERT INTO F u n c t i o n s v a l u e s ( \ ” ” + functionName + ” \ ” , ” +
10                                 S t r i n g U t i l i t y : : numberToString ( c o u n t e r ) + ” ) ; ” ;
11              // A l t e r n a t i v e i n t e r f a c e
12                    >s
                // q− e t ( command ) ;
13                                                                  >p
                // c o u t << ” E x e c u t i n g : ” << q− r e v i e w ( ) << ”\n ” ;
14                    >e
                // q− x e c u t e ( ) ;
15                        >e
                   gDB− x e c u t e ( command . c s t r ( ) ) ;
16   #e n d i f
17               }
18
19   // TPS ( 0 1 Dec2008 ) : Enabled mysql and t h i s f a i l s .
20   // seems l i k e i t i s n o t s u p p o s e d t o be i n c l u d e d
21   #i f 0
22          //# i f d e f HAVE MYSQL
23          command = ”SELECT ∗ from F u n c t i o n s ; ” ;
24
25      // A l t e r n a t i v e I n t e r f a c e ( u s i n g q u e r y o b j e c t s )
26      // q << command ;
27            >s
           q− e t ( command ) ;
28                                                          >p
           c o u t << ” E x e c u t i n g : ” << q− r e v i e w ( ) << ”\n ” ;
29
30                                                                                            >s
        // e x e c u t e and r e t u r n r e s u l t ( a l t e r n a t i v e u s a g e : ”gDB− e l e c t ( ) ” )
31         R e s u l t ∗ r e s = q− t o r e ( ) ;
                                        >s
32                      >s
           i f ( q− u c c e s s ( ) != 0 )
33                                                                                  >e
                     c o u t << ” E r r o r r e a d i n g v a l u e s : ” << q− r r o r ( ) << ”\n ” ;
34         else
35           {
36         // Read t h e t a b l e r e t u r n e d from t h e q u e r y
37         // r e s − h o w R e s u l t ( ) ;
                        >s
38               f o r ( R e s u l t : : i t e r a t o r i = r e s − e g i n ( ) ; i != r e s −
                                                                     >b                          >end ( ) ; i++ )
39                     {
40                   // A l t e r n a t i v e s y n t a x i s p o s s i b l e : ”Row r = ∗ i ; ”
41                         s t r i n g functionName = ( ∗ i ) [ 0 ] . g e t s t r i n g ( ) ;
42                         i n t counter = (∗ i ) [ 1 ] ;
43                         p r i n t f ( ” functionName = %s c o u n t e r = %d \n ” , functionName . c s t r ( ) , c o u n t e r ) ;
44                     }
45           }
46
47              gDB−>shutdown ( ) ;
48   #e l s e
49            p r i n t f ( ” Program c o m p i l e d w i t h o u t d a t a b a s e c o n n e c t i o n s u p p o r t ( add u s i n g ROSE c o n f i g u r e o p t i o n ) \n ” ) ;
50   #e n d i f
51
52              return 0;
53        }



       Figure 26.2: Example translator (part 2) using database connection to store function names.
     166                                                                      CHAPTER 26. DATABASE SUPPORT


 1   // T h i s example c o d e i s u s e d t o r e c o r d names o f f u n c t i o n s i n t o t h e d a t a b a s e .
 2
 3   class A
 4      {
 5         public :
 6              virtual int f1 () = 0;
 7              v i r t u a l i n t f 2 ( ) {}
 8              int f3 ( ) ;
 9              virtual int f4 ( ) ;
10      };
11
12   int A: : f3 () { f1 ( ) ;    return f3 ( ) ; }
13   i n t A : : f 4 ( ) {}
14
15   class B : public A
16      {
17         public :
18              virtual int f1 ( ) ;
19              v i r t u a l i n t f 2 ( ) {}
20      };
21
22   i n t B : : f 1 ( ) {}
23
24   class C : public A
25      {
26         public :
27              v i r t u a l i n t f 1 ( ) {}
28              i n t f 3 ( ) {}
29      };
30
31   class D : public B
32      {
33         public :
34              v i r t u a l i n t f 2 ( ) {}
35      };
36
37   class E : public D
38      {
39         public :
40              virtual int f1 () { return 5; }
41      };
42
43   class G : public E
44      {
45         public :
46              virtual int f1 ( ) ;
47      };
48
49   i n t G : : f 1 ( ) {}
50
51   class F : public D
52      {
53         public :
54              v i r t u a l i n t f 1 ( ) {}
55              virtual int f2 () { return 5;}
56              int f3 () { return 2;}
57      };
58
59   class H : public C
60      {
61         public :
62              v i r t u a l i n t f 1 ( ) {}
63              v i r t u a l i n t f 2 ( ) {}
64              i n t f 3 ( ) {}
65      };



                       Figure 26.3: Example source code used as input to database example.
     26.3. CLASS HIERARCHY GRAPH                                                                                                            167




 1   f u n c t i o n name #0 i s       s y n c l o c k t e s t a n d s e t at l i n e 0
 2   f u n c t i o n name #1 i s       s y n c l o c k r e l e a s e at l i n e 0
 3   f u n c t i o n name #2 i s f 1 a t l i n e 6
 4   f u n c t i o n name #3 i s f 2 a t l i n e 7
 5   f u n c t i o n name #4 i s f 3 a t l i n e 8
 6   f u n c t i o n name #5 i s f 4 a t l i n e 9
 7   f u n c t i o n name #6 i s f 3 a t l i n e 12
 8   f u n c t i o n name #7 i s f 4 a t l i n e 13
 9   f u n c t i o n name #8 i s f 1 a t l i n e 18
10   f u n c t i o n name #9 i s f 2 a t l i n e 19
11   f u n c t i o n name #10 i s f 1 a t l i n e 22
12   f u n c t i o n name #11 i s f 1 a t l i n e 27
13   f u n c t i o n name #12 i s f 3 a t l i n e 28
14   f u n c t i o n name #13 i s f 2 a t l i n e 34
15   f u n c t i o n name #14 i s f 1 a t l i n e 40
16   f u n c t i o n name #15 i s f 1 a t l i n e 46
17   f u n c t i o n name #16 i s f 1 a t l i n e 49
18   f u n c t i o n name #17 i s f 1 a t l i n e 54
19   f u n c t i o n name #18 i s f 2 a t l i n e 55
20   f u n c t i o n name #19 i s f 3 a t l i n e 56
21   f u n c t i o n name #20 i s f 1 a t l i n e 62
22   f u n c t i o n name #21 i s f 2 a t l i n e 63
23   f u n c t i o n name #22 i s f 3 a t l i n e 64
24   Program c o m p i l e d w i t h o u t d a t a b a s e c o n n e c t i o n s u p p o r t ( add u s i n g ROSE c o n f i g u r e o p t i o n )



     Figure 26.4: Output from processing input code through database example dataBaseTransla-
     tor26.1.
168   CHAPTER 26. DATABASE SUPPORT
Chapter 27

Building Custom Graphs

What To Learn From This Example This example shows how to generate custom graphs
using SgGraph class.
   Rose provides a collection type SgGraph to store a graph. Two specific graphs are also
provided which are derived from SgGraph: SgIncidenceDirectedGraph and SgIncidenceUndirect-
edGraph.
   Nodes and edges in a SgGraph are represented by SgGraphNode and SgGraphEdge separately.
A SgGraph is built by adding SgGraphNodes and SgGraphEdges using its member function
addNode and addEdge. You can get all nodes and edges of a SgGraph by calling its functions
computeNodeSet and computeEdgeSet separately. More interfaces of SgGraph and its subclasses
can be found in doxygen of Rose.
   Since SgGraph is for Rose use, each node in it holds a pointer to SgNode, which is the
default attribute of a SgGraphNode. If you want to add more attributes inside, you can use
SgGraphNode’s member function addNewAttribute by providing a name and an AstAttribute
object to add a new attribute to a node. Normally, you have to build your own attribute class
which should be derived from class AstAttribute. Three attribute classes are provided by Rose:
AstRegExAttribute, AstTextAttribute, and MetricAttribute. For more information about them,
please refer to Rose’s doxygen.




                                             169
170   CHAPTER 27. BUILDING CUSTOM GRAPHS
                              Part IV

  Program Transformations and
         Optimizations


This part gives examples of building source-to-source program transformations
                              and optimizations.




                                     171
Chapter 28

Generating Unique Names for
Declarations

There are many instances where a unique name must be generated for either a function or vari-
able declaration. ROSE defines a mechanism to make the generation of unique names from all
SgDeclarationStatment IR nodes and the SgInitializedName IR node. This simplifies ROSE-
based applications that require this sort of mechanism. Our experience has found that a signif-
icant number of tools require such a mechanism and that its correct implementation can have
subtle points.
    The specific translator described in this chapter traverses an AST and outputs the unique
names that can be generated for each declaration showing the use of the unique name generation
mechanism. This tool is intended as an example of how to generate unique names using ROSE.
Not all IR nodes can be used to generate a unique name. The generated names are unique under
the following rules:
  1. Any two generated names are the same if the declarations are the same.
     Declaration can be the same across files or within the same file. Declarations that are the
     same can have different location in the same file (be represented multiple times) or be in
     different files. Language constructs that are the same must follow the One-time Definition
     Rule (ODR) across files.
  2. Declarations in different unnamed scopes (e.g. for loop bodies) will generate different
     names.
  3. Names are the same when generated by different ROSE tools.
     Pointer values could be used to generate unique names of all IR nodes, but this would work
     only within a single invocation of the ROSE based tool. Generated names are not based
     on internal pointer values and are thus insensitive to pointer values. Generated names of
     the same declaration are thus the same even if generated from different tools. This allows
     multiple ROSE tools to inter-operate.
   This unique name generation mechanism is only applicable to specific IR nodes, specifically:
   • SgInitializedName

                                             173
174                   CHAPTER 28. GENERATING UNIQUE NAMES FOR DECLARATIONS

      • SgDeclarationStatement IR nodes:
          – Obvious IR nodes supported:
              ∗ SgClassDeclaration
              ∗ SgFunctionDeclaration
              ∗ SgEnumDeclaration
              ∗ SgNamespaceDeclarationStatement
              ∗ SgTypedefDeclaration
          – Less obvious IR nodes not supported (support for these would not make sense):
              ∗ SgAsmStmt
              ∗ SgCtorInitializerList
              ∗ SgFunctionParameterList
              ∗ SgNamespaceAliasDeclarationStatement
              ∗ SgPragmaDeclaration
              ∗ SgTemplateDeclaration (can this have a mangled name?)
              ∗ SgTemplateInstantiationDirectiveStatement
              ∗ SgUsingDeclarationStatement
              ∗ SgUsingDirectiveStatement
              ∗ SgVariableDeclaration
                Note that the SgVariableDeclaration contains a list of SgInitializedName nodes
                and the mangled names are best queried from each SgInitializedName instead of
                the SgVariableDeclaration.
              ∗ SgVariableDefinition
      • Un-named scopes
        A number of scopes are un-names and so there is an opportunity to generate non-unique
        names from declarations in such scopes. To fix this we generate names for each un-named
        scope to guarantee uniqueness. Nodes handled are:
          –   SgForStatement
          –   SgBasicBlock
          –   SgIfStmt
          –   get the complete list ...
Other language constructs can generate unique names as well, but their name could be invalid
after certain transformation that move it structurally within the generated source code.


28.1          Example Code Showing Generation of Unique Names
28.2          Input For Examples Showing Unique Name Genera-
              tion for Variables
Figure 28.1, shows an example translator demonstrating the generation of unique names from
declarations in the AST. For each SgInitializedName we generate the mangled name. Figure 28.2
28.3. EXAMPLE OUTPUT SHOWING UNIQUE VARIABLE NAMES                                         175

shows the input code and figure 28.3 shows the generated output from the translator (the mangled
names from the AST associated with the input application).


28.3      Example Output Showing Unique Variable Names
28.4      Input For Examples Showing Unique Name Genera-
          tion for Functions
Figure 28.1, shows an example translator demonstrating the generation of unique names from
declarations in the AST. For each SgInitializedName we generate the mangled name. Figure 28.4
shows the input code and figure 28.5 shows the generated output from the translator (the mangled
names from the AST associated with the input application).


28.5      Example Output Showing Unique Function Names
     176                           CHAPTER 28. GENERATING UNIQUE NAMES FOR DECLARATIONS


 1   // T h i s example shows t h e g e n e r a t i o n o f u n i q u e names from d e c l a r a t i o n s .
 2
 3   //   Mangled name demo
 4   //
 5   //   T h i s t r a n s l a t o r q u e r i e s t h e AST f o r a l l S g I n i t i a l i z e d N a m e s and
 6   //   S g F u n c t i o n D e c l a r a t i o n s , and f o r e a c h one p r i n t s ( a ) t h e s o u r c e
 7   //   l o c a t i o n , ( b ) t h e s o u r c e name o f t h e o b j e c t , and ( c ) t h e mangled
 8   //   name .
 9
10   #i n c l u d e <r o s e . h>
11
12   u s i n g namespace s t d ;
13
14   // R e t u r n s a S g F i l e I n f o o b j e c t a s a d i s p l a y −f r i e n d l y           string ,      ”[ source : l i n e ] ” .
15   s t a t i c s t r i n g toString ( const S g F i l e I n f o ∗ info )
16        {
17             ostringstream i n f o s t r ;
18             i f ( info )
19             i n f o s t r << ’ [ ’
20                           << i n f o − e t r a w f i l e n a m e ( )
                                         >g
21                           << ” : ” << i n f o − e t r a w l i n e ( )
                                                  >g
22                           << ’ ] ’ ;
23             return i n f o s t r . str ( ) ;
24        }
25
26   // D i s p l a y s l o c a t i o n and mangled name o f an S g I n i t i a l i z e d N a m e o b j e c t .
27   s t a t i c v o i d p r i n t I n i t i a l i z e d N a m e ( c o n s t SgNode∗ node )
28        {
29             c o n s t S g I n i t i a l i z e d N a m e ∗ name = i s S g I n i t i a l i z e d N a m e ( node ) ;
30             ROSE ASSERT ( name != NULL ) ;
31
32            if             >
                   ( name− g e t f i l e i n f o ()−> i s C o m p i l e r G e n e r a t e d ( ) == f a l s e )
33                                                           >
                      c o u t // << t o S t r i n g ( name− g e t f i l e i n f o ( ) )
34                            // << ” ”
35                            << name−     >get name ( ) . s t r ( )
36                            << ” −−> ” << name− e t m a n g l e d n a m e ( ) . s t r ( )
                                                          >g
37                            << e n d l ;
38        }
39
40   // D i s p l a y s l o c a t i o n and mangled name o f an S g F u n c t i o n D e c l a r a t i o n o b j e c t .
41   s t a t i c v o i d p r i n t F u n c t i o n D e c l a r a t i o n ( c o n s t SgNode∗ node )
42        {
43             c o n s t S g F u n c t i o n D e c l a r a t i o n ∗ d e c l = i s S g F u n c t i o n D e c l a r a t i o n ( node ) ;
44             ROSE ASSERT ( d e c l != NULL ) ;
45
46            if   ( d e c l − g e t f i l e i n f o ()−> i s C o m p i l e r G e n e r a t e d ( ) == f a l s e )
                              >
47                    c o u t // << t o S t r i n g ( d e c l − e t s t a r t O f C o n s t r u c t ( ) )
                                                                 >g
48                             // << ” ”
49                             << d e c l − e t q u a l i f i e d n a m e ( ) . s t r ( )
                                            >g
50                             << ” −−> ” << d e c l − e t m a n g l e d n a m e ( ) . s t r ( )
                                                              >g
51                             << e n d l ;
52        }
53
54   i n t main ( i n t a r g c , c h a r ∗∗ a r g v )
55        {
56          SgProject ∗ p r o j = frontend ( argc , argv ) ;
57
58            c o u t << e n d l << ”∗∗∗∗∗ BEGIN i n i t i a l i z e d names ∗∗∗∗∗” << e n d l ;
59            R o s e S T L C o n t a i n e r<SgNode ∗> i n i t n a m e s = NodeQuery : : querySubTree ( p r o j , V S g I n i t i a l i z e d N a m e ) ;
60            f o r e a c h ( i n i t n a m e s . b e g i n ( ) , i n i t n a m e s . end ( ) , p r i n t I n i t i a l i z e d N a m e ) ;
61            c o u t << ”∗∗∗∗∗ END i n i t i a l i z e d names ∗∗∗∗∗” << e n d l ;
62
63            c o u t << e n d l << ”∗∗∗∗∗ BEGIN f u n c t i o n d e c l a r a t i o n s ∗∗∗∗∗” << e n d l ;
64            R o s e S T L C o n t a i n e r<SgNode ∗> f u n c d e c l s = NodeQuery : : querySubTree ( p r o j , V S g F u n c t i o n D e c l a r a t i o n ) ;
65            f o r e a c h ( f u n c d e c l s . b e g i n ( ) , f u n c d e c l s . end ( ) , p r i n t F u n c t i o n D e c l a r a t i o n ) ;
66            c o u t << ”∗∗∗∗∗ END f u n c t i o n d e c l a r a t i o n s ∗∗∗∗∗” << e n d l ;
67
68            r e t u r n backend ( p r o j ) ;
69        }



     Figure 28.1: Example source code showing the output of mangled name. The string represents
     the code associated with the subtree of the target IR node.
     28.5. EXAMPLE OUTPUT SHOWING UNIQUE FUNCTION NAMES                                                  177


 1   // I n p u t   file     to t e s t mangling o f S g I n i t i a l i z e d N a m e o b j e c t s .
 2
 3   int x ;
 4
 5   // G l o b a l c l a s s
 6   class A
 7      {
 8          private :
 9                  int x ;
10             // N e s t e d c l a s s
11                  class B
12                        {
13                            private :
14                                  int x ;
15                            public :
16                                  void foo ( i n t x arg ) { i n t x ; }
17                        };
18      };
19
20   t e m p l a t e <typename T>
21   void
22   f o o (T x a r g )
23        {
24            T x;
25             f o r ( x = 0 ; x < 1 0 ; x++)
26                  {
27                    T x = 0;
28                     do {
29                       // L o c a l c l a s s
30                           class A
31                               {
32                                   private :
33                                     // N e s t e d c l a s s
34                                            class B
35                                                  {
36                                                     T x;
37                                                  };
38                                   public :
39                                            v o i d f o o (T x ) {}
40                               };
41                          T x = 0;
42                        }
43                     while (x > 0 ) ;
44
45                    do {
46                         T x = 0;
47                       }
48                    while (x > 0 ) ;
49
50              // N e s t e d s c o p e
51                      {
52                           T x = 0;
53                      }
54               }
55       }
56
57   t e m p l a t e v o i d f o o <i n t > ( i n t x ) ;
58   t e m p l a t e v o i d f o o <d o u b l e> ( d o u b l e x ) ;
59
60   void bar ( void )
61      {
62        f o r ( i n t x = 0 ; x != 0 ; x++)
63                 f o r ( i n t x = 0 ; x != 0 ; x++)
64                          f o r ( l o n g x = 0 ; x != 0 ; x++)
65                                  ;
66        try {
67            f o r ( i n t x = 0 ; x != 0 ; x++) ;
68        }
69        c a t c h ( i n t ) {}
70        c a t c h ( c h a r x ) {}
71      }



     Figure 28.2: Example source code used as input to program in codes showing debugging tech-
     niques shown in this section.
     178                         CHAPTER 28. GENERATING UNIQUE NAMES FOR DECLARATIONS




 1
 2   ∗∗∗∗∗ BEGIN i n i t i a l i z e d names ∗∗∗∗∗
 3      −
     x − > x
 4      −
     x − > A    scope      x
 5      −
     x − > A    scope      B      scope  x
 6            −
     x a r g − > L2R L3R ARG1
 7      −
     x − > L2R     L3R       scope       SgSS2     scope  x
 8            −
     x arg − > foo       tas      i  tae      Fb v Gb i Fe     ARG1
 9            −
     x arg − > foo       tas      i  tae      Fb v Gb i Fe     ARG1
10            −
     x arg − > foo       tas      d  tae      Fb v Gb d Fe     ARG1
11            −
     x arg − > foo       tas      d  tae      Fb v Gb d Fe     ARG1
12      −
     x − > bar     Fb v Gb           Fe   L4R   scope    SgSS2      scope    SgSS3     scope   x
13      −
     x − > bar     Fb v Gb           Fe   L4R    scope    SgSS2     scope    SgSS3      scope      SgSS4     scope   x
14      −
     x − > bar     Fb v Gb           Fe   L4R    scope    SgSS2     scope    SgSS3      scope      SgSS4     scope       SgSS5    scope   x
15      −
     x − > bar     Fb v Gb           Fe   L4R    scope    SgSS2     scope    SgSS6      scope      SgSS7     scope   x
16     −
      − > bar     Fb v Gb Fe            L4R scope      SgSS2     scope    SgSS8    scope CATCHARG
17      −
     x − > bar     Fb v Gb           Fe   L4R   scope    SgSS2      scope    SgSS10      scope   x
18            −
     x arg − > foo       tas      i  tae      Fb v Gb i Fe     ARG1
19      −
     x − > foo    tas     i     tae      Fb v Gb i Fe      scope     SgSS2     scope   x
20      −
     x − > foo    tas     i     tae      Fb v Gb i Fe      scope     SgSS2     scope     SgSS3       scope    SgSS4       scope   x
21      −
     x − > L0R     scope        B   scope   x
22      −
     x − > L5R L6R ARG1
23      −
     x − > foo    tas     i     tae      Fb v Gb i Fe      scope     SgSS2     scope     SgSS3      scope     SgSS4       scope       SgSS5    scope    SgSS6
24      −
     x − > foo    tas     i     tae      Fb v Gb i Fe      scope     SgSS2    scope      SgSS3      scope     SgSS4       scope       SgSS11    scope    SgSS1
25      −
     x − > foo    tas     i     tae      Fb v Gb i Fe      scope     SgSS2     scope     SgSS3      scope     SgSS4       scope       SgSS13    scope   x
26            −
     x arg − > foo       tas      d  tae      Fb v Gb d Fe     ARG1
27      −
     x − > foo    tas     d      tae      Fb v Gb d Fe      scope    SgSS2      scope  x
28      −
     x − > foo    tas     d      tae      Fb v Gb d Fe      scope     SgSS2     scope     SgSS3      scope    SgSS4       scope   x
29      −
     x − > L1R     scope        B   scope   x
30      −
     x − > L7R L8R ARG1
31      −
     x − > foo    tas     d      tae      Fb v Gb d Fe      scope     SgSS2     scope     SgSS3      scope    SgSS4       scope       SgSS5    scope    SgSS6
32      −
     x − > foo    tas     d      tae      Fb v Gb d Fe      scope    SgSS2      scope     SgSS3      scope    SgSS4       scope       SgSS11    scope    SgSS1
33      −
     x − > foo    tas     d      tae      Fb v Gb d Fe      scope    SgSS2      scope     SgSS3      scope    SgSS4       scope       SgSS13    scope   x
34   ∗∗∗∗∗ END i n i t i a l i z e d names ∗∗∗∗∗
35
36   ∗∗∗∗∗ BEGIN f u n c t i o n d e c l a r a t i o n s ∗∗∗∗∗
37                          −
     : : A : : B : : f o o − > L2R L3R
38   : : foo < int > − > f o o −         tas       i   tae     Fb   v Gb i Fe
39   : : foo < int > − > f o o −         tas       i   tae     Fb   v Gb i Fe
40   : : foo < double > − > f o o −           tas      d   tae      Fb v Gb d Fe
41   : : foo < double > − > f o o −           tas      d   tae      Fb v Gb d Fe
42                −
     : : bar − > b a r        Fb v Gb Fe            L4R
43   : : foo < int > − > f o o −         tas       i   tae     Fb   v Gb i Fe
44                  −
     A : : f o o − > L5R L6R
45   : : foo < double > − > f o o −           tas      d   tae      Fb v Gb d Fe
46                  −
     A : : f o o − > L7R L8R
47   ∗∗∗∗∗ END f u n c t i o n d e c l a r a t i o n s ∗∗∗∗∗




             Figure 28.3: Output of input code using generatingUniqueNamesFromDeclaration.C
     28.5. EXAMPLE OUTPUT SHOWING UNIQUE FUNCTION NAMES                                                                        179


 1   // I n p u t       file   to t e s t mangling o f S g F u n c t i o n D e c l a r a t i o n o b j e c t s .
 2
 3
 4   long    foobar ( ) ;
 5   long    foobar ( int        );
 6   long    foobar ( int         y);
 7   long    foobar ( int         x);
 8   long    foobar ( int         x = 0);
 9   long    foobar ( int         xyz )
10      {
11           r e t u r n xyz ;
12       }
13
14   char foobarChar ( char ) ;
15   char foobarChar ( char c ) ;
16
17   // I n p u t       file   to t e s t mangling o f S g F u n c t i o n D e c l a r a t i o n o b j e c t s .
18
19   typedef int value0 t ;
20   typedef value0 t value t ;
21   namespace N
22      {
23        typedef struct { int a ; } s t ;
24        c l a s s A { p u b l i c : A ( ) {} v i r t u a l v o i d f o o ( i n t ) {} } ;
25        c l a s s B { p u b l i c : B ( ) {} v o i d f o o ( v a l u e t ) c o n s t {} } ;
26        c l a s s C : p u b l i c A { p u b l i c : C ( ) {} v o i d f o o ( i n t ) {} v o i d f o o ( c o n s t   s t &) {} } ;
27        v o i d f o o ( c o n s t s t ∗ ) {}
28      }
29
30   typedef N: : s t s2 t ;
31   void foo ( v a l u e t ) ;
32   v o i d f o o ( s 2 t ) {}
33   v o i d f o o ( f l o a t x [ ] ) {}
34   void foo ( value t , s 2 t ) ;
35
36   t e m p l a t e <typename T>
37   v o i d f o o (T) {}
38
39   namespace P
40      {
41        typedef long double t y p e t ;
42        namespace Q
43           {
44             t e m p l a t e <typename T>
45             v o i d f o o (T) {}
46
47                       class R
48                          {
49                             public :
50                                  R ( ) {}
51                                  t e m p l a t e <typename T>
52                                  v o i d f o o (T) {}
53                                  v o i d f o o (P : : t y p e t ) {}
54                                  t e m p l a t e <typename T, i n t x>
55                                  i n t f o o (T) { r e t u r n x ; }
56                          };
57                  }
58       }
59
60   t e m p l a t e <typename T, i n t x>
61   i n t f o o (T) { r e t u r n x ; }
62
63   template           void   f o o <char> ( c h a r ) ;
64   template           void   f o o <c o n s t v a l u e t ∗> ( c o n s t v a l u e t    ∗);
65   template           void   P : : Q : : f o o <l o n g > ( l o n g ) ;
66   template           void   P : : Q : : R : : f o o <v a l u e t > ( v a l u e t ) ;



     Figure 28.4: Example source code used as input to program in codes showing debugging tech-
     niques shown in this section.
     180                       CHAPTER 28. GENERATING UNIQUE NAMES FOR DECLARATIONS




 1
 2   ∗∗∗∗∗ BEGIN i n i t i a l i z e d names ∗∗∗∗∗
 3     −
      − > foobar       Fb l Gb i Fe        L4R ARG1
 4      −
     y − > foobar        Fb l Gb i Fe       L4R ARG1
 5      −
     x − > foobar        Fb l Gb i Fe       L4R ARG1
 6      −
     x − > foobar        Fb l Gb i Fe       L4R ARG1
 7        −
     xyz − > f o o b a r    Fb l Gb i Fe      L4R ARG1
 8     −
      − > foobarChar          Fb c Gb c Fe     L5R ARG1
 9      −
     c − > foobarChar           Fb c Gb c Fe    L5R ARG1
10      −
     a − > L2R scope a
11     −
      − > L6R L7R ARG1
12     −
      − > L8R L9R ARG1
13     −
      − > L10R L11R ARG1
14     −
      − > L12R L13R ARG1
15     −
      − > L14R L15R ARG1
16     −
      − > foo    Fb v Gb L0R Fe           L16R ARG1
17     −
      − > foo    Fb v Gb L3R Fe           L17R ARG1
18      −
     x − > L18R L19R ARG1
19     −
      − > foo    Fb v Gb L0R sep L3R Fe            L20R ARG1
20     −
      − > foo    Fb v Gb L0R sep L3R Fe            L20R ARG2
21     −
      − > L21R L22R ARG1
22     −
      − > foo   tas      c   tae       Fb v Gb c Fe   ARG1
23     −
      − > foo   tas      c   tae       Fb v Gb c Fe   ARG1
24     −
      − > L23R ARG1
25     −
      − > L23R ARG1
26     −
      − > L24R ARG1
27     −
      − > L24R ARG1
28     −
      − > L25R ARG1
29     −
      − > L25R ARG1
30     −
      − > foo   tas      c   tae       Fb v Gb c Fe   ARG1
31     −
      − > L23R ARG1
32     −
      − > L24R ARG1
33     −
      − > L25R ARG1
34   ∗∗∗∗∗ END i n i t i a l i z e d names ∗∗∗∗∗
35
36   ∗∗∗∗∗ BEGIN f u n c t i o n d e c l a r a t i o n s ∗∗∗∗∗
37                      −
     : : foobar − > foobar                 Fb l Gb     Fe   L26R
38                      −
     : : foobar − > foobar                  Fb l Gb i Fe     L4R
39                      −
     : : foobar − > foobar                  Fb l Gb i Fe     L4R
40                      −
     : : foobar − > foobar                  Fb l Gb i Fe     L4R
41                      −
     : : foobar − > foobar                  Fb l Gb i Fe     L4R
42                      −
     : : foobar − > foobar                  Fb l Gb i Fe     L4R
43                              −
     : : foobarChar − > foobarChar                   Fb c Gb c Fe    L5R
44                              −
     : : foobarChar − > foobarChar                   Fb c Gb c Fe    L5R
45                        −
     : : N : : A : : A − > L27R L28R
46                            −
     : : N : : A : : f o o − > L6R L7R
47                        −
     : : N : : B : : B − > L29R L30R
48                            −
     : : N : : B : : f o o − > L8R L9R
49                        −
     : : N : : C : : C − > L31R L32R
50                            −
     : : N : : C : : f o o − > L10R L11R
51                            −
     : : N : : C : : f o o − > L12R L13R
52                      −
     : : N : : f o o − > L14R L15R
53                −
     : : foo − > foo              Fb v Gb L0R Fe         L16R
54                −
     : : foo − > foo              Fb v Gb L3R Fe         L17R
55                −
     : : f o o − > L18R L19R
56                −
     : : foo − > foo              Fb v Gb L0R sep L3R Fe           L20R
57                              −
     : : P : : Q : : R : : R − > L33R L34R
58                                −
     : : P : : Q : : R : : f o o − > L21R L22R
59   : : foo < char > − > f o o    −           tas   c   tae     Fb v Gb c Fe
60   : : foo < char > − > f o o    −           tas   c   tae     Fb v Gb c Fe
61   : : f o o < c o n s t v a l u e t ∗ > − > L23R−
62   : : f o o < c o n s t v a l u e t ∗ > − > L23R−
63                                           −
     : : P : : Q : : f o o < l o n g > − > L24R
64                                           −
     : : P : : Q : : f o o < l o n g > − > L24R
65                                                  −
     : : P : : Q : : R : : f o o < v a l u e t > − > L25R
66                                                  −
     : : P : : Q : : R : : f o o < v a l u e t > − > L25R
67   : : foo < char > − > f o o    −           tas   c   tae     Fb v Gb c Fe
68   : : f o o < c o n s t v a l u e t ∗ > − > L23R−
69                                           −
     : : P : : Q : : f o o < l o n g > − > L24R
70                                                  −
     : : P : : Q : : R : : f o o < v a l u e t > − > L25R
71   ∗∗∗∗∗ END f u n c t i o n d e c l a r a t i o n s ∗∗∗∗∗




             Figure 28.5: Output of input code using generatingUniqueNamesFromDeclaration.C
Chapter 29

Command-line Processing Within
Translators

ROSE includes mechanism to simplify the processing of command-line arguments so that trans-
lators using ROSE can trivially replace compilers within makefiles. This example shows some
of the many command-line handling options within ROSE and the ways in which customized
options may be added for specific translators.


29.1      Commandline Selection of Files
Overview This example shows the optional processing of specific files selected after the call
to the frontend to build the project. First the SgProject if build and then the files are selected
for processing via ROSE or the backend compiler directly.
    This example demonstrates the separation of the construction of a SgProject with valid SgFile
objects for each file on the command line, but with an empty SgGlobal scope, and the call to
the frontend, called for each SgFile in a separate loop over all the SgFile objects.




                                              181
     182                       CHAPTER 29. COMMAND-LINE PROCESSING WITHIN TRANSLATORS




 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an example p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   // r o s e . C : Example ( d e f a u l t ) ROSE P r e p r o c e s s o r : u s e d f o r t e s t i n g ROSE i n f r a s t r u c t u r e
 3
 4   #i n c l u d e ” r o s e . h”
 5
 6   u s i n g namespace s t d ;
 7
 8   int
 9   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
10       {
11         R o s e S T L C o n t a i n e r<s t r i n g > l = C o m m a n d l i n e P r o c e s s i n g : : g e n e r a t e A r g L i s t F r o m A r g c A r g v ( a r g c , a r g v ) ;
12         p r i n t f ( ” P r e p r o c e s s o r ( b e f o r e ) : a r g v = \n%s \n ” , S t r i n g U t i l i t y : : l i s t T o S t r i n g ( l ) . c s t r ( ) ) ;
13
14      // Remove c e r t a i n s o r t s o f o p t i o n s from t h e command l i n e
15         C o m m a n d l i n e P r o c e s s i n g : : removeArgs ( l ,” − edg : ” ) ;
16         C o m m a n d l i n e P r o c e s s i n g : : removeArgs ( l ,”−−edg : ” ) ;
17         C o m m a n d l i n e P r o c e s s i n g : : removeArgsWithParameters ( l ,” − e d g p a r a m e t e r : ” ) ;
18         C o m m a n d l i n e P r o c e s s i n g : : removeArgsWithParameters ( l ,”−− e d g p a r a m e t e r : ” ) ;
19
20      // Add a t e s t f o r a custom command l i n e o p t i o n
21         int integerOptionForVerbose = 0;
22         i f ( C o m m a n d l i n e P r o c e s s i n g : : i s O p t i o n W i t h P a r a m e t e r ( l ,” − m y T r a n s l a t o r : ” , ” ( v | v e r b o s e ) ” , i n t e g e r O p t i o n F o r V e r b o s
23             {
24               p r i n t f ( ” Turning on my t r a n s l a t o r ’ s v e r b o s e mode ( s e t t o %d ) \n ” , i n t e g e r O p t i o n F o r V e r b o s e ) ;
25             }
26
27      // Adding a new command l i n e p a r a m e t e r ( f o r mechanisms i n ROSE t h a t t a k e command l i n e s )
28
29            // p r i n t f ( ” a r g c = %zu \n ” , l . s i z e ( ) ) ;
30            // l = C o m m a n d l i n e P r o c e s s i n g : : g e n e r a t e A r g L i s t F r o m A r g c A r g v ( a r g c , a r g v ) ;
31            p r i n t f ( ” l . s i z e ( ) = %zu \n ” , l . s i z e ( ) ) ;
32            p r i n t f ( ” P r e p r o c e s s o r ( a f t e r ) : a r g v = \n%s \n ” , S t r i n g U t i l i t y : : l i s t T o S t r i n g ( l ) . c s t r ( ) ) ;
33
34      //   SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
35      //   ROSE ASSERT ( p r o j e c t != NULL ) ;
36      //   G e n e r a t e t h e s o u r c e c o d e and c o m p i l e u s i n g t h e vendor ’ s c o m p i l e r
37      //   r e t u r n backend ( p r o j e c t ) ;
38
39      // B u i l d t h e AST,           g e n e r a t e t h e s o u r c e c o d e and c a l l             t h e backend c o m p i l e r           ...
40         frontend ( l ) ;
41         return 0;
42       }



     Figure 29.1: Example source code showing simple command-line processing within ROSE trans-
     lator.




 1   P r e p r o c e s s o r ( b e f o r e ) : argv =
 2                                                                              −d                                                        −b
     / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x /ROSE u i l d / t u t o r i a l / . l i b s / l t
 3   Turning on my t r a n s l a t o r ’ s v e r b o s e mode ( s e t t o 4 2 )
 4   l . size () = 4
 5   P r e p r o c e s s o r ( a f t e r ) : argv =
 6                                                                              −d                                                        −b
     / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x /ROSE u i l d / t u t o r i a l / . l i b s / l t



                            Figure 29.2: Output of input code using commandlineProcessing.C
     29.1. COMMANDLINE SELECTION OF FILES                                                                                                                           183




 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an example p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   // r o s e . C : Example ( d e f a u l t ) ROSE P r e p r o c e s s o r : u s e d f o r t e s t i n g ROSE i n f r a s t r u c t u r e
 3
 4   #i n c l u d e ” r o s e . h”
 5
 6   u s i n g namespace s t d ;
 7
 8   int
 9   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
10       {
11         R o s e S T L C o n t a i n e r<s t r i n g > l = C o m m a n d l i n e P r o c e s s i n g : : g e n e r a t e A r g L i s t F r o m A r g c A r g v ( a r g c , a r g v ) ;
12         p r i n t f ( ” P r e p r o c e s s o r ( b e f o r e ) : a r g v = \n%s \n ” , S t r i n g U t i l i t y : : l i s t T o S t r i n g ( l ) . c s t r ( ) ) ;
13
14      // Remove c e r t a i n s o r t s o f o p t i o n s from t h e command l i n e
15         C o m m a n d l i n e P r o c e s s i n g : : removeArgs ( l ,” − edg : ” ) ;
16         C o m m a n d l i n e P r o c e s s i n g : : removeArgs ( l ,”−−edg : ” ) ;
17         C o m m a n d l i n e P r o c e s s i n g : : removeArgsWithParameters ( l ,” − e d g p a r a m e t e r : ” ) ;
18         C o m m a n d l i n e P r o c e s s i n g : : removeArgsWithParameters ( l ,”−− e d g p a r a m e t e r : ” ) ;
19
20      // Add a t e s t f o r a custom command l i n e o p t i o n
21         int integerOptionForVerbose = 0;
22         i f ( C o m m a n d l i n e P r o c e s s i n g : : i s O p t i o n W i t h P a r a m e t e r ( l ,” − m y T r a n s l a t o r : ” , ” ( v | v e r b o s e ) ” , i n t e g e r O p t i o n F o r V e r b o s e , t r u e ) )
23             {
24               p r i n t f ( ” Turning on my t r a n s l a t o r ’ s v e r b o s e mode ( s e t t o %d ) \n ” , i n t e g e r O p t i o n F o r V e r b o s e ) ;
25             }
26
27      // Adding a new command l i n e p a r a m e t e r ( f o r mechanisms i n ROSE t h a t t a k e command l i n e s )
28
29            // p r i n t f ( ” a r g c = %zu \n ” , l . s i z e ( ) ) ;
30            // l = C o m m a n d l i n e P r o c e s s i n g : : g e n e r a t e A r g L i s t F r o m A r g c A r g v ( a r g c , a r g v ) ;
31            p r i n t f ( ” l . s i z e ( ) = %zu \n ” , l . s i z e ( ) ) ;
32            p r i n t f ( ” P r e p r o c e s s o r ( a f t e r ) : a r g v = \n%s \n ” , S t r i n g U t i l i t y : : l i s t T o S t r i n g ( l ) . c s t r ( ) ) ;
33
34      //   SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
35      //   ROSE ASSERT ( p r o j e c t != NULL ) ;
36      //   G e n e r a t e t h e s o u r c e c o d e and c o m p i l e u s i n g t h e vendor ’ s c o m p i l e r
37      //   r e t u r n backend ( p r o j e c t ) ;
38
39      // B u i l d t h e AST,           g e n e r a t e t h e s o u r c e c o d e and c a l l            t h e backend c o m p i l e r            ...
40         frontend ( l ) ;
41         return 0;
42       }



     Figure 29.3: Example source code showing simple command-line processing within ROSE trans-
     lator.




 1   P r e p r o c e s s o r ( b e f o r e ) : argv =
 2                                                                              −d                                                        −b
     / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x /ROSE u i l d / t u t o r i a l / . l i b s / l t −c o m m a n d l i n
 3   Turning on my t r a n s l a t o r ’ s v e r b o s e mode ( s e t t o 4 2 )
 4   l . size () = 4
 5   P r e p r o c e s s o r ( a f t e r ) : argv =
 6                                                                              −d                                                        −b
     / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x /ROSE u i l d / t u t o r i a l / . l i b s / l t −c o m m a n d l i n



                            Figure 29.4: Output of input code using commandlineProcessing.C
184   CHAPTER 29. COMMAND-LINE PROCESSING WITHIN TRANSLATORS
Chapter 30

Tailoring The Code Generation
Format

Figure 30.1 shows an example of how to use the mechanisms in ROSE to tailor the format
and style of the generated code. This chapter presents an example translator that modifies the
formatting of the code that is generated within ROSE.
    The details of functionality are hidden from the user and a high level interface is provided
that permits key parameters to be specified. This example will be made more sophisticated
later, for now it just modifies the indentation of nested code blocks (from 2 spaces/block to 5
spaces/block).


30.1      Source Code for Example that Tailors the Code Gen-
          eration
Figure 30.1 shows an example translator which calls the inliner mechanism. The code is designed
to only inline up to ten functions. the list of function calls is recomputed after any function call
is successfully inlined.
    The input code is shown in figure 30.2, the output of this code is shown in figure 30.3.


30.2      Input to Demonstrate Tailoring the Code Generation
Figure 30.2 shows the example input used for demonstration of how to control the formatting of
generated code.


30.3      Final Code After Tailoring the Code Generation
Figure 30.3 shows the results from changes to the formatting of generated code.




                                                185
     186                                      CHAPTER 30. TAILORING THE CODE GENERATION FORMAT


 1   // T h i s example w i l l be made more s o p h i s t i c a t e d l a t e r , f o r now i t j u s t
 2   // m o d i f i e s t h e i n d e n t a t i o n o f n e s t e d c o d e b l o c k s ( from 2 s p a c e s / b l o c k
 3   // t o 5 s p a c e s / b l o c k ) .
 4
 5   #i n c l u d e ” r o s e . h”
 6   #i n c l u d e ” unparseFormatHelp . h”
 7
 8   c l a s s CustomCodeFormat : p u b l i c UnparseFormatHelp
 9         {
10            public :
11                  CustomCodeFormat ( ) ;
12                ˜ CustomCodeFormat ( ) ;
13
14                       virtual       i n t g e t L i n e ( SgLocatedNode ∗ , S g U n p a r s e I n f o& i n f o , FormatOpt o p t ) ;
15                       virtual       i n t g e t C o l ( SgLocatedNode ∗ , S g U n p a r s e I n f o& i n f o , FormatOpt o p t ) ;
16
17                 // r e t u r n t h e v a l u e f o r i n d e n t a t i o n o f c o d e ( p a r t o f c o n t r o l o v e r s t y l e )
18                    v i r t u a l i n t tabIndent ( ) ;
19
20                 // r e t u r n t h e v a l u e f o r where l i n e wrapping s t a r t s ( p a r t o f c o n t r o l o v e r s t y l e )
21                    v i r t u a l i n t maxLineLength ( ) ;
22
23              private :
24                   int defaultLineLength ;
25                   int defaultIndentation ;
26         };
27
28
29   CustomCodeFormat : : CustomCodeFormat ( )
30      {
31     // d e f a u l t v a l u e s h e r e !
32        defaultLineLength = 20;
33        defaultIndentation = 5;
34      }
35
36   CustomCodeFormat : : ˜ CustomCodeFormat ( )
37      {}
38
39   // r e t u r n : > 0 : s t a r t new l i n e s ; == 0 : u s e same l i n e ; < 0 : d e f a u l t
40   int
41   CustomCodeFormat : : g e t L i n e ( SgLocatedNode ∗ , S g U n p a r s e I n f o& i n f o , FormatOpt o p t )
42       {
43     // Use d e f a u l t mechanism t o s e l e c t t h e l i n e where t o o u t p u t g e n e r a t e d c o d e
44          r e t u r n −1;
45       }
46
47   // r e t u r n s t a r t i n g column . i f < 0 , u s e d e f a u l t
48   int
49   CustomCodeFormat : : g e t C o l ( SgLocatedNode ∗ , S g U n p a r s e I n f o& i n f o , FormatOpt o p t )
50       {
51     // Use d e f a u l t mechanism t o s e l e c t t h e column where t o o u t p u t g e n e r a t e d c o d e
52          r e t u r n −1;
53       }
54
55   int
56   CustomCodeFormat : : t a b I n d e n t ( )
57       {
58     // Modify t h e i n d e n t a t i o n o f t h e g e n e r a t e d c o d e ( t r i v a l example o f               t a i l o r i n g code g e n e r a t i o n )
59         return defaultIndentation ;
60       }
61
62   int
63   CustomCodeFormat : : maxLineLength ( )
64       {
65         return defaultLineLength ;
66       }
67
68
69   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
70        {
71       // B u i l d t h e p r o j e c t o b j e c t (AST) which we w i l l f i l l up w i t h m u l t i p l e f i l e s and u s e a s a
72       // h a n d l e f o r a l l p r o c e s s i n g o f t h e AST( s ) a s s o c i a t e d w i t h one o r more s o u r c e f i l e s .
73          S g P r o j e c t ∗ p r o j e c t = new S g P r o j e c t ( a r g c , a r g v ) ;
74
75              CustomCodeFormat∗ f o r m a t C o n t r o l = new CustomCodeFormat ( ) ;
76
77              r e t u r n backend ( p r o j e c t , f o r m a t C o n t r o l ) ;
78       }



             Figure 30.1: Example source code showing how to tailor the code generation format.
     30.3. FINAL CODE AFTER TAILORING THE CODE GENERATION                                                          187




 1   e x t e r n i n t min ( i n t   , int   );
 2
 3   v o i d dgemm( d o u b l e ∗a , d o u b l e ∗b , d o u b l e ∗ c , i n t n )
 4   {
 5       int     var 1 ;
 6       int     var 0 ;
 7       int i ;
 8       int j ;
 9       int k ;
10       for ( var 1 = 0;            v a r 1 <= −1 + n ;          v a r 1 += 1 6 ) {
11          for ( var 0 = 0;            v a r 0 <= −1 + n ;           v a r 0 += 1 6 ) {
12             f o r ( i = 0 ; i <= −1 + n ; i += 1 ) {
13                 f o r ( k = v a r 1 ; k <= min(−1 + n , v a r 1 + 1 5 ) ; k += 1 ) {
14                     i n t dummy 1 = k ∗ n + i ;
15                     f o r ( j = v a r 0 ; j <= min ( n + −16 , v a r 0 ) ; j += 1 6 ) {
16                         int    var 2 = ( j );
17                         c [ j ∗ n + i ] = c[ j ∗ n + i ] + a[k ∗ n + i ] ∗ b[ j ∗ n       + k];
18                          var 2 = 1 + var 2 ;
19                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
20                          var 2 = 1 + var 2 ;
21                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
22                          var 2 = 1 + var 2 ;
23                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
24                          var 2 = 1 + var 2 ;
25                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
26                          var 2 = 1 + var 2 ;
27                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
28                          var 2 = 1 + var 2 ;
29                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
30                          var 2 = 1 + var 2 ;
31                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
32                          var 2 = 1 + var 2 ;
33                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
34                          var 2 = 1 + var 2 ;
35                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
36                          var 2 = 1 + var 2 ;
37                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
38                          var 2 = 1 + var 2 ;
39                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
40                          var 2 = 1 + var 2 ;
41                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
42                          var 2 = 1 + var 2 ;
43                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
44                          var 2 = 1 + var 2 ;
45                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
46                          var 2 = 1 + var 2 ;
47                         c [ var 2 ∗ n + i ] = c [ var 2 ∗ n + i ] + a [ k ∗ n + i ]       ∗ b [ var 2 ∗ n + k ] ;
48                     }
49                     f o r ( ; j <= min(−1 + n , v a r 0 + 1 5 ) ; j += 1 ) {
50                         c [ j ∗ n + i ] = c[ j ∗ n + i ] + a[k ∗ n + i ] ∗ b[ j ∗ n       + k];
51                     }
52                 }
53            }
54          }
55      }
56   }



     Figure 30.2: Example source code used as input to program to the tailor the code generation.
     188                              CHAPTER 30. TAILORING THE CODE GENERATION FORMAT




 1   i n t min ( i n t   , int   );
 2
 3   v o i d dgemm( d o u b l e ∗a , d o u b l e ∗b , d o u b l e ∗ c , i n t n )
 4   {
 5           int   var 1 ;
 6           int   var 0 ;
 7           int i ;
 8           int j ;
 9           int k ;
10           for ( var 1 = 0;            v a r 1 <= (−1 + n ) ;             v a r 1 += 1 6 ) {
11                 for ( var 0 = 0;              v a r 0 <= (−1 + n ) ;            v a r 0 += 1 6 ) {
12                         f o r ( i = 0 ; i <= (−1 + n ) ; i += 1 ) {
13                                 f o r ( k = v a r 1 ; k <= min (( −1 + n ) , ( v a r 1 + 1 5 ) ) ; k += 1 ) {
14                                 i n t dummy 1 = ( ( k ∗ n ) + i ) ;
15                                 f o r ( j = v a r 0 ; j <= min ( ( n + −16) , v a r 0 ) ; j += 1 6 ) {
16                                 int      var 2 = j ;
17                                 c [ ( j ∗ n) + i ] = ( c [ ( j ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗ b [ ( j ∗ n)   + k]));
18                                   v a r 2 = (1 + v a r 2 ) ;
19                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
20                                   v a r 2 = (1 + v a r 2 ) ;
21                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
22                                   v a r 2 = (1 + v a r 2 ) ;
23                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
24                                   v a r 2 = (1 + v a r 2 ) ;
25                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
26                                   v a r 2 = (1 + v a r 2 ) ;
27                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
28                                   v a r 2 = (1 + v a r 2 ) ;
29                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
30                                   v a r 2 = (1 + v a r 2 ) ;
31                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
32                                   v a r 2 = (1 + v a r 2 ) ;
33                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
34                                   v a r 2 = (1 + v a r 2 ) ;
35                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
36                                   v a r 2 = (1 + v a r 2 ) ;
37                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
38                                   v a r 2 = (1 + v a r 2 ) ;
39                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
40                                   v a r 2 = (1 + v a r 2 ) ;
41                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
42                                   v a r 2 = (1 + v a r 2 ) ;
43                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
44                                   v a r 2 = (1 + v a r 2 ) ;
45                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
46                                   v a r 2 = (1 + v a r 2 ) ;
47                                 c [ ( var 2 ∗ n) + i ] = ( c [ ( var 2 ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗        b [ ( var 2 ∗ n) + k ] ) ) ;
48                                 }
49                                 f o r ( ; j <= min (( −1 + n ) , ( v a r 0 + 1 5 ) ) ; j += 1 ) {
50                                 c [ ( j ∗ n) + i ] = ( c [ ( j ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗ b [ ( j ∗ n)   + k]));
51                                 }
52                                 }
53                         }
54                 }
55           }
56   }



             Figure 30.3: Output of input code after changing the format of the generated code.
Chapter 31

AST Construction

AST construction is a fundamental operation needed for building ROSE source-to-source trans-
lators. Several levels of interfaces are available in ROSE for users to build AST from scratch.
High level interfaces are recommended to use whenever possible for their simplicity. Low level
interfaces can give users the maximum freedom to manipulate some details in AST trees.
    This chapter uses several examples to demonstrate how to create AST fragments for common
language constructs (such as variable declarations, functions, function calls, etc.) and how to
insert them into an existing AST tree. More examples of constructing AST using high level
interfaces can be found at rose/tests/roseTests/astInterfaceTests. The source files of the high
level interfaces are located in rose/src/frontend/SageIII/sageInterface.


31.1      Variable Declarations
What To Learn Two examples are given to show how to construct a SAGE III AST subtree
for a variable declaration and its insertion into the existing AST tree.

   • Example 1. Building a variable declaration using the high level AST construction and
     manipulation interfaces defined in namespace SageBuilder and SageInterface.
     Figure 31.1 shows the high level construction of an AST fragment (a variable declaration)
     and its insertion into the AST at the top of each block. buildVariableDeclaration() takes
     the name and type to build a variable declaration node. prependStatement() inserts the
     declaration at the top of a basic block node. Details for parent and scope pointers, symbol
     tables, source file position information and so on are handled transparently.

   • Example 2. Building the variable declaration using low level member functions of SAGE
     III node classes.
     Figure 31.2 shows the low level construction of the same AST fragment (for the same
     variable declaration) and its insertion into the AST at the top of each block. SgNode
     constructors and their member functions are used. Side effects for scope, parent pointers
     and symbol tables have to be handled by programmers explicitly.

                                             189
     190                                                                                CHAPTER 31. AST CONSTRUCTION




 1   // S a g e B u i l d e r c o n t a i n s a l l h i g h l e v e l buildXXX ( ) f u n c t i o n s ,
 2   // s u c h a s b u i l d V a r i a b l e D e c l a r a t i o n ( ) , b u i l d L a b e l S t a t e m e n t ( ) e t c .
 3   // S a g e I n t e r f a c e c o n t a i n s h i g h l e v e l AST m a n i p u l a t i o n and u t i l i t y f u n c t i o n s ,
 4   // e . g . appendStatement ( ) , l o o k u p F u n c t i o n S y m b o l I n P a r e n t S c o p e s ( ) e t c .
 5   #i n c l u d e ” r o s e . h”
 6   u s i n g namespace S a g e B u i l d e r ;
 7   u s i n g namespace S a g e I n t e r f a c e ;
 8
 9   c l a s s SimpleInstrumentation : public SgSimpleProcessing
10   {
11   public :
12       v o i d v i s i t ( SgNode ∗ astNode ) ;
13   };
14
15   void
16   S i m p l e I n s t r u m e n t a t i o n : : v i s i t ( SgNode ∗ astNode )
17   {
18       S g B a s i c B l o c k ∗ b l o c k = i s S g B a s i c B l o c k ( astNode ) ;
19       i f ( b l o c k != NULL)
20          {
21               SgVariableDeclaration ∗ variableDeclaration =
22                                   b u i l d V a r i a b l e D e c l a r a t i o n (” newVariable ” , buildIntType       ());
23              prependStatement ( v a r i a b l e D e c l a r a t i o n , block ) ;
24          }
25   }
26
27   int
28   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
29   {
30     SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
31     ROSE ASSERT ( p r o j e c t != NULL ) ;
32
33       SimpleInstrumentation treeTraversal ;
34       treeTraversal . traverseInputFiles ( project ,                          preorder ) ;
35
36       AstTests : : runAllTests ( p r o j e c t ) ;
37       r e t u r n backend ( p r o j e c t ) ;
38   }



          Figure 31.1: AST construction and insertion for a variable using the high level interfaces
     31.1. VARIABLE DECLARATIONS                                                                                                                               191


 1   //   ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an example p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   //   S p e c i f i c a l l y i t shows t h e d e s i g n o f a t r a n s f o r m a t i o n t o i n s t r u m e n t s o u r c e code , p l a c i n g s o u r c e c o d e
 3   //   a t t h e t o p and bottom o f e a c h b a s i c b l o c k .
 4   //   Member f u n c t i o n s o f SAGE I I I AST node c l a s s e s a r e d i r e c t l y u s e d .
 5   //   So a l l d e t a i l s f o r S g F i l e I n f o , s c o p e , p a r e n t , symbol t a b l e s have t o be e x p l i c i t l y h a n d l e d .
 6
 7   #i n c l u d e ” r o s e . h”
 8
 9   class      SimpleInstrumentation :                    public SgSimpleProcessing
10      {
11             public :
12                  void       visit      ( SgNode∗ astNode ) ;
13        };
14
15   void
16   S i m p l e I n s t r u m e n t a t i o n : : v i s i t ( SgNode∗ astNode )
17        {
18            S g B a s i c B l o c k ∗ b l o c k = i s S g B a s i c B l o c k ( astNode ) ;
19             i f ( b l o c k != NULL)
20                  {
21                 // Mark t h i s a s a t r a n s f o r m a t i o n ( r e q u i r e d )
22                        Sg File Info ∗ sourceLocation = Sg File Info : : generateDefaultFileInfoForTransformationNode ( ) ;
23                       ROSE ASSERT( s o u r c e L o c a t i o n != NULL ) ;
24
25                    SgType∗ t y p e = new SgTypeInt ( ) ;
26                    ROSE ASSERT( t y p e != NULL ) ;
27
28                    SgName name = ” n e w V a r i a b l e ” ;
29
30                    S g V a r i a b l e D e c l a r a t i o n ∗ v a r i a b l e D e c l a r a t i o n = new S g V a r i a b l e D e c l a r a t i o n ( s o u r c e L o c a t i o n , name , t y p e ) ;
31                    ROSE ASSERT( v a r i a b l e D e c l a r a t i o n != NULL ) ;
32
33                    SgInitializedName ∗ i n i t i a l i z e d N a m e = ∗( v a r i a b l e D e c l a r a t i o n −>g e t v a r i a b l e s ( ) . b e g i n ( ) ) ;
34
35               // DQ ( 6 / 1 8 / 2 0 0 7 ) : The u n p a r s e r r e q u i r e s t h a t t h e s c o p e be s e t ( f o r name q u a l i f i c a t i o n t o work ) .
36                  initializedName − et scope ( block ) ;
                                               >s
37
38               // L i a o ( 2 / 1 3 / 2 0 0 8 ) : A s t T e s t s r e q u i r e s t h i s t o be s e t
39                  variableDeclaration − et firstNondefiningDeclaration ( variableDeclaration );
                                                      >s
40
41                    ROSE ASSERT( b l o c k − e t s t a t e m e n t s ( ) . s i z e ( ) > 0 ) ;
                                              >g
42
43                    block− e t s t a t e m e n t s ( ) . i n s e r t ( block− e t s t a t e m e n t s ( ) . begin ( ) , v a r i a b l e D e c l a r a t i o n ) ;
                            >g                                                 >g
44                    variableDeclaration − et parent ( block ) ;
                                                  >s
45
46               // Add a symbol t o t h e s y b o l t a b l e f o r t h e new v a r i a b l e
47                  S g V a r i a b l e S y m b o l ∗ v a r i a b l e S y m b o l = new S g V a r i a b l e S y m b o l ( i n i t i a l i z e d N a m e ) ;
48                  b l o c k − n s e r t s y m b o l ( name , v a r i a b l e S y m b o l ) ;
                               >i
49                }
50        }
51
52   int
53   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
54       {
55         SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
56         ROSE ASSERT( p r o j e c t != NULL ) ;
57
58             SimpleInstrumentation treeTraversal ;
59             treeTraversal . traverseInputFiles ( project ,                                  preorder        );
60
61             AstTests : : runAllTests ( p r o j e c t ) ;
62             r e t u r n backend ( p r o j e c t ) ;
63        }



     Figure 31.2: Example source code to read an input program and add a new variable declaration
     at the top of each block.
     192                                                     CHAPTER 31. AST CONSTRUCTION


 1   i n t main ( )
 2        {
 3          f o r ( i n t i =0; i < 4 ;    i ++)
 4               {
 5                  int x ;
 6               }
 7
 8          return 0;
 9      }



           Figure 31.3: Example source code used as input to the translators adding new variable.

 1
 2   i n t main ( )
 3   {
 4       i n t newVariable ;
 5       for ( int i = 0; i < 4;          i ++) {
 6           i n t newVariable ;
 7           int x ;
 8       }
 9       return 0;
10   }



                     Figure 31.4: Output of input to the translators adding new variable.


        Figure 31.3 shows the input code used to test the translator. Figure 31.4 shows the resulting
     output.
     31.2. EXPRESSIONS                                                                                                                                       193

     31.2              Expressions
     Figure 31.5 shows a translator using the high level AST builder interface to add an assignment
     statement right before the last statement in a main() function.
         Figure 31.6 shows the input code used to test the translator. Figure 31.7 shows the resulting
     output.

 1   // E x p r e s s i o n s can be b u i l t u s i n g both bottomup ( recommended ) and topdown o r d e r s .
 2   // Bottomup : b u i l d o p e r a n d s f i r s t , o p e r a t i o n l a t e r
 3   // Topdown : b u i l d o p e r a t i o n f i r s t , s e t o p e r a n d s l a t e r on .
 4
 5   #i n c l u d e ” r o s e . h”
 6   u s i n g namespace S a g e B u i l d e r ;
 7   u s i n g namespace S a g e I n t e r f a c e ;
 8
 9   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
10   {
11       SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
12       // go t o t h e f u n c t i o n body
13       S g F u n c t i o n D e c l a r a t i o n ∗ mainFunc= f i n d M a i n ( p r o j e c t ) ;
14
15        S g B a s i c B l o c k ∗ body= mainFunc−>g e t d e f i n i t i o n ()−> g e t b o d y ( ) ;
16        p u s h S c o p e S t a c k ( body ) ;
17
18       // bottomup : b u i l d o p e r a n d s f i r s t , c r e a t e e x p r e s s i o n l a t e r on
19       //    d o u b l e r e s u l t = 2 ∗ ( 1 − gama ∗ gama ) ;
20        SgExpression ∗ i n i t e x p =
21                            buildMultiplyOp ( buildDoubleVal ( 2 . 0 ) ,
22                                     buildSubtractOp ( buildDoubleVal ( 1 . 0 ) ,
23                                                b u i l d M u l t i p l y O p ( buildVarRefExp ( ” gama ” ) , buildVarRefExp ( ” gama ” )
24                                                                              )));
25        S g V a r i a b l e D e c l a r a t i o n ∗ d e c l = b u i l d V a r i a b l e D e c l a r a t i o n (” r e s u l t ” , buildDoubleType ( ) , b u i l d A s s i g n I n i t i a l i z e r ( i n i t e x p ) ) ;
26
27        S gS tatement ∗ l a s t s t m t = g e t L a s t S t a t e m e n t ( t o p S c o p e S t a c k ( ) ) ;
28        insertStatementBefore ( laststmt , decl ) ;
29
30       // topdown : b u i l d e x p r e s s i o n f i r s t , s e t o p e r a n d s l a t e r on
31       // d o u b l e r e s u l t 2 = a l p h a ∗ b e t a ;
32        SgExpression ∗ i n i t e x p 2 = buildMultiplyOp ( ) ;
33        set L hsOperand ( i n i t e x p 2 , buildVarRefExp ( ” a l p h a ” ) ) ;
34        setRhsOperand ( i n i t e x p 2 , buildVarRefExp ( ” b e t a ” ) ) ;
35
36        S g V a r i a b l e D e c l a r a t i o n ∗ d e c l 2 = b u i l d V a r i a b l e D e c l a r a t i o n (” r e s u l t 2 ” , buildDoubleType ( ) , b u i l d A s s i g n I n i t i a l i z e r ( i n i t e x p 2 ) ) ;
37        l a s t s t m t = getLastStatement ( topScopeStack ( ) ) ;
38        insertStatementBefore ( laststmt , decl2 ) ;
39
40        popScopeStack ( ) ;
41        AstTests : : runAllTests ( p r o j e c t ) ;
42
43        // i n v o k e backend c o m p i l e r t o g e n e r a t e o b j e c t / b i n a r y          files
44         r e t u r n backend ( p r o j e c t ) ;
45
46   }



                                           Figure 31.5: Example translator to add expressions
     194                                                                        CHAPTER 31. AST CONSTRUCTION




 1   i n t main ( )
 2   {
 3       d o u b l e a l p h a= 0 . 5 ;
 4       double beta = 0 . 1 ;
 5       d o u b l e gama = 0 . 7 ;
 6
 7       return 0;
 8   }



                                          Figure 31.6: Example source code used as input




 1
 2   i n t main ( )
 3   {
 4       double alpha = 0 . 5 ;
 5       double beta = 0 . 1 ;
 6       d o u b l e gama = 0 . 7 ;
 7       d o u b l e r e s u l t = 2 . 0 0 0 0 0 ∗ ( 1 . 0 0 0 0 0 − gama ∗ gama ) ;
 8       double r e s u l t 2 = alpha ∗ beta ;
 9       return 0;
10   }



                                                 Figure 31.7: Output of the input
     31.3. ASSIGNMENT STATEMENTS                                                                                                        195

     31.3              Assignment Statements
     Figure 31.8 shows a translator using the high level AST builder interface to add an assignment
     statement right before the last statement in a main() function.
         Figure 31.9 shows the input code used to test the translator. Figure 31.10 shows the resulting
     output.

 1   // S a g e B u i l d e r c o n t a i n s a l l h i g h l e v e l buildXXX ( ) f u n c t i o n s ,
 2   // s u c h a s b u i l d V a r i a b l e D e c l a r a t i o n ( ) , b u i l d L a b e l S t a t e m e n t ( ) e t c .
 3   // S a g e I n t e r f a c e c o n t a i n s h i g h l e v e l AST m a n i p u l a t i o n and u t i l i t y f u n c t i o n s ,
 4   // e . g . appendStatement ( ) , l o o k u p F u n c t i o n S y m b o l I n P a r e n t S c o p e s ( ) e t c .
 5   #i n c l u d e ” r o s e . h”
 6   u s i n g namespace S a g e B u i l d e r ;
 7   u s i n g namespace S a g e I n t e r f a c e ;
 8
 9   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
10   {
11       SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
12
13        // go t o t h e f u n c t i o n body o f main ( )
14        // and push i t t o t h e s c o p e s t a c k
15        S g F u n c t i o n D e c l a r a t i o n ∗ mainFunc= f i n d M a i n ( p r o j e c t ) ;
16        S g B a s i c B l o c k ∗ body= mainFunc−         >g e t d e f i n i t i o n ()−> g e t b o d y ( ) ;
17        p u s h S c o p e S t a c k ( body ) ;
18
19        // b u i l d a v a r i a b l e a s s i g n m e n t s t a t e m e n t : i =9;
20        // buildVarRefExp ( s t r i n g varName ) w i l l a u t o m a t i c a l l y s e a r c h f o r a matching v a r i a b l e symbol s t a r t i n g
21        // from t h e c u r r e n t s c o p e t o t h e g l o b a l s c o p e .
22        SgExprStatement ∗ a s s i g n S t m t = b u i l d A s s i g n S t a t e m e n t ( buildVarRefExp ( ” i ” ) , b u i l d I n t V a l ( 9 ) ) ;
23
24       // i n s e r t i t b e f o r e t h e l a s t r e t u r n s t a t e m e n t
25        S gS tatement ∗ l a s t S t m t = g e t L a s t S t a t e m e n t ( t o p S c o p e S t a c k ( ) ) ;
26        insertStatementBefore ( lastStmt , assignStmt ) ;
27
28        popScopeStack ( ) ;
29
30        // A s t T e s t s e n s u r e s t h e r e i s no d a n g l i n g SgVarRefExp w i t h o u t a mathing symbol
31        AstTests : : runAllTests ( p r o j e c t ) ;
32        r e t u r n backend ( p r o j e c t ) ;
33   }



                             Figure 31.8: Example source code to add an assignment statement


 1   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 2   {
 3       int i ;
 4       return 0;
 5   }



                                            Figure 31.9: Example source code used as input
    196                                                                  CHAPTER 31. AST CONSTRUCTION




1
2   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
3   {
4       int i ;
5       i = 9;
6       return 0;
7   }



                                                   Figure 31.10: Output of the input
     31.4. FUNCTIONS                                                                                                                                             197

     31.4               Functions
     This section shows how to add a function at the top of a global scope in a file. Again, examples
     for both high level and low level constructions of AST are given.

           • Figure 31.11 shows the high level construction of a defining function (a function with a
             function body). Scope information is passed to builder functions explicitly when it is
             needed.

 1   // T h i s e x a m p l e sh ow s how t o c o n s t r u c t a d e f i n i n g           function        ( with    a   function       body )
 2   // u s i n g h i g h l e v e l AST c o n s t r u c t i o n i n t e r f a c e s .
 3   //
 4   #i n c l u d e ” r o s e . h ”
 5   u s i n g namespace S a g e B u i l d e r ;
 6   u s i n g namespace S a g e I n t e r f a c e ;
 7
 8   class      SimpleInstrumentation                 :   public     SgSimpleProcessing
 9      {
10             public :
11                  void       visit     (   SgNode ∗ a s t N o d e      );
12        };
13
14   void
15   S i m p l e I n s t r u m e n t a t i o n : : v i s i t ( SgNode ∗ a s t N o d e )
16        {
17            SgGlobal ∗ g l o b a l S c o p e = i s S g G l o b a l ( astNode ) ;
18             i f ( g l o b a l S c o p e != NULL)
19                  {
20                 // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
21                 // C r e a t e a p a r a m e t e r l i s t w i t h a p a r a m e t e r
22                 // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
23                       SgName v a r 1 n a m e = ” v a r n a m e ” ;
24                       SgReferenceType ∗ r e f t y p e = buildReferenceType ( buildIntType ( ) ) ;
25                       S g I n i t i a l i z e d N a m e ∗ v a r 1 i n i t n a m e = b u i l d I n i t i a l i z e d N a m e ( var1 name , r e f t y p e ) ;
26                       SgFunctionParameterList∗ parameterList = buildFunctionParameterList ( ) ;
27                       appendArg ( p a r a m e t e r L i s t , v a r 1 i n i t n a m e ) ;
28
29               // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
30               // C r e a t e a d e f i n i n g f u n c t i o n D e c l a r a t i o n ( w i t h a f u n c t i o n body )
31               // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
32                  SgName f u n c n a m e                                            = ” my function ”;
33                  SgFunctionDeclaration ∗ func                                      = buildDefiningFunctionDeclaration
34                                          ( func name , b u i l d I n t T y p e ( ) , p a r a m e t e r L i s t , g l o b a l S c o p e ) ;
35                  SgBasicBlock∗             func body             = func−         >g e t d e f i n i t i o n ()−> g e t b o d y ( ) ;
36
37               // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
38               // I n s e r t a s t a t e m e n t i n t h e f u n c t i o n body
39               // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
40
41                     SgVarRefExp ∗ v a r r e f = b u i l d V a r R e f E x p ( v ar 1 n a m e , f u n c b o d y ) ;
42                     SgPlusPlusOp ∗ p p e x p r e s s i o n = b u i l d P l u s P l u s O p ( v a r r e f ) ;
43                     SgExprStatement ∗ new stmt = buildExprStatement ( p p e x p r e s s i o n ) ;
44
45               //    i n s e r t a s t a t e m e n t i n t o t h e f u n c t i o n body
46                     p r e p e n d S t a t e m e n t ( n ew stm t , f u n c b o d y ) ;
47                     prependStatement ( func , g l o b a l S c o p e ) ;
48
49                 }
50        }
51
52   int
53   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
54       {
55         SgProject ∗ p r o j e c t = f r o n t e n d ( argc , argv ) ;
56         ROSE ASSERT ( p r o j e c t != NULL ) ;
57
58             SimpleInstrumentation treeTraversal ;
59             treeTraversal . traverseInputFiles ( project ,                             preorder        );
60
61             AstTests : : runAllTests ( p r o j e c t ) ;
62             r e t u r n backend ( p r o j e c t ) ;
63        }




                       Figure 31.11: Addition of function to global scope using high level interfaces


           • Figure 31.12 shows almost the same high level construction of the defining function, but
     198                                                                                                CHAPTER 31. AST CONSTRUCTION

               with an additional scope stack. Scope information is passed to builder functions implicitly
               when it is needed.

 1   // T h i s e x a m p l e sh ow s how t o c o n s t r u c t a d e f i n i n g f u n c t i o n ( w i t h         a   function       body )
 2   // u s i n g h i g h l e v e l AST c o n s t r u c t i o n i n t e r f a c e s .
 3   // A s c o p e s t a c k i s u s e d t o p a s s s c o p e i n f o r m a t i o n i m p l i c i t l y t o       some     builder       functions
 4   #i n c l u d e ” r o s e . h ”
 5   u s i n g namespace S a g e B u i l d e r ;
 6   u s i n g namespace S a g e I n t e r f a c e ;
 7
 8   int
 9   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
10       {
11         SgProject ∗ p r o j e c t = f r o n t e n d ( argc , argv ) ;
12         ROSE ASSERT( p r o j e c t != NULL ) ;
13         SgGlobal ∗ globalSc ope = g e t F i r s t G l o b a l S c o p e               ( project );
14
15     // push g l o b a l s c o p e i n t o s t a c k
16          pushScopeStack ( isSgScopeStatement                            ( globalScope ) ) ;
17
18     //   Create a parameter l i s t with a parameter
19          SgName v a r 1 n a m e = ” v a r n a m e ” ;
20          SgReferenceType ∗ r e f t y p e = buildReferenceType ( buildIntType ( ) ) ;
21          S g I n i t i a l i z e d N a m e ∗ v a r 1 i n i t n a m e = b u i l d I n i t i a l i z e d N a m e ( var1 name , r e f t y p e ) ;
22          SgFunctionParameterList∗ parameterList = buildFunctionParameterList ( ) ;
23          appendArg ( p a r a m e t e r L i s t , v a r 1 i n i t n a m e ) ;
24
25     //   C r e a t e a d e f i n i n g f u n c t i o n D e c l a r a t i o n ( w i t h a f u n c t i o n body )
26          SgName f u n c n a m e                                            = ” my function ”;
27          SgFunctionDeclaration ∗ func                                      = buildDefiningFunctionDeclaration
28                                  ( func name , b u i l d I n t T y p e ( ) , p a r a m e t e r L i s t ) ;
29          SgBasicBlock∗             func body             = func−         >g e t d e f i n i t i o n ()−> g e t b o d y ( ) ;
30
31     //   push f u n c t i o n body s c o p e i n t o s t a c k
32          pushScopeStack ( isSgScopeStatement ( func body ) ) ;
33
34     //   b u i l d a s t a t e m e n t i n t h e f u n c t i o n body
35          SgVarRefExp ∗ v a r r e f = b u i l d V a r R e f E x p ( v a r 1 n a m e ) ;
36          SgPlusPlusOp ∗ p p e x p r e s s i o n = b u i l d P l u s P l u s O p ( v a r r e f ) ;
37          SgExprStatement ∗ new stmt = buildExprStatement ( p p e x p r e s s i o n ) ;
38
39     //   i n s e r t a statement in to the fu n c t io n                 body
40          appendStatement ( new stmt ) ;
41   //     pop f u n c t i o n body o f f t h e s t a c k
42          popScopeStack ( ) ;
43
44   //     i n s e r t the f u n c t i o n d e c l a r a t i o n   into    the    scope    at   the    top    of   the    scope     stack
45          prependStatement ( func ) ;
46          popScopeStack ( ) ;
47
48          AstTests : : runAllTests ( p r o j e c t ) ;
49          r e t u r n backend ( p r o j e c t ) ;
50    }




     Figure 31.12: Addition of function to global scope using high level interfaces and a scope stack


            • The low level construction of the AST fragment of the same function declaration and
              its insertion is separated into two portions and shown in two figures (Figure 31.13 and
              Figure 31.14 ).

            Figure 31.29 and Figure 31.30 give the input code and output result for the translators above.
     31.4. FUNCTIONS                                                                                                                                                          199


 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e           is    an e x a m p l e    preprocessor         b u i l t w i t h ROSE .
 2   // S p e c i f i c a l l y i t sh ow s t h e d e s i g n o f a t r a n s f o r m a t i o n        to    instrument          s o u r c e code ,   p l a c i n g s o u r c e code
 3   // a t t h e t o p o f t h e s o u r c e f i l e .
 4
 5   #i n c l u d e   ” r o s e . h”
 6
 7   #d e f i n e TRANSFORMATION FILE INFO S g F i l e I n f o : : g e n e r a t e D e f a u l t F i l e I n f o F o r T r a n s f o r m a t i o n N o d e ( )
 8
 9   class      SimpleInstrumentation                 :   public      SgSimpleProcessing
10      {
11             public :
12                  void         visit    (   SgNode ∗ a s t N o d e      );
13        };
14
15   void
16   S i m p l e I n s t r u m e n t a t i o n : : v i s i t ( SgNode ∗ a s t N o d e )
17        {
18            SgGlobal ∗ g l o b a l S c o p e = i s S g G l o b a l ( astNode ) ;
19             i f ( g l o b a l S c o p e != NULL)
20                  {
21                 // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
22                 // C r e a t e t h e f u n c t i o n D e c l a r a t i o n
23                 // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
24                       SgType ∗ f u n c r e t u r n t y p e                        = new S g T y p e I n t ( ) ;
25                       SgName f u n c n a m e                                      = ” my function ”;
26                       SgFunctionType ∗ f u n c t y p e                            = new S g F u n c t i o n T y p e ( f u n c r e t u r n t y p e , f a l s e ) ;
27                       SgFunctionDeclaration ∗ func                                = new S g F u n c t i o n D e c l a r a t i o n ( TRANSFORMATION FILE INFO , f u n c n a m e ,                 func type ) ;
28                       SgFunctionDefinition ∗                   func def           = new S g F u n c t i o n D e f i n i t i o n ( TRANSFORMATION FILE INFO , f u n c ) ;
29                       SgBasicBlock                          ∗ func body           = new S g B a s i c B l o c k (TRANSFORMATION FILE INFO ) ;
30
31               //    s e t t h e end s o u r c e p o s i t i o n a s t r a n s f o r m a t i o n g e n e r a t e d
32               //    s i n c e the c o n s t r u c t o r s only s e t the beginning source p o s i t i o n                       by    default
33                     func−    >s e t e n d O f C o n s t r u c t (TRANSFORMATION FILE INFO ) ;
34                     func−    >g e t e n d O f C o n s t r u c t ()−> s e t p a r e n t ( f u n c ) ;
35
36                      func def−>s e t e n d O f C o n s t r u c t (TRANSFORMATION FILE INFO ) ;
37                      func def−>g e t e n d O f C o n s t r u c t ()−> s e t p a r e n t ( f u n c d e f ) ;
38
39                     func body−>s e t e n d O f C o n s t r u c t (TRANSFORMATION FILE INFO ) ;
40                     func body−>g e t e n d O f C o n s t r u c t ()−> s e t p a r e n t ( f u n c b o d y ) ;
41
42               //    Sets     t h e body i n t o t h e d e f i n i t i o n
43                     func     def−  >s e t b o d y ( f u n c b o d y ) ;
44               //    Sets     the d e f i n t i o n ’ s parent to the             declaration
45                     func     def−  >s e t p a r e n t ( f u n c ) ;
46
47               // DQ ( 9 / 8 / 2 0 0 7 ) : F ix u p t h e d e f i n i n g and non−d e f i n i n g d e c l a r a t i o n s
48                  ROSE ASSERT ( f u n c −     >g e t d e f i n i n g D e c l a r a t i o n ( ) == NULL ) ;
49                  func− >s e t d e f i n i n g D e c l a r a t i o n ( f u n c ) ;
50                  ROSE ASSERT ( f u n c −     >g e t d e f i n i n g D e c l a r a t i o n ( )                 != NULL ) ;
51                  ROSE ASSERT ( f u n c −     >g e t f i r s t N o n d e f i n i n g D e c l a r a t i o n ( ) != f u n c ) ;
52
53               // DQ ( 9 / 8 / 2 0 0 7 ) : We h a v e n o t b u i l d a non−d e f i n i n g d e c l a r a t i o n , s o               this     should   be NULL .
54                  ROSE ASSERT ( f u n c −    >g e t f i r s t N o n d e f i n i n g D e c l a r a t i o n ( ) == NULL ) ;
55
56               // DQ ( 9 / 8 / 2 0 0 7 ) : Need t o add f u n c t i o n s y m b o l t o g l o b a l s c o p e !
57                  // p r i n t f ( ” F i x i n g up t h e s y m b o l t a b l e i n s c o p e = %p = %s f o r f u n c t i o n = %p = %s \n ” , g l o b a l S c o p e , g l o b a l S c o p e −>c l a s s n a m e ( ) . c s t r ( ) , f u
58                  S g F u n c t i o n S y m b o l ∗ f u n c t i o n S y m b o l = new S g F u n c t i o n S y m b o l ( f u n c ) ;
59                  g l o b a l S c o p e − n s e r t s y m b o l ( func−
                                            >i                                   >g e t n a m e ( ) , f u n c t i o n S y m b o l ) ;
60                  ROSE ASSERT ( g l o b a l S c o p e − o o k u p f u n c t i o n s y m b o l ( f u n c −
                                                                  >l                                                    >g e t n a m e ( ) ) != NULL ) ;
61
62               // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
63               // C r e a t e t h e I n i t i a l i z e d N a m e f o r a p a r a m e t e r w i t h i n t h e p a r a m e t e r l i s t
64               // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
65                  SgName v a r 1 n a m e = ” v a r n a m e ” ;
66
67                     SgTypeInt ∗ v a r 1 t y p e                   = new S g T y p e I n t ( ) ;
68                     S g R e f e r e n c e T y p e ∗ r e f t y p e = new S g R e f e r e n c e T y p e ( v a r 1 t y p e ) ;
69                     S g I n i t i a l i z e r ∗ v a r 1 i n i t i a l i z e r = NULL ;
70
71                     S g I n i t i a l i z e d N a m e ∗ v a r 1 i n i t n a m e = new S g I n i t i a l i z e d N a m e ( v a r 1 n a m e ,   ref type ,    v a r 1 i n i t i a l i z e r , NULL ) ;
72                     v a r 1 i n i t n a m e − s e t f i l e i n f o (TRANSFORMATION FILE INFO ) ;
                                                     >
73
74               // DQ ( 9 / 8 / 2 0 0 7 ) : We now t e s t t h i s , s o i t              has    to   be    set    explicitly .
75                  var1 init name−          >s e t s c o p e ( f u n c d e f ) ;
76
77               // DQ ( 9 / 8 / 2 0 0 7 ) : Need t o add v a r i a b l e s y m b o l t o g l o b a l s c o p e !
78                  // p r i n t f ( ” F i x i n g up t h e s y m b o l t a b l e i n s c o p e = %p = %s f o r S g I n i t i a l i z e d N a m e = %p = %s \n ” , g l o b a l S c o p e , g l o b a l S c o p e −>c l a s s n a m e ( ) . c
79                  S g V a r i a b l e S y m b o l ∗ v a r s y m b o l = new S g V a r i a b l e S y m b o l ( v a r 1 i n i t n a m e ) ;
80                  func def− nsert symbol ( var1 init name−
                                      >i                                            >g e t n a m e ( ) , v a r s y m b o l ) ;




             Figure 31.13: Example source code shows addition of function to global scope (part 1).
     200                                                                                                          CHAPTER 31. AST CONSTRUCTION


 1                         ROSE ASSERT ( f u n c d e f − o o k u p v a r i a b l e s y m b o l ( v a r 1 i n i t n a m e −
                                                         >l                                                               >g e t n a m e ( ) )                   != NULL ) ;
 2                         ROSE ASSERT ( v a r 1 i n i t n a m e −>g e t s y m b o l f r o m s y m b o l t a b l e ( ) != NULL ) ;
 3
 4                    // Done       constructing            the    InitializedName               variable
 5
 6                    // I n s e r t argument i n f u n c t i o n p a r a m e t e r l i s t
 7                       ROSE ASSERT ( f u n c != NULL ) ;
 8                    // S g F i l e I n f o ∗ p a r a m e t e r L i s t F i l e I n f o           = new S g F i l e I n f o ( ) ;
 9                    // S g F i l e I n f o ∗ p a r a m e t e r L i s t F i l e I n f o = S g F i l e I n f o : : g e n e r a t e D e f a u l t F i l e I n f o F o r T r a n s f o r m a t i o n N o d e ( ) ;
10                       S g F u n c t i o n P a r a m e t e r L i s t ∗ p a r a m e t e r L i s t = new S g F u n c t i o n P a r a m e t e r L i s t (TRANSFORMATION FILE INFO ) ;
11                       ROSE ASSERT ( p a r a m e t e r L i s t != NULL ) ;
12                       func−    >s e t p a r a m e t e r L i s t ( p a r a m e t e r L i s t ) ;
13                       ROSE ASSERT ( f u n c −         >g e t p a r a m e t e r L i s t ( ) != NULL ) ;
14                       func−    >g e t p a r a m e t e r L i s t ()−> a p p e n d a r g ( v a r 1 i n i t n a m e ) ;
15
16                    // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
17                    // I n s e r t a s t a t e m e n t i n t h e f u n c t i o n body
18                    // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
19
20                    //   c r e a t e a VarRefExp
21                    //   S g V a r i a b l e S y m b o l ∗ v a r s y m b o l = new S g V a r i a b l e S y m b o l ( v a r 1 i n i t n a m e ) ;
22                         SgVarRefExp ∗ v a r r e f = new SgVarRefExp ( TRANSFORMATION FILE INFO , v a r s y m b o l ) ;
23                         var ref−       >s e t e n d O f C o n s t r u c t (TRANSFORMATION FILE INFO ) ;
24                         var ref−       >g e t e n d O f C o n s t r u c t ()−> s e t p a r e n t ( v a r r e f ) ;
25
26                    //   c r e a t e a ++ e x p r e s s i o n , 0 f o r p r e f i x ++
27                         S g P l u s P l u s O p ∗ p p e x p r e s s i o n = new S g P l u s P l u s O p (TRANSFORMATION FILE INFO , v a r r e f , 0 ) ;
28                         pp expression−           >s e t e n d O f C o n s t r u c t (TRANSFORMATION FILE INFO ) ;
29                         pp expression−           >g e t e n d O f C o n s t r u c t ()−> s e t p a r e n t ( p p e x p r e s s i o n ) ;
30
31                    //   c r e a t e an e x p r e s s i o n s t a t e m e n t
32                         S g E x p r S t a t e m e n t ∗ n e w s t m t = new S g E x p r S t a t e m e n t ( TRANSFORMATION FILE INFO , p p e x p r e s s i o n ) ;
33                         n e w stm t−     >s e t e n d O f C o n s t r u c t (TRANSFORMATION FILE INFO ) ;
34                         n e w stm t−     >g e t e n d O f C o n s t r u c t ()−> s e t p a r e n t ( n e w s t m t ) ;
35
36   #i f       0
37                    // DQ ( 9 / 8 / 2 0 0 7 ) : T h i s i s no l o n g e r r e q u i r e d ,             SgExpressionRoot              is   not     longer       used     in   t h e ROSE IR .
38                    // c r e a t e an e x p r e s s i o n t y p e
39                       S g T y p e I n t ∗ e x p r t y p e = new S g T y p e I n t ( ) ;
40
41                    //   c r e a t e an e x p r e s s i o n r o o t
42                         S g E x p r e s s i o n R o o t ∗ e x p r r o o t = new S g E x p r e s s i o n R o o t ( TRANSFORMATION FILE INFO , p p e x p r e s s i o n , e x p r t y p e ) ;
43                         expr root−          >s e t p a r e n t ( n e w s t m t ) ;
44
45                    // DQ ( 1 1 / 8 / 2 0 0 6 ) : M o d i f i e d t o r e f l e c t u s e         of     SgExpression           instead       of    SgExpressionRoot
46                       n e w stm t−  >s e t e x p r e s s i o n ( e x p r r o o t ) ;
47
48                         pp expression−>s e t p a r e n t ( ne w stm t−>g e t e x p r e s s i o n ( ) ) ;
49   #e n d i f
50                         pp expression−>s e t p a r e n t ( n e w s t m t ) ;
51
52                    //   i n s e r t a s t a t e m e n t i n t o t h e f u n c t i o n body
53                         func body−     >p r e p e n d s t a t e m e n t ( n e w s t m t ) ;
54
55                    //   s e t t i n g the parent e x p l i c i t l y i s            not     required        since       it   would be        done     w i t h i n AST p o s t −p r o c e s s i n g
56                         func−     >s e t p a r e n t ( g l o b a l S c o p e ) ;
57
58                    //   s c o p e s o f s t a t m e n t s must be s e t e x p l i c i t l y s i n c e w i t h i n C++ t h e y a r e n o t                     guaranteed
59                    //   t o be t h e same a s t h a t i n d i c a t e d by t h e p a r e n t ( s e e ChangeLog f o r S p r i n g                              2005).
60                         func−    >s e t s c o p e ( g l o b a l S c o p e ) ;
61
62                    // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
63                    // I n s e r t t h e f u n c t i o n d e c l a r a t i o n i n t h e c o d e
64                    // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
65                       globalScope−       >p r e p e n d d e c l a r a t i o n ( f u n c ) ;
66
67                    //   R e q u i r e d p o s t p r o c e s s i n g o f AST r e q u i r e d        to    set    parent       pointers        and     fixup      template        names ,      etc .
68                    //   temporaryAstFixes ( globalScope ) ;
69                         AstPostProcessing ( globalScope ) ;
70                     }
71          }
72
73   int
74   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
75       {
76         SgProject ∗ p r o j e c t = f r o n t e n d ( argc , argv ) ;
77         ROSE ASSERT( p r o j e c t != NULL ) ;
78
79                  SimpleInstrumentation treeTraversal ;
80                  treeTraversal . traverseInputFiles ( project ,                                 preorder        );




                Figure 31.14: Example source code shows addition of function to global scope (part 2).
     31.4. FUNCTIONS                                                                              201




 1
 2   i n t main ( )
 3        {
 4          f o r ( i n t i =0; i < 4 ;     i ++)
 5               {
 6                  int x ;
 7               }
 8
 9           return 0;
10       }



             Figure 31.15: Example source code used as input to translator adding new function.




 1
 2   i n t m y f u n c t i o n ( i n t &var name )
 3   {
 4      ++var name ;
 5   }
 6
 7   i n t main ( )
 8   {
 9       for ( int i = 0;       i < 4;     i ++) {
10         int x ;
11       }
12       return 0;
13   }



                         Figure 31.16: Output of input to translator adding new function.
                         202                                                     CHAPTER 31. AST CONSTRUCTION

                         31.5        Function Calls
                         Adding functions calls is a typical task for instrumentation translator.

                               • Figure 31.17 shows the use of the AST string based rewrite mechanism to add function
                                 calls to the top and bottom of each block within the AST.

                               • Figure 31.18 shows the use of the AST builder interface to do the same instrumentation
                                 work.

                         Figure 31.19 shows the input code used to get the translator. Figure 31.20 shows the resulting
                         output.
                             Another example shows how to add a function call at the end of each function body. A utility
                         function, instrumentEndOfFunction(), from SageInterface name space is used. The interface tries
                         to locate all return statements of a target function and rewriting return expressions with side
                         effects, if there are any. Figure 31.21 shows the translator code. Figure 31.22 shows the input
                         code. The instrumented code is shown in Figure 31.23.


                         31.6        Creating a ’struct’ for Global Variables
: TODO: This tutorial
vel AST manipulation.    This is an example written to support the Charm++ tool. This translator extracts global
 d have a more concise   variables from the program and builds a structure to hold them. The support is part of a
sing SageInterface and
SageBuilder functions.
                         number of requirements associated with using Charm++ and AMPI.
                             Figure 31.24 shows repackaging of global variables within an application into a struct. All
                         reference to the global variables are also transformed to reference the original variable indirectly
                         through the structure. This processing is part of preprocessing to use Charm++.
                             This example shows the low level handling directly at the level of the IR.
     31.6. CREATING A ’STRUCT’ FOR GLOBAL VARIABLES                                                                                                 203




 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an example p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   // S p e c i f i c a l l y i t shows t h e d e s i g n o f a t r a n s f o r m a t i o n t o i n s t r u m e n t s o u r c e code , p l a c i n g s o u r c e c o d e
 3   // a t t h e t o p and bottome o f e a c h b a s i c b l o c k .
 4
 5   #i n c l u d e ” r o s e . h”
 6
 7   u s i n g namespace s t d ;
 8
 9   class      SimpleInstrumentation :                 public SgSimpleProcessing
10      {
11             public :
12                  void       visit    ( SgNode∗ astNode ) ;
13        };
14
15   void
16   S i m p l e I n s t r u m e n t a t i o n : : v i s i t ( SgNode∗ astNode )
17        {
18            S g B a s i c B l o c k ∗ b l o c k = i s S g B a s i c B l o c k ( astNode ) ;
19             i f ( b l o c k != NULL)
20                  {
21                       c o n s t u n s i g n e d i n t SIZE OF BLOCK = 1 ;
22                        i f ( b l o c k − e t s t a t e m e n t s ( ) . s i z e ( ) > SIZE OF BLOCK )
                                             >g
23                            {
24                           // I t i s up t o t h e u s e r t o l i n k t h e i m p l e m e n t a t i o n s o f t h e s e f u n c t i o n s l i n k t i m e
25                                s t r i n g codeAtTopOfBlock                   = ” v o i d my TimerF unct io nSta rt ( ) ; myTi mer Functi onSt art ( ) ; ” ;
26                                s t r i n g codeAtBottomOfBlock = ” v o i d myTimerFunctionEnd ( ) ; myTimerFunctionEnd ( ) ; ” ;
27
28                       // I n s e r t new c o d e i n t o t h e s c o p e r e p r e s e n t e d by t h e s t a t e m e n t ( a p p l i e s t o S g S c o p e S t a t e m e n t s )
29                          MiddleLevelRewrite : : ScopeIdentifierEnum scope = MidLevelCollectionTypedefs : : StatementScope ;
30
31                       // I n s e r t t h e new c o d e a t t h e t o p and bottom o f t h e s c o p e r e p r e s e n t e d by b l o c k
32                          M i d d l e L e v e l R e w r i t e : : i n s e r t ( b l o c k , codeAtTopOfBlock , s c o p e ,
33                                                                                M i d L e v e l C o l l e c t i o n T y p e d e f s : : TopOfCurrentScope ) ;
34                          M i d d l e L e v e l R e w r i t e : : i n s e r t ( b l o c k , codeAtBottomOfBlock , s c o p e ,
35                                                                                M i d L e v e l C o l l e c t i o n T y p e d e f s : : BottomOfCurrentScope ) ;
36                        }
37                 }
38        }
39
40   int
41   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
42       {
43         SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
44         ROSE ASSERT( p r o j e c t != NULL ) ;
45
46             SimpleInstrumentation treeTraversal ;
47             treeTraversal . traverseInputFiles ( project ,                           preorder        );
48
49             AstTests : : runAllTests ( p r o j e c t ) ;
50             r e t u r n backend ( p r o j e c t ) ;
51        }



                         Figure 31.17: Example source code to instrument any input program.
     204                                                                                     CHAPTER 31. AST CONSTRUCTION


 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e i s an example p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   // S p e c i f i c a l l y i t shows t h e d e s i g n o f a t r a n s f o r m a t i o n t o i n s t r u m e n t s o u r c e code , p l a c i n g s o u r c e c o d e
 3   // a t t h e t o p and bottom o f e a c h b a s i c b l o c k .
 4
 5   #i n c l u d e ” r o s e . h”
 6   u s i n g namespace s t d ;
 7   u s i n g namespace S a g e I n t e r f a c e ;
 8   u s i n g namespace S a g e B u i l d e r ;
 9
10   c l a s s SimpleInstrumentation : public SgSimpleProcessing
11   {
12   public :
13       v o i d v i s i t ( SgNode ∗ astNode ) ;
14   };
15
16   void
17   S i m p l e I n s t r u m e n t a t i o n : : v i s i t ( SgNode ∗ astNode )
18   {
19       S g B a s i c B l o c k ∗ b l o c k = i s S g B a s i c B l o c k ( astNode ) ;
20       i f ( b l o c k != NULL)
21          {
22               c o n s t u n s i g n e d i n t SIZE OF BLOCK = 1 ;
23               i f ( b l o c k − e t s t a t e m e n t s ( ) . s i z e ( ) > SIZE OF BLOCK )
                                    >g
24                   {
25                       SgName name1 ( ” my TimerF unct io nSta rt ” ) ;
26                       // I t i s up t o t h e u s e r t o l i n k t h e i m p l e m e n t a t i o n s o f t h e s e f u n c t i o n s l i n k t i m e
27                       SgFunctionDeclaration ∗ decl 1 = buildNondefiningFunctionDeclaration
28                              ( name1 , b u i l d V o i d T y p e ( ) , b u i l d F u n c t i o n P a r a m e t e r L i s t ( ) , b l o c k ) ;
29                        (( decl 1 − et declarationModifier ( ) ) . get storageModifier ( ) ) . setExtern ( ) ;
                                           >g
30
31                    SgExprStatement ∗ c a l l S t m t 1 = b u i l d F u n c t i o n C a l l S t m t
32                        ( name1 , b u i l d V o i d T y p e ( ) , b u i l d E x p r L i s t E x p ( ) , b l o c k ) ;
33
34                    prependStatement ( call Stmt 1 , block ) ;
35                    prependStatement ( d e c l 1 , block ) ;
36
37                    SgName name2 ( ” myTimerFunctionEnd ” ) ;
38                    // I t i s up t o t h e u s e r t o l i n k t h e i m p l e m e n t a t i o n s o f t h e s e f u n c t i o n s l i n k t i m e
39                    SgFunctionDeclaration ∗ decl 2 = buildNondefiningFunctionDeclaration
40                         ( name2 , b u i l d V o i d T y p e ( ) , b u i l d F u n c t i o n P a r a m e t e r L i s t ( ) , b l o c k ) ;
41                    (( decl 2 − et declarationModifier ( ) ) . get storageModifier ( ) ) . setExtern ( ) ;
                                 >g
42
43                    SgExprStatement ∗ c a l l S t m t 2 = b u i l d F u n c t i o n C a l l S t m t
44                        ( name2 , b u i l d V o i d T y p e ( ) , b u i l d E x p r L i s t E x p ( ) , b l o c k ) ;
45
46                    appendStatement ( d e c l 2 , b l o c k ) ;
47                    appendStatement ( c a l l S t m t 2 , b l o c k ) ;
48                }
49         }
50   }
51
52   int
53   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
54   {
55     SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
56     ROSE ASSERT ( p r o j e c t != NULL ) ;
57
58       SimpleInstrumentation treeTraversal ;
59       treeTraversal . traverseInputFiles ( project ,                              preorder ) ;
60
61       AstTests : : runAllTests ( p r o j e c t ) ;
62       r e t u r n backend ( p r o j e c t ) ;
63   }



                            Figure 31.18: Example source code using the high level interfaces
     31.6. CREATING A ’STRUCT’ FOR GLOBAL VARIABLES                                                    205




 1   // O v e r l o a d e d f u n c t i o n s   for   t e s t i n g overloaded function   resolution
 2   void foo ( double )
 3      {
 4         int x = 1;
 5         int y ;
 6
 7      // I t h i n k t h a t t h i s c a s e f a i l s     currently
 8      // i f ( x ) y = 1 ; e l s e y = 2 ;
 9       }



                  Figure 31.19: Example source code used as input to instrumenting translator.




 1   // O v e r l o a d e d f u n c t i o n s   for   t e s t i n g overloaded function   resolution
 2
 3   void foo ( double )
 4   {
 5     v o i d myT imerFunct io nSt ar t ( ) ;
 6     myT imerFunctionSta rt ( ) ;
 7     int x = 1;
 8     int y ;
 9     v o i d myTimerFunctionEnd ( ) ; ;
10     myTimerFunctionEnd ( ) ;
11   // I t h i n k t h a t t h i s c a s e f a i l s     currently
12   // i f ( x ) y = 1 ; e l s e y = 2 ;
13   }



                                  Figure 31.20: Output of input to instrumenting translator.
     206                                                                                         CHAPTER 31. AST CONSTRUCTION


 1   /∗! \ b r i e f      test instrumentation right                           b e f o r e t h e end o f a f u n c t i o n
 2   ∗/
 3   #i n c l u d e ” r o s e . h”
 4   #i n c l u d e <i o s t r e a m >
 5   u s i n g namespace S a g e I n t e r f a c e ;
 6   u s i n g namespace S a g e B u i l d e r ;
 7
 8   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 9   {
10       SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
11
12           // Find a l l f u n c t i o n d e f i n i t i o n s we want t o i n s t r u m e n t
13           s t d : : v e c t o r <SgNode∗ > f u n c D e f L i s t =
14               NodeQuery : : querySubTree ( p r o j e c t , V S g F u n c t i o n D e f i n i t i o n ) ;
15
16       s t d : : v e c t o r <SgNode ∗ > : : i t e r a t o r i t e r ;
17       f o r ( i t e r = f u n c D e f L i s t . b e g i n ( ) ; i t e r != f u n c D e f L i s t . end ( ) ; i t e r ++)
18       {
19           SgFunctionDefinition ∗ c u r d e f = i s S g F u n c t i o n D e f i n i t i o n (∗ i t e r ) ;
20           ROSE ASSERT( c u r d e f ) ;
21         S g B a s i c B l o c k ∗ body = c u r d e f − e t b o d y ( ) ;
                                                                   >g
22         // B u i l d t h e c a l l s t a t e m e n t f o r e a c h p l a c e
23         SgExprStatement ∗ c a l l S t m t 1 = b u i l d F u n c t i o n C a l l S t m t ( ” c a l l 1 ” ,
24                                  b u i l d I n t T y p e ( ) , b u i l d E x p r L i s t E x p ( ) , body ) ;
25
26           // i n s t r u m e n t t h e f u n c t i o n
27           i n t i= i n s t r u m e n t E n d O f F u n c t i o n ( c u r d e f − e t d e c l a r a t i o n ( ) , c a l l S t m t 1 ) ;
                                                                                   >g
28           s t d : : cout <<”I n s t r u m e n t e d ”<<i <<” p l a c e s . ”<<s t d : : e n d l ;
29
30       }      // end o f i n s t r u m e n t a t i o n
31
32           AstTests : : runAllTests ( p r o j e c t ) ;
33           // t r a n s l a t i o n o n l y
34            project − nparse ( ) ;
                           >u
35   }



                                Figure 31.21: Example source code instrumenting end of functions


 1   /∗ Example c o d e :
 2     ∗ a f u n c t i o n with m u l t i p l e r e t u r n s
 3     ∗         some r e t u r n s have e x p r e s s i o n s w i t h s i d e               effects
 4     ∗ a f u n c t i o n w i t h o u t any r e t u r n
 5     ∗/
 6   extern int foo ( ) ;
 7   extern int c a l l 1 ( ) ;
 8   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 9   {
10       i f ( a r g c >1)
11          return foo ( ) ;
12       else
13           return foo ( ) ;
14       return 0;
15   }
16
17   void bar ( )
18   {
19     int i ;
20   }



                Figure 31.22: Example input code of the instrumenting translator for end of functions.
     31.6. CREATING A ’STRUCT’ FOR GLOBAL VARIABLES                                              207




 1   /∗ Example c o d e :
 2    ∗ a f u n c t i o n with m u l t i p l e r e t u r n s
 3    ∗      some r e t u r n s have e x p r e s s i o n s w i t h s i d e   effects
 4    ∗ a f u n c t i o n w i t h o u t any r e t u r n
 5    ∗/
 6   int foo ( ) ;
 7   int call1 ();
 8
 9   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
10   {
11       i f ( argc > 1) {
12          int rose temp 1 = foo ( ) ;
13          call1 ();
14          return rose temp 1 ;
15       }
16       else {
17          int rose temp 2 = foo ( ) ;
18          call1 ();
19          return rose temp 2 ;
20       }
21       call1 ();
22       return 0;
23   }
24
25   void bar ( )
26   {
27     int i ;
28     call1 ();
29   }



                        Figure 31.23: Output of instrumenting translator for end of functions.
     208                                                                                                               CHAPTER 31. AST CONSTRUCTION



 1   // ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e                      is   an e x a m p l e p r e p r o c e s s o r b u i l t w i t h ROSE .
 2   // S p e c i f i c a l l y i t sh ows t h e d e s i g n o f a t r a n s f o r m a t i o n                    to   do a t r a n s f o r m a t i o n s p e c i f i c f o r Charm++.
 3
 4   #i n c l u d e       ” r o s e . h”
 5
 6   using      namespace             std ;
 7
 8   R o s e S T L C o n t a i n e r <S g I n i t i a l i z e d N a m e ∗>
 9   buildListOfGlobalVariables ( SgSourceFile∗                                          file     )
10        {
11      // T h i s f u n c t i o n b u i l d s a l i s t o f g l o b a l                 variables          ( from a       SgFile ) .
12           a s s e r t ( f i l e != NULL ) ;
13
14            R o s e S T L C o n t a i n e r <S g I n i t i a l i z e d N a m e ∗> g l o b a l V a r i a b l e L i s t ;
15
16            SgGlobal ∗ globalSc ope = f i l e −                      >g e t g l o b a l S c o p e ( ) ;
17            a s s e r t ( g l o b a l S c o p e != NULL ) ;
18            R o s e S T L C o n t a i n e r <S g D e c l a r a t i o n S t a t e m e n t ∗ > : : i t e r a t o r i = g l o b a l S c o p e −     >g e t d e c l a r a t i o n s ( ) . b e g i n ( ) ;
19            w h i l e ( i != g l o b a l S c o p e −     >g e t d e c l a r a t i o n s ( ) . end ( ) )
20                 {
21                      S g V a r i a b l e D e c l a r a t i o n ∗ v a r i a b l e D e c l a r a t i o n = i s S g V a r i a b l e D e c l a r a t i o n (∗ i ) ;
22                      i f ( v a r i a b l e D e c l a r a t i o n != NULL)
23                           {
24                                R o s e S T L C o n t a i n e r <S g I n i t i a l i z e d N a m e ∗> & v a r i a b l e L i s t = v a r i a b l e D e c l a r a t i o n −    >g e t v a r i a b l e s ( ) ;
25                                R o s e S T L C o n t a i n e r <S g I n i t i a l i z e d N a m e ∗ > : : i t e r a t o r v a r = v a r i a b l e L i s t . b e g i n ( ) ;
26                                w h i l e ( v a r != v a r i a b l e L i s t . end ( ) )
27                                      {
28                                          g l o b a l V a r i a b l e L i s t . push back (∗ var ) ;
29                                          v a r ++;
30                                      }
31                           }
32                      i ++;
33                 }
34
35            return           globalVariableList ;
36        }
37
38   // T h i s f u n c t i o n i s n o t u s e d , b u t i s u s e f u l f o r
39   // g e n e r a t i n g t h e l i s t o f a l l g l o b a l v a r i a b l e s
40   R o s e S T L C o n t a i n e r <S g I n i t i a l i z e d N a m e ∗>
41   buildListOfGlobalVariables ( SgProject∗ project )
42        {
43      // T h i s f u n c t i o n b u i l d s a l i s t o f g l o b a l v a r i a b l e s                  ( from a       SgProject ) .
44
45            R o s e S T L C o n t a i n e r <S g I n i t i a l i z e d N a m e ∗> g l o b a l V a r i a b l e L i s t ;
46
47            c o n s t S g F i l e P t r L i s t& f i l e L i s t = p r o j e c t − g e t f i l e L i s t ( ) ;
                                                                                    >
48            SgFilePtrList : : c o n s t i t e r a t o r f i l e = f i l e L i s t . begin ( ) ;
49
50       // Loop o v e r t h e             f i l e s in the p r o j e c t ( multiple f i l e s e x i s t
51       // when m u l t i p l e           s o u r c e f i l e s a r e p l a c e d on t h e command l i n e ) .
52          w h i l e ( f i l e !=         f i l e L i s t . end ( ) )
53               {
54                    Rose STL             C o n t a i n e r <S g I n i t i a l i z e d N a m e ∗> f i l e G l o b a l V a r i a b l e L i s t = b u i l d L i s t O f G l o b a l V a r i a b l e s ( i s S g S o u r c e F i l e ( ∗ f i l e ) ) ;
55
56               // DQ ( 9 / 2 6 / 2 0 0 7 ) : Moved f r o m s t d : : l i s t t o s t d : : v e c t o r
57               // g l o b a l V a r i a b l e L i s t . merge ( f i l e G l o b a l V a r i a b l e L i s t ) ;
58                  g l o b a l V a r i a b l e L i s t . i n s e r t ( g l o b a l V a r i a b l e L i s t . b e g i n ( ) , f i l e G l o b a l V a r i a b l e L i s t . b e g i n ( ) , f i l e G l o b a l V a r i a b l e L i s t . end ( ) ) ;
59
60                          f i l e ++;
61                    }
62
63            return           globalVariableList ;
64        }
65
66   R o s e S T L C o n t a i n e r <SgVarRefExp∗>
67   b u i l d L i s t O f V a r i a b l e R e f e r e n c e E x p r e s s i o n s U s i n g G l o b a l V a r i a b l e s ( SgNode ∗ node )
68        {
69      // T h i s f u n c t i o n b u i l d s a l i s t o f ” u s e s ” o f v a r i a b l e s ( SgVarRefExp IR n o d e s )                                   within       t h e AST .
70
71       //   return variable
72            R o s e S T L C o n t a i n e r <SgVarRefExp∗> g l o b a l V a r i a b l e U s e L i s t ;
73
74       //   l i s t o f a l l v a r i a b l e s ( t h e n s e l e c t o u t t h e g l o b a l v a r i a b l e s by                     testing        the scope )
75            R o s e S T L C o n t a i n e r <SgNode∗> n o d e L i s t = NodeQuery : : q u e r y S u b T r e e                          ( node ,       V SgVarRefExp              );
76
77            R o s e S T L C o n t a i n e r <SgNode ∗ > : : i t e r a t o r i = n o d e L i s t . b e g i n ( ) ;
78            w h i l e ( i != n o d e L i s t . end ( ) )
79                 {
80                      SgVarRefExp ∗ v a r i a b l e R e f e r e n c e E x p r e s s i o n = i s S g V a r R e f E x p ( ∗ i ) ;




       Figure 31.24: Example source code shows repackaging of global variables to a struct (part 1).
     31.6. CREATING A ’STRUCT’ FOR GLOBAL VARIABLES                                                                                                                                           209



 1                     assert ( variableReferenceExpression                              != NULL ) ;
 2
 3                     assert ( variableReferenceExpression−>g e t s y m b o l ( ) != NULL ) ;
 4                     assert ( variableReferenceExpression−>g e t s y m b o l ()−> g e t d e c l a r a t i o n ( ) != NULL ) ;
 5                     assert ( variableReferenceExpression−>g e t s y m b o l ()−> g e t d e c l a r a t i o n ()−> g e t s c o p e ( )                                         != NULL ) ;
 6
 7               // Note t h a t v a r i a b l e R e f e r e n c e E x p r e s s i o n −>g e t s y m b o l ()−> g e t d e c l a r a t i o n ( ) r e t u r n s t h e
 8               // S g I n i t i a l i z e d N a m e ( n o t t h e S g V a r i a b l e D e c l a r a t i o n w h e r e i t was d e c l a r e d ) !
 9                  SgInitializedName∗ variable = variableReferenceExpression−                                              >g e t s y m b o l ()−> g e t d e c l a r a t i o n ( ) ;
10
11                     SgScopeStatement ∗                variableScope = variable−>g e t s c o p e ( ) ;
12
13               // Check i f t h i s i s a v a r i a b l e d e c l a r e d i n g l o b a l s c o p e , i f s o , t h e n s a v e                            it
14                  i f ( i s S g G l o b a l ( v a r i a b l e S c o p e ) != NULL)
15                      {
16                          g l o b a l V a r i a b l e U s e L i s t . push back ( v a r i a b l e R e f e r e n c e E x p r e s s i o n ) ;
17                      }
18                  i ++;
19                }
20
21            return       globalVariableUseList ;
22        }
23
24
25   SgClassDeclaration∗
26   b u i l d C l a s s D e c l a r a t i o n A n d D e f i n i t i o n ( s t r i n g name , S g S c o p e S t a t e m e n t ∗   scope )
27        {
28      // T h i s f u n c t i o n b u i l d s a c l a s s d e c l a r a t i o n and d e f i n i t i o n
29      // ( b o t h t h e d e f i n i n g and n o n d e f i n i n g d e c l a r a t i o n s a s r e q u i r e d ) .
30
31      //    B u i l d a f i l e i n f o o b j e c t marked a s a t r a n s f o r m a t i o n
32            Sg File Info ∗ f i l e I n f o = Sg File Info : : generateDefaultFileInfoForTransformationNode ( ) ;
33            a s s e r t ( f i l e I n f o != NULL ) ;
34
35      //    This i s the c l a s s d e f i n i t i o n ( the f i l e I n f o                 i s the p o s i t i o n of the opening                     brace )
36            SgClassDefinition∗ classDefinition                      = new                    SgClassDefinition ( f i l e I n f o );
37            a s s e r t ( c l a s s D e f i n i t i o n != NULL ) ;
38
39      //    S e t t h e end o f c o n s t r u c t e x p l i c t l y ( w h e r e n o t a               transformation              this      is    the    location         of    the     closing     brace )
40            classDefinition −    >s e t e n d O f C o n s t r u c t ( f i l e I n f o ) ;
41
42      //    This i s the d e f i n i n g d e c l a r a t i o n f o r the c l a s s ( with a r e f e r e n c e to the c l a s s d e f i n i t i o n )
43            S g C l a s s D e c l a r a t i o n ∗ c l a s s D e c l a r a t i o n = new S g C l a s s D e c l a r a t i o n ( f i l e I n f o , name . c s t r ( ) , S g C l a s s D e c l a r a t i o n : : e s t r u c t , NULL, c l a s s D e f i n i t i o n ) ;
44            a s s e r t ( c l a s s D e c l a r a t i o n != NULL ) ;
45
46      //    Set the d e f i n i n g d e c l a r a t i o n in the d e f i n i n g d e c l a r a t i o n !
47            classDeclaration−       >s e t d e f i n i n g D e c l a r a t i o n ( c l a s s D e c l a r a t i o n ) ;
48
49      //    S e t t h e non d e f i n i n g d e c l a r a t i o n i n t h e d e f i n i n g d e c l a r a t i o n ( b o t h a r e r e q u i r e d )
50            S g C l a s s D e c l a r a t i o n ∗ n o n d e f i n i n g C l a s s D e c l a r a t i o n = new S g C l a s s D e c l a r a t i o n ( f i l e I n f o , name . c s t r ( ) , S g C l a s s D e c l a r a t i o n : : e s t r u c t , NULL, NULL ) ;
51            a s s e r t ( c l a s s D e c l a r a t i o n != NULL ) ;
52            nondefiningClassDeclaration−                         >s e t t y p e ( S g C l a s s T y p e : : c r e a t e T y p e ( n o n d e f i n i n g C l a s s D e c l a r a t i o n ) ) ;
53
54      //    S e t t h e i n t e r n a l r e f e r e n c e t o t h e non−d e f i n i n g d e c l a r a t i o n
55            classDeclaration−           >s e t f i r s t N o n d e f i n i n g D e c l a r a t i o n ( n o n d e f i n i n g C l a s s D e c l a r a t i o n ) ;
56            classDeclaration−           >s e t t y p e ( n o n d e f i n i n g C l a s s D e c l a r a t i o n −>g e t t y p e ( ) ) ;
57
58      //    S e t t h e d e f i n i n g and no−d e f i n i n g d e c l a r a t i o n s i n t h e non−d e f i n i n g c l a s s d e c l a r a t i o n !
59            nondefiningClassDeclaration−               >s e t f i r s t N o n d e f i n i n g D e c l a r a t i o n ( n o n d e f i n i n g C l a s s D e c l a r a t i o n ) ;
60            nondefiningClassDeclaration−               >s e t d e f i n i n g D e c l a r a t i o n ( c l a s s D e c l a r a t i o n ) ;
61
62      //    Set the nondefining d e c l a r a t i o n as a forward                              declaration !
63            n o n d e f i n i n g C l a s s D e c l a r a t i o n − etForward ( ) ;
                                                                     >s
64
65      // Don ’ t f o r g e t t h e s e t t h e d e c l a r a t i o n i n t h e d e f i n i t i o n                 ( IR node          constructors           are     s i d e −e f f e c t   free !)!
66         classDefinition −        >s e t d e c l a r a t i o n ( c l a s s D e c l a r a t i o n ) ;
67
68      //    s e t t h e s c o p e e x p l i c i t l y ( name q u a l i f i c a t i o n t r i c k s           can     imply       it    is   not    always          the   parent       IR node ! )
69            classDeclaration−         >s e t s c o p e ( s c o p e ) ;
70            nondefiningClassDeclaration−                  >s e t s c o p e ( s c o p e ) ;
71
72      // some e r r o r c h e c k i n g
73         assert ( classDeclaration−     >g e t d e f i n i n g D e c l a r a t i o n ( ) != NULL ) ;
74         assert ( classDeclaration−     >g e t f i r s t N o n d e f i n i n g D e c l a r a t i o n ( ) != NULL ) ;
75         assert ( classDeclaration−     >g e t d e f i n i t i o n ( ) != NULL ) ;
76
77      // DQ ( 9 / 8 / 2 0 0 7 ) : Need t o add f u n c t i o n s y m b o l t o g l o b a l s c o p e !
78         p r i n t f ( ” F i x i n g up t h e s y m b o l t a b l e i n s c o p e = %p = %s f o r c l a s s = %p = %s \n ” , s c o p e , s c o p e −>c l a s s n a m e ( ) . c s t r ( ) , c l a s s D e c l a r a t i o n , c l a s s D e
79         S g C l a s s S y m b o l ∗ c l a s s S y m b o l = new S g C l a s s S y m b o l ( c l a s s D e c l a r a t i o n ) ;
80         scope− n s e r t s y m b o l ( c l a s s D e c l a r a t i o n −
                      >i                                                    >g e t n a m e ( ) , c l a s s S y m b o l ) ;




      Figure 31.25: Example source code shows repackaging of global variables to a struct (part 2).
     210                                                                                                             CHAPTER 31. AST CONSTRUCTION



 1            ROSE ASSERT( s c o p e − o o k u p c l a s s s y m b o l ( c l a s s D e c l a r a t i o n −
                                      >l                                                                  >g e t n a m e ( ) )                  != NULL ) ;
 2
 3            return         classDeclaration ;
 4        }
 5
 6
 7
 8   SgVariableSymbol ∗
 9   putGlobalVariablesIntoClass                          ( R o s e S T L C o n t a i n e r <S g I n i t i a l i z e d N a m e ∗> & g l o b a l V a r i a b l e s ,      SgClassDeclaration∗                  classDeclaration                  )
10      {
11     // T h i s f u n c t i o n i t e r a t e s         over      the     list     of   global       variables          and    inserts        them      into     the    iput      class      definition
12
13            SgVariableSymbol ∗                 g l o b a l C l a s s V a r i a b l e S y m b o l = NULL ;
14
15            for     ( R o s e S T L C o n t a i n e r <S g I n i t i a l i z e d N a m e ∗ > : : i t e r a t o r   var = g l o b a l V a r i a b l e s . begin ( ) ;        var    !=    g l o b a l V a r i a b l e s . end ( ) ;   v a r++)
16                {
17               //    p r i n t f ( ” Appending g l o b a l v a r i a b l e = %s t o new g l o b a l V a r i a b l e C o n t a i n e r \n ” , ( ∗ v a r)−>g e t n a m e ( ) . s t r ( ) ) ;
18                     S g V a r i a b l e D e c l a r a t i o n ∗ g l o b a l V a r i a b l e D e c l a r a t i o n = i s S g V a r i a b l e D e c l a r a t i o n ( ( ∗ v a r)−> g e t p a r e n t ( ) ) ;
19                     a s s e r t ( g l o b a l V a r i a b l e D e c l a r a t i o n != NULL ) ;
20
21               // Get t h e g l o b a l s c o p e f r o m t h e g l o b a l v a r i a b l e d i r e c t l y
22                  SgGlobal ∗ globalScope = i sS gG l ob a l ( g l o b a l V a r i a b l e D e c l a r a t i o n −>g e t s c o p e ( ) ) ;
23                  a s s e r t ( g l o b a l S c o p e != NULL ) ;
24
25                      if    ( v a r == g l o b a l V a r i a b l e s . b e g i n ( ) )
26                            {
27                           // T h i s i s t h e f i r s t t i m e i n t h i s l o o p , r e p l a c e t h e f i r s t g l o b a l v a r i a b l e w i t h
28                           // t h e c l a s s d e c l a r a t i o n / d e f i n i t i o n c o n t a i n i n g a l l t h e g l o b a l v a r i a b l e s !
29                           // Note t h a t i n i t i a l i z e r s i n t h e g l o b a l v a r i a b l e d e c l a r a t i o n s r e q u i r e m o d i f i c a t i o n
30                           // o f t h e p r e i n i t i a l i z a t i o n l i s t i n t h e c l a s s ’ s c o n s t r u c t o r !            I am i g n o r i n g t h i s f o r          now !
31                                globalScope−      >r e p l a c e s t a t e m e n t ( g l o b a l V a r i a b l e D e c l a r a t i o n , c l a s s D e c l a r a t i o n ) ;
32
33                           //   B u i l d s o u r c e p o s i t i o n i n f o r m a i t o n ( marked a s t r a n s f o r m a t i o n )
34                                Sg File Info ∗ f i l e I n f o = Sg File Info : : generateDefaultFileInfoForTransformationNode ( ) ;
35                                a s s e r t ( f i l e I n f o != NULL ) ;
36
37                           // Add t h e v a r i a b l e o f t h e c l a s s t y p e t o t h e g l o b a l s c o p e !
38                              S g C l a s s T y p e ∗ v a r i a b l e T y p e = new S g C l a s s T y p e ( c l a s s D e c l a r a t i o n −>g e t f i r s t N o n d e f i n i n g D e c l a r a t i o n ( ) ) ;
39                              a s s e r t ( v a r i a b l e T y p e != NULL ) ;
40                              S g V a r i a b l e D e c l a r a t i o n ∗ v a r i a b l e D e c l a r a t i o n = new S g V a r i a b l e D e c l a r a t i o n ( f i l e I n f o , ” A M P I g l o b a l s ” , v a r i a b l e T y p e ) ;
41                              a s s e r t ( v a r i a b l e D e c l a r a t i o n != NULL ) ;
42
43                                globalScope− nsert statement ( classDeclaration , variableDeclaration , f a l s e ) ;
                                              >i
44
45                                assert ( variableDeclaration−                >g e t v a r i a b l e s ( ) . empty ( ) == f a l s e ) ;
46                                S g I n i t i a l i z e d N a m e ∗ variableName = ∗( v a r i a b l e D e c l a r a t i o n −>g e t v a r i a b l e s ( ) . b e g i n ( ) ) ;
47                                a s s e r t ( v a r i a b l e N a m e != NULL ) ;
48
49                           // DQ ( 9 / 8 / 2 0 0 7 ) : Need t o s e t t h e s c o p e o f                  t h e new      variable .
50                              variableName−         >s e t s c o p e ( g l o b a l S c o p e ) ;
51
52                           //   build the return value
53                                g l o b a l C l a s s V a r i a b l e S y m b o l = new S g V a r i a b l e S y m b o l ( v a r i a b l e N a m e ) ;
54
55                           // DQ ( 9 / 8 / 2 0 0 7 ) : Need t o add t h e s y m b o l t o t h e g l o b a l s c o p e ( new t e s t i n g r e q u i r e s t h i s ) .
56                              g l o b a l S c o p e − n s e r t s y m b o l ( variableName−
                                                       >i                                                  >g e t n a m e ( ) , g l o b a l C l a s s V a r i a b l e S y m b o l ) ;
57                              ROSE ASSERT ( g l o b a l S c o p e − o o k u p v a r i a b l e s y m b o l ( v a r i a b l e N a m e −
                                                                            >l                                                                    >g e t n a m e ( ) ) != NULL ) ;
58                            }
59                           else
60                            {
61                           // f o r a l l o t h e r i t e r a t i o n s o f t h i s l o o p . . .
62                           // remove v a r i a b l e d e c l a r a t i o n f r o m t h e g l o b a l s c o p e
63                              globalScope−           >r e m o v e s t a t e m e n t ( g l o b a l V a r i a b l e D e c l a r a t i o n ) ;
64                            }
65
66               // add t h e v a r i a b l e d e c l a r a t i o n t o t h e c l a s s d e f i n i t i o n
67                  classDeclaration−         >g e t d e f i n i t i o n ()−>append member ( g l o b a l V a r i a b l e D e c l a r a t i o n ) ;
68                }
69
70            return         globalClassVariableSymbol ;
71        }
72
73
74   void
75   f i x u p R e f e r e n c e s T o G l o b a l V a r i a b l e s ( R o s e S T L C o n t a i n e r <SgVarRefExp∗> & v a r i a b l e R e f e r e n c e L i s t , S g V a r i a b l e S y m b o l ∗ g l o b a l C l a s s V a r i a b l e
76         {
77        // Now f i x u p t h e SgVarRefExp t o r e f e r e n c e t h e g l o b a l v a r i a b l e s t h r o u g h a s t r u c t
78             f o r ( R o s e S T L C o n t a i n e r <SgVarRefExp ∗ > : : i t e r a t o r v a r = v a r i a b l e R e f e r e n c e L i s t . b e g i n ( ) ; v a r != v a r i a b l e R e f e r e n c e L i s t . end ( ) ; v a r
79                   {
80                       a s s e r t ( ∗ v a r != NULL ) ;




      Figure 31.26: Example source code shows repackaging of global variables to a struct (part 3).
     31.6. CREATING A ’STRUCT’ FOR GLOBAL VARIABLES                                                                                                                            211



 1              //    printf       (” Variable        reference       f o r %s \n ” , ( ∗ v a r)−> g e t s y m b o l ()−> g e t d e c l a r a t i o n ()−> g e t n a m e ( ) . s t r ( ) ) ;
 2
 3                    SgNode ∗ p a r e n t = ( ∗ v a r)−> g e t p a r e n t ( ) ;
 4                    a s s e r t ( p a r e n t != NULL ) ;
 5
 6              //    I f t h i s i s n o t an e x p r e s s i o n t h e n i s l i k e l y a m e a n i n g l e s s        statement         such    as   (” x ; ” )
 7                    SgExpression∗ parentExpression = isSgExpression ( parent ) ;
 8                    a s s e r t ( p a r e n t E x p r e s s i o n != NULL ) ;
 9
10              //    Build      the    reference        through      the       global   class      variable       (” x” − > ” AMPI globals . x ”)
                                                                                                                          −
11
12              //    B u i l d s o u r c e p o s i t i o n i n f o r m a i t o n ( marked a s t r a n s f o r m a t i o n )
13                    Sg File Info ∗ f i l e I n f o = Sg File Info : : generateDefaultFileInfoForTransformationNode ( ) ;
14                    a s s e r t ( f i l e I n f o != NULL ) ;
15
16              // Build ” AMPI globals ”
17                 S g E x p r e s s i o n ∗ l h s = new SgVarRefExp ( f i l e I n f o , g l o b a l C l a s s V a r i a b l e S y m b o l ) ;
18                 a s s e r t ( l h s != NULL ) ;
19              // B u i l d ” A M P I g l o b a l s . x ” f r o m ” x ”
20                 SgDotExp∗ g l o b a l V a r i a b l e R e f e r e n c e = new SgDotExp ( f i l e I n f o , l h s , ∗ v a r ) ;
21                 a s s e r t ( g l o b a l V a r i a b l e R e f e r e n c e != NULL ) ;
22
23                    if    ( p a r e n t E x p r e s s i o n != NULL)
24                          {
25                         // I n t r o d u c e r e f e r e n c e t o ∗ v a r    through     the    data    structure
26
27                         //      case of binary operator
28                                 SgUnaryOp∗ u n a r y O p e r a t o r = isSgUnaryOp ( p a r e n t E x p r e s s i o n ) ;
29                                 i f ( u n a r y O p e r a t o r != NULL)
30                                     {
31                                        unaryOperator−              >s e t o p e r a n d ( g l o b a l V a r i a b l e R e f e r e n c e ) ;
32                                     }
33                                    else
34                                     {
35                                    // c a s e o f b i n a r y o p e r a t o r
36                                        SgBinaryOp ∗ b i n a r y O p e r a t o r = i s S g B i n a r y O p ( p a r e n t E x p r e s s i o n ) ;
37                                         i f ( b i n a r y O p e r a t o r != NULL)
38                                               {
39                                            // f i g u r e o u t i f t h e ∗ v a r i s on t h e l h s o r t h e r h s
40                                                  i f ( binaryOperator−               >g e t l h s o p e r a n d ( ) == ∗ v a r )
41                                                        {
42                                                             binaryOperator−            >s e t l h s o p e r a n d ( g l o b a l V a r i a b l e R e f e r e n c e ) ;
43                                                        }
44                                                      else
45                                                        {
46                                                             a s s e r t ( binaryOperator−          >g e t r h s o p e r a n d ( ) == ∗ v a r ) ;
47                                                             binaryOperator−            >s e t r h s o p e r a n d ( g l o b a l V a r i a b l e R e f e r e n c e ) ;
48                                                        }
49                                               }
50                                             else
51                                               {
52                                            // i g n o r e t h e s e c a s e s f o r now !
53                                                  switch ( parentExpression−                    >v a r i a n t T ( ) )
54                                                        {
55                                                      // Where t h e v a r i a b l e a p p e r s i n t h e f u n c t i o n argument l i s t t h e p a r e n t i s a S g E x p r L i s t E x p
56                                                             c a s e V SgExprListExp :
57                                                                   {
58                                                                       p r i n t f ( ” S o r r y n o t i m p l e m e n t e d , c a s e o f g l o b a l v a r i a b l e i n f u n c t i o n argument l i s t   ...   \n ” ) ;
59                                                                       assert ( false );
60                                                                       break ;
61                                                                   }
62                                                             case V S g I n i t i a l i z e r :
63                                                             c a s e V SgRefExp :
64                                                             c a s e V SgVarArgOp :
65                                                             default :
66                                                                   {
67                                                                       p r i n t f (” Error : d e f a u l t reached in switch
     p a r e n t E x p r e s s i o n = %p = %s \n ” , p a r e n t E x p r e s s i o n , p a r e n t E x p r e s s i o n −       >c l a s s n a m e ( ) . c s t r ( ) ) ;
68                                                                       assert ( false );
69                                                                   }
70                                                        }
71                                               }
72                                     }
73                            }
74                  }
75        }
76
77   #d e f i n e OUTPUT NAMES OF GLOBAL VARIABLES           0
78   #d e f i n e OUTPUT NAMES OF GLOBAL VARIABLE REFERENCES 0
79
80   void




      Figure 31.27: Example source code shows repackaging of global variables to a struct (part 4).
     212                                                                                                                   CHAPTER 31. AST CONSTRUCTION




 1   transformGlobalVariablesToUseStruct                                     (   SgSourceFile             ∗file        )
 2      {
 3        a s s e r t ( f i l e != NULL ) ;
 4
 5       //     T h e s e a r e t h e g l o b a l v a r i a b l e s i n t h e i n p u t program ( p r o v i d e d a s h e l p f u l i n f o r m a t i o n )
 6              R o s e S T L C o n t a i n e r <S g I n i t i a l i z e d N a m e ∗> g l o b a l V a r i a b l e s = b u i l d L i s t O f G l o b a l V a r i a b l e s ( f i l e ) ;
 7
 8   #i f  OUTPUT NAMES OF GLOBAL VARIABLES
 9            p r i n t f (” g l o b a l v a r i a b l e s ( declared in g l o b a l scope ) :                             \n ” ) ;
10            f o r ( R o s e S T L C o n t a i n e r <S g I n i t i a l i z e d N a m e ∗ > : : i t e r a t o r           var = g l o b a l V a r i a b l e s . begin ( ) ;   var   !=   g l o b a l V a r i a b l e s . end ( ) ;   v a r++)
11                  {
12                      p r i n t f (”    %s \n ” , ( ∗ v a r)−>g e t n a m e ( ) . s t r ( ) ) ;
13                  }
14            p r i n t f (”\n ” ) ;
15   #e n d i f
16
17       //     get the g l o b a l scope within the f i r s t f i l e ( c u r r e n t l y                                 ignoring          all   other   files )
18              SgGlobal ∗ globalSc ope = f i l e −             >g e t g l o b a l S c o p e ( ) ;
19              a s s e r t ( g l o b a l S c o p e != NULL ) ;
20
21       //     Build the c l a s s d e c l a r a t i o n
22              SgClassDeclaration ∗ c l a s s D e c l a r a t i o n = buildClassDeclarationAndDefinition (” AMPI globals t ” , globalScope ) ;
23
24       // Put t h e g l o b a l v a r i a b l e s i n t o t h e c l a s s
25          SgVariableSymbol ∗ globalClassVariableSymbol = p u t G l o b a l V a r i a b l e s I n t o C l a s s ( g l o b a l V a r i a b l e s , c l a s s D e c l a r a t i o n ) ;
26
27       //     T h e i r a s s o c i a t e d s y m b o l s w i l l be l o c a t e d w i t h i n t h e p r o j e c t ’ s AST
28       //     ( where they o c c u r i n v a r i a b l e r e f e r e n c e e x p r e s s i o n s ) .
29              R o s e S T L C o n t a i n e r <SgVarRefExp∗> v a r i a b l e R e f e r e n c e L i s t = b u i l d L i s t O f V a r i a b l e R e f e r e n c e E x p r e s s i o n s U s i n g G l o b a l V a r i a b l e s ( f i l e ) ;
30
31   #i f  OUTPUT NAMES OF GLOBAL VARIABLE REFERENCES
32            p r i n t f ( ” g l o b a l v a r i a b l e s a p p e a r i n g i n t h e a p p l i c a t i o n : \n ” ) ;
33            f o r ( R o s e S T L C o n t a i n e r <SgVarRefExp ∗ > : : i t e r a t o r v a r = v a r i a b l e R e f e r e n c e L i s t . b e g i n ( ) ;                 var   !=   v a r i a b l e R e f e r e n c e L i s t . end ( ) ;   var
34                  {
35                      p r i n t f (”     %s \n ” , ( ∗ v a r)−> g e t s y m b o l ()−> g e t d e c l a r a t i o n ()−> g e t n a m e ( ) . s t r ( ) ) ;
36                  }
37            p r i n t f (”\n ” ) ;
38   #e n d i f
39
40       //                                                                                                                                           −
                Fixup a l l r e f e r e n c e s t o g l o b a l v a r i a b l e t o a c c e s s t h e v a r i a b l e through t h e c l a s s ( ” x” − > ” AMPI globals . x ” )
41              fixupReferencesToGlobalVariables ( variableReferenceList , globalClassVariableSymbol ) ;
42          }
43
44
45   void
46   transformGlobalVariablesToUseStruct ( SgProject ∗ project )
47      {
48     // C a l l t h e t r a n s f o r m a t i o n o f e a c h f i l e ( t h e r e a r e m u l t i p l e S g F i l e
49     // o b j e c t s when m u l t i p l e f i l e s a r e s p e c f i e d on t h e command l i n e ! ) .
50        a s s e r t ( p r o j e c t != NULL ) ;
51
52              c o n s t S g F i l e P t r L i s t& f i l e L i s t = p r o j e c t − g e t f i l e L i s t ( ) ;
                                                                                               >
53              SgFilePtrList : : c o n s t i t e r a t o r f i l e = f i l e L i s t . begin ( ) ;
54              w h i l e ( f i l e != f i l e L i s t . end ( ) )
55                   {
56                        t r a n s f o r m G l o b a l V a r i a b l e s T o U s e S t r u c t ( i s S g S o u r c e F i l e (∗ f i l e ) ) ;
57                        f i l e ++;
58                   }
59          }
60
61   // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
62   //                         MAIN PROGRAM
63   // ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
64   int
65   main ( i n t a r g c , c h a r ∗ a r g v [ ] )
66       {
67      // B u i l d t h e AST u s e d by ROSE
68         SgProject ∗ p r o j e c t = f r o n t e n d ( argc , argv ) ;
69         a s s e r t ( p r o j e c t != NULL ) ;
70
71       //     transform a p p l i c a t i o n as r e q u i r e d
72              transformGlobalVariablesToUseStruct ( project ) ;
73
74       // Code g e n e r a t i o n p h a s e ( w r i t e               o u t new       application            ” r o s e <i n p u t       file    name>”)
75          r e t u r n backend ( p r o j e c t ) ;
76        }




       Figure 31.28: Example source code shows repackaging of global variables to a struct (part 5).
     31.6. CREATING A ’STRUCT’ FOR GLOBAL VARIABLES                                                213




 1   int x ;
 2   int y ;
 3   long z ;
 4   float pressure ;
 5
 6   i n t main ( )
 7        {
 8          int a = 0;
 9          int b = 0;
10          fl oa t density = 1.0;
11
12            x++;
13            b++;
14
15            x = a + y;
16
17            return 0;
18        }



              Figure 31.29: Example source code used as input to translator adding new function.




 1
 2   s t r u c t AMPI globals t
 3   {
 4       int x ;
 5       int y ;
 6       long z ;
 7       float pressure ;
 8   }
 9   ;
10   s t r u c t A M P I g l o b a l s t A MP I g l o b a l s ;
11
12   i n t main ( )
13   {
14       int a = 0;
15       int b = 0;
16       f l oa t density = 1.0;
17       A M PI gl oba ls . x++;
18       b++;
19       A M PI gl oba ls . x = ( a + A M P I g l o b a l s . y ) ;
20       return 0;
21   }



                           Figure 31.30: Output of input to translator adding new function.
214   CHAPTER 31. AST CONSTRUCTION
Chapter 32

Parser Building Blocks

It is often needed to write a small parser to parse customized source code annotations in forms
of C/C++ pragmas or Fortran comments. The parser is responsible for recognizing keywords,
variables, expressions in the annotations and storing the recognized information in AST, often
in a form of persistent AstAttribute. ROSE provides a set of helper functions which can be used
to create such a simple parser using recursive descent parsing1 . These functions are collected in
the namespace named AstFromString, documented under the Namespace List of ROSE’s online
Doxygen web references.
    A suggested workflow to build your own pragma (or comment) parser is:

   • input: define the grammar of your pragma, including keywords, directives and optional
     clauses. Borrowing grammars of OpenMP is a good idea.

   • output: define your own AstAttribute in order to store any possible pragma information
     generated by your parser. The attribute data structure should store the AST subtrees
     generated by a parser. The Attribute itself should be attached to relevant statement node
     (pragma declarations or others) in the original AST.

   • parser: write your recursive descent pragma parser by leveraging the functions defined in
     rose sourcetree/src/frontend/SageIII/astFromString/AstFromString.h. The parsing results
     will be used to generate your attribute which should be attached to your pragma declaration
     statement (or a following statement for a Fortran comment).

   • use of parsing results: in another phase, write your traversal to recognize the attached
     attribute to do whatever you plan to do with the pragma information.

   A full example is provided under rose/projects/pragmaParsing to demonstrate the entire
workflow of using parser building blocks (helper functions within AstFromString) to create a
parser to parse user-defined pragmas. We will use this example in this tutorial.
   1 Description of basic recursive descent parsing techniques can be found at http://en.wikipedia.org/wiki/

Recursive descent parser


                                                    215
216                                            CHAPTER 32. PARSER BUILDING BLOCKS

32.1      Grammar Examples
The first step of building a parser is to formally define the grammar of input strings. The online
Doxygen web reference lists helper functions from the AstFromString namespace, with detailed
information about the underneath grammars they try to recognize. These grammar can be used
as example about how to prepare grammars.
    For example bool afs match cast expression () follows the grammar like:
cast expression : ’(’ type name ’)’ cast expression | unary expression, which means a cast ex-
pression is either a unary expression or another cast expression prepended with a type name
enclosed in a pair of parenthesis. Note that the grammar has a rule with a right recursion here
(cast expression : ’(’ type name ’)’ cast expression). The grammars should try to avoid left
recursion (e.g., result : result something else ), which may leads infinite recursive calls in your
parser. Again, a helper function in AstFromString often implements a grammar. Please take a
look at some of them to see how grammars are written to facilitate building recursive descent
parsers.
    The pragma in the pragmaParsing project has the following grammar (documented in rose/pro-
jects/pragmaParsing/hcpragma.h):

---------- grammar begin -----------
% ’string’ means literal string to be matched
% | means alternation
hcc_pragma = ’#pragma’ hc_simple_part | hc_cuda_part

hc_simple_part = ’hc’ ’entry’| ’suspendable’ | ’entry suspendable’ | ’suspendable entry’

hc_cuda_part = ’CUDA’ kernel_part| place_part

kernel_part = ’kernel’

% place could be an expression
% the grammar uses assignment_expression instead of expression to disallow comma expressions
% (list of expressions connected with comma) e.g. exp1, exp2 will be parsed to be (ex1, exp2)
% otherwise.
%
place_part = assignment_expression autodim_part | dim_part

% autodim(<dim1>[, <dim2>, <dim3>, <shared_size>])
% [ ] means optional
% , means ’,’ to be simple
% assignment_expression is used to avoid parsing exp1, exp2, exp3 to be one single comma
% expression ((exp1,exp2),exp3)
autodim_part = ’autodim’ ’(’ assignment_expression [, assignment_expression
              [, assignment_expression [, assignment_expression ] ] ] ’)’

% dim(blocksPerGrid, threadsPerBlock[, shared_size])
dim_part = ’dim’ ’(’ assignment_expression , assignment_expression ,
32.2. ASTATTRIBUTE TO STORE RESULTS                                                            217

              [ , assignment_expression ]          ’)’


The example grammar allows a list of expressions inside a pragma. A tricky part here is that
C allows single comma expression like ((exp1,exp2),exp3). We use assignment expression to
avoid parsing exp1, exp2, exp3 to be one such single comma expression. The point here is
that the terms in the grammar have to be accurately mapped to formal C grammar terms.
Some familiarity of formal C grammar terms, as shown at http://www.antlr.org/grammar/
1153358328744/C.g, is required since helper functions have names matching the formal terms
in C grammar. The assignment expressions, not expressions, are what we care about in this
particular simple grammar.


32.2      AstAttribute to Store results
Once the grammar is defined with terms matching helper functions of AstFromString, a data
structure is needed to store the results of parsing. It is recommended to create your data
structure by inheriting AstAttribute, which can be attached to any AST nodes with location
information.
    As in the pragmaParsing project, we define a few classes as the following:

class HC_PragmaAttribute: public AstAttribute
{
    public:
        SgNode * node;
            enum hcpragma_enum pragma_type;
... };

class HC_CUDA_PragmaAttribute: public HC_PragmaAttribute
{
  public:
    SgExpression* place_exp;
...
};
class HC_CUDA_autodim_PragmaAttribute: public HC_CUDA_PragmaAttribute
{
  public:
    SgExpression* dim1_exp;
    SgExpression* dim2_exp;
    SgExpression* dim3_exp;
    SgExpression* shared_size_exp;
...
};


The point is that the class is inherited from AstAttribute and it contains fields to store all terms
defined in the grammar.
218                                              CHAPTER 32. PARSER BUILDING BLOCKS

32.3         The AstFromString Namespace
AstFromString has a few namespace scope variables, such as:

      • char ∗ c char: this indicates the current position of the input string being parsed.

      • SgNode∗ c sgnode: a SgNode variable storing the current anchor AST node, which servers
        as a start point to find enclosing scopes for resolving identifiers/symbols discovered by a
        parser.

      • SgNode ∗ c parsed node: a SgNode variable storing the root of an AST subtree generated
        by the parser.

    In general, your parser should initialize c char with the first position of an input string to
be parsed. It should also set c sgnode to be the pragma statement when you parse pragma
strings, or the immediate following statement when you parse Fortran comments. The results
often are stored in c parsed node. Your parser should timely check the results and filling your
AstAttribute structure with the AST substrees for identifiers, constants, expressions, etc.
    Helper functions within AstFromString include functions to parse identifiers, types, sub-
strings, expressions. AST pieces will be generated automatically as needed. So users can focus
on building their grammar and parser without doing repetitive chores of parsing common lan-
guage constructs.
    Take bool afs match assignment expression() as an example, this function will try to match
an expression that satisfies a grammar rule like:
assignment expression : lvalue assignment operator assignment expression | conditional expression .
If a successful match is found, the function returns true. In the meantime, the function builds
an AST subtree to represent the matched expression and stores the subtree into the variable
SgNode∗ c sgnode.


32.4         Write your parsers using parser building blocks
rose/src/frontend/SageIII/astFromString/AstFromString.cpp has the implementation of all parser
building blocks (helper functions) for a wide range of grammar rules. They can serve as examples
about how to hand-write additional functions to recognize your own grammars. For example,
to implement a simple grammar rule like type qualifier : ’const’        | ’ volatile ’, we have the
following helper function:

 /*
        type_qualifier
        : ’const’
        | ’volatile’
        ;

*/
  bool afs_match_type_qualifier()
  {
    bool result = false;
32.4. WRITE YOUR PARSERS USING PARSER BUILDING BLOCKS                                             219

      const char* old_char = c_char;
      if (afs_match_substr("const"))
      {
        c_parsed_node = buildConstVolatileModifier (SgConstVolatileModifier::e_const);
        result = true;
      }
      else if (afs_match_substr("volatile"))
      {
        c_parsed_node = buildConstVolatileModifier (SgConstVolatileModifier::e_volatile);
        result = true;
      }
      if (result == false)   c_char = old_char;
      return result;
  }
Please note that the function above tries to undo any side effects if the parsing fails. If successful,
the parsed result will be stored into c parsed node.
   Here is another example with right recursion:
/* Grammar is
     cast_expression
     : ’(’ type_name ’)’ cast_expression
     | unary_expression
     ;

*/
  bool afs_match_cast_expression()
  {
    bool result = false;
    const char* old_char = c_char;

      if (afs_match_unary_expression())
      {
        if (isSgExpression(c_parsed_node))
        result = true;
      }
      else if (afs_match_char(’(’))
      {
        if (afs_match_type_name())
        {
          SgType* t = isSgType(c_parsed_node);
          assert (t!= NULL);
          if (afs_match_char(’)’))
          {
            if (afs_match_cast_expression())
            {
              SgExpression* operand = isSgExpression(c_parsed_node);
220                                          CHAPTER 32. PARSER BUILDING BLOCKS

               c_parsed_node = buildCastExp(operand, t);
               result = true; // must set this!!
             }
             else
             {
               c_char = old_char;
             }
           }
           else
           {
             c_char = old_char;
           }

          }
          else
          {
            c_char = old_char;
            result = false;
          }
      }

      if (result == false)       c_char = old_char;
      return result;
  }


   sourcetree/projects/pragmaParsing/hcpragma.C gives a full example of how to use helper
functions inside your own parsing functions.


32.5        Limitations
Currently, the parser building blocks support C only. Expressions parsing functions are ready
to be used by users. Statement parsing is still ongoing work.
Chapter 33

Handling Comments,
Preprocessor Directives, And
Adding Arbitrary Text to
Generated Code

What To Learn From These Examples Learn how to access existing comments and CPP
directives and modify programs to include new ones. Where such comments can be automated
they can be used to explain transformations or for more complex transformations using other
tools designed to read the generated comments. Also included is how to add arbitrary text to the
generated code (often useful for embedded system programming to support back-end compiler
specific functionality).
    This chapter deals with comments and preprocessor directives. These are often dropped
from compiler infrastructures and ignored by make tools. ROSE takes great care to preserve all
comments and preprocessor directives. Where they exist in the input code we save them (note
that EDG drops them from their AST) and weave them back into the AST.
    Note that #pragma is not a CPP directive and is part of the C and C++ grammar, thus
represented explicitly in the AST (see SgPragmaDeclaration).



33.1      How to Access Comments and Preprocessor Direc-
          tives
Comments and CPP directives are treated identically within ROSE and are saved as special
preprocessor attributes to IR nodes within the AST. Not all IR nodes can have these specific
type of attributes, only SgLocatedNodes can be associated with such preprocessor attributes.
The more general persistent attribute mechanism within ROSE is separate from this preprocessor
attribute mechanism and is available on a wider selection of IR nodes.

                                              221
222CHAPTER 33. HANDLING COMMENTS, PREPROCESSOR DIRECTIVES, AND ADDING ARBITRARY TEXT

33.1.1     Source Code Showing How to Access Comments and Prepro-
           cessor Directives
Figure 33.1 shows an example translator which access the comments and preprocessor directives
on each statement. Note that in this example the AST is traversed twice, first header files
are ignored and then the full AST (including header files) are traversed (generated additional
comments).
   The input code is shown in figure 33.2, the output of this code is shown in figure 33.3 for
the source file only. Figure 33.4 shows the same input code processed to output comments and
preprocessor directives assembled from the source file and all header files.


33.1.2     Input to example showing how to access comments and CPP
           directives
Figure 33.2 shows the example input used for demonstration of how to collect comments and
CPP directives.


33.1.3     Comments and CPP Directives collected from source file (skip-
           ping headers)
Figure 33.3 shows the results from the collection of comments and CPP directives within the
input source file only (without -rose:collectAllCommentsAndDirectives).


33.1.4     Comments and CPP Directives collected from source file and
           all header files
Figure 33.4 shows the results from the collection of comments and CPP directives within the
input source file and all headers (with -rose:collectAllCommentsAndDirectives).


33.2      Collecting #define C Preprocessor Directives
This example shows how to collect the #define directives as a list for later processing.


33.2.1     Source Code Showing How to Collect #define Directives
Figure 33.5 shows an example translator which access the comments and preprocessor directives
on each statement. Note that in this example the AST is traversed twice, first header files
are ignored and then the full AST (including header files) are traversed (generated additional
comments).
   The input code is shown in figure 33.6, the output of this code is shown in Figure 33.7 shows
the same input code processed to output comments and preprocessor directives assembled from
the source file and all header files.
     33.2. COLLECTING #DEFINE C PREPROCESSOR DIRECTIVES                                                                                                     223


 1   // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   u s i n g namespace s t d ;
 6
 7   // C l a s s d e c l a r a t i o n
 8   c l a s s v i s i t o r T r a v e r s a l : public AstSimpleProcessing
 9         {
10            public :
11                      v i r t u a l v o i d v i s i t ( SgNode∗ n ) ;
12         };
13
14   v o i d v i s i t o r T r a v e r s a l : : v i s i t ( SgNode∗ n )
15         {
16      // On e a c h node l o o k f o r any comments o f CPP d i r e c t i v e s
17           SgLocatedNode ∗ l o c a t e d N o d e = i s S g L o c a t e d N o d e ( n ) ;
18           i f ( l o c a t e d N o d e != NULL)
19                 {
20                      A t t a c h e d P r e p r o c e s s i n g I n f o T y p e ∗ comments = l o c a t e d N o d e − e t A t t a c h e d P r e p r o c e s s i n g I n f o ( ) ;
                                                                                                                      >g
21
22                     if    ( comments != NULL)
23                           {
24                              p r i n t f ( ” Found a t t a c h e d comments ( t o IR node a t %p o f t y p e : %s ) : \n ” , l o c a t e d N o d e , l o c a t e d N o d e − l a s s n a m e ( )  >c
25                              int counter = 0;
26                              AttachedPreprocessingInfoType : : i t e r a t o r i ;
27                                                                 >b
                                f o r ( i = comments− e g i n ( ) ; i != comments−                                   >end ( ) ; i ++)
28                                    {
29                                        p r i n t f (”                        Attached Comment #%d i n f i l e %s ( r e l a t i v e P o s i t i o n=%s ) : c l a s s i f i c a t i o n %s : \ n%s \n ” ,
30                                                  c o u n t e r ++,(∗ i )−> g e t f i l e i n f o ()−> g e t f i l e n a m e S t r i n g ( ) . c s t r ( ) ,
31                                                  ( ( ∗ i )−> g e t R e l a t i v e P o s i t i o n ( ) == P r e p r o c e s s i n g I n f o : : b e f o r e ) ? ” b e f o r e ” : ” a f t e r ” ,
32                                                  P r e p r o c e s s i n g I n f o : : d i r e c t i v e T y p e N a m e ( ( ∗ i )−>g e t T y p e O f D i r e c t i v e ( ) ) . c s t r ( ) ,
33                                                  ( ∗ i )−> g e t S t r i n g ( ) . c s t r ( ) ) ;
34                                    }
35                           }
36                          else
37                           {
38                              p r i n t f ( ” No a t t a c h e d comments ( a t %p o f t y p e : %s ) : \n ” , l o c a t e d N o d e , l o c a t e d N o d e − a g e c l a s s n a m e ( ) ) ;
                                                                                                                                                                                        >s
39                           }
40                 }
41        }
42
43   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
44        {
45       // B u i l d t h e AST u s e d by ROSE
46          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
47
48      // B u i l d t h e t r a v e r s a l o b j e c t
49         v i s i t o r T r a v e r s a l exampleTraversal ;
50
51      //    C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t node o f t h e AST
52      //    T r a v e r s e a l l h e a d e r f i l e s and s o u r c e f i l e ( t h e −r o s e : c o l l e c t A l l C o m m e n t s A n d D i r e c t i v e s
53      //    commandline o p t i o n c o n t r o l s i f comments and CPP d i r e c t i v e s a r e s e p a r a t e l y e x t r a c t e d
54      //    from h e a d e r f i l e s ) .
55      //    exampleTraversal . t r a v e r s e ( project , preorder ) ;
56            exampleTraversal . t r a v e r s e I n p u t F i l e s ( project , preorder ) ;
57
58            return 0;
59        }



                            Figure 33.1: Example source code showing how to access comments.
     224CHAPTER 33. HANDLING COMMENTS, PREPROCESSOR DIRECTIVES, AND ADDING ARBITRARY TEXT


 1
 2   // #i n c l u d e <s t d i o . h>
 3
 4   #d e f i n e SOURCE CODE BEFORE INCLUDE A
 5   #d e f i n e SOURCE CODE BEFORE INCLUDE B
 6   #i n c l u d e <i n p u t C o d e c o l l e c t C o m m e n t s . h>
 7   #d e f i n e SOURCE CODE AFTER INCLUDE A
 8   #d e f i n e SOURCE CODE AFTER INCLUDE B
 9
10   // main program : c o l l e c t C o m m e n t s i n p u t t e s t c o d e
11   i n t main ( )
12        {
13          return 0;
14        }



     Figure 33.2: Example source code used as input to collection of comments and CPP directives.



 1   No a t t a c h e d comments ( a t 0 x 2b0 cd7 85 301 0 o f t y p e : S g G l o b a l ) :
 2   Found a t t a c h e d comments ( t o IR node a t 0 x 2 b 0 c d 7 9 5 c b e 0 o f t y p e : S g F u n c t i o n D e c l a r a t i o n ) :
 3                                                                                                                                            −d
                      Attached Comment #0 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /am
 4   // #i n c l u d e <s t d i o . h>
 5
 6                                                                                                                             −d
                    Attached Comment #1 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /am
 7   #d e f i n e SOURCE CODE BEFORE INCLUDE A
 8
 9                                                                                                                             −d
                    Attached Comment #2 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /am
10   #d e f i n e SOURCE CODE BEFORE INCLUDE B
11
12                                                                                                                                −d
                       Attached Comment #3 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /am
13   #i n c l u d e <i n p u t C o d e c o l l e c t C o m m e n t s . h>
14
15                                                                                                                             −d
                    Attached Comment #4 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /am
16   #d e f i n e SOURCE CODE AFTER INCLUDE A
17
18                                                                                                                             −d
                    Attached Comment #5 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /am
19   #d e f i n e SOURCE CODE AFTER INCLUDE B
20
21                                                                                                                        −d
               Attached Comment #6 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /am
22   // main program : c o l l e c t C o m m e n t s i n p u t t e s t c o d e
23
24   No    attached        comments        ( at    0 x2 b0cd 7a 17 9 00 o f t y p e : S g F u n c t i o n P a r a m e t e r L i s t ) :
25   No    attached        comments        ( at    0 x2b0cd7c21010 o f type : S g F u n c t i o n D e f i n i t i o n ) :
26   No    attached        comments        ( at    0 x2b0cd7c6a010 o f type : SgBasicBlock ) :
27   No    attached        comments        ( at    0 x f 8 5 e 0 4 0 o f t y p e : SgReturnStmt ) :
28   No    attached        comments        ( at    0 xf873920 o f type : SgIntVal ) :



     Figure 33.3: Output from collection of comments and CPP directives on the input source file
     only.



     33.2.2            Input to example showing how to access comments and CPP
                       directives

     Figure 33.6 shows the example input used for demonstration of how to collect comments and
     CPP directives.
     33.3. AUTOMATED GENERATION OF COMMENTS                                                                                       225


 1   No a t t a c h e d comments ( a t 0 x 2 b 8 f 7 1 9 e 5 0 1 0 o f t y p e : S g G l o b a l ) :
 2   Found a t t a c h e d comments ( t o IR node a t 0 x 2 b 8 f 7 1 a c d b e 0 o f t y p e : S g F u n c t i o n D e c l a r a t i o n ) :
 3                                                                                                                                            −d
                      Attached Comment #0 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t
 4   // #i n c l u d e <s t d i o . h>
 5
 6                                                                                                                             −d
                    Attached Comment #1 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t
 7   #d e f i n e SOURCE CODE BEFORE INCLUDE A
 8
 9                                                                                                                             −d
                    Attached Comment #2 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t
10   #d e f i n e SOURCE CODE BEFORE INCLUDE B
11
12                                                                                                                                −d
                       Attached Comment #3 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t
13   #i n c l u d e <i n p u t C o d e c o l l e c t C o m m e n t s . h>
14
15                                                                                                                             −d
                    Attached Comment #4 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t
16   #d e f i n e SOURCE CODE AFTER INCLUDE A
17
18                                                                                                                             −d
                    Attached Comment #5 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t
19   #d e f i n e SOURCE CODE AFTER INCLUDE B
20
21                                                                                                                        −d
               Attached Comment #6 i n f i l e / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t
22   // main program : c o l l e c t C o m m e n t s i n p u t t e s t c o d e
23
24   No   attached     comments     ( at   0 x2b8f71b88900 o f type : SgFunctionParameterList ) :
25   No   attached     comments     ( at   0 x2b8f71d92010 o f type : S g F u n c t i o n D e f i n i t i o n ) :
26   No   attached     comments     ( at   0 x2b8f71ddb010 o f type : SgBasicBlock ) :
27   No   attached     comments     ( at   0 x e 7 2 9 1 1 0 o f t y p e : SgReturnStmt ) :
28   No   attached     comments     ( at   0 xe73e9f0 o f type : SgIntVal ) :



     Figure 33.4: Output from collection of comments and CPP directives on the input source file
     and all header files.


     33.2.3         Comments and CPP Directives collected from source file and
                    all header files
     Figure 33.7 shows the results from the collection of comments and CPP directives within the
     input source file and all headers (with -rose:collectAllCommentsAndDirectives).


     33.3          Automated Generation of Comments
     Figure 33.8 shows an example of how to introduce comments into the AST which will then show
     up in the generated source code. The purpose for this is generally to add comments to where
     transformations are introduced. If the code is read by the use the generated comments can be
     useful in identifying, marking, and/or explaining the transformation.
         This chapter presents an example translator which just introduces a comment at the top of
     each function. The comment includes the name of the function and indicates that the comment
     is automatically generated.
         Where appropriate such techniques could be used to automate the generation of documen-
     tation templates in source code that would be further filled in by the used. In this case the
     automatically generated templates would be put into the generated source code and a patch
     formed between the generated source and the original source. The patch could be easily in-
     spected and applied to the original source code to place the documentation templates into the
     original source. The skeleton of the documentation in the source code could been be filled in
226CHAPTER 33. HANDLING COMMENTS, PREPROCESSOR DIRECTIVES, AND ADDING ARBITRARY TEXT

by the use. The template would have all relevant information obtained by analysis (function
parameters, system functions used, security information, side-effects, anything that could come
from an analysis of the source code using ROSE).

33.3.1     Source Code Showing Automated Comment Generation
Figure 33.8 shows an example translator which calls the mechanism to add a comment to the
IR node representing a function declaration (SgFunctionDeclaration).
   The input code is shown in figure 33.9, the output of this code is shown in figure 33.10.

33.3.2     Input to Automated Addition of Comments
Figure 33.9 shows the example input used for demonstration of an automated commenting.

33.3.3     Final Code After Automatically Adding Comments
Figure 33.10 shows the results from the addition of comments to the generated source code.


33.4      Addition of Arbitrary Text to Unparsed Code Gen-
          eration
This section is different from the comment generation (section 33.3) because it is more flexible
and does not introduce any formatting. It also does not use the same internal mechanism,
this mechanism supports the addition of new strings or the replacement of the IR node (where
the string is attached) with the new string. It is fundamentally lower level and a more powerful
mechanism to support generation of tailored output where more than comments, CPP directives,
or AST transformation are required. It is also much more dangerous to use.
    This mechanism is expected to be used rarely and sparingly since no analysis of the AST is
likely to leverage this mechanism and search for code that introduced as a transformation here.
Code introduced using this mechanism is for the most part unanalyzable since it would have to
be reparsed in the context of the location in the AST were it is attached. (Technically this is
possible and is the subject of the existing ROSE AST Rewrite mechanism, but that is a different
subject).
    Figure 33.11 shows an example of how to introduce arbitrary text into the AST for output
by the unparser which will then show up in the generated source code. The purpose for this
is generally to add backend compiler or tool specific code generation which don’t map to any
formal language constructs and so cannot be represented in the AST. However, since most tools
that require specialized annotations read them as comments, the mechanism in the previous
section 33.3 may be more appropriate. It is because this is not always that case that we have
provide this more general mechanism (often useful for embedded system compilers).

33.4.1     Source Code Showing Automated Arbitrary Text Generation
Figure 33.11 shows an example translator which calls the mechanism to add a arbitrary text to
the IR node representing a function declaration (SgFunctionDeclaration).
33.4. ADDITION OF ARBITRARY TEXT TO UNPARSED CODE GENERATION                                227

   The input code is shown in figure 33.12, the output of this code is shown in figure 33.13.

33.4.2     Input to Automated Addition of Arbitrary Text
Figure 33.12 shows the example input used for demonstration of the automated introduction of
text via the unparser.

33.4.3     Final Code After Automatically Adding Arbitrary Text
Figure 33.13 shows the results from the addition of arbitrary text to the generated source code.
     228CHAPTER 33. HANDLING COMMENTS, PREPROCESSOR DIRECTIVES, AND ADDING ARBITRARY TEXT


 1   // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   u s i n g namespace s t d ;
 6
 7   // B u i l d a s y n t h e s i z e d a t t r i b u t e f o r t h e t r e e t r a v e r s a l
 8   class SynthesizedAttribute
 9      {
10         public :
11              // L i s t o f #d e f i n e d i r e c t i v e s ( s a v e t h e P r e p r o c e s s i n g I n f o o b j e c t s
12              // s o t h a t we have a l l t h e s o u r c e c o d e p o s i t i o n i n f o r m a t i o n ) .
13                 l i s t <P r e p r o c e s s i n g I n f o ∗> a c c u m u l a t e d L i s t ;
14
15                     void display () const ;
16        };
17
18   void
19   SynthesizedAttribute : : display () const
20      {
21        l i s t <P r e p r o c e s s i n g I n f o ∗ > : : c o n s t i t e r a t o r i = a c c u m u l a t e d L i s t . b e g i n ( ) ;
22        w h i l e ( i != a c c u m u l a t e d L i s t . end ( ) )
23              {
24                 p r i n t f ( ”CPP d e f i n e d i r e c t i v e = %s \n ” , ( ∗ i )−> g e t S t r i n g ( ) . c s t r ( ) ) ;
25                 i ++;
26              }
27      }
28
29   class        visitorTraversal             :   p u b l i c AstBottomUpProcessing<S y n t h e s i z e d A t t r i b u t e >
30      {
31             public :
32               // v i r t u a l     f u n c t i o n must be d e f i n e d
33                  virtual           SynthesizedAttribute evaluateSynthesizedAttribute (
34                                              SgNode∗ n , S y n t h e s i z e d A t t r i b u t e s L i s t c h i l d A t t r i b u t e s   );
35        };
36
37   SynthesizedAttribute
38   v i s i t o r T r a v e r s a l : : e v a l u a t e S y n t h e s i z e d A t t r i b u t e ( SgNode∗ n ,   SynthesizedAttributesList                      childAttributes )
39         {
40              SynthesizedAttribute localResult ;
41
42      // p r i n t f ( ” I n e v a l u a t e S y n t h e s i z e d A t t r i b u t e ( n = %p = %s ) \n ” , n , n− l a s s n a m e ( ) . c s t r ( ) ) ;
                                                                                                                    >c
43
44      // B u i l d t h e l i s t from c h i l d r e n ( i n r e v e r s e o r d e r t o p r e s e r v e t h e f i n a l o r d e r i n g )
45         f o r ( S y n t h e s i z e d A t t r i b u t e s L i s t : : r e v e r s e i t e r a t o r c h i l d = c h i l d A t t r i b u t e s . r b e g i n ( ) ; c h i l d != c h i l d A t t r i b u t
46              {
47                  l o c a l R e s u l t . accumulatedList . s p l i c e ( l o c a l R e s u l t . accumulatedList . begin ( ) , child − ccumulatedList ) ;        >a
48              }
49
50      // Add i n t h e i n f o r m a t i o n from t h e c u r r e n t node
51         SgLocatedNode ∗ l o c a t e d N o d e = i s S g L o c a t e d N o d e ( n ) ;
52         i f ( l o c a t e d N o d e != NULL)
53             {
54                 AttachedPreprocessingInfoType ∗ commentsAndDirectives = locatedNode− e t A t t a c h e d P r e p r o c e s s i n g I n f o ( ) ;
                                                                                         >g
55
56                     if    ( c o m m e n t s A n d D i r e c t i v e s != NULL)
57                           {
58                          // p r i n t f ( ” Found a t t a c h e d comments ( t o IR node a t %p o f t y p e : %s ) : \n ” , l o c a t e d N o d e , l o c a t e d N o d e −>c
59                          // i n t c o u n t e r = 0 ;
60
61                          // Use a r e v e r s e i t e r a t o r s o t h a t we p r e s e r v e t h e o r d e r when u s i n g p u s h f r o n t t o add e a c h d i r e c t i v
62                             AttachedPreprocessingInfoType : : r e v e r s e i t e r a t o r i ;
63                                                                                   >r
                               f o r ( i = commentsAndDir ectiv es− b e g i n ( ) ; i != commentsAndDir ect ives− e n d ( ) ; i ++)                         >r
64                                  {
65                                 // The d i f f e r e n t c l a s s i f i c a t i o n s o f comments and d i r e c t i v e s a r e i n ROSE/ s r c / f r o n t e n d / S a g e I I I /
66                                     i f ( ( ∗ i )−>g e t T y p e O f D i r e c t i v e ( ) == P r e p r o c e s s i n g I n f o : : C p r e p r o c e s s o r D e f i n e D e c l a r a t i o n )
67                                         {
68   #i f 0
69                                                 p r i n t f (”                        Attached Comment #%d i n f i l e %s ( r e l a t i v e P o s i t i o n=%s ) : c l a s s i f i c a t
70                                                       c o u n t e r ++,(∗ i )−> g e t f i l e i n f o ()−> g e t f i l e n a m e S t r i n g ( ) . c s t r ( ) ,
71                                                       ( ( ∗ i )−> g e t R e l a t i v e P o s i t i o n ( ) == P r e p r o c e s s i n g I n f o : : b e f o r e ) ? ” b e f o r e ” : ” a f t e r ” ,
72                                                       P r e p r o c e s s i n g I n f o : : d i r e c t i v e T y p e N a m e ( ( ∗ i )−>g e t T y p e O f D i r e c t i v e ( ) ) . c s t r ( ) ,
73                                                       ( ∗ i )−> g e t S t r i n g ( ) . c s t r ( ) ) ;
74   #e n d i f
75                                           // u s e p u s h f r o n t ( ) t o end up w i t h s o u r c e o r d e r i n g o f                final      list     of   directives
76                                              l o c a l R e s u l t . accumulatedList . p u s h f r o n t (∗ i ) ;
77                                            }
78                                    }
79                           }
80                 }
81
82      // p r i n t f ( ” l o c a l R e s u l t a f t e r a d d i n g c u r r e n t node i n f o \n ” ) ;
83      // l o c a l R e s u l t . d i s p l a y ( ) ;
     33.4. ADDITION OF ARBITRARY TEXT TO UNPARSED CODE GENERATION                                                                229




 1
 2   #d e f i n e JUST A MACRO j u s t a m a c r o
 3
 4   #d e f i n e ANOTHER MACRO a n o t h e r m a c r o



     Figure 33.6: Example source code used as input to collection of comments and CPP directives.




 1   CPP d e f i n e   d i r e c t i v e = #d e f i n e max ( a , b ) ( ( a ) > ( b ) ? ( a ) : ( b ) )
 2
 3   CPP d e f i n e   d i r e c t i v e = #d e f i n e maxint ( a , b ) ( { i n t   a = (a) ,    b = (b ) ;   a >   b ?   a :   b ; })
 4
 5   CPP d e f i n e   d i r e c t i v e = #d e f i n e SOURCE CODE BEFORE INCLUDE A
 6
 7   CPP d e f i n e   d i r e c t i v e = #d e f i n e SOURCE CODE BEFORE INCLUDE B
 8
 9   CPP d e f i n e   d i r e c t i v e = #d e f i n e SOURCE CODE AFTER INCLUDE A
10
11   CPP d e f i n e   d i r e c t i v e = #d e f i n e SOURCE CODE AFTER INCLUDE B



     Figure 33.7: Output from collection of comments and CPP directives on the input source file
     and all header files.
     230CHAPTER 33. HANDLING COMMENTS, PREPROCESSOR DIRECTIVES, AND ADDING ARBITRARY TEXT




 1   // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   u s i n g namespace s t d ;
 6
 7   class      visitorTraversal            :   public AstSimpleProcessing
 8      {
 9             public :
10                  v i r t u a l void       v i s i t ( SgNode∗ n ) ;
11        };
12
13   v o i d v i s i t o r T r a v e r s a l : : v i s i t ( SgNode∗ n )
14         {
15           SgFunctionDeclaration ∗ functionDeclaration = isSgFunctionDeclaration (n ) ;
16           i f ( f u n c t i o n D e c l a r a t i o n != NULL)
17                 {
18                      s t r i n g comment = s t r i n g ( ” Auto−comment f u n c t i o n name : ” ) +
19                                                       functionDeclaration− >get name ( ) . s t r ( ) +
20                                                       ” i s now a commented f u n c t i o n ” ;
21
22                // Note t h a t t h i s f u n c t i o n w i l l add t h e ”//” o r ”/∗ ∗/” comment s y n t a x a s r e q u i r e d                 f o r C o r C++, o r F o r
23                   S a g e I n t e r f a c e : : attachComment ( f u n c t i o n D e c l a r a t i o n , comment ) ;
24                 }
25
26             SgValueExp ∗ valueExp = i s S g V a l u e E x p ( n ) ;
27             i f ( valueExp != NULL)
28                 {
29                // Check i f t h e r e i s an e x p r e s s i o n t r e e from t h e o r i g i n a l u n f o l d e d e x p r e s s i o n .
30                // T h i s i s a t r i v i a l example o u f t h e o u t p u t o f an a n a l y s i s r e s u l t .
31                    s t r i n g comment = s t r i n g ( ” Auto−comment v a l u e : ” ) +
32                                              >g
                                ( ( valueExp− e t o r i g i n a l E x p r e s s i o n T r e e ( ) != NULL) ?
33                                     ” t h i s I S a c o n s t a n t f o l d e d v a l u e ” : ” t h i s i s NOT a c o n s t a n t f o l d e d v a l u e ” ) ;
34
35                     S a g e I n t e r f a c e : : attachComment ( valueExp , comment ) ;
36                 }
37
38        }
39
40   // T y p i c a l main f u n c t i o n f o r ROSE t r a n s l a t o r
41   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
42        {
43       // B u i l d t h e AST u s e d by ROSE
44          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
45
46      // B u i l d t h e t r a v e r s a l o b j e c t
47         v i s i t o r T r a v e r s a l exampleTraversal ;
48
49      // C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t node o f t h e AST
50         exampleTraversal . t r a v e r s e I n p u t F i l e s ( project , preorder ) ;
51
52             r e t u r n backend ( p r o j e c t ) ;
53        }



                         Figure 33.8: Example source code showing how automate comments.
     33.4. ADDITION OF ARBITRARY TEXT TO UNPARSED CODE GENERATION                                231




 1
 2   int
 3   foo ()
 4       {
 5            int x = 2;
 6            x += 3 + 4 ;
 7            return x ;
 8      }



            Figure 33.9: Example source code used as input to automate generation of comments.




 1   // Auto−comment f u n c t i o n name :   foo   i s now a commented f u n c t i o n
 2
 3   int foo ()
 4   {
 5      int x =
 6   // Auto−comment v a l u e :   this   i s NOT a c o n s t a n t f o l d e d v a l u e
 7   2;
 8      x +=
 9   // Auto−comment v a l u e :   this   i s NOT a c o n s t a n t f o l d e d v a l u e
10   3 +
11   // Auto−comment v a l u e :   this   i s NOT a c o n s t a n t f o l d e d v a l u e
12   4;
13      return x ;
14   }



                 Figure 33.10: Output of input code after automating generation of comments.
     232CHAPTER 33. HANDLING COMMENTS, PREPROCESSOR DIRECTIVES, AND ADDING ARBITRARY TEXT



 1   // Example ROSE T r a n s l a t o r : u s e d w i t h i n ROSE/ t u t o r i a l
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   u s i n g namespace s t d ;
 6
 7   class      visitorTraversal               :   public AstSimpleProcessing
 8      {
 9             public :
10                  v i r t u a l void         v i s i t ( SgNode∗ n ) ;
11        };
12
13   v o i d v i s i t o r T r a v e r s a l : : v i s i t ( SgNode∗ n )
14         {
15           SgFunctionDeclaration ∗ functionDeclaration = isSgFunctionDeclaration (n ) ;
16            i f ( f u n c t i o n D e c l a r a t i o n != NULL)
17                  {
18                // T h i s i s an example o f a XYZ t o o l s p e c i f i c a n n o t a t i o n
19                      s t r i n g c o m p i l e r S p e c i f i c D i r e c t i v e = ”\n# i f XYZ TOOL \n
     \” b u i l t i n \”\ n#e n d i f \n ” ;
20                      S a g e I n t e r f a c e : : addTextForUnparser ( f u n c t i o n D e c l a r a t i o n , c o m p i l e r S p e c i f i c D i r e c t i v e , A s t U n p a r s e A t t r i b u t e :
21                  }
22
23           SgValueExp ∗ valueExp = i s S g V a l u e E x p ( n ) ;
24           i f ( valueExp != NULL)
25                {
26              // Add a backend s p e c i f i c c o m p i l e r d i r e c t i v e
27                    s t r i n g c o m p i l e r S p e c i f i c D i r e c t i v e = ”\n# i f CRAY \n
     c r a y s p e c i f i c a t t r i b u t e \n#e n d i f \n ” ;
28                    S a g e I n t e r f a c e : : addTextForUnparser ( valueExp , c o m p i l e r S p e c i f i c D i r e c t i v e , A s t U n p a r s e A t t r i b u t e : : e b e f o r e ) ;
29                }
30
31        }
32
33   // T y p i c a l main f u n c t i o n f o r ROSE t r a n s l a t o r
34   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
35        {
36       // B u i l d t h e AST u s e d by ROSE
37          SgProject ∗ p r o j e c t = fr ontend ( argc , argv ) ;
38
39      // B u i l d t h e t r a v e r s a l o b j e c t
40         v i s i t o r T r a v e r s a l exampleTraversal ;
41
42      // C a l l t h e t r a v e r s a l s t a r t i n g a t t h e p r o j e c t node o f t h e AST
43         exampleTraversal . t r a v e r s e I n p u t F i l e s ( project , preorder ) ;
44
45             r e t u r n backend ( p r o j e c t ) ;
46        }



      Figure 33.11: Example source code showing how automate the introduction of arbitrary text.



 1   int
 2   foo ()
 3       {
 4             int x = 42;
 5             return x ;
 6        }



        Figure 33.12: Example source code used as input to automate generation of arbitrary text.
     33.4. ADDITION OF ARBITRARY TEXT TO UNPARSED CODE GENERATION                                233




 1   # i f XYZ TOOL
 2        ” builtin ”
 3   #e n d i f
 4
 5   int foo ()
 6   {
 7       int x =
 8   # i f CRAY
 9         cray specific attribute
10   #e n d i f
11   42;
12       return x ;
13   }



             Figure 33.13: Output of input code after automating generation of arbitrary text.
234CHAPTER 33. HANDLING COMMENTS, PREPROCESSOR DIRECTIVES, AND ADDING ARBITRARY TEXT
     Chapter 34

     Partial Redundancy Elimination
     (PRE)

     Figure 34.1 shows an example of how to call the Partial Redundancy Elimination (PRE) im-
     plemented by Jeremiah Willcock. This transformation is useful for cleaning up code generated
     from other transformations (used in Qing’s loop optimizations).



     34.1             Source Code for example using PRE
     Figure 34.1 shows an example translator which calls the PRE mechanism.
        The input code is shown in figure 34.2, the output of this code is shown in figure 34.3.


 1   // Example t r a n s l a t o r d e m o n t r a t i n g P a r t i a l Redundancy E l i m i n a t i o n (PRE ) .
 2
 3   #i n c l u d e ” r o s e . h”
 4   #i n c l u d e ”CommandOptions . h”
 5
 6   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
 7        {
 8       // B u i l d t h e p r o j e c t o b j e c t (AST) which we w i l l f i l l up w i t h m u l t i p l e f i l e s and u s e a s a
 9       // h a n d l e f o r a l l p r o c e s s i n g o f t h e AST( s ) a s s o c i a t e d w i t h one o r more s o u r c e f i l e s .
10          s t d : : v e c t o r <s t d : : s t r i n g > l = C o m m a n d l i n e P r o c e s s i n g : : g e n e r a t e A r g L i s t F r o m A r g c A r g v ( a r g c , a r g v ) ;
11
12            CmdOptions : : G e t I n s t a n c e ()−> S e t O p t i o n s ( a r g c , a r g v ) ;
13            SgProject ∗ project = frontend ( l ) ;
14
15            PRE : : p a r t i a l R e d u n d a n c y E l i m i n a t i o n ( p r o j e c t ) ;
16
17            r e t u r n backend ( p r o j e c t ) ;
18        }



      Figure 34.1: Example source code showing how use Partial Redundancy Elimination (PRE).



                                                                                      235
     236                                    CHAPTER 34. PARTIAL REDUNDANCY ELIMINATION (PRE)

     34.2            Input to Example Demonstrating PRE

     Figure 34.2 shows the example input used for demonstration of Partial Redundancy Elimination
     (PRE) transformation.



 1   // Program , b a s e d on example i n Knoop e t a l ( ” Optimal c o d e motion : t h e o r y and
 2   // p r a c t i c e ” , ACM TOPLAS 1 6 ( 4 ) , 1 9 9 4 , pp . 1117 −1155 , a s c i t e d i n P a l e r i e t a l
 3   // ( s e e p r e . C ) ) , c o n v e r t e d t o C++
 4
 5   i n t unknown ( ) ; // ROSE bug :           i n c l u d i n g body ” r e t u r n 0 ; ” h e r e doesn ’ t work
 6
 7   void foo () {
 8     i n t a , b , c , x , y , z , w;
 9
10        if ( unknown ( ) ) {
11          y = a + b;
12          a = c;
13       // Added by J e r e m i a h W i l l c o c k t o t e s t   l o c a l PRE
14          w = a + b;
15          a = b;
16          x = a + b;
17          w = a + b;
18          a = c;
19       // End o f added p a r t
20          x = a + b;
21        }
22
23        if  ( unknown ( ) ) {
24          w h i l e ( unknown ( ) ) {y = a + b ; }
25        } e l s e i f ( unknown ( ) ) {
26          w h i l e ( unknown ( ) ) {}
27          i f ( unknown ( ) ) {y = a + b ; } e l s e { g o t o L9 ; } // FIXME : t h e PRE c o d e c r a s h e s     if   t h i s isn ’ t in a block
28        } else {
29          g o t o L10 ;
30        }
31
32        z = a + b;
33        a = c;
34
35        L9 : x = a + b ;
36
37        L10 :   0 ; // ROSE bug : u s i n g r e t u r n ; h e r e doesn ’ t work
38   }
39
40   i n t unknown ( ) {
41       0 ; // Works around ROSE bug
42       return 0;
43   }
44
45   i n t main ( i n t , c h a r ∗ ∗ ) {
46       foo ( ) ;
47       return 0;
48   }



     Figure 34.2: Example source code used as input to program to the Partial Redundancy Elimi-
     nation (PRE) transformation.
34.3. FINAL CODE AFTER PRE TRANSFORMATION                                         237

34.3     Final Code After PRE Transformation
Figure 34.3 shows the results from the use of PRE on an the example input code.
     238                                CHAPTER 34. PARTIAL REDUNDANCY ELIMINATION (PRE)


 1   // Program , b a s e d on example i n Knoop e t a l ( ” Optimal c o d e motion : t h e o r y and
 2   // p r a c t i c e ” , ACM TOPLAS 1 6 ( 4 ) , 1 9 9 4 , pp . 1117 −1155 , a s c i t e d i n P a l e r i e t a l
 3   // ( s e e p r e . C ) ) , c o n v e r t e d t o C++
 4   // ROSE bug : i n c l u d i n g body ” r e t u r n 0 ; ” h e r e doesn ’ t work
 5   i n t unknown ( ) ;
 6
 7   void foo ()
 8   {
 9   // P a r t i a l redundancy e l i m i n a t i o n : c a c h e v a r 1 i s a c a c h e o f ( a + b )
10     int cachevar 1 ;
11     int a ;
12     int b;
13     int c ;
14     int x ;
15     int y ;
16     int z ;
17     i n t w;
18     i f ( ( unknown ( ) ) ) {
19         y = (a + b);
20         a = c;
21   // Added by J e r e m i a h W i l l c o c k t o t e s t l o c a l PRE
22        w = (a + b);
23         a = b;
24         cachevar 1 = (a + b ) ;
25         x = cachevar 1 ;
26        w = cachevar 1 ;
27         a = c;
28   // End o f added p a r t
29         x = (a + b);
30     }
31     else {
32     }
33     i f ( ( unknown ( ) ) ) {
34         cachevar 1 = (a + b ) ;
35         w h i l e ( ( unknown ( ) ) ) {
36            y = cachevar 1 ;
37         }
38     }
39     e l s e i f ( ( unknown ( ) ) ) {
40         w h i l e ( ( unknown ( ) ) ) {
41         }
42   // FIXME : t h e PRE c o d e c r a s h e s i f t h i s i s n ’ t i n a b l o c k
43         i f ( ( unknown ( ) ) ) {
44             cachevar 1 = (a + b ) ;
45            y = cachevar 1 ;
46         }
47         else {
48             g o t o L9 ;
49         }
50     }
51     else {
52         g o t o L10 ;
53     }
54     z = cachevar 1 ;
55     a = c;
56     L9 :
57     x = (a + b);
58   // ROSE bug : u s i n g r e t u r n ; h e r e doesn ’ t work
59     L10 :
60     0;
61   }
62
63   i n t unknown ( )
64   {
65   // Works around ROSE bug
66       0;
67       return 0;
68   }
69
70   i n t main ( i n t   , char ∗∗)
71   {
72       foo ( ) ;
73       return 0;
74   }



     Figure 34.3: Output of input code after Partial Redundancy Elimination (PRE) transformation.
Chapter 35

Calling the Inliner

Figure 35.1 shows an example of how to use the inline mechanism. This chapter presents an
example translator to to inlining of function calls where they are called. Such transformations
are quite complex in a number of cases (one case is shown in the input code; a function call in a
for loop conditional test). The details of functionality are hidden from the user and a high level
interface is provided.


35.1      Source Code for Inliner
Figure 35.1 shows an example translator which calls the inliner mechanism. The code is designed
to only inline up to ten functions. the list of function calls is recomputed after any function call
is successfully inlined.
    The input code is shown in figure 35.2, the output of this code is shown in figure 35.3.


35.2      Input to Demonstrate Function Inlining
Figure 35.2 shows the example input used for demonstration of an inlining transformation.


35.3      Final Code After Function Inlining
Figure 35.3 shows the results from the inlining of three function calls. The first two function
calls are the same, and trivial. The second function call appears in the test of a for loop and is
more complex.




                                                239
     240                                                                             CHAPTER 35. CALLING THE INLINER


 1   // Example d e m o n s t r a t i n g f u n c t i o n      i n l i n i n g ( maximal i n l i n i n g , up t o p r e s e t number o f              inlinings ).
 2
 3   #i n c l u d e ” r o s e . h”
 4
 5   u s i n g namespace s t d ;
 6
 7   // T h i s i s a f u n c t i o n i n Qing ’ s AST i n t e r f a c e
 8   v o i d F i x S g P r o j e c t ( S g P r o j e c t& p r o j ) ;
 9
10   i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
11        {
12       // B u i l d t h e p r o j e c t o b j e c t (AST) which we w i l l f i l l up w i t h m u l t i p l e f i l e s and u s e a s a
13       // h a n d l e f o r a l l p r o c e s s i n g o f t h e AST( s ) a s s o c i a t e d w i t h one o r more s o u r c e f i l e s .
14          S g P r o j e c t ∗ p r o j e c t = new S g P r o j e c t ( a r g c , a r g v ) ;
15
16      // DQ ( 7 / 2 0 / 2 0 0 4 ) : Added i n t e r n a l c o n s i s t a n c y      t e s t s on AST
17         AstTests : : runAllTests ( p r o j e c t ) ;
18
19            b o o l modifiedAST = t r u e ;
20            int     count   = 0;
21
22      // I n l i n e one c a l l a t a t i m e u n t i l          a l l have been i n l i n e d .          Loops on r e c u r s i v e c o d e .
23         do {
24                   modifiedAST = f a l s e ;
25
26               // B u i l d a l i s t o f f u n c t i o n s w i t h i n t h e AST
27                  R o s e S T L C o n t a i n e r<SgNode∗> f u n c t i o n C a l l L i s t = NodeQuery : : querySubTree ( p r o j e c t , V S g F u n c t i o n C a l l E x p )
28
29               // Loop o v e r a l l f u n c t i o n c a l l s
30               // f o r ( l i s t <SgNode ∗ > : : i t e r a t o r i = f u n c t i o n C a l l L i s t . b e g i n ( ) ; i != f u n c t i o n C a l l L i s t . end ( ) ;   i ++)
31                  R o s e S T L C o n t a i n e r<SgNode ∗ > : : i t e r a t o r i = f u n c t i o n C a l l L i s t . b e g i n ( ) ;
32                  w h i l e ( modifiedAST == f a l s e && i != f u n c t i o n C a l l L i s t . end ( ) )
33                       {
34                           SgFunctionCallExp ∗ f u n c t i o n C a l l = isSgFunctionCallExp (∗ i ) ;
35                           ROSE ASSERT( f u n c t i o n C a l l != NULL ) ;
36
37                       // Not a l l f u n c t i o n c a l l s can be i n l i n e d i n C++, s o r e p o r t                 if   successful .
38                          bool s u c e s s f u l l y I n l i n e d = doInline ( functionCall ) ;
39
40                             if     ( s u c e s s f u l l y I n l i n e d == t r u e )
41                                    {
42                                   // As s o o n a s t h e AST i s m o d i f i e d recompute t h e l i s t o f f u n c t i o n
43                                   // c a l l s ( and r e s t a r t t h e i t e r a t i o n s o v e r t h e m o d i f i e d l i s t )
44                                        modifiedAST = t r u e ;
45                                    }
46                                   else
47                                    {
48                                        modifiedAST = f a l s e ;
49                                    }
50
51                       // I n c r e m e n t t h e     list   iterator
52                          i ++;
53                        }
54
55               // Q u i t e when we have c e a s e d t o do any i n l i n e t r a n s f o r m a t i o n s
56               // and o n l y do a p r e d e f i n e d number o f i n l i n e t r a n s f o r m a t i o n s
57                      c o u n t++;
58                 }
59            w h i l e ( modifiedAST == t r u e && c o u n t < 1 0 ) ;
60
61      // C a l l f u n c t i o n t o p o s t p r o c e s s t h e AST and f i x u p symbol t a b l e s
62         FixSgProject (∗ p r o j e c t ) ;
63
64      // Rename e a c h v a r i a b l e d e c l a r a t i o n
65         renameVariables ( p r o j e c t ) ;
66
67      // Fold up b l o c k s
68         flattenBlocks ( project );
69
70      // Clean up i n l i n e r −g e n e r a t e d c o d e
71         cleanupInlinedCode ( project ) ;
72
73      // Change members t o p u b l i c
74         changeAllMembersToPublic ( p r o j e c t ) ;
75
76      // DQ ( 3 / 1 1 / 2 0 0 6 ) : T h i s f a i l s s o t h e i n l i n i n g , o r t h e AST I n t e r f a c e
77      // s u p p o r t , n e e d s more work even though i t g e n e r a t e d good c o d e .
78      // A s t T e s t s : : r u n A l l T e s t s ( p r o j e c t ) ;
79
80            r e t u r n backend ( p r o j e c t ) ;
81        }
     35.3. FINAL CODE AFTER FUNCTION INLINING                                                                                 241




 1   // T h i s t e s t c o d e i s a c o m b i n a t i o n o f p a s s 1 and p a s s 7 , s e l e c t e d somewhat randomly
 2   // from Jeremiah ’ s t e s t c o d e o f h i s i n l i n i n g t r a n s f o r m a t i o n from summer 2 0 0 4 .
 3
 4   int x = 0;
 5
 6   // F u n c t i o n i t i n c r e m e n t ”x”
 7   v o i d incrementX ( )
 8         {
 9           x++;
10         }
11
12   int foo ()
13      {
14        int a = 0;
15        while ( a < 5)
16            {
17              ++a ;
18            }
19
20           return a + 3;
21       }
22
23   i n t main ( i n t , c h a r ∗ ∗ )
24        {
25       // Two t r i v a l f u n c t i o n   calls   to i n l i n e
26          incrementX ( ) ;
27          incrementX ( ) ;
28
29      // Somthing more i n t e r e s t i n g t o i n l i n e
30         for ( ; foo () < 7;)
31            {
32              x++;
33            }
34
35           return x ;
36       }



       Figure 35.2: Example source code used as input to program to the inlining transformation.
     242                                                                 CHAPTER 35. CALLING THE INLINER




 1   // T h i s t e s t c o d e i s a c o m b i n a t i o n o f p a s s 1 and p a s s 7 , s e l e c t e d somewhat randomly
 2   // from Jeremiah ’ s t e s t c o d e o f h i s i n l i n i n g t r a n s f o r m a t i o n from summer 2 0 0 4 .
 3   int x = 0;
 4   // F u n c t i o n i t i n c r e m e n t ”x”
 5
 6   v o i d incrementX ( )
 7   {
 8      x++;
 9   }
10
11   int foo ()
12   {
13     int a 0 = 0;
14     while ( a 0 < 5){
15       ++a 0 ;
16     }
17     return a 0 + 3;
18   }
19
20   i n t main ( i n t , c h a r ∗ ∗ )
21   {
22       x++;
23       x++;
24   // Somthing more i n t e r e s t i n g t o i n l i n e
25       for ( ; true ; ) {
26         int a 1 = 0;
27         while ( a 1 < 5){
28            ++a 1 ;
29         }
30         int rose temp 7 0 = a 1 + 3;
31         bool rose temp 2 = ( bool )( rose temp                    7   0 < 7);
32         i f (! rose temp 2 ) {
33            break ;
34         }
35         else {
36         }
37         x++;
38       }
39       return x ;
40   }



                         Figure 35.3: Output of input code after inlining transformations.
Chapter 36

Using the AST Outliner

Outlining is the process of replacing a block of consecutive statements with a function call
to a new function containing those statements. Conceptually, outlining the inverse of inlining
(Chapter 35). This chapter shows how to use the basic experimental outliner implementation
included in the ROSE projects directory.
    There are two basic ways to use the outliner. The first is a “user-level” method, in which
you may use a special pragma to mark outline targets in the input program, and then call a
high-level driver routine to process these pragmas. You can also use command line option to
specify outlining targets using abstract handle strings (detailed in Chapter 45). The second
method is to call “low-level” outlining routines that operate directly on AST nodes. After a
brief example of what the outliner can do and a discussion of its limits (Sections 36.1–36.2), we
discuss each of these methods in Sections 36.3 and 36.5, respectively.




36.1      An Outlining Example
Figure 36.1 shows a small program with a pragma marking the outline target, a nested for loop,
and Figure 36.2 shows the result. The outliner extracts the loop and inserts it into the body of a
new function, and inserts a call to that function. The outlined code’s input and output variables
are wrapped up as parameters to this function. We make the following observations about this
output.
   Placement and forward declarations. The function itself is placed, by default, at the
end of the input file to guarantee that it has access to all of the same declarations that were
available at the outline target site. The outliner inserts any necessary forward declarations
as well, including any necessary friend declarations if the outline target appeared in a class
member function.
   Calling convention. The outliner generates a C-callable function (extern ‘‘C’’, with
pointer arguments). This design choice is motivated by our need to use the outliner to extract
code into external, dynamically loadable library modules.

                                               243
     244                                                                 CHAPTER 36. USING THE AST OUTLINER

     36.2          Limitations of the Outliner
     The main limitation of the outliner implementation is that it can only outline single SgStatement
     nodes. However, since an SgStatement node may be a block (i.e., an SgBasicBlock node), a
     “single statement” may actually comprise a sequence of complex statements.
         The rationale for restricting to single SgStatement nodes is to avoid subtly changing the
     program’s semantics when outlining code. Consider the following example, in which we wish to
     outline the middle 3 lines of executable code.
       int x = 5 ;
2    // START o u t l i n i n g h e r e .
       foo (x ) ;
4      Object y ( x ) ;
       y . foo ( ) ;
6    // STOP o u t l i n i n g h e r e .
       y . bar ( ) ;
     This example raises a number of issues. How should an outliner handle the declaration of y,
     which constructs an object in local scope? It cannot just cut-and-paste the declaration of y to
     the body of the new outlined function because that will change its scope and lifetime, rendering
     the call to y.bar() impossible. Additionally, it may be unsafe to move the declaration of y
     so that it precedes the outlined region because the constructor call may have side-effects that
     could affect the execution of foo(x). It is possible to heap-allocate y inside the body of the


     namespace N
     {
        class A
        {
 5         i n t f o o ( void ) const { return 7 ; }
           i n t b a r ( void ) const { return f o o ( ) / 2 ; }
        public :
           i n t b i z ( void ) const
           {
10             int r e s u l t = 0 ;
     # pragma r o s e o u t l i n e
               f o r ( i n t i = 1 ; i <= f o o ( ) ; i ++)
                   f o r ( i n t j = 1 ; j <= b a r ( ) ; j ++)
                       r e s u l t += i ∗ j ;
15             return r e s u l t ;
           }
        };
     }

20   extern ”C” i n t p r i n t f ( const char ∗ fmt ,                ...);

     i n t main ( )
     {
        N: :A x ;
25       p r i n t f ( ”%d\n” , x . b i z   ( ) ) ; // P r i n t s   ’168 ’
         return 0 ;
     }



     Figure 36.1: inputCode OutlineLoop.cc: Sample input program. The #pragma directive marks
     the nested for loop for outlining.
     36.2. LIMITATIONS OF THE OUTLINER                                                                                    245


     extern ”C” void O U T              1    13785       ( int ∗ r e s u l t p    , const void ∗ t h i s   ptr   p   );
     namespace N
     {

 5      class A
        {
        public : f r i e n d void : : O U T        1   13785   ( int ∗ r e s u l t p ,
                                                           const void ∗ t h i s p t r         p   );

10      private : i n l i n e i n t f o o ( ) const
          {
            return 7 ;
          }

15         i n l i n e i n t b a r ( ) const
           {
               return ( t h i s)−> f o o ( ) / 2 ;
           }

20     public :        i n l i n e i n t b i z ( ) const
           {
     // //A d e c l a r a t i o n f o r t h i s p o i n t e r
             const c l a s s A ∗ t h i s p t r               = this ;
             int r e s u l t = 0 ;
25              OUT 1 13785                  (& r e s u l t , & t h i s p t r    );
                return r e s u l t ;
           }
       }
         ;
30   }
     extern ”C”
     {
       i n t p r i n t f ( const char ∗ fmt , . . . ) ;
     }
35
     i n t main ( )
     {
         class N: :A x ;
     // P r i n t s ’ 1 6 8 ’
40       p r i n t f ( ”%d\n” , x . b i z   ());
         return 0 ;
     }

     extern ”C” void O U T 1 1 3 7 8 5                  ( int ∗ r e s u l t p , const void ∗ t h i s p t r p      )
45   {
       i n t &r e s u l t = ∗ ( ( i n t ∗ ) r e s u l t p   );
       const c l a s s N : : A ∗ & t h i s p t r           = ∗ ( ( const c l a s s N : : A ∗ ∗ ) t h i s p t r p );
       f o r ( i n t i = 1 ; i <= t h i s p t r − o o ( ) ; i ++)
                                                            >f
           f o r ( i n t j = 1 ; j <= t h i s p t r − a r ( ) ; j ++)
                                                               >b
50             r e s u l t += ( i ∗ j ) ;
     }



     Figure 36.2: rose outlined-inputCode OutlineLoop.cc: The nested for loop of Figure 36.1 has
     been outlined.


     outlined function so that it can be returned to the caller and later freed, but it is not clear if
     changing y from a stack-allocated variable to a heap-allocated one will always be acceptable,
     particularly if the developer of the original program has, for instance, implemented a customized
     memory allocator. Restricting outlining to well-defined SgStatement objects avoids these issues.
     It is possible to build a “higher-level” outliner that extends the outliner’s basic infrastructure to
     handle these and other issues.
246                                             CHAPTER 36. USING THE AST OUTLINER

    The outliner cannot outline all possible SgStatement nodes. However, the outliner interface
provides a routine, outliner::isOutlineable(s), for testing whether an SgStatement object
s is known to satisfy the outliner’s preconditions (see Section 36.5 for details).



36.3      User-Directed Outlining via Pragmas
Figure 36.3 shows the basic translator, outline, that produces Figure 36.2 from Figure 36.1.
This translator extends the identity translator with an include directive on line 5 of Figure 36.3,
and a call to the outliner on line 16. All outliner routines live in the Outliner namespace. Here,
the call to Outliner::outlineAll (proj) on line 16 traverses the AST, looks for #pragma
rose outline directives, outlines the SgStatement objects to which each pragma is attached,
and returns the number of outlined objects.
   A slightly lower-level outlining primitive. The Outliner::outlineAll() routine is a
wrapper around calls to a simpler routine, Outliner::outline(), that operates on pragmas:

   Outliner : : Result      O u t l i n e r : : o u t l i n e ( SgPragmaDeclaration ∗ s ) ;

Given a pragma statement AST node s, this routine checks if s is a rose outline directive,
and if so, outlines the statement with which s is associated. It returns a Outliner::Result
object, which is simply a structure that points to (a) the newly generated outlined function and
(b) the statement corresponding to the new function call (i.e., the outlined function call-site).
See Outliner.hh or the ROSE Programmer’s Reference for more details.
   The Outliner::outlineAll() wrapper. The advantage of using the wrapper instead of
the lower-level primitive is that the wrapper processes the pragmas in an order that ensures the
outlining can be performed correctly in-place. This order is neither a preorder nor a postorder
traversal, but in fact a “reverse” preorder traversal; refer to the wrapper’s documentation for an
explanation.



36.4      Outlining via Abstract Handles
The ROSE AST outliner also allows users to specify outlining targets using abstract handles
(details are given in Chapter 45) without relying on planting pragmas into the source code.
For the translator (e.g. named outline) built from the source shown in Figure 36.3, it accepts
a command line option in a form of -rose:outline:abstract handle handle string. The outline
program is able to locate a language construct matching the handle string within an input source
file and then outline the construct.
    For example, a handle string ”ForStatement¡position,12¿” will tell the outliner to outline the
for loop at source position line 12.
Another handle, ”FunctionDeclaration¡name,initialize¿::ForStatement¡numbering,2¿” indicates
that the outlining target is the second loop within a function named initializer. Figure 36.5
shows the outlining results using the first handle(”ForStatement¡position,12¿”) from an input
source file (shown in Figure 36.4). Figure 36.6 shows the results using the second handle string
for the same input.
     36.4. OUTLINING VIA ABSTRACT HANDLES                                                                                   247


     // ! o u t l i n e . cc : Demonstrates t h e pragma−i n t e r f a c e o f t h e O u t l i n e r .
     #include <r o s e . h>
     #include <i o s t r e a m >

 5   #include <O u t l i n e r . hh>
     #include <v e c t o r >
     #include <s t r i n g >

     using namespace s t d ;
10
     int
     main ( i n t a r g c , char ∗ a r g v [ ] )
     {
       // ! A c c e p t i n g command l i n e o p t i o n s t o t h e o u t l i n e r
15     v e c t o r <s t r i n g > a r g v L i s t ( argv , a r g v+a r g c ) ;
       O u t l i n e r : : commandLineProcessing ( a r g v L i s t ) ;

         SgProject ∗ proj = frontend ( argvList ) ;
         ROSE ASSERT ( p r o j ) ;
20
         c e r r << ” [ O u t l i n i n g . . . ] ” << e n d l ;
         s i z e t count = O u t l i n e r : : o u t l i n e A l l ( p r o j ) ;

         c e r r << ” [ P r o c e s s e d ” << c o u n t << ” o u t l i n e        d i r e c t i v e s . ] ” << e n d l ;
25       return backend ( p r o j ) ;
     }



     Figure 36.3: outline.cc: A basic outlining translator, which generates Figure 36.2 from Fig-
     ure 36.1. This outliner relies on the high-level driver, Outliner::outlineAll(), which scans
     the AST for outlining pragma directives (#pragma rose outline) that mark outline targets.




     #d e f i n e MSIZE 500
     i n t n ,m, m i t s ;
     double t o l , r e l a x = 1 . 0 , a l p h a = 0 . 0 5 4 3 ;
     double u [ MSIZE ] [ MSIZE ] , f [ MSIZE ] [ MSIZE ] , u o l d [ MSIZE ] [ MSIZE ] ;
 5   double dx , dy ;

     void i n i t i a l i z e ( )
     {
       i n t i , j , xx , yy ;
10     dx = 2 . 0 / ( n − 1 );
       dy = 2 . 0 / (m− 1 );
       f o r ( i =0; i <n ; i ++)
                              m;
           f o r ( j =0; j < j ++)
           {
15             xx =( i n t ) ( −1.0 + dx ∗ ( i − 1 ) ) ;
               yy = ( i n t ) ( − 1 . 0 + dy ∗ ( j − 1 ) ) ;
               u[ i ][ j ] = 0.0;
               f [ i ] [ j ] = −1.0∗ a l p h a ∗(1.0 − xx ∗ xx ) ∗ ( 1 . 0 − yy ∗ yy ) \
                               − 2 . 0 ∗ ( 1 . 0 − xx ∗ xx ) −2.0∗(1.0 − yy ∗ yy ) ;
20         }
     }



               Figure 36.4: inputCode OutlineLoop2.c: Sample input program without pragmas.
     248                                                          CHAPTER 36. USING THE AST OUTLINER


     #d e f i n e MSIZE 500
     int n ;
     i n t m;
     int mits ;
 5   double t o l ;
     double r e l a x = 1 . 0 ;
     double a l p h a = 0 . 0 5 4 3 ;
     double u [ 5 0 0UL ] [ 5 0 0 UL ] ;
     double f [ 5 0 0UL ] [ 5 0 0 UL ] ;
10   double u o l d [ 5 0 0UL ] [ 5 0 0 UL ] ;
     double dx ;
     double dy ;
     void O U T 1 1 1 8 9 0 ( i n t ∗ i p        , int ∗ j p   , int ∗ xxp   , int ∗ yyp   );

15   void i n i t i a l i z e ( )
     {
       int i ;
       int j ;
       i n t xx ;
20     i n t yy ;
       dx = ( 2 . 0 / ( n − 1 ) ) ;
       dy = ( 2 . 0 / (m − 1 ) ) ;
       O U T 1 1 1 8 9 0 (& i ,& j ,&xx ,& yy ) ;
     }
25
     void O U T 1 1 1 8 9 0 ( i n t ∗ i p      , int ∗ j p , int ∗ xxp , int ∗ yyp )
     {
        for ( ∗ i p   = 0;      ∗ip     < n ; ( ∗ i p )++)
          for ( ∗ j p   = 0;      ∗jp        < m; ( ∗ j p )++) {
30            ∗ xxp   = ( ( i n t ) ( − 1 . 0 + ( dx ∗ ( ∗ i p   − 1))));
              ∗ yyp   = ( ( i n t ) ( − 1 . 0 + ( dy ∗ ( ∗ j p   − 1))));
            u[ ∗ip    ][ ∗jp      ] = 0.0;
            f [ ∗ip   ][ ∗jp      ] = ((((( −1.0 ∗ alpha ) ∗ ( 1 . 0 − ( ∗ xxp       ∗
     ∗ xxp ) ) ) ∗ (1.0 − ( ∗ yyp            ∗ ∗ yyp ) ) ) − (2.0 ∗ (1.0 − ( ∗ xxp              ∗
     ∗ xxp ) ) ) ) − (2.0 ∗ (1.0 − ( ∗ yyp            ∗ ∗ yyp ) ) ) ) ;
          }
35   }



     Figure 36.5: rose inputCode OutlineLoop2.c: The loop at line 12 of Figure 36.12 has been
     outlined.



     36.5          Calling Outliner Directly on AST Nodes

     The preceding examples rely on the outliner’s #pragma interface to identify outline targets. In
     this section, we show how to call the outliner directly on SgStatement nodes from within your
     translator.
        Figure 36.7 shows an example translator that finds all if statements and outlines them. A
     sample input appears in Figure 36.8, with the corresponding output shown in Figure 36.9. Notice
     that valid preprocessor control structure is accounted for and preserved in the output.
         The translator has two distinct phases. The first phase selects all outlineable if-statements,
     using the CollectOutlineableIfs helper class. This class produces a list that stores the targets
     in an order appropriate for outlining them in-place. The second phase iterates over the list of
     statements and outlines each one. The rest of this section explains these phases, as well as
     various aspects of the sample input and output.
     36.5. CALLING OUTLINER DIRECTLY ON AST NODES                                                           249


     #d e f i n e MSIZE 500
     int n ;
     i n t m;
     int mits ;
 5   double t o l ;
     double r e l a x = 1 . 0 ;
     double a l p h a = 0 . 0 5 4 3 ;
     double u [ 5 0 0UL ] [ 5 0 0 UL ] ;
     double f [ 5 0 0UL ] [ 5 0 0 UL ] ;
10   double u o l d [ 5 0 0UL ] [ 5 0 0 UL ] ;
     double dx ;
     double dy ;
     void O U T 1 1 1 8 9 0 ( i n t i , i n t ∗ j p    , int ∗ xxp   , int ∗ yyp   );

15   void i n i t i a l i z e ( )
     {
       int i ;
       int j ;
       i n t xx ;
20     i n t yy ;
       dx = ( 2 . 0 / ( n − 1 ) ) ;
       dy = ( 2 . 0 / (m − 1 ) ) ;
       f o r ( i = 0 ; i < n ; i ++)
           O U T 1 1 1 8 9 0 ( i ,& j ,&xx ,& yy ) ;
25   }

     void O U T 1 1 1 8 9 0 ( i n t i , i n t ∗ j p     , int ∗ xxp , int ∗ yyp )
     {
        for ( ∗ j p    = 0;      ∗jp        < m; ( ∗ j p )++) {
30          ∗ xxp    = ( ( i n t ) ( − 1 . 0 + ( dx ∗ ( i − 1 ) ) ) ) ;
            ∗ yyp    = ( ( i n t ) ( − 1 . 0 + ( dy ∗ ( ∗ j p    − 1))));
          u[ i ][ ∗jp    ] = 0.0;
          f [ i ][ ∗jp   ] = ((((( −1.0 ∗ alpha ) ∗ ( 1 . 0 − ( ∗ xxp          ∗ ∗ xxp ) ) ) ∗ (1.0 − ( ∗ yyp   ∗
     ∗ yyp ) ) ) − (2.0 ∗ (1.0 − ( ∗ xxp               ∗ ∗ xxp ) ) ) ) − (2.0 ∗ (1.0 − ( ∗ yyp    ∗
     ∗ yyp ) ) ) ) ;
        }
35   }



     Figure 36.6: rose inputCode OutlineLoop2b.c: The 2nd loop within a function named initialize-
     from Figure 36.12 has been outlined.


     36.5.1       Selecting the outlineable if statements
     Line 45 of Figure 36.7 builds a list, ifs (declared on line 44), of outlineable if-statements. The
     helper class, CollectOutlineableIfs in lines 12–35, implements a traversal to build this list.
     Notice that a node is inserted into the target list only if it satisfies the outliner’s preconditions;
     this check is the call to Outliner::isOutlineable() on line 28.
         The function Outliner::isOutlineable() also accepts an optional second boolean param-
     eter (not shown). When this parameter is true and the statement cannot be outlined, the check
     will print an explanatory message to standard error. Such messages are useful for discovering
     why the outliner will not outline a particular statement. The default value of this parameter is
     false.

     36.5.2       Properly ordering statements for in-place outlining
     Each call to Outliner::outline(*i) on line 50 of Figure 36.7 outlines a target if-statement *i
     in if targets. However, in order for these statements to be outlined in-place, it is essential to
     250                                                                        CHAPTER 36. USING THE AST OUTLINER


     // o u t l i n e I f s . cc : C a l l s O u t l i n e r d i r e c t l y t o o u t l i n e   if   statements .
     #include <r o s e . h>
     #include <i o s t r e a m >
     #include <s e t >
 5   #include < l i s t >

     #include <O u t l i n e r . hh>

     using namespace s t d ;
10
     // T r a v e r s a l t o g a t h e r a l l o u t l i n e a b l e S g I f S t m t nodes .
     c l a s s C o l l e c t O u t l i n e a b l e I f s : public A s t S i m p l e P r o c e s s i n g
     {
     public :
15       // Container o f l i s t s t a t e m e n t s i n ‘ ‘ o u t l i n e a b l e ’ ’ o r d e r .
         typedef l i s t <S g I f S t m t ∗> I f L i s t t ;

         // C a l l t h i s r o u t i n e t o g a t h e r t h e o u t l i n e t a r g e t s .
         s t a t i c void c o l l e c t ( S g P r o j e c t ∗ p , I f L i s t t & f i n a l )
20       {
             CollectOutlineableIfs collector ( final );
             c o l l e c t o r . traverseInputFiles (p , postorder ) ;
         }

25       v i r t u a l void v i s i t ( SgNode∗ n )
         {
             SgIfStmt ∗ s = isSgIfStmt (n ) ;
             i f ( Outliner : : isOutlineable ( s ))
                 f i n a l t a r g e t s . push back ( s ) ;
30       }

     private :
        C o l l e c t O u t l i n e a b l e I f s ( I f L i s t t& f i n a l ) : f i n a l t a r g e t s ( f i n a l ) {}
        I f L i s t t & f i n a l t a r g e t s ; // F i n a l l i s t o f o u t l i n e t a r g e t s .
35   };

          = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
     //= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
     i n t main ( i n t a r g c , char ∗ a r g v [ ] )
     {
40       SgProject ∗ p r o j = frontend ( argc , argv ) ;
        ROSE ASSERT ( p r o j ) ;

     #i f 1
        // B u i l d a s e t o f o u t l i n e a b l e i f s t a t e m e n t s .
45      CollectOutlineableIfs : : IfList t i f s ;
        C o l l e c t O u t l i n e a b l e I f s : : c o l l e c t ( proj , i f s ) ;

       // O u t l i n e them a l l .
        for ( C o l l e c t O u t l i n e a b l e I f s : : I f L i s t t : : i t e r a t o r i = i f s . begin ( ) ;
50                i != i f s . end ( ) ; ++i )
            Outliner : : o u t l i n e (∗ i ) ;
     #e l s e
        p r i n t f ( ” S k i p p i n g o u t l i n i n g due t o r e c e n t move from s t d : : l i s t t o s t d : : v e c t o r i n ROSE \n” ) ;
     #e n d i f
55
         // Unparse
         return backend ( p r o j ) ;
     }



     Figure 36.7: outlineIfs.cc: A lower-level outlining translator, which calls Outliner::outline()
     directly on SgStatement nodes. This particular translator outlines all SgIfStmt nodes.



     outline the statements in the proper order.
     36.5. CALLING OUTLINER DIRECTLY ON AST NODES                                              251


     #include <i o s t r e a m >

     using namespace s t d ;

 5   #d e f i n e LEAP YEAR 0

     i n t main ( i n t a r g c , char ∗ a r g v [ ] )
     {
         f o r ( i n t i = 1 ; i < a r g c ; ++i )
10           {
                s t r i n g month ( a r g v [ i ] ) ;
                s i z e t days = 0 ;
                i f ( month == ” January ”
                         | | month == ”March”
15                       | | month == ”May”
                         | | month == ” J u l y ”
                         | | month == ” August ”
                         | | month == ” Oc t o b er ”
                         | | month == ” December ” )
20                  days = 3 1 ;
     #i f LEAP YEAR
                e l s e i f ( month == ” F eb r u a r y ” )
                    days = 2 9 ;
     #e l s e
25              e l s e i f ( month == ” F eb r u a r y ” )
                    days = 2 8 ;
     #e n d i f
                e l s e i f ( month == ” A p r i l ”
                               | | month == ” June ”
30                             | | month == ” September ”
                               | | month == ” November ” )
                    days = 3 0 ;
                c o u t << a r g v [ i ] << ” ” << days << e n d l ;
             }
35       return 0 ;
     }



     Figure 36.8: inputCode Ifs.cc: Sample input program, without explicit outline targets specified
     using #pragma rose outline, as in Figures 36.1 and 36.12.


        The postorder traversal implemented by the helper class, CollectOutlineableIfs, produces
     the correct ordering. To see why, consider the following example code:
     i f ( a ) // [ 1 ]
2    {
         i f ( b ) f o o ( ) ; // [ 2 ]
4    }
     e l s e i f ( c ) // [ 3 ]
6    {
         i f ( d ) bar ( ) ; // [ 4 ]
8    }
     The corresponding AST is (roughly)
            SgIfStmt:[1]
           /             \
          /                \
     SgIfStmt:[2]    SgIfStmt:[3]
     252                                                             CHAPTER 36. USING THE AST OUTLINER

                                    |
                               SgIfStmt:[4]
     The postorder traversal—2, 4, 3, 1—ensures that child if-statements are outlined before their
     parents.

     #include <i o s t r e a m >
     using namespace s t d ;
     #d e f i n e LEAP YEAR 0
     extern ”C” void O U T           1   11083     ( void ∗ monthp        , s i z e t ∗ daysp     );
 5   extern ”C” void O U T           2   11083     ( void ∗ monthp        , s i z e t ∗ daysp     );
     extern ”C” void O U T           3   11083     ( void ∗ monthp        , s i z e t ∗ daysp     );

     i n t main ( i n t a r g c , char ∗ a r g v [ ] )
     {
10       f o r ( i n t i = 1 ; i < a r g c ; ++i ) {
             s t d : : s t r i n g month ( a r g v [ i ] ) ;
             s i z e t days = 0 ;
             O U T 3 1 1 0 8 3 (&month ,& days ) ;
             ( ( ∗(& s t d : : c o u t)<<a r g v [ i ]<<” ” ) << days ) << s t d : : e n d l < char
     , s t d : : c h a r t r a i t s < char > > ;
15       }
         return 0 ;
     }

     extern ”C” void O U T 1 1 1 0 8 3 ( void ∗ monthp , s i z e t ∗ d a y s p )
20   {
       s t d : : s t r i n g &month = ∗ ( ( s t d : : s t r i n g ∗ ) monthp ) ;
       s i z e t &days = ∗ ( ( s i z e t ∗ ) d a y s p ) ;
       i f ( ( ( month==” A p r i l ” | | month==” June ” ) | | month==” September ” )                 | | month==” November ” )
           days = 3 0 ;
25   }

     extern ”C” void O U T 2 1 1 0 8 3 ( void ∗ monthp , s i z e t ∗ d a y s p                   )
     {
        s t d : : s t r i n g &month = ∗ ( ( s t d : : s t r i n g ∗ ) monthp ) ;
30      s i z e t &days = ∗ ( ( s i z e t ∗ ) d a y s p ) ;
     #i f 1 /∗ #i f LEAP YEAR . . . #e l s e ∗/
     ;
        i f ( month==” F e br ua r y ” )
            days = 2 8 ;
35      else
     #e n d i f
            O U T 1 1 1 0 8 3 (&month ,& days ) ;
     }

40   extern ”C” void O U T 3 1 1 0 8 3 ( void ∗ monthp , s i z e t ∗ d a y s p )
     {
        s t d : : s t r i n g &month = ∗ ( ( s t d : : s t r i n g ∗ ) monthp ) ;
        s i z e t &days = ∗ ( ( s i z e t ∗ ) d a y s p ) ;
        i f ( ( ( ( ( ( month==” January ” | | month==”March” ) | | month==”May” )                     | | month==” J u l y ” )   | | month==” August ” )   | | month=
45          days = 3 1 ;
        else
     #i f LEAP YEAR
     #e l s e
     {
50   #e n d i f /∗ #i f LEAP YEAR . . . #e l s e             ∗/
            O U T 2 1 1 0 8 3 (&month ,& days ) ;
        }
     }



     Figure 36.9: rose inputCode Ifs.cc: Figure 36.8, after outlining using the translator in Fig-
     ure 36.7.
     36.6. OUTLINER’S PREPROCESSING PHASE                                                                                               253

     36.6            Outliner’s Preprocessing Phase
     Internally, the outliner implementation itself has two distinct phases. The first is a preprocessing
     phase, in which an arbitrary outlineable target is placed into a canonical form that is relatively
     simple to extract. The second phase then creates the outlined function, replacing the original
     target with a call to the outlined function. It is possible to run just the preprocessing phase,
     which is useful for understanding or even debugging the outliner implementation.

     // o u t l i n e P r e p r o c . cc : Shows t h e o u t l i n e r ’ s p r e p r o c e s s o r −o n l y phase .
     #include <r o s e . h>
     #include <i o s t r e a m >

 5   #include <O u t l i n e r . hh>

     using namespace s t d ;

     int
10   main ( i n t a r g c , char ∗ a r g v [ ] )
     {
       SgProject ∗ p r o j = frontend ( argc , argv ) ;
       ROSE ASSERT ( p r o j ) ;

15   #i f 1
        c e r r << ” [ Running o u t l i n e r ’ s p r e p r o c e s s i n g p h a s e o n l y . . . ] ” << e n d l ;
        s i z e t count = O u t l i n e r : : p r e p r o c e s s A l l ( p r o j ) ;
        c e r r << ”        [ P r o c e s s e d ” << c o u n t << ” o u t l i n e d i r e c t i v e s . ] ” << e n d l ;
     #e l s e
20      p r i n t f ( ” S k i p p i n g o u t l i n i n g due t o r e c e n t move from s t d : : l i s t t o s t d : : v e c t o r i n ROSE \n” ) ;
     #e n d i f

         c e r r << ” [ U n p a r s i n g . . . ] ” << e n d l ;
         return backend ( p r o j ) ;
25   }



     Figure 36.10: outlinePreproc.cc: The basic translator of Figure 36.3, modified to execute the
     Outliner’s preprocessing phase only. In particular, the original call to Outliner::outlineAll()
     has been replaced by a call to Outliner::preprocessAll().

         To call just the preprocessor, simply replace a call to Outliner::outlineAll(s) or Outliner::outline(s)
     with a call to Outliner::preprocessAll(s) or Outliner::preprocess(s), respectively. The
     translator in Figure 36.10 modifies the translator in Figure 36.3 in this way to create a preprocessing-
     only translator.
         The preprocessing phase consists of a sequence of initial analyses and transformations that
     the outliner performs in order to put the outline target into a particular canonical form. Roughly
     speaking, this form is an enclosing SgBasicBlock node, possibly preceded or followed by start-up
     and tear-down code. Running just the preprocessing phase on Figure 36.1 produces the output in
     Figure 36.11. In this example, the original loop is now enclosed in two additional SgBasicBlocks
     (Figure 36.11, lines 24–35), the outermost of which contains a declaration that shadows the
     object’s this pointer, replacing all local references to this with the new shadow pointer. In
     this case, this initial transformation is used by the main underlying outliner implementation to
     explicitly identify all references to the possibly implicit references to this.
         The preprocessing phase is more interesting in the presence of non-local control flow out-
     side the outline target. Consider Figure 36.12, in which the outline target contains two break
     254                                                            CHAPTER 36. USING THE AST OUTLINER


     namespace N
     {

        class A
 5      {

        private : i n l i n e i n t f o o ( ) const
          {
            return 7 ;
10        }

           i n l i n e i n t b a r ( ) const
           {
               return ( t h i s)−> f o o ( ) / 2 ;
15         }

       public :     i n l i n e i n t b i z ( ) const
         {
     // //A d e c l a r a t i o n f o r t h i s p o i n t e r
20         const c l a s s A ∗ t h i s p t r             = this ;
           int r e s u l t = 0 ;

     # pragma r o s e o u t l i n e
              {
25              f o r ( i n t i = 1 ; i <= t h i s p t r − o o ( ) ; i ++)
                                                             >f
                    f o r ( i n t j = 1 ; j <= t h i s p t r − a r ( ) ; j ++)
                                                                >b
                        r e s u l t += ( i ∗ j ) ;
              }
              return r e s u l t ;
30          }
        }
          ;
     }

35   extern ”C”
     {
       i n t p r i n t f ( const char ∗ fmt ,       ...);
     }

40   i n t main ( )
     {
         class N: :A x ;
     // P r i n t s ’ 1 6 8 ’
         p r i n t f ( ”%d\n” , x . b i z   ());
45       return 0 ;
     }



     Figure 36.11: rose outlined pp-inputCode OutlineLoop.cc: Figure 36.1 after outline preprocess-
     ing only, i.e., specifying -rose:outline:preproc-only as an option to the translator of Fig-
     ure 36.3.


     statements, which require jumping to a regions of code outside the target. We show the prepro-
     cessed code in Figure 36.13. The original non-local jumps are first transformed into assignments
     to a flag, EXIT TAKEN (lines 18–20 and 26–29), and then relocated to a subsequent block of
     code (lines 38–53) with their execution controlled by the value of the flag. The final outlined
     result appears in Figure 36.14; the initial preprocessing simplifies this final step of extracting
     the outline target.
     36.6. OUTLINER’S PREPROCESSING PHASE                                                                          255




     #include <i o s t r e a m >

     s i z e t f a c t o r i a l ( s i z e t n)
     {
 5       s i z e t i = 1;
         s i z e t r = 1;
         while ( 1 )
             {
     # pragma r o s e o u t l i n e
10              i f ( i <= 1 )
                    break ; // Non−l o c a l jump #1
                e l s e i f ( i >= n )
                    break ; // Non−l o c a l jump #2
                else
15                  r ∗= ++i ;
             }
         return r ;
     }

20   i n t main ( i n t a r g c , char ∗ a r g v [ ] )
     {
         s t d : : c o u t << ” 7 ! == ” << f a c t o r i a l   ( 7 ) << s t d : : e n d l ; // P r i n t s 5040
         return 0 ;
     }



     Figure 36.12: inputCode OutlineNonLocalJumps.cc: Sample input program, with an outlining
     target that contains two non-local jumps (here, break statements).
     256                                                                      CHAPTER 36. USING THE AST OUTLINER




     #include <i o s t r e a m >

     size t factorial            ( s i z e t n)
     {
 5     s i z e t i = 1;
       s i z e t r = 1;
       while ( 1 )
           {

10   # pragma r o s e o u t l i n e
            {
              i n t EXIT TAKEN = 0 ;
              {
                  i f ( i <= 1 )
15                    {
                         EXIT TAKEN = 1 ;
                         goto NON LOCAL EXIT ;
                      }
                  e l s e i f ( i >= n )
20                    {
                         EXIT TAKEN = 2 ;
                         goto NON LOCAL EXIT ;
                      }
                  else
25                    r ∗= ++i ;
              NON LOCAL EXIT : ;
              }
              i f ( EXIT TAKEN == 1 )
                  {
30   // Non−l o c a l jump #1
                      break ;
                  }
              else
                  {
35                    i f ( EXIT TAKEN == 2 )
                         {
     // Non−l o c a l jump #2
                            break ;
                         }
40                    else
                         {
                         }
                  }
            }
45        }
        return r ;
     }

     i n t main ( i n t a r g c , char ∗ a r g v [ ] )
50   {
     // P r i n t s 5040
         ( ( ∗ ( & s t d : : c o u t ) << ” 7 ! == ” ) << f a c t o r i a l    ( 7 ) ) << s t d : : e n d l < char ,
              s t d : : c h a r t r a i t s < char >>;
         return 0 ;
55   }



     Figure 36.13: rose outlined pp-inputCode OutlineNonLocalJumps.cc: The non-local jump ex-
     ample of Figure 36.12 after outliner preprocessing, but before the actual outlining. The non-local
     jump is handled by an additional flag, EXIT TAKEN , which indicates what non-local jump is to
     be taken.
     36.6. OUTLINER’S PREPROCESSING PHASE                                                                                       257


     #include <i o s t r e a m >
     extern ”C” void O U T              1    14692       ( s i z e t ∗ np , s i z e t ∗ i p            ,   s i z e t ∗ rp   ,
                                                           i n t ∗EXIT TAKEN p ) ;

 5   s i z e t f a c t o r i a l ( s i z e t n)
     {
         s i z e t i = 1;
         s i z e t r = 1;
         while ( 1 )
10           {
                {
                  i n t EXIT TAKEN = 0 ;
                  OUT 1 14692               (&n , &i , &r , &EXIT TAKEN                );
                  i f ( EXIT TAKEN == 1 )
15                    {
     // Non−l o c a l jump #1
                        break ;
                      }
                  else
20                    {
                         i f ( EXIT TAKEN == 2 )
                             {
     // Non−l o c a l jump #2
                                break ;
25                           }
                        else
                             {
                             }
                      }
30              }
             }
         return r ;
     }

35   i n t main ( i n t a r g c , char ∗ a r g v [ ] )
     {
     // P r i n t s 5040
         ( ( ∗ ( & s t d : : c o u t ) << ” 7 ! == ” ) << f a c t o r i a l   ( 7 ) ) << s t d : : e n d l < char ,
              s t d : : c h a r t r a i t s < char >>;
40       return 0 ;
     }

     extern ”C” void O U T              1    14692       ( s i z e t ∗ np , s i z e t ∗ i p            ,   s i z e t ∗ rp   ,
                                                           i n t ∗EXIT TAKEN p )
45   {
       s i z e t & n = ∗(( s i z e t ∗) np               );
       s i z e t & i = ∗(( s i z e t ∗) i p              );
       s i z e t & r = ∗(( s i z e t ∗) r p              );
       i n t &EXIT TAKEN = ∗ ( ( i n t ∗ )               EXIT TAKEN p             );
50     i f ( i <= 1 )
           {
              EXIT TAKEN = 1 ;
              goto NON LOCAL EXIT ;
           }
55     e l s e i f ( i >= n )
           {
              EXIT TAKEN = 2 ;
              goto NON LOCAL EXIT ;
           }
60     else
           r ∗= ++i ;
     NON LOCAL EXIT : ;
     }



     Figure 36.14: rose outlined-inputCode OutlineNonLocalJumps.cc: Figure 36.12 after outlining.
258   CHAPTER 36. USING THE AST OUTLINER
Chapter 37

Loop Optimization

This section is specific to loop optimization and show several tutorial examples using the opti-
mization mechanisms within ROSE.                                                                       FIXME: We might want to
                                                                                                    reference Qing’s work explicitly
                                                                                                    since this is really just showing
37.1      Example Loop Optimizer                                                                                       off here work.


Simple example translator showing use of pre-defined loop optimizations.                              FIXME: We are not running
    Figure 37.1 shows the code required to call some loop optimizations within ROSE. The              performance tests within this
                                                                                                     tutorial, but perhaps we could
translator that we build for this tutorial is simple and takes the following command line options                             later.
to control which optimizations are done.

   -ic1 :loop interchange for more reuses
   -bk1/2/3 <blocksize> :block outer/inner/all loops
   -fs1/2 :single/multi-level loop fusion for more reuses
   -cp <copydim> :copy array
   -fs0 : loop fission
   -splitloop: loop splitting
   -unroll [locond] [nvar] <unrollsize> : loop unrolling
   -bs <stmtsize> : break up statements in loops
   -annot <filename>:
        Read annotation from a file which defines side effects of functions
   -arracc <funcname> :
        Use special function to denote array access (the special function can be replaced
        with macros after transformation). This option is for circumventing complex
        subscript expressions for linearized multi-dimensional arrays.
   -opt <level=0> : The level of loop optimizations to apply (By default, only the outermost
                 level is optimized).
   -ta <int> : Max number of nodes to split for transitive dependence analysis (to limit the
            overhead of transitive dep. analysis)
   -clsize <int> : set cache line size in evaluating spatial locality (affect decisions in
                applying loop optimizations)
   -reuse_dist <int> : set maximum distance of reuse that can exploit cache (used to evaluate

                                              259
260                                 CHAPTER 37. LOOP OPTIMIZATION

      temporal locality of loops)
     37.1. EXAMPLE LOOP OPTIMIZER                                                                                                                       261


     // LoopProcessor :
 2   //   Assume no a l i a s i n g
     //   apply loop opt to the bodies of                               all    function definitions
 4
         = = = = = = = = = = = = = = = = = =
     // = = = = = = = = = = = = = = = = = = =
 6
     #include ” r o s e . h”
 8
     #include <A s t I n t e r f a c e R O S E . h>
10   #include ” L o o p T r a n s f o r m I n t e r f a c e . h”
     #include ”CommandOptions . h”
12
     using namespace s t d ;
14
     int
16   main ( i n t a r g c ,         char ∗ a r g v [ ] )
         {
18         v e c t o r <s t r i n g > a r g v L i s t ( argv , a r g v + a r g c ) ;
           CmdOptions : : G e t I n s t a n c e ()−> S e t O p t i o n s ( a r g v L i s t ) ;
20         AssumeNoAlias a l i a s I n f o ;
           LoopTransformInterface : : cmdline configure ( argvList ) ;
22         L o o p T r a n s f o r m I n t e r f a c e : : s e t a l i a s I n f o (& a l i a s I n f o ) ;

24            S g P r o j e c t ∗ p r o j e c t = new S g P r o j e c t ( a r g v L i s t ) ;

26      // Loop o v e r t h e number o f f i l e s i n t h e p r o j e c t
           int filenum = p r o j e c t − umberOfFiles ( ) ;
                                                     >n
28         f o r ( i n t i = 0 ; i < f i l e n u m ; ++i )
                {
30                 SgSourceFile ∗ f i l e = isSgSourceFile ( project − g e t f i l e L i s t ( ) [ i ] ) ;
                                                                                                   >
                   SgGlobal ∗ r o o t = f i l e − e t g l o b a l S c o p e ( ) ;
                                                              >g
32                 S g D e c l a r a t i o n S t a t e m e n t P t r L i s t& d e c l L i s t = r o o t −>g e t d e c l a r a t i o n s   ();

34               // Loop o v e r t h e d e c l a r a t i o n i n t h e g l o b a l s c o p e o f each f i l e
                    f o r ( S g D e c l a r a t i o n S t a t e m e n t P t r L i s t : : i t e r a t o r p = d e c l L i s t . b e g i n ( ) ; p != d e c l L i s t . end ( ) ; ++p )
36                       {
                            SgFunctionDeclaration ∗ func = i s S g F u n c t i o n D e c l a r a t i o n (∗p ) ;
38                          i f ( f u n c == NULL)
                                    continue ;
40                          S g F u n c t i o n D e f i n i t i o n ∗ d e f n = f u n c−        >g e t d e f i n i t i o n ( ) ;
                            i f ( d e f n == NULL)
42                                  continue ;

44                              S g B a s i c B l o c k ∗ s t m t s = d e f n− e t b o d y ( ) ;
                                                                              >g
                                A s t I n t e r f a c e I m p l faImpl ( stmts ) ;
46
                          // This w i l l do as much f u s i o n as p o s s i b l e ( f i n e r g r a i n e d
48                        // c o n t r o l o v e r l o o p o p t i m i z a t i o n s u s e s a d i f f e r e n t i n t e r f a c e ) .
                             L o o p T r a n s f o r m I n t e r f a c e : : T r a n s f o r m T r a v e r s e ( f a I m p l , AstNodePtrImpl ( s t m t s ) ) ;
50
                          // JJW 10−29−2007 A d j u s t f o r i t e r a t o r i n v a l i d a t i o n and p o s s i b l e
52                        // i n s e r t e d s t a t e m e n t s
                             p = s t d : : f i n d ( d e c l L i s t . b e g i n ( ) , d e c l L i s t . end ( ) , f u n c ) ;
54                           a s s e r t ( p != d e c l L i s t . end ( ) ) ;
                           }
56                 }

58      // Generate s o u r c e code from AST and c a l l                           t h e vendor ’ s c o m p i l e r
           return backend ( p r o j e c t ) ;
60       }



                Figure 37.1: Example source code showing use of loop optimization mechanisms.
     262                                                                        CHAPTER 37. LOOP OPTIMIZATION

     37.2           Matrix Multiply Example
     Using the matrix multiply example code shown in figure 37.2, we run the loop optimizer in
     figure 37.1 and generate the code shown in figure 37.3.

     // Example program showing m a t r i x m u l t i p l y
 2   // ( f o r use w i t h l o o p o p t i m i z a t i o n t u t o r i a l example )

 4   #d e f i n e N 50

 6   i n t main ( )
          {
 8          int i , j , k ;
            double a [ N ] [ N ] , b [ N ] [ N ] , c [ N ] [ N ] ;
10
             f o r ( i = 0 ; i <= N−1; i +=1)
12                {
                     f o r ( j = 0 ; j <= N−1; j +=1)
14                        {
                             f o r ( k = 0 ; k <= N−1; k+=1)
16                                {
                                     c [ i ][ j ] = c [ i ][ j ] + a[ i ][ k] ∗ b[k ][ j ];
18                                }
                          }
20                }

22           return 0 ;
         }



                Figure 37.2: Example source code used as input to loop optimization processor.
     37.2. MATRIX MULTIPLY EXAMPLE                                                             263




 2   i n t min2 ( i n t a0 , i n t a1 )
     {
 4       return a0 < a1 ? a0 : a1 ;
     }
 6   // Example program showing m a t r i x m u l t i p l y
     // ( f o r use w i t h l o o p o p t i m i z a t i o n t u t o r i a l example )
 8   #d e f i n e N 50

10   i n t main ( )
     {
12       int i ;
         int j ;
14       int k ;
         double a [ 5 0UL ] [ 5 0 UL ] ;
16       double b [ 5 0UL ] [ 5 0 UL ] ;
         double c [ 5 0UL ] [ 5 0 UL ] ;
18       int   var 0 ;
         int   var 1 ;
20       for ( v a r 1 = 0 ;           v a r 1 <= 4 9 ;    v a r 1 += 1 6 ) {
           for ( v a r 0 = 0 ;            v a r 0 <= 4 9 ;     v a r 0 += 1 6 ) {
22           f o r ( k = 0 ; k <= 4 9 ; k += 1 ) {
                 f o r ( i = v a r 1 ; i <= min2 ( 4 9 , v a r 1 + 1 5 ) ; i += 1 ) {
24                   f o r ( j = v a r 0 ; j <= min2 ( 4 9 , v a r 0 + 1 5 ) ; j += 1 ) {
                         c [ i ] [ j ] = (c [ i ] [ j ] + (a [ i ] [ k] ∗ b[k ] [ j ] ) ) ;
26                   }
                 }
28           }
           }
30       }
         return 0 ;
32   }



     Figure 37.3: Output of loop optimization processor showing matrix multiply optimization (using
     options: -bk1 -fs0).
     264                                                             CHAPTER 37. LOOP OPTIMIZATION

     37.3            Loop Fusion Example
     Using the loop fusion example code shown in figure 37.4, we run the loop optimizer in figure 37.1
     and generate the code shown in figure 37.5.


 2
     main ( ) {
 4
         int x [ 3 0 ] ,    i;
 6
          for ( i =        1 ; i <= 1 0 ; i += 1 ) {
 8          x[2 ∗ i        ] = x[2 ∗ i + 1] + 2;
          }
10        for ( i =        1 ; i <= 1 0 ; i += 1 ) {
            x[2 ∗ i         + 3] = x[2 ∗ i ] + i ;
12        }

14   }



                  Figure 37.4: Example source code used as input to loop optimization processor.



 2   i n t main ( )
     {
 4       i n t x [ 3 0UL ] ;
         int i ;
 6       f o r ( i = 1 ; i <= 1 1 ; i += 1 ) {
             i f ( i <= 1 0 ) {
 8              x[2 ∗ i ] = (x [(2 ∗ i ) + 1] + 2);
             }
10           else {
             }
12           i f ( i >= 2 ) {
                x [ ( 2 ∗ (−1 + i ) ) + 3 ] = ( x [ 2 ∗ (−1 + i ) ] + (−1 + i ) ) ;
14           }
             else {
16           }
         }
18       return 0 ;
     }



     Figure 37.5: Output of loop optimization processor showing loop fusion (using options: -fs2).



     37.4            Example Loop Processor (LoopProcessor.C)
     This section contains a more detail translator which uses the command-line for input of specific
     loop processing options and is more sophisticated than the previous translator used to handle
     the previous two examples.
         Figure 37.6 shows the code required to call the loop optimizations within ROSE. The transla-
     tor that we build for this tutorial is simple and takes command line parameters to control which
     optimizations are done.
     37.4. EXAMPLE LOOP PROCESSOR (LOOPPROCESSOR.C)                                                                                             265




     #include ” r o s e . h”
 2   #include <g e n e r a l . h>

 4   #include ” p r e . h”
     #include ” f i n i t e D i f f e r e n c i n g . h”
 6

 8   // DQ ( 1 / 2 / 2 0 0 8 ) : I t h i n k t h i s         i s no l o n g e r used !
     // #i n c l u d e ” c o p y u n p a r s e r . h”
10
     #include       ” r e w r i t e . h”
12   #include       <CommandOptions . h>
     #include       <A s t I n t e r f a c e R O S E . h>
14   #include       <L o o p T r a n s f o r m I n t e r f a c e . h>
     #include       <A n n o t C o l l e c t . h>
16   #include       <O p e r a t o r A n n o t a t i o n . h>

18   using namespace s t d ;

20   #i f d e f USE OMEGA
     #include <D e p T e s t S t a t i s t i c s . h>
22
     extern D e p T e s t S t a t i s t i c s D e p S t a t s ;
24   #e n d i f

26   extern bool DebugAnnot ( ) ;
     extern void F i x F i l e I n f o ( SgNode∗ n ) ;
28   c l a s s UnparseFormatHelp ;
     class UnparseDelegate ;
30   void u n p a r s e P r o j e c t ( S g P r o j e c t ∗ p r o j e c t , UnparseFormatHelp ∗ u n p a r s e H e l p /∗= NULL∗/ , U n p a r s e D e l e g a t e ∗ r e p l
     /∗= NULL ∗/ ) ;

32   void P r i n t U s a g e ( char ∗ name )
     {
34     c e r r << name << ” <o p t i o n s > ” << ”<program name>” << ” \n” ;
       c e r r << ”−g o b j : g e n e r a t e o b j e c t f i l e \n” ;
36     c e r r << ”−o r i g : copy non−m o d i f i e d s t a t e m e n t s from o r i g i n a l f i l e \n” ;
       c e r r << ”− s p l i t l o o p : a p p l y i n g l o o p s p l i t t i n g t o remove c o n d i t i o n a l s i n s i d e   l o o p s \n” ;
38     c e r r << ReadAnnotation : : g e t i n s t ()−> O p t i o n S t r i n g ( ) << e n d l ;
       c e r r << ”−p r e :       a p p l y p a r t i a l redundancy e l i m i n a t i o n \n” ;
40     c e r r << ”−f d :       a p p l y f i n i t e d i f f e r e n c i n g t o a r r a y i n d e x e x p r e s s i o n s \n” ;
       LoopTransformInterface : : PrintTransformUsage ( c e r r ) ;
42   }



     Figure 37.6: Detailed example source code showing use of loop optimization mechanisms (loop-
     Processor.C part 1).
     266                                                                                       CHAPTER 37. LOOP OPTIMIZATION




 2   bool GenerateObj ( )
     {
 4     return CmdOptions : : G e t I n s t a n c e ()−>HasOption ( ”−g o b j ” ) ;
     }
 6

 8   int
     main ( i n t a r g c ,          char ∗ a r g v [ ]        )
10   {

12       if   ( a r g c <= 1 ) {
                PrintUsage ( argv [ 0 ] ) ;
14              return −1;
        }
16      v e c t o r <s t r i n g > a r g v L i s t ( argv , a r g v + a r g c ) ;
        CmdOptions : : G e t I n s t a n c e ()−> S e t O p t i o n s ( a r g v L i s t ) ;
18      AssumeNoAlias a l i a s I n f o ;
        LoopTransformInterface : : cmdline configure ( argvList ) ;
20      L o o p T r a n s f o r m I n t e r f a c e : : s e t a l i a s I n f o (& a l i a s I n f o ) ;

22   #i f d e f USE OMEGA
        D e p S t a t s . SetFileName ( b u f f e r . s t r ( ) ) ;
24   #e n d i f

26       OperatorSideEffectAnnotation ∗ funcInfo =
                       OperatorSideEffectAnnotation : : g e t i n s t ( ) ;
28       funcInfo− egister annot ( ) ;
                          >r
         ReadAnnotation : : g e t i n s t ()−> r e a d ( ) ;
30       i f ( DebugAnnot ( ) )
              funcInfo−       >Dump ( ) ;
32       LoopTransformInterface : : s e t s i d e E f f e c t I n f o ( funcInfo ) ;
         S g P r o j e c t ∗ p r o j e c t = new S g P r o j e c t ( a r g v L i s t ) ;
34
         int filenum = p r o j e c t − umberOfFiles ( ) ;
                                                    >n
36       f o r ( i n t i = 0 ; i < f i l e n u m ; ++i ) {
        // S g F i l e &s a g e F i l e = s a g e P r o j e c t − g e t f i l e ( i ) ;
                                                                        >
38      // S g G l o b a l ∗ r o o t = s a g e F i l e . g e t r o o t ( ) ;
             SgSourceFile ∗ f i l e = isSgSourceFile ( project − g e t f i l e L i s t ( ) [ i ] ) ;
                                                                                                  >
40           SgGlobal ∗ r o o t = f i l e − e t g l o b a l S c o p e ( ) ;
                                                        >g
             S g D e c l a r a t i o n S t a t e m e n t P t r L i s t& d e c l L i s t = r o o t −  >g e t d e c l a r a t i o n s ( ) ;
42           f o r ( S g D e c l a r a t i o n S t a t e m e n t P t r L i s t : : i t e r a t o r p = d e c l L i s t . b e g i n ( ) ; p != d e c l L i s t . end ( ) ; ++p ) {



                                          Figure 37.7: loopProcessor.C source code (Part 2).
     37.5. MATRIX MULTIPLICATION EXAMPLE (MM.C)                                                    267

     37.5          Matrix Multiplication Example (mm.C)
     Using the matrix multiplication example code shown in figure 37.8, we run the loop optimizer
     in figure 37.6 and generate the code shown in figure 37.9.


 2   #d e f i n e N 50

 4   void p r i n t m a t r i x ( double x [ ] [ N ] ) ;
     void i n i t m a t r i x ( double x [ ] [ N ] , double s ) ;
 6
     main ( )
 8   {
       int i , j , k ;
10     double a [ N ] [ N] , b [ N ] [ N ] , c [ N ] [ N ] ;

12       double s ;
         s = 235.0;
14       initmatrix (a , s );
         s = 321.0;
16       initmatrix (b , s ) ;

18       printmatrix (a ) ;
         printmatrix (b ) ;
20       f o r ( i = 0 ; i <= N−1; i +=1) {
             f o r ( j = 0 ; j <= N−1; j +=1) {
22                f o r ( k = 0 ; k <= N−1; k+=1) {
                       c [ i ][ j ] = c [ i ][ j ] + a[ i ][ k] ∗ b[k ][ j ];
24                }
             }
26       }

28       printmatrix ( c ) ;
     }



            Figure 37.8: Example source code used as input to loopProcessor, show in figure 37.6.
     268                                                                 CHAPTER 37. LOOP OPTIMIZATION




 2   i n t min2 ( i n t a0 , i n t a1 )
     {
 4       return a0 < a1 ? a0 : a1 ;
     }
 6   #d e f i n e N 50
     void p r i n t m a t r i x ( double x [ ] [ 5 0 UL ] ) ;
 8   void i n i t m a t r i x ( double x [ ] [ 5 0 UL ] , double s ) ;

10   i n t main ( )
     {
12       int i ;
         int j ;
14       int k ;
         double a [ 5 0UL ] [ 5 0 UL ] ;
16       double b [ 5 0UL ] [ 5 0 UL ] ;
         double c [ 5 0UL ] [ 5 0 UL ] ;
18       double s ;
         int   var 0 ;
20       int   var 1 ;
         s = 235.0;
22       initmatrix (a , s );
         s = 321.0;
24       initmatrix (b , s ) ;
         printmatrix (a ) ;
26       printmatrix (b ) ;
         for ( v a r 1 = 0 ;           v a r 1 <= 4 9 ;    v a r 1 += 1 6 ) {
28         for ( v a r 0 = 0 ;            v a r 0 <= 4 9 ;     v a r 0 += 1 6 ) {
             f o r ( k = 0 ; k <= 4 9 ; k += 1 ) {
30               f o r ( i = v a r 1 ; i <= min2 ( 4 9 , v a r 1 + 1 5 ) ; i += 1 ) {
                     f o r ( j = v a r 0 ; j <= min2 ( 4 9 , v a r 0 + 1 5 ) ; j += 1 ) {
32                       c [ i ] [ j ] = (c [ i ] [ j ] + (a [ i ] [ k] ∗ b[k ] [ j ] ) ) ;
                     }
34               }
             }
36         }
         }
38       printmatrix ( c ) ;
         return 0 ;
40   }



     Figure 37.9: Output of loopProcessor using input from figure 37.8 (using options: -bk1 -fs0).
     37.6. MATRIX MULTIPLICATION EXAMPLE USING LINEARIZED MATRICES (DGEMM.C)269

     37.6          Matrix Multiplication Example Using Linearized Ma-
                   trices (dgemm.C)
     Using the matrix multiplication example code shown in figure 37.10, we run the loop optimizer
     in figure 37.6 and generate the code shown in figure 37.11.


 2   // Function p r o t o t y p e
     void dgemm( double ∗a , double ∗b , double ∗ c , i n t n ) ;
 4
     // Function d e f i n i t i o n
 6   void dgemm( double ∗a , double ∗b , double ∗ c , i n t n )
        {
 8        int i , j , k ;

10     // i n t n ;

12          f o r ( k =0;k<n ; k+=1){
                f o r ( j =0; j <n ; j +=1){
14                  f o r ( i =0; i <n ; i +=1){
                        c [ j ∗n+i ]= c [ j ∗n+i ]+ a [ k∗n+i ] ∗ b [ j ∗n+k ] ;
16                  }
                }
18          }
        }



         Figure 37.10: Example source code used as input to loopProcessor, show in figure 37.6.
     270                                                            CHAPTER 37. LOOP OPTIMIZATION




 2   i n t min2 ( i n t a0 , i n t a1 )
     {
 4       return a0 < a1 ? a0 : a1 ;
     }
 6
     i n t min2 ( i n t a0 , i n t a1 )
 8   {
         return a0 < a1 ? a0 : a1 ;
10   }

12   i n t min2 ( i n t a0 , i n t a1 )
     {
14       return a0 < a1 ? a0 : a1 ;
     }
16   // Function p r o t o t y p e
     void dgemm( double ∗a , double ∗b , double ∗ c , i n t n ) ;
18   // Function d e f i n i t i o n

20   void dgemm( double ∗a , double ∗b , double ∗ c , i n t n )
     {
22     int i ;
       int j ;
24     int k ;
       int   var 0 ;
26     int   var 1 ;
       for ( v a r 1 = 0 ;         v a r 1 <= −1 + n ;    v a r 1 += 1 6 ) {
28       for ( v a r 0 = 0 ;          v a r 0 <= −1 + n ;     v a r 0 += 1 6 ) {
           f o r ( i = 0 ; i <= −1 + n ; i += 1 ) {
30             f o r ( k = v a r 1 ; k <= min2(−1 + n , v a r 1 + 1 5 ) ; k += 1 ) {
                   f o r ( j = v a r 0 ; j <= : : min2 ( n + −16 , v a r 0 ) ; j += 1 6 ) {
32                     c [ ( j ∗ n) + i ] = ( c [ ( j ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗ b [ ( j ∗   n) +   k]));
                       c [ ( ( 1 + j ) ∗ n) + i ] = ( c [ ( ( 1 + j ) ∗ n) + i ] + (a [ ( k ∗ n) +   i] ∗   b[((1 +    j)   ∗ n)   + k]));
34                     c [ ( ( 2 + j ) ∗ n) + i ] = ( c [ ( ( 2 + j ) ∗ n) + i ] + (a [ ( k ∗ n) +   i] ∗   b[((2 +    j)   ∗ n)   + k]));
                       c [ ( ( 3 + j ) ∗ n) + i ] = ( c [ ( ( 3 + j ) ∗ n) + i ] + (a [ ( k ∗ n) +   i] ∗   b[((3 +    j)   ∗ n)   + k]));
36                     c [ ( ( 4 + j ) ∗ n) + i ] = ( c [ ( ( 4 + j ) ∗ n) + i ] + (a [ ( k ∗ n) +   i] ∗   b[((4 +    j)   ∗ n)   + k]));
                       c [ ( ( 5 + j ) ∗ n) + i ] = ( c [ ( ( 5 + j ) ∗ n) + i ] + (a [ ( k ∗ n) +   i] ∗   b[((5 +    j)   ∗ n)   + k]));
38                     c [ ( ( 6 + j ) ∗ n) + i ] = ( c [ ( ( 6 + j ) ∗ n) + i ] + (a [ ( k ∗ n) +   i] ∗   b[((6 +    j)   ∗ n)   + k]));
                       c [ ( ( 7 + j ) ∗ n) + i ] = ( c [ ( ( 7 + j ) ∗ n) + i ] + (a [ ( k ∗ n) +   i] ∗   b[((7 +    j)   ∗ n)   + k]));
40                     c [ ( ( 8 + j ) ∗ n) + i ] = ( c [ ( ( 8 + j ) ∗ n) + i ] + (a [ ( k ∗ n) +   i] ∗   b[((8 +    j)   ∗ n)   + k]));
                       c [ ( ( 9 + j ) ∗ n) + i ] = ( c [ ( ( 9 + j ) ∗ n) + i ] + (a [ ( k ∗ n) +   i] ∗   b[((9 +    j)   ∗ n)   + k]));
42                     c [((10 + j ) ∗ n) + i ] = ( c [((10 + j ) ∗ n) + i ] + (a [ ( k ∗ n)         + i]   ∗ b[((10    +   j) ∗   n) + k ] ) ) ;
                       c [((11 + j ) ∗ n) + i ] = ( c [((11 + j ) ∗ n) + i ] + (a [ ( k ∗ n)         + i]   ∗ b[((11    +   j) ∗   n) + k ] ) ) ;
44                     c [((12 + j ) ∗ n) + i ] = ( c [((12 + j ) ∗ n) + i ] + (a [ ( k ∗ n)         + i]   ∗ b[((12    +   j) ∗   n) + k ] ) ) ;
                       c [((13 + j ) ∗ n) + i ] = ( c [((13 + j ) ∗ n) + i ] + (a [ ( k ∗ n)         + i]   ∗ b[((13    +   j) ∗   n) + k ] ) ) ;
46                     c [((14 + j ) ∗ n) + i ] = ( c [((14 + j ) ∗ n) + i ] + (a [ ( k ∗ n)         + i]   ∗ b[((14    +   j) ∗   n) + k ] ) ) ;
                       c [((15 + j ) ∗ n) + i ] = ( c [((15 + j ) ∗ n) + i ] + (a [ ( k ∗ n)         + i]   ∗ b[((15    +   j) ∗   n) + k ] ) ) ;
48                 }
                   f o r ( ; j <= : : min2(−1 + n , 1 5 + v a r 0 ) ; j += 1 ) {
50                     c [ ( j ∗ n) + i ] = ( c [ ( j ∗ n) + i ] + (a [ ( k ∗ n) + i ] ∗ b [ ( j ∗   n) + k ] ) ) ;
                   }
52             }
           }
54       }
       }
56   }



     Figure 37.11: Output of loopProcessor using input from figure 37.10 (using options: -bk1
     -unroll nvar 16).
     37.7. LU FACTORIZATION EXAMPLE (LUFAC.C)                                                     271

     37.7           LU Factorization Example (lufac.C)
     Using the LU factorization example code shown in figure 37.12, we run the loop optimizer in
     figure 37.6 and generate the code shown in figure 37.13.

     double abs ( double x ) { i f               ( x < 0 ) return −x ; e l s e return x ; }
 2
     #d e f i n e n 50
 4   void p r i n t m a t r i x ( double x [ ] [ n ] ) ;
     void i n i t m a t r i x ( double x [ ] [ n ] , double s ) ;
 6
     main ( i n t a r g c , char ∗ a r g v [ ] ) {
 8   int p [ n ] , i , j , k ;
     double a [ n ] [ n ] , mu, t ;
10
     initmatrix (a , 5.0);
12   printmatrix (a ) ;

14   f o r ( k = 0 ; k<=n −2; k+=1) {
           p[k] = k;
16         mu = abs ( a [ k ] [ k ] ) ;
           f o r ( i = k +1; i <= n −1; i +=1) {
18             i f (mu < abs ( a [ i ] [ k ] ) ) {
                  mu = abs ( a [ i ] [ k ] ) ;
20                p[k] = i ;
               }
22         }

24         f o r ( j = k ; j <= n −1; j +=1) {
                t = a[k][ j ];
26              a[k ][ j ] = a[p[k ] ] [ j ];
                a[p[k ] ] [ j ] = t ;
28         }

30         f o r ( i = k +1; i <= n −1; i +=1) {
                     a [ i ] [ k ] = a [ i ] [ k]/ a [ k ] [ k ] ;
32         }
           f o r ( j = k +1; j <=n −1; j +=1) {
34              f o r ( i = k +1; i <=n −1; i +=1) {
                         a [ i ] [ j ] = a [ i ] [ j ] − a [ i ] [ k]∗ a [ k ] [ j ] ;
36              }
           }
38    }

40   printmatrix (a ) ;
     }



          Figure 37.12: Example source code used as input to loopProcessor, show in figure 37.6.
     272                                                                              CHAPTER 37. LOOP OPTIMIZATION




 2   double abs ( double x )
     {
 4      i f ( x < 0)
            return −x ;
 6     else
            return x ;
 8   }
     #d e f i n e n 50
10   void p r i n t m a t r i x ( double x [ ] [ 5 0 UL ] ) ;
     void i n i t m a t r i x ( double x [ ] [ 5 0 UL ] , double s ) ;
12
     i n t main ( i n t a r g c , char ∗ a r g v [ ] )
14   {
         i n t p [ 5 0UL ] ;
16       int i ;
         int j ;
18       int k ;
         double a [ 5 0UL ] [ 5 0 UL ] ;
20       double mu ;
         double t ;
22       initmatrix (a , 5 . 0 ) ;
         printmatrix (a ) ;
24       f o r ( k = 0 ; k <= 4 8 ; k += 1 ) {
             p[k] = k;
26          mu = a bs ( a [ k ] [ k ] ) ;
             f o r ( i = 1 + k ; i <= 4 9 ; i += 1 ) {
28               i f (mu < a bs ( a [ i ] [ k ] ) ) {
                    mu = ab s ( a [ i ] [ k ] ) ;
30                   p[k] = i ;
                 }
32           }
             f o r ( j = k ; j <= 4 9 ; j += 1 ) {
34               t = a[k][ j ];
                 a[k ][ j ] = a[p[k ] ] [ j ];
36               a[p[k ] ] [ j ] = t ;
             }
38           f o r ( i = 1 + k ; i <= 4 9 ; i += 1 ) {
                 a [ i ] [ k] = (a [ i ] [ k] / a [k ] [ k ] ) ;
40           }
             f o r ( j = 1 + k ; j <= 4 9 ; j += 1 ) {
42               f o r ( i = 1 + k ; i <= 4 9 ; i += 1 ) {
                     a [ i ] [ j ] = (a [ i ] [ j ] − (a [ i ] [ k] ∗ a [k ] [ j ] ) ) ;
44               }
             }
46       }
         printmatrix (a ) ;
48       return 0 ;
     }



     Figure 37.13: Output of loopProcessor using input from figure 37.12 (using options: -bk1 -fs0
     -splitloop -annotation).
     37.8. LOOP FUSION EXAMPLE (TRIDVPK.C)                                                                                                      273

     37.8            Loop Fusion Example (tridvpk.C)
     Using the loop fusion example code shown in figure 37.14, we run the loop optimizer in figure 37.6
     and generate the code shown in figure 37.15.

     #d e f i n e n 100
 2
         double a [ n ] , b [ n ] , c [ n ] , d [ n ] , e [ n ] ;
 4       double t o t [ n ] [ n ] ;
         double dux [ n ] [ n ] [ n ] , duy [ n ] [ n ] [ n ] , duz [ n ] [ n ] [ n ] ;
 6
     main ( )
 8   {
       int i , j , k ;
10            f o r ( j =0; j <=n −1; j +=1)
              f o r ( i =0; i <=n −1; i +=1)
12                 duz [ i ] [ j ] [ 0 ] = duz [ i ] [ j ] [ 0 ] ∗ b [ 0 ] ;

14             f o r ( k =1; k<=n −2; k+=1)
               f o r ( j = 0 ; j <= n −1; j +=1)
16             f o r ( i =0; i <=n −1; i +=1)
                    duz [ i ] [ j ] [ k ] = ( duz [ i ] [ j ] [ k]−a [ k ] ∗ duz [ i ] [ j ] [ k −1 ] )∗ b [ k ] ;
18
               f o r ( j =0; j <=n −1; j +=1)
20             f o r ( i =0; i <=n −1; i +=1)
                    tot [ i ] [ j ] = 0;
22
               f o r ( k =0; k<=n −2; k+=1)
24             f o r ( j =0; j <=n −1; j +=1)
               f o r ( i =0; i <=n −1; i +=1)
26                     t o t [ i ] [ j ] = t o t [ i ] [ j ] + d [ k ] ∗ duz [ i ] [ j ] [ k ] ;

28             f o r ( j =0; j <=n −1; j +=1)
               f o r ( i =0; i <=n −1; i +=1)
30                  duz [ i ] [ j ] [ n −1] = ( duz [ i ] [ j ] [ n −1] − t o t [ i ] [ j ] ) ∗ b [ n − 1 ] ;

32             f o r ( j =0; j <=n −1; j +=1)
               f o r ( i =0; i <=n −1; i +=1)
34                  duz [ i ] [ j ] [ n−2]=duz [ i ] [ j ] [ n −2] − e [ n −2]∗ duz [ i ] [ j ] [ n − 1 ] ;

36             f o r ( k=n −3; k>=0; k+=−1)
               f o r ( j = 0 ; j <= n −1; j +=1)
38             f o r ( i =0; i <=n −1; i +=1)
                    duz [ i ] [ j ] [ k ] = duz [ i ] [ j ] [ k ] − c [ k ] ∗ duz [ i ] [ j ] [ k +1] − e [ k ] ∗ duz [ i ] [ j ] [ n − 1 ] ;
40
     }



           Figure 37.14: Example source code used as input to loopProcessor, show in figure 37.6.
     274                                                                            CHAPTER 37. LOOP OPTIMIZATION




     #d e f i n e n 100
 2   double a [ 1 0 0UL ] ;
     double b [ 1 0 0UL ] ;
 4   double c [ 1 0 0UL ] ;
     double d [ 1 0 0UL ] ;
 6   double e [ 1 0 0UL ] ;
     double t o t [ 1 0 0UL ] [ 1 0 0 UL ] ;
 8   double dux [ 1 0 0UL ] [ 1 0 0 UL ] [ 1 0 0 UL ] ;
     double duy [ 1 0 0UL ] [ 1 0 0 UL ] [ 1 0 0 UL ] ;
10   double duz [ 1 0 0UL ] [ 1 0 0 UL ] [ 1 0 0 UL ] ;

12   i n t main ( )
     {
14       int i ;
         int j ;
16       int k ;
         f o r ( i = 0 ; i <= 9 9 ; i += 1 ) {
18           f o r ( j = 0 ; j <= 9 9 ; j += 1 ) {
                 duz [ i ] [ j ] [ 0 ] = ( duz [ i ] [ j ] [ 0 ] ∗ b [ 0 ] ) ;
20               tot [ i ] [ j ] = 0;
                 f o r ( k = 0 ; k <= 9 8 ; k += 1 ) {
22                   i f ( k >= 1 ) {
                         duz [ i ] [ j ] [ k ] = ( ( duz [ i ] [ j ] [ k ] − ( a [ k ] ∗ duz [ i ] [ j ] [ k − 1 ] ) ) ∗ b [ k ] ) ;
24                   }
                     else {
26                   }
                     t o t [ i ] [ j ] = ( t o t [ i ] [ j ] + ( d [ k ] ∗ duz [ i ] [ j ] [ k ] ) ) ;
28               }
                 duz [ i ] [ j ] [ 1 0 0 − 1 ] = ( ( duz [ i ] [ j ] [ 1 0 0 − 1 ] − t o t [ i ] [ j ] ) ∗ b [ 1 0 0 − 1 ] ) ;
30               duz [ i ] [ j ] [ 1 0 0 − 2 ] = ( duz [ i ] [ j ] [ 1 0 0 − 2 ] − ( e [ 1 0 0 − 2 ] ∗ duz [ i ] [ j ] [ 1 0 0 − 1 ] ) ) ;
                 f o r ( k = 9 7 ; k >= 0 ; k += −1) {
32                   duz [ i ] [ j ] [ k ] = ( ( duz [ i ] [ j ] [ k ] − ( c [ k ] ∗ duz [ i ] [ j ] [ k + 1 ] ) ) − ( e [ k ] ∗ duz [ i ] [ j ] [ 1 0 0 − 1 ] ) ) ;
                 }
34           }
         }
36       return 0 ;
     }



     Figure 37.15: Output of loopProcessor input from figure 37.14 (using options: -fs2 -ic1 -opt
     1 ).
Chapter 38

Parameterized Code Translation

This chapter gives examples of using ROSE’s high level loop translation interfaces to perform
parameterized loop transformations, including loop unrolling, interchanging and tiling. The
motivation is to give users the maximized flexibility to orchestrate code transformations on the
targets they want, the order they want, and the parameters they want. One typical application
scenario is to support generating desired code variants for empirical tuning.
   The ROSE internal interfaces (declared within the SageInterface namespace) to call loop
transformations are:

   • bool loopUnrolling (SgForStatement *loop, size t unrolling factor): This function needs two
     parameters: one for the loop to be unrolled and the other for the unrolling factor.

   • bool loopInterchange (SgForStatement *loop, size t depth, size t lexicoOrder): The loop
     interchange function has three parameters, the first one to specify a loop which starts a
     perfectly-nested loop and is to be interchanged, the 2nd for the depth of the loop nest to
     be interchanged, and finally the lexicographical order for the permutation.

   • bool loopTiling (SgForStatement *loopNest, size t targetLevel, size t tileSize) The loop
     tiling interface needs to know the loop nest to be tiled, which loop level to tile, and
     the tile size for the level.

For efficiency concerns, those functions only perform the specified translations without doing any
legitimacy check. It is up to the users to make sure the transformations won’t generate wrong
code. We will soon provide interfaces to do the eligibility check for each transformation.
    We also provide standalone executable programs (loopUnrolling,loopInterchange, and loop-
Tiling under ROSE INSTALL/bin) for the transformations mentioned above so users can directly
use them via command lines and abstract handles (explained in Chapter 45) to orchestrate trans-
formations they want.


38.1      Loop Unrolling
Figure 38.1 gives an example input code for loopUnrolling.
   An example command line to invoke loop unrolling on the example can look like the following:

                                              275
     276                                     CHAPTER 38. PARAMETERIZED CODE TRANSLATION


     int a [ 1 0 0 ] [ 1 0 0 ] ;
 2   i n t main ( void )
     {
 4       int j ;
         f o r ( i n t i =0; i <100; i ++)
 6           f o r ( j =0; j <100; j ++)
             {
 8               i n t k =3;
                 a [ i ] [ j ]= i+j+k ;
10           }
         return 0 ;
12   }



                        Figure 38.1: Example source code used as input to loopUnrolling


     # unroll a for statement 5 times. The loop is a statement at line 6 within
     # an input file.
     loopUnrolling -c inputloopUnrolling.C \
     -rose:loopunroll:abstract_handle "Statement<position,6>" -rose:loopunroll:factor 5

         Two kinds of output can be expected after loop unrolling. One (Shown in Figure 38.2) is the
     case that the loop iteration count is known at compile-time and can be evenly divisible by the
     unrolling factor. The other case (Shown in Figure 38.3 is when the divisibility is unknown and
     a fringe loop has to be generated to run possible leftover iterations.
     38.1. LOOP UNROLLING                                                                            277




     i n t a [ 1 0 0UL ] [ 1 0 0 UL ] ;
 2
     int
 4   main ( )
     {
 6     int j ;
       f o r ( i n t i = 0 ; i < 1 0 0 ; i ++)
 8         {
             f o r ( j = 0 ; j <= 9 9 ; j += 5 )
10               {
                    int k = 3 ;
12                  a [ i ] [ j ] = (( i + j ) + k );
                    {
14                      int k = 3 ;
                        a [ i ] [ ( j + 1)] = (( i + ( j   + 1)) + k ) ;
16                  }
                    {
18                      int k = 3 ;
                        a [ i ] [ ( j + 2)] = (( i + ( j   + 2)) + k ) ;
20                  }
                    {
22                      int k = 3 ;
                        a [ i ] [ ( j + 3)] = (( i + ( j   + 3)) + k ) ;
24                  }
                    {
26                      int k = 3 ;
                        a [ i ] [ ( j + 4)] = (( i + ( j   + 4)) + k ) ;
28                  }
                 }
30         }
       return 0 ;
32   }



            Figure 38.2: Output for a unrolling factor which can divide the iteration space evenly
     278                                         CHAPTER 38. PARAMETERIZED CODE TRANSLATION




     i n t a [ 1 0 0UL ] [ 1 0 0 UL ] ;
 2
     int
 4   main ( )
     {
 6     int j ;
       f o r ( i n t i = 0 ; i < 1 0 0 ; i ++)
 8         {
     // i t e r c o u n t = ( ub−l b +1)%s t e p ==0?(ub−l b +1)/ s t e p : ( ub−l b +1)/ s t e p +1;
10   // f r i n g e = i t e r c o u n t%u n r o l l f a c t o r==0 ? 0 : u n r o l l f a c t o r ∗ s t e p
              int     lu fringe 1 = 3;
12            f o r ( j = 0 ; j <= 99 − l u f r i n g e 1 ; j += 3 )
                  {
14                  int k = 3 ;
                    a [ i ] [ j ] = (( i + j ) + k );
16                  {
                        int k = 3 ;
18                      a [ i ] [ ( j + 1)] = (( i + ( j + 1)) + k ) ;
                    }
20                  {
                        int k = 3 ;
22                      a [ i ] [ ( j + 2)] = (( i + ( j + 2)) + k ) ;
                    }
24                }
              f o r ( ; j <= 9 9 ; j += 1 )
26                {
                    int k = 3 ;
28                  a [ i ] [ j ] = (( i + j ) + k );
                  }
30         }
       return 0 ;
32   }



                  Figure 38.3: Output for the case when divisibility is unknown at compile-time
     38.2. LOOP INTERCHANGE                                                                                            279

     38.2         Loop Interchange
     Figure 38.4 gives an example input code for loopInterchange.

     void O U T 1 6 1 1 9 ( i n t r i , double ∗ rp , i n t s t e n c i l s i z e , i n t hypre      m , const double ∗ Ap 0 )
 2   {
       i n t s i , i i , j j , kk ;
 4     // t h e f o l l o w i n g 4− l e v e l l o o p n e s t i s t o be i n t e r c h a n g e d
       f o r ( s i = 0 ; s i < s t e n c i l s i z e ; s i ++)
 6         f o r ( kk = 0 ; kk < hypr e m ; kk++)
               f o r ( j j = 0 ; j j < hypr e m ; j j ++)
 8                 f o r ( i i = 0 ; i i < hypr e m ; i i ++)
                       rp [ ( r i + i i ) + j j + kk ] −= Ap 0 [ i i + j j + kk ] ;
10   }



                      Figure 38.4: Example source code used as input to loopInterchange

         An example command line to invoke loop interchange:

     # interchange a loop nest starting from the first loop within the input file,
     # interchange depth is 4 and
     # the lexicographical order is 1 (swap the innermost two levels)
     loopInterchange -c inputloopInterchange.C -rose:loopInterchange:abstract_handle \
     "ForStatement<numbering,1>" -rose:loopInterchange:depth 4 \
     -rose:loopInterchange:order 1

         Figure 38.5 shows the output.


 2   void O U T 1 6 1 1 9 ( i n t r i , double ∗ rp , i n t s t e n c i l s i z e , i n t hypre   m , const double ∗ Ap 0 )
     {
 4     int s i ;
       int i i ;
 6     int j j ;
       i n t kk ;
 8   // t h e f o l l o w i n g 4− l e v e l l o o p n e s t i s t o be i n t e r c h a n g e d
       f o r ( s i = 0 ; s i < s t e n c i l s i z e ; s i ++)
10         f o r ( kk = 0 ; kk < hypr e m ; kk++)
               f o r ( i i = 0 ; i i < hypr e m ; i i ++)
12                 f o r ( j j = 0 ; j j < hypr e m ; j j ++)
                       rp [ ( ( r i + i i ) + j j ) + kk ] −= Ap 0 [ ( i i + j j ) + kk ] ;
14   }



                                        Figure 38.5: Output for loop interchange
     280                                                     CHAPTER 38. PARAMETERIZED CODE TRANSLATION

     38.3             Loop Tiling
     Figure 38.6 gives an example input code for loopTiling.

     #d e f i n e N 100
 2   int i , j , k ;
     double a [ N ] [ N ] , b [ N ] [ N ] , c [ N ] [ N ] ;
 4
     i n t main ( )
 6   {
         f o r ( i = 0 ; i < N ; i ++)
 8           f o r ( j = 0 ; j < N ; j ++)
                 f o r ( k = 0 ; k < N ; k++)
10                   c [ i ] [ j ]= c [ i ] [ j ]+ a [ i ] [ k ] ∗ b [ k ] [ j ] ;
         return 0 ;
12   }



                                 Figure 38.6: Example source code used as input to loopTiling

           An example command line to invoke loop tiling:
     # Tile the loop with a depth of 3 within the first loop of the input file
     # tile size is 5
     loopTiling -c inputloopTiling.C -rose:loopTiling:abstract_handle \
     "ForStatement<numbering,1>" -rose:loopTiling:depth 3 -rose:loopTiling:tilesize 5


     Figure 38.7 shows the output.

     #d e f i n e N 100
 2   int i ;
     int j ;
 4   int k ;
     double a [ 1 0 0UL ] [ 1 0 0 UL ] ;
 6   double b [ 1 0 0UL ] [ 1 0 0 UL ] ;
     double c [ 1 0 0UL ] [ 1 0 0 UL ] ;
 8
     i n t main ( )
10   {
         int     lt var k ;
12       for ( l t v a r k = 0 ;            l t v a r k <= 9 9 ;      l t v a r k += 5 ) {
           f o r ( i = 0 ; i < 1 0 0 ; i ++)
14             f o r ( j = 0 ; j < 1 0 0 ; j ++)
                   f o r ( k = l t v a r k ; k <= ( ( ( 9 9 < ( l t v a r k + 5 − 1 ) ) ? 9 9   : ( l t v a r k + 5 − 1 ) ) ) ; k += 1 ) {
16                     c [ i ] [ j ] = (c [ i ] [ j ] + (a [ i ] [ k] ∗ b[k ] [ j ] ) ) ;
                   }
18       }
         return 0 ;
20   }



                                                         Figure 38.7: Output for loop tiling
                              Part V

           Correctness Checking


Tutorials of using ROSE to help program correctness checking or debugging.




                                   281
Chapter 39

Code Coverage

This translator is part of ongoing collaboration with IBM on the support of code coverage analysis
tools for C, C++ and F90 applications. the subject of code coverage is much more complex than
this example code would cover. The following web site: http://www.bullseye.com/coverage.html
contains more information and is the source for the descriptions below. Code coverage can
include:

   • Statement Coverage
     This measure reports whether each executable statement is encountered.

   • Decision Coverage
     This measure reports whether boolean expressions tested in control structures (such as the
     if-statement and while-statement) evaluated to both true and false. The entire boolean
     expression is considered one true-or-false predicate regardless of whether it contains logical-
     and or logical-or operators. Additionally, this measure includes coverage of switch-statement
     cases, exception handlers, and interrupt handlers.

   • Condition Coverage
     Condition coverage reports the true or false outcome of each boolean sub-expression, sep-
     arated by logical-and and logical-or if they occur. Condition coverage measures the sub-
     expressions independently of each other.

   • Multiple Condition Coverage
     Multiple condition coverage reports whether every possible combination of boolean sub-
     expressions occurs. As with condition coverage, the sub-expressions are separated by
     logical-and and logical-or, when present. The test cases required for full multiple condition
     coverage of a condition are given by the logical operator truth table for the condition.

   • Condition/Decision Coverage
     Condition/Decision Coverage is a hybrid measure composed by the union of condition
     coverage and decision coverage. This measure was created at Boeing and is required for
     aviation software by RCTA/DO-178B.

                                               283
284                                                            CHAPTER 39. CODE COVERAGE

      • Modified Condition/Decision Coverage
        This measure requires enough test cases to verify every condition can affect the result of
        its encompassing decision.

      • Path Coverage
        This measure reports whether each of the possible paths in each function have been fol-
        lowed. A path is a unique sequence of branches from the function entry to the exit.

      • Function Coverage
        This measure reports whether you invoked each function or procedure. It is useful during
        preliminary testing to assure at least some coverage in all areas of the software. Broad,
        shallow testing finds gross deficiencies in a test suite quickly.

      • Call Coverage
        This measure reports whether you executed each function call. The hypothesis is that
        faults commonly occur in interfaces between modules.

      • Linear Code Sequence and Jump (LCSAJ) Coverage
        This variation of path coverage considers only sub-paths that can easily be represented
        in the program source code, without requiring a flow graph. An LCSAJ is a sequence of
        source code lines executed in sequence. This ”linear” sequence can contain decisions as
        long as the control flow actually continues from one line to the next at run-time. Sub-paths
        are constructed by concatenating LCSAJs. Researchers refer to the coverage ratio of paths
        of length n LCSAJs as the test effectiveness ratio (TER) n+2.

      • Data Flow Coverage
        This variation of path coverage considers only the sub-paths from variable assignments to
        subsequent references of the variables.

      • Object Code Branch Coverage
        This measure reports whether each machine language conditional branch instruction both
        took the branch and fell through.

      • Loop Coverage
        This measure reports whether you executed each loop body zero times, exactly once, and
        more than once (consecutively). For do-while loops, loop coverage reports whether you
        executed the body exactly once, and more than once. The valuable aspect of this measure
        is determining whether while-loops and for-loops execute more than once, information not
        reported by others measure.

      • Race Coverage
        This measure reports whether multiple threads execute the same code at the same time. It
        helps detect failure to synchronize access to resources. It is useful for testing multi-threaded
        programs such as in an operating system.

      • Relational Operator Coverage
        This measure reports whether boundary situations occur with relational operators (¡, ¡=,
        ¿, ¿=). The hypothesis is that boundary test cases find off-by-one errors and mistaken
        uses of wrong relational operators such as ¡ instead of ¡=.
                                                                                          285

   • Weak Mutation Coverage
     This measure is similar to relational operator coverage but much more general [How-
     den1982]. It reports whether test cases occur which would expose the use of wrong oper-
     ators and also wrong operands. It works by reporting coverage of conditions derived by
     substituting (mutating) the program’s expressions with alternate operators, such as ”-”
     substituted for ”+”, and with alternate variables substituted.

   • Table Coverage
     This measure indicates whether each entry in a particular array has been referenced. This
     is useful for programs that are controlled by a finite state machine.

   The rest of this text must be changed to refer to the code coverage example within ROSE/-
tutorial.
   Figure 39 shows the low level construction of a more complex AST fragment (a function
declaration) and its insertion into the AST at the top of each block. Note that the code does
not handle symbol table issues, yet.
   Building a function in global scope.
     286                                                                                                             CHAPTER 39. CODE COVERAGE


     / / ROSE i s a t o o l f o r b u i l d i n g p r e p r o c e s s o r s , t h i s f i l e               is   an e x a m p l e    preprocessor           b u i l t w i t h ROSE .
2    // S p e c i f i c a l l y i t s h o w s t h e d e s i g n o f a t r a n s f o r m a t i o n           to   instrument          s o u r c e code ,     p l a c i n g source code
     // a t t h e t o p and b o t t o m e o f e a c h b a s i c b l o c k .
4
     #i n c l u d e   ” r o s e . h”
6
     u s i n g namespace s t d ;
8
     /∗
10         Design of t h i s code .
              I n p u t s : s o u r c e code ( f i l e . C)
12            Outputs : instrumented source                          code     ( rose      f i l e . C and    file .o)

14          P r o p e r t i e s of instrumented source code :
                   1) added d e c l a r a t i o n f o r coverage sup po r t f u n c t i o n
16                        ( e i t h e r forward f u n c t i o n d e c l a r a t i o n or a #i n c l u d e to i n c l u d e a header f i l e ) .
                   2) Each f u n c t i o n i n t h e s o u r c e program i s i n s t r u m e n t e d t o i n c l u d e a c a l l t o t h e
18                        coverage support function /
     ∗/
20

22
     // G l o b a l v a r i a b l e s so t h a t t h e g l o b a l f u n c t i o n d e c l a r a t i o n can be r e u s e d t o b u i l d
24   / / e a c h f u n c t i o n c a l l e x p r e s s i o n i n t h e AST t r a v e r s a l t o i n s t r u m e n t a l l f u n c t i o n s .
     S g F u n c t i o n D e c l a r a t i o n ∗ g l o b a l F u n c t i o n D e c l a r a t i o n = NULL ;
26   SgFunctionType ∗                             globalFunctionType                               = NULL ;
     S g F u n c t i o n S y m b o l ∗ f u n c t i o n S y m b o l = NULL ;
28
     / / S i m p l e ROSE t r a v e r s a l c l a s s : T h i s a l l o w s u s t o v i s i t a l l t h e                 functions         and    add
30   // new c o d e t o i n s t r u m e n t / r e c o r d t h e i r u s e .
     c l a s s S i m p l e I n s t r u m e n t a t i o n : public S g S i m p l e P r o c e s s i n g
32         {
              public :
34              // r e q u i r e d v i s i t f u n c t i o n t o d e f i n e what i s t o be done
                      void v i s i t ( SgNode ∗ a s t N o d e ) ;
36         };

38
     // Code t o b u i l d f u n c t i o n d e c l a r a t i o n : T h i s d e c l a r e s Shmuel ’ s f u n c t i o n c a l l w h i c h
40   // w i l l be i n s e r t e d ( as a f u n c t i o n c a l l ) i n t o e a c h f u n c t i o n b o d y o f t h e i n p u t
     // a p p l i c a t i o n .
42   void b u i l d F u n c t i o n D e c l a r a t i o n ( S g P r o j e c t ∗ p r o j e c t )
         {
44     / / ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
       // C r e a t e t h e f u n c t i o n D e c l a r a t i o n
46     / / ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗

48        //   SgGlobal∗ globalScope = project − g e t f i l e (0). get root ();
                                                            >
               SgSourceFile∗ sourceFile = isSgSourceFile ( project− g e t f i l e L i s t ( ) [ 0 ] ) ;
                                                                                        >
50             ROSE ASSERT( s o u r c e F i l e != NULL ) ;
               SgGlobal ∗ globalScope = s o u r c e F i l e − >g e t g l o b a l S c o p e ( ) ;
52             ROSE ASSERT( g l o b a l S c o p e != NULL ) ;

54             Sg File Info ∗ fil e in fo                                      = Sg File Info : : generateDefaultFileInfoForTransformationNode ( ) ;
               SgType ∗ f u n c t i o n r e t u r n t y p e                    = new SgTypeVoid ( ) ;
56
               SgName f u n c t i o n n a m e                                                    = ” coverageTraceFunc1 ” ;
58             SgFunctionType ∗ f u n c t i o n t y p e                                          = new S g F u n c t i o n T y p e ( f u n c t i o n r e t u r n t y p e , f a l s e ) ;
               S g F u n c t i o n D e c l a r a t i o n ∗ f u n c t i o n D e c l a r a t i o n = new S g F u n c t i o n D e c l a r a t i o n ( f i l e i n f o , f u n c t i o n n a m e ,   function type );
60
          / / DQ ( 9 / 8 / 2 0 0 7 ) : F i x u p t h e d e f i n i n g a n d n o n− d e f i n i n g d e c l a r a t i o n s
62            ROSE ASSERT( f u n c t i o n D e c l a r a t i o n −   >g e t d e f i n i n g D e c l a r a t i o n ( ) == NULL ) ;
              functionDeclaration−              >s e t d e f i n i n g D e c l a r a t i o n ( f u n c t i o n D e c l a r a t i o n ) ;
64            ROSE ASSERT( f u n c t i o n D e c l a r a t i o n −   >g e t d e f i n i n g D e c l a r a t i o n ( )                    != NULL ) ;
              ROSE ASSERT( f u n c t i o n D e c l a r a t i o n −   >g e t f i r s t N o n d e f i n i n g D e c l a r a t i o n ( ) != f u n c t i o n D e c l a r a t i o n ) ;
66
          / / DQ ( 9 / 8 / 2 0 0 7 ) : We h a v e n o t b u i l d a n o n− d e f i n i n g d e c l a r a t i o n , s o t h i s s h o u l d b e               NULL .
68            ROSE ASSERT( f u n c t i o n D e c l a r a t i o n −>g e t f i r s t N o n d e f i n i n g D e c l a r a t i o n ( ) == NULL ) ;

70        / / DQ ( 9 / 8 / 2 0 0 7 ) : N e e d t o a d d f u n c t i o n s y m b o l t o g l o b a l s c o p e !
              p r i n t f ( ” F i x i n g up t h e s y m b o l t a b l e i n s c o p e = %p = %s f o r f u n c t i o n = %p = %s \n ” , g l o b a l S c o p e , g l o b a l S c o p e −>c l a s s n a m e ( ) . c s t
72            f u n c t i o n S y m b o l = new S g F u n c t i o n S y m b o l ( f u n c t i o n D e c l a r a t i o n ) ;
              globalScope− nsert symbol ( functionDeclaration −
                                     >i                                                            >g e t n a m e ( ) , f u n c t i o n S y m b o l ) ;
74            ROSE ASSERT( g l o b a l S c o p e − o o k u p f u n c t i o n s y m b o l ( f u n c t i o n D e c l a r a t i o n −
                                                     >l                                                                                   >g e t n a m e ( ) ) != NULL ) ;

76        //   ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
          //   Create the InitializedName for a parameter within the parameter l i s t
78        //   ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
               SgName v a r 1 n a m e = ” t e x t S t r i n g ” ;




     Figure 39.1: Example source code shows instrumentation to call a test function from the top of
     each function body in the application (part 1).
                                                                                                                                                                                               287


              SgTypeChar ∗ v a r 1 t y p e                                  = new SgTypeChar ( ) ;
2             SgPointerType ∗ p o i n t e r t y p e                         = new S g P o i n t e r T y p e ( v a r 1 t y p e ) ;
              SgInitializer ∗ var1 initializer                              = NULL ;
4             SgInitializedName ∗var1 init name                             = new S g I n i t i a l i z e d N a m e ( v a r 1 n a m e , p o i n t e r t y p e , v a r 1 i n i t i a l i z e r , NULL ) ;
              var1 init name− s e t f i l e i n f o ( S g
                              >                                             File Info : : generateDefaultFileInfoForTransformationNode ( ) ) ;
6
         //   I n s e r t argument in f u n c t i o n parameter l i s t
8             ROSE ASSERT ( f u n c t i o n D e c l a r a t i o n != NULL ) ;
              ROSE ASSERT ( f u n c t i o n D e c l a r a t i o n −>g e t p a r a m e t e r L i s t ( )            != NULL ) ;
10
              ROSE ASSERT ( f u n c t i o n D e c l a r a t i o n −>g e t p a r a m e t e r L i s t ( ) != NULL ) ;
12            functionDeclaration−         >g e t p a r a m e t e r L i s t ()−> a p p e n d a r g ( v a r 1 i n i t n a m e ) ;

14       //   S e t t h e p a r e n t n o d e i n t h e AST ( t h i s c o u l d b e                  done     by    the     AstPostProcessing
              functionDeclaration−            >s e t p a r e n t ( g l o b a l S c o p e ) ;
16
         //   Set t h e scope e x p l i c i t l y ( s i n c e i t c o u l d be d i f f e r e n t from t h e p a r e n t ?)
18       //   T h i s c a n ’ t b e d o n e b y t h e A s t P o s t P r o c e s s i n g ( u n l e s s we r e l a x s o m e c o n s t r a i n t s )
              functionDeclaration−           >s e t s c o p e ( g l o b a l S c o p e ) ;
20
         //   I f i t i s not a forward d e c l a r a t i o n then the unparser w i l l                                     skip     the     ”;”    at    the     end    ( need      to    fix    this      better )
22            f u n c t i o n D e c l a r a t i o n − etForward ( ) ;
                                                     >s
              ROSE ASSERT ( f u n c t i o n D e c l a r a t i o n − s F o r w a r d ( ) == t r u e ) ;
                                                                   >i
24
         //   M a r k f u n c t i o n a s e x t e r n ”C”
26            functionDeclaration−             >g e t d e c l a r a t i o n M o d i f i e r ( ) . g e t s t o r a g e M o d i f i e r ( ) . s e t E x t e r n ( ) ;
              functionDeclaration−             >s e t l i n k a g e ( ”C” ) ;      // T h i s mechanism c o u l d be i m p r o v e d !
28
              globalFunctionType = fu n ct ion ty p e ;
30            globalFunctionDeclaration = functionDeclaration ;

32       //   Add f u n c t i o n d e c l a r a t i o n t o g l o b a l s c o p e !
              globalScope−       >p r e p e n d d e c l a r a t i o n ( g l o b a l F u n c t i o n D e c l a r a t i o n ) ;
34
         //   f u n c t i o n S y m b o l = new S g F u n c t i o n S y m b o l ( g l o b a l F u n c t i o n D e c l a r a t i o n ) ;
36       //   A l l any m o d i f i c a t i o n s t o b e f i x e d up ( p a r e n t s e t c )
         //   A s t P o s t P r o c e s s i n g ( p r o j e c t ) ; // T h i s i s n o t a l l o w e d and s h o u l d be f i x e d !
38            AstPostProcessing ( globalScope ) ;

40        }

42   #i f 0
     / / DQ ( 1 2 / 1 / 2 0 0 5 ) : T h i s v e r s i o n o f         the v i s i t function handles the special case of
44   // i n s t u m e n t a t i o n a t e a c h f u n c t i o n       ( a f u n c t i o n c a l l at the top of each f u n c t i o n ) .
     / / A t IBM w e m o d i f i e d t h i s t o b e a                version which instrumented every bloc k .
46
     void
48   S i m p l e I n s t r u m e n t a t i o n : : v i s i t ( SgNode ∗ a s t N o d e )
          {
50            S g F u n c t i o n D e c l a r a t i o n ∗ f u n c t i o n D e c l a r a t i o n = i s S g F u n c t i o n D e c l a r a t i o n ( astNode ) ;
              SgFunctionDefinition∗                         functionDefinition                  = f u n c t i o n D e c l a r a t i o n != NULL ? f u n c t i o n D e c l a r a t i o n −>g e t d e f i n i t i o n ( )              : NULL ;
52             i f ( f u n c t i o n D e c l a r a t i o n != NULL && f u n c t i o n D e f i n i t i o n != NULL)
                    {
54                 // I t i s up t o t h e u s e r t o l i n k t h e i m p l e m e n t a t i o n s o f t h e s e f u n c t i o n s l i n k t i m e
                          s t r i n g functionName = f u n c t i o n D e c l a r a t i o n −           >g e t n a m e ( ) . s t r ( ) ;
56                        s t r i n g fileName                = f u n c t i o n D e c l a r a t i o n − g e t f i l e i n f o ()−> g e t f i l e n a m e ( ) ;
                                                                                                       >

58               //    B u i l d a s o u r c e f i l e l o c a t i o n o b j e c t ( f o r c o n s t r u c t i o n o f e a c h IR n o d e )
                 //    N o t e t h a t we s h o u l d n o t b e s h a r i n g t h e s a m e S g F i l e I n f o o b j e c t i n m u l t i p l e IR n o d e s .
60                     Sg File Info ∗ f i l e i n f o = Sg File Info : : generateDefaultFileInfoForTransformationNode ( ) ;

62                     SgFunctionSymbol ∗ functionSymbol                                           = new S g F u n c t i o n S y m b o l ( g l o b a l F u n c t i o n D e c l a r a t i o n ) ;
                       S g F u n c t i o n R e f E x p ∗ f u n c t i o n R e f E x p r e s s i o n = new S g F u n c t i o n R e f E x p ( f i l e i n f o , f u n c t i o n S y m b o l , g l o b a l F u n c t i o n T y p e ) ;
64                     SgExprListExp ∗ e x p r e s s i o n L i s t                                 = new S g E x p r L i s t E x p ( f i l e i n f o ) ;

66                     s t r i n g c on v e r ag e F u n c ti on I n pu t = functionName + s t r i n g ( ” i n ” ) + fileName ;
                       S g S t r i n g V a l ∗ f u n c t i o n N a m e S t r i n g V a l u e = new S g S t r i n g V a l ( f i l e i n f o , ( char ∗ ) c o n v e r a g e F u n c t i o n I n p u t . c s t r ( ) ) ;
68                     expressionList−            >a p p e n d e x p r e s s i o n ( f u n c t i o n N a m e S t r i n g V a l u e ) ;
                       SgFunctionCallExp ∗ functionCallExp                                   = new S g F u n c t i o n C a l l E x p ( f i l e i n f o , f u n c t i o n R e f E x p r e s s i o n , e x p r e s s i o n L i s t , g l o b a l F u n c t i o n T y p e ) ;
70
                 //    c r e a t e an e x p r e s s i o n t y p e
72                     SgTypeVoid ∗ e x p r t y p e = new SgTypeVoid ( ) ;

74               //    c r e a t e an e x p r e s s i o n r o o t
                 //    SgExpressionRoot ∗ expr root                          = new       SgExpressionRoot ( f i l e                info , functionCallExp , expr type );
76
                 //    c r e a t e an e x p r e s s i o n s t a t e m e n t
78                     S g E x p r S t a t e m e n t ∗ n e w s t m t = new S g E x p r S t a t e m e n t ( f i l e i n f o , f u n c t i o n C a l l E x p ) ;

80               //    expr root − s e t p a r e n t ( new stmt );
                                  >




     Figure 39.2: Example source code shows instrumentation to call a test function from the top of
     each function body in the application (part 2).
     288                                                                                                                     CHAPTER 39. CODE COVERAGE


                     //    new stmt− s e t e x p r e s s i o n r o o t ( e x p r r o o t ) ;
                                    >
2                    //    functionCallExp − s e t p a r e n t ( new stmt− g e t
                                              >                                     >                      expression           root ());

4                          functionCallExp−>s e t p a r e n t ( n e w s t m t ) ;

6                    //    i n s e r t a statement i n t o the f u n c t i o n body
                           functionDefinition −    >g e t b o d y ()−> p r e p e n d s t a t e m e n t ( n e w s t m t ) ;
8
     #i f       0
10                   //    T h i s s h o w s t h e a l t e r n a t i v e u s e o f t h e ROSE                R e w r i t e Mechanism t o do t h e same t h i n g !
                     //    However , t h e p e r f o r m a n c e i s n o t as good as                        t h e v e r s i o n w r i t t e n a b o v e ( more d i r e c t l y
12                   //    b u i l d i n g t h e IR n o d e s ) .

14                   //    string       c o d e A t T o p O f B l o c k = ” v o i d p r i n t f ( c h a r ∗ ) ; p r i n t f ( \ ” FUNCTION NAME i n FILE NAME \\ n \ ” ) ; ” ;
                           string       c od e AtT op OfB loc k = ” c o v e r a g e T r a c e F u n c 1 ( \ ”FUNCTION NAME i n FILE NAME \ ” ) ; ” ;
16
                           string       functionTarget                  = ”FUNCTION NAME” ;
18                         string       fileTarget                      = ”FILE NAME” ;

20                         cod e AtT op OfB loc k . r e p l a c e ( co de AtT o pO fBlo ck . f i n d ( f u n c t i o n T a r g e t ) , f u n c t i o n T a r g e t . s i z e ( ) , f u n c t i o n N a m e ) ;
                           c ode At T opO fBl oc k . r e p l a c e ( c od eA tT op OfBl oc k . f i n d ( f i l e T a r g e t ) , f i l e T a r g e t . s i z e ( ) , f i l e N a m e ) ;
22
                     //    printf       ( ” c o d e A t T o p O f B l o c k = %s \ n ” , c o d e A t T o p O f B l o c k . c s t r ( ) ) ;
24                         printf       ( ”%s i n %s \n ” , f u n c t i o n N a m e . c s t r ( ) , f i l e N a m e . c s t r ( ) ) ;

26                   //    I n s e r t new c o d e i n t o t h e s c o p e r e p r e s e n t e d b y t h e s t a t e m e n t ( a p p l i e s t o S g S c o p e S t a t e m e n t s )
                           MiddleLevelRewrite : : ScopeIdentifierEnum scope = MidLevelCollectionTypedefs : : StatementScope ;
28
                           SgBasicBlock ∗ functionBody = f u n c t i o n D e f i n i t i o n −>g e t b o d y ( ) ;
30                         ROSE ASSERT ( f u n c t i o n B o d y != NULL ) ;

32                   //    I n s e r t t h e new c o d e a t t h e t o p o f t h e s c o p e r e p r e s e n t e d b y b l o c k
                           M i d d l e L e v e l R e w r i t e : : i n s e r t ( f u n c t i o n B o d y , codeAtTopOfBlock , s c o p e , M i d L e v e l C o l l e c t i o n T y p e d e f s : : T o p O f C u r r e n t S c o p e ) ;
34   #e n d i f
                       }
36       }
     #e n d i f
38
     void
40   S i m p l e I n s t r u m e n t a t i o n : : v i s i t ( SgNode ∗ a s t N o d e ) {
          S g B a s i c B l o c k ∗ b l o c k = NULL ;
42        b l o c k = i s S g B a s i c B l o c k ( astNode ) ;
          i f ( b l o c k != NULL) {
44                 // I t i s up t o t h e u s e r t o l i n k t h e i m p l e m e n t a t i o n s                      of    these       functions          link     time
                   S g F i l e I n f o ∗ f i l e I n f o = block− g e t f i l e i n f o ( ) ;
                                                                           >
46                 s t r i n g fileName = f i l e I n f o −         >g e t f i l e n a m e ( ) ;
                   i n t lineNum = f i l e I n f o −          >g e t l i n e ( ) ;
48
                     / / B u i l d a s o u r c e f i l e l o c a t i o n o b j e c t ( f o r c o n s t r u c t i o n o f e a c h IR n o d e )
50                   / / N o t e t h a t we s h o u l d n o t b e s h a r i n g t h e s a m e S g F i l e I n f o o b j e c t i n m u l t i p l e IR n o d e s .
                     Sg File Info ∗ newCallfileInfo = Sg File Info : : generateDefaultFileInfoForTransformationNode ( ) ;
52
                     ROSE ASSERT ( f u n c t i o n S y m b o l != NULL ) ;
54                   S g F u n c t i o n R e f E x p ∗ f u n c t i o n R e f E x p r e s s i o n = new S g F u n c t i o n R e f E x p ( n e w C a l l f i l e I n f o , f u n c t i o n S y m b o l , g l o b a l F u n c t i o n T y p e ) ;
                     SgExprListExp ∗ e x p r e s s i o n L i s t                                 = new S g E x p r L i s t E x p ( n e w C a l l f i l e I n f o ) ;
56
                     s t r i n g c o d e L o c a t i o n = f i l e N a m e + ” ” + S t r i n g U t i l i t y : : n u m b e r T o S t r i n g ( lineNum ) ;
58                   S g S t r i n g V a l ∗ f u n c t i o n N a m e S t r i n g V a l u e = new S g S t r i n g V a l ( n e w C a l l f i l e I n f o , ( char ∗ ) c o d e L o c a t i o n . c s t r ( ) ) ;
                     expressionList−            >a p p e n d e x p r e s s i o n ( f u n c t i o n N a m e S t r i n g V a l u e ) ;
60                   SgFunctionCallExp ∗ functionCallExp                                   = new S g F u n c t i o n C a l l E x p ( n e w C a l l f i l e I n f o , f u n c t i o n R e f E x p r e s s i o n , e x p r e s s i o n L i s t , g l o b a l F u

62              //   c r e a t e an e x p r e s s i o n t y p e
                //   S g T y p e V o i d ∗ e x p r t y p e = new S g T y p e V o i d ( ) ;
64              //   c r e a t e an e x p r e s s i o n r o o t
                //   S g E x p r e s s i o n R o o t ∗ e x p r r o o t = new S g E x p r e s s i o n R o o t ( n e w C a l l f i l e I n f o , f u n c t i o n C a l l E x p , e x p r t y p e ) ;
66
                //   c r e a t e an e x p r e s s i o n s t a t e m e n t
68                   S g E x p r S t a t e m e n t ∗ n e w s t m t = new S g E x p r S t a t e m e n t ( n e w C a l l f i l e I n f o , f u n c t i o n C a l l E x p ) ;

70              //   expr root − s e t p a r e n t ( new stmt );
                                >
                //   new stmt− s e t e x p r e s s i o n ( e x p r r o o t ) ;
                               >
72              //   functionCallExp − s e t p a r e n t ( new stmt− g e t e x p r e s s i o n ( ) ) ;
                                        >                                      >
                     functionCallExp−   >s e t p a r e n t ( n e w s t m t ) ;
74
                //   insert a statement into the function                                   body
76                   block−>p r e p e n d s t a t e m e n t ( n e w s t m t ) ;

78          }
     }




     Figure 39.3: Example source code shows instrumentation to call a test function from the top of
     each function body in the application (part 3).
                                                                                                  289




     void f o o ( )
 2       {
       // Should        d e t e c t t h a t f o o IS c a l l e d
 4     i f ( true )     {
             int x      = 3;
 6     }
       else {
 8           int x      = 4;
         }
10   }

12   void f o o b a r ( )
        {
14          i n t y =4;
            switch ( y ) {
16                case 1 :
                        // h e l l o w o r l d
18                      break ;
                  case 2 :
20
                   case 3 :
22
                   default : {
24                    //
                   }
26            }

28      // Should d e t e c t t h a t f o o b a r i s NOT c a l l e d
         }
30
     i n t main ( )
32        {
             i f ( true ) {
34           }
            foo ( ) ;
36          return 0 ;
          }



              Figure 39.4: Example source code used as input to translator adding new function.
     290                                                                                     CHAPTER 39. CODE COVERAGE




     extern ”C” void c o v e r a g e T r a c e F u n c 1 ( char ∗ t e x t S t r i n g ) ;
 2
     void f o o ( )
 4   {
                                                                                                                            −d
       c o v e r a g e T r a c e F u n c 1 ( ” / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t u t o r i
 6   // Should d e t e c t t h a t f o o IS c a l l e d
       i f ( true ) {
 8                                                                                                                            −d
           c o v e r a g e T r a c e F u n c 1 ( ” / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t u t o
           int x = 3 ;
10     }
       else {
12                                                                                                                            −d
           c o v e r a g e T r a c e F u n c 1 ( ” / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t u t o
           int x = 4 ;
14     }
     }
16
     void f o o b a r ( )
18   {
                                                                                                                                 −d
        c o v e r a g e T r a c e F u n c 1 ( ” / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t u t o r i
20      int y = 4 ;
        switch ( y ) {
22                                                                                                                                 −d
            c o v e r a g e T r a c e F u n c 1 ( ” / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t u t o
            case 1 :
24   {
                                                                                                                                     −d
                c o v e r a g e T r a c e F u n c 1 ( ” / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t u
26   // h e l l o w o r l d
                break ;
28          }
            default :
30   {
                                                                                                                                     −d
                c o v e r a g e T r a c e F u n c 1 ( ” / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t u
32   {
                                                                                                                                       −d
                    c o v e r a g e T r a c e F u n c 1 ( ” / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x /
34   //
                }
36          }
        }
38   // Should d e t e c t t h a t f o o b a r i s NOT c a l l e d
     }
40
     i n t main ( )
42   {
                                                                                                                              −d
         c o v e r a g e T r a c e F u n c 1 ( ” / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t u t o r i
44       i f ( true ) {
                                                                                                                                −d
             c o v e r a g e T r a c e F u n c 1 ( ” / e x p o r t /tmp . hudson−r o s e / hudson / w o r k s p a c e / a90−ROSE a i l y −r e l e a s e / l a b e l /amd64−l i n u x / t u t o
46       }
         foo ( ) ;
48       return 0 ;
     }



                            Figure 39.5: Output of input to translator adding new function.
     Chapter 40

     Bug Seeding

     Bug seeding is a technique used to construct example codes from existing codes which can be
     used to evaluate tools for the finding bugs in randomly selected existing applications. The idea
     is to seed an existing application with a known number of known bugs and evaluate the bug
     finding or security tool based on the percentage of the number of bugs found by the tool. If the
     bug finding tool can identify all known bugs then there can be some confidence that the tool
     detects all bugs of that type used to seed the application.
         This example tutorial code is a demonstration of a more complete technique being developed
     in collaboration with NIST to evaluate tools for finding security flaws in applications. It will
     in the future be a basis for testing of tools built using ROSE, specifically Compass, but the
     techniques are not in any way specific to ROSE or Compass.



     40.1           Input For Examples Showing Bug Seeding
     Figure ?? shows the example input used for demonstration of bug seeding as a transformation.



 2   void f o o b a r ( )
        {
 4     // S t a t i c a r r a y d e c l a r a t i o n
          float array [ 1 0 ] ;
 6
             array [ 0 ] = 0;
 8
             f o r ( i n t i =0; i < 5 ;        i ++)
10                {
                     array [ i ] = 0;
12                }
         }



        Figure 40.1: Example source code used as input to program in codes used in this chapter.



                                                        291
292                                                          CHAPTER 40. BUG SEEDING

40.2      Generating the code representing the seeded bug
Figure 40.2 shows a code that traverses each IR node and for and modifies array reference index
expressions to be out of bounds. The input code is shown in figure 40.1, the output of this code
is shown in figure 40.3.
      40.2. GENERATING THE CODE REPRESENTING THE SEEDED BUG                                                                                                                                293


      //    This example demonstrates the                           seeding of a            s p e c i f i c type
 2    //    of bug ( b u f f e r o v e r f l o w ) i n t o          any e x i s t i n g     a p p l i c a t i o n to
      //    t e s t bug f i n d i n g t o o l s .
 4
      #i n c l u d e ” r o s e . h ”
 6    u s i n g namespace S a g e B u i l d e r ;
      u s i n g namespace S a g e I n t e r f a c e ;
 8
      namespace S e e d B u g s A r r a y I n d e x i n g {
10
      class       InheritedAttribute
12       {
                 public :
14                    bool i s L o o p ;
                      bool i s V u l n e r a b i l i t y ;
16                    I n h e r i t e d A t t r i b u t e ( ) : i s L o o p ( f a l s e ) , i s V u l n e r a b i l i t y ( f a l s e ) {}
                      I n h e r i t e d A t t r i b u t e ( c o n s t I n h e r i t e d A t t r i b u t e & X) : i s L o o p (X . i s L o o p ) , i s V u l n e r a b i l i t y (X . i s V u l n e r a b i l i t y )   {}
18          };

20    class       BugSeeding           :    public     SgTopDownProcessing<I n h e r i t e d A t t r i b u t e >
         {
22               public :
                      InheritedAttribute evaluateInheritedAttribute                                              (
24                        SgNode ∗ a s t N o d e ,
                          InheritedAttribute inheritedAttribute );
26          };

28    InheritedAttribute
      BugSeeding : : e v a l u a t e I n h e r i t e d A t t r i b u t e (
30          SgNode ∗ a s t N o d e ,
            InheritedAttribute inheritedAttribute )
32        {
        / / U s e t h i s i f we o n l y w a n t t o s e e d b u g s i n l o o p s
34          bool i s L o o p = i n h e r i t e d A t t r i b u t e . i s L o o p                  ||
                                    ( i s S g F o r S t a t e m e n t ( a s t N o d e ) != NULL) | |
36                                  ( i s S g W h i l e S t m t ( a s t N o d e ) != NULL)        ||
                                    ( i s S g D o W h i l e S t m t ( a s t N o d e ) != NULL ) ;
38      / / Add F o r t r a n s u p p o r t
            i s L o o p = i s L o o p | | ( i s S g F o r t r a n D o ( a s t N o d e ) != NULL ) ;
40
           //    Mark f u t u r e n o e s i n t h i s s u b t r e e a s            being      part     of    a       loop
42               i n h e r i t e d A t t r i b u t e . isLoop = isLoop ;

44         //    To t e s t t h i s o n s i m p l e c o d e s ,         optionally          allow      it    to       be    applied   everywhere
                 bool a p p l y E v e r y W h e r e = t r u e ;
46
                 if     ( i s L o o p == t r u e | | a p p l y E v e r y W h e r e == t r u e )
48                      {
                      / / The i n h e r i t e d a t t r i b u t e i s t r u e i f f we a r e i n s i d e a l o o p a n d t h i s                   is   a   SgPntrArrRefExp .
50                          SgPntrArrRefExp ∗ a r r a y R e f e r e n c e = isSgPntrArrRefExp ( astNode ) ;
                             i f ( a r r a y R e f e r e n c e != NULL)
52                                {
                                // Mark a s a v u l n e r a b i l i t y
54                                   i n h e r i t e d A t t r i b u t e . i s V u l n e r a b i l i t y = true ;

56                           //    Now c h a n g e t h e a r r a y i n d e x ( t o s e e d t h e b u f f e r o v e r f l o w b u g )
                                   SgVarRefExp ∗ a r r a y V a r R e f = i s S g V a r R e f E x p ( a r r a y R e f e r e n c e −   >g e t l h s o p e r a n d ( ) ) ;
58                                 ROSE ASSERT( a r r a y V a r R e f != NULL ) ;
                                   ROSE ASSERT( a r r a y V a r R e f −        >g e t s y m b o l ( ) != NULL ) ;
60                                 S g I n i t i a l i z e d N a m e ∗ arrayName = i s S g I n i t i a l i z e d N a m e ( a r r a y V a r R e f −>g e t s y m b o l ()−> g e t d e c l a r a t i o n ( ) ) ;
                                   ROSE ASSERT( arrayName != NULL ) ;
62                                 SgArrayType ∗ a r r a y T y p e = i s S g A r r a y T y p e ( arrayName−               >g e t t y p e ( ) ) ;
                                   ROSE ASSERT( a r r a y T y p e != NULL ) ;
64                                 S g E x p r e s s i o n ∗ a r r a y S i z e = a r r a y T y p e−>g e t i n d e x ( ) ;

66                                 SgTreeCopy c o p y H e l p ;
                             //    Make a c o p y o f t h e e x p r e s s i o n u s e d t o h o l d t h e a r r a y s i z e i n t h e a r r a y d e c l a r a t i o n .
68                                 S g E x p r e s s i o n ∗ a r r a y S i z e C o p y = i s S g E x p r e s s i o n ( a r r a y S i z e − opy ( copyHelp ) ) ;
                                                                                                                                          >c
                                   ROSE ASSERT( a r r a y S i z e C o p y != NULL ) ;
70
                             //    This i s the e x i s t i n g index e x p r e s s i o n
72                                 SgExpression∗ indexExpression = arrayReference−        >g e t r h s o p e r a n d ( ) ;
                                   ROSE ASSERT( i n d e x E x p r e s s i o n != NULL ) ;
74
                             //                                                                     −
                                   B u i l d a n e w e x p r e s s i o n : ” a r r a y [ n ] ” − > ” a r r a y [ n+ a r r a y S i z e C o p y ] ” , w h e r e t h e a r r a y S i z e C o p y         is   a   size     of   ” array ”
76                                 S g E x p r e s s i o n ∗ n e w I n d e x E x p r e s s i o n = buildAddOp ( i n d e x E x p r e s s i o n , a r r a y S i z e C o p y ) ;

78                           //    S u b s t i t u t e t h e new e x p r e s s i o n f o r t h e o l d e x p r e s s i o n
                                   arrayReference−           >s e t r h s o p e r a n d ( n e w I n d e x E x p r e s s i o n ) ;
80                             }
                       }
82
                 return      inheritedAttribute ;
84          }
      }
86
      int
88    main ( i n t a r g c , char ∗ a r g v [ ] )
          {
90          SgProject ∗ p r o j e c t = f r o n t e n d ( argc ,                    argv ) ;
            ROSE ASSERT ( p r o j e c t != NULL ) ;
92
                 SeedBugsArrayIndexing : : BugSeeding t r e e T r a v e r s a l ;
94               SeedBugsArrayIndexing : : I n h e r i t e d A t t r i b u t e i n h e r i t e d A t t r i b u t e ;

96               treeTraversal . traverseInputFiles                            ( project , inheritedAttribute ) ;

98         //    Running i n t e r n a l t e s t s ( o p t i o n a l )
                 AstTests : : runAllTests ( p r o j e c t ) ;
100
           //    Output      t h e new       code seeded          with     a    specific       form     of       bug .
102              return      backend         ( project );
            }




                                           Figure 40.2: Example source code showing how to seed bugs.
     294                                                          CHAPTER 40. BUG SEEDING




 2   void f o o b a r ( )
     {
 4   // S t a t i c a r r a y d e c l a r a t i o n
       f l o a t a r r a y [ 1 0UL ] ;
 6     a r r a y [ 0 + 10UL ] = 0 ;
       f o r ( i n t i = 0 ; i < 5 ; i ++) {
 8         a r r a y [ i + 10UL ] = 0 ;
       }
10   }



                  Figure 40.3: Output of input code using seedBugsExample arrayIndexing.C
                    Part VI

         Binary Support


Tutorials of using ROSE to handle binary executable files.




                           295
Chapter 41

Instruction Semantics

The Instruction Semantics layer in ROSE can be used to “evaluate” instructions and is controlled
by a policy that defines the details of what “evaluate” means. For instance, given the following
“xor” instruction, the X86InstructionSemantics class specifies that the value of the “eax” and
“edx” registers are read, those two 32-bit values are xor’d together, and the 32-bit result is then
written to the “eax” register. The policy defines what a 32-bit value is (it could be an integer,
some representation of a constant, etc), how it is read and written to the registers, and how to
compute an xor.

xor eax, edx

   ROSE has a collection instruction semantic classes, one for each architecture. It also has a
small collection of policies. This chapter briefly describes a policy that tracks constant values.


41.1      The FindConstantsPolicy Class
The FindConstantsPolicy is used to track constant values across an instruction trace. The basic
idea is that ROSE “executes” the instructions one at a time in the instruction semantics layer,
identifies constants, performs operations on those constants, and assigns constants to registers
and memory locations. Each constant also maintains information about which instructions led
to that particular constant’s existence.
    A “constant” is an abstract datum that has a known integer value, or a name corresponding
to some unknown value, or a name and a known integer offset. Names take the form of the
letter “v” (for “value”) followed by a unique integer. Known values are represented as signed
hexadecimal values in the output.
    The findConstants.C program in the tests/roseTests/binaryTests directory (which is de-
scribed herein) takes each function and processes the instructions of that function in address
order. It makes no attempt to follow branches or any other kind of control flow, but serves to
demonstrate a simple way to track constants.

 1   #define \_\_STDC_FORMAT_MACROS
 2   #include "rose.h"

                                               297
298                                      CHAPTER 41. INSTRUCTION SEMANTICS

 3    #include "findConstants.h"
 4    #include <inttypes.h>
 5
 6    /* Returns the function name if known, or the address as a string otherwise. */
 7    static std::string
 8    name\_or\_addr(const SgAsmFunction *f)
 9    {
10        if (f->get\_name()!="")
11            return f->get\_name();
12
13        char buf[128];
14        SgAsmBlock *first\_bb = isSgAsmBlock(f->get\_statementList().front());
15        sprintf(buf, "0x%"PRIx64, first\_bb->get\_id());
16        return buf;
17    }
18
19    class AnalyzeFunctions : public SgSimpleProcessing {
20       public:
21         AnalyzeFunctions(SgProject *project) {
22             traverse(project, postorder);
23         }
24         void visit(SgNode *node) {
25             SgAsmFunction *func = isSgAsmFunction(node);
26             if (func) {
27                 std::cout <<"==============================================\n"
28                           <<"Constant propagation in function \"" <<name\_or\_addr(func) <<"\"\n"
29                           <<"==============================================\n";
30                 FindConstantsPolicy policy;
31                 X86InstructionSemantics<FindConstantsPolicy, XVariablePtr> t(policy);
32                 std::vector<SgNode*> instructions = NodeQuery::querySubTree(func, V\_SgAsmx86Instruct
33                 for (size\_t i=0; i<instructions.size(); i++) {
34                     SgAsmx86Instruction *insn = isSgAsmx86Instruction(instructions[i]);
35                     ROSE\_ASSERT(insn);
36                     t.processInstruction(insn);
37                     RegisterSet rset = policy.currentRset;
38                     std::cout <<unparseInstructionWithAddress(insn) <<"\n"
39                               <<rset;
40                 }
41             }
42         }
43    };
44
45    int main(int argc, char *argv[]) {
46        AnalyzeFunctions(frontend(argc, argv));
47    }
41.2. SAMPLE OUTPUT                                                                         299

    Lines 30 through 40 are the main meat of the example. For each function, we construct a
fresh policy. Since the policy holds the values of registers and memory, this resets them all to
an initial state having unknown values. The instruction semantics engine depends on the policy,
so we also create a new one for each function.
    Then we loop over the instructions of the function in order of their addresses at lines 33
through 40. Each instruction is processed in turn by the X86InstructionSemantics object, ad-
justing the state of the associated policy.
    Finally, the assembly language instruction is output followed by the values contained in the
registers as a result of processing the instruction.


41.2      Sample Output
Here’s some abbreviated output from running the “findConstants” test on a binary executable:

 1   ==============================================
 2   Constant propagation in function "_init"
 3   ==============================================
 4   0x80482c8:push   ebp
 5       ax = v62
 6       cx = v63
 7       dx = v64
 8       bx = v65
 9       sp = v66-0x4 [from 0x80482c8:push    ebp]
10       bp = v67
11       si = v68
12       di = v69
13       es = v70
14       cs = v71
15       ss = v72
16       ds = v73
17       fs = v74
18       gs = v75
19       cf = v76
20       ?1 = v77
21       pf = v78
22       ?3 = v79
23       af = v80
24       ?5 = v81
25       zf = v82
26       sf = v83
27       tf = v84
28       if = v85
29       df = v86
30       of = v87
31       iopl0 = v88
300                                             CHAPTER 41. INSTRUCTION SEMANTICS

32       iopl1 = v89
33       nt = v90
34       ?15 = v91
35       memory = {
36           size=4; addr=v66-0x4 [from 0x80482c8:push              ebp]; value=v67
37       }

    Line 4 indicates that the instruction “push ebp” is located at address 0x80482c8 and the
following lines show the contents of registers and known memory addresses following execution
of the “push.” One can readily see that each register has a unique constant of unknown value by
virtue of each constant having a unique name. The stack pointer register (sp) has the constant
“v66-0x4” obtained from this very instruction. If we had printed the registers prior to executing
the “push” we would have seen that the original sp constant was “v66”. Therefore, this “push”
instruction reduced the value of sp by four.
    Line 36 indicates that the four bytes beginning at memory address “v66-0x4” (which happens
to be the constant stored in the stack pointer register at line 9) contain the value “v67” (which
is the constant stored in the bp register at line 10).
    Therefore, it can be determined that “push ebp” decrements the stack pointer by four bytes,
then copies a 32-bit value from the bp register to the memory pointed to by the new stack
pointer.

 1    0x80482c9:mov    ebp, esp
 2        ax = v62
 3        cx = v63
 4        dx = v64
 5        bx = v65
 6        sp = v66-0x4 [from 0x80482c8:push          ebp]
 7        bp = v66-0x4 [from 0x80482c8:push          ebp]
 8        si = v68
 9        di = v69
10        es = v70
11        cs = v71
12        ss = v72
13        ds = v73
14        fs = v74
15        gs = v75
16        cf = v76
17        ?1 = v77
18        pf = v78
19        ?3 = v79
20        af = v80
21        ?5 = v81
22        zf = v82
23        sf = v83
24        tf = v84
25        if = v85
41.2. SAMPLE OUTPUT                                                                    301

26       df = v86
27       of = v87
28       iopl0 = v88
29       iopl1 = v89
30       nt = v90
31       ?15 = v91
32       memory = {
33           size=4; addr=v66-0x4 [from 0x80482c8:push          ebp]; value=v67
34       }

    The output after the “mov ebp, esp” instruction at address 0x80482c9 subsequent to the
“push ebp” that we just saw, shows that the new stack pointer has been copied into the “bp”
register and that nothing else has changed. A more interesting instruction follows...

 1   0x80482cb:sub    esp, 0x8
 2       ax = v62
 3       cx = v63
 4       dx = v64
 5       bx = v65
 6       sp = v66-0xc [from 0x80482cb:sub     esp, 0x8]
 7       bp = v66-0x4 [from 0x80482c8:push    ebp]
 8       si = v68
 9       di = v69
10       es = v70
11       cs = v71
12       ss = v72
13       ds = v73
14       fs = v74
15       gs = v75
16       cf = -v193-0x1 [from 0x80482cb:sub     esp, 0x8]
17       ?1 = v77
18       pf = -v187-0x1 [from 0x80482cb:sub     esp, 0x8]
19       ?3 = v79
20       af = -v191-0x1 [from 0x80482cb:sub     esp, 0x8]
21       ?5 = v81
22       zf = v190 [from 0x80482cb:sub     esp, 0x8]
23       sf = v189 [from 0x80482cb:sub     esp, 0x8]
24       tf = v84
25       if = v85
26       df = v86
27       of = v197 [from 0x80482cb:sub     esp, 0x8]
28       iopl0 = v88
29       iopl1 = v89
30       nt = v90
31       ?15 = v91
32       memory = {
302                                             CHAPTER 41. INSTRUCTION SEMANTICS

33            size=4; addr=v66-0x4 [from 0x80482c8:push              ebp]; value=v67
34       }

   The “sub esp, 0x8” subtracts eight from the value of the stack pointer register and then stores
the result in the stack pointer register. This can be seen by the fact that the constant stored in
the “sp” register has changed from “v66-0x4” to “v66-0xc.” One can also see that various flags
have been modified, although we don’t know the values of any of them.

 1    0x80482ce:call   0x8048364
 2        ax = v62
 3        cx = v63
 4        dx = v64
 5        bx = v65
 6        sp = v66-0x10 [from 0x80482ce:call   0x8048364]
 7        bp = v66-0x4 [from 0x80482c8:push   ebp]
 8        si = v68
 9        di = v69
10        es = v70
11        cs = v71
12        ss = v72
13        ds = v73
14        fs = v74
15        gs = v75
16        cf = -v193-0x1 [from 0x80482cb:sub    esp, 0x8]
17        ?1 = v77
18        pf = -v187-0x1 [from 0x80482cb:sub    esp, 0x8]
19        ?3 = v79
20        af = -v191-0x1 [from 0x80482cb:sub    esp, 0x8]
21        ?5 = v81
22        zf = v190 [from 0x80482cb:sub    esp, 0x8]
23        sf = v189 [from 0x80482cb:sub    esp, 0x8]
24        tf = v84
25        if = v85
26        df = v86
27        of = v197 [from 0x80482cb:sub    esp, 0x8]
28        iopl0 = v88
29        iopl1 = v89
30        nt = v90
31        ?15 = v91
32        memory = {
33            size=4; addr=v66-0x10 [from 0x80482ce:call   0x8048364]; value=0x80482d3 [from 0x80482ce:
34            size=4; addr=v66-0x4 [from 0x80482c8:push   ebp]; value=v67
35        }

   Now we get to our first branch-type instruction, a “call” to a particular address. Instruction
semantics describe what the call instruction does to the registers and memory, but does not
actually execute a call or process the called instructions. That means that a “ret” was not
41.3. BUILDING ON INSTRUCTION SEMANTICS                                                       303

processed and thus we should see the return address sitting on the stack. In fact, we do: the
stack pointer has been decremented by another four bytes and the memory address to which the
stack pointer points contains the address of the instruction immediately after the “call”.


41.3      Building on Instruction Semantics
The X86InstructionSemantics class and the policy classes can be extended to handle special cases.
For instance, the X86InstructionSemantics class processes the “rep stosd” instruction in such a
way that only one iteration of the “stosd” is considered. Sometimes it’s more useful to process
the entire repeated sequence in one step rather than iterating through the loop. Subclassing
X86InstructionSemantics to override individual instructions or classes of instructions is simple.
The subclass should redefine the “translate” method to do whatever is necessary for certain
instructions while delegating to the superclass for all remaining instructions. For example:

/* Augments super::translate() to override rep\_stos instructions */
virtual void translate(SgAsmx86Instruction *insn) {
    switch (insn->get\_kind()) {
        case x86\_rep\_stosb: updateIP(insn); rep\_stos\_semantics<1>(insn); break;
        case x86\_rep\_stosw: updateIP(insn); rep\_stos\_semantics<2>(insn); break;
        case x86\_rep\_stosd: updateIP(insn); rep\_stos\_semantics<4>(insn); break;
        default: super::translate(insn); break;
    }
}

    It’s also possible to subclass the policies. For instance, if you need to do something special
for binary AND operations on the stack pointer you could override the “and ” method in the
policy.
304   CHAPTER 41. INSTRUCTION SEMANTICS
Chapter 42

Binary Analysis

This chapter discusses the capabilities of ROSE to read, analyze and transform (transformations
to the binary file format) binary executables.
    Binary support in ROSE is currently based on a custom build ROSE Disassembler (for ARM,
x86, and PowerPC).
    The following code reads in a binary and creates a binary ROSE AST:

    SgProject* project = frontend(argc,argv);


    Similarily, one can unparse the AST to assembly using a call to the backend, cf. Figure ??.
    The best documentation for ROSE’s binary analysis capabilities is found in the doxygen-
generated API reference manual which can be found on the ROSE web site. Please refer to the fol-
lowing documented entities: class AsmUnparser, class Assembler, class BinaryLoader, namespace
BinaryAnalysis, class Disassembler, class MemoryMap, class Partitioner, class SymbolicSeman-
tics, class VirtualMachineSemantics, class RegisterDictionary, and class X86InstructionSemantics.


42.1       The ControlFlowGraph
Based on a control flow traversal of the binary AST, a separate control flow graph is created
that can be used for further analyses.
   TODO: Describe recent work on binary CFG.


42.2       DataFlow Analysis
Based on the control flow many forms of dataflow analysis may be performed. Dataflow analyses
available are:


42.2.1     Def-Use Analysis
Definition-Usage is one way to compute dataflow information about a binary program.

                                                305
306                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                CHAPTER 42. BINARY ANALYSIS




                                                                                                                                                                                                                                                                                                   8048414_f:__libc_csu_fini                                                                 8048460_f:__do_global_ctors_aux                                                                                                                                                                                                                                                                                                                                                                                                                          8048364_f:main                                                                                                                                                                                                                                                                                  80482c0_f:_start
                                                                                                                                                                                                                                                                                                       type = removed                                                                               type = removed                                                                                                                                                                                                                                                                                                                                                                                                                                    type = removed                                                                                                                                                                                                                                                                                  type = removed




                                                                                                                                                                                                                                                                                                    0x8048414:push ebp                                                                                  0x8048460:push ebp                                                                                                                                                                                                                                                                                                                                                                                                                   0x8048364:lea ecx, [esp + 0x04]                                                                                                                                                                                                                                                                    0x80482c0:xor ebp, ebp
                                                                                                                                                                                                                                                                                                          visited: 1                                                                                          visited: 1                                                                                                                                                                                                                                                                                                                                                                                                                                 visited: 1                                                                                                                                                                                                                                                                                     visited: 1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             Use of rbp at 80482c0
                                                                                                                                                                                                                                                                                                    type = removed push                                                                                 type = removed push                                                                                                                                                                                                                                                                                                                                                                                                                        type = removed lea                                                                                                                                                                                                                                                                              type = removed xor




                                                                                                                                                                                                                                                                                                                                                                                            Def of rax at 8048346
                                                                                                                                                                                                                                                                                                                                                                                            Def of rax at 804834f
                                                                                                                                                                                                                                                                                                     Def of rsp at 8048414 Use of rsp at 8048415                                                                       Use of rsp at 8048461                                                                                                                                                                                                                                                                                                                                                                                                                   Def of rcx at 8048364                                                                                                                                                                                                                                                                              Def of rbp at 80482c0
                                                                                                                                                                                                                                                                                                                                                                                            Def of rsp at 8048460
                                                                                                                                                                                                                                                                                                                                                                                            Def of rbp at 8048288


                                                                                                                                                                                                                                                                                                  0x8048415:mov ebp, esp                                                         0x8048461:mov ebp, esp                                                                                                                                                                                                                                                                                                                                                                                                                                  0x8048368:and esp, 0xf0<-0x10>                                                                                                                                                                                                                                                                            0x80482c2:pop esi
                                                                                                                                                                                                                                                                                                           visited: 1                                                                     visited: 1                                                                                                                                                                                                                                                                                                                                                                                                                                                 visited: 1                                                                                                                                                                                                                                                                                          visited: 1
                                                                                                                                                                                                                                                                                                     type = removed mov                                                             type = removed mov                                                                                                                                                                                                                                                                                                                                                                                                                                         type = removed and                                                                                                                                                                                                                                                                                  type = removed pop




                                                                                                                                                                                                                                                                                                                                                                                             Def of rax at 8048346
                                                                                                                                                                                                                                                                                                                  Def of rsp at 8048414                                                      Def of rax at 804834f                                                                                                                                                                                                                                                                                                                                                                                                                                             Def of rcx at 8048364                                                                                                                                                                                                                                                                   Def of rsp at 80482c2
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             Use of rsp at 80482c3
                                                                                                                                                                                                                                                                                                                  Def of rbp at 8048415                                                      Def of rsp at 8048460                                                                                                                                                                                                                                                                                                                                                                                                                                             Def of rsp at 8048368                                                                                                                                                                                                                                                                   Def of rbp at 80482c0
                                                                                                                                                                                                                                                                                                                                                                                             Def of rbp at 8048461


                                                                                                                                                                                                                                                                                                    0x8048417:push edi                                                             0x8048463:push ebx                                                                                                                                                                                                                                                                                                                                                                                                                      0x804836b:push DWORD PTR ds:[ecx + 0xfc<-0x04>]                                                                                                                                                                                                                                                                             0x80482c3:mov ecx, esp
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              80482e4_f:call_gmon_start                                                                                                                                                                                                                                                                                                                                                         804836e_f: 804836e
                                                                                                                                                                                                                                                                                                          visited: 1                                                                     visited: 1                                                                                                                                                                                                                                                                                                                                                                                                                                            visited: 1                                                                                                                                                                                                                                                                                                      visited: 1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   type = removed                                                                                                                                                                                                                                                                                                                                                                 type = removed
                                                                                                                                                                                                                                                                                                    type = removed push                                                            type = removed push                                                                                                                                                                                                                                                                                                                                                                                                                                   type = removed push                                                                                                                                                                                                                                                                                             type = removed mov




                                                                                                                                                                                                                                                                                                                                                                                             Def of rax at 8048346
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         Def of rcx at 80482c3
                                                                                                                                                                                                                                                                                                                  Def of rsp at 8048417                                                      Def of rax at 804834f                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Def of rcx at 8048364
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         Def of rsp at 80482c2
                                                                                                                                                                                                                                                                                                                  Def of rbp at 8048415                                                      Def of rsp at 8048463                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Def of rsp at 804836b
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         Def of rbp at 80482c0
                                                                                                                                                                                                                                                                                                                                                                                             Def of rbp at 8048461


                                                                                                                                                                                                                                                                                                    0x8048418:push esi                                                     0x8048464:mov ebx, 0x080494bc                                                                                                                       0x80482e4:push ebp                                                                                                                                                                                                                                                                                                                                                             0x804836e:push ebp                                                                                                                                                                                                                  0x80482c5:and esp, 0xf0<-0x10>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        80483c0_f:__libc_csu_init
                                                                                                                                                                                                                                                                                                          visited: 1                                                                   visited: 1                                                                                                                                    visited: 1                                                                                                                                                                                                                                                                                                                                                                     visited: 1                                                                                                                                                                                                                                visited: 1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            type = removed
                                                                                                                                                                                                                                                                                                    type = removed push                                                          type = removed mov                                                                                                                            type = removed push                                                                                                                                                                                                                                                                                                                                                            type = removed push                                                                                                                                                                                                                       type = removed and




                                                                                                                                                                                                                                                                                                                                                                                             Def of rax at 8048346
                                                                                                                                                                                                                                                                                                                                                                                             Def of rax at 804834f                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      Def of rcx at 80482c3
                                                                                                                                                                                                                                                                                                                  Def of rsp at 8048418                                                                                                                                                                           Def of rsp at 80482e4                                                                                                                                                                                                                                                                                                                                                                                    Def of rcx at 8048364
                                                                                                                                                                                                                                                                                                                                                                                             Def of rbx at 8048464                                                                                                                           Use of rsp at 80482e5                                                                                                                                                                                                                                                                                                                                                                                 Use of rsp at 804836f                                                                                                                                                                                                Def of rsp at 80482c5
                                                                                                                                                                                                                                                                                                                  Def of rbp at 8048415                                                                                                                                                                           Def of rbp at 804827e                                                                                                                                                                                                                                                                                                                                                                                    Def of rsp at 804836e
                                                                                                                                                                                                                                                                                                                                                                                             Def of rsp at 8048463                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      Def of rbp at 80482c0
                                                                                                                                                                                                                                                                                                                                                                                             Def of rbp at 8048461


                                                                                                                                                                                                                                                                                                    0x8048419:push ebx                                                           0x8048469:sub esp, 0x04                                                                                            0x80482e5:mov ebp, esp                                                                                                                               0x80483c0:push ebp                                                                                                                                                                                                                                                         0x804836f:mov ebp, esp                                                                                                                                                                                              0x80482c8:push eax
                                                                                                                                                                                                                                                                                                          visited: 1                                                                      visited: 1                                                                                                         visited: 1                                                                                                                                        visited: 1                                                                                                                                                                                                                                                                   visited: 1                                                                                                                                                                                                        visited: 1
                                                                                                                                                                                                                                                                                                    type = removed push                                                             type = removed sub                                                                                                 type = removed mov                                                                                                                                type = removed push                                                                                                                                                                                                                                                          type = removed mov                                                                                                                                                                                                type = removed push




                                                                                                                                                                                                                                                                                                                                                                                             Def of rax at 8048346
                                                                                                                                                                                                                                                                                                                                                                                             Def of rax at 804834f                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 Def of rcx at 8048364                                                                                                                                                                                              Def of rcx at 80482c3
                                                                                                                                                                                                                                                                                                                  Def of rsp at 8048419                                                                                                                                                                         Def of rsp at 80482e4
                                                                                                                                                                                                                                                                                                                                                                                             Def of rbx at 8048464                                                                                                                                                                                                                                        Def of rsp at 80483c0 Use of rsp at 80483c1                                                                                                                                                                                                                                              Def of rsp at 804836e                                                                                                                                                                                              Def of rsp at 80482c8
                                                                                                                                                                                                                                                                                                                  Def of rbp at 8048415                                                                                                                                                                         Def of rbp at 80482e5
                                                                                                                                                                                                                                                                                                                                                                                             Def of rsp at 8048469                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 Def of rbp at 804836f                                                                                                                                                                                              Def of rbp at 80482c0
                                                                                                                                                                                                                                                                                                                                                                                             Def of rbp at 8048461


                                                                                                                                                                                                                                                                                                 0x804841a:call 0x0804841f                                       0x804846c:mov        eax, DWORD PTR ds:[0x080494bc]                                                                             0x80482e7:push ebx                                                                                                                                              0x80483c1:mov ebp, esp                                                                                                                                                                                                                                                  0x8048371:push ecx                                                                                                                                                                                            0x80482c9:push esp
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   8048340_f:frame_dummy
                                                                                                                                                                                                                                                                                                           visited: 1                                                                     visited: 1                                                                                                   visited: 1                                                                                                                                                         visited: 1                                                                                                                                                                                                                                                           visited: 1                                                                                                                                                                                                    visited: 1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       type = removed
                                                                                                                                                                                                                                                                                                     type = removed call                                                            type = removed mov                                                                                           type = removed push                                                                                                                                                type = removed mov                                                                                                                                                                                                                                                   type = removed push                                                                                                                                                                                           type = removed push




                                                                                                                                                                                                                                                                                                                                                                                             Def of rax at 804846c
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      Def of rcx at 8048364                                                                                                                                                                                          Def of rcx at 80482c3
                                                                                                                                                                                                                                                                                                                                                                                             Def of rbx at 8048464                                                                                        Def of rsp at 80482e7                                                                                                                                                Def of rsp at 80483c0
                                                                                                                                                                                                                                                                                                                  call                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                Def of rsp at 8048371                                                                                                                                                                                          Def of rsp at 80482c9
                                                                                                                                                                                                                                                                                                                                                                                             Def of rsp at 8048469                                                                                        Def of rbp at 80482e5                                                                                                                                                Def of rbp at 80483c1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      Def of rbp at 804836f                                                                                                                                                                                          Def of rbp at 80482c0
                                                                                                                                                                                                                                                                                                                                                                                             Def of rbp at 8048461


                                                                                                                                                                                                                                                                                                     0x804841f:pop ebx                                                     0x8048471:cmp eax, 0xff<-0x01>                                                                                   0x80482e8:call 0x080482ed                                                                                                                                              0x80483c3:push edi                                                                                                                             0x8048340:push ebp                                                                                                     0x8048372:sub esp, 0x14                                                                                                                                                                                      0x80482ca:push edx
                                                                                                                                                                                                                                                                                                           visited:                                                                    visited: 1                                                                                                     visited: 1                                                                                                                                                         visited: 1                                                                                                                                     visited: 1                                                                                                                visited: 1                                                                                                                                                                                                visited: 1
                                                                                                                                                                                                                                                                                                     type = removed pop                                                          type = removed cmp                                                                                             type = removed call                                                                                                                                                type = removed push                                                                                                                            type = removed push                                                                                                       type = removed sub                                                                                                                                                                                        type = removed push




                                                                                                                                                                                                                                                                                                                                                                                             Def of rax at 804846c
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       Def of rcx at 8048364                                                                                                                                                                                         Def of rcx at 80482c3
                                                                                                                                                                                                                                                                                                                                                                                             Def of rbx at 8048464                                                                                                                                                                                                                                                             Def of rsp at 80483c3
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         call                                                                                                                                                                                                                                                                                                                  Def of rsp at 8048340        Use of rsp at 8048341                                                                      Def of rsp at 8048372                                                                                                                                                                                         Def of rsp at 80482ca
                                                                                                                                                                                                                                                                                                                                                                                             Def of rsp at 8048469                                                                                                                                                                                                                                                             Def of rbp at 80483c1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       Def of rbp at 804836f                                                                                                                                                                                         Def of rbp at 80482c0
                                                                                                                                                                                                                                                                                                                                                                                             Def of rbp at 8048461


                                                                                                                                                                                                                                                                                              0x8048420:add ebx, 0x0000117d                                                     0x8048474:je    0x0804848c                                                                                     0x80482ed:pop ebx                                                                                                                                                   0x80483c4:push esi                                                                                                                                                        0x8048341:mov ebp, esp                                                         0x8048375:mov      DWORD PTR ss:[esp], 0x00000028                                                                                                                                                                    0x80482cb:push 0x08048414
                                                                                                                                                                                                                                                                                                          visited:                                                                       visited: 1                                                                                                   visited:                                                                                                                                                           visited: 1                                                                                                                                                                   visited: 1                                                                                   visited: 1                                                                                                                                                                                              visited: 1
                                                                                                                                                                                                                                                                                                    type = removed add                                                              type = removed je                                                                                           type = removed pop                                                                                                                                                 type = removed push                                                                                                                                                          type = removed mov                                                                           type = removed mov                                                                                                                                                                                      type = removed push




                                                                                                                                                                                                                                                                                                                                                                                              Def of rax at 804846c
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        Def of rcx at 8048364                                                                                                                                                                                        Def of rcx at 80482c3
                                                                                                                                                                                                                                                                                                                                                                                              Def of rbx at 8048464                                                                                                                                                                                                                                                            Def of rsp at 80483c4                                                                                                                                                       Def of rsp at 8048340
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        Def of rsp at 8048372                                                                                                                                                                                        Def of rsp at 80482cb
                                                                                                                                                                                                                                                                                                                                                                                              Def of rsp at 8048469                                                                                                                                                                                                                                                            Def of rbp at 80483c1                                                                                                                                                       Def of rbp at 8048341
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        Def of rbp at 804836f                                                                                                                                                                                        Def of rbp at 80482c0
                                                                                                                                                                                                                                                                                                                                                                                              Def of rbp at 8048461


                                                                                                                                                                                                                                                                                  0x8048426:lea      eax, [ebx + 0xffffff20<-0x000000e0>]                                      0x8048476:lea esi, [esi + 0x00]                                                                           0x80482ee:add ebx, 0x000012af                                                                                                                                             0x80483c5:push ebx                                                                                                                                                        0x8048343:sub esp, 0x08                                                                     0x804837c:call 0x080482a0                                                                                                                                                                               0x80482d0:push 0x080483c0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          80482a0_f:malloc@plt
                                                                                                                                                                                                                                                                                                            visited:                                                                      visited: 1                                                                                                 visited:                                                                                                                                                            visited: 1                                                                                                                                                                   visited: 1                                                                                   visited: 1                                                                                                                                                                                              visited: 1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            type = removed
                                                                                                                                                                                                                                                                                                     type = removed lea                                                              type = removed lea                                                                                        type = removed add                                                                                                                                                  type = removed push                                                                                                                                                          type = removed sub                                                                           type = removed call                                                                                                                                                                                     type = removed push




                                                                                                                                                                                                                                                                                                                                                                                              Def of rax at 804846c
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 call
                                                                                                                                                                                                                                                                                                                                                                                              Def of rbx at 8048464                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  Def of rcx at 80482c3
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Def of rsp at 80483c5                                                                                                                                                       Def of rsp at 8048343                                                                                                                                                                                       Def of rcx at 8048364
                                                                                                                                                                                                                                                                                                                                                                                              Def of rsp at 8048469                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  Def of rsp at 80482d0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Def of rbp at 80483c1                                                                                                                                                       Def of rbp at 8048341                                                                                                                                                                                       Def of rsp at 804837c
                                                                                                                                                                                                                                                                                                                                                                                              Def of rbp at 8048461                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  Def of rbp at 80482c0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       Def of rbp at 804837c
                                                                                                                                                                                                                                                                                                                                                                                              Def of rsi at 8048476


                                                                                                                                                                                                                                                                                  0x804842c:lea      edi, [ebx + 0xffffff20<-0x000000e0>]                                0x8048479:lea     edi, [edi + 0x00000000]                                                                             0x80482f4:push edx                                                                                                                                                0x80483c6:sub esp, 0x0c                                                                                                                                 0x8048346:mov            eax, DWORD PTR ds:[0x080494cc]                                         0x8048381:mov      DWORD PTR ss:[ebp + 0xf0<-0x10>], eax                                                                                                              0x80482a0:jmp  DWORD PTR ds:[0x080495a8]               0x80482d5:push ecx
                                                                                                                                                                                                                                                                                                            visited:                                                                      visited: 1                                                                                                  visited:                                                                                                                                                            visited: 1                                                                                                                                                                  visited: 1                                                                                    visited:                                                                                                                                                    visited: 1                                  visited: 1
                                                                                                                                                                                                                                                                                                     type = removed lea                                                              type = removed lea                                                                                        type = removed push                                                                                                                                                  type = removed sub                                                                                                                                                          type = removed mov                                                                           type = removed mov                                                                                                                                           type = removed jmp                          type = removed push




                                                                                                                                                                                                                                                                                                                                                                                               Def of rax at 804846c
                                                                                                                                                                                                                                                                                                                                                                                                                                                           call
                                                                                                                                                                                                                                                                                                                                                                                               Def of rbx at 8048464
                                                                                                                                                                                                                                                                                                                                                                                                                                                 Def of rax at 8048346                                                                                                                                                                                                                                                                                                                                                           Def of rax at 8048346                                                                                                                                                                                                                                                                                                               Def of rcx at 80482c3
                                                                                                                                                                                                                                                                                                                                                                                               Def of rsp at 8048469                                                                                                                                                                                                                                                           Def of rsp at 80483c6
                                                                                                                                                                                                                                                                                                                                                                                                                                                 Def of rax at 804834f                                                                                                                                                                                                                                                                                                                                                           Def of rsp at 8048343       Use of rax at 804834b                                                                                                                                                                                                                                                                                   Def of rsp at 80482d5
                                                                                                                                                                                                                                                                                                                                                                                               Def of rbp at 8048461                                                                                                                                                                                                                                                           Def of rbp at 80483c1
                                                                                                                                                                                                                                                                                                                                                                                                                                                 Def of rsp at 8048288                                                                                                                                                                                                                                                                                                                                                           Def of rbp at 8048341                                                                                                                                                                                                                                                                                                               Def of rbp at 80482c0
                                                                                                                                                                                                                                                                                                                                                                                               Def of rsi at 8048476
                                                                                                                                                                                                                                                                                                                                                                                                                                                 Def of rbp at 8048288
                                                                                                                                                                                                                                                                                                                                                                                               Def of rdi at 8048479


                                                                                                                                                                                                                                                                                                   0x8048432:sub eax, edi                                                         0x8048480:sub ebx, 0x04                                                                0x80482f5:mov   eax, DWORD PTR ds:[ebx + 0xfffffffc<-0x00000004>]                                                                                                                   0x80483c9:call 0x080483ce                                                                                                                                    0x804834b:test eax, eax                                                                  0x8048384:mov       DWORD PTR ss:[ebp + 0xf4<-0x0c>], 0x00000000                                                                                                                                                                   0x80482d6:push esi
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               call
                                                                                                                                                                                                                                                                                                           visited:                                                                        visited: 1                                                                                                 visited:                                                                                                                                                         visited: 1                                                                                                                                                 visited: 1                                                                                                     visited:                                                                                                                                                                                                   visited: 1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     Def of rsp at 804827e
                                                                                                                                                                                                                                                                                                     type = removed sub                                                              type = removed sub                                                                                        type = removed mov                                                                                                                                                type = removed call                                                                                                                                        type = removed test                                                                                           type = removed mov                                                                                                                                                                                          type = removed push
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     Def of rbp at 804827e



                                                                                                                                                                                                                                                                                                                                                                                               Def of rax at 804846c
                                                                                                                                                                                                                                                                                                                                                                                               Def of rbx at 8048480
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              Def of rax at 8048346                                                                                                                                                                                                                                                                                                  Def of rcx at 80482c3
                                                                                                                                                                                                                                                                                                                                                                                               Def of rsp at 8048469
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               call                                                                                                                                                           Def of rsp at 8048343                                                                                                                                                                                                                                                                                                  Def of rsp at 80482d6
                                                                                                                                                                                                                                                                                                                                                                                               Def of rbp at 8048461
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              Def of rbp at 8048341                                                                                                                                                                                                                                                                                                  Def of rbp at 80482c0
                                                                                                                                                                                                                                                                                                                                                                                               Def of rsi at 8048476
                                                                                                                                                                                                                                                                                                                                                                                               Def of rdi at 8048479


                                                                                                                                                                                                                                                                                                                                                               jmp_if
                                                                                                                                                                                                                                                                                                  0x8048434:sar eax, 0x02                                                            0x8048483:call eax                                                                                              0x80482fb:test eax, eax                                                                                                                                        0x80483ce:pop ebx                                                                                                                                   0x804834d:je    0x08048361                                                                 0x804838b:mov       DWORD PTR ss:[ebp + 0xf4<-0x0c>], 0x00000000                                                                                                                                                              0x80482d7:push 0x08048364
                                                                                                                                                                                                                                                                                                                                                       Def of rax at 804846c
                                                                                                                                                                                                                                                                                                           visited:                                                                        visited: 1                                                                                                        visited:                                                                                                                                                     visited:                                                                                                                                               visited: 1                                                                                                      visited:                                                                                                                                                                                                  visited: 1
                                                                                                                                                                                                                                                                                                                                                       Def of rbx at 8048464
                                                                                                                                                                                                                                                                                                     type = removed sar                                                              type = removed call                                                                                               type = removed test                                                                                                                                          type = removed pop                                                                                                                                      type = removed je                                                                                             type = removed mov                                                                                                                                                                                         type = removed push
                                                                                                                                                                                                                                                                                                                                                       Def of rsp at 8048469
                                                                                                                                                                                                                                                                                                                                                       Def of rbp at 8048461


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 Def of rax at 8048346                                                                                                                                                                                                                                                                                               Def of rcx at 80482c3
                                                                                                                                                                                                                                                                                                                                                                                             call                                                                                                                                                                                                                                                                                                                                                                                                                                                Def of rsp at 8048343                                                                                                                                                                                                                                                                                               Def of rsp at 80482d7
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 Def of rbp at 8048341                                                                                                                                                                                                                                                                                               Def of rbp at 80482c0


                                                                                                                                                                                                                                                                                                  0x8048437:sub esp, 0x0c                                          0x8048485:mov       eax, DWORD PTR ds:[ebx]                                                                                       0x80482fd:je   0x08048301                                                                                                                           0x80483cf:add ebx, 0x000011ce                                                                                                                                    0x804834f:mov eax, 0x00000000                                                                                 0x8048392:jmp 0x080483a6                                                                                                                                                                                 0x80482dc:call 0x080482b0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     80482b0_f:__libc_start_main@plt
                                                                                                                                                                                                                                                                                                           visited:                                                                     visited:                                                                                                              visited:                                                                                                                                               visited:                                                                                                                                                         visited: 1                                                                                                  visited:                                                                                                                                                                                                 visited: 1
                                                                                                                                                                                                                                                                                                                                                                                                                                       jmp_if                                                                                                                                                                                                                                                                                                                                                          call                                                                                                                                                                                                                                                                                                 type = removed
                                                                                                                                                                                                                                                                                                     type = removed sub                                                          type = removed mov                                                                                                      type = removed je                                                                                                                                     type = removed add                                                                                                                                               type = removed mov                                                                                          type = removed jmp                                                                                                                                                                                       type = removed call




                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    call
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Def of rax at 804834f
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          Def of rcx at 80482c3
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Def of rsp at 8048343     Use of rax at 8048354                                                         jmp
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          Def of rsp at 80482dc
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Def of rbp at 8048341
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          Def of rbp at 80482dc


                                                                                                                                                                                                                                                                                            0x804843a:lea  esi, [eax + 0xff<-0x01>]                                             0x8048487:cmp eax, 0xff<-0x01>                                                                                                                     0x80482ff:call eax                                                                                                       0x80483d5:call 0x08048278                                                                                                                                                                0x8048354:test eax, eax                                        0x80483a6:cmp        DWORD PTR ss:[ebp + 0xf4<-0x0c>], 0x09                                                                                                                0x80482b0:jmp  DWORD PTR ds:[0x080495ac]                 0x80482e1:hlt
                                                                                                                     8048310_f:__do_global_dtors_aux                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                8048278_f:_init
                                                                                                                                                                                                                                                                                                           visited:                                                                          visited:                                                                                                                                    visited:                                                                                                                     visited:                                                                                                                                                                               visited: 1                                                                          visited:                                                                                                                                                       visited: 1                                   visited:
                                                                                                                             type = removed                                                                                                                                                                                                                                                                                                                                                                          jmp_if                                                                                                                                                                                                        type = removed
                                                                                                                                                                                                                                                                                                     type = removed lea                                                               type = removed cmp                                                                                                                           type = removed call                                                                                                          type = removed call                                                                                                                                                                    type = removed test                                                                type = removed cmp                                                                                                                                              type = removed jmp                           type = removed hlt




                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      Def of rax at 804834f
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               call                                                                                                                                                                   call                                                                                                                                            Def of rsp at 8048343
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      Def of rbp at 8048341


                                                                                                                         0x8048310:push ebp                                                                                                                                                      0x804843d:jmp 0x08048444                                                                     0x804848a:jne 0x08048480                                                                                                            0x8048301:pop eax                                                                              0x80483da:lea       eax, [ebx + 0xffffff20<-0x000000e0>]                                       0x8048278:push ebp                                                                    jmp_if                       0x8048356:je    0x08048361                                                          0x80483aa:jle 0x08048394
                                                                                                                               visited: 1                                                                                                                                                                  visited:                                                                                     visited:                                                                                                                        visited:                                                                                                           visited: 1                                                                 visited: 1                                                              Def of rax at 8048346                         visited: 1                                                                           visited:
                                                                                                                         type = removed push                                                                                                                                                         type = removed jmp                                                                           type = removed jne                                                                                                              type = removed pop                                                                                                 type = removed lea                                                         type = removed push                                                           Def of rsp at 8048343                    type = removed je                                                                   type = removed jle
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              Def of rbp at 8048341