Skip to content

Commit 821b1f6

Browse files
eee4017hydai
authored andcommitted
[CodeGen] Add sar(signed arithmetic shift right) support
1 parent 1bf21ee commit 821b1f6

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

include/soll/AST/Expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class BinaryOperator : public Expr {
241241
return Opc == BO_Add || Opc == BO_Sub;
242242
}
243243
static inline constexpr bool isShiftOp(Opcode Opc) {
244-
return Opc == BO_Shl || Opc == BO_Shr;
244+
return Opc == BO_Shl || Opc == BO_Shr || Opc == BO_AShr;
245245
}
246246
static inline constexpr bool isBitwiseOp(Opcode Opc) {
247247
return Opc >= BO_And && Opc <= BO_Or;

include/soll/AST/OperationKinds.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ enum BinaryOperatorKind {
3434
// Bitwise shift operators.
3535
BO_Shl,
3636
BO_Shr,
37+
BO_AShr,
3738
// Bitwise AND operator.
3839
BO_And,
3940
// Bitwise XOR operator.

lib/CodeGen/ExprEmitter.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,18 @@ ExprValuePtr ExprEmitter::visit(const BinaryOperator *BO) {
407407
}
408408
break;
409409
}
410+
case BinaryOperatorKind::BO_AShr: {
411+
auto LHSType = LHS->getType();
412+
auto RHSType = RHS->getType();
413+
auto returnType =
414+
LHSType->getIntegerBitWidth() > RHSType->getIntegerBitWidth()
415+
? LHSType
416+
: RHSType;
417+
V = Builder.CreateAShr(Builder.CreateZExtOrTrunc(LHS, returnType),
418+
Builder.CreateZExtOrTrunc(RHS, returnType),
419+
"BO_AShr");
420+
break;
421+
}
410422
case BinaryOperatorKind::BO_And:
411423
case BinaryOperatorKind::BO_AsmAnd:
412424
V = Builder.CreateAnd(LHS, RHS, "BO_And");

lib/Sema/SemaExprAsm.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,11 @@ Sema::CreateAsmBuiltinCallExpr(SourceRange L, const AsmIdentifier &Callee,
669669
case AsmIdentifier::SpecialIdentifier::shr:
670670
return std::make_unique<AsmBinaryOperator>(
671671
L, std::move(Args[1]), std::move(Args[0]), std::move(ReturnTy), BO_Shr);
672-
// TODO: case AsmIdentifier::SpecialIdentifier::sars256:
673-
// TODO: case AsmIdentifier::SpecialIdentifier::sar:
672+
case AsmIdentifier::SpecialIdentifier::sars256:
673+
case AsmIdentifier::SpecialIdentifier::sar:
674+
return std::make_unique<AsmBinaryOperator>(L, std::move(Args[1]),
675+
std::move(Args[0]),
676+
std::move(ReturnTy), BO_AShr);
674677
default: ///< treated as normal CallExpr
675678
break;
676679
}

0 commit comments

Comments
 (0)