137 lines
4.0 KiB
C
137 lines
4.0 KiB
C
#include <mie/attribute/attribute-map.h>
|
|
#include <mie/ctx.h>
|
|
#include <mie/dialect/builtin.h>
|
|
#include <mie/dialect/dialect.h>
|
|
#include <mie/dialect/func.h>
|
|
#include <mie/interface/interface-definition.h>
|
|
#include <mie/interface/interface.h>
|
|
#include <mie/ir/block.h>
|
|
#include <mie/ir/emit.h>
|
|
#include <mie/ir/op-definition.h>
|
|
#include <mie/ir/op.h>
|
|
#include <mie/ir/region.h>
|
|
#include <mie/macros.h>
|
|
#include <mie/print/printer.h>
|
|
#include <mie/type/function.h>
|
|
|
|
static enum mie_status print(struct mie_printer *printer, const struct mie_op *op)
|
|
{
|
|
const struct mie_attribute *sym_name
|
|
= mie_attribute_map_get(&op->op_attrib, "sym_name");
|
|
const struct mie_attribute *function_type_attr
|
|
= mie_attribute_map_get(&op->op_attrib, "function_type");
|
|
const char *sym_name_cstr = mie_string_get_cstr(sym_name);
|
|
const struct mie_type *function_type_g
|
|
= mie_type_attr_get_type(function_type_attr);
|
|
const struct mie_function_type *function_ty
|
|
= (const struct mie_function_type *)function_type_g;
|
|
|
|
b_stream_write_fmt(printer->p_stream, NULL, " @%s(", sym_name_cstr);
|
|
|
|
const struct mie_region *code = mie_op_get_first_region(op);
|
|
const struct mie_block *entry = mie_region_get_first_block(code);
|
|
|
|
for (size_t i = 0; i < MIE_VECTOR_COUNT(entry->b_params); i++) {
|
|
if (i > 0) {
|
|
b_stream_write_string(printer->p_stream, ", ", NULL);
|
|
}
|
|
|
|
const struct mie_register *param = &entry->b_params.items[i];
|
|
|
|
mie_printer_print_register(printer, param, MIE_PRINT_F_INCLUDE_TYPE);
|
|
}
|
|
|
|
b_stream_write_string(printer->p_stream, ") -> ", NULL);
|
|
|
|
if (MIE_VECTOR_COUNT(function_ty->func_out) != 1) {
|
|
b_stream_write_char(printer->p_stream, '(');
|
|
}
|
|
|
|
for (size_t i = 0; i < MIE_VECTOR_COUNT(function_ty->func_out); i++) {
|
|
if (i > 0) {
|
|
b_stream_write_string(printer->p_stream, ", ", NULL);
|
|
}
|
|
|
|
const struct mie_type *ty = function_ty->func_out.items[i];
|
|
|
|
mie_printer_print_type(printer, ty);
|
|
}
|
|
|
|
if (MIE_VECTOR_COUNT(function_ty->func_out) != 1) {
|
|
b_stream_write_char(printer->p_stream, ')');
|
|
}
|
|
|
|
b_stream_write_char(printer->p_stream, ' ');
|
|
|
|
mie_printer_print_region(
|
|
printer, code, MIE_PRINT_F_EXCLUDE_FIRST_BLOCK_HEADER);
|
|
|
|
return MIE_SUCCESS;
|
|
}
|
|
|
|
static enum mie_status parse(struct mie_parser *parser, struct mie_op *out)
|
|
{
|
|
return MIE_SUCCESS;
|
|
}
|
|
|
|
struct mie_op *mie_func_func_put(
|
|
struct mie_emitter *e, const char *name, struct mie_func_parameter *params,
|
|
size_t nr_params, const struct mie_type **ret_types, size_t nr_ret_types)
|
|
{
|
|
const struct mie_type **param_types
|
|
= calloc(nr_params, sizeof *param_types);
|
|
if (!param_types) {
|
|
return NULL;
|
|
}
|
|
|
|
for (size_t i = 0; i < nr_params; i++) {
|
|
param_types[i] = params[i].param_type;
|
|
}
|
|
|
|
const struct mie_type *func_type = mie_ctx_get_function_type(
|
|
mie_emitter_get_ctx(e), param_types, nr_params, ret_types,
|
|
nr_ret_types);
|
|
free(param_types);
|
|
|
|
if (!func_type) {
|
|
return NULL;
|
|
}
|
|
|
|
struct mie_op *op = mie_emitter_put_op(e, "func", "func", NULL, 0);
|
|
|
|
struct mie_region *region = mie_op_add_region(op);
|
|
struct mie_block *entry = mie_region_add_block(region);
|
|
mie_name_map_put(region->r_names, &entry->b_name, "entry", 0);
|
|
|
|
for (size_t i = 0; i < nr_params; i++) {
|
|
struct mie_register *param_reg = mie_block_add_param(entry);
|
|
mie_name_map_put(
|
|
region->r_names, ¶m_reg->reg_name,
|
|
params[i].param_name, 0);
|
|
param_reg->reg_type = params[i].param_type;
|
|
params[i].param_reg = param_reg;
|
|
}
|
|
|
|
struct mie_attribute *sym_name
|
|
= mie_ctx_get_string(mie_emitter_get_ctx(e), name);
|
|
struct mie_attribute *function_type
|
|
= mie_type_attr_create(mie_emitter_get_ctx(e), func_type);
|
|
|
|
mie_attribute_map_put(
|
|
&op->op_attrib, "sym_name", sym_name, MIE_ATTRMAP_F_REPLACE);
|
|
mie_attribute_map_put(
|
|
&op->op_attrib, "function_type", function_type,
|
|
MIE_ATTRMAP_F_REPLACE);
|
|
|
|
return op;
|
|
}
|
|
|
|
MIE_OP_DEFINITION_BEGIN(mie_func_func, "func")
|
|
MIE_OP_DEFINITION_PRINT(print);
|
|
MIE_OP_DEFINITION_PARSE(parse);
|
|
MIE_OP_DEFINITION_TRAIT("builtin", "isolated-from-above");
|
|
MIE_OP_INTERFACE_BEGIN("builtin", "symbol", struct mie_symbol)
|
|
MIE_OP_INTERFACE_FUNC(sym_get_name) = NULL;
|
|
MIE_OP_INTERFACE_END()
|
|
MIE_OP_DEFINITION_END()
|