# Thursday, 19 June 2003
One Year Ago Today

It's been exactly one year since I started blogging about the development of IKVM.NET. I actually started development about a month earlier on the 22nd of May.

To celebrate this, I've created a time-line of significant (or interesting) events that happened in the past year.

2002-05-22

Started development on a project called "bytecode". I started by porting my Java class file reader from Java to C#.

2002-06-19

Started my IK<<VM.NET Radio weblog about "The Development of a Java VM for .NET"

2002-06-27

"Hello, World!" runs for the very first time. Replaced the << in the name with a dot, because Radio doesn't escape the title properly.

2002-08-07

First version of netexp is released. Java code can now directly use .NET types.

2002-08-12

The static compiles makes its appearance. For the first time the binary snapshot contains a statically compiled classpath.dll.

2002-09-03

Very basic AWT support is introduced.

2002-09-09

Class loader support makes its first appearance.

2002-10-25

James runs!

2002-11-01

Zoltan Varga starts to work on getting IKVM to run on Mono.

2002-12-19

Created the IKVM.NET SourceForge project. Dropped the first dot from the name.

2002-12-28

Eclipse starts up!

2003-01-17

Zoltan Varga gets IKVM to run HelloWorld on Mono.

2003-03-13

Moved blog from Radio to BlogX.

2003-04-05

Added support for using value types from Java.

2003-05-10

Zoltan Varga gets IKVM to run Eclipse on Mono.

2003-06-19

The IKVM.NET blog celebrates its first birthday.

Thanks to everyone who contributed to IKVM.NET in the past year. It's been great fun and I hope the coming year will be as productive as this past year has been.

Thursday, 19 June 2003 11:22:49 (W. Europe Daylight Time, UTC+02:00)  #    Comments [4]
# Monday, 02 June 2003
Invokespecial

One of the more interesting bytecode instructions is invokespecial. During Java's early days this instruction was called invokenonvirtual, but in JDK 1.0.2 it was renamed to invokespecial to indicate it has some very special semantics.

Invokespecial is used in three ways, to call instance initializers (constructors),  to call base class methods non-virtually and to call private methods. It's worth mentioning that, unlike the CLR call instruction, invokespecial cannot be used to call arbitrary methods, it can only call methods in the current class or in a base class of of the current class (and then only on references of the type of the current class, or subclasses of it). The JVM's invokevirtual is very similar to the CLR's callvirt instruction. In addition to invokespecial and invokevirtual, the JVM also has invokestatic and invokeinterface to invoke static methods and interface methods, respectively. The CLR has no special instructions for that, it uses the call and callvirt instructions to call static and interface methods.

Prior to JDK 1.1, invokespecial called the exact method specified in the instruction. In JDK 1.1 this behavior was changed, because it caused versioning problems. Here is an example of what could go wrong:

Component A - version 1

public class GrandParent {
  protected void myMethod() {
    // ...
  }
}
public class Parent extends GrandParent {
}

Component B

public class Child extends Parent {
  protected void myMethod() {
    // ...
    super.myMethod();
  }
}

The compiler would compile the super.myMethod() call in Child to invokespecial GrandParent.myMethod(). Now suppose a new version of Component A is released:

Component A - version 2

public class GrandParent {
  protected void myMethod() {
    // ...
  }
}
public class Parent extends GrandParent {
  protected void myMethod() {
    // ...
    super.myMethod();
  }
}

When Component B is used (without recompiling) with this new version of Component A, the super.myMethod call in Child will still go directly to GrandParent.myMethod and this is probably not what the author of Component A had intended.

To fix this, invokespecial was changed to search the class hierarchy if the called class is a base class of the caller (from the caller's base class on up), but only if the caller's class has the ACC_SUPER bit set in the class' access_flags mask. All Java compilers since JDK 1.1 always set the ACC_SUPER flag. It's interesting to note that the current Sun JRE 1.4.1 still honors a cleared ACC_SUPER flag.

Why doesn't the CLR have an equivalent of the ACC_SUPER flag? The reason is that it isn't needed. When, for example, the C# compiler compiles a base method call, it emits a call instruction to the immediate base class of the caller, even if that class isn't the one that implements the called method[1]. When the JIT is resolving the call it searches up the class hierarchy to find the actual method.

Comparison JVM and CLR call instructions

JVM

CLR

Notes

invokespecial

call

invokespecial checks the object reference for null, call doesn't.

invokevirtual

callvirt

invokeinterface

callvirt

Like in other places where the JVM consumes interface references, the object reference doesn't have to statically implement the called interface type.

invokestatic

call

One final issue relevant to IKVM here is that invokespecial requires the object reference to be checked for null, the CLR call instruction doesn't do this (for this reason the C# compiler uses callvirt when calling non-virtual methods, except when explicitly calling a base class method of course, it always makes sure the object reference isn't null). The IKVM compiler has to insert an explicit check for null references when it is compiling invokespecial. It took me a while to come up with an efficient way of doing this. Javac faces a similar issue when it compiles the explicit instantation of an inner class:

