Browse Source

Refactor: Remove dead code

Ginger Bill 9 years ago
parent
commit
f6589d9814
8 changed files with 111 additions and 161 deletions
  1. 8 7
      code/demo.odin
  2. 3 1
      core/fmt.odin
  3. 6 27
      src/checker/checker.cpp
  4. 55 61
      src/checker/expr.cpp
  5. 9 11
      src/checker/stmt.cpp
  6. 0 6
      src/codegen/print_llvm.cpp
  7. 4 1
      src/main.cpp
  8. 26 47
      src/parser.cpp

+ 8 - 7
code/demo.odin

@@ -4,12 +4,13 @@
 #import "mem.odin"
 #import "game.odin"
 
-Vec3 :: struct {
-	x, y, z: f32
-}
-
 main :: proc() {
-	v0 := V
-	v1 := V
-	v2 := V
+	Vector3 :: struct {
+		x, y, z: f32
+	}
+	Entity :: struct {
+		guid:     u64
+		position: Vector3
+	}
+
 }

+ 3 - 1
core/fmt.odin

@@ -80,7 +80,9 @@ print_nl_to_buffer    :: proc(buf: ^[]byte) { print_rune_to_buffer(buf, #rune "\
 print_int_to_buffer :: proc(buf: ^[]byte, i: int) {
 	print_int_base_to_buffer(buf, i, 10);
 }
-__NUM_TO_CHAR_TABLE :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$"
+
+__NUM_TO_CHAR_TABLE := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$"
+
 print_int_base_to_buffer :: proc(buffer: ^[]byte, i, base: int) {
 
 	buf: [65]byte

+ 6 - 27
src/checker/checker.cpp

@@ -267,6 +267,12 @@ CycleChecker *cycle_checker_add(CycleChecker *cc, Entity *e) {
 	return cc;
 }
 
+void cycle_checker_destroy(CycleChecker *cc) {
+	if (cc != NULL && cc->path != NULL)  {
+		gb_array_free(cc->path);
+	}
+}
+
 
 
 Scope *make_scope(Scope *parent, gbAllocator allocator) {
@@ -871,33 +877,6 @@ Map<Entity *> generate_minimum_dependency_map(CheckerInfo *info, Entity *start)
 #include "expr.cpp"
 #include "stmt.cpp"
 
-
-
-struct CycleCheck {
-	gbArray(Entity *) path; // HACK(bill): Memory Leak
-};
-
-void cycle_check_add(CycleCheck *cc, Entity *entity) {
-	if (cc == NULL)
-		return;
-	if (cc->path == NULL) {
-		gb_array_init(cc->path, gb_heap_allocator());
-	}
-	GB_ASSERT(entity->kind == Entity_TypeName);
-	gb_array_append(cc->path, entity);
-}
-
-void check_type_name_cycles(Checker *c, CycleCheck *cc, Entity *e) {
-	GB_ASSERT(e->kind == Entity_TypeName);
-	Type *t = e->type;
-	// if (t->kind == Type_Named) {
-	// 	if (t->Named.type_name == e) {
-	// 		gb_printf("Illegal cycle %.*s!!!\n", LIT(e->token.string));
-	// 		GB_PANIC("!!!");
-	// 	}
-	// }
-}
-
 void init_runtime_types(Checker *c) {
 	if (t_type_info == NULL) {
 		Entity *e = current_scope_lookup_entity(c->global_scope, make_string("Type_Info"));

+ 55 - 61
src/checker/expr.cpp

@@ -65,7 +65,6 @@ b32 check_is_assignable_to(Checker *c, Operand *operand, Type *type, b32 is_argu
 	Type *src = base_type(s);
 	Type *dst = base_type(type);
 
-
 	if (is_type_untyped(src)) {
 		switch (dst->kind) {
 		case Type_Basic:
@@ -86,11 +85,13 @@ b32 check_is_assignable_to(Checker *c, Operand *operand, Type *type, b32 is_argu
 		return true;
 	}
 
-	if (is_type_pointer(dst) && is_type_rawptr(src))
+	if (is_type_pointer(dst) && is_type_rawptr(src)) {
 	    return true;
+	}
 
-	if (is_type_rawptr(dst) && is_type_pointer(src))
+	if (is_type_rawptr(dst) && is_type_pointer(src)) {
 	    return true;
+	}
 
 	if (dst->kind == Type_Array && src->kind == Type_Array) {
 		if (are_types_identical(dst->Array.elem, src->Array.elem)) {
@@ -513,7 +514,6 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, CycleChecke
 
 		struct_type->Record.fields = reordered_fields;
 	}
-
 }
 
 void check_union_type(Checker *c, Type *union_type, AstNode *node, CycleChecker *cycle_checker) {
@@ -848,9 +848,7 @@ void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type, Cycl
 	// if (cycle_checker == NULL) {
 	// 	cycle_checker = &local_cycle_checker;
 	// }
-	// defer (if (local_cycle_checker.path != NULL) {
-	// 	gb_array_free(local_cycle_checker.path);
-	// });
+	// defer (cycle_checker_destroy(&local_cycle_checker));
 
 	check_entity_decl(c, e, NULL, named_type, cycle_checker);
 
@@ -939,8 +937,9 @@ i64 check_array_count(Checker *c, AstNode *e) {
 	if (is_type_untyped(o.type) || is_type_integer(o.type)) {
 		if (o.value.kind == ExactValue_Integer) {
 			i64 count = o.value.value_integer;
-			if (count >= 0)
+			if (count >= 0) {
 				return count;
+			}
 			error(ast_node_token(e), "Invalid array count");
 			return 0;
 		}
@@ -966,8 +965,6 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
 			break;
 		case Addressing_Type: {
 			type = o.type;
-			type->flags |= e->type_flags;
-			set_base_type(named_type, type);
 			goto end;
 		} break;
 		case Addressing_NoValue:
@@ -991,9 +988,7 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
 		case Addressing_Type:
 			GB_ASSERT(o.type != NULL);
 			type = o.type;
-			type->flags |= e->type_flags;
-			set_base_type(named_type, type);
-			return type;
+			goto end;
 		case Addressing_NoValue:
 			err_str = expr_to_string(e);
 			error(ast_node_token(e), "`%s` used as a type", err_str);
@@ -1007,19 +1002,29 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
 
 	case_ast_node(pe, ParenExpr, e);
 		type = check_type(c, pe->expr, named_type, cycle_checker);
-		type->flags |= e->type_flags;
-		return type;
+		goto end;
+	case_end;
+
+	case_ast_node(ue, UnaryExpr, e);
+		if (ue->op.kind == Token_Pointer) {
+			type = make_type_pointer(c->allocator, check_type(c, ue->expr));
+			goto end;
+		}
+	case_end;
+
+	case_ast_node(pt, PointerType, e);
+		Type *elem = check_type(c, pt->type);
+		type = make_type_pointer(c->allocator, elem);
+		goto end;
 	case_end;
 
 	case_ast_node(at, ArrayType, e);
 		if (at->count != NULL) {
-			type = make_type_array(c->allocator,
-			                       check_type(c, at->elem, NULL, cycle_checker),
-			                       check_array_count(c, at->count));
-			set_base_type(named_type, type);
+			Type *elem = check_type(c, at->elem, NULL, cycle_checker);
+			type = make_type_array(c->allocator, elem, check_array_count(c, at->count));
 		} else {
-			type = make_type_slice(c->allocator, check_type(c, at->elem));
-			set_base_type(named_type, type);
+			Type *elem = check_type(c, at->elem);
+			type = make_type_slice(c->allocator, elem);
 		}
 		goto end;
 	case_end;
@@ -1034,7 +1039,6 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
 			error(ast_node_token(vt->elem), "Vector element type must be numerical or a boolean. Got `%s`", err_str);
 		}
 		type = make_type_vector(c->allocator, elem, count);
-		set_base_type(named_type, type);
 		goto end;
 	case_end;
 
@@ -1078,12 +1082,6 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
 		goto end;
 	case_end;
 
-	case_ast_node(pt, PointerType, e);
-		type = make_type_pointer(c->allocator, check_type(c, pt->type));
-		set_base_type(named_type, type);
-		goto end;
-	case_end;
-
 	case_ast_node(pt, ProcType, e);
 		type = alloc_type(c->allocator, Type_Proc);
 		set_base_type(named_type, type);
@@ -1093,34 +1091,27 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
 		goto end;
 	case_end;
 
-	default: {
-		if (e->kind == AstNode_CallExpr) {
-			Operand o = {};
-			check_expr_or_type(c, &o, e);
-			if (o.mode == Addressing_Type) {
-				type = o.type;
-				goto end;
-			}
-		} else if (e->kind == AstNode_UnaryExpr) {
-			ast_node(ue, UnaryExpr, e);
-			if (ue->op.kind == Token_Pointer) {
-				type = make_type_pointer(c->allocator, check_type(c, ue->expr));
-				set_base_type(named_type, type);
-				goto end;
-			}
+	case_ast_node(ce, CallExpr, e);
+		Operand o = {};
+		check_expr_or_type(c, &o, e);
+		if (o.mode == Addressing_Type) {
+			type = o.type;
+			goto end;
 		}
-
-		err_str = expr_to_string(e);
-		error(ast_node_token(e), "`%s` is not a type", err_str);
-	} break;
+	case_end;
 	}
+	err_str = expr_to_string(e);
+	error(ast_node_token(e), "`%s` is not a type", err_str);
 
 	type = t_invalid;
-	set_base_type(named_type, type);
-
 end:
+	if (type == NULL) {
+		type = t_invalid;
+	}
+
+	set_base_type(named_type, type);
 	GB_ASSERT(is_type_typed(type));
-	type->flags |= e->type_flags;
+
 	add_type_and_value(&c->info, e, Addressing_Type, type, null_value);
 	return type;
 }
@@ -1550,19 +1541,13 @@ b32 check_is_castable_to(Checker *c, Operand *operand, Type *y) {
 			return true;
 	}
 
-	// // untyped integers -> pointers
-	// if (is_type_untyped(xb) && is_type_integer(xb)) {
-	// 	if (is_type_pointer(yb))
-	// 		return true;
-	// }
-
 	// (u)int <-> pointer
-	if (is_type_pointer(xb) || (is_type_int_or_uint(xb) && !is_type_untyped(xb))) {
+	if (is_type_int_or_uint(xb) && !is_type_untyped(xb)) {
 		if (is_type_pointer(yb))
 			return true;
 	}
 	if (is_type_pointer(xb)) {
-		if (is_type_pointer(yb) || (is_type_int_or_uint(yb) && !is_type_untyped(yb)))
+		if (is_type_int_or_uint(yb) && !is_type_untyped(yb))
 			return true;
 	}
 
@@ -1643,7 +1628,9 @@ void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
 				}
 			}
 		} else if (check_is_castable_to(c, x, type)) {
-			x->mode = Addressing_Value;
+			if (x->mode != Addressing_Constant) {
+				x->mode = Addressing_Value;
+			}
 			can_convert = true;
 		}
 
@@ -3963,17 +3950,24 @@ gbString write_expr_to_string(gbString str, AstNode *node) {
 	case_end;
 
 	case_ast_node(st, StructType, node);
-		str = gb_string_appendc(str, "struct{");
+		str = gb_string_appendc(str, "struct ");
+		if (st->is_packed)  str = gb_string_appendc(str, "#packed ");
+		if (st->is_ordered) str = gb_string_appendc(str, "#ordered ");
 		// str = write_fields_to_string(str, st->decl_list, ", ");
 		str = gb_string_appendc(str, "}");
 	case_end;
 
 	case_ast_node(st, RawUnionType, node);
-		str = gb_string_appendc(str, "raw_union{");
+		str = gb_string_appendc(str, "raw_union {");
 		// str = write_fields_to_string(str, st->decl_list, ", ");
 		str = gb_string_appendc(str, "}");
 	case_end;
 
+	case_ast_node(st, UnionType, node);
+		str = gb_string_appendc(str, "union {");
+		// str = write_fields_to_string(str, st->decl_list, ", ");
+		str = gb_string_appendc(str, "}");
+	case_end;
 
 	case_ast_node(et, EnumType, node);
 		str = gb_string_appendc(str, "enum ");

+ 9 - 11
src/checker/stmt.cpp

@@ -429,14 +429,14 @@ void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_e
 
 	if (type_expr) {
 		Type *t = check_type(c, type_expr);
-		if (!is_type_constant_type(t)) {
-			gbString str = type_to_string(t);
-			defer (gb_string_free(str));
-			error(ast_node_token(type_expr),
-			      "Invalid constant type `%s`", str);
-			e->type = t_invalid;
-			return;
-		}
+		// if (!is_type_constant_type(t)) {
+		// 	gbString str = type_to_string(t);
+		// 	defer (gb_string_free(str));
+		// 	error(ast_node_token(type_expr),
+		// 	      "Invalid constant type `%s`", str);
+		// 	e->type = t_invalid;
+		// 	return;
+		// }
 		e->type = t;
 	}
 
@@ -460,9 +460,7 @@ void check_type_decl(Checker *c, Entity *e, AstNode *type_expr, Type *def, Cycle
 	if (cycle_checker == NULL) {
 		cycle_checker = &local_cycle_checker;
 	}
-	defer (if (local_cycle_checker.path != NULL) {
-		gb_array_free(local_cycle_checker.path);
-	});
+	defer (cycle_checker_destroy(&local_cycle_checker));
 
 	Type *bt = check_type(c, type_expr, named, cycle_checker_add(cycle_checker, e));
 	named->Named.base = bt;

+ 0 - 6
src/codegen/print_llvm.cpp

@@ -597,9 +597,6 @@ void ssa_print_instr(ssaFileBuffer *f, ssaModule *m, ssaValue *value) {
 	case ssaInstr_Store: {
 		Type *type = ssa_type(instr);
 		ssa_fprintf(f, "store ");
-		if ((type->flags & TypeFlag_volatile) != 0) {
-			ssa_fprintf(f, "volatile ");
-		}
 		ssa_print_type(f, m, type);
 		ssa_fprintf(f, " ");
 		ssa_print_value(f, m, instr->Store.value, type);
@@ -613,9 +610,6 @@ void ssa_print_instr(ssaFileBuffer *f, ssaModule *m, ssaValue *value) {
 	case ssaInstr_Load: {
 		Type *type = instr->Load.type;
 		ssa_fprintf(f, "%%%d = load ", value->id);
-		if ((type->flags & TypeFlag_volatile) != 0) {
-			ssa_fprintf(f, "volatile ");
-		}
 		ssa_print_type(f, m, type);
 		ssa_fprintf(f, ", ");
 		ssa_print_type(f, m, type);

+ 4 - 1
src/main.cpp

@@ -176,6 +176,8 @@ int main(int argc, char **argv) {
 	isize base_name_len = gb_path_extension(output_name)-1 - output_name;
 	String output = make_string(cast(u8 *)output_name, base_name_len);
 
+	int optimization_level = 0;
+	optimization_level = gb_clamp(optimization_level, 0, 3);
 
 	i32 exit_code = 0;
 	// For more passes arguments: http://llvm.org/docs/Passes.html
@@ -199,11 +201,12 @@ int main(int argc, char **argv) {
 
 	// For more arguments: http://llvm.org/docs/CommandGuide/llc.html
 	exit_code = win32_exec_command_line_app(
-		"%.*sbin/llc %.*s.bc -filetype=obj -O0 "
+		"%.*sbin/llc %.*s.bc -filetype=obj -O%d "
 		"%.*s "
 		"",
 		LIT(module_dir),
 		LIT(output),
+		optimization_level,
 		LIT(arch_data.llc_flags));
 	if (exit_code != 0) {
 		return exit_code;

+ 26 - 47
src/parser.cpp

@@ -82,14 +82,6 @@ enum VarDeclTag {
 	VarDeclTag_thread_local = GB_BIT(0),
 };
 
-
-enum TypeFlag : u32 {
-	TypeFlag_thread_local = GB_BIT(0),
-	TypeFlag_volatile     = GB_BIT(1),
-	TypeFlag_atomic       = GB_BIT(2),
-
-};
-
 enum StmtStateFlag : u32 {
 	StmtStateFlag_bounds_check    = GB_BIT(0),
 	StmtStateFlag_no_bounds_check = GB_BIT(1),
@@ -325,20 +317,23 @@ String const ast_node_strings[] = {
 #undef AST_NODE_KIND
 };
 
+#define AST_NODE_KIND(_kind_name_, name, ...) typedef __VA_ARGS__ GB_JOIN2(AstNode, _kind_name_);
+	AST_NODE_KINDS
+#undef AST_NODE_KIND
+
 struct AstNode {
 	AstNodeKind kind;
 	// AstNode *prev, *next; // NOTE(bill): allow for Linked list
-	u32 type_flags;
 	u32 stmt_state_flags;
 	union {
-#define AST_NODE_KIND(_kind_name_, name, ...) __VA_ARGS__ _kind_name_;
+#define AST_NODE_KIND(_kind_name_, name, ...) GB_JOIN2(AstNode, _kind_name_) _kind_name_;
 	AST_NODE_KINDS
 #undef AST_NODE_KIND
 	};
 };
 
 
-#define ast_node(n_, Kind_, node_) auto *n_ = &(node_)->Kind_; GB_ASSERT((node_)->kind == GB_JOIN2(AstNode_, Kind_))
+#define ast_node(n_, Kind_, node_) GB_JOIN2(AstNode, Kind_) *n_ = &(node_)->Kind_; GB_ASSERT((node_)->kind == GB_JOIN2(AstNode_, Kind_))
 #define case_ast_node(n_, Kind_, node_) case GB_JOIN2(AstNode_, Kind_): { ast_node(n_, Kind_, node_);
 #define case_end } break;
 
@@ -371,7 +366,10 @@ Token ast_node_token(AstNode *node) {
 	case AstNode_ProcLit:
 		return ast_node_token(node->ProcLit.type);
 	case AstNode_CompoundLit:
-		return ast_node_token(node->CompoundLit.type);
+		if (node->CompoundLit.type != NULL) {
+			return ast_node_token(node->CompoundLit.type);
+		}
+		return node->CompoundLit.open;
 	case AstNode_TagExpr:
 		return node->TagExpr.token;
 	case AstNode_BadExpr:
@@ -1964,14 +1962,6 @@ AstNodeArray parse_struct_params(AstFile *f, isize *decl_count_, b32 using_allow
 
 AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
 	switch (f->curr_token.kind) {
-	case Token_volatile:
-		next_token(f);
-		return parse_identifier_or_type(f, flags | TypeFlag_volatile);
-
-	case Token_atomic:
-		next_token(f);
-		return parse_identifier_or_type(f, flags | TypeFlag_atomic);
-
 	case Token_Identifier: {
 		AstNode *e = parse_identifier(f);
 		while (f->curr_token.kind == Token_Period) {
@@ -1984,15 +1974,11 @@ AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
 			// HACK NOTE(bill): For type_of_val(expr)
 			e = parse_call_expr(f, e);
 		}
-		e->type_flags = flags;
 		return e;
 	}
 
-	case Token_Pointer: {
-		AstNode *e = make_pointer_type(f, expect_token(f, Token_Pointer), parse_type(f));
-		e->type_flags = flags;
-		return e;
-	}
+	case Token_Pointer:
+		return make_pointer_type(f, expect_token(f, Token_Pointer), parse_type(f));
 
 	case Token_OpenBracket: {
 		f->expr_level++;
@@ -2008,7 +1994,6 @@ AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
 		expect_token(f, Token_CloseBracket);
 		f->expr_level--;
 		AstNode *e = make_array_type(f, token, count_expr, parse_type(f));
-		e->type_flags = flags;
 		return e;
 	}
 
@@ -2018,9 +2003,7 @@ AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
 		AstNode *count_expr = parse_expr(f, false);
 		expect_token(f, Token_CloseBrace);
 		f->expr_level--;
-		AstNode *e = make_vector_type(f, token, count_expr, parse_type(f));
-		e->type_flags = flags;
-		return e;
+		return make_vector_type(f, token, count_expr, parse_type(f));
 	}
 
 	case Token_struct: {
@@ -2030,11 +2013,17 @@ AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
 		while (allow_token(f, Token_Hash)) {
 			Token tag = expect_token(f, Token_Identifier);
 			if (tag.string == "packed") {
+				if (is_packed) {
+					syntax_error(tag, "Duplicate struct tag `#%.*s`", LIT(tag.string));
+				}
 				is_packed = true;
 			} else if (tag.string == "ordered") {
+				if (is_ordered) {
+					syntax_error(tag, "Duplicate struct tag `#%.*s`", LIT(tag.string));
+				}
 				is_ordered = true;
 			} else {
-				syntax_error(tag, "Expected a `#packed` or `#ordered` tag");
+				syntax_error(tag, "Invalid struct tag `#%.*s`", LIT(tag.string));
 			}
 		}
 
@@ -2047,9 +2036,7 @@ AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
 		AstNodeArray decls = parse_struct_params(f, &decl_count, true);
 		Token close = expect_token(f, Token_CloseBrace);
 
-		AstNode *e = make_struct_type(f, token, decls, decl_count, is_packed, is_ordered);
-		e->type_flags = flags;
-		return e;
+		return make_struct_type(f, token, decls, decl_count, is_packed, is_ordered);
 	} break;
 
 	case Token_union: {
@@ -2059,9 +2046,7 @@ AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
 		AstNodeArray decls = parse_struct_params(f, &decl_count, false);
 		Token close = expect_token(f, Token_CloseBrace);
 
-		AstNode *e = make_union_type(f, token, decls, decl_count);
-		e->type_flags = flags;
-		return e;
+		return make_union_type(f, token, decls, decl_count);
 	}
 
 	case Token_raw_union: {
@@ -2071,9 +2056,7 @@ AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
 		AstNodeArray decls = parse_struct_params(f, &decl_count, true);
 		Token close = expect_token(f, Token_CloseBrace);
 
-		AstNode *e = make_raw_union_type(f, token, decls, decl_count);
-		e->type_flags = flags;
-		return e;
+		return make_raw_union_type(f, token, decls, decl_count);
 	}
 
 	case Token_enum: {
@@ -2108,9 +2091,7 @@ AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
 
 		close = expect_token(f, Token_CloseBrace);
 
-		AstNode *e = make_enum_type(f, token, base_type, fields);
-		e->type_flags = flags;
-		return e;
+		return make_enum_type(f, token, base_type, fields);
 	}
 
 	case Token_proc: {
@@ -2121,7 +2102,6 @@ AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
 		return type;
 	}
 
-
 	case Token_OpenParen: {
 		// NOTE(bill): Skip the paren expression
 		AstNode *type;
@@ -2129,9 +2109,8 @@ AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
 		open = expect_token(f, Token_OpenParen);
 		type = parse_type(f);
 		close = expect_token(f, Token_CloseParen);
-		AstNode *e = make_paren_expr(f, type, open, close);
-		e->type_flags = flags;
-		return e;
+		return type;
+		// return make_paren_expr(f, type, open, close);
 	}
 
 	// TODO(bill): Why is this even allowed? Is this a parsing error?