authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-11-30 15:36:58-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-11-30 15:36:58-07:00
logef482ece7c047e898fdc2ea15ba4216c15309d0c
treeb674912fde81d65b2cf42afe64ac97a91679716f
parentb77c423f773cdf05c95b293f44852ec5a39fc4ed

no more TypeId. use g->builtin_types.


3 files changed, 32 insertions(+), 48 deletions(-)

src/analyze.cpp+11-13
......@@ -57,7 +57,7 @@ static void resolve_type(CodeGen *g, AstNode *node) {
5757 } else {
5858 add_node_error(g, node,
5959 buf_sprintf("invalid type name: '%s'", buf_ptr(name)));
60 type_node->entry = g->invalid_type_entry;
60 type_node->entry = g->builtin_types.entry_invalid;
6161 }
6262 break;
6363 }
......@@ -65,7 +65,7 @@ static void resolve_type(CodeGen *g, AstNode *node) {
6565 {
6666 resolve_type(g, node->data.type.child_type);
6767 TypeNode *child_type_node = &node->data.type.child_type->codegen_node->data.type_node;
68 if (child_type_node->entry->id == TypeIdUnreachable) {
68 if (child_type_node->entry == g->builtin_types.entry_unreachable) {
6969 add_node_error(g, node,
7070 buf_create_from_str("pointer to unreachable not allowed"));
7171 }
......@@ -77,7 +77,6 @@ static void resolve_type(CodeGen *g, AstNode *node) {
7777 type_node->entry = *parent_pointer;
7878 } else {
7979 TypeTableEntry *entry = allocate<TypeTableEntry>(1);
80 entry->id = TypeIdPointer;
8180 entry->type_ref = LLVMPointerType(child_type_node->entry->type_ref, 0);
8281 buf_resize(&entry->name, 0);
8382 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) {
249248 assert(return_type_node->codegen_node);
250249 TypeTableEntry *type_entry = return_type_node->codegen_node->data.type_node.entry;
251250 assert(type_entry);
252 TypeId type_id = type_entry->id;
253251
254252 AstNode *body_node = node->data.fn_def.body;
255253 assert(body_node->type == NodeTypeBlock);
......@@ -261,7 +259,7 @@ static void check_fn_def_control_flow(CodeGen *g, AstNode *node) {
261259 for (int i = 0; i < body_node->data.block.statements.length; i += 1) {
262260 AstNode *statement_node = body_node->data.block.statements.at(i);
263261 if (statement_node->type == NodeTypeReturnExpr) {
264 if (type_id == TypeIdUnreachable) {
262 if (type_entry == g->builtin_types.entry_unreachable) {
265263 add_node_error(g, statement_node,
266264 buf_sprintf("return statement in function with unreachable return type"));
267265 return;
......@@ -275,9 +273,9 @@ static void check_fn_def_control_flow(CodeGen *g, AstNode *node) {
275273 }
276274
277275 if (!prev_statement_return) {
278 if (type_id == TypeIdVoid) {
276 if (type_entry == g->builtin_types.entry_void) {
279277 codegen_fn_def->add_implicit_return = true;
280 } else if (type_id != TypeIdUnreachable) {
278 } else if (type_entry != g->builtin_types.entry_unreachable) {
281279 add_node_error(g, node,
282280 buf_sprintf("control reaches end of non-void function"));
283281 }
......@@ -428,41 +426,41 @@ static void analyze_root(CodeGen *g, AstNode *node) {
428426static void define_primitive_types(CodeGen *g) {
429427 {
430428 TypeTableEntry *entry = allocate<TypeTableEntry>(1);
431 entry->id = TypeIdU8;
432429 entry->type_ref = LLVMInt8Type();
433430 buf_init_from_str(&entry->name, "u8");
434431 entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 8, 8,
435432 LLVMZigEncoding_DW_ATE_unsigned());
436433 g->type_table.put(&entry->name, entry);
434 g->builtin_types.entry_u8 = entry;
437435 }
438436 {
439437 TypeTableEntry *entry = allocate<TypeTableEntry>(1);
440 entry->id = TypeIdI32;
441438 entry->type_ref = LLVMInt32Type();
442439 buf_init_from_str(&entry->name, "i32");
443440 entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 32, 32,
444441 LLVMZigEncoding_DW_ATE_signed());
445442 g->type_table.put(&entry->name, entry);
443 g->builtin_types.entry_i32 = entry;
446444 }
447445 {
448446 TypeTableEntry *entry = allocate<TypeTableEntry>(1);
449 entry->id = TypeIdVoid;
450447 entry->type_ref = LLVMVoidType();
451448 buf_init_from_str(&entry->name, "void");
452449 entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 0, 0,
453450 LLVMZigEncoding_DW_ATE_unsigned());
454451 g->type_table.put(&entry->name, entry);
452 g->builtin_types.entry_void = entry;
455453
456454 // invalid types are void
457 g->invalid_type_entry = entry;
455 g->builtin_types.entry_invalid = entry;
458456 }
459457 {
460458 TypeTableEntry *entry = allocate<TypeTableEntry>(1);
461 entry->id = TypeIdUnreachable;
462459 entry->type_ref = LLVMVoidType();
463460 buf_init_from_str(&entry->name, "unreachable");
464 entry->di_type = g->invalid_type_entry->di_type;
461 entry->di_type = g->builtin_types.entry_invalid->di_type;
465462 g->type_table.put(&entry->name, entry);
463 g->builtin_types.entry_unreachable = entry;
466464 }
467465}
468466
src/codegen.cpp+12-24
......@@ -72,11 +72,11 @@ static LLVMZigDIType *to_llvm_debug_type(AstNode *type_node) {
7272 return type_node->codegen_node->data.type_node.entry->di_type;
7373}
7474
75static bool type_is_unreachable(AstNode *type_node) {
75static bool type_is_unreachable(CodeGen *g, AstNode *type_node) {
7676 assert(type_node->type == NodeTypeType);
7777 assert(type_node->codegen_node);
7878 assert(type_node->codegen_node->data.type_node.entry);
79 return type_node->codegen_node->data.type_node.entry->id == TypeIdUnreachable;
79 return type_node->codegen_node->data.type_node.entry == g->builtin_types.entry_unreachable;
8080}
8181
8282static void add_debug_source_node(CodeGen *g, AstNode *node) {
......@@ -137,7 +137,7 @@ static LLVMValueRef gen_fn_call_expr(CodeGen *g, AstNode *node) {
137137 LLVMValueRef result = LLVMZigBuildCall(g->builder, fn_table_entry->fn_value,
138138 param_values, actual_param_count, fn_table_entry->calling_convention, "");
139139
140 if (type_is_unreachable(fn_table_entry->proto_node->data.fn_proto.return_type)) {
140 if (type_is_unreachable(g, fn_table_entry->proto_node->data.fn_proto.return_type)) {
141141 return LLVMBuildUnreachable(g->builder);
142142 } else {
143143 return result;
......@@ -509,7 +509,7 @@ void code_gen(CodeGen *g) {
509509
510510 LLVMSetLinkage(fn, fn_table_entry->internal_linkage ? LLVMInternalLinkage : LLVMExternalLinkage);
511511
512 if (type_is_unreachable(fn_proto->return_type)) {
512 if (type_is_unreachable(g, fn_proto->return_type)) {
513513 LLVMAddFunctionAttr(fn, LLVMNoReturnAttribute);
514514 }
515515 LLVMSetFunctionCallConv(fn, fn_table_entry->calling_convention);
......@@ -589,27 +589,15 @@ static Buf *to_c_type(CodeGen *g, AstNode *type_node) {
589589 TypeTableEntry *type_entry = type_node->codegen_node->data.type_node.entry;
590590 assert(type_entry);
591591
592 switch (type_entry->id) {
593 case TypeIdUserDefined:
594 zig_panic("TODO");
595 break;
596 case TypeIdPointer:
597 zig_panic("TODO");
598 break;
599 case TypeIdU8:
600 g->c_stdint_used = true;
601 return buf_create_from_str("uint8_t");
602 case TypeIdI32:
603 g->c_stdint_used = true;
604 return buf_create_from_str("int32_t");
605 case TypeIdVoid:
606 zig_panic("TODO");
607 break;
608 case TypeIdUnreachable:
609 zig_panic("TODO");
610 break;
592 if (type_entry == g->builtin_types.entry_u8) {
593 g->c_stdint_used = true;
594 return buf_create_from_str("uint8_t");
595 } else if (type_entry == g->builtin_types.entry_i32) {
596 g->c_stdint_used = true;
597 return buf_create_from_str("int32_t");
598 } else {
599 zig_panic("TODO");
611600 }
612 zig_unreachable();
613601}
614602
615603static void generate_h_file(CodeGen *g) {
src/semantic_info.hpp+9-11
......@@ -21,17 +21,7 @@ struct FnTableEntry {
2121 unsigned calling_convention;
2222};
2323
24enum TypeId {
25 TypeIdUserDefined,
26 TypeIdPointer,
27 TypeIdU8,
28 TypeIdI32,
29 TypeIdVoid,
30 TypeIdUnreachable,
31};
32
3324struct TypeTableEntry {
34 TypeId id;
3525 LLVMTypeRef type_ref;
3626 LLVMZigDIType *di_type;
3727
......@@ -54,7 +44,15 @@ struct CodeGen {
5444 HashMap<Buf *, LLVMValueRef, buf_hash, buf_eql_buf> str_table;
5545 HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> type_table;
5646 HashMap<Buf *, bool, buf_hash, buf_eql_buf> link_table;
57 TypeTableEntry *invalid_type_entry;
47
48 struct {
49 TypeTableEntry *entry_u8;
50 TypeTableEntry *entry_i32;
51 TypeTableEntry *entry_void;
52 TypeTableEntry *entry_unreachable;
53 TypeTableEntry *entry_invalid;
54 } builtin_types;
55
5856 LLVMTargetDataRef target_data_ref;
5957 unsigned pointer_size_bytes;
6058 bool is_static;