347 lines
8.1 KiB
C
347 lines
8.1 KiB
C
#include "../debug.h"
|
|
#include "cmd.h"
|
|
#include "mie/select/builder.h"
|
|
|
|
#include <blue/cmd.h>
|
|
#include <blue/term.h>
|
|
#include <errno.h>
|
|
#include <ivy/diag.h>
|
|
#include <ivy/file.h>
|
|
#include <ivy/lang/ast.h>
|
|
#include <ivy/lang/codegen.h>
|
|
#include <ivy/lang/diag.h>
|
|
#include <ivy/lang/lex.h>
|
|
#include <mie/ctx.h>
|
|
#include <mie/ir/block.h>
|
|
#include <mie/ir/convert.h>
|
|
#include <mie/ir/func.h>
|
|
#include <mie/ir/module.h>
|
|
#include <mie/select/builder.h>
|
|
#include <mie/select/graph.h>
|
|
#include <stdio.h>
|
|
|
|
enum {
|
|
ARG_SOURCE_FILE = 200,
|
|
OPT_SHOW_LEX_TOKENS,
|
|
OPT_SHOW_AST_NODES,
|
|
OPT_SHOW_IR,
|
|
OPT_SHOW_ISEL_PRE,
|
|
OPT_SHOW_ISEL_POST,
|
|
};
|
|
|
|
struct codegen_args {
|
|
struct ivy_codegen *args_gen;
|
|
bool args_print_ast;
|
|
};
|
|
|
|
static enum ivy_status node_codegen(
|
|
struct ivy_ast_node *node, enum ivy_ast_iteration_type iteration_type,
|
|
struct ivy_ast_node_iterator *it, void *arg)
|
|
{
|
|
if (iteration_type != IVY_AST_ITERATION_PRE) {
|
|
return IVY_OK;
|
|
}
|
|
|
|
struct codegen_args *args = arg;
|
|
struct ivy_codegen *gen = args->args_gen;
|
|
if (args->args_print_ast) {
|
|
print_ast_node(node, iteration_type, it, NULL);
|
|
}
|
|
|
|
return ivy_codegen_push_node(gen, node, node->n_it.it_depth);
|
|
}
|
|
|
|
static int compile_file(const char *path, const b_arglist *args)
|
|
{
|
|
bool show_lex = b_arglist_get_count(
|
|
args, OPT_SHOW_LEX_TOKENS, B_COMMAND_INVALID_ID)
|
|
> 0;
|
|
bool show_ast = b_arglist_get_count(
|
|
args, OPT_SHOW_AST_NODES, B_COMMAND_INVALID_ID)
|
|
> 0;
|
|
bool show_ir
|
|
= b_arglist_get_count(args, OPT_SHOW_IR, B_COMMAND_INVALID_ID) > 0;
|
|
bool show_isel_pre
|
|
= b_arglist_get_count(args, OPT_SHOW_ISEL_PRE, B_COMMAND_INVALID_ID)
|
|
> 0;
|
|
enum ivy_status status = IVY_OK;
|
|
|
|
struct ivy_file *src = NULL;
|
|
status = ivy_file_open(path, &src);
|
|
if (!src) {
|
|
b_err("cannot open source file '%s'", path);
|
|
b_i("reason: %s", ivy_status_to_string(status));
|
|
return -1;
|
|
}
|
|
|
|
struct ivy_diag_stream diag_stream;
|
|
ivy_diag_stream_init_tty(&diag_stream, b_stdtty);
|
|
|
|
struct ivy_diag_ctx *diag = NULL;
|
|
status = ivy_diag_ctx_create(&diag);
|
|
if (status != IVY_OK) {
|
|
b_err("failed to initialise Ivy diagnostic context");
|
|
ivy_file_close(src);
|
|
return -1;
|
|
}
|
|
|
|
ivy_lang_diag_ctx_init(diag);
|
|
ivy_diag_ctx_set_line_source(diag, &src->f_base);
|
|
|
|
struct ivy_lexer *lex = NULL;
|
|
if (ivy_lexer_create(&lex) != IVY_OK) {
|
|
b_err("failed to initialise Ivy lexer");
|
|
ivy_file_close(src);
|
|
return -1;
|
|
}
|
|
|
|
ivy_lexer_set_source(lex, &src->f_base);
|
|
ivy_lexer_set_diag_ctx(lex, diag);
|
|
|
|
struct ivy_parser *parser = NULL;
|
|
status = ivy_parser_create(&parser);
|
|
|
|
if (status != IVY_OK) {
|
|
b_err("failed to initialise Ivy parser");
|
|
ivy_lexer_destroy(lex);
|
|
ivy_file_close(src);
|
|
return -1;
|
|
}
|
|
|
|
ivy_parser_set_diag_ctx(parser, diag);
|
|
|
|
struct mie_ctx *ctx = mie_ctx_create();
|
|
|
|
struct ivy_codegen *codegen;
|
|
status = ivy_codegen_create(ctx, &codegen);
|
|
|
|
if (status != IVY_OK) {
|
|
b_err("failed to initialise Ivy code generator");
|
|
ivy_parser_destroy(parser, NULL);
|
|
ivy_lexer_destroy(lex);
|
|
ivy_file_close(src);
|
|
return -1;
|
|
}
|
|
|
|
while (true) {
|
|
struct ivy_token *tok = ivy_lexer_read(lex);
|
|
status = ivy_lexer_get_status(lex);
|
|
if (status == IVY_ERR_EOF) {
|
|
break;
|
|
}
|
|
|
|
if (status != IVY_OK) {
|
|
ivy_diag_ctx_write(
|
|
diag, IVY_DIAG_FORMAT_PRETTY, &diag_stream);
|
|
break;
|
|
}
|
|
|
|
if (show_lex) {
|
|
print_lex_token(tok);
|
|
}
|
|
|
|
status = ivy_parser_push_token(parser, tok);
|
|
|
|
if (status == IVY_ERR_BAD_SYNTAX) {
|
|
ivy_diag_ctx_write(
|
|
diag, IVY_DIAG_FORMAT_PRETTY, &diag_stream);
|
|
ivy_token_destroy(tok);
|
|
break;
|
|
}
|
|
|
|
if (status != IVY_OK) {
|
|
b_err("failed to parse '%s'", path);
|
|
b_i("reason: parse error (%s)",
|
|
ivy_status_to_string(status));
|
|
ivy_token_destroy(tok);
|
|
break;
|
|
}
|
|
}
|
|
|
|
int r = (status == IVY_OK || status == IVY_ERR_EOF) ? 0 : -1;
|
|
ivy_file_close(src);
|
|
ivy_lexer_destroy(lex);
|
|
|
|
if (r != 0) {
|
|
return r;
|
|
}
|
|
|
|
if (r != 0) {
|
|
ivy_parser_destroy(parser, NULL);
|
|
return r;
|
|
}
|
|
|
|
if (show_ast && !show_ir) {
|
|
struct ivy_ast_node *root = ivy_parser_root_node(parser);
|
|
struct ivy_ast_node_iterator it = {0};
|
|
ivy_ast_node_iterate(root, &it, print_ast_node, NULL);
|
|
}
|
|
|
|
ivy_codegen_start_module(codegen);
|
|
|
|
struct ivy_ast_node *node = ivy_parser_root_node(parser);
|
|
struct ivy_ast_node_iterator it = {0};
|
|
struct codegen_args gen_args = {
|
|
.args_gen = codegen,
|
|
.args_print_ast = show_ast,
|
|
};
|
|
|
|
status = ivy_ast_node_iterate(node, &it, node_codegen, &gen_args);
|
|
if (status != IVY_OK) {
|
|
printf("codegen failed: error %s\n", ivy_status_to_string(status));
|
|
return -1;
|
|
}
|
|
|
|
ivy_codegen_push_eof(codegen);
|
|
|
|
struct mie_module *mod = NULL;
|
|
ivy_codegen_end_module(codegen, &mod);
|
|
|
|
if (show_ir) {
|
|
struct mie_ir_converter *convert
|
|
= mie_ir_converter_create(ctx, MIE_IR_MEM, MIE_IR_TEXT);
|
|
mie_ir_converter_set_src_value(convert, MIE_VALUE(mod));
|
|
mie_ir_converter_set_dest_file(convert, stdout);
|
|
mie_ir_converter_process(convert);
|
|
|
|
mie_ir_converter_destroy(convert);
|
|
}
|
|
|
|
struct mie_select_builder *builder = mie_select_builder_create(ctx);
|
|
|
|
b_queue_iterator func_it;
|
|
b_queue_foreach (&func_it, &mod->m_func) {
|
|
struct mie_value *func_v
|
|
= b_unbox(struct mie_value, func_it.entry, v_entry);
|
|
struct mie_func *func = (struct mie_func *)func_v;
|
|
|
|
b_queue_iterator block_it;
|
|
b_queue_foreach (&block_it, &func->f_blocks) {
|
|
struct mie_value *block_v = b_unbox(
|
|
struct mie_value, block_it.entry, v_entry);
|
|
struct mie_block *block = (struct mie_block *)block_v;
|
|
|
|
b_queue_iterator instr_it;
|
|
b_queue_foreach (&instr_it, &block->b_phi) {
|
|
struct mie_value *instr_v = b_unbox(
|
|
struct mie_value, instr_it.entry, v_entry);
|
|
mie_select_builder_push_instr(
|
|
builder, (struct mie_instr *)instr_v);
|
|
}
|
|
|
|
b_queue_foreach (&instr_it, &block->b_instr) {
|
|
struct mie_value *instr_v = b_unbox(
|
|
struct mie_value, instr_it.entry, v_entry);
|
|
mie_select_builder_push_instr(
|
|
builder, (struct mie_instr *)instr_v);
|
|
}
|
|
|
|
if (block->b_terminator) {
|
|
mie_select_builder_push_instr(
|
|
builder, block->b_terminator);
|
|
}
|
|
|
|
struct mie_select_graph *graph
|
|
= mie_select_builder_finish(builder);
|
|
|
|
if (show_isel_pre) {
|
|
printf("%s.%s instruction graph:\n",
|
|
func->f_base.v_name.n_str,
|
|
block->b_base.v_name.n_str);
|
|
mie_select_graph_dump_dot(graph);
|
|
mie_select_graph_destroy(graph);
|
|
}
|
|
}
|
|
}
|
|
|
|
mie_value_destroy(MIE_VALUE(mod));
|
|
ivy_codegen_destroy(codegen);
|
|
|
|
ivy_parser_destroy(parser, NULL);
|
|
|
|
return r;
|
|
}
|
|
|
|
static int compile(const b_command *cmd, const b_arglist *args, const b_array *_)
|
|
{
|
|
b_arglist_iterator it;
|
|
b_arglist_foreach_filtered(&it, args, B_COMMAND_INVALID_ID, ARG_SOURCE_FILE)
|
|
{
|
|
b_arglist_value *path = it.value;
|
|
if (path->val_type != B_COMMAND_ARG_STRING) {
|
|
continue;
|
|
}
|
|
|
|
int r = compile_file(path->val_str, args);
|
|
if (r != 0) {
|
|
return r;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
B_COMMAND(CMD_COMPILE, CMD_ROOT)
|
|
{
|
|
B_COMMAND_NAME("compile");
|
|
B_COMMAND_SHORT_NAME('C');
|
|
B_COMMAND_DESC(
|
|
"compile one or more Ivy source files into Ivy object files.");
|
|
B_COMMAND_FLAGS(B_COMMAND_SHOW_HELP_BY_DEFAULT);
|
|
B_COMMAND_FUNCTION(compile);
|
|
|
|
B_COMMAND_OPTION(OPT_SHOW_LEX_TOKENS)
|
|
{
|
|
B_OPTION_LONG_NAME("show-lex");
|
|
B_OPTION_SHORT_NAME('l');
|
|
B_OPTION_DESC(
|
|
"print the lexical tokens generated from the input "
|
|
"files.");
|
|
}
|
|
|
|
B_COMMAND_OPTION(OPT_SHOW_AST_NODES)
|
|
{
|
|
B_OPTION_LONG_NAME("show-ast");
|
|
B_OPTION_SHORT_NAME('a');
|
|
B_OPTION_DESC(
|
|
"print the abstract syntax tree generated from the "
|
|
"input files.");
|
|
}
|
|
|
|
B_COMMAND_OPTION(OPT_SHOW_IR)
|
|
{
|
|
B_OPTION_LONG_NAME("show-ir");
|
|
B_OPTION_SHORT_NAME('i');
|
|
B_OPTION_DESC(
|
|
"print the Mie IR generated from the "
|
|
"input files.");
|
|
}
|
|
|
|
B_COMMAND_OPTION(OPT_SHOW_ISEL_PRE)
|
|
{
|
|
B_OPTION_LONG_NAME("show-isel-pre");
|
|
B_OPTION_SHORT_NAME('s');
|
|
B_OPTION_DESC(
|
|
"print the instruction selection graph before "
|
|
"selection has taken place.");
|
|
}
|
|
|
|
B_COMMAND_OPTION(OPT_SHOW_ISEL_POST)
|
|
{
|
|
B_OPTION_LONG_NAME("show-isel-post");
|
|
B_OPTION_SHORT_NAME('m');
|
|
B_OPTION_DESC(
|
|
"print the instruction selection graph after "
|
|
"selection has taken place.");
|
|
}
|
|
|
|
B_COMMAND_ARG(ARG_SOURCE_FILE)
|
|
{
|
|
B_ARG_NAME("source file");
|
|
B_ARG_DESC("the .im source files to compile.");
|
|
B_ARG_NR_VALUES(B_ARG_1_OR_MORE_VALUES);
|
|
}
|
|
|
|
B_COMMAND_HELP_OPTION();
|
|
}
|