mie: ctx: implementing registering dialects and type-instances
This commit is contained in:
109
mie/ctx.c
109
mie/ctx.c
@@ -2,10 +2,20 @@
|
|||||||
#include <blue/ds/list.h>
|
#include <blue/ds/list.h>
|
||||||
#include <blue/ds/string.h>
|
#include <blue/ds/string.h>
|
||||||
#include <mie/ctx.h>
|
#include <mie/ctx.h>
|
||||||
// #include <mie/ir/const.h>
|
#include <mie/dialect/dialect.h>
|
||||||
|
#include <mie/dialect/type.h>
|
||||||
|
#include <mie/ir/op.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#define DIALECT_NS_ID \
|
||||||
|
MIE_ID(0xa1, 0x35, 0x01, 0x1b, 0xe4, 0x71, 0x46, 0x08, 0x92, 0xd4, \
|
||||||
|
0xe6, 0x5a, 0x40, 0xba, 0x7f, 0xee)
|
||||||
|
|
||||||
|
#define TYPE_NS_ID \
|
||||||
|
MIE_ID(0xf5, 0x4e, 0xc5, 0x8c, 0xc0, 0x1e, 0x48, 0x47, 0xb5, 0xf4, \
|
||||||
|
0x7b, 0xb9, 0x6b, 0x47, 0xca, 0x48)
|
||||||
|
|
||||||
struct ctx_int_cache_entry {
|
struct ctx_int_cache_entry {
|
||||||
b_btree_node i_node;
|
b_btree_node i_node;
|
||||||
// struct mie_type i_type;
|
// struct mie_type i_type;
|
||||||
@@ -284,3 +294,100 @@ struct mie_value *mie_ctx_create_array(struct mie_ctx *ctx)
|
|||||||
return MIE_VALUE(array);
|
return MIE_VALUE(array);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
struct mie_ctx *mie_ctx_create(void)
|
||||||
|
{
|
||||||
|
struct mie_ctx *out = malloc(sizeof *out);
|
||||||
|
|
||||||
|
if (!out) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(out, 0x0, sizeof *out);
|
||||||
|
|
||||||
|
mie_id dialects_ns = DIALECT_NS_ID;
|
||||||
|
mie_id_map_init(&out->ctx_dialects, &dialects_ns);
|
||||||
|
|
||||||
|
mie_id types_ns = TYPE_NS_ID;
|
||||||
|
mie_id_map_init(&out->ctx_types, &types_ns);
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mie_ctx_resolve_op(const struct mie_ctx *ctx, struct mie_op *op)
|
||||||
|
{
|
||||||
|
if (op->op_flags & MIE_OP_F_RESOLVED) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *dialect_name = NULL, *op_name = NULL;
|
||||||
|
|
||||||
|
char *dot = strchr(op->op_name, '.');
|
||||||
|
if (dot) {
|
||||||
|
*dot = 0;
|
||||||
|
dialect_name = op->op_name;
|
||||||
|
op_name = dot + 1;
|
||||||
|
} else {
|
||||||
|
dialect_name = NULL;
|
||||||
|
op_name = op->op_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
const struct mie_dialect *dialect = mie_ctx_get_dialect(ctx, dialect_name);
|
||||||
|
if (dot) {
|
||||||
|
*dot = '.';
|
||||||
|
}
|
||||||
|
|
||||||
|
/* dialect_name is no longer valid after this point */
|
||||||
|
dialect_name = NULL;
|
||||||
|
|
||||||
|
if (!dialect) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const struct mie_dialect_op *op_info
|
||||||
|
= mie_dialect_get_op(dialect, op_name);
|
||||||
|
if (!op) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
op->op_dialect = dialect;
|
||||||
|
op->op_info = op_info;
|
||||||
|
|
||||||
|
free(op->op_name);
|
||||||
|
op->op_name = NULL;
|
||||||
|
|
||||||
|
op->op_flags |= MIE_OP_F_RESOLVED;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_dialect *mie_ctx_get_dialect(const struct mie_ctx *ctx, const char *name)
|
||||||
|
{
|
||||||
|
b_rope name_rope = B_ROPE_CSTR(name);
|
||||||
|
mie_id id;
|
||||||
|
mie_id_init_ns(&id, mie_id_map_get_ns(&ctx->ctx_dialects), &name_rope);
|
||||||
|
|
||||||
|
mie_id *target = mie_id_map_get(&ctx->ctx_dialects, &id);
|
||||||
|
return b_unbox(struct mie_dialect, target, d_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_dialect_type *mie_ctx_get_dialect_type(
|
||||||
|
const struct mie_ctx *ctx, const char *dialect_name, const char *type_name)
|
||||||
|
{
|
||||||
|
b_rope dialect_name_rope = B_ROPE_CSTR(dialect_name);
|
||||||
|
mie_id id;
|
||||||
|
mie_id_init_ns(
|
||||||
|
&id, mie_id_map_get_ns(&ctx->ctx_dialects), &dialect_name_rope);
|
||||||
|
|
||||||
|
mie_id *target = mie_id_map_get(&ctx->ctx_dialects, &id);
|
||||||
|
struct mie_dialect *dialect = b_unbox(struct mie_dialect, target, d_id);
|
||||||
|
if (!dialect) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_rope type_name_rope = B_ROPE_CSTR(type_name);
|
||||||
|
mie_id_init_ns(&id, mie_id_map_get_ns(&dialect->d_types), &type_name_rope);
|
||||||
|
target = mie_id_map_get(&dialect->d_types, &id);
|
||||||
|
|
||||||
|
return b_unbox(struct mie_dialect_type, target, ty_id);
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
#include <blue/core/btree.h>
|
#include <blue/core/btree.h>
|
||||||
#include <blue/ds/hashmap.h>
|
#include <blue/ds/hashmap.h>
|
||||||
// #include <mie/type.h>
|
#include <mie/id.h>
|
||||||
|
|
||||||
|
struct mie_op;
|
||||||
|
|
||||||
struct mie_ctx {
|
struct mie_ctx {
|
||||||
#if 0
|
#if 0
|
||||||
@@ -13,24 +15,32 @@ struct mie_ctx {
|
|||||||
b_hashmap *ctx_sel_cache;
|
b_hashmap *ctx_sel_cache;
|
||||||
b_hashmap *ctx_string_cache;
|
b_hashmap *ctx_string_cache;
|
||||||
#endif
|
#endif
|
||||||
|
struct mie_id_map ctx_dialects;
|
||||||
|
struct mie_id_map ctx_types;
|
||||||
b_btree ctx_int_cache;
|
b_btree ctx_int_cache;
|
||||||
|
b_btree ctx_float_cache;
|
||||||
|
b_btree ctx_index_cache;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct mie_ctx *mie_ctx_create(void);
|
MIE_API struct mie_ctx *mie_ctx_create(void);
|
||||||
extern void mie_ctx_destroy(struct mie_ctx *ctx);
|
MIE_API void mie_ctx_destroy(struct mie_ctx *ctx);
|
||||||
|
|
||||||
#if 0
|
MIE_API bool mie_ctx_resolve_op(const struct mie_ctx *ctx, struct mie_op *op);
|
||||||
extern struct mie_type *mie_ctx_get_type(
|
|
||||||
struct mie_ctx *ctx, enum mie_type_id type_id);
|
MIE_API struct mie_dialect *mie_ctx_get_dialect(
|
||||||
#endif
|
const struct mie_ctx *ctx, const char *name);
|
||||||
extern struct mie_type *mie_ctx_get_int_type(
|
MIE_API struct mie_dialect_type *mie_ctx_get_dialect_type(
|
||||||
|
const struct mie_ctx *ctx, const char *dialect_name, const char *type_name);
|
||||||
|
|
||||||
|
MIE_API struct mie_type *mie_ctx_get_int_type(
|
||||||
struct mie_ctx *ctx, unsigned int nr_bits);
|
struct mie_ctx *ctx, unsigned int nr_bits);
|
||||||
extern struct mie_value *mie_ctx_get_null(struct mie_ctx *ctx);
|
MIE_API struct mie_type *mie_ctx_get_float_type(
|
||||||
extern struct mie_value *mie_ctx_get_bool(struct mie_ctx *ctx, bool val);
|
struct mie_ctx *ctx, unsigned int nr_bits);
|
||||||
extern struct mie_value *mie_ctx_get_int(
|
MIE_API struct mie_value *mie_ctx_get_null(struct mie_ctx *ctx);
|
||||||
|
MIE_API struct mie_value *mie_ctx_get_int(
|
||||||
struct mie_ctx *ctx, long long val, unsigned int nr_bits);
|
struct mie_ctx *ctx, long long val, unsigned int nr_bits);
|
||||||
extern struct mie_value *mie_ctx_get_string(struct mie_ctx *ctx, const char *s);
|
MIE_API struct mie_value *mie_ctx_get_float(
|
||||||
extern struct mie_value *mie_ctx_get_selector(struct mie_ctx *ctx, const char *sel);
|
struct mie_ctx *ctx, double val, unsigned int nr_bits);
|
||||||
extern struct mie_value *mie_ctx_create_array(struct mie_ctx *ctx);
|
MIE_API struct mie_value *mie_ctx_get_index(struct mie_ctx *ctx, size_t val);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user