From 47c7dc6a9bfc679f027984bd68523743b4d7734f Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 22 Apr 2021 10:35:17 +0100 Subject: Add new intrinsics: debug_trap, trap, read_cycle_counter, expect --- src/types.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src/types.cpp') diff --git a/src/types.cpp b/src/types.cpp index 7d85aa6bb..9871e469c 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -460,8 +460,8 @@ gb_global Type basic_types[] = { {Type_Basic, {Basic_i64, BasicFlag_Integer, 8, STR_LIT("i64")}}, {Type_Basic, {Basic_u64, BasicFlag_Integer | BasicFlag_Unsigned, 8, STR_LIT("u64")}}, - {Type_Basic, {Basic_i128, BasicFlag_Integer, 16, STR_LIT("i128")}}, - {Type_Basic, {Basic_u128, BasicFlag_Integer | BasicFlag_Unsigned, 16, STR_LIT("u128")}}, + {Type_Basic, {Basic_i128, BasicFlag_Integer, 16, STR_LIT("i128")}}, + {Type_Basic, {Basic_u128, BasicFlag_Integer | BasicFlag_Unsigned, 16, STR_LIT("u128")}}, {Type_Basic, {Basic_rune, BasicFlag_Integer | BasicFlag_Rune, 4, STR_LIT("rune")}}, @@ -1012,6 +1012,20 @@ bool is_type_integer(Type *t) { } return false; } +bool is_type_integer_like(Type *t) { + t = core_type(t); + if (t->kind == Type_Basic) { + return (t->Basic.flags & (BasicFlag_Integer|BasicFlag_Boolean)) != 0; + } + if (t->kind == Type_BitSet) { + if (t->BitSet.underlying) { + return is_type_integer_like(t->BitSet.underlying); + } + return true; + } + return false; +} + bool is_type_unsigned(Type *t) { t = base_type(t); // t = core_type(t); -- cgit v1.2.3 From 0a66f8c9a35c57714952182143984eb988f2ef0f Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 22 Apr 2021 10:39:20 +0100 Subject: Remove `intrinsics.x86_mmx` type --- core/fmt/fmt.odin | 3 --- core/intrinsics/intrinsics.odin | 4 ---- core/odin/doc-format/doc_format.odin | 5 ----- core/reflect/types.odin | 12 ++++-------- core/runtime/core.odin | 1 - core/runtime/print.odin | 12 ++++-------- src/check_expr.cpp | 3 --- src/checker.cpp | 9 --------- src/docs_format.cpp | 4 ---- src/docs_writer.cpp | 10 +++------- src/ir.cpp | 10 +++------- src/ir_print.cpp | 10 +++------- src/llvm_backend.cpp | 18 ++++-------------- src/types.cpp | 25 ++++--------------------- 14 files changed, 25 insertions(+), 101 deletions(-) (limited to 'src/types.cpp') diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index 4716f7954..6de6b0245 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -1638,9 +1638,6 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) { } case runtime.Type_Info_Simd_Vector: - if info.is_x86_mmx { - io.write_string(fi.writer, "intrinsics.x86_mmx<>"); - } io.write_byte(fi.writer, '<'); defer io.write_byte(fi.writer, '>'); for i in 0.. type/#simd[N]T soa_struct :: proc($N: int, $T: typeid) -> type/#soa[N]T diff --git a/core/odin/doc-format/doc_format.odin b/core/odin/doc-format/doc_format.odin index 61bfc913d..8d9d0a027 100644 --- a/core/odin/doc-format/doc_format.odin +++ b/core/odin/doc-format/doc_format.odin @@ -212,11 +212,6 @@ Type_Flag_Bit_Set :: enum u32le { Underlying_Type = 4, } -Type_Flags_SimdVector :: distinct bit_set[Type_Flag_SimdVector; u32le]; -Type_Flag_SimdVector :: enum u32le { - x86_mmx = 1, -} - from_array :: proc(base: ^Header_Base, a: $A/Array($T)) -> []T { s: mem.Raw_Slice; s.data = rawptr(uintptr(base) + uintptr(a.offset)); diff --git a/core/reflect/types.odin b/core/reflect/types.odin index 34a53b10a..8c7dc1735 100644 --- a/core/reflect/types.odin +++ b/core/reflect/types.odin @@ -565,14 +565,10 @@ write_type_writer :: proc(w: io.Writer, ti: ^Type_Info) -> (n: int) { n += _n(io.write_byte(w, ']')); case Type_Info_Simd_Vector: - if info.is_x86_mmx { - n += write_string(w, "intrinsics.x86_mmx"); - } else { - n += write_string(w, "#simd["); - n += _n(io.write_i64(w, i64(info.count))); - n += _n(io.write_byte(w, ']')); - n += write_type(w, info.elem); - } + n += write_string(w, "#simd["); + n += _n(io.write_i64(w, i64(info.count))); + n += _n(io.write_byte(w, ']')); + n += write_type(w, info.elem); case Type_Info_Relative_Pointer: n += write_string(w, "#relative("); diff --git a/core/runtime/core.odin b/core/runtime/core.odin index 1052b74fb..78d43b65a 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -144,7 +144,6 @@ Type_Info_Simd_Vector :: struct { elem: ^Type_Info, elem_size: int, count: int, - is_x86_mmx: bool, }; Type_Info_Relative_Pointer :: struct { pointer: ^Type_Info, diff --git a/core/runtime/print.odin b/core/runtime/print.odin index c05e6039c..e60d97103 100644 --- a/core/runtime/print.odin +++ b/core/runtime/print.odin @@ -347,14 +347,10 @@ print_type :: proc "contextless" (ti: ^Type_Info) { case Type_Info_Simd_Vector: - if info.is_x86_mmx { - print_string("intrinsics.x86_mmx"); - } else { - print_string("#simd["); - print_u64(u64(info.count)); - print_byte(']'); - print_type(info.elem); - } + print_string("#simd["); + print_u64(u64(info.count)); + print_byte(']'); + print_type(info.elem); case Type_Info_Relative_Pointer: print_string("#relative("); diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 31057598d..b9ff3bf1f 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -9324,9 +9324,6 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type if (!is_constant) { error(node, "Expected all constant elements for a simd vector"); } - if (t->SimdVector.is_x86_mmx) { - error(node, "Compound literals are not allowed with intrinsics.x86_mmx"); - } } diff --git a/src/checker.cpp b/src/checker.cpp index 10376b149..eab704d9c 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -783,15 +783,6 @@ void init_universal(void) { } } - // TODO(bill): Set the correct arch for this - if (bc->metrics.arch == TargetArch_amd64 || bc->metrics.arch == TargetArch_386) { - t_vector_x86_mmx = alloc_type(Type_SimdVector); - t_vector_x86_mmx->SimdVector.is_x86_mmx = true; - - Entity *entity = alloc_entity(Entity_TypeName, nullptr, make_token_ident(str_lit("x86_mmx")), t_vector_x86_mmx); - add_global_entity(entity, intrinsics_pkg->scope); - } - bool defined_values_double_declaration = false; for_array(i, bc->defined_values.entries) { char const *name = cast(char const *)cast(uintptr)bc->defined_values.entries[i].key.key; diff --git a/src/docs_format.cpp b/src/docs_format.cpp index c3aaebf64..e620da51f 100644 --- a/src/docs_format.cpp +++ b/src/docs_format.cpp @@ -114,10 +114,6 @@ enum OdinDocTypeFlag_BitSet : u32 { OdinDocTypeFlag_BitSet_UnderlyingType = 1<<4, }; -enum OdinDocTypeFlag_SimdVector : u32 { - OdinDocTypeFlag_BitSet_x86_mmx = 1<<1, -}; - enum { // constants OdinDocType_ElemsCap = 4, diff --git a/src/docs_writer.cpp b/src/docs_writer.cpp index 5c9bb9f63..f4fd02376 100644 --- a/src/docs_writer.cpp +++ b/src/docs_writer.cpp @@ -724,13 +724,9 @@ OdinDocTypeIndex odin_doc_type(OdinDocWriter *w, Type *type) { break; case Type_SimdVector: doc_type.kind = OdinDocType_SimdVector; - if (type->SimdVector.is_x86_mmx) { - doc_type.flags |= OdinDocTypeFlag_BitSet_x86_mmx; - } else { - doc_type.elem_count_len = 1; - doc_type.elem_counts[0] = type->SimdVector.count; - doc_type.types = odin_doc_type_as_slice(w, type->SimdVector.elem); - } + doc_type.elem_count_len = 1; + doc_type.elem_counts[0] = type->SimdVector.count; + doc_type.types = odin_doc_type_as_slice(w, type->SimdVector.elem); // TODO(bill): break; case Type_RelativePointer: diff --git a/src/ir.cpp b/src/ir.cpp index 0ad48ca27..9f08450d6 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -12564,13 +12564,9 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info case Type_SimdVector: ir_emit_comment(proc, str_lit("Type_SimdVector")); tag = ir_emit_conv(proc, variant_ptr, t_type_info_simd_vector_ptr); - if (t->SimdVector.is_x86_mmx) { - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 3), v_true); - } else { - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), ir_get_type_info_ptr(proc, t->SimdVector.elem)); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 1), ir_const_int(type_size_of(t->SimdVector.elem))); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), ir_const_int(t->SimdVector.count)); - } + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), ir_get_type_info_ptr(proc, t->SimdVector.elem)); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 1), ir_const_int(type_size_of(t->SimdVector.elem))); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), ir_const_int(t->SimdVector.count)); break; case Type_RelativePointer: diff --git a/src/ir_print.cpp b/src/ir_print.cpp index d0c014a27..54ce5dca1 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -624,13 +624,9 @@ void ir_print_type(irFileBuffer *f, irModule *m, Type *t, bool in_struct) { } case Type_SimdVector: - if (t->SimdVector.is_x86_mmx) { - ir_write_str_lit(f, "x86_mmx"); - } else { - ir_fprintf(f, "<%lld x ", t->SimdVector.count);; - ir_print_type(f, m, t->SimdVector.elem); - ir_write_byte(f, '>'); - } + ir_fprintf(f, "<%lld x ", t->SimdVector.count);; + ir_print_type(f, m, t->SimdVector.elem); + ir_write_byte(f, '>'); return; case Type_RelativePointer: diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 48c276771..a67078180 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1445,9 +1445,6 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { } case Type_SimdVector: - if (type->SimdVector.is_x86_mmx) { - return LLVMX86MMXTypeInContext(ctx); - } return LLVMVectorType(lb_type(m, type->SimdVector.elem), cast(unsigned)type->SimdVector.count); case Type_RelativePointer: @@ -1899,9 +1896,6 @@ LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) { break; case Type_SimdVector: - if (type->SimdVector.is_x86_mmx) { - return LLVMDIBuilderCreateVectorType(m->debug_builder, 2, 8*cast(unsigned)type_align_of(type), lb_debug_type(m, t_f64), nullptr, 0); - } return LLVMDIBuilderCreateVectorType(m->debug_builder, cast(unsigned)type->SimdVector.count, 8*cast(unsigned)type_align_of(type), lb_debug_type(m, type->SimdVector.elem), nullptr, 0); case Type_RelativePointer: { @@ -13432,15 +13426,11 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da { tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_simd_vector_ptr); - LLVMValueRef vals[4] = {}; + LLVMValueRef vals[3] = {}; - if (t->SimdVector.is_x86_mmx) { - vals[3] = lb_const_bool(m, t_bool, true).value; - } else { - vals[0] = lb_get_type_info_ptr(m, t->SimdVector.elem).value; - vals[1] = lb_const_int(m, t_int, type_size_of(t->SimdVector.elem)).value; - vals[2] = lb_const_int(m, t_int, t->SimdVector.count).value; - } + vals[0] = lb_get_type_info_ptr(m, t->SimdVector.elem).value; + vals[1] = lb_const_int(m, t_int, type_size_of(t->SimdVector.elem)).value; + vals[2] = lb_const_int(m, t_int, t->SimdVector.count).value; lbValue res = {}; res.type = type_deref(tag.type); diff --git a/src/types.cpp b/src/types.cpp index 9871e469c..f706032a5 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -283,7 +283,6 @@ struct TypeProc { TYPE_KIND(SimdVector, struct { \ i64 count; \ Type *elem; \ - bool is_x86_mmx; \ }) \ TYPE_KIND(RelativePointer, struct { \ Type *pointer_type; \ @@ -679,8 +678,6 @@ gb_global Type *t_source_code_location_ptr = nullptr; gb_global Type *t_map_hash = nullptr; gb_global Type *t_map_header = nullptr; -gb_global Type *t_vector_x86_mmx = nullptr; - gb_global Type *t_equal_proc = nullptr; gb_global Type *t_hasher_proc = nullptr; @@ -2162,12 +2159,8 @@ bool are_types_identical(Type *x, Type *y) { case Type_SimdVector: if (y->kind == Type_SimdVector) { - if (x->SimdVector.is_x86_mmx == y->SimdVector.is_x86_mmx) { - if (x->SimdVector.is_x86_mmx) { - return true; - } else if (x->SimdVector.count == y->SimdVector.count) { - return are_types_identical(x->SimdVector.elem, y->SimdVector.elem); - } + if (x->SimdVector.count == y->SimdVector.count) { + return are_types_identical(x->SimdVector.elem, y->SimdVector.elem); } } break; @@ -2967,9 +2960,6 @@ i64 type_align_of_internal(Type *t, TypePath *path) { } case Type_SimdVector: { - if (t->SimdVector.is_x86_mmx) { - return 8; - } // align of i64 count = t->SimdVector.count; Type *elem = t->SimdVector.elem; @@ -3233,9 +3223,6 @@ i64 type_size_of_internal(Type *t, TypePath *path) { } case Type_SimdVector: { - if (t->SimdVector.is_x86_mmx) { - return 8; - } i64 count = t->SimdVector.count; Type *elem = t->SimdVector.elem; return count * type_size_of_internal(elem, path); @@ -3670,12 +3657,8 @@ gbString write_type_to_string(gbString str, Type *type) { break; case Type_SimdVector: - if (type->SimdVector.is_x86_mmx) { - return gb_string_appendc(str, "intrinsics.x86_mmx"); - } else { - str = gb_string_append_fmt(str, "#simd[%d]", cast(int)type->SimdVector.count); - str = write_type_to_string(str, type->SimdVector.elem); - } + str = gb_string_append_fmt(str, "#simd[%d]", cast(int)type->SimdVector.count); + str = write_type_to_string(str, type->SimdVector.elem); break; case Type_RelativePointer: -- cgit v1.2.3 From b68b090f136d43c3501c4c9772bd4dd68399eb37 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 22 Apr 2021 11:33:46 +0100 Subject: Add intrinsics: overflow_add, overflow_sub, overflow_mul; Change byte swap behaviour in -llvm-api to be the same as the intrinsic --- src/check_expr.cpp | 115 ++++++++++++++++++++++++++++++++------- src/check_type.cpp | 9 ++-- src/checker.cpp | 26 +++++---- src/checker_builtin_procs.hpp | 8 +++ src/llvm_backend.cpp | 123 +++++++++++++++++++++++++----------------- src/llvm_backend.hpp | 2 +- src/types.cpp | 2 +- 7 files changed, 200 insertions(+), 85 deletions(-) (limited to 'src/types.cpp') diff --git a/src/check_expr.cpp b/src/check_expr.cpp index ae4f1ec30..a915aa92f 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -5760,14 +5760,14 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 case BuiltinProc_trap: case BuiltinProc_debug_trap: if (!build_context.use_llvm_api) { - error(ce->args[0], "%.*s is not supported on this backend", LIT(builtin_procs[id].name)); + error(ce->args[0], "'%.*s' is not supported on this backend", LIT(builtin_procs[id].name)); } operand->mode = Addressing_NoValue; break; case BuiltinProc_read_cycle_counter: if (!build_context.use_llvm_api) { - error(ce->args[0], "%.*s is not supported on this backend", LIT(builtin_procs[id].name)); + error(ce->args[0], "'%.*s' is not supported on this backend", LIT(builtin_procs[id].name)); } operand->mode = Addressing_Value; operand->type = t_i64; @@ -5776,9 +5776,8 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 case BuiltinProc_count_ones: case BuiltinProc_trailing_zeros: case BuiltinProc_reverse_bits: - case BuiltinProc_byte_swap: if (!build_context.use_llvm_api) { - error(ce->args[0], "%.*s is not supported on this backend", LIT(builtin_procs[id].name)); + error(ce->args[0], "'%.*s' is not supported on this backend", LIT(builtin_procs[id].name)); // continue anyway } { @@ -5788,24 +5787,102 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 return false; } - if (id == BuiltinProc_byte_swap && (!is_type_integer_like(x.type) && !is_type_float(x.type))) { + if (!is_type_integer_like(x.type)) { + gbString xts = type_to_string(x.type); + error(x.expr, "Values passed to '%.*s' must be an integer-like type (integer, boolean, enum, bit_set), got %s", LIT(builtin_procs[id].name), xts); + gb_string_free(xts); + } else if (x.type == t_llvm_bool) { gbString xts = type_to_string(x.type); - error(x.expr, "Values passed to %.*s must be an integer-like type (integer, boolean, enum, bit_set) or float, got %s", LIT(builtin_procs[id].name), xts); + error(x.expr, "Invalid type passed to '%.*s', got %s", LIT(builtin_procs[id].name), xts); gb_string_free(xts); - } else if (id != BuiltinProc_byte_swap && !is_type_integer_like(x.type)) { + } + + operand->mode = Addressing_Value; + operand->type = default_type(x.type); + } + break; + + case BuiltinProc_byte_swap: + if (!build_context.use_llvm_api) { + error(ce->args[0], "'%.*s' is not supported on this backend", LIT(builtin_procs[id].name)); + // continue anyway + } + { + Operand x = {}; + check_expr(c, &x, ce->args[0]); + if (x.mode == Addressing_Invalid) { + return false; + } + + if (!is_type_integer_like(x.type) && !is_type_float(x.type)) { gbString xts = type_to_string(x.type); - error(x.expr, "Values passed to %.*s must be an integer-like type (integer, boolean, enum, bit_set), got %s", LIT(builtin_procs[id].name), xts); + error(x.expr, "Values passed to '%.*s' must be an integer-like type (integer, boolean, enum, bit_set) or float, got %s", LIT(builtin_procs[id].name), xts); gb_string_free(xts); } else if (x.type == t_llvm_bool) { gbString xts = type_to_string(x.type); - error(x.expr, "Invalid type passed to %.*s, got %s", LIT(builtin_procs[id].name), xts); + error(x.expr, "Invalid type passed to '%.*s', got %s", LIT(builtin_procs[id].name), xts); gb_string_free(xts); } + i64 sz = type_size_of(x.type); + if (sz < 2) { + gbString xts = type_to_string(x.type); + error(x.expr, "Type passed to '%.*s' must be at least 2 bytes, got %s with size of %lld", LIT(builtin_procs[id].name), xts, sz); + gb_string_free(xts); + } + operand->mode = Addressing_Value; operand->type = default_type(x.type); } break; + case BuiltinProc_overflow_add: + case BuiltinProc_overflow_sub: + case BuiltinProc_overflow_mul: + if (!build_context.use_llvm_api) { + error(ce->args[0], "'%.*s' is not supported on this backend", LIT(builtin_procs[id].name)); + // continue anyway + } + { + Operand x = {}; + Operand y = {}; + check_expr(c, &x, ce->args[0]); + check_expr(c, &y, ce->args[1]); + if (x.mode == Addressing_Invalid) { + return false; + } + if (y.mode == Addressing_Invalid) { + return false; + } + convert_to_typed(c, &y, x.type); + convert_to_typed(c, &x, y.type); + if (is_type_untyped(x.type)) { + gbString xts = type_to_string(x.type); + error(x.expr, "Expected a typed integer for '%.*s', got %s", LIT(builtin_procs[id].name), xts); + gb_string_free(xts); + return false; + } + if (!is_type_integer(x.type)) { + gbString xts = type_to_string(x.type); + error(x.expr, "Expected an integer for '%.*s', got %s", LIT(builtin_procs[id].name), xts); + gb_string_free(xts); + return false; + } + Type *ct = core_type(x.type); + if (is_type_different_to_arch_endianness(ct)) { + GB_ASSERT(ct->kind == Type_Basic); + if (ct->Basic.flags & (BasicFlag_EndianLittle|BasicFlag_EndianBig)) { + gbString xts = type_to_string(x.type); + error(x.expr, "Expected an integer which does not specify the explicit endianness for '%.*s', got %s", LIT(builtin_procs[id].name), xts); + gb_string_free(xts); + return false; + } + } + + operand->mode = Addressing_Value; + operand->type = make_optional_ok_type(default_type(x.type), false); // Just reusing this procedure, it's not optional + } + break; + case BuiltinProc_atomic_fence: case BuiltinProc_atomic_fence_acq: case BuiltinProc_atomic_fence_rel: @@ -5945,7 +6022,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 case BuiltinProc_fixed_point_div_sat: { if (!build_context.use_llvm_api) { - error(ce->args[0], "%.*s is not supported on this backend", LIT(builtin_procs[id].name)); + error(ce->args[0], "'%.*s' is not supported on this backend", LIT(builtin_procs[id].name)); // continue anyway } @@ -5971,7 +6048,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 if (!are_types_identical(x.type, y.type)) { gbString xts = type_to_string(x.type); gbString yts = type_to_string(y.type); - error(x.expr, "Mismatched types for %.*s, %s vs %s", LIT(builtin_procs[id].name), xts, yts); + error(x.expr, "Mismatched types for '%.*s', %s vs %s", LIT(builtin_procs[id].name), xts, yts); gb_string_free(yts); gb_string_free(xts); return false; @@ -5979,7 +6056,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 if (!is_type_integer(x.type) || is_type_untyped(x.type)) { gbString xts = type_to_string(x.type); - error(x.expr, "Expected an integer type for %.*s, got %s", LIT(builtin_procs[id].name), xts); + error(x.expr, "Expected an integer type for '%.*s', got %s", LIT(builtin_procs[id].name), xts); gb_string_free(xts); return false; } @@ -5989,17 +6066,17 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 return false; } if (z.mode != Addressing_Constant || !is_type_integer(z.type)) { - error(z.expr, "Expected a constant integer for the scale in %.*s", LIT(builtin_procs[id].name)); + error(z.expr, "Expected a constant integer for the scale in '%.*s'", LIT(builtin_procs[id].name)); return false; } i64 n = exact_value_to_i64(z.value); if (n <= 0) { - error(z.expr, "Scale parameter in %.*s must be positive, got %lld", LIT(builtin_procs[id].name), n); + error(z.expr, "Scale parameter in '%.*s' must be positive, got %lld", LIT(builtin_procs[id].name), n); return false; } i64 sz = 8*type_size_of(x.type); if (n > sz) { - error(z.expr, "Scale parameter in %.*s is larger than the base integer bit width, got %lld, expected a maximum of %lld", LIT(builtin_procs[id].name), n, sz); + error(z.expr, "Scale parameter in '%.*s' is larger than the base integer bit width, got %lld, expected a maximum of %lld", LIT(builtin_procs[id].name), n, sz); return false; } @@ -6011,7 +6088,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 case BuiltinProc_expect: if (!build_context.use_llvm_api) { - error(ce->args[0], "%.*s is not supported on this backend", LIT(builtin_procs[id].name)); + error(ce->args[0], "'%.*s' is not supported on this backend", LIT(builtin_procs[id].name)); // continue anyway } { @@ -6030,7 +6107,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 if (!are_types_identical(x.type, y.type)) { gbString xts = type_to_string(x.type); gbString yts = type_to_string(y.type); - error(x.expr, "Mismatched types for %.*s, %s vs %s", LIT(builtin_procs[id].name), xts, yts); + error(x.expr, "Mismatched types for '%.*s', %s vs %s", LIT(builtin_procs[id].name), xts, yts); gb_string_free(yts); gb_string_free(xts); *operand = x; // minimize error propagation @@ -6039,14 +6116,14 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 if (!is_type_integer_like(x.type)) { gbString xts = type_to_string(x.type); - error(x.expr, "Values passed to %.*s must be an integer-like type (integer, boolean, enum, bit_set), got %s", LIT(builtin_procs[id].name), xts); + error(x.expr, "Values passed to '%.*s' must be an integer-like type (integer, boolean, enum, bit_set), got %s", LIT(builtin_procs[id].name), xts); gb_string_free(xts); *operand = x; return true; } if (y.mode != Addressing_Constant) { - error(y.expr, "Second argument to %.*s must be constant as it is the expected value", LIT(builtin_procs[id].name)); + error(y.expr, "Second argument to '%.*s' must be constant as it is the expected value", LIT(builtin_procs[id].name)); } if (x.mode == Addressing_Constant) { diff --git a/src/check_type.cpp b/src/check_type.cpp index 612a11593..e3aac161c 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -2619,12 +2619,11 @@ i64 check_array_count(CheckerContext *ctx, Operand *o, Ast *e) { } Type *make_optional_ok_type(Type *value, bool typed) { - // LEAK TODO(bill): probably don't reallocate everything here and reuse the same one for the same type if possible - gbAllocator a = heap_allocator(); + gbAllocator a = permanent_allocator(); Type *t = alloc_type_tuple(); - array_init(&t->Tuple.variables, a, 0, 2); - array_add (&t->Tuple.variables, alloc_entity_field(nullptr, blank_token, value, false, 0)); - array_add (&t->Tuple.variables, alloc_entity_field(nullptr, blank_token, typed ? t_bool : t_untyped_bool, false, 1)); + array_init(&t->Tuple.variables, a, 2); + t->Tuple.variables[0] = alloc_entity_field(nullptr, blank_token, value, false, 0); + t->Tuple.variables[1] = alloc_entity_field(nullptr, blank_token, typed ? t_bool : t_untyped_bool, false, 1); return t; } diff --git a/src/checker.cpp b/src/checker.cpp index eab704d9c..f386d6da7 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1773,20 +1773,28 @@ void generate_minimum_dependency_set(Checker *c, Entity *start) { str_lit("memory_equal"), str_lit("memory_compare"), str_lit("memory_compare_zero"), - - str_lit("bswap_16"), - str_lit("bswap_32"), - str_lit("bswap_64"), - str_lit("bswap_128"), - - str_lit("bswap_f16"), - str_lit("bswap_f32"), - str_lit("bswap_f64"), }; for (isize i = 0; i < gb_count_of(required_runtime_entities); i++) { force_add_dependency_entity(c, c->info.runtime_package->scope, required_runtime_entities[i]); } + if (!build_context.use_llvm_api) { + String other_required_runtime_entities[] = { + str_lit("bswap_16"), + str_lit("bswap_32"), + str_lit("bswap_64"), + str_lit("bswap_128"), + + str_lit("bswap_f16"), + str_lit("bswap_f32"), + str_lit("bswap_f64"), + }; + + for (isize i = 0; i < gb_count_of(other_required_runtime_entities); i++) { + force_add_dependency_entity(c, c->info.runtime_package->scope, other_required_runtime_entities[i]); + } + } + if (build_context.no_crt) { String required_no_crt_entities[] = { // NOTE(bill): Only if these exist diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index 7f8fd5323..b9794da8a 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -50,6 +50,10 @@ enum BuiltinProcId { BuiltinProc_reverse_bits, BuiltinProc_byte_swap, + BuiltinProc_overflow_add, + BuiltinProc_overflow_sub, + BuiltinProc_overflow_mul, + BuiltinProc_volatile_store, BuiltinProc_volatile_load, @@ -264,6 +268,10 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {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}, + {STR_LIT("overflow_mul"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("volatile_store"), 2, false, Expr_Stmt, BuiltinProcPkg_intrinsics}, {STR_LIT("volatile_load"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 2e77d66f2..a74d5bd3a 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -9127,34 +9127,57 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, { lbValue x = lb_build_expr(p, ce->args[0]); x = lb_emit_conv(p, x, tv.type); - if (is_type_float(tv.type)) { - i64 sz = type_size_of(tv.type); - Type *integer_type = nullptr; - switch (sz) { - case 2: integer_type = t_u16; break; - case 4: integer_type = t_u32; break; - case 8: integer_type = t_u64; break; + return lb_emit_byte_swap(p, x, tv.type); + } + + case BuiltinProc_overflow_add: + case BuiltinProc_overflow_sub: + case BuiltinProc_overflow_mul: + { + Type *tuple = tv.type; + GB_ASSERT(is_type_tuple(tuple)); + Type *type = tuple->Tuple.variables[0]->type; + + lbValue x = lb_build_expr(p, ce->args[0]); + lbValue y = lb_build_expr(p, ce->args[1]); + x = lb_emit_conv(p, x, type); + y = lb_emit_conv(p, y, type); + + char const *name = nullptr; + if (is_type_unsigned(type)) { + switch (id) { + case BuiltinProc_overflow_add: name = "llvm.uadd.with.overflow"; break; + case BuiltinProc_overflow_sub: name = "llvm.usub.with.overflow"; break; + case BuiltinProc_overflow_mul: name = "llvm.umul.with.overflow"; break; + } + } else { + switch (id) { + case BuiltinProc_overflow_add: name = "llvm.sadd.with.overflow"; break; + case BuiltinProc_overflow_sub: name = "llvm.ssub.with.overflow"; break; + case BuiltinProc_overflow_mul: name = "llvm.smul.with.overflow"; break; } - GB_ASSERT(integer_type != nullptr); - x = lb_emit_transmute(p, x, integer_type); } - - char const *name = "llvm.bswap"; - LLVMTypeRef types[1] = {lb_type(p->module, x.type)}; + 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] = {}; + LLVMValueRef args[2] = {}; args[0] = x.value; + args[1] = y.value; + + Type *res_type = nullptr; + { + gbAllocator a = permanent_allocator(); + res_type = alloc_type_tuple(); + array_init(&res_type->Tuple.variables, a, 2); + res_type->Tuple.variables[0] = alloc_entity_field(nullptr, blank_token, type, false, 0); + res_type->Tuple.variables[1] = alloc_entity_field(nullptr, blank_token, t_llvm_bool, false, 1); + } lbValue res = {}; res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); - res.type = x.type; - - if (is_type_float(tv.type)) { - res = lb_emit_transmute(p, res, tv.type); - } + res.type = res_type; return res; } @@ -9896,43 +9919,43 @@ LLVMValueRef lb_lookup_runtime_procedure(lbModule *m, String const &name) { return found->value; } -lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *platform_type) { - Type *vt = core_type(value.type); - GB_ASSERT(type_size_of(vt) == type_size_of(platform_type)); +lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *end_type) { + GB_ASSERT(type_size_of(value.type) == type_size_of(end_type)); - // TODO(bill): lb_emit_byte_swap - lbValue res = {}; - res.type = platform_type; - res.value = value.value; - - int sz = cast(int)type_size_of(vt); - if (sz > 1) { - if (is_type_float(platform_type)) { - String name = {}; - switch (sz) { - case 2: name = str_lit("bswap_f16"); break; - case 4: name = str_lit("bswap_f32"); break; - case 8: name = str_lit("bswap_f64"); break; - default: GB_PANIC("unhandled byteswap size"); break; - } - LLVMValueRef fn = lb_lookup_runtime_procedure(p->module, name); - res.value = LLVMBuildCall(p->builder, fn, &value.value, 1, ""); - } else { - GB_ASSERT(is_type_integer(platform_type)); - String name = {}; - switch (sz) { - case 2: name = str_lit("bswap_16"); break; - case 4: name = str_lit("bswap_32"); break; - case 8: name = str_lit("bswap_64"); break; - case 16: name = str_lit("bswap_128"); break; - default: GB_PANIC("unhandled byteswap size"); break; - } - LLVMValueRef fn = lb_lookup_runtime_procedure(p->module, name); + if (type_size_of(value.type) < 2) { + return value; + } - res.value = LLVMBuildCall(p->builder, fn, &value.value, 1, ""); + Type *original_type = value.type; + if (is_type_float(original_type)) { + i64 sz = type_size_of(original_type); + Type *integer_type = nullptr; + switch (sz) { + case 2: integer_type = t_u16; break; + case 4: integer_type = t_u32; break; + case 8: integer_type = t_u64; break; } + GB_ASSERT(integer_type != nullptr); + value = lb_emit_transmute(p, value, integer_type); } + char const *name = "llvm.bswap"; + LLVMTypeRef types[1] = {lb_type(p->module, value.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] = value.value; + + lbValue res = {}; + res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.type = value.type; + + if (is_type_float(original_type)) { + res = lb_emit_transmute(p, res, original_type); + } + res.type = end_type; return res; } diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 8117271c1..47c58fde3 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -310,7 +310,7 @@ lbValue lb_emit_deep_field_gep(lbProcedure *p, lbValue e, Selection sel); lbValue lb_emit_deep_field_ev(lbProcedure *p, lbValue e, Selection sel); lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type); -lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *platform_type); +lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *end_type); void lb_emit_defer_stmts(lbProcedure *p, lbDeferExitKind kind, lbBlock *block); lbValue lb_emit_transmute(lbProcedure *p, lbValue value, Type *t); lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right); diff --git a/src/types.cpp b/src/types.cpp index f706032a5..a5c5c2eb2 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -1479,7 +1479,7 @@ Type *integer_endian_type_to_platform_type(Type *t) { if (t->kind == Type_BitSet) { t = bit_set_to_int(t); } - GB_ASSERT(t->kind == Type_Basic); + GB_ASSERT_MSG(t->kind == Type_Basic, "%s", type_to_string(t)); switch (t->Basic.kind) { // Endian Specific Types -- cgit v1.2.3