Files
mie/tool/cmd/internal/ctx-dump.c

315 lines
9.3 KiB
C

#include "../cmd.h"
#include <assert.h>
#include <blue/cmd.h>
#include <blue/term.h>
#include <mie/attribute/attribute-definition.h>
#include <mie/ctx.h>
#include <mie/diag/class.h>
#include <mie/diag/msg.h>
#include <mie/dialect/arith.h>
#include <mie/dialect/builtin.h>
#include <mie/dialect/cf.h>
#include <mie/dialect/dialect.h>
#include <mie/dialect/func.h>
#include <mie/dialect/index.h>
#include <mie/dialect/meta.h>
#include <mie/dialect/ptr.h>
#include <mie/dialect/scf.h>
#include <mie/dialect/select.h>
#include <mie/interface/interface-definition.h>
#include <mie/ir/block.h>
#include <mie/ir/op-definition.h>
#include <mie/ir/op.h>
#include <mie/ir/region.h>
#include <mie/ir/register.h>
#include <mie/pass/builtin.h>
#include <mie/pass/pass-manager.h>
#include <mie/print/printer.h>
#include <mie/trait/trait-definition.h>
#include <mie/trait/trait.h>
#include <mie/type/type-definition.h>
#include <mie/type/type.h>
static void mie_op_definition_print(const struct mie_op_definition *op)
{
char id_str[MIE_ID_STRING_MAX];
mie_id_to_string(&op->op_id, id_str, sizeof id_str);
b_printf(
" [bold,red]Op:[reset]%-20s [dark_grey]{%s}[reset]\n",
op->op_name, id_str);
}
static void mie_type_definition_print(const struct mie_type_definition *type)
{
char id_str[MIE_ID_STRING_MAX];
mie_id_to_string(&type->ty_id, id_str, sizeof id_str);
b_printf(
" [bold,blue]Ty:[reset]%-20s [dark_grey]{%s}[reset]\n",
type->ty_name, id_str);
}
static void mie_trait_definition_print(const struct mie_trait_definition *trait)
{
char id_str[MIE_ID_STRING_MAX];
mie_id_to_string(&trait->tr_id, id_str, sizeof id_str);
b_printf(
" [bold,yellow]Tr:[reset]%-20s [dark_grey]{%s}[reset]\n",
trait->tr_name, id_str);
}
static void mie_attribute_definition_print(
const struct mie_attribute_definition *attribute)
{
char id_str[MIE_ID_STRING_MAX];
mie_id_to_string(&attribute->a_id, id_str, sizeof id_str);
b_printf(
" [bold,magenta]At:[reset]%-20s [dark_grey]{%s}[reset]\n",
attribute->a_name, id_str);
}
static void mie_interface_definition_print(
const struct mie_interface_definition *interface)
{
char id_str[MIE_ID_STRING_MAX];
mie_id_to_string(&interface->if_id, id_str, sizeof id_str);
b_printf(
" [bold,cyan]If:[reset]%-20s [dark_grey]{%s}[reset]\n",
interface->if_name, id_str);
}
static void mie_pass_definition_print(const struct mie_pass_definition *interface)
{
char id_str[MIE_ID_STRING_MAX];
mie_id_to_string(&interface->p_id, id_str, sizeof id_str);
b_printf(
" [bold,cyan]Ps:[reset]%-20s [dark_grey]{%s}[reset]\n",
interface->p_name, id_str);
}
static void mie_diag_class_print(
const struct mie_dialect *dialect, const struct mie_diag_class *c)
{
switch (c->c_type) {
case MIE_DIAG_CLASS_HINT:
b_printf(" [bold,cyan]Hint:[reset]");
break;
case MIE_DIAG_CLASS_WARNING:
b_printf(" [bold,yellow]Warn:[reset]");
break;
case MIE_DIAG_CLASS_ERROR:
b_printf(" [bold,red]Err: [reset]");
break;
default:
return;
}
b_printf(
"%s.%-25s [green]%s[reset]\n", dialect->d_name,
c->c_id_str_short, c->c_title);
}
static void mie_diag_msg_print(
const struct mie_dialect *dialect, const struct mie_diag_msg *msg)
{
b_printf(
" [bold,blue]Msg: [reset]%s.%-25s [green]%s[reset]\n",
dialect->d_name, msg->msg_id_str_short, msg->msg_content);
}
static void mie_dialect_print(const struct mie_dialect *dialect)
{
char id_str[MIE_ID_STRING_MAX];
mie_id_to_string(&dialect->d_id, id_str, sizeof id_str);
b_printf(
"[bold,green]D:[reset]%-20s [dark_grey]{%s}[reset]\n",
dialect->d_name, id_str);
b_btree_node *node = b_btree_first(&dialect->d_ops.map_entries);
while (node) {
mie_id *id = b_unbox(mie_id, node, e_node);
struct mie_op_definition *op
= b_unbox(struct mie_op_definition, id, op_id);
mie_op_definition_print(op);
node = b_btree_next(node);
}
node = b_btree_first(&dialect->d_types.map_entries);
while (node) {
mie_id *id = b_unbox(mie_id, node, e_node);
struct mie_type_definition *type
= b_unbox(struct mie_type_definition, id, ty_id);
mie_type_definition_print(type);
node = b_btree_next(node);
}
node = b_btree_first(&dialect->d_traits.map_entries);
while (node) {
mie_id *id = b_unbox(mie_id, node, e_node);
struct mie_trait_definition *trait
= b_unbox(struct mie_trait_definition, id, tr_id);
mie_trait_definition_print(trait);
node = b_btree_next(node);
}
node = b_btree_first(&dialect->d_attributes.map_entries);
while (node) {
mie_id *id = b_unbox(mie_id, node, e_node);
struct mie_attribute_definition *attribute
= b_unbox(struct mie_attribute_definition, id, a_id);
mie_attribute_definition_print(attribute);
node = b_btree_next(node);
}
node = b_btree_first(&dialect->d_interfaces.map_entries);
while (node) {
mie_id *id = b_unbox(mie_id, node, e_node);
struct mie_interface_definition *interface = b_unbox(
struct mie_interface_definition, id, if_id);
mie_interface_definition_print(interface);
node = b_btree_next(node);
}
}
static void mie_ctx_print(const struct mie_ctx *ctx)
{
printf("Dialects:\n");
b_btree_node *node = b_btree_first(&ctx->ctx_dialects.map_entries);
while (node) {
mie_id *id = b_unbox(mie_id, node, e_node);
struct mie_dialect *dialect
= b_unbox(struct mie_dialect, id, d_id);
mie_dialect_print(dialect);
node = b_btree_next(node);
}
printf("\nPasses:\n");
node = b_btree_first(&ctx->ctx_passes.map_entries);
while (node) {
mie_id *id = b_unbox(mie_id, node, e_node);
struct mie_pass_definition *pass
= b_unbox(struct mie_pass_definition, id, p_id);
mie_pass_definition_print(pass);
node = b_btree_next(node);
}
printf("\nDiagnostics:\n");
node = b_btree_first(&ctx->ctx_dialects.map_entries);
while (node) {
mie_id *id = b_unbox(mie_id, node, e_node);
struct mie_dialect *dialect
= b_unbox(struct mie_dialect, id, d_id);
for (size_t i = 0; i < dialect->d_nr_diag_classes; i++) {
mie_diag_class_print(dialect, &dialect->d_diag_classes[i]);
}
for (size_t i = 0; i < dialect->d_nr_diag_msgs; i++) {
mie_diag_msg_print(dialect, &dialect->d_diag_msgs[i]);
}
node = b_btree_next(node);
}
}
static int ctx_dump(const b_command *cmd, const b_arglist *args, const b_array *_)
{
struct mie_ctx *ctx = mie_ctx_create();
mie_builtin_dialect_create(ctx);
mie_meta_dialect_create(ctx);
mie_select_dialect_create(ctx);
mie_ptr_dialect_create(ctx);
mie_arith_dialect_create(ctx);
mie_func_dialect_create(ctx);
mie_cf_dialect_create(ctx);
mie_scf_dialect_create(ctx);
mie_index_dialect_create(ctx);
mie_builtin_passes_register(ctx);
mie_ctx_print(ctx);
struct mie_type *i32 = mie_ctx_get_int_type(ctx, 32);
struct mie_type *str = mie_ctx_get_type(ctx, "builtin", "string");
struct mie_type *index = mie_ctx_get_type(ctx, "builtin", "index");
struct mie_attribute *i32_value = mie_ctx_get_int(ctx, 1024, 32);
struct mie_attribute *index_value = mie_ctx_get_index(ctx, 25000);
struct mie_attribute *str_value
= mie_ctx_get_string(ctx, "Hello, world!");
const struct mie_type *storage_type_parts[] = {i32, str, index};
struct mie_type *storage_type = mie_ctx_get_storage_type(
ctx, storage_type_parts,
sizeof storage_type_parts / sizeof *storage_type_parts);
const struct mie_type *func_in_parts[] = {i32, str};
const struct mie_type *func_out_parts[] = {index};
struct mie_type *func_type = mie_ctx_get_function_type(
ctx, func_in_parts, sizeof func_in_parts / sizeof *func_in_parts,
func_out_parts, sizeof func_out_parts / sizeof *func_out_parts);
/* make sure storage/function type caching is working */
assert(storage_type
== mie_ctx_get_storage_type(
ctx, storage_type_parts,
sizeof storage_type_parts / sizeof *storage_type_parts));
assert(func_type
== mie_ctx_get_function_type(
ctx, func_in_parts,
sizeof func_in_parts / sizeof *func_in_parts, func_out_parts,
sizeof func_out_parts / sizeof *func_out_parts));
struct mie_printer printer;
mie_printer_init(&printer, ctx, b_stdout, MIE_PRINT_F_ABBREVIATED);
char id_str[MIE_ID_STRING_MAX];
mie_id_to_string(&i32->ty_id, id_str, sizeof id_str);
printf("i32 type: {%s} %s (instance of %s.%s)\n", id_str, i32->ty_name,
i32->ty_def->ty_parent->d_name, i32->ty_def->ty_name);
mie_id_to_string(&str->ty_id, id_str, sizeof id_str);
printf("str type: {%s} %s (instance of %s.%s)\n", id_str, str->ty_name,
str->ty_def->ty_parent->d_name, str->ty_def->ty_name);
mie_id_to_string(&index->ty_id, id_str, sizeof id_str);
printf("index type: {%s} %s (instance of %s.%s)\n", id_str, index->ty_name,
index->ty_def->ty_parent->d_name, index->ty_def->ty_name);
mie_id_to_string(&storage_type->ty_id, id_str, sizeof id_str);
printf("storage type: {%s} ", id_str);
mie_printer_print_type(&printer, storage_type);
printf("\n");
mie_id_to_string(&func_type->ty_id, id_str, sizeof id_str);
printf("function type: {%s} ", id_str);
mie_printer_print_type(&printer, func_type);
printf("\n");
printf("i32 value: ");
mie_printer_print_attribute(&printer, i32_value);
printf("\n");
printf("index value: ");
mie_printer_print_attribute(&printer, index_value);
printf("\n");
printf("str value: ");
mie_printer_print_attribute(&printer, str_value);
printf("\n");
return 0;
}
B_COMMAND(CMD_INTERNAL_CTX_DUMP, CMD_INTERNAL)
{
B_COMMAND_NAME("ctx-dump");
B_COMMAND_DESC("mie_ctx dump");
B_COMMAND_FUNCTION(ctx_dump);
B_COMMAND_HELP_OPTION();
}