polyglot-compiler / JLang

JLang: Ahead-of-time compilation of Java programs to LLVM
http://polyglot-compiler.github.io/JLang/
Other
284 stars 29 forks source link

All array types share the same class object #7

Open dz333 opened 6 years ago

dz333 commented 6 years ago

From @yizhouzhang on September 14, 2017 19:5

Copied from original issue: gharrma/polyllvm#34

dz333 commented 6 years ago

From @gharrma on March 14, 2018 3:14

This is still an issue, unfortunately.

To fix this, my idea is to (1) Switch to using java.lang.Class object pointers (which were recently added to each dispatch vector) to implement instanceof, and (2) Make sure that a distinct class object gets created for each array type. This could be done by calling java.lang.Class.forName("..."), and including code in that method to maintain reference equality between class objects for the same array type. It might also be possible to use linkonce_odr LLVM global variables.

dz333 commented 6 years ago

From @gharrma on May 24, 2018 17:33

This issue was originally de-prioritized, but it's becoming important again the JDK needs array runtime type information in order to compute things like array element size (see Java_sun_misc_Unsafe_arrayIndexScale) and System#arraycopy(see JVM_ArrayCopy).

Fixing this is not quite as easy as calling Class#forName with an appropriately mangled array type, because we also need to give each array type its own dispatch vector (and this is tricky to do at runtime, given the current way that dispatch vectors are generated). The most promising idea is to emit a dispatch vector for every array type that we see while compiling Java source code (using linkonce_odr linkage to merge identical array dispatch vectors), and then hope that nobody uses reflection to instantiate array types that never existed in the source code (probably a reasonable assumption).

dz333 commented 6 years ago

In the current state, all Array types contain the same class object (polyllvm.runtime.Array); however, Java Reflection requires us to store typed array class information.

E.g the class with the name "[[B" is a 2-D array containing byte primitives.

The current runtime generates such class objects on the fly as needed (e.g. when the jni_FindClass function is called). These are all copies of the polyllvm.runtime.Array class object, but registered with their own names and class info.

Additionally, the polyllvm.runtime.Array class contains a second field "elemLength" which stores the lengths of each individual element (so that part is currently solved).