From fe1e7d81c4d03b3eb5e0d435bf89a300d939858f Mon Sep 17 00:00:00 2001 From: Max Wash Date: Mon, 16 Mar 2026 11:58:56 +0000 Subject: [PATCH] builtin: add diag definitions and parser callbacks --- mie/dialect/builtin/attribute/float.c | 2 +- mie/dialect/builtin/attribute/int.c | 2 +- mie/dialect/builtin/attribute/type.c | 2 +- mie/dialect/builtin/diag.c | 62 +++++++++++++++++++++++++++ mie/dialect/builtin/op/module.c | 16 ++++++- mie/include/mie/dialect/builtin.h | 55 ++++++++++++++++++++++++ 6 files changed, 135 insertions(+), 4 deletions(-) diff --git a/mie/dialect/builtin/attribute/float.c b/mie/dialect/builtin/attribute/float.c index 7eec5ff..fc2564e 100644 --- a/mie/dialect/builtin/attribute/float.c +++ b/mie/dialect/builtin/attribute/float.c @@ -59,7 +59,7 @@ static enum mie_status parse( } const struct mie_type *type = NULL; - if (!mie_parser_parse_type(ctx, &type)) { + if (!mie_parser_parse_type(ctx, NULL, &type, NULL)) { return MIE_ERR_BAD_SYNTAX; } diff --git a/mie/dialect/builtin/attribute/int.c b/mie/dialect/builtin/attribute/int.c index ad20887..8902f2b 100644 --- a/mie/dialect/builtin/attribute/int.c +++ b/mie/dialect/builtin/attribute/int.c @@ -64,7 +64,7 @@ static enum mie_status parse( if (!mie_parser_parse_symbol(ctx, MIE_SYM_COLON)) { width = 64; - } else if (!mie_parser_parse_type(ctx, &type)) { + } else if (!mie_parser_parse_type(ctx, NULL, &type, NULL)) { return false; } else { width = mie_int_type_get_width(type); diff --git a/mie/dialect/builtin/attribute/type.c b/mie/dialect/builtin/attribute/type.c index 2f927ca..b0040c2 100644 --- a/mie/dialect/builtin/attribute/type.c +++ b/mie/dialect/builtin/attribute/type.c @@ -53,7 +53,7 @@ static enum mie_status parse( const struct mie_type *type = NULL; - if (!mie_parser_parse_type(ctx, &type)) { + if (!mie_parser_parse_type(ctx, NULL, &type, NULL)) { free(ty); return MIE_ERR_BAD_FORMAT; } diff --git a/mie/dialect/builtin/diag.c b/mie/dialect/builtin/diag.c index ad3f101..70428f6 100644 --- a/mie/dialect/builtin/diag.c +++ b/mie/dialect/builtin/diag.c @@ -8,13 +8,36 @@ MIE_DIAG_CLASS_LIST_BEGIN(mie_builtin_diag) MIE_DIAG_CLASS(UNRECOGNISED_TOKEN, ERROR, "Unrecognised token") + MIE_DIAG_CLASS(UNEXPECTED_TOKEN, ERROR, "Unexpected token") + MIE_DIAG_CLASS(UNRESOLVED_TYPE, ERROR, "Unresolved type") MIE_DIAG_CLASS(UNRESOLVED_VALUE, ERROR, "Unresolved value") MIE_DIAG_CLASS(UNRESOLVED_SUCCESSOR, ERROR, "Unresolved successor") + MIE_DIAG_CLASS(UNKNOWN_OP, ERROR, "Unknown op") + MIE_DIAG_CLASS( + OP_REQUIRES_PARENT_SCOPE, ERROR, "Op requires parent scope") + MIE_DIAG_CLASS( + INCORRECT_NUMBER_OF_RESULTS, ERROR, + "Incorrect number of results") + MIE_DIAG_CLASS( + INCORRECT_NUMBER_OF_ARGUMENTS, ERROR, + "Incorrect number of arguments") + MIE_DIAG_CLASS( + INCORRECT_NUMBER_OF_TYPES, ERROR, "Incorrect number of types") + MIE_DIAG_CLASS(INCORRECT_TYPE, ERROR, "Incorrect type") + MIE_DIAG_CLASS(INVALID_TYPE, ERROR, "Invalid type") + MIE_DIAG_CLASS(NAME_ALREADY_IN_USE, ERROR, "Name already in use") + MIE_DIAG_CLASS(INTERNAL_ERROR, ERROR, "Internal error") MIE_DIAG_CLASS_LIST_END(mie_builtin_diag) MIE_DIAG_MSG_LIST_BEGIN(mie_builtin_msg) MIE_DIAG_MSG(UNRECOGNISED_TOKEN, "encountered an unrecognised token.") MIE_DIAG_MSG(UNRESOLVED_VALUE, "cannot resolve this value reference.") + MIE_DIAG_MSG( + UNRESOLVED_BUILTIN_TYPE, + "cannot resolve this builtin type reference.") + MIE_DIAG_MSG( + UNRESOLVED_DIALECT_TYPE, + "cannot resolve this dialect type reference.") MIE_DIAG_MSG( CANNOT_FIND_BLOCK, "cannot find a block with this name in this region.") @@ -33,4 +56,43 @@ MIE_DIAG_MSG_LIST_BEGIN(mie_builtin_msg) "outside of this region.") MIE_DIAG_MSG( VALUE_DEFINED_IN_BLOCK, "the value is defined in this block.") + MIE_DIAG_MSG(UNKNOWN_OP, "encountered an unknown operation.") + MIE_DIAG_MSG( + OP_REQUIRES_PARENT_SCOPE, + "this operation requires a parent scope.") + MIE_DIAG_MSG( + OP_PARENT_SCOPE_EXAMPLE, + "operations that require a parent scope cannot be used as a " + "top-level operation. try wrapping this operation in a " + "[blue]module[reset].") + MIE_DIAG_MSG( + USE_GENERIC_OP_SYNTAX, + "if this op belongs to an unknown dialect, you can use generic " + "op syntax to represent it.") + MIE_DIAG_MSG( + OP_HAS_NO_PARSER, + "this operation has no associated parser functionality.") + MIE_DIAG_MSG( + DIALECT_INTERNAL_ERROR, + "this error is caused by an internal problem with the relevant " + "dialect. please report the issue to the dialect vendor.") + MIE_DIAG_MSG( + COMPILER_INTERNAL_ERROR, + "this error is caused by an internal compiler issue. please " + "report the issue to the compiler vendor.") + MIE_DIAG_MSG( + NR_RESULT_NAME_OUTPUT_MISMATCH, + "the number of [red]output value names[reset] does not match " + "the number of results.") + MIE_DIAG_MSG( + OP_INCORRECT_NUMBER_OF_ARGS, + "this operation does not have the correct number of arguments.") + MIE_DIAG_MSG( + OP_NR_TYPES_DOESNT_MATCH_NR_ARGS, + "the number of types specified does not match the number of " + "arguments.") + MIE_DIAG_MSG( + OP_NR_TYPES_DOESNT_MATCH_NR_RESULTS, + "the number of types specified does not match the number of " + "results.") MIE_DIAG_MSG_LIST_END(mie_builtin_msg) diff --git a/mie/dialect/builtin/op/module.c b/mie/dialect/builtin/op/module.c index 38a1abc..6af7da0 100644 --- a/mie/dialect/builtin/op/module.c +++ b/mie/dialect/builtin/op/module.c @@ -4,6 +4,7 @@ #include #include #include +#include #include static enum mie_status print(struct mie_printer *out, const struct mie_op *op) @@ -18,8 +19,21 @@ static enum mie_status print(struct mie_printer *out, const struct mie_op *op) return MIE_SUCCESS; } -static enum mie_status parse(struct mie_parser *parser, struct mie_op *out) +static enum mie_status parse( + struct mie_parser *parser, struct mie_parser_scope *scope, + struct mie_op *out) { + struct mie_region *region = mie_op_add_region(out); + if (mie_parser_peek_symbol(parser) != MIE_SYM_LEFT_BRACE) { + mie_parser_report_unexpected_token_s( + parser, "region", "module body"); + return MIE_ERR_BAD_SYNTAX; + } + + if (!mie_parser_parse_region(parser, out, region, NULL)) { + return MIE_ERR_BAD_SYNTAX; + } + return MIE_SUCCESS; } diff --git a/mie/include/mie/dialect/builtin.h b/mie/include/mie/dialect/builtin.h index 5984142..74f3838 100644 --- a/mie/include/mie/dialect/builtin.h +++ b/mie/include/mie/dialect/builtin.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -17,18 +18,51 @@ struct mie_float_type; enum mie_builtin_diag { MIE_BUILTIN_E_UNRECOGNISED_TOKEN, + MIE_BUILTIN_E_UNEXPECTED_TOKEN, + MIE_BUILTIN_E_UNRESOLVED_TYPE, MIE_BUILTIN_E_UNRESOLVED_VALUE, MIE_BUILTIN_E_UNRESOLVED_SUCCESSOR, + MIE_BUILTIN_E_UNKNOWN_OP, + MIE_BUILTIN_E_OP_REQUIRES_PARENT_SCOPE, + MIE_BUILTIN_E_INCORRECT_NUMBER_OF_ARGUMENTS, + MIE_BUILTIN_E_INCORRECT_NUMBER_OF_RESULTS, + MIE_BUILTIN_E_INCORRECT_NUMBER_OF_TYPES, + MIE_BUILTIN_E_INCORRECT_TYPE, + MIE_BUILTIN_E_INVALID_TYPE, + MIE_BUILTIN_E_NAME_ALREADY_IN_USE, + MIE_BUILTIN_E_INTERNAL_ERROR, }; enum mie_builtin_msg { MIE_BUILTIN_MSG_UNRECOGNISED_TOKEN, + MIE_BUILTIN_MSG_UNEXPECTED_TOKEN, MIE_BUILTIN_MSG_UNRESOLVED_VALUE, + MIE_BUILTIN_MSG_UNRESOLVED_BUILTIN_TYPE, + MIE_BUILTIN_MSG_UNRESOLVED_DIALECT_TYPE, MIE_BUILTIN_MSG_CANNOT_FIND_BLOCK, MIE_BUILTIN_MSG_VALUE_DEFINED_IN_NON_DOMINANT_BLOCK, MIE_BUILTIN_MSG_VALUE_DEFINED_AFTER_USE, MIE_BUILTIN_MSG_VALUE_DEFINED_OUTSIDE_ISOLATED_REGION, MIE_BUILTIN_MSG_VALUE_DEFINED_IN_BLOCK, + MIE_BUILTIN_MSG_UNKNOWN_OP, + MIE_BUILTIN_MSG_OP_REQUIRES_PARENT_SCOPE, + MIE_BUILTIN_MSG_OP_PARENT_SCOPE_EXAMPLE, + MIE_BUILTIN_MSG_USE_GENERIC_OP_SYNTAX, + MIE_BUILTIN_MSG_OP_HAS_NO_PARSER, + MIE_BUILTIN_MSG_DIALECT_INTERNAL_ERROR, + MIE_BUILTIN_MSG_COMPILER_INTERNAL_ERROR, + MIE_BUILTIN_MSG_OP_INCORRECT_NUMBER_OF_ARGS, + MIE_BUILTIN_MSG_OP_INCORRECT_NUMBER_OF_RESULTS, + MIE_BUILTIN_MSG_OP_NR_TYPES_DOESNT_MATCH_NR_ARGS, + MIE_BUILTIN_MSG_OP_NR_TYPES_DOESNT_MATCH_NR_RESULTS, + MIE_BUILTIN_MSG_OP_EXPECTS_X_RESULTS, + MIE_BUILTIN_MSG_NR_RESULT_NAME_OUTPUT_MISMATCH, + MIE_BUILTIN_MSG_NAME_ALREADY_IN_USE, + MIE_BUILTIN_MSG_NAME_ALREADY_USED_HERE, + MIE_BUILTIN_MSG_CANNOT_USE_FUNCTION_TYPE_HERE, + MIE_BUILTIN_MSG_CANNOT_USE_STORAGE_TYPE_HERE, + MIE_BUILTIN_MSG_MUST_USE_FUNCTION_TYPE_HERE, + MIE_BUILTIN_MSG_MUST_USE_STORAGE_TYPE_HERE, }; enum mie_float_width { @@ -36,6 +70,12 @@ enum mie_float_width { MIE_FLOAT_64 = 64, }; +enum mie_memref_rank_type { + MIE_MEMREF_RANK_UNKNOWN = 0, + MIE_MEMREF_RANK_STATIC, + MIE_MEMREF_RANK_TYPE, +}; + struct mie_string { struct mie_attribute str_base; char *str_val; @@ -90,6 +130,15 @@ struct mie_symbol_table { struct mie_trait tab_base; }; +struct mie_memref_rank { + enum mie_memref_rank_type r_ranktype; + struct mie_file_span r_span; + union { + size_t r_static; + const struct mie_type *r_type; + }; +}; + struct mie_int_cache; struct mie_float_cache; struct mie_string_cache; @@ -136,6 +185,8 @@ MIE_API bool mie_float_get_value(const struct mie_attribute *attrib, double *out MIE_API struct mie_type *mie_ctx_get_int_type(struct mie_ctx *ctx, size_t bit_width); MIE_API struct mie_type *mie_ctx_get_float_type( struct mie_ctx *ctx, size_t bit_width); +MIE_API struct mie_type *mie_ctx_get_memref_type( + struct mie_ctx *ctx, const struct mie_memref_rank *ranks, size_t nr_ranks); MIE_API struct mie_attribute *mie_type_attr_create( struct mie_ctx *ctx, const struct mie_type *ty); @@ -143,4 +194,8 @@ MIE_API struct mie_attribute *mie_type_attr_create( MIE_API size_t mie_int_type_get_width(const struct mie_type *type); MIE_API size_t mie_float_type_get_width(const struct mie_type *type); +MIE_API size_t mie_memref_type_get_nr_ranks(const struct mie_type *type); +MIE_API const struct mie_memref_rank *mie_memref_type_get_rank( + const struct mie_type *type, size_t i); + #endif