lang: start implementing code generation (ast to ir) facility
This commit is contained in:
@@ -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})
|
||||
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})
|
||||
|
||||
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;
|
||||
}
|
||||
18
lang/codegen/codegen.h
Normal file
18
lang/codegen/codegen.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef _LANG_CODEGEN_H_
|
||||
#define _LANG_CODEGEN_H_
|
||||
|
||||
#include <blue/core/queue.h>
|
||||
|
||||
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
|
||||
22
lang/include/ivy/lang/codegen.h
Normal file
22
lang/include/ivy/lang/codegen.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef IVY_LANG_CODEGEN_H_
|
||||
#define IVY_LANG_CODEGEN_H_
|
||||
|
||||
#include <ivy/misc.h>
|
||||
#include <ivy/status.h>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user