Skip to content

Commit 55706b1

Browse files
committed
added gc and also generic logging
1 parent 1c58fb2 commit 55706b1

File tree

10 files changed

+112
-37
lines changed

10 files changed

+112
-37
lines changed

examples/fibonacci.sq

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
proclaim(10);
21
journey fibonacci(n) {
32
if (n <= I) { reward n; }
43
reward fibonacci(n - I) + fibonacci(n - II);

include/squire/attributes.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@
8585
#endif /* SQ_HAS_ATTRIBUTE(nonnull) */
8686

8787
#if SQ_HAS_BUILTIN(__builtin_expect)
88-
# define SQ_LIKELY(x) (__builtin_expect(1, !!(x)))
88+
# define SQ_LIKELY(x) (__builtin_expect(!!(x), 1))
89+
# define SQ_UNLIKELY(x) (__builtin_expect(!!(x), 0))
8990
#else
9091
# define SQ_LIKELY(x) (!!(x))
92+
# define SQ_UNLIKELY(x) (!!(x))
9193
#endif /* SQ_HAS_BUILTIN(__builtin_expect) */
92-
#define SQ_UNLIKELY(x) (!SQ_LIKELY(!(x)))
9394

9495
#if SQ_HAS_BUILTIN(__builtin_unreachable)
9596
# define SQ_UNREACHABLE __builtin_unreachable()

include/squire/gc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
#include <squire/program.h>
55
#include <squire/valuedecl.h>
66

7+
/*
8+
todo: restyle these
9+
`gc initialize` := `sq_begin_black_death`
10+
`gc start sweep` := `sq_enter_the_body_cart`
11+
`gc mark` := `sq_condemn_XXX`
12+
lol
13+
*/
714
void sq_gc_init(long long heap_size, struct sq_program *program);
815
void sq_gc_start(void);
916
void sq_gc_teardown(void);

include/squire/log.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef SQ_LOG_H
2+
#define SQ_LOG_H
3+
4+
#include <squire/attributes.h>
5+
6+
#ifdef SQ_LOG_ALL
7+
# define SQ_LOG_GC
8+
# define SQ_LOG_PARSE
9+
#endif
10+
11+
void sq_log_fn(const char *category, const char *fmt, ...) SQ_NONNULL SQ_ATTR_PRINTF(2, 3);
12+
13+
#define SQ_EXPAND(x) x
14+
#define sq_log(category, verbosity, ...) SQ_EXPAND(sq_log_ ## category ## _ ## verbosity)(__VA_ARGS__)
15+
16+
#if SQ_LOG_GC >= 2
17+
# define sq_log_gc_2(...) sq_log_fn("GC[2]", __VA_ARGS__)
18+
#else
19+
# define sq_log_gc_2(...)
20+
#endif
21+
22+
#if SQ_LOG_GC >= 1
23+
# define sq_log_gc_1(...) sq_log_fn("GC[1]", __VA_ARGS__)
24+
#else
25+
# define sq_log_gc_1(...)
26+
#endif
27+
28+
// #if SQ_LOG_PARSE >= 1
29+
// # define sq_log_parse(...) sq_log_fn("PARSE", __VA_ARGS__)
30+
// #else
31+
// # define sq_log_parse(...)
32+
// #endif
33+
34+
#endif

include/squire/shared.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#include <stdlib.h>
88

99
#ifdef SQ_LOG
10-
#define sq_log printf
10+
#define sq_log_old printf
1111
#else
12-
#define sq_log(...) ((void) 0)
12+
#define sq_log_old(...) ((void) 0)
1313
#endif /* SQ_LOG */
1414

1515

src/gc.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#include <squire/gc.h>
22
#include <squire/basic.h>
3+
#include <squire/log.h>
34
#include <squire/shared.h>
45
#include <sys/mman.h>
56

67
struct anyvalue {
78
struct sq_basic basic;
89
SQ_ALIGNAS(SQ_VALUE_ALIGNMENT) char _ignored[SQ_VALUE_SIZE - SQ_VALUE_ALIGNMENT];
910
};
10-
_Static_assert(sizeof(struct anyvalue) == SQ_VALUE_SIZE, "size isnt equal");
11+
12+
SQ_STATIC_ASSERT(sizeof(struct anyvalue) == SQ_VALUE_SIZE, "size isnt equal");
1113

1214
struct sq_program *program;
1315
struct anyvalue *heap_start, *heap;
@@ -16,11 +18,14 @@ long long heap_size;
1618

1719
void sq_gc_init(long long heap_size_, struct sq_program *program_) {
1820
heap_size = heap_size_ * SQ_VALUE_SIZE;
21+
1922
program = program_;
2023
heap_start = heap = mmap(NULL, heap_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0);
2124

2225
if (heap == MAP_FAILED)
2326
sq_throw_io("unable to mmap %zu bytes for the heap", heap_size);
27+
28+
sq_log(gc, 1, "initialized gc heap with %lld (0x%llx) bytes of memory", heap_size, heap_size);
2429
}
2530

2631
void sq_gc_teardown(void) {
@@ -36,30 +41,48 @@ void *sq_gc_malloc(enum sq_genus_tag genus) {
3641
sq_throw("heap exhausted.");
3742
}
3843

39-
while (heap->basic.in_use) ++heap;
44+
while (heap->basic.in_use) {
45+
sq_log(gc, 2, "skipping in-use address %p", (void *) heap);
46+
++heap;
47+
}
4048
heap->basic.genus = genus;
4149
heap->basic.in_use = 1;
50+
sq_log(gc, 2, "found unused heap at address %p", (void *) heap);
4251
return heap++;
4352
}
4453

4554
void sq_gc_start(void) {
46-
// for (struct anyvalue *ptr = heap_start; ptr < heap; ++ptr) {
47-
// if (!ptr->basic.in_use) continue;
48-
// sq_value_mark(sq_value_new_ptr_unchecked((void *) ptr, ptr->basic.genus));
49-
// }
55+
#if SQ_LOG_GC >= 1
56+
#define INCR_METRIC(name) do { name++; } while(0);
57+
long unsigned unused = 0, marked = 0, freed = 0;
58+
sq_log(gc, 1, "starting gc cycle");
59+
#else
60+
#define INCR_METRIC(name) do { } while(0)
61+
#endif
5062

5163
sq_program_mark(program);
5264

5365
for (struct anyvalue *ptr = heap_start; ptr < heap; ++ptr) {
54-
if (!ptr->basic.in_use) continue;
66+
if (!ptr->basic.in_use) {
67+
sq_log(gc, 2, "ptr %p is in unused", heap);
68+
INCR_METRIC(unused);
69+
continue;
70+
}
71+
5572
if (ptr->basic.marked) {
73+
sq_log(gc, 2, "ptr %p is in marked", heap);
74+
INCR_METRIC(marked);
5675
ptr->basic.marked = 0;
5776
continue;
5877
}
5978

79+
sq_log(gc, 2, "ptr %p is in unmarked", heap);
80+
INCR_METRIC(freed);
81+
6082
sq_value_deallocate(sq_value_new_ptr_unchecked((void *) ptr, ptr->basic.genus));
6183
ptr->basic.in_use = 0;
6284
}
6385

86+
sq_log(gc, 1, "gc finished: %lu unused, %lu marked, %lu freed", unused, marked, freed);
6487
heap = heap_start;
6588
}

src/log.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <squire/log.h>
2+
#include <stdarg.h>
3+
#include <stdio.h>
4+
5+
void sq_log_fn(const char *category, const char *fmt, ...) {
6+
va_list args;
7+
va_start(args, fmt);
8+
9+
printf("LOG [%s] ", category);
10+
vprintf(fmt, args);
11+
putchar('\n');
12+
}

src/program/compile.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,32 @@ static void extend_bytecode_cap(struct sq_code *code) {
6464
}
6565

6666
static void set_opcode(struct sq_code *code, enum sq_opcode opcode) {
67-
sq_log("bytecode[%d].opcode=%s\n", code->codelen, sq_opcode_repr(opcode));
67+
sq_log_old("bytecode[%d].opcode=%s\n", code->codelen, sq_opcode_repr(opcode));
6868
extend_bytecode_cap(code);
6969
code->bytecode[code->codelen++].opcode = opcode;
7070
}
7171

7272
static void set_index(struct sq_code *code, unsigned index) {
73-
sq_log("bytecode[%d].index=%d\n", code->codelen, index);
73+
sq_log_old("bytecode[%d].index=%d\n", code->codelen, index);
7474
extend_bytecode_cap(code);
7575
code->bytecode[code->codelen++].index = index;
7676
}
7777

7878
static void set_interrupt(struct sq_code *code, enum sq_interrupt interrupt) {
79-
sq_log("bytecode[%d].interrupt=%s\n", code->codelen, sq_interrupt_repr(interrupt));
79+
sq_log_old("bytecode[%d].interrupt=%s\n", code->codelen, sq_interrupt_repr(interrupt));
8080
extend_bytecode_cap(code);
8181
code->bytecode[code->codelen++].interrupt = interrupt;
8282
}
8383

8484
static void set_count(struct sq_code *code, unsigned count) {
85-
sq_log("bytecode[%d].count=%d\n", code->codelen, count);
85+
sq_log_old("bytecode[%d].count=%d\n", code->codelen, count);
8686

8787
RESIZE(codecap, codelen, bytecode, union sq_bytecode);
8888
code->bytecode[code->codelen++].count = count;
8989
}
9090

9191
static void set_target_to_codelen(struct sq_code *code, unsigned target) {
92-
sq_log("bytecode[%d].index=%d [update]\n", target, code->codelen);
92+
sq_log_old("bytecode[%d].index=%d [update]\n", target, code->codelen);
9393

9494
code->bytecode[target].index = code->codelen;
9595
}
@@ -108,7 +108,7 @@ static unsigned declare_constant(struct sq_code *code, sq_value value) {
108108
printf("consts[%d]=", code->consts.len);
109109
sq_value_dump(stdout, value);
110110
putchar('\n');
111-
#endif /* sq_log */
111+
#endif /* sq_log_old */
112112

113113
code->consts.ary[code->consts.len] = value;
114114
return code->consts.len++;
@@ -171,7 +171,7 @@ static unsigned declare_global_variable(const char *name, sq_value value) {
171171
globals.ary = sq_realloc_vec(struct global, globals.ary, globals.cap);
172172
}
173173

174-
sq_log("global[%d]: %s\n", globals.len, name);
174+
sq_log_old("global[%d]: %s\n", globals.len, name);
175175

176176
// initialize the global
177177
globals.ary[globals.len].name = strdup(name);
@@ -191,7 +191,7 @@ static unsigned declare_local_variable(struct sq_code *code, const char *name) {
191191
// reallocate if necessary
192192
RESIZE(vars.cap, vars.len, vars.ary, struct local);
193193

194-
sq_log("local[%d]: %s\n", globals.len, name);
194+
sq_log_old("local[%d]: %s\n", globals.len, name);
195195

196196
code->vars.ary[code->vars.len].name = strdup(name);
197197
return code->vars.ary[code->vars.len++].index = next_local(code);

src/program/compiler.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@ static void extend_bytecode_cap(struct sq_compiler *compiler) {
1212
}
1313

1414
void sq_compiler_set_opcode(struct sq_compiler *compiler, enum sq_opcode opcode) {
15-
sq_log("bytecode[%d].opcode=%s\n", compiler->code.len, sq_opcode_repr(opcode));
15+
sq_log_old("bytecode[%d].opcode=%s\n", compiler->code.len, sq_opcode_repr(opcode));
1616

1717
extend_bytecode_cap(compiler);
1818

1919
compiler->code.ary[compiler->code.len++].opcode = opcode;
2020
}
2121

2222
void sq_compiler_set_index(struct sq_compiler *compiler, unsigned index) {
23-
sq_log("bytecode[%d].index=%d\n", compiler->code.len, index);
23+
sq_log_old("bytecode[%d].index=%d\n", compiler->code.len, index);
2424

2525
extend_bytecode_cap(compiler);
2626

2727
compiler->code.ary[compiler->code.len++].index = index;
2828
}
2929

3030
void sq_compiler_set_interrupt(struct sq_compiler *compiler, enum sq_interrupt interrupt) {
31-
sq_log("bytecode[%d].interrupt=%s\n", compiler->code.len, sq_interrupt_repr(interrupt));
31+
sq_log_old("bytecode[%d].interrupt=%s\n", compiler->code.len, sq_interrupt_repr(interrupt));
3232

3333
extend_bytecode_cap(compiler);
3434

3535
compiler->code.ary[compiler->code.len++].interrupt = interrupt;
3636
}
3737

3838
void sq_compiler_set_count(struct sq_compiler *compiler, unsigned count) {
39-
sq_log("bytecode[%d].count=%d\n", compiler->code.len, count);
39+
sq_log_old("bytecode[%d].count=%d\n", compiler->code.len, count);
4040

4141
extend_bytecode_cap(compiler);
4242

@@ -68,7 +68,7 @@ unsigned sq_compiler_constant_declare(struct sq_compiler *compiler, sq_value con
6868
printf("consts[%d]=", index);
6969
sq_value_dump(stdout, constant);
7070
putchar('\n');
71-
#endif /* sq_log */
71+
#endif /* sq_log_old */
7272

7373
compiler->consts.ary[index] = constant;
7474

@@ -124,7 +124,7 @@ unsigned sq_compiler_global_declare(struct sq_globals *globals, char *name, sq_v
124124

125125
index = globals->len++;
126126

127-
sq_log("global[%d]: %s\n", index, name);
127+
sq_log_old("global[%d]: %s\n", index, name);
128128

129129
// initialize the global
130130
globals->ary[index].name = strdup(name);
@@ -168,7 +168,7 @@ unsigned sq_compiler_variable_declare(struct sq_compiler *compiler, char *name)
168168
unsigned variable_index = compiler->variables.len++;
169169
unsigned local_index = sq_compiler_next_local(compiler);
170170

171-
sq_log("locals[%d]: %s (variables[%d])\n", local_index, name, variable_index);
171+
sq_log_old("locals[%d]: %s (variables[%d])\n", local_index, name, variable_index);
172172

173173
compiler->variables.ary[variable_index].name = name;
174174
compiler->variables.ary[variable_index].index = local_index;

src/value/journey.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
# endif /* defined(__clang__) */
2222
# define VM_SWITCH(oc, labels) goto *(labels)[oc];
2323
# define VM_CASE_NAME(oc) vm_case_##oc
24-
# define VM_CASE(oc) SQ_UNREACHABLE /* no fallthroughs */; VM_CASE_NAME(oc):
24+
# define VM_CASE(oc) SQ_UNREACHABLE /* no fallthroughs */; VM_CASE_FT(oc)
25+
# define VM_CASE_FT(oc) VM_CASE_NAME(oc):
2526
# define VM_SWITCH_END
2627
# define VM_DEFAULT if (0)
2728
#else
2829
# define VM_SWITCH(oc, _labels) switch (oc) {
29-
# define VM_CASE(oc) SQ_UNREACHABLE /* no fallthroughs */; case oc :
30+
# define VM_CASE(oc) SQ_UNREACHABLE /* no fallthroughs */; VM_CASE_FT(oc)
31+
# define VM_CASE_FT(oc) case oc:
3032
# define VM_SWITCH_END }
3133
# define VM_DEFAULT default:
3234
#endif /* defined(SQ_USE_COMPUTED_GOTOS) */
@@ -60,7 +62,6 @@ void sq_stackframe_mark(struct sq_stackframe *stackframe) {
6062

6163
void sq_journey_mark(struct sq_journey *journey) {
6264
SQ_GUARD_MARK(journey);
63-
printf("marking: %s (%d)\n", journey->name, journey->npatterns);
6465

6566
for (unsigned i = 0; i < journey->npatterns; ++i) {
6667
struct sq_journey_pattern pattern = journey->patterns[i];
@@ -252,7 +253,7 @@ bool sq_moon_joke_does_were_flip() {
252253

253254
static inline union sq_bytecode next_bytecode(struct sq_stackframe *sf) {
254255
union sq_bytecode bc = sf->pattern->code.bytecode[sf->ip++];
255-
sq_log("runtime[%d]=%u\n", sf->ip-1, bc.index);
256+
sq_log_old("runtime[%d]=%u\n", sf->ip-1, bc.index);
256257
return bc;
257258
}
258259

@@ -488,8 +489,6 @@ static void handle_interrupt(struct sq_stackframe *sf) {
488489
VM_CASE(SQ_INT_DUMP)
489490
sq_value_dump(stdout, operands[0]);
490491
set_next_local(sf, operands[0]);
491-
sq_gc_start();
492-
#warning gc start in dump
493492
return;
494493

495494
// [DST] DST <- next line from stdin
@@ -814,9 +813,9 @@ static sq_value sq_run_stackframe(struct sq_stackframe *sf) {
814813
continue;
815814

816815
VM_CASE(SQ_OC_JMP_FALSE)
817-
VM_CASE(SQ_OC_JMP_TRUE)
816+
VM_CASE_FT(SQ_OC_JMP_TRUE)
818817
#ifndef SQ_NMOON_JOKE
819-
VM_CASE(SQ_OC_WERE_JMP)
818+
VM_CASE_FT(SQ_OC_WERE_JMP)
820819
#endif /* SQ_NMOON_JOKE */
821820
index = next_index(sf);
822821
bool should_jump = sq_value_to_veracity(operands[0]) == (opcode == SQ_OC_JMP_TRUE);
@@ -912,8 +911,8 @@ static sq_value sq_run_stackframe(struct sq_stackframe *sf) {
912911
SET_RESULT(sq_value_new_veracity(sq_value_matches(operands[0], operands[1])));
913912

914913
VM_CASE(SQ_OC_PAT_NOT)
915-
VM_CASE(SQ_OC_PAT_OR)
916-
VM_CASE(SQ_OC_PAT_AND) {
914+
VM_CASE_FT(SQ_OC_PAT_OR)
915+
VM_CASE_FT(SQ_OC_PAT_AND) {
917916
struct sq_other *helper = sq_mallocv(struct sq_other);
918917
helper->kind = SQ_OK_PAT_HELPER;
919918
helper->helper.left = operands[0];

0 commit comments

Comments
 (0)