~lbnz/xr0

c0e6554a7b1a15ae26b6585abe4d3de62275c7d2 — Amisi Kiarie a month ago edd01f8
checkpoint
5 files changed, 47 insertions(+), 18 deletions(-)

M src/object/object.c
M src/state/block.c
M src/state/heap.c
M src/state/heap.h
M src/state/state.c
M src/object/object.c => src/object/object.c +3 -1
@@ 610,7 610,9 @@ range_copy(struct range *r)
static struct range * 
range_permuteheaplocs(struct range *r, struct permutation *p)
{
	assert(false);
	return range_create(
		ast_expr_copy(r->size), location_permuteheap(r->loc, p)
	);
}

static struct int_arr *

M src/state/block.c => src/state/block.c +5 -1
@@ 47,6 47,7 @@ block_copy(struct block *old)
{
	struct block *new = malloc(sizeof(struct block));
	new->arr = object_arr_copy(old->arr);
	new->caller = old->caller;
	return new;
}



@@ 55,6 56,7 @@ block_permuteheaplocs(struct block *old, struct permutation *p)
{
	struct block *new = malloc(sizeof(struct block));
	new->arr = object_arr_create();
	new->caller = old->caller;

	struct object **obj = object_arr_objects(old->arr);
	int n = object_arr_nobjects(old->arr);


@@ 177,7 179,9 @@ block_range_alloc(struct block *b, struct ast_expr *lw, struct ast_expr *up,
					ast_expr_copy(up),
					ast_expr_copy(lw)
				),
				heap_newblock(heap)
				b->caller
					? heap_newcallerblock(heap)
					: heap_newblock(heap)
			)
		)
	);

M src/state/heap.c => src/state/heap.c +36 -12
@@ 2,6 2,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include "ast.h"
#include "block.h"
#include "heap.h"


@@ 65,8 66,14 @@ heap_str(struct heap *h, char *indent)
			continue;
		}
		char *block = block_str(arr[i]);
		strbuilder_printf(b, "%s%d: %s %s%s", indent, i, block,
			printdelim(h, i) ? "\n" : "");
		strbuilder_printf(
			b, "%s%d: %s%s",
			indent, i, (block_iscaller(arr[i]) ? "(c) " : ""),
			block
		);
		if (printdelim(h, i)) {
			strbuilder_printf(b, "\n");
		}
		free(block);
	}
	return strbuilder_build(b);


@@ 102,21 109,38 @@ heap_permute(struct heap *old, struct permutation *p)
	return new;
}

struct int_arr *
heap_deriveorder(struct heap *h, struct circuitbreaker *cb, struct state *s)
{
	struct int_arr *derived = int_arr_create();
/* foundmap: return int map of the given size with the values in the array set
 * to 1, and all the others to 0. */
static int *
foundmap(struct int_arr *, int size);

void
heap_fillorder(struct heap *h, struct int_arr *arr)
{
	int n = block_arr_nblocks(h->blocks);
	struct block **b = block_arr_blocks(h->blocks);
	int *found = foundmap(arr, n);
	for (int i = 0; i < n; i++) {
		struct location *loc = location_create_dynamic(
			i, ast_expr_constant_create(0)
		);
		int_arr_appendrange(derived, location_deriveorder(loc, cb, s));	
		location_destroy(loc);
		if (!found[i]) {
			int_arr_append(arr, i);
		}
	}
	free(found);
}

static int *
foundmap(struct int_arr *arr, int size)
{
	int arr_len = int_arr_len(arr);
	int *arr_v = int_arr_arr(arr);
	assert(arr_len <= size);

	int *m = calloc(size, sizeof(int));
	for (int i = 0; i < arr_len; i++) {
		assert(arr_v[i] < size);
		m[arr_v[i]] = 1;
	}
	return derived;
	return m;
}

struct block_arr *

M src/state/heap.h => src/state/heap.h +2 -2
@@ 55,8 55,8 @@ heap_deallocblock(struct heap *h, int block);
void
heap_undeclare(struct heap *, struct state *);

struct int_arr *
heap_deriveorder(struct heap *, struct circuitbreaker *, struct state *);
void
heap_fillorder(struct heap *, struct int_arr *);

/* TODO: extract to own file */


M src/state/state.c => src/state/state.c +1 -2
@@ 728,9 728,8 @@ deriveorder(struct state *s)
{
	struct circuitbreaker *cb = circuitbreaker_create();
	struct int_arr *arr = value_deriveorder(s->reg, cb, s);
	struct int_arr *derived = heap_deriveorder(s->heap, cb, s);
	int_arr_appendrange(arr, derived);
	circuitbreaker_destroy(cb);
	heap_fillorder(s->heap, arr);
	return arr;
}