Browse Source

Remove non-InContext type creations

gingerBill 4 years ago
parent
commit
0d044eabac
2 changed files with 24 additions and 114 deletions
  1. 0 104
      src/llvm_abi.cpp
  2. 24 10
      src/llvm_backend.cpp

+ 0 - 104
src/llvm_abi.cpp

@@ -271,110 +271,6 @@ i64 lb_alignof(LLVMTypeRef type) {
 	return 1;
 	return 1;
 }
 }
 
 
-#if 0
-Type *lb_abi_to_odin_type(lbModule *m, LLVMTypeRef type, bool is_return, u32 level = 0) {
-	Type **found = map_get(&m->llvm_types, hash_pointer(type));
-	if (found) {
-		return *found;
-	}
-	GB_ASSERT_MSG(level < 64, "%s %d", LLVMPrintTypeToString(type), is_return);
-
-	LLVMTypeKind kind = LLVMGetTypeKind(type);
-	switch (kind) {
-	case LLVMVoidTypeKind:
-		return nullptr;
-	case LLVMIntegerTypeKind:
-		{
-			unsigned w = LLVMGetIntTypeWidth(type);
-			if (w == 1) {
-				return t_llvm_bool;
-			}
-			unsigned bytes = (w + 7)/8;
-			switch (bytes) {
-			case 1:  return t_u8;
-			case 2:  return t_u16;
-			case 4:  return t_u32;
-			case 8:  return t_u64;
-			case 16: return t_u128;
-			}
-			GB_PANIC("Unhandled integer type");
-		}
-	case LLVMFloatTypeKind:
-		return t_f32;
-	case LLVMDoubleTypeKind:
-		return t_f64;
-	case LLVMPointerTypeKind:
-		{
-			LLVMTypeRef elem = LLVMGetElementType(type);
-			if (lb_is_type_kind(elem, LLVMFunctionTypeKind)) {
-				unsigned param_count = LLVMCountParamTypes(elem);
-				LLVMTypeRef *params = gb_alloc_array(heap_allocator(), LLVMTypeRef, param_count);
-				defer (gb_free(heap_allocator(), params));
-				LLVMGetParamTypes(elem, params);
-
-				Type **param_types = gb_alloc_array(heap_allocator(), Type *, param_count);
-				defer (gb_free(heap_allocator(), param_types));
-
-				for (unsigned i = 0; i < param_count; i++) {
-					param_types[i] = lb_abi_to_odin_type(m, params[i], false, level+1);
-				}
-
-				LLVMTypeRef ret = LLVMGetReturnType(elem);
-				Type *ret_type = lb_abi_to_odin_type(m, ret, true, level+1);
-
-				bool is_c_vararg = !!LLVMIsFunctionVarArg(elem);
-				return alloc_type_proc_from_types(param_types, param_count, ret_type, is_c_vararg);
-			}
-			return alloc_type_pointer(lb_abi_to_odin_type(m, elem, false, level+1));
-		}
-	case LLVMFunctionTypeKind:
-		GB_PANIC("LLVMFunctionTypeKind should not be seen on its own");
-		break;
-
-	case LLVMStructTypeKind:
-		{
-			unsigned field_count = LLVMCountStructElementTypes(type);
-			Type **fields = gb_alloc_array(heap_allocator(), Type *, field_count);
-			for (unsigned i = 0; i < field_count; i++) {
-				LLVMTypeRef field_type = LLVMStructGetTypeAtIndex(type, i);
-				if (lb_is_type_kind(field_type, LLVMPointerTypeKind) && level > 0) {
-					fields[i] = t_rawptr;
-				} else {
-					fields[i] = lb_abi_to_odin_type(m, field_type, false, level+1);
-				}
-			}
-			if (is_return) {
-				return alloc_type_tuple_from_field_types(fields, field_count, !!LLVMIsPackedStruct(type), false);
-			} else {
-				return alloc_type_struct_from_field_types(fields, field_count, !!LLVMIsPackedStruct(type));
-			}
-		}
-		break;
-	case LLVMArrayTypeKind:
-		{
-
-			i64 count = LLVMGetArrayLength(type);
-			Type *elem = lb_abi_to_odin_type(m, LLVMGetElementType(type), false, level+1);
-			return alloc_type_array(elem, count);
-		}
-		break;
-
-	case LLVMX86_MMXTypeKind:
-		return t_vector_x86_mmx;
-	case LLVMVectorTypeKind:
-		{
-			i64 count = LLVMGetVectorSize(type);
-			Type *elem = lb_abi_to_odin_type(m, LLVMGetElementType(type), false, level+1);
-			return alloc_type_simd_vector(count, elem);
-		}
-
-	}
-	GB_PANIC("Unhandled type for lb_abi_to_odin_type -> %s", LLVMPrintTypeToString(type));
-
-	return 0;
-}
-#endif
-
 
 
 #define LB_ABI_INFO(name) lbFunctionType *name(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, LLVMTypeRef return_type, bool return_is_defined, ProcCallingConvention calling_convention)
 #define LB_ABI_INFO(name) lbFunctionType *name(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, LLVMTypeRef return_type, bool return_is_defined, ProcCallingConvention calling_convention)
 typedef LB_ABI_INFO(lbAbiInfoType);
 typedef LB_ABI_INFO(lbAbiInfoType);

