@@ -303,6 +303,7 @@ let applyOp (com: ICompiler) (ctx: Context) r t opName (args: Expr list) =
303303 let truncateUnsigned operation = // see #1550
304304 match t with
305305 | Number( UInt32, _) -> Operation( Binary( BinaryShiftRightZeroFill, operation, makeIntConst 0 ), Tags.empty, t, r)
306+ | Number( UInt64, _) -> Helper.LibCall( com, " Util" , " toUInt64" , t, [ operation ], ?loc = r)
306307 | _ -> operation
307308
308309 let logicOp op left right =
@@ -326,6 +327,8 @@ let applyOp (com: ICompiler) (ctx: Context) r t opName (args: Expr list) =
326327 | Operators.rightShift, [ left; right ] ->
327328 match argTypes with
328329 | Number( UInt32, _) :: _ -> binOp BinaryShiftRightZeroFill left right // See #646
330+ | Number( UInt64, _) :: _ ->
331+ Helper.LibCall( com, " Util" , " rightShiftUnsigned64" , t, [ left; right ], argTypes, ?loc = r)
329332 | _ -> binOp BinaryShiftRightSignPropagating left right
330333 | Operators.bitwiseAnd, [ left; right ] -> binOp BinaryAndBitwise left right |> truncateUnsigned
331334 | Operators.bitwiseOr, [ left; right ] -> binOp BinaryOrBitwise left right |> truncateUnsigned
@@ -334,13 +337,12 @@ let applyOp (com: ICompiler) (ctx: Context) r t opName (args: Expr list) =
334337 | Operators.booleanOr, [ left; right ] -> logicOp LogicalOr left right
335338 | Operators.logicalNot, [ operand ] -> unOp UnaryNotBitwise operand |> truncateUnsigned
336339 | Operators.unaryNegation, [ operand ] ->
337- // TODO: Check for min value, see "Unary negation with integer MinValue works" test
338- unOp UnaryMinus operand
339- // match argTypes with
340- // | Number(Int8,_)::_ -> Helper.LibCall(com, "Int32", "op_UnaryNegation_Int8", t, args, ?loc=r)
341- // | Number(Int16,_)::_ -> Helper.LibCall(com, "Int32", "op_UnaryNegation_Int16", t, args, ?loc=r)
342- // | Number(Int32,_)::_ -> Helper.LibCall(com, "Int32", "op_UnaryNegation_Int32", t, args, ?loc=r)
343- // | _ -> unOp UnaryMinus operand
340+ match argTypes with
341+ | Number( Int8, _) :: _ -> Helper.LibCall( com, " Util" , " negateInt8" , t, args, ?loc = r)
342+ | Number( Int16, _) :: _ -> Helper.LibCall( com, " Util" , " negateInt16" , t, args, ?loc = r)
343+ | Number( Int32, _) :: _ -> Helper.LibCall( com, " Util" , " negateInt32" , t, args, ?loc = r)
344+ | Number( Int64, _) :: _ -> Helper.LibCall( com, " Util" , " negateInt64" , t, args, ?loc = r)
345+ | _ -> unOp UnaryMinus operand
344346 | Operators.unaryPlus, [ operand ] -> unOp UnaryPlus operand
345347 | _ ->
346348 $" Operator %s {opName} not found in %A {argTypes}"
@@ -944,10 +946,14 @@ let defaultValue com ctx r t defValue option =
944946 |> Some
945947
946948let operators ( com : ICompiler ) ( ctx : Context ) r t ( i : CallInfo ) ( thisArg : Expr option ) ( args : Expr list ) =
947- let math r t ( args : Expr list ) argTypes genArgs methName =
949+ let math r t ( args : Expr list ) argTypes methName =
948950 let meth = Naming.lowerFirst methName
949951
950- Helper.ImportedCall( " dart:math" , meth, t, args, argTypes, genArgs = genArgs, ?loc = r)
952+ let call = Helper.ImportedCall( " dart:math" , meth, t, args, argTypes, ?loc = r)
953+
954+ match meth, t with
955+ | " pow" , Number(( Float32 | Float64), _) -> Helper.InstanceCall( call, " toDouble" , t, [], ?loc = r)
956+ | _ -> call
951957
952958 match i.CompiledName, args with
953959 | ( " DefaultArg" | " DefaultValueArg" ), [ option; defValue ] -> defaultValue com ctx r t defValue option
@@ -1037,7 +1043,7 @@ let operators (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
10371043 )
10381044 |> Some
10391045 | CustomOp com ctx r t " Pow" args e -> Some e
1040- | _ -> math r t args i.SignatureArgTypes i.GenericArgs " pow" |> Some
1046+ | _ -> math r t args i.SignatureArgTypes " pow" |> Some
10411047 | ( " Ceiling" | " Floor" as meth), [ arg ] ->
10421048 let meth = Naming.lowerFirst meth
10431049
@@ -1065,11 +1071,14 @@ let operators (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
10651071 Helper.InstanceCall( arg, meth, t, [], ?loc = r) |> Some
10661072 | " Log" , [ arg1; arg2 ] ->
10671073 // "Math.log($0) / Math.log($1)"
1068- let dividend = math None t [ arg1 ] [] ( List.take 1 i.SignatureArgTypes) " log"
1074+ let dividend = math None t [ arg1 ] ( List.take 1 i.SignatureArgTypes) " log"
10691075
1070- let divisor = math None t [ arg2 ] [] ( List.skip 1 i.SignatureArgTypes) " log"
1076+ let divisor = math None t [ arg2 ] ( List.skip 1 i.SignatureArgTypes) " log"
10711077
10721078 makeBinOp r t dividend divisor BinaryDivide |> Some
1079+ | ( " Cosh" | " Sinh" | " Tanh" | " Log2" | " Log10" as meth), _ ->
1080+ Helper.LibCall( com, " Util" , Naming.lowerFirst meth, t, args, i.SignatureArgTypes, ?loc = r)
1081+ |> Some
10731082 | " Abs" , [ arg ] ->
10741083 match arg with
10751084 | ExprType( Number( BigInt | Decimal as kind, _)) ->
@@ -1096,17 +1105,13 @@ let operators (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
10961105 | " Atan" , _
10971106 | " Atan2" , _
10981107 | " Cos" , _
1099- | " Cosh" , _
11001108 | " Exp" , _
11011109 | " Log" , _
1102- | " Log10" , _
11031110 | " Sin" , _
1104- | " Sinh" , _
11051111 | " Sqrt" , _
1106- | " Tan" , _
1107- | " Tanh" , _ ->
1112+ | " Tan" , _ ->
11081113 match args with
1109- | ExprType( Number(_, _)) :: _ -> math r t args i.SignatureArgTypes [] i.CompiledName |> Some
1114+ | ExprType( Number(_, _)) :: _ -> math r t args i.SignatureArgTypes i.CompiledName |> Some
11101115 | _ -> applyOp com ctx r t i.CompiledName args |> Some
11111116 | " Round" , _ ->
11121117 match args with
@@ -1124,17 +1129,7 @@ let operators (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
11241129 )
11251130 |> Some
11261131 | _ ->
1127- Helper.LibCall(
1128- com,
1129- " Util" ,
1130- " round" ,
1131- t,
1132- args,
1133- i.SignatureArgTypes,
1134- genArgs = i.GenericArgs,
1135- ?thisArg = thisArg,
1136- ?loc = r
1137- )
1132+ Helper.LibCall( com, " Util" , " round" , t, args, i.SignatureArgTypes, ?thisArg = thisArg, ?loc = r)
11381133 |> Some
11391134 | " Truncate" , _ ->
11401135 match args with
@@ -1151,21 +1146,14 @@ let operators (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
11511146 ?loc = r
11521147 )
11531148 |> Some
1154- | _ ->
1155- Helper.GlobalCall( " Math" , t, args, i.SignatureArgTypes, genArgs = i.GenericArgs, memb = " trunc" , ?loc = r)
1156- |> Some
1149+ | _ -> Helper.InstanceCall( args.Head, " truncateToDouble" , t, [], ?loc = r) |> Some
11571150 | " Sign" , _ ->
11581151 let args = toFloat com ctx r t args |> List.singleton
11591152
1160- Helper.LibCall( com, " Util" , " sign" , t, args, i.SignatureArgTypes, genArgs = i.GenericArgs , ?loc = r)
1153+ Helper.LibCall( com, " Util" , " sign" , t, args, i.SignatureArgTypes, ?loc = r)
11611154 |> Some
11621155 | " DivRem" , _ ->
1163- let modName =
1164- match i.SignatureArgTypes with
1165- | Number( Int64, _) :: _ -> " Long"
1166- | _ -> " Int32"
1167-
1168- Helper.LibCall( com, modName, " divRem" , t, args, i.SignatureArgTypes, genArgs = i.GenericArgs, ?loc = r)
1156+ Helper.LibCall( com, " Types" , " divRem" , t, args, i.SignatureArgTypes, ?loc = r)
11691157 |> Some
11701158 // Numbers
11711159 | ( " Infinity" | " InfinitySingle" ), _ -> Helper.GlobalIdent( " Number" , " POSITIVE_INFINITY" , t, ?loc = r) |> Some
@@ -1209,12 +1197,15 @@ let operators (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
12091197 | ( Operators.greaterThan | " Gt" ), [ left; right ] -> booleanCompare com ctx r left right BinaryGreater |> Some
12101198 | ( Operators.greaterThanOrEqual | " Gte" ), [ left; right ] ->
12111199 booleanCompare com ctx r left right BinaryGreaterOrEqual |> Some
1212- | ( " Min" | " Max" | " Clamp" as meth), _ ->
1200+ | ( " Min" | " Max" | " MinMagnitude " | " MaxMagnitude " | " Clamp" as meth), _ ->
12131201 let meth = Naming.lowerFirst meth
12141202
12151203 match meth, t with
12161204 | ( " min" | " max" ), Number(( DartInt | DartDouble), NumberInfo.Empty) ->
1217- Helper.ImportedCall( " dart:math" , meth, t, args, i.SignatureArgTypes, genArgs = i.GenericArgs, ?loc = r)
1205+ Helper.ImportedCall( " dart:math" , meth, t, args, i.SignatureArgTypes, ?loc = r)
1206+ |> Some
1207+ | ( " minMagnitude" | " maxMagnitude" ), Number(( DartInt | DartDouble), NumberInfo.Empty) ->
1208+ Helper.LibCall( com, " Util" , meth, t, args, i.SignatureArgTypes, ?loc = r)
12181209 |> Some
12191210 | _ ->
12201211 let f = makeComparerFunction com ctx t
@@ -2046,46 +2037,31 @@ let parseNum (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
20462037 [ str; makeIntConst style; makeBoolConst unsigned; makeIntConst bitsize ]
20472038 @ outValue
20482039
2049- Helper.LibCall( com, numberModule, Naming.lowerFirst meth, t, args, ?loc = r)
2050- |> Some
2040+ match kind, meth with
2041+ | ( Float32 | Float64), " Parse" -> Helper.GlobalCall( " double" , t, [ str ], memb = " parse" , ?loc = r) |> Some
2042+ | _ ->
2043+ Helper.LibCall( com, numberModule, Naming.lowerFirst meth, t, args, ?loc = r)
2044+ |> Some
20512045
20522046 let isFloat =
20532047 match i.SignatureArgTypes with
20542048 | Number(( Float32 | Float64), _) :: _ -> true
20552049 | _ -> false
20562050
20572051 match i.CompiledName, args with
2058- | " IsNaN" , [ _ ] when isFloat -> Helper.GlobalCall( " Number" , t, args, memb = " isNaN" , ?loc = r) |> Some
2059- | " IsPositiveInfinity" , [ _ ] when isFloat ->
2060- Helper.LibCall(
2061- com,
2062- " Double" ,
2063- " isPositiveInfinity" ,
2064- t,
2065- args,
2066- i.SignatureArgTypes,
2067- genArgs = i.GenericArgs,
2068- ?loc = r
2069- )
2070- |> Some
2071- | " IsNegativeInfinity" , [ _ ] when isFloat ->
2072- Helper.LibCall(
2073- com,
2074- " Double" ,
2075- " isNegativeInfinity" ,
2076- t,
2077- args,
2078- i.SignatureArgTypes,
2079- genArgs = i.GenericArgs,
2080- ?loc = r
2081- )
2082- |> Some
2083- | " IsInfinity" , [ _ ] when isFloat ->
2084- Helper.LibCall( com, " Double" , " isInfinity" , t, args, i.SignatureArgTypes, genArgs = i.GenericArgs, ?loc = r)
2085- |> Some
2086- | " IsInfinity" , [ _ ] when isFloat ->
2087- Helper.LibCall( com, " Double" , " isInfinity" , t, args, i.SignatureArgTypes, genArgs = i.GenericArgs, ?loc = r)
2088- |> Some
2052+ | " IsNaN" , [ _ ] when isFloat -> emitExpr r t args " $0.isNaN" |> Some
2053+ | " Log2" , _ ->
2054+ let log =
2055+ Helper.LibCall( com, " Util" , " log2" , Float64.Number, args, i.SignatureArgTypes, ?loc = r)
2056+
2057+ match t with
2058+ | Number(( Float32 | Float64), _) -> TypeCast( log, t) |> Some
2059+ | Number _ -> toInt com ctx r t [ log ] |> Some
2060+ | _ -> log |> Some
2061+ | " IsPositiveInfinity" , [ _ ] when isFloat -> emitExpr r t args " $0 == double.infinity" |> Some
2062+ | " IsNegativeInfinity" , [ _ ] when isFloat -> emitExpr r t args " $0 == double.negativeInfinity" |> Some
2063+ | " IsInfinity" , [ _ ] when isFloat -> emitExpr r t args " $0.isInfinite" |> Some
2064+ | ( " Min" | " Max" | " MinMagnitude" | " MaxMagnitude" | " Clamp" ), _ -> operators com ctx r t i thisArg args
20892065 | ( " Parse" | " TryParse" ) as meth, str :: NumberConst( NumberValue.Int32 style, _) :: _ ->
20902066 let hexConst = int System.Globalization.NumberStyles.HexNumber
20912067 let intConst = int System.Globalization.NumberStyles.Integer
0 commit comments