Monday, January 24, 2011

Tart status update

I got a lot of Tart-related work done over the weekend, and am very close to having a simple garbage collector working.

One new feature is the ability to create objects using a custom allocator. The garbage collector needs this, because it requires some objects to be created before it has finished initializing. These objects are allocated in "permanent memory" outside of the region of memory that the collector manages.

Since Tart doesn't have a 'new' keyword, we can't use the C++ syntax "new(allocator) Type(args...)". Instead, the type's Type object has a method, 'create', which takes an allocator as an optional argument. There are currently two allocators, the "default" allocator, which just calls GC_alloc(), and the "malloc" allocator which calls malloc(). Allocating an object via malloc() looks like this:

let obj = typecast[MyClass](CompositeType.of(MyClass).create(MallocAllocator.INSTANCE));

A bit wordy, but that's fine, because this should only be used rarely. It's also slower than the normal object creation pattern, but again that should be fine.

One question that I've been mulling over is Tart's support of pointers. In the ideal case, a user of Tart should never have to deal with pointers directly, everything should be done in terms of object references (which are essentially pointers underneath, but have a lot of special semantics known to the compiler.) However, because a lot of Tart's runtime is written in Tart, there is a large body of Tart code that does in fact use pointers directly.

Because I don't want to encourage people to use pointers, the syntax for declaring and using a pointer is deliberately somewhat verbose:

import tart.core.Memory.Address;

var p:Address[ubyte];

Similarly, there's no special syntax for dereferencing a pointer - instead you say something like "Memory.deref(ptr) = value" or "ptr[0] = value".

However, because I've been working with pointers so much recently, I've slowly been giving way and adding operator support for pointers - so now you can say "ptr += 3" instead of "p = Memory.addressOf(p[3])".

Yesterday I added comparison operators for pointers - <, <=, >, >=, and so forth. Unfortunately, this confused the type solver, and I'm still tracking that one down. Aside from that, adding operator support for pointers is fairly trivial. It would take me all of 10 minutes to add "type^" or "type*" as a synonym for "Address[type]". The question is, should I do it?

No comments:

Post a Comment