mie: implement more ir building functionality

This commit is contained in:
2025-04-13 18:34:02 +01:00
parent 7f0d8b87c5
commit deb1232bf9
21 changed files with 694 additions and 132 deletions

13
mie/include/mie/arg.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef MIE_ARG_H_
#define MIE_ARG_H_
#include <mie/value.h>
#define MIE_ARG(p) ((struct mie_arg *)(p))
struct mie_arg {
struct mie_value arg_base;
struct mie_type *arg_type;
};
#endif

View File

@@ -1,10 +1,13 @@
#ifndef MIE_BLOCK_H_
#define MIE_BLOCK_H_
#define MIE_BLOCK(p) ((struct mie_block *)(p))
#include <mie/value.h>
struct mie_block {
struct mie_value b_base;
struct mie_func *b_parent;
/* the phi instruction(s). these must appear at the start of the block
* and are separated to make traversing the CFG easier */
b_queue b_phi;
@@ -15,4 +18,8 @@ struct mie_block {
struct mie_instr *b_terminator;
};
MIE_API struct mie_block *mie_block_create(
struct mie_func *parent, const char *name);
MIE_API bool mie_block_add_instr(struct mie_block *block, struct mie_instr *instr);
#endif

View File

@@ -6,7 +6,7 @@
struct mie_branch {
struct mie_instr b_base;
struct mie_label *b_dest;
struct mie_block *b_dest;
};
struct mie_branch_if {

View File

@@ -4,6 +4,7 @@
#include <blue/core/btree.h>
#include <mie/misc.h>
#include <mie/switch.h>
#include <mie/type.h>
#include <mie/value.h>
struct b_dict;
@@ -15,22 +16,28 @@ struct mie_type;
struct mie_phi;
struct mie_ctx {
struct mie_int *ctx_true, *ctx_false;
b_btree ctx_ints;
struct mie_const *ctx_true, *ctx_false;
struct mie_type *ctx_types[__MIE_TYPE_COUNT];
b_btree ctx_int_cache;
};
struct mie_builder {
struct mie_ctx *b_ctx;
struct mie_module *b_module;
struct mie_instr *b_prev_instr;
struct mie_block *b_current_block;
struct mie_instr *b_prev_instr;
};
extern struct mie_ctx *mie_ctx_create(void);
extern void mie_ctx_destroy(struct mie_ctx *ctx);
extern struct mie_type *mie_ctx_get_type(
struct mie_ctx *ctx, enum mie_type_id type_id);
extern struct mie_type *mie_ctx_get_int_type(
struct mie_ctx *ctx, unsigned int nr_bits);
extern struct mie_value *mie_ctx_get_bool(struct mie_ctx *ctx, bool val);
extern struct mie_value *mie_ctx_get_int(struct mie_ctx *ctx, long long val);
extern struct mie_value *mie_ctx_get_int(
struct mie_ctx *ctx, long long val, unsigned int nr_bits);
extern struct mie_builder *mie_builder_create(
struct mie_ctx *ctx, struct mie_module *mod);
@@ -58,7 +65,7 @@ extern struct mie_value *mie_builder_div(
struct mie_builder *builder, struct mie_value *left,
struct mie_value *right, const char *name);
extern struct mie_value *mie_builder_load(
struct mie_builder *builder, struct mie_value *type,
struct mie_builder *builder, struct mie_type *type,
struct mie_value *src, const char *name);
extern struct mie_value *mie_builder_store(
struct mie_builder *builder, struct mie_value *val, struct mie_value *dest);

View File

@@ -3,54 +3,18 @@
#include <mie/value.h>
enum mie_const_type {
MIE_CONST_NONE = 0,
MIE_CONST_INT,
MIE_CONST_FLOAT,
MIE_CONST_STRING,
MIE_CONST_ATOM,
MIE_CONST_LABEL,
MIE_CONST_ARRAY,
};
#define MIE_CONST(p) ((struct mie_const *)(p))
struct mie_const {
struct mie_value c_base;
enum mie_const_type c_type;
};
struct mie_type *c_type;
struct mie_int {
struct mie_const i_base;
unsigned short i_width;
long long i_value;
union {
int64_t v_int;
double v_float;
char *v_str;
char *v_atom;
} c_v;
};
struct mie_float {
struct mie_const f_base;
double f_value;
};
struct mie_str {
struct mie_const s_base;
char *s_value;
};
struct mie_atom {
struct mie_const s_base;
char *s_value;
};
struct mie_label {
struct mie_const l_base;
char *l_value;
};
struct mie_array {
struct mie_const a_base;
unsigned int a_nr_values;
struct mie_value **a_values;
};
extern void mie_int_init(struct mie_int *out, long long val, unsigned int nr_bits);
extern struct mie_int *mie_int_create(long long val, unsigned int nr_bits);
#endif

View File

@@ -1,10 +1,14 @@
#ifndef MIE_FUNC_H_
#define MIE_FUNC_H_
#define MIE_FUNC(p) ((struct mie_func *)(p))
#include <mie/value.h>
struct mie_name_map;
struct mie_type;
struct mie_arg;
struct mie_block;
struct b_dict;
enum mie_func_type {
@@ -17,11 +21,11 @@ enum mie_func_type {
struct mie_func {
struct mie_value f_base;
struct b_dict *f_named_values;
enum mie_func_type f_type;
struct mie_type *f_ret;
struct mie_name_map *f_names;
b_queue f_args;
b_queue f_blocks;
};
@@ -29,7 +33,11 @@ struct mie_func {
extern struct mie_func *mie_func_create(
const char *name, enum mie_func_type type, struct mie_type *ret_type,
struct mie_arg **args, unsigned int nr_args);
extern char *mie_func_generate_value_name(
struct mie_func *func, const char *name_hint);
extern struct mie_block *mie_func_create_block(
struct mie_func *func, const char *name);
extern void mie_func_insert_block(
struct mie_func *func, struct mie_block *block, struct mie_block *after);
extern struct mie_value *mie_func_generate_value_name(
struct mie_func *func, struct mie_value *val, const char *hint);
#endif

View File

@@ -3,6 +3,8 @@
#include <mie/value.h>
#define MIE_INSTR(p) ((struct mie_instr *)(p))
enum mie_instr_type {
MIE_INSTR_NONE = 0,
MIE_INSTR_RET,

View File

@@ -1,9 +1,13 @@
#ifndef MIE_MODULE_H_
#define MIE_MODULE_H_
#define MIE_MODULE(p) ((struct mie_module *)(p))
#include <blue/core/queue.h>
#include <mie/value.h>
struct mie_func;
struct mie_module {
struct mie_value m_base;
b_queue m_records;
@@ -12,4 +16,7 @@ struct mie_module {
b_queue m_func;
};
extern struct mie_module *mie_module_create(void);
extern void mie_module_add_function(struct mie_module *mod, struct mie_func *func);
#endif

View File

@@ -1,6 +1,8 @@
#ifndef MIE_RECORD_H_
#define MIE_RECORD_H_
#define MIE_RECORD(p) ((struct mie_record *)(p))
#include <mie/const.h>
#include <mie/value.h>

View File

@@ -1,15 +1,21 @@
#ifndef MIE_TYPE_H_
#define MIE_TYPE_H_
#define MIE_TYPE(p) ((struct mie_type *)(p))
#include <mie/value.h>
enum mie_type_id {
MIE_TYPE_VOID = 0x00u,
MIE_TYPE_INT = 0x01u,
MIE_TYPE_ID = 0x02u,
MIE_TYPE_STR = 0x03u,
MIE_TYPE_SYM = 0x04u,
MIE_TYPE_CLASS = 0x05u,
MIE_TYPE_PTR = 0x02u,
MIE_TYPE_ID = 0x03u,
MIE_TYPE_STR = 0x04u,
MIE_TYPE_ATOM = 0x05u,
MIE_TYPE_CLASS = 0x06u,
MIE_TYPE_ARRAY = 0x08u,
MIE_TYPE_LABEL = 0x09u,
__MIE_TYPE_COUNT,
};
struct mie_type {

View File

@@ -7,14 +7,18 @@
#define MIE_VALUE(p) ((struct mie_value *)(p))
enum mie_value_type {
struct mie_value;
enum mie_value_type_id {
MIE_VALUE_NONE = 0,
MIE_VALUE_CONST,
MIE_VALUE_MODULE,
MIE_VALUE_TYPE,
MIE_VALUE_RECORD,
MIE_VALUE_FUNC,
MIE_VALUE_ARG,
MIE_VALUE_BLOCK,
MIE_VALUE_INSTR,
MIE_VALUE_OP,
MIE_VALUE_RECORD,
MIE_VALUE_CONST,
};
enum mie_value_flags {
@@ -22,16 +26,31 @@ enum mie_value_flags {
MIE_VALUE_F_STATIC = 0x01u,
};
struct mie_value_type {
enum mie_value_type_id t_id;
struct mie_type *(*t_get_type)(struct mie_value *);
};
struct mie_value {
unsigned int v_ref;
struct mie_name v_name;
enum mie_value_type v_type;
const struct mie_value_type *v_type;
enum mie_value_flags v_flags;
b_queue_entry v_entry;
};
MIE_API void mie_value_init(struct mie_value *val, enum mie_value_type type);
MIE_API struct mie_value *mie_value_retain(struct mie_value *val);
MIE_API void mie_value_release(struct mie_value *val);
MIE_API void mie_value_init(struct mie_value *val, enum mie_value_type_id type);
MIE_API void mie_value_destroy(struct mie_value *val);
MIE_API struct mie_type *mie_value_get_type(struct mie_value *val);
static inline bool mie_value_is(
const struct mie_value *val, enum mie_value_type_id type_id)
{
if (!val->v_type) {
return false;
}
return val->v_type->t_id == type_id;
}
#endif