mie: arith: add support for int and float values

This commit is contained in:
2026-01-04 18:56:03 +00:00
parent 7c7e5e7af0
commit 8b3093411a
2 changed files with 82 additions and 1 deletions

View File

@@ -1,12 +1,73 @@
#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_dialect_type f_base;
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_width = bit_width;
type->f_base.ty_instance_size = sizeof(struct mie_float);
type->f_base.ty_value_print = value_print;
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)
{
@@ -21,6 +82,7 @@ static enum mie_status parse(
}
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);

View File

@@ -1,15 +1,31 @@
#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
@@ -37,6 +53,8 @@ struct mie_type *mie_arith_int_get_type(struct mie_ctx *ctx, size_t bit_width)
type->i_base.ty_name = b_bstr_fmt("arith.int<%zu>", bit_width);
type->i_width = bit_width;
type->i_base.ty_instance_size = sizeof(struct mie_int);
type->i_base.ty_value_print = value_print;
mie_id_map_put(&ctx->ctx_types, &type->i_base.ty_id, &type_name);
return (struct mie_type *)type;
@@ -56,6 +74,7 @@ static enum mie_status parse(
}
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);