public class Foo {
  public class Inner {}
  static void method() {
    // next line throws a NullPointerException
    ((Foo)null).new Inner();
  }
}

Why does this throw a NullPointerException? The answer may be surprising, but it is because Javac inserts a call to ((Foo)null).getClass() to make it throw a NullPointerException if the outer this is null. If it didn't do this, you'd run into a NullPointerException later on when one of Inner's methods tried to use the outer this and this would be a very surprising and hard to find bug. However, I didn't want to use this trick because it isn't particularly efficient. What I came up with is the following trick:

ldvirtftn instance string System.Object::ToString()
pop

The JIT compiles this to:

mov   eax,dword ptr [ecx]
mov   eax,dword ptr [eax+28h]

If the reference in question is null, the first instruction causes an x86 trap that the CLR translates into a NullReferenceException. The second instruction is overhead and hopefully a future version of the JIT will stop emitting it, but it isn't very expensive anyway.

Note to self: Consider adding an optimization to the IKVM compiler to detect Javac's null reference check:

invokevirtual java/lang/Object.getClass()
pop

and replace it with the more efficient ldvirtftn based check above. This could actually be an important optimization, because getClass() on IKVM is pretty expensive.

[1] When the base classes are in the same module, the C# actually emits a call to the class the defines the method, instead of to the direct base class. This is an optimization, because it saves the JIT from having to search for the method in the class hierarchy. When all classes are in the same module, there obviously aren't any versioning issues, since the module is always built as one unit.

Monday, 02 June 2003 17:56:11 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
# Friday, 30 May 2003
Update

After a long hiatus, finally another update. Many changes, mostly clean up. I moved some of the custom attributes to a new assembly OpenSystem.Java.dll. Hopefully this will be useful for the dotGNU Java compiler.

I made new source and binaries snapshots available, these are based on the current GNU Classpath cvs code + one patch.

Friday, 30 May 2003 14:17:50 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
Redmond
Like Ingo, I'll be in Redmond on the 25th and 26th of June. Anyone else?
Friday, 30 May 2003 08:51:15 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
# Saturday, 10 May 2003
Eclipse on Mono

Zoltan Varga just posted the following to the ikvm-developers list:

Hi,

I got Eclipse running under IKVM under Mono. A screenshot, Makefiles etc. can be found here:

http://www.nexus.hu/vargaz/

The port uses a JNI provider written in C which works with mono. Eclipse startup up+shuts down in about 1 minute on an 1Ghz PC, while consuming about 90MB of memory. It works with Mono 0.24 and current mono CVS.

I made some modifications to IKVM which are in the attached patch.

Could they be applied to the official version?

The modifications are:

- ikvm.build: fix case of directory names

- ClassLoaderWrapper.cs: Create Assemblies with Run flag, so the

runtime can apply some memory saving optimizations.

- TypeWrapper.cs: Cache field lookups

- classpath.cs: Make shared library loading work under UNIX

There is one other issue: Mono does not yet support the

GetLoadedModules() method, so this has to be commmented out in classpath.cs and in Handler.java.

BTW: The IBM RVM project includes a nice JNI testsuite:

http://www-124.ibm.com/developerworks/oss/jikesrvm/

The Mauve test status of mono:

191 of 7294 tests failed

Would it be possible to post the Mauve test results of IKVM somewhere (with -verbose) so I can compare it to the Mono version?

have fun

Zoltan

Wow! This is great news.

Saturday, 10 May 2003 13:33:40 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
# Friday, 09 May 2003
Class.forName()

Stuart asks:

Can IKVMC handle Class.forName if the target class is in an assembly that isn't directly referenced by the assembly making the call?

Yes, but then you have to specify the assembly qualified name of the class. For example:

Class.forName("System.Windows.Forms.Form, " +
    "System.Windows.Forms, " +
    "Version=1.0.3300.0, " +
    "Culture=neutral, " +
    "PublicKeyToken=b77a5c561934e089");

Another alternative, when the class hasn't been compiled to an assembly, is to instantiate a class loader and use that to load the class:

URL[] classpath = new URL[1];
classpath[0] = new URL("...");
ClassLoader loader = new URLClassLoader(classpath);
Class clazz = loader.loadClass("...");

Currently, when ikvm.exe isn't used to start your application you don't have an application class loader that loads classes from the directories specified in the CLASSPATH environment variable.

Friday, 09 May 2003 11:16:56 (W. Europe Daylight Time, UTC+02:00)  #    Comments [2]
# Monday, 28 April 2003
DotGNU & New Snapshot

Saturday I joined the DotGNU IRC meeting to discuss their Java support. We decided to make sure that our efforts will be interoperable.
They are working on a Java source to IL compiler and this complements IKVM's byte code to IL compiler nicely. We'll be working together to create a standard format for remapping Java classes to .NET classes and define custom attributes to express Java semantics that don't map directly onto the .NET standard attributes.

