Compare commits
12 Commits
b5fa40d4d8
...
6abdf8f993
| Author | SHA1 | Date | |
|---|---|---|---|
| 6abdf8f993 | |||
| 870b2bfbac | |||
| 5344c02b48 | |||
| 4a4f0c483a | |||
| f37ee90b0e | |||
| 3f901ed83a | |||
| b28efa2d08 | |||
| 5835d25679 | |||
| d4fbd75a59 | |||
| 0b1d4697a2 | |||
| cd49417972 | |||
| bd5ba9e9fd |
@@ -1,22 +1,22 @@
|
|||||||
select.graph {
|
select.graph {
|
||||||
%entry = +select.entry -> #select.chain
|
%entry = +select.entry -> !select.chain
|
||||||
|
|
||||||
%0 = +select.register @0 : i32 -> #select.register
|
%0 = +select.register @0 : i32 -> !select.register
|
||||||
%1 = +select.register @1 : i32 -> #select.register
|
%1 = +select.register @1 : i32 -> !select.register
|
||||||
|
|
||||||
%v.0, %c.0 = +select.register.read %entry, %0 \
|
%v.0, %c.0 = +select.register.read %entry, %0 \
|
||||||
: (#select.chain, #select.register) \
|
: (!select.chain, !select.register) \
|
||||||
-> (i32, #select.chain)
|
-> (i32, !select.chain)
|
||||||
|
|
||||||
%v.1, %c.1 = +select.register.read %entry, %1 \
|
%v.1, %c.1 = +select.register.read %entry, %1 \
|
||||||
: (#select.chain, #select.register) \
|
: (!select.chain, !select.register) \
|
||||||
-> (i32, #select.chain)
|
-> (i32, !select.chain)
|
||||||
|
|
||||||
%2 = +select.register @2 : i32
|
%2 = +select.register @2 : i32
|
||||||
|
|
||||||
%v.2, %c.2 = +select.register.read %entry, %2 \
|
%v.2, %c.2 = +select.register.read %entry, %2 \
|
||||||
: (#select.chain, #select.register) \
|
: (!select.chain, !select.register) \
|
||||||
-> (#select.register, #select.chain)
|
-> (!select.register, !select.chain)
|
||||||
|
|
||||||
%v.3 = arith.build-pair %v.0, %v.1 : (i32, i32) -> i64
|
%v.3 = arith.build-pair %v.0, %v.1 : (i32, i32) -> i64
|
||||||
|
|
||||||
@@ -24,15 +24,15 @@ select.graph {
|
|||||||
|
|
||||||
%v.5 = arith.mul %v.4, %v.3 : (i32, i32) -> i32
|
%v.5 = arith.mul %v.4, %v.3 : (i32, i32) -> i32
|
||||||
|
|
||||||
%x0 = +select.mach-register @x0 : i32 -> #select.register
|
%x0 = +select.mach-register @x0 : i32 -> !select.register
|
||||||
|
|
||||||
%c.3, %g.0 = +select.register.write %entry, %x0, %v.5 \
|
%c.3, %g.0 = +select.register.write %entry, %x0, %v.5 \
|
||||||
: (#select.chain, #select.register, i32) \
|
: (!select.chain, !select.register, i32) \
|
||||||
-> (#select.chain, #select.glue)
|
-> (!select.chain, !select.glue)
|
||||||
|
|
||||||
%c.4 = *risc-v.RET %c.3, %x0, %g.0 \
|
%c.4 = *risc-v.RET %c.3, %x0, %g.0 \
|
||||||
: (#select.chain, #select.register, #select.glue) \
|
: (!select.chain, !select.register, !select.glue) \
|
||||||
-> #select.chain
|
-> !select.chain
|
||||||
|
|
||||||
+select.graph-root %c.4 : #select.chain
|
+select.graph-root %c.4 : !select.chain
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,31 +1,31 @@
|
|||||||
select.graph {
|
select.graph {
|
||||||
%entry = +select.entry -> #select.chain
|
%entry = +select.entry -> !select.chain
|
||||||
|
|
||||||
%N1 = ptr.alloca i32 -> ptr
|
%N1 = ptr.alloca i32 -> ptr
|
||||||
|
|
||||||
%N2 = i32.constant 3 -> i32
|
%N2 = i32.constant 3 -> i32
|
||||||
|
|
||||||
%N3.c = +ptr.store %entry, %N2, %N1 : (#select.chain, i32, ptr) -> #select.chain
|
%N3.c = +ptr.store %entry, %N2, %N1 : (!select.chain, i32, ptr) -> !select.chain
|
||||||
|
|
||||||
%N4 = ptr.alloca i32 -> ptr
|
%N4 = ptr.alloca i32 -> ptr
|
||||||
|
|
||||||
%N5 = i32.constant 0 -> i32
|
%N5 = i32.constant 0 -> i32
|
||||||
|
|
||||||
%N6.c = +ptr.store %entry, %N5, %N4 : (#select.chain, i32, ptr) -> #select.chain
|
%N6.c = +ptr.store %entry, %N5, %N4 : (!select.chain, i32, ptr) -> !select.chain
|
||||||
|
|
||||||
%N7, %N7.c = +ptr.load %N6.c, %N1 : (#select.chain, ptr) -> (#ivy.id, #select.chain)
|
%N7, %N7.c = +ptr.load %N6.c, %N1 : (!select.chain, ptr) -> (!ivy.id, !select.chain)
|
||||||
|
|
||||||
%N8 = i32.constant 10 -> i32
|
%N8 = i32.constant 10 -> i32
|
||||||
|
|
||||||
%N9 = arith.cmp gt %N7, %N8 : (#ivy.id, i32) -> i1
|
%N9 = arith.cmp gt %N7, %N8 : (!ivy.id, i32) -> i1
|
||||||
|
|
||||||
%N10.c = +select.chain-group %N7.c, %N6.c : (#select.chain, #select.chain) -> #select.chain
|
%N10.c = +select.chain-group %N7.c, %N6.c : (!select.chain, !select.chain) -> !select.chain
|
||||||
|
|
||||||
%N11 = +select.block-ref @if.true -> #select.block
|
%N11 = +select.block-ref @if.true -> !select.block
|
||||||
|
|
||||||
%N12.c = +cf.br-cond %N10.c, %N9, %N11 : (#select.chain, i1, #select.block) -> #select.chain
|
%N12.c = +cf.br-cond %N10.c, %N9, %N11 : (!select.chain, i1, !select.block) -> !select.chain
|
||||||
|
|
||||||
%N13 = +select.block-ref @if.false -> #select.block
|
%N13 = +select.block-ref @if.false -> !select.block
|
||||||
|
|
||||||
%N14.c = cf.br %N12.c, %N13
|
%N14.c = cf.br %N12.c, %N13
|
||||||
|
|
||||||
|
|||||||
45
mie/ctx.c
45
mie/ctx.c
@@ -4,6 +4,8 @@
|
|||||||
#include <blue/ds/string.h>
|
#include <blue/ds/string.h>
|
||||||
#include <mie/attribute/attribute-definition.h>
|
#include <mie/attribute/attribute-definition.h>
|
||||||
#include <mie/ctx.h>
|
#include <mie/ctx.h>
|
||||||
|
#include <mie/diag/class.h>
|
||||||
|
#include <mie/diag/diag.h>
|
||||||
#include <mie/dialect/arith.h>
|
#include <mie/dialect/arith.h>
|
||||||
#include <mie/dialect/builtin.h>
|
#include <mie/dialect/builtin.h>
|
||||||
#include <mie/dialect/dialect.h>
|
#include <mie/dialect/dialect.h>
|
||||||
@@ -86,7 +88,6 @@ struct mie_op *mie_ctx_create_op(
|
|||||||
|
|
||||||
out->op_flags |= MIE_OP_F_OP_RESOLVED;
|
out->op_flags |= MIE_OP_F_OP_RESOLVED;
|
||||||
out->op_info = def;
|
out->op_info = def;
|
||||||
out->op_dialect = def->op_parent;
|
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
@@ -127,7 +128,6 @@ bool mie_ctx_resolve_op(const struct mie_ctx *ctx, struct mie_op *op)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
op->op_dialect = dialect;
|
|
||||||
op->op_info = op_info;
|
op->op_info = op_info;
|
||||||
|
|
||||||
free(op->op_name);
|
free(op->op_name);
|
||||||
@@ -484,3 +484,44 @@ enum mie_status mie_ctx_get_pass(
|
|||||||
*out = pass;
|
*out = pass;
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct mie_diag *mie_ctx_push_diag(
|
||||||
|
struct mie_ctx *ctx, struct mie_line_source *src,
|
||||||
|
const struct mie_file_cell *loc, const char *dialect_name,
|
||||||
|
unsigned long diag_class)
|
||||||
|
{
|
||||||
|
struct mie_diag *diag = malloc(sizeof *diag);
|
||||||
|
if (!diag) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_dialect *dialect = mie_ctx_get_dialect(ctx, dialect_name);
|
||||||
|
if (!dialect) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (diag_class >= dialect->d_nr_diag_classes) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(diag, 0x0, sizeof *diag);
|
||||||
|
|
||||||
|
diag->diag_src = src;
|
||||||
|
diag->diag_loc = *loc;
|
||||||
|
diag->diag_parent = dialect;
|
||||||
|
diag->diag_class = &dialect->d_diag_classes[diag_class];
|
||||||
|
|
||||||
|
b_queue_push_back(&ctx->ctx_diag, &diag->diag_entry);
|
||||||
|
|
||||||
|
return diag;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_diag *mie_ctx_pop_diag(struct mie_ctx *ctx)
|
||||||
|
{
|
||||||
|
b_queue_entry *entry = b_queue_pop_front(&ctx->ctx_diag);
|
||||||
|
if (!entry) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return b_unbox(struct mie_diag, entry, diag_entry);
|
||||||
|
}
|
||||||
|
|||||||
106
mie/diag/diag.c
Normal file
106
mie/diag/diag.c
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
#include <blue/ds/string.h>
|
||||||
|
#include <mie/ctx.h>
|
||||||
|
#include <mie/diag/amendment.h>
|
||||||
|
#include <mie/diag/component.h>
|
||||||
|
#include <mie/diag/diag.h>
|
||||||
|
#include <mie/diag/highlight.h>
|
||||||
|
#include <mie/diag/msg.h>
|
||||||
|
#include <mie/dialect/dialect.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
struct mie_diag_c_msg *diag_msg_create(const struct mie_diag_msg *content)
|
||||||
|
{
|
||||||
|
struct mie_diag_c_msg *out = malloc(sizeof *out);
|
||||||
|
if (!out) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(out, 0x0, sizeof *out);
|
||||||
|
|
||||||
|
out->msg_base.c_type = MIE_DIAG_COMPONENT_MSG;
|
||||||
|
out->msg_content = b_strdup(content->msg_content);
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_diag_c_snippet *diag_snippet_create(
|
||||||
|
unsigned long first_line, unsigned long last_line,
|
||||||
|
const struct mie_diag_amendment *amendments, size_t nr_amendments,
|
||||||
|
const struct mie_diag_highlight *highlights, size_t nr_highlights)
|
||||||
|
{
|
||||||
|
struct mie_diag_c_snippet *out = malloc(sizeof *out);
|
||||||
|
if (!out) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(out, 0x0, sizeof *out);
|
||||||
|
|
||||||
|
out->s_base.c_type = MIE_DIAG_COMPONENT_SNIPPET;
|
||||||
|
out->s_first_line = first_line;
|
||||||
|
out->s_last_line = last_line;
|
||||||
|
|
||||||
|
out->s_nr_amendments = nr_amendments;
|
||||||
|
out->s_amendments = calloc(nr_amendments, sizeof *amendments);
|
||||||
|
if (!out->s_amendments) {
|
||||||
|
free(out);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
memcpy(out->s_amendments, amendments, nr_amendments * sizeof *amendments);
|
||||||
|
|
||||||
|
out->s_nr_highlights = nr_highlights;
|
||||||
|
out->s_highlights = calloc(nr_highlights, sizeof *highlights);
|
||||||
|
if (!out->s_highlights) {
|
||||||
|
free(out->s_amendments);
|
||||||
|
free(out);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
memcpy(out->s_highlights, highlights, nr_highlights * sizeof *highlights);
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mie_diag_set_location(
|
||||||
|
struct mie_diag *diag, unsigned long row, unsigned long col)
|
||||||
|
{
|
||||||
|
diag->diag_loc.c_row = row;
|
||||||
|
diag->diag_loc.c_row = col;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mie_diag_push_msg(
|
||||||
|
struct mie_diag *diag, struct mie_ctx *ctx, const char *dialect_name,
|
||||||
|
unsigned long msg, ...)
|
||||||
|
{
|
||||||
|
struct mie_dialect *dialect = mie_ctx_get_dialect(ctx, dialect_name);
|
||||||
|
if (!dialect) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg >= dialect->d_nr_diag_msgs) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const struct mie_diag_msg *msg_info = &dialect->d_diag_msgs[msg];
|
||||||
|
|
||||||
|
struct mie_diag_c_msg *c_msg = diag_msg_create(msg_info);
|
||||||
|
if (!c_msg) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_queue_push_back(&diag->diag_components, &c_msg->msg_base.c_entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mie_diag_push_snippet(
|
||||||
|
struct mie_diag *diag, unsigned long first_line, unsigned long last_line,
|
||||||
|
const struct mie_diag_amendment *amendments, size_t nr_amendments,
|
||||||
|
const struct mie_diag_highlight *highlights, size_t nr_highlights)
|
||||||
|
{
|
||||||
|
struct mie_diag_c_snippet *c_snippet = diag_snippet_create(
|
||||||
|
first_line, last_line, amendments, nr_amendments, highlights,
|
||||||
|
nr_highlights);
|
||||||
|
if (!c_snippet) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_queue_push_back(&diag->diag_components, &c_snippet->s_base.c_entry);
|
||||||
|
}
|
||||||
6
mie/diag/diag.h
Normal file
6
mie/diag/diag.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef _DIAG_DIAG_H_
|
||||||
|
#define _DIAG_DIAG_H_
|
||||||
|
|
||||||
|
#include <blue/core/queue.h>
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#include <mie/ctx.h>
|
#include <mie/ctx.h>
|
||||||
|
#include <mie/diag/class.h>
|
||||||
|
#include <mie/diag/msg.h>
|
||||||
#include <mie/dialect/builtin.h>
|
#include <mie/dialect/builtin.h>
|
||||||
#include <mie/dialect/dialect.h>
|
#include <mie/dialect/dialect.h>
|
||||||
#include <mie/macros.h>
|
#include <mie/macros.h>
|
||||||
@@ -81,9 +83,14 @@ struct mie_attribute *mie_ctx_get_index(struct mie_ctx *ctx, size_t val)
|
|||||||
dialect->ctx_indices, ctx, val);
|
dialect->ctx_indices, ctx, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MIE_DIAG_CLASS_LIST_EXTERN(mie_builtin_diag);
|
||||||
|
MIE_DIAG_MSG_LIST_EXTERN(mie_builtin_msg);
|
||||||
|
|
||||||
MIE_DIALECT_BEGIN(mie_builtin, struct builtin_dialect, "builtin")
|
MIE_DIALECT_BEGIN(mie_builtin, struct builtin_dialect, "builtin")
|
||||||
MIE_DIALECT_INIT(init);
|
MIE_DIALECT_INIT(init);
|
||||||
MIE_DIALECT_CLEANUP(cleanup);
|
MIE_DIALECT_CLEANUP(cleanup);
|
||||||
|
MIE_DIALECT_DIAG_CLASS_LIST(mie_builtin_diag);
|
||||||
|
MIE_DIALECT_DIAG_MSG_LIST(mie_builtin_msg);
|
||||||
MIE_DIALECT_ADD_TRAIT(mie_builtin_isolated_from_above);
|
MIE_DIALECT_ADD_TRAIT(mie_builtin_isolated_from_above);
|
||||||
MIE_DIALECT_ADD_TRAIT(mie_builtin_symbol_table);
|
MIE_DIALECT_ADD_TRAIT(mie_builtin_symbol_table);
|
||||||
MIE_DIALECT_ADD_TYPE(mie_builtin_int);
|
MIE_DIALECT_ADD_TYPE(mie_builtin_int);
|
||||||
|
|||||||
36
mie/dialect/builtin/diag.c
Normal file
36
mie/dialect/builtin/diag.c
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include <mie/diag/class.h>
|
||||||
|
#include <mie/diag/msg.h>
|
||||||
|
#include <mie/dialect/builtin.h>
|
||||||
|
#include <mie/macros.h>
|
||||||
|
|
||||||
|
#define MIE_DIAG_CLASS_PREFIX MIE_BUILTIN_E
|
||||||
|
#define MIE_DIAG_MSG_PREFIX MIE_BUILTIN_MSG
|
||||||
|
|
||||||
|
MIE_DIAG_CLASS_LIST_BEGIN(mie_builtin_diag)
|
||||||
|
MIE_DIAG_CLASS(UNRECOGNISED_TOKEN, ERROR, "Unrecognised token")
|
||||||
|
MIE_DIAG_CLASS(UNRESOLVED_VALUE, ERROR, "Unresolved value")
|
||||||
|
MIE_DIAG_CLASS(UNRESOLVED_SUCCESSOR, ERROR, "Unresolved successor")
|
||||||
|
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(
|
||||||
|
CANNOT_FIND_BLOCK,
|
||||||
|
"cannot find a block with this name in this region.")
|
||||||
|
MIE_DIAG_MSG(
|
||||||
|
VALUE_DEFINED_IN_NON_DOMINANT_BLOCK,
|
||||||
|
"this value cannot be referenced here, as it is defined in a "
|
||||||
|
"block that does not dominate this block.")
|
||||||
|
MIE_DIAG_MSG(
|
||||||
|
VALUE_DEFINED_AFTER_USE,
|
||||||
|
"this value cannot be referenced here, as it is not defined "
|
||||||
|
"until later in this block.")
|
||||||
|
MIE_DIAG_MSG(
|
||||||
|
VALUE_DEFINED_OUTSIDE_ISOLATED_REGION,
|
||||||
|
"this value cannot be referenced here, as this region is "
|
||||||
|
"isolated from above, and the definition for this value is "
|
||||||
|
"outside of this region.")
|
||||||
|
MIE_DIAG_MSG(
|
||||||
|
VALUE_DEFINED_IN_BLOCK, "the value is defined in this block.")
|
||||||
|
MIE_DIAG_MSG_LIST_END(mie_builtin_msg)
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <mie/macros.h>
|
#include <mie/macros.h>
|
||||||
|
|
||||||
MIE_DIALECT_BEGIN(mie_func, struct mie_dialect, "func")
|
MIE_DIALECT_BEGIN(mie_func, struct mie_dialect, "func")
|
||||||
|
MIE_DIALECT_ADD_TRAIT(mie_func_function_like);
|
||||||
MIE_DIALECT_ADD_OP(mie_func_func);
|
MIE_DIALECT_ADD_OP(mie_func_func);
|
||||||
MIE_DIALECT_ADD_OP(mie_func_return);
|
MIE_DIALECT_ADD_OP(mie_func_return);
|
||||||
MIE_DIALECT_END()
|
MIE_DIALECT_END()
|
||||||
|
|||||||
@@ -130,6 +130,7 @@ MIE_OP_DEFINITION_BEGIN(mie_func_func, "func")
|
|||||||
MIE_OP_DEFINITION_PRINT(print);
|
MIE_OP_DEFINITION_PRINT(print);
|
||||||
MIE_OP_DEFINITION_PARSE(parse);
|
MIE_OP_DEFINITION_PARSE(parse);
|
||||||
MIE_OP_DEFINITION_TRAIT("builtin", "isolated-from-above");
|
MIE_OP_DEFINITION_TRAIT("builtin", "isolated-from-above");
|
||||||
|
MIE_OP_DEFINITION_TRAIT("func", "function-like");
|
||||||
MIE_OP_INTERFACE_BEGIN("builtin", "symbol", struct mie_symbol)
|
MIE_OP_INTERFACE_BEGIN("builtin", "symbol", struct mie_symbol)
|
||||||
MIE_OP_INTERFACE_FUNC(sym_get_name) = NULL;
|
MIE_OP_INTERFACE_FUNC(sym_get_name) = NULL;
|
||||||
MIE_OP_INTERFACE_END()
|
MIE_OP_INTERFACE_END()
|
||||||
|
|||||||
23
mie/dialect/func/trait/function-like.c
Normal file
23
mie/dialect/func/trait/function-like.c
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#include <mie/dialect/dialect.h>
|
||||||
|
#include <mie/macros.h>
|
||||||
|
#include <mie/trait/trait-definition.h>
|
||||||
|
#include <mie/trait/trait.h>
|
||||||
|
|
||||||
|
static enum mie_status validate(
|
||||||
|
const struct mie_trait_definition *trait_def,
|
||||||
|
const struct mie_trait *trait, const struct mie_trait_target *target)
|
||||||
|
{
|
||||||
|
return MIE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* func.function-like trait:
|
||||||
|
* the op behaves like a func.func. this means that it:
|
||||||
|
* a) has a single region
|
||||||
|
* b) that region has one or more blocks representing the body of a function.
|
||||||
|
* c) the op has sym_name, function_type, and other function-related attributes.
|
||||||
|
*/
|
||||||
|
MIE_TRAIT_DEFINITION_BEGIN(mie_func_function_like, "function-like")
|
||||||
|
MIE_TRAIT_DEFINITION_TARGETS(MIE_TRAIT_TARGET_OP);
|
||||||
|
MIE_TRAIT_DEFINITION_STRUCT(struct mie_trait);
|
||||||
|
MIE_TRAIT_DEFINITION_VALIDATE(validate);
|
||||||
|
MIE_TRAIT_DEFINITION_END()
|
||||||
@@ -7,4 +7,5 @@
|
|||||||
|
|
||||||
MIE_DIALECT_BEGIN(mie_memref, struct mie_dialect, "memref")
|
MIE_DIALECT_BEGIN(mie_memref, struct mie_dialect, "memref")
|
||||||
MIE_DIALECT_ADD_TYPE(mie_memref_memref);
|
MIE_DIALECT_ADD_TYPE(mie_memref_memref);
|
||||||
|
MIE_DIALECT_ADD_OP(mie_memref_load);
|
||||||
MIE_DIALECT_END()
|
MIE_DIALECT_END()
|
||||||
|
|||||||
18
mie/dialect/memref/op/load.c
Normal file
18
mie/dialect/memref/op/load.c
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#include <mie/ctx.h>
|
||||||
|
#include <mie/ir/op-definition.h>
|
||||||
|
#include <mie/macros.h>
|
||||||
|
|
||||||
|
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
||||||
|
{
|
||||||
|
return MIE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
||||||
|
{
|
||||||
|
return MIE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
MIE_OP_DEFINITION_BEGIN(mie_memref_load, "load")
|
||||||
|
MIE_OP_DEFINITION_PRINT(print);
|
||||||
|
MIE_OP_DEFINITION_PARSE(parse);
|
||||||
|
MIE_OP_DEFINITION_END()
|
||||||
@@ -7,7 +7,10 @@
|
|||||||
|
|
||||||
struct mie_op;
|
struct mie_op;
|
||||||
struct mie_pass;
|
struct mie_pass;
|
||||||
|
struct mie_diag;
|
||||||
|
struct mie_file_cell;
|
||||||
struct mie_int_cache;
|
struct mie_int_cache;
|
||||||
|
struct mie_line_source;
|
||||||
struct mie_index_cache;
|
struct mie_index_cache;
|
||||||
struct mie_string_cache;
|
struct mie_string_cache;
|
||||||
struct mie_attribute_map;
|
struct mie_attribute_map;
|
||||||
@@ -31,6 +34,8 @@ struct mie_ctx {
|
|||||||
struct mie_id_map ctx_attributes;
|
struct mie_id_map ctx_attributes;
|
||||||
/* map of struct mie_pass_definition */
|
/* map of struct mie_pass_definition */
|
||||||
struct mie_id_map ctx_passes;
|
struct mie_id_map ctx_passes;
|
||||||
|
/* queue of struct mie_diag */
|
||||||
|
b_queue ctx_diag;
|
||||||
};
|
};
|
||||||
|
|
||||||
MIE_API struct mie_ctx *mie_ctx_create(void);
|
MIE_API struct mie_ctx *mie_ctx_create(void);
|
||||||
@@ -38,7 +43,6 @@ MIE_API void mie_ctx_destroy(struct mie_ctx *ctx);
|
|||||||
|
|
||||||
MIE_API struct mie_op *mie_ctx_create_op(
|
MIE_API struct mie_op *mie_ctx_create_op(
|
||||||
const struct mie_ctx *ctx, const char *dialect, const char *op);
|
const struct mie_ctx *ctx, const char *dialect, const char *op);
|
||||||
MIE_API bool mie_ctx_resolve_op(const struct mie_ctx *ctx, struct mie_op *op);
|
|
||||||
|
|
||||||
MIE_API struct mie_dialect *mie_ctx_get_dialect(
|
MIE_API struct mie_dialect *mie_ctx_get_dialect(
|
||||||
const struct mie_ctx *ctx, const char *name);
|
const struct mie_ctx *ctx, const char *name);
|
||||||
@@ -73,4 +77,11 @@ MIE_API enum mie_status mie_ctx_get_pass(
|
|||||||
struct mie_ctx *ctx, const char *name,
|
struct mie_ctx *ctx, const char *name,
|
||||||
const struct mie_attribute_map *args, struct mie_pass **out);
|
const struct mie_attribute_map *args, struct mie_pass **out);
|
||||||
|
|
||||||
|
MIE_API struct mie_diag *mie_ctx_push_diag(
|
||||||
|
struct mie_ctx *ctx, struct mie_line_source *src,
|
||||||
|
const struct mie_file_cell *loc, const char *dialect,
|
||||||
|
unsigned long diag_class);
|
||||||
|
|
||||||
|
MIE_API struct mie_diag *mie_ctx_pop_diag(struct mie_ctx *ctx);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
36
mie/include/mie/diag/amendment.h
Normal file
36
mie/include/mie/diag/amendment.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef MIE_DIAG_AMENDMENT_H_
|
||||||
|
#define MIE_DIAG_AMENDMENT_H_
|
||||||
|
|
||||||
|
#include <mie/parse/file-span.h>
|
||||||
|
|
||||||
|
enum mie_diag_amendment_type {
|
||||||
|
MIE_DIAG_AMENDMENT_NONE = 0,
|
||||||
|
MIE_DIAG_AMENDMENT_ADD,
|
||||||
|
MIE_DIAG_AMENDMENT_REMOVE,
|
||||||
|
MIE_DIAG_AMENDMENT_REPLACE,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mie_diag_amendment {
|
||||||
|
enum mie_diag_amendment_type a_type;
|
||||||
|
unsigned long __x;
|
||||||
|
|
||||||
|
union {
|
||||||
|
struct {
|
||||||
|
struct mie_file_cell a_loc;
|
||||||
|
char *a_str;
|
||||||
|
} a_add;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
struct mie_file_cell a_loc;
|
||||||
|
unsigned long a_length;
|
||||||
|
} a_remove;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
struct mie_file_cell a_loc;
|
||||||
|
unsigned long a_length;
|
||||||
|
char *a_str;
|
||||||
|
} a_replace;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
18
mie/include/mie/diag/class.h
Normal file
18
mie/include/mie/diag/class.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef MIE_DIAG_CLASS_H_
|
||||||
|
#define MIE_DIAG_CLASS_H_
|
||||||
|
|
||||||
|
enum mie_diag_class_type {
|
||||||
|
MIE_DIAG_CLASS_NONE = 0,
|
||||||
|
MIE_DIAG_CLASS_HINT,
|
||||||
|
MIE_DIAG_CLASS_WARNING,
|
||||||
|
MIE_DIAG_CLASS_ERROR,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mie_diag_class {
|
||||||
|
unsigned int c_id;
|
||||||
|
const char *c_id_str_short, *c_id_str_long;
|
||||||
|
enum mie_diag_class_type c_type;
|
||||||
|
const char *c_title;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
34
mie/include/mie/diag/component.h
Normal file
34
mie/include/mie/diag/component.h
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#ifndef MIE_DIAG_COMPONENT_H_
|
||||||
|
#define MIE_DIAG_COMPONENT_H_
|
||||||
|
|
||||||
|
#include <blue/core/queue.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
enum mie_diag_component_type {
|
||||||
|
MIE_DIAG_COMPONENT_NONE = 0,
|
||||||
|
MIE_DIAG_COMPONENT_MSG,
|
||||||
|
MIE_DIAG_COMPONENT_SNIPPET,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mie_diag_component {
|
||||||
|
enum mie_diag_component_type c_type;
|
||||||
|
b_queue_entry c_entry;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mie_diag_c_snippet {
|
||||||
|
struct mie_diag_component s_base;
|
||||||
|
unsigned long s_first_line, s_last_line;
|
||||||
|
|
||||||
|
struct mie_diag_amendment *s_amendments;
|
||||||
|
size_t s_nr_amendments;
|
||||||
|
|
||||||
|
struct mie_diag_highlight *s_highlights;
|
||||||
|
size_t s_nr_highlights;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mie_diag_c_msg {
|
||||||
|
struct mie_diag_component msg_base;
|
||||||
|
char *msg_content;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
39
mie/include/mie/diag/diag.h
Normal file
39
mie/include/mie/diag/diag.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#ifndef MIE_DIAG_DIAG_H_
|
||||||
|
#define MIE_DIAG_DIAG_H_
|
||||||
|
|
||||||
|
#include <blue/core/queue.h>
|
||||||
|
#include <mie/parse/file-span.h>
|
||||||
|
|
||||||
|
struct mie_ctx;
|
||||||
|
struct mie_diag_class;
|
||||||
|
struct mie_diag_amendment;
|
||||||
|
struct mie_diag_highlight;
|
||||||
|
|
||||||
|
enum mie_diag_type {
|
||||||
|
MIE_DIAG_NONE = 0,
|
||||||
|
MIE_DIAG_HINT,
|
||||||
|
MIE_DIAG_WARNING,
|
||||||
|
MIE_DIAG_ERROR,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mie_diag {
|
||||||
|
struct mie_dialect *diag_parent;
|
||||||
|
const struct mie_diag_class *diag_class;
|
||||||
|
|
||||||
|
struct mie_line_source *diag_src;
|
||||||
|
struct mie_file_cell diag_loc;
|
||||||
|
b_queue_entry diag_entry;
|
||||||
|
b_queue diag_components;
|
||||||
|
};
|
||||||
|
|
||||||
|
MIE_API void mie_diag_set_location(
|
||||||
|
struct mie_diag *diag, unsigned long row, unsigned long col);
|
||||||
|
MIE_API void mie_diag_push_msg(
|
||||||
|
struct mie_diag *diag, struct mie_ctx *ctx, const char *dialect,
|
||||||
|
unsigned long msg, ...);
|
||||||
|
MIE_API void mie_diag_push_snippet(
|
||||||
|
struct mie_diag *diag, unsigned long first_line, unsigned long last_line,
|
||||||
|
const struct mie_diag_amendment *amendmends, size_t nr_amendments,
|
||||||
|
const struct mie_diag_highlight *highlights, size_t nr_highlights);
|
||||||
|
|
||||||
|
#endif
|
||||||
18
mie/include/mie/diag/highlight.h
Normal file
18
mie/include/mie/diag/highlight.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef MIE_DIAG_HIGHLIGHT_H_
|
||||||
|
#define MIE_DIAG_HIGHLIGHT_H_
|
||||||
|
|
||||||
|
#include <mie/parse/file-span.h>
|
||||||
|
|
||||||
|
enum mie_diag_highlight_type {
|
||||||
|
MIE_DIAG_HIGHLIGHT_NONE = 0,
|
||||||
|
MIE_DIAG_HIGHLIGHT_HINT,
|
||||||
|
MIE_DIAG_HIGHLIGHT_WARNING,
|
||||||
|
MIE_DIAG_HIGHLIGHT_ERROR,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mie_diag_highlight {
|
||||||
|
enum mie_diag_highlight_type hl_type;
|
||||||
|
struct mie_file_span hl_span;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
10
mie/include/mie/diag/msg.h
Normal file
10
mie/include/mie/diag/msg.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef MIE_DIAG_MSG_H_
|
||||||
|
#define MIE_DIAG_MSG_H_
|
||||||
|
|
||||||
|
struct mie_diag_msg {
|
||||||
|
unsigned int msg_id;
|
||||||
|
const char *msg_id_str_short, *msg_id_str_long;
|
||||||
|
const char *msg_content;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
0
mie/include/mie/diag/snippet.h
Normal file
0
mie/include/mie/diag/snippet.h
Normal file
@@ -15,6 +15,22 @@ struct mie_op;
|
|||||||
struct mie_int_type;
|
struct mie_int_type;
|
||||||
struct mie_float_type;
|
struct mie_float_type;
|
||||||
|
|
||||||
|
enum mie_builtin_diag {
|
||||||
|
MIE_BUILTIN_E_UNRECOGNISED_TOKEN,
|
||||||
|
MIE_BUILTIN_E_UNRESOLVED_VALUE,
|
||||||
|
MIE_BUILTIN_E_UNRESOLVED_SUCCESSOR,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum mie_builtin_msg {
|
||||||
|
MIE_BUILTIN_MSG_UNRECOGNISED_TOKEN,
|
||||||
|
MIE_BUILTIN_MSG_UNRESOLVED_VALUE,
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
|
||||||
enum mie_float_width {
|
enum mie_float_width {
|
||||||
MIE_FLOAT_32 = 32,
|
MIE_FLOAT_32 = 32,
|
||||||
MIE_FLOAT_64 = 64,
|
MIE_FLOAT_64 = 64,
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ struct mie_op_definition;
|
|||||||
struct mie_type_definition;
|
struct mie_type_definition;
|
||||||
struct mie_trait_definition;
|
struct mie_trait_definition;
|
||||||
|
|
||||||
|
struct mie_diag_class;
|
||||||
|
struct mie_diag_msg;
|
||||||
|
|
||||||
struct mie_dialect {
|
struct mie_dialect {
|
||||||
mie_id d_id;
|
mie_id d_id;
|
||||||
char *d_name;
|
char *d_name;
|
||||||
@@ -27,6 +30,12 @@ struct mie_dialect {
|
|||||||
struct mie_id_map d_attributes;
|
struct mie_id_map d_attributes;
|
||||||
/* map of struct mie_interface_definition */
|
/* map of struct mie_interface_definition */
|
||||||
struct mie_id_map d_interfaces;
|
struct mie_id_map d_interfaces;
|
||||||
|
/* array of mie_diag_class */
|
||||||
|
struct mie_diag_class *d_diag_classes;
|
||||||
|
size_t d_nr_diag_classes;
|
||||||
|
/* array of mie_diag_msg */
|
||||||
|
struct mie_diag_msg *d_diag_msgs;
|
||||||
|
size_t d_nr_diag_msgs;
|
||||||
|
|
||||||
enum mie_status (*d_cleanup)(struct mie_dialect *);
|
enum mie_status (*d_cleanup)(struct mie_dialect *);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,13 +6,26 @@
|
|||||||
#include <mie/name.h>
|
#include <mie/name.h>
|
||||||
#include <mie/vector.h>
|
#include <mie/vector.h>
|
||||||
|
|
||||||
|
#define MIE_BLOCK_ID_INVALID ((size_t)0)
|
||||||
|
|
||||||
struct mie_op;
|
struct mie_op;
|
||||||
struct mie_register;
|
struct mie_register;
|
||||||
|
|
||||||
|
struct mie_block_predecessor {
|
||||||
|
struct mie_block *p_block;
|
||||||
|
};
|
||||||
|
|
||||||
struct mie_block {
|
struct mie_block {
|
||||||
struct mie_name b_name;
|
struct mie_name b_name;
|
||||||
struct mie_region *b_parent;
|
struct mie_region *b_parent;
|
||||||
|
|
||||||
|
/* immediate predecessor blocks */
|
||||||
|
MIE_VECTOR_DECLARE(struct mie_block_predecessor, b_ipred);
|
||||||
|
|
||||||
/* used by struct mie_region */
|
/* used by struct mie_region */
|
||||||
|
unsigned int b_id;
|
||||||
|
struct mie_block *b_idom;
|
||||||
|
// struct mie_block *b_sdom, *b_dfs_parent;
|
||||||
b_queue_entry b_entry;
|
b_queue_entry b_entry;
|
||||||
|
|
||||||
MIE_VECTOR_DECLARE(struct mie_register, b_params);
|
MIE_VECTOR_DECLARE(struct mie_register, b_params);
|
||||||
@@ -29,9 +42,33 @@ MIE_API struct mie_op *mie_block_get_next_op(
|
|||||||
const struct mie_block *block, const struct mie_op *after);
|
const struct mie_block *block, const struct mie_op *after);
|
||||||
MIE_API struct mie_op *mie_block_get_last_op(const struct mie_block *block);
|
MIE_API struct mie_op *mie_block_get_last_op(const struct mie_block *block);
|
||||||
|
|
||||||
|
MIE_API struct mie_op *mie_block_get_terminator(const struct mie_block *block);
|
||||||
|
|
||||||
|
MIE_API struct mie_op_successor *mie_block_get_first_successor(
|
||||||
|
const struct mie_block *block);
|
||||||
|
MIE_API struct mie_op_successor *mie_block_get_prev_successor(
|
||||||
|
const struct mie_block *block, const struct mie_op_successor *before);
|
||||||
|
MIE_API struct mie_op_successor *mie_block_get_next_successor(
|
||||||
|
const struct mie_block *block, const struct mie_op_successor *after);
|
||||||
|
MIE_API struct mie_op_successor *mie_block_get_last_successor(
|
||||||
|
const struct mie_block *block);
|
||||||
|
|
||||||
|
MIE_API struct mie_block_predecessor *mie_block_get_first_predecessor(
|
||||||
|
const struct mie_block *block);
|
||||||
|
MIE_API struct mie_block_predecessor *mie_block_get_prev_predecessor(
|
||||||
|
const struct mie_block *block, const struct mie_block_predecessor *before);
|
||||||
|
MIE_API struct mie_block_predecessor *mie_block_get_next_predecessor(
|
||||||
|
const struct mie_block *block, const struct mie_block_predecessor *after);
|
||||||
|
MIE_API struct mie_block_predecessor *mie_block_get_last_predecessor(
|
||||||
|
const struct mie_block *block);
|
||||||
|
|
||||||
MIE_API struct mie_op *mie_block_add_op(struct mie_block *block);
|
MIE_API struct mie_op *mie_block_add_op(struct mie_block *block);
|
||||||
MIE_API struct mie_op *mie_block_add_op_after(
|
MIE_API struct mie_op *mie_block_add_op_after(
|
||||||
struct mie_block *block, struct mie_op *after);
|
struct mie_block *block, struct mie_op *after);
|
||||||
MIE_API struct mie_register *mie_block_add_param(struct mie_block *block);
|
MIE_API struct mie_register *mie_block_add_param(struct mie_block *block);
|
||||||
|
|
||||||
|
MIE_API struct mie_register *mie_block_find_register(
|
||||||
|
const struct mie_block *block, const char *name,
|
||||||
|
const struct mie_op *start_point);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -9,8 +9,8 @@
|
|||||||
#include <mie/name.h>
|
#include <mie/name.h>
|
||||||
#include <mie/parse/file-span.h>
|
#include <mie/parse/file-span.h>
|
||||||
|
|
||||||
|
struct mie_ctx;
|
||||||
struct mie_type;
|
struct mie_type;
|
||||||
|
|
||||||
struct mie_value;
|
struct mie_value;
|
||||||
struct mie_block;
|
struct mie_block;
|
||||||
struct mie_region;
|
struct mie_region;
|
||||||
@@ -37,6 +37,7 @@ struct mie_op_arg {
|
|||||||
|
|
||||||
struct mie_op_successor {
|
struct mie_op_successor {
|
||||||
enum mie_op_flags s_flags;
|
enum mie_op_flags s_flags;
|
||||||
|
struct mie_file_span s_name_span;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
/* only valid if F_SUCCESSOR_RESOLVED is set in s_flags; */
|
/* only valid if F_SUCCESSOR_RESOLVED is set in s_flags; */
|
||||||
@@ -53,8 +54,8 @@ struct mie_op {
|
|||||||
/* used by struct mie_block */
|
/* used by struct mie_block */
|
||||||
b_queue_entry op_entry;
|
b_queue_entry op_entry;
|
||||||
|
|
||||||
/* these pointers are only valid if the F_OP_RESOLVED flag is set */
|
struct mie_line_source *op_src;
|
||||||
const struct mie_dialect *op_dialect;
|
/* this pointer is only valid if the F_OP_RESOLVED flag is set */
|
||||||
const struct mie_op_definition *op_info;
|
const struct mie_op_definition *op_info;
|
||||||
|
|
||||||
struct mie_block *op_container;
|
struct mie_block *op_container;
|
||||||
@@ -76,6 +77,12 @@ MIE_API void mie_op_destroy(struct mie_op *op);
|
|||||||
MIE_API void mie_op_init(struct mie_op *op);
|
MIE_API void mie_op_init(struct mie_op *op);
|
||||||
MIE_API void mie_op_cleanup(struct mie_op *op);
|
MIE_API void mie_op_cleanup(struct mie_op *op);
|
||||||
|
|
||||||
|
MIE_API size_t mie_op_get_name(const struct mie_op *op, char *out, size_t max);
|
||||||
|
|
||||||
|
MIE_API bool mie_op_resolve_self(struct mie_op *op, struct mie_ctx *ctx);
|
||||||
|
MIE_API bool mie_op_resolve_args(struct mie_op *op, struct mie_ctx *ctx);
|
||||||
|
MIE_API bool mie_op_resolve_successors(struct mie_op *op, struct mie_ctx *ctx);
|
||||||
|
|
||||||
MIE_API struct mie_op_arg *mie_op_add_arg(struct mie_op *op);
|
MIE_API struct mie_op_arg *mie_op_add_arg(struct mie_op *op);
|
||||||
MIE_API struct mie_register *mie_op_add_result(
|
MIE_API struct mie_register *mie_op_add_result(
|
||||||
struct mie_op *op, const struct mie_type *ty);
|
struct mie_op *op, const struct mie_type *ty);
|
||||||
@@ -100,6 +107,8 @@ MIE_API struct mie_region *mie_op_get_next_region(
|
|||||||
MIE_API struct mie_region *mie_op_get_last_region(const struct mie_op *op);
|
MIE_API struct mie_region *mie_op_get_last_region(const struct mie_op *op);
|
||||||
|
|
||||||
MIE_API struct mie_register *mie_op_get_arg(const struct mie_op *op, size_t index);
|
MIE_API struct mie_register *mie_op_get_arg(const struct mie_op *op, size_t index);
|
||||||
|
MIE_API struct mie_register *mie_op_get_result_with_name(
|
||||||
|
const struct mie_op *op, const char *name);
|
||||||
|
|
||||||
MIE_API struct mie_op *mie_op_get_first_child_op(const struct mie_op *op);
|
MIE_API struct mie_op *mie_op_get_first_child_op(const struct mie_op *op);
|
||||||
MIE_API struct mie_op *mie_op_get_last_child_op(const struct mie_op *op);
|
MIE_API struct mie_op *mie_op_get_last_child_op(const struct mie_op *op);
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
#ifndef MIE_IR_REGION_H_
|
#ifndef MIE_IR_REGION_H_
|
||||||
#define MIE_IR_REGION_H_
|
#define MIE_IR_REGION_H_
|
||||||
|
|
||||||
|
#include <blue/core/btree.h>
|
||||||
#include <blue/core/queue.h>
|
#include <blue/core/queue.h>
|
||||||
#include <mie/misc.h>
|
#include <mie/misc.h>
|
||||||
#include <mie/vector.h>
|
#include <mie/vector.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
struct mie_op;
|
struct mie_op;
|
||||||
|
struct mie_ctx;
|
||||||
struct mie_block;
|
struct mie_block;
|
||||||
|
|
||||||
struct mie_region {
|
struct mie_region {
|
||||||
@@ -16,6 +18,9 @@ struct mie_region {
|
|||||||
b_queue_entry r_entry;
|
b_queue_entry r_entry;
|
||||||
/* queue of struct mie_block */
|
/* queue of struct mie_block */
|
||||||
b_queue r_blocks;
|
b_queue r_blocks;
|
||||||
|
/* bst of struct mie_block, as numbered by a depth-first search.
|
||||||
|
* this ordering is recalculated by mie_region_refresh_dominance */
|
||||||
|
b_btree r_blocks_s;
|
||||||
};
|
};
|
||||||
|
|
||||||
MIE_API struct mie_block *mie_region_get_first_block(
|
MIE_API struct mie_block *mie_region_get_first_block(
|
||||||
@@ -29,5 +34,22 @@ MIE_API struct mie_block *mie_region_get_last_block(const struct mie_region *reg
|
|||||||
MIE_API struct mie_block *mie_region_add_block(struct mie_region *region);
|
MIE_API struct mie_block *mie_region_add_block(struct mie_region *region);
|
||||||
MIE_API struct mie_block *mie_region_add_block_after(
|
MIE_API struct mie_block *mie_region_add_block_after(
|
||||||
struct mie_region *region, struct mie_block *after);
|
struct mie_region *region, struct mie_block *after);
|
||||||
|
MIE_API struct mie_block *mie_region_find_block(
|
||||||
|
const struct mie_region *region, const char *name);
|
||||||
|
MIE_API struct mie_register *mie_region_find_register(
|
||||||
|
const struct mie_region *region, const char *name);
|
||||||
|
|
||||||
|
/* calculate the dominance-tree of all blocks in a region.
|
||||||
|
* this function populates the following attributes of every block in the
|
||||||
|
* region:
|
||||||
|
* b_id
|
||||||
|
* b_node
|
||||||
|
* b_parent
|
||||||
|
* as well as the following attributes of the region itself:
|
||||||
|
* r_blocks_s
|
||||||
|
* only top-level blocks and ops are considered by this function. if an op
|
||||||
|
* within the region itself contains one/more child regions, these are ignored.
|
||||||
|
*/
|
||||||
|
MIE_API void mie_region_refresh_dominance(struct mie_region *region);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ struct mie_register_use {
|
|||||||
|
|
||||||
struct mie_register {
|
struct mie_register {
|
||||||
enum mie_register_flags reg_flags;
|
enum mie_register_flags reg_flags;
|
||||||
|
struct mie_file_span reg_span;
|
||||||
union {
|
union {
|
||||||
/* only valid if F_VIRTUAL is set. */
|
/* only valid if F_VIRTUAL is set. */
|
||||||
struct mie_name reg_name;
|
struct mie_name reg_name;
|
||||||
|
|||||||
14
mie/include/mie/ir/resolve.h
Normal file
14
mie/include/mie/ir/resolve.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef MIE_IR_RESOLVE_H_
|
||||||
|
#define MIE_IR_RESOLVE_H_
|
||||||
|
|
||||||
|
#include <mie/misc.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
struct mie_op;
|
||||||
|
struct mie_ctx;
|
||||||
|
|
||||||
|
MIE_API bool mie_resolve_op_self(struct mie_op *op, struct mie_ctx *ctx);
|
||||||
|
MIE_API bool mie_resolve_op_args(struct mie_op *op, struct mie_ctx *ctx);
|
||||||
|
MIE_API bool mie_resolve_op_successors(struct mie_op *op, struct mie_ctx *ctx);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -6,18 +6,18 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define MIE_DIALECT_BEGIN(func_prefix, c_struct, dialect_name) \
|
#define MIE_DIALECT_BEGIN(func_prefix, c_struct, dialect_name) \
|
||||||
struct mie_dialect *func_prefix##_dialect_create(struct mie_ctx *ctx) \
|
struct mie_dialect* func_prefix##_dialect_create(struct mie_ctx* ctx) \
|
||||||
{ \
|
{ \
|
||||||
struct mie_dialect *self = mie_dialect_create( \
|
struct mie_dialect* self = mie_dialect_create( \
|
||||||
ctx, dialect_name, sizeof(c_struct)); \
|
ctx, dialect_name, sizeof(c_struct)); \
|
||||||
if (!self) { \
|
if (!self) { \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
} \
|
} \
|
||||||
struct mie_op_definition *op = NULL; \
|
struct mie_op_definition* op = NULL; \
|
||||||
struct mie_type_definition *type = NULL; \
|
struct mie_type_definition* type = NULL; \
|
||||||
struct mie_trait_definition *trait = NULL; \
|
struct mie_trait_definition* trait = NULL; \
|
||||||
struct mie_attribute_definition *attribute = NULL; \
|
struct mie_attribute_definition* attribute = NULL; \
|
||||||
struct mie_interface_definition *interface = NULL;
|
struct mie_interface_definition* interface = NULL;
|
||||||
|
|
||||||
#define MIE_DIALECT_END() \
|
#define MIE_DIALECT_END() \
|
||||||
return self; \
|
return self; \
|
||||||
@@ -25,25 +25,31 @@
|
|||||||
|
|
||||||
#define MIE_DIALECT_INIT(func) func(self)
|
#define MIE_DIALECT_INIT(func) func(self)
|
||||||
#define MIE_DIALECT_CLEANUP(func) self->d_cleanup = (func)
|
#define MIE_DIALECT_CLEANUP(func) self->d_cleanup = (func)
|
||||||
|
#define MIE_DIALECT_DIAG_CLASS_LIST(list) \
|
||||||
|
self->d_diag_classes = (list); \
|
||||||
|
self->d_nr_diag_classes = (list##_count)
|
||||||
|
#define MIE_DIALECT_DIAG_MSG_LIST(list) \
|
||||||
|
self->d_diag_msgs = (list); \
|
||||||
|
self->d_nr_diag_msgs = (list##_count)
|
||||||
#define MIE_DIALECT_ADD_OP(op_id) \
|
#define MIE_DIALECT_ADD_OP(op_id) \
|
||||||
extern struct mie_op_definition *op_id##_op_create( \
|
extern struct mie_op_definition* op_id##_op_create( \
|
||||||
struct mie_dialect *, struct mie_ctx *); \
|
struct mie_dialect*, struct mie_ctx*); \
|
||||||
op = op_id##_op_create(self, ctx)
|
op = op_id##_op_create(self, ctx)
|
||||||
#define MIE_DIALECT_ADD_TYPE(type_id) \
|
#define MIE_DIALECT_ADD_TYPE(type_id) \
|
||||||
extern struct mie_type_definition *type_id##_type_create( \
|
extern struct mie_type_definition* type_id##_type_create( \
|
||||||
struct mie_dialect *, struct mie_ctx *); \
|
struct mie_dialect*, struct mie_ctx*); \
|
||||||
type = type_id##_type_create(self, ctx)
|
type = type_id##_type_create(self, ctx)
|
||||||
#define MIE_DIALECT_ADD_TRAIT(trait_id) \
|
#define MIE_DIALECT_ADD_TRAIT(trait_id) \
|
||||||
extern struct mie_trait_definition *trait_id##_trait_create( \
|
extern struct mie_trait_definition* trait_id##_trait_create( \
|
||||||
struct mie_dialect *, struct mie_ctx *); \
|
struct mie_dialect*, struct mie_ctx*); \
|
||||||
trait = trait_id##_trait_create(self, ctx)
|
trait = trait_id##_trait_create(self, ctx)
|
||||||
#define MIE_DIALECT_ADD_ATTRIBUTE(attr_id) \
|
#define MIE_DIALECT_ADD_ATTRIBUTE(attr_id) \
|
||||||
extern struct mie_attribute_definition *attr_id##_attribute_create( \
|
extern struct mie_attribute_definition* attr_id##_attribute_create( \
|
||||||
struct mie_dialect *, struct mie_ctx *); \
|
struct mie_dialect*, struct mie_ctx*); \
|
||||||
attribute = attr_id##_attribute_create(self, ctx)
|
attribute = attr_id##_attribute_create(self, ctx)
|
||||||
#define MIE_DIALECT_ADD_INTERFACE(iface_id) \
|
#define MIE_DIALECT_ADD_INTERFACE(iface_id) \
|
||||||
extern struct mie_interface_definition *iface_id##_interface_create( \
|
extern struct mie_interface_definition* iface_id##_interface_create( \
|
||||||
struct mie_dialect *, struct mie_ctx *); \
|
struct mie_dialect*, struct mie_ctx*); \
|
||||||
interface = iface_id##_interface_create(self, ctx)
|
interface = iface_id##_interface_create(self, ctx)
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
@@ -51,17 +57,17 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define MIE_OP_DEFINITION_BEGIN(func_prefix, op_name) \
|
#define MIE_OP_DEFINITION_BEGIN(func_prefix, op_name) \
|
||||||
struct mie_op_definition *func_prefix##_op_create( \
|
struct mie_op_definition* func_prefix##_op_create( \
|
||||||
struct mie_dialect *d, struct mie_ctx *ctx) \
|
struct mie_dialect* d, struct mie_ctx* ctx) \
|
||||||
{ \
|
{ \
|
||||||
struct mie_op_definition *op \
|
struct mie_op_definition* op \
|
||||||
= mie_op_definition_create(d, op_name); \
|
= mie_op_definition_create(d, op_name); \
|
||||||
if (!op) { \
|
if (!op) { \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
} \
|
} \
|
||||||
const struct mie_trait *trait = NULL; \
|
const struct mie_trait* trait = NULL; \
|
||||||
struct mie_interface *i = NULL; \
|
struct mie_interface* i = NULL; \
|
||||||
const struct mie_interface_definition *id = NULL;
|
const struct mie_interface_definition* id = NULL;
|
||||||
|
|
||||||
#define MIE_OP_DEFINITION_END() \
|
#define MIE_OP_DEFINITION_END() \
|
||||||
return op; \
|
return op; \
|
||||||
@@ -75,10 +81,10 @@
|
|||||||
#define MIE_OP_INTERFACE_BEGIN(dialect_name, if_name, c_struct) \
|
#define MIE_OP_INTERFACE_BEGIN(dialect_name, if_name, c_struct) \
|
||||||
id = mie_ctx_get_interface_definition(ctx, dialect_name, if_name); \
|
id = mie_ctx_get_interface_definition(ctx, dialect_name, if_name); \
|
||||||
if (id) { \
|
if (id) { \
|
||||||
c_struct *iface = (c_struct *)mie_interface_create(id);
|
c_struct* iface = (c_struct*)mie_interface_create(id);
|
||||||
#define MIE_OP_INTERFACE_FUNC(func_name) iface->func_name
|
#define MIE_OP_INTERFACE_FUNC(func_name) iface->func_name
|
||||||
#define MIE_OP_INTERFACE_END() \
|
#define MIE_OP_INTERFACE_END() \
|
||||||
mie_op_definition_add_interface(op, (struct mie_interface *)iface); \
|
mie_op_definition_add_interface(op, (struct mie_interface*)iface); \
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
@@ -86,15 +92,15 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define MIE_TYPE_DEFINITION_BEGIN(func_prefix, type_name) \
|
#define MIE_TYPE_DEFINITION_BEGIN(func_prefix, type_name) \
|
||||||
struct mie_type_definition *func_prefix##_type_create( \
|
struct mie_type_definition* func_prefix##_type_create( \
|
||||||
struct mie_dialect *d, struct mie_ctx *ctx) \
|
struct mie_dialect* d, struct mie_ctx* ctx) \
|
||||||
{ \
|
{ \
|
||||||
struct mie_type_definition *type \
|
struct mie_type_definition* type \
|
||||||
= mie_type_definition_create(d, type_name); \
|
= mie_type_definition_create(d, type_name); \
|
||||||
if (!type) { \
|
if (!type) { \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
} \
|
} \
|
||||||
const struct mie_trait *trait = NULL;
|
const struct mie_trait* trait = NULL;
|
||||||
|
|
||||||
#define MIE_TYPE_DEFINITION_END() \
|
#define MIE_TYPE_DEFINITION_END() \
|
||||||
return type; \
|
return type; \
|
||||||
@@ -117,10 +123,10 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define MIE_TRAIT_DEFINITION_BEGIN(func_prefix, trait_name) \
|
#define MIE_TRAIT_DEFINITION_BEGIN(func_prefix, trait_name) \
|
||||||
struct mie_trait_definition *func_prefix##_trait_create( \
|
struct mie_trait_definition* func_prefix##_trait_create( \
|
||||||
struct mie_dialect *d, struct mie_ctx *ctx) \
|
struct mie_dialect* d, struct mie_ctx* ctx) \
|
||||||
{ \
|
{ \
|
||||||
struct mie_trait_definition *trait \
|
struct mie_trait_definition* trait \
|
||||||
= mie_trait_definition_create(d, trait_name); \
|
= mie_trait_definition_create(d, trait_name); \
|
||||||
if (!trait) { \
|
if (!trait) { \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
@@ -142,10 +148,10 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define MIE_ATTRIBUTE_DEFINITION_BEGIN(func_prefix, attribute_name) \
|
#define MIE_ATTRIBUTE_DEFINITION_BEGIN(func_prefix, attribute_name) \
|
||||||
struct mie_attribute_definition *func_prefix##_attribute_create( \
|
struct mie_attribute_definition* func_prefix##_attribute_create( \
|
||||||
struct mie_dialect *d, struct mie_ctx *ctx) \
|
struct mie_dialect* d, struct mie_ctx* ctx) \
|
||||||
{ \
|
{ \
|
||||||
struct mie_attribute_definition *attribute \
|
struct mie_attribute_definition* attribute \
|
||||||
= mie_attribute_definition_create(d, attribute_name); \
|
= mie_attribute_definition_create(d, attribute_name); \
|
||||||
if (!attribute) { \
|
if (!attribute) { \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
@@ -167,10 +173,10 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define MIE_INTERFACE_DEFINITION_BEGIN(func_prefix, iface_name) \
|
#define MIE_INTERFACE_DEFINITION_BEGIN(func_prefix, iface_name) \
|
||||||
struct mie_interface_definition *func_prefix##_interface_create( \
|
struct mie_interface_definition* func_prefix##_interface_create( \
|
||||||
struct mie_dialect *d, struct mie_ctx *ctx) \
|
struct mie_dialect* d, struct mie_ctx* ctx) \
|
||||||
{ \
|
{ \
|
||||||
struct mie_interface_definition *i \
|
struct mie_interface_definition* i \
|
||||||
= mie_interface_definition_create(d, iface_name); \
|
= mie_interface_definition_create(d, iface_name); \
|
||||||
if (!i) { \
|
if (!i) { \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
@@ -187,9 +193,9 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define MIE_PASS_DEFINITION_BEGIN(prefix) \
|
#define MIE_PASS_DEFINITION_BEGIN(prefix) \
|
||||||
struct mie_pass_definition *prefix##_pass_create(struct mie_ctx *ctx) \
|
struct mie_pass_definition* prefix##_pass_create(struct mie_ctx* ctx) \
|
||||||
{ \
|
{ \
|
||||||
struct mie_pass_definition *p = mie_pass_definition_create(); \
|
struct mie_pass_definition* p = mie_pass_definition_create(); \
|
||||||
if (!p) { \
|
if (!p) { \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
}
|
}
|
||||||
@@ -224,21 +230,21 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define MIE_PASS_GROUP_BEGIN(prefix) \
|
#define MIE_PASS_GROUP_BEGIN(prefix) \
|
||||||
enum mie_status prefix##_passes_register(struct mie_ctx *ctx) \
|
enum mie_status prefix##_passes_register(struct mie_ctx* ctx) \
|
||||||
{ \
|
{ \
|
||||||
struct mie_pass_definition *pass = NULL;
|
struct mie_pass_definition* pass = NULL;
|
||||||
|
|
||||||
#define MIE_PASS_GROUP_END() \
|
#define MIE_PASS_GROUP_END() \
|
||||||
return MIE_SUCCESS; \
|
return MIE_SUCCESS; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MIE_PASS_GROUP_DECLARATION(prefix) \
|
#define MIE_PASS_GROUP_DECLARATION(prefix) \
|
||||||
enum mie_status prefix##_passes_register(struct mie_ctx *);
|
enum mie_status prefix##_passes_register(struct mie_ctx*);
|
||||||
|
|
||||||
#define MIE_PASS_GROUP_ADD_PASS(id) \
|
#define MIE_PASS_GROUP_ADD_PASS(id) \
|
||||||
do { \
|
do { \
|
||||||
extern struct mie_pass_definition *id##_pass_create( \
|
extern struct mie_pass_definition* id##_pass_create( \
|
||||||
struct mie_ctx *); \
|
struct mie_ctx*); \
|
||||||
pass = id##_pass_create(ctx); \
|
pass = id##_pass_create(ctx); \
|
||||||
if (!pass) { \
|
if (!pass) { \
|
||||||
return MIE_ERR_INTERNAL_FAILURE; \
|
return MIE_ERR_INTERNAL_FAILURE; \
|
||||||
@@ -248,13 +254,13 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* REWRITE PATTER MACROS */
|
/* REWRITE PATTERN MACROS */
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define MIE_REWRITE_PATTERN_BEGIN(name) \
|
#define MIE_REWRITE_PATTERN_BEGIN(name) \
|
||||||
struct mie_pattern *name##_create(struct mie_pattern_set *set) \
|
struct mie_pattern* name##_create(struct mie_pattern_set* set) \
|
||||||
{ \
|
{ \
|
||||||
struct mie_pattern *self = mie_pattern_set_add(set); \
|
struct mie_pattern* self = mie_pattern_set_add(set); \
|
||||||
if (!self) { \
|
if (!self) { \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
}
|
}
|
||||||
@@ -267,3 +273,62 @@
|
|||||||
self->p_root.t_op_name = (op_name)
|
self->p_root.t_op_name = (op_name)
|
||||||
#define MIE_REWRITE_PATTERN_MATCH(func) self->p_match = (func)
|
#define MIE_REWRITE_PATTERN_MATCH(func) self->p_match = (func)
|
||||||
#define MIE_REWRITE_PATTERN_REWRITE(func) self->p_rewrite = (func)
|
#define MIE_REWRITE_PATTERN_REWRITE(func) self->p_rewrite = (func)
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/* DIAG CLASS MACROS */
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define __MIE_CNAME2(x, y) x##_##y
|
||||||
|
#define __MIE_CNAME1(x, y) __MIE_CNAME2(x, y)
|
||||||
|
#define __MIE_CNAME(fun) __MIE_CNAME1(MIE_DIAG_CLASS_PREFIX, fun)
|
||||||
|
|
||||||
|
#define __MIE_CSNAME2(x, y) #x #y
|
||||||
|
#define __MIE_CSNAME1(x, y) __MIE_CSNAME2(x, y)
|
||||||
|
#define __MIE_CSNAME(fun) __MIE_CSNAME1(MIE_DIAG_CLASS_PREFIX, fun)
|
||||||
|
|
||||||
|
#define MIE_DIAG_CLASS_LIST_EXTERN(list_name) \
|
||||||
|
extern struct mie_diag_class list_name[]; \
|
||||||
|
extern const size_t list_name##_count
|
||||||
|
#define MIE_DIAG_CLASS_LIST_BEGIN(list_name) \
|
||||||
|
struct mie_diag_class list_name[] = {
|
||||||
|
#define MIE_DIAG_CLASS_LIST_END(list_name) \
|
||||||
|
} \
|
||||||
|
; \
|
||||||
|
const size_t list_name##_count = sizeof(list_name) / sizeof(list_name[0]);
|
||||||
|
#define MIE_DIAG_CLASS(id, type, title) \
|
||||||
|
[__MIE_CNAME(id)] = { \
|
||||||
|
.c_id = __MIE_CNAME(id), \
|
||||||
|
.c_type = MIE_DIAG_CLASS_##type, \
|
||||||
|
.c_title = (title), \
|
||||||
|
.c_id_str_short = #id, \
|
||||||
|
.c_id_str_long = __MIE_CSNAME(id), \
|
||||||
|
},
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/* DIAG MSG MACROS */
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define __MIE_MNAME2(x, y) x##_##y
|
||||||
|
#define __MIE_MNAME1(x, y) __MIE_CNAME2(x, y)
|
||||||
|
#define __MIE_MNAME(fun) __MIE_CNAME1(MIE_DIAG_MSG_PREFIX, fun)
|
||||||
|
|
||||||
|
#define __MIE_MSNAME2(x, y) #x #y
|
||||||
|
#define __MIE_MSNAME1(x, y) __MIE_CSNAME2(x, y)
|
||||||
|
#define __MIE_MSNAME(fun) __MIE_CSNAME1(MIE_DIAG_MSG_PREFIX, fun)
|
||||||
|
|
||||||
|
#define MIE_DIAG_MSG_LIST_EXTERN(list_name) \
|
||||||
|
extern struct mie_diag_msg list_name[]; \
|
||||||
|
extern const size_t list_name##_count
|
||||||
|
#define MIE_DIAG_MSG_LIST_BEGIN(list_name) \
|
||||||
|
struct mie_diag_msg list_name[] = {
|
||||||
|
#define MIE_DIAG_MSG_LIST_END(list_name) \
|
||||||
|
} \
|
||||||
|
; \
|
||||||
|
const size_t list_name##_count = sizeof(list_name) / sizeof(list_name[0]);
|
||||||
|
#define MIE_DIAG_MSG(id, content) \
|
||||||
|
[__MIE_MNAME(id)] = { \
|
||||||
|
.msg_id = __MIE_MNAME(id), \
|
||||||
|
.msg_content = (content), \
|
||||||
|
.msg_id_str_short = #id, \
|
||||||
|
.msg_id_str_long = __MIE_MSNAME(id), \
|
||||||
|
},
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ struct mie_name {
|
|||||||
|
|
||||||
/* if this name was read from a file, these structs can be used to
|
/* if this name was read from a file, these structs can be used to
|
||||||
* record the location within the file where the name is found. */
|
* record the location within the file where the name is found. */
|
||||||
struct mie_file_span n_start, n_end;
|
struct mie_file_span n_span;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct mie_name_map {
|
struct mie_name_map {
|
||||||
|
|||||||
@@ -1,17 +1,22 @@
|
|||||||
#ifndef MIE_PARSE_LEX_H_
|
#ifndef MIE_PARSE_LEX_H_
|
||||||
#define MIE_PARSE_LEX_H_
|
#define MIE_PARSE_LEX_H_
|
||||||
|
|
||||||
|
#include <blue/core/queue.h>
|
||||||
#include <blue/core/stream.h>
|
#include <blue/core/stream.h>
|
||||||
#include <mie/misc.h>
|
#include <mie/misc.h>
|
||||||
#include <mie/status.h>
|
#include <mie/status.h>
|
||||||
|
|
||||||
struct mie_lex;
|
struct mie_lex;
|
||||||
|
struct mie_ctx;
|
||||||
struct mie_token;
|
struct mie_token;
|
||||||
|
struct mie_line_source;
|
||||||
|
|
||||||
MIE_API struct mie_lex *mie_lex_create(b_stream *src);
|
MIE_API struct mie_lex *mie_lex_create(
|
||||||
|
struct mie_line_source *src, struct mie_ctx *ctx);
|
||||||
MIE_API void mie_lex_destroy(struct mie_lex *lex);
|
MIE_API void mie_lex_destroy(struct mie_lex *lex);
|
||||||
|
|
||||||
MIE_API enum mie_status mie_lex_get_status(const struct mie_lex *lex);
|
MIE_API enum mie_status mie_lex_get_status(const struct mie_lex *lex);
|
||||||
|
MIE_API struct mie_line_source *mie_lex_get_line_source(const struct mie_lex *lex);
|
||||||
MIE_API struct mie_token *mie_lex_peek(struct mie_lex *lex);
|
MIE_API struct mie_token *mie_lex_peek(struct mie_lex *lex);
|
||||||
MIE_API void mie_lex_advance(struct mie_lex *lex);
|
MIE_API void mie_lex_advance(struct mie_lex *lex);
|
||||||
|
|
||||||
|
|||||||
35
mie/include/mie/parse/line-source.h
Normal file
35
mie/include/mie/parse/line-source.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#ifndef MIE_PARSE_LINE_SOURCE_H_
|
||||||
|
#define MIE_PARSE_LINE_SOURCE_H_
|
||||||
|
|
||||||
|
#include <blue/core/stream.h>
|
||||||
|
#include <blue/ds/array.h>
|
||||||
|
#include <blue/ds/string.h>
|
||||||
|
#include <mie/misc.h>
|
||||||
|
#include <mie/parse/file-span.h>
|
||||||
|
#include <mie/status.h>
|
||||||
|
|
||||||
|
struct mie_line_source {
|
||||||
|
b_stream *s_stream;
|
||||||
|
const char *s_path;
|
||||||
|
b_string *s_linebuf;
|
||||||
|
b_iterator *s_linebuf_ptr;
|
||||||
|
b_array *s_lines;
|
||||||
|
|
||||||
|
struct mie_file_cell s_cursor;
|
||||||
|
};
|
||||||
|
|
||||||
|
MIE_API enum mie_status mie_line_source_init(
|
||||||
|
struct mie_line_source *src, const char *path, b_stream *stream);
|
||||||
|
MIE_API void mie_line_source_cleanup(struct mie_line_source *src);
|
||||||
|
|
||||||
|
MIE_API const char *mie_line_source_get_path(const struct mie_line_source *src);
|
||||||
|
MIE_API const struct mie_file_cell *mie_line_source_get_cursor(
|
||||||
|
const struct mie_line_source *src);
|
||||||
|
MIE_API b_wchar mie_line_source_peekc(struct mie_line_source *src);
|
||||||
|
MIE_API b_wchar mie_line_source_getc(struct mie_line_source *src);
|
||||||
|
MIE_API enum mie_status mie_line_source_get_row(
|
||||||
|
struct mie_line_source *src, size_t row, const b_string **out);
|
||||||
|
|
||||||
|
MIE_API bool mie_line_source_input_available(struct mie_line_source *src);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -23,6 +23,8 @@ enum mie_print_flags {
|
|||||||
MIE_PRINT_F_INCLUDE_TYPE = 0x04u,
|
MIE_PRINT_F_INCLUDE_TYPE = 0x04u,
|
||||||
MIE_PRINT_F_EXCLUDE_BLOCK_HEADER = 0x08u,
|
MIE_PRINT_F_EXCLUDE_BLOCK_HEADER = 0x08u,
|
||||||
MIE_PRINT_F_EXCLUDE_FIRST_BLOCK_HEADER = 0x10u,
|
MIE_PRINT_F_EXCLUDE_FIRST_BLOCK_HEADER = 0x10u,
|
||||||
|
|
||||||
|
MIE_PRINT_F_MARK_UNRESOLVED_ELEMENTS = 0x20u,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct mie_printer {
|
struct mie_printer {
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ struct mie_vector_ops {
|
|||||||
&(vector.max), ops)
|
&(vector.max), ops)
|
||||||
#define mie_vector_pop_back(vector, ops) \
|
#define mie_vector_pop_back(vector, ops) \
|
||||||
__mie_vector_pop_back( \
|
__mie_vector_pop_back( \
|
||||||
(void **)&(vector), sizeof *vector, &(vector.count), \
|
(void **)&(vector.items), sizeof *vector.items, \
|
||||||
&(vector.max), ops)
|
&(vector.count), &(vector.max), ops)
|
||||||
#define mie_vector_emplace_back(vector, ops) \
|
#define mie_vector_emplace_back(vector, ops) \
|
||||||
__mie_vector_emplace_back( \
|
__mie_vector_emplace_back( \
|
||||||
(void **)&(vector.items), sizeof *vector.items, \
|
(void **)&(vector.items), sizeof *vector.items, \
|
||||||
@@ -86,6 +86,10 @@ struct mie_vector_ops {
|
|||||||
__mie_vector_destroy( \
|
__mie_vector_destroy( \
|
||||||
(void **)(vector), sizeof **vector, (vector##_count), \
|
(void **)(vector), sizeof **vector, (vector##_count), \
|
||||||
(vector##_max), ops)
|
(vector##_max), ops)
|
||||||
|
#define mie_vector_ref_get_item(vector, index) (*vector)[index]
|
||||||
|
#define mie_vector_ref_get_item_ptr(vector, index) (&(*vector)[index])
|
||||||
|
#define mie_vector_ref_get_count(vector) *(vector##_count)
|
||||||
|
#define mie_vector_ref_get_max(vector) *(vector##_count)
|
||||||
|
|
||||||
/* don't use these functions */
|
/* don't use these functions */
|
||||||
MIE_API int __mie_vector_push_back(
|
MIE_API int __mie_vector_push_back(
|
||||||
|
|||||||
166
mie/ir/block.c
166
mie/ir/block.c
@@ -52,6 +52,144 @@ struct mie_op *mie_block_get_last_op(const struct mie_block *block)
|
|||||||
return b_unbox(struct mie_op, entry, op_entry);
|
return b_unbox(struct mie_op, entry, op_entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct mie_op *mie_block_get_terminator(const struct mie_block *block)
|
||||||
|
{
|
||||||
|
b_queue_entry *op_entry = b_queue_last(&block->b_ops);
|
||||||
|
return b_unbox(struct mie_op, op_entry, op_entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_op_successor *mie_block_get_first_successor(const struct mie_block *block)
|
||||||
|
{
|
||||||
|
struct mie_op *op = mie_block_get_terminator(block);
|
||||||
|
if (!op) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MIE_VECTOR_COUNT(op->op_successors) == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &op->op_successors.items[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_op_successor *mie_block_get_prev_successor(
|
||||||
|
const struct mie_block *block, const struct mie_op_successor *before)
|
||||||
|
{
|
||||||
|
struct mie_op *op = mie_block_get_terminator(block);
|
||||||
|
if (!op) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MIE_VECTOR_COUNT(op->op_successors) == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t i = before - &op->op_successors.items[0];
|
||||||
|
if (i == 0 || i >= MIE_VECTOR_COUNT(op->op_successors)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &op->op_successors.items[i - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_op_successor *mie_block_get_next_successor(
|
||||||
|
const struct mie_block *block, const struct mie_op_successor *after)
|
||||||
|
{
|
||||||
|
struct mie_op *op = mie_block_get_terminator(block);
|
||||||
|
if (!op) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MIE_VECTOR_COUNT(op->op_successors) == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t i = after - &op->op_successors.items[0];
|
||||||
|
if (i >= MIE_VECTOR_COUNT(op->op_successors)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i + 1 >= MIE_VECTOR_COUNT(op->op_successors)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &op->op_successors.items[i + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_op_successor *mie_block_get_last_successor(const struct mie_block *block)
|
||||||
|
{
|
||||||
|
struct mie_op *op = mie_block_get_terminator(block);
|
||||||
|
if (!op) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t nr_successors = MIE_VECTOR_COUNT(op->op_successors);
|
||||||
|
|
||||||
|
if (nr_successors == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &op->op_successors.items[nr_successors - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_block_predecessor *mie_block_get_first_predecessor(
|
||||||
|
const struct mie_block *block)
|
||||||
|
{
|
||||||
|
size_t nr_predecessors = MIE_VECTOR_COUNT(block->b_ipred);
|
||||||
|
|
||||||
|
if (nr_predecessors == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &block->b_ipred.items[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_block_predecessor *mie_block_get_prev_predecessor(
|
||||||
|
const struct mie_block *block, const struct mie_block_predecessor *before)
|
||||||
|
{
|
||||||
|
if (MIE_VECTOR_COUNT(block->b_ipred) == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t i = before - &block->b_ipred.items[0];
|
||||||
|
if (i == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &block->b_ipred.items[i - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_block_predecessor *mie_block_get_next_predecessor(
|
||||||
|
const struct mie_block *block, const struct mie_block_predecessor *after)
|
||||||
|
{
|
||||||
|
if (MIE_VECTOR_COUNT(block->b_ipred) == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t i = after - &block->b_ipred.items[0];
|
||||||
|
if (i >= MIE_VECTOR_COUNT(block->b_ipred)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i + 1 >= MIE_VECTOR_COUNT(block->b_ipred)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &block->b_ipred.items[i + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_block_predecessor *mie_block_get_last_predecessor(
|
||||||
|
const struct mie_block *block)
|
||||||
|
{
|
||||||
|
size_t nr_predecessors = MIE_VECTOR_COUNT(block->b_ipred);
|
||||||
|
|
||||||
|
if (nr_predecessors == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &block->b_ipred.items[nr_predecessors - 1];
|
||||||
|
}
|
||||||
|
|
||||||
struct mie_op *mie_block_add_op(struct mie_block *block)
|
struct mie_op *mie_block_add_op(struct mie_block *block)
|
||||||
{
|
{
|
||||||
struct mie_op *op = malloc(sizeof *op);
|
struct mie_op *op = malloc(sizeof *op);
|
||||||
@@ -100,3 +238,31 @@ struct mie_register *mie_block_add_param(struct mie_block *block)
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct mie_register *mie_block_find_register(
|
||||||
|
const struct mie_block *block, const char *name,
|
||||||
|
const struct mie_op *start_point)
|
||||||
|
{
|
||||||
|
const b_queue_entry *entry = b_queue_last(&block->b_ops);
|
||||||
|
if (start_point) {
|
||||||
|
entry = &start_point->op_entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < MIE_VECTOR_COUNT(block->b_params); i++) {
|
||||||
|
if (!strcmp(block->b_params.items[i].reg_name.n_str, name)) {
|
||||||
|
return &block->b_params.items[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (entry) {
|
||||||
|
struct mie_op *op = b_unbox(struct mie_op, entry, op_entry);
|
||||||
|
struct mie_register *reg = mie_op_get_result_with_name(op, name);
|
||||||
|
if (reg) {
|
||||||
|
return reg;
|
||||||
|
}
|
||||||
|
|
||||||
|
entry = b_queue_prev(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|||||||
@@ -145,7 +145,6 @@ struct mie_op *mie_builder_put_op(
|
|||||||
struct mie_op *op = mie_block_add_op(block);
|
struct mie_op *op = mie_block_add_op(block);
|
||||||
op->op_flags = MIE_OP_F_OP_RESOLVED;
|
op->op_flags = MIE_OP_F_OP_RESOLVED;
|
||||||
op->op_info = op_def;
|
op->op_info = op_def;
|
||||||
op->op_dialect = op_def->op_parent;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < nr_args; i++) {
|
for (size_t i = 0; i < nr_args; i++) {
|
||||||
struct mie_op_arg *arg = mie_op_add_arg(op);
|
struct mie_op_arg *arg = mie_op_add_arg(op);
|
||||||
|
|||||||
31
mie/ir/op.c
31
mie/ir/op.c
@@ -1,4 +1,8 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <mie/ctx.h>
|
||||||
|
#include <mie/diag/diag.h>
|
||||||
|
#include <mie/diag/highlight.h>
|
||||||
|
#include <mie/dialect/builtin.h>
|
||||||
#include <mie/dialect/dialect.h>
|
#include <mie/dialect/dialect.h>
|
||||||
#include <mie/ir/block.h>
|
#include <mie/ir/block.h>
|
||||||
#include <mie/ir/op-definition.h>
|
#include <mie/ir/op-definition.h>
|
||||||
@@ -66,6 +70,17 @@ void mie_op_cleanup(struct mie_op *op)
|
|||||||
/* TODO */
|
/* TODO */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t mie_op_get_name(const struct mie_op *op, char *out, size_t max)
|
||||||
|
{
|
||||||
|
if (!(op->op_flags & MIE_OP_F_OP_RESOLVED)) {
|
||||||
|
return snprintf(out, max, "%s", op->op_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return snprintf(
|
||||||
|
out, max, "%s.%s", op->op_info->op_parent->d_name,
|
||||||
|
op->op_info->op_name);
|
||||||
|
}
|
||||||
|
|
||||||
struct mie_op_arg *mie_op_add_arg(struct mie_op *op)
|
struct mie_op_arg *mie_op_add_arg(struct mie_op *op)
|
||||||
{
|
{
|
||||||
return mie_vector_emplace_back(op->op_args, &op_arg_vector_ops);
|
return mie_vector_emplace_back(op->op_args, &op_arg_vector_ops);
|
||||||
@@ -139,11 +154,11 @@ struct mie_region *mie_op_add_region(struct mie_op *op)
|
|||||||
|
|
||||||
bool mie_op_is(const struct mie_op *op, const char *dialect_name, const char *op_name)
|
bool mie_op_is(const struct mie_op *op, const char *dialect_name, const char *op_name)
|
||||||
{
|
{
|
||||||
if (!op->op_info || !op->op_dialect) {
|
if (!op->op_info || !op->op_info->op_parent) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(op->op_dialect->d_name, dialect_name) != 0) {
|
if (strcmp(op->op_info->op_parent->d_name, dialect_name) != 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -249,6 +264,18 @@ struct mie_register *mie_op_get_arg(const struct mie_op *op, size_t index)
|
|||||||
return arg->arg_value.u_reg;
|
return arg->arg_value.u_reg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct mie_register *mie_op_get_result_with_name(
|
||||||
|
const struct mie_op *op, const char *name)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_result); i++) {
|
||||||
|
if (!strcmp(op->op_result.items[i].reg_name.n_str, name)) {
|
||||||
|
return &op->op_result.items[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
struct mie_op *mie_op_get_first_child_op(const struct mie_op *op)
|
struct mie_op *mie_op_get_first_child_op(const struct mie_op *op)
|
||||||
{
|
{
|
||||||
struct mie_region *first_region = mie_op_get_first_region(op);
|
struct mie_region *first_region = mie_op_get_first_region(op);
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
|
#include <limits.h>
|
||||||
|
#include <mie/ctx.h>
|
||||||
|
#include <mie/diag/diag.h>
|
||||||
|
#include <mie/dialect/builtin.h>
|
||||||
#include <mie/ir/block.h>
|
#include <mie/ir/block.h>
|
||||||
|
#include <mie/ir/op.h>
|
||||||
#include <mie/ir/region.h>
|
#include <mie/ir/region.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -63,6 +68,7 @@ struct mie_block *mie_region_add_block(struct mie_region *region)
|
|||||||
|
|
||||||
memset(block, 0x0, sizeof *block);
|
memset(block, 0x0, sizeof *block);
|
||||||
block->b_parent = region;
|
block->b_parent = region;
|
||||||
|
block->b_id = MIE_BLOCK_ID_INVALID;
|
||||||
|
|
||||||
b_queue_push_back(®ion->r_blocks, &block->b_entry);
|
b_queue_push_back(®ion->r_blocks, &block->b_entry);
|
||||||
|
|
||||||
@@ -89,3 +95,38 @@ struct mie_block *mie_region_add_block_after(
|
|||||||
|
|
||||||
return block;
|
return block;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct mie_block *mie_region_find_block(
|
||||||
|
const struct mie_region *region, const char *name)
|
||||||
|
{
|
||||||
|
b_queue_entry *cur = b_queue_first(®ion->r_blocks);
|
||||||
|
while (cur) {
|
||||||
|
struct mie_block *block = b_unbox(struct mie_block, cur, b_entry);
|
||||||
|
if (!strcmp(block->b_name.n_str, name)) {
|
||||||
|
return block;
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = b_queue_next(cur);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_register *mie_region_find_register(
|
||||||
|
const struct mie_region *region, const char *name)
|
||||||
|
{
|
||||||
|
b_queue_entry *cur = b_queue_first(®ion->r_blocks);
|
||||||
|
|
||||||
|
while (cur) {
|
||||||
|
struct mie_block *block = b_unbox(struct mie_block, cur, b_entry);
|
||||||
|
struct mie_register *reg
|
||||||
|
= mie_block_find_register(block, name, NULL);
|
||||||
|
if (reg) {
|
||||||
|
return reg;
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = b_queue_next(cur);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|||||||
264
mie/ir/resolve.c
Normal file
264
mie/ir/resolve.c
Normal file
@@ -0,0 +1,264 @@
|
|||||||
|
#include <mie/ctx.h>
|
||||||
|
#include <mie/diag/diag.h>
|
||||||
|
#include <mie/diag/highlight.h>
|
||||||
|
#include <mie/dialect/builtin.h>
|
||||||
|
#include <mie/dialect/dialect.h>
|
||||||
|
#include <mie/ir/block.h>
|
||||||
|
#include <mie/ir/op.h>
|
||||||
|
#include <mie/ir/region.h>
|
||||||
|
#include <mie/ir/register.h>
|
||||||
|
|
||||||
|
enum register_find_result {
|
||||||
|
REG_FIND_NONE = 0,
|
||||||
|
REG_FIND_USE_BEFORE_DEFINE,
|
||||||
|
REG_FIND_UNDOMINATED,
|
||||||
|
REG_FIND_ISOLATED,
|
||||||
|
};
|
||||||
|
|
||||||
|
bool mie_op_resolve_self(struct mie_op *op, struct mie_ctx *ctx)
|
||||||
|
{
|
||||||
|
if (op->op_flags & MIE_OP_F_OP_RESOLVED) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *dialect_name = NULL, *op_name = NULL;
|
||||||
|
|
||||||
|
char *dot = strchr(op->op_name, '.');
|
||||||
|
if (dot) {
|
||||||
|
*dot = 0;
|
||||||
|
dialect_name = op->op_name;
|
||||||
|
op_name = dot + 1;
|
||||||
|
} else {
|
||||||
|
dialect_name = NULL;
|
||||||
|
op_name = op->op_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
const struct mie_dialect *dialect = mie_ctx_get_dialect(ctx, dialect_name);
|
||||||
|
if (dot) {
|
||||||
|
*dot = '.';
|
||||||
|
}
|
||||||
|
|
||||||
|
/* dialect_name is no longer valid after this point */
|
||||||
|
dialect_name = NULL;
|
||||||
|
|
||||||
|
if (!dialect) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const struct mie_op_definition *op_info
|
||||||
|
= mie_dialect_get_op(dialect, op_name);
|
||||||
|
if (!op_info) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
op->op_info = op_info;
|
||||||
|
|
||||||
|
free(op->op_name);
|
||||||
|
op->op_name = NULL;
|
||||||
|
|
||||||
|
op->op_flags |= MIE_OP_F_OP_RESOLVED;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static enum register_find_result find_register_wide(
|
||||||
|
struct mie_op *op, const char *name, struct mie_register **out)
|
||||||
|
{
|
||||||
|
struct mie_region *region = op->op_container->b_parent;
|
||||||
|
enum register_find_result result = REG_FIND_UNDOMINATED;
|
||||||
|
struct mie_register *reg = NULL;
|
||||||
|
|
||||||
|
while (region) {
|
||||||
|
reg = mie_region_find_register(region, name);
|
||||||
|
if (reg) {
|
||||||
|
*out = reg;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_op *op = region->r_parent;
|
||||||
|
if (mie_op_has_trait(op, "builtin", "isolated-from-above")) {
|
||||||
|
result = REG_FIND_ISOLATED;
|
||||||
|
}
|
||||||
|
|
||||||
|
region = op->op_container ? op->op_container->b_parent : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!reg) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (reg->reg_block == op->op_container) {
|
||||||
|
return REG_FIND_USE_BEFORE_DEFINE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool resolve_arg(
|
||||||
|
struct mie_op *op, struct mie_op_arg *arg, struct mie_ctx *ctx)
|
||||||
|
{
|
||||||
|
const char *arg_name = arg->arg_unresolved.reg_name;
|
||||||
|
struct mie_block *block = op->op_container;
|
||||||
|
struct mie_op *search_start = op;
|
||||||
|
struct mie_register *reg = NULL;
|
||||||
|
|
||||||
|
while (block) {
|
||||||
|
reg = mie_block_find_register(block, arg_name, search_start);
|
||||||
|
if (reg) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
search_start = NULL;
|
||||||
|
block = block->b_idom;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (reg) {
|
||||||
|
free(arg->arg_unresolved.reg_name);
|
||||||
|
arg->arg_flags |= MIE_OP_F_ARG_RESOLVED;
|
||||||
|
|
||||||
|
memset(&arg->arg_value, 0x0, sizeof arg->arg_value);
|
||||||
|
arg->arg_value.u_reg = reg;
|
||||||
|
arg->arg_value.u_user = op;
|
||||||
|
b_queue_push_back(®->reg_use, &arg->arg_value.u_entry);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum register_find_result find_result
|
||||||
|
= find_register_wide(op, arg_name, ®);
|
||||||
|
|
||||||
|
struct mie_diag *diag = mie_ctx_push_diag(
|
||||||
|
ctx, op->op_src, &arg->arg_span.s_start, "builtin",
|
||||||
|
MIE_BUILTIN_E_UNRESOLVED_VALUE);
|
||||||
|
mie_diag_push_msg(diag, ctx, "builtin", MIE_BUILTIN_MSG_UNRESOLVED_VALUE);
|
||||||
|
|
||||||
|
struct mie_diag_highlight hl[] = {
|
||||||
|
{
|
||||||
|
.hl_type = MIE_DIAG_HIGHLIGHT_ERROR,
|
||||||
|
.hl_span = arg->arg_span,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
mie_diag_push_snippet(
|
||||||
|
diag, arg->arg_span.s_start.c_row, arg->arg_span.s_end.c_row,
|
||||||
|
NULL, 0, hl, 1);
|
||||||
|
|
||||||
|
if (!reg) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_block *reg_container = reg->reg_block;
|
||||||
|
|
||||||
|
switch (find_result) {
|
||||||
|
case REG_FIND_ISOLATED:
|
||||||
|
mie_diag_push_msg(
|
||||||
|
diag, ctx, "builtin",
|
||||||
|
MIE_BUILTIN_MSG_VALUE_DEFINED_OUTSIDE_ISOLATED_REGION);
|
||||||
|
break;
|
||||||
|
case REG_FIND_UNDOMINATED:
|
||||||
|
mie_diag_push_msg(
|
||||||
|
diag, ctx, "builtin",
|
||||||
|
MIE_BUILTIN_MSG_VALUE_DEFINED_IN_NON_DOMINANT_BLOCK);
|
||||||
|
break;
|
||||||
|
case REG_FIND_USE_BEFORE_DEFINE:
|
||||||
|
mie_diag_push_msg(
|
||||||
|
diag, ctx, "builtin",
|
||||||
|
MIE_BUILTIN_MSG_VALUE_DEFINED_AFTER_USE);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
hl[0].hl_type = MIE_DIAG_HIGHLIGHT_HINT;
|
||||||
|
hl[0].hl_span = reg->reg_span;
|
||||||
|
|
||||||
|
mie_diag_push_snippet(
|
||||||
|
diag, reg->reg_span.s_start.c_row, reg->reg_span.s_end.c_row,
|
||||||
|
NULL, 0, hl, 1);
|
||||||
|
|
||||||
|
if (!reg_container) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
hl[0].hl_span = reg_container->b_name.n_span;
|
||||||
|
|
||||||
|
mie_diag_push_snippet(
|
||||||
|
diag, reg_container->b_name.n_span.s_start.c_row,
|
||||||
|
reg_container->b_name.n_span.s_end.c_row, NULL, 0, hl, 1);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool resolve_successor(
|
||||||
|
struct mie_op *op, struct mie_op_successor *s, struct mie_ctx *ctx)
|
||||||
|
{
|
||||||
|
if (s->s_flags & MIE_OP_F_SUCCESSOR_RESOLVED) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_block *container = op->op_container;
|
||||||
|
struct mie_region *region = container ? container->b_parent : NULL;
|
||||||
|
if (!region) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_diag *diag = NULL;
|
||||||
|
struct mie_block *dest = mie_region_find_block(region, s->s_block_name);
|
||||||
|
if (!dest) {
|
||||||
|
diag = mie_ctx_push_diag(
|
||||||
|
ctx, op->op_src, &s->s_name_span.s_start, "builtin",
|
||||||
|
MIE_BUILTIN_E_UNRESOLVED_SUCCESSOR);
|
||||||
|
mie_diag_push_msg(
|
||||||
|
diag, ctx, "builtin", MIE_BUILTIN_MSG_CANNOT_FIND_BLOCK);
|
||||||
|
const struct mie_diag_highlight hl[] = {
|
||||||
|
{
|
||||||
|
.hl_type = MIE_DIAG_HIGHLIGHT_ERROR,
|
||||||
|
.hl_span = s->s_name_span,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
mie_diag_push_snippet(
|
||||||
|
diag, s->s_name_span.s_start.c_row,
|
||||||
|
s->s_name_span.s_end.c_row, NULL, 0, hl, 1);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(s->s_block_name);
|
||||||
|
s->s_block = dest;
|
||||||
|
s->s_flags |= MIE_OP_F_SUCCESSOR_RESOLVED;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mie_op_resolve_args(struct mie_op *op, struct mie_ctx *ctx)
|
||||||
|
{
|
||||||
|
bool ok = true;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_args); i++) {
|
||||||
|
if (!resolve_arg(op, &op->op_args.items[i], ctx)) {
|
||||||
|
ok = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_successors); i++) {
|
||||||
|
struct mie_op_successor *s = &op->op_successors.items[i];
|
||||||
|
for (size_t k = 0; k < MIE_VECTOR_COUNT(s->s_args); k++) {
|
||||||
|
if (!resolve_arg(op, &s->s_args.items[k], ctx)) {
|
||||||
|
ok = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mie_op_resolve_successors(struct mie_op *op, struct mie_ctx *ctx)
|
||||||
|
{
|
||||||
|
bool ok = true;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_successors); i++) {
|
||||||
|
if (!resolve_successor(op, &op->op_successors.items[i], ctx)) {
|
||||||
|
ok = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
@@ -326,7 +326,8 @@ static void print_stack(struct mie_walker *walker)
|
|||||||
case MIE_WALK_ITEM_OP:
|
case MIE_WALK_ITEM_OP:
|
||||||
printf("* %zu: op %p", item->i_depth, item->i_op);
|
printf("* %zu: op %p", item->i_depth, item->i_op);
|
||||||
if (item->i_op->op_flags & MIE_OP_F_OP_RESOLVED) {
|
if (item->i_op->op_flags & MIE_OP_F_OP_RESOLVED) {
|
||||||
printf(" %s.%s", item->i_op->op_dialect->d_name,
|
printf(" %s.%s",
|
||||||
|
item->i_op->op_info->op_parent->d_name,
|
||||||
item->i_op->op_info->op_name);
|
item->i_op->op_info->op_name);
|
||||||
} else {
|
} else {
|
||||||
printf(" %s", item->i_op->op_name);
|
printf(" %s", item->i_op->op_name);
|
||||||
|
|||||||
254
mie/lt-idom.c
Normal file
254
mie/lt-idom.c
Normal file
@@ -0,0 +1,254 @@
|
|||||||
|
#include <mie/ir/block.h>
|
||||||
|
#include <mie/ir/op.h>
|
||||||
|
#include <mie/ir/region.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
struct set {
|
||||||
|
MIE_VECTOR_DECLARE(unsigned int, v);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct dfs_stack_item {
|
||||||
|
struct mie_block *i_parent, *i_node;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct lt_ctx {
|
||||||
|
size_t nr_nodes;
|
||||||
|
struct mie_block **vertex;
|
||||||
|
struct set *bucket;
|
||||||
|
unsigned int *semi;
|
||||||
|
unsigned int *idom;
|
||||||
|
unsigned int *samedom;
|
||||||
|
unsigned int *parent;
|
||||||
|
unsigned int *ancestor;
|
||||||
|
unsigned int *label;
|
||||||
|
|
||||||
|
MIE_VECTOR_DECLARE(struct dfs_stack_item, dfstack);
|
||||||
|
};
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
static B_BTREE_DEFINE_SIMPLE_INSERT(struct mie_block, b_node, b_id, put_block_by_id);
|
||||||
|
static B_BTREE_DEFINE_SIMPLE_GET(
|
||||||
|
struct mie_block, size_t, b_node, b_id, get_block_by_id);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void set_add(struct set *set, unsigned int val)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < MIE_VECTOR_COUNT(set->v); i++) {
|
||||||
|
if (set->v.items[i] == val) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mie_vector_push_back(set->v, &val, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct mie_block *pop_block_successor(
|
||||||
|
MIE_VECTOR_REF_PARAM(struct mie_block *, successors))
|
||||||
|
{
|
||||||
|
size_t count = mie_vector_ref_get_count(successors);
|
||||||
|
if (count == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_block *out = mie_vector_ref_get_item(successors, count - 1);
|
||||||
|
mie_vector_ref_pop_back(successors, NULL);
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void enqueue_block_successors(
|
||||||
|
struct mie_block *block,
|
||||||
|
MIE_VECTOR_REF_PARAM(struct mie_block *, successors))
|
||||||
|
{
|
||||||
|
struct mie_op_successor *s = mie_block_get_first_successor(block);
|
||||||
|
struct mie_block_predecessor pred = {.p_block = block};
|
||||||
|
|
||||||
|
while (s) {
|
||||||
|
if (s->s_flags & MIE_OP_F_SUCCESSOR_RESOLVED) {
|
||||||
|
mie_vector_ref_push_back(successors, &s->s_block, NULL);
|
||||||
|
mie_vector_push_back(s->s_block->b_ipred, &pred, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
s = mie_block_get_next_successor(block, s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dfs_stack_pop(
|
||||||
|
struct lt_ctx *ctx, struct mie_block **parent, struct mie_block **node)
|
||||||
|
{
|
||||||
|
size_t nr = MIE_VECTOR_COUNT(ctx->dfstack);
|
||||||
|
if (!nr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct dfs_stack_item *item = &ctx->dfstack.items[nr - 1];
|
||||||
|
*parent = item->i_parent;
|
||||||
|
*node = item->i_node;
|
||||||
|
mie_vector_pop_back(ctx->dfstack, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dfs_stack_push(
|
||||||
|
struct lt_ctx *ctx, struct mie_block *parent, struct mie_block *node)
|
||||||
|
{
|
||||||
|
struct dfs_stack_item item = {.i_node = node, .i_parent = parent};
|
||||||
|
mie_vector_push_back(ctx->dfstack, &item, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dfs(struct lt_ctx *ctx, struct mie_block *root)
|
||||||
|
{
|
||||||
|
dfs_stack_push(ctx, NULL, root);
|
||||||
|
unsigned int id = 1;
|
||||||
|
|
||||||
|
while (MIE_VECTOR_COUNT(ctx->dfstack) > 0) {
|
||||||
|
struct mie_block *p, *n;
|
||||||
|
dfs_stack_pop(ctx, &p, &n);
|
||||||
|
|
||||||
|
if (n->b_id != MIE_BLOCK_ID_INVALID) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
printf("dfs(p=%s, n=%s)\n", p ? p->b_name.n_str : "NA",
|
||||||
|
n->b_name.n_str);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
n->b_id = id++;
|
||||||
|
ctx->vertex[n->b_id] = n;
|
||||||
|
ctx->semi[n->b_id] = n->b_id;
|
||||||
|
ctx->parent[n->b_id] = p ? p->b_id : MIE_BLOCK_ID_INVALID;
|
||||||
|
|
||||||
|
struct mie_op_successor *s = mie_block_get_first_successor(n);
|
||||||
|
while (s) {
|
||||||
|
dfs_stack_push(ctx, n, s->s_block);
|
||||||
|
mie_vector_push_back(s->s_block->b_ipred, &n, NULL);
|
||||||
|
s = mie_block_get_next_successor(n, s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void link(struct lt_ctx *ctx, unsigned int parent, unsigned int node)
|
||||||
|
{
|
||||||
|
ctx->ancestor[node] = parent;
|
||||||
|
ctx->label[node] = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int eval(struct lt_ctx *ctx, unsigned int v)
|
||||||
|
{
|
||||||
|
unsigned int a = ctx->ancestor[v];
|
||||||
|
if (ctx->ancestor[a] > MIE_BLOCK_ID_INVALID) {
|
||||||
|
unsigned int b = eval(ctx, a);
|
||||||
|
ctx->ancestor[v] = ctx->ancestor[a];
|
||||||
|
if (ctx->semi[b] < ctx->semi[ctx->label[v]]) {
|
||||||
|
ctx->label[v] = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx->label[v];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void reset_block_relationship_metadata(struct mie_region *region)
|
||||||
|
{
|
||||||
|
b_queue_entry *cur = b_queue_first(®ion->r_blocks);
|
||||||
|
while (cur) {
|
||||||
|
struct mie_block *block = b_unbox(struct mie_block, cur, b_entry);
|
||||||
|
|
||||||
|
block->b_id = MIE_BLOCK_ID_INVALID;
|
||||||
|
block->b_idom = NULL;
|
||||||
|
// block->b_sdom = block->b_dfs_parent = NULL;
|
||||||
|
mie_vector_destroy(block->b_ipred, NULL);
|
||||||
|
|
||||||
|
cur = b_queue_next(cur);
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(®ion->r_blocks_s, 0x0, sizeof region->r_blocks_s);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void lt_ctx_init(struct lt_ctx *ctx, size_t nr_nodes)
|
||||||
|
{
|
||||||
|
memset(ctx, 0x0, sizeof *ctx);
|
||||||
|
ctx->idom = calloc(nr_nodes + 1, sizeof(unsigned int));
|
||||||
|
ctx->samedom = calloc(nr_nodes + 1, sizeof(unsigned int));
|
||||||
|
ctx->vertex = calloc(nr_nodes + 1, sizeof(struct mie_block *));
|
||||||
|
ctx->bucket = calloc(nr_nodes + 1, sizeof(struct set));
|
||||||
|
ctx->semi = calloc(nr_nodes + 1, sizeof(unsigned int));
|
||||||
|
ctx->parent = calloc(nr_nodes + 1, sizeof(unsigned int));
|
||||||
|
ctx->ancestor = calloc(nr_nodes + 1, sizeof(unsigned int));
|
||||||
|
ctx->label = calloc(nr_nodes + 1, sizeof(unsigned int));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void lt_ctx_cleanup(struct lt_ctx *ctx)
|
||||||
|
{
|
||||||
|
free(ctx->idom);
|
||||||
|
free(ctx->samedom);
|
||||||
|
free(ctx->vertex);
|
||||||
|
free(ctx->bucket);
|
||||||
|
free(ctx->semi);
|
||||||
|
free(ctx->parent);
|
||||||
|
free(ctx->ancestor);
|
||||||
|
free(ctx->label);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mie_region_refresh_dominance(struct mie_region *region)
|
||||||
|
{
|
||||||
|
struct lt_ctx ctx;
|
||||||
|
size_t nr_nodes = b_queue_length(®ion->r_blocks);
|
||||||
|
lt_ctx_init(&ctx, nr_nodes);
|
||||||
|
|
||||||
|
struct mie_block *root = mie_region_get_first_block(region);
|
||||||
|
|
||||||
|
reset_block_relationship_metadata(region);
|
||||||
|
dfs(&ctx, root);
|
||||||
|
|
||||||
|
for (size_t i = nr_nodes; i > 0; i--) {
|
||||||
|
struct mie_block *n = ctx.vertex[i];
|
||||||
|
unsigned int p = ctx.parent[i];
|
||||||
|
unsigned int s = p;
|
||||||
|
|
||||||
|
struct mie_block_predecessor *pred
|
||||||
|
= mie_block_get_first_predecessor(n);
|
||||||
|
while (pred) {
|
||||||
|
struct mie_block *v = pred->p_block;
|
||||||
|
unsigned int s_prime = MIE_BLOCK_ID_INVALID;
|
||||||
|
|
||||||
|
if (v->b_id <= n->b_id) {
|
||||||
|
s_prime = v->b_id;
|
||||||
|
} else {
|
||||||
|
s_prime = eval(&ctx, v->b_id);
|
||||||
|
s_prime = ctx.semi[s_prime];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s_prime < s) {
|
||||||
|
s = s_prime;
|
||||||
|
}
|
||||||
|
|
||||||
|
pred = mie_block_get_next_predecessor(n, pred);
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.semi[n->b_id] = s;
|
||||||
|
set_add(&ctx.bucket[s], n->b_id);
|
||||||
|
link(&ctx, p, n->b_id);
|
||||||
|
|
||||||
|
for (size_t k = 0; k < MIE_VECTOR_COUNT(ctx.bucket[p].v); k++) {
|
||||||
|
unsigned int v = ctx.bucket[p].v.items[k];
|
||||||
|
unsigned int y = eval(&ctx, v);
|
||||||
|
if (ctx.semi[y] == ctx.semi[p]) {
|
||||||
|
ctx.idom[v] = p;
|
||||||
|
} else {
|
||||||
|
ctx.samedom[v] = y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 1; i <= nr_nodes; i++) {
|
||||||
|
struct mie_block *n = ctx.vertex[i];
|
||||||
|
if (ctx.samedom[i] > 0) {
|
||||||
|
ctx.idom[i] = ctx.idom[ctx.samedom[i]];
|
||||||
|
}
|
||||||
|
|
||||||
|
n->b_idom = ctx.vertex[ctx.idom[i]];
|
||||||
|
// n->b_sdom = ctx.vertex[ctx.semi[i]];
|
||||||
|
// n->b_dfs_parent = ctx.vertex[ctx.parent[i]];
|
||||||
|
}
|
||||||
|
|
||||||
|
lt_ctx_cleanup(&ctx);
|
||||||
|
}
|
||||||
210
mie/parse/lex.c
210
mie/parse/lex.c
@@ -7,6 +7,10 @@
|
|||||||
#include <blue/ds/number.h>
|
#include <blue/ds/number.h>
|
||||||
#include <blue/ds/string.h>
|
#include <blue/ds/string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <mie/ctx.h>
|
||||||
|
#include <mie/diag/diag.h>
|
||||||
|
#include <mie/diag/highlight.h>
|
||||||
|
#include <mie/dialect/builtin.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -66,6 +70,14 @@ static struct mie_lex_symbol_node *get_symbol_node(
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct mie_diag *push_diag(struct mie_lex *lex, unsigned long diag_class)
|
||||||
|
{
|
||||||
|
return mie_ctx_push_diag(
|
||||||
|
lex->lex_ctx, lex->lex_source,
|
||||||
|
mie_line_source_get_cursor(lex->lex_source), "builtin",
|
||||||
|
diag_class);
|
||||||
|
}
|
||||||
|
|
||||||
static b_string *get_temp_string(struct mie_lex *lex)
|
static b_string *get_temp_string(struct mie_lex *lex)
|
||||||
{
|
{
|
||||||
if (!lex->lex_temp) {
|
if (!lex->lex_temp) {
|
||||||
@@ -145,7 +157,7 @@ static struct mie_lex_symbol_node *build_symbol_tree(void)
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct mie_lex *mie_lex_create(b_stream *src)
|
struct mie_lex *mie_lex_create(struct mie_line_source *src, struct mie_ctx *ctx)
|
||||||
{
|
{
|
||||||
struct mie_lex *lex = malloc(sizeof *lex);
|
struct mie_lex *lex = malloc(sizeof *lex);
|
||||||
if (!lex) {
|
if (!lex) {
|
||||||
@@ -154,11 +166,10 @@ struct mie_lex *mie_lex_create(b_stream *src)
|
|||||||
|
|
||||||
memset(lex, 0x0, sizeof *lex);
|
memset(lex, 0x0, sizeof *lex);
|
||||||
|
|
||||||
lex->lex_cursor_row = lex->lex_cursor_col = 1;
|
lex->lex_ctx = ctx;
|
||||||
|
|
||||||
lex->lex_status = MIE_SUCCESS;
|
lex->lex_status = MIE_SUCCESS;
|
||||||
lex->lex_source = src;
|
lex->lex_source = src;
|
||||||
lex->lex_linebuf = b_string_create();
|
|
||||||
|
|
||||||
lex->lex_sym_tree = build_symbol_tree();
|
lex->lex_sym_tree = build_symbol_tree();
|
||||||
if (!lex->lex_sym_tree) {
|
if (!lex->lex_sym_tree) {
|
||||||
@@ -184,10 +195,6 @@ void mie_lex_destroy(struct mie_lex *lex)
|
|||||||
entry = next;
|
entry = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lex->lex_linebuf) {
|
|
||||||
free(lex->lex_linebuf);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lex->lex_sym_tree) {
|
if (lex->lex_sym_tree) {
|
||||||
destroy_symbol_tree(lex->lex_sym_tree);
|
destroy_symbol_tree(lex->lex_sym_tree);
|
||||||
}
|
}
|
||||||
@@ -204,87 +211,9 @@ enum mie_status mie_lex_get_status(const struct mie_lex *lex)
|
|||||||
return lex->lex_status;
|
return lex->lex_status;
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum mie_status refill_linebuf(struct mie_lex *lex)
|
struct mie_line_source *mie_lex_get_line_source(const struct mie_lex *lex)
|
||||||
{
|
{
|
||||||
if (!lex->lex_source) {
|
return lex->lex_source;
|
||||||
return MIE_ERR_EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lex->lex_linebuf_ptr) {
|
|
||||||
b_iterator_unref(lex->lex_linebuf_ptr);
|
|
||||||
lex->lex_linebuf_ptr = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
b_stringstream *s = b_stringstream_create();
|
|
||||||
|
|
||||||
b_status status = b_stream_read_line_s(lex->lex_source, s);
|
|
||||||
|
|
||||||
if (status == B_ERR_NO_DATA) {
|
|
||||||
return MIE_ERR_EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!B_OK(status)) {
|
|
||||||
return MIE_ERR_INTERNAL_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
b_string_replace_all_with_stringstream(lex->lex_linebuf, s);
|
|
||||||
b_stringstream_unref(s);
|
|
||||||
|
|
||||||
lex->lex_linebuf_ptr = b_iterator_begin(lex->lex_linebuf);
|
|
||||||
|
|
||||||
return MIE_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int peek(struct mie_lex *lex)
|
|
||||||
{
|
|
||||||
enum mie_status status = MIE_SUCCESS;
|
|
||||||
|
|
||||||
if (!lex->lex_linebuf_ptr || !b_iterator_is_valid(lex->lex_linebuf_ptr)) {
|
|
||||||
status = refill_linebuf(lex);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (status != MIE_SUCCESS) {
|
|
||||||
return -status;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (b_string_get_size(lex->lex_linebuf, B_STRLEN_NORMAL) == 0) {
|
|
||||||
return -MIE_ERR_EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
b_wchar c = b_iterator_get_value(lex->lex_linebuf_ptr).v_int;
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int advance(struct mie_lex *lex)
|
|
||||||
{
|
|
||||||
enum mie_status status = MIE_SUCCESS;
|
|
||||||
|
|
||||||
if (!b_iterator_is_valid(lex->lex_linebuf_ptr)) {
|
|
||||||
status = refill_linebuf(lex);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (status != MIE_SUCCESS) {
|
|
||||||
return -status;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (b_string_get_size(lex->lex_linebuf, B_STRLEN_NORMAL) == 0) {
|
|
||||||
return -MIE_ERR_EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
b_wchar c = b_iterator_get_value(lex->lex_linebuf_ptr).v_int;
|
|
||||||
b_iterator_move_next(lex->lex_linebuf_ptr);
|
|
||||||
|
|
||||||
lex->lex_cursor_col++;
|
|
||||||
if (c == '\n') {
|
|
||||||
lex->lex_cursor_col = 1;
|
|
||||||
lex->lex_cursor_row++;
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool input_available(struct mie_lex *lex)
|
|
||||||
{
|
|
||||||
return lex->lex_linebuf_ptr && b_iterator_is_valid(lex->lex_linebuf_ptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool char_can_begin_symbol(char c)
|
static bool char_can_begin_symbol(char c)
|
||||||
@@ -313,22 +242,18 @@ static struct mie_token *create_token(enum mie_token_type type)
|
|||||||
|
|
||||||
static void set_token_start(struct mie_lex *lex)
|
static void set_token_start(struct mie_lex *lex)
|
||||||
{
|
{
|
||||||
lex->lex_token_start_row = lex->lex_cursor_row;
|
lex->lex_token_start = *mie_line_source_get_cursor(lex->lex_source);
|
||||||
lex->lex_token_start_col = lex->lex_cursor_col;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set_token_end(struct mie_lex *lex)
|
static void set_token_end(struct mie_lex *lex)
|
||||||
{
|
{
|
||||||
lex->lex_token_end_row = lex->lex_cursor_row;
|
lex->lex_token_end = *mie_line_source_get_cursor(lex->lex_source);
|
||||||
lex->lex_token_end_col = lex->lex_cursor_col;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum mie_status push_token(struct mie_lex *lex, struct mie_token *tok)
|
static enum mie_status push_token(struct mie_lex *lex, struct mie_token *tok)
|
||||||
{
|
{
|
||||||
tok->tok_location.s_start.c_row = lex->lex_token_start_row;
|
tok->tok_location.s_start = lex->lex_token_start;
|
||||||
tok->tok_location.s_start.c_col = lex->lex_token_start_col;
|
tok->tok_location.s_end = lex->lex_token_end;
|
||||||
tok->tok_location.s_end.c_row = lex->lex_token_end_row;
|
|
||||||
tok->tok_location.s_end.c_col = lex->lex_token_end_col;
|
|
||||||
|
|
||||||
b_queue_push_back(&lex->lex_queue, &tok->tok_entry);
|
b_queue_push_back(&lex->lex_queue, &tok->tok_entry);
|
||||||
return MIE_SUCCESS;
|
return MIE_SUCCESS;
|
||||||
@@ -423,7 +348,7 @@ static enum mie_status push_float(struct mie_lex *lex, double v)
|
|||||||
static enum mie_status read_line_comment(struct mie_lex *lex)
|
static enum mie_status read_line_comment(struct mie_lex *lex)
|
||||||
{
|
{
|
||||||
while (true) {
|
while (true) {
|
||||||
b_wchar c = advance(lex);
|
b_wchar c = mie_line_source_getc(lex->lex_source);
|
||||||
|
|
||||||
if (c == -MIE_ERR_EOF || c == '\n') {
|
if (c == -MIE_ERR_EOF || c == '\n') {
|
||||||
break;
|
break;
|
||||||
@@ -449,7 +374,7 @@ static enum mie_status read_number(struct mie_lex *lex, bool negate)
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
b_wchar c = peek(lex);
|
b_wchar c = mie_line_source_peekc(lex->lex_source);
|
||||||
if (c == -MIE_ERR_EOF) {
|
if (c == -MIE_ERR_EOF) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -461,7 +386,7 @@ static enum mie_status read_number(struct mie_lex *lex, bool negate)
|
|||||||
if (c == '_') {
|
if (c == '_') {
|
||||||
token_len++;
|
token_len++;
|
||||||
set_token_end(lex);
|
set_token_end(lex);
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -479,7 +404,7 @@ static enum mie_status read_number(struct mie_lex *lex, bool negate)
|
|||||||
char s[] = {c, 0};
|
char s[] = {c, 0};
|
||||||
b_string_append_cstr(str, s);
|
b_string_append_cstr(str, s);
|
||||||
set_token_end(lex);
|
set_token_end(lex);
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -491,7 +416,7 @@ static enum mie_status read_number(struct mie_lex *lex, bool negate)
|
|||||||
base = 16;
|
base = 16;
|
||||||
token_len++;
|
token_len++;
|
||||||
set_token_end(lex);
|
set_token_end(lex);
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -499,7 +424,7 @@ static enum mie_status read_number(struct mie_lex *lex, bool negate)
|
|||||||
base = 2;
|
base = 2;
|
||||||
token_len++;
|
token_len++;
|
||||||
set_token_end(lex);
|
set_token_end(lex);
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -517,7 +442,7 @@ static enum mie_status read_number(struct mie_lex *lex, bool negate)
|
|||||||
|
|
||||||
b_string_append_wc(str, c);
|
b_string_append_wc(str, c);
|
||||||
set_token_end(lex);
|
set_token_end(lex);
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
token_len++;
|
token_len++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -569,7 +494,7 @@ static enum mie_status read_ident(struct mie_lex *lex, enum mie_token_type type)
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
b_wchar c = peek(lex);
|
b_wchar c = mie_line_source_peekc(lex->lex_source);
|
||||||
|
|
||||||
if ((c == '.' || c == '-') && prev == c) {
|
if ((c == '.' || c == '-') && prev == c) {
|
||||||
return MIE_ERR_BAD_SYNTAX;
|
return MIE_ERR_BAD_SYNTAX;
|
||||||
@@ -586,7 +511,7 @@ static enum mie_status read_ident(struct mie_lex *lex, enum mie_token_type type)
|
|||||||
prev = c;
|
prev = c;
|
||||||
b_string_append_wc(str, c);
|
b_string_append_wc(str, c);
|
||||||
set_token_end(lex);
|
set_token_end(lex);
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == MIE_TOK_NONE) {
|
if (type == MIE_TOK_NONE) {
|
||||||
@@ -613,17 +538,17 @@ static enum mie_status read_string(struct mie_lex *lex)
|
|||||||
{
|
{
|
||||||
b_string *str = get_temp_string(lex);
|
b_string *str = get_temp_string(lex);
|
||||||
|
|
||||||
b_wchar c = peek(lex);
|
b_wchar c = mie_line_source_peekc(lex->lex_source);
|
||||||
bool esc = false;
|
bool esc = false;
|
||||||
|
|
||||||
if (c != '"') {
|
if (c != '"') {
|
||||||
return MIE_ERR_BAD_SYNTAX;
|
return MIE_ERR_BAD_SYNTAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
b_wchar c = peek(lex);
|
b_wchar c = mie_line_source_peekc(lex->lex_source);
|
||||||
|
|
||||||
if (esc) {
|
if (esc) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
@@ -636,23 +561,23 @@ static enum mie_status read_string(struct mie_lex *lex)
|
|||||||
}
|
}
|
||||||
|
|
||||||
esc = false;
|
esc = false;
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == '\\') {
|
if (c == '\\') {
|
||||||
esc = true;
|
esc = true;
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == '"') {
|
if (c == '"') {
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
b_string_append_wc(str, c);
|
b_string_append_wc(str, c);
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *s = b_string_steal(str);
|
char *s = b_string_steal(str);
|
||||||
@@ -666,7 +591,7 @@ static enum mie_status read_symbol(struct mie_lex *lex)
|
|||||||
b_wchar prev = 0;
|
b_wchar prev = 0;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
b_wchar c = peek(lex);
|
b_wchar c = mie_line_source_peekc(lex->lex_source);
|
||||||
if (c < 0) {
|
if (c < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -679,11 +604,12 @@ static enum mie_status read_symbol(struct mie_lex *lex)
|
|||||||
|
|
||||||
node = next;
|
node = next;
|
||||||
set_token_end(lex);
|
set_token_end(lex);
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
prev = c;
|
prev = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!node || node->s_def == NULL) {
|
if (!node || node->s_def == NULL) {
|
||||||
|
push_diag(lex, MIE_BUILTIN_E_UNRECOGNISED_TOKEN);
|
||||||
return MIE_ERR_BAD_SYNTAX;
|
return MIE_ERR_BAD_SYNTAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -734,11 +660,11 @@ static enum mie_status read_symbol(struct mie_lex *lex)
|
|||||||
|
|
||||||
static void skip_whitespace(struct mie_lex *lex)
|
static void skip_whitespace(struct mie_lex *lex)
|
||||||
{
|
{
|
||||||
b_wchar c = peek(lex);
|
b_wchar c = mie_line_source_peekc(lex->lex_source);
|
||||||
|
|
||||||
while (b_wchar_is_space(c)) {
|
while (b_wchar_is_space(c)) {
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
c = peek(lex);
|
c = mie_line_source_peekc(lex->lex_source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -755,34 +681,34 @@ static bool should_skip(b_wchar c, bool skip_linefeeds)
|
|||||||
|
|
||||||
static void skip_ignored_chars(struct mie_lex *lex, bool include_linefeeds)
|
static void skip_ignored_chars(struct mie_lex *lex, bool include_linefeeds)
|
||||||
{
|
{
|
||||||
b_wchar c = peek(lex);
|
b_wchar c = mie_line_source_peekc(lex->lex_source);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
while (should_skip(c, include_linefeeds)) {
|
while (should_skip(c, include_linefeeds)) {
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
c = peek(lex);
|
c = mie_line_source_peekc(lex->lex_source);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c != ';') {
|
if (c != ';') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
c = peek(lex);
|
c = mie_line_source_peekc(lex->lex_source);
|
||||||
|
|
||||||
while (c != '\n') {
|
while (c != '\n') {
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
c = peek(lex);
|
c = mie_line_source_peekc(lex->lex_source);
|
||||||
}
|
}
|
||||||
|
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
c = peek(lex);
|
c = mie_line_source_peekc(lex->lex_source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum mie_status pump_tokens(struct mie_lex *lex)
|
static enum mie_status pump_tokens(struct mie_lex *lex)
|
||||||
{
|
{
|
||||||
b_wchar c = peek(lex);
|
b_wchar c = mie_line_source_peekc(lex->lex_source);
|
||||||
|
|
||||||
if (c < 0) {
|
if (c < 0) {
|
||||||
return -c;
|
return -c;
|
||||||
@@ -795,13 +721,13 @@ static enum mie_status pump_tokens(struct mie_lex *lex)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
c = peek(lex);
|
c = mie_line_source_peekc(lex->lex_source);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == '\\') {
|
if (c == '\\') {
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
skip_ignored_chars(lex, true);
|
skip_ignored_chars(lex, true);
|
||||||
c = peek(lex);
|
c = mie_line_source_peekc(lex->lex_source);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == '\n') {
|
if (c == '\n') {
|
||||||
@@ -809,13 +735,13 @@ static enum mie_status pump_tokens(struct mie_lex *lex)
|
|||||||
set_token_end(lex);
|
set_token_end(lex);
|
||||||
|
|
||||||
while (c == '\n') {
|
while (c == '\n') {
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
|
|
||||||
if (!input_available(lex)) {
|
if (!mie_line_source_input_available(lex->lex_source)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
c = peek(lex);
|
c = mie_line_source_peekc(lex->lex_source);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c < 0) {
|
if (c < 0) {
|
||||||
@@ -826,8 +752,8 @@ static enum mie_status pump_tokens(struct mie_lex *lex)
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (b_wchar_is_space(c) && c != '\n') {
|
while (b_wchar_is_space(c) && c != '\n') {
|
||||||
advance(lex);
|
mie_line_source_getc(lex->lex_source);
|
||||||
c = peek(lex);
|
c = mie_line_source_peekc(lex->lex_source);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IS_VALID_IDENT_START_CHAR(c)) {
|
if (IS_VALID_IDENT_START_CHAR(c)) {
|
||||||
@@ -846,6 +772,20 @@ static enum mie_status pump_tokens(struct mie_lex *lex)
|
|||||||
return read_number(lex, false);
|
return read_number(lex, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct mie_diag *diag = push_diag(lex, MIE_BUILTIN_E_UNRECOGNISED_TOKEN);
|
||||||
|
mie_diag_push_msg(
|
||||||
|
diag, lex->lex_ctx, "builtin", MIE_BUILTIN_MSG_UNRECOGNISED_TOKEN);
|
||||||
|
const struct mie_file_cell *cursor
|
||||||
|
= mie_line_source_get_cursor(lex->lex_source);
|
||||||
|
unsigned long line = cursor->c_row;
|
||||||
|
const struct mie_diag_highlight hl[] = {
|
||||||
|
{
|
||||||
|
.hl_type = MIE_DIAG_HIGHLIGHT_ERROR,
|
||||||
|
.hl_span = {.s_start = *cursor, .s_end = *cursor},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const size_t nr_hl = sizeof hl / sizeof hl[0];
|
||||||
|
mie_diag_push_snippet(diag, line, line, NULL, 0, hl, nr_hl);
|
||||||
return MIE_ERR_BAD_SYNTAX;
|
return MIE_ERR_BAD_SYNTAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -893,7 +833,7 @@ bool mie_lex_tokens_available(struct mie_lex *lex)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input_available(lex)) {
|
if (mie_line_source_input_available(lex->lex_source)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,27 +5,24 @@
|
|||||||
#include <blue/ds/dict.h>
|
#include <blue/ds/dict.h>
|
||||||
#include <blue/ds/string.h>
|
#include <blue/ds/string.h>
|
||||||
#include <mie/parse/lex.h>
|
#include <mie/parse/lex.h>
|
||||||
|
#include <mie/parse/line-source.h>
|
||||||
#include <mie/parse/token.h>
|
#include <mie/parse/token.h>
|
||||||
#include <mie/status.h>
|
#include <mie/status.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
struct mie_lex {
|
struct mie_lex {
|
||||||
struct mie_lex_symbol_node *lex_sym_tree;
|
struct mie_lex_symbol_node *lex_sym_tree;
|
||||||
b_stream *lex_source;
|
struct mie_line_source *lex_source;
|
||||||
enum mie_status lex_status;
|
enum mie_status lex_status;
|
||||||
|
|
||||||
|
struct mie_ctx *lex_ctx;
|
||||||
b_queue lex_queue;
|
b_queue lex_queue;
|
||||||
|
|
||||||
b_string *lex_temp;
|
b_string *lex_temp;
|
||||||
b_queue lex_state;
|
b_queue lex_state;
|
||||||
unsigned int lex_brace_depth;
|
unsigned int lex_brace_depth;
|
||||||
|
|
||||||
unsigned long lex_token_start_row, lex_token_start_col;
|
struct mie_file_cell lex_token_start, lex_token_end;
|
||||||
unsigned long lex_token_end_row, lex_token_end_col;
|
|
||||||
unsigned long lex_cursor_row, lex_cursor_col;
|
|
||||||
|
|
||||||
b_string *lex_linebuf;
|
|
||||||
b_iterator *lex_linebuf_ptr;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct mie_lex_symbol_node {
|
struct mie_lex_symbol_node {
|
||||||
|
|||||||
161
mie/parse/line-source.c
Normal file
161
mie/parse/line-source.c
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
#include <mie/parse/line-source.h>
|
||||||
|
|
||||||
|
enum mie_status mie_line_source_init(
|
||||||
|
struct mie_line_source *src, const char *path, b_stream *stream)
|
||||||
|
{
|
||||||
|
memset(src, 0x0, sizeof *src);
|
||||||
|
|
||||||
|
src->s_lines = b_array_create();
|
||||||
|
|
||||||
|
if (!src->s_lines) {
|
||||||
|
return MIE_ERR_NO_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
|
src->s_stream = stream;
|
||||||
|
src->s_path = path;
|
||||||
|
src->s_cursor.c_col = 1;
|
||||||
|
src->s_cursor.c_row = 1;
|
||||||
|
|
||||||
|
return MIE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mie_line_source_cleanup(struct mie_line_source *src)
|
||||||
|
{
|
||||||
|
if (src->s_linebuf_ptr) {
|
||||||
|
b_iterator_unref(src->s_linebuf_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (src->s_lines) {
|
||||||
|
b_array_unref(src->s_lines);
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(src, 0x0, sizeof *src);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *mie_line_source_get_path(const struct mie_line_source *src)
|
||||||
|
{
|
||||||
|
return src->s_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
const struct mie_file_cell *mie_line_source_get_cursor(
|
||||||
|
const struct mie_line_source *src)
|
||||||
|
{
|
||||||
|
return &src->s_cursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
static enum mie_status refill_linebuf(struct mie_line_source *src)
|
||||||
|
{
|
||||||
|
if (!src->s_stream) {
|
||||||
|
return MIE_ERR_EOF;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (src->s_linebuf_ptr) {
|
||||||
|
b_iterator_unref(src->s_linebuf_ptr);
|
||||||
|
src->s_linebuf_ptr = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_stringstream *s = b_stringstream_create();
|
||||||
|
|
||||||
|
b_status status = b_stream_read_line_s(src->s_stream, s);
|
||||||
|
|
||||||
|
if (status == B_ERR_NO_DATA) {
|
||||||
|
return MIE_ERR_EOF;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!B_OK(status)) {
|
||||||
|
return MIE_ERR_INTERNAL_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_string *line = b_string_create();
|
||||||
|
b_string_replace_all_with_stringstream(line, s);
|
||||||
|
b_stringstream_unref(s);
|
||||||
|
|
||||||
|
b_array_append(src->s_lines, line);
|
||||||
|
b_string_unref(line);
|
||||||
|
|
||||||
|
src->s_linebuf = line;
|
||||||
|
src->s_linebuf_ptr = b_iterator_begin(src->s_linebuf);
|
||||||
|
|
||||||
|
return MIE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int peek(struct mie_line_source *src)
|
||||||
|
{
|
||||||
|
enum mie_status status = MIE_SUCCESS;
|
||||||
|
|
||||||
|
if (!src->s_linebuf_ptr || !b_iterator_is_valid(src->s_linebuf_ptr)) {
|
||||||
|
status = refill_linebuf(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status != MIE_SUCCESS) {
|
||||||
|
return -status;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (b_string_get_size(src->s_linebuf, B_STRLEN_NORMAL) == 0) {
|
||||||
|
return -MIE_ERR_EOF;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_wchar c = b_iterator_get_value(src->s_linebuf_ptr).v_int;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int advance(struct mie_line_source *src)
|
||||||
|
{
|
||||||
|
enum mie_status status = MIE_SUCCESS;
|
||||||
|
|
||||||
|
if (!b_iterator_is_valid(src->s_linebuf_ptr)) {
|
||||||
|
status = refill_linebuf(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status != MIE_SUCCESS) {
|
||||||
|
return -status;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (b_string_get_size(src->s_linebuf, B_STRLEN_NORMAL) == 0) {
|
||||||
|
return -MIE_ERR_EOF;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_wchar c = b_iterator_get_value(src->s_linebuf_ptr).v_int;
|
||||||
|
b_iterator_move_next(src->s_linebuf_ptr);
|
||||||
|
|
||||||
|
src->s_cursor.c_col++;
|
||||||
|
if (c == '\n') {
|
||||||
|
src->s_cursor.c_col = 1;
|
||||||
|
src->s_cursor.c_row++;
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_wchar mie_line_source_peekc(struct mie_line_source *src)
|
||||||
|
{
|
||||||
|
return peek(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
b_wchar mie_line_source_getc(struct mie_line_source *src)
|
||||||
|
{
|
||||||
|
return advance(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
enum mie_status mie_line_source_get_row(
|
||||||
|
struct mie_line_source *src, size_t row, const b_string **out)
|
||||||
|
{
|
||||||
|
if (row == 0) {
|
||||||
|
return MIE_ERR_INVALID_ARGUMENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
row--;
|
||||||
|
|
||||||
|
if (row >= b_array_size(src->s_lines)) {
|
||||||
|
return MIE_ERR_EOF;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_string *line = b_array_at(src->s_lines, row);
|
||||||
|
*out = line;
|
||||||
|
|
||||||
|
return MIE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mie_line_source_input_available(struct mie_line_source *src)
|
||||||
|
{
|
||||||
|
return src->s_linebuf_ptr && b_iterator_is_valid(src->s_linebuf_ptr);
|
||||||
|
}
|
||||||
@@ -260,11 +260,11 @@ bool mie_parser_parse_symbol(struct mie_parser *ctx, enum mie_token_symbol sym)
|
|||||||
bool mie_parser_parse_linefeed(struct mie_parser *ctx)
|
bool mie_parser_parse_linefeed(struct mie_parser *ctx)
|
||||||
{
|
{
|
||||||
struct mie_token *tok = mie_lex_peek(ctx->p_lex);
|
struct mie_token *tok = mie_lex_peek(ctx->p_lex);
|
||||||
if (tok->tok_type != MIE_TOK_LINEFEED) {
|
if (!MIE_TOKEN_IS(tok, MIE_TOK_LINEFEED)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (tok && tok->tok_type == MIE_TOK_LINEFEED) {
|
while (MIE_TOKEN_IS(tok, MIE_TOK_LINEFEED)) {
|
||||||
mie_lex_advance(ctx->p_lex);
|
mie_lex_advance(ctx->p_lex);
|
||||||
tok = mie_lex_peek(ctx->p_lex);
|
tok = mie_lex_peek(ctx->p_lex);
|
||||||
}
|
}
|
||||||
@@ -543,6 +543,8 @@ bool mie_parser_parse_operand(struct mie_parser *ctx, struct mie_op_arg *out)
|
|||||||
result = true;
|
result = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out->arg_span = loc;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -611,6 +613,8 @@ bool mie_parser_parse_parameter(struct mie_parser *ctx, struct mie_op_arg *out)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out->arg_span = loc;
|
||||||
|
|
||||||
if (!mie_parser_parse_symbol(ctx, MIE_SYM_COLON)) {
|
if (!mie_parser_parse_symbol(ctx, MIE_SYM_COLON)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -710,6 +714,7 @@ bool mie_parser_parse_register(
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out->reg_span = tok->tok_location;
|
||||||
mie_parser_advance(ctx);
|
mie_parser_advance(ctx);
|
||||||
|
|
||||||
return name != NULL;
|
return name != NULL;
|
||||||
@@ -927,6 +932,8 @@ bool mie_parser_parse_block(
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
memcpy(&block->b_name.n_span, &span, sizeof span);
|
||||||
|
|
||||||
if (mie_parser_peek_symbol(ctx) == MIE_SYM_LEFT_PAREN
|
if (mie_parser_peek_symbol(ctx) == MIE_SYM_LEFT_PAREN
|
||||||
&& !parse_block_parameters(ctx, names, block)) {
|
&& !parse_block_parameters(ctx, names, block)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -964,9 +971,8 @@ bool mie_parser_parse_successor(struct mie_parser *ctx, struct mie_op_successor
|
|||||||
memset(out, 0x0, sizeof *out);
|
memset(out, 0x0, sizeof *out);
|
||||||
b_string *str = get_temp_string(ctx);
|
b_string *str = get_temp_string(ctx);
|
||||||
bool result = false;
|
bool result = false;
|
||||||
struct mie_file_span span;
|
|
||||||
|
|
||||||
if (!mie_parser_parse_blockname(ctx, str, &span)) {
|
if (!mie_parser_parse_blockname(ctx, str, &out->s_name_span)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1159,6 +1165,15 @@ bool mie_parser_parse_op(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < MIE_VECTOR_COUNT(dest->op_result); i++) {
|
||||||
|
struct mie_register *reg = &dest->op_result.items[i];
|
||||||
|
reg->reg_flags |= MIE_REGISTER_F_OP_RESULT;
|
||||||
|
reg->reg_op = dest;
|
||||||
|
reg->reg_block = dest->op_container;
|
||||||
|
}
|
||||||
|
|
||||||
|
dest->op_src = mie_lex_get_line_source(ctx->p_lex);
|
||||||
|
|
||||||
if (mie_parser_check_type(ctx, MIE_TOK_NAME)) {
|
if (mie_parser_check_type(ctx, MIE_TOK_NAME)) {
|
||||||
/* custom-format operation */
|
/* custom-format operation */
|
||||||
return parse_custom_op(ctx, names, dest);
|
return parse_custom_op(ctx, names, dest);
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ static enum mie_match_result if_match(const struct mie_op *op)
|
|||||||
static struct mie_rewrite_result if_rewrite(
|
static struct mie_rewrite_result if_rewrite(
|
||||||
struct mie_op *op, struct mie_rewriter *rewriter)
|
struct mie_op *op, struct mie_rewriter *rewriter)
|
||||||
{
|
{
|
||||||
printf("if: rewriting %p %s.%s\n", op, op->op_dialect->d_name,
|
printf("if: rewriting %p %s.%s\n", op, op->op_info->op_parent->d_name,
|
||||||
op->op_info->op_name);
|
op->op_info->op_name);
|
||||||
|
|
||||||
struct mie_register *cond = mie_op_get_arg(op, 0);
|
struct mie_register *cond = mie_op_get_arg(op, 0);
|
||||||
@@ -111,7 +111,7 @@ static struct mie_pass_result transform(
|
|||||||
struct mie_pass *pass, struct mie_op *op, struct mie_pass_args *args)
|
struct mie_pass *pass, struct mie_op *op, struct mie_pass_args *args)
|
||||||
{
|
{
|
||||||
printf("%s: taking a look at %p %s.%s\n", mie_pass_get_name(pass), op,
|
printf("%s: taking a look at %p %s.%s\n", mie_pass_get_name(pass), op,
|
||||||
op->op_dialect->d_name, op->op_info->op_name);
|
op->op_info->op_parent->d_name, op->op_info->op_name);
|
||||||
|
|
||||||
struct mie_convert_config *cfg = mie_convert_config_create(args->p_ctx);
|
struct mie_convert_config *cfg = mie_convert_config_create(args->p_ctx);
|
||||||
mie_convert_config_add_illegal_op(cfg, "scf", "if");
|
mie_convert_config_add_illegal_op(cfg, "scf", "if");
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <mie/dialect/dialect.h>
|
#include <mie/dialect/dialect.h>
|
||||||
#include <mie/interface/interface-definition.h>
|
#include <mie/interface/interface-definition.h>
|
||||||
#include <mie/ir/block.h>
|
#include <mie/ir/block.h>
|
||||||
|
#include <mie/ir/op-definition.h>
|
||||||
#include <mie/ir/op.h>
|
#include <mie/ir/op.h>
|
||||||
#include <mie/ir/region.h>
|
#include <mie/ir/region.h>
|
||||||
#include <mie/ir/walk.h>
|
#include <mie/ir/walk.h>
|
||||||
@@ -153,7 +154,7 @@ static bool filter_check_op(
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filter->f_dialect && op->op_dialect != filter->f_dialect) {
|
if (filter->f_dialect && op->op_info->op_parent != filter->f_dialect) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,9 +11,13 @@ void mie_printer_print_op_arg(
|
|||||||
enum mie_register_flags arg_flags = 0;
|
enum mie_register_flags arg_flags = 0;
|
||||||
const char *arg_name = NULL;
|
const char *arg_name = NULL;
|
||||||
const struct mie_type *arg_type = NULL;
|
const struct mie_type *arg_type = NULL;
|
||||||
|
bool resolved = false;
|
||||||
|
|
||||||
if (MIE_TEST_FLAGS(arg->arg_flags, MIE_OP_F_ARG_RESOLVED)) {
|
if (MIE_TEST_FLAGS(arg->arg_flags, MIE_OP_F_ARG_RESOLVED)) {
|
||||||
|
resolved = true;
|
||||||
|
|
||||||
if (!arg->arg_value.u_reg) {
|
if (!arg->arg_value.u_reg) {
|
||||||
|
/* this should only be caused by an internal parser/rewriter bug */
|
||||||
arg_flags = 0;
|
arg_flags = 0;
|
||||||
arg_name = "<NULL>";
|
arg_name = "<NULL>";
|
||||||
arg_type = NULL;
|
arg_type = NULL;
|
||||||
@@ -36,6 +40,12 @@ void mie_printer_print_op_arg(
|
|||||||
|
|
||||||
b_stream_write_string(printer->p_stream, arg_name, NULL);
|
b_stream_write_string(printer->p_stream, arg_name, NULL);
|
||||||
|
|
||||||
|
if (!resolved
|
||||||
|
&& MIE_TEST_FLAGS(
|
||||||
|
printer->p_flags, MIE_PRINT_F_MARK_UNRESOLVED_ELEMENTS)) {
|
||||||
|
b_stream_write_char(printer->p_stream, '?');
|
||||||
|
}
|
||||||
|
|
||||||
if (!include_type || !arg_type) {
|
if (!include_type || !arg_type) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -50,14 +60,23 @@ void mie_printer_print_op_successor(
|
|||||||
bool compact)
|
bool compact)
|
||||||
{
|
{
|
||||||
b_stream_write_char(printer->p_stream, '^');
|
b_stream_write_char(printer->p_stream, '^');
|
||||||
|
bool resolved = false;
|
||||||
|
|
||||||
if (successor->s_flags & MIE_OP_F_SUCCESSOR_RESOLVED) {
|
if (successor->s_flags & MIE_OP_F_SUCCESSOR_RESOLVED) {
|
||||||
b_stream_write_string(
|
b_stream_write_string(
|
||||||
printer->p_stream, successor->s_block->b_name.n_str, NULL);
|
printer->p_stream, successor->s_block->b_name.n_str, NULL);
|
||||||
|
resolved = true;
|
||||||
} else {
|
} else {
|
||||||
b_stream_write_string(
|
b_stream_write_string(
|
||||||
printer->p_stream, successor->s_block_name, NULL);
|
printer->p_stream, successor->s_block_name, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!resolved
|
||||||
|
&& MIE_TEST_FLAGS(
|
||||||
|
printer->p_flags, MIE_PRINT_F_MARK_UNRESOLVED_ELEMENTS)) {
|
||||||
|
b_stream_write_char(printer->p_stream, '?');
|
||||||
|
}
|
||||||
|
|
||||||
if (MIE_VECTOR_COUNT(successor->s_args) == 0) {
|
if (MIE_VECTOR_COUNT(successor->s_args) == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -207,9 +226,14 @@ static void print_generic_op(struct mie_printer *printer, const struct mie_op *o
|
|||||||
if (op->op_flags & MIE_OP_F_OP_RESOLVED) {
|
if (op->op_flags & MIE_OP_F_OP_RESOLVED) {
|
||||||
b_stream_write_fmt(
|
b_stream_write_fmt(
|
||||||
printer->p_stream, NULL, "%s.%s",
|
printer->p_stream, NULL, "%s.%s",
|
||||||
op->op_dialect->d_name, op->op_info->op_name);
|
op->op_info->op_parent->d_name, op->op_info->op_name);
|
||||||
} else {
|
} else {
|
||||||
b_stream_write_string(printer->p_stream, op->op_name, NULL);
|
b_stream_write_string(printer->p_stream, op->op_name, NULL);
|
||||||
|
|
||||||
|
if (MIE_TEST_FLAGS(
|
||||||
|
printer->p_flags, MIE_PRINT_F_MARK_UNRESOLVED_ELEMENTS)) {
|
||||||
|
b_stream_write_char(printer->p_stream, '?');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
b_stream_write_char(printer->p_stream, '(');
|
b_stream_write_char(printer->p_stream, '(');
|
||||||
@@ -275,7 +299,7 @@ void mie_printer_print_op_name(struct mie_printer *printer, const struct mie_op
|
|||||||
if (printer->p_flags & MIE_PRINT_F_GENERIC) {
|
if (printer->p_flags & MIE_PRINT_F_GENERIC) {
|
||||||
generic = true;
|
generic = true;
|
||||||
b_stream_write_char(printer->p_stream, '~');
|
b_stream_write_char(printer->p_stream, '~');
|
||||||
} else if (!strcmp(op->op_dialect->d_name, "builtin")) {
|
} else if (!strcmp(op->op_info->op_parent->d_name, "builtin")) {
|
||||||
builtin = true;
|
builtin = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,7 +310,8 @@ void mie_printer_print_op_name(struct mie_printer *printer, const struct mie_op
|
|||||||
|
|
||||||
if (generic || !builtin || !abbreviate) {
|
if (generic || !builtin || !abbreviate) {
|
||||||
b_stream_write_fmt(
|
b_stream_write_fmt(
|
||||||
printer->p_stream, NULL, "%s.", op->op_dialect->d_name);
|
printer->p_stream, NULL, "%s.",
|
||||||
|
op->op_info->op_parent->d_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
b_stream_write_string(printer->p_stream, op->op_info->op_name, NULL);
|
b_stream_write_string(printer->p_stream, op->op_info->op_name, NULL);
|
||||||
|
|||||||
@@ -382,7 +382,6 @@ struct mie_op *mie_rewriter_put_op(
|
|||||||
|
|
||||||
op->op_flags = MIE_OP_F_OP_RESOLVED;
|
op->op_flags = MIE_OP_F_OP_RESOLVED;
|
||||||
op->op_info = op_def;
|
op->op_info = op_def;
|
||||||
op->op_dialect = op_def->op_parent;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < nr_args; i++) {
|
for (size_t i = 0; i < nr_args; i++) {
|
||||||
struct mie_op_arg *arg = mie_op_add_arg(op);
|
struct mie_op_arg *arg = mie_op_add_arg(op);
|
||||||
@@ -407,7 +406,6 @@ struct mie_op *mie_rewriter_replace_op(
|
|||||||
}
|
}
|
||||||
|
|
||||||
op->op_info = op_def;
|
op->op_info = op_def;
|
||||||
op->op_dialect = op_def->op_parent;
|
|
||||||
|
|
||||||
return op;
|
return op;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
file(GLOB tool_sources
|
file(GLOB tool_sources
|
||||||
*.c *.h
|
*.c *.h
|
||||||
cmd/*.c cmd/*.h
|
cmd/*.c cmd/*.h
|
||||||
|
diag/*.c diag/*.h
|
||||||
cmd/internal/*.c)
|
cmd/internal/*.c)
|
||||||
|
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
#include <blue/term.h>
|
#include <blue/term.h>
|
||||||
#include <mie/attribute/attribute-definition.h>
|
#include <mie/attribute/attribute-definition.h>
|
||||||
#include <mie/ctx.h>
|
#include <mie/ctx.h>
|
||||||
|
#include <mie/diag/class.h>
|
||||||
|
#include <mie/diag/msg.h>
|
||||||
#include <mie/dialect/arith.h>
|
#include <mie/dialect/arith.h>
|
||||||
#include <mie/dialect/builtin.h>
|
#include <mie/dialect/builtin.h>
|
||||||
#include <mie/dialect/cf.h>
|
#include <mie/dialect/cf.h>
|
||||||
@@ -29,108 +31,6 @@
|
|||||||
#include <mie/type/type-definition.h>
|
#include <mie/type/type-definition.h>
|
||||||
#include <mie/type/type.h>
|
#include <mie/type/type.h>
|
||||||
|
|
||||||
static int trait_ref_print(const struct mie_trait *trait, void *arg)
|
|
||||||
{
|
|
||||||
fputc(' ', stdout);
|
|
||||||
mie_trait_print(trait, b_stdout);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void op_arg_dump(struct mie_printer *printer, const struct mie_op_arg *arg)
|
|
||||||
{
|
|
||||||
enum mie_register_flags arg_flags = 0;
|
|
||||||
const char *arg_name = NULL;
|
|
||||||
const struct mie_type *arg_type = NULL;
|
|
||||||
|
|
||||||
if (MIE_TEST_FLAGS(arg->arg_flags, MIE_OP_F_ARG_RESOLVED)) {
|
|
||||||
arg_flags = arg->arg_value.u_reg->reg_flags;
|
|
||||||
arg_name = arg->arg_value.u_reg->reg_name.n_str;
|
|
||||||
arg_type = arg->arg_value.u_reg->reg_type;
|
|
||||||
} else {
|
|
||||||
arg_flags = arg->arg_unresolved.reg_flags;
|
|
||||||
arg_name = arg->arg_unresolved.reg_name;
|
|
||||||
arg_type = arg->arg_unresolved.reg_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (arg_flags & MIE_REGISTER_F_MACHINE) {
|
|
||||||
b_printf(" [bold,red]MR");
|
|
||||||
} else {
|
|
||||||
b_printf(" [bold,magenta]VR");
|
|
||||||
}
|
|
||||||
|
|
||||||
printf(":(");
|
|
||||||
mie_printer_print_type(printer, arg_type);
|
|
||||||
b_printf(")%s[reset]", arg_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void op_dump(const struct mie_op *op)
|
|
||||||
{
|
|
||||||
b_stringstream *tmp = b_stringstream_create();
|
|
||||||
struct mie_printer printer;
|
|
||||||
mie_printer_init(&printer, NULL, b_stdout, MIE_PRINT_F_GENERIC);
|
|
||||||
|
|
||||||
printf("FLAGS:");
|
|
||||||
(op->op_flags & MIE_OP_F_OP_RESOLVED) && printf(" OP_RESOLVED");
|
|
||||||
(op->op_flags & MIE_OP_F_ARG_RESOLVED) && printf(" ARG_RESOLVED");
|
|
||||||
printf("\n");
|
|
||||||
printf("DIALECT: %s\n",
|
|
||||||
op->op_dialect ? op->op_dialect->d_name : "<unknown>");
|
|
||||||
printf("OP: %s\n", op->op_info ? op->op_info->op_name : "<unknown>");
|
|
||||||
printf("NAME: %s\n", op->op_name ? op->op_name : "<unknown>");
|
|
||||||
printf("TRAITS:");
|
|
||||||
mie_trait_table_iterate(&op->op_info->op_traits, trait_ref_print, NULL);
|
|
||||||
printf("\n");
|
|
||||||
printf("ATTRIBUTES:");
|
|
||||||
#if 0
|
|
||||||
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_attrib); i++) {
|
|
||||||
printf(" (%s = ", op->op_attrib.items[i].attrib_name);
|
|
||||||
mie_printer_print_value(
|
|
||||||
&printer, op->op_attrib.items[i].attrib_value);
|
|
||||||
printf(")");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("REGIONS: %zu\n", b_queue_length(&op->op_regions));
|
|
||||||
printf("SUCCESSORS:");
|
|
||||||
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_successors); i++) {
|
|
||||||
const struct mie_op_successor *s = &op->op_successors.items[i];
|
|
||||||
if (MIE_TEST_FLAGS(s->s_flags, MIE_OP_F_SUCCESSOR_RESOLVED)) {
|
|
||||||
printf(" ^%s", s->s_block->b_name.n_str);
|
|
||||||
} else {
|
|
||||||
printf(" ^%s?", s->s_block_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf(":(");
|
|
||||||
|
|
||||||
for (size_t i = 0; i < MIE_VECTOR_COUNT(s->s_args); i++) {
|
|
||||||
const struct mie_op_arg *arg = &s->s_args.items[i];
|
|
||||||
op_arg_dump(&printer, arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf(" )");
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("ARGS:");
|
|
||||||
for (size_t i = 0; i < op->op_args.count; i++) {
|
|
||||||
const struct mie_op_arg *arg = &op->op_args.items[i];
|
|
||||||
op_arg_dump(&printer, arg);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("RESULT:");
|
|
||||||
for (size_t i = 0; i < MIE_VECTOR_COUNT(op->op_result); i++) {
|
|
||||||
const struct mie_register *reg = &op->op_result.items[i];
|
|
||||||
printf(" %s:(",
|
|
||||||
(reg->reg_flags & MIE_REGISTER_F_MACHINE) ? "MR" : "VR");
|
|
||||||
mie_printer_print_type(&printer, reg->reg_type);
|
|
||||||
printf(")%s", reg->reg_name.n_str);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mie_op_definition_print(const struct mie_op_definition *op)
|
static void mie_op_definition_print(const struct mie_op_definition *op)
|
||||||
{
|
{
|
||||||
char id_str[MIE_ID_STRING_MAX];
|
char id_str[MIE_ID_STRING_MAX];
|
||||||
@@ -187,6 +87,36 @@ static void mie_pass_definition_print(const struct mie_pass_definition *interfac
|
|||||||
interface->p_name, id_str);
|
interface->p_name, id_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void mie_diag_class_print(
|
||||||
|
const struct mie_dialect *dialect, const struct mie_diag_class *c)
|
||||||
|
{
|
||||||
|
switch (c->c_type) {
|
||||||
|
case MIE_DIAG_CLASS_HINT:
|
||||||
|
b_printf(" [bold,cyan]Hint:[reset]");
|
||||||
|
break;
|
||||||
|
case MIE_DIAG_CLASS_WARNING:
|
||||||
|
b_printf(" [bold,yellow]Warn:[reset]");
|
||||||
|
break;
|
||||||
|
case MIE_DIAG_CLASS_ERROR:
|
||||||
|
b_printf(" [bold,red]Err: [reset]");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_printf(
|
||||||
|
"%s.%-25s [green]%s[reset]\n", dialect->d_name,
|
||||||
|
c->c_id_str_short, c->c_title);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mie_diag_msg_print(
|
||||||
|
const struct mie_dialect *dialect, const struct mie_diag_msg *msg)
|
||||||
|
{
|
||||||
|
b_printf(
|
||||||
|
" [bold,blue]Msg: [reset]%s.%-25s [green]%s[reset]\n",
|
||||||
|
dialect->d_name, msg->msg_id_str_short, msg->msg_content);
|
||||||
|
}
|
||||||
|
|
||||||
static void mie_dialect_print(const struct mie_dialect *dialect)
|
static void mie_dialect_print(const struct mie_dialect *dialect)
|
||||||
{
|
{
|
||||||
char id_str[MIE_ID_STRING_MAX];
|
char id_str[MIE_ID_STRING_MAX];
|
||||||
@@ -269,6 +199,24 @@ static void mie_ctx_print(const struct mie_ctx *ctx)
|
|||||||
mie_pass_definition_print(pass);
|
mie_pass_definition_print(pass);
|
||||||
node = b_btree_next(node);
|
node = b_btree_next(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("\nDiagnostics:\n");
|
||||||
|
node = b_btree_first(&ctx->ctx_dialects.map_entries);
|
||||||
|
while (node) {
|
||||||
|
mie_id *id = b_unbox(mie_id, node, e_node);
|
||||||
|
struct mie_dialect *dialect
|
||||||
|
= b_unbox(struct mie_dialect, id, d_id);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < dialect->d_nr_diag_classes; i++) {
|
||||||
|
mie_diag_class_print(dialect, &dialect->d_diag_classes[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < dialect->d_nr_diag_msgs; i++) {
|
||||||
|
mie_diag_msg_print(dialect, &dialect->d_diag_msgs[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
node = b_btree_next(node);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ctx_dump(const b_command *cmd, const b_arglist *args, const b_array *_)
|
static int ctx_dump(const b_command *cmd, const b_arglist *args, const b_array *_)
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
#include <mie/ir/walk.h>
|
#include <mie/ir/walk.h>
|
||||||
#include <mie/name.h>
|
#include <mie/name.h>
|
||||||
#include <mie/parse/lex.h>
|
#include <mie/parse/lex.h>
|
||||||
|
#include <mie/parse/line-source.h>
|
||||||
#include <mie/parse/parser.h>
|
#include <mie/parse/parser.h>
|
||||||
#include <mie/parse/token.h>
|
#include <mie/parse/token.h>
|
||||||
#include <mie/pass/builtin.h>
|
#include <mie/pass/builtin.h>
|
||||||
@@ -86,7 +87,10 @@ static int optimise_file(
|
|||||||
|
|
||||||
mie_builtin_passes_register(ctx);
|
mie_builtin_passes_register(ctx);
|
||||||
|
|
||||||
struct mie_lex *lex = mie_lex_create(file);
|
struct mie_line_source src;
|
||||||
|
mie_line_source_init(&src, path, file);
|
||||||
|
|
||||||
|
struct mie_lex *lex = mie_lex_create(&src, ctx);
|
||||||
struct mie_parser *parse = mie_parser_create(ctx, lex);
|
struct mie_parser *parse = mie_parser_create(ctx, lex);
|
||||||
|
|
||||||
struct mie_name_map *names = mie_name_map_create(NULL);
|
struct mie_name_map *names = mie_name_map_create(NULL);
|
||||||
@@ -181,7 +185,7 @@ static int optimise_file(
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
mie_lex_destroy(lex);
|
mie_lex_destroy(lex);
|
||||||
|
mie_line_source_cleanup(&src);
|
||||||
b_file_unref(file);
|
b_file_unref(file);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include "../diag/diag.h"
|
||||||
#include "cmd.h"
|
#include "cmd.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@@ -26,6 +27,7 @@
|
|||||||
#include <mie/ir/walk.h>
|
#include <mie/ir/walk.h>
|
||||||
#include <mie/name.h>
|
#include <mie/name.h>
|
||||||
#include <mie/parse/lex.h>
|
#include <mie/parse/lex.h>
|
||||||
|
#include <mie/parse/line-source.h>
|
||||||
#include <mie/parse/parser.h>
|
#include <mie/parse/parser.h>
|
||||||
#include <mie/parse/token.h>
|
#include <mie/parse/token.h>
|
||||||
#include <mie/pass/builtin.h>
|
#include <mie/pass/builtin.h>
|
||||||
@@ -68,7 +70,10 @@ static int validate_file(const char *path, const b_arglist *args)
|
|||||||
|
|
||||||
mie_builtin_passes_register(ctx);
|
mie_builtin_passes_register(ctx);
|
||||||
|
|
||||||
struct mie_lex *lex = mie_lex_create(file);
|
struct mie_line_source src;
|
||||||
|
mie_line_source_init(&src, path, file);
|
||||||
|
|
||||||
|
struct mie_lex *lex = mie_lex_create(&src, ctx);
|
||||||
struct mie_parser *parse = mie_parser_create(ctx, lex);
|
struct mie_parser *parse = mie_parser_create(ctx, lex);
|
||||||
|
|
||||||
struct mie_name_map *names = mie_name_map_create(NULL);
|
struct mie_name_map *names = mie_name_map_create(NULL);
|
||||||
@@ -76,6 +81,13 @@ static int validate_file(const char *path, const b_arglist *args)
|
|||||||
struct mie_op *root = mie_op_create();
|
struct mie_op *root = mie_op_create();
|
||||||
|
|
||||||
if (!mie_parser_parse_op(parse, NULL, root)) {
|
if (!mie_parser_parse_op(parse, NULL, root)) {
|
||||||
|
struct mie_diag *diag = mie_ctx_pop_diag(ctx);
|
||||||
|
while (diag) {
|
||||||
|
mie_diag_write_pretty(diag, b_stdtty_err);
|
||||||
|
/* TODO cleanup */
|
||||||
|
diag = mie_ctx_pop_diag(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
printf("parse failed\n");
|
printf("parse failed\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -215,6 +227,8 @@ static int validate_file(const char *path, const b_arglist *args)
|
|||||||
|
|
||||||
mie_lex_destroy(lex);
|
mie_lex_destroy(lex);
|
||||||
|
|
||||||
|
mie_line_source_cleanup(&src);
|
||||||
|
|
||||||
b_file_unref(file);
|
b_file_unref(file);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
11
tool/diag/diag.h
Normal file
11
tool/diag/diag.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef _TOOL_DIAG_DIAG_H_
|
||||||
|
#define _TOOL_DIAG_DIAG_H_
|
||||||
|
|
||||||
|
#include <blue/term/tty.h>
|
||||||
|
#include <mie/status.h>
|
||||||
|
|
||||||
|
struct mie_diag;
|
||||||
|
|
||||||
|
extern enum mie_status mie_diag_write_pretty(struct mie_diag *diag, b_tty *stream);
|
||||||
|
|
||||||
|
#endif
|
||||||
0
tool/diag/json.c
Normal file
0
tool/diag/json.c
Normal file
461
tool/diag/pretty.c
Normal file
461
tool/diag/pretty.c
Normal file
@@ -0,0 +1,461 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <blue/core/stringstream.h>
|
||||||
|
#include <blue/term/print.h>
|
||||||
|
#include <blue/term/tty.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <mie/diag/amendment.h>
|
||||||
|
#include <mie/diag/class.h>
|
||||||
|
#include <mie/diag/component.h>
|
||||||
|
#include <mie/diag/diag.h>
|
||||||
|
#include <mie/diag/highlight.h>
|
||||||
|
#include <mie/parse/line-source.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define GET_CHAR_STOP 0
|
||||||
|
#define GET_CHAR_CONTINUE -1
|
||||||
|
|
||||||
|
struct snippet_print_ctx {
|
||||||
|
b_tty *ctx_stream;
|
||||||
|
struct mie_line_source *ctx_line_source;
|
||||||
|
|
||||||
|
size_t ctx_row, ctx_col;
|
||||||
|
|
||||||
|
bool ctx_has_underline;
|
||||||
|
b_stringstream *ctx_underline;
|
||||||
|
const b_string *ctx_linebuf;
|
||||||
|
const b_iterator *ctx_linebuf_ptr;
|
||||||
|
|
||||||
|
struct mie_diag_c_snippet *ctx_snippet;
|
||||||
|
|
||||||
|
const struct mie_diag_highlight *ctx_hl;
|
||||||
|
const struct mie_diag_amendment *ctx_amendment;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define __STREAM_COLOUR_ERROR "[red,bold]"
|
||||||
|
#define __STREAM_COLOUR_WARN "[yellow,bold]"
|
||||||
|
#define __STREAM_COLOUR_HINT "[blue,bold]"
|
||||||
|
#define __STREAM_COLOUR_LINE_NO "[cyan]"
|
||||||
|
#define __STREAM_COLOUR_ACCENT "[cyan]"
|
||||||
|
#define __STREAM_COLOUR_RESET "[reset]"
|
||||||
|
|
||||||
|
#define STREAM_COLOUR_ERROR(stream) \
|
||||||
|
b_tty_puts(stream, 0, __STREAM_COLOUR_ERROR);
|
||||||
|
#define STREAM_COLOUR_WARN(stream) b_tty_puts(stream, 0, __STREAM_COLOUR_WARN);
|
||||||
|
#define STREAM_COLOUR_HINT(stream) b_tty_puts(stream, 0, __STREAM_COLOUR_HINT);
|
||||||
|
#define STREAM_COLOUR_ACCENT(stream) \
|
||||||
|
b_tty_puts(stream, 0, __STREAM_COLOUR_ACCENT);
|
||||||
|
#define STREAM_COLOUR_LINE_NO(stream) \
|
||||||
|
b_tty_puts(stream, 0, __STREAM_COLOUR_LINE_NO);
|
||||||
|
#define STREAM_COLOUR_RESET(stream) \
|
||||||
|
b_tty_puts(stream, 0, __STREAM_COLOUR_RESET);
|
||||||
|
|
||||||
|
#define STREAM_COLOUR_ERROR_B(stream, str) \
|
||||||
|
b_stream_write_string(str, __STREAM_COLOUR_ERROR, NULL);
|
||||||
|
#define STREAM_COLOUR_WARN_B(stream, str) \
|
||||||
|
b_stream_write_string(str, __STREAM_COLOUR_WARN, NULL);
|
||||||
|
#define STREAM_COLOUR_HINT_B(stream, str) \
|
||||||
|
b_stream_write_string(str, __STREAM_COLOUR_HINT, NULL);
|
||||||
|
#define STREAM_COLOUR_ACCENT_B(stream, str) \
|
||||||
|
b_stream_write_string(str, __STREAM_COLOUR_ACCENT, NULL);
|
||||||
|
#define STREAM_COLOUR_RESET_B(stream, str) \
|
||||||
|
b_stream_write_string(str, __STREAM_COLOUR_RESET, NULL);
|
||||||
|
|
||||||
|
static void print_header(struct mie_diag *diag, b_tty *dest)
|
||||||
|
{
|
||||||
|
unsigned int w;
|
||||||
|
b_tty_get_dimensions(dest, &w, NULL);
|
||||||
|
|
||||||
|
const struct mie_diag_class *diag_class = diag->diag_class;
|
||||||
|
assert(diag_class);
|
||||||
|
|
||||||
|
STREAM_COLOUR_ACCENT(dest);
|
||||||
|
|
||||||
|
size_t nr_written = 0;
|
||||||
|
nr_written += b_tty_puts(dest, 0, "--- ");
|
||||||
|
|
||||||
|
const char *s = diag_class->c_title;
|
||||||
|
while (*s) {
|
||||||
|
b_tty_putc(dest, 0, toupper(*s));
|
||||||
|
s++;
|
||||||
|
nr_written++;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_tty_putc(dest, 0, ' ');
|
||||||
|
nr_written++;
|
||||||
|
|
||||||
|
while (nr_written < w) {
|
||||||
|
b_tty_putc(dest, 0, '-');
|
||||||
|
nr_written++;
|
||||||
|
}
|
||||||
|
|
||||||
|
STREAM_COLOUR_RESET(dest);
|
||||||
|
|
||||||
|
b_tty_putc(dest, 0, '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_location(struct mie_diag *diag, b_tty *out)
|
||||||
|
{
|
||||||
|
size_t x;
|
||||||
|
const char *name = mie_line_source_get_path(diag->diag_src);
|
||||||
|
|
||||||
|
b_tty_puts(out, 0, " ");
|
||||||
|
STREAM_COLOUR_ACCENT(out);
|
||||||
|
b_tty_puts(out, 0, "> ");
|
||||||
|
STREAM_COLOUR_RESET(out);
|
||||||
|
b_tty_printf(
|
||||||
|
out, 0, "%s:%lu:%lu\n", name, diag->diag_loc.c_row,
|
||||||
|
diag->diag_loc.c_col);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_msg(
|
||||||
|
struct mie_diag *diag, struct mie_diag_c_msg *msg, b_tty *stream)
|
||||||
|
{
|
||||||
|
b_paragraph_format format = {
|
||||||
|
.p_left_margin = 2,
|
||||||
|
.p_right_margin = 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
b_print_paragraph(msg->msg_content, stream, &format);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool highlight_contains_cell(
|
||||||
|
const struct mie_diag_highlight *hl, size_t row, size_t col)
|
||||||
|
{
|
||||||
|
if (row < hl->hl_span.s_start.c_row || row > hl->hl_span.s_end.c_row) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (row == hl->hl_span.s_start.c_row && col < hl->hl_span.s_start.c_col) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (row == hl->hl_span.s_end.c_row && col > hl->hl_span.s_end.c_col) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool amendment_contains_cell(
|
||||||
|
const struct mie_diag_amendment *a, size_t row, size_t col)
|
||||||
|
{
|
||||||
|
unsigned long limit = 0;
|
||||||
|
|
||||||
|
switch (a->a_type) {
|
||||||
|
case MIE_DIAG_AMENDMENT_ADD:
|
||||||
|
if (a->a_add.a_loc.c_row != row) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (col < a->a_add.a_loc.c_col) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (col > a->a_add.a_loc.c_col + a->__x - 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
case MIE_DIAG_AMENDMENT_REMOVE:
|
||||||
|
if (a->a_remove.a_loc.c_row != row) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (col < a->a_remove.a_loc.c_row) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (col > a->a_remove.a_length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
case MIE_DIAG_AMENDMENT_REPLACE:
|
||||||
|
if (a->a_replace.a_loc.c_row != row) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (col < a->a_replace.a_loc.c_row) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
limit = a->a_replace.a_loc.c_row
|
||||||
|
+ b_max(ulong, a->a_replace.a_length, a->__x) - 1;
|
||||||
|
|
||||||
|
if (col > limit) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct mie_diag_highlight *find_highlight(
|
||||||
|
struct mie_diag_c_snippet *snippet, size_t row, size_t col)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < snippet->s_nr_highlights; i++) {
|
||||||
|
struct mie_diag_highlight *hl = &snippet->s_highlights[i];
|
||||||
|
|
||||||
|
if (highlight_contains_cell(hl, row, col)) {
|
||||||
|
return hl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct mie_diag_amendment *find_amendment(
|
||||||
|
struct mie_diag_c_snippet *snippet, size_t row, size_t col)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < snippet->s_nr_amendments; i++) {
|
||||||
|
struct mie_diag_amendment *a = &snippet->s_amendments[i];
|
||||||
|
|
||||||
|
if (amendment_contains_cell(a, row, col)) {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void update_amendment(struct snippet_print_ctx *ctx)
|
||||||
|
{
|
||||||
|
if (ctx->ctx_amendment
|
||||||
|
&& amendment_contains_cell(
|
||||||
|
ctx->ctx_amendment, ctx->ctx_row, ctx->ctx_col)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx->ctx_amendment
|
||||||
|
= find_amendment(ctx->ctx_snippet, ctx->ctx_row, ctx->ctx_col);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void update_highlighting(struct snippet_print_ctx *ctx)
|
||||||
|
{
|
||||||
|
if (ctx->ctx_hl
|
||||||
|
&& highlight_contains_cell(ctx->ctx_hl, ctx->ctx_row, ctx->ctx_col)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx->ctx_hl = NULL;
|
||||||
|
|
||||||
|
STREAM_COLOUR_RESET(ctx->ctx_stream);
|
||||||
|
|
||||||
|
const struct mie_diag_highlight *new_hl
|
||||||
|
= find_highlight(ctx->ctx_snippet, ctx->ctx_row, ctx->ctx_col);
|
||||||
|
|
||||||
|
if (!new_hl) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx->ctx_has_underline = true;
|
||||||
|
ctx->ctx_hl = new_hl;
|
||||||
|
|
||||||
|
switch (ctx->ctx_hl->hl_type) {
|
||||||
|
case MIE_DIAG_HIGHLIGHT_ERROR:
|
||||||
|
STREAM_COLOUR_ERROR(ctx->ctx_stream);
|
||||||
|
STREAM_COLOUR_ERROR_B(ctx->ctx_stream, ctx->ctx_underline);
|
||||||
|
break;
|
||||||
|
case MIE_DIAG_HIGHLIGHT_WARNING:
|
||||||
|
STREAM_COLOUR_WARN(ctx->ctx_stream);
|
||||||
|
STREAM_COLOUR_WARN_B(ctx->ctx_stream, ctx->ctx_underline);
|
||||||
|
break;
|
||||||
|
case MIE_DIAG_HIGHLIGHT_HINT:
|
||||||
|
STREAM_COLOUR_HINT(ctx->ctx_stream);
|
||||||
|
STREAM_COLOUR_HINT_B(ctx->ctx_stream, ctx->ctx_underline);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static enum mie_status read_row(struct snippet_print_ctx *ctx, size_t row)
|
||||||
|
{
|
||||||
|
if (ctx->ctx_linebuf_ptr) {
|
||||||
|
b_iterator_unref(ctx->ctx_linebuf_ptr);
|
||||||
|
ctx->ctx_linebuf_ptr = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx->ctx_has_underline = false;
|
||||||
|
|
||||||
|
size_t nr_read;
|
||||||
|
enum mie_status status = mie_line_source_get_row(
|
||||||
|
ctx->ctx_line_source, row, &ctx->ctx_linebuf);
|
||||||
|
if (status != MIE_SUCCESS) {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx->ctx_linebuf_ptr = b_iterator_cbegin(ctx->ctx_linebuf);
|
||||||
|
return MIE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int get_char_amendment(struct snippet_print_ctx *ctx)
|
||||||
|
{
|
||||||
|
const struct mie_diag_amendment *a = ctx->ctx_amendment;
|
||||||
|
size_t i = 0;
|
||||||
|
int c = 0;
|
||||||
|
|
||||||
|
switch (a->a_type) {
|
||||||
|
case MIE_DIAG_AMENDMENT_ADD:
|
||||||
|
i = ctx->ctx_col - a->a_add.a_loc.c_row;
|
||||||
|
return a->a_add.a_str[i];
|
||||||
|
case MIE_DIAG_AMENDMENT_REMOVE:
|
||||||
|
b_iterator_move_next(ctx->ctx_linebuf_ptr);
|
||||||
|
return GET_CHAR_CONTINUE;
|
||||||
|
case MIE_DIAG_AMENDMENT_REPLACE:
|
||||||
|
i = ctx->ctx_col - a->a_add.a_loc.c_col;
|
||||||
|
|
||||||
|
if (i > a->__x) {
|
||||||
|
i = a->__x;
|
||||||
|
}
|
||||||
|
|
||||||
|
c = a->a_replace.a_str[i];
|
||||||
|
if (i < a->a_replace.a_length) {
|
||||||
|
b_iterator_move_next(ctx->ctx_linebuf_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c != 0) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
return GET_CHAR_CONTINUE;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return GET_CHAR_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int get_char(struct snippet_print_ctx *ctx)
|
||||||
|
{
|
||||||
|
if (ctx->ctx_amendment) {
|
||||||
|
return get_char_amendment(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
b_wchar c = b_iterator_get_cvalue(ctx->ctx_linebuf_ptr).v_int;
|
||||||
|
if (c == B_WCHAR_INVALID) {
|
||||||
|
return GET_CHAR_STOP;
|
||||||
|
};
|
||||||
|
|
||||||
|
b_iterator_move_next(ctx->ctx_linebuf_ptr);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void update_underline(struct snippet_print_ctx *ctx)
|
||||||
|
{
|
||||||
|
if (!ctx->ctx_hl) {
|
||||||
|
b_stream_write_char(ctx->ctx_underline, ' ');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (ctx->ctx_hl->hl_type) {
|
||||||
|
case MIE_DIAG_HIGHLIGHT_ERROR:
|
||||||
|
case MIE_DIAG_HIGHLIGHT_WARNING:
|
||||||
|
case MIE_DIAG_HIGHLIGHT_HINT:
|
||||||
|
b_stream_write_char(ctx->ctx_underline, '^');
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
b_stream_write_char(ctx->ctx_underline, ' ');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_underline(struct snippet_print_ctx *ctx)
|
||||||
|
{
|
||||||
|
if (!ctx->ctx_has_underline) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
STREAM_COLOUR_RESET(ctx->ctx_stream);
|
||||||
|
STREAM_COLOUR_LINE_NO(ctx->ctx_stream);
|
||||||
|
b_tty_puts(ctx->ctx_stream, 0, " : ");
|
||||||
|
b_tty_puts(ctx->ctx_stream, 0, b_stringstream_ptr(ctx->ctx_underline));
|
||||||
|
STREAM_COLOUR_RESET(ctx->ctx_stream);
|
||||||
|
b_tty_putc(ctx->ctx_stream, 0, '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_snippet(
|
||||||
|
struct mie_diag *diag, struct mie_diag_c_snippet *snippet, b_tty *stream)
|
||||||
|
{
|
||||||
|
enum mie_status status = MIE_SUCCESS;
|
||||||
|
|
||||||
|
struct snippet_print_ctx printer = {
|
||||||
|
.ctx_stream = stream,
|
||||||
|
.ctx_snippet = snippet,
|
||||||
|
.ctx_underline = b_stringstream_create(),
|
||||||
|
.ctx_line_source = diag->diag_src,
|
||||||
|
};
|
||||||
|
|
||||||
|
for (unsigned long row = snippet->s_first_line;
|
||||||
|
row <= snippet->s_last_line; row++) {
|
||||||
|
STREAM_COLOUR_LINE_NO(stream);
|
||||||
|
b_tty_printf(stream, 0, " %4lu | ", row);
|
||||||
|
printer.ctx_row = row;
|
||||||
|
|
||||||
|
status = read_row(&printer, row);
|
||||||
|
if (status != MIE_SUCCESS) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0;; i++) {
|
||||||
|
printer.ctx_col = i + 1;
|
||||||
|
|
||||||
|
update_amendment(&printer);
|
||||||
|
|
||||||
|
int c = get_char(&printer);
|
||||||
|
|
||||||
|
if (c == GET_CHAR_CONTINUE) {
|
||||||
|
continue;
|
||||||
|
} else if (c == GET_CHAR_STOP || c == '\n') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
update_highlighting(&printer);
|
||||||
|
|
||||||
|
b_tty_putc(stream, 0, c);
|
||||||
|
|
||||||
|
update_underline(&printer);
|
||||||
|
}
|
||||||
|
|
||||||
|
b_tty_putc(stream, 0, '\n');
|
||||||
|
write_underline(&printer);
|
||||||
|
}
|
||||||
|
|
||||||
|
b_stringstream_unref(printer.ctx_underline);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_component(
|
||||||
|
struct mie_diag *diag, struct mie_diag_component *component, b_tty *stream)
|
||||||
|
{
|
||||||
|
switch (component->c_type) {
|
||||||
|
case MIE_DIAG_COMPONENT_MSG:
|
||||||
|
print_msg(diag, (struct mie_diag_c_msg *)component, stream);
|
||||||
|
break;
|
||||||
|
case MIE_DIAG_COMPONENT_SNIPPET:
|
||||||
|
print_snippet(diag, (struct mie_diag_c_snippet *)component, stream);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum mie_status mie_diag_write_pretty(struct mie_diag *diag, b_tty *stream)
|
||||||
|
{
|
||||||
|
print_header(diag, stream);
|
||||||
|
print_location(diag, stream);
|
||||||
|
|
||||||
|
b_queue_entry *entry = b_queue_first(&diag->diag_components);
|
||||||
|
while (entry) {
|
||||||
|
struct mie_diag_component *c
|
||||||
|
= b_unbox(struct mie_diag_component, entry, c_entry);
|
||||||
|
|
||||||
|
b_tty_putc(stream, 0, '\n');
|
||||||
|
print_component(diag, c, stream);
|
||||||
|
|
||||||
|
entry = b_queue_next(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
return MIE_SUCCESS;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user