summaryrefslogtreecommitdiff
path: root/src/main/java/com/seaofnodes/simple/node/StopNode.java
blob: e52a49a2082127326d95285c5bb28d51875d4a67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.seaofnodes.simple.node;

import com.seaofnodes.print.ExprPrinter;

import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.type.*;
import com.seaofnodes.simple.util.BAOS;

import java.util.HashMap;
import java.util.IdentityHashMap;

// A collection of function ReturnNodes for a compilation unit.
public class StopNode extends CFGNode {

    public StopNode() {
        super();
        _type = compute();
    }
    public StopNode(StopNode stop) { super(stop); }
    @Override public Tag serialTag() { return Tag.Stop; }
    @Override public void packed( BAOS baos, HashMap<String,Integer> strs, HashMap<Type,Integer> types, IdentityHashMap<Node, Integer> anodes ) { baos.packed1(nIns()); }
    static Node make( BAOS bais ) {
        StopNode stop = new StopNode();
        stop.setDefX(bais.packed1()-1,null);
        return stop;
    }

    @Override
    protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
        // For the sake of many old tests, and single value prints as "return val"
        ReturnNode ret1 = null;
        for( Node n : _inputs ) {
            if( n instanceof ReturnNode ret ) {
                if( ret1 != null ) { ret1=null; break; }
                ret1 = ret;
            }
        }
        if( ret1!=null ) return p.n(ret1);

        p.p("Stop[ ");
        for( Node n : _inputs )
            if( n instanceof ReturnNode ret ) {
                String name = ret.fun()._name;
                if( name== null || !name.startsWith("sys.") )
                    p.n(ret).p(" ");
            }
        return p.p("]");
    }

    @Override public boolean blockHead() { return true; }


    // If a single Return, return it.
    // Otherwise, null because ambiguous.
    public ReturnNode ret() {
        ReturnNode ret1 = null;
        for( Node n : _inputs ) {
            // TODO: remove isPublic
            if( n instanceof ReturnNode ret && ret.fun().isPublic() ) {
                if( ret1 != null ) return null; // Ambiguous
                ret1 = ret;
            }
        }
        return ret1;
    }

    @Override
    public TypeTuple compute() {
        // During Parsing, new Stops and Returns are being added.  This Stop
        // cannot know if more are coming, so must assume the worst.
        if( CodeGen.CODE._phase ==null || CodeGen.CODE._phase == CodeGen.Phase.Parse )
            return TypeTuple.STATE;

        // Just meet-over-inputs.
        TypeTuple tt = TypeTuple.STOP_HIGH;
        for( Node def : _inputs )
            tt = (TypeTuple)tt.meet(def._type);
        assert tt.mem()._alias==1 && !tt.mem()._one;
        return tt;
    }

    @Override public Node idealize() {
        int len = nIns();
        for( int i=0; i<nIns(); i++ )
            if( in(i).nIns()==0 )
                delDef(i--);
        if( len != nIns() ) return this;
        return null;
    }

    @Override public int idepth() {
        if( validIDepth() ) return _idepth;
        int d=0;
        for( Node ret : _inputs )
            // All public results are hooked on the Stop, including data
            // values, so must check CFG
            if( ret instanceof CFGNode cfg )
                d = Math.max(d,cfg.idepth()+1);
        return cacheIDepth(d);
    }
}