Re: writing two different bytecode parts at the same time

Eric Bruneton <[email protected]>
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <[email protected]>
Eugene Kuleshov wrote:
> 
>  If I understand you correctly, right now, the closest would be to use 
> several MethodNode instances from the tree package (those implement 
> MethodVisitor interface) and then use InsnLists from those instances to 
> assemble them together and call insnList.accept(methodVisitor) after 
> that to get the result.

for me the simplest solution is to generate two versions of each method, 
one for the slow path, the other for the fast path. Indeed you can use 
several MethodVisitor at the same time, provided they are related to 
different methods. Hence this code

 >> mv1.visitInsn(ILOAD, 1)
 >>
 >> mv2.visitInsn(ILOAD, 1)
 >> mv2.visitMethodcall(SBA.box(I)Object)

is ok, provided mv1 and mv2 refer to different methods (and use 
different labels - you cannot share a label between two visitors).

>  On the other hand I recall this is a common scenario for compilers 
> (Janino and Kawa bytecode libs both have support for that). So we 
> probably should add a new MethodWriter API to provide a more lightweight 
> support, e.g. allow work with ByteVector trough MethodVisitor API. 
> Perhaps add a method into the MethodWriter class like MethodVisitor 
> branch(); which could record the current position and then would insert 
> bytecodes emitted trough returned MethodVisitor right into that saved 
> position. Eric, what do you say about that?

it would really complicate the implementation of MethodWriter 
(especially for bytecode offsets). Also returning a MethodVisitor in a 
branch() method is not very good; it would be better to return a 
CodeVisitor (restricted to visitXxxInsn methods), thus requiring a new 
interface. Finally it would be strange to have this branch() method only 
in MethodWriter, and not in the general MethodVisitor interface. But 
supporting this generalization would be really complicated, including 
for users. In summary I don't think this is good idea.

Eric

> 
> Jochen Theodorou wrote:
>> Hi all,
>>
>> I am currently experimenting in Groovy with a special way of compiling 
>> expressions. One way is the classic way, the other is more or less 
>> java style static compilation. Since I want to switch between them at 
>> runtime and express it completely in bytecode I have currently a small 
>> problem.
>>
>> So for example I would compile
>>
>> void foo(int x) {
>>   x++
>> }
>>
>> into something like this
>>
>> if !fastpath GOTO slowpath
>>   ILAOD 1
>>   IINC
>>   ISTORE
>> slowpath:
>>   x' = box(x)
>>   x' = x'.next()
>>   x  = unbox(x)
>>
>> I hope you guys forgive me for using some shortcuts here, but it 
>> should give away the idea
>>
>> Currently I can switch the compiler to do either the one thing or the 
>> other, but I would like to do both at the same time. Ideally I would 
>> like to do that with two method visitors on the same method. Then I 
>> could do (not real API, I know):
>>
>> mv1.visitInsn(ILOAD, 1)
>>
>> mv2.visitInsn(ILOAD, 1)
>> mv2.visitMethodcall(SBA.box(I)Object)
>>
>> to handle loading x in the fast path (mv1) and loading x in the slow 
>> path (mv2). That means I can handle both ways while handling the x in 
>> x++ and have not to duplicate the my AST, or revisit it.
>>
>> So basically I would like to be able to define a label and have one 
>> method visitor write after it, while another one can write before it. 
>> Or a way to somehow jump
>>
>> Is there anything slightly like that in th asm lib I can use?
>>
>> bye Jochen
>>
> 
>
message-footer.txt (text/plain, 238 B)
-- 
You receive this message as a subscriber of the [email protected] mailing list.
To unsubscribe: mailto:[email protected]
For general help: mailto:[email protected]?subject=help
OW2 mailing lists service home page: http://www.ow2.org/wws
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.