Sorry, you need to enable JavaScript to visit this website.

Feedback

Your feedback is important to keep improving our website and offer you a more reliable experience.

Android* Run Time Compiler Output Navigator Tool

BY 01 Staff (not verified) ON Sep 20, 2019

Introduction

This article describes the Android* Run Time (ART) Compiler Output Navigator, which is an interactive compiler output analyzer tool. It helps Android App developers to quickly view and analyze code generated by any Java* compiler such as Java Bytecode, Dex code, and native assembly code.

This tool provides insights for Android App Developers to see the impact of their changes with ease without bothering with conversion details in the background. The tool also provides detailed information related to generated code at method level, and it is equipped with editor features to make developer ease in writing and analyze the source code.

Background

Optimized Assembly enables CPU to perform to its optimal level. So, it is important from time to time to take a look at the assembly code generated by the compiler for the programs. The knowledge of code to instruction mapping will be helpful while making high-level decisions and optimizations. [Ref 1]

There are various open source compiler interactive tools available to generate and analyze the machine code of languages like C, C++, Rust, Go, Haskell, Swift, and Pascal code, but they are incompatible with the Java language. There is no tool available to view the byte code and optimized dex code in the Android ecosystem. During development, programmers creating Java programs for Android applications would like to see the assembly output for the target platform to understand if there are any further optimizations possible.

Approach

The Android Run Time Compiler Output Navigator tool was developed to solve the above problem. The tool uses a two-step process: the first step converts the Java constructs to Byte code; the second step converts the Byte code to optimized dex code or Assembly code for the target architecture.

The binaries required for processing of Java to Assembly code are already available in the Android OS. The binaries used for this process are dx, dex2oat, and oatdump. Generally developers are interested in viewing the assembly code for their optimization and would like to compare with different Android versions.

The tool performs the steps shown in the figure below to convert and generate the Assembly code for the Java source.

Steps for generating the Assembly code for Java Source

  • The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. It can also process annotations in Java source files and classes.
  • The javap [Ref 3] command disassembles one or more class files. javap prints the package, protected, and public fields and methods, stack size, number of locals and args of the classes passed to it. javap prints its output to stdout.
  • The dx/d8 [Ref 4] command line tool compiles java byte code to DEX byte code. The input bytecode can be in any combination of *.class files or containers, such as JAR, APK, or ZIP files.
  • The dexdump is a disassembler tool to create Dalvik Virtual Machine byte code from a dex file.
  • The dex2oat takes a dex file and compiles it. The result is essentially an elf file that is then executed natively. Instead of having bytecode that is interpreted by a virtual machine, it now has native code that can be executed natively by the processor. This is called AOT (ahead-of-time) compilation.
  • The oatdump is a disassembler tool that provides a disassemble function for Dalvik bytecode as well as machine code from odex file.

Let’s consider a Java method (reductionByte) which takes a byte array as the argument and returns the sum of the values in that byte array and understand how it converts to java byte code/ dalvik VM byte code/ assembly code.

1)     Java Program:

2)     Java Byte Code generated for reductionByte method:

3)     Dalvik VM byte code (dex) for reductionByte method:

4)     Assembly/Machine code for reductionByte method:

Tool Features

Using this tool, we can select different Android versions and easily analyze Dalvik VM byte code or assembly code generated for a particular Java program. The figure below shows the tool GUI and highlights its key features.

 

ART Compiler Output Navigator

1)     Highlight Java keywords:

2)     Expand/fold the code:

3)     Match Braces/Tags:

 

Tool location

Access the art compiler navigator tool at this link:  http://10.66.254.47/

Summary

This article describes an interactive tool that can generate assembly code quickly. It helps to analyze assembly code and identify if there are opportunities for optimizations. This also helps developers specifically to check if their changes are reflected in assembly code by selecting a different tool chain corresponding to their changes.

ABOUT THE AUTHORS

This article was written by Jaishankar Rajendran, Biboshan Banerjee, BC, Anuvarshini, and Yasoda Aravapalli who are members of Google OS Run Times Team at Intel Technology India Pvt. Ltd.

Contact us via email to ask questions or discuss issues: jaishankar.rajendran@intel.combiboshan.banerjee@intel.comanuvarshini.bc@intel.com, or yasoda.aravapalli@intel.com

References

[1]   Compiler Explorer Documentation [Online], Available: https://blog.jetbrains.com/clion/2017/10/godbolting-your-cpp-code/

[2]   Javascript tutorial W3school [Online], Available: https://www.w3schools.com/js/default.asp

[3]   javap - The Java Class File [Online], Available: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html

[4]   d8 [Online], Available: https://developer.android.com/studio/command-line/d8

[5]   Develop Android apps with Kotlin [Online], Available: https://developer.android.com/kotlin