diff options
| author | gingerBill <bill@gingerbill.org> | 2018-11-25 16:19:17 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2018-11-25 16:19:17 +0000 |
| commit | e496b95881dffa3eda352cec6aa3249c6062d40a (patch) | |
| tree | d959dec4d5e020469594bd4243ee6abad449f5f0 /src | |
| parent | 444f4f446a8272ca3a1ab763c92bc18375c4d947 (diff) | |
Subset and superset operators for `bit_set`: < <= > >=
Diffstat (limited to 'src')
| -rw-r--r-- | src/check_expr.cpp | 46 | ||||
| -rw-r--r-- | src/entity.cpp | 8 | ||||
| -rw-r--r-- | src/ir.cpp | 32 |
3 files changed, 77 insertions, 9 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp index cb54f6b7a..47b741f40 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -1558,7 +1558,11 @@ void check_comparison(CheckerContext *c, Operand *x, Operand *y, TokenKind op) { case Token_Gt: case Token_LtEq: case Token_GtEq: - defined = is_type_ordered(x->type) && is_type_ordered(y->type); + if (are_types_identical(x->type, y->type) && is_type_bit_set(x->type)) { + defined = true; + } else { + defined = is_type_ordered(x->type) && is_type_ordered(y->type); + } break; } @@ -1596,7 +1600,42 @@ void check_comparison(CheckerContext *c, Operand *x, Operand *y, TokenKind op) { if (x->mode == Addressing_Constant && y->mode == Addressing_Constant) { if (is_type_constant_type(x->type)) { - x->value = exact_value_bool(compare_exact_values(op, x->value, y->value)); + if (is_type_bit_set(x->type)) { + switch (op) { + case Token_CmpEq: + case Token_NotEq: + x->value = exact_value_bool(compare_exact_values(op, x->value, y->value)); + break; + case Token_Lt: + case Token_LtEq: + { + ExactValue lhs = x->value; + ExactValue rhs = y->value; + ExactValue res = exact_binary_operator_value(Token_And, lhs, rhs); + res = exact_value_bool(compare_exact_values(op, res, lhs)); + if (op == Token_Lt) { + res = exact_binary_operator_value(Token_And, res, exact_value_bool(compare_exact_values(op, lhs, rhs))); + } + x->value = res; + break; + } + case Token_Gt: + case Token_GtEq: + { + ExactValue lhs = x->value; + ExactValue rhs = y->value; + ExactValue res = exact_binary_operator_value(Token_And, lhs, rhs); + res = exact_value_bool(compare_exact_values(op, res, rhs)); + if (op == Token_Gt) { + res = exact_binary_operator_value(Token_And, res, exact_value_bool(compare_exact_values(op, lhs, rhs))); + } + x->value = res; + break; + } + } + } else { + x->value = exact_value_bool(compare_exact_values(op, x->value, y->value)); + } } else { x->mode = Addressing_Value; } @@ -2084,8 +2123,8 @@ void check_binary_expr(CheckerContext *c, Operand *x, Ast *node, bool use_lhs_as add_package_dependency(c, "runtime", "__dynamic_map_get"); } else if (is_type_bit_set(y->type)) { Type *yt = base_type(y->type); - check_assignment(c, x, yt->BitSet.elem, str_lit("bit_set 'in'")); + check_assignment(c, x, yt->BitSet.elem, str_lit("bit_set 'in'")); if (x->mode == Addressing_Constant && y->mode == Addressing_Constant) { ExactValue k = exact_value_to_integer(x->value); ExactValue v = exact_value_to_integer(y->value); @@ -2109,7 +2148,6 @@ void check_binary_expr(CheckerContext *c, Operand *x, Ast *node, bool use_lhs_as x->mode = Addressing_Invalid; } } - } else { gbString t = type_to_string(y->type); error(x->expr, "expected either a map or bitset for 'in', got %s", t); diff --git a/src/entity.cpp b/src/entity.cpp index 22c7a24f4..707a04962 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -85,14 +85,14 @@ struct Entity { Token token; Scope * scope; Type * type; - Ast * identifier; // Can be nullptr + Ast * identifier; // Can be nullptr DeclInfo * decl_info; DeclInfo * parent_proc_decl; // nullptr if in file/global scope AstPackage *pkg; // TODO(bill): Cleanup how `using` works for entities Entity * using_parent; - Ast * using_expr; + Ast * using_expr; isize order_in_src; String deprecated_message; @@ -109,7 +109,7 @@ struct Entity { String thread_local_model; Entity * foreign_library; - Ast * foreign_library_ident; + Ast * foreign_library_ident; String link_name; String link_prefix; bool is_foreign; @@ -117,9 +117,9 @@ struct Entity { bool is_immutable; } Variable; struct { - bool is_type_alias; Type * type_parameter_specialization; String ir_mangled_name; + bool is_type_alias; } TypeName; struct { u64 tags; diff --git a/src/ir.cpp b/src/ir.cpp index 7ee3c9600..618996bcc 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -3823,6 +3823,37 @@ irValue *ir_emit_comp(irProcedure *proc, TokenKind op_kind, irValue *left, irVal return ir_emit_runtime_call(proc, runtime_proc, args); } + if (is_type_bit_set(a)) { + switch (op_kind) { + case Token_Lt: + case Token_LtEq: + case Token_Gt: + case Token_GtEq: + { + Type *it = bit_set_to_int(a); + irValue *lhs = ir_emit_bitcast(proc, left, it); + irValue *rhs = ir_emit_bitcast(proc, right, it); + irValue *res = ir_emit_arith(proc, Token_And, lhs, rhs, it); + + if (op_kind == Token_Lt || op_kind == Token_LtEq) { + // (lhs & rhs) == lhs + res = ir_emit(proc, ir_instr_binary_op(proc, Token_CmpEq, res, lhs, t_llvm_bool)); + } else if (op_kind == Token_Gt || op_kind == Token_GtEq) { + // (lhs & rhs) == rhs + res = ir_emit(proc, ir_instr_binary_op(proc, Token_CmpEq, res, rhs, t_llvm_bool)); + } + + // NOTE(bill): Strict subsets + if (op_kind == Token_Lt || op_kind == Token_Gt) { + // res &~ (lhs == rhs) + irValue *eq = ir_emit(proc, ir_instr_binary_op(proc, Token_CmpEq, lhs, rhs, t_llvm_bool)); + res = ir_emit_arith(proc, Token_AndNot, res, eq, t_llvm_bool); + } + + return res; + } + } + } return ir_emit(proc, ir_instr_binary_op(proc, op_kind, left, right, t_llvm_bool)); } @@ -6168,7 +6199,6 @@ irValue *ir_build_expr_internal(irProcedure *proc, Ast *expr) { Type *key_type = rt->BitSet.elem; GB_ASSERT(are_types_identical(ir_type(left), key_type)); - Type *it = bit_set_to_int(rt); left = ir_emit_conv(proc, left, it); |