104 lines
2.4 KiB
C
104 lines
2.4 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/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_int *int_val = (const struct mie_int *)value;
|
|
const struct int_type *int_ty = (const struct int_type *)int_val->i_type;
|
|
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) {
|
|
b_stream_write_fmt(
|
|
out->p_stream, NULL, "%zu", int_val->i_val.v_small,
|
|
int_ty->i_width);
|
|
} else {
|
|
b_stream_write_fmt(out->p_stream, NULL, "INF", int_ty->i_width);
|
|
}
|
|
|
|
if (int_ty->i_width != 64 || !abbrev) {
|
|
b_stream_write_string(out->p_stream, " : ", NULL);
|
|
|
|
if (abbrev) {
|
|
b_stream_write_fmt(
|
|
out->p_stream, NULL, "i%zu", int_ty->i_width);
|
|
} else {
|
|
b_stream_write_fmt(
|
|
out->p_stream, NULL, "!builtin.int<%zu>",
|
|
int_ty->i_width);
|
|
}
|
|
}
|
|
|
|
if (!abbrev) {
|
|
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 MIE_ERR_BAD_SYNTAX;
|
|
}
|
|
|
|
const struct mie_type *type = NULL;
|
|
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);
|
|
}
|
|
|
|
if (width == (size_t)-1) {
|
|
return MIE_ERR_BAD_SYNTAX;
|
|
}
|
|
|
|
struct mie_attribute *v
|
|
= mie_ctx_get_int(mie_parser_get_mie_ctx(ctx), value, width);
|
|
if (!v) {
|
|
return MIE_ERR_NO_MEMORY;
|
|
}
|
|
|
|
*out = v;
|
|
return MIE_SUCCESS;
|
|
}
|
|
|
|
bool mie_int_get_value(const struct mie_attribute *attrib, long long *out)
|
|
{
|
|
if (!mie_attribute_check_name(attrib, "builtin", "int")) {
|
|
return false;
|
|
}
|
|
|
|
const struct mie_int *v = (const struct mie_int *)attrib;
|
|
*out = v->i_val.v_small;
|
|
|
|
return true;
|
|
}
|
|
|
|
MIE_ATTRIBUTE_DEFINITION_BEGIN(mie_builtin_int, "int")
|
|
MIE_ATTRIBUTE_DEFINITION_STRUCT(struct mie_int);
|
|
MIE_ATTRIBUTE_DEFINITION_PRINT(print);
|
|
MIE_ATTRIBUTE_DEFINITION_PARSE(parse);
|
|
MIE_ATTRIBUTE_DEFINITION_END()
|