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/arith.h>
#include <mie/dialect/dialect.h> #include <mie/dialect/dialect.h>
#include <mie/macros.h> #include <mie/macros.h>
#include <mie/print/printer.h>
#include <mie/type/type-definition.h> #include <mie/type/type-definition.h>
#include <mie/type/type.h> #include <mie/type/type.h>
#include <mie/value.h> #include <mie/value.h>
@@ -13,23 +14,24 @@ struct float_type {
}; };
static enum mie_status value_print( 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; struct mie_float *float_val = (struct mie_float *)value;
switch (float_ty->f_width) { switch (float_ty->f_width) {
case MIE_FLOAT_32: case MIE_FLOAT_32:
b_stream_write_fmt( 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); float_ty->f_width);
break; break;
case MIE_FLOAT_64: case MIE_FLOAT_64:
b_stream_write_fmt( b_stream_write_fmt(
out, NULL, "%lf : f%zu", float_val->f_val.v_64, out->p_stream, NULL, "%lf : f%zu",
float_ty->f_width); float_val->f_val.v_64, float_ty->f_width);
break; break;
default: 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; break;
} }
@@ -83,10 +85,15 @@ size_t mie_arith_float_type_get_width(const struct mie_type *type)
return float_type->f_width; return float_type->f_width;
} }
static enum mie_status print( static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
const struct mie_type_definition *def, const struct mie_type *ty,
b_stream *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; return MIE_SUCCESS;
} }

View File

@@ -3,6 +3,7 @@
#include <mie/dialect/arith.h> #include <mie/dialect/arith.h>
#include <mie/dialect/dialect.h> #include <mie/dialect/dialect.h>
#include <mie/macros.h> #include <mie/macros.h>
#include <mie/print/printer.h>
#include <mie/type/type-definition.h> #include <mie/type/type-definition.h>
#include <mie/type/type.h> #include <mie/type/type.h>
#include <mie/value.h> #include <mie/value.h>
@@ -13,16 +14,17 @@ struct int_type {
}; };
static enum mie_status value_print( 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; struct mie_int *int_val = (struct mie_int *)value;
if (int_ty->i_width <= 64) { if (int_ty->i_width <= 64) {
b_stream_write_fmt( b_stream_write_fmt(
out, NULL, "%zu : i%zu", int_val->i_val.v_small, out->p_stream, NULL, "%zu : i%zu",
int_ty->i_width); int_val->i_val.v_small, int_ty->i_width);
} else { } 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; 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; return int_type->i_width;
} }
static enum mie_status print( static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
const struct mie_type_definition *def, const struct mie_type *ty,
b_stream *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; return MIE_SUCCESS;
} }

View File

@@ -3,15 +3,16 @@
#include <mie/dialect/builtin.h> #include <mie/dialect/builtin.h>
#include <mie/dialect/dialect.h> #include <mie/dialect/dialect.h>
#include <mie/macros.h> #include <mie/macros.h>
#include <mie/print/printer.h>
#include <mie/type/type-definition.h> #include <mie/type/type-definition.h>
#include <mie/type/type.h> #include <mie/type/type.h>
#include <mie/value.h> #include <mie/value.h>
static enum mie_status value_print( 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; 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; return MIE_SUCCESS;
} }
@@ -21,9 +22,7 @@ static void type_init(
type->ty_instance_size = sizeof(struct mie_string); type->ty_instance_size = sizeof(struct mie_string);
} }
static enum mie_status print( static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
const struct mie_type_definition *def, const struct mie_type *ty,
b_stream *out)
{ {
return MIE_SUCCESS; return MIE_SUCCESS;
} }

View File

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

View File

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

View File

@@ -1,6 +1,7 @@
#ifndef MIE_IR_MODULE_H_ #ifndef MIE_IR_MODULE_H_
#define MIE_IR_MODULE_H_ #define MIE_IR_MODULE_H_
#include <blue/core/stream.h>
#include <mie/misc.h> #include <mie/misc.h>
#include <mie/name.h> #include <mie/name.h>
#include <mie/vector.h> #include <mie/vector.h>

