Skip to content

Commit

Permalink
C++: Insert int-to-bool conversions at binary conditional expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasVP committed Nov 19, 2024
1 parent 9dd6678 commit 50b332c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ newtype TInstructionTag =
ValueConditionCompareTag() or
ValueConditionConstantTag() or
ValueConditionConditionalBranchTag() or
ValueConditionConditionalConstantTag() or
ValueConditionConditionalCompareTag() or
ConditionValueTrueTempAddressTag() or
ConditionValueTrueConstantTag() or
ConditionValueTrueStoreTag() or
Expand Down Expand Up @@ -172,6 +174,10 @@ string getInstructionTagId(TInstructionTag tag) {
or
tag = ValueConditionConditionalBranchTag() and result = "ValCondCondBranch"
or
tag = ValueConditionConditionalConstantTag() and result = "ValueConditionConditionalConstant"
or
tag = ValueConditionConditionalCompareTag() and result = "ValueConditionConditionalCompare"
or
tag = ValueConditionCompareTag() and result = "ValCondCondCompare"
or
tag = ValueConditionConstantTag() and result = "ValCondConstant"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2957,18 +2957,42 @@ class TranslatedBinaryConditionalExpr extends TranslatedConditionalExpr {
result = this.getCondition().getFirstInstruction(kind)
}

private Type getConditionType() { result = this.getCondition().getExprType() }

override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
super.hasInstruction(opcode, tag, resultType)
or
// For the binary variant, we create our own conditional branch.
tag = ValueConditionConditionalBranchTag() and
opcode instanceof Opcode::ConditionalBranch and
resultType = getVoidType()
or
not this.getConditionType() instanceof BoolType and
(
tag = ValueConditionConditionalConstantTag() and
opcode instanceof Opcode::Constant and
resultType = getIntType()
or
tag = ValueConditionConditionalCompareTag() and
opcode instanceof Opcode::CompareNE and
resultType = getBoolType()
)
}

override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) {
result = super.getInstructionSuccessorInternal(tag, kind)
or
not this.getConditionType() instanceof BoolType and
(
tag = ValueConditionConditionalConstantTag() and
kind instanceof GotoEdge and
result = this.getInstruction(ValueConditionConditionalCompareTag())
or
tag = ValueConditionConditionalCompareTag() and
kind instanceof GotoEdge and
result = this.getInstruction(ValueConditionConditionalBranchTag())
)
or
tag = ValueConditionConditionalBranchTag() and
(
kind instanceof TrueEdge and
Expand All @@ -2984,15 +3008,29 @@ class TranslatedBinaryConditionalExpr extends TranslatedConditionalExpr {
or
tag = ValueConditionConditionalBranchTag() and
operandTag instanceof ConditionOperandTag and
result = this.getCondition().getResult()
if this.getConditionType() instanceof BoolType
then result = this.getCondition().getResult()
else result = this.getInstruction(ValueConditionConditionalCompareTag())
or
not this.getConditionType() instanceof BoolType and
tag = ValueConditionConditionalCompareTag() and
(
operandTag instanceof LeftOperandTag and
result = this.getCondition().getResult()
or
operandTag instanceof RightOperandTag and
result = this.getInstruction(ValueConditionConditionalConstantTag())
)
}

override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) {
result = super.getChildSuccessorInternal(child, kind)
or
kind instanceof GotoEdge and
child = this.getCondition() and
result = this.getInstruction(ValueConditionConditionalBranchTag())
if this.getConditionType() instanceof BoolType
then result = this.getInstruction(ValueConditionConditionalBranchTag())
else result = this.getInstruction(ValueConditionConditionalConstantTag())
}

private TranslatedExpr getCondition() {
Expand All @@ -3009,6 +3047,11 @@ class TranslatedBinaryConditionalExpr extends TranslatedConditionalExpr {
// always converting the "then" operand to `bool`, which is almost always the wrong type.
result = getTranslatedExpr(expr.getThen().getExplicitlyConverted())
}

override string getInstructionConstantValue(InstructionTag tag) {
tag = ValueConditionConditionalConstantTag() and
result = "0"
}
}

/**
Expand Down

0 comments on commit 50b332c

Please sign in to comment.