Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
fix logical operators returning -1 when true, fixes #71
Browse files Browse the repository at this point in the history
  • Loading branch information
cpdt committed Sep 21, 2018
1 parent afd882d commit 98fc986
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions compiler/src/codegen/block/gen_math_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,39 @@ pub fn gen_math_op_statement(
left_vec,
right_vec,
node.ctx.context.i32_type(),
true,
&|lhs, rhs| node.ctx.b.build_and(lhs, rhs, "num.and.vec"),
),
OperatorType::BitwiseOr => apply_int_op(
node,
left_vec,
right_vec,
node.ctx.context.i32_type(),
true,
&|lhs, rhs| node.ctx.b.build_or(lhs, rhs, "num.or.vec"),
),
OperatorType::BitwiseXor => apply_int_op(
node,
left_vec,
right_vec,
node.ctx.context.i32_type(),
true,
&|lhs, rhs| node.ctx.b.build_xor(lhs, rhs, "num.xor.vec"),
),
OperatorType::LogicalAnd => apply_int_op(
node,
left_vec,
right_vec,
node.ctx.context.bool_type(),
false,
&|lhs, rhs| node.ctx.b.build_and(lhs, rhs, "num.and.vec"),
),
OperatorType::LogicalOr => apply_int_op(
node,
left_vec,
right_vec,
node.ctx.context.bool_type(),
false,
&|lhs, rhs| node.ctx.b.build_or(lhs, rhs, "num.or.vec"),
),
OperatorType::LogicalEqual => unsigned_int_to_float_vec(
Expand Down Expand Up @@ -138,16 +143,26 @@ fn apply_int_op(
lhs: VectorValue,
rhs: VectorValue,
num_type: IntType,
signed_result: bool,
op: &Fn(VectorValue, VectorValue) -> VectorValue,
) -> VectorValue {
let left_int = float_vec_to_int(node, lhs, num_type);
let right_int = float_vec_to_int(node, rhs, num_type);
let result_int = op(left_int, right_int);
node.ctx.b.build_signed_int_to_float(
result_int,
node.ctx.context.f32_type().vec_type(2),
"num.vec.float",
)

if signed_result {
node.ctx.b.build_signed_int_to_float(
result_int,
node.ctx.context.f32_type().vec_type(2),
"num.vec.float",
)
} else {
node.ctx.b.build_unsigned_int_to_float(
result_int,
node.ctx.context.f32_type().vec_type(2),
"num.vec.float",
)
}
}

fn float_vec_to_int(node: &BlockContext, vec: VectorValue, num_type: IntType) -> VectorValue {
Expand Down

0 comments on commit 98fc986

Please sign in to comment.