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

This commit is contained in:
gingerBill
2020-11-15 21:19:08 +00:00
parent 0d6f5cec37
commit db0bcbc4f4
5 changed files with 71 additions and 59 deletions
+3 -6
View File
@@ -1679,8 +1679,6 @@ void add_dependency_to_set(Checker *c, Entity *entity) {
CheckerInfo *info = &c->info; CheckerInfo *info = &c->info;
auto *set = &info->minimum_dependency_set; auto *set = &info->minimum_dependency_set;
String name = entity->token.string;
if (entity->type != nullptr && if (entity->type != nullptr &&
is_type_polymorphic(entity->type)) { is_type_polymorphic(entity->type)) {
@@ -1714,16 +1712,15 @@ void add_dependency_to_set(Checker *c, Entity *entity) {
if (fl != nullptr) { if (fl != nullptr) {
GB_ASSERT_MSG(fl->kind == Entity_LibraryName && GB_ASSERT_MSG(fl->kind == Entity_LibraryName &&
(fl->flags&EntityFlag_Used), (fl->flags&EntityFlag_Used),
"%.*s", LIT(name)); "%.*s", LIT(entity->token.string));
add_dependency_to_set(c, fl); add_dependency_to_set(c, fl);
} }
} } else if (e->kind == Entity_Variable && e->Variable.is_foreign) {
if (e->kind == Entity_Variable && e->Variable.is_foreign) {
Entity *fl = e->Variable.foreign_library; Entity *fl = e->Variable.foreign_library;
if (fl != nullptr) { if (fl != nullptr) {
GB_ASSERT_MSG(fl->kind == Entity_LibraryName && GB_ASSERT_MSG(fl->kind == Entity_LibraryName &&
(fl->flags&EntityFlag_Used), (fl->flags&EntityFlag_Used),
"%.*s", LIT(name)); "%.*s", LIT(entity->token.string));
add_dependency_to_set(c, fl); add_dependency_to_set(c, fl);
} }
} }
+11 -13
View File
@@ -103,6 +103,11 @@ void lb_add_function_type_attributes(LLVMValueRef fn, lbFunctionType *ft, ProcCa
offset += 1; 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; unsigned arg_index = offset;
for (unsigned i = 0; i < arg_count; i++) { for (unsigned i = 0; i < arg_count; i++) {
lbArgType *arg = &ft->args[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); LLVMSetFunctionCallConv(fn, cc_kind);
if (calling_convention == ProcCC_Odin) { if (calling_convention == ProcCC_Odin) {
unsigned context_index = offset+arg_count; unsigned context_index = offset+arg_count;
LLVMContextRef c = ft->ctx; LLVMAddAttributeAtIndex(fn, context_index, noalias_attr);
LLVMAddAttributeAtIndex(fn, context_index, lb_create_enum_attribute(c, "noalias", true)); LLVMAddAttributeAtIndex(fn, context_index, nonnull_attr);
LLVMAddAttributeAtIndex(fn, context_index, lb_create_enum_attribute(c, "nonnull", true)); LLVMAddAttributeAtIndex(fn, context_index, nocapture_attr);
LLVMAddAttributeAtIndex(fn, context_index, lb_create_enum_attribute(c, "nocapture", true));
} }
} }
@@ -201,9 +205,6 @@ i64 lb_sizeof(LLVMTypeRef type) {
} }
GB_PANIC("Unhandled type for lb_sizeof -> %s", LLVMPrintTypeToString(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; 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)); 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; return 0;
} }
@@ -441,7 +439,7 @@ namespace lbAbi386 {
LLVMAttributeRef attr = nullptr; LLVMAttributeRef attr = nullptr;
LLVMTypeRef i1 = LLVMInt1TypeInContext(c); LLVMTypeRef i1 = LLVMInt1TypeInContext(c);
if (type == i1) { 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, i1, nullptr, attr);
} }
return lb_arg_type_direct(type, nullptr, nullptr, attr); return lb_arg_type_direct(type, nullptr, nullptr, attr);
@@ -625,7 +623,7 @@ namespace lbAbiAmd64SysV {
if (is_register(type)) { if (is_register(type)) {
LLVMAttributeRef attribute = nullptr; LLVMAttributeRef attribute = nullptr;
if (type == LLVMInt1TypeInContext(c)) { 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); return lb_arg_type_direct(type, nullptr, nullptr, attribute);
} }
@@ -648,7 +646,7 @@ namespace lbAbiAmd64SysV {
LLVMAttributeRef attr = nullptr; LLVMAttributeRef attr = nullptr;
LLVMTypeRef i1 = LLVMInt1TypeInContext(c); LLVMTypeRef i1 = LLVMInt1TypeInContext(c);
if (type == i1) { 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); return lb_arg_type_direct(type, nullptr, nullptr, attr);
} }
+16 -11
View File
@@ -7440,8 +7440,14 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args,
} }
} else if (arg->kind == lbArg_Indirect) { } else if (arg->kind == lbArg_Indirect) {
// lbValue ptr = lb_copy_value_to_ptr(p, x, original_type, 16); lbValue ptr = {};
lbValue ptr = lb_address_from_load_or_generate_local(p, x); 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); 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); Type *rt = reduce_tuple_to_single_type(results);
if (return_by_pointer) { if (return_by_pointer) {
lbValue return_ptr = {}; lbValue return_ptr = {};
// if (use_return_ptr_hint && p->return_ptr_hint_value.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)) { if (are_types_identical(type_deref(p->return_ptr_hint_value.type), rt)) {
// return_ptr = p->return_ptr_hint_value; return_ptr = p->return_ptr_hint_value;
// p->return_ptr_hint_used = true; p->return_ptr_hint_used = true;
// } }
// } }
// if (return_ptr.value == nullptr) { if (return_ptr.value == nullptr) {
lbAddr r = lb_add_local_generated(p, rt, true); lbAddr r = lb_add_local_generated(p, rt, true);
return_ptr = r.addr; return_ptr = r.addr;
// } }
GB_ASSERT(is_type_pointer(return_ptr.type)); GB_ASSERT(is_type_pointer(return_ptr.type));
lb_emit_call_internal(p, value, return_ptr, processed_args, nullptr, context_ptr, inlining); lb_emit_call_internal(p, value, return_ptr, processed_args, nullptr, context_ptr, inlining);
result = lb_emit_load(p, return_ptr); result = lb_emit_load(p, return_ptr);
+32 -29
View File
@@ -1,19 +1,23 @@
typedef u32 PtrSetIndex;
struct PtrSetFindResult { struct PtrSetFindResult {
isize hash_index; PtrSetIndex hash_index;
isize entry_prev; PtrSetIndex entry_prev;
isize entry_index; PtrSetIndex entry_index;
}; };
enum : PtrSetIndex { PTR_SET_SENTINEL = ~(PtrSetIndex)0 };
template <typename T> template <typename T>
struct PtrSetEntry { struct PtrSetEntry {
T ptr; T ptr;
isize next; PtrSetIndex next;
}; };
template <typename T> template <typename T>
struct PtrSet { struct PtrSet {
Array<isize> hashes; Array<PtrSetIndex> hashes;
Array<PtrSetEntry<T>> entries; Array<PtrSetEntry<T>> entries;
}; };
@@ -29,10 +33,12 @@ template <typename T> void ptr_set_rehash (PtrSet<T> *s, isize new_count);
template <typename T> template <typename T>
void ptr_set_init(PtrSet<T> *s, gbAllocator a, isize capacity) { 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->hashes, a, capacity);
array_init(&s->entries, a, 0, capacity); array_init(&s->entries, a, 0, capacity);
for (isize i = 0; i < capacity; i++) { 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> 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 = {}; PtrSetEntry<T> e = {};
e.ptr = ptr; e.ptr = ptr;
e.next = -1; e.next = PTR_SET_SENTINEL;
array_add(&s->entries, e); array_add(&s->entries, e);
return s->entries.count-1; return cast(PtrSetIndex)(s->entries.count-1);
} }
template <typename T> template <typename T>
gb_internal PtrSetFindResult ptr_set__find(PtrSet<T> *s, T ptr) { gb_internal PtrSetFindResult ptr_set__find(PtrSet<T> *s, T ptr) {
PtrSetFindResult fr = {-1, -1, -1}; PtrSetFindResult fr = {PTR_SET_SENTINEL, PTR_SET_SENTINEL, PTR_SET_SENTINEL};
if (s->hashes.count > 0) { if (s->hashes.count != 0) {
u64 hash = 0xcbf29ce484222325ull ^ cast(u64)cast(uintptr)ptr; u64 hash = 0xcbf29ce484222325ull ^ cast(u64)cast(uintptr)ptr;
u64 n = cast(u64)s->hashes.count; 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]; 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) { if (s->entries[fr.entry_index].ptr == ptr) {
return fr; return fr;
} }
@@ -72,28 +78,25 @@ gb_internal PtrSetFindResult ptr_set__find(PtrSet<T> *s, T ptr) {
} }
template <typename T> 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; 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> template <typename T>
gb_inline void ptr_set_grow(PtrSet<T> *s) { 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); ptr_set_rehash(s, new_count);
} }
template <typename T> template <typename T>
void ptr_set_rehash(PtrSet<T> *s, isize new_count) { void ptr_set_rehash(PtrSet<T> *s, isize new_count) {
isize i, j; PtrSetIndex i, j;
PtrSet<T> ns = {}; PtrSet<T> ns = {};
ptr_set_init(&ns, s->hashes.allocator); ptr_set_init(&ns, s->hashes.allocator);
array_resize(&ns.hashes, new_count); array_resize(&ns.hashes, new_count);
array_reserve(&ns.entries, s->entries.count); array_reserve(&ns.entries, s->entries.count);
for (i = 0; i < new_count; i++) { 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++) { for (i = 0; i < s->entries.count; i++) {
PtrSetEntry<T> *e = &s->entries[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); fr = ptr_set__find(&ns, e->ptr);
j = ptr_set__add_entry(&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; ns.hashes[fr.hash_index] = j;
} else { } else {
ns.entries[fr.entry_prev].next = j; ns.entries[fr.entry_prev].next = j;
@@ -120,23 +123,23 @@ void ptr_set_rehash(PtrSet<T> *s, isize new_count) {
template <typename T> template <typename T>
gb_inline bool ptr_set_exists(PtrSet<T> *s, T ptr) { gb_inline bool ptr_set_exists(PtrSet<T> *s, T ptr) {
isize index = ptr_set__find(s, ptr).entry_index; isize index = ptr_set__find(s, ptr).entry_index;
return index >= 0; return index != PTR_SET_SENTINEL;
} }
// Returns true if it already exists // Returns true if it already exists
template <typename T> template <typename T>
T ptr_set_add(PtrSet<T> *s, T ptr) { T ptr_set_add(PtrSet<T> *s, T ptr) {
isize index; PtrSetIndex index;
PtrSetFindResult fr; PtrSetFindResult fr;
if (s->hashes.count == 0) { if (s->hashes.count == 0) {
ptr_set_grow(s); ptr_set_grow(s);
} }
fr = ptr_set__find(s, ptr); fr = ptr_set__find(s, ptr);
if (fr.entry_index >= 0) { if (fr.entry_index != PTR_SET_SENTINEL) {
index = fr.entry_index; index = fr.entry_index;
} else { } else {
index = ptr_set__add_entry(s, ptr); 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; s->entries[fr.entry_prev].next = index;
} else { } else {
s->hashes[fr.hash_index] = index; s->hashes[fr.hash_index] = index;
@@ -152,7 +155,7 @@ T ptr_set_add(PtrSet<T> *s, T ptr) {
template <typename T> template <typename T>
void ptr_set__erase(PtrSet<T> *s, PtrSetFindResult fr) { void ptr_set__erase(PtrSet<T> *s, PtrSetFindResult fr) {
PtrSetFindResult last; 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; s->hashes[fr.hash_index] = s->entries[fr.entry_index].next;
} else { } else {
s->entries[fr.entry_prev].next = s->entries[fr.entry_index].next; 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]; s->entries[fr.entry_index] = s->entries[s->entries.count-1];
last = ptr_set__find(s, s->entries[fr.entry_index].ptr); 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; s->entries[last.entry_prev].next = fr.entry_index;
} else { } else {
s->hashes[last.hash_index] = fr.entry_index; s->hashes[last.hash_index] = fr.entry_index;
@@ -173,7 +176,7 @@ void ptr_set__erase(PtrSet<T> *s, PtrSetFindResult fr) {
template <typename T> template <typename T>
void ptr_set_remove(PtrSet<T> *s, T ptr) { void ptr_set_remove(PtrSet<T> *s, T ptr) {
PtrSetFindResult fr = ptr_set__find(s, 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); ptr_set__erase(s, fr);
} }
} }
+9
View File
@@ -897,6 +897,15 @@ bool is_calling_convention_none(ProcCallingConvention calling_convention) {
return false; 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 *alloc_type_tuple() {
Type *t = alloc_type(Type_Tuple); Type *t = alloc_type(Type_Tuple);
return t; return t;