mie: implement support for custom op print formats
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/ir/op-definition.h>
|
||||
#include <mie/ir/op.h>
|
||||
#include <mie/macros.h>
|
||||
#include <mie/print/printer.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
||||
{
|
||||
const struct mie_attribute *sym_name
|
||||
= mie_attribute_map_get(&op->op_attrib, "sym_name");
|
||||
b_stream_write_fmt(printer->p_stream, NULL, " @%s", NULL);
|
||||
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <mie/ir/op-definition.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
#include <mie/attribute/attribute-definition.h>
|
||||
#include <mie/attribute/attribute.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/builtin.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
#include <mie/parse/parser.h>
|
||||
#include <mie/print/printer.h>
|
||||
|
||||
static enum mie_status print(
|
||||
const struct mie_attribute *value, struct mie_printer *out)
|
||||
{
|
||||
const struct mie_array *array = (const struct mie_array *)value;
|
||||
|
||||
if (!(out->p_flags & MIE_PRINT_F_ABBREVIATED)) {
|
||||
b_stream_write_string(out->p_stream, "#builtin.array<", NULL);
|
||||
}
|
||||
|
||||
b_stream_write_char(out->p_stream, '[');
|
||||
|
||||
for (size_t i = 0; i < MIE_VECTOR_COUNT(array->a_items); i++) {
|
||||
if (i > 0) {
|
||||
b_stream_write_char(out->p_stream, ',');
|
||||
}
|
||||
|
||||
b_stream_write_char(out->p_stream, ' ');
|
||||
mie_printer_print_attribute(out, array->a_items.items[i]);
|
||||
}
|
||||
|
||||
if (MIE_VECTOR_COUNT(array->a_items) != 0) {
|
||||
b_stream_write_char(out->p_stream, ' ');
|
||||
}
|
||||
b_stream_write_char(out->p_stream, ']');
|
||||
|
||||
if (!(out->p_flags & MIE_PRINT_F_ABBREVIATED)) {
|
||||
b_stream_write_char(out->p_stream, '>');
|
||||
}
|
||||
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static struct mie_array *array_create(struct mie_ctx *ctx)
|
||||
{
|
||||
struct mie_array *array = malloc(sizeof *array);
|
||||
if (!array) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(array, 0x0, sizeof *array);
|
||||
|
||||
array->a_base.a_def
|
||||
= mie_ctx_get_attribute_definition(ctx, "builtin", "array");
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
struct mie_parser *ctx, const struct mie_attribute **out)
|
||||
{
|
||||
if (!mie_parser_parse_symbol(ctx, MIE_SYM_LEFT_BRACKET)) {
|
||||
return MIE_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
struct mie_array *array = array_create(mie_parser_get_mie_ctx(ctx));
|
||||
if (!array) {
|
||||
return MIE_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
const struct mie_attribute *item = NULL;
|
||||
|
||||
if (!mie_parser_parse_attribute(ctx, &item)) {
|
||||
free(array);
|
||||
return MIE_ERR_BAD_FORMAT;
|
||||
}
|
||||
|
||||
mie_vector_push_back(array->a_items, &item);
|
||||
|
||||
while (1) {
|
||||
if (mie_parser_parse_symbol(ctx, MIE_SYM_RIGHT_BRACKET)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!mie_parser_parse_symbol(ctx, MIE_SYM_COMMA)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!mie_parser_parse_attribute(ctx, &item)) {
|
||||
free(array);
|
||||
return MIE_ERR_BAD_FORMAT;
|
||||
}
|
||||
|
||||
mie_vector_push_back(array->a_items, &item);
|
||||
}
|
||||
|
||||
*out = (struct mie_attribute *)array;
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_ATTRIBUTE_DEFINITION_BEGIN(mie_builtin_array, "array")
|
||||
MIE_ATTRIBUTE_DEFINITION_STRUCT(struct mie_array);
|
||||
MIE_ATTRIBUTE_DEFINITION_PRINT(print);
|
||||
MIE_ATTRIBUTE_DEFINITION_PARSE(parse);
|
||||
MIE_ATTRIBUTE_DEFINITION_END()
|
||||
|
||||
@@ -51,31 +51,31 @@ static enum mie_status parse(
|
||||
struct mie_file_span span;
|
||||
|
||||
if (!mie_parser_parse_float(ctx, &value, &span)) {
|
||||
return false;
|
||||
return MIE_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
if (!mie_parser_parse_symbol(ctx, MIE_SYM_COLON)) {
|
||||
return false;
|
||||
return MIE_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
const struct mie_type *type = NULL;
|
||||
if (!mie_parser_parse_type(ctx, &type)) {
|
||||
return false;
|
||||
return MIE_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
size_t width = mie_float_type_get_width(type);
|
||||
if (width == (size_t)-1) {
|
||||
return false;
|
||||
return MIE_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
struct mie_attribute *v
|
||||
= mie_ctx_get_float(mie_parser_get_mie_ctx(ctx), value, width);
|
||||
if (!v) {
|
||||
return false;
|
||||
return MIE_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
*out = v;
|
||||
return true;
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_ATTRIBUTE_DEFINITION_BEGIN(mie_builtin_float, "float")
|
||||
|
||||
@@ -15,20 +15,25 @@ static enum mie_status print(
|
||||
{
|
||||
const struct mie_int *int_val = (const struct mie_int *)value;
|
||||
const struct int_type *int_ty = (const struct int_type *)int_val->i_type;
|
||||
if (!(out->p_flags & MIE_PRINT_F_ABBREVIATED)) {
|
||||
bool abbrev = ((out->p_flags & MIE_PRINT_F_ABBREVIATED) != 0);
|
||||
|
||||
if (!abbrev) {
|
||||
b_stream_write_string(out->p_stream, "#builtin.int<", NULL);
|
||||
}
|
||||
|
||||
if (int_ty->i_width <= 64) {
|
||||
if (int_ty->i_width < 64 || !abbrev) {
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL, "%zu : i%zu",
|
||||
int_val->i_val.v_small, int_ty->i_width);
|
||||
} else if (int_ty->i_width == 64 && abbrev) {
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL, "%zu", int_val->i_val.v_small);
|
||||
} else {
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL, "INF : i%zu", int_ty->i_width);
|
||||
}
|
||||
|
||||
if (!(out->p_flags & MIE_PRINT_F_ABBREVIATED)) {
|
||||
if (!abbrev) {
|
||||
b_stream_write_string(out->p_stream, ">", NULL);
|
||||
}
|
||||
|
||||
@@ -42,31 +47,32 @@ static enum mie_status parse(
|
||||
struct mie_file_span span;
|
||||
|
||||
if (!mie_parser_parse_int(ctx, &value, &span)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!mie_parser_parse_symbol(ctx, MIE_SYM_COLON)) {
|
||||
return false;
|
||||
return MIE_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
const struct mie_type *type = NULL;
|
||||
if (!mie_parser_parse_type(ctx, &type)) {
|
||||
size_t width = (size_t)-1;
|
||||
|
||||
if (!mie_parser_parse_symbol(ctx, MIE_SYM_COLON)) {
|
||||
width = 64;
|
||||
} else if (!mie_parser_parse_type(ctx, &type)) {
|
||||
return false;
|
||||
} else {
|
||||
width = mie_int_type_get_width(type);
|
||||
}
|
||||
|
||||
size_t width = mie_int_type_get_width(type);
|
||||
if (width == (size_t)-1) {
|
||||
return false;
|
||||
return MIE_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
struct mie_attribute *v
|
||||
= mie_ctx_get_int(mie_parser_get_mie_ctx(ctx), value, width);
|
||||
if (!v) {
|
||||
return false;
|
||||
return MIE_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
*out = v;
|
||||
return true;
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_ATTRIBUTE_DEFINITION_BEGIN(mie_builtin_int, "int")
|
||||
|
||||
@@ -26,18 +26,28 @@ static enum mie_status parse(
|
||||
b_string *str = mie_parser_get_tempstr(ctx);
|
||||
struct mie_file_span span;
|
||||
if (!mie_parser_parse_string(ctx, str, &span)) {
|
||||
return false;
|
||||
return MIE_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
struct mie_attribute *v = mie_ctx_get_string(
|
||||
mie_parser_get_mie_ctx(ctx), b_string_ptr(str));
|
||||
|
||||
if (!v) {
|
||||
return false;
|
||||
return MIE_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
*out = v;
|
||||
return true;
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
const char *mie_string_get_cstr(const struct mie_attribute *attrib)
|
||||
{
|
||||
if (!mie_attribute_check_name(attrib, "builtin", "string")) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const struct mie_string *str = (const struct mie_string *)attrib;
|
||||
return str->str_val;
|
||||
}
|
||||
|
||||
MIE_ATTRIBUTE_DEFINITION_BEGIN(mie_builtin_string, "string")
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
#include <mie/attribute/attribute-definition.h>
|
||||
#include <mie/attribute/attribute.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/builtin.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
#include <mie/parse/parser.h>
|
||||
#include <mie/print/printer.h>
|
||||
|
||||
static enum mie_status print(
|
||||
const struct mie_attribute *value, struct mie_printer *out)
|
||||
{
|
||||
const struct mie_type_attr *ty = (const struct mie_type_attr *)value;
|
||||
|
||||
if (!(out->p_flags & MIE_PRINT_F_ABBREVIATED)) {
|
||||
b_stream_write_string(out->p_stream, "#builtin.type<", NULL);
|
||||
}
|
||||
|
||||
mie_printer_print_type(out, ty->ty_value);
|
||||
|
||||
if (!(out->p_flags & MIE_PRINT_F_ABBREVIATED)) {
|
||||
b_stream_write_char(out->p_stream, '>');
|
||||
}
|
||||
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static struct mie_type_attr *type_attr_create(struct mie_ctx *ctx)
|
||||
{
|
||||
struct mie_type_attr *ty = malloc(sizeof *ty);
|
||||
if (!ty) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(ty, 0x0, sizeof *ty);
|
||||
|
||||
ty->ty_base.a_def
|
||||
= mie_ctx_get_attribute_definition(ctx, "builtin", "type");
|
||||
|
||||
return ty;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
struct mie_parser *ctx, const struct mie_attribute **out)
|
||||
{
|
||||
struct mie_type_attr *ty = type_attr_create(mie_parser_get_mie_ctx(ctx));
|
||||
if (!ty) {
|
||||
return MIE_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
const struct mie_type *type = NULL;
|
||||
|
||||
if (!mie_parser_parse_type(ctx, &type)) {
|
||||
free(ty);
|
||||
return MIE_ERR_BAD_FORMAT;
|
||||
}
|
||||
|
||||
ty->ty_value = type;
|
||||
|
||||
*out = (struct mie_attribute *)ty;
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
const struct mie_type *mie_type_attr_get_type(const struct mie_attribute *attrib)
|
||||
{
|
||||
if (!mie_attribute_check_name(attrib, "builtin", "type")) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const struct mie_type_attr *ty = (const struct mie_type_attr *)attrib;
|
||||
return ty->ty_value;
|
||||
}
|
||||
|
||||
MIE_ATTRIBUTE_DEFINITION_BEGIN(mie_builtin_type, "type")
|
||||
MIE_ATTRIBUTE_DEFINITION_STRUCT(struct mie_type_attr);
|
||||
MIE_ATTRIBUTE_DEFINITION_PRINT(print);
|
||||
MIE_ATTRIBUTE_DEFINITION_PARSE(parse);
|
||||
MIE_ATTRIBUTE_DEFINITION_END()
|
||||
|
||||
@@ -91,6 +91,8 @@ MIE_DIALECT_BEGIN(mie_builtin, struct builtin_dialect, "builtin")
|
||||
MIE_DIALECT_ADD_ATTRIBUTE(mie_builtin_string);
|
||||
MIE_DIALECT_ADD_ATTRIBUTE(mie_builtin_int);
|
||||
MIE_DIALECT_ADD_ATTRIBUTE(mie_builtin_float);
|
||||
MIE_DIALECT_ADD_ATTRIBUTE(mie_builtin_array);
|
||||
MIE_DIALECT_ADD_ATTRIBUTE(mie_builtin_type);
|
||||
MIE_DIALECT_ADD_TRAIT(mie_builtin_isolated_from_above);
|
||||
MIE_DIALECT_ADD_TRAIT(mie_builtin_symbol_table);
|
||||
MIE_DIALECT_ADD_INTERFACE(mie_builtin_symbol);
|
||||
|
||||
@@ -15,6 +15,10 @@ static void type_init(
|
||||
|
||||
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) ? "str" : "!builtin.string",
|
||||
NULL);
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <mie/ir/op-definition.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <mie/ir/op-definition.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,69 @@
|
||||
#include <mie/attribute/attribute-map.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/builtin.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/interface/interface-definition.h>
|
||||
#include <mie/interface/interface.h>
|
||||
#include <mie/ir/block.h>
|
||||
#include <mie/ir/op-definition.h>
|
||||
#include <mie/ir/op.h>
|
||||
#include <mie/ir/region.h>
|
||||
#include <mie/macros.h>
|
||||
#include <mie/print/printer.h>
|
||||
#include <mie/type/function.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
||||
{
|
||||
const struct mie_attribute *sym_name
|
||||
= mie_attribute_map_get(&op->op_attrib, "sym_name");
|
||||
const struct mie_attribute *function_type_attr
|
||||
= mie_attribute_map_get(&op->op_attrib, "function_type");
|
||||
const char *sym_name_cstr = mie_string_get_cstr(sym_name);
|
||||
const struct mie_type *function_type_g
|
||||
= mie_type_attr_get_type(function_type_attr);
|
||||
const struct mie_function_type *function_ty
|
||||
= (const struct mie_function_type *)function_type_g;
|
||||
|
||||
b_stream_write_fmt(printer->p_stream, NULL, " @%s(", sym_name_cstr);
|
||||
|
||||
const struct mie_region *code = &op->op_regions.items[0];
|
||||
const struct mie_block *entry = code->r_blocks.items[0];
|
||||
|
||||
for (size_t i = 0; i < MIE_VECTOR_COUNT(entry->b_params); i++) {
|
||||
if (i > 0) {
|
||||
b_stream_write_string(printer->p_stream, ", ", NULL);
|
||||
}
|
||||
|
||||
const struct mie_register *param = entry->b_params.items[i];
|
||||
|
||||
mie_printer_print_register(printer, param, MIE_PRINT_F_INCLUDE_TYPE);
|
||||
}
|
||||
|
||||
b_stream_write_string(printer->p_stream, ") -> ", NULL);
|
||||
|
||||
if (MIE_VECTOR_COUNT(function_ty->func_out) != 1) {
|
||||
b_stream_write_char(printer->p_stream, '(');
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < MIE_VECTOR_COUNT(function_ty->func_out); i++) {
|
||||
if (i > 0) {
|
||||
b_stream_write_string(printer->p_stream, ", ", NULL);
|
||||
}
|
||||
|
||||
const struct mie_type *ty = function_ty->func_out.items[i];
|
||||
|
||||
mie_printer_print_type(printer, ty);
|
||||
}
|
||||
|
||||
if (MIE_VECTOR_COUNT(function_ty->func_out) != 1) {
|
||||
b_stream_write_char(printer->p_stream, ')');
|
||||
}
|
||||
|
||||
b_stream_write_char(printer->p_stream, ' ');
|
||||
|
||||
mie_printer_print_region(
|
||||
printer, code, MIE_PRINT_F_EXCLUDE_FIRST_BLOCK_HEADER);
|
||||
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <mie/ir/op-definition.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <mie/ir/op-definition.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <mie/ir/op-definition.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <mie/ir/op-definition.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <mie/ir/op-definition.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <mie/ir/op-definition.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <mie/macros.h>
|
||||
#include <mie/trait/trait.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user