Skip to content

Commit

Permalink
Just pass a char to neat_runtime_refcount_ instead of a string (cheap…
Browse files Browse the repository at this point in the history
…er).
  • Loading branch information
FeepingCreature committed Jan 4, 2024
1 parent cc81618 commit 74acbcb
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 29 deletions.
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ fi
mkdir -p build

echo "Building stage 1..."
# FIXME remove hack
cp src/runtime.c .cache/bootstrap/v0.5.1/neat-v0.5.1-gcc/src/runtime.c
FLAGS="$FLAGS -version=LLVMBackend"
FLAGS="$FLAGS -file-id-output build/fileIdPins"
# see generation.md
Expand Down
10 changes: 5 additions & 5 deletions src/neat/array.nt
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ class Array : Type
(nullable Expression | Error) do_(Expression source) {
mut auto context = context.withNamespace(context.compiler.exprAlias(context.namespace, "source", source));
auto refCountIncFun = new FunctionDeclaration(
"neat_runtime_refcount_inc", new Void, [
Parameter.simple("label", new Array(new Character, context.platform)),
"neat_runtime_refcount_inc2", new Void, [
Parameter.simple("label", new Pointer(new Character)),
Parameter.simple("ptr", new Pointer(new Void))]);
context = context.withNamespace(context.compiler.exprAlias(
context.namespace, "neat_runtime_refcount_inc", refCountIncFun));
context.namespace, "neat_runtime_refcount_inc2", refCountIncFun));
// count up reference
auto stmt = compiler.$stmt if (source.base) {
neat_runtime_refcount_inc("array acquire", source.base);
neat_runtime_refcount_inc2("array acquire", source.base);
};
auto result = stmt.compile(context)?;
return compiler.statementExpression(result.statement, source);
Expand Down Expand Up @@ -556,7 +556,7 @@ Expression releaseArray(Context context, Expression value) {
{
return compiler.$stmt {
if (!value.base) return;
if (!neat_runtime_refcount_dec("array release", value.base))
if (!neat_runtime_refcount_dec2("array release", value.base))
return;
for (i in 0 .. value.length) {
__destroy(value[i]);
Expand Down
12 changes: 6 additions & 6 deletions src/neat/compiler.nt
Original file line number Diff line number Diff line change
Expand Up @@ -1008,13 +1008,13 @@ class CompilerImpl : CompilerBase
"print", new Void, [Parameter.fromType(new Array(new Character, context.platform))]));
rtMod.register("neat_runtime_alloc", Protection.public_, new FunctionDeclaration(
"neat_runtime_alloc", new Pointer(new Void), [Parameter.fromType(context.nativeWordType)]));
rtMod.register("neat_runtime_refcount_inc", Protection.public_, new FunctionDeclaration(
"neat_runtime_refcount_inc", new Void, [
Parameter.simple("label", new Array(new Character, context.platform)),
rtMod.register("neat_runtime_refcount_inc2", Protection.public_, new FunctionDeclaration(
"neat_runtime_refcount_inc2", new Void, [
Parameter.simple("label", new Pointer(new Character)),
Parameter.simple("ptr", new Pointer(new Void))]));
rtMod.register("neat_runtime_refcount_dec", Protection.public_, new FunctionDeclaration(
"neat_runtime_refcount_dec", new Integer, [
Parameter.simple("label", new Array(new Character, context.platform)),
rtMod.register("neat_runtime_refcount_dec2", Protection.public_, new FunctionDeclaration(
"neat_runtime_refcount_dec2", new Integer, [
Parameter.simple("label", new Pointer(new Character)),
Parameter.simple("ptr", new Pointer(new Void))]));

fun.parent = rtMod;
Expand Down
10 changes: 5 additions & 5 deletions src/neat/hashmap.nt
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ class HashMap : Type
(nullable Expression | Error) do_(Expression source) {
mut auto context = context.withNamespace(context.compiler.exprAlias(context.namespace, "source", source));
auto refCountIncFun = new FunctionDeclaration(
"neat_runtime_refcount_inc", new Void, [
Parameter.simple("label", new Array(new Character, context.platform)),
"neat_runtime_refcount_inc2", new Void, [
Parameter.simple("label", new Pointer(new Character)),
Parameter.simple("ptr", new Pointer(new Void))]);
context = context.withNamespace(context.compiler.exprAlias(
context.namespace, "neat_runtime_refcount_inc", refCountIncFun));
context.namespace, "neat_runtime_refcount_inc2", refCountIncFun));
// count up reference
auto stmt = compiler.$stmt if (cast(void*) source != null) {
neat_runtime_refcount_inc("hashmap acquire", cast(size_t*) source);
neat_runtime_refcount_inc2("hashmap acquire", cast(size_t*) source);
};
auto result = stmt.compile(context)?;
return compiler.statementExpression(result.statement, source);
Expand Down Expand Up @@ -551,7 +551,7 @@ public Expression releaseHashmap(Context context, Expression value) {
ASTStatement body_() {
auto expr = context.compiler.$expr ({
if (cast(void*) value == null) return;
if (!neat_runtime_refcount_dec("hashmap release", cast(size_t*) value))
if (!neat_runtime_refcount_dec2("hashmap release", cast(size_t*) value))
return;
auto base = cast(HashMapBase*) value;
auto rows = cast(HashMapRow*) &base[1];
Expand Down
4 changes: 2 additions & 2 deletions src/neat/runtime.nt
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,5 @@ bottom die() {
exit(1);
}

extern(C) void neat_runtime_refcount_inc(string s, void* ptr);
extern(C) int neat_runtime_refcount_dec(string s, void* ptr);
extern(C) void neat_runtime_refcount_inc2(char* desc, void* ptr);
extern(C) int neat_runtime_refcount_dec2(char* desc, void* ptr);
3 changes: 2 additions & 1 deletion src/neat/struct_.nt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import neat.base;
import neat.decl;
import neat.expr;
import neat.function_;
import neat.runtime;
import neat.statements;
import neat.types;
import neat.util;
Expand Down Expand Up @@ -79,7 +80,7 @@ class Struct : Type, Hashable
backendCache.platform = platform;
backendCache.type = new BackendStructType(memberTypes.freeze);
// FIXME figure out why references go to zero
neat_runtime_refcount_inc("workaround", &(cast(size_t*) backendCache.type)[1]);
neat_runtime_refcount_inc2("workaround", &(cast(size_t*) backendCache.type)[1]);
}
return backendCache.type;
}
Expand Down
46 changes: 36 additions & 10 deletions src/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ void print_backtrace()

#define FATALERROR __attribute__((cold)) __attribute__((noinline)) __attribute__((noreturn))

void FATALERROR neat_runtime_refcount_violation(struct String s, ptrdiff_t *ptr)
void FATALERROR neat_runtime_refcount_violation(const char *desc, ptrdiff_t *ptr)
{
printf("<%.*s: refcount logic violated: %zd at %p\n", (int) s.length, s.ptr, *ptr, ptr);
printf("<%s: refcount logic violated: %zd at %p\n", desc, *ptr, ptr);
print_backtrace();
exit(1);
}
Expand All @@ -187,36 +187,62 @@ void FATALERROR neat_runtime_index_oob(size_t index)
exit(1);
}

void neat_runtime_refcount_inc(struct String s, ptrdiff_t *ptr)
// FIXME: rename back to _inc
void neat_runtime_refcount_inc2(const char *desc, ptrdiff_t *ptr)
{
// ptrdiff_t result = *ptr += 1;
ptrdiff_t result = __atomic_add_fetch(ptr, 1, __ATOMIC_ACQ_REL);
if (result <= 1)
{
neat_runtime_refcount_violation(s, ptr);
neat_runtime_refcount_violation(desc, ptr);
}
}

int neat_runtime_refcount_dec(struct String s, ptrdiff_t *ptr)
// FIXME: remove
void neat_runtime_refcount_inc(struct String desc, ptrdiff_t *ptr)
{
// ptrdiff_t result = *ptr += 1;
ptrdiff_t result = __atomic_add_fetch(ptr, 1, __ATOMIC_ACQ_REL);
if (result <= 1)
{
neat_runtime_refcount_violation(desc.ptr, ptr);
}
}

// FIXME: rename back to _dec
int neat_runtime_refcount_dec2(const char *desc, ptrdiff_t *ptr)
{
// ptrdiff_t result = *ptr -= 1;
ptrdiff_t result = __atomic_sub_fetch(ptr, 1, __ATOMIC_ACQ_REL);
if (result <= -1)
{
neat_runtime_refcount_violation(desc, ptr);
}

return result == 0;
}

// FIXME: remove
int neat_runtime_refcount_dec(struct String desc, ptrdiff_t *ptr)
{
// ptrdiff_t result = *ptr -= 1;
ptrdiff_t result = __atomic_sub_fetch(ptr, 1, __ATOMIC_ACQ_REL);
if (result <= -1)
{
neat_runtime_refcount_violation(s, ptr);
neat_runtime_refcount_violation(desc.ptr, ptr);
}

return result == 0;
}

void neat_runtime_class_refcount_inc(void **ptr) {
if (!ptr) return;
neat_runtime_refcount_inc((struct String){5, "class", NULL}, (ptrdiff_t*) &ptr[1]);
neat_runtime_refcount_inc2("class", (ptrdiff_t*) &ptr[1]);
}

void neat_runtime_class_refcount_dec(void **ptr) {
if (!ptr) return;
if (neat_runtime_refcount_dec((struct String){5, "class", NULL}, (ptrdiff_t*) &ptr[1]))
if (neat_runtime_refcount_dec2("class", (ptrdiff_t*) &ptr[1]))
{
void (**vtable)(void*) = *(void(***)(void*)) ptr;
void (*destroy)(void*) = vtable[1];
Expand All @@ -229,14 +255,14 @@ void neat_runtime_intf_refcount_inc(void *ptr) {
if (!ptr) return;
size_t base_offset = **(size_t**) ptr;
void **object = (void**) ((char*) ptr - base_offset);
neat_runtime_refcount_inc((struct String){9, "interface", NULL}, (ptrdiff_t*) &object[1]);
neat_runtime_refcount_inc2("interface", (ptrdiff_t*) &object[1]);
}

void neat_runtime_intf_refcount_dec(void *ptr) {
if (!ptr) return;
size_t base_offset = **(size_t**) ptr;
void **object = (void**) ((char*) ptr - base_offset);
if (neat_runtime_refcount_dec((struct String){9, "interface", NULL}, (ptrdiff_t*) &object[1])) {
if (neat_runtime_refcount_dec2("interface", (ptrdiff_t*) &object[1])) {
void (**vtable)(void*) = *(void(***)(void*)) object;
void (*destroy)(void*) = vtable[1];
destroy(object);
Expand Down

0 comments on commit 74acbcb

Please sign in to comment.