import segregation for types

See #3
master
Andrew Kelley 2016-01-04 03:52:37 -07:00
parent 44d5d008d0
commit 2c8958f454
3 changed files with 39 additions and 31 deletions

View File

@ -142,15 +142,16 @@ TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool
entry->data.pointer.child_type = child_type;
entry->data.pointer.is_const = is_const;
g->type_table.put(&entry->name, entry);
*parent_pointer = entry;
return entry;
}
}
static TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) {
static TypeTableEntry *get_maybe_type(CodeGen *g, ImportTableEntry *import, TypeTableEntry *child_type) {
if (child_type->maybe_parent) {
return child_type->maybe_parent;
TypeTableEntry *entry = child_type->maybe_parent;
import->type_table.put(&entry->name, entry);
return entry;
} else {
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdMaybe);
// create a struct with a boolean whether this is the null value
@ -193,16 +194,20 @@ static TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) {
entry->data.maybe.child_type = child_type;
g->type_table.put(&entry->name, entry);
import->type_table.put(&entry->name, entry);
child_type->maybe_parent = entry;
return entry;
}
}
static TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t array_size) {
static TypeTableEntry *get_array_type(CodeGen *g, ImportTableEntry *import,
TypeTableEntry *child_type, uint64_t array_size)
{
auto existing_entry = child_type->arrays_by_size.maybe_get(array_size);
if (existing_entry) {
return existing_entry->value;
TypeTableEntry *entry = existing_entry->value;
import->type_table.put(&entry->name, entry);
return entry;
} else {
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdArray);
entry->type_ref = LLVMArrayType(child_type->type_ref, array_size);
@ -217,7 +222,7 @@ static TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, ui
entry->data.array.child_type = child_type;
entry->data.array.len = array_size;
g->type_table.put(&entry->name, entry);
import->type_table.put(&entry->name, entry);
child_type->arrays_by_size.put(array_size, entry);
return entry;
}
@ -231,7 +236,7 @@ static TypeTableEntry *resolve_type(CodeGen *g, AstNode *node, ImportTableEntry
case AstNodeTypeTypePrimitive:
{
Buf *name = &node->data.type.primitive_name;
auto table_entry = g->type_table.maybe_get(name);
auto table_entry = import->type_table.maybe_get(name);
if (table_entry) {
type_node->entry = table_entry->value;
} else {
@ -268,7 +273,8 @@ static TypeTableEntry *resolve_type(CodeGen *g, AstNode *node, ImportTableEntry
if (size_node->type == NodeTypeNumberLiteral &&
is_num_lit_unsigned(size_node->data.number_literal.kind))
{
type_node->entry = get_array_type(g, child_type, size_node->data.number_literal.data.x_uint);
type_node->entry = get_array_type(g, import, child_type,
size_node->data.number_literal.data.x_uint);
} else {
add_node_error(g, size_node,
buf_create_from_str("array size must be literal unsigned integer"));
@ -287,7 +293,7 @@ static TypeTableEntry *resolve_type(CodeGen *g, AstNode *node, ImportTableEntry
} else if (child_type->id == TypeTableEntryIdInvalid) {
return child_type;
}
type_node->entry = get_maybe_type(g, child_type);
type_node->entry = get_maybe_type(g, import, child_type);
return type_node->entry;
}
case AstNodeTypeTypeCompilerExpr:
@ -651,7 +657,7 @@ static void preview_types(CodeGen *g, ImportTableEntry *import, AstNode *node) {
StructDeclNode *struct_codegen = &node->codegen_node->data.struct_decl_node;
Buf *name = &node->data.struct_decl.name;
auto table_entry = g->type_table.maybe_get(name);
auto table_entry = import->type_table.maybe_get(name);
if (table_entry) {
struct_codegen->type_entry = table_entry->value;
add_node_error(g, node,
@ -667,7 +673,7 @@ static void preview_types(CodeGen *g, ImportTableEntry *import, AstNode *node) {
buf_init_from_buf(&entry->name, name);
// put off adding the debug type until we do the full struct body
// this type is incomplete until we do another pass
g->type_table.put(&entry->name, entry);
import->type_table.put(&entry->name, entry);
struct_codegen->type_entry = entry;
}
break;
@ -1809,7 +1815,8 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
if (node->data.string_literal.c) {
return_type = g->builtin_types.entry_c_string_literal;
} else {
return_type = get_array_type(g, g->builtin_types.entry_u8, buf_len(&node->data.string_literal.buf));
return_type = get_array_type(g, import, g->builtin_types.entry_u8,
buf_len(&node->data.string_literal.buf));
}
break;
case NodeTypeCharLiteral:

View File

@ -110,6 +110,7 @@ struct ImportTableEntry {
// reminder: hash tables must be initialized before use
HashMap<Buf *, FnTableEntry *, buf_hash, buf_eql_buf> fn_table;
HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> type_table;
};
struct LabelTableEntry {
@ -151,7 +152,6 @@ struct CodeGen {
// reminder: hash tables must be initialized before use
HashMap<Buf *, LLVMValueRef, buf_hash, buf_eql_buf> str_table;
HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> type_table;
HashMap<Buf *, bool, buf_hash, buf_eql_buf> link_table;
HashMap<Buf *, ImportTableEntry *, buf_hash, buf_eql_buf> import_table;

View File

@ -20,7 +20,6 @@
CodeGen *codegen_create(Buf *root_source_dir) {
CodeGen *g = allocate<CodeGen>(1);
g->str_table.init(32);
g->type_table.init(32);
g->link_table.init(32);
g->import_table.init(32);
g->build_type = CodeGenBuildTypeDebug;
@ -1695,7 +1694,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_unsigned());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_bool = entry;
}
{
@ -1707,7 +1705,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_unsigned());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_u8 = entry;
}
{
@ -1720,7 +1717,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_unsigned());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_u16 = entry;
}
{
@ -1733,7 +1729,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_unsigned());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_u32 = entry;
}
{
@ -1746,7 +1741,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_unsigned());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_u64 = entry;
}
g->builtin_types.entry_c_string_literal = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
@ -1760,7 +1754,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_signed());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_i8 = entry;
}
{
@ -1773,7 +1766,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_signed());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_i16 = entry;
}
{
@ -1786,7 +1778,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_signed());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_i32 = entry;
}
{
@ -1799,7 +1790,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_signed());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_i64 = entry;
}
{
@ -1812,7 +1802,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_signed());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_isize = entry;
}
{
@ -1825,7 +1814,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_unsigned());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_usize = entry;
}
{
@ -1837,7 +1825,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_float());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_f32 = entry;
}
{
@ -1849,7 +1836,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_float());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_f64 = entry;
}
{
@ -1859,7 +1845,6 @@ static void define_builtin_types(CodeGen *g) {
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
entry->size_in_bits, entry->align_in_bits,
LLVMZigEncoding_DW_ATE_unsigned());
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_void = entry;
}
{
@ -1867,7 +1852,6 @@ static void define_builtin_types(CodeGen *g) {
entry->type_ref = LLVMVoidType();
buf_init_from_str(&entry->name, "unreachable");
entry->di_type = g->builtin_types.entry_void->di_type;
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_unreachable = entry;
}
{
@ -1903,7 +1887,6 @@ static void define_builtin_types(CodeGen *g) {
"string", g->dummy_di_file, 0, entry->size_in_bits, entry->align_in_bits, 0,
nullptr, di_element_types, element_count, 0, nullptr, "");
g->type_table.put(&entry->name, entry);
g->builtin_types.entry_string = entry;
}
}
@ -2033,6 +2016,24 @@ static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *abs_full_path,
import_entry->line_offsets = tokenization.line_offsets;
import_entry->path = full_path;
import_entry->fn_table.init(32);
import_entry->type_table.init(32);
import_entry->type_table.put(&g->builtin_types.entry_bool->name, g->builtin_types.entry_bool);
import_entry->type_table.put(&g->builtin_types.entry_u8->name, g->builtin_types.entry_u8);
import_entry->type_table.put(&g->builtin_types.entry_u16->name, g->builtin_types.entry_u16);
import_entry->type_table.put(&g->builtin_types.entry_u32->name, g->builtin_types.entry_u32);
import_entry->type_table.put(&g->builtin_types.entry_u64->name, g->builtin_types.entry_u64);
import_entry->type_table.put(&g->builtin_types.entry_i8->name, g->builtin_types.entry_i8);
import_entry->type_table.put(&g->builtin_types.entry_i16->name, g->builtin_types.entry_i16);
import_entry->type_table.put(&g->builtin_types.entry_i32->name, g->builtin_types.entry_i32);
import_entry->type_table.put(&g->builtin_types.entry_i64->name, g->builtin_types.entry_i64);
import_entry->type_table.put(&g->builtin_types.entry_isize->name, g->builtin_types.entry_isize);
import_entry->type_table.put(&g->builtin_types.entry_usize->name, g->builtin_types.entry_usize);
import_entry->type_table.put(&g->builtin_types.entry_f32->name, g->builtin_types.entry_f32);
import_entry->type_table.put(&g->builtin_types.entry_f64->name, g->builtin_types.entry_f64);
import_entry->type_table.put(&g->builtin_types.entry_void->name, g->builtin_types.entry_void);
import_entry->type_table.put(&g->builtin_types.entry_unreachable->name, g->builtin_types.entry_unreachable);
import_entry->type_table.put(&g->builtin_types.entry_string->name, g->builtin_types.entry_string);
import_entry->root = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color);
assert(import_entry->root);
if (g->verbose) {