jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrew Huntwork <...@huntwork.net>
Subject Re: Query regarding index of local variables
Date Tue, 08 Feb 2005 16:39:05 GMT
Koduru, Rajendra Kumar Reddy wrote:
> Hi Andrew
> 
>    Thank you for recommending me the VM spec...

Reading the VM spec is not as stupid an idea as it might sound.  I'm not 
aware of any more concise or helpful reference for bytecode 
manipulators.  Can anyone suggest a better one?

> 
>   But my prob is a little different,

It seems that your problem can be summarized as, "How do i choose a free 
slot?"  That's the question i answered in my previous email.  see below 
for further explanation.

> 
> 	as you have said I can add local variables using 
> 
> 	LocalVar var = localvargen.addLocalvar()
> 	int index = var.getIndex();
> 	llcalvargen.setstart( ilist.append ( new ASTORE ( index ) ) )
> 	.
> 	.
> 	. 
>   
> 	as many as required
> 
> 	and at the calling setMaxLocals();
> 
> 	That is OK.

none of this is actually required.  the local variable table is just for 
debugging purposes, and as i said before, may be inaccurate, incomplete, 
obfuscated, or missing.  feel free to ignore LocalVar objects completely 
and just choose some slots yourself and use them.

The usual way of choosing slots, as far as I'm aware, is to call 
getMaxLocals:

MethodGen mg = ...
int freeSlot = mg.getMaxLocals();
int freeSlot2 = freeSlot + 1;
mg.getInstructionList().append(new ASTORE(freeSlot));
...

> 
> 	but now consider a scenario like this
> 	
> 	public int getI(){
> 		return 2;
> 	}
> 
> 	Now I am trying to insert try-finally to this method by
> instrumenting in bytecode 
> 
> 	which should look like  
> 
> 	public int getI(){
> 	try{
> 		return 2;
> 	}finally { 
> 			System.out.println("sample");
> 	}
> 	
> 	}	
> 
> 	To create this, I should create a empty catch block which throws
> the exception that is not handled and redirect all the calls to finally
> before the 
> 	return statement
> 
> 	now comes the problem with local variables.
> 
> 
> 	I will get the instructionlist
> 	I will find the instance of return, and insert a JSR instr
> before this return and redirect all the branches

btw, when you get past your other problems, it seems like you might have 
a stack height mismatch verification error at your subroutine.  ask 
about that if it happens.  if i'm right, it'll happen in any non-void 
method you instrument.

> 	Next step is creation of catch block, where I have a problem
> 			here I should store the exception, make a jsr
> call, load the exception which I stored before and rethrow it
> 
> 	so I use ilist.append ( new ASTORE ( ? ) )
> 		   ilist.append ( new JSR() );
> 		   ilist.load ( new ALOAD ( ? ) )
> 	
> 	so what should I use in the place of "?"  (2,3,4......)

i suggest mg.getMaxLocals()

> 
> 	same while creating finally block, I should store the return
> address
> 	
> 		   ilist.append ( new ASTORE ( ?? ) )
> 		   // performs the instrumentation of system.out.....
> 		   ret ?? 
> 	
> 	so what should I use in place of "?"  (2,3,4......)

i suggest mg.getMaxLocals() + 1

> 
> 	so what I am doing is 
> 	
> 	getting the max_index (maximum index) available in the
> localvargen 
> 	and assigning max_index+1 to the first ?
> 	and assigning max_index+2 to the second ??
> 
> 	am I wrong in doing like these?????

If the local variable table were complete and accurate, this method 
would be correct, but you should not assume that.  getMaxLocals() always 
returns a free slot (assuming you call setMaxLocals() appropriately) and 
frees you from writing your own code to find max_index.

> 
> 	is there some other efficient way for doing this???

getMaxLocals()

> 
> 	Please help me in this regard.
> 
> Thank you
> Reddy
> 		

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Mime
View raw message