From ef482ece7c047e898fdc2ea15ba4216c15309d0c Mon Sep 17 00:00:00 2001 From: Josh Wolfe Date: Mon, 30 Nov 2015 15:36:58 -0700 Subject: [PATCH] no more TypeId. use g->builtin_types. --- src/analyze.cpp | 24 +++++++++++------------- src/codegen.cpp | 36 ++++++++++++------------------------ src/semantic_info.hpp | 20 +++++++++----------- 3 files changed, 32 insertions(+), 48 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index 268f164c3e..8dae356e81 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -57,7 +57,7 @@ static void resolve_type(CodeGen *g, AstNode *node) { } else { add_node_error(g, node, buf_sprintf("invalid type name: '%s'", buf_ptr(name))); - type_node->entry = g->invalid_type_entry; + type_node->entry = g->builtin_types.entry_invalid; } break; } @@ -65,7 +65,7 @@ static void resolve_type(CodeGen *g, AstNode *node) { { resolve_type(g, node->data.type.child_type); TypeNode *child_type_node = &node->data.type.child_type->codegen_node->data.type_node; - if (child_type_node->entry->id == TypeIdUnreachable) { + if (child_type_node->entry == g->builtin_types.entry_unreachable) { add_node_error(g, node, buf_create_from_str("pointer to unreachable not allowed")); } @@ -77,7 +77,6 @@ static void resolve_type(CodeGen *g, AstNode *node) { type_node->entry = *parent_pointer; } else { TypeTableEntry *entry = allocate(1); - entry->id = TypeIdPointer; entry->type_ref = LLVMPointerType(child_type_node->entry->type_ref, 0); buf_resize(&entry->name, 0); buf_appendf(&entry->name, "*%s %s", const_or_mut_str, buf_ptr(&child_type_node->entry->name)); @@ -249,7 +248,6 @@ static void check_fn_def_control_flow(CodeGen *g, AstNode *node) { assert(return_type_node->codegen_node); TypeTableEntry *type_entry = return_type_node->codegen_node->data.type_node.entry; assert(type_entry); - TypeId type_id = type_entry->id; AstNode *body_node = node->data.fn_def.body; assert(body_node->type == NodeTypeBlock); @@ -261,7 +259,7 @@ static void check_fn_def_control_flow(CodeGen *g, AstNode *node) { for (int i = 0; i < body_node->data.block.statements.length; i += 1) { AstNode *statement_node = body_node->data.block.statements.at(i); if (statement_node->type == NodeTypeReturnExpr) { - if (type_id == TypeIdUnreachable) { + if (type_entry == g->builtin_types.entry_unreachable) { add_node_error(g, statement_node, buf_sprintf("return statement in function with unreachable return type")); return; @@ -275,9 +273,9 @@ static void check_fn_def_control_flow(CodeGen *g, AstNode *node) { } if (!prev_statement_return) { - if (type_id == TypeIdVoid) { + if (type_entry == g->builtin_types.entry_void) { codegen_fn_def->add_implicit_return = true; - } else if (type_id != TypeIdUnreachable) { + } else if (type_entry != g->builtin_types.entry_unreachable) { add_node_error(g, node, buf_sprintf("control reaches end of non-void function")); } @@ -428,41 +426,41 @@ static void analyze_root(CodeGen *g, AstNode *node) { static void define_primitive_types(CodeGen *g) { { TypeTableEntry *entry = allocate(1); - entry->id = TypeIdU8; entry->type_ref = LLVMInt8Type(); buf_init_from_str(&entry->name, "u8"); entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 8, 8, LLVMZigEncoding_DW_ATE_unsigned()); g->type_table.put(&entry->name, entry); + g->builtin_types.entry_u8 = entry; } { TypeTableEntry *entry = allocate(1); - entry->id = TypeIdI32; entry->type_ref = LLVMInt32Type(); buf_init_from_str(&entry->name, "i32"); entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 32, 32, LLVMZigEncoding_DW_ATE_signed()); g->type_table.put(&entry->name, entry); + g->builtin_types.entry_i32 = entry; } { TypeTableEntry *entry = allocate(1); - entry->id = TypeIdVoid; entry->type_ref = LLVMVoidType(); buf_init_from_str(&entry->name, "void"); entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 0, 0, LLVMZigEncoding_DW_ATE_unsigned()); g->type_table.put(&entry->name, entry); + g->builtin_types.entry_void = entry; // invalid types are void - g->invalid_type_entry = entry; + g->builtin_types.entry_invalid = entry; } { TypeTableEntry *entry = allocate(1); - entry->id = TypeIdUnreachable; entry->type_ref = LLVMVoidType(); buf_init_from_str(&entry->name, "unreachable"); - entry->di_type = g->invalid_type_entry->di_type; + entry->di_type = g->builtin_types.entry_invalid->di_type; g->type_table.put(&entry->name, entry); + g->builtin_types.entry_unreachable = entry; } } diff --git a/src/codegen.cpp b/src/codegen.cpp index 56f1fcabda..c5e90cf75a 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -72,11 +72,11 @@ static LLVMZigDIType *to_llvm_debug_type(AstNode *type_node) { return type_node->codegen_node->data.type_node.entry->di_type; } -static bool type_is_unreachable(AstNode *type_node) { +static bool type_is_unreachable(CodeGen *g, AstNode *type_node) { assert(type_node->type == NodeTypeType); assert(type_node->codegen_node); assert(type_node->codegen_node->data.type_node.entry); - return type_node->codegen_node->data.type_node.entry->id == TypeIdUnreachable; + return type_node->codegen_node->data.type_node.entry == g->builtin_types.entry_unreachable; } static void add_debug_source_node(CodeGen *g, AstNode *node) { @@ -137,7 +137,7 @@ static LLVMValueRef gen_fn_call_expr(CodeGen *g, AstNode *node) { LLVMValueRef result = LLVMZigBuildCall(g->builder, fn_table_entry->fn_value, param_values, actual_param_count, fn_table_entry->calling_convention, ""); - if (type_is_unreachable(fn_table_entry->proto_node->data.fn_proto.return_type)) { + if (type_is_unreachable(g, fn_table_entry->proto_node->data.fn_proto.return_type)) { return LLVMBuildUnreachable(g->builder); } else { return result; @@ -509,7 +509,7 @@ void code_gen(CodeGen *g) { LLVMSetLinkage(fn, fn_table_entry->internal_linkage ? LLVMInternalLinkage : LLVMExternalLinkage); - if (type_is_unreachable(fn_proto->return_type)) { + if (type_is_unreachable(g, fn_proto->return_type)) { LLVMAddFunctionAttr(fn, LLVMNoReturnAttribute); } LLVMSetFunctionCallConv(fn, fn_table_entry->calling_convention); @@ -589,27 +589,15 @@ static Buf *to_c_type(CodeGen *g, AstNode *type_node) { TypeTableEntry *type_entry = type_node->codegen_node->data.type_node.entry; assert(type_entry); - switch (type_entry->id) { - case TypeIdUserDefined: - zig_panic("TODO"); - break; - case TypeIdPointer: - zig_panic("TODO"); - break; - case TypeIdU8: - g->c_stdint_used = true; - return buf_create_from_str("uint8_t"); - case TypeIdI32: - g->c_stdint_used = true; - return buf_create_from_str("int32_t"); - case TypeIdVoid: - zig_panic("TODO"); - break; - case TypeIdUnreachable: - zig_panic("TODO"); - break; + if (type_entry == g->builtin_types.entry_u8) { + g->c_stdint_used = true; + return buf_create_from_str("uint8_t"); + } else if (type_entry == g->builtin_types.entry_i32) { + g->c_stdint_used = true; + return buf_create_from_str("int32_t"); + } else { + zig_panic("TODO"); } - zig_unreachable(); } static void generate_h_file(CodeGen *g) { diff --git a/src/semantic_info.hpp b/src/semantic_info.hpp index 70b5bc1000..13a05b1207 100644 --- a/src/semantic_info.hpp +++ b/src/semantic_info.hpp @@ -21,17 +21,7 @@ struct FnTableEntry { unsigned calling_convention; }; -enum TypeId { - TypeIdUserDefined, - TypeIdPointer, - TypeIdU8, - TypeIdI32, - TypeIdVoid, - TypeIdUnreachable, -}; - struct TypeTableEntry { - TypeId id; LLVMTypeRef type_ref; LLVMZigDIType *di_type; @@ -54,7 +44,15 @@ struct CodeGen { HashMap str_table; HashMap type_table; HashMap link_table; - TypeTableEntry *invalid_type_entry; + + struct { + TypeTableEntry *entry_u8; + TypeTableEntry *entry_i32; + TypeTableEntry *entry_void; + TypeTableEntry *entry_unreachable; + TypeTableEntry *entry_invalid; + } builtin_types; + LLVMTargetDataRef target_data_ref; unsigned pointer_size_bytes; bool is_static;