RE: Query regarding index of local variables

"Koduru, Rajendra Kumar Reddy" <[email protected]>
Newsgroups gmane.comp.jakarta.bcel.user
Message-ID <[email protected]>
Hi Andrew, 

  I have gone thru this concept of assigning a slot to local variables
by observing the local variable tables of  the class files, yep each
slot is being taken by many local var. so I used getMaxLocals() and it
worked fine (removed my own way of fidning max index, which sometimes is
causing an error due to incomplete lv table).

Please find a comment inline regarding the comment made by you "stack
height mismatch verification error".



	
 

>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.



	Andrew, I did not get that type of error, however could you
please let me know , why were you expecting this??
	in non void methods, I was using 

	1.	new ASTORE(x) 	// storing the result
	2.	jsr call		// calling jsr
	3.	new ALOAD(x)	// loading back the result
	4.	return		// returning the result

	and all the branches were reidrected to 1 (i.e store statement)
in case of non void methods

	so could you please let me know , why were you expecting an
error?? So that I could rectify it
	am I right in doing like this???

	However just to mention, I was also using methodgen.setMaxStack
before returning the isntrumented method



>
>> 	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: [email protected]
>For additional commands, e-mail: [email protected]
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.