mie: implement a printer system for converting IR to textual form

This commit is contained in:
2026-01-12 10:33:08 +00:00
parent 826380ea34
commit 49df8616a8
24 changed files with 511 additions and 108 deletions

View File

@@ -3,6 +3,7 @@
#include <mie/dialect/arith.h>
#include <mie/dialect/dialect.h>
#include <mie/macros.h>
#include <mie/print/printer.h>
#include <mie/type/type-definition.h>
#include <mie/type/type.h>
#include <mie/value.h>
@@ -13,23 +14,24 @@ struct float_type {
};
static enum mie_status value_print(
const struct mie_type *ty, const struct mie_value *value, b_stream *out)
const struct mie_value *value, struct mie_printer *out)
{
struct float_type *float_ty = (struct float_type *)ty;
struct float_type *float_ty = (struct float_type *)value->v_type;
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,
out->p_stream, 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);
out->p_stream, 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);
b_stream_write_fmt(
out->p_stream, NULL, "NaN : f%zu", float_ty->f_width);
break;
}
@@ -83,10 +85,15 @@ size_t mie_arith_float_type_get_width(const struct mie_type *type)
return float_type->f_width;
}
static enum mie_status print(
const struct mie_type_definition *def, const struct mie_type *ty,
b_stream *out)
static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
{
const struct float_type *float_type = (const struct float_type *)ty;
b_stream_write_fmt(
out->p_stream, NULL,
(out->p_flags & MIE_PRINT_F_ABBREVIATED) ? "f%zu"
: "arith.float<%zu>",
float_type->f_width);
return MIE_SUCCESS;
}

View File

@@ -3,6 +3,7 @@
#include <mie/dialect/arith.h>
#include <mie/dialect/dialect.h>
#include <mie/macros.h>
#include <mie/print/printer.h>
#include <mie/type/type-definition.h>
#include <mie/type/type.h>
#include <mie/value.h>
@@ -13,16 +14,17 @@ struct int_type {
};
static enum mie_status value_print(
const struct mie_type *ty, const struct mie_value *value, b_stream *out)
const struct mie_value *value, struct mie_printer *out)
{
struct int_type *int_ty = (struct int_type *)ty;
struct int_type *int_ty = (struct int_type *)value->v_type;
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);
out->p_stream, 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);
b_stream_write_fmt(
out->p_stream, NULL, "INF : i%zu", int_ty->i_width);
}
return MIE_SUCCESS;
@@ -75,10 +77,14 @@ size_t mie_arith_int_type_get_width(const struct mie_type *type)
return int_type->i_width;
}
static enum mie_status print(
const struct mie_type_definition *def, const struct mie_type *ty,
b_stream *out)
static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
{
const struct int_type *int_type = (const struct int_type *)ty;
b_stream_write_fmt(
out->p_stream, NULL,
(out->p_flags & MIE_PRINT_F_ABBREVIATED) ? "i%zu" : "arith.int<%zu>",
int_type->i_width);
return MIE_SUCCESS;
}

View File

@@ -3,15 +3,16 @@
#include <mie/dialect/builtin.h>
#include <mie/dialect/dialect.h>
#include <mie/macros.h>
#include <mie/print/printer.h>
#include <mie/type/type-definition.h>
#include <mie/type/type.h>
#include <mie/value.h>
static enum mie_status value_print(
const struct mie_type *ty, const struct mie_value *value, b_stream *out)
const struct mie_value *value, struct mie_printer *out)
{
const struct mie_string *str = (const struct mie_string *)value;
b_stream_write_fmt(out, NULL, "\"%s\"", str->str_val);
b_stream_write_fmt(out->p_stream, NULL, "\"%s\"", str->str_val);
return MIE_SUCCESS;
}
@@ -21,9 +22,7 @@ static void type_init(
type->ty_instance_size = sizeof(struct mie_string);
}
static enum mie_status print(
const struct mie_type_definition *def, const struct mie_type *ty,
b_stream *out)
static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
{
return MIE_SUCCESS;
}

View File

@@ -1,6 +1,7 @@
#include <mie/dialect/dialect.h>
#include <mie/dialect/index.h>
#include <mie/macros.h>
#include <mie/print/printer.h>
#include <mie/type/type-definition.h>
#include <mie/type/type.h>
#include <mie/value.h>
@@ -10,12 +11,12 @@ struct index_type {
};
static enum mie_status value_print(
const struct mie_type *ty, const struct mie_value *value, b_stream *out)
const struct mie_value *value, struct mie_printer *out)
{
struct index_type *index_ty = (struct index_type *)ty;
struct index_type *index_ty = (struct index_type *)value->v_type;
struct mie_index *index_val = (struct mie_index *)value;
b_stream_write_fmt(
out, NULL, "%zu : %s", index_val->i_value,
out->p_stream, NULL, "%zu : %s", index_val->i_value,
index_ty->i_base.ty_def->ty_name);
return MIE_SUCCESS;
}
@@ -26,10 +27,12 @@ static void type_init(
type->ty_instance_size = sizeof(struct mie_index);
}
static enum mie_status print(
const struct mie_type_definition *def, const struct mie_type *ty,
b_stream *out)
static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
{
b_stream_write_string(
out->p_stream,
(out->p_flags & MIE_PRINT_F_ABBREVIATED) ? "index" : "index.index",
NULL);
return MIE_SUCCESS;
}

View File

@@ -6,9 +6,7 @@ struct ptr_type {
struct mie_type_definition ptr_base;
};
static enum mie_status print(
const struct mie_type_definition *def, const struct mie_type *ty,
b_stream *out)
static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
{
return MIE_SUCCESS;
}