*** X language mapping

** Basic considerations

** Three examples

* Hello World

* Basic arithmetic and control

* Objects and methods

** Discussion of Issues

** Conclusion on required VM changes

*** Java language mapping

** Basic considerations

A pre-vm service corresponds to a complete Java application.  Communication
between Java applications is typically done using RMI, which could be the case
for inter-service communication as well.

A pre-vm component corresponds to a jar archive.  A Java jar file can contain a
number of classes and includes meta-information describing these classes, and
can also include other resources such as binary files.

A pre-vm class corresponds to a Java class, similarly for methods and fields.

** Three examples

* 1a: Hello World with printing as primitive

Printing to standard out can be done by mapping "System.out.println(...)" to an
invocation of a primitive, which is likely the simplest possible program.

--Java source-------------------------------
public class HelloWorld {
  public static void main(String argv[]) {
    System.out.println("Hello, world!");
  }
}
--------------------------------------------

This sourcecode is compiled into the following preVM bytecodes; an open
question is whether to support the "void" return type in the virtual machine
instruction set, or just require the compiler to always e.g. generate an
implicit push of the self object before the return statement.

--preVM bytecode----------------------------------------
...
--------------------------------------------------

This example can be compiled straightforwardly into the existing bytecode set.
We currently assume that the RETURN_LOCAL bytecode inspects the signature of
the method to determine that it does not return any values.  See discussion at
the end of this section.

* 1b: Hello world with printing as library functionality

Realistically, we would want printing to be a library functionality.  This
raises the question of what to do with the massive amount of Java libraries
available.  Our idea is to place Java library functionality into a component,
which can be done on a by-need basis.

--Java source----------------------------------------------
public class HelloWorld {
  public static void main(String argv[]) {
    System.out.println("Hello, world!");
  }
}
-----------------------------------------------------------------

Performing "System.out.println(...)" is now a question of first looking up the
class "java.lang.System" in the component "JavaLibrary", then accessing the
field "out", and last sending the message "println" to this object.

--preVM bytecode--------------------------------------------------
...
------------------------------------------------------------

We added a special bytecode for accessing an import (to avoid using the
problematic globals).  Alternatively, the standard PUSH_GLOBAL bytecode could
have been used, or method invocations on the class could perhaps have been used
- see discussion at the end of this section.

We have added a bytecode for accessing a field; doing so is perhaps problematic
- see discussion at the end of this section.  In Java methods can be overloaded,
so using just the name of the method is not enough to disambiguate it (println
is a perfect example from the Java API).  For this reason, we consider the
signature of the method to be part of its name.  For example, to invoke the
println method we send the message "println%void(java.lang.String)" to the
printer object.  Since all types relevant to the signatures and overloading are
statically resolved by the compiler anyway, this does not raise any
complications.

* 2: Basic arithmetic and control

Arithmetic raises the issue of whether there are primitive values.  For the
moment, we see no advantage in having primitive values (they are more likely to
complicate the virtual machine, and we do not know at the moment if they will
give us any advantages).  So we represent all values using the existing object
structure from Smalltalk.

Control raises the issue of whether to use blocks, like in Smalltalk, or
directly represent the control using branching instructions.  Since Smalltalk
implementations often compile down to branching instructions anyway, and adding
them does not complicate the virtual machine in any way, we choose to add
branching to the VM.

--Java source--------------------------------------------------
...
------------------------------------------------------------

An issue which we choose to ignore for the moment is that of overflow semantics.
Java has a well-defined overflow semantics which should maybe be preserved in
our language mapping (some programs may rely on it, e.g., when doing
bit-operations).  Smalltalk on the other hand transparently replaces the integer
object with a BigInteger object.  How to support both conventions on the same
VM without ending up with 27-bit integers (assuming 5 bits worth of tags for
different language conventions)?

Branching instructions should operate on boolean values, which in Smalltalk is
the true and false singleton objects (which are known by the system anyway).

--preVM bytecode--------------------------------------------------
...
------------------------------------------------------------

We have thus added standard conditional and unconditional branching
instructions, which test if the element at the top of the stack is equal to the
appropriate singleton boolean object.

* Objects and methods

Earlier examples have already treated accessing fields and invoking methods.
Manipulating objects does however raise the issue of constructors.  These are
easily represented as class methods in the standard Smalltalk way.  To make the
example more interesting, we also include a static field and a static method.

--Java source--------------------------------------------------
public class Point {
  int x,y;
  static int add_to_x;

  public Point(int x, int y) {
    this.x=x; this.y=y;
  }

  public int dist_sq() {
    int tmp = this.x+add_to_x;
    return tmp*tmp + y*y;
  }

  public void set_add_to_x(int ax) {
    add_to_x = ax;
  }
}
------------------------------------------------------------

We assume that fields and methods can be declared in a special static section
to make them be class methods (having a per-method/field modifier would of
course be equivalent).

--preVM bytecode------------------------------------------------
...
------------------------------------------------------------

Thus, no additional special bytecodes were needed for this example.

** Discussion of issues

The above examples have raised a number of issues, as did the general
discussion which we had on Java.

* Field access bytecode vs. auto-generated accessor methods

A major issue is how to access fields.  We used bytecodes for directly
accessing fields, but one can argue that this is problematic for an untyped
language like Smalltalk since we may very well be exposing much more
information than we intended to (public/private is non-trivial in such a
language, and fields cannot be accessed efficiently anyway without type
information).

Thus, we are left with two options, one is requiring the class as an argument
to the bytecode (a kind of "I know what I'm doing") or using accessor methods
(e.g., treating all fields like properties).  Ignoring efficiency issues (or
trusting the compiler/virtual machine to do optimizations), the property
approach is probably cleaner and fits better with an untyped language.

* "RETURN_LOCAL 0" for void method

In the examples, we required the RETURN_LOCAL bytecode to either return a value
or perform a void return depending on the method signature.  Alternatively, we
could require the compiler to generate an implicit "push_arg 0" to push the
self object on the stack before returning from a void method.

If other languages require multiple return values, with zero as a special case,
this could also be used to implement void returns, e.g.:

  RETURN_LOCAL 0

to return zero values to the caller.

* Resolving import

In the examples, we resolved imports using a special bytecode.  Alternatively,
we could use primitives defined at the class level to get hold of the component
object, for example (using direct field access):

  ((self class) component).x

Alternatively, using accessor methods

  ((self class) component) get_x

* Resolving global

Similarly to the imports, globals can also be resolved using method calls:

((self class) component).x

* Representing interfaces

Interfaces should be represented as method-less (?) classes.  Every class
(including the ones that represent interfaces) should have an array of super
interfaces, which for example is used to implement the instanceof operator.
 
** Conclusion on required VM changes

* Bytecode instructions

Standard bytecodes for branching
Multiple return values useful but not required

* Primitives

Access to component from class

* Other

Arbitrary method signatures (so that we can stuff Java type names in there)

* Open questions

Properties vs. direct field access (at the time I'm writing this, properties
seem to be underway, which is fine for us).
