under this new system, dialects can define their own custom attributes, complete with their own print() and parse() callbacks, which can then be used as values in an op's attribute dictionary. alongside custom dialect attributes, the former int, float, and string constant values have been converted to attributes provided by the arith and builtin dialects respectively. the caches for these attributes have also been moved from mie_ctx to their respective dialect data structures. this system will allow new types of attributes to be implemented, including dictionaries, arrays, and references to types themselves (rather than just particular values of a given type).
77 lines
1.8 KiB
C
77 lines
1.8 KiB
C
#include "../int.h"
|
|
|
|
#include <blue/core/bstr.h>
|
|
#include <mie/attribute/attribute-definition.h>
|
|
#include <mie/attribute/attribute.h>
|
|
#include <mie/ctx.h>
|
|
#include <mie/dialect/arith.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_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)) {
|
|
b_stream_write_string(out->p_stream, "#arith.int<", NULL);
|
|
}
|
|
|
|
if (int_ty->i_width <= 64) {
|
|
b_stream_write_fmt(
|
|
out->p_stream, NULL, "%zu : i%zu",
|
|
int_val->i_val.v_small, int_ty->i_width);
|
|
} else {
|
|
b_stream_write_fmt(
|
|
out->p_stream, NULL, "INF : i%zu", int_ty->i_width);
|
|
}
|
|
|
|
if (!(out->p_flags & MIE_PRINT_F_ABBREVIATED)) {
|
|
b_stream_write_string(out->p_stream, ">", NULL);
|
|
}
|
|
|
|
return MIE_SUCCESS;
|
|
}
|
|
|
|
static enum mie_status parse(
|
|
struct mie_parser *ctx, const struct mie_attribute **out)
|
|
{
|
|
long long value = 0;
|
|
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;
|
|
}
|
|
|
|
const struct mie_type *type = NULL;
|
|
if (!mie_parser_parse_type(ctx, &type)) {
|
|
return false;
|
|
}
|
|
|
|
size_t width = mie_arith_int_type_get_width(type);
|
|
if (width == (size_t)-1) {
|
|
return false;
|
|
}
|
|
|
|
struct mie_attribute *v
|
|
= mie_ctx_get_int(mie_parser_get_mie_ctx(ctx), value, width);
|
|
if (!v) {
|
|
return false;
|
|
}
|
|
|
|
*out = v;
|
|
return true;
|
|
}
|
|
|
|
MIE_ATTRIBUTE_DEFINITION_BEGIN(mie_arith_int, "int")
|
|
MIE_ATTRIBUTE_DEFINITION_STRUCT(struct mie_int);
|
|
MIE_ATTRIBUTE_DEFINITION_PRINT(print);
|
|
MIE_ATTRIBUTE_DEFINITION_PARSE(parse);
|
|
MIE_ATTRIBUTE_DEFINITION_END()
|