On Tue, Dec 22, 2009 at 09:38:26PM -0700, ThanhVu (Vu) Nguyen wrote:
> is there an easy way to add a new local variable ?   e.g., 

makeLocalVar, as you spotted.  Or makeTempVar.

> I realize that this has several instructions instead of just one instruction
> like before

That's precisely why vinst returns a *list* of instructions: to let you
add some on the fly.  ChangeTo([i1; i2; i3]).

> does it mean I need to use a different CilVisitor like 
> visitCilFunction.   Current I use visitCilFileSameGlobal  

You seem not to understand how the visitor works.  A visitor is an
object, whose methods drive the traversal of the Abstract Syntax Tree
(AST).  By default, the nopCilVisitor methods have the following
behaviour: visit every node, do not modify any (you can think of it as
the identity).  When you inherit the nopCilVisitor, you inherit this
behaviour too.  Then, you overload the methods according to your needs,
to drive the traversal and modify things when needed.

Thus, a single visitor object/class may have many methods (re)defined;
as a matter of fact, it will always have every method you can find in
http://hal.cs.berkeley.edu/cil/api/Cil.cilVisitor.html.  The methods
that you do not write will simply be inherited from nopCilVisitor
(which blindly return DoChildren in every method).

What you need here, is to focus on the instructions (since you want to
change some Call(...)).  Thus, you probably need to define vinst and/or
vstmt.  Moreover, if you need to remember the current fundec (to use
makeLocalVar), you could define vfunc so that it stores it in a member of
your class.  All of this is of course happening *in the same visitor*;
you need those tasks to cooperate.

Here is a class I defined for my own needs:

class enclosingFunction = object(self)                           
  inherit mynopCilVisitor

  val mutable cur_fundec = dummyFundec

  method vfunc f =
    cur_fundec <- f;
    DoChildren
end

That way, I can write new visitors which "know" the current fundec (it
is stored in cur_fundec):

class bar = object(self)
  inherit enclosingFunction

  method ... =
    ...
    makeTempVar cur_fundec type
    ...
end

Of course, I shall not define vfunc when I inherit enclosingFunction,
otherwise cur_fundec won't be update anymore (except if I call
ignore(super#vfunc f)).

Regards,
-- 
Gabriel Kerneis

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
CIL-users mailing list
CIL-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/cil-users

Reply via email to