Skip to content

Instantly share code, notes, and snippets.

@SeijiEmery
Last active January 30, 2017 23:22
Show Gist options
  • Save SeijiEmery/a6bfcdb3a80fc2c15926fdafb178d8cb to your computer and use it in GitHub Desktop.
Save SeijiEmery/a6bfcdb3a80fc2c15926fdafb178d8cb to your computer and use it in GitHub Desktop.
spec for asm toy interpreter + oo runtime
Asm project idea: mini interpreter
LANGUAGE SPEC
types:
64-bit int
64-bit float (?)
"object" pointer
"closure" pointer
c pointer (?)
c function pointer (?)
object data layout:
ptr ->
0x0 classinfo ptr
0x4 refcount
0x8 user data...
closures are special objects w/ the following layout:
ptr ->
0x0 classinfo (classid, argcount, argtypes)
0x4 refcount
0x8 function pointer
0xB bound arguments...
objects are ref counted, allocated from allocator:
memory blocks: linked list of malloced SOME_SIZE mem chunks, next item
free list: linked list of malloced SOME_SIZE free list chunks, next item
struct MemBlock {
MemBlock* prev;
size_t head = 0;
ubyte[SIZE] data;
}
struct FreeBlock {
FreeBlock* prev;
size_t head = 0;
ubyte[SIZE] data;
}
struct Allocator {
MemBlock* allocChain;
FreeBlock* freeChain;
}
naive, but should work ok.
Allocator use: separate allocators for
– 64-bit values (8 bytes) (?)
– up to 16 byte values
– up to 32 byte values
– up to 48 byte values
– up to 64 byte values
– up to 128 byte values
– up to 256 byte values
– malloc the rest
– store pointer type in upper bits of refcount (impl refcount as a signed 16-bit integer?)
EDIT: this might actually be how malloc() is implemented; should definitely bench first
to see if this approach is worthwhile (I'd hypothesize that it's at _least_ useful for
small values, and the freelist + allocator per size type might be faster than some implementations;
plus, it would be a worthwhile exercise to implement an allocator anyways).
INSTRUCTION LIST
- integer arithmetic
- floating point arithmetic
- branch
– call address
- dereference / getfield (ptr, offset)
– call pointer (?)
– call method (object, method index)
– push arg (closure)
– call closure (args from closure ptr)
– alloc obj (incl closure) from classinfo pointer
– retain obj (incl closure)
– release obj (incl closure)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment