Compare commits

...

6 Commits

Author SHA1 Message Date
d19e8626da mie: stop using movable memory for mie_name instances.
any struct that contains a mie_name cannot be stored in movable memory
(i.e. any memory that may be re-allocated using realloc(), or whose
contents may be moved to a different buffer).

mie_names form part of a bst when they are added to a mie_name_map,
and moving them after this happens will result in the bst pointers
being invalidated. this causes some obscure and hard-to-debug
memory errors.

all structs that contain a mie_name (including named IR objects like
mie_register and mie_block) are no longer stored directly in vectors.
rather, vectors of pointers are used instead.
2026-01-12 10:40:57 +00:00
0fdadb3250 mie: trait: fix trait definitions not being initialised correctly 2026-01-12 10:40:14 +00:00
2d903f4a94 mie: type: fix type definitions not being initialised correctly 2026-01-12 10:37:49 +00:00
3aad8fd4a3 mie: index: cache: fix cache entries not being initialised correctly 2026-01-12 10:36:32 +00:00
49df8616a8 mie: implement a printer system for converting IR to textual form 2026-01-12 10:33:08 +00:00
826380ea34 mie: ctx: fix new mie_trait instances not being fully initialised 2026-01-12 10:05:12 +00:00
37 changed files with 613 additions and 133 deletions

View File

@@ -231,7 +231,7 @@ const struct mie_trait *mie_ctx_get_trait(
return NULL;
}
memset(trait, 0x0, sizeof *trait);
memset(trait, 0x0, sizeof trait_info->tr_data_size);
trait->tr_def = trait_info;
trait->tr_name = b_bstr_fmt("%s.%s", dialect_name, trait_name);

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

@@ -26,6 +26,8 @@ static struct index_cache_entry *index_cache_entry_create(
return NULL;
}
memset(out, 0x0, sizeof *out);
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) {

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;
}

View File

@@ -12,7 +12,7 @@ struct mie_register;
struct mie_block {
struct mie_name b_name;
MIE_VECTOR_DECLARE(struct mie_register, b_params);
MIE_VECTOR_DECLARE(struct mie_register *, b_params);
MIE_VECTOR_DECLARE(struct mie_op, b_ops);
};

View File

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

View File

@@ -67,7 +67,7 @@ struct mie_op {
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_VECTOR_DECLARE(struct mie_register *, op_result);
};
MIE_API void mie_op_destroy(struct mie_op *op);

View File

@@ -9,7 +9,7 @@ struct mie_block;
struct mie_region {
struct mie_name_map *r_names;
MIE_VECTOR_DECLARE(struct mie_block, r_blocks);
MIE_VECTOR_DECLARE(struct mie_block *, r_blocks);
};
MIE_API struct mie_block *mie_region_add_block(struct mie_region *region);

View File