View File

@@ -0,0 +1,47 @@
#ifndef MIE_PRINT_PRINTER_H_
#define MIE_PRINT_PRINTER_H_
#include <blue/core/stream.h>
#include <mie/misc.h>
#include <mie/status.h>
struct mie_ctx;
struct mie_module;
struct mie_op;
struct mie_region;
struct mie_block;
struct mie_type;
struct mie_value;
struct mie_register;
enum mie_print_flags {
MIE_PRINT_F_GENERIC = 0x01u,
MIE_PRINT_F_ABBREVIATED = 0x02u,
};
struct mie_printer {
enum mie_print_flags p_flags;
struct mie_ctx *p_mie;
b_stream *p_stream;
};
MIE_API enum mie_status mie_printer_init(
struct mie_printer *printer, struct mie_ctx *ctx, b_stream *out,
enum mie_print_flags flags);
MIE_API void mie_printer_print_module(
struct mie_printer *printer, const struct mie_module *mod);
MIE_API void mie_printer_print_op(
struct mie_printer *printer, const struct mie_op *op);
MIE_API void mie_printer_print_region(
struct mie_printer *printer, const struct mie_region *region);
MIE_API void mie_printer_print_block(
struct mie_printer *printer, const struct mie_block *block);
MIE_API void mie_printer_print_type(
struct mie_printer *printer, const struct mie_type *type);
MIE_API void mie_printer_print_value(
struct mie_printer *printer, const struct mie_value *value);
MIE_API void mie_printer_print_register(
struct mie_printer *printer, const struct mie_register *reg);
#endif

View File

