Compare commits
27 Commits
archive/le
...
27e42340de
| Author | SHA1 | Date | |
|---|---|---|---|
| 27e42340de | |||
| c686c31ca0 | |||
| 8ad3f36288 | |||
| 0a45e3b571 | |||
| e98ccd0c80 | |||
| 14a1ef30ee | |||
| 344626f207 | |||
| f4d374288f | |||
| 789bd1309f | |||
| 5b06934e85 | |||
| 9d80d94244 | |||
| 3c2ca0f70e | |||
| aad2cad0a8 | |||
| 8b3093411a | |||
| 7c7e5e7af0 | |||
| 9126edfd57 | |||
| 3c9506256d | |||
| add09d4958 | |||
| 7b6ce3bf6e | |||
| 79ab1c175b | |||
| 0dce84cb7f | |||
| 2e22898fc8 | |||
| 86005451cb | |||
| 21bcbb7edc | |||
| 915cf836f2 | |||
| f9a2cf3b8e | |||
| 6573360656 |
@@ -11,5 +11,5 @@ else ()
|
||||
endif ()
|
||||
|
||||
target_include_directories(mie PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include/)
|
||||
target_link_libraries(mie Bluelib::Core Bluelib::Ds)
|
||||
target_link_libraries(mie Bluelib::Core Bluelib::Ds Bluelib::Io)
|
||||
target_compile_definitions(mie PRIVATE MIE_EXPORT=1 MIE_STATIC=${MIE_STATIC})
|
||||
|
||||
248
mie/ctx.c
248
mie/ctx.c
@@ -1,22 +1,41 @@
|
||||
#include <blue/core/bstr.h>
|
||||
#include <blue/ds/hashmap.h>
|
||||
#include <blue/ds/list.h>
|
||||
#include <blue/ds/string.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/ir/const.h>
|
||||
#include <mie/dialect/arith.h>
|
||||
#include <mie/dialect/builtin.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/index.h>
|
||||
#include <mie/dialect/type.h>
|
||||
#include <mie/ir/op.h>
|
||||
#include <mie/type/function.h>
|
||||
#include <mie/type/storage.h>
|
||||
#include <mie/type/type.h>
|
||||
#include <stdlib.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 {
|
||||
b_btree_node i_node;
|
||||
struct mie_type i_type;
|
||||
// struct mie_type i_type;
|
||||
b_btree i_values;
|
||||
};
|
||||
|
||||
struct ctx_int_value_cache_entry {
|
||||
b_btree_node i_node;
|
||||
struct mie_int i_value;
|
||||
// struct mie_int i_value;
|
||||
};
|
||||
|
||||
#if 0
|
||||
|
||||
B_BTREE_DEFINE_SIMPLE_INSERT(
|
||||
struct ctx_int_cache_entry, i_node, i_type.t_width, put_cached_int_type)
|
||||
B_BTREE_DEFINE_SIMPLE_GET(
|
||||
@@ -281,3 +300,226 @@ struct mie_value *mie_ctx_create_array(struct mie_ctx *ctx)
|
||||
|
||||
return MIE_VALUE(array);
|
||||
}
|
||||
#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);
|
||||
|
||||
out->ctx_ints = mie_int_cache_create();
|
||||
out->ctx_indices = mie_index_cache_create();
|
||||
out->ctx_strings = mie_string_cache_create();
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
bool mie_ctx_resolve_op(const struct mie_ctx *ctx, struct mie_op *op)
|
||||
{
|
||||
bool fully_resolved = MIE_TEST_FLAGS(
|
||||
op->op_flags, MIE_OP_F_OP_RESOLVED | MIE_OP_F_ARGS_RESOLVED);
|
||||
|
||||
if (fully_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_OP_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);
|
||||
}
|
||||
|
||||
struct mie_type *mie_ctx_get_type(
|
||||
struct mie_ctx *ctx, const char *dialect_name, const char *type_name)
|
||||
{
|
||||
char full_name[256];
|
||||
snprintf(full_name, sizeof full_name, "%s.%s", dialect_name, dialect_name);
|
||||
b_rope full_name_rope = B_ROPE_CSTR(full_name);
|
||||
|
||||
mie_id id;
|
||||
mie_id_init_ns(&id, mie_id_map_get_ns(&ctx->ctx_types), &full_name_rope);
|
||||
|
||||
mie_id *target = mie_id_map_get(&ctx->ctx_types, &id);
|
||||
struct mie_type *type = b_unbox(struct mie_type, target, ty_id);
|
||||
if (type) {
|
||||
return type;
|
||||
}
|
||||
|
||||
struct mie_dialect_type *type_info
|
||||
= mie_ctx_get_dialect_type(ctx, dialect_name, type_name);
|
||||
if (!type_info /* || (type_info->ty_flags & MIE_DIALECT_TYPE_PARAMETISED) */) {
|
||||
/* cannot initialise unknown or parametised types */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (type_info->ty_data_size < sizeof(struct mie_type)) {
|
||||
/* invalid type info */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
type = malloc(type_info->ty_data_size);
|
||||
if (!type) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(type, 0x0, sizeof *type);
|
||||
|
||||
type->ty_def = type_info;
|
||||
type->ty_name = b_bstr_fmt("%s.%s", dialect_name, type_name);
|
||||
|
||||
if (type_info->ty_init) {
|
||||
type_info->ty_init(type_info, type);
|
||||
}
|
||||
|
||||
mie_id_map_put(&ctx->ctx_types, &type->ty_id, &full_name_rope);
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
struct mie_type *mie_ctx_get_storage_type(
|
||||
struct mie_ctx *ctx, const struct mie_type **parts, size_t nr_parts)
|
||||
{
|
||||
struct mie_id_builder id_builder;
|
||||
mie_id_builder_begin(&id_builder, &ctx->ctx_types.map_ns_id);
|
||||
mie_storage_type_build_id(&id_builder, parts, nr_parts);
|
||||
mie_id id;
|
||||
mie_id_builder_end(&id_builder, &id);
|
||||
|
||||
mie_id *target = mie_id_map_get(&ctx->ctx_types, &id);
|
||||
struct mie_type *type = b_unbox(struct mie_type, target, ty_id);
|
||||
if (type) {
|
||||
return type;
|
||||
}
|
||||
|
||||
struct mie_storage_type *new_type = mie_storage_type_create();
|
||||
for (size_t i = 0; i < nr_parts; i++) {
|
||||
mie_storage_type_add_part(new_type, parts[i]);
|
||||
}
|
||||
|
||||
new_type->st_base.ty_id = id;
|
||||
mie_id_map_put_id(&ctx->ctx_types, &new_type->st_base.ty_id);
|
||||
return (struct mie_type *)new_type;
|
||||
}
|
||||
|
||||
struct mie_type *mie_ctx_get_function_type(
|
||||
struct mie_ctx *ctx, const struct mie_type **in, size_t nr_in,
|
||||
const struct mie_type **out, size_t nr_out)
|
||||
{
|
||||
struct mie_id_builder id_builder;
|
||||
mie_id_builder_begin(&id_builder, &ctx->ctx_types.map_ns_id);
|
||||
mie_function_type_build_id(&id_builder, in, nr_in, out, nr_out);
|
||||
mie_id id;
|
||||
mie_id_builder_end(&id_builder, &id);
|
||||
|
||||
mie_id *target = mie_id_map_get(&ctx->ctx_types, &id);
|
||||
struct mie_type *type = b_unbox(struct mie_type, target, ty_id);
|
||||
if (type) {
|
||||
return type;
|
||||
}
|
||||
|
||||
struct mie_function_type *new_type = mie_function_type_create();
|
||||
for (size_t i = 0; i < nr_in; i++) {
|
||||
mie_function_type_add_in_part(new_type, in[i]);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < nr_out; i++) {
|
||||
mie_function_type_add_out_part(new_type, out[i]);
|
||||
}
|
||||
|
||||
new_type->func_base.ty_id = id;
|
||||
mie_id_map_put_id(&ctx->ctx_types, &new_type->func_base.ty_id);
|
||||
return (struct mie_type *)new_type;
|
||||
}
|
||||
|
||||
struct mie_value *mie_ctx_get_int(struct mie_ctx *ctx, long long val, size_t nr_bits)
|
||||
{
|
||||
return (struct mie_value *)mie_int_cache_get(
|
||||
ctx->ctx_ints, ctx, val, nr_bits);
|
||||
}
|
||||
|
||||
struct mie_value *mie_ctx_get_string(struct mie_ctx *ctx, const char *s)
|
||||
{
|
||||
return (struct mie_value *)mie_string_cache_get(ctx->ctx_strings, ctx, s);
|
||||
}
|
||||
|
||||
struct mie_value *mie_ctx_get_index(struct mie_ctx *ctx, size_t val)
|
||||
{
|
||||
return (struct mie_value *)mie_index_cache_get(ctx->ctx_indices, ctx, val);
|
||||
}
|
||||
|
||||
18
mie/dialect/arith/addf.c
Normal file
18
mie/dialect/arith/addf.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_OP_BEGIN(mie_arith_addf, "addf")
|
||||
MIE_DIALECT_OP_PRINT(print);
|
||||
MIE_DIALECT_OP_PARSE(parse);
|
||||
MIE_DIALECT_OP_END()
|
||||
18
mie/dialect/arith/addi.c
Normal file
18
mie/dialect/arith/addi.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_OP_BEGIN(mie_arith_addi, "addi")
|
||||
MIE_DIALECT_OP_PRINT(print);
|
||||
MIE_DIALECT_OP_PARSE(parse);
|
||||
MIE_DIALECT_OP_END()
|
||||
9
mie/dialect/arith/arith.c
Normal file
9
mie/dialect/arith/arith.c
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_arith, "arith")
|
||||
MIE_DIALECT_ADD_TYPE(mie_arith_int);
|
||||
MIE_DIALECT_ADD_TYPE(mie_arith_float);
|
||||
MIE_DIALECT_ADD_OP(mie_arith_addi);
|
||||
MIE_DIALECT_ADD_OP(mie_arith_addf);
|
||||
MIE_DIALECT_END()
|
||||
89
mie/dialect/arith/float.c
Normal file
89
mie/dialect/arith/float.c
Normal file
@@ -0,0 +1,89 @@
|
||||
#include <blue/core/bstr.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/arith.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/type.h>
|
||||
#include <mie/macros.h>
|
||||
#include <mie/type/type.h>
|
||||
#include <mie/value.h>
|
||||
|
||||
struct float_type {
|
||||
struct mie_type f_base;
|
||||
size_t f_width;
|
||||
};
|
||||
|
||||
static void value_print(
|
||||
const struct mie_type *ty, const struct mie_value *value, b_stream *out)
|
||||
{
|
||||
struct float_type *float_ty = (struct float_type *)ty;
|
||||
struct mie_float *float_val = (struct mie_float *)value;
|
||||
switch (float_ty->f_width) {
|
||||
case MIE_FLOAT_32:
|
||||
b_stream_write_fmt(
|
||||
out, NULL, "%f : f%zu", float_val->f_val.v_32,
|
||||
float_ty->f_width);
|
||||
break;
|
||||
case MIE_FLOAT_64:
|
||||
b_stream_write_fmt(
|
||||
out, NULL, "%lf : f%zu", float_val->f_val.v_64,
|
||||
float_ty->f_width);
|
||||
break;
|
||||
default:
|
||||
b_stream_write_fmt(out, NULL, "NaN : f%zu", float_ty->f_width);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
struct mie_type *mie_arith_float_get_type(struct mie_ctx *ctx, size_t bit_width)
|
||||
{
|
||||
struct mie_dialect_type *type_info
|
||||
= mie_ctx_get_dialect_type(ctx, "arith", "float");
|
||||
if (!type_info) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
b_rope rope_i = B_ROPE_CHAR('f'), rope_width = B_ROPE_UINT(bit_width);
|
||||
b_rope type_name;
|
||||
b_rope_concat(&type_name, &rope_i, &rope_width);
|
||||
|
||||
mie_id id;
|
||||
mie_id_init_ns(&id, mie_id_map_get_ns(&ctx->ctx_types), &type_name);
|
||||
|
||||
mie_id *target = mie_id_map_get(&ctx->ctx_types, &id);
|
||||
if (target) {
|
||||
return b_unbox(struct mie_type, target, ty_id);
|
||||
}
|
||||
|
||||
struct float_type *type = (struct float_type *)mie_type_create(type_info);
|
||||
if (!type) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
type->f_base.ty_name = b_bstr_fmt("arith.float<%zu>", bit_width);
|
||||
type->f_base.ty_instance_size = sizeof(struct mie_float);
|
||||
type->f_width = bit_width;
|
||||
|
||||
mie_id_map_put(&ctx->ctx_types, &type->f_base.ty_id, &type_name);
|
||||
return (struct mie_type *)type;
|
||||
}
|
||||
|
||||
static enum mie_status print(
|
||||
const struct mie_dialect_type *def, const struct mie_type *ty, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
const struct mie_dialect_type *def, struct mie_parser *parser,
|
||||
struct mie_type **out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_TYPE_BEGIN(mie_arith_float, "float")
|
||||
// MIE_DIALECT_TYPE_FLAGS(MIE_DIALECT_TYPE_PARAMETISED);
|
||||
MIE_DIALECT_TYPE_STRUCT(struct float_type);
|
||||
MIE_DIALECT_TYPE_PRINT(print);
|
||||
MIE_DIALECT_TYPE_PARSE(parse);
|
||||
MIE_DIALECT_TYPE_VALUE_PRINT(value_print);
|
||||
MIE_DIALECT_TYPE_END()
|
||||
139
mie/dialect/arith/int-cache.c
Normal file
139
mie/dialect/arith/int-cache.c
Normal file
@@ -0,0 +1,139 @@
|
||||
#include <blue/core/btree.h>
|
||||
#include <mie/dialect/arith.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct int_width_cache_entry {
|
||||
b_btree_node e_node;
|
||||
struct mie_int e_value;
|
||||
};
|
||||
|
||||
struct int_width_cache {
|
||||
b_btree_node c_node;
|
||||
size_t c_width;
|
||||
b_btree c_ints;
|
||||
};
|
||||
|
||||
struct mie_int_cache {
|
||||
b_btree c_widths;
|
||||
};
|
||||
|
||||
static B_BTREE_DEFINE_SIMPLE_INSERT(
|
||||
struct int_width_cache, c_node, c_width, put_width_cache);
|
||||
static B_BTREE_DEFINE_SIMPLE_GET(
|
||||
struct int_width_cache, size_t, c_node, c_width, get_width_cache);
|
||||
|
||||
static B_BTREE_DEFINE_SIMPLE_INSERT(
|
||||
struct int_width_cache_entry, e_node, e_value.i_val.v_small, put_int);
|
||||
static B_BTREE_DEFINE_SIMPLE_GET(
|
||||
struct int_width_cache_entry, uint64_t, e_node, e_value.i_val.v_small,
|
||||
get_int);
|
||||
|
||||
static struct int_width_cache *int_width_cache_create(size_t width)
|
||||
{
|
||||
struct int_width_cache *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
out->c_width = width;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
static void int_width_cache_destroy(struct int_width_cache *cache)
|
||||
{
|
||||
b_btree_node *cur = b_btree_first(&cache->c_ints);
|
||||
while (cur) {
|
||||
b_btree_node *next = b_btree_next(cur);
|
||||
b_btree_delete(&cache->c_ints, cur);
|
||||
|
||||
struct int_width_cache_entry *entry
|
||||
= b_unbox(struct int_width_cache_entry, cur, e_node);
|
||||
free(entry);
|
||||
|
||||
cur = next;
|
||||
}
|
||||
|
||||
free(cache);
|
||||
}
|
||||
|
||||
static struct int_width_cache_entry *int_width_cache_entry_create(
|
||||
struct mie_ctx *ctx, size_t width, size_t value)
|
||||
{
|
||||
struct int_width_cache_entry *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
out->e_value.i_val.v_small = value;
|
||||
out->e_value.i_base.v_type = mie_arith_int_get_type(ctx, width);
|
||||
if (!out->e_value.i_base.v_type) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
struct mie_int_cache *mie_int_cache_create(void)
|
||||
{
|
||||
struct mie_int_cache *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
return out;
|
||||
}
|
||||
|
||||
void mie_int_cache_destroy(struct mie_int_cache *cache)
|
||||
{
|
||||
b_btree_node *cur = b_btree_first(&cache->c_widths);
|
||||
while (cur) {
|
||||
b_btree_node *next = b_btree_next(cur);
|
||||
b_btree_delete(&cache->c_widths, cur);
|
||||
|
||||
struct int_width_cache *width_cache
|
||||
= b_unbox(struct int_width_cache, cur, c_node);
|
||||
int_width_cache_destroy(width_cache);
|
||||
|
||||
cur = next;
|
||||
}
|
||||
|
||||
free(cache);
|
||||
}
|
||||
|
||||
struct mie_int *mie_int_cache_get(
|
||||
struct mie_int_cache *cache, struct mie_ctx *ctx, size_t value,
|
||||
size_t bit_width)
|
||||
{
|
||||
struct int_width_cache *width_cache
|
||||
= get_width_cache(&cache->c_widths, bit_width);
|
||||
if (!width_cache) {
|
||||
width_cache = int_width_cache_create(bit_width);
|
||||
|
||||
if (!width_cache) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
put_width_cache(&cache->c_widths, width_cache);
|
||||
}
|
||||
|
||||
struct int_width_cache_entry *entry = get_int(&width_cache->c_ints, value);
|
||||
if (!entry) {
|
||||
entry = int_width_cache_entry_create(ctx, bit_width, value);
|
||||
|
||||
if (!entry) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
put_int(&width_cache->c_ints, entry);
|
||||
}
|
||||
|
||||
return &entry->e_value;
|
||||
}
|
||||
81
mie/dialect/arith/int.c
Normal file
81
mie/dialect/arith/int.c
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <blue/core/bstr.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/arith.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/type.h>
|
||||
#include <mie/macros.h>
|
||||
#include <mie/type/type.h>
|
||||
#include <mie/value.h>
|
||||
|
||||
struct int_type {
|
||||
struct mie_type i_base;
|
||||
size_t i_width;
|
||||
};
|
||||
|
||||
static void value_print(
|
||||
const struct mie_type *ty, const struct mie_value *value, b_stream *out)
|
||||
{
|
||||
struct int_type *int_ty = (struct int_type *)ty;
|
||||
struct mie_int *int_val = (struct mie_int *)value;
|
||||
if (int_ty->i_width <= 64) {
|
||||
b_stream_write_fmt(
|
||||
out, NULL, "%zu : i%zu", int_val->i_val.v_small,
|
||||
int_ty->i_width);
|
||||
} else {
|
||||
b_stream_write_fmt(out, NULL, "INF : i%zu", int_ty->i_width);
|
||||
}
|
||||
}
|
||||
|
||||
struct mie_type *mie_arith_int_get_type(struct mie_ctx *ctx, size_t bit_width)
|
||||
{
|
||||
struct mie_dialect_type *type_info
|
||||
= mie_ctx_get_dialect_type(ctx, "arith", "int");
|
||||
if (!type_info) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
b_rope rope_i = B_ROPE_CHAR('i'), rope_width = B_ROPE_UINT(bit_width);
|
||||
b_rope type_name;
|
||||
b_rope_concat(&type_name, &rope_i, &rope_width);
|
||||
|
||||
mie_id id;
|
||||
mie_id_init_ns(&id, mie_id_map_get_ns(&ctx->ctx_types), &type_name);
|
||||
|
||||
mie_id *target = mie_id_map_get(&ctx->ctx_types, &id);
|
||||
if (target) {
|
||||
return b_unbox(struct mie_type, target, ty_id);
|
||||
}
|
||||
|
||||
struct int_type *type = (struct int_type *)mie_type_create(type_info);
|
||||
if (!type) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
type->i_base.ty_name = b_bstr_fmt("arith.int<%zu>", bit_width);
|
||||
type->i_base.ty_instance_size = sizeof(struct mie_int);
|
||||
type->i_width = bit_width;
|
||||
|
||||
mie_id_map_put(&ctx->ctx_types, &type->i_base.ty_id, &type_name);
|
||||
return (struct mie_type *)type;
|
||||
}
|
||||
|
||||
static enum mie_status print(
|
||||
const struct mie_dialect_type *def, const struct mie_type *ty, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
const struct mie_dialect_type *def, struct mie_parser *parser,
|
||||
struct mie_type **out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_TYPE_BEGIN(mie_arith_int, "int")
|
||||
// MIE_DIALECT_TYPE_FLAGS(MIE_DIALECT_TYPE_PARAMETISED);
|
||||
MIE_DIALECT_TYPE_STRUCT(struct int_type);
|
||||
MIE_DIALECT_TYPE_PRINT(print);
|
||||
MIE_DIALECT_TYPE_PARSE(parse);
|
||||
MIE_DIALECT_TYPE_VALUE_PRINT(value_print);
|
||||
MIE_DIALECT_TYPE_END()
|
||||
6
mie/dialect/builtin/builtin.c
Normal file
6
mie/dialect/builtin/builtin.c
Normal file
@@ -0,0 +1,6 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_builtin, "builtin")
|
||||
MIE_DIALECT_ADD_TYPE(mie_builtin_string);
|
||||
MIE_DIALECT_END()
|
||||
84
mie/dialect/builtin/string-cache.c
Normal file
84
mie/dialect/builtin/string-cache.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <blue/core/btree.h>
|
||||
#include <blue/ds/hashmap.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/builtin.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct mie_string_cache {
|
||||
b_hashmap *c_entries;
|
||||
};
|
||||
|
||||
static struct mie_string *mie_string_create(
|
||||
struct mie_ctx *ctx, const char *s, size_t len)
|
||||
{
|
||||
struct mie_string *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->str_base.v_type = mie_ctx_get_type(ctx, "builtin", "string");
|
||||
if (!out->str_base.v_type) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->str_val = b_strdup(s);
|
||||
if (!out->str_val) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->str_len = len;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
static void mie_string_destroy(struct mie_string *str)
|
||||
{
|
||||
}
|
||||
|
||||
struct mie_string_cache *mie_string_cache_create(void)
|
||||
{
|
||||
struct mie_string_cache *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
out->c_entries = b_hashmap_create(
|
||||
NULL, (b_hashmap_value_destructor)mie_string_destroy);
|
||||
if (!out->c_entries) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void mie_string_cache_destroy(struct mie_string_cache *cache)
|
||||
{
|
||||
b_hashmap_unref(cache->c_entries);
|
||||
free(cache);
|
||||
}
|
||||
|
||||
struct mie_string *mie_string_cache_get(
|
||||
struct mie_string_cache *cache, struct mie_ctx *ctx, const char *s)
|
||||
{
|
||||
size_t s_len = strlen(s);
|
||||
b_hashmap_key key = {.key_data = s, .key_size = s_len};
|
||||
const b_hashmap_value *value = b_hashmap_get(cache->c_entries, &key);
|
||||
if (value) {
|
||||
return value->value_data;
|
||||
}
|
||||
|
||||
struct mie_string *str = mie_string_create(ctx, s, s_len);
|
||||
if (!str) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
b_hashmap_value new_val = {.value_data = str, .value_size = sizeof *str};
|
||||
b_hashmap_put(cache->c_entries, &key, &new_val);
|
||||
|
||||
return str;
|
||||
}
|
||||
41
mie/dialect/builtin/string.c
Normal file
41
mie/dialect/builtin/string.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <blue/core/bstr.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/builtin.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/type.h>
|
||||
#include <mie/macros.h>
|
||||
#include <mie/type/type.h>
|
||||
#include <mie/value.h>
|
||||
|
||||
static void value_print(
|
||||
const struct mie_type *ty, const struct mie_value *value, b_stream *out)
|
||||
{
|
||||
const struct mie_string *str = (const struct mie_string *)value;
|
||||
b_stream_write_fmt(out, NULL, "\"%s\"", str->str_val);
|
||||
}
|
||||
|
||||
static void type_init(const struct mie_dialect_type *type_info, struct mie_type *type)
|
||||
{
|
||||
type->ty_instance_size = sizeof(struct mie_string);
|
||||
}
|
||||
|
||||
static enum mie_status print(
|
||||
const struct mie_dialect_type *def, const struct mie_type *ty, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
const struct mie_dialect_type *def, struct mie_parser *parser,
|
||||
struct mie_type **out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_TYPE_BEGIN(mie_builtin_string, "string")
|
||||
MIE_DIALECT_TYPE_STRUCT(struct mie_type);
|
||||
MIE_DIALECT_TYPE_INIT(type_init);
|
||||
MIE_DIALECT_TYPE_PRINT(print);
|
||||
MIE_DIALECT_TYPE_PARSE(parse);
|
||||
MIE_DIALECT_TYPE_VALUE_PRINT(value_print);
|
||||
MIE_DIALECT_TYPE_END()
|
||||
18
mie/dialect/cf/br-cond.c
Normal file
18
mie/dialect/cf/br-cond.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_OP_BEGIN(mie_cf_br_cond, "br-cond")
|
||||
MIE_DIALECT_OP_PRINT(print);
|
||||
MIE_DIALECT_OP_PARSE(parse);
|
||||
MIE_DIALECT_OP_END()
|
||||
18
mie/dialect/cf/br.c
Normal file
18
mie/dialect/cf/br.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_OP_BEGIN(mie_cf_br, "br")
|
||||
MIE_DIALECT_OP_PRINT(print);
|
||||
MIE_DIALECT_OP_PARSE(parse);
|
||||
MIE_DIALECT_OP_END()
|
||||
7
mie/dialect/cf/cf.c
Normal file
7
mie/dialect/cf/cf.c
Normal file
@@ -0,0 +1,7 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_cf, "cf")
|
||||
MIE_DIALECT_ADD_OP(mie_cf_br);
|
||||
MIE_DIALECT_ADD_OP(mie_cf_br_cond);
|
||||
MIE_DIALECT_END()
|
||||
61
mie/dialect/dialect.c
Normal file
61
mie/dialect/dialect.c
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <blue/ds/string.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
#include <mie/dialect/type.h>
|
||||
|
||||
#define TYPE_NS_ID \
|
||||
MIE_ID(0xe4, 0x99, 0x42, 0x58, 0x2b, 0xdb, 0x45, 0xa3, 0xbd, 0x4b, \
|
||||
0x17, 0xe3, 0xc4, 0xa9, 0xbc, 0x30)
|
||||
#define OP_NS_ID \
|
||||
MIE_ID(0xb9, 0x97, 0xcf, 0xd3, 0x81, 0xd4, 0x45, 0x06, 0x9b, 0x44, \
|
||||
0x05, 0x9f, 0xb4, 0x76, 0xf1, 0x2d)
|
||||
|
||||
struct mie_dialect *mie_dialect_create(struct mie_ctx *ctx, const char *name)
|
||||
{
|
||||
struct mie_dialect *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
out->d_name = b_strdup(name);
|
||||
if (!out->d_name) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_id op_ns = OP_NS_ID;
|
||||
mie_id_map_init(&out->d_ops, &op_ns);
|
||||
|
||||
mie_id type_ns = TYPE_NS_ID;
|
||||
mie_id_map_init(&out->d_types, &type_ns);
|
||||
|
||||
b_rope name_rope = B_ROPE_CSTR(name);
|
||||
mie_id_map_put(&ctx->ctx_dialects, &out->d_id, &name_rope);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
const struct mie_dialect_op *mie_dialect_get_op(
|
||||
const struct mie_dialect *dialect, const char *name)
|
||||
{
|
||||
b_rope name_rope = B_ROPE_CSTR(name);
|
||||
mie_id id;
|
||||
mie_id_init_ns(&id, mie_id_map_get_ns(&dialect->d_ops), &name_rope);
|
||||
|
||||
mie_id *target = mie_id_map_get(&dialect->d_ops, &id);
|
||||
return b_unbox(struct mie_dialect_op, target, op_id);
|
||||
}
|
||||
|
||||
const struct mie_dialect_type *mie_dialect_get_type(
|
||||
const struct mie_dialect *dialect, const char *name)
|
||||
{
|
||||
b_rope name_rope = B_ROPE_CSTR(name);
|
||||
mie_id id;
|
||||
mie_id_init_ns(&id, mie_id_map_get_ns(&dialect->d_types), &name_rope);
|
||||
|
||||
mie_id *target = mie_id_map_get(&dialect->d_types, &id);
|
||||
return b_unbox(struct mie_dialect_type, target, ty_id);
|
||||
}
|
||||
22
mie/dialect/func/func.c
Normal file
22
mie/dialect/func/func.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_OP_BEGIN(mie_func_func, "func")
|
||||
MIE_DIALECT_OP_PRINT(print);
|
||||
MIE_DIALECT_OP_PARSE(parse);
|
||||
MIE_DIALECT_OP_END()
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_func, "func")
|
||||
MIE_DIALECT_ADD_OP(mie_func_func);
|
||||
MIE_DIALECT_END()
|
||||
82
mie/dialect/index/index-cache.c
Normal file
82
mie/dialect/index/index-cache.c
Normal file
@@ -0,0 +1,82 @@
|
||||
#include <blue/core/btree.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/index.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct index_cache_entry {
|
||||
b_btree_node e_node;
|
||||
struct mie_index e_value;
|
||||
};
|
||||
|
||||
struct mie_index_cache {
|
||||
b_btree c_entries;
|
||||
};
|
||||
|
||||
static B_BTREE_DEFINE_SIMPLE_INSERT(
|
||||
struct index_cache_entry, e_node, e_value.i_value, put_index);
|
||||
static B_BTREE_DEFINE_SIMPLE_GET(
|
||||
struct index_cache_entry, size_t, e_node, e_value.i_value, get_index);
|
||||
|
||||
static struct index_cache_entry *index_cache_entry_create(
|
||||
struct mie_ctx *ctx, size_t val)
|
||||
{
|
||||
struct index_cache_entry *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->e_value.i_value = val;
|
||||
out->e_value.i_base.v_type = mie_ctx_get_type(ctx, "index", "index");
|
||||
if (!out->e_value.i_base.v_type) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
struct mie_index_cache *mie_index_cache_create(void)
|
||||
{
|
||||
struct mie_index_cache *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
return out;
|
||||
}
|
||||
|
||||
void mie_index_cache_destroy(struct mie_index_cache *cache)
|
||||
{
|
||||
b_btree_node *cur = b_btree_first(&cache->c_entries);
|
||||
while (cur) {
|
||||
b_btree_node *next = b_btree_next(cur);
|
||||
b_btree_delete(&cache->c_entries, cur);
|
||||
|
||||
struct index_cache_entry *entry
|
||||
= b_unbox(struct index_cache_entry, cur, e_node);
|
||||
free(entry);
|
||||
|
||||
cur = next;
|
||||
}
|
||||
|
||||
free(cache);
|
||||
}
|
||||
|
||||
struct mie_index *mie_index_cache_get(
|
||||
struct mie_index_cache *cache, struct mie_ctx *ctx, size_t value)
|
||||
{
|
||||
struct index_cache_entry *entry = get_index(&cache->c_entries, value);
|
||||
if (!entry) {
|
||||
entry = index_cache_entry_create(ctx, value);
|
||||
|
||||
if (!entry) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
put_index(&cache->c_entries, entry);
|
||||
}
|
||||
|
||||
return &entry->e_value;
|
||||
}
|
||||
50
mie/dialect/index/index.c
Normal file
50
mie/dialect/index/index.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/index.h>
|
||||
#include <mie/dialect/type.h>
|
||||
#include <mie/macros.h>
|
||||
#include <mie/type/type.h>
|
||||
#include <mie/value.h>
|
||||
|
||||
struct index_type {
|
||||
struct mie_type i_base;
|
||||
};
|
||||
|
||||
static void value_print(
|
||||
const struct mie_type *ty, const struct mie_value *value, b_stream *out)
|
||||
{
|
||||
struct index_type *index_ty = (struct index_type *)ty;
|
||||
struct mie_index *index_val = (struct mie_index *)value;
|
||||
b_stream_write_fmt(
|
||||
out, NULL, "%zu : %s", index_val->i_value,
|
||||
index_ty->i_base.ty_def->ty_name);
|
||||
}
|
||||
|
||||
static void type_init(const struct mie_dialect_type *type_info, struct mie_type *type)
|
||||
{
|
||||
type->ty_instance_size = sizeof(struct mie_index);
|
||||
}
|
||||
|
||||
static enum mie_status print(
|
||||
const struct mie_dialect_type *def, const struct mie_type *ty, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
const struct mie_dialect_type *def, struct mie_parser *parser,
|
||||
struct mie_type **out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_TYPE_BEGIN(mie_index_index, "index")
|
||||
MIE_DIALECT_TYPE_STRUCT(struct index_type);
|
||||
MIE_DIALECT_TYPE_INIT(type_init);
|
||||
MIE_DIALECT_TYPE_PRINT(print);
|
||||
MIE_DIALECT_TYPE_PARSE(parse);
|
||||
MIE_DIALECT_TYPE_VALUE_PRINT(value_print);
|
||||
MIE_DIALECT_TYPE_END()
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_index, "index")
|
||||
MIE_DIALECT_ADD_TYPE(mie_index_index);
|
||||
MIE_DIALECT_END()
|
||||
6
mie/dialect/meta/meta.c
Normal file
6
mie/dialect/meta/meta.c
Normal file
@@ -0,0 +1,6 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_meta, "meta")
|
||||
MIE_DIALECT_ADD_OP(mie_meta_source_filename);
|
||||
MIE_DIALECT_END()
|
||||
18
mie/dialect/meta/source-filename.c
Normal file
18
mie/dialect/meta/source-filename.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_OP_BEGIN(mie_meta_source_filename, "source-filename")
|
||||
MIE_DIALECT_OP_PRINT(print);
|
||||
MIE_DIALECT_OP_PARSE(parse);
|
||||
MIE_DIALECT_OP_END()
|
||||
25
mie/dialect/op.c
Normal file
25
mie/dialect/op.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <blue/ds/string.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
|
||||
struct mie_dialect_op *mie_dialect_op_create(
|
||||
struct mie_dialect *parent, const char *name)
|
||||
{
|
||||
struct mie_dialect_op *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->op_name = b_strdup(name);
|
||||
if (!out->op_name) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->op_parent = parent;
|
||||
|
||||
b_rope name_rope = B_ROPE_CSTR(name);
|
||||
mie_id_map_put(&parent->d_ops, &out->op_id, &name_rope);
|
||||
|
||||
return out;
|
||||
}
|
||||
18
mie/dialect/ptr/load.c
Normal file
18
mie/dialect/ptr/load.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_OP_BEGIN(mie_ptr_load, "load")
|
||||
MIE_DIALECT_OP_PRINT(print);
|
||||
MIE_DIALECT_OP_PARSE(parse);
|
||||
MIE_DIALECT_OP_END()
|
||||
32
mie/dialect/ptr/ptr.c
Normal file
32
mie/dialect/ptr/ptr.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/type.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
struct ptr_type {
|
||||
struct mie_dialect_type ptr_base;
|
||||
};
|
||||
|
||||
static enum mie_status print(
|
||||
const struct mie_dialect_type *def, const struct mie_type *ty, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
const struct mie_dialect_type *def, struct mie_parser *parser,
|
||||
struct mie_type **out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_TYPE_BEGIN(mie_ptr_ptr, "ptr")
|
||||
MIE_DIALECT_TYPE_STRUCT(struct ptr_type);
|
||||
MIE_DIALECT_TYPE_PRINT(print);
|
||||
MIE_DIALECT_TYPE_PARSE(parse);
|
||||
MIE_DIALECT_TYPE_END()
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_ptr, "ptr")
|
||||
MIE_DIALECT_ADD_OP(mie_ptr_load);
|
||||
MIE_DIALECT_ADD_OP(mie_ptr_store);
|
||||
MIE_DIALECT_ADD_TYPE(mie_ptr_ptr);
|
||||
MIE_DIALECT_END()
|
||||
18
mie/dialect/ptr/store.c
Normal file
18
mie/dialect/ptr/store.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_OP_BEGIN(mie_ptr_store, "store")
|
||||
MIE_DIALECT_OP_PRINT(print);
|
||||
MIE_DIALECT_OP_PARSE(parse);
|
||||
MIE_DIALECT_OP_END()
|
||||
18
mie/dialect/scf/for.c
Normal file
18
mie/dialect/scf/for.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_OP_BEGIN(mie_scf_for, "for")
|
||||
MIE_DIALECT_OP_PRINT(print);
|
||||
MIE_DIALECT_OP_PARSE(parse);
|
||||
MIE_DIALECT_OP_END()
|
||||
18
mie/dialect/scf/if.c
Normal file
18
mie/dialect/scf/if.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_OP_BEGIN(mie_scf_if, "if")
|
||||
MIE_DIALECT_OP_PRINT(print);
|
||||
MIE_DIALECT_OP_PARSE(parse);
|
||||
MIE_DIALECT_OP_END()
|
||||
10
mie/dialect/scf/scf.c
Normal file
10
mie/dialect/scf/scf.c
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "scf.h"
|
||||
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_scf, "scf")
|
||||
MIE_DIALECT_ADD_OP(mie_scf_if);
|
||||
MIE_DIALECT_ADD_OP(mie_scf_for);
|
||||
MIE_DIALECT_ADD_OP(mie_scf_yield);
|
||||
MIE_DIALECT_END()
|
||||
7
mie/dialect/scf/scf.h
Normal file
7
mie/dialect/scf/scf.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef MIE_DIALECT_SCF_H_
|
||||
#define MIE_DIALECT_SCF_H_
|
||||
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
|
||||
#endif
|
||||
18
mie/dialect/scf/yield.c
Normal file
18
mie/dialect/scf/yield.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/op.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_OP_BEGIN(mie_scf_yield, "yield")
|
||||
MIE_DIALECT_OP_PRINT(print);
|
||||
MIE_DIALECT_OP_PARSE(parse);
|
||||
MIE_DIALECT_OP_END()
|
||||
25
mie/dialect/type.c
Normal file
25
mie/dialect/type.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <blue/ds/string.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/type.h>
|
||||
|
||||
struct mie_dialect_type *mie_dialect_type_create(
|
||||
struct mie_dialect *parent, const char *name)
|
||||
{
|
||||
struct mie_dialect_type *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->ty_name = b_strdup(name);
|
||||
if (!out->ty_name) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->ty_parent = parent;
|
||||
|
||||
b_rope name_rope = B_ROPE_CSTR(name);
|
||||
mie_id_map_put(&parent->d_types, &out->ty_id, &name_rope);
|
||||
|
||||
return out;
|
||||
}
|
||||
183
mie/id.c
Normal file
183
mie/id.c
Normal file
@@ -0,0 +1,183 @@
|
||||
#include <blue/core/bstr.h>
|
||||
#include <blue/core/endian.h>
|
||||
#include <blue/core/hash.h>
|
||||
#include <blue/core/random.h>
|
||||
#include <mie/id.h>
|
||||
#include <string.h>
|
||||
|
||||
static inline int id_memcmp(const uint8_t *a, const uint8_t *b)
|
||||
{
|
||||
return memcmp(a, b, MIE_ID_NR_BYTES);
|
||||
}
|
||||
|
||||
static mie_id *get_id(const b_btree *tree, mie_id *key)
|
||||
{
|
||||
b_btree_node *cur = tree->b_root;
|
||||
while (cur) {
|
||||
mie_id *cur_node = b_unbox(mie_id, cur, e_node);
|
||||
int cmp = memcmp(
|
||||
key->id_bytes, cur_node->id_bytes, sizeof key->id_bytes);
|
||||
|
||||
if (cmp > 0) {
|
||||
cur = b_btree_right(cur);
|
||||
} else if (cmp < 0) {
|
||||
cur = b_btree_left(cur);
|
||||
} else {
|
||||
return cur_node;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void put_id(b_btree *tree, mie_id *node)
|
||||
{
|
||||
if (!tree->b_root) {
|
||||
tree->b_root = &node->e_node;
|
||||
b_btree_insert_fixup(tree, &node->e_node);
|
||||
return;
|
||||
}
|
||||
|
||||
b_btree_node *cur = tree->b_root;
|
||||
while (1) {
|
||||
mie_id *cur_node = b_unbox(mie_id, cur, e_node);
|
||||
b_btree_node *next = NULL;
|
||||
int cmp = memcmp(
|
||||
node->id_bytes, cur_node->id_bytes, sizeof node->id_bytes);
|
||||
|
||||
if (cmp > 0) {
|
||||
next = b_btree_right(cur);
|
||||
|
||||
if (!next) {
|
||||
b_btree_put_right(cur, &node->e_node);
|
||||
break;
|
||||
}
|
||||
} else if (cmp < 0) {
|
||||
next = b_btree_left(cur);
|
||||
|
||||
if (!next) {
|
||||
b_btree_put_left(cur, &node->e_node);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
cur = next;
|
||||
}
|
||||
|
||||
b_btree_insert_fixup(tree, &node->e_node);
|
||||
}
|
||||
|
||||
void mie_id_init(
|
||||
mie_id *out, uint32_t a, uint16_t b, uint16_t c, uint16_t d, uint64_t e)
|
||||
{
|
||||
b_i32 x_a = b_i32_htob(a);
|
||||
b_i16 x_b = b_i16_htob(b);
|
||||
b_i16 x_c = b_i16_htob(c);
|
||||
b_i16 x_d = b_i16_htob(d);
|
||||
b_i64 x_e = b_i64_htob(e);
|
||||
|
||||
memcpy(&out->id_bytes[0], x_a.i_bytes, sizeof x_a.i_bytes);
|
||||
memcpy(&out->id_bytes[4], x_b.i_bytes, sizeof x_b.i_bytes);
|
||||
memcpy(&out->id_bytes[6], x_c.i_bytes, sizeof x_c.i_bytes);
|
||||
memcpy(&out->id_bytes[8], x_d.i_bytes, sizeof x_d.i_bytes);
|
||||
memcpy(&out->id_bytes[10], &x_e.i_bytes[2], sizeof x_e.i_bytes - 2);
|
||||
}
|
||||
|
||||
void mie_id_init_zero(mie_id *out)
|
||||
{
|
||||
memset(out, 0x0, sizeof *out);
|
||||
}
|
||||
|
||||
void mie_id_init_random(mie_id *out)
|
||||
{
|
||||
b_random_ctx *random = b_random_global_ctx();
|
||||
b_random_next_bytes(
|
||||
random, (unsigned char *)out->id_bytes, sizeof out->id_bytes);
|
||||
}
|
||||
|
||||
void mie_id_init_ns(mie_id *out, const mie_id *ns, const b_rope *name)
|
||||
{
|
||||
struct mie_id_builder ctx;
|
||||
mie_id_builder_begin(&ctx, ns);
|
||||
mie_id_builder_add_rope(&ctx, name);
|
||||
mie_id_builder_end(&ctx, out);
|
||||
}
|
||||
|
||||
void mie_id_to_string(const mie_id *id, char *out, size_t max)
|
||||
{
|
||||
b_bstr str;
|
||||
b_bstr_begin(&str, out, max);
|
||||
for (size_t i = 0; i < sizeof id->id_bytes; i++) {
|
||||
if (i == 4 || i == 6 || i == 8 || i == 10) {
|
||||
b_bstr_write_char(&str, '-');
|
||||
}
|
||||
|
||||
b_bstr_write_fmt(&str, "%02x", id->id_bytes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void mie_id_map_init(struct mie_id_map *map, const mie_id *ns)
|
||||
{
|
||||
memset(map, 0x0, sizeof *map);
|
||||
|
||||
map->map_ns_id = *ns;
|
||||
}
|
||||
|
||||
const mie_id *mie_id_map_get_ns(const struct mie_id_map *map)
|
||||
{
|
||||
return &map->map_ns_id;
|
||||
}
|
||||
|
||||
void mie_id_map_put(struct mie_id_map *map, mie_id *id, const b_rope *name)
|
||||
{
|
||||
mie_id_init_ns(id, &map->map_ns_id, name);
|
||||
put_id(&map->map_entries, id);
|
||||
}
|
||||
|
||||
void mie_id_map_put_id(struct mie_id_map *map, mie_id *node)
|
||||
{
|
||||
put_id(&map->map_entries, node);
|
||||
}
|
||||
|
||||
mie_id *mie_id_map_get(const struct mie_id_map *map, mie_id *id)
|
||||
{
|
||||
return get_id(&map->map_entries, id);
|
||||
}
|
||||
|
||||
void mie_id_builder_begin(struct mie_id_builder *builder, const mie_id *ns)
|
||||
{
|
||||
memset(builder, 0x0, sizeof *builder);
|
||||
b_hash_ctx_init(&builder->b_hash, B_HASH_SHA1);
|
||||
b_hash_ctx_update(&builder->b_hash, ns->id_bytes, sizeof ns->id_bytes);
|
||||
}
|
||||
|
||||
void mie_id_builder_add(struct mie_id_builder *builder, const void *p, size_t len)
|
||||
{
|
||||
b_hash_ctx_update(&builder->b_hash, p, len);
|
||||
}
|
||||
|
||||
void mie_id_builder_add_rope(struct mie_id_builder *builder, const b_rope *rope)
|
||||
{
|
||||
b_hash_ctx_update_rope(&builder->b_hash, rope);
|
||||
}
|
||||
|
||||
void mie_id_builder_add_marker(
|
||||
struct mie_id_builder *builder, enum mie_id_builder_marker marker)
|
||||
{
|
||||
unsigned char c = marker;
|
||||
b_hash_ctx_update(&builder->b_hash, &c, sizeof c);
|
||||
}
|
||||
|
||||
void mie_id_builder_end(struct mie_id_builder *builder, mie_id *out)
|
||||
{
|
||||
memset(out, 0x0, sizeof *out);
|
||||
b_hash_ctx_finish(&builder->b_hash, out->id_bytes, sizeof out->id_bytes);
|
||||
|
||||
out->id_bytes[6] &= 0x0F;
|
||||
out->id_bytes[6] |= 0x50;
|
||||
|
||||
out->id_bytes[8] &= 0x3F;
|
||||
out->id_bytes[8] |= 0x80;
|
||||
}
|
||||
@@ -3,30 +3,59 @@
|
||||
|
||||
#include <blue/core/btree.h>
|
||||
#include <blue/ds/hashmap.h>
|
||||
#include <mie/type.h>
|
||||
#include <mie/id.h>
|
||||
|
||||
struct mie_op;
|
||||
struct mie_int_cache;
|
||||
struct mie_index_cache;
|
||||
struct mie_string_cache;
|
||||
;
|
||||
|
||||
struct mie_ctx {
|
||||
#if 0
|
||||
struct mie_const *ctx_true, *ctx_false;
|
||||
struct mie_value *ctx_null;
|
||||
struct mie_type *ctx_types[__MIE_TYPE_COUNT];
|
||||
b_btree ctx_int_cache;
|
||||
b_hashmap *ctx_sel_cache;
|
||||
b_hashmap *ctx_string_cache;
|
||||
#endif
|
||||
/* map of struct mie_dialect */
|
||||
struct mie_id_map ctx_dialects;
|
||||
/* map of struct mie_type */
|
||||
struct mie_id_map ctx_types;
|
||||
/* map of struct mie_trait */
|
||||
struct mie_id_map ctx_traits;
|
||||
|
||||
struct mie_int_cache *ctx_ints;
|
||||
struct mie_index_cache *ctx_indices;
|
||||
struct mie_string_cache *ctx_strings;
|
||||
};
|
||||
|
||||
extern struct mie_ctx *mie_ctx_create(void);
|
||||
extern void mie_ctx_destroy(struct mie_ctx *ctx);
|
||||
MIE_API struct mie_ctx *mie_ctx_create(void);
|
||||
MIE_API 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_null(struct mie_ctx *ctx);
|
||||
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, unsigned int nr_bits);
|
||||
extern struct mie_value *mie_ctx_get_string(struct mie_ctx *ctx, const char *s);
|
||||
extern struct mie_value *mie_ctx_get_selector(struct mie_ctx *ctx, const char *sel);
|
||||
extern struct mie_value *mie_ctx_create_array(struct mie_ctx *ctx);
|
||||
MIE_API bool mie_ctx_resolve_op(const struct mie_ctx *ctx, struct mie_op *op);
|
||||
|
||||
MIE_API struct mie_dialect *mie_ctx_get_dialect(
|
||||
const struct mie_ctx *ctx, const char *name);
|
||||
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_type(
|
||||
struct mie_ctx *ctx, const char *dialect_name, const char *type_name);
|
||||
MIE_API struct mie_type *mie_ctx_get_trait(
|
||||
struct mie_ctx *ctx, const char *dialect_name, const char *trait_name);
|
||||
MIE_API struct mie_type *mie_ctx_get_storage_type(
|
||||
struct mie_ctx *ctx, const struct mie_type **parts, size_t nr_parts);
|
||||
MIE_API struct mie_type *mie_ctx_get_function_type(
|
||||
struct mie_ctx *ctx, const struct mie_type **in, size_t nr_in,
|
||||
const struct mie_type **out, size_t nr_out);
|
||||
|
||||
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, size_t nr_bits);
|
||||
MIE_API struct mie_value *mie_ctx_get_float(
|
||||
struct mie_ctx *ctx, double val, size_t nr_bits);
|
||||
MIE_API struct mie_value *mie_ctx_get_string(struct mie_ctx *ctx, const char *s);
|
||||
MIE_API struct mie_value *mie_ctx_get_index(struct mie_ctx *ctx, size_t val);
|
||||
|
||||
#endif
|
||||
|
||||
53
mie/include/mie/dialect/arith.h
Normal file
53
mie/include/mie/dialect/arith.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef MIE_DIALECT_ARITH_H_
|
||||
#define MIE_DIALECT_ARITH_H_
|
||||
|
||||
#include <mie/misc.h>
|
||||
#include <mie/value.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct mie_ctx;
|
||||
struct mie_dialect;
|
||||
|
||||
enum mie_float_width {
|
||||
MIE_FLOAT_32 = 32,
|
||||
MIE_FLOAT_64 = 64,
|
||||
};
|
||||
|
||||
struct mie_int {
|
||||
struct mie_value i_base;
|
||||
union {
|
||||
int64_t v_small;
|
||||
|
||||
struct {
|
||||
long *a_parts;
|
||||
size_t a_nr_parts;
|
||||
} v_arbitrary;
|
||||
} i_val;
|
||||
};
|
||||
|
||||
struct mie_float {
|
||||
struct mie_value f_base;
|
||||
|
||||
union {
|
||||
float v_32;
|
||||
double v_64;
|
||||
} f_val;
|
||||
};
|
||||
|
||||
struct mie_int_cache;
|
||||
|
||||
MIE_API struct mie_dialect *mie_arith_dialect_create(struct mie_ctx *ctx);
|
||||
|
||||
MIE_API struct mie_type *mie_arith_int_get_type(
|
||||
struct mie_ctx *ctx, size_t bit_width);
|
||||
MIE_API struct mie_type *mie_arith_float_get_type(
|
||||
struct mie_ctx *ctx, size_t bit_width);
|
||||
|
||||
MIE_API struct mie_int_cache *mie_int_cache_create(void);
|
||||
MIE_API void mie_int_cache_destroy(struct mie_int_cache *cache);
|
||||
MIE_API struct mie_int *mie_int_cache_get(
|
||||
struct mie_int_cache *cache, struct mie_ctx *ctx, size_t value,
|
||||
size_t bit_width);
|
||||
|
||||
#endif
|
||||
29
mie/include/mie/dialect/builtin.h
Normal file
29
mie/include/mie/dialect/builtin.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef MIE_DIALECT_BUILTIN_H_
|
||||
#define MIE_DIALECT_BUILTIN_H_
|
||||
|
||||
#include <blue/ds/string.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/value.h>
|
||||
|
||||
struct mie_dialect;
|
||||
|
||||
struct mie_ctx;
|
||||
struct mie_int_type;
|
||||
struct mie_float_type;
|
||||
|
||||
struct mie_string {
|
||||
struct mie_value str_base;
|
||||
char *str_val;
|
||||
size_t str_len;
|
||||
};
|
||||
|
||||
struct mie_string_cache;
|
||||
|
||||
MIE_API struct mie_dialect *mie_builtin_dialect_create(struct mie_ctx *ctx);
|
||||
|
||||
MIE_API struct mie_string_cache *mie_string_cache_create(void);
|
||||
MIE_API void mie_string_cache_destroy(struct mie_string_cache *cache);
|
||||
MIE_API struct mie_string *mie_string_cache_get(
|
||||
struct mie_string_cache *cache, struct mie_ctx *ctx, const char *val);
|
||||
|
||||
#endif
|
||||
11
mie/include/mie/dialect/cf.h
Normal file
11
mie/include/mie/dialect/cf.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef MIE_DIALECT_CF_H_
|
||||
#define MIE_DIALECT_CF_H_
|
||||
|
||||
#include <mie/misc.h>
|
||||
|
||||
struct mie_ctx;
|
||||
struct mie_dialect;
|
||||
|
||||
MIE_API struct mie_dialect *mie_cf_dialect_create(struct mie_ctx *ctx);
|
||||
|
||||
#endif
|
||||
38
mie/include/mie/dialect/dialect.h
Normal file
38
mie/include/mie/dialect/dialect.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef MIE_DIALECT_DIALECT_H_
|
||||
#define MIE_DIALECT_DIALECT_H_
|
||||
|
||||
#include <blue/core/btree.h>
|
||||
#include <blue/core/queue.h>
|
||||
#include <mie/id.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/vector.h>
|
||||
|
||||
struct mie_ctx;
|
||||
|
||||
struct mie_dialect_op;
|
||||
struct mie_dialect_type;
|
||||
struct mie_trait_definition;
|
||||
|
||||
struct mie_dialect {
|
||||
mie_id d_id;
|
||||
char *d_name;
|
||||
|
||||
/* map of struct mie_dialect_op */
|
||||
struct mie_id_map d_ops;
|
||||
/* map of struct mie_dialect_type */
|
||||
struct mie_id_map d_types;
|
||||
/* map of struct mie_trait_definition */
|
||||
struct mie_id_map d_traits;
|
||||
};
|
||||
|
||||
MIE_API struct mie_dialect *mie_dialect_create(
|
||||
struct mie_ctx *ctx, const char *name);
|
||||
|
||||
MIE_API const struct mie_dialect_op *mie_dialect_get_op(
|
||||
const struct mie_dialect *dialect, const char *name);
|
||||
MIE_API const struct mie_dialect_type *mie_dialect_get_type(
|
||||
const struct mie_dialect *dialect, const char *name);
|
||||
MIE_API const struct mie_trait_definition *mie_dialect_get_trait(
|
||||
const struct mie_dialect *dialect, const char *name);
|
||||
|
||||
#endif
|
||||
11
mie/include/mie/dialect/func.h
Normal file
11
mie/include/mie/dialect/func.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef MIE_DIALECT_FUNC_H_
|
||||
#define MIE_DIALECT_FUNC_H_
|
||||
|
||||
#include <mie/misc.h>
|
||||
|
||||
struct mie_ctx;
|
||||
struct mie_dialect;
|
||||
|
||||
MIE_API struct mie_dialect *mie_func_dialect_create(struct mie_ctx *ctx);
|
||||
|
||||
#endif
|
||||
26
mie/include/mie/dialect/index.h
Normal file
26
mie/include/mie/dialect/index.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef MIE_DIALECT_INDEX_H_
|
||||
#define MIE_DIALECT_INDEX_H_
|
||||
|
||||
#include <mie/misc.h>
|
||||
#include <mie/value.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct mie_ctx;
|
||||
struct mie_dialect;
|
||||
|
||||
struct mie_index {
|
||||
struct mie_value i_base;
|
||||
size_t i_value;
|
||||
};
|
||||
|
||||
struct mie_index_cache;
|
||||
|
||||
MIE_API struct mie_dialect *mie_index_dialect_create(struct mie_ctx *ctx);
|
||||
|
||||
MIE_API struct mie_index_cache *mie_index_cache_create(void);
|
||||
MIE_API void mie_index_cache_destroy(struct mie_index_cache *cache);
|
||||
MIE_API struct mie_index *mie_index_cache_get(
|
||||
struct mie_index_cache *cache, struct mie_ctx *ctx, size_t value);
|
||||
|
||||
#endif
|
||||
11
mie/include/mie/dialect/meta.h
Normal file
11
mie/include/mie/dialect/meta.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef MIE_DIALECT_META_H_
|
||||
#define MIE_DIALECT_META_H_
|
||||
|
||||
#include <mie/misc.h>
|
||||
|
||||
struct mie_ctx;
|
||||
struct mie_dialect;
|
||||
|
||||
MIE_API struct mie_dialect *mie_meta_dialect_create(struct mie_ctx *ctx);
|
||||
|
||||
#endif
|
||||
107
mie/include/mie/dialect/op.h
Normal file
107
mie/include/mie/dialect/op.h
Normal file
@@ -0,0 +1,107 @@
|
||||
#ifndef MIE_DIALECT_OP_H_
|
||||
#define MIE_DIALECT_OP_H_
|
||||
|
||||
#include <blue/core/btree.h>
|
||||
#include <blue/core/stream.h>
|
||||
#include <mie/id.h>
|
||||
#include <mie/status.h>
|
||||
#include <mie/trait/trait-table.h>
|
||||
|
||||
#define MIE_OP_MAX_PARAMS 8
|
||||
#define MIE_OP_MAX_RESULTS 8
|
||||
|
||||
struct mie_op;
|
||||
struct mie_parser;
|
||||
struct mie_dialect;
|
||||
|
||||
enum mie_op_trait {
|
||||
MIE_OP_TRAIT_NONE = 0x00u,
|
||||
|
||||
/* this is a graph op. references to it by name must be prefixed with +
|
||||
* and the op can only be used within graph regions.
|
||||
* a graph op can have the same name as a regular or non-graph op. */
|
||||
MIE_OP_TRAIT_GRAPH_ONLY = 0x01u,
|
||||
|
||||
/* this is a non-graph op. this op cannot be used within graph regions */
|
||||
MIE_OP_TRAIT_NO_GRAPH = 0x02u,
|
||||
|
||||
/* regions of an op that has this trait are graph regions. graph regions
|
||||
* cannot have more than one block, and the entry block must be unnamed
|
||||
* with no parameters. However, graph ops can be used. */
|
||||
MIE_OP_TRAIT_GRAPH_SCOPE = 0x04u,
|
||||
|
||||
/* regions of an op that has this trait cannot capture or reference
|
||||
* values defined in the enclosing scope. */
|
||||
MIE_OP_TRAIT_ISOLATED_FROM_ABOVE = 0x08u,
|
||||
};
|
||||
|
||||
enum mie_op_param_type {
|
||||
/* op has no parameter */
|
||||
MIE_OP_PARTYPE_NONE = 0,
|
||||
/* op has a parameter of any type. */
|
||||
MIE_OP_PARTYPE_ANY,
|
||||
/* op has a parameter of a fixed, named type. */
|
||||
MIE_OP_PARTYPE_TYPE_NAME,
|
||||
/* op has a parameter whose type matches the type of an attribute value */
|
||||
MIE_OP_PARTYPE_ATTRIB_VAL,
|
||||
};
|
||||
|
||||
enum mie_op_result_type {
|
||||
/* op has no result */
|
||||
MIE_OP_RESTYPE_NONE = 0,
|
||||
/* op has a result of any type. */
|
||||
MIE_OP_RESTYPE_ANY,
|
||||
/* op has a result of a fixed, named type. */
|
||||
MIE_OP_RESTYPE_TYPE_NAME,
|
||||
/* op has a result whose type matches the type of an input parameter */
|
||||
MIE_OP_RESTYPE_IN_VAL,
|
||||
/* op has a result whose type matches the type of an attribute value */
|
||||
MIE_OP_RESTYPE_ATTRIB_VAL,
|
||||
};
|
||||
|
||||
struct mie_op_param {
|
||||
enum mie_op_param_type param_type;
|
||||
|
||||
union {
|
||||
struct {
|
||||
const char *t_dialect;
|
||||
/* this can be NULL, in which case any type from the
|
||||
* dialect named in t_dialect will be accepted. */
|
||||
const char *t_name;
|
||||
} param_type_name;
|
||||
|
||||
const char *param_attrib;
|
||||
};
|
||||
};
|
||||
|
||||
struct mie_op_result {
|
||||
enum mie_op_result_type r_type;
|
||||
|
||||
union {
|
||||
struct {
|
||||
const char *t_dialect;
|
||||
const char *t_name;
|
||||
} r_type_name;
|
||||
|
||||
unsigned int r_in_index;
|
||||
const char *r_attrib;
|
||||
};
|
||||
};
|
||||
|
||||
struct mie_dialect_op {
|
||||
mie_id op_id;
|
||||
struct mie_dialect *op_parent;
|
||||
char *op_name;
|
||||
|
||||
struct mie_trait_table op_traits;
|
||||
const struct mie_op_param op_params[MIE_OP_MAX_PARAMS];
|
||||
const struct mie_op_result op_results[MIE_OP_MAX_RESULTS];
|
||||
|
||||
enum mie_status (*op_print)(const struct mie_op *, b_stream *);
|
||||
enum mie_status (*op_parse)(struct mie_parser *, struct mie_op *);
|
||||
};
|
||||
|
||||
MIE_API struct mie_dialect_op *mie_dialect_op_create(
|
||||
struct mie_dialect *parent, const char *name);
|
||||
|
||||
#endif
|
||||
11
mie/include/mie/dialect/ptr.h
Normal file
11
mie/include/mie/dialect/ptr.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef MIE_DIALECT_PTR_H_
|
||||
#define MIE_DIALECT_PTR_H_
|
||||
|
||||
#include <mie/misc.h>
|
||||
|
||||
struct mie_ctx;
|
||||
struct mie_dialect;
|
||||
|
||||
MIE_API struct mie_dialect *mie_ptr_dialect_create(struct mie_ctx *ctx);
|
||||
|
||||
#endif
|
||||
11
mie/include/mie/dialect/scf.h
Normal file
11
mie/include/mie/dialect/scf.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef MIE_DIALECT_SCF_H_
|
||||
#define MIE_DIALECT_SCF_H_
|
||||
|
||||
#include <mie/misc.h>
|
||||
|
||||
struct mie_ctx;
|
||||
struct mie_dialect;
|
||||
|
||||
MIE_API struct mie_dialect *mie_scf_dialect_create(struct mie_ctx *ctx);
|
||||
|
||||
#endif
|
||||
52
mie/include/mie/dialect/type.h
Normal file
52
mie/include/mie/dialect/type.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#ifndef MIE_DIALECT_TYPE_H_
|
||||
#define MIE_DIALECT_TYPE_H_
|
||||
|
||||
#include <blue/core/btree.h>
|
||||
#include <blue/core/stream.h>
|
||||
#include <mie/id.h>
|
||||
#include <mie/status.h>
|
||||
#include <mie/trait/trait-table.h>
|
||||
|
||||
struct mie_type;
|
||||
struct mie_value;
|
||||
struct mie_parser;
|
||||
struct mie_dialect;
|
||||
|
||||
enum mie_type_trait {
|
||||
/* this type is parametised. one or more arguments are required to create
|
||||
* a corresponding mie_type instance. in textual IR, references to this
|
||||
* type will be followed by a set of arguments surrounded
|
||||
* by <angle brackets>. */
|
||||
MIE_TYPE_TRAIT_PARAMETISED = 0x01u,
|
||||
|
||||
/* this type can only be used within graph regions. unlike graph ops,
|
||||
* no special prefix is required */
|
||||
MIE_TYPE_TRAIT_GRAPH_TYPE = 0x02u,
|
||||
};
|
||||
|
||||
struct mie_dialect_type {
|
||||
mie_id ty_id;
|
||||
struct mie_trait_table ty_traits;
|
||||
struct mie_dialect *ty_parent;
|
||||
char *ty_name;
|
||||
|
||||
size_t ty_data_size;
|
||||
|
||||
void (*ty_init)(const struct mie_dialect_type *, struct mie_type *);
|
||||
void (*ty_cleanup)(const struct mie_dialect_type *, struct mie_type *);
|
||||
void (*ty_instance_cleanup)(const struct mie_type *, struct mie_value *);
|
||||
enum mie_status (*ty_print)(
|
||||
const struct mie_dialect_type *, const struct mie_type *,
|
||||
b_stream *);
|
||||
void (*ty_value_print)(
|
||||
const struct mie_type *, const struct mie_value *, b_stream *);
|
||||
enum mie_status (*ty_parse)(
|
||||
const struct mie_dialect_type *, struct mie_parser *,
|
||||
struct mie_type **);
|
||||
void (*ty_build_id)(const struct mie_type *, struct mie_id_builder *);
|
||||
};
|
||||
|
||||
MIE_API struct mie_dialect_type *mie_dialect_type_create(
|
||||
struct mie_dialect *parent, const char *name);
|
||||
|
||||
#endif
|
||||
96
mie/include/mie/id.h
Normal file
96
mie/include/mie/id.h
Normal file
@@ -0,0 +1,96 @@
|
||||
#ifndef MIE_ID_H_
|
||||
#define MIE_ID_H_
|
||||
|
||||
#include <blue/core/btree.h>
|
||||
#include <blue/core/queue.h>
|
||||
#include <blue/core/rope.h>
|
||||
#include <mie/misc.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define MIE_ID(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
|
||||
{ \
|
||||
.id_bytes = {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p}, \
|
||||
}
|
||||
#define MIE_ID_ZERO ((mie_id) {0})
|
||||
|
||||
#define MIE_ID_STRING_MAX 37
|
||||
|
||||
#define MIE_ID_NR_BYTES 16
|
||||
#define MIE_ID_NR_WORDS (MIE_ID_NR_BYTES / sizeof(uintptr_t))
|
||||
#define MIE_ID_NR_WORDS_16 (MIE_ID_NR_BYTES / sizeof(uint16_t))
|
||||
#define MIE_ID_NR_WORDS_32 (MIE_ID_NR_BYTES / sizeof(uint32_t))
|
||||
#define MIE_ID_NR_WORDS_64 (MIE_ID_NR_BYTES / sizeof(uint64_t))
|
||||
|
||||
enum mie_id_builder_marker {
|
||||
MIE_ID_BUILDER_STORAGE_START = 0x32,
|
||||
MIE_ID_BUILDER_STORAGE_END = 0x8a,
|
||||
MIE_ID_BUILDER_FUNCTION_IN_START = 0x78,
|
||||
MIE_ID_BUILDER_FUNCTION_IN_END = 0x55,
|
||||
MIE_ID_BUILDER_FUNCTION_OUT_START = 0xa2,
|
||||
MIE_ID_BUILDER_FUNCTION_OUT_END = 0xbc,
|
||||
};
|
||||
|
||||
typedef struct mie_id {
|
||||
union {
|
||||
uint8_t id_bytes[MIE_ID_NR_BYTES];
|
||||
uintptr_t id_words[MIE_ID_NR_WORDS];
|
||||
uint16_t id_words_16[MIE_ID_NR_WORDS_16];
|
||||
uint32_t id_words_32[MIE_ID_NR_WORDS_32];
|
||||
uint64_t id_words_64[MIE_ID_NR_WORDS_64];
|
||||
};
|
||||
|
||||
union {
|
||||
b_btree_node e_node;
|
||||
b_queue_entry e_entry;
|
||||
};
|
||||
} mie_id;
|
||||
|
||||
struct mie_id_builder {
|
||||
b_hash_ctx b_hash;
|
||||
};
|
||||
|
||||
struct mie_id_map {
|
||||
mie_id map_ns_id;
|
||||
b_btree map_entries;
|
||||
};
|
||||
|
||||
MIE_API void mie_id_init(
|
||||
mie_id *out, uint32_t a, uint16_t b, uint16_t c, uint16_t d, uint64_t e);
|
||||
MIE_API void mie_id_init_zero(mie_id *out);
|
||||
MIE_API void mie_id_init_random(mie_id *out);
|
||||
MIE_API void mie_id_init_ns(mie_id *out, const mie_id *ns, const b_rope *name);
|
||||
|
||||
static inline int mie_id_compare(const mie_id *a, const mie_id *b)
|
||||
{
|
||||
return memcmp(a->id_bytes, b->id_bytes, sizeof a->id_bytes);
|
||||
}
|
||||
|
||||
MIE_API void mie_id_to_string(const mie_id *id, char *out, size_t max);
|
||||
|
||||
MIE_API void mie_id_map_init(struct mie_id_map *map, const mie_id *ns);
|
||||
MIE_API const mie_id *mie_id_map_get_ns(const struct mie_id_map *map);
|
||||
MIE_API void mie_id_map_put(
|
||||
struct mie_id_map *map, mie_id *node, const b_rope *name);
|
||||
MIE_API void mie_id_map_put_id(struct mie_id_map *map, mie_id *node);
|
||||
MIE_API mie_id *mie_id_map_get(const struct mie_id_map *map, mie_id *id);
|
||||
|
||||
MIE_API void mie_id_builder_begin(struct mie_id_builder *builder, const mie_id *ns);
|
||||
MIE_API void mie_id_builder_add(
|
||||
struct mie_id_builder *builder, const void *p, size_t len);
|
||||
static inline void mie_id_builder_add_char(struct mie_id_builder *builder, char c)
|
||||
{
|
||||
mie_id_builder_add(builder, &c, 1);
|
||||
}
|
||||
static inline void mie_id_builder_add_cstr(
|
||||
struct mie_id_builder *builder, const char *s)
|
||||
{
|
||||
mie_id_builder_add(builder, s, strlen(s));
|
||||
}
|
||||
MIE_API void mie_id_builder_add_rope(
|
||||
struct mie_id_builder *builder, const b_rope *rope);
|
||||
MIE_API void mie_id_builder_add_marker(
|
||||
struct mie_id_builder *builder, enum mie_id_builder_marker marker);
|
||||
MIE_API void mie_id_builder_end(struct mie_id_builder *builder, mie_id *out);
|
||||
|
||||
#endif
|
||||
@@ -1,12 +0,0 @@
|
||||
#ifndef MIE_ALLOCA_H_
|
||||
#define MIE_ALLOCA_H_
|
||||
|
||||
#include <mie/ir/instr.h>
|
||||
#include <mie/type.h>
|
||||
|
||||
struct mie_alloca {
|
||||
struct mie_instr a_base;
|
||||
struct mie_type *a_type;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,15 +0,0 @@
|
||||
#ifndef MIE_ARG_H_
|
||||
#define MIE_ARG_H_
|
||||
|
||||
#include <mie/ir/value.h>
|
||||
|
||||
#define MIE_ARG(p) ((struct mie_arg *)(p))
|
||||
|
||||
struct mie_arg {
|
||||
struct mie_value arg_base;
|
||||
struct mie_type *arg_type;
|
||||
};
|
||||
|
||||
extern struct mie_arg *mie_arg_create(struct mie_type *type);
|
||||
|
||||
#endif
|
||||
@@ -1,30 +1,21 @@
|
||||
#ifndef MIE_BLOCK_H_
|
||||
#define MIE_BLOCK_H_
|
||||
#ifndef MIE_IR_BLOCK_H_
|
||||
#define MIE_IR_BLOCK_H_
|
||||
|
||||
#define MIE_BLOCK(p) ((struct mie_block *)(p))
|
||||
#include <blue/core/queue.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/name.h>
|
||||
#include <mie/vector.h>
|
||||
|
||||
#include <mie/ir/value.h>
|
||||
struct mie_op;
|
||||
struct mie_register;
|
||||
|
||||
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;
|
||||
/* the rest of the instructions in the block */
|
||||
b_queue b_instr;
|
||||
/* the instruction that transfers control to the next block,
|
||||
* could be a switch, branch, or ret */
|
||||
struct mie_instr *b_terminator;
|
||||
struct mie_name b_name;
|
||||
|
||||
MIE_VECTOR_DECLARE(struct mie_register, b_params);
|
||||
MIE_VECTOR_DECLARE(struct mie_op, b_ops);
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
static inline bool mie_block_is_terminated(const struct mie_block *block)
|
||||
{
|
||||
return block->b_terminator != NULL;
|
||||
}
|
||||
MIE_API struct mie_op *mie_block_add_op(struct mie_block *block);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
#ifndef MIE_BRANCH_H_
|
||||
#define MIE_BRANCH_H_
|
||||
|
||||
#include <mie/ir/const.h>
|
||||
#include <mie/ir/instr.h>
|
||||
|
||||
struct mie_branch {
|
||||
struct mie_instr b_base;
|
||||
struct mie_block *b_dest;
|
||||
};
|
||||
|
||||
struct mie_branch_if {
|
||||
struct mie_instr b_base;
|
||||
struct mie_value *b_cond;
|
||||
struct mie_block *b_true_block;
|
||||
struct mie_block *b_false_block;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,119 +0,0 @@
|
||||
#ifndef MIE_BUILDER_H_
|
||||
#define MIE_BUILDER_H_
|
||||
|
||||
#include <blue/core/btree.h>
|
||||
#include <mie/ir/switch.h>
|
||||
#include <mie/ir/value.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/type.h>
|
||||
|
||||
struct b_hashmap;
|
||||
|
||||
struct mie_block;
|
||||
struct mie_module;
|
||||
struct mie_data;
|
||||
struct mie_type;
|
||||
struct mie_phi;
|
||||
struct mie_phi_edge;
|
||||
struct mie_ctx;
|
||||
|
||||
struct mie_builder {
|
||||
struct mie_ctx *b_ctx;
|
||||
struct mie_module *b_module;
|
||||
struct mie_block *b_current_block;
|
||||
struct mie_instr *b_prev_instr;
|
||||
};
|
||||
|
||||
enum mie_builder_flags {
|
||||
MIE_BUILDER_IGNORE_RESULT = 0x01u,
|
||||
};
|
||||
|
||||
extern struct mie_builder *mie_builder_create(
|
||||
struct mie_ctx *ctx, struct mie_module *mod);
|
||||
extern void mie_builder_destroy(struct mie_builder *builder);
|
||||
|
||||
extern struct mie_func *mie_builder_get_current_func(struct mie_builder *builder);
|
||||
static inline struct mie_block *mie_builder_get_current_block(
|
||||
struct mie_builder *builder)
|
||||
{
|
||||
return builder->b_current_block;
|
||||
}
|
||||
|
||||
extern struct mie_record *mie_builder_put_record(
|
||||
struct mie_builder *builder, struct mie_const *val, const char *name);
|
||||
extern struct mie_record *mie_builder_get_record(
|
||||
struct mie_builder *builder, const char *name);
|
||||
extern void mie_builder_put_data(struct mie_builder *builder, struct mie_data *data);
|
||||
extern void mie_builder_put_type(struct mie_builder *builder, struct mie_type *type);
|
||||
extern void mie_builder_set_insert_point(
|
||||
struct mie_builder *builder, struct mie_block *block);
|
||||
|
||||
extern struct mie_value *mie_builder_get_data_ptr(
|
||||
struct mie_builder *builder, const char *data_ident);
|
||||
extern struct mie_value *mie_builder_get_string_ptr(
|
||||
struct mie_builder *builder, const char *s);
|
||||
|
||||
extern struct mie_value *mie_builder_ret(
|
||||
struct mie_builder *builder, struct mie_value *val);
|
||||
extern struct mie_value *mie_builder_add(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name);
|
||||
extern struct mie_value *mie_builder_sub(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name);
|
||||
extern struct mie_value *mie_builder_mul(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name);
|
||||
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_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);
|
||||
extern struct mie_value *mie_builder_alloca(
|
||||
struct mie_builder *builder, struct mie_type *type, const char *name);
|
||||
extern struct mie_value *mie_builder_switch(
|
||||
struct mie_builder *builder, struct mie_value *cond,
|
||||
struct mie_switch_branch *branches, size_t nr_branches,
|
||||
struct mie_block *default_block);
|
||||
extern struct mie_value *mie_builder_br(
|
||||
struct mie_builder *builder, struct mie_block *dest);
|
||||
extern struct mie_value *mie_builder_br_if(
|
||||
struct mie_builder *builder, struct mie_value *cond,
|
||||
struct mie_block *if_true, struct mie_block *if_false);
|
||||
extern struct mie_value *mie_builder_msg(
|
||||
struct mie_builder *builder, struct mie_type *ret_type,
|
||||
struct mie_value *recipient, struct mie_value *selector,
|
||||
struct mie_value **args, size_t nr_args, enum mie_builder_flags flags,
|
||||
const char *name);
|
||||
extern struct mie_value *mie_builder_cmp_eq(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name);
|
||||
extern struct mie_value *mie_builder_cmp_neq(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name);
|
||||
extern struct mie_value *mie_builder_cmp_lt(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name);
|
||||
extern struct mie_value *mie_builder_cmp_gt(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name);
|
||||
extern struct mie_value *mie_builder_cmp_leq(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name);
|
||||
extern struct mie_value *mie_builder_cmp_geq(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name);
|
||||
extern struct mie_value *mie_builder_getelementptr(
|
||||
struct mie_builder *builder, struct mie_type *container_type,
|
||||
struct mie_value *container, struct mie_value *index, const char *name);
|
||||
extern struct mie_value *mie_builder_setelementptr(
|
||||
struct mie_builder *builder, struct mie_type *container_type,
|
||||
struct mie_value *container, struct mie_value *index);
|
||||
extern struct mie_value *mie_builder_phi(
|
||||
struct mie_builder *builder, struct mie_type *type,
|
||||
struct mie_phi_edge *edges, unsigned int nr_edges, const char *name);
|
||||
|
||||
#endif
|
||||
@@ -1,65 +0,0 @@
|
||||
#ifndef MIE_CONST_H_
|
||||
#define MIE_CONST_H_
|
||||
|
||||
#include <blue/ds/list.h>
|
||||
#include <mie/ir/value.h>
|
||||
#include <mie/type.h>
|
||||
|
||||
#define MIE_CONST(p) ((struct mie_const *)(p))
|
||||
|
||||
#define MIE_INT(p) ((struct mie_int *)(p))
|
||||
#define MIE_DOUBLE(p) ((struct mie_double *)(p))
|
||||
#define MIE_STRING(p) ((struct mie_string *)(p))
|
||||
#define MIE_ATOM(p) ((struct mie_atom *)(p))
|
||||
#define MIE_SELECTOR(p) ((struct mie_selector *)(p))
|
||||
#define MIE_ARRAY(p) ((struct mie_array *)(p))
|
||||
|
||||
struct mie_const {
|
||||
struct mie_value c_base;
|
||||
struct mie_type *c_type;
|
||||
};
|
||||
|
||||
struct mie_int {
|
||||
struct mie_const i_base;
|
||||
int64_t i_value;
|
||||
};
|
||||
|
||||
struct mie_double {
|
||||
struct mie_const d_base;
|
||||
double d_value;
|
||||
};
|
||||
|
||||
struct mie_string {
|
||||
struct mie_const s_base;
|
||||
char *s_value;
|
||||
};
|
||||
|
||||
struct mie_atom {
|
||||
struct mie_const a_base;
|
||||
char *a_value;
|
||||
};
|
||||
|
||||
struct mie_selector {
|
||||
struct mie_const sel_base;
|
||||
char *sel_value;
|
||||
};
|
||||
|
||||
struct mie_array {
|
||||
struct mie_const a_base;
|
||||
b_list *a_values;
|
||||
};
|
||||
|
||||
extern void mie_const_init(struct mie_const *c, struct mie_type *type);
|
||||
|
||||
static inline bool mie_value_is_selector(const struct mie_value *v)
|
||||
{
|
||||
if (v->v_type->t_id != MIE_VALUE_CONST) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const struct mie_const *c = MIE_CONST(v);
|
||||
|
||||
return c->c_type->t_id == MIE_TYPE_SELECTOR;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,82 +0,0 @@
|
||||
#ifndef MIE_CONVERT_H_
|
||||
#define MIE_CONVERT_H_
|
||||
|
||||
#include <blue/core/stringstream.h>
|
||||
#include <blue/ds/bitbuffer.h>
|
||||
#include <blue/ds/string.h>
|
||||
#include <mie/misc.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct mie_value;
|
||||
struct mie_ctx;
|
||||
|
||||
enum mie_ir_format {
|
||||
MIE_IR_NONE = 0,
|
||||
MIE_IR_MEM,
|
||||
MIE_IR_BITCODE,
|
||||
MIE_IR_TEXT,
|
||||
};
|
||||
|
||||
enum mie_ir_converter_medium {
|
||||
MIE_IR_CONVERTER_NONE = 0,
|
||||
MIE_IR_CONVERTER_MIE_VALUE,
|
||||
MIE_IR_CONVERTER_BITSTREAM,
|
||||
MIE_IR_CONVERTER_BITBUFFER,
|
||||
MIE_IR_CONVERTER_STRINGSTREAM,
|
||||
MIE_IR_CONVERTER_STRING,
|
||||
MIE_IR_CONVERTER_FILE,
|
||||
};
|
||||
|
||||
struct mie_ir_converter {
|
||||
struct mie_ctx *c_ctx;
|
||||
enum mie_ir_format c_src_format, c_dest_format;
|
||||
enum mie_ir_converter_medium c_src_medium, c_dest_medium;
|
||||
|
||||
union {
|
||||
/* TODO bitstream */
|
||||
struct mie_value *value;
|
||||
b_bitbuffer *bitbuffer;
|
||||
b_stringstream *stringstream;
|
||||
b_string *string;
|
||||
FILE *file;
|
||||
} c_src;
|
||||
|
||||
union {
|
||||
/* TODO bitstream */
|
||||
struct mie_value **value;
|
||||
b_bitbuffer *bitbuffer;
|
||||
b_stringstream *stringstream;
|
||||
b_string *string;
|
||||
FILE *file;
|
||||
} c_dest;
|
||||
};
|
||||
|
||||
MIE_API struct mie_ir_converter *mie_ir_converter_create(
|
||||
struct mie_ctx *ctx, enum mie_ir_format src, enum mie_ir_format dest);
|
||||
MIE_API void mie_ir_converter_destroy(struct mie_ir_converter *converter);
|
||||
|
||||
MIE_API b_status mie_ir_converter_set_src_value(
|
||||
struct mie_ir_converter *converter, struct mie_value *value);
|
||||
MIE_API b_status mie_ir_converter_set_src_bitbuffer(
|
||||
struct mie_ir_converter *converter, b_bitbuffer *bitbuffer);
|
||||
MIE_API b_status mie_ir_converter_set_src_stringstream(
|
||||
struct mie_ir_converter *converter, b_stringstream *stringstream);
|
||||
MIE_API b_status mie_ir_converter_set_src_string(
|
||||
struct mie_ir_converter *converter, b_string *string);
|
||||
MIE_API b_status mie_ir_converter_set_src_file(
|
||||
struct mie_ir_converter *converter, FILE *file);
|
||||
|
||||
MIE_API b_status mie_ir_converter_set_dest_value(
|
||||
struct mie_ir_converter *converter, struct mie_value **value);
|
||||
MIE_API b_status mie_ir_converter_set_dest_bitbuffer(
|
||||
struct mie_ir_converter *converter, b_bitbuffer *bitbuffer);
|
||||
MIE_API b_status mie_ir_converter_set_dest_stringstream(
|
||||
struct mie_ir_converter *converter, b_stringstream *stringstream);
|
||||
MIE_API b_status mie_ir_converter_set_dest_string(
|
||||
struct mie_ir_converter *converter, b_string *string);
|
||||
MIE_API b_status mie_ir_converter_set_dest_file(
|
||||
struct mie_ir_converter *converter, FILE *file);
|
||||
|
||||
MIE_API b_status mie_ir_converter_process(struct mie_ir_converter *converter);
|
||||
|
||||
#endif
|
||||
@@ -1,32 +0,0 @@
|
||||
#ifndef MIE_DATA_H_
|
||||
#define MIE_DATA_H_
|
||||
|
||||
#include <mie/ir/value.h>
|
||||
|
||||
#define MIE_DATA(p) ((struct mie_data *)(p))
|
||||
|
||||
enum mie_data_type {
|
||||
MIE_DATA_NONE = 0,
|
||||
MIE_DATA_EXTERN_GLOBAL,
|
||||
MIE_DATA_CONST,
|
||||
};
|
||||
|
||||
struct mie_data {
|
||||
struct mie_value d_base;
|
||||
enum mie_data_type d_type;
|
||||
union {
|
||||
struct {
|
||||
struct mie_const *c_value;
|
||||
} d_const;
|
||||
|
||||
struct {
|
||||
struct mie_type *g_type;
|
||||
} d_extern_global;
|
||||
};
|
||||
};
|
||||
|
||||
extern struct mie_data *mie_data_create_extern_global(
|
||||
struct mie_type *type, const char *ident);
|
||||
extern struct mie_data *mie_data_create_const(struct mie_const *value);
|
||||
|
||||
#endif
|
||||
@@ -1,47 +0,0 @@
|
||||
#ifndef MIE_FUNC_H_
|
||||
#define MIE_FUNC_H_
|
||||
|
||||
#define MIE_FUNC(p) ((struct mie_func *)(p))
|
||||
|
||||
#include <mie/ir/value.h>
|
||||
|
||||
struct mie_name_map;
|
||||
struct mie_type;
|
||||
struct mie_arg;
|
||||
struct mie_block;
|
||||
struct b_dict;
|
||||
|
||||
enum mie_func_type {
|
||||
MIE_FUNC_NONE = 0x00u,
|
||||
MIE_FUNC_STATIC = 0x01u,
|
||||
MIE_FUNC_INSTANCE = 0x02u,
|
||||
MIE_FUNC_LAMBDA = 0x03u,
|
||||
};
|
||||
|
||||
struct mie_func {
|
||||
struct mie_value f_base;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
extern struct mie_func *mie_func_create(
|
||||
enum mie_func_type type, struct mie_type *ret_type);
|
||||
extern struct mie_value *mie_func_add_arg(
|
||||
struct mie_func *func, struct mie_type *type, const char *name);
|
||||
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);
|
||||
|
||||
extern struct mie_block *mie_func_get_first_block(struct mie_func *func);
|
||||
extern struct mie_block *mie_func_get_last_block(struct mie_func *func);
|
||||
|
||||
#endif
|
||||
@@ -1,45 +0,0 @@
|
||||
#ifndef MIE_INSTR_H_
|
||||
#define MIE_INSTR_H_
|
||||
|
||||
#include <mie/ir/value.h>
|
||||
|
||||
#define MIE_INSTR(p) ((struct mie_instr *)(p))
|
||||
|
||||
enum mie_instr_type {
|
||||
MIE_INSTR_NONE = 0,
|
||||
MIE_INSTR_RET,
|
||||
MIE_INSTR_ADD,
|
||||
MIE_INSTR_SUB,
|
||||
MIE_INSTR_MUL,
|
||||
MIE_INSTR_DIV,
|
||||
MIE_INSTR_LOAD,
|
||||
MIE_INSTR_STORE,
|
||||
MIE_INSTR_ALLOCA,
|
||||
MIE_INSTR_SWITCH,
|
||||
MIE_INSTR_BR,
|
||||
MIE_INSTR_BR_IF,
|
||||
MIE_INSTR_MSG,
|
||||
MIE_INSTR_CMP_EQ,
|
||||
MIE_INSTR_CMP_NEQ,
|
||||
MIE_INSTR_CMP_LT,
|
||||
MIE_INSTR_CMP_GT,
|
||||
MIE_INSTR_CMP_LEQ,
|
||||
MIE_INSTR_CMP_GEQ,
|
||||
MIE_INSTR_GETELEMENTPTR,
|
||||
MIE_INSTR_SETELEMENTPTR,
|
||||
MIE_INSTR_PHI,
|
||||
};
|
||||
|
||||
struct mie_instr {
|
||||
struct mie_value i_base;
|
||||
enum mie_instr_type i_type;
|
||||
};
|
||||
|
||||
struct mie_ret {
|
||||
struct mie_instr r_base;
|
||||
struct mie_value *r_val;
|
||||
};
|
||||
|
||||
extern void mie_instr_init(struct mie_instr *instr, enum mie_instr_type type);
|
||||
|
||||
#endif
|
||||
@@ -1,37 +1,21 @@
|
||||
#ifndef MIE_MODULE_H_
|
||||
#define MIE_MODULE_H_
|
||||
#ifndef MIE_IR_MODULE_H_
|
||||
#define MIE_IR_MODULE_H_
|
||||
|
||||
#define MIE_MODULE(p) ((struct mie_module *)(p))
|
||||
|
||||
#include <blue/core/queue.h>
|
||||
#include <blue/ds/hashmap.h>
|
||||
#include <mie/ir/value.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/name.h>
|
||||
#include <mie/vector.h>
|
||||
#include <stddef.h>
|
||||
|
||||
struct mie_func;
|
||||
struct mie_op;
|
||||
|
||||
struct mie_module {
|
||||
struct mie_value m_base;
|
||||
struct mie_name_map *m_names;
|
||||
b_queue m_records;
|
||||
b_queue m_types;
|
||||
b_queue m_func;
|
||||
|
||||
b_hashmap *m_data;
|
||||
b_hashmap *m_data_strings;
|
||||
MIE_VECTOR_DECLARE(struct mie_op, m_ops);
|
||||
};
|
||||
|
||||
extern struct mie_module *mie_module_create(void);
|
||||
extern void mie_module_add_function(
|
||||
struct mie_module *mod, struct mie_func *func, const char *name);
|
||||
extern struct mie_data *mie_module_get_string_ptr(
|
||||
struct mie_module *mod, const char *s);
|
||||
extern struct mie_data *mie_module_get_data(
|
||||
struct mie_module *mod, const char *name);
|
||||
extern enum b_status mie_module_put_data(
|
||||
struct mie_module *mod, struct mie_data *data, const char *name);
|
||||
MIE_API struct mie_module *mie_module_create(void);
|
||||
MIE_API void mie_module_destroy(struct mie_module *mod);
|
||||
|
||||
extern struct mie_value *mie_module_generate_value_name(
|
||||
struct mie_module *mod, struct mie_value *val, const char *hint);
|
||||
MIE_API struct mie_op *mie_module_add_op(struct mie_module *mod);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
#ifndef MIE_MSG_H_
|
||||
#define MIE_MSG_H_
|
||||
|
||||
#include <mie/ir/instr.h>
|
||||
#include <mie/type.h>
|
||||
|
||||
#define MIE_MSG(p) ((struct mie_msg *)(p))
|
||||
|
||||
struct mie_msg {
|
||||
struct mie_instr msg_base;
|
||||
struct mie_type *msg_ret_type;
|
||||
struct mie_value *msg_recipient;
|
||||
struct mie_value *msg_selector;
|
||||
size_t msg_nr_args;
|
||||
struct mie_value **msg_args;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,13 +1,68 @@
|
||||
#ifndef MIE_ARITH_H_
|
||||
#define MIE_ARITH_H_
|
||||
#ifndef MIE_IR_OP_H_
|
||||
#define MIE_IR_OP_H_
|
||||
|
||||
#include <mie/ir/instr.h>
|
||||
#include "mie/vector.h"
|
||||
|
||||
struct mie_binary_op {
|
||||
struct mie_instr op_base;
|
||||
struct mie_type *op_type;
|
||||
struct mie_value *op_left;
|
||||
struct mie_value *op_right;
|
||||
#include <mie/ir/register.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/name.h>
|
||||
#include <mie/parse/file-span.h>
|
||||
|
||||
struct mie_type;
|
||||
|
||||
struct mie_value;
|
||||
struct mie_block;
|
||||
struct mie_region;
|
||||
|
||||
struct mie_dialect;
|
||||
struct mie_dialect_op;
|
||||
|
||||
enum mie_op_flags {
|
||||
MIE_OP_F_OP_RESOLVED = 0x01u,
|
||||
MIE_OP_F_ARGS_RESOLVED = 0x02u,
|
||||
};
|
||||
|
||||
struct mie_op_successor {
|
||||
struct mie_block *s_block;
|
||||
MIE_VECTOR_DECLARE(struct mie_register *, s_params);
|
||||
};
|
||||
|
||||
struct mie_op_attribute {
|
||||
char *attrib_name;
|
||||
struct mie_value *attrib_value;
|
||||
};
|
||||
|
||||
struct mie_op_arg {
|
||||
struct mie_file_span arg_span;
|
||||
union {
|
||||
/* only valid if the parent op's F_ARGS_RESOLVED flag is set */
|
||||
struct mie_register *arg_value;
|
||||
/* only valid if the parent op's F_ARGS_RESOLVED flag is NOT set */
|
||||
struct mie_register_ref arg_unresolved;
|
||||
};
|
||||
};
|
||||
|
||||
struct mie_op {
|
||||
enum mie_op_flags op_flags;
|
||||
|
||||
/* these pointers are only valid if the F_RESOLVED flag is set */
|
||||
const struct mie_dialect *op_dialect;
|
||||
const struct mie_dialect_op *op_info;
|
||||
|
||||
struct mie_file_span op_name_span;
|
||||
/* only valid if the F_RESOLVED flag is NOT set */
|
||||
char *op_name;
|
||||
|
||||
MIE_VECTOR_DECLARE(struct mie_region, op_regions);
|
||||
MIE_VECTOR_DECLARE(struct mie_op_successor, op_successors);
|
||||
MIE_VECTOR_DECLARE(struct mie_op_attribute, op_attrib);
|
||||
MIE_VECTOR_DECLARE(struct mie_op_arg, op_args);
|
||||
MIE_VECTOR_DECLARE(struct mie_register, op_result);
|
||||
};
|
||||
|
||||
MIE_API void mie_op_destroy(struct mie_op *op);
|
||||
|
||||
MIE_API struct mie_region *mie_op_add_region(struct mie_op *op);
|
||||
MIE_API struct mie_op_successor *mie_op_add_successor(struct mie_op *op);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
#ifndef MIE_PHI_H_
|
||||
#define MIE_PHI_H_
|
||||
|
||||
#include <blue/core/queue.h>
|
||||
#include <mie/ir/instr.h>
|
||||
#include <mie/misc.h>
|
||||
|
||||
struct mie_phi_edge {
|
||||
b_queue_entry e_entry;
|
||||
struct mie_block *e_incoming_block;
|
||||
struct mie_value *e_value;
|
||||
};
|
||||
|
||||
struct mie_phi {
|
||||
struct mie_instr p_base;
|
||||
struct mie_type *p_type;
|
||||
|
||||
unsigned int p_nr_edges;
|
||||
struct mie_phi_edge *p_edges;
|
||||
};
|
||||
|
||||
MIE_API struct mie_phi_edge *mie_phi_edge_create(
|
||||
struct mie_block *incoming_block, struct mie_value *value);
|
||||
|
||||
#endif
|
||||
@@ -1,33 +0,0 @@
|
||||
#ifndef MIE_PTR_H_
|
||||
#define MIE_PTR_H_
|
||||
|
||||
#include <mie/ir/instr.h>
|
||||
#include <mie/type.h>
|
||||
|
||||
struct mie_getelementptr {
|
||||
struct mie_instr gep_base;
|
||||
struct mie_type *gep_container_type;
|
||||
struct mie_value *gep_container;
|
||||
struct mie_value *gep_index;
|
||||
};
|
||||
|
||||
struct mie_setelementptr {
|
||||
struct mie_instr sep_base;
|
||||
struct mie_type *sep_container_type;
|
||||
struct mie_value *sep_container;
|
||||
struct mie_value *sep_index;
|
||||
};
|
||||
|
||||
struct mie_load {
|
||||
struct mie_instr l_base;
|
||||
struct mie_type *l_type;
|
||||
struct mie_value *l_src;
|
||||
};
|
||||
|
||||
struct mie_store {
|
||||
struct mie_instr s_base;
|
||||
struct mie_value *s_val;
|
||||
struct mie_value *s_dest;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,16 +0,0 @@
|
||||
#ifndef MIE_RECORD_H_
|
||||
#define MIE_RECORD_H_
|
||||
|
||||
#define MIE_RECORD(p) ((struct mie_record *)(p))
|
||||
|
||||
#include <mie/ir/const.h>
|
||||
#include <mie/ir/value.h>
|
||||
|
||||
struct mie_record {
|
||||
struct mie_value r_base;
|
||||
const struct mie_const *r_value;
|
||||
};
|
||||
|
||||
extern struct mie_record *mie_record_create(const struct mie_const *val);
|
||||
|
||||
#endif
|
||||
17
mie/include/mie/ir/region.h
Normal file
17
mie/include/mie/ir/region.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef MIE_IR_REGION_H_
|
||||
#define MIE_IR_REGION_H_
|
||||
|
||||
#include <mie/misc.h>
|
||||
#include <mie/vector.h>
|
||||
#include <stddef.h>
|
||||
|
||||
struct mie_block;
|
||||
|
||||
struct mie_region {
|
||||
struct mie_name_map *r_names;
|
||||
MIE_VECTOR_DECLARE(struct mie_block, r_blocks);
|
||||
};
|
||||
|
||||
MIE_API struct mie_block *mie_region_add_block(struct mie_region *region);
|
||||
|
||||
#endif
|
||||
51
mie/include/mie/ir/register.h
Normal file
51
mie/include/mie/ir/register.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef MIE_IR_REGISTER_H_
|
||||
#define MIE_IR_REGISTER_H_
|
||||
|
||||
#include <mie/name.h>
|
||||
|
||||
struct mie_target_register;
|
||||
|
||||
enum mie_register_flags {
|
||||
MIE_REGISTER_F_NONE = 0,
|
||||
MIE_REGISTER_F_OP_RESULT = 0x01u,
|
||||
MIE_REGISTER_F_OP_PARAM = 0x02u,
|
||||
MIE_REGISTER_F_BLOCK_PARAM = 0x04u,
|
||||
|
||||
/* only ONE of these two flags can be set at a time */
|
||||
MIE_REGISTER_F_VIRTUAL = 0x10u,
|
||||
MIE_REGISTER_F_MACHINE = 0x20u,
|
||||
};
|
||||
|
||||
/* represents a yet-to-be-resolved reference to a mie_register */
|
||||
struct mie_register_ref {
|
||||
enum mie_register_flags reg_flags;
|
||||
char *reg_name;
|
||||
/* what we THINK the type of the register is.
|
||||
* if this is set, it gets checked during the register resolution process. */
|
||||
const struct mie_type *reg_type;
|
||||
};
|
||||
|
||||
struct mie_register {
|
||||
enum mie_register_flags reg_flags;
|
||||
union {
|
||||
/* only valid if F_VIRTUAL is set. */
|
||||
struct mie_name reg_name;
|
||||
/* only valid if F_MACHINE is set. */
|
||||
struct mie_target_register *reg_mach;
|
||||
};
|
||||
const struct mie_type *reg_type;
|
||||
/* if this is an OP_RESULT register, this points to the block within
|
||||
* which this register is defined.
|
||||
* if this is an OP_PARAM register, this pointer is NULL.
|
||||
* if this is a BLOCK_PARAM register, this points to the block for
|
||||
* which this register is a parameter. */
|
||||
struct mie_block *reg_block;
|
||||
/* if this is an OP_RESULT register, this points to the operation
|
||||
* that defines the register.
|
||||
* if this is an OP_PARAM register, this points to the operation
|
||||
* for which this register is a parameter
|
||||
* if this is a BLOCK_PARAM register, this pointer is NULL */
|
||||
struct mie_op *reg_op;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,21 +0,0 @@
|
||||
#ifndef MIE_SWITCH_H_
|
||||
#define MIE_SWITCH_H_
|
||||
|
||||
#include <mie/ir/const.h>
|
||||
#include <mie/ir/instr.h>
|
||||
|
||||
struct mie_switch_branch {
|
||||
struct mie_value *b_value;
|
||||
struct mie_block *b_jump_to;
|
||||
};
|
||||
|
||||
struct mie_switch {
|
||||
struct mie_instr s_base;
|
||||
struct mie_value *s_src;
|
||||
struct mie_block *s_default;
|
||||
|
||||
size_t s_nr_branches;
|
||||
struct mie_switch_branch *s_branches;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,60 +0,0 @@
|
||||
#ifndef MIE_VALUE_H_
|
||||
#define MIE_VALUE_H_
|
||||
|
||||
#include <blue/core/queue.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/name.h>
|
||||
|
||||
#define MIE_VALUE(p) ((struct mie_value *)(p))
|
||||
|
||||
struct mie_ctx;
|
||||
struct mie_value;
|
||||
|
||||
enum mie_value_type_id {
|
||||
MIE_VALUE_NONE = 0,
|
||||
MIE_VALUE_MODULE,
|
||||
MIE_VALUE_TYPE,
|
||||
MIE_VALUE_RECORD,
|
||||
MIE_VALUE_FUNC,
|
||||
MIE_VALUE_ARG,
|
||||
MIE_VALUE_BLOCK,
|
||||
MIE_VALUE_INSTR,
|
||||
MIE_VALUE_CONST,
|
||||
MIE_VALUE_DATA,
|
||||
};
|
||||
|
||||
enum mie_value_flags {
|
||||
MIE_VALUE_F_NONE = 0x00u,
|
||||
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_ctx *);
|
||||
void (*t_cleanup)(struct mie_value *);
|
||||
};
|
||||
|
||||
struct mie_value {
|
||||
struct mie_name v_name;
|
||||
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_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, struct mie_ctx *ctx);
|
||||
|
||||
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
|
||||
83
mie/include/mie/macros.h
Normal file
83
mie/include/mie/macros.h
Normal file
@@ -0,0 +1,83 @@
|
||||
#ifndef MIE_MACROS_H_
|
||||
#define MIE_MACROS_H_
|
||||
|
||||
#define __MIE_DIALECT_BEGIN(func_prefix, dialect_name) \
|
||||
struct mie_dialect *func_prefix##_dialect_create(struct mie_ctx *ctx) \
|
||||
{ \
|
||||
struct mie_dialect *self = mie_dialect_create(ctx, dialect_name); \
|
||||
if (!self) { \
|
||||
return NULL; \
|
||||
} \
|
||||
struct mie_dialect_op *op = NULL; \
|
||||
struct mie_dialect_type *type = NULL;
|
||||
|
||||
#define __MIE_DIALECT_END() \
|
||||
return self; \
|
||||
}
|
||||
|
||||
#define __MIE_DIALECT_OP_BEGIN(func_prefix, op_name) \
|
||||
struct mie_dialect_op *func_prefix##_op_create( \
|
||||
struct mie_dialect *d, struct mie_ctx *ctx) \
|
||||
{ \
|
||||
struct mie_dialect_op *op = mie_dialect_op_create(d, op_name); \
|
||||
if (!op) { \
|
||||
return NULL; \
|
||||
}
|
||||
|
||||
#define __MIE_DIALECT_OP_END() \
|
||||
return op; \
|
||||
}
|
||||
|
||||
#define __MIE_DIALECT_TYPE_BEGIN(func_prefix, type_name) \
|
||||
struct mie_dialect_type *func_prefix##_type_create( \
|
||||
struct mie_dialect *d, struct mie_ctx *ctx) \
|
||||
{ \
|
||||
struct mie_dialect_type *type \
|
||||
= mie_dialect_type_create(d, type_name); \
|
||||
if (!type) { \
|
||||
return NULL; \
|
||||
}
|
||||
|
||||
#define __MIE_DIALECT_TYPE_END() \
|
||||
return type; \
|
||||
}
|
||||
|
||||
#define __MIE_DIALECT_ADD_OP(op_id) \
|
||||
extern struct mie_dialect_op *op_id##_op_create( \
|
||||
struct mie_dialect *, struct mie_ctx *); \
|
||||
op = op_id##_op_create(self, ctx)
|
||||
#define __MIE_DIALECT_ADD_TYPE(type_id) \
|
||||
extern struct mie_dialect_type *type_id##_type_create( \
|
||||
struct mie_dialect *, struct mie_ctx *); \
|
||||
type = type_id##_type_create(self, ctx)
|
||||
|
||||
#define MIE_DIALECT_BEGIN(c_sym, name) __MIE_DIALECT_BEGIN(c_sym, name)
|
||||
#define MIE_DIALECT_END() __MIE_DIALECT_END()
|
||||
#define MIE_DIALECT_ADD_OP(c_sym) __MIE_DIALECT_ADD_OP(c_sym)
|
||||
#define MIE_DIALECT_ADD_TYPE(c_sym) __MIE_DIALECT_ADD_TYPE(c_sym)
|
||||
|
||||
#define MIE_DIALECT_OP_BEGIN(c_sym, op) __MIE_DIALECT_OP_BEGIN(c_sym, op)
|
||||
#define MIE_DIALECT_OP_END() __MIE_DIALECT_OP_END()
|
||||
#define MIE_DIALECT_OP_PRINT(func) op->op_print = (func)
|
||||
#define MIE_DIALECT_OP_PARSE(func) op->op_parse = (func)
|
||||
|
||||
#define MIE_DIALECT_TYPE_BEGIN(c_sym, type) \
|
||||
__MIE_DIALECT_TYPE_BEGIN(c_sym, type)
|
||||
#define MIE_DIALECT_TYPE_END() __MIE_DIALECT_TYPE_END()
|
||||
#define MIE_DIALECT_TYPE_FLAGS(flags) type->ty_flags = flags
|
||||
#define MIE_DIALECT_TYPE_STRUCT(name) type->ty_data_size = sizeof(name)
|
||||
#define MIE_DIALECT_TYPE_INIT(func) type->ty_init = (func)
|
||||
#define MIE_DIALECT_TYPE_PRINT(func) type->ty_print = (func)
|
||||
#define MIE_DIALECT_TYPE_VALUE_PRINT(func) type->ty_value_print = (func)
|
||||
#define MIE_DIALECT_TYPE_PARSE(func) type->ty_parse = (func)
|
||||
#define MIE_DIALECT_TYPE_BUILD_ID(func) type->ty_build_id = (func)
|
||||
#define MIE_DIALECT_TYPE_INIT(func) type->ty_init = (func)
|
||||
#define MIE_DIALECT_TYPE_CLEANUP(func) type->ty_cleanup = (func)
|
||||
|
||||
#define MIE_DIALECT_TRAIT_BEGIN(c_sym, trait)
|
||||
#define MIE_DIALECT_TRAIT_END()
|
||||
#define MIE_DIALECT_TRAIT_STRUCT(name)
|
||||
#define MIE_DIALECT_TRAIT_PRINT(func)
|
||||
#define MIE_DIALECT_TRAIT_CLEANUP(func)
|
||||
|
||||
#endif
|
||||
@@ -11,7 +11,9 @@
|
||||
#define MIE_API extern
|
||||
#endif
|
||||
|
||||
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
||||
#define MAX(x, y) ((x) > (y) ? (x) : (y))
|
||||
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
||||
#define MAX(x, y) ((x) > (y) ? (x) : (y))
|
||||
|
||||
#define MIE_TEST_FLAGS(value, flags) (((value) & (flags)) == (flags))
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,13 +3,21 @@
|
||||
|
||||
#include <blue/core/btree.h>
|
||||
#include <blue/core/queue.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/parse/file-span.h>
|
||||
|
||||
#define MIE_NAME_VALID(p) (((p)->n_str) != NULL)
|
||||
|
||||
struct mie_name_map;
|
||||
|
||||
enum mie_name_map_flags {
|
||||
MIE_NAME_MAP_STRICT = 0x01u,
|
||||
/* put(): take the name hint as the required name, and don't amend it
|
||||
* to ensure uniqueness. if the given name already exists, the operation
|
||||
* fails. */
|
||||
MIE_NAME_MAP_F_STRICT = 0x01u,
|
||||
/* get(): if the given name does not exist in the name map, recursively
|
||||
* search the parent map(s) until a result is found. */
|
||||
MIE_NAME_MAP_F_RECURSIVE = 0x02u,
|
||||
};
|
||||
|
||||
enum mie_name_map_entry_type {
|
||||
@@ -31,6 +39,10 @@ struct mie_name {
|
||||
struct mie_name_map_entry n_base;
|
||||
struct mie_name_map *n_parent;
|
||||
char *n_str;
|
||||
|
||||
/* if this name was read from a file, these structs can be used to
|
||||
* record the location within the file where the name is found. */
|
||||
struct mie_file_span n_start, n_end;
|
||||
};
|
||||
|
||||
struct mie_name_bucket {
|
||||
@@ -39,13 +51,17 @@ struct mie_name_bucket {
|
||||
};
|
||||
|
||||
struct mie_name_map {
|
||||
const struct mie_name_map *m_parent;
|
||||
b_btree m_entries;
|
||||
size_t m_next_id;
|
||||
};
|
||||
|
||||
extern struct mie_name_map *mie_name_map_create(void);
|
||||
extern struct mie_name_map *mie_name_map_create(const struct mie_name_map *parent);
|
||||
extern void mie_name_map_destroy(struct mie_name_map *map);
|
||||
|
||||
extern const struct mie_name *mie_name_map_get(
|
||||
const struct mie_name_map *map, const char *name,
|
||||
enum mie_name_map_flags flags);
|
||||
extern struct mie_name *mie_name_map_put(
|
||||
struct mie_name_map *map, struct mie_name *entry, const char *hint,
|
||||
enum mie_name_map_flags flags);
|
||||
|
||||
23
mie/include/mie/parse/file-span.h
Normal file
23
mie/include/mie/parse/file-span.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef MIE_PARSE_FILE_SPAN_H_
|
||||
#define MIE_PARSE_FILE_SPAN_H_
|
||||
|
||||
#include <mie/misc.h>
|
||||
|
||||
struct mie_token;
|
||||
|
||||
/* a single row/column pair, representing a location within a file.
|
||||
* both values are 1-based (i.e. the first character in a file is at c_row=1,
|
||||
* c_col=1*/
|
||||
struct mie_file_cell {
|
||||
unsigned int c_row, c_col;
|
||||
};
|
||||
|
||||
/* represents a span of characters within a file.
|
||||
* used for diagnostic reporting */
|
||||
struct mie_file_span {
|
||||
struct mie_file_cell s_start, s_end;
|
||||
};
|
||||
|
||||
MIE_API void mie_file_span_init(struct mie_file_span *span, struct mie_token *tok);
|
||||
|
||||
#endif
|
||||
18
mie/include/mie/parse/lex.h
Normal file
18
mie/include/mie/parse/lex.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef MIE_PARSE_LEX_H_
|
||||
#define MIE_PARSE_LEX_H_
|
||||
|
||||
#include <blue/core/stream.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/status.h>
|
||||
|
||||
struct mie_lex;
|
||||
struct mie_token;
|
||||
|
||||
MIE_API struct mie_lex *mie_lex_create(b_stream *src);
|
||||
MIE_API void mie_lex_destroy(struct mie_lex *lex);
|
||||
|
||||
MIE_API enum mie_status mie_lex_get_status(const struct mie_lex *lex);
|
||||
MIE_API struct mie_token *mie_lex_peek(struct mie_lex *lex);
|
||||
MIE_API void mie_lex_advance(struct mie_lex *lex);
|
||||
|
||||
#endif
|
||||
107
mie/include/mie/parse/parse.h
Normal file
107
mie/include/mie/parse/parse.h
Normal file
@@ -0,0 +1,107 @@
|
||||
#ifndef MIE_PARSE_PARSE_H_
|
||||
#define MIE_PARSE_PARSE_H_
|
||||
|
||||
#include <blue/ds/string.h>
|
||||
#include <mie/ir/register.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/parse/token.h>
|
||||
#include <mie/status.h>
|
||||
#include <mie/vector.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
struct mie_parser;
|
||||
struct mie_name_map;
|
||||
struct mie_lex;
|
||||
struct mie_ctx;
|
||||
|
||||
struct mie_type;
|
||||
struct mie_module;
|
||||
struct mie_region;
|
||||
struct mie_op;
|
||||
struct mie_op_arg;
|
||||
struct mie_op_attribute;
|
||||
struct mie_op_successor;
|
||||
struct mie_register;
|
||||
|
||||
MIE_API struct mie_parser *mie_parser_create(
|
||||
struct mie_ctx *ctx, struct mie_lex *lex);
|
||||
MIE_API void mie_parser_destroy(struct mie_parser *ctx);
|
||||
MIE_API enum mie_status mie_parser_get_status(const struct mie_parser *ctx);
|
||||
|
||||
MIE_API struct mie_token *mie_parser_peek(struct mie_parser *ctx);
|
||||
MIE_API enum mie_token_type mie_parser_peek_type(struct mie_parser *ctx);
|
||||
MIE_API enum mie_token_symbol mie_parser_peek_symbol(struct mie_parser *ctx);
|
||||
MIE_API bool mie_parser_advance(struct mie_parser *ctx);
|
||||
|
||||
MIE_API bool mie_parser_check_eof(struct mie_parser *ctx);
|
||||
MIE_API bool mie_parser_check_type(struct mie_parser *ctx, enum mie_token_type type);
|
||||
MIE_API bool mie_parser_check_symbol(
|
||||
struct mie_parser *ctx, enum mie_token_symbol sym);
|
||||
|
||||
MIE_API bool mie_parser_parse_word(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_instname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_graphname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_opname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_vregname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_mregname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_blockname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_typename(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_symname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_string(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_keyword(struct mie_parser *ctx, const char *kw);
|
||||
MIE_API bool mie_parser_parse_symbol(
|
||||
struct mie_parser *ctx, enum mie_token_symbol sym);
|
||||
|
||||
MIE_API bool mie_parser_parse_type(
|
||||
struct mie_parser *ctx, const struct mie_type **out);
|
||||
MIE_API bool mie_parser_parse_type_list(
|
||||
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(const struct mie_type *, out));
|
||||
MIE_API bool mie_parser_parse_function_type(
|
||||
struct mie_parser *ctx, struct mie_type **out);
|
||||
|
||||
MIE_API bool mie_parser_parse_operand(
|
||||
struct mie_parser *ctx, struct mie_op_arg *out);
|
||||
MIE_API bool mie_parser_parse_operand_list(
|
||||
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(struct mie_op_arg, out));
|
||||
|
||||
MIE_API bool mie_parser_parse_unknown_keyword(struct mie_parser *ctx, b_string *out);
|
||||
MIE_API bool mie_parser_parse_unknown_symbol(
|
||||
struct mie_parser *ctx, enum mie_token_symbol sym);
|
||||
|
||||
MIE_API bool mie_parser_parse_register(
|
||||
struct mie_parser *ctx, struct mie_name_map *names,
|
||||
struct mie_register *out);
|
||||
MIE_API bool mie_parser_parse_register_list(
|
||||
struct mie_parser *ctx, struct mie_name_map *names,
|
||||
MIE_VECTOR_REF_PARAM(struct mie_register, out));
|
||||
|
||||
MIE_API bool mie_parser_parse_region(
|
||||
struct mie_parser *ctx, struct mie_region *region);
|
||||
MIE_API bool mie_parser_parse_region_list(
|
||||
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(struct mie_region, out));
|
||||
|
||||
MIE_API bool mie_parser_parse_attribute(
|
||||
struct mie_parser *ctx, struct mie_op_attribute *attrib);
|
||||
MIE_API bool mie_parser_parse_attribute_list(
|
||||
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(struct mie_op_attribute, out));
|
||||
|
||||
MIE_API bool mie_parser_parse_successor(
|
||||
struct mie_parser *ctx, struct mie_op_successor *successor);
|
||||
MIE_API bool mie_parser_parse_successor_list(
|
||||
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(struct mie_op_successor, out));
|
||||
|
||||
MIE_API bool mie_parser_parse_module(struct mie_parser *ctx, struct mie_module *mod);
|
||||
MIE_API bool mie_parser_parse_op(
|
||||
struct mie_parser *ctx, struct mie_name_map *names, struct mie_op *dest);
|
||||
|
||||
#endif
|
||||
94
mie/include/mie/parse/token.h
Normal file
94
mie/include/mie/parse/token.h
Normal file
@@ -0,0 +1,94 @@
|
||||
#ifndef MIE_PARSE_TOKEN_H_
|
||||
#define MIE_PARSE_TOKEN_H_
|
||||
|
||||
#include <blue/core/queue.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/parse/file-span.h>
|
||||
|
||||
#define MIE_TOKEN_TYPE(tok) ((tok) ? (tok)->tok_type : MIE_TOK_NONE)
|
||||
#define MIE_TOKEN_IS(tok, type) ((tok) && ((tok)->tok_type & (type)) != 0)
|
||||
#define MIE_TOKEN_IS_SYMBOL(tok, sym) \
|
||||
((tok) && (tok)->tok_type == MIE_TOK_SYMBOL && (tok)->tok_sym == (sym))
|
||||
|
||||
enum mie_token_type {
|
||||
MIE_TOK_NONE = 0,
|
||||
MIE_TOK_LINEFEED = 0x0001u,
|
||||
MIE_TOK_INT = 0x0002u,
|
||||
MIE_TOK_DOUBLE = 0x0004u,
|
||||
MIE_TOK_SYMBOL = 0x0008u,
|
||||
MIE_TOK_STRING = 0x0010u,
|
||||
/* single words, not dot-delimited */
|
||||
MIE_TOK_WORD = 0x0020u,
|
||||
/* set of words with at least one dot */
|
||||
MIE_TOK_NAME = 0x0040u,
|
||||
/* word or name, prefixed with an * asterisk */
|
||||
MIE_TOK_INSTNAME = 0x0080u,
|
||||
/* word or name, prefixed with an @ at */
|
||||
MIE_TOK_SYMNAME = 0x0100u,
|
||||
/* word or name, prefixed with a ~ tilde */
|
||||
MIE_TOK_OPNAME = 0x0200u,
|
||||
/* word or name, prefixed with a + plus */
|
||||
MIE_TOK_GRAPHNAME = 0x0400u,
|
||||
/* word or name, prefixed with a % percent */
|
||||
MIE_TOK_VREGNAME = 0x0800u,
|
||||
/* word or name, prefixed with a $ dollar */
|
||||
MIE_TOK_MREGNAME = 0x1000u,
|
||||
/* word or name, prefixed with a ^ caret */
|
||||
MIE_TOK_BLOCKNAME = 0x2000u,
|
||||
/* word or name, prefixed with a # hash */
|
||||
MIE_TOK_TYPENAME = 0x4000u,
|
||||
};
|
||||
|
||||
enum mie_token_value_type {
|
||||
MIE_TOK_V_NONE = 0,
|
||||
MIE_TOK_V_INT,
|
||||
MIE_TOK_V_DOUBLE,
|
||||
MIE_TOK_V_STRING,
|
||||
MIE_TOK_V_SYMBOL,
|
||||
};
|
||||
|
||||
enum mie_token_symbol {
|
||||
MIE_SYM_NONE = 0,
|
||||
MIE_SYM_COLON,
|
||||
MIE_SYM_EQUAL,
|
||||
MIE_SYM_COMMA,
|
||||
MIE_SYM_HYPHEN,
|
||||
MIE_SYM_ASTERISK,
|
||||
MIE_SYM_PLUS,
|
||||
MIE_SYM_PERCENT,
|
||||
MIE_SYM_DOLLAR,
|
||||
MIE_SYM_CARET,
|
||||
MIE_SYM_HASH,
|
||||
MIE_SYM_TILDE,
|
||||
MIE_SYM_ATSIGN,
|
||||
MIE_SYM_LEFT_BRACE,
|
||||
MIE_SYM_RIGHT_BRACE,
|
||||
MIE_SYM_LEFT_BRACKET,
|
||||
MIE_SYM_RIGHT_BRACKET,
|
||||
MIE_SYM_LEFT_PAREN,
|
||||
MIE_SYM_RIGHT_PAREN,
|
||||
MIE_SYM_LEFT_ANGLE,
|
||||
MIE_SYM_RIGHT_ANGLE,
|
||||
MIE_SYM_HYPHEN_RIGHT_ANGLE,
|
||||
MIE_SYM_OTHER,
|
||||
};
|
||||
|
||||
struct mie_token {
|
||||
struct mie_file_span tok_location;
|
||||
enum mie_token_type tok_type;
|
||||
enum mie_token_value_type tok_value_type;
|
||||
b_queue_entry tok_entry;
|
||||
|
||||
union {
|
||||
char *tok_str;
|
||||
enum mie_token_symbol tok_sym;
|
||||
long long tok_int;
|
||||
double tok_double;
|
||||
};
|
||||
};
|
||||
|
||||
MIE_API void mie_token_destroy(struct mie_token *tok);
|
||||
MIE_API const char *mie_token_type_to_string(enum mie_token_type type);
|
||||
MIE_API const char *mie_token_symbol_to_string(enum mie_token_symbol sym);
|
||||
|
||||
#endif
|
||||
@@ -1,55 +0,0 @@
|
||||
#ifndef MIE_SELECT_BUILDER_H_
|
||||
#define MIE_SELECT_BUILDER_H_
|
||||
|
||||
#include <mie/misc.h>
|
||||
#include <mie/status.h>
|
||||
|
||||
struct mie_ctx;
|
||||
struct mie_instr;
|
||||
struct mie_value;
|
||||
struct mie_select_builder;
|
||||
struct mie_select_node;
|
||||
struct mie_select_graph;
|
||||
struct mie_select_value;
|
||||
struct mie_target;
|
||||
|
||||
MIE_API struct mie_select_builder *mie_select_builder_create(
|
||||
struct mie_ctx *ctx, const struct mie_target *target);
|
||||
MIE_API void mie_select_builder_destroy(struct mie_select_builder *builder);
|
||||
|
||||
MIE_API struct mie_select_graph *mie_select_builder_get_graph(
|
||||
struct mie_select_builder *builder);
|
||||
MIE_API struct mie_ctx *mie_select_builder_get_ctx(
|
||||
struct mie_select_builder *builder);
|
||||
MIE_API const struct mie_target *mie_select_builder_get_target(
|
||||
struct mie_select_builder *builder);
|
||||
MIE_API struct mie_select_graph *mie_select_builder_finish(
|
||||
struct mie_select_builder *builder);
|
||||
|
||||
MIE_API enum mie_status mie_select_builder_get_const(
|
||||
struct mie_select_builder *builder, long long value,
|
||||
struct mie_select_value *out);
|
||||
|
||||
MIE_API enum mie_status mie_select_builder_push_instr(
|
||||
struct mie_select_builder *builder, struct mie_instr *instr);
|
||||
|
||||
MIE_API struct mie_select_node *mie_select_builder_find_node_with_ivalue(
|
||||
struct mie_select_builder *builder, const struct mie_target *target,
|
||||
unsigned int opcode, long long val);
|
||||
|
||||
MIE_API struct mie_select_value *mie_select_builder_get_value(
|
||||
struct mie_select_builder *builder, struct mie_value *ir_val);
|
||||
MIE_API enum mie_status mie_select_builder_set_value(
|
||||
struct mie_select_builder *builder, struct mie_value *ir_val,
|
||||
struct mie_select_value *graph_val);
|
||||
|
||||
MIE_API struct mie_select_value *mie_select_builder_get_mem_access(
|
||||
struct mie_select_builder *builder, struct mie_value *ir_val);
|
||||
MIE_API enum mie_status mie_select_builder_set_mem_access(
|
||||
struct mie_select_builder *builder, struct mie_value *ir_val,
|
||||
struct mie_select_value *graph_val);
|
||||
|
||||
MIE_API enum mie_status mie_select_builder_collapse_chain_ends(
|
||||
struct mie_select_builder *builder, struct mie_select_value *out);
|
||||
|
||||
#endif
|
||||
@@ -1,54 +0,0 @@
|
||||
#ifndef MIE_SELECT_GRAPH_H_
|
||||
#define MIE_SELECT_GRAPH_H_
|
||||
|
||||
#include <blue/core/btree.h>
|
||||
#include <blue/core/queue.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/target/target.h>
|
||||
|
||||
struct mie_ctx;
|
||||
struct mie_type;
|
||||
struct mie_instr;
|
||||
|
||||
struct mie_select_value {
|
||||
struct mie_select_node *v_node;
|
||||
unsigned int v_index;
|
||||
};
|
||||
|
||||
struct mie_select_use {
|
||||
struct mie_select_node *u_user;
|
||||
struct mie_select_value u_value;
|
||||
b_queue_entry u_entry;
|
||||
};
|
||||
|
||||
struct mie_select_chain_end {
|
||||
struct mie_select_value c_value;
|
||||
b_queue_entry c_entry;
|
||||
};
|
||||
|
||||
struct mie_select_graph {
|
||||
b_queue g_nodes;
|
||||
struct mie_select_node *g_root;
|
||||
struct mie_select_value g_entry;
|
||||
b_queue g_chain_ends;
|
||||
size_t g_frame_index;
|
||||
size_t g_node_id;
|
||||
};
|
||||
|
||||
struct mie_select_use_iterator {
|
||||
b_queue_entry *it_ptr;
|
||||
};
|
||||
|
||||
MIE_API struct mie_select_graph *mie_select_graph_create(struct mie_ctx *ctx);
|
||||
MIE_API void mie_select_graph_destroy(struct mie_select_graph *graph);
|
||||
|
||||
MIE_API enum mie_status mie_select_graph_get_node(
|
||||
struct mie_select_graph *graph, const struct mie_target *target,
|
||||
unsigned int op, struct mie_select_value **operands, size_t nr_operands,
|
||||
struct mie_type **values, size_t nr_values, struct mie_select_node **out);
|
||||
|
||||
MIE_API void mie_select_graph_dump_text(struct mie_select_graph *graph);
|
||||
MIE_API void mie_select_graph_dump_dot(
|
||||
struct mie_select_graph *graph, const char *filename);
|
||||
|
||||
#endif
|
||||
@@ -1,104 +0,0 @@
|
||||
#ifndef MIE_SELECT_NODE_H_
|
||||
#define MIE_SELECT_NODE_H_
|
||||
|
||||
#include <blue/core/queue.h>
|
||||
#include <mie/status.h>
|
||||
#include <mie/type.h>
|
||||
|
||||
#define MIE_SELECT_NODE_OUTPUT_MAX 4
|
||||
|
||||
struct mie_target;
|
||||
struct mie_value;
|
||||
struct mie_select_value;
|
||||
|
||||
enum mie_select_node_flags {
|
||||
MIE_SELECT_NODE_F_IVALUE = 0x01u,
|
||||
MIE_SELECT_NODE_F_PVALUE = 0x02u,
|
||||
};
|
||||
|
||||
struct mie_select_node {
|
||||
size_t n_id;
|
||||
unsigned long n_opcode;
|
||||
enum mie_select_node_flags n_flags;
|
||||
|
||||
char *n_description;
|
||||
|
||||
/* certain "builtin" parameters that can be used by opcodes */
|
||||
struct {
|
||||
struct mie_value *v;
|
||||
long long i;
|
||||
void *p;
|
||||
} n_value;
|
||||
|
||||
/* queue entry, links to mie_select_graph.g_nodes */
|
||||
b_queue_entry n_entry;
|
||||
|
||||
/* linked lists of struct mie_select_use,
|
||||
* listing all the places where this node is being used as an
|
||||
* operand.
|
||||
* these pointers point to data in another node's n_operands,
|
||||
* and the memory belongs to these other nodes, not us.
|
||||
*/
|
||||
b_queue n_use;
|
||||
|
||||
/* array of struct mie_select_use
|
||||
* listing all the nodes that are being used as operands by
|
||||
* this node. other nodes hold pointers to these mie_select_use array
|
||||
* entries as part of their n_use queue.
|
||||
*/
|
||||
struct mie_select_use *n_operands;
|
||||
size_t n_nr_operands;
|
||||
|
||||
/* array of struct mie_type pointers
|
||||
* listing the types of the values that are produced as outputs by
|
||||
* this node. the type pointers themselves are owned by a mie_ctx.
|
||||
*/
|
||||
struct mie_type **n_results;
|
||||
size_t n_nr_results;
|
||||
|
||||
/* the target system that provides the operation described by n_opcode. */
|
||||
const struct mie_target *n_target;
|
||||
};
|
||||
|
||||
struct mie_select_node_iterator {
|
||||
b_queue_entry *it_ptr;
|
||||
};
|
||||
|
||||
static inline struct mie_select_node *mie_select_node_iterator_unbox(
|
||||
struct mie_select_node_iterator *it)
|
||||
{
|
||||
return b_unbox(struct mie_select_node, it->it_ptr, n_entry);
|
||||
}
|
||||
|
||||
MIE_API void mie_select_node_iterator_next(struct mie_select_node_iterator *it);
|
||||
MIE_API bool mie_select_node_iterator_is_valid(
|
||||
const struct mie_select_node_iterator *it);
|
||||
|
||||
MIE_API struct mie_select_node *mie_select_node_create(
|
||||
const struct mie_target *target, unsigned int op,
|
||||
struct mie_type **results, size_t nr_results);
|
||||
MIE_API void mie_select_node_destroy(struct mie_select_node *node);
|
||||
|
||||
MIE_API enum mie_status mie_select_node_set_operands(
|
||||
struct mie_select_node *node, struct mie_select_value *operands,
|
||||
size_t nr_operands);
|
||||
MIE_API enum mie_status mie_select_node_clear_operands(struct mie_select_node *node);
|
||||
|
||||
MIE_API void mie_select_node_get_users(
|
||||
struct mie_select_node *node, struct mie_select_node_iterator *it);
|
||||
MIE_API void mie_select_node_get_uses(
|
||||
struct mie_select_node *node, struct mie_select_node_iterator *it);
|
||||
|
||||
MIE_API enum mie_status mie_select_node_get_value(
|
||||
struct mie_select_node *node, struct mie_type *type, size_t index,
|
||||
struct mie_select_value *out);
|
||||
|
||||
MIE_API enum mie_status mie_select_node_set_description(
|
||||
struct mie_select_node *node, const char *format, ...);
|
||||
|
||||
MIE_API struct mie_select_node *mie_select_node_get_glued_node(
|
||||
struct mie_select_node *node);
|
||||
MIE_API struct mie_select_node *mie_select_node_get_glued_user(
|
||||
struct mie_select_node *node);
|
||||
|
||||
#endif
|
||||
@@ -1,33 +0,0 @@
|
||||
#ifndef MIE_SELECT_OPCODE_H_
|
||||
#define MIE_SELECT_OPCODE_H_
|
||||
|
||||
enum mie_select_opcode {
|
||||
MIE_SELECT_OP_NONE = 0,
|
||||
MIE_SELECT_OP_ENTRY,
|
||||
MIE_SELECT_OP_ROOT,
|
||||
MIE_SELECT_OP_BLOCK,
|
||||
MIE_SELECT_OP_CONSTANT,
|
||||
MIE_SELECT_OP_FRAME_INDEX,
|
||||
MIE_SELECT_OP_REGISTER,
|
||||
MIE_SELECT_OP_COPY_FROM_REG,
|
||||
MIE_SELECT_OP_COPY_TO_REG,
|
||||
MIE_SELECT_OP_GLOBAL_ADDRESS,
|
||||
MIE_SELECT_OP_CHAIN_GROUP,
|
||||
MIE_SELECT_OP_LOAD,
|
||||
MIE_SELECT_OP_STORE,
|
||||
MIE_SELECT_OP_ADD,
|
||||
MIE_SELECT_OP_SUB,
|
||||
MIE_SELECT_OP_MUL,
|
||||
MIE_SELECT_OP_DIV,
|
||||
MIE_SELECT_OP_XOR,
|
||||
MIE_SELECT_OP_CMP_EQ,
|
||||
MIE_SELECT_OP_CMP_NEQ,
|
||||
MIE_SELECT_OP_CMP_LT,
|
||||
MIE_SELECT_OP_CMP_GT,
|
||||
MIE_SELECT_OP_CMP_LEQ,
|
||||
MIE_SELECT_OP_CMP_GEQ,
|
||||
MIE_SELECT_OP_BR,
|
||||
MIE_SELECT_OP_BR_COND,
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,36 +0,0 @@
|
||||
#ifndef MIE_TARGET_SELECT_H_
|
||||
#define MIE_TARGET_SELECT_H_
|
||||
|
||||
#include <mie/misc.h>
|
||||
#include <mie/status.h>
|
||||
#include <stddef.h>
|
||||
|
||||
struct mie_target;
|
||||
struct mie_call;
|
||||
struct mie_msg;
|
||||
struct mie_select_builder;
|
||||
struct mie_select_value;
|
||||
|
||||
struct mie_target_select_ops {
|
||||
size_t (*s_node_name)(
|
||||
const struct mie_target *, unsigned int, char *, size_t);
|
||||
enum mie_status (*s_lower_call)(
|
||||
const struct mie_target *, struct mie_select_builder *,
|
||||
struct mie_call *, struct mie_select_value *);
|
||||
enum mie_status (*s_lower_msg)(
|
||||
const struct mie_target *, struct mie_select_builder *,
|
||||
struct mie_msg *, struct mie_select_value *);
|
||||
};
|
||||
|
||||
MIE_API size_t mie_target_select_node_name(
|
||||
const struct mie_target *target, unsigned int opcode, char *out,
|
||||
size_t max);
|
||||
|
||||
MIE_API enum mie_status mie_target_select_lower_call(
|
||||
const struct mie_target *target, struct mie_select_builder *builder,
|
||||
struct mie_call *call, struct mie_select_value *result);
|
||||
MIE_API enum mie_status mie_target_select_lower_msg(
|
||||
const struct mie_target *target, struct mie_select_builder *builder,
|
||||
struct mie_msg *msg, struct mie_select_value *result);
|
||||
|
||||
#endif
|
||||
@@ -1,14 +0,0 @@
|
||||
#ifndef MIE_TARGET_TARGET_H_
|
||||
#define MIE_TARGET_TARGET_H_
|
||||
|
||||
#include <mie/misc.h>
|
||||
#include <mie/target/select.h>
|
||||
|
||||
struct mie_target {
|
||||
const char *t_name;
|
||||
const struct mie_target_select_ops *t_select;
|
||||
};
|
||||
|
||||
MIE_API const struct mie_target *mie_target_builtin(void);
|
||||
|
||||
#endif
|
||||
39
mie/include/mie/trait/trait-table.h
Normal file
39
mie/include/mie/trait/trait-table.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef MIE_TRAIT_TRAIT_TABLE_H_
|
||||
#define MIE_TRAIT_TRAIT_TABLE_H_
|
||||
|
||||
#include <blue/core/btree.h>
|
||||
#include <mie/id.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/status.h>
|
||||
|
||||
/* for IR objects that support traits (ops, types, etc), this struct can be used
|
||||
* to store and retrieve references to the traits that apply */
|
||||
struct mie_trait_table {
|
||||
struct mie_id_map tr_unique;
|
||||
struct mie_id_map tr_generic;
|
||||
};
|
||||
|
||||
struct mie_trait_table_iterator {
|
||||
const struct mie_trait *it_trait;
|
||||
b_queue_entry *_e;
|
||||
};
|
||||
|
||||
MIE_API void mie_trait_table_init(struct mie_trait_table *out);
|
||||
MIE_API void mie_trait_table_cleanup(struct mie_trait_table *table);
|
||||
|
||||
/* retrieve a trait based on its name. this function can only be used to
|
||||
* retrieve non-parametised traits. to retrieve parametised traits, use
|
||||
* mie_trait_table_get_generic. */
|
||||
MIE_API const struct mie_trait *mie_trait_table_get_unique(
|
||||
const struct mie_trait_table *table, const char *dialect_name,
|
||||
const char *trait_name);
|
||||
MIE_API enum mie_status mie_trait_table_get_generic(
|
||||
const struct mie_trait_table *table, const char *dialect_name,
|
||||
const char *trait_name, struct mie_trait_table_iterator *it);
|
||||
MIE_API enum mie_status mie_trait_table_put(
|
||||
struct mie_trait_table *table, const struct mie_trait *trait);
|
||||
|
||||
MIE_API enum mie_status mie_trait_table_iterator_move_next(
|
||||
struct mie_trait_table_iterator *it);
|
||||
|
||||
#endif
|
||||
42
mie/include/mie/trait/trait.h
Normal file
42
mie/include/mie/trait/trait.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef MIE_TRAIT_TRAIT_H_
|
||||
#define MIE_TRAIT_TRAIT_H_
|
||||
|
||||
#include <mie/id.h>
|
||||
|
||||
struct mie_trait;
|
||||
|
||||
enum mie_trait_flags {
|
||||
/* a trait user can only apply the trait once. this is assumed to be the
|
||||
* case unless a trait is parametised, in which case this flag can be
|
||||
* used to prevent more than one specialisation from being used. */
|
||||
MIE_TRAIT_F_UNIQUE = 0x01u,
|
||||
MIE_TRAIT_F_PARAMETISED = 0x02u,
|
||||
};
|
||||
|
||||
/* used to define a trait */
|
||||
struct mie_trait_definition {
|
||||
/* ID for the generic trait */
|
||||
mie_id tr_id;
|
||||
char *tr_name;
|
||||
enum mie_trait_flags tr_flags;
|
||||
|
||||
/* the size of any corresponding struct mie_trait instances */
|
||||
size_t tr_data_size;
|
||||
|
||||
enum mie_status (*tr_build_id)(
|
||||
const struct mie_trait_definition *, const struct mie_trait *,
|
||||
struct mie_id_builder *);
|
||||
enum mie_status (*tr_print)(
|
||||
const struct mie_trait_definition *, const struct mie_trait *,
|
||||
b_stream *);
|
||||
};
|
||||
|
||||
/* used to link an op/type/etc to a trait. if the trait is parametised, this
|
||||
* struct includes any parameter values. */
|
||||
struct mie_trait {
|
||||
/* this is NOT the same as tr_def->tr_id */
|
||||
mie_id tr_id;
|
||||
const struct mie_trait_definition *tr_def;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,41 +0,0 @@
|
||||
#ifndef MIE_TYPE_H_
|
||||
#define MIE_TYPE_H_
|
||||
|
||||
#define MIE_TYPE(p) ((struct mie_type *)(p))
|
||||
|
||||
#include <mie/ir/value.h>
|
||||
|
||||
enum mie_type_id {
|
||||
MIE_TYPE_VOID = 0x00u,
|
||||
MIE_TYPE_INT = 0x01u,
|
||||
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_SELECTOR = 0x0Au,
|
||||
MIE_TYPE_FUNC = 0x0Bu,
|
||||
MIE_TYPE_GLUE = 0xFEu,
|
||||
MIE_TYPE_OTHER = 0xFFu,
|
||||
__MIE_TYPE_COUNT,
|
||||
};
|
||||
|
||||
struct mie_type {
|
||||
struct mie_value t_base;
|
||||
enum mie_type_id t_id;
|
||||
unsigned int t_count;
|
||||
|
||||
union {
|
||||
unsigned int t_width;
|
||||
b_queue t_vars;
|
||||
};
|
||||
};
|
||||
|
||||
extern struct mie_type *mie_type_create(void);
|
||||
|
||||
MIE_API void mie_type_to_string(const struct mie_type *type, char *out, size_t max);
|
||||
MIE_API bool mie_type_compare(const struct mie_type *a, const struct mie_type *b);
|
||||
|
||||
#endif
|
||||
28
mie/include/mie/type/function.h
Normal file
28
mie/include/mie/type/function.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef MIE_TYPE_FUNCTION_H_
|
||||
#define MIE_TYPE_FUNCTION_H_
|
||||
|
||||
#include <blue/core/stream.h>
|
||||
#include <mie/type/type.h>
|
||||
#include <mie/vector.h>
|
||||
|
||||
struct mie_function_type {
|
||||
struct mie_type func_base;
|
||||
|
||||
MIE_VECTOR_DECLARE(const struct mie_type *, func_in);
|
||||
MIE_VECTOR_DECLARE(const struct mie_type *, func_out);
|
||||
};
|
||||
|
||||
MIE_API struct mie_function_type *mie_function_type_create(void);
|
||||
MIE_API void mie_function_type_add_in_part(
|
||||
struct mie_function_type *ty, const struct mie_type *part);
|
||||
MIE_API void mie_function_type_add_out_part(
|
||||
struct mie_function_type *ty, const struct mie_type *part);
|
||||
|
||||
MIE_API void mie_function_type_print(
|
||||
const struct mie_function_type *type, b_stream *out);
|
||||
MIE_API void mie_function_type_build_id(
|
||||
struct mie_id_builder *builder, const struct mie_type **in_types,
|
||||
size_t nr_in_types, const struct mie_type **out_types,
|
||||
size_t nr_out_types);
|
||||
|
||||
#endif
|
||||
24
mie/include/mie/type/storage.h
Normal file
24
mie/include/mie/type/storage.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef MIE_TYPE_STORAGE_H_
|
||||
#define MIE_TYPE_STORAGE_H_
|
||||
|
||||
#include <blue/core/stream.h>
|
||||
#include <mie/type/type.h>
|
||||
#include <mie/vector.h>
|
||||
|
||||
struct mie_storage_type {
|
||||
struct mie_type st_base;
|
||||
|
||||
MIE_VECTOR_DECLARE(const struct mie_type *, st_parts);
|
||||
};
|
||||
|
||||
MIE_API struct mie_storage_type *mie_storage_type_create(void);
|
||||
MIE_API void mie_storage_type_add_part(
|
||||
struct mie_storage_type *ty, const struct mie_type *part);
|
||||
|
||||
MIE_API void mie_storage_type_print(
|
||||
const struct mie_storage_type *type, b_stream *out);
|
||||
MIE_API void mie_storage_type_build_id(
|
||||
struct mie_id_builder *builder, const struct mie_type **parts,
|
||||
size_t nr_parts);
|
||||
|
||||
#endif
|
||||
48
mie/include/mie/type/type.h
Normal file
48
mie/include/mie/type/type.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef MIE_TYPE_TYPE_H_
|
||||
#define MIE_TYPE_TYPE_H_
|
||||
|
||||
#include <blue/core/stream.h>
|
||||
#include <blue/core/stringstream.h>
|
||||
#include <mie/id.h>
|
||||
#include <mie/misc.h>
|
||||
#include <stddef.h>
|
||||
|
||||
struct mie_dialect;
|
||||
struct mie_dialect_type;
|
||||
|
||||
struct mie_value;
|
||||
struct mie_id_builder;
|
||||
|
||||
enum mie_type_category {
|
||||
/* all other types */
|
||||
MIE_TYPE_OTHER = 0,
|
||||
/* storage types (structs, etc) */
|
||||
MIE_TYPE_STORAGE,
|
||||
/* functional types (represents the param/result types of a function) */
|
||||
MIE_TYPE_FUNCTION,
|
||||
};
|
||||
|
||||
/* a mie_type is an instance of mie_dialect_type.
|
||||
* if the mie_dialect_type is a parametised type, the resulting mie_type
|
||||
* will have those parameters baked in.
|
||||
* this is a base struct. dialects that defined their own types do so by
|
||||
* inheriting from this struct. */
|
||||
struct mie_type {
|
||||
/* this is NOT the same as ty_def->ty_id */
|
||||
mie_id ty_id;
|
||||
enum mie_type_category ty_category;
|
||||
|
||||
char *ty_name;
|
||||
struct mie_dialect_type *ty_def;
|
||||
|
||||
size_t ty_instance_size;
|
||||
};
|
||||
|
||||
MIE_API struct mie_type *mie_type_create(struct mie_dialect_type *type);
|
||||
MIE_API void mie_type_print(const struct mie_type *type, b_stream *out);
|
||||
MIE_API void mie_type_build_id(
|
||||
const struct mie_type *type, struct mie_id_builder *ctx);
|
||||
MIE_API void mie_type_generate_id(
|
||||
const struct mie_type *type, const mie_id *ns, mie_id *out);
|
||||
|
||||
#endif
|
||||
15
mie/include/mie/value.h
Normal file
15
mie/include/mie/value.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef MIE_VALUE_VALUE_H_
|
||||
#define MIE_VALUE_VALUE_H_
|
||||
|
||||
#include <blue/core/stream.h>
|
||||
#include <mie/misc.h>
|
||||
|
||||
struct mie_type;
|
||||
|
||||
struct mie_value {
|
||||
struct mie_type *v_type;
|
||||
};
|
||||
|
||||
MIE_API void mie_value_print(const struct mie_value *value, b_stream *dest);
|
||||
|
||||
#endif
|
||||
94
mie/include/mie/vector.h
Normal file
94
mie/include/mie/vector.h
Normal file
@@ -0,0 +1,94 @@
|
||||
#ifndef MIE_VECTOR_H_
|
||||
#define MIE_VECTOR_H_
|
||||
|
||||
#include <mie/misc.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#if 0
|
||||
#define MIE_VECTOR_DEFINE(type, name) \
|
||||
size_t name##_count = 0; \
|
||||
size_t name##_max = 0; \
|
||||
type *name = NULL
|
||||
#endif
|
||||
|
||||
#define MIE_VECTOR_DEFINE(type, name) \
|
||||
struct { \
|
||||
size_t count; \
|
||||
size_t max; \
|
||||
type *items; \
|
||||
} name = {0}
|
||||
|
||||
#define MIE_VECTOR_DECLARE(type, name) \
|
||||
struct { \
|
||||
size_t count; \
|
||||
size_t max; \
|
||||
type *items; \
|
||||
} name
|
||||
|
||||
#define MIE_VECTOR_ITEM(name, index) name.items[index]
|
||||
#define MIE_VECTOR_ITEM_PTR(name, index) &name.items[index]
|
||||
#define MIE_VECTOR_COUNT(name) name.count
|
||||
#define MIE_VECTOR_MAX(name) name.max
|
||||
|
||||
/* use this macros in your function prototype, to allow the function to accept
|
||||
* a reference to a vector as a parameter */
|
||||
#define MIE_VECTOR_REF_PARAM(type, name) \
|
||||
type **name, size_t *name##_count, size_t *name##_max
|
||||
/* use this macro to pass a reference to a vector as a parameter to a function
|
||||
* whose prototype uses MIE_VECTOR_REF_PARAM */
|
||||
#define MIE_VECTOR_REF(name) &(name.items), &(name.count), &(name.max)
|
||||
/* use this macro to forward your reference to a vector (which you got via
|
||||
* MIE_VECTOR_REF_PARAM in your function prototype), to another function whose
|
||||
* prototype also uses MIE_VECTOR_REF_PARAM */
|
||||
#define MIE_VECTOR_REF2(name) &(name.items), &(name.count), &(name.max)
|
||||
|
||||
/* use these functions if you're accessing a vector directly. */
|
||||
#define mie_vector_push_back(vector, ptr) \
|
||||
__mie_vector_push_back( \
|
||||
(void **)&(vector.items), ptr, sizeof *ptr, &(vector.count), \
|
||||
&(vector.max))
|
||||
#define mie_vector_pop_back(vector) \
|
||||
__mie_vector_pop_back( \
|
||||
(void **)&(vector), sizeof *vector, &(vector.count), \
|
||||
&(vector.max))
|
||||
#define mie_vector_emplace_back(vector) \
|
||||
__mie_vector_emplace_back( \
|
||||
(void **)&(vector.items), sizeof *vector.items, \
|
||||
&(vector.count), &(vector.max))
|
||||
#define mie_vector_destroy(vector, dtor) \
|
||||
__mie_vector_destroy( \
|
||||
(void **)&(vector.items), sizeof *vector.items, \
|
||||
&(vector.count), &(vector.max), dtor)
|
||||
|
||||
/* use these functions if you're accessing a vector as a reference
|
||||
* via MIE_VECTOR_REF_PARAM. */
|
||||
#define mie_vector_ref_push_back(vector, ptr) \
|
||||
__mie_vector_push_back( \
|
||||
(void **)(vector), ptr, sizeof *ptr, (vector##_count), \
|
||||
(vector##_max))
|
||||
#define mie_vector_ref_pop_back(vector) \
|
||||
__mie_vector_pop_back( \
|
||||
(void **)(vector), sizeof **vector, (vector##_count), \
|
||||
(vector##_max))
|
||||
#define mie_vector_ref_emplace_back(vector) \
|
||||
__mie_vector_emplace_back( \
|
||||
(void **)(vector), sizeof **vector, (vector##_count), \
|
||||
(vector##_max))
|
||||
#define mie_vector_ref_destroy(vector, dtor) \
|
||||
__mie_vector_destroy( \
|
||||
(void **)(vector), sizeof **vector, (vector##_count), \
|
||||
(vector##_max), dtor)
|
||||
|
||||
/* don't use these functions */
|
||||
MIE_API int __mie_vector_push_back(
|
||||
void **vector, const void *item, size_t item_size, size_t *count,
|
||||
size_t *max);
|
||||
MIE_API void __mie_vector_pop_back(
|
||||
void **vector, size_t item_size, size_t *count, size_t *max);
|
||||
MIE_API void *__mie_vector_emplace_back(
|
||||
void **vector, size_t item_size, size_t *count, size_t *max);
|
||||
MIE_API void __mie_vector_destroy(
|
||||
void **vector, size_t item_size, size_t *count, size_t *max,
|
||||
void (*dtor)(void *));
|
||||
|
||||
#endif
|
||||
30
mie/ir/arg.c
30
mie/ir/arg.c
@@ -1,30 +0,0 @@
|
||||
#include <mie/ir/arg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct mie_arg *mie_arg_create(struct mie_type *type)
|
||||
{
|
||||
struct mie_arg *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
mie_value_init(&out->arg_base, MIE_VALUE_ARG);
|
||||
|
||||
out->arg_type = type;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
static struct mie_type *get_type(struct mie_value *v, struct mie_ctx *ctx)
|
||||
{
|
||||
struct mie_arg *arg = MIE_ARG(v);
|
||||
return arg->arg_type;
|
||||
}
|
||||
|
||||
const struct mie_value_type arg_value_type = {
|
||||
.t_id = MIE_VALUE_ARG,
|
||||
.t_get_type = get_type,
|
||||
};
|
||||
104
mie/ir/block.c
104
mie/ir/block.c
@@ -1,104 +0,0 @@
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/ir/block.h>
|
||||
#include <mie/ir/func.h>
|
||||
#include <mie/ir/instr.h>
|
||||
#include <mie/type.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct mie_block *mie_block_create(struct mie_func *parent, const char *name)
|
||||
{
|
||||
struct mie_block *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
mie_value_init(&out->b_base, MIE_VALUE_BLOCK);
|
||||
|
||||
struct mie_value *block = MIE_VALUE(out);
|
||||
|
||||
if (parent) {
|
||||
block = mie_func_generate_value_name(parent, block, name);
|
||||
if (!block) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
b_queue_push_back(&parent->f_blocks, &block->v_entry);
|
||||
out->b_parent = parent;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
bool mie_block_add_instr(struct mie_block *block, struct mie_instr *instr)
|
||||
{
|
||||
switch (instr->i_type) {
|
||||
case MIE_INSTR_PHI:
|
||||
if (!b_queue_empty(&block->b_instr) || block->b_terminator) {
|
||||
return false;
|
||||
}
|
||||
|
||||
b_queue_push_back(&block->b_phi, &instr->i_base.v_entry);
|
||||
return true;
|
||||
case MIE_INSTR_RET:
|
||||
case MIE_INSTR_BR:
|
||||
case MIE_INSTR_SWITCH:
|
||||
if (block->b_terminator) {
|
||||
return false;
|
||||
}
|
||||
|
||||
block->b_terminator = instr;
|
||||
return true;
|
||||
default:
|
||||
if (block->b_terminator) {
|
||||
return false;
|
||||
}
|
||||
|
||||
b_queue_push_back(&block->b_instr, &instr->i_base.v_entry);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static struct mie_type *get_type(struct mie_value *v, struct mie_ctx *ctx)
|
||||
{
|
||||
return mie_ctx_get_type(ctx, MIE_TYPE_LABEL);
|
||||
}
|
||||
|
||||
static void cleanup(struct mie_value *value)
|
||||
{
|
||||
struct mie_block *block = MIE_BLOCK(value);
|
||||
|
||||
b_queue_entry *entry = b_queue_first(&block->b_phi);
|
||||
b_queue_entry *next = NULL;
|
||||
|
||||
while (entry) {
|
||||
struct mie_value *v = b_unbox(struct mie_value, entry, v_entry);
|
||||
next = b_queue_next(entry);
|
||||
b_queue_delete(&block->b_phi, entry);
|
||||
mie_value_destroy(v);
|
||||
|
||||
entry = next;
|
||||
}
|
||||
|
||||
entry = b_queue_first(&block->b_instr);
|
||||
while (entry) {
|
||||
struct mie_value *v = b_unbox(struct mie_value, entry, v_entry);
|
||||
next = b_queue_next(entry);
|
||||
b_queue_delete(&block->b_instr, entry);
|
||||
mie_value_destroy(v);
|
||||
|
||||
entry = next;
|
||||
}
|
||||
|
||||
if (block->b_terminator) {
|
||||
mie_value_destroy(MIE_VALUE(block->b_terminator));
|
||||
}
|
||||
}
|
||||
|
||||
const struct mie_value_type block_value_type = {
|
||||
.t_id = MIE_VALUE_BLOCK,
|
||||
.t_get_type = get_type,
|
||||
.t_cleanup = cleanup,
|
||||
};
|
||||
834
mie/ir/builder.c
834
mie/ir/builder.c
@@ -1,834 +0,0 @@
|
||||
#include <blue/ds/string.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/ir/alloca.h>
|
||||
#include <mie/ir/block.h>
|
||||
#include <mie/ir/branch.h>
|
||||
#include <mie/ir/builder.h>
|
||||
#include <mie/ir/data.h>
|
||||
#include <mie/ir/func.h>
|
||||
#include <mie/ir/module.h>
|
||||
#include <mie/ir/msg.h>
|
||||
#include <mie/ir/op.h>
|
||||
#include <mie/ir/phi.h>
|
||||
#include <mie/ir/ptr.h>
|
||||
#include <mie/ir/record.h>
|
||||
#include <mie/type.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct mie_builder *mie_builder_create(struct mie_ctx *ctx, struct mie_module *mod)
|
||||
{
|
||||
struct mie_builder *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
out->b_ctx = ctx;
|
||||
out->b_module = mod;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void mie_builder_destroy(struct mie_builder *builder)
|
||||
{
|
||||
free(builder);
|
||||
}
|
||||
|
||||
struct mie_func *mie_builder_get_current_func(struct mie_builder *builder)
|
||||
{
|
||||
struct mie_block *block = builder->b_current_block;
|
||||
if (!block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return block->b_parent;
|
||||
}
|
||||
|
||||
struct mie_record *mie_builder_put_record(
|
||||
struct mie_builder *builder, struct mie_const *val, const char *name)
|
||||
{
|
||||
if (!builder->b_module) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
b_queue_entry *entry = b_queue_first(&builder->b_module->m_records);
|
||||
while (entry) {
|
||||
struct mie_value *rec = b_unbox(struct mie_value, entry, v_entry);
|
||||
|
||||
if (!strcmp(rec->v_name.n_str, name)) {
|
||||
/* TODO what to do about `val` here? */
|
||||
return MIE_RECORD(rec);
|
||||
}
|
||||
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
struct mie_record *rec = mie_record_create(val);
|
||||
rec->r_base.v_name.n_str = b_strdup(name);
|
||||
b_queue_push_back(&builder->b_module->m_records, &rec->r_base.v_entry);
|
||||
|
||||
return rec;
|
||||
}
|
||||
|
||||
struct mie_record *mie_builder_get_record(
|
||||
struct mie_builder *builder, const char *name)
|
||||
{
|
||||
if (!builder->b_module) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
b_queue_entry *entry = b_queue_first(&builder->b_module->m_records);
|
||||
while (entry) {
|
||||
struct mie_value *rec = b_unbox(struct mie_value, entry, v_entry);
|
||||
|
||||
if (!strcmp(rec->v_name.n_str, name)) {
|
||||
return MIE_RECORD(rec);
|
||||
}
|
||||
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if 0
|
||||
void mie_builder_put_data(struct mie_builder *builder, struct mie_data *data)
|
||||
{
|
||||
if (!builder->b_module) {
|
||||
return;
|
||||
}
|
||||
|
||||
const char *data_name = data->d_base.v_name.n_str;
|
||||
struct mie_data *v = mie_module_get_data(builder->b_module, data_name);
|
||||
if (v) {
|
||||
return;
|
||||
}
|
||||
|
||||
mie_module_put_data(builder->b_module, data);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
void mie_builder_put_type(struct mie_builder *builder, struct mie_type *type)
|
||||
{
|
||||
if (!builder->b_module) {
|
||||
return;
|
||||
}
|
||||
|
||||
const char *type_name = type->t_base.v_name.n_str;
|
||||
struct mie_data *v = mie_module_get_data(builder->b_module, type_name);
|
||||
if (v) {
|
||||
return;
|
||||
}
|
||||
|
||||
mie_module_put_data(builder->b_module, MIE_VALUE);
|
||||
}
|
||||
#endif
|
||||
|
||||
void mie_builder_set_insert_point(
|
||||
struct mie_builder *builder, struct mie_block *block)
|
||||
{
|
||||
builder->b_current_block = block;
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_get_data_ptr(
|
||||
struct mie_builder *builder, const char *data_ident)
|
||||
{
|
||||
if (!builder->b_module) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_data *data = mie_module_get_data(builder->b_module, data_ident);
|
||||
if (!data) {
|
||||
struct mie_type *id
|
||||
= mie_ctx_get_type(builder->b_ctx, MIE_TYPE_ID);
|
||||
data = mie_data_create_extern_global(id, data_ident);
|
||||
|
||||
if (!data) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_module_put_data(builder->b_module, data, data_ident);
|
||||
}
|
||||
|
||||
return MIE_VALUE(data);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_get_string_ptr(
|
||||
struct mie_builder *builder, const char *s)
|
||||
{
|
||||
if (!builder->b_module) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_data *data = mie_module_get_string_ptr(builder->b_module, s);
|
||||
if (!data) {
|
||||
struct mie_type *str
|
||||
= mie_ctx_get_type(builder->b_ctx, MIE_TYPE_STR);
|
||||
struct mie_value *value = mie_ctx_get_string(builder->b_ctx, s);
|
||||
|
||||
data = mie_data_create_const((struct mie_const *)value);
|
||||
|
||||
if (!data) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_module_put_data(builder->b_module, data, ".str");
|
||||
}
|
||||
|
||||
return MIE_VALUE(data);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_ret(struct mie_builder *builder, struct mie_value *val)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_ret *ret = malloc(sizeof *ret);
|
||||
if (!ret) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_instr_init(&ret->r_base, MIE_INSTR_RET);
|
||||
ret->r_val = val;
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &ret->r_base)) {
|
||||
free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return MIE_VALUE(ret);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_add(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_binary_op *add = malloc(sizeof *add);
|
||||
if (!add) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(add, 0x0, sizeof *add);
|
||||
|
||||
mie_instr_init(&add->op_base, MIE_INSTR_ADD);
|
||||
|
||||
add->op_left = left;
|
||||
add->op_right = right;
|
||||
add->op_type = mie_value_get_type(left, builder->b_ctx);
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &add->op_base)) {
|
||||
free(add);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(add), name);
|
||||
|
||||
return MIE_VALUE(add);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_sub(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_binary_op *sub = malloc(sizeof *sub);
|
||||
if (!sub) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(sub, 0x0, sizeof *sub);
|
||||
|
||||
mie_instr_init(&sub->op_base, MIE_INSTR_SUB);
|
||||
|
||||
sub->op_left = left;
|
||||
sub->op_right = right;
|
||||
sub->op_type = mie_value_get_type(left, builder->b_ctx);
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &sub->op_base)) {
|
||||
free(sub);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(sub), name);
|
||||
|
||||
return MIE_VALUE(sub);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_mul(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_binary_op *mul = malloc(sizeof *mul);
|
||||
if (!mul) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(mul, 0x0, sizeof *mul);
|
||||
|
||||
mie_instr_init(&mul->op_base, MIE_INSTR_MUL);
|
||||
|
||||
mul->op_left = left;
|
||||
mul->op_right = right;
|
||||
mul->op_type = mie_value_get_type(left, builder->b_ctx);
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &mul->op_base)) {
|
||||
free(mul);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(mul), name);
|
||||
|
||||
return MIE_VALUE(mul);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_div(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_binary_op *div = malloc(sizeof *div);
|
||||
if (!div) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(div, 0x0, sizeof *div);
|
||||
|
||||
mie_instr_init(&div->op_base, MIE_INSTR_DIV);
|
||||
|
||||
div->op_left = left;
|
||||
div->op_right = right;
|
||||
div->op_type = mie_value_get_type(left, builder->b_ctx);
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &div->op_base)) {
|
||||
free(div);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(div), name);
|
||||
|
||||
return MIE_VALUE(div);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_load(
|
||||
struct mie_builder *builder, struct mie_type *type,
|
||||
struct mie_value *src, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_load *load = malloc(sizeof *load);
|
||||
if (!load) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_instr_init(&load->l_base, MIE_INSTR_LOAD);
|
||||
load->l_src = src;
|
||||
load->l_type = type;
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &load->l_base)) {
|
||||
free(load);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(load), name);
|
||||
|
||||
return MIE_VALUE(load);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_store(
|
||||
struct mie_builder *builder, struct mie_value *val, struct mie_value *dest)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_store *store = malloc(sizeof *store);
|
||||
if (!store) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_instr_init(&store->s_base, MIE_INSTR_STORE);
|
||||
store->s_val = val;
|
||||
store->s_dest = dest;
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &store->s_base)) {
|
||||
free(store);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return MIE_VALUE(store);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_alloca(
|
||||
struct mie_builder *builder, struct mie_type *type, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_alloca *alloca = malloc(sizeof *alloca);
|
||||
if (!alloca) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_instr_init(&alloca->a_base, MIE_INSTR_ALLOCA);
|
||||
alloca->a_type = type;
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &alloca->a_base)) {
|
||||
free(alloca);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(alloca), name);
|
||||
|
||||
return MIE_VALUE(alloca);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_switch(
|
||||
struct mie_builder *builder, struct mie_value *cond,
|
||||
struct mie_switch_branch *branches, size_t nr_branches,
|
||||
struct mie_block *default_block)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_br(struct mie_builder *builder, struct mie_block *dest)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_branch *br = malloc(sizeof *br);
|
||||
if (!br) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(br, 0x0, sizeof *br);
|
||||
|
||||
mie_instr_init(&br->b_base, MIE_INSTR_BR);
|
||||
|
||||
br->b_dest = dest;
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &br->b_base)) {
|
||||
free(br);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return MIE_VALUE(br);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_br_if(
|
||||
struct mie_builder *builder, struct mie_value *cond,
|
||||
struct mie_block *if_true, struct mie_block *if_false)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_branch_if *br = malloc(sizeof *br);
|
||||
if (!br) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(br, 0x0, sizeof *br);
|
||||
|
||||
mie_instr_init(&br->b_base, MIE_INSTR_BR_IF);
|
||||
|
||||
br->b_cond = cond;
|
||||
br->b_true_block = if_true;
|
||||
br->b_false_block = if_false;
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &br->b_base)) {
|
||||
free(br);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return MIE_VALUE(br);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_msg(
|
||||
struct mie_builder *builder, struct mie_type *ret_type,
|
||||
struct mie_value *recipient, struct mie_value *selector,
|
||||
struct mie_value **args, size_t nr_args, enum mie_builder_flags flags,
|
||||
const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((flags & MIE_BUILDER_IGNORE_RESULT) && name) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_msg *msg = malloc(sizeof *msg);
|
||||
if (!msg) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(msg, 0x0, sizeof *msg);
|
||||
|
||||
mie_instr_init(&msg->msg_base, MIE_INSTR_MSG);
|
||||
|
||||
msg->msg_ret_type = ret_type;
|
||||
msg->msg_recipient = recipient;
|
||||
msg->msg_selector = selector;
|
||||
|
||||
msg->msg_nr_args = nr_args;
|
||||
msg->msg_args = calloc(nr_args, sizeof(struct mie_value *));
|
||||
memcpy(msg->msg_args, args, nr_args * sizeof(struct mie_value *));
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &msg->msg_base)) {
|
||||
free(msg);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!(flags & MIE_BUILDER_IGNORE_RESULT)) {
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(msg), name);
|
||||
}
|
||||
|
||||
return MIE_VALUE(msg);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_cmp_eq(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_binary_op *sub = malloc(sizeof *sub);
|
||||
if (!sub) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(sub, 0x0, sizeof *sub);
|
||||
|
||||
mie_instr_init(&sub->op_base, MIE_INSTR_CMP_EQ);
|
||||
|
||||
sub->op_left = left;
|
||||
sub->op_right = right;
|
||||
sub->op_type = mie_ctx_get_int_type(builder->b_ctx, 1);
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &sub->op_base)) {
|
||||
free(sub);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(sub), name);
|
||||
|
||||
return MIE_VALUE(sub);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_cmp_neq(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_binary_op *sub = malloc(sizeof *sub);
|
||||
if (!sub) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(sub, 0x0, sizeof *sub);
|
||||
|
||||
mie_instr_init(&sub->op_base, MIE_INSTR_CMP_NEQ);
|
||||
|
||||
sub->op_left = left;
|
||||
sub->op_right = right;
|
||||
sub->op_type = mie_ctx_get_int_type(builder->b_ctx, 1);
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &sub->op_base)) {
|
||||
free(sub);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(sub), name);
|
||||
|
||||
return MIE_VALUE(sub);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_cmp_lt(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_binary_op *sub = malloc(sizeof *sub);
|
||||
if (!sub) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(sub, 0x0, sizeof *sub);
|
||||
|
||||
mie_instr_init(&sub->op_base, MIE_INSTR_CMP_LT);
|
||||
|
||||
sub->op_left = left;
|
||||
sub->op_right = right;
|
||||
sub->op_type = mie_ctx_get_int_type(builder->b_ctx, 1);
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &sub->op_base)) {
|
||||
free(sub);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(sub), name);
|
||||
|
||||
return MIE_VALUE(sub);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_cmp_gt(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_binary_op *sub = malloc(sizeof *sub);
|
||||
if (!sub) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(sub, 0x0, sizeof *sub);
|
||||
|
||||
mie_instr_init(&sub->op_base, MIE_INSTR_CMP_GT);
|
||||
|
||||
sub->op_left = left;
|
||||
sub->op_right = right;
|
||||
sub->op_type = mie_ctx_get_int_type(builder->b_ctx, 1);
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &sub->op_base)) {
|
||||
free(sub);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(sub), name);
|
||||
|
||||
return MIE_VALUE(sub);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_cmp_leq(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_binary_op *sub = malloc(sizeof *sub);
|
||||
if (!sub) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(sub, 0x0, sizeof *sub);
|
||||
|
||||
mie_instr_init(&sub->op_base, MIE_INSTR_CMP_LEQ);
|
||||
|
||||
sub->op_left = left;
|
||||
sub->op_right = right;
|
||||
sub->op_type = mie_ctx_get_int_type(builder->b_ctx, 1);
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &sub->op_base)) {
|
||||
free(sub);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(sub), name);
|
||||
|
||||
return MIE_VALUE(sub);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_cmp_geq(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_binary_op *sub = malloc(sizeof *sub);
|
||||
if (!sub) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(sub, 0x0, sizeof *sub);
|
||||
|
||||
mie_instr_init(&sub->op_base, MIE_INSTR_CMP_GEQ);
|
||||
|
||||
sub->op_left = left;
|
||||
sub->op_right = right;
|
||||
sub->op_type = mie_ctx_get_int_type(builder->b_ctx, 1);
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &sub->op_base)) {
|
||||
free(sub);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(sub), name);
|
||||
|
||||
return MIE_VALUE(sub);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_getelementptr(
|
||||
struct mie_builder *builder, struct mie_type *container_type,
|
||||
struct mie_value *container, struct mie_value *index, const char *name)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_setelementptr(
|
||||
struct mie_builder *builder, struct mie_type *container_type,
|
||||
struct mie_value *container, struct mie_value *index)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_phi(
|
||||
struct mie_builder *builder, struct mie_type *type,
|
||||
struct mie_phi_edge *edges, unsigned int nr_edges, const char *name)
|
||||
{
|
||||
struct mie_block *block = builder->b_current_block;
|
||||
|
||||
if (!block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (block->b_terminator || !b_queue_empty(&block->b_instr)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_phi *phi = malloc(sizeof *phi);
|
||||
if (!phi) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(phi, 0x0, sizeof *phi);
|
||||
|
||||
mie_instr_init(&phi->p_base, MIE_INSTR_PHI);
|
||||
|
||||
phi->p_type = type;
|
||||
phi->p_nr_edges = nr_edges;
|
||||
phi->p_edges = calloc(nr_edges, sizeof(struct mie_phi_edge));
|
||||
if (!phi->p_edges) {
|
||||
free(phi);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(phi->p_edges, edges, nr_edges * sizeof(struct mie_phi_edge));
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &phi->p_base)) {
|
||||
free(phi);
|
||||
free(phi->p_edges);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(phi), name);
|
||||
|
||||
return MIE_VALUE(phi);
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
#include <mie/ir/const.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void mie_const_init(struct mie_const *c, struct mie_type *type)
|
||||
{
|
||||
memset(c, 0x0, sizeof *c);
|
||||
mie_value_init(&c->c_base, MIE_VALUE_CONST);
|
||||
|
||||
c->c_type = type;
|
||||
}
|
||||
|
||||
static struct mie_type *get_type(struct mie_value *v, struct mie_ctx *ctx)
|
||||
{
|
||||
struct mie_const *c = MIE_CONST(v);
|
||||
return c->c_type;
|
||||
}
|
||||
|
||||
const struct mie_value_type const_value_type = {
|
||||
.t_id = MIE_VALUE_CONST,
|
||||
.t_get_type = get_type,
|
||||
};
|
||||
@@ -1,7 +0,0 @@
|
||||
#include "convert.h"
|
||||
|
||||
b_status read_value_from_bitcode(
|
||||
struct mie_ir_converter *converter, struct mie_value **out)
|
||||
{
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
#include "convert.h"
|
||||
|
||||
b_status write_value_to_bitcode(
|
||||
struct mie_ir_converter *converter, struct mie_value *value)
|
||||
{
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
@@ -1,217 +0,0 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <mie/ir/convert.h>
|
||||
#include <mie/ir/value.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct mie_ir_converter *mie_ir_converter_create(
|
||||
struct mie_ctx *ctx, enum mie_ir_format src, enum mie_ir_format dest)
|
||||
{
|
||||
if (src == dest) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_ir_converter *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
out->c_ctx = ctx;
|
||||
out->c_src_format = src;
|
||||
out->c_dest_format = dest;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void mie_ir_converter_destroy(struct mie_ir_converter *converter)
|
||||
{
|
||||
free(converter);
|
||||
}
|
||||
|
||||
b_status mie_ir_converter_set_src_value(
|
||||
struct mie_ir_converter *converter, struct mie_value *value)
|
||||
{
|
||||
if (converter->c_src_format != MIE_IR_MEM) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
converter->c_src_medium = MIE_IR_CONVERTER_MIE_VALUE;
|
||||
converter->c_src.value = value;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status mie_ir_converter_set_src_bitbuffer(
|
||||
struct mie_ir_converter *converter, b_bitbuffer *bitbuffer)
|
||||
{
|
||||
if (converter->c_src_format != MIE_IR_BITCODE) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
converter->c_src_medium = MIE_IR_CONVERTER_BITBUFFER;
|
||||
converter->c_src.bitbuffer = bitbuffer;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status mie_ir_converter_set_src_stringstream(
|
||||
struct mie_ir_converter *converter, b_stringstream *stringstream)
|
||||
{
|
||||
if (converter->c_src_format != MIE_IR_TEXT) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
converter->c_src_medium = MIE_IR_CONVERTER_STRINGSTREAM;
|
||||
converter->c_src.stringstream = stringstream;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status mie_ir_converter_set_src_string(
|
||||
struct mie_ir_converter *converter, b_string *string)
|
||||
{
|
||||
if (converter->c_src_format != MIE_IR_TEXT) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
converter->c_src_medium = MIE_IR_CONVERTER_STRING;
|
||||
converter->c_src.string = string;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status mie_ir_converter_set_src_file(struct mie_ir_converter *converter, FILE *file)
|
||||
{
|
||||
if (converter->c_src_format != MIE_IR_TEXT) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
converter->c_src_medium = MIE_IR_CONVERTER_FILE;
|
||||
converter->c_src.file = file;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status mie_ir_converter_set_dest_value(
|
||||
struct mie_ir_converter *converter, struct mie_value **value)
|
||||
{
|
||||
if (converter->c_dest_format != MIE_IR_MEM) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
converter->c_dest_medium = MIE_IR_CONVERTER_MIE_VALUE;
|
||||
converter->c_dest.value = value;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status mie_ir_converter_set_dest_bitbuffer(
|
||||
struct mie_ir_converter *converter, b_bitbuffer *bitbuffer)
|
||||
{
|
||||
if (converter->c_dest_format != MIE_IR_BITCODE) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
converter->c_dest_medium = MIE_IR_CONVERTER_BITBUFFER;
|
||||
converter->c_dest.bitbuffer = bitbuffer;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status mie_ir_converter_set_dest_stringstream(
|
||||
struct mie_ir_converter *converter, b_stringstream *stringstream)
|
||||
{
|
||||
if (converter->c_dest_format != MIE_IR_TEXT) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
converter->c_dest_medium = MIE_IR_CONVERTER_STRINGSTREAM;
|
||||
converter->c_dest.stringstream = stringstream;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status mie_ir_converter_set_dest_string(
|
||||
struct mie_ir_converter *converter, b_string *string)
|
||||
{
|
||||
if (converter->c_dest_format != MIE_IR_TEXT) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
converter->c_dest_medium = MIE_IR_CONVERTER_STRING;
|
||||
converter->c_dest.string = string;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status mie_ir_converter_set_dest_file(
|
||||
struct mie_ir_converter *converter, FILE *file)
|
||||
{
|
||||
if (converter->c_dest_format != MIE_IR_TEXT) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
converter->c_dest_medium = MIE_IR_CONVERTER_FILE;
|
||||
converter->c_dest.file = file;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static b_status read_value_from_medium(
|
||||
struct mie_ir_converter *converter, struct mie_value **value)
|
||||
{
|
||||
switch (converter->c_src_format) {
|
||||
case MIE_IR_MEM:
|
||||
*value = converter->c_src.value;
|
||||
return B_SUCCESS;
|
||||
case MIE_IR_TEXT:
|
||||
return read_value_from_text(converter, value);
|
||||
case MIE_IR_BITCODE:
|
||||
return read_value_from_bitcode(converter, value);
|
||||
default:
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
}
|
||||
|
||||
static b_status write_value_to_medium(
|
||||
struct mie_ir_converter *converter, struct mie_value *value)
|
||||
{
|
||||
switch (converter->c_dest_format) {
|
||||
case MIE_IR_MEM:
|
||||
*converter->c_dest.value = value;
|
||||
return B_SUCCESS;
|
||||
case MIE_IR_TEXT:
|
||||
return write_value_to_text(converter, value);
|
||||
case MIE_IR_BITCODE:
|
||||
return write_value_to_bitcode(converter, value);
|
||||
default:
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
}
|
||||
|
||||
b_status mie_ir_converter_process(struct mie_ir_converter *converter)
|
||||
{
|
||||
b_status status = B_SUCCESS;
|
||||
struct mie_value *value = NULL;
|
||||
|
||||
status = read_value_from_medium(converter, &value);
|
||||
|
||||
if (!B_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (!value) {
|
||||
return B_ERR_BAD_FORMAT;
|
||||
}
|
||||
|
||||
status = write_value_to_medium(converter, value);
|
||||
|
||||
if (converter->c_src_format != MIE_IR_MEM) {
|
||||
mie_value_destroy(value);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
#ifndef _MIE_CONVERT_H_
|
||||
#define _MIE_CONVERT_H_
|
||||
|
||||
#include <mie/ir/convert.h>
|
||||
|
||||
extern b_status write_char(struct mie_ir_converter *converter, char c);
|
||||
extern b_status write_string(struct mie_ir_converter *converter, const char *s);
|
||||
|
||||
extern b_status read_value_from_text(
|
||||
struct mie_ir_converter *converter, struct mie_value **out);
|
||||
extern b_status read_value_from_bitcode(
|
||||
struct mie_ir_converter *converter, struct mie_value **out);
|
||||
|
||||
extern b_status write_value_to_text(
|
||||
struct mie_ir_converter *converter, struct mie_value *value);
|
||||
extern b_status write_value_to_bitcode(
|
||||
struct mie_ir_converter *converter, struct mie_value *value);
|
||||
|
||||
#endif
|
||||
@@ -1,7 +0,0 @@
|
||||
#include "convert.h"
|
||||
|
||||
b_status read_value_from_text(
|
||||
struct mie_ir_converter *converter, struct mie_value **out)
|
||||
{
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
@@ -1,747 +0,0 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <blue/ds/hashmap.h>
|
||||
#include <blue/ds/list.h>
|
||||
#include <inttypes.h>
|
||||
#include <mie/ir/alloca.h>
|
||||
#include <mie/ir/arg.h>
|
||||
#include <mie/ir/block.h>
|
||||
#include <mie/ir/branch.h>
|
||||
#include <mie/ir/const.h>
|
||||
#include <mie/ir/data.h>
|
||||
#include <mie/ir/func.h>
|
||||
#include <mie/ir/instr.h>
|
||||
#include <mie/ir/module.h>
|
||||
#include <mie/ir/msg.h>
|
||||
#include <mie/ir/op.h>
|
||||
#include <mie/ir/phi.h>
|
||||
#include <mie/ir/ptr.h>
|
||||
#include <mie/ir/record.h>
|
||||
#include <mie/ir/value.h>
|
||||
#include <mie/type.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define F_INCLUDE_TYPE 0x01u
|
||||
|
||||
typedef b_status (*write_function)(struct mie_ir_converter *, struct mie_value *);
|
||||
|
||||
b_status write_char(struct mie_ir_converter *converter, char c)
|
||||
{
|
||||
long result = 0;
|
||||
char s[] = {c, '\0'};
|
||||
|
||||
switch (converter->c_dest_medium) {
|
||||
case MIE_IR_CONVERTER_STRINGSTREAM:
|
||||
return b_stream_write_string(
|
||||
converter->c_dest.stringstream, s, NULL);
|
||||
case MIE_IR_CONVERTER_STRING:
|
||||
b_string_append_cstr(converter->c_dest.string, s);
|
||||
return B_SUCCESS;
|
||||
case MIE_IR_CONVERTER_FILE:
|
||||
result = fputc(c, converter->c_dest.file);
|
||||
return result == EOF ? B_ERR_IO_FAILURE : B_SUCCESS;
|
||||
default:
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status write_string(struct mie_ir_converter *converter, const char *s)
|
||||
{
|
||||
long result = 0;
|
||||
|
||||
switch (converter->c_dest_medium) {
|
||||
case MIE_IR_CONVERTER_STRINGSTREAM:
|
||||
return b_stream_write_string(
|
||||
converter->c_dest.stringstream, s, NULL);
|
||||
case MIE_IR_CONVERTER_STRING:
|
||||
b_string_append_cstr(converter->c_dest.string, s);
|
||||
return B_SUCCESS;
|
||||
case MIE_IR_CONVERTER_FILE:
|
||||
result = fputs(s, converter->c_dest.file);
|
||||
return result == EOF ? B_ERR_IO_FAILURE : B_SUCCESS;
|
||||
default:
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status write_string_f(struct mie_ir_converter *converter, const char *format, ...)
|
||||
{
|
||||
long result = 0;
|
||||
|
||||
char buf[512];
|
||||
va_list arg;
|
||||
va_start(arg, format);
|
||||
|
||||
b_status status = B_SUCCESS;
|
||||
|
||||
switch (converter->c_dest_medium) {
|
||||
case MIE_IR_CONVERTER_STRINGSTREAM:
|
||||
vsnprintf(buf, sizeof buf, format, arg);
|
||||
status = b_stream_write_string(
|
||||
converter->c_dest.stringstream, buf, NULL);
|
||||
break;
|
||||
case MIE_IR_CONVERTER_STRING:
|
||||
vsnprintf(buf, sizeof buf, format, arg);
|
||||
b_string_append_cstr(converter->c_dest.string, buf);
|
||||
status = B_SUCCESS;
|
||||
break;
|
||||
case MIE_IR_CONVERTER_FILE:
|
||||
result = vfprintf(converter->c_dest.file, format, arg);
|
||||
status = (result == EOF ? B_ERR_IO_FAILURE : B_SUCCESS);
|
||||
break;
|
||||
default:
|
||||
status = B_ERR_NOT_SUPPORTED;
|
||||
break;
|
||||
}
|
||||
|
||||
va_end(arg);
|
||||
return status;
|
||||
}
|
||||
|
||||
static b_status write_operand_const(
|
||||
struct mie_ir_converter *converter, struct mie_value *value, int flags)
|
||||
{
|
||||
struct mie_const *c = MIE_CONST(value);
|
||||
|
||||
if (flags & F_INCLUDE_TYPE) {
|
||||
char type_name[64];
|
||||
mie_type_to_string(c->c_type, type_name, sizeof type_name);
|
||||
write_string_f(converter, "%s", type_name);
|
||||
|
||||
if (type_name[0] != 0) {
|
||||
write_char(converter, ' ');
|
||||
}
|
||||
}
|
||||
|
||||
switch (c->c_type->t_id) {
|
||||
case MIE_TYPE_INT: {
|
||||
struct mie_int *v = MIE_INT(c);
|
||||
write_string_f(converter, "#%" PRId64, v->i_value);
|
||||
break;
|
||||
}
|
||||
case MIE_TYPE_PTR:
|
||||
case MIE_TYPE_ID:
|
||||
write_string_f(converter, "%%%s", value->v_name.n_str);
|
||||
break;
|
||||
case MIE_TYPE_STR: {
|
||||
struct mie_string *v = MIE_STRING(c);
|
||||
|
||||
write_string_f(converter, "\"%s\"", v->s_value);
|
||||
break;
|
||||
}
|
||||
case MIE_TYPE_ATOM: {
|
||||
struct mie_atom *v = MIE_ATOM(c);
|
||||
|
||||
write_string_f(converter, "\"%s\"", v->a_value);
|
||||
break;
|
||||
}
|
||||
case MIE_TYPE_SELECTOR: {
|
||||
struct mie_selector *v = MIE_SELECTOR(c);
|
||||
|
||||
write_string_f(converter, "@%s", v->sel_value);
|
||||
break;
|
||||
}
|
||||
case MIE_TYPE_CLASS:
|
||||
write_string_f(converter, "@%s", value->v_name.n_str);
|
||||
break;
|
||||
case MIE_TYPE_ARRAY: {
|
||||
struct mie_array *array = MIE_ARRAY(value);
|
||||
b_iterator *it = b_list_begin(array->a_values);
|
||||
|
||||
write_char(converter, '{');
|
||||
size_t i = 0;
|
||||
b_foreach(void *, item, it)
|
||||
{
|
||||
if (i > 0) {
|
||||
write_char(converter, ',');
|
||||
}
|
||||
|
||||
write_string(converter, "\n ");
|
||||
|
||||
struct mie_value *child = item;
|
||||
write_operand_const(converter, child, F_INCLUDE_TYPE);
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
b_iterator_unref(it);
|
||||
|
||||
if (!b_list_empty(array->a_values)) {
|
||||
write_char(converter, '\n');
|
||||
}
|
||||
|
||||
write_char(converter, '}');
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static b_status write_operand_instr(
|
||||
struct mie_ir_converter *converter, struct mie_value *value, int flags)
|
||||
{
|
||||
struct mie_instr *instr = MIE_INSTR(value);
|
||||
|
||||
if (flags & F_INCLUDE_TYPE) {
|
||||
char type_name[64];
|
||||
struct mie_type *type
|
||||
= mie_value_get_type(value, converter->c_ctx);
|
||||
mie_type_to_string(type, type_name, sizeof type_name);
|
||||
write_string_f(converter, "%s ", type_name);
|
||||
}
|
||||
|
||||
write_string_f(converter, "%%%s", value->v_name.n_str);
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static b_status write_operand_block(
|
||||
struct mie_ir_converter *converter, struct mie_value *value, int flags)
|
||||
{
|
||||
struct mie_block *block = MIE_BLOCK(value);
|
||||
|
||||
if (flags & F_INCLUDE_TYPE) {
|
||||
char type_name[64];
|
||||
struct mie_type *type
|
||||
= mie_value_get_type(value, converter->c_ctx);
|
||||
mie_type_to_string(type, type_name, sizeof type_name);
|
||||
write_string_f(converter, "%s ", type_name);
|
||||
}
|
||||
|
||||
write_string_f(converter, "%%%s", value->v_name.n_str);
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static b_status write_operand_data(
|
||||
struct mie_ir_converter *converter, struct mie_value *value, int flags)
|
||||
{
|
||||
struct mie_const *c = MIE_CONST(value);
|
||||
|
||||
if (flags & F_INCLUDE_TYPE) {
|
||||
write_string(converter, "ptr ");
|
||||
}
|
||||
|
||||
write_string_f(converter, "@%s", value->v_name.n_str);
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static b_status write_operand_arg(
|
||||
struct mie_ir_converter *converter, struct mie_value *value, int flags)
|
||||
{
|
||||
struct mie_arg *arg = MIE_ARG(value);
|
||||
|
||||
if (flags & F_INCLUDE_TYPE) {
|
||||
char type_name[64];
|
||||
mie_type_to_string(arg->arg_type, type_name, sizeof type_name);
|
||||
write_string_f(converter, "%s ", type_name);
|
||||
}
|
||||
|
||||
write_string_f(converter, "%%%s", value->v_name.n_str);
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static b_status write_operand_func(
|
||||
struct mie_ir_converter *converter, struct mie_value *value, int flags)
|
||||
{
|
||||
struct mie_func *func = MIE_FUNC(value);
|
||||
|
||||
if (flags & F_INCLUDE_TYPE) {
|
||||
write_string(converter, "ptr ");
|
||||
}
|
||||
|
||||
write_string_f(converter, "@%s", value->v_name.n_str);
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static b_status write_operand(
|
||||
struct mie_ir_converter *converter, struct mie_value *value, int flags)
|
||||
{
|
||||
if (!value) {
|
||||
write_string(converter, "<NULL VALUE PTR>");
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
switch (value->v_type->t_id) {
|
||||
case MIE_VALUE_NONE:
|
||||
write_string(converter, "null");
|
||||
return B_SUCCESS;
|
||||
case MIE_VALUE_CONST:
|
||||
return write_operand_const(converter, value, flags);
|
||||
case MIE_VALUE_INSTR:
|
||||
return write_operand_instr(converter, value, flags);
|
||||
case MIE_VALUE_BLOCK:
|
||||
return write_operand_block(converter, value, flags);
|
||||
case MIE_VALUE_DATA:
|
||||
return write_operand_data(converter, value, flags);
|
||||
case MIE_VALUE_FUNC:
|
||||
return write_operand_func(converter, value, flags);
|
||||
case MIE_VALUE_ARG:
|
||||
return write_operand_arg(converter, value, flags);
|
||||
default:
|
||||
write_string_f(converter, "<unknown-value:%d>", value->v_type->t_id);
|
||||
return B_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
static b_status write_module(
|
||||
struct mie_ir_converter *converter, struct mie_value *value)
|
||||
{
|
||||
struct mie_module *mod = MIE_MODULE(value);
|
||||
b_status status = B_SUCCESS;
|
||||
|
||||
b_queue_entry *entry = b_queue_first(&mod->m_records);
|
||||
while (entry) {
|
||||
struct mie_value *record
|
||||
= b_unbox(struct mie_value, entry, v_entry);
|
||||
status = write_value_to_text(converter, record);
|
||||
|
||||
if (!B_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
if (!b_queue_empty(&mod->m_records)) {
|
||||
write_char(converter, '\n');
|
||||
}
|
||||
|
||||
entry = b_queue_first(&mod->m_types);
|
||||
while (entry) {
|
||||
struct mie_value *type = b_unbox(struct mie_value, entry, v_entry);
|
||||
status = write_value_to_text(converter, type);
|
||||
|
||||
if (!B_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
if (!b_queue_empty(&mod->m_types)) {
|
||||
write_char(converter, '\n');
|
||||
}
|
||||
|
||||
b_iterator *it = b_iterator_begin(mod->m_data);
|
||||
b_foreach_ptr(b_hashmap_item, item, it)
|
||||
{
|
||||
struct mie_value *data = item->value.value_data;
|
||||
status = write_value_to_text(converter, data);
|
||||
|
||||
if (!B_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
b_iterator_unref(it);
|
||||
|
||||
if (!b_hashmap_is_empty(mod->m_data)) {
|
||||
write_char(converter, '\n');
|
||||
}
|
||||
|
||||
unsigned long i = 0;
|
||||
entry = b_queue_first(&mod->m_func);
|
||||
while (entry) {
|
||||
if (i > 0) {
|
||||
write_char(converter, '\n');
|
||||
}
|
||||
|
||||
struct mie_value *func = b_unbox(struct mie_value, entry, v_entry);
|
||||
status = write_value_to_text(converter, func);
|
||||
|
||||
if (!B_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
i++;
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static b_status write_type_definition(
|
||||
struct mie_ir_converter *converter, struct mie_value *value)
|
||||
{
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static b_status write_record(
|
||||
struct mie_ir_converter *converter, struct mie_value *value)
|
||||
{
|
||||
struct mie_record *rec = MIE_RECORD(value);
|
||||
write_string_f(converter, "record %s", rec->r_base.v_name.n_str);
|
||||
|
||||
if (rec->r_value) {
|
||||
write_string(converter, " = ");
|
||||
write_operand_const(
|
||||
converter, MIE_VALUE(rec->r_value), F_INCLUDE_TYPE);
|
||||
}
|
||||
|
||||
write_char(converter, '\n');
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static b_status write_func_definition(
|
||||
struct mie_ir_converter *converter, struct mie_value *value)
|
||||
{
|
||||
struct mie_func *func = MIE_FUNC(value);
|
||||
|
||||
char type_name[32];
|
||||
mie_type_to_string(func->f_ret, type_name, sizeof type_name);
|
||||
|
||||
write_string_f(
|
||||
converter, "define %s @%s(", type_name, func->f_base.v_name.n_str);
|
||||
|
||||
unsigned int i = 0;
|
||||
struct b_queue_entry *entry = b_queue_first(&func->f_args);
|
||||
while (entry) {
|
||||
struct mie_arg *arg = (struct mie_arg *)b_unbox(
|
||||
struct mie_value, entry, v_entry);
|
||||
|
||||
if (i > 0) {
|
||||
write_string(converter, ", ");
|
||||
}
|
||||
|
||||
mie_type_to_string(func->f_ret, type_name, sizeof type_name);
|
||||
|
||||
write_string_f(
|
||||
converter, "%s %%%s", type_name,
|
||||
arg->arg_base.v_name.n_str);
|
||||
i++;
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
write_string_f(converter, ")");
|
||||
|
||||
switch (func->f_type) {
|
||||
case MIE_FUNC_STATIC:
|
||||
write_string_f(converter, " static");
|
||||
break;
|
||||
case MIE_FUNC_INSTANCE:
|
||||
write_string_f(converter, " instance");
|
||||
break;
|
||||
case MIE_FUNC_LAMBDA:
|
||||
write_string_f(converter, " lambda");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
write_string_f(converter, " {\n");
|
||||
|
||||
entry = b_queue_first(&func->f_blocks);
|
||||
while (entry) {
|
||||
struct mie_value *block
|
||||
= b_unbox(struct mie_value, entry, v_entry);
|
||||
write_value_to_text(converter, block);
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
write_string_f(converter, "}\n");
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static b_status write_block_definition(
|
||||
struct mie_ir_converter *converter, struct mie_value *value)
|
||||
{
|
||||
struct mie_block *block = MIE_BLOCK(value);
|
||||
|
||||
write_string_f(converter, "%s:\n", block->b_base.v_name.n_str);
|
||||
b_queue_entry *entry = b_queue_first(&block->b_phi);
|
||||
while (entry) {
|
||||
struct mie_value *instr
|
||||
= b_unbox(struct mie_value, entry, v_entry);
|
||||
write_value_to_text(converter, instr);
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
entry = b_queue_first(&block->b_instr);
|
||||
while (entry) {
|
||||
struct mie_value *instr
|
||||
= b_unbox(struct mie_value, entry, v_entry);
|
||||
write_value_to_text(converter, instr);
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
if (block->b_terminator) {
|
||||
write_value_to_text(converter, MIE_VALUE(block->b_terminator));
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static b_status write_instr(
|
||||
struct mie_ir_converter *converter, struct mie_value *value)
|
||||
{
|
||||
struct mie_instr *instr = MIE_INSTR(value);
|
||||
write_string_f(converter, "\t");
|
||||
|
||||
if (instr->i_base.v_name.n_str) {
|
||||
write_string_f(converter, "%%%s = ", instr->i_base.v_name.n_str);
|
||||
}
|
||||
|
||||
char type[128];
|
||||
|
||||
switch (instr->i_type) {
|
||||
case MIE_INSTR_RET: {
|
||||
struct mie_ret *ret = (struct mie_ret *)instr;
|
||||
write_string(converter, "ret ");
|
||||
if (!ret->r_val) {
|
||||
write_string(converter, "void");
|
||||
break;
|
||||
}
|
||||
|
||||
write_operand(converter, ret->r_val, F_INCLUDE_TYPE);
|
||||
break;
|
||||
}
|
||||
|
||||
case MIE_INSTR_ADD: {
|
||||
struct mie_binary_op *op = (struct mie_binary_op *)instr;
|
||||
mie_type_to_string(op->op_type, type, sizeof type);
|
||||
write_string_f(converter, "add %s ", type);
|
||||
write_operand(converter, op->op_left, 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_SUB: {
|
||||
struct mie_binary_op *op = (struct mie_binary_op *)instr;
|
||||
mie_type_to_string(op->op_type, type, sizeof type);
|
||||
write_string_f(converter, "sub %s ", type);
|
||||
write_operand(converter, op->op_left, 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_MUL: {
|
||||
struct mie_binary_op *op = (struct mie_binary_op *)instr;
|
||||
mie_type_to_string(op->op_type, type, sizeof type);
|
||||
write_string_f(converter, "mul %s ", type);
|
||||
write_operand(converter, op->op_left, 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_DIV: {
|
||||
struct mie_binary_op *op = (struct mie_binary_op *)instr;
|
||||
mie_type_to_string(op->op_type, type, sizeof type);
|
||||
write_string_f(converter, "div %s ", type);
|
||||
write_operand(converter, op->op_left, 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_LOAD: {
|
||||
struct mie_load *load = (struct mie_load *)instr;
|
||||
mie_type_to_string(load->l_type, type, sizeof type);
|
||||
write_string_f(converter, "load %s, ", type);
|
||||
write_operand(converter, load->l_src, F_INCLUDE_TYPE);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_STORE: {
|
||||
struct mie_store *store = (struct mie_store *)instr;
|
||||
write_string(converter, "store ");
|
||||
write_operand(converter, store->s_val, F_INCLUDE_TYPE);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, store->s_dest, F_INCLUDE_TYPE);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_ALLOCA: {
|
||||
struct mie_alloca *alloca = (struct mie_alloca *)instr;
|
||||
mie_type_to_string(alloca->a_type, type, sizeof type);
|
||||
write_string_f(converter, "alloca %s", type);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_SWITCH:
|
||||
write_string(converter, "switch");
|
||||
break;
|
||||
case MIE_INSTR_BR: {
|
||||
struct mie_branch *br = (struct mie_branch *)instr;
|
||||
write_string(converter, "br ");
|
||||
write_operand(converter, MIE_VALUE(br->b_dest), F_INCLUDE_TYPE);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_BR_IF: {
|
||||
struct mie_branch_if *br = (struct mie_branch_if *)instr;
|
||||
write_string(converter, "br ");
|
||||
write_operand(converter, MIE_VALUE(br->b_cond), F_INCLUDE_TYPE);
|
||||
write_string(converter, ", ");
|
||||
write_operand(
|
||||
converter, MIE_VALUE(br->b_true_block), F_INCLUDE_TYPE);
|
||||
write_string(converter, ", ");
|
||||
write_operand(
|
||||
converter, MIE_VALUE(br->b_false_block), F_INCLUDE_TYPE);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_MSG: {
|
||||
struct mie_msg *msg = (struct mie_msg *)instr;
|
||||
mie_type_to_string(msg->msg_ret_type, type, sizeof type);
|
||||
write_string_f(converter, "msg %s, ", type);
|
||||
write_operand(converter, msg->msg_recipient, F_INCLUDE_TYPE);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, msg->msg_selector, F_INCLUDE_TYPE);
|
||||
|
||||
if (msg->msg_nr_args == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
write_string(converter, " [");
|
||||
for (size_t i = 0; i < msg->msg_nr_args; i++) {
|
||||
if (i > 0) {
|
||||
write_string(converter, ", ");
|
||||
}
|
||||
|
||||
write_operand(converter, msg->msg_args[i], F_INCLUDE_TYPE);
|
||||
}
|
||||
write_string(converter, "]");
|
||||
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_CMP_EQ: {
|
||||
struct mie_binary_op *op = (struct mie_binary_op *)instr;
|
||||
mie_type_to_string(op->op_type, type, sizeof type);
|
||||
write_string_f(converter, "cmp eq %s ", type);
|
||||
write_operand(converter, op->op_left, 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_CMP_NEQ: {
|
||||
struct mie_binary_op *op = (struct mie_binary_op *)instr;
|
||||
mie_type_to_string(op->op_type, type, sizeof type);
|
||||
write_string_f(converter, "cmp neq %s ", type);
|
||||
write_operand(converter, op->op_left, 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_CMP_LT: {
|
||||
struct mie_binary_op *op = (struct mie_binary_op *)instr;
|
||||
mie_type_to_string(op->op_type, type, sizeof type);
|
||||
write_string_f(converter, "cmp lt %s ", type);
|
||||
write_operand(converter, op->op_left, 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_CMP_LEQ: {
|
||||
struct mie_binary_op *op = (struct mie_binary_op *)instr;
|
||||
mie_type_to_string(op->op_type, type, sizeof type);
|
||||
write_string_f(converter, "cmp leq %s ", type);
|
||||
write_operand(converter, op->op_left, 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_CMP_GT: {
|
||||
struct mie_binary_op *op = (struct mie_binary_op *)instr;
|
||||
mie_type_to_string(op->op_type, type, sizeof type);
|
||||
write_string_f(converter, "cmp gt %s ", type);
|
||||
write_operand(converter, op->op_left, 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_CMP_GEQ: {
|
||||
struct mie_binary_op *op = (struct mie_binary_op *)instr;
|
||||
mie_type_to_string(op->op_type, type, sizeof type);
|
||||
write_string_f(converter, "cmp geq %s ", type);
|
||||
write_operand(converter, op->op_left, 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_GETELEMENTPTR:
|
||||
write_string(converter, "getelementptr");
|
||||
break;
|
||||
case MIE_INSTR_SETELEMENTPTR:
|
||||
write_string(converter, "setelementptr");
|
||||
break;
|
||||
case MIE_INSTR_PHI: {
|
||||
struct mie_phi *phi = (struct mie_phi *)instr;
|
||||
mie_type_to_string(phi->p_type, type, sizeof type);
|
||||
write_string_f(converter, "phi %s ", type);
|
||||
|
||||
for (size_t i = 0; i < phi->p_nr_edges; i++) {
|
||||
if (i > 0) {
|
||||
write_string(converter, ", ");
|
||||
}
|
||||
|
||||
write_string(converter, "[ ");
|
||||
write_operand(
|
||||
converter,
|
||||
MIE_VALUE(phi->p_edges[i].e_incoming_block), 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, phi->p_edges[i].e_value, 0);
|
||||
write_string(converter, " ]");
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
write_string_f(converter, "<unknown>");
|
||||
break;
|
||||
}
|
||||
|
||||
write_char(converter, '\n');
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static b_status write_data(
|
||||
struct mie_ir_converter *converter, struct mie_value *value)
|
||||
{
|
||||
char type_name[128];
|
||||
|
||||
struct mie_data *data = MIE_DATA(value);
|
||||
write_string_f(converter, "data @%s = ", value->v_name.n_str);
|
||||
|
||||
switch (data->d_type) {
|
||||
case MIE_DATA_EXTERN_GLOBAL:
|
||||
mie_type_to_string(
|
||||
data->d_extern_global.g_type, type_name, sizeof type_name);
|
||||
write_string_f(converter, "extern global %s", type_name);
|
||||
break;
|
||||
case MIE_DATA_CONST:
|
||||
write_operand_const(
|
||||
converter, MIE_VALUE(data->d_const.c_value),
|
||||
F_INCLUDE_TYPE);
|
||||
break;
|
||||
default:
|
||||
write_string(converter, "<unknown>");
|
||||
break;
|
||||
}
|
||||
|
||||
write_char(converter, '\n');
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static const write_function value_writers[] = {
|
||||
[MIE_VALUE_MODULE] = write_module,
|
||||
[MIE_VALUE_TYPE] = write_type_definition,
|
||||
[MIE_VALUE_RECORD] = write_record,
|
||||
[MIE_VALUE_FUNC] = write_func_definition,
|
||||
[MIE_VALUE_ARG] = NULL,
|
||||
[MIE_VALUE_BLOCK] = write_block_definition,
|
||||
[MIE_VALUE_INSTR] = write_instr,
|
||||
[MIE_VALUE_DATA] = write_data,
|
||||
[MIE_VALUE_CONST] = NULL,
|
||||
};
|
||||
static const size_t nr_value_printers
|
||||
= sizeof value_writers / sizeof value_writers[0];
|
||||
|
||||
b_status write_value_to_text(
|
||||
struct mie_ir_converter *converter, struct mie_value *value)
|
||||
{
|
||||
write_function writer = value_writers[value->v_type->t_id];
|
||||
return writer(converter, value);
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
#include <blue/ds/string.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/ir/data.h>
|
||||
#include <mie/type.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct mie_data *mie_data_create_extern_global(
|
||||
struct mie_type *type, const char *ident)
|
||||
{
|
||||
struct mie_data *data = malloc(sizeof *data);
|
||||
if (!data) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(data, 0x0, sizeof *data);
|
||||
|
||||
mie_value_init(&data->d_base, MIE_VALUE_DATA);
|
||||
|
||||
data->d_type = MIE_DATA_EXTERN_GLOBAL;
|
||||
data->d_extern_global.g_type = type;
|
||||
data->d_base.v_name.n_str = b_strdup(ident);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
struct mie_data *mie_data_create_const(struct mie_const *value)
|
||||
{
|
||||
struct mie_data *data = malloc(sizeof *data);
|
||||
if (!data) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(data, 0x0, sizeof *data);
|
||||
|
||||
mie_value_init(&data->d_base, MIE_VALUE_DATA);
|
||||
|
||||
data->d_type = MIE_DATA_CONST;
|
||||
data->d_const.c_value = value;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
static struct mie_type *get_type(struct mie_value *v, struct mie_ctx *ctx)
|
||||
{
|
||||
return mie_ctx_get_type(ctx, MIE_TYPE_PTR);
|
||||
}
|
||||
|
||||
const struct mie_value_type data_value_type = {
|
||||
.t_id = MIE_VALUE_DATA,
|
||||
.t_get_type = get_type,
|
||||
};
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user