From 74acbcb86b0025258258e7ce4dd7d0d8b18094e3 Mon Sep 17 00:00:00 2001 From: FeepingCreature Date: Thu, 4 Jan 2024 08:16:45 +0100 Subject: [PATCH] Just pass a char to neat_runtime_refcount_ instead of a string (cheaper). --- build.sh | 2 ++ src/neat/array.nt | 10 +++++----- src/neat/compiler.nt | 12 ++++++------ src/neat/hashmap.nt | 10 +++++----- src/neat/runtime.nt | 4 ++-- src/neat/struct_.nt | 3 ++- src/runtime.c | 46 ++++++++++++++++++++++++++++++++++---------- 7 files changed, 58 insertions(+), 29 deletions(-) diff --git a/build.sh b/build.sh index 330c108..4c9952d 100755 --- a/build.sh +++ b/build.sh @@ -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 diff --git a/src/neat/array.nt b/src/neat/array.nt index 4776ee4..28335b0 100644 --- a/src/neat/array.nt +++ b/src/neat/array.nt @@ -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); @@ -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]); diff --git a/src/neat/compiler.nt b/src/neat/compiler.nt index cc5a771..3f5362f 100644 --- a/src/neat/compiler.nt +++ b/src/neat/compiler.nt @@ -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; diff --git a/src/neat/hashmap.nt b/src/neat/hashmap.nt index 78c7656..9d6539b 100644 --- a/src/neat/hashmap.nt +++ b/src/neat/hashmap.nt @@ -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); @@ -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]; diff --git a/src/neat/runtime.nt b/src/neat/runtime.nt index 83e2b5d..425acc9 100644 --- a/src/neat/runtime.nt +++ b/src/neat/runtime.nt @@ -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); diff --git a/src/neat/struct_.nt b/src/neat/struct_.nt index cf3bb67..ceea42c 100644 --- a/src/neat/struct_.nt +++ b/src/neat/struct_.nt @@ -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; @@ -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; } diff --git a/src/runtime.c b/src/runtime.c index 82c2c82..8d0f839 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -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); } @@ -187,23 +187,49 @@ 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; @@ -211,12 +237,12 @@ int neat_runtime_refcount_dec(struct String s, ptrdiff_t *ptr) 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]; @@ -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);