Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Compiler/TypedTree/TcGlobals.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,8 @@ type TcGlobals(
member val unchecked_unary_not_vref = ValRefForIntrinsic v_unchecked_unary_not_info
member val unchecked_subtraction_vref = ValRefForIntrinsic v_unchecked_subtraction_info
member val unchecked_multiply_vref = ValRefForIntrinsic v_unchecked_multiply_info
member val unchecked_division_vref = ValRefForIntrinsic v_unchecked_division_info
member val unchecked_modulus_vref = ValRefForIntrinsic v_unchecked_modulus_info
member val unchecked_defaultof_vref = ValRefForIntrinsic v_unchecked_defaultof_info
member val refcell_deref_vref = ValRefForIntrinsic v_refcell_deref_info
member val refcell_assign_vref = ValRefForIntrinsic v_refcell_assign_info
Expand Down
109 changes: 85 additions & 24 deletions src/Compiler/TypedTree/TypedTreeOps.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3742,6 +3742,14 @@ let (|SpecificUnopExpr|_|) g vrefReqd expr =
| UnopExpr g (vref, arg1) when valRefEq g vref vrefReqd -> Some arg1
| _ -> None

let (|SignedIntegerConstExpr|_|) expr =
match expr with
| Expr.Const (Const.Int32 _, _, _)
| Expr.Const (Const.SByte _, _, _)
| Expr.Const (Const.Int16 _, _, _)
| Expr.Const (Const.Int64 _, _, _) -> Some ()
| _ -> None

let (|SpecificBinopExpr|_|) g vrefReqd expr =
match expr with
| BinopExpr g (vref, arg1, arg2) when valRefEq g vref vrefReqd -> Some (arg1, arg2)
Expand Down Expand Up @@ -9647,12 +9655,44 @@ let IsSimpleSyntacticConstantExpr g inputExpr =
checkExpr vrefs e

checkExpr Set.empty inputExpr
let EvalArithBinOp (opInt8, opInt16, opInt32, opInt64, opUInt8, opUInt16, opUInt32, opUInt64) (arg1: Expr) (arg2: Expr) =
// At compile-time we check arithmetic

let EvalArithShiftOp (opInt8, opInt16, opInt32, opInt64, opUInt8, opUInt16, opUInt32, opUInt64) (arg1: Expr) (arg2: Expr) =
// At compile-time we check arithmetic
let m = unionRanges arg1.Range arg2.Range
try
match arg1, arg2 with
try
match arg1, arg2 with
| Expr.Const (Const.Int32 x1, _, ty), Expr.Const (Const.Int32 shift, _, _) -> Expr.Const (Const.Int32 (opInt32 x1 shift), m, ty)
| Expr.Const (Const.SByte x1, _, ty), Expr.Const (Const.Int32 shift, _, _) -> Expr.Const (Const.SByte (opInt8 x1 shift), m, ty)
| Expr.Const (Const.Int16 x1, _, ty), Expr.Const (Const.Int32 shift, _, _) -> Expr.Const (Const.Int16 (opInt16 x1 shift), m, ty)
| Expr.Const (Const.Int64 x1, _, ty), Expr.Const (Const.Int32 shift, _, _) -> Expr.Const (Const.Int64 (opInt64 x1 shift), m, ty)
| Expr.Const (Const.Byte x1, _, ty), Expr.Const (Const.Int32 shift, _, _) -> Expr.Const (Const.Byte (opUInt8 x1 shift), m, ty)
| Expr.Const (Const.UInt16 x1, _, ty), Expr.Const (Const.Int32 shift, _, _) -> Expr.Const (Const.UInt16 (opUInt16 x1 shift), m, ty)
| Expr.Const (Const.UInt32 x1, _, ty), Expr.Const (Const.Int32 shift, _, _) -> Expr.Const (Const.UInt32 (opUInt32 x1 shift), m, ty)
| Expr.Const (Const.UInt64 x1, _, ty), Expr.Const (Const.Int32 shift, _, _) -> Expr.Const (Const.UInt64 (opUInt64 x1 shift), m, ty)
| _ -> error (Error ( FSComp.SR.tastNotAConstantExpression(), m))
with :? System.OverflowException -> error (Error ( FSComp.SR.tastConstantExpressionOverflow(), m))

let EvalArithUnOp (opInt8, opInt16, opInt32, opInt64, opUInt8, opUInt16, opUInt32, opUInt64) (arg1: Expr) =
// At compile-time we check arithmetic
let m = arg1.Range
try
match arg1 with
| Expr.Const (Const.Int32 x1, _, ty) -> Expr.Const (Const.Int32 (opInt32 x1), m, ty)
| Expr.Const (Const.SByte x1, _, ty) -> Expr.Const (Const.SByte (opInt8 x1), m, ty)
| Expr.Const (Const.Int16 x1, _, ty) -> Expr.Const (Const.Int16 (opInt16 x1), m, ty)
| Expr.Const (Const.Int64 x1, _, ty) -> Expr.Const (Const.Int64 (opInt64 x1), m, ty)
| Expr.Const (Const.Byte x1, _, ty) -> Expr.Const (Const.Byte (opUInt8 x1), m, ty)
| Expr.Const (Const.UInt16 x1, _, ty) -> Expr.Const (Const.UInt16 (opUInt16 x1), m, ty)
| Expr.Const (Const.UInt32 x1, _, ty) -> Expr.Const (Const.UInt32 (opUInt32 x1), m, ty)
| Expr.Const (Const.UInt64 x1, _, ty) -> Expr.Const (Const.UInt64 (opUInt64 x1), m, ty)
| _ -> error (Error ( FSComp.SR.tastNotAConstantExpression(), m))
with :? System.OverflowException -> error (Error ( FSComp.SR.tastConstantExpressionOverflow(), m))

let EvalArithBinOp (opInt8, opInt16, opInt32, opInt64, opUInt8, opUInt16, opUInt32, opUInt64) (arg1: Expr) (arg2: Expr) =
// At compile-time we check arithmetic
let m = unionRanges arg1.Range arg2.Range
try
match arg1, arg2 with
| Expr.Const (Const.Int32 x1, _, ty), Expr.Const (Const.Int32 x2, _, _) -> Expr.Const (Const.Int32 (opInt32 x1 x2), m, ty)
| Expr.Const (Const.SByte x1, _, ty), Expr.Const (Const.SByte x2, _, _) -> Expr.Const (Const.SByte (opInt8 x1 x2), m, ty)
| Expr.Const (Const.Int16 x1, _, ty), Expr.Const (Const.Int16 x2, _, _) -> Expr.Const (Const.Int16 (opInt16 x1 x2), m, ty)
Expand Down Expand Up @@ -9700,29 +9740,50 @@ let rec EvalAttribArgExpr g x =
// Detect bitwise or of attribute flags
| AttribBitwiseOrExpr g (arg1, arg2) ->
EvalArithBinOp ((|||), (|||), (|||), (|||), (|||), (|||), (|||), (|||)) (EvalAttribArgExpr g arg1) (EvalAttribArgExpr g arg2)
| SpecificBinopExpr g g.unchecked_addition_vref (arg1, arg2) ->
// At compile-time we check arithmetic
let v1, v2 = EvalAttribArgExpr g arg1, EvalAttribArgExpr g arg2
match v1, v2 with
| Expr.Const (Const.String x1, m, ty), Expr.Const (Const.String x2, _, _) -> Expr.Const (Const.String (x1 + x2), m, ty)
| _ ->
#if ALLOW_ARITHMETIC_OPS_IN_LITERAL_EXPRESSIONS_AND_ATTRIBUTE_ARGS
EvalArithBinOp (Checked.(+), Checked.(+), Checked.(+), Checked.(+), Checked.(+), Checked.(+), Checked.(+), Checked.(+)) g v1 v2
#else
errorR (Error ( FSComp.SR.tastNotAConstantExpression(), x.Range))
x
#endif
#if ALLOW_ARITHMETIC_OPS_IN_LITERAL_EXPRESSIONS_AND_ATTRIBUTE_ARGS
| SpecificBinopExpr g g.unchecked_subtraction_vref (arg1, arg2) ->
EvalArithBinOp (Checked.(-), Checked.(-), Checked.(-), Checked.(-), Checked.(-), Checked.(-), Checked.(-), Checked.(-)) g (EvalAttribArgExpr g arg1) (EvalAttribArgExpr g arg2)
| SpecificBinopExpr g g.unchecked_multiply_vref (arg1, arg2) ->
EvalArithBinOp (Checked.(*), Checked.(*), Checked.(*), Checked.(*), Checked.(*), Checked.(*), Checked.(*), Checked.(*)) g (EvalAttribArgExpr g arg1) (EvalAttribArgExpr g arg2)
#endif
| SpecificBinopExpr g g.unchecked_addition_vref (arg1, arg2) ->
// At compile-time we check arithmetic
let v1, v2 = EvalAttribArgExpr g arg1, EvalAttribArgExpr g arg2
match v1, v2 with
| Expr.Const (Const.String x1, m, ty), Expr.Const (Const.String x2, _, _) -> Expr.Const (Const.String (x1 + x2), m, ty)
| _ ->
EvalArithBinOp (Checked.(+), Checked.(+), Checked.(+), Checked.(+), Checked.(+), Checked.(+), Checked.(+), Checked.(+)) v1 v2
| SpecificBinopExpr g g.unchecked_subtraction_vref (arg1, arg2) ->
EvalArithBinOp (Checked.(-), Checked.(-), Checked.(-), Checked.(-), Checked.(-), Checked.(-), Checked.(-), Checked.(-)) (EvalAttribArgExpr g arg1) (EvalAttribArgExpr g arg2)
| SpecificBinopExpr g g.unchecked_multiply_vref (arg1, arg2) ->
EvalArithBinOp (Checked.(*), Checked.(*), Checked.(*), Checked.(*), Checked.(*), Checked.(*), Checked.(*), Checked.(*)) (EvalAttribArgExpr g arg1) (EvalAttribArgExpr g arg2)
| SpecificBinopExpr g g.unchecked_division_vref (arg1, arg2) ->
EvalArithBinOp ((/), (/), (/), (/), (/), (/), (/), (/)) (EvalAttribArgExpr g arg1) (EvalAttribArgExpr g arg2)
| SpecificBinopExpr g g.unchecked_modulus_vref (arg1, arg2) ->
EvalArithBinOp ((%), (%), (%), (%), (%), (%), (%), (%)) (EvalAttribArgExpr g arg1) (EvalAttribArgExpr g arg2)
| SpecificBinopExpr g g.bitwise_shift_left_vref (arg1, arg2) ->
EvalArithShiftOp ((<<<), (<<<), (<<<), (<<<), (<<<), (<<<), (<<<), (<<<)) (EvalAttribArgExpr g arg1) (EvalAttribArgExpr g arg2)
| SpecificBinopExpr g g.bitwise_shift_right_vref (arg1, arg2) ->
EvalArithShiftOp ((>>>), (>>>), (>>>), (>>>), (>>>), (>>>), (>>>), (>>>)) (EvalAttribArgExpr g arg1) (EvalAttribArgExpr g arg2)
| SpecificBinopExpr g g.bitwise_and_vref (arg1, arg2) ->
EvalArithBinOp ((&&&), (&&&), (&&&), (&&&), (&&&), (&&&), (&&&), (&&&)) (EvalAttribArgExpr g arg1) (EvalAttribArgExpr g arg2)
| SpecificUnopExpr g g.unchecked_unary_minus_vref arg1 ->
let v1 = EvalAttribArgExpr g arg1

match v1 with
| SignedIntegerConstExpr ->
let ignore (_x: 'a) = Unchecked.defaultof<'a>
EvalArithUnOp (Checked.(~-), Checked.(~-), Checked.(~-), Checked.(~-), ignore, ignore, ignore, ignore) v1
| _ ->
errorR (Error ( FSComp.SR.tastNotAConstantExpression(), v1.Range))
x
| SpecificUnopExpr g g.unchecked_unary_plus_vref arg1 ->
EvalArithUnOp ((~+), (~+), (~+), (~+), (~+), (~+), (~+), (~+)) (EvalAttribArgExpr g arg1)
| SpecificUnopExpr g g.unchecked_unary_not_vref arg1 ->
match EvalAttribArgExpr g arg1 with
| Expr.Const (Const.Bool value, m, ty) ->
Expr.Const (Const.Bool (not value), m, ty)
| expr ->
errorR (Error ( FSComp.SR.tastNotAConstantExpression(), expr.Range))
x
| _ ->
errorR (Error ( FSComp.SR.tastNotAConstantExpression(), x.Range))
x


and EvaledAttribExprEquality g e1 e2 =
match e1, e2 with
| Expr.Const (c1, _, _), Expr.Const (c2, _, _) -> c1 = c2
Expand Down