Browse Source

Fix calling convention for new LLVM ABI, and change`PtrSet` index to be `u32` rather than `isize`

gingerBill 4 years ago
parent
commit
db0bcbc4f4
5 changed files with 71 additions and 59 deletions
  1. 3 6
      src/checker.cpp
  2. 11 13
      src/llvm_abi.cpp
  3. 16 11
      src/llvm_backend.cpp
  4. 32 29
      src/ptr_set.cpp
  5. 9 0
      src/types.cpp

+ 3 - 6
src/checker.cpp

@@ -1679,8 +1679,6 @@ void add_dependency_to_set(Checker *c, Entity *entity) {
 	CheckerInfo *info = &c->info;
 	auto *set = &info->minimum_dependency_set;
 
-	String name = entity->token.string;
-
 	if (entity->type != nullptr &&
 	    is_type_polymorphic(entity->type)) {
 
@@ -1714,16 +1712,15 @@ void add_dependency_to_set(Checker *c, Entity *entity) {
 			if (fl != nullptr) {
 				GB_ASSERT_MSG(fl->kind == Entity_LibraryName &&
 				              (fl->flags&EntityFlag_Used),
-				              "%.*s", LIT(name));
+				              "%.*s", LIT(entity->token.string));
 				add_dependency_to_set(c, fl);
 			}
-		}
-		if (e->kind == Entity_Variable && e->Variable.is_foreign) {
+		} else if (e->kind == Entity_Variable && e->Variable.is_foreign) {
 			Entity *fl = e->Variable.foreign_library;
 			if (fl != nullptr) {
 				GB_ASSERT_MSG(fl->kind == Entity_LibraryName &&
 				              (fl->flags&EntityFlag_Used),
-				              "%.*s", LIT(name));
+				              "%.*s", LIT(entity->token.string));
 				add_dependency_to_set(c, fl);
 			}
 		}

+ 11 - 13
src/llvm_abi.cpp

@@ -103,6 +103,11 @@ void lb_add_function_type_attributes(LLVMValueRef fn, lbFunctionType *ft, ProcCa
 		offset += 1;
 	}
 
+	LLVMContextRef c = ft->ctx;
+	LLVMAttributeRef noalias_attr   = lb_create_enum_attribute(c, "noalias", true);
+	LLVMAttributeRef nonnull_attr   = lb_create_enum_attribute(c, "nonnull", true);
+	LLVMAttributeRef nocapture_attr = lb_create_enum_attribute(c, "nocapture", true);
+
 	unsigned arg_index = offset;
 	for (unsigned i = 0; i < arg_count; i++) {
 		lbArgType *arg = &ft->args[i];
@@ -129,10 +134,9 @@ void lb_add_function_type_attributes(LLVMValueRef fn, lbFunctionType *ft, ProcCa
 	LLVMSetFunctionCallConv(fn, cc_kind);
 	if (calling_convention == ProcCC_Odin) {
 		unsigned context_index = offset+arg_count;
-		LLVMContextRef c = ft->ctx;
-		LLVMAddAttributeAtIndex(fn, context_index, lb_create_enum_attribute(c, "noalias", true));
-		LLVMAddAttributeAtIndex(fn, context_index, lb_create_enum_attribute(c, "nonnull", true));
-		LLVMAddAttributeAtIndex(fn, context_index, lb_create_enum_attribute(c, "nocapture", true));
+		LLVMAddAttributeAtIndex(fn, context_index, noalias_attr);
+		LLVMAddAttributeAtIndex(fn, context_index, nonnull_attr);
+		LLVMAddAttributeAtIndex(fn, context_index, nocapture_attr);
 	}
 
 }
@@ -201,9 +205,6 @@ i64 lb_sizeof(LLVMTypeRef type) {
 	}
 	GB_PANIC("Unhandled type for lb_sizeof -> %s", LLVMPrintTypeToString(type));
 
-	// LLVMValueRef v = LLVMSizeOf(type);
-	// GB_ASSERT(LLVMIsConstant(v));
-	// return cast(i64)LLVMConstIntGetSExtValue(v);
 	return 0;
 }
 
