lang: start implementing code generation (ast to ir) facility
This commit is contained in:
69
lang/codegen/codegen.c
Normal file
69
lang/codegen/codegen.c
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "codegen.h"
|
||||
|
||||
#include <ivy/lang/codegen.h>
|
||||
#include <mie/builder.h>
|
||||
#include <mie/module.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
enum ivy_status ivy_codegen_create(struct ivy_codegen **out)
|
||||
{
|
||||
struct ivy_codegen *gen = malloc(sizeof *gen);
|
||||
if (!gen) {
|
||||
return IVY_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
memset(gen, 0x0, sizeof *gen);
|
||||
|
||||
gen->c_ctx = mie_ctx_create();
|
||||
|
||||
*out = gen;
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
void ivy_codegen_destroy(struct ivy_codegen *gen)
|
||||
{
|
||||
if (gen->c_module) {
|
||||
mie_value_destroy(MIE_VALUE(gen->c_module));
|
||||
}
|
||||
|
||||
if (gen->c_builder) {
|
||||
mie_builder_destroy(gen->c_builder);
|
||||
}
|
||||
|
||||
if (gen->c_ctx) {
|
||||
mie_ctx_destroy(gen->c_ctx);
|
||||
}
|
||||
|
||||
free(gen);
|
||||
}
|
||||
|
||||
enum ivy_status ivy_codegen_start_module(struct ivy_codegen *gen)
|
||||
{
|
||||
if (gen->c_module || gen->c_builder) {
|
||||
return IVY_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
gen->c_module = mie_module_create();
|
||||
gen->c_builder = mie_builder_create(gen->c_ctx, gen->c_module);
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
enum ivy_status ivy_codegen_end_module(
|
||||
struct ivy_codegen *gen, struct mie_module **out)
|
||||
{
|
||||
if (!gen->c_module) {
|
||||
return IVY_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
struct mie_module *module = gen->c_module;
|
||||
mie_builder_destroy(gen->c_builder);
|
||||
|
||||
gen->c_module = NULL;
|
||||
gen->c_builder = NULL;
|
||||
|
||||
*out = module;
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
Reference in New Issue
Block a user