Files
mie/mie/print/block.c
Max Wash 00ba3a3d87 mie: print: implement exception-printing of resolved, but null, op args
this is to make it easier to spot mistakes made when modifying IR.
2026-01-25 15:02:48 +00:00

97 lines
2.3 KiB
C

#include <assert.h>
#include <mie/ir/block.h>
#include <mie/ir/op.h>
#include <mie/ir/register.h>
#include <mie/print/printer.h>
static enum mie_status cleanup(void *p)
{
return MIE_SUCCESS;
}
static enum mie_status move(void *dst, void *src, size_t itemsz)
{
assert(itemsz == sizeof(struct mie_block));
struct mie_block *dest_block = dst, *src_block = src;
memmove(dest_block, src_block, sizeof *src_block);
mie_name_move(&dest_block->b_name, &src_block->b_name);
return MIE_SUCCESS;
}
struct mie_vector_ops mie_block_vector_ops = {
.v_destroy = cleanup,
.v_move = move,
};
static void print_block_header_params(
struct mie_printer *printer, const struct mie_block *block)
{
if (!MIE_VECTOR_COUNT(block->b_params)) {
return;
}
b_stream_write_char(printer->p_stream, '(');
for (size_t i = 0; i < MIE_VECTOR_COUNT(block->b_params); i++) {
if (i > 0) {
b_stream_write_string(printer->p_stream, ", ", NULL);
}
mie_printer_print_register(printer, &block->b_params.items[i], 0);
b_stream_write_string(printer->p_stream, ": ", NULL);
mie_printer_print_type(printer, block->b_params.items[i].reg_type);
}
b_stream_write_char(printer->p_stream, ')');
}
static void print_block_header(
struct mie_printer *printer, const struct mie_block *block)
{
bool first_block = b_queue_prev(&block->b_entry) == NULL;
if (first_block && !block->b_name.n_str) {
return;
}
b_stream_write_char(printer->p_stream, '^');
if (block->b_name.n_str) {
b_stream_write_string(printer->p_stream, block->b_name.n_str, NULL);
} else {
b_stream_write_string(printer->p_stream, "<UNNAMED>", NULL);
}
print_block_header_params(printer, block);
b_stream_write_string(printer->p_stream, ":\n", NULL);
}
void mie_printer_print_block(
struct mie_printer *printer, const struct mie_block *block,
enum mie_print_flags flags)
{
if (!(flags & MIE_PRINT_F_EXCLUDE_BLOCK_HEADER)) {
print_block_header(printer, block);
}
b_stream_push_indent(printer->p_stream, 4);
struct mie_op *op = mie_block_get_first_op(block);
while (op) {
mie_printer_print_op(printer, op);
b_stream_write_char(printer->p_stream, '\n');
op = mie_block_get_next_op(block, op);
}
b_stream_pop_indent(printer->p_stream);
}
void mie_printer_print_block_ref(
struct mie_printer *printer, const struct mie_block *block)
{
print_block_header(printer, block);
}