# Thursday, 19 December 2002

I set up a SourceForge project. I checked in all the code and created a mailing list. Since I'm a total SourceForge newby, any comments are appreciated.

I expect the mailing list to be very low traffic, so if you're at all interested in following IKVM.NET, please subscribe.

BTW, I dropped the first dot from the name. It's now IKVM.NET.

Thursday, 19 December 2002 09:07:54 (W. Europe Standard Time, UTC+01:00)  #    Comments [2]
# Tuesday, 17 December 2002
Back home and logs

I'm back from Bonaire. Many things to do, and I'm not sure when there will be progress.

While browsing the web server logs, I found an interesting item:
tide72.microsoft.com using Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3215; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 1.2.21120)

Someone inside Microsoft running Whidbey on Longhorn? Not very surprising of course, but the fact the version number is 1.2 puzzles me. It has been generally assumed that Whidbey would be 2.0.

I'm looking forward to beta testing that stuff... On a related note, anybody have any idea when the next PDC will be?

Tuesday, 17 December 2002 12:48:01 (W. Europe Standard Time, UTC+01:00)  #    Comments [5]
# Friday, 13 December 2002
temporarily out of order

The server hosting www.frijters.net seems to be having problems. For the time being the source and binaries can be downloaded here: source, binaries.

UPDATE: server is back up again.

Friday, 13 December 2002 19:37:56 (W. Europe Standard Time, UTC+01:00)  #    Comments [0]

Interesting post by Chris Daly on the advanced-dotnet list today. He questions the C# language spec, which says that the following is illegal:

