generate ir
This commit is contained in:
parent
28f4b4cfc2
commit
968bdec2eb
6
Makefile
6
Makefile
@ -1,13 +1,15 @@
|
|||||||
|
|
||||||
CFLAGS := -std=c23 -pedantic -Wall -Wextra -g -Werror=switch
|
CFLAGS := -std=c23 -pedantic -Wall -Wextra -g -Werror=switch
|
||||||
|
|
||||||
# CFLAGS += -fsanitize=address
|
ifeq ($(ASAN),1)
|
||||||
|
CFLAGS += -fsanitize=address
|
||||||
|
endif
|
||||||
|
|
||||||
c_files := $(wildcard *.c)
|
c_files := $(wildcard *.c)
|
||||||
h_files := $(wildcard *.h)
|
h_files := $(wildcard *.h)
|
||||||
|
|
||||||
|
|
||||||
larpcc: $(c_files) $(h_files)
|
larpcc: $(c_files) $(h_files)
|
||||||
gcc $(c_files) -o $@ -std=c23 -pedantic -Wall -Wextra -g -Werror=switch
|
gcc $(c_files) -o $@ $(CFLAGS)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
27
ast.h
27
ast.h
@ -120,31 +120,4 @@ void ast_decl_print(FILE* f, const AstDecl* node, int depth);
|
|||||||
void ast_declar_print(FILE* f, const AstDeclar* node, int depth);
|
void ast_declar_print(FILE* f, const AstDeclar* node, int depth);
|
||||||
void ast_expr_print(FILE* f, const AstExpr* node, int depth);
|
void ast_expr_print(FILE* f, const AstExpr* node, int depth);
|
||||||
|
|
||||||
typedef struct AstVisitor AstVisitor;
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
AstVisitor_Continue,
|
|
||||||
AstVisitor_Break,
|
|
||||||
} AstVisitorResult;
|
|
||||||
|
|
||||||
struct AstVisitor {
|
|
||||||
AstVisitorResult (*visit_stmt_block)(AstVisitor* v, AstStmtBlock* node);
|
|
||||||
AstVisitorResult (*visit_stmt)(AstVisitor* v, AstStmt* node);
|
|
||||||
AstVisitorResult (*visit_decl)(AstVisitor* v, AstDecl* node);
|
|
||||||
AstVisitorResult (*visit_declar)(AstVisitor* v, AstDeclar* node);
|
|
||||||
AstVisitorResult (*visit_expr)(AstVisitor* v, AstExpr* node);
|
|
||||||
void* op;
|
|
||||||
};
|
|
||||||
|
|
||||||
void ast_stmt_block_visit(AstVisitor* v, AstStmtBlock* node);
|
|
||||||
void ast_stmt_block_visit_body(AstVisitor* v, AstStmtBlock* node);
|
|
||||||
void ast_stmt_visit(AstVisitor* v, AstStmt* node);
|
|
||||||
void ast_stmt_visit_body(AstVisitor* v, AstStmt* node);
|
|
||||||
void ast_decl_visit(AstVisitor* v, AstDecl* node);
|
|
||||||
void ast_decl_visit_body(AstVisitor* v, AstDecl* node);
|
|
||||||
void ast_declar_visit(AstVisitor* v, AstDeclar* node);
|
|
||||||
void ast_declar_visit_body(AstVisitor* v, AstDeclar* node);
|
|
||||||
void ast_expr_visit(AstVisitor* v, AstExpr* node);
|
|
||||||
void ast_expr_visit_body(AstVisitor* v, AstExpr* node);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
98
ast_print.c
98
ast_print.c
@ -274,101 +274,3 @@ void ast_expr_print(FILE* f, const AstExpr* node, int depth)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_stmt_block_visit(AstVisitor* v, AstStmtBlock* node)
|
|
||||||
{
|
|
||||||
if (v->visit_stmt_block && v->visit_stmt_block(v, node) == AstVisitor_Break)
|
|
||||||
return;
|
|
||||||
ast_stmt_block_visit_body(v, node);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_stmt_block_visit_body(AstVisitor* v, AstStmtBlock* node)
|
|
||||||
{
|
|
||||||
for (size_t i = 0; i < node->stmts_size; ++i) {
|
|
||||||
ast_stmt_visit(v, node->stmts[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_stmt_visit(AstVisitor* v, AstStmt* node)
|
|
||||||
{
|
|
||||||
if (v->visit_stmt && v->visit_stmt(v, node) == AstVisitor_Break)
|
|
||||||
return;
|
|
||||||
ast_stmt_visit_body(v, node);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_stmt_visit_body(AstVisitor* v, AstStmt* node)
|
|
||||||
{
|
|
||||||
switch (node->ty) {
|
|
||||||
case AstStmt_Decl:
|
|
||||||
ast_decl_visit(v, node->decl);
|
|
||||||
break;
|
|
||||||
case AstStmt_Expr:
|
|
||||||
ast_expr_visit(v, node->expr);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_decl_visit(AstVisitor* v, AstDecl* node)
|
|
||||||
{
|
|
||||||
if (v->visit_decl && v->visit_decl(v, node) == AstVisitor_Break)
|
|
||||||
return;
|
|
||||||
ast_decl_visit_body(v, node);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_decl_visit_body(AstVisitor* v, AstDecl* node)
|
|
||||||
{
|
|
||||||
for (size_t i = 0; i < node->declars_size; ++i)
|
|
||||||
ast_declar_visit(v, node->declars[i]);
|
|
||||||
if (node->function_body)
|
|
||||||
ast_stmt_block_visit(v, node->function_body);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_declar_visit(AstVisitor* v, AstDeclar* node)
|
|
||||||
{
|
|
||||||
if (v->visit_declar && v->visit_declar(v, node) == AstVisitor_Break)
|
|
||||||
return;
|
|
||||||
ast_declar_visit_body(v, node);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_declar_visit_body(AstVisitor* v, AstDeclar* node)
|
|
||||||
{
|
|
||||||
switch (node->ty) {
|
|
||||||
case AstDeclar_Anon:
|
|
||||||
case AstDeclar_Ident:
|
|
||||||
break;
|
|
||||||
case AstDeclar_Pointer:
|
|
||||||
ast_declar_visit(v, node->pointer.declar);
|
|
||||||
break;
|
|
||||||
case AstDeclar_Array:
|
|
||||||
ast_declar_visit(v, node->array.declar);
|
|
||||||
ast_expr_visit(v, node->array.expr);
|
|
||||||
break;
|
|
||||||
case AstDeclar_Function:
|
|
||||||
ast_declar_visit(v, node->function.declar);
|
|
||||||
for (size_t i = 0; i < node->function.params_size; ++i)
|
|
||||||
ast_decl_visit(v, node->function.params[i]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_expr_visit(AstVisitor* v, AstExpr* node)
|
|
||||||
{
|
|
||||||
if (v->visit_expr && v->visit_expr(v, node) == AstVisitor_Break)
|
|
||||||
return;
|
|
||||||
ast_expr_visit_body(v, node);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_expr_visit_body(AstVisitor* v, AstExpr* node)
|
|
||||||
{
|
|
||||||
switch (node->ty) {
|
|
||||||
case AstExpr_Ident:
|
|
||||||
break;
|
|
||||||
case AstExpr_Str:
|
|
||||||
break;
|
|
||||||
case AstExpr_Call:
|
|
||||||
ast_expr_visit(v, node->call.expr);
|
|
||||||
for (size_t i = 0; i < node->call.args_size; ++i)
|
|
||||||
ast_expr_visit(v, node->call.args[i]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
27
collection.c
27
collection.c
@ -1,4 +1,5 @@
|
|||||||
#include "collection.h"
|
#include "collection.h"
|
||||||
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -255,8 +256,9 @@ void keymap_init(Keymap* m, size_t elem_size)
|
|||||||
*m = (Keymap) {
|
*m = (Keymap) {
|
||||||
ARRAY_FIELD_INIT(entries),
|
ARRAY_FIELD_INIT(entries),
|
||||||
.elem_size = elem_size,
|
.elem_size = elem_size,
|
||||||
.entry_size = entry_size,
|
.size_of_entry = entry_size,
|
||||||
.temp_entry = malloc(entry_size),
|
.temp_entry = malloc(entry_size),
|
||||||
|
.dump_entry = nullptr,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -269,7 +271,7 @@ void keymap_deinit(Keymap* m)
|
|||||||
|
|
||||||
static KeymapEntry* keymap_idx(Keymap* m, size_t idx)
|
static KeymapEntry* keymap_idx(Keymap* m, size_t idx)
|
||||||
{
|
{
|
||||||
return (KeymapEntry*)((unsigned char*)m->entries + m->entry_size * idx);
|
return (KeymapEntry*)((unsigned char*)m->entries + m->size_of_entry * idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SearchResult {
|
struct SearchResult {
|
||||||
@ -303,13 +305,13 @@ void keymap_set(Keymap* m, KeymapKey key, void* value)
|
|||||||
}
|
}
|
||||||
|
|
||||||
KeymapEntry* entry = m->temp_entry;
|
KeymapEntry* entry = m->temp_entry;
|
||||||
memset(entry, 0, m->entry_size);
|
memset(entry, 0, m->size_of_entry);
|
||||||
|
|
||||||
array_push((void**)&m->entries,
|
array_push((void**)&m->entries,
|
||||||
&m->entries_cap,
|
&m->entries_cap,
|
||||||
&m->entries_size,
|
&m->entries_size,
|
||||||
entry,
|
entry,
|
||||||
m->entry_size);
|
m->size_of_entry);
|
||||||
|
|
||||||
entry->key = key;
|
entry->key = key;
|
||||||
memcpy(&entry->value, value, m->elem_size);
|
memcpy(&entry->value, value, m->elem_size);
|
||||||
@ -317,10 +319,10 @@ void keymap_set(Keymap* m, KeymapKey key, void* value)
|
|||||||
if (result.idx < m->entries_size - 1) {
|
if (result.idx < m->entries_size - 1) {
|
||||||
memmove(keymap_idx(m, result.idx + 1),
|
memmove(keymap_idx(m, result.idx + 1),
|
||||||
keymap_idx(m, result.idx),
|
keymap_idx(m, result.idx),
|
||||||
(m->entries_size - result.idx - 1) * m->entry_size);
|
(m->entries_size - result.idx - 1) * m->size_of_entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(keymap_idx(m, result.idx), entry, m->entry_size);
|
memcpy(keymap_idx(m, result.idx), entry, m->size_of_entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* keymap_get(Keymap* m, KeymapKey key)
|
void* keymap_get(Keymap* m, KeymapKey key)
|
||||||
@ -328,3 +330,16 @@ void* keymap_get(Keymap* m, KeymapKey key)
|
|||||||
struct SearchResult result = keymap_search(m, 0, m->entries_size, key);
|
struct SearchResult result = keymap_search(m, 0, m->entries_size, key);
|
||||||
return result.entry ? &result.entry->value : nullptr;
|
return result.entry ? &result.entry->value : nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void keymap_dump(Keymap* m, FILE* file)
|
||||||
|
{
|
||||||
|
assert(m->dump_entry);
|
||||||
|
|
||||||
|
fprintf(file, "Keymap {\n");
|
||||||
|
for (size_t i = 0; i < m->entries_size; ++i) {
|
||||||
|
fprintf(file, " %lx => ", m->entries[i].key);
|
||||||
|
m->dump_entry(m->entries[i].value, file);
|
||||||
|
fprintf(file, ",\n");
|
||||||
|
}
|
||||||
|
fprintf(file, "}");
|
||||||
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
#define COLLECTION_H
|
#define COLLECTION_H
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
void array_push(
|
void array_push(
|
||||||
void** data, size_t* capacity, size_t* size, void* elem, size_t elem_size);
|
void** data, size_t* capacity, size_t* size, void* elem, size_t elem_size);
|
||||||
@ -100,13 +101,15 @@ typedef struct KeymapEntry KeymapEntry;
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
ARRAY_FIELD(KeymapEntry, entries);
|
ARRAY_FIELD(KeymapEntry, entries);
|
||||||
size_t elem_size;
|
size_t elem_size;
|
||||||
size_t entry_size;
|
size_t size_of_entry;
|
||||||
KeymapEntry* temp_entry;
|
KeymapEntry* temp_entry;
|
||||||
|
void (*dump_entry)(const void* entry, FILE* file);
|
||||||
} Keymap;
|
} Keymap;
|
||||||
|
|
||||||
void keymap_init(Keymap* m, size_t elem_size);
|
void keymap_init(Keymap* m, size_t elem_size);
|
||||||
void keymap_deinit(Keymap* m);
|
void keymap_deinit(Keymap* m);
|
||||||
void keymap_set(Keymap* m, KeymapKey key, void* value);
|
void keymap_set(Keymap* m, KeymapKey key, void* value);
|
||||||
void* keymap_get(Keymap* m, KeymapKey key);
|
void* keymap_get(Keymap* m, KeymapKey key);
|
||||||
|
void keymap_dump(Keymap* m, FILE* file);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
326
ir.c
Normal file
326
ir.c
Normal file
@ -0,0 +1,326 @@
|
|||||||
|
#include "ir.h"
|
||||||
|
#include "ast.h"
|
||||||
|
#include "collection.h"
|
||||||
|
#include "sema.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static void keymap_irval_dump_fn(const void* entry, FILE* file)
|
||||||
|
{
|
||||||
|
irval_dump(*(IrVal**)entry, file, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void astlowerer_init(AstLowerer* l, Sema* sema)
|
||||||
|
{
|
||||||
|
*l = (AstLowerer) {
|
||||||
|
.cx = {
|
||||||
|
.arena = { },
|
||||||
|
ARRAY_FIELD_INIT(fns),
|
||||||
|
ARRAY_FIELD_INIT(readonly_vals),
|
||||||
|
},
|
||||||
|
.a = nullptr,
|
||||||
|
.sema = sema,
|
||||||
|
.curr_fn = nullptr,
|
||||||
|
.curr_block = nullptr,
|
||||||
|
.sym_map = { },
|
||||||
|
};
|
||||||
|
|
||||||
|
l->a = &l->cx.arena;
|
||||||
|
|
||||||
|
arena_init(l->a, 8192);
|
||||||
|
keymap_init(&l->sym_map, sizeof(IrVal*));
|
||||||
|
l->sym_map.dump_entry = keymap_irval_dump_fn;
|
||||||
|
}
|
||||||
|
|
||||||
|
void astlowerer_deinit(AstLowerer* l)
|
||||||
|
{
|
||||||
|
arena_deinit(l->a);
|
||||||
|
keymap_deinit(&l->sym_map);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef AstLowerer L;
|
||||||
|
|
||||||
|
static void l_lower_top_declar(L* l, AstStmt* stmt, AstDeclar* declar);
|
||||||
|
static void l_lower_fn(L* l, AstStmt* stmt);
|
||||||
|
static void l_lower_stmtblock(L* l, AstStmtBlock* block);
|
||||||
|
static void l_lower_stmt(L* l, AstStmt* stmt);
|
||||||
|
static IrInst* l_lower_expr(L* l, AstExpr* expr);
|
||||||
|
|
||||||
|
void astlowerer_lower_top_stmt(AstLowerer* l, AstStmt* stmt)
|
||||||
|
{
|
||||||
|
switch (stmt->ty) {
|
||||||
|
case AstStmt_Decl:
|
||||||
|
if (stmt->decl->function_body) {
|
||||||
|
l_lower_fn(l, stmt);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < stmt->decl->declars_size; ++i)
|
||||||
|
l_lower_top_declar(l, stmt, stmt->decl->declars[i]);
|
||||||
|
break;
|
||||||
|
case AstStmt_Expr:
|
||||||
|
assert(false && "expressions not allowed here");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static IrInst* l_push_inst(AstLowerer* l, IrInst inst)
|
||||||
|
{
|
||||||
|
IrInst* allocated = arena_alloc(l->a, sizeof(*allocated));
|
||||||
|
*allocated = inst;
|
||||||
|
ARENA_ARRAY_FIELD_PUSH(l->a, l->curr_block, insts, &allocated);
|
||||||
|
return allocated;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void l_lower_top_declar(AstLowerer* l, AstStmt* stmt, AstDeclar* declar)
|
||||||
|
{
|
||||||
|
(void)stmt;
|
||||||
|
if (declar->ty == AstDeclar_Function) {
|
||||||
|
assert(declar->function.declar->ty == AstDeclar_Ident);
|
||||||
|
|
||||||
|
IrFn* fn = arena_alloc(l->a, sizeof(*fn));
|
||||||
|
*fn = (IrFn) {
|
||||||
|
.ident = arena_strdup(l->a, declar->function.declar->ident),
|
||||||
|
ARRAY_FIELD_INIT(blocks),
|
||||||
|
.defined = false,
|
||||||
|
};
|
||||||
|
ARENA_ARRAY_FIELD_PUSH(l->a, &l->cx, fns, &fn);
|
||||||
|
|
||||||
|
Sym* sym = sema_get_ord_sym(l->sema, declar->function.declar->ident);
|
||||||
|
assert(sym);
|
||||||
|
|
||||||
|
IrVal* val = arena_alloc(l->a, sizeof(*val));
|
||||||
|
*val = (IrVal) { .ty = IrVal_Fn, .fn = fn };
|
||||||
|
keymap_set(&l->sym_map, (KeymapKey)sym, &val);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void l_lower_fn(L* l, AstStmt* stmt)
|
||||||
|
{
|
||||||
|
Sym* sym = sema_get_ord_sym(l->sema, stmt->decl->declars[0]->ident);
|
||||||
|
assert(sym);
|
||||||
|
|
||||||
|
IrFn* fn;
|
||||||
|
IrVal* val;
|
||||||
|
IrVal** found = keymap_get(&l->sym_map, (KeymapKey)sym);
|
||||||
|
|
||||||
|
if (found) {
|
||||||
|
val = *found;
|
||||||
|
assert(val->ty == IrVal_Fn);
|
||||||
|
fn = val->fn;
|
||||||
|
assert(!fn->defined);
|
||||||
|
} else {
|
||||||
|
fn = arena_alloc(l->a, sizeof(*fn));
|
||||||
|
*fn = (IrFn) {
|
||||||
|
.ident = arena_strdup(
|
||||||
|
l->a, stmt->decl->declars[0]->function.declar->ident),
|
||||||
|
ARRAY_FIELD_INIT(blocks),
|
||||||
|
};
|
||||||
|
ARENA_ARRAY_FIELD_PUSH(l->a, &l->cx, fns, &fn);
|
||||||
|
|
||||||
|
val = arena_alloc(l->a, sizeof(*val));
|
||||||
|
*val = (IrVal) { .ty = IrVal_Fn, .fn = fn };
|
||||||
|
keymap_set(&l->sym_map, (KeymapKey)sym, &val);
|
||||||
|
}
|
||||||
|
|
||||||
|
IrBlock* new_block = arena_alloc(l->a, sizeof(*new_block));
|
||||||
|
*new_block = (IrBlock) {
|
||||||
|
ARRAY_FIELD_INIT(insts),
|
||||||
|
};
|
||||||
|
ARENA_ARRAY_FIELD_PUSH(
|
||||||
|
l->a, l->cx.fns[l->cx.fns_size - 1], blocks, &new_block);
|
||||||
|
l->curr_fn = l->cx.fns[l->cx.fns_size - 1];
|
||||||
|
l->curr_block = l->curr_fn->blocks[l->curr_fn->blocks_size - 1];
|
||||||
|
|
||||||
|
fn->defined = true;
|
||||||
|
|
||||||
|
l_lower_stmtblock(l, stmt->decl->function_body);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void l_lower_stmtblock(L* l, AstStmtBlock* block)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < block->stmts_size; ++i) {
|
||||||
|
l_lower_stmt(l, block->stmts[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void l_lower_stmt(L* l, AstStmt* stmt)
|
||||||
|
{
|
||||||
|
switch (stmt->ty) {
|
||||||
|
case AstStmt_Decl:
|
||||||
|
assert(!"not implemented");
|
||||||
|
break;
|
||||||
|
case AstStmt_Expr:
|
||||||
|
l_lower_expr(l, stmt->expr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static IrInst* l_lower_expr(L* l, AstExpr* expr)
|
||||||
|
{
|
||||||
|
switch (expr->ty) {
|
||||||
|
case AstExpr_Ident: {
|
||||||
|
Sym* sym = sema_get_ord_sym(l->sema, expr->ident);
|
||||||
|
assert(sym);
|
||||||
|
IrVal** val_ptr = keymap_get(&l->sym_map, (KeymapKey)sym);
|
||||||
|
assert(val_ptr);
|
||||||
|
IrInst inst = {
|
||||||
|
.ty = IrInst_Val,
|
||||||
|
.val = *val_ptr,
|
||||||
|
};
|
||||||
|
return l_push_inst(l, inst);
|
||||||
|
}
|
||||||
|
case AstExpr_Str: {
|
||||||
|
|
||||||
|
size_t elems_size = strlen(expr->str_val) + 1;
|
||||||
|
IrVal** elems = arena_alloc(l->a, sizeof(*elems) * elems_size);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < elems_size; ++i) {
|
||||||
|
IrVal* val = arena_alloc(l->a, sizeof(*val));
|
||||||
|
*val = (IrVal) { .ty = IrVal_I8, .i8_val = expr->str_val[i] };
|
||||||
|
elems[i] = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
IrVal* val = arena_alloc(l->a, sizeof(*val));
|
||||||
|
*val = (IrVal) {
|
||||||
|
.ty = IrVal_Array,
|
||||||
|
.array = {
|
||||||
|
.size_of_elem = sizeof(char),
|
||||||
|
.elems_size = elems_size,
|
||||||
|
.elems = elems,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
ARENA_ARRAY_FIELD_PUSH(l->a, &l->cx, readonly_vals, val);
|
||||||
|
|
||||||
|
IrInst inst = {
|
||||||
|
.ty = IrInst_Val,
|
||||||
|
.val = val,
|
||||||
|
};
|
||||||
|
return l_push_inst(l, inst);
|
||||||
|
}
|
||||||
|
case AstExpr_Call: {
|
||||||
|
IrInst* inner = l_lower_expr(l, expr->call.expr);
|
||||||
|
size_t args_size = expr->call.args_size;
|
||||||
|
IrInst** args = arena_alloc(l->a, sizeof(**args) * args_size);
|
||||||
|
for (size_t i = 0; i < args_size; ++i)
|
||||||
|
args[i] = l_lower_expr(l, expr->call.args[i]);
|
||||||
|
IrInst inst = {
|
||||||
|
.ty = IrInst_Call,
|
||||||
|
.call = {
|
||||||
|
.expr = inner,
|
||||||
|
.args = args,
|
||||||
|
.args_size = args_size,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return l_push_inst(l, inst);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void irval_dump(const IrVal* val, FILE* file, Keymap* inst_ids)
|
||||||
|
{
|
||||||
|
switch (val->ty) {
|
||||||
|
case IrVal_I8:
|
||||||
|
fprintf(file, "I8 %d", val->i8_val);
|
||||||
|
return;
|
||||||
|
case IrVal_Fn:
|
||||||
|
fprintf(file, "Fn %s", val->fn->ident);
|
||||||
|
return;
|
||||||
|
case IrVal_Array:
|
||||||
|
fprintf(file,
|
||||||
|
"Array { elems_size: %zu, size_of_elem: %zu, elems: [",
|
||||||
|
val->array.elems_size,
|
||||||
|
val->array.size_of_elem);
|
||||||
|
for (size_t i = 0; i < val->array.elems_size; ++i) {
|
||||||
|
if (i != 0)
|
||||||
|
fprintf(file, ", ");
|
||||||
|
irval_dump(val->array.elems[i], file, inst_ids);
|
||||||
|
}
|
||||||
|
fprintf(file, "] }");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void irinst_dump(const IrInst* inst, FILE* file, Keymap* inst_ids)
|
||||||
|
{
|
||||||
|
int* inst_id = nullptr;
|
||||||
|
if (inst_ids)
|
||||||
|
inst_id = keymap_get(inst_ids, (KeymapKey)inst);
|
||||||
|
switch (inst->ty) {
|
||||||
|
case IrInst_Val:
|
||||||
|
if (inst_id)
|
||||||
|
fprintf(file, "%%%d = ", *inst_id);
|
||||||
|
irval_dump(inst->val, file, inst_ids);
|
||||||
|
return;
|
||||||
|
case IrInst_Call:
|
||||||
|
assert(inst_ids);
|
||||||
|
fprintf(file, "%%%d = ", *inst_id);
|
||||||
|
int* expr_id = keymap_get(inst_ids, (KeymapKey)inst->call.expr);
|
||||||
|
fprintf(file, "Call %%%d (", *expr_id);
|
||||||
|
for (size_t i = 0; i < inst->call.args_size; ++i) {
|
||||||
|
int* arg_id
|
||||||
|
= keymap_get(inst_ids, (KeymapKey)inst->call.args[i]);
|
||||||
|
fprintf(file, "%s%%%d", i ? ", " : "", *arg_id);
|
||||||
|
}
|
||||||
|
fprintf(file, ")");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void int_dump_fn(const void* val, FILE* file)
|
||||||
|
{
|
||||||
|
fprintf(file, "%d", *(int*)val);
|
||||||
|
}
|
||||||
|
|
||||||
|
void irfn_dump(const IrFn* fn, FILE* file)
|
||||||
|
{
|
||||||
|
Keymap inst_ids;
|
||||||
|
keymap_init(&inst_ids, sizeof(int));
|
||||||
|
inst_ids.dump_entry = int_dump_fn;
|
||||||
|
|
||||||
|
int inst_id_counter = 0;
|
||||||
|
for (size_t block_i = 0; block_i < fn->blocks_size; ++block_i) {
|
||||||
|
IrBlock* block = fn->blocks[block_i];
|
||||||
|
|
||||||
|
for (size_t inst_i = 0; inst_i < block->insts_size; ++inst_i) {
|
||||||
|
IrInst* inst = block->insts[inst_i];
|
||||||
|
|
||||||
|
keymap_set(&inst_ids, (KeymapKey)inst, &inst_id_counter);
|
||||||
|
inst_id_counter += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(file, "fn %s() -> _", fn->ident);
|
||||||
|
|
||||||
|
if (!fn->defined) {
|
||||||
|
fprintf(file, ";");
|
||||||
|
goto leave;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(file, " {\n");
|
||||||
|
for (size_t block_i = 0; block_i < fn->blocks_size; ++block_i) {
|
||||||
|
IrBlock* block = fn->blocks[block_i];
|
||||||
|
|
||||||
|
fprintf(file, "bb%zu:\n", block_i);
|
||||||
|
for (size_t inst_i = 0; inst_i < block->insts_size; ++inst_i) {
|
||||||
|
IrInst* inst = block->insts[inst_i];
|
||||||
|
|
||||||
|
fprintf(file, " ");
|
||||||
|
irinst_dump(inst, file, &inst_ids);
|
||||||
|
fprintf(file, "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fprintf(file, "}");
|
||||||
|
|
||||||
|
leave:
|
||||||
|
keymap_deinit(&inst_ids);
|
||||||
|
}
|
||||||
85
ir.h
Normal file
85
ir.h
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
#ifndef IR_H
|
||||||
|
#define IR_H
|
||||||
|
|
||||||
|
#include "ast.h"
|
||||||
|
#include "collection.h"
|
||||||
|
#include "sema.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
IrVal_I8,
|
||||||
|
IrVal_Fn,
|
||||||
|
IrVal_Array,
|
||||||
|
} IrValTy;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
IrInst_Val,
|
||||||
|
IrInst_Call,
|
||||||
|
} IrInstTy;
|
||||||
|
|
||||||
|
typedef struct IrVal IrVal;
|
||||||
|
typedef struct IrInst IrInst;
|
||||||
|
typedef struct IrFn IrFn;
|
||||||
|
|
||||||
|
struct IrVal {
|
||||||
|
IrValTy ty;
|
||||||
|
union {
|
||||||
|
int8_t i8_val;
|
||||||
|
IrFn* fn;
|
||||||
|
struct {
|
||||||
|
IrVal** elems;
|
||||||
|
size_t elems_size;
|
||||||
|
size_t size_of_elem;
|
||||||
|
} array;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct IrInst {
|
||||||
|
IrInstTy ty;
|
||||||
|
union {
|
||||||
|
IrVal* val;
|
||||||
|
struct {
|
||||||
|
IrInst* expr;
|
||||||
|
IrInst** args;
|
||||||
|
size_t args_size;
|
||||||
|
} call;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
ARRAY_FIELD(IrInst*, insts);
|
||||||
|
} IrBlock;
|
||||||
|
|
||||||
|
struct IrFn {
|
||||||
|
char* ident;
|
||||||
|
ARRAY_FIELD(IrBlock*, blocks);
|
||||||
|
bool defined;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Arena arena;
|
||||||
|
ARRAY_FIELD(IrFn*, fns);
|
||||||
|
ARRAY_FIELD(IrVal*, readonly_vals);
|
||||||
|
} IrCx;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
IrCx cx;
|
||||||
|
Arena* a;
|
||||||
|
Sema* sema;
|
||||||
|
IrFn* curr_fn;
|
||||||
|
IrBlock* curr_block;
|
||||||
|
Keymap sym_map;
|
||||||
|
} AstLowerer;
|
||||||
|
|
||||||
|
void astlowerer_init(AstLowerer* l, Sema* sema);
|
||||||
|
void astlowerer_deinit(AstLowerer* l);
|
||||||
|
|
||||||
|
void astlowerer_lower_top_stmt(AstLowerer* l, AstStmt* stmt);
|
||||||
|
|
||||||
|
void irval_dump(const IrVal* val, FILE* file, Keymap* inst_ids);
|
||||||
|
void irinst_dump(const IrInst* val, FILE* file, Keymap* inst_ids);
|
||||||
|
void irfn_dump(const IrFn* val, FILE* file);
|
||||||
|
|
||||||
|
#endif
|
||||||
60
main.c
60
main.c
@ -1,5 +1,6 @@
|
|||||||
#include "ast.h"
|
#include "ast.h"
|
||||||
#include "collection.h"
|
#include "collection.h"
|
||||||
|
#include "ir.h"
|
||||||
#include "parse.h"
|
#include "parse.h"
|
||||||
#include "preproc.h"
|
#include "preproc.h"
|
||||||
#include "sema.h"
|
#include "sema.h"
|
||||||
@ -21,48 +22,6 @@ int main(int argc, char* argv[])
|
|||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("--- text ---\n%s\n", text);
|
|
||||||
|
|
||||||
{
|
|
||||||
|
|
||||||
Arena tok_arena;
|
|
||||||
arena_init(&tok_arena, 4096);
|
|
||||||
|
|
||||||
Preproc* preproc
|
|
||||||
= preproc_new(strdup(filename), strdup(text), &tok_arena);
|
|
||||||
|
|
||||||
TokStream tokstream;
|
|
||||||
tokstream_init(&tokstream, preproc, &tok_arena);
|
|
||||||
|
|
||||||
printf("--- tokens ---\n");
|
|
||||||
|
|
||||||
Loc loc = { 1, 1, nullptr };
|
|
||||||
Tok tok;
|
|
||||||
while ((tok = tokstream_next(&tokstream)).ty != Tok_Eof) {
|
|
||||||
// printf("%s\t%s\n", tokty_str(tok.ty), tok.text);
|
|
||||||
Loc tok_loc = tok.loc;
|
|
||||||
if (loc.line > tok_loc.line) {
|
|
||||||
loc.line = 0;
|
|
||||||
}
|
|
||||||
while (loc.line < tok_loc.line) {
|
|
||||||
printf("\n");
|
|
||||||
loc.line += 1;
|
|
||||||
loc.col = 1;
|
|
||||||
}
|
|
||||||
while (loc.col < tok_loc.col) {
|
|
||||||
printf(" ");
|
|
||||||
loc.col += 1;
|
|
||||||
}
|
|
||||||
printf("%s", tok.text);
|
|
||||||
loc.line = tok_loc.line;
|
|
||||||
loc.col = tok_loc.col + strlen(tok.text);
|
|
||||||
}
|
|
||||||
fputs("\n", stdout);
|
|
||||||
|
|
||||||
preproc_free(preproc);
|
|
||||||
arena_deinit(&tok_arena);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
{
|
||||||
Arena tok_arena;
|
Arena tok_arena;
|
||||||
arena_init(&tok_arena, 4096);
|
arena_init(&tok_arena, 4096);
|
||||||
@ -82,29 +41,32 @@ int main(int argc, char* argv[])
|
|||||||
Parser parser;
|
Parser parser;
|
||||||
parser_init(&parser, &tokstream, sema, &ast_arena);
|
parser_init(&parser, &tokstream, sema, &ast_arena);
|
||||||
|
|
||||||
// Arena sym_arena;
|
AstLowerer astlowerer;
|
||||||
// arena_init(&sym_arena, 4096);
|
astlowerer_init(&astlowerer, sema);
|
||||||
// Resolver* resolver = resolver_new(&sym_arena);
|
|
||||||
|
|
||||||
AstStmt* stmt;
|
AstStmt* stmt;
|
||||||
while (!parser_done(&parser)) {
|
while (!parser_done(&parser)) {
|
||||||
stmt = parser_parse_stmt(&parser);
|
stmt = parser_parse_stmt(&parser);
|
||||||
|
|
||||||
ast_stmt_print(stdout, stmt, 0);
|
ast_stmt_print(stdout, stmt, 0);
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
// resolver_resolve_top_stmt(resolver, stmt);
|
astlowerer_lower_top_stmt(&astlowerer, stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolver_free(resolver);
|
IrCx* ir_cx = &astlowerer.cx;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < ir_cx->fns_size; ++i) {
|
||||||
|
irfn_dump(ir_cx->fns[i], stdout);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
parser_deinit(&parser);
|
parser_deinit(&parser);
|
||||||
preproc_free(preproc);
|
preproc_free(preproc);
|
||||||
arena_deinit(&tok_arena);
|
arena_deinit(&tok_arena);
|
||||||
|
|
||||||
arena_deinit(&ast_arena);
|
arena_deinit(&ast_arena);
|
||||||
// arena_deinit(&sym_arena);
|
astlowerer_deinit(&astlowerer);
|
||||||
|
|
||||||
sema_free(sema);
|
sema_free(sema);
|
||||||
}
|
}
|
||||||
|
|||||||
8
sema.c
8
sema.c
@ -152,7 +152,8 @@ void sema_dump(Sema* s)
|
|||||||
for (size_t i = 0; i < rib->ord_syms.entries_size; ++i) {
|
for (size_t i = 0; i < rib->ord_syms.entries_size; ++i) {
|
||||||
const char* key = hashmap_key_at(&rib->ord_syms, i);
|
const char* key = hashmap_key_at(&rib->ord_syms, i);
|
||||||
Sym** sym = hashmap_val_at(&rib->ord_syms, i);
|
Sym** sym = hashmap_val_at(&rib->ord_syms, i);
|
||||||
printf(" \"%s\" => Sym {}\n", key);
|
(void)sym;
|
||||||
|
printf(" \"%s\" => Sym { ... }\n", key);
|
||||||
}
|
}
|
||||||
printf(" }\n");
|
printf(" }\n");
|
||||||
printf(" }\n");
|
printf(" }\n");
|
||||||
@ -411,6 +412,11 @@ bool sema_ident_is_typedef(Sema* s, const char* ident)
|
|||||||
&& (sym->declar.type->spec & TypeSpec_Typedef);
|
&& (sym->declar.type->spec & TypeSpec_Typedef);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Sym* sema_get_ord_sym(Sema* s, const char* ident)
|
||||||
|
{
|
||||||
|
return get_ord_sym(s, ident);
|
||||||
|
}
|
||||||
|
|
||||||
void sema_visit_stmt(Sema* s, AstStmt* stmt)
|
void sema_visit_stmt(Sema* s, AstStmt* stmt)
|
||||||
{
|
{
|
||||||
if (stmt->ty == AstStmt_Decl) {
|
if (stmt->ty == AstStmt_Decl) {
|
||||||
|
|||||||
1
sema.h
1
sema.h
@ -81,6 +81,7 @@ void sema_function_enter(Sema* s, AstSpec spec, AstDeclar* declar);
|
|||||||
void sema_function_leave(Sema* s);
|
void sema_function_leave(Sema* s);
|
||||||
|
|
||||||
bool sema_ident_is_typedef(Sema* s, const char* ident);
|
bool sema_ident_is_typedef(Sema* s, const char* ident);
|
||||||
|
Sym* sema_get_ord_sym(Sema* s, const char* ident);
|
||||||
|
|
||||||
void sema_visit_stmt(Sema* s, AstStmt* stmt);
|
void sema_visit_stmt(Sema* s, AstStmt* stmt);
|
||||||
void sema_visit_expr(Sema* s, AstExpr* expr);
|
void sema_visit_expr(Sema* s, AstExpr* expr);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user