aa
AssertNode.java
Go to the documentation of this file.
1 package com.cliffc.aa.node;
2 
3 import com.cliffc.aa.AA;
4 import com.cliffc.aa.Env;
5 import com.cliffc.aa.GVNGCM;
6 import com.cliffc.aa.Parse;
7 import com.cliffc.aa.type.*;
8 
9 import java.util.Arrays;
10 
11 import static com.cliffc.aa.AA.*;
12 
13 // Assert the matching type. Parse-time error if it does not remove. Note the
14 // difference with CastNode: both Nodes always join their input with their
15 // constant but a TypeNode has to be proven useless and removed before the
16 // program is type-correct. A CastNode is always correct from local semantics,
17 // and the join is non-trivial.
18 public class AssertNode extends Node {
19  private final Type _t; // Asserted type
20  private final Parse _error_parse; // Used for error messages
21  private final Env _env; // Lexical scope
22  public AssertNode( Node mem, Node a, Type t, Parse P, Env e ) {
23  super(OP_TYPE,null,mem,a);
24  assert !(t instanceof TypeFunPtr);
25  _t=t;
26  _error_parse = P;
27  _env = e;
28  }
29  @Override public String xstr() { return "assert:"+_t; }
30  Node mem() { return in(1); }
31  Node arg() { return in(2); }
32 
33  @Override public Node ideal_reduce() {
34  Type actual = arg().sharptr(mem());
35  return actual.isa(_t) ? arg() : null;
36  }
37  @Override public Node ideal_grow() {
38  Node arg= arg();
39  // If TypeNode check is for a function, it will wrap any incoming function
40  // with a new function which does the right arg-checks. This happens
41  // immediately in the Parser and is here to declutter the Parser.
42  if( _t instanceof TypeFunSig ) {
43  TypeFunSig sig = (TypeFunSig)_t;
44  try(GVNGCM.Build<Node> X = Env.GVN.new Build<>()) {
45  X.add(this);
46  Node[] args = new Node[sig.nargs()+1];
47  FunNode fun = (FunNode)X.init(new FunNode(null,sig,-1,false).add_def(Env.ALL_CTRL));
48  fun._val = Type.CTRL;
49  args[CTL_IDX] = fun; // Call control
50  args[MEM_IDX] = X.xform(new ParmNode(MEM_IDX,"mem" ,fun,TypeMem.MEM,Env.DEFMEM,null));
51  args[DSP_IDX] = X.xform(new ParmNode(DSP_IDX,"disp",fun,(ConNode)Node.con(TypeMemPtr.DISP_SIMPLE),_error_parse));
52  for( int i=ARG_IDX; i<sig.nargs(); i++ ) // 1 is memory, 2 is display.
53  // All the parms; types in the function signature
54  args[i] = X.xform(new ParmNode(i,"arg"+i,fun,(ConNode)Node.con(Type.SCALAR),_error_parse));
55  args[sig.nargs()] = arg; // The whole TFP to the call
56  Parse[] badargs = new Parse[sig.nargs()];
57  Arrays.fill(badargs,_error_parse);
58  Node rpc= X.xform(new ParmNode(0,"rpc",fun,Env.ALL_CALL,null));
59  CallNode call = (CallNode)X.xform(new CallNode(true,badargs,args));
60  Node cepi = X.xform(new CallEpiNode(/*TODO: Suspect need to carry a prior Env thru*/call,Env.DEFMEM));
61  Node ctl = X.xform(new CProjNode(cepi));
62  Node postmem= X.xform(new MProjNode(cepi));
63  Node val = X.xform(new ProjNode(cepi,AA.REZ_IDX));
64  // Type-check the return also
65  Node chk = X.xform(new AssertNode(postmem,val,sig._ret.at(REZ_IDX),_error_parse,_env));
66  RetNode ret = (RetNode)X.xform(new RetNode(ctl,postmem,chk,rpc,fun));
67  // Just the same Closure when we make a new TFP
68  Node clos = X.xform(new FP2DispNode(arg));
69  return (X._ret=X.xform(new FunPtrNode(ret,clos)));
70  }
71  }
72 
73  // Push TypeNodes 'up' to widen the space they apply to, and hopefully push
74  // the type check closer to the source of a conflict.
75  Node fun = arg.in(0);
76  if( arg instanceof PhiNode &&
77  // Not allowed to push up the typing on the unknown arg... because
78  // unknown new callers also need the check.
79 
80  // TODO: Probably not legit for FunNodes ever, because have to match
81  // the CallNode args with the FunNode args. Or need to add the very
82  // same node as the matching call arg. Alternatively, simplify the
83  // double-edge game going on with CG edges.
84  //(!(fun instanceof FunNode) || !((FunNode)fun).has_unknown_callers()) ) {
85  fun.getClass() == RegionNode.class ) {
86  //for( int i=1; i<arg._defs._len; i++ )
87  // gvn.set_def_reg(arg,i,gvn.xform(new TypeNode(_t,arg.in(i),_error_parse)));
88  //return arg; // Remove TypeNode, since completely replaced
89  throw AA.unimpl("untested");
90  }
91 
92  return null;
93  }
94 
95  @Override public Type value(GVNGCM.Mode opt_mode) {
96  Node arg = arg();
97  Type t1 = arg._val;
98  Type t0 = _t.simple_ptr();
99  if( t1.isa(t0) ) {
100  Type actual = arg.sharptr(mem());
101  if( actual.isa(_t) )
102  return t1;
103  }
104  // Value is capped to the assert value.
105  return t1.oob(t0);
106  }
107  @Override public void add_flow_use_extra(Node chg) {
108  if( ideal_reduce()!=null )
109  Env.GVN.add_reduce(this);
110  }
111 
112  @Override public TypeMem live_use(GVNGCM.Mode opt_mode, Node def ) {
113  if( def==arg() ) return _live; // Alive as I am
114  // Alive (like normal liveness), plus the address, plus whatever can be
115  // reached from the address.
116  return ScopeNode.compute_live_mem(mem(),arg());
117  }
118 
119  // Check TypeNode for being in-error
120  @Override public ErrMsg err( boolean fast ) {
121  Type arg = arg()._val;
122  Type mem = mem()._val;
124  }
125 }
com.cliffc.aa.node.AssertNode._error_parse
final Parse _error_parse
Definition: AssertNode.java:20
com.cliffc.aa.type.TypeFunPtr
Definition: TypeFunPtr.java:23
com.cliffc.aa.type.Type.isa
boolean isa(Type t)
Definition: Type.java:623
com.cliffc.aa.type.TypeMem
Memory type; the state of all of memory; memory edges order memory ops.
Definition: TypeMem.java:53
com.cliffc.aa.node.Node._live
TypeMem _live
Definition: Node.java:89
com.cliffc.aa.type.Type.SCALAR
static final Type SCALAR
Definition: Type.java:328
com.cliffc
com.cliffc.aa.Env.ALL_CALL
static ConNode ALL_CALL
Definition: Env.java:26
com.cliffc.aa.node.ScopeNode
Definition: ScopeNode.java:17
com.cliffc.aa.node.Node
Definition: Node.java:16
com.cliffc.aa.node.FP2DispNode
Definition: FP2DispNode.java:7
com.cliffc.aa.type.Type
an implementation of language AA
Definition: Type.java:94
Arrays
com.cliffc.aa.node.PhiNode
Definition: PhiNode.java:9
com.cliffc.aa.node.CProjNode
Definition: CProjNode.java:10
com.cliffc.aa.node.Node._val
Type _val
Definition: Node.java:88
com.cliffc.aa.node.Node.ErrMsg
Definition: Node.java:888
com.cliffc.aa.node.ConNode
Definition: ConNode.java:9
com.cliffc.aa.node.Node.add_def
Node add_def(Node n)
Definition: Node.java:152
com.cliffc.aa.node.FunPtrNode
Definition: FunPtrNode.java:40
com.cliffc.aa.node.RetNode
Definition: RetNode.java:19
com.cliffc.aa.node.CallNode
Definition: CallNode.java:86
com.cliffc.aa.AA.unimpl
static RuntimeException unimpl()
Definition: AA.java:10
com.cliffc.aa.node.MProjNode
Definition: MProjNode.java:10
com.cliffc.aa.type.TypeFunSig._ret
TypeTuple _ret
Definition: TypeFunSig.java:16
com.cliffc.aa.node.AssertNode
Definition: AssertNode.java:18
com.cliffc.aa.Env.GVN
static final GVNGCM GVN
Definition: Env.java:13
com.cliffc.aa.type.TypeFunSig.nargs
int nargs()
Definition: TypeFunSig.java:87
com.cliffc.aa.type.TypeMemPtr.DISP_SIMPLE
static final TypeMemPtr DISP_SIMPLE
Definition: TypeMemPtr.java:105
com.cliffc.aa.node.AssertNode.add_flow_use_extra
void add_flow_use_extra(Node chg)
Definition: AssertNode.java:107
com.cliffc.aa.AA.REZ_IDX
static final int REZ_IDX
Definition: AA.java:16
com.cliffc.aa.type.Type.CTRL
static final Type CTRL
Definition: Type.java:326
com.cliffc.aa.node.AssertNode.ideal_grow
Node ideal_grow()
Definition: AssertNode.java:37
com.cliffc.aa.node.Node.in
Node in(int i)
Definition: Node.java:126
com.cliffc.aa.GVNGCM
Definition: GVNGCM.java:12
com.cliffc.aa.node.Node.ErrMsg.asserterr
static ErrMsg asserterr(Parse loc, Type actual, Type t0mem, Type expected)
Definition: Node.java:926
com.cliffc.aa.type.Type.simple_ptr
Type simple_ptr()
Definition: Type.java:358
com.cliffc.aa.node.ProjNode
Definition: ProjNode.java:11
com.cliffc.aa.node.Node.con
static Node con(Type t)
Definition: Node.java:670
com.cliffc.aa.AA
an implementation of language AA
Definition: AA.java:9
com.cliffc.aa.node.Node.val
Type val(int idx)
Definition: Node.java:470
com.cliffc.aa
Definition: AA.java:1
com.cliffc.aa.node.ScopeNode.compute_live_mem
static TypeMem compute_live_mem(Node mem, Node rez)
Definition: ScopeNode.java:130
com.cliffc.aa.node.AssertNode.ideal_reduce
Node ideal_reduce()
Definition: AssertNode.java:33
com.cliffc.aa.type.TypeTuple.at
Type at(int idx)
Definition: TypeTuple.java:182
com.cliffc.aa.node.AssertNode.mem
Node mem()
Definition: AssertNode.java:30
com.cliffc.aa.node.Node.OP_TYPE
static final byte OP_TYPE
Definition: Node.java:50
com.cliffc.aa.type.TypeFunSig
Definition: TypeFunSig.java:10
com.cliffc.aa.node.Node.sharptr
Type sharptr(Node mem)
Definition: Node.java:855
com.cliffc.aa.GVNGCM.add_reduce
public< N extends Node > N add_reduce(N n)
Definition: GVNGCM.java:49
com.cliffc.aa.node.CallEpiNode
Definition: CallEpiNode.java:24
com.cliffc.aa.node.ParmNode
Definition: ParmNode.java:14
com.cliffc.aa.type.Type.oob
Type oob()
Definition: Type.java:635
com.cliffc.aa.node.AssertNode.AssertNode
AssertNode(Node mem, Node a, Type t, Parse P, Env e)
Definition: AssertNode.java:22
com.cliffc.aa.GVNGCM.Build
Definition: GVNGCM.java:356
com.cliffc.aa.node.AssertNode.live_use
TypeMem live_use(GVNGCM.Mode opt_mode, Node def)
Definition: AssertNode.java:112
com.cliffc.aa.node.AssertNode.xstr
String xstr()
Definition: AssertNode.java:29
com.cliffc.aa.node.AssertNode.err
ErrMsg err(boolean fast)
Definition: AssertNode.java:120
com.cliffc.aa.node.FunNode
Definition: FunNode.java:58
com.cliffc.aa.node.AssertNode._t
final Type _t
Definition: AssertNode.java:19
com.cliffc.aa.node.RegionNode
Definition: RegionNode.java:11
com.cliffc.aa.node.AssertNode.value
Type value(GVNGCM.Mode opt_mode)
Definition: AssertNode.java:95
com
com.cliffc.aa.type.TypeMem.MEM
static final TypeMem MEM
Definition: TypeMem.java:224
com.cliffc.aa.Env.DEFMEM
static DefMemNode DEFMEM
Definition: Env.java:19
com.cliffc.aa.Env
Definition: Env.java:12
com.cliffc.aa.node.AssertNode.arg
Node arg()
Definition: AssertNode.java:31
com.cliffc.aa.type
Definition: Bits.java:1
com.cliffc.aa.type.TypeMemPtr
Definition: TypeMemPtr.java:14
com.cliffc.aa.Parse
an implementation of language AA
Definition: Parse.java:68
com.cliffc.aa.Env.ALL_CTRL
static ConNode ALL_CTRL
Definition: Env.java:20
com.cliffc.aa.node.AssertNode._env
final Env _env
Definition: AssertNode.java:21
com.cliffc.aa.GVNGCM.Mode
Definition: GVNGCM.java:14