Daneel: The difference between Java and Dalvik

Those of you who kept following IcedRobot might have seen that quite some work went into Daneel over the past months. He1 is in charge of parsing Android applications containing code intended to run on a Dalvik VM and transforming this code into something which can run on any underlying Java VM. So he is a VM compatible with Dalvik on top of a Java VM, or at least that’s what he wants to become.

So Daneel is multilingual in a strange way, he can read and understand Dalvik bytecode, but he only speaks and writes Java bytecode. To understand how he can do that we have to look at the differences between those two dialects.

Registers vs. Stack: We know Dalvik bytecode uses a register-machine, and Java bytecode uses a stack-machine. But each method frame on that stack-machine not only has an operand stack, it also has an array of local variables. Unfortunately this distinction is lost in our register-machine. To understand what this means, let us look at a full Java-Dalvik-Daneel round-trip for a simple method like the following.

public static int addConst(int val) {
   return val + 123456;
}

The first stop on our round-trip is the Java bytecode. So after we push this snippet through javac we get the following code which makes use of both, an operand stack and local variables.

public static int addConst(int);
  [max_stack=2, max_locals=1, args_size=1]
   0: iload_0
   1: ldc #int 123456
   3: iadd
   4: ireturn

The second stop takes us to the Dalvik bytecode. We push the above code through the dx tool and are left with the following code. Note that the distinction between the operand stack and local variables is lost completely, everything is stored in registers.

public static int addConst(int);
  [regs=2, ins=1, outs=0]
   0: const v0, #0x1E240
   1: add-int/2addr v0, v1
   2: return v0

The third and last step is Daneel reading the Dalvik bytecode and trying to reproduce sane Java bytecode again. The following is what he spits out after chewing on the input for a bit.

public static int addConst(int);
  [max_stack=2, max_locals=2, args_size=1]
   0: ldc #int 123456
   1: istore_1
   2: iload_1
   3: iload_0
   4: iadd
   5: istore_1
   6: iload_1
   7: ireturn

The observant reader will notice the vast difference between what we had at the beginning of our round-trip and what we ended up with. Daneel maps each Dalvik register to a Java local variable. Fortunately any decent Java VM will optimize away the unnecessary load and store instructions and we can achieve acceptable performance with this naive approach already.

Untyped Instructions: Another big difference might not be that obvious at first glance. Notice how the instruction at label 0 in the above Dalvik bytecode (the second stop on our round-trip) accesses register v0 without specifying the exact type of that register? The only thing Daneel can determine at that point in the code is that it’s a 32-bit value we are dealing with, it could be an int or a float value. For zero-constants it could even be a null reference we are dealing with. The exact type of that register is not revealed before the instruction at label 1, where v0 is read again by a typed instruction. It’s at that point that we learn the exact type of that register.

So Daneel has to keep track of all register types while iterating through the instruction stream to determine the exact types and decide which Java bytecode instructions to emit. I intend to write a separate article about how this is done by Daneel in the following days, so stay tuned.

Disclaimer: This is a technical description of just two major differences between Dalvik bytecode and Java bytecode. All political discussions about differences or similarities between Dalvik and Java in general are outside the scope of this article and I won’t comment on them.

1 Yes, Daneel is male. His girlfriend is called Ika. Together they love to drink iced tea because they try to get off caffeine. They even have a butler working for them who is called Jenkins, a very lazy guy who regularly was seen to crash during work.

An interesting discussion on

An interesting discussion on the difference between Java and Dalvik. Quite educating. Thanks for sharing.

An interesting discussion on

