| ... | ... | @@ -10472,6 +10472,20 @@ static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) { |
| 10472 | 10472 | return const_val->data.x_type; |
| 10473 | 10473 | } |
| 10474 | 10474 | |
| 10475 | static ZigType *ir_resolve_int_type(IrAnalyze *ira, IrInstruction *type_value) { |
| 10476 | ZigType *ty = ir_resolve_type(ira, type_value); |
| 10477 | if (type_is_invalid(ty)) |
| 10478 | return ira->codegen->builtin_types.entry_invalid; |
| 10479 | |
| 10480 | if (ty->id != ZigTypeIdInt) { |
| 10481 | ir_add_error(ira, type_value, |
| 10482 | buf_sprintf("expected integer type, found '%s'", buf_ptr(&ty->name))); |
| 10483 | return ira->codegen->builtin_types.entry_invalid; |
| 10484 | } |
| 10485 | |
| 10486 | return ty; |
| 10487 | } |
| 10488 | |
| 10475 | 10489 | static ZigType *ir_resolve_error_set_type(IrAnalyze *ira, IrInstruction *op_source, IrInstruction *type_value) { |
| 10476 | 10490 | if (type_is_invalid(type_value->value.type)) |
| 10477 | 10491 | return ira->codegen->builtin_types.entry_invalid; |
| ... | ... | @@ -17025,123 +17039,93 @@ static IrInstruction *ir_analyze_instruction_optional_unwrap_ptr(IrAnalyze *ira, |
| 17025 | 17039 | } |
| 17026 | 17040 | |
| 17027 | 17041 | static IrInstruction *ir_analyze_instruction_ctz(IrAnalyze *ira, IrInstructionCtz *instruction) { |
| 17028 | | ZigType *int_type = ir_resolve_type(ira, instruction->type->child); |
| 17042 | ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child); |
| 17029 | 17043 | if (type_is_invalid(int_type)) |
| 17030 | 17044 | return ira->codegen->invalid_instruction; |
| 17031 | 17045 | |
| 17032 | | IrInstruction *op = instruction->op->child; |
| 17033 | | |
| 17034 | | if (int_type->id != ZigTypeIdInt) { |
| 17035 | | ir_add_error(ira, instruction->type, |
| 17036 | | buf_sprintf("expected integer type, found '%s'", buf_ptr(&int_type->name))); |
| 17037 | | return ira->codegen->invalid_instruction; |
| 17038 | | } |
| 17039 | | |
| 17040 | | IrInstruction *casted_op = ir_implicit_cast(ira, op, int_type); |
| 17041 | | if (type_is_invalid(casted_op->value.type)) |
| 17046 | IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type); |
| 17047 | if (type_is_invalid(op->value.type)) |
| 17042 | 17048 | return ira->codegen->invalid_instruction; |
| 17043 | 17049 | |
| 17044 | | ZigType *return_type = get_smallest_popcount_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count); |
| 17050 | if (int_type->data.integral.bit_count == 0) |
| 17051 | return ir_const_unsigned(ira, &instruction->base, 0); |
| 17045 | 17052 | |
| 17046 | | if (int_type->data.integral.bit_count == 0) { |
| 17047 | | IrInstruction *result = ir_const(ira, &instruction->base, return_type); |
| 17048 | | bigint_init_unsigned(&result->value.data.x_bigint, 0); |
| 17049 | | return result; |
| 17050 | | } |
| 17051 | | |
| 17052 | | if (instr_is_comptime(casted_op)) { |
| 17053 | | size_t result_usize = bigint_ctz(&op->value.data.x_bigint, |
| 17054 | | op->value.type->data.integral.bit_count); |
| 17055 | | IrInstruction *result = ir_const(ira, &instruction->base, return_type); |
| 17056 | | bigint_init_unsigned(&result->value.data.x_bigint, result_usize); |
| 17057 | | return result; |
| 17053 | if (instr_is_comptime(op)) { |
| 17054 | ConstExprValue *val = ir_resolve_const(ira, op, UndefOk); |
| 17055 | if (val == nullptr) |
| 17056 | return ira->codegen->invalid_instruction; |
| 17057 | if (val->special == ConstValSpecialUndef) |
| 17058 | return ir_const_undef(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int); |
| 17059 | size_t result_usize = bigint_ctz(&op->value.data.x_bigint, int_type->data.integral.bit_count); |
| 17060 | return ir_const_unsigned(ira, &instruction->base, result_usize); |
| 17058 | 17061 | } |
| 17059 | 17062 | |
| 17063 | ZigType *return_type = get_smallest_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count); |
| 17060 | 17064 | IrInstruction *result = ir_build_ctz(&ira->new_irb, instruction->base.scope, |
| 17061 | | instruction->base.source_node, nullptr, casted_op); |
| 17065 | instruction->base.source_node, nullptr, op); |
| 17062 | 17066 | result->value.type = return_type; |
| 17063 | 17067 | return result; |
| 17064 | 17068 | } |
| 17065 | 17069 | |
| 17066 | 17070 | static IrInstruction *ir_analyze_instruction_clz(IrAnalyze *ira, IrInstructionClz *instruction) { |
| 17067 | | ZigType *int_type = ir_resolve_type(ira, instruction->type->child); |
| 17071 | ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child); |
| 17068 | 17072 | if (type_is_invalid(int_type)) |
| 17069 | 17073 | return ira->codegen->invalid_instruction; |
| 17070 | 17074 | |
| 17071 | | IrInstruction *op = instruction->op->child; |
| 17072 | | |
| 17073 | | if (int_type->id != ZigTypeIdInt) { |
| 17074 | | ir_add_error(ira, instruction->type, |
| 17075 | | buf_sprintf("expected integer type, found '%s'", buf_ptr(&int_type->name))); |
| 17076 | | return ira->codegen->invalid_instruction; |
| 17077 | | } |
| 17078 | | |
| 17079 | | IrInstruction *casted_op = ir_implicit_cast(ira, op, int_type); |
| 17080 | | if (type_is_invalid(casted_op->value.type)) |
| 17075 | IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type); |
| 17076 | if (type_is_invalid(op->value.type)) |
| 17081 | 17077 | return ira->codegen->invalid_instruction; |
| 17082 | 17078 | |
| 17083 | | ZigType *return_type = get_smallest_popcount_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count); |
| 17079 | if (int_type->data.integral.bit_count == 0) |
| 17080 | return ir_const_unsigned(ira, &instruction->base, 0); |
| 17084 | 17081 | |
| 17085 | | if (int_type->data.integral.bit_count == 0) { |
| 17086 | | IrInstruction *result = ir_const(ira, &instruction->base, return_type); |
| 17087 | | bigint_init_unsigned(&result->value.data.x_bigint, 0); |
| 17088 | | return result; |
| 17089 | | } |
| 17090 | | |
| 17091 | | if (instr_is_comptime(casted_op)) { |
| 17092 | | size_t result_usize = bigint_clz(&op->value.data.x_bigint, |
| 17093 | | op->value.type->data.integral.bit_count); |
| 17094 | | IrInstruction *result = ir_const(ira, &instruction->base, return_type); |
| 17095 | | bigint_init_unsigned(&result->value.data.x_bigint, result_usize); |
| 17096 | | return result; |
| 17082 | if (instr_is_comptime(op)) { |
| 17083 | ConstExprValue *val = ir_resolve_const(ira, op, UndefOk); |
| 17084 | if (val == nullptr) |
| 17085 | return ira->codegen->invalid_instruction; |
| 17086 | if (val->special == ConstValSpecialUndef) |
| 17087 | return ir_const_undef(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int); |
| 17088 | size_t result_usize = bigint_clz(&op->value.data.x_bigint, int_type->data.integral.bit_count); |
| 17089 | return ir_const_unsigned(ira, &instruction->base, result_usize); |
| 17097 | 17090 | } |
| 17098 | 17091 | |
| 17092 | ZigType *return_type = get_smallest_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count); |
| 17099 | 17093 | IrInstruction *result = ir_build_clz(&ira->new_irb, instruction->base.scope, |
| 17100 | | instruction->base.source_node, nullptr, casted_op); |
| 17094 | instruction->base.source_node, nullptr, op); |
| 17101 | 17095 | result->value.type = return_type; |
| 17102 | 17096 | return result; |
| 17103 | 17097 | } |
| 17104 | 17098 | |
| 17105 | 17099 | static IrInstruction *ir_analyze_instruction_pop_count(IrAnalyze *ira, IrInstructionPopCount *instruction) { |
| 17106 | | ZigType *int_type = ir_resolve_type(ira, instruction->type->child); |
| 17100 | ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child); |
| 17107 | 17101 | if (type_is_invalid(int_type)) |
| 17108 | 17102 | return ira->codegen->invalid_instruction; |
| 17109 | 17103 | |
| 17110 | | IrInstruction *op = instruction->op->child; |
| 17111 | | |
| 17112 | | if (int_type->id != ZigTypeIdInt) { |
| 17113 | | ir_add_error(ira, instruction->type, |
| 17114 | | buf_sprintf("expected integer type, found '%s'", buf_ptr(&int_type->name))); |
| 17115 | | return ira->codegen->invalid_instruction; |
| 17116 | | } |
| 17117 | | |
| 17118 | | IrInstruction *casted_op = ir_implicit_cast(ira, op, int_type); |
| 17119 | | if (type_is_invalid(casted_op->value.type)) |
| 17104 | IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type); |
| 17105 | if (type_is_invalid(op->value.type)) |
| 17120 | 17106 | return ira->codegen->invalid_instruction; |
| 17121 | 17107 | |
| 17122 | | ZigType *return_type = get_smallest_popcount_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count); |
| 17108 | if (int_type->data.integral.bit_count == 0) |
| 17109 | return ir_const_unsigned(ira, &instruction->base, 0); |
| 17123 | 17110 | |
| 17124 | | if (int_type->data.integral.bit_count == 0) { |
| 17125 | | IrInstruction *result = ir_const(ira, &instruction->base, return_type); |
| 17126 | | bigint_init_unsigned(&result->value.data.x_bigint, 0); |
| 17127 | | return result; |
| 17128 | | } |
| 17129 | | |
| 17130 | | if (instr_is_comptime(casted_op)) { |
| 17131 | | ConstExprValue *val = ir_resolve_const(ira, casted_op, UndefBad); |
| 17132 | | if (!val) |
| 17111 | if (instr_is_comptime(op)) { |
| 17112 | ConstExprValue *val = ir_resolve_const(ira, op, UndefOk); |
| 17113 | if (val == nullptr) |
| 17133 | 17114 | return ira->codegen->invalid_instruction; |
| 17115 | if (val->special == ConstValSpecialUndef) |
| 17116 | return ir_const_undef(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int); |
| 17134 | 17117 | |
| 17135 | 17118 | if (bigint_cmp_zero(&val->data.x_bigint) != CmpLT) { |
| 17136 | 17119 | size_t result = bigint_popcount_unsigned(&val->data.x_bigint); |
| 17137 | 17120 | return ir_const_unsigned(ira, &instruction->base, result); |
| 17138 | 17121 | } |
| 17139 | | size_t result = bigint_popcount_signed(&val->data.x_bigint, op->value.type->data.integral.bit_count); |
| 17122 | size_t result = bigint_popcount_signed(&val->data.x_bigint, int_type->data.integral.bit_count); |
| 17140 | 17123 | return ir_const_unsigned(ira, &instruction->base, result); |
| 17141 | 17124 | } |
| 17142 | 17125 | |
| 17126 | ZigType *return_type = get_smallest_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count); |
| 17143 | 17127 | IrInstruction *result = ir_build_pop_count(&ira->new_irb, instruction->base.scope, |
| 17144 | | instruction->base.source_node, nullptr, casted_op); |
| 17128 | instruction->base.source_node, nullptr, op); |
| 17145 | 17129 | result->value.type = return_type; |
| 17146 | 17130 | return result; |
| 17147 | 17131 | } |
| ... | ... | @@ -23002,28 +22986,13 @@ static IrInstruction *ir_analyze_instruction_sqrt(IrAnalyze *ira, IrInstructionS |
| 23002 | 22986 | } |
| 23003 | 22987 | |
| 23004 | 22988 | static IrInstruction *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstructionBswap *instruction) { |
| 23005 | | ZigType *int_type = ir_resolve_type(ira, instruction->type->child); |
| 22989 | ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child); |
| 23006 | 22990 | if (type_is_invalid(int_type)) |
| 23007 | 22991 | return ira->codegen->invalid_instruction; |
| 23008 | 22992 | |
| 23009 | | IrInstruction *op = instruction->op->child; |
| 23010 | | |
| 23011 | | if (int_type->id != ZigTypeIdInt) { |
| 23012 | | ir_add_error(ira, instruction->type, |
| 23013 | | buf_sprintf("expected integer type, found '%s'", buf_ptr(&int_type->name))); |
| 23014 | | return ira->codegen->invalid_instruction; |
| 23015 | | } |
| 23016 | | |
| 23017 | | IrInstruction *casted_op = ir_implicit_cast(ira, op, int_type); |
| 23018 | | if (type_is_invalid(casted_op->value.type)) |
| 23019 | | return ira->codegen->invalid_instruction; |
| 23020 | | |
| 23021 | | if (int_type->data.integral.bit_count % 8 != 0) { |
| 23022 | | ir_add_error(ira, instruction->op, |
| 23023 | | buf_sprintf("@byteSwap integer type '%s' has %" PRIu32 " bits which is not evenly divisible by 8", |
| 23024 | | buf_ptr(&int_type->name), int_type->data.integral.bit_count)); |
| 22993 | IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type); |
| 22994 | if (type_is_invalid(op->value.type)) |
| 23025 | 22995 | return ira->codegen->invalid_instruction; |
| 23026 | | } |
| 23027 | 22996 | |
| 23028 | 22997 | if (int_type->data.integral.bit_count == 0) { |
| 23029 | 22998 | IrInstruction *result = ir_const(ira, &instruction->base, int_type); |
| ... | ... | @@ -23031,14 +23000,22 @@ static IrInstruction *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstruction |
| 23031 | 23000 | return result; |
| 23032 | 23001 | } |
| 23033 | 23002 | |
| 23034 | | if (int_type->data.integral.bit_count == 8) { |
| 23003 | if (int_type->data.integral.bit_count == 8) |
| 23035 | 23004 | return op; |
| 23005 | |
| 23006 | if (int_type->data.integral.bit_count % 8 != 0) { |
| 23007 | ir_add_error(ira, instruction->op, |
| 23008 | buf_sprintf("@byteSwap integer type '%s' has %" PRIu32 " bits which is not evenly divisible by 8", |
| 23009 | buf_ptr(&int_type->name), int_type->data.integral.bit_count)); |
| 23010 | return ira->codegen->invalid_instruction; |
| 23036 | 23011 | } |
| 23037 | 23012 | |
| 23038 | | if (instr_is_comptime(casted_op)) { |
| 23039 | | ConstExprValue *val = ir_resolve_const(ira, casted_op, UndefBad); |
| 23040 | | if (!val) |
| 23013 | if (instr_is_comptime(op)) { |
| 23014 | ConstExprValue *val = ir_resolve_const(ira, op, UndefOk); |
| 23015 | if (val == nullptr) |
| 23041 | 23016 | return ira->codegen->invalid_instruction; |
| 23017 | if (val->special == ConstValSpecialUndef) |
| 23018 | return ir_const_undef(ira, &instruction->base, int_type); |
| 23042 | 23019 | |
| 23043 | 23020 | IrInstruction *result = ir_const(ira, &instruction->base, int_type); |
| 23044 | 23021 | size_t buf_size = int_type->data.integral.bit_count / 8; |
| ... | ... | @@ -23050,26 +23027,18 @@ static IrInstruction *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstruction |
| 23050 | 23027 | } |
| 23051 | 23028 | |
| 23052 | 23029 | IrInstruction *result = ir_build_bswap(&ira->new_irb, instruction->base.scope, |
| 23053 | | instruction->base.source_node, nullptr, casted_op); |
| 23030 | instruction->base.source_node, nullptr, op); |
| 23054 | 23031 | result->value.type = int_type; |
| 23055 | 23032 | return result; |
| 23056 | 23033 | } |
| 23057 | 23034 | |
| 23058 | 23035 | static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstructionBitReverse *instruction) { |
| 23059 | | ZigType *int_type = ir_resolve_type(ira, instruction->type->child); |
| 23036 | ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child); |
| 23060 | 23037 | if (type_is_invalid(int_type)) |
| 23061 | 23038 | return ira->codegen->invalid_instruction; |
| 23062 | 23039 | |
| 23063 | | IrInstruction *op = instruction->op->child; |
| 23064 | | |
| 23065 | | if (int_type->id != ZigTypeIdInt) { |
| 23066 | | ir_add_error(ira, instruction->type, |
| 23067 | | buf_sprintf("expected integer type, found '%s'", buf_ptr(&int_type->name))); |
| 23068 | | return ira->codegen->invalid_instruction; |
| 23069 | | } |
| 23070 | | |
| 23071 | | IrInstruction *casted_op = ir_implicit_cast(ira, op, int_type); |
| 23072 | | if (type_is_invalid(casted_op->value.type)) |
| 23040 | IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type); |
| 23041 | if (type_is_invalid(op->value.type)) |
| 23073 | 23042 | return ira->codegen->invalid_instruction; |
| 23074 | 23043 | |
| 23075 | 23044 | if (int_type->data.integral.bit_count == 0) { |
| ... | ... | @@ -23078,10 +23047,12 @@ static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstr |
| 23078 | 23047 | return result; |
| 23079 | 23048 | } |
| 23080 | 23049 | |
| 23081 | | if (instr_is_comptime(casted_op)) { |
| 23082 | | ConstExprValue *val = ir_resolve_const(ira, casted_op, UndefBad); |
| 23083 | | if (!val) |
| 23050 | if (instr_is_comptime(op)) { |
| 23051 | ConstExprValue *val = ir_resolve_const(ira, op, UndefOk); |
| 23052 | if (val == nullptr) |
| 23084 | 23053 | return ira->codegen->invalid_instruction; |
| 23054 | if (val->special == ConstValSpecialUndef) |
| 23055 | return ir_const_undef(ira, &instruction->base, int_type); |
| 23085 | 23056 | |
| 23086 | 23057 | IrInstruction *result = ir_const(ira, &instruction->base, int_type); |
| 23087 | 23058 | size_t num_bits = int_type->data.integral.bit_count; |
| ... | ... | @@ -23111,7 +23082,7 @@ static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstr |
| 23111 | 23082 | } |
| 23112 | 23083 | |
| 23113 | 23084 | IrInstruction *result = ir_build_bit_reverse(&ira->new_irb, instruction->base.scope, |
| 23114 | | instruction->base.source_node, nullptr, casted_op); |
| 23085 | instruction->base.source_node, nullptr, op); |
| 23115 | 23086 | result->value.type = int_type; |
| 23116 | 23087 | return result; |
| 23117 | 23088 | } |