Skip to content

Introduce basic block #91

New issue

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

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

Already on GitHub? Sign in to your account

Merged
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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ all: $(BIN)
OBJS := \
map.o \
utils.o \
decode.o \
emulate.o \
decode.o \
riscv.o \
io.o \
elf.o \
main.o \
Expand Down
31 changes: 23 additions & 8 deletions src/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include "decode.h"
#include "riscv_private.h"

/* decode rd field
* rd = insn[11:7]
Expand Down Expand Up @@ -1653,19 +1654,18 @@ static inline bool op_cbnez(struct rv_insn_t *ir, const uint32_t insn)
#define op_cfsd OP_UNIMP

/* handler for all unimplemented opcodes */
static inline bool op_unimp(struct rv_insn_t *rv_insn UNUSED,
uint32_t insn UNUSED)
static inline bool op_unimp(struct rv_insn_t *ir UNUSED, uint32_t insn UNUSED)
{
return false;
}

/* RV32 decode handler type */
typedef bool (*decode_t)(struct rv_insn_t *rv_insn, uint32_t insn);
typedef bool (*decode_t)(struct rv_insn_t *ir, uint32_t insn);

/* decode RISC-V instruction */
bool rv_decode(struct rv_insn_t *ir, uint32_t insn, uint8_t *insn_len)
bool rv_decode(struct rv_insn_t *ir, uint32_t insn)
{
assert(ir && insn_len);
assert(ir);

#define OP_UNIMP op_unimp
#define OP(insn) op_##insn
Expand Down Expand Up @@ -1704,7 +1704,7 @@ bool rv_decode(struct rv_insn_t *ir, uint32_t insn, uint8_t *insn_len)
if ((insn & FC_OPCODE) != 3) {
insn &= 0x0000FFFF;
const uint16_t c_index = (insn & FC_FUNC3) >> 11 | (insn & FC_OPCODE);
*insn_len = INSN_16;
ir->insn_len = INSN_16;

/* decode instruction (compressed instructions) */
const decode_t op = rvc_jump_table[c_index];
Expand All @@ -1715,7 +1715,7 @@ bool rv_decode(struct rv_insn_t *ir, uint32_t insn, uint8_t *insn_len)

/* standard uncompressed instruction */
const uint32_t index = (insn & INSN_6_2) >> 2;
*insn_len = INSN_32;
ir->insn_len = INSN_32;

/* decode instruction */
const decode_t op = rv_jump_table[index];
Expand All @@ -1725,3 +1725,18 @@ bool rv_decode(struct rv_insn_t *ir, uint32_t insn, uint8_t *insn_len)
#undef OP_UNIMP
#undef OP
}

/* clear all block in the block map */
void block_map_clear(struct block_map *map)
{
assert(map);
for (uint32_t i = 0; i < map->block_capacity; i++) {
struct block *block = map->map[i];
if (block) {
free(block->ir);
free(block);
map->map[i] = NULL;
}
}
map->size = 0;
}
42 changes: 28 additions & 14 deletions src/decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ enum {
/* clang-format on */

enum {
INSN_UNKNOWN = 0,
INSN_16 = 2,
INSN_32 = 4,
};
Expand All @@ -236,21 +235,36 @@ struct rv_insn_t {
#if RV32_HAS(EXT_C)
uint8_t shamt;
#endif

/* instruction length */
uint8_t insn_len;
};

/* translated basic block */
struct block {
/* number of instructions encompased */
uint32_t insn_number;
/* address range of the basic block */
uint32_t pc_start, pc_end;
/* maximum of instructions encompased */
uint32_t insn_capacity;
/* block predictoin */
struct block *predict;
/* memory blocks */
struct rv_insn_t *ir;
};

/* sign extend a 16 bit value */
static inline uint32_t sign_extend_h(const uint32_t x)
{
return (int32_t) ((int16_t) x);
}
struct block_map {
/* max number of entries in the block map */
uint32_t block_capacity;
/* number of entries currently in the map */
uint32_t size;
/* block map */
struct block **map;
};

/* sign extend an 8 bit value */
static inline uint32_t sign_extend_b(const uint32_t x)
{
return (int32_t) ((int8_t) x);
}
/* clear all block in the block map */
void block_map_clear(struct block_map *map);

/* decode the RISC-V instruction */
bool rv_decode(struct rv_insn_t *rv_insn,
const uint32_t insn,
uint8_t *insn_len);
bool rv_decode(struct rv_insn_t *ir, const uint32_t insn);
Loading