Compare commits

..

No commits in common. "6247cc7f844bcd2564ef2ec7c683142ae851ead4" and "b18962cc82e6b070ba980904680c7f24d7a9f53e" have entirely different histories.

4 changed files with 47 additions and 60 deletions

8
do.ps1
View file

@ -145,7 +145,6 @@ switch ($command) {
Write-Host (":: Compiling natively" + $(if ($release) { " (release mode)" } else { "" })); Write-Host (":: Compiling natively" + $(if ($release) { " (release mode)" } else { "" }));
Write-Host $cmd; Write-Host $cmd;
Invoke-Expression $cmd | Write-Host; Invoke-Expression $cmd | Write-Host;
} }
{($_ -eq "build-cross") -or ($_ -eq "bc")} { {($_ -eq "build-cross") -or ($_ -eq "bc")} {
@ -162,7 +161,6 @@ switch ($command) {
Write-Host (":: Cross-compiling for $target" + $(if ($release) { " (release mode)" } else { "" })); Write-Host (":: Cross-compiling for $target" + $(if ($release) { " (release mode)" } else { "" }));
Write-Host $cmd; Write-Host $cmd;
Invoke-Expression $cmd | Write-Host; Invoke-Expression $cmd | Write-Host;
} }
{($_ -eq "run") -or ($_ -eq "r")} { {($_ -eq "run") -or ($_ -eq "r")} {
@ -172,12 +170,8 @@ switch ($command) {
Write-Host (":: Compiling natively" + $(if ($release) { " (release mode)" } else { "" })); Write-Host (":: Compiling natively" + $(if ($release) { " (release mode)" } else { "" }));
Write-Host $cmd; Write-Host $cmd;
Invoke-Expression $cmd | Write-Host; 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")} { {($_ -eq "list-targets") -or ($_ -eq "lt")} {
# zig target doesn't actually output json... # 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 enum { arena__alloc, arena__reset, arena__save, arena__restore, arena__release } arena__action;
typedef closure(void*, arena__action, uregister, uregister, void*, const char*, sregister) Arena; typedef void* (^Arena)(arena__action, ureg, ureg, void*, const char*, sreg);
#define arena_new(A, T) cast(T*, A(arena__alloc, sizeof(T), alignof(T), 0, __FILE__, __LINE__)) #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__)) #define arena_make(A, C, T) cast(T*, A(arena__alloc, sizeof(T) * (C), alignof(T), 0, __FILE__, __LINE__))
@ -13,23 +13,22 @@ typedef closure(void*, arena__action, uregister, uregister, void*, const char*,
#define arena_save(A, SP) A(arena__save, 0, 0, (SP), __FILE__, __LINE__) #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__) #define arena_restore(A, SP) A(arena__restore, 0, 0, (SP), __FILE__, __LINE__)
// tiny macro to make arenas easier to declare // just a nice macro to make these easier to write
#define $(a1, a2, a3, a4, a5, a6) ^void* (arena__action a1, uregister a2, uregister a3, void* a4, const char* a5, sregister a6) #define func(a1, a2, a3, a4, a5, a6) ^void* (arena__action a1, ureg a2, ureg a3, void* a4, const char* a5, sreg a6)
static Arena static Arena
Static(uint8* data, uregister count) { Static(u8* data, ureg count) {
memset(data, 0, count); memset(data, 0, count);
capture uregister offset = 0; capture ureg offset = 0;
return persist($(action, size, align, savepoint, _2, _3) { return closure(func(action, size, align, savepoint, _2, _3) {
switch (action) { switch (action) {
default: { default: {
} break; } break;
case arena__alloc: { case arena__alloc: {
offset = (offset + align - 1) & ~(align - 1); offset = (offset + align - 1) & ~(align - 1);
if (offset + size > count) if (offset + size > count) return 0;
{ return 0; }
void* ptr = data + offset; void* ptr = data + offset;
offset += size; offset += size;
@ -43,12 +42,12 @@ Static(uint8* data, uregister count) {
case arena__save: { case arena__save: {
assert(savepoint != NULL && "Static: savepoint was null"); assert(savepoint != NULL && "Static: savepoint was null");
*cast(uregister*, savepoint) = offset; *cast(ureg*, savepoint) = offset;
} break; } break;
case arena__restore: { case arena__restore: {
assert(savepoint != NULL && "Static: savepoint was null"); assert(savepoint != NULL && "Static: savepoint was null");
offset = *cast(uregister*, savepoint); offset = *cast(ureg*, savepoint);
} break; } break;
} }
@ -57,10 +56,10 @@ Static(uint8* data, uregister count) {
} }
static Arena static Arena
Linear(uregister max_memory) { Linear(ureg max_memory) {
void* ptr = malloc(max_memory); void* ptr = malloc(max_memory);
Arena backing = Static(ptr, max_memory); Arena backing = Static(ptr, max_memory);
return persist($(action, size, align, savepoint, file, line) { return closure(func(action, size, align, savepoint, file, line) {
if (action == arena__release) { if (action == arena__release) {
free(ptr); free(ptr);
return 0; return 0;
@ -72,7 +71,7 @@ Linear(uregister max_memory) {
static Arena static Arena
Logger(Arena arena) { Logger(Arena arena) {
return persist($(action, size, align, savepoint, file, line) { return closure(func(action, size, align, savepoint, file, line) {
char* action_str = "unknown"; char* action_str = "unknown";
switch (action) { switch (action) {
case arena__alloc: action_str = "alloc"; break; case arena__alloc: action_str = "alloc"; break;
@ -89,10 +88,10 @@ Logger(Arena arena) {
static Arena static Arena
Region(Arena arena) { Region(Arena arena) {
capture uregister savepoint = 0; capture ureg savepoint = 0;
arena_save(arena, &savepoint); arena_save(arena, &savepoint);
return persist($(action, size, align, _, file, line) { return closure(func(action, size, align, _, file, line) {
if (action == arena__reset || action == arena__restore) { if (action == arena__reset || action == arena__restore) {
return arena(arena__restore, size, align, &savepoint, file, line); return arena(arena__restore, size, align, &savepoint, file, line);
} }
@ -101,4 +100,4 @@ Region(Arena arena) {
}); });
} }
#undef $ #undef func

View file

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

View file

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