public class A
protected int x;
public class B: A
static void F(A a, B b) {
a.x = 1; // Error, must access through instance of B
b.x = 1; // Ok

The equivalent Java is legal. When I statically compile the Java equivalent of A and B into two different assemblies, to resulting B.dll is unverifiable. It's not just a C# language issue, the CLR restricts access to protected members in this way. When both types are compiled into the same assembly there is no problem, because protected is compiled as famorassem so any type in the assembly already has access (this is needed because protected also implies package access, which has no CLR equivalent).

I wonder if a work around is required.

UPDATE: I wasn't quite awake yet. Of course, the reason that the above code works in Java is because protected implies package access, it has nothing to do with the protectness of the field. When you move A and B into different packages, javac fails with the same error as the C# compiler.

Friday, 13 December 2002 14:32:21 (W. Europe Standard Time, UTC+01:00)  #    Comments [0]
# Tuesday, 10 December 2002
No Title
I did some debugging last night to try and figure out where the random NullReferenceExceptions come from when running the SWT samples. I couldn't pinpoint anything (adding Console.WriteLines completely changed the behavior :-(), but when I tried running the exe under the 1.1 beta CLR the problem didn't occur, so naturally, now I'm beginning to wonder whether it is actually caused by a CLR bug...
Tuesday, 10 December 2002 16:25:19 (W. Europe Standard Time, UTC+01:00)  #    Comments [3]
# Friday, 06 December 2002

I'm off to Bonaire until the 17th, so there probably won't be any updates before then.

Friday, 06 December 2002 08:34:09 (W. Europe Standard Time, UTC+01:00)  #    Comments [0]
# Tuesday, 03 December 2002

I've been working on getting the SWT (the Eclipse windowing toolkit) examples to run. This mostly involved implementing a lot of JNI methods.

The examples now run, but they do run into the occasional random NullPointerException. Probably caused by a sneaky bug in the (un)managed C++ code.

Here is a statically compiled version ControlExample (including all the supporting DLLs needed).

Note that this will only run on Windows, because my JNI implementation is written in Managed C++ it will not run on Mono.

Some interesting findings:

  • SWT has a reference to the sun.awt.windows.WEmbeddedFrame class. What's that about?
  • In order to get the FileViewer example to run, I had to use the STAThread attribute on my main method. Boy I'm I glad I missed the whole COM thing (was busy doing Java). This just too lame.
  • Reflection.Emit does not support embedding resources in a module (lame!). I had to add resource support to ikvmc (and the VM) to get ControlExample to work, statically compiled, so I added initialized global public fields to the assembly and used System.Runtime.CompilerServices.RuntimeHelpers.InitializeArray to copy the field data to an array.
Tuesday, 03 December 2002 09:32:32 (W. Europe Standard Time, UTC+01:00)  #    Comments [10]
# Saturday, 30 November 2002
Interfaces & the Verifier

To my surprise to following code (Jasmin source) is verifiable and runs without throwing any exceptions (Sun's JRE 1.4.1):

.class public ifmerge3
.super java/lang/Object
.field public static r Ljava/lang/Runnable;
.method public <init>()V
   .limit locals 2
   .limit stack 8
   invokenonvirtual java/lang/Object/<init>()V
.end method
.method public static main([Ljava/lang/String;)V
   .limit stack 5
   .limit locals 4
   ldc ""
   putstatic ifmerge3/r Ljava/lang/Runnable;
.end method

The red lines are the interesting ones. It is apparantly (the vmspec doesn't really talk about this) legal to use any object reference in any place where an interface reference is expected.

This has an interesting implication for the performance of interfaces in Java. It means that whenever an interface method is invoked, the VM will always need to check if the reference does in fact implement the interface in question. This should make interface invocation slower. To test this theory, I wrote a small benchmark:

class ifperf implements Runnable {
  public void
run() {}
  public static void
main(String[] args) {
    Runnable r =
    Runnable[] ar =
i = 0; i < ar.length; i++)
      ar[i] = r;
start = System.currentTimeMillis();
i = 0; i < 1000; i++)
j = 0; j < ar.length; j++)
end = System.currentTimeMillis();
    System.out.println(end - start);

Sun's J2RE 1.4.1: 300 ms
IKVM (on .NET 1.1 beta): 150 ms

John Lam has written about the implementation of .NET's interface method dispatch here. I haven't been able to find any articles that talk about HotSpot's implementation of interface method dispatch. Let's look at the code that is generated by both JITs.

.NET 1.1 beta code (inner loop only):

07591DCC  cmp         edi,dword ptr [esi+4] 
07591DCF  jl          07591DD4
07591DD1  inc         ebx 
07591DD2  jmp         07591D91
07591DD4  cmp         edi,dword ptr [esi+4]
07591DD7  jae         07591DED
07591DD9  mov         ecx,dword ptr [esi+edi*4+0Ch]
07591DDD  mov         eax,dword ptr [ecx]
07591DDF  mov         eax,dword ptr [eax+0Ch]
07591DE2  mov         eax,dword ptr [eax+94h]
07591DE8  call        dword ptr [eax]
07591DEA  inc         edi 
07591DEB  jmp         07591DCC
07591E80  ret              

J2RE 1.4.1 HotSpot code (inner loop only):

00AC7023  mov         esi,dword ptr [ebp-18h] 
00AC7026  mov         edi,dword ptr [ebp-0Ch]
00AC7029  cmp         edi,dword ptr [esi+8]
00AC702C  jae         00AC7146
00AC7032  mov         esi,dword ptr [esi+edi*4+0Ch]
00AC7036  mov         dword ptr [esp],esi
00AC7039  mov         ecx,esi
00AC703B  cmp         eax,dword ptr [ecx]
00AC703D  mov         eax,6B68778h
00AC7042  call        00ABCE25
00AC7047  inc         dword ptr [ebp-0Ch]
00AC704A  mov         esi,dword ptr [ebp-18h]
00AC704D  mov         esi,dword ptr [esi+8]
00AC7050  mov         edi,dword ptr [ebp-0Ch]
00AC7053  es:
00AC7054  cs:
00AC7055  fs:
00AC7056  gs:
00AC7057  nop
00AC7058  cmp         edi,esi
00AC705A  jl          00AC7023
00ABCE25  mov         edx,dword ptr [eax+0Ch] 
00ABCE28  mov         ebx,dword ptr [eax+8]
00ABCE2B  cmp         edx,dword ptr [ecx+4]
00ABCE2E  jne         00ABB6C0
00ABCE34  cmp         dword ptr [ebx+38h],0
00ABCE3B  jne         00ABB8C0
00ABCE41  jmp         00A67940
00A67940  ret              

The first thing to notice, is that HotSpot (in this particular example) is not very good at optimizing register usage, compared to the .NET JIT. .NET uses EDI as the loop counter and ESI as the array reference, whereas HotSpot keeps both the counter and the array reference on the stack. This difference probably accounts for a large part of the performance difference.

The second thing to notice, is that both HotSpot and .NET do not eliminate the array bounds check (again, in this particular example).

The code at 00ABCE25 is where it gets really interesting. After running a while, HotSpot noticed that all calls to Runnable.run() at this site actually resolved to ifperf.run() and so it emitted code that takes advantage of that fact. However, since it cannot prove that this will always be true, it has generated code that checks that the reference does indeed refer to an ifperf object (the cmp instruction at 00ABCE2B does this), and if it doesn't, it jumps to some code at 00ABB6C0 to do the interface method lookup and patch the call at 00AC7042 to point to a new location that does the interface method lookup and does the profiling that started this optimization in the first place. I'm not sure what the second comparison does, but it's something similar. The final branch at 00ABCE41 jumps to the ifperf.run() implementation (bypassing any dynamic dispatch!).

Conclusions: All of this complex optimization (and on-the-fly deoptimization!) makes micro benchmarking fairly useless (as many others have previously demonstrated). As an example, storing just a single non ifperf Runnable in the array slows HotSpot down by about 10%, while in .NET this has no effect. However, interleaving two different Runnable object types in the array slows HotSpot down by about 20% whereas it slows down .NET by about 300% (and I cannot explain that).

Now, the real question is, does IK.VM.NET need to support assigning object references (that do not implement a particular interface) to references of that interface type? The current design (although the implementation isn't quite correct) is to allow the code above to be verified, but when it runs it throws an IncompatibleClassChangeError when the putstatic is executed.

Saturday, 30 November 2002 14:20:15 (W. Europe Standard Time, UTC+01:00)  #    Comments [2]
# Monday, 25 November 2002
More Miranda

The System.TypeLoadException I mentioned here turns out to be caused by a bug in the .NET 1.0 CLR (MS has already fixed it in the current 1.1 beta).

Here is some code that triggers it:

interface __Shape
    public abstract __Rectangle getBounds();
    public abstract __Rectangle2D getBounds2D();
abstract class __RectangularShape implements __Shape
    public __Rectangle getBounds()
return null;
abstract class __Rectangle2D extends __RectangularShape
    public __Rectangle2D getBounds2D()
        return null;
class __Rectangle extends __Rectangle2D implements __Shape
    public __Rectangle getBounds()
        return null;
    public __Rectangle2D getBounds2D()
        return null;

If this code was compiled with Jikes 1.18 (which doesn't emit Miranda methods) and then run in IK.VM.NET, it failed with a System.TypeLoadException. The fix was easy, just emit an abstract method for each interface method that a type doesn't implement. A compiled DLL of the above code can be found here. When run through peverify 1.0, it will fail verification, in 1.1 this is fixed.

I compiled the Classpath code I got on Friday with Jikes 1.18 and made a new release based on that (including, of course, all the VM fixes I had to do).

Updated the binaries and source snaphots.

Monday, 25 November 2002 10:05:05 (W. Europe Standard Time, UTC+01:00)  #    Comments [1]
# Saturday, 23 November 2002

For the time being I switched back to jikes 1.15. When trying to start Eclipse (M2), it failed with a ClassNotFoundException: org.xml.sax.InputSource.

It turns out that in org.eclipse.core.internal.runtime.InternalPlatform the following method causes a problem:

public synchronized static
PluginRegistryModel parsePlugins(URL[] pluginPath,
 Factory factory,
debug) {
// If the platform is not running then simply parse the
 We don't need to play any funny class loader games as
 we assume the XML classes are on the class path
// This happens when we are running this code as part of
// a
 utility (as opposed to starting or inside the
// platform).

if (!(InternalBootLoader.isRunning() ||
  return RegistryLoader.parseRegistry(pluginPath,

// If we are running the platform, we want to conserve
// class loaders.
// Temporarily install the xml class loader as a
// prerequisite
of the platform class loader
// This allows us to find the xml classes. Be sure to
// reset the prerequisites after loading.

new DelegatingURLClassLoader[]
    { xmlClassLoader });

try {
  return RegistryLoader.parseRegistry(pluginPath,
} finally {

The bold line basically adds the xerces plugin to the classpath, this is needed because RegistryLoader depends on it. However, in IK.VM.NET the RegistryLoader class gets compiled when the above parsePlugins method is JITted by the .NET runtime, and at that moment the xerces code is not yet available.

This is an interesting problem, but I'm not sure if this construction is actually guaranteed to work on all VMs by the spec.

Saturday, 23 November 2002 14:19:31 (W. Europe Standard Time, UTC+01:00)  #    Comments [0]