Lecture 15: Representing Objects

Eelco Visser
Lecture | PDF
December 16, 2021

In the first part of this lecture we study the implementation of objects.

We look at classes in ChocoPy with inheritance of attributes and methods, dynamic dispatch of method calls, object construction, and object initialization using __init__ methods.

Objects are represented in memory in two parts. The encoding of a specific object stores its data attributes, the type of the object and its size. Since all objects of a type share the same methods, those are not store in the object itself, but in a separate dispatch table. The object representation includes a pointer to that dispatch table.

Implementing a class is a matter of creating a prototype object for the class, including a link to a separate dispatch table. Allocating an object is then a matter of copying the prototype. Each becomes a regular top-level function that takes the receiver object as an additional parameter. Accessing the attributes of a class requires its offset with respect to the object pointer.

In the second part of the lecture we make a start with the study of data-flow analysis.