Compare commits

...

2 commits

Author SHA1 Message Date
6247cc7f84 . 2026-02-19 11:48:16 -07:00
c841ece42e fixes + macros 2026-02-19 11:08:34 -07:00
4 changed files with 60 additions and 47 deletions

8
do.ps1
View file

@ -145,6 +145,7 @@ switch ($command) {
Write-Host (":: Compiling natively" + $(if ($release) { " (release mode)" } else { "" }));
Write-Host $cmd;
Invoke-Expression $cmd | Write-Host;
}
{($_ -eq "build-cross") -or ($_ -eq "bc")} {
@ -161,6 +162,7 @@ switch ($command) {
Write-Host (":: Cross-compiling for $target" + $(if ($release) { " (release mode)" } else { "" }));
Write-Host $cmd;
Invoke-Expression $cmd | Write-Host;
}
{($_ -eq "run") -or ($_ -eq "r")} {
@ -170,8 +172,12 @@ switch ($command) {
Write-Host (":: Compiling natively" + $(if ($release) { " (release mode)" } else { "" }));
Write-Host $cmd;
Invoke-Expression $cmd | Write-Host;
Invoke-Expression $binary | Write-Host;
if ($LASTEXITCODE -eq 0) {
Invoke-Expression $binary | Write-Host;
}
}
{($_ -eq "list-targets") -or ($_ -eq "lt")} {
# zig target doesn't actually output json...

View file

@ -4,7 +4,7 @@
typedef enum { arena__alloc, arena__reset, arena__save, arena__restore, arena__release } arena__action;
typedef void* (^Arena)(arena__action, ureg, ureg, void*, const char*, sreg);
typedef closure(void*, arena__action, uregister, uregister, void*, const char*, sregister) Arena;
#define arena_new(A, T) cast(T*, A(arena__alloc, sizeof(T), alignof(T), 0, __FILE__, __LINE__))
#define arena_make(A, C, T) cast(T*, A(arena__alloc, sizeof(T) * (C), alignof(T), 0, __FILE__, __LINE__))
@ -13,22 +13,23 @@ typedef void* (^Arena)(arena__action, ureg, ureg, void*, const char*, sreg);
#define arena_save(A, SP) A(arena__save, 0, 0, (SP), __FILE__, __LINE__)
#define arena_restore(A, SP) A(arena__restore, 0, 0, (SP), __FILE__, __LINE__)
// just a nice macro to make these easier to write
#define func(a1, a2, a3, a4, a5, a6) ^void* (arena__action a1, ureg a2, ureg a3, void* a4, const char* a5, sreg a6)
// tiny macro to make arenas easier to declare
#define $(a1, a2, a3, a4, a5, a6) ^void* (arena__action a1, uregister a2, uregister a3, void* a4, const char* a5, sregister a6)
static Arena
Static(u8* data, ureg count) {
Static(uint8* data, uregister count) {
memset(data, 0, count);
capture ureg offset = 0;
return closure(func(action, size, align, savepoint, _2, _3) {
capture uregister offset = 0;
return persist($(action, size, align, savepoint, _2, _3) {
switch (action) {
default: {
} break;
case arena__alloc: {
offset = (offset + align - 1) & ~(align - 1);
if (offset + size > count) return 0;
if (offset + size > count)
{ return 0; }
void* ptr = data + offset;
offset += size;
@ -42,12 +43,12 @@ Static(u8* data, ureg count) {
case arena__save: {
assert(savepoint != NULL && "Static: savepoint was null");
*cast(ureg*, savepoint) = offset;
*cast(uregister*, savepoint) = offset;
} break;
case arena__restore: {
assert(savepoint != NULL && "Static: savepoint was null");
offset = *cast(ureg*, savepoint);
offset = *cast(uregister*, savepoint);
} break;
}
@ -56,10 +57,10 @@ Static(u8* data, ureg count) {
}
static Arena
Linear(ureg max_memory) {
Linear(uregister max_memory) {
void* ptr = malloc(max_memory);
Arena backing = Static(ptr, max_memory);
return closure(func(action, size, align, savepoint, file, line) {
return persist($(action, size, align, savepoint, file, line) {
if (action == arena__release) {
free(ptr);
return 0;
@ -71,7 +72,7 @@ Linear(ureg max_memory) {
static Arena
Logger(Arena arena) {
return closure(func(action, size, align, savepoint, file, line) {
return persist($(action, size, align, savepoint, file, line) {
char* action_str = "unknown";
switch (action) {
case arena__alloc: action_str = "alloc"; break;
@ -88,10 +89,10 @@ Logger(Arena arena) {
static Arena
Region(Arena arena) {
capture ureg savepoint = 0;
capture uregister savepoint = 0;
arena_save(arena, &savepoint);
return closure(func(action, size, align, _, file, line) {
return persist($(action, size, align, _, file, line) {
if (action == arena__reset || action == arena__restore) {
return arena(arena__restore, size, align, &savepoint, file, line);
}
@ -100,4 +101,4 @@ Region(Arena arena) {
});
}
#undef func
#undef $

View file

@ -1,23 +1,23 @@
#include <stdint.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef int8_t sint8;
typedef int16_t sint16;
typedef int32_t sint32;
typedef int64_t sint64;
typedef float f32;
typedef double f64;
typedef float float32;
typedef double float64;
typedef uintptr_t uptr;
typedef intptr_t sptr;
typedef uintptr_t upointer;
typedef intptr_t spointer;
typedef uptr ureg;
typedef sptr sreg;
typedef upointer uregister;
typedef spointer sregister;
#define auto __auto_type
@ -27,19 +27,21 @@ typedef sptr sreg;
#define ptrcast(to_type, from_type, expr) _Generic(expr, from_type: (to_type)(uintptr_t)(expr))
#define bitcast(to_type, from_type, expr) (((union { from_type from; to_type to; }){_Generic(expr, from_type: expr)}).to)
#define procedure(ret_type, ...) typeof(ret_type(__VA_ARGS__))*
#define closure(ret_type, ...) typeof(ret_type(__VA_ARGS__))^
#include <Block.h>
#define capture __block
#define closure Block_copy
#define capture __block // allows a variable to be mutably captured by a closure (will be moved to the heap if needed)
#define persist Block_copy // moves a closure to the heap (reference counted) so it can persist outside of its local stack frame
#define unpersist Block_release // frees a closure (or decrements its reference count)
typedef void (^__defer__block)(void);
static inline void __defer__exec(__defer__block *blk) { (*blk)(); }
typedef closure(void, void) __scope_exit;
static inline void __scope_exit_run(__scope_exit *blk) { (*blk)(); }
#define __concat__2(a, b) a##b
#define __concat(a, b) __concat__2(a, b)
#define scope_exit \
__attribute__((cleanup(__defer__exec), unused)) \
__defer__block __concat(_defer_, __LINE__) = ^
__attribute__((cleanup(__scope_exit_run), unused)) \
__scope_exit __concat(_defer_, __LINE__) = ^void(void)

View file

@ -25,6 +25,10 @@ sg_pass_action pass_action;
static void
init(void) {
scope_exit {
printf("leaving init...");
};
sg_setup(&(sg_desc){
.environment = sglue_environment(),
.logger.func = slog_func,
@ -50,12 +54,12 @@ init(void) {
// c23 tests
capture auto x = 0;
auto inc = closure(^{
auto inc = persist(^{
x += 1;
});
printf("x = %d (before)\n", x);
for (ureg i = 0; i < 10; i += 1) {
for (uregister i = 0; i < 10; i += 1) {
inc();
}
printf("x = %d (after)\n", x);
@ -63,19 +67,19 @@ init(void) {
auto arena = Logger(Linear(1024 * 1024));
scope_exit { arena_release(arena); };
sreg* a = arena_new(arena, sreg);
sreg* b = arena_new(arena, sreg);
sregister* a = arena_new(arena, sregister);
sregister* b = arena_new(arena, sregister);
{
auto region = Region(arena);
scope_exit { arena_reset(region); };
arena_make(region, 10, sreg);
arena_make(region, 10, f32);
arena_make(region, 10, sregister);
arena_make(region, 10, float32);
arena_make(region, 10, bool);
}
sreg* c = arena_new(arena, sreg);
sregister* c = arena_new(arena, sregister);
*a = 1024;
*b = 2048;
@ -87,9 +91,9 @@ init(void) {
arena_reset(arena);
c = arena_new(arena, sreg);
b = arena_new(arena, sreg);
a = arena_new(arena, sreg);
c = arena_new(arena, sregister);
b = arena_new(arena, sregister);
a = arena_new(arena, sregister);
printf("%zu (%p)\n", *a, a);
printf("%zu (%p)\n", *b, b);