Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Move block and IRs memory pool to rv #246

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,17 +289,13 @@ static inline uint32_t hash(size_t k)
return k;
}

static void block_translate(riscv_t *rv, block_map_t *map, block_t *block);
/* allocate a basic block */
static block_t *block_alloc(riscv_t *rv, block_map_t *map)
static block_t *block_alloc(riscv_t *rv)
{
block_t *block = mpool_alloc(map->block_mp);
block_t *block = mpool_alloc(rv->block_mp);
assert(block);
block->n_insn = 0;
block->predict = NULL;

/* Initialize remaining part of block_t */
block_translate(rv, map, block);
return block;
}

Expand Down Expand Up @@ -608,12 +604,12 @@ FORCE_INLINE bool insn_is_unconditional_branch(uint8_t opcode)
return false;
}

static void block_translate(riscv_t *rv, block_map_t *map, block_t *block)
static void block_translate(riscv_t *rv, block_t *block)
{
block->pc_start = block->pc_end = rv->PC;

rv_insn_t *prev_ir = NULL;
rv_insn_t *ir = mpool_alloc(map->block_ir_mp);
rv_insn_t *ir = mpool_alloc(rv->block_ir_mp);
block->ir_head = ir;

/* translate the basic block */
Expand Down Expand Up @@ -642,7 +638,7 @@ static void block_translate(riscv_t *rv, block_map_t *map, block_t *block)
if (insn_is_branch(ir->opcode))
break;

ir = mpool_alloc(map->block_ir_mp);
ir = mpool_alloc(rv->block_ir_mp);
}

assert(prev_ir);
Expand Down Expand Up @@ -725,7 +721,7 @@ FORCE_INLINE void remove_next_nth_ir(riscv_t *rv,
for (uint8_t i = 0; i < n; i++) {
rv_insn_t *next = ir->next;
ir->next = ir->next->next;
mpool_free(rv->block_map.block_ir_mp, next);
mpool_free(rv->block_ir_mp, next);
}
if (!ir->next) {
block->ir_tail = ir;
Expand Down Expand Up @@ -949,12 +945,13 @@ static block_t *block_find_or_translate(riscv_t *rv)

if (!next) {
if (map->size * 1.25 > map->block_capacity) {
block_map_clear(map);
block_map_clear(rv);
prev = NULL;
}

/* allocate a new block */
next = block_alloc(rv, map);
next = block_alloc(rv);
block_translate(rv, next);

if (!libc_substitute(rv, next)) {
optimize_constant(rv, next);
Expand Down
31 changes: 16 additions & 15 deletions src/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,12 @@ static void block_map_init(block_map_t *map, const uint8_t bits)
map->block_capacity = 1 << bits;
map->size = 0;
map->map = calloc(map->block_capacity, sizeof(struct block *));

map->block_mp = mpool_create(sizeof(block_t) << BLOCK_MAP_CAPACITY_BITS,
sizeof(block_t));
map->block_ir_mp = mpool_create(
sizeof(rv_insn_t) << BLOCK_IR_MAP_CAPACITY_BITS, sizeof(rv_insn_t));
}

/* clear all block in the block map */
void block_map_clear(block_map_t *map)
void block_map_clear(riscv_t *rv)
{
assert(map);
block_map_t *map = &rv->block_map;
for (uint32_t i = 0; i < map->block_capacity; i++) {
block_t *block = map->map[i];
if (!block)
Expand All @@ -41,21 +36,21 @@ void block_map_clear(block_map_t *map)
idx++, ir = next) {
free(ir->fuse);
next = ir->next;
mpool_free(map->block_ir_mp, ir);
mpool_free(rv->block_ir_mp, ir);
}
mpool_free(map->block_mp, block);
mpool_free(rv->block_mp, block);
map->map[i] = NULL;
}
map->size = 0;
}

static void block_map_destroy(block_map_t *map)
static void block_map_destroy(riscv_t *rv)
{
block_map_clear(map);
free(map->map);
block_map_clear(rv);
free(rv->block_map.map);

mpool_destroy(map->block_mp);
mpool_destroy(map->block_ir_mp);
mpool_destroy(rv->block_mp);
mpool_destroy(rv->block_ir_mp);
}

riscv_user_t rv_userdata(riscv_t *rv)
Expand Down Expand Up @@ -118,6 +113,12 @@ riscv_t *rv_create(const riscv_io_t *io,

rv->output_exit_code = output_exit_code;

/* create block and IRs memory pool */
rv->block_mp = mpool_create(sizeof(block_t) << BLOCK_MAP_CAPACITY_BITS,
sizeof(block_t));
rv->block_ir_mp = mpool_create(
sizeof(rv_insn_t) << BLOCK_IR_MAP_CAPACITY_BITS, sizeof(rv_insn_t));

/* initialize the block map */
block_map_init(&rv->block_map, 10);

Expand Down Expand Up @@ -145,7 +146,7 @@ bool rv_enables_to_output_exit_code(riscv_t *rv)
void rv_delete(riscv_t *rv)
{
assert(rv);
block_map_destroy(&rv->block_map);
block_map_destroy(rv);
free(rv);
}

Expand Down
6 changes: 2 additions & 4 deletions src/riscv_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,10 @@ typedef struct {
uint32_t block_capacity; /**< max number of entries in the block map */
uint32_t size; /**< number of entries currently in the map */
block_t **map; /**< block map */

struct mpool *block_mp, *block_ir_mp;
} block_map_t;

/* clear all block in the block map */
void block_map_clear(block_map_t *map);
void block_map_clear(riscv_t *rv);

struct riscv_internal {
bool halt; /* indicate whether the core is halted */
Expand Down Expand Up @@ -123,7 +121,7 @@ struct riscv_internal {

bool compressed; /**< current instruction is compressed or not */
block_map_t block_map; /**< basic block map */

struct mpool *block_mp, *block_ir_mp;
/* print exit code on syscall_exit */
bool output_exit_code;
};
Expand Down