2025-04-14 09:48:16 +01:00
|
|
|
#ifndef _LANG_CODEGEN_H_
|
|
|
|
|
#define _LANG_CODEGEN_H_
|
|
|
|
|
|
|
|
|
|
#include <blue/core/queue.h>
|
2025-04-14 20:15:05 +01:00
|
|
|
#include <ivy/lang/ast.h>
|
|
|
|
|
#include <ivy/lang/lex.h>
|
|
|
|
|
#include <mie/builder.h>
|
2025-04-14 09:48:16 +01:00
|
|
|
|
2025-04-14 20:15:05 +01:00
|
|
|
#define CODEGEN_RESULT_OK(flags) \
|
|
|
|
|
(struct code_generator_result) \
|
|
|
|
|
{ \
|
|
|
|
|
.r_status = (IVY_OK), .r_flags = (flags) \
|
|
|
|
|
}
|
|
|
|
|
#define CODEGEN_RESULT_ERR(status) \
|
|
|
|
|
(struct code_generator_result) \
|
|
|
|
|
{ \
|
|
|
|
|
.r_status = (status), .r_flags = (0) \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ivy_codegen;
|
|
|
|
|
struct ivy_ast_node;
|
|
|
|
|
|
|
|
|
|
struct mie_value;
|
2025-04-14 09:48:16 +01:00
|
|
|
struct mie_func;
|
|
|
|
|
|
2025-04-14 20:15:05 +01:00
|
|
|
enum code_generator_type {
|
|
|
|
|
CODE_GENERATOR_NONE = 0,
|
|
|
|
|
CODE_GENERATOR_UNIT,
|
|
|
|
|
CODE_GENERATOR_EXPR,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct code_generator_result {
|
|
|
|
|
enum ivy_status r_status;
|
|
|
|
|
|
|
|
|
|
enum code_generator_result_flags {
|
|
|
|
|
CODEGEN_REPEAT_NODE = 0x01u,
|
|
|
|
|
} r_flags;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct code_generator_state;
|
|
|
|
|
|
|
|
|
|
typedef enum ivy_status (*code_generator_state_init_callback)(
|
|
|
|
|
struct ivy_codegen *, struct code_generator_state *);
|
|
|
|
|
typedef enum ivy_status (*code_generator_state_fini_callback)(
|
|
|
|
|
struct ivy_codegen *, struct code_generator_state *);
|
|
|
|
|
typedef struct code_generator_result (*code_generator_callback)(
|
|
|
|
|
struct ivy_codegen *);
|
|
|
|
|
typedef struct code_generator_result (*code_generator_node_callback)(
|
|
|
|
|
struct ivy_codegen *, struct code_generator_state *, struct ivy_ast_node *);
|
|
|
|
|
|
|
|
|
|
struct code_generator {
|
|
|
|
|
enum code_generator_type g_type;
|
|
|
|
|
size_t g_state_size;
|
|
|
|
|
|
|
|
|
|
code_generator_state_init_callback g_state_init;
|
|
|
|
|
code_generator_state_fini_callback g_state_fini;
|
|
|
|
|
code_generator_node_callback g_node_generators_pre[IVY_AST_TYPE_COUNT];
|
|
|
|
|
code_generator_node_callback g_node_generators_post[IVY_AST_TYPE_COUNT];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct code_generator_state {
|
|
|
|
|
b_queue_entry s_entry;
|
|
|
|
|
void *s_arg;
|
|
|
|
|
const struct code_generator *s_gen;
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-14 09:48:16 +01:00
|
|
|
struct ivy_codegen {
|
|
|
|
|
b_queue c_ir_values;
|
2025-04-14 20:15:05 +01:00
|
|
|
b_queue c_state;
|
2025-04-14 09:48:16 +01:00
|
|
|
struct mie_builder *c_builder;
|
|
|
|
|
struct mie_ctx *c_ctx;
|
|
|
|
|
struct mie_module *c_module;
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-14 20:15:05 +01:00
|
|
|
extern const struct code_generator *get_code_generator(
|
|
|
|
|
enum code_generator_type type);
|
|
|
|
|
|
|
|
|
|
extern enum ivy_status codegen_push_generator(
|
|
|
|
|
struct ivy_codegen *gen, enum code_generator_type gen_type, void *arg);
|
|
|
|
|
extern enum ivy_status codegen_pop_generator(struct ivy_codegen *gen);
|
|
|
|
|
|
|
|
|
|
extern void codegen_push_value(struct ivy_codegen *gen, struct mie_value *value);
|
|
|
|
|
extern struct mie_value *codegen_pop_value(struct ivy_codegen *gen);
|
|
|
|
|
|
2025-04-14 09:48:16 +01:00
|
|
|
#endif
|