5
5
* LICENSE file in the root directory of this source tree.
6
6
*/
7
7
8
- import prettyFormat from 'pretty-format' ;
9
8
import { CompilerError , SourceLocation } from '..' ;
10
9
import {
11
10
BlockId ,
@@ -23,14 +22,9 @@ import {
23
22
eachTerminalOperand ,
24
23
} from '../HIR/visitors' ;
25
24
import { assertExhaustive , getOrInsertWith } from '../Utils/utils' ;
26
- import { printFunction } from '../HIR' ;
27
- import { printIdentifier , printPlace } from '../HIR/PrintHIR' ;
28
25
import { MutationKind } from './InferFunctionExpressionAliasingEffectsSignature' ;
29
26
import { Result } from '../Utils/Result' ;
30
27
31
- const DEBUG = false ;
32
- const VERBOSE = false ;
33
-
34
28
/**
35
29
* Infers mutable ranges for all values in the program, using previously inferred
36
30
* mutation/aliasing effects. This pass builds a data flow graph using the effects,
@@ -56,10 +50,6 @@ export function inferMutationAliasingRanges(
56
50
fn : HIRFunction ,
57
51
{ isFunctionExpression} : { isFunctionExpression : boolean } ,
58
52
) : Result < void , CompilerError > {
59
- if ( VERBOSE ) {
60
- console . log ( ) ;
61
- console . log ( printFunction ( fn ) ) ;
62
- }
63
53
/**
64
54
* Part 1: Infer mutable ranges for values. We build an abstract model of
65
55
* values, the alias/capture edges between them, and the set of mutations.
@@ -115,20 +105,6 @@ export function inferMutationAliasingRanges(
115
105
seenBlocks . add ( block . id ) ;
116
106
117
107
for ( const instr of block . instructions ) {
118
- if (
119
- instr . value . kind === 'FunctionExpression' ||
120
- instr . value . kind === 'ObjectMethod'
121
- ) {
122
- state . create ( instr . lvalue , {
123
- kind : 'Function' ,
124
- function : instr . value . loweredFunc . func ,
125
- } ) ;
126
- } else {
127
- for ( const lvalue of eachInstructionLValue ( instr ) ) {
128
- state . create ( lvalue , { kind : 'Object' } ) ;
129
- }
130
- }
131
-
132
108
if ( instr . effects == null ) continue ;
133
109
for ( const effect of instr . effects ) {
134
110
if ( effect . kind === 'Create' ) {
@@ -141,6 +117,15 @@ export function inferMutationAliasingRanges(
141
117
} else if ( effect . kind === 'CreateFrom' ) {
142
118
state . createFrom ( index ++ , effect . from , effect . into ) ;
143
119
} else if ( effect . kind === 'Assign' ) {
120
+ /**
121
+ * TODO: Invariant that the node is not initialized yet
122
+ *
123
+ * InferFunctionExpressionAliasingEffectSignatures currently infers
124
+ * Assign effects in some places that should be Alias, leading to
125
+ * Assign effects that reinitialize a value. The end result appears to
126
+ * be fine, but we should fix that inference pass so that we add the
127
+ * invariant here.
128
+ */
144
129
if ( ! state . nodes . has ( effect . into . identifier ) ) {
145
130
state . create ( effect . into , { kind : 'Object' } ) ;
146
131
}
@@ -216,10 +201,6 @@ export function inferMutationAliasingRanges(
216
201
}
217
202
}
218
203
219
- if ( VERBOSE ) {
220
- console . log ( state . debug ( ) ) ;
221
- console . log ( pretty ( mutations ) ) ;
222
- }
223
204
for ( const mutation of mutations ) {
224
205
state . mutate (
225
206
mutation . index ,
@@ -234,9 +215,6 @@ export function inferMutationAliasingRanges(
234
215
for ( const render of renders ) {
235
216
state . render ( render . index , render . place . identifier , errors ) ;
236
217
}
237
- if ( DEBUG ) {
238
- console . log ( pretty ( [ ...state . nodes . keys ( ) ] ) ) ;
239
- }
240
218
fn . aliasingEffects ??= [ ] ;
241
219
for ( const param of [ ...fn . context , ...fn . params ] ) {
242
220
const place = param . kind === 'Identifier' ? param : param . place ;
@@ -458,9 +436,6 @@ export function inferMutationAliasingRanges(
458
436
}
459
437
}
460
438
461
- if ( VERBOSE ) {
462
- console . log ( printFunction ( fn ) ) ;
463
- }
464
439
return errors . asResult ( ) ;
465
440
}
466
441
@@ -511,11 +486,6 @@ class AliasingState {
511
486
const fromNode = this . nodes . get ( from . identifier ) ;
512
487
const toNode = this . nodes . get ( into . identifier ) ;
513
488
if ( fromNode == null || toNode == null ) {
514
- if ( VERBOSE ) {
515
- console . log (
516
- `skip: createFrom ${ printPlace ( from ) } ${ ! ! fromNode } -> ${ printPlace ( into ) } ${ ! ! toNode } ` ,
517
- ) ;
518
- }
519
489
return ;
520
490
}
521
491
fromNode . edges . push ( { index, node : into . identifier , kind : 'alias' } ) ;
@@ -528,11 +498,6 @@ class AliasingState {
528
498
const fromNode = this . nodes . get ( from . identifier ) ;
529
499
const toNode = this . nodes . get ( into . identifier ) ;
530
500
if ( fromNode == null || toNode == null ) {
531
- if ( VERBOSE ) {
532
- console . log (
533
- `skip: capture ${ printPlace ( from ) } ${ ! ! fromNode } -> ${ printPlace ( into ) } ${ ! ! toNode } ` ,
534
- ) ;
535
- }
536
501
return ;
537
502
}
538
503
fromNode . edges . push ( { index, node : into . identifier , kind : 'capture' } ) ;
@@ -545,11 +510,6 @@ class AliasingState {
545
510
const fromNode = this . nodes . get ( from . identifier ) ;
546
511
const toNode = this . nodes . get ( into . identifier ) ;
547
512
if ( fromNode == null || toNode == null ) {
548
- if ( VERBOSE ) {
549
- console . log (
550
- `skip: assign ${ printPlace ( from ) } ${ ! ! fromNode } -> ${ printPlace ( into ) } ${ ! ! toNode } ` ,
551
- ) ;
552
- }
553
513
return ;
554
514
}
555
515
fromNode . edges . push ( { index, node : into . identifier , kind : 'alias' } ) ;
@@ -604,11 +564,6 @@ class AliasingState {
604
564
loc : SourceLocation ,
605
565
errors : CompilerError ,
606
566
) : void {
607
- if ( DEBUG ) {
608
- console . log (
609
- `mutate ix=${ index } start=$${ start . id } end=[${ end } ]${ transitive ? ' transitive' : '' } kind=${ kind } ` ,
610
- ) ;
611
- }
612
567
const seen = new Set < Identifier > ( ) ;
613
568
const queue : Array < {
614
569
place : Identifier ;
@@ -623,18 +578,8 @@ class AliasingState {
623
578
seen . add ( current ) ;
624
579
const node = this . nodes . get ( current ) ;
625
580
if ( node == null ) {
626
- if ( DEBUG ) {
627
- console . log (
628
- `no node! ${ printIdentifier ( start ) } for identifier ${ printIdentifier ( current ) } ` ,
629
- ) ;
630
- }
631
581
continue ;
632
582
}
633
- if ( DEBUG ) {
634
- console . log (
635
- ` mutate $${ node . id . id } transitive=${ transitive } direction=${ direction } ` ,
636
- ) ;
637
- }
638
583
node . id . mutableRange . end = makeInstructionId (
639
584
Math . max ( node . id . mutableRange . end , end ) ,
640
585
) ;
@@ -701,37 +646,5 @@ class AliasingState {
701
646
}
702
647
}
703
648
}
704
- if ( DEBUG ) {
705
- const nodes = new Map ( ) ;
706
- for ( const id of seen ) {
707
- const node = this . nodes . get ( id ) ;
708
- nodes . set ( id . id , node ) ;
709
- }
710
- console . log ( pretty ( nodes ) ) ;
711
- }
712
649
}
713
-
714
- debug ( ) : string {
715
- return pretty ( this . nodes ) ;
716
- }
717
- }
718
-
719
- export function pretty ( v : any ) : string {
720
- return prettyFormat ( v , {
721
- plugins : [
722
- {
723
- test : v =>
724
- v !== null && typeof v === 'object' && v . kind === 'Identifier' ,
725
- serialize : v => printPlace ( v ) ,
726
- } ,
727
- {
728
- test : v =>
729
- v !== null &&
730
- typeof v === 'object' &&
731
- typeof v . declarationId === 'number' ,
732
- serialize : v =>
733
- `${ printIdentifier ( v ) } :${ v . mutableRange . start } :${ v . mutableRange . end } ` ,
734
- } ,
735
- ] ,
736
- } ) ;
737
650
}
0 commit comments