@@ -14,6 +14,7 @@ enum mie_status {
MIE_ERR_BAD_FORMAT, MIE_ERR_BAD_FORMAT,
MIE_ERR_NOT_SUPPORTED, MIE_ERR_NOT_SUPPORTED,
MIE_ERR_INVALID_VALUE, MIE_ERR_INVALID_VALUE,
MIE_ERR_INVALID_ARGUMENT,
MIE_ERR_INTERNAL_FAILURE, MIE_ERR_INTERNAL_FAILURE,
MIE_ERR_NO_MEMORY, MIE_ERR_NO_MEMORY,
MIE_ERR_NO_ENTRY, MIE_ERR_NO_ENTRY,

View File

@@ -18,8 +18,6 @@ MIE_API void mie_function_type_add_in_part(
MIE_API void mie_function_type_add_out_part( MIE_API void mie_function_type_add_out_part(
struct mie_function_type *ty, const struct mie_type *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( MIE_API void mie_function_type_build_id(
struct mie_id_builder *builder, const struct mie_type **in_types, 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_in_types, const struct mie_type **out_types,

View File

@@ -15,8 +15,6 @@ MIE_API struct mie_storage_type *mie_storage_type_create(void);
MIE_API void mie_storage_type_add_part( MIE_API void mie_storage_type_add_part(
struct mie_storage_type *ty, const struct mie_type *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( MIE_API void mie_storage_type_build_id(
struct mie_id_builder *builder, const struct mie_type **parts, struct mie_id_builder *builder, const struct mie_type **parts,
size_t nr_parts); size_t nr_parts);

View File

@@ -6,6 +6,7 @@
struct mie_value; struct mie_value;
struct mie_type; struct mie_type;
struct mie_parser; struct mie_parser;
struct mie_printer;
/* a static struct that defines the properties, traits, and callbacks for a type. */ /* a static struct that defines the properties, traits, and callbacks for a type. */
struct mie_type_definition { struct mie_type_definition {
@@ -19,11 +20,9 @@ struct mie_type_definition {
void (*ty_init)(const struct mie_type_definition *, struct mie_type *); void (*ty_init)(const struct mie_type_definition *, struct mie_type *);
void (*ty_cleanup)(const struct mie_type_definition *, struct mie_type *); void (*ty_cleanup)(const struct mie_type_definition *, struct mie_type *);
void (*ty_instance_cleanup)(const struct mie_type *, struct mie_value *); void (*ty_instance_cleanup)(const struct mie_type *, struct mie_value *);
enum mie_status (*ty_print)( enum mie_status (*ty_print)(const struct mie_type *, struct mie_printer *);
const struct mie_type_definition *, const struct mie_type *,
b_stream *);
enum mie_status (*ty_value_print)( enum mie_status (*ty_value_print)(
const struct mie_type *, const struct mie_value *, b_stream *); const struct mie_value *, struct mie_printer *);
enum mie_status (*ty_parse)( enum mie_status (*ty_parse)(
const struct mie_type_definition *, struct mie_parser *, const struct mie_type_definition *, struct mie_parser *,
struct mie_type **); struct mie_type **);

View File

@@ -42,7 +42,6 @@ struct mie_type {
}; };
MIE_API struct mie_type *mie_type_create(struct mie_type_definition *type); MIE_API struct mie_type *mie_type_create(struct mie_type_definition *type);
MIE_API void mie_type_print(const struct mie_type *type, b_stream *out);
MIE_API void mie_type_build_id( MIE_API void mie_type_build_id(
const struct mie_type *type, struct mie_id_builder *ctx); const struct mie_type *type, struct mie_id_builder *ctx);
MIE_API void mie_type_generate_id( MIE_API void mie_type_generate_id(

View File

@@ -1,7 +1,6 @@
#ifndef MIE_VALUE_VALUE_H_ #ifndef MIE_VALUE_VALUE_H_
#define MIE_VALUE_VALUE_H_ #define MIE_VALUE_VALUE_H_
#include <blue/core/stream.h>
#include <mie/misc.h> #include <mie/misc.h>
struct mie_type; struct mie_type;
@@ -10,6 +9,4 @@ struct mie_value {
struct mie_type *v_type; struct mie_type *v_type;
}; };
MIE_API void mie_value_print(const struct mie_value *value, b_stream *dest);
#endif #endif

61
mie/print/block.c Normal file
View File

@@ -0,0 +1,61 @@
#include <mie/ir/block.h>
#include <mie/ir/op.h>
#include <mie/ir/register.h>
#include <mie/print/printer.h>
static void print_block_header_params(
struct mie_printer *printer, const struct mie_block *block)
{
if (!MIE_VECTOR_COUNT(block->b_params)) {
return;
}
b_stream_write_char(printer->p_stream, '(');
for (size_t i = 0; i < MIE_VECTOR_COUNT(block->b_params); i++) {
if (i > 0) {
b_stream_write_string(printer->p_stream, ", ", NULL);
}
mie_printer_print_register(printer, block->b_params.items[i]);
b_stream_write_string(printer->p_stream, ": ", NULL);
mie_printer_print_type(printer, block->b_params.items[i]->reg_type);
}
b_stream_write_char(printer->p_stream, ')');
}
static void print_block_header(
struct mie_printer *printer, const struct mie_block *block)
{
if (!block->b_name.n_str && !MIE_VECTOR_COUNT(block->b_params)) {
return;
}
b_stream_write_char(printer->p_stream, '^');
if (block->b_name.n_str) {
b_stream_write_string(printer->p_stream, block->b_name.n_str, NULL);
}
print_block_header_params(printer, block);
b_stream_write_string(printer->p_stream, ":\n", NULL);
}
void mie_printer_print_block(
struct mie_printer *printer, const struct mie_block *block)
{
print_block_header(printer, block);
b_stream_push_indent(printer->p_stream, 4);
for (size_t i = 0; i < MIE_VECTOR_COUNT(block->b_ops); i++) {
const struct mie_op *op = &block->b_ops.items[i];
mie_printer_print_op(printer, op);
b_stream_write_char(printer->p_stream, '\n');
}
b_stream_pop_indent(printer->p_stream);
}

12
mie/print/module.c Normal file
View File

@@ -0,0 +1,12 @@
#include <mie/ir/module.h>
#include <mie/ir/op.h>
#include <mie/print/printer.h>
void mie_printer_print_module(
struct mie_printer *printer, const struct mie_module *mod)
{
for (size_t i = 0; i < MIE_VECTOR_COUNT(mod->m_ops); i++) {
mie_printer_print_op(printer, &mod->m_ops.items[i]);
b_stream_write_char(printer->p_stream, '\n');
}
}

220
mie/print/op.c Normal file
View File

@@ -0,0 +1,220 @@
#include <mie/dialect/dialect.h>
#include <mie/ir/block.h>
#include <mie/ir/op-definition.h>
#include <mie/ir/op.h>
#include <mie/ir/region.h>
#include <mie/print/printer.h>
static void print_op_arg(
struct mie_printer *printer, const struct mie_op_arg *arg, bool include_type)
{
enum mie_register_flags arg_flags = 0;
const char *arg_name = NULL;
const struct mie_type *arg_type = NULL;
if (MIE_TEST_FLAGS(arg->arg_flags, MIE_OP_F_ARG_RESOLVED)) {
arg_flags = arg->arg_value->reg_flags;
arg_name = arg->arg_value->reg_name.n_str;
arg_type = arg->arg_value->reg_type;
} else {
arg_flags = arg->arg_unresolved.reg_flags;
arg_name = arg->arg_unresolved.reg_name;
arg_type = arg->arg_unresolved.reg_type;
}
if (arg_flags & MIE_REGISTER_F_MACHINE) {
b_stream_write_char(printer->p_stream, '$');
} else {
b_stream_write_char(printer->p_stream, '%');
}
b_stream_write_string(printer->p_stream, arg_name, NULL);
if (!include_type) {
return;
}
b_stream_write_string(printer->p_stream, ": ", NULL);
mie_printer_print_type(printer, arg_type);
}
static void print_successor(
struct mie_printer *printer, const struct mie_op_successor *successor)
{
b_stream_write_char(printer->p_stream, '^');
if (successor->s_flags & MIE_OP_F_SUCCESSOR_RESOLVED) {
b_stream_write_string(
printer->p_stream, successor->s_block->b_name.n_str, NULL);
} else {
b_stream_write_string(
printer->p_stream, successor->s_block_name, NULL);
}
if (MIE_VECTOR_COUNT(successor->s_args) == 0) {
return;
}
b_stream_write_string(printer->p_stream, ":(", NULL);
for (size_t i = 0; i < MIE_VECTOR_COUNT(successor->s_args); i++) {
if (i > 0) {
b_stream_write_string(printer->p_stream, ", ", NULL);
}
print_op_arg(printer, &successor->s_args.items[i], true);
}
b_stream_write_char(printer->p_stream, ')');
}
static void print_successor_list(
struct mie_printer *printer, const struct mie_op *op)
{
if (!MIE_VECTOR_COUNT(op->op_successors)) {
return;
}
b_stream_write_string(printer->p_stream, " [ ", NULL);
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_successors); i++) {
if (i > 0) {
b_stream_write_string(printer->p_stream, ", ", NULL);
}
print_successor(printer, &op->op_successors.items[i]);
}
b_stream_write_string(printer->p_stream, " ]", NULL);
}
static void print_region_list(struct mie_printer *printer, const struct mie_op *op)
{
if (!MIE_VECTOR_COUNT(op->op_regions)) {
return;
}
b_stream_write_string(printer->p_stream, " (", NULL);
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_regions); i++) {
if (i > 0) {
b_stream_write_string(printer->p_stream, ", ", NULL);
}
mie_printer_print_region(printer, &op->op_regions.items[i]);
}
b_stream_write_string(printer->p_stream, ")", NULL);
}
static void print_attribute(
struct mie_printer *printer, const struct mie_op_attribute *attrib)
{
b_stream_write_fmt(printer->p_stream, NULL, "%s = ", attrib->attrib_name);
mie_printer_print_value(printer, attrib->attrib_value);
}
static void print_attribute_list(
struct mie_printer *printer, const struct mie_op *op)
{
if (!MIE_VECTOR_COUNT(op->op_attrib)) {
return;
}
b_stream_write_string(printer->p_stream, " { ", NULL);
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_attrib); i++) {
if (i > 0) {
b_stream_write_string(printer->p_stream, ", ", NULL);
}
print_attribute(printer, &op->op_attrib.items[i]);
}
b_stream_write_string(printer->p_stream, " }", NULL);
}
static void print_type_signature(
struct mie_printer *printer, const struct mie_op *op)
{
const struct mie_type *type = NULL;
b_stream_write_string(printer->p_stream, " : (", NULL);
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_args); i++) {
if (i > 0) {
b_stream_write_string(printer->p_stream, ", ", NULL);
}
const struct mie_op_arg *arg = &op->op_args.items[i];
if (arg->arg_flags & MIE_OP_F_ARG_RESOLVED) {
type = arg->arg_value->reg_type;
} else {
type = arg->arg_unresolved.reg_type;
}
mie_printer_print_type(printer, type);
}
b_stream_write_string(printer->p_stream, ") -> ", NULL);
if (MIE_VECTOR_COUNT(op->op_result) != 1) {
b_stream_write_char(printer->p_stream, '(');
}
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_result); i++) {
if (i > 0) {
b_stream_write_string(printer->p_stream, ", ", NULL);
}
type = op->op_result.items[i]->reg_type;
mie_printer_print_type(printer, type);
}
if (MIE_VECTOR_COUNT(op->op_result) != 1) {
b_stream_write_char(printer->p_stream, ')');
}
}
void mie_printer_print_op(struct mie_printer *printer, const struct mie_op *op)
{
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_result); i++) {
if (i > 0) {
b_stream_write_string(printer->p_stream, ", ", NULL);
}
mie_printer_print_register(printer, op->op_result.items[i]);
}
if (MIE_VECTOR_COUNT(op->op_result) > 0) {
b_stream_write_string(printer->p_stream, " = ", NULL);
}
if (printer->p_flags & MIE_PRINT_F_GENERIC) {
b_stream_write_char(printer->p_stream, '~');
}
if (op->op_flags & MIE_OP_F_OP_RESOLVED) {
b_stream_write_fmt(
printer->p_stream, NULL, "%s.%s",
op->op_dialect->d_name, op->op_info->op_name);
} else {
b_stream_write_string(printer->p_stream, op->op_name, NULL);
}
b_stream_write_char(printer->p_stream, '(');
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_args); i++) {
if (i > 0) {
b_stream_write_string(printer->p_stream, ", ", NULL);
}
print_op_arg(printer, &op->op_args.items[i], false);
}
b_stream_write_char(printer->p_stream, ')');
print_successor_list(printer, op);
print_region_list(printer, op);
print_attribute_list(printer, op);
print_type_signature(printer, op);
}

