Bootstrapping java and python compilers - java

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.

Related

How to compile Java to WASM (WebAssembly)?

I wonder can I use Java and compile it to WASM (WebAssembly)?
The list https://webassembly.org/getting-started/developers-guide/ has no Java, Kotlin
GraalVM WASM project https://www.graalvm.org/reference-manual/wasm/ is for running wasm inside JVM, not for running Java projects within WebAssembly.
Here are a few compilers that can do this for you:
https://github.com/konsoletyper/teavm (most popular & my own reccomendation: https://teavm.org/)
https://github.com/i-net-software/JWebAssembly (only to webassembly, the others can do webassembly and javascript)
https://github.com/mirkosertic/Bytecoder
https://github.com/leaningtech/cheerpj-meta
Do note, that all of them have their limitations, most commonly that every Java class in Java's standard library won't work well with it (for example, TeaVM and others have problems with reflection).
They also require pipeline integration -- make sure to be using a build tool these compilers support, such as gradle or maven

Is it possible to compile to machine code in Java without an external program?

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.

Why Eclipse doesn't have path to java compiler?

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.

What is the difference between using javac and javax.tools.JavaCompiler?

Maven Compiler Plugin documentation states:
The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse
And indeed when forceJavacCompilerUse is not specified in our build there are some build errors, for example when the code references the com.sun. packages (legacy, we know that its a bad idea...)
What are other differences between these two compile modes in general and with maven? Are there any output differences that one should know?
javac (as "java compiler") is an executable, which could be theoretically even a platform-dependent executable or a script. This is called to compile a .java to a .class.
On windows is its name javac.exe, and it is normally somewhere below C:\Program Files*\jdk*\bin.
This compiler was developed in java as well. That means, if we start this .exe, a new java virtual machine need to be started to run it. This is slow.
But, because it was written in Java, there is a much faster alternative to it: from our already running jvm, we simply import its main class (f.e. javax.tools.JavaCompiler or such) and call this. This doesn't need to start an unneeded jvm. That is what maven does. Simply 10 yrs was them enough to make this correctly. :-)
Of course it has some fallbacks as well. To most probable cause is that in the case of the internal compiler it needs to run from the same jvm and in the same namespace as the maven core. Also specifying an alternate jvm is impossible, and there could be some side effects as well resulting from the namespace collisions. But they are very improbable, because both of them is well-designed software.

Java bytecode equivalents for ilasm / ildasm

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.

Categories

Resources