An interesting discussion on the difference between Java and Dalvik. Quite educating. Thanks for sharing. [url=https://www.bedford-flooring.co.uk] Bedford flooring [/url]

Very interesting discussion

Very interesting discussion on the difference between Java and Dalvik, I have really learned a lot all thanks to you. Please keep us updated.

Thank you for sharing! Such

Thank you for sharing! Such great content, glad I found this blog!!

Very interesting discussion

Very interesting discussion on the difference between Java and Dalvik, I have really learned a lot all thanks to you. Please keep us updated

You wrote well and the

You wrote well and the content is very informative. Mind, if I share my website, feel free to click the link https://www.goidahoinsurance.com/life-insurance-boise to learn more about our insurance services.

This is a great site

This is a great site

There is such great

There is such great information on this site

This is a great post, the

This is a great post, the differences of each one have their own functions. Visit now this site here https://www.a1treeservicespokane.com/

Thanks for posting this

Thanks for posting this article on the difference between Java and Dalvik. It's very informative and helpful. Some persons think they are similar, but article really highlights the difference in their features. Great work
Cheers
https://www.derbyroofers.co.uk

This is great content, thanks

This is great content, thanks for sharing the differences as programming languages become more abundant it's important to know how they differ. https://www.roofingthunderbay.com/

Cant believe this article

Cant believe this article made soo much sense now. You really have a talent for this! Continue pressing on!

Thanks for this nice script.

Thanks for this nice script. Check out https://www.builditspokane.com/

Hello, I am looking forward

Hello, I am looking forward to another great post from you.

I was very encouraged to find

I was very encouraged to find this site. The reason being that this is such an informative post. Thanks for sharing!

I regard something genuinely

I regard something genuinely interesting about your website so I bookmarked.

Nice script! Keep sharing an

Nice script! Keep sharing an informative post. See more concrete services here https://www.concreteharrisonburg.com/

I have been studying your

I have been studying your entries, and I really found it interesting....

Some person really can't

Some person really can't pinpoint the difference between Java and Dalvik. This post is very helpful on doing that. You can as well visit https://www.roofersnottinghamgrp.co.uk for info on roofing

I just can't believe I found

I just can't believe I found this site. I really like it http://www.surpriseroofer.com

Wow! In the end I got a blog

Wow! In the end I got a blog from where I can genuinely get helpful
facts regarding my study and knowledge.

Helpful article! Another idea

Helpful article! Another idea to improve my business site https://www.toptreeserviceharrisonburg.com/. Thanks for this!

Enlightening article. Thanks

Enlightening article. Thanks for this info. You can also check <a href="https://www.seriezloaded.com"> here </a> for info on some series

Nice post, thanks for this

Nice post, thanks for this info. Keep posting! Click here: https://www.spokaneheatingcooling.com/

Thanks for this post, every

Thanks for this post, every detail is clear and understandable. Keep us updated. See more from https://www.treeservicemviejo.com/

Interesting! sounds great to

Interesting! sounds great to me!

Great post - want to know

Great post - want to know more about this. Hoping that this pandemic would cease. Furthermore, this is nine post.

I agree

Learned a lot from this post.

Learned a lot from this post. Will wait on the next one that you'll post. Great!

Yes, same here!! Can't wait!

Yes, same here!! Can't wait!

Thanks for this, Will check

Thanks for this, Will check it out to prove these differences. Keep on posting this kind of blog.

Great info - actually there's

Great info - actually there's a big difference between these two. Thanks for this.

Thank you for sharing this

Thank you for sharing this amazing post. keep up the great work

I appreciate how you explain

I appreciate how you explain your points in here. Much thanks! https://www.carefulmovers.net/storage/

This is such a great blog.

This is such a great blog. Thank you for sharing your talent with everyone. You are an inspiration.

I thought they are the same

I thought they are the same before. But you already explained it well. Thank you.

Found this helpful tip from

Found this helpful tip from Fort Lauderdale concrete. And as I was browsing, I found your site.

I had fun reading your

I had fun reading your content

Found this helpful tip from

Found this helpful tip from <a href="https://creativeresurfacingsolutions.com/concrete-resurfacing-ft-lauderdale/">Fort Lauderdale concrete</a>. And as I was browsing, I found your site.

They differ in a lot of ways.

They differ in a lot of ways. I still prefer to use Java though.

I agree indeed but some

I agree indeed but some points were even clearer when explained code by code.

Hello there, You've done an

Hello there, You've done an excellent job. I'll definitely digg it and personally suggest to
my friends. I am sure they'll be benefited from this
website. https://www.zazzle.com/mbr/238576877026298076

Thank you for sharing your

Thank you for sharing your knowledge. You are awesome!

This is a good blog,it teach

This is a good blog,it teach me many things.Thank for your share!

You presented and explained

You presented and explained it well. It's a big help for those aspiring programmer. Thanks!

What do you think about Iced

What do you think about Iced Robot? I think the ideas are brilliant!

Your site has a lot of useful

Your site has a lot of useful information for me, I visit it very often. Keep it up!

This is totally awesome! you

This is totally awesome! you save my time. Thanks for sharing.

-Ryan

Ryan thank you for that

Ryan thank you for that comment! You're great man.

Really love the way the

Really love the way the details are presented