From c29b643a58d3d31cae55a58a3efbbe2df5a111ad Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 24 Apr 2021 15:00:01 +0100 Subject: Move out some intrinsics into separate procedures in llvm_backend.cpp; Rename `InlineRangeStmt` to `UnrollRangeStmt` (eventually merge the two AST nodes) --- src/llvm_backend.cpp | 115 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 72 insertions(+), 43 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index d3bc3d0f8..6ed94661e 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -4295,7 +4295,7 @@ void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *scope) { lb_start_block(p, done); } -void lb_build_inline_range_stmt(lbProcedure *p, AstInlineRangeStmt *rs, Scope *scope) { +void lb_build_inline_range_stmt(lbProcedure *p, AstUnrollRangeStmt *rs, Scope *scope) { lbModule *m = p->module; lb_open_scope(p, scope); // Open scope here @@ -5288,7 +5288,7 @@ void lb_build_stmt(lbProcedure *p, Ast *node) { lb_build_range_stmt(p, rs, node->scope); case_end; - case_ast_node(rs, InlineRangeStmt, node); + case_ast_node(rs, UnrollRangeStmt, node); lb_build_inline_range_stmt(p, rs, node->scope); case_end; @@ -9103,50 +9103,13 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, } case BuiltinProc_trailing_zeros: - { - lbValue x = lb_build_expr(p, ce->args[0]); - x = lb_emit_conv(p, x, tv.type); - - char const *name = "llvm.cttz"; - LLVMTypeRef types[1] = {lb_type(p->module, tv.type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - - LLVMValueRef args[2] = {}; - args[0] = x.value; - args[1] = LLVMConstNull(LLVMInt1TypeInContext(p->module->ctx)); - - lbValue res = {}; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); - res.type = tv.type; - return res; - } + return lb_emit_trailing_zeros(p, lb_build_expr(p, ce->args[0]), tv.type); case BuiltinProc_count_ones: - case BuiltinProc_reverse_bits: - { - lbValue x = lb_build_expr(p, ce->args[0]); - x = lb_emit_conv(p, x, tv.type); + return lb_emit_count_ones(p, lb_build_expr(p, ce->args[0]), tv.type); - char const *name = nullptr; - switch (id) { - case BuiltinProc_count_ones: name = "llvm.ctpop"; break; - case BuiltinProc_reverse_bits: name = "llvm.bitreverse"; break; - } - LLVMTypeRef types[1] = {lb_type(p->module, tv.type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - - LLVMValueRef args[1] = {}; - args[0] = x.value; - - lbValue res = {}; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); - res.type = tv.type; - return res; - } + case BuiltinProc_reverse_bits: + return lb_emit_reverse_bits(p, lb_build_expr(p, ce->args[0]), tv.type); case BuiltinProc_byte_swap: { @@ -9985,6 +9948,72 @@ lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *end_type) { } +lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type) { + x = lb_emit_conv(p, x, type); + + char const *name = "llvm.ctpop"; + LLVMTypeRef types[1] = {lb_type(p->module, type)}; + unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); + GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); + LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); + + LLVMValueRef args[1] = {}; + args[0] = x.value; + + lbValue res = {}; + res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.type = type; + return res; +} + + +lbValue lb_emit_trailing_zeros(lbProcedure *p, lbValue x, Type *type) { + x = lb_emit_conv(p, x, type); + + char const *name = "llvm.cttz"; + LLVMTypeRef types[1] = {lb_type(p->module, type)}; + unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); + GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); + LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); + + LLVMValueRef args[2] = {}; + args[0] = x.value; + args[1] = LLVMConstNull(LLVMInt1TypeInContext(p->module->ctx)); + + lbValue res = {}; + res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.type = type; + return res; +} + + +lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type) { + x = lb_emit_conv(p, x, type); + + char const *name = "llvm.bitreverse"; + LLVMTypeRef types[1] = {lb_type(p->module, type)}; + unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); + GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); + LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); + + LLVMValueRef args[1] = {}; + args[0] = x.value; + + lbValue res = {}; + res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.type = type; + return res; +} + + +lbValue lb_emit_bit_set_card(lbProcedure *p, lbValue x) { + GB_ASSERT(is_type_bit_set(x.type)); + Type *underlying = bit_set_to_int(x.type); + lbValue card = lb_emit_count_ones(p, x, underlying); + return lb_emit_conv(p, card, t_int); +} + + lbLoopData lb_loop_start(lbProcedure *p, isize count, Type *index_type) { lbLoopData data = {}; -- cgit v1.2.3 From 2b4010998d2b1992c0f7ffd6e5a5260886d36d4b Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 24 Apr 2021 20:55:50 +0100 Subject: Up ci.yml --- .github/workflows/ci.yml | 4 ++-- Makefile | 4 ++-- src/llvm_backend.cpp | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 219dcebfd..c76aa5360 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ jobs: steps: - uses: actions/checkout@v1 - name: Download LLVM - run: sudo apt-get install llvm + run: sudo apt-get install llvm-11 llvm-11 - name: build odin run: make release - name: Odin run @@ -20,7 +20,7 @@ jobs: - uses: actions/checkout@v1 - name: Download LLVM and setup PATH run: | - brew install llvm + brew install llvm@11 echo "/usr/local/opt/llvm/bin" >> $GITHUB_PATH TMP_PATH=$(xcrun --show-sdk-path)/user/include echo "CPATH=$TMP_PATH" >> $GITHUB_ENV diff --git a/Makefile b/Makefile index d0dafd68c..2c2c5f380 100644 --- a/Makefile +++ b/Makefile @@ -9,11 +9,11 @@ OS=$(shell uname) ifeq ($(OS), Darwin) LDFLAGS:=$(LDFLAGS) -liconv CFLAGS:=$(CFLAGS) $(shell llvm-config --cflags) -DLLVM_BACKEND_SUPPORT -DUSE_NEW_LLVM_ABI_SYSTEM - LDFLAGS:=$(LDFLAGS) $(shell llvm-config --ldflags --libs) + LDFLAGS:=$(LDFLAGS) $(shell llvm-config --ldflags --libs) -lLLVM-C endif ifeq ($(OS), Linux) CFLAGS:=$(CFLAGS) $(shell llvm-config --cflags) -DLLVM_BACKEND_SUPPORT -DUSE_NEW_LLVM_ABI_SYSTEM - LDFLAGS:=$(LDFLAGS) $(shell llvm-config --ldflags --libs) + LDFLAGS:=$(LDFLAGS) $(shell llvm-config --ldflags --libs) -lLLVM-C endif all: debug demo diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 6ed94661e..7a21ad885 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -2323,7 +2323,7 @@ void lb_debug_complete_types(lbModule *m) { for (unsigned i = 0; i < element_count; i++) { u64 offset_in_bits = i; i64 val = bt->BitSet.lower + cast(i64)i; - gb_snprintf(name, gb_count_of(name), "%lld", val); + gb_snprintf(name, gb_count_of(name), "%lld", cast(long long)val); elements[i] = LLVMDIBuilderCreateBitFieldMemberType( m->debug_builder, scope, -- cgit v1.2.3 From 5685a8d88558c3bf17d8156edf851b86652c8e42 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 25 Apr 2021 19:19:44 +0100 Subject: Fix #911 for -llvm-api backend --- src/llvm_backend.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 7a21ad885..3cccdad1b 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -3282,7 +3282,7 @@ void lb_end_procedure_body(lbProcedure *p) { // Make sure every block terminates, and if not, make it unreachable for (block = first_block; block != nullptr; block = LLVMGetNextBasicBlock(block)) { LLVMValueRef instr = LLVMGetLastInstruction(block); - if (instr == nullptr) { + if (instr == nullptr || !lb_is_instr_terminating(instr)) { LLVMPositionBuilderAtEnd(p->builder, block); LLVMBuildUnreachable(p->builder); } -- cgit v1.2.3 From ff620422fafdbda5684033b8118d6f6611eb62ac Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 25 Apr 2021 19:40:02 +0100 Subject: Fix #857 --- src/llvm_backend.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 3cccdad1b..0d27b9c6f 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -4641,9 +4641,13 @@ void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss) { lbValue tag_index = {}; lbValue union_data = {}; if (switch_kind == TypeSwitch_Union) { - lbValue tag_ptr = lb_emit_union_tag_ptr(p, parent_ptr); - tag_index = lb_emit_load(p, tag_ptr); union_data = lb_emit_conv(p, parent_ptr, t_rawptr); + if (is_type_union_maybe_pointer(type_deref(parent_ptr.type))) { + tag_index = lb_emit_conv(p, lb_emit_comp_against_nil(p, Token_NotEq, union_data), t_int); + } else { + lbValue tag_ptr = lb_emit_union_tag_ptr(p, parent_ptr); + tag_index = lb_emit_load(p, tag_ptr); + } } lbBlock *start_block = lb_create_block(p, "typeswitch.case.first"); @@ -11232,8 +11236,15 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) { Type *src_type = type_deref(v.type); Type *dst_type = type; - lbValue src_tag = lb_emit_load(p, lb_emit_union_tag_ptr(p, v)); - lbValue dst_tag = lb_const_union_tag(p->module, src_type, dst_type); + lbValue src_tag = {}; + lbValue dst_tag = {}; + if (is_type_union_maybe_pointer(src_type)) { + src_tag = lb_emit_comp_against_nil(p, Token_NotEq, v); + dst_tag = lb_const_bool(p->module, t_bool, true); + } else { + src_tag = lb_emit_load(p, lb_emit_union_tag_ptr(p, v)); + dst_tag = lb_const_union_tag(p->module, src_type, dst_type); + } lbValue ok = lb_emit_comp(p, Token_CmpEq, src_tag, dst_tag); auto args = array_make(permanent_allocator(), 6); -- cgit v1.2.3 From 6383714bffb05a34a5320a757e08dd73bf9a2b0c Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 25 Apr 2021 19:56:46 +0100 Subject: Remove old procedure ABI code --- src/check_expr.cpp | 8 - src/check_type.cpp | 618 --------------------------------------------------- src/checker.cpp | 4 - src/llvm_backend.cpp | 19 +- src/types.cpp | 4 - 5 files changed, 8 insertions(+), 645 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 885a2eaa3..61cdf7822 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -103,10 +103,6 @@ CallArgumentData check_call_arguments (CheckerContext *c, Operand *operand, Ty Type * check_init_variable (CheckerContext *c, Entity *e, Operand *operand, String context_name); -Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type, ProcCallingConvention cc); -Type *type_to_abi_compat_result_type(gbAllocator a, Type *original_type, ProcCallingConvention cc); -bool abi_compat_return_by_pointer(gbAllocator a, ProcCallingConvention cc, Type *abi_return_type); -void set_procedure_abi_types(Type *type); void check_assignment_error_suggestion(CheckerContext *c, Operand *o, Type *type); void add_map_key_type_dependencies(CheckerContext *ctx, Type *key); @@ -1088,10 +1084,6 @@ bool is_polymorphic_type_assignable(CheckerContext *c, Type *poly, Type *source, if (!ok) return false; } - if (modify_type) { - set_procedure_abi_types(source); - } - return true; #endif } diff --git a/src/check_type.cpp b/src/check_type.cpp index e3aac161c..aef1ddc7a 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -1162,9 +1162,6 @@ Type *determine_type_from_polymorphic(CheckerContext *ctx, Type *poly_type, Oper } if (is_polymorphic_type_assignable(ctx, poly_type, operand.type, false, modify_type)) { - if (show_error) { - set_procedure_abi_types(poly_type); - } return poly_type; } if (show_error) { @@ -1767,623 +1764,8 @@ Type *check_get_results(CheckerContext *ctx, Scope *scope, Ast *_results) { return tuple; } -Array systemv_distribute_struct_fields(Type *t) { - Type *bt = core_type(t); - - - isize distributed_cap = 1; - if (bt->kind == Type_Struct) { - distributed_cap = bt->Struct.fields.count; - } - auto distributed = array_make(heap_allocator(), 0, distributed_cap); - - i64 sz = type_size_of(bt); - switch (bt->kind) { - case Type_Basic: - switch (bt->Basic.kind){ - case Basic_complex64: - array_add(&distributed, t_f32); - array_add(&distributed, t_f32); - break; - case Basic_complex128: - array_add(&distributed, t_f64); - array_add(&distributed, t_f64); - break; - case Basic_quaternion128: - array_add(&distributed, t_f32); - array_add(&distributed, t_f32); - array_add(&distributed, t_f32); - array_add(&distributed, t_f32); - break; - case Basic_quaternion256: - goto DEFAULT; - case Basic_string: - array_add(&distributed, t_u8_ptr); - array_add(&distributed, t_int); - break; - case Basic_any: - GB_ASSERT(type_size_of(t_uintptr) == type_size_of(t_typeid)); - array_add(&distributed, t_rawptr); - array_add(&distributed, t_uintptr); - break; - - case Basic_u128: - case Basic_i128: - if (build_context.ODIN_OS == "windows") { - array_add(&distributed, alloc_type_simd_vector(2, t_u64)); - } else { - array_add(&distributed, bt); - } - break; - - default: - goto DEFAULT; - } - break; - - case Type_Struct: - if (bt->Struct.is_raw_union) { - goto DEFAULT; - } else { - // IMPORTANT TOOD(bill): handle #packed structs correctly - // IMPORTANT TODO(bill): handle #align structs correctly - for_array(field_index, bt->Struct.fields) { - Entity *f = bt->Struct.fields[field_index]; - auto nested = systemv_distribute_struct_fields(f->type); - array_add_elems(&distributed, nested.data, nested.count); - array_free(&nested); - } - } - break; - - case Type_Array: - for (i64 i = 0; i < bt->Array.count; i++) { - array_add(&distributed, bt->Array.elem); - } - break; - - case Type_BitSet: - array_add(&distributed, bit_set_to_int(bt)); - break; - - case Type_Tuple: - GB_PANIC("Invalid struct field type"); - break; - - case Type_Slice: - array_add(&distributed, t_rawptr); - array_add(&distributed, t_int); - break; - - case Type_Union: - case Type_DynamicArray: - case Type_Map: - // NOTE(bill, 2019-10-10): Odin specific, don't worry about C calling convention yet - goto DEFAULT; - - case Type_Pointer: - case Type_Proc: - case Type_SimdVector: // TODO(bill): Is this correct logic? - default: - DEFAULT:; - if (sz > 0) { - array_add(&distributed, bt); - } - break; - } - - return distributed; -} - -Type *struct_type_from_systemv_distribute_struct_fields(Type *abi_type) { - GB_ASSERT(is_type_tuple(abi_type)); - Type *final_type = alloc_type_struct(); - final_type->Struct.fields = abi_type->Tuple.variables; - return final_type; -} - - -Type *handle_single_distributed_type_parameter(Array const &types, bool packed, isize *offset) { - GB_ASSERT(types.count > 0); - - if (types.count == 1) { - if (offset) *offset = 1; - - i64 sz = type_size_of(types[0]); - - if (is_type_float(types[0])) { - return types[0]; - } - switch (sz) { - case 0: - GB_PANIC("Zero sized type found!"); - case 1: return t_u8; - case 2: return t_u16; - case 4: return t_u32; - case 8: return t_u64; - default: - return types[0]; - } - } else if (types.count >= 2) { - if (types[0] == t_f32 && types[1] == t_f32) { - if (offset) *offset = 2; - return alloc_type_simd_vector(2, t_f32); - } else if (type_size_of(types[0]) == 8) { - if (offset) *offset = 1; - return types[0]; - } - - i64 total_size = 0; - isize i = 0; - if (packed) { - for (; i < types.count && total_size < 8; i += 1) { - Type *t = types[i]; - i64 s = type_size_of(t); - total_size += s; - } - } else { - for (; i < types.count && total_size < 8; i += 1) { - Type *t = types[i]; - i64 s = gb_max(type_size_of(t), 0); - i64 a = gb_max(type_align_of(t), 1); - isize ts = align_formula(total_size, a); - if (ts >= 8) { - break; - } - total_size = ts + s; - } - } - if (offset) *offset = i; - switch (total_size) { - case 1: return t_u8; - case 2: return t_u16; - case 4: return t_u32; - case 8: return t_u64; - } - return t_u64; - } - - return nullptr; -} - -Type *handle_struct_system_v_amd64_abi_type(Type *t) { - if (type_size_of(t) > 16) { - return alloc_type_pointer(t); - } - Type *original_type = t; - Type *bt = core_type(t); - t = base_type(t); - i64 size = type_size_of(bt); - - switch (t->kind) { - case Type_Slice: - case Type_Struct: - break; - - case Type_Basic: - switch (bt->Basic.kind) { - case Basic_string: - case Basic_any: - case Basic_complex64: - case Basic_complex128: - case Basic_quaternion128: - break; - default: - return original_type; - } - break; - - default: - return original_type; - } - - bool is_packed = false; - if (is_type_struct(bt)) { - is_packed = bt->Struct.is_packed; - } - - if (is_type_raw_union(bt)) { - // TODO(bill): Handle raw union correctly for - return t; - } else { - auto field_types = systemv_distribute_struct_fields(bt); - defer (array_free(&field_types)); - - GB_ASSERT(field_types.count <= 16); - - Type *final_type = nullptr; - - if (field_types.count == 0) { - final_type = t; - } else if (field_types.count == 1) { - final_type = field_types[0]; - } else { - if (size <= 8) { - isize offset = 0; - final_type = handle_single_distributed_type_parameter(field_types, is_packed, &offset); - } else { - isize offset = 0; - isize next_offset = 0; - Type *two_types[2] = {}; - - two_types[0] = handle_single_distributed_type_parameter(field_types, is_packed, &offset); - auto remaining = array_slice(field_types, offset, field_types.count); - two_types[1] = handle_single_distributed_type_parameter(remaining, is_packed, &next_offset); - GB_ASSERT(offset + next_offset == field_types.count); - - auto variables = array_make(heap_allocator(), 2); - variables[0] = alloc_entity_param(nullptr, empty_token, two_types[0], false, false); - variables[1] = alloc_entity_param(nullptr, empty_token, two_types[1], false, false); - final_type = alloc_type_tuple(); - final_type->Tuple.variables = variables; - if (t->kind == Type_Struct) { - // NOTE(bill): Make this packed - final_type->Tuple.is_packed = t->Struct.is_packed; - } - } - } - - - GB_ASSERT(final_type != nullptr); - i64 ftsz = type_size_of(final_type); - i64 otsz = type_size_of(original_type); - if (ftsz != otsz) { - // TODO(bill): Handle this case which will be caused by #packed most likely - switch (otsz) { - case 1: - case 2: - case 4: - case 8: - GB_PANIC("Incorrectly handled case for handle_struct_system_v_amd64_abi_type, %s %lld vs %s %lld", type_to_string(final_type), ftsz, type_to_string(original_type), otsz); - } - } - - return final_type; - } -} -Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type, ProcCallingConvention cc) { - Type *new_type = original_type; - if (is_type_boolean(original_type)) { - Type *t = core_type(base_type(new_type)); - if (t == t_bool) { - return t_llvm_bool; - } - return new_type; - } - - if (is_type_proc(original_type)) { - // NOTE(bill): Force a cast to prevent a possible type cycle - return t_rawptr; - } - - if (is_calling_convention_none(cc)) { - return new_type; - } - - if (build_context.ODIN_ARCH == "386") { - return new_type; - } - - if (is_type_simd_vector(original_type)) { - return new_type; - } - if (build_context.ODIN_ARCH == "amd64") { - bool is_128 = is_type_integer_128bit(original_type); - if (!is_128 && is_type_bit_set(original_type) && type_size_of(original_type) == 16) { - // is_128 = true; - } - if (is_128) { - if (build_context.ODIN_OS == "windows") { - return alloc_type_simd_vector(2, t_u64); - } else { - return original_type; - } - } - } - - if (build_context.ODIN_OS == "windows") { - // NOTE(bill): Changing the passing parameter value type is to match C's ABI - // IMPORTANT TODO(bill): This only matches the ABI on MSVC at the moment - // SEE: https://msdn.microsoft.com/en-us/library/zthk2dkh.aspx - - - Type *bt = core_type(original_type); - switch (bt->kind) { - // Okay to pass by value (usually) - // Especially the only Odin types - case Type_Basic: { - i64 sz = bt->Basic.size; - // if (sz > 8 && build_context.word_size < 8) { - if (sz > 8) { - new_type = alloc_type_pointer(original_type); - } - break; - } - case Type_Pointer: - if (is_type_struct(bt->Pointer.elem)) { - // Force to a raw pointer - new_type = t_rawptr; - } - break; - case Type_Proc: - new_type = t_rawptr; - break; // NOTE(bill): Just a pointer - - // Odin specific - case Type_Slice: - case Type_Array: - case Type_DynamicArray: - case Type_Map: - case Type_Union: - // Could be in C too - case Type_Struct: - { - i64 align = type_align_of(original_type); - i64 size = type_size_of(original_type); - - switch (8*size) { - case 8: new_type = t_u8; break; - case 16: new_type = t_u16; break; - case 32: new_type = t_u32; break; - case 64: new_type = t_u64; break; - default: - new_type = alloc_type_pointer(original_type); - break; - } - - break; - } - } - } else if (build_context.ODIN_OS == "linux" || - build_context.ODIN_OS == "darwin") { - Type *bt = core_type(original_type); - switch (bt->kind) { - // Okay to pass by value (usually) - // Especially the only Odin types - case Type_Basic: { - i64 sz = bt->Basic.size; - // if (sz > 8 && build_context.word_size < 8) { - if (sz > 8) { - new_type = alloc_type_pointer(original_type); - } - - break; - } - case Type_Pointer: break; - case Type_Proc: break; // NOTE(bill): Just a pointer - - default: { - i64 size = type_size_of(original_type); - if (size > 16) { - new_type = alloc_type_pointer(original_type); - } else if (build_context.ODIN_ARCH == "amd64") { - // NOTE(bill): System V AMD64 ABI - new_type = handle_struct_system_v_amd64_abi_type(bt); - if (are_types_identical(core_type(original_type), new_type)) { - new_type = original_type; - } - return new_type; - } - - break; - } - } - } else { - // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for - // their architectures - } - - return new_type; -} - - -Type *type_to_abi_compat_result_type(gbAllocator a, Type *original_type, ProcCallingConvention cc) { - Type *new_type = original_type; - if (new_type == nullptr) { - return nullptr; - } - GB_ASSERT(is_type_tuple(original_type)); - - Type *single_type = reduce_tuple_to_single_type(original_type); - - if (cc == ProcCC_InlineAsm) { - return new_type; - } - - if (is_type_proc(single_type)) { - // NOTE(bill): Force a cast to prevent a possible type cycle - return t_rawptr; - } - - if (is_type_simd_vector(single_type)) { - return new_type; - } - - if (is_type_pointer(single_type)) { - // NOTE(bill): Force a cast to prevent a possible type cycle - return t_rawptr; - } - - if (build_context.ODIN_OS == "windows") { - if (build_context.ODIN_ARCH == "amd64") { - if (is_type_integer_128bit(single_type)) { - if (is_calling_convention_none(cc)) { - return original_type; - } else { - return alloc_type_simd_vector(2, t_u64); - } - } - } - - Type *bt = core_type(reduce_tuple_to_single_type(original_type)); - // NOTE(bill): This is just reversed engineered from LLVM IR output - switch (bt->kind) { - // Okay to pass by value - // Especially the only Odin types - case Type_Pointer: break; - case Type_Proc: break; // NOTE(bill): Just a pointer - case Type_Basic: break; - - - default: { - i64 align = type_align_of(original_type); - i64 size = type_size_of(original_type); - switch (8*size) { -#if 1 - case 8: new_type = t_u8; break; - case 16: new_type = t_u16; break; - case 32: new_type = t_u32; break; - case 64: new_type = t_u64; break; -#endif - } - - break; - } - } - } else if (build_context.ODIN_OS == "linux" || build_context.ODIN_OS == "darwin") { - if (build_context.ODIN_ARCH == "amd64") { - - } - } else { - // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for - // their architectures - } - - if (is_type_integer_128bit(single_type)) { - if (build_context.word_size == 8) { - return original_type; - } - } - - - if (new_type != original_type) { - Type *tuple = alloc_type_tuple(); - auto variables = array_make(a, 0, 1); - array_add(&variables, alloc_entity_param(original_type->Tuple.variables[0]->scope, empty_token, new_type, false, false)); - tuple->Tuple.variables = variables; - new_type = tuple; - } - - if (cc == ProcCC_None) { - for_array(i, new_type->Tuple.variables) { - Type **tp = &new_type->Tuple.variables[i]->type; - Type *t = core_type(*tp); - if (t == t_bool) { - *tp = t_llvm_bool; - } - } - } - - new_type->cached_size = -1; - new_type->cached_align = -1; - return new_type; -} - -bool abi_compat_return_by_pointer(gbAllocator a, ProcCallingConvention cc, Type *abi_return_type) { - if (abi_return_type == nullptr) { - return false; - } - if (is_calling_convention_none(cc)) { - return false; - } - - Type *single_type = reduce_tuple_to_single_type(abi_return_type); - - if (is_type_simd_vector(single_type)) { - return false; - } - - if (build_context.word_size == 8) { - if (is_type_integer_128bit(single_type)) { - return false; - } - } - - if (build_context.ODIN_OS == "windows" || build_context.ODIN_OS == "linux" ) { - i64 size = 8*type_size_of(abi_return_type); - switch (size) { - case 0: - case 8: - case 16: - case 32: - case 64: - return false; - default: - return true; - } - } else { - if (is_type_integer_128bit(single_type)) { - return build_context.word_size < 8; - } - } - - - - return false; -} - -void set_procedure_abi_types(Type *type) { - type = base_type(type); - if (type->kind != Type_Proc) { - return; - } - - if (type->Proc.abi_types_set || type->flags & TypeFlag_InProcessOfCheckingABI) { - return; - } - - gbAllocator allocator = permanent_allocator(); - - u32 flags = type->flags; - type->flags |= TypeFlag_InProcessOfCheckingABI; - - type->Proc.abi_compat_params = array_make(allocator, cast(isize)type->Proc.param_count); - for (i32 i = 0; i < type->Proc.param_count; i++) { - Entity *e = type->Proc.params->Tuple.variables[i]; - if (e->kind == Entity_Variable) { - Type *original_type = e->type; - Type *new_type = type_to_abi_compat_param_type(allocator, original_type, type->Proc.calling_convention); - type->Proc.abi_compat_params[i] = new_type; - switch (type->Proc.calling_convention) { - case ProcCC_Odin: - case ProcCC_Contextless: - if (is_type_pointer(new_type) && !is_type_pointer(e->type) && !is_type_proc(e->type)) { - e->flags |= EntityFlag_ImplicitReference; - } - break; - } - - if (build_context.ODIN_OS == "linux" || - build_context.ODIN_OS == "darwin") { - if (is_type_pointer(new_type) & !is_type_pointer(e->type) && !is_type_proc(e->type)) { - e->flags |= EntityFlag_ByVal; - } - } - } - } - - for (i32 i = 0; i < type->Proc.param_count; i++) { - Entity *e = type->Proc.params->Tuple.variables[i]; - if (e->kind == Entity_Variable) { - set_procedure_abi_types(e->type); - } - } - for (i32 i = 0; i < type->Proc.result_count; i++) { - Entity *e = type->Proc.results->Tuple.variables[i]; - if (e->kind == Entity_Variable) { - set_procedure_abi_types(e->type); - } - } - - // NOTE(bill): The types are the same - type->Proc.abi_compat_result_type = type_to_abi_compat_result_type(allocator, type->Proc.results, type->Proc.calling_convention); - type->Proc.return_by_pointer = abi_compat_return_by_pointer(allocator, type->Proc.calling_convention, type->Proc.abi_compat_result_type); - - type->Proc.abi_types_set = true; - type->flags = flags; -} // NOTE(bill): 'operands' is for generating non generic procedure type bool check_procedure_type(CheckerContext *ctx, Type *type, Ast *proc_type_node, Array *operands) { diff --git a/src/checker.cpp b/src/checker.cpp index e74c6fc10..5a2203cfb 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -732,15 +732,11 @@ void init_universal(void) { add_global_type_entity(str_lit("byte"), &basic_types[Basic_u8]); { - void set_procedure_abi_types(Type *type); - Type *equal_args[2] = {t_rawptr, t_rawptr}; t_equal_proc = alloc_type_proc_from_types(equal_args, 2, t_bool, false, ProcCC_Contextless); - set_procedure_abi_types(t_equal_proc); Type *hasher_args[2] = {t_rawptr, t_uintptr}; t_hasher_proc = alloc_type_proc_from_types(hasher_args, 2, t_uintptr, false, ProcCC_Contextless); - set_procedure_abi_types(t_hasher_proc); } // Constants diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 0d27b9c6f..3e7d858ed 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -2545,8 +2545,6 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity) { Type *pt = base_type(entity->type); GB_ASSERT(pt->kind == Type_Proc); - set_procedure_abi_types(entity->type); - p->type = entity->type; p->type_expr = decl->type_expr; p->body = pl->body; @@ -2845,6 +2843,14 @@ lbValue lb_value_param(lbProcedure *p, Entity *e, Type *abi_type, i32 index, lbP return res; } +Type *struct_type_from_systemv_distribute_struct_fields(Type *abi_type) { + GB_ASSERT(is_type_tuple(abi_type)); + Type *final_type = alloc_type_struct(); + final_type->Struct.fields = abi_type->Tuple.variables; + return final_type; +} + + lbValue lb_add_param(lbProcedure *p, Entity *e, Ast *expr, Type *abi_type, i32 index) { lbParamPasskind kind = lbParamPass_Value; lbValue v = lb_value_param(p, e, abi_type, index, &kind); @@ -3476,9 +3482,6 @@ void lb_build_nested_proc(lbProcedure *p, AstProcLit *pd, Entity *e) { name_len = gb_snprintf(name_text, name_len, "%.*s.%.*s-%d", LIT(p->name), LIT(pd_name), guid); String name = make_string(cast(u8 *)name_text, name_len-1); - set_procedure_abi_types(e->type); - - e->Procedure.link_name = name; lbProcedure *nested_proc = lb_create_procedure(p->module, e); @@ -3613,7 +3616,6 @@ void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd) { return; } - set_procedure_abi_types(e->type); e->Procedure.link_name = name; lbProcedure *nested_proc = lb_create_procedure(p->module, e); @@ -8201,8 +8203,6 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, LLVMBuildUnreachable(p->builder); }); - set_procedure_abi_types(pt); - bool is_c_vararg = pt->Proc.c_vararg; isize param_count = pt->Proc.param_count; if (is_c_vararg) { @@ -9584,7 +9584,6 @@ lbValue lb_build_call_expr(lbProcedure *p, Ast *expr) { Type *proc_type_ = base_type(value.type); GB_ASSERT(proc_type_->kind == Type_Proc); TypeProc *pt = &proc_type_->Proc; - set_procedure_abi_types(proc_type_); if (is_call_expr_field_value(ce)) { auto args = array_make(permanent_allocator(), pt->param_count); @@ -10765,8 +10764,6 @@ lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, A String name = make_string((u8 *)name_text, name_len-1); Type *type = type_of_expr(expr); - set_procedure_abi_types(type); - Token token = {}; token.pos = ast_token(expr).pos; diff --git a/src/types.cpp b/src/types.cpp index a5c5c2eb2..56081acc8 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -198,11 +198,8 @@ struct TypeProc { isize specialization_count; ProcCallingConvention calling_convention; i32 variadic_index; - Array abi_compat_params; - Type * abi_compat_result_type; // TODO(bill): Make this a flag set rather than bools bool variadic; - bool abi_types_set; bool require_results; bool c_vararg; bool is_polymorphic; @@ -317,7 +314,6 @@ enum TypeFlag : u32 { TypeFlag_Polymorphic = 1<<1, TypeFlag_PolySpecialized = 1<<2, TypeFlag_InProcessOfCheckingPolymorphic = 1<<3, - TypeFlag_InProcessOfCheckingABI = 1<<4, }; struct Type { -- cgit v1.2.3 From 72aa0e6e3891c034863476751b2aefda781de5b2 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 25 Apr 2021 20:22:26 +0100 Subject: Replace many `foreign` llvm calls with intrinsics --- core/math/bits/bits.odin | 160 +++++-------------------------------- core/runtime/core.odin | 17 ++-- core/runtime/core_builtin.odin | 17 ++-- core/runtime/internal.odin | 45 +++-------- core/runtime/internal_linux.odin | 10 +-- core/runtime/internal_windows.odin | 10 +-- core/runtime/udivmod128.odin | 32 +------- core/time/time.odin | 8 +- src/check_builtin.cpp | 1 + src/checker_builtin_procs.hpp | 2 + src/llvm_backend.cpp | 22 +++++ src/llvm_backend.hpp | 1 + 12 files changed, 78 insertions(+), 247 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/core/math/bits/bits.odin b/core/math/bits/bits.odin index 2ab1de6e7..303a94c1d 100644 --- a/core/math/bits/bits.odin +++ b/core/math/bits/bits.odin @@ -1,5 +1,6 @@ package math_bits +import "intrinsics" import "core:runtime" U8_MIN :: 0; @@ -22,32 +23,10 @@ I16_MAX :: 1 << 15 - 1; I32_MAX :: 1 << 31 - 1; I64_MAX :: 1 << 63 - 1; -@(default_calling_convention="none") -foreign { - @(link_name="llvm.ctpop.i8") count_ones8 :: proc(i: u8) -> u8 --- - @(link_name="llvm.ctpop.i16") count_ones16 :: proc(i: u16) -> u16 --- - @(link_name="llvm.ctpop.i32") count_ones32 :: proc(i: u32) -> u32 --- - @(link_name="llvm.ctpop.i64") count_ones64 :: proc(i: u64) -> u64 --- - @(link_name="llvm.cttz.i8") trailing_zeros8 :: proc(i: u8, is_zero_undef := false) -> u8 --- - @(link_name="llvm.cttz.i16") trailing_zeros16 :: proc(i: u16, is_zero_undef := false) -> u16 --- - @(link_name="llvm.cttz.i32") trailing_zeros32 :: proc(i: u32, is_zero_undef := false) -> u32 --- - @(link_name="llvm.cttz.i64") trailing_zeros64 :: proc(i: u64, is_zero_undef := false) -> u64 --- - - @(link_name="llvm.bitreverse.i8") reverse_bits8 :: proc(i: u8) -> u8 --- - @(link_name="llvm.bitreverse.i16") reverse_bits16 :: proc(i: u16) -> u16 --- - @(link_name="llvm.bitreverse.i32") reverse_bits32 :: proc(i: u32) -> u32 --- - @(link_name="llvm.bitreverse.i64") reverse_bits64 :: proc(i: u64) -> u64 --- -} - - -trailing_zeros_uint :: proc(i: uint) -> uint { - when size_of(uint) == size_of(u64) { - return uint(trailing_zeros64(u64(i))); - } else { - return uint(trailing_zeros32(u32(i))); - } -} +count_ones :: intrinsics.count_ones; +trailing_zeros :: intrinsics.trailing_zeros; +reverse_bits :: intrinsics.reverse_bits; leading_zeros_u8 :: proc(i: u8) -> int { @@ -117,10 +96,17 @@ byte_swap :: proc{ byte_swap_int, }; -count_zeros8 :: proc(i: u8) -> u8 { return 8 - count_ones8(i); } -count_zeros16 :: proc(i: u16) -> u16 { return 16 - count_ones16(i); } -count_zeros32 :: proc(i: u32) -> u32 { return 32 - count_ones32(i); } -count_zeros64 :: proc(i: u64) -> u64 { return 64 - count_ones64(i); } +count_zeros8 :: proc(i: u8) -> u8 { return 8 - count_ones(i); } +count_zeros16 :: proc(i: u16) -> u16 { return 16 - count_ones(i); } +count_zeros32 :: proc(i: u32) -> u32 { return 32 - count_ones(i); } +count_zeros64 :: proc(i: u64) -> u64 { return 64 - count_ones(i); } + +count_zeros :: proc{ + count_zeros8, + count_zeros16, + count_zeros32, + count_zeros64, +}; rotate_left8 :: proc(x: u8, k: int) -> u8 { @@ -176,120 +162,10 @@ to_le_u64 :: proc(i: u64) -> u64 { when ODIN_ENDIAN == "little" { return i; } to_le_uint :: proc(i: uint) -> uint { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } } -@(default_calling_convention="none") -foreign { - @(link_name="llvm.uadd.with.overflow.i8") overflowing_add_u8 :: proc(lhs, rhs: u8) -> (u8, bool) --- - @(link_name="llvm.sadd.with.overflow.i8") overflowing_add_i8 :: proc(lhs, rhs: i8) -> (i8, bool) --- - @(link_name="llvm.uadd.with.overflow.i16") overflowing_add_u16 :: proc(lhs, rhs: u16) -> (u16, bool) --- - @(link_name="llvm.sadd.with.overflow.i16") overflowing_add_i16 :: proc(lhs, rhs: i16) -> (i16, bool) --- - @(link_name="llvm.uadd.with.overflow.i32") overflowing_add_u32 :: proc(lhs, rhs: u32) -> (u32, bool) --- - @(link_name="llvm.sadd.with.overflow.i32") overflowing_add_i32 :: proc(lhs, rhs: i32) -> (i32, bool) --- - @(link_name="llvm.uadd.with.overflow.i64") overflowing_add_u64 :: proc(lhs, rhs: u64) -> (u64, bool) --- - @(link_name="llvm.sadd.with.overflow.i64") overflowing_add_i64 :: proc(lhs, rhs: i64) -> (i64, bool) --- -} - -overflowing_add_uint :: proc(lhs, rhs: uint) -> (uint, bool) { - when size_of(uint) == size_of(u32) { - x, ok := overflowing_add_u32(u32(lhs), u32(rhs)); - return uint(x), ok; - } else { - x, ok := overflowing_add_u64(u64(lhs), u64(rhs)); - return uint(x), ok; - } -} -overflowing_add_int :: proc(lhs, rhs: int) -> (int, bool) { - when size_of(int) == size_of(i32) { - x, ok := overflowing_add_i32(i32(lhs), i32(rhs)); - return int(x), ok; - } else { - x, ok := overflowing_add_i64(i64(lhs), i64(rhs)); - return int(x), ok; - } -} - -overflowing_add :: proc{ - overflowing_add_u8, overflowing_add_i8, - overflowing_add_u16, overflowing_add_i16, - overflowing_add_u32, overflowing_add_i32, - overflowing_add_u64, overflowing_add_i64, - overflowing_add_uint, overflowing_add_int, -}; -@(default_calling_convention="none") -foreign { - @(link_name="llvm.usub.with.overflow.i8") overflowing_sub_u8 :: proc(lhs, rhs: u8) -> (u8, bool) --- - @(link_name="llvm.ssub.with.overflow.i8") overflowing_sub_i8 :: proc(lhs, rhs: i8) -> (i8, bool) --- - @(link_name="llvm.usub.with.overflow.i16") overflowing_sub_u16 :: proc(lhs, rhs: u16) -> (u16, bool) --- - @(link_name="llvm.ssub.with.overflow.i16") overflowing_sub_i16 :: proc(lhs, rhs: i16) -> (i16, bool) --- - @(link_name="llvm.usub.with.overflow.i32") overflowing_sub_u32 :: proc(lhs, rhs: u32) -> (u32, bool) --- - @(link_name="llvm.ssub.with.overflow.i32") overflowing_sub_i32 :: proc(lhs, rhs: i32) -> (i32, bool) --- - @(link_name="llvm.usub.with.overflow.i64") overflowing_sub_u64 :: proc(lhs, rhs: u64) -> (u64, bool) --- - @(link_name="llvm.ssub.with.overflow.i64") overflowing_sub_i64 :: proc(lhs, rhs: i64) -> (i64, bool) --- -} -overflowing_sub_uint :: proc(lhs, rhs: uint) -> (uint, bool) { - when size_of(uint) == size_of(u32) { - x, ok := overflowing_sub_u32(u32(lhs), u32(rhs)); - return uint(x), ok; - } else { - x, ok := overflowing_sub_u64(u64(lhs), u64(rhs)); - return uint(x), ok; - } -} -overflowing_sub_int :: proc(lhs, rhs: int) -> (int, bool) { - when size_of(int) == size_of(i32) { - x, ok := overflowing_sub_i32(i32(lhs), i32(rhs)); - return int(x), ok; - } else { - x, ok := overflowing_sub_i64(i64(lhs), i64(rhs)); - return int(x), ok; - } -} - -overflowing_sub :: proc{ - overflowing_sub_u8, overflowing_sub_i8, - overflowing_sub_u16, overflowing_sub_i16, - overflowing_sub_u32, overflowing_sub_i32, - overflowing_sub_u64, overflowing_sub_i64, - overflowing_sub_uint, overflowing_sub_int, -}; - -@(default_calling_convention="none") -foreign { - @(link_name="llvm.umul.with.overflow.i8") overflowing_mul_u8 :: proc(lhs, rhs: u8) -> (u8, bool) --- - @(link_name="llvm.smul.with.overflow.i8") overflowing_mul_i8 :: proc(lhs, rhs: i8) -> (i8, bool) --- - @(link_name="llvm.umul.with.overflow.i16") overflowing_mul_u16 :: proc(lhs, rhs: u16) -> (u16, bool) --- - @(link_name="llvm.smul.with.overflow.i16") overflowing_mul_i16 :: proc(lhs, rhs: i16) -> (i16, bool) --- - @(link_name="llvm.umul.with.overflow.i32") overflowing_mul_u32 :: proc(lhs, rhs: u32) -> (u32, bool) --- - @(link_name="llvm.smul.with.overflow.i32") overflowing_mul_i32 :: proc(lhs, rhs: i32) -> (i32, bool) --- - @(link_name="llvm.umul.with.overflow.i64") overflowing_mul_u64 :: proc(lhs, rhs: u64) -> (u64, bool) --- - @(link_name="llvm.smul.with.overflow.i64") overflowing_mul_i64 :: proc(lhs, rhs: i64) -> (i64, bool) --- -} -overflowing_mul_uint :: proc(lhs, rhs: uint) -> (uint, bool) { - when size_of(uint) == size_of(u32) { - x, ok := overflowing_mul_u32(u32(lhs), u32(rhs)); - return uint(x), ok; - } else { - x, ok := overflowing_mul_u64(u64(lhs), u64(rhs)); - return uint(x), ok; - } -} -overflowing_mul_int :: proc(lhs, rhs: int) -> (int, bool) { - when size_of(int) == size_of(i32) { - x, ok := overflowing_mul_i32(i32(lhs), i32(rhs)); - return int(x), ok; - } else { - x, ok := overflowing_mul_i64(i64(lhs), i64(rhs)); - return int(x), ok; - } -} - -overflowing_mul :: proc{ - overflowing_mul_u8, overflowing_mul_i8, - overflowing_mul_u16, overflowing_mul_i16, - overflowing_mul_u32, overflowing_mul_i32, - overflowing_mul_u64, overflowing_mul_i64, - overflowing_mul_uint, overflowing_mul_int, -}; +overflowing_add :: intrinsics.overflow_add; +overflowing_sub :: intrinsics.overflow_sub; +overflowing_mul :: intrinsics.overflow_mul; len_u8 :: proc(x: u8) -> int { diff --git a/core/runtime/core.odin b/core/runtime/core.odin index 78d43b65a..0033aad9a 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -20,6 +20,8 @@ // package runtime +import "intrinsics" + // NOTE(bill): This must match the compiler's Calling_Convention :: enum u8 { Invalid = 0, @@ -430,17 +432,9 @@ typeid_base_without_enum :: typeid_core; -@(default_calling_convention = "none") -foreign { - @(link_name="llvm.debugtrap") - debug_trap :: proc() ---; - - @(link_name="llvm.trap") - trap :: proc() -> ! ---; - - @(link_name="llvm.readcyclecounter") - read_cycle_counter :: proc() -> u64 ---; -} +debug_trap :: intrinsics.debug_trap; +trap :: intrinsics.trap; +read_cycle_counter :: intrinsics.read_cycle_counter; @@ -488,7 +482,6 @@ __init_context :: proc "contextless" (c: ^Context) { c.logger.data = nil; } - default_assertion_failure_proc :: proc(prefix, message: string, loc: Source_Code_Location) { print_caller_location(loc); print_string(" "); diff --git a/core/runtime/core_builtin.odin b/core/runtime/core_builtin.odin index 6656c16a0..237ad0670 100644 --- a/core/runtime/core_builtin.odin +++ b/core/runtime/core_builtin.odin @@ -1,5 +1,7 @@ package runtime +import "intrinsics" + @builtin Maybe :: union(T: typeid) #maybe {T}; @@ -539,20 +541,15 @@ excl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) { @builtin card :: proc(s: $S/bit_set[$E; $U]) -> int { when size_of(S) == 1 { - foreign { @(link_name="llvm.ctpop.i8") count_ones :: proc(i: u8) -> u8 --- } - return int(count_ones(transmute(u8)s)); + return int(intrinsics.count_ones(transmute(u8)s)); } else when size_of(S) == 2 { - foreign { @(link_name="llvm.ctpop.i16") count_ones :: proc(i: u16) -> u16 --- } - return int(count_ones(transmute(u16)s)); + return int(intrinsics.count_ones(transmute(u16)s)); } else when size_of(S) == 4 { - foreign { @(link_name="llvm.ctpop.i32") count_ones :: proc(i: u32) -> u32 --- } - return int(count_ones(transmute(u32)s)); + return int(intrinsics.count_ones(transmute(u32)s)); } else when size_of(S) == 8 { - foreign { @(link_name="llvm.ctpop.i64") count_ones :: proc(i: u64) -> u64 --- } - return int(count_ones(transmute(u64)s)); + return int(intrinsics.count_ones(transmute(u64)s)); } else when size_of(S) == 16 { - foreign { @(link_name="llvm.ctpop.i128") count_ones :: proc(i: u128) -> u128 --- } - return int(count_ones(transmute(u128)s)); + return int(intrinsics.count_ones(transmute(u128)s)); } else { #panic("Unhandled card bit_set size"); } diff --git a/core/runtime/internal.odin b/core/runtime/internal.odin index e2f8c7287..0e128567a 100644 --- a/core/runtime/internal.odin +++ b/core/runtime/internal.odin @@ -415,59 +415,32 @@ foreign { @(link_name="llvm.sqrt.f64") _sqrt_f64 :: proc(x: f64) -> f64 --- } abs_f16 :: #force_inline proc "contextless" (x: f16) -> f16 { - foreign { - @(link_name="llvm.fabs.f16") _abs :: proc "none" (x: f16) -> f16 --- - } - return _abs(x); + return -x if x < 0 else x; } abs_f32 :: #force_inline proc "contextless" (x: f32) -> f32 { - foreign { - @(link_name="llvm.fabs.f32") _abs :: proc "none" (x: f32) -> f32 --- - } - return _abs(x); + return -x if x < 0 else x; } abs_f64 :: #force_inline proc "contextless" (x: f64) -> f64 { - foreign { - @(link_name="llvm.fabs.f64") _abs :: proc "none" (x: f64) -> f64 --- - } - return _abs(x); + return -x if x < 0 else x; } min_f16 :: proc(a, b: f16) -> f16 { - foreign { - @(link_name="llvm.minnum.f16") _min :: proc "none" (a, b: f16) -> f16 --- - } - return _min(a, b); + return a if a < b else b; } min_f32 :: proc(a, b: f32) -> f32 { - foreign { - @(link_name="llvm.minnum.f32") _min :: proc "none" (a, b: f32) -> f32 --- - } - return _min(a, b); + return a if a < b else b; } min_f64 :: proc(a, b: f64) -> f64 { - foreign { - @(link_name="llvm.minnum.f64") _min :: proc "none" (a, b: f64) -> f64 --- - } - return _min(a, b); + return a if a < b else b; } max_f16 :: proc(a, b: f16) -> f16 { - foreign { - @(link_name="llvm.maxnum.f16") _max :: proc "none" (a, b: f16) -> f16 --- - } - return _max(a, b); + return a if a > b else b; } max_f32 :: proc(a, b: f32) -> f32 { - foreign { - @(link_name="llvm.maxnum.f32") _max :: proc "none" (a, b: f32) -> f32 --- - } - return _max(a, b); + return a if a > b else b; } max_f64 :: proc(a, b: f64) -> f64 { - foreign { - @(link_name="llvm.maxnum.f64") _max :: proc "none" (a, b: f64) -> f64 --- - } - return _max(a, b); + return a if a > b else b; } abs_complex32 :: #force_inline proc "contextless" (x: complex32) -> f16 { diff --git a/core/runtime/internal_linux.odin b/core/runtime/internal_linux.odin index aecd7f601..1e1a25bb8 100644 --- a/core/runtime/internal_linux.odin +++ b/core/runtime/internal_linux.odin @@ -1,5 +1,7 @@ package runtime +import "intrinsics" + @(link_name="__umodti3") umodti3 :: proc "c" (a, b: u128) -> u128 { r: u128 = ---; @@ -86,12 +88,6 @@ fixdfti :: proc(a: u64) -> i128 { } -@(default_calling_convention = "none") -foreign { - @(link_name="llvm.ctlz.i128") _clz_i128 :: proc(x: i128, is_zero_undef := false) -> i128 --- -} - - @(link_name="__floattidf") floattidf :: proc(a: i128) -> f64 { DBL_MANT_DIG :: 53; @@ -102,7 +98,7 @@ floattidf :: proc(a: i128) -> f64 { N :: size_of(i128) * 8; s := a >> (N-1); a = (a ~ s) - s; - sd: = N - _clz_i128(a); // number of significant digits + sd: = N - intrinsics.leading_zeros(a); // number of significant digits e := u32(sd - 1); // exponent if sd > DBL_MANT_DIG { switch sd { diff --git a/core/runtime/internal_windows.odin b/core/runtime/internal_windows.odin index 79a4bcdcb..8f50baf7f 100644 --- a/core/runtime/internal_windows.odin +++ b/core/runtime/internal_windows.odin @@ -1,5 +1,7 @@ package runtime +import "intrinsics" + @(link_name="__umodti3") umodti3 :: proc "c" (a, b: u128) -> u128 { r: u128 = ---; @@ -86,12 +88,6 @@ fixdfti :: proc(a: u64) -> i128 { } -@(default_calling_convention = "none") -foreign { - @(link_name="llvm.ctlz.i128") _clz_i128 :: proc(x: i128, is_zero_undef := false) -> i128 --- -} - - @(link_name="__floattidf") floattidf :: proc(a: i128) -> f64 { DBL_MANT_DIG :: 53; @@ -102,7 +98,7 @@ floattidf :: proc(a: i128) -> f64 { N :: size_of(i128) * 8; s := a >> (N-1); a = (a ~ s) - s; - sd: = N - _clz_i128(a); // number of significant digits + sd: = N - intrinsics.leading_zeros((a); // number of significant digits e := u32(sd - 1); // exponent if sd > DBL_MANT_DIG { switch sd { diff --git a/core/runtime/udivmod128.odin b/core/runtime/udivmod128.odin index 3486dffc2..c0ba6b9a3 100644 --- a/core/runtime/udivmod128.odin +++ b/core/runtime/udivmod128.odin @@ -1,35 +1,11 @@ package runtime -@(default_calling_convention="none") -foreign { - @(link_name="llvm.cttz.i8") _ctz_u8 :: proc(i: u8, is_zero_undef := false) -> u8 --- - @(link_name="llvm.cttz.i16") _ctz_u16 :: proc(i: u16, is_zero_undef := false) -> u16 --- - @(link_name="llvm.cttz.i32") _ctz_u32 :: proc(i: u32, is_zero_undef := false) -> u32 --- - @(link_name="llvm.cttz.i64") _ctz_u64 :: proc(i: u64, is_zero_undef := false) -> u64 --- -} -_ctz :: proc{ - _ctz_u8, - _ctz_u16, - _ctz_u32, - _ctz_u64, -}; - -@(default_calling_convention="none") -foreign { - @(link_name="llvm.ctlz.i8") _clz_u8 :: proc(i: u8, is_zero_undef := false) -> u8 --- - @(link_name="llvm.ctlz.i16") _clz_u16 :: proc(i: u16, is_zero_undef := false) -> u16 --- - @(link_name="llvm.ctlz.i32") _clz_u32 :: proc(i: u32, is_zero_undef := false) -> u32 --- - @(link_name="llvm.ctlz.i64") _clz_u64 :: proc(i: u64, is_zero_undef := false) -> u64 --- -} -_clz :: proc{ - _clz_u8, - _clz_u16, - _clz_u32, - _clz_u64, -}; - +import "intrinsics" udivmod128 :: proc "c" (a, b: u128, rem: ^u128) -> u128 { + _ctz :: intrinsics.trailing_zeros; + _clz :: intrinsics.leading_zeros; + n := transmute([2]u64)a; d := transmute([2]u64)b; q, r: [2]u64 = ---, ---; diff --git a/core/time/time.odin b/core/time/time.odin index eb35ac0d9..00d7e529a 100644 --- a/core/time/time.odin +++ b/core/time/time.odin @@ -1,5 +1,7 @@ package time +import "intrinsics" + Duration :: distinct i64; Nanosecond :: Duration(1); @@ -137,11 +139,7 @@ clock :: proc(t: Time) -> (hour, min, sec: int) { read_cycle_counter :: proc() -> u64 { - foreign _ { - @(link_name="llvm.readcyclecounter") - llvm_readcyclecounter :: proc "none" () -> u64 --- - } - return llvm_readcyclecounter(); + return u64(intrinsics.read_cycle_counter()); } diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 94e8dcee6..95e1f78cb 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1924,6 +1924,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 case BuiltinProc_count_ones: case BuiltinProc_trailing_zeros: + case BuiltinProc_leading_zeros: case BuiltinProc_reverse_bits: { Operand x = {}; diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index b9794da8a..4079d743b 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -47,6 +47,7 @@ enum BuiltinProcId { BuiltinProc_count_ones, BuiltinProc_trailing_zeros, + BuiltinProc_leading_zeros, BuiltinProc_reverse_bits, BuiltinProc_byte_swap, @@ -265,6 +266,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("count_ones"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("trailing_zeros"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("leading_zeros"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("reverse_bits"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("byte_swap"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 3e7d858ed..cefe740f0 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -9108,6 +9108,8 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, case BuiltinProc_trailing_zeros: return lb_emit_trailing_zeros(p, lb_build_expr(p, ce->args[0]), tv.type); + case BuiltinProc_leading_zeros: + return lb_emit_leading_zeros(p, lb_build_expr(p, ce->args[0]), tv.type); case BuiltinProc_count_ones: return lb_emit_count_ones(p, lb_build_expr(p, ce->args[0]), tv.type); @@ -9989,6 +9991,26 @@ lbValue lb_emit_trailing_zeros(lbProcedure *p, lbValue x, Type *type) { return res; } +lbValue lb_emit_leading_zeros(lbProcedure *p, lbValue x, Type *type) { + x = lb_emit_conv(p, x, type); + + char const *name = "llvm.ctlz"; + LLVMTypeRef types[1] = {lb_type(p->module, type)}; + unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); + GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); + LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); + + LLVMValueRef args[2] = {}; + args[0] = x.value; + args[1] = LLVMConstNull(LLVMInt1TypeInContext(p->module->ctx)); + + lbValue res = {}; + res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.type = type; + return res; +} + + lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type) { x = lb_emit_conv(p, x, type); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index c2202131a..a7faa83b2 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -396,6 +396,7 @@ LLVMMetadataRef lb_debug_type(lbModule *m, Type *type); lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type); lbValue lb_emit_trailing_zeros(lbProcedure *p, lbValue x, Type *type); +lbValue lb_emit_leading_zeros(lbProcedure *p, lbValue x, Type *type); lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type); lbValue lb_emit_bit_set_card(lbProcedure *p, lbValue x); -- cgit v1.2.3 From 7086b49ae6755292232d9175d12bd76d93290b9c Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 25 Apr 2021 20:26:11 +0100 Subject: Rename intrinsics to count_trailing_zeros and count_leading_zeros --- core/math/bits/bits.odin | 33 ++++++--------------------------- core/runtime/internal_linux.odin | 2 +- core/runtime/internal_windows.odin | 2 +- core/runtime/udivmod128.odin | 4 ++-- src/check_builtin.cpp | 4 ++-- src/checker_builtin_procs.hpp | 14 +++++++------- src/llvm_backend.cpp | 12 ++++++------ src/llvm_backend.hpp | 4 ++-- 8 files changed, 27 insertions(+), 48 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/core/math/bits/bits.odin b/core/math/bits/bits.odin index 303a94c1d..653cddacd 100644 --- a/core/math/bits/bits.odin +++ b/core/math/bits/bits.odin @@ -25,23 +25,14 @@ I64_MAX :: 1 << 63 - 1; count_ones :: intrinsics.count_ones; -trailing_zeros :: intrinsics.trailing_zeros; +trailing_zeros :: intrinsics.count_trailing_zeros; +leading_zeros :: intrinsics.count_leading_zeros; +count_trailing_zeros :: intrinsics.count_trailing_zeros; +count_leading_zeros :: intrinsics.count_leading_zeros; reverse_bits :: intrinsics.reverse_bits; +byte_swap :: intrinsics.byte_swap; -leading_zeros_u8 :: proc(i: u8) -> int { - return 8*size_of(i) - len_u8(i); -} -leading_zeros_u16 :: proc(i: u16) -> int { - return 8*size_of(i) - len_u16(i); -} -leading_zeros_u32 :: proc(i: u32) -> int { - return 8*size_of(i) - len_u32(i); -} -leading_zeros_u64 :: proc(i: u64) -> int { - return 8*size_of(i) - len_u64(i); -} - byte_swap_u16 :: proc(x: u16) -> u16 { return runtime.bswap_16(x); @@ -83,18 +74,6 @@ byte_swap_int :: proc(i: int) -> int { } } -byte_swap :: proc{ - byte_swap_u16, - byte_swap_u32, - byte_swap_u64, - byte_swap_u128, - byte_swap_i16, - byte_swap_i32, - byte_swap_i64, - byte_swap_i128, - byte_swap_uint, - byte_swap_int, -}; count_zeros8 :: proc(i: u8) -> u8 { return 8 - count_ones(i); } count_zeros16 :: proc(i: u16) -> u16 { return 16 - count_ones(i); } @@ -324,7 +303,7 @@ div_u64 :: proc(hi, lo, y: u64) -> (quo, rem: u64) { panic("overflow error"); } - s := uint(leading_zeros_u64(y)); + s := uint(count_leading_zeros(y)); y <<= s; yn1 := y >> 32; diff --git a/core/runtime/internal_linux.odin b/core/runtime/internal_linux.odin index 1e1a25bb8..19b52a42a 100644 --- a/core/runtime/internal_linux.odin +++ b/core/runtime/internal_linux.odin @@ -98,7 +98,7 @@ floattidf :: proc(a: i128) -> f64 { N :: size_of(i128) * 8; s := a >> (N-1); a = (a ~ s) - s; - sd: = N - intrinsics.leading_zeros(a); // number of significant digits + sd: = N - intrinsics.count_leading_zeros(a); // number of significant digits e := u32(sd - 1); // exponent if sd > DBL_MANT_DIG { switch sd { diff --git a/core/runtime/internal_windows.odin b/core/runtime/internal_windows.odin index 8f50baf7f..d7f00d155 100644 --- a/core/runtime/internal_windows.odin +++ b/core/runtime/internal_windows.odin @@ -98,7 +98,7 @@ floattidf :: proc(a: i128) -> f64 { N :: size_of(i128) * 8; s := a >> (N-1); a = (a ~ s) - s; - sd: = N - intrinsics.leading_zeros((a); // number of significant digits + sd: = N - intrinsics.count_leading_zeros(a); // number of significant digits e := u32(sd - 1); // exponent if sd > DBL_MANT_DIG { switch sd { diff --git a/core/runtime/udivmod128.odin b/core/runtime/udivmod128.odin index c0ba6b9a3..e4b7380d3 100644 --- a/core/runtime/udivmod128.odin +++ b/core/runtime/udivmod128.odin @@ -3,8 +3,8 @@ package runtime import "intrinsics" udivmod128 :: proc "c" (a, b: u128, rem: ^u128) -> u128 { - _ctz :: intrinsics.trailing_zeros; - _clz :: intrinsics.leading_zeros; + _ctz :: intrinsics.count_trailing_zeros; + _clz :: intrinsics.count_leading_zeros; n := transmute([2]u64)a; d := transmute([2]u64)b; diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 95e1f78cb..d3dbb3120 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1923,8 +1923,8 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 break; case BuiltinProc_count_ones: - case BuiltinProc_trailing_zeros: - case BuiltinProc_leading_zeros: + case BuiltinProc_count_trailing_zeros: + case BuiltinProc_count_leading_zeros: case BuiltinProc_reverse_bits: { Operand x = {}; diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index 4079d743b..7282e9895 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -46,8 +46,8 @@ enum BuiltinProcId { BuiltinProc_read_cycle_counter, BuiltinProc_count_ones, - BuiltinProc_trailing_zeros, - BuiltinProc_leading_zeros, + BuiltinProc_count_trailing_zeros, + BuiltinProc_count_leading_zeros, BuiltinProc_reverse_bits, BuiltinProc_byte_swap, @@ -264,11 +264,11 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("debug_trap"), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics, /*diverging*/false}, {STR_LIT("read_cycle_counter"), 0, false, Expr_Expr, BuiltinProcPkg_intrinsics}, - {STR_LIT("count_ones"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, - {STR_LIT("trailing_zeros"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, - {STR_LIT("leading_zeros"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, - {STR_LIT("reverse_bits"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, - {STR_LIT("byte_swap"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("count_ones"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("count_trailing_zeros"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("count_leading_zeros"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("reverse_bits"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("byte_swap"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("overflow_add"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("overflow_sub"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index cefe740f0..c6f1c3693 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -9106,10 +9106,10 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, return res; } - case BuiltinProc_trailing_zeros: - return lb_emit_trailing_zeros(p, lb_build_expr(p, ce->args[0]), tv.type); - case BuiltinProc_leading_zeros: - return lb_emit_leading_zeros(p, lb_build_expr(p, ce->args[0]), tv.type); + case BuiltinProc_count_trailing_zeros: + return lb_emit_count_trailing_zeros(p, lb_build_expr(p, ce->args[0]), tv.type); + case BuiltinProc_count_leading_zeros: + return lb_emit_count_leading_zeros(p, lb_build_expr(p, ce->args[0]), tv.type); case BuiltinProc_count_ones: return lb_emit_count_ones(p, lb_build_expr(p, ce->args[0]), tv.type); @@ -9972,7 +9972,7 @@ lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type) { } -lbValue lb_emit_trailing_zeros(lbProcedure *p, lbValue x, Type *type) { +lbValue lb_emit_count_trailing_zeros(lbProcedure *p, lbValue x, Type *type) { x = lb_emit_conv(p, x, type); char const *name = "llvm.cttz"; @@ -9991,7 +9991,7 @@ lbValue lb_emit_trailing_zeros(lbProcedure *p, lbValue x, Type *type) { return res; } -lbValue lb_emit_leading_zeros(lbProcedure *p, lbValue x, Type *type) { +lbValue lb_emit_count_leading_zeros(lbProcedure *p, lbValue x, Type *type) { x = lb_emit_conv(p, x, type); char const *name = "llvm.ctlz"; diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index a7faa83b2..b25d94abb 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -395,8 +395,8 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t); LLVMMetadataRef lb_debug_type(lbModule *m, Type *type); lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type); -lbValue lb_emit_trailing_zeros(lbProcedure *p, lbValue x, Type *type); -lbValue lb_emit_leading_zeros(lbProcedure *p, lbValue x, Type *type); +lbValue lb_emit_count_trailing_zeros(lbProcedure *p, lbValue x, Type *type); +lbValue lb_emit_count_leading_zeros(lbProcedure *p, lbValue x, Type *type); lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type); lbValue lb_emit_bit_set_card(lbProcedure *p, lbValue x); -- cgit v1.2.3 From 2691c394e071f63a72a0f425e1ed2415e14515f5 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 25 Apr 2021 20:50:25 +0100 Subject: Add `intrinsics.count_zeros` --- core/math/bits/bits.odin | 73 ++++++------------------------------------- src/check_builtin.cpp | 1 + src/checker_builtin_procs.hpp | 2 ++ src/llvm_backend.cpp | 10 ++++++ src/llvm_backend.hpp | 1 + 5 files changed, 23 insertions(+), 64 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/core/math/bits/bits.odin b/core/math/bits/bits.odin index 653cddacd..dbd7cc002 100644 --- a/core/math/bits/bits.odin +++ b/core/math/bits/bits.odin @@ -24,68 +24,18 @@ I32_MAX :: 1 << 31 - 1; I64_MAX :: 1 << 63 - 1; -count_ones :: intrinsics.count_ones; -trailing_zeros :: intrinsics.count_trailing_zeros; -leading_zeros :: intrinsics.count_leading_zeros; +count_ones :: intrinsics.count_ones; +count_zeros :: intrinsics.count_zeros; +trailing_zeros :: intrinsics.count_trailing_zeros; +leading_zeros :: intrinsics.count_leading_zeros; count_trailing_zeros :: intrinsics.count_trailing_zeros; count_leading_zeros :: intrinsics.count_leading_zeros; -reverse_bits :: intrinsics.reverse_bits; -byte_swap :: intrinsics.byte_swap; +reverse_bits :: intrinsics.reverse_bits; +byte_swap :: intrinsics.byte_swap; - - -byte_swap_u16 :: proc(x: u16) -> u16 { - return runtime.bswap_16(x); -} -byte_swap_u32 :: proc(x: u32) -> u32 { - return runtime.bswap_32(x); -} -byte_swap_u64 :: proc(x: u64) -> u64 { - return runtime.bswap_64(x); -} -byte_swap_i16 :: proc(x: i16) -> i16 { - return i16(runtime.bswap_16(u16(x))); -} -byte_swap_i32 :: proc(x: i32) -> i32 { - return i32(runtime.bswap_32(u32(x))); -} -byte_swap_i64 :: proc(x: i64) -> i64 { - return i64(runtime.bswap_64(u64(x))); -} -byte_swap_u128 :: proc(x: u128) -> u128 { - return runtime.bswap_128(x); -} -byte_swap_i128 :: proc(x: i128) -> i128 { - return i128(runtime.bswap_128(u128(x))); -} - -byte_swap_uint :: proc(i: uint) -> uint { - when size_of(uint) == size_of(u32) { - return uint(byte_swap_u32(u32(i))); - } else { - return uint(byte_swap_u64(u64(i))); - } -} -byte_swap_int :: proc(i: int) -> int { - when size_of(int) == size_of(i32) { - return int(byte_swap_i32(i32(i))); - } else { - return int(byte_swap_i64(i64(i))); - } -} - - -count_zeros8 :: proc(i: u8) -> u8 { return 8 - count_ones(i); } -count_zeros16 :: proc(i: u16) -> u16 { return 16 - count_ones(i); } -count_zeros32 :: proc(i: u32) -> u32 { return 32 - count_ones(i); } -count_zeros64 :: proc(i: u64) -> u64 { return 64 - count_ones(i); } - -count_zeros :: proc{ - count_zeros8, - count_zeros16, - count_zeros32, - count_zeros64, -}; +overflowing_add :: intrinsics.overflow_add; +overflowing_sub :: intrinsics.overflow_sub; +overflowing_mul :: intrinsics.overflow_mul; rotate_left8 :: proc(x: u8, k: int) -> u8 { @@ -142,11 +92,6 @@ to_le_uint :: proc(i: uint) -> uint { when ODIN_ENDIAN == "little" { return i; } -overflowing_add :: intrinsics.overflow_add; -overflowing_sub :: intrinsics.overflow_sub; -overflowing_mul :: intrinsics.overflow_mul; - - len_u8 :: proc(x: u8) -> int { return int(len_u8_table[x]); } diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index d3dbb3120..00963487f 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1923,6 +1923,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 break; case BuiltinProc_count_ones: + case BuiltinProc_count_zeros: case BuiltinProc_count_trailing_zeros: case BuiltinProc_count_leading_zeros: case BuiltinProc_reverse_bits: diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index 7282e9895..98ef5180b 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -46,6 +46,7 @@ enum BuiltinProcId { BuiltinProc_read_cycle_counter, BuiltinProc_count_ones, + BuiltinProc_count_zeros, BuiltinProc_count_trailing_zeros, BuiltinProc_count_leading_zeros, BuiltinProc_reverse_bits, @@ -265,6 +266,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("read_cycle_counter"), 0, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("count_ones"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("count_zeros"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("count_trailing_zeros"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("count_leading_zeros"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("reverse_bits"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index c6f1c3693..c52c9269e 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -9113,6 +9113,8 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, case BuiltinProc_count_ones: return lb_emit_count_ones(p, lb_build_expr(p, ce->args[0]), tv.type); + case BuiltinProc_count_zeros: + return lb_emit_count_zeros(p, lb_build_expr(p, ce->args[0]), tv.type); case BuiltinProc_reverse_bits: return lb_emit_reverse_bits(p, lb_build_expr(p, ce->args[0]), tv.type); @@ -9971,6 +9973,14 @@ lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type) { return res; } +lbValue lb_emit_count_zeros(lbProcedure *p, lbValue x, Type *type) { + i64 sz = 8*type_size_of(type); + lbValue size = lb_const_int(p->module, type, cast(u64)sz); + lbValue count = lb_emit_count_ones(p, x, type); + return lb_emit_arith(p, Token_Sub, size, count, type); +} + + lbValue lb_emit_count_trailing_zeros(lbProcedure *p, lbValue x, Type *type) { x = lb_emit_conv(p, x, type); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index b25d94abb..e8811a91e 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -395,6 +395,7 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t); LLVMMetadataRef lb_debug_type(lbModule *m, Type *type); lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type); +lbValue lb_emit_count_zeros(lbProcedure *p, lbValue x, Type *type); lbValue lb_emit_count_trailing_zeros(lbProcedure *p, lbValue x, Type *type); lbValue lb_emit_count_leading_zeros(lbProcedure *p, lbValue x, Type *type); lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type); -- cgit v1.2.3 From aa846d0ea5f40361cc4ada2213c4659fe1b59580 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 26 Apr 2021 11:46:26 +0100 Subject: Fix `union #maybe` comparison against `nil` -llvm-api --- src/llvm_backend.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/llvm_backend.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 0d27b9c6f..a92f53774 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -10118,6 +10118,9 @@ lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x) { } else if (op_kind == Token_NotEq) { return lb_const_bool(p->module, t_llvm_bool, false); } + } else if (is_type_union_maybe_pointer(t)) { + lbValue tag = lb_emit_transmute(p, x, t_rawptr); + return lb_emit_comp_against_nil(p, op_kind, tag); } else { lbValue tag = lb_emit_union_tag_value(p, x); return lb_emit_comp(p, op_kind, tag, lb_zero(p->module, tag.type)); -- cgit v1.2.3 From 04535b291310ff53c19fdc96817f2a651b540907 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 26 Apr 2021 22:36:20 +0100 Subject: Fix constant aliasing for debug information --- src/check_decl.cpp | 2 +- src/llvm_backend.cpp | 8 ++++++-- src/parser.cpp | 4 ++++ 3 files changed, 11 insertions(+), 3 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/check_decl.cpp b/src/check_decl.cpp index 218dce2ee..f008317ad 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -359,7 +359,7 @@ void override_entity_in_scope(Entity *original_entity, Entity *new_entity) { } if (original_entity->identifier != nullptr && original_entity->identifier->kind == Ast_Ident) { - original_entity->identifier->Ident.entity = new_entity; + original_entity->identifier->Ident.entity = nullptr; } original_entity->flags |= EntityFlag_Overridden; diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 0fd778b9c..758f8e5d1 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -3544,7 +3544,9 @@ void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd) { Ast *ident = vd->names[i]; GB_ASSERT(ident->kind == Ast_Ident); Entity *e = entity_of_node(ident); - GB_ASSERT(e != nullptr); + if (e == nullptr) { + continue; + } if (e->kind != Entity_TypeName) { continue; } @@ -3573,7 +3575,9 @@ void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd) { Ast *ident = vd->names[i]; GB_ASSERT(ident->kind == Ast_Ident); Entity *e = entity_of_node(ident); - GB_ASSERT(e != nullptr); + if (e == nullptr) { + continue; + } if (e->kind != Entity_Procedure) { continue; } diff --git a/src/parser.cpp b/src/parser.cpp index aff7c6bc7..c81827aa3 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1502,6 +1502,10 @@ bool is_semicolon_optional_for_node(AstFile *f, Ast *s) { return false; } + if (build_context.insert_semicolon) { + return true; + } + switch (s->kind) { case Ast_EmptyStmt: case Ast_BlockStmt: -- cgit v1.2.3