For CIL / MSIL, I can write the code in a text editor and compile / decompile with ilasm / ildasm.
I can use Reflector to see the CIL generated by a .NET class.
In the Java world, javap -c shows the disassembled byte code.
How do I compile Java bytecode? (i.e. the Java equivalent of ilasm / ildasm).
Is there an IDE that supports Java bytecode?
Does the IDE support debugging i.e. single stepping / breakpoints etc.?
Bytecode Outline plugin for Eclipse
to play with bytecode you can use ASM or BCEL
Take a look at org.apache.bcel.util.BCELifier, it takes a given class and converts it to a BCEL program (in Java, of course). It will show you how certain code is generated using BCEL.
Jasmin:
http://jasmin.sourceforge.net/
It's an assembler for Java bytecode. While an above poster pointed out that hand-coding Java bytecode may not be very useful, Jasmin has been used as a backend for compilers targeting the JVM as a runtime. Thus, your compiler can output Jasmin assembler instructions and then Jasmin converts them into Java classes.
Related
The wikipedia page on compiler bootstrapping lists python and java among the languages whose compilers are bootstrapped. Aren't javac and cpython implemented in c?
Why are these languages listed on that page?
javac is written in Java, and compiles Java source to bytecode. The source is available online. So javac was bootstrapped. You may be confusing javac with the JVM as the latter is written in C and is used to compile bytecode to machine code and run it.
Same thing for Python. The compiler is not the same thing as the execution environment.
With Java, there is the distinction between the Java compiler (javac) and the Java Virtual Machine (java). Unless an operating system and/or CPU would have support to run Java applications natively, you will need a JVM to run any Java application.
On the other hand, tools such as Apache Maven are written in Java, and invoke the Java compiler as well. This is not done by invoking a Java package. The page on the Maven Compiler Plugin describes how this is done, and how you can configure the plugin to use the javac executable instead.
There will always be the need for some native code. But nothing is stopping you from writing all of the code that produces Java bytecode in Java itself. After all, what it does is convert text into bytes. You can do that in any programming language, including Java.
First of all, this is not your standard "I want to compile Java code to machine code" question.
I'm working on a compiler written in Java, that will translate a certain language (in my case: Brainfuck) to x86 Assembly, after that I'm currently planning to use NASM and GCC to produce machine code.
Seeing as the HotSpot JVM can compile Java bytecode to machine code, I assume there is some mechanism available to compile source code of type A to machine code.
Is there any way to use this in a compiler written in Java? My main goal is to explore the possibility of writing a compiler in Java without relying on external programs, for example GCC and NASM, being available on the path. I do need a C Compiler because I'm linking with the cstdlib as I'm using those functions in my x86 Assembly code.
To clarify, I'm doing the following currently:
Write x86 Assembly to a bf.asm file.
Transform Assembly to Object code with nasm -f win32 bf.asm.
Link the Object code with Windows OS and cstdlib libraries with gcc -o bf bf.obj.
I'm searching for the possibilities of replacing the need of using nasm and gcc in steps 2 and 3 and instead do those with Java code.
Seeing as the HotSpot JVM can compile Java bytecode to machine code, I assume there is some mechanism available to compile source code of type A to machine code.
This does not follow.
The JIT compiler compiles Java bytecodes to native code. It does not understand anything other than Java bytescodes. And bytecodes are not "source code". (They are actually a form of machine code ... for an abstract computer ... a Java virtual machine.)
In short, there is no mechanism available as part of the JVM for compiling source code to machine code.
And, as it turns out, the JIT compiler is not designed for generating native code in files that something else could use. The native code is in the form of raw machine instructions in blocks of memory. No symbol tables. No relocation information. Probably full of hard-wired calls into other parts of the JVM. Basically it is designed for execution in the currently running JVM, not for anything else.
Is there any way to use this in a compiler written in Java?
The JIT compiler is not applicable to your problem ... unless you write your compiler to generate valid Java bytecodes. And if you did that, then the JVM could run your code, and the JIT compiler would at some point compile your bytecodes to native code.
Bottom line: if your goal is to generate native code that can be run as or linked to a separate executable,
the JIT compiler is of no use to you, but
you could use the JVM including the JIT compiler as your execution platform, by generating bytecodes, and
you could use also ordinary Java programming to implement your compiler or assembler, including a component that generates and emits native code in a format that is appropriate to your needs.
Is it possible to compile to machine code in Java without an external program?
Yes. Write an x86 assembler in Java.
If you're generating x86 assembly, the next step is obviously to assemble it.
Seeing as the HotSpot JVM can compile Java bytecode to machine code, I assume there is some mechanism available to compile source code of type A to machine code.
Just because HotSpot can convert Java byte code to x86 machine code, doesn't mean it can convert any other input to the same.
You're essentially asking if one can use a Java JITter to assemble x86 asm. It makes no sense.
I do need a C Compiler because I'm linking with the cstdlib
No, you need a linker. Nothing about linking necessitates a compiler.
I'm trying to compile some interdependent java and scala code at runtime in scala. I thought scala runtime compiler(reflection API) would compile Java as well just like scala's compiletime compiler, but it turns out it does not recognize Java's syntax.
Then I tried compiling java code with Java reflection and add them to the class path, then compile the scala code that depends on the java code compiled earlier.
Is there a way to do this more robustly for multiple files mixed with both Java and Scala? Java code might depend on the Scala code and some scala code might depend on some Java code as well...
As I know, java source code is compiled into class files by the java compiler (javac.exe); then these class files are put into JVM to interpret using java.exe
But Eclipse is only using javaw.exe. So, I think javaw.exe is equivalent to javac + java
But some references say javaw.exe nearly equivalent java.exe. thus Eclipse only has the interpreter progress. Compiler process takes place where, how?
How should I understand this?
Eclipse does not compile files using javac. It has its own, independent implementation of a Java compiler, complete with its own set of error messages—and bugs.
Eclipse still needs access to all the standard library classes against which it compiles Java code.
The Java runtime which runs the Eclipse IDE process is not related to the JDK used to compile the code against. For all that matters, Eclipse could be a native application written in C++ and that wouldn't stop it from being able to compile Java.
I am new to java and I need to manipulate java bytecode for some purposes (see this). Java bytecode manipulation need following imports:
org.objectweb.asm
java.lang.instrument
I resolved org.objectweb.asm by downloading ASM package from asm website and related imports have been resolved.
I don't know how to resolve java.lang.instrument, My default ADT bundle hasn't it:
How do I resolve this import? Should I download any library? from where?
any help would be appreciated.
Thanks.
The java.lang.instrument package isn't available for Android. Just like AWT and Swing. Have a look at this question:
Android & Dalvik - Get the size of an object
But it makes sense. Android apps are written at the source level in Java, but they don't run on the JVM -- they run on the dalvik VM. There's no contract there that says they have to support the standard Java library.
Sorry :(
You're out of luck – find a way that does not rely on java.lang.instrument. java.lang.instrument is part of Java SE, but is not available on Android because of fundamental limitations of Dalvik.
The java.lang.instrument package was removed from dalvik core library, because this package makes a fundamental assumption that the execution format being used by the VM is a .class file. .class files do not appear on Android at all.
https://groups.google.com/forum/#!topic/android-developers/MR4W2roQ3Xw
Javassist is another tools to manipulate java bytecode. There is already someone who tried to use javassist in android. You might want to try it. As far as I know, bytecode manipulation on android runtime isn't possible, except in instrumentation (usually for testing). Manipulation on compile time is a different story, because java .class file generated first before converted to Dalvik bytecode. So if you modify .class file before being dexed, the dexed classes will be the modified one.
This article also worth reading, because it noted of ASMDEX which claim can manipulate DEX bytecode.
The java.lang.instrument package was removed. So you can't perform bytecode manipulation at runtime. However you can perform bytecode manipulation at build time with javaassist or ASM.
This sample project performs bytecode manipulation. It's usage is discussed here.