How to introspect method instance variables

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to introspect method instance variables

rdmerrio
I have defined a method, i.e.,

someMethod
    anInstVariable := anotherInstVariable1 + anotherInstVariable2.

I would like to intercept the acceptance of this method by the browser
and programatically determine what instance variables this method is
using so that I can grab these names for other processing tasks.

Additionally, I would really like to be able to determine what instance
variables are being assigned to, for instance, anInstVariable in this
case and which ones are the "independent" instance variables,
anotherInstVariable1 and anotherInstVariable2 in this case.

How can I do this?

Thanks
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to introspect method instance variables

Michael van der Gulik-2


On Tue, Aug 25, 2009 at 2:16 PM, rdmerrio <[hidden email]> wrote:
I have defined a method, i.e.,

someMethod
  anInstVariable := anotherInstVariable1 + anotherInstVariable2.

I would like to intercept the acceptance of this method by the browser and programatically determine what instance variables this method is using so that I can grab these names for other processing tasks.

Additionally, I would really like to be able to determine what instance variables are being assigned to, for instance, anInstVariable in this case and which ones are the "independent" instance variables, anotherInstVariable1 and anotherInstVariable2 in this case.

How can I do this?

Why? What are you trying to achieve? You're talking about some pretty intrusive techniques. Unless you're developing a code analyser of some sort, you probably should be looking at a better way of doing what you're doing.

To capture the acceptance of a method (assuming you mean the action that happens when you press alt+s), you insert a bit of code into PluggableTextMorph>>accept.

To determine which instance variables are being assigned to, you'll need to somehow look at the bytecodes. They're not too hard to analyse, but it can be a bit of work. Alternatively, maybe the refactory browser can help, or maybe you can look at the intermediate code that the compiler generates.

The bytecodes are described here: http://burks.bton.ac.uk/burks/language/smaltalk/goldberg/blueb003.htm. They're in the "Blue book chapter 28" if you need to Google it. You'll want the "store" bytecodes.

To see real bytecodes, either inspect "Morph>>#basicInitialize" to see a CompiledMethod, or use the "byteCodes" view in a Browser (hidden behind the "source" button).

Gulik.

--
http://gulik.pbwiki.com/

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to introspect method instance variables

Yoshiki Ohshima-2
In reply to this post by rdmerrio
At Mon, 24 Aug 2009 21:16:16 -0500,
rdmerrio wrote:

>
> I have defined a method, i.e.,
>
> someMethod
>     anInstVariable := anotherInstVariable1 + anotherInstVariable2.
>
> I would like to intercept the acceptance of this method by the browser
> and programatically determine what instance variables this method is
> using so that I can grab these names for other processing tasks.
>
> Additionally, I would really like to be able to determine what instance
> variables are being assigned to, for instance, anInstVariable in this
> case and which ones are the "independent" instance variables,
> anotherInstVariable1 and anotherInstVariable2 in this case.
>
> How can I do this?

  The "why" question Michael asked is an interesting one, but I can
see many potential use of it.  (I've been writing a few automatic inst
var access rewriting compilers and friends with similar techniques.)

  During the compilation, the Compiler uses Encoder to support the
compilation and Encoder keeps track of the accesses of variables.

Encoder>>init:context:notifying:

populates "scopeTable" inst var with VariableNodes.  When an
assignment to the variable is encountered, #nowHasDef message is sent
to the node.  When a reference is encountered, #nowHasRef message is
sent to the node.  The default behavior is do nothing, but you can
define methods like (in the latest image, the actual class name varies
based on the Squeak version):

InstanceVariableNode
nowHasRef
        Transcript show: 'ref: '; show: self printString; cr.

InstanceVariableNode
nowHasDef
        Transcript show: 'def: '; show: self printString; cr.

and you get the list of these accesses.

To be less intrusive, you can store these info into somewhere and
analyze it.

-- Yoshiki

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners