Implement mark&sweep garbage collection and bytecode compilation
Instead of the previous refcount base garbage collection, we're now using
a basic tri-color mark&sweep collector. This is done to support cyclical
value relationships in the future (functions can form cycles, all values
implemented up to this point can not).
The collector maintains a set of roots and a set of objects (grouped into
blocks). The GC enabled objects are no longer allocated manually, but will
be allocated by the GC. The GC also wraps an allocator, this way the GC
knows, if we ran out of memory and will try to get out of this situation by
performing a full collection cycle.
The tri-color abstraction was chosen for two reasons:
- We don't have to maintain a list of objects that need to be marked, we
can simply grab the next grey one.
- It should allow us to later implement incremental collection (right now
we only do a stop-the-world collection).
This also switches to a bytecode based evaluation of the code: We no longer
directly evaluate the AST, but first compile it into a series of
instructions, that are evaluated in a separate step. This was done in
preparation for inplementing functions: We only need to turn a function
body into instructions instead of evaluating the node again with each call
of the function. Also, since an instruction list is implemented as a GC
object, this then removes manual memory management of the function body and
it's child nodes. Since the GC and the bytecode go hand in hand, this was
done in one (giant) commit.
As a downside, we've now lost the ability do do list matching on
assignments. I've already started to work on implementing this in the new
architecture, but left it out of this commit, as it's already quite a large
commit :)
2022-04-11 20:24:22 +00:00
|
|
|
#ifndef APFL_CONTEXT_H
|
|
|
|
|
#define APFL_CONTEXT_H
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
#include "bytecode.h"
|
|
|
|
|
#include "hashmap.h"
|
|
|
|
|
#include "gc.h"
|
|
|
|
|
#include "value.h"
|
2022-04-15 12:41:22 +00:00
|
|
|
#include "scope.h"
|
Implement mark&sweep garbage collection and bytecode compilation
Instead of the previous refcount base garbage collection, we're now using
a basic tri-color mark&sweep collector. This is done to support cyclical
value relationships in the future (functions can form cycles, all values
implemented up to this point can not).
The collector maintains a set of roots and a set of objects (grouped into
blocks). The GC enabled objects are no longer allocated manually, but will
be allocated by the GC. The GC also wraps an allocator, this way the GC
knows, if we ran out of memory and will try to get out of this situation by
performing a full collection cycle.
The tri-color abstraction was chosen for two reasons:
- We don't have to maintain a list of objects that need to be marked, we
can simply grab the next grey one.
- It should allow us to later implement incremental collection (right now
we only do a stop-the-world collection).
This also switches to a bytecode based evaluation of the code: We no longer
directly evaluate the AST, but first compile it into a series of
instructions, that are evaluated in a separate step. This was done in
preparation for inplementing functions: We only need to turn a function
body into instructions instead of evaluating the node again with each call
of the function. Also, since an instruction list is implemented as a GC
object, this then removes manual memory management of the function body and
it's child nodes. Since the GC and the bytecode go hand in hand, this was
done in one (giant) commit.
As a downside, we've now lost the ability do do list matching on
assignments. I've already started to work on implementing this in the new
architecture, but left it out of this commit, as it's already quite a large
commit :)
2022-04-11 20:24:22 +00:00
|
|
|
|
|
|
|
|
struct stack {
|
|
|
|
|
struct apfl_value *items;
|
|
|
|
|
size_t len;
|
|
|
|
|
size_t cap;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct apfl_ctx_data {
|
|
|
|
|
struct gc gc;
|
|
|
|
|
|
|
|
|
|
struct scope *scope;
|
|
|
|
|
struct stack *stack;
|
|
|
|
|
|
|
|
|
|
int execution_line;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void apfl_stack_deinit(struct apfl_allocator, struct stack *);
|
|
|
|
|
void apfl_gc_stack_traverse(struct stack *, gc_visitor, void *);
|
|
|
|
|
|
|
|
|
|
bool apfl_stack_push(apfl_ctx, struct apfl_value);
|
|
|
|
|
bool apfl_stack_check_index(apfl_ctx, apfl_stackidx *);
|
|
|
|
|
bool apfl_stack_pop(apfl_ctx, struct apfl_value *value, apfl_stackidx);
|
|
|
|
|
bool apfl_stack_get(apfl_ctx, struct apfl_value *value, apfl_stackidx);
|
|
|
|
|
bool apfl_stack_drop(apfl_ctx, apfl_stackidx);
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif
|