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

   Thank you for recommending me the VM spec...

  But my prob is a little different,

	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.

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

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

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

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

	Please help me in this regard.

Thank you
Reddy
		
	




>-----Original Message-----
>From: Andrew Huntwork [mailto:[email protected]] 
>Sent: Dienstag, 8. Februar 2005 16:01
>To: BCEL Users List
>Subject: Re: Query regarding index of local variables
>
>
>First, let me recommend a book to you that you may already know about:
>http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTO
>C.doc.html
>
>read carefully enough, this book will tell you everything you need to 
>know to screw around with class files to your heart's content.
>
>see below.
>
>Koduru, Rajendra Kumar Reddy wrote:
>[...]
>> 	// code
>> 
>> 		int i = methodgen.getLocalVariables().length
>> 		my_lv1 = i+1;
>> 		my_lv2 = i+2;
>> 		my_lv =  i+3;
>> 
>> 	// code
>> 
>> 	and I used to store using il.append ( new ASTORE ( my_lv2 ) );
>> 
>> 
>> 	 I encountered a problem with this, i.e. the variable i returned
>> 22,
>> 	 where as the max index is 7.. So there was overlap in the local
>> vars and my class file got corrupted
>
>there is no relationship between number of local variables and 
>the first 
>free slot.  each LocalVariable describes the type contained in a slot 
>over a particular range of instructions.  there are 0 or more 
>LocalVariables for each slot.  You should print out the various fields 
>in your LocalVariables just to get a feel for what they contain, 
>especially the start index, end index and type.
>
>what you want is:
>
>setMaxLocals(); //if you have inserted variables already
>int i = methodgen.getMaxLocals();
>...
>
>In the project I used to work on (sandmark.cs.arizona.edu), 
>almost every 
>occurrence of getLocalVariables was a bug.  That's because the local 
>variable table may be incomplete, inaccurate (to some extent), 
>obfuscated, or just plain missing (MethodGen.removeLocalVariables() or 
>something like that).
>
>> 
>> 
>> 
>> 	 I just wanted to know..why is it like that.. Doesn't each local
>> variable have unique index?
>> 
>> 	 Isnt the length (i that is returned) mean the no of local
>> variables in the corresponding method?
>> 
>> 	However now I tried using 
>> 
>> 	// code
>> 
>> 	int max_index = 0;
>> 
>>      LocalVariableGen[] lv1 = mgen.getLocalVariables();
>> 	
>>     for(int i = 0; i< lv1.length; i++){
>> 
>>       if(lv1[i].getIndex() > max_index)
>>           max_index = lv1[i].getIndex();
>>       }	
>> 		
>> 		my_lv1 = i+1;
>> 		my_lv2 = i+2;
>> 		my_lv =  i+3;
>> 	
>> 	// code
>> 
>> 	and it works fine..
>> 
>> 	but I just wanted to clarify it.
>> 
>> 	Please help me in this regard.
>> 
>> 
>> Thank you
>> Reddy
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>> 
>> 
>
>---------------------------------------------------------------------
>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.