tool: validate: resolve op definitions; run test pass
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
#include <blue/io/file.h>
|
#include <blue/io/file.h>
|
||||||
#include <blue/io/path.h>
|
#include <blue/io/path.h>
|
||||||
#include <blue/term.h>
|
#include <blue/term.h>
|
||||||
|
#include <mie/attribute/attribute-definition.h>
|
||||||
#include <mie/ctx.h>
|
#include <mie/ctx.h>
|
||||||
#include <mie/dialect/arith.h>
|
#include <mie/dialect/arith.h>
|
||||||
#include <mie/dialect/builtin.h>
|
#include <mie/dialect/builtin.h>
|
||||||
@@ -16,15 +17,20 @@
|
|||||||
#include <mie/dialect/ptr.h>
|
#include <mie/dialect/ptr.h>
|
||||||
#include <mie/dialect/scf.h>
|
#include <mie/dialect/scf.h>
|
||||||
#include <mie/dialect/select.h>
|
#include <mie/dialect/select.h>
|
||||||
|
#include <mie/interface/interface-definition.h>
|
||||||
#include <mie/ir/block.h>
|
#include <mie/ir/block.h>
|
||||||
#include <mie/ir/op-definition.h>
|
#include <mie/ir/op-definition.h>
|
||||||
#include <mie/ir/op.h>
|
#include <mie/ir/op.h>
|
||||||
#include <mie/ir/region.h>
|
#include <mie/ir/region.h>
|
||||||
#include <mie/ir/register.h>
|
#include <mie/ir/register.h>
|
||||||
|
#include <mie/ir/walk.h>
|
||||||
#include <mie/name.h>
|
#include <mie/name.h>
|
||||||
#include <mie/parse/lex.h>
|
#include <mie/parse/lex.h>
|
||||||
#include <mie/parse/parse.h>
|
#include <mie/parse/parser.h>
|
||||||
#include <mie/parse/token.h>
|
#include <mie/parse/token.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-definition.h>
|
||||||
#include <mie/trait/trait.h>
|
#include <mie/trait/trait.h>
|
||||||
#include <mie/type/type-definition.h>
|
#include <mie/type/type-definition.h>
|
||||||
@@ -45,16 +51,16 @@ static int trait_ref_print(const struct mie_trait *trait, void *arg)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mie_op_arg_print(const struct mie_op_arg *arg)
|
static void op_arg_dump(struct mie_printer *printer, const struct mie_op_arg *arg)
|
||||||
{
|
{
|
||||||
enum mie_register_flags arg_flags = 0;
|
enum mie_register_flags arg_flags = 0;
|
||||||
const char *arg_name = NULL;
|
const char *arg_name = NULL;
|
||||||
const struct mie_type *arg_type = NULL;
|
const struct mie_type *arg_type = NULL;
|
||||||
|
|
||||||
if (MIE_TEST_FLAGS(arg->arg_flags, MIE_OP_F_ARG_RESOLVED)) {
|
if (MIE_TEST_FLAGS(arg->arg_flags, MIE_OP_F_ARG_RESOLVED)) {
|
||||||
arg_flags = arg->arg_value->reg_flags;
|
arg_flags = arg->arg_value.u_reg->reg_flags;
|
||||||
arg_name = arg->arg_value->reg_name.n_str;
|
arg_name = arg->arg_value.u_reg->reg_name.n_str;
|
||||||
arg_type = arg->arg_value->reg_type;
|
arg_type = arg->arg_value.u_reg->reg_type;
|
||||||
} else {
|
} else {
|
||||||
arg_flags = arg->arg_unresolved.reg_flags;
|
arg_flags = arg->arg_unresolved.reg_flags;
|
||||||
arg_name = arg->arg_unresolved.reg_name;
|
arg_name = arg->arg_unresolved.reg_name;
|
||||||
@@ -68,13 +74,15 @@ static void mie_op_arg_print(const struct mie_op_arg *arg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf(":(");
|
printf(":(");
|
||||||
mie_type_print(arg_type, b_stdout);
|
mie_printer_print_type(printer, arg_type);
|
||||||
b_printf(")%s[reset]", arg_name);
|
b_printf(")%s[reset]", arg_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mie_op_print(const struct mie_op *op)
|
static void op_dump(const struct mie_op *op)
|
||||||
{
|
{
|
||||||
b_stringstream *tmp = b_stringstream_create();
|
b_stringstream *tmp = b_stringstream_create();
|
||||||
|
struct mie_printer printer;
|
||||||
|
mie_printer_init(&printer, NULL, b_stdout, MIE_PRINT_F_GENERIC);
|
||||||
|
|
||||||
printf("FLAGS:");
|
printf("FLAGS:");
|
||||||
(op->op_flags & MIE_OP_F_OP_RESOLVED) && printf(" OP_RESOLVED");
|
(op->op_flags & MIE_OP_F_OP_RESOLVED) && printf(" OP_RESOLVED");
|
||||||
@@ -88,11 +96,14 @@ static void mie_op_print(const struct mie_op *op)
|
|||||||
mie_trait_table_iterate(&op->op_info->op_traits, trait_ref_print, NULL);
|
mie_trait_table_iterate(&op->op_info->op_traits, trait_ref_print, NULL);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf("ATTRIBUTES:");
|
printf("ATTRIBUTES:");
|
||||||
|
#if 0
|
||||||
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_attrib); i++) {
|
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_attrib); i++) {
|
||||||
printf(" (%s = ", op->op_attrib.items[i].attrib_name);
|
printf(" (%s = ", op->op_attrib.items[i].attrib_name);
|
||||||
mie_value_print(op->op_attrib.items[i].attrib_value, b_stdout);
|
mie_printer_print_value(
|
||||||
|
&printer, op->op_attrib.items[i].attrib_value);
|
||||||
printf(")");
|
printf(")");
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
printf("REGIONS: %zu\n", MIE_VECTOR_COUNT(op->op_regions));
|
printf("REGIONS: %zu\n", MIE_VECTOR_COUNT(op->op_regions));
|
||||||
@@ -109,7 +120,7 @@ static void mie_op_print(const struct mie_op *op)
|
|||||||
|
|
||||||
for (size_t i = 0; i < MIE_VECTOR_COUNT(s->s_args); i++) {
|
for (size_t i = 0; i < MIE_VECTOR_COUNT(s->s_args); i++) {
|
||||||
const struct mie_op_arg *arg = &s->s_args.items[i];
|
const struct mie_op_arg *arg = &s->s_args.items[i];
|
||||||
mie_op_arg_print(arg);
|
op_arg_dump(&printer, arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf(" )");
|
printf(" )");
|
||||||
@@ -119,7 +130,7 @@ static void mie_op_print(const struct mie_op *op)
|
|||||||
printf("ARGS:");
|
printf("ARGS:");
|
||||||
for (size_t i = 0; i < op->op_args.count; i++) {
|
for (size_t i = 0; i < op->op_args.count; i++) {
|
||||||
const struct mie_op_arg *arg = &op->op_args.items[i];
|
const struct mie_op_arg *arg = &op->op_args.items[i];
|
||||||
mie_op_arg_print(arg);
|
op_arg_dump(&printer, arg);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
@@ -128,7 +139,7 @@ static void mie_op_print(const struct mie_op *op)
|
|||||||
const struct mie_register *reg = &op->op_result.items[i];
|
const struct mie_register *reg = &op->op_result.items[i];
|
||||||
printf(" %s:(",
|
printf(" %s:(",
|
||||||
(reg->reg_flags & MIE_REGISTER_F_MACHINE) ? "MR" : "VR");
|
(reg->reg_flags & MIE_REGISTER_F_MACHINE) ? "MR" : "VR");
|
||||||
mie_type_print(reg->reg_type, b_stdout);
|
mie_printer_print_type(&printer, reg->reg_type);
|
||||||
printf(")%s", reg->reg_name.n_str);
|
printf(")%s", reg->reg_name.n_str);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
@@ -161,6 +172,35 @@ static void mie_trait_definition_print(const struct mie_trait_definition *trait)
|
|||||||
trait->tr_name, id_str);
|
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_dialect_print(const struct mie_dialect *dialect)
|
static void mie_dialect_print(const struct mie_dialect *dialect)
|
||||||
{
|
{
|
||||||
char id_str[MIE_ID_STRING_MAX];
|
char id_str[MIE_ID_STRING_MAX];
|
||||||
@@ -198,10 +238,31 @@ static void mie_dialect_print(const struct mie_dialect *dialect)
|
|||||||
mie_trait_definition_print(trait);
|
mie_trait_definition_print(trait);
|
||||||
node = b_btree_next(node);
|
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)
|
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);
|
b_btree_node *node = b_btree_first(&ctx->ctx_dialects.map_entries);
|
||||||
while (node) {
|
while (node) {
|
||||||
mie_id *id = b_unbox(mie_id, node, e_node);
|
mie_id *id = b_unbox(mie_id, node, e_node);
|
||||||
@@ -211,6 +272,17 @@ static void mie_ctx_print(const struct mie_ctx *ctx)
|
|||||||
mie_dialect_print(dialect);
|
mie_dialect_print(dialect);
|
||||||
node = b_btree_next(node);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int validate_file(const char *path, const b_arglist *args)
|
static int validate_file(const char *path, const b_arglist *args)
|
||||||
@@ -236,14 +308,19 @@ static int validate_file(const char *path, const b_arglist *args)
|
|||||||
mie_scf_dialect_create(ctx);
|
mie_scf_dialect_create(ctx);
|
||||||
mie_index_dialect_create(ctx);
|
mie_index_dialect_create(ctx);
|
||||||
|
|
||||||
|
mie_builtin_passes_register(ctx);
|
||||||
|
|
||||||
mie_ctx_print(ctx);
|
mie_ctx_print(ctx);
|
||||||
|
|
||||||
struct mie_type *i32 = mie_arith_int_get_type(ctx, 32);
|
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 *str = mie_ctx_get_type(ctx, "builtin", "string");
|
||||||
struct mie_type *index = mie_ctx_get_type(ctx, "index", "index");
|
struct mie_type *index = mie_ctx_get_type(ctx, "builtin", "index");
|
||||||
struct mie_value *i32_value = mie_ctx_get_int(ctx, 1024, 32);
|
struct mie_attribute *i32_value = mie_ctx_get_int(ctx, 1024, 32);
|
||||||
struct mie_value *index_value = mie_ctx_get_index(ctx, 25000);
|
struct mie_attribute *index_value = mie_ctx_get_index(ctx, 25000);
|
||||||
struct mie_value *str_value = mie_ctx_get_string(ctx, "Hello, world!");
|
struct mie_attribute *str_value
|
||||||
|
= mie_ctx_get_string(ctx, "Hello, world!");
|
||||||
|
|
||||||
|
printf("%zu\n", sizeof(struct mie_op));
|
||||||
|
|
||||||
const struct mie_type *storage_type_parts[] = {i32, str, index};
|
const struct mie_type *storage_type_parts[] = {i32, str, index};
|
||||||
struct mie_type *storage_type = mie_ctx_get_storage_type(
|
struct mie_type *storage_type = mie_ctx_get_storage_type(
|
||||||
@@ -267,6 +344,9 @@ static int validate_file(const char *path, const b_arglist *args)
|
|||||||
sizeof func_in_parts / sizeof *func_in_parts, func_out_parts,
|
sizeof func_in_parts / sizeof *func_in_parts, func_out_parts,
|
||||||
sizeof func_out_parts / sizeof *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];
|
char id_str[MIE_ID_STRING_MAX];
|
||||||
mie_id_to_string(&i32->ty_id, id_str, sizeof id_str);
|
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,
|
printf("i32 type: {%s} %s (instance of %s.%s)\n", id_str, i32->ty_name,
|
||||||
@@ -279,24 +359,24 @@ static int validate_file(const char *path, const b_arglist *args)
|
|||||||
index->ty_def->ty_parent->d_name, index->ty_def->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);
|
mie_id_to_string(&storage_type->ty_id, id_str, sizeof id_str);
|
||||||
printf("storage type: {%s} ", id_str);
|
printf("storage type: {%s} ", id_str);
|
||||||
mie_type_print(storage_type, b_stdout);
|
mie_printer_print_type(&printer, storage_type);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
mie_id_to_string(&func_type->ty_id, id_str, sizeof id_str);
|
mie_id_to_string(&func_type->ty_id, id_str, sizeof id_str);
|
||||||
printf("function type: {%s} ", id_str);
|
printf("function type: {%s} ", id_str);
|
||||||
mie_type_print(func_type, b_stdout);
|
mie_printer_print_type(&printer, func_type);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
printf("i32 value: ");
|
printf("i32 value: ");
|
||||||
mie_value_print(i32_value, b_stdout);
|
mie_printer_print_attribute(&printer, i32_value);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
printf("index value: ");
|
printf("index value: ");
|
||||||
mie_value_print(index_value, b_stdout);
|
mie_printer_print_attribute(&printer, index_value);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
printf("str value: ");
|
printf("str value: ");
|
||||||
mie_value_print(str_value, b_stdout);
|
mie_printer_print_attribute(&printer, str_value);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
struct mie_lex *lex = mie_lex_create(file);
|
struct mie_lex *lex = mie_lex_create(file);
|
||||||
@@ -304,19 +384,52 @@ static int validate_file(const char *path, const b_arglist *args)
|
|||||||
|
|
||||||
struct mie_name_map *names = mie_name_map_create(NULL);
|
struct mie_name_map *names = mie_name_map_create(NULL);
|
||||||
|
|
||||||
struct mie_op op = {};
|
struct mie_op *root = mie_op_create();
|
||||||
|
|
||||||
if (!mie_parser_parse_op(parse, names, &op)) {
|
if (!mie_parser_parse_op(parse, NULL, root)) {
|
||||||
printf("parse failed\n");
|
printf("parse failed\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mie_ctx_resolve_op(ctx, &op)) {
|
struct mie_walker *walker = mie_walker_begin(
|
||||||
printf("op resolve failed\n");
|
root, MIE_WALKER_F_INCLUDE_OPS | MIE_WALKER_F_PREORDER
|
||||||
|
| MIE_WALKER_F_FORWARD | MIE_WALKER_F_RECURSIVE);
|
||||||
|
|
||||||
|
do {
|
||||||
|
const struct mie_walker_item *item = mie_walker_get(walker);
|
||||||
|
for (size_t i = 0; i < item->i_depth; i++) {
|
||||||
|
fputs(" ", stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("resolving %p %s... ", item->i_op, item->i_op->op_name);
|
||||||
|
bool ok = mie_ctx_resolve_op(ctx, item->i_op);
|
||||||
|
printf("%s\n", ok ? "OK" : "FAIL");
|
||||||
|
} while (mie_walker_step(walker) == MIE_SUCCESS);
|
||||||
|
|
||||||
|
struct mie_pass *prefix_func_with_underscore = NULL;
|
||||||
|
if (mie_ctx_get_pass(
|
||||||
|
ctx, "prefix-func-with-underscore", NULL,
|
||||||
|
&prefix_func_with_underscore)
|
||||||
|
!= MIE_SUCCESS) {
|
||||||
|
printf("cannot load pass prefix-func-with-underscore\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
mie_op_print(&op);
|
struct mie_pass_manager *pm = mie_pass_manager_create(ctx);
|
||||||
|
struct mie_pass_manager *module_pm = mie_pass_manager_nest(pm);
|
||||||
|
mie_pass_manager_filter_op(module_pm, "builtin", "module");
|
||||||
|
|
||||||
|
struct mie_pass_manager *func_pm = mie_pass_manager_nest(pm);
|
||||||
|
mie_pass_manager_filter_op(module_pm, "func", "func");
|
||||||
|
|
||||||
|
mie_pass_manager_add_pass(func_pm, prefix_func_with_underscore);
|
||||||
|
|
||||||
|
mie_pass_manager_parse(
|
||||||
|
pm, "builtin.module(func.func(prefix-func-with-underscore))");
|
||||||
|
mie_pass_manager_run(pm, root);
|
||||||
|
|
||||||
|
mie_printer_print_op(&printer, root);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user