First, we want to implement the operations
NEW and
GET
defined like this:
NEW classdesc
GET fieldname
NEW instruction takes a
JSObject stored in the
dictionary as parameter that describes the fields and creates a new object
in the heap.
The JSObject acts as a hashtable that stores for each field the offset of
the field. By example,
{
x: 2,
y: 4
}
should be translated to
CONST 2
CONST 4
NEW 'index'
STORE local_index_of_point
with 'index' corresponding to the index in the dictionary of the JSObject
{ x: 0, y: 1 }.
In the heap, 4 integers should be reserved, the first one is the JSObject
that describes the field of the object, the second one is for the GC and
will be used in exercise 3. The fields 3 and 4 correspond to the field "x"
and the field "y".
Note: the size of an object is the size of the header (2 fields in our
implementation) + the length of the JSObject that describes the fields.
The instruction
GET takes a field name as parameter, pops an
object from the stack and push the value of the field of this object on
the stack