18
mie/print/printer.c Normal file
View File

@@ -0,0 +1,18 @@
#include <mie/print/printer.h>
enum mie_status mie_printer_init(
struct mie_printer *printer, struct mie_ctx *ctx, b_stream *out,
enum mie_print_flags flags)
{
memset(printer, 0x0, sizeof *printer);
if (!(flags & MIE_PRINT_F_GENERIC) && !ctx) {
return MIE_ERR_INVALID_ARGUMENT;
}
printer->p_mie = ctx;
printer->p_stream = out;
printer->p_flags = flags;
return MIE_SUCCESS;
}

16
mie/print/region.c Normal file
View File

@@ -0,0 +1,16 @@
#include <mie/ir/block.h>
#include <mie/ir/region.h>
#include <mie/print/printer.h>
void mie_printer_print_region(
struct mie_printer *printer, const struct mie_region *region)
{
b_stream_write_string(printer->p_stream, "{\n", NULL);
for (size_t i = 0; i < MIE_VECTOR_COUNT(region->r_blocks); i++) {
const struct mie_block *block = region->r_blocks.items[i];
mie_printer_print_block(printer, block);
}
b_stream_write_string(printer->p_stream, "}", NULL);
}

17
mie/print/register.c Normal file
View File

@@ -0,0 +1,17 @@
#include <mie/ir/register.h>
#include <mie/print/printer.h>
void mie_printer_print_register(
struct mie_printer *printer, const struct mie_register *reg)
{
if (reg->reg_flags & MIE_REGISTER_F_VIRTUAL) {
b_stream_write_fmt(
printer->p_stream, NULL, "%%%s", reg->reg_name.n_str);
} else if (reg->reg_flags & MIE_REGISTER_F_MACHINE) {
// TODO this shouldnt use reg_name
b_stream_write_fmt(
printer->p_stream, NULL, "$%s", reg->reg_name.n_str);
} else {
b_stream_write_string(printer->p_stream, "?REG", NULL);
}
}

