<div dir="ltr">


  
  

<div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div bgcolor="#ffffff" text="#000000"><div><div class="Wj3C7c">
<tt>My apologies for the vague question formulation. I'll explain my
objective with an example. <br>
<br>
My initial source is as follows:<br>
</tt><tt>unsigned int fib(unsigned int m) {<br>
  unsigned int f0 = 0 ;<br>
  unsigned int f1 = 1, f2, i ;<br>
  unsigned int l1 = 0;<br>
  if (m <= 1)  {<br>
</tt><tt>    MARKSTART();<br>
    printf("Return\n");<br>
</tt><tt>    MARKEND();</tt><br>
<tt>    return m;<br>
  } else {<br>
    for (i = 2; i <=m; i++) {<br>
      f2 = f0 + f1;<br>
      f0 = f1;<br>
      f1 = f2;<br>
      for (l1 = 0; l1 <5; l1++) {<br>
        printf("woof\n");<br>
        MARKSTART();<br>
      }<br>
      MARKEND();<br>
      MARKSTART();<br>
    }<br>
    MARKEND();<br>
    return f2;<br>
  }<br>
}<br>
<br>
The MARKSTART and MARKEND represent MACROs that implement a certain
instruction in a particular processor. The semantics of the MARKEND and
MARKSTART can be simply thought of as starting and stopping cycle
timers for segments of code between the MARKSTART and MARKEND. The
resulting code after the transformation is as follows:</tt><tt><br>
unsigned int fib(unsigned int m) {<br>
  unsigned int f0 = 0 ;<br>
  unsigned int f1 = 1, f2, i ;<br>
  unsigned int l1 = 0;<br>
  if (m <= 1)  {<br>
    MARKSTART0("fib_seq0");<br>
    printf("Return\n");<br>
</tt><tt>    MARKEND0("fib_seq0");</tt><br>
<tt>    return m;</tt><br>
<tt>  } else {<br>
    for (i = 2; i <=m; i++) {<br>
      MARKSTARTCOUNT("bb_k");<br>
      f2 = f0 + f1;<br>
      f0 = f1;<br>
      f1 = f2;<br>
</tt><tt>      MARKENDCOUNT("bb_k");</tt><br>
<tt>      for (l1 = 0; l1 <5; l1++) {<br>
        printf("woof\n");<br>
        MARKSTART2("fib_loop1");<br>
      }<br>
      MARKEND2("fib_loop1");<br>
      MARKSTART1("fib_loop0");<br>
    }<br>
    MARKEND1("fib_loop0");<br>
    return f2;<br>
  }<br>
}<br>
<br>
The number after a MARKSTART and MARKEND denotes a timer in the
processor and the string, a unique label identifying blocks of interest
used during profiling this code. We have certain rules that form a
block, such as, a MARKSTART must always follow by a MARKEND and use of
timers must be correctly nested. Notice the MARK*COUNT as only one
example of labeling one basic block in the source. I would like to add
these for every BB in the CFG as well. So the tasks here are:<br>
<br>
- Identify MARKSTART and MARKEND, allocate a cycle timer such that
there are no conflicts between the START and END of a block. <br>
- Identify MARKSTART and MARKEND, add a unique label used in profiling.
The labels much match the START and END that form a block.<br>
- For every basic block, add MARKSTARTCOUNT and MARKENDCOUNT
respectively with a unique BB label.<br>
<br>
<br>
</tt>
</div></div><div class="Ih2E3d"><blockquote type="cite"> 
  <tt>Are you talking about determining where in a function/method body
you would want to insert code, or are you scanning for particular
statements to replace?
  <br>
  </tt></blockquote></div><div class="Ih2E3d">
I want to scan the methods and insert code as described above. There
are two levels of source changes: a) MARKSTARTCOUNTs from basic block
information and b) transformation of MARKSTART and MARKENDs with unique
labels and timer numbers.<br>
</div><blockquote type="cite"><tt><br>
  </tt>
  <blockquote type="cite"><tt><br><div class="Ih2E3d">
2. Replace the statements with certain macros with additional labels.
    <br>
    </div></tt></blockquote><div class="Ih2E3d">
  <tt><br>
Which statements are you replacing?  Inserting macros should be fairly
straightforward by using the rewriter (this is a textual translation by
editing the source code, not editing the ASTs).
  <br>
  </tt></div></blockquote><div class="Ih2E3d">
I believe the translation for MARKSTART and MARKEND are possible in the
straightforward manner. However, I'm not so sure about the basic block
information. <br>
</div><blockquote type="cite">
  <blockquote type="cite"><tt><br><div class="Ih2E3d">
4. Output the changed C file.
    <br>
    </div></tt></blockquote><div class="Ih2E3d">
  <tt><br>
If you are using the rewriter to do your edits, this is very
straightforward.  I would like at how the Objective-C -> C
translator (RewriteObjC) uses the Rewriter to make edits to the source
file and then blast the new source to disk.
  <br>
  <br>
  </tt>
  <blockquote type="cite"><tt><br>
    <br>
I've successfully tried the following:
    <br>
    <br>
- Added an Analysis consumer, but I noticed that this Analysis consumer
method is invoked twice; once for each function in the C source. I'd
like to have access to the full Analysis and then walk it using walker
methods.
    <br>
- Added an ASTConsumer, but again, this is invoked twice. Once per
function.
    <br>
    <br>
    </tt>
  </blockquote>
  <tt><br>
I'm not certain what you mean by the "invoked twice" part.  ASTConsumer
implements an action/visitor interface where it receives callbacks for
each top level declaration (HandleTopLevelDecl) as well as a single
call back once the entire translation unit has been parsed
(HandleTranslationUnit).
  <br>
  </tt></div></blockquote><div class="Ih2E3d">
Sorry. I sort of figured this out while learning more about clang. <br>
</div><blockquote type="cite"><tt><br>
  <br><div class="Ih2E3d">
My gut feeling is that you want to use the Objective-C rewriter as your
model, and that you want to rewrite pieces of the C source file by
removing some statements and inserting others.  You're probably just
going to want to write a fresh ASTConsumer that contains a Rewriter
object.  You can either scan for functions/methods to edit using
HandleTopLevelDecl, or you can wait until HandleTranslationUnit is
called to scan all the declarations at once.  In HandleTranlsationUnit
you would also call the appropriate methods on the Rewriter to emit
your changed source code to disk.  If you need CFGs, it's very easy to
create a CFG using a single method: CFG::BuildCFG (just pass as an
argument the Stmt* that represents the body of the function or method
you want to analyze).
  </div></tt></blockquote>
I will look more closely into the rewriter.<div class="Ih2E3d"><br>
<br>
Many thanks for your detailed responses.<br>
<br>
Regards,<br>
~Hiren<br>
<br>
</div></div>


</blockquote></div><br></div>