+ 24 - 10
src/llvm_backend.cpp

@@ -1110,7 +1110,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
 
 
 		case Basic_uintptr: return LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.word_size);
 		case Basic_uintptr: return LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.word_size);
 
 
-		case Basic_rawptr: return LLVMPointerType(LLVMInt8Type(), 0);
+		case Basic_rawptr: return LLVMPointerType(LLVMInt8TypeInContext(ctx), 0);
 		case Basic_string:
 		case Basic_string:
 			{
 			{
 				char const *name = "..string";
 				char const *name = "..string";
@@ -1126,7 +1126,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
 				LLVMStructSetBody(type, fields, 2, false);
 				LLVMStructSetBody(type, fields, 2, false);
 				return type;
 				return type;
 			}
 			}
-		case Basic_cstring: return LLVMPointerType(LLVMInt8Type(), 0);
+		case Basic_cstring: return LLVMPointerType(LLVMInt8TypeInContext(ctx), 0);
 		case Basic_any:
 		case Basic_any:
 			{
 			{
 				char const *name = "..any";
 				char const *name = "..any";
@@ -1452,21 +1452,35 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
 			}
 			}
 			if (param_index < param_count) {
 			if (param_index < param_count) {
 				params[param_index++] = lb_type(m, t_rawptr);
 				params[param_index++] = lb_type(m, t_rawptr);
-				// params[param_index++] = lb_type(m, t_context_ptr);
 			}
 			}
 			GB_ASSERT(param_index == param_count);
 			GB_ASSERT(param_index == param_count);
 
 
-
 			lbFunctionType *ft = lb_get_abi_info(m->ctx, params, param_count, ret, ret != nullptr, type->Proc.calling_convention);
 			lbFunctionType *ft = lb_get_abi_info(m->ctx, params, param_count, ret, ret != nullptr, type->Proc.calling_convention);
+			{
+				for_array(j, ft->args) {
+					auto arg = ft->args[j];
+					GB_ASSERT_MSG(LLVMGetTypeContext(arg.type) == ft->ctx,
+					              "\n\t%s %td/%td"
+					              "\n\tArgTypeCtx: %p\n\tCurrentCtx: %p\n\tGlobalCtx:  %p",
+					              LLVMPrintTypeToString(arg.type),
+					              j, ft->args.count,
+					              LLVMGetTypeContext(arg.type), ft->ctx, LLVMGetGlobalContext());
+				}
+				GB_ASSERT_MSG(LLVMGetTypeContext(ft->ret.type) == ft->ctx,
+				              "\n\t%s"
+				              "\n\tRetTypeCtx: %p\n\tCurrentCtx: %p\n\tGlobalCtx:  %p",
+				              LLVMPrintTypeToString(ft->ret.type),
+				              LLVMGetTypeContext(ft->ret.type), ft->ctx, LLVMGetGlobalContext());
+			}
+
 			map_set(&m->function_type_map, hash_type(type), ft);
 			map_set(&m->function_type_map, hash_type(type), ft);
 			LLVMTypeRef new_abi_fn_ptr_type = lb_function_type_to_llvm_ptr(ft, type->Proc.c_vararg);
 			LLVMTypeRef new_abi_fn_ptr_type = lb_function_type_to_llvm_ptr(ft, type->Proc.c_vararg);
 			LLVMTypeRef new_abi_fn_type = LLVMGetElementType(new_abi_fn_ptr_type);
 			LLVMTypeRef new_abi_fn_type = LLVMGetElementType(new_abi_fn_ptr_type);
 
 
-			// LLVMTypeRef new_ret = LLVMGetReturnType(new_abi_fn_type);
-			// LLVMTypeRef old_ret = LLVMGetReturnType(old_abi_fn_type);
-			// unsigned new_count = LLVMCountParamTypes(new_abi_fn_type);
-			// unsigned old_count = LLVMCountParamTypes(old_abi_fn_type);
-			// GB_ASSERT_MSG(new_count == old_count, "%u %u, %s %s", new_count, old_count, LLVMPrintTypeToString(new_abi_fn_type), LLVMPrintTypeToString(old_abi_fn_type));
+			GB_ASSERT_MSG(LLVMGetTypeContext(new_abi_fn_type) == m->ctx,
+			              "\n\tFuncTypeCtx: %p\n\tCurrentCtx:  %p\n\tGlobalCtx:   %p",
+			              LLVMGetTypeContext(new_abi_fn_type), m->ctx, LLVMGetGlobalContext());
+
 			return new_abi_fn_ptr_type;
 			return new_abi_fn_ptr_type;
 		}
 		}
 
 
@@ -12898,7 +12912,7 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
 void lb_init_module(lbModule *m, Checker *c) {
 void lb_init_module(lbModule *m, Checker *c) {
 	m->info = &c->info;
 	m->info = &c->info;
 
 
-#if 1
+#if 0
 	m->ctx = LLVMGetGlobalContext();
 	m->ctx = LLVMGetGlobalContext();
 #else
 #else
 	m->ctx = LLVMContextCreate();
 	m->ctx = LLVMContextCreate();