issue #98 Start work generating linear IR

parser
Dibyendu Majumdar 4 years ago
parent 21310c9162
commit 226ef82d0f

@ -54,7 +54,7 @@ struct ptr_list_iter {
/* The ptr list */
extern int ptrlist_size(const struct ptr_list *self);
extern void **ptrlist_add(struct ptr_list **self, void *ptr, struct allocator *alloc);
extern void **ptrlist_add(struct ptr_list **self, void *ptr, struct allocator *ptr_list_allocator);
extern void *ptrlist_nth_entry(struct ptr_list *list, unsigned int idx);
extern void *ptrlist_first(struct ptr_list *list);
extern void *ptrlist_last(struct ptr_list *list);

@ -331,6 +331,8 @@ struct basic_block {
};
#define CFG_FIELDS \
unsigned node_count; \
struct node **nodes; \
struct node *entry; \
struct node *exit
@ -343,6 +345,7 @@ struct proc {
CFG_FIELDS;
struct proc_list* procs; /* procs defined in this proc */
struct proc* parent; /* enclosing proc */
struct ast_node* function_expr; /* function ast that we are compiling */
};
static inline struct basic_block * n2bb(struct node *n) { return (struct basic_block *)n; }
@ -354,8 +357,14 @@ struct linearizer {
struct allocator edge_allocator;
struct allocator pseudo_allocator;
struct allocator ptrlist_allocator;
struct allocator basic_block_allocator;
struct allocator proc_allocator;
struct ast_container *ast_container;
struct proc* main_proc; /* The root of the compiled chunk of code */
struct proc_list *all_procs; /* All procs allocated by the linearizer */
struct proc *current_proc; /* proc being compiled */
};
void raviA_ast_linearize(struct linearizer *linearizer, struct ast_container *container);
#endif

@ -10,11 +10,61 @@ Copyright (C) 2018-2020 Dibyendu Majumdar
#include <stddef.h>
/* Linearizer - WIP */
void raviA_init_linearizer(struct linearizer *linearizer, struct ast_container *container) {
memset(linearizer, 0, sizeof * linearizer);
linearizer->ast_container = container;
dmrC_allocator_init(&linearizer->edge_allocator, "edge_allocator", sizeof(struct edge), sizeof(double), CHUNK);
dmrC_allocator_init(&linearizer->instruction_allocator, "instruction_allocator", sizeof(struct instruction), sizeof(double), CHUNK);
dmrC_allocator_init(&linearizer->ptrlist_allocator, "ptrlist_allocator", sizeof(struct ptr_list), sizeof(double), CHUNK);
dmrC_allocator_init(&linearizer->pseudo_allocator, "pseudo_allocator", sizeof(struct pseudo), sizeof(double), CHUNK);
static void ravi_init_linearizer(struct linearizer *linearizer, struct ast_container *container) {
memset(linearizer, 0, sizeof *linearizer);
linearizer->ast_container = container;
dmrC_allocator_init(&linearizer->edge_allocator, "edge_allocator", sizeof(struct edge), sizeof(double), CHUNK);
dmrC_allocator_init(&linearizer->instruction_allocator, "instruction_allocator", sizeof(struct instruction),
sizeof(double), CHUNK);
dmrC_allocator_init(&linearizer->ptrlist_allocator, "ptrlist_allocator", sizeof(struct ptr_list), sizeof(double),
CHUNK);
dmrC_allocator_init(&linearizer->pseudo_allocator, "pseudo_allocator", sizeof(struct pseudo), sizeof(double), CHUNK);
dmrC_allocator_init(&linearizer->basic_block_allocator, "basic_block_allocator", sizeof(struct basic_block),
sizeof(double), CHUNK);
dmrC_allocator_init(&linearizer->proc_allocator, "proc_allocator", sizeof(struct proc), sizeof(double), CHUNK);
}
static void ravi_destroy_linearizer(struct linearizer *linearizer) {
dmrC_allocator_destroy(&linearizer->edge_allocator);
dmrC_allocator_destroy(&linearizer->instruction_allocator);
dmrC_allocator_destroy(&linearizer->ptrlist_allocator);
dmrC_allocator_destroy(&linearizer->pseudo_allocator);
dmrC_allocator_destroy(&linearizer->basic_block_allocator);
dmrC_allocator_destroy(&linearizer->proc_allocator);
}
/**
* Allocate a new proc. If no root proc then this becomes root proc.
* If no current proc then this becomes current proc.
* If there is a current proc, then this proc gets added to the current procs children, and it becomes the current proc.
* @param linearizer
* @param function_expr
* @return
*/
static struct proc* allocate_proc(struct linearizer *linearizer, struct ast_node *function_expr) {
assert(function_expr->type == AST_FUNCTION_EXPR);
struct proc *proc = dmrC_allocator_allocate(&linearizer->proc_allocator, 0);
proc->function_expr = function_expr;
ptrlist_add(&linearizer->all_procs, proc, &linearizer->ptrlist_allocator);
if (linearizer->main_proc == NULL) {
assert(function_expr->function_expr.parent_function == NULL);
// This proc must be the main
linearizer->main_proc = proc;
}
if (linearizer->current_proc == NULL) {
proc->parent = NULL;
}
else {
proc->parent = linearizer->current_proc;
ptrlist_add(&linearizer->current_proc, proc, &linearizer->ptrlist_allocator);
linearizer->current_proc = proc;
}
return proc;
}
void raviA_ast_linearize(struct linearizer *linearizer, struct ast_container *container) {
ravi_init_linearizer(linearizer, container);
struct proc *proc = allocate_proc(linearizer, container->main_function);
ravi_destroy_linearizer(linearizer);
}

@ -1433,6 +1433,8 @@ static void parse_lua_chunk(struct parser_state *parser) {
assert(parser->current_scope == NULL);
check(parser->ls, TK_EOS);
raviA_ast_typecheck(parser->container);
struct linearizer linearizer;
raviA_ast_linearize(&linearizer, parser->container);
}
static void parser_state_init(struct parser_state *parser, LexState *ls, struct ast_container *container) {

Loading…
Cancel
Save