Files
mie/mie/dialect/arith/float.c

90 lines
2.3 KiB
C

#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_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)
{
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_END()