17
mie/print/type.c Normal file
View File

@@ -0,0 +1,17 @@
#include <mie/print/printer.h>
#include <mie/type/type-definition.h>
#include <mie/type/type.h>
void mie_printer_print_type(struct mie_printer *printer, const struct mie_type *type)
{
if (type->ty_def && type->ty_def->ty_print) {
type->ty_def->ty_print(type, printer);
} else if (type->ty_name) {
b_stream_write_string(printer->p_stream, type->ty_name, NULL);
} else if (type->ty_def && type->ty_def->ty_name) {
b_stream_write_string(
printer->p_stream, type->ty_def->ty_name, NULL);
} else {
b_stream_write_string(printer->p_stream, "<UNNAMED-TYPE>", NULL);
}
}

View File

@@ -2,9 +2,10 @@
#include <mie/type/type.h> #include <mie/type/type.h>
#include <mie/value.h> #include <mie/value.h>
void mie_value_print(const struct mie_value *value, b_stream *dest) void mie_printer_print_value(
struct mie_printer *printer, const struct mie_value *value)
{ {
if (value->v_type && value->v_type->ty_def->ty_value_print) { if (value->v_type && value->v_type->ty_def->ty_value_print) {
value->v_type->ty_def->ty_value_print(value->v_type, value, dest); value->v_type->ty_def->ty_value_print(value, printer);
} }
} }

