Skip to content

Commit

Permalink
add more static allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
HackerFoo committed Aug 15, 2020
1 parent 87005dd commit 84830d0
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 65 deletions.
5 changes: 5 additions & 0 deletions cgen/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "startle/support.h"
#include "startle/error.h"
#include "startle/log.h"
#include "startle/static_alloc.h"
#include "cgen/primitives.h"
#include "io.h"

#define MAIN(fn) \
int main(UNUSED int argc, UNUSED char **argv) \
Expand All @@ -20,6 +22,9 @@
print_last_log_msg(); \
return -error.type; \
} \
static_alloc_init(); \
log_init(); \
io_init(); \
init_primitives(); \
fn(SYM_IO); \
return 0; \
Expand Down
10 changes: 5 additions & 5 deletions debug/log_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "debug/log_tree.h"
#include "var.h"

static MAP(map, 255);
STATIC_ALLOC(log_tree_map, pair_t, 256);

static
void fprint_arg(FILE *f, map_t map, cell_t *c) {
Expand Down Expand Up @@ -110,8 +110,8 @@ void fprint_tree(cell_t *c, map_t map, FILE *f) {

void print_tree(cell_t *c) {
if(c) {
map_clear(map);
fprint_tree(c, map, stdout);
init_map(log_tree_map, log_tree_map_size);
fprint_tree(c, log_tree_map, stdout);
}
}

Expand All @@ -121,11 +121,11 @@ void log_trees() {
tag_t tag;
get_tag(tag);
fprintf(f, "# TAG: " FORMAT_TAG "\n", tag);
map_clear(map);
init_map(log_tree_map, log_tree_map_size);
COUNTUP(i, rt_roots_size) {
cell_t **p = rt_roots[i];
if(p && *p) {
fprint_tree(*p, map, f);
fprint_tree(*p, log_tree_map, f);
}
}
fprintf(f, "\n");
Expand Down
8 changes: 4 additions & 4 deletions debug/tags.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// Provides a way to assign string tags to pointers, which is useful
// to understand what things are when debugging.

static MAP(tags, 4096);
STATIC_ALLOC(tags_map, pair_t, 256);

#if INTERFACE
#define TAG_PTR(ptr, str) \
Expand All @@ -35,13 +35,13 @@ static MAP(tags, 4096);

void set_ptr_tag(const void *ptr, const char *str) {
pair_t p = { (uintptr_t)ptr, (uintptr_t)str };
map_insert(tags, p);
map_insert(tags_map, p);
}

// TODO: a way to remove tags

const char *get_ptr_tag(const void *ptr) {
pair_t *p = map_find(tags, (uintptr_t)ptr);
pair_t *p = map_find(tags_map, (uintptr_t)ptr);
if(p) {
return (const char *)p->second;
} else {
Expand All @@ -50,5 +50,5 @@ const char *get_ptr_tag(const void *ptr) {
}

void clear_ptr_tags() {
map_clear(tags);
init_map(tags_map, tags_map_size);
}
4 changes: 2 additions & 2 deletions eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -701,11 +701,11 @@ bool get_arity(const cell_t *p, csize_t *in, csize_t *out, cell_t *module) {
return true;
}

static struct mmfile files[16] = {};
STATIC_ALLOC(files, struct mmfile, 16);
size_t files_cnt = 0;

bool load_file(int dirfd, const char *path) {
if(files_cnt >= LENGTH(files)) {
if(files_cnt >= files_size) {
if(!quiet) printf("Can't load any more files.\n");
return false;
}
Expand Down
9 changes: 5 additions & 4 deletions gen/cgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "startle/test.h"
#include "startle/support.h"
#include "startle/map.h"
#include "startle/static_alloc.h"

#include "cells.h"
#include "rt.h"
Expand All @@ -39,7 +40,7 @@
#include "ir/trace.h"
#include "var.h"

static uintptr_t assert_set[67];
STATIC_ALLOC(assert_set, uintptr_t, 67);

// table of corresponding C types
static
Expand Down Expand Up @@ -267,7 +268,7 @@ bool last_call(const tcell_t *e, const tcell_t *c) {
FOR_TRACE_CONST(p, e, c - e) {
if(p->op == OP_assert &&
set_member(cgen_index(e, p->expr.arg[1]),
assert_set, LENGTH(assert_set))) {
assert_set, assert_set_size)) {
continue;
}
if(!gen_skip(p)) {
Expand Down Expand Up @@ -536,7 +537,7 @@ void gen_assert(const tcell_t *e, const tcell_t *c, int depth) {
const tcell_t *ret = NULL;
const tcell_t *end = e + trace_entry_size(e);

if(!set_insert(iq, assert_set, LENGTH(assert_set))) {
if(!set_insert(iq, assert_set, assert_set_size)) {
FOR_TRACE_CONST(p, e, closure_next_const(c) - e) {
if(!ret && trace_type(p) == T_RETURN) {
ret = p;
Expand Down Expand Up @@ -577,7 +578,7 @@ bool external_includes(const tcell_t *e) {

void gen_function(tcell_t *e) {
e->op = OP_value;
zero(assert_set);
static_zero(assert_set);

gen_function_signature(e);
printf("\n{\n");
Expand Down
57 changes: 31 additions & 26 deletions gen/vlgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "startle/test.h"
#include "startle/support.h"
#include "startle/map.h"
#include "startle/static_alloc.h"

#include "cells.h"
#include "rt.h"
Expand All @@ -42,7 +43,9 @@
#include "var.h"
#include "ir/analysis.h"

#define MAX_DEGREE 31
// size must be a prime number
STATIC_ALLOC(vl_set, uintptr_t, 31);
STATIC_ALLOC_DEPENDENT(vl_set_final, uintptr_t, vl_set_size);

// table of corresponding Verilog types
static
Expand Down Expand Up @@ -337,28 +340,29 @@ void find_sync_inputs(const tcell_t *e, const tcell_t *c, uintptr_t *set, size_t
}

// synchronize the minimum set of inputs
// TODO document
static
void gen_sync_disjoint_inputs(const tcell_t *e, const tcell_t *c) {
uintptr_t set[MAX_DEGREE] = {0};
uintptr_t input_set[MAX_DEGREE] = {0};
LOG("gen_sync_disjoint_inputs %E %d", e, c-e);
find_sync_inputs(e, c, set, LENGTH(set), 1);
FOREACH(i, set) {
if(INRANGE(set[i], 1, e->entry.len)) {
find_sync_inputs(e, &e[set[i]], input_set, LENGTH(input_set), 1);
static_zero(vl_set);
static_zero(vl_set_final);
find_sync_inputs(e, c, vl_set, vl_set_size, 1);
STATIC_FOREACH(i, vl_set) {
if(INRANGE(vl_set[i], 1, e->entry.len)) {
find_sync_inputs(e, &e[vl_set[i]], vl_set_final, vl_set_final_size, 1);
}
}
FOREACH(i, input_set) {
if(input_set[i]) {
set_remove(input_set[i], set, LENGTH(set));
STATIC_FOREACH(i, vl_set_final) {
if(vl_set_final[i]) {
set_remove(vl_set_final[i], vl_set, vl_set_size);
}
}
size_t inputs = squeeze(set, LENGTH(set));
size_t inputs = squeeze(vl_set, vl_set_size);
if(inputs) {
COUNTUP(i, inputs) {
if(i) printf(" & ");
if(set[i] <= e->entry.len) {
printf("inst%d_out_valid", (int)set[i]);
if(vl_set[i] <= e->entry.len) {
printf("inst%d_out_valid", (int)vl_set[i]);
} else {
if(FLAG(*e, entry, RECURSIVE)) {
printf("active");
Expand Down Expand Up @@ -426,35 +430,36 @@ void find_sync_outputs(const tcell_t *e, const tcell_t *c, uintptr_t *set, size_
}

// synchronize the minimum set of outputs
// TODO document
static
void gen_sync_disjoint_outputs(const tcell_t *e, const tcell_t *c, uintptr_t const *const *backrefs) {
uintptr_t set[MAX_DEGREE] = {0};
uintptr_t output_set[MAX_DEGREE] = {0};
static_zero(vl_set);
static_zero(vl_set_final);
LOG("gen_sync_disjoint_outputs %E %d", e, c-e);
if(c > e) {
find_sync_outputs(e, c, set, LENGTH(set), backrefs);
find_sync_outputs(e, c, vl_set, vl_set_size, backrefs);
} else {
COUNTUP(i, e->entry.in) {
find_sync_outputs(e, &e[i+1], set, LENGTH(set), backrefs);
find_sync_outputs(e, &e[i+1], vl_set, vl_set_size, backrefs);
}
}
FOREACH(i, set) {
if(INRANGE(set[i], 1, e->entry.len)) {
find_sync_outputs(e, &e[set[i]], output_set, LENGTH(output_set), backrefs);
STATIC_FOREACH(i, vl_set) {
if(INRANGE(vl_set[i], 1, e->entry.len)) {
find_sync_outputs(e, &e[vl_set[i]], vl_set_final, vl_set_final_size, backrefs);
}
}
FOREACH(i, output_set) {
if(output_set[i]) {
set_remove(output_set[i], set, LENGTH(set));
STATIC_FOREACH(i, vl_set_final) {
if(vl_set_final[i]) {
set_remove(vl_set_final[i], vl_set, vl_set_size);
}
}
size_t outputs = squeeze(set, LENGTH(set));
size_t outputs = squeeze(vl_set, vl_set_size);
if(outputs) {
bool top = false;
COUNTUP(i, outputs) {
if(i) printf(" & ");
if(!is_return(&e[set[i]])) {
printf("inst%d_in_ready", (int)set[i]);
if(!is_return(&e[vl_set[i]])) {
printf("inst%d_in_ready", (int)vl_set[i]);
} else if(!top) {
printf("out_ready");
top = true;
Expand Down
2 changes: 1 addition & 1 deletion io.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct ring_buffer;

#define WORD_SIZE 4

typedef struct {
typedef struct file {
seg_t name;
struct ring_buffer *buffer;
int descriptor;
Expand Down
2 changes: 1 addition & 1 deletion poprc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ OUT=`./eval -ident $1`
DIR=poprc_out

BUILDDIR="build/clang/release-with-asserts"
RT="${BUILDDIR}/cgen/primitives.o ${BUILDDIR}/io.o ${BUILDDIR}/startle/support.o ${BUILDDIR}/startle/error.o ${BUILDDIR}/startle/log.o"
RT="${BUILDDIR}/cgen/primitives.o ${BUILDDIR}/io.o ${BUILDDIR}/startle/support.o ${BUILDDIR}/startle/error.o ${BUILDDIR}/startle/static_alloc.o ${BUILDDIR}/startle/log.o"

make -s .gen/cgen/primitives.h
make -s ${RT} CC=clang BUILD=release-with-asserts
Expand Down
13 changes: 3 additions & 10 deletions primitive/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "startle/error.h"
#include "startle/support.h"
#include "startle/log.h"
#include "startle/static_alloc.h"

#include "cells.h"
#include "rt.h"
Expand Down Expand Up @@ -212,15 +213,7 @@ static MAP(arrays, 1 << 16);

val_t next_array_id = 1;

typedef struct mmap_array {
uintptr_t id;
size_t size;
unsigned int width;
file_t *file;
char *data;
} mmap_array_t;

static mmap_array_t mmap_array[8];
STATIC_ALLOC(mmap_array, mmap_array_t, 8);
static unsigned int mmap_array_count = 0;

void array_init() {
Expand All @@ -240,7 +233,7 @@ mmap_array_t *lookup_mmap_array(uintptr_t arr) {

static
mmap_array_t *new_mmap_array(file_t *file, void *addr, int size, int width) {
if(mmap_array_count >= LENGTH(mmap_array)) return NULL;
if(mmap_array_count >= mmap_array_size) return NULL;
if(width < 1) width = 1;
if(size < 0) size = 0;
mmap_array_t *ma = &mmap_array[mmap_array_count++];
Expand Down
4 changes: 2 additions & 2 deletions rt.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ STATIC_ALLOC(watched_cells, cell_t *, 4);
static op watched_op = OP_null;
bool watch_enabled = false;

seg_t fail_location[64];
STATIC_ALLOC(fail_location, seg_t, 64);
size_t fail_location_n = 0;

#if INTERFACE
Expand Down Expand Up @@ -307,7 +307,7 @@ cell_t *fill_incomplete(cell_t *c) {

void log_fail(seg_t src) {
if(src.s &&
fail_location_n < LENGTH(fail_location)) {
fail_location_n < fail_location_size) {
fail_location[fail_location_n++] = src;
}
}
Expand Down
9 changes: 9 additions & 0 deletions rt_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,13 @@ ASSERT_ALIAS(tcell_t, c.n, n);
ASSERT_ALIAS(tcell_t, c.size, size);
ASSERT_ALIAS(tcell_t, c.expr, expr);

struct file;
typedef struct mmap_array {
uintptr_t id;
size_t size;
unsigned int width;
struct file *file;
char *data;
} mmap_array_t;

#endif
7 changes: 4 additions & 3 deletions startle/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "startle/macros.h"
#include "startle/error.h"
#include "startle/log.h"
#include "startle/static_alloc.h"

static bool breakpoint_disabled = false;

Expand Down Expand Up @@ -255,13 +256,13 @@ void breakpoint() {
}
}

static unsigned int counters[8] = {0};
STATIC_ALLOC(counters, unsigned int, 8);
static unsigned int counters_n = 0;

unsigned int *alloc_counter() {
assert_error(counters_n < LENGTH(counters));
assert_error(counters_n < counters_size);
return &counters[counters_n++];
}
void reset_counters() {
zero(counters);
static_zero(counters);
}
Loading

0 comments on commit 84830d0

Please sign in to comment.