@@ -35,6 +35,13 @@ struct mie_name_map_entry {
};
};
/* stores a unique name, and forms a binary tree of similar entries.
* this struct is designed to be embedded directly within a larger struct
* (rather than just as a pointer), allowing the larger struct to be part
* of a name map. Because of this, mie_name CANNOT be used in any memory that
* may be re-allocated. If a mie_name is moved after it is added to a
* mie_name_map, the internal bst pointers that make up the map will be
* invalidated, causing some very obscure memory-related errors. */
struct mie_name {
struct mie_name_map_entry n_base;
struct mie_name_map *n_parent;

View File

@@ -1,5 +1,5 @@
#ifndef MIE_PARSE_PARSE_H_
#define MIE_PARSE_PARSE_H_
#ifndef MIE_PARSE_PARSER_H_
#define MIE_PARSE_PARSER_H_
#include <blue/ds/string.h>
#include <mie/ir/register.h>
@@ -94,7 +94,7 @@ MIE_API bool mie_parser_parse_register(
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_VECTOR_REF_PARAM(struct mie_register *, out));
MIE_API bool mie_parser_parse_region(
struct mie_parser *ctx, struct mie_region *region);

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_NOT_SUPPORTED,
MIE_ERR_INVALID_VALUE,
MIE_ERR_INVALID_ARGUMENT,
MIE_ERR_INTERNAL_FAILURE,
MIE_ERR_NO_MEMORY,
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(
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,

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(
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);

View File

@@ -6,6 +6,7 @@
struct mie_value;
struct mie_type;
struct mie_parser;
struct mie_printer;
/* a static struct that defines the properties, traits, and callbacks for a type. */
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_cleanup)(const struct mie_type_definition *, struct mie_type *);
void (*ty_instance_cleanup)(const struct mie_type *, struct mie_value *);
enum mie_status (*ty_print)(
const struct mie_type_definition *, const struct mie_type *,
b_stream *);
enum mie_status (*ty_print)(const struct mie_type *, struct mie_printer *);
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)(
const struct mie_type_definition *, struct mie_parser *,
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 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(

View File

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

28
mie/ir/module.c Normal file
View File

@@ -0,0 +1,28 @@
#include <mie/ir/module.h>
#include <mie/ir/op.h>
#include <stdlib.h>
#include <string.h>
struct mie_module *mie_module_create(void)
{
struct mie_module *out = malloc(sizeof *out);
if (!out) {
return NULL;
}
memset(out, 0x0, sizeof *out);
out->m_names = mie_name_map_create(NULL);
return out;
}
void mie_module_destroy(struct mie_module *mod)
{
/* TODO */
}
struct mie_op *mie_module_add_op(struct mie_module *mod)
{
return mie_vector_emplace_back(mod->m_ops);
}

18
mie/ir/region.c Normal file
View File

@@ -0,0 +1,18 @@
#include <mie/ir/block.h>
#include <mie/ir/region.h>
#include <stdlib.h>
#include <string.h>
struct mie_block *mie_region_add_block(struct mie_region *region)
{
struct mie_block *block = malloc(sizeof *block);
if (!block) {
return NULL;
}
memset(block, 0x0, sizeof *block);
mie_vector_push_back(region->r_blocks, &block);
return block;
}

View File

@@ -7,7 +7,7 @@
#include <mie/ir/region.h>
#include <mie/ir/register.h>
#include <mie/parse/lex.h>
#include <mie/parse/parse.h>
#include <mie/parse/parser.h>
#include <mie/parse/token.h>
#include <mie/type/function.h>
#include <mie/type/type-definition.h>
@@ -200,7 +200,11 @@ bool mie_parser_parse_linefeed(struct mie_parser *ctx)
return false;
}
while (tok && tok->tok_type == MIE_TOK_LINEFEED) {
mie_lex_advance(ctx->p_lex);
tok = mie_lex_peek(ctx->p_lex);
}
return true;
}
@@ -617,7 +621,7 @@ bool mie_parser_parse_register(
bool mie_parser_parse_register_list(
struct mie_parser *ctx, struct mie_name_map *names,
MIE_VECTOR_REF_PARAM(struct mie_register, out))
MIE_VECTOR_REF_PARAM(struct mie_register *, out))
{
bool ok = false;
struct mie_register *reg = NULL;
@@ -628,11 +632,13 @@ bool mie_parser_parse_register_list(
return false;
}
reg = mie_vector_ref_emplace_back(out);
reg = calloc(1, sizeof *reg);
if (!reg) {
return false;
}
mie_vector_ref_push_back(out, &reg);
if (!mie_parser_parse_register(ctx, names, reg)) {
return false;
}
@@ -652,10 +658,11 @@ bool mie_parser_parse_register_list(
return false;
}
reg = mie_vector_ref_emplace_back(out);
reg = calloc(1, sizeof *reg);
if (!reg) {
return false;
}
mie_vector_ref_push_back(out, &reg);
if (!mie_parser_parse_register(ctx, names, reg)) {
return false;
@@ -670,7 +677,16 @@ bool mie_parser_parse_module(struct mie_parser *ctx, struct mie_module *mod)
#define OP_TOKEN_TYPES \
(MIE_TOK_NAME | MIE_TOK_OPNAME | MIE_TOK_VREGNAME | MIE_TOK_MREGNAME \
| MIE_TOK_GRAPHNAME | MIE_TOK_INSTNAME)
while (1) {
for (size_t i = 0;; i++) {
if (mie_parser_check_eof(ctx)) {
break;
}
if (i > 0 && !mie_parser_parse_linefeed(ctx)) {
return false;
}
enum mie_token_type type = mie_parser_peek_type(ctx);
bool is_op = (type & OP_TOKEN_TYPES);
@@ -678,7 +694,7 @@ bool mie_parser_parse_module(struct mie_parser *ctx, struct mie_module *mod)
break;
}
struct mie_op *op = mie_vector_emplace_back(mod->m_ops);
struct mie_op *op = mie_module_add_op(mod);
if (!op) {
return false;
}
@@ -699,11 +715,14 @@ bool mie_parser_parse_region(struct mie_parser *ctx, struct mie_region *region)
return false;
}
mie_parser_parse_linefeed(ctx);
if (!mie_parser_parse_linefeed(ctx)) {
return false;
}
struct mie_block *block = NULL;
if (mie_parser_peek_type(ctx) != MIE_TOK_BLOCKNAME) {
block = mie_vector_emplace_back(region->r_blocks);
block = mie_region_add_block(region);
if (!mie_parser_parse_anonymous_block(ctx, region->r_names, block)) {
return false;
@@ -715,7 +734,7 @@ bool mie_parser_parse_region(struct mie_parser *ctx, struct mie_region *region)
break;
}
block = mie_vector_emplace_back(region->r_blocks);
block = mie_region_add_block(region);
if (!mie_parser_parse_block(ctx, region->r_names, block)) {
return false;
@@ -804,16 +823,15 @@ static bool parse_block_parameters(
return false;
}
if (!mie_parser_parse_symbol(ctx, MIE_SYM_RIGHT_PAREN)
|| !mie_parser_parse_symbol(ctx, MIE_SYM_COLON)
|| !mie_parser_parse_linefeed(ctx)) {
if (!mie_parser_parse_symbol(ctx, MIE_SYM_RIGHT_PAREN)) {
mie_vector_destroy(block_params, NULL);
return false;
}
for (size_t i = 0; i < MIE_VECTOR_COUNT(block_params); i++) {
struct mie_register *param_reg
= mie_vector_emplace_back(block->b_params);
struct mie_register *param_reg = calloc(1, sizeof *param_reg);
mie_vector_push_back(block->b_params, &param_reg);
param_reg->reg_flags = block_params.items[i].arg_unresolved.reg_flags
| MIE_REGISTER_F_BLOCK_PARAM;
param_reg->reg_type = block_params.items[i].arg_unresolved.reg_type;
@@ -1134,7 +1152,7 @@ static bool parse_generic_op(
}
for (size_t i = 0; i < MIE_VECTOR_COUNT(func_type->func_out); i++) {
dest->op_result.items[i].reg_type = func_type->func_out.items[i];
dest->op_result.items[i]->reg_type = func_type->func_out.items[i];
}
return true;

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/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) {
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

@@ -10,6 +10,8 @@ struct mie_trait_definition *mie_trait_definition_create(
return NULL;
}
memset(out, 0x0, sizeof *out);
out->tr_name = b_strdup(name);
if (!out->tr_name) {
free(out);

View File

@@ -1,3 +1,4 @@
#include <mie/print/printer.h>
#include <mie/type/function.h>
#include <mie/type/type-definition.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(
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_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;
}
@@ -53,36 +83,9 @@ void mie_function_type_add_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(

View File

@@ -1,3 +1,4 @@
#include <mie/print/printer.h>
#include <mie/type/storage.h>
#include <mie/type/type-definition.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(
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;
}
@@ -46,21 +59,6 @@ void mie_storage_type_add_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(
struct mie_id_builder *ctx, const struct mie_type **parts, size_t nr_parts)
{

View File

@@ -10,6 +10,8 @@ struct mie_type_definition *mie_type_definition_create(
return NULL;
}
memset(out, 0x0, sizeof *out);
out->ty_name = b_strdup(name);
if (!out->ty_name) {
free(out);

View File

@@ -20,19 +20,6 @@ struct mie_type *mie_type_create(struct mie_type_definition *type)
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)
{
if (type->ty_def->ty_build_id) {

View File

@@ -2,7 +2,7 @@
#include <stdlib.h>
#include <string.h>
#define DEFAULT_CAPACITY 4
#define DEFAULT_CAPACITY 16
struct vector {
void *v_buf;
@@ -49,7 +49,7 @@ static int vector_reserve(struct vector *v, size_t new_capacity)
static int vector_push_back(struct vector *v, const void *item)
{
int err = vector_reserve(v, v->v_count + 1);
int err = vector_reserve(v, v->v_count + DEFAULT_CAPACITY);
if (err != 0) {
return err;
}