View File

@@ -1,3 +1,4 @@
#include <mie/print/printer.h>
#include <mie/type/function.h> #include <mie/type/function.h>
#include <mie/type/type-definition.h> #include <mie/type/type-definition.h>
#include <stdlib.h> #include <stdlib.h>
@@ -13,10 +14,39 @@ static void build_id(const struct mie_type *type, struct mie_id_builder *ctx)
} }
static enum mie_status type_print( static enum mie_status type_print(
const struct mie_type_definition *type_info, const struct mie_type *type, struct mie_printer *out)
const struct mie_type *type, b_stream *out)
{ {
mie_function_type_print((const struct mie_function_type *)type, out); const struct mie_function_type *func
= (const struct mie_function_type *)type;
b_stream_write_char(out->p_stream, '(');
for (size_t i = 0; i < MIE_VECTOR_COUNT(func->func_in); i++) {
if (i > 0) {
b_stream_write_string(out->p_stream, ", ", NULL);
}
mie_printer_print_type(out, MIE_VECTOR_ITEM(func->func_in, i));
}
b_stream_write_string(out->p_stream, ") -> ", NULL);
if (MIE_VECTOR_COUNT(func->func_out) == 1) {
mie_printer_print_type(out, MIE_VECTOR_ITEM(func->func_out, 0));
return MIE_SUCCESS;
}
b_stream_write_char(out->p_stream, '(');
for (size_t i = 0; i < MIE_VECTOR_COUNT(func->func_out); i++) {
if (i > 0) {
b_stream_write_string(out->p_stream, ", ", NULL);
}
mie_printer_print_type(out, MIE_VECTOR_ITEM(func->func_out, i));
}
b_stream_write_char(out->p_stream, ')');
return MIE_SUCCESS; return MIE_SUCCESS;
} }
@@ -53,36 +83,9 @@ void mie_function_type_add_out_part(
mie_vector_push_back(ty->func_out, &part); mie_vector_push_back(ty->func_out, &part);
} }
void mie_function_type_print(const struct mie_function_type *type, b_stream *out) void mie_function_type_print(
const struct mie_function_type *type, struct mie_printer *out)
{ {
b_stream_write_char(out, '(');
for (size_t i = 0; i < MIE_VECTOR_COUNT(type->func_in); i++) {
if (i > 0) {
b_stream_write_string(out, ", ", NULL);
}
mie_type_print(MIE_VECTOR_ITEM(type->func_in, i), out);
}
b_stream_write_string(out, ") -> ", NULL);
if (MIE_VECTOR_COUNT(type->func_out) == 1) {
mie_type_print(MIE_VECTOR_ITEM(type->func_out, 0), out);
return;
}
b_stream_write_char(out, '(');
for (size_t i = 0; i < MIE_VECTOR_COUNT(type->func_out); i++) {
if (i > 0) {
b_stream_write_string(out, ", ", NULL);
}
mie_type_print(MIE_VECTOR_ITEM(type->func_out, i), out);
}
b_stream_write_char(out, ')');
} }
void mie_function_type_build_id( void mie_function_type_build_id(

View File

@@ -1,3 +1,4 @@
#include <mie/print/printer.h>
#include <mie/type/storage.h> #include <mie/type/storage.h>
#include <mie/type/type-definition.h> #include <mie/type/type-definition.h>
#include <stdlib.h> #include <stdlib.h>
@@ -12,10 +13,22 @@ static void build_id(const struct mie_type *type, struct mie_id_builder *ctx)
} }
static enum mie_status type_print( static enum mie_status type_print(
const struct mie_type_definition *type_info,
const struct mie_type *type, b_stream *out) const struct mie_type *type, struct mie_printer *out)
{ {
mie_storage_type_print((const struct mie_storage_type *)type, out); const struct mie_storage_type *storage
= (const struct mie_storage_type *)type;
b_stream_write_char(out->p_stream, '(');
for (size_t i = 0; i < MIE_VECTOR_COUNT(storage->st_parts); i++) {
if (i > 0) {
b_stream_write_string(out->p_stream, ", ", NULL);
}
mie_printer_print_type(out, storage->st_parts.items[i]);
}
b_stream_write_char(out->p_stream, ')');
return MIE_SUCCESS; return MIE_SUCCESS;
} }
@@ -46,21 +59,6 @@ void mie_storage_type_add_part(
mie_vector_push_back(ty->st_parts, &part); mie_vector_push_back(ty->st_parts, &part);
} }
void mie_storage_type_print(const struct mie_storage_type *type, b_stream *out)
{
b_stream_write_char(out, '(');
for (size_t i = 0; i < MIE_VECTOR_COUNT(type->st_parts); i++) {
if (i > 0) {
b_stream_write_string(out, ", ", NULL);
}
mie_type_print(MIE_VECTOR_ITEM(type->st_parts, i), out);
}
b_stream_write_char(out, ')');
}
void mie_storage_type_build_id( void mie_storage_type_build_id(
struct mie_id_builder *ctx, const struct mie_type **parts, size_t nr_parts) struct mie_id_builder *ctx, const struct mie_type **parts, size_t nr_parts)
{ {

View File

@@ -20,19 +20,6 @@ struct mie_type *mie_type_create(struct mie_type_definition *type)
return out; return out;
} }
void mie_type_print(const struct mie_type *type, b_stream *out)
{
if (type->ty_name) {
b_stream_write_string(out, type->ty_name, NULL);
} else if (type->ty_def && type->ty_def->ty_print) {
type->ty_def->ty_print(type->ty_def, type, out);
} else if (type->ty_def->ty_name) {
b_stream_write_string(out, type->ty_def->ty_name, NULL);
} else {
b_stream_write_string(out, "<UNNAMED-TYPE>", NULL);
}
}
void mie_type_build_id(const struct mie_type *type, struct mie_id_builder *ctx) void mie_type_build_id(const struct mie_type *type, struct mie_id_builder *ctx)
{ {
if (type->ty_def->ty_build_id) { if (type->ty_def->ty_build_id) {