55 * LICENSE file in the root directory of this source tree.
66 */
77
8- import prettyFormat from 'pretty-format' ;
98import { CompilerError , SourceLocation } from '..' ;
109import {
1110 BlockId ,
@@ -23,14 +22,9 @@ import {
2322 eachTerminalOperand ,
2423} from '../HIR/visitors' ;
2524import { assertExhaustive , getOrInsertWith } from '../Utils/utils' ;
26- import { printFunction } from '../HIR' ;
27- import { printIdentifier , printPlace } from '../HIR/PrintHIR' ;
2825import { MutationKind } from './InferFunctionExpressionAliasingEffectsSignature' ;
2926import { Result } from '../Utils/Result' ;
3027
31- const DEBUG = false ;
32- const VERBOSE = false ;
33-
3428/**
3529 * Infers mutable ranges for all values in the program, using previously inferred
3630 * mutation/aliasing effects. This pass builds a data flow graph using the effects,
@@ -56,10 +50,6 @@ export function inferMutationAliasingRanges(
5650 fn : HIRFunction ,
5751 { isFunctionExpression} : { isFunctionExpression : boolean } ,
5852) : Result < void , CompilerError > {
59- if ( VERBOSE ) {
60- console . log ( ) ;
61- console . log ( printFunction ( fn ) ) ;
62- }
6353 /**
6454 * Part 1: Infer mutable ranges for values. We build an abstract model of
6555 * values, the alias/capture edges between them, and the set of mutations.
@@ -115,20 +105,6 @@ export function inferMutationAliasingRanges(
115105 seenBlocks . add ( block . id ) ;
116106
117107 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-
132108 if ( instr . effects == null ) continue ;
133109 for ( const effect of instr . effects ) {
134110 if ( effect . kind === 'Create' ) {
@@ -141,6 +117,15 @@ export function inferMutationAliasingRanges(
141117 } else if ( effect . kind === 'CreateFrom' ) {
142118 state . createFrom ( index ++ , effect . from , effect . into ) ;
143119 } 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+ */
144129 if ( ! state . nodes . has ( effect . into . identifier ) ) {
145130 state . create ( effect . into , { kind : 'Object' } ) ;
146131 }
@@ -216,10 +201,6 @@ export function inferMutationAliasingRanges(
216201 }
217202 }
218203
219- if ( VERBOSE ) {
220- console . log ( state . debug ( ) ) ;
221- console . log ( pretty ( mutations ) ) ;
222- }
223204 for ( const mutation of mutations ) {
224205 state . mutate (
225206 mutation . index ,
@@ -234,9 +215,6 @@ export function inferMutationAliasingRanges(
234215 for ( const render of renders ) {
235216 state . render ( render . index , render . place . identifier , errors ) ;
236217 }
237- if ( DEBUG ) {
238- console . log ( pretty ( [ ...state . nodes . keys ( ) ] ) ) ;
239- }
240218 fn . aliasingEffects ??= [ ] ;
241219 for ( const param of [ ...fn . context , ...fn . params ] ) {
242220 const place = param . kind === 'Identifier' ? param : param . place ;
@@ -458,9 +436,6 @@ export function inferMutationAliasingRanges(
458436 }
459437 }
460438
461- if ( VERBOSE ) {
462- console . log ( printFunction ( fn ) ) ;
463- }
464439 return errors . asResult ( ) ;
465440}
466441
@@ -511,11 +486,6 @@ class AliasingState {
511486 const fromNode = this . nodes . get ( from . identifier ) ;
512487 const toNode = this . nodes . get ( into . identifier ) ;
513488 if ( fromNode == null || toNode == null ) {
514- if ( VERBOSE ) {
515- console . log (
516- `skip: createFrom ${ printPlace ( from ) } ${ ! ! fromNode } -> ${ printPlace ( into ) } ${ ! ! toNode } ` ,
517- ) ;
518- }
519489 return ;
520490 }
521491 fromNode . edges . push ( { index, node : into . identifier , kind : 'alias' } ) ;
@@ -528,11 +498,6 @@ class AliasingState {
528498 const fromNode = this . nodes . get ( from . identifier ) ;
529499 const toNode = this . nodes . get ( into . identifier ) ;
530500 if ( fromNode == null || toNode == null ) {
531- if ( VERBOSE ) {
532- console . log (
533- `skip: capture ${ printPlace ( from ) } ${ ! ! fromNode } -> ${ printPlace ( into ) } ${ ! ! toNode } ` ,
534- ) ;
535- }
536501 return ;
537502 }
538503 fromNode . edges . push ( { index, node : into . identifier , kind : 'capture' } ) ;
@@ -545,11 +510,6 @@ class AliasingState {
545510 const fromNode = this . nodes . get ( from . identifier ) ;
546511 const toNode = this . nodes . get ( into . identifier ) ;
547512 if ( fromNode == null || toNode == null ) {
548- if ( VERBOSE ) {
549- console . log (
550- `skip: assign ${ printPlace ( from ) } ${ ! ! fromNode } -> ${ printPlace ( into ) } ${ ! ! toNode } ` ,
551- ) ;
552- }
553513 return ;
554514 }
555515 fromNode . edges . push ( { index, node : into . identifier , kind : 'alias' } ) ;
@@ -604,11 +564,6 @@ class AliasingState {
604564 loc : SourceLocation ,
605565 errors : CompilerError ,
606566 ) : void {
607- if ( DEBUG ) {
608- console . log (
609- `mutate ix=${ index } start=$${ start . id } end=[${ end } ]${ transitive ? ' transitive' : '' } kind=${ kind } ` ,
610- ) ;
611- }
612567 const seen = new Set < Identifier > ( ) ;
613568 const queue : Array < {
614569 place : Identifier ;
@@ -623,18 +578,8 @@ class AliasingState {
623578 seen . add ( current ) ;
624579 const node = this . nodes . get ( current ) ;
625580 if ( node == null ) {
626- if ( DEBUG ) {
627- console . log (
628- `no node! ${ printIdentifier ( start ) } for identifier ${ printIdentifier ( current ) } ` ,
629- ) ;
630- }
631581 continue ;
632582 }
633- if ( DEBUG ) {
634- console . log (
635- ` mutate $${ node . id . id } transitive=${ transitive } direction=${ direction } ` ,
636- ) ;
637- }
638583 node . id . mutableRange . end = makeInstructionId (
639584 Math . max ( node . id . mutableRange . end , end ) ,
640585 ) ;
@@ -701,37 +646,5 @@ class AliasingState {
701646 }
702647 }
703648 }
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- }
712649 }
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- } ) ;
737650}
0 commit comments