|
2 | 2 |
|
3 | 3 | module internal FSharp.Compiler.LowerComputedCollectionExpressions
|
4 | 4 |
|
5 |
| -open Internal.Utilities.Library |
| 5 | +open FSharp.Compiler.AbstractIL.IL |
6 | 6 | open FSharp.Compiler.AccessibilityLogic
|
7 | 7 | open FSharp.Compiler.DiagnosticsLogger
|
| 8 | +open FSharp.Compiler.Features |
8 | 9 | open FSharp.Compiler.InfoReader
|
9 | 10 | open FSharp.Compiler.LowerSequenceExpressions
|
10 | 11 | open FSharp.Compiler.MethodCalls
|
@@ -255,18 +256,178 @@ let (|SeqToArray|_|) g expr =
|
255 | 256 | | ValApp g g.seq_to_array_vref (_, [seqExpr], m) -> ValueSome (seqExpr, m)
|
256 | 257 | | _ -> ValueNone
|
257 | 258 |
|
| 259 | +module List = |
| 260 | + /// Makes an expression that will build a list from an integral range. |
| 261 | + let mkFromIntegralRange tcVal (g: TcGlobals) amap m overallElemTy overallSeqExpr start step finish = |
| 262 | + let collectorTy = g.mk_ListCollector_ty overallElemTy |
| 263 | + |
| 264 | + /// let collector = ListCollector () in |
| 265 | + /// <initialization loop> |
| 266 | + /// collector.Close () |
| 267 | + let mkListInit mkLoop = |
| 268 | + mkCompGenLetMutableIn m "collector" collectorTy (mkDefault (m, collectorTy)) (fun (_, collector) -> |
| 269 | + let reader = InfoReader (g, amap) |
| 270 | + let loop = mkLoop (fun _idxVar loopVar -> mkCallCollectorAdd tcVal g reader m collector loopVar) |
| 271 | + let close = mkCallCollectorClose tcVal g reader m collector |
| 272 | + mkSequential m loop close |
| 273 | + ) |
| 274 | + |
| 275 | + mkOptimizedRangeLoop |
| 276 | + g |
| 277 | + (m, m, m, DebugPointAtWhile.No) |
| 278 | + (overallElemTy, overallSeqExpr) |
| 279 | + (start, step, finish) |
| 280 | + (fun count mkLoop -> |
| 281 | + match count with |
| 282 | + | Expr.Const (value = IntegralConst.Zero) -> |
| 283 | + mkNil g m overallElemTy |
| 284 | + |
| 285 | + | Expr.Const (value = _nonzeroConstant) -> |
| 286 | + mkListInit mkLoop |
| 287 | + |
| 288 | + | _dynamicCount -> |
| 289 | + mkListInit mkLoop |
| 290 | + ) |
| 291 | + |
| 292 | +module Array = |
| 293 | + /// Whether to check for overflow when converting a value to a native int. |
| 294 | + [<NoEquality; NoComparison>] |
| 295 | + type Ovf = |
| 296 | + /// Check for overflow. We need this when passing the count into newarr. |
| 297 | + | CheckOvf |
| 298 | + |
| 299 | + /// Don't check for overflow. We don't need to check when indexing into the array, |
| 300 | + /// since we already know count didn't overflow during initialization. |
| 301 | + | NoCheckOvf |
| 302 | + |
| 303 | + /// Makes an expression that will build an array from an integral range. |
| 304 | + let mkFromIntegralRange g m overallElemTy overallSeqExpr start step finish = |
| 305 | + let arrayTy = mkArrayType g overallElemTy |
| 306 | + |
| 307 | + let convToNativeInt ovf expr = |
| 308 | + let ty = stripMeasuresFromTy g (tyOfExpr g expr) |
| 309 | + |
| 310 | + let conv = |
| 311 | + match ovf with |
| 312 | + | NoCheckOvf -> AI_conv DT_I |
| 313 | + | CheckOvf when isSignedIntegerTy g ty -> AI_conv_ovf DT_I |
| 314 | + | CheckOvf -> AI_conv_ovf_un DT_I |
| 315 | + |
| 316 | + if typeEquiv g ty g.int64_ty then |
| 317 | + mkAsmExpr ([conv], [], [expr], [g.nativeint_ty], m) |
| 318 | + elif typeEquiv g ty g.nativeint_ty then |
| 319 | + mkAsmExpr ([conv], [], [mkAsmExpr ([AI_conv DT_I8], [], [expr], [g.int64_ty], m)], [g.nativeint_ty], m) |
| 320 | + elif typeEquiv g ty g.uint64_ty then |
| 321 | + mkAsmExpr ([conv], [], [expr], [g.nativeint_ty], m) |
| 322 | + elif typeEquiv g ty g.unativeint_ty then |
| 323 | + mkAsmExpr ([conv], [], [mkAsmExpr ([AI_conv DT_U8], [], [expr], [g.uint64_ty], m)], [g.nativeint_ty], m) |
| 324 | + else |
| 325 | + expr |
| 326 | + |
| 327 | + let ilTy, ilBasicTy = |
| 328 | + let ty = stripMeasuresFromTy g overallElemTy |
| 329 | + |
| 330 | + if typeEquiv g ty g.int32_ty then g.ilg.typ_Int32, DT_I4 |
| 331 | + elif typeEquiv g ty g.int64_ty then g.ilg.typ_Int64, DT_I8 |
| 332 | + elif typeEquiv g ty g.uint64_ty then g.ilg.typ_UInt64, DT_U8 |
| 333 | + elif typeEquiv g ty g.uint32_ty then g.ilg.typ_UInt32, DT_U4 |
| 334 | + elif typeEquiv g ty g.nativeint_ty then g.ilg.typ_IntPtr, DT_I |
| 335 | + elif typeEquiv g ty g.unativeint_ty then g.ilg.typ_UIntPtr, DT_U |
| 336 | + elif typeEquiv g ty g.int16_ty then g.ilg.typ_Int16, DT_I2 |
| 337 | + elif typeEquiv g ty g.uint16_ty then g.ilg.typ_UInt16, DT_U2 |
| 338 | + elif typeEquiv g ty g.sbyte_ty then g.ilg.typ_SByte, DT_I1 |
| 339 | + elif typeEquiv g ty g.byte_ty then g.ilg.typ_Byte, DT_U1 |
| 340 | + elif typeEquiv g ty g.char_ty then g.ilg.typ_Char, DT_U2 |
| 341 | + else error (InternalError ($"Unable to find IL type for integral type '{overallElemTy}'.", m)) |
| 342 | + |
| 343 | + /// (# "newarr !0" type ('T) count : 'T array #) |
| 344 | + let mkNewArray count = |
| 345 | + mkAsmExpr |
| 346 | + ( |
| 347 | + [I_newarr (ILArrayShape.SingleDimensional, ilTy)], |
| 348 | + [], |
| 349 | + [convToNativeInt CheckOvf count], |
| 350 | + [arrayTy], |
| 351 | + m |
| 352 | + ) |
| 353 | + |
| 354 | + /// let array = (# "newarr !0" type ('T) count : 'T array #) in |
| 355 | + /// <initialization loop> |
| 356 | + /// array |
| 357 | + let mkArrayInit count mkLoop = |
| 358 | + mkCompGenLetIn m "array" arrayTy (mkNewArray count) (fun (_, array) -> |
| 359 | + let loop = mkLoop (fun idxVar loopVar -> mkAsmExpr ([I_stelem ilBasicTy], [], [array; convToNativeInt NoCheckOvf idxVar; loopVar], [], m)) |
| 360 | + mkSequential m loop array) |
| 361 | + |
| 362 | + mkOptimizedRangeLoop |
| 363 | + g |
| 364 | + (m, m, m, DebugPointAtWhile.No) |
| 365 | + (overallElemTy, overallSeqExpr) |
| 366 | + (start, step, finish) |
| 367 | + (fun count mkLoop -> |
| 368 | + match count with |
| 369 | + | Expr.Const (value = IntegralConst.Zero) -> |
| 370 | + mkArray (overallElemTy, [], m) |
| 371 | + |
| 372 | + | Expr.Const (value = _nonzeroConstant) -> |
| 373 | + mkArrayInit count mkLoop |
| 374 | + |
| 375 | + | _dynamicCount -> |
| 376 | + mkCompGenLetIn m (nameof count) (tyOfExpr g count) count (fun (_, count) -> |
| 377 | + let countTy = tyOfExpr g count |
| 378 | + |
| 379 | + // count < 1 |
| 380 | + let countLtOne = |
| 381 | + if isSignedIntegerTy g countTy then |
| 382 | + mkILAsmClt g m count (mkTypedOne g m countTy) |
| 383 | + else |
| 384 | + mkAsmExpr ([AI_clt_un], [], [count; mkTypedOne g m countTy], [g.bool_ty], m) |
| 385 | + |
| 386 | + // if count < 1 then |
| 387 | + // [||] |
| 388 | + // else |
| 389 | + // let array = (# "newarr !0" type ('T) count : 'T array #) in |
| 390 | + // <initialization loop> |
| 391 | + // array |
| 392 | + mkCond |
| 393 | + DebugPointAtBinding.NoneAtInvisible |
| 394 | + m |
| 395 | + arrayTy |
| 396 | + countLtOne |
| 397 | + (mkArray (overallElemTy, [], m)) |
| 398 | + (mkArrayInit count mkLoop) |
| 399 | + ) |
| 400 | + ) |
| 401 | + |
258 | 402 | let LowerComputedListOrArrayExpr tcVal (g: TcGlobals) amap overallExpr =
|
259 | 403 | // If ListCollector is in FSharp.Core then this optimization kicks in
|
260 | 404 | if g.ListCollector_tcr.CanDeref then
|
261 |
| - |
262 | 405 | match overallExpr with
|
| 406 | + // […] |
263 | 407 | | SeqToList g (OptionalCoerce (OptionalSeq g amap (overallSeqExpr, overallElemTy)), m) ->
|
264 |
| - let collectorTy = g.mk_ListCollector_ty overallElemTy |
265 |
| - LowerComputedListOrArraySeqExpr tcVal g amap m collectorTy overallSeqExpr |
266 |
| - |
| 408 | + match overallSeqExpr with |
| 409 | + // [start..finish] |
| 410 | + // [start..step..finish] |
| 411 | + | IntegralRange g (_, (start, step, finish)) when g.langVersion.SupportsFeature LanguageFeature.LowerIntegralRangesToFastLoops -> |
| 412 | + Some (List.mkFromIntegralRange tcVal g amap m overallElemTy overallSeqExpr start step finish) |
| 413 | + |
| 414 | + // [(* Anything more complex. *)] |
| 415 | + | _ -> |
| 416 | + let collectorTy = g.mk_ListCollector_ty overallElemTy |
| 417 | + LowerComputedListOrArraySeqExpr tcVal g amap m collectorTy overallSeqExpr |
| 418 | + |
| 419 | + // [|…|] |
267 | 420 | | SeqToArray g (OptionalCoerce (OptionalSeq g amap (overallSeqExpr, overallElemTy)), m) ->
|
268 |
| - let collectorTy = g.mk_ArrayCollector_ty overallElemTy |
269 |
| - LowerComputedListOrArraySeqExpr tcVal g amap m collectorTy overallSeqExpr |
| 421 | + match overallSeqExpr with |
| 422 | + // [|start..finish|] |
| 423 | + // [|start..step..finish|] |
| 424 | + | IntegralRange g (_, (start, step, finish)) when g.langVersion.SupportsFeature LanguageFeature.LowerIntegralRangesToFastLoops -> |
| 425 | + Some (Array.mkFromIntegralRange g m overallElemTy overallSeqExpr start step finish) |
| 426 | + |
| 427 | + // [|(* Anything more complex. *)|] |
| 428 | + | _ -> |
| 429 | + let collectorTy = g.mk_ArrayCollector_ty overallElemTy |
| 430 | + LowerComputedListOrArraySeqExpr tcVal g amap m collectorTy overallSeqExpr |
270 | 431 |
|
271 | 432 | | _ -> None
|
272 | 433 | else
|
|
0 commit comments