@@ -400,9 +401,6 @@ Type *lb_abi_to_odin_type(LLVMTypeRef type, bool is_return, u32 level = 0) {
 	}
 	GB_PANIC("Unhandled type for lb_abi_to_odin_type -> %s", LLVMPrintTypeToString(type));
 
-	// LLVMValueRef v = LLVMSizeOf(type);
-	// GB_ASSERT(LLVMIsConstant(v));
-	// return cast(i64)LLVMConstIntGetSExtValue(v);
 	return 0;
 }
 
@@ -441,7 +439,7 @@ namespace lbAbi386 {
 		LLVMAttributeRef attr = nullptr;
 		LLVMTypeRef i1 = LLVMInt1TypeInContext(c);
 		if (type == i1) {
-			// attr = lb_create_enum_attribute(c, "zext", true);
+			attr = lb_create_enum_attribute(c, "zeroext", true);
 			// return lb_arg_type_direct(type, i1, nullptr, attr);
 		}
 		return lb_arg_type_direct(type, nullptr, nullptr, attr);
@@ -625,7 +623,7 @@ namespace lbAbiAmd64SysV {
 		if (is_register(type)) {
 			LLVMAttributeRef attribute = nullptr;
 			if (type == LLVMInt1TypeInContext(c)) {
-				attribute = lb_create_enum_attribute(c, "zext", true);
+				attribute = lb_create_enum_attribute(c, "zeroext", true);
 			}
 			return lb_arg_type_direct(type, nullptr, nullptr, attribute);
 		}
@@ -648,7 +646,7 @@ namespace lbAbiAmd64SysV {
 		LLVMAttributeRef attr = nullptr;
 		LLVMTypeRef i1 = LLVMInt1TypeInContext(c);
 		if (type == i1) {
-			attr = lb_create_enum_attribute(c, "zext", true);
+			attr = lb_create_enum_attribute(c, "zeroext", true);
 		}
 		return lb_arg_type_direct(type, nullptr, nullptr, attr);
 	}

+ 16 - 11
src/llvm_backend.cpp

@@ -7440,8 +7440,14 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args,
 				}
 
 			} else if (arg->kind == lbArg_Indirect) {
-				// lbValue ptr = lb_copy_value_to_ptr(p, x, original_type, 16);
-				lbValue ptr = lb_address_from_load_or_generate_local(p, x);
+				lbValue ptr = {};
+				if (is_calling_convention_odin(pt->Proc.calling_convention)) {
+					// NOTE(bill): Odin parameters are immutable so the original value can be passed if possible
+					// i.e. `T const &` in C++
+					ptr = lb_address_from_load_or_generate_local(p, x);
+				} else {
+					ptr = lb_copy_value_to_ptr(p, x, original_type, 16);
+				}
 				array_add(&processed_args, ptr);
 			}
 
