mie: replace fixed value system with an extensible attribute interface
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).
This commit is contained in:
@@ -1,9 +1,63 @@
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/arith.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_arith, "arith")
|
||||
struct arith_dialect {
|
||||
struct mie_dialect d_base;
|
||||
struct mie_int_cache *ctx_ints;
|
||||
struct mie_float_cache *ctx_floats;
|
||||
};
|
||||
|
||||
static enum mie_status init(struct mie_dialect *d)
|
||||
{
|
||||
struct arith_dialect *dialect = (struct arith_dialect *)d;
|
||||
dialect->ctx_ints = mie_int_cache_create();
|
||||
dialect->ctx_floats = mie_float_cache_create();
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status cleanup(struct mie_dialect *d)
|
||||
{
|
||||
struct arith_dialect *dialect = (struct arith_dialect *)d;
|
||||
mie_int_cache_destroy(dialect->ctx_ints);
|
||||
mie_float_cache_destroy(dialect->ctx_floats);
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
struct mie_attribute *mie_ctx_get_int(
|
||||
struct mie_ctx *ctx, long long val, size_t nr_bits)
|
||||
{
|
||||
struct arith_dialect *dialect
|
||||
= (struct arith_dialect *)mie_ctx_get_dialect(ctx, "arith");
|
||||
if (!dialect) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (struct mie_attribute *)mie_int_cache_get(
|
||||
dialect->ctx_ints, ctx, val, nr_bits);
|
||||
}
|
||||
|
||||
struct mie_attribute *mie_ctx_get_float(
|
||||
struct mie_ctx *ctx, double val, size_t nr_bits)
|
||||
{
|
||||
struct arith_dialect *dialect
|
||||
= (struct arith_dialect *)mie_ctx_get_dialect(ctx, "arith");
|
||||
if (!dialect) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (struct mie_attribute *)mie_float_cache_get(
|
||||
dialect->ctx_floats, ctx, val, nr_bits);
|
||||
}
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_arith, struct arith_dialect, "arith")
|
||||
MIE_DIALECT_INIT(init);
|
||||
MIE_DIALECT_CLEANUP(cleanup);
|
||||
MIE_DIALECT_ADD_TYPE(mie_arith_int);
|
||||
MIE_DIALECT_ADD_TYPE(mie_arith_float);
|
||||
MIE_DIALECT_ADD_OP(mie_arith_addi);
|
||||
MIE_DIALECT_ADD_OP(mie_arith_addf);
|
||||
MIE_DIALECT_ADD_ATTRIBUTE(mie_arith_int);
|
||||
MIE_DIALECT_ADD_ATTRIBUTE(mie_arith_float);
|
||||
MIE_DIALECT_END()
|
||||
|
||||
85
mie/dialect/arith/attribute/float.c
Normal file
85
mie/dialect/arith/attribute/float.c
Normal file
@@ -0,0 +1,85 @@
|
||||
#include "../float.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_float *float_val = (const struct mie_float *)value;
|
||||
const struct float_type *float_ty
|
||||
= (const struct float_type *)float_val->f_type;
|
||||
if (!(out->p_flags & MIE_PRINT_F_ABBREVIATED)) {
|
||||
b_stream_write_string(out->p_stream, "#arith.float<", NULL);
|
||||
}
|
||||
|
||||
switch (float_ty->f_width) {
|
||||
case MIE_FLOAT_32:
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL, "%f : f%zu", float_val->f_val.v_32,
|
||||
float_ty->f_width);
|
||||
break;
|
||||
case MIE_FLOAT_64:
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL, "%lf : f%zu",
|
||||
float_val->f_val.v_64, float_ty->f_width);
|
||||
break;
|
||||
default:
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL, "NaN : f%zu", float_ty->f_width);
|
||||
break;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
double value = 0;
|
||||
struct mie_file_span span;
|
||||
|
||||
if (!mie_parser_parse_float(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_float_type_get_width(type);
|
||||
if (width == (size_t)-1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct mie_attribute *v
|
||||
= mie_ctx_get_float(mie_parser_get_mie_ctx(ctx), value, width);
|
||||
if (!v) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*out = v;
|
||||
return true;
|
||||
}
|
||||
|
||||
MIE_ATTRIBUTE_DEFINITION_BEGIN(mie_arith_float, "float")
|
||||
MIE_ATTRIBUTE_DEFINITION_STRUCT(struct mie_float);
|
||||
MIE_ATTRIBUTE_DEFINITION_PRINT(print);
|
||||
MIE_ATTRIBUTE_DEFINITION_PARSE(parse);
|
||||
MIE_ATTRIBUTE_DEFINITION_END()
|
||||
76
mie/dialect/arith/attribute/int.c
Normal file
76
mie/dialect/arith/attribute/int.c
Normal file
@@ -0,0 +1,76 @@
|
||||
#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()
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <blue/core/btree.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/arith.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -145,8 +146,10 @@ static struct float_width_cache_entry *float_width_cache_entry_create(
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->e_value.f_base.v_type = mie_arith_float_get_type(ctx, width);
|
||||
if (!out->e_value.f_base.v_type) {
|
||||
out->e_value.f_base.a_def
|
||||
= mie_ctx_get_attribute_definition(ctx, "arith", "float");
|
||||
out->e_value.f_type = mie_arith_float_get_type(ctx, width);
|
||||
if (!out->e_value.f_type) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
12
mie/dialect/arith/float.h
Normal file
12
mie/dialect/arith/float.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef _ARITH_FLOAT_H_
|
||||
#define _ARITH_FLOAT_H_
|
||||
|
||||
#include <mie/attribute/attribute.h>
|
||||
#include <mie/type/type.h>
|
||||
|
||||
struct float_type {
|
||||
struct mie_type f_base;
|
||||
size_t f_width;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <blue/core/btree.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/arith.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -70,9 +71,11 @@ static struct int_width_cache_entry *int_width_cache_entry_create(
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
out->e_value.i_base.a_def
|
||||
= mie_ctx_get_attribute_definition(ctx, "arith", "int");
|
||||
out->e_value.i_val.v_small = value;
|
||||
out->e_value.i_base.v_type = mie_arith_int_get_type(ctx, width);
|
||||
if (!out->e_value.i_base.v_type) {
|
||||
out->e_value.i_type = mie_arith_int_get_type(ctx, width);
|
||||
if (!out->e_value.i_type) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
12
mie/dialect/arith/int.h
Normal file
12
mie/dialect/arith/int.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef _ARITH_INT_H_
|
||||
#define _ARITH_INT_H_
|
||||
|
||||
#include <mie/attribute/attribute.h>
|
||||
#include <mie/type/type.h>
|
||||
|
||||
struct int_type {
|
||||
struct mie_type i_base;
|
||||
size_t i_width;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,3 +1,5 @@
|
||||
#include "../float.h"
|
||||
|
||||
#include <blue/core/bstr.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/arith.h>
|
||||
@@ -6,37 +8,6 @@
|
||||
#include <mie/print/printer.h>
|
||||
#include <mie/type/type-definition.h>
|
||||
#include <mie/type/type.h>
|
||||
#include <mie/value.h>
|
||||
|
||||
struct float_type {
|
||||
struct mie_type f_base;
|
||||
size_t f_width;
|
||||
};
|
||||
|
||||
static enum mie_status value_print(
|
||||
const struct mie_value *value, struct mie_printer *out)
|
||||
{
|
||||
struct float_type *float_ty = (struct float_type *)value->v_type;
|
||||
struct mie_float *float_val = (struct mie_float *)value;
|
||||
switch (float_ty->f_width) {
|
||||
case MIE_FLOAT_32:
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL, "%f : f%zu", float_val->f_val.v_32,
|
||||
float_ty->f_width);
|
||||
break;
|
||||
case MIE_FLOAT_64:
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL, "%lf : f%zu",
|
||||
float_val->f_val.v_64, float_ty->f_width);
|
||||
break;
|
||||
default:
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL, "NaN : f%zu", float_ty->f_width);
|
||||
break;
|
||||
}
|
||||
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
struct mie_type *mie_arith_float_get_type(struct mie_ctx *ctx, size_t bit_width)
|
||||
{
|
||||
@@ -91,15 +62,13 @@ static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL,
|
||||
(out->p_flags & MIE_PRINT_F_ABBREVIATED) ? "f%zu"
|
||||
: "arith.float<%zu>",
|
||||
: "!arith.float<%zu>",
|
||||
float_type->f_width);
|
||||
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
const struct mie_type_definition *def, struct mie_parser *parser,
|
||||
struct mie_type **out)
|
||||
static enum mie_status parse(struct mie_parser *parser, const struct mie_type **out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
@@ -109,5 +78,4 @@ MIE_TYPE_DEFINITION_BEGIN(mie_arith_float, "float")
|
||||
MIE_TYPE_DEFINITION_STRUCT(struct float_type);
|
||||
MIE_TYPE_DEFINITION_PRINT(print);
|
||||
MIE_TYPE_DEFINITION_PARSE(parse);
|
||||
MIE_TYPE_DEFINITION_VALUE_PRINT(value_print);
|
||||
MIE_TYPE_DEFINITION_END()
|
||||
@@ -1,3 +1,5 @@
|
||||
#include "../int.h"
|
||||
|
||||
#include <blue/core/bstr.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/arith.h>
|
||||
@@ -6,29 +8,6 @@
|
||||
#include <mie/print/printer.h>
|
||||
#include <mie/type/type-definition.h>
|
||||
#include <mie/type/type.h>
|
||||
#include <mie/value.h>
|
||||
|
||||
struct int_type {
|
||||
struct mie_type i_base;
|
||||
size_t i_width;
|
||||
};
|
||||
|
||||
static enum mie_status value_print(
|
||||
const struct mie_value *value, struct mie_printer *out)
|
||||
{
|
||||
struct int_type *int_ty = (struct int_type *)value->v_type;
|
||||
struct mie_int *int_val = (struct mie_int *)value;
|
||||
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);
|
||||
}
|
||||
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
struct mie_type *mie_arith_int_get_type(struct mie_ctx *ctx, size_t bit_width)
|
||||
{
|
||||
@@ -82,15 +61,13 @@ static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
|
||||
const struct int_type *int_type = (const struct int_type *)ty;
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL,
|
||||
(out->p_flags & MIE_PRINT_F_ABBREVIATED) ? "i%zu" : "arith.int<%zu>",
|
||||
(out->p_flags & MIE_PRINT_F_ABBREVIATED) ? "i%zu" : "!arith.int<%zu>",
|
||||
int_type->i_width);
|
||||
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
const struct mie_type_definition *def, struct mie_parser *parser,
|
||||
struct mie_type **out)
|
||||
static enum mie_status parse(struct mie_parser *parser, const struct mie_type **out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
@@ -100,5 +77,4 @@ MIE_TYPE_DEFINITION_BEGIN(mie_arith_int, "int")
|
||||
MIE_TYPE_DEFINITION_STRUCT(struct int_type);
|
||||
MIE_TYPE_DEFINITION_PRINT(print);
|
||||
MIE_TYPE_DEFINITION_PARSE(parse);
|
||||
MIE_TYPE_DEFINITION_VALUE_PRINT(value_print);
|
||||
MIE_TYPE_DEFINITION_END()
|
||||
47
mie/dialect/builtin/attribute/string.c
Normal file
47
mie/dialect/builtin/attribute/string.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <mie/attribute/attribute-definition.h>
|
||||
#include <mie/attribute/attribute.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/builtin.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_string *str = (const struct mie_string *)value;
|
||||
if (out->p_flags & MIE_PRINT_F_ABBREVIATED) {
|
||||
b_stream_write_fmt(out->p_stream, NULL, "\"%s\"", str->str_val);
|
||||
} else {
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL, "#builtin.string<\"%s\">",
|
||||
str->str_val);
|
||||
}
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
struct mie_parser *ctx, const struct mie_attribute **out)
|
||||
{
|
||||
b_string *str = mie_parser_get_tempstr(ctx);
|
||||
struct mie_file_span span;
|
||||
if (!mie_parser_parse_string(ctx, str, &span)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct mie_attribute *v = mie_ctx_get_string(
|
||||
mie_parser_get_mie_ctx(ctx), b_string_ptr(str));
|
||||
|
||||
if (!v) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*out = v;
|
||||
return true;
|
||||
}
|
||||
|
||||
MIE_ATTRIBUTE_DEFINITION_BEGIN(mie_builtin_string, "string")
|
||||
MIE_ATTRIBUTE_DEFINITION_STRUCT(struct mie_string);
|
||||
MIE_ATTRIBUTE_DEFINITION_PRINT(print);
|
||||
MIE_ATTRIBUTE_DEFINITION_PARSE(parse);
|
||||
MIE_ATTRIBUTE_DEFINITION_END();
|
||||
0
mie/dialect/builtin/attribute/type.c
Normal file
0
mie/dialect/builtin/attribute/type.c
Normal file
@@ -1,7 +1,43 @@
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/builtin.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_builtin, "builtin")
|
||||
struct builtin_dialect {
|
||||
struct mie_dialect d_base;
|
||||
struct mie_string_cache *ctx_strings;
|
||||
};
|
||||
|
||||
static enum mie_status init(struct mie_dialect *d)
|
||||
{
|
||||
struct builtin_dialect *dialect = (struct builtin_dialect *)d;
|
||||
dialect->ctx_strings = mie_string_cache_create();
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status cleanup(struct mie_dialect *d)
|
||||
{
|
||||
struct builtin_dialect *dialect = (struct builtin_dialect *)d;
|
||||
mie_string_cache_destroy(dialect->ctx_strings);
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
struct mie_attribute *mie_ctx_get_string(struct mie_ctx *ctx, const char *s)
|
||||
{
|
||||
struct builtin_dialect *dialect
|
||||
= (struct builtin_dialect *)mie_ctx_get_dialect(ctx, "builtin");
|
||||
if (!dialect) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (struct mie_attribute *)mie_string_cache_get(
|
||||
dialect->ctx_strings, ctx, s);
|
||||
}
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_builtin, struct builtin_dialect, "builtin")
|
||||
MIE_DIALECT_INIT(init);
|
||||
MIE_DIALECT_CLEANUP(cleanup);
|
||||
MIE_DIALECT_ADD_TYPE(mie_builtin_string);
|
||||
MIE_DIALECT_ADD_ATTRIBUTE(mie_builtin_string);
|
||||
MIE_DIALECT_ADD_TRAIT(mie_builtin_isolated_from_above);
|
||||
MIE_DIALECT_END()
|
||||
|
||||
@@ -17,12 +17,8 @@ static struct mie_string *mie_string_create(
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->str_base.v_type = mie_ctx_get_type(ctx, "builtin", "string");
|
||||
if (!out->str_base.v_type) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->str_base.a_def
|
||||
= mie_ctx_get_attribute_definition(ctx, "builtin", "string");
|
||||
out->str_val = b_strdup(s);
|
||||
if (!out->str_val) {
|
||||
return NULL;
|
||||
|
||||
@@ -6,15 +6,6 @@
|
||||
#include <mie/print/printer.h>
|
||||
#include <mie/type/type-definition.h>
|
||||
#include <mie/type/type.h>
|
||||
#include <mie/value.h>
|
||||
|
||||
static enum mie_status value_print(
|
||||
const struct mie_value *value, struct mie_printer *out)
|
||||
{
|
||||
const struct mie_string *str = (const struct mie_string *)value;
|
||||
b_stream_write_fmt(out->p_stream, NULL, "\"%s\"", str->str_val);
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static void type_init(
|
||||
const struct mie_type_definition *type_info, struct mie_type *type)
|
||||
@@ -27,17 +18,8 @@ static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
const struct mie_type_definition *def, struct mie_parser *parser,
|
||||
struct mie_type **out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_TYPE_DEFINITION_BEGIN(mie_builtin_string, "string")
|
||||
MIE_TYPE_DEFINITION_STRUCT(struct mie_type);
|
||||
MIE_TYPE_DEFINITION_INIT(type_init);
|
||||
MIE_TYPE_DEFINITION_PRINT(print);
|
||||
MIE_TYPE_DEFINITION_PARSE(parse);
|
||||
MIE_TYPE_DEFINITION_VALUE_PRINT(value_print);
|
||||
MIE_TYPE_DEFINITION_END()
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_cf, "cf")
|
||||
MIE_DIALECT_BEGIN(mie_cf, struct mie_dialect, "cf")
|
||||
MIE_DIALECT_ADD_OP(mie_cf_br);
|
||||
MIE_DIALECT_ADD_OP(mie_cf_br_cond);
|
||||
MIE_DIALECT_END()
|
||||
|
||||
@@ -11,14 +11,27 @@
|
||||
MIE_ID(0xb9, 0x97, 0xcf, 0xd3, 0x81, 0xd4, 0x45, 0x06, 0x9b, 0x44, \
|
||||
0x05, 0x9f, 0xb4, 0x76, 0xf1, 0x2d)
|
||||
|
||||
struct mie_dialect *mie_dialect_create(struct mie_ctx *ctx, const char *name)
|
||||
#define TRAIT_NS_ID \
|
||||
MIE_ID(0x5a, 0x11, 0x68, 0x8e, 0x21, 0x1d, 0x4d, 0x5f, 0xb8, 0x6b, \
|
||||
0x39, 0x73, 0xb0, 0x1f, 0xce, 0xf7)
|
||||
|
||||
#define ATTRIBUTE_NS_ID \
|
||||
MIE_ID(0x86, 0x76, 0xcb, 0xfb, 0xc8, 0xe5, 0x40, 0x7d, 0xa3, 0x84, \
|
||||
0x93, 0xe3, 0xa5, 0x29, 0x74, 0xfe)
|
||||
|
||||
struct mie_dialect *mie_dialect_create(
|
||||
struct mie_ctx *ctx, const char *name, size_t size)
|
||||
{
|
||||
struct mie_dialect *out = malloc(sizeof *out);
|
||||
if (size < sizeof(struct mie_dialect)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_dialect *out = malloc(size);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
memset(out, 0x0, size);
|
||||
|
||||
out->d_name = b_strdup(name);
|
||||
if (!out->d_name) {
|
||||
@@ -32,6 +45,12 @@ struct mie_dialect *mie_dialect_create(struct mie_ctx *ctx, const char *name)
|
||||
mie_id type_ns = TYPE_NS_ID;
|
||||
mie_id_map_init(&out->d_types, &type_ns);
|
||||
|
||||
mie_id trait_ns = TRAIT_NS_ID;
|
||||
mie_id_map_init(&out->d_traits, &trait_ns);
|
||||
|
||||
mie_id attribute_ns = ATTRIBUTE_NS_ID;
|
||||
mie_id_map_init(&out->d_attributes, &attribute_ns);
|
||||
|
||||
b_rope name_rope = B_ROPE_CSTR(name);
|
||||
mie_id_map_put(&ctx->ctx_dialects, &out->d_id, &name_rope);
|
||||
|
||||
|
||||
@@ -2,21 +2,6 @@
|
||||
#include <mie/ir/op-definition.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_OP_DEFINITION_BEGIN(mie_func_func, "func")
|
||||
MIE_OP_DEFINITION_PRINT(print);
|
||||
MIE_OP_DEFINITION_PARSE(parse);
|
||||
MIE_OP_DEFINITION_END()
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_func, "func")
|
||||
MIE_DIALECT_BEGIN(mie_func, struct mie_dialect, "func")
|
||||
MIE_DIALECT_ADD_OP(mie_func_func);
|
||||
MIE_DIALECT_END()
|
||||
|
||||
18
mie/dialect/func/op/func.c
Normal file
18
mie/dialect/func/op/func.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/ir/op-definition.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
static enum mie_status print(const struct mie_op *op, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_OP_DEFINITION_BEGIN(mie_func_func, "func")
|
||||
MIE_OP_DEFINITION_PRINT(print);
|
||||
MIE_OP_DEFINITION_PARSE(parse);
|
||||
MIE_OP_DEFINITION_END()
|
||||
@@ -29,11 +29,6 @@ static struct index_cache_entry *index_cache_entry_create(
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
out->e_value.i_value = val;
|
||||
out->e_value.i_base.v_type = mie_ctx_get_type(ctx, "index", "index");
|
||||
if (!out->e_value.i_base.v_type) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -1,56 +1,44 @@
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/index.h>
|
||||
#include <mie/macros.h>
|
||||
#include <mie/print/printer.h>
|
||||
#include <mie/type/type-definition.h>
|
||||
#include <mie/type/type.h>
|
||||
#include <mie/value.h>
|
||||
|
||||
struct index_type {
|
||||
struct mie_type i_base;
|
||||
struct index_dialect {
|
||||
struct mie_dialect d_base;
|
||||
struct mie_index_cache *ctx_indices;
|
||||
};
|
||||
|
||||
static enum mie_status value_print(
|
||||
const struct mie_value *value, struct mie_printer *out)
|
||||
static enum mie_status init(struct mie_dialect *d)
|
||||
{
|
||||
struct index_type *index_ty = (struct index_type *)value->v_type;
|
||||
struct mie_index *index_val = (struct mie_index *)value;
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL, "%zu : %s", index_val->i_value,
|
||||
index_ty->i_base.ty_def->ty_name);
|
||||
struct index_dialect *dialect = (struct index_dialect *)d;
|
||||
dialect->ctx_indices = mie_index_cache_create();
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static void type_init(
|
||||
const struct mie_type_definition *type_info, struct mie_type *type)
|
||||
static enum mie_status cleanup(struct mie_dialect *d)
|
||||
{
|
||||
type->ty_instance_size = sizeof(struct mie_index);
|
||||
}
|
||||
|
||||
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) ? "index" : "index.index",
|
||||
NULL);
|
||||
struct index_dialect *dialect = (struct index_dialect *)d;
|
||||
mie_index_cache_destroy(dialect->ctx_indices);
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
const struct mie_type_definition *def, struct mie_parser *parser,
|
||||
struct mie_type **out)
|
||||
struct mie_attribute *mie_ctx_get_index(struct mie_ctx *ctx, size_t val)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
struct index_dialect *dialect
|
||||
= (struct index_dialect *)mie_ctx_get_dialect(ctx, "index");
|
||||
if (!dialect) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (struct mie_attribute *)mie_index_cache_get(
|
||||
dialect->ctx_indices, ctx, val);
|
||||
}
|
||||
|
||||
MIE_TYPE_DEFINITION_BEGIN(mie_index_index, "index")
|
||||
MIE_TYPE_DEFINITION_STRUCT(struct index_type);
|
||||
MIE_TYPE_DEFINITION_INIT(type_init);
|
||||
MIE_TYPE_DEFINITION_PRINT(print);
|
||||
MIE_TYPE_DEFINITION_PARSE(parse);
|
||||
MIE_TYPE_DEFINITION_VALUE_PRINT(value_print);
|
||||
MIE_TYPE_DEFINITION_END()
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_index, "index")
|
||||
MIE_DIALECT_BEGIN(mie_index, struct index_dialect, "index")
|
||||
MIE_DIALECT_INIT(init);
|
||||
MIE_DIALECT_CLEANUP(cleanup);
|
||||
MIE_DIALECT_ADD_TYPE(mie_index_index);
|
||||
MIE_DIALECT_END()
|
||||
|
||||
31
mie/dialect/index/type/index.c
Normal file
31
mie/dialect/index/type/index.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/index.h>
|
||||
#include <mie/macros.h>
|
||||
#include <mie/print/printer.h>
|
||||
#include <mie/type/type-definition.h>
|
||||
#include <mie/type/type.h>
|
||||
|
||||
struct index_type {
|
||||
struct mie_type i_base;
|
||||
};
|
||||
|
||||
static void type_init(
|
||||
const struct mie_type_definition *type_info, struct mie_type *type)
|
||||
{
|
||||
type->ty_instance_size = sizeof(struct mie_index);
|
||||
}
|
||||
|
||||
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) ? "index" : "!index.index",
|
||||
NULL);
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_TYPE_DEFINITION_BEGIN(mie_index_index, "index")
|
||||
MIE_TYPE_DEFINITION_STRUCT(struct index_type);
|
||||
MIE_TYPE_DEFINITION_INIT(type_init);
|
||||
MIE_TYPE_DEFINITION_PRINT(print);
|
||||
MIE_TYPE_DEFINITION_END()
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_meta, "meta")
|
||||
MIE_DIALECT_BEGIN(mie_meta, struct mie_dialect, "meta")
|
||||
MIE_DIALECT_ADD_OP(mie_meta_source_filename);
|
||||
MIE_DIALECT_END()
|
||||
|
||||
@@ -2,29 +2,7 @@
|
||||
#include <mie/macros.h>
|
||||
#include <mie/type/type-definition.h>
|
||||
|
||||
struct ptr_type {
|
||||
struct mie_type_definition ptr_base;
|
||||
};
|
||||
|
||||
static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
const struct mie_type_definition *def, struct mie_parser *parser,
|
||||
struct mie_type **out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_TYPE_DEFINITION_BEGIN(mie_ptr_ptr, "ptr")
|
||||
MIE_TYPE_DEFINITION_STRUCT(struct ptr_type);
|
||||
MIE_TYPE_DEFINITION_PRINT(print);
|
||||
MIE_TYPE_DEFINITION_PARSE(parse);
|
||||
MIE_TYPE_DEFINITION_END()
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_ptr, "ptr")
|
||||
MIE_DIALECT_BEGIN(mie_ptr, struct mie_dialect, "ptr")
|
||||
MIE_DIALECT_ADD_OP(mie_ptr_load);
|
||||
MIE_DIALECT_ADD_OP(mie_ptr_store);
|
||||
MIE_DIALECT_ADD_TYPE(mie_ptr_ptr);
|
||||
|
||||
17
mie/dialect/ptr/type/ptr.c
Normal file
17
mie/dialect/ptr/type/ptr.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
#include <mie/type/type-definition.h>
|
||||
|
||||
struct ptr_type {
|
||||
struct mie_type_definition ptr_base;
|
||||
};
|
||||
|
||||
static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_TYPE_DEFINITION_BEGIN(mie_ptr_ptr, "ptr")
|
||||
MIE_TYPE_DEFINITION_STRUCT(struct ptr_type);
|
||||
MIE_TYPE_DEFINITION_PRINT(print);
|
||||
MIE_TYPE_DEFINITION_END()
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_scf, "scf")
|
||||
MIE_DIALECT_BEGIN(mie_scf, struct mie_dialect, "scf")
|
||||
MIE_DIALECT_ADD_OP(mie_scf_if);
|
||||
MIE_DIALECT_ADD_OP(mie_scf_for);
|
||||
MIE_DIALECT_ADD_OP(mie_scf_yield);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_select, "select")
|
||||
MIE_DIALECT_BEGIN(mie_select, struct mie_dialect, "select")
|
||||
MIE_DIALECT_ADD_TRAIT(mie_select_graph_only);
|
||||
MIE_DIALECT_ADD_TRAIT(mie_select_graph_op);
|
||||
MIE_DIALECT_ADD_TRAIT(mie_select_graph_scope);
|
||||
|
||||
Reference in New Issue
Block a user