diff --git a/lang/CMakeLists.txt b/lang/CMakeLists.txt index a1bae23..d26b2d3 100644 --- a/lang/CMakeLists.txt +++ b/lang/CMakeLists.txt @@ -11,5 +11,5 @@ else () endif () target_include_directories(ivy-lang PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include/) -target_link_libraries(ivy-lang ivy-common Bluelib::Core Bluelib::Object Bluelib::Term) -target_compile_definitions(ivy-lang PRIVATE IVY_EXPORT=1 IVY_STATIC=${IVY_STATIC}) \ No newline at end of file +target_link_libraries(ivy-lang mie ivy-common Bluelib::Core Bluelib::Object Bluelib::Term) +target_compile_definitions(ivy-lang PRIVATE IVY_EXPORT=1 IVY_STATIC=${IVY_STATIC}) diff --git a/lang/codegen/codegen.c b/lang/codegen/codegen.c new file mode 100644 index 0000000..2268530 --- /dev/null +++ b/lang/codegen/codegen.c @@ -0,0 +1,69 @@ +#include "codegen.h" + +#include +#include +#include +#include +#include + +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; +} diff --git a/lang/codegen/codegen.h b/lang/codegen/codegen.h new file mode 100644 index 0000000..4b52a9f --- /dev/null +++ b/lang/codegen/codegen.h @@ -0,0 +1,18 @@ +#ifndef _LANG_CODEGEN_H_ +#define _LANG_CODEGEN_H_ + +#include + +struct mie_builder; +struct mie_ctx; +struct mie_func; + +struct ivy_codegen { + b_queue c_ir_values; + struct mie_builder *c_builder; + struct mie_ctx *c_ctx; + struct mie_module *c_module; + struct mie_func *c_immediate; +}; + +#endif diff --git a/lang/include/ivy/lang/codegen.h b/lang/include/ivy/lang/codegen.h new file mode 100644 index 0000000..9667626 --- /dev/null +++ b/lang/include/ivy/lang/codegen.h @@ -0,0 +1,22 @@ +#ifndef IVY_LANG_CODEGEN_H_ +#define IVY_LANG_CODEGEN_H_ + +#include +#include + +struct ivy_codegen; +struct ivy_ast_node; + +struct mie_module; + +IVY_API enum ivy_status ivy_codegen_create(struct ivy_codegen **out); +IVY_API void ivy_codegen_destroy(struct ivy_codegen *gen); + +IVY_API enum ivy_status ivy_codegen_start_module(struct ivy_codegen *gen); +IVY_API enum ivy_status ivy_codegen_end_module( + struct ivy_codegen *gen, struct mie_module **out); + +IVY_API enum ivy_status ivy_codegen_push_node( + struct ivy_codegen *gen, struct ivy_ast_node *node); + +#endif