Skip to content
Open
80 changes: 80 additions & 0 deletions src/code_generator/symbol_table.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "code_generator/symbol_table.h"
#include "code_generator/symbol_table_impl.h"
#include "stdstring.h"
#include "utility.h"

DEFINE_VECTOR(SymbolInfoRef)
DEFINE_VECTOR(SymbolBlockRef)

static SymbolTableRef g_symbol_table;

SymbolInfoRef symbol_info_ctor(StringRef name, LLVMTypeRef type,
LLVMValueRef value) {
const SymbolInfoRef symbol = safe_malloc(struct SymbolInfo);
const StringRef symbol_name = string_ctor(string_data(name), NULL);
symbol->name = symbol_name;
symbol->type = type;
symbol->value = value;
return symbol;
}

void symbol_info_dtor(SymbolInfoRef* pself) {
assert(pself);
string_dtor(&(*pself)->name);
safe_free(*pself);
}

SymbolBlockRef symbol_block_ctor(StringRef name) {
const SymbolBlockRef block = safe_malloc(struct SymbolBlock);
const StringRef block_name = string_ctor(string_data(name), NULL);
block->name = block_name;
block->symbols = VECTORFUNC(SymbolInfoRef, ctor)(NULL);
return block;
}

void symbol_block_dtor(SymbolBlockRef* pself) {
assert(pself);
string_dtor(&(*pself)->name);
{
const VECTORREF(SymbolInfoRef) vector = (*pself)->symbols;
SymbolInfoRef* iter = VECTORFUNC(SymbolInfoRef, begin)(vector);
const SymbolInfoRef* const end = VECTORFUNC(SymbolInfoRef, end)(vector);
for (; iter != end; ++iter) {
SymbolInfoRef* const tmp = iter;
symbol_info_dtor(tmp);
}
VECTORFUNC(SymbolInfoRef, dtor)(&(*pself)->symbols);
}
safe_free(*pself);
}

SymbolTableRef symbol_table_ctor(void) {
const SymbolTableRef table = safe_malloc(struct SymbolTable);
table->stack = VECTORFUNC(SymbolBlockRef, ctor)(NULL);
return table;
}

void symbol_table_dtor(SymbolTableRef* pself) {
assert(pself);
{
const VECTORREF(SymbolBlockRef) vector = (*pself)->stack;
SymbolBlockRef* iter = VECTORFUNC(SymbolBlockRef, begin)(vector);
const SymbolBlockRef* const end = VECTORFUNC(SymbolBlockRef, end)(vector);
for (; iter != end; ++iter) {
SymbolBlockRef* const tmp = iter;
symbol_block_dtor(tmp);
}
VECTORFUNC(SymbolBlockRef, dtor)(&(*pself)->stack);
}
safe_free(*pself);
}

void initialize_symbol_table(void) {
assert(!g_symbol_table);
g_symbol_table = symbol_table_ctor();
}

void finalize_symbol_table(void) {
assert(g_symbol_table);
symbol_table_dtor(&g_symbol_table);
}
11 changes: 11 additions & 0 deletions src/code_generator/symbol_table.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef KMC_C90_COMPILER_CODE_GENERATOR_SYMBOL_TABLE_H
#define KMC_C90_COMPILER_CODE_GENERATOR_SYMBOL_TABLE_H

typedef struct SymbolInfo* SymbolInfoRef;
typedef struct SymbolBlock* SymbolBlockRef;
typedef struct SymbolTable* SymbolTableRef;

void initialize_symbol_table(void);
void finalize_symbol_table(void);

#endif /* KMC_C90_COMPILER_CODE_GENERATOR_SYMBOL_TABLE_H */
27 changes: 27 additions & 0 deletions src/code_generator/symbol_table_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef KMC_C90_COMPILER_CODE_GENERATOR_SYMBOL_TABLE_IMPL_H
#define KMC_C90_COMPILER_CODE_GENERATOR_SYMBOL_TABLE_IMPL_H

#include <llvm-c/Core.h>
#include "stdstring.h"
#include "vector.h"

struct SymbolInfo {
StringRef name;
LLVMTypeRef type;
LLVMValueRef value;
};

DECLARE_VECTOR(SymbolInfoRef)

struct SymbolBlock {
StringRef name;
VECTORREF(SymbolInfoRef) symbols;
};

DECLARE_VECTOR(SymbolBlockRef)

struct SymbolTable {
VECTORREF(SymbolBlockRef) stack;
};

#endif /* KMC_C90_COMPILER_CODE_GENERATOR_SYMBOL_TABLE_IMPL_H */