261 lines
6.4 KiB
C
261 lines
6.4 KiB
C
#include "../diag/diag.h"
|
|
#include "../resolve.h"
|
|
#include "cmd.h"
|
|
|
|
#include <assert.h>
|
|
#include <fx/cmd.h>
|
|
#include <fx/io/file.h>
|
|
#include <fx/io/path.h>
|
|
#include <fx/term.h>
|
|
#include <mie/attribute/attribute-definition.h>
|
|
#include <mie/ctx.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/memref.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/ir/walk.h>
|
|
#include <mie/name.h>
|
|
#include <mie/parse/lex.h>
|
|
#include <mie/parse/line-source.h>
|
|
#include <mie/parse/parser.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.h>
|
|
#include <mie/type/type-definition.h>
|
|
#include <mie/type/type.h>
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
enum {
|
|
ARG_FILEPATH,
|
|
};
|
|
|
|
static void report_diag(struct mie_ctx *ctx)
|
|
{
|
|
struct mie_diag *diag = mie_ctx_pop_diag(ctx);
|
|
while (diag) {
|
|
mie_diag_write_pretty(diag, fx_stdtty_err);
|
|
/* TODO cleanup */
|
|
diag = mie_ctx_pop_diag(ctx);
|
|
}
|
|
}
|
|
|
|
static int validate_file(const char *path, const fx_arglist *args)
|
|
{
|
|
fx_file *file = NULL;
|
|
fx_result result
|
|
= fx_file_open(NULL, FX_RV_PATH(path), FX_FILE_READ_ONLY, &file);
|
|
if (fx_result_is_error(result)) {
|
|
fx_throw(result);
|
|
return -1;
|
|
}
|
|
|
|
printf("File OK\n");
|
|
|
|
struct mie_ctx *ctx = mie_ctx_create();
|
|
mie_builtin_dialect_create(ctx);
|
|
mie_meta_dialect_create(ctx);
|
|
mie_select_dialect_create(ctx);
|
|
mie_memref_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);
|
|
|
|
struct mie_line_source src;
|
|
mie_line_source_init(&src, path, file);
|
|
|
|
struct mie_lex *lex = mie_lex_create(&src, ctx);
|
|
struct mie_parser *parse = mie_parser_create(ctx, lex);
|
|
|
|
struct mie_name_map *names = mie_name_map_create(NULL);
|
|
|
|
struct mie_op *root = mie_op_create();
|
|
|
|
if (!mie_parser_parse_op(parse, NULL, root)) {
|
|
report_diag(ctx);
|
|
printf("parse failed\n");
|
|
return -1;
|
|
}
|
|
|
|
if (!resolve_op(root, ctx)) {
|
|
report_diag(ctx);
|
|
return -1;
|
|
}
|
|
|
|
struct mie_pass *prefix_func_with_underscore = NULL;
|
|
enum mie_status status = mie_ctx_get_pass(
|
|
ctx, "prefix-func-with-underscore", NULL,
|
|
&prefix_func_with_underscore);
|
|
if (status != MIE_SUCCESS) {
|
|
printf("cannot load pass prefix-func-with-underscore\n");
|
|
return -1;
|
|
}
|
|
|
|
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);
|
|
|
|
#if 0
|
|
mie_pass_manager_parse(
|
|
pm, "builtin.module(func.func(prefix-func-with-underscore))");
|
|
#endif
|
|
mie_pass_manager_run(pm, root);
|
|
|
|
struct mie_printer printer;
|
|
mie_printer_init(
|
|
&printer, ctx, fx_stdout,
|
|
MIE_PRINT_F_ABBREVIATED | MIE_PRINT_F_MARK_UNRESOLVED_ELEMENTS);
|
|
mie_printer_print_op(&printer, root);
|
|
printf("\n");
|
|
|
|
#if 0
|
|
struct mie_walker walker;
|
|
enum mie_walker_flags walk_flags = MIE_WALKER_F_INCLUDE_BLOCKS
|
|
| MIE_WALKER_F_PREORDER
|
|
| MIE_WALKER_F_FORWARD;
|
|
mie_walker_begin(&walker, root, walk_flags);
|
|
|
|
do {
|
|
const struct mie_walk_item *item = mie_walker_get(&walker);
|
|
printf("looking at block %p %s...\n", item->i_block,
|
|
item->i_block->b_name.n_str);
|
|
printf(" id=%u\n", item->i_block->b_id);
|
|
const char *idom = item->i_block->b_idom
|
|
? item->i_block->b_idom->b_name.n_str
|
|
: "Unk";
|
|
printf(" idom=%s\n", idom);
|
|
#if 0
|
|
const char *sdom = item->i_block->b_sdom
|
|
? item->i_block->b_sdom->b_name.n_str
|
|
: "Unk";
|
|
const char *dfs_parent
|
|
= item->i_block->b_dfs_parent
|
|
? item->i_block->b_dfs_parent->b_name.n_str
|
|
: "Unk";
|
|
printf(" sdom=%s\n", sdom);
|
|
printf(" dfs_parent=%s\n", dfs_parent);
|
|
#endif
|
|
|
|
printf(" preds=");
|
|
|
|
struct mie_block_predecessor *pred
|
|
= mie_block_get_first_predecessor(item->i_block);
|
|
while (pred) {
|
|
printf(" %s", pred->p_block->b_name.n_str);
|
|
pred = mie_block_get_next_predecessor(item->i_block, pred);
|
|
}
|
|
printf("\n");
|
|
} while (mie_walker_step(&walker) == MIE_SUCCESS);
|
|
mie_walker_end(&walker);
|
|
#endif
|
|
|
|
#if 0
|
|
|
|
while (1) {
|
|
struct mie_token *tok = mie_lex_peek(lex);
|
|
if (!tok) {
|
|
break;
|
|
}
|
|
|
|
printf("%s[%d:%d -> %d:%d]",
|
|
mie_token_type_to_string(tok->tok_type),
|
|
tok->tok_start.c_row, tok->tok_start.c_col,
|
|
tok->tok_end.c_row, tok->tok_end.c_col);
|
|
|
|
switch (tok->tok_value_type) {
|
|
case MIE_TOK_V_STRING:
|
|
printf(" S:%s", tok->tok_str);
|
|
break;
|
|
case MIE_TOK_V_INT:
|
|
printf(" I:%lld", tok->tok_int);
|
|
break;
|
|
case MIE_TOK_V_DOUBLE:
|
|
printf(" D:%lf", tok->tok_double);
|
|
break;
|
|
case MIE_TOK_V_SYMBOL:
|
|
printf(" SYM:%s", mie_token_symbol_to_string(tok->tok_sym));
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
mie_lex_advance(lex);
|
|
}
|
|
#endif
|
|
|
|
mie_lex_destroy(lex);
|
|
|
|
mie_line_source_cleanup(&src);
|
|
|
|
fx_file_unref(file);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int validate(const fx_command *cmd, const fx_arglist *args, const fx_array *_)
|
|
{
|
|
fx_arglist_iterator it;
|
|
fx_arglist_foreach_filtered(&it, args, FX_COMMAND_INVALID_ID, ARG_FILEPATH)
|
|
{
|
|
fx_arglist_value *path = it.value;
|
|
if (path->val_type != FX_COMMAND_ARG_STRING) {
|
|
continue;
|
|
}
|
|
|
|
int r = validate_file(path->val_str, args);
|
|
if (r != 0) {
|
|
return r;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
FX_COMMAND(CMD_VALIDATE, CMD_ROOT)
|
|
{
|
|
FX_COMMAND_NAME("validate");
|
|
FX_COMMAND_SHORT_NAME('V');
|
|
FX_COMMAND_DESC("validate a mie ir file.");
|
|
FX_COMMAND_HELP_OPTION();
|
|
FX_COMMAND_FLAGS(FX_COMMAND_SHOW_HELP_BY_DEFAULT);
|
|
FX_COMMAND_FUNCTION(validate);
|
|
|
|
FX_COMMAND_ARG(ARG_FILEPATH)
|
|
{
|
|
FX_ARG_NAME("filepath");
|
|
FX_ARG_DESC("the mie file to validate");
|
|
FX_ARG_NR_VALUES(1);
|
|
}
|
|
|
|
FX_COMMAND_HELP_OPTION();
|
|
}
|