@@ -7454,18 +7460,17 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args,
 
 		Type *rt = reduce_tuple_to_single_type(results);
 		if (return_by_pointer) {
-
 			lbValue return_ptr = {};
-			// if (use_return_ptr_hint && p->return_ptr_hint_value.value != nullptr) {
-			// 	if (are_types_identical(type_deref(p->return_ptr_hint_value.type), rt)) {
-			// 		return_ptr = p->return_ptr_hint_value;
-			// 		p->return_ptr_hint_used = true;
-			// 	}
-			// }
-			// if (return_ptr.value == nullptr) {
+			if (use_return_ptr_hint && p->return_ptr_hint_value.value != nullptr) {
+				if (are_types_identical(type_deref(p->return_ptr_hint_value.type), rt)) {
+					return_ptr = p->return_ptr_hint_value;
+					p->return_ptr_hint_used = true;
+				}
+			}
+			if (return_ptr.value == nullptr) {
 				lbAddr r = lb_add_local_generated(p, rt, true);
 				return_ptr = r.addr;
-			// }
+			}
 			GB_ASSERT(is_type_pointer(return_ptr.type));
 			lb_emit_call_internal(p, value, return_ptr, processed_args, nullptr, context_ptr, inlining);
 			result = lb_emit_load(p, return_ptr);

+ 32 - 29
src/ptr_set.cpp

@@ -1,19 +1,23 @@
+typedef u32 PtrSetIndex;
+
 struct PtrSetFindResult {
-	isize hash_index;
-	isize entry_prev;
-	isize entry_index;
+	PtrSetIndex hash_index;
+	PtrSetIndex entry_prev;
+	PtrSetIndex entry_index;
 };
 
+enum : PtrSetIndex { PTR_SET_SENTINEL = ~(PtrSetIndex)0 };
+
 
 template <typename T>
 struct PtrSetEntry {
-	T     ptr;
-	isize next;
+	T           ptr;
+	PtrSetIndex next;
 };
 
 template <typename T>
 struct PtrSet {
-	Array<isize>          hashes;
+	Array<PtrSetIndex>    hashes;
 	Array<PtrSetEntry<T>> entries;
 };
 
@@ -29,10 +33,12 @@ template <typename T> void ptr_set_rehash (PtrSet<T> *s, isize new_count);
 
 template <typename T>
 void ptr_set_init(PtrSet<T> *s, gbAllocator a, isize capacity) {
+	capacity = next_pow2(gb_max(16, capacity));
+
 	array_init(&s->hashes,  a, capacity);
 	array_init(&s->entries, a, 0, capacity);
 	for (isize i = 0; i < capacity; i++) {
-		s->hashes.data[i] = -1;
+		s->hashes.data[i] = PTR_SET_SENTINEL;
 	}
 }
 
@@ -43,24 +49,24 @@ void ptr_set_destroy(PtrSet<T> *s) {
 }
 
 template <typename T>
-gb_internal isize ptr_set__add_entry(PtrSet<T> *s, T ptr) {
+gb_internal PtrSetIndex ptr_set__add_entry(PtrSet<T> *s, T ptr) {
 	PtrSetEntry<T> e = {};
 	e.ptr = ptr;
-	e.next = -1;
+	e.next = PTR_SET_SENTINEL;
 	array_add(&s->entries, e);
-	return s->entries.count-1;
+	return cast(PtrSetIndex)(s->entries.count-1);
 }
 
 
 template <typename T>
 gb_internal PtrSetFindResult ptr_set__find(PtrSet<T> *s, T ptr) {
-	PtrSetFindResult fr = {-1, -1, -1};
-	if (s->hashes.count > 0) {
+	PtrSetFindResult fr = {PTR_SET_SENTINEL, PTR_SET_SENTINEL, PTR_SET_SENTINEL};
+	if (s->hashes.count != 0) {
 		u64 hash = 0xcbf29ce484222325ull ^ cast(u64)cast(uintptr)ptr;
 		u64 n = cast(u64)s->hashes.count;
-		fr.hash_index = cast(isize)(hash % n);
+		fr.hash_index = cast(PtrSetIndex)(hash & (n-1));
 		fr.entry_index = s->hashes[fr.hash_index];
-		while (fr.entry_index >= 0) {
+		while (fr.entry_index != PTR_SET_SENTINEL) {
 			if (s->entries[fr.entry_index].ptr == ptr) {
 				return fr;
 			}
@@ -72,28 +78,25 @@ gb_internal PtrSetFindResult ptr_set__find(PtrSet<T> *s, T ptr) {
 }
 
 template <typename T>
-gb_internal b32 ptr_set__full(PtrSet<T> *s) {
+gb_internal bool ptr_set__full(PtrSet<T> *s) {
 	return 0.75f * s->hashes.count <= s->entries.count;
 }
 
-#define PTR_ARRAY_GROW_FORMULA(x) (4*(x) + 7)
-GB_STATIC_ASSERT(PTR_ARRAY_GROW_FORMULA(0) > 0);
-
 template <typename T>
 gb_inline void ptr_set_grow(PtrSet<T> *s) {
-	isize new_count = PTR_ARRAY_GROW_FORMULA(s->entries.count);
+	isize new_count = s->hashes.count*2;
 	ptr_set_rehash(s, new_count);
 }
 
 template <typename T>
 void ptr_set_rehash(PtrSet<T> *s, isize new_count) {
-	isize i, j;
+	PtrSetIndex i, j;
 	PtrSet<T> ns = {};
 	ptr_set_init(&ns, s->hashes.allocator);
 	array_resize(&ns.hashes, new_count);
 	array_reserve(&ns.entries, s->entries.count);
 	for (i = 0; i < new_count; i++) {
-		ns.hashes[i] = -1;
+		ns.hashes[i] = PTR_SET_SENTINEL;
 	}
 	for (i = 0; i < s->entries.count; i++) {
 		PtrSetEntry<T> *e = &s->entries[i];
@@ -103,7 +106,7 @@ void ptr_set_rehash(PtrSet<T> *s, isize new_count) {
 		}
 		fr = ptr_set__find(&ns, e->ptr);
 		j = ptr_set__add_entry(&ns, e->ptr);
-		if (fr.entry_prev < 0) {
+		if (fr.entry_prev == PTR_SET_SENTINEL) {
 			ns.hashes[fr.hash_index] = j;
 		} else {
 			ns.entries[fr.entry_prev].next = j;
@@ -120,23 +123,23 @@ void ptr_set_rehash(PtrSet<T> *s, isize new_count) {
 template <typename T>
 gb_inline bool ptr_set_exists(PtrSet<T> *s, T ptr) {
 	isize index = ptr_set__find(s, ptr).entry_index;
-	return index >= 0;
+	return index != PTR_SET_SENTINEL;
 }
 
 // Returns true if it already exists
 template <typename T>
 T ptr_set_add(PtrSet<T> *s, T ptr) {
-	isize index;
+	PtrSetIndex index;
 	PtrSetFindResult fr;
 	if (s->hashes.count == 0) {
 		ptr_set_grow(s);
 	}
 	fr = ptr_set__find(s, ptr);
-	if (fr.entry_index >= 0) {
+	if (fr.entry_index != PTR_SET_SENTINEL) {
 		index = fr.entry_index;
 	} else {
 		index = ptr_set__add_entry(s, ptr);
-		if (fr.entry_prev >= 0) {
+		if (fr.entry_prev != PTR_SET_SENTINEL) {
 			s->entries[fr.entry_prev].next = index;
 		} else {
 			s->hashes[fr.hash_index] = index;
@@ -152,7 +155,7 @@ T ptr_set_add(PtrSet<T> *s, T ptr) {
 template <typename T>
 void ptr_set__erase(PtrSet<T> *s, PtrSetFindResult fr) {
 	PtrSetFindResult last;
-	if (fr.entry_prev < 0) {
+	if (fr.entry_prev == PTR_SET_SENTINEL) {
 		s->hashes[fr.hash_index] = s->entries[fr.entry_index].next;
 	} else {
 		s->entries[fr.entry_prev].next = s->entries[fr.entry_index].next;
@@ -163,7 +166,7 @@ void ptr_set__erase(PtrSet<T> *s, PtrSetFindResult fr) {
 	}
 	s->entries[fr.entry_index] = s->entries[s->entries.count-1];
 	last = ptr_set__find(s, s->entries[fr.entry_index].ptr);
-	if (last.entry_prev >= 0) {
+	if (last.entry_prev != PTR_SET_SENTINEL) {
 		s->entries[last.entry_prev].next = fr.entry_index;
 	} else {
 		s->hashes[last.hash_index] = fr.entry_index;
@@ -173,7 +176,7 @@ void ptr_set__erase(PtrSet<T> *s, PtrSetFindResult fr) {
 template <typename T>
 void ptr_set_remove(PtrSet<T> *s, T ptr) {
 	PtrSetFindResult fr = ptr_set__find(s, ptr);
-	if (fr.entry_index >= 0) {
+	if (fr.entry_index != PTR_SET_SENTINEL) {
 		ptr_set__erase(s, fr);
 	}
 }

+ 9 - 0
src/types.cpp

@@ -897,6 +897,15 @@ bool is_calling_convention_none(ProcCallingConvention calling_convention) {
 	return false;
 }
 
+bool is_calling_convention_odin(ProcCallingConvention calling_convention) {
+	switch (calling_convention) {
+	case ProcCC_Odin:
+	case ProcCC_Contextless:
+		return true;
+	}
+	return false;
+}
+
 Type *alloc_type_tuple() {
 	Type *t = alloc_type(Type_Tuple);
 	return t;