I made new source and binaries snapshots available. Not many changes:

  • invokespecial byte code now checks for null references before calling the method. I intend to write an item on invokespecial in the future, because it is quite an interesting instruction with a bit of a history.
  • Improved method overriding handling for methods that are less accessible than the method they override. Java allows this, but .NET doesn't. It is handled by giving the overriding method the same (or better) accessibility as the overriden method.
    IMHO this is a design flaw in the CLR, because it makes it a breaking change to widen the access of a virtual method in a new version of a type.
  • Improved ikvmc's handling of -recurse and added wildcard support to file arguments.
Monday, 28 April 2003 10:54:39 (W. Europe Daylight Time, UTC+02:00)  #    Comments [2]
# Friday, 25 April 2003
Feedback
Stuart posted a nice comment:

Well, I now am a legitimate happy IKVM user.

I just found myself in a situation where I wanted to be able to call nrdo code (nrdo is a Java tool for database access, http://nrdo.sab39.org/) from a web application, that also had to read its input files from a windows machine. The individual parts of that aren't a problem: nrdo is reasonably well librarified so it's perfectly possible to call it from a web application, and it's perfectly possible to run Java code on windows (indeed, that's how our normal development is done).

But we don't have any windows machines running any Java application servers, and spawning a Java process from inside ASP.NET didn't appeal very much :)

It's funny how I've been following IKVM for a while but it still took ages before the obvious solution hit me. I compiled a nrdo.dll (okay, this took me longer than I thought because the -recurse option to ikvmc didn't do what I thought it would and neither did path\*.class), created an ASP.NET web application in Visual Studio, added references to classpath.dll, nrdo.dll and ik.vm.net.dll, and started typing.

It was a seriously schizophrenic experience (but in a good way!). I don't normally use an IDE for Java coding so it was the first time I'd ever seen intellisense for my nrdo libraries. And you can get seriously confused as to which language you're using when you write C# code that looks like this:

using java.util;

for (Iterator i = myList.iterator(); i.hasNext(); ) {
MyThing foo = (MyThing) i.next();
foo.something();
}

But it worked, and it took me about an hour to solve what I'd expected to be a pretty complex problem. The schizophrenic aspect is just an artefact of IKVM being really, *really* good at its job!

Thanks! Patches to make ikvmc more intuitive are always welcome of course ;-)

Friday, 25 April 2003 08:53:56 (W. Europe Daylight Time, UTC+02:00)  #    Comments [3]
# Monday, 14 April 2003
Snapshot

I checked in my value type changes, along with a bunch of other changes and made new snapshots.

Here are the highlights:

  • (limited) support for using value types
  • support for calling methods with ByRef arguments
  • better support in map.xml for redirecting to Java classes
  • moved parts of StringHelper & ObjectHelper from C# to Java
  • updated to be compatible with current Classpath cvs

Source and binaries snapshots are in the usual locations.

Monday, 14 April 2003 12:32:48 (W. Europe Daylight Time, UTC+02:00)  #    Comments [1]
# Friday, 11 April 2003
More value type feedback

Stuart Ballard commented on yesterday's entry:

I definitely think these changes should be included.

As I understand it, without them it is impossible to use C# structs, enums and other value types in Java code at all, and also impossible to call methods which require ref or out parameters.

This is correct (except that enums were already usable from Java, they are visible as classes containing public static final int fields and arguments are just int arguments).

To me this scenario is very disappointing - the dream of IKVM, for me, was to see Java as a first-class .NET language while still remaining "true java". Eliminating support for value types and reference arguments clashes with the first of these goals; requiring a hacked compiler violates the second.

I'd even go so far as to ask whether it would be possible to actually *write* a "struct" in java, along the lines of the following:

public class JavaStruct extends system.ValueType {
int structMember;
}

Presumably it would be possible for ikvm to detect classes that inherit from ValueType and compile them as structs rather than classes...

I briefly considered it, but dismissed it at the time because I didn't need it. You've convinced me to take another look at this.

To summarize, not only do I think this is cool functionality (even if the implementation *is* a little bit of a hack) I think it's extremely important functionality and I'd like to see further fine-tuning of the "hack" and further enhancements to the functionality, rather than leaving it out entirely.

The reason I didn't comment earlier, though, is that I don't really have any right to comment, as I don't actually use IKVM at all (although I may in the future to save my co-developers from downloading the JRE to run a single java program). I'm only commenting now to provide at least some positive feedback on the idea of checking it in, so that you don't throw it out on the basis that nobody thinks it's valuable (although of course I fully respect your right to throw it out for any other reason).

Thanks for commenting, I really appreciate it! I will go ahead and polish up the changes and check them in.

Friday, 11 April